def test_observation_attributes(db_info): populated_db, _ = db_info attr = "region" value = "1" name = "observation_one" expected = {"attributes": {attr: value}} with StorageApi(rdb_url=populated_db, blob_url=populated_db) as api: api.set_observation_attribute(name, attr, value) with StorageApi(rdb_url=populated_db, blob_url=populated_db) as api: assert api.get_observation_attribute(name, attr) == expected
def test_data(db_info): populated_db, db_lookup = db_info with StorageApi(rdb_url=populated_db, blob_url=populated_db) as api: blob = api.get_data(db_lookup["data_blob"]) assert blob is not None blob = api.get_data("non_existing") assert blob is None
def test_single_observation_misfit_calculation(db_info): populated_db, _ = db_info # observation values_obs = [10.1, 10.2] stds_obs = [1, 3] data_indexes_obs = [2, 3] # response values_res = [11.1, 11.2, 9.9, 9.3] misfit_expected = { "observation_one": [{ "value": ((values_res[index] - obs_value) / obs_std)**2, "sign": values_res[index] - obs_value > 0, "obs_index": obs_index, } for obs_index, ( obs_value, obs_std, index) in enumerate(zip(values_obs, stds_obs, data_indexes_obs))] } with StorageApi(rdb_url=populated_db, blob_url=populated_db) as api: univariate_misfit = api.get_response(ensemble_id=1, response_name="response_one", filter=None) assert (univariate_misfit["realizations"][0]["univariate_misfits"] == misfit_expected)
def test_observation(db_info): populated_db, _ = db_info name = "observation_one" with StorageApi(rdb_url=populated_db, blob_url=populated_db) as api: obs = api.get_observation(name) assert obs == { "attributes": { "region": "1" }, "name": name, "data": { "data_indexes": { "data_ref": 2 }, "key_indexes": { "data_ref": 1 }, "std": { "data_ref": 4 }, "values": { "data_ref": 3 }, }, }
def ensemble_by_id(self, ensemble_id): with StorageApi(rdb_url=self._rdb_url, blob_url=self._blob_url) as api: ensemble = api.get_ensemble(ensemble_id) if ensemble is None: raise werkzeug_exc.NotFound() resolve_ref_uri(ensemble, ensemble_id) return ensemble
def test_ensemble(db_info): populated_db, db_lookup = db_info with StorageApi(rdb_url=populated_db, blob_url=populated_db) as api: schema = api.get_ensemble(db_lookup["ensemble"]) assert schema["name"] == "ensemble_name" assert schema["time_created"] == db_lookup[ "ensemble_timestamp"].isoformat() assert schema["parent"] == {} assert schema["children"] == [] assert schema["ensemble_ref"] == db_lookup["ensemble"] assert { "group": "G", "key": "A", "parameter_ref": db_lookup["parameter_def_A_G"], "prior": { "function": "function", "parameter_names": ["paramA", "paramB"], "parameter_values": [0.5, 0.6], }, } in schema["parameters"] assert {"name": 0, "realization_ref": 0} in schema["realizations"] assert { "name": "response_one", "response_ref": "response_one" } in schema["responses"]
def parameter_by_id(self, ensemble_id, parameter_def_id): with StorageApi(rdb_url=self._rdb_url, blob_url=self._blob_url) as api: parameter = api.get_parameter(ensemble_id, parameter_def_id) if parameter is None: raise werkzeug_exc.NotFound() parameter["alldata_ref"] = None # value is irrelevant resolve_ref_uri(parameter, ensemble_id) return parameter
def get_observation(self, name): """Return an observation.""" with StorageApi(rdb_url=self._rdb_url, blob_url=self._blob_url) as api: obs = api.get_observation(name) resolve_ref_uri(obs) if obs is None: raise werkzeug_exc.NotFound() return obs
def realization_by_id(self, ensemble_id, realization_idx): with StorageApi(rdb_url=self._rdb_url, blob_url=self._blob_url) as api: realization = api.get_realization(ensemble_id, realization_idx, None) if realization is None: raise werkzeug_exc.NotFound() resolve_ref_uri(realization, ensemble_id) return realization
def response_by_name(self, ensemble_id, response_name): with StorageApi(rdb_url=self._rdb_url, blob_url=self._blob_url) as api: response = api.get_response(ensemble_id, response_name, None) if response is None: raise werkzeug_exc.NotFound() response["alldata_ref"] = None # value is irrelevant resolve_ref_uri(response, ensemble_id) return response
def test_realization(db_info): populated_db, db_lookup = db_info with StorageApi(rdb_url=populated_db, blob_url=populated_db) as api: schema = api.get_realization(ensemble_id=db_lookup["ensemble"], realization_idx=0, filter=None) assert schema["name"] == 0 assert len(schema["responses"]) == 2 assert len(schema["parameters"]) == 3
def data(self, data_id): with StorageApi(rdb_url=self._rdb_url, blob_url=self._blob_url) as api: data = api.get_data(data_id) if data is None: raise werkzeug_exc.NotFound() if isinstance(data, list): return ",".join([str(x) for x in data]) else: return str(data)
def test_parameter(db_info): populated_db, db_lookup = db_info with StorageApi(rdb_url=populated_db, blob_url=populated_db) as api: ens_id = db_lookup["ensemble"] par_def_id = db_lookup["parameter_def_key1_group"] schema = api.get_parameter(ensemble_id=ens_id, parameter_def_id=par_def_id) assert schema["key"] == "key1" assert schema["group"] == "group" assert schema["prior"]["function"] == "function"
def test_response(db_info): populated_db, db_lookup = db_info with StorageApi(rdb_url=populated_db, blob_url=populated_db) as api: schema = api.get_response(db_lookup["ensemble"], "response_one", None) assert len(schema["realizations"]) == 2 assert len(schema["observations"]) == 1 assert len(schema["observations"][0]["data"]) == 5 schema = api.get_response(db_lookup["ensemble"], "response_not_existing", None) assert schema is None
def session(self): """Provide a transactional scope around a series of operations.""" session = ERT_STORAGE.Session() try: yield StorageApi(session) session.commit() except: session.rollback() raise finally: session.close()
def get_observation_attributes(self, name): """Return attributes for an observation. { "attributes": {...} } """ with StorageApi(rdb_url=self._rdb_url, blob_url=self._blob_url) as api: attrs = api.get_observation_attributes(name) if attrs is None: raise werkzeug_exc.NotFound() return attrs
def test_ensembles(db_info): populated_db, db_lookup = db_info with StorageApi(rdb_url=populated_db, blob_url=populated_db) as api: schema = api.get_ensembles() assert type(schema["ensembles"]) == list assert { "name": "ensemble_name", "time_created": db_lookup["ensemble_timestamp"].isoformat(), "parent": {}, "children": [], "ensemble_ref": db_lookup["ensemble"], } in schema["ensembles"]
def generator(): with StorageApi(rdb_url=self._rdb_url, blob_url=self._blob_url) as api: first = True for data in api.get_datas(ids): if first: first = False else: yield "\n" if isinstance(data, list): yield ",".join([str(x) for x in data]) else: yield str(data)
def test_priors(db_info): populated_db, db_lookup = db_info with StorageApi(rdb_url=populated_db, blob_url=populated_db) as api: schema = api.get_ensemble(db_lookup["ensemble"]) assert { "group": "group", "key": "key1", "prior": { "function": "function", "parameter_names": ["paramA", "paramB"], "parameter_values": [0.1, 0.2], }, "parameter_ref": 3, } in schema["parameters"]
def session(self): """Provide a transactional scope around a series of operations.""" rdb_session = ERT_STORAGE.RdbSession() blob_session = ERT_STORAGE.BlobSession() rdb_api = RdbApi(session=rdb_session) blob_api = BlobApi(session=blob_session) try: yield StorageApi(rdb_api, blob_api) rdb_session.commit() blob_session.commit() except: rdb_session.rollback() blob_session.rollback() raise finally: rdb_session.close() blob_session.close()
def set_observation_attributes(self, name): """Set attributes on an observation. The posted JSON will be expected to be { "attributes": { "region": "1", "depth": "2892.1" } } """ with StorageApi(rdb_url=self._rdb_url, blob_url=self._blob_url) as api: js = request.get_json() if not js["attributes"]: raise werkzeug_exc.BadRequest() for k, v in js["attributes"].items(): obs = api.set_observation_attribute(name, k, v) if obs is None: raise werkzeug_exc.NotFound() return api.get_observation(name), 201
def storage_api(db_apis): rdb_api, blob_api, db_lookup = db_apis yield StorageApi(rdb_api=rdb_api, blob_api=blob_api), db_lookup
def test_nonexisiting_parameter(db_info): populated_db, db_lookup = db_info with StorageApi(rdb_url=populated_db, blob_url=populated_db) as api: schema = api.get_parameter(ensemble_id=db_lookup["ensemble"], parameter_def_id="1293495") assert schema is None
def storage_api(db_api): api, db_lookup = db_api yield StorageApi(session=ERT_STORAGE.Session()), db_lookup
def ensembles(self): with StorageApi(rdb_url=self._rdb_url, blob_url=self._blob_url) as api: ensembles = api.get_ensembles() resolve_ref_uri(ensembles) return ensembles
def response_data_by_name(self, ensemble_id, response_name): with StorageApi(rdb_url=self._rdb_url, blob_url=self._blob_url) as api: ids = api.get_response_data(ensemble_id, response_name) if ids is None: raise werkzeug_exc.NotFound() return self._datas(ids)
def parameter_data_by_id(self, ensemble_id, parameter_def_id): with StorageApi(rdb_url=self._rdb_url, blob_url=self._blob_url) as api: ids = api.get_parameter_data(ensemble_id, parameter_def_id) if ids is None: raise werkzeug_exc.NotFound() return self._datas(ids)