Skip to content

Commit

Permalink
implemented arbitrary data separator, for now supported Tab and Comma.
Browse files Browse the repository at this point in the history
  • Loading branch information
jankoslavic committed May 13, 2024
1 parent a20623c commit 20cab1c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
27 changes: 27 additions & 0 deletions data/comma_separator.lvm
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
LabVIEW Measurement,
Writer_Version,2
Reader_Version,2
Separator,Comma
Decimal_Separator,.
Multi_Headings,No
X_Columns,One
Time_Pref,Absolute
Operator,photron
Date,2024/04/10
Time,13:06:46.2639230999998221435
***End_of_Header***,
,
Channels,4,,,,
Samples,4,4,4,4,
Date,2024/04/10,2024/04/10,2024/04/10,2024/04/10,
Time,13:06:46.2639230999998221435,13:06:46.2639230999998221435,13:06:46.2639230999998221435,13:06:46.2639230999998221435,
Y_Unit_Label,Volts,Volts,Volts,Volts,
X_Dimension,Time,Time,Time,Time,
X0,0.0000000000000000E+0,0.0000000000000000E+0,0.0000000000000000E+0,0.0000000000000000E+0,
Delta_X,2.000000E-6,2.000000E-6,2.000000E-6,2.000000E-6,
***End_of_Header***,,,,,
X_Value,p_st1,p_st2,p_plate,TC1,Comment
0.000000,-0.000424,-0.002187,-0.002505,0.163976
2.000000E-6,-0.000424,-0.002827,-0.002185,0.163337
4.000000E-6,-0.000744,-0.003147,-0.003144,0.163657
6.000000E-6,-0.000424,-0.002827,-0.002505,0.163657
26 changes: 21 additions & 5 deletions lvm_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pickle
import numpy as np

__version__ = '1.21'
__version__ = '1.22'

def _lvm_pickle(filename):
""" Reads pickle file (for local use)
Expand Down Expand Up @@ -42,6 +42,21 @@ def _lvm_dump(lvm_data, filename, protocol=-1):
pickle.dump(lvm_data, output, protocol=protocol)
output.close()

def get_separator(file):
separators = {'Tab': '\t',
'Comma': ','
}
for line in file:
if line.startswith('Separator'):
separator = line.strip()[10:]
break
file.seek(0)
if separator in separators.keys():
return separators[separator]
else:
raise Warning('No separator defined, using tabulator!')
return '\t'


def _read_lvm_base(filename):
""" Base lvm reader. Should be called from ``read``, only
Expand All @@ -50,11 +65,12 @@ def _read_lvm_base(filename):
:return lvm_data: lvm dict
"""
with open(filename, 'r', encoding="utf8", errors='ignore') as f:
lvm_data = read_lines(f)
separator = get_separator(f)
lvm_data = read_lines(f, separator=separator)
return lvm_data


def read_lines(lines):
def read_lines(lines, separator='\t'):
""" Read lines of strings.
:param lines: lines of the lvm file
Expand All @@ -76,10 +92,10 @@ def to_float(a):
return np.nan
for line in lines:
line = line.replace('\r', '')
line_sp = line.replace('\n', '').split('\t')
line_sp = line.replace('\n', '').split(separator)
if line_sp[0] in ['***End_of_Header***', 'LabVIEW Measurement']:
continue
elif line in ['\n', '\t\n']:
elif line in ['\n', separator+'\n']:
# segment finished, new segment follows
segment = dict()
lvm_data[segment_nr] = segment
Expand Down
8 changes: 6 additions & 2 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from lvm_read import read

def test_short_lvm():
def test_short_lvm():
data = read('./data/pickle_only.lvm')
np.testing.assert_equal(data[0]['data'][0,0],0.914018)

Expand Down Expand Up @@ -48,9 +48,13 @@ def timing_on_long_short_lvm():
toc = time.time()
print(f'Average time: {(toc-tic)/N:3.1f}s')

def test_comma_separator():
data = read('./data/comma_separator.lvm', read_from_pickle=False, dump_file=False)
np.testing.assert_equal(data[0]['data'][0,1],-0.000424)

if __name__ == '__mains__':
np.testing.run_module_suite()

if __name__ == '__main__':
test_several_comments()
test_comma_separator()
#timing_on_long_short_lvm()

0 comments on commit 20cab1c

Please sign in to comment.