hertzforge.io.delimited¶
delimited
¶
Time histories from delimited text: CSV, TSV and the column exports of controllers, recorders and spreadsheets.
history = hertzforge.import_file('run.csv')
history = hertzforge.import_file('run.txt', sample_rate=5120,
units='g')
The commonest way a recorded test leaves the machine that recorded it is
a text file with one column per channel. The file is laid out the way
its program chose, so the reader finds the layout first
(text_layout) and anything it finds can be stated instead:
- The delimiter: tab, semicolon, comma, pipe or a run of spaces,
whichever splits the data rows into the same number of numeric fields.
A semicolon file whose numbers carry a decimal comma (
0,25) is read with the comma as the decimal point. - The header: the line of column names directly above the data, and
a line of units under it when every field of that line reads as a
unit. A unit can also ride in the name, as
Accel (g)orAccel [g]. Lines above the header (a preamble) are read only for a sample rate:Sample Rate: 5120,fs = 5120 Hzor a step such asdt, 1.953125e-4. - The time column: a column named for time (
Time,t,Seconds,X_Value, with or without a unit). In a file with no header, a first column that rises in an even step is taken as time, and the import says so. - The sample rate: from the time column's step, the preamble, or
sample_rate. When two of them are given and disagree the import is refused: one of them is wrong, and the reader cannot tell which.
A time column without a unit is read as seconds, and the import says
so with the sample rate it gives. A time column written to a fixed
number of decimal places steps unevenly by up to a unit of its last
digit (1/5120 s written to six places alternates between 0.000195 and
0.000196), each time being half a unit from its true value; when an even
step lies within that rounding of every time, the even step is taken and
the import says so. A time column that is uneven beyond its rounding, or
out of order, is refused with the reason TimeHistory's own spectra
would give, and so is one whose steps are all alike but for one or two
whole multiples of them, which reads as dropped rows.
Channels. Every column but the time column is a channel, or the
ones columns names. A channel's name is its header, or column 3
counted from 1 as a spreadsheet counts. A name that is a DOF
(101X+) becomes the record's response DOF; any other name
(Current) is the record's DOF as it stands, which tells the channel
from the others and matches no node, until a node and a direction are
set in the channel table. names renames the channels.
Units. A unit from the header is declared when it reads as a
quantity a channel measures, converting the values to SI; one that does
not (counts, Gs, which reads as gigaseconds) leaves the channel
undeclared, holding the file's numbers, and the import says why. A unit
stated in units that cannot be declared is refused. quantities
claims what each channel measures without giving its unit, which labels
it and narrows the units offered; a unit that contradicts a claimed
quantity is refused. Values are never scaled by a guess.
Missing samples and dead channels. An empty cell, NaN or
N/A is a missing sample, kept as NaN, and the import names each
channel that has one and where the first falls; with
missing='refuse' the first one refuses the file. A row shorter than
the header is padded as missing and noted, where a delimiter marks each
cell; split by spaces, a short row cannot be placed and is refused. A channel whose every
sample is one value is noted as dead (core.channel_health). Neither
drops the channel: what the file holds is imported, and the notes say
what is wrong with it.
Refusals say what was found and where: a cell that is not a number (by line, as an editor numbers it, and column name), a row longer than the header, a file with neither a time column nor a sample rate, two sample rates that disagree, a preamble rate in a unit that is not a frequency, a time column that is not even, two columns named for time with neither chosen, a column named that is not there or a name two columns share, and a stated unit that contradicts the header's.
Every note is an ImportNote warning, which the window shows after the
import and a script sees as a warning. save writes a time history
back out as a CSV with one column per record and its unit in the
header, which this reader takes back in.
Classes:
| Name | Description |
|---|---|
TextLayout |
How a delimited text file is laid out, found by |
Functions:
| Name | Description |
|---|---|
text_layout |
Find how a delimited text file is laid out. |
sniff |
Whether a file is delimited numeric text: a |
read_delimited |
Read a delimited text file as a |
load |
|
handles |
Whether |
save |
Write a time history as a CSV this module reads back. |
Classes¶
TextLayout
dataclass
¶
TextLayout(delimiter: str, decimal: str, names_line: int, units_line: int, data_line: int, columns: tuple[str, ...], units: tuple[str | None, ...], time_column: int | None, sample_rate: float | None = None, rate_line: int = 0, rate_problem: str = '', time_candidates: tuple[int, ...] = ())
How a delimited text file is laid out, found by text_layout.
Line numbers count from 1, as an editor numbers them; 0 means the file has no such line.
Attributes:
| Name | Type | Description |
|---|---|---|
delimiter |
str
|
What separates the fields: |
decimal |
str
|
The decimal point, |
names_line |
int
|
The line of column names. |
units_line |
int
|
The line of units under the names. |
data_line |
int
|
The first line of data. |
columns |
tuple of str
|
Every column's name, the time column included: the header's,
or |
units |
tuple of str or None
|
Each column's unit as the file wrote it, or None. |
time_column |
int or None
|
Which column (from 0) holds time, or None. |
sample_rate |
float or None
|
A sample rate the preamble states, per second. |
rate_line |
int
|
The line it was stated on. |
rate_problem |
str
|
Why a rate the preamble states cannot be used (its unit is not a frequency, or a step's not a time), or ''; the read refuses it unless a sample rate is stated. |
time_candidates |
tuple of int
|
Every column named for time. With more than one, |
Methods:
| Name | Description |
|---|---|
describe |
The layout in one line, for a dialog's header. |
Functions:¶
text_layout
¶
text_layout(path: str | PathLike, *, delimiter: str | None = None, decimal: str | None = None, names_line: int | None = None, units_line: int | None = None, data_line: int | None = None) -> TextLayout
Find how a delimited text file is laid out.
Reads the first HEAD_LINES lines. Any argument given is taken as
stated and the rest is found around it; the module docstring says
how each part is found. Line numbers count from 1; a names_line or
units_line of 0 says the file has none.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or PathLike
|
The file. |
required |
delimiter
|
str
|
|
None
|
decimal
|
str
|
|
None
|
names_line
|
int
|
The line of column names, of units, and the first line of data. |
None
|
units_line
|
int
|
The line of column names, of units, and the first line of data. |
None
|
data_line
|
int
|
The line of column names, of units, and the first line of data. |
None
|
Returns:
| Type | Description |
|---|---|
TextLayout
|
What was found and stated. |
Raises:
| Type | Description |
|---|---|
ValueError
|
When no delimiter splits the data rows evenly into numbers, a stated line is past the lines read, or a stated delimiter or decimal is not one this reader takes. |
sniff
¶
Whether a file is delimited numeric text: a SUFFIXES name whose
last lines split evenly into numbers.
read_delimited
¶
read_delimited(path: str | PathLike, *, delimiter: str | None = None, decimal: str | None = None, names_line: int | None = None, units_line: int | None = None, data_line: int | None = None, time_column: int | str | bool | None = None, time_unit: str | None = None, sample_rate: float | None = None, columns: Sequence[int | str] | None = None, names: Sequence[str] | None = None, units: str | Sequence[str | None] | Mapping | None = None, quantities: str | Sequence[str | None] | Mapping | None = None, missing: str = 'keep', max_rows: int | None = None) -> Any
Read a delimited text file as a TimeHistory.
history = read_delimited('run.csv')
history = read_delimited('run.txt', time_column=False,
sample_rate=5120, units='g',
names=['101Z+', '102Z+'])
The layout is found by text_layout and anything stated here is
taken instead; the module docstring says how each part is found, and
what is noted and refused. Notes are ImportNote warnings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or PathLike
|
The file. |
required |
delimiter
|
str | None
|
As for |
None
|
decimal
|
str | None
|
As for |
None
|
names_line
|
str | None
|
As for |
None
|
units_line
|
str | None
|
As for |
None
|
data_line
|
str | None
|
As for |
None
|
time_column
|
(int, str or bool)
|
The time column, by index (from 0) or name; False when the file has none, which needs a sample rate. Found when omitted. |
None
|
time_unit
|
str
|
The time column's unit, when its header does not give one. |
None
|
sample_rate
|
float
|
Samples per second. With a time column, it must agree with the column's step. |
None
|
columns
|
sequence of int or str
|
The channels to read, by index (from 0) or name, in the order given. Every column but the time column when omitted. |
None
|
names
|
sequence of str
|
A name for each channel read: a DOF such as '101X+' becomes the record's response DOF. |
None
|
units
|
(str, sequence or mapping)
|
The unit of every channel, one per channel, or {name or index: unit}; taken where the header gives none or one that cannot be read, and refused where it contradicts one that can. |
None
|
quantities
|
(str, sequence or mapping)
|
What each channel measures ('acceleration', 'force', 'displacement'), in the same forms. |
None
|
missing
|
('keep', 'refuse')
|
Whether a missing sample is kept as NaN, with a note, or refuses the file. |
'keep'
|
max_rows
|
int
|
Read only the first so many data rows (a preview). |
None
|
Returns:
| Type | Description |
|---|---|
TimeHistory
|
One record per channel. |
Raises:
| Type | Description |
|---|---|
ValueError
|
For anything the module docstring lists as refused, a stated unit or quantity that cannot be used, or a stated argument that names no column. |
load
¶
read_delimited, as the importer registry calls it.
save
¶
Write a time history as a CSV this module reads back.
One line naming what the file holds, then a header of time (s)
and one column per record named by its label with its unit in
brackets, then one row per sample, every value written to the
digits that read back as the same number. With unit_system each
record is written in that system's unit for its quantity (g
included, since a header can say g); without one, as stored, in SI.
A record with no units is written as its numbers, with no unit in
its header.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
history
|
TimeHistory
|
What to write. |
required |
path
|
str or PathLike
|
Where. |
required |
unit_system
|
UnitSystem
|
The units to write in. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
|