11from __future__ import annotations
2+
3+ import io
24import time
35from uuid import UUID
46from pydantic .alias_generators import to_camel
57from pydantic import BaseModel , ConfigDict , RootModel , Field
68from typing_extensions import Doc
7- from typing import Annotated , Any , Literal
9+ from typing import Annotated , Any , Literal , Iterator
810import httpx
11+ import pandas as pd
912
1013
1114DEFAULT_API_URL = "https://backend-acidwatch-prod.radix.equinor.com"
@@ -38,6 +41,24 @@ class Model(BaseModel):
3841 parameters : dict [str , _Parameter ]
3942
4043
44+ class Models (RootModel [list [Model ]]):
45+ def __getitem__ (self , item : int ) -> Model :
46+ return self .root [item ]
47+
48+ def __iter__ (self ) -> Iterator [Model ]: # type: ignore
49+ return self .root .__iter__ ()
50+
51+ def _repr_markdown_ (self ) -> str :
52+ out = io .StringIO ()
53+ print ("| # | ModelID | Name |" , file = out )
54+ print ("|---|---------|------|" , file = out )
55+
56+ for index , model in enumerate (self ):
57+ print (f"| { index } | { model .model_id } | { model .display_name } |" , file = out )
58+
59+ return out .getvalue ()
60+
61+
4162class _IndivitualResult (BaseModel ):
4263 concentrations : dict [str , float ]
4364
@@ -74,15 +95,10 @@ def __init__(
7495 ) -> None :
7596 super ().__init__ (base_url = api_url )
7697
77- def list_models (self ) -> list [ Model ] :
98+ def list_models (self ) -> Models :
7899 resp = self .get ("/models" )
79-
80100 assert resp .status_code == 200
81-
82- root_model = RootModel [list [Model ]]
83- object = root_model .model_validate_json (resp .content )
84-
85- return object .root
101+ return Models .model_validate_json (resp .content )
86102
87103 def run_model (
88104 self ,
@@ -93,7 +109,7 @@ def run_model(
93109 temperature : float = 25 ,
94110 pressure : float = 10 ,
95111 retries : int = 9999 ,
96- ) -> SimulationResult :
112+ ) -> pd . DataFrame :
97113 response = self .post (
98114 "/simulations" ,
99115 json = {
@@ -123,7 +139,16 @@ def run_model(
123139
124140 res = _SimulationResult .model_validate_json (response .content )
125141 if isinstance (res .root , SimulationResult ):
126- return res .root
142+ substances : set [str ] = set ()
143+ for r in res .root .results :
144+ substances |= set (r .concentrations .keys ())
145+
146+ return pd .DataFrame (
147+ {
148+ s : [r .concentrations [s ] for r in res .root .results ]
149+ for s in substances
150+ }
151+ )
127152
128153 time .sleep (0.5 )
129154
0 commit comments