Beispiel #1
0
    def _images_init(self):
        memory = self._m_config_port.get_attribute("MEMORY")

        ndim_image = self.m_image_in_port.get_ndim()
        ndim_psf = self.m_psf_in_port.get_ndim()

        if ndim_image == 3:
            nimages = self.m_image_in_port.get_shape()[0]

            im_size = (self.m_image_in_port.get_shape()[1],
                       self.m_image_in_port.get_shape()[2])

            frames = memory_frames(memory, nimages)

        else:
            raise ValueError(
                "The image_in_tag should contain a cube of images.")

        if ndim_psf == 2:
            npsf = 1
            psf_size = (self.m_psf_in_port.get_shape()[0],
                        self.m_psf_in_port.get_shape()[1])

        elif ndim_psf == 3:
            npsf = self.m_psf_in_port.get_shape()[0]
            psf_size = (self.m_psf_in_port.get_shape()[1],
                        self.m_psf_in_port.get_shape()[2])

        if psf_size != im_size:
            raise ValueError("The images in '" + self.m_image_in_port.tag +
                             "' should have the same "
                             "dimensions as the images images in '" +
                             self.m_psf_in_port.tag + "'.")

        if ndim_psf == 3 and npsf == 1:
            psf = np.squeeze(self.m_psf_in_port.get_all(), axis=0)
            ndim_psf = psf.ndim

        elif ndim_psf == 2:
            psf = self.m_psf_in_port.get_all()

        elif ndim_psf == 3 and nimages != npsf:
            psf = np.zeros((self.m_psf_in_port.get_shape()[1],
                            self.m_psf_in_port.get_shape()[2]))

            frames_psf = memory_frames(memory, npsf)

            for i, _ in enumerate(frames_psf[:-1]):
                psf += np.sum(self.m_psf_in_port[frames_psf[i]:frames_psf[i +
                                                                          1]],
                              axis=0)

            psf /= float(npsf)

            ndim_psf = psf.ndim

        elif ndim_psf == 3 and nimages == npsf:
            psf = None

        return frames, psf, ndim_psf
Beispiel #2
0
        def _stack(nimages, im_shape, parang):
            im_new = None
            parang_new = None

            if self.m_stacking is not None:
                frames = memory_frames(self.m_stacking, nimages)

                nimages_new = np.size(frames) - 1
                if parang is not None:
                    parang_new = np.zeros(nimages_new)
                im_new = np.zeros((nimages_new, im_shape[1], im_shape[2]))

                for i in range(nimages_new):
                    progress(i, nimages_new, "Running StackAndSubsetModule...")

                    if parang is not None:
                        parang_new[i] = np.mean(parang[frames[i]:frames[i +
                                                                        1]])
                    im_new[i, ] = np.mean(
                        self.m_image_in_port[frames[i]:frames[i + 1], ],
                        axis=0)

                im_shape = im_new.shape

            else:
                if parang is not None:
                    parang_new = np.copy(parang)

            return im_shape, im_new, parang_new
Beispiel #3
0
    def run(self):
        """
        Run method of the module. Subtracts the images from the second database tag from the images
        of the first database tag, on a frame-by-frame basis.

        :return: None
        """

        self.m_image_out_port.del_all_attributes()
        self.m_image_out_port.del_all_data()

        if self.m_image_in1_port.get_shape() != self.m_image_in2_port.get_shape():
            raise ValueError("The shape of the two input tags have to be equal.")

        memory = self._m_config_port.get_attribute("MEMORY")
        nimages = self.m_image_in1_port.get_shape()[0]

        frames = memory_frames(memory, nimages)

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

            images1 = self.m_image_in1_port[frames[i]:frames[i+1], ]
            images2 = self.m_image_in2_port[frames[i]:frames[i+1], ]

            self.m_image_out_port.append(images1-images2)

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

        self.m_image_out_port.add_history_information("Images subtracted", "")
        self.m_image_out_port.copy_attributes_from_input_port(self.m_image_in1_port)
        self.m_image_out_port.close_port()
Beispiel #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 = self.m_image_in_port.get_shape()[0]

        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]))

            if np.size(index_del) > 0:
                if self.m_removed_out_port is not None:
                    self.m_removed_out_port.append(
                        images[self.m_frames[index_del] % memory])

                images = np.delete(images,
                                   self.m_frames[index_del] % memory,
                                   axis=0)

            if self.m_selected_out_port is not None:
                self.m_selected_out_port.append(images)

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

        self._write_attributes()

        if self.m_selected_out_port is not None:
            self.m_selected_out_port.add_history_information(
                "Frames removed", str(np.size(self.m_frames)))

        if self.m_removed_out_port is not None:
            self.m_removed_out_port.add_history_information(
                "Frames removed", str(np.size(self.m_frames)))

        self.m_image_in_port.close_port()
Beispiel #5
0
        def _initialize():
            if self.m_radius is not None:
                self.m_radius /= pixscale

            ndim = self.m_image_in_port.get_ndim()

            if ndim == 2:
                nimages = 1
            elif ndim == 3:
                nimages = self.m_image_in_port.get_shape()[0]

            npix = self.m_image_in_port.get_shape()[1]

            if npix/2.+self.m_guess[0]+self.m_radius > npix or \
                    npix/2.+self.m_guess[1]+self.m_radius > npix or \
                    npix/2.+self.m_guess[1]-self.m_radius < 0. or \
                    npix/2.+self.m_guess[1]-self.m_radius < 0.:
                raise ValueError(
                    "Mask radius extends beyond the size of the image.")

            frames = memory_frames(memory, nimages)

            return ndim, nimages, npix, frames
Beispiel #6
0
        def _initialize():
            """
            Internal function to get the number of dimensions and subdivide the images by the
            MEMORY attribute.

            :return: Number of dimensions and array with subdivision of the images.
            :rtype: int, numpy.ndarray
            """

            memory = self._m_config_port.get_attribute("MEMORY")
            ndim = image_in_port.get_ndim()

            if ndim == 2:
                nimages = 1
            elif ndim == 3:
                nimages = image_in_port.get_shape()[0]

            if image_out_port is not None and image_out_port.tag != image_in_port.tag:
                image_out_port.del_all_attributes()
                image_out_port.del_all_data()

            frames = memory_frames(memory, nimages)

            return ndim, frames
Beispiel #7
0
    def run(self):
        """
        Run method of the module. Creates a PCA basis set of the background frames, masks the PSF
        in the star frames and optionally an off-axis point source, fits the star frames with a
        linear combination of the principle components, and writes the residuals of the background
        subtracted images.

        :return: None
        """

        def _create_mask(radius, position, nimages):
            """
            Method for creating a circular mask at the star or planet position.
            """

            npix = self.m_star_in_port.get_shape()[1]

            x_grid = np.arange(0, npix, 1)
            y_grid = np.arange(0, npix, 1)

            xx_grid, yy_grid = np.meshgrid(x_grid, y_grid)

            mask = np.ones((nimages, npix, npix))

            cent_x = position[:, 1]
            cent_y = position[:, 0]

            for i in range(nimages):
                rr_grid = np.sqrt((xx_grid - cent_x[i])**2 + (yy_grid - cent_y[i])**2)
                mask[i, ][rr_grid < radius] = 0.

            return mask

        def _create_basis(images, pca_number):
            """
            Method for creating a set of principle components for a stack of images.
            """

            _, _, v_svd = svds(images.reshape(images.shape[0],
                                              images.shape[1]*images.shape[2]),
                               k=pca_number)

            v_svd = v_svd[::-1, ]

            pca_basis = v_svd.reshape(v_svd.shape[0], images.shape[1], images.shape[2])

            return pca_basis

        def _model_background(basis, im_arr, mask):
            """
            Method for creating a model of the background.
            """

            def _dot_product(x_dot, *p):
                return np.dot(p, x_dot)

            fit_im_chi = np.zeros(im_arr.shape)
            # fit_coeff_chi = np.zeros((im_arr.shape[0], basis.shape[0]))

            basis_reshaped = basis.reshape(basis.shape[0], -1)

            for i in xrange(im_arr.shape[0]):
                basis_reshaped_masked = (basis*mask[i]).reshape(basis.shape[0], -1)

                data_to_fit = im_arr[i, ]

                init = np.ones(basis_reshaped_masked.shape[0])

                fitted = curve_fit(_dot_product,
                                   basis_reshaped_masked,
                                   data_to_fit.reshape(-1),
                                   init)

                fit_im = np.dot(fitted[0], basis_reshaped)
                fit_im = fit_im.reshape(data_to_fit.shape[0], data_to_fit.shape[1])

                fit_im_chi[i, ] = fit_im
                # fit_coeff_chi[i, ] = fitted[0]

            return fit_im_chi

        self.m_residuals_out_port.del_all_data()
        self.m_residuals_out_port.del_all_attributes()

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

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

        memory = self._m_config_port.get_attribute("MEMORY")
        pixscale = self.m_star_in_port.get_attribute("PIXSCALE")
        star = self.m_star_in_port.get_attribute("STAR_POSITION")

        self.m_mask_star /= pixscale

        if self.m_mask_planet is not None:
            parang = self.m_star_in_port.get_attribute("PARANG")

            self.m_mask_planet = np.asarray(self.m_mask_planet)

            self.m_mask_planet[0] /= pixscale
            self.m_mask_planet[3] /= pixscale

        sys.stdout.write("Creating PCA basis set...")
        sys.stdout.flush()

        basis_pca = _create_basis(self.m_background_in_port.get_all(),
                                  self.m_pca_number)

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

        nimages = self.m_star_in_port.get_shape()[0]

        frames = memory_frames(memory, nimages)

        for i, _ in enumerate(frames[:-1]):
            progress(i, len(frames[:-1]), "Calculating background model...")

            im_star = self.m_star_in_port[frames[i]:frames[i+1], ]

            mask_star = _create_mask(self.m_mask_star,
                                     star[frames[i]:frames[i+1], ],
                                     frames[i+1]-frames[i])

            if self.m_mask_planet is None:
                mask_planet = np.ones(im_star.shape)

            else:
                cent_x = star[frames[i]:frames[i+1], 1]
                cent_y = star[frames[i]:frames[i+1], 0]

                theta = np.radians(self.m_mask_planet[1] + 90. - \
                            parang[frames[i]:frames[i+1]] + self.m_mask_planet[2])

                x_planet = self.m_mask_planet[0]*np.cos(theta) + cent_x
                y_planet = self.m_mask_planet[0]*np.sin(theta) + cent_y

                planet = np.stack((y_planet, x_planet))

                mask_planet = _create_mask(self.m_mask_planet[3],
                                           np.transpose(planet),
                                           frames[i+1]-frames[i])

            fit_im = _model_background(basis_pca,
                                       im_star*mask_star*mask_planet,
                                       mask_star*mask_planet)

            self.m_residuals_out_port.append(im_star-fit_im)

            if self.m_fit_out_port is not None:
                self.m_fit_out_port.append(fit_im)

            if self.m_mask_out_port is not None:
                self.m_mask_out_port.append(mask_star*mask_planet)

        sys.stdout.write("Calculating background model... [DONE]\n")
        sys.stdout.flush()

        self.m_residuals_out_port.copy_attributes_from_input_port(self.m_star_in_port)
        self.m_residuals_out_port.add_history_information("Background subtraction", "PCA")

        if self.m_fit_out_port is not None:
            self.m_fit_out_port.copy_attributes_from_input_port(self.m_star_in_port)
            self.m_fit_out_port.add_history_information("Background subtraction", "PCA")

        if self.m_mask_out_port is not None:
            self.m_mask_out_port.copy_attributes_from_input_port(self.m_star_in_port)
            self.m_mask_out_port.add_history_information("Background subtraction", "PCA")

        self.m_residuals_out_port.close_port()
Beispiel #8
0
    def run(self):
        """
        Run method of the module. Combines the frames of multiple tags into a single output tag
        and adds the static and non-static attributes. The values of the attributes are compared
        between the input tags to make sure that the input tags decent from the same data set.

        :return: None
        """

        self.m_image_out_port.del_all_data()
        self.m_image_out_port.del_all_attributes()

        if len(self.m_image_in_tags) < 2:
            raise ValueError(
                "The tuple of image_in_tags should contain at least two tags.")

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

        for i, item in enumerate(self.m_image_in_tags):
            progress(i, len(self.m_image_in_tags),
                     "Running CombineTagsModule...")

            image_in_port = self.add_input_port(item)
            nimages = image_in_port.get_shape()[0]

            frames = memory_frames(memory, nimages)

            for j, _ in enumerate(frames[:-1]):
                im_tmp = image_in_port[frames[j]:frames[j + 1], ]
                self.m_image_out_port.append(im_tmp)

            static_attr = image_in_port.get_all_static_attributes()
            non_static_attr = image_in_port.get_all_non_static_attributes()

            for key in static_attr:
                status = self.m_image_out_port.check_static_attribute(
                    key, static_attr[key])

                if status == 1:
                    self.m_image_out_port.add_attribute(key,
                                                        static_attr[key],
                                                        static=True)

                elif status == -1 and key[0:7] != "History":
                    warnings.warn(
                        'The static keyword %s is already used but with a different '
                        'value. It is advisable to only combine tags that descend from '
                        'the same data set.' % key)

            for key in non_static_attr:
                values = image_in_port.get_attribute(key)
                status = self.m_image_out_port.check_non_static_attribute(
                    key, values)

                if self.m_check_attr:
                    if key == "PARANG" or key == "STAR_POSITION" or key == "INDEX":
                        if status == 1:
                            self.m_image_out_port.add_attribute(key,
                                                                values,
                                                                static=False)

                        else:
                            for j in values:
                                self.m_image_out_port.append_attribute_data(
                                    key, j)

                    elif key == "NFRAMES":
                        continue

                    else:
                        if status == 1:
                            self.m_image_out_port.add_attribute(key,
                                                                values,
                                                                static=False)

                        if status == -1:
                            warnings.warn(
                                'The non-static keyword %s is already used but with '
                                'different values. It is advisable to only combine tags '
                                'that descend from the same data set.' % key)

                else:
                    if status == 1:
                        self.m_image_out_port.add_attribute(key,
                                                            values,
                                                            static=False)

                    else:
                        for j in values:
                            self.m_image_out_port.append_attribute_data(key, j)

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

        self.m_image_out_port.add_history_information(
            "Database entries combined", str(np.size(self.m_image_in_tags)))

        self.m_image_out_port.close_port()
Beispiel #9
0
    def run(self):
        """
        Run method of the module. Uses the PARANG attributes to derotate the images and applies
        an optional mean stacking afterwards.

        :return: None
        """
        def _derotate(frames, im_tot, parang, count):
            for j in range(frames[count + 1] - frames[count]):
                im_rot = rotate(
                    input=self.m_image_in_port[frames[count] + j, ],
                    angle=-parang[frames[count] + j] + self.m_extra_rot,
                    reshape=False)

                if self.m_stack:
                    im_tot += im_rot

                elif not self.m_stack:
                    if ndim == 2:
                        self.m_image_out_port.set_all(im_rot)
                    elif ndim == 3:
                        self.m_image_out_port.append(im_rot, data_dim=3)

            return im_tot

        def _stack(frames, im_tot, count):
            im_tmp = self.m_image_in_port[frames[count]:frames[count + 1], ]

            if im_tmp.ndim == 2:
                im_tot += im_tmp
            elif im_tmp.ndim == 3:
                im_tot += np.sum(im_tmp, axis=0)

            return 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]

        if ndim == 2:
            nimages = 1
        elif ndim == 3:
            nimages = self.m_image_in_port.get_shape()[0]

        frames = memory_frames(memory, nimages)

        if self.m_stack:
            im_tot = np.zeros((npix, npix))
        else:
            im_tot = None

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

            if self.m_derotate:
                im_tot = _derotate(frames, im_tot, parang, i)

            else:
                if self.m_stack:
                    im_tot = _stack(frames, im_tot, i)

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

        if self.m_stack:
            self.m_image_out_port.set_all(im_tot / float(nimages))

        if self.m_derotate or self.m_stack:
            self.m_image_out_port.copy_attributes_from_input_port(
                self.m_image_in_port)

        self.m_image_out_port.close_port()
Beispiel #10
0
    def run(self):
        """
        Run method of the module. Sorts the images and relevant non-static attributes.

        :return: None
        """

        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")
        index = self.m_image_in_port.get_attribute("INDEX")

        index_new = np.zeros(index.shape, dtype=np.int)

        if "PARANG" in self.m_image_in_port.get_all_non_static_attributes():
            parang = self.m_image_in_port.get_attribute("PARANG")
            parang_new = np.zeros(parang.shape)

        else:
            parang_new = None

        if "STAR_POSITION" in self.m_image_in_port.get_all_non_static_attributes(
        ):
            star = self.m_image_in_port.get_attribute("STAR_POSITION")
            star_new = np.zeros(star.shape)

        else:
            star_new = None

        index_sort = np.argsort(index)

        nimages = self.m_image_in_port.get_shape()[0]

        frames = memory_frames(memory, nimages)

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

            index_new[frames[i]:frames[i + 1]] = index[
                index_sort[frames[i]:frames[i + 1]]]

            if parang_new is not None:
                parang_new[frames[i]:frames[i + 1]] = parang[
                    index_sort[frames[i]:frames[i + 1]]]

            if star_new is not None:
                star_new[frames[i]:frames[i + 1]] = star[
                    index_sort[frames[i]:frames[i + 1]]]

            # h5py indexing elements must be in increasing order
            for _, item in enumerate(index_sort[frames[i]:frames[i + 1]]):
                self.m_image_out_port.append(self.m_image_in_port[item, ],
                                             data_dim=3)

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

        self.m_image_out_port.copy_attributes_from_input_port(
            self.m_image_in_port)

        self.m_image_out_port.add_attribute("INDEX", index_new, static=False)

        if parang_new is not None:
            self.m_image_out_port.add_attribute("PARANG",
                                                parang_new,
                                                static=False)

        if star_new is not None:
            self.m_image_out_port.add_attribute("STAR_POSITION",
                                                star_new,
                                                static=False)

        if "NFRAMES" in self.m_image_in_port.get_all_non_static_attributes():
            self.m_image_out_port.del_attribute("NFRAMES")

        self.m_image_out_port.add_history_information("Images sorted",
                                                      "parang")
        self.m_image_out_port.close_port()