Skip to content

Commit b3d3c0d

Browse files
Add time dependency in random functions
1 parent 08b1699 commit b3d3c0d

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

docs/whats_new.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
What's New
22
==========
33

4-
v3.9.1 (2023/02/xx)
4+
v3.9.1 (2023/03/11)
55
-------------------
66

77
New Features
@@ -19,6 +19,7 @@ Bug fixes
1919
- Set :py:mod:`numpy` <1.24 to avoid errors with least squares equation in :py:func:`pysd.py_backend.allocation.allocate_available`. (`@enekomartinmartinez <https://github.com/enekomartinmartinez>`_)
2020
- Keep the attributes of a component when using :py:meth:`pysd.py_backend.model.Macro.set_components` to avoid losing coords or arguments information. (`@enekomartinmartinez <https://github.com/enekomartinmartinez>`_)
2121
- Set :py:mod:`openpyxl` <3.1 to avoid errors due to non-backwards compatible changes. (`@enekomartinmartinez <https://github.com/enekomartinmartinez>`_)
22+
- Include time dependency in random functions to avoid them using constant cache. (`@enekomartinmartinez <https://github.com/enekomartinmartinez>`_)
2223

2324
Documentation
2425
~~~~~~~~~~~~~

pysd/builders/python/python_expressions_builder.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ def build_function_call(self, arguments: dict) -> BuildAST:
642642
self.def_subs)[1]
643643
expression = f"xr.DataArray({expression}, {subs}, "\
644644
f"{list(self.def_subs)})"
645+
calls["time"] = 1
645646

646647
elif self.function == "active_initial":
647648
# Ee need to ensure that active initial outputs are always the

tests/pytest_pysd/pytest_random.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import pytest
33
import shutil
44

5+
import numpy as np
6+
import xarray as xr
7+
58
import pysd
69

710

@@ -28,4 +31,28 @@ def test_translate(self, model_path):
2831
"""
2932
# expected file
3033
model = pysd.read_vensim(model_path)
31-
model.run()
34+
random_vars = [
35+
"A B uniform matrix",
36+
"A B uniform matrix 1",
37+
"A B uniform matrix 1 0",
38+
"A B uniform scalar",
39+
"A B uniform vec",
40+
"A B uniform vec 1",
41+
"normal A B uniform matrix",
42+
"normal A B uniform matrix 1",
43+
"normal A B uniform matrix 1 0",
44+
"normal scalar",
45+
"normal vec",
46+
"normal vec 1",
47+
"uniform matrix",
48+
"uniform scalar",
49+
"uniform vec"
50+
]
51+
out = model.run(return_columns=random_vars, flatten_output=False)
52+
for var in out.columns:
53+
if isinstance(out[var].values[0], xr.DataArray):
54+
values = np.array([a.values for a in out[var].values])
55+
else:
56+
values = out[var].values
57+
# assert all values are different in each dimension and time step
58+
assert len(np.unique(values)) == np.prod(values.shape)

0 commit comments

Comments
 (0)