ContactEngineering / ce-ui

User interface of contact.engineering
https://contact.engineering
MIT License
0 stars 0 forks source link

Move unit conversion for plots to frontend #36

Open pastewka opened 8 months ago

pastewka commented 8 months ago

Is your feature request related to a problem? Please describe. Analysis results are reported in some unit (typically one choses such that data values are close to unity), but when displaying in the frontend in a plot combining different results these units must be converted. All unit conversion currently happens in the backend in topobank.analysis.views via the Python library pint. This means the backend must actually inspect all analysis results. To minimize loading data from the S3 system, it would be better if unit conversion happened in the frontend where the data needs to be loaded anyway.

The first step for this would be to check if there is a JavaScript library that can do unit conversion, e.g. convert nm^3 to µm^3 etc.

This is a first step towards implementing ContactEngineering/ce-ui#16

SickSmile1 commented 8 months ago

preferred Packages: https://github.com/convert-units/convert-units#packaged-units ( -> example: convert(900).from('mm').toBest({ cutOffNumber: 10 }); )

https://github.com/josdejong/mathjs

alternatives: https://github.com/moment/moment/ (- last commit 2022, + has biggest user base)

pastewka commented 8 months ago

I think I had used convert-units in the past and liked it. mathjs looks like it tries to do too much. moment appears deprecated. I say go for convert-units.

SickSmile1 commented 8 months ago

I'm not sure, i understand, where i can start changing the unit conversion from backend to frontend... I started looking at topobank/analysis/views.py in lines 191++ Then i found the function: "_get_si_unit_conversion(result):" in /topobank/analysis/downloads.py , but as i understand for now, the conversion happens before/while saving the topography/surface on the S3, and the views.py just checks if the received data has a unit and, if not, changes it to "m" instead. So it seems that the best way would be to just change the units and the axis in the frontend -> redraw plot, without changing the data in the backend?

pastewka commented 8 months ago

There is unit conversion that happens when the analysis stores to S3, but you can ignore this. When loading multiple results, the units may be different. What the code is doing it is looking into the first analysis results and uses that to decide the unit used for displaying the results. If there is None, then it assumes "m" otherwise it is using that first unit. All other analysis results are converted to the respective unit here:

https://github.com/ContactEngineering/topobank/blob/555ae10bbc41c610483455c0d7b36c84f1d7fdc0/topobank/analysis/views.py#L338

Note that the actual conversion (i.e. multiplication with the analysis_xscale factor) is done in the frontend even now, but the conversion factor is currently computed by the backend. The first step is to compute that conversion factor in the frontend.

SickSmile1 commented 4 months ago

Data source does not contain the units. The units are only visible in results.json (it's easier to find the file through minio webview, the login is in .envs/.django).