CPPxDIC
C++ Digital Image Correlation library for fingertip 3D reconstruction
Loading...
Searching...
No Matches
trial_selection.h
Go to the documentation of this file.
1
10#ifndef TRIAL_SELECTION_H
11#define TRIAL_SELECTION_H
12
13#include <cctype>
14#include <cstdlib>
15#include <fstream>
16#include <sstream>
17#include <stdexcept>
18#include <string>
19#include <vector>
20
21namespace cppxdic {
22
25 std::string subject;
26 int trial = 0;
27};
28
30inline std::string trim(const std::string& s) {
31 const char* ws = " \t\r\n";
32 size_t b = s.find_first_not_of(ws);
33 if (b == std::string::npos) return "";
34 size_t e = s.find_last_not_of(ws);
35 return s.substr(b, e - b + 1);
36}
37
42inline std::vector<int> parseTrialList(const std::string& spec) {
43 std::vector<int> trials;
44 std::string token;
45 for (char c : spec) {
46 if (c == ',' || c == ' ' || c == '\t' || c == ';') {
47 if (!token.empty()) {
48 trials.push_back(std::stoi(token));
49 token.clear();
50 }
51 } else {
52 token.push_back(c);
53 }
54 }
55 if (!token.empty()) trials.push_back(std::stoi(token));
56 return trials;
57}
58
63inline std::vector<int> readTrialsFile(const std::string& path) {
64 std::ifstream in(path);
65 if (!in) {
66 throw std::runtime_error("Cannot open trials file: " + path);
67 }
68 std::vector<int> trials;
69 std::string line;
70 while (std::getline(in, line)) {
71 std::string t = trim(line);
72 if (t.empty() || t[0] == '#') continue;
73 trials.push_back(std::stoi(t));
74 }
75 return trials;
76}
77
83inline std::vector<SubjectTrial> readSubjectTrialCsv(const std::string& path) {
84 std::ifstream in(path);
85 if (!in) {
86 throw std::runtime_error("Cannot open subject_trial CSV: " + path);
87 }
88 std::vector<SubjectTrial> rows;
89 std::string line;
90 bool header_skipped = false;
91 while (std::getline(in, line)) {
92 std::string t = trim(line);
93 if (t.empty() || t[0] == '#') continue;
94 // Skip the header line ("subject,trial") once.
95 if (!header_skipped) {
96 header_skipped = true;
97 std::string lower;
98 for (char c : t)
99 lower.push_back(static_cast<char>(std::tolower(static_cast<unsigned char>(c))));
100 if (lower.rfind("subject", 0) == 0) continue;
101 }
102 std::stringstream ss(t);
103 std::string subj, trial_str;
104 if (!std::getline(ss, subj, ',')) continue;
105 if (!std::getline(ss, trial_str, ',')) {
106 throw std::runtime_error("Malformed line in " + path + ": " + line);
107 }
108 SubjectTrial row;
109 row.subject = trim(subj);
110 row.trial = std::stoi(trim(trial_str));
111 rows.push_back(row);
112 }
113 return rows;
114}
115
120inline int slurmArrayTaskId() {
121 const char* env = std::getenv("SLURM_ARRAY_TASK_ID");
122 if (!env || *env == '\0') return -1;
123 return std::stoi(std::string(env));
124}
125
126} // namespace cppxdic
127
128#endif // TRIAL_SELECTION_H
DataSerializer - Abstract base class for format-agnostic DIC data I/O.
std::vector< int > parseTrialList(const std::string &spec)
Parse a comma/space-separated list of integer trial IDs.
std::string trim(const std::string &s)
Trim leading/trailing whitespace (spaces, tabs, CR, LF).
int slurmArrayTaskId()
Resolve SLURM_ARRAY_TASK_ID from the environment.
std::vector< int > readTrialsFile(const std::string &path)
Read a trials file: one trial ID per line.
std::vector< SubjectTrial > readSubjectTrialCsv(const std::string &path)
Read subject_trial.csv (header "subject,trial") into rows, skipping the header line and any blank/com...
A single (subject, trial) row, e.g.