-
-
Notifications
You must be signed in to change notification settings - Fork 402
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 datetimes in histogram operation #2719
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6385972
Added support for datetimes in histogram operation
philippjfr e9b52f3
Allowed datetime bins in GridInterface
philippjfr c890561
Added datetime support to matplotlib HistogramPlot
philippjfr e146de7
Minor fix for unit test
philippjfr 5ceb680
Fixed bug in matplotlib HistogramPlot
philippjfr 918a70b
Small fixes for pandas Period and Timestamp
philippjfr 98c9e38
Added unit tests for histogram operation datetime handling
philippjfr a987859
Added unit tests for HistogramPlot datetime handling
philippjfr 400540e
Better variable naming
philippjfr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,9 @@ | |
import param | ||
|
||
from ...core import OrderedDict, Dimension, Store | ||
from ...core.util import match_spec, unique_iterator, basestring, max_range, isfinite | ||
from ...core.util import ( | ||
match_spec, unique_iterator, basestring, max_range, isfinite, datetime_types | ||
) | ||
from ...element import Raster, HeatMap | ||
from ...operation import interpolate_curve | ||
from ..plot import PlotSelector | ||
|
@@ -258,7 +260,11 @@ def initialize_plot(self, ranges=None): | |
el_ranges = match_spec(hist, ranges) | ||
|
||
# Get plot ranges and values | ||
edges, hvals, widths, lims = self._process_hist(hist) | ||
dims = hist.dimensions()[:2] | ||
edges, hvals, widths, lims, isdatetime = self._process_hist(hist) | ||
if isdatetime and not dims[0].value_format: | ||
dt_format = Dimension.type_formatters[np.datetime64] | ||
dims[0] = dims[0](value_format=DateFormatter(dt_format)) | ||
|
||
style = self.style[self.cyclic_index] | ||
if self.invert_axes: | ||
|
@@ -275,6 +281,7 @@ def initialize_plot(self, ranges=None): | |
|
||
ticks = self._compute_ticks(hist, edges, widths, lims) | ||
ax_settings = self._process_axsettings(hist, lims, ticks) | ||
ax_settings['dimensions'] = dims | ||
|
||
return self._finalize_axis(self.keys[-1], ranges=el_ranges, element=hist, **ax_settings) | ||
|
||
|
@@ -288,9 +295,15 @@ def _process_hist(self, hist): | |
edges = hist.interface.coords(hist, x, edges=True) | ||
values = hist.dimension_values(1) | ||
hist_vals = np.array(values) | ||
xlim = hist.range(0) | ||
ylim = hist.range(1) | ||
isdatetime = False | ||
if edges.dtype.kind == 'M' or isinstance(edges[0], datetime_types): | ||
edges = date2num([v.tolist() if isinstance(v, np.datetime64) else v for v in edges]) | ||
xlim = date2num([v.tolist() if isinstance(v, np.datetime64) else v for v in xlim]) | ||
isdatetime = True | ||
widths = np.diff(edges) | ||
lims = hist.range(0) + hist.range(1) | ||
return edges[:-1], hist_vals, widths, lims | ||
return edges[:-1], hist_vals, widths, xlim+ylim, isdatetime | ||
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 can still conceive of wanting support for other numeric types one day (e.g rationals?) but this is fine for now. We can use a general type specifier when we find ourselves needing to do something similar for another type. This this fine for now. |
||
|
||
|
||
def _compute_ticks(self, element, edges, widths, lims): | ||
|
@@ -357,7 +370,7 @@ def _update_artists(self, key, hist, edges, hvals, widths, lims, ranges): | |
|
||
def update_handles(self, key, axis, element, ranges, style): | ||
# Process values, axes and style | ||
edges, hvals, widths, lims = self._process_hist(element) | ||
edges, hvals, widths, lims, _ = self._process_hist(element) | ||
|
||
ticks = self._compute_ticks(element, edges, widths, lims) | ||
ax_settings = self._process_axsettings(element, lims, ticks) | ||
|
@@ -381,12 +394,12 @@ def _process_hist(self, hist): | |
""" | ||
Subclassed to offset histogram by defined amount. | ||
""" | ||
edges, hvals, widths, lims = super(SideHistogramPlot, self)._process_hist(hist) | ||
edges, hvals, widths, lims, isdatetime = super(SideHistogramPlot, self)._process_hist(hist) | ||
offset = self.offset * lims[3] | ||
hvals *= 1-self.offset | ||
hvals += offset | ||
lims = lims[0:3] + (lims[3] + offset,) | ||
return edges, hvals, widths, lims | ||
return edges, hvals, widths, lims, isdatetime | ||
|
||
|
||
def _update_artists(self, n, element, edges, hvals, widths, lims, ranges): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import datetime as dt | ||
|
||
import numpy as np | ||
|
||
from holoviews.element import Dataset | ||
from holoviews.operation import histogram | ||
|
||
from .testplot import TestMPLPlot, mpl_renderer | ||
|
||
|
||
class TestCurvePlot(TestMPLPlot): | ||
|
||
def test_histogram_datetime64_plot(self): | ||
dates = np.array([dt.datetime(2017, 1, i) for i in range(1, 5)]) | ||
hist = histogram(Dataset(dates, 'Date'), num_bins=4) | ||
plot = mpl_renderer.get_plot(hist) | ||
artist = plot.handles['artist'] | ||
ax = plot.handles['axis'] | ||
self.assertEqual(ax.get_xlim(), (736330.0, 736333.0)) | ||
self.assertEqual([p.get_x() for p in artist.patches], [736330.0, 736330.75, 736331.5, 736332.25]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Much simpler than a weird and confusing
convolve
call!