Moved hooke.driver.hemingclamp to hemingway and updated to new Driver architecture
authorW. Trevor King <wking@drexel.edu>
Thu, 17 Jun 2010 23:40:22 +0000 (19:40 -0400)
committerW. Trevor King <wking@drexel.edu>
Thu, 17 Jun 2010 23:40:22 +0000 (19:40 -0400)
hooke/driver/__init__.py
hooke/driver/hemingclamp.py [deleted file]
hooke/driver/hemingway.py [new file with mode: 0644]
test/hemingway_driver.py [new file with mode: 0644]

index 824a5e28b6efaec1ccb36cb761a88ab7d7d0d514..cdfb4aecbecbbb7e8cd14215a532f56581cf3d10 100644 (file)
@@ -33,7 +33,7 @@ from ..util.pluggable import IsSubclass, construct_graph
 DRIVER_MODULES = [
 #    ('csvdriver', True),
 #    ('hdf5', True),
-#    ('hemingclamp', True),
+    ('hemingway', True),
     ('jpk', True),
 #    ('mcs', True),
 #    ('mfp1dexport', True),
diff --git a/hooke/driver/hemingclamp.py b/hooke/driver/hemingclamp.py
deleted file mode 100644 (file)
index dc9f3cf..0000000
+++ /dev/null
@@ -1,144 +0,0 @@
-# Copyright (C) 2008-2010 Massimo Sandal <devicerandom@gmail.com>
-#                         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/>.
-
-"""Library for interpreting Hemingway force spectroscopy files.
-"""
-__version__='2007_02_15_devel'
-
-__changelog__='''
-2007_02_15: fixed time counter with my counter
-2007_02_07: Initial implementation
-'''
-import string
-from .. import curve as lhc
-
-class DataChunk(list):
-    '''Dummy class to provide ext and ret methods to the data list.
-    In this case ext and self can be equal.
-    '''
-
-    def ext(self):
-        return self
-
-    def ret(self):
-        return self
-
-class hemingclampDriver(lhc.Driver):
-
-    def __init__(self, filename):
-
-        self.filedata = open(filename,'r')
-        self.data = self.filedata.readlines()[6:]
-        self.filedata.close()
-
-        self.filetype = 'hemingclamp'
-        self.experiment = 'clamp'
-
-        self.filename=filename
-
-    def __del__(self):
-        self.filedata.close()
-
-    def is_me(self):
-        '''
-        we define our magic heuristic for HemingClamp files
-        '''
-        myfile=file(self.filename)
-        headerlines=myfile.readlines()[0:3]
-        myfile.close()
-        if headerlines[0][0:10]=='#Hemingway' and headerlines[1][0:19]=='#Experiment: FClamp':
-            return True
-        else:
-            return False
-
-    def _getdata_all(self):
-        time = []
-        phase = []
-        zpiezo = []
-        defl = []
-        imposed = []
-        trim_indexes = []
-        trim_counter = 0.0
-
-        for i in self.data:
-            temp = string.split(i)
-            #time.append(float(temp[0])*(1.0e-3)) # This is managed differently now, since each data point = 1ms: see below
-            phase.append(float(temp[1])*(1.0e-7)) # The nonsensical (e-7) multiplier is just there to make phase data nicely plottable along other data
-            zpiezo.append(float(temp[2])*(1.0e-9))
-            defl.append(float(temp[3])*(1.0e-9))
-            imposed.append(float(temp[4])*(1.0e-9))
-
-        for x in range (0,len(phase)):
-            if phase[x] != trim_counter:
-                trim_indexes.append(x)
-                trim_counter = phase[x]
-
-        #we rebuild the time counter assuming 1 point = 1 millisecond
-        c=0.0
-        for z in zpiezo:
-            time.append(c)
-            c+=(1.0e-3)
-
-        return time,phase,zpiezo,defl,imposed,trim_indexes
-
-    def time(self):
-        return DataChunk(self._getdata_all()[0])
-
-    def phase(self):
-        return DataChunk(self._getdata_all()[1])
-
-    def zpiezo(self):
-        return DataChunk(self._getdata_all()[2])
-
-    def deflection(self):
-        return DataChunk(self._getdata_all()[3])
-
-    def imposed(self):
-        return DataChunk(self._getdata_all()[4])
-
-    def trimindexes(self):
-        return DataChunk(self._getdata_all()[5])
-
-    def close_all(self):
-        '''
-        Explicitly closes all files
-        '''
-        self.filedata.close()
-
-    def default_plots(self):
-        main_plot=lhc.PlotObject()
-        defl_plot=lhc.PlotObject()
-
-        time=self.time()
-        phase=self.phase()
-        zpiezo=self.zpiezo()
-        deflection=self.deflection()
-        imposed=self.imposed()
-
-        main_plot.vectors=[[time,zpiezo],[time,phase]]
-        main_plot.units=['seconds','meters']
-        main_plot.destination=0
-        main_plot.title=self.filename
-
-        defl_plot.vectors=[[time,deflection],[time,imposed]]
-        defl_plot.units=['seconds','Newtons']
-        defl_plot.destination=1
-
-        return [main_plot, defl_plot]
-
diff --git a/hooke/driver/hemingway.py b/hooke/driver/hemingway.py
new file mode 100644 (file)
index 0000000..7fecbb0
--- /dev/null
@@ -0,0 +1,75 @@
+# Copyright (C) 2008-2010 Massimo Sandal <devicerandom@gmail.com>
+#                         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/>.
+
+"""Library for interpreting Hemingway force spectroscopy files.
+"""
+
+import numpy
+
+from .. import curve as curve
+from .. import experiment as experiment
+from ..config import Setting
+from ..util.util import Closing as Closing
+from . import Driver as Driver
+
+
+class HemingwayDriver (Driver):
+    """Handle Hemingway force spectroscopy files.
+    """
+    def __init__(self):
+        super(HemingwayDriver, self).__init__(name='hemingway')
+
+    def is_me(self, path):
+        headlines = []
+        with Closing(file(path, 'r')) as f:
+            for i in range(2):
+                headlines.append(f.readline())
+        return (headlines[0].startswith('#Hemingway')
+                and headlines[1].startswith('#Experiment: FClamp'))
+
+    def read(self, path, info=None):
+        file_info = {}
+        with Closing(file(path, 'r')) as f:
+            while True:
+                line = f.readline().strip()
+                if line == '#END':
+                    break
+                fields = line.split(':', 1)
+                if len(fields) == 2:
+                    file_info[fields[0]] = fields[1]
+            data = numpy.genfromtxt(f, dtype=numpy.float)
+        ret = curve.Data(
+            shape=data.shape,
+            dtype=data.dtype,
+            buffer=data,
+            info=file_info,
+            )
+        ret.info['columns'] = [
+            'time (s)',  # first data column in file just increasing index
+            'phase (m?rad)',
+            'z piezo (m)',
+            'deflection (N)',
+            'imposed (N)',
+            ]
+        # assume 1 ms timestep
+        ret[:,0] = numpy.arange(0, 1e-3*data.shape[0], 1e-3, dtype=ret.dtype)
+        
+        file_info['filetype'] = self.name
+        file_info['experiment'] = experiment.ForceClamp
+        return ([ret,], file_info)
diff --git a/test/hemingway_driver.py b/test/hemingway_driver.py
new file mode 100644 (file)
index 0000000..d43d2c3
--- /dev/null
@@ -0,0 +1,40 @@
+# 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 os.path
+>>> from hooke.hooke import Hooke, HookeRunner
+>>> h = Hooke()
+>>> r = HookeRunner()
+>>> playlist = os.path.join('test', 'data', 'fclamp_hemingway', 'playlist')
+>>> h = r.run_lines(h, ['load_playlist ' + playlist]) # doctest: +ELLIPSIS
+<FilePlaylist playlist.hkp>
+Success
+<BLANKLINE>
+>>> h = r.run_lines(h, ['curve_info']) # doctest: +ELLIPSIS, +REPORT_UDIFF
+name: 20080428_a53t-0-0-10.dat
+path: test/data/fclamp_hemingway/20080428_a53t-0-0-10.dat
+experiment: <class 'hooke.experiment.ForceClamp'>
+driver: <hooke.driver.hemingway.HemingwayDriver object at 0x...>
+filetype: hemingway
+note: 
+blocks: 1
+block sizes: [(14798, 5)]
+Success
+<BLANKLINE>
+"""