/**
   * Add a new entry to the diagnosis table.
   * @param runTitle       :: Title of the run fitted
   * @param fitQuality     :: Number representing a goodness of the fit
   * @param fittedFunction :: Function containing fitted parameters 
   */
  void MuonSequentialFitDialog::addDiagnosisEntry(const std::string& runTitle, double fitQuality,
      IFunction_sptr fittedFunction)
  {
    int newRow = m_ui.diagnosisTable->rowCount();

    m_ui.diagnosisTable->insertRow(newRow);

    QString runTitleDisplay = QString::fromStdString(runTitle);
    m_ui.diagnosisTable->setItem( newRow, 0, createTableWidgetItem(runTitleDisplay) );

    QString fitQualityDisplay = QString::number(fitQuality);
    m_ui.diagnosisTable->setItem( newRow, 1, createTableWidgetItem(fitQualityDisplay) );

    for(int i = 2; i < m_ui.diagnosisTable->columnCount(); i += 2)
    {
      std::string paramName = m_ui.diagnosisTable->horizontalHeaderItem(i)->text().toStdString();
      size_t paramIndex = fittedFunction->parameterIndex(paramName);

      QString value = QString::number( fittedFunction->getParameter(paramIndex) );
      QString error = QString::number( fittedFunction->getError(paramIndex) );

      m_ui.diagnosisTable->setItem(newRow, i, createTableWidgetItem(value) );
      m_ui.diagnosisTable->setItem(newRow, i + 1, createTableWidgetItem(error) );
    }

    m_ui.diagnosisTable->scrollToBottom();
  }
  /** Fit function
    * Minimizer: "Levenberg-MarquardtMD"/"Simplex"
   */
  bool RefinePowderInstrumentParameters2::doFitFunction(IFunction_sptr function, Workspace2D_sptr dataws, int wsindex,
                                                        string minimizer, int numiters, double& chi2, string& fitstatus)
  {
    // 0. Debug output
    stringstream outss;
    outss << "Fit function: " << m_positionFunc->asString() << endl << "Data To Fit: \n";
    for (size_t i = 0; i < dataws->readX(0).size(); ++i)
      outss << dataws->readX(wsindex)[i] << "\t\t" << dataws->readY(wsindex)[i] << "\t\t"
            << dataws->readE(wsindex)[i] << "\n";
    g_log.information() << outss.str();

    // 1. Create and setup fit algorithm
    API::IAlgorithm_sptr fitalg = createChildAlgorithm("Fit", 0.0, 0.2, true);
    fitalg->initialize();

    fitalg->setProperty("Function", function);
    fitalg->setProperty("InputWorkspace", dataws);
    fitalg->setProperty("WorkspaceIndex", wsindex);
    fitalg->setProperty("Minimizer", minimizer);
    fitalg->setProperty("CostFunction", "Least squares");
    fitalg->setProperty("MaxIterations", numiters);
    fitalg->setProperty("CalcErrors", true);

    // 2. Fit
    bool successfulfit = fitalg->execute();
    if (!fitalg->isExecuted() || ! successfulfit)
    {
      // Early return due to bad fit
      g_log.warning("Fitting to instrument geometry function failed. ");
      chi2 = DBL_MAX;
      fitstatus = "Minimizer throws exception.";
      return false;
    }

    // 3. Understand solution
    chi2 = fitalg->getProperty("OutputChi2overDoF");
    string tempfitstatus = fitalg->getProperty("OutputStatus");
    fitstatus = tempfitstatus;

    bool goodfit = fitstatus.compare("success") == 0;

    stringstream dbss;
    dbss << "Fit Result (GSL):  Chi^2 = " << chi2
         << "; Fit Status = " << fitstatus << ", Return Bool = " << goodfit << std::endl;
    vector<string> funcparnames = function->getParameterNames();
    for (size_t i = 0; i < funcparnames.size(); ++i)
      dbss << funcparnames[i] << " = " << setw(20) << function->getParameter(funcparnames[i])
           << " +/- " << function->getError(i) << "\n";
    g_log.debug() << dbss.str();

    return goodfit;
  }
Example #3
0
/// Returns a TableWorkspace with refined cell parameters and error.
ITableWorkspace_sptr PoldiFitPeaks2D::getRefinedCellParameters(
    const IFunction_sptr &fitFunction) const {
  Poldi2DFunction_sptr poldi2DFunction =
      boost::dynamic_pointer_cast<Poldi2DFunction>(fitFunction);

  if (!poldi2DFunction || poldi2DFunction->nFunctions() < 1) {
    throw std::invalid_argument(
        "Cannot process function that is not a Poldi2DFunction.");
  }

  // Create a new table for lattice parameters
  ITableWorkspace_sptr latticeParameterTable =
      WorkspaceFactory::Instance().createTable();

  latticeParameterTable->addColumn("str", "Parameter");
  latticeParameterTable->addColumn("double", "Value");
  latticeParameterTable->addColumn("double", "Error");

  // The first function should be PoldiSpectrumPawleyFunction
  boost::shared_ptr<PoldiSpectrumPawleyFunction> poldiPawleyFunction =
      boost::dynamic_pointer_cast<PoldiSpectrumPawleyFunction>(
          poldi2DFunction->getFunction(0));

  if (!poldiPawleyFunction) {
    throw std::invalid_argument("First function in Poldi2DFunction is not "
                                "PoldiSpectrumPawleyFunction.");
  }

  // Get the actual PawleyFunction to extract parameters.
  IPawleyFunction_sptr pawleyFunction =
      boost::dynamic_pointer_cast<IPawleyFunction>(
          poldiPawleyFunction->getDecoratedFunction());

  if (pawleyFunction) {
    CompositeFunction_sptr pawleyParts =
        boost::dynamic_pointer_cast<CompositeFunction>(
            pawleyFunction->getDecoratedFunction());

    // The first function in PawleyFunction contains the parameters
    IFunction_sptr pawleyParameters = pawleyParts->getFunction(0);

    for (size_t i = 0; i < pawleyParameters->nParams(); ++i) {
      TableRow newRow = latticeParameterTable->appendRow();
      newRow << pawleyParameters->parameterName(i)
             << pawleyParameters->getParameter(i)
             << pawleyParameters->getError(i);
    }
  }

  return latticeParameterTable;
}
  /** Store function parameter values to a map
    */
  void storeFunctionParameterValue(IFunction_sptr function, map<string, pair<double, double> >& parvaluemap)
  {
    parvaluemap.clear();

    vector<string> parnames = function->getParameterNames();
    for (size_t i = 0; i < parnames.size(); ++i)
    {
      string& parname = parnames[i];
      double parvalue = function->getParameter(i);
      double parerror = function->getError(i);
      parvaluemap.insert(make_pair(parname, make_pair(parvalue, parerror)));
    }

    return;
  }