Skip to content

Conversation

@jokasimr
Copy link
Contributor

@jokasimr jokasimr commented Dec 12, 2025

This PR makes some unit conversions safer by explicitly casting their dtypes.

This is important when converting quantities that are provided by users because if the user provides a value with integer dtype the result of the unit conversion can be unexpected.

For example, if the user provides a SampleRotationOffset: sc.scalar(1, unit='deg') and we convert that to rad then after the conversion the value will be 0 rad. By converting the dtype to whatever the SampleRotation dtype is (almost certainly float) we avoid the error.

This doesn't solve the problem completely, because strictly speaking we don't distinguish between user provided parameters and non-user provided parameters, but it should prevent errors in most common cases.

Should be rebased on main once #186 is merged.

@jokasimr jokasimr force-pushed the fix-unit-conversions branch from a674844 to bf7a0a2 Compare December 15, 2025 09:40
@jokasimr jokasimr requested a review from jl-wynen December 15, 2025 09:41
Copy link
Member

@nvaytet nvaytet left a comment

Choose a reason for hiding this comment

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

I would just convert to float64 everywhere and not worry about the dtypes of the other operands.
We don't always know if L2 or mu will be float (they could also be ints)?

@jokasimr
Copy link
Contributor Author

Yeah, maybe that's better 🤷 It's a trade-off, we want some flexibility letting users determine data types, without setting up footguns.

https://scipp.github.io/essreduce/user-guide/reduction-workflow-guidelines.html#s-6-preserve-floating-point-precision-of-input-data-and-coordinates

@SimonHeybrock
Copy link
Member

Yeah, maybe that's better 🤷 It's a trade-off, we want some flexibility letting users determine data types, without setting up footguns.

https://scipp.github.io/essreduce/user-guide/reduction-workflow-guidelines.html#s-6-preserve-floating-point-precision-of-input-data-and-coordinates

I think this rule is mainly referring to the "large" coords, i.e., the ones we have per-event, rather than per-pixel (or scalars).

@jokasimr
Copy link
Contributor Author

I think this rule is mainly referring to the "large" coords, i.e., the ones we have per-event, rather than per-pixel (or scalars).

But the reflection angle is used to compute Q and that's a per-event coord. So if we cast the angle to float64 then Q will also always be float64.

But I don't mind casting it to float64 instead if you both think that's better.

@SimonHeybrock
Copy link
Member

I think this rule is mainly referring to the "large" coords, i.e., the ones we have per-event, rather than per-pixel (or scalars).

But the reflection angle is used to compute Q and that's a per-event coord. So if we cast the angle to float64 then Q will also always be float64.

Not if you convert to the desired unit before the operation?

But probably flat64 everywhere is also fine.

@jokasimr
Copy link
Contributor Author

Not if you convert to the desired unit before the operation?

Do you mean dtype?

I don't understand what operation you're referring to. The operation that computes Q?

@SimonHeybrock
Copy link
Member

Not if you convert to the desired unit before the operation?

Do you mean dtype?

Yes.

I don't understand what operation you're referring to. The operation that computes Q?

Yes.

)
).to(unit=theta.unit)
sc.atan(detector_spatial_resolution.to(unit=L2.unit, dtype='float64') / L2)
).to(unit=theta.unit, dtype='float64')
Copy link
Member

Choose a reason for hiding this comment

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

This last conversion is operating on a variable which has size Npixels (because of L2).
You could add a copy=False in the call to to(...) in case the units are already the correct ones and we want to return early. That said, Npixels is not that large.

@jokasimr jokasimr enabled auto-merge December 19, 2025 12:44
@jokasimr jokasimr merged commit 85c3fcd into main Dec 19, 2025
4 checks passed
@jokasimr jokasimr deleted the fix-unit-conversions branch December 19, 2025 12:57
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.

4 participants