def _construct_secondary_album(self, in_album):
        """
        function constructs the secondary album, now it will contain new Gaussians for the second phase of running
        """
        assert self.in_album != None

        # album and PSF initialisation
        album = astrohack_projections.album_and_model()
        psf = astrohack_projections.mixture_of_gaussians(2)
        psf.add_gaussian(1., numpy.array([0., 0.]), numpy.eye(2)*1.)

        # construct the galaxy using Gaussian mixture
        gal_model = self.in_album.galaxy
        basevar = 0.5 * numpy.eye(3)
        for i in xrange(self.iter_gaus_num):
            v = numpy.random.uniform(0, 10, size=3)
            mu = numpy.random.uniform(-3, 3, size=3)
            gal_model.add_gaussian(1.0, mu, basevar + numpy.outer(v, v))

        # add all images to the album
        for i in xrange(self.image_parameters['num']):
            old_image = self.in_album.get_all_images()[i]
            data = old_image.get_data()
            
            image = astrohack_projections.image_and_model()
            image.set_shape(old_image.get_shape())
            image.set_psf(psf)
            image.set_parameters_from_vector(old_image.get_parameters_vector())
            image.set_galaxy(gal_model)
            image.set_ivar(old_image.get_ivar())
            image.set_data(data)
            album.add_image(image)
        album.set_galaxy(gal_model)
        self.albums[len(album.galaxy.alphas)] = album
        return album
def construct_initial_album(illustris_images, psf_blur_size=None):
    """
    the initial album contains 32 images of the illustris galaxy,
    the images will be a little blured so that 3 Gaussians will be sufficient for the fitting process.
    the album will contain a galaxy_3d_model that contains 3 Gaussians
    """
    num_of_images = len(illustris_images)

    # album and PSF initialisation
    album = astrohack_projections.album_and_model()
    psf = astrohack_projections.mixture_of_gaussians(2)
    psf.add_gaussian(1., numpy.array([0., 0.]), numpy.eye(2)*1.)

    # construct the galaxy using Gaussian mixture
    basevar = 0.5 * numpy.eye(3)
    gal_model = astrohack_projections.galaxy_model_3d()
    for i in xrange(3):
        v = numpy.random.uniform(0, 3, size=3)
        mu = numpy.random.uniform(-3, 3, size=3)
        gal_model.add_gaussian(1.0, mu, basevar + numpy.outer(v, v))

    # add all images to the album
    for i in xrange(num_of_images):
        if psf_blur_size != None:
            data = gaussian_filter(illustris_images[i], psf_blur_size)
        else:
            data = illustris_images[i]

        # projection parameters
        xi_hat, eta_hat = astrohack_projections.choose_random_projection()
        alpha, beta, gamma = numpy.random.uniform(0.0, 360.0, 3)
        intensity = 20 #150 #
        scale = 0.5 * numpy.exp(numpy.random.uniform()) #0.18 * numpy.exp(numpy.random.uniform()) #
        xshift = numpy.random.uniform(29.5, 31.)#(13., 16.)#
        yshift = numpy.random.uniform(39.5, 41.)#(18., 21.)#
        bg = 0.
        
        image = astrohack_projections.image_and_model()
        image.set_shape((60, 80))
        image.set_psf(psf)
        scale = 0.3 #0.5
        kwargs = {'alpha':alpha, 'beta':beta, 'gamma':gamma, 'intensity':intensity, 'scale':scale, 'xshift': xshift, 'yshift': yshift, 'bg':0.0}
        image.set_parameters(**kwargs)
        image.set_galaxy(gal_model)
        image.set_ivar(numpy.ones(data.shape))
        image.set_data(data + numpy.random.normal(size=data.shape)/ numpy.sqrt(image.ivar))
        # album
        album.add_image(image)

    return album
    def _construct_initial_album(self):
        """
        construct the initial album with which the fitting begins,
        the album is constrcuted based on the same image parameters 
        """
        assert len(self.images_to_fit) > 0
        assert self.in_gaus_num != None

        # album and PSF initialisation
        album = astrohack_projections.album_and_model()
        psf = astrohack_projections.mixture_of_gaussians(2)
        psf.add_gaussian(1., numpy.array([0., 0.]), numpy.eye(2)*1.)

        # construct the galaxy using Gaussian mixture
        basevar = 0.5 * numpy.eye(3)
        gal_model = astrohack_projections.galaxy_model_3d()
        for i in xrange(self.in_gaus_num):
            v = numpy.random.uniform(0, 3, size=3)
            mu = numpy.random.uniform(-3, 3, size=3)
            gal_model.add_gaussian(1.0, mu, basevar + numpy.outer(v, v))

        # add all images to the album
        for i in xrange(self.image_parameters['num']):
            data = self.images_to_fit[i]

            # projection parameters
            xi_hat, eta_hat = astrohack_projections.choose_random_projection()
            alpha, beta, gamma = numpy.random.uniform(0.0, 360.0, 3)
            intensity = self.image_parameters['intensity']

            scale = self.image_parameters['scale'] * numpy.exp(numpy.random.uniform())
            if scale < 0.5:
                scale = 0.5
            xshift = numpy.random.uniform(self.image_parameters['xshift'][0], self.image_parameters['xshift'][1])
            yshift = numpy.random.uniform(self.image_parameters['yshift'][0], self.image_parameters['yshift'][1])
            bg = self.image_parameters['bg']
            
            image = astrohack_projections.image_and_model()
            image.set_shape(self.image_parameters['shape'])
            image.set_psf(psf)
            kwargs = {'alpha':alpha, 'beta':beta, 'gamma':gamma, 'intensity':intensity, 'scale':scale, 'xshift': xshift, 'yshift': yshift, 'bg':0.0}
            image.set_parameters(**kwargs)
            image.set_galaxy(gal_model)
            image.set_ivar(numpy.ones(data.shape))
            image.set_data(data + numpy.random.normal(size=data.shape)/ numpy.sqrt(image.ivar))
            # album
            album.add_image(image)
        album.set_galaxy(gal_model)
        self.albums[len(album.galaxy.alphas)] = album
        self.in_album = album
def construct_secondary_album(illustris_images, in_album, psf_blur_size=None):
    """
    function constructs the secondary album, now it will contain new Gaussians for the second phase of running
    """
    num_of_images = len(in_album)

    # album and PSF initialisation
    album = astrohack_projections.album_and_model()
    psf = astrohack_projections.mixture_of_gaussians(2)
    psf.add_gaussian(1., numpy.array([0., 0.]), numpy.eye(2)*1.)

    # construct the galaxy using Gaussian mixture
    gal_model = in_album.galaxy
    basevar = 0.5 * numpy.eye(3)
    for i in xrange(2):
        v = numpy.random.uniform(0, 10, size=3)
        mu = numpy.random.uniform(6, 10, size=3) * numpy.random.choice((-1, 1), size=3)
        gal_model.add_gaussian(1.0, mu, basevar + numpy.outer(v, v))

    # add all images to the album
    for i in xrange(num_of_images):
        old_image = in_album.get_all_images()[i]
        if psf_blur_size==None:
            data = illustris_images[i]
        else:
            data = gaussian_filter(illustris_images[i], psf_blur_size)
        
        image = astrohack_projections.image_and_model()
        image.set_shape(old_image.get_shape())
        image.set_psf(psf)
        image.set_parameters_from_vector(old_image.get_parameters_vector())
        image.set_galaxy(gal_model)
        image.set_ivar(old_image.get_ivar())
        image.set_data(data + numpy.random.normal(size=data.shape)/ numpy.sqrt(image.ivar))
        album.add_image(image)

    return album