The DataLinker class#
Aliases#
halerium.core.DataLinker
halerium.core.data_linker.DataLinker
- class DataLinker(n_data=1)#
The DataLinker holds data in the form of numpy arrays which are to be linked to specific Variables in a Graph.
Additionally, the DataLinker dictates the size of the data-dimension. All non-static variables will be scaled up according to n_data. For example, a Variable with a shape of (3, 4) will be represented in the model by an array of shape (n_data, 3, 4).
DataLinker instances are therefore a key ingredient when creating a model.
Holds links of data to variables in a graph.
- Parameters:
n_data (int) – The number of examples in data. The default is 1.
- add_links_from(data)#
Add links from data.
- Parameters:
data (dict) – A dictionary with Variable instances as keys and array-likes as values.
- assert_has_compatible_shape(variable, array, name_of_array)#
Assert variable and array have compatible shape.
- Parameters:
variable – The variable with given shape.
array – The array to check shape.
name_of_array (str) – The name of the array in the potential error message.
- Raises:
ValueError : – If shape of variable and array are incompatible.
- copy(deep=False)#
Make a copy.
Returns a copy of the DataLinker instance which holds the same data links.
Note that unless deep=True, the `numpy.ndarray`s holding the data are not copied but referenced. So in-place manipulations of the arrays will change both the original and the copied data_linker.
- Parameters:
deep (bool, optional) –
- Returns:
copy_instance – The copy of the data linker.
- Return type:
- get_data_and_mask(variable_or_global_name)#
Get data and mask.
Get the data and mask, if present, for a given variable, else return None.
- Parameters:
variable_or_global_name (VariableBase or string) – either the variable instance or its global name
- Returns:
data (np.ndarray, None) – Ff the DataLinker contains a link for the given variable, the data are returned. Otherwise, the return is None.
mask (np.ndarray, None) – If the DataLinker contains a link for the given variable and the data have masked entries, the boolean mask is returned. Otherwise, the return is None.
- get_full_shape(variable)#
Get full shape of variable.
- Parameters:
variable (VariableBase) – The variable to compute the full shape for.
- Returns:
shape – The full shape of the variable.
- Return type:
tuple
- has_data(variable_or_global_name)#
Inquire whether a given variable has data.
- Parameters:
variable_or_global_name (str, VariableBase) – Either the variable instance or its global name.
- Returns:
Whether the variable has a data.
- Return type:
bool
- is_fully_determined_by_data(variable_or_global_name)#
Inquire whether a given variable is fully determined by data linked to the DataLinker. A variable is fully determined, if the data linker contains a link without any masked entries in the data.
- Parameters:
variable_or_global_name (VariableBase or string) – either the variable instance or its global name
- Returns:
bool
- Return type:
True if fully determined, otherwise False
- items()#
- Returns:
The items of the link registry.
- Return type:
items
- keys()#
- Returns:
The keys of the link registry (global names of variables).
- Return type:
keys
- link(variable, data, mask=None)#
Link data to variable.
The method to link data to a halerium variable. Additionally, to the data the user can provide a mask, that flags unknown values in the data array.
- Parameters:
variable (VariableBase) – The Variable instance to which the data belong.
data (array-like) – The data to be linked to the variable. It will be cast to a numpy.ndarray of dtype float. Non-finite values will be masked automatically, but a mask can also be provided explicitly.
mask (numpy.ndarray, None) – The mask to apply to data. Either None, or a numpy.ndarray of dtype bool. If provided, the indices containing True will be masked in the data array.
- property n_data#
- property registry#
A dictionary containing the linked data assigned to variable names.
Warning: Although a new dict is created and returned, the returned dict values are numpy arrays which are mutable. Thus changing values in these arrays in this instance’s _link_registry will change these values in the returned dict as well (and vice versa).
- Returns:
A shallow copy of the instance’s link registry.
- Return type:
registry
- reset()#
Reset the DataLinker instance. All links are deleted.
- to_compatible_shape(variable, array)#
Cast array to shape compatible with variable.
- Parameters:
variable (VariableBase) – The variable to which the array shape to adjust.
array (numpy.ndarray) – The array whose shape is to be adjusted.
- Returns:
array – The array with possibly adjusted shape compatible with variable.
- Return type:
np.ndarray
- unlink(variable_or_global_name)#
Remove the data link for a specific variable.
- Parameters:
variable_or_global_name (VariableBase, str) – Either the variable instance or its global name.
- update(other)#
Update self with other.
Update self with data from other overwriting existing keys.
- Parameters:
other (dict, DataLinker,) – The other data to update self with.
- values()#
- Returns:
The values of the link registry (pairs of variable data and mask).
- Return type:
values