Skip to content

ENH: implemented arbitrary data separator, for now supported Tab and Comma. #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -6,8 +6,9 @@
from os import path
import pickle
import numpy as np
import io

__version__ = '1.21'
__version__ = '1.23'

def _lvm_pickle(filename):
""" Reads pickle file (for local use)
Expand Down Expand Up @@ -42,6 +43,20 @@ def _lvm_dump(lvm_data, filename, protocol=-1):
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
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()
Loading