1 # Copyright (C) 2008-2010 Massimo Sandal <devicerandom@gmail.com>
2 # W. Trevor King <wking@drexel.edu>
4 # This file is part of Hooke.
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.
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.
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/>.
20 """Global analysis of force curves with various parameters
30 from .. import libpeakspot as lps
31 from .. import curve as lhc
32 from .. import libhooke as lh
34 class massanalysisCommands(object):
37 self.mass_variables={}
38 self.interesting_variables=['curve','firstpeak_distance','lastpeak_distance','Npeaks','median_distance','mean_distance']
41 def _clean_data(self):
42 for variable in self.interesting_variables:
43 self.mass_variables[variable]=[]
45 def peak_position_from_contact(self, item, locations):
47 calculates X distance of a peak from the contact point
49 item.identify(self.drivers)
52 cut_index=self.find_contact_point()
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]
58 start_x=xret[cut_index]
60 real_positions=[abs((xret[index])-(start_x)) for index in locations]
62 item.curve.close_all()
63 #needed to avoid *big* memory leaks!
68 def do_maplist(self,args):
75 self._clean_data() #if we recall it, clean previous data!
76 min_deviation=self.convfilt_config['mindeviation']
80 for item in self.current_list:
82 peak_location,peak_size=self.exec_has_peaks(item, min_deviation)
83 real_positions=self.peak_position_from_contact(item, peak_location)
85 self.mass_variables['Npeaks'].append(len(peak_location))
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))
92 for index in range(len(real_positions)-1):
93 distancepeaks.append(real_positions[index+1]-real_positions[index])
95 self.mass_variables['firstpeak_distance'].append(0)
96 self.mass_variables['lastpeak_distance'].append(0)
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))
102 self.mass_variables['median_distance'].append(0)
103 self.mass_variables['mean_distance'].append(0)
107 print 'curve',c,'not mapped'
112 def do_plotmap(self,args):
117 x=self.mass_variables[args[0]]
118 y=self.mass_variables[args[1]]
120 print 'Give me two arguments between those:'
121 print self.interesting_variables
124 scattermap=lhc.PlotObject()
125 scattermap.vectors=[[]]
126 scattermap.vectors[0].append(x)
127 scattermap.vectors[0].append(y)
129 scattermap.units=[args[0],args[1]]
130 scattermap.styles=['scatter']
131 scattermap.destination=1
133 self._send_plot([scattermap])
135 def do_savemaps(self,args):
141 def csv_write_cols(data, f):
143 #from Bruno Desthuillers on comp.lang.python
145 writer = csv.writer(f)
147 writer.writerow(dict(zip(keys,keys)))
148 for row in zip(*data.values()):
149 writer.writerow(dict(zip(keys, row)))
153 lh.csv_write_dictionary(f,self.mass_variables)