Skip to content

Commit 9ef04b0

Browse files
committed
Refactor text to use virtualfile_from_vectors instead of pandas tempfile
Modified virtualfile_from_vectors to use put_strings on last column instead of put_vectors if it has a string data type. In theory this should work for `text`, but in reality, a segmentation fault happens.
1 parent 42af00e commit 9ef04b0

File tree

2 files changed

+18
-26
lines changed

2 files changed

+18
-26
lines changed

pygmt/base_plotting.py

+10-25
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
Does not define any special non-GMT methods (savefig, show, etc).
44
"""
55
import contextlib
6-
import csv
76
import numpy as np
8-
import pandas as pd
97

108
from .clib import Session
119
from .exceptions import GMTInvalidInput
@@ -14,7 +12,6 @@
1412
dummy_context,
1513
data_kind,
1614
fmt_docstring,
17-
GMTTempFile,
1815
use_alias,
1916
kwargs_to_strings,
2017
)
@@ -970,27 +967,15 @@ def text(
970967
if position is not None and isinstance(position, str):
971968
kwargs["F"] += f'+c{position}+t"{text}"'
972969

973-
with GMTTempFile(suffix=".txt") as tmpfile:
974-
with Session() as lib:
975-
fname = textfiles if kind == "file" else ""
976-
if kind == "vectors":
977-
if position is not None:
978-
fname = ""
979-
else:
980-
pd.DataFrame.from_dict(
981-
{
982-
"x": np.atleast_1d(x),
983-
"y": np.atleast_1d(y),
984-
"text": np.atleast_1d(text),
985-
}
986-
).to_csv(
987-
tmpfile.name,
988-
sep="\t",
989-
header=False,
990-
index=False,
991-
quoting=csv.QUOTE_NONE,
992-
)
993-
fname = tmpfile.name
994-
970+
with Session() as lib:
971+
file_context = dummy_context(textfiles) if kind == "file" else ""
972+
if kind == "vectors":
973+
if position is not None:
974+
file_context = dummy_context("")
975+
else:
976+
file_context = lib.virtualfile_from_vectors(
977+
np.atleast_1d(x), np.atleast_1d(y), np.atleast_1d(text)
978+
)
979+
with file_context as fname:
995980
arg_str = " ".join([fname, build_arg_string(kwargs)])
996981
lib.call_module("text", arg_str)

pygmt/clib/session.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,9 @@ def virtualfile_from_vectors(self, *vectors):
11351135
arrays = vectors_to_arrays(vectors)
11361136

11371137
columns = len(arrays)
1138+
if np.issubdtype(arrays[-1].dtype, np.str_):
1139+
columns -= 1
1140+
11381141
rows = len(arrays[0])
11391142
if not all(len(i) == rows for i in arrays):
11401143
raise GMTInvalidInput("All arrays must have same size.")
@@ -1146,8 +1149,12 @@ def virtualfile_from_vectors(self, *vectors):
11461149
family, geometry, mode="GMT_CONTAINER_ONLY", dim=[columns, rows, 1, 0]
11471150
)
11481151

1149-
for col, array in enumerate(arrays):
1152+
# Use put_vector for first n columns with numerical type data
1153+
for col, array in enumerate(arrays[:columns]):
11501154
self.put_vector(dataset, column=col, vector=array)
1155+
# Use put_strings for last column with string type data
1156+
for array in arrays[columns:]:
1157+
self.put_strings(dataset, family="GMT_IS_VECTOR", strings=array)
11511158

11521159
with self.open_virtual_file(
11531160
family, geometry, "GMT_IN|GMT_IS_REFERENCE", dataset

0 commit comments

Comments
 (0)