Skip to content

Commit 043784d

Browse files
author
Mario Fink
committed
* rawmerge.hpp: generalize/fix condition for consistent timeseries
* introduce propagation of C++ exceptions to Cython/Python * convert all cout/cerr output to exceptions * Cython: introduce separate 'do_conversion' => avoid constructor to trigger conversion
1 parent 3611e43 commit 043784d

File tree

9 files changed

+137
-130
lines changed

9 files changed

+137
-130
lines changed

.gitignore

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11

2-
/build
3-
42
eatraw
53
eatdev
64

75
nohup.out
86

97
.DS_Store
108

11-
*.so
12-
139
raw_eater.cpp
1410
raw_meat.cpp
1511

1612
cyt/*.cpp
13+
/build
1714

15+
*.log
16+
*.so
1817
*.o
18+
*.csv
19+
*.parquet

cyt/raw_eater.pxd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ from libcpp cimport bool
1414
#
1515
# for how to overload the constructor see
1616
# https://cython.readthedocs.io/en/latest/src/userguide/wrapping_CPlusPlus.html
17+
# and propagating exceptions from C++ to Python
18+
# http://docs.cython.org/en/latest/src/userguide/wrapping_CPlusPlus.html#exceptions
1719

1820
cdef extern from "../lib/raweat.hpp":
1921
cdef cppclass raw_eater:
2022
# constructor(s)
21-
raw_eater() except+
23+
raw_eater() except +
2224
raw_eater(string) except +
2325
# set new file for decoding
2426
void set_file(string)
27+
# perform conversion (pass any C++ exceptions to Python)
28+
void setup_and_conversion() except +
2529
# get validity of data format
2630
bool get_valid()
2731
# get channel name and unit

cyt/raw_eater.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ cdef class raweater:
3030
raise ValueError("'" + str(rawfile) + "' does not exist")
3131
self.rawit.set_file(rawfile)
3232

33+
def do_conversion(self):
34+
self.rawit.setup_and_conversion()
35+
3336
def validity(self):
3437
return self.rawit.get_valid()
3538

cyt/raw_meat.pxd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ cdef extern from "../lib/rawmerge.hpp":
2323
vector[double] get_data()
2424
# dump all data to .csv
2525
void write_table(const char*,char)
26-
# add channel and try to merge it
27-
bool add_channel(string)
26+
# add channel and try to merge it (pass C++ exceptions to Python)
27+
bool add_channel(string) except +
2828
# get total number of (added) channels
2929
int get_num_channels()
3030
# get list of channel names

lib/raweat.hpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,20 @@ class raw_eater
8080
raw_eater(std::string rawfile, bool showlog = false) : rawfile_(rawfile)
8181
{
8282
// trigger setup and conversion
83-
if ( ! rawfile.empty() ) this->setup_and_conversion(showlog);
83+
//if ( ! rawfile.empty() ) this->setup_and_conversion(showlog);
8484
}
8585

8686
raw_eater()
8787
{
8888

8989
}
9090

91+
// destructor
92+
~raw_eater()
93+
{
94+
95+
}
96+
9197
// provide/set new raw file
9298
void set_file(std::string rawfile, bool showlog = false)
9399
{
@@ -100,11 +106,11 @@ class raw_eater
100106
datmes_.clear();
101107

102108
// do setup and conversion
103-
setup_and_conversion(showlog);
109+
//setup_and_conversion(showlog);
104110
}
105111

106112
// set up and conversion
107-
void setup_and_conversion(bool showlog)
113+
void setup_and_conversion(bool showlog = false)
108114
{
109115
// make sure 'raw_file' is already set
110116
assert ( !rawfile_.empty() );
@@ -123,10 +129,8 @@ class raw_eater
123129

124130
if ( showlog )
125131
{
126-
// show raw data
132+
// show raw data and display predefined markers
127133
this->show_buffer();
128-
129-
// display predefined markers
130134
this->show_markers();
131135
}
132136

@@ -150,12 +154,6 @@ class raw_eater
150154
}
151155
}
152156

153-
// destructor
154-
~raw_eater()
155-
{
156-
157-
}
158-
159157
// display buffer/data properties
160158
void show_buffer(int numel = 32)
161159
{
@@ -239,18 +237,23 @@ class raw_eater
239237
unsigned long int totalmarksize = 0;
240238
for (std::pair<std::string,std::vector<unsigned char>> mrk : markers_ )
241239
{
242-
//assert ( datasec_[mrk.first].size() > 0 && "marker segment of length zero" );
243240
if ( datasec_[mrk.first].size() == 0 )
244241
{
245-
std::cout<<"warning: "<<mrk.first<<" not found in buffer\n";
242+
std::string errmess = mrk.first + std::string(" not found in buffer");
243+
// std::cerr<<errmess;
244+
try {
245+
throw std::runtime_error(errmess);
246+
} catch( const std::exception& e ) {
247+
throw;
248+
//std::cout<<e.what()<<"\n";
249+
}
246250
}
247251
totalmarksize += datasec_[mrk.first].size();
248252
}
249253
// std::cout<<"totalmarksize "<<totalmarksize<<"\n";
250254
// std::cout<<"\n";
251255

252256
// check validity of format
253-
// assert ( totalmarksize > 0 && "didn't find any predefined marker => probably not a valid .raw-file" );
254257
valid_ = ( totalmarksize < 100 ) ? false : true;
255258
}
256259

@@ -389,7 +392,6 @@ class raw_eater
389392
}
390393
else
391394
{
392-
393395
// switch for datatypes
394396
switch ( dattype )
395397
{
@@ -414,9 +416,9 @@ class raw_eater
414416
convert_data_as_type<unsigned long int>(datbuf,factor,offset);
415417
break;
416418
case 6 :
417-
std::cout<<"warning: 'signed long int' datatype with experimental support\n";
418419
// assert ( sizeof(signed long int)*8 == typesize );
419420
// convert_data_as_type<signed long int>(datbuf,factor,offset);
421+
// throw std::runtime_error("warning: 'signed long int' datatype with experimental support");
420422
assert ( sizeof(int)*8 == typesize );
421423
convert_data_as_type<int>(datbuf,factor,offset);
422424
break;
@@ -429,13 +431,13 @@ class raw_eater
429431
convert_data_as_type<double>(datbuf,factor,offset);
430432
break;
431433
case 9 :
432-
std::cerr<<"'imc Devices Transitional Recording' datatype not supported\n";
434+
throw std::runtime_error("'imc Devices Transitional Recording' datatype not supported");
433435
break;
434436
case 10 :
435-
std::cerr<<"'Timestamp Ascii' datatype not supported\n";
437+
throw std::runtime_error("'Timestamp Ascii' datatype not supported");
436438
break;
437439
case 11 :
438-
std::cout<<"warning: '2-Byte-Word digital' datatype with experimental support\n";
440+
// throw std::runtime_error("warning: '2-Byte-Word digital' datatype with experimental support");
439441
assert ( sizeof(short int)*8 == typesize );
440442
convert_data_as_type<short int>(datbuf,factor,offset);
441443
break;

lib/rawmerge.hpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,19 @@ class raw_merger : public raw_eater
8383
{
8484
// set raw file and perform conversion
8585
this->set_file(rawfile,showlog);
86+
// try {
87+
this->setup_and_conversion();
88+
// } catch (const std::exception& e) {
89+
// // throw;
90+
// std::cout<<e.what();
91+
// }
8692

8793
// show channel name, unit, timestep, time unit, etc.
8894
if ( showlog && this->get_valid() )
8995
{
90-
std::cout<<this->get_name()<<" ["<<this->get_unit()<<"]"<<"\n";
9196
std::cout<<"Time ["<<this->get_temp_unit()<<"]"<<"\n";
9297
for ( unsigned long int i = 0; i < 5; i++ ) std::cout<<this->get_time()[i]<<"\n";
98+
std::cout<<this->get_name()<<" ["<<this->get_unit()<<"]"<<"\n";
9399
for ( unsigned long int i = 0; i < 5; i++ ) std::cout<<this->get_data()[i]<<"\n";
94100
std::cout<<"lenght of channel "<<this->get_time().size()<<"\n";
95101
std::cout<<"\n";
@@ -126,12 +132,9 @@ class raw_merger : public raw_eater
126132
std::vector<double> td = this->get_data();
127133

128134
// compare start/end of timeseries (define tolerance)
129-
double deltat = 7*fmax(this->get_dt(),this->dt_);
130-
if ( ( this->timeseries_[0] - ts[0] < deltat )
131-
&& ( this->timeseries_.back() - ts.back() < deltat ) )
132-
// double tol = 0.001;
133-
// if ( ( (this->timeseries_[0]-ts[0])/ts[0] < tol )
134-
// && ( (this->timeseries_.back()-ts.back())/ts.back() < tol ) )
135+
double deltat = 9*fmax(this->get_dt(),this->dt_);
136+
if ( ( fabs( this->timeseries_[0] - ts[0] ) < deltat )
137+
&& ( fabs( this->timeseries_.back() - ts.back() ) < deltat ) )
135138
{
136139
// resulting new time series
137140
std::vector<double> newts;
@@ -155,27 +158,38 @@ class raw_merger : public raw_eater
155158
else
156159
{
157160
// refuse to merge due to inconsistent start of timeseries
158-
std::cerr<<"rawmerge: add_channel '"<<rawfile
159-
<<"' : inconsistent start of time series, i.e. "
160-
<<timeseries_[0]<<" vs. "<<ts[0]<<"\n";
161+
std::string errmess = std::string("rawmerge: add_channel '") + rawfile
162+
+ std::string("' : inconsistent start of time series, i.e. ")
163+
+ std::to_string(timeseries_[0]) + std::string(" vs. ")
164+
+ std::to_string(ts[0])
165+
+ std::string("( timesteps: ") + std::to_string(this->get_dt())
166+
+ std::string(" and ") + std::to_string(this->dt_) + std::string(")");
167+
// std::cerr<<errmess<<"\n";
168+
throw std::runtime_error(errmess);
161169

162170
return false;
163171
}
164172
}
165173
else
166174
{
167175
// refuse to merge due to different temporal units
168-
std::cerr<<"rawmerge: add_channel '"<<rawfile
169-
<<"' : inconsistent time units: '"
170-
<<this->get_temp_unit()<<"' versus '"<<this->temp_unit_<<"'\n";
176+
std::string errmess = std::string("rawmerge: add_channel '") + rawfile
177+
+ std::string("' : inconsistent time units: ', i.e. '")
178+
+ this->get_temp_unit() + std::string("' vs. '")
179+
+ this->temp_unit_ + std::string("'");
180+
// std::cerr<<errmess<<"\n";
181+
throw std::runtime_error(errmess);
171182

172183
return false;
173184
}
174185
}
175186
else
176187
{
177188
// provided file does not feature a valid .raw format
178-
std::cerr<<"rawmerge: add_channel '"<<rawfile<<"' : invalid .raw file\n";
189+
std::string errmess = std::string("rawmerge: add_channel '") + rawfile
190+
+ std::string("' : invalid .raw file");
191+
// std::cerr<<errmess<<"\n";
192+
throw std::runtime_error(errmess);
179193

180194
return false;
181195
}

makefile

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ EXE = eatraw
1515

1616
# compiler and its options
1717
CCC = g++ -std=c++11
18-
OPT = -O3 -Wall -mavx -mno-tbm -mf16c -mno-f16c
18+
#OPT = -O3 -Wall -mavx -mno-tbm -mf16c -mno-f16c
19+
OPT = -O3 -Wall -Werror
1920

2021
#-----------------------------------------------------------------------------#
2122
# C++
2223

2324
# build executable
24-
$(EXE) : $(SRC)main.cpp $(LIB)raweat.hpp $(LIB)hexshow.hpp $(LIB)rawmerge.hpp
25+
$(EXE) : $(SRC)main.cpp $(LIB)raweat.hpp $(LIB)hexshow.hpp $(LIB)rawmerge.hpp output
2526
$(CCC) $(OPT) $< -o $@
2627

2728
# development version
@@ -37,6 +38,7 @@ clean :
3738
rm -f $(EXE)
3839
rm -f eatall
3940
rm -f eatdev
41+
rm -rf output/
4042

4143
# check existence of name of executable globally
4244
chexe:=$(shell command -v $(EXE))
@@ -59,7 +61,8 @@ uninstall :
5961

6062
# build python module
6163
py : $(CYT)setup_raw_eater.py $(CYT)raw_eater.pyx $(CYT)raw_eater.pxd $(LIB)raweat.hpp \
62-
$(CYT)setup_raw_meat.py $(CYT)raw_meat.pyx $(CYT)raw_meat.pxd $(LIB)rawmerge.hpp
64+
$(CYT)setup_raw_meat.py $(CYT)raw_meat.pyx $(CYT)raw_meat.pxd $(LIB)rawmerge.hpp \
65+
output
6366
python3 $(CYT)setup_raw_eater.py build_ext --inplace
6467
python3 $(CYT)setup_raw_meat.py build_ext --inplace
6568
cp raw_eater.cpython-*.so pyt/
@@ -80,5 +83,10 @@ py-clean :
8083
rm -f $(CYT)raw_meat.cpp
8184
rm -rf build/
8285
rm -f *.txt
86+
rm -rf output/
87+
88+
# prepare directory for test output
89+
output :
90+
mkdir -pv output/
8391

8492
#-----------------------------------------------------------------------------#

0 commit comments

Comments
 (0)