Esempio n. 1
0
/// Fit the data simultaneously.
void MultiDatasetFit::fitSimultaneous() {
  try {

    m_uiForm.btnFit->setEnabled(false);
    auto fun = createFunction();
    auto fit = Mantid::API::AlgorithmManager::Instance().create("Fit");
    fit->initialize();
    fit->setProperty("Function", fun);
    fit->setPropertyValue("InputWorkspace", getWorkspaceName(0).toStdString());
    fit->setProperty("WorkspaceIndex", getWorkspaceIndex(0));
    auto range = getFittingRange(0);
    fit->setProperty("StartX", range.first);
    fit->setProperty("EndX", range.second);

    int n = getNumberOfSpectra();
    for (int ispec = 1; ispec < n; ++ispec) {
      std::string suffix = boost::lexical_cast<std::string>(ispec);
      fit->setPropertyValue("InputWorkspace_" + suffix,
                            getWorkspaceName(ispec).toStdString());
      fit->setProperty("WorkspaceIndex_" + suffix, getWorkspaceIndex(ispec));
      auto range = getFittingRange(ispec);
      fit->setProperty("StartX_" + suffix, range.first);
      fit->setProperty("EndX_" + suffix, range.second);
    }

    m_fitOptionsBrowser->copyPropertiesToAlgorithm(*fit);

    m_outputWorkspaceName = m_fitOptionsBrowser->getProperty("Output");
    if (m_outputWorkspaceName.isEmpty()) {
      m_outputWorkspaceName = "out";
      fit->setPropertyValue("Output", m_outputWorkspaceName.toStdString());
      m_fitOptionsBrowser->setProperty("Output", "out");
    }
    if (n == 1) {
      m_outputWorkspaceName += "_Workspace";
    } else {
      m_outputWorkspaceName += "_Workspaces";
    }

    removeOldOutput();

    m_fitRunner.reset(new API::AlgorithmRunner());
    connect(m_fitRunner.get(), SIGNAL(algorithmComplete(bool)), this,
            SLOT(finishFit(bool)), Qt::QueuedConnection);

    m_fitRunner->startAlgorithm(fit);

  } catch (std::exception &e) {
    QString mess(e.what());
    const int maxSize = 500;
    if (mess.size() > maxSize) {
      mess = mess.mid(0, maxSize);
      mess += "...";
    }
    QMessageBox::critical(this, "MantidPlot - Error",
                          QString("Fit failed:\n\n  %1").arg(mess));
    m_uiForm.btnFit->setEnabled(true);
  }
}
Esempio n. 2
0
/**
 * Get value of named log from workspace i
 * (Must be able to cast to double)
 * @param logName :: [input] Name of log
 * @param function :: [input] Function to apply to log e.g. min, max, mean...
 * @param i :: [input] index of dataset
 * @returns :: value of log cast to double
 */
double DataController::getLogValue(const QString &logName,
                                   const StatisticType &function, int i) const {
  auto &ads = Mantid::API::AnalysisDataService::Instance();
  const auto &wsName = getWorkspaceName(i).toStdString();
  if (ads.doesExist(wsName)) {
    const auto &workspace =
        ads.retrieveWS<Mantid::API::MatrixWorkspace>(wsName);
    return workspace->run().getLogAsSingleValue(logName.toStdString(),
                                                function);
  } else {
    throw std::runtime_error("Workspace not found: " + wsName);
  }
}
Esempio n. 3
0
/// Fit the data sets sequentially if there are no global parameters.
void MultiDatasetFit::fitSequential() {
  try {

    /// disable button to avoid multiple fit click
    m_uiForm.btnFit->setEnabled(false);

    std::ostringstream input;

    int n = getNumberOfSpectra();
    for (int ispec = 0; ispec < n; ++ispec) {
      input << getWorkspaceName(ispec).toStdString() << ",i"
            << getWorkspaceIndex(ispec) << ";";
    }

    auto fun = m_functionBrowser->getFunction();
    auto fit =
        Mantid::API::AlgorithmManager::Instance().create("PlotPeakByLogValue");
    fit->initialize();
    fit->setPropertyValue("Function", fun->asString());
    fit->setPropertyValue("Input", input.str());
    auto range = getFittingRange(0);
    fit->setProperty("StartX", range.first);
    fit->setProperty("EndX", range.second);

    m_fitOptionsBrowser->copyPropertiesToAlgorithm(*fit);

    m_outputWorkspaceName =
        m_fitOptionsBrowser->getProperty("OutputWorkspace") + "_Workspaces";

    removeOldOutput();

    m_fitRunner.reset(new API::AlgorithmRunner());
    connect(m_fitRunner.get(), SIGNAL(algorithmComplete(bool)), this,
            SLOT(finishFit(bool)), Qt::QueuedConnection);

    m_fitRunner->startAlgorithm(fit);

  } catch (std::exception &e) {
    QString mess(e.what());
    const int maxSize = 500;
    if (mess.size() > maxSize) {
      mess = mess.mid(0, maxSize);
      mess += "...";
    }
    QMessageBox::critical(
        this, "MantidPlot - Error",
        QString("PlotPeakByLogValue failed:\n\n  %1").arg(mess));
    m_uiForm.btnFit->setEnabled(true);
  }
}
Esempio n. 4
0
/// Check that the data sets in the table are valid and remove invalid ones.
void DataController::checkSpectra() {
  QList<int> rows;
  int nrows = getNumberOfSpectra();
  auto &ADS = Mantid::API::AnalysisDataService::Instance();
  for (int row = 0; row < nrows; ++row) {
    auto wsName = getWorkspaceName(row).toStdString();
    auto i = getWorkspaceIndex(row);
    if (!ADS.doesExist(wsName)) {
      rows.push_back(row);
      continue;
    }
    auto ws = ADS.retrieveWS<Mantid::API::MatrixWorkspace>(wsName);
    if (!ws || i >= static_cast<int>(ws->getNumberHistograms())) {
      rows.push_back(row);
      continue;
    }
  }

  removeSpectra(rows);
}
Esempio n. 5
0
/**
 * Collect names of the logs in the data workspaces and pass them on to
 * m_fitOptionsBrowser.
 */
void MultiDatasetFit::setLogNames() {
  if (getNumberOfSpectra() > 0) {
    try {
      auto ws = Mantid::API::AnalysisDataService::Instance()
                    .retrieveWS<Mantid::API::MatrixWorkspace>(
                        getWorkspaceName(0).toStdString());
      const std::vector<Mantid::Kernel::Property *> logs =
          ws->run().getLogData();
      QStringList logNames;
      for (int i = 0; i < static_cast<int>(logs.size()); ++i) {
        if (dynamic_cast<Mantid::Kernel::TimeSeriesProperty<double> *>(
                logs[i])) {
          logNames << QString::fromStdString(logs[i]->name());
        }
      }
      if (!logNames.isEmpty()) {
        m_fitOptionsBrowser->setLogNames(logNames);
      }
    } catch (...) { /*Maybe the data table hasn't updated yet*/
    }
  }
}
Esempio n. 6
0
/**
 * Get list of log names from workspace i
 * @param i :: [input] index of dataset
 * @returns :: list of log names
 */
std::vector<std::string> DataController::getWorkspaceLogNames(int i) const {
  std::vector<std::string> logNames;

  // validate input
  if (i > getNumberOfSpectra()) {
    std::ostringstream err;
    err << "Index " << i << "greater than number of spectra ("
        << getNumberOfSpectra() << ")";
    throw std::invalid_argument(err.str());
  }

  // populate vector of names
  auto &ads = Mantid::API::AnalysisDataService::Instance();
  const auto &wsName = getWorkspaceName(i).toStdString();
  if (ads.doesExist(wsName)) {
    const auto &workspace =
        ads.retrieveWS<Mantid::API::MatrixWorkspace>(wsName);
    const auto &logs = workspace->run().getLogData();
    for (const auto &log : logs) {
      logNames.push_back(log->name());
    }
  }
  return logNames;
}