Ran update_copyright.py
[hooke.git] / hooke / plugin / massanalysis.py
1 # Copyright (C) 2008-2010 Massimo Sandal <devicerandom@gmail.com>
2 #                         W. Trevor King <wking@drexel.edu>
3 #
4 # This file is part of Hooke.
5 #
6 # Hooke is free software: you can redistribute it and/or modify it
7 # under the terms of the GNU Lesser General Public License as
8 # published by the Free Software Foundation, either version 3 of the
9 # License, or (at your option) any later version.
10 #
11 # Hooke is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
14 # Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with Hooke.  If not, see
18 # <http://www.gnu.org/licenses/>.
19
20 """Global analysis of force curves with various parameters
21
22 Requires:
23 libpeakspot.py
24 flatfilts.py
25 """
26
27 import numpy as np
28 import csv
29
30 from .. import libpeakspot as lps
31 from .. import curve as lhc
32 from .. import libhooke as lh
33
34 class massanalysisCommands(object):
35
36     def _plug_init(self):
37         self.mass_variables={}
38         self.interesting_variables=['curve','firstpeak_distance','lastpeak_distance','Npeaks','median_distance','mean_distance']
39         self._clean_data()
40
41     def _clean_data(self):
42         for variable in self.interesting_variables:
43             self.mass_variables[variable]=[]
44
45     def peak_position_from_contact(self, item, locations):
46         '''
47         calculates X distance of a peak from the contact point
48         '''
49         item.identify(self.drivers)
50
51         real_positions=[]
52         cut_index=self.find_contact_point()
53
54         #we assume the first is the plot with the force curve
55         plot=item.curve.default_plots()[0]
56         xret=plot.vectors[1][0]
57
58         start_x=xret[cut_index]
59
60         real_positions=[abs((xret[index])-(start_x)) for index in locations]
61         #close all open files
62         item.curve.close_all()
63         #needed to avoid *big* memory leaks!
64         del item.curve
65         del item
66         return real_positions
67
68     def do_maplist(self,args):
69         '''
70         MAPLIST
71         (flatfilts.py)
72         ----
73         pass
74         '''
75         self._clean_data() #if we recall it, clean previous data!
76         min_deviation=self.convfilt_config['mindeviation']
77
78
79         c=0
80         for item in self.current_list:
81             try:
82                 peak_location,peak_size=self.exec_has_peaks(item, min_deviation)
83                 real_positions=self.peak_position_from_contact(item, peak_location)
84
85                 self.mass_variables['Npeaks'].append(len(peak_location))
86
87                 if len(peak_location) > 1:
88                     self.mass_variables['firstpeak_distance'].append(min(real_positions))
89                     self.mass_variables['lastpeak_distance'].append(max(real_positions))
90
91                     distancepeaks=[]
92                     for index in range(len(real_positions)-1):
93                         distancepeaks.append(real_positions[index+1]-real_positions[index])
94                 else:
95                     self.mass_variables['firstpeak_distance'].append(0)
96                     self.mass_variables['lastpeak_distance'].append(0)
97
98                 if len(peak_location) > 2:
99                     self.mass_variables['median_distance'].append(np.median(distancepeaks))
100                     self.mass_variables['mean_distance'].append(np.mean(distancepeaks))
101                 else:
102                     self.mass_variables['median_distance'].append(0)
103                     self.mass_variables['mean_distance'].append(0)
104
105                 print 'curve',c
106             except SyntaxError:
107                 print 'curve',c,'not mapped'
108                 pass
109
110             c+=1
111
112     def do_plotmap(self,args):
113         '''
114         '''
115         args=args.split()
116         if len(args)>1:
117             x=self.mass_variables[args[0]]
118             y=self.mass_variables[args[1]]
119         else:
120             print 'Give me two arguments between those:'
121             print self.interesting_variables
122             return
123
124         scattermap=lhc.PlotObject()
125         scattermap.vectors=[[]]
126         scattermap.vectors[0].append(x)
127         scattermap.vectors[0].append(y)
128
129         scattermap.units=[args[0],args[1]]
130         scattermap.styles=['scatter']
131         scattermap.destination=1
132
133         self._send_plot([scattermap])
134
135     def do_savemaps(self,args):
136         '''
137         args=filename
138         '''
139
140         '''
141         def csv_write_cols(data, f):
142
143             #from Bruno Desthuillers on comp.lang.python
144
145             writer = csv.writer(f)
146             keys = data.keys()
147             writer.writerow(dict(zip(keys,keys)))
148             for row in zip(*data.values()):
149                 writer.writerow(dict(zip(keys, row)))
150         '''
151
152         f=open(args,'wb')
153         lh.csv_write_dictionary(f,self.mass_variables)
154         f.close()