[792db7d5] | 1 | """ |
---|
[aa36f96] | 2 | Class Fit contains ScipyFit and ParkFit methods declaration |
---|
| 3 | allows to create instance of type ScipyFit or ParkFit to perform either |
---|
| 4 | a park fit or a scipy fit. |
---|
[792db7d5] | 5 | """ |
---|
[d8a2e31] | 6 | |
---|
[89f3b66] | 7 | #from scipy import optimize |
---|
[c4d6900] | 8 | from sans.fit.ScipyFitting import ScipyFit |
---|
| 9 | from sans.fit.ParkFitting import ParkFit |
---|
[7705306] | 10 | |
---|
| 11 | |
---|
[06e7c26] | 12 | class Fit(object): |
---|
[7705306] | 13 | """ |
---|
[aa36f96] | 14 | Wrap class that allows to select the fitting type.this class |
---|
| 15 | can be used as follow : :: |
---|
| 16 | |
---|
[792db7d5] | 17 | from sans.fit.Fitting import Fit |
---|
| 18 | fitter= Fit() |
---|
| 19 | fitter.fit_engine('scipy') or fitter.fit_engine('park') |
---|
| 20 | engine = fitter.returnEngine() |
---|
[c4d6900] | 21 | engine.set_data(data,id) |
---|
[792db7d5] | 22 | engine.set_param( model,model.name, pars) |
---|
[c4d6900] | 23 | engine.set_model(model,id) |
---|
[393f0f3] | 24 | |
---|
[792db7d5] | 25 | chisqr1, out1, cov1=engine.fit(pars,qmin,qmax) |
---|
[aa36f96] | 26 | |
---|
[7705306] | 27 | """ |
---|
[93f0a586] | 28 | def __init__(self, engine='scipy'): |
---|
[792db7d5] | 29 | """ |
---|
| 30 | """ |
---|
[aa36f96] | 31 | #self._engine will contain an instance of ScipyFit or ParkFit |
---|
[20d30e9] | 32 | self._engine = None |
---|
[06e7c26] | 33 | self.fitter_id = None |
---|
[93f0a586] | 34 | self.set_engine(engine) |
---|
[7705306] | 35 | |
---|
[06e7c26] | 36 | def __setattr__(self, name, value): |
---|
| 37 | """ |
---|
| 38 | set fitter_id and its engine at the same time |
---|
| 39 | """ |
---|
| 40 | if name == "fitter_id": |
---|
| 41 | self.__dict__[name] = value |
---|
| 42 | if hasattr(self, "_engine") and self._engine is not None: |
---|
| 43 | self._engine.fitter_id = value |
---|
| 44 | elif name == "_engine": |
---|
| 45 | self.__dict__[name] = value |
---|
| 46 | if hasattr(self, "fitter_id") and self.fitter_id is not None: |
---|
| 47 | self._engine.fitter_id = self.fitter_id |
---|
| 48 | else: |
---|
| 49 | self.__dict__[name] = value |
---|
| 50 | |
---|
[89f3b66] | 51 | def set_engine(self, word): |
---|
[7705306] | 52 | """ |
---|
[aa36f96] | 53 | Select the type of Fit |
---|
| 54 | |
---|
| 55 | :param word: the keyword to select the fit type |
---|
| 56 | |
---|
| 57 | :raise: if the user does not enter 'scipy' or 'park', |
---|
| 58 | a valueError is raised |
---|
| 59 | |
---|
[7705306] | 60 | """ |
---|
[89f3b66] | 61 | if word == "scipy": |
---|
| 62 | self._engine = ScipyFit() |
---|
| 63 | elif word == "park": |
---|
[c4d6900] | 64 | self._engine = ParkFit() |
---|
[7705306] | 65 | else: |
---|
| 66 | raise ValueError, "enter the keyword scipy or park" |
---|
[aa36f96] | 67 | |
---|
[ba7dceb] | 68 | def fit(self, msg_q=None, q=None, handler=None, |
---|
[7db52f1] | 69 | curr_thread=None, |
---|
| 70 | ftol=1.49012e-8, |
---|
| 71 | reset_flag=False): |
---|
[792db7d5] | 72 | """Perform the fit """ |
---|
[ba7dceb] | 73 | return self._engine.fit(msg_q=msg_q, |
---|
| 74 | q=q, handler=handler, curr_thread=curr_thread, |
---|
[7db52f1] | 75 | ftol=ftol, reset_flag=reset_flag) |
---|
[511c6810] | 76 | |
---|
[634ca14] | 77 | def set_model(self, model, id, pars=[], constraints=[], data=None): |
---|
[20d30e9] | 78 | """ |
---|
[c4d6900] | 79 | store a model model to fit at the position id of the fit engine |
---|
[20d30e9] | 80 | """ |
---|
[634ca14] | 81 | self._engine.set_model(model, id, pars, constraints, data=data) |
---|
[20d30e9] | 82 | |
---|
[c4d6900] | 83 | def set_data(self, data, id, smearer=None, qmin=None, qmax=None): |
---|
[20d30e9] | 84 | """ |
---|
[c4d6900] | 85 | Store data to fit at the psotion id of the fit engine |
---|
[aa36f96] | 86 | |
---|
| 87 | :param data: data to fit |
---|
| 88 | :param smearer: smearerobject to smear data |
---|
| 89 | :param qmin: the minimum q range to fit |
---|
| 90 | :param qmax: the minimum q range to fit |
---|
| 91 | |
---|
[20d30e9] | 92 | """ |
---|
[c4d6900] | 93 | self._engine.set_data(data, id, smearer, qmin, qmax) |
---|
[ca6d914] | 94 | |
---|
[c4d6900] | 95 | def get_model(self, id): |
---|
[7705306] | 96 | """ return list of data""" |
---|
[c4d6900] | 97 | self._engine.get_model(id) |
---|
[9855699] | 98 | |
---|
[20d30e9] | 99 | |
---|
[c4d6900] | 100 | def remove_fit_problem(self, id): |
---|
| 101 | """remove fitarrange in id""" |
---|
| 102 | self._engine.remove_fit_problem(id) |
---|
[ca6d914] | 103 | |
---|
[c4d6900] | 104 | def select_problem_for_fit(self, id, value): |
---|
[a9e04aa] | 105 | """ |
---|
[c4d6900] | 106 | select a couple of model and data at the id position in dictionary |
---|
[aa36f96] | 107 | and set in self.selected value to value |
---|
| 108 | |
---|
| 109 | :param value: the value to allow fitting. |
---|
| 110 | can only have the value one or zero |
---|
[a9e04aa] | 111 | """ |
---|
[c4d6900] | 112 | self._engine.select_problem_for_fit(id, value) |
---|
[20d30e9] | 113 | |
---|
[c4d6900] | 114 | def get_problem_to_fit(self, id): |
---|
[a9e04aa] | 115 | """ |
---|
[c4d6900] | 116 | return the self.selected value of the fit problem of id |
---|
[aa36f96] | 117 | |
---|
[c4d6900] | 118 | :param id: the id of the problem |
---|
[aa36f96] | 119 | |
---|
[a9e04aa] | 120 | """ |
---|
[c4d6900] | 121 | return self._engine.get_problem_to_fit(id) |
---|