74e483626be410a1a9fc49cac37a2bf64dc8b188
[hooke.git] / hooke / plugin / massanalysis.py
1 '''
2 massanalysis.py
3
4 Global analysis of force curves with various parameters
5
6 Requires:
7 libpeakspot.py
8 flatfilts.py
9 '''
10
11 import numpy as np
12 import csv
13
14 from .. import libpeakspot as lps
15 from .. import curve as lhc
16 from .. import libhooke as lh
17
18 class massanalysisCommands(object):
19
20     def _plug_init(self):
21         self.mass_variables={}
22         self.interesting_variables=['curve','firstpeak_distance','lastpeak_distance','Npeaks','median_distance','mean_distance']
23         self._clean_data()
24
25     def _clean_data(self):
26         for variable in self.interesting_variables:
27             self.mass_variables[variable]=[]
28
29     def peak_position_from_contact(self, item, locations):
30         '''
31         calculates X distance of a peak from the contact point
32         '''
33         item.identify(self.drivers)
34
35         real_positions=[]
36         cut_index=self.find_contact_point()
37
38         #we assume the first is the plot with the force curve
39         plot=item.curve.default_plots()[0]
40         xret=plot.vectors[1][0]
41
42         start_x=xret[cut_index]
43
44         real_positions=[abs((xret[index])-(start_x)) for index in locations]
45         #close all open files
46         item.curve.close_all()
47         #needed to avoid *big* memory leaks!
48         del item.curve
49         del item
50         return real_positions
51
52     def do_maplist(self,args):
53         '''
54         MAPLIST
55         (flatfilts.py)
56         ----
57         pass
58         '''
59         self._clean_data() #if we recall it, clean previous data!
60         min_deviation=self.convfilt_config['mindeviation']
61
62
63         c=0
64         for item in self.current_list:
65             try:
66                 peak_location,peak_size=self.exec_has_peaks(item, min_deviation)
67                 real_positions=self.peak_position_from_contact(item, peak_location)
68
69                 self.mass_variables['Npeaks'].append(len(peak_location))
70
71                 if len(peak_location) > 1:
72                     self.mass_variables['firstpeak_distance'].append(min(real_positions))
73                     self.mass_variables['lastpeak_distance'].append(max(real_positions))
74
75                     distancepeaks=[]
76                     for index in range(len(real_positions)-1):
77                         distancepeaks.append(real_positions[index+1]-real_positions[index])
78                 else:
79                     self.mass_variables['firstpeak_distance'].append(0)
80                     self.mass_variables['lastpeak_distance'].append(0)
81
82                 if len(peak_location) > 2:
83                     self.mass_variables['median_distance'].append(np.median(distancepeaks))
84                     self.mass_variables['mean_distance'].append(np.mean(distancepeaks))
85                 else:
86                     self.mass_variables['median_distance'].append(0)
87                     self.mass_variables['mean_distance'].append(0)
88
89                 print 'curve',c
90             except SyntaxError:
91                 print 'curve',c,'not mapped'
92                 pass
93
94             c+=1
95
96     def do_plotmap(self,args):
97         '''
98         '''
99         args=args.split()
100         if len(args)>1:
101             x=self.mass_variables[args[0]]
102             y=self.mass_variables[args[1]]
103         else:
104             print 'Give me two arguments between those:'
105             print self.interesting_variables
106             return
107
108         scattermap=lhc.PlotObject()
109         scattermap.vectors=[[]]
110         scattermap.vectors[0].append(x)
111         scattermap.vectors[0].append(y)
112
113         scattermap.units=[args[0],args[1]]
114         scattermap.styles=['scatter']
115         scattermap.destination=1
116
117         self._send_plot([scattermap])
118
119     def do_savemaps(self,args):
120         '''
121         args=filename
122         '''
123
124         '''
125         def csv_write_cols(data, f):
126
127             #from Bruno Desthuillers on comp.lang.python
128
129             writer = csv.writer(f)
130             keys = data.keys()
131             writer.writerow(dict(zip(keys,keys)))
132             for row in zip(*data.values()):
133                 writer.writerow(dict(zip(keys, row)))
134         '''
135
136         f=open(args,'wb')
137         lh.csv_write_dictionary(f,self.mass_variables)
138         f.close()