Skip to content

Commit

Permalink
Pass kwargs from pm.Data to aesara.shared (#5098)
Browse files Browse the repository at this point in the history
* Tests kwargs are passed from Data to aesara.shared

* Pass kwargs from Data to aesara.shared

* Document Data kwargs in release notes

* Convert Aesara references to intersphinx
  • Loading branch information
paw-lu authored Nov 5, 2021
1 parent 02b8675 commit 5f9a2e3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ This includes API changes we did not warn about since at least `3.11.0` (2021-01
- New features for BART:
- Added linear response, increased number of trees fitted per step [5044](https://github.com/pymc-devs/pymc3/pull/5044).
- Added partial dependence plots and individual conditional expectation plots [5091](https://github.com/pymc-devs/pymc3/pull/5091).
- `pm.Data` now passes additional kwargs to `aesara.shared`. [#5098](https://github.com/pymc-devs/pymc/pull/5098)
- ...


Expand Down
20 changes: 15 additions & 5 deletions pymc/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,8 @@ def align_minibatches(batches=None):


class Data:
"""Data container class that wraps the Aesara ``SharedVariable`` class
and lets the model be aware of its inputs and outputs.
"""Data container class that wraps :func:`aesara.shared` and lets
the model be aware of its inputs and outputs.
Parameters
----------
Expand All @@ -478,10 +478,12 @@ class Data:
random variables). Use this when `value` is a pandas Series or DataFrame. The
`dims` will then be the name of the Series / DataFrame's columns. See ArviZ
documentation for more information about dimensions and coordinates:
https://arviz-devs.github.io/arviz/notebooks/Introduction.html
:ref:`arviz:quickstart`.
export_index_as_coords: bool, optional, default=False
If True, the `Data` container will try to infer what the coordinates should be
if there is an index in `value`.
**kwargs: dict, optional
Extra arguments passed to :func:`aesara.shared`.
Examples
--------
Expand Down Expand Up @@ -512,7 +514,15 @@ class Data:
https://docs.pymc.io/notebooks/data_container.html
"""

def __new__(self, name, value, *, dims=None, export_index_as_coords=False):
def __new__(
self,
name,
value,
*,
dims=None,
export_index_as_coords=False,
**kwargs,
):
if isinstance(value, list):
value = np.array(value)

Expand All @@ -528,7 +538,7 @@ def __new__(self, name, value, *, dims=None, export_index_as_coords=False):

# `pandas_to_array` takes care of parameter `value` and
# transforms it to something digestible for pymc
shared_object = aesara.shared(pandas_to_array(value), name)
shared_object = aesara.shared(pandas_to_array(value), name, **kwargs)

if isinstance(dims, str):
dims = (dims,)
Expand Down
13 changes: 13 additions & 0 deletions pymc/tests/test_data_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,19 @@ def test_implicit_coords_dataframe(self):
assert "columns" in pmodel.coords
assert pmodel.RV_dims == {"observations": ("rows", "columns")}

def test_data_kwargs(self):
strict_value = True
allow_downcast_value = False
with pm.Model():
data = pm.Data(
"data",
value=[[1.0], [2.0], [3.0]],
strict=strict_value,
allow_downcast=allow_downcast_value,
)
assert data.container.strict is strict_value
assert data.container.allow_downcast is allow_downcast_value


def test_data_naming():
"""
Expand Down

0 comments on commit 5f9a2e3

Please sign in to comment.