Skip to content

al/bf-mie-fix#478

Open
Pwhsky wants to merge 4 commits into
gv/final/features2from
al/brightfield-mie-fix
Open

al/bf-mie-fix#478
Pwhsky wants to merge 4 commits into
gv/final/features2from
al/brightfield-mie-fix

Conversation

@Pwhsky
Copy link
Copy Markdown
Collaborator

@Pwhsky Pwhsky commented May 4, 2026

Adds a line to convert a list to an xp-array when resolving fields for Mie scatterers.

line 2002 in optics.py:

...
     field_arrays.append(arr)

field = xp.sum(field_arrays, axis=0)

Will fail when attempting xp.sum due to field_arrays being a list.

Adding an intermediate conversion:

...
     field_arrays.append(arr)

field_arrays = xp.asarray(field_arrays)
field = xp.sum(field_arrays, axis=0)

Resolves this.

@edudc
Copy link
Copy Markdown

edudc commented May 8, 2026

xp.asarray(field_arrays) fixes the NumPy case, but still fails for torch when
field_arrays is a list of multi-element tensors:

ValueError: only one element tensors can be converted to Python scalars

The numpy and torch safe operation is stack instead of xp.asarray, then sum:
field = xp.sum(xp.stack(field_arrays, axis=0), axis=0)
This also preserves torch autograd through the stacked scattered fields.

Copy link
Copy Markdown
Contributor

@cmanzo cmanzo left a comment

Choose a reason for hiding this comment

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

Please implement Eduard suggestions

@Pwhsky Pwhsky requested a review from cmanzo May 12, 2026 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants