Skip to content

Commit

Permalink
Re #6192. Fix code & tests for changes in SpectraAxis class.
Browse files Browse the repository at this point in the history
  • Loading branch information
RussellTaylor committed Mar 8, 2013
1 parent 129ee2f commit 6f4d834
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 70 deletions.
25 changes: 5 additions & 20 deletions Code/Mantid/Framework/API/src/MatrixWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,10 @@ namespace Mantid
*/
void MatrixWorkspace::updateSpectraUsingMap()
{
// The Axis1 needs to be set correctly for the ISpectraDetectorMap to make any sense
if (m_axes.size() < 2) throw std::runtime_error("MatrixWorkspace::updateSpectraUsingMap() needs a SpectraAxis at index 1 to work.");
SpectraAxis * ax1 = dynamic_cast<SpectraAxis *>(m_axes[1]);
if (!ax1)
throw std::runtime_error("MatrixWorkspace::updateSpectraUsingMap() needs a SpectraAxis at index 1 to work.");
if (ax1->length() < this->getNumberHistograms())
throw std::runtime_error("MatrixWorkspace::updateSpectraUsingMap(): the SpectraAxis is shorter than the number of histograms! Cannot run.");

for (size_t wi=0; wi < this->getNumberHistograms(); wi++)
{
specid_t specNo = ax1->spectraNo(wi);

ISpectrum * spec = getSpectrum(wi);
spec->setSpectrumNo(specNo);
specid_t specNo = spec->getSpectrumNo();

std::vector<detid_t> dets = m_spectraMap->getDetectors(specNo);
spec->clearDetectorIDs();
Expand Down Expand Up @@ -221,12 +211,12 @@ namespace Mantid
if( m_axes.size() > 1 && m_axes[1]->isSpectra() )
{
delete m_axes[1];
m_axes[1] = new SpectraAxis(pixelIDs.size(), false);
m_axes[1] = new SpectraAxis(this);
}
else
{
if (m_axes.size() == 0) m_axes.push_back( new NumericAxis(this->blocksize()) );
m_axes.push_back( new SpectraAxis(pixelIDs.size(), false) );
m_axes.push_back( new SpectraAxis(this) );
}

try
Expand All @@ -243,9 +233,6 @@ namespace Mantid
// We keep the entry in the spectraDetectorMap. TODO: Deprecate spectraDetectorMap entirely.
spectramap->addSpectrumEntry(specNo, detId);

// Also set the spectrum number in the axis(1). TODO: Remove this, it is redundant (but it is stuck everywhere)
m_axes[1]->setValue(index, specNo);

if (index < this->getNumberHistograms())
{
ISpectrum * spec = getSpectrum(index);
Expand Down Expand Up @@ -287,12 +274,11 @@ namespace Mantid
if( m_axes.size() > 1 )
{
delete m_axes[1];
m_axes[1] = new SpectraAxis(this->getNumberHistograms(), false);
m_axes[1] = new SpectraAxis(this);
}
else
m_axes.push_back( new SpectraAxis(this->getNumberHistograms(), false) );
m_axes.push_back( new SpectraAxis(this) );

API::Axis *ax1 = getAxis(1);
API::SpectraDetectorMap *newMap = new API::SpectraDetectorMap;

//Go through all the spectra
Expand All @@ -301,7 +287,6 @@ namespace Mantid
ISpectrum * spec = getSpectrum(wi);
specid_t specNo = spec->getSpectrumNo();
newMap->addSpectrumEntries(specNo, spec->getDetectorIDs());
ax1->setValue(wi, specNo);
//std::cout << "generateSpectraMap : wi " << wi << " specNo " << specNo << " detID " << *spec->getDetectorIDs().begin() << std::endl;
}

Expand Down
10 changes: 4 additions & 6 deletions Code/Mantid/Framework/API/test/MatrixWorkspaceTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class MatrixWorkspaceTest : public CxxTest::TestSuite
testWS->initialize(nhist,1,1);
for (size_t i=0; i<testWS->getNumberHistograms(); i++)
{
TS_ASSERT_EQUALS(testWS->getSpectrum(i)->getSpectrumNo(), specid_t(i));
TS_ASSERT_EQUALS(testWS->getSpectrum(i)->getSpectrumNo(), specid_t(i+1));
TS_ASSERT(testWS->getSpectrum(i)->hasDetectorID(detid_t(i)));
}
}
Expand All @@ -117,10 +117,9 @@ class MatrixWorkspaceTest : public CxxTest::TestSuite
boost::scoped_ptr<MatrixWorkspace> testWS(new WorkspaceTester);
testWS->initialize(1,1,1);
// Default one
TS_ASSERT_EQUALS(testWS->getSpectrum(0)->getSpectrumNo(), 0);
TS_ASSERT_EQUALS(testWS->getSpectrum(0)->getSpectrumNo(), 1);

ISpectraDetectorMap * spectraMap = new OneToOneSpectraDetectorMap(1,10);
testWS->replaceAxis(1, new SpectraAxis(10, true));
testWS->replaceSpectraMap(spectraMap);
// Has it been replaced
for (size_t i=0; i<testWS->getNumberHistograms(); i++)
Expand All @@ -135,7 +134,6 @@ class MatrixWorkspaceTest : public CxxTest::TestSuite
boost::shared_ptr<MatrixWorkspace> parent(new WorkspaceTester);
parent->initialize(1,1,1);
ISpectraDetectorMap * spectraMap = new OneToOneSpectraDetectorMap(1,10);
parent->replaceAxis(1, new SpectraAxis(10, true));
parent->replaceSpectraMap(spectraMap);

MatrixWorkspace_sptr copied = WorkspaceFactory::Instance().create(parent,1,1,1);
Expand Down Expand Up @@ -174,7 +172,7 @@ class MatrixWorkspaceTest : public CxxTest::TestSuite

void testReplaceAxis()
{
Axis* ax = new SpectraAxis(1);
Axis* ax = new SpectraAxis(ws.get());
TS_ASSERT_THROWS( ws->replaceAxis(2,ax), Exception::IndexError );
TS_ASSERT_THROWS_NOTHING( ws->replaceAxis(0,ax) );
TS_ASSERT( ws->getAxis(0)->isSpectra() );
Expand Down Expand Up @@ -591,7 +589,7 @@ class MatrixWorkspaceTest : public CxxTest::TestSuite
std::vector<size_t> out;
detid_t offset = -1234;
TS_ASSERT_THROWS_NOTHING( ws->getSpectrumToWorkspaceIndexVector(out, offset) );
TS_ASSERT_EQUALS( offset, 0);
TS_ASSERT_EQUALS( offset, -1);
TS_ASSERT_EQUALS( out.size(), 100);
TS_ASSERT_EQUALS( out[0], 0);
TS_ASSERT_EQUALS( out[1], 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,8 @@ bool CheckWorkspacesMatch::checkAxes(API::MatrixWorkspace_const_sptr ws1, API::M
}

// Use Axis's equality operator to check length and values
if ( ! ax1->operator==(*ax2) )
// Don't check spectra axis as that just takes it values from the ISpectrum (see checkSpectraMap)
if ( ! ax1->isSpectra() && ! ax1->operator==(*ax2) )
{
result = axis_name + " values mismatch";
return false;
Expand Down
19 changes: 17 additions & 2 deletions Code/Mantid/Framework/Algorithms/test/CheckWorkspacesMatchTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,16 +508,31 @@ class CheckWorkspacesMatchTest : public CxxTest::TestSuite
{
if ( !checker.isInitialized() ) checker.initialize();

Mantid::API::MatrixWorkspace_sptr ws1local = WorkspaceCreationHelper::Create2DWorkspace123(2,2);
Mantid::API::MatrixWorkspace_sptr ws2 = WorkspaceCreationHelper::Create2DWorkspace123(2,2);
// Put numeric axes on these workspaces as checkAxes won't test values on spectra axes
Axis * newAxisWS1 = new NumericAxis(ws1local->getAxis(1)->length());
newAxisWS1->setValue(0,1);
newAxisWS1->setValue(1,2);
Axis * newAxisWS2 = new NumericAxis(ws2->getAxis(1)->length());
newAxisWS2->setValue(0,1);
newAxisWS2->setValue(1,2);
ws1local->replaceAxis(1, newAxisWS1);
ws2->replaceAxis(1, newAxisWS2);

// Check that it's all good
TS_ASSERT( (Mantid::API::equals(ws1local, ws2)) );

// Now change a value in one axis
ws2->getAxis(1)->setValue(1,99);

TS_ASSERT_THROWS_NOTHING( checker.setProperty("Workspace1",ws1) );
TS_ASSERT_THROWS_NOTHING( checker.setProperty("Workspace1",ws1local) );
TS_ASSERT_THROWS_NOTHING( checker.setProperty("Workspace2",ws2) );

TS_ASSERT( checker.execute() )
TS_ASSERT_EQUALS( checker.getPropertyValue("Result"), "Axis 1 values mismatch" ) ;
// Same, using the !Mantid::API::equals() function
TS_ASSERT( (!Mantid::API::equals(ws1, ws2)) );
TS_ASSERT( (!Mantid::API::equals(ws1local, ws2)) );
}

void testDifferentYUnit()
Expand Down
1 change: 0 additions & 1 deletion Code/Mantid/Framework/DataHandling/src/LoadEventNexus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2074,7 +2074,6 @@ void LoadEventNexus::createSpectraMapping(const std::string &nxsfile,
}
else
{
workspace->replaceAxis(1, new API::SpectraAxis(spectramap->nSpectra(), *spectramap));
workspace->replaceSpectraMap(spectramap);
}
}
Expand Down
25 changes: 17 additions & 8 deletions Code/Mantid/Framework/DataHandling/src/LoadRaw3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,11 +578,24 @@ namespace Mantid
DataObjects::Workspace2D_sptr localWorkspace = DataObjects::Workspace2D_sptr(
new ManagedRawFileWorkspace2D(fileName, static_cast<int>(option)));
setProg( 0.2 );
progress(m_prog);
loadRunParameters(localWorkspace);
runLoadInstrument(fileName,localWorkspace, 0.2, 0.4 );
setProg( 0.4 );
runLoadMappingTable(fileName,localWorkspace);
progress(m_prog);
runLoadInstrument(fileName,localWorkspace, 0.2, 0.4 );
setProg( 0.5 );
progress(m_prog);
// Since all spectra are being loaded if we get to here,
// we can just set the spectrum numbers to start at 1 and increase monotonically
for (int i = 0; i < m_numberOfSpectra; ++i)
{
localWorkspace->getSpectrum(i)->setSpectrumNo(i+1);
}
setProg( 0.6 );
progress(m_prog);
runLoadMappingTable(fileName,localWorkspace);
setProg( 0.7 );
progress(m_prog);
if (bLoadlogFiles)
{
runLoadLog(fileName,localWorkspace, 0.5, 0.7);
Expand All @@ -591,14 +604,10 @@ namespace Mantid
}
setProtonCharge(localWorkspace->mutableRun());

setProg( 0.7 );
setProg( 0.8 );
progress(m_prog);
for (int i = 0; i < m_numberOfSpectra; ++i)
{
localWorkspace->getAxis(1)->setValue(i, i + 1);
}
setProg( 0.9 );
localWorkspace->populateInstrumentParameters();
setProg( 0.9 );
separateOrexcludeMonitors(localWorkspace, bincludeMonitors, bexcludeMonitors,
bseparateMonitors,m_numberOfSpectra,fileName);
setProg( 1.0 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void AbsManagedWorkspace2D::init(const std::size_t &NVectors, const std::size_t
m_noVectors = NVectors;
m_axes.resize(2);
m_axes[0] = new API::RefAxis(XLength, this);
m_axes[1] = new API::SpectraAxis(NVectors);
m_axes[1] = new API::SpectraAxis(this);
m_XLength = XLength;
m_YLength = YLength;
}
Expand Down
24 changes: 6 additions & 18 deletions Code/Mantid/Framework/DataObjects/src/EventWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,8 @@ namespace DataObjects

//Create axes.
m_axes.resize(2);
//I'm not sure what the heck this first axis is supposed to be; copying from Workspace2D
m_axes[0] = new API::RefAxis(XLength, this);
// Spectrum axis
m_axes[1] = new API::SpectraAxis(m_noVectors);
m_axes[1] = new API::SpectraAxis(this);
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -559,31 +557,21 @@ namespace DataObjects
throw std::runtime_error("EventWorkspace::padSpectra - The spectra-detector map has not been "
"populated.");
}
API::Axis *ax1 = getAxis(1);
if( !ax1 )
{
throw std::runtime_error("EventWorkspace::padSpectra - NULL axis 1");
}
if( !ax1->isSpectra() )
{
throw std::runtime_error("EventWorkspace::padSpectra - Axis 1 is not a SpectraAxis");
}

// Remove all old EventLists and resize the vector to hold everything
this->clearData();
data.resize(numSpectra);
m_noVectors = numSpectra;
PRAGMA_OMP( parallel for )
for( int iwi = 0; iwi < int(numSpectra); ++iwi )
size_t wi = 0;
for ( auto it = spectramap.cbegin(); it != spectramap.cend(); ++it )
{
size_t wi = size_t(iwi);
const specid_t specNo = ax1->spectraNo(wi);
const specid_t specNo = it->first;//ax1->spectraNo(wi);
//Create an event list for here
EventList * newel = new EventList(mru, specNo);
newel->addDetectorIDs( spectramap.getDetectors(specNo) );
newel->setSpectrumNo( specNo );
newel->setDetectorID( it->second );
//Save it in the list
data[wi] = newel;
++wi;
}

// Put on a default set of X vectors, with one bin of 0 & extremely close to zero
Expand Down
3 changes: 1 addition & 2 deletions Code/Mantid/Framework/DataObjects/src/Workspace2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ namespace Mantid
data.resize(m_noVectors);
m_axes.resize(2);
m_axes[0] = new API::RefAxis(XLength, this);
// This axis is always a spectra one for now. By default its values run from 1->NVectors
m_axes[1] = new API::SpectraAxis(m_noVectors);
m_axes[1] = new API::SpectraAxis(this);

MantidVecPtr t1,t2;
t1.access().resize(XLength); //this call initializes array to zero
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ class CompressedWorkspace2DTest : public CxxTest::TestSuite
{
for (size_t i = 0; i < bigWorkspace.getNumberHistograms(); ++i)
{
TS_ASSERT_EQUALS( bigWorkspace.getAxis(1)->spectraNo(i), i+1 );
TS_ASSERT_EQUALS( bigWorkspace.getAxis(1)->spectraNo(i), i );
TS_ASSERT_EQUALS( bigWorkspace.getSpectrum(i)->getSpectrumNo(), i );
TS_ASSERT( bigWorkspace.getSpectrum(i)->hasDetectorID((int)i*100) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ class ManagedWorkspace2DTest : public CxxTest::TestSuite
{
for (size_t i = 0; i < bigWorkspace.getNumberHistograms(); ++i)
{
TS_ASSERT_EQUALS( bigWorkspace.getAxis(1)->spectraNo(i), i+1 );
TS_ASSERT_EQUALS( bigWorkspace.getAxis(1)->spectraNo(i), i );
// Values were set in the constructor
TS_ASSERT_EQUALS( bigWorkspace.getSpectrum(i)->getSpectrumNo(), i );
TS_ASSERT( bigWorkspace.getSpectrum(i)->hasDetectorID((int)i*100) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
#include "MantidAPI/ISpectrum.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidKernel/cow_ptr.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidAPI/RefAxis.h"
#include "MantidAPI/SpectraAxis.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/ISpectraDetectorMap.h"
#include "MantidGeometry/Instrument/DetectorGroup.h"
Expand Down Expand Up @@ -88,15 +89,13 @@ class WorkspaceTester : public MatrixWorkspace
vec[i].dataY().resize(k,1.0);
vec[i].dataE().resize(k,1.0);
vec[i].addDetectorID(detid_t(i));
vec[i].setSpectrumNo(specid_t(i));
vec[i].setSpectrumNo(specid_t(i+1));
}

// Put an 'empty' axis in to test the getAxis method
m_axes.resize(0);
m_axes.push_back(new NumericAxis(1));
m_axes[0]->title() = "1";
m_axes.push_back(new NumericAxis(1));
m_axes[1]->title() = "2";
m_axes.resize(2);
m_axes[0] = new Mantid::API::RefAxis(j, this);
m_axes[1] = new Mantid::API::SpectraAxis(this);

generateSpectraMap();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,6 @@ namespace WorkspaceCreationHelper
while(rHist*rHist<numPixels)rHist++;

Mantid::API::MatrixWorkspace_sptr ws = CreateGroupedWorkspace2DWithRingsAndBoxes(rHist,10,0.1);
ws->replaceAxis(1, new SpectraAxis(rHist, true));
NumericAxis *pAxis0 = new NumericAxis(numBins);

for(size_t i=0;i<numBins;i++){
Expand Down Expand Up @@ -840,7 +839,6 @@ namespace WorkspaceCreationHelper
}
}
// set axis, correspondent to the X-values
ws->replaceAxis(1, new SpectraAxis(numPixels, true));
NumericAxis *pAxis0 = new NumericAxis(numBins);
MantidVec& E_transfer = ws->dataX(0);
for(size_t i=0; i < numBins; i++){
Expand Down

0 comments on commit 6f4d834

Please sign in to comment.