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

paulmthompson / WhiskerToolbox / 15909500151

26 Jun 2025 06:20PM UTC coverage: 71.617% (-0.03%) from 71.644%
15909500151

push

github

paulmthompson
scrollbar updates video time

11309 of 15791 relevant lines covered (71.62%)

1134.15 hits per line

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

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

13
#include "AnalogTimeSeries/IO/JSON/Analog_Time_Series_JSON.hpp"
14
#include "DigitalTimeSeries/IO/CSV/Digital_Interval_Series_CSV.hpp"
15
#include "DigitalTimeSeries/IO/JSON/Digital_Event_Series_JSON.hpp"
16
#include "DigitalTimeSeries/IO/JSON/Digital_Interval_Series_JSON.hpp"
17
#include "Lines/IO/JSON/Line_Data_JSON.hpp"
18
#include "Masks/IO/JSON/Mask_Data_JSON.hpp"
19
#include "Media/Video_Data_Loader.hpp"
20
#include "Points/IO/JSON/Point_Data_JSON.hpp"
21

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

25
#include "TimeFrame.hpp"
26
#include "TimeFrame/TimeFrameV2.hpp"
27

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

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

37
using namespace nlohmann;
38

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

43
    setTimeFrame("media", "time");
320✔
44
    _output_path = std::filesystem::current_path();
64✔
45
}
64✔
46

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

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

54
    if (_times.find(key) != _times.end()) {
27✔
55
        if (overwrite) {
2✔
56
            _times[key] = std::move(timeframe);
×
57
            return true;
×
58
        } else {
59
            std::cerr << "Error: Time key already exists in DataManager: " << key << std::endl;
2✔
60
            return false;
2✔
61
        }
62
    }
63

64
    _times[key] = std::move(timeframe);
25✔
65
    return true;
25✔
66
}
67

68
std::shared_ptr<TimeFrame> DataManager::getTime() {
1✔
69
    return _times["time"];
3✔
70
};
71

72
std::shared_ptr<TimeFrame> DataManager::getTime(std::string const & key) {
11✔
73
    if (_times.find(key) != _times.end()) {
11✔
74
        return _times[key];
9✔
75
    }
76
    return nullptr;
2✔
77
};
78

79
bool DataManager::removeTime(std::string const & key) {
×
80
    if (_times.find(key) == _times.end()) {
×
81
        std::cerr << "Error: could not find time key in DataManager: " << key << std::endl;
×
82
        return false;
×
83
    }
84

85
    auto it = _times.find(key);
×
86
    _times.erase(it);
×
87
    return true;
×
88
}
89

90
bool DataManager::setTimeFrame(std::string const & data_key, std::string const & time_key) {
124✔
91
    if (_data.find(data_key) == _data.end()) {
124✔
92
        std::cerr << "Error: Data key not found in DataManager: " << data_key << std::endl;
1✔
93
        return false;
1✔
94
    }
95

96
    if (_times.find(time_key) == _times.end()) {
123✔
97
        std::cerr << "Error: Time key not found in DataManager: " << time_key << std::endl;
1✔
98
        return false;
1✔
99
    }
100

101
    _time_frames[data_key] = time_key;
122✔
102
    return true;
122✔
103
}
104

105
std::string DataManager::getTimeFrame(std::string const & data_key) {
9✔
106
    // check if data_key exists
107
    if (_data.find(data_key) == _data.end()) {
9✔
108
        std::cerr << "Error: Data key not found in DataManager: " << data_key << std::endl;
1✔
109
        return "";
3✔
110
    }
111

112
    // check if data key has time frame
113
    if (_time_frames.find(data_key) == _time_frames.end()) {
8✔
114
        std::cerr << "Error: Data key "
115
                  << data_key
116
                  << " exists, but not assigned to a TimeFrame" << std::endl;
×
117
        return "";
×
118
    }
119

120
    return _time_frames[data_key];
8✔
121
}
122

123
std::vector<std::string> DataManager::getTimeFrameKeys() {
9✔
124
    std::vector<std::string> keys;
9✔
125
    keys.reserve(_times.size());
9✔
126
    for (auto const & [key, value]: _times) {
27✔
127

128
        keys.push_back(key);
18✔
129
    }
130
    return keys;
9✔
131
}
×
132

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

135
    int id = -1;
7✔
136

137
    if (_data.find(key) != _data.end()) {
7✔
138
        auto data = _data[key];
6✔
139

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

146
    return id;
7✔
147
}
148

149
bool DataManager::removeCallbackFromData(std::string const & key, int callback_id) {
4✔
150
    if (_data.find(key) != _data.end()) {
4✔
151
        auto data = _data[key];
3✔
152

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

158
        return true;
3✔
159
    }
3✔
160

161
    return false;
1✔
162
}
163

164
void DataManager::addObserver(ObserverCallback callback) {
5✔
165
    _observers.push_back(std::move(callback));
5✔
166
}
5✔
167

168
void DataManager::_notifyObservers() {
62✔
169
    for (auto & observer: _observers) {
70✔
170
        observer();
8✔
171
    }
172
}
62✔
173

174
std::vector<std::string> DataManager::getAllKeys() {
5✔
175
    std::vector<std::string> keys;
5✔
176
    keys.reserve(_data.size());
5✔
177
    for (auto const & [key, value]: _data) {
17✔
178

179
        keys.push_back(key);
12✔
180
    }
181
    return keys;
5✔
182
}
×
183

184
std::optional<DataTypeVariant> DataManager::getDataVariant(std::string const & key) {
×
185
    if (_data.find(key) != _data.end()) {
×
186
        return _data[key];
×
187
    }
188
    return std::nullopt;
×
189
}
190

191
void DataManager::setData(std::string const & key, DataTypeVariant data) {
1✔
192
    _data[key] = data;
1✔
193
    setTimeFrame(key, "time");
3✔
194
    _notifyObservers();
1✔
195
}
1✔
196

197
std::optional<std::string> processFilePath(
×
198
        std::string const & file_path,
199
        std::filesystem::path const & base_path) {
200
    std::filesystem::path full_path = file_path;
×
201

202
    // Check for wildcard character
203
    if (file_path.find('*') != std::string::npos) {
×
204
        // Convert wildcard pattern to regex
205
        std::string const pattern = std::regex_replace(full_path.string(), std::regex("\\*"), ".*");
×
206
        std::regex const regex_pattern(pattern);
×
207

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

232
bool checkRequiredFields(json const & item, std::vector<std::string> const & requiredFields) {
×
233
    for (auto const & field: requiredFields) {
×
234
        if (!item.contains(field)) {
×
235
            std::cerr << "Error: Missing required field \"" << field << "\" in JSON item." << std::endl;
×
236
            return false;
×
237
        }
238
    }
239
    return true;
×
240
}
241

242
void checkOptionalFields(json const & item, std::vector<std::string> const & optionalFields) {
×
243
    for (auto const & field: optionalFields) {
×
244
        if (!item.contains(field)) {
×
245
            std::cout << "Warning: Optional field \"" << field << "\" is missing in JSON item." << std::endl;
×
246
        }
247
    }
248
}
×
249

250
DM_DataType stringToDataType(std::string const & data_type_str) {
×
251
    if (data_type_str == "video") return DM_DataType::Video;
×
252
    if (data_type_str == "images") return DM_DataType::Images;
×
253
    if (data_type_str == "points") return DM_DataType::Points;
×
254
    if (data_type_str == "mask") return DM_DataType::Mask;
×
255
    if (data_type_str == "line") return DM_DataType::Line;
×
256
    if (data_type_str == "analog") return DM_DataType::Analog;
×
257
    if (data_type_str == "digital_event") return DM_DataType::DigitalEvent;
×
258
    if (data_type_str == "digital_interval") return DM_DataType::DigitalInterval;
×
259
    if (data_type_str == "tensor") return DM_DataType::Tensor;
×
260
    if (data_type_str == "time") return DM_DataType::Time;
×
261
    return DM_DataType::Unknown;
×
262
}
263

264
std::vector<DataInfo> load_data_from_json_config(DataManager * dm, std::string const & json_filepath) {
×
265
    std::vector<DataInfo> data_info_list;
×
266
    // Open JSON file
267
    std::ifstream ifs(json_filepath);
×
268
    if (!ifs.is_open()) {
×
269
        std::cerr << "Failed to open JSON file: " << json_filepath << std::endl;
×
270
        return data_info_list;
×
271
    }
272

273
    // Parse JSON
274
    json j;
×
275
    ifs >> j;
×
276

277
    // get base path of filepath
278
    std::filesystem::path const base_path = std::filesystem::path(json_filepath).parent_path();
×
279

280
    // Iterate through JSON array
281
    for (auto const & item: j) {
×
282

283
        if (!checkRequiredFields(item, {"data_type", "name", "filepath"})) {
×
284
            continue;// Exit if any required field is missing
×
285
        }
286

287
        std::string const data_type_str = item["data_type"];
×
288
        auto const data_type = stringToDataType(data_type_str);
×
289
        if (data_type == DM_DataType::Unknown) {
×
290
            std::cout << "Unknown data type: " << data_type_str << std::endl;
×
291
            continue;
×
292
        }
293

294
        std::string const name = item["name"];
×
295

296
        auto file_exists = processFilePath(item["filepath"], base_path);
×
297
        if (!file_exists) {
×
298
            std::cerr << "File does not exist: " << item["filepath"] << std::endl;
×
299
            continue;
×
300
        }
301

302
        std::string const file_path = file_exists.value();
×
303

304
        switch (data_type) {
×
305
            case DM_DataType::Video: {
×
306

307
                auto video_data = load_video_into_VideoData(file_path);
×
308
                dm->setData<VideoData>("media", video_data);
×
309

310
                data_info_list.push_back({name, "VideoData", ""});
×
311
                break;
×
312
            }
×
313
            case DM_DataType::Images: {
×
314

315
                auto media = std::make_shared<ImageData>();
×
316
                media->LoadMedia(file_path);
×
317
                dm->setData<ImageData>("media", media);
×
318

319
                data_info_list.push_back({name, "ImageData", ""});
×
320
                break;
×
321
            }
×
322
            case DM_DataType::Points: {
×
323

324
                auto point_data = load_into_PointData(file_path, item);
×
325

326
                dm->setData<PointData>(name, point_data);
×
327

328
                std::string const color = item.value("color", "#0000FF");
×
329
                data_info_list.push_back({name, "PointData", color});
×
330
                break;
×
331
            }
×
332
            case DM_DataType::Mask: {
×
333

334
                auto mask_data = load_into_MaskData(file_path, item);
×
335

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

339
                data_info_list.push_back({name, "MaskData", color});
×
340

341
                if (item.contains("operations")) {
×
342

343
                    for (auto const & operation: item["operations"]) {
×
344

345
                        std::string const operation_type = operation["type"];
×
346

347
                        if (operation_type == "area") {
×
348
                            std::cout << "Calculating area for mask: " << name << std::endl;
×
349
                            auto area_data = area(dm->getData<MaskData>(name).get());
×
350
                            std::string const output_name = name + "_area";
×
351
                            dm->setData<AnalogTimeSeries>(output_name, area_data);
×
352
                        }
×
353
                    }
×
354
                }
355
                break;
×
356
            }
×
357
            case DM_DataType::Line: {
×
358

359
                auto line_data = load_into_LineData(file_path, item);
×
360

361
                dm->setData<LineData>(name, line_data);
×
362

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

365
                data_info_list.push_back({name, "LineData", color});
×
366

367
                break;
×
368
            }
×
369
            case DM_DataType::Analog: {
×
370

371
                auto analog_time_series = load_into_AnalogTimeSeries(file_path, item);
×
372

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

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

378
                    if (item.contains("clock")) {
×
379
                        std::string const clock = item["clock"];
×
380
                        dm->setTimeFrame(channel_name, clock);
×
381
                    }
×
382
                }
×
383
                break;
×
384
            }
×
385
            case DM_DataType::DigitalEvent: {
×
386

387
                auto digital_event_series = load_into_DigitalEventSeries(file_path, item);
×
388

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

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

394
                    if (item.contains("clock")) {
×
395
                        std::string const clock = item["clock"];
×
396
                        dm->setTimeFrame(channel_name, clock);
×
397
                    }
×
398
                }
×
399
                break;
×
400
            }
×
401
            case DM_DataType::DigitalInterval: {
×
402

403
                auto digital_interval_series = load_into_DigitalIntervalSeries(file_path, item);
×
404
                dm->setData<DigitalIntervalSeries>(name, digital_interval_series);
×
405

406
                break;
×
407
            }
×
408
            case DM_DataType::Tensor: {
×
409

410
                if (item["format"] == "numpy") {
×
411

412
                    TensorData tensor_data;
×
413
                    loadNpyToTensorData(file_path, tensor_data);
×
414

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

417
                } else {
×
418
                    std::cout << "Format " << item["format"] << " not found for " << name << std::endl;
×
419
                }
420
                break;
×
421
            }
422
            case DM_DataType::Time: {
×
423

424
                if (item["format"] == "uint16") {
×
425

426
                    int const channel = item["channel"];
×
427
                    std::string const transition = item["transition"];
×
428

429
                    int const header_size = item.value("header_size", 0);
×
430

431
                    auto opts = Loader::BinaryAnalogOptions{.file_path = file_path,
×
432
                                                            .header_size_bytes = static_cast<size_t>(header_size)};
×
433
                    auto data = Loader::readBinaryFile<uint16_t>(opts);
×
434

435
                    auto digital_data = Loader::extractDigitalData(data, channel);
×
436
                    auto events = Loader::extractEvents(digital_data, transition);
×
437

438
                    // convert to int with std::transform
439
                    std::vector<int> events_int;
×
440
                    events_int.reserve(events.size());
×
441
                    for (auto e: events) {
×
442
                        events_int.push_back(static_cast<int>(e));
×
443
                    }
444
                    std::cout << "Loaded " << events_int.size() << " events for " << name << std::endl;
×
445

446
                    auto timeframe = std::make_shared<TimeFrame>(events_int);
×
447
                    dm->setTime(name, timeframe, true);
×
448
                }
×
449

450
                if (item["format"] == "uint16_length") {
×
451

452
                    int const header_size = item.value("header_size", 0);
×
453

454
                    auto opts = Loader::BinaryAnalogOptions{.file_path = file_path,
×
455
                                                            .header_size_bytes = static_cast<size_t>(header_size)};
×
456
                    auto data = Loader::readBinaryFile<uint16_t>(opts);
×
457

458
                    std::vector<int> t(data.size());
×
459
                    std::iota(std::begin(t), std::end(t), 0);
×
460

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

463
                    auto timeframe = std::make_shared<TimeFrame>(t);
×
464
                    dm->setTime(name, timeframe, true);
×
465
                }
×
466

467
                if (item["format"] == "filename") {
×
468

469
                    // Get required parameters
470
                    std::string const folder_path = file_path; // file path is required argument
×
471
                    std::string const regex_pattern = item["regex_pattern"];
×
472

473
                    // Get optional parameters with defaults
474
                    std::string const file_extension = item.value("file_extension", "");
×
475
                    std::string const mode_str = item.value("mode", "found_values");
×
476
                    bool const sort_ascending = item.value("sort_ascending", true);
×
477

478
                    // Convert mode string to enum
479
                    FilenameTimeFrameMode mode = FilenameTimeFrameMode::FOUND_VALUES;
×
480
                    if (mode_str == "zero_to_max") {
×
481
                        mode = FilenameTimeFrameMode::ZERO_TO_MAX;
×
482
                    } else if (mode_str == "min_to_max") {
×
483
                        mode = FilenameTimeFrameMode::MIN_TO_MAX;
×
484
                    }
485

486
                    // Create options
487
                    FilenameTimeFrameOptions options;
×
488
                    options.folder_path = folder_path;
×
489
                    options.file_extension = file_extension;
×
490
                    options.regex_pattern = regex_pattern;
×
491
                    options.mode = mode;
×
492
                    options.sort_ascending = sort_ascending;
×
493

494
                    // Create TimeFrame from filenames
495
                    auto timeframe = createTimeFrameFromFilenames(options);
×
496
                    if (timeframe) {
×
497
                        dm->setTime(name, timeframe, true);
×
498
                        std::cout << "Created TimeFrame '" << name << "' from filenames in "
499
                                  << folder_path << std::endl;
×
500
                    } else {
501
                        std::cerr << "Error: Failed to create TimeFrame from filenames for "
502
                                  << name << std::endl;
×
503
                    }
504
                }
×
505
                break;
×
506
            }
507
            default:
×
508
                std::cout << "Unsupported data type: " << data_type_str << std::endl;
×
509
                continue;
×
510
        }
×
511
        if (item.contains("clock")) {
×
512
            std::string const clock = item["clock"];
×
513
            std::cout << "Setting time for " << name << " to " << clock << std::endl;
×
514
            dm->setTimeFrame(name, clock);
×
515
        }
×
516
    }
×
517

518
    return data_info_list;
519
}
×
520

521
DM_DataType DataManager::getType(std::string const & key) const {
22✔
522
    auto it = _data.find(key);
22✔
523
    if (it != _data.end()) {
22✔
524
        if (std::holds_alternative<std::shared_ptr<MediaData>>(it->second)) {
19✔
525
            //Dynamic cast to videodata or image data
526

527
            auto media_data = std::get<std::shared_ptr<MediaData>>(it->second);
15✔
528
            if (dynamic_cast<VideoData *>(media_data.get()) != nullptr) {
15✔
529
                return DM_DataType::Video;
7✔
530
            } else if (dynamic_cast<ImageData *>(media_data.get()) != nullptr) {
8✔
531
                return DM_DataType::Images;
7✔
532
            } else {
533
                return DM_DataType::Video; //Old behavior
1✔
534
            }
535
        } else if (std::holds_alternative<std::shared_ptr<PointData>>(it->second)) {
19✔
536
            return DM_DataType::Points;
2✔
537
        } else if (std::holds_alternative<std::shared_ptr<LineData>>(it->second)) {
2✔
538
            return DM_DataType::Line;
1✔
539
        } else if (std::holds_alternative<std::shared_ptr<MaskData>>(it->second)) {
1✔
540
            return DM_DataType::Mask;
1✔
541
        } else if (std::holds_alternative<std::shared_ptr<AnalogTimeSeries>>(it->second)) {
×
542
            return DM_DataType::Analog;
×
543
        } else if (std::holds_alternative<std::shared_ptr<DigitalEventSeries>>(it->second)) {
×
544
            return DM_DataType::DigitalEvent;
×
545
        } else if (std::holds_alternative<std::shared_ptr<DigitalIntervalSeries>>(it->second)) {
×
546
            return DM_DataType::DigitalInterval;
×
547
        } else if (std::holds_alternative<std::shared_ptr<TensorData>>(it->second)) {
×
548
            return DM_DataType::Tensor;
×
549
        }
550
        return DM_DataType::Unknown;
×
551
    }
552
    return DM_DataType::Unknown;
3✔
553
}
554

555
// ========== TimeFrameV2 Implementation ==========
556

557
bool DataManager::setTimeV2(std::string const & key, AnyTimeFrame timeframe, bool overwrite) {
18✔
558
    if (_times_v2.find(key) != _times_v2.end()) {
18✔
559
        if (overwrite) {
6✔
560
            _times_v2[key] = std::move(timeframe);
6✔
561
            return true;
6✔
562
        } else {
563
            std::cerr << "Error: TimeFrameV2 key already exists in DataManager: " << key << std::endl;
×
564
            return false;
×
565
        }
566
    }
567

568
    _times_v2[key] = std::move(timeframe);
12✔
569
    return true;
12✔
570
}
571

572
std::optional<AnyTimeFrame> DataManager::getTimeV2(std::string const & key) {
15✔
573
    if (_times_v2.find(key) != _times_v2.end()) {
15✔
574
        return _times_v2[key];
12✔
575
    }
576
    return std::nullopt;
3✔
577
}
578

579
bool DataManager::removeTimeV2(std::string const & key) {
2✔
580
    if (_times_v2.find(key) == _times_v2.end()) {
2✔
581
        std::cerr << "Error: could not find TimeFrameV2 key in DataManager: " << key << std::endl;
1✔
582
        return false;
1✔
583
    }
584

585
    auto it = _times_v2.find(key);
1✔
586
    _times_v2.erase(it);
1✔
587
    return true;
1✔
588
}
589

590
std::vector<std::string> DataManager::getTimeFrameV2Keys() {
3✔
591
    std::vector<std::string> keys;
3✔
592
    keys.reserve(_times_v2.size());
3✔
593
    for (auto const & [key, value]: _times_v2) {
7✔
594
        keys.push_back(key);
4✔
595
    }
596
    return keys;
3✔
597
}
×
598

599
bool DataManager::createClockTimeFrame(std::string const & key, int64_t start_tick,
4✔
600
                                       int64_t num_samples, double sampling_rate_hz,
601
                                       bool overwrite) {
602
    auto clock_frame = TimeFrameUtils::createDenseClockTimeFrame(start_tick, num_samples, sampling_rate_hz);
4✔
603
    AnyTimeFrame any_frame = clock_frame;
4✔
604
    return setTimeV2(key, std::move(any_frame), overwrite);
8✔
605
}
4✔
606

607
bool DataManager::createCameraTimeFrame(std::string const & key, std::vector<int64_t> frame_indices,
4✔
608
                                        bool overwrite) {
609
    auto camera_frame = TimeFrameUtils::createSparseCameraTimeFrame(std::move(frame_indices));
4✔
610
    AnyTimeFrame any_frame = camera_frame;
4✔
611
    return setTimeV2(key, std::move(any_frame), overwrite);
8✔
612
}
4✔
613

614
bool DataManager::createDenseCameraTimeFrame(std::string const & key, int64_t start_frame,
1✔
615
                                             int64_t num_frames, bool overwrite) {
616
    auto camera_frame = TimeFrameUtils::createDenseCameraTimeFrame(start_frame, num_frames);
1✔
617
    AnyTimeFrame any_frame = camera_frame;
1✔
618
    return setTimeV2(key, std::move(any_frame), overwrite);
2✔
619
}
1✔
620

621
std::string convert_data_type_to_string(DM_DataType type) {
×
622
    switch (type) {
×
623
        case DM_DataType::Video:
×
624
            return "video";
×
625
        case DM_DataType::Images:
×
626
            return "images";
×
627
        case DM_DataType::Points:
×
628
            return "points";
×
629
        case DM_DataType::Mask:
×
630
            return "mask";
×
631
        case DM_DataType::Line:
×
632
            return "line";
×
633
        case DM_DataType::Analog:
×
634
            return "analog";
×
635
        case DM_DataType::DigitalEvent:
×
636
            return "digital_event";
×
637
        case DM_DataType::DigitalInterval:
×
638
            return "digital_interval";
×
639
        case DM_DataType::Tensor:
×
640
            return "tensor";
×
641
        case DM_DataType::Time:
×
642
            return "time";
×
643
        default:
×
644
            return "unknown";
×
645
    }
646
}
647

648
// ========== Enhanced AnalogTimeSeries Support Implementation ==========
649

650
bool DataManager::createAnalogTimeSeriesWithClock(std::string const & data_key,
3✔
651
                                                  std::string const & timeframe_key,
652
                                                  std::vector<float> analog_data,
653
                                                  int64_t start_tick,
654
                                                  double sampling_rate_hz,
655
                                                  bool overwrite) {
656
    // Create the clock timeframe
657
    if (!createClockTimeFrame(timeframe_key, start_tick,
3✔
658
                              static_cast<int64_t>(analog_data.size()),
3✔
659
                              sampling_rate_hz, overwrite)) {
660
        return false;
×
661
    }
662

663
    // Get the timeframe
664
    auto timeframe_opt = getTimeV2(timeframe_key);
3✔
665
    if (!timeframe_opt.has_value()) {
3✔
666
        return false;
×
667
    }
668

669
    // Create time vector with the actual tick values
670
    std::vector<TimeFrameIndex> time_vector;
3✔
671
    time_vector.reserve(analog_data.size());
3✔
672
    for (size_t i = 0; i < analog_data.size(); ++i) {
31,007✔
673
        time_vector.push_back(TimeFrameIndex(start_tick + static_cast<int64_t>(i)));
31,004✔
674
    }
675

676
    // Create the analog series
677
    auto series = std::make_shared<AnalogTimeSeries>(std::move(analog_data), std::move(time_vector));
3✔
678
    setDataV2(data_key, series, timeframe_opt.value(), timeframe_key);
3✔
679
    return true;
3✔
680
}
3✔
681

682
bool DataManager::createAnalogTimeSeriesWithCamera(std::string const & data_key,
2✔
683
                                                   std::string const & timeframe_key,
684
                                                   std::vector<float> analog_data,
685
                                                   std::vector<int64_t> frame_indices,
686
                                                   bool overwrite) {
687
    if (analog_data.size() != frame_indices.size()) {
2✔
688
        std::cerr << "Error: analog data and frame indices must have same size" << std::endl;
×
689
        return false;
×
690
    }
691

692
    // Save frame_indices before they're moved
693
    std::vector<int64_t> frame_indices_copy = frame_indices;
2✔
694

695
    // Create the camera timeframe
696
    if (!createCameraTimeFrame(timeframe_key, std::move(frame_indices), overwrite)) {
2✔
697
        return false;
×
698
    }
699

700
    // Get the timeframe
701
    auto timeframe_opt = getTimeV2(timeframe_key);
2✔
702
    if (!timeframe_opt.has_value()) {
2✔
703
        return false;
×
704
    }
705

706
    // Create time vector with the actual frame indices
707
    std::vector<TimeFrameIndex> time_vector;
2✔
708
    time_vector.reserve(analog_data.size());
2✔
709
    for (int64_t frame_idx: frame_indices_copy) {
107✔
710
        time_vector.push_back(TimeFrameIndex(frame_idx));
105✔
711
    }
712

713
    // Create the analog series
714
    auto series = std::make_shared<AnalogTimeSeries>(std::move(analog_data), std::move(time_vector));
2✔
715
    setDataV2(data_key, series, timeframe_opt.value(), timeframe_key);
2✔
716
    return true;
2✔
717
}
2✔
718

719
bool DataManager::createAnalogTimeSeriesWithDenseCamera(std::string const & data_key,
1✔
720
                                                        std::string const & timeframe_key,
721
                                                        std::vector<float> analog_data,
722
                                                        int64_t start_frame,
723
                                                        bool overwrite) {
724
    // Create the dense camera timeframe
725
    if (!createDenseCameraTimeFrame(timeframe_key, start_frame,
1✔
726
                                    static_cast<int64_t>(analog_data.size()), overwrite)) {
1✔
727
        return false;
×
728
    }
729

730
    // Get the timeframe
731
    auto timeframe_opt = getTimeV2(timeframe_key);
1✔
732
    if (!timeframe_opt.has_value()) {
1✔
733
        return false;
×
734
    }
735

736
    // Create time vector with the actual frame indices
737
    std::vector<TimeFrameIndex> time_vector;
1✔
738
    time_vector.reserve(analog_data.size());
1✔
739
    for (size_t i = 0; i < analog_data.size(); ++i) {
7✔
740
        time_vector.push_back(TimeFrameIndex(start_frame + static_cast<int64_t>(i)));
6✔
741
    }
742

743
    // Create the analog series
744
    auto series = std::make_shared<AnalogTimeSeries>(std::move(analog_data), std::move(time_vector));
1✔
745
    setDataV2(data_key, series, timeframe_opt.value(), timeframe_key);
1✔
746
    return true;
1✔
747
}
1✔
748

749
std::string DataManager::getAnalogCoordinateType(std::string const & data_key) {
5✔
750
    // Check if the data exists and is an AnalogTimeSeries
751
    if (_data.find(data_key) == _data.end()) {
5✔
752
        return "not_found";
×
753
    }
754

755
    if (!std::holds_alternative<std::shared_ptr<AnalogTimeSeries>>(_data[data_key])) {
5✔
756
        return "not_analog_timeseries";
×
757
    }
758

759
    auto series = std::get<std::shared_ptr<AnalogTimeSeries>>(_data[data_key]);
5✔
760
    if (!series) {
5✔
761
        return "null_series";
×
762
    }
763

764
    return series->getCoordinateType();
5✔
765
}
5✔
766

767
bool DataManager::analogUsesCoordinateTypeImpl(std::string const & data_key, std::string const & type_name) {
3✔
768
    // Check if the data exists and is an AnalogTimeSeries
769
    if (_data.find(data_key) == _data.end()) {
3✔
770
        return false;
×
771
    }
772

773
    if (!std::holds_alternative<std::shared_ptr<AnalogTimeSeries>>(_data[data_key])) {
3✔
774
        return false;
×
775
    }
776

777
    auto series = std::get<std::shared_ptr<AnalogTimeSeries>>(_data[data_key]);
3✔
778
    if (!series) {
3✔
779
        return false;
×
780
    }
781

782
    return series->getCoordinateType() == type_name;
3✔
783
}
3✔
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