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