Skip to content

Commit 645eeb8

Browse files
authored
API Support spectrumPlot(..., by=None|"max") (#457)
* STY Apply some black formatting to spectrumPlot * BUG Use logy in Detector.spectrumPlot * DEV Use broadcasting to improve spectrum normalization Avoids for loops and lets numpy handle division at once * API Support spectrumPlot(..., by={None, "max"}) Default is "max" which will scale tallied data by the max value in each column (plotted data). Passing by=None will not perform any additional modifications. Neither options changes the division by lethargy. Closes #455 * TST Test spectrumPlot(..., by=None)
1 parent c4b8ee6 commit 645eeb8

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

serpentTools/detectors.py

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
from numpy import (
2929
unique, inf, hstack, arange, log, divide, asfortranarray,
30-
ndarray, asarray,
30+
ndarray, asarray, newaxis
3131
)
3232
from matplotlib.patches import RegularPolygon
3333
from matplotlib.collections import PatchCollection
@@ -495,10 +495,25 @@ def _compare(self, other, lower, upper, sigma):
495495
return similar
496496

497497
@magicPlotDocDecorator
498-
def spectrumPlot(self, fixed=None, ax=None, normalize=True, xlabel=None,
499-
ylabel=None, steps=True, logx=True, logy=False,
500-
loglog=False, sigma=3, labels=None, legend=None, ncol=1,
501-
title=None, **kwargs):
498+
def spectrumPlot(
499+
self,
500+
fixed=None,
501+
ax=None,
502+
normalize=True,
503+
by="max",
504+
xlabel=None,
505+
ylabel=None,
506+
steps=True,
507+
logx=True,
508+
logy=False,
509+
loglog=False,
510+
sigma=3,
511+
labels=None,
512+
legend=None,
513+
ncol=1,
514+
title=None,
515+
**kwargs,
516+
):
502517
"""
503518
Quick plot of the detector value as a function of energy.
504519
@@ -508,7 +523,12 @@ def spectrumPlot(self, fixed=None, ax=None, normalize=True, xlabel=None,
508523
Dictionary controlling the reduction in data
509524
{ax}
510525
normalize: bool
511-
Normalize quantities per unit lethargy
526+
Divide tally values by size of lethargy bins.
527+
by : {"max", None}, optional
528+
How to scale the spectrum after considering ``normalize``.
529+
``"max"`` (default) will divide by the maximum value such
530+
that the max value is unity. ``None`` will not perform any
531+
additional modifications.
512532
{xlabel}
513533
{ylabel}
514534
steps: bool
@@ -546,13 +566,17 @@ def spectrumPlot(self, fixed=None, ax=None, normalize=True, xlabel=None,
546566
)
547567
elif len(slicedTallies.shape) == 1:
548568
slicedTallies = slicedTallies.reshape(slicedTallies.size, 1)
569+
549570
lowerE = self.grids['E'][:, 0]
550571
if normalize:
551572
lethBins = log(
552573
divide(self.grids['E'][:, -1], lowerE))
553-
for indx in range(slicedTallies.shape[1]):
554-
scratch = divide(slicedTallies[:, indx], lethBins)
555-
slicedTallies[:, indx] = scratch / scratch.max()
574+
# Need to scale up the dimension of the energy grids to support
575+
# broadcasting -> (E, N) / (E, )
576+
slicedTallies /= lethBins[:, newaxis]
577+
578+
if by == "max":
579+
slicedTallies /= slicedTallies.max(axis=0)
556580

557581
if steps:
558582
if 'drawstyle' in kwargs:
@@ -576,9 +600,17 @@ def spectrumPlot(self, fixed=None, ax=None, normalize=True, xlabel=None,
576600

577601
if legend is None and labels:
578602
legend = True
579-
ax = formatPlot(ax, loglog=loglog, logx=logx, legendcols=ncol,
580-
xlabel=xlabel or "Energy [MeV]", ylabel=ylabel,
581-
legend=legend, title=title)
603+
ax = formatPlot(
604+
ax,
605+
loglog=loglog,
606+
logx=logx,
607+
logy=logy,
608+
legendcols=ncol,
609+
xlabel=xlabel or "Energy [MeV]",
610+
ylabel=ylabel,
611+
legend=legend,
612+
title=title,
613+
)
582614
return ax
583615

584616
@magicPlotDocDecorator
19.5 KB
Loading

tests/plots/test_detectors.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ def testBwrMeshPlot():
1717
with pytest.warns(None) as record:
1818
reader["xymesh"].meshPlot(fixed={"energy": 0})
1919
assert len(record) == 0
20+
21+
22+
@compare_or_update_plot
23+
def testSpectrumJustNormPerLethargy():
24+
reader = readDataFile("bwr_det0.m")
25+
reader["spectrum"].spectrumPlot(by=None, fixed={"reaction": 1})

0 commit comments

Comments
 (0)