Add functionality to the flexible unit type
@mnakh Here is a preliminary description of the flexible unit issue that I mentioned previously:
Background:
The Quantity()
class has a boolean attribute called flexible_unit
. If set to True in the definition of a quantity, it allows one to set that quantity to a value with units different then the units provided in the class definition. This can be seen in nomad/metainfo/metainfo.py
, and specifically in the serialize()
function within the _Unit()
class.
Problem:
setting flexible_unit = True
simply results in the unit being ignored (and as a consequence, lost) during serialization.
Desired Functionality: Currently, there are at least a couple situations where the unit of a quantity may not be known in advance:
-
We are trying to build support for uploading (semi-)custom computational data to NOMAD. In particular, we want users to be able to upload some observables that they calculate from their simulations, following a semi-flexible schema that does not require certain details (including units) to be specified in advance. In the current framework, we are forced to save units as a string in the archive corresponding to some value. I.e., we always have to define
observable_value = Quantity(..)
andobservable_value_unit = Quantity(type=str, ...)
. It would be very useful to be able to define the unit at the time of class instantiation. I don't have a good idea though if this would be possible or what complications would come from it. -
More generally, in defining useful base class structures, with subclasses that share quantities but have different units. I believe that it would be useful (for schema consistence and maintenance, among other reasons) to not completely redefine the Quantity in order to define the units for the subclass, but instead simply define the unit either in the init or in the normalization (not sure if this works/makes sense).
Probably there are additional use-cases, but this gives you some idea to start.