예제 #1
0
def run():

    vector_define_phi0 = [0.0, 0.0, 1.0]

    phi_plane_normal = ip.source_position[0]

    phi0_vector = find_vector_component_in_phi_plane.run(
        vector_define_phi0, phi_plane_normal)

    all_gsqr_phi_bins = []

    for k, current_image in enumerate(ip.image_filename):

        ################################################################################################################
        # The following section opens the image and sets up some data structures that are required for the code to function.

        output_folder = "output_" + current_image[:-4]

        raw_pixel_value = io.imread(current_image)

        working_pixel_value = raw_pixel_value

        working_pixel_value = np.nan_to_num(working_pixel_value)

        subpixel_value = make_subpixel_array.run(working_pixel_value,
                                                 ip.num_subpixels_height,
                                                 ip.num_subpixels_width)

        make_plot_from_array_1.run(subpixel_value, "subpixel_image.png",
                                   "viridis", "none", output_folder, False)

        make_tif_from_array.run(subpixel_value, "subpixel_image.tif",
                                output_folder)

        make_plot_from_array_1.run(subpixel_value, "subpixel_image_log.png",
                                   "viridis", "none", output_folder, True)

        working_pixel_value = subpixel_value

        working_height, working_width = np.shape(working_pixel_value)

        filter_angles_deg = np.empty((working_height, working_width))
        gsqr = np.empty((working_height, working_width))
        phi = np.empty((working_height, working_width))
        attenuation_correction = np.empty((working_height, working_width))
        pixel_value_corrected_for_attenuation = np.empty(
            (working_height, working_width))
        polarisation_angles_deg = np.empty((working_height, working_width))
        bragg_angles_deg = np.empty((working_height, working_width))
        vector_origin_to_pixels = [int(0)] * working_height * working_width

        ################################################################################################################
        # The following section assigns gsqr and phi values to each of the pixels.

        print "Initialised..."

        central_point, width_mm_per_pixel, height_mm_per_pixel, norm_view_x, norm_view_y, vector_origin_to_central_pixel, \
        unit_vector_source_to_origin, adjust_to_centre_of_pixel = work_out_common_results.run(
            working_width, working_height, ip.x_scale[k], ip.y_scale[k], ip.view_x[k], ip.view_y[k], ip.offset[k], ip.normal[k],
            ip.source_position[k])

        binary_directory = "binaries/" + output_folder

        array_data_filenames = [
            "filter_angles", "gsqr", "phi", "polarisation_angles",
            "bragg_angle"
        ]

        array_data_list = [
            filter_angles_deg, gsqr, phi, polarisation_angles_deg,
            bragg_angles_deg
        ]

        list_data_filenames = ["vector_origin_to_pixels"]

        list_data = [vector_origin_to_pixels]

        if ip.use_previous_pixel_loop is False:

            print "Calculating values for each pixel..."

            filter_angles_deg, gsqr, phi, vector_origin_to_pixels, polarisation_angles_deg, bragg_angles_deg = \
                loop_through_pixels.run(
                    working_height, working_width, ip.wavelength, ip.a_lattice, norm_view_x, norm_view_y,
                    central_point, width_mm_per_pixel, height_mm_per_pixel, vector_origin_to_central_pixel,
                    unit_vector_source_to_origin, adjust_to_centre_of_pixel, phi_plane_normal, ip.normal[k], filter_angles_deg,
                    gsqr, phi, vector_origin_to_pixels, polarisation_angles_deg, phi0_vector, bragg_angles_deg)

            save_pixel_data_to_binary_files.run(array_data_filenames,
                                                array_data_list,
                                                list_data_filenames, list_data,
                                                binary_directory)
        else:

            print "Loading values for each pixel..."

            filter_angles_deg, gsqr, phi, polarisation_angles_deg, bragg_angles_deg, vector_origin_to_pixels = \
                load_pixel_data_from_binary_files.run(array_data_filenames, list_data_filenames, binary_directory)

        make_plot_from_array_1.run(gsqr, "gsqr_map.png", "viridis", "none",
                                   output_folder, False)

        make_tif_from_array.run(gsqr, 'gsqr_map.tif', output_folder)

        make_plot_from_array_1.run(phi, "phi_map.png", "viridis", "none",
                                   output_folder, False)

        make_tif_from_array.run(phi, 'phi_map.tif', output_folder)

        make_plot_from_array_1.run(filter_angles_deg, "filter_angle_map.png",
                                   "viridis", "none", output_folder, False)

        make_tif_from_array.run(filter_angles_deg, 'filter_angle_map.tif',
                                output_folder)

        make_plot_from_array_1.run(polarisation_angles_deg,
                                   "polarisation_angle_map.png", "viridis",
                                   "none", output_folder, False)

        make_tif_from_array.run(polarisation_angles_deg,
                                'polarisation_angle_map.tif', output_folder)

        make_plot_from_array_1.run(bragg_angles_deg, "bragg_angle_map.png",
                                   "viridis", "none", output_folder, False)

        make_tif_from_array.run(bragg_angles_deg, 'bragg_angle_map.tif',
                                output_folder)

        ################################################################################################################
        # The following section applies a correction to the intensity for each pixel due to attenuation by any filters.

        if ip.correct_for_filter_attenuation is True:

            pixel_value_corrected_for_attenuation, attenuation_correction = compensate_for_filters.run(
                working_height, working_width, filter_angles_deg,
                working_pixel_value, ip.filter_attenuation_length_at_90_deg[k],
                ip.filter_thickness[k], attenuation_correction,
                pixel_value_corrected_for_attenuation)

            working_pixel_value = pixel_value_corrected_for_attenuation

            make_plot_from_array_1.run(
                attenuation_correction,
                "filter_attenuation_correction_map.png", "viridis", "none",
                output_folder, False)

            make_tif_from_array.run(attenuation_correction,
                                    'filter_attenuation_correction_map.tif',
                                    output_folder)

            pixel_value_with_only_filter_correction, attenuation_correction = compensate_for_filters.run(
                working_height, working_width, filter_angles_deg,
                working_pixel_value, ip.filter_attenuation_length_at_90_deg[k],
                ip.filter_thickness[k], attenuation_correction,
                pixel_value_corrected_for_attenuation)

            make_tif_from_array.run(pixel_value_with_only_filter_correction,
                                    'image_corrected_filter_ONLY.tif',
                                    output_folder)

            make_plot_from_array_1.run(pixel_value_with_only_filter_correction,
                                       "image_corrected_filter_ONLY.png",
                                       "viridis", "none", output_folder, False)

        ################################################################################################################
        # The following section applies a correction to the intensity for each pixel due to sample attenuation.

        if ip.correct_for_sample_attenuation is True:

            pixel_value_corrected_for_sample_attenuation, sample_attenuation_correction = compensate_for_sample_attenuation.run(
                working_pixel_value, ip.sample_normal[k],
                ip.source_position[k], vector_origin_to_pixels)

            make_tif_from_array.run(sample_attenuation_correction,
                                    'sample_attenuation_correction_map.tif',
                                    output_folder)

            make_plot_from_array_1.run(
                sample_attenuation_correction,
                "sample_attenuation_correction_map.png", "viridis", "none",
                output_folder, True)

            working_pixel_value = pixel_value_corrected_for_sample_attenuation

            pixel_value_with_only_sample_correction, sample_attenuation_correction = compensate_for_sample_attenuation.run(
                subpixel_value, ip.sample_normal[k], ip.source_position[k],
                vector_origin_to_pixels)

            make_tif_from_array.run(
                pixel_value_with_only_sample_correction,
                'image_corrected_sample_attenuation_ONLY.tif', output_folder)

            make_plot_from_array_1.run(
                pixel_value_with_only_sample_correction,
                "image_corrected_sample_attenuation_ONLY.png", "viridis",
                "none", output_folder, True)

        ################################################################################################################
        # The following section applies a correction to the intensity for each pixel due to the lorentz factor.

        if ip.correct_for_lorentz_polarisation_factor is True:

            pixel_value_corrected_for_lorentz_polarisation_factor, lorentz_polarisation_correction = compensate_for_lorentz_polarisation_factor.run(
                working_pixel_value, working_height, working_width,
                bragg_angles_deg)

            working_pixel_value = pixel_value_corrected_for_lorentz_polarisation_factor

            make_plot_from_array_1.run(
                lorentz_polarisation_correction,
                "lorentz_polarisation_correction_map.png", "viridis", "none",
                output_folder, False)

            make_tif_from_array.run(lorentz_polarisation_correction,
                                    'lorentz_polarisation_correction_map.tif',
                                    output_folder)

            pixel_value_with_only_lp_correction, lorentz_polarisation_correction = compensate_for_lorentz_polarisation_factor.run(
                subpixel_value, working_height, working_width,
                bragg_angles_deg)

            make_tif_from_array.run(pixel_value_with_only_lp_correction,
                                    'image_corrected_lp_ONLY.tif',
                                    output_folder)

            make_plot_from_array_1.run(pixel_value_with_only_lp_correction,
                                       "image_corrected_lp_ONLY.png",
                                       "viridis", "none", output_folder, False)

        ################################################################################################################
        # The following section applies a correction to the intensity for each pixel due to the lorentz factor.

        if ip.correct_for_atomic_form_factor is True:

            pixel_value_corrected_for_atomic_form_factor, atomic_form_factor_correction = compensate_for_NIOBIUM_atomic_form_factor.run(
                working_pixel_value, working_height, working_width,
                bragg_angles_deg, output_folder, ip.wavelength)

            working_pixel_value = pixel_value_corrected_for_atomic_form_factor

            make_plot_from_array_1.run(
                atomic_form_factor_correction,
                "atomic_form_factor_correction_map.png", "viridis", "none",
                output_folder, False)

            make_tif_from_array.run(atomic_form_factor_correction,
                                    'atomic_form_factor_correction_map.tif',
                                    output_folder)

            pixel_value_with_only_atomic_correction, atomic_form_factor_correction = compensate_for_NIOBIUM_atomic_form_factor.run(
                subpixel_value, working_height, working_width,
                bragg_angles_deg, output_folder, ip.wavelength)

            make_plot_from_array_1.run(pixel_value_with_only_atomic_correction,
                                       "image_corrected_atomic_ONLY.png",
                                       "viridis", "none", output_folder, False)

            make_tif_from_array.run(pixel_value_with_only_atomic_correction,
                                    'image_corrected_atomic_ONLY.tif',
                                    output_folder)

        ################################################################################################################
        # This section makes an image with all the applied corrections.

        make_plot_from_array_1.run(working_pixel_value,
                                   "corrections_applied_" + current_image,
                                   "viridis", "none", output_folder, False)

        make_tif_from_array.run(working_pixel_value,
                                "corrections_applied_" + current_image,
                                output_folder)

        total_correction_array = np.ones((working_height, working_width))

        if ip.correct_for_filter_attenuation is True:

            total_correction_array *= attenuation_correction

        if ip.correct_for_sample_attenuation is True:

            total_correction_array *= sample_attenuation_correction

        if ip.correct_for_lorentz_polarisation_factor is True:

            total_correction_array *= lorentz_polarisation_correction

        if ip.correct_for_atomic_form_factor is True:

            total_correction_array *= atomic_form_factor_correction

        make_tif_from_array.run(total_correction_array,
                                "total_corrections.tif", output_folder)

        make_plot_from_array_1.run(total_correction_array,
                                   "total_correction_map.png", "viridis",
                                   "none", output_folder, False)

        make_plot_from_array_1.run(total_correction_array,
                                   "total_correction_map_logged.png",
                                   "viridis", "none", output_folder, True)

        ################################################################################################################
        # The following section sorts the pixels from the original image into bins according to the gsqr and phi values
        # determined previously.

        gsqr_phi_bins, gsqr_phi_bin_pixel_counter, gsqr_bins, phi_bins = make_bins_for_theta_phi.run(
            ip.num_gsqr_bins, ip.num_phi_bins, ip.gsqr_limit[k],
            ip.phi_limit[k])

        gsqr_phi_bins, gsqr_phi_bin_pixel_counter, dumped_pixel_counter = populate_theta_phi_bins.run(
            working_width, working_height, gsqr, phi, gsqr_bins, phi_bins,
            gsqr_phi_bins, gsqr_phi_bin_pixel_counter, working_pixel_value,
            subpixel_value, ip.gsqr_limit[k], ip.phi_limit[k],
            ip.minimum_pixels_per_bin)

        all_gsqr_phi_bins.append(gsqr_phi_bins)

        make_plot_from_array_2.run(
            gsqr_phi_bins, "phi_vs_gsqr.png", "viridis", "none", output_folder,
            False, [
                ip.gsqr_limit[0][0], ip.gsqr_limit[0][1], ip.phi_limit[0][1],
                ip.phi_limit[0][0]
            ])

        make_tif_from_array.run(gsqr_phi_bins, "phi_vs_gsqr.tif",
                                output_folder)

        #make_plot_from_array.run(gsqr_phi_bins, "phi_vs_gsqr_log.png", "viridis", "none", output_folder, True)

        ################################################################################################################
        # The following section integrates along phi.

        intensity_integrated_along_phi, intensity_summed_along_phi = integrate_along_phi.run(
            gsqr_phi_bins, gsqr_phi_bin_pixel_counter,
            ip.minimum_pixels_in_column)

        make_simple_plot.run(gsqr_bins, intensity_integrated_along_phi[0], "c",
                             "$|G^2|$", "Intensity $(arb.)$",
                             "Integrated intensity vs. $|G^2|$",
                             "integrated_intensity_vs_gsqr.png", output_folder)

        make_simple_plot.run(gsqr_bins, intensity_summed_along_phi[0], "r",
                             "$|G^2|$", "Intensity $(arb.)$",
                             "Summed intensity vs. $|G^2|$",
                             "summed_intensity_vs_gsqr.png", output_folder)

        write_data_to_file_type_1.run("integrated_intensity_vs_gsqr.dat",
                                      gsqr_bins,
                                      intensity_integrated_along_phi,
                                      output_folder)

        write_data_to_file_type_1.run("summed_intensity_vs_gsqr.dat",
                                      gsqr_bins, intensity_summed_along_phi,
                                      output_folder)

        print "Completed image " + str(k) + "..."

        ################################################################################################################

    output_folder = "output_all" + ip.image_filename[0]

    gsqr, integrated_intensity = compile_multiple_integrated_intensities.run(
        gsqr_bins, phi_bins, ip.image_filename)

    gsqr, summed_intensity = compile_multiple_summed_intensities.run(
        gsqr_bins, ip.image_filename)

    master_gsqr_phi_bins = compile_multiple_gsqr_vs_phi.run(all_gsqr_phi_bins)

    min_phi, max_phi = find_phi_extents.run(master_gsqr_phi_bins, phi_bins,
                                            gsqr_bins)

    write_data_to_file_type_3.run('phi_extent.dat', gsqr_bins, min_phi,
                                  max_phi, output_folder)

    make_simple_plot.run(gsqr, integrated_intensity, "c", "$|G^2|$",
                         "Intensity $(arb.)$",
                         "Integrated intensity vs. $|G^2|$",
                         "integrated_intensity_vs_gsqr.png", output_folder)

    make_simple_plot.run(gsqr, summed_intensity, "c", "$|G^2|$",
                         "Summed Intensity $(arb.)$",
                         "Summed intensity vs. $|G^2|$",
                         "summed_intensity_vs_gsqr.png", output_folder)

    write_data_to_file_type_2.run("integrated_intensity_vs_gsqr.dat", gsqr,
                                  integrated_intensity, output_folder)

    write_data_to_file_type_2.run("summed_intensity_vs_gsqr.dat", gsqr,
                                  summed_intensity, output_folder)

    make_plot_from_array_2.run(master_gsqr_phi_bins, "phi_vs_gsqr.png",
                               "viridis", "none", output_folder, False, [
                                   ip.gsqr_limit[0][0], ip.gsqr_limit[0][1],
                                   ip.phi_limit[0][1], ip.phi_limit[0][0]
                               ])

    make_tif_from_array.run(master_gsqr_phi_bins, "phi_vs_gsqr.tif",
                            output_folder)

    make_plot_from_array_2.run(master_gsqr_phi_bins, "phi_vs_gsqr_log.png",
                               "viridis", "none", output_folder, True, [
                                   ip.gsqr_limit[0][0], ip.gsqr_limit[0][1],
                                   ip.phi_limit[0][1], ip.phi_limit[0][0]
                               ])

    print "Finished!"
예제 #2
0
    print len(filter_attenuation_correction_factor_list[i])
    make_mask_image.run("filter_" + str(i) + "_attenuation_factor_mask.png",
                        "attenuation_factor",
                        filter_attenuation_correction_factor_list[i], width,
                        height)

make_mask_image.run("thomsons_attenuation_factor_mask.png",
                    "attenuation_factor",
                    thomson_attenuation_correction_factor_list, width, height)

############################################

im_theta_phi = build_image_skeleton.run(ip.theta_phi_n_pixels_width,
                                        ip.theta_phi_n_pixels_height)

gsqr_bin_list, phi_bin_list, image_bin_list, gsqr_bin_width, phi_bin_height = make_bins_for_theta_phi.run(
    im_theta_phi, gsqr, phi)

image_bin_list = populate_theta_phi_bins.run(gsqr, phi, pixel_value,
                                             gsqr_bin_list, phi_bin_list,
                                             image_bin_list, gsqr_bin_width,
                                             phi_bin_height)

image_bin_list = sum_pixel_values.run(image_bin_list, pixel_value)

gsqr_integrated_intensity = sum_along_theta.run(image_bin_list, gsqr_bin_list,
                                                phi_bin_list)

plot_integrated_intensity.run(gsqr_bin_list, gsqr_integrated_intensity)

write_data_to_file.run("integrated_intensity_vs_gsqr.dat", gsqr_bin_list,
                       gsqr_integrated_intensity)
예제 #3
0
def main():

    # The following value keeps track of whether phi_0 has been defined yet for the transformation. After the first
    # iteration this variable is set to true, which skips the define_phi_0 function.

    occurrence_phi_0_definition = False

    # These lists are collected for checking by pytest.

    all_initial_pixel_value_lists = []
    all_subpixel_lists = []
    all_filter_attenuation_correction_lists = []
    all_sample_attenuation_correction_lists = []
    all_thomson_attenuation_correction_lists = []
    all_final_pixel_values_lists = []
    all_image_bin_lists = []

    for i in range(len(ip.image_files)):

        ######################################
        # This section performs a transformation into theta-phi space.

        im, width, height, pix, initial_pixel_values, initial_total_pixel_value = \
            initialise.run(
                ip.image_files[i], ip.debug, i)

        pix, subpixel_list, width, height = \
            make_subpixels.run(
                pix, width, height, ip.num_width_subpixels, ip.num_height_subpixels, ip.debug)

        central_pixel, width_mm_per_pixel, height_mm_per_pixel, norm_view_x, norm_view_y, \
        vector_origin_to_central_pixel, unit_vector_source_to_origin, adjust_to_centre_of_pixel = \
            work_out_common_results.run(
                width, height, ip.x_scale[i], ip.y_scale[i], ip.view_x[i], ip.view_y[i], ip.offset[i], ip.normal[i],
                ip.source_position[i], ip.debug)

        if occurrence_phi_0_definition is False:
            unit_phi_plane_normal, phi_0_vector = define_phi_0.run(
                ip.source_position[i], ip.phi_0_definer, ip.debug)
            occurrence_phi_0_definition = True

        else:
            unit_phi_plane_normal = unit_phi_plane_normal
            phi_0_vector = phi_0_vector

        filter_angles_list_deg, thomson_angles_list_deg, gsqr, pixel_value, phi = \
            calc_theta_phi.run(
                width, height, pix, ip.phi_limit, ip.gsqr_limit, ip.wavelength, ip.a_lattice, ip.normal[i], 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, unit_phi_plane_normal, phi_0_vector, ip.debug)

        ##########################################
        # The following functions apply corrections for thomson polarisation, filter attenuation, and the sample absorption.

        pixel_value, filter_attenuation_correction_factor_list = \
            compensate_for_filters.run(
                filter_angles_list_deg, pixel_value, ip.filter_attenuation_length[i], ip.filter_thickness[i], ip.debug)

        # The thomson_angles_list_deg is the angle from source -> origin -> pixel, and is the same list of angles required by
        # the sample attenuation correction.
        pixel_value, sample_attenuation_correction_factor_list = \
            compensate_for_sample_attenuation.run(
                thomson_angles_list_deg, pixel_value, ip.source_position[i], ip.sample_normal[i])

        pixel_value, thomson_attenuation_correction_factor_list = \
            compensate_for_thomson_factor.run(
                thomson_angles_list_deg, pixel_value, ip.debug)

        ##########################################
        # This section makes the images of masks for filter and thomson corrections.

        make_mask_image.run(
            "filter_angles_mask.png", "degrees", filter_angles_list_deg, width,
            height, [min(filter_angles_list_deg),
                     max(filter_angles_list_deg)], ip.debug)

        make_mask_image.run(
            "thomson_angles_polarisation_mask.png", "degrees",
            thomson_angles_list_deg, width, height,
            [min(thomson_angles_list_deg),
             max(thomson_angles_list_deg)], ip.debug)

        for j in range(len(ip.filter_thickness)):
            make_mask_image.run(
                "filter_" + str(j) + "_attenuation_factor_mask.png",
                "attenuation_factor",
                filter_attenuation_correction_factor_list[j], width, height, [
                    min(filter_attenuation_correction_factor_list[j]),
                    max(filter_attenuation_correction_factor_list[j])
                ], ip.debug)

        make_mask_image.run(
            "thomsons_attenuation_factor_mask.png", "attenuation_factor",
            thomson_attenuation_correction_factor_list, width, height, [
                min(thomson_attenuation_correction_factor_list),
                max(thomson_attenuation_correction_factor_list)
            ], ip.debug)

        ############################################
        # This section bins the pixels according to their gsqr-phi values calculated above.

        gsqr_bin_list, phi_bin_list, image_bin_list = \
            make_bins_for_theta_phi.run(
                ip.num_gsqr_bins, ip.num_phi_bins, gsqr, phi, ip.debug)

        image_bin_list = \
            populate_theta_phi_bins.run(
                gsqr, phi, pixel_value, gsqr_bin_list, phi_bin_list, image_bin_list, ip.debug)

        image_bin_list = \
            sum_pixel_values_in_bins.run(
                image_bin_list, pixel_value, ip.debug)

        gsqr_integrated_intensity = \
            sum_along_theta.run(
                image_bin_list, gsqr_bin_list, phi_bin_list, ip.debug)

        plot_integrated_intensity.run(gsqr_bin_list, gsqr_integrated_intensity,
                                      ip.name_plot_integrated_intensity,
                                      ip.debug)

        write_data_to_file.run("integrated_intensity_vs_gsqr.dat",
                               gsqr_bin_list, gsqr_integrated_intensity,
                               ip.debug)

        build_theta_phi_image.run('normalised_gsqr_vs_phi.tif', image_bin_list,
                                  ip.num_gsqr_bins, ip.num_phi_bins,
                                  gsqr_bin_list, phi_bin_list, True, ip.debug)

        build_theta_phi_image.run('preserved_gsqr_vs_phi.tif', image_bin_list,
                                  ip.num_gsqr_bins, ip.num_phi_bins,
                                  gsqr_bin_list, phi_bin_list, False, ip.debug)

        check_image_total_pixel_value.run(initial_total_pixel_value,
                                          'preserved_gsqr_vs_phi.tif',
                                          ip.debug)

        if ip.plot is True:
            plt.scatter(gsqr, pixel_value, 1)
            plt.xlabel("$G^2$")
            plt.ylabel("PSL")
            plt.savefig('gsqr_vs_intensity.png')

        sort_data_files.run(ip.image_files[i], i)

        all_initial_pixel_value_lists.append(initial_pixel_values)
        all_subpixel_lists.append(subpixel_list)
        all_filter_attenuation_correction_lists.append(
            filter_attenuation_correction_factor_list)

        print "Finished with " + ip.image_files[i] + "\n"

    return all_initial_pixel_value_lists, all_subpixel_lists, all_filter_attenuation_correction_lists