From 8e193dc6bc09ba378e59366f88bec0b0e972baba Mon Sep 17 00:00:00 2001 From: Ricardo Vieira Date: Fri, 24 Nov 2023 11:38:08 +0100 Subject: [PATCH 1/3] Remove deprecated model methods --- pymc/model/core.py | 40 ---------------------------------------- tests/model/test_core.py | 20 ++++---------------- 2 files changed, 4 insertions(+), 56 deletions(-) diff --git a/pymc/model/core.py b/pymc/model/core.py index 4a0ff75733..dc03728c72 100644 --- a/pymc/model/core.py +++ b/pymc/model/core.py @@ -886,27 +886,11 @@ def unobserved_value_vars(self): return vars + untransformed_vars + deterministics - @property - def disc_vars(self): - warnings.warn( - "Model.disc_vars has been deprecated. Use Model.discrete_value_vars instead.", - FutureWarning, - ) - return self.discrete_value_vars - @property def discrete_value_vars(self): """All the discrete value variables in the model""" return list(typefilter(self.value_vars, discrete_types)) - @property - def cont_vars(self): - warnings.warn( - "Model.cont_vars has been deprecated. Use Model.continuous_value_vars instead.", - FutureWarning, - ) - return self.continuous_value_vars - @property def continuous_value_vars(self): """All the continuous value variables in the model""" @@ -935,18 +919,6 @@ def unobserved_RVs(self): """ return self.free_RVs + self.deterministics - @property - def RV_dims(self) -> Dict[str, Tuple[Union[str, None], ...]]: - """Tuples of dimension names for specific model variables. - - Entries in the tuples may be ``None``, if the RV dimension was not given a name. - """ - warnings.warn( - "Model.RV_dims is deprecated. Use Model.named_vars_to_dims instead.", - FutureWarning, - ) - return self.named_vars_to_dims - @property def coords(self) -> Dict[str, Union[Tuple, None]]: """Coordinate values for model dimensions.""" @@ -1090,18 +1062,6 @@ def initial_point(self, random_seed: SeedSequenceSeed = None) -> Dict[str, np.nd fn = make_initial_point_fn(model=self, return_transformed=True) return Point(fn(random_seed), model=self) - @property - def initial_values(self) -> Dict[TensorVariable, Optional[Union[np.ndarray, Variable, str]]]: - """Maps transformed variables to initial value placeholders. - - Keys are the random variables (as returned by e.g. ``pm.Uniform()``) and - values are the numeric/symbolic initial values, strings denoting the strategy to get them, or None. - """ - warnings.warn( - "Model.initial_values is deprecated. Use Model.rvs_to_initial_values instead." - ) - return self.rvs_to_initial_values - def set_initval(self, rv_var, initval): """Sets an initial value (strategy) for a random variable.""" if initval is not None and not isinstance(initval, (Variable, str)): diff --git a/tests/model/test_core.py b/tests/model/test_core.py index 2f4b1ae49a..97401bda09 100644 --- a/tests/model/test_core.py +++ b/tests/model/test_core.py @@ -705,9 +705,9 @@ def test_set_initval(): alpha = pm.HalfNormal("alpha", initval=100) value = pm.NegativeBinomial("value", mu=mu, alpha=alpha) - assert np.array_equal(model.initial_values[mu], np.array([[100.0]])) - np.testing.assert_array_equal(model.initial_values[alpha], np.array(100)) - assert model.initial_values[value] is None + assert np.array_equal(model.rvs_to_initial_values[mu], np.array([[100.0]])) + np.testing.assert_array_equal(model.rvs_to_initial_values[alpha], np.array(100)) + assert model.rvs_to_initial_values[value] is None # `Flat` cannot be sampled, so let's make sure that doesn't break initial # value computations @@ -715,7 +715,7 @@ def test_set_initval(): x = pm.Flat("x") y = pm.Normal("y", x, 1) - assert y in model.initial_values + assert y in model.rvs_to_initial_values def test_datalogp_multiple_shapes(): @@ -974,18 +974,6 @@ def test_set_data_constant_shape_error(): pmodel.set_data("y", np.arange(10)) -def test_model_deprecation_warning(): - with pm.Model() as m: - x = pm.Normal("x", 0, 1, size=2) - y = pm.LogNormal("y", 0, 1, size=2) - - with pytest.warns(FutureWarning): - m.disc_vars - - with pytest.warns(FutureWarning): - m.cont_vars - - @pytest.mark.parametrize("jacobian", [True, False]) def test_model_logp(jacobian): with pm.Model() as m: From af8cf19916c7e644f1f084f79f5f37832dca86b9 Mon Sep 17 00:00:00 2001 From: Ricardo Vieira Date: Fri, 24 Nov 2023 11:31:02 +0100 Subject: [PATCH 2/3] Deprecate Model.model property --- pymc/model/core.py | 7 ++++--- tests/model/test_core.py | 7 +++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/pymc/model/core.py b/pymc/model/core.py index dc03728c72..cfc331937a 100644 --- a/pymc/model/core.py +++ b/pymc/model/core.py @@ -559,6 +559,7 @@ def __init__( @property def model(self): + warnings.warn("Model.model property is deprecated. Just use Model.", FutureWarning) return self @property @@ -629,7 +630,7 @@ def compile_logp( Whether to sum all logp terms or return elemwise logp for each variable. Defaults to True. """ - return self.model.compile_fn(self.logp(vars=vars, jacobian=jacobian, sum=sum)) + return self.compile_fn(self.logp(vars=vars, jacobian=jacobian, sum=sum)) def compile_dlogp( self, @@ -646,7 +647,7 @@ def compile_dlogp( jacobian: Whether to include jacobian terms in logprob graph. Defaults to True. """ - return self.model.compile_fn(self.dlogp(vars=vars, jacobian=jacobian)) + return self.compile_fn(self.dlogp(vars=vars, jacobian=jacobian)) def compile_d2logp( self, @@ -663,7 +664,7 @@ def compile_d2logp( jacobian: Whether to include jacobian terms in logprob graph. Defaults to True. """ - return self.model.compile_fn(self.d2logp(vars=vars, jacobian=jacobian)) + return self.compile_fn(self.d2logp(vars=vars, jacobian=jacobian)) def logp( self, diff --git a/tests/model/test_core.py b/tests/model/test_core.py index 97401bda09..38d2b27111 100644 --- a/tests/model/test_core.py +++ b/tests/model/test_core.py @@ -1107,6 +1107,13 @@ def test_model_pytensor_config(): assert pytensor.config.mode != "JAX" +def test_deprecated_model_property(): + m = pm.Model() + with pytest.warns(FutureWarning, match="Model.model property is deprecated"): + m_property = m.model + assert m is m_property + + def test_model_parent_set_programmatically(): with pm.Model() as model: x = pm.Normal("x") From e76ccb45b916349b4d27ccf9728a8f5bc66c39cb Mon Sep 17 00:00:00 2001 From: Ricardo Vieira Date: Fri, 24 Nov 2023 11:37:18 +0100 Subject: [PATCH 3/3] Deprecate pytensor_config --- pymc/model/core.py | 8 +++++++- tests/model/test_core.py | 4 +++- tests/sampling/test_mcmc.py | 8 -------- tests/variational/test_approximations.py | 4 ---- 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/pymc/model/core.py b/pymc/model/core.py index cfc331937a..d8779ab89e 100644 --- a/pymc/model/core.py +++ b/pymc/model/core.py @@ -496,7 +496,13 @@ def __new__(cls, *args, **kwargs): instance._parent = kwargs.get("model") else: instance._parent = cls.get_context(error_if_none=False) - instance._pytensor_config = kwargs.get("pytensor_config", {}) + pytensor_config = kwargs.get("pytensor_config", {}) + if pytensor_config: + warnings.warn( + "pytensor_config is deprecated. Use pytensor.config or pytensor.config.change_flags context manager instead.", + FutureWarning, + ) + instance._pytensor_config = pytensor_config return instance @staticmethod diff --git a/tests/model/test_core.py b/tests/model/test_core.py index 38d2b27111..715836e24b 100644 --- a/tests/model/test_core.py +++ b/tests/model/test_core.py @@ -1102,7 +1102,9 @@ def test_compile_fn(): def test_model_pytensor_config(): assert pytensor.config.mode != "JAX" - with pm.Model(pytensor_config=dict(mode="JAX")) as model: + with pytest.warns(FutureWarning, match="pytensor_config is deprecated"): + m = pm.Model(pytensor_config=dict(mode="JAX")) + with m: assert pytensor.config.mode == "JAX" assert pytensor.config.mode != "JAX" diff --git a/tests/sampling/test_mcmc.py b/tests/sampling/test_mcmc.py index ef13ae2c19..4535d87cb7 100644 --- a/tests/sampling/test_mcmc.py +++ b/tests/sampling/test_mcmc.py @@ -797,14 +797,6 @@ def test_step_vars_in_model(self): class TestType: samplers = (Metropolis, Slice, HamiltonianMC, NUTS) - def setup_method(self): - # save PyTensor config object - self.pytensor_config = copy(pytensor.config) - - def teardown_method(self): - # restore pytensor config - pytensor.config = self.pytensor_config - @pytensor.config.change_flags({"floatX": "float64", "warn_float64": "ignore"}) def test_float64(self): with pm.Model() as model: diff --git a/tests/variational/test_approximations.py b/tests/variational/test_approximations.py index c1482ed8eb..97c2a56805 100644 --- a/tests/variational/test_approximations.py +++ b/tests/variational/test_approximations.py @@ -84,10 +84,6 @@ def test_scale_cost_to_minibatch_works(aux_total_size): y_obs = np.array([1.6, 1.4]) beta = len(y_obs) / float(aux_total_size) - # TODO: pytensor_config - # with pm.Model(pytensor_config=dict(floatX='float64')): - # did not not work as expected - # there were some numeric problems, so float64 is forced with pytensor.config.change_flags(floatX="float64", warn_float64="ignore"): assert pytensor.config.floatX == "float64" assert pytensor.config.warn_float64 == "ignore"