def exercise_miller_array_data_types(): miller_set = crystal.symmetry(unit_cell=(10, 10, 10, 90, 90, 90), space_group_symbol="P1").miller_set( indices=flex.miller_index([(1, 2, 3), (4, 5, 6)]), anomalous_flag=False ) for data in [ flex.bool([False, True]), flex.int([0, 1]), flex.size_t([0, 1]), flex.double([0, 1]), flex.complex_double([0, 1]), ]: miller_array = miller_set.array(data=data) if op.isfile("tmp_iotbx_mtz.mtz"): os.remove("tmp_iotbx_mtz.mtz") assert not op.isfile("tmp_iotbx_mtz.mtz") miller_array.as_mtz_dataset(column_root_label="DATA").mtz_object().write(file_name="tmp_iotbx_mtz.mtz") assert op.isfile("tmp_iotbx_mtz.mtz") mtz_obj = mtz.object(file_name="tmp_iotbx_mtz.mtz") miller_arrays_read_back = mtz_obj.as_miller_arrays() assert len(miller_arrays_read_back) == 1 miller_array_read_back = miller_arrays_read_back[0] assert miller_array_read_back.indices().all_eq(miller_array.indices()) if miller_array.is_integer_array() or miller_array.is_bool_array(): assert miller_array_read_back.data().all_eq(flex.int([0, 1])) elif miller_array.is_real_array(): assert miller_array_read_back.data().all_eq(flex.double([0, 1])) elif miller_array.is_complex_array(): assert miller_array_read_back.data().all_eq(flex.complex_double([0, 1])) else: raise RuntimeError("Programming error.")
def exercise_phase_transfer(): sg = sgtbx.space_group_info("P 21 21 21").group() i = flex.miller_index(((1,2,3), (3,0,3))) a = flex.double((-3.6,4.6)) p = flex.complex_double((1+2j, 0)) assert approx_equal(tuple(miller.phase_transfer(sg, i, a, p, 1.e-10)), ((-1.6099689-3.2199379j), 0j)) a = flex.complex_double((3.6,4.6)) try: miller.phase_transfer(sg, i, a, p) except Exception as e: if (str(e.__class__).find("Boost.Python.ArgumentError") < 0): raise RuntimeError("Unexpected exception: %s" % str(e)) else: raise Exception_expected a = flex.double((-3.6,4.6)) p = flex.double((10,20)) t = miller.phase_transfer(sg, i, a, p, True) assert approx_equal(tuple(flex.abs(t)), flex.abs(a)) assert approx_equal(tuple(flex.arg(t, True)), (-170,90)) p = p * (math.pi/180) t = miller.phase_transfer(sg, i, a, p, False) assert approx_equal(tuple(flex.abs(t)), flex.abs(a)) assert approx_equal(tuple(flex.arg(t, True)), (-170,90))
def exercise_miller_array_data_types(): miller_set = crystal.symmetry(unit_cell=(10, 10, 10, 90, 90, 90), space_group_symbol="P1").miller_set( indices=flex.miller_index([(1, 2, 3), (4, 5, 6)]), anomalous_flag=False) for data in [ flex.bool([False, True]), flex.int([0, 1]), flex.size_t([0, 1]), flex.double([0, 1]), flex.complex_double([0, 1]) ]: miller_array = miller_set.array(data=data) if (op.isfile("tmp_iotbx_mtz.mtz")): os.remove("tmp_iotbx_mtz.mtz") assert not op.isfile("tmp_iotbx_mtz.mtz") miller_array.as_mtz_dataset( column_root_label="DATA").mtz_object().write( file_name="tmp_iotbx_mtz.mtz") assert op.isfile("tmp_iotbx_mtz.mtz") mtz_obj = mtz.object(file_name="tmp_iotbx_mtz.mtz") miller_arrays_read_back = mtz_obj.as_miller_arrays() assert len(miller_arrays_read_back) == 1 miller_array_read_back = miller_arrays_read_back[0] assert miller_array_read_back.indices().all_eq(miller_array.indices()) if (miller_array.is_integer_array() or miller_array.is_bool_array()): assert miller_array_read_back.data().all_eq(flex.int([0, 1])) elif (miller_array.is_real_array()): assert miller_array_read_back.data().all_eq(flex.double([0, 1])) elif (miller_array.is_complex_array()): assert miller_array_read_back.data().all_eq( flex.complex_double([0, 1])) else: raise RuntimeError("Programming error.")
def exercise_match_indices(): h0 = flex.miller_index( ((1, 2, 3), (-1, -2, -3), (2, 3, 4), (-2, -3, -4), (3, 4, 5))) d0 = flex.double((1, 2, 3, 4, 5)) h1 = flex.miller_index(((-1, -2, -3), (-2, -3, -4), (1, 2, 3), (2, 3, 4))) d1 = flex.double((10, 20, 30, 40)) mi = miller.match_indices(h0, h0) assert mi.have_singles() == 0 assert list(mi.pairs()) == zip(range(5), range(5)) mi = miller.match_indices(h0, h1) assert tuple(mi.singles(0)) == (4, ) assert tuple(mi.singles(1)) == () assert tuple(mi.pairs()) == ((0, 2), (1, 0), (2, 3), (3, 1)) assert tuple(mi.pair_selection(0)) == (1, 1, 1, 1, 0) assert tuple(mi.single_selection(0)) == (0, 0, 0, 0, 1) assert tuple(mi.pair_selection(1)) == (1, 1, 1, 1) assert tuple(mi.single_selection(1)) == (0, 0, 0, 0) assert tuple(mi.paired_miller_indices(0)) \ == tuple(h0.select(mi.pair_selection(0))) l1 = list(mi.paired_miller_indices(1)) l2 = list(h1.select(mi.pair_selection(1))) l1.sort() l2.sort() assert l1 == l2 assert approx_equal(tuple(mi.plus(d0, d1)), (31, 12, 43, 24)) assert approx_equal(tuple(mi.minus(d0, d1)), (-29, -8, -37, -16)) assert approx_equal(tuple(mi.multiplies(d0, d1)), (30, 20, 120, 80)) assert approx_equal(tuple(mi.divides(d0, d1)), (1 / 30., 2 / 10., 3 / 40., 4 / 20.)) assert approx_equal(tuple(mi.additive_sigmas(d0, d1)), [ math.sqrt(x * x + y * y) for x, y in ((1, 30), (2, 10), (3, 40), (4, 20)) ]) q = flex.size_t((3, 2, 0, 4, 1)) h1 = h0.select(q) assert tuple(miller.match_indices(h1, h0).permutation()) == tuple(q) p = miller.match_indices(h0, h1).permutation() assert tuple(p) == (2, 4, 1, 0, 3) assert tuple(h1.select(p)) == tuple(h0) cd0 = [ complex(a, b) for (a, b) in (1, 1), (2, 0), (3.5, -1.5), (5, -3), (-8, 5.4) ] cd1 = [ complex(a, b) for (a, b) in (1, -1), (2, 1), (0.5, 1.5), (-1, -8), (10, 0) ] cd2 = flex.complex_double(cd0) cd3 = flex.complex_double(cd1) mi = miller.match_indices(h0, h0) assert approx_equal(tuple(mi.plus(cd2, cd3)), ((2 + 0j), (4 + 1j), (4 + 0j), (4 - 11j), (2 + 5.4j)))
def exercise_fast_nv1995(): space_group = sgtbx.space_group_info("P 21 21 21").group() miller_indices_f_obs = flex.miller_index(((3,4,5),(4,5,6))) f = translation_search.fast_nv1995( gridding=(20,20,36), space_group=space_group, anomalous_flag=False, miller_indices_f_obs=miller_indices_f_obs, f_obs=flex.double((1,2)), f_part=flex.complex_double(), miller_indices_p1_f_calc=flex.miller_index(((1,2,3),)), p1_f_calc=flex.complex_double((12,))) assert f.target_map().all() == (20,20,36)
def exercise_fast_nv1995(): space_group = sgtbx.space_group_info("P 21 21 21").group() miller_indices_f_obs = flex.miller_index(((3, 4, 5), (4, 5, 6))) f = translation_search.fast_nv1995( gridding=(20, 20, 36), space_group=space_group, anomalous_flag=False, miller_indices_f_obs=miller_indices_f_obs, f_obs=flex.double((1, 2)), f_part=flex.complex_double(), miller_indices_p1_f_calc=flex.miller_index(((1, 2, 3), )), p1_f_calc=flex.complex_double((12, ))) assert f.target_map().all() == (20, 20, 36)
def exercise_phase_transfer(): sg = sgtbx.space_group_info("P 21 21 21").group() i = flex.miller_index(((1,2,3), (3,0,3))) a = flex.double((-3.6,4.6)) p = flex.complex_double((1+2j, 0)) assert approx_equal(tuple(miller.phase_transfer(sg, i, a, p, 1.e-10)), ((-1.6099689-3.2199379j), 0j)) a = flex.complex_double((3.6,4.6)) try: miller.phase_transfer(sg, i, a, p) except Exception, e: if (str(e.__class__).find("Boost.Python.ArgumentError") < 0): raise RuntimeError("Unexpected exception: %s" % str(e))
def exercise_miller_arrays_as_cif_block(): from iotbx.cif import reader cif_model = reader(input_string=cif_miller_array, builder=cif.builders.cif_model_builder()).model() ma_builder = cif.builders.miller_array_builder(cif_model['global']) ma1 = ma_builder.arrays()['_refln_F_squared_meas'] mas_as_cif_block = cif.miller_arrays_as_cif_block(ma1, array_type='meas', format="corecif") mas_as_cif_block.add_miller_array( ma1.array(data=flex.complex_double([1 - 1j] * ma1.size())), array_type='calc') mas_as_cif_block.add_miller_array( ma1.array(data=flex.complex_double([1 - 2j] * ma1.size())), column_names=['_refln_A_calc', '_refln_B_calc']) for key in ('_refln_F_squared_meas', '_refln_F_squared_sigma', '_refln_F_calc', '_refln_phase_calc', '_refln_A_calc', '_refln_A_calc'): assert key in mas_as_cif_block.cif_block.keys(), key # mas_as_cif_block = cif.miller_arrays_as_cif_block(ma1, array_type='meas', format="mmcif") mas_as_cif_block.add_miller_array( ma1.array(data=flex.complex_double([1 - 1j] * ma1.size())), array_type='calc') for key in ('_refln.F_squared_meas', '_refln.F_squared_sigma', '_refln.F_calc', '_refln.phase_calc', '_space_group_symop.operation_xyz', '_cell.length_a', '_refln.index_h'): assert key in mas_as_cif_block.cif_block.keys() # mas_as_cif_block = cif.miller_arrays_as_cif_block( ma1, column_names=[ '_diffrn_refln_intensity_net', '_diffrn_refln_intensity_sigma' ], miller_index_prefix='_diffrn_refln') mas_as_cif_block.add_miller_array( ma1.array(data=flex.std_string(ma1.size(), 'om')), column_name='_diffrn_refln_intensity_u') for key in ('_diffrn_refln_intensity_net', '_diffrn_refln_intensity_sigma', '_diffrn_refln_intensity_u'): assert key in mas_as_cif_block.cif_block.keys() # try: reader(input_string=cif_global) except CifParserError, e: pass
def exercise(): ma = miller.array( miller.set(crystal.symmetry(unit_cell=(5,5,5, 90, 90, 90), space_group=sgtbx.space_group('P 2x')), indices=flex.miller_index( [(1,0,0), (0,1,0), (0,0,1), (-1,0,0), (0,-1,0), (0,0,-1), (1,1,0), (1,0,1), (0,1,1), (-1,-1,0), (-1,0,-1), (0,-1,-1), (1,-1,0), (1,0,-1), (0,1,-1), (-1,1,0), (-1,0,1), (0,-1,1), (1,1,1), (-1,1,1), (1,-1,1), (1,1,-1), (-1,-1,-1), (1,-1,-1), (-1,1,-1), (-1,-1,1)])), data=flex.complex_double(flex.random_double(26), flex.random_double(26))) f_at_h = dict(zip(ma.indices(), ma.data())) for op in ("-x, y+1/2, -z", "x+1/2, -y, z-1/2"): op = sgtbx.rt_mx(op) original, transformed = ma.common_sets( ma.change_basis(sgtbx.change_of_basis_op(op.inverse()))) for h, f in original: assert f == f_at_h[h] for h, op_f in transformed: assert approx_equal( op_f, f_at_h[h*op.r()]*exp(1j*2*pi*row(h).dot(col(op.t().as_double()))))
def compare_with_cctbx_structure_factors(n_scatt, n_refl, output_lines): from cctbx import xray from cctbx import miller from cctbx import crystal from cctbx.array_family import flex crystal_symmetry = crystal.symmetry(unit_cell=(11, 12, 13, 90, 90, 90), space_group_symbol="P1") scatterers = flex.xray_scatterer() miller_indices = flex.miller_index() f_calc = flex.complex_double() for line in output_lines: flds = line.split() assert len(flds) in [4, 5] if (len(flds) == 4): x, y, z, b_iso = [float(s) for s in flds] scatterers.append( xray.scatterer(site=(x, y, z), b=b_iso, scattering_type="const")) else: miller_indices.append([int(s) for s in flds[:3]]) f_calc.append(complex(float(flds[3]), float(flds[4]))) assert scatterers.size() == n_scatt assert miller_indices.size() == n_refl xs = xray.structure(crystal_symmetry=crystal_symmetry, scatterers=scatterers) fc = miller_array = miller.set(crystal_symmetry=crystal_symmetry, indices=miller_indices, anomalous_flag=False).array(data=f_calc) fc2 = fc.structure_factors_from_scatterers(xray_structure=xs, algorithm="direct", cos_sin_table=False).f_calc() for f1, f2 in zip(fc.data(), fc2.data()): assert approx_equal(f1, f2, eps=1e-5)
def exercise_singular_least_squares(): obs = flex.double([1.234]) weights_2345 = flex.double([2.345]) weights_zero = flex.double([0]) r_free_flags = flex.bool([False]) a = flex.double([0]) b = flex.double([0]) for obs_type in ["F", "I"]: for weights, scale_factor in [(weights_2345, 3.456), (weights_zero, 0)]: tg = ext.targets_least_squares(compute_scale_using_all_data=False, obs_type=obs_type, obs=obs, weights=weights, r_free_flags=r_free_flags, f_calc=flex.complex_double(a, b), derivatives_depth=2, scale_factor=scale_factor) if (weights is weights_2345): assert approx_equal(tg.scale_factor(), scale_factor) assert list(tg.gradients_work()) == [0j] assert list(tg.hessians_work()) == [(1, 1, 1)] else: assert tg.scale_factor() is None assert tg.target_work() is None assert tg.target_test() is None assert tg.gradients_work().size() == 0 assert tg.hessians_work().size() == 0
def convert_to_sf(hkl, intensities, phases, cs): """ Reformat intensities and phases into a CCTBX-style Miller array, with space group symmetry enforced. Inputs: ------- hkl: list of Miller indices, formatted as tuples intensities: array of intensities, with ordering matching hkl phases: array of phases with ordering matching hkl cs: CCTBX crystal.symmetry object Outputs: -------- ma: CCTBX-style Miller array """ # compute structure factors in complex number format if (phases.min() < -1*np.pi) or (phases.max() > np.pi): print "Error: Invalid phase values; may be in degrees rather than radians." A, B = np.sqrt(intensities)*np.cos(phases), np.sqrt(intensities)*np.sin(phases) B[np.abs(B)<1e-12] = 0 # reformat miller indices and structure factor information into CCTBX format indices = flex.miller_index(hkl) sf_data = flex.complex_double(flex.double(A),flex.double(B)) ma = miller.array(miller_set=miller.set(cs, indices, anomalous_flag=False), data=sf_data) return ma
def exercise(): ma = miller.array(miller.set( crystal.symmetry(unit_cell=(5, 5, 5, 90, 90, 90), space_group=sgtbx.space_group('P 2x')), indices=flex.miller_index([(1, 0, 0), (0, 1, 0), (0, 0, 1), (-1, 0, 0), (0, -1, 0), (0, 0, -1), (1, 1, 0), (1, 0, 1), (0, 1, 1), (-1, -1, 0), (-1, 0, -1), (0, -1, -1), (1, -1, 0), (1, 0, -1), (0, 1, -1), (-1, 1, 0), (-1, 0, 1), (0, -1, 1), (1, 1, 1), (-1, 1, 1), (1, -1, 1), (1, 1, -1), (-1, -1, -1), (1, -1, -1), (-1, 1, -1), (-1, -1, 1)])), data=flex.complex_double(flex.random_double(26), flex.random_double(26))) f_at_h = dict(zip(ma.indices(), ma.data())) for op in ("-x, y+1/2, -z", "x+1/2, -y, z-1/2"): op = sgtbx.rt_mx(op) original, transformed = ma.common_sets( ma.change_basis(sgtbx.change_of_basis_op(op.inverse()))) for h, f in original: assert f == f_at_h[h] for h, op_f in transformed: assert approx_equal( op_f, f_at_h[h * op.r()] * exp(1j * 2 * pi * row(h).dot(col(op.t().as_double()))))
def exercise_map_to_asu(sg_symbol): sg_type = sgtbx.space_group_type(sg_symbol) index_abs_range = (4,4,4) for anomalous_flag in (False,True): m = miller.index_generator( sg_type, anomalous_flag, index_abs_range).to_array() a = flex.double() p = flex.double() c = flex.hendrickson_lattman() for i in range(m.size()): a.append(random.random()) p.append(random.random() * 2) c.append([random.random() for j in range(4)]) f = flex.polar(a, p) p = [p, p*(180/math.pi)] m_random = flex.miller_index() p_random = [flex.double(), flex.double()] c_random = flex.hendrickson_lattman() f_random = flex.complex_double() for i,h_asym in enumerate(m): h_eq = miller.sym_equiv_indices(sg_type.group(), h_asym) i_eq = random.randrange(h_eq.multiplicity(anomalous_flag)) h_i = h_eq(i_eq) m_random.append(h_i.h()) for deg in (False,True): p_random[deg].append(h_i.phase_eq(p[deg][i], deg)) f_random.append(h_i.complex_eq(f[i])) c_random.append(h_i.hendrickson_lattman_eq(c[i])) m_random_copy = m_random.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] m_random_copy = m_random.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy, f_random) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,f_asym in enumerate(f): assert abs(f_asym - f_random[i]) < 1.e-6 m_random_copy = m_random.deep_copy() a_random = a.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy, a_random) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,a_asym in enumerate(a): assert a_asym == a_random[i] for deg in (False,True): m_random_copy = m_random.deep_copy() miller.map_to_asu( sg_type, anomalous_flag, m_random_copy, p_random[deg], deg) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,p_asym in enumerate(p[deg]): assert scitbx.math.phase_error(p_asym, p_random[deg][i], deg) < 1.e-5 m_random_copy = m_random.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy, c_random) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,c_asym in enumerate(c): for j in range(4): assert abs(c_asym[j] - c_random[i][j]) < 1.e-5
def exercise_core_LS(target_class, verbose): n_refl = 10 f_calc = flex.polar( flex.random_double(n_refl)*10-5, flex.random_double(n_refl)*10-5) f_obs = flex.abs(f_calc) + (flex.random_double(n_refl)*2-1) weights = flex.random_double(n_refl) r = xray.targets_least_squares_residual( f_obs, weights, f_calc, True, 0) scale_factor = r.scale_factor() gr_ana = r.derivatives() gr_fin = flex.complex_double() eps = 1.e-6 for i_refl in xrange(n_refl): gc = [] for i_part in [0,1]: fc0 = f_calc[i_refl] ts = [] for signed_eps in [eps,-eps]: if (i_part == 0): f_calc[i_refl] = complex(fc0.real + signed_eps, fc0.imag) else: f_calc[i_refl] = complex(fc0.real, fc0.imag + signed_eps) r = xray.targets_least_squares_residual( f_obs, weights, f_calc, False, scale_factor) ts.append(r.target()) f_calc[i_refl] = fc0 gc.append((ts[0]-ts[1])/(2*eps)) gr_fin.append(complex(*gc)) if (verbose): print "ana:", list(gr_ana) print "fin:", list(gr_fin) assert approx_equal(gr_fin, gr_ana)
def check_f_derivs(): g_ana = trg.f_gradients c_ana = trg.f_hessians eps = 1e-6 g_fin = flex.complex_double() c_fin = flex.vec3_double() for ih in xrange(i_calc.size()): c_orig = f_calc[ih] g_fin_ab = [] c_fin_ab = [] for iab in [0,1]: fs = [] gs = [] for signed_eps in [eps, -eps]: if (iab == 0): f_calc[ih] = complex(c_orig.real + signed_eps, c_orig.imag) else: f_calc[ih] = complex(c_orig.real, c_orig.imag + signed_eps) trg_eps = kwt2( f_obs=f_obs, i_obs=i_obs, i_sig=i_sig, f_calc=f_calc, i_calc=None, wa=wa, wb=wb) fs.append(trg_eps.target) gs.append(trg_eps.f_gradients[ih]) g_fin_ab.append((fs[0]-fs[1])/(2*eps)) c_fin_ab.append((gs[0]-gs[1])/(2*eps)) g_fin.append(complex(*g_fin_ab)) assert approx_equal(c_fin_ab[0].imag, c_fin_ab[1].real) c_fin.append((c_fin_ab[0].real, c_fin_ab[1].imag, c_fin_ab[0].imag)) f_calc[ih] = c_orig assert approx_equal(g_ana, g_fin) assert approx_equal(c_ana, c_fin)
def exercise_f000(): miller_indices = flex.miller_index([(0,0,0)]) data = flex.complex_double([1-2j]) n_real = [1,2,3] conjugate_flag = True for hall_symbol in ["P 1", "P 3", "R 3*"]: for is_centric in [False, True]: if (not is_centric): space_group = sgtbx.space_group(hall_symbol) else: space_group.expand_smx("-x,-y,-z") for anomalous_flag in [False, True]: if (not anomalous_flag): rfft = fftpack.real_to_complex_3d(n_real) n_complex = rfft.n_complex() else: cfft = fftpack.complex_to_complex_3d(n_real) n_complex = cfft.n() for treat_restricted in [False, True]: map = maptbx.structure_factors.to_map( space_group=space_group, anomalous_flag=anomalous_flag, miller_indices=miller_indices, structure_factors=data, n_real=n_real, map_grid=flex.grid(n_complex), conjugate_flag=conjugate_flag, treat_restricted=treat_restricted) if (treat_restricted): assert approx_equal( map.complex_map()[0], data[0]) else: assert approx_equal( map.complex_map()[0], data[0]*space_group.order_p())
def run_fast_nv1995(f_obs, f_calc_fixed, f_calc_p1, symmetry_flags, gridding, grid_tags, verbose): if (f_calc_fixed is None): f_part = flex.complex_double() else: f_part = f_calc_fixed.data() assert f_obs.anomalous_flag() == f_calc_p1.anomalous_flag() fast_nv1995 = translation_search.fast_nv1995( gridding=gridding, space_group=f_obs.space_group(), anomalous_flag=f_obs.anomalous_flag(), miller_indices_f_obs=f_obs.indices(), f_obs=f_obs.data(), f_part=f_part, miller_indices_p1_f_calc=f_calc_p1.indices(), p1_f_calc=f_calc_p1.data()) assert fast_nv1995.target_map().all() == gridding map_stats = maptbx.statistics(fast_nv1995.target_map()) if (0 or verbose): map_stats.show_summary() grid_tags.build(f_obs.space_group_info().type(), symmetry_flags) assert grid_tags.n_grid_misses() == 0 assert grid_tags.verify(fast_nv1995.target_map()) peak_list = maptbx.peak_list( data=fast_nv1995.target_map(), tags=grid_tags.tag_array(), peak_search_level=1, max_peaks=10, interpolate=True) if (0 or verbose): print "gridding:", gridding for i,site in enumerate(peak_list.sites()): print "(%.4f,%.4f,%.4f)" % site, "%.6g" % peak_list.heights()[i] assert approx_equal(map_stats.max(), flex.max(peak_list.grid_heights())) return peak_list
def get_tg(self, k_sol, b_sol, u_star): fmodel_data = ext.core( f_calc = self.fmodel.f_calc().data(), shell_f_masks = self.f_masks, k_sols = k_sol, b_sols = b_sol, f_part1 = flex.complex_double(self.fmodel.f_calc().data().size(),0), f_part2 = flex.complex_double(self.fmodel.f_calc().data().size(),0), u_star = list(u_star), hkl = self.fmodel.f_calc().indices(), ss = self.fmodel.ss) return bss.bulk_solvent.ls_u_star( f_model_abs_no_k_total = flex.abs(fmodel_data.f_model_no_aniso_scale), f_obs = self.fmodel.f_obs().data(), miller_indices = self.fmodel.f_calc().indices(), k_anisotropic = fmodel_data.k_anisotropic)
def exercise_map_to_asu(sg_symbol): sg_type = sgtbx.space_group_type(sg_symbol) index_abs_range = (4,4,4) for anomalous_flag in (False,True): m = miller.index_generator( sg_type, anomalous_flag, index_abs_range).to_array() a = flex.double() p = flex.double() c = flex.hendrickson_lattman() for i in xrange(m.size()): a.append(random.random()) p.append(random.random() * 2) c.append([random.random() for j in xrange(4)]) f = flex.polar(a, p) p = [p, p*(180/math.pi)] m_random = flex.miller_index() p_random = [flex.double(), flex.double()] c_random = flex.hendrickson_lattman() f_random = flex.complex_double() for i,h_asym in enumerate(m): h_eq = miller.sym_equiv_indices(sg_type.group(), h_asym) i_eq = random.randrange(h_eq.multiplicity(anomalous_flag)) h_i = h_eq(i_eq) m_random.append(h_i.h()) for deg in (False,True): p_random[deg].append(h_i.phase_eq(p[deg][i], deg)) f_random.append(h_i.complex_eq(f[i])) c_random.append(h_i.hendrickson_lattman_eq(c[i])) m_random_copy = m_random.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] m_random_copy = m_random.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy, f_random) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,f_asym in enumerate(f): assert abs(f_asym - f_random[i]) < 1.e-6 m_random_copy = m_random.deep_copy() a_random = a.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy, a_random) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,a_asym in enumerate(a): assert a_asym == a_random[i] for deg in (False,True): m_random_copy = m_random.deep_copy() miller.map_to_asu( sg_type, anomalous_flag, m_random_copy, p_random[deg], deg) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,p_asym in enumerate(p[deg]): assert scitbx.math.phase_error(p_asym, p_random[deg][i], deg) < 1.e-5 m_random_copy = m_random.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy, c_random) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,c_asym in enumerate(c): for j in xrange(4): assert abs(c_asym[j] - c_random[i][j]) < 1.e-5
def exercise_f000(): miller_indices = flex.miller_index([(0, 0, 0)]) data = flex.complex_double([1 - 2j]) n_real = [1, 2, 3] conjugate_flag = True for hall_symbol in ["P 1", "P 3", "R 3*"]: for is_centric in [False, True]: if (not is_centric): space_group = sgtbx.space_group(hall_symbol) else: space_group.expand_smx("-x,-y,-z") for anomalous_flag in [False, True]: if (not anomalous_flag): rfft = fftpack.real_to_complex_3d(n_real) n_complex = rfft.n_complex() else: cfft = fftpack.complex_to_complex_3d(n_real) n_complex = cfft.n() for treat_restricted in [False, True]: map = maptbx.structure_factors.to_map( space_group=space_group, anomalous_flag=anomalous_flag, miller_indices=miller_indices, structure_factors=data, n_real=n_real, map_grid=flex.grid(n_complex), conjugate_flag=conjugate_flag, treat_restricted=treat_restricted) if (treat_restricted): assert approx_equal(map.complex_map()[0], data[0]) else: assert approx_equal(map.complex_map()[0], data[0] * space_group.order_p())
def load_sfall(fname): """ special script for loading the structure factor file generated in main() :param fname: file generated in the main method above.. :return: mil_ar, energies mil_ar: dict of miller arrays (complex) energies: array of xray energies in electron volts such that mil_ar[0] is Fhkl at energy energies[0] """ f = h5py.File(fname, "r") data = f["data"][()] indices = f["indices"][()] hall = f["hall_symbol"][()] ucell_param = f["ucell_tuple"][()] energies = f["energies"][()] sg = sgtbx.space_group(hall) Symm = crystal.symmetry(unit_cell=ucell_param, space_group=sg) indices_flex = tuple(map(tuple, indices)) mil_idx = flex.miller_index(indices_flex) mil_set = miller.set(crystal_symmetry=Symm, indices=mil_idx, anomalous_flag=True) mil_ar = {} # load a dict of "sfall at each energy" for i_chan, data_chan in enumerate(data): data_flex = flex.complex_double(np.ascontiguousarray(data_chan)) mil_ar[i_chan] = miller.array(mil_set, data=data_flex) return mil_ar, energies
def exercise_core_LS(target_class, verbose): n_refl = 10 f_calc = flex.polar( flex.random_double(n_refl) * 10 - 5, flex.random_double(n_refl) * 10 - 5) f_obs = flex.abs(f_calc) + (flex.random_double(n_refl) * 2 - 1) weights = flex.random_double(n_refl) r = xray.targets_least_squares_residual(f_obs, weights, f_calc, True, 0) scale_factor = r.scale_factor() gr_ana = r.derivatives() gr_fin = flex.complex_double() eps = 1.e-6 for i_refl in xrange(n_refl): gc = [] for i_part in [0, 1]: fc0 = f_calc[i_refl] ts = [] for signed_eps in [eps, -eps]: if (i_part == 0): f_calc[i_refl] = complex(fc0.real + signed_eps, fc0.imag) else: f_calc[i_refl] = complex(fc0.real, fc0.imag + signed_eps) r = xray.targets_least_squares_residual( f_obs, weights, f_calc, False, scale_factor) ts.append(r.target()) f_calc[i_refl] = fc0 gc.append((ts[0] - ts[1]) / (2 * eps)) gr_fin.append(complex(*gc)) if (verbose): print "ana:", list(gr_ana) print "fin:", list(gr_fin) assert approx_equal(gr_fin, gr_ana)
def exercise_singular_least_squares(): obs = flex.double([1.234]) weights_2345 = flex.double([2.345]) weights_zero = flex.double([0]) r_free_flags = flex.bool([False]) a = flex.double([0]) b = flex.double([0]) for obs_type in ["F", "I"]: for weights,scale_factor in [ (weights_2345, 3.456), (weights_zero, 0)]: tg = ext.targets_least_squares( compute_scale_using_all_data=False, obs_type=obs_type, obs=obs, weights=weights, r_free_flags=r_free_flags, f_calc=flex.complex_double(a, b), derivatives_depth=2, scale_factor=scale_factor) if (weights is weights_2345): assert approx_equal(tg.scale_factor(), scale_factor) assert list(tg.gradients_work()) == [0j] assert list(tg.hessians_work()) == [(1,1,1)] else: assert tg.scale_factor() is None assert tg.target_work() is None assert tg.target_test() is None assert tg.gradients_work().size() == 0 assert tg.hessians_work().size() == 0
def mtz_to_miller_array(mtz_object): """ Recombine intensities and phases (under 'IMEAN' and 'PHIB' labels, respectively) from input MTZ file into a Miller array object. Probably there's a CCTBX utility to accomplish this, but I haven't been able to locate it. Inputs: ------- mtz_object: a CCTBX MTZ object (from mtz.object(file_name=filename.mtz)) Outputs: -------- ma: a CCTBX Miller array object of complex structure factors """ # extract Miller arrays and crystal symmetry I_ma = mtz_object.as_miller_arrays_dict()[('crystal', 'dataset', 'IMEAN')] p_ma = mtz_object.as_miller_arrays_dict()[('crystal', 'dataset', 'PHIB')] assert list(I_ma.indices()) == list(p_ma.indices()) cs = mtz_object.crystals()[1].crystal_symmetry() # compute complex structure factors I, p = np.array(I_ma.data()), np.deg2rad(np.array(p_ma.data())) A, B = np.sqrt(I) * np.cos(p), np.sqrt(I) * np.sin(p) indices = I_ma.indices() sf_data = flex.complex_double(flex.double(A), flex.double(B)) # convert complex structure factors to CCTBX-style Miller array ma = miller.array(miller_set = miller.set(cs, indices, anomalous_flag=False), data = sf_data) return ma
def generate_table(complex_data, indices, numpy_args=False, anom=True): """ :param complex_data: structure factors :param indices: miller indices in a list, Nx3 :param numpy_args: are the complex data and indices numpy type, if not assume flex :param anom: return a miller array with +H and -H ? :return: dictionary whose keys are miller index tuple and values are structure fact """ sg = sgtbx.space_group(" P 4nw 2abw") Symm = crystal.symmetry(unit_cell=(79, 79, 38, 90, 90, 90), space_group=sg) if numpy_args: assert type(indices) == tuple assert (type(indices[0]) == tuple) indices = flex.miller_index(indices) mil_set = miller.set(crystal_symmetry=Symm, indices=indices, anomalous_flag=anom) if numpy_args: complex_data = flex.complex_double(np.ascontiguousarray(complex_data)) mil_ar = miller.array(mil_set, data=complex_data) mil_dict = {h: val for h, val in izip(mil_ar.indices(), mil_ar.data())} return mil_dict
def get_tg(self, k_sol, b_sol, u_star): fmodel_data = ext.core( f_calc=self.fmodel.f_calc().data(), shell_f_masks=self.f_masks, k_sols=k_sol, b_sols=b_sol, f_part1=flex.complex_double(self.fmodel.f_calc().data().size(), 0), f_part2=flex.complex_double(self.fmodel.f_calc().data().size(), 0), u_star=list(u_star), hkl=self.fmodel.f_calc().indices(), ss=self.fmodel.ss) return bss.bulk_solvent.ls_u_star( f_model_abs_no_k_total=flex.abs( fmodel_data.f_model_no_aniso_scale), f_obs=self.fmodel.f_obs().data(), miller_indices=self.fmodel.f_calc().indices(), k_anisotropic=fmodel_data.k_anisotropic)
def get_tg(self, k_sol, b_sol, u_star): fmodel_data = ext.core( f_calc = self.fmodel.f_calc().data(), shell_f_masks = self.f_masks, k_sols = k_sol, b_sols = b_sol, f_part1 = flex.complex_double(self.fmodel.f_calc().data().size(),0), f_part2 = flex.complex_double(self.fmodel.f_calc().data().size(),0), u_star = list(u_star), hkl = self.fmodel.f_calc().indices(), ss = self.fmodel.ss) return bss.bulk_solvent.ls_kb_sol_u_star( f_model = fmodel_data, f_obs = self.fmodel.f_obs().data(), scale = 10.0, kb_sol_grad = True, u_star_grad = True, kb_sol_curv = True)
def exercise_py_LS(obs, f_calc, weighting, verbose): weighting.computing_derivatives_wrt_f_c = True r = xray.unified_least_squares_residual(obs, weighting=weighting) rt = r(f_calc, compute_derivatives=True) if obs.is_xray_amplitude_array(): assert(isinstance(rt, xray.targets_least_squares_residual)) elif obs.is_xray_intensity_array(): assert(isinstance(rt, xray.targets_least_squares_residual_for_intensity)) scale_factor = rt.scale_factor() gr_ana = rt.derivatives() K = scale_factor w = weighting.weights if w is not None: w = w.deep_copy() dw_dfc = weighting.derivatives_wrt_f_c if dw_dfc is not None: dw_dfc = dw_dfc.deep_copy() y_o = obs.data() if w is None: w = flex.double(obs.size(), 1) sum_w_y_o_sqr = flex.sum(w * y_o * y_o) f_c = f_calc.data().deep_copy() if obs.is_xray_amplitude_array(): y_c = flex.abs(f_c) der = f_c * (1/y_c) elif obs.is_xray_intensity_array(): y_c = flex.norm(f_c) der = 2 * f_c gr_explicit = w*2*K*(K*y_c - y_o) * der / sum_w_y_o_sqr sum_w_squares = flex.sum(w*flex.pow2(K*y_c - y_o)) assert approx_equal(gr_ana, gr_explicit) gr_fin = flex.complex_double() eps = 1.e-6 for i_refl in xrange(obs.size()): gc = [] for i_part in [0,1]: fc0 = f_calc.data()[i_refl] ts = [] for signed_eps in [eps,-eps]: if (i_part == 0): f_calc.data()[i_refl] = complex(fc0.real + signed_eps, fc0.imag) else: f_calc.data()[i_refl] = complex(fc0.real, fc0.imag + signed_eps) rt = r(f_calc, compute_derivatives=False, scale_factor=scale_factor) ts.append(rt.target()) f_calc.data()[i_refl] = fc0 gc.append((ts[0]-ts[1])/(2*eps)) gr_fin.append(complex(*gc)) if (verbose): print "ana:", list(gr_ana) print "fin:", list(gr_fin) if dw_dfc is None: assert approx_equal(gr_fin, gr_ana) else: gr_total_ana = ( gr_ana + dw_dfc*(flex.pow2(K*y_c - y_o)/sum_w_y_o_sqr - sum_w_squares*flex.pow2(y_o)/sum_w_y_o_sqr**2) ) assert approx_equal(gr_fin, gr_total_ana)
def __init__( self, fmodel, coeffs): # XXX see f_model.py: duplication! Consolidate. self.fmodel = fmodel self.coeffs = coeffs crystal_gridding = fmodel.f_obs().crystal_gridding( d_min = self.fmodel.f_obs().d_min(), resolution_factor = 1./3) fft_map = miller.fft_map( crystal_gridding = crystal_gridding, fourier_coefficients = self.coeffs) fft_map.apply_sigma_scaling() map_data = fft_map.real_map_unpadded() rho_atoms = flex.double() for site_frac in self.fmodel.xray_structure.sites_frac(): rho_atoms.append(map_data.eight_point_interpolation(site_frac)) rho_mean = flex.mean_default(rho_atoms.select(rho_atoms>0.5), 0.5) sel_exclude = rho_atoms > min(rho_mean/2., 1) sites_cart = fmodel.xray_structure.sites_cart() # fft_map = miller.fft_map( crystal_gridding = crystal_gridding, fourier_coefficients = self.fmodel.f_model()) fft_map.apply_sigma_scaling() map_data2 = fft_map.real_map_unpadded() # for i_seq, site_cart in enumerate(sites_cart): selection = maptbx.grid_indices_around_sites( unit_cell = self.coeffs.unit_cell(), fft_n_real = map_data.focus(), fft_m_real = map_data.all(), sites_cart = flex.vec3_double([site_cart]), site_radii = flex.double([1.5])) cc = flex.linear_correlation(x=map_data.select(selection), y=map_data2.select(selection)).coefficient() if(cc<0.7): sel_exclude[i_seq] = False # del map_data, fft_map, rho_atoms self.d_min = fmodel.f_obs().d_min() cs = self.fmodel.f_obs().average_bijvoet_mates().complete_set(d_min=self.d_min) self.complete_set = cs.array(data = flex.double(cs.indices().size(), 0)) self.xray_structure_cut = self.fmodel.xray_structure.select(sel_exclude) self.missing_set = self.complete_set.common_set(self.coeffs) # self.f_calc_missing = self.complete_set.structure_factors_from_scatterers( xray_structure = self.xray_structure_cut).f_calc() self.ss_missing = 1./flex.pow2(self.f_calc_missing.d_spacings().data()) / 4. mask_manager = mmtbx.masks.manager( miller_array = self.f_calc_missing, miller_array_twin = None, mask_params = None) self.f_mask_missing = mask_manager.shell_f_masks( xray_structure = self.xray_structure_cut, force_update = True) self.zero_data = flex.complex_double(self.f_calc_missing.data().size(), 0)
def fft(self): if self.params.fft3d.reciprocal_space_grid.d_min is libtbx.Auto: # rough calculation of suitable d_min based on max cell # see also Campbell, J. (1998). J. Appl. Cryst., 31(3), 407-413. # fft_cell should be greater than twice max_cell, so say: # fft_cell = 2.5 * max_cell # then: # fft_cell = n_points * d_min/2 # 2.5 * max_cell = n_points * d_min/2 # a little bit of rearrangement: # d_min = 5 * max_cell/n_points max_cell = self.params.max_cell d_min = (5 * max_cell / self.params.fft3d.reciprocal_space_grid.n_points) d_spacings = 1 / self.reflections['rlp'].norms() self.params.fft3d.reciprocal_space_grid.d_min = max( d_min, min(d_spacings)) logger.info("Setting d_min: %.2f" % self.params.fft3d.reciprocal_space_grid.d_min) n_points = self.params.fft3d.reciprocal_space_grid.n_points self.gridding = fftpack.adjust_gridding_triple( (n_points, n_points, n_points), max_prime=5) n_points = self.gridding[0] self.map_centroids_to_reciprocal_space_grid() self.d_min = self.params.fft3d.reciprocal_space_grid.d_min logger.info("Number of centroids used: %i" % ((self.reciprocal_space_grid > 0).count(True))) #gb_to_bytes = 1073741824 #bytes_to_gb = 1/gb_to_bytes #(128**3)*8*2*bytes_to_gb #0.03125 #(256**3)*8*2*bytes_to_gb #0.25 #(512**3)*8*2*bytes_to_gb #2.0 fft = fftpack.complex_to_complex_3d(self.gridding) grid_complex = flex.complex_double( reals=self.reciprocal_space_grid, imags=flex.double(self.reciprocal_space_grid.size(), 0)) grid_transformed = fft.forward(grid_complex) #self.grid_real = flex.pow2(flex.abs(grid_transformed)) self.grid_real = flex.pow2(flex.real(grid_transformed)) #self.grid_real = flex.pow2(flex.imag(self.grid_transformed)) del grid_transformed if self.params.debug: self.debug_write_ccp4_map(map_data=self.grid_real, file_name="fft3d.map") if self.params.fft3d.peak_search == 'flood_fill': self.find_peaks() elif self.params.fft3d.peak_search == 'clean': self.find_peaks_clean()
def exercise_match_indices(): h0 = flex.miller_index(((1,2,3), (-1,-2,-3), (2,3,4), (-2,-3,-4), (3,4,5))) d0 = flex.double((1,2,3,4,5)) h1 = flex.miller_index(((-1,-2,-3), (-2,-3,-4), (1,2,3), (2,3,4))) d1 = flex.double((10,20,30,40)) mi = miller.match_indices(h0, h0) assert mi.have_singles() == 0 assert list(mi.pairs()) == zip(range(5), range(5)) mi = miller.match_indices(h0, h1) assert tuple(mi.singles(0)) == (4,) assert tuple(mi.singles(1)) == () assert tuple(mi.pairs()) == ((0,2), (1,0), (2,3), (3,1)) assert tuple(mi.pair_selection(0)) == (1, 1, 1, 1, 0) assert tuple(mi.single_selection(0)) == (0, 0, 0, 0, 1) assert tuple(mi.pair_selection(1)) == (1, 1, 1, 1) assert tuple(mi.single_selection(1)) == (0, 0, 0, 0) assert tuple(mi.paired_miller_indices(0)) \ == tuple(h0.select(mi.pair_selection(0))) l1 = list(mi.paired_miller_indices(1)) l2 = list(h1.select(mi.pair_selection(1))) l1.sort() l2.sort() assert l1 == l2 assert approx_equal(tuple(mi.plus(d0, d1)), (31, 12, 43, 24)) assert approx_equal(tuple(mi.minus(d0, d1)), (-29,-8,-37,-16)) assert approx_equal(tuple(mi.multiplies(d0, d1)), (30,20,120,80)) assert approx_equal(tuple(mi.divides(d0, d1)), (1/30.,2/10.,3/40.,4/20.)) assert approx_equal(tuple(mi.additive_sigmas(d0, d1)), [ math.sqrt(x*x+y*y) for x,y in ((1,30), (2,10), (3,40), (4,20))]) q = flex.size_t((3,2,0,4,1)) h1 = h0.select(q) assert tuple(miller.match_indices(h1, h0).permutation()) == tuple(q) p = miller.match_indices(h0, h1).permutation() assert tuple(p) == (2,4,1,0,3) assert tuple(h1.select(p)) == tuple(h0) cd0 = [ complex(a,b) for (a,b) in (1,1),(2,0),(3.5,-1.5),(5, -3),(-8,5.4) ] cd1 = [ complex(a,b) for (a,b) in (1,-1),(2,1),(0.5,1.5),(-1, -8),(10,0) ] cd2 = flex.complex_double(cd0) cd3 = flex.complex_double(cd1) mi = miller.match_indices(h0, h0) assert approx_equal(tuple(mi.plus(cd2,cd3)), ((2+0j), (4+1j), (4+0j), (4-11j), (2+5.4j)))
def get(O, derivatives_depth=0): if (O.target_type == "ls"): return ext.targets_least_squares( compute_scale_using_all_data=False, obs_type=O.obs_type, obs=O.obs, weights=O.weights, r_free_flags=O.r_free_flags, f_calc=flex.complex_double(O.a, O.b), derivatives_depth=derivatives_depth, scale_factor=O.scale_factor) if (O.target_type == "cc"): return ext.targets_correlation(obs_type=O.obs_type, obs=O.obs, weights=O.weights, r_free_flags=O.r_free_flags, f_calc=flex.complex_double( O.a, O.b), derivatives_depth=derivatives_depth) raise RuntimeError("Unknown target_type.")
def get(O, derivatives_depth=0): if (O.target_type == "ls"): return ext.targets_least_squares( compute_scale_using_all_data=False, obs_type=O.obs_type, obs=O.obs, weights=O.weights, r_free_flags=O.r_free_flags, f_calc=flex.complex_double(O.a, O.b), derivatives_depth=derivatives_depth, scale_factor=O.scale_factor) if (O.target_type == "cc"): return ext.targets_correlation( obs_type=O.obs_type, obs=O.obs, weights=O.weights, r_free_flags=O.r_free_flags, f_calc=flex.complex_double(O.a, O.b), derivatives_depth=derivatives_depth) raise RuntimeError("Unknown target_type.")
def fft(self): fft = fftpack.complex_to_complex_3d(self.gridding) grid_complex = flex.complex_double( reals=self.reciprocal_space_grid, imags=flex.double(self.reciprocal_space_grid.size(), 0)) grid_transformed = fft.forward(grid_complex) #self.grid_real = flex.pow2(flex.abs(grid_transformed)) self.grid_real = flex.abs(grid_transformed) #self.grid_real = flex.pow2(flex.real(grid_transformed)) #self.grid_real = flex.pow2(flex.imag(self.grid_transformed)) del grid_transformed
def get_tg(self, k_sol, b_sol, u_star): fmodel_data = ext.core( f_calc=self.fmodel.f_calc().data(), shell_f_masks=self.f_masks, k_sols=k_sol, b_sols=b_sol, f_part1=flex.complex_double(self.fmodel.f_calc().data().size(), 0), f_part2=flex.complex_double(self.fmodel.f_calc().data().size(), 0), u_star=list(u_star), hkl=self.fmodel.f_calc().indices(), ss=self.fmodel.ss) return bss.bulk_solvent.ls_kbp_sol_u_star( f_model=fmodel_data, f_obs=self.fmodel.f_obs().data(), scale=10.0, kb_sol_grad=True, p_sol_grad=False, u_star_grad=True, kb_sol_curv=True, p_sol_curv=False)
def fft(self): fft = fftpack.complex_to_complex_3d(self.gridding) grid_complex = flex.complex_double( reals=self.reciprocal_space_grid, imags=flex.double(self.reciprocal_space_grid.size(), 0), ) grid_transformed = fft.forward(grid_complex) # self.grid_real = flex.pow2(flex.abs(grid_transformed)) self.grid_real = flex.abs(grid_transformed) # self.grid_real = flex.pow2(flex.real(grid_transformed)) # self.grid_real = flex.pow2(flex.imag(self.grid_transformed)) del grid_transformed
def exercise_extract_miller_array_from_file(): from iotbx import reflection_file_utils as rfu from libtbx.test_utils import approx_equal log = null_out() sorry_counts = 0 crystal_symmetry = crystal.symmetry( unit_cell=(30,31,32,85,95,100), space_group_symbol="P 1") miller_set = miller.build_set( crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=3) size = miller_set.indices().size() a1 = miller_set.array( data=flex.hendrickson_lattman(size, (1,1,1,1))) a2 = miller_set.array(data=flex.double(size, 2)) a3 = miller_set.array(data=flex.double(size, 3)) a4 = miller_set.array(data=flex.complex_double(size, 4+4j)) a5 = miller_set.array(data=flex.complex_double(size, 5+5j)) # mtz_dataset = a1.as_mtz_dataset(column_root_label="A1") mtz_dataset.mtz_object().write("tmp.mtz") ma = rfu.extract_miller_array_from_file(file_name="tmp.mtz", log=log) assert type(ma.data()) == flex.hendrickson_lattman # mtz_dataset = a5.as_mtz_dataset(column_root_label="A5") mtz_dataset.mtz_object().write("tmp.mtz") ma = rfu.extract_miller_array_from_file(file_name="tmp.mtz", log=log) assert type(ma.data()) == flex.complex_double # for tp in [None, "complex"]: mtz_dataset = a4.as_mtz_dataset(column_root_label="A4") mtz_dataset.add_miller_array( miller_array=a5, column_root_label="A5") mtz_dataset.mtz_object().write("tmp.mtz") try: rfu.extract_miller_array_from_file(file_name="tmp.mtz",type=tp, log=log) except Sorry, e: assert ("Multiple choices available." in str(e)) sorry_counts += 1
def run(): target_structure = random_structure.xray_structure( space_group_info=sgtbx.space_group_info("I432"), elements=['C']*6+['O'], use_u_iso=False, use_u_aniso=True, ) shift = tuple(flex.random_double(3)) print "shift to be found: (%.3f, %.3f, %.3f)" % shift target_structure_in_p1 = target_structure.expand_to_p1().apply_shift(shift) miller_indices = miller.build_set( crystal_symmetry=target_structure, anomalous_flag=True, d_min=0.8) f_obs = miller_indices.structure_factors_from_scatterers( xray_structure=target_structure, algorithm="direct").f_calc().amplitudes() miller_indices_in_p1 = miller.build_set( crystal_symmetry=target_structure_in_p1, anomalous_flag=True, d_min=0.8) f_calc = miller_indices_in_p1.structure_factors_from_scatterers( xray_structure=target_structure_in_p1, algorithm="direct").f_calc() crystal_gridding = f_calc.crystal_gridding( symmetry_flags=translation_search.symmetry_flags( is_isotropic_search_model=False, have_f_part=False), resolution_factor=1/2 ) omptbx.env.num_threads = 1 t_fast_tf = show_times() fast_tf_map = translation_search.fast_nv1995( gridding=crystal_gridding.n_real(), space_group=f_obs.space_group(), anomalous_flag=f_obs.anomalous_flag(), miller_indices_f_obs=f_obs.indices(), f_obs=f_obs.data(), f_part=flex.complex_double(), ## no sub-structure is already fixed miller_indices_p1_f_calc=f_calc.indices(), p1_f_calc=f_calc.data()).target_map() print print "Fast translation function" t_fast_tf() t_cross_corr = show_times() for op in target_structure.space_group(): f, op_times_f = f_calc.original_and_transformed(op) cross_corr_map = miller.fft_map(crystal_gridding, f * op_times_f.conjugate().data()) print print "Traditional cross-correlation" t_cross_corr()
def algorithm_4(f_obs, F, phase_source, max_cycles=100, auto_converge_eps=1.e-7, use_cpp=True): """ Phased simultaneous search (alg4) """ fc, f_masks = F[0], F[1:] fc = fc.deep_copy() F = [fc] + F[1:] # C++ version if (use_cpp): return mosaic_ext.alg4([f.data() for f in F], f_obs.data(), phase_source.data(), max_cycles, auto_converge_eps) # Python version (1.2-3 times slower, but much more readable!) cntr = 0 x_prev = None while True: f_obs_cmpl = f_obs.phase_transfer(phase_source=phase_source) A = [] b = [] for j, Fj in enumerate(F): A_rows = [] for n, Fn in enumerate(F): Gjn = flex.real(Fj.data() * flex.conj(Fn.data())) A_rows.append(flex.sum(Gjn)) Hj = flex.real(Fj.data() * flex.conj(f_obs_cmpl.data())) b.append(flex.sum(Hj)) A.extend(A_rows) A = matrix.sqr(A) A_1 = A.inverse() b = matrix.col(b) x = A_1 * b # fc_d = flex.complex_double(phase_source.indices().size(), 0) for i, f in enumerate(F): fc_d += f.data() * x[i] phase_source = phase_source.customized_copy(data=fc_d) x_ = x[:] # cntr += 1 if (cntr > max_cycles): break if (x_prev is None): x_prev = x_[:] else: max_diff = flex.max( flex.abs(flex.double(x_prev) - flex.double(x_))) if (max_diff <= auto_converge_eps): break x_prev = x_[:] return x_
def check(): for obs_type in ["F", "I"]: tg = ext.targets_correlation(obs_type=obs_type, obs=obs, weights=weights, r_free_flags=None, f_calc=flex.complex_double(a, b), derivatives_depth=2) assert tg.cc() is None assert tg.target_work() is None assert tg.target_test() is None assert tg.gradients_work().size() == 0 assert tg.hessians_work().size() == 0
def _compute_f(self, mask_obj, ma, sel): result = [] ma_sel = ma.select(sel) for i in range(0, self.mask_params.n_radial_shells): if (self.mask_params.use_asu_masks): fm_asu = mask_obj.structure_factors(ma_sel.indices(), i + 1) else: assert self.mask_params.n_radial_shells == 1 fm_asu = mask_obj.structure_factors(ma_sel).data() data = flex.complex_double(ma.indices().size(), 0) data = data.set_selected(sel, fm_asu) result.append(ma.set().array(data=data)) return result
def gradients_work_fd(O, eps=1.e-6): result = flex.complex_double() for ih,a,b,f in zip(count(), O.a, O.b, O.r_free_flags): if (f): continue def fd(x, Ox): fs = [] for signed_eps in [eps, -eps]: Ox[ih] = x + signed_eps fs.append(O.get(derivatives_depth=0).target_work()) Ox[ih] = x return (fs[0]-fs[1])/(2*eps) result.append(complex(fd(a, O.a), fd(b, O.b))) return result
def exercise_extract_miller_array_from_file(): from iotbx import reflection_file_utils as rfu from libtbx.test_utils import approx_equal log = null_out() sorry_counts = 0 crystal_symmetry = crystal.symmetry(unit_cell=(30, 31, 32, 85, 95, 100), space_group_symbol="P 1") miller_set = miller.build_set(crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=3) size = miller_set.indices().size() a1 = miller_set.array(data=flex.hendrickson_lattman(size, (1, 1, 1, 1))) a2 = miller_set.array(data=flex.double(size, 2)) a3 = miller_set.array(data=flex.double(size, 3)) a4 = miller_set.array(data=flex.complex_double(size, 4 + 4j)) a5 = miller_set.array(data=flex.complex_double(size, 5 + 5j)) # mtz_dataset = a1.as_mtz_dataset(column_root_label="A1") mtz_dataset.mtz_object().write("tmp.mtz") ma = rfu.extract_miller_array_from_file(file_name="tmp.mtz", log=log) assert type(ma.data()) == flex.hendrickson_lattman # mtz_dataset = a5.as_mtz_dataset(column_root_label="A5") mtz_dataset.mtz_object().write("tmp.mtz") ma = rfu.extract_miller_array_from_file(file_name="tmp.mtz", log=log) assert type(ma.data()) == flex.complex_double # for tp in [None, "complex"]: mtz_dataset = a4.as_mtz_dataset(column_root_label="A4") mtz_dataset.add_miller_array(miller_array=a5, column_root_label="A5") mtz_dataset.mtz_object().write("tmp.mtz") try: rfu.extract_miller_array_from_file(file_name="tmp.mtz", type=tp, log=log) except Sorry, e: assert ("Multiple choices available." in str(e)) sorry_counts += 1
def exercise_asu(): sg_type = sgtbx.space_group_type("P 41") asu = sgtbx.reciprocal_space_asu(sg_type) miller_indices = flex.miller_index(((1, 2, 3), (3, 5, 0))) for h in miller_indices: h_eq = miller.sym_equiv_indices(sg_type.group(), h) for i_eq in xrange(h_eq.multiplicity(False)): h_i = h_eq(i_eq) for anomalous_flag in (False, True): a = miller.asym_index(sg_type.group(), asu, h_i.h()) assert a.h() == h o = a.one_column(anomalous_flag) assert o.i_column() == 0 t = a.two_column(anomalous_flag) assert t.h() == h assert (o.h() != h) == (t.i_column() == 1) assert not anomalous_flag or (t.i_column() != 0) == h_i.friedel_flag() assert anomalous_flag or t.i_column() == 0 miller.map_to_asu(sg_type, False, miller_indices) data = flex.double((0, 0)) miller.map_to_asu(sg_type, False, miller_indices, data) miller.map_to_asu(sg_type, False, miller_indices, data, False) miller.map_to_asu(sg_type, False, miller_indices, data, True) data = flex.complex_double((0, 0)) miller.map_to_asu(sg_type, False, miller_indices, data) data = flex.hendrickson_lattman(((1, 2, 3, 4), (2, 3, 4, 5))) miller.map_to_asu(sg_type, False, miller_indices, data) for sg_symbol in ("P 41", "P 31 1 2"): exercise_map_to_asu(sg_symbol) # sg_type = sgtbx.space_group_type("P 2") miller_indices = flex.miller_index(((1, 2, 3), (-1, -2, -3))) assert not miller.is_unique_set_under_symmetry( space_group_type=sg_type, anomalous_flag=False, miller_indices=miller_indices) assert miller.is_unique_set_under_symmetry(space_group_type=sg_type, anomalous_flag=True, miller_indices=miller_indices) assert list( miller.unique_under_symmetry_selection( space_group_type=sg_type, anomalous_flag=False, miller_indices=miller_indices)) == [0] assert list( miller.unique_under_symmetry_selection( space_group_type=sg_type, anomalous_flag=True, miller_indices=miller_indices)) == [0, 1]
def exercise_fast_terms(): space_group = sgtbx.space_group_info("P 21 21 21").group() miller_indices_f_obs = flex.miller_index(((3,4,5),(4,5,6))) f = translation_search.fast_terms( gridding=(20,20,36), anomalous_flag=False, miller_indices_p1_f_calc=flex.miller_index(((1,2,3),)), p1_f_calc=flex.complex_double((12,))) assert f.summation( space_group=space_group, miller_indices_f_obs=miller_indices_f_obs, m=flex.double((1,2)), f_part=None, squared_flag=False).fft().accu_real_copy().all() == (20,20,36)
def check(): for obs_type in ["F", "I"]: tg = ext.targets_correlation( obs_type=obs_type, obs=obs, weights=weights, r_free_flags=None, f_calc=flex.complex_double(a, b), derivatives_depth=2) assert tg.cc() is None assert tg.target_work() is None assert tg.target_test() is None assert tg.gradients_work().size() == 0 assert tg.hessians_work().size() == 0
def __init__(self, name, type): self.name = name self.type = type self.indices = flex.miller_index() self.has_non_zero_phases = None if (type == "real"): self.data = flex.double() elif (type == "complex"): self.data = flex.complex_double() self.has_non_zero_phases = False elif (type == "integer"): self.data = flex.int() else: raise RuntimeError("Internal Error.")
def f_calc_symmetrisations(f_obs, f_calc_in_p1, min_cc_peak_height): # The fast correlation map as per cctbx.translation_search.fast_nv1995 # is computed and its peaks studied. # Inspiration from phenix.substructure.hyss for the parameters tuning. if 0: # Display f_calc_in_p1 from crys3d.qttbx import map_viewer map_viewer.display(window_title="f_calc in P1 before fast CC", fft_map=f_calc_in_p1.fft_map(), iso_level_positive_range_fraction=0.8) crystal_gridding = f_obs.crystal_gridding( symmetry_flags=translation_search.symmetry_flags( is_isotropic_search_model=False, have_f_part=False), resolution_factor=1/3 ) correlation_map = translation_search.fast_nv1995( gridding=crystal_gridding.n_real(), space_group=f_obs.space_group(), anomalous_flag=f_obs.anomalous_flag(), miller_indices_f_obs=f_obs.indices(), f_obs=f_obs.data(), f_part=flex.complex_double(), ## no sub-structure is already fixed miller_indices_p1_f_calc=f_calc_in_p1.indices(), p1_f_calc=f_calc_in_p1.data()).target_map() if 0: # Display correlation_map from crys3d.qttbx import map_viewer map_viewer.display(window_title="Fast CC map", raw_map=correlation_map, unit_cell=f_calc_in_p1.unit_cell(), positive_iso_level=0.8) search_parameters = maptbx.peak_search_parameters( peak_search_level=1, peak_cutoff=0.5, interpolate=True, min_distance_sym_equiv=1e-6, general_positions_only=False, min_cross_distance=f_obs.d_min()/2) ## The correlation map is not a miller.fft_map, just a 3D flex.double correlation_map_peaks = crystal_gridding.tags().peak_search( map=correlation_map, parameters=search_parameters) # iterate over the strong peak; for each, shift and symmetrised f_calc for peak in correlation_map_peaks: if peak.height < min_cc_peak_height: break sr = symmetry_search.shift_refinement( f_obs, f_calc_in_p1, peak.site) yield sr.symmetrised_shifted_sf.f_x, sr.shift, sr.goos.correlation
def gradients_work_fd(O, eps=1.e-6): result = flex.complex_double() for ih, a, b, f in zip(count(), O.a, O.b, O.r_free_flags): if (f): continue def fd(x, Ox): fs = [] for signed_eps in [eps, -eps]: Ox[ih] = x + signed_eps fs.append(O.get(derivatives_depth=0).target_work()) Ox[ih] = x return (fs[0] - fs[1]) / (2 * eps) result.append(complex(fd(a, O.a), fd(b, O.b))) return result
def exercise_f_calc_map(): i = flex.miller_index(( (1,1,0), (-1,-1,0), (1,2,3), (3,2,1) )) f = flex.complex_double(( 1+1j, 2+2j, 3+3j, 4+4j )) f_map = miller.f_calc_map(i, f, anomalous_flag=True) assert f_map[(1,1,0)] == 1+1j assert f_map[(-1,-1,0)] == 2+2j assert f_map[(1,2,3)] == 3+3j assert f_map[(3,2,1)] == 4+4j assert f_map[(2,2,2)] == 0 f_map = miller.f_calc_map(i, f, anomalous_flag=False) assert f_map[(1,1,0)] == 2-2j assert f_map[(-1,-1,0)] == 2+2j assert f_map[(1,2,3)] == 3+3j assert f_map[(3,2,1)] == 4+4j assert f_map[(-1,-3,-5)] == 0
def exercise_asu(): sg_type = sgtbx.space_group_type("P 41") asu = sgtbx.reciprocal_space_asu(sg_type) miller_indices = flex.miller_index(((1,2,3), (3,5,0))) for h in miller_indices: h_eq = miller.sym_equiv_indices(sg_type.group(), h) for i_eq in xrange(h_eq.multiplicity(False)): h_i = h_eq(i_eq) for anomalous_flag in (False,True): a = miller.asym_index(sg_type.group(), asu, h_i.h()) assert a.h() == h o = a.one_column(anomalous_flag) assert o.i_column() == 0 t = a.two_column(anomalous_flag) assert t.h() == h assert (o.h() != h) == (t.i_column() == 1) assert not anomalous_flag or (t.i_column() != 0) == h_i.friedel_flag() assert anomalous_flag or t.i_column() == 0 miller.map_to_asu(sg_type, False, miller_indices) data = flex.double((0,0)) miller.map_to_asu(sg_type, False, miller_indices, data) miller.map_to_asu(sg_type, False, miller_indices, data, False) miller.map_to_asu(sg_type, False, miller_indices, data, True) data = flex.complex_double((0,0)) miller.map_to_asu(sg_type, False, miller_indices, data) data = flex.hendrickson_lattman(((1,2,3,4),(2,3,4,5))) miller.map_to_asu(sg_type, False, miller_indices, data) for sg_symbol in ("P 41", "P 31 1 2"): exercise_map_to_asu(sg_symbol) # sg_type = sgtbx.space_group_type("P 2") miller_indices = flex.miller_index(((1,2,3), (-1,-2,-3))) assert not miller.is_unique_set_under_symmetry( space_group_type=sg_type, anomalous_flag=False, miller_indices=miller_indices) assert miller.is_unique_set_under_symmetry( space_group_type=sg_type, anomalous_flag=True, miller_indices=miller_indices) assert list(miller.unique_under_symmetry_selection( space_group_type=sg_type, anomalous_flag=False, miller_indices=miller_indices)) == [0] assert list(miller.unique_under_symmetry_selection( space_group_type=sg_type, anomalous_flag=True, miller_indices=miller_indices)) == [0,1]
def exercise_flex_miller_index(): from scitbx.array_family.flex import exercise_triple exercise_triple(flex_triple=flex.miller_index, flex_order=flex.order) a = flex.miller_index([(1,2,3), (2,3,4)]) assert approx_equal(a.as_vec3_double(), [(1,2,3), (2,3,4)]) h, k, l = [flex.int((0,1,2,3)), flex.int((1,2,3,4)), flex.int((2,3,4,5))] b = flex.miller_index(h, k, l) assert approx_equal(b, ((0,1,2),(1,2,3),(2,3,4),(3,4,5))) # i = flex.miller_index([(1,-2,3), (-2,3,4)]) c = flex.complex_double([3-4j, -7+8j]) x = (0.9284, -1.2845, -0.2293) assert approx_equal( i.fourier_transform_real_part_at_x(fourier_coeffs=c, x=x), 15.4357188164)
def reset_all_scales(self): size = self.f_obs().data().size() zero_c = flex.complex_double(size,0) zero_d = flex.double(size,0) one_d = flex.double(size,1) f_part1_twin = self.f_calc_twin() f_part2_twin = self.f_calc_twin() if(f_part1_twin is not None): f_part1_twin = self.f_calc_twin().array(data=zero_c) f_part2_twin = self.f_calc_twin().array(data=zero_c) self.update_core( f_part1 = self.f_calc().array(data=zero_c), f_part2 = self.f_calc().array(data=zero_c), f_part1_twin = f_part1_twin, f_part2_twin = f_part2_twin, k_isotropic = one_d, k_anisotropic = one_d, k_mask = [zero_d]*len(self.k_masks()))
def main(filenames, map_file, npoints=192, max_resolution=6, reverse_phi=False): rec_range = 1 / max_resolution image = ImageFactory(filenames[0]) panel = image.get_detector()[0] beam = image.get_beam() s0 = beam.get_s0() pixel_size = panel.get_pixel_size() xlim, ylim = image.get_raw_data().all() xy = recviewer.get_target_pixels(panel, s0, xlim, ylim, max_resolution) s1 = panel.get_lab_coord(xy * pixel_size[0]) # FIXME: assumed square pixel s1 = s1 / s1.norms() * (1 / beam.get_wavelength()) # / is not supported... S = s1 - s0 grid = flex.double(flex.grid(npoints, npoints, npoints), 0) cnts = flex.int(flex.grid(npoints, npoints, npoints), 0) for filename in filenames: print "Processing image", filename try: fill_voxels(ImageFactory(filename), grid, cnts, S, xy, reverse_phi, rec_range) except: print " Failed to process. Skipped this." recviewer.normalize_voxels(grid, cnts) uc = uctbx.unit_cell((npoints, npoints, npoints, 90, 90, 90)) ccp4_map.write_ccp4_map(map_file, uc, sgtbx.space_group("P1"), (0, 0, 0), grid.all(), grid, flex.std_string(["cctbx.miller.fft_map"])) return from scitbx import fftpack fft = fftpack.complex_to_complex_3d(grid.all()) grid_complex = flex.complex_double( reals=flex.pow2(grid), imags=flex.double(grid.size(), 0)) grid_transformed = flex.abs(fft.backward(grid_complex)) print flex.max(grid_transformed), flex.min(grid_transformed), grid_transformed.all() ccp4_map.write_ccp4_map(map_file, uc, sgtbx.space_group("P1"), (0, 0, 0), grid.all(), grid_transformed, flex.std_string(["cctbx.miller.fft_map"]))
def fft(self): #gb_to_bytes = 1073741824 #bytes_to_gb = 1/gb_to_bytes #(128**3)*8*2*bytes_to_gb #0.03125 #(256**3)*8*2*bytes_to_gb #0.25 #(512**3)*8*2*bytes_to_gb #2.0 fft = fftpack.complex_to_complex_3d(self.gridding) grid_complex = flex.complex_double( reals=self.reciprocal_space_grid, imags=flex.double(self.reciprocal_space_grid.size(), 0)) grid_transformed = fft.forward(grid_complex) #self.grid_real = flex.pow2(flex.abs(grid_transformed)) self.grid_real = flex.pow2(flex.real(grid_transformed)) #self.grid_real = flex.pow2(flex.imag(self.grid_transformed)) del grid_transformed