1 | |
---|
2 | import sys |
---|
3 | from data_util.calcthread import CalcThread |
---|
4 | |
---|
5 | def map_getattr(classInstance, classFunc, *args): |
---|
6 | """ |
---|
7 | Take an instance of a class and a function name as a string. |
---|
8 | Execute class.function and return result |
---|
9 | """ |
---|
10 | return getattr(classInstance,classFunc)(*args) |
---|
11 | |
---|
12 | def map_apply(arguments): |
---|
13 | return apply(arguments[0], arguments[1:]) |
---|
14 | |
---|
15 | class FitThread(CalcThread): |
---|
16 | """Thread performing the fit """ |
---|
17 | |
---|
18 | def __init__(self, |
---|
19 | fn, |
---|
20 | page_id, |
---|
21 | handler, |
---|
22 | pars=None, |
---|
23 | completefn = None, |
---|
24 | updatefn = None, |
---|
25 | yieldtime = 0.01, |
---|
26 | worktime = 0.01, |
---|
27 | ftol = None): |
---|
28 | CalcThread.__init__(self,completefn, |
---|
29 | updatefn, |
---|
30 | yieldtime, |
---|
31 | worktime) |
---|
32 | self.handler = handler |
---|
33 | self.fitter = fn |
---|
34 | self.pars = pars |
---|
35 | self.page_id = page_id |
---|
36 | self.starttime = 0 |
---|
37 | self.updatefn = updatefn |
---|
38 | #Relative error desired in the sum of squares. |
---|
39 | self.ftol = ftol |
---|
40 | |
---|
41 | def isquit(self): |
---|
42 | """ |
---|
43 | :raise KeyboardInterrupt: when the thread is interrupted |
---|
44 | |
---|
45 | """ |
---|
46 | try: |
---|
47 | CalcThread.isquit(self) |
---|
48 | except KeyboardInterrupt: |
---|
49 | raise KeyboardInterrupt |
---|
50 | |
---|
51 | def compute(self): |
---|
52 | """ |
---|
53 | Perform a fit |
---|
54 | """ |
---|
55 | msg = "" |
---|
56 | try: |
---|
57 | list_handler = [] |
---|
58 | list_curr_thread = [] |
---|
59 | list_ftol = [] |
---|
60 | list_map_get_attr = [] |
---|
61 | list_fit_function = [] |
---|
62 | list_q = [] |
---|
63 | for i in range(len(self.fitter)): |
---|
64 | list_handler.append(None) |
---|
65 | list_q.append(None) |
---|
66 | list_curr_thread.append(None) |
---|
67 | list_ftol.append(self.ftol) |
---|
68 | list_fit_function.append('fit') |
---|
69 | list_map_get_attr.append(map_getattr) |
---|
70 | from multiprocessing import Pool |
---|
71 | inputs = zip(list_map_get_attr,self.fitter, list_fit_function, |
---|
72 | list_handler, list_q, list_curr_thread,list_ftol) |
---|
73 | result = Pool(1).map(func=map_apply, |
---|
74 | iterable=inputs) |
---|
75 | #self.handler.starting_fit() |
---|
76 | self.complete(result= result, |
---|
77 | page_id=self.page_id, |
---|
78 | pars = self.pars) |
---|
79 | |
---|
80 | except KeyboardInterrupt, msg: |
---|
81 | # Thread was interrupted, just proceed and re-raise. |
---|
82 | # Real code should not print, but this is an example... |
---|
83 | #print "keyboard exception" |
---|
84 | #Stop on exception during fitting. Todo: need to put |
---|
85 | #some mssg and reset progress bar. |
---|
86 | raise |
---|
87 | #if self.handler is not None: |
---|
88 | # self.handler.error(msg=msg) |
---|
89 | except: |
---|
90 | raise |
---|
91 | #if self.handler is not None: |
---|
92 | # self.handler.error(msg=str(sys.exc_value)) |
---|
93 | |
---|
94 | |
---|
95 | |
---|