| 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 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | .. module:: lfov_loader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |    :platform: Unix | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |    :synopsis: Large field-of-view loader. A class for loading 2 standard | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     tomography scans. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | .. moduleauthor:: Nicola Wadeson <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | import os | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | import copy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | import numpy as np | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | import glob | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | import savu.test.test_utils as tu | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | from savu.plugins.utils import register_plugin | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | from savu.plugins.loaders.full_field_loaders.multi_nxtomo_loader import \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         MultiNxtomoLoader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | @register_plugin | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | class LfovLoader(MultiNxtomoLoader): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     def __init__(self, name='LfovLoader'): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         super(LfovLoader, self).__init__(name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     def _update_preview(self, preview, offset): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         Add extra sinograms to the preview if there are not enough to stitch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         at least one frame after taking into account the offset between images. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         Parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         ---------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         preview : list | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             A Savu preview data list. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         Returns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         ------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         None. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if not offset or not preview: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             return preview | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         dObj = self.exp.index['in_data']['tomo'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         # Revert the data shape to before previewing was applied | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         dObj.set_shape(dObj.get_original_shape()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         preview = dObj.get_preview().get_integer_entries(preview) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         sino_slice_dim = dObj.get_data_dimension_by_axis_label("detector_y") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         max_sino_idx = dObj.data.shape[sino_slice_dim] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         sl = list(map(int, preview[sino_slice_dim].split(":"))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         sl_0 = sl[0] + offset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         sl_1 = sl[1] + offset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if (max_sino_idx >= sl_0 >= 0) and (max_sino_idx >= sl_1 >= 0): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             sl[0] = sl_0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             sl[1] = sl_1    | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             preview[sino_slice_dim] = ":".join(map(str, sl)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             self.parameters['preview'][sino_slice_dim] = preview[sino_slice_dim] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return preview | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     def _get_order_list(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         order = self.parameters['order'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         if order[0] < order[-1]: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             order_list = np.arange(order[0], order[1] + 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             order_list = np.arange(order[0], order[1] - 1, -1)         | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return order_list | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     def _get_file_list(self, order_list): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         shared_name = self.parameters["file_name"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         file_path = copy.copy(self.exp.meta_data.get('data_file')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         self.exp.meta_data.set('data_file', file_path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         if shared_name is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             file_list = self._find_files(file_path, "/*.hdf") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             if len(file_list) == 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 file_list = self._find_files(file_path, "/*.nxs") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             file_list = self._find_files(file_path, "/*" + shared_name + "*") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         if len(order_list) != len(file_list): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             raise ValueError( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 "Number of files found in the folder is not the same as the" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 " requested number") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         return file_list | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     def _find_files(self, base, name): | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         return sorted(glob.glob(base + name)) if base else [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     def _get_data_objects(self, nxtomo): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         order_list = self._get_order_list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         file_list = self._get_file_list(order_list)         | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         dark_folder, dark_key, dark_scale = self.parameters['dark'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         flat_folder, flat_key, flat_scale = self.parameters['flat'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         dark_folder = self.get_conf_path(dark_folder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         flat_folder = self.get_conf_path(flat_folder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         dark_list = self._find_files(dark_folder, "/*dark*") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         flat_list = self._find_files(flat_folder, "/*flat*") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         offset = self.parameters['row_offset'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         data_obj_list = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         for i in order_list: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             self.exp.meta_data.set('data_file', file_list[i]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             # update darks and flats | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             if dark_folder is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 nxtomo.parameters['dark'] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     dark_list[i], dark_key, dark_scale] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             if flat_folder is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 nxtomo.parameters['flat'] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                     flat_list[i], flat_key, flat_scale] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             nxtomo.setup() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             # update preview | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             if offset[i] != 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 nxtomo.parameters['preview'] = \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                     self._update_preview(nxtomo.parameters['preview'], offset[i]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             data_obj_list.append(self.exp.index['in_data']['tomo']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             self.exp.index['in_data'] = {} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         return data_obj_list | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     def get_conf_path(self, path): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         # For Savu tests | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         if path.split(os.sep)[0] == 'Savu': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             path = tu.get_test_data_path(path.split('/test_data/data/')[1]) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 138 |  |  |         return path | 
            
                                                        
            
                                    
            
            
                | 139 |  |  |  |