map_signals(encodings, last_intermediaries, collapse_level=DEFAULT_COLLAPSE_LEVEL, collapse_lead_lags=False)

Collapse level 1: collapse distribution, economic, weather, all lead/lags, price signals PINC, ROI curves into parent vehicle Collapse 2: holiday all into 1, pinc merges iwth pricing TODO: add level for merging lead/lags but keeping individual signals (temp vs precipitation) (become lvl 1 and increment others)

Source code in wt_ml/output/utils/viz_utils.py
308
309
310
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
354
355
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
def map_signals(
    encodings: dict[str, Any],
    last_intermediaries: dict[str, EconomicIntermediaries] | EconomicIntermediaries,
    collapse_level: CollapseLevel | dict[str, CollapseLevel] = DEFAULT_COLLAPSE_LEVEL,
    collapse_lead_lags: bool = False,
) -> dict[str, str]:
    """
    Collapse level 1: collapse distribution, economic, weather, all lead/lags, price signals
    PINC, ROI curves into parent vehicle
    Collapse 2: holiday all into 1, pinc merges iwth pricing
    TODO: add level for merging lead/lags but keeping individual signals (temp vs precipitation)
    (become lvl 1 and increment others)
    """
    intermediaries = (
        last_intermediaries[sorted(last_intermediaries.keys())[0]]
        if isinstance(last_intermediaries, Mapping)
        else last_intermediaries
    )
    mappings = {k: k for k in get_signal_names(intermediaries)}
    # renaming to make it look nicer in plot
    mappings |= {
        "coupons": "Coupons",
        "coupon_decayed": "Coupons Decayed",
        "baseline": "Baseline",
        "national_trend": "National Category Trend",
        "regional_trend": "Regional Category Trend",
        "Festivals": "Festivals",
        "pre_investment": "Missing Investment Adjustment",
        "covid": "COVID-19",
        "Supply Chain Issues": "Supply Chain Issues",
        "bud_light_event": "Bud Light Event",
        "drop_hold": "Natty Supply Chain Drop",
    }
    if collapse_level == 0:
        return map_lead_lags(intermediaries) if collapse_lead_lags else mappings

    if isinstance(collapse_level, int):
        collapse_level = CollapseDict.with_default(collapse_level)
    else:
        collapse_level = CollapseDict(collapse_level)

    parent_vehicle_indices = getattr(intermediaries.inputs, "parent_vehicle_index", None)
    parent_names = get_lookups(encodings, "parent_vehicle")
    vehicle_names = get_lookups(encodings, "vehicle")
    assert parent_vehicle_indices is not None
    assert parent_names is not None
    assert vehicle_names is not None

    econ_signals = encodings["economic_signals"]
    state_econ_signals = encodings["state_signals"]
    weather_signals = encodings["weather_signals"]
    temperature_signals = encodings["temperature_signals"]

    SEASONALITY = "Seasonality" if collapse_level["holiday_me"] < 2 else "Holidays / Seasonality"
    holiday_mappings = {
        holiday: (holiday, "Holidays", SEASONALITY)[min(collapse_level["holiday_me"], 2)]
        for holiday in set(
            sanitize_lagged_name(k)
            for k in to_signal_names(getattr(intermediaries.impacts.holiday_me, "signal_names", []))
        )
    } | {
        k: (k, "Time of Year", SEASONALITY)[min(collapse_level["periodic_me"], 2)]
        for k in to_signal_names(getattr(intermediaries.impacts.periodic_me, "signal_names", []))
    }
    # NOTE: cannot find this signal name. perhaps 'week_name' is deprecated?
    if collapse_level["week_name"]:
        holiday_mappings["week_name"] = SEASONALITY

    price_me_mapping = (
        {
            price_signal: "Price"
            for price_signal in set(
                sanitize_lagged_name(k)
                for k in to_signal_names(getattr(intermediaries.impacts.pricing_lead_lag_me, "signal_names", []))
            )
        }
        if collapse_level["pricing_lead_lag_me"]
        else {}
    )

    if parent_vehicle_indices is not None and collapse_level["vehicle"]:
        mappings |= {vname: parent_names[pidx] for pidx, vname in zip(parent_vehicle_indices, vehicle_names)} | {
            f"{vname}_decayed": f"{parent_names[pidx]} Decayed" if collapse_level["vehicle"] < 2 else parent_names[pidx]
            for pidx, vname in zip(parent_vehicle_indices, vehicle_names)
        }

    if collapse_level["economic_signals"]:
        mappings |= {s: "Economic" for s in econ_signals}

    if collapse_level["state_economic_signals"]:
        mappings |= {s: "State Economic" for s in state_econ_signals}

    if collapse_level["trend"] > 1:
        mappings |= {s: "Category Trend" for s in ["national_trend", "regional_trend"]}

    if collapse_level["temperature_signals"]:
        mappings |= {
            s: "Temperature" if collapse_level["temperature_signals"] < 2 else "Weather" for s in temperature_signals
        }

    if collapse_level["weather_signals"]:
        mappings |= {s: "Weather" for s in weather_signals}

    if collapse_level["pricing"]:
        mappings |= {
            price_signal: "Price"
            for price_signal in to_signal_names(getattr(intermediaries.impacts.pricing, "signal_names", []))
        }

    if collapse_level["price_ratio"]:
        mappings |= {
            price_ratio: (price_ratio, "Price Ratio", "Price")[min(collapse_level["price_ratio"], 2)]
            for price_ratio in to_signal_names(getattr(intermediaries.impacts.price_ratio, "signal_names", []))
        }

    if collapse_level["distribution"]:
        mappings |= {
            s: "Distribution" for s in to_signal_names(getattr(intermediaries.impacts.distribution, "signal_names", []))
        }

    if collapse_level["coupons"] >= 2:
        mappings |= {
            "coupons": "Price",
            "coupon_decayed": "Price",
        }

    # to_signal_names always returns with ("baseline", "Festivals", "Supply Chain Issues")
    # replacing them so they don't accidentally get matched with some other group signal.
    ensure_naming = {s: s.title() for s in ("baseline", "Festivals", "Supply Chain Issues")}
    mappings |= holiday_mappings | price_me_mapping | ensure_naming

    if collapse_lead_lags:
        mappings = compose_mapping(map_lead_lags(intermediaries), mappings)
        if TYPE_CHECKING:
            assert isinstance(mappings, dict)
    return mappings

sanitize_lagged_name(signal_name)

Given a signal name with lead lag suffix return just the signal name

Parameters:

Name Type Description Default
signal_name str

A signal ending with r"[+-]\d+"

required

Returns:

Name Type Description
str str

The signal name without the suffix

Source code in wt_ml/output/utils/viz_utils.py
278
279
280
281
282
283
284
285
286
287
def sanitize_lagged_name(signal_name: str) -> str:
    r"""Given a signal name with lead lag suffix return just the signal name

    Args:
        signal_name (str): A signal ending with r"[+-]\d+"

    Returns:
        str: The signal  name without the suffix
    """
    return "_".join(signal_name.split("_")[:-1])

sanitize_prefixed_lagged_name(signal_name)

Given a signal name with lead lag suffix return just the signal name

Parameters:

Name Type Description Default
signal_name str

A signal starting with r"[^]+" and ending with r"[+-]\d+"

required

Returns:

Name Type Description
str str

The signal name without the prefix or suffix

Source code in wt_ml/output/utils/viz_utils.py
266
267
268
269
270
271
272
273
274
275
def sanitize_prefixed_lagged_name(signal_name: str) -> str:
    r"""Given a signal name with lead lag suffix return just the signal name

    Args:
        signal_name (str): A signal starting with r"[^_]+_" and ending with r"[+-]\d+"

    Returns:
        str: The signal name without the prefix or suffix
    """
    return "_".join(signal_name.split("_")[1:-1])