Skip to content

Commit

Permalink
added and propagated FullBinsOnly flag (#38550)
Browse files Browse the repository at this point in the history
* added and propagated FullBinsOnly flag

* added a release note

* added tests for ragged and non ragged workspace paths in rebin ragged for fullbinsonly

* add missing - for release note

* added doc reference to rebinragged page for a better explanation of FullBinsOnly

* added an additional code sample explaining the new param

* spellcheck
  • Loading branch information
walshmm authored Jan 15, 2025
1 parent eb814b0 commit 30baff0
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 3 deletions.
12 changes: 9 additions & 3 deletions Framework/Algorithms/src/RebinRagged2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void RebinRagged::init() {
declareProperty(std::make_unique<ArrayProperty<double>>("XMax"), "maximum x values with NaN meaning no maximum");
declareProperty(std::make_unique<ArrayProperty<double>>("Delta"), "step parameter for rebin");
declareProperty("PreserveEvents", true, "False converts event workspaces to histograms");
declareProperty("FullBinsOnly", false, "Omit the final bin if it's width is smaller than the step size");
}

std::map<std::string, std::string> RebinRagged::validateInputs() {
Expand Down Expand Up @@ -107,6 +108,7 @@ void RebinRagged::exec() {
MatrixWorkspace_sptr outputWS = getProperty("OutputWorkspace");

bool preserveEvents = getProperty("PreserveEvents");
bool fullBinsOnly = getProperty("FullBinsOnly");

// Rebinning in-place
bool inPlace = (inputWS == outputWS);
Expand All @@ -123,6 +125,7 @@ void RebinRagged::exec() {
auto rebin = createChildAlgorithm("Rebin", 0.0, 1.0);
rebin->setProperty("InputWorkspace", inputWS);
rebin->setProperty("PreserveEvents", preserveEvents);
rebin->setProperty("FullBinsOnly", fullBinsOnly);
const std::vector<double> params = {xmins[0], deltas[0], xmaxs[0]};
rebin->setProperty("Params", params);
rebin->execute();
Expand Down Expand Up @@ -164,7 +167,8 @@ void RebinRagged::exec() {
const auto delta = deltas[hist];

HistogramData::BinEdges XValues_new(0);
static_cast<void>(VectorHelper::createAxisFromRebinParams({xmin, delta, xmax}, XValues_new.mutableRawData()));
static_cast<void>(VectorHelper::createAxisFromRebinParams({xmin, delta, xmax}, XValues_new.mutableRawData(),
true, fullBinsOnly));
EventList &el = eventOutputWS->getSpectrum(hist);
el.setHistogram(XValues_new);
}
Expand All @@ -188,7 +192,8 @@ void RebinRagged::exec() {
const EventList &el = eventInputWS->getSpectrum(hist);

HistogramData::BinEdges XValues_new(0);
static_cast<void>(VectorHelper::createAxisFromRebinParams({xmin, delta, xmax}, XValues_new.mutableRawData()));
static_cast<void>(VectorHelper::createAxisFromRebinParams({xmin, delta, xmax}, XValues_new.mutableRawData(),
true, fullBinsOnly));

MantidVec y_data, e_data;
// The EventList takes care of histogramming.
Expand Down Expand Up @@ -242,7 +247,8 @@ void RebinRagged::exec() {
const auto delta = deltas[hist];

HistogramData::BinEdges XValues_new(0);
static_cast<void>(VectorHelper::createAxisFromRebinParams({xmin, delta, xmax}, XValues_new.mutableRawData()));
static_cast<void>(VectorHelper::createAxisFromRebinParams({xmin, delta, xmax}, XValues_new.mutableRawData(), true,
fullBinsOnly));

outputWS->setHistogram(hist, HistogramData::rebin(inputWS->histogram(hist), XValues_new));
prog.report();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,73 @@ def test_event_workspace(self):
# parameters are set so all y-values are 0.6
np.testing.assert_almost_equal(14, y, err_msg=label)

def test_FullBinsOnly(self):
xExpected = [0.5, 2.5, 4.5, 6.5]
yExpected = [10, 24, 38]

def Create1DWorkspace(size):
xData = []
yData = []
j = 0.5
for i in range(0, size + 1):
xData.append(j)
yData.append((i + 1) * 2)
j = 0.75 + j
yData.pop()
ws = api.CreateWorkspace(xData, yData)
return ws

inputWs = Create1DWorkspace(10)

api.RebinRagged(InputWorkspace=inputWs, OutputWorkspace="NotFullBinsOnly", Delta=2.0, PreserveEvents=True, FullBinsOnly=False)
fullBinsOnlyWs = api.RebinRagged(
InputWorkspace=inputWs, OutputWorkspace="FullBinsOnly", Delta=2.0, PreserveEvents=True, FullBinsOnly=True
)

fullBinsXValues = AnalysisDataService.retrieve("FullBinsOnly").readX(0)
fullBinsYValues = AnalysisDataService.retrieve("FullBinsOnly").readY(0)

notFullBinsXValues = AnalysisDataService.retrieve("NotFullBinsOnly").readX(0)

assert not fullBinsOnlyWs.isRaggedWorkspace()

assert len(fullBinsXValues) == len(xExpected)
assert len(notFullBinsXValues) != len(fullBinsXValues)

for i in range(len(fullBinsXValues)):
np.testing.assert_almost_equal(fullBinsXValues[i], xExpected[i])

for i in range(len(fullBinsYValues)):
np.testing.assert_almost_equal(fullBinsYValues[i], yExpected[i])

api.DeleteWorkspace("NotFullBinsOnly")
api.DeleteWorkspace("FullBinsOnly")
api.DeleteWorkspace(inputWs)

def test_hist_workspace_fullBinsOnly(self):
# numpy 1.7 (on rhel7) doesn't have np.full
xmins = np.full((200,), 50.0)
xmins[11] = 3000.0
xmaxs = np.full((200,), 650.0)
xmaxs[12] = 5000.0
deltas = np.full(200, -2.0)
deltas[13] = 100.0

inputWs = api.CreateSampleWorkspace(OutputWorkspace="RebinRagged_hist", WorkspaceType="Histogram", BinWidth=75, XMin=50)

notFullBinsOnlyWs = api.RebinRagged(
InputWorkspace=inputWs, OutputWorkspace="NotFullBinsOnly", XMin=xmins, XMax=xmaxs, Delta=deltas, FullBinsOnly=False
)
fullBinsOnlyWs = api.RebinRagged(
InputWorkspace=inputWs, OutputWorkspace="FullBinsOnly", XMin=xmins, XMax=xmaxs, Delta=deltas, FullBinsOnly=True
)

assert len(fullBinsOnlyWs.readX(0)) != len(notFullBinsOnlyWs.readX(0))
assert fullBinsOnlyWs.isRaggedWorkspace()
api.DeleteWorkspace("NotFullBinsOnly")
api.DeleteWorkspace("FullBinsOnly")
api.DeleteWorkspace(inputWs)


if __name__ == "__main__":
unittest.main()
36 changes: 36 additions & 0 deletions docs/source/algorithms/RebinRagged-v2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ The minimum and maximum values that are specified are interpreted as follows:
The ``Delta`` parameter is required and can either be a single number which is common to all, or one number per spectra.
Positive values are interpreted as constant step-size. Negative are logorithmic.

Please refer to :ref:`Rebin <algm-Rebin>` for a more analytical explanation of `FullBinsOnly`.

Usage
-----

Expand All @@ -39,6 +41,40 @@ This particular use-case, which uses the input workspace's binning, could be don
Xmax=[10.20, 20.8, nan, nan, nan, 9.35])
Sometimes due to the data or logarithmic rebinning, there are incomplete bins left over at the end of the spectrum. These incomplete bins may result in artifacts at the tail end. This can be removed by setting the `FullBinsOnly` parameter to `True`.

.. code-block:: python
from mantid.simpleapi import *
from time import time
## create a workspace to be rebin-ragged
wsname = "ws"
CreateSampleWorkspace(
OutputWorkspace=wsname,
BankPixelWidth=3,
)
GroupDetectors(
InputWorkspace=wsname,
OutputWorkspace=wsname,
GroupingPattern="0-3,4-5,6-8,9-12,13-14,15-17",
)
# rebin the workspace raggedly
xMin = [0.05,0.06,0.1,0.07,0.04, 0.04]
xMax = [0.36,0.41,0.64,0.48,0.48,0.48]
delta = [-0.000401475,-0.000277182,-0.000323453,-0.000430986,-0.000430986,-0.000430986]
RebinRagged(
InputWorkspace=wsname,
XMin=xMin,
XMax=xMax,
Delta=delta,
FullBinsOnly=True,
OutputWorkspace=wsname,
)
.. categories::

.. sourcelink::
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- :ref:`RebinRagged <algm-RebinRagged>` exposes FullBinsOnly from Rebin Algo.

0 comments on commit 30baff0

Please sign in to comment.