Repository navigation
Fix parameter units being silently changed to SI units - #311
henrikjacobsenfys wants to merge 6 commits into
Conversation
|
The design is good and the implementation is careful. There is one inconsistency worth fixing before merge, one API contract change that needs to be documented and communicated, and a substantial amount of duplicated code that should be factored out
DescriptorNumber('s', 1.0, 'dm*m').value # 1.0 , unit 'dm*m' (scipp 0.1m^2)
DescriptorNumber('s', 1.0, sc.Unit('dm*m')).value # 0.1 , unit 'm^2'
DescriptorNumber('s', 1.0, '1/meV^2').value # 1.0 , unit '1/meV^2'
DescriptorNumber('s', 1.0, sc.Unit('1/meV^2')).value # 3.9e+43, unit '1/J^2'
DescriptorNumber('a', 1, 'm**-1').unit == DescriptorNumber('b', 1, '1/m').unit # False
DescriptorNumber('a', 1, 'angstrom').unit == DescriptorNumber('b', 1, 'A').unit # False
# but in both cases a._scalar.unit == b._scalar.unit -> TrueI guess this is "as designed", but the dependent modules, like reflectometry, might be comparing
|
| self._convert_unit(self._base_unit()) | ||
| # Make sure no magnitude is left hiding inside the unit. This has to happen after | ||
| # super().__init__, so that subclasses which convert more than the scalar (such | ||
| # as Parameter, with its bounds) are fully constructed. It should not be recorded |
There was a problem hiding this comment.
super().__init__ here is DescriptorBase.__init__, which knows nothing about Parameter!!
| fitter : Fitter | ||
| A configured ``Fitter`` (or ``MultiFitter``) whose minimizer has been | ||
| switched to ``AvailableMinimizers.Bumps``. | ||
| fitter : 'Fitter' |
There was a problem hiding this comment.
The quotes around Fitter come from some linting tool and are not valid numpydoc syntax.
| try: | ||
| for i in range(count): | ||
| param = Parameter(name=f'thread_{thread_id}_param_{i}', value=float(i)) | ||
| created.append(param) |
There was a problem hiding this comment.
errr why is this change here? Leftover from testing?
There was a problem hiding this comment.
It's because Claude found that this test passed for the wrong reason
| """ | ||
| self._scalar, self._input_unit, self._input_unit_parsed = state | ||
|
|
||
| def _remember_unit(self, unit: str | sc.Unit | None) -> None: |
There was a problem hiding this comment.
descriptor_array.py version of this method uses typing Union
def _remember_unit(self, unit: Union[str, sc.Unit, None]) -> None:
standardize on one
There was a problem hiding this comment.
I think we need to go through the entire library and update it - we still use Optional in some other places..
| if sc.Unit(target) != base_unit: | ||
| # The target does not survive a round trip, so it is not safe to use. | ||
| return None | ||
| except Exception: |
There was a problem hiding this comment.
Bare exception is bad! Especially covering si_base_unit call, conversion to string and calling scipp's Unit. Please consider making the exception more specific.
Yes, there's now explicitly a difference between using a string an a A sc.Unit is the user saying "I want this unit, call it what you want". I'm not entirely sure what to do about "unit as a key" vs "unit as a display string". We can't really do both with just |
|
I don't know why it keeps failing the lint format. pixi run fix is green locally |
This was written partly by Claude under my guidance.
Handles the issue mentioned here: #300
The solution is twofold:
For 1, it also handles e.g. when parameters are added or subtracted from other parameters.