added cython exension
This commit is contained in:
parent
46044ec969
commit
a08fab03ba
BIN
build/lib.linux-x86_64-3.6/raw_eater.cpython-36m-x86_64-linux-gnu.so
Executable file
BIN
build/lib.linux-x86_64-3.6/raw_eater.cpython-36m-x86_64-linux-gnu.so
Executable file
Binary file not shown.
BIN
build/temp.linux-x86_64-3.6/raw_eater.o
Normal file
BIN
build/temp.linux-x86_64-3.6/raw_eater.o
Normal file
Binary file not shown.
12
example.py
Normal file
12
example.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
import raw_eater
|
||||||
|
|
||||||
|
eatraw = raw_eater.raweater(b"/home/mario/raw_eater/sample/VehicleSpeed_HS.raw")
|
||||||
|
|
||||||
|
print(eatraw.channel_name())
|
||||||
|
print(eatraw.unit())
|
||||||
|
|
||||||
|
print(eatraw.get_time())
|
||||||
|
print(eatraw.get_channel())
|
||||||
|
|
||||||
|
eatraw.print(b"mycsv.csv")
|
15
makefile
15
makefile
@ -29,7 +29,18 @@ else
|
|||||||
@exit 1
|
@exit 1
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# deinstall
|
# uninstall
|
||||||
deinstall :
|
uninstall :
|
||||||
rm /usr/local/bin/$(EXE)
|
rm /usr/local/bin/$(EXE)
|
||||||
|
|
||||||
|
# build python module
|
||||||
|
build : setup.py raw_eater.pyx raw_eater.pxd $(SRC)raweat.hpp
|
||||||
|
python3 setup.py build_ext --inplace
|
||||||
|
|
||||||
|
install_py : setup.py raw_eater.pyx raw_eater.pxd $(SRC)raweat.hpp
|
||||||
|
python3 setup.py install
|
||||||
|
|
||||||
|
clean_py :
|
||||||
|
rm -f raw_eater.cpython-36m-x86_64-linux-gnu.so
|
||||||
|
rm -f raw_eater.cpp
|
||||||
|
|
||||||
|
27
raw_eater.pxd
Normal file
27
raw_eater.pxd
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
# cython: language_level = 3
|
||||||
|
# distutils: language = c++
|
||||||
|
|
||||||
|
# use some C++ STL libraries
|
||||||
|
from libcpp.string cimport string
|
||||||
|
from libcpp.vector cimport vector
|
||||||
|
from libcpp cimport bool
|
||||||
|
|
||||||
|
#cdef extern from "raweat.hpp":
|
||||||
|
# pass
|
||||||
|
|
||||||
|
# these method names have to match the C definitions of the methods!!
|
||||||
|
|
||||||
|
cdef extern from "raweat.hpp":
|
||||||
|
cdef cppclass raw_eater:
|
||||||
|
raw_eater(string) except +
|
||||||
|
# get channel name and unit
|
||||||
|
string get_name()
|
||||||
|
string get_unit()
|
||||||
|
# get time step and time unit
|
||||||
|
double get_dt()
|
||||||
|
string get_temp_unit()
|
||||||
|
# get data array of time and measured quantity's channel
|
||||||
|
vector[double] get_time()
|
||||||
|
vector[double] get_data()
|
||||||
|
# dump all data to .csv
|
||||||
|
void write_data(const char*)
|
37
raw_eater.pyx
Normal file
37
raw_eater.pyx
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
|
||||||
|
from raw_eater cimport raweater
|
||||||
|
import numpy as np
|
||||||
|
import re
|
||||||
|
|
||||||
|
cdef class raweater:
|
||||||
|
|
||||||
|
# pointer to C++ instance (since there's no nullary constructor)
|
||||||
|
cdef raw_eater *rawit
|
||||||
|
|
||||||
|
def __cinit__(self, string rawfile):
|
||||||
|
self.rawit = new raw_eater(rawfile)
|
||||||
|
|
||||||
|
def __dealloc__(self):
|
||||||
|
del self.rawit
|
||||||
|
|
||||||
|
def channel_name(self):
|
||||||
|
return self.rawit.get_name()
|
||||||
|
|
||||||
|
def unit(self):
|
||||||
|
return self.rawit.get_unit()
|
||||||
|
|
||||||
|
def dt(self):
|
||||||
|
return self.rawit.get_dt()
|
||||||
|
|
||||||
|
def time_unit(self):
|
||||||
|
return self.rawit.get_temp_unit()
|
||||||
|
|
||||||
|
def get_time(self):
|
||||||
|
return self.rawit.get_time()
|
||||||
|
|
||||||
|
def get_channel(self):
|
||||||
|
return self.rawit.get_data()
|
||||||
|
|
||||||
|
def print(self, const char* csvfile):
|
||||||
|
return self.rawit.write_data(csvfile)
|
||||||
|
|
19
setup.py
Normal file
19
setup.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
from distutils.core import setup
|
||||||
|
from distutils.extension import Extension
|
||||||
|
from Cython.Build import cythonize
|
||||||
|
|
||||||
|
extensions = Extension(
|
||||||
|
name="raw_eater",
|
||||||
|
sources=["raw_eater.pyx"],
|
||||||
|
# libraries=[""],
|
||||||
|
library_dirs=["src"],
|
||||||
|
include_dirs=["src"],
|
||||||
|
language='c++',
|
||||||
|
extra_compile_args=['-std=c++11','-Wno-unused-variable'],
|
||||||
|
extra_link_args=['-std=c++11'],
|
||||||
|
)
|
||||||
|
|
||||||
|
setup(
|
||||||
|
name="raw_eater",
|
||||||
|
ext_modules=cythonize(extensions)
|
||||||
|
)
|
@ -71,16 +71,20 @@ public:
|
|||||||
// open file and put data in buffer
|
// open file and put data in buffer
|
||||||
std::ifstream fin(rawfile.c_str(),std::ifstream::binary);
|
std::ifstream fin(rawfile.c_str(),std::ifstream::binary);
|
||||||
assert( fin.good() && "failed to open file" );
|
assert( fin.good() && "failed to open file" );
|
||||||
try {
|
// try {
|
||||||
std::ifstream fin(rawfile.c_str(),std::ifstream::binary);
|
// std::ifstream fin(rawfile.c_str(),std::ifstream::binary);
|
||||||
}
|
// }
|
||||||
catch (std::ifstream::failure e) {
|
// catch (std::ifstream::failure e) {
|
||||||
std::cerr<<"opening file " + rawfile + " failed";
|
// std::cerr<<"opening file " + rawfile + " failed";
|
||||||
}
|
// }
|
||||||
std::vector<unsigned char> rawdata((std::istreambuf_iterator<char>(fin)),
|
std::vector<unsigned char> rawdata((std::istreambuf_iterator<char>(fin)),
|
||||||
(std::istreambuf_iterator<char>()));
|
(std::istreambuf_iterator<char>()));
|
||||||
rawdata_ = rawdata;
|
rawdata_ = rawdata;
|
||||||
|
|
||||||
|
// prepare and convert data
|
||||||
|
find_markers();
|
||||||
|
split_segments();
|
||||||
|
convert_data();
|
||||||
}
|
}
|
||||||
|
|
||||||
// destructor
|
// destructor
|
||||||
@ -368,48 +372,86 @@ public:
|
|||||||
// get timestep
|
// get timestep
|
||||||
double get_dt()
|
double get_dt()
|
||||||
{
|
{
|
||||||
|
assert ( segments_.size() > 0 );
|
||||||
|
|
||||||
return std::stod(segments_["sampl marker"][2]);
|
return std::stod(segments_["sampl marker"][2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// get time unit
|
// get time unit
|
||||||
std::string get_temp_unit()
|
std::string get_temp_unit()
|
||||||
{
|
{
|
||||||
|
assert ( segments_.size() > 0 );
|
||||||
|
|
||||||
return segments_["sampl marker"][5];
|
return segments_["sampl marker"][5];
|
||||||
}
|
}
|
||||||
|
|
||||||
// get name of measured entity
|
// get name of measured entity
|
||||||
std::string get_name()
|
std::string get_name()
|
||||||
{
|
{
|
||||||
|
assert ( segments_.size() > 0 );
|
||||||
|
|
||||||
return segments_["ename marker"][6];
|
return segments_["ename marker"][6];
|
||||||
}
|
}
|
||||||
|
|
||||||
// get unit of measured entity
|
// get unit of measured entity
|
||||||
std::string get_unit()
|
std::string get_unit()
|
||||||
{
|
{
|
||||||
|
assert ( segments_.size() > 0 );
|
||||||
|
|
||||||
return segments_["punit marker"][7];
|
return segments_["punit marker"][7];
|
||||||
}
|
}
|
||||||
|
|
||||||
// get time offset
|
// get time offset
|
||||||
double get_time_offset()
|
double get_time_offset()
|
||||||
{
|
{
|
||||||
|
assert ( segments_.size() > 0 );
|
||||||
|
|
||||||
return std::stod(segments_["minma marker"][11]);
|
return std::stod(segments_["minma marker"][11]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get time array
|
||||||
|
std::vector<double> get_time()
|
||||||
|
{
|
||||||
|
assert ( datmes_.size() > 0 );
|
||||||
|
|
||||||
|
// declare array of time
|
||||||
|
std::vector<double> timearr;
|
||||||
|
|
||||||
|
// get time step and offset
|
||||||
|
double dt = get_dt();
|
||||||
|
double timoff = get_time_offset();
|
||||||
|
|
||||||
|
// fill array
|
||||||
|
for ( unsigned long int t = 0; t < datmes_.size(); t++ )
|
||||||
|
{
|
||||||
|
timearr.push_back(timoff + t*dt);
|
||||||
|
}
|
||||||
|
|
||||||
|
return timearr;
|
||||||
|
}
|
||||||
|
|
||||||
// get data array encoded as floats/doubles
|
// get data array encoded as floats/doubles
|
||||||
std::vector<double>& get_data()
|
std::vector<double>& get_data()
|
||||||
{
|
{
|
||||||
|
assert ( datmes_.size() > 0 );
|
||||||
|
|
||||||
return datmes_;
|
return datmes_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get segment's array of elements
|
// get segment's array of elements
|
||||||
std::vector<std::string> get_segment(std::string marker)
|
std::vector<std::string> get_segment(std::string marker)
|
||||||
{
|
{
|
||||||
|
assert ( segments_.size() > 0 );
|
||||||
|
|
||||||
return segments_[marker];
|
return segments_[marker];
|
||||||
}
|
}
|
||||||
|
|
||||||
// write data to csv-like file
|
// write data to csv-like file
|
||||||
void write_data(std::string filename, int precision = 9, int width = 25)
|
void write_data(std::string filename, int precision = 9, int width = 25)
|
||||||
{
|
{
|
||||||
|
assert ( segments_.size() > 0 );
|
||||||
|
assert ( datmes_.size() > 0 );
|
||||||
|
|
||||||
// open file
|
// open file
|
||||||
std::ofstream fout(filename.c_str());
|
std::ofstream fout(filename.c_str());
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user