def _init_phasing_algorithms(self):
     if not self._phasing_algorithms_dirty:
         self._log("Phasing algorithms already initialised.","DEBUG")
         return       
     i = 0
     for alg_conf in self._phasing_algorithms_configs:
         alg = dict(alg_conf)
         if alg_conf["number_of_iterations"] is None:
             if len(self._phasing_algorithms_configs) == 1:
                 alg["number_of_iterations"] = self._number_of_iterations
             else:
                 self._log("Number of iterations can not be None if many phasing algorithms are specified. Please report this error.","ERROR")
                 return
         # constraints
         constraints = spimage.SpNoConstraints
         if "constraints" in alg:
             if "enforce_positivity" in alg["constraints"] and "enforce_real" in alg["constraints"]:
                 constraints |= spimage.SpPositiveRealObject
             elif "enforce_real" in alg["constraints"]:
                 constraints |= spimage.SpRealObject
             elif "enforce_positivity" in alg["constraints"]:
                 constraints |= spimage.SpPositiveComplexObject
             if "enforce_centrosymmetry" in alg["constraints"]:
                 constraints |= spimage.SpCentrosymmetricObject
         # phasing algorithm
         if alg["type"] in ["raar","hio","diffmap"]:
             alg["beta"] = spimage.sp_smap_alloc(1)
             spimage.sp_smap_insert(alg["beta"], i,alg["beta_init"])
             spimage.sp_smap_insert(alg["beta"], i+alg["number_of_iterations"], alg["beta_final"])
         if alg["type"] == "raar":
             alg["spimage_phasing"] = spimage.sp_phasing_raar_alloc(alg["beta"], constraints)
         elif alg["type"] == "hio":
             alg["spimage_phasing"] = spimage.sp_phasing_hio_alloc(alg["beta"], constraints)
         elif alg["type"] == "diffmap":
             alg["spimage_phasing"] = spimage.sp_phasing_diff_map_alloc(alg["beta"], alg["gamma1"], alg["gamma2"], constraints)
         elif alg["type"] == "er":
             alg["spimage_phasing"] = spimage.sp_phasing_er_alloc(constraints)
         else:
             self._log("Phasing algorithm %s not implemented. Please report this error!" % type,"ERROR")
             return
         i += alg["number_of_iterations"]
         self._phasing_algorithms.append(alg)
     self._phasing_algorithms_dirty = False
     self._log("Phasing algorithm initialised.","DEBUG")
 def _init_phasing_algorithms(self):
     if not self._phasing_algorithms_dirty:
         self._log("Phasing algorithms already initialised.","DEBUG")
         return       
     i = 0
     for alg_conf in self._phasing_algorithms_configs:
         alg = dict(alg_conf)
         if alg_conf["number_of_iterations"] is None:
             if len(self._phasing_algorithms_configs) == 1:
                 alg["number_of_iterations"] = self._number_of_iterations
             else:
                 self._log("Number of iterations can not be None if many phasing algorithms are specified. Please report this error.","ERROR")
                 return
         # constraints
         constraints = spimage.SpNoConstraints
         if "constraints" in alg:
             if "enforce_positivity" in alg["constraints"] and "enforce_real" in alg["constraints"]:
                 constraints |= spimage.SpPositiveRealObject
             elif "enforce_real" in alg["constraints"]:
                 constraints |= spimage.SpRealObject
             elif "enforce_positivity" in alg["constraints"]:
                 constraints |= spimage.SpPositiveComplexObject
             if "enforce_centrosymmetry" in alg["constraints"]:
                 constraints |= spimage.SpCentrosymmetricObject
         # phasing algorithm
         if alg["type"] in ["raar","hio","diffmap"]:
             alg["beta"] = spimage.sp_smap_alloc(1)
             spimage.sp_smap_insert(alg["beta"], i,alg["beta_init"])
             spimage.sp_smap_insert(alg["beta"], i+alg["number_of_iterations"], alg["beta_final"])
         if alg["type"] == "raar":
             alg["spimage_phasing"] = spimage.sp_phasing_raar_alloc(alg["beta"], constraints)
         elif alg["type"] == "hio":
             alg["spimage_phasing"] = spimage.sp_phasing_hio_alloc(alg["beta"], constraints)
         elif alg["type"] == "diffmap":
             alg["spimage_phasing"] = spimage.sp_phasing_diffmap_alloc(alg["beta"], alg["gamma1"], alg["gamma2"], constraints)
         elif alg["type"] == "er":
             alg["spimage_phasing"] = spimage.sp_phasing_er_alloc(constraints)
         else:
             self._log("Phasing algorithm %s not implemented. Please report this error!" % type,"ERROR")
             return
         i += alg["number_of_iterations"]
         self._phasing_algorithms.append(alg)
     self._phasing_algorithms_dirty = False
     self._log("Phasing algorithm initialised.","DEBUG")
Example #3
0
def algorithm_er(real=False, positive=False):
    """Create an ER algorithm object"""
    constraints = get_constraints(real, positive)
    algorithm = _spimage.sp_phasing_er_alloc(constraints)
    return algorithm
support_raw = spimage.sp_image_read(support_file, 0)
support = spimage.sp_image_shift(support_raw)

sup_alg_static = spimage_tools.support_static()  # Our support is static

# phasing algorithm
# beta is a constant, 0.9, but in a format that spimage can read
beta = spimage_tools.smap(0.9)

# constraints = spimage.SpNoConstraints
# constraints = spimage.SpPositiveComplexObject
# Additional constraints, other than Fourier-constraint or Real-constraint.
constraints = spimage.SpPositiveRealObject

phase_alg_main = spimage.sp_phasing_hio_alloc(beta, constraints)
phase_alg_refine = spimage.sp_phasing_er_alloc(constraints)

# create phaser
phaser = spimage.sp_phaser_alloc()
spimage.sp_phaser_init(phaser, phase_alg_main, sup_alg_static,
                       spimage.SpEngineCUDA)
spimage.sp_phaser_set_amplitudes(phaser, amplitudes)
spimage.sp_phaser_init_model(phaser, None, spimage.SpModelRandomPhases)
spimage.sp_phaser_init_support(phaser, support, 0, 0)


def run_it(number_of_iterations):
    spimage.sp_phaser_iterate(phaser, number_of_iterations)


run_it(NUMBER_OF_ITERATIONS)
    options, args = parser.parse_args()

    _numpy.random.seed()
    _spimage.sp_srand(_numpy.random.randint(1e6))
    intensities = _spimage.sp_image_read(options.pattern, 0)
    if intensities.shifted == 0:
        amplitudes = _spimage.sp_image_shift(intensities)
    else:
        amplitudes = _spimage.sp_image_duplicate(intensities, _spimage.SP_COPY_ALL)
    _spimage.sp_image_dephase(amplitudes)
    _spimage.sp_image_to_amplitudes(amplitudes)

    real_space = _spimage.sp_image_read(options.real_space, 0)
    support = _spimage.sp_image_read(options.support, 0)

    phase_alg = _spimage.sp_phasing_er_alloc(_spimage.SpNoConstraints)

    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()))
Example #6
0
def algorithm_er(real=False, positive=False):
    """Create an ER algorithm object"""
    constraints = get_constraints(real, positive)
    algorithm = _spimage.sp_phasing_er_alloc(constraints)
    return algorithm
Example #7
0
    _numpy.random.seed()
    _spimage.sp_srand(_numpy.random.randint(1e6))
    intensities = _spimage.sp_image_read(args.pattern, 0)
    if intensities.shifted == 0:
        amplitudes = _spimage.sp_image_shift(intensities)
    else:
        amplitudes = _spimage.sp_image_duplicate(intensities,
                                                 _spimage.SP_COPY_ALL)
    _spimage.sp_image_dephase(amplitudes)
    _spimage.sp_image_to_amplitudes(amplitudes)

    real_space = _spimage.sp_image_read(args.real_space, 0)
    support = _spimage.sp_image_read(args.support, 0)

    phase_alg = _spimage.sp_phasing_er_alloc(_spimage.SpNoConstraints)

    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)

    fourier_space = _spimage.sp_image_ifftw3(real_space)

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