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

complex units, like eV/mm #1435

Closed
3 tasks
sanbrock opened this issue Sep 26, 2024 · 11 comments · Fixed by #1489
Closed
3 tasks

complex units, like eV/mm #1435

sanbrock opened this issue Sep 26, 2024 · 11 comments · Fixed by #1489
Assignees
Milestone

Comments

@sanbrock
Copy link
Contributor

sanbrock commented Sep 26, 2024

@sanbrock sanbrock converted this from a draft issue Sep 26, 2024
@prjemian
Copy link
Contributor

prjemian commented Sep 28, 2024

Is it appropriate to define a new type of units for this purpose? Here is where they are defined.
https://github.com/nexusformat/definitions/blob/main/nxdlTypes.xsd

@phyy-nx
Copy link
Contributor

phyy-nx commented Sep 28, 2024

Proposal:

For complex types that are not currently in the list NeXus unit categories, allow the attribute, units, to in addition to being NX_ANGLE, NX_AREA, etc, be a vector of fractional SI dimensions. Example,

<field name="mega_unit" type="NX_FLOAT" units="0,1,2,3,4,0,0">

This means the user needs to include the attribute units, and the string they provide needs to have dimensionality of mass * time^2 * electric current^3 * temperature^4

The vector is:
length
mass
time
electric current
thermodynamic temperature
amount of substance
luminous intensity

This is the same order as in UDunits (link)

The HTML rendering won't show the numbers, but instead will use the SI defaults and the exponents to be more human readable. For example, g * s^2 * A^3 * K^4. The XML will have the vector. Whenever this convention is used, it is highly encouraged to proved the common name for these units in the doc string of the field.

This is a supplement to the list of allowed NeXus unit categories, and can be considered for complex types not in the list.

(the NIAC will return to this tomorrow)

@tacaswell
Copy link
Contributor

' '.join(__ for __ in (f'{_}^{v}' if v != 0 else '' for _, v in zip(('s', 'm', 'kg', 'A', 'K', 'mol', 'cd'), (1, 0, 0, 3, -1, 0))) if __)

@prjemian
Copy link
Contributor

prjemian commented Sep 28, 2024

A sequence of seven numbers (in the NXDL file) is hard to embrace as a person who is coaching people to write new application definitions. This is a code that must be remembered. I foresee continual challenges about the proper order of the numbers, for example.

Perhaps there is another, more natural, way to implement this idea?

The problem to be solved here is that a pre-defined a list of units categories might not be bounded. To allow the NeXus standard to be flexible to our community, we need a more robust system to describe expected units as our community's needs expand.

We could add a unit_example="ev/mm" attribute for the monochromator disperion cited above. In general, the unit_example would have a human readable expression that is convertible with UDunits, pint, whatever. This type of expression satisfies the list of numbers with a human readable text. The expression is comparable to the examples already provided in nxdlTypes.xsd. Could it eventually replace the unit categories altogether?

@prjemian
Copy link
Contributor

The second example above (units="0,1,2,3,4,0,0") could be expressed
unit_example="g * s^2 * A^3 * K^4". It could also be expressed in other variations which evaluate to the same units.

@tacaswell
Copy link
Contributor

n [24]: {**dict.fromkeys(('s', 'm', 'kg', 'A', 'K', 'mol', 'cd'), 0), **{k:int(v) for k, v in (g.split('^') for g in 's^1 A^3 K^-1'.split())}}
Out[24]: {'s': 1, 'm': 0, 'kg': 0, 'A': 3, 'K': -1, 'mol': 0, 'cd': 0}

is also pretty straight forward (modulo details like supporting ^1 implicitly).

Maybe instead of trying to write a "nice" string we have to parse, we instead make it a json object as:

<field name="mega_unit" type="NX_FLOAT" units="{'s': 1, 'm': 0, 'kg': 0, 'A': 3, 'K': -1, 'mol': 0, 'cd': 0}">

It is a bit verbose, but it is unambiguous to both humans and computers. Dropping the keys who's value is 0 is likely reasonable.

@phyy-nx
Copy link
Contributor

phyy-nx commented Sep 29, 2024

Of these I prefer the vector representation, but only because the user would never see it. The HTML for the documentation would be rendered as g * s^2 * A^3 * K^4, and we would recommend that developers put in their doc strings the more conventional name for their units. Only the XML would contain the more machine-readable vector.

The unit_example attribute is worth considering though.

@phyy-nx
Copy link
Contributor

phyy-nx commented Sep 29, 2024

Note this is literally #1325, with the addition of extra HTML rendering code to smooth out the non-human readable unit vector.

@phyy-nx
Copy link
Contributor

phyy-nx commented Sep 29, 2024

Alternate proposal:

  • If a unit is not provided by the list of NeXus unit categories, instead of providing a category, the field statement can include an example of the units directly.
  • The example does not constrain the scale of the units. For example, if the unit is eV/mm, they user could specify in a data file eV/cm, or any other unit that is convertible to the example given.

Example:

<field name="monochromator_dispersion" units="eV/mm">

It is recommended that users and application developers check if their units and their unit examples adhere to the UDUNITS standard.

This proposal is accepted by NIAC vote as part of NIAC2024

@prjemian
Copy link
Contributor

prjemian commented Sep 29, 2024

The PR should close these related issues:

@prjemian
Copy link
Contributor

Probably needs a modification to the NXDL Schema.

prjemian added a commit that referenced this issue Sep 30, 2024
@prjemian prjemian self-assigned this Sep 30, 2024
@prjemian prjemian added this to the NXDL 2025 milestone Sep 30, 2024
prjemian added a commit that referenced this issue Oct 14, 2024
@github-project-automation github-project-automation bot moved this from To finish after the meeting to Done in NIAC 2024 project Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants