1 | #class Fitting |
---|
2 | from sans.guitools.plottables import Data1D |
---|
3 | from Loader import Load |
---|
4 | from scipy import optimize |
---|
5 | from ScipyFitting import ScipyFit |
---|
6 | from ParkFitting import ParkFit |
---|
7 | class Fit: |
---|
8 | """ |
---|
9 | Wrap class that allows to select the fitting type |
---|
10 | """ |
---|
11 | def __init__(self): |
---|
12 | |
---|
13 | # To initialize a type of Fit |
---|
14 | self._engine=None |
---|
15 | |
---|
16 | def fit_engine(self,word): |
---|
17 | """ |
---|
18 | Select the type of Fit |
---|
19 | @param word: the keyword to select the fit type |
---|
20 | """ |
---|
21 | if word=="scipy": |
---|
22 | self._engine=ScipyFit() |
---|
23 | elif word=="park": |
---|
24 | self._engine=ParkFit() |
---|
25 | else: |
---|
26 | raise ValueError, "enter the keyword scipy or park" |
---|
27 | def returnEngine(self): |
---|
28 | return self._engine |
---|
29 | |
---|
30 | def fit(self,pars, qmin=None, qmax=None): |
---|
31 | """ Do the fit """ |
---|
32 | |
---|
33 | def set_model(self,model,Uid): |
---|
34 | """ Set model """ |
---|
35 | |
---|
36 | def set_data(self,data,Uid): |
---|
37 | """ Receive plottable and create a list of data to fit""" |
---|
38 | |
---|
39 | def get_model(self,Uid): |
---|
40 | """ return list of data""" |
---|
41 | |
---|
42 | def set_param(self,model, pars): |
---|
43 | """ Recieve a dictionary of parameter and save it """ |
---|
44 | |
---|
45 | def add_constraint(self, constraint): |
---|
46 | """ User specify contraint to fit """ |
---|
47 | |
---|
48 | def get_constraint(self): |
---|
49 | """ return the contraint value """ |
---|
50 | |
---|
51 | def set_constraint(self,constraint): |
---|
52 | """ |
---|
53 | receive a string as a constraint |
---|
54 | @param constraint: a string used to constraint some parameters to get a |
---|
55 | specific value |
---|
56 | """ |
---|
57 | if __name__ == "__main__": |
---|
58 | load= Load() |
---|
59 | # test scipy |
---|
60 | """test fit one data set one model with scipy """ |
---|
61 | #load data |
---|
62 | load.set_filename("testdata_line.txt") |
---|
63 | load.set_values() |
---|
64 | data1 = Data1D(x=[], y=[], dx=None,dy=None) |
---|
65 | data1.name = "data1" |
---|
66 | load.load_data(data1) |
---|
67 | #choose a model |
---|
68 | from sans.guitools.LineModel import LineModel |
---|
69 | model = LineModel() |
---|
70 | #Create a Fit engine |
---|
71 | fitter =Fit() |
---|
72 | fitter.fit_engine('scipy') |
---|
73 | engine = fitter.returnEngine() |
---|
74 | |
---|
75 | #set the model |
---|
76 | engine.set_model(model,1) |
---|
77 | engine.set_data(data1,1) |
---|
78 | |
---|
79 | print"fit only one data SCIPY:",engine.fit({'A':2,'B':1},None,None) |
---|
80 | |
---|
81 | |
---|
82 | """ test fit one data set one model with park """ |
---|
83 | fitter.fit_engine('scipy') |
---|
84 | engine = fitter.returnEngine() |
---|
85 | #set the model |
---|
86 | engine.set_model(model,1) |
---|
87 | engine.set_data(data1,1) |
---|
88 | |
---|
89 | print"fit only one data PARK:",engine.fit({'A':2,'B':1},None,None) |
---|
90 | |
---|
91 | |
---|
92 | """test fit with 2 data and one model SCIPY:""" |
---|
93 | # reinitialize the fitter |
---|
94 | fitter =Fit() |
---|
95 | #create an engine |
---|
96 | fitter.fit_engine("scipy") |
---|
97 | engine=fitter.returnEngine() |
---|
98 | #set the model for fit |
---|
99 | engine.set_model(model,2 ) |
---|
100 | #load 1 st data |
---|
101 | load.set_filename("testdata1.txt") |
---|
102 | load.set_values() |
---|
103 | data2 = Data1D(x=[], y=[], dx=None,dy=None) |
---|
104 | data2.name = "data2" |
---|
105 | load.load_data(data2) |
---|
106 | #load 2nd data |
---|
107 | load.set_filename("testdata2.txt") |
---|
108 | load.set_values() |
---|
109 | data3 = Data1D(x=[], y=[], dx=None,dy=None) |
---|
110 | data3.name = "data2" |
---|
111 | load.load_data(data3) |
---|
112 | |
---|
113 | #set data in the engine |
---|
114 | engine.set_data(data2,2) |
---|
115 | engine.set_data(data3,2) |
---|
116 | print"fit two data SCIPY:",engine.fit({'A':2,'B':1},None,None) |
---|
117 | |
---|
118 | """ test fit with 2 data and one model PARK:""" |
---|
119 | fitter.fit_engine("park") |
---|
120 | engine=fitter.returnEngine() |
---|
121 | #set the model for fit |
---|
122 | engine.set_model(model,2 ) |
---|
123 | #load 1 st data |
---|
124 | load.set_filename("testdata1.txt") |
---|
125 | load.set_values() |
---|
126 | data2 = Data1D(x=[], y=[], dx=None,dy=None) |
---|
127 | data2.name = "data2" |
---|
128 | load.load_data(data2) |
---|
129 | #load 2nd data |
---|
130 | load.set_filename("testdata2.txt") |
---|
131 | load.set_values() |
---|
132 | data3 = Data1D(x=[], y=[], dx=None,dy=None) |
---|
133 | data3.name = "data2" |
---|
134 | load.load_data(data3) |
---|
135 | |
---|
136 | #set data in the engine |
---|
137 | engine.set_data(data2,2) |
---|
138 | engine.set_data(data3,2) |
---|
139 | print"fit two data PARK:",engine.fit({'A':2,'B':1},None,None) |
---|
140 | |
---|
141 | |
---|