Transformation

Transformation module for Macrodata Refinement (MDR).

This module provides functions for transforming macrodata through various statistical and mathematical operations.

class mdr.core.transformation.NormalizationType(value)[source]

Bases: Enum

Types of normalization methods.

MINMAX = 1
ZSCORE = 2
ROBUST = 3
DECIMAL_SCALING = 4
mdr.core.transformation.normalize_data(data, method='minmax', params=None)[source]

Normalize data using the specified method.

Parameters:
  • data (<MagicMock id='136017410896464'>) – Input data array

  • method (str | NormalizationType) – Normalization method (‘minmax’, ‘zscore’, ‘robust’, ‘decimal_scaling’)

  • params (Dict[str, Any] | None) – Additional parameters for normalization

Returns:

Tuple of (normalized data array, normalization parameters)

Return type:

Tuple[<MagicMock id=’136017410653152’>, Dict[str, float]]

mdr.core.transformation.scale_data(data, factor, offset=0.0)[source]

Scale data by a factor and add an offset.

Parameters:
  • data (<MagicMock id='136017404941344'>) – Input data array

  • factor (float) – Scaling factor

  • offset (float) – Offset to add after scaling

Returns:

Scaled data array

Return type:

<MagicMock id=’136017411230448’>

mdr.core.transformation.apply_logarithmic_transform(data, base=10.0, epsilon=1e-10)[source]

Apply logarithmic transformation to the data.

Parameters:
  • data (<MagicMock id='136017410779328'>) – Input data array

  • base (float) – Logarithm base

  • epsilon (float) – Small value to add to prevent log(0)

Returns:

Log-transformed data array

Return type:

<MagicMock id=’136017410771744’>

mdr.core.transformation.apply_power_transform(data, power, preserve_sign=True)[source]

Apply power transformation to the data.

Parameters:
  • data (<MagicMock id='136017411337008'>) – Input data array

  • power (float) – Power to raise the data to

  • preserve_sign (bool) – Whether to preserve the sign of the original data

Returns:

Power-transformed data array

Return type:

<MagicMock id=’136017416485328’>

mdr.core.transformation.apply_rolling_window(data, window_size, window_function, center=True)[source]

Apply a rolling window function to the data.

Parameters:
  • data (<MagicMock id='136017416477648'>) – Input data array

  • window_size (int) – Size of the rolling window

  • window_function (Callable[[<MagicMock id='136017521700928'>], float]) – Function to apply to each window (e.g., np.mean, np.median)

  • center (bool) – Whether to center the window

Returns:

Data array with the rolling window function applied

Return type:

<MagicMock id=’136017412003040’>

mdr.core.transformation.transform_data(data, transformations)[source]

Apply a sequence of transformations to the data.

Parameters:
  • data (<MagicMock id='136017412010960'>) – Input data array

  • transformations (List[Dict[str, Any]]) – List of transformation specifications

Returns:

Transformed data array

Return type:

<MagicMock id=’136017410893392’>

Overview

The transformation module provides functions for transforming data through various statistical and mathematical operations. These transformations help prepare data for analysis or visualization by standardizing scales, normalizing distributions, or applying mathematical transformations.

Core Functions

mdr.core.transformation.transform_data(data, transformations)[source]

Apply a sequence of transformations to the data.

Parameters:
  • data (<MagicMock id='136017412010960'>) – Input data array

  • transformations (List[Dict[str, Any]]) – List of transformation specifications

Returns:

Transformed data array

Return type:

<MagicMock id=’136017410893392’>

Available Transformations

The module supports the following transformation types:

  • normalize: Scale data to a standard range (e.g., 0-1, -1 to 1)

  • scale: Apply linear scaling with a factor and offset

  • log: Apply logarithmic transformation

  • power: Apply power transformation

  • boxcox: Apply Box-Cox transformation for normalizing distributions

  • winsorize: Limit extreme values to reduce the effect of outliers

Usage Examples

Basic transformation of data:

import numpy as np
from mdr.core.transformation import transform_data

# Create sample data
data = np.array([1.0, 2.0, 3.0, 4.0, 5.0])

# Define transformation pipeline
transformations = [
    {"type": "normalize", "method": "minmax"},
    {"type": "scale", "factor": 2.0, "offset": 1.0}
]

# Apply transformations
transformed_data = transform_data(data, transformations)

print("Original data:", data)
print("Transformed data:", transformed_data)

Applying multiple transformations to multiple variables:

import numpy as np
from mdr.core.transformation import transform_data

# Create a dictionary of data variables
data_dict = {
    "temperature": np.array([20.5, 21.3, 22.1, 21.7, 23.0]),
    "pressure": np.array([101.3, 101.4, 101.5, 101.2, 101.1])
}

# Define transformation pipeline
transformations = [
    {"type": "normalize", "method": "zscore"},
    {"type": "scale", "factor": 1.0, "offset": 0.0}
]

# Apply transformations to each variable
transformed_dict = {}
for var_name, values in data_dict.items():
    transformed_dict[var_name] = transform_data(values, transformations)