-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSxConfig.cpp
312 lines (234 loc) · 8.51 KB
/
NSxConfig.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
303
304
305
306
307
308
309
310
311
312
#include "NSxConfig.h"
NSxConfig::NSxConfig(void) : _valid(false), desc("Convert a Ripple NSx file to losslessly-compressed FLAC files") {
desc.add_options()
("help", "Show this help message")
("input,i",
opts::value<std::string>(),
"NSx file to convert. If a directory, convert all NSx files in the directory.")
("output-dir,o",
opts::value<std::string>()->default_value("./"),
"Place the converted FLAC files in this directory")
("output-prefix",
opts::value<std::string>()->default_value(""),
"The converted FLAC filenames start with this, followed by the channel number")
("threads",
opts::value<unsigned>()->default_value(1),
"Number of threads to use for compression")
("read-size",
opts::value<unsigned>()->default_value(60000),
"Maximum number of samples to read at once")
("flac-compression",
opts::value<unsigned>()->default_value(8),
"FLAC compression level")
("matlab-header",
opts::value<bool>()->default_value(true),
"Write header/metadata as a Matlab file?")
("text-header",
opts::value<bool>()->default_value(true),
"Write header/metadata as a text file?")
("compress-data",
opts::value<bool>()->default_value(true),
"Compress the data in the NSx file?")
;
pos.add("input", 1);
pos.add("output-dir", 2);
pos.add("output-prefix", 3);
}
unsigned int NSxConfig::nThreads(void) const {
if(_valid)
return _nThreads;
else
throw(std::runtime_error("Options not initalized"));
}
unsigned int NSxConfig::readSize(void) const {
if(_valid)
return _readSize;
else
throw(std::runtime_error("Options not initalized"));
}
unsigned int NSxConfig::flacCompression(void) const {
if(_valid)
return _flacCompression;
else
throw(std::runtime_error("Options not initalized"));
}
std::string NSxConfig::input(void) const {
if(_valid)
return _input;
else
throw(std::runtime_error("Options not initalized"));
}
std::string NSxConfig::outputDir(void) const {
if(_valid)
return _outputDir;
else
throw(std::runtime_error("Options not initalized"));
}
std::string NSxConfig::outputPrefix(void) const {
if(!_valid)
throw(std::runtime_error("Options not initalized"));
return _outputPrefix;
}
bool NSxConfig::matlabHeader(void) const {
if(_valid)
return _matlabHeader;
else
throw(std::runtime_error("Options not initalized"));
}
bool NSxConfig::textHeader(void) const {
if(_valid)
return _textHeader;
else
throw(std::runtime_error("Options not initalized"));
}
bool NSxConfig::compressData(void) const {
if(_valid)
return _compressData;
else
throw(std::runtime_error("Options not initalized"));
}
void NSxConfig::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);
// Check output directory and return it if valid
setOutputDir(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) + "_ch";
}
_nThreads = vm["threads"].as<unsigned>();
_readSize = vm["read-size"].as<unsigned>();
_flacCompression = vm["flac-compression"].as<unsigned>();
_matlabHeader = vm["matlab-header"].as<bool>();
_textHeader = vm["text-header"].as<bool>();
_compressData = vm["compress-data"].as<bool>();
_valid = true;
}
void NSxConfig::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."));
}
}
void NSxConfig::setOutputDir(const opts::variables_map& vm) {
this->_outputDir = vm["output-dir"].as<std::string>();
this->outputPath = fs::path(this->_outputDir);
if(fs::exists(this->outputPath) && fs::is_regular_file(this->outputPath))
throw(std::runtime_error("Cannot create a directory with the same name as a file!"));
fs::create_directories(this->outputPath); //Throws on failure, does nothing if exists
return;
}
void NSxConfig::setOutputDir(const fs::path &p) {
this->outputPath = p;
this->_outputDir = p.string();
if(fs::exists(this->outputPath) && fs::is_regular_file(this->outputPath))
throw(std::runtime_error("Cannot create a directory with the same name as a file!"));
fs::create_directories(this->outputPath); //Throws on failure, does nothing if exists
return;
}
std::string NSxConfig::outputFilename(std::uint16_t electrode, bool withPath) const {
std::ostringstream str;
str << outputPrefix() << std::setfill('0') << std::setw(3) << electrode;
str << ".flac";
if(withPath)
return(outputPath / str.str()).string();
else
return str.str();
}
std::string NSxConfig::matlabHeaderFilename() const {
std::string filename = outputPrefix();
auto startAt = filename.find_last_of("_");
if(startAt == std::string::npos) {
return (outputPath / "header.mat").string();
} else {
filename.replace(startAt, std::string::npos, ".mat");
return (outputPath / filename).string();
}
}
std::string NSxConfig::textHeaderFilename() const {
std::string filename = outputPrefix();
auto startAt = filename.find_last_of("_");
if(startAt == std::string::npos) {
return (outputPath / "header.txt").string();
} else {
filename.replace(startAt, std::string::npos, ".txt");
return (outputPath / filename).string();
}
}
WorkQueue NSxConfig::toWorkQueue() {
WorkQueue 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() == ".ns5") {
NSxConfig fileConfig(*this);
fileConfig._input = p.string();
fs::path out = this->outputPath / p.stem();
fileConfig.setOutputDir(out);
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 NSxConfig &c) {
out << "Ripple-To-FLac Conversation Configuration: " << std::endl <<
"\t Input: " << c._input << std::endl <<
"\t Output Directory: " << c._outputDir << std::endl <<
"\t Running in " << (c._singleFile ? "single file mode" : "directory mode") << std::endl <<
std::endl <<
"\t Writing Matlab header: " << (c._matlabHeader ? "Yes" : "No") << std::endl <<
"\t Writing text header: " << (c._textHeader ? "Yes" : "No") << std::endl <<
"\t Writing compressed data: " << (c._compressData ? "Yes": "No") << std::endl <<
std::endl <<
"\t Output Prefix: " << c._outputPrefix << std::endl <<
"\t Compression level: " << c._flacCompression << std::endl <<
"\t # of threads: " << c._nThreads << std::endl <<
"\t I/O Block Size: " << c._readSize << std::endl <<
std::endl;
if(c._singleFile) {
out << "\t First file will be called: " << c.outputFilename(1, true) <<
std::endl;
} else {
out << "\t No files generated. Unpack this into per-file configuration." <<
std::endl;
}
return out;
}