-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor plot and plot3d to use virtualfile_from_data #990
Changes from 6 commits
53da83a
1a552f3
a8c00ac
293f0b9
bd09782
5c24f23
b0ab97d
824a659
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1360,7 +1360,9 @@ def virtualfile_from_grid(self, grid): | |||||||||||||||||
with self.open_virtual_file(*args) as vfile: | ||||||||||||||||||
yield vfile | ||||||||||||||||||
|
||||||||||||||||||
def virtualfile_from_data(self, check_kind=None, data=None, x=None, y=None, z=None): | ||||||||||||||||||
def virtualfile_from_data( | ||||||||||||||||||
self, check_kind=None, data=None, x=None, y=None, z=None, extra_arrays=None | ||||||||||||||||||
): | ||||||||||||||||||
""" | ||||||||||||||||||
Store any data inside a virtual file. | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -1378,6 +1380,9 @@ def virtualfile_from_data(self, check_kind=None, data=None, x=None, y=None, z=No | |||||||||||||||||
raster grid, a vector matrix/arrays, or other supported data input. | ||||||||||||||||||
x/y/z : 1d arrays or None | ||||||||||||||||||
x, y and z columns as numpy arrays. | ||||||||||||||||||
extra_arrays : list | ||||||||||||||||||
Optional. A list of numpy arrays in addition to x, y and z. All | ||||||||||||||||||
of these arrays must be of the same size as the x/y/z arrays. | ||||||||||||||||||
|
||||||||||||||||||
Returns | ||||||||||||||||||
------- | ||||||||||||||||||
|
@@ -1430,14 +1435,25 @@ def virtualfile_from_data(self, check_kind=None, data=None, x=None, y=None, z=No | |||||||||||||||||
if kind in ("file", "grid"): | ||||||||||||||||||
_data = (data,) | ||||||||||||||||||
elif kind == "vectors": | ||||||||||||||||||
_data = (x, y, z) | ||||||||||||||||||
_data = [np.atleast_1d(x), np.atleast_1d(y)] | ||||||||||||||||||
if z is not None: | ||||||||||||||||||
_data.append(np.atleast_1d(z)) | ||||||||||||||||||
if extra_arrays: | ||||||||||||||||||
_data.extend(extra_arrays) | ||||||||||||||||||
elif kind == "matrix": # turn 2D arrays into list of vectors | ||||||||||||||||||
try: | ||||||||||||||||||
# pandas.DataFrame and xarray.Dataset types | ||||||||||||||||||
_data = [array for _, array in data.items()] | ||||||||||||||||||
except AttributeError: | ||||||||||||||||||
# Python lists, tuples, and numpy ndarray types | ||||||||||||||||||
_data = np.atleast_2d(np.asanyarray(data).T) | ||||||||||||||||||
try: | ||||||||||||||||||
# Just use virtualfile_from_matrix for 2D | ||||||||||||||||||
# numpy.ndarray which are not datetime (M) types | ||||||||||||||||||
assert data.ndim == 2 and not data.dtype.kind == "M" | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I'll try |
||||||||||||||||||
_virtualfile_from = self.virtualfile_from_matrix | ||||||||||||||||||
_data = (data,) | ||||||||||||||||||
except (AssertionError, AttributeError): | ||||||||||||||||||
# Python lists, tuples, and numpy ndarray types | ||||||||||||||||||
_data = np.atleast_2d(np.asanyarray(data).T) | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would appreciate advice on improving/refactoring this chunk of code. I'm half thinking whether to move some of the logic to Lines 65 to 72 in 65b099a
Or we could just keep things like this as it is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't read the codes carefully, but I think the "matrix" kind is quite confusing. The GMT API function However, currently, data types like pandas.DataFrame are also "matrix". So we have to check the data types to choose either I agree with you that we can/shoudl add a new kind to distinguish between a GMT-compatible "matrix" and a more complicated data structure. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. |
||||||||||||||||||
|
||||||||||||||||||
# Finally create the virtualfile from the data, to be passed into GMT | ||||||||||||||||||
file_context = _virtualfile_from(*_data) | ||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.