|
| 1 | +""" |
| 2 | +Tests for GMT_DATASET data type. |
| 3 | +""" |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import pandas as pd |
| 8 | +from pygmt.clib import Session |
| 9 | +from pygmt.helpers import GMTTempFile |
| 10 | + |
| 11 | + |
| 12 | +def dataframe_from_pandas(filepath_or_buffer, sep=r"\s+", comment="#"): |
| 13 | + """ |
| 14 | + Read a tabular data as pandas.DataFrame object using pandas.read_csv(). |
| 15 | +
|
| 16 | + The parameters have the same meaning as in ``pandas.read_csv()``. |
| 17 | + """ |
| 18 | + try: |
| 19 | + df = pd.read_csv(filepath_or_buffer, sep=sep, comment=comment, header=None) |
| 20 | + except pd.errors.EmptyDataError: |
| 21 | + # Return an empty DataFrame if the file has no data |
| 22 | + return pd.DataFrame() |
| 23 | + |
| 24 | + # By default, pandas reads text strings with whitespaces as multiple columns, but |
| 25 | + # GMT contacatenates all trailing text as a single string column. Neet do find all |
| 26 | + # string columns (with dtype="object") and combine them into a single string column. |
| 27 | + string_columns = df.select_dtypes(include=["object"]).columns |
| 28 | + if len(string_columns) > 1: |
| 29 | + df[string_columns[0]] = df[string_columns].apply(lambda x: " ".join(x), axis=1) |
| 30 | + df = df.drop(string_columns[1:], axis=1) |
| 31 | + # Convert 'object' to 'string' type |
| 32 | + df = df.convert_dtypes( |
| 33 | + convert_string=True, |
| 34 | + convert_integer=False, |
| 35 | + convert_boolean=False, |
| 36 | + convert_floating=False, |
| 37 | + ) |
| 38 | + return df |
| 39 | + |
| 40 | + |
| 41 | +def dataframe_from_gmt(fname): |
| 42 | + """ |
| 43 | + Read a tabular data as pandas.DataFrame using GMT virtual file. |
| 44 | + """ |
| 45 | + with Session() as lib: |
| 46 | + with lib.virtualfile_out(kind="dataset") as vouttbl: |
| 47 | + lib.call_module("read", f"{fname} {vouttbl} -Td") |
| 48 | + df = lib.virtualfile_to_dataset(vfname=vouttbl) |
| 49 | + return df |
| 50 | + |
| 51 | + |
| 52 | +def test_dataset(benchmark): |
| 53 | + """ |
| 54 | + Test the basic functionality of GMT_DATASET. |
| 55 | + """ |
| 56 | + with GMTTempFile(suffix=".txt") as tmpfile: |
| 57 | + with Path(tmpfile.name).open(mode="w") as fp: |
| 58 | + print(">", file=fp) |
| 59 | + print("1.0 2.0 3.0 TEXT1 TEXT23", file=fp) |
| 60 | + print("4.0 5.0 6.0 TEXT4 TEXT567", file=fp) |
| 61 | + print(">", file=fp) |
| 62 | + print("7.0 8.0 9.0 TEXT8 TEXT90", file=fp) |
| 63 | + print("10.0 11.0 12.0 TEXT123 TEXT456789", file=fp) |
| 64 | + |
| 65 | + # The normal version is: |
| 66 | + # df = dataframe_from_gmt(tmpfile.name) |
| 67 | + # but we want to benchmark the GMT_DATASET->DataFrame conversion. |
| 68 | + df = benchmark(dataframe_from_gmt, tmpfile.name) # The benchmark version |
| 69 | + expected_df = dataframe_from_pandas(tmpfile.name, comment=">") |
| 70 | + pd.testing.assert_frame_equal(df, expected_df) |
| 71 | + |
| 72 | + |
| 73 | +def test_dataset_empty(): |
| 74 | + """ |
| 75 | + Make sure that an empty DataFrame is returned if a file has no data. |
| 76 | + """ |
| 77 | + with GMTTempFile(suffix=".txt") as tmpfile: |
| 78 | + with Path(tmpfile.name).open(mode="w") as fp: |
| 79 | + print("# This is a comment line.", file=fp) |
| 80 | + |
| 81 | + df = dataframe_from_gmt(tmpfile.name) |
| 82 | + assert df.empty # Empty DataFrame |
| 83 | + expected_df = dataframe_from_pandas(tmpfile.name) |
| 84 | + pd.testing.assert_frame_equal(df, expected_df) |
0 commit comments