Merged Rolf Schmidt's illysam branch
[hooke.git] / hooke / ui / gui / results.py
index 77b9ca7ea69367cd26ba769c478c676472792d85..ca458bd474c03747df079e11b1ad6791b3286d69 100644 (file)
-# Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
-#
-# This file is part of Hooke.
-#
-# Hooke is free software: you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation, either
-# version 3 of the License, or (at your option) any later version.
-#
-# Hooke is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public
-# License along with Hooke.  If not, see
-# <http://www.gnu.org/licenses/>.
-
-import prettyformat
-
-DEFAULT_COLOR = 'orange'
-DEFAULT_DECIMAL = 2
-DEFAULT_STYLE = 'plot'
-
-class Result:
-    def __init__(self):
-        self.color = DEFAULT_COLOR
-        self.result = {}
-        self.style = DEFAULT_STYLE
-        self.visible = True
-        self.x = []
-        self.y = []
-
-class Results:
-    def __init__(self):
-        self.columns = []
-        self.decimals = {}
-        self.has_multipliers = False
-        self.multipliers = {}
-        self.results = []
-        self.separator='\t'
-        self.units = {}
-
-    def get_pretty_value(self, column, value):
-        if self.has_multipliers and self.has_results():
-            multiplier = self.multipliers[column]
-            decimals = self.decimals[column]
-            return prettyformat.pretty_format(value, '', decimals, multiplier, True)
-        return str(value)
-
-    #def get_fit_result(self):
-        #if not(self.has_multipliers):
-            #self.set_multipliers()
-
-        #sResult = 'Contour length ['+prettyformat.get_prefix(self.multiplierContourLength) + 'm]' + self.separator
-        #sResult += prettyformat.pretty_format(self.contourLength[0], '', self.decimals, self.multiplierContourLength, True) + '\n'
-        #sResult += 'Persistence length ['+prettyformat.get_prefix(self.multiplierPersistenceLength) + 'm]' + self.separator
-        #sResult += prettyformat.pretty_format(self.persistenceLength[0], '', self.decimals, self.multiplierPersistenceLength, True) + '\n'
-        #sResult += 'Rupture force ['+prettyformat.get_prefix(self.multiplierRuptureForce) + 'N]' + self.separator
-        #sResult += prettyformat.pretty_format(self.ruptureForces[0], '', self.decimals, self.multiplierRuptureForce, True) + '\n'
-        #sResult += 'Loading rate ['+prettyformat.get_prefix(self.multiplierSlope) + 'N/m]' + self.separator
-        #sResult += prettyformat.pretty_format(self.slopes[0], '', self.decimals, self.multiplierSlope, True)+'\n'
-        #sResult += 'Sigma contour ['+prettyformat.get_prefix(self.multiplierContourLength) + 'm]' + self.separator
-        #sResult += prettyformat.pretty_format(self.contourLengthSigma[0], '', self.decimals, self.multiplierContourLength, True) + '\n'
-        #sResult += 'Sigma persistence ['+prettyformat.get_prefix(self.multiplierPersistenceLength) + 'm]' + self.separator
-        #sResult += prettyformat.pretty_format(self.persistenceLengthSigma[0], '', self.decimals, self.multiplierPersistenceLength, True)
-
-        #return sResult
-
-    #def get_fit_results(self, index):
-        #if index >= 0 and index < len(self.contourLength):
-            #if not(self.has_multipliers):
-                #self.set_multipliers()
-            #sLine = prettyformat.pretty_format(self.contourLength[index], '', self.decimals, self.multiplierContourLength, True) + self.separator
-            #sLine += prettyformat.pretty_format(self.persistenceLength[index], '', self.decimals, self.multiplierPersistenceLength, True) + self.separator
-            #sLine += prettyformat.pretty_format(self.ruptureForces[index], '', self.decimals, self.multiplierRuptureForce, True) + self.separator
-            #sLine += prettyformat.pretty_format(self.slopes[index], '', self.decimals, self.multiplierSlope, True) + self.separator
-            #sLine += prettyformat.pretty_format(self.contourLengthSigma[index], '', self.decimals, self.multiplierContourLength, True) + self.separator
-            #sLine += prettyformat.pretty_format(self.persistenceLengthSigma[index], '', self.decimals, self.multiplierPersistenceLength, True)
-
-            #return sLine
-        #else:
-            #return ''
-
-    def has_results(self):
-        return self.results
-
-    def header_as_list(self):
-        header = []
-        if self.has_results():
-            if not self.has_multipliers:
-                self.set_multipliers()
-            for column in self.columns:
-                #result will contain the results dictionary for 'column'
-                #result = self.results[0][0][column]
-                #result[1] contains the unit
-                unit_str = ''.join([prettyformat.get_prefix(self.multipliers[column]), self.units[column]])
-                header_str = ''.join([column, ' [', unit_str, ']'])
-                header.append(header_str)
-        return header
-
-    #def header_as_str(self):
-        #if self.has_results():
-            #if not self.has_multipliers:
-                #self.set_multipliers()
-            #header_str = ''
-            #for column in self.columns:
-                ##result will contain the results dictionary for 'column'
-                #result = self.results[0][0][column]
-                ##result[1] contains the unit
-                #unit_str = ''.join([prettyformat.get_prefix(self.multipliers[column]), result[1]])
-                #header_str = ''.join([header_str, result_str, ' [', unit_str, ']', self.separator])
-            #return header_str
-        #else:
-            #return None
-
-    def set_decimal(self, column, decimal=DEFAULT_DECIMAL):
-        if self.decimals.has_key(name):
-            self.decimals[name] = decimal
-
-    def set_decimals(self, decimals=DEFAULT_DECIMAL):
-        if decimals < 0:
-            #set default value if necessary
-            decimals = DEFAULT_DECIMAL
-        for column in self.columns:
-            self.decimals[column] = decimals
-
-    def set_multipliers(self, index=0):
-        if self.has_results():
-            if index >= 0 and index < len(self.results):
-                for column in self.columns:
-                    #result will contain the results dictionary at 'index'
-                    result = self.results[index][0]
-                    #in position 0 of the result we find the value
-                    self.multipliers[column] = prettyformat.get_multiplier(result[column][0])
-                self.has_multipliers = True
-        else:
-            self.has_multipliers = False
-
-
-class ResultsWLC(Results):
-    def __init__(self):
-        Results.__init__(self)
-        self.columns = ['Contour length', 'sigma contour length', 'Persistence length', 'sigma persistence length', 'Rupture force', 'Loading rate']
-        self.units['Contour length'] = 'm'
-        self.units['sigma contour length'] = 'm'
-        self.units['Persistence length'] = 'm'
-        self.units['sigma persistence length'] = 'm'
-        self.units['Rupture force'] = 'N'
-        self.units['Loading rate'] = 'N/m'
-        self.set_decimals(2)
-
-    def set_multipliers(self, index=0):
-        if self.has_results():
-            if index >= 0 and index < len(self.results):
-                for column in self.columns:
-                    #result will contain the results dictionary at 'index'
-                    result = self.results[index].result
-                    #in position 0 of the result we find the value
-                    if column == 'sigma contour length':
-                        self.multipliers[column] = self.multipliers['Contour length']
-                    elif column == 'sigma persistence length':
-                        self.multipliers[column] = self.multipliers['Persistence length']
-                    else:
-                        self.multipliers[column] = prettyformat.get_multiplier(result[column])
-                self.has_multipliers = True
-        else:
-            self.has_multipliers = False
+#!/usr/bin/env python\r
+\r
+'''\r
+results.py\r
+\r
+Result and Results classes for Hooke.\r
+\r
+Copyright 2009 by Dr. Rolf Schmidt (Concordia University, Canada)\r
+\r
+This program is released under the GNU General Public License version 2.\r
+'''\r
+\r
+from numpy import nan\r
+\r
+import prettyformat\r
+import lib.curve\r
+\r
+DEFAULT_COLOR = 'green'\r
+DEFAULT_DECIMAL = 2\r
+DEFAULT_STYLE = 'scatter'\r
+\r
+class Result(lib.curve.Curve):\r
+    def __init__(self):\r
+        lib.curve.Curve.__init__(self)\r
+        self.color = DEFAULT_COLOR\r
+        self.result = {}\r
+        self.style = DEFAULT_STYLE\r
+\r
+class Results(object):\r
+    def __init__(self):\r
+        self.columns = []\r
+        self.decimals = {}\r
+        self.has_multipliers = False\r
+        self.multipliers = {}\r
+        self.results = []\r
+        self.separator='\t'\r
+        self.units = {}\r
+\r
+    def get_pretty_value(self, column, value):\r
+        if self.has_multipliers and self.has_results():\r
+            multiplier = self.multipliers[column]\r
+            decimals = self.decimals[column]\r
+            return prettyformat.pretty_format(value, '', decimals, multiplier, True)\r
+        return str(value)\r
+\r
+    def has_results(self):\r
+        return len(self.results) > 0\r
+\r
+    def get_header_as_list(self):\r
+        header_list = []\r
+        if self.has_results():\r
+            if not self.has_multipliers:\r
+                self.set_multipliers()\r
+            for column in self.columns:\r
+                unit_str = ''.join([prettyformat.get_prefix(self.multipliers[column]), self.units[column]])\r
+                header_str = ''.join([column, ' [', unit_str, ']'])\r
+                header_list.append(header_str)\r
+        return header_list\r
+\r
+    def get_header_as_str(self, separator=None):\r
+        if separator is None:\r
+            separator = self.separator\r
+        return separator.join(map(str, self.get_header_as_list()))\r
+\r
+    def get_result_as_list(self, index=0):\r
+        if index >= 0 and index < len(self.results):\r
+            result_list = []\r
+            if self.has_results():\r
+                if not self.has_multipliers:\r
+                    self.set_multipliers()\r
+                for column in self.columns:\r
+                    result_str = prettyformat.pretty_format(self.results[index].result[column], '', self.decimals[column], self.multipliers[column], True)\r
+                    result_list.append(result_str)\r
+            return result_list\r
+        else:\r
+            return None\r
+\r
+    def get_result_as_string(self, index=0):\r
+        results_list = self.get_result_as_list(index)\r
+        if results_list is not None:\r
+            return self.separator.join(map(str, results_list))\r
+        else:\r
+            return ''\r
+\r
+    def set_decimal(self, column, decimal=DEFAULT_DECIMAL):\r
+        if self.decimals.has_key(column):\r
+            self.decimals[column] = decimal\r
+\r
+    def set_decimals(self, decimals=DEFAULT_DECIMAL):\r
+        if decimals < 0:\r
+            #set default value if necessary\r
+            decimals = DEFAULT_DECIMAL\r
+        for column in self.columns:\r
+            self.decimals[column] = decimals\r
+\r
+    def set_multipliers(self, index=0):\r
+        if self.has_results():\r
+            for column in self.columns:\r
+                #result will contain the results dictionary at 'index'\r
+                result = self.results[index].result\r
+                #in position 0 of the result we find the value\r
+                self.multipliers[column] = prettyformat.get_multiplier(result[column])\r
+            self.has_multipliers = True\r
+        else:\r
+            self.has_multipliers = False\r
+\r
+    def update(self):\r
+        pass\r
+\r
+\r
+class ResultsFJC(Results):\r
+    def __init__(self):\r
+        Results.__init__(self)\r
+        self.columns = ['Contour length', 'sigma contour length', 'Kuhn length', 'sigma Kuhn length', 'Rupture force', 'Slope', 'Loading rate']\r
+        self.units['Contour length'] = 'm'\r
+        self.units['sigma contour length'] = 'm'\r
+        self.units['Kuhn length'] = 'm'\r
+        self.units['sigma Kuhn length'] = 'm'\r
+        self.units['Rupture force'] = 'N'\r
+        self.units['Slope'] = 'N/m'\r
+        self.units['Loading rate'] = 'N/s'\r
+        self.set_decimals(2)\r
+\r
+    def set_multipliers(self, index=0):\r
+        if self.has_results():\r
+            for column in self.columns:\r
+                #result will contain the results dictionary at 'index'\r
+                result = self.results[index].result\r
+                #in position 0 of the result we find the value\r
+                if column == 'sigma contour length':\r
+                    self.multipliers[column] = self.multipliers['Contour length']\r
+                elif column == 'sigma Kuhn length':\r
+                    self.multipliers[column] = self.multipliers['Kuhn length']\r
+                else:\r
+                    self.multipliers[column] = prettyformat.get_multiplier(result[column])\r
+            self.has_multipliers = True\r
+        else:\r
+            self.has_multipliers = False\r
+\r
+class ResultsMultiDistance(Results):\r
+    def __init__(self):\r
+        Results.__init__(self)\r
+        self.columns = ['Distance']\r
+        self.units['Distance'] = 'm'\r
+        self.set_decimals(2)\r
+\r
+    def update(self):\r
+        if (self.results) > 0:\r
+            for result in self.results:\r
+                if result.visible:\r
+                    reference_peak = result.x\r
+                    break\r
+\r
+            for result in self.results:\r
+                if result.visible:\r
+                    result.result['Distance'] = reference_peak - result.x\r
+                    reference_peak = result.x\r
+                else:\r
+                    result.result['Distance'] = nan\r
+\r
+\r
+class ResultsWLC(Results):\r
+    def __init__(self):\r
+        Results.__init__(self)\r
+        self.columns = ['Contour length', 'sigma contour length', 'Persistence length', 'sigma persistence length', 'Rupture force', 'Slope', 'Loading rate']\r
+        self.units['Contour length'] = 'm'\r
+        self.units['sigma contour length'] = 'm'\r
+        self.units['Persistence length'] = 'm'\r
+        self.units['sigma persistence length'] = 'm'\r
+        self.units['Rupture force'] = 'N'\r
+        self.units['Slope'] = 'N/m'\r
+        self.units['Loading rate'] = 'N/s'\r
+        self.set_decimals(2)\r
+\r
+    def set_multipliers(self, index=0):\r
+        if self.has_results():\r
+            for column in self.columns:\r
+                #result will contain the results dictionary at 'index'\r
+                result = self.results[index].result\r
+                #in position 0 of the result we find the value\r
+                if column == 'sigma contour length':\r
+                    self.multipliers[column] = self.multipliers['Contour length']\r
+                elif column == 'sigma persistence length':\r
+                    self.multipliers[column] = self.multipliers['Persistence length']\r
+                else:\r
+                    self.multipliers[column] = prettyformat.get_multiplier(result[column])\r
+            self.has_multipliers = True\r
+        else:\r
+            self.has_multipliers = False\r