ftag.transform#

Classes#

Transform

Apply variable name remapping, integer remapping, and float transformations.

Module Contents#

class ftag.transform.Transform#

Apply variable name remapping, integer remapping, and float transformations.

The Transform class provides a unified mechanism to perform: - variable renaming (variable_map) - integer value remapping (ints_map) - float transformations (floats_map)

Each transformation is applied to a batch consisting of a dictionary of structured numpy arrays.

variable_map#

A nested mapping where variable_map[group][old] = new specifies how variable names should be renamed inside a given group. If None, no variable renaming is applied.

Type:

dict[str, dict[str, str]]

ints_map#

A nested mapping where ints_map[group][variable][old] = new specifies how integer values should be remapped. If None, no integer remapping is applied.

Type:

dict[str, dict[str, dict[int, int]]]

floats_map#

A nested mapping where floats_map[group][variable] = func specifies a float transformation function. func may either be: - a callable - a string giving the name of a numpy function (e.g. “log”)

Strings are resolved to numpy.<func> automatically.

Type:

dict[str, dict[str, str | Callable]]

variable_map_inv#

Automatically generated inverse of variable_map used for reverse variable lookup in map_variable_names().

Type:

dict[str, dict[str, str]]

variable_map: dict[str, dict[str, str]]#
ints_map: dict[str, dict[str, dict[int, int]]]#
floats_map: dict[str, dict[str, str | collections.abc.Callable]]#
variable_map_inv: dict[str, dict[str, str]]#
__post_init__() None#

Initialize internal maps and convert float transformation strings.

This method ensures that variable_map, ints_map, and floats_map are always dictionaries (never None), constructs the inverse variable map, and converts any string-based float transformations into their numpy equivalents.

__call__(batch: Batch) Batch#

Apply integer remapping, float transformations, and variable renaming.

Parameters:

batch (Batch) – A mapping from group name to structured numpy arrays.

Returns:

The transformed batch.

Return type:

Batch

map_variables(batch: Batch) Batch#

Rename variables in each group according to variable_map.

Parameters:

batch (Batch) – Dictionary mapping group names to structured numpy arrays.

Returns:

The batch with variables renamed where applicable.

Return type:

Batch

map_ints(batch: Batch) Batch#

Remap integer values for specified variables inside each group.

Parameters:

batch (Batch) – Dictionary mapping group names to structured numpy arrays.

Returns:

The batch with integer values remapped.

Return type:

Batch

map_floats(batch: Batch) Batch#

Apply float transformations to selected variables.

Parameters:

batch (Batch) – Dictionary mapping group names to structured numpy arrays.

Returns:

The batch with float transformations applied.

Return type:

Batch

map_dtype(name: str, dtype: numpy.dtype) numpy.dtype#

Compute a new dtype with renamed fields according to variable_map.

Parameters:
  • name (str) – Group name associated with the dtype.

  • dtype (np.dtype) – Structured dtype whose field names may be modified.

Returns:

A dtype with renamed fields where required.

Return type:

np.dtype

Raises:

ValueError – When the variables already exist in the dataset.

map_variable_names(name: str, variables: list[str], inverse: bool = False) list[str]#

Map a list of variable names using variable_map or variable_map_inv.

Parameters:
  • name (str) – Group name used to select the appropriate name-mapping dictionary.

  • variables (list[str]) – List of variable names to be mapped.

  • inverse (bool, optional) – If False (default), apply variable_map. If True, apply the inverse mapping variable_map_inv.

Returns:

A new list of mapped variable names.

Return type:

list[str]