def ValidateROI(self, ROI): return MI.ValidateROI(ROI, self.ImageShape())
def Compute(self): sf.CheckCreateFolder(self.outFolder) logging.info( 'NonAffMaps.Compute() started! Result will be saved in folder ' + str(self.outFolder)) # Search for correlation map MIfiles, skip autocorrelation maps fw_mistack = self.cmaps_fw.GetCorrMaps(openMIfiles=True) bk_mistack = self.cmaps_bk.GetCorrMaps(openMIfiles=True) common_lags = list( set(fw_mistack.IdxList).intersection(bk_mistack.IdxList)) if self.lag_range is None: if 0 in common_lags: common_lags.remove(0) else: if self.lag_range[1] < 0: self.lag_range[1] = np.max(common_lags) + 1 common_lags = [ lag for lag in common_lags if (lag != 0 and lag >= self.lag_range[0] and lag <= self.lag_range[1]) ] self.lagList = common_lags # Export configuration self.ExportConfiguration() if self.trans_bk_matrix is not None: tr_matrix = np.reshape(np.asarray(self.trans_bk_matrix), (2, 2)) logging.debug( 'Backscattered correlation maps will be transformed using matrix ' + str(tr_matrix) + ' and offset ' + str(self.trans_bk_offset)) # For each couple of correlation maps (with equal lagtime) for lidx in range(len(self.lagList)): logging.info('Now working on lagtime ' + str(lidx) + '/' + str(len(self.lagList)) + ' (d' + str(self.lagList[lidx]) + ')') fw_lidx = fw_mistack.IdxList.index(self.lagList[lidx]) bk_lidx = bk_mistack.IdxList.index(self.lagList[lidx]) # eventually compute normalization factors if self.norm_range is not None: fw_norm_factor = np.mean(fw_mistack.MIfiles[fw_lidx].Read( zRange=self.norm_range[:2], cropROI=self.norm_range[2:], closeAfter=False)) if self.trans_bk_matrix is None and self.trans_bk_offset is None: bk_norm_factor = np.mean(bk_mistack.MIfiles[bk_lidx].Read( zRange=self.norm_range[:2], cropROI=self.norm_range[2:], closeAfter=False)) else: bk_norm_data = bk_mistack.MIfiles[bk_lidx].Read( zRange=self.norm_range[:2], cropROI=None, closeAfter=False) if len(bk_norm_data.shape) > 2: bk_norm_data = np.mean(bk_norm_data, axis=0) logging.debug('shape before transformation: ' + str(bk_norm_data.shape)) bk_norm_data = sp.ndimage.affine_transform(bk_norm_data, tr_matrix, offset=self.trans_bk_offset,\ output_shape=bk_norm_data.shape, order=1, mode='constant', cval=1.0) norm_cropROI = MI.ValidateROI(self.norm_range[2:], bk_norm_data.shape, replaceNone=True) logging.debug('shape after transformation: ' + str(bk_norm_data.shape) + ' will be cropped with ROI ' + str(norm_cropROI)) bk_norm_factor = np.mean(bk_norm_data[norm_cropROI[1]:norm_cropROI[1]+norm_cropROI[3],\ norm_cropROI[0]:norm_cropROI[0]+norm_cropROI[2]]) bk_norm_data = None else: fw_norm_factor, bk_norm_factor = 1, 1 logging.info('Normalization factors: ' + str(fw_norm_factor) + ' (front) and ' + str(bk_norm_factor) + ' (back)') # load, normalize and eventually smooth correlation maps. fw_data = np.true_divide( fw_mistack.MIfiles[fw_lidx].Read(zRange=self.t_range, cropROI=self.cropROI, closeAfter=True), fw_norm_factor) bk_data = np.true_divide( bk_mistack.MIfiles[bk_lidx].Read(zRange=self.t_range, cropROI=self.cropROI, closeAfter=True), bk_norm_factor) if self.smooth_kernel_specs is not None: Kernel3D = self.LoadKernel(self.smooth_kernel_specs) logging.debug('Smoothing with kernel with shape ' + str(Kernel3D.shape)) fw_data = signal.convolve(fw_data, Kernel3D, mode='same') bk_data = signal.convolve(bk_data, Kernel3D, mode='same') # transform backscattered images if self.trans_bk_matrix is not None: tr_matrix3D = np.asarray( [[1, 0, 0], [0, tr_matrix[0, 0], tr_matrix[0, 1]], [0, tr_matrix[1, 0], tr_matrix[1, 1]]]) tr_offset3D = np.asarray( [0, self.trans_bk_offset[0], self.trans_bk_offset[1]]) bk_data = sp.ndimage.affine_transform(bk_data, tr_matrix3D, offset=tr_offset3D,\ output_shape=fw_data.shape, order=1, mode='constant', cval=1.0) # sigma2 = ln(forward-scattering corr / backscattering corr) * 6 / (qz_bk^2 - qz_fw^2) sigma2 = np.log(np.true_divide( fw_data, bk_data)) * 6.0 / (self.qz_bk**2 - self.qz_fw**2) # For the first lagtime, generate and export metadata if (lidx == 0): out_meta = fw_mistack.MIfiles[fw_lidx].GetMetadata().copy() out_meta['hdr_len'] = 0 out_meta['gap_bytes'] = 0 out_meta['shape'] = list(sigma2.shape) if ('fps' in out_meta): val_tRange = fw_mistack.MIfiles[fw_lidx].Validate_zRange( self.t_range) out_meta['fps'] = float( out_meta['fps']) * 1.0 / val_tRange[2] exp_config = cf.Config() exp_config.Import(out_meta, section_name='MIfile') metadata_fname = os.path.join(self.outFolder, 'NAffMap_metadata.ini') exp_config.Export(metadata_fname) logging.info('Metadata exported to file ' + str(metadata_fname)) # export data cur_fname = 'NaffMap_d' + str(self.lagList[lidx]).zfill(4) + '.dat' MI.MIfile(os.path.join(self.outFolder, cur_fname), metadata_fname).WriteData(sigma2) logging.info('Result saved to file ' + str(cur_fname)) fw_mistack.MIfiles[fw_lidx].Close() bk_mistack.MIfiles[bk_lidx].Close()