-
Notifications
You must be signed in to change notification settings - Fork 13
/
lvm_read.py
211 lines (182 loc) · 6.87 KB
/
lvm_read.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
"""
This module is used for the reading LabView Measurement File
Author: Janko Slavič et al. ([email protected])
"""
from os import path
import pickle
import numpy as np
import io
__version__ = '1.23'
def _lvm_pickle(filename):
""" Reads pickle file (for local use)
:param filename: filename of lvm file
:return lvm_data: dict with lvm data
"""
p_file = '{}.pkl'.format(filename)
pickle_file_exist = path.exists(p_file)
original_file_exist = path.exists(filename)
if pickle_file_exist and original_file_exist:
read_pickle = path.getctime(p_file) > path.getctime(filename)
if not original_file_exist:
read_pickle = True
lvm_data = False
if pickle_file_exist and read_pickle:
f = open(p_file, 'rb')
lvm_data = pickle.load(f)
f.close()
return lvm_data
def _lvm_dump(lvm_data, filename, protocol=-1):
""" Dump lvm_data dict to disc
:param lvm_data: lvm data dict
:param filename: filename of the lvm file
:param protocol: pickle protocol
"""
p_file = '{}.pkl'.format(filename)
output = open(p_file, 'wb')
pickle.dump(lvm_data, output, protocol=protocol)
output.close()
def _get_separator(file):
separator = '\t'
i = 0
for line in file:
if line.startswith('Separator'):
separator = line.strip()[9]
break
if i>20:
break
i+=1
if isinstance(file, io.IOBase):
file.seek(0)
return separator
def _read_lvm_base(filename):
""" Base lvm reader. Should be called from ``read``, only
:param filename: filename of the lvm file
:return lvm_data: lvm dict
"""
with open(filename, 'r', encoding="utf8", errors='ignore') as f:
separator = _get_separator(f)
lvm_data = read_lines(f, separator=separator)
return lvm_data
def read_lines(lines, separator='\t'):
""" Read lines of strings.
:param lines: lines of the lvm file
:return lvm_data: lvm dict
"""
lvm_data = dict()
lvm_data['Decimal_Separator'] = '.'
data_channels_comment_reading = False
data_reading = False
segment = None
seg_data = []
first_column = 0
nr_of_columns = 0
segment_nr = 0
def to_float(a):
try:
return float(a.replace(lvm_data['Decimal_Separator'], '.'))
except:
return np.nan
for line in lines:
line = line.replace('\r', '')
line_sp = line.replace('\n', '').split(separator)
if line_sp[0] in ['***End_of_Header***', 'LabVIEW Measurement']:
continue
elif line in ['\n', separator+'\n']:
# segment finished, new segment follows
segment = dict()
lvm_data[segment_nr] = segment
data_reading = False
segment_nr += 1
continue
elif data_reading: # this was moved up, to speed up the reading
seg_data.append([to_float(a) for a in
line_sp[first_column:(nr_of_columns + 1)]])
elif segment == None:
if len(line_sp) == 2:
key, value = line_sp
lvm_data[key] = value
elif segment != None:
if line_sp[0] == 'Channels':
key, value = line_sp[:2]
nr_of_columns = len(line_sp) - 1
segment[key] = eval(value)
if nr_of_columns < segment['Channels']:
nr_of_columns = segment['Channels']
data_channels_comment_reading = True
elif line_sp[0] == 'X_Value':
seg_data = []
segment['data'] = seg_data
if lvm_data['X_Columns'] == 'No':
first_column = 1
segment['Channel names'] = line_sp[first_column:(nr_of_columns + 1)]
data_channels_comment_reading = False
data_reading = True
elif data_channels_comment_reading:
key, values = line_sp[0], line_sp[1:(nr_of_columns + 1)]
if key in ['Delta_X', 'X0', 'Samples']:
segment[key] = [eval(val.replace(lvm_data['Decimal_Separator'], '.')) if val else np.nan for val in
values]
else:
segment[key] = values
elif len(line_sp) == 2:
key, value = line_sp
segment[key] = value
if not lvm_data[segment_nr - 1]:
del lvm_data[segment_nr - 1]
segment_nr -= 1
lvm_data['Segments'] = segment_nr
for s in range(segment_nr):
lvm_data[s]['data'] = np.asarray(lvm_data[s]['data'])
return lvm_data
def read_str(str):
"""
Parse the string as the content of lvm file.
:param str: input string
:return: dictionary with lvm data
Examples
--------
>>> import numpy as np
>>> import urllib
>>> filename = 'short.lvm' #download a sample file from github
>>> sample_file = urllib.request.urlopen('https://github.com/ladisk/lvm_read/blob/master/data/'+filename).read()
>>> str = sample_file.decode('utf-8') # convert to string
>>> lvm = lvm_read.read_str(str) #read the string as lvm file content
>>> lvm.keys() #explore the dictionary
dict_keys(['', 'Date', 'X_Columns', 'Time_Pref', 'Time', 'Writer_Version',...
"""
return read_lines(str.splitlines(keepends=True))
def read(filename, read_from_pickle=True, dump_file=True):
"""Read from .lvm file and by default for faster reading save to pickle.
See also specifications: http://www.ni.com/tutorial/4139/en/
:param filename: file which should be read
:param read_from_pickle: if True, it tries to read from pickle
:param dump_file: dump file to pickle (significantly increases performance)
:return: dictionary with lvm data
Examples
--------
>>> import numpy as np
>>> import urllib
>>> filename = 'short.lvm' #download a sample file from github
>>> sample_file = urllib.request.urlopen('https://github.com/ladisk/lvm_read/blob/master/data/'+filename).read()
>>> with open(filename, 'wb') as f: # save the file locally
f.write(sample_file)
>>> lvm = lvm_read.read('short.lvm') #read the file
>>> lvm.keys() #explore the dictionary
dict_keys(['', 'Date', 'X_Columns', 'Time_Pref', 'Time', 'Writer_Version',...
"""
lvm_data = _lvm_pickle(filename)
if read_from_pickle and lvm_data:
return lvm_data
else:
lvm_data = _read_lvm_base(filename)
if dump_file:
_lvm_dump(lvm_data, filename)
return lvm_data
if __name__ == '__main__':
import matplotlib.pyplot as plt
da = read('data/with_comments.lvm',read_from_pickle=False)
#da = read('data\with_empty_fields.lvm',read_from_pickle=False)
print(da.keys())
print('Number of segments:', da['Segments'])
plt.plot(da[0]['data'])
plt.show()