Skip to content

Commit ed591a3

Browse files
seismanMax Jonesweiji14
authored
Refactor text to use virtualfile_from_data (#1121)
Co-authored-by: Max Jones <[email protected]> Co-authored-by: Wei Ji <[email protected]>
1 parent e56429c commit ed591a3

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

pygmt/src/text.py

+19-22
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from pygmt.helpers import (
88
build_arg_string,
99
data_kind,
10-
dummy_context,
1110
fmt_docstring,
1211
is_nonstr_iter,
1312
kwargs_to_strings,
@@ -164,24 +163,27 @@ def text_(
164163
for texts, but this option is only valid if using x/y/text.
165164
{w}
166165
"""
167-
168-
# pylint: disable=too-many-locals
169166
# pylint: disable=too-many-branches
170-
171167
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
172168

173169
# Ensure inputs are either textfiles, x/y/text, or position/text
174170
if position is None:
171+
if (x is not None or y is not None) and textfiles is not None:
172+
raise GMTInvalidInput(
173+
"Provide either position only, or x/y pairs, or textfiles."
174+
)
175175
kind = data_kind(textfiles, x, y, text)
176+
if kind == "vectors" and text is None:
177+
raise GMTInvalidInput("Must provide text with x/y pairs")
176178
else:
177-
if x is not None or y is not None:
179+
if x is not None or y is not None or textfiles is not None:
178180
raise GMTInvalidInput(
179-
"Provide either position only, or x/y pairs, not both"
181+
"Provide either position only, or x/y pairs, or textfiles."
180182
)
181-
kind = "vectors"
182-
183-
if kind == "vectors" and text is None:
184-
raise GMTInvalidInput("Must provide text with x/y pairs or position")
183+
if text is None or is_nonstr_iter(text):
184+
raise GMTInvalidInput("Text can't be None or array.")
185+
kind = None
186+
textfiles = ""
185187

186188
# Build the -F option in gmt text.
187189
if kwargs.get("F") is None and (
@@ -219,18 +221,13 @@ def text_(
219221
extra_arrays.append(kwargs["t"])
220222
kwargs["t"] = ""
221223

224+
# Append text at last column. Text must be passed in as str type.
225+
if kind == "vectors":
226+
extra_arrays.append(np.atleast_1d(text).astype(str))
227+
222228
with Session() as lib:
223-
file_context = dummy_context(textfiles) if kind == "file" else ""
224-
if kind == "vectors":
225-
if position is not None:
226-
file_context = dummy_context("")
227-
else:
228-
file_context = lib.virtualfile_from_vectors(
229-
np.atleast_1d(x),
230-
np.atleast_1d(y),
231-
*extra_arrays,
232-
# text must be in str type, see issue #706
233-
np.atleast_1d(text).astype(str),
234-
)
229+
file_context = lib.virtualfile_from_data(
230+
check_kind="vector", data=textfiles, x=x, y=y, extra_arrays=extra_arrays
231+
)
235232
with file_context as fname:
236233
lib.call_module(module="text", args=build_arg_string(kwargs, infile=fname))

pygmt/tests/test_text.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# pylint: disable=redefined-outer-name
21
"""
32
Tests text.
43
"""
@@ -15,16 +14,16 @@
1514
CITIES_DATA = os.path.join(TEST_DATA_DIR, "cities.txt")
1615

1716

18-
@pytest.fixture(scope="module")
19-
def projection():
17+
@pytest.fixture(scope="module", name="projection")
18+
def fixture_projection():
2019
"""
2120
The projection system.
2221
"""
2322
return "x10c"
2423

2524

26-
@pytest.fixture(scope="module")
27-
def region():
25+
@pytest.fixture(scope="module", name="region")
26+
def fixture_region():
2827
"""
2928
The data region.
3029
"""
@@ -124,15 +123,25 @@ def test_text_position(region):
124123
return fig
125124

126125

127-
def test_text_xy_with_position_fails(region):
126+
def test_text_invalid_inputs(region):
128127
"""
129-
Run text by providing both x/y pairs and position arguments.
128+
Run text by providing invalid combinations of inputs.
130129
"""
131130
fig = Figure()
132131
with pytest.raises(GMTInvalidInput):
133132
fig.text(
134133
region=region, projection="x1c", x=1.2, y=2.4, position="MC", text="text"
135134
)
135+
with pytest.raises(GMTInvalidInput):
136+
fig.text(region=region, projection="x1c", textfiles="file.txt", text="text")
137+
with pytest.raises(GMTInvalidInput):
138+
fig.text(region=region, projection="x1c", position="MC", text=None)
139+
with pytest.raises(GMTInvalidInput):
140+
fig.text(
141+
region=region, projection="x1c", position="MC", text=["text1", "text2"]
142+
)
143+
with pytest.raises(GMTInvalidInput):
144+
fig.text(region=region, projection="x1c", textfiles="file.txt", x=1.2, y=2.4)
136145

137146

138147
@pytest.mark.mpl_image_compare
@@ -313,10 +322,8 @@ def test_text_transparency():
313322
text = [f"TEXT-{i}-{j}" for i, j in zip(x, y)]
314323

315324
fig = Figure()
316-
317325
fig.basemap(region=[0, 10, 10, 20], projection="X10c", frame=True)
318326
fig.text(x=x, y=y, text=text, transparency=50)
319-
320327
return fig
321328

322329

@@ -333,7 +340,6 @@ def test_text_varying_transparency():
333340
fig = Figure()
334341
fig.basemap(region=[0, 10, 10, 20], projection="X10c", frame=True)
335342
fig.text(x=x, y=y, text=text, transparency=transparency)
336-
337343
return fig
338344

339345

@@ -357,7 +363,6 @@ def test_text_nonstr_text():
357363
Input text is in non-string type (e.g., int, float)
358364
"""
359365
fig = Figure()
360-
361366
fig.text(
362367
region=[0, 10, 0, 10],
363368
projection="X10c",
@@ -366,5 +371,4 @@ def test_text_nonstr_text():
366371
y=[1, 2, 3, 4],
367372
text=[1, 2, 3.0, 4.0],
368373
)
369-
370374
return fig

0 commit comments

Comments
 (0)