def test_roundtrip_resinsight(filename, readonly_testdata_dir): """Test converting all test data sets in testdir into resinsight and back again. ResInsight only supports SUMMARY_OBSERVATION. """ dframe = autoparse_file(filename)[1] # Reduce to the subset supported by yaml: dframe = dframe[dframe["CLASS"] == "SUMMARY_OBSERVATION"].dropna( axis="columns", how="all") # Drop observations with no date: dframe = dframe[~dframe["DATE"].isna()].dropna(axis=1, how="all") # Convert to ResInsight dataframe format and back again: ri_dframe = df2resinsight_df(dframe) ri_roundtrip_dframe = resinsight_df2df(ri_dframe) # LABEL is not part of the ResInsight format, and a made-up label # is obtained through the roundtrip (when importing back). Skip it # when comparing. pd.testing.assert_frame_equal( ri_roundtrip_dframe.sort_index(axis="columns").drop( ["LABEL", "COMMENT", "SUBCOMMENT"], axis="columns", errors="ignore"), dframe.sort_index(axis="columns").drop( ["LABEL", "COMMENT", "SUBCOMMENT"], axis="columns", errors="ignore"), check_like=True, )
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_df2resinsight_df(obs_df, expected_ri_df): """Test that we can go from internal dataframe representation to the resinsight dataframe representation of observations (which only supports a subset of ERT observations)""" pd.testing.assert_frame_equal(df2resinsight_df(obs_df), expected_ri_df)