void ALCPeakFittingView::setFunction(const IFunction_const_sptr &newFunction) {
  if (newFunction) {
    size_t nParams = newFunction->nParams();
    for (size_t i = 0; i < nParams; i++) {

      QString name = QString::fromStdString(newFunction->parameterName(i));
      double value = newFunction->getParameter(i);
      double error = newFunction->getError(i);

      m_ui.peaks->setParameter(name, value);
      m_ui.peaks->setParamError(name, error);
    }
  } else {
    m_ui.peaks->clear();
  }
}
void ALCBaselineModellingView::setFunction(IFunction_const_sptr func) {
  if (!func) {
    m_ui.function->clear();
  } else {
    size_t nParams = func->nParams();
    for (size_t i = 0; i < nParams; i++) {

      QString name = QString::fromStdString(func->parameterName(i));
      double value = func->getParameter(i);
      double error = func->getError(i);

      m_ui.function->setParameter(name, value);
      m_ui.function->setParamError(name, error);
    }
  }
}
Example #3
0
  /**
   * Creates a single-spectrum workspace filled with function values for given X values
   * @param func :: Function to calculate values
   * @param xValues :: X values to use
   * @return Single-spectrum workspace with calculated function values
   */
  MatrixWorkspace_sptr createWsFromFunction(IFunction_const_sptr func,
                                            const std::vector<double>& xValues)
  {
    auto inputWs = boost::dynamic_pointer_cast<MatrixWorkspace>(
          WorkspaceFactory::Instance().create("Workspace2D", 1, xValues.size(), xValues.size()));
    inputWs->dataX(0) = xValues;

    IAlgorithm_sptr fit = AlgorithmManager::Instance().create("Fit");
    fit->setChild(true); // Don't want workspace in the ADS
    fit->setProperty("Function", func->asString());
    fit->setProperty("InputWorkspace", inputWs);
    fit->setProperty("MaxIterations", 0); // Don't want to fit, just calculate output workspace
    fit->setProperty("CreateOutput", true);
    fit->execute();

    MatrixWorkspace_sptr fitOutput = fit->getProperty("OutputWorkspace");

    IAlgorithm_sptr extract = AlgorithmManager::Instance().create("ExtractSingleSpectrum");
    extract->setChild(true); // Don't want workspace in the ADS
    extract->setProperty("InputWorkspace", fitOutput);
    extract->setProperty("WorkspaceIndex", 1); // "Calc"
    extract->setPropertyValue("OutputWorkspace", "__NotUsed");
    extract->execute();

    return extract->getProperty("OutputWorkspace");
  }
Example #4
0
/// Set the weight function
void CustomPolynomial::setWeightFunction(IFunction_const_sptr wgtFun, const double& tol)
{
  m_weightFunction = wgtFun;
  auto cheb = dynamic_cast<const ChebFunction*>( wgtFun.get() );
  if ( cheb && cheb->nfuns() == 1 && !cheb->cfun(0).hasScaling() )
  {
      m_fun = cheb->cfun(0).getUnscaled();
  }
  else
  {
    m_fun.bestFit( *wgtFun, tol );
  }
}
void ALCBaselineModellingModel::fit(IFunction_const_sptr function,
                                    const std::vector<Section> &sections) {
  // Create a copy of the data
  IAlgorithm_sptr clone = AlgorithmManager::Instance().create("CloneWorkspace");
  clone->setChild(true);
  clone->setProperty("InputWorkspace",
                     boost::const_pointer_cast<MatrixWorkspace>(m_data));
  clone->setProperty("OutputWorkspace", "__NotUsed__");
  clone->execute();

  Workspace_sptr cloned = clone->getProperty("OutputWorkspace");
  MatrixWorkspace_sptr dataToFit =
      boost::dynamic_pointer_cast<MatrixWorkspace>(cloned);
  assert(dataToFit); // CloneWorkspace should take care of that

  disableUnwantedPoints(dataToFit, sections);

  IFunction_sptr funcToFit =
      FunctionFactory::Instance().createInitialized(function->asString());

  IAlgorithm_sptr fit = AlgorithmManager::Instance().create("Fit");
  fit->setChild(true);
  fit->setProperty("Function", funcToFit);
  fit->setProperty("InputWorkspace", dataToFit);
  fit->setProperty("CreateOutput", true);

  // Run async so that progress can be shown
  Poco::ActiveResult<bool> result(fit->executeAsync());
  while (!result.available()) {
    QCoreApplication::processEvents();
  }
  if (!result.error().empty()) {
    throw std::runtime_error(result.error());
  }

  MatrixWorkspace_sptr fitOutput = fit->getProperty("OutputWorkspace");
  m_parameterTable = fit->getProperty("OutputParameters");

  enableDisabledPoints(fitOutput, m_data);
  setErrorsAfterFit(fitOutput);

  setCorrectedData(fitOutput);
  setFittedFunction(funcToFit);
  m_sections = sections;
}
  void ALCPeakFittingModel::fitPeaks(IFunction_const_sptr peaks)
  {
    IAlgorithm_sptr fit = AlgorithmManager::Instance().create("Fit");
    fit->setChild(true);
    fit->setProperty("Function", peaks->asString());
    fit->setProperty("InputWorkspace", boost::const_pointer_cast<MatrixWorkspace>(m_data));
    fit->setProperty("CreateOutput", true);
    fit->setProperty("OutputCompositeMembers", true);

    // Execute async so we can show progress bar
    Poco::ActiveResult<bool> result(fit->executeAsync());
    while (!result.available()) {
      QCoreApplication::processEvents();
    }
    if (!result.error().empty()) {
      throw std::runtime_error(result.error());
    }

    m_data = fit->getProperty("OutputWorkspace");
    m_parameterTable = fit->getProperty("OutputParameters");
    setFittedPeaks(static_cast<IFunction_sptr>(fit->getProperty("Function")));
  }
  void ALCBaselineModellingModel::fit(IFunction_const_sptr function, const std::vector<Section>& sections)
  {
    // Create a copy of the data
    IAlgorithm_sptr clone = AlgorithmManager::Instance().create("CloneWorkspace");
    clone->setChild(true);
    clone->setProperty("InputWorkspace", boost::const_pointer_cast<MatrixWorkspace>(m_data));
    clone->setProperty("OutputWorkspace", "__NotUsed__");
    clone->execute();

    Workspace_sptr cloned = clone->getProperty("OutputWorkspace");
    MatrixWorkspace_sptr dataToFit = boost::dynamic_pointer_cast<MatrixWorkspace>(cloned);
    assert(dataToFit); // CloneWorkspace should take care of that

    disableUnwantedPoints(dataToFit, sections);

    IFunction_sptr funcToFit =
        FunctionFactory::Instance().createInitialized(function->asString());

    IAlgorithm_sptr fit = AlgorithmManager::Instance().create("Fit");
    fit->setChild(true);
    fit->setProperty("Function", funcToFit);
    fit->setProperty("InputWorkspace", dataToFit);
    fit->setProperty("CreateOutput", true);
    fit->execute();

    MatrixWorkspace_sptr fitOutput = fit->getProperty("OutputWorkspace");
    m_parameterTable = fit->getProperty("OutputParameters");

    enableDisabledPoints(fitOutput,m_data);
    setErrorsAfterFit(fitOutput);

    setCorrectedData(fitOutput);
    setFittedFunction(funcToFit);
    m_sections = sections;

  }