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