Spectrum: Allow assigning None
to the quantity
field
#336
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR closes #333.
Description
This commit allows leaving the
Spectrum.quantity
field unset. This is required to allow for incompleteSpectrum
object initialization, which is, in turn, required to allow for the deferred application of a defaultquantity
field by a converter.The policy is as follows:
quantity
is set, unitless spectrum values are applied appropriate units and validators check if spectrum values have consistent units;quantity
isNone
, unitless spectrum values are allowed and validators do not check spectrum value units, which can be anything.Integral computations will consider unitless fields as dimensionless.
How does it work, in practice?
I refactored the
Spectrum
constructors to apply units, if relevant, prior to initialization. For example withUniformSpectrum
:quantity
is set andvalue
is assigned a quantity, the validator makes a consistency check;quantity
is set andvalue
is assigned a unitless value,value
is attached units, then the validator makes a consistency check;quantity
is unset nothing happens.If one instantiates a
UniformSpectrum
with unsetquantity
, it is possible to:value
field;quantity
field.Both should be done with
attrs.evolve()
to make sure that validation occurs.The whole point of it
The
SpectrumFactory.converter()
method is also updated to allow for consistent initialization ofSpectrum
objects in situations where a missing quantity field can be inferred from the context.In practice, these fixes enforce symmetry between spectrum dictionary and constructor syntaxes:
In snippet 1, the conversion protocol completes the dictionary with a
"quantity"
entry, then used to instantiate a well-formedInterpolatedSpectrum
. In snippet 2, an incompleteInterpolatedSpectrum
instance is created; the converter then evolves it into a new object for which thequantity
field is specified; all unitless fields are then converted and validated.Checklist
main
branch