-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNEVConfig.cpp
302 lines (222 loc) · 8.19 KB
/
NEVConfig.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "NEVConfig.h"
#include <map>
NEVConfig::NEVConfig(void) :
_valid(false),
desc("Extract events from a Ripple NEV (Neural Events) file") {
desc.add_options()
("help", "Show this help message")
("input",
opts::value<std::string>(),
"NEV file to convert.\nIf a directory, convert all NEV files in the directory.")
("output-prefix",
opts::value<std::string>()->default_value(""),
"Output filenames begin with this prefix (defaults to the stem of the .nev file)")
("events-filetype",
opts::value<std::string>()->default_value("matlab,text"),
"If empty, or omitted, digital events are ignored. Otherwise, digital events are written in the specified formats. Supply more than one as a comma-separated list.\n\t- matlab: MATLAB .mat file (version 7).\n\t- csv: comma-separated values\n\t- text: Pretty-printed text")
("stim-filetype",
opts::value<std::string>()->default_value(""),
"If empty or omitted, stimulation events are ignored. Otherwise, stimulation events are written in the specified formats. Supply more than one as a comma separated list.\n\t- matlab: MATLAB .mat file (version 7)\n\t- csv: comma-separated values\n\t- text: Pretty-printed text (no waveforms, regardless of include-stim-waveforms).")
("spike-filetype",
opts::value<std::string>()->default_value(""),
"Currently unimplemented--throws if not empty. (This wouldn't be hard to fix). ")
("include-stim-waveforms",
opts::value<bool>()->default_value(true),
"Include stimulation waveforms in output?. Waveforms are never included in the text file.")
("include-spike-waveforms",
opts::value<bool>()->default_value(false),
"Include spike waveforms in output?");
pos.add("input", 1);
pos.add("output-prefix", 2);
exts = {
{OutputFormat::TEXT, "txt"},
{OutputFormat::CSV, "csv"},
{OutputFormat::MATLAB, "mat"}
};
};
std::string NEVConfig::input(void) const {
if(!_valid)
throw(std::runtime_error("Options not initalized"));
return _input;
}
std::string NEVConfig::outputPrefix(void) const {
if(!_valid)
throw(std::runtime_error("Options not initalized"));
return _outputPrefix;
}
size_t NEVConfig::bufferSize(void) const {
if(!_valid)
throw(std::runtime_error("Options not initalized"));
return _bufferSize;
}
void NEVConfig::parse(int argc, char* argv[]) {
opts::variables_map vm;
opts::store(opts::command_line_parser(argc, argv).
options(desc).positional(pos).run(), vm);
if(vm.count("help") || argc==1) {
std::cout << desc << std::endl;
exit(0);
}
opts::notify(vm);
// Check input file/dir and return it if valid
setInput(vm);
_outputPrefix = vm["output-prefix"].as<std::string>();
if(_singleFile && _outputPrefix.empty()) {
fs::path p(_input);
std::string f = p.filename().string();
auto loc = f.find_last_of(".");
_outputPrefix = f.substr(0, loc) ;
}
_eventFileTypes = parseFormatString(vm["events-filetype"].as<std::string>());
_stimFileTypes = parseFormatString(vm["stim-filetype"].as<std::string>());
_spikeFileTypes = parseFormatString(vm["spike-filetype"].as<std::string>());
_stimWaves = vm["include-stim-waveforms"].as<bool>();
_spikeWaves = vm["include-spike-waveforms"].as<bool>();
_valid = true;
}
void NEVConfig::setInput(const opts::variables_map& vm) {
if(!vm.count("input"))
throw(std::runtime_error("No input file or directory found!"));
this->_input = vm["input"].as<std::string>();
fs::path inputPath(_input);
if(fs::exists(inputPath)) {
if(fs::is_regular_file(inputPath) || fs::is_symlink(inputPath)) {
_singleFile = true;
} else if(fs::is_directory(inputPath)) {
_singleFile = false;
} else {
throw(std::runtime_error("The input file is not a regular file or directory."));
}
} else {
throw(std::runtime_error("The input file does not exist."));
}
}
NEVWorkQueue NEVConfig::toWorkQueue() {
NEVWorkQueue work;
//Easy case: inputFile is a single file
if(isSingleFileConfig()) {
work.push_back(*this);
return work;
}
/*Harder case: inputFile is a directory and we want to process all
NSx files inside it. We want to extract inputDir/a.ns5 -->
outputDir/a/, inputDir/b.ns5 --> outputDir/b/, and so on */
fs::directory_iterator input_iter(_input);
fs::directory_iterator end_of_dir; //Default ctor --> special "end" value
for(fs::directory_iterator i(_input); i!=end_of_dir; ++i) {
fs::path p = i->path();
if((fs::is_regular_file(p) || fs::is_symlink(p)) && p.extension() == ".nev") {
NEVConfig fileConfig(*this);
fileConfig._input = p.string();
fs::path out = this->outputPath / p.stem();
fileConfig._singleFile = true;
if(fileConfig.outputPrefix().empty()) {
fs::path p(fileConfig._input);
std::string f = p.filename().string();
auto loc = f.find_last_of(".");
fileConfig._outputPrefix = f.substr(0, loc) + "_ch";
}
work.push_back(fileConfig);
}
}
return work;
}
std::ostream& operator<<(std::ostream &out, const NEVConfig &c) {
out << "Ripple Event Extraction Configuration: " << std::endl <<
"\t Input: " << c._input << '\n' <<
"\t Output Prefix: " << c._outputPrefix << "\n\n";
auto ev = c.eventFileTypes();
if(ev.empty()) {
out << "\t Digital events ignored.\n";
} else {
out << "\t Digital events extracted to\n";
for(auto fmt : ev) {
out << "\t \t* " << c.eventFilename(fmt) << " (" << fmt << ")\n";
}
}
auto stim = c.stimFileTypes();
if(stim.empty()) {
out << "\t Stimulation events ignored.\n";
} else {
out << "\t Stimulation events extracted to\n";
for(auto fmt: stim) {
out << "\t \t* " << c.stimFilename(fmt) << " (" << fmt
<< (c.includeStimWaves() && fmt != OutputFormat::TEXT ? "#" : "")
<< ")\n";
}
}
auto spike = c.spikeFileTypes();
if(spike.empty()) {
out << "\t Spike events ignored.\n";
} else {
out << "\t Spike events extracted to\n";
for(auto fmt: spike) {
out << "\t \t* " << c.spikeFilename(fmt) << " (" << fmt
<< (c.includeSpikeWaves() && fmt != OutputFormat::TEXT ? "#" : "")
<< ")\n";
}
}
out << "\t (#) Includes waveform data.\n" << std::endl;
return out;
}
std::vector<OutputFormat> NEVConfig::eventFileTypes(void) const {
if(!valid())
throw(std::runtime_error("Configuration is not valid"));
return _eventFileTypes;
}
std::vector<OutputFormat> NEVConfig::stimFileTypes(void) const {
if(!valid())
throw(std::runtime_error("Configuration is not valid"));
return _stimFileTypes;
}
std::vector<OutputFormat> NEVConfig::spikeFileTypes(void) const {
if(!valid())
throw(std::runtime_error("Configuration is not valid"));
return _spikeFileTypes;
}
std::vector<OutputFormat> NEVConfig::parseFormatString(const std::string &fmtstr) {
std::vector<OutputFormat> formats;
std::stringstream ss(fmtstr);
std::string token;
const char DELIM = ',';
const std::map<std::string, OutputFormat> VALID_FORMATS {
{"matlab", OutputFormat::MATLAB},
{"csv", OutputFormat::CSV},
{"text", OutputFormat::TEXT}
};
while(getline(ss, token, DELIM)) {
auto res = VALID_FORMATS.find(token);
if(res == VALID_FORMATS.end())
throw(std::runtime_error("Unrecognized format " + token + "found"));
else {
formats.push_back(res->second);
}
}
return formats;
}
std::string NEVConfig::eventFilename(const OutputFormat type) const {
std::string filename = _outputPrefix + "-events." + exts.find(type)->second;
return filename;
}
std::string NEVConfig::stimFilename(const OutputFormat type) const {
std::string filename = _outputPrefix + "-microstim." + exts.find(type)->second;
return filename;
}
std::string NEVConfig::spikeFilename(const OutputFormat type) const {
std::string filename = _outputPrefix + "-online-spikes." + exts.find(type)->second;
return filename;
}
std::ostream& operator<<(std::ostream &out, OutputFormat of) {
switch(of) {
case OutputFormat::TEXT:
out << "text";
break;
case OutputFormat::CSV:
out << "CSV";
break;
case OutputFormat::MATLAB:
out << "matlab";
break;
}
return out;
}