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

paulmthompson / WhiskerToolbox / 14759446314

30 Apr 2025 04:26PM UTC coverage: 18.699% (+1.7%) from 16.999%
14759446314

push

github

paulmthompson
fix test errors

319 of 1706 relevant lines covered (18.7%)

1.87 hits per line

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

4.29
/src/WhiskerToolbox/DataManager/DataManager.cpp
1

2
#include "DataManager.hpp"
3
#include "AnalogTimeSeries/Analog_Time_Series.hpp"
4
#include "DigitalTimeSeries/Digital_Event_Series.hpp"
5
#include "DigitalTimeSeries/Digital_Interval_Series.hpp"
6
#include "Lines/Line_Data.hpp"
7
#include "Masks/Mask_Data.hpp"
8
#include "Media/Video_Data.hpp"
9
#include "Points/Point_Data.hpp"
10
#include "Tensors/Tensor_Data.hpp"
11

12
#include "AnalogTimeSeries/Analog_Time_Series_Loader.hpp"
13
#include "DigitalTimeSeries/Digital_Event_Series_Loader.hpp"
14
#include "DigitalTimeSeries/Digital_Interval_Series_Loader.hpp"
15
#include "Masks/Mask_Data_Loader.hpp"
16
#include "Media/Video_Data_Loader.hpp"
17
#include "Points/Point_Data_Loader.hpp"
18
#include "Lines/Line_Data_Loader.hpp"
19

20
#include "loaders/binary_loaders.hpp"
21
#include "transforms/data_transforms.hpp"
22
#include "transforms/Masks/mask_area.hpp"
23

24
#include "TimeFrame.hpp"
25

26
#include "nlohmann/json.hpp"
27
#include "utils/string_manip.hpp"
28

29
#include <filesystem>
30
#include <fstream>
31
#include <iostream>
32
#include <optional>
33
#include <regex>
34

35
using namespace nlohmann;
36

37
DataManager::DataManager() {
2✔
38
    _times["time"] = std::make_shared<TimeFrame>();
6✔
39
    _data["media"] = std::make_shared<MediaData>();
6✔
40

41
    setTimeFrame("media", "time");
10✔
42
    _output_path = std::filesystem::current_path();
2✔
43
}
2✔
44

45
void DataManager::setTimeFrame(std::string const & data_key, std::string const & time_key) {
2✔
46
    //Check that data_key is in _data
47
    if (_data.find(data_key) == _data.end()) {
2✔
48
        std::cerr << "Data key not found in DataManager: " << data_key << std::endl;
×
49
        return;
×
50
    }
51

52
    //Check that time_key is in _times
53
    if (_times.find(time_key) == _times.end()) {
2✔
54
        std::cerr << "Time key not found in DataManager: " << time_key << std::endl;
×
55
        return;
×
56
    }
57

58
    _time_frames[data_key] = time_key;
2✔
59
}
60

61
enum class DataType {
62
    Video,
63
    Points,
64
    Mask,
65
    Line,
66
    Analog,
67
    DigitalEvent,
68
    DigitalInterval,
69
    Tensor,
70
    Time,
71
    Unknown
72
};
73

74
DataType stringToDataType(std::string const & data_type_str) {
×
75
    if (data_type_str == "video") return DataType::Video;
×
76
    if (data_type_str == "points") return DataType::Points;
×
77
    if (data_type_str == "mask") return DataType::Mask;
×
78
    if (data_type_str == "line") return DataType::Line;
×
79
    if (data_type_str == "analog") return DataType::Analog;
×
80
    if (data_type_str == "digital_event") return DataType::DigitalEvent;
×
81
    if (data_type_str == "digital_interval") return DataType::DigitalInterval;
×
82
    if (data_type_str == "tensor") return DataType::Tensor;
×
83
    if (data_type_str == "time") return DataType::Time;
×
84
    return DataType::Unknown;
×
85
}
86

87
std::optional<std::string> processFilePath(
×
88
        std::string const & file_path,
89
        std::filesystem::path const & base_path) {
90
    std::filesystem::path full_path = file_path;
×
91

92
    // Check for wildcard character
93
    if (file_path.find('*') != std::string::npos) {
×
94
        // Convert wildcard pattern to regex
95
        std::string const pattern = std::regex_replace(full_path.string(), std::regex("\\*"), ".*");
×
96
        std::regex const regex_pattern(pattern);
×
97

98
        // Iterate through the directory to find matching files
99
        for (auto const & entry: std::filesystem::directory_iterator(base_path)) {
×
100
            std::cout << "Checking " << entry.path().string() << " with full path " << full_path << std::endl;
×
101
            if (std::regex_match(entry.path().string(), regex_pattern)) {
×
102
                std::cout << "Loading file " << entry.path().string() << std::endl;
×
103
                return entry.path().string();
×
104
            }
105
        }
×
106
        return std::nullopt;
×
107
    } else {
×
108
        // Check if the file path is relative
109
        if (!std::filesystem::path(file_path).is_absolute()) {
×
110
            full_path = base_path / file_path;
×
111
        }
112
        // Check for the presence of the file
113
        if (std::filesystem::exists(full_path)) {
×
114
            std::cout << "Loading file " << full_path.string() << std::endl;
×
115
            return full_path.string();
×
116
        } else {
117
            return std::nullopt;
×
118
        }
119
    }
120
}
×
121

122
bool checkRequiredFields(json const & item, std::vector<std::string> const & requiredFields) {
×
123
    for (auto const & field: requiredFields) {
×
124
        if (!item.contains(field)) {
×
125
            std::cerr << "Error: Missing required field \"" << field << "\" in JSON item." << std::endl;
×
126
            return false;
×
127
        }
128
    }
129
    return true;
×
130
}
131

132
int DataManager::addCallbackToData(std::string const & key, ObserverCallback callback) {
×
133

134
    int id = -1;
×
135

136
    if (_data.find(key) != _data.end()) {
×
137
        auto data = _data[key];
×
138

139
        id = std::visit([callback](auto & x) {
×
140
            return x.get()->addObserver(callback);
×
141
        },
142
                        data);
143
    }
×
144

145
    return id;
×
146
}
147

148
void DataManager::removeCallbackFromData(std::string const & key, int callback_id) {
×
149
    if (_data.find(key) != _data.end()) {
×
150
        auto data = _data[key];
×
151

152
        std::visit([callback_id](auto & x) {
×
153
            x.get()->removeObserver(callback_id);
×
154
        },
×
155
                   data);
156
    }
×
157
}
×
158

159
void checkOptionalFields(json const & item, std::vector<std::string> const & optionalFields) {
×
160
    for (auto const & field: optionalFields) {
×
161
        if (!item.contains(field)) {
×
162
            std::cout << "Warning: Optional field \"" << field << "\" is missing in JSON item." << std::endl;
×
163
        }
164
    }
165
}
×
166

167
std::vector<DataInfo> load_data_from_json_config(DataManager * dm, std::string const & json_filepath) {
×
168
    std::vector<DataInfo> data_info_list;
×
169
    // Open JSON file
170
    std::ifstream ifs(json_filepath);
×
171
    if (!ifs.is_open()) {
×
172
        std::cerr << "Failed to open JSON file: " << json_filepath << std::endl;
×
173
        return data_info_list;
×
174
    }
175

176
    // Parse JSON
177
    json j;
×
178
    ifs >> j;
×
179

180
    // get base path of filepath
181
    std::filesystem::path const base_path = std::filesystem::path(json_filepath).parent_path();
×
182

183
    // Iterate through JSON array
184
    for (auto const & item: j) {
×
185

186
        if (!checkRequiredFields(item, {"data_type", "name", "filepath"})) {
×
187
            continue;// Exit if any required field is missing
×
188
        }
189

190
        std::string const data_type_str = item["data_type"];
×
191
        DataType const data_type = stringToDataType(data_type_str);
×
192
        if (data_type == DataType::Unknown) {
×
193
            std::cout << "Unknown data type: " << data_type_str << std::endl;
×
194
            continue;
×
195
        }
196

197
        std::string const name = item["name"];
×
198

199
        auto file_exists = processFilePath(item["filepath"], base_path);
×
200
        if (!file_exists) {
×
201
            std::cerr << "File does not exist: " << item["filepath"] << std::endl;
×
202
            continue;
×
203
        }
204

205
        std::string const file_path = file_exists.value();
×
206

207
        switch (data_type) {
×
208
            case DataType::Video: {
×
209
                // Create VideoData object
210
                auto video_data = load_video_into_VideoData(file_path);
×
211

212
                // Add VideoData to DataManager
213
                dm->setMedia(video_data);
×
214

215
                data_info_list.push_back({name, "VideoData", ""});
×
216
                break;
×
217
            }
×
218
            case DataType::Points: {
×
219

220
                auto point_data = load_into_PointData(file_path, item);
×
221

222
                dm->setData<PointData>(name, point_data);
×
223

224
                std::string const color = item.value("color", "#0000FF");
×
225
                data_info_list.push_back({name, "PointData", color});
×
226
                break;
×
227
            }
×
228
            case DataType::Mask: {
×
229

230
                auto mask_data = load_into_MaskData(file_path, item);
×
231

232
                std::string const color = item.value("color", "0000FF");
×
233
                dm->setData<MaskData>(name, mask_data);
×
234

235
                data_info_list.push_back({name, "MaskData", color});
×
236

237
                if (item.contains("operations")) {
×
238

239
                    for (auto const & operation: item["operations"]) {
×
240

241
                        std::string const operation_type = operation["type"];
×
242

243
                        if (operation_type == "area") {
×
244
                            std::cout << "Calculating area for mask: " << name << std::endl;
×
245
                            auto area_data = area(dm->getData<MaskData>(name));
×
246
                            std::string const output_name = name + "_area";
×
247
                            dm->setData<AnalogTimeSeries>(output_name, area_data);
×
248
                        }
×
249
                    }
×
250
                }
251
                break;
×
252
            }
×
253
            case DataType::Line: {
×
254

255
                auto line_map = load_line_csv(file_path);
×
256

257
                //Get the whisker name from the filename using filesystem
258
                auto whisker_filename = std::filesystem::path(file_path).filename().string();
×
259

260
                //Remove .csv suffix from filename
261
                auto whisker_name = remove_extension(whisker_filename);
×
262

263
                dm->setData<LineData>(whisker_name, std::make_shared<LineData>(line_map));
×
264

265
                std::string const color = item.value("color", "0000FF");
×
266

267
                data_info_list.push_back({name, "LineData", color});
×
268

269
                break;
×
270
            }
×
271
            case DataType::Analog: {
×
272

273
                auto analog_time_series = load_into_AnalogTimeSeries(file_path, item);
×
274

275
                for (int channel = 0; channel < analog_time_series.size(); channel++) {
×
276
                    std::string const channel_name = name + "_" + std::to_string(channel);
×
277

278
                    dm->setData<AnalogTimeSeries>(channel_name, analog_time_series[channel]);
×
279

280
                    if (item.contains("clock")) {
×
281
                        std::string const clock = item["clock"];
×
282
                        dm->setTimeFrame(channel_name, clock);
×
283
                    }
×
284
                }
×
285
                break;
×
286
            }
×
287
            case DataType::DigitalEvent: {
×
288

289
                auto digital_event_series = load_into_DigitalEventSeries(file_path, item);
×
290

291
                for (int channel = 0; channel < digital_event_series.size(); channel++) {
×
292
                    std::string const channel_name = name + "_" + std::to_string(channel);
×
293

294
                    dm->setData<DigitalEventSeries>(channel_name, digital_event_series[channel]);
×
295

296
                    if (item.contains("clock")) {
×
297
                        std::string const clock = item["clock"];
×
298
                        dm->setTimeFrame(channel_name, clock);
×
299
                    }
×
300
                }
×
301
                break;
×
302
            }
×
303
            case DataType::DigitalInterval: {
×
304

305
                auto digital_interval_series = load_into_DigitalIntervalSeries(file_path, item);
×
306
                dm->setData<DigitalIntervalSeries>(name, digital_interval_series);
×
307

308
                break;
×
309
            }
×
310
            case DataType::Tensor: {
×
311

312
                if (item["format"] == "numpy") {
×
313

314
                    TensorData tensor_data;
×
315
                    loadNpyToTensorData(file_path, tensor_data);
×
316

317
                    dm->setData<TensorData>(name, std::make_shared<TensorData>(tensor_data));
×
318

319
                } else {
×
320
                    std::cout << "Format " << item["format"] << " not found for " << name << std::endl;
×
321
                }
322
                break;
×
323
            }
324
            case DataType::Time: {
×
325

326
                if (item["format"] == "uint16") {
×
327

328
                    int const channel = item["channel"];
×
329
                    std::string const transition = item["transition"];
×
330

331
                    int const header_size = item.value("header_size", 0);
×
332

333
                    auto opts = Loader::BinaryAnalogOptions{.file_path = file_path,
×
334
                                                            .header_size_bytes = static_cast<size_t>(header_size)};
×
335
                    auto data = readBinaryFile<uint16_t>(opts);
×
336

337
                    auto digital_data = Loader::extractDigitalData(data, channel);
×
338
                    auto events = Loader::extractEvents(digital_data, transition);
×
339

340
                    // convert to int with std::transform
341
                    std::vector<int> events_int;
×
342
                    events_int.reserve(events.size());
×
343
                    for (auto e: events) {
×
344
                        events_int.push_back(static_cast<int>(e));
×
345
                    }
346
                    std::cout << "Loaded " << events_int.size() << " events for " << name << std::endl;
×
347

348
                    auto timeframe = std::make_shared<TimeFrame>(events_int);
×
349
                    dm->setTime(name, timeframe);
×
350
                }
×
351

352
                if (item["format"] == "uint16_length") {
×
353

354
                    int const header_size = item.value("header_size", 0);
×
355

356
                    auto opts = Loader::BinaryAnalogOptions{.file_path = file_path,
×
357
                                                            .header_size_bytes = static_cast<size_t>(header_size)};
×
358
                    auto data = readBinaryFile<uint16_t>(opts);
×
359

360
                    std::vector<int> t(data.size());
×
361
                    std::iota(std::begin(t), std::end(t), 0);
×
362

363
                    std::cout << "Total of " << t.size() << " timestamps for " << name << std::endl;
×
364

365
                    auto timeframe = std::make_shared<TimeFrame>(t);
×
366
                    dm->setTime(name, timeframe);
×
367
                }
×
368
                break;
×
369
            }
370
            default:
×
371
                std::cout << "Unsupported data type: " << data_type_str << std::endl;
×
372
                continue;
×
373
        }
×
374
        if (item.contains("clock")) {
×
375
            std::string const clock = item["clock"];
×
376
            std::cout << "Setting time for " << name << " to " << clock << std::endl;
×
377
            dm->setTimeFrame(name, clock);
×
378
        }
×
379
    }
×
380

381
    return data_info_list;
382
}
×
383

384
std::string DataManager::getType(std::string const & key) const {
×
385
    auto it = _data.find(key);
×
386
    if (it != _data.end()) {
×
387
        if (std::holds_alternative<std::shared_ptr<MediaData>>(it->second)) {
×
388
            return "MediaData";
×
389
        } else if (std::holds_alternative<std::shared_ptr<PointData>>(it->second)) {
×
390
            return "PointData";
×
391
        } else if (std::holds_alternative<std::shared_ptr<LineData>>(it->second)) {
×
392
            return "LineData";
×
393
        } else if (std::holds_alternative<std::shared_ptr<MaskData>>(it->second)) {
×
394
            return "MaskData";
×
395
        } else if (std::holds_alternative<std::shared_ptr<AnalogTimeSeries>>(it->second)) {
×
396
            return "AnalogTimeSeries";
×
397
        } else if (std::holds_alternative<std::shared_ptr<DigitalEventSeries>>(it->second)) {
×
398
            return "DigitalEventSeries";
×
399
        } else if (std::holds_alternative<std::shared_ptr<DigitalIntervalSeries>>(it->second)) {
×
400
            return "DigitalIntervalSeries";
×
401
        } else if (std::holds_alternative<std::shared_ptr<TensorData>>(it->second)) {
×
402
            return "TensorData";
×
403
        }
404
        return "Unknown";
×
405
    }
406
}
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