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