Esempio n. 1
0
static int
PyLibPlot_init(struct PyLibPlot* self, PyObject *args, PyObject *kwds)
{
    int status=0;
	PyObject *params_dict=NULL;
	char *type=NULL;
    char *filename=NULL;
	plPlotterParams *params;

    self->fptr=NULL;

	if ( !PyArg_ParseTuple( args, "sOs", &type, &params_dict, &filename) ) {
		return -1;
    }

    // for the repr
    snprintf(self->type, sizeof(self->type), "%s", type);

	params = pl_newplparams();

    if (!extract_params(params_dict, params)) {
        // exception set inside
        goto bail;
    }

    if (0 != strcmp(filename, "")) {
        // a filename was passed
        self->fptr = fopen(filename,"w");
        if (self->fptr==NULL) {
            fprintf(stderr,"error opening %s\n", filename);
            PyErr_Format( PyExc_TypeError, "could not open file: %s", filename);
            goto bail;
        }
    }

	self->pl = pl_newpl_r( type, NULL, self->fptr, NULL, params );
	if (!self->pl) {
		PyErr_SetString(PyExc_RuntimeError, "could not create plotter");
        goto bail;
	}

    status=1;

bail:

	pl_deleteplparams( params );

    if (!status) {
        return -1;
    } else {
        return 0;
    }

}
Esempio n. 2
0
Hypermet::Hypermet(const std::vector<double> &x, const std::vector<double> &y,
                   Gaussian gauss,
                    FitSettings settings):
  Hypermet(gauss, settings) {
  std::vector<double> sigma;
    for (auto &q : y) {
      sigma.push_back(sqrt(q));
    }
//  sigma.resize(x.size(), 1);

  bool success = true;

  if ((x.size() < 1) || (x.size() != y.size()))
    return;

  fityk::Fityk *f = new fityk::Fityk;
  f->redir_messages(NULL);
  f->load_data(0, x, y, sigma);

  try {
//        f->execute("set fitting_method = mpfit");
//    f->execute("set fitting_method = nlopt_nm");
//    f->execute("set fitting_method = nlopt_bobyqa");
        f->execute("set fitting_method = nlopt_lbfgs");
  } catch ( ... ) {
    success = false;
    PL_DBG << "Hypermet failed to set fitter";
  }

  try {
    f->execute(fityk_definition());
  } catch ( ... ) {
    success = false;
    PL_DBG << "Hypermet failed to define";
  }

  double lateral_slack = (x[x.size() -1]  - x[0]) / 5.0;

  center_.lbound = center_.val - lateral_slack;
  center_.ubound = center_.val + lateral_slack;

  height_.lbound = height_.val * 0.003;
  height_.ubound = height_.val * 3000;

  width_.lbound = width_.val * 0.7;
  width_.ubound = width_.val * 1.3;

  std::string initial_c = "$c = " + center_.def_bounds();
  std::string initial_h = "$h = " + height_.def_bounds();
  std::string initial_w = "$w = " + width_.def_bounds();

  std::string initial_lsh = "$lsh = " + Lskew_amplitude.def_bounds();
  std::string initial_lss = "$lss = " + Lskew_slope.def_bounds();

  std::string initial_rsh = "$rsh = " + Rskew_amplitude.def_bounds();
  std::string initial_rss = "$rss = " + Rskew_slope.def_bounds();

//  std::string initial_llh = FitykUtil::var_def("llh", 0.0000, 0, 0.015);
//  std::string initial_lls = FitykUtil::var_def("lls", 2.5, 2.5, 50);

  std::string initial_step = "$step = " + step_amplitude.def_bounds();

  std::string initial = "F += Hypermet($c,$h,$w,$lsh,$lss,$rsh,$rss,$step)";

  //PL_DBG << "Hypermet initial c=" << center_ << " h=" << height_ << " w=" << width_;

  try {
    f->execute(initial_h);
    f->execute(initial_c);
    f->execute(initial_w);
    f->execute(initial_lsh);
    f->execute(initial_lss);
    f->execute(initial_rsh);
    f->execute(initial_rss);
//    f->execute(initial_llh);
//    f->execute(initial_lls);
    f->execute(initial_step);
    f->execute(initial);

  } catch ( ... ) {
    success = false;
    PL_DBG << "Hypermet: failed to set up initial";
  }

  try {
    f->execute("fit");
  }
  catch ( ... ) {
    PL_DBG << "Hypermet could not fit";
    success = false;
  }

  if (success) {
    f->execute("set fitting_method = levenberg_marquardt");
    std::vector<fityk::Func*> fns = f->all_functions();
    if (fns.size()) {
      fityk::Func* func = fns.back();
      extract_params(f, func); //discard return value
    }
    rsq_ = f->get_rsquared(0);
  }


  delete f;
}