/** Restore function parameter values saved in a (string,double) map to a function object
    * and a (string, Parameter) map
    */
  void restoreFunctionParameterValue(map<string, pair<double, double> > parvaluemap, IFunction_sptr function,
                                     map<string, Parameter>& parammap)
  {
    vector<string> parnames = function->getParameterNames();

    for (size_t i = 0; i < parnames.size(); ++i)
    {
      string& parname = parnames[i];
      map<string, pair<double, double> >::iterator miter;
      miter = parvaluemap.find(parname);

      if (miter != parvaluemap.end())
      {
        double parvalue = miter->second.first;
        double parerror = miter->second.second;

        // 1. Function
        function->setParameter(parname, parvalue);

        // 2. Parameter map
        map<string, Parameter>::iterator pariter = parammap.find(parname);
        if (pariter != parammap.end())
        {
          // Find the entry
          pariter->second.value = parvalue;
          pariter->second.error = parerror;
        }
      }
    }

    return;
  }
Exemple #2
0
IFunction_sptr IqtFit::createUserFunction(const QString &name, bool tie) {
  IFunction_sptr result =
      FunctionFactory::Instance().createFunction("UserFunction");
  std::string formula;

  if (name.startsWith("Exp")) {
    formula = "Intensity*exp(-(x/Tau))";
  } else {
    formula = "Intensity*exp(-(x/Tau)^Beta)";
  }

  IFunction::Attribute att(formula);
  result->setAttribute("Formula", att);

  QList<QtProperty *> props = m_properties[name]->subProperties();
  for (int i = 0; i < props.size(); i++) {
    std::string name = props[i]->propertyName().toStdString();
    result->setParameter(name, m_dblManager->value(props[i]));

    // add tie if parameter is fixed
    if (tie || !props[i]->subProperties().isEmpty()) {
      std::string value = props[i]->valueText().toStdString();
      result->tie(name, value);
    }
  }

  result->applyTies();
  return result;
}
Exemple #3
0
    /** 
     * Create a function from an expression.
     * @param expr :: The input expression
     * @param parentAttributes :: An output map filled with the attribute name & values of the parent function
     * @return A pointer to the created function
     */
    IFunction_sptr FunctionFactoryImpl::createSimple(const Expression& expr, std::map<std::string,std::string>& parentAttributes)const
    {
      if (expr.name() == "=" && expr.size() > 1)
      {
        return createFunction(expr.terms()[1].name());
      }

      if (expr.name() != "," || expr.size() == 0)
      {
        inputError(expr.str());
      }

      const std::vector<Expression>& terms = expr.terms();
      std::vector<Expression>::const_iterator term = terms.begin();

      if (term->name() != "=") inputError(expr.str());
      if (term->terms()[0].name() != "name" && term->terms()[0].name() != "composite")
      {
        throw std::invalid_argument("Function name must be defined before its parameters");
      }
      std::string fnName = term->terms()[1].name();

      IFunction_sptr fun = createFunction(fnName);
      for(++term;term!=terms.end();++term)
      {// loop over function's parameters/attributes
        if (term->name() != "=") inputError(expr.str());
        std::string parName = term->terms()[0].name();
        std::string parValue = term->terms()[1].str();
        if (fun->hasAttribute(parName))
        {// set attribute
          if (parValue.size() > 1 && parValue[0] == '"')
          {// remove the double quotes
            parValue = parValue.substr(1,parValue.size()-2);
          }
          IFunction::Attribute att = fun->getAttribute(parName);
          att.fromString(parValue);
          fun->setAttribute(parName,att);
        }
        else if (parName.size() >= 10 && parName.substr(0,10) == "constraint")
        {// or it can be a list of constraints
          addConstraints(fun,(*term)[1]);
        }
        else if (parName == "ties")
        {
          addTies(fun,(*term)[1]);
        }
        else if (!parName.empty() && parName[0] == '$')
        {
          parName.erase(0,1);
          parentAttributes[parName] = parValue;
        }
        else
        {// set initial parameter value
          fun->setParameter(parName,atof(parValue.c_str()));
        }
      }// for term

      fun->applyTies();
      return fun;
    }
Exemple #4
0
/**
 * Adds background functions for the background if applicable
 *
 * If specified by the user via the corresponding algorithm parameters,
 * this function adds a constant and a linear background term to the
 * supplied Poldi2DFunction.
 *
 * @param poldi2DFunction :: Poldi2DFunction to which the background is added.
 */
void PoldiFitPeaks2D::addBackgroundTerms(
    Poldi2DFunction_sptr poldi2DFunction) const {
  bool addConstantBackground = getProperty("FitConstantBackground");
  if (addConstantBackground) {
    IFunction_sptr constantBackground =
        FunctionFactory::Instance().createFunction(
            "PoldiSpectrumConstantBackground");
    constantBackground->setParameter(
        0, getProperty("ConstantBackgroundParameter"));
    poldi2DFunction->addFunction(constantBackground);
  }

  bool addLinearBackground = getProperty("FitLinearBackground");
  if (addLinearBackground) {
    IFunction_sptr linearBackground =
        FunctionFactory::Instance().createFunction(
            "PoldiSpectrumLinearBackground");
    linearBackground->setParameter(0, getProperty("LinearBackgroundParameter"));
    poldi2DFunction->addFunction(linearBackground);
  }
}
  /** Set parameter values to function from Parameter map
   */
  void RefinePowderInstrumentParameters2::setFunctionParameterValues(IFunction_sptr function,
                                                                     map<string, Parameter> params)
  {
    // 1. Prepare
    vector<string> funparamnames = function->getParameterNames();

    // 2. Set up
    stringstream msgss;
    msgss << "Set Instrument Function Parameter : " << endl;

    std::map<std::string, Parameter>::iterator paramiter;
    for (size_t i = 0; i < funparamnames.size(); ++i)
    {
      string parname = funparamnames[i];
      paramiter = params.find(parname);

      if (paramiter != params.end())
      {
        // Found, set up the parameter
        Parameter& param = paramiter->second;
        function->setParameter(parname, param.value);

        msgss << setw(10) << parname << " = " << param.value << endl;
      }
      else
      {
        // Not found and thus quit
        stringstream errss;
        errss << "Peak profile parameter " << parname << " is not found in input parameters. ";
        g_log.error(errss.str());
        throw runtime_error(errss.str());
      }
    } // ENDFOR parameter name

    g_log.information(msgss.str());

    return;
  }
Exemple #6
0
CompositeFunction_sptr IqtFit::createFunction(bool tie) {
  CompositeFunction_sptr result(new CompositeFunction);
  QString fname;
  const int fitType = m_uiForm.cbFitType->currentIndex();

  IFunction_sptr func =
      FunctionFactory::Instance().createFunction("LinearBackground");
  func->setParameter("A0",
                     m_ffRangeManager->value(m_properties["BackgroundA0"]));
  result->addFunction(func);
  result->tie("f0.A1", "0");
  if (tie) {
    result->tie("f0.A0",
                m_properties["BackgroundA0"]->valueText().toStdString());
  }

  if (fitType == 2) {
    fname = "StretchedExp";
  } else {
    fname = "Exponential1";
  }

  result->addFunction(createUserFunction(fname, tie));

  if (fitType == 1 || fitType == 3) {
    if (fitType == 1) {
      fname = "Exponential2";
    } else {
      fname = "StretchedExp";
    }
    result->addFunction(createUserFunction(fname, tie));
  }

  // Return CompositeFunction object to caller.
  result->applyTies();
  return result;
}