Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for newest numpy and xarray versions #456

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@


html_theme = "sphinx_rtd_theme"
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]


# -- General configuration ------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# File: docs/requirements.txt

sphinx>=7.0.0
sphinx_rtd_theme>=1.0.0
sphinx>=8.0.0
sphinx_rtd_theme>=3.0.0
readthedocs-sphinx-search>=0.3.2
jinja2>=3.0.0
mock
Expand Down
31 changes: 31 additions & 0 deletions docs/whats_new.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
What's New
==========
v3.14.2 (2024/10/21)
--------------------
New Features
~~~~~~~~~~~~

Breaking changes
~~~~~~~~~~~~~~~~

Deprecations
~~~~~~~~~~~~

Bug fixes
~~~~~~~~~

Documentation
~~~~~~~~~~~~~

Performance
~~~~~~~~~~~

Internal Changes
~~~~~~~~~~~~~~~~
- Several changes to allow compatibility with latest versions of :py:mod:`numpy` and :py:mod:`xarray`:

- Ensure that :py:class:`float` objects evaluated with :py:mod:`numpy` are converted to :py:class:`float`.

- Ensure proper shape of inputs when loading external data.

- Avoid :py:class:`DeprecationWarning` raised in `invert_matrix`` test.
- Remove `get_html_theme_path` from docs conf.

v3.14.1 (2024/07/18)
--------------------
New Features
Expand Down
2 changes: 1 addition & 1 deletion pysd/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "3.14.1"
__version__ = "3.14.2"
18 changes: 11 additions & 7 deletions pysd/builders/python/python_expressions_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
from pysd.py_backend.utils import compute_shape

from pysd.translators.structures.abstract_expressions import\
AbstractSyntax, AllocateAvailableStructure, AllocateByPriorityStructure,\
ArithmeticStructure, CallStructure, DataStructure, DelayFixedStructure,\
DelayStructure, DelayNStructure, ForecastStructure, GameStructure,\
GetConstantsStructure, GetDataStructure, GetLookupsStructure,\
InitialStructure, InlineLookupsStructure, IntegStructure,\
LogicStructure, LookupsStructure, ReferenceStructure,\
SampleIfTrueStructure, SmoothNStructure, SmoothStructure,\
AbstractSyntax, AllocateAvailableStructure, AllocateByPriorityStructure, \
ArithmeticStructure, CallStructure, DataStructure, DelayFixedStructure, \
DelayStructure, DelayNStructure, ForecastStructure, GameStructure, \
GetConstantsStructure, GetDataStructure, GetLookupsStructure, \
InitialStructure, InlineLookupsStructure, IntegStructure, \
LogicStructure, LookupsStructure, ReferenceStructure, \
SampleIfTrueStructure, SmoothNStructure, SmoothStructure, \
SubscriptsReferenceStructure, TrendStructure

from .python_functions import functionspace
Expand Down Expand Up @@ -694,6 +694,10 @@ def build_function_call(self, arguments: dict) -> BuildAST:
self.section.subscripts, final_subscripts, True)
for i in ["0", "1", "2"]]

# ensure numpy outputs of floats being floats
if "np." in expression and not final_subscripts:
expression = "float(" + expression + ")"

return BuildAST(
expression=expression % arguments,
calls=calls,
Expand Down
17 changes: 7 additions & 10 deletions pysd/py_backend/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,9 @@ def _initialize_data(self, element_type):
if self.interp != "raw":
self._fill_missing(series, data)

# reshape the data to fit in the xarray.DataArray
reshape_dims = tuple([len(series)] + utils.compute_shape(self.coords))

if len(reshape_dims) > 1:
data = self._reshape(data, reshape_dims)
data = self._reshape(data, reshape_dims)

if element_type == "lookup":
dim_name = "lookup_dim"
Expand Down Expand Up @@ -977,15 +976,13 @@ def _initialize(self):
# Create only an xarray if the data is not 0 dimensional
if len(self.coords) > 0:
reshape_dims = tuple(utils.compute_shape(self.coords))

if len(reshape_dims) > 1:
data = self._reshape(data, reshape_dims)

data = xr.DataArray(
data = self._reshape(data, reshape_dims)
return xr.DataArray(
data=data, coords=self.coords, dims=list(self.coords)
)

return data
else:
# need to ensure float is returned and not numpy.float
return float(data)

def _get_constant_data(self, data_across, cell, shape):
"""
Expand Down
6 changes: 3 additions & 3 deletions pysd/py_backend/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1169,14 +1169,14 @@ def _timeseries_component(self, series, dims):
elif dims:
# the interpolation will be time dependent
return lambda: utils.rearrange(
np.interp(self.time(), series.index, series.values),
float(np.interp(self.time(), series.index, series.values)),
dims, self._subscript_dict), {'time': 1}

else:
# the interpolation will be time dependent
return lambda: np.interp(
return lambda: float(np.interp(
self.time(), series.index, series.values
), {'time': 1}
)), {'time': 1}

def _constant_component(self, value, dims):
""" Internal function for creating a constant model element """
Expand Down
3 changes: 1 addition & 2 deletions tests/data/expected_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
coords_2dl = {'ABC': ['A', 'B', 'C'], 'XY': ['X', 'Y']}
dims_2dl = ['XY', 'ABC']

xpts = np.arange(-0.5, 8.6, 0.5)
xpts = np.arange(-0.5, 8.6, 0.5).tolist()

# 1d lookup/data

Expand Down Expand Up @@ -246,4 +246,3 @@

forward_3d = [xr.DataArray(data, coords_2dl, dims_2dl)
for data in forward_3dl]

2 changes: 1 addition & 1 deletion tests/pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ filterwarnings =
error
always:numpy.ndarray size changed, may indicate binary incompatibility.:RuntimeWarning
always::DeprecationWarning
always::PendingDeprecationWarning
always::PendingDeprecationWarning
12 changes: 7 additions & 5 deletions tests/pytest_pysd/pytest_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,27 +445,29 @@ def test_invert_matrix(self):
for data in [data1, data2, data3]:
datai = invert_matrix(data)
assert data.dims == datai.dims
data_v = data.values
datai_v = datai.values

if len(data.shape) == 2:
# two dimensions xarrays
assert (
abs(np.dot(data, datai) - np.dot(datai, data))
abs(np.dot(data_v, datai_v) - np.dot(datai_v, data_v))
< 1e-14
).all()
assert (
abs(np.dot(data, datai) - np.identity(data.shape[-1]))
abs(np.dot(data_v, datai_v) - np.identity(data.shape[-1]))
< 1e-14
).all()
else:
# three dimensions xarrays
for i in range(data.shape[0]):
assert (
abs(np.dot(data[i], datai[i])
- np.dot(datai[i], data[i]))
abs(np.dot(data_v[i], datai_v[i])
- np.dot(datai_v[i], data_v[i]))
< 1e-14
).all()
assert (
abs(np.dot(data[i], datai[i])
abs(np.dot(data_v[i], datai_v[i])
- np.identity(data.shape[-1]))
< 1e-14
).all()
Expand Down
2 changes: 1 addition & 1 deletion tests/pytest_pysd/pytest_pysd.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ def test_set_subscripted_timeseries_parameter_with_constant(self, model):

timeseries = list(range(10))
val_series = [50 + rd for rd in np.random.rand(len(timeseries)
).cumsum()]
).cumsum().tolist()]
xr_series = [xr.DataArray(val, coords, dims) for val in val_series]

temp_timeseries = pd.Series(index=timeseries, data=val_series)
Expand Down
3 changes: 3 additions & 0 deletions tests/pytest_pysd/pytest_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ def data_python(self, data_raw, fake_component, random_size):
}
expr = builder.build(args).expression
expr = expr.replace('()', str(random_size))
if expr.startswith('float'):
# remove float conversion as size is set bigger than 1
expr = expr.replace('float(', '')[:-1]
out[col] = eval(expr)

return pd.DataFrame(out)
Expand Down
2 changes: 2 additions & 0 deletions tests/pytest_types/external/pytest_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,9 @@ def test_constant_0d(self, _root):
data2.initialize()

assert data() == -1
assert type(data()) == float
assert data2() == 0
assert type(data2()) == float

def test_constant_n0d(self, _root):
"""
Expand Down
Loading