def test_roundtrip_ertobs(filename, readonly_testdata_dir): """Test converting all included test data sets into ERT observations (as strings) and then parsing it, ensuring that we end up in the same place""" dframe = autoparse_file(filename)[1] # Convert to ERT obs format and back again: ertobs_str = df2ertobs(dframe) ert_roundtrip_dframe = ertobs2df(ertobs_str) ert_roundtrip_dframe.set_index("CLASS", inplace=True) dframe.set_index("CLASS", inplace=True) # This big loop is only here to aid in debugging when # the dataframes do not match, asserting equivalence of # subframes for _class in dframe.index.unique(): roundtrip_subframe = ( ert_roundtrip_dframe.loc[[_class]] .dropna(axis=1, how="all") .sort_index(axis=1) ) subframe = dframe.loc[[_class]].dropna(axis=1, how="all").sort_index(axis=1) roundtrip_subframe.set_index( list( {"CLASS", "LABEL", "OBS", "SEGMENT"}.intersection( set(roundtrip_subframe.columns) ) ), inplace=True, ) roundtrip_subframe.sort_index(inplace=True) subframe.set_index( list( {"CLASS", "LABEL", "OBS", "SEGMENT"}.intersection(set(subframe.columns)) ), inplace=True, ) subframe.sort_index(inplace=True) # Comments are not preservable through ertobs roundtrips: subframe.drop( ["COMMENT", "SUBCOMMENT"], axis="columns", errors="ignore", inplace=True ) if _class == "BLOCK_OBSERVATION": if "WELL" in subframe: # WELL as used in yaml is not preservable in roundtrips del subframe["WELL"] # print(roundtrip_subframe) # print(subframe) pd.testing.assert_frame_equal( roundtrip_subframe.sort_index(), subframe.sort_index(), check_dtype=False, )
def test_ertobs2df(string, expected): """Test converting all the way from ERT observation format to a Pandas Dataframe works as expected (this includes many of the other functions that are also tested individually)""" dframe = ertobs2df(string) pd.testing.assert_frame_equal(dframe.sort_index(axis=1), expected.sort_index(axis=1), check_dtype=False) pd.testing.assert_frame_equal( ertobs2df(df2ertobs(dframe)).sort_index(axis=1), dframe.sort_index(axis=1)) # Round-trip test via yaml: if "DATE" not in expected: return round_trip_yaml_dframe = obsdict2df(df2obsdict(dframe)) pd.testing.assert_frame_equal(round_trip_yaml_dframe.sort_index(axis=1), dframe.sort_index(axis=1))
def dump_results( dframe, csvfile=None, yamlfile=None, resinsightfile=None, ertfile=None ): """Dump dataframe with ERT observations to CSV and/or YML format to disk. Writes to stdout if filenames are "-". Skips export if filenames are empty or None. Args: dframe (pd.DataFrame) csvfile (str): Filename yamlfile (str): Filename resinsightfile (str): Filename ertfile (str): Filename """ if not (csvfile or yamlfile or resinsightfile or ertfile): logger.warning("No output filenames provided") if csvfile: if csvfile != __MAGIC_STDOUT__: logger.info("Writing observations as CSV to %s", csvfile) dframe.to_csv(csvfile, index=False) else: # Ignore pipe errors when writing to stdout: signal.signal(signal.SIGPIPE, signal.SIG_DFL) dframe.to_csv(sys.stdout, index=False) if yamlfile and yamlfile: obs_dict_for_yaml = df2obsdict(dframe) if not obs_dict_for_yaml and not dframe.empty: logger.error("None of your observations are supported in YAML") yaml_str = yaml.safe_dump(obs_dict_for_yaml) if yamlfile != __MAGIC_STDOUT__: logger.info( "Writing observations in YAML (webviz) format to file: %s", resinsightfile, ) with open(yamlfile, "w") as f_handle: f_handle.write(yaml_str) else: print(yaml_str) if resinsightfile: ri_dframe = df2resinsight_df(dframe) if resinsightfile != __MAGIC_STDOUT__: logger.info( "Writing observations in ResInsight format to CSV-file: %s", resinsightfile, ) ri_dframe.to_csv(resinsightfile, index=False, sep=";") else: # Ignore pipe errors when writing to stdout: signal.signal(signal.SIGPIPE, signal.SIG_DFL) ri_dframe.to_csv(sys.stdout, index=False, sep=";") if ertfile: ertobs_str = df2ertobs(dframe) if ertfile != __MAGIC_STDOUT__: with open(ertfile, "w") as f_handle: logger.info("Writing ERT observation format to %s", ertfile) f_handle.write(ertobs_str) else: print(ertobs_str)
def test_df2ertobs(obs_df, expected_str): """Test making any kind of *OBSERVATION in ert format from dataframe format""" # Relaxed on whitespace assert df2ertobs(obs_df).strip().replace("\n", "").replace( " ", " ") == expected_str.strip().replace("\n", "").replace(" ", " ")