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