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