pyCBD.lib.io module¶
This module contains the library building blocks to handle input/output in the form of file interaction.
- class pyCBD.lib.io.Interpolation[source]¶
Bases:
objectHelper class to handle CSV interpolations.
- FIRST = 0¶
Use the first value to interpolate.
- LAST = 1¶
Use the last value to interpolate.
- LINEAR = 2¶
Do a linear interpolation between the two values.
- static interpolate(a, b, t, method=0)[source]¶
Interpolates between two elements. When
tcorresponds toa[0],a[1]will always be returned.- Parameters
a (tuple) – A pair
(time, value); representing the first element.b (tuple) – A pair
(time, value); representing the second element.t (float) – The time at which the interpolation happens.
method (int) – The interpolation method to use.
- Raises
AssertionError – When
a[0] <= t < b[0]is not satisfied.
- class pyCBD.lib.io.ReadCSV(block_name, file_name, hold=0, time_col='time', repeat=False, **kwargs)[source]¶
Bases:
pyCBD.Core.BaseBlockReads data from a CSV file and outputs it on the corresponding timestamps. The output ports are defined by the read CSV columns.
Warning
It is required to have a column that represents the time. Furthermore, the dataset must be sorted w.r.t. this column.
Important
When repeating, the last value of the CSV is used for time 0.0, hence it is prohibited to use time 0 in your CSV.
- Parameters
block_name (str) – The name of the block.
file_name (str) – The name of the CSV to read.
hold (Interpolation) – How the data should be interpreted if the time falls in-between the records of the (sampled) CSV.
time_col (str) – The name of the column identifying the time. Defaults to
"time".repeat (bool) – When
True, the CSV will repeat when at end. Additionally, when the time is before the first time in the file, an interpolation is done between the last read and first read value. WhenFalse, the first and last values will be outputted when lower/higher than the predefined range of times. Defaults toFalse.
- Keyword Arguments
columns (iter) – A
set,listor adict, identifying all columns that need to be read. Must correspond to the columns that can be found in the file itself. WhenNone(default), all columns from the first line of the file will be used. In the case of adict, it is expected the keys are the column names and the values refer to the data types of these columns. I.e.{ "time": float }reads thetimecolumn as floats. Forsetandlist, the data is read as strings.dtype – The default data type of the CSV. This does not override optional values set by the
columnsargument when it’s adict. Defaults tofloat.dialect (str) – The dialect for parsing the data. Defaults to
excel.nan (Any) – The value to insert when some fields are missing data. Defaults to
None.** – For other values, take a look at the CSV Dialect and Formatting Parameters.
- class pyCBD.lib.io.WriteCSV(block_name, filename, columns, time_col='time', **kwargs)[source]¶
Bases:
pyCBD.Core.BaseBlockOutputs data to a CSV file. Every time a message is received, a new record is added to the file. At the end of the execution, when this block is destroyed, the file pointer will be closed (and removed).
- Parameters
block_name (str) – The name of the block.
filename (str) – The name of the file to write to. All contents of this file will be cleared.
columns (list) – The (ordered) names of the columns in the file. These generally represent the input ports for this block.
time_col (str) – The name for the column to store the times. If this corresponds to a value listed in the
columnsargument, that column will be used to keep track of the time. There will never be an input port calledtime_col. If this value is not located in thecolumnslist, a new column is added to the front of the provided columns. Defaults to"time".
- Keyword Arguments
Take a look at the csv.DictWriter class for more info.
Tip
When the time-value is not needed in the CSV, just ignore it when reading it out again.