Skip to content
Snippets Groups Projects
SortXAxisTest.h 14.1 KiB
Newer Older
#ifndef MANTID_ALGORITHMS_SORTXAXISTEST_H_
#define MANTID_ALGORITHMS_SORTXAXISTEST_H_

#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAlgorithms/SortXAxis.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidDataObjects/WorkspaceCreation.h"
#include "MantidHistogramData/Histogram.h"
#include <cxxtest/TestSuite.h>

/**
  Copyright &copy; 2018 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge
  National Laboratory & European Spallation Source
  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>
*/

using namespace Mantid;
using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace Mantid::Algorithms;
using namespace Mantid::DataObjects;
using namespace Mantid::HistogramData;

MatrixWorkspace_sptr createWorkspaceE(const std::vector<double> xData,
                                      const std::vector<double> yData,
                                      const std::vector<double> eData,
                                      const int nSpec = 1) {

  Workspace2D_sptr outputWorkspace = create<DataObjects::Workspace2D>(
      nSpec, Mantid::HistogramData::Histogram(
                 Mantid::HistogramData::Points(xData.size())));
  for (int i = 0; i < nSpec; ++i) {
    outputWorkspace->mutableY(i) = yData;
    outputWorkspace->mutableE(i) = eData;
    outputWorkspace->mutableX(i) = xData;
  }
  return outputWorkspace;
}

MatrixWorkspace_sptr createHistoWorkspaceE(const std::vector<double> xData,
                                           const std::vector<double> yData,
                                           const std::vector<double> eData,
                                           const int nSpec = 1) {
  Workspace2D_sptr outputWorkspace = create<DataObjects::Workspace2D>(
      nSpec, Mantid::HistogramData::Histogram(
                 Mantid::HistogramData::BinEdges(xData.size())));
  for (int i = 0; i < nSpec; ++i) {
    outputWorkspace->mutableY(i) = yData;
    outputWorkspace->mutableE(i) = eData;
    outputWorkspace->mutableX(i) = xData;
  return outputWorkspace;
}

MatrixWorkspace_sptr createWorkspaceDx(const std::vector<double> xData,
                                       const std::vector<double> yData,
                                       const std::vector<double> dxData,
                                       const int nSpec = 1) {

  Workspace2D_sptr outputWorkspace = create<DataObjects::Workspace2D>(
      nSpec, Mantid::HistogramData::Histogram(
                 Mantid::HistogramData::Points(xData.size())));
  for (int i = 0; i < nSpec; ++i) {
    outputWorkspace->mutableY(i) = yData;
    outputWorkspace->mutableX(i) = xData;
    outputWorkspace->setPointStandardDeviations(i, dxData);
  }
  return outputWorkspace;
}

MatrixWorkspace_sptr createHistoWorkspaceDx(const std::vector<double> xData,
                                            const std::vector<double> yData,
                                            const std::vector<double> dxData,
                                            const int nSpec = 1) {

  Workspace2D_sptr outputWorkspace = create<DataObjects::Workspace2D>(
      nSpec, Mantid::HistogramData::Histogram(
                 Mantid::HistogramData::BinEdges(xData.size())));
  for (int i = 0; i < nSpec; ++i) {
    outputWorkspace->mutableY(i) = yData;
    outputWorkspace->mutableX(i) = xData;
    outputWorkspace->setPointStandardDeviations(i, dxData);
  }
  return outputWorkspace;
}

MatrixWorkspace_sptr createHistoWorkspace(const std::vector<double> xData,
                                          const std::vector<double> yData,
                                          const int nSpec = 1) {

  Workspace2D_sptr outputWorkspace = create<DataObjects::Workspace2D>(
      nSpec, Mantid::HistogramData::Histogram(
                 Mantid::HistogramData::BinEdges(xData.size())));
  for (int i = 0; i < nSpec; ++i) {
    outputWorkspace->mutableY(i) = yData;
    outputWorkspace->mutableX(i) = xData;
  }
  return outputWorkspace;
}
} // namespace

class SortXAxisTest : public CxxTest::TestSuite {
  // This pair of boilerplate methods prevent the suite being created statically
  // This means the constructor isn't called when running other tests
  static SortXAxisTest *createSuite() { return new SortXAxisTest(); }
  static void destroySuite(SortXAxisTest *suite) { delete suite; }

  void testXAscending() {
    std::vector<double> xData = {1, 2, 3};
    std::vector<double> yData = {1, 2, 3};
    std::vector<double> eData = {1, 2, 3};
    MatrixWorkspace_sptr unsortedws = createWorkspaceE(xData, yData, eData);
    TS_ASSERT_THROWS_NOTHING(alg.initialize())
    TS_ASSERT(alg.isInitialized())
    TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", unsortedws));
    TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "sortedws"));
    TS_ASSERT_THROWS_NOTHING(alg.execute());

    MatrixWorkspace_sptr sortedws;
    TS_ASSERT_THROWS_NOTHING(
        sortedws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "sortedws"));
    TS_ASSERT(sortedws);

    TS_ASSERT_EQUALS(sortedws->x(0), xData);
    TS_ASSERT_EQUALS(sortedws->y(0), yData);
    TS_ASSERT_EQUALS(sortedws->e(0), eData);

    TS_ASSERT_THROWS_NOTHING(
        AnalysisDataService::Instance().remove("unsortedws"));
    TS_ASSERT_THROWS_NOTHING(
        AnalysisDataService::Instance().remove("sortedws"));
  void testXDescending() {
    std::vector<double> xData = {3, 2, 1};
    std::vector<double> sortedXData = {1, 2, 3};
    std::vector<double> yData = {1, 2, 3};
    std::vector<double> reverseYData = {3, 2, 1};
    std::vector<double> eData = {1, 2, 3};
    std::vector<double> reverseEData = {3, 2, 1};
    MatrixWorkspace_sptr unsortedws = createWorkspaceE(xData, yData, eData);
    SortXAxis alg;
    TS_ASSERT_THROWS_NOTHING(alg.initialize())
    TS_ASSERT(alg.isInitialized())
    TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", unsortedws));
    TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "sortedws"));
    TS_ASSERT_THROWS_NOTHING(alg.execute());
    MatrixWorkspace_sptr sortedws;
    TS_ASSERT_THROWS_NOTHING(
        sortedws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "sortedws"));
    TS_ASSERT(sortedws);
    TS_ASSERT_EQUALS(sortedws->x(0), sortedXData);
    TS_ASSERT_EQUALS(sortedws->y(0), reverseYData);
    TS_ASSERT_EQUALS(sortedws->e(0), reverseEData);
    TS_ASSERT_THROWS_NOTHING(
        AnalysisDataService::Instance().remove("unsortedws"));
    TS_ASSERT_THROWS_NOTHING(
        AnalysisDataService::Instance().remove("sortedws"));
  }
  void testOnMultipleSpectrum() {
    std::vector<double> xData = {3, 2, 1};
    std::vector<double> sortedXData = {1, 2, 3};
    std::vector<double> yData = {1, 2, 3};
    std::vector<double> reverseYData = {3, 2, 1};
    std::vector<double> eData = {1, 2, 3};
    std::vector<double> reverseEData = {3, 2, 1};
    MatrixWorkspace_sptr unsortedws = createWorkspaceE(xData, yData, eData, 2);

    SortXAxis alg;
    TS_ASSERT_THROWS_NOTHING(alg.initialize())
    TS_ASSERT(alg.isInitialized())
    TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", unsortedws));
    TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "sortedws"));
    TS_ASSERT_THROWS_NOTHING(alg.execute());

    MatrixWorkspace_sptr sortedws;
    TS_ASSERT_THROWS_NOTHING(
        sortedws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "sortedws"));
    TS_ASSERT(sortedws);

    TS_ASSERT_EQUALS(sortedws->x(0), sortedXData);
    TS_ASSERT_EQUALS(sortedws->y(0), reverseYData);
    TS_ASSERT_EQUALS(sortedws->e(0), reverseEData);

    TS_ASSERT_EQUALS(sortedws->x(1), sortedXData);
    TS_ASSERT_EQUALS(sortedws->y(1), reverseYData);
    TS_ASSERT_EQUALS(sortedws->e(1), reverseEData);

    TS_ASSERT_THROWS_NOTHING(
        AnalysisDataService::Instance().remove("unsortedws"));
    TS_ASSERT_THROWS_NOTHING(
        AnalysisDataService::Instance().remove("sortedws"));
  }

  void testSortsXHistogramAscending() {
    std::vector<double> xData = {1, 2, 3, 4};
    std::vector<double> yData = {1, 2, 3};
    std::vector<double> eData = {1, 2, 3};

    MatrixWorkspace_sptr unsortedws =
        createHistoWorkspaceE(xData, yData, eData);
    SortXAxis alg;
    TS_ASSERT_THROWS_NOTHING(alg.initialize())
    TS_ASSERT(alg.isInitialized())
    TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", unsortedws));
    TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "sortedws"));
    TS_ASSERT_THROWS_NOTHING(alg.execute());

    MatrixWorkspace_sptr sortedws;
    TS_ASSERT_THROWS_NOTHING(
        sortedws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "sortedws"));
    TS_ASSERT(sortedws);

    TS_ASSERT_EQUALS(sortedws->x(0), xData);
    TS_ASSERT_EQUALS(sortedws->y(0), yData);
    TS_ASSERT_EQUALS(sortedws->e(0), eData);

    TS_ASSERT_THROWS_NOTHING(
        AnalysisDataService::Instance().remove("unsortedws"));
    TS_ASSERT_THROWS_NOTHING(
        AnalysisDataService::Instance().remove("sortedws"));
  }

  void testSortsXHistogramDescending() {
    std::vector<double> xData = {4, 3, 2, 1};
    std::vector<double> sortedXData = {1, 2, 3, 4};
    std::vector<double> yData = {1, 2, 3};
    std::vector<double> reverseYData = {3, 2, 1};
    std::vector<double> eData = {1, 2, 3};
    std::vector<double> reverseEData = {3, 2, 1};

    MatrixWorkspace_sptr unsortedws =
        createHistoWorkspaceE(xData, yData, eData);

    SortXAxis alg;
    TS_ASSERT_THROWS_NOTHING(alg.initialize())
    TS_ASSERT(alg.isInitialized())
    TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", unsortedws));
    TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "sortedws"));
    TS_ASSERT_THROWS_NOTHING(alg.execute());

    MatrixWorkspace_sptr sortedws;
    TS_ASSERT_THROWS_NOTHING(
        sortedws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "sortedws"));
    TS_ASSERT(sortedws);

    TS_ASSERT_EQUALS(sortedws->x(0), sortedXData);
    TS_ASSERT_EQUALS(sortedws->y(0), reverseYData);
    TS_ASSERT_EQUALS(sortedws->e(0), reverseEData);

    TS_ASSERT_THROWS_NOTHING(
        AnalysisDataService::Instance().remove("unsortedws"));
    TS_ASSERT_THROWS_NOTHING(
        AnalysisDataService::Instance().remove("sortedws"));
  }

  void testDxMultipleSpectrum() {
    std::vector<double> xData = {3, 2, 1};
    std::vector<double> yData = {1, 2, 3};
    std::vector<double> dxData = {1, 2, 3};
    std::vector<double> reverseDxData = {3, 2, 1};

    MatrixWorkspace_sptr unsortedws =
        createWorkspaceDx(xData, yData, dxData, 2);

    SortXAxis alg;
    TS_ASSERT_THROWS_NOTHING(alg.initialize())
    TS_ASSERT(alg.isInitialized())
    TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", unsortedws));
    TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "sortedws"));
    TS_ASSERT_THROWS_NOTHING(alg.execute());

    MatrixWorkspace_sptr sortedws;
    TS_ASSERT_THROWS_NOTHING(
        sortedws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "sortedws"));
    TS_ASSERT(sortedws);

    TS_ASSERT_EQUALS(sortedws->dx(0), reverseDxData);
    TS_ASSERT_EQUALS(sortedws->dx(1), reverseDxData);

    TS_ASSERT_THROWS_NOTHING(
        AnalysisDataService::Instance().remove("unsortedws"));
    TS_ASSERT_THROWS_NOTHING(
        AnalysisDataService::Instance().remove("sortedws"));
  }

  void testDxHistogramAscending() {
    std::vector<double> xData = {1, 2, 3, 4};
    std::vector<double> yData = {1, 2, 3};
    std::vector<double> dxData = {1, 2, 3};

    MatrixWorkspace_sptr unsortedws =
        createHistoWorkspaceDx(xData, yData, dxData, 2);

    SortXAxis alg;
    TS_ASSERT_THROWS_NOTHING(alg.initialize())
    TS_ASSERT(alg.isInitialized())
    TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", unsortedws));
    TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "sortedws"));
    TS_ASSERT_THROWS_NOTHING(alg.execute());

    MatrixWorkspace_sptr sortedws;
    TS_ASSERT_THROWS_NOTHING(
        sortedws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "sortedws"));
    TS_ASSERT(sortedws);

    TS_ASSERT_EQUALS(sortedws->dx(0), dxData);

    TS_ASSERT_THROWS_NOTHING(
        AnalysisDataService::Instance().remove("unsortedws"));
    TS_ASSERT_THROWS_NOTHING(
        AnalysisDataService::Instance().remove("sortedws"));
  }

  void testSortDescending() {
    std::vector<double> xData = {1, 2, 3, 4};
    std::vector<double> reverseXData = {4, 3, 2, 1};
    std::vector<double> yData = {1, 2, 3};
    std::vector<double> reverseYData = {3, 2, 1};

    MatrixWorkspace_sptr unsortedws = createHistoWorkspace(xData, yData, 2);

    SortXAxis alg;
    TS_ASSERT_THROWS_NOTHING(alg.initialize())
    TS_ASSERT(alg.isInitialized())
    TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", unsortedws));
    TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "sortedws"));
    TS_ASSERT_THROWS_NOTHING(alg.setProperty("Ordering", "Descending"));
    TS_ASSERT_THROWS_NOTHING(alg.execute());

    MatrixWorkspace_sptr sortedws;
    TS_ASSERT_THROWS_NOTHING(
        sortedws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            "sortedws"));
    TS_ASSERT(sortedws);

    TS_ASSERT_EQUALS(sortedws->x(0), reverseXData);
    TS_ASSERT_EQUALS(sortedws->y(0), reverseYData);

    TS_ASSERT_THROWS_NOTHING(
        AnalysisDataService::Instance().remove("unsortedws"));
    TS_ASSERT_THROWS_NOTHING(
        AnalysisDataService::Instance().remove("sortedws"));
  }
};
#endif /*MANTID_ALGORITHMS_SORTXAXISTEST_H_*/