libICEpost.src.base.dataStructures.Tabulation.OFTabulation

@author: F. Ramognino <federico.ramognino@polimi.it> Last update: 12/06/2023

Attributes

to_pandas

merge

Classes

_TableData

Dataclass storing the data for a tabulation

_InputProps

Dataclass storing properties for each input-variable

OFTabulation

Class used to store and handle an OpenFOAM tabulation (structured table).

Functions

toPandas(→ pandas.DataFrame)

Convert an instance of OFTabulation to a pandas.DataFrame with all

concat(table, *tables[, inplace, verbose])

Extend the table with the data of other tables. The tables must have the same variables but

writeOFTable(table[, path, binary, overwrite])

Write the tabulation.

sliceOFTable(→ OFTabulation | None)

Extract a table with sliced datase. Can access in two ways:

clipOFTable(→ OFTabulation | None)

Clip the table to the given ranges. The ranges are given as a dictionary with the

insertDimension(→ OFTabulation | None)

Insert a new dimension in the table by adding a new variable with constant value.

squeeze(→ OFTabulation | None)

Remove dimensions with only one sampling point.

plotOFTable(→ matplotlib.pyplot.Axes)

Plot a field of a tabulation.

plotHeatmapOFTable(→ matplotlib.pyplot.Axes)

Plot a heatmap of a field of a tabulation.

Module Contents

class libICEpost.src.base.dataStructures.Tabulation.OFTabulation._TableData[source]

Bases: object

Dataclass storing the data for a tabulation

file: str

The name of the file for I/O

table: libICEpost.src.base.dataStructures.Tabulation.Tabulation.Tabulation

The tabulation

__eq__(value: object) bool[source]
class libICEpost.src.base.dataStructures.Tabulation.OFTabulation._InputProps[source]

Bases: object

Dataclass storing properties for each input-variable

name: str

The name used in the tablePropeties file

data: Iterable[float]

The data-points

__post_init__()[source]
property numel
__eq__(value: object) bool[source]
libICEpost.src.base.dataStructures.Tabulation.OFTabulation.toPandas(table: OFTabulation) pandas.DataFrame[source]

Convert an instance of OFTabulation to a pandas.DataFrame with all the points stored in the tabulation.

Parameters:

table (OFTabulation) – The OpenFOAM tabulation to convert to a dataframe.

Returns:

A dataframe with all the points stored in the tabulation. Columns for input and output variables

Return type:

pd.DataFrame

libICEpost.src.base.dataStructures.Tabulation.OFTabulation.to_pandas
libICEpost.src.base.dataStructures.Tabulation.OFTabulation.concat(table: OFTabulation, *tables: OFTabulation, inplace: bool = False, verbose: bool = True, **kwargs)[source]

Extend the table with the data of other tables. The tables must have the same variables but not necessarily in the same order. The data of the second table is appended to the data of the first table, preserving the order of the variables.

If fillValue is not given, the ranges of the second table must be consistent with those of the first table in the variables that are not concatenated. If fillValue is given, the missing sampling points are filled with the given value.

Parameters:
  • table (OFTabulation) – The table to which the data is appended.

  • *tables (OFTabulation) – The tables to append.

  • inplace (bool, optional) – If True, the operation is performed in-place. Defaults to False.

  • verbose (bool, optional) – Print information. Defaults to True.

  • **kwargs – Keyword arguments to pass to the ‘concat’ method of the Tabulation objects.

Keyword Arguments:
  • fillValue (float, optional) – The value to fill missing sampling points. Defaults to None.

  • overwrite (bool, optional) – If True, overwrite the data of the first table with the data of the second table in overlapping regions. Otherwise raise an error. Defaults to False.

Returns:

The concatenated table if inplace is False, None otherwise.

Return type:

OFTabulation|None

libICEpost.src.base.dataStructures.Tabulation.OFTabulation.merge
libICEpost.src.base.dataStructures.Tabulation.OFTabulation.writeOFTable(table: OFTabulation, path: str = None, *, binary: bool = False, overwrite: bool = False)[source]

Write the tabulation. Directory structure as follows: ``` path |-tableProperties |-constant | |-variable1 | |-variable2 | |-… |-system

|-controlDict

``` :param table: The tabulation to write. :type table: OFTabulation :param path: Path where to save the table. In case not give, self.path is used. Defaults to None. :type path: str, optional :param binary: Writing in binary? Defaults to False. :type binary: bool, optional :param overwrite: Overwrite the table if found? Defaults to False. :type overwrite: bool, optional

libICEpost.src.base.dataStructures.Tabulation.OFTabulation.sliceOFTable(table: OFTabulation, *, slices: Iterable[slice | Iterable[int] | int] = None, ranges: dict[str, float | Iterable[float]] = None, inplace=False, **argv) OFTabulation | None[source]
Extract a table with sliced datase. Can access in two ways:
  1. by slicer

  2. sub-set of interpolation points. Keyword arguments also accepred.

For safety, the new table will not be writable and the path will be set to None.

Parameters:
  • table (Tabulation) – The table

  • slices (Iterable[slice|Iterable[int]|int], optional) – The slices to extract the table. Defaults to None.

  • ranges (dict[str,float|Iterable[float]], optional) – The ranges to extract the table. Defaults to None.

  • inplace (bool, optional) – If True, the operation is performed in-place. Defaults to False.

  • **argv – Keyword arguments to pass to the ranges.

Returns:

The sliced table if inplace is False, None otherwise.

Return type:

OFTabulation|None

libICEpost.src.base.dataStructures.Tabulation.OFTabulation.clipOFTable(table: OFTabulation, *, ranges: dict[str, tuple[float, float]] = None, inplace: bool = False, **kwargs) OFTabulation | None[source]

Clip the table to the given ranges. The ranges are given as a dictionary with the variable names as keys and a tuple with the minimum and maximum values.

Parameters:
  • table (OFTabulation) – The table to clip.

  • ranges (dict[str,tuple[float|None,float|None]], optional) – The ranges to clip for each input-variable. If min or max is None, the range is unbounded.

  • inplace (bool, optional) – If True, the operation is performed in-place. Defaults to False.

  • **kwargs – Can access also by keyword arguments.

Returns:

The clipped table if inplace is False, None otherwise.

Return type:

OFTabulation|None

libICEpost.src.base.dataStructures.Tabulation.OFTabulation.insertDimension(table: OFTabulation, *, variable: str, value: float, index: int = None, inplace: bool = False) OFTabulation | None[source]

Insert a new dimension in the table by adding a new variable with constant value.

Parameters:
  • table (OFTabulation) – The table to insert the dimension.

  • variable (str) – The name of the input variable to insert.

  • value (float) – The value to assign to the new variable.

  • index (int, optional) – The index where to insert the new variable. Defaults to None (append at the end).

  • inplace (bool, optional) – If True, the operation is performed in-place. Defaults to False.

Returns:

The new table with the inserted dimension if inplace is False, None otherwise.

Return type:

OFTabulation|None

libICEpost.src.base.dataStructures.Tabulation.OFTabulation.squeeze(table: OFTabulation, *, inplace: bool = False) OFTabulation | None[source]

Remove dimensions with only one sampling point.

Parameters:
  • table (OFTabulation) – The table to squeeze.

  • inplace (bool, optional) – If True, the operation is performed in-place. Defaults to False.

Returns:

The squeezed table if inplace is False, None otherwise.

Return type:

OFTabulation|None

libICEpost.src.base.dataStructures.Tabulation.OFTabulation.plotOFTable(table: OFTabulation, field: str, **kwargs) matplotlib.pyplot.Axes[source]

Plot a field of a tabulation.

Parameters:
  • table (OFTabulation) – The tabulation to plot.

  • field (str) – The field to plot.

  • **kwargs – Keyword arguments to pass to the ‘plot’ method of the Tabulation object.

Returns:

The axis where the plot is drawn.

Return type:

plt.Axes

libICEpost.src.base.dataStructures.Tabulation.OFTabulation.plotHeatmapOFTable(table: OFTabulation, field: str, **kwargs) matplotlib.pyplot.Axes[source]

Plot a heatmap of a field of a tabulation.

Parameters:
  • table (OFTabulation) – The tabulation to plot.

  • field (str) – The field to plot.

  • **kwargs – Keyword arguments to pass to the ‘plotHeatmap’ method of the Tabulation object.

Returns:

The axis where the plot is drawn.

Return type:

plt.Axes

class libICEpost.src.base.dataStructures.Tabulation.OFTabulation.OFTabulation(ranges: dict[str, Iterable[float]], data: dict[str, Iterable[float]], *, path: str = None, order: Iterable[str], files: dict[str, str] = None, inputNames: dict[str, str] = None, outputNames: dict[str, str] = None, noWrite: bool = True, tablePropertiesParameters: dict[str, Any] = None, **kwargs)[source]

Bases: libICEpost.src.base.dataStructures.Tabulation.BaseTabulation.BaseTabulation

Class used to store and handle an OpenFOAM tabulation (structured table).

The tabulation is a multi-input multi-output, i.e., it access through a set of input variables (IV) to a set of tabulated variables (TV):

[IV1, IV2, IV3, …] -> [TV1, TV2, TV3, …]

_path: str

The path where the table is stored

_baseTableProperties: dict

The additional data in the ‘tableProperties’ file apart from sampling points.

_data: dict[str, _TableData]

The data stored in the tabulation

_inputVariables: dict[str, _InputProps]

The properties of the input variables used to access the tabulation

_noWrite: bool

Allow writing

property path: str | None

The path of the tabulation

property tableProperties: dict[str:str]

The table properties dictionary (read-only).

property names: dict[str, str]

Names to give at the variables found in the ‘tableProperties’ dictionary (read-only).

property inputVariables: list[str]

The input variables to access the tabulation (read-only).

property fields: list[str]

The fields tabulated.

property ranges: dict[str, numpy.array[float]]

The sampling points of the input variables to access the tabulation (read-only).

property noWrite: bool

Allow writing?

property tables: dict[str, libICEpost.src.base.dataStructures.Tabulation.Tabulation.Tabulation | None]

The tabulations for each variable (read-only).

property files: dict[str, str]

The name of the files where tables are saved (read-only).

property size

Returns the size of the table, i.e., the number of sampling points.

property shape: tuple[int]

The dimensions (dim1, dim2,…, dimn) of the tabulation.

property ndim: int

Returns the number of dimentsions of the table.

classmethod fromFile(path: str, order: Iterable[str] = None, *, inputNames: dict[str, str] = None, fields: Iterable[str] = None, outputNames: dict[str, str] = None, files: dict[str, str] = None, noRead: Iterable[str] = None, verbose: bool = True, **kwargs) OFTabulation[source]

Construct a table from files stored in an OpenFOAM-LibICE tabulation located at ‘path’. Directory structure as follows:

path |-tableProperties |—constant | |-variable1 | |-variable2 | |-… |—system

|-controlDict |-fvSchemes |-fvSolutions

Parameters:
  • path (str) – The master path where the tabulation is stored.

  • order (Iterable[str], optional) – Nesting order of the input-variables used to access the tabulation. In case not given, lookup for entry ‘inputVariables’ in ‘tableProperties’ file.

  • inputNames (dict[str,str], optional) – Renaming the input variables found in the ‘tableProperties’ file. Defaults to None.

  • fields (Iterable[str], optional) – The name of the fields to use for each output variable (by default, lookup for entry ‘fields’ in ‘tableProperties’ file). Defaults to None.

  • outputNames (dict[str,str], optional) – Renaming the output variables found in the ‘tableProperties’ file. Defaults to None.

  • files (dict[str,str], optional) – The name of the files to use for each output variable (by default, the name of the fields). Defaults to None.

  • noRead (Iterable[str], optional) – Do not read the data of the given variables. Defaults to None.

  • verbose (bool, optional) – Print information. Defaults to True.

  • **kwargs – Optional keyword arguments of Tabulation.__init__ method of each Tabulation object.

Kwargs:

outOfBounds (Literal[‘fatal’, ‘clamp’, ‘extrapolate’], optional): Option to perform in case of out-of-bounds data (TODO).

Returns:

The tabulation loaded from files.

Return type:

OFTabulation

classmethod from_pandas(data: pandas.DataFrame, order: Iterable[str], **kwargs)[source]

Construct a tabulation from a pandas DataFrame.

Parameters:
  • data (pd.DataFrame) – The data to construct the tabulation.

  • order (Iterable[str]) – The order of the input-variables.

  • **kwargs – Optional keyword arguments of OFTabulation.__init__ method.

Kwargs:

outOfBounds (Literal[‘fatal’, ‘clamp’, ‘extrapolate’], optional): Option to perform in case of out-of-bounds data (TODO).

Returns:

The tabulation constructed from the pandas DataFrame.

Return type:

OFTabulation

fromPandas
_order

The order in which the input variables are nested

checkDir()[source]
Check if all information required to read the tabulation are consistent and present in ‘path’. Looking for:

path path/constant path/tableProperties

copy()[source]

Return a copy of the tabulation. For safety, the new table will not be writable and the path will be set to None.

slice
Extract a table with sliced data. Can access in two ways:
  1. by slicer

  2. sub-set of interpolation points. Keyword arguments also accepred.

Parameters:
  • slices (Iterable[slice|Iterable[int]|int]) – The slicers for each input-variable.

  • ranges (dict[str,float|Iterable[float]], optional) – Ranges of sliced table. Defaults to None.

  • inplace (bool, optional) – If True, the operation is performed in-place. Defaults to False.

Returns:

The sliced table if inplace is False, None otherwise.

Return type:

Self|None

clip

Clip the table to the given ranges. The ranges are given as a dictionary with the variable names as keys and a tuple with the minimum and maximum values.

Parameters:
  • ranges (dict[str,tuple[float|None,float|None]], optional) – The ranges to clip for each input-variable. If min or max is None, the range is unbounded.

  • inplace (bool, optional) – If True, the operation is performed in-place. Defaults to False.

  • **kwargs – Can access also by keyword arguments.

Returns:

The clipped table if inplace is False, None otherwise.

Return type:

Self|None

write
insertDimension

Insert an axis to the dimension-set of the table with a single value. This is useful to merge two tables with respect to an additional variable.

Parameters:
  • table (BaseTabulation) – The table to modify.

  • variable (str) – The name of the variable to insert.

  • value (float) – The value for the range of the corresponding variable.

  • index (int, optional) – The index where to insert the variable in nesting order. If None, append the variable at the end. Defaults to None.

  • inplace (bool, optional) – If True, the operation is performed in-place. Defaults to False.

Returns:

The table with the inserted dimension if inplace is False, None otherwise.

Return type:

BaseTabulation|None

Example

Create a table with two variables: ` >>> tab1 = Tabulation([1, 2, 3, 4], {"x":[0, 1], "y":[0, 1]}, ["x", "y"]) >>> tab1.insertDimension("z", 0.0, 1) >>> tab1.ranges {"x":[0, 1], "z":[0.0], "y":[0, 1]} ` Create a second table with the same variables: ` >>> tab2 = Tabulation([5, 6, 7, 8], {"x":[0, 1], "y":[0, 1]}, ["x", "y"]) >>> tab2.insertDimension("z", 1.0, 1) >>> tab2.ranges {"x":[0, 1], "z":[1.0], "y":[0, 1]} `

Concatenate the two tables: ` >>> tab1.concat(tab2, inplace=True) >>> tab1.ranges {"x":[0, 1], "z":[0.0, 1.0], "y":[0, 1]} `

squeeze

Remove dimensions with only 1 data-point.

Parameters:

inplace (bool, optional) – If True, the operation is performed in-place. Defaults to False.

Returns:

The squeezed tabulation if inplace is False, None otherwise.

Return type:

Self|None

plot

Plot the tabulation.

plotHeatmap

Plot a heatmap of the tabulation.

clear()[source]

Clear the tabulation.

setFile(field: str, file: str) None[source]

Set the name of the file where to save the table of a field.

Parameters:
  • field (str) – The field to set the file-name of.

  • file (str) – The name of the file.

setTable(field: str, table: libICEpost.src.base.dataStructures.Tabulation.Tabulation.Tabulation | None) None[source]

Overwrite the table of a field.

Parameters:
  • field (str) – The field to set the file-name of.

  • file (str) – The name of the file.

addField(data: Iterable[float] | float | int | libICEpost.src.base.dataStructures.Tabulation.Tabulation.Tabulation | None, *, field: str, file: str = None, **kwargs)[source]

Add a new tabulated field (output variable).

Parameters:
  • field (str) – The name of the variable.

  • data (Iterable | list[float] | float | Tabulation) – The data used to construct the tabulation. Defaults to None.

  • file (str, optional) – The name of the file for I/O. Defaults to None (same as ‘field’ value).

  • **kwargs – Keyword arguments for construction of each Tabulation object.

delField(field: str)[source]

Delete a field from the tabulation.

Parameters:

field (str) – The field to delete.

setName(variable: str, name: str) None[source]

Set the name of a input-variable to use in the ‘tableProperties’ dictionary.

Parameters:
  • variable (str) – The input-variable to set the name of.

  • name (str) – The name of the input-variable.

outOfBounds(field: str, method: str = None) str | None[source]

Get/set the out-of-bounds method for a field.

Parameters:
  • field (str) – The field to get/set the out-of-bounds method.

  • method (str, optional) – The method to set. Defaults to None.

Returns:

The out-of-bounds method for the field if method is not None, None otherwise.

Return type:

str|None

setRange(variable: str, range: Iterable[float]) None[source]

Set the range of a variable.

Parameters:
  • variable (str) – The variable to set the range of.

  • range (Iterable[float]) – The range of the variable.

_readTableProperties(*, inputNames: dict[str, str] = None, inputVariables: Iterable[str] = None, fields: Iterable[str] = None) Iterable[str][source]

Read information stored in file ‘path/tableProperties’.

Parameters:
  • inputNames (dict[str,str], optional) – The names to give to the input variables (optionally). Defaults to None.

  • inputVariables (Iterable[str], optional) – The input variables in correct nesting order. If not given, lookup for entry inputVariables in tableProperties file. Defaults to None.

  • fields (Iterable[str], optional) – The names of the fields to use for each output variable (by default, lookup for entry fields in tableProperties file). Defaults to None.

Returns:

The names of the fields stored in the tabulation.

Return type:

Iterable[str]

_readTable(fileName: str, tableName: str, *, verbose: bool = True, **kwargs)[source]

Read a tabulation from path/constant/fileName.

Parameters:
  • fileName (str) – The name of the file where the tabulation is stored.

  • tableName (str) – The name to give to the loaded field in the tabulation.

  • verbose (bool, optional) – Print information about the loading process. Defaults to True.

  • **kwargs – Optional keyword arguments of Tabulation.__init__ method of each Tabulation object.

Returns:

self

Return type:

Self

__getitem__(index: int | Iterable[int] | slice) dict[str, float] | dict[str, numpy.ndarray[float]][source]

Get an element in the table.

Parameters:

index (int | Iterable[int] | slice | Iterable[slice]) – Either: - An index to access the table (flattened). - A tuple of the x,y,z,… indices to access the table. - A slice to access the table (flattened). - A tuple of slices to access the table.

Returns:

The data stored in the table. - If a single index is given, a dictionary with the output variables at that index. - If slice|Iterable[slice] is given, a dictionary with the output variables at that slice.

Return type:

dict[str,float]|dict[str,np.ndarray[float]]

abstractmethod __setitem__(index: int | Iterable[int] | slice, value: dict[str, float] | dict[str, numpy.ndarray[float]]) None[source]

Setting values in the table is not allowed.

__call__(table: str, *args, **kwargs)[source]

Interpolate from a specific table stored in the tabulation.

Parameters:
  • table (str) – The name of the table to use to interpolate the data.

  • *args – Passed to the ‘__call__’ method of the Tabulation instance to interpolate.

  • **kwargs – Passed to the ‘__call__’ method of the Tabulation instance to interpolate.

Returns:

The interpolated data from the specified table.

Return type:

float|np.ndarray[float]

__eq__(value: OFTabulation) bool[source]
__repr__()[source]
__str__()[source]