visualdynamics.io.delimited¶
delimited
¶
Time histories from delimited text: CSV, TSV and the column exports of controllers, recorders and spreadsheets.
history = visualdynamics.import_file('run.csv')
history = visualdynamics.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); when every time lies within that rounding of an even step,
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.
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 labels the
record (its block) and leaves its DOF empty, to be 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. |
Methods:¶
describe
¶
The layout in one line, for a dialog's header.
Source code in src/visualdynamics/io/delimited.py
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. |
Source code in src/visualdynamics/io/delimited.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 | |
sniff
¶
Whether a file is delimited numeric text: a SUFFIXES name whose
last lines split evenly into numbers.
Source code in src/visualdynamics/io/delimited.py
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. |
Source code in src/visualdynamics/io/delimited.py
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 | |
load
¶
handles
¶
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
|
|