def get_ensemble_responses(*, res: LibresFacade = Depends(get_res), ensemble_id: UUID) -> Mapping[str, js.RecordOut]: response_map: Mapping[str, js.RecordOut] = {} case = get_name("ensemble", ensemble_id) for response_name in get_response_names(): obs_keys = res.observation_keys(response_name) observations_list = [ js.ObservationOut( id=uuid4(), userData=[], errors=obs["errors"], values=obs["values"], x_axis=obs["x_axis"], name=obs["name"], ) for obs in observations_for_obs_keys(case, obs_keys) ] response_map[str(response_name)] = js.RecordOut( id=get_id(f"response", f"{ensemble_id}/{response_name}"), name=response_name, userdata={}, observations=observations_list, ) return response_map
def get_ensemble(*, res: LibresFacade = Depends(get_res), ensemble_id: UUID) -> js.EnsembleOut: return js.EnsembleOut( id=ensemble_id, children=[], parent=None, experiment_id=get_id("experiment", "default"), userdata={"name": get_name("ensemble", ensemble_id)}, size=get_size(get_name("ensemble", ensemble_id)), parameter_names=ensemble_parameter_names( get_name("ensemble", ensemble_id)), response_names=get_response_names(), child_ensemble_ids=[], )
async def get_ensemble_record( *, res: LibresFacade = Depends(get_res), name: str, ensemble_id: UUID, accept: str = Header("application/json"), realization_index: Optional[int] = None, label: Optional[str] = None, ) -> Any: ensemble_name = get_name("ensemble", ensemble_id) dataframe = data_for_key(ensemble_name, name, realization_index) if realization_index is not None: # dataframe.loc returns a Series, and when we reconstruct a DataFrame from a Series, it defaults to be # oriented the wrong way, so we must transpose it dataframe = pd.DataFrame(dataframe.loc[realization_index]).T if accept == "application/x-parquet": dataframe.columns = [str(s) for s in dataframe.columns] stream = io.BytesIO() dataframe.to_parquet(stream) return Response( content=stream.getvalue(), media_type=accept, ) else: return Response( content=dataframe.to_csv().encode(), media_type="text/csv", )
async def get_ensemble_response_dataframe( *, res: LibresFacade = Depends(get_res), ensemble_id: UUID, response_name: str ) -> Response: ensemble_name = get_name("ensemble", ensemble_id) dataframe = data_for_key(res, ensemble_name, response_name) return Response( content=dataframe.to_csv().encode(), media_type="text/csv", )
async def get_record_observations( *, res: LibresFacade = Depends(get_res), ensemble_id: UUID, name: str, realization_index: Optional[int] = None, ) -> List[js.ObservationOut]: obs_keys = res.observation_keys(name) case = get_name("ensemble", ensemble_id) obss = observations_for_obs_keys(case, obs_keys) return [ js.ObservationOut( id=uuid4(), userData=[], errors=obs["errors"], values=obs["values"], x_axis=obs["x_axis"], name=obs["name"], ) for obs in obss ]
async def get_response_misfits( *, res: LibresFacade = Depends(get_res), ensemble_id: UUID, response_name: str, realization_index: Optional[int] = None, summary_misfits: bool = False, ) -> Response: ensemble_name = get_name("ensemble", ensemble_id) dataframe = data_for_key(res, ensemble_name, response_name) if realization_index is not None: dataframe = pd.DataFrame(dataframe.loc[realization_index]).T response_dict = {} for index, data in dataframe.iterrows(): data_df = pd.DataFrame(data).T response_dict[index] = data_df obs_keys = res.observation_keys(response_name) obs = observations_for_obs_keys(res, ensemble_name, obs_keys) if not obs_keys: raise ValueError(f"No observations for key {response_name}") if not obs: raise ValueError(f"Cant fetch observations for key {response_name}") o = obs[0] def parse_index(x): try: return int(x) except ValueError: return parse(x) observation_df = pd.DataFrame( data={ "values": o["values"], "errors": o["errors"] }, index=[parse_index(x) for x in o["x_axis"]], ) try: result_df = calculate_misfits_from_pandas(response_dict, observation_df, summary_misfits) except Exception as misfits_exc: raise exc.UnprocessableError( f"Unable to compute misfits: {misfits_exc}") from misfits_exc return Response( content=result_df.to_csv().encode(), media_type="text/csv", )
async def get_ensemble_parameters(*, res: LibresFacade = Depends(get_res), ensemble_id: UUID) -> List[dict]: ensemble_name = get_name(type="ensemble", uuid=ensemble_id) return ensemble_parameters(ensemble_name)
def resolve_ensemble(root: Any, info: ResolveInfo, id: str) -> None: return get_name("ensemble", id)