def test_calc_angle_between_vectors(): assert calc_angle_between_vectors.run([1.0, 0.0, 0.0], [0.0, 0.0, 1.0]) == 90.0 assert calc_angle_between_vectors.run([2.0, 1.0, 3.0], [-2.0, -1.0, -3.0]) == 180.0 assert calc_angle_between_vectors.run([-2.0, -1.0, -3.0], [2.0, 1.0, 3.0]) == 180.0
def run(working_pixel_value, sample_normal, source, vector_origin_to_pixels): import calc_angle_between_vectors import numpy as np print "Compensate for sample attenuation..." source_normal_angle = calc_angle_between_vectors.run( source, sample_normal) source_origin_incident_angle = 90.0 - source_normal_angle incident_angle_term = 1.0/np.sin(np.deg2rad(source_origin_incident_angle)) normal_angle_term = 1.0/np.sin(np.deg2rad(90.0)) working_height, working_width = np.shape(working_pixel_value) corrected_pixel_value = np.zeros((working_height, working_width)) sample_correction_factor = np.zeros((working_height, working_width)) for i in range(working_height): for j in range(working_width): current_vector_origin_pixel = vector_origin_to_pixels[(i * working_width) + j] if current_vector_origin_pixel is int(0): correction_factor = 0 sample_correction_factor[i][j] = correction_factor corrected_pixel_value[i][j] = working_pixel_value[i][j] * correction_factor else: angle_normal_origin_pixel_deg = abs(calc_angle_between_vectors.run(current_vector_origin_pixel, sample_normal)) diffraction_angle = 90.0 - angle_normal_origin_pixel_deg diffraction_angle_term = 1.0/np.sin(np.deg2rad(diffraction_angle)) ratio_diffracted_ray_over_normal_ray = (normal_angle_term + incident_angle_term) / (diffraction_angle_term + incident_angle_term) sample_correction_factor[i][j] = 1.0 / ratio_diffracted_ray_over_normal_ray corrected_pixel_value[i][j] = working_pixel_value[i][j] * sample_correction_factor[i][j] if sample_correction_factor[i][j] < 0.0: print "#### WARNING: NEGATIVE SAMPLE ATTENUATION CORRECTION FACTOR ####" return corrected_pixel_value, sample_correction_factor
def run(width, height, pix, phi_limit, gsqr_limit, wavelength, a_lattice, normal, norm_view_x, norm_view_y, central_pixel, width_mm_per_pixel, height_mm_per_pixel, vector_origin_to_central_pixel, unit_vector_source_to_origin, adjust_to_centre_of_pixel, phi0_plane_normal): import give_gsqr_value import calc_plane_from_two_vectors import calc_angle_between_vectors print "Calculating gsqr and phi for each pixel..." filter_angles_list_deg = [] thomson_angles_list_deg = [] gsqr = [] pixel_value = [] phi = [] for i in range(width): for j in range(height): current_pixel_value = pix[i, j] current_gsqr, vector_origin_to_current_pixel = give_gsqr_value.run( i, j, wavelength, a_lattice, normal, norm_view_x, norm_view_y, central_pixel, width_mm_per_pixel, height_mm_per_pixel, vector_origin_to_central_pixel, unit_vector_source_to_origin, adjust_to_centre_of_pixel, width, height) current_normal_to_plane = calc_plane_from_two_vectors.run( vector_origin_to_current_pixel, [0.0, 0.0, 1.0]) current_phi_deg = calc_angle_between_vectors.run( current_normal_to_plane, phi0_plane_normal) current_filter_angle_deg = calc_angle_between_vectors.run( vector_origin_to_current_pixel, normal) current_thomson_angle_deg = calc_angle_between_vectors.run( vector_origin_to_current_pixel, unit_vector_source_to_origin) if current_phi_deg >= phi_limit[0] and current_phi_deg <= phi_limit[ 1] and current_gsqr >= gsqr_limit[ 0] and current_gsqr <= gsqr_limit[ 1]: # Enforces the phi and gsqr limits. filter_angles_list_deg.append(abs(current_filter_angle_deg)) thomson_angles_list_deg.append(abs(current_thomson_angle_deg)) gsqr.append(current_gsqr) phi.append(current_phi_deg) pixel_value.append(current_pixel_value) else: continue return filter_angles_list_deg, thomson_angles_list_deg, gsqr, pixel_value, phi
def run(source_origin_pixel_angle_list_deg, pixel_value, source, sample_normal): import calc_angle_between_vectors import numpy as np # This function will correct the intensity of each pixel, since the x-rays are attenuated by travelling through the # diffracting sample. This means that there will be an angular dependence on the correction to each pixel. print "Compensating for sample attenuation..." new_pixel_value = [] correction_factor_list = [] source_normal_angle = calc_angle_between_vectors.run(source, sample_normal) source_origin_incident_angle = 90.0 - source_normal_angle for i in range(len(source_origin_pixel_angle_list_deg)): current_pixel_value = pixel_value[i] current_source_origin_pixel_angle = source_origin_pixel_angle_list_deg[ i] sample_pixel_diffraction_angle_deg = 180.0 - current_source_origin_pixel_angle - source_origin_incident_angle sample_pixel_diffraction_angle_rad = np.deg2rad( sample_pixel_diffraction_angle_deg) source_origin_incident_angle_rad = np.deg2rad( source_origin_incident_angle) # The following are used to calculate the correction factor due to sample absorption. diffraction_angle_term = (1 / np.sin(sample_pixel_diffraction_angle_rad)) incident_angle_term = (1 / np.sin(source_origin_incident_angle_rad)) complete_angle_term = diffraction_angle_term + incident_angle_term # The following term is the factor which, when multiplied by the source intensity, will give us the intensity # at the point of detection. sample_absorption_term = 1.0 / complete_angle_term # Since we are trying to correct for attenuation, we multiply the pixel intensity by the # inverse of the absorption term so as to simulate the intensity as if the sample were not # attenuating the diffraction signal. current_correction_factor = 1.0 / sample_absorption_term current_pixel_value = current_pixel_value * current_correction_factor new_pixel_value.append(current_pixel_value) correction_factor_list.append(current_correction_factor) return new_pixel_value, correction_factor_list
def run(vector_origin_pixel, unit_phi_plane_normal, phi_0_vector): import numpy as np import calc_angle_between_vectors import give_phi_sign # First collapse the vector_origin_pixel onto the phi_0_plane: length_vector_origin_pixel = np.linalg.norm(vector_origin_pixel) unit_vector_origin_pixel = vector_origin_pixel / length_vector_origin_pixel parallel_component_origin_pixel = unit_phi_plane_normal * np.dot( unit_vector_origin_pixel, unit_phi_plane_normal) origin_pixel_vector_in_plane = unit_vector_origin_pixel - parallel_component_origin_pixel # Then calculate the angle between these vectors: phi_deg = calc_angle_between_vectors.run(origin_pixel_vector_in_plane, phi_0_vector) phi_deg = give_phi_sign.run(phi_deg, phi_0_vector, origin_pixel_vector_in_plane, unit_phi_plane_normal) return phi_deg
if ip.debug == True: print "\n###### COLUMN " + str(i) + " ############" for j in range(height): current_pixel_value = pix[i, j] current_gsqr, vector_origin_to_current_pixel = give_gsqr_value.run( i, j, ip.wavelength, ip.a_lattice, ip.normal, norm_view_x, norm_view_y, central_pixel, width_mm_per_pixel, height_mm_per_pixel, vector_origin_to_central_pixel, unit_vector_source_to_origin, adjust_to_centre_of_pixel, width, height) current_normal_to_plane = calc_plane_from_two_vectors.run( vector_origin_to_current_pixel, [0.0, 0.0, 1.0]) current_phi_deg = calc_angle_between_vectors.run( current_normal_to_plane, phi0_plane_normal) current_filter_angle_deg = calc_angle_between_vectors.run( vector_origin_to_current_pixel, ip.normal) if current_phi_deg >= ip.phi_limit[ 0] and current_phi_deg <= ip.phi_limit[ 1]: # Enforces the phi limits. filter_angles_list_deg.append(abs(current_filter_angle_deg)) gsqr.append(current_gsqr) phi.append(current_phi_deg) pixel_value.append(current_pixel_value) else: continue
def run(working_height, working_width, wavelength, a_lattice, norm_view_x, norm_view_y, central_point, width_mm_per_pixel, height_mm_per_pixel, vector_origin_to_central_point, unit_vector_source_to_origin, adjust_to_centre_of_pixel, phi_plane_normal, normal, filter_angles_deg, gsqr, phi, vector_origin_to_pixels, polarisation_angles_deg, phi0_vector, bragg_angles_deg): import calc_angle_between_vectors import find_vector_component_in_phi_plane import numpy as np for i in range(working_height): for j in range(working_width): # numpy arrays have shape = (working_height, working_width) current_pixel = [i, j] pixel_difference = [0, 0] pixel_difference[0] = -(current_pixel[0] - central_point[0]) pixel_difference[1] = current_pixel[1] - central_point[1] mm_difference = [ pixel_difference[0] * height_mm_per_pixel, pixel_difference[1] * width_mm_per_pixel ] mm_difference_centre_to_current = [ mm_difference[0] * norm_view_y, mm_difference[1] * norm_view_x ] vector_origin_to_current_pixel_top_left = vector_origin_to_central_point + mm_difference_centre_to_current[0] + \ mm_difference_centre_to_current[1] vector_origin_to_current_pixel_centre = vector_origin_to_current_pixel_top_left + \ adjust_to_centre_of_pixel[0] + adjust_to_centre_of_pixel[1] distance_origin_to_current_pixel_centre = np.linalg.norm( vector_origin_to_current_pixel_centre) unit_vector_origin_to_current_pixel_centre = vector_origin_to_current_pixel_centre / distance_origin_to_current_pixel_centre g = (unit_vector_origin_to_current_pixel_centre - unit_vector_source_to_origin) * (1 / wavelength) g = np.linalg.norm(g) / (1.0 / a_lattice) current_gsqr = g**2 unit_vector_component_in_phi_plane = find_vector_component_in_phi_plane.run( vector_origin_to_current_pixel_centre, phi_plane_normal) current_bragg_angle = 0.5 * abs( calc_angle_between_vectors.run( unit_vector_source_to_origin, unit_vector_origin_to_current_pixel_centre)) current_phi_deg = calc_angle_between_vectors.run( unit_vector_component_in_phi_plane, phi0_vector) current_filter_angle_deg = calc_angle_between_vectors.run( vector_origin_to_current_pixel_centre, normal) current_polarisation_angle = abs( calc_angle_between_vectors.run( unit_vector_source_to_origin, vector_origin_to_current_pixel_centre)) if current_polarisation_angle > 90.0: # This corrects for the case where the polarisation angle > 90.0. This angle should have a maximum value # of 90.0. current_polarisation_angle = 90.0 - abs( current_polarisation_angle - 90.0) filter_angles_deg[i][j] = abs(current_filter_angle_deg) gsqr[i][j] = current_gsqr phi[i][j] = current_phi_deg vector_origin_to_pixels[(i * working_width) + j] = vector_origin_to_current_pixel_centre polarisation_angles_deg[i][j] = abs(current_polarisation_angle) bragg_angles_deg[i][j] = current_bragg_angle return filter_angles_deg, gsqr, phi, vector_origin_to_pixels, polarisation_angles_deg, bragg_angles_deg
def run(width, height, pix, phi_limit, gsqr_limit, wavelength, a_lattice, normal, norm_view_x, norm_view_y, central_pixel, width_mm_per_pixel, height_mm_per_pixel, vector_origin_to_central_pixel, unit_vector_source_to_origin, adjust_to_centre_of_pixel, phi_0_plane_normal, phi_0_vector, debug): import give_gsqr_value import calc_angle_between_vectors import calc_phi_deg print "Calculating gsqr and phi for each pixel..." # This first set of lists is used to hold all of the values that fit within our phi and gsqr limits. These will \ # later be used for the other calculations in the code. filter_angles_list_deg = [] thomson_angles_list_deg = [] gsqr = [] pixel_value = [] phi = [] # This second set of list holds ALL of the values for every pixel; no pixel's values are filtered out including # those that exist outside of out phi and gsqr limits. These are only used to make the mask images of the # attenuation corrections. complete_filter_angles_list_deg = [] complete_thomson_angles_list_deg = [] complete_gsqr = [] complete_pixel_value = [] complete_phi = [] for i in range(width): for j in range(height): current_pixel_value = pix[i, j] current_gsqr, vector_origin_to_current_pixel = give_gsqr_value.run( i, j, wavelength, a_lattice, norm_view_x, norm_view_y, central_pixel, width_mm_per_pixel, height_mm_per_pixel, vector_origin_to_central_pixel, unit_vector_source_to_origin, adjust_to_centre_of_pixel, width, height) current_phi_deg = calc_phi_deg.run(vector_origin_to_current_pixel, phi_0_plane_normal, phi_0_vector) current_filter_angle_deg = calc_angle_between_vectors.run( vector_origin_to_current_pixel, normal) current_thomson_angle_deg = calc_angle_between_vectors.run( vector_origin_to_current_pixel, unit_vector_source_to_origin) complete_filter_angles_list_deg.append( abs(current_filter_angle_deg)) complete_thomson_angles_list_deg.append( abs(current_thomson_angle_deg)) complete_gsqr.append(current_gsqr) complete_phi.append(current_phi_deg) complete_pixel_value.append(current_pixel_value) if phi_limit[0] <= current_phi_deg <= phi_limit[1] and gsqr_limit[ 0] <= current_gsqr <= gsqr_limit[1]: filter_angles_list_deg.append(abs(current_filter_angle_deg)) thomson_angles_list_deg.append(abs(current_thomson_angle_deg)) gsqr.append(current_gsqr) phi.append(current_phi_deg) pixel_value.append(current_pixel_value) else: continue debug_message = ( "\n\n~~~~~~~~~~~~~\nFILENAME = " + __name__ + ".py" + "\n\nINPUTS:" + "\nworking_width = " + str(width) + "\nworking_height = " + str(height) + "\npix = " + str(pix) + "\nphi_limit = " + str(phi_limit) + "\ngsqr_limit = " + str(gsqr_limit) + "\nwavelength = " + str(wavelength) + "\na_lattice = " + str(a_lattice) + "\nnormal = " + str(normal) + "\nnorm_view_x = " + str(norm_view_x) + "\nnorm_view_y = " + str(norm_view_y) + "\ncentral_pixel = " + str(central_pixel) + "\nwidth_mm_per_pixel = " + str(width_mm_per_pixel) + "\nheight_mm_per_pixel = " + str(height_mm_per_pixel) + "\nvector_origin_to_central_pixel = " + str(vector_origin_to_central_pixel) + "\nunit_vector_source_to_origin = " + str(unit_vector_source_to_origin) + "\nadjust_to_centre_of_pixel = " + str(adjust_to_centre_of_pixel) + "\nphi_0_plane_normal = " + str(phi_0_plane_normal) + "\nphi_0_vector = " + str(phi_0_vector) + "\ndebug = " + str(debug) + "\n\nOUTPUTS:" + "\nfilter_angles_deg = " + str(filter_angles_list_deg) + "\nthomson_angles_list_deg = " + str(thomson_angles_list_deg) + "\ngsqr = " + str(gsqr) + "\nraw_pixel_value = " + str(pixel_value) + "\nphi = " + str(phi) + "\n~~~~~~~~~~~~~~~") if debug: print debug_message return filter_angles_list_deg, thomson_angles_list_deg, gsqr, pixel_value, phi, complete_filter_angles_list_deg, \ complete_thomson_angles_list_deg, complete_gsqr, complete_pixel_value, complete_phi
def run(width, height, pix, phi_limit, gsqr_limit, wavelength, a_lattice, normal, norm_view_x, norm_view_y, central_pixel, width_mm_per_pixel, height_mm_per_pixel, vector_origin_to_central_pixel, unit_vector_source_to_origin, adjust_to_centre_of_pixel, phi_0_plane_normal, phi_0_vector, debug): import give_gsqr_value import calc_angle_between_vectors import calc_phi_deg print "Calculating gsqr and phi for each pixel..." filter_angles_list_deg = [] thomson_angles_list_deg = [] gsqr = [] pixel_value = [] phi = [] for i in range(width): for j in range(height): current_pixel_value = pix[i, j] current_gsqr, vector_origin_to_current_pixel = give_gsqr_value.run( i, j, wavelength, a_lattice, norm_view_x, norm_view_y, central_pixel, width_mm_per_pixel, height_mm_per_pixel, vector_origin_to_central_pixel, unit_vector_source_to_origin, adjust_to_centre_of_pixel, width, height) current_phi_deg = calc_phi_deg.run(vector_origin_to_current_pixel, phi_0_plane_normal, phi_0_vector) current_filter_angle_deg = calc_angle_between_vectors.run( vector_origin_to_current_pixel, normal) current_thomson_angle_deg = calc_angle_between_vectors.run( vector_origin_to_current_pixel, unit_vector_source_to_origin) if phi_limit[0] <= current_phi_deg <= phi_limit[1] and gsqr_limit[ 0] <= current_gsqr <= gsqr_limit[1]: filter_angles_list_deg.append(abs(current_filter_angle_deg)) thomson_angles_list_deg.append(abs(current_thomson_angle_deg)) gsqr.append(current_gsqr) phi.append(current_phi_deg) pixel_value.append(current_pixel_value) else: continue debug_message = ( "\n\n~~~~~~~~~~~~~\nFILENAME = " + __name__ + ".py" + "\n\nINPUTS:" + "\nworking_width = " + str(width) + "\nworking_height = " + str(height) + "\npix = " + str(pix) + "\nphi_limit = " + str(phi_limit) + "\ngsqr_limit = " + str(gsqr_limit) + "\nwavelength = " + str(wavelength) + "\na_lattice = " + str(a_lattice) + "\nnormal = " + str(normal) + "\nnorm_view_x = " + str(norm_view_x) + "\nnorm_view_y = " + str(norm_view_y) + "\ncentral_pixel = " + str(central_pixel) + "\nwidth_mm_per_pixel = " + str(width_mm_per_pixel) + "\nheight_mm_per_pixel = " + str(height_mm_per_pixel) + "\nvector_origin_to_central_pixel = " + str(vector_origin_to_central_pixel) + "\nunit_vector_source_to_origin = " + str(unit_vector_source_to_origin) + "\nadjust_to_centre_of_pixel = " + str(adjust_to_centre_of_pixel) + "\nphi_0_plane_normal = " + str(phi_0_plane_normal) + "\nphi_0_vector = " + str(phi_0_vector) + "\ndebug = " + str(debug) + "\n\nOUTPUTS:" + "\nfilter_angles_deg = " + str(filter_angles_list_deg) + "\nthomson_angles_list_deg = " + str(thomson_angles_list_deg) + "\ngsqr = " + str(gsqr) + "\nraw_pixel_value = " + str(pixel_value) + "\nphi = " + str(phi) + "\n~~~~~~~~~~~~~~~") if debug: print debug_message return filter_angles_list_deg, thomson_angles_list_deg, gsqr, pixel_value, phi