|
| 1 | +# Copyright 2022 - 2026 The PyMC Labs Developers |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Compatibility tests for optional pymc-marketing components in pymc_models.""" |
| 15 | + |
| 16 | +import pytensor.tensor as pt |
| 17 | + |
| 18 | +from causalpy.pymc_models import ( |
| 19 | + _call_seasonality_component_apply, |
| 20 | + _call_time_component_apply, |
| 21 | + _uses_xtensor_api, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +def test_call_seasonality_component_apply_supports_tensor_input(): |
| 26 | + calls: dict[str, object] = {} |
| 27 | + |
| 28 | + class FakeSeasonalityComponent: |
| 29 | + def apply(self, dayofperiod, result_callback=None): # noqa: ANN001, ARG002 |
| 30 | + calls["dayofperiod"] = dayofperiod |
| 31 | + return "seasonality-result" |
| 32 | + |
| 33 | + signal = pt.vector("signal") |
| 34 | + |
| 35 | + result = _call_seasonality_component_apply(FakeSeasonalityComponent(), signal) |
| 36 | + |
| 37 | + assert result == "seasonality-result" |
| 38 | + assert calls == {"dayofperiod": signal} |
| 39 | + |
| 40 | + |
| 41 | +def test_call_seasonality_component_apply_supports_xtensor_signature(): |
| 42 | + calls: dict[str, object] = {} |
| 43 | + |
| 44 | + class FakeXTensorResult: |
| 45 | + def __init__(self, values: str) -> None: |
| 46 | + self.values = values |
| 47 | + |
| 48 | + class FakeSeasonalityComponent: |
| 49 | + def apply(self, dayofperiod, sum=True): # noqa: ANN001, FBT002 |
| 50 | + calls["has_dims"] = hasattr(dayofperiod.type, "dims") |
| 51 | + calls["sum"] = sum |
| 52 | + return FakeXTensorResult("xtensor-result") |
| 53 | + |
| 54 | + result = _call_seasonality_component_apply( |
| 55 | + FakeSeasonalityComponent(), |
| 56 | + pt.vector("signal"), |
| 57 | + ) |
| 58 | + |
| 59 | + assert result == "xtensor-result" |
| 60 | + assert calls == { |
| 61 | + "has_dims": True, |
| 62 | + "sum": True, |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | +def test_call_time_component_apply_supports_tensor_input(): |
| 67 | + calls: dict[str, object] = {} |
| 68 | + |
| 69 | + class FakeTimeComponent: |
| 70 | + def apply(self, t): # noqa: ANN001 |
| 71 | + calls["t"] = t |
| 72 | + return "time-result" |
| 73 | + |
| 74 | + signal = pt.vector("signal") |
| 75 | + |
| 76 | + result = _call_time_component_apply(FakeTimeComponent(), signal) |
| 77 | + |
| 78 | + assert result == "time-result" |
| 79 | + assert calls == {"t": signal} |
| 80 | + |
| 81 | + |
| 82 | +def test_call_time_component_apply_supports_xtensor_source(): |
| 83 | + calls: dict[str, object] = {} |
| 84 | + |
| 85 | + class FakeXTensorResult: |
| 86 | + def __init__(self, values: str) -> None: |
| 87 | + self.values = values |
| 88 | + |
| 89 | + class FakeTimeComponent: |
| 90 | + def apply(self, t): # noqa: ANN001 |
| 91 | + # as_xtensor compatibility path |
| 92 | + calls["has_dims"] = hasattr(t.type, "dims") |
| 93 | + return FakeXTensorResult("xtensor-time-result") |
| 94 | + |
| 95 | + result = _call_time_component_apply(FakeTimeComponent(), pt.vector("signal")) |
| 96 | + |
| 97 | + assert result == "xtensor-time-result" |
| 98 | + assert calls == {"has_dims": True} |
| 99 | + |
| 100 | + |
| 101 | +def test_uses_xtensor_api_falls_back_to_code_names(monkeypatch): |
| 102 | + namespace: dict[str, object] = {} |
| 103 | + exec( # noqa: S102 |
| 104 | + "def fake_transform(x):\n return as_xtensor(x)\n", |
| 105 | + {}, |
| 106 | + namespace, |
| 107 | + ) |
| 108 | + fake_transform = namespace["fake_transform"] |
| 109 | + |
| 110 | + def raise_oserror(_function): # noqa: ANN001 |
| 111 | + raise OSError("source unavailable") |
| 112 | + |
| 113 | + monkeypatch.setattr("causalpy.pymc_models.inspect.getsource", raise_oserror) |
| 114 | + |
| 115 | + assert _uses_xtensor_api(fake_transform) |
0 commit comments