Skip to content

Commit

Permalink
Refactor text to use virtualfile_from_vectors instead of pandas tempfile
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
weiji14 committed Jul 14, 2020
1 parent 42af00e commit 9ef04b0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
35 changes: 10 additions & 25 deletions pygmt/base_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
Does not define any special non-GMT methods (savefig, show, etc).
"""
import contextlib
import csv
import numpy as np
import pandas as pd

from .clib import Session
from .exceptions import GMTInvalidInput
Expand All @@ -14,7 +12,6 @@
dummy_context,
data_kind,
fmt_docstring,
GMTTempFile,
use_alias,
kwargs_to_strings,
)
Expand Down Expand Up @@ -970,27 +967,15 @@ def text(
if position is not None and isinstance(position, str):
kwargs["F"] += f'+c{position}+t"{text}"'

with GMTTempFile(suffix=".txt") as tmpfile:
with Session() as lib:
fname = textfiles if kind == "file" else ""
if kind == "vectors":
if position is not None:
fname = ""
else:
pd.DataFrame.from_dict(
{
"x": np.atleast_1d(x),
"y": np.atleast_1d(y),
"text": np.atleast_1d(text),
}
).to_csv(
tmpfile.name,
sep="\t",
header=False,
index=False,
quoting=csv.QUOTE_NONE,
)
fname = tmpfile.name

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), np.atleast_1d(text)
)
with file_context as fname:
arg_str = " ".join([fname, build_arg_string(kwargs)])
lib.call_module("text", arg_str)
9 changes: 8 additions & 1 deletion pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,9 @@ def virtualfile_from_vectors(self, *vectors):
arrays = vectors_to_arrays(vectors)

columns = len(arrays)
if np.issubdtype(arrays[-1].dtype, np.str_):
columns -= 1

rows = len(arrays[0])
if not all(len(i) == rows for i in arrays):
raise GMTInvalidInput("All arrays must have same size.")
Expand All @@ -1146,8 +1149,12 @@ def virtualfile_from_vectors(self, *vectors):
family, geometry, mode="GMT_CONTAINER_ONLY", dim=[columns, rows, 1, 0]
)

for col, array in enumerate(arrays):
# Use put_vector for first n columns with numerical type data
for col, array in enumerate(arrays[:columns]):
self.put_vector(dataset, column=col, vector=array)
# Use put_strings for last column with string type data
for array in arrays[columns:]:
self.put_strings(dataset, family="GMT_IS_VECTOR", strings=array)

with self.open_virtual_file(
family, geometry, "GMT_IN|GMT_IS_REFERENCE", dataset
Expand Down

0 comments on commit 9ef04b0

Please sign in to comment.