def reconstruct(self):
        """
        Performs a single reconstruction with the specified configuration and outputs a dictionary with the results.
        """
        self._prepare_reconstruction()
        if not self._ready:
            return
        Out = {}
        #self._init_new_phaser()
        i_alg = 0
        i_sup = 0
        i_out_images = 0
        i_out_scores = 0
        iteration0_alg = 0
        iteration0_sup = 0
        iterations_propagated = 0
        fourier_error = np.zeros(self._number_of_outputs_scores)
        real_error = np.zeros(self._number_of_outputs_scores)
        support_size = np.zeros(self._number_of_outputs_scores)
        real_space = np.zeros(shape=(self._number_of_outputs_images,self._Ny,self._Nx),dtype="complex128")
        support = np.zeros(shape=(self._number_of_outputs_images,self._Ny,self._Nx),dtype="bool")
        fourier_space = np.zeros(shape=(self._number_of_outputs_images,self._Ny,self._Nx),dtype="complex128")
        mask = np.zeros(shape=(self._number_of_outputs_images,self._Ny,self._Nx),dtype="bool")
        for self._iteration in range(self._number_of_iterations+1):
            change_algorithm = (self._iteration-iteration0_alg == self._phasing_algorithms[i_alg]["number_of_iterations"]) and (self._iteration != self._number_of_iterations)
            change_support = (self._iteration-iteration0_sup == self._support_algorithms[i_sup]["number_of_iterations"]) and (self._iteration != self._number_of_iterations)
            if change_algorithm or change_support or self._iteration in self._out_iterations_images or self._iteration in self._out_iterations_scores:
                if self._reconstruction is None:
                    self._log("Iteration %i" % (self._iteration),"INFO")
                else:
                    self._log("Reconstruction %i - Iteration %i" % (self._reconstruction,self._iteration),"INFO")
                # iterate to self._iteration
                if self._iteration != 0:
                    spimage.sp_phaser_iterate(self._phaser,self._iteration-iterations_propagated)
                    iterations_propagated = self._iteration
                if change_algorithm:
                    i_alg += 1
                    iteration0_alg = self._iteration
                    self._phaser.algorithm = self._phasing_algorithms[i_alg]["spimage_phasing"]
                    self._log("Change of phasing algorithm to %s." % self._phasing_algorithms[i_alg]["type"],"INFO")
                if change_support:
                    i_sup += 1
                    iteration0_sup = self._iteration
                    self._phaser.sup_algorithm = self._support_algorithms[i_sup]["spimage_support_array"]
                    self._log("Change of support algorithm to %s." % self._support_algorithms[i_sup]["type"],"INFO")
                if self._iteration in self._out_iterations_images:
                    [real_space[i_out_images,:,:],support[i_out_images,:,:]] = self._get_curr_model(shifted=False)
                    [fourier_space[i_out_images,:,:],mask[i_out_images,:,:]] = self._get_curr_fmodel(shifted=False)
                    i_out_images += 1
                    self._log("Outputting images.","DEBUG")
                if self._iteration in self._out_iterations_scores:
                    scores = self._get_scores()
                    fourier_error[i_out_scores] = scores["fourier_error"]
                    real_error[i_out_scores] = scores["real_error"]
                    support_size[i_out_scores] = scores["support_size"]
                    i_out_scores += 1
                    self._log("Outputting scores.","DEBUG")
        self._iteration = None
        out = {"iteration_index_images":self._out_iterations_images,
               "iteration_index_scores":self._out_iterations_scores,
               "real_space":real_space,
               "support":support,
               "fourier_space":fourier_space,
               "mask":mask,
               "real_error":real_error,
               "fourier_error":fourier_error,
               "support_size":support_size}

        return out
Ejemplo n.º 2
0
    def reconstruct(self):
        """
        Performs a single reconstruction with the specified configuration and outputs a dictionary with the results.
        """
        self._prepare_reconstruction()
        if not self._ready:
            return
        out = {}
        #self._init_new_phaser()
        i_alg = 0
        i_sup = 0
        i_out_images = 0
        i_out_scores = 0
        iteration0_alg = 0
        iteration0_sup = 0
        iterations_propagated = 0
        fourier_error = np.zeros(self._number_of_outputs_scores)
        real_error = np.zeros(self._number_of_outputs_scores)
        support_size = np.zeros(self._number_of_outputs_scores)
        A = self._amplitudes
        if len(A.shape) == 2:
            real_space = np.zeros(shape=(self._number_of_outputs_images,self._Ny,self._Nx),dtype="complex128")
            support = np.zeros(shape=(self._number_of_outputs_images,self._Ny,self._Nx),dtype="bool")
            fourier_space = np.zeros(shape=(self._number_of_outputs_images,self._Ny,self._Nx),dtype="complex128")
            mask = np.zeros(shape=(self._number_of_outputs_images,self._Ny,self._Nx),dtype="bool")
            for self._iteration in range(self._number_of_iterations+1):
                change_algorithm = (self._iteration-iteration0_alg == self._phasing_algorithms[i_alg]["number_of_iterations"]) and (self._iteration != self._number_of_iterations)
                change_support = (self._iteration-iteration0_sup == self._support_algorithms[i_sup]["number_of_iterations"]) and (self._iteration != self._number_of_iterations)
                if change_algorithm or change_support or self._iteration in self._out_iterations_images or self._iteration in self._out_iterations_scores:
                    if self._reconstruction is None:
                        self._log("Iteration %i" % (self._iteration),"INFO")
                    else:
                        self._log("Reconstruction %i - Iteration %i" % (self._reconstruction,self._iteration),"INFO")
                    # iterate to self._iteration
                    if self._iteration != 0:
                        spimage.sp_phaser_iterate(self._sp_phaser,self._iteration-iterations_propagated)
                        iterations_propagated = self._iteration
                    if change_algorithm:
                        i_alg += 1
                        iteration0_alg = self._iteration
                        self._sp_phaser.algorithm = self._phasing_algorithms[i_alg]["spimage_phasing"]
                        self._log("Change of phasing algorithm to %s." % self._phasing_algorithms[i_alg]["type"],"INFO")
                    if change_support:
                        i_sup += 1
                        iteration0_sup = self._iteration
                        self._sp_phaser.sup_algorithm = self._support_algorithms[i_sup]["spimage_support_array"]
                        self._log("Change of support algorithm to %s." % self._support_algorithms[i_sup]["type"],"INFO")
                    if self._iteration in self._out_iterations_images:
                        [real_space[i_out_images,:,:],support[i_out_images,:,:]] = self._get_curr_model(shifted=False)
                        [fourier_space[i_out_images,:,:],mask[i_out_images,:,:]] = self._get_curr_fmodel(shifted=False)
                        i_out_images += 1
                        self._log("Outputting images.","DEBUG")
                    if self._iteration in self._out_iterations_scores:
                        scores = self._get_scores()
                        fourier_error[i_out_scores] = scores["fourier_error"]
                        real_error[i_out_scores] = scores["real_error"]
                        support_size[i_out_scores] = scores["support_size"]
                        i_out_scores += 1
                        self._log("Outputting scores.","DEBUG")
            self._iteration = None
            out = {"iteration_index_images":self._out_iterations_images,
                   "iteration_index_scores":self._out_iterations_scores,
                   "real_space":real_space,
                   "support":support,
                   "fourier_space":fourier_space,
                   "mask":mask,
                   "real_error":real_error,
                   "fourier_error":fourier_error,
                   "support_size":support_size}

            return out

        if len(A.shape) == 3:
            real_space = np.zeros(shape=(self._number_of_outputs_images,self._Nz,self._Ny,self._Nx),dtype="complex128")
            support = np.zeros(shape=(self._number_of_outputs_images,self._Nz,self._Ny,self._Nx),dtype="bool")
            fourier_space = np.zeros(shape=(self._number_of_outputs_images,self._Nz,self._Ny,self._Nx),dtype="complex128")
            mask = np.zeros(shape=(self._number_of_outputs_images,self._Nz,self._Ny,self._Nx),dtype="bool")
            for self._iteration in range(self._number_of_iterations+1):
                change_algorithm = (self._iteration-iteration0_alg == self._phasing_algorithms[i_alg]["number_of_iterations"]) and (self._iteration != self._number_of_iterations)
                change_support = (self._iteration-iteration0_sup == self._support_algorithms[i_sup]["number_of_iterations"]) and (self._iteration != self._number_of_iterations)
                if change_algorithm or change_support or self._iteration in self._out_iterations_images or self._iteration in self._out_iterations_scores:
                    if self._reconstruction is None:
                        self._log("Iteration %i" % (self._iteration),"INFO")
                    else:
                        self._log("Reconstruction %i - Iteration %i" % (self._reconstruction,self._iteration),"INFO")
                    # iterate to self._iteration
                    if self._iteration != 0:
                        spimage.sp_phaser_iterate(self._sp_phaser,self._iteration-iterations_propagated)
                        iterations_propagated = self._iteration
                    if change_algorithm:
                        i_alg += 1
                        iteration0_alg = self._iteration
                        self._sp_phaser.algorithm = self._phasing_algorithms[i_alg]["spimage_phasing"]
                        self._log("Change of phasing algorithm to %s." % self._phasing_algorithms[i_alg]["type"],"INFO")
                    if change_support:
                        i_sup += 1
                        iteration0_sup = self._iteration
                        self._sp_phaser.sup_algorithm = self._support_algorithms[i_sup]["spimage_support_array"]
                        self._log("Change of support algorithm to %s." % self._support_algorithms[i_sup]["type"],"INFO")
                    if self._iteration in self._out_iterations_images:
                        [real_space[i_out_images,:,:,:],support[i_out_images,:,:,:]] = self._get_curr_model(shifted=False)
                        [fourier_space[i_out_images,:,:,:],mask[i_out_images,:,:,:]] = self._get_curr_fmodel(shifted=False)
                        i_out_images += 1
                        self._log("Outputting images.","DEBUG")
                    if self._iteration in self._out_iterations_scores:
                        scores = self._get_scores()
                        fourier_error[i_out_scores] = scores["fourier_error"]
                        real_error[i_out_scores] = scores["real_error"]
                        support_size[i_out_scores] = scores["support_size"]
                        i_out_scores += 1
                        self._log("Outputting scores.","DEBUG")
            self._iteration = None
            out = {"iteration_index_images":self._out_iterations_images,
                   "iteration_index_scores":self._out_iterations_scores,
                   "real_space":real_space,
                   "support":support,
                   "fourier_space":fourier_space,
                   "mask":mask,
                   "real_error":real_error,
                   "fourier_error":fourier_error,
                   "support_size":support_size}

            return out
Ejemplo n.º 3
0
    sup_alg = _spimage.sp_support_array_init(_spimage.sp_support_static_alloc(), 20)

    # create phaser
    phaser = _spimage.sp_phaser_alloc()
    _spimage.sp_phaser_init(phaser, phase_alg, sup_alg, _spimage.SpEngineCUDA)
    _spimage.sp_phaser_set_amplitudes(phaser, amplitudes)
    _spimage.sp_phaser_init_model(phaser, real_space, 0)
    _spimage.sp_phaser_init_support(phaser, support, 0, 0)

    #real_space_s = _spimage.sp_image_shift(real_space)
    fourier_space = _spimage.sp_image_ifftw3(real_space)

    ereal_start = _numpy.sqrt((abs(real_space.image[~_numpy.bool8(support.image)])**2).sum() / (abs(real_space.image)**2).sum())
    efourier_start = _numpy.sqrt(((abs(fourier_space.image[_numpy.bool8(amplitudes.mask)]) - abs(amplitudes.image[_numpy.bool8(amplitudes.mask)]))**2).sum() / ((abs(amplitudes.image[_numpy.bool8(amplitudes.mask)])**2).sum() + (abs(fourier_space.image[~_numpy.bool8(amplitudes.mask)])**2).sum()))

    _spimage.sp_phaser_iterate(phaser, options.number_of_iterations)

    model_out = _spimage.sp_phaser_model(phaser)
    support_out = _spimage.sp_phaser_support(phaser)
    fmodel_out = _spimage.sp_phaser_fmodel(phaser)
    real_space_end = _spimage.sp_phaser_model_before_projection(phaser)
    fourier_space_end = _spimage.sp_phaser_fmodel(phaser)

    ereal_end = _numpy.sqrt((abs(real_space_end.image[~_numpy.bool8(support.image)])**2).sum() / (abs(real_space_end.image)**2).sum())
    efourier_end = _numpy.sqrt(((abs(fourier_space_end.image[_numpy.bool8(amplitudes.mask)]) - abs(amplitudes.image[_numpy.bool8(amplitudes.mask)]))**2).sum() / ((abs(amplitudes.image[_numpy.bool8(amplitudes.mask)])**2).sum() + (abs(fourier_space_end.image[~_numpy.bool8(amplitudes.mask)])**2).sum()))

    _spimage.sp_image_write(model_out, "%s/real_space-%s.h5" % (options.output_dir, options.output_affix), 0)
    _spimage.sp_image_write(support_out, "%s/support-%s.h5" % (options.output_dir, options.output_affix), 0)
    _spimage.sp_image_write(fmodel_out, "%s/fourier_space-%s.h5" % (options.output_dir, options.output_affix), 0)
    print "Ereal:    %g -> %g" % (ereal_start, ereal_end)
    print "Efourier: %g -> %g" % (efourier_start, efourier_end)
Ejemplo n.º 4
0
def run_it(number_of_iterations):
    spimage.sp_phaser_iterate(phaser, number_of_iterations)
Ejemplo n.º 5
0
    _spimage.sp_phaser_init_model(phaser, real_space, 0)
    _spimage.sp_phaser_init_support(phaser, support, 0, 0)

    fourier_space = _spimage.sp_image_ifftw3(real_space)

    support = _numpy.bool8(support.image)
    ereal_start = _numpy.sqrt((abs(real_space.image[~support])**2).sum() /
                              (abs(real_space.image)**2).sum())

    mask = _numpy.bool8(amplitudes.mask)
    efourier_start = _numpy.sqrt(
        ((abs(fourier_space.image[mask]) - abs(amplitudes.image[mask]))**
         2).sum() / ((abs(amplitudes.image[mask])**2).sum() +
                     (abs(fourier_space.image[~mask])**2).sum()))

    _spimage.sp_phaser_iterate(phaser, args.number_of_iterations)

    model_out = _spimage.sp_phaser_model(phaser)
    support_out = _spimage.sp_phaser_support(phaser)
    fmodel_out = _spimage.sp_phaser_fmodel(phaser)
    real_space_end = _spimage.sp_phaser_model_before_projection(phaser)
    fourier_space_end = _spimage.sp_phaser_fmodel(phaser)

    ereal_end = _numpy.sqrt((abs(real_space_end.image[~support])**2).sum() /
                            (abs(real_space_end.image)**2).sum())
    efourier_end = _numpy.sqrt(
        ((abs(fourier_space_end.image[mask]) - abs(amplitudes.image[mask]))**
         2).sum() / ((abs(amplitudes.image[mask])**2).sum() +
                     (abs(fourier_space_end.image[~mask])**2).sum()))

    _spimage.sp_image_write(model_out,