def get_intensity_and_phase(complex_flex): real, imag = complex_flex.parts() intensity = real**2 + imag**2 intensity /= 1e10 print "Shifting intensity..." intensity = flex.double(fft.fftshift(intensity.as_numpy_array())) print intensity.focus() phase = flex.atan2(imag, real) * 180 / math.pi print "Shifting phases..." phase = flex.double(fft.fftshift(phase.as_numpy_array())) print phase.focus() return intensity, phase
def project_extrema(self, detector, scan_angle): coords = self.extrema_at_scan_angle(scan_angle) shadow_boundary = [] for p_id, p in enumerate(detector): # project coordinates onto panel plane a = p.get_D_matrix() * coords x, y, z = a.parts() valid = z > 0 x.set_selected(valid, x.select(valid) / z.select(valid)) y.set_selected(valid, y.select(valid) / z.select(valid)) if valid.count(True) < 3: # no shadow projected onto this panel shadow_boundary.append(flex.vec2_double()) continue # Compute convex hull of shadow points points = flex.vec2_double(x.select(valid), y.select(valid)) shadow = flex.vec2_double(_convex_hull(points)) shadow *= 1 / p.get_pixel_size()[0] shadow_orig = shadow.deep_copy() for i in (0, p.get_image_size()[0]): points = flex.vec2_double( flex.double(p.get_image_size()[1], i), flex.double_range(0, p.get_image_size()[1]), ) inside = is_inside_polygon(shadow_orig, points) # only add those points needed to define vertices of shadow inside_isel = inside.iselection() outside_isel = (~inside).iselection() while inside_isel.size(): j = inside_isel[0] shadow.append(points[j]) outside_isel = outside_isel.select(outside_isel > j) if outside_isel.size() == 0: shadow.append(points[inside_isel[-1]]) break sel = inside_isel >= outside_isel[0] if sel.count(True) == 0: shadow.append(points[inside_isel[-1]]) break inside_isel = inside_isel.select(sel) for i in (0, p.get_image_size()[1]): points = flex.vec2_double( flex.double_range(0, p.get_image_size()[0]), flex.double(p.get_image_size()[0], i), ) inside = is_inside_polygon(shadow_orig, points) # only add those points needed to define vertices of shadow inside_isel = inside.iselection() outside_isel = (~inside).iselection() while inside_isel.size(): j = inside_isel[0] shadow.append(points[j]) outside_isel = outside_isel.select(outside_isel > j) if outside_isel.size() == 0: shadow.append(points[inside_isel[-1]]) break sel = inside_isel >= outside_isel[0] if sel.count(True) == 0: shadow.append(points[inside_isel[-1]]) break inside_isel = inside_isel.select(sel) # Select only those vertices that are within the panel dimensions n_px = p.get_image_size() x, y = shadow.parts() valid = (x >= 0) & (x <= n_px[0]) & (y >= 0) & (y <= n_px[1]) shadow = shadow.select(valid) # sort vertices clockwise from centre of mass com = principal_axes_of_inertia_2d(shadow).center_of_mass() sx, sy = shadow.parts() shadow = shadow.select( flex.sort_permutation(flex.atan2(sy - com[1], sx - com[0])) ) shadow_boundary.append(shadow) return shadow_boundary
def plot_angles(coords, labels=None, show=False, plot_name=None): assert len(coords) >= 2 coord_x = coords[0] coord_y = coords[1] if len(coords) > 2: coord_z = coords[2] else: coord_z = None r = flex.sqrt(flex.pow2(coord_x) + flex.pow2(coord_y)) phi = flex.atan2(coord_y, coord_x) import math phi_deg = (180 / math.pi) * phi from matplotlib import pyplot as plt import numpy fig = plt.figure() if 0 and coord_z is not None: from mpl_toolkits.mplot3d import Axes3D # import dependency ax = fig.add_subplot(111, projection='3d') else: ax = fig.add_subplot(111) if labels is None: labels = flex.int(len(coord_x), -1) unique_labels = set(labels) unique_labels = sorted(unique_labels) n_clusters = len(unique_labels) - (1 if -1 in unique_labels else 0) colours = list(plt.cm.Spectral(numpy.linspace(0, 1, n_clusters))) if -1 in unique_labels: colours.insert(0, (255, 255, 255, 1)) for k, col in zip(unique_labels, colours): isel = (labels == k).iselection() if k == -1: # or len(class_members) < min_cluster_size: # Black used for noise. col = 'k' col = '0.25' # mid-grey markersize = 1 marker = '+' #continue else: markersize = 2 marker = 'o' if 0 and not isinstance(col, basestring) and len(col) == 4: # darken the edges frac = 0.75 edgecolor = [col[0] * frac, col[1] * frac, col[2] * frac, col[3]] else: edgecolor = col if coord_z is None: ax.scatter(phi_deg.select(isel), r.select(isel), s=markersize, marker=marker, c=col, edgecolor=edgecolor) else: ax.scatter(phi_deg.select(isel), r.select(isel), coord_z.select(isel), s=markersize, marker=marker, c=col, edgecolor=edgecolor) ax.set_xlim(-180, 180) ax.set_ylim(0, ax.get_ylim()[1]) ax.set_xlabel('Angle ($^{\circ}$)') ax.set_ylabel('Magnitude') if plot_name is not None: plt.savefig(plot_name, size_inches=(10, 10), dpi=300, bbox_inches='tight') if show: plt.show() plt.close(fig)
def project_extrema(self, detector, scan_angle): from dials.util import is_inside_polygon coords = self.extrema_at_scan_angle(scan_angle) shadow_boundary = [] for p_id, p in enumerate(detector): # project coordinates onto panel plane a = p.get_D_matrix() * coords x, y, z = a.parts() valid = z > 0 x.set_selected(valid, x.select(valid)/z.select(valid)) y.set_selected(valid, y.select(valid)/z.select(valid)) if valid.count(True) < 3: # no shadow projected onto this panel shadow_boundary.append(flex.vec2_double()) continue # Compute convex hull of shadow points points = flex.vec2_double(x.select(valid), y.select(valid)) shadow = flex.vec2_double(convex_hull(points)) shadow *= 1/p.get_pixel_size()[0] shadow_orig = shadow.deep_copy() for i in (0, p.get_image_size()[0]): points = flex.vec2_double(flex.double(p.get_image_size()[1], i), flex.double_range(0, p.get_image_size()[1])) inside = is_inside_polygon(shadow_orig, points) # only add those points needed to define vertices of shadow inside_isel = inside.iselection() outside_isel = (~inside).iselection() while inside_isel.size(): j = inside_isel[0] shadow.append(points[j]) outside_isel = outside_isel.select(outside_isel > j) if outside_isel.size() == 0: shadow.append(points[inside_isel[-1]]) break sel = inside_isel >= outside_isel[0] if sel.count(True) == 0: shadow.append(points[inside_isel[-1]]) break inside_isel = inside_isel.select(sel) for i in (0, p.get_image_size()[1]): points = flex.vec2_double(flex.double_range(0, p.get_image_size()[0]), flex.double(p.get_image_size()[0], i)) inside = is_inside_polygon(shadow_orig, points) # only add those points needed to define vertices of shadow inside_isel = inside.iselection() outside_isel = (~inside).iselection() while inside_isel.size(): j = inside_isel[0] shadow.append(points[j]) outside_isel = outside_isel.select(outside_isel > j) if outside_isel.size() == 0: shadow.append(points[inside_isel[-1]]) break sel = inside_isel >= outside_isel[0] if sel.count(True) == 0: shadow.append(points[inside_isel[-1]]) break inside_isel = inside_isel.select(sel) # Select only those vertices that are within the panel dimensions n_px = p.get_image_size() x, y = shadow.parts() valid = (x >= 0) & (x <= n_px[0]) & (y >= 0) & (y <= n_px[1]) shadow = shadow.select(valid) # sort vertices clockwise from centre of mass from scitbx.math import principal_axes_of_inertia_2d com = principal_axes_of_inertia_2d(shadow).center_of_mass() sx, sy = shadow.parts() shadow = shadow.select( flex.sort_permutation(flex.atan2(sy-com[1],sx-com[0]))) shadow_boundary.append(shadow) return shadow_boundary