as_dict(output)

Converts an output with ExtensionType or dataclass type to dict. Annotations are preserved in the dict. Use RecursiveNamespace to load it back with annotations.

Source code in wt_ml/tuning/utils.py
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
@in_cpu
def as_dict(output: "ModelOutputType" | dict | Mapping | tf.experimental.ExtensionType) -> dict[str, ...]:
    """
    Converts an output with ExtensionType or dataclass type to dict.
    Annotations are preserved in the dict.
    Use RecursiveNamespace to load it back with __annotations__.
    """
    if isinstance(output, (dict, Mapping)):
        return {k: as_dict(v) for k, v in output.items()}
    elif isinstance(output, tf.experimental.ExtensionType):
        return {k: as_dict(v) for k, v in tf.experimental.extension_type.as_dict(output).items()} | {
            "__annotations__": getattr(type(output), "annotations", getattr(type(output), "__annotations__", {}))
        }
    elif is_dataclass(output):
        return {k: as_dict(v) for k, v in dataclass_asdict(output).items()} | {
            "__annotations__": getattr(type(output), "__annotations__", {})
        }
    elif isinstance(output, SimpleNamespace):
        return {k: as_dict(v) for k, v in vars(output).items()} | {
            "__annotations__": getattr(type(output), "__annotations__", {})
        }
    else:
        return output

calculate_curves_diff(prev_output, current_output, encodings)

Takes 2 model outputs with trackers and then calculates _calculate_curves_diff.

Source code in wt_ml/tuning/utils.py
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def calculate_curves_diff(
    prev_output: "EconomicIntermediariesWithTrackers",
    current_output: "EconomicIntermediariesWithTrackers",
    encodings: Encodings,
) -> dict[str, NDArray]:
    """Takes 2 model outputs with trackers and then calculates _calculate_curves_diff."""
    if not (hasattr(prev_output, "trackers") and hasattr(current_output, "trackers")):
        raise ValueError("`trackers` not found! Please run WFO with `calculate_trackers=True`.")

    _, prev_roicurve = get_curve_matrix(
        to_numpy(prev_output.trackers.curve_tracker.spends),
        to_numpy(prev_output.trackers.curve_tracker.impact),
        to_numpy(prev_output.trackers.curve_tracker.slope),
        encodings["normalization_factor"],
        dynamic_range=True,
    )
    _, prev_global_me = get_mixed_effect_matrix(prev_output.trackers.global_me_tracker)
    _, prev_pricing_me = get_mixed_effect_matrix(prev_output.trackers.pricing_me_tracker)

    curr_roi_idx, curr_roicurve = get_curve_matrix(
        to_numpy(current_output.trackers.curve_tracker.spends),
        to_numpy(current_output.trackers.curve_tracker.impact),
        to_numpy(current_output.trackers.curve_tracker.slope),
        encodings["normalization_factor"],
        dynamic_range=True,
    )
    curr_global_idx, curr_global_me = get_mixed_effect_matrix(current_output.trackers.global_me_tracker)
    curr_pricing_idx, curr_pricing_me = get_mixed_effect_matrix(current_output.trackers.pricing_me_tracker)

    metric_combinations = {
        "roi_diffs": (prev_roicurve, curr_roicurve, curr_roi_idx),
        "global_me_impacts_diffs": (prev_global_me, curr_global_me, curr_global_idx),
        "pricing_me_impacts_diffs": (prev_pricing_me, curr_pricing_me, curr_pricing_idx),
    }

    return {
        metric: calculate_curves_diff_utils(prev[:, :, 1, :], curr[:, :, 1, :], idx[:, None, None])
        for metric, (prev, curr, idx) in metric_combinations.items()
    }

calculate_curves_diff_utils(y_a, y_b, x_vals, axis=0)

Approximate \(\int_{\min{spend}}^{\max{spend}} \left(roi_i'(x) - roi_{i+1}'(x)\right)^2\) to get difference of rois or any two curves in general. Units of the result are the same as the units of the y values passed in.

Parameters:

Name Type Description Default
y_a ndarray

Curve 1 y values at each x. Shape: num_points, batch, vehicles.

required
y_b ndarray

Curve 2 y values at each x. Shape: num_points, batch, vehicles.

required
x_vals TensorLike

The common x axis values of the two curves. Shape: spend_points,.

required
axis int

The axis of integration, defaults to the 1st axis (spend_points).

0

Returns: np.ndarray: Computed integral of squared differences between two curves.

Source code in wt_ml/tuning/utils.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def calculate_curves_diff_utils(y_a: TensorLike, y_b: TensorLike, x_vals: TensorLike, axis: int = 0) -> NDArray:
    """Approximate $\\int_{\\min{spend}}^{\\max{spend}} \\left(roi_i'(x) - roi_{i+1}'(x)\\right)^2$
       to get difference of rois or any two curves in general. Units of the result are the same as
       the units of the y values passed in.

    Args:
        y_a (np.ndarray): Curve 1 y values at each x. Shape: num_points, batch, vehicles.
        y_b (np.ndarray): Curve 2 y values at each x. Shape: num_points, batch, vehicles.
        x_vals: The common x axis values of the two curves. Shape: spend_points,.
        axis (int, optional): The axis of integration, defaults to the 1st axis (spend_points).
    Returns:
        np.ndarray: Computed integral of squared differences between two curves.
    """
    # spend_points, batch, vehicles
    squared_diff = np.square(y_a - y_b)
    # batch, vehicles
    areas = np.trapz(squared_diff, x_vals, axis=axis) / (np.max(x_vals, axis=axis) - np.min(x_vals, axis=axis))
    return np.sqrt(areas)

concat_intermediaries(intermediaries, axis=0, axis_type=Axis.Batch)

Concatenates tf.experimental.ExtensionType objects in the batch_axis. Make sure that both axis and axis_type matches else it leads to unexpected results.

Parameters:

Name Type Description Default
intermediaries list[ExtensionType]

tf.experimental.ExtensionType type instance.

required
axis int

The axis where we need to concatenate. Defaults to 0 which is assumed to be batch.

0
axis_type(Axis, optional

The annotated axis where we need to concatenate. Defaults to Axis.Batch.

required

Returns:

Type Description
T

tf.experimental.ExtensionType: The concatenated output which is the same type that is passed in.

Source code in wt_ml/tuning/utils.py
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
@in_cpu
@warn_once(ConcatWarning)
def concat_intermediaries(
    intermediaries: list[T],
    axis: int = 0,
    axis_type: Axis = Axis.Batch,
) -> T:
    """Concatenates tf.experimental.ExtensionType objects in the batch_axis.
    Make sure that both `axis` and `axis_type` matches else it leads to unexpected results.

    Args:
        intermediaries (list[tf.experimental.ExtensionType]): tf.experimental.ExtensionType type instance.
        axis (int, optional): The axis where we need to concatenate. Defaults to 0 which is assumed to be batch.
        axis_type(Axis, optional): The annotated axis where we need to concatenate. Defaults to Axis.Batch.

    Returns:
        tf.experimental.ExtensionType: The concatenated output which is the same type that is passed in.
    """
    if not hasattr(intermediaries, "__len__") or len(intermediaries) < 1:
        # when we don't pass a list. Only avoid an error.
        return intermediaries
    elif len(intermediaries) == 1:
        # Handling edge case where its full batch.
        return intermediaries[0]

    OutputType = type(intermediaries[0])
    annotations: dict[str, ...] = getattr(OutputType, "annotations", getattr(OutputType, "__annotations__", {}))
    concated_intermediaries = {}
    # ExtensionType supports Mapping, so this detects that for us. else we assume its an ExtensionType
    dict_type = issubclass(OutputType, (dict, Mapping))
    attributes = intermediaries[0].keys() if dict_type else vars(intermediaries[0])
    for attr in attributes:
        annotation = annotations.get(attr, None)
        batch_values = [batch.get(attr) if dict_type else getattr(batch, attr) for batch in intermediaries]
        value = batch_values[0]
        if value is None:
            concatenated_values = None
        elif isinstance(value, (tf.experimental.ExtensionType, dict, Mapping, SimpleNamespace)):
            concatenated_values = concat_intermediaries(batch_values, axis=axis, axis_type=axis_type)
        else:
            concatenated_values = _concatenate_intermediaries_leaf(
                annotation, batch_values, value, axis, axis_type, attr
            )
        concated_intermediaries[attr] = concatenated_values

    return OutputType(**concated_intermediaries)

gather(output, indices, axis=0, axis_type=Axis.Batch)

Gathers slices from different params in output according to indices. NOTE: this can lead to unexpected behaviour when tensors are not annotated as the axis might be available but it's not meant to be gathered.

Parameters:

Name Type Description Default
output list[ExtensionType]

tf.experimental.ExtensionType type instance.

required
indices NDArray[int32]

The index Tensor. Must be one of the following types: int32, int64. The values must be in range [0, params.shape[axis]).

required
axis int

The axis where we need to concatenate. Defaults to 0 which is assumed to be batch.

0
axis_type(Axis, optional

The annotated axis where we need to concatenate. Defaults to Axis.Batch.

required

Returns:

Type Description
T

tf.experimental.ExtensionType: The gathered output which is the same type that is passed in.

Source code in wt_ml/tuning/utils.py
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
@in_cpu
def gather(
    output: T,
    indices: NDArray[np.int_],
    axis: int = 0,
    axis_type: Axis = Axis.Batch,
) -> T:
    """
    Gathers slices from different params in `output` according to `indices`.
    NOTE: this can lead to unexpected behaviour when tensors are not annotated as the `axis` might be available but
    it's not meant to be gathered.

    Args:
        output (list[tf.experimental.ExtensionType]): tf.experimental.ExtensionType type instance.
        indices (NDArray[np.int32]): The index Tensor. Must be one of the following types: int32, int64.
            The values must be in range [0, params.shape[axis]).
        axis (int, optional): The axis where we need to concatenate. Defaults to 0 which is assumed to be batch.
        axis_type(Axis, optional): The annotated axis where we need to concatenate. Defaults to Axis.Batch.

    Returns:
        tf.experimental.ExtensionType: The gathered output which is the same type that is passed in.
    """
    OutputType = type(output)
    # an AnnotatedExtensionType will have `annotations` to handle recursion
    annotations: dict[str, Any] = getattr(OutputType, "annotations", getattr(OutputType, "__annotations__", {}))
    gathered_output = {}
    # ExtensionType supports Mapping, so this detects that for us. else we assume its an ExtensionType
    dict_type = issubclass(OutputType, (dict, Mapping))

    if dict_type:
        output_dict = output
    elif isinstance(output, tf.experimental.ExtensionType):
        output_dict = tf.experimental.extension_type.as_dict(output)
    else:
        output_dict = dict(vars(output))

    for attr, values in output_dict.items():
        annotation = annotations.get(attr, None)
        gathered_values = _get_gathered_values(annotation, values, indices, axis, axis_type)

        gathered_output[attr] = gathered_values

    return OutputType(**gathered_output)

get_index(level, inputs, encodings)

For given level, pd.Index | pd.MultiIndex is created.

Parameters:

Name Type Description Default
level tuple[str, ...] | str | None

The level(s) on which we need to create the index from.

required
inputs EconomicModelInput

Inputs intermediearies that contains the level(s) index values.

required
encodings dict[str, dict[str, int]]

Encodings dict which will be used to decode the index values.

required

Returns:

Type Description
Index | MultiIndex

pd.Index | pd.MultiIndex: The decoded index created based on level(s).

Source code in wt_ml/tuning/utils.py
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
def get_index(
    level: tuple[str, ...] | str | None,
    inputs: EconomicModelInput,
    encodings: Encodings,
) -> pd.Index | pd.MultiIndex:
    """For given `level`, pd.Index | pd.MultiIndex is created.

    Args:
        level (tuple[str, ...] | str | None): The level(s) on which we need to create the index from.
        inputs (EconomicModelInput): Inputs intermediearies that contains the level(s) index values.
        encodings (dict[str, dict[str, int]]): Encodings dict which will be used to decode the index values.

    Returns:
        pd.Index | pd.MultiIndex: The decoded index created based on level(s).
    """
    if level is None:
        level = ("brand", "wholesaler")

    if isinstance(level, str) and (level == "country"):
        # NOTE: add 'country' in encodings so that this can be generalized as well.
        group_index = pd.Index(["US" for _ in range(len(inputs.brand_index))], name="country")
    elif isinstance(level, str):
        group_index = pd.Index(getattr(inputs, f"{level}_index"), name=level)
    elif isinstance(level, (tuple, list)):
        group_index = pd.MultiIndex.from_arrays(
            [np.array(getattr(inputs, f"{lvl}_index")) for lvl in level], names=level
        )
    else:
        raise ValueError(f"Invalid level {level}")

    if level != "country":
        # NOTE: add 'country' in encodings so that this can be generalized as well.
        group_index = _index_mapper(group_index, encodings)
    return group_index

get_indexer(index, target)

Compute indexer and mask for new index given the current index. The indexer should be then used as an input to ndarray.take to align the current data to the new index. This is based on pd.Index.get_indexer. Pandas implementation doesn't support if the index is not unique.

Parameters:

Name Type Description Default
index MultiIndex | Index

Current index.

required
target Sequence[tuple | Hashable]

New target index, we like to compute indexes on current index.

required

Returns:

Type Description
NDArray[intp]

NDArray[np.intp]: Integers from 0 to n - 1 indicating that the index at these positions matches the corresponding target values. Missing values in the target are marked by -1.

Source code in wt_ml/tuning/utils.py
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
def get_indexer(index: pd.MultiIndex | pd.Index, target: Sequence[tuple | Hashable]) -> NDArray[np.intp]:
    """
    Compute indexer and mask for new index given the current index.
    The indexer should be then used as an input to ndarray.take to align the current data to the new index.
    This is based on pd.Index.get_indexer. Pandas implementation doesn't support if the `index` is not unique.

    Args:
        index (pd.MultiIndex | pd.Index): Current index.
        target (Sequence[tuple | Hashable]): New target index, we like to compute indexes on current index.

    Returns:
        NDArray[np.intp]: Integers from 0 to n - 1 indicating that the index at these positions matches the
            corresponding target values. Missing values in the target are marked by -1.
    """
    index_mapping = defaultdict(lambda: -1, zip(target, range(len(target))))
    return index.map(index_mapping).values.astype(np.intp)

process_period_curves_diff(differences)

Processes the differences calculated for each period and curves.

Source code in wt_ml/tuning/utils.py
118
119
120
121
122
123
124
def process_period_curves_diff(differences: list[dict[str, NDArray]]) -> dict[str, NDArray]:
    """Processes the differences calculated for each period and curves."""
    metrics = defaultdict(list)
    for period_curves in differences:
        for curve_name, value in period_curves.items():
            metrics[curve_name].append(value)
    return {curve_name: _process_period_curve(period_curves) for curve_name, period_curves in metrics.items()}

sort_batch_index(output, encodings, level=('brand', 'wholesaler'), axis=0, axis_type=Axis.Batch)

Sorts the given intermediaries on the batch axis, based on level indexes. The sort order is based on the level(s) provided. This is useful when we need to have the outputs in the same order.

Parameters:

Name Type Description Default
output EconomicIntermediaries | dict[str, EconomicIntermediaries]

Outputs of the model.

required
encodings dict[str, dict[str, int]]

Encodings dict which will be used to decode the index values.

required
level tuple[str, ...]

The level(s) on which the batches is ordered. Defaults to ("brand", "wholesaler").

('brand', 'wholesaler')
axis int

The batch axis. Defaults to 0 which is assumed to be batch.

0
axis_type(Axis, optional

The annotated batch axis. Defaults to Axis.Batch.

required

Returns:

Type Description
EconomicIntermediaries | dict[str, EconomicIntermediaries]

EconomicIntermediaries | dict[str, EconomicIntermediaries]: The sorted intermediares based on level(s).

Source code in wt_ml/tuning/utils.py
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
@in_cpu
def sort_batch_index(
    output: EconomicIntermediaries | dict[str, EconomicIntermediaries],
    encodings: Encodings,
    level: tuple[str, ...] = ("brand", "wholesaler"),
    axis: int = 0,
    axis_type: Axis = Axis.Batch,
) -> EconomicIntermediaries | dict[str, EconomicIntermediaries]:
    """
    Sorts the given intermediaries on the batch axis, based on level indexes.
    The sort order is based on the level(s) provided. This is useful when we need to have the outputs in the same order.

    Args:
        output (EconomicIntermediaries | dict[str, EconomicIntermediaries]): Outputs of the model.
        encodings (dict[str, dict[str, int]]): Encodings dict which will be used to decode the index values.
        level (tuple[str, ...], optional): The level(s) on which the batches is ordered.
            Defaults to ("brand", "wholesaler").
        axis (int, optional): The batch axis. Defaults to 0 which is assumed to be batch.
        axis_type(Axis, optional): The annotated batch axis. Defaults to Axis.Batch.

    Returns:
        EconomicIntermediaries | dict[str, EconomicIntermediaries]: The sorted intermediares based on level(s).
    """
    if isinstance(output, dict):
        return {
            key: sort_batch_index(
                output[key],
                encodings=encodings,
                level=level,
                axis=axis,
                axis_type=axis_type,
            )
            for key in output.keys()
        }

    if TYPE_CHECKING:
        assert isinstance(output, EconomicIntermediaries)
        assert output.inputs is not None
    index = get_index(level=level, inputs=output.inputs, encodings=encodings)
    sorted_idx = tf.convert_to_tensor(index.argsort(), dtype=tf.int32)

    return gather(output, sorted_idx, axis=axis, axis_type=axis_type)