Reads the text .lvm files written by National Instruments LabVIEW into
plain Python dictionaries and NumPy arrays. Tab- and comma-separated files,
the comma decimal separator, several segments per file, missing fields and
comments are all handled.
pip install lvm_read
Requires Python 3.10 or newer and NumPy.
import lvm_read
lvm = lvm_read.read('short.lvm')
lvm['Segments'] # 1
lvm[0]['Channel names'] # ['Excitation (Trigger)', 'Response (Trigger)', 'Comment']
lvm[0]['data'] # array of shape (10, 2), one column per channel
lvm[0]['data'][0, 0] # 0.914018The time axis is not stored column by column unless the file says so; it is
reconstructed from X0 and Delta_X:
import numpy as np
segment = lvm[0]
time = segment['X0'][0] + np.arange(len(segment['data'])) * segment['Delta_X'][0]A dictionary with the header fields of the file (Date, Operator,
Separator, Decimal_Separator, X_Columns, ...), the number of
segments under Segments, and one entry per segment under the integer
keys 0, 1, ...:
data |
2-D NumPy array of floats, one row per sample |
Channel names |
names from the X_Value line |
Channels |
number of channels |
Samples |
samples per channel |
X0, Delta_X |
start and step of the X axis, per channel |
comments |
text of the comment of each row, '' where there is
none; present only if the last channel is Comment |
Anything that is not a number - a comment, an empty field - becomes nan
in data, so the array stays numeric. Rows of unequal length, which occur
when only some rows carry a comment, are padded with nan.
lvm = lvm_read.read_str(open('short.lvm', encoding='utf8').read())Both read and read_str take the column separator from the
Separator field of the header. Pass separator explicitly for a file
whose header does not have it. A file no segment can be read from raises
ValueError.
read writes a <filename>.pkl next to the file and reads it back on the
next call, which is much faster than parsing the text again. Turn it off with
read_from_pickle=False and dump_file=False. Note that the cache is a
pickle: do not point read at .lvm files from an untrusted source while
it is enabled.
See the Showcase lvm_read.ipynb notebook for a walk through a few sample files, and the file format specification for what the fields mean.