improver.utilities.extract_from_table module#
Provides ability to extract values from table.
- class ExtractValueFromTable(row_name, new_name=None)[source]#
Bases:
BasePluginPlugin to extract values from a table using the provided inputs cubes to select which rows and columns to extract.
The table is expected to contain numerical labels for every row and column. These labels will be sorted into ascending order.
The input cubes are used to select the row and column labels from the table with one cube used to select the rows and the other cube used to select the columns. The values in the table are then extracted based on the row and column labels selected from the cubes such that the nearest lower label from the table is used.
For Example: If the table has the following row labels of [0,1,2,3] and the row cube had a values of 1.9 then the row with label 1 would be selected. If the row cube had a value of 2 then the row with label 2 would be selected. If the row cube had a value of -4 then the row with label 0 would be selected.
- _abc_impl = <_abc._abc_data object>#
- convert_dict_to_dataframe(table)[source]#
Converts a dictionary to a pandas DataFrame
- Return type:
DataFrame
- extract_table_values(table, columns_cube, row_cube)[source]#
Extract values from the table based on the provided row and column cubes. :type table:
DataFrame:param table: DataFrame representing the table from which values are extracted. :type columns_cube:Cube:param columns_cube: Cube used to index the columns of the table. :type row_cube:Cube:param row_cube: Cube used to index the rows of the table.- Return type:
array- Returns:
Array of values extracted from the table.
- static nearest_lower_index(values, sorted_table_labels)[source]#
Returns the index of the nearest lower label for every element of values. :type values:
array:param values: Array of values to extract from table :param table_label: An array of the labels along an axis of the table.- Return type:
array- Returns:
An array of indices of the nearest lower label for each value in values.
- process(*cubes, table)[source]#
Process the input cubes and extract values from a table based on the provided row and column indices. The row name is used to identify the cube used for indexing the rows of the table. The new name is an optional argument that can be used to rename the resulting cube.
- Parameters:
cubes (
List[Cube]) – Input cubes for indexing columns and rows of the table. Exactly 2 cubes should be provided one which contains the values to extract from the rows and one for the columns.table (
dict) – A dictionary representing the table from which values are extracted. Dictionary should be in the form: {“data”:{column_name_1:{row_name_1:value, row_name_2:value},…}, “metadata”:{“units”:table_units}} Other metadata can be included in the metadata dictionary such as a title for the table but this will be ignored.
- Returns:
Cube of the same shape and metadata as the row input cube with values extracted from the table based on the row and column input cubes. The cube will be re-named if new_name is provided and the units updated to match the units of the table.
- Raises:
If exactly 2 cubes are not provided. - If the shapes of the column and row cubes do not match.