-
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
Support various datetime types as input #464
Conversation
Currently supported datetime types: - [numpy.datetime64](https://numpy.org/doc/stable/reference/arrays.datetime.html) - [pandas.DateTimeIndex](https://pandas.pydata.org/docs/user_guide/timeseries.html)
@GenericMappingTools/python @GenericMappingTools/python-contributors This PR is almost done. If you have the GMT latest master branch installed, please try this branch and leave your comments. |
Some instructions on how to test this branch: curl https://raw.githubusercontent.com/GenericMappingTools/gmt/master/ci/build-gmt-master.sh | bash
conda activate --name pygmt-test # or some virtual env you have
pip install https://github.com/GenericMappingTools/pygmt/archive/datetime-input.zip and then in python:
and then try to test out |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Ok, I tried installing it in a different conda environment and it works now! Below is my convoluted real-data example from Antarctica (longitude=46.522298201524514, latitude=-73.05774246536473). Code: # %%
import os
os.environ["GMT_LIBRARY_PATH"] = "/home/username/gmt-install-dir/lib"
import numpy as np
import pygmt
pygmt.show_versions()
# %%
x = np.array(
[
np.datetime64(dt)
for dt in [
"NaT",
"2019-01-28T07:51:16.582496785",
"2019-04-29T03:30:57.620034382",
"2019-07-28T23:10:36.096639410",
"2019-10-27T18:50:32.615134843",
"2020-01-26T14:30:18.835180975",
]
]
)
y = np.array(
[np.NaN, 3126.60298909, 3126.67885045, 3126.67276984, 3126.7217197, 3126.68279204,]
)
# %%
# Mask out NaT values
mask = ~np.isnan(y)
x = x[mask]
y = y[mask]
# %%
dates = np.datetime_as_string(x, unit="ns")
region = [dates[0], dates[-1], np.nanmin(y) - 0.1, np.nanmax(y) + 0.1]
# %%
fig = pygmt.Figure()
fig.basemap(
projection="X15c/5c",
region=region,
frame=["WSne", "xaf+lDateTime", "yaf+lHeight(m)"],
)
fig.plot(x=x, y=y, style="t1c", pen="1p")
fig.savefig("height_over_time.png")
fig.show() Glad to see that these work off the shelf:
Places to improve:
|
I'll add it to the test
Perhaps not true. GMT converts ISO datetime to double values internally.
The decorator
Just opened an issue (GenericMappingTools/gmt#3414) in the upstream repository.
Perhaps we need to convert timedelta64 to double values before passing to GMT. Leave it for a separate PR. |
Ideally there would be a warning, but I'm not sure if it matters. The data will be plotted in pretty much the same position anyway (unless someone looks at changes every microsecond?) so it's low priority.
Ok, I've tested the 'new' GMT master with GenericMappingTools/gmt#3415 merged in, and it's giving a warning (as expected), but the plot shows up now 🙌 Code (without mask!): import os
os.environ["GMT_LIBRARY_PATH"] = os.path.join(os.environ["HOME"], "gmt-install-dir/lib")
import numpy as np
import pygmt
pygmt.show_versions()
# %%
x = np.array(
[
np.datetime64(dt)
for dt in [
"NaT",
"2019-01-28T07:51:16.582496785",
"2019-04-29T03:30:57.620034382",
"2019-07-28T23:10:36.096639410",
"2019-10-27T18:50:32.615134843",
"2020-01-26T14:30:18.835180975",
]
]
)
y = np.array(
[np.NaN, 3126.60298909, 3126.67885045, 3126.67276984, 3126.7217197, 3126.68279204,]
)
# %%
dates = np.datetime_as_string(x, unit="ns")
region = [dates[1], dates[-1], np.nanmin(y) - 0.1, np.nanmax(y) + 0.1]
# %%
fig = pygmt.Figure()
fig.basemap(
projection="X15c/5c",
region=region,
frame=["WSne", "xaf+lDateTime", "yaf+lHeight(m)"],
)
fig.plot(x=x, y=y, style="t1c", pen="1p")
fig.savefig("height_over_time.png")
fig.show() |
…andas.DateTimeIndex and numpy.datetime64 types
@weiji14 Could you help provide a simple example for this? And what's the best place to add some examples for calendar plots? |
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.
This seems about ready, and it would be nice to merge this before we start working on #520 properly to avoid the conflicts. I'll help to fix the tests in a few minutes, seems to be just whitespace and a wrong error captured.
np.int32: "GMT_INT", | ||
np.uint64: "GMT_ULONG", | ||
np.uint32: "GMT_UINT", | ||
np.datetime64: "GMT_DATETIME", |
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.
Quick question, is there a GMT dtype for string types? I can't seem to find one at https://docs.generic-mapping-tools.org/6.1/api.html#gmt-c-api.
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.
There is a GMT_TEXT
, but it may not related to the GMT_Put_Strings function.
Thanks for help improving this PR. There are still something to do (e.g., add examples, pass a datetime region as a list), but we can merge this PR and open issues for the missing feature. |
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.
There are still something to do (e.g., add examples, pass a datetime region as a list), but we can merge this PR and open issues for the missing feature.
Yep, we can add those tutorials and enhancements later 🚀
….DataFrame tables (#619) Changes the backend mechanism of `info` from using lib.virtualfile_from_matrix() (which only supports single non-datetime dtypes) to using lib.virtualfile_from_vectors() (which supports datetime inputs as of #464). * Refactor info to use virtualfile_from_vectors to support datetime inputs * Test that xarray.Dataset inputs into pygmt.info works too * Expect failures on test_info_*_time_column on GMT 6.1.1 * Document xarray.Datasets with 1D data_vars as allowed inputs to info Co-authored-by: Dongdong Tian <[email protected]>
Based on GenericMappingTools#464 and GenericMappingTools#549 I prepared a gallery example for plotting datetime inputs.
Description of proposed changes
This PR allows PyGMT accept vectors of various datetime types so that PyGMT can plot datetime axis.
Vectors of following datetime types are supported:
2010-01-01
,1/1/2018
,Jul 5, 2019
TODO:
Known issues but won't be fixed in this PR:
region
argument doesn't work with numpydatetime64
objects, have to convert to a string usingnp.datetime_as_string
in order to set the map frame bounds.timedelta64
, e.g. GPS time from a certain epoch.Fixes #242.
Example script:
Output:
Reminders
make format
andmake check
to make sure the code follows the style guide.doc/api/index.rst
.