-
Notifications
You must be signed in to change notification settings - Fork 137
Conserve rescale shape #863
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
base: main
Are you sure you want to change the base?
Changes from all commits
456576b
8270402
27f4ef6
60d7be1
6c1bd66
2ddf678
1a1ec87
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 |
|---|---|---|
|
|
@@ -598,20 +598,26 @@ def rescale(self, keys, theta): | |
|
|
||
| Parameters | ||
| ========== | ||
| keys: list | ||
| keys: array-like | ||
| List of prior keys to be rescaled | ||
| theta: list | ||
| List of randomly drawn values on a unit cube associated with the prior keys | ||
| theta: array-like | ||
| Randomly drawn values on a unit cube associated with the prior keys | ||
|
|
||
| Returns | ||
| ======= | ||
| list: List of floats containing the rescaled sample | ||
| list: | ||
| If theta is 1D, returns list of floats containing the rescaled sample. | ||
| If theta is 2D, returns list of lists containing the rescaled samples. | ||
| """ | ||
| theta = list(theta) | ||
| if isinstance(theta, {}.values().__class__): | ||
| theta = list(theta) | ||
| samples = [] | ||
| for key, units in zip(keys, theta): | ||
| samps = self[key].rescale(units) | ||
| samples += list(np.asarray(samps).flatten()) | ||
| samples.append(samps) | ||
| for i, samps in enumerate(samples): | ||
| # turns 0d-arrays into scalars | ||
| samples[i] = np.squeeze(samps).tolist() | ||
|
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. I wonder if it would not make more sense to preserve numpy arrays to make it easier to define conversion_functions and so on without needing to cast to array again
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. I think it is would be worth returning arrays, I think it's worth accepting that this is going to not be fully backward compatible. |
||
| return samples | ||
|
|
||
| def test_redundancy(self, key, disable_logging=False): | ||
|
|
@@ -830,30 +836,33 @@ def rescale(self, keys, theta): | |
|
|
||
| Parameters | ||
| ========== | ||
| keys: list | ||
| keys: array-like | ||
| List of prior keys to be rescaled | ||
| theta: list | ||
| List of randomly drawn values on a unit cube associated with the prior keys | ||
| theta: array-like | ||
| Randomly drawn values on a unit cube associated with the prior keys | ||
|
|
||
| Returns | ||
| ======= | ||
| list: List of floats containing the rescaled sample | ||
| list: | ||
| If theta is float for each key, returns list of floats containing the rescaled sample. | ||
| If theta is array-like for each key, returns list of lists containing the rescaled samples. | ||
| """ | ||
| keys = list(keys) | ||
| theta = list(theta) | ||
| if isinstance(theta, {}.values().__class__): | ||
| theta = list(theta) | ||
| if isinstance(keys, {}.keys().__class__): | ||
| keys = list(keys) | ||
|
|
||
| self._check_resolved() | ||
| self._update_rescale_keys(keys) | ||
| result = dict() | ||
| for key, index in zip( | ||
| self.sorted_keys_without_fixed_parameters, self._rescale_indexes | ||
| ): | ||
| result[key] = self[key].rescale( | ||
| theta[index], **self.get_required_variables(key) | ||
| ) | ||
| for key, index in zip(self.sorted_keys_without_fixed_parameters, self._rescale_indexes): | ||
| result[key] = self[key].rescale(theta[index], **self.get_required_variables(key)) | ||
| self[key].least_recently_sampled = result[key] | ||
| samples = [] | ||
| for key in keys: | ||
| samples += list(np.asarray(result[key]).flatten()) | ||
| # turns 0d-arrays into scalars | ||
| res = np.squeeze(result[key]).tolist() | ||
| samples.append(res) | ||
| return samples | ||
|
|
||
| def _update_rescale_keys(self, keys): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth returning the same type as the input, i.e., return a dict if a dict is passed.
Another option would be to add a specific method to handle dicts, I've thought about doing this a few times before, e.g.,
PriorDict.rescale_dictthat just wrapsPriorDict.rescale.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could also apply to the conservation of numpy arrays, ie return a dict, list or array depending on the input.