clean up repo, include print methods in cython, update README python docu
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
# distutils: language = c++
|
||||
|
||||
from tdm_reaper cimport tdm_reaper
|
||||
import json as jsn
|
||||
import json as jn
|
||||
import numpy as np
|
||||
# import re
|
||||
# import os
|
||||
|
||||
cdef class tdmreaper:
|
||||
|
||||
@@ -29,40 +27,18 @@ cdef class tdmreaper:
|
||||
def get_channel(self, string id):
|
||||
return self.cpp_tdm.get_channel_as_double(id)
|
||||
|
||||
# get meta-data of channel(-group)
|
||||
# get meta-data of channel(-group) (as dictionary)
|
||||
def get_channelgroup_info(self, string id):
|
||||
return self.cpp_tdm.get_channelgroup_info(id)
|
||||
grpstr = self.cpp_tdm.get_channelgroup_info(id)
|
||||
return jn.loads(grpstr.decode())
|
||||
def get_channel_info(self, string id):
|
||||
return self.cpp_tdm.get_channel_info(id)
|
||||
chnstr = self.cpp_tdm.get_channel_info(id)
|
||||
return jn.loads(chnstr.decode())
|
||||
|
||||
# def set_file(self, string rawfile):
|
||||
# if not os.path.isfile(rawfile) :
|
||||
# raise ValueError("'" + str(rawfile) + "' does not exist")
|
||||
# self.rawit.set_file(rawfile)
|
||||
#
|
||||
# def do_conversion(self):
|
||||
# self.rawit.setup_and_conversion()
|
||||
#
|
||||
# def validity(self):
|
||||
# return self.rawit.get_valid()
|
||||
#
|
||||
# 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 write_table(self, const char* csvfile, char delimiter):
|
||||
# self.rawit.write_table(csvfile,delimiter)
|
||||
# print a channel(-group)
|
||||
def print_channelgroup(self, string id, const char* filename,
|
||||
bool include_meta, char delimiter):
|
||||
self.cpp_tdm.print_group(id,filename,include_meta,delimiter)
|
||||
def print_channel(self, string id, const char* filename,
|
||||
bool include_meta):
|
||||
self.cpp_tdm.print_channel(id,filename,include_meta)
|
||||
|
@@ -1,142 +0,0 @@
|
||||
|
||||
from tdm_ripper cimport tdm_ripper
|
||||
import numpy as np
|
||||
import re
|
||||
|
||||
cdef class pytdmripper:
|
||||
|
||||
# pointer to C++ instance (since there's no nullary constructor)
|
||||
cdef tdm_ripper *cripp
|
||||
|
||||
def __cinit__(self, string tdmfile, string tdxfile = b""):
|
||||
self.cripp = new tdm_ripper(tdmfile,tdxfile)
|
||||
|
||||
def __dealloc__(self):
|
||||
del self.cripp
|
||||
|
||||
def show_channels(self):
|
||||
self.cripp.list_channels()
|
||||
|
||||
def show_groups(self):
|
||||
self.cripp.list_groups()
|
||||
|
||||
def num_channels(self):
|
||||
return self.cripp.num_channels()
|
||||
|
||||
def no_channels(self, int groupid):
|
||||
assert (groupid >= 0 and groupid < self.cripp.num_groups()), "index of group must be in [0,n-1]"
|
||||
return self.cripp.no_channels(groupid)
|
||||
|
||||
def num_groups(self):
|
||||
return self.cripp.num_groups()
|
||||
|
||||
def no_channel_groups(self):
|
||||
return self.cripp.num_groups()
|
||||
|
||||
def channel_name(self,int groupid,int channelid):
|
||||
return self.cripp.channel_name(groupid,channelid).decode('utf-8')
|
||||
|
||||
def group_name(self,int groupid):
|
||||
return self.cripp.group_name(groupid).decode('utf-8')
|
||||
|
||||
def channel_unit(self,int groupid,int channelid):
|
||||
return (self.cripp.channel_unit(groupid,channelid))
|
||||
|
||||
def channel_exists(self,int groupid, string channelname):
|
||||
return self.cripp.channel_exists(groupid,channelname)
|
||||
|
||||
def obtain_channel_id(self,int groupid, int channelid):
|
||||
return self.cripp.obtain_channel_id(groupid,channelid)
|
||||
|
||||
def get_channel(self, int channelid):
|
||||
return np.asarray(self.cripp.get_channel(channelid))
|
||||
|
||||
def channel(self,int groupid,int channelid):
|
||||
return self.cripp.channel(groupid,channelid)
|
||||
|
||||
def channel_length(self,int groupid, int channelid):
|
||||
return self.cripp.channel_length(groupid,channelid)
|
||||
|
||||
def time_stamp(self,int groupid, bool startstop):
|
||||
return self.cripp.time_stamp(groupid,startstop)
|
||||
|
||||
def get_min(self,int groupid, int channelid):
|
||||
return self.cripp.get_min(groupid,channelid)
|
||||
|
||||
def get_max(self,int groupid, int channelid):
|
||||
return self.cripp.get_max(groupid,channelid)
|
||||
|
||||
def print_channel(self, int channelid, const char* filename):
|
||||
self.cripp.print_channel(channelid,filename)
|
||||
|
||||
def meta_info(self, string attribute_name):
|
||||
return self.cripp.get_meta(attribute_name)
|
||||
|
||||
def print_meta(self, const char* filename):
|
||||
self.cripp.print_meta(filename)
|
||||
|
||||
def close(self):
|
||||
dummy = ""
|
||||
|
||||
#=============================================================================#
|
||||
|
||||
def extract_all(string tdmfile, string tdxfile, string outdirx = b"./", string prfxnam = b""):
|
||||
"""
|
||||
Python function extracting all available data from .tdm and .tdx to .csv dump
|
||||
|
||||
Args:
|
||||
tdmfile: path and filename of .tdm file
|
||||
tdxfile: path and filename of associated .tdx file
|
||||
outdirx: directory where all .csv output is dumped
|
||||
prfxnam: optionally specify a name prefix for all .csv files
|
||||
|
||||
Return:
|
||||
None
|
||||
"""
|
||||
|
||||
# TODO preliminary: assume utf-8
|
||||
encoding = 'utf-8'
|
||||
|
||||
# set up instance of Cython ripper
|
||||
td = pytdmripper(tdmfile,tdxfile)
|
||||
|
||||
# if no name prefix is given, .tdm filename will be used
|
||||
prfx = ""
|
||||
if prfxnam.decode(encoding) == prfx :
|
||||
prfx = tdmfile.decode(encoding).rstrip('.tdm').split('/')[-1]
|
||||
else:
|
||||
prfx = prfxnam.decode(encoding)
|
||||
|
||||
# obtain number of available groups and channels
|
||||
numgr = td.num_groups()
|
||||
numch = td.num_channels()
|
||||
|
||||
# generate list of all files produced
|
||||
filelist = []
|
||||
|
||||
# dump all meta information
|
||||
metafile = outdirx.decode(encoding)+prfx+'.csv'
|
||||
td.print_meta(metafile.encode(encoding))
|
||||
filelist.append(metafile.lstrip("./"))
|
||||
|
||||
# dump all available groups and channels
|
||||
for g in range(0,numgr):
|
||||
numgrch = td.no_channels(g)
|
||||
for c in range(0,numgrch):
|
||||
# obtained overall channel id
|
||||
chid = td.obtain_channel_id(g,c)
|
||||
# get group's and channel's name
|
||||
gname = td.group_name(g)
|
||||
cname = td.channel_name(g,c)
|
||||
# use regular expression replacement to sanitize group and channel names
|
||||
gname = re.sub('[!@#$%^&*()-+= ,]','',gname)
|
||||
cname = re.sub('[!@#$%^&*()-+= ,]','',cname)
|
||||
# generate filename
|
||||
fichan = prfx + '_' + str(g+1) + '_' + str(c+1) + '_' + gname + '_' + cname + '.csv'
|
||||
# print channel
|
||||
td.print_channel(chid,(outdirx.decode(encoding)+fichan).encode(encoding))
|
||||
# append filename to list
|
||||
filelist.append(fichan)
|
||||
|
||||
# return list of all files
|
||||
return filelist
|
@@ -23,20 +23,6 @@ cdef extern from "tdm_reaper.hpp":
|
||||
# get meta-data
|
||||
string get_channelgroup_info(string id) except+
|
||||
string get_channel_info(string id) except+
|
||||
|
||||
# void set_file(string)
|
||||
# # perform conversion (pass any C++ exceptions to Python)
|
||||
# void setup_and_conversion() except +
|
||||
# # get validity of data format
|
||||
# bool get_valid()
|
||||
# # 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_table(const char*,char delimiter)
|
||||
# print a channel(-group)
|
||||
void print_group(string id, const char* filename, bool include_meta, char delimiter)
|
||||
void print_channel(string id, const char* filename, bool include_meta)
|
||||
|
@@ -1,36 +0,0 @@
|
||||
# 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 "tdm_ripper.cpp":
|
||||
pass
|
||||
|
||||
cdef extern from "tdm_ripper.hpp":
|
||||
cdef cppclass tdm_ripper:
|
||||
tdm_ripper(string,string) except +
|
||||
void list_channels()
|
||||
void list_groups()
|
||||
int num_channels()
|
||||
int no_channels(int)
|
||||
int num_groups()
|
||||
int no_channel_groups()
|
||||
string channel_name(int,int)
|
||||
string group_name(int)
|
||||
string channel_unit(int,int)
|
||||
int channel_exists(int,string)
|
||||
int obtain_channel_id(int,int)
|
||||
vector[double] get_channel(int)
|
||||
int channel_length(int,int)
|
||||
string time_stamp(int,bool)
|
||||
double get_min(int,int)
|
||||
double get_max(int,int)
|
||||
vector[double] channel(int,int)
|
||||
void print_channel(int,const char*)
|
||||
string get_meta(string attribute_name)
|
||||
void print_meta(const char*)
|
||||
# dummy method for compatibility
|
||||
void close()
|
Reference in New Issue
Block a user