|
10 | 10 |
|
11 | 11 | from annotated_types import Not |
12 | 12 | import h5py |
13 | | -import matplotlib.pyplot as plt |
| 13 | +import plotly.graph_objects as go |
| 14 | +import plotly.express as px |
| 15 | +from plotly.subplots import make_subplots |
| 16 | +from plotly.offline import iplot |
14 | 17 | import numpy as np |
15 | 18 | import pandas as pd |
16 | 19 | from IPython.display import HTML, display |
|
24 | 27 | # from scipy.constants import h, c, electron_volt, m_n |
25 | 28 | # from timepix_geometry_correction.correct import TimepixGeometryCorrection |
26 | 29 |
|
27 | | -MARKERSIZE = 2 |
| 30 | +MARKERSIZE = 6 |
28 | 31 |
|
29 | 32 | class NormalizedData: |
30 | 33 | data= {} |
@@ -85,46 +88,49 @@ def normalization_with_list_of_full_path( |
85 | 88 | roi = None, |
86 | 89 | container_roi = None, |
87 | 90 | container_roi_file = None) -> NormalizedData: |
88 | | - """normalize the sample data with ob data using proton charge and shutter counts |
| 91 | + |
| 92 | + # """ |
| 93 | + # normalize the sample data with ob data using proton charge and shutter counts |
89 | 94 |
|
90 | | - Args: |
91 | | - sample_dict (dict): dictionary with sample run numbers and their data |
92 | | - {base_name_run1: {'full_path': full_path, 'nexus': nexus_path}, |
93 | | - base_name_run2: {'full_path': full_path, 'nexus': nexus_path}, ...} |
94 | | -
|
95 | | - ob_dict (dict): dictionary with ob run numbers and their data |
96 | | - {base_name_run1: {'full_path': full_path, 'nexus': nexus_path}, |
97 | | - base_name_run2: {'full_path': full_path, 'nexus': nexus_path}, ...} |
98 | | -
|
99 | | - dc_dict (dict): dictionary with dc run numbers and their data |
100 | | - {base_name_run1: {'full_path': full_path, 'nexus': nexus_path}, |
101 | | - base_name_run2: {'full_path': full_path, 'nexus': nexus_path}, ...} |
102 | | -
|
103 | | - output_folder (str): folder to save the output data |
104 | | - verbose (bool): if True, display additional information |
105 | | - combine_samples (bool): if True, combine sample runs |
106 | | - proton_charge_flag (bool): if True, normalize by proton charge |
107 | | - monitor_counts_flag (bool): if True, normalize by monitor counts |
108 | | - shutter_counts_flag (bool): if True, normalize by shutter counts |
109 | | - replace_ob_zeros_by_nan_flag (bool): if True, replace OB zeros by NaN |
110 | | - replace_ob_zeros_by_local_median_flag (bool): if True, replace OB zeros by local median |
111 | | - kernel_size_for_local_median (Tuple[int, int, int]): kernel size for local median (y, x, tof) |
112 | | - max_iterations (int): maximum number of iterations for local median |
113 | | - output_tif (bool): if True, export the data as tif files |
114 | | - instrument (str): instrument name |
115 | | - detector_delay_us (float): detector delay in microseconds |
116 | | - preview (bool): if True, display preview of the data |
117 | | - distance_source_detector_m (float): distance from source to detector in meters |
118 | | - correct_chips_alignment_flag (bool): if True, correct chips alignment |
119 | | - correct_chips_alignment_config (dict): configuration for chips alignment correction |
120 | | - export_mode (dict): dictionary with export options |
121 | | - roi (Roi): region of interest for full spectrum normalization |
122 | | - container_roi (Roi): region of interest for container only normalization |
123 | | - container_roi_file (str): file path to container ROI file (scitiff format) (will take precedence over container_roi if both are provided) |
124 | | -
|
125 | | - Returns: |
126 | | - normalized_data | np.ndarray: normalized data |
127 | | - """ |
| 95 | + # Args: |
| 96 | + # sample_dict (dict): dictionary with sample run numbers and their data |
| 97 | + # {base_name_run1: {'full_path': full_path, 'nexus': nexus_path}, |
| 98 | + # base_name_run2: {'full_path': full_path, 'nexus': nexus_path}, ...} |
| 99 | + |
| 100 | + # ob_dict (dict): dictionary with ob run numbers and their data |
| 101 | + # {base_name_run1: {'full_path': full_path, 'nexus': nexus_path}, |
| 102 | + # base_name_run2: {'full_path': full_path, 'nexus': nexus_path}, ...} |
| 103 | + |
| 104 | + # dc_dict (dict): dictionary with dc run numbers and their data |
| 105 | + # {base_name_run1: {'full_path': full_path, 'nexus': nexus_path}, |
| 106 | + # base_name_run2: {'full_path': full_path, 'nexus': nexus_path}, ...} |
| 107 | + |
| 108 | + # output_folder (str): folder to save the output data |
| 109 | + # verbose (bool): if True, display additional information |
| 110 | + # combine_samples (bool): if True, combine sample runs |
| 111 | + # proton_charge_flag (bool): if True, normalize by proton charge |
| 112 | + # monitor_counts_flag (bool): if True, normalize by monitor counts |
| 113 | + # shutter_counts_flag (bool): if True, normalize by shutter counts |
| 114 | + # replace_ob_zeros_by_nan_flag (bool): if True, replace OB zeros by NaN |
| 115 | + # replace_ob_zeros_by_local_median_flag (bool): if True, replace OB zeros by local median |
| 116 | + # kernel_size_for_local_median (Tuple[int, int, int]): kernel size for local median (y, x, tof) |
| 117 | + # max_iterations (int): maximum number of iterations for local median |
| 118 | + # output_tif (bool): if True, export the data as tif files |
| 119 | + # instrument (str): instrument name |
| 120 | + # detector_delay_us (float): detector delay in microseconds |
| 121 | + # preview (bool): if True, display preview of the data |
| 122 | + # distance_source_detector_m (float): distance from source to detector in meters |
| 123 | + # correct_chips_alignment_flag (bool): if True, correct chips alignment |
| 124 | + # correct_chips_alignment_config (dict): configuration for chips alignment correction |
| 125 | + # export_mode (dict): dictionary with export options |
| 126 | + # roi (Roi): region of interest for full spectrum normalization |
| 127 | + # container_roi (Roi): region of interest for container only normalization |
| 128 | + # container_roi_file (str): file path to container ROI file (scitiff format) (will take precedence over container_roi if both are provided) |
| 129 | + |
| 130 | + # Returns: |
| 131 | + # normalized_data | np.ndarray: normalized data |
| 132 | + |
| 133 | + # """ |
128 | 134 |
|
129 | 135 | initialize_logging() |
130 | 136 |
|
@@ -615,103 +621,10 @@ def normalization_with_list_of_full_path( |
615 | 621 | spectra_array=spectra_array, |
616 | 622 | spectra_file=sample_master_dict[_sample_run_number][MasterDictKeys.spectra_file_name]) |
617 | 623 |
|
618 | | - # if combine_samples: |
619 | | - |
620 | | - # # combine all normalized data |
621 | | - # array_of_normalized_data = [] |
622 | | - # for _key in normalized_data.keys(): |
623 | | - # array_of_normalized_data.append(normalized_data[_key]) |
624 | | - |
625 | | - # combined_normalized_data = np.nanmean(np.array(array_of_normalized_data), axis=0) |
626 | | - # combined_spectrum_normalized_data = np.nanmean(np.array(list(spectrum_normalized_data.values())), axis=0) |
627 | | - # dict_to_return.data['combined'] = combined_normalized_data |
628 | | - |
629 | | - # # if preview, display the combined normalized data |
630 | | - # if preview: |
631 | | - |
632 | | - # fig, axs3 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) |
633 | | - # normalized_data_integrated = np.nanmean(combined_normalized_data, axis=0) |
634 | | - # im2 = axs3[0].imshow(normalized_data_integrated, cmap="gray") |
635 | | - # plt.colorbar(im2, ax=axs3[0]) |
636 | | - # axs3[0].set_title(f"Integrated combined Normalized data") |
637 | | - |
638 | | - # _label = "pixel by pixel normalization profile of full image" |
639 | | - # if roi is not None: |
640 | | - # profile_step1 = np.nanmean(combined_normalized_data[:, roi.top:roi.top+roi.height, roi.left:roi.left+roi.width], axis=1) |
641 | | - # profile = np.nanmean(profile_step1, axis=1) |
642 | | - # else: |
643 | | - # profile_step1 = np.nanmean(combined_normalized_data, axis=1) |
644 | | - # profile = np.nanmean(profile_step1, axis=1) |
645 | | - |
646 | | - # axs3[1].plot(profile, 'o', markersize=MARKERSIZE, label=_label) |
647 | | - # axs3[1].set_xlabel("File image index") |
648 | | - # axs3[1].set_ylabel("Transmission (a.u.)") |
649 | | - # axs3[1].legend() |
650 | | - |
651 | | - # plt.tight_layout() |
652 | | - |
653 | | - # if lambda_array is not None: |
654 | | - |
655 | | - # fig, axs4 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) |
656 | | - # logging.info(f"{np.shape(profile) = }") |
657 | | - |
658 | | - # axs4[0].plot(lambda_array, profile, "*", markersize=MARKERSIZE, label=_label) |
659 | | - # #axs4[0].plot(lambda_array, combined_spectrum_normalized_data, label="spectrum normalization") |
660 | | - # axs4[0].set_xlabel("Lambda (A)") |
661 | | - # axs4[0].set_ylabel("mean of full image") |
662 | | - # axs4[0].legend() |
663 | | - |
664 | | - # axs4[1].plot(energy_array, profile, "*", markersize=MARKERSIZE, label=_label) |
665 | | - # #axs4[1].plot(energy_array, combined_spectrum_normalized_data, label="spectrum normalization") |
666 | | - # axs4[1].set_xlabel("Energy (eV)") |
667 | | - # axs4[1].set_ylabel("Transmission (a.u.)") |
668 | | - # axs4[1].set_xscale("log") |
669 | | - # axs4[1].legend() |
670 | | - |
671 | | - # plt.tight_layout() |
672 | | - |
673 | | - # if combined_spectrum_normalized_data is not None: |
674 | | - # fig, axs5 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) |
675 | | - # logging.info(f"{np.shape(profile) = }") |
676 | | - |
677 | | - # axs5[0].plot(lambda_array, combined_spectrum_normalized_data, "r*", |
678 | | - # markersize=MARKERSIZE, |
679 | | - # label="spectrum normalization of ROI") |
680 | | - # axs5[0].set_xlabel("Lambda (A)") |
681 | | - # axs5[0].set_ylabel("mean of full image") |
682 | | - # axs5[0].legend() |
683 | | - |
684 | | - # axs5[1].plot(energy_array, combined_spectrum_normalized_data, "r*", |
685 | | - # markersize=MARKERSIZE, |
686 | | - # label="spectrum normalization of ROI") |
687 | | - # axs5[1].set_xlabel("Energy (eV)") |
688 | | - # axs5[1].set_ylabel("Transmission (a.u.)") |
689 | | - # axs5[1].set_xscale("log") |
690 | | - # axs5[1].legend() |
691 | | - |
692 | | - # plt.tight_layout() |
693 | | - |
694 | | - # if export_corrected_integrated_combined_normalized_data or export_corrected_stack_of_combined_normalized_data: |
695 | | - |
696 | | - # export_corrected_normalized_data(sample_master_dict=sample_master_dict, |
697 | | - # ob_master_dict=ob_master_dict, |
698 | | - # dc_master_dict=dc_master_dict, |
699 | | - # combined_normalized_data=combined_normalized_data, |
700 | | - # export_corrected_integrated_combined_normalized_data=export_corrected_integrated_combined_normalized_data, |
701 | | - # export_corrected_stack_of_combined_normalized_data=export_corrected_stack_of_combined_normalized_data, |
702 | | - # lambda_array=lambda_array, |
703 | | - # energy_array=energy_array, |
704 | | - # output_folder=output_folder, |
705 | | - # spectra_array=spectra_array) |
706 | | - |
707 | | - # else: |
708 | | - # dict_to_return.data = normalized_data |
709 | | - |
710 | 624 | dict_to_return.data = normalized_data |
711 | 625 |
|
712 | 626 | logging.info("Normalization and export is done!") |
713 | 627 | if verbose: |
714 | 628 | display(HTML("Normalization and export is done!")) |
715 | 629 |
|
716 | 630 | return dict_to_return |
717 | | - |
0 commit comments