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

Improved error messages from wraps_ufunc #108

Merged
merged 10 commits into from
Sep 19, 2024
Merged

Conversation

tbody-cfs
Copy link
Collaborator

@tbody-cfs tbody-cfs commented Sep 16, 2024

  1. Improves error message if wraps_ufunc input_units don't match function parameters
  2. Improves error message if arguments provided to a wraps_ufunc-wrapped function don't match the expected arguments
  3. Improves error message if number of returned values from a wraps_ufunc-wrapped function don't match the number of returned units
  4. In case return_keys isn't given as a list, converts to a list.
  5. Let's you build a CompositeAlgorithm from a list of algorithm names, as a shorthand for the [Algorithm.get_algorithm(key) for key in keys] pattern.
  6. Instead of failing the file write due to unhandled variable sanitization, throw a warning and return UNHANDLED

Example errors

ValueError: Keys for input_units for calc_effective_collisionality did not match the declared function inputs (n.b. order matters!)
i: input_key, func_param
0: average_electron_denssity, average_electron_density DOES NOT MATCH
1: average_electron_temp, average_electron_temp
2: major_radius, major_radius
3: z_effective, z_effective
ValueError: Number of returned values (1) did not match length of units_mapping (2)
i: return_key, returned_value
0: effective_collisionality, <xarray.DataArray 'average_electron_density' (dim_average_electron_density: 40,
                                              dim_average_electron_temp: 30)> Size: 10kB
array([[0.00981582, 0.00807611, 0.00676363, ..., 0.00069563, 0.00065933,
        0.00062581],
       [0.01963165, 0.01615222, 0.01352725, ..., 0.00139126, 0.00131866,
        0.00125162],
       [0.02944747, 0.02422833, 0.02029088, ..., 0.00208689, 0.001978  ,
        0.00187744],
       ...,
       [0.3730014 , 0.30689229, 0.25701795, ..., 0.02643434, 0.02505507,
        0.02378129],
       [0.38281722, 0.3149684 , 0.26378158, ..., 0.02712999, 0.02571442,
        0.02440711],
       [0.39263305, 0.32304452, 0.27054521, ..., 0.02782563, 0.02637377,
        0.02503294]])
Coordinates:
  * dim_average_electron_density  (dim_average_electron_density) float64 320B ...
  * dim_average_electron_temp     (dim_average_electron_temp) float64 240B 5....
1: test, MISSING
ValueError: Number of returned values (2) did not match length of units_mapping (1)
i: return_key, returned_value
0: effective_collisionality, <xarray.DataArray 'average_electron_density' (dim_average_electron_density: 40,
                                              dim_average_electron_temp: 30)> Size: 10kB
array([[0.00981582, 0.00807611, 0.00676363, ..., 0.00069563, 0.00065933,
        0.00062581],
       [0.01963165, 0.01615222, 0.01352725, ..., 0.00139126, 0.00131866,
        0.00125162],
       [0.02944747, 0.02422833, 0.02029088, ..., 0.00208689, 0.001978  ,
        0.00187744],
       ...,
       [0.3730014 , 0.30689229, 0.25701795, ..., 0.02643434, 0.02505507,
        0.02378129],
       [0.38281722, 0.3149684 , 0.26378158, ..., 0.02712999, 0.02571442,
        0.02440711],
       [0.39263305, 0.32304452, 0.27054521, ..., 0.02782563, 0.02637377,
        0.02503294]])
Coordinates:
  * dim_average_electron_density  (dim_average_electron_density) float64 320B ...
  * dim_average_electron_temp     (dim_average_electron_temp) float64 240B 5....
1: MISSING, <xarray.DataArray 'average_electron_density' (dim_average_electron_density: 40,
                                              dim_average_electron_temp: 30)> Size: 10kB
array([[3.2, 3.2, 3.2, ..., 3.2, 3.2, 3.2],
       [3.2, 3.2, 3.2, ..., 3.2, 3.2, 3.2],
       [3.2, 3.2, 3.2, ..., 3.2, 3.2, 3.2],
       ...,
       [3.2, 3.2, 3.2, ..., 3.2, 3.2, 3.2],
       [3.2, 3.2, 3.2, ..., 3.2, 3.2, 3.2],
       [3.2, 3.2, 3.2, ..., 3.2, 3.2, 3.2]])
Coordinates:
  * dim_average_electron_density  (dim_average_electron_density) float64 320B ...
  * dim_average_electron_temp     (dim_average_electron_temp) float64 240B 5....

@tbody-cfs tbody-cfs requested a review from hassec September 16, 2024 17:53
Copy link
Collaborator

@hassec hassec left a comment

Choose a reason for hiding this comment

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

Some small comments. Would you mind making sure to run pre-commit locally to fix the formatting error the CI is complaining about?

cfspopcon/file_io.py Outdated Show resolved Hide resolved
cfspopcon/unit_handling/decorator.py Outdated Show resolved Hide resolved
cfspopcon/unit_handling/decorator.py Show resolved Hide resolved
@tbody-cfs tbody-cfs changed the title Raddivmom compatibility Improved error messages from wraps_ufunc Sep 19, 2024
Copy link
Collaborator

@hassec hassec left a comment

Choose a reason for hiding this comment

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

Great stuff, thanks Tom 👍

@tbody-cfs tbody-cfs merged commit 5b30fa0 into main Sep 19, 2024
8 checks passed
@tbody-cfs tbody-cfs deleted the raddivmom_compatibility branch September 19, 2024 17:04
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