finam.modules.CsvReader#
- class finam.modules.CsvReader(path, time_column, outputs, date_format=None, separator=';')[source]#
Bases:
TimeComponent
Reads CSV time series with one row per time step, and emits values based on a time column.
+-----------+ | | [custom] --> | CsvReader | [custom] --> | | [......] --> +-----------+
Examples
import finam as fm reader = fm.modules.CsvReader( path="test.csv", time_column="T", outputs={ "A": "meter", "B": "", }, date_format=None, separator=",", )
- Parameters:
path (PathLike) – Path to the input file.
time_column – Time column name.
outputs (dict(str, str)) – Data column names and units. Names become output names. Use
""
for dimensionless columns andNone
to get units from connected components.date_format (optional) – Format specifier for date. See datetime.strftime for details. Default is ISO format.
separator (str) – Columns separator. Default “;”
- Attributes:
connector
The component’s
tools.ConnectHelper
.inputs
dict: The component’s inputs.
logger
Logger for this component.
logger_name
Logger name derived from base logger name and class name.
metadata
The component’s metadata.
name
Component name.
next_time
The component’s predicted simulation time of the next pulls.
outputs
dict: The component’s outputs.
status
The component’s current status.
time
The component’s current simulation time.
uses_base_logger_name
Whether this class has a
base_logger_name
attribute.
Methods
connect
(start_time)Connect exchange data and metadata with linked components.
create_connector
([pull_data, in_info_rules, ...])Initialize the component's
tools.ConnectHelper
.finalize
()Finalize and clean up the component.
Initialize the component.
try_connect
(start_time[, exchange_infos, ...])Exchange the info and data with linked components.
update
()Update the component by one time step.
validate
()Validate the correctness of the component's settings and coupling.
with_name
(name)Renames the component and returns self.
- property next_time#
The component’s predicted simulation time of the next pulls.
Can be
None
if the component has no inputs.
- _initialize()[source]#
Initialize the component.
After the method call, the component’s inputs and outputs must be available, and the component should have status INITIALIZED.
- _connect(start_time)[source]#
Push initial values to outputs.
After the method call, the component should have status CONNECTED.
- _validate()[source]#
Validate the correctness of the component’s settings and coupling.
After the method call, the component should have status VALIDATED.
- _update()[source]#
Update the component by one time step. Push new values to outputs.
After the method call, the component should have status UPDATED or FINISHED.
- _finalize()[source]#
Finalize and clean up the component.
After the method call, the component should have status FINALIZED.
- __getitem__(name)#
Get an input or output by name. Implements access through square brackets.
Allows for the use of
comp["Name"]
as shortcut forcomp.inputs["Name"]
andcomp.outputs["Name"]
.Requires that the name does not appear in inputs as well as outputs.
- connect(start_time)#
Connect exchange data and metadata with linked components.
The method can be called multiple times if there are failed pull attempts.
After each method call, the component should have
status
ComponentStatus.CONNECTED
if connecting was completed,ComponentStatus.CONNECTING
if some but not all required initial input(s) could be pulled, andComponentStatus.CONNECTING_IDLE
if nothing could be pulled.- Parameters:
start_time (
datetime
) – The composition’s starting time. Can be before the component’s actual time.
- property connector#
The component’s
tools.ConnectHelper
.See also
create_connector()
andtry_connect()
.
- create_connector(pull_data=None, in_info_rules=None, out_info_rules=None, cache=True)#
Initialize the component’s
tools.ConnectHelper
.See also
try_connect()
,connector
andConnectHelper
for details.- Parameters:
pull_data (arraylike) – Names of the inputs that are to be pulled.
in_info_rules (dict) – Info transfer rules for inputs. See the examples for details.
See also
tools.FromInput
,tools.FromOutput
andtools.FromValue
.out_info_rules (dict) – Info transfer rules for outputs. See the examples for details.
See also
tools.FromInput
,tools.FromOutput
andtools.FromValue
.cache (bool) – Whether data and
Info
objects passed viatry_connect()
are cached for later calls. DefaultTrue
.
Examples
The following examples show the usage of this method in
_initialize()
.Simple usage if no input data or any metadata from connected components is required:
self.inputs.add(name="In", time=self.time, grid=fm.NoGrid()) self.outputs.add(name="Out", time=self.time, grid=fm.NoGrid()) self.create_connector()
To pull specific inputs, use
pull_data
like this:self.inputs.add(name="In1", time=self.time, grid=fm.NoGrid()) self.inputs.add(name="In2", time=self.time, grid=fm.NoGrid()) self.create_connector(pull_data=["In1", "In2"])
With the
in_info_rules
andout_info_rules
, metadata can be transferred between coupling slots.Here, the metadata for an output is taken from an input:
self.inputs.add(name="In", time=self.time, grid=None, units=None) self.outputs.add(name="Out") self.create_connector( out_info_rules={ "Out": [ fm.tools.FromInput("In") ] } )
The
Info
object for outputOut
will be created and pushed automatically intry_connect()
as soon as the metadata forIn
becomes available.Here, the metadata of an output is composed from the metadata of two inputs and a user-defined value:
self.inputs.add(name="In1", time=self.time, grid=None, units=None) self.inputs.add(name="In2", time=self.time, grid=None, units=None) self.outputs.add(name="Out") self.create_connector( out_info_rules={ "Out": [ fm.tools.FromInput("In1", ["time", "grid"]), fm.tools.FromInput("In2", ["units"]), fm.tools.FromValue("source", "FINAM"), ] } )
The
Info
object for outputOut
would be automatically composed intry_connect()
as soon as the infos of both inputs become available.time
andgrid
would be taken fromIn1
,units
fromIn2
, andsource
would be set to"finam"
.Rules are evaluated in the given order. Later rules can overwrite attributes set by earlier rules.
- finalize()#
Finalize and clean up the component.
After the method call, the component should have
status
ComponentStatus.FINALIZED
.
- initialize()#
Initialize the component.
After the method call, the component’s inputs and outputs must be available, and the component should have
status
ComponentStatus.INITIALIZED
.
- property logger#
Logger for this component.
- property logger_name#
Logger name derived from base logger name and class name.
- property metadata#
The component’s metadata. Will only be called after the connect phase from
Composition.metadata
.Components can overwrite this property to add their own specific metadata:
import finam as fm class MyComponent(fm.Component): @property def metadata(self): # Get the default metadata md = super().metadata # Add your own metadata md["my_field"] = "some value" # Return the dictionary return md
- Returns:
- A
dict
with the following default metadata: name
- the component’s nameclass
- the component’s classinputs
-dict
of metadata for all inputsoutputs
-dict
of metadata for all outputs
- A
- Return type:
- property name#
Component name.
- property status#
The component’s current status.
- property time#
The component’s current simulation time.
- try_connect(start_time, exchange_infos=None, push_infos=None, push_data=None)#
Exchange the info and data with linked components.
Values passed by the arguments are cached internally for later calls to the method if the connector was created with
cache=True
(the default). Thus, it is sufficient to provide only data and infos that became newly available. Giving the same data or infos repeatedly overwrites the cache.Sets the component’s
status
according to success of exchange.See also
create_connector()
,connector
andConnectHelper
for details.- Parameters:
start_time (
datetime
) – the composition’s starting time as passed toComponent.try_connect()
exchange_infos (dict of [str, Info]) – currently or newly available input data infos by input name
push_infos (dict of [str, Info]) – currently or newly available output data infos by output name
push_data (dict of [str, array-like]) – currently or newly available output data by output name
- update()#
Update the component by one time step. Push new values to outputs.
After the method call, the component should have
status
ComponentStatus.UPDATED
orComponentStatus.FINISHED
.
- property uses_base_logger_name#
Whether this class has a
base_logger_name
attribute. True.
- validate()#
Validate the correctness of the component’s settings and coupling.
After the method call, the component should have
status
ComponentStatus.VALIDATED
.
- with_name(name)#
Renames the component and returns self.