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