From 035706f11c2eb7f17cb61959950a9a4e32f4e4b5 Mon Sep 17 00:00:00 2001 From: Wei Ji Date: Tue, 11 Aug 2020 09:29:22 +1200 Subject: [PATCH 1/2] 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 for GMT < 6.1.1. --- pygmt/base_plotting.py | 35 ++++++++++------------------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/pygmt/base_plotting.py b/pygmt/base_plotting.py index f978e9c9cf5..a396da0a297 100644 --- a/pygmt/base_plotting.py +++ b/pygmt/base_plotting.py @@ -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 GMTError, GMTInvalidInput @@ -14,7 +12,6 @@ dummy_context, data_kind, fmt_docstring, - GMTTempFile, use_alias, kwargs_to_strings, ) @@ -970,28 +967,16 @@ 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) From 706f7fe55270affe8ee9bd0db5a51260a7502496 Mon Sep 17 00:00:00 2001 From: Wei Ji Date: Sat, 5 Sep 2020 00:14:25 +1200 Subject: [PATCH 2/2] Keep pandas for now since meca still requires it --- pygmt/base_plotting.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pygmt/base_plotting.py b/pygmt/base_plotting.py index abeb77a9150..a3f682bebf9 100644 --- a/pygmt/base_plotting.py +++ b/pygmt/base_plotting.py @@ -4,6 +4,7 @@ """ import contextlib import numpy as np +import pandas as pd from .clib import Session from .exceptions import GMTError, GMTInvalidInput