Skip to content

WIP: fix input broadcasting in modeling#10362

Closed
nden wants to merge 4 commits into
astropy:mainfrom
nden:input-broadcasting-2
Closed

WIP: fix input broadcasting in modeling#10362
nden wants to merge 4 commits into
astropy:mainfrom
nden:input-broadcasting-2

Conversation

@nden

@nden nden commented May 17, 2020

Copy link
Copy Markdown
Contributor

This PR is functionally complete but I marked it as WIP to get opinions on the approach. I will add documentation later.

This takes a slightly different approach to validating inputs in modeling.
The previous behavior was:

  • Input shapes were compared (in _validate_input_shapes) and if not the same an error was raised.
  • If Model.standard_broadcasting=True parameters and inputs were broadcasted to a common shape.
  • There was no way to specify that inputs should not be broadcast among themselves or with parameters.

The new approach:

  • Model.standard_broadcasting=True means that broadcasting rules apply to inputs and parameters. prepare_inputs is not broadcasting the inputs before passing them to Model.evaluate (although it computes the broadcasted shape ) but let's the function implementation deal with this following numpy broadcasting rules.
  • If Model.standard_broadcasting=False it is not assumed that inputs should broadcast nor that inputs and parameters should broadcast. Dealing with input and output shapes is left to the writer of the model. This is a rare use case but is necessary.
  • A special treatment is the case of n_outputs > n_inputs with standard_broadcasting=True. In this case the shape fo the extra outputs is assumed to be the precomputed broadcasted shape.

Fixes #10170
Fixes #9953

@nden nden requested a review from perrygreenfield May 17, 2020 15:54
@astropy-bot astropy-bot Bot added the modeling label May 17, 2020
@nden

nden commented May 17, 2020

Copy link
Copy Markdown
Contributor Author

@chris-simpson

@pllim pllim added the Bug label May 18, 2020
@pllim pllim added this to the v4.1.1 milestone May 18, 2020
@perrygreenfield

Copy link
Copy Markdown
Member

It would be good to give an example of Model.standard_broacasting=False to make it clear (e.g., the model is doing some weird shape changing; what do they call that? Oh yeah, a shapeshifter :-).

That is, point to such a model that does that.

@pllim

pllim commented May 21, 2020

Copy link
Copy Markdown
Member

shapeshifter :-)

Does it only happen during full moon? :D

@perrygreenfield perrygreenfield left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments, but otherwise looks good.

Comment thread astropy/modeling/core.py
if broadcasted_shape is not None:
# Ensure parameter and input shapes broadcast
broadcasted_shape = check_broadcast(broadcasted_shape, param_broadcast)
shapes_of_inputs = [check_broadcast(shape, param_broadcast) for shape in shapes_of_inputs]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering of this check of broadcast should be using broadcasted_shape instead of param_broadcast, in which case an empty broadcasted_shape may need modification if check_broadcast can not handle it.

Comment thread astropy/modeling/core.py
# first check inputs are consistent in shape
input_shape = _validate_input_shapes(args, (), self._n_models,
self.model_set_axis, self.standard_broadcasting)
self.model_set_axis)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this cause problems if self.standard_broadcasting is False (well, it looks like this doesn't change past behavior, but still, is it not a problem?)

result = c(1, [1, 1])
expected = [1., 0., [2., 2.]]
for r, e in zip (result, expected):
assert_allclose(r, e)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a test case where inputs are not shape compatible for a standard_broadcasting=False

@bsipocz bsipocz modified the milestones: v4.1.1, v4.2.1 Nov 25, 2020
@pllim pllim modified the milestones: v4.2.1, v4.2.2 Apr 8, 2021
WilliamJamieson added a commit to WilliamJamieson/astropy that referenced this pull request Apr 15, 2021
WilliamJamieson added a commit to WilliamJamieson/astropy that referenced this pull request Apr 15, 2021
WilliamJamieson added a commit to WilliamJamieson/astropy that referenced this pull request Apr 15, 2021
WilliamJamieson added a commit to WilliamJamieson/astropy that referenced this pull request Apr 27, 2021
Added tests to show that issue astropy#10170 is fixed.

Added `towncrier` change log.

Fixed codestyle check by adding specific exceptions to check.

Possible fixes for issue astropy#11360. More investigation is necessary.

Cleaned up last bugfix.

Updated change log entry.

Added tests for astropy#11360 fixes.

Fixed whitespace

Fix for jwst and gwcs test failures.

Added testing for issue causing fails in jwst and gwcs

Fixed a typo.

Added some of astropy#10362 tests

Changes in astropy#10362 to pass new tests

Code cleanup

More code cleanup

Added additional testing from astropy#10362

Possible fix for issue astropy#11060.

Added test to show astropy#11060 has been fixed

Fixed failing test under no-scipy

Added future warning ignore to newest test

Modified tabular so that both inputs in issue astropy#11060 produce same output
@nden nden closed this in #11548 Apr 27, 2021
@nden nden deleted the input-broadcasting-2 branch January 22, 2026 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Model evaluation crashes if some inputs are scalars Modeling issues with input shape validation

4 participants