Skip to content

Commit

Permalink
refs #9257. Add composite cluster.
Browse files Browse the repository at this point in the history
A composite cluster will fix the problem I've been having. Namely that the clusters are processed in parallel to get the uniform min, but the order that they are processed in will affect the end labeling. If clusters are set up as composites before hand. This will not be a problem.
  • Loading branch information
OwenArnold committed Apr 14, 2014
1 parent ed5270c commit 7561484
Show file tree
Hide file tree
Showing 7 changed files with 396 additions and 10 deletions.
4 changes: 4 additions & 0 deletions Code/Mantid/Framework/Crystal/CMakeLists.txt
Expand Up @@ -7,6 +7,7 @@ set ( SRC_FILES
src/ClearUB.cpp
src/Cluster.cpp
src/CombinePeaksWorkspaces.cpp
src/CompositeCluster.cpp
src/ConnectedComponentLabeling.cpp
src/DiffPeaksWorkspaces.cpp
src/DisjointElement.cpp
Expand Down Expand Up @@ -71,6 +72,7 @@ set ( INC_FILES
inc/MantidCrystal/ClearUB.h
inc/MantidCrystal/Cluster.h
inc/MantidCrystal/CombinePeaksWorkspaces.h
inc/MantidCrystal/CompositeCluster.h
inc/MantidCrystal/ConnectedComponentLabeling.h
inc/MantidCrystal/DiffPeaksWorkspaces.h
inc/MantidCrystal/DisjointElement.h
Expand All @@ -84,6 +86,7 @@ set ( INC_FILES
inc/MantidCrystal/GoniometerAnglesFromPhiRotation.h
inc/MantidCrystal/HardThresholdBackground.h
inc/MantidCrystal/HasUB.h
inc/MantidCrystal/ICluster.h
inc/MantidCrystal/IndexPeaks.h
inc/MantidCrystal/IndexSXPeaks.h
inc/MantidCrystal/IntegratePeakTimeSlices.h
Expand Down Expand Up @@ -134,6 +137,7 @@ set ( TEST_FILES
ClearUBTest.h
ClusterTest.h
CombinePeaksWorkspacesTest.h
CompositeClusterTest.h
ConnectedComponentLabelingTest.h
DiffPeaksWorkspacesTest.h
DisjointElementTest.h
Expand Down
12 changes: 4 additions & 8 deletions Code/Mantid/Framework/Crystal/inc/MantidCrystal/Cluster.h
@@ -1,6 +1,7 @@
#ifndef MANTID_CRYSTAL_CLUSTER_H_
#define MANTID_CRYSTAL_CLUSTER_H_

#include "MantidCrystal/ICluster.h"
#include "MantidKernel/System.h"
#include "MantidCrystal/DisjointElement.h"
#include <vector>
Expand All @@ -10,10 +11,6 @@

namespace Mantid
{
namespace API
{
class IMDHistoWorkspace;
}
namespace Crystal
{

Expand All @@ -39,13 +36,11 @@ namespace Mantid
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport Cluster
class DLLExport Cluster : public ICluster
{

public:

typedef boost::tuple<double, double> ClusterIntegratedValues;

/// Constructor
Cluster(const size_t& label);

Expand Down Expand Up @@ -76,6 +71,7 @@ namespace Mantid
/// Merge and own
void attachCluster(boost::shared_ptr<const Cluster>& toOwn);


private:

/// Disabled copy construction
Expand All @@ -98,4 +94,4 @@ namespace Mantid
} // namespace Crystal
} // namespace Mantid

#endif /* MANTID_CRYSTAL_CLUSTER_H_ */
#endif /* MANTID_CRYSTAL_CLUSTER_H_ */
81 changes: 81 additions & 0 deletions Code/Mantid/Framework/Crystal/inc/MantidCrystal/CompositeCluster.h
@@ -0,0 +1,81 @@
#ifndef MANTID_CRYSTAL_COMPOSITECLUSTER_H_
#define MANTID_CRYSTAL_COMPOSITECLUSTER_H_

#include "MantidKernel/System.h"
#include "MantidCrystal/ICluster.h"
#include <boost/shared_ptr.hpp>
#include <boost/optional.hpp>

namespace Mantid
{
namespace Crystal
{

/** CompositeCluster : Cluster made by by merging other IClusters.
Copyright &copy; 2014 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport CompositeCluster: ICluster
{
public:
CompositeCluster();
virtual ~CompositeCluster();

/// integrate the cluster
ICluster::ClusterIntegratedValues integrate(
boost::shared_ptr<const Mantid::API::IMDHistoWorkspace> ws) const;

/// Apply labels to the workspace
void writeTo(boost::shared_ptr<Mantid::API::IMDHistoWorkspace> ws) const;

/// Get the cluster label
size_t getLabel() const;

/// Number of indexes tracked
size_t size() const;

/// Track a linear IMDHistoWorkspace index that belongs to the cluster.
void addIndex(const size_t& index);

/// Resolve the proper label for this cluster.
void toUniformMinimum(std::vector<DisjointElement>& disjointSet);

/// Own.
void add(boost::shared_ptr<ICluster>& toOwn);

private:

/// Disabled copy construction
CompositeCluster(const CompositeCluster&);
/// Disabled assignement
CompositeCluster& operator=(const CompositeCluster&);

/// Label used by cluster
boost::optional<size_t> m_label;
/// Attached clusters.
std::vector<boost::shared_ptr<ICluster> > m_ownedClusters;

};

} // namespace Crystal
} // namespace Mantid

#endif /* MANTID_CRYSTAL_COMPOSITECLUSTER_H_ */
74 changes: 74 additions & 0 deletions Code/Mantid/Framework/Crystal/inc/MantidCrystal/ICluster.h
@@ -0,0 +1,74 @@
#ifndef MANTID_CRYSTAL_ICLUSTER_H_
#define MANTID_CRYSTAL_ICLUSTER_H_

#include "MantidKernel/System.h"
#include "MantidCrystal/DisjointElement.h"
#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/tuple/tuple.hpp>

namespace Mantid
{
namespace API
{
class IMDHistoWorkspace;
}
namespace Crystal
{

/** ICluster : Abstract cluster. Identifies neighbour elements in an image that are connected.
Copyright &copy; 2014 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport ICluster
{
public:

typedef boost::tuple<double, double> ClusterIntegratedValues;

/// integrate the cluster
virtual ClusterIntegratedValues integrate(
boost::shared_ptr<const Mantid::API::IMDHistoWorkspace> ws) const = 0;

/// Apply labels to the workspace
virtual void writeTo(boost::shared_ptr<Mantid::API::IMDHistoWorkspace> ws) const = 0;

/// Get the cluster label
virtual size_t getLabel() const = 0;

/// Number of indexes tracked
virtual size_t size() const = 0;

/// Track a linear IMDHistoWorkspace index that belongs to the cluster.
virtual void addIndex(const size_t& index) = 0;

/// Resolve the proper label for this cluster.
virtual void toUniformMinimum(std::vector<DisjointElement>& disjointSet) = 0;

/// Virtual destructor
virtual ~ICluster(){};

};

} // namespace Crystal
} // namespace Mantid

#endif /* MANTID_CRYSTAL_ICLUSTER_H_ */
4 changes: 2 additions & 2 deletions Code/Mantid/Framework/Crystal/src/Cluster.cpp
Expand Up @@ -59,7 +59,7 @@ namespace Mantid
}
}

Cluster::ClusterIntegratedValues Cluster::integrate(Mantid::API::IMDHistoWorkspace_const_sptr ws) const
ICluster::ClusterIntegratedValues Cluster::integrate(Mantid::API::IMDHistoWorkspace_const_sptr ws) const
{
double errorIntSQ = 0;
double sigInt = 0;
Expand Down Expand Up @@ -117,4 +117,4 @@ namespace Mantid


} // namespace Crystal
} // namespace Mantid
} // namespace Mantid
103 changes: 103 additions & 0 deletions Code/Mantid/Framework/Crystal/src/CompositeCluster.cpp
@@ -0,0 +1,103 @@
#include "MantidCrystal/CompositeCluster.h"
#include <stdexcept>
namespace Mantid
{
namespace Crystal
{

//----------------------------------------------------------------------------------------------
/** Constructor
*/
CompositeCluster::CompositeCluster()
{
}

//----------------------------------------------------------------------------------------------
/** Destructor
*/
CompositeCluster::~CompositeCluster()
{
}

ICluster::ClusterIntegratedValues CompositeCluster::integrate(
boost::shared_ptr<const Mantid::API::IMDHistoWorkspace> ws) const
{

double errorIntSQ = 0;
double sigInt = 0;
// Integrate owned clusters and add those results too.
for (size_t i = 0; i < m_ownedClusters.size(); ++i)
{
auto integratedValues = m_ownedClusters[i]->integrate(ws);
sigInt += integratedValues.get<0>();
errorIntSQ += integratedValues.get<1>();
}
return ClusterIntegratedValues(sigInt, errorIntSQ);
}

void CompositeCluster::writeTo(boost::shared_ptr<Mantid::API::IMDHistoWorkspace> ws) const
{
for (size_t i = 0; i < m_ownedClusters.size(); ++i)
{
m_ownedClusters[i]->writeTo(ws);
}
}

size_t CompositeCluster::getLabel() const
{
if (!m_label.is_initialized())
{
throw std::runtime_error("No child IClusters. CompositeCluster::getLabel() is not supported.");
}
else
{
return m_label.get(); // Assumes all are uniform.
}
}

size_t CompositeCluster::size() const
{
size_t size = 0;
for (size_t i = 0; i < m_ownedClusters.size(); ++i)
{
size += m_ownedClusters[i]->size();
}
return size;
}

void CompositeCluster::addIndex(const size_t& index)
{
throw std::runtime_error("addIndex not implemented on CompositeCluster");
}

void CompositeCluster::toUniformMinimum(std::vector<DisjointElement>& disjointSet)
{
if (!m_ownedClusters.empty())
{
ICluster* minCluster = m_ownedClusters.front().get();
size_t minLabel = minCluster->getLabel();
for (size_t i = 1; i < m_ownedClusters.size(); ++i)
{
size_t temp = m_ownedClusters[i]->getLabel();
if (temp < minLabel)
{
minLabel = temp;
minCluster = m_ownedClusters[i].get();
}
}
m_label = minLabel;

for (size_t i = 0; i < m_ownedClusters.size(); ++i)
{
minCluster->setAsParentOn(m_ownedClusters[i].get());// TODO
}
}
}

void CompositeCluster::add(boost::shared_ptr<ICluster>& toOwn)
{
m_ownedClusters.push_back(toOwn);
}

} // namespace Crystal
} // namespace Mantid

0 comments on commit 7561484

Please sign in to comment.