示例#1
0
    def run(self) -> None:
        """
        Run method of the module. Removes the frames and corresponding attributes, updates the
        NFRAMES attribute, and saves the data and attributes.

        Returns
        -------
        NoneType
            None
        """

        if self.m_index_in_port is not None:
            self.m_frames = self.m_index_in_port.get_all()

        if np.size(np.where(self.m_frames >= self.m_image_in_port.get_shape()[0])) > 0:
            raise ValueError(f'Some values in \'frames\' are larger than the total number of '
                             f'available frames, {self.m_image_in_port.get_shape()[0]}.')

        write_selected_data(memory=self._m_config_port.get_attribute('MEMORY'),
                            indices=self.m_frames,
                            image_in_port=self.m_image_in_port,
                            selected_out_port=self.m_selected_out_port,
                            removed_out_port=self.m_removed_out_port)

        write_selected_attributes(indices=self.m_frames,
                                  image_in_port=self.m_image_in_port,
                                  selected_out_port=self.m_selected_out_port,
                                  removed_out_port=self.m_removed_out_port,
                                  module_type='RemoveFramesModule',
                                  history=f'frames removed = {np.size(self.m_frames)}')

        self.m_image_in_port.close_port()
示例#2
0
    def run(self) -> None:
        """
        Run method of the module. Applies a frame selection on the derotated residuals from the
        PSF subtraction. The pixels within an annulus (e.g. at the separation of an expected
        planet) are selected and the standard deviation is calculated. The chosen percentage
        of images with the lowest standard deviation are stored as output.

        Returns
        -------
        NoneType
            None
        """

        pixscale = self.m_image_in_port.get_attribute('PIXSCALE')
        nimages = self.m_image_in_port.get_shape()[0]
        npix = self.m_image_in_port.get_shape()[-1]

        rr_grid, _, _ = pixel_distance((npix, npix), position=None)

        pixel_select = np.where((rr_grid > self.m_annulus_radii[0] / pixscale)
                                &
                                (rr_grid < self.m_annulus_radii[1] / pixscale))

        start_time = time.time()
        phot_annulus = np.zeros(nimages)

        for i in range(nimages):
            progress(i, nimages, 'Aperture photometry...', start_time)

            phot_annulus[i] = np.sum(
                np.abs(self.m_image_in_port[i][pixel_select]))

        print(
            f'Minimum, maximum = {np.amin(phot_annulus):.2f}, {np.amax(phot_annulus):.2f}'
        )
        print(
            f'Mean, median = {np.nanmean(phot_annulus):.2f}, {np.nanmedian(phot_annulus):.2f}'
        )
        print(f'Standard deviation = {np.nanstd(phot_annulus):.2f}')

        n_select = int(nimages * self.m_percentage / 100.)
        index_del = np.argsort(phot_annulus)[n_select:]

        write_selected_data(memory=self._m_config_port.get_attribute('MEMORY'),
                            indices=index_del,
                            image_in_port=self.m_image_in_port,
                            selected_out_port=self.m_selected_out_port,
                            removed_out_port=self.m_removed_out_port)

        write_selected_attributes(indices=index_del,
                                  image_in_port=self.m_image_in_port,
                                  selected_out_port=self.m_selected_out_port,
                                  removed_out_port=self.m_removed_out_port,
                                  module_type='ResidualSelectionModule',
                                  history=f'frames removed = {index_del.size}')

        self.m_image_in_port.close_port()
示例#3
0
    def run(self) -> None:
        """
        Run method of the module. Removes the frames and corresponding attributes, updates the
        NFRAMES attribute, and saves the data and attributes.

        Returns
        -------
        NoneType
            None
        """

        self._initialize()

        memory = self._m_config_port.get_attribute('MEMORY')

        nimages = self.m_image_in_port.get_shape()[0]
        frames = memory_frames(memory, nimages)

        if memory == 0 or memory >= nimages:
            memory = nimages

        start_time = time.time()
        for i, _ in enumerate(frames[:-1]):
            progress(i, len(frames[:-1]), 'Running RemoveFramesModule...', start_time)

            images = self.m_image_in_port[frames[i]:frames[i+1], ]

            index_del = np.where(np.logical_and(self.m_frames >= frames[i],
                                                self.m_frames < frames[i+1]))

            write_selected_data(images,
                                self.m_frames[index_del] % memory,
                                self.m_selected_out_port,
                                self.m_removed_out_port)

        sys.stdout.write('Running RemoveFramesModule... [DONE]\n')
        sys.stdout.flush()

        history = 'frames removed = '+str(np.size(self.m_frames))

        if self.m_selected_out_port is not None:
            # Copy attributes before write_selected_attributes is used
            self.m_selected_out_port.copy_attributes(self.m_image_in_port)
            self.m_selected_out_port.add_history('RemoveFramesModule', history)

        if self.m_removed_out_port is not None:
            # Copy attributes before write_selected_attributes is used
            self.m_removed_out_port.copy_attributes(self.m_image_in_port)
            self.m_removed_out_port.add_history('RemoveFramesModule', history)

        write_selected_attributes(self.m_frames,
                                  self.m_image_in_port,
                                  self.m_selected_out_port,
                                  self.m_removed_out_port)

        self.m_image_in_port.close_port()
示例#4
0
    def run(self):
        """
        Run method of the module. Removes the frames and corresponding attributes, updates the
        NFRAMES attribute, and saves the data and attributes.

        :return: None
        """

        self._initialize()

        memory = self._m_config_port.get_attribute("MEMORY")

        nimages = number_images_port(self.m_image_in_port)
        frames = memory_frames(memory, nimages)

        if memory == 0 or memory >= nimages:
            memory = nimages

        for i, _ in enumerate(frames[:-1]):
            progress(i, len(frames[:-1]), "Running RemoveFramesModule...")

            images = self.m_image_in_port[frames[i]:frames[i + 1], ]

            index_del = np.where(np.logical_and(self.m_frames >= frames[i], \
                                                self.m_frames < frames[i+1]))

            write_selected_data(images, self.m_frames[index_del] % memory,
                                self.m_selected_out_port,
                                self.m_removed_out_port)

        sys.stdout.write("Running RemoveFramesModule... [DONE]\n")
        sys.stdout.flush()

        history = "frames removed = " + str(np.size(self.m_frames))

        if self.m_selected_out_port is not None:
            # Copy attributes before write_selected_attributes is used
            self.m_selected_out_port.copy_attributes_from_input_port(
                self.m_image_in_port)
            self.m_selected_out_port.add_history_information(
                "RemoveFramesModule", history)

        if self.m_removed_out_port is not None:
            # Copy attributes before write_selected_attributes is used
            self.m_removed_out_port.copy_attributes_from_input_port(
                self.m_image_in_port)
            self.m_removed_out_port.add_history_information(
                "RemoveFramesModule", history)

        write_selected_attributes(self.m_frames, self.m_image_in_port,
                                  self.m_selected_out_port,
                                  self.m_removed_out_port)

        self.m_image_in_port.close_port()
示例#5
0
    def run(self) -> None:
        """
        Run method of the module. Selects images according to a specified attribute tag and
        ordering, e.g. the highest 150 ``INDEX`` frames, or the lowest 50 ``PCC`` frames.
        The order of the selected images is determined by the `descending` or `ascending`
        attribute values. To sort the images again by their original order, the
        :class:`~pynpoint.processing.psfpreparation.SortParangModule` can be used.

        Returns
        -------
        NoneType
            None
        """

        nimages = self.m_image_in_port.get_shape()[0]
        attribute = self.m_image_in_port.get_attribute(
            f'{self.m_attribute_tag}')

        if nimages != attribute.size:
            raise ValueError(
                f'The attribute {{self.m_attribute_tag}} does not have the same '
                f'length ({len(attribute)}) as the tag has images ({nimages}). '
                f'Please check the attribute you have chosen for selection.')

        if self.m_order == 'descending':
            # sort attribute in descending order
            sorting_order = np.argsort(attribute)[::-1]
        else:
            # sort attribute in ascending order
            sorting_order = np.argsort(attribute)

        index_del = sorting_order[self.m_number_frames:]

        write_selected_data(memory=self._m_config_port.get_attribute('MEMORY'),
                            indices=index_del,
                            image_in_port=self.m_image_in_port,
                            selected_out_port=self.m_selected_out_port,
                            removed_out_port=self.m_removed_out_port)

        write_selected_attributes(
            indices=index_del,
            image_in_port=self.m_image_in_port,
            selected_out_port=self.m_selected_out_port,
            removed_out_port=self.m_removed_out_port,
            module_type='SelectByAttributeModule',
            history=f'selected tag = {self.m_attribute_tag}')

        self.m_image_in_port.close_port()
示例#6
0
    def run(self) -> None:
        """
        Run method of the module. Smooths the images with a Gaussian kernel, locates the brightest
        pixel in each image, measures the integrated flux around the brightest pixel, calculates
        the median and standard deviation of the photometry, and applies sigma clipping to remove
        low quality images.

        Returns
        -------
        NoneType
            None
        """

        def _get_aperture(aperture):
            if aperture[0] == 'circular':
                aperture = (0., aperture[1]/pixscale)

            elif aperture[0] == 'annulus' or aperture[0] == 'ratio':
                aperture = (aperture[1]/pixscale, aperture[2]/pixscale)

            return aperture

        def _get_starpos(fwhm, position):
            starpos = np.zeros((nimages, 2), dtype=np.int64)

            if fwhm is None:
                starpos[:, 0] = position[0]
                starpos[:, 1] = position[1]

            else:
                if position is None:
                    center = None
                    width = None

                else:
                    if position[0] is None and position[1] is None:
                        center = None
                    else:
                        center = position[0:2]

                    width = int(math.ceil(position[2]/pixscale))

                for i, _ in enumerate(starpos):
                    starpos[i, :] = locate_star(image=self.m_image_in_port[i, ],
                                                center=center,
                                                width=width,
                                                fwhm=int(math.ceil(fwhm/pixscale)))

            return starpos

        def _photometry(images, starpos, aperture):
            check_pos_in = any(np.floor(starpos[:]-aperture[1]) < 0.)
            check_pos_out = any(np.ceil(starpos[:]+aperture[1]) > images.shape[0])

            if check_pos_in or check_pos_out:
                phot = np.nan

            else:
                im_crop = crop_image(images, tuple(starpos), 2*int(math.ceil(aperture[1])))

                npix = im_crop.shape[0]

                x_grid = y_grid = np.linspace(-(npix-1)/2, (npix-1)/2, npix)
                xx_grid, yy_grid = np.meshgrid(x_grid, y_grid)
                rr_grid = np.sqrt(xx_grid*xx_grid+yy_grid*yy_grid)

                if self.m_aperture[0] == 'circular':
                    phot = np.sum(im_crop[rr_grid < aperture[1]])

                elif self.m_aperture[0] == 'annulus':
                    phot = np.sum(im_crop[(rr_grid > aperture[0]) & (rr_grid < aperture[1])])

                elif self.m_aperture[0] == 'ratio':
                    phot = np.sum(im_crop[rr_grid < aperture[0]]) / \
                        np.sum(im_crop[(rr_grid > aperture[0]) & (rr_grid < aperture[1])])

            return phot

        self._initialize()

        pixscale = self.m_image_in_port.get_attribute('PIXSCALE')
        nimages = self.m_image_in_port.get_shape()[0]

        aperture = _get_aperture(self.m_aperture)
        starpos = _get_starpos(self.m_fwhm, self.m_position)

        phot = np.zeros(nimages)

        start_time = time.time()
        for i in range(nimages):
            progress(i, nimages, 'Running FrameSelectionModule...', start_time)

            images = self.m_image_in_port[i]
            phot[i] = _photometry(images, starpos[i, :], aperture)

        if self.m_method == 'median':
            phot_ref = np.nanmedian(phot)
        elif self.m_method == 'max':
            phot_ref = np.nanmax(phot)

        phot_std = np.nanstd(phot)

        index_rm = np.logical_or((phot > phot_ref+self.m_threshold*phot_std),
                                 (phot < phot_ref-self.m_threshold*phot_std))

        index_rm[np.isnan(phot)] = True

        indices = np.where(index_rm)[0]
        indices = np.asarray(indices, dtype=np.int)

        if np.size(indices) > 0:
            memory = self._m_config_port.get_attribute('MEMORY')
            frames = memory_frames(memory, nimages)

            if memory == 0 or memory >= nimages:
                memory = nimages

            for i, _ in enumerate(frames[:-1]):
                images = self.m_image_in_port[frames[i]:frames[i+1], ]

                index_del = np.where(np.logical_and(indices >= frames[i],
                                                    indices < frames[i+1]))

                write_selected_data(images,
                                    indices[index_del] % memory,
                                    self.m_selected_out_port,
                                    self.m_removed_out_port)

        else:
            warnings.warn('No frames were removed.')

        history = 'frames removed = '+str(np.size(indices))

        if self.m_index_out_port is not None:
            self.m_index_out_port.set_all(np.transpose(indices))
            self.m_index_out_port.copy_attributes(self.m_image_in_port)
            self.m_index_out_port.add_attribute('STAR_POSITION', starpos, static=False)
            self.m_index_out_port.add_history('FrameSelectionModule', history)

        if self.m_selected_out_port is not None:
            # Copy attributes before write_selected_attributes is used
            self.m_selected_out_port.copy_attributes(self.m_image_in_port)

        if self.m_removed_out_port is not None:
            # Copy attributes before write_selected_attributes is used
            self.m_removed_out_port.copy_attributes(self.m_image_in_port)

        write_selected_attributes(indices,
                                  self.m_image_in_port,
                                  self.m_selected_out_port,
                                  self.m_removed_out_port)

        if self.m_selected_out_port is not None:
            indices_select = np.ones(nimages, dtype=bool)
            indices_select[indices] = False
            indices_select = np.where(indices_select)

            self.m_selected_out_port.add_attribute('STAR_POSITION',
                                                   starpos[indices_select],
                                                   static=False)

            self.m_selected_out_port.add_history('FrameSelectionModule', history)

        if self.m_removed_out_port is not None:
            self.m_removed_out_port.add_attribute('STAR_POSITION',
                                                  starpos[indices],
                                                  static=False)

            self.m_removed_out_port.add_history('FrameSelectionModule', history)

        sys.stdout.write('Running FrameSelectionModule... [DONE]\n')
        sys.stdout.flush()

        self.m_image_in_port.close_port()
示例#7
0
    def run(self) -> None:
        """
        Run method of the module. Selects images according to a specified attribute tag and
        ordering, e.g. the highest 150 ``INDEX`` frames, or the lowest 50 ``PCC`` frames.

        Returns
        -------
        NoneType
            None
        """

        if self.m_selected_out_port is not None:
            self.m_selected_out_port.del_all_data()
            self.m_selected_out_port.del_all_attributes()

        if self.m_removed_out_port is not None:
            self.m_removed_out_port.del_all_data()
            self.m_removed_out_port.del_all_attributes()

        images = self.m_image_in_port.get_all()
        nimages = images.shape[0]

        attribute = self.m_image_in_port.get_attribute(f'{self.m_attribute_tag}')

        if nimages != len(attribute):
            raise ValueError(f'The attribute {{self.m_attribute_tag}} does not have the same '
                             f'length ({len(attribute)}) as the tag has images ({nimages}). '
                             f'Please check the attribute you have chosen for selection.')

        index = self.m_image_in_port.get_attribute('INDEX')

        if self.m_order == 'descending':
            # sort attribute in descending order
            sorting_order = np.argsort(attribute)[::-1]
        else:
            # sort attribute in ascending order
            sorting_order = np.argsort(attribute)

        attribute = attribute[sorting_order]
        index = index[sorting_order]

        indices = index[:self.m_number_frames]
        # copied from FrameSelectionModule ...
        # possibly refactor to @staticmethod or move to util.remove
        start_time = time.time()
        if np.size(indices) > 0:
            memory = self._m_config_port.get_attribute('MEMORY')
            frames = memory_frames(memory, nimages)

            if memory == 0 or memory >= nimages:
                memory = nimages

            for i, _ in enumerate(frames[:-1]):
                images = self.m_image_in_port[frames[i]:frames[i+1], ]

                index_del = np.where(np.logical_and(indices >= frames[i],
                                                    indices < frames[i+1]))

                write_selected_data(images,
                                    indices[index_del] % memory,
                                    self.m_removed_out_port,
                                    self.m_selected_out_port)

                progress(i, len(frames[:-1]), 'Running SelectByAttributeModule...', start_time)

        else:
            warnings.warn('No frames were removed.')

        if self.m_selected_out_port is not None:
            # Copy attributes before write_selected_attributes is used
            self.m_selected_out_port.copy_attributes(self.m_image_in_port)

        if self.m_removed_out_port is not None:
            # Copy attributes before write_selected_attributes is used
            self.m_removed_out_port.copy_attributes(self.m_image_in_port)

        # write the selected and removed data to the respective output ports
        write_selected_attributes(indices,
                                  self.m_image_in_port,
                                  self.m_removed_out_port,
                                  self.m_selected_out_port)

        sys.stdout.write('Running SelectByAttributeModule... [DONE]\n')
        sys.stdout.flush()
示例#8
0
    def run(self) -> None:
        """
        Run method of the module. Smooths the images with a Gaussian
        kernel, locates the brightest pixel in each image, measures the
        integrated flux around the brightest pixel, calculates the
        median and standard deviation of the photometry, and applies
        sigma clipping to remove low quality images.

        Returns
        -------
        NoneType
            None
        """

        pixscale = self.m_image_in_port.get_attribute('PIXSCALE')
        nimages = self.m_image_in_port.get_shape()[0]

        if self.m_fwhm is not None:
            self.m_fwhm = int(math.ceil(self.m_fwhm / pixscale))

        if self.m_position is not None and self.m_position[2] is not None:
            self.m_position = (self.m_position[0], self.m_position[0],
                               int(math.ceil(self.m_position[2] / pixscale)))

        if len(self.m_aperture) == 2:
            self.m_aperture = (self.m_aperture[0], None,
                               self.m_aperture[1] / pixscale)

        elif len(self.m_aperture) == 3:
            self.m_aperture = (self.m_aperture[0],
                               self.m_aperture[1] / pixscale,
                               self.m_aperture[2] / pixscale)

        starpos = star_positions(self.m_image_in_port, self.m_fwhm,
                                 self.m_position)

        phot = np.zeros(nimages)
        start_time = time.time()

        for i in range(nimages):
            progress(i, nimages, 'Aperture photometry...', start_time)

            phot[i] = self.aperture_phot(self.m_image_in_port[i, ],
                                         starpos[i, :], self.m_aperture)

        if self.m_method == 'median':
            phot_ref = np.nanmedian(phot)
            print(f'Median = {phot_ref:.2f}')

        elif self.m_method == 'max':
            phot_ref = np.nanmax(phot)
            print(f'Maximum = {phot_ref:.2f}')

        elif self.m_method == 'range':
            phot_ref = np.nanmedian(phot)
            print(f'Median = {phot_ref:.2f}')

        phot_std = np.nanstd(phot)
        print(f'Standard deviation = {phot_std:.2f}')

        if self.m_method in ['median', 'max']:
            index_del = np.logical_or(
                (phot > phot_ref + self.m_threshold * phot_std),
                (phot < phot_ref - self.m_threshold * phot_std))

        elif self.m_method == 'range':
            index_del = np.logical_or((phot < self.m_threshold[0]),
                                      (phot > self.m_threshold[1]))

        index_del[np.isnan(phot)] = True
        index_del = np.where(index_del)[0]

        index_sel = np.ones(nimages, dtype=bool)
        index_sel[index_del] = False

        write_selected_data(memory=self._m_config_port.get_attribute('MEMORY'),
                            indices=index_del,
                            image_in_port=self.m_image_in_port,
                            selected_out_port=self.m_selected_out_port,
                            removed_out_port=self.m_removed_out_port)

        history = f'frames removed = {np.size(index_del)}'

        if self.m_index_out_port is not None:
            self.m_index_out_port.set_all(index_del, data_dim=1)
            self.m_index_out_port.copy_attributes(self.m_image_in_port)
            self.m_index_out_port.add_attribute('STAR_POSITION',
                                                starpos,
                                                static=False)
            self.m_index_out_port.add_history('FrameSelectionModule', history)

        write_selected_attributes(indices=index_del,
                                  image_in_port=self.m_image_in_port,
                                  selected_out_port=self.m_selected_out_port,
                                  removed_out_port=self.m_removed_out_port,
                                  module_type='FrameSelectionModule',
                                  history=history)

        self.m_selected_out_port.add_attribute('STAR_POSITION',
                                               starpos[index_sel],
                                               static=False)
        self.m_selected_out_port.add_history('FrameSelectionModule', history)

        self.m_removed_out_port.add_attribute('STAR_POSITION',
                                              starpos[index_del],
                                              static=False)
        self.m_removed_out_port.add_history('FrameSelectionModule', history)

        self.m_image_in_port.close_port()
示例#9
0
    def run(self) -> None:
        """
        Run method of the module. Removes the frames and corresponding attributes, updates the
        NFRAMES attribute, and saves the data and attributes.

        Returns
        -------
        NoneType
            None
        """

        self.m_selected_out_port.del_all_data()
        self.m_selected_out_port.del_all_attributes()

        self.m_removed_out_port.del_all_data()
        self.m_removed_out_port.del_all_attributes()

        if self.m_index_in_port is not None:
            self.m_frames = self.m_index_in_port.get_all()

        if np.size(
                np.where(
                    self.m_frames >= self.m_image_in_port.get_shape()[0])) > 0:
            raise ValueError(
                f'Some values in \'frames\' are larger than the total number of '
                f'available frames, {self.m_image_in_port.get_shape()[0]}')

        memory = self._m_config_port.get_attribute('MEMORY')

        nimages = self.m_image_in_port.get_shape()[0]
        frames = memory_frames(memory, nimages)

        if memory == 0 or memory >= nimages:
            memory = nimages

        start_time = time.time()

        for i, _ in enumerate(frames[:-1]):
            progress(i, len(frames[:-1]), 'Removing images...', start_time)

            images = self.m_image_in_port[frames[i]:frames[i + 1], ]

            index_del = np.where(
                np.logical_and(self.m_frames >= frames[i],
                               self.m_frames < frames[i + 1]))

            write_selected_data(images, self.m_frames[index_del] % memory,
                                self.m_selected_out_port,
                                self.m_removed_out_port)

        history = f'frames removed = {np.size(self.m_frames)}'

        if self.m_selected_out_port is not None:
            # Copy attributes before write_selected_attributes is used
            self.m_selected_out_port.copy_attributes(self.m_image_in_port)
            self.m_selected_out_port.add_history('RemoveFramesModule', history)

        if self.m_removed_out_port is not None:
            # Copy attributes before write_selected_attributes is used
            self.m_removed_out_port.copy_attributes(self.m_image_in_port)
            self.m_removed_out_port.add_history('RemoveFramesModule', history)

        write_selected_attributes(self.m_frames, self.m_image_in_port,
                                  self.m_selected_out_port,
                                  self.m_removed_out_port)

        self.m_image_in_port.close_port()
示例#10
0
    def run(self) -> None:
        """
        Run method of the module. Smooths the images with a Gaussian kernel, locates the brightest
        pixel in each image, measures the integrated flux around the brightest pixel, calculates
        the median and standard deviation of the photometry, and applies sigma clipping to remove
        low quality images.

        Returns
        -------
        NoneType
            None
        """

        self.m_selected_out_port.del_all_data()
        self.m_selected_out_port.del_all_attributes()

        self.m_removed_out_port.del_all_data()
        self.m_removed_out_port.del_all_attributes()

        if self.m_index_out_port is not None:
            self.m_index_out_port.del_all_data()
            self.m_index_out_port.del_all_attributes()

        pixscale = self.m_image_in_port.get_attribute('PIXSCALE')
        nimages = self.m_image_in_port.get_shape()[0]

        self.m_fwhm = int(math.ceil(self.m_fwhm / pixscale))

        if self.m_position is not None and self.m_position[2] is not None:
            self.m_position = (self.m_position[0], self.m_position[0],
                               int(math.ceil(self.m_position[2] / pixscale)))

        if len(self.m_aperture) == 2:
            self.m_aperture = (self.m_aperture[0], None,
                               self.m_aperture[1] / pixscale)

        elif len(self.m_aperture) == 3:
            self.m_aperture = (self.m_aperture[0],
                               self.m_aperture[1] / pixscale,
                               self.m_aperture[2] / pixscale)

        starpos = star_positions(self.m_image_in_port, self.m_fwhm,
                                 self.m_position)

        phot = np.zeros(nimages)
        start_time = time.time()

        for i in range(nimages):
            progress(i, nimages, 'Aperture photometry...', start_time)

            phot[i] = self.photometry(self.m_image_in_port[i, ], starpos[i, :],
                                      self.m_aperture)

        if self.m_method == 'median':
            phot_ref = np.nanmedian(phot)
            print(f'Median = {phot_ref:.2f}')

        elif self.m_method == 'max':
            phot_ref = np.nanmax(phot)
            print(f'Maximum = {phot_ref:.2f}')

        phot_std = np.nanstd(phot)
        print(f'Standard deviation = {phot_std:.2f}')

        index_rm = np.logical_or(
            (phot > phot_ref + self.m_threshold * phot_std),
            (phot < phot_ref - self.m_threshold * phot_std))

        index_rm[np.isnan(phot)] = True
        indices = np.asarray(np.where(index_rm)[0], dtype=np.int)

        if np.size(indices) > 0:
            memory = self._m_config_port.get_attribute('MEMORY')
            frames = memory_frames(memory, nimages)

            if memory == 0 or memory >= nimages:
                memory = nimages

            start_time = time.time()

            for i, _ in enumerate(frames[:-1]):
                progress(i, len(frames[:-1]), 'Writing selected data...',
                         start_time)

                index_del = np.where(
                    np.logical_and(indices >= frames[i],
                                   indices < frames[i + 1]))

                write_selected_data(
                    images=self.m_image_in_port[frames[i]:frames[i + 1], ],
                    indices=indices[index_del] % memory,
                    port_selected=self.m_selected_out_port,
                    port_removed=self.m_removed_out_port)

        else:
            warnings.warn('No frames were removed.')

        history = f'frames removed = {np.size(indices)}'

        if self.m_index_out_port is not None:
            self.m_index_out_port.set_all(np.transpose(indices))
            self.m_index_out_port.copy_attributes(self.m_image_in_port)
            self.m_index_out_port.add_attribute('STAR_POSITION',
                                                starpos,
                                                static=False)
            self.m_index_out_port.add_history('FrameSelectionModule', history)

        # Copy attributes before write_selected_attributes is used
        self.m_selected_out_port.copy_attributes(self.m_image_in_port)

        # Copy attributes before write_selected_attributes is used
        self.m_removed_out_port.copy_attributes(self.m_image_in_port)

        write_selected_attributes(indices, self.m_image_in_port,
                                  self.m_selected_out_port,
                                  self.m_removed_out_port)

        indices_select = np.ones(nimages, dtype=bool)
        indices_select[indices] = False
        indices_select = np.where(indices_select)

        self.m_selected_out_port.add_attribute('STAR_POSITION',
                                               starpos[indices_select],
                                               static=False)

        self.m_selected_out_port.add_history('FrameSelectionModule', history)

        self.m_removed_out_port.add_attribute('STAR_POSITION',
                                              starpos[indices],
                                              static=False)

        self.m_removed_out_port.add_history('FrameSelectionModule', history)

        self.m_image_in_port.close_port()