Skip to content

Commit 24888ac

Browse files
authored
Remove duplicate methods (#6291)
* remove dups, add type hints, and docstrings * replacing references to old code * reorder and reword * deprecate the "test_point" method * fix NameError. got to work locally
1 parent d36bed3 commit 24888ac

File tree

4 files changed

+47
-83
lines changed

4 files changed

+47
-83
lines changed

pymc/model.py

Lines changed: 34 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
from pymc.aesaraf import (
5353
PointFunc,
54+
SeedSequenceSeed,
5455
compile_pymc,
5556
convert_observed_data,
5657
gradient,
@@ -986,43 +987,6 @@ def dim_lengths(self) -> Dict[str, Variable]:
986987
"""
987988
return self._dim_lengths
988989

989-
@property
990-
def test_point(self) -> Dict[str, np.ndarray]:
991-
"""Deprecated alias for `Model.initial_point(seed=None)`."""
992-
warnings.warn(
993-
"`Model.test_point` has been deprecated. Use `Model.initial_point(seed=None)`.",
994-
FutureWarning,
995-
)
996-
return self.initial_point()
997-
998-
def initial_point(self, seed=None) -> Dict[str, np.ndarray]:
999-
"""Computes the initial point of the model.
1000-
1001-
Returns
1002-
-------
1003-
ip : dict
1004-
Maps names of transformed variables to numeric initial values in the transformed space.
1005-
"""
1006-
fn = make_initial_point_fn(model=self, return_transformed=True)
1007-
return Point(fn(seed), model=self)
1008-
1009-
@property
1010-
def initial_values(self) -> Dict[TensorVariable, Optional[Union[np.ndarray, Variable, str]]]:
1011-
"""Maps transformed variables to initial value placeholders.
1012-
1013-
Keys are the random variables (as returned by e.g. ``pm.Uniform()``) and
1014-
values are the numeric/symbolic initial values, strings denoting the strategy to get them, or None.
1015-
"""
1016-
return self._initial_values
1017-
1018-
def set_initval(self, rv_var, initval):
1019-
"""Sets an initial value (strategy) for a random variable."""
1020-
if initval is not None and not isinstance(initval, (Variable, str)):
1021-
# Convert scalars or array-like inputs to ndarrays
1022-
initval = rv_var.type.filter(initval)
1023-
1024-
self.initial_values[rv_var] = initval
1025-
1026990
def shape_from_dims(self, dims):
1027991
shape = []
1028992
if len(set(dims)) != len(dims):
@@ -1137,6 +1101,39 @@ def set_dim(self, name: str, new_length: int, coord_values: Optional[Sequence] =
11371101
self.dim_lengths[name].set_value(new_length)
11381102
return
11391103

1104+
def initial_point(self, random_seed: SeedSequenceSeed = None) -> Dict[str, np.ndarray]:
1105+
"""Computes the initial point of the model.
1106+
1107+
Parameters
1108+
----------
1109+
random_seed : SeedSequenceSeed, default None
1110+
Seed(s) for generating initial point from the model. Passed into :func:`pymc.aesaraf.reseed_rngs`
1111+
1112+
Returns
1113+
-------
1114+
ip : dict of {str : array_like}
1115+
Maps names of transformed variables to numeric initial values in the transformed space.
1116+
"""
1117+
fn = make_initial_point_fn(model=self, return_transformed=True)
1118+
return Point(fn(random_seed), model=self)
1119+
1120+
@property
1121+
def initial_values(self) -> Dict[TensorVariable, Optional[Union[np.ndarray, Variable, str]]]:
1122+
"""Maps transformed variables to initial value placeholders.
1123+
1124+
Keys are the random variables (as returned by e.g. ``pm.Uniform()``) and
1125+
values are the numeric/symbolic initial values, strings denoting the strategy to get them, or None.
1126+
"""
1127+
return self._initial_values
1128+
1129+
def set_initval(self, rv_var, initval):
1130+
"""Sets an initial value (strategy) for a random variable."""
1131+
if initval is not None and not isinstance(initval, (Variable, str)):
1132+
# Convert scalars or array-like inputs to ndarrays
1133+
initval = rv_var.type.filter(initval)
1134+
1135+
self.initial_values[rv_var] = initval
1136+
11401137
def set_data(
11411138
self,
11421139
name: str,
@@ -1259,34 +1256,6 @@ def set_data(
12591256

12601257
shared_object.set_value(values)
12611258

1262-
def initial_point(self, seed=None) -> Dict[str, np.ndarray]:
1263-
"""Computes the initial point of the model.
1264-
1265-
Returns
1266-
-------
1267-
ip : dict
1268-
Maps names of transformed variables to numeric initial values in the transformed space.
1269-
"""
1270-
fn = make_initial_point_fn(model=self, return_transformed=True)
1271-
return Point(fn(seed), model=self)
1272-
1273-
@property
1274-
def initial_values(self) -> Dict[TensorVariable, Optional[Union[np.ndarray, Variable, str]]]:
1275-
"""Maps transformed variables to initial value placeholders.
1276-
1277-
Keys are the random variables (as returned by e.g. ``pm.Uniform()``) and
1278-
values are the numeric/symbolic initial values, strings denoting the strategy to get them, or None.
1279-
"""
1280-
return self._initial_values
1281-
1282-
def set_initval(self, rv_var, initval):
1283-
"""Sets an initial value (strategy) for a random variable."""
1284-
if initval is not None and not isinstance(initval, (Variable, str)):
1285-
# Convert scalars or array-like inputs to ndarrays
1286-
initval = rv_var.type.filter(initval)
1287-
1288-
self.initial_values[rv_var] = initval
1289-
12901259
def register_rv(
12911260
self, rv_var, name, data=None, total_size=None, dims=None, transform=UNSET, initval=None
12921261
):

pymc/smc/kernels.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def _initialize_kernel(self):
198198
199199
"""
200200
# Create dictionary that stores original variables shape and size
201-
initial_point = self.model.initial_point(seed=self.rng.integers(2**30))
201+
initial_point = self.model.initial_point(random_seed=self.rng.integers(2**30))
202202
for v in self.variables:
203203
self.var_info[v.name] = (initial_point[v.name].shape, initial_point[v.name].size)
204204
# Create particles bijection map

pymc/tests/test_initial_point.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_new_warnings(self):
4747
with pm.Model() as pmodel:
4848
with pytest.warns(FutureWarning, match="`testval` argument is deprecated"):
4949
rv = pm.Uniform("u", 0, 1, testval=0.75)
50-
initial_point = pmodel.initial_point(seed=0)
50+
initial_point = pmodel.initial_point(random_seed=0)
5151
assert initial_point["u_interval__"] == transform_fwd(rv, 0.75, model=pmodel)
5252
assert not hasattr(rv.tag, "test_value")
5353
pass
@@ -56,7 +56,7 @@ def test_valid_string_strategy(self):
5656
with pm.Model() as pmodel:
5757
pm.Uniform("x", 0, 1, size=2, initval="unknown")
5858
with pytest.raises(ValueError, match="Invalid string strategy: unknown"):
59-
pmodel.initial_point(seed=0)
59+
pmodel.initial_point(random_seed=0)
6060

6161

6262
class TestInitvalEvaluation:
@@ -79,15 +79,15 @@ def test_dependent_initvals(self):
7979
U = pm.Uniform("U", lower=9, upper=10, initval=9.5)
8080
B1 = pm.Uniform("B1", lower=L, upper=U, initval=5)
8181
B2 = pm.Uniform("B2", lower=L, upper=U, initval=(L + U) / 2)
82-
ip = pmodel.initial_point(seed=0)
82+
ip = pmodel.initial_point(random_seed=0)
8383
assert ip["L_interval__"] == 0
8484
assert ip["U_interval__"] == 0
8585
assert ip["B1_interval__"] == 0
8686
assert ip["B2_interval__"] == 0
8787

8888
# Modify initval of L and re-evaluate
8989
pmodel.initial_values[U] = 9.9
90-
ip = pmodel.initial_point(seed=0)
90+
ip = pmodel.initial_point(random_seed=0)
9191
assert ip["B1_interval__"] < 0
9292
assert ip["B2_interval__"] == 0
9393
pass
@@ -121,11 +121,11 @@ def test_initval_resizing(self):
121121
data = aesara.shared(np.arange(4))
122122
rv = pm.Uniform("u", lower=data, upper=10, initval="prior")
123123

124-
ip = pmodel.initial_point(seed=0)
124+
ip = pmodel.initial_point(random_seed=0)
125125
assert np.shape(ip["u_interval__"]) == (4,)
126126

127127
data.set_value(np.arange(5))
128-
ip = pmodel.initial_point(seed=0)
128+
ip = pmodel.initial_point(random_seed=0)
129129
assert np.shape(ip["u_interval__"]) == (5,)
130130
pass
131131

@@ -134,9 +134,9 @@ def test_seeding(self):
134134
pm.Normal("A", initval="prior")
135135
pm.Uniform("B", initval="prior")
136136
pm.Normal("C", initval="moment")
137-
ip1 = pmodel.initial_point(seed=42)
138-
ip2 = pmodel.initial_point(seed=42)
139-
ip3 = pmodel.initial_point(seed=15)
137+
ip1 = pmodel.initial_point(random_seed=42)
138+
ip2 = pmodel.initial_point(random_seed=42)
139+
ip3 = pmodel.initial_point(random_seed=15)
140140
assert ip1 == ip2
141141
assert ip3 != ip2
142142
pass
@@ -285,7 +285,7 @@ class MyNormalDistribution(pm.Normal):
285285
def test_pickling_issue_5090():
286286
with pm.Model() as model:
287287
pm.Normal("x", initval="prior")
288-
ip_before = model.initial_point(seed=5090)
288+
ip_before = model.initial_point(random_seed=5090)
289289
model = cloudpickle.loads(cloudpickle.dumps(model))
290-
ip_after = model.initial_point(seed=5090)
290+
ip_after = model.initial_point(random_seed=5090)
291291
assert ip_before["x"] == ip_after["x"]

pymc/tests/test_model.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def test_missing_data(self):
355355

356356
assert m["x2_missing"].type == gf._extra_vars_shared["x2_missing"].type
357357

358-
pnt = m.initial_point(seed=None).copy()
358+
pnt = m.initial_point(random_seed=None).copy()
359359
del pnt["x2_missing"]
360360

361361
res = [gf(DictToArrayBijection.map(Point(pnt, model=m))) for i in range(5)]
@@ -573,11 +573,6 @@ def test_initial_point():
573573
a = pm.Uniform("a")
574574
x = pm.Normal("x", a)
575575

576-
with pytest.warns(FutureWarning):
577-
initial_point = model.test_point
578-
579-
assert all(var.name in initial_point for var in model.value_vars)
580-
581576
b_initval = np.array(0.3, dtype=aesara.config.floatX)
582577

583578
with pytest.warns(FutureWarning), model:

0 commit comments

Comments
 (0)