Skip to content

Commit b7269ec

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 b7269ec

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

pygmt/base_plotting.py

+9-22
Original file line numberDiff line numberDiff line change
@@ -972,25 +972,12 @@ def text(
972972

973973
with GMTTempFile(suffix=".txt") as tmpfile:
974974
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-
995-
arg_str = " ".join([fname, build_arg_string(kwargs)])
996-
lib.call_module("text", arg_str)
975+
if kind == "file" or (kind == "vectors" and position is not None):
976+
file_context = dummy_context(textfiles)
977+
elif kind == "vectors" and position is None:
978+
file_context = lib.virtualfile_from_vectors(
979+
np.atleast_1d(x), np.atleast_1d(y), np.atleast_1d(text)
980+
)
981+
with file_context as fname:
982+
arg_str = " ".join([fname, build_arg_string(kwargs)])
983+
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)