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