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