def run(self): """ Run method of the module. Uses the PARANG attributes to derotate the images (if *derotate* is set to True) and applies an optional mean or median stacking afterwards. Returns ------- NoneType None """ def _initialize(ndim, npix): if ndim == 2: nimages = 1 elif ndim == 3: nimages = self.m_image_in_port.get_shape()[0] if self.m_stack == 'median': frames = [0, nimages] else: frames = memory_frames(memory, nimages) if self.m_stack == 'mean': im_tot = np.zeros((npix, npix)) else: im_tot = None return nimages, frames, im_tot self.m_image_out_port.del_all_data() self.m_image_out_port.del_all_attributes() if self.m_image_in_port.tag == self.m_image_out_port.tag: raise ValueError( 'Input and output port should have a different tag.') memory = self._m_config_port.get_attribute('MEMORY') if self.m_derotate: parang = self.m_image_in_port.get_attribute('PARANG') ndim = self.m_image_in_port.get_ndim() npix = self.m_image_in_port.get_shape()[1] nimages, frames, im_tot = _initialize(ndim, npix) start_time = time.time() for i, _ in enumerate(frames[:-1]): progress(i, len(frames[:-1]), 'Running DerotateAndStackModule...', start_time) images = self.m_image_in_port[frames[i]:frames[i + 1], ] if self.m_derotate: angles = -parang[frames[i]:frames[i + 1]] + self.m_extra_rot images = rotate_images(images, angles) if self.m_stack is None: if ndim == 2: self.m_image_out_port.set_all(images[np.newaxis, ...]) elif ndim == 3: self.m_image_out_port.append(images, data_dim=3) elif self.m_stack == 'mean': im_tot += np.sum(images, axis=0) sys.stdout.write('Running DerotateAndStackModule... [DONE]\n') sys.stdout.flush() if self.m_stack == 'mean': im_stack = im_tot / float(nimages) self.m_image_out_port.set_all(im_stack[np.newaxis, ...]) elif self.m_stack == 'median': im_stack = np.median(images, axis=0) self.m_image_out_port.set_all(im_stack[np.newaxis, ...]) if self.m_derotate or self.m_stack is not None: self.m_image_out_port.copy_attributes(self.m_image_in_port) self.m_image_out_port.close_port()
def run(self) -> None: """ Run method of the module. Uses the ``PARANG`` attributes to derotate the images (if ``derotate`` is set to ``True``) and applies an optional mean or median stacking along the time or wavelengths dimension afterwards. Returns ------- NoneType None """ @typechecked def _initialize( ndim: int, npix: int ) -> Tuple[int, np.ndarray, Optional[np.ndarray], Optional[np.ndarray]]: if ndim == 2: nimages = 1 elif ndim == 3: nimages = self.m_image_in_port.get_shape()[-3] if self.m_stack == 'median': frames = np.array([0, nimages]) else: frames = memory_frames(memory, nimages) elif ndim == 4: nimages = self.m_image_in_port.get_shape()[-3] nwave = self.m_image_in_port.get_shape()[-4] if self.m_dimension == 'time': frames = np.linspace(0, nwave, nwave + 1) elif self.m_dimension == 'wavelength': frames = np.linspace(0, nimages, nimages + 1) else: raise ValueError( 'The dimension should be set to \'time\' or \'wavelength\'.' ) if self.m_stack == 'mean': if ndim == 4: if self.m_dimension == 'time': im_tot = np.zeros((nwave, npix, npix)) elif self.m_dimension == 'wavelength': im_tot = np.zeros((nimages, npix, npix)) else: im_tot = np.zeros((npix, npix)) else: im_tot = None if self.m_stack is None and ndim == 4: im_none = np.zeros((nwave, nimages, npix, npix)) else: im_none = None return nimages, frames, im_tot, im_none memory = self._m_config_port.get_attribute('MEMORY') if self.m_derotate: parang = self.m_image_in_port.get_attribute('PARANG') ndim = self.m_image_in_port.get_ndim() npix = self.m_image_in_port.get_shape()[-2] nimages, frames, im_tot, im_none = _initialize(ndim, npix) start_time = time.time() for i, _ in enumerate(frames[:-1]): progress(i, len(frames[:-1]), 'Derotating and/or stacking images...', start_time) if ndim == 3: # Get the images and ensure they have the correct 3D shape with the following # three dimensions: (batch_size, height, width) images = self.m_image_in_port[frames[i]:frames[i + 1], ] elif ndim == 4: # Process all time frames per exposure at once if self.m_dimension == 'time': images = self.m_image_in_port[i, :, ] elif self.m_dimension == 'wavelength': images = self.m_image_in_port[:, i, ] if self.m_derotate: if ndim == 4: if self.m_dimension == 'time': angles = -1. * parang + self.m_extra_rot elif self.m_dimension == 'wavelength': n_wavel = self.m_image_in_port.get_shape()[-4] angles = np.full(n_wavel, -1. * parang[i]) + self.m_extra_rot else: angles = -parang[frames[i]:frames[i + 1]] + self.m_extra_rot images = rotate_images(images, angles) if self.m_stack is None: if ndim == 2: self.m_image_out_port.set_all(images[np.newaxis, ...]) elif ndim == 3: self.m_image_out_port.append(images, data_dim=3) elif ndim == 4: if self.m_dimension == 'time': im_none[i] = images elif self.m_dimension == 'wavelength': im_none[:, i] = images elif self.m_stack == 'mean': if ndim == 4: im_tot[i] = np.sum(images, axis=0) else: im_tot += np.sum(images, axis=0) if self.m_stack == 'mean': if ndim == 4: im_stack = im_tot / float(im_tot.shape[0]) if self.m_dimension == 'time': self.m_image_out_port.set_all(im_stack[:, np.newaxis, ...]) elif self.m_dimension == 'wavelength': self.m_image_out_port.set_all(im_stack[np.newaxis, ...]) else: im_stack = im_tot / float(nimages) self.m_image_out_port.set_all(im_stack[np.newaxis, ...]) elif self.m_stack == 'median': if ndim == 4: images = self.m_image_in_port[:] if self.m_dimension == 'time': im_stack = np.median(images, axis=1) self.m_image_out_port.set_all(im_stack[:, np.newaxis, ...]) elif self.m_dimension == 'wavelength': im_stack = np.median(images, axis=0) self.m_image_out_port.set_all(im_stack[np.newaxis, ...]) else: im_stack = np.median(images, axis=0) self.m_image_out_port.set_all(im_stack[np.newaxis, ...]) elif self.m_stack is None and ndim == 4: if self.m_dimension == 'time': self.m_image_out_port.set_all(im_none) elif self.m_dimension == 'wavelength': self.m_image_out_port.set_all(im_none) if self.m_derotate or self.m_stack is not None: self.m_image_out_port.copy_attributes(self.m_image_in_port) self.m_image_out_port.close_port()