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