Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions pygmt/src/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from pygmt.helpers import (
build_arg_string,
data_kind,
dummy_context,
fmt_docstring,
is_nonstr_iter,
kwargs_to_strings,
Expand Down Expand Up @@ -164,24 +163,27 @@ def text_(
for texts, but this option is only valid if using x/y/text.
{w}
"""

# pylint: disable=too-many-locals
# pylint: disable=too-many-branches

kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access

# Ensure inputs are either textfiles, x/y/text, or position/text
if position is None:
if (x is not None or y is not None) and textfiles is not None:
raise GMTInvalidInput(
"Provide either position only, or x/y pairs, or textfiles."
)
kind = data_kind(textfiles, x, y, text)
if kind == "vectors" and text is None:
raise GMTInvalidInput("Must provide text with x/y pairs")
else:
if x is not None or y is not None:
if x is not None or y is not None or textfiles is not None:
raise GMTInvalidInput(
"Provide either position only, or x/y pairs, not both"
"Provide either position only, or x/y pairs, or textfiles."
)
kind = "vectors"

if kind == "vectors" and text is None:
raise GMTInvalidInput("Must provide text with x/y pairs or position")
if text is None or is_nonstr_iter(text):
raise GMTInvalidInput("Text can't be None or array.")
kind = None
textfiles = ""

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

# Append text at last column. Text must be passed in as str type.
if kind == "vectors":
extra_arrays.append(np.atleast_1d(text).astype(str))

with Session() as lib:
file_context = dummy_context(textfiles) if kind == "file" else ""
if kind == "vectors":
if position is not None:
file_context = dummy_context("")
else:
file_context = lib.virtualfile_from_vectors(
np.atleast_1d(x),
np.atleast_1d(y),
*extra_arrays,
# text must be in str type, see issue #706
np.atleast_1d(text).astype(str),
)
file_context = lib.virtualfile_from_data(
check_kind="vector", data=textfiles, x=x, y=y, extra_arrays=extra_arrays
)
with file_context as fname:
lib.call_module(module="text", args=build_arg_string(kwargs, infile=fname))
28 changes: 16 additions & 12 deletions pygmt/tests/test_text.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# pylint: disable=redefined-outer-name
"""
Tests text.
"""
Expand All @@ -15,16 +14,16 @@
CITIES_DATA = os.path.join(TEST_DATA_DIR, "cities.txt")


@pytest.fixture(scope="module")
def projection():
@pytest.fixture(scope="module", name="projection")
def fixture_projection():
"""
The projection system.
"""
return "x10c"


@pytest.fixture(scope="module")
def region():
@pytest.fixture(scope="module", name="region")
def fixture_region():
"""
The data region.
"""
Expand Down Expand Up @@ -124,15 +123,25 @@ def test_text_position(region):
return fig


def test_text_xy_with_position_fails(region):
def test_text_invalid_inputs(region):
"""
Run text by providing both x/y pairs and position arguments.
Run text by providing invalid combinations of inputs.
"""
fig = Figure()
with pytest.raises(GMTInvalidInput):
fig.text(
region=region, projection="x1c", x=1.2, y=2.4, position="MC", text="text"
)
with pytest.raises(GMTInvalidInput):
fig.text(region=region, projection="x1c", textfiles="file.txt", text="text")
with pytest.raises(GMTInvalidInput):
fig.text(region=region, projection="x1c", position="MC", text=None)
with pytest.raises(GMTInvalidInput):
fig.text(
region=region, projection="x1c", position="MC", text=["text1", "text2"]
)
with pytest.raises(GMTInvalidInput):
fig.text(region=region, projection="x1c", textfiles="file.txt", x=1.2, y=2.4)


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

fig = Figure()

fig.basemap(region=[0, 10, 10, 20], projection="X10c", frame=True)
fig.text(x=x, y=y, text=text, transparency=50)

return fig


Expand All @@ -333,7 +340,6 @@ def test_text_varying_transparency():
fig = Figure()
fig.basemap(region=[0, 10, 10, 20], projection="X10c", frame=True)
fig.text(x=x, y=y, text=text, transparency=transparency)

return fig


Expand All @@ -357,7 +363,6 @@ def test_text_nonstr_text():
Input text is in non-string type (e.g., int, float)
"""
fig = Figure()

fig.text(
region=[0, 10, 0, 10],
projection="X10c",
Expand All @@ -366,5 +371,4 @@ def test_text_nonstr_text():
y=[1, 2, 3, 4],
text=[1, 2, 3.0, 4.0],
)

return fig