1 # Copyright (C) 2008-2012 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 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
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
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/>.
19 """Global analysis of force curves with various parameters
29 from .. import libpeakspot as lps
30 from .. import curve as lhc
31 from .. import libhooke as lh
33 class massanalysisCommands(object):
36 self.mass_variables={}
37 self.interesting_variables=['curve','firstpeak_distance','lastpeak_distance','Npeaks','median_distance','mean_distance']
40 def _clean_data(self):
41 for variable in self.interesting_variables:
42 self.mass_variables[variable]=[]
44 def peak_position_from_contact(self, item, locations):
46 calculates X distance of a peak from the contact point
48 item.identify(self.drivers)
51 cut_index=self.find_contact_point()
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]
57 start_x=xret[cut_index]
59 real_positions=[abs((xret[index])-(start_x)) for index in locations]
61 item.curve.close_all()
62 #needed to avoid *big* memory leaks!
67 def do_maplist(self,args):
74 self._clean_data() #if we recall it, clean previous data!
75 min_deviation=self.convfilt_config['mindeviation']
79 for item in self.current_list:
81 peak_location,peak_size=self.exec_has_peaks(item, min_deviation)
82 real_positions=self.peak_position_from_contact(item, peak_location)
84 self.mass_variables['Npeaks'].append(len(peak_location))
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))
91 for index in range(len(real_positions)-1):
92 distancepeaks.append(real_positions[index+1]-real_positions[index])
94 self.mass_variables['firstpeak_distance'].append(0)
95 self.mass_variables['lastpeak_distance'].append(0)
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))
101 self.mass_variables['median_distance'].append(0)
102 self.mass_variables['mean_distance'].append(0)
106 print 'curve',c,'not mapped'
111 def do_plotmap(self,args):
116 x=self.mass_variables[args[0]]
117 y=self.mass_variables[args[1]]
119 print 'Give me two arguments between those:'
120 print self.interesting_variables
123 scattermap=lhc.PlotObject()
124 scattermap.vectors=[[]]
125 scattermap.vectors[0].append(x)
126 scattermap.vectors[0].append(y)
128 scattermap.units=[args[0],args[1]]
129 scattermap.styles=['scatter']
130 scattermap.destination=1
132 self._send_plot([scattermap])
134 def do_savemaps(self,args):
140 def csv_write_cols(data, f):
142 #from Bruno Desthuillers on comp.lang.python
144 writer = csv.writer(f)
146 writer.writerow(dict(zip(keys,keys)))
147 for row in zip(*data.values()):
148 writer.writerow(dict(zip(keys, row)))
152 lh.csv_write_dictionary(f,self.mass_variables)