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