Skip to content

Commit

Permalink
Add RangeCumulativeHistogramPlot class (#385)
Browse files Browse the repository at this point in the history
* added RangeCumulativeHistogramPlot class

* remove range smaller than 1 for cumulative histogram

* add min, max and autoscaling range options to histograms

* remove data rescale

* fixed long line

* fixed white spaces and blank lines

* deal with empty arr

* update get_range for histogram

* fix lint

* add new class to __init__

---------

Co-authored-by: Iara Ota <iara.ota@ligo.org>
  • Loading branch information
iaraota and Iara Ota authored Feb 9, 2024
1 parent 07e1f43 commit 21b3e62
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
1 change: 1 addition & 0 deletions gwsumm/plot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
RangeSpectrogramDataPlot,
RangeSpectrumDataPlot,
RangeCumulativeSpectrumDataPlot,
RangeCumulativeHistogramPlot,
SimpleTimeVolumeDataPlot,
GWpyTimeVolumeDataPlot,
)
Expand Down
36 changes: 25 additions & 11 deletions gwsumm/plot/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,15 +710,14 @@ def draw(self, outputfile=None):

# plot
for ax, arr, pargs in zip(cycle(axes), data, histargs):
# set range if not given
if pargs.get('range') is None:
pargs['range'] = self._get_range(
data,
# use range from first dataset if already calculated
range=histargs[0].get('range'),
# use xlim if manually set (user or INI)
xlim=None if ax.get_autoscalex_on() else ax.get_xlim(),
)
# set range
pargs['range'] = self._get_range(
data,
# use range from first dataset if already calculated
range=histargs[0].get('range'),
# use xlim if manually set (user or INI)
xlim=None if ax.get_autoscalex_on() else ax.get_xlim(),
)

# plot histogram
_, _, patches = ax.hist(arr, **pargs)
Expand Down Expand Up @@ -759,8 +758,23 @@ def draw(self, outputfile=None):
return self.finalize(outputfile=outputfile)

def _get_range(self, data, range=None, xlim=None):
if range is not None or xlim is not None:
return range or xlim
if range is not None:
if range == 'autoscaling':
return None
else:
range = list(range)
try:
if range[0] == 'min':
range[0] = numpy.min(data)
if range[1] == 'max':
range[1] = numpy.max(data)
if range[0] < range[1]:
return range
except (ValueError, IndexError) as exc:
if not str(exc).startswith('zero-size array'):
raise
if xlim is not None:
return xlim
try:
return numpy.min(data), numpy.max(data)
except ValueError as exc:
Expand Down
17 changes: 17 additions & 0 deletions gwsumm/plot/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,23 @@ class RangeDataHistogramPlot(RangePlotMixin, get_plot('histogram')):
register_plot(RangeDataHistogramPlot)


class RangeCumulativeHistogramPlot(RangePlotMixin, get_plot('histogram')):
type = 'range-cumulative-histogram'
defaults = get_plot('histogram').defaults.copy()
defaults.update(RangePlotMixin.defaults.copy())
defaults.update({
'xlabel': 'Angle-averaged range [Mpc]',
'ylabel': 'Cumulative time duration',
'log': False,
'cumulative': True,
'density': True,
'range': (1, 'max'),
})


register_plot(RangeCumulativeHistogramPlot)


class RangeSpectrogramDataPlot(RangePlotMixin, get_plot('spectrogram')):
type = 'range-spectrogram'
defaults = get_plot('spectrogram').defaults.copy()
Expand Down
3 changes: 2 additions & 1 deletion gwsumm/tabs/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ def from_ini(cls, cp, section, plotdir='plots', **kwargs):
if cp.has_section(pdef):
type_ = cp.get(pdef, 'type')
PlotClass = get_plot(type_)
elif (pdef not in ['range-histogram', 'segment-histogram'] and
elif (pdef not in ['range-histogram', 'segment-histogram',
'range-cumulative-histogram'] and
pdef.endswith('-histogram')):
type_ = None
etg, column = pdef.rsplit('-', 2)[:2]
Expand Down

0 comments on commit 21b3e62

Please sign in to comment.