gafusion / omas

Ordered Multidimensional Array Structure
http://gafusion.github.io/omas
MIT License
30 stars 14 forks source link

OMAS enhancement proposals #261

Closed kripnerl closed 5 months ago

kripnerl commented 10 months ago

Hi,

since I have been working with OMAS package lately, I have found some things that may be pretty handy to implement.

Feel free to check the Streamlit app, even though it is trivial. I find it pretty handy.

Support "+" operator

This one is simple:

ods1 = omas.ODS()
ods1.sample_magnetics()

ods2 = omas.ODS()
ods2.sample_pf_active()

ods3 = ods1 + ods2

An implementation may be pretty straightforward:

def plus(ods1, ods2):
    ods_res  = omas.ODS()
    ods_res.update(ods1)
    ods_res.update(ods2)
    return ods_res

not sure how memory heavy this is...

Support ByteIO in "load" function.

This one is quite important with the increase in the number of web services. I have created a simple Streamlit OMAS viewer:

import os

import omas
from omas import ODS
from omas.omas_utils import json_dumper
from tempfile import NamedTemporaryFile

import streamlit as st
import json

import matplotlib.pyplot as plt

def ods2json(ods):
    kw = dict()
    kw.setdefault('indent', 0)
    kw.setdefault('separators', (',', ': '))
    kw.setdefault('sort_keys', True)

    json_string = json.dumps(ods, default=lambda x: json_dumper(x, None), **kw)

    return json_string

def load_uploaded_omas_file(file):
    _, suffix = os.path.splitext(file.name)

    with NamedTemporaryFile(suffix=suffix, delete=False) as temp:
        temp.write(file.getvalue())
        temp_file_path = temp.name

    ods = ODS().load(temp_file_path)
    os.remove(temp_file_path)
    return ods

ods_file = st.file_uploader("Choose OMAS IDS")

if ods_file is not None:
    plot_tab, tree_tab = st.tabs(["Plot", "Tree"])

    # ods_obj = ODS().load("./machine@2022.05v2.h5")
    ods_obj = load_uploaded_omas_file(ods_file)

    with plot_tab:
        fig = plt.figure()
        ods_obj.plot_overlay(wall=True, pf_active=True, pf_passive=True)
        st.pyplot(fig)

    with tree_tab:
        ods_json = ods2json(ods_obj)
        st.json(ods_json, expanded=False)

image

with installed Streamlit, you can run it as streamlit run app.py.

However, you may notice load_uploaded_omas_file function here. I need to create a temporary file, save and reload the downloaded IDS. I can imagine a lot of similar applications when working with IDS/OMAS data.

ods2json function.

As mentioned above, working with JSON "in" memory" is handy. Esp. when JSON saving function supports only the filename and no file object.

Support pathlib

Fallowing example

import pathlib
import omas

path = pathlib.Path("my_omas.h5")
ods = omas.ODS()
ods.load(path)

should be equivalent to

# ...

ods.load(str(path))
# or
ods.load("my_omas.h5")
orso82 commented 10 months ago

Thank you @kripnerl ! I am very glad that OMAS is working for you, and these are all excellent suggestions.

I was not aware of Streamlit. It seems pretty neat. This is something that perhaps @AreWeDreaming @wdeshazer and @smithsp can investigate!

For the other two proposals (+ operator and pathlib) where you planning of opening a PR?

Thanks again!

kripnerl commented 10 months ago

Hi @orso82, not at the moment, since I am quite overloaded; however, it may happened 😊. No promises.

Main concept of streamlit is that whole scripted is rerun on any event. It is excellent for prototypes of dashboards. However, heavy calculations may make it unusable.

nicegui project seems to be also very promising. It grans much more freedom in the look and behaviour. For the cost of slightly more complicated code.

Cheers, Lukáš

github-actions[bot] commented 8 months ago

Stale issue message

kripnerl commented 8 months ago

On hold and on my TODO.

github-actions[bot] commented 6 months ago

Stale issue message