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