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