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

Calculated and conditional fields in metadata #3703

Open
cm-le opened this issue Jul 11, 2022 · 8 comments
Open

Calculated and conditional fields in metadata #3703

cm-le opened this issue Jul 11, 2022 · 8 comments

Comments

@cm-le
Copy link

cm-le commented Jul 11, 2022

Feature request

Hi Nico,

When designing the new format for the metadata JSON, I suggest - if you haven't already - to consider support for calculated fields and conditional displaying of fields.

Calculated fields could be used directly for all kinds of scores or unit conversions, and conditionally displayed fields would provide most of "intelligent forms" features.

There are libraries like JSFormula or fparse which allow evaluating simple formulas without the headaches of Javascript eval() - and are still flexible enough to provide custom features.

Suppose you have fields:

{
    "extra_fields": [
			 {
			        "name": "sio2_possible",
			        "label": "Was the SiO2 measurement possible?",
				"type": "radio",
				 "value": "n/a",
				 "options": ["n/a", "Yes", "No"]
			 },
		        {
			     "name": "sio2_wt_percent",
			     "label": "SiO2 Wt.%",
			     "type": "number",
			     "show_only_if": "eq({sio2_possible},'Yes')"
			 },
			 {
			     "name": "sio2_gfw",
			     "label": "SiO2 GFW",
			     "type": "calculated",
			     "show_only_if": "gt({sio2_wt_percent},0)"
			     "formula": "{sio2_wt_percent}/60.084"
			 },
		        {
			    "name": "sio2_missing_reason",
			    "label": "Why was the SiO2 measurement not possible?",
			    "type": "text",
			     "show_only_if": "eq({sio2_possible},'No')"
			 }
  ]
}

With these features (and the heavy lifting provided by the formula evaluator) it should be possible to support quite sophisticated forms with very little implementation effort. The formula evaluators simply get the formula as a string, and a Javascript object with all the previous field values.

I'm very much aware that this is a slippery slope, and one might be tempted to request editable per-project function libraries, additional layout parameters, matrix/tabular-questions..., but this should not stand in the way of a simple solution.

I would be happy to provide a proof-of-concept implementation if this is not already done or you are outright rejecting this idea - regardless of the outcome, I would expect to learn something about elabftw development.

PS: Sorry for sneaking in the distinction between name and label, which is actually another suggestion.

I'm using the hosted version or I have PRO support: NO

@garrettcurley
Copy link

Nice suggestions. I agree with the idea of a "calculated" field, but not for the same application. I would have more in mind the possibility of entering values using different units. Say, a pressure measurement. The user could fill in a field named "Pressure" in Pascal, bar, mbar, or mmHg (Torr) and select the unit used. This would go to a "rawValue" field that would be displayed. The actual "value" field would be hidden and contain the calculated standardised value (in Pascal in this case). When a search is done for a pressure value or range, then it could be found regardless of unit used during the search. Ideally all measurements would be in S.I., but everyday research is not like this. Hope this makes sense. It's not a priority, IMO, but would be nice. Unless others can come up with more urgent reasons.

@cm-le
Copy link
Author

cm-le commented Jul 13, 2022

One way users could implement this with the above proposal would be

{
    "extra_fields": [
       {
           "name": "pressure_input",
           "label": "Pressure",
           "type": "number",
       },
       {
           "name": "pressure_unit",
           "label": "Unit of pressure measurement",
           "type": "select",
           "value": "Pascal",
           "options": ["Pascal", "bar", "mmHg"]
       },
       {
           "name": "pressure_value",
           "type": "calculated",
           "show_only_if": "false",
           "formula": "ifelse(eq({pressure_unit}, 'Pascal'), {pressure_input},ifelse(eq({pressure_unit}, 'bar'), {pressure_input} * 100000, {pressure_input} * 133.322))"
       }
   ]
}

It might be nice to provide lots of helper functions already for example for unit conversions,
but you can always start small :-)

@killianrochet
Copy link
Contributor

Hi Nicolas, @JuliaSprenger, @SylvainTakerkart
I'm coming back to this idea to know if there was anything new or if you had thought about it in terms of feasibility? It would indeed be very useful for many functions, for example to make a field appear when a certain value has been filled in, these are things we use a lot on our side!

@NicolasCARPi
Copy link
Contributor

I haven't looked into it precisely yet, but I agree it would be an interesting feature.

@EydlinIlya
Copy link

EydlinIlya commented Feb 25, 2023

@NicolasCARPi
Among other benefits, it can help to create user-defined custom id's, as suggested in other issues (i.e. #3724, which you marked as a high priority)

I used similar functionality in Salesforce to create (really) complex id's and was happy with it

@ivan-paleo
Copy link

I would find such functionality very useful as well. In my case, it would be for acquisition settings on imaging instruments.
For example, on the SEM, different settings make sense depending on the detector used (conditional fields).
Also, resolution on a microscope depends on properties of objectives and light source, so it would be great to have a field that calculates automatically to show the user which optical resolution is achieved with the current settings (calculated fields). Similar idea on our CT where the focal spot size is proportional to the energy, which is calculated from acceleration voltage and current. Only the latter two are user inputs, but it would be good for the user to get the former two shown.

@finnock
Copy link

finnock commented Jan 18, 2024

I would be very interested having calculated fields and values. the extra step of manually calculating fields is very error prone since it moves the responsibility from the software to the user.

as a work around this could be solved by using worker scripts which run on a regular basis and fill out all "calculated" fields, but this adds a whole new layer of possible problems.

is this feature something that you plan on implementing, or is it more on the "could be" list?

@NicolasCARPi
Copy link
Contributor

seems very far on the todolist, I'm currently focusing on other matters that I belive are more important to implement. But the whole idea is good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

7 participants