Skip to content
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

Allow scalar input for 't', 'E' and 'data' arguments of Container class #301

Merged
merged 6 commits into from
Feb 13, 2024

Conversation

Sheshuk
Copy link
Contributor

@Sheshuk Sheshuk commented Feb 8, 2024

Fixing problem if a user passes a scalar value to container.
Example:

import numpy as np
import astropy.units as u
from snewpy.flux import Flux
from snewpy.neutrino import Flavor

# this was the only valid way before this fix: wrap everything in []
Flux(data = [1] << u.Unit('1/(MeV s cm**2)'), 
     flavor=[Flavor.NU_E],
     time=[1]<<u.s,
     energy=[1]<<u.MeV)

#now it's possible to pass scalars if there is only a single value or point
Flux(data = 1 << u.Unit('1/(MeV s cm**2)'), 
     flavor=Flavor.NU_E, 
     time=1<<u.s,
     energy=1<<u.MeV)

#they're converted internally to arrays. This produces 
# d2FdEdT (1, 1, 1) [1 / (MeV s m2)]: <1 flavor(0;0) x 1 time(1.0 s;1.0 s) x 1 energy(1.0 MeV;1.0 MeV)>

@JostMigenda
Copy link
Member

Even worse—before, you needed data = [[[1]]] << u.Unit('1/(MeV s cm**2)') (with triple brackets). I agree this is a nice improvement; thanks!

Copy link
Member

@JostMigenda JostMigenda left a comment

Choose a reason for hiding this comment

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

Actually, on second thought, one thing gives me pause:

In addition to giving scalars for all arguments, this would also enable using a mix of scalars and vectors. E.g.

# pass array of energies, scalars for all quantities
Flux(data = [1, 1, 1] << u.Unit('1/(MeV s cm**2)'), 
     flavor=Flavor.NU_E, 
     time=1<<u.s,
     energy=[1, 2, 3]<<u.MeV)

# pass array of times, scalars for all other quantities
Flux(data = [1, 1, 1] << u.Unit('1/(MeV s cm**2)'), 
     flavor=Flavor.NU_E, 
     time=[1, 2, 3]<<u.s,
     energy=1<<u.MeV)

Both would produce the same 3D data array, but the intended physics meaning is different. I wonder if this makes it too easy to introduce mistakes?

@Sheshuk
Copy link
Contributor Author

Sheshuk commented Feb 12, 2024

Thanks, you're right! I didn't think about that. That's definitely against what the user would expect.

@Sheshuk
Copy link
Contributor Author

Sheshuk commented Feb 12, 2024

I added:

  1. Reshaping data array if it's 1D to one of the expected shapes.
  2. Array shape validation vs. expected shapes.

Maybe it still doesn't cover all the possible corner cases.
But in general this class is not supposed to be created by user, but rather produced in the SupernovaModel.get_flux etc...

But I need to think of more strict conditions, when to use this reshaping

Copy link
Member

@JostMigenda JostMigenda left a comment

Choose a reason for hiding this comment

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

I’m not quite sure about corner cases either; but this certainly adds useful guard rails for the intuitive(-seeming) case. And for all other cases, one has to consciously think about the array shape anyway; so having no extra guard rails is probably fine.

Thanks!

@JostMigenda JostMigenda merged commit 7ef5775 into main Feb 13, 2024
10 checks passed
@sybenzvi sybenzvi deleted the Sheshuk/allow_scalar_input_for_FluxContainer branch March 4, 2024 22:13
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.

2 participants