-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix dim lengths created from coords or ConstantData #5751 #5763
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
import warnings | ||
|
||
from copy import copy | ||
from typing import Any, Dict, List, Optional, Sequence, Union, cast | ||
from typing import Any, Dict, List, Optional, Sequence, Tuple, Union, cast | ||
|
||
import aesara | ||
import aesara.tensor as at | ||
|
@@ -466,9 +466,15 @@ def align_minibatches(batches=None): | |
rng.seed() | ||
|
||
|
||
def determine_coords(model, value, dims: Optional[Sequence[str]] = None) -> Dict[str, Sequence]: | ||
def determine_coords( | ||
model, | ||
value, | ||
dims: Optional[Sequence[Optional[str]]] = None, | ||
coords: Optional[Dict[str, Sequence]] = None, | ||
) -> Tuple[Dict[str, Sequence], Sequence[Optional[str]]]: | ||
"""Determines coordinate values from data or the model (via ``dims``).""" | ||
coords = {} | ||
if coords is None: | ||
coords = {} | ||
|
||
# If value is a df or a series, we interpret the index as coords: | ||
if hasattr(value, "index"): | ||
|
@@ -499,17 +505,22 @@ def determine_coords(model, value, dims: Optional[Sequence[str]] = None) -> Dict | |
) | ||
for size, dim in zip(value.shape, dims): | ||
coord = model.coords.get(dim, None) | ||
if coord is None: | ||
if coord is None and dim is not None: | ||
coords[dim] = range(size) | ||
|
||
return coords | ||
if dims is None: | ||
# TODO: Also determine dim names from the index | ||
dims = [None] * np.ndim(value) | ||
|
||
return coords, dims | ||
|
||
|
||
def ConstantData( | ||
name: str, | ||
value, | ||
*, | ||
dims: Optional[Sequence[str]] = None, | ||
coords: Optional[Dict[str, Sequence]] = None, | ||
export_index_as_coords=False, | ||
**kwargs, | ||
) -> TensorConstant: | ||
|
@@ -522,6 +533,7 @@ def ConstantData( | |
name, | ||
value, | ||
dims=dims, | ||
coords=coords, | ||
export_index_as_coords=export_index_as_coords, | ||
mutable=False, | ||
**kwargs, | ||
|
@@ -534,6 +546,7 @@ def MutableData( | |
value, | ||
*, | ||
dims: Optional[Sequence[str]] = None, | ||
coords: Optional[Dict[str, Sequence]] = None, | ||
export_index_as_coords=False, | ||
**kwargs, | ||
) -> SharedVariable: | ||
|
@@ -546,6 +559,7 @@ def MutableData( | |
name, | ||
value, | ||
dims=dims, | ||
coords=coords, | ||
export_index_as_coords=export_index_as_coords, | ||
mutable=True, | ||
**kwargs, | ||
|
@@ -558,6 +572,7 @@ def Data( | |
value, | ||
*, | ||
dims: Optional[Sequence[str]] = None, | ||
coords: Optional[Dict[str, Sequence]] = None, | ||
export_index_as_coords=False, | ||
mutable: Optional[bool] = None, | ||
**kwargs, | ||
|
@@ -588,9 +603,11 @@ def Data( | |
:ref:`arviz:quickstart`. | ||
If this parameter is not specified, the random variables will not have dimension | ||
names. | ||
coords : dict, optional | ||
Coordinate values to set for new dimensions introduced by this ``Data`` variable. | ||
export_index_as_coords : bool, default=False | ||
If True, the ``Data`` container will try to infer what the coordinates should be | ||
if there is an index in ``value``. | ||
If True, the ``Data`` container will try to infer what the coordinates | ||
and dimension names should be if there is an index in ``value``. | ||
mutable : bool, optional | ||
Switches between creating a :class:`~aesara.compile.sharedvalue.SharedVariable` | ||
(``mutable=True``) vs. creating a :class:`~aesara.tensor.TensorConstant` | ||
|
@@ -624,6 +641,9 @@ def Data( | |
... model.set_data('data', data_vals) | ||
... idatas.append(pm.sample()) | ||
""" | ||
if coords is None: | ||
coords = {} | ||
|
||
if isinstance(value, list): | ||
value = np.array(value) | ||
|
||
|
@@ -665,15 +685,28 @@ def Data( | |
expected=x.ndim, | ||
) | ||
|
||
coords = determine_coords(model, value, dims) | ||
|
||
# Optionally infer coords and dims from the input value. | ||
if export_index_as_coords: | ||
model.add_coords(coords) | ||
elif dims: | ||
coords, dims = determine_coords(model, value, dims) | ||
|
||
if dims: | ||
if not mutable: | ||
# Use the dimension lengths from the before it was tensorified. | ||
# These can still be tensors, but in many cases they are numeric. | ||
xshape = np.shape(arr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't matter much but |
||
else: | ||
xshape = x.shape | ||
# Register new dimension lengths | ||
for d, dname in enumerate(dims): | ||
if not dname in model.dim_lengths: | ||
model.add_coord(dname, values=None, length=x.shape[d]) | ||
model.add_coord( | ||
name=dname, | ||
# Note: Coordinate values can't be taken from | ||
# the value, because it could be N-dimensional. | ||
values=coords.get(dname, None), | ||
mutable=mutable, | ||
length=xshape[d], | ||
) | ||
|
||
model.add_random_variable(x, dims=dims) | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.