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

paulmthompson / WhiskerToolbox / 15046951426

15 May 2025 02:00PM UTC coverage: 23.832% (+0.02%) from 23.816%
15046951426

push

github

paulmthompson
fix includes in test files

561 of 2354 relevant lines covered (23.83%)

2.96 hits per line

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

24.21
/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/Media_Data.hpp"
9
#include "Media/Video_Data.hpp"
10
#include "Points/Point_Data.hpp"
11
#include "Tensors/Tensor_Data.hpp"
12

13
#include "AnalogTimeSeries/Analog_Time_Series_Loader.hpp"
14
#include "DigitalTimeSeries/Digital_Event_Series_Loader.hpp"
15
#include "DigitalTimeSeries/Digital_Interval_Series_Loader.hpp"
16
#include "Lines/IO/CSV/Line_Data_CSV.hpp"
17
#include "Masks/IO/HDF5/Mask_Data_HDF5.hpp"
18
#include "Media/Video_Data_Loader.hpp"
19
#include "Points/IO/CSV/Point_Data_CSV.hpp"
20

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

25
#include "TimeFrame.hpp"
26

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

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

36
using namespace nlohmann;
37

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

42
    setTimeFrame("media", "time");
195✔
43
    _output_path = std::filesystem::current_path();
39✔
44
}
39✔
45

46
bool DataManager::setTime(std::string const & key, std::shared_ptr<TimeFrame> timeframe) {
16✔
47

48
    if (!timeframe) {
16✔
49
        std::cerr << "Error: Cannot register a nullptr TimeFrame for key: " << key << std::endl;
1✔
50
        return false;
1✔
51
    }
52

53
    if (_times.find(key) != _times.end()) {
15✔
54
        std::cerr << "Error: Time key already exists in DataManager: " << key << std::endl;
2✔
55
        return false;
2✔
56
    }
57

58
    _times[key] = std::move(timeframe);
13✔
59
    return true;
13✔
60
}
61

62
std::shared_ptr<TimeFrame>  DataManager::getTime() {
1✔
63
    return _times["time"];
3✔
64
};
65

66
std::shared_ptr<TimeFrame> DataManager::getTime(std::string const & key) {
7✔
67
    if (_times.find(key) != _times.end()) {
7✔
68
        return _times[key];
5✔
69
    }
70
    return nullptr;
2✔
71
};
72

73
bool DataManager::removeTime(std::string const & key)
×
74
{
75
    if (_times.find(key) == _times.end()) {
×
76
        std::cerr << "Error: could not find time key in DataManager: " << key << std::endl;
×
77
        return false;
×
78
    }
79

80
    auto it = _times.find(key);
×
81
    _times.erase(it);
×
82
    return true;
×
83
}
84

85
bool DataManager::setTimeFrame(std::string const & data_key, std::string const & time_key) {
78✔
86
    if (_data.find(data_key) == _data.end()) {
78✔
87
        std::cerr << "Error: Data key not found in DataManager: " << data_key << std::endl;
1✔
88
        return false;
1✔
89
    }
90

91
    if (_times.find(time_key) == _times.end()) {
77✔
92
        std::cerr << "Error: Time key not found in DataManager: " << time_key << std::endl;
1✔
93
        return false;
1✔
94
    }
95

96
    _time_frames[data_key] = time_key;
76✔
97
    return true;
76✔
98
}
99

100
std::string DataManager::getTimeFrame(std::string const & data_key) {
6✔
101
    // check if data_key exists
102
    if (_data.find(data_key) == _data.end()) {
6✔
103
        std::cerr << "Error: Data key not found in DataManager: " << data_key << std::endl;
1✔
104
        return "";
3✔
105
    }
106

107
    // check if data key has time frame
108
    if (_time_frames.find(data_key) == _time_frames.end()) {
5✔
109
        std::cerr << "Error: Data key "
110
                  << data_key
111
                  << " exists, but not assigned to a TimeFrame" <<  std::endl;
×
112
        return "";
×
113
    }
114

115
    return _time_frames[data_key];
5✔
116
}
117

118
std::vector<std::string> DataManager::getTimeFrameKeys() {
8✔
119
    std::vector<std::string> keys;
8✔
120
    keys.reserve(_times.size());
8✔
121
    for (auto const & [key, value]: _times) {
24✔
122

123
        keys.push_back(key);
16✔
124
    }
125
    return keys;
8✔
126
}
×
127

128
int DataManager::addCallbackToData(std::string const & key, ObserverCallback callback) {
7✔
129

130
    int id = -1;
7✔
131

132
    if (_data.find(key) != _data.end()) {
7✔
133
        auto data = _data[key];
6✔
134

135
        id = std::visit([callback](auto & x) {
12✔
136
            return x.get()->addObserver(callback);
6✔
137
        }, data);
138
    }
6✔
139

140
    return id;
7✔
141
}
142

143
bool DataManager::removeCallbackFromData(std::string const & key, int callback_id) {
4✔
144
    if (_data.find(key) != _data.end()) {
4✔
145
        auto data = _data[key];
3✔
146

147
        std::visit([callback_id](auto & x) {
6✔
148
            x.get()->removeObserver(callback_id);
3✔
149
        }, data);
3✔
150

151
        return true;
3✔
152
    }
3✔
153

154
    return false;
1✔
155
}
156

157
void DataManager::addObserver(ObserverCallback callback) {
5✔
158
    _observers.push_back(std::move(callback));
5✔
159
}
5✔
160

161
void DataManager::_notifyObservers() {
33✔
162
    for (auto & observer: _observers) {
41✔
163
        observer();
8✔
164
    }
165
}
33✔
166

167
std::vector<std::string> DataManager::getAllKeys() {
5✔
168
    std::vector<std::string> keys;
5✔
169
    keys.reserve(_data.size());
5✔
170
    for (auto const & [key, value]: _data) {
17✔
171

172
        keys.push_back(key);
12✔
173
    }
174
    return keys;
5✔
175
}
×
176

177
std::optional<DataTypeVariant> DataManager::getDataVariant(std::string const & key) {
×
178
    if (_data.find(key) != _data.end()) {
×
179
        return _data[key];
×
180
    }
181
    return std::nullopt;
×
182
}
183

184
void DataManager::setData(std::string const & key, DataTypeVariant data) {
1✔
185
    _data[key] = data;
1✔
186
    setTimeFrame(key, "time");
3✔
187
    _notifyObservers();
1✔
188
}
1✔
189

190
std::optional<std::string> processFilePath(
×
191
        std::string const & file_path,
192
        std::filesystem::path const & base_path) {
193
    std::filesystem::path full_path = file_path;
×
194

195
    // Check for wildcard character
196
    if (file_path.find('*') != std::string::npos) {
×
197
        // Convert wildcard pattern to regex
198
        std::string const pattern = std::regex_replace(full_path.string(), std::regex("\\*"), ".*");
×
199
        std::regex const regex_pattern(pattern);
×
200

201
        // Iterate through the directory to find matching files
202
        for (auto const & entry: std::filesystem::directory_iterator(base_path)) {
×
203
            std::cout << "Checking " << entry.path().string() << " with full path " << full_path << std::endl;
×
204
            if (std::regex_match(entry.path().string(), regex_pattern)) {
×
205
                std::cout << "Loading file " << entry.path().string() << std::endl;
×
206
                return entry.path().string();
×
207
            }
208
        }
×
209
        return std::nullopt;
×
210
    } else {
×
211
        // Check if the file path is relative
212
        if (!std::filesystem::path(file_path).is_absolute()) {
×
213
            full_path = base_path / file_path;
×
214
        }
215
        // Check for the presence of the file
216
        if (std::filesystem::exists(full_path)) {
×
217
            std::cout << "Loading file " << full_path.string() << std::endl;
×
218
            return full_path.string();
×
219
        } else {
220
            return std::nullopt;
×
221
        }
222
    }
223
}
×
224

225
bool checkRequiredFields(json const & item, std::vector<std::string> const & requiredFields) {
×
226
    for (auto const & field: requiredFields) {
×
227
        if (!item.contains(field)) {
×
228
            std::cerr << "Error: Missing required field \"" << field << "\" in JSON item." << std::endl;
×
229
            return false;
×
230
        }
231
    }
232
    return true;
×
233
}
234

235
void checkOptionalFields(json const & item, std::vector<std::string> const & optionalFields) {
×
236
    for (auto const & field: optionalFields) {
×
237
        if (!item.contains(field)) {
×
238
            std::cout << "Warning: Optional field \"" << field << "\" is missing in JSON item." << std::endl;
×
239
        }
240
    }
241
}
×
242

243
DM_DataType stringToDataType(std::string const & data_type_str) {
×
244
    if (data_type_str == "video") return DM_DataType::Video;
×
245
    if (data_type_str == "points") return DM_DataType::Points;
×
246
    if (data_type_str == "mask") return DM_DataType::Mask;
×
247
    if (data_type_str == "line") return DM_DataType::Line;
×
248
    if (data_type_str == "analog") return DM_DataType::Analog;
×
249
    if (data_type_str == "digital_event") return DM_DataType::DigitalEvent;
×
250
    if (data_type_str == "digital_interval") return DM_DataType::DigitalInterval;
×
251
    if (data_type_str == "tensor") return DM_DataType::Tensor;
×
252
    if (data_type_str == "time") return DM_DataType::Time;
×
253
    return DM_DataType::Unknown;
×
254
}
255

256
std::vector<DataInfo> load_data_from_json_config(DataManager * dm, std::string const & json_filepath) {
×
257
    std::vector<DataInfo> data_info_list;
×
258
    // Open JSON file
259
    std::ifstream ifs(json_filepath);
×
260
    if (!ifs.is_open()) {
×
261
        std::cerr << "Failed to open JSON file: " << json_filepath << std::endl;
×
262
        return data_info_list;
×
263
    }
264

265
    // Parse JSON
266
    json j;
×
267
    ifs >> j;
×
268

269
    // get base path of filepath
270
    std::filesystem::path const base_path = std::filesystem::path(json_filepath).parent_path();
×
271

272
    // Iterate through JSON array
273
    for (auto const & item: j) {
×
274

275
        if (!checkRequiredFields(item, {"data_type", "name", "filepath"})) {
×
276
            continue;// Exit if any required field is missing
×
277
        }
278

279
        std::string const data_type_str = item["data_type"];
×
280
        auto const data_type = stringToDataType(data_type_str);
×
281
        if (data_type == DM_DataType::Unknown) {
×
282
            std::cout << "Unknown data type: " << data_type_str << std::endl;
×
283
            continue;
×
284
        }
285

286
        std::string const name = item["name"];
×
287

288
        auto file_exists = processFilePath(item["filepath"], base_path);
×
289
        if (!file_exists) {
×
290
            std::cerr << "File does not exist: " << item["filepath"] << std::endl;
×
291
            continue;
×
292
        }
293

294
        std::string const file_path = file_exists.value();
×
295

296
        switch (data_type) {
×
297
            case DM_DataType::Video: {
×
298

299
                auto video_data = load_video_into_VideoData(file_path);
×
300
                dm->setData<VideoData>("media", video_data);
×
301

302
                data_info_list.push_back({name, "VideoData", ""});
×
303
                break;
×
304
            }
×
305
            case DM_DataType::Points: {
×
306

307
                auto point_data = load_into_PointData(file_path, item);
×
308

309
                dm->setData<PointData>(name, point_data);
×
310

311
                std::string const color = item.value("color", "#0000FF");
×
312
                data_info_list.push_back({name, "PointData", color});
×
313
                break;
×
314
            }
×
315
            case DM_DataType::Mask: {
×
316

317
                auto mask_data = load_into_MaskData(file_path, item);
×
318

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

322
                data_info_list.push_back({name, "MaskData", color});
×
323

324
                if (item.contains("operations")) {
×
325

326
                    for (auto const & operation: item["operations"]) {
×
327

328
                        std::string const operation_type = operation["type"];
×
329

330
                        if (operation_type == "area") {
×
331
                            std::cout << "Calculating area for mask: " << name << std::endl;
×
332
                            auto area_data = area(dm->getData<MaskData>(name).get());
×
333
                            std::string const output_name = name + "_area";
×
334
                            dm->setData<AnalogTimeSeries>(output_name, area_data);
×
335
                        }
×
336
                    }
×
337
                }
338
                break;
×
339
            }
×
340
            case DM_DataType::Line: {
×
341

342
                auto line_map = load_line_csv(file_path);
×
343

344
                //Get the whisker name from the filename using filesystem
345
                auto whisker_filename = std::filesystem::path(file_path).filename().string();
×
346

347
                //Remove .csv suffix from filename
348
                auto whisker_name = remove_extension(whisker_filename);
×
349

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

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

354
                data_info_list.push_back({name, "LineData", color});
×
355

356
                break;
×
357
            }
×
358
            case DM_DataType::Analog: {
×
359

360
                auto analog_time_series = load_into_AnalogTimeSeries(file_path, item);
×
361

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

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

367
                    if (item.contains("clock")) {
×
368
                        std::string const clock = item["clock"];
×
369
                        dm->setTimeFrame(channel_name, clock);
×
370
                    }
×
371
                }
×
372
                break;
×
373
            }
×
374
            case DM_DataType::DigitalEvent: {
×
375

376
                auto digital_event_series = load_into_DigitalEventSeries(file_path, item);
×
377

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

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

383
                    if (item.contains("clock")) {
×
384
                        std::string const clock = item["clock"];
×
385
                        dm->setTimeFrame(channel_name, clock);
×
386
                    }
×
387
                }
×
388
                break;
×
389
            }
×
390
            case DM_DataType::DigitalInterval: {
×
391

392
                auto digital_interval_series = load_into_DigitalIntervalSeries(file_path, item);
×
393
                dm->setData<DigitalIntervalSeries>(name, digital_interval_series);
×
394

395
                break;
×
396
            }
×
397
            case DM_DataType::Tensor: {
×
398

399
                if (item["format"] == "numpy") {
×
400

401
                    TensorData tensor_data;
×
402
                    loadNpyToTensorData(file_path, tensor_data);
×
403

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

406
                } else {
×
407
                    std::cout << "Format " << item["format"] << " not found for " << name << std::endl;
×
408
                }
409
                break;
×
410
            }
411
            case DM_DataType::Time: {
×
412

413
                if (item["format"] == "uint16") {
×
414

415
                    int const channel = item["channel"];
×
416
                    std::string const transition = item["transition"];
×
417

418
                    int const header_size = item.value("header_size", 0);
×
419

420
                    auto opts = Loader::BinaryAnalogOptions{.file_path = file_path,
×
421
                                                            .header_size_bytes = static_cast<size_t>(header_size)};
×
422
                    auto data = readBinaryFile<uint16_t>(opts);
×
423

424
                    auto digital_data = Loader::extractDigitalData(data, channel);
×
425
                    auto events = Loader::extractEvents(digital_data, transition);
×
426

427
                    // convert to int with std::transform
428
                    std::vector<int> events_int;
×
429
                    events_int.reserve(events.size());
×
430
                    for (auto e: events) {
×
431
                        events_int.push_back(static_cast<int>(e));
×
432
                    }
433
                    std::cout << "Loaded " << events_int.size() << " events for " << name << std::endl;
×
434

435
                    auto timeframe = std::make_shared<TimeFrame>(events_int);
×
436
                    dm->setTime(name, timeframe);
×
437
                }
×
438

439
                if (item["format"] == "uint16_length") {
×
440

441
                    int const header_size = item.value("header_size", 0);
×
442

443
                    auto opts = Loader::BinaryAnalogOptions{.file_path = file_path,
×
444
                                                            .header_size_bytes = static_cast<size_t>(header_size)};
×
445
                    auto data = readBinaryFile<uint16_t>(opts);
×
446

447
                    std::vector<int> t(data.size());
×
448
                    std::iota(std::begin(t), std::end(t), 0);
×
449

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

452
                    auto timeframe = std::make_shared<TimeFrame>(t);
×
453
                    dm->setTime(name, timeframe);
×
454
                }
×
455
                break;
×
456
            }
457
            default:
×
458
                std::cout << "Unsupported data type: " << data_type_str << std::endl;
×
459
                continue;
×
460
        }
×
461
        if (item.contains("clock")) {
×
462
            std::string const clock = item["clock"];
×
463
            std::cout << "Setting time for " << name << " to " << clock << std::endl;
×
464
            dm->setTimeFrame(name, clock);
×
465
        }
×
466
    }
×
467

468
    return data_info_list;
469
}
×
470

471
DM_DataType DataManager::getType(std::string const & key) const {
×
472
    auto it = _data.find(key);
×
473
    if (it != _data.end()) {
×
474
        if (std::holds_alternative<std::shared_ptr<MediaData>>(it->second)) {
×
475
            return DM_DataType::Video;
×
476
        } else if (std::holds_alternative<std::shared_ptr<PointData>>(it->second)) {
×
477
            return DM_DataType::Points;
×
478
        } else if (std::holds_alternative<std::shared_ptr<LineData>>(it->second)) {
×
479
            return DM_DataType::Line;
×
480
        } else if (std::holds_alternative<std::shared_ptr<MaskData>>(it->second)) {
×
481
            return DM_DataType::Mask;
×
482
        } else if (std::holds_alternative<std::shared_ptr<AnalogTimeSeries>>(it->second)) {
×
483
            return DM_DataType::Analog;
×
484
        } else if (std::holds_alternative<std::shared_ptr<DigitalEventSeries>>(it->second)) {
×
485
            return DM_DataType::DigitalEvent;
×
486
        } else if (std::holds_alternative<std::shared_ptr<DigitalIntervalSeries>>(it->second)) {
×
487
            return DM_DataType::DigitalInterval;
×
488
        } else if (std::holds_alternative<std::shared_ptr<TensorData>>(it->second)) {
×
489
            return DM_DataType::Tensor;
×
490
        }
491
        return DM_DataType::Unknown;
×
492
    }
493
    return DM_DataType::Unknown;
×
494
}
495

496
std::string convert_data_type_to_string(DM_DataType type) {
×
497
    switch (type) {
×
498
        case DM_DataType::Video:
×
499
            return "video";
×
500
        case DM_DataType::Points:
×
501
            return "points";
×
502
        case DM_DataType::Mask:
×
503
            return "mask";
×
504
        case DM_DataType::Line:
×
505
            return "line";
×
506
        case DM_DataType::Analog:
×
507
            return "analog";
×
508
        case DM_DataType::DigitalEvent:
×
509
            return "digital_event";
×
510
        case DM_DataType::DigitalInterval:
×
511
            return "digital_interval";
×
512
        case DM_DataType::Tensor:
×
513
            return "tensor";
×
514
        case DM_DataType::Time:
×
515
            return "time";
×
516
        default:
×
517
            return "unknown";
×
518
    }
519
}
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