WLC and FJC output are now with 2 decimal precision. Added a comment on autopeak...
[hooke.git] / review.py
1 #!/usr/bin/env python
2
3 '''review.py
4 Alberto Gomez-Casado (c) 2010 University of Twente
5 '''
6
7 from libhooke import WX_GOOD
8 import wxversion
9 wxversion.select(WX_GOOD)
10 from wx import PostEvent
11 import numpy as np
12 import shutil
13 import libinput as linp
14 import copy
15 import os.path
16
17 import warnings
18 warnings.simplefilter('ignore',np.RankWarning)
19
20
21 class reviewCommands:
22
23     def do_review(self,args):
24         '''
25         REVIEW
26         (review.py)
27         Presents curves (in current playlist) in groups of ten. User can indicate which curves will be selected to be saved in a separate directory for further analysis.
28         By default curves are presented separated -30 nm in x and -100 pN in y. 
29         Curve number one of each set is the one showing the approach.
30         ------------
31         Syntax:
32         review [x spacing (nm)] [y spacing (pN]
33
34         '''
35
36         args=args.split()
37
38         if len(args)==2:
39                 try:
40                         xgap=int(args[0])*1e-9 #scale to SI units 
41                         ygap=int(args[1])*1e-12
42                 except:
43                         print 'Spacings must be numeric! Using defaults'
44                         xgap=-30*1e-9
45                         ygap=-100*1e-12 
46         else:
47                 xgap=-30*1e-9
48                 ygap=-100*1e-12 
49                   
50         print 'Processing playlist...'
51         print '(Please wait)'
52         keep_list=[]
53         
54         c=0
55         print 'You can stop the review at any moment by entering \'q\' you can go back ten curves entering \'b\''
56         print 'What curve no. you would like to start? (Enter for starting from the first)'
57         skip=raw_input()
58         
59         if skip.isdigit()==False:
60             skip=0
61         else:
62             skip=int(skip)
63             print 'Skipping '+str(skip)+ ' curves'
64             c=skip      
65
66         while c < len(self.current_list):
67                 
68         
69             #take a group of ten curves and plot them with some spacing
70                                 
71             curveset=self.current_list[c:c+10]
72         
73             base=curveset[0]    
74             self.current=base
75             self.do_plot(0)     
76             multiplot=copy.deepcopy(self._get_displayed_plot(0))
77             self.current.curve.close_all()      
78
79             for i in range(1,10):
80                 if i >= len(curveset):
81                         print 'End of the list'
82                         print 'WARNING: maybe you want to finish!'
83                         break
84                 nextitem=curveset[i]
85                 if not nextitem.identify(self.drivers):
86                         continue                
87                 nextplot=self.plotmanip_correct(nextitem.curve.default_plots()[0],nextitem)
88                 nextvect=nextplot.vectors
89                 nextitem.curve.close_all()
90
91                 nextx=nextvect[1][0]
92                 nexty=nextvect[1][1]
93                 #center y around 0      
94                 ymedian=np.median(nexty)
95                 pos=0           
96                 for j in range(0,len(nextx)):
97                         nextx[j]=nextx[j]+i*xgap
98                         nexty[j]=nexty[j]+i*ygap-ymedian
99                 multiplot.add_set(nextx,nexty) 
100                 multiplot.styles.append('lines')
101                 multiplot.colors.append(None)
102                 
103             self._send_plot([multiplot])                
104                                 
105             
106             print 'Which ones you want to keep?'        
107             keep=raw_input()
108             if keep.isalpha():
109                 if keep=='b':
110                         print 'Going back ten curves'
111                         c-=10
112                         if c<0:
113                                 print 'We are already at the start'
114                                 c=0
115                         continue
116                 if keep=='q':
117                         break
118             else:
119                 for i in keep.split():
120                         if i.isdigit() and int(i)>0 and int(i)<11: #if it is not digit the int() call is never made, so no exception should be happening
121                                 keep_item=curveset[int(i)-1].path
122                                 if keep_item in keep_list:
123                                         print 'This curve ('+keep_item+') was already selected, skipping'
124                                 else:
125                                         keep_list.append(keep_item)
126                         else:
127                                 print 'You entered an invalid value: '+i
128                                         
129             c+=10
130
131         #FIXME I don't know why the print below gives errors sometimes
132         try:
133                 print 'Kept '+str(len(keep_list))+' curves from '+str(min(c+i+1,len(self.current_list)))
134         except:
135                 print 'Display error, never mind, we continue. Below the amount of kept and total curves:'
136                 print str(len(keep_list))
137                 print str(len(self.current_list))
138
139         allok=0  #flag to keep from losing all the work in a slight mistake
140         while allok==0:
141                 if len(keep_list) == 0:
142                         return
143                 save=linp.safeinput('Do you want to save the selected curves?',['y','n'])
144                 if save=='y':
145                         savedir=linp.safeinput('Destination directory?')
146                         savedir=os.path.abspath(savedir)
147                         if not os.path.isdir(savedir):
148                                 print 'Destination is not a directory. Try again'
149                                 continue
150                 if save=='n':
151                         allok=1
152                         return
153                 
154                 for item in keep_list:
155                         try:
156                                 shutil.copy(item, savedir)
157                                 allok=1
158                         except (OSError, IOError):
159                                 print 'Cannot copy file. '+item+' Perhaps you gave me a wrong directory?'
160                                 allok=0
161                                 break
162
163         return