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

paulmthompson / WhiskerToolbox / 14843312717

05 May 2025 06:19PM UTC coverage: 22.285% (+4.0%) from 18.243%
14843312717

push

github

paulmthompson
add point tests

470 of 2109 relevant lines covered (22.29%)

2.09 hits per line

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

0.0
/src/WhiskerToolbox/DataManager/Points/Point_Data_Loader.cpp
1

2
#include "Point_Data_Loader.hpp"
3

4
#include "Points/Point_Data.hpp"
5
#include "transforms/data_transforms.hpp"
6
#include "utils/container.hpp"
7
#include "utils/string_manip.hpp"
8

9
#include <fstream>
10
#include <iomanip>
11
#include <iostream>
12
#include <sstream>
13

14
std::shared_ptr<PointData> load_into_PointData(std::string const & file_path, nlohmann::basic_json<> const & item) {
×
15

16
    int const frame_column = item["frame_column"];
×
17
    int const x_column = item["x_column"];
×
18
    int const y_column = item["y_column"];
×
19

20
    std::string const delim = item.value("delim", " ");
×
21

22
    int const height = item.value("height", -1);
×
23
    int const width = item.value("width", -1);
×
24

25
    int const scaled_height = item.value("scale_to_height", -1);
×
26
    int const scaled_width = item.value("scale_to_width", -1);
×
27

28
    auto opts = CSVPointLoaderOptions{.filename = file_path,
×
29
                                      .frame_column = frame_column,
30
                                      .x_column = x_column,
31
                                      .y_column = y_column,
32
                                      .column_delim = delim.c_str()[0]};
×
33

34
    auto keypoints = load_points_from_csv(opts);
×
35

36
    std::cout << "There are " << keypoints.size() << " keypoints " << std::endl;
×
37

38
    auto point_data = std::make_shared<PointData>(keypoints);
×
39
    point_data->setImageSize(ImageSize{.width = width, .height = height});
×
40

41
    if (scaled_height > 0 && scaled_width > 0) {
×
42
        scale(point_data, ImageSize{scaled_width, scaled_height});
×
43
    }
44

45
    return point_data;
×
46
}
×
47

48
//https://stackoverflow.com/questions/4654636/how-to-determine-if-a-string-is-a-number-with-c
49
bool is_number(std::string const & s) {
×
50
    return !s.empty() && std::find_if(s.begin(),
×
51
                                      s.end(), [](unsigned char c) { return !std::isdigit(c); }) == s.end();
×
52
}
53

54
std::map<int, Point2D<float>> load_points_from_csv(CSVPointLoaderOptions const & opts) {
×
55
    std::string csv_line;
×
56

57
    auto line_output = std::map<int, Point2D<float>>{};
×
58

59
    std::fstream myfile;
×
60
    myfile.open(opts.filename, std::fstream::in);
×
61

62
    std::string x_str;
×
63
    std::string y_str;
×
64
    std::string frame_str;
×
65
    std::string col_value;
×
66

67
    std::vector<std::pair<int, Point2D<float>>> csv_vector = {};
×
68

69
    while (getline(myfile, csv_line)) {
×
70

71
        std::stringstream ss(csv_line);
×
72

73
        int cols_read = 0;
×
74
        while (getline(ss, col_value, opts.column_delim)) {
×
75
            if (cols_read == opts.frame_column) {
×
76
                frame_str = col_value;
×
77
            } else if (cols_read == opts.x_column) {
×
78
                x_str = col_value;
×
79
            } else if (cols_read == opts.y_column) {
×
80
                y_str = col_value;
×
81
            }
82
            cols_read++;
×
83
        }
84

85
        if (is_number(frame_str)) {
×
86
            //line_output[std::stoi(frame_str)]=Point2D<float>{std::stof(x_str),std::stof(y_str)};
87
            csv_vector.emplace_back(std::stoi(frame_str), Point2D<float>{std::stof(x_str), std::stof(y_str)});
×
88
        }
89
    }
×
90
    std::cout.flush();
×
91

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

94
    line_output.insert(csv_vector.begin(), csv_vector.end());
×
95

96
    return line_output;
×
97
}
×
98

99
std::map<std::string, std::map<int, Point2D<float>>> load_multiple_points_from_csv(std::string const & filename, int const frame_column) {
×
100
    std::fstream file;
×
101
    file.open(filename, std::fstream::in);
×
102

103
    std::string ln, ele;
×
104

105
    getline(file, ln);// skip the "scorer" row
×
106

107
    getline(file, ln);// bodyparts row
×
108
    std::vector<std::string> bodyparts;
×
109
    {
110
        std::stringstream ss(ln);
×
111
        while (getline(ss, ele, ',')) {
×
112
            bodyparts.push_back(ele);
×
113
        }
114
    }
×
115

116
    getline(file, ln);// coords row
×
117
    std::vector<std::string> dims;
×
118
    {
119
        std::stringstream ss(ln);
×
120
        while (getline(ss, ele, ',')) {
×
121
            dims.push_back(ele);
×
122
        }
123
    }
×
124

125
    std::map<std::string, std::map<int, Point2D<float>>> data;
×
126
    while (getline(file, ln)) {
×
127
        std::stringstream ss(ln);
×
128
        int col_no = 0;
×
129
        int frame_no = -1;
×
130
        while (getline(ss, ele, ',')) {
×
131
            if (col_no == frame_column) {
×
132
                frame_no = std::stoi(extract_numbers_from_string(ele));
×
133
            } else if (dims[col_no] == "x") {
×
134
                data[bodyparts[col_no]][frame_no].x = std::stof(ele);
×
135
            } else if (dims[col_no] == "y") {
×
136
                data[bodyparts[col_no]][frame_no].y = std::stof(ele);
×
137
            }
138
            ++col_no;
×
139
        }
140
    }
×
141

142
    return data;
×
143
}
×
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