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

paulmthompson / WhiskerToolbox / 17270491352

27 Aug 2025 02:57PM UTC coverage: 65.333%. Remained the same
17270491352

push

github

paulmthompson
Merge branch 'main' of https://github.com/paulmthompson/WhiskerToolbox

352 of 628 new or added lines in 92 files covered. (56.05%)

357 existing lines in 24 files now uncovered.

26429 of 40453 relevant lines covered (65.33%)

1119.34 hits per line

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

0.0
/src/DataManager/Points/IO/CSV/Point_Data_CSV.cpp
1
#include "Point_Data_CSV.hpp"
2

3
#include "Points/Point_Data.hpp"
4
#include "transforms/data_transforms.hpp"
5
#include "utils/string_manip.hpp"
6

7
#include <filesystem>
8
#include <fstream>
9
#include <iomanip>
10
#include <iostream>
11
#include <sstream>
12

13
//https://stackoverflow.com/questions/4654636/how-to-determine-if-a-string-is-a-number-with-c
14
bool is_number(std::string const & s) {
×
15
    return !s.empty() && std::find_if(s.begin(),
×
16
                                      s.end(), [](unsigned char c) { return !std::isdigit(c); }) == s.end();
×
17
}
18

19
std::map<TimeFrameIndex, Point2D<float>> load(CSVPointLoaderOptions const & opts) {
×
20
    std::string csv_line;
×
21

22
    auto line_output = std::map<TimeFrameIndex, Point2D<float>>{};
×
23

24
    std::fstream myfile;
×
25
    myfile.open(opts.filename, std::fstream::in);
×
26

27
    std::string x_str;
×
28
    std::string y_str;
×
29
    std::string frame_str;
×
30
    std::string col_value;
×
31

32
    std::vector<std::pair<TimeFrameIndex, Point2D<float>>> csv_vector = {};
×
33

34
    while (getline(myfile, csv_line)) {
×
35

36
        std::stringstream ss(csv_line);
×
37

38
        int cols_read = 0;
×
39
        while (getline(ss, col_value, opts.column_delim)) {
×
40
            if (cols_read == opts.frame_column) {
×
41
                frame_str = col_value;
×
42
            } else if (cols_read == opts.x_column) {
×
43
                x_str = col_value;
×
44
            } else if (cols_read == opts.y_column) {
×
45
                y_str = col_value;
×
46
            }
47
            cols_read++;
×
48
        }
49

50
        if (is_number(frame_str)) {
×
51
            //line_output[TimeFrameIndex(std::stoi(frame_str))]=Point2D<float>{std::stof(x_str),std::stof(y_str)};
52
            csv_vector.emplace_back(TimeFrameIndex(std::stoi(frame_str)), Point2D<float>{std::stof(x_str), std::stof(y_str)});
×
53
        }
54
    }
×
55
    std::cout.flush();
×
56

57
    std::cout << "Read " << csv_vector.size() << " lines from " << opts.filename << std::endl;
×
58

59
    line_output.insert(csv_vector.begin(), csv_vector.end());
×
60

61
    return line_output;
×
62
}
×
63

64
std::map<std::string, std::map<TimeFrameIndex, Point2D<float>>> load_multiple_points_from_csv(std::string const & filename, int const frame_column) {
×
65
    std::fstream file;
×
66
    file.open(filename, std::fstream::in);
×
67

68
    std::string ln, ele;
×
69

70
    getline(file, ln);// skip the "scorer" row
×
71

72
    getline(file, ln);// bodyparts row
×
73
    std::vector<std::string> bodyparts;
×
74
    {
75
        std::stringstream ss(ln);
×
76
        while (getline(ss, ele, ',')) {
×
77
            bodyparts.push_back(ele);
×
78
        }
79
    }
×
80

81
    getline(file, ln);// coords row
×
82
    std::vector<std::string> dims;
×
83
    {
84
        std::stringstream ss(ln);
×
85
        while (getline(ss, ele, ',')) {
×
86
            dims.push_back(ele);
×
87
        }
88
    }
×
89

90
    std::map<std::string, std::map<TimeFrameIndex, Point2D<float>>> data;
×
91
    while (getline(file, ln)) {
×
92
        std::stringstream ss(ln);
×
NEW
93
        size_t col_no = 0;
×
94
        TimeFrameIndex frame_no(0);
×
95
        while (getline(ss, ele, ',')) {
×
NEW
96
            if (static_cast<int>(col_no) == frame_column) {
×
97
                frame_no = TimeFrameIndex(std::stoi(extract_numbers_from_string(ele)));
×
NEW
98
            } else if (col_no < dims.size() && dims[col_no] == "x") {
×
NEW
99
                if (col_no < bodyparts.size()) {
×
NEW
100
                    data[bodyparts[col_no]][frame_no].x = std::stof(ele);
×
101
                }
NEW
102
            } else if (col_no < dims.size() && dims[col_no] == "y") {
×
NEW
103
                if (col_no < bodyparts.size()) {
×
NEW
104
                    data[bodyparts[col_no]][frame_no].y = std::stof(ele);
×
105
                }
106
            }
107
            ++col_no;
×
108
        }
UNCOV
109
    }
×
110

111
    return data;
×
UNCOV
112
}
×
113

UNCOV
114
void save(PointData const * point_data, CSVPointSaverOptions const & opts)
×
115
{
116
    //Check if directory exists
UNCOV
117
    if (!std::filesystem::exists(opts.parent_dir)) {
×
118
        std::filesystem::create_directories(opts.parent_dir);
×
UNCOV
119
        std::cout << "Created directory: " << opts.parent_dir << std::endl;
×
120
    }
121

122
    std::string const filename = opts.parent_dir + "/" + opts.filename;
×
123
    std::fstream fout;
×
124

UNCOV
125
    fout.open(filename, std::fstream::out);
×
126
    if (!fout.is_open()) {
×
127
        std::cerr << "Failed to open file for saving: " << filename << std::endl;
×
UNCOV
128
        return;
×
129
    }
130

131
    if (opts.save_header) {
×
132
        fout << opts.header << opts.line_delim;
×
133
    }
134

135
    for (auto const& timePointsPair : point_data->GetAllPointsAsRange()) {
×
136
        fout << timePointsPair.time.getValue();
×
UNCOV
137
        for (size_t i = 0; i < timePointsPair.points.size(); ++i) {
×
UNCOV
138
            fout << opts.delimiter << timePointsPair.points[i].x << opts.delimiter << timePointsPair.points[i].y;
×
139
        }
140
        fout << opts.line_delim;
×
141
    }
142
    fout.close();
×
UNCOV
143
    std::cout << "Successfully saved points to " << filename << std::endl;
×
144
}
×
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