def from_dict(d): ''' Convert the dictionary to a crystal model Params: d The dictionary of parameters Returns: The crystal model ''' from dxtbx.model import Crystal # If None, return None if d is None: return None # Check the version and id if str(d['__id__']) != "crystal": raise ValueError("\"__id__\" does not equal \"crystal\"") # Extract from the dictionary real_space_a = d['real_space_a'] real_space_b = d['real_space_b'] real_space_c = d['real_space_c'] # str required to force unicode to ascii conversion space_group = str("Hall:" + d['space_group_hall_symbol']) xl = Crystal(real_space_a, real_space_b, real_space_c, space_group_symbol=space_group) # Isoforms used for stills try: xl.identified_isoform = d['identified_isoform'] except KeyError: pass # Extract scan point setting matrices, if present try: A_at_scan_points = d['A_at_scan_points'] xl.set_A_at_scan_points(A_at_scan_points) except KeyError: pass # Extract covariance of B, if present try: cov_B = d['B_covariance'] xl.set_B_covariance(cov_B) except KeyError: pass # Extract covariance of B at scan points, if present cov_B_at_scan_points = d.get('B_covariance_at_scan_points') if cov_B_at_scan_points is not None: from scitbx.array_family import flex cov_B_at_scan_points = flex.double(cov_B_at_scan_points).as_1d() cov_B_at_scan_points.reshape(flex.grid(xl.num_scan_points, 9, 9)) xl.set_B_covariance_at_scan_points(cov_B_at_scan_points) return xl
def test_crystal_with_scan_points(example_crystal): c1 = Crystal(**example_crystal) A = c1.get_A() c1.set_A_at_scan_points([A for i in range(5)]) # Set the B covariance. The values are nonsense, just ensure they are # all different cov_B = flex.double(range(9 * 9)) * 1e-5 c1.set_B_covariance(cov_B) cov_B.reshape(flex.grid(1, 9, 9)) cov_B_array = flex.double(flex.grid(5, 9, 9)) for i in range(5): cov_B_array[i:(i + 1), :, :] = cov_B c1.set_B_covariance_at_scan_points(cov_B_array) cov_B = c1.get_B_covariance() d = c1.to_dict() c2 = CrystalFactory.from_dict(d) eps = 1e-9 for Acomp in d["A_at_scan_points"]: for e1, e2 in zip(A, Acomp): assert abs(e1 - e2) <= eps for covBcomp in d["B_covariance_at_scan_points"]: for e1, e2 in zip(cov_B, covBcomp): assert abs(e1 - e2) <= eps assert c1 == c2
def test_crystal_with_scan_points(): from dxtbx.model import Crystal, CrystalFactory from scitbx import matrix real_space_a = matrix.col((35.2402102454, -7.60002142787, 22.080026774)) real_space_b = matrix.col((22.659572494, 1.47163505925, -35.6586361881)) real_space_c = matrix.col((5.29417246554, 38.9981792999, 4.97368666613)) c1 = Crystal( real_space_a=real_space_a, real_space_b=real_space_b, real_space_c=real_space_c, space_group_symbol="P 1 2/m 1", ) A = c1.get_A() c1.set_A_at_scan_points([A for i in range(5)]) # Set the B covariance. The values are nonsense, just ensure they are # all different from scitbx.array_family import flex cov_B = flex.double(range((9 * 9))) * 1e-5 c1.set_B_covariance(cov_B) cov_B.reshape(flex.grid(1, 9, 9)) cov_B_array = flex.double(flex.grid(5, 9, 9)) for i in range(5): cov_B_array[i : (i + 1), :, :] = cov_B c1.set_B_covariance_at_scan_points(cov_B_array) cov_B = c1.get_B_covariance() d = c1.to_dict() c2 = CrystalFactory.from_dict(d) eps = 1e-9 for Acomp in d["A_at_scan_points"]: for e1, e2 in zip(A, Acomp): assert abs(e1 - e2) <= eps for covBcomp in d["B_covariance_at_scan_points"]: for e1, e2 in zip(cov_B, covBcomp): assert abs(e1 - e2) <= eps assert c1 == c2