1 | import sys |
---|
2 | import time |
---|
3 | from sas.sascalc.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 | batch_outputs, |
---|
23 | batch_inputs=None, |
---|
24 | pars=None, |
---|
25 | completefn=None, |
---|
26 | updatefn=None, |
---|
27 | yieldtime=0.03, |
---|
28 | worktime=0.03, |
---|
29 | reset_flag=False): |
---|
30 | CalcThread.__init__(self, |
---|
31 | completefn, |
---|
32 | updatefn, |
---|
33 | yieldtime, |
---|
34 | worktime) |
---|
35 | self.handler = handler |
---|
36 | self.fitter = fn |
---|
37 | self.pars = pars |
---|
38 | self.batch_inputs = batch_inputs |
---|
39 | self.batch_outputs = batch_outputs |
---|
40 | self.page_id = page_id |
---|
41 | self.starttime = time.time() |
---|
42 | self.updatefn = updatefn |
---|
43 | #Relative error desired in the sum of squares. |
---|
44 | self.reset_flag = reset_flag |
---|
45 | |
---|
46 | def isquit(self): |
---|
47 | """ |
---|
48 | :raise KeyboardInterrupt: when the thread is interrupted |
---|
49 | |
---|
50 | """ |
---|
51 | try: |
---|
52 | CalcThread.isquit(self) |
---|
53 | except KeyboardInterrupt: |
---|
54 | msg = "Fitting: terminated by the user." |
---|
55 | raise KeyboardInterrupt, msg |
---|
56 | |
---|
57 | def compute(self): |
---|
58 | """ |
---|
59 | Perform a fit |
---|
60 | """ |
---|
61 | msg = "" |
---|
62 | try: |
---|
63 | import copy |
---|
64 | list_handler = [] |
---|
65 | list_curr_thread = [] |
---|
66 | list_reset_flag = [] |
---|
67 | list_map_get_attr = [] |
---|
68 | list_fit_function = [] |
---|
69 | list_q = [] |
---|
70 | for i in range(len(self.fitter)): |
---|
71 | list_handler.append(self.handler) |
---|
72 | list_q.append(None) |
---|
73 | list_curr_thread.append(self) |
---|
74 | list_reset_flag.append(self.reset_flag) |
---|
75 | list_fit_function.append('fit') |
---|
76 | list_map_get_attr.append(map_getattr) |
---|
77 | #from multiprocessing import Pool |
---|
78 | inputs = zip(list_map_get_attr, self.fitter, list_fit_function, |
---|
79 | list_q, list_q, list_handler, list_curr_thread, |
---|
80 | list_reset_flag) |
---|
81 | result = map(map_apply, inputs) |
---|
82 | |
---|
83 | self.complete(result=result, |
---|
84 | batch_inputs=self.batch_inputs, |
---|
85 | batch_outputs=self.batch_outputs, |
---|
86 | page_id=self.page_id, |
---|
87 | pars=self.pars, |
---|
88 | elapsed=time.time() - self.starttime) |
---|
89 | |
---|
90 | except KeyboardInterrupt, msg: |
---|
91 | # Thread was interrupted, just proceed and re-raise. |
---|
92 | # Real code should not print, but this is an example... |
---|
93 | #print "keyboard exception" |
---|
94 | #Stop on exception during fitting. Todo: need to put |
---|
95 | #some mssg and reset progress bar. |
---|
96 | |
---|
97 | # Shouldn't this be re-raising? ConsoleUpdate doesn't act on it. |
---|
98 | # raise KeyboardInterrupt |
---|
99 | if self.handler is not None: |
---|
100 | self.handler.stop(msg=msg) |
---|
101 | except: |
---|
102 | import traceback |
---|
103 | if self.handler is not None: |
---|
104 | self.handler.error(msg=traceback.format_exc()) |
---|