Skip to content

Commit

Permalink
Refactor the Session.virtualfile_from_data method to avoid use of the…
Browse files Browse the repository at this point in the history
… assert keyword (#2851)
  • Loading branch information
seisman authored Dec 6, 2023
1 parent 251a5f1 commit c2d5e24
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -1575,23 +1575,19 @@ def virtualfile_from_data( # noqa: PLR0912
if extra_arrays:
_data.extend(extra_arrays)
elif kind == "matrix": # turn 2-D arrays into list of vectors
try:
# pandas.Series will be handled below like a 1-D numpy.ndarray
assert not hasattr(data, "to_frame")
# pandas.DataFrame and xarray.Dataset types
if hasattr(data, "items") and not hasattr(data, "to_frame"):
# pandas.DataFrame or xarray.Dataset types.
# pandas.Series will be handled below like a 1-D numpy.ndarray.
_data = [array for _, array in data.items()]
except (AttributeError, AssertionError):
try:
# Just use virtualfile_from_matrix for 2-D numpy.ndarray
# which are signed integer (i), unsigned integer (u) or
# floating point (f) types
assert data.ndim == 2 and data.dtype.kind in "iuf"
_virtualfile_from = self.virtualfile_from_matrix
_data = (data,)
except (AssertionError, AttributeError):
# Python list, tuple, numpy.ndarray, and pandas.Series
# types
_data = np.atleast_2d(np.asanyarray(data).T)
elif hasattr(data, "ndim") and data.ndim == 2 and data.dtype.kind in "iuf":
# Just use virtualfile_from_matrix for 2-D numpy.ndarray
# which are signed integer (i), unsigned integer (u) or
# floating point (f) types
_virtualfile_from = self.virtualfile_from_matrix
_data = (data,)
else:
# Python list, tuple, numpy.ndarray, and pandas.Series types
_data = np.atleast_2d(np.asanyarray(data).T)

# Finally create the virtualfile from the data, to be passed into GMT
file_context = _virtualfile_from(*_data)
Expand Down

0 comments on commit c2d5e24

Please sign in to comment.