| 1 |  |  | # Copyright 2014 Diamond Light Source Ltd. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | # Licensed under the Apache License, Version 2.0 (the "License"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | # you may not use this file except in compliance with the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | # You may obtain a copy of the License at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | #     http://www.apache.org/licenses/LICENSE-2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | # Unless required by applicable law or agreed to in writing, software | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | # distributed under the License is distributed on an "AS IS" BASIS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | # See the License for the specific language governing permissions and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | # limitations under the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | # $Id: dezing_filter.py 467 2016-02-16 11:40:42Z kny48981 $ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | .. module:: dezinger_deprecated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |    :platform: Unix | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |    :synopsis: A plugin to remove zingers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | .. moduleauthor:: Mark Basham <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | import numpy as np | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | import dezing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | from savu.plugins.filters.base_filter import BaseFilter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | from savu.plugins.driver.cpu_plugin import CpuPlugin | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | from savu.plugins.utils import register_plugin | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | class DezingerDeprecated(BaseFilter, CpuPlugin): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         super(DezingerDeprecated, self).__init__("DezingerDeprecated") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         self.warnflag = 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         self.errflag = 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     def pre_process(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         # Apply dezing to dark and flat images | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         inData = self.get_in_datasets()[0] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         dark = inData.data.dark() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         flat = inData.data.flat() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         self.data_size = inData.get_shape() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         pad_list = ((self.pad, self.pad), (0, 0), (0, 0)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         # dezing the dark field        print "*****in data shape in base filter", in_dataset[0].get_shape() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if dark.size: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             (retval, self.warnflag, self.errflag) = dezing.setup_size( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 dark.shape, self.parameters['outlier_mu'], self.pad, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 mode=self.parameters['mode']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             dark = self._dezing(np.pad(dark, pad_list, mode='edge')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             inData.data.update_dark(dark[self.pad:-self.pad]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             (retval, self.warnflag, self.errflag) = dezing.cleanup() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         # dezing the flat field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         if flat.size: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             (retval, self.warnflag, self.errflag) = dezing.setup_size( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 flat.shape, self.parameters['outlier_mu'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 self.pad, mode=self.parameters['mode']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             flat = self._dezing(np.pad(flat, pad_list, mode='edge')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             inData.data.update_flat(flat[self.pad:-self.pad]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             (retval, self.warnflag, self.errflag) = dezing.cleanup() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         # setup dezing for data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         self._dezing_setup(self.data_size) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     def _dezing_setup(self, shape): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         (retval, self.warnflag, self.errflag) = \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             dezing.setup_size(shape, self.parameters['outlier_mu'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                               self.pad, mode=self.parameters['mode']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     def _process_calibration_frames(self, data): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         nSlices = data.shape[self.proj_dim] - 2*self.pad | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         nSublists = int(np.ceil(nSlices/float(self.frame_limit))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         idx = np.array_split(np.arange(self.pad, nSlices+self.pad), nSublists) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         idx = [np.arange(a[0]-self.pad, a[-1]+self.pad+1) for a in idx] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         out_sl = np.tile([slice(None)]*3, [len(idx), 1]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         out_sl[:, self.proj_dim] = idx | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         result = np.empty_like(data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         for sl in out_sl: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             result[tuple(sl)] = self._dezing(data[tuple(sl)]) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     def _dezing(self, data): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         result = np.empty_like(data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         (retval, self.warnflag, self.errflag) = dezing.run(data, result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         return result | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     def process_frames(self, data): | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         return self._dezing(data[0]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     def post_process(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         (retval, self.warnflag, self.errflag) = dezing.cleanup() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     def get_max_frames(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         return 'multiple' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     def raw_data(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         return True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     def set_filter_padding(self, in_data, out_data): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         in_data = in_data[0] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         self.pad = (self.parameters['kernel_size'] - 1) / 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         in_data.padding = {'pad_multi_frames': self.pad} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         out_data[0].padding = {'pad_multi_frames': self.pad} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     def executive_summary(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         if self.errflag != 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             return(["ERRORS detected in dezing plugin, Check the detailed \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | log messages."]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         if self.warnflag != 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             return(["WARNINGS detected in dezing plugin, Check the detailed \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | log messages."]) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 119 |  |  |         return ["Nothing to Report"] | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |  |