fastoad.io.variable_io module

class fastoad.io.variable_io.VariableIO(data_source: str | PathLike | IO | None, formatter: IVariableIOFormatter = None)[source]

Bases: object

Class for reading and writing variable values from/to file.

The file format is defined by the class provided as formatter argument.

Parameters:
  • data_source – I/O stream, or file path, used for reading or writing data

  • formatter – a class that determines the file format to be used. Defaults to a VariableBasicXmlFormatter instance.

data_source

I/O stream, or file path, used for reading or writing data

property formatter: IVariableIOFormatter

Class that determines the file format to be used.

read(only: List[str] = None, ignore: List[str] = None) VariableList | None[source]

Reads variables from provided data source.

Elements of only and ignore can be real variable names or Unix-shell-style patterns. In any case, comparison is case-sensitive.

Parameters:
  • only – List of variable names that should be read. Other names will be ignored. If None, all variables will be read.

  • ignore – List of variable names that should be ignored when reading.

Returns:

a VariableList instance where outputs have been defined using provided source.

write(variables: VariableList, only: List[str] = None, ignore: List[str] = None)[source]

Writes variables from provided VariableList instance.

Elements of only and ignore can be real variable names or Unix-shell-style patterns. In any case, comparison is case-sensitive.

Parameters:
  • variables – a VariableList instance

  • only – List of variable names that should be written. Other names will be ignored. If None, all variables will be written.

  • ignore – List of variable names that should be ignored when writing

class fastoad.io.variable_io.DataFile(data_source: str | PathLike | IO | list = None, formatter: IVariableIOFormatter = None, load_data: bool = True)[source]

Bases: VariableList

Class for managing FAST-OAD data files.

Behaves like VariableList class but has load() and save() methods.

If variable list is specified for data_source, file_path will have to be set before using :method:`save`.

Parameters:
  • data_source – Can be the file path where data will be loaded and saved, or a list of Variable instances that will be used for initialization (or a VariableList instance).

  • formatter – (ignored if data_source is not an I/O stream nor a file path) a class that determines the file format to be used. Defaults to FAST-OAD native format. See VariableIO for more information.

  • load_data – (ignored if data_source is not an I/O stream nor a file path) if True, file is expected to exist and its content will be loaded at instantiation.

add_var(name, **kwargs)

Adds, or replace, the named variable with given attributes

Parameters:
  • name

  • kwargs

append(var: Variable) None

Appends var to the end of the list, unless its name is already used. In that case, var will replace the previous Variable instance with the same name.

clear()

Remove all items from list.

copy()

Return a shallow copy of the list.

count(value, /)

Return number of occurrences of value.

extend(iterable, /)

Extend list by appending elements from the iterable.

property file_path: str

Path of data file.

classmethod from_dataframe(df: DataFrame) VariableList

Creates a VariableList instance from a pandas DataFrame instance.

The DataFrame instance is expected to have column names “name” + some keys among the ones given by Variable.get_openmdao_keys().

Parameters:

df – a DataFrame instance

Returns:

a VariableList instance

classmethod from_dict(var_dict: Mapping[str, dict] | Iterable[Tuple[str, dict]]) VariableList

Creates a VariableList instance from a dict-like object.

Parameters:

var_dict

Returns:

a VariableList instance

classmethod from_ivc(ivc: IndepVarComp) VariableList

Creates a VariableList instance from an OpenMDAO IndepVarComp instance

Parameters:

ivc – an IndepVarComp instance

Returns:

a VariableList instance

classmethod from_problem(problem: Problem, use_initial_values: bool = False, get_promoted_names: bool = True, promoted_only: bool = True, io_status: str = 'all') VariableList

Creates a VariableList instance containing inputs and outputs of an OpenMDAO Problem.

The inputs (is_input=True) correspond to the variables of IndepVarComp components and all the unconnected input variables.

Note

Variables from _auto_ivc are ignored.

Parameters:
  • problem – OpenMDAO Problem instance to inspect

  • use_initial_values – if True, or if problem has not been run, returned instance will contain values before computation

  • get_promoted_names – if True, promoted names will be returned instead of absolute ones (if no promotion, absolute name will be returned)

  • promoted_only – if True, only promoted variable names will be returned

  • io_status – to choose with type of variable we return (“all”, “inputs, “outputs”)

Returns:

VariableList instance

classmethod from_unconnected_inputs(problem: Problem, with_optional_inputs: bool = False) VariableList

Creates a VariableList instance containing unconnected inputs of an OpenMDAO Problem.

Warning

problem.setup() must have been run.

If optional_inputs is False, only inputs that have numpy.nan as default value (hence considered as mandatory) will be in returned instance. Otherwise, all unconnected inputs will be in returned instance.

Parameters:
  • problem – OpenMDAO Problem instance to inspect

  • with_optional_inputs – If True, returned instance will contain all unconnected inputs. Otherwise, it will contain only mandatory ones.

Returns:

VariableList instance

index(value, start=0, stop=9223372036854775807, /)

Return first index of value.

Raises ValueError if the value is not present.

insert(index, object, /)

Insert object before index.

metadata_keys() List[str]
Returns:

the metadata keys that are common to all variables in the list

names() List[str]
Returns:

names of variables

pop(index=-1, /)

Remove and return item at index (default last).

Raises IndexError if list is empty or index is out of range.

remove(value, /)

Remove first occurrence of value.

Raises ValueError if the value is not present.

reverse()

Reverse IN PLACE.

sort(*, key=None, reverse=False)

Sort the list in ascending order and return None.

The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained).

If a key function is given, apply it once to each list item and sort them, ascending or descending, according to their function values.

The reverse flag can be set to sort in descending order.

to_dataframe() DataFrame

Creates a DataFrame instance from a VariableList instance.

Column names are “name” + the keys returned by Variable.get_openmdao_keys(). Values in Series “value” are floats or lists (numpy arrays are converted).

Returns:

a pandas DataFrame instance with all variables from current list

to_ivc() IndepVarComp
Returns:

an OpenMDAO IndepVarComp instance with all variables from current list

update(other_var_list: list, add_variables: bool = True)

Uses variables in other_var_list to update the current VariableList instance.

For each Variable instance in other_var_list:
  • if a Variable instance with same name exists, it is replaced by the one in other_var_list (special case: if one in other_var_list has an empty description, the original description is kept)

  • if not, Variable instance from other_var_list will be added only if add_variables==True

Parameters:
  • other_var_list – source for new Variable data

  • add_variables – if True, unknown variables are also added

property formatter: IVariableIOFormatter

Class that defines the file format.

load()[source]

Loads file content.

save()[source]

Saves current state of variables in file.

save_as(file_path: str | PathLike, overwrite=False, formatter: IVariableIOFormatter = None)[source]

Sets the associated file path as specified and saves current state of variables.

Parameters:
  • file_path

  • overwrite – if specified file already exists and overwrite is False, an error is triggered.

  • formatter – a class that determines the file format to be used. Defaults to FAST-OAD native format. See VariableIO for more information.