the-grey-group / datalab

datalab is a place to store experimental data and the connections between them.
https://the-datalab.readthedocs.io/en/latest
MIT License
39 stars 15 forks source link

Fix Chemical formula formatting of formulas such as Na2/3CoO2 and Ni(NO3)2.6H2O #634

Open jdbocarsly opened 3 months ago

MJCliffe commented 1 month ago

This might be the same issue, but if you put in the chemical formula section (NH4)2(SO4) it renders as NH$_4$SO$_4$ - this is pretty important for molecular stuff. Might be nice to have it also expand it to the empirical formula & molecular mass?

jdbocarsly commented 1 month ago

Yes, this is part of the same issue. I've been meaning to fix this for a while, so will go on and try to come up with something that works nicely across a wider range of cases. For the mean time, the formula is actually just stored as a string in the database, so even though the display is incorrect, your data should be intact (you can see the actual data stored by clicking in the field in the UI)

ml-evs commented 1 month ago

I think we should also have a fail-safe, if the entire formula cannot be captured by whatever regexp we conjure up for the formatting, it should just show the formula unformatted.

jdbocarsly commented 1 month ago

I think we should also have a fail-safe, if the entire formula cannot be captured by whatever regexp we conjure up for the formatting, it should just show the formula unformatted.

good idea

I am also am thinking of all the ways we often express formulas, like Li1-xCoO2 if we aren't sure about x. It probably will be hard to properly handle everything people want to do, but we can certainly do a bit better.

MJCliffe commented 1 month ago

This package might help: https://pypi.org/project/chemformula/#:~:text=Description-,ChemFormula%20is%20a%20Python%20class%20for%20working%20with%20chemical%20formulas,4%E2%8B%85H2O.

Doesn't do variable occupancy - I guess it depends what is required.

ml-evs commented 1 month ago

Trouble is this part of the code is JavaScript (though I'm sure a similar package exists) -- we could do the parsing on the server side but it would need another round-trip so probably want to avoid it