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")
def get_basic_phaser(amplitudes, support, mask=None): """This function is not finished""" if mask is None: amplitudes_sp = image_from_array(amplitudes) else: amplitudes_sp = image_from_array(amplitudes, mask) beta = _spimage.sp_smap_alloc(1) _spimage.sp_smap_insert(beta, 0, 0.9) phase_alg = _spimage.sp_phasing_hio_alloc(beta, _spimage.SpNoConstraints) support_sp = image_from_array(support) sup_alg = _spimage.sp_support_array_init(_spimage.sp_support_static_alloc(), 20) phaser = _spimage.sp_phaser_alloc() _spimage.sp_phaser_init(phaser, phase_alg, sup_alg, _spimage.SpEngineCUDA) _spimage.sp_phaser_set_amplitudes(phaser, amplitudes_sp) _spimage.sp_phaser_init_model(phaser, None, _spimage.SpModelRandomPhases) _spimage.sp_phaser_init_support(phaser, support_sp, 0, 0) return phaser
def get_basic_phaser(amplitudes, support, mask=None, algorithm=None): """This function is not finished""" if mask is None: amplitudes_sp = image_from_array(amplitudes) else: amplitudes_sp = image_from_array(amplitudes, mask) if algorithm is None: beta = _spimage.sp_smap_alloc(1) _spimage.sp_smap_insert(beta, 0, 0.9) phase_alg = _spimage.sp_phasing_hio_alloc(beta, _spimage.SpNoConstraints) else: phase_alg = algorithm support_sp = image_from_array(support) sup_alg = _spimage.sp_support_array_init( _spimage.sp_support_static_alloc(), 20) phaser = _spimage.sp_phaser_alloc() _spimage.sp_phaser_init(phaser, phase_alg, sup_alg, _spimage.SpEngineCUDA) _spimage.sp_phaser_set_amplitudes(phaser, amplitudes_sp) _spimage.sp_phaser_init_model(phaser, None, _spimage.SpModelRandomPhases) _spimage.sp_phaser_init_support(phaser, support_sp, 0, 0) return phaser
def algorithm_hio(beta, real=False, positive=False): """Create an HIO algorithm object""" beta_sp = smap(beta) constraints = get_constraints(real, positive) algorithm = _spimage.sp_phasing_hio_alloc(beta_sp, constraints) return algorithm
support_file = os.path.join(INPUT_DIR, 'particle_support.h5') 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)