Data Export¶
PyRolL includes a class capable of converting the simulation results to
a pandas DataFrame
and save this to different file formats. The feature can be accessed
by use of the CLI through the export
command.
The data included in the frame can be modified by hooks. The available file formats can be extended by the use of hooks.
To read about the basics of hooks and plugins, see here.
Specifying data to include¶
There is a hook columns(unit : Unit)
that can be used to specify the columns included in the data frame. One can use
the pyroll.utils.hookutils.applies_to_unit_types(types)
decorator to specify the unit types the hook implementation
should apply to (currently only Unit
, RollPass
, Transport
).
Each implementation must return a mapping of column names (string) to values (any type that can be data in
a DataFrame
). The list of hook results will be combined to the final set of columns. Later registered implementations
will override earlier ones.
Define new implementations of this hook to include more data in the export. Commonly you would return a dict
mapping
from str
to a numeric type or string.
Adding new file formats¶
For exporting to a file a hook is defined to handle the formatting:
export(data: pandas.DataFrame, export_format: str)
It takes the generated DataFrame
and a string specifying the format as arguments. Depending on the value
of export_format
an implementation can decide whether it is able to handle the format or not. If it can, it should
return the binary data that will be saved to file. If it can not, it should return None
. The first implementation not
returning None
will be used for the file
content (firstresult
).
Current basic implementations support CSV and XML formats by use of the methods provided by DataFrame
.
Class Documentation¶
- class Exporter¶
Class able to export simulation results to several data formats.
- export(units: List[Unit], export_format: str) bytes ¶
Call get_dataframe and export its results to a specified format.
- Parameters
units – list of units to take the data from
export_format – a string key identifying the export format, valid values depend on the loaded implementations of the ‘export’ hook
- Returns
the exported data as binary stream
- get_dataframe(units: List[Unit]) DataFrame ¶
Generate a pandas DataFrame by use of the unit_columns, roll_pass_columns and transport_columns hooks.
- Parameters
units – list of units to take the data from
- Returns
a pandas data frame filled with the exported data
Hooks¶
- columns(unit) Mapping[str, Any] ¶
Take a unit object and extract some data to be listed in the CSV output. Return a mapping of column names to values. All hookimpls will be joined in order of definition.
- export(data: DataFrame, export_format: str) bytes ¶
Export the data to a specified format. Return binary data that can be saved to a file. First hookimpl that does not return None is taken. Return None to signal, that the impl does not support the export_format