|
| 1 | +""" |
| 2 | +Test the Session.virtualfile_from_vectors method. |
| 3 | +""" |
| 4 | + |
| 5 | +from importlib.util import find_spec |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import pandas as pd |
| 9 | +import pytest |
| 10 | +from pygmt import clib |
| 11 | +from pygmt.exceptions import GMTInvalidInput |
| 12 | +from pygmt.helpers import GMTTempFile |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(scope="module", name="dtypes") |
| 16 | +def fixture_dtypes(): |
| 17 | + """ |
| 18 | + List of supported numpy dtypes. |
| 19 | + """ |
| 20 | + return "int8 int16 int32 int64 uint8 uint16 uint32 uint64 float32 float64".split() |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture(scope="module", name="dtypes_pandas") |
| 24 | +def fixture_dtypes_pandas(dtypes): |
| 25 | + """ |
| 26 | + List of supported pandas dtypes. |
| 27 | + """ |
| 28 | + dtypes_pandas = dtypes.copy() |
| 29 | + |
| 30 | + if find_spec("pyarrow") is not None: |
| 31 | + dtypes_pandas.extend([f"{dtype}[pyarrow]" for dtype in dtypes_pandas]) |
| 32 | + |
| 33 | + return tuple(dtypes_pandas) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.benchmark |
| 37 | +def test_virtualfile_from_vectors(dtypes): |
| 38 | + """ |
| 39 | + Test the automation for transforming vectors to virtual file dataset. |
| 40 | + """ |
| 41 | + size = 10 |
| 42 | + for dtype in dtypes: |
| 43 | + x = np.arange(size, dtype=dtype) |
| 44 | + y = np.arange(size, size * 2, 1, dtype=dtype) |
| 45 | + z = np.arange(size * 2, size * 3, 1, dtype=dtype) |
| 46 | + with clib.Session() as lib: |
| 47 | + with lib.virtualfile_from_vectors(x, y, z) as vfile: |
| 48 | + with GMTTempFile() as outfile: |
| 49 | + lib.call_module("info", [vfile, f"->{outfile.name}"]) |
| 50 | + output = outfile.read(keep_tabs=True) |
| 51 | + bounds = "\t".join([f"<{i.min():.0f}/{i.max():.0f}>" for i in (x, y, z)]) |
| 52 | + expected = f"<vector memory>: N = {size}\t{bounds}\n" |
| 53 | + assert output == expected |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.benchmark |
| 57 | +@pytest.mark.parametrize("dtype", [str, object]) |
| 58 | +def test_virtualfile_from_vectors_one_string_or_object_column(dtype): |
| 59 | + """ |
| 60 | + Test passing in one column with string or object dtype into virtual file dataset. |
| 61 | + """ |
| 62 | + size = 5 |
| 63 | + x = np.arange(size, dtype=np.int32) |
| 64 | + y = np.arange(size, size * 2, 1, dtype=np.int32) |
| 65 | + strings = np.array(["a", "bc", "defg", "hijklmn", "opqrst"], dtype=dtype) |
| 66 | + with clib.Session() as lib: |
| 67 | + with lib.virtualfile_from_vectors(x, y, strings) as vfile: |
| 68 | + with GMTTempFile() as outfile: |
| 69 | + lib.call_module("convert", [vfile, f"->{outfile.name}"]) |
| 70 | + output = outfile.read(keep_tabs=True) |
| 71 | + expected = "".join( |
| 72 | + f"{i}\t{j}\t{k}\n" for i, j, k in zip(x, y, strings, strict=True) |
| 73 | + ) |
| 74 | + assert output == expected |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize("dtype", [str, object]) |
| 78 | +def test_virtualfile_from_vectors_two_string_or_object_columns(dtype): |
| 79 | + """ |
| 80 | + Test passing in two columns of string or object dtype into virtual file dataset. |
| 81 | + """ |
| 82 | + size = 5 |
| 83 | + x = np.arange(size, dtype=np.int32) |
| 84 | + y = np.arange(size, size * 2, 1, dtype=np.int32) |
| 85 | + # Catch bug in https://github.com/GenericMappingTools/pygmt/pull/2719 |
| 86 | + strings1 = np.array(["a", "bc", "def", "ghij", "klmnolooong"], dtype=dtype) |
| 87 | + strings2 = np.array(["pqrst", "uvwx", "yz!", "@#", "$"], dtype=dtype) |
| 88 | + with clib.Session() as lib: |
| 89 | + with lib.virtualfile_from_vectors(x, y, strings1, strings2) as vfile: |
| 90 | + with GMTTempFile() as outfile: |
| 91 | + lib.call_module("convert", [vfile, f"->{outfile.name}"]) |
| 92 | + output = outfile.read(keep_tabs=True) |
| 93 | + expected = "".join( |
| 94 | + f"{h}\t{i}\t{j} {k}\n" |
| 95 | + for h, i, j, k in zip(x, y, strings1, strings2, strict=True) |
| 96 | + ) |
| 97 | + assert output == expected |
| 98 | + |
| 99 | + |
| 100 | +def test_virtualfile_from_vectors_transpose(dtypes): |
| 101 | + """ |
| 102 | + Test transforming matrix columns to virtual file dataset. |
| 103 | + """ |
| 104 | + shape = (7, 5) |
| 105 | + for dtype in dtypes: |
| 106 | + data = np.arange(shape[0] * shape[1], dtype=dtype).reshape(shape) |
| 107 | + with clib.Session() as lib: |
| 108 | + with lib.virtualfile_from_vectors(*data.T) as vfile: |
| 109 | + with GMTTempFile() as outfile: |
| 110 | + lib.call_module("info", [vfile, "-C", f"->{outfile.name}"]) |
| 111 | + output = outfile.read(keep_tabs=True) |
| 112 | + bounds = "\t".join([f"{col.min():.0f}\t{col.max():.0f}" for col in data.T]) |
| 113 | + expected = f"{bounds}\n" |
| 114 | + assert output == expected |
| 115 | + |
| 116 | + |
| 117 | +def test_virtualfile_from_vectors_diff_size(): |
| 118 | + """ |
| 119 | + Test the function fails for arrays of different sizes. |
| 120 | + """ |
| 121 | + x = np.arange(5) |
| 122 | + y = np.arange(6) |
| 123 | + with clib.Session() as lib: |
| 124 | + with pytest.raises(GMTInvalidInput): |
| 125 | + with lib.virtualfile_from_vectors(x, y): |
| 126 | + pass |
| 127 | + |
| 128 | + |
| 129 | +def test_virtualfile_from_vectors_pandas(dtypes_pandas): |
| 130 | + """ |
| 131 | + Pass vectors to a dataset using pandas.Series, checking both numpy and pyarrow |
| 132 | + dtypes. |
| 133 | + """ |
| 134 | + size = 13 |
| 135 | + |
| 136 | + for dtype in dtypes_pandas: |
| 137 | + data = pd.DataFrame( |
| 138 | + data={ |
| 139 | + "x": np.arange(size), |
| 140 | + "y": np.arange(size, size * 2, 1), |
| 141 | + "z": np.arange(size * 2, size * 3, 1), |
| 142 | + }, |
| 143 | + dtype=dtype, |
| 144 | + ) |
| 145 | + with clib.Session() as lib: |
| 146 | + with lib.virtualfile_from_vectors(data.x, data.y, data.z) as vfile: |
| 147 | + with GMTTempFile() as outfile: |
| 148 | + lib.call_module("info", [vfile, f"->{outfile.name}"]) |
| 149 | + output = outfile.read(keep_tabs=True) |
| 150 | + bounds = "\t".join( |
| 151 | + [f"<{i.min():.0f}/{i.max():.0f}>" for i in (data.x, data.y, data.z)] |
| 152 | + ) |
| 153 | + expected = f"<vector memory>: N = {size}\t{bounds}\n" |
| 154 | + assert output == expected |
| 155 | + |
| 156 | + |
| 157 | +def test_virtualfile_from_vectors_arraylike(): |
| 158 | + """ |
| 159 | + Pass array-like vectors to a dataset. |
| 160 | + """ |
| 161 | + size = 13 |
| 162 | + x = list(range(0, size, 1)) |
| 163 | + y = tuple(range(size, size * 2, 1)) |
| 164 | + z = range(size * 2, size * 3, 1) |
| 165 | + with clib.Session() as lib: |
| 166 | + with lib.virtualfile_from_vectors(x, y, z) as vfile: |
| 167 | + with GMTTempFile() as outfile: |
| 168 | + lib.call_module("info", [vfile, f"->{outfile.name}"]) |
| 169 | + output = outfile.read(keep_tabs=True) |
| 170 | + bounds = "\t".join([f"<{min(i):.0f}/{max(i):.0f}>" for i in (x, y, z)]) |
| 171 | + expected = f"<vector memory>: N = {size}\t{bounds}\n" |
| 172 | + assert output == expected |
0 commit comments