-
Notifications
You must be signed in to change notification settings - Fork 247
Test set x deprecation #1946
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
Test set x deprecation #1946
Changes from 14 commits
89bc9a2
3e52f56
30459d9
8fdc10c
968b58b
84c4731
ee2adfa
0fe9359
e8444c5
81b0709
3292830
6e9020e
43e46b8
5035bd1
12944af
0a15ef2
63845fe
5247683
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -479,14 +479,20 @@ def allow_iid_x(self) -> bool: | |
| ) | ||
|
|
||
| def set_x(self, x_o: Optional[Tensor]): | ||
| """Check the shape of the observed data and, if valid, set it.""" | ||
| if x_o is not None: | ||
| x_o = process_x(x_o).to( # type: ignore | ||
| self.device | ||
| ) | ||
| self._x_o = x_o | ||
| for comp_potential in self.potential_fns: | ||
| comp_potential.set_x(x_o) | ||
| """Check the shape of the observed data and, if valid, set it. | ||
|
|
||
| DEPRECATED: Use bind() instead. This method delegates to bind() internally. | ||
|
dgedon marked this conversation as resolved.
|
||
| """ | ||
| import warnings | ||
|
Collaborator
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. imports on file level
Collaborator
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. Not yet addressed. If you decide against it, please let me know why or resolve the comment
Contributor
Author
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. ah sorry, I overlooked that. |
||
|
|
||
| warnings.warn( | ||
| "set_x() is deprecated, use bind() instead", | ||
|
Collaborator
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. check other deprecation statements in the toolbox. Probably we want to add something saying that it will be removed in the future. |
||
| FutureWarning, | ||
| stacklevel=2, | ||
| ) | ||
| bound = self.bind(x_o) | ||
| self._x_o = bound._x_o | ||
| self.potential_fns = bound.potential_fns | ||
|
|
||
| def bind( | ||
| self, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,20 +102,35 @@ def to(self, device: Union[str, torch.device]) -> "PosteriorBasedPotential": | |
|
|
||
| def bind(self, x_o: Tensor, x_is_iid: bool = False) -> "PosteriorBasedPotential": | ||
| """Create new potential with x bound, without mutable state.""" | ||
| from sbi.utils.user_input_checks import process_x | ||
|
Collaborator
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. put on file level |
||
|
|
||
| bound = PosteriorBasedPotential( | ||
| posterior_estimator=self.posterior_estimator, | ||
| prior=self.prior, | ||
| x_o=None, | ||
| device=self.device, | ||
| ) | ||
| bound.set_x(x_o, x_is_iid=x_is_iid) | ||
| x_o = process_x(x_o).to(self.device) | ||
| bound._x_o = x_o | ||
| bound._x_is_iid = x_is_iid | ||
| return bound | ||
|
|
||
| def set_x(self, x_o: Optional[Tensor], x_is_iid: Optional[bool] = False): | ||
| """ | ||
| Check the shape of the observed data and, if valid, set it. | ||
|
|
||
| DEPRECATED: Use bind() instead. This method delegates to bind() internally. | ||
| """ | ||
| super().set_x(x_o, x_is_iid=x_is_iid) | ||
| import warnings | ||
|
|
||
| warnings.warn( | ||
| "set_x() is deprecated, use bind() instead", | ||
|
Collaborator
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. same as above |
||
| FutureWarning, | ||
| stacklevel=2, | ||
| ) | ||
| bound = self.bind(x_o, x_is_iid=x_is_iid) | ||
| self._x_o = bound._x_o | ||
| self._x_is_iid = bound._x_is_iid | ||
|
|
||
| def __call__(self, theta: Tensor, track_gradients: bool = True) -> Tensor: | ||
| r"""Returns the potential for posterior-based methods. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,8 @@ def __init__( | |
| self.vector_field_estimator.eval() | ||
| self.iid_method = iid_method | ||
| self.iid_params = iid_params | ||
| self.guidance_method: Optional[str] = None | ||
| self.guidance_params: Optional[Dict[str, Any]] = None | ||
| self.neural_ode_backend = neural_ode_backend | ||
|
|
||
| neural_ode_kwargs = neural_ode_kwargs or {} | ||
|
|
@@ -110,6 +112,8 @@ def set_x( | |
|
|
||
| Rebuilds the continuous normalizing flow if the observed data is set. | ||
|
|
||
| DEPRECATED: Use bind() instead. This method delegates to bind() internally. | ||
|
|
||
| Args: | ||
| x_o: The observed data. | ||
| x_is_iid: Whether the observed data is IID (if batch_dim>1). | ||
|
|
@@ -119,15 +123,32 @@ def set_x( | |
| `IIDScoreFunction`. | ||
| ode_kwargs: Additional keyword arguments for the neural ODE. | ||
| """ | ||
| super().set_x(x_o, x_is_iid) | ||
| self.iid_method = iid_method or self.iid_method | ||
| self.iid_params = iid_params | ||
| self.guidance_method = guidance_method | ||
| self.guidance_params = guidance_params | ||
| if not x_is_iid and (self._x_o is not None): | ||
| self.flow = self.rebuild_flow(**ode_kwargs) | ||
| elif self._x_o is not None: | ||
| self.flows = self.rebuild_flows_for_batch(**ode_kwargs) | ||
| import warnings | ||
|
Collaborator
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. import on file level |
||
|
|
||
| warnings.warn( | ||
| "set_x() is deprecated, use bind() instead", | ||
| FutureWarning, | ||
| stacklevel=2, | ||
| ) | ||
| bound = self.bind( | ||
| x_o, | ||
| x_is_iid=x_is_iid, | ||
| iid_method=iid_method, | ||
| iid_params=iid_params, | ||
| guidance_method=guidance_method, | ||
| guidance_params=guidance_params, | ||
| **ode_kwargs, | ||
| ) | ||
| self._x_o = bound._x_o | ||
| self._x_is_iid = bound._x_is_iid | ||
| self.iid_method = bound.iid_method | ||
| self.iid_params = bound.iid_params | ||
| self.guidance_method = bound.guidance_method | ||
| self.guidance_params = bound.guidance_params | ||
| if not x_is_iid and (bound._x_o is not None): | ||
| self.flow = bound.flow | ||
| elif bound._x_o is not None: | ||
| self.flows = bound.flows | ||
|
|
||
| def bind( | ||
| self, | ||
|
|
@@ -140,27 +161,31 @@ def bind( | |
| **ode_kwargs, | ||
| ) -> "VectorFieldBasedPotential": | ||
| """Create new potential with x bound, without mutable state.""" | ||
| from sbi.utils.user_input_checks import process_x | ||
|
|
||
| bound = VectorFieldBasedPotential( | ||
| vector_field_estimator=self.vector_field_estimator, | ||
| prior=self.prior, | ||
| x_o=None, | ||
| device=self.device, | ||
| iid_method=self.iid_method, | ||
| iid_params=self.iid_params, | ||
| iid_method=iid_method or self.iid_method, | ||
| iid_params=iid_params if iid_params is not None else self.iid_params, | ||
| neural_ode_backend=self.neural_ode_backend, | ||
| ) | ||
| bound.neural_ode.params.update(self.neural_ode.params) | ||
| # Transfer learned neural ODE parameters (e.g., from training) to the bound | ||
| # potential. This preserves the trained flow model when conditioning on new x. | ||
| bound.set_x( | ||
| x_o, | ||
| x_is_iid=x_is_iid, | ||
| iid_method=iid_method, | ||
| iid_params=iid_params, | ||
| guidance_method=guidance_method, | ||
| guidance_params=guidance_params, | ||
| **ode_kwargs, | ||
| x_o = process_x(x_o).to(self.device) | ||
| bound._x_o = x_o | ||
| bound._x_is_iid = x_is_iid | ||
| bound.guidance_method = ( | ||
| guidance_method if guidance_method is not None else self.guidance_method | ||
| ) | ||
| bound.guidance_params = ( | ||
| guidance_params if guidance_params is not None else self.guidance_params | ||
| ) | ||
| if not x_is_iid and (bound._x_o is not None): | ||
| bound.flow = bound.rebuild_flow(**ode_kwargs) | ||
| elif bound._x_o is not None: | ||
| bound.flows = bound.rebuild_flows_for_batch(**ode_kwargs) | ||
| return bound | ||
|
|
||
| def __call__( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.