This repository was archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathtdms_file.py
225 lines (196 loc) · 7.98 KB
/
tdms_file.py
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
import numpy as np
import pandas as pd
import os
try:
from .file import File, WrongFormatError, BrokenFormatError, OptionalImportError
except:
File = dict
class WrongFormatError(Exception): pass
class BrokenFormatError(Exception): pass
class OptionalImportError(Exception): pass
class TDMSFile(File):
@staticmethod
def defaultExtensions():
return ['.tdms']
@staticmethod
def formatName():
return 'TDMS file'
def __init__(self, filename=None, **kwargs):
""" Class constructor. If a `filename` is given, the file is read. """
self.filename = filename
if filename:
self.read(**kwargs)
def read(self, filename=None, **kwargs):
""" Reads the file self.filename, or `filename` if provided """
# --- Standard tests and exceptions (generic code)
if filename:
self.filename = filename
if not self.filename:
raise Exception('No filename provided')
if not os.path.isfile(self.filename):
raise OSError(2,'File not found:',self.filename)
if os.stat(self.filename).st_size == 0:
raise EmptyFileError('File is empty:',self.filename)
try:
from nptdms import TdmsFile
except:
raise OptionalImportError('Install the library nptdms to read this file')
fh = TdmsFile(self.filename, read_metadata_only=False)
# --- OLD, using some kind of old version of tdms and probably specific to one file
# channels_address = list(fh.objects.keys())
# channels_address = [ s.replace("'",'') for s in channels_address]
# channel_keys= [ s.split('/')[1:] for s in channels_address if len(s.split('/'))==3]
# # --- Setting up list of signals and times
# signals=[]
# times=[]
# for i,ck in enumerate(channel_keys):
# channel = fh.object(ck[0],ck[1])
# signals.append(channel.data)
# times.append (channel.time_track())
# lenTimes = [len(time) for time in times]
# minTimes = [np.min(time) for time in times]
# maxTimes = [np.max(time) for time in times]
# if len(np.unique(lenTimes))>1:
# print(lenTimes)
# raise NotImplementedError('Different time length')
# # NOTE: could use fh.as_dataframe
# if len(np.unique(minTimes))>1:
# print(minTimes)
# raise NotImplementedError('Different time span')
# if len(np.unique(maxTimes))>1:
# print(maxTimes)
# raise NotImplementedError('Different time span')
# # --- Gathering into a data frame with time
# time =times[0]
# signals = [time]+signals
# M = np.column_stack(signals)
# colnames = ['Time_[s]'] + [ck[1] for ck in channel_keys]
# self['data'] = pd.DataFrame(data = M, columns=colnames)
# --- NEW
self['data'] = fh
#for group in fh.groups():
# for channel in group.channels():
# #channel = group['channel name']
# print('Group:',group.name , 'Chan:',channel.name)
# channel_data = channel[:]
# if len(channel_data)>0:
# print(' ', type(channel_data))
# #print(' ', len(channel_data))
# print(' ', channel_data)
# print(' ', channel_data[0])
# try:
# print(channel.time_track())
# except KeyError:
# print('>>> No time track')
def write(self, filename=None, df=None):
""""
Write to TDMS file.
NOTE: for now only using a conversion from dataframe...
"""
if filename is None:
filename = self.filename
if df is None:
df = self.toDataFrame(split=False)
writeTDMSFromDataFrame(filename, df)
def groups(self):
return self['data'].groups()
@property
def groupNames(self):
return [group.name for group in self['data'].groups()]
def __repr__(self):
s ='Class TDMS (key: data)\n'
s +=' - data: TdmsFile\n'
s +=' * groupNames: {}\n'.format(self.groupNames)
#for group in fh.groups():
# for channel in group.channels():
# print(group.name)
# print(channel.name)
return s
def toDataFrame(self, split=True):
""" Export to one (split=False) or several dataframes (split=True)
Splitting on the group
"""
def cleanColumns(df):
# Cleanup columns
colnames = df.columns
colnames=[c.replace('\'','') for c in colnames]
colnames=[c[1:] if c.startswith('/') else c for c in colnames]
# If there is only one group, we remove the group key
groupNames = self.groupNames
if len(groupNames)==1:
nChar = len(groupNames[0])
colnames=[c[nChar+1:] for c in colnames] # +1 for the "/"
df.columns = colnames
fh = self['data']
if split:
# --- One dataframe per group. We skip group that have empty data
dfs={}
for group in fh.groups():
try:
df = group.as_dataframe(time_index=True)
df.insert(0,'Time_[s]', df.index.values)
df.index=np.arange(0,len(df))
except KeyError:
df = group.as_dataframe(time_index=False)
if len(df)>0:
dfs[group.name] = df
if len(dfs)==1:
dfs=dfs[group.name]
return dfs
else:
# --- One dataframe with all data
try:
df = fh.as_dataframe(time_index=True)
cleanColumns(df)
df.insert(0,'Time_[s]', df.index.values)
df.index=np.arange(0,len(df))
except KeyError:
df = fh.as_dataframe(time_index=False)
return df
def writeTDMSFromDataFrame(filename, df, defaultGroupName='default'):
"""
Write a TDMS file from a pandas dataframe
Example:
# --- Create a TDMS file - One group two channels with time track
time = np.linspace(0,1,20)
colA = np.random.normal(0,1,20)
colB = np.random.normal(0,1,20)
df = pd.DataFrame(data={'Time_[s]':time ,'ColA':colA,'ColB':colB})
writeTDMSFromDataFrame('out12.tdms', df, defaultGroupName = 'myGroup')
#--- Create a TDMS file - Two groups, two channels without time track but with timestamp
TS = np.arange('2010-02', '2010-02-21', dtype='datetime64[D]')
df = pd.DataFrame(data={'GroupA/ColTime':time,'GroupA/ColA':colA,'GroupB/ColTimestamp': TS,'GroupB/ColA':colB)})
writeTDMSFromDataFrame('out22.tdms', df)
"""
from nptdms import TdmsWriter, ChannelObject
defaultGroupName = 'default'
columns =df.columns
# Check if first column is time
if columns[0].lower().find('time')==0:
t = df.iloc[:,0].values
n = len(t)
dt1 = (np.max(t)-np.min(t))/(n-1)
if n>1:
dt2 = t[1]-t[0]
timeProperties = {'wf_increment':dt1, 'wf_start_offset':t[0]}
columns = columns[1:] # We remove the time column
else:
timeProperties = {}
with TdmsWriter(filename) as tdms_writer:
channels=[]
for iCol, col in enumerate(columns):
sp = col.split('/')
if len(sp)==2:
groupName = sp[0]
channelName = sp[1]
else:
groupName = defaultGroupName
channelName = col
data_array = df[col].values
channels.append(ChannelObject(groupName, channelName, data_array, timeProperties))
tdms_writer.write_segment(channels)
if __name__ == '__main__':
pass
# f = TDMSFile('TDMS_.tdms')
# dfs = f.toDataFrame(split=True)
# print(f)