nexusformat / definitions

Definitions of the NeXus Standard File Structure and Contents
https://manual.nexusformat.org/
Other
26 stars 56 forks source link

math in the <dim> element #1084

Open prjemian opened 2 years ago

prjemian commented 2 years ago

Some NXDL files use mathematics involving one or more symbols to describe a <dim /> element.

https://github.com/nexusformat/definitions/blob/ed064a1d983eb27242591440f67fe9dc1c673d27/applications/NXtomophase.nxdl.xml#L162

https://github.com/nexusformat/definitions/blob/ed064a1d983eb27242591440f67fe9dc1c673d27/contributed_definitions/NXsnshisto.nxdl.xml#L196

https://github.com/nexusformat/definitions/blob/ed064a1d983eb27242591440f67fe9dc1c673d27/contributed_definitions/NXsnshisto.nxdl.xml#L314

prjemian commented 2 years ago

It was suggested at the 2022 Code Camp that such math be expressed in a single symbol (in the symbol table) and that symbol be used in the <dim /> element. Such as:

    <symbols>
        <doc>symbolic array lengths to be coordinated between various fields</doc>
        <symbol name="nDarkFrames "><doc>number of dark frames </doc></symbol>
        <symbol name="nBrightFrames "><doc>number of bright frames </doc></symbol>
        <symbol name="nSampleFrame"><doc>number of sample frame </doc></symbol>
        <symbol name="nTotalFrames">
          <doc>total number of frames</doc>
          <math>nDarkFrames + nBrightFrames + nSampleFrame</math>
        </symbol>
    </symbols>

then later

    <dim index="1" value="nTotalFrames" />
prjemian commented 2 years ago

We would add a math element to the symbols element in the nxdl.xsd file. How should the math element be described (both in documentation and how it is prescribed in the XML Schema)?

prjemian commented 2 years ago

For example, that the math expression can be interpreted as javascript and the variables must be defined in the symbol table.

prjemian commented 2 years ago

@mkoennecke asked:

cnxvalidate is written in C, is there a library it can use?

@yayahjb suggests:

https://github.com/samthor/gumnut

RussBerg commented 2 years ago

A C library for processing math strings in EPICS is one used in the calc record https://epics.anl.gov/EpicsDocumentation/AppDevManuals/RecordRef/Recordref-13.html

scrolling down a bit it discusses supported operators and some examples

prjemian commented 2 years ago

The EPICS library likely needs additional work to use in this context.

RussBerg commented 2 years ago

next code camp I could look at how much work this would be

benajamin commented 2 years ago

Proposal is to explore the use of javascript syntax for mathematical expressions in NXDL symbol tables (and elsewhere in NeXus) and encourage the production of a technical demonstration.

PeterC-DLS commented 2 years ago

Other candidates for a parser/interpreter:

yayahjb commented 2 years ago

Sounds worth exploring

PeterC-DLS commented 1 year ago

Formal grammars are available for Javascript/Typescript expressions:

sanbrock commented 1 year ago

Also mentioned this topic on the Code Camp in relationship to #1271 where it is suggested that an NX_CHAR metadata filed with a purpose of describing relationships between other data objects shall follow a specific syntax (provided by an EBNF grammar).

Please note that we do not introduce any formal language to the NXDL in #1271 , but only encourage the use of text entries to become easier to read by machine (which may be less and less important with the evolution of AI).

What I mean:

Hence, some rules are formal part of the definitions expressed in NXDL and are expected to be interpreted, verified by a generic(!) NXDL tool, while others are only expressed in docstrings and expected to be understood only(!) by specific software coded after understanding the docsrting.

One more interesting point:

Note that here we do not use, but define a syntax, and so we rule the space what can be expressed by it. Although we have not proposed, the adoption of the use of EBNF in NXDL can be also discussed if it would be a good choice, so next to the existing XSD rules, all NXDL definitions could also have certain parts which must be compliant with certain EBNF rules. Indeed this is a much safer way then using a generic programming language as we define the syntax and we implement its interpretation.