Skip to content

Commit

Permalink
Refs #5187: fix dimension labels and volume (area) normalization
Browse files Browse the repository at this point in the history
for WS2Ds in SliceViewer. I did not disable the VolumeNormalization but it gets switched to
no normalization by default when opening (except via the plotSlice method). Instead I fixed the
calculation so that it would be properly doing area normalization, in case anyone wants that.
  • Loading branch information
Janik Zikovsky committed May 2, 2012
1 parent 5ad344b commit 3b68692
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ namespace API

/// Workspace index at which the iterator ends
size_t m_endWI;

/// For numeric axes, this is the size of the bin in the vertical direction.
/// It is 1.0 for spectrum axes
double m_verticalBinSize;
};


Expand Down
19 changes: 15 additions & 4 deletions Code/Mantid/Framework/API/src/MatrixWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ namespace Mantid
{
}
/// the name of the dimennlsion as can be displayed along the axis
virtual std::string getName() const {return m_axis.title();}
virtual std::string getName() const {return m_axis.unit()->caption();}

/// @return the units of the dimension as a string
virtual std::string getUnits() const {return m_axis.unit()->label();}
Expand Down Expand Up @@ -1444,7 +1444,7 @@ namespace Mantid
virtual ~MWXDimension(){};

/// the name of the dimennlsion as can be displayed along the axis
virtual std::string getName() const {return m_ws->getAxis(0)->title();}
virtual std::string getName() const {return m_ws->getAxis(0)->unit()->caption();}

/// @return the units of the dimension as a string
virtual std::string getUnits() const {return m_ws->getAxis(0)->unit()->label();}
Expand Down Expand Up @@ -1589,6 +1589,7 @@ namespace Mantid

// If a spectra/text axis, just use the Y coord as the workspace index
size_t wi = size_t(y);
double yBinSize = 1.0;
if (ax1)
{
const MantidVec & yVals = ax1->getValues();
Expand All @@ -1600,6 +1601,15 @@ namespace Mantid
}
// The workspace index is the point in the vector that we found
wi = it - yVals.begin();

// Find the size of the bin in Y, if needed
if (normalization == VolumeNormalization)
{
if ((it+1) == yVals.end() && (yVals.size() > 1))
yBinSize = *it - *(it-1);
else
yBinSize = *(it+1) - *it;
}
}

if (wi < this->getNumberHistograms())
Expand All @@ -1623,9 +1633,10 @@ namespace Mantid
case NoNormalization:
return y;
case VolumeNormalization:
// TODO: calculate the Y size
return y / (X[i] - X[i-1]);
// Divide the signal by the area
return y / (yBinSize*(X[i] - X[i-1]));
case NumEventsNormalization:
// Not yet implemented, may not make sense
return y;
}
// This won't happen
Expand Down
22 changes: 18 additions & 4 deletions Code/Mantid/Framework/API/src/MatrixWorkspaceMDIterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "MantidKernel/System.h"
#include "MantidKernel/VMD.h"
#include "MantidKernel/cow_ptr.h"
#include "MantidAPI/NumericAxis.h"

using namespace Mantid::Kernel;
using namespace Mantid::API;
Expand Down Expand Up @@ -94,6 +95,21 @@ namespace API
m_Y = m_ws->readY(m_workspaceIndex);
m_errorIsCached = false;
m_center[1] = m_dimY->getX(m_workspaceIndex);

// Find the vertical bin size
m_verticalBinSize = 1.0;
NumericAxis * ax1 = dynamic_cast<NumericAxis*>(m_ws->getAxis(1));
if (ax1)
{
const MantidVec & yVals = ax1->getValues();
if (yVals.size() > 1)
{
if (m_workspaceIndex < yVals.size()-1)
m_verticalBinSize = yVals[m_workspaceIndex+1] - yVals[m_workspaceIndex];
else
m_verticalBinSize = yVals[m_workspaceIndex] - yVals[m_workspaceIndex-1];
}
}
}
}

Expand Down Expand Up @@ -163,8 +179,7 @@ namespace API
case NoNormalization:
return m_Y[m_xIndex];
case VolumeNormalization:
// TODO: calculate the Y size
return m_Y[m_xIndex] / (m_X[m_xIndex+1] - m_X[m_xIndex]);
return m_Y[m_xIndex] / (m_verticalBinSize * (m_X[m_xIndex+1] - m_X[m_xIndex]));
case NumEventsNormalization:
return m_Y[m_xIndex];
}
Expand All @@ -181,8 +196,7 @@ namespace API
case NoNormalization:
return getError();
case VolumeNormalization:
// TODO: calculate the Y size
return getError() / (m_X[m_xIndex+1] - m_X[m_xIndex]);
return getError() / (m_verticalBinSize * (m_X[m_xIndex+1] - m_X[m_xIndex]));
case NumEventsNormalization:
return getError();
}
Expand Down
52 changes: 34 additions & 18 deletions Code/Mantid/Framework/API/test/MatrixWorkspaceMDIteratorTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,34 @@ class MatrixWorkspaceMDIteratorTest : public CxxTest::TestSuite
boost::shared_ptr<MatrixWorkspace> ws(new WorkspaceTester());
// Matrix with 4 spectra, 5 bins each
ws->initialize(4,6,5);
NumericAxis * ax1 = new NumericAxis(4);
for (size_t wi=0; wi<4; wi++)
{
ax1->setValue(wi, double(wi)*2.0);
for (size_t x=0; x<6; x++)
{
ws->dataX(wi)[x] = double(x);
ws->dataX(wi)[x] = double(x)*2.0;
if (x<5)
{
ws->dataY(wi)[x] = double(wi*10 + x);
ws->dataE(wi)[x] = double((wi*10 + x)*2);
}
}
}
Instrument_sptr inst(new Instrument("TestInstrument"));
ws->setInstrument(inst);
// We get a 1:1 map by default so the detector ID should match the spectrum number
for( size_t i = 0; i < ws->getNumberHistograms(); ++i )
{
// Create a detector for each spectra
Detector * det = new Detector("pixel", static_cast<detid_t>(i), inst.get());
inst->add(det);
inst->markAsDetector(det);
ws->getSpectrum(i)->addDetectorID(static_cast<detid_t>(i));
}
ws->replaceAxis(1, ax1);

Instrument_sptr inst(new Instrument("TestInstrument"));
ws->setInstrument(inst);
// We get a 1:1 map by default so the detector ID should match the spectrum number
for( size_t i = 0; i < ws->getNumberHistograms(); ++i )
{
// Create a detector for each spectra
Detector * det = new Detector("pixel", static_cast<detid_t>(i), inst.get());
inst->add(det);
inst->markAsDetector(det);
ws->getSpectrum(i)->addDetectorID(static_cast<detid_t>(i));
}

return ws;
return ws;
}

void test_iterating()
Expand All @@ -60,6 +64,18 @@ class MatrixWorkspaceMDIteratorTest : public CxxTest::TestSuite
it->next();
TS_ASSERT_DELTA( it->getSignal(), 1.0, 1e-5);
TS_ASSERT_DELTA( it->getError(), 2.0, 1e-5);

it->setNormalization(NoNormalization);
TS_ASSERT_DELTA( it->getNormalizedSignal(), 1.0, 1e-5);
TS_ASSERT_DELTA( it->getNormalizedError(), 2.0, 1e-5);
// Area of each bin is 4.0
it->setNormalization(VolumeNormalization);
TS_ASSERT_DELTA( it->getNormalizedSignal(), 1.0 / 4.0, 1e-5);
TS_ASSERT_DELTA( it->getNormalizedError(), 2.0 / 4.0, 1e-5);
it->setNormalization(NumEventsNormalization);
TS_ASSERT_DELTA( it->getNormalizedSignal(), 1.0, 1e-5);
TS_ASSERT_DELTA( it->getNormalizedError(), 2.0, 1e-5);

it->next();
it->next();
it->next();
Expand All @@ -70,8 +86,8 @@ class MatrixWorkspaceMDIteratorTest : public CxxTest::TestSuite
// Workspace index 1, x index 1
TS_ASSERT_DELTA( it->getSignal(), 11.0, 1e-5);
TS_ASSERT_DELTA( it->getError(), 22.0, 1e-5);
TS_ASSERT_DELTA( it->getCenter()[0], 1.5, 1e-5);
TS_ASSERT_DELTA( it->getCenter()[1], 1.0, 1e-5);
TS_ASSERT_DELTA( it->getCenter()[0], 3.0, 1e-5);
TS_ASSERT_DELTA( it->getCenter()[1], 2.0, 1e-5);
}


Expand All @@ -96,9 +112,9 @@ class MatrixWorkspaceMDIteratorTest : public CxxTest::TestSuite
TS_ASSERT_DELTA( it->getSignal(), double(i)*10 + 1.0, 1e-5);
TS_ASSERT_DELTA( it->getError(), double(i)*20 + 2.0, 1e-5);
// Coordinates at X index = 1
TS_ASSERT_DELTA( it->getCenter()[0], 1.5, 1e-5);
TS_ASSERT_DELTA( it->getCenter()[0], 3.0, 1e-5);
// And this coordinate is the spectrum number
TS_ASSERT_DELTA( it->getCenter()[1], double(i), 1e-5);
TS_ASSERT_DELTA( it->getCenter()[1], double(i*2), 1e-5);
TS_ASSERT( it->next() );
TS_ASSERT( it->next() );
TS_ASSERT( it->next() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class EXPORT_OPT_MANTIDQT_SLICEVIEWER SliceViewer : public QWidget
void setXYCenter(double x, double y);
void openFromXML(const QString & xml);
void toggleLineMode(bool);
void setNormalization(Mantid::API::MDNormalization norm);
void setNormalization(Mantid::API::MDNormalization norm, bool update=true);
Mantid::API::MDNormalization getNormalization() const;

/// Dynamic Rebinning-related Python bindings
Expand Down Expand Up @@ -121,7 +121,9 @@ public slots:
void colorRangeChanged();
void loadColorMapSlot();
void setTransparentZeros(bool transparent);
void changeNormalization();
void changeNormalizationNone();
void changeNormalizationVolume();
void changeNormalizationNumEvents();

// Buttons or actions
void clearLine();
Expand Down
7 changes: 5 additions & 2 deletions Code/Mantid/MantidQt/SliceViewer/src/LineViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -974,8 +974,11 @@ void LineViewer::showFull()
{
MantidQwtMatrixWorkspaceData curveData(sliceMatrix, 0, false /*not logScale*/);
m_fullCurve->setData(curveData);
m_plot->setAxisTitle( QwtPlot::xBottom, QString::fromStdString( sliceMatrix->getAxis(0)->unit()->label() ));;
m_plot->setAxisTitle( QwtPlot::yLeft, QString::fromStdString( sliceMatrix->YUnitLabel() ));;
Unit_const_sptr unit = sliceMatrix->getAxis(0)->unit();
std::string title = unit->caption() + " (" + unit->label() + ")";
m_plot->setAxisTitle( QwtPlot::xBottom, QString::fromStdString(title));;
title = sliceMatrix->YUnit() + " (" + sliceMatrix->YUnitLabel() + ")";
m_plot->setAxisTitle( QwtPlot::yLeft, QString::fromStdString(title));;
}
else
{
Expand Down
35 changes: 17 additions & 18 deletions Code/Mantid/MantidQt/SliceViewer/src/SliceViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,22 +294,22 @@ void SliceViewer::initMenus()
m_menuView->addAction(action);
action->setActionGroup(group);
action->setCheckable(true);
connect(action, SIGNAL(triggered()), this, SLOT(changeNormalization()));
connect(action, SIGNAL(triggered()), this, SLOT(changeNormalizationNone()));
m_actionNormalizeNone = action;

action = new QAction(QPixmap(), "Volume Normalization", this);
m_menuView->addAction(action);
action->setActionGroup(group);
action->setCheckable(true);
action->setChecked(true);
connect(action, SIGNAL(triggered()), this, SLOT(changeNormalization()));
connect(action, SIGNAL(triggered()), this, SLOT(changeNormalizationVolume()));
m_actionNormalizeVolume = action;

action = new QAction(QPixmap(), "Num. Events Normalization", this);
m_menuView->addAction(action);
action->setActionGroup(group);
action->setCheckable(true);
connect(action, SIGNAL(triggered()), this, SLOT(changeNormalization()));
connect(action, SIGNAL(triggered()), this, SLOT(changeNormalizationNumEvents()));
m_actionNormalizeNumEvents = action;


Expand Down Expand Up @@ -522,6 +522,10 @@ void SliceViewer::setWorkspace(Mantid::API::IMDWorkspace_sptr ws)
m_syncRebinMode->setEnabled(!matrix);
m_syncRebinLock->setEnabled(!matrix);

// Go to no normalization by default for MatrixWorkspaces
if (matrix)
this->setNormalization(Mantid::API::NoNormalization, false /* without updating */ );

// Emit the signal that we changed the workspace
emit workspaceChanged();

Expand Down Expand Up @@ -692,28 +696,23 @@ void SliceViewer::setTransparentZeros(bool transparent)

//------------------------------------------------------------------------------------
/// Slot called when changing the normalization menu
void SliceViewer::changeNormalization()
{
Mantid::API::MDNormalization normalization;
if (m_actionNormalizeNone->isChecked())
normalization = Mantid::API::NoNormalization;
else if (m_actionNormalizeVolume->isChecked())
normalization = Mantid::API::VolumeNormalization;
else if (m_actionNormalizeNumEvents->isChecked())
normalization = Mantid::API::NumEventsNormalization;
else
normalization = Mantid::API::NoNormalization;
void SliceViewer::changeNormalizationNone()
{ this->setNormalization(Mantid::API::NoNormalization, true); }

this->setNormalization(normalization);
}
void SliceViewer::changeNormalizationVolume()
{ this->setNormalization(Mantid::API::VolumeNormalization, true); }

void SliceViewer::changeNormalizationNumEvents()
{ this->setNormalization(Mantid::API::NumEventsNormalization, true); }


//------------------------------------------------------------------------------------
/** Set the normalization mode for viewing the data
*
* @param norm :: MDNormalization enum. 0=none; 1=volume; 2=# of events
* @param update :: update the displayed image. If false, just sets it and shows the checkboxes.
*/
void SliceViewer::setNormalization(Mantid::API::MDNormalization norm)
void SliceViewer::setNormalization(Mantid::API::MDNormalization norm, bool update)
{
m_actionNormalizeNone->blockSignals(true);
m_actionNormalizeVolume->blockSignals(true);
Expand All @@ -728,7 +727,7 @@ void SliceViewer::setNormalization(Mantid::API::MDNormalization norm)
m_actionNormalizeNumEvents->blockSignals(false);

m_data->setNormalization(norm);
this->updateDisplay();
if (update) this->updateDisplay();
}

//------------------------------------------------------------------------------------
Expand Down

0 comments on commit 3b68692

Please sign in to comment.