Ran update_copyright.py, updating all the copyright blurbs and adding AUTHORS.
[hooke.git] / hooke / ui / gui / results.py
1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Hooke.
4 #
5 # Hooke is free software: you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation, either
8 # version 3 of the License, or (at your option) any later version.
9 #
10 # Hooke is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke.  If not, see
17 # <http://www.gnu.org/licenses/>.
18
19 import prettyformat
20
21 DEFAULT_COLOR = 'orange'
22 DEFAULT_DECIMAL = 2
23 DEFAULT_STYLE = 'plot'
24
25 class Result:
26     def __init__(self):
27         self.color = DEFAULT_COLOR
28         self.result = {}
29         self.style = DEFAULT_STYLE
30         self.visible = True
31         self.x = []
32         self.y = []
33
34 class Results:
35     def __init__(self):
36         self.columns = []
37         self.decimals = {}
38         self.has_multipliers = False
39         self.multipliers = {}
40         self.results = []
41         self.separator='\t'
42         self.units = {}
43
44     def get_pretty_value(self, column, value):
45         if self.has_multipliers and self.has_results():
46             multiplier = self.multipliers[column]
47             decimals = self.decimals[column]
48             return prettyformat.pretty_format(value, '', decimals, multiplier, True)
49         return str(value)
50
51     #def get_fit_result(self):
52         #if not(self.has_multipliers):
53             #self.set_multipliers()
54
55         #sResult = 'Contour length ['+prettyformat.get_prefix(self.multiplierContourLength) + 'm]' + self.separator
56         #sResult += prettyformat.pretty_format(self.contourLength[0], '', self.decimals, self.multiplierContourLength, True) + '\n'
57         #sResult += 'Persistence length ['+prettyformat.get_prefix(self.multiplierPersistenceLength) + 'm]' + self.separator
58         #sResult += prettyformat.pretty_format(self.persistenceLength[0], '', self.decimals, self.multiplierPersistenceLength, True) + '\n'
59         #sResult += 'Rupture force ['+prettyformat.get_prefix(self.multiplierRuptureForce) + 'N]' + self.separator
60         #sResult += prettyformat.pretty_format(self.ruptureForces[0], '', self.decimals, self.multiplierRuptureForce, True) + '\n'
61         #sResult += 'Loading rate ['+prettyformat.get_prefix(self.multiplierSlope) + 'N/m]' + self.separator
62         #sResult += prettyformat.pretty_format(self.slopes[0], '', self.decimals, self.multiplierSlope, True)+'\n'
63         #sResult += 'Sigma contour ['+prettyformat.get_prefix(self.multiplierContourLength) + 'm]' + self.separator
64         #sResult += prettyformat.pretty_format(self.contourLengthSigma[0], '', self.decimals, self.multiplierContourLength, True) + '\n'
65         #sResult += 'Sigma persistence ['+prettyformat.get_prefix(self.multiplierPersistenceLength) + 'm]' + self.separator
66         #sResult += prettyformat.pretty_format(self.persistenceLengthSigma[0], '', self.decimals, self.multiplierPersistenceLength, True)
67
68         #return sResult
69
70     #def get_fit_results(self, index):
71         #if index >= 0 and index < len(self.contourLength):
72             #if not(self.has_multipliers):
73                 #self.set_multipliers()
74             #sLine = prettyformat.pretty_format(self.contourLength[index], '', self.decimals, self.multiplierContourLength, True) + self.separator
75             #sLine += prettyformat.pretty_format(self.persistenceLength[index], '', self.decimals, self.multiplierPersistenceLength, True) + self.separator
76             #sLine += prettyformat.pretty_format(self.ruptureForces[index], '', self.decimals, self.multiplierRuptureForce, True) + self.separator
77             #sLine += prettyformat.pretty_format(self.slopes[index], '', self.decimals, self.multiplierSlope, True) + self.separator
78             #sLine += prettyformat.pretty_format(self.contourLengthSigma[index], '', self.decimals, self.multiplierContourLength, True) + self.separator
79             #sLine += prettyformat.pretty_format(self.persistenceLengthSigma[index], '', self.decimals, self.multiplierPersistenceLength, True)
80
81             #return sLine
82         #else:
83             #return ''
84
85     def has_results(self):
86         return self.results
87
88     def header_as_list(self):
89         header = []
90         if self.has_results():
91             if not self.has_multipliers:
92                 self.set_multipliers()
93             for column in self.columns:
94                 #result will contain the results dictionary for 'column'
95                 #result = self.results[0][0][column]
96                 #result[1] contains the unit
97                 unit_str = ''.join([prettyformat.get_prefix(self.multipliers[column]), self.units[column]])
98                 header_str = ''.join([column, ' [', unit_str, ']'])
99                 header.append(header_str)
100         return header
101
102     #def header_as_str(self):
103         #if self.has_results():
104             #if not self.has_multipliers:
105                 #self.set_multipliers()
106             #header_str = ''
107             #for column in self.columns:
108                 ##result will contain the results dictionary for 'column'
109                 #result = self.results[0][0][column]
110                 ##result[1] contains the unit
111                 #unit_str = ''.join([prettyformat.get_prefix(self.multipliers[column]), result[1]])
112                 #header_str = ''.join([header_str, result_str, ' [', unit_str, ']', self.separator])
113             #return header_str
114         #else:
115             #return None
116
117     def set_decimal(self, column, decimal=DEFAULT_DECIMAL):
118         if self.decimals.has_key(name):
119             self.decimals[name] = decimal
120
121     def set_decimals(self, decimals=DEFAULT_DECIMAL):
122         if decimals < 0:
123             #set default value if necessary
124             decimals = DEFAULT_DECIMAL
125         for column in self.columns:
126             self.decimals[column] = decimals
127
128     def set_multipliers(self, index=0):
129         if self.has_results():
130             if index >= 0 and index < len(self.results):
131                 for column in self.columns:
132                     #result will contain the results dictionary at 'index'
133                     result = self.results[index][0]
134                     #in position 0 of the result we find the value
135                     self.multipliers[column] = prettyformat.get_multiplier(result[column][0])
136                 self.has_multipliers = True
137         else:
138             self.has_multipliers = False
139
140
141 class ResultsWLC(Results):
142     def __init__(self):
143         Results.__init__(self)
144         self.columns = ['Contour length', 'sigma contour length', 'Persistence length', 'sigma persistence length', 'Rupture force', 'Loading rate']
145         self.units['Contour length'] = 'm'
146         self.units['sigma contour length'] = 'm'
147         self.units['Persistence length'] = 'm'
148         self.units['sigma persistence length'] = 'm'
149         self.units['Rupture force'] = 'N'
150         self.units['Loading rate'] = 'N/m'
151         self.set_decimals(2)
152
153     def set_multipliers(self, index=0):
154         if self.has_results():
155             if index >= 0 and index < len(self.results):
156                 for column in self.columns:
157                     #result will contain the results dictionary at 'index'
158                     result = self.results[index].result
159                     #in position 0 of the result we find the value
160                     if column == 'sigma contour length':
161                         self.multipliers[column] = self.multipliers['Contour length']
162                     elif column == 'sigma persistence length':
163                         self.multipliers[column] = self.multipliers['Persistence length']
164                     else:
165                         self.multipliers[column] = prettyformat.get_multiplier(result[column])
166                 self.has_multipliers = True
167         else:
168             self.has_multipliers = False