Skip to content

Commit

Permalink
Implemented Workspace::toString for all workspace types.
Browse files Browse the repository at this point in the history
This now returns a human-readable string detailing the structure
of the workspace.
Refs #7708
  • Loading branch information
martyngigg committed Aug 9, 2013
1 parent 297f458 commit d28b436
Show file tree
Hide file tree
Showing 30 changed files with 256 additions and 24 deletions.
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/ExperimentInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ namespace API
/// Clone us
ExperimentInfo * cloneExperimentInfo()const;

/// Returns a string description of the object
const std::string toString() const;

/// Instrument accessors
void setInstrument(const Geometry::Instrument_const_sptr& instr);
/// Returns the parameterized instrument
Expand Down
1 change: 1 addition & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/IEventWorkspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace API
virtual void clearMRU() const = 0;

protected:
virtual const std::string toString() const;
/// Create and return a new InfoNode describing this workspace.
virtual InfoNode *createInfoNode() const;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ namespace API
virtual void setCoordinateSystem(const Mantid::API::SpecialCoordinateSystem coordinateSystem) = 0;

protected:
virtual const std::string toString() const;
/// Create and return a new InfoNode describing this workspace.
virtual InfoNode *createInfoNode() const;
/// Marker set to true when a file-backed workspace needs its back-end file updated (by calling SaveMD(UpdateFileBackEnd=1) )
Expand Down
1 change: 1 addition & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/IMDWorkspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ namespace Mantid
{throw Kernel::Exception::NotImplementedError("This method is not generally implemented ");}

protected:
virtual const std::string toString() const;
/// Create and return a new InfoNode describing this workspace.
virtual InfoNode *createInfoNode() const;
};
Expand Down
1 change: 1 addition & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/ITableWorkspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class MANTID_API_DLL ITableWorkspace: public API::Workspace

/// Return the workspace typeID
virtual const std::string id() const{return "ITableWorkspace";}
virtual const std::string toString() const;
/** Creates a new column
* @param type :: The datatype of the column
* @param name :: The name to assign to the column
Expand Down
10 changes: 9 additions & 1 deletion Code/Mantid/Framework/API/inc/MantidAPI/MatrixWorkspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ namespace Mantid
*/
class MANTID_API_DLL MatrixWorkspace : public IMDWorkspace, public ExperimentInfo
{

private:
using ExperimentInfo::toString;

public:

// The Workspace Factory create-from-parent method needs direct access to the axes.
Expand All @@ -72,7 +76,11 @@ namespace Mantid
void initialize(const std::size_t &NVectors, const std::size_t &XLength, const std::size_t &YLength);
/// Delete
virtual ~MatrixWorkspace();


using IMDWorkspace::toString;
/// String description of state
const std::string toString() const;

/**@name Instrument queries */
//@{
Geometry::IDetector_const_sptr getDetector(const size_t workspaceIndex) const;
Expand Down
5 changes: 2 additions & 3 deletions Code/Mantid/Framework/API/inc/MantidAPI/Workspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ class MANTID_API_DLL Workspace : public Kernel::DataItem
* @return true if the workspace is suitable for multithreaded operations, otherwise false.
*/
virtual bool threadSafe() const { return true; }
/** Returns the name of the workspace **/
virtual std::string toString() const { return name(); }

void virtual setTitle(const std::string&);
void setComment(const std::string&);
Expand All @@ -123,7 +121,8 @@ class MANTID_API_DLL Workspace : public Kernel::DataItem
bool isDirty(const int n=1) const;
/// Get the footprint in memory in bytes.
virtual size_t getMemorySize() const = 0;

/// Returns the memory footprint in sensible units
std::string getMemorySizeAsStr() const;

/// Returns a reference to the WorkspaceHistory
WorkspaceHistory& history() { return m_history; }
Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/WorkspaceGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class MANTID_API_DLL WorkspaceGroup : public Workspace
~WorkspaceGroup();
/// Return a string ID of the class
virtual const std::string id() const { return "WorkspaceGroup"; }
/// Returns a formatted string detailing the contents of the group
virtual const std::string toString() const;

/// The collection itself is considered to take up no space
virtual size_t getMemorySize() const { return 0; }
/// Adds a workspace to the group.
Expand Down
30 changes: 22 additions & 8 deletions Code/Mantid/Framework/API/src/ExperimentInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

#include "MantidAPI/ChopperModel.h"
#include "MantidAPI/InstrumentDataService.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/ModeratorModel.h"

#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/InstrumentDefinitionParser.h"

#include "MantidGeometry/Crystal/OrientedLattice.h"
#include "MantidGeometry/Instrument/ParameterMap.h"
#include "MantidGeometry/Instrument/ParComponentFactory.h"
#include "MantidGeometry/Instrument/XMLlogfile.h"
Expand All @@ -16,9 +14,7 @@
#include "MantidKernel/DateAndTime.h"
#include "MantidKernel/InstrumentInfo.h"
#include "MantidKernel/Property.h"
#include "MantidKernel/SingletonHolder.h"
#include "MantidKernel/Strings.h"
#include "MantidKernel/System.h"
#include "MantidKernel/TimeSeriesProperty.h"

#include <boost/make_shared.hpp>
Expand All @@ -30,9 +26,6 @@
#include <Poco/SAX/SAXParser.h>
#include <Poco/ScopedLock.h>

#include <fstream>
#include <map>

using namespace Mantid::Geometry;
using namespace Mantid::Kernel;
using namespace Poco::XML;
Expand Down Expand Up @@ -104,6 +97,27 @@ namespace API
return out;
}

//---------------------------------------------------------------------------------------

/// @returns A human-readable description of the object
const std::string ExperimentInfo::toString() const
{
std::ostringstream out;

Geometry::Instrument_const_sptr inst = this->getInstrument();
out << "Instrument: " << inst->getName() << " ("
<< inst->getValidFromDate().toFormattedString("%Y-%b-%d")
<< " to " << inst->getValidToDate().toFormattedString("%Y-%b-%d") << ")";
out << "\n";
if (this->sample().hasOrientedLattice())
{
const Geometry::OrientedLattice & latt = this->sample().getOrientedLattice();
out << "Sample: a " << std::fixed << std::setprecision(1) << latt.a() <<", b " << latt.b() << ", c " << latt.c();
out << "; alpha " << std::fixed << std::setprecision(0) << latt.alpha() <<", beta " << latt.beta() << ", gamma " << latt.gamma();
out << "\n";
}
return out.str();
}

//---------------------------------------------------------------------------------------
/** Set the instrument
Expand Down
23 changes: 23 additions & 0 deletions Code/Mantid/Framework/API/src/IEventWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ namespace Mantid
namespace API
{

/**
*/
const std::string IEventWorkspace::toString() const
{
std::ostringstream os;
os << MatrixWorkspace::toString() << "\n";

os << "Events: " + boost::lexical_cast<std::string>(getNumberEvents());
switch ( getEventType() )
{
case WEIGHTED:
os << " (weighted)\n";
break;
case WEIGHTED_NOTIME:
os << " (weighted, no times)\n";
break;
case TOF:
os << "\n";
break;
}
return os.str();
}

/**
* @return :: A pointer to the created info node.
*/
Expand Down
20 changes: 20 additions & 0 deletions Code/Mantid/Framework/API/src/IMDEventWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ namespace API
return !this->isFileBacked();
}

//-----------------------------------------------------------------------------------------------

/**
*/
const std::string IMDEventWorkspace::toString() const
{
std::ostringstream os;
os << IMDWorkspace::toString();

// Now box controller details
std::vector<std::string> stats = getBoxControllerStats();
for (size_t i=0; i < stats.size(); i++)
{
os << stats[i] << "\n";
}

os << "Events: " << getNPoints() << "\n";
return os.str();
}

//-----------------------------------------------------------------------------------------------
/**
* @return :: A pointer to the created info node.
Expand Down
26 changes: 26 additions & 0 deletions Code/Mantid/Framework/API/src/IMDWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,32 @@ namespace Mantid
}

//-----------------------------------------------------------------------------------------------

/**
*/
const std::string IMDWorkspace::toString() const
{
std::ostringstream os;
os << "Title: " + getTitle() << "\n";
for (size_t i=0; i < getNumDims(); i++)
{
Geometry::IMDDimension_const_sptr dim = getDimension(i);
os << "Dim " << i << ": (" << dim->getName() << ") " << dim->getMinimum() << " to " << dim->getMaximum() << " in " << dim->getNBins() << " bins";
// Also show the dimension ID string, if different than name
if (dim->getDimensionId() != dim->getName())
os << ". Id=" << dim->getDimensionId();
os << "\n";
}
if( hasOriginalWorkspace() )
{
os << "Binned from '" << getOriginalWorkspace()->getName();
}
os << "\n";
return os.str();
}

//-----------------------------------------------------------------------------------------------

/**
* @return :: A pointer to the created info node.
*/
Expand Down
11 changes: 11 additions & 0 deletions Code/Mantid/Framework/API/src/ITableWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ namespace API
// Get a reference to the logger
Kernel::Logger& ITableWorkspace::g_log = Kernel::Logger::get("ITableWorkspace");

/**
*/
const std::string ITableWorkspace::toString() const
{
std::ostringstream os;
os << id() << "\n";
os << "Columns: " << boost::lexical_cast<std::string>(columnCount()) << "\n";
os << "Rows: " << boost::lexical_cast<std::string>(rowCount()) << "\n";
os << getMemorySizeAsStr();
return os.str();
}

/** Creates n new columns of the same type
* @param type :: The datatype of the column
Expand Down
30 changes: 30 additions & 0 deletions Code/Mantid/Framework/API/src/MatrixWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,36 @@ namespace Mantid
}
}

/// @returns A human-readable string of the current state
const std::string MatrixWorkspace::toString() const
{
std::ostringstream os;
os << id() << "\n"
<< "Title: " << getTitle() << "\n"
<< "Histograms: " << getNumberHistograms() << "\n"
<< "Bins: " << blocksize() << "\n";

if ( isHistogramData() ) os << "Histogram\n";
else os << "Data points\n";

os << "X axis: ";
if (axes() > 0 )
{
Axis *ax = getAxis(0);
if ( ax && ax->unit() ) os << ax->unit()->caption() << " / " << ax->unit()->label();
else os << "Not set";
}
else
{
os << "N/A";
}
os << "\n"
<< "Y axis: " << YUnitLabel() << "\n";

os << ExperimentInfo::toString();
return os.str();
}

/** Initialize the workspace. Calls the protected init() method, which is implemented in each type of
* workspace. Returns immediately if the workspace is already initialized.
* @param NVectors :: The number of spectra in the workspace (only relevant for a 2D workspace
Expand Down
10 changes: 10 additions & 0 deletions Code/Mantid/Framework/API/src/Workspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/ExperimentInfo.h"
#include "MantidKernel/Memory.h"
#include "MantidKernel/PropertyManager.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"

Expand Down Expand Up @@ -106,6 +107,15 @@ bool Workspace::isDirty(const int n) const
return static_cast<int>(m_history.size()) > n;
}

/**
* Returns the memory footprint in sensible units
* @return A string with the
*/
std::string Workspace::getMemorySizeAsStr() const
{
return Mantid::Kernel::memToString<size_t>(getMemorySize()/1024);
}

/**
* Create this workspace's InfoNode and add it to a parent node.
* @param parentNode :: A node to add to.
Expand Down
19 changes: 19 additions & 0 deletions Code/Mantid/Framework/API/src/WorkspaceGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ WorkspaceGroup::~WorkspaceGroup()
observeADSNotifications(false);
}

/**
* The format is:
* ID
* -- Name1
* -- Name2
* @returns A formatted human-readable string specifying the contents of the group
*/
const std::string WorkspaceGroup::toString() const
{
std::string descr = this->id() + "\n";
Poco::Mutex::ScopedLock _lock(m_mutex);
for(auto it = m_workspaces.begin(); it != m_workspaces.end(); ++it)
{
descr += " -- " + (*it)->name() + "\n";
}
return descr;
}


/**
* Turn on/off observing delete and rename notifications to update the group accordingly
* It can be useful to turn them off when constructing the group.
Expand Down
1 change: 1 addition & 0 deletions Code/Mantid/Framework/API/test/AnalysisDataServiceTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace
class MockWorkspace : public Workspace
{
virtual const std::string id() const { return "MockWorkspace"; }
virtual const std::string toString() const { return ""; }
virtual size_t getMemorySize() const { return 1; }
};
typedef boost::shared_ptr<MockWorkspace> MockWorkspace_sptr;
Expand Down

0 comments on commit d28b436

Please sign in to comment.