Skip to content

Commit 8857ce6

Browse files
committed
pygmt.filter1d: Improve performance by getting rid of temporary files
1 parent 9640c26 commit 8857ce6

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed

pygmt/src/filter1d.py

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
filter1d - Time domain filtering of 1-D data tables
33
"""
44

5-
import pandas as pd
5+
from typing import Literal
6+
67
from pygmt.clib import Session
78
from pygmt.exceptions import GMTInvalidInput
89
from pygmt.helpers import (
9-
GMTTempFile,
1010
build_arg_string,
1111
fmt_docstring,
1212
use_alias,
@@ -20,7 +20,12 @@
2020
F="filter_type",
2121
N="time_col",
2222
)
23-
def filter1d(data, output_type="pandas", outfile=None, **kwargs):
23+
def filter1d(
24+
data,
25+
output_type: Literal["pandas", "numpy", "file"] = "pandas",
26+
outfile: str | None = None,
27+
**kwargs,
28+
):
2429
r"""
2530
Time domain filtering of 1-D data tables.
2631
@@ -38,6 +43,15 @@ def filter1d(data, output_type="pandas", outfile=None, **kwargs):
3843
3944
Parameters
4045
----------
46+
output_type
47+
Desired output type of the result data.
48+
49+
- ``pandas`` will return a :class:`pandas.DataFrame` object.
50+
- ``numpy`` will return a :class:`numpy.ndarray` object.
51+
- ``file`` will save the result to the file given by the ``outfile`` parameter.
52+
outfile
53+
The file name for saving the result. If specified, ``output_type`` will be
54+
forced to be ``"file"``.
4155
filter_type : str
4256
**type**\ *width*\ [**+h**].
4357
Set the filter **type**. Choose among convolution and non-convolution
@@ -91,48 +105,27 @@ def filter1d(data, output_type="pandas", outfile=None, **kwargs):
91105
left-most column is 0, while the right-most is (*n_cols* - 1)
92106
[Default is ``0``].
93107
94-
output_type : str
95-
Determine the format the xyz data will be returned in [Default is
96-
``pandas``]:
97-
98-
- ``numpy`` - :class:`numpy.ndarray`
99-
- ``pandas``- :class:`pandas.DataFrame`
100-
- ``file`` - ASCII file (requires ``outfile``)
101-
outfile : str
102-
The file name for the output ASCII file.
103-
104108
Returns
105109
-------
106110
ret : pandas.DataFrame or numpy.ndarray or None
107111
Return type depends on ``outfile`` and ``output_type``:
108112
109-
- None if ``outfile`` is set (output will be stored in file set by
110-
``outfile``)
111-
- :class:`pandas.DataFrame` or :class:`numpy.ndarray` if ``outfile`` is
112-
not set (depends on ``output_type`` [Default is
113-
:class:`pandas.DataFrame`])
113+
- None if ``outfile`` is set (output will be stored in file set by ``outfile``)
114+
- :class:`pandas.DataFrame` or :class:`numpy.ndarray` if ``outfile`` is not set
115+
(depends on ``output_type``)
114116
"""
115117
if kwargs.get("F") is None:
116118
raise GMTInvalidInput("Pass a required argument to 'filter_type'.")
117119

118120
output_type = validate_output_table_type(output_type, outfile=outfile)
119121

120-
with GMTTempFile() as tmpfile:
121-
with Session() as lib:
122-
with lib.virtualfile_in(check_kind="vector", data=data) as vintbl:
123-
if outfile is None:
124-
outfile = tmpfile.name
125-
lib.call_module(
126-
module="filter1d",
127-
args=build_arg_string(kwargs, infile=vintbl, outfile=outfile),
128-
)
129-
130-
# Read temporary csv output to a pandas table
131-
if outfile == tmpfile.name: # if user did not set outfile, return pd.DataFrame
132-
result = pd.read_csv(tmpfile.name, sep="\t", header=None, comment=">")
133-
elif outfile != tmpfile.name: # return None if outfile set, output in outfile
134-
result = None
135-
136-
if output_type == "numpy":
137-
result = result.to_numpy()
138-
return result
122+
with Session() as lib:
123+
with (
124+
lib.virtualfile_in(check_kind="vector", data=data) as vintbl,
125+
lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl,
126+
):
127+
lib.call_module(
128+
module="filter1d",
129+
args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl),
130+
)
131+
return lib.return_dataset(output_type=output_type, vfile=vouttbl)

0 commit comments

Comments
 (0)