• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

paulmthompson / WhiskerToolbox / 16080442806

04 Jul 2025 08:10PM UTC coverage: 75.191% (+0.6%) from 74.575%
16080442806

push

github

paulmthompson
remove concrete filters header

14051 of 18687 relevant lines covered (75.19%)

1040.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/src/WhiskerToolbox/DataManager/transforms/AnalogTimeSeries/analog_filter.cpp
1
#include "analog_filter.hpp"
2
#include "AnalogTimeSeries/Analog_Time_Series.hpp"
3
#include "utils/filter/new_filter_bridge.hpp"
4
#include "utils/filter/FilterFactory.hpp"
5

6
#include <stdexcept>
7

8
std::shared_ptr<AnalogTimeSeries> filter_analog(
11✔
9
        AnalogTimeSeries const * analog_time_series,
10
        AnalogFilterParams const & filterParams) {
11
    return filter_analog(analog_time_series, filterParams, [](int) {});
11✔
12
}
13

14
std::shared_ptr<AnalogTimeSeries> filter_analog(
12✔
15
        AnalogTimeSeries const * analog_time_series,
16
        AnalogFilterParams const & filterParams,
17
        ProgressCallback progressCallback) {
18
    if (!analog_time_series) {
12✔
19
        throw std::invalid_argument("Input analog time series is null");
×
20
    }
21

22
    // Validate filter parameters
23
    if (!filterParams.isValid()) {
12✔
24
        throw std::invalid_argument("Invalid filter parameters");
×
25
    }
26

27
    // Report initial progress
28
    progressCallback(0);
12✔
29

30
    // Handle different parameter types
31
    if (filterParams.filter_instance) {
12✔
32
        // Use pre-created filter instance
33
        auto result = filterWithInstance(analog_time_series, filterParams.filter_instance);
10✔
34
        progressCallback(100);
10✔
35
        return result;
10✔
36
    } else if (filterParams.filter_factory) {
12✔
37
        // Create filter from factory
38
        auto filter = filterParams.filter_factory();
1✔
39
        auto result = filterWithInstance(analog_time_series, std::move(filter));
1✔
40
        progressCallback(100);
1✔
41
        return result;
1✔
42
    } else if (filterParams.legacy_options.has_value()) {
2✔
43
        // Fall back to legacy FilterOptions
44
        auto result = filterAnalogTimeSeriesNew(analog_time_series, filterParams.legacy_options.value());
1✔
45
        if (!result.success) {
1✔
46
            throw std::runtime_error("Filtering failed: " + result.error_message);
×
47
        }
48
        progressCallback(100);
1✔
49
        return result.filtered_data;
1✔
50
    } else {
1✔
51
        throw std::invalid_argument("No valid filter configuration provided");
×
52
    }
53
}
54

55
// Helper function to filter with a direct filter instance
56
std::shared_ptr<AnalogTimeSeries> filterWithInstance(
11✔
57
        AnalogTimeSeries const * analog_time_series,
58
        std::shared_ptr<IFilter> filter) {
59
    if (!analog_time_series || !filter) {
11✔
60
        throw std::invalid_argument("Input analog time series or filter is null");
×
61
    }
62

63
    // Get all data from the time series
64
    auto & data_span = analog_time_series->getAnalogTimeSeries();
11✔
65
    auto time_series = analog_time_series->getTimeSeries();
11✔
66

67
    if (data_span.empty()) {
11✔
68
        throw std::invalid_argument("No data found in time series");
×
69
    }
70

71
    // Convert to mutable vector for processing
72
    std::vector<float> filtered_data(data_span.begin(), data_span.end());
33✔
73
    std::vector<TimeFrameIndex> filtered_times(time_series.begin(), time_series.end());
33✔
74

75
    // Apply filter
76
    std::span<float> data_span_mutable(filtered_data);
11✔
77
    filter->process(data_span_mutable);
11✔
78

79
    // Create new AnalogTimeSeries with filtered data
80
    return std::make_shared<AnalogTimeSeries>(
81
        std::move(filtered_data),
11✔
82
        std::move(filtered_times));
33✔
83
}
11✔
84

85
// Helper function to filter with a unique_ptr filter instance
86
std::shared_ptr<AnalogTimeSeries> filterWithInstance(
1✔
87
        AnalogTimeSeries const * analog_time_series,
88
        std::unique_ptr<IFilter> filter) {
89
    return filterWithInstance(analog_time_series, std::shared_ptr<IFilter>(std::move(filter)));
2✔
90
}
91

92
std::string AnalogFilterOperation::getName() const {
1✔
93
    return "Filter";
3✔
94
}
95

96
std::type_index AnalogFilterOperation::getTargetInputTypeIndex() const {
1✔
97
    return typeid(std::shared_ptr<AnalogTimeSeries>);
1✔
98
}
99

100
std::unique_ptr<TransformParametersBase> AnalogFilterOperation::getDefaultParameters() const {
1✔
101
    auto params = std::make_unique<AnalogFilterParams>();
1✔
102
    
103
    // Set default filter using legacy options for compatibility (4th order Butterworth lowpass at 100 Hz)
104
    params->legacy_options = FilterDefaults::lowpass(100.0, 1000.0, 4);
1✔
105
    
106
    return params;
2✔
107
}
1✔
108

109
bool AnalogFilterOperation::canApply(DataTypeVariant const & dataVariant) const {
1✔
110
    return std::holds_alternative<std::shared_ptr<AnalogTimeSeries>>(dataVariant);
1✔
111
}
112

113
DataTypeVariant AnalogFilterOperation::execute(
×
114
        DataTypeVariant const & dataVariant,
115
        TransformParametersBase const * params) {
116
    return execute(dataVariant, params, [](int) {});
×
117
}
118

119
DataTypeVariant AnalogFilterOperation::execute(
1✔
120
        DataTypeVariant const & dataVariant,
121
        TransformParametersBase const * params,
122
        ProgressCallback progressCallback) {
123
    if (!params) {
1✔
124
        throw std::invalid_argument("Filter parameters are null");
×
125
    }
126

127
    auto const * filterParams = dynamic_cast<AnalogFilterParams const *>(params);
1✔
128
    if (!filterParams) {
1✔
129
        throw std::invalid_argument("Invalid parameter type for filter operation");
×
130
    }
131

132
    auto const * analog_time_series = std::get_if<std::shared_ptr<AnalogTimeSeries>>(&dataVariant);
1✔
133
    if (!analog_time_series || !*analog_time_series) {
1✔
134
        throw std::invalid_argument("Invalid input data type or null pointer");
×
135
    }
136

137
    auto filtered_data = filter_analog(analog_time_series->get(), *filterParams, progressCallback);
1✔
138
    return DataTypeVariant(filtered_data);
2✔
139
} 
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc