diff --git a/.gitignore b/.gitignore index 53cc96591..d949e42cd 100755 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,4 @@ dist/* .pixi/* !.pixi/config.toml -notebooks/pleiades_logs \ No newline at end of file +notebooks/pleiades_logs diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml old mode 100755 new mode 100644 index c46138760..c818a7bd6 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,7 @@ exclude: "^pixi.lock$" repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v5.0.0 + rev: v6.0.0 hooks: - id: check-added-large-files args: [--maxkb=8192] @@ -16,7 +16,7 @@ repos: - id: trailing-whitespace exclude: "notebooks/.*\\.ipynb$" - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.8.6 + rev: v0.14.10 hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] @@ -24,13 +24,13 @@ repos: - id: ruff-format exclude: "notebooks/.*\\.ipynb$" - repo: https://github.com/codespell-project/codespell - rev: v2.3.0 + rev: v2.4.1 hooks: - id: codespell exclude: "notebooks/.*\\.ipynb$" args: ["--skip=*.js,*.html,*.css"] - repo: https://github.com/adrienverge/yamllint - rev: v1.35.1 + rev: v1.37.1 hooks: - id: yamllint - repo: https://github.com/ComPWA/taplo-pre-commit diff --git a/notebooks/__code/_utilities/array.py b/notebooks/__code/_utilities/array.py index 265315fa6..8f834b568 100755 --- a/notebooks/__code/_utilities/array.py +++ b/notebooks/__code/_utilities/array.py @@ -33,7 +33,11 @@ def reject_outliers(array=None): if len(array) < 3: return array - new_array = [_value for _value in array if (not (_value == np.min(array))) and (not (_value == np.max(array)))] + new_array = [ + _value + for _value in array + if (not (_value == np.min(array))) and (not (_value == np.max(array))) + ] return new_array diff --git a/notebooks/__code/_utilities/file.py b/notebooks/__code/_utilities/file.py index 45c7d3124..bd3cfdbfe 100755 --- a/notebooks/__code/_utilities/file.py +++ b/notebooks/__code/_utilities/file.py @@ -192,7 +192,9 @@ def make_or_increment_folder_name(folder_name): if not os.path.exists(folder_name): os.makedirs(folder_name) else: - new_folder_name = "_".join([folder_name, get_current_time_in_special_file_name_format()]) + new_folder_name = "_".join( + [folder_name, get_current_time_in_special_file_name_format()] + ) if os.path.exists(new_folder_name): os.removedirs(new_folder_name) os.makedirs(new_folder_name) @@ -205,7 +207,9 @@ def copy_files_to_folder(list_files=[], output_folder=""): shutil.copy(_file, output_folder) -def copy_and_rename_files_to_folder(list_files=[], new_list_files_names=[], output_folder=""): +def copy_and_rename_files_to_folder( + list_files=[], new_list_files_names=[], output_folder="" +): for _index_file, _original_file in enumerate(list_files): _new_file = os.path.join(output_folder, new_list_files_names[_index_file]) shutil.copy(_original_file, _new_file) @@ -250,7 +254,9 @@ def append_to_file(data=[], output_file_name=""): f.write(_line + "\n") -def make_ascii_file_from_2dim_array(metadata=None, col1=None, col2=None, output_file_name=None, sep=", "): +def make_ascii_file_from_2dim_array( + metadata=None, col1=None, col2=None, output_file_name=None, sep=", " +): with open(output_file_name, "w") as f: for _meta in metadata: _line = _meta + "\n" @@ -315,7 +321,9 @@ def retrieve_metadata_from_dsc_list_files(list_files=[]): metadata = {} for _index, _file in enumerate(list_files): - metadata[os.path.basename(_file)] = retrieve_metadata_from_dsc_file(filename=_file) + metadata[os.path.basename(_file)] = retrieve_metadata_from_dsc_file( + filename=_file + ) w.value = _index + 1 return metadata @@ -426,7 +434,9 @@ def retrieve_time_stamp(list_images, label=""): box = widgets.HBox( [ widgets.Label(message, layout=widgets.Layout(width="20%")), - widgets.IntProgress(min=0, max=len(list_images), value=0, layout=widgets.Layout(width="50%")), + widgets.IntProgress( + min=0, max=len(list_images), value=0, layout=widgets.Layout(width="50%") + ), ] ) progress_bar = box.children[1] @@ -494,21 +504,33 @@ def read_bragg_edge_fitting_ascii_format(full_file_name): regular = r"^#fitting peak range in file index: \[(?P\d+), (?P\d+)\]$" m = re.search(regular, line.strip()) if m: - metadata["bragg_edge_range"] = [int(m.group("left_index")), int(m.group("right_index"))] + metadata["bragg_edge_range"] = [ + int(m.group("left_index")), + int(m.group("right_index")), + ] else: metadata["bragg_edge_range"] = [None, None] line_number += 1 continue if "#distance detector-sample: " in line: - metadata["distance_detector_sample"] = line.split("#distance detector-sample: ")[1].strip() + metadata["distance_detector_sample"] = line.split( + "#distance detector-sample: " + )[1].strip() line_number += 1 continue if "#detector offset: " in line: - metadata["detector_offset"] = line.split("#detector offset: ")[1].strip() + metadata["detector_offset"] = line.split("#detector offset: ")[ + 1 + ].strip() line_number += 1 continue if "#kropff fitting procedure started: " in line: - result = True if line.split("#kropff fitting procedure started: ")[1].strip() == "True" else False + result = ( + True + if line.split("#kropff fitting procedure started: ")[1].strip() + == "True" + else False + ) metadata["kropff fitting procedure started"] = result line_number += 1 continue @@ -524,7 +546,12 @@ def read_bragg_edge_fitting_ascii_format(full_file_name): continue if "#march-dollase fitting procedure started: " in line: result = ( - True if line.split("#march-dollase fitting procedure started: ")[1].strip() == "True" else False + True + if line.split("#march-dollase fitting procedure started: ")[ + 1 + ].strip() + == "True" + else False ) metadata["march-dollase fitting procedure started"] = result line_number += 1 @@ -533,11 +560,17 @@ def read_bragg_edge_fitting_ascii_format(full_file_name): regular = r"^#Bragg peak selection range: \[(?P\d+), (?P\d+)\]$" m = re.search(regular, line.strip()) if m: - metadata["bragg_peak_selection_range"] = [int(m.group("left_index")), int(m.group("right_index"))] + metadata["bragg_peak_selection_range"] = [ + int(m.group("left_index")), + int(m.group("right_index")), + ] line_number += 1 continue if "#kropff " in line: - regular = r"^#kropff (?P\w+) selection range: \[(?P\d+), " r"(?P\d+)\]$" + regular = ( + r"^#kropff (?P\w+) selection range: \[(?P\d+), " + r"(?P\d+)\]$" + ) m = re.search(regular, line.strip()) if m: metadata["kropff_{}".format(m.group("type"))] = [ @@ -547,17 +580,23 @@ def read_bragg_edge_fitting_ascii_format(full_file_name): line_number += 1 continue if "#fitting algorithm selected: " in line: - metadata["fitting_algorithm_selected"] = line.split("#fitting algorithm selected: ")[1].strip() + metadata["fitting_algorithm_selected"] = line.split( + "#fitting algorithm selected: " + )[1].strip() line_number += 1 continue if "#march-dollase history table row " in line: - _row_and_list_flag = line.split("#march-dollase history table row ")[1].strip() + _row_and_list_flag = line.split("#march-dollase history table row ")[ + 1 + ].strip() [_row, list_flag] = _row_and_list_flag.split(":") march_history_table[_row] = list_flag line_number += 1 continue if "#march-dollase history init " in line: - _parameter_and_value = line.split("#march-dollase history init ")[1].strip() + _parameter_and_value = line.split("#march-dollase history init ")[ + 1 + ].strip() [_parameter, _value] = _parameter_and_value.split(":") march_history_init[_parameter] = _value line_number += 1 @@ -648,7 +687,9 @@ def read_bragg_edge_fitting_ascii_format(full_file_name): metadata["march-dollase history table"] = march_history_table metadata["march-dollase history init"] = march_history_init - pd_data = pd.read_csv(full_file_name, skiprows=line_number, header=0, names=col_label) + pd_data = pd.read_csv( + full_file_name, skiprows=line_number, header=0, names=col_label + ) return {"data": pd_data, "metadata": metadata} @@ -661,7 +702,9 @@ def __init__(self, working_dir=""): def get_list_of_files(self): list_of_input_files = glob.glob(os.path.join(self.working_dir, "*")) list_of_input_files.sort() - self.list_of_base_name = [os.path.basename(_file) for _file in list_of_input_files] + self.list_of_base_name = [ + os.path.basename(_file) for _file in list_of_input_files + ] def get_counter_of_extension(self): counter_extension = Counter() @@ -686,7 +729,11 @@ def get_dominant_extension(self): def check_uniqueness_of_dominant_extension(self): # check if there are several ext with the same max number - indices = [i for i, x in enumerate(self.list_of_number_of_ext) if x == self.dominant_number] + indices = [ + i + for i, x in enumerate(self.list_of_number_of_ext) + if x == self.dominant_number + ] if len(indices) > 1: # found several majority ext self.uniqueness = False else: @@ -701,21 +748,34 @@ def calculate(self): def retrieve_parameters(self): if self.uniqueness: - list_of_input_files = glob.glob(os.path.join(self.working_dir, "*" + self.dominant_extension)) + list_of_input_files = glob.glob( + os.path.join(self.working_dir, "*" + self.dominant_extension) + ) list_of_input_files.sort() - self.result = self.Result(list_files=list_of_input_files, ext=self.dominant_extension, uniqueness=True) + self.result = self.Result( + list_files=list_of_input_files, + ext=self.dominant_extension, + uniqueness=True, + ) else: list_of_maj_ext = [ - _ext for _ext in self.counter_extension.keys() if self.counter_extension[_ext] == self.dominant_number + _ext + for _ext in self.counter_extension.keys() + if self.counter_extension[_ext] == self.dominant_number ] box = widgets.HBox( [ - widgets.Label("Select Extension to work with", layout=widgets.Layout(width="20%")), + widgets.Label( + "Select Extension to work with", + layout=widgets.Layout(width="20%"), + ), widgets.Dropdown( - options=list_of_maj_ext, layout=widgets.Layout(width="20%"), value=list_of_maj_ext[0] + options=list_of_maj_ext, + layout=widgets.Layout(width="20%"), + value=list_of_maj_ext[0], ), ] ) @@ -728,7 +788,13 @@ def get_files_of_selected_ext(self): else: _ext_selected = self.dropdown_ui.value - list_of_input_files = glob.glob(os.path.join(self.working_dir, "*" + _ext_selected)) + list_of_input_files = glob.glob( + os.path.join(self.working_dir, "*" + _ext_selected) + ) list_of_input_files.sort() - return self.Result(list_files=list_of_input_files, ext=self.dominant_extension, uniqueness=True) + return self.Result( + list_files=list_of_input_files, + ext=self.dominant_extension, + uniqueness=True, + ) diff --git a/notebooks/__code/_utilities/folder.py b/notebooks/__code/_utilities/folder.py index de938377f..0d9de07a4 100755 --- a/notebooks/__code/_utilities/folder.py +++ b/notebooks/__code/_utilities/folder.py @@ -5,7 +5,9 @@ import numpy as np -def get_list_of_folders_with_specified_file_type(list_of_folders_to_check=None, file_extension=["tiff", "tif"]): +def get_list_of_folders_with_specified_file_type( + list_of_folders_to_check=None, file_extension=["tiff", "tif"] +): """ check in the list of folder given (list_of_folders_to_check) if files of the type specified are there. If no file can be found in that folder with that type, the folder name is removed from the list diff --git a/notebooks/__code/_utilities/images.py b/notebooks/__code/_utilities/images.py index 5e9483a95..f83d850f6 100755 --- a/notebooks/__code/_utilities/images.py +++ b/notebooks/__code/_utilities/images.py @@ -16,7 +16,9 @@ def _init_arr_from_stack(list_files, ext=".tiff", slc=None): _arr = dxchange.read_tiff(first_file) f_type = "tif" else: - raise ValueError(f"'{first_file}', only '.tif/.tiff' and '.fits' are supported.") + raise ValueError( + f"'{first_file}', only '.tif/.tiff' and '.fits' are supported." + ) size = (number_of_files, _arr.shape[0], _arr.shape[1]) return np.empty(size, dtype=_arr.dtype), f_type diff --git a/notebooks/__code/_utilities/legend.py b/notebooks/__code/_utilities/legend.py index a9a62bb6b..c09400c20 100644 --- a/notebooks/__code/_utilities/legend.py +++ b/notebooks/__code/_utilities/legend.py @@ -5,9 +5,13 @@ def legend() -> None: display(HTML("
")) display(HTML("

Legend

")) - display(HTML("
    " - "
  • Mandatory steps must be performed to ensure proper data processing.
  • " - "
  • Optional but recommended steps are not mandatory but should be performed to ensure proper data processing.
  • " - "
  • Optional steps are not mandatory but highly recommended to improve the quality of your data processing.
  • " - "
")) - display(HTML("
")) \ No newline at end of file + display( + HTML( + "
    " + "
  • Mandatory steps must be performed to ensure proper data processing.
  • " + "
  • Optional but recommended steps are not mandatory but should be performed to ensure proper data processing.
  • " + "
  • Optional steps are not mandatory but highly recommended to improve the quality of your data processing.
  • " + "
" + ) + ) + display(HTML("
")) diff --git a/notebooks/__code/_utilities/list.py b/notebooks/__code/_utilities/list.py index 4c2dd97f9..3216038fb 100755 --- a/notebooks/__code/_utilities/list.py +++ b/notebooks/__code/_utilities/list.py @@ -25,7 +25,9 @@ def are_those_two_lists_identical_within_tolerance(list1, list2, tolerance=0.01) return True -def are_those_two_lists_of_lists_identical_within_tolerance(list1, list2, tolerance=0.01): +def are_those_two_lists_of_lists_identical_within_tolerance( + list1, list2, tolerance=0.01 +): """ check that 2 lists composed of lists are identical @@ -44,7 +46,9 @@ def are_those_two_lists_of_lists_identical_within_tolerance(list1, list2, tolera return True for list_item1, list_item2 in zip(list1, list2, strict=False): - if not are_those_two_lists_identical_within_tolerance(list_item1, list_item2, tolerance=tolerance): + if not are_those_two_lists_identical_within_tolerance( + list_item1, list_item2, tolerance=tolerance + ): return False return True @@ -60,10 +64,14 @@ def is_this_list_already_in_those_lists_within_tolerance(list1, list2, tolerance :return: """ if (not (type(list1) == list)) or (not (type(list2) == list)): - raise TypeError("First argument should be a list of floats, second argument a list of lists") + raise TypeError( + "First argument should be a list of floats, second argument a list of lists" + ) for target_list in list2: - if are_those_two_lists_identical_within_tolerance(list1, target_list, tolerance=tolerance): + if are_those_two_lists_identical_within_tolerance( + list1, target_list, tolerance=tolerance + ): return True return False diff --git a/notebooks/__code/_utilities/logger.py b/notebooks/__code/_utilities/logger.py index 609c47cf5..c1d581bb7 100644 --- a/notebooks/__code/_utilities/logger.py +++ b/notebooks/__code/_utilities/logger.py @@ -2,7 +2,6 @@ def display_dictionary_in_logging(dictionary): - for key, value in dictionary.items(): logging.info(f"\t {key}: {value}") logging.info("") # Add an empty line for better readability diff --git a/notebooks/__code/_utilities/metadata_handler.py b/notebooks/__code/_utilities/metadata_handler.py index 6be29cc5b..ff64c377e 100755 --- a/notebooks/__code/_utilities/metadata_handler.py +++ b/notebooks/__code/_utilities/metadata_handler.py @@ -34,7 +34,11 @@ def get_time_stamp(file_name="", ext="tif"): except: time_stamp = o_dict[65000] - time_stamp = MetadataHandler._convert_epics_timestamp_to_rfc3339_timestamp(time_stamp) + time_stamp = ( + MetadataHandler._convert_epics_timestamp_to_rfc3339_timestamp( + time_stamp + ) + ) except: time_stamp = os.path.getctime(file_name) @@ -113,7 +117,9 @@ def retrieve_metadata(list_files=[], list_metadata=[], using_enum_object=False): _dict = OrderedDict() for _file in list_files: _meta = MetadataHandler.get_metadata( - filename=_file, list_metadata=list_metadata, using_enum_object=using_enum_object + filename=_file, + list_metadata=list_metadata, + using_enum_object=using_enum_object, ) _dict[_file] = _meta @@ -145,7 +151,9 @@ def retrieve_value_of_metadata_key(list_files=[], list_key=[]): _dict = OrderedDict() for _file in list_files: - _meta = MetadataHandler.get_value_of_metadata_key(filename=_file, list_key=list_key) + _meta = MetadataHandler.get_value_of_metadata_key( + filename=_file, list_key=list_key + ) _dict[_file] = _meta return _dict diff --git a/notebooks/__code/_utilities/nexus.py b/notebooks/__code/_utilities/nexus.py index 5749f7ecf..74acd5bf6 100644 --- a/notebooks/__code/_utilities/nexus.py +++ b/notebooks/__code/_utilities/nexus.py @@ -13,7 +13,9 @@ def extract_file_path_from_nexus(nexus_file_path): """ logging.info(f"Extracting file path from NeXus file: {nexus_file_path}") with h5py.File(str(nexus_file_path), "r") as hdf5_data: - list_file_path = hdf5_data["entry"]["DASlogs"]["BL10:Exp:IM:ImageFilePath"]["value"][:] + list_file_path = hdf5_data["entry"]["DASlogs"]["BL10:Exp:IM:ImageFilePath"][ + "value" + ][:] logging.info(f"\t{list_file_path} = ") file_path = list_file_path[-1][0].decode("utf-8") file_path = file_path.strip() diff --git a/notebooks/__code/_utilities/status_message.py b/notebooks/__code/_utilities/status_message.py index 50759a850..000ea7672 100755 --- a/notebooks/__code/_utilities/status_message.py +++ b/notebooks/__code/_utilities/status_message.py @@ -9,7 +9,9 @@ class StatusMessageStatus: warning = "QStatusBar{padding-left:8px;background:rgba(236,236,236,75);color:red;font-weight:normal;}" -def show_status_message(parent=None, message="", status=StatusMessageStatus.ready, duration_s=None): +def show_status_message( + parent=None, message="", status=StatusMessageStatus.ready, duration_s=None +): parent.ui.statusbar.setStyleSheet(status) if duration_s: parent.ui.statusbar.showMessage(message, duration_s * 1000) diff --git a/notebooks/__code/_utilities/string.py b/notebooks/__code/_utilities/string.py index 3eece471a..044ad4b60 100755 --- a/notebooks/__code/_utilities/string.py +++ b/notebooks/__code/_utilities/string.py @@ -2,7 +2,9 @@ from IPython.display import HTML -def get_beginning_common_part_of_string_from_list(list_of_text=None, filename_spacer="_"): +def get_beginning_common_part_of_string_from_list( + list_of_text=None, filename_spacer="_" +): """This method returns the continuous part of a string, from the beginning, that can be found in all string provided. The match will stop before the last filename_spacer diff --git a/notebooks/__code/_utilities/table_handler.py b/notebooks/__code/_utilities/table_handler.py index 95c4dbb0e..063e4ce31 100755 --- a/notebooks/__code/_utilities/table_handler.py +++ b/notebooks/__code/_utilities/table_handler.py @@ -202,7 +202,9 @@ def set_background_color(self, row=0, column=0, qcolor=QtGui.QColor(0, 255, 255) _item = self.table_ui.item(row, column) _item.setBackground(qcolor) - def fill_table_with(self, list_items=None, editable_columns_boolean=None, block_signal=False): + def fill_table_with( + self, list_items=None, editable_columns_boolean=None, block_signal=False + ): """ :param: list_items: 2D array of text to put in the table @@ -223,7 +225,12 @@ def fill_table_with(self, list_items=None, editable_columns_boolean=None, block_ editable_flag = False else: editable_flag = editable_columns_boolean[_column_index] - self.insert_item(row=_row_index, column=_column_index, value=_text, editable=editable_flag) + self.insert_item( + row=_row_index, + column=_column_index, + value=_text, + editable=editable_flag, + ) if block_signal: WidgetsHandler.block_signals(ui=self.table_ui, status=False) diff --git a/notebooks/__code/_utilities/time.py b/notebooks/__code/_utilities/time.py index f00bb69dc..b446e155b 100755 --- a/notebooks/__code/_utilities/time.py +++ b/notebooks/__code/_utilities/time.py @@ -38,7 +38,13 @@ def format_time_stamp(file_name=None, time_stamp=None): [hours, minutes, seconds] = hours.split(":") _dict_time = {"hours": hours, "minutes": minutes, "seconds": seconds} - _dict_time_stamp = {"week_day": week_day, "month": month, "day": day, "hours": _dict_time, "year": year} + _dict_time_stamp = { + "week_day": week_day, + "month": month, + "day": day, + "hours": _dict_time, + "year": year, + } return [_short_file_name, _dict_time_stamp] @@ -118,8 +124,10 @@ def __init__(self, folder="", files=[], is_notebook=False): self.__is_notebook = is_notebook def _run(self): - [list_files, ext] = file_handler.retrieve_list_of_most_dominant_extension_from_folder( - folder=self.folder, files=self.input_list_files + [list_files, ext] = ( + file_handler.retrieve_list_of_most_dominant_extension_from_folder( + folder=self.folder, files=self.input_list_files + ) ) self.output_list_files = list_files @@ -133,8 +141,15 @@ def _run(self): if self.__is_notebook: box = widgets.HBox( [ - widgets.Label("Retrieving Time Stamp", layout=widgets.Layout(width="20%")), - widgets.IntProgress(min=0, max=len(list_files), value=0, layout=widgets.Layout(width="50%")), + widgets.Label( + "Retrieving Time Stamp", layout=widgets.Layout(width="20%") + ), + widgets.IntProgress( + min=0, + max=len(list_files), + value=0, + layout=widgets.Layout(width="50%"), + ), ] ) progress_bar = box.children[1] @@ -171,7 +186,12 @@ class TimestampFormatter: "%Y-%m-%dT%I:%M:%S-", ] - def __init__(self, timestamp="", input_timestamp_format=None, output_timestamp_format=TIMESTAMP_FORMAT): + def __init__( + self, + timestamp="", + input_timestamp_format=None, + output_timestamp_format=TIMESTAMP_FORMAT, + ): self.timestamp = timestamp if input_timestamp_format is None: self.input_timestamp_format = self.list_input_timestamp @@ -208,7 +228,9 @@ def convert_timestamp(self, timestamp): o_time = None for _input_timestamp_format in input_timestamp_format: # print("trying this format {} with this {}".format(_input_timestamp_format, timestamp)) - o_time = TimestampFormatter.get_time_dict(timestamp=timestamp, input_time_format=_input_timestamp_format) + o_time = TimestampFormatter.get_time_dict( + timestamp=timestamp, input_time_format=_input_timestamp_format + ) if o_time: break @@ -247,11 +269,15 @@ def convert_to_second(timestamp_value, timestamp_format=TIMESTAMP_FORMAT): class AbsoluteTimeHandler: def __init__(self, initial_absolute_time=None): if initial_absolute_time is None: - raise ValueError("Please provide an initial absolute time format as 'YYYY-MM-DDTHH:MM:SS.SSSSSS-05:00") + raise ValueError( + "Please provide an initial absolute time format as 'YYYY-MM-DDTHH:MM:SS.SSSSSS-05:00" + ) self.formatted_initial_absolute_time = parse(initial_absolute_time) - def get_absolute_time_for_this_delta_time_array(self, delta_time_array=None, units="seconds"): + def get_absolute_time_for_this_delta_time_array( + self, delta_time_array=None, units="seconds" + ): """ :param delta_time_array: list of time offset @@ -273,7 +299,10 @@ def get_absolute_time_for_this_delta_time_array(self, delta_time_array=None, uni self.delta_time_formated = delta_time_formated - absolute_time = [delta_time + self.formatted_initial_absolute_time for delta_time in delta_time_formated] + absolute_time = [ + delta_time + self.formatted_initial_absolute_time + for delta_time in delta_time_formated + ] return absolute_time @@ -287,7 +316,9 @@ class RelativeTimeHandler: def __init__(self, master_initial_time=None, local_initial_time=None): if (master_initial_time is None) or (local_initial_time is None): - raise ValueError("Please provide an initial absolute time format as 'YYYY-MM-DDTHH:MM:SS.SSSSSS-05:00") + raise ValueError( + "Please provide an initial absolute time format as 'YYYY-MM-DDTHH:MM:SS.SSSSSS-05:00" + ) formatted_master_initial_time = parse(master_initial_time) formatted_local_initial_time = parse(local_initial_time) @@ -295,7 +326,9 @@ def __init__(self, master_initial_time=None, local_initial_time=None): if formatted_local_initial_time < formatted_master_initial_time: raise ValueError("Master time should be before local time!") - time_offset_calculated = formatted_local_initial_time - formatted_master_initial_time + time_offset_calculated = ( + formatted_local_initial_time - formatted_master_initial_time + ) self.time_offset_calculated_s = time_offset_calculated.seconds def get_relative_time_for_this_time_array(self, time_array=None): diff --git a/notebooks/__code/addie.py b/notebooks/__code/addie.py index e850a5c72..147c50337 100755 --- a/notebooks/__code/addie.py +++ b/notebooks/__code/addie.py @@ -111,7 +111,9 @@ class Interface(QMainWindow): sample_children_1["sample_absolute_correction"]["name"] = "Abs. Correction" sample_children_1["sample_multi_scattering_correction"] = copy.deepcopy(base_dict) - sample_children_1["sample_multi_scattering_correction"]["name"] = "Multi Scattering Correction" + sample_children_1["sample_multi_scattering_correction"]["name"] = ( + "Multi Scattering Correction" + ) sample_children_1["sample_inelastic_correction"] = copy.deepcopy(base_dict) sample_children_1["sample_inelastic_correction"]["name"] = "Inelastic Correction" @@ -158,11 +160,17 @@ class Interface(QMainWindow): vanadium_children_1["vanadium_absolute_correction"] = copy.deepcopy(base_dict) vanadium_children_1["vanadium_absolute_correction"]["name"] = "Abs. Correction" - vanadium_children_1["vanadium_multi_scattering_correction"] = copy.deepcopy(base_dict) - vanadium_children_1["vanadium_multi_scattering_correction"]["name"] = "Multi Scattering Correction" + vanadium_children_1["vanadium_multi_scattering_correction"] = copy.deepcopy( + base_dict + ) + vanadium_children_1["vanadium_multi_scattering_correction"]["name"] = ( + "Multi Scattering Correction" + ) vanadium_children_1["vanadium_inelastic_correction"] = copy.deepcopy(base_dict) - vanadium_children_1["vanadium_inelastic_correction"]["name"] = "Inelastic Correction" + vanadium_children_1["vanadium_inelastic_correction"]["name"] = ( + "Inelastic Correction" + ) tree_dict["vanadium"] = copy.deepcopy(base_dict) tree_dict["vanadium"]["name"] = "Vanadium" @@ -221,9 +229,15 @@ def init_signals(self): self.h2_header_table.sectionResized.connect(self.resizing_h2) self.h3_header_table.sectionResized.connect(self.resizing_h3) - self.ui.h1_table.horizontalScrollBar().valueChanged.connect(self.scroll_h1_table) - self.ui.h2_table.horizontalScrollBar().valueChanged.connect(self.scroll_h2_table) - self.ui.h3_table.horizontalScrollBar().valueChanged.connect(self.scroll_h3_table) + self.ui.h1_table.horizontalScrollBar().valueChanged.connect( + self.scroll_h1_table + ) + self.ui.h2_table.horizontalScrollBar().valueChanged.connect( + self.scroll_h2_table + ) + self.ui.h3_table.horizontalScrollBar().valueChanged.connect( + self.scroll_h3_table + ) def load_this_config(self, key="", resize=False): if key == "": @@ -247,17 +261,23 @@ def load_this_config(self, key="", resize=False): for _col in h1_dict: _visible = h1_dict[_col]["visible"] _width = h1_dict[_col]["width"] - self.set_size_and_visibility_column(h1=_col, width=_width, visibility=_visible, resize=resize) + self.set_size_and_visibility_column( + h1=_col, width=_width, visibility=_visible, resize=resize + ) for _col in h2_dict: _visible = h2_dict[_col]["visible"] _width = h2_dict[_col]["width"] - self.set_size_and_visibility_column(h2=_col, width=_width, visibility=_visible, resize=resize) + self.set_size_and_visibility_column( + h2=_col, width=_width, visibility=_visible, resize=resize + ) for _col in h3_dict: _visible = h3_dict[_col]["visible"] _width = h3_dict[_col]["width"] - self.set_size_and_visibility_column(h3=_col, width=_width, visibility=_visible, resize=resize) + self.set_size_and_visibility_column( + h3=_col, width=_width, visibility=_visible, resize=resize + ) self.update_tree_dict_and_tree(config_to_load) # self.update_full_tree_status() @@ -323,7 +343,14 @@ def scroll_h3_table(self, value): self.ui.h1_table.horizontalScrollBar().setValue(value) self.ui.h2_table.horizontalScrollBar().setValue(value) - def block_table_ui(self, block_all=True, unblock_all=False, block_h1=False, block_h2=False, block_h3=False): + def block_table_ui( + self, + block_all=True, + unblock_all=False, + block_h1=False, + block_h2=False, + block_h3=False, + ): if block_all: block_h1 = True block_h2 = True @@ -374,8 +401,12 @@ def resizing_h1(self, index_column, old_size, new_size): self.set_size_column(h1=index_column, width=old_size) else: last_h2_visible_size = self.get_size_column(h2=last_h2_visible) - self.set_size_column(h2=last_h2_visible, width=last_h2_visible_size + size_diff) - self.set_size_column(h3=last_h3_visible, width=last_h3_visible_size + size_diff) + self.set_size_column( + h2=last_h2_visible, width=last_h2_visible_size + size_diff + ) + self.set_size_column( + h3=last_h3_visible, width=last_h3_visible_size + size_diff + ) self.block_table_ui(unblock_all=True) # print("") @@ -403,7 +434,9 @@ def resizing_h2(self, index_column, old_size, new_size): # add this size_diff to parent and last h3 parent_size = self.get_size_column(h1=h1_parent) self.set_size_column(h1=h1_parent, width=parent_size + size_diff) - self.set_size_column(h3=last_h3_visible, width=last_h3_visible_size + size_diff) + self.set_size_column( + h3=last_h3_visible, width=last_h3_visible_size + size_diff + ) self.block_table_ui(unblock_all=True) @@ -501,7 +534,9 @@ def set_visibility_column(self, h1=None, h2=None, h3=None, visibility=True): h = self.get_master_h(h1=h1, h2=h2, h3=h3) table_ui.setColumnHidden(h, not visibility) - def set_size_and_visibility_column(self, h1=None, h2=None, h3=None, width=None, visibility=True, resize=False): + def set_size_and_visibility_column( + self, h1=None, h2=None, h3=None, width=None, visibility=True, resize=False + ): if resize: self.set_size_column(h1=h1, h2=h2, h3=h3, width=width) self.set_visibility_column(h1=h1, h2=h2, h3=h3, visibility=visibility) @@ -520,7 +555,9 @@ def get_all_h2_visible(self, list_h2=[]): if list_h2 == []: return None - list_h2_visible = [_h2 for _h2 in list_h2 if not self.ui.h2_table.isColumnHidden(_h2)] + list_h2_visible = [ + _h2 for _h2 in list_h2 if not self.ui.h2_table.isColumnHidden(_h2) + ] return list_h2_visible def get_last_h2_visible(self, list_h2=[]): @@ -540,7 +577,9 @@ def get_all_h3_visible(self, list_h3=[]): if list_h3 == []: return None - list_h3_visible = [_h3 for _h3 in list_h3 if not self.ui.h3_table.isColumnHidden(_h3)] + list_h3_visible = [ + _h3 for _h3 in list_h3 if not self.ui.h3_table.isColumnHidden(_h3) + ] return list_h3_visible def get_last_h3_visible(self, list_h3=[]): @@ -686,8 +725,14 @@ def init_headers(self): for _key_h2 in td[_key_h1]["children"].keys(): table_headers["h2"].append(td[_key_h1]["children"][_key_h2]["name"]) if td[_key_h1]["children"][_key_h2]["children"]: - for _key_h3 in td[_key_h1]["children"][_key_h2]["children"].keys(): - table_headers["h3"].append(td[_key_h1]["children"][_key_h2]["children"][_key_h3]["name"]) + for _key_h3 in td[_key_h1]["children"][_key_h2][ + "children" + ].keys(): + table_headers["h3"].append( + td[_key_h1]["children"][_key_h2]["children"][_key_h3][ + "name" + ] + ) else: table_headers["h3"].append("") else: @@ -741,7 +786,9 @@ def init_table_dimensions(self): ## h2 header will be += 1 absolute_nbr_h3_for_this_h1 += 1 - table_width["h1"].append(absolute_nbr_h3_for_this_h1 * self.default_width) + table_width["h1"].append( + absolute_nbr_h3_for_this_h1 * self.default_width + ) # if h1 has no children else: @@ -755,15 +802,27 @@ def init_table_dimensions(self): def init_tables(self): # set h1, h2 and h3 headers self.init_headers() - self.init_table_header(table_ui=self.ui.h1_table, list_items=self.table_headers["h1"]) - self.init_table_header(table_ui=self.ui.h2_table, list_items=self.table_headers["h2"]) - self.init_table_header(table_ui=self.ui.h3_table, list_items=self.table_headers["h3"]) + self.init_table_header( + table_ui=self.ui.h1_table, list_items=self.table_headers["h1"] + ) + self.init_table_header( + table_ui=self.ui.h2_table, list_items=self.table_headers["h2"] + ) + self.init_table_header( + table_ui=self.ui.h3_table, list_items=self.table_headers["h3"] + ) # set h1, h2 and h3 width self.init_table_dimensions() - self.init_table_col_width(table_width=self.table_width["h1"], table_ui=self.ui.h1_table) - self.init_table_col_width(table_width=self.table_width["h2"], table_ui=self.ui.h2_table) - self.init_table_col_width(table_width=self.table_width["h3"], table_ui=self.ui.h3_table) + self.init_table_col_width( + table_width=self.table_width["h1"], table_ui=self.ui.h1_table + ) + self.init_table_col_width( + table_width=self.table_width["h2"], table_ui=self.ui.h2_table + ) + self.init_table_col_width( + table_width=self.table_width["h3"], table_ui=self.ui.h3_table + ) self.h1_header_table = self.ui.h1_table.horizontalHeader() self.h2_header_table = self.ui.h2_table.horizontalHeader() @@ -793,8 +852,15 @@ def get_item_name(self, item): return _key_h2 if td[_key_h1]["children"][_key_h2]["children"]: - for _key_h3 in td[_key_h1]["children"][_key_h2]["children"].keys(): - if item == td[_key_h1]["children"][_key_h2]["children"][_key_h3]["ui"]: + for _key_h3 in td[_key_h1]["children"][_key_h2][ + "children" + ].keys(): + if ( + item + == td[_key_h1]["children"][_key_h2]["children"][ + _key_h3 + ]["ui"] + ): return _key_h3 return None @@ -807,7 +873,9 @@ def tree_item_changed(self, item, _): self.h2_header_table.blockSignals(True) # self.h3_header_table.blockSignals(True) - h_columns_affected = self.get_h_columns_from_item_name(item_name=self.get_item_name(item)) + h_columns_affected = self.get_h_columns_from_item_name( + item_name=self.get_item_name(item) + ) # import pprint # pprint.pprint(h_columns_affected) @@ -870,33 +938,57 @@ def get_boolean_state(key=None): for h1_counter, _key_h1 in enumerate(td.keys()): _h1_boolean_status = get_boolean_state(td[_key_h1]) - set_column_visibility(column=h1_counter, table_ui=self.ui.h1_table, visible=_h1_boolean_status) + set_column_visibility( + column=h1_counter, table_ui=self.ui.h1_table, visible=_h1_boolean_status + ) if td[_key_h1]["children"]: for _key_h2 in td[_key_h1]["children"].keys(): - _h2_boolean_status = get_boolean_state(td[_key_h1]["children"][_key_h2]) - set_column_visibility(column=h2_counter, table_ui=self.ui.h2_table, visible=_h2_boolean_status) + _h2_boolean_status = get_boolean_state( + td[_key_h1]["children"][_key_h2] + ) + set_column_visibility( + column=h2_counter, + table_ui=self.ui.h2_table, + visible=_h2_boolean_status, + ) if td[_key_h1]["children"][_key_h2]["children"]: - for _key_h3 in td[_key_h1]["children"][_key_h2]["children"].keys(): + for _key_h3 in td[_key_h1]["children"][_key_h2][ + "children" + ].keys(): _h3_boolean_status = get_boolean_state( td[_key_h1]["children"][_key_h2]["children"][_key_h3] ) set_column_visibility( - column=h3_counter, table_ui=self.ui.h3_table, visible=_h3_boolean_status + column=h3_counter, + table_ui=self.ui.h3_table, + visible=_h3_boolean_status, ) h3_counter += 1 else: - set_column_visibility(column=h3_counter, table_ui=self.ui.h3_table, visible=_h2_boolean_status) + set_column_visibility( + column=h3_counter, + table_ui=self.ui.h3_table, + visible=_h2_boolean_status, + ) h3_counter += 1 h2_counter += 1 else: # h2 and h3 should have the same status as h1 - set_column_visibility(column=h2_counter, table_ui=self.ui.h2_table, visible=_h1_boolean_status) - set_column_visibility(column=h3_counter, table_ui=self.ui.h3_table, visible=_h1_boolean_status) + set_column_visibility( + column=h2_counter, + table_ui=self.ui.h2_table, + visible=_h1_boolean_status, + ) + set_column_visibility( + column=h3_counter, + table_ui=self.ui.h3_table, + visible=_h1_boolean_status, + ) h2_counter += 1 h3_counter += 1 @@ -939,15 +1031,21 @@ def update_full_tree_status(self): for _key_h2 in td[_key_h1]["children"].keys(): if td[_key_h1]["children"][_key_h2]["children"]: all_h3_disabled = True - for _key_h3 in td[_key_h1]["children"][_key_h2]["children"].keys(): - if td[_key_h1]["children"][_key_h2]["children"][_key_h3]["ui"].checkState(0): + for _key_h3 in td[_key_h1]["children"][_key_h2][ + "children" + ].keys(): + if td[_key_h1]["children"][_key_h2]["children"][_key_h3][ + "ui" + ].checkState(0): all_h3_disabled = False all_h2_disabled = False break if all_h3_disabled: # we need to make sure the h2 is disabled as well - td[_key_h1]["children"][_key_h2]["ui"].setCheckState(0, QtCore.Qt.Unchecked) + td[_key_h1]["children"][_key_h2]["ui"].setCheckState( + 0, QtCore.Qt.Unchecked + ) else: if td[_key_h1]["children"][_key_h2]["ui"].checkState(0): @@ -963,13 +1061,19 @@ def update_full_tree_status(self): if td[_key_h1]["children"]: for _key_h2 in td[_key_h1]["children"].keys(): - td[_key_h1]["children"][_key_h2]["state"] = td[_key_h1]["children"][_key_h2]["ui"].checkState(0) + td[_key_h1]["children"][_key_h2]["state"] = td[_key_h1]["children"][ + _key_h2 + ]["ui"].checkState(0) if td[_key_h1]["children"][_key_h2]["children"]: - for _key_h3 in td[_key_h1]["children"][_key_h2]["children"].keys(): - td[_key_h1]["children"][_key_h2]["children"][_key_h3]["state"] = td[_key_h1]["children"][ - _key_h2 - ]["children"][_key_h3]["ui"].checkState(0) + for _key_h3 in td[_key_h1]["children"][_key_h2][ + "children" + ].keys(): + td[_key_h1]["children"][_key_h2]["children"][_key_h3][ + "state" + ] = td[_key_h1]["children"][_key_h2]["children"][_key_h3][ + "ui" + ].checkState(0) self.tree_dict = td @@ -1062,9 +1166,15 @@ def get_h_columns_from_item_name(self, item_name=None): for _key_h2 in td[_key_h1]["children"]: if td[_key_h1]["children"][_key_h2]["children"]: list_tree_ui.append(td[_key_h1]["children"][_key_h2]["ui"]) - for _key_h3 in td[_key_h1]["children"][_key_h2]["children"].keys(): + for _key_h3 in td[_key_h1]["children"][_key_h2][ + "children" + ].keys(): h3_columns.append(h3_global_counter) - list_tree_ui.append(td[_key_h1]["children"][_key_h2]["children"][_key_h3]["ui"]) + list_tree_ui.append( + td[_key_h1]["children"][_key_h2]["children"][ + _key_h3 + ]["ui"] + ) h3_global_counter += 1 else: @@ -1105,9 +1215,15 @@ def get_h_columns_from_item_name(self, item_name=None): # if key_h2 has children # list all h3 leaves for this h2 - for _key_h3 in td[_key_h1]["children"][_key_h2]["children"].keys(): + for _key_h3 in td[_key_h1]["children"][_key_h2][ + "children" + ].keys(): h3_columns.append(h3_global_counter) - list_tree_ui.append(td[_key_h1]["children"][_key_h2]["children"][_key_h3]["ui"]) + list_tree_ui.append( + td[_key_h1]["children"][_key_h2]["children"][ + _key_h3 + ]["ui"] + ) h3_global_counter += 1 else: @@ -1130,13 +1246,17 @@ def get_h_columns_from_item_name(self, item_name=None): if td[_key_h1]["children"][_key_h2]["children"]: # loop through all the h3 and look for item_name. If found # we are done - for _key_h3 in td[_key_h1]["children"][_key_h2]["children"].keys(): + for _key_h3 in td[_key_h1]["children"][_key_h2][ + "children" + ].keys(): if item_name == _key_h3: # we found the item name at the h3 layer, # no leaf below, so we are done list_parent_ui.append(td[_key_h1]["ui"]) - list_parent_ui.append(td[_key_h1]["children"][_key_h2]["ui"]) + list_parent_ui.append( + td[_key_h1]["children"][_key_h2]["ui"] + ) return { "h1": [], "h2": [], @@ -1188,29 +1308,48 @@ def set_h_indexes(location, h1=None, h2=None, h3=None): for _key_h1 in td.keys(): # if there are children, we need to use addParent if td[_key_h1]["children"]: - _h1_parent = self.addParent(absolute_parent, td[_key_h1]["name"], _key_h1) + _h1_parent = self.addParent( + absolute_parent, td[_key_h1]["name"], _key_h1 + ) td[_key_h1]["ui"] = _h1_parent tree_ui["h1"].append(_h1_parent) for _key_h2 in td[_key_h1]["children"].keys(): # if there are children, we need to use addParent if td[_key_h1]["children"][_key_h2]["children"]: - _h2_parent = self.addParent(_h1_parent, td[_key_h1]["children"][_key_h2]["name"], _key_h2) + _h2_parent = self.addParent( + _h1_parent, + td[_key_h1]["children"][_key_h2]["name"], + _key_h2, + ) td[_key_h1]["children"][_key_h2]["ui"] = _h2_parent tree_ui["h2"].append(_h2_parent) for _key_h3 in td[_key_h1]["children"][_key_h2]["children"]: _h3_child = self.addChild( - _h2_parent, td[_key_h1]["children"][_key_h2]["children"][_key_h3]["name"], _key_h3 + _h2_parent, + td[_key_h1]["children"][_key_h2]["children"][_key_h3][ + "name" + ], + _key_h3, ) - td[_key_h1]["children"][_key_h2]["children"][_key_h3]["ui"] = _h3_child + td[_key_h1]["children"][_key_h2]["children"][_key_h3][ + "ui" + ] = _h3_child - set_h_indexes(td[_key_h1]["children"][_key_h2]["children"][_key_h3], h3=h3_index) + set_h_indexes( + td[_key_h1]["children"][_key_h2]["children"][_key_h3], + h3=h3_index, + ) tree_ui["h3"].append(_h3_child) h3_index += 1 else: # key_h2 has no children, it's a leaf - _h3_child = self.addChild(_h1_parent, td[_key_h1]["children"][_key_h2]["name"], _key_h2) + _h3_child = self.addChild( + _h1_parent, + td[_key_h1]["children"][_key_h2]["name"], + _key_h2, + ) td[_key_h1]["children"][_key_h2]["ui"] = _h3_child tree_ui["h2"].append(_h3_child) tree_ui["h3"].append(None) @@ -1282,7 +1421,9 @@ def ok_clicked(self): name_config = self.get_defined_name_config() if name_config: self.parent.save_as_config_name_selected(name=name_config) - self.grand_parent.ui.statusbar.showMessage(f"New configuration saved ({name_config})", 8000) + self.grand_parent.ui.statusbar.showMessage( + f"New configuration saved ({name_config})", 8000 + ) self.grand_parent.ui.statusbar.setStyleSheet("color: green") self.close() @@ -1491,14 +1632,18 @@ def right_click(self): self.remove_this_config(config=list_config_displayed[_index]) def remove_this_config(self, config): - config_dict = ConfigHandler.remove_this_config(config=self.parent.config_dict, key=config) + config_dict = ConfigHandler.remove_this_config( + config=self.parent.config_dict, key=config + ) self.parent.config_dict = config_dict # import pprint # pprint.pprint(config_dict) ConfigHandler.lazy_export_config(config_dict=config_dict) def activate_this_config(self, config): - config_dict = ConfigHandler.deactivate_all_config(config=self.parent.config_dict) + config_dict = ConfigHandler.deactivate_all_config( + config=self.parent.config_dict + ) config_dict = ConfigHandler.activate_this_config(config=config_dict, key=config) self.parent.config_dict = config_dict ConfigHandler.lazy_export_config(config_dict=config_dict) diff --git a/notebooks/__code/advanced_roi_selection_ui/advanced_roi_selection_ui.py b/notebooks/__code/advanced_roi_selection_ui/advanced_roi_selection_ui.py index 923be437b..37d142e27 100755 --- a/notebooks/__code/advanced_roi_selection_ui/advanced_roi_selection_ui.py +++ b/notebooks/__code/advanced_roi_selection_ui/advanced_roi_selection_ui.py @@ -7,7 +7,13 @@ from IPython.display import HTML, display from NeuNorm.normalization import Normalization from qtpy import QtGui -from qtpy.QtWidgets import QMainWindow, QProgressBar, QTableWidgetItem, QTableWidgetSelectionRange, QVBoxLayout +from qtpy.QtWidgets import ( + QMainWindow, + QProgressBar, + QTableWidgetItem, + QTableWidgetSelectionRange, + QVBoxLayout, +) from __code import load_ui from __code.config import ( @@ -65,7 +71,9 @@ def __init__( self.list_of_files = list_of_files if percentage_of_data_to_use is None: - percentage_of_data_to_use = percentage_of_images_to_use_for_roi_selection + percentage_of_data_to_use = ( + percentage_of_images_to_use_for_roi_selection + ) self.percentage_of_data_to_use = percentage_of_data_to_use self.mandatory_regions = mandatory_regions @@ -84,7 +92,8 @@ def __init__( super(QMainWindow, self).__init__(parent) ui_full_path = os.path.join( - os.path.dirname(os.path.dirname(os.path.dirname(__file__))), os.path.join("ui", "ui_roi_selection.ui") + os.path.dirname(os.path.dirname(os.path.dirname(__file__))), + os.path.join("ui", "ui_roi_selection.ui"), ) self.ui = load_ui(ui_full_path, baseinstance=self) @@ -148,7 +157,9 @@ def __built_html_table_row_3_columns(self, name, nbr, height, width): def recap(self): """Display nbr of files loaded and size. This can be used to figure why a normalization failed""" - [nbr_sample, height_sample, width_sample] = self.__get_recap(self.o_norm.data["sample"]["data"]) + [nbr_sample, height_sample, width_sample] = self.__get_recap( + self.o_norm.data["sample"]["data"] + ) [nbr_ob, height_ob, width_ob] = self.__get_recap(self.o_norm.data["ob"]["data"]) [nbr_df, height_df, width_df] = self.__get_recap(self.o_norm.data["df"]["data"]) @@ -156,7 +167,9 @@ def recap(self): '" ) - html += self.__built_html_table_row_3_columns("sample", nbr_sample, height_sample, width_sample) + html += self.__built_html_table_row_3_columns( + "sample", nbr_sample, height_sample, width_sample + ) html += self.__built_html_table_row_3_columns("ob", nbr_ob, height_ob, width_ob) html += self.__built_html_table_row_3_columns("df", nbr_df, height_df, width_df) html += "
TypeNumber' + "Size (height*width)
" @@ -183,7 +196,9 @@ def integrate_images(self): random_list = random.sample(range(0, nbr_files), nbr_files_to_use) if self.o_norm: - list_data_to_use = [self.o_norm.data["sample"]["data"][_index] for _index in random_list] + list_data_to_use = [ + self.o_norm.data["sample"]["data"][_index] for _index in random_list + ] else: o_norm = Normalization() list_of_files = np.array(self.list_of_files) @@ -380,7 +395,9 @@ def roi_manually_moved(self): _roi = list_roi[_row] roi_id = _roi["id"] - region = roi_id.getArraySlice(self.integrated_image, self.ui.image_view.imageItem) + region = roi_id.getArraySlice( + self.integrated_image, self.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop @@ -437,7 +454,9 @@ def add_rois(self, list_roi=None): width_int = np.abs(x0_int - int(_roi["x1"])) height_int = np.abs(y0_int - int(_roi["y1"])) - _roi_id = self.init_roi(x0=x0_int, y0=y0_int, width=width_int, height=height_int) + _roi_id = self.init_roi( + x0=x0_int, y0=y0_int, width=width_int, height=height_int + ) list_roi[key]["id"] = _roi_id self.list_roi = list_roi @@ -493,7 +512,9 @@ def add_roi_button_clicked(self): width_int = np.abs(x0_int - int(_x1)) height_int = np.abs(y0_int - int(_y1)) - _roi_id = self.init_roi(x0=x0_int, y0=y0_int, width=width_int, height=height_int) + _roi_id = self.init_roi( + x0=x0_int, y0=y0_int, width=width_int, height=height_int + ) _roi["id"] = _roi_id list_roi[_row] = _roi diff --git a/notebooks/__code/bin/bin_images.py b/notebooks/__code/bin/bin_images.py index ebe717c4d..5e9943cb9 100755 --- a/notebooks/__code/bin/bin_images.py +++ b/notebooks/__code/bin/bin_images.py @@ -24,7 +24,10 @@ def __init__(self, working_dir=""): def select_images(self): _instruction = "Select images to bin" self.images_ui = fileselector.FileSelectorPanel( - instruction=_instruction, start_dir=self.working_dir, multiple=True, next=self.load + instruction=_instruction, + start_dir=self.working_dir, + multiple=True, + next=self.load, ) self.images_ui.show() @@ -41,7 +44,9 @@ def load(self, list_images): def __calculate_image_dimension(self): _image_0 = self.data[0] - [self.image_dimension["height"], self.image_dimension["width"]] = np.shape(_image_0) + [self.image_dimension["height"], self.image_dimension["width"]] = np.shape( + _image_0 + ) def __bin_parameter_changed(self, sender): new_width_bin = int(self.bin_width_para.value) @@ -64,9 +69,16 @@ def select_bin_parameter(self): _height = self.image_dimension["height"] left_widgets = widgets.VBox( [ - widgets.HTML(value="Current Image Size:", layout=widgets.Layout(width="250px")), - widgets.Label(f"Width: {_width} pixels", layout=widgets.Layout(width="100%")), - widgets.Label(f"Height: {_height} pixels", layout=widgets.Layout(width="100%")), + widgets.HTML( + value="Current Image Size:", + layout=widgets.Layout(width="250px"), + ), + widgets.Label( + f"Width: {_width} pixels", layout=widgets.Layout(width="100%") + ), + widgets.Label( + f"Height: {_height} pixels", layout=widgets.Layout(width="100%") + ), ] ) @@ -91,7 +103,9 @@ def select_bin_parameter(self): center_widgets = widgets.VBox( [ - widgets.HTML("Bin Parameter:", layout=widgets.Layout(width="250px")), + widgets.HTML( + "Bin Parameter:", layout=widgets.Layout(width="250px") + ), self.bin_width_para, self.bin_height_para, ] @@ -99,9 +113,15 @@ def select_bin_parameter(self): self.right_widgets = widgets.VBox( [ - widgets.HTML("New Image Size:", layout=widgets.Layout(width="250px")), - widgets.Label(f"Width: {250} pixels", layout=widgets.Layout(width="100%")), - widgets.Label(f"Height: {250} pixels", layout=widgets.Layout(width="100%")), + widgets.HTML( + "New Image Size:", layout=widgets.Layout(width="250px") + ), + widgets.Label( + f"Width: {250} pixels", layout=widgets.Layout(width="100%") + ), + widgets.Label( + f"Height: {250} pixels", layout=widgets.Layout(width="100%") + ), ] ) @@ -144,7 +164,9 @@ def rebin_data(self, data=[]): new_width = int(new_width) _new_data = data[0:new_height, 0:new_width] - _new_data = _new_data.reshape(_nbr_height_bin, height_bin, _nbr_width_bin, width_bin) + _new_data = _new_data.reshape( + _nbr_height_bin, height_bin, _nbr_width_bin, width_bin + ) data_rebinned = _new_data.mean(axis=3).mean(axis=1) return data_rebinned @@ -160,7 +182,9 @@ def export(self, output_folder): # output_folder = os.path.abspath(os.path.join(self.output_folder_ui.selected, # "{}_rebin_by_{}".format(input_folder, self.bin_value))) bin_string = f"{self.bin_height_value}height_{self.bin_width_value}width" - output_folder = os.path.abspath(os.path.join(output_folder, f"{input_folder}_rebin_by_{bin_string}")) + output_folder = os.path.abspath( + os.path.join(output_folder, f"{input_folder}_rebin_by_{bin_string}") + ) utilities.make_dir(dir=output_folder, overwrite=False) w = widgets.IntProgress() @@ -176,4 +200,10 @@ def export(self, output_folder): w.value = _index + 1 - display(HTML('File created in ' + output_folder + "")) + display( + HTML( + 'File created in ' + + output_folder + + "" + ) + ) diff --git a/notebooks/__code/bragg_edge/bragg_edge.py b/notebooks/__code/bragg_edge/bragg_edge.py index 1b4a49e6e..fcff8b64c 100755 --- a/notebooks/__code/bragg_edge/bragg_edge.py +++ b/notebooks/__code/bragg_edge/bragg_edge.py @@ -11,10 +11,18 @@ from NeuNorm.normalization import Normalization from neutronbraggedge.braggedge import BraggEdge as BraggEdgeLibrary from neutronbraggedge.experiment_handler import * -from neutronbraggedge.material_handler.retrieve_material_metadata import RetrieveMaterialMetadata +from neutronbraggedge.material_handler.retrieve_material_metadata import ( + RetrieveMaterialMetadata, +) from plotly.offline import iplot from qtpy import QtGui -from qtpy.QtWidgets import QMainWindow, QProgressBar, QTableWidgetItem, QTableWidgetSelectionRange, QVBoxLayout +from qtpy.QtWidgets import ( + QMainWindow, + QProgressBar, + QTableWidgetItem, + QTableWidgetSelectionRange, + QVBoxLayout, +) from __code import file_handler, load_ui from __code.ipywe import fileselector @@ -37,14 +45,20 @@ def full_list_elements(self): box4 = widgets.HBox( [ - widgets.Label("List of elements", layout=widgets.Layout(width=self.label_width)), - widgets.Select(options=self.list_returned, layout=widgets.Layout(width="20%")), + widgets.Label( + "List of elements", layout=widgets.Layout(width=self.label_width) + ), + widgets.Select( + options=self.list_returned, layout=widgets.Layout(width="20%") + ), ] ) box5 = widgets.HBox( [ - widgets.Label("Nbr Bragg Edges", layout=widgets.Layout(width=self.label_width)), + widgets.Label( + "Nbr Bragg Edges", layout=widgets.Layout(width=self.label_width) + ), widgets.IntText(8, layout=widgets.Layout(width="20%")), ] ) @@ -64,14 +78,20 @@ def list_elements(self): box4 = widgets.HBox( [ - widgets.Label("List of elements", layout=widgets.Layout(width=self.label_width)), - widgets.Text(",".join(self.list_of_elements), layout=widgets.Layout(width="20%")), + widgets.Label( + "List of elements", layout=widgets.Layout(width=self.label_width) + ), + widgets.Text( + ",".join(self.list_of_elements), layout=widgets.Layout(width="20%") + ), ] ) box5 = widgets.HBox( [ - widgets.Label("Nbr Bragg Edges", layout=widgets.Layout(width=self.label_width)), + widgets.Label( + "Nbr Bragg Edges", layout=widgets.Layout(width=self.label_width) + ), widgets.Text(str(8), layout=widgets.Layout(width="20%")), ] ) @@ -92,7 +112,10 @@ def exp_setup(self): box3 = widgets.HBox( [ - widgets.Label("detector offset (microS)", layout=widgets.Layout(width=self.label_width)), + widgets.Label( + "detector offset (microS)", + layout=widgets.Layout(width=self.label_width), + ), widgets.Text(str(3700), layout=widgets.Layout(width="20%")), ] ) @@ -109,7 +132,9 @@ def list_powder_bragg_edges(self): list_of_elements = [_element.strip() for _element in list_of_elements] number_of_bragg_edges = int(self.nbr_bragg_edges_ui.value) - _handler = BraggEdgeLibrary(material=list_of_elements, number_of_bragg_edges=number_of_bragg_edges) + _handler = BraggEdgeLibrary( + material=list_of_elements, number_of_bragg_edges=number_of_bragg_edges + ) self.bragg_edges = _handler.bragg_edges self.hkl = _handler.hkl self.handler = _handler @@ -147,7 +172,11 @@ def load_data(self, folder_selected): self.list_files = o_norm.data["sample"]["file_name"] display( - HTML('' + str(len(list_files)) + " files have been loaded") + HTML( + '' + + str(len(list_files)) + + " files have been loaded" + ) ) # define time spectra file @@ -179,7 +208,13 @@ def load_time_spectra(self): def save_time_spectra(self, file): self.spectra_file = file - display(HTML(' Spectra File : ' + self.spectra_file + "")) + display( + HTML( + ' Spectra File : ' + + self.spectra_file + + "" + ) + ) def select_time_spectra_file(self): self.working_dir = os.path.dirname(self.list_files[0]) @@ -209,11 +244,20 @@ def how_many_data_to_use_to_select_sample_roi(self): init_value = 1 box1 = widgets.HBox( [ - widgets.Label("Nbr of images to use:", layout=widgets.Layout(width="15")), - widgets.IntSlider(value=init_value, max=nbr_images, min=1, layout=widgets.Layout(width="50%")), + widgets.Label( + "Nbr of images to use:", layout=widgets.Layout(width="15") + ), + widgets.IntSlider( + value=init_value, + max=nbr_images, + min=1, + layout=widgets.Layout(width="50%"), + ), ] ) - box2 = widgets.Label("(The more you select, the longer it will take to display the preview!)") + box2 = widgets.Label( + "(The more you select, the longer it will take to display the preview!)" + ) vbox = widgets.VBox([box1, box2]) display(vbox) self.number_of_data_to_use_ui = box1.children[1] @@ -221,7 +265,9 @@ def how_many_data_to_use_to_select_sample_roi(self): def define_sample_roi(self): nbr_data_to_use = int(self.number_of_data_to_use_ui.value) nbr_images = len(self.data) - list_of_indexes_to_keep = random.sample(list(range(nbr_images)), nbr_data_to_use) + list_of_indexes_to_keep = random.sample( + list(range(nbr_images)), nbr_data_to_use + ) final_array = [] for _index in list_of_indexes_to_keep: final_array.append(self.data[_index]) @@ -231,7 +277,9 @@ def define_sample_roi(self): def define_integrated_sample_to_use(self): nbr_data_to_use = int(self.number_of_data_to_use_ui.value) nbr_images = len(self.data) - list_of_indexes_to_keep = random.sample(list(range(nbr_images)), nbr_data_to_use) + list_of_indexes_to_keep = random.sample( + list(range(nbr_images)), nbr_data_to_use + ) final_array = [] for _index in list_of_indexes_to_keep: final_array.append(self.data[_index]) @@ -271,7 +319,9 @@ def plot(self): _hkl_string.append(_hkl_s) _hkl_formated[_material] = _hkl_string - trace = go.Scatter(x=self.lambda_array, y=self.counts_vs_file_index, mode="markers") + trace = go.Scatter( + x=self.lambda_array, y=self.counts_vs_file_index, mode="markers" + ) layout = go.Layout( width=1000, @@ -349,9 +399,17 @@ def export_table(self, output_folder): output_file_name = os.path.join(output_folder, f"bragg_edges_of_{material}.txt") - file_handler.make_ascii_file(metadata=metadata, data=data, dim="1d", output_file_name=output_file_name) + file_handler.make_ascii_file( + metadata=metadata, data=data, dim="1d", output_file_name=output_file_name + ) - display(HTML('File created : ' + output_file_name + "")) + display( + HTML( + 'File created : ' + + output_file_name + + "" + ) + ) def select_folder(self, message="", next_function=None): folder_widget = fileselector.FileSelectorPanel( @@ -390,7 +448,8 @@ def __init__(self, parent=None, data=None, instruction="", next=None): super(QMainWindow, self).__init__(parent) ui_full_path = os.path.join( - os.path.dirname(os.path.dirname(os.path.dirname(__file__))), os.path.join("ui", "ui_roi_selection.ui") + os.path.dirname(os.path.dirname(os.path.dirname(__file__))), + os.path.join("ui", "ui_roi_selection.ui"), ) self.ui = load_ui(ui_full_path, baseinstance=self) @@ -647,7 +706,9 @@ def roi_manually_moved(self): _roi = list_roi[_row] roi_id = _roi["id"] - region = roi_id.getArraySlice(self.integrated_image, self.ui.image_view.imageItem) + region = roi_id.getArraySlice( + self.integrated_image, self.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop @@ -721,7 +782,9 @@ def add_roi_button_clicked(self): width_int = np.abs(x0_int - int(_x1)) height_int = np.abs(y0_int - int(_y1)) - _roi_id = self.init_roi(x0=x0_int, y0=y0_int, width=width_int, height=height_int) + _roi_id = self.init_roi( + x0=x0_int, y0=y0_int, width=width_int, height=height_int + ) _roi["id"] = _roi_id list_roi[_row] = _roi diff --git a/notebooks/__code/bragg_edge/bragg_edge_normalization.py b/notebooks/__code/bragg_edge/bragg_edge_normalization.py index 4c8b617fa..ae85c1b7c 100755 --- a/notebooks/__code/bragg_edge/bragg_edge_normalization.py +++ b/notebooks/__code/bragg_edge/bragg_edge_normalization.py @@ -62,7 +62,9 @@ def load_data(self, folder_selected): else: # ask for spectra file - logging.info("-> spectra file NOT FOUND! Asking user to select time spectra file") + logging.info( + "-> spectra file NOT FOUND! Asking user to select time spectra file" + ) self.select_time_spectra_file() def select_time_spectra_file(self): @@ -77,7 +79,9 @@ def select_time_spectra_file(self): self.time_spectra_ui.show() self.cancel_button = widgets.Button( - description="or Do Not Select any Time Spectra", button_style="info", layout=widgets.Layout(width="100%") + description="or Do Not Select any Time Spectra", + button_style="info", + layout=widgets.Layout(width="100%"), ) display(self.cancel_button) self.cancel_button.on_click(self.cancel_time_spectra_selection) @@ -91,7 +95,11 @@ def cancel_time_spectra_selection(self, value): logging.info("User cancel loading time spectra!") self.time_spectra_ui.remove() self.cancel_button.close() - display(HTML('NO Spectra File loaded! ')) + display( + HTML( + 'NO Spectra File loaded! ' + ) + ) def load_files(self, data_type="sample", folder=None): self.starting_dir = os.path.dirname(folder) @@ -144,7 +152,9 @@ def normalization_settings_widgets(self): ## button self.select_ob_widget = widgets.Button( - description="Select OB ...", button_style="success", layout=widgets.Layout(width="100%") + description="Select OB ...", + button_style="success", + layout=widgets.Layout(width="100%"), ) self.select_ob_widget.on_click(self.select_ob_folder) @@ -152,15 +162,23 @@ def normalization_settings_widgets(self): spacer = widgets.HTML(value="
") ## nbr of images to use - nbr_images_to_use_label = widgets.Label("Nbr of images to use in preview", layout=widgets.Layout(width="20%")) + nbr_images_to_use_label = widgets.Label( + "Nbr of images to use in preview", layout=widgets.Layout(width="20%") + ) nbr_of_images_to_use_in_preview = self.get_nbr_of_images_to_use_in_preview() self.nbr_images_slider_with_ob = widgets.IntSlider( - min=2, max=len(self.list_files), value=nbr_of_images_to_use_in_preview, layout=widgets.Layout(width="80%") + min=2, + max=len(self.list_files), + value=nbr_of_images_to_use_in_preview, + layout=widgets.Layout(width="80%"), ) hbox_1 = widgets.HBox([nbr_images_to_use_label, self.nbr_images_slider_with_ob]) self.select_roi_widget_with_ob = widgets.Button( - description="OPTIONAL: Select Region of interest away from " "sample " "to " "improve normalization", + description="OPTIONAL: Select Region of interest away from " + "sample " + "to " + "improve normalization", layout=widgets.Layout(width="100%"), ) self.select_roi_widget_with_ob.on_click(self.select_roi_with_ob) @@ -178,18 +196,25 @@ def normalization_settings_widgets(self): ## nbr of images to use self.nbr_images_slider_without_ob = widgets.IntSlider( - min=2, max=len(self.list_files), value=nbr_of_images_to_use_in_preview, layout=widgets.Layout(width="80%") + min=2, + max=len(self.list_files), + value=nbr_of_images_to_use_in_preview, + layout=widgets.Layout(width="80%"), + ) + hbox_without_ob = widgets.HBox( + [nbr_images_to_use_label, self.nbr_images_slider_without_ob] ) - hbox_without_ob = widgets.HBox([nbr_images_to_use_label, self.nbr_images_slider_without_ob]) select_roi_widget_without_ob = widgets.Button( - description="MANDATORY: Select region of interest " "away from " "sample", + description="MANDATORY: Select region of interest away from sample", button_style="success", layout=widgets.Layout(width="100%"), ) select_roi_widget_without_ob.on_click(self.select_roi_without_ob) - vbox_without_ob = widgets.VBox([hbox_without_ob, spacer, select_roi_widget_without_ob]) + vbox_without_ob = widgets.VBox( + [hbox_without_ob, spacer, select_roi_widget_without_ob] + ) self.accordion = widgets.Accordion(children=[vbox_with_ob, vbox_without_ob]) self.accordion.set_title(0, "With OB") @@ -242,17 +267,37 @@ def check_data_array_sizes(self): len_sample = len(self.o_norm.data["sample"]["file_name"]) if len_ob == len_sample: - display(HTML(' Sample and OB have the same size!')) + display( + HTML( + ' Sample and OB have the same size!' + ) + ) return if len_ob < len_sample: - self.o_norm.data["sample"]["data"] = self.o_norm.data["sample"]["data"][0:len_ob] - self.o_norm.data["sample"]["file_name"] = self.o_norm.data["sample"]["file_name"][0:len_ob] - display(HTML(' Truncated Sample array to match OB!')) + self.o_norm.data["sample"]["data"] = self.o_norm.data["sample"]["data"][ + 0:len_ob + ] + self.o_norm.data["sample"]["file_name"] = self.o_norm.data["sample"][ + "file_name" + ][0:len_ob] + display( + HTML( + ' Truncated Sample array to match OB!' + ) + ) else: - self.o_norm.data["ob"]["data"] = self.o_norm.data["ob"]["data"][0:len_sample] - self.o_norm.data["ob"]["file_name"] = self.o_norm.data["ob"]["file_name"][0:len_sample] - display(HTML(' Truncated OB array to match Sample!')) + self.o_norm.data["ob"]["data"] = self.o_norm.data["ob"]["data"][ + 0:len_sample + ] + self.o_norm.data["ob"]["file_name"] = self.o_norm.data["ob"]["file_name"][ + 0:len_sample + ] + display( + HTML( + ' Truncated OB array to match Sample!' + ) + ) def load_time_spectra(self): _tof_handler = TOF(filename=self.spectra_file) @@ -274,12 +319,16 @@ def how_many_data_to_use_to_select_sample_roi(self): init_value = 1 box1 = widgets.HBox( [ - widgets.Label("Nbr of images to use:", layout=widgets.Layout(width="15")), + widgets.Label( + "Nbr of images to use:", layout=widgets.Layout(width="15") + ), widgets.IntSlider(value=init_value, max=nbr_images, min=1), ] ) # layout=widgets.Layout(width='50%'))]) - box2 = widgets.Label("(The more you select, the longer it will take to display the preview!)") + box2 = widgets.Label( + "(The more you select, the longer it will take to display the preview!)" + ) vbox = widgets.VBox([box1, box2]) display(vbox) self.number_of_data_to_use_ui = box1.children[1] @@ -288,7 +337,9 @@ def get_image_to_use_for_display(self, nbr_data_to_use=2): _data = self.o_norm.data["sample"]["data"] nbr_images = len(_data) - list_of_indexes_to_keep = random.sample(list(range(nbr_images)), nbr_data_to_use) + list_of_indexes_to_keep = random.sample( + list(range(nbr_images)), nbr_data_to_use + ) final_array = [] for _index in list_of_indexes_to_keep: @@ -316,8 +367,14 @@ def normalization(self): def normalization_without_ob(self, list_rois): logging.info("Running normalization without OB") if list_rois is None: - logging.info("-> no ROIs found! At least one ROI must be provided. Normalization Aborted!") - display(HTML(' You need to provide a ROI!')) + logging.info( + "-> no ROIs found! At least one ROI must be provided. Normalization Aborted!" + ) + display( + HTML( + ' You need to provide a ROI!' + ) + ) return else: @@ -332,8 +389,14 @@ def normalization_without_ob(self, list_rois): list_o_roi.append(ROI(x0=_x0, y0=_y0, x1=_x1, y1=_y1)) logging.info(f"-> Normalization with {len(list_o_roi)} ROIs") - self.o_norm.normalization(roi=list_o_roi, use_only_sample=True, notebook=True) - display(HTML(' Normalization DONE! ')) + self.o_norm.normalization( + roi=list_o_roi, use_only_sample=True, notebook=True + ) + display( + HTML( + ' Normalization DONE! ' + ) + ) logging.info("-> Done!") def normalization_with_ob(self, list_rois): @@ -354,21 +417,31 @@ def normalization_with_ob(self, list_rois): logging.info(f"-> Normalization with {len(list_o_roi)} ROIs") self.o_norm.normalization(roi=list_o_roi, notebook=True) - display(HTML(' Normalization DONE! ')) + display( + HTML( + ' Normalization DONE! ' + ) + ) logging.info("-> Done!") def export_normalized_data(self): self.o_folder = FileFolderBrowser( - working_dir=self.working_dir, next_function=self.export_normalized_data_step2, ipts_folder=self.ipts_folder + working_dir=self.working_dir, + next_function=self.export_normalized_data_step2, + ipts_folder=self.ipts_folder, + ) + self.o_folder.select_output_folder_with_new( + instruction="Select where to create the normalized data ..." ) - self.o_folder.select_output_folder_with_new(instruction="Select where to create the normalized data ...") def export_normalized_data_step2(self, output_folder): logging.info("export normalized data") logging.info(f"-> output_folder: {output_folder}") output_folder = os.path.abspath(output_folder) self.o_folder.list_output_folders_ui.shortcut_buttons.close() - normalized_export_folder = str(Path(output_folder) / (self.data_folder_name + "_normalized")) + normalized_export_folder = str( + Path(output_folder) / (self.data_folder_name + "_normalized") + ) file_handler.make_or_reset_folder(normalized_export_folder) self.o_norm.export(folder=normalized_export_folder) @@ -381,8 +454,14 @@ def export_normalized_data_step2(self, output_folder): ) if self.spectra_file: logging.info("-> time spectra copied to output folder!") - file_handler.copy_files_to_folder(list_files=[self.spectra_file], output_folder=normalized_export_folder) - display(HTML(' Copied time spectra file to same folder ')) + file_handler.copy_files_to_folder( + list_files=[self.spectra_file], output_folder=normalized_export_folder + ) + display( + HTML( + ' Copied time spectra file to same folder ' + ) + ) else: logging.info("->No time spectra copied!") @@ -393,8 +472,12 @@ def calculate_counts_vs_file_index_of_regions_selected(self, list_roi=None): nbr_data = len(data) box_ui = widgets.HBox( [ - widgets.Label("Calculate Counts vs lambda", layout=widgets.Layout(width="20%")), - widgets.IntProgress(min=0, max=nbr_data, value=0, layout=widgets.Layout(width="50%")), + widgets.Label( + "Calculate Counts vs lambda", layout=widgets.Layout(width="20%") + ), + widgets.IntProgress( + min=0, max=nbr_data, value=0, layout=widgets.Layout(width="50%") + ), ] ) progress_bar = box_ui.children[1] @@ -423,7 +506,9 @@ def calculate_counts_vs_file_index_of_regions_selected(self, list_roi=None): box_ui.close() def plot(self): - trace = go.Scatter(x=self.lambda_array, y=self.counts_vs_file_index, mode="markers") + trace = go.Scatter( + x=self.lambda_array, y=self.counts_vs_file_index, mode="markers" + ) layout = go.Layout( height=500, @@ -438,8 +523,12 @@ def plot(self): iplot(figure) def select_output_data_folder(self): - o_folder = FileFolderBrowser(working_dir=self.working_dir, next_function=self.export_data) - o_folder.select_output_folder(instruction="Select where to create the ascii file...") + o_folder = FileFolderBrowser( + working_dir=self.working_dir, next_function=self.export_data + ) + o_folder.select_output_folder( + instruction="Select where to create the ascii file..." + ) def make_output_file_name(self, output_folder="", input_folder=""): file_name = os.path.basename(input_folder) + "_counts_vs_lambda_tof.txt" @@ -447,7 +536,9 @@ def make_output_file_name(self, output_folder="", input_folder=""): def export_data(self, output_folder): input_folder = os.path.dirname(self.o_norm.data["sample"]["file_name"][0]) - output_file_name = self.make_output_file_name(output_folder=output_folder, input_folder=input_folder) + output_file_name = self.make_output_file_name( + output_folder=output_folder, input_folder=input_folder + ) lambda_array = self.lambda_array counts_vs_file_index = self.counts_vs_file_index @@ -465,15 +556,21 @@ def export_data(self, output_folder): _y0 = roi["y0"] _x1 = roi["x1"] _y1 = roi["y1"] - metadata.append(f"# ROI {index}: x0={_x0}, y0={_y0}, x1={_x1}, y1={_y1}") + metadata.append( + f"# ROI {index}: x0={_x0}, y0={_y0}, x1={_x1}, y1={_y1}" + ) metadata.append("#") metadata.append("# tof (micros), lambda (Angstroms), Average transmission") data = [] - for _t, _l, _c in zip(tof_array, lambda_array, counts_vs_file_index, strict=False): + for _t, _l, _c in zip( + tof_array, lambda_array, counts_vs_file_index, strict=False + ): data.append(f"{_t}, {_l}, {_c}") - file_handler.make_ascii_file(metadata=metadata, data=data, output_file_name=output_file_name, dim="1d") + file_handler.make_ascii_file( + metadata=metadata, data=data, output_file_name=output_file_name, dim="1d" + ) if os.path.exists(output_file_name): display( @@ -494,7 +591,9 @@ def export_data(self, output_folder): ) def select_output_table_folder(self): - o_folder = FileFolderBrowser(working_dir=self.working_dir, next_function=self.export_table) + o_folder = FileFolderBrowser( + working_dir=self.working_dir, next_function=self.export_table + ) o_folder.select_output_folder() def export_table(self, output_folder): @@ -521,9 +620,17 @@ def export_table(self, output_folder): output_file_name = os.path.join(output_folder, f"bragg_edges_of_{material}.txt") - file_handler.make_ascii_file(metadata=metadata, data=data, dim="1d", output_file_name=output_file_name) + file_handler.make_ascii_file( + metadata=metadata, data=data, dim="1d", output_file_name=output_file_name + ) - display(HTML('File created : ' + output_file_name + "")) + display( + HTML( + 'File created : ' + + output_file_name + + "" + ) + ) def select_folder(self, message="", next_function=None): folder_widget = ipywe.fileselector.FileSelectorPanel( diff --git a/notebooks/__code/bragg_edge/bragg_edge_peak_fitting.py b/notebooks/__code/bragg_edge/bragg_edge_peak_fitting.py index d2b20569d..0a46a0ebe 100755 --- a/notebooks/__code/bragg_edge/bragg_edge_peak_fitting.py +++ b/notebooks/__code/bragg_edge/bragg_edge_peak_fitting.py @@ -20,7 +20,9 @@ from __code.bragg_edge.kropff import Kropff from __code.bragg_edge.kropff_fitting_job_handler import KropffFittingJobHandler from __code.bragg_edge.march_dollase import MarchDollase -from __code.bragg_edge.march_dollase_fitting_job_handler import MarchDollaseFittingJobHandler +from __code.bragg_edge.march_dollase_fitting_job_handler import ( + MarchDollaseFittingJobHandler, +) from __code.bragg_edge.peak_fitting_initialization import PeakFittingInitialization from __code.bragg_edge.peak_fitting_interface_initialization import Initialization from __code.table_handler import TableHandler @@ -42,7 +44,15 @@ def divide_normalized_data_by_OB_white_beam(self): class Interface(QMainWindow): fitting_parameters_init = { - "kropff": {"a0": 1, "b0": 1, "ahkl": 1, "bhkl": 1, "ldahkl": 1e-8, "tau": 1, "sigma": [1e-7, 1e-6, 1e-5]} + "kropff": { + "a0": 1, + "b0": 1, + "ahkl": 1, + "bhkl": 1, + "ldahkl": 1e-8, + "tau": 1, + "sigma": [1e-7, 1e-6, 1e-5], + } } bragg_edge_range = [5, 20] @@ -52,13 +62,17 @@ class Interface(QMainWindow): selection_roi_rgb = (62, 13, 244) roi_settings = { - "color": QtGui.QColor(selection_roi_rgb[0], selection_roi_rgb[1], selection_roi_rgb[2]), + "color": QtGui.QColor( + selection_roi_rgb[0], selection_roi_rgb[1], selection_roi_rgb[2] + ), "width": 0.01, "position": [10, 10], } shrinking_roi_rgb = (13, 214, 244) shrinking_roi_settings = { - "color": QtGui.QColor(shrinking_roi_rgb[0], shrinking_roi_rgb[1], shrinking_roi_rgb[2]), + "color": QtGui.QColor( + shrinking_roi_rgb[0], shrinking_roi_rgb[1], shrinking_roi_rgb[2] + ), "width": 0.01, "dashes_pattern": [4, 2], } @@ -69,7 +83,11 @@ class Interface(QMainWindow): previous_roi_selection = {"width": None, "height": None} image_size = {"width": None, "height": None} roi_id = None - xaxis_label = {"index": "File index", "tof": "TOF (\u00b5s)", "lambda": "\u03bb (\u212b)"} + xaxis_label = { + "index": "File index", + "tof": "TOF (\u00b5s)", + "lambda": "\u03bb (\u212b)", + } fitting_rois = { "kropff": { "step1": None, @@ -81,7 +99,11 @@ class Interface(QMainWindow): is_file_imported = False # True only when the import button has been used bragg_edge_range_ui = None - kropff_fitting_range = {"high": [None, None], "low": [None, None], "bragg_peak": [None, None]} + kropff_fitting_range = { + "high": [None, None], + "low": [None, None], + "bragg_peak": [None, None], + } fitting_peak_ui = None # vertical line in fitting view (tab 2) fitting_procedure_started = {"march-dollase": False, "kropff": False} @@ -249,7 +271,9 @@ def update_time_spectra(self): distance_source_detector_m = float(self.ui.distance_detector_sample.text()) self.ui.statusbar.showMessage("", 100) # 10s except ValueError: - self.ui.statusbar.showMessage("distance source detector input is WRONG", 120000) # 2mn + self.ui.statusbar.showMessage( + "distance source detector input is WRONG", 120000 + ) # 2mn self.ui.statusbar.setStyleSheet("color: red") return @@ -257,7 +281,9 @@ def update_time_spectra(self): detector_offset_micros = float(self.ui.detector_offset.text()) self.ui.statusbar.showMessage("", 100) # 10s except ValueError: - self.ui.statusbar.showMessage("detector offset input is WRONG", 120000) # 2mn + self.ui.statusbar.showMessage( + "detector offset input is WRONG", 120000 + ) # 2mn self.ui.statusbar.setStyleSheet("color: red") return @@ -277,7 +303,9 @@ def get_live_image(self): _data = self.o_norm.data["sample"]["data"] nbr_images = len(_data) - list_of_indexes_to_keep = random.sample(list(range(nbr_images)), nbr_data_to_use) + list_of_indexes_to_keep = random.sample( + list(range(nbr_images)), nbr_data_to_use + ) final_array = [] for _index in list_of_indexes_to_keep: @@ -327,7 +355,10 @@ def bragg_edge_range_changed(self): def reset_profile_of_bin_size_slider(self): max_value = np.min( - [int(str(self.ui.profile_of_bin_size_width.text())), int(str(self.ui.profile_of_bin_size_height.text()))] + [ + int(str(self.ui.profile_of_bin_size_width.text())), + int(str(self.ui.profile_of_bin_size_height.text())), + ] ) self.ui.profile_of_bin_size_slider.setMaximum(max_value) self.ui.profile_of_bin_size_slider.setValue(max_value) @@ -382,7 +413,11 @@ def update_dict_profile_to_fit(self): profile_to_fit = { "yaxis": yaxis, - "xaxis": {"index": index_selected, "tof": tof_selected, "lambda": lambda_selected}, + "xaxis": { + "index": index_selected, + "tof": tof_selected, + "lambda": lambda_selected, + }, } self.dict_profile_to_fit = profile_to_fit @@ -392,11 +427,16 @@ def fit_that_selection_pushed_by_program(self, initialize_region=True): dict_regions = o_get.all_russian_doll_region_full_infos() o_init = PeakFittingInitialization(parent=self) - fitting_input_dictionary = o_init.fitting_input_dictionary(nbr_rois=len(dict_regions)) + fitting_input_dictionary = o_init.fitting_input_dictionary( + nbr_rois=len(dict_regions) + ) o_init.set_top_keys_values( - fitting_input_dictionary, {"xaxis": x_axis, "bragg_edge_range": self.bragg_edge_range} + fitting_input_dictionary, + {"xaxis": x_axis, "bragg_edge_range": self.bragg_edge_range}, + ) + self.append_dict_regions_to_fitting_input_dictionary( + dict_regions, fitting_input_dictionary ) - self.append_dict_regions_to_fitting_input_dictionary(dict_regions, fitting_input_dictionary) # fitting_input_dictionary['xaxis'] = x_axis # fitting_input_dictionary['bragg_edge_range'] = self.bragg_edge_range @@ -424,7 +464,9 @@ def fit_that_selection_pushed_by_program(self, initialize_region=True): self.ui.actionExport.setEnabled(True) self.select_first_row_of_all_fitting_table() - def append_dict_regions_to_fitting_input_dictionary(self, dict_regions, fitting_input_dictionary): + def append_dict_regions_to_fitting_input_dictionary( + self, dict_regions, fitting_input_dictionary + ): for _row in dict_regions.keys(): _entry = dict_regions[_row] for _key in _entry.keys(): @@ -566,18 +608,27 @@ def update_profile_of_bin_slider_labels(self): def change_profile_of_bin_slider_signal(self): self.ui.profile_of_bin_size_slider.valueChanged.disconnect() - self.ui.profile_of_bin_size_slider.valueChanged.connect(self.profile_of_bin_size_slider_changed_after_import) + self.ui.profile_of_bin_size_slider.valueChanged.connect( + self.profile_of_bin_size_slider_changed_after_import + ) def update_vertical_line_in_profile_plot(self): o_get = Get(parent=self) x_axis, x_axis_label = o_get.x_axis() - bragg_edge_range = [x_axis[self.bragg_edge_range[0]], x_axis[self.bragg_edge_range[1]]] + bragg_edge_range = [ + x_axis[self.bragg_edge_range[0]], + x_axis[self.bragg_edge_range[1]], + ] if self.bragg_edge_range_ui: self.ui.profile.removeItem(self.bragg_edge_range_ui) self.bragg_edge_range_ui = pg.LinearRegionItem( - values=bragg_edge_range, orientation=None, brush=None, movable=True, bounds=None + values=bragg_edge_range, + orientation=None, + brush=None, + movable=True, + bounds=None, ) self.bragg_edge_range_ui.sigRegionChanged.connect(self.bragg_edge_range_changed) self.bragg_edge_range_ui.setZValue(-10) @@ -628,7 +679,12 @@ def export_button_clicked(self): def roi_radiobuttons_changed(self): if self.ui.square_roi_radiobutton.isChecked(): slider_visible = True - new_width = np.min([int(str(self.ui.roi_width.text())), int(str(self.ui.roi_height.text()))]) + new_width = np.min( + [ + int(str(self.ui.roi_width.text())), + int(str(self.ui.roi_height.text())), + ] + ) mode = "square" else: slider_visible = False @@ -701,30 +757,34 @@ def update_kropff_fit_table_graph(self, fit_region="high"): :param fit_region: 'high', 'low' or 'bragg_peak' """ o_gui = GuiUtility(parent=self) - fit_parameter_selected = o_gui.get_kropff_fit_parameter_selected(fit_region=fit_region) + fit_parameter_selected = o_gui.get_kropff_fit_parameter_selected( + fit_region=fit_region + ) parameter_array = [] parameter_error_array = [] fitting_input_dictionary = self.fitting_input_dictionary for _index in fitting_input_dictionary["rois"].keys(): - _parameter = fitting_input_dictionary["rois"][_index]["fitting"]["kropff"][fit_region][ - fit_parameter_selected - ] - _error = fitting_input_dictionary["rois"][_index]["fitting"]["kropff"][fit_region][ - f"{fit_parameter_selected}_error" - ] + _parameter = fitting_input_dictionary["rois"][_index]["fitting"]["kropff"][ + fit_region + ][fit_parameter_selected] + _error = fitting_input_dictionary["rois"][_index]["fitting"]["kropff"][ + fit_region + ][f"{fit_parameter_selected}_error"] parameter_array.append(_parameter) parameter_error_array.append(_error) plot_ui = o_gui.get_kropff_fit_graph_ui(fit_region=fit_region) x_array = np.arange(len(parameter_array)) - cleaned_parameter_array, cleaned_parameter_error_array = exclude_y_value_when_error_is_nan( - parameter_array, parameter_error_array + cleaned_parameter_array, cleaned_parameter_error_array = ( + exclude_y_value_when_error_is_nan(parameter_array, parameter_error_array) ) plot_ui.axes.cla() if fit_region == "bragg_peak": plot_ui.axes.set_yscale("log") - plot_ui.axes.errorbar(x_array, cleaned_parameter_array, cleaned_parameter_error_array, marker="s") + plot_ui.axes.errorbar( + x_array, cleaned_parameter_array, cleaned_parameter_error_array, marker="s" + ) plot_ui.axes.set_xlabel("Row # (see Table tab)") plot_ui.draw() @@ -760,14 +820,18 @@ def kropff_bragg_peak_right_click(self, position): def march_dollase_table_state_changed(self, state=None, row=None, column=None): o_march = MarchDollase(parent=self) if row == 0: - _widget = self.ui.march_dollase_user_input_table.cellWidget(row, column).children()[-1] + _widget = self.ui.march_dollase_user_input_table.cellWidget( + row, column + ).children()[-1] if (column == 1) or (column == 2): _textedit = _widget _textedit.setText(o_march.get_initial_parameter_value(column=column)) _textedit.setVisible(not state) elif column == 0: _label = _widget - _label.setText(f"{float(o_march.get_initial_parameter_value(column=column)):0.6f}") + _label.setText( + f"{float(o_march.get_initial_parameter_value(column=column)):0.6f}" + ) _label.setVisible(not state) else: _label = _widget diff --git a/notebooks/__code/bragg_edge/bragg_edge_peak_fitting_gui_utility.py b/notebooks/__code/bragg_edge/bragg_edge_peak_fitting_gui_utility.py index 31eabe248..9179cde29 100755 --- a/notebooks/__code/bragg_edge/bragg_edge_peak_fitting_gui_utility.py +++ b/notebooks/__code/bragg_edge/bragg_edge_peak_fitting_gui_utility.py @@ -1,5 +1,12 @@ import numpy as np -from qtpy.QtWidgets import QCheckBox, QHBoxLayout, QLabel, QLineEdit, QVBoxLayout, QWidget +from qtpy.QtWidgets import ( + QCheckBox, + QHBoxLayout, + QLabel, + QLineEdit, + QVBoxLayout, + QWidget, +) from __code.table_handler import TableHandler @@ -29,14 +36,18 @@ def get_toolbox_selected(self, toolbox_ui=None): toolbox_index = toolbox_ui.currentIndex() return toolbox_ui.itemText(toolbox_index) - def update_kropff_high_lambda_table_ui(self, row=0, a0=None, b0=None, a0_error=None, b0_error=None): + def update_kropff_high_lambda_table_ui( + self, row=0, a0=None, b0=None, a0_error=None, b0_error=None + ): table_ui = self.parent.ui.high_lda_tableWidget table_ui.item(row, 1).setText(self.cell_str_format.format(a0)) table_ui.item(row, 2).setText(self.cell_str_format.format(b0)) table_ui.item(row, 3).setText(self.cell_str_format.format(a0_error)) table_ui.item(row, 4).setText(self.cell_str_format.format(b0_error)) - def update_kropff_low_lambda_table_ui(self, row=0, ahkl=None, bhkl=None, ahkl_error=None, bhkl_error=None): + def update_kropff_low_lambda_table_ui( + self, row=0, ahkl=None, bhkl=None, ahkl_error=None, bhkl_error=None + ): table_ui = self.parent.ui.low_lda_tableWidget table_ui.item(row, 1).setText(self.cell_str_format.format(ahkl)) table_ui.item(row, 2).setText(self.cell_str_format.format(bhkl)) @@ -44,7 +55,14 @@ def update_kropff_low_lambda_table_ui(self, row=0, ahkl=None, bhkl=None, ahkl_er table_ui.item(row, 4).setText(self.cell_str_format.format(bhkl_error)) def update_kropff_bragg_edge_table_ui( - self, row=0, ldahkl=None, ldahkl_error=None, tau=None, tau_error=None, sigma=None, sigma_error=None + self, + row=0, + ldahkl=None, + ldahkl_error=None, + tau=None, + tau_error=None, + sigma=None, + sigma_error=None, ): ldahkl_error = np.nan if ldahkl_error is None else ldahkl_error tau_error = np.nan if tau_error is None else tau_error @@ -80,11 +98,17 @@ def get_kropff_fit_parameter_selected(self, fit_region="high"): """ list_fit_parameters_radio_button = { "high": { - "ui": [self.parent.ui.kropff_a0_radioButton, self.parent.ui.kropff_b0_radioButton], + "ui": [ + self.parent.ui.kropff_a0_radioButton, + self.parent.ui.kropff_b0_radioButton, + ], "name": ["a0", "b0"], }, "low": { - "ui": [self.parent.ui.kropff_ahkl_radioButton, self.parent.ui.kropff_bhkl_radioButton], + "ui": [ + self.parent.ui.kropff_ahkl_radioButton, + self.parent.ui.kropff_bhkl_radioButton, + ], "name": ["ahkl", "bhkl"], }, "bragg_peak": { @@ -97,7 +121,9 @@ def get_kropff_fit_parameter_selected(self, fit_region="high"): }, } - for _index, _ui in enumerate(list_fit_parameters_radio_button[fit_region]["ui"]): + for _index, _ui in enumerate( + list_fit_parameters_radio_button[fit_region]["ui"] + ): if _ui.isChecked(): return list_fit_parameters_radio_button[fit_region]["name"][_index] @@ -132,7 +158,11 @@ def fill_march_dollase_table(self, list_state=None, initial_parameters=None): for _row in np.arange(len(list_state)): self.parent.ui.march_dollase_user_input_table.insertRow(_row) - row_height = march_dollase_row_height[0] if _row == 0 else march_dollase_row_height["other"] + row_height = ( + march_dollase_row_height[0] + if _row == 0 + else march_dollase_row_height["other"] + ) table_ui.setRowHeight(_row, row_height) for _col in np.arange(nbr_column): @@ -144,7 +174,9 @@ def fill_march_dollase_table(self, list_state=None, initial_parameters=None): _checkbox = QCheckBox() _checkbox.setChecked(_state_col) _checkbox.stateChanged.connect( - lambda state=0, row=_row, column=_col: self.parent.march_dollase_table_state_changed( + lambda state=0, + row=_row, + column=_col: self.parent.march_dollase_table_state_changed( state=state, row=row, column=column ) ) @@ -161,7 +193,9 @@ def fill_march_dollase_table(self, list_state=None, initial_parameters=None): if (_col == 1) or (_col == 2): _input = QLineEdit() _input.returnPressed.connect( - lambda column=_col: self.parent.march_dollase_table_init_value_changed(column=column) + lambda column=_col: self.parent.march_dollase_table_init_value_changed( + column=column + ) ) _input.setText(str(initial_parameters[parameter_key])) verti_layout.addWidget(_input) @@ -170,7 +204,9 @@ def fill_march_dollase_table(self, list_state=None, initial_parameters=None): elif _col == 0: _label = QLabel() try: - str_format = f"{float(initial_parameters[parameter_key]):0.6f}" + str_format = ( + f"{float(initial_parameters[parameter_key]):0.6f}" + ) except ValueError: str_format = initial_parameters[parameter_key] diff --git a/notebooks/__code/bragg_edge/bragg_edge_raw_sample_and_powder.py b/notebooks/__code/bragg_edge/bragg_edge_raw_sample_and_powder.py index 876ca6eaa..f3f70db9c 100755 --- a/notebooks/__code/bragg_edge/bragg_edge_raw_sample_and_powder.py +++ b/notebooks/__code/bragg_edge/bragg_edge_raw_sample_and_powder.py @@ -63,7 +63,9 @@ def load_data(self, folder_selected): else: # ask for spectra file - logging.info("-> spectra file NOT FOUND! Asking user to select time spectra file") + logging.info( + "-> spectra file NOT FOUND! Asking user to select time spectra file" + ) self.select_time_spectra_file() def select_time_spectra_file(self): @@ -78,7 +80,9 @@ def select_time_spectra_file(self): self.time_spectra_ui.show() self.cancel_button = widgets.Button( - description="or Do Not Select any Time Spectra", button_style="info", layout=widgets.Layout(width="100%") + description="or Do Not Select any Time Spectra", + button_style="info", + layout=widgets.Layout(width="100%"), ) display(self.cancel_button) self.cancel_button.on_click(self.cancel_time_spectra_selection) @@ -92,7 +96,11 @@ def cancel_time_spectra_selection(self, value): logging.info("User cancel loading time spectra!") self.time_spectra_ui.remove() self.cancel_button.close() - display(HTML('NO Spectra File loaded! ')) + display( + HTML( + 'NO Spectra File loaded! ' + ) + ) def load_files(self, data_type="sample", folder=None): self.starting_dir = os.path.dirname(folder) @@ -145,7 +153,9 @@ def normalization_settings_widgets(self): ## button self.select_ob_widget = widgets.Button( - description="Select OB ...", button_style="success", layout=widgets.Layout(width="100%") + description="Select OB ...", + button_style="success", + layout=widgets.Layout(width="100%"), ) self.select_ob_widget.on_click(self.select_ob_folder) @@ -153,15 +163,23 @@ def normalization_settings_widgets(self): spacer = widgets.HTML(value="
") ## nbr of images to use - nbr_images_to_use_label = widgets.Label("Nbr of images to use in preview", layout=widgets.Layout(width="20%")) + nbr_images_to_use_label = widgets.Label( + "Nbr of images to use in preview", layout=widgets.Layout(width="20%") + ) nbr_of_images_to_use_in_preview = self.get_nbr_of_images_to_use_in_preview() self.nbr_images_slider_with_ob = widgets.IntSlider( - min=2, max=len(self.list_files), value=nbr_of_images_to_use_in_preview, layout=widgets.Layout(width="80%") + min=2, + max=len(self.list_files), + value=nbr_of_images_to_use_in_preview, + layout=widgets.Layout(width="80%"), ) hbox_1 = widgets.HBox([nbr_images_to_use_label, self.nbr_images_slider_with_ob]) self.select_roi_widget_with_ob = widgets.Button( - description="OPTIONAL: Select Region of interest away from " "sample " "to " "improve normalization", + description="OPTIONAL: Select Region of interest away from " + "sample " + "to " + "improve normalization", layout=widgets.Layout(width="100%"), ) self.select_roi_widget_with_ob.on_click(self.select_roi_with_ob) @@ -179,18 +197,25 @@ def normalization_settings_widgets(self): ## nbr of images to use self.nbr_images_slider_without_ob = widgets.IntSlider( - min=2, max=len(self.list_files), value=nbr_of_images_to_use_in_preview, layout=widgets.Layout(width="80%") + min=2, + max=len(self.list_files), + value=nbr_of_images_to_use_in_preview, + layout=widgets.Layout(width="80%"), + ) + hbox_without_ob = widgets.HBox( + [nbr_images_to_use_label, self.nbr_images_slider_without_ob] ) - hbox_without_ob = widgets.HBox([nbr_images_to_use_label, self.nbr_images_slider_without_ob]) select_roi_widget_without_ob = widgets.Button( - description="MANDATORY: Select region of interest " "away from " "sample", + description="MANDATORY: Select region of interest away from sample", button_style="success", layout=widgets.Layout(width="100%"), ) select_roi_widget_without_ob.on_click(self.select_roi_without_ob) - vbox_without_ob = widgets.VBox([hbox_without_ob, spacer, select_roi_widget_without_ob]) + vbox_without_ob = widgets.VBox( + [hbox_without_ob, spacer, select_roi_widget_without_ob] + ) self.accordion = widgets.Accordion(children=[vbox_with_ob, vbox_without_ob]) self.accordion.set_title(0, "With OB") @@ -243,17 +268,37 @@ def check_data_array_sizes(self): len_sample = len(self.o_norm.data["sample"]["file_name"]) if len_ob == len_sample: - display(HTML(' Sample and OB have the same size!')) + display( + HTML( + ' Sample and OB have the same size!' + ) + ) return if len_ob < len_sample: - self.o_norm.data["sample"]["data"] = self.o_norm.data["sample"]["data"][0:len_ob] - self.o_norm.data["sample"]["file_name"] = self.o_norm.data["sample"]["file_name"][0:len_ob] - display(HTML(' Truncated Sample array to match OB!')) + self.o_norm.data["sample"]["data"] = self.o_norm.data["sample"]["data"][ + 0:len_ob + ] + self.o_norm.data["sample"]["file_name"] = self.o_norm.data["sample"][ + "file_name" + ][0:len_ob] + display( + HTML( + ' Truncated Sample array to match OB!' + ) + ) else: - self.o_norm.data["ob"]["data"] = self.o_norm.data["ob"]["data"][0:len_sample] - self.o_norm.data["ob"]["file_name"] = self.o_norm.data["ob"]["file_name"][0:len_sample] - display(HTML(' Truncated OB array to match Sample!')) + self.o_norm.data["ob"]["data"] = self.o_norm.data["ob"]["data"][ + 0:len_sample + ] + self.o_norm.data["ob"]["file_name"] = self.o_norm.data["ob"]["file_name"][ + 0:len_sample + ] + display( + HTML( + ' Truncated OB array to match Sample!' + ) + ) def load_time_spectra(self): _tof_handler = TOF(filename=self.spectra_file) @@ -275,12 +320,16 @@ def how_many_data_to_use_to_select_sample_roi(self): init_value = 1 box1 = widgets.HBox( [ - widgets.Label("Nbr of images to use:", layout=widgets.Layout(width="15")), + widgets.Label( + "Nbr of images to use:", layout=widgets.Layout(width="15") + ), widgets.IntSlider(value=init_value, max=nbr_images, min=1), ] ) # layout=widgets.Layout(width='50%'))]) - box2 = widgets.Label("(The more you select, the longer it will take to display the preview!)") + box2 = widgets.Label( + "(The more you select, the longer it will take to display the preview!)" + ) vbox = widgets.VBox([box1, box2]) display(vbox) self.number_of_data_to_use_ui = box1.children[1] @@ -289,7 +338,9 @@ def get_image_to_use_for_display(self, nbr_data_to_use=2): _data = self.o_norm.data["sample"]["data"] nbr_images = len(_data) - list_of_indexes_to_keep = random.sample(list(range(nbr_images)), nbr_data_to_use) + list_of_indexes_to_keep = random.sample( + list(range(nbr_images)), nbr_data_to_use + ) final_array = [] for _index in list_of_indexes_to_keep: @@ -321,7 +372,11 @@ def normalization(self, o_background=None): logging.info(f"-> Normalization with {len(list_o_roi)} ROIs") self.o_norm.normalization(roi=list_o_roi, notebook=True, force=True) - display(HTML(' Normalization DONE! ')) + display( + HTML( + ' Normalization DONE! ' + ) + ) logging.info("-> Done!") # def normalization_without_ob(self, list_rois): @@ -381,16 +436,22 @@ def normalization(self, o_background=None): def export_normalized_data(self): self.o_folder = FileFolderBrowser( - working_dir=self.working_dir, next_function=self.export_normalized_data_step2, ipts_folder=self.ipts_folder + working_dir=self.working_dir, + next_function=self.export_normalized_data_step2, + ipts_folder=self.ipts_folder, + ) + self.o_folder.select_output_folder_with_new( + instruction="Select where to create the normalized data ..." ) - self.o_folder.select_output_folder_with_new(instruction="Select where to create the normalized data ...") def export_normalized_data_step2(self, output_folder): logging.info("export normalized data") logging.info(f"-> output_folder: {output_folder}") output_folder = os.path.abspath(output_folder) self.o_folder.list_output_folders_ui.shortcut_buttons.close() - normalized_export_folder = str(Path(output_folder) / (self.data_folder_name + "_normalized")) + normalized_export_folder = str( + Path(output_folder) / (self.data_folder_name + "_normalized") + ) file_handler.make_or_reset_folder(normalized_export_folder) self.o_norm.export(folder=normalized_export_folder) @@ -403,8 +464,14 @@ def export_normalized_data_step2(self, output_folder): ) if self.spectra_file: logging.info("-> time spectra copied to output folder!") - file_handler.copy_files_to_folder(list_files=[self.spectra_file], output_folder=normalized_export_folder) - display(HTML(' Copied time spectra file to same folder ')) + file_handler.copy_files_to_folder( + list_files=[self.spectra_file], output_folder=normalized_export_folder + ) + display( + HTML( + ' Copied time spectra file to same folder ' + ) + ) else: logging.info("->No time spectra copied!") @@ -415,8 +482,12 @@ def calculate_counts_vs_file_index_of_regions_selected(self, list_roi=None): nbr_data = len(normalized_data) box_ui = widgets.HBox( [ - widgets.Label("Calculate Counts vs lambda", layout=widgets.Layout(width="20%")), - widgets.IntProgress(min=0, max=nbr_data, value=0, layout=widgets.Layout(width="50%")), + widgets.Label( + "Calculate Counts vs lambda", layout=widgets.Layout(width="20%") + ), + widgets.IntProgress( + min=0, max=nbr_data, value=0, layout=widgets.Layout(width="50%") + ), ] ) progress_bar = box_ui.children[1] @@ -445,7 +516,9 @@ def calculate_counts_vs_file_index_of_regions_selected(self, list_roi=None): box_ui.close() def plot(self): - trace = go.Scatter(x=self.lambda_array, y=self.counts_vs_file_index, mode="markers") + trace = go.Scatter( + x=self.lambda_array, y=self.counts_vs_file_index, mode="markers" + ) layout = go.Layout( height=500, @@ -511,8 +584,12 @@ def plot(self): iplot(figure) def select_output_data_folder(self): - o_folder = FileFolderBrowser(working_dir=self.working_dir, next_function=self.export_data) - o_folder.select_output_folder(instruction="Select where to create the ascii file...") + o_folder = FileFolderBrowser( + working_dir=self.working_dir, next_function=self.export_data + ) + o_folder.select_output_folder( + instruction="Select where to create the ascii file..." + ) def make_output_file_name(self, output_folder="", input_folder=""): file_name = os.path.basename(input_folder) + "_counts_vs_lambda_tof.txt" @@ -520,7 +597,9 @@ def make_output_file_name(self, output_folder="", input_folder=""): def export_data(self, output_folder): input_folder = os.path.dirname(self.o_norm.data["sample"]["file_name"][0]) - output_file_name = self.make_output_file_name(output_folder=output_folder, input_folder=input_folder) + output_file_name = self.make_output_file_name( + output_folder=output_folder, input_folder=input_folder + ) lambda_array = self.lambda_array counts_vs_file_index = self.counts_vs_file_index @@ -538,7 +617,9 @@ def export_data(self, output_folder): _y0 = roi["y0"] _x1 = roi["x1"] _y1 = roi["y1"] - metadata.append(f"# Sample ROI {index}: x0={_x0}, y0={_y0}, x1={_x1}, y1={_y1}") + metadata.append( + f"# Sample ROI {index}: x0={_x0}, y0={_y0}, x1={_x1}, y1={_y1}" + ) background_rois = self.background_rois if len(background_rois) == 0: @@ -550,16 +631,22 @@ def export_data(self, output_folder): _y0 = roi["y0"] _x1 = roi["x1"] _y1 = roi["y1"] - metadata.append(f"# Background ROI {index}: x0={_x0}, y0={_y0}, x1={_x1}, y1={_y1}") + metadata.append( + f"# Background ROI {index}: x0={_x0}, y0={_y0}, x1={_x1}, y1={_y1}" + ) metadata.append("#") metadata.append("# tof (micros), lambda (Angstroms), Average transmission") data = [] - for _t, _l, _c in zip(tof_array, lambda_array, counts_vs_file_index, strict=False): + for _t, _l, _c in zip( + tof_array, lambda_array, counts_vs_file_index, strict=False + ): data.append(f"{_t}, {_l}, {_c}") - file_handler.make_ascii_file(metadata=metadata, data=data, output_file_name=output_file_name, dim="1d") + file_handler.make_ascii_file( + metadata=metadata, data=data, output_file_name=output_file_name, dim="1d" + ) if os.path.exists(output_file_name): display( @@ -580,7 +667,9 @@ def export_data(self, output_folder): ) def select_output_table_folder(self): - o_folder = FileFolderBrowser(working_dir=self.working_dir, next_function=self.export_table) + o_folder = FileFolderBrowser( + working_dir=self.working_dir, next_function=self.export_table + ) o_folder.select_output_folder() def export_table(self, output_folder): @@ -607,9 +696,17 @@ def export_table(self, output_folder): output_file_name = os.path.join(output_folder, f"bragg_edges_of_{material}.txt") - file_handler.make_ascii_file(metadata=metadata, data=data, dim="1d", output_file_name=output_file_name) + file_handler.make_ascii_file( + metadata=metadata, data=data, dim="1d", output_file_name=output_file_name + ) - display(HTML('File created : ' + output_file_name + "")) + display( + HTML( + 'File created : ' + + output_file_name + + "" + ) + ) def select_folder(self, message="", next_function=None): folder_widget = ipywe.fileselector.FileSelectorPanel( diff --git a/notebooks/__code/bragg_edge/bragg_edge_sample_and_powder.py b/notebooks/__code/bragg_edge/bragg_edge_sample_and_powder.py index fafefeac5..0a4d4cc6f 100755 --- a/notebooks/__code/bragg_edge/bragg_edge_sample_and_powder.py +++ b/notebooks/__code/bragg_edge/bragg_edge_sample_and_powder.py @@ -63,7 +63,9 @@ def load_data(self, folder_selected): else: # ask for spectra file - logging.info("-> spectra file NOT FOUND! Asking user to select time spectra file") + logging.info( + "-> spectra file NOT FOUND! Asking user to select time spectra file" + ) self.select_time_spectra_file() def select_time_spectra_file(self): @@ -78,7 +80,9 @@ def select_time_spectra_file(self): self.time_spectra_ui.show() self.cancel_button = widgets.Button( - description="or Do Not Select any Time Spectra", button_style="info", layout=widgets.Layout(width="100%") + description="or Do Not Select any Time Spectra", + button_style="info", + layout=widgets.Layout(width="100%"), ) display(self.cancel_button) self.cancel_button.on_click(self.cancel_time_spectra_selection) @@ -92,7 +96,11 @@ def cancel_time_spectra_selection(self, value): logging.info("User cancel loading time spectra!") self.time_spectra_ui.remove() self.cancel_button.close() - display(HTML('NO Spectra File loaded! ')) + display( + HTML( + 'NO Spectra File loaded! ' + ) + ) def load_files(self, data_type="sample", folder=None): self.starting_dir = os.path.dirname(folder) @@ -145,7 +153,9 @@ def normalization_settings_widgets(self): ## button self.select_ob_widget = widgets.Button( - description="Select OB ...", button_style="success", layout=widgets.Layout(width="100%") + description="Select OB ...", + button_style="success", + layout=widgets.Layout(width="100%"), ) self.select_ob_widget.on_click(self.select_ob_folder) @@ -153,15 +163,23 @@ def normalization_settings_widgets(self): spacer = widgets.HTML(value="
") ## nbr of images to use - nbr_images_to_use_label = widgets.Label("Nbr of images to use in preview", layout=widgets.Layout(width="20%")) + nbr_images_to_use_label = widgets.Label( + "Nbr of images to use in preview", layout=widgets.Layout(width="20%") + ) nbr_of_images_to_use_in_preview = self.get_nbr_of_images_to_use_in_preview() self.nbr_images_slider_with_ob = widgets.IntSlider( - min=2, max=len(self.list_files), value=nbr_of_images_to_use_in_preview, layout=widgets.Layout(width="80%") + min=2, + max=len(self.list_files), + value=nbr_of_images_to_use_in_preview, + layout=widgets.Layout(width="80%"), ) hbox_1 = widgets.HBox([nbr_images_to_use_label, self.nbr_images_slider_with_ob]) self.select_roi_widget_with_ob = widgets.Button( - description="OPTIONAL: Select Region of interest away from " "sample " "to " "improve normalization", + description="OPTIONAL: Select Region of interest away from " + "sample " + "to " + "improve normalization", layout=widgets.Layout(width="100%"), ) self.select_roi_widget_with_ob.on_click(self.select_roi_with_ob) @@ -179,18 +197,25 @@ def normalization_settings_widgets(self): ## nbr of images to use self.nbr_images_slider_without_ob = widgets.IntSlider( - min=2, max=len(self.list_files), value=nbr_of_images_to_use_in_preview, layout=widgets.Layout(width="80%") + min=2, + max=len(self.list_files), + value=nbr_of_images_to_use_in_preview, + layout=widgets.Layout(width="80%"), + ) + hbox_without_ob = widgets.HBox( + [nbr_images_to_use_label, self.nbr_images_slider_without_ob] ) - hbox_without_ob = widgets.HBox([nbr_images_to_use_label, self.nbr_images_slider_without_ob]) select_roi_widget_without_ob = widgets.Button( - description="MANDATORY: Select region of interest " "away from " "sample", + description="MANDATORY: Select region of interest away from sample", button_style="success", layout=widgets.Layout(width="100%"), ) select_roi_widget_without_ob.on_click(self.select_roi_without_ob) - vbox_without_ob = widgets.VBox([hbox_without_ob, spacer, select_roi_widget_without_ob]) + vbox_without_ob = widgets.VBox( + [hbox_without_ob, spacer, select_roi_widget_without_ob] + ) self.accordion = widgets.Accordion(children=[vbox_with_ob, vbox_without_ob]) self.accordion.set_title(0, "With OB") @@ -243,17 +268,37 @@ def check_data_array_sizes(self): len_sample = len(self.o_norm.data["sample"]["file_name"]) if len_ob == len_sample: - display(HTML(' Sample and OB have the same size!')) + display( + HTML( + ' Sample and OB have the same size!' + ) + ) return if len_ob < len_sample: - self.o_norm.data["sample"]["data"] = self.o_norm.data["sample"]["data"][0:len_ob] - self.o_norm.data["sample"]["file_name"] = self.o_norm.data["sample"]["file_name"][0:len_ob] - display(HTML(' Truncated Sample array to match OB!')) + self.o_norm.data["sample"]["data"] = self.o_norm.data["sample"]["data"][ + 0:len_ob + ] + self.o_norm.data["sample"]["file_name"] = self.o_norm.data["sample"][ + "file_name" + ][0:len_ob] + display( + HTML( + ' Truncated Sample array to match OB!' + ) + ) else: - self.o_norm.data["ob"]["data"] = self.o_norm.data["ob"]["data"][0:len_sample] - self.o_norm.data["ob"]["file_name"] = self.o_norm.data["ob"]["file_name"][0:len_sample] - display(HTML(' Truncated OB array to match Sample!')) + self.o_norm.data["ob"]["data"] = self.o_norm.data["ob"]["data"][ + 0:len_sample + ] + self.o_norm.data["ob"]["file_name"] = self.o_norm.data["ob"]["file_name"][ + 0:len_sample + ] + display( + HTML( + ' Truncated OB array to match Sample!' + ) + ) def load_time_spectra(self): _tof_handler = TOF(filename=self.spectra_file) @@ -275,12 +320,16 @@ def how_many_data_to_use_to_select_sample_roi(self): init_value = 1 box1 = widgets.HBox( [ - widgets.Label("Nbr of images to use:", layout=widgets.Layout(width="15")), + widgets.Label( + "Nbr of images to use:", layout=widgets.Layout(width="15") + ), widgets.IntSlider(value=init_value, max=nbr_images, min=1), ] ) # layout=widgets.Layout(width='50%'))]) - box2 = widgets.Label("(The more you select, the longer it will take to display the preview!)") + box2 = widgets.Label( + "(The more you select, the longer it will take to display the preview!)" + ) vbox = widgets.VBox([box1, box2]) display(vbox) self.number_of_data_to_use_ui = box1.children[1] @@ -289,7 +338,9 @@ def get_image_to_use_for_display(self, nbr_data_to_use=2): _data = self.o_norm.data["sample"]["data"] nbr_images = len(_data) - list_of_indexes_to_keep = random.sample(list(range(nbr_images)), nbr_data_to_use) + list_of_indexes_to_keep = random.sample( + list(range(nbr_images)), nbr_data_to_use + ) final_array = [] for _index in list_of_indexes_to_keep: @@ -317,8 +368,14 @@ def normalization(self): def normalization_without_ob(self, list_rois): logging.info("Running normalization without OB") if list_rois is None: - logging.info("-> no ROIs found! At least one ROI must be provided. Normalization Aborted!") - display(HTML(' You need to provide a ROI!')) + logging.info( + "-> no ROIs found! At least one ROI must be provided. Normalization Aborted!" + ) + display( + HTML( + ' You need to provide a ROI!' + ) + ) return else: @@ -333,8 +390,14 @@ def normalization_without_ob(self, list_rois): list_o_roi.append(ROI(x0=_x0, y0=_y0, x1=_x1, y1=_y1)) logging.info(f"-> Normalization with {len(list_o_roi)} ROIs") - self.o_norm.normalization(roi=list_o_roi, use_only_sample=True, notebook=True, force=True) - display(HTML(' Normalization DONE! ')) + self.o_norm.normalization( + roi=list_o_roi, use_only_sample=True, notebook=True, force=True + ) + display( + HTML( + ' Normalization DONE! ' + ) + ) logging.info("-> Done!") def normalization_with_ob(self, list_rois): @@ -355,21 +418,31 @@ def normalization_with_ob(self, list_rois): logging.info(f"-> Normalization with {len(list_o_roi)} ROIs") self.o_norm.normalization(roi=list_o_roi, notebook=True, force=True) - display(HTML(' Normalization DONE! ')) + display( + HTML( + ' Normalization DONE! ' + ) + ) logging.info("-> Done!") def export_normalized_data(self): self.o_folder = FileFolderBrowser( - working_dir=self.working_dir, next_function=self.export_normalized_data_step2, ipts_folder=self.ipts_folder + working_dir=self.working_dir, + next_function=self.export_normalized_data_step2, + ipts_folder=self.ipts_folder, + ) + self.o_folder.select_output_folder_with_new( + instruction="Select where to create the normalized data ..." ) - self.o_folder.select_output_folder_with_new(instruction="Select where to create the normalized data ...") def export_normalized_data_step2(self, output_folder): logging.info("export normalized data") logging.info(f"-> output_folder: {output_folder}") output_folder = os.path.abspath(output_folder) self.o_folder.list_output_folders_ui.shortcut_buttons.close() - normalized_export_folder = str(Path(output_folder) / (self.data_folder_name + "_normalized")) + normalized_export_folder = str( + Path(output_folder) / (self.data_folder_name + "_normalized") + ) file_handler.make_or_reset_folder(normalized_export_folder) self.o_norm.export(folder=normalized_export_folder) @@ -382,8 +455,14 @@ def export_normalized_data_step2(self, output_folder): ) if self.spectra_file: logging.info("-> time spectra copied to output folder!") - file_handler.copy_files_to_folder(list_files=[self.spectra_file], output_folder=normalized_export_folder) - display(HTML(' Copied time spectra file to same folder ')) + file_handler.copy_files_to_folder( + list_files=[self.spectra_file], output_folder=normalized_export_folder + ) + display( + HTML( + ' Copied time spectra file to same folder ' + ) + ) else: logging.info("->No time spectra copied!") @@ -394,8 +473,12 @@ def calculate_counts_vs_file_index_of_regions_selected(self, list_roi=None): nbr_data = len(data) box_ui = widgets.HBox( [ - widgets.Label("Calculate Counts vs lambda", layout=widgets.Layout(width="20%")), - widgets.IntProgress(min=0, max=nbr_data, value=0, layout=widgets.Layout(width="50%")), + widgets.Label( + "Calculate Counts vs lambda", layout=widgets.Layout(width="20%") + ), + widgets.IntProgress( + min=0, max=nbr_data, value=0, layout=widgets.Layout(width="50%") + ), ] ) progress_bar = box_ui.children[1] @@ -424,7 +507,9 @@ def calculate_counts_vs_file_index_of_regions_selected(self, list_roi=None): box_ui.close() def plot(self): - trace = go.Scatter(x=self.lambda_array, y=self.counts_vs_file_index, mode="markers") + trace = go.Scatter( + x=self.lambda_array, y=self.counts_vs_file_index, mode="markers" + ) layout = go.Layout( height=500, @@ -487,8 +572,12 @@ def plot(self): iplot(figure) def select_output_data_folder(self): - o_folder = FileFolderBrowser(working_dir=self.working_dir, next_function=self.export_data) - o_folder.select_output_folder(instruction="Select where to create the ascii file...") + o_folder = FileFolderBrowser( + working_dir=self.working_dir, next_function=self.export_data + ) + o_folder.select_output_folder( + instruction="Select where to create the ascii file..." + ) def make_output_file_name(self, output_folder="", input_folder=""): file_name = os.path.basename(input_folder) + "_counts_vs_lambda_tof.txt" @@ -496,7 +585,9 @@ def make_output_file_name(self, output_folder="", input_folder=""): def export_data(self, output_folder): input_folder = os.path.dirname(self.o_norm.data["sample"]["file_name"][0]) - output_file_name = self.make_output_file_name(output_folder=output_folder, input_folder=input_folder) + output_file_name = self.make_output_file_name( + output_folder=output_folder, input_folder=input_folder + ) lambda_array = self.lambda_array counts_vs_file_index = self.counts_vs_file_index @@ -514,15 +605,21 @@ def export_data(self, output_folder): _y0 = roi["y0"] _x1 = roi["x1"] _y1 = roi["y1"] - metadata.append(f"# ROI {index}: x0={_x0}, y0={_y0}, x1={_x1}, y1={_y1}") + metadata.append( + f"# ROI {index}: x0={_x0}, y0={_y0}, x1={_x1}, y1={_y1}" + ) metadata.append("#") metadata.append("# tof (micros), lambda (Angstroms), Average transmission") data = [] - for _t, _l, _c in zip(tof_array, lambda_array, counts_vs_file_index, strict=False): + for _t, _l, _c in zip( + tof_array, lambda_array, counts_vs_file_index, strict=False + ): data.append(f"{_t}, {_l}, {_c}") - file_handler.make_ascii_file(metadata=metadata, data=data, output_file_name=output_file_name, dim="1d") + file_handler.make_ascii_file( + metadata=metadata, data=data, output_file_name=output_file_name, dim="1d" + ) if os.path.exists(output_file_name): display( @@ -543,7 +640,9 @@ def export_data(self, output_folder): ) def select_output_table_folder(self): - o_folder = FileFolderBrowser(working_dir=self.working_dir, next_function=self.export_table) + o_folder = FileFolderBrowser( + working_dir=self.working_dir, next_function=self.export_table + ) o_folder.select_output_folder() def export_table(self, output_folder): @@ -570,9 +669,17 @@ def export_table(self, output_folder): output_file_name = os.path.join(output_folder, f"bragg_edges_of_{material}.txt") - file_handler.make_ascii_file(metadata=metadata, data=data, dim="1d", output_file_name=output_file_name) + file_handler.make_ascii_file( + metadata=metadata, data=data, dim="1d", output_file_name=output_file_name + ) - display(HTML('File created : ' + output_file_name + "")) + display( + HTML( + 'File created : ' + + output_file_name + + "" + ) + ) def select_folder(self, message="", next_function=None): folder_widget = ipywe.fileselector.FileSelectorPanel( diff --git a/notebooks/__code/bragg_edge/bragg_edge_selection_tab.py b/notebooks/__code/bragg_edge/bragg_edge_selection_tab.py index f4758ca9f..756871e26 100755 --- a/notebooks/__code/bragg_edge/bragg_edge_selection_tab.py +++ b/notebooks/__code/bragg_edge/bragg_edge_selection_tab.py @@ -17,7 +17,9 @@ def update_all_size_widgets_infos(self): return roi_id = self.parent.roi_id - region = roi_id.getArraySlice(self.parent.final_image, self.parent.ui.image_view.imageItem) + region = roi_id.getArraySlice( + self.parent.final_image, self.parent.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop y0 = region[0][1].start @@ -54,7 +56,9 @@ def get_coordinates_of_new_inside_selection_box(self): height_requested = int(str(self.parent.ui.profile_of_bin_size_height.text())) # retrieve x0, y0, width and height of full selection - region = self.parent.roi_id.getArraySlice(self.parent.final_image, self.parent.ui.image_view.imageItem) + region = self.parent.roi_id.getArraySlice( + self.parent.final_image, self.parent.ui.image_view.imageItem + ) x0 = region[0][0].start y0 = region[0][1].start # [x0, y0] = self.parentselection_x0y0 @@ -121,12 +125,21 @@ def update_selection_profile_plot(self): self.parent.ui.profile.setLabel("left", "Mean transmission") # vertical line showing peak to fit - bragg_edge_range = [x_axis[self.parent.bragg_edge_range[0]], x_axis[self.parent.bragg_edge_range[1]]] + bragg_edge_range = [ + x_axis[self.parent.bragg_edge_range[0]], + x_axis[self.parent.bragg_edge_range[1]], + ] self.parent.bragg_edge_range_ui = pg.LinearRegionItem( - values=bragg_edge_range, orientation=None, brush=None, movable=True, bounds=None + values=bragg_edge_range, + orientation=None, + brush=None, + movable=True, + bounds=None, + ) + self.parent.bragg_edge_range_ui.sigRegionChanged.connect( + self.parent.bragg_edge_range_changed ) - self.parent.bragg_edge_range_ui.sigRegionChanged.connect(self.parent.bragg_edge_range_changed) self.parent.bragg_edge_range_ui.setZValue(-10) self.parent.ui.profile.addItem(self.parent.bragg_edge_range_ui) @@ -135,7 +148,9 @@ def update_selection(self, new_value=None, mode="square"): return try: - region = self.parent.roi_id.getArraySlice(self.parent.final_image, self.parent.ui.image_view.imageItem) + region = self.parent.roi_id.getArraySlice( + self.parent.final_image, self.parent.ui.image_view.imageItem + ) except TypeError: return @@ -149,7 +164,9 @@ def update_selection(self, new_value=None, mode="square"): _pen = QtGui.QPen() _pen.setColor(self.parent.roi_settings["color"]) _pen.setWidthF(self.parent.roi_settings["width"]) - self.parent.roi_id = pg.ROI([x0, y0], [new_value, new_value], pen=_pen, scaleSnap=True) + self.parent.roi_id = pg.ROI( + [x0, y0], [new_value, new_value], pen=_pen, scaleSnap=True + ) self.parent.ui.image_view.addItem(self.parent.roi_id) self.parent.roi_id.sigRegionChanged.connect(self.parent.roi_moved) @@ -177,7 +194,11 @@ def update_selection_plot(self): self.parent.ui.profile.plot( x_axis, y_axis, - pen=(self.parent.shrinking_roi_rgb[0], self.parent.shrinking_roi_rgb[1], self.parent.shrinking_roi_rgb[2]), + pen=( + self.parent.shrinking_roi_rgb[0], + self.parent.shrinking_roi_rgb[1], + self.parent.shrinking_roi_rgb[2], + ), ) self.parent.ui.profile.setLabel("bottom", x_axis_label) self.parent.ui.profile.setLabel("left", "Mean transmission") @@ -187,7 +208,11 @@ def update_selection_plot(self): self.parent.ui.profile.plot( x_axis, y_axis, - pen=(self.parent.selection_roi_rgb[0], self.parent.selection_roi_rgb[1], self.parent.selection_roi_rgb[2]), + pen=( + self.parent.selection_roi_rgb[0], + self.parent.selection_roi_rgb[1], + self.parent.selection_roi_rgb[2], + ), ) def profile_of_bin_size_slider_changed(self, new_value): @@ -236,7 +261,9 @@ def update_roi_defined_by_profile_of_bin_size_slider(self): _pen.setColor(self.parent.shrinking_roi_settings["color"]) _pen.setWidthF(self.parent.shrinking_roi_settings["width"]) - self.parent.shrinking_roi_id = pg.ROI([x0, y0], [width, height], pen=_pen, scaleSnap=True, movable=False) + self.parent.shrinking_roi_id = pg.ROI( + [x0, y0], [width, height], pen=_pen, scaleSnap=True, movable=False + ) self.parent.ui.image_view.addItem(self.parent.shrinking_roi_id) def update_profile_of_bin_slider_widget(self): @@ -250,7 +277,9 @@ def update_profile_of_bin_slider_widget(self): "height": fitting_input_dictionary["rois"][_key]["height"], } self.parent.dict_rois_imported = dict_rois_imported - self.parent.ui.profile_of_bin_size_slider.setRange(0, len(dict_rois_imported) - 1) + self.parent.ui.profile_of_bin_size_slider.setRange( + 0, len(dict_rois_imported) - 1 + ) # self.parent.ui.profile_of_bin_size_slider.setMinimum(0) # self.parent.ui.profile_of_bin_size_slider.setMaximum(len(dict_rois_imported)-1) self.parent.ui.profile_of_bin_size_slider.setSingleStep(1) diff --git a/notebooks/__code/bragg_edge/export_handler.py b/notebooks/__code/bragg_edge/export_handler.py index 19c0dec5f..cd2481384 100755 --- a/notebooks/__code/bragg_edge/export_handler.py +++ b/notebooks/__code/bragg_edge/export_handler.py @@ -18,7 +18,10 @@ def configuration(self): base_folder = Path(self.parent.working_dir) directory = str(base_folder.parent) _export_folder = QFileDialog.getExistingDirectory( - self.parent, directory=directory, caption="Select Output Folder", options=QFileDialog.ShowDirsOnly + self.parent, + directory=directory, + caption="Select Output Folder", + options=QFileDialog.ShowDirsOnly, ) if _export_folder: @@ -29,17 +32,34 @@ def configuration(self): [x0, y0, x1, y1, width, height] = o_get.selection_roi_dimension() name_of_ascii_file = ExportHandler.makeup_name_of_profile_ascii_file( - base_name=str(base_folder.name), export_folder=_export_folder, x0=x0, y0=y0, width=width, height=height + base_name=str(base_folder.name), + export_folder=_export_folder, + x0=x0, + y0=y0, + width=width, + height=height, ) - make_ascii_file(metadata=metadata, data=data, output_file_name=name_of_ascii_file, dim="1d") + make_ascii_file( + metadata=metadata, + data=data, + output_file_name=name_of_ascii_file, + dim="1d", + ) - self.parent.ui.statusbar.showMessage(f"{name_of_ascii_file} has been created!", 10000) # 10s + self.parent.ui.statusbar.showMessage( + f"{name_of_ascii_file} has been created!", 10000 + ) # 10s self.parent.ui.statusbar.setStyleSheet("color: green") @staticmethod def makeup_name_of_profile_ascii_file( - base_name="default", export_folder="./", x0=None, y0=None, width=None, height=None + base_name="default", + export_folder="./", + x0=None, + y0=None, + width=None, + height=None, ): """this will return the full path name of the ascii file to create that will contain all the profiles starting with the selection box and all the way to the minimal size""" @@ -76,20 +96,29 @@ def get_data_metadata_from_selection_tab(self): metadata.append("#") metadata.append("#File Index, TOF(micros), lambda(Angstroms), ROIs (see above)") - data = ExportHandler.format_data(col1=index_axis, col2=tof_axis, col3=lambda_axis, dict_regions=dict_regions) + data = ExportHandler.format_data( + col1=index_axis, col2=tof_axis, col3=lambda_axis, dict_regions=dict_regions + ) return data, metadata def add_fitting_infos_to_metadata(self, metadata): o_tab = GuiUtility(parent=self.parent) - fitting_algorithm_used = o_tab.get_tab_selected(tab_ui=self.parent.ui.tab_algorithm) + fitting_algorithm_used = o_tab.get_tab_selected( + tab_ui=self.parent.ui.tab_algorithm + ) # fitting_rois = self.fitting_rois # fitting_flag = True if self.parent.fitting_peak_ui else False metadata.append(f"#fitting algorithm selected: {fitting_algorithm_used}") - metadata.append("#kropff fitting procedure started: {}".format(self.parent.fitting_procedure_started["kropff"])) + metadata.append( + "#kropff fitting procedure started: {}".format( + self.parent.fitting_procedure_started["kropff"] + ) + ) metadata.append( "#Bragg peak selection range: [{}, {}]".format( - self.parent.kropff_fitting_range["bragg_peak"][0], self.parent.kropff_fitting_range["bragg_peak"][1] + self.parent.kropff_fitting_range["bragg_peak"][0], + self.parent.kropff_fitting_range["bragg_peak"][1], ) ) # kropff @@ -100,16 +129,22 @@ def add_fitting_infos_to_metadata(self, metadata): # March-dollase [left_peak, right_peak] = self.parent.march_dollase_fitting_range_selected - metadata.append(f"#march-dollase bragg peak selection range: [{left_peak}, {right_peak}]") + metadata.append( + f"#march-dollase bragg peak selection range: [{left_peak}, {right_peak}]" + ) metadata.append( "#march-dollase fitting procedure started: {}".format( self.parent.fitting_procedure_started["march-dollase"] ) ) - for _row_index, _row_entry in enumerate(self.parent.march_dollase_fitting_history_table): + for _row_index, _row_entry in enumerate( + self.parent.march_dollase_fitting_history_table + ): str_row_entry = [str(_value) for _value in _row_entry] joined_str_row_entry = ", ".join(str_row_entry) - metadata.append(f"#march-dollase history table row {_row_index}: {joined_str_row_entry}") + metadata.append( + f"#march-dollase history table row {_row_index}: {joined_str_row_entry}" + ) sigma = self.parent.march_dollase_fitting_initial_parameters["sigma"] alpha = self.parent.march_dollase_fitting_initial_parameters["alpha"] @@ -182,7 +217,9 @@ def make_metadata( march_dollase_fitting_values=None, ): metadata = [f"#base folder: {base_folder}"] - metadata.append(f"#fitting peak range in file index: [{fitting_peak_range[0]}, {fitting_peak_range[1]}]") + metadata.append( + f"#fitting peak range in file index: [{fitting_peak_range[0]}, {fitting_peak_range[1]}]" + ) metadata.append(f"#distance detector-sample: {distance_detector_sample}") metadata.append(f"#detector offset: {detector_offset}") for _row, _key in enumerate(dict_regions.keys()): @@ -253,5 +290,7 @@ def format_data(col1=None, col2=None, col3=None, dict_regions=None): _col1 = col1[_row_index] _col2 = col2[_row_index] _col3 = col3[_row_index] - data.append(f"{_col1}, {_col2}, {_col3}, " + ", ".join(list_profile_for_this_row)) + data.append( + f"{_col1}, {_col2}, {_col3}, " + ", ".join(list_profile_for_this_row) + ) return data diff --git a/notebooks/__code/bragg_edge/get.py b/notebooks/__code/bragg_edge/get.py index fc39f48c5..12b5b96c9 100755 --- a/notebooks/__code/bragg_edge/get.py +++ b/notebooks/__code/bragg_edge/get.py @@ -80,7 +80,9 @@ def all_russian_doll_region_full_infos(self): # collect initial selection size (x0, y0, width, height) [x0, y0, x1, y1, width, height] = self.selection_roi_dimension() # create profile for all the fitting region inside that first box - o_regions = SelectionRegionUtilities(x0=x0, y0=y0, width=width, height=height) + o_regions = SelectionRegionUtilities( + x0=x0, y0=y0, width=width, height=height + ) dict_regions = o_regions.get_all_russian_doll_regions() self.parent.add_profile_to_dict_of_all_regions(dict_regions=dict_regions) return dict_regions @@ -91,7 +93,9 @@ def selection_roi_dimension(self): x0, y0, x1, y1, width, height = None, None, None, None, None, None if roi_id: - region = roi_id.getArraySlice(self.parent.final_image, self.parent.ui.image_view.imageItem) + region = roi_id.getArraySlice( + self.parent.final_image, self.parent.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop y0 = region[0][1].start @@ -104,7 +108,9 @@ def selection_roi_dimension(self): return [x0, y0, x1, y1, width, height] - def profile_of_roi(self, x0=None, y0=None, x1=None, y1=None, width=None, height=None): + def profile_of_roi( + self, x0=None, y0=None, x1=None, y1=None, width=None, height=None + ): profile_value = [] if width: @@ -120,11 +126,17 @@ def profile_of_roi(self, x0=None, y0=None, x1=None, y1=None, width=None, height= def requested_xaxis(self, xaxis_label="index"): if xaxis_label == "index": - return self.parent.dict_profile_to_fit["xaxis"]["index"], self.parent.xaxis_label["index"] + return self.parent.dict_profile_to_fit["xaxis"][ + "index" + ], self.parent.xaxis_label["index"] elif xaxis_label == "tof": - return self.parent.dict_profile_to_fit["xaxis"]["tof"], self.parent.xaxis_label["tof"] + return self.parent.dict_profile_to_fit["xaxis"][ + "tof" + ], self.parent.xaxis_label["tof"] elif xaxis_label == "lambda": - return self.parent.dict_profile_to_fit["xaxis"]["lambda"], self.parent.xaxis_label["lambda"] + return self.parent.dict_profile_to_fit["xaxis"][ + "lambda" + ], self.parent.xaxis_label["lambda"] def fitting_profile_xaxis(self): if self.parent.ui.fitting_tof_radiobutton.isChecked(): @@ -146,7 +158,10 @@ def part_of_fitting_selected(self): page_index = self.parent.ui.kropff_toolBox.currentIndex() - return {"name_of_page": list_pages[page_index], "table_ui": list_table_ui[page_index]} + return { + "name_of_page": list_pages[page_index], + "table_ui": list_table_ui[page_index], + } def y_axis_data_of_selected_row(self, row_selected): selected_roi = self.parent.fitting_input_dictionary["rois"][row_selected] diff --git a/notebooks/__code/bragg_edge/import_handler.py b/notebooks/__code/bragg_edge/import_handler.py index 92b084f78..a1f46ff4f 100755 --- a/notebooks/__code/bragg_edge/import_handler.py +++ b/notebooks/__code/bragg_edge/import_handler.py @@ -6,7 +6,11 @@ from __code.bragg_edge.bragg_edge_peak_fitting_gui_utility import GuiUtility from __code.bragg_edge.bragg_edge_selection_tab import BraggEdgeSelectionTab -from __code.bragg_edge.fitting_functions import kropff_bragg_peak_tof, kropff_high_lambda, kropff_low_lambda +from __code.bragg_edge.fitting_functions import ( + kropff_bragg_peak_tof, + kropff_high_lambda, + kropff_low_lambda, +) from __code.bragg_edge.kropff import Kropff from __code.bragg_edge.march_dollase import MarchDollase from __code.bragg_edge.peak_fitting_initialization import PeakFittingInitialization @@ -20,23 +24,36 @@ def __init__(self, parent=None): def run(self): working_dir = str(Path(self.parent.working_dir).parent) ascii_file = QFileDialog.getOpenFileName( - self.parent, caption="Select ASCII file", directory=working_dir, filter="ASCII (*.txt)" + self.parent, + caption="Select ASCII file", + directory=working_dir, + filter="ASCII (*.txt)", ) if ascii_file[0]: self.parent.full_reset_of_ui() self.parent.block_table_ui(True) self.parent.is_file_imported = True - result_of_import = read_bragg_edge_fitting_ascii_format(full_file_name=str(ascii_file[0])) - self.save_initial_roi_dimension_from_config_file(result_of_import["metadata"]["columns"]["3"]) + result_of_import = read_bragg_edge_fitting_ascii_format( + full_file_name=str(ascii_file[0]) + ) + self.save_initial_roi_dimension_from_config_file( + result_of_import["metadata"]["columns"]["3"] + ) self.save_march_dollase_parameters(result_of_import["metadata"]) - self.parent.bragg_edge_range = result_of_import["metadata"]["bragg_edge_range"] - self.parent.bragg_peak_selection_range = result_of_import["metadata"]["bragg_peak_selection_range"] + self.parent.bragg_edge_range = result_of_import["metadata"][ + "bragg_edge_range" + ] + self.parent.bragg_peak_selection_range = result_of_import["metadata"][ + "bragg_peak_selection_range" + ] self.update_selection_tab(result_of_import=result_of_import) self.update_interface(result_of_import=result_of_import) - self.parent.ui.statusbar.showMessage(f"{ascii_file[0]} has been imported!", 10000) # 10s + self.parent.ui.statusbar.showMessage( + f"{ascii_file[0]} has been imported!", 10000 + ) # 10s self.parent.ui.statusbar.setStyleSheet("color: green") o_selection = BraggEdgeSelectionTab(parent=self.parent) @@ -47,11 +64,13 @@ def run(self): self.parent.ui.tabWidget.setEnabled(True) self.parent.ui.actionExport.setEnabled(True) - self.parent.fitting_procedure_started["kropff"] = result_of_import.get("metadata").get( - "kropff fitting " "procedure " "started", False - ) - self.parent.fitting_procedure_started["march-dollase"] = result_of_import.get("metadata").get( - "march-dollase fitting procedure started", False + self.parent.fitting_procedure_started["kropff"] = result_of_import.get( + "metadata" + ).get("kropff fitting procedure started", False) + self.parent.fitting_procedure_started["march-dollase"] = ( + result_of_import.get("metadata").get( + "march-dollase fitting procedure started", False + ) ) o_kropff = Kropff(parent=self.parent) @@ -60,11 +79,15 @@ def run(self): o_march = MarchDollase(parent=self.parent) o_march.reset_table() - if result_of_import.get("metadata").get("kropff fitting procedure started", False): + if result_of_import.get("metadata").get( + "kropff fitting procedure started", False + ): # fill tables with minimum contains o_kropff.fill_table_with_fitting_information() - if result_of_import.get("metadata").get("march-dollase fitting procedure started", False): + if result_of_import.get("metadata").get( + "march-dollase fitting procedure started", False + ): # fill tables with minimum contains o_march.fill_tables_with_fitting_information() o_march.fill_history_table_with_fitting_information() @@ -82,14 +105,26 @@ def run(self): o_gui.check_status_of_kropff_fitting_buttons() def update_interface(self, result_of_import=None): - self.create_fitting_input_dictionary_from_imported_ascii_file(result_of_import=result_of_import) - self.parent.tof_array_s = self.parent.fitting_input_dictionary["xaxis"]["tof"][0] * 1e-6 - self.parent.lambda_array = self.parent.fitting_input_dictionary["xaxis"]["lambda"][0] - self.parent.index_array = self.parent.fitting_input_dictionary["xaxis"]["index"][0] + self.create_fitting_input_dictionary_from_imported_ascii_file( + result_of_import=result_of_import + ) + self.parent.tof_array_s = ( + self.parent.fitting_input_dictionary["xaxis"]["tof"][0] * 1e-6 + ) + self.parent.lambda_array = self.parent.fitting_input_dictionary["xaxis"][ + "lambda" + ][0] + self.parent.index_array = self.parent.fitting_input_dictionary["xaxis"][ + "index" + ][0] def update_selection_tab(self, result_of_import=None): - self.parent.ui.distance_detector_sample.setText(result_of_import["metadata"]["distance_detector_sample"]) - self.parent.ui.detector_offset.setText(result_of_import["metadata"]["detector_offset"]) + self.parent.ui.distance_detector_sample.setText( + result_of_import["metadata"]["distance_detector_sample"] + ) + self.parent.ui.detector_offset.setText( + result_of_import["metadata"]["detector_offset"] + ) self.parent.disable_left_part_of_selection_tab() self.parent.ui.info_message_about_cyan.setVisible(False) @@ -119,9 +154,15 @@ def save_march_dollase_parameters(self, metadata_dict): "a6": np.nan, } - self.parent.march_dollase_fitting_history_table = march_dollase_fitting_history_table - self.parent.march_dollase_fitting_initial_parameters = march_dollase_fitting_initial_parameters - self.parent.march_dollase_fitting_range_selected = metadata_dict["march-dollase bragg peak selection range"] + self.parent.march_dollase_fitting_history_table = ( + march_dollase_fitting_history_table + ) + self.parent.march_dollase_fitting_initial_parameters = ( + march_dollase_fitting_initial_parameters + ) + self.parent.march_dollase_fitting_range_selected = metadata_dict[ + "march-dollase bragg peak selection range" + ] def save_initial_roi_dimension_from_config_file(self, column_3_dict): """column_3_dict = {'x0': value, 'y0': value, 'width': value, 'height': value}""" @@ -134,7 +175,9 @@ def save_initial_roi_dimension_from_config_file(self, column_3_dict): column_3_dict["height"], ] - def create_fitting_input_dictionary_from_imported_ascii_file(self, result_of_import): + def create_fitting_input_dictionary_from_imported_ascii_file( + self, result_of_import + ): metadata = result_of_import["metadata"] self.parent.kropff_fitting_range["high"] = metadata["kropff_high"] self.parent.kropff_fitting_range["low"] = metadata["kropff_low"] @@ -146,8 +189,12 @@ def create_fitting_input_dictionary_from_imported_ascii_file(self, result_of_imp columns_roi = metadata["columns"] o_init = PeakFittingInitialization(parent=self.parent) - self.parent.fitting_input_dictionary = o_init.fitting_input_dictionary(nbr_rois=len(columns_roi)) - self.parent.fitting_input_dictionary["bragg_edge_range"] = metadata["bragg_edge_range"] + self.parent.fitting_input_dictionary = o_init.fitting_input_dictionary( + nbr_rois=len(columns_roi) + ) + self.parent.fitting_input_dictionary["bragg_edge_range"] = metadata[ + "bragg_edge_range" + ] data = result_of_import["data"] tof_array = np.array(data["tof"]) @@ -155,37 +202,51 @@ def create_fitting_input_dictionary_from_imported_ascii_file(self, result_of_imp lambda_array = np.array(data["lambda"]) rois_dictionary = OrderedDict() - lda_array_of_peak_selected = lambda_array[self.parent.bragg_edge_range[0] : self.parent.bragg_edge_range[1]] + lda_array_of_peak_selected = lambda_array[ + self.parent.bragg_edge_range[0] : self.parent.bragg_edge_range[1] + ] for col in np.arange(3, len(columns_roi) + 3): str_col = str(col) col_index = col - 3 # high lambda - self.parent.fitting_input_dictionary["rois"][col_index]["profile"] = np.array(data[str_col]) - self.parent.fitting_input_dictionary["rois"][col_index]["x0"] = columns_roi[str_col]["x0"] - self.parent.fitting_input_dictionary["rois"][col_index]["y0"] = columns_roi[str_col]["y0"] - self.parent.fitting_input_dictionary["rois"][col_index]["width"] = columns_roi[str_col]["width"] - self.parent.fitting_input_dictionary["rois"][col_index]["height"] = columns_roi[str_col]["height"] - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["high"]["a0"] = columns_roi[ + self.parent.fitting_input_dictionary["rois"][col_index]["profile"] = ( + np.array(data[str_col]) + ) + self.parent.fitting_input_dictionary["rois"][col_index]["x0"] = columns_roi[ str_col - ]["kropff"]["a0"] - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["high"]["b0"] = columns_roi[ + ]["x0"] + self.parent.fitting_input_dictionary["rois"][col_index]["y0"] = columns_roi[ str_col - ]["kropff"]["b0"] - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["high"]["a0_error"] = ( - columns_roi[str_col]["kropff"]["a0_error"] + ]["y0"] + self.parent.fitting_input_dictionary["rois"][col_index]["width"] = ( + columns_roi[str_col]["width"] ) - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["high"]["b0_error"] = ( - columns_roi[str_col]["kropff"]["b0_error"] + self.parent.fitting_input_dictionary["rois"][col_index]["height"] = ( + columns_roi[str_col]["height"] ) + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["high"]["a0"] = columns_roi[str_col]["kropff"]["a0"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["high"]["b0"] = columns_roi[str_col]["kropff"]["b0"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["high"]["a0_error"] = columns_roi[str_col]["kropff"]["a0_error"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["high"]["b0_error"] = columns_roi[str_col]["kropff"]["b0_error"] xaxis_to_fit = lda_array_of_peak_selected[ - self.parent.kropff_fitting_range["high"][0] : self.parent.kropff_fitting_range["high"][1] + self.parent.kropff_fitting_range["high"][ + 0 + ] : self.parent.kropff_fitting_range["high"][1] ] - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["high"]["xaxis_to_fit"] = ( - xaxis_to_fit - ) + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["high"]["xaxis_to_fit"] = xaxis_to_fit kropff_a0 = ( np.nan @@ -198,30 +259,32 @@ def create_fitting_input_dictionary_from_imported_ascii_file(self, result_of_imp else float(columns_roi[str_col]["kropff"]["b0"]) ) yaxis_fitted = kropff_high_lambda(xaxis_to_fit, kropff_a0, kropff_b0) - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["high"]["yaxis_fitted"] = ( - yaxis_fitted - ) + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["high"]["yaxis_fitted"] = yaxis_fitted # low lambda - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["low"]["ahkl"] = columns_roi[ - str_col - ]["kropff"]["ahkl"] - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["low"]["bhkl"] = columns_roi[ - str_col - ]["kropff"]["bhkl"] - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["low"]["ahkl_error"] = ( - columns_roi[str_col]["kropff"]["ahkl_error"] - ) - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["low"]["bhkl_error"] = ( - columns_roi[str_col]["kropff"]["bhkl_error"] - ) + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["low"]["ahkl"] = columns_roi[str_col]["kropff"]["ahkl"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["low"]["bhkl"] = columns_roi[str_col]["kropff"]["bhkl"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["low"]["ahkl_error"] = columns_roi[str_col]["kropff"]["ahkl_error"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["low"]["bhkl_error"] = columns_roi[str_col]["kropff"]["bhkl_error"] xaxis_to_fit = lda_array_of_peak_selected[ - self.parent.kropff_fitting_range["low"][0] : self.parent.kropff_fitting_range["low"][1] + self.parent.kropff_fitting_range["low"][ + 0 + ] : self.parent.kropff_fitting_range["low"][1] ] - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["low"]["xaxis_to_fit"] = ( - xaxis_to_fit - ) + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["low"]["xaxis_to_fit"] = xaxis_to_fit kropff_ahkl = ( np.nan @@ -234,37 +297,45 @@ def create_fitting_input_dictionary_from_imported_ascii_file(self, result_of_imp else float(columns_roi[str_col]["kropff"]["bhkl"]) ) - yaxis_fitted = kropff_low_lambda(xaxis_to_fit, kropff_a0, kropff_b0, kropff_ahkl, kropff_bhkl) - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["low"]["yaxis_fitted"] = ( - yaxis_fitted + yaxis_fitted = kropff_low_lambda( + xaxis_to_fit, kropff_a0, kropff_b0, kropff_ahkl, kropff_bhkl ) + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["low"]["yaxis_fitted"] = yaxis_fitted # Bragg peak - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["bragg_peak"]["ldahkl"] = ( - columns_roi[str_col]["kropff"]["ldahkl"] - ) - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["bragg_peak"]["tau"] = ( - columns_roi[str_col]["kropff"]["tau"] - ) - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["bragg_peak"]["sigma"] = ( - columns_roi[str_col]["kropff"]["sigma"] - ) - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["bragg_peak"][ + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["bragg_peak"]["ldahkl"] = columns_roi[str_col]["kropff"]["ldahkl"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["bragg_peak"]["tau"] = columns_roi[str_col]["kropff"]["tau"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["bragg_peak"]["sigma"] = columns_roi[str_col]["kropff"]["sigma"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["bragg_peak"]["ldahkl_error"] = columns_roi[str_col]["kropff"][ "ldahkl_error" - ] = columns_roi[str_col]["kropff"]["ldahkl_error"] - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["bragg_peak"]["tau_error"] = ( - columns_roi[str_col]["kropff"]["tau_error"] - ) - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["bragg_peak"][ + ] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["bragg_peak"]["tau_error"] = columns_roi[str_col]["kropff"]["tau_error"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["bragg_peak"]["sigma_error"] = columns_roi[str_col]["kropff"][ "sigma_error" - ] = columns_roi[str_col]["kropff"]["sigma_error"] + ] xaxis_to_fit = lda_array_of_peak_selected[ - self.parent.kropff_fitting_range["bragg_peak"][0] : self.parent.kropff_fitting_range["bragg_peak"][1] + self.parent.kropff_fitting_range["bragg_peak"][ + 0 + ] : self.parent.kropff_fitting_range["bragg_peak"][1] ] - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["bragg_peak"][ - "xaxis_to_fit" - ] = xaxis_to_fit + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["bragg_peak"]["xaxis_to_fit"] = xaxis_to_fit kropff_tau = ( np.nan @@ -283,55 +354,64 @@ def create_fitting_input_dictionary_from_imported_ascii_file(self, result_of_imp ) yaxis_fitted = kropff_bragg_peak_tof( - xaxis_to_fit, kropff_a0, kropff_b0, kropff_ahkl, kropff_bhkl, kropff_ldahkl, kropff_sigma, kropff_tau + xaxis_to_fit, + kropff_a0, + kropff_b0, + kropff_ahkl, + kropff_bhkl, + kropff_ldahkl, + kropff_sigma, + kropff_tau, ) - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["kropff"]["bragg_peak"][ - "yaxis_fitted" - ] = yaxis_fitted + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "kropff" + ]["bragg_peak"]["yaxis_fitted"] = yaxis_fitted # March_dollase - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["march_dollase"]["d_spacing"] = ( - columns_roi[str_col]["march_dollase"]["d_spacing"] - ) - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["march_dollase"]["sigma"] = columns_roi[ - str_col - ]["march_dollase"]["sigma"] - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["march_dollase"]["alpha"] = columns_roi[ - str_col - ]["march_dollase"]["alpha"] - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["march_dollase"]["a1"] = columns_roi[ - str_col - ]["march_dollase"]["a1"] - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["march_dollase"]["a2"] = columns_roi[ - str_col - ]["march_dollase"]["a2"] - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["march_dollase"]["a5"] = columns_roi[ - str_col - ]["march_dollase"]["a5"] - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["march_dollase"]["a6"] = columns_roi[ - str_col - ]["march_dollase"]["a6"] - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["march_dollase"]["d_spacing_error"] = ( - columns_roi[str_col]["march_dollase"]["d_spacing_error"] - ) - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["march_dollase"]["sigma_error"] = ( - columns_roi[str_col]["march_dollase"]["sigma_error"] - ) - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["march_dollase"]["alpha_error"] = ( - columns_roi[str_col]["march_dollase"]["alpha_error"] - ) - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["march_dollase"]["a1_error"] = ( - columns_roi[str_col]["march_dollase"]["a1_error"] - ) - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["march_dollase"]["a2_error"] = ( - columns_roi[str_col]["march_dollase"]["a2_error"] - ) - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["march_dollase"]["a5_error"] = ( - columns_roi[str_col]["march_dollase"]["a5_error"] - ) - self.parent.fitting_input_dictionary["rois"][col_index]["fitting"]["march_dollase"]["a6_error"] = ( - columns_roi[str_col]["march_dollase"]["a6_error"] - ) + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "march_dollase" + ]["d_spacing"] = columns_roi[str_col]["march_dollase"]["d_spacing"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "march_dollase" + ]["sigma"] = columns_roi[str_col]["march_dollase"]["sigma"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "march_dollase" + ]["alpha"] = columns_roi[str_col]["march_dollase"]["alpha"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "march_dollase" + ]["a1"] = columns_roi[str_col]["march_dollase"]["a1"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "march_dollase" + ]["a2"] = columns_roi[str_col]["march_dollase"]["a2"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "march_dollase" + ]["a5"] = columns_roi[str_col]["march_dollase"]["a5"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "march_dollase" + ]["a6"] = columns_roi[str_col]["march_dollase"]["a6"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "march_dollase" + ]["d_spacing_error"] = columns_roi[str_col]["march_dollase"][ + "d_spacing_error" + ] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "march_dollase" + ]["sigma_error"] = columns_roi[str_col]["march_dollase"]["sigma_error"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "march_dollase" + ]["alpha_error"] = columns_roi[str_col]["march_dollase"]["alpha_error"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "march_dollase" + ]["a1_error"] = columns_roi[str_col]["march_dollase"]["a1_error"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "march_dollase" + ]["a2_error"] = columns_roi[str_col]["march_dollase"]["a2_error"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "march_dollase" + ]["a5_error"] = columns_roi[str_col]["march_dollase"]["a5_error"] + self.parent.fitting_input_dictionary["rois"][col_index]["fitting"][ + "march_dollase" + ]["a6_error"] = columns_roi[str_col]["march_dollase"]["a6_error"] xaxis_dictionary = { "index": (index_array, self.parent.xaxis_label["index"]), @@ -339,6 +419,6 @@ def create_fitting_input_dictionary_from_imported_ascii_file(self, result_of_imp "tof": (tof_array, self.parent.xaxis_label["tof"]), } self.parent.fitting_input_dictionary["xaxis"] = xaxis_dictionary - self.parent.fitting_input_dictionary["bragg_edge_range_selected"] = result_of_import["metadata"][ - "bragg_edge_range" - ] + self.parent.fitting_input_dictionary["bragg_edge_range_selected"] = ( + result_of_import["metadata"]["bragg_edge_range"] + ) diff --git a/notebooks/__code/bragg_edge/kropff.py b/notebooks/__code/bragg_edge/kropff.py index af7e6ad3d..7751a4edc 100755 --- a/notebooks/__code/bragg_edge/kropff.py +++ b/notebooks/__code/bragg_edge/kropff.py @@ -40,7 +40,9 @@ def reset_low_lambda_table(self): def reset_bragg_peak_table(self): self.clear_table(table_name="bragg_peak") - self.fill_table_with_minimum_contain(table_ui=self.parent.ui.bragg_edge_tableWidget) + self.fill_table_with_minimum_contain( + table_ui=self.parent.ui.bragg_edge_tableWidget + ) def clear_table(self, table_name="high_lambda", is_all=False): """remove all the rows of the table name specified, or all if is_all is True""" @@ -90,7 +92,9 @@ def fill_table_with_fitting_information(self): o_table = TableHandler(table_ui=self.table_ui["bragg_peak"]) _col = 1 for _row in fitting_input_dictionary["rois"].keys(): - _entry = fitting_input_dictionary["rois"][_row]["fitting"]["kropff"]["bragg_peak"] + _entry = fitting_input_dictionary["rois"][_row]["fitting"]["kropff"][ + "bragg_peak" + ] o_table.set_item_with_float(_row, _col, _entry["ldahkl"]) o_table.set_item_with_float(_row, _col + 1, _entry["tau"]) o_table.set_item_with_float(_row, _col + 2, _entry["sigma"]) @@ -123,8 +127,12 @@ def bragg_peak_right_click(self, position=None): def fit_bragg_peak_selected_rows(self): o_gui = GuiUtility(parent=self.parent) - list_rows_selected = o_gui.get_rows_of_table_selected(table_ui=self.parent.ui.bragg_edge_tableWidget) - self.parent.kropff_fit_bragg_peak_region_of_selected_rows(list_row_to_fit=list_rows_selected) + list_rows_selected = o_gui.get_rows_of_table_selected( + table_ui=self.parent.ui.bragg_edge_tableWidget + ) + self.parent.kropff_fit_bragg_peak_region_of_selected_rows( + list_row_to_fit=list_rows_selected + ) def export_bragg_peak_profile(self): working_dir = str(Path(self.parent.working_dir).parent) @@ -136,12 +144,16 @@ def export_bragg_peak_profile(self): if _export_folder: o_gui = GuiUtility(parent=self.parent) - list_row_selected = o_gui.get_rows_of_table_selected(table_ui=self.parent.ui.bragg_edge_tableWidget) + list_row_selected = o_gui.get_rows_of_table_selected( + table_ui=self.parent.ui.bragg_edge_tableWidget + ) for row_selected in list_row_selected: # make up output file name name_of_row = o_gui.get_table_str_item( - table_ui=self.parent.ui.bragg_edge_tableWidget, row=row_selected, column=0 + table_ui=self.parent.ui.bragg_edge_tableWidget, + row=row_selected, + column=0, ) [x0, y0, width, height] = name_of_row.split("; ") name_of_row_formatted = f"x0{x0}_y0{y0}_width{width}_height{height}" @@ -154,10 +166,18 @@ def export_bragg_peak_profile(self): x_axis = o_fit.xaxis_to_fit y_axis = o_fit.list_yaxis_to_fit[row_selected] - a0 = self.parent.fitting_input_dictionary["rois"][row_selected]["fitting"]["kropff"]["high"]["a0"] - b0 = self.parent.fitting_input_dictionary["rois"][row_selected]["fitting"]["kropff"]["high"]["b0"] - ahkl = self.parent.fitting_input_dictionary["rois"][row_selected]["fitting"]["kropff"]["low"]["ahkl"] - bhkl = self.parent.fitting_input_dictionary["rois"][row_selected]["fitting"]["kropff"]["low"]["bhkl"] + a0 = self.parent.fitting_input_dictionary["rois"][row_selected][ + "fitting" + ]["kropff"]["high"]["a0"] + b0 = self.parent.fitting_input_dictionary["rois"][row_selected][ + "fitting" + ]["kropff"]["high"]["b0"] + ahkl = self.parent.fitting_input_dictionary["rois"][row_selected][ + "fitting" + ]["kropff"]["low"]["ahkl"] + bhkl = self.parent.fitting_input_dictionary["rois"][row_selected][ + "fitting" + ]["kropff"]["low"]["bhkl"] metadata = [f"# Bragg peak fitting of row {row_selected + 1}"] metadata.append(f"# x0: {x0}") @@ -172,7 +192,10 @@ def export_bragg_peak_profile(self): metadata.append("# lambda (Angstroms), average transmission") make_ascii_file_from_2dim_array( - metadata=metadata, col1=x_axis, col2=y_axis, output_file_name=full_file_name + metadata=metadata, + col1=x_axis, + col2=y_axis, + output_file_name=full_file_name, ) message = f"Exported {len(list_row_selected)} file(s) in {_export_folder}" @@ -190,12 +213,19 @@ def select_all_rows_with_negative_thkl(self): list_of_rows_to_select = [] fitting_input_dictionary_rois = self.parent.fitting_input_dictionary["rois"] for _row in fitting_input_dictionary_rois.keys(): - _thkl = float(fitting_input_dictionary_rois[_row]["fitting"]["kropff"]["bragg_peak"]["ldahkl"]) + _thkl = float( + fitting_input_dictionary_rois[_row]["fitting"]["kropff"]["bragg_peak"][ + "ldahkl" + ] + ) if _thkl < 0: list_of_rows_to_select.append(_row) o_gui = GuiUtility(parent=self.parent) - o_gui.select_rows_of_table(table_ui=self.parent.ui.bragg_edge_tableWidget, list_of_rows=list_of_rows_to_select) + o_gui.select_rows_of_table( + table_ui=self.parent.ui.bragg_edge_tableWidget, + list_of_rows=list_of_rows_to_select, + ) def update_fitting_plot(self): self.parent.ui.fitting.clear() @@ -240,15 +270,23 @@ def update_fitting_plot(self): if self.parent.fitting_peak_ui: self.parent.ui.fitting.removeItem(self.parent.fitting_peak_ui) self.parent.fitting_peak_ui = pg.LinearRegionItem( - values=peak_range, orientation=None, brush=None, movable=True, bounds=None + values=peak_range, + orientation=None, + brush=None, + movable=True, + bounds=None, + ) + self.parent.fitting_peak_ui.sigRegionChanged.connect( + self.parent.fitting_range_changed ) - self.parent.fitting_peak_ui.sigRegionChanged.connect(self.parent.fitting_range_changed) self.parent.fitting_peak_ui.setZValue(-10) self.parent.ui.fitting.addItem(self.parent.fitting_peak_ui) else: for row_selected in list_row_selected: - selected_roi = self.parent.fitting_input_dictionary["rois"][row_selected] + selected_roi = self.parent.fitting_input_dictionary["rois"][ + row_selected + ] xaxis_dict = self.parent.fitting_input_dictionary["xaxis"] [left_xaxis_index, right_xaxis_index] = self.parent.bragg_edge_range @@ -260,7 +298,9 @@ def update_fitting_plot(self): yaxis = yaxis[left_xaxis_index:right_xaxis_index] self.parent.ui.fitting.setLabel("bottom", xaxis_label) - self.parent.ui.fitting.setLabel("left", "Cross Section (arbitrary units)") + self.parent.ui.fitting.setLabel( + "left", "Cross Section (arbitrary units)" + ) yaxis = -np.log(yaxis) self.parent.ui.fitting.plot( xaxis, @@ -277,15 +317,24 @@ def update_fitting_plot(self): if peak_range_index[0] is None: peak_range = self.parent.bragg_edge_range else: - peak_range = [xaxis[peak_range_index[0]], xaxis[peak_range_index[1]]] + peak_range = [ + xaxis[peak_range_index[0]], + xaxis[peak_range_index[1]], + ] if self.parent.fitting_peak_ui: self.parent.ui.fitting.removeItem(self.parent.fitting_peak_ui) self.parent.fitting_peak_ui = pg.LinearRegionItem( - values=peak_range, orientation=None, brush=None, movable=False, bounds=None + values=peak_range, + orientation=None, + brush=None, + movable=False, + bounds=None, + ) + self.parent.fitting_peak_ui.sigRegionChanged.connect( + self.parent.fitting_range_changed ) - self.parent.fitting_peak_ui.sigRegionChanged.connect(self.parent.fitting_range_changed) self.parent.fitting_peak_ui.setZValue(-10) self.parent.ui.fitting.addItem(self.parent.fitting_peak_ui) @@ -294,18 +343,31 @@ def update_fitting_plot(self): if key_path_exists_in_dictionary( dictionary=self.parent.fitting_input_dictionary, - tree_key=["rois", row_selected, "fitting", algo_name, name_of_page, "xaxis_to_fit"], + tree_key=[ + "rois", + row_selected, + "fitting", + algo_name, + name_of_page, + "xaxis_to_fit", + ], ): # show fit only if tof scale selected if x_axis_selected == "lambda": - _entry = self.parent.fitting_input_dictionary["rois"][row_selected]["fitting"][algo_name][ - name_of_page - ] + _entry = self.parent.fitting_input_dictionary["rois"][ + row_selected + ]["fitting"][algo_name][name_of_page] xaxis = _entry["xaxis_to_fit"] yaxis = _entry["yaxis_fitted"] # yaxis = -np.log(yaxis) self.parent.ui.fitting.plot( - xaxis, yaxis, pen=(self.parent.fit_rgb[0], self.parent.fit_rgb[1], self.parent.fit_rgb[2]) + xaxis, + yaxis, + pen=( + self.parent.fit_rgb[0], + self.parent.fit_rgb[1], + self.parent.fit_rgb[2], + ), ) if peak_range_index[0] is None: @@ -328,9 +390,9 @@ def update_roi_labels(self): left_index = find_nearest_index(array=xaxis, value=left_range) right_index = find_nearest_index(array=xaxis, value=right_range) - xaxis_in_selected_axis = self.parent.fitting_input_dictionary["xaxis"][x_axis_selected][0][ - global_left_range:global_right_range - ] + xaxis_in_selected_axis = self.parent.fitting_input_dictionary["xaxis"][ + x_axis_selected + ][0][global_left_range:global_right_range] real_left_value = xaxis_in_selected_axis[left_index] real_right_value = xaxis_in_selected_axis[right_index] if x_axis_selected == "lambda": diff --git a/notebooks/__code/bragg_edge/kropff_fitting_job_handler.py b/notebooks/__code/bragg_edge/kropff_fitting_job_handler.py index 1830ab89a..830f8f86a 100755 --- a/notebooks/__code/bragg_edge/kropff_fitting_job_handler.py +++ b/notebooks/__code/bragg_edge/kropff_fitting_job_handler.py @@ -4,7 +4,11 @@ from lmfit import Model, Parameter from __code.bragg_edge.bragg_edge_peak_fitting_gui_utility import GuiUtility -from __code.bragg_edge.fitting_functions import kropff_bragg_peak_tof, kropff_high_lambda, kropff_low_lambda +from __code.bragg_edge.fitting_functions import ( + kropff_bragg_peak_tof, + kropff_high_lambda, + kropff_low_lambda, +) class KropffFittingJobHandler: @@ -21,7 +25,10 @@ def prepare(self, kropff_tooldbox="high"): :param kropff_tooldbox: 'high', 'low', 'bragg_peak' """ if kropff_tooldbox == "bragg_peak": - fitting_range = [self.parent.kropff_fitting_range["low"][0], self.parent.kropff_fitting_range["high"][1]] + fitting_range = [ + self.parent.kropff_fitting_range["low"][0], + self.parent.kropff_fitting_range["high"][1], + ] else: fitting_range = self.parent.kropff_fitting_range[kropff_tooldbox] # fitting_range = self.parent.kropff_fitting_range[kropff_tooldbox] @@ -36,7 +43,9 @@ def prepare(self, kropff_tooldbox="high"): for _key in self.parent.fitting_input_dictionary["rois"].keys(): _yaxis = self.parent.fitting_input_dictionary["rois"][_key]["profile"] full_fitting_yaxis = _yaxis[left_xaxis_index:right_xaxis_index] - list_yaxis_to_fit.append(full_fitting_yaxis[fitting_range[0] : fitting_range[1] + 1]) + list_yaxis_to_fit.append( + full_fitting_yaxis[fitting_range[0] : fitting_range[1] + 1] + ) self.list_yaxis_to_fit = list_yaxis_to_fit def run_kropff_high_lambda(self, update_table_ui=False): @@ -66,10 +75,14 @@ def run_kropff_high_lambda(self, update_table_ui=False): "yaxis_fitted": yaxis_fitted, } - self.parent.fitting_input_dictionary["rois"][_index]["fitting"]["kropff"]["high"] = deepcopy(result_dict) + self.parent.fitting_input_dictionary["rois"][_index]["fitting"]["kropff"][ + "high" + ] = deepcopy(result_dict) if update_table_ui: - o_gui.update_kropff_high_lambda_table_ui(row=_index, a0=a0, b0=b0, a0_error=a0_error, b0_error=b0_error) + o_gui.update_kropff_high_lambda_table_ui( + row=_index, a0=a0, b0=b0, a0_error=a0_error, b0_error=b0_error + ) def run_kropff_low_lambda(self, update_table_ui=False): gmodel = Model(kropff_low_lambda, missing="drop", independent_vars=["lda"]) @@ -81,7 +94,9 @@ def run_kropff_low_lambda(self, update_table_ui=False): bhkl_init = float(str(self.parent.kropff_low_lda_bhkl_init.text())) for _row, yaxis in enumerate(self.list_yaxis_to_fit): - _entry = self.parent.fitting_input_dictionary["rois"][_row]["fitting"]["kropff"]["high"] + _entry = self.parent.fitting_input_dictionary["rois"][_row]["fitting"][ + "kropff" + ]["high"] a0 = float(_entry["a0"]) b0 = float(_entry["b0"]) @@ -111,15 +126,23 @@ def run_kropff_low_lambda(self, update_table_ui=False): "yaxis_fitted": yaxis_fitted, } - self.parent.fitting_input_dictionary["rois"][_row]["fitting"]["kropff"]["low"] = deepcopy(result_dict) + self.parent.fitting_input_dictionary["rois"][_row]["fitting"]["kropff"][ + "low" + ] = deepcopy(result_dict) if update_table_ui: o_gui.update_kropff_low_lambda_table_ui( - row=_row, ahkl=ahkl, bhkl=bhkl, ahkl_error=ahkl_error, bhkl_error=bhkl_error + row=_row, + ahkl=ahkl, + bhkl=bhkl, + ahkl_error=ahkl_error, + bhkl_error=bhkl_error, ) def run_bragg_peak(self, update_table_ui=False, list_row_to_fit=None): - gmodel = Model(kropff_bragg_peak_tof, nan_policy="propagate", independent_vars=["lda"]) + gmodel = Model( + kropff_bragg_peak_tof, nan_policy="propagate", independent_vars=["lda"] + ) lda = self.xaxis_to_fit o_gui = GuiUtility(parent=self.parent) @@ -133,11 +156,15 @@ def run_bragg_peak(self, update_table_ui=False, list_row_to_fit=None): if _row not in list_row_to_fit: continue - _entry_high = self.parent.fitting_input_dictionary["rois"][_row]["fitting"]["kropff"]["high"] + _entry_high = self.parent.fitting_input_dictionary["rois"][_row]["fitting"][ + "kropff" + ]["high"] a0 = float(_entry_high["a0"]) b0 = float(_entry_high["b0"]) - _entry_low = self.parent.fitting_input_dictionary["rois"][_row]["fitting"]["kropff"]["low"] + _entry_low = self.parent.fitting_input_dictionary["rois"][_row]["fitting"][ + "kropff" + ]["low"] ahkl = float(_entry_low["ahkl"]) bhkl = float(_entry_low["bhkl"]) @@ -162,7 +189,9 @@ def run_bragg_peak(self, update_table_ui=False, list_row_to_fit=None): tau = _result.params["tau"].value tau_error = _result.params["tau"].stderr - yaxis_fitted = kropff_bragg_peak_tof(self.xaxis_to_fit, a0, b0, ahkl, bhkl, ldahkl, sigma, tau) + yaxis_fitted = kropff_bragg_peak_tof( + self.xaxis_to_fit, a0, b0, ahkl, bhkl, ldahkl, sigma, tau + ) result_dict = { "ldahkl": ldahkl, @@ -175,9 +204,9 @@ def run_bragg_peak(self, update_table_ui=False, list_row_to_fit=None): "yaxis_fitted": yaxis_fitted, } - self.parent.fitting_input_dictionary["rois"][_row]["fitting"]["kropff"]["bragg_peak"] = deepcopy( - result_dict - ) + self.parent.fitting_input_dictionary["rois"][_row]["fitting"]["kropff"][ + "bragg_peak" + ] = deepcopy(result_dict) if update_table_ui: o_gui.update_kropff_bragg_edge_table_ui( diff --git a/notebooks/__code/bragg_edge/march_dollase.py b/notebooks/__code/bragg_edge/march_dollase.py index 6103cb58f..4f9f57e41 100755 --- a/notebooks/__code/bragg_edge/march_dollase.py +++ b/notebooks/__code/bragg_edge/march_dollase.py @@ -96,13 +96,17 @@ def move_table_row(self, to_row_offset=-1): :param to_row_offset: +1 means moving row to the next row, -1 means moving up by one row """ row_selected = self.get_row_selected() - march_dollase_fitting_history_table = self.parent.march_dollase_fitting_history_table + march_dollase_fitting_history_table = ( + self.parent.march_dollase_fitting_history_table + ) row_to_move = march_dollase_fitting_history_table.pop(row_selected) new_row = row_selected + to_row_offset march_dollase_fitting_history_table.insert(new_row, row_to_move) - self.parent.march_dollase_fitting_history_table = march_dollase_fitting_history_table + self.parent.march_dollase_fitting_history_table = ( + march_dollase_fitting_history_table + ) o_gui = GuiUtility(parent=self.parent) o_gui.fill_march_dollase_table( @@ -165,42 +169,60 @@ def insert_row(self): march_dollase_fitting_history_table = list() new_entry = self.parent.march_dollase_fitting_history_table_default_new_row march_dollase_fitting_history_table.insert(0, new_entry) - self.parent.march_dollase_fitting_history_table = march_dollase_fitting_history_table + self.parent.march_dollase_fitting_history_table = ( + march_dollase_fitting_history_table + ) self.update_table_after_changing_row(changing_row=0) def insert_row_above(self): o_table = TableHandler(table_ui=self.parent.ui.march_dollase_user_input_table) row_selected = o_table.get_row_selected() - march_dollase_fitting_history_table = self.parent.march_dollase_fitting_history_table + march_dollase_fitting_history_table = ( + self.parent.march_dollase_fitting_history_table + ) new_entry = [False for _entry in march_dollase_fitting_history_table[0]] march_dollase_fitting_history_table.insert(row_selected, new_entry) - self.parent.march_dollase_fitting_history_table = march_dollase_fitting_history_table + self.parent.march_dollase_fitting_history_table = ( + march_dollase_fitting_history_table + ) self.update_table_after_changing_row(changing_row=row_selected + 1) def insert_row_below(self): o_table = TableHandler(table_ui=self.parent.ui.march_dollase_user_input_table) row_selected = o_table.get_row_selected() - march_dollase_fitting_history_table = self.parent.march_dollase_fitting_history_table + march_dollase_fitting_history_table = ( + self.parent.march_dollase_fitting_history_table + ) new_entry = [False for _entry in march_dollase_fitting_history_table[0]] march_dollase_fitting_history_table.insert(row_selected + 1, new_entry) - self.parent.march_dollase_fitting_history_table = march_dollase_fitting_history_table + self.parent.march_dollase_fitting_history_table = ( + march_dollase_fitting_history_table + ) self.update_table_after_changing_row(changing_row=row_selected + 2) def duplicate_row(self): o_table = TableHandler(table_ui=self.parent.ui.march_dollase_user_input_table) row_selected = o_table.get_row_selected() - march_dollase_fitting_history_table = self.parent.march_dollase_fitting_history_table + march_dollase_fitting_history_table = ( + self.parent.march_dollase_fitting_history_table + ) new_entry = march_dollase_fitting_history_table[row_selected] march_dollase_fitting_history_table.insert(row_selected, new_entry) - self.parent.march_dollase_fitting_history_table = march_dollase_fitting_history_table + self.parent.march_dollase_fitting_history_table = ( + march_dollase_fitting_history_table + ) self.update_table_after_changing_row(changing_row=row_selected + 1) def delete_row(self): o_table = TableHandler(table_ui=self.parent.ui.march_dollase_user_input_table) row_selected = o_table.get_row_selected() - march_dollase_fitting_history_table = self.parent.march_dollase_fitting_history_table + march_dollase_fitting_history_table = ( + self.parent.march_dollase_fitting_history_table + ) march_dollase_fitting_history_table.pop(row_selected) - self.parent.march_dollase_fitting_history_table = march_dollase_fitting_history_table + self.parent.march_dollase_fitting_history_table = ( + march_dollase_fitting_history_table + ) self.update_table_after_changing_row(changing_row=row_selected) def full_reset(self): @@ -224,13 +246,19 @@ def update_table_after_changing_row(self, changing_row=-1): self.table_clicked(row=changing_row) def advanced_mode_clicked(self): - hide_advanced = not self.parent.ui.march_dollase_advanced_mode_checkBox.isChecked() + hide_advanced = ( + not self.parent.ui.march_dollase_advanced_mode_checkBox.isChecked() + ) o_gui = GuiUtility(parent=self.parent) o_gui.set_columns_hidden( - table_ui=self.parent.ui.march_dollase_user_input_table, list_of_columns=[5, 6], state=hide_advanced + table_ui=self.parent.ui.march_dollase_user_input_table, + list_of_columns=[5, 6], + state=hide_advanced, ) o_gui.set_columns_hidden( - table_ui=self.parent.ui.march_dollase_result_table, list_of_columns=[6, 7, 13, 14], state=hide_advanced + table_ui=self.parent.ui.march_dollase_result_table, + list_of_columns=[6, 7, 13, 14], + state=hide_advanced, ) def update_fitting_plot(self): @@ -275,7 +303,11 @@ def update_fitting_plot(self): if self.parent.march_dollase_fitting_peak_ui: self.parent.ui.fitting.removeItem(self.parent.march_dollase_fitting_peak_ui) self.parent.march_dollase_fitting_peak_ui = pg.LinearRegionItem( - values=local_peak_range, orientation=None, brush=None, movable=move_bragg_peak_range, bounds=None + values=local_peak_range, + orientation=None, + brush=None, + movable=move_bragg_peak_range, + bounds=None, ) self.parent.march_dollase_fitting_peak_ui.sigRegionChanged.connect( self.parent.march_dollase_fitting_range_changed @@ -287,7 +319,9 @@ def update_fitting_plot(self): def save_table_history_and_initial_parameters(self): march_dollase_fitting_history_table = list() - march_dollase_fitting_initial_parameters = self.parent.march_dollase_fitting_initial_parameters + march_dollase_fitting_initial_parameters = ( + self.parent.march_dollase_fitting_initial_parameters + ) nbr_row = self.history_table_ui.rowCount() nbr_column = self.history_table_ui.columnCount() for _row in np.arange(nbr_row): @@ -309,8 +343,12 @@ def save_table_history_and_initial_parameters(self): march_dollase_fitting_history_table.append(_row_history) - self.parent.march_dollase_fitting_history_table = march_dollase_fitting_history_table - self.parent.march_dollase_fitting_initial_parameters = march_dollase_fitting_initial_parameters + self.parent.march_dollase_fitting_history_table = ( + march_dollase_fitting_history_table + ) + self.parent.march_dollase_fitting_initial_parameters = ( + march_dollase_fitting_initial_parameters + ) def fill_tables_with_fitting_information(self): self.fill_history_table_with_fitting_information() @@ -341,7 +379,9 @@ def get_initial_parameter_value(self, column=-1): def update_roi_labels(self): [global_left_range, global_right_range] = self.parent.bragg_edge_range - [left_range, right_range] = list(self.parent.march_dollase_fitting_peak_ui.getRegion()) + [left_range, right_range] = list( + self.parent.march_dollase_fitting_peak_ui.getRegion() + ) o_get = Get(parent=self.parent) x_axis_selected = o_get.x_axis_checked() @@ -356,9 +396,9 @@ def update_roi_labels(self): self.parent.march_dollase_fitting_range_selected = [left_index, right_index] - xaxis_in_selected_axis = self.parent.fitting_input_dictionary["xaxis"][x_axis_selected][0][ - global_left_range:global_right_range - ] + xaxis_in_selected_axis = self.parent.fitting_input_dictionary["xaxis"][ + x_axis_selected + ][0][global_left_range:global_right_range] real_left_value = xaxis_in_selected_axis[left_index] real_right_value = xaxis_in_selected_axis[right_index] if x_axis_selected == "lambda": @@ -371,8 +411,12 @@ def update_roi_labels(self): real_right_value = str_format.format(real_right_value) units = Get.units(name=x_axis_selected) - self.parent.ui.march_dollase_bragg_peak_range_from_value.setText(str(real_left_value)) - self.parent.ui.march_dollase_bragg_peak_range_to_value.setText(str(real_right_value)) + self.parent.ui.march_dollase_bragg_peak_range_from_value.setText( + str(real_left_value) + ) + self.parent.ui.march_dollase_bragg_peak_range_to_value.setText( + str(real_right_value) + ) self.parent.ui.march_dollase_from_bragg_peak_range_units.setText(units) self.parent.ui.march_dollase_to_bragg_peak_range_units.setText(units) @@ -397,7 +441,9 @@ def export_data_of_selected_rows(self): str_list_of_rows_selected = [str(_row) for _row in list_of_rows_selected] str_rows = "_".join(str_list_of_rows_selected) - output_file_name = os.path.join(str(_export_folder), f"march_dollase_result_fitting_row{str_rows}.txt") + output_file_name = os.path.join( + str(_export_folder), f"march_dollase_result_fitting_row{str_rows}.txt" + ) fitting_input_dictionary = self.parent.fitting_input_dictionary @@ -408,13 +454,17 @@ def export_data_of_selected_rows(self): # metadata metadata = ["#Marche Dollase Result of Fitting"] - is_advance_mode = self.parent.ui.march_dollase_advanced_mode_checkBox.isChecked() + is_advance_mode = ( + self.parent.ui.march_dollase_advanced_mode_checkBox.isChecked() + ) metadata.append(f"#Using advanced fitting mode: {is_advance_mode}") data_label = "#image index, TOF(micros), Lambda(Angstroms)" temp_data = [] for _row in list_of_rows_selected: - _entry = fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"] + _entry = fitting_input_dictionary["rois"][_row]["fitting"][ + "march_dollase" + ] _top_entry = fitting_input_dictionary["rois"][_row] _line = ( "#-> row {}: x0: {}, y0:{}, width:{}, height:{}, " @@ -452,7 +502,12 @@ def export_data_of_selected_rows(self): str_data += f", {temp_data[_col_index][_index]}" data.append(str_data) - make_ascii_file(metadata=metadata, data=data, output_file_name=output_file_name, dim="1d") + make_ascii_file( + metadata=metadata, + data=data, + output_file_name=output_file_name, + dim="1d", + ) message = f"{output_file_name} has been created!" self.parent.ui.statusbar.showMessage(message, 10000) # 10s diff --git a/notebooks/__code/bragg_edge/march_dollase_fitting_job_handler.py b/notebooks/__code/bragg_edge/march_dollase_fitting_job_handler.py index 5aea28615..bd5d7ce63 100755 --- a/notebooks/__code/bragg_edge/march_dollase_fitting_job_handler.py +++ b/notebooks/__code/bragg_edge/march_dollase_fitting_job_handler.py @@ -2,7 +2,10 @@ from lmfit import Model from qtpy.QtWidgets import QApplication -from __code.bragg_edge.fitting_functions import march_dollase_advanced_fit, march_dollase_basic_fit +from __code.bragg_edge.fitting_functions import ( + march_dollase_advanced_fit, + march_dollase_basic_fit, +) from __code.bragg_edge.get import Get @@ -27,10 +30,14 @@ def initialize_fitting_input_dictionary(self): nbr_column = self.parent.ui.march_dollase_user_input_table.columnCount() list_name_of_parameters = [] for _col in np.arange(nbr_column): - _item = self.parent.ui.march_dollase_user_input_table.horizontalHeaderItem(_col).text() + _item = self.parent.ui.march_dollase_user_input_table.horizontalHeaderItem( + _col + ).text() list_name_of_parameters.append(_item) - march_dollase_fitting_history_table = self.parent.march_dollase_fitting_history_table + march_dollase_fitting_history_table = ( + self.parent.march_dollase_fitting_history_table + ) [d_spacing_flag, sigma_flag, alpha_flag, a1_flag, a2_flag, a5_flag, a6_flag] = ( march_dollase_fitting_history_table[0] ) @@ -44,13 +51,19 @@ def initialize_fitting_input_dictionary(self): for _row in fitting_input_dictionary["rois"].keys(): if not d_spacing_flag: - fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"]["d_spacing"] = d_spacing + fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"][ + "d_spacing" + ] = d_spacing if not sigma_flag: - fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"]["sigma"] = sigma + fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"][ + "sigma" + ] = sigma if not alpha_flag: - fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"]["alpha"] = alpha + fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"][ + "alpha" + ] = alpha # {'left_part': {'lambda_x_axis': [], # 'y_axis': [], @@ -65,27 +78,42 @@ def initialize_fitting_input_dictionary(self): self.left_center_right_axis = self.isolate_left_center_right_axis(row=_row) if self.is_advanced_mode(): a2 = self.get_a2(advanced_mode=self.is_advanced_mode()) - fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"]["a2"] = a2 + fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"][ + "a2" + ] = a2 a5 = self.get_a5() - fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"]["a5"] = a5 + fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"][ + "a5" + ] = a5 a6 = self.get_a6() - fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"]["a6"] = a6 + fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"][ + "a6" + ] = a6 a1 = self.get_a1(advanced_mode=self.is_advanced_mode()) - fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"]["a1"] = a1 + fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"][ + "a1" + ] = a1 else: a1 = self.get_a1(advanced_mode=self.is_advanced_mode()) - fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"]["a1"] = a1 + fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"][ + "a1" + ] = a1 a2 = self.get_a2(advanced_mode=self.is_advanced_mode()) - fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"]["a2"] = a2 + fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"][ + "a2" + ] = a2 def isolate_left_center_right_axis(self, row=-1): bragg_edge_range = self.parent.fitting_input_dictionary["bragg_edge_range"] - [global_left_index, global_right_index] = [int(bragg_edge_range[0]), int(bragg_edge_range[1])] + [global_left_index, global_right_index] = [ + int(bragg_edge_range[0]), + int(bragg_edge_range[1]), + ] # get full x-axis (lambda) full_lambda_x_axis = self.parent.fitting_input_dictionary["xaxis"]["lambda"][0] lambda_x_axis = full_lambda_x_axis[global_left_index:global_right_index] @@ -97,12 +125,18 @@ def isolate_left_center_right_axis(self, row=-1): [left_index, right_index] = self.parent.march_dollase_fitting_range_selected return { - "left_part": {"lambda_x_axis": lambda_x_axis[0:left_index], "y_axis": y_axis[0:left_index]}, + "left_part": { + "lambda_x_axis": lambda_x_axis[0:left_index], + "y_axis": y_axis[0:left_index], + }, "center_part": { "lambda_x_axis": lambda_x_axis[left_index:right_index], "y_axis": y_axis[left_index:right_index], }, - "right_part": {"lambda_x_axis": lambda_x_axis[right_index:], "y_axis": y_axis[right_index:]}, + "right_part": { + "lambda_x_axis": lambda_x_axis[right_index:], + "y_axis": y_axis[right_index:], + }, } def get_a1(self, advanced_mode=True): @@ -182,7 +216,9 @@ def get_d_spacing(self): def prepare(self): self.initialize_fitting_input_dictionary() fitting_input_dictionary = self.parent.fitting_input_dictionary - march_dollase_fitting_history_table = self.parent.march_dollase_fitting_history_table + march_dollase_fitting_history_table = ( + self.parent.march_dollase_fitting_history_table + ) _is_advanced_mode = self.is_advanced_mode() if _is_advanced_mode: @@ -191,7 +227,9 @@ def prepare(self): # gmodel = Model(march_dollase_basic_fit, missing='drop') gmodel = Model(march_dollase_basic_fit, nan_policy="propagate") - march_dollase_fitting_history_table = self.parent.march_dollase_fitting_history_table + march_dollase_fitting_history_table = ( + self.parent.march_dollase_fitting_history_table + ) nbr_row_in_fitting_scenario = len(march_dollase_fitting_history_table) self.parent.ui.eventProgress.setValue(0) @@ -200,9 +238,15 @@ def prepare(self): self.parent.ui.eventProgress.setVisible(True) def set_params(params_object, name_of_parameter, dict_entry, parameter_flag): - params_object.add(name_of_parameter, value=float(dict_entry[name_of_parameter]), vary=parameter_flag) - - def record_result_into_dict(entry_dict, result_object, name_of_parameter, parameter_flag): + params_object.add( + name_of_parameter, + value=float(dict_entry[name_of_parameter]), + vary=parameter_flag, + ) + + def record_result_into_dict( + entry_dict, result_object, name_of_parameter, parameter_flag + ): if parameter_flag: print(f"-> name_of_parameter: {name_of_parameter}") [value, error] = result_object.get_value_err(tag=name_of_parameter) @@ -212,14 +256,26 @@ def record_result_into_dict(entry_dict, result_object, name_of_parameter, parame print(f" - error: {error}") for _roi_row in np.arange(nbr_roi_row): - _entry = fitting_input_dictionary["rois"][_roi_row]["fitting"]["march_dollase"] + _entry = fitting_input_dictionary["rois"][_roi_row]["fitting"][ + "march_dollase" + ] o_get = Get(parent=self.parent) xaxis = o_get.x_axis_data(x_axis_selected="lambda") yaxis = o_get.y_axis_data_of_selected_row(_roi_row) - for _history_row, _row_entry in enumerate(march_dollase_fitting_history_table): - [d_spacing_flag, sigma_flag, alpha_flag, a1_flag, a2_flag, a5_flag, a6_flag] = _row_entry + for _history_row, _row_entry in enumerate( + march_dollase_fitting_history_table + ): + [ + d_spacing_flag, + sigma_flag, + alpha_flag, + a1_flag, + a2_flag, + a5_flag, + a6_flag, + ] = _row_entry if _is_advanced_mode: a5_flag = _entry["a5"] diff --git a/notebooks/__code/bragg_edge/peak_fitting_evaluation/bragg_edge.py b/notebooks/__code/bragg_edge/peak_fitting_evaluation/bragg_edge.py index 1b4a49e6e..fcff8b64c 100755 --- a/notebooks/__code/bragg_edge/peak_fitting_evaluation/bragg_edge.py +++ b/notebooks/__code/bragg_edge/peak_fitting_evaluation/bragg_edge.py @@ -11,10 +11,18 @@ from NeuNorm.normalization import Normalization from neutronbraggedge.braggedge import BraggEdge as BraggEdgeLibrary from neutronbraggedge.experiment_handler import * -from neutronbraggedge.material_handler.retrieve_material_metadata import RetrieveMaterialMetadata +from neutronbraggedge.material_handler.retrieve_material_metadata import ( + RetrieveMaterialMetadata, +) from plotly.offline import iplot from qtpy import QtGui -from qtpy.QtWidgets import QMainWindow, QProgressBar, QTableWidgetItem, QTableWidgetSelectionRange, QVBoxLayout +from qtpy.QtWidgets import ( + QMainWindow, + QProgressBar, + QTableWidgetItem, + QTableWidgetSelectionRange, + QVBoxLayout, +) from __code import file_handler, load_ui from __code.ipywe import fileselector @@ -37,14 +45,20 @@ def full_list_elements(self): box4 = widgets.HBox( [ - widgets.Label("List of elements", layout=widgets.Layout(width=self.label_width)), - widgets.Select(options=self.list_returned, layout=widgets.Layout(width="20%")), + widgets.Label( + "List of elements", layout=widgets.Layout(width=self.label_width) + ), + widgets.Select( + options=self.list_returned, layout=widgets.Layout(width="20%") + ), ] ) box5 = widgets.HBox( [ - widgets.Label("Nbr Bragg Edges", layout=widgets.Layout(width=self.label_width)), + widgets.Label( + "Nbr Bragg Edges", layout=widgets.Layout(width=self.label_width) + ), widgets.IntText(8, layout=widgets.Layout(width="20%")), ] ) @@ -64,14 +78,20 @@ def list_elements(self): box4 = widgets.HBox( [ - widgets.Label("List of elements", layout=widgets.Layout(width=self.label_width)), - widgets.Text(",".join(self.list_of_elements), layout=widgets.Layout(width="20%")), + widgets.Label( + "List of elements", layout=widgets.Layout(width=self.label_width) + ), + widgets.Text( + ",".join(self.list_of_elements), layout=widgets.Layout(width="20%") + ), ] ) box5 = widgets.HBox( [ - widgets.Label("Nbr Bragg Edges", layout=widgets.Layout(width=self.label_width)), + widgets.Label( + "Nbr Bragg Edges", layout=widgets.Layout(width=self.label_width) + ), widgets.Text(str(8), layout=widgets.Layout(width="20%")), ] ) @@ -92,7 +112,10 @@ def exp_setup(self): box3 = widgets.HBox( [ - widgets.Label("detector offset (microS)", layout=widgets.Layout(width=self.label_width)), + widgets.Label( + "detector offset (microS)", + layout=widgets.Layout(width=self.label_width), + ), widgets.Text(str(3700), layout=widgets.Layout(width="20%")), ] ) @@ -109,7 +132,9 @@ def list_powder_bragg_edges(self): list_of_elements = [_element.strip() for _element in list_of_elements] number_of_bragg_edges = int(self.nbr_bragg_edges_ui.value) - _handler = BraggEdgeLibrary(material=list_of_elements, number_of_bragg_edges=number_of_bragg_edges) + _handler = BraggEdgeLibrary( + material=list_of_elements, number_of_bragg_edges=number_of_bragg_edges + ) self.bragg_edges = _handler.bragg_edges self.hkl = _handler.hkl self.handler = _handler @@ -147,7 +172,11 @@ def load_data(self, folder_selected): self.list_files = o_norm.data["sample"]["file_name"] display( - HTML('' + str(len(list_files)) + " files have been loaded") + HTML( + '' + + str(len(list_files)) + + " files have been loaded" + ) ) # define time spectra file @@ -179,7 +208,13 @@ def load_time_spectra(self): def save_time_spectra(self, file): self.spectra_file = file - display(HTML(' Spectra File : ' + self.spectra_file + "")) + display( + HTML( + ' Spectra File : ' + + self.spectra_file + + "" + ) + ) def select_time_spectra_file(self): self.working_dir = os.path.dirname(self.list_files[0]) @@ -209,11 +244,20 @@ def how_many_data_to_use_to_select_sample_roi(self): init_value = 1 box1 = widgets.HBox( [ - widgets.Label("Nbr of images to use:", layout=widgets.Layout(width="15")), - widgets.IntSlider(value=init_value, max=nbr_images, min=1, layout=widgets.Layout(width="50%")), + widgets.Label( + "Nbr of images to use:", layout=widgets.Layout(width="15") + ), + widgets.IntSlider( + value=init_value, + max=nbr_images, + min=1, + layout=widgets.Layout(width="50%"), + ), ] ) - box2 = widgets.Label("(The more you select, the longer it will take to display the preview!)") + box2 = widgets.Label( + "(The more you select, the longer it will take to display the preview!)" + ) vbox = widgets.VBox([box1, box2]) display(vbox) self.number_of_data_to_use_ui = box1.children[1] @@ -221,7 +265,9 @@ def how_many_data_to_use_to_select_sample_roi(self): def define_sample_roi(self): nbr_data_to_use = int(self.number_of_data_to_use_ui.value) nbr_images = len(self.data) - list_of_indexes_to_keep = random.sample(list(range(nbr_images)), nbr_data_to_use) + list_of_indexes_to_keep = random.sample( + list(range(nbr_images)), nbr_data_to_use + ) final_array = [] for _index in list_of_indexes_to_keep: final_array.append(self.data[_index]) @@ -231,7 +277,9 @@ def define_sample_roi(self): def define_integrated_sample_to_use(self): nbr_data_to_use = int(self.number_of_data_to_use_ui.value) nbr_images = len(self.data) - list_of_indexes_to_keep = random.sample(list(range(nbr_images)), nbr_data_to_use) + list_of_indexes_to_keep = random.sample( + list(range(nbr_images)), nbr_data_to_use + ) final_array = [] for _index in list_of_indexes_to_keep: final_array.append(self.data[_index]) @@ -271,7 +319,9 @@ def plot(self): _hkl_string.append(_hkl_s) _hkl_formated[_material] = _hkl_string - trace = go.Scatter(x=self.lambda_array, y=self.counts_vs_file_index, mode="markers") + trace = go.Scatter( + x=self.lambda_array, y=self.counts_vs_file_index, mode="markers" + ) layout = go.Layout( width=1000, @@ -349,9 +399,17 @@ def export_table(self, output_folder): output_file_name = os.path.join(output_folder, f"bragg_edges_of_{material}.txt") - file_handler.make_ascii_file(metadata=metadata, data=data, dim="1d", output_file_name=output_file_name) + file_handler.make_ascii_file( + metadata=metadata, data=data, dim="1d", output_file_name=output_file_name + ) - display(HTML('File created : ' + output_file_name + "")) + display( + HTML( + 'File created : ' + + output_file_name + + "" + ) + ) def select_folder(self, message="", next_function=None): folder_widget = fileselector.FileSelectorPanel( @@ -390,7 +448,8 @@ def __init__(self, parent=None, data=None, instruction="", next=None): super(QMainWindow, self).__init__(parent) ui_full_path = os.path.join( - os.path.dirname(os.path.dirname(os.path.dirname(__file__))), os.path.join("ui", "ui_roi_selection.ui") + os.path.dirname(os.path.dirname(os.path.dirname(__file__))), + os.path.join("ui", "ui_roi_selection.ui"), ) self.ui = load_ui(ui_full_path, baseinstance=self) @@ -647,7 +706,9 @@ def roi_manually_moved(self): _roi = list_roi[_row] roi_id = _roi["id"] - region = roi_id.getArraySlice(self.integrated_image, self.ui.image_view.imageItem) + region = roi_id.getArraySlice( + self.integrated_image, self.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop @@ -721,7 +782,9 @@ def add_roi_button_clicked(self): width_int = np.abs(x0_int - int(_x1)) height_int = np.abs(y0_int - int(_y1)) - _roi_id = self.init_roi(x0=x0_int, y0=y0_int, width=width_int, height=height_int) + _roi_id = self.init_roi( + x0=x0_int, y0=y0_int, width=width_int, height=height_int + ) _roi["id"] = _roi_id list_roi[_row] = _roi diff --git a/notebooks/__code/bragg_edge/peak_fitting_evaluation/bragg_edge_normalization.py b/notebooks/__code/bragg_edge/peak_fitting_evaluation/bragg_edge_normalization.py index ecda5a9fc..6d513d2ca 100755 --- a/notebooks/__code/bragg_edge/peak_fitting_evaluation/bragg_edge_normalization.py +++ b/notebooks/__code/bragg_edge/peak_fitting_evaluation/bragg_edge_normalization.py @@ -15,7 +15,9 @@ from __code import file_handler, ipywe from __code.bragg_edge.bragg_edge import Interface -from __code.bragg_edge.peak_fitting_evaluation.bragg_edge import BraggEdge as BraggEdgeParent +from __code.bragg_edge.peak_fitting_evaluation.bragg_edge import ( + BraggEdge as BraggEdgeParent, +) from __code.file_folder_browser import FileFolderBrowser @@ -65,7 +67,9 @@ def select_time_spectra_file(self): self.time_spectra_ui.show() self.cancel_button = widgets.Button( - description="or Do Not Select any Time Spectra", button_style="info", layout=widgets.Layout(width="100%") + description="or Do Not Select any Time Spectra", + button_style="info", + layout=widgets.Layout(width="100%"), ) display(self.cancel_button) self.cancel_button.on_click(self.cancel_time_spectra_selection) @@ -79,7 +83,11 @@ def cancel_time_spectra_selection(self, value): logging.info("User cancel loading time spectra!") self.time_spectra_ui.remove() self.cancel_button.close() - display(HTML('NO Spectra File loaded! ')) + display( + HTML( + 'NO Spectra File loaded! ' + ) + ) def load_files(self, data_type="sample", folder=None): self.starting_dir = os.path.dirname(folder) @@ -132,7 +140,9 @@ def normalization_settings_widgets(self): ## button self.select_ob_widget = widgets.Button( - description="Select OB ...", button_style="success", layout=widgets.Layout(width="100%") + description="Select OB ...", + button_style="success", + layout=widgets.Layout(width="100%"), ) self.select_ob_widget.on_click(self.select_ob_folder) @@ -140,15 +150,23 @@ def normalization_settings_widgets(self): spacer = widgets.HTML(value="
") ## nbr of images to use - nbr_images_to_use_label = widgets.Label("Nbr of images to use in preview", layout=widgets.Layout(width="20%")) + nbr_images_to_use_label = widgets.Label( + "Nbr of images to use in preview", layout=widgets.Layout(width="20%") + ) nbr_of_images_to_use_in_preview = self.get_nbr_of_images_to_use_in_preview() self.nbr_images_slider_with_ob = widgets.IntSlider( - min=2, max=len(self.list_files), value=nbr_of_images_to_use_in_preview, layout=widgets.Layout(width="80%") + min=2, + max=len(self.list_files), + value=nbr_of_images_to_use_in_preview, + layout=widgets.Layout(width="80%"), ) hbox_1 = widgets.HBox([nbr_images_to_use_label, self.nbr_images_slider_with_ob]) self.select_roi_widget_with_ob = widgets.Button( - description="OPTIONAL: Select Region of interest away from " "sample " "to " "improve normalization", + description="OPTIONAL: Select Region of interest away from " + "sample " + "to " + "improve normalization", layout=widgets.Layout(width="100%"), ) self.select_roi_widget_with_ob.on_click(self.select_roi_with_ob) @@ -166,18 +184,25 @@ def normalization_settings_widgets(self): ## nbr of images to use self.nbr_images_slider_without_ob = widgets.IntSlider( - min=2, max=len(self.list_files), value=nbr_of_images_to_use_in_preview, layout=widgets.Layout(width="80%") + min=2, + max=len(self.list_files), + value=nbr_of_images_to_use_in_preview, + layout=widgets.Layout(width="80%"), + ) + hbox_without_ob = widgets.HBox( + [nbr_images_to_use_label, self.nbr_images_slider_without_ob] ) - hbox_without_ob = widgets.HBox([nbr_images_to_use_label, self.nbr_images_slider_without_ob]) select_roi_widget_without_ob = widgets.Button( - description="MANDATORY: Select region of interest " "away from " "sample", + description="MANDATORY: Select region of interest away from sample", button_style="success", layout=widgets.Layout(width="100%"), ) select_roi_widget_without_ob.on_click(self.select_roi_without_ob) - vbox_without_ob = widgets.VBox([hbox_without_ob, spacer, select_roi_widget_without_ob]) + vbox_without_ob = widgets.VBox( + [hbox_without_ob, spacer, select_roi_widget_without_ob] + ) self.accordion = widgets.Accordion(children=[vbox_with_ob, vbox_without_ob]) self.accordion.set_title(0, "With OB") @@ -230,17 +255,37 @@ def check_data_array_sizes(self): len_sample = len(self.o_norm.data["sample"]["file_name"]) if len_ob == len_sample: - display(HTML(' Sample and OB have the same size!')) + display( + HTML( + ' Sample and OB have the same size!' + ) + ) return if len_ob < len_sample: - self.o_norm.data["sample"]["data"] = self.o_norm.data["sample"]["data"][0:len_ob] - self.o_norm.data["sample"]["file_name"] = self.o_norm.data["sample"]["file_name"][0:len_ob] - display(HTML(' Truncated Sample array to match OB!')) + self.o_norm.data["sample"]["data"] = self.o_norm.data["sample"]["data"][ + 0:len_ob + ] + self.o_norm.data["sample"]["file_name"] = self.o_norm.data["sample"][ + "file_name" + ][0:len_ob] + display( + HTML( + ' Truncated Sample array to match OB!' + ) + ) else: - self.o_norm.data["ob"]["data"] = self.o_norm.data["ob"]["data"][0:len_sample] - self.o_norm.data["ob"]["file_name"] = self.o_norm.data["ob"]["file_name"][0:len_sample] - display(HTML(' Truncated OB array to match Sample!')) + self.o_norm.data["ob"]["data"] = self.o_norm.data["ob"]["data"][ + 0:len_sample + ] + self.o_norm.data["ob"]["file_name"] = self.o_norm.data["ob"]["file_name"][ + 0:len_sample + ] + display( + HTML( + ' Truncated OB array to match Sample!' + ) + ) def load_time_spectra(self): _tof_handler = TOF(filename=self.spectra_file) @@ -262,12 +307,16 @@ def how_many_data_to_use_to_select_sample_roi(self): init_value = 1 box1 = widgets.HBox( [ - widgets.Label("Nbr of images to use:", layout=widgets.Layout(width="15")), + widgets.Label( + "Nbr of images to use:", layout=widgets.Layout(width="15") + ), widgets.IntSlider(value=init_value, max=nbr_images, min=1), ] ) # layout=widgets.Layout(width='50%'))]) - box2 = widgets.Label("(The more you select, the longer it will take to display the preview!)") + box2 = widgets.Label( + "(The more you select, the longer it will take to display the preview!)" + ) vbox = widgets.VBox([box1, box2]) display(vbox) self.number_of_data_to_use_ui = box1.children[1] @@ -276,7 +325,9 @@ def get_image_to_use_for_display(self, nbr_data_to_use=2): _data = self.o_norm.data["sample"]["data"] nbr_images = len(_data) - list_of_indexes_to_keep = random.sample(list(range(nbr_images)), nbr_data_to_use) + list_of_indexes_to_keep = random.sample( + list(range(nbr_images)), nbr_data_to_use + ) final_array = [] for _index in list_of_indexes_to_keep: @@ -304,8 +355,14 @@ def normalization(self): def normalization_without_ob(self, list_rois): logging.info("Running normalization without OB") if list_rois is None: - logging.info("-> no ROIs found! At least one ROI must be provided. Normalization Aborted!") - display(HTML(' You need to provide a ROI!')) + logging.info( + "-> no ROIs found! At least one ROI must be provided. Normalization Aborted!" + ) + display( + HTML( + ' You need to provide a ROI!' + ) + ) return else: @@ -320,8 +377,14 @@ def normalization_without_ob(self, list_rois): list_o_roi.append(ROI(x0=_x0, y0=_y0, x1=_x1, y1=_y1)) logging.info(f"-> Normalization with {len(list_o_roi)} ROIs") - self.o_norm.normalization(roi=list_o_roi, use_only_sample=True, notebook=True) - display(HTML(' Normalization DONE! ')) + self.o_norm.normalization( + roi=list_o_roi, use_only_sample=True, notebook=True + ) + display( + HTML( + ' Normalization DONE! ' + ) + ) logging.info("-> Done!") def normalization_with_ob(self, list_rois): @@ -342,21 +405,31 @@ def normalization_with_ob(self, list_rois): logging.info(f"-> Normalization with {len(list_o_roi)} ROIs") self.o_norm.normalization(roi=list_o_roi, notebook=True) - display(HTML(' Normalization DONE! ')) + display( + HTML( + ' Normalization DONE! ' + ) + ) logging.info("-> Done!") def export_normalized_data(self): self.o_folder = FileFolderBrowser( - working_dir=self.working_dir, next_function=self.export_normalized_data_step2, ipts_folder=self.ipts_folder + working_dir=self.working_dir, + next_function=self.export_normalized_data_step2, + ipts_folder=self.ipts_folder, + ) + self.o_folder.select_output_folder_with_new( + instruction="Select where to create the normalized data ..." ) - self.o_folder.select_output_folder_with_new(instruction="Select where to create the normalized data ...") def export_normalized_data_step2(self, output_folder): logging.info("export normalized data") logging.info(f"-> output_folder: {output_folder}") output_folder = os.path.abspath(output_folder) self.o_folder.list_output_folders_ui.shortcut_buttons.close() - normalized_export_folder = str(Path(output_folder) / (self.data_folder_name + "_normalized")) + normalized_export_folder = str( + Path(output_folder) / (self.data_folder_name + "_normalized") + ) file_handler.make_or_reset_folder(normalized_export_folder) self.o_norm.export(folder=normalized_export_folder) @@ -369,8 +442,14 @@ def export_normalized_data_step2(self, output_folder): ) if self.spectra_file: logging.info("-> time spectra copied to output folder!") - file_handler.copy_files_to_folder(list_files=[self.spectra_file], output_folder=normalized_export_folder) - display(HTML(' Copied time spectra file to same folder ')) + file_handler.copy_files_to_folder( + list_files=[self.spectra_file], output_folder=normalized_export_folder + ) + display( + HTML( + ' Copied time spectra file to same folder ' + ) + ) else: logging.info("->No time spectra copied!") @@ -381,8 +460,12 @@ def calculate_counts_vs_file_index_of_regions_selected(self, list_roi=None): nbr_data = len(data) box_ui = widgets.HBox( [ - widgets.Label("Calculate Counts vs lambda", layout=widgets.Layout(width="20%")), - widgets.IntProgress(min=0, max=nbr_data, value=0, layout=widgets.Layout(width="50%")), + widgets.Label( + "Calculate Counts vs lambda", layout=widgets.Layout(width="20%") + ), + widgets.IntProgress( + min=0, max=nbr_data, value=0, layout=widgets.Layout(width="50%") + ), ] ) progress_bar = box_ui.children[1] @@ -411,7 +494,9 @@ def calculate_counts_vs_file_index_of_regions_selected(self, list_roi=None): box_ui.close() def plot(self): - trace = go.Scatter(x=self.lambda_array, y=self.counts_vs_file_index, mode="markers") + trace = go.Scatter( + x=self.lambda_array, y=self.counts_vs_file_index, mode="markers" + ) layout = go.Layout( height=500, @@ -426,8 +511,12 @@ def plot(self): iplot(figure) def select_output_data_folder(self): - o_folder = FileFolderBrowser(working_dir=self.working_dir, next_function=self.export_data) - o_folder.select_output_folder(instruction="Select where to create the ascii file...") + o_folder = FileFolderBrowser( + working_dir=self.working_dir, next_function=self.export_data + ) + o_folder.select_output_folder( + instruction="Select where to create the ascii file..." + ) def make_output_file_name(self, output_folder="", input_folder=""): file_name = os.path.basename(input_folder) + "_counts_vs_lambda_tof.txt" @@ -435,7 +524,9 @@ def make_output_file_name(self, output_folder="", input_folder=""): def export_data(self, output_folder): input_folder = os.path.dirname(self.o_norm.data["sample"]["file_name"][0]) - output_file_name = self.make_output_file_name(output_folder=output_folder, input_folder=input_folder) + output_file_name = self.make_output_file_name( + output_folder=output_folder, input_folder=input_folder + ) lambda_array = self.lambda_array counts_vs_file_index = self.counts_vs_file_index @@ -453,15 +544,21 @@ def export_data(self, output_folder): _y0 = roi["y0"] _x1 = roi["x1"] _y1 = roi["y1"] - metadata.append(f"# ROI {index}: x0={_x0}, y0={_y0}, x1={_x1}, y1={_y1}") + metadata.append( + f"# ROI {index}: x0={_x0}, y0={_y0}, x1={_x1}, y1={_y1}" + ) metadata.append("#") metadata.append("# tof (micros), lambda (Angstroms), Average transmission") data = [] - for _t, _l, _c in zip(tof_array, lambda_array, counts_vs_file_index, strict=False): + for _t, _l, _c in zip( + tof_array, lambda_array, counts_vs_file_index, strict=False + ): data.append(f"{_t}, {_l}, {_c}") - file_handler.make_ascii_file(metadata=metadata, data=data, output_file_name=output_file_name, dim="1d") + file_handler.make_ascii_file( + metadata=metadata, data=data, output_file_name=output_file_name, dim="1d" + ) if os.path.exists(output_file_name): display( @@ -482,7 +579,9 @@ def export_data(self, output_folder): ) def select_output_table_folder(self): - o_folder = FileFolderBrowser(working_dir=self.working_dir, next_function=self.export_table) + o_folder = FileFolderBrowser( + working_dir=self.working_dir, next_function=self.export_table + ) o_folder.select_output_folder() def export_table(self, output_folder): @@ -509,9 +608,17 @@ def export_table(self, output_folder): output_file_name = os.path.join(output_folder, f"bragg_edges_of_{material}.txt") - file_handler.make_ascii_file(metadata=metadata, data=data, dim="1d", output_file_name=output_file_name) + file_handler.make_ascii_file( + metadata=metadata, data=data, dim="1d", output_file_name=output_file_name + ) - display(HTML('File created : ' + output_file_name + "")) + display( + HTML( + 'File created : ' + + output_file_name + + "" + ) + ) def select_folder(self, message="", next_function=None): folder_widget = ipywe.fileselector.FileSelectorPanel( diff --git a/notebooks/__code/bragg_edge/peak_fitting_evaluation/bragg_edge_peak_fitting.py b/notebooks/__code/bragg_edge/peak_fitting_evaluation/bragg_edge_peak_fitting.py index 897c52792..21873553d 100755 --- a/notebooks/__code/bragg_edge/peak_fitting_evaluation/bragg_edge_peak_fitting.py +++ b/notebooks/__code/bragg_edge/peak_fitting_evaluation/bragg_edge_peak_fitting.py @@ -16,14 +16,24 @@ from __code.bragg_edge.bragg_edge_peak_fitting_gui_utility import GuiUtility from __code.bragg_edge.export_handler import ExportHandler from __code.bragg_edge.import_handler import ImportHandler -from __code.bragg_edge.peak_fitting_evaluation.bragg_edge_normalization import BraggEdge as BraggEdgeParent -from __code.bragg_edge.peak_fitting_evaluation.bragg_edge_selection_tab import BraggEdgeSelectionTab +from __code.bragg_edge.peak_fitting_evaluation.bragg_edge_normalization import ( + BraggEdge as BraggEdgeParent, +) +from __code.bragg_edge.peak_fitting_evaluation.bragg_edge_selection_tab import ( + BraggEdgeSelectionTab, +) from __code.bragg_edge.peak_fitting_evaluation.get import Get, GetInfo from __code.bragg_edge.peak_fitting_evaluation.kropff import Kropff -from __code.bragg_edge.peak_fitting_evaluation.kropff_fitting_job_handler import KropffFittingJobHandler +from __code.bragg_edge.peak_fitting_evaluation.kropff_fitting_job_handler import ( + KropffFittingJobHandler, +) from __code.bragg_edge.peak_fitting_evaluation.march_dollase import MarchDollase -from __code.bragg_edge.peak_fitting_evaluation.march_dollase_fitting_job_handler import MarchDollaseFittingJobHandler -from __code.bragg_edge.peak_fitting_evaluation.peak_fitting_interface_initialization import Initialization +from __code.bragg_edge.peak_fitting_evaluation.march_dollase_fitting_job_handler import ( + MarchDollaseFittingJobHandler, +) +from __code.bragg_edge.peak_fitting_evaluation.peak_fitting_interface_initialization import ( + Initialization, +) from __code.bragg_edge.peak_fitting_initialization import PeakFittingInitialization from __code.table_handler import TableHandler from __code.utilities import find_nearest_index @@ -44,7 +54,15 @@ def divide_normalized_data_by_OB_white_beam(self): class Interface(QMainWindow): fitting_parameters_init = { - "kropff": {"a0": 1, "b0": 1, "ahkl": 1, "bhkl": 1, "ldahkl": 1e-8, "tau": 1, "sigma": [1e-7, 1e-6, 1e-5]} + "kropff": { + "a0": 1, + "b0": 1, + "ahkl": 1, + "bhkl": 1, + "ldahkl": 1e-8, + "tau": 1, + "sigma": [1e-7, 1e-6, 1e-5], + } } bragg_edge_range = [5, 20] @@ -54,13 +72,17 @@ class Interface(QMainWindow): selection_roi_rgb = (62, 13, 244) roi_settings = { - "color": QtGui.QColor(selection_roi_rgb[0], selection_roi_rgb[1], selection_roi_rgb[2]), + "color": QtGui.QColor( + selection_roi_rgb[0], selection_roi_rgb[1], selection_roi_rgb[2] + ), "width": 0.01, "position": [10, 10], } shrinking_roi_rgb = (13, 214, 244) shrinking_roi_settings = { - "color": QtGui.QColor(shrinking_roi_rgb[0], shrinking_roi_rgb[1], shrinking_roi_rgb[2]), + "color": QtGui.QColor( + shrinking_roi_rgb[0], shrinking_roi_rgb[1], shrinking_roi_rgb[2] + ), "width": 0.01, "dashes_pattern": [4, 2], } @@ -71,7 +93,11 @@ class Interface(QMainWindow): previous_roi_selection = {"width": None, "height": None} image_size = {"width": None, "height": None} roi_id = None - xaxis_label = {"index": "File index", "tof": "TOF (\u00b5s)", "lambda": "\u03bb (\u212b)"} + xaxis_label = { + "index": "File index", + "tof": "TOF (\u00b5s)", + "lambda": "\u03bb (\u212b)", + } fitting_rois = { "kropff": { "step1": None, @@ -83,7 +109,11 @@ class Interface(QMainWindow): is_file_imported = False # True only when the import button has been used bragg_edge_range_ui = None - kropff_fitting_range = {"high": [None, None], "low": [None, None], "bragg_peak": [None, None]} + kropff_fitting_range = { + "high": [None, None], + "low": [None, None], + "bragg_peak": [None, None], + } fitting_peak_ui = None # vertical line in fitting view (tab 2) fitting_procedure_started = {"march-dollase": False, "kropff": False} @@ -223,7 +253,11 @@ def __init__(self, parent=None, working_dir="", o_bragg=None, spectra_file=None) ) super(Interface, self).__init__(parent) - ui_full_path = Path(__file__).parent.parent.parent.parent / "ui" / "ui_bragg_edge_peak_fitting.ui" + ui_full_path = ( + Path(__file__).parent.parent.parent.parent + / "ui" + / "ui_bragg_edge_peak_fitting.ui" + ) self.ui = load_ui(ui_full_path, baseinstance=self) self.setWindowTitle("Peak Fitting Tool") @@ -262,7 +296,9 @@ def update_time_spectra(self): distance_source_detector_m = float(self.ui.distance_detector_sample.text()) self.ui.statusbar.showMessage("", 100) # 10s except ValueError: - self.ui.statusbar.showMessage("distance source detector input is WRONG", 120000) # 2mn + self.ui.statusbar.showMessage( + "distance source detector input is WRONG", 120000 + ) # 2mn self.ui.statusbar.setStyleSheet("color: red") return @@ -270,7 +306,9 @@ def update_time_spectra(self): detector_offset_micros = float(self.ui.detector_offset.text()) self.ui.statusbar.showMessage("", 100) # 10s except ValueError: - self.ui.statusbar.showMessage("detector offset input is WRONG", 120000) # 2mn + self.ui.statusbar.showMessage( + "detector offset input is WRONG", 120000 + ) # 2mn self.ui.statusbar.setStyleSheet("color: red") return @@ -290,7 +328,9 @@ def get_live_image(self): _data = self.o_norm.data["sample"]["data"] nbr_images = len(_data) - list_of_indexes_to_keep = random.sample(list(range(nbr_images)), nbr_data_to_use) + list_of_indexes_to_keep = random.sample( + list(range(nbr_images)), nbr_data_to_use + ) final_array = [] for _index in list_of_indexes_to_keep: @@ -340,7 +380,10 @@ def bragg_edge_range_changed(self): def reset_profile_of_bin_size_slider(self): max_value = np.min( - [int(str(self.ui.profile_of_bin_size_width.text())), int(str(self.ui.profile_of_bin_size_height.text()))] + [ + int(str(self.ui.profile_of_bin_size_width.text())), + int(str(self.ui.profile_of_bin_size_height.text())), + ] ) self.ui.profile_of_bin_size_slider.setMaximum(max_value) self.ui.profile_of_bin_size_slider.setValue(max_value) @@ -395,7 +438,11 @@ def update_dict_profile_to_fit(self): profile_to_fit = { "yaxis": yaxis, - "xaxis": {"index": index_selected, "tof": tof_selected, "lambda": lambda_selected}, + "xaxis": { + "index": index_selected, + "tof": tof_selected, + "lambda": lambda_selected, + }, } self.dict_profile_to_fit = profile_to_fit @@ -407,12 +454,17 @@ def fit_that_selection_pushed_by_program(self, initialize_region=True): dict_regions = o_get.all_russian_doll_region_full_infos() o_init = PeakFittingInitialization(parent=self) - fitting_input_dictionary = o_init.fitting_input_dictionary(nbr_rois=len(dict_regions)) + fitting_input_dictionary = o_init.fitting_input_dictionary( + nbr_rois=len(dict_regions) + ) o_init.set_top_keys_values( - fitting_input_dictionary, {"xaxis": x_axis, "bragg_edge_range": self.bragg_edge_range} + fitting_input_dictionary, + {"xaxis": x_axis, "bragg_edge_range": self.bragg_edge_range}, + ) + self.append_dict_regions_to_fitting_input_dictionary( + dict_regions, fitting_input_dictionary ) - self.append_dict_regions_to_fitting_input_dictionary(dict_regions, fitting_input_dictionary) # fitting_input_dictionary['xaxis'] = x_axis # fitting_input_dictionary['bragg_edge_range'] = self.bragg_edge_range @@ -440,7 +492,9 @@ def fit_that_selection_pushed_by_program(self, initialize_region=True): self.ui.actionExport.setEnabled(True) self.select_first_row_of_all_fitting_table() - def append_dict_regions_to_fitting_input_dictionary(self, dict_regions, fitting_input_dictionary): + def append_dict_regions_to_fitting_input_dictionary( + self, dict_regions, fitting_input_dictionary + ): for _row in dict_regions.keys(): _entry = dict_regions[_row] for _key in _entry.keys(): @@ -589,18 +643,27 @@ def update_profile_of_bin_slider_labels(self): def change_profile_of_bin_slider_signal(self): self.ui.profile_of_bin_size_slider.valueChanged.disconnect() - self.ui.profile_of_bin_size_slider.valueChanged.connect(self.profile_of_bin_size_slider_changed_after_import) + self.ui.profile_of_bin_size_slider.valueChanged.connect( + self.profile_of_bin_size_slider_changed_after_import + ) def update_vertical_line_in_profile_plot(self): o_get = Get(parent=self) x_axis, x_axis_label = o_get.x_axis() - bragg_edge_range = [x_axis[self.bragg_edge_range[0]], x_axis[self.bragg_edge_range[1]]] + bragg_edge_range = [ + x_axis[self.bragg_edge_range[0]], + x_axis[self.bragg_edge_range[1]], + ] if self.bragg_edge_range_ui: self.ui.profile.removeItem(self.bragg_edge_range_ui) self.bragg_edge_range_ui = pg.LinearRegionItem( - values=bragg_edge_range, orientation=None, brush=None, movable=True, bounds=None + values=bragg_edge_range, + orientation=None, + brush=None, + movable=True, + bounds=None, ) self.bragg_edge_range_ui.sigRegionChanged.connect(self.bragg_edge_range_changed) self.bragg_edge_range_ui.setZValue(-10) @@ -651,7 +714,12 @@ def export_button_clicked(self): def roi_radiobuttons_changed(self): if self.ui.square_roi_radiobutton.isChecked(): slider_visible = True - new_width = np.min([int(str(self.ui.roi_width.text())), int(str(self.ui.roi_height.text()))]) + new_width = np.min( + [ + int(str(self.ui.roi_width.text())), + int(str(self.ui.roi_height.text())), + ] + ) mode = "square" else: slider_visible = False @@ -724,30 +792,34 @@ def update_kropff_fit_table_graph(self, fit_region="high"): :param fit_region: 'high', 'low' or 'bragg_peak' """ o_gui = GuiUtility(parent=self) - fit_parameter_selected = o_gui.get_kropff_fit_parameter_selected(fit_region=fit_region) + fit_parameter_selected = o_gui.get_kropff_fit_parameter_selected( + fit_region=fit_region + ) parameter_array = [] parameter_error_array = [] fitting_input_dictionary = self.fitting_input_dictionary for _index in fitting_input_dictionary["rois"].keys(): - _parameter = fitting_input_dictionary["rois"][_index]["fitting"]["kropff"][fit_region][ - fit_parameter_selected - ] - _error = fitting_input_dictionary["rois"][_index]["fitting"]["kropff"][fit_region][ - f"{fit_parameter_selected}_error" - ] + _parameter = fitting_input_dictionary["rois"][_index]["fitting"]["kropff"][ + fit_region + ][fit_parameter_selected] + _error = fitting_input_dictionary["rois"][_index]["fitting"]["kropff"][ + fit_region + ][f"{fit_parameter_selected}_error"] parameter_array.append(_parameter) parameter_error_array.append(_error) plot_ui = o_gui.get_kropff_fit_graph_ui(fit_region=fit_region) x_array = np.arange(len(parameter_array)) - cleaned_parameter_array, cleaned_parameter_error_array = exclude_y_value_when_error_is_nan( - parameter_array, parameter_error_array + cleaned_parameter_array, cleaned_parameter_error_array = ( + exclude_y_value_when_error_is_nan(parameter_array, parameter_error_array) ) plot_ui.axes.cla() if fit_region == "bragg_peak": plot_ui.axes.set_yscale("log") - plot_ui.axes.errorbar(x_array, cleaned_parameter_array, cleaned_parameter_error_array, marker="s") + plot_ui.axes.errorbar( + x_array, cleaned_parameter_array, cleaned_parameter_error_array, marker="s" + ) plot_ui.axes.set_xlabel("Row # (see Table tab)") plot_ui.draw() @@ -783,14 +855,18 @@ def kropff_bragg_peak_right_click(self, position): def march_dollase_table_state_changed(self, state=None, row=None, column=None): o_march = MarchDollase(parent=self) if row == 0: - _widget = self.ui.march_dollase_user_input_table.cellWidget(row, column).children()[-1] + _widget = self.ui.march_dollase_user_input_table.cellWidget( + row, column + ).children()[-1] if (column == 1) or (column == 2): _textedit = _widget _textedit.setText(o_march.get_initial_parameter_value(column=column)) _textedit.setVisible(not state) elif column == 0: _label = _widget - _label.setText(f"{float(o_march.get_initial_parameter_value(column=column)):0.6f}") + _label.setText( + f"{float(o_march.get_initial_parameter_value(column=column)):0.6f}" + ) _label.setVisible(not state) else: _label = _widget diff --git a/notebooks/__code/bragg_edge/peak_fitting_evaluation/bragg_edge_selection_tab.py b/notebooks/__code/bragg_edge/peak_fitting_evaluation/bragg_edge_selection_tab.py index 8c7ce58cb..c384653d2 100755 --- a/notebooks/__code/bragg_edge/peak_fitting_evaluation/bragg_edge_selection_tab.py +++ b/notebooks/__code/bragg_edge/peak_fitting_evaluation/bragg_edge_selection_tab.py @@ -17,7 +17,9 @@ def update_all_size_widgets_infos(self): return roi_id = self.parent.roi_id - region = roi_id.getArraySlice(self.parent.final_image, self.parent.ui.image_view.imageItem) + region = roi_id.getArraySlice( + self.parent.final_image, self.parent.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop y0 = region[0][1].start @@ -54,7 +56,9 @@ def get_coordinates_of_new_inside_selection_box(self): height_requested = int(str(self.parent.ui.profile_of_bin_size_height.text())) # retrieve x0, y0, width and height of full selection - region = self.parent.roi_id.getArraySlice(self.parent.final_image, self.parent.ui.image_view.imageItem) + region = self.parent.roi_id.getArraySlice( + self.parent.final_image, self.parent.ui.image_view.imageItem + ) x0 = region[0][0].start y0 = region[0][1].start # [x0, y0] = self.parentselection_x0y0 @@ -121,12 +125,21 @@ def update_selection_profile_plot(self): self.parent.ui.profile.setLabel("left", "Mean transmission") # vertical line showing peak to fit - bragg_edge_range = [x_axis[self.parent.bragg_edge_range[0]], x_axis[self.parent.bragg_edge_range[1]]] + bragg_edge_range = [ + x_axis[self.parent.bragg_edge_range[0]], + x_axis[self.parent.bragg_edge_range[1]], + ] self.parent.bragg_edge_range_ui = pg.LinearRegionItem( - values=bragg_edge_range, orientation="vertical", brush=None, movable=True, bounds=None + values=bragg_edge_range, + orientation="vertical", + brush=None, + movable=True, + bounds=None, + ) + self.parent.bragg_edge_range_ui.sigRegionChanged.connect( + self.parent.bragg_edge_range_changed ) - self.parent.bragg_edge_range_ui.sigRegionChanged.connect(self.parent.bragg_edge_range_changed) self.parent.bragg_edge_range_ui.setZValue(-10) self.parent.ui.profile.addItem(self.parent.bragg_edge_range_ui) @@ -135,7 +148,9 @@ def update_selection(self, new_value=None, mode="square"): return try: - region = self.parent.roi_id.getArraySlice(self.parent.final_image, self.parent.ui.image_view.imageItem) + region = self.parent.roi_id.getArraySlice( + self.parent.final_image, self.parent.ui.image_view.imageItem + ) except TypeError: return @@ -149,7 +164,9 @@ def update_selection(self, new_value=None, mode="square"): _pen = QtGui.QPen() _pen.setColor(self.parent.roi_settings["color"]) _pen.setWidthF(self.parent.roi_settings["width"]) - self.parent.roi_id = pg.ROI([x0, y0], [new_value, new_value], pen=_pen, scaleSnap=True) + self.parent.roi_id = pg.ROI( + [x0, y0], [new_value, new_value], pen=_pen, scaleSnap=True + ) self.parent.ui.image_view.addItem(self.parent.roi_id) self.parent.roi_id.sigRegionChanged.connect(self.parent.roi_moved) @@ -177,7 +194,11 @@ def update_selection_plot(self): self.parent.ui.profile.plot( x_axis, y_axis, - pen=(self.parent.shrinking_roi_rgb[0], self.parent.shrinking_roi_rgb[1], self.parent.shrinking_roi_rgb[2]), + pen=( + self.parent.shrinking_roi_rgb[0], + self.parent.shrinking_roi_rgb[1], + self.parent.shrinking_roi_rgb[2], + ), ) self.parent.ui.profile.setLabel("bottom", x_axis_label) self.parent.ui.profile.setLabel("left", "Mean transmission") @@ -187,7 +208,11 @@ def update_selection_plot(self): self.parent.ui.profile.plot( x_axis, y_axis, - pen=(self.parent.selection_roi_rgb[0], self.parent.selection_roi_rgb[1], self.parent.selection_roi_rgb[2]), + pen=( + self.parent.selection_roi_rgb[0], + self.parent.selection_roi_rgb[1], + self.parent.selection_roi_rgb[2], + ), ) def profile_of_bin_size_slider_changed(self, new_value): @@ -236,7 +261,9 @@ def update_roi_defined_by_profile_of_bin_size_slider(self): _pen.setColor(self.parent.shrinking_roi_settings["color"]) _pen.setWidthF(self.parent.shrinking_roi_settings["width"]) - self.parent.shrinking_roi_id = pg.ROI([x0, y0], [width, height], pen=_pen, scaleSnap=True, movable=False) + self.parent.shrinking_roi_id = pg.ROI( + [x0, y0], [width, height], pen=_pen, scaleSnap=True, movable=False + ) self.parent.ui.image_view.addItem(self.parent.shrinking_roi_id) def update_profile_of_bin_slider_widget(self): @@ -250,7 +277,9 @@ def update_profile_of_bin_slider_widget(self): "height": fitting_input_dictionary["rois"][_key]["height"], } self.parent.dict_rois_imported = dict_rois_imported - self.parent.ui.profile_of_bin_size_slider.setRange(0, len(dict_rois_imported) - 1) + self.parent.ui.profile_of_bin_size_slider.setRange( + 0, len(dict_rois_imported) - 1 + ) # self.parent.ui.profile_of_bin_size_slider.setMinimum(0) # self.parent.ui.profile_of_bin_size_slider.setMaximum(len(dict_rois_imported)-1) self.parent.ui.profile_of_bin_size_slider.setSingleStep(1) diff --git a/notebooks/__code/bragg_edge/peak_fitting_evaluation/get.py b/notebooks/__code/bragg_edge/peak_fitting_evaluation/get.py index cc715ba7a..6474d2f95 100755 --- a/notebooks/__code/bragg_edge/peak_fitting_evaluation/get.py +++ b/notebooks/__code/bragg_edge/peak_fitting_evaluation/get.py @@ -86,7 +86,9 @@ def all_russian_doll_region_full_infos(self): # collect initial selection size (x0, y0, width, height) [x0, y0, x1, y1, width, height] = self.selection_roi_dimension() # create profile for all the fitting region inside that first box - o_regions = SelectionRegionUtilities(x0=x0, y0=y0, width=width, height=height) + o_regions = SelectionRegionUtilities( + x0=x0, y0=y0, width=width, height=height + ) dict_regions = o_regions.get_all_russian_doll_regions() self.parent.add_profile_to_dict_of_all_regions(dict_regions=dict_regions) return dict_regions @@ -97,7 +99,9 @@ def selection_roi_dimension(self): x0, y0, x1, y1, width, height = None, None, None, None, None, None if roi_id: - region = roi_id.getArraySlice(self.parent.final_image, self.parent.ui.image_view.imageItem) + region = roi_id.getArraySlice( + self.parent.final_image, self.parent.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop y0 = region[0][1].start @@ -110,7 +114,9 @@ def selection_roi_dimension(self): return [x0, y0, x1, y1, width, height] - def profile_of_roi(self, x0=None, y0=None, x1=None, y1=None, width=None, height=None): + def profile_of_roi( + self, x0=None, y0=None, x1=None, y1=None, width=None, height=None + ): profile_value = [] if width: @@ -126,11 +132,17 @@ def profile_of_roi(self, x0=None, y0=None, x1=None, y1=None, width=None, height= def requested_xaxis(self, xaxis_label="index"): if xaxis_label == "index": - return self.parent.dict_profile_to_fit["xaxis"]["index"], self.parent.xaxis_label["index"] + return self.parent.dict_profile_to_fit["xaxis"][ + "index" + ], self.parent.xaxis_label["index"] elif xaxis_label == "tof": - return self.parent.dict_profile_to_fit["xaxis"]["tof"], self.parent.xaxis_label["tof"] + return self.parent.dict_profile_to_fit["xaxis"][ + "tof" + ], self.parent.xaxis_label["tof"] elif xaxis_label == "lambda": - return self.parent.dict_profile_to_fit["xaxis"]["lambda"], self.parent.xaxis_label["lambda"] + return self.parent.dict_profile_to_fit["xaxis"][ + "lambda" + ], self.parent.xaxis_label["lambda"] def fitting_profile_xaxis(self): if self.parent.ui.fitting_tof_radiobutton.isChecked(): @@ -152,7 +164,10 @@ def part_of_fitting_selected(self): page_index = self.parent.ui.kropff_toolBox.currentIndex() - return {"name_of_page": list_pages[page_index], "table_ui": list_table_ui[page_index]} + return { + "name_of_page": list_pages[page_index], + "table_ui": list_table_ui[page_index], + } def y_axis_data_of_selected_row(self, row_selected): selected_roi = self.parent.fitting_input_dictionary["rois"][row_selected] diff --git a/notebooks/__code/bragg_edge/peak_fitting_evaluation/kropff.py b/notebooks/__code/bragg_edge/peak_fitting_evaluation/kropff.py index 6e8b00454..e4ff32a0e 100755 --- a/notebooks/__code/bragg_edge/peak_fitting_evaluation/kropff.py +++ b/notebooks/__code/bragg_edge/peak_fitting_evaluation/kropff.py @@ -40,7 +40,9 @@ def reset_low_lambda_table(self): def reset_bragg_peak_table(self): self.clear_table(table_name="bragg_peak") - self.fill_table_with_minimum_contain(table_ui=self.parent.ui.bragg_edge_tableWidget) + self.fill_table_with_minimum_contain( + table_ui=self.parent.ui.bragg_edge_tableWidget + ) def clear_table(self, table_name="high_lambda", is_all=False): """remove all the rows of the table name specified, or all if is_all is True""" @@ -90,7 +92,9 @@ def fill_table_with_fitting_information(self): o_table = TableHandler(table_ui=self.table_ui["bragg_peak"]) _col = 1 for _row in fitting_input_dictionary["rois"].keys(): - _entry = fitting_input_dictionary["rois"][_row]["fitting"]["kropff"]["bragg_peak"] + _entry = fitting_input_dictionary["rois"][_row]["fitting"]["kropff"][ + "bragg_peak" + ] o_table.set_item_with_float(_row, _col, _entry["ldahkl"]) o_table.set_item_with_float(_row, _col + 1, _entry["tau"]) o_table.set_item_with_float(_row, _col + 2, _entry["sigma"]) @@ -123,8 +127,12 @@ def bragg_peak_right_click(self, position=None): def fit_bragg_peak_selected_rows(self): o_gui = GuiUtility(parent=self.parent) - list_rows_selected = o_gui.get_rows_of_table_selected(table_ui=self.parent.ui.bragg_edge_tableWidget) - self.parent.kropff_fit_bragg_peak_region_of_selected_rows(list_row_to_fit=list_rows_selected) + list_rows_selected = o_gui.get_rows_of_table_selected( + table_ui=self.parent.ui.bragg_edge_tableWidget + ) + self.parent.kropff_fit_bragg_peak_region_of_selected_rows( + list_row_to_fit=list_rows_selected + ) def export_bragg_peak_profile(self): working_dir = str(Path(self.parent.working_dir).parent) @@ -136,12 +144,16 @@ def export_bragg_peak_profile(self): if _export_folder: o_gui = GuiUtility(parent=self.parent) - list_row_selected = o_gui.get_rows_of_table_selected(table_ui=self.parent.ui.bragg_edge_tableWidget) + list_row_selected = o_gui.get_rows_of_table_selected( + table_ui=self.parent.ui.bragg_edge_tableWidget + ) for row_selected in list_row_selected: # make up output file name name_of_row = o_gui.get_table_str_item( - table_ui=self.parent.ui.bragg_edge_tableWidget, row=row_selected, column=0 + table_ui=self.parent.ui.bragg_edge_tableWidget, + row=row_selected, + column=0, ) [x0, y0, width, height] = name_of_row.split("; ") name_of_row_formatted = f"x0{x0}_y0{y0}_width{width}_height{height}" @@ -154,10 +166,18 @@ def export_bragg_peak_profile(self): x_axis = o_fit.xaxis_to_fit y_axis = o_fit.list_yaxis_to_fit[row_selected] - a0 = self.parent.fitting_input_dictionary["rois"][row_selected]["fitting"]["kropff"]["high"]["a0"] - b0 = self.parent.fitting_input_dictionary["rois"][row_selected]["fitting"]["kropff"]["high"]["b0"] - ahkl = self.parent.fitting_input_dictionary["rois"][row_selected]["fitting"]["kropff"]["low"]["ahkl"] - bhkl = self.parent.fitting_input_dictionary["rois"][row_selected]["fitting"]["kropff"]["low"]["bhkl"] + a0 = self.parent.fitting_input_dictionary["rois"][row_selected][ + "fitting" + ]["kropff"]["high"]["a0"] + b0 = self.parent.fitting_input_dictionary["rois"][row_selected][ + "fitting" + ]["kropff"]["high"]["b0"] + ahkl = self.parent.fitting_input_dictionary["rois"][row_selected][ + "fitting" + ]["kropff"]["low"]["ahkl"] + bhkl = self.parent.fitting_input_dictionary["rois"][row_selected][ + "fitting" + ]["kropff"]["low"]["bhkl"] metadata = [f"# Bragg peak fitting of row {row_selected + 1}"] metadata.append(f"# x0: {x0}") @@ -172,7 +192,10 @@ def export_bragg_peak_profile(self): metadata.append("# lambda (Angstroms), average transmission") make_ascii_file_from_2dim_array( - metadata=metadata, col1=x_axis, col2=y_axis, output_file_name=full_file_name + metadata=metadata, + col1=x_axis, + col2=y_axis, + output_file_name=full_file_name, ) message = f"Exported {len(list_row_selected)} file(s) in {_export_folder}" @@ -190,12 +213,19 @@ def select_all_rows_with_negative_thkl(self): list_of_rows_to_select = [] fitting_input_dictionary_rois = self.parent.fitting_input_dictionary["rois"] for _row in fitting_input_dictionary_rois.keys(): - _thkl = float(fitting_input_dictionary_rois[_row]["fitting"]["kropff"]["bragg_peak"]["ldahkl"]) + _thkl = float( + fitting_input_dictionary_rois[_row]["fitting"]["kropff"]["bragg_peak"][ + "ldahkl" + ] + ) if _thkl < 0: list_of_rows_to_select.append(_row) o_gui = GuiUtility(parent=self.parent) - o_gui.select_rows_of_table(table_ui=self.parent.ui.bragg_edge_tableWidget, list_of_rows=list_of_rows_to_select) + o_gui.select_rows_of_table( + table_ui=self.parent.ui.bragg_edge_tableWidget, + list_of_rows=list_of_rows_to_select, + ) def update_fitting_plot(self): self.parent.ui.fitting.clear() @@ -240,15 +270,23 @@ def update_fitting_plot(self): if self.parent.fitting_peak_ui: self.parent.ui.fitting.removeItem(self.parent.fitting_peak_ui) self.parent.fitting_peak_ui = pg.LinearRegionItem( - values=peak_range, orientation="vertical", brush=None, movable=True, bounds=None + values=peak_range, + orientation="vertical", + brush=None, + movable=True, + bounds=None, + ) + self.parent.fitting_peak_ui.sigRegionChanged.connect( + self.parent.fitting_range_changed ) - self.parent.fitting_peak_ui.sigRegionChanged.connect(self.parent.fitting_range_changed) self.parent.fitting_peak_ui.setZValue(-10) self.parent.ui.fitting.addItem(self.parent.fitting_peak_ui) else: for row_selected in list_row_selected: - selected_roi = self.parent.fitting_input_dictionary["rois"][row_selected] + selected_roi = self.parent.fitting_input_dictionary["rois"][ + row_selected + ] xaxis_dict = self.parent.fitting_input_dictionary["xaxis"] [left_xaxis_index, right_xaxis_index] = self.parent.bragg_edge_range @@ -260,7 +298,9 @@ def update_fitting_plot(self): yaxis = yaxis[left_xaxis_index:right_xaxis_index] self.parent.ui.fitting.setLabel("bottom", xaxis_label) - self.parent.ui.fitting.setLabel("left", "Cross Section (arbitrary units)") + self.parent.ui.fitting.setLabel( + "left", "Cross Section (arbitrary units)" + ) yaxis = -np.log(yaxis) self.parent.ui.fitting.plot( xaxis, @@ -277,15 +317,24 @@ def update_fitting_plot(self): if peak_range_index[0] is None: peak_range = self.parent.bragg_edge_range else: - peak_range = [xaxis[peak_range_index[0]], xaxis[peak_range_index[1]]] + peak_range = [ + xaxis[peak_range_index[0]], + xaxis[peak_range_index[1]], + ] if self.parent.fitting_peak_ui: self.parent.ui.fitting.removeItem(self.parent.fitting_peak_ui) self.parent.fitting_peak_ui = pg.LinearRegionItem( - values=peak_range, orientation="vertical", brush=None, movable=False, bounds=None + values=peak_range, + orientation="vertical", + brush=None, + movable=False, + bounds=None, + ) + self.parent.fitting_peak_ui.sigRegionChanged.connect( + self.parent.fitting_range_changed ) - self.parent.fitting_peak_ui.sigRegionChanged.connect(self.parent.fitting_range_changed) self.parent.fitting_peak_ui.setZValue(-10) self.parent.ui.fitting.addItem(self.parent.fitting_peak_ui) @@ -294,18 +343,31 @@ def update_fitting_plot(self): if key_path_exists_in_dictionary( dictionary=self.parent.fitting_input_dictionary, - tree_key=["rois", row_selected, "fitting", algo_name, name_of_page, "xaxis_to_fit"], + tree_key=[ + "rois", + row_selected, + "fitting", + algo_name, + name_of_page, + "xaxis_to_fit", + ], ): # show fit only if tof scale selected if x_axis_selected == "lambda": - _entry = self.parent.fitting_input_dictionary["rois"][row_selected]["fitting"][algo_name][ - name_of_page - ] + _entry = self.parent.fitting_input_dictionary["rois"][ + row_selected + ]["fitting"][algo_name][name_of_page] xaxis = _entry["xaxis_to_fit"] yaxis = _entry["yaxis_fitted"] # yaxis = -np.log(yaxis) self.parent.ui.fitting.plot( - xaxis, yaxis, pen=(self.parent.fit_rgb[0], self.parent.fit_rgb[1], self.parent.fit_rgb[2]) + xaxis, + yaxis, + pen=( + self.parent.fit_rgb[0], + self.parent.fit_rgb[1], + self.parent.fit_rgb[2], + ), ) if peak_range_index[0] is None: @@ -328,9 +390,9 @@ def update_roi_labels(self): left_index = find_nearest_index(array=xaxis, value=left_range) right_index = find_nearest_index(array=xaxis, value=right_range) - xaxis_in_selected_axis = self.parent.fitting_input_dictionary["xaxis"][x_axis_selected][0][ - global_left_range:global_right_range - ] + xaxis_in_selected_axis = self.parent.fitting_input_dictionary["xaxis"][ + x_axis_selected + ][0][global_left_range:global_right_range] real_left_value = xaxis_in_selected_axis[left_index] real_right_value = xaxis_in_selected_axis[right_index] if x_axis_selected == "lambda": diff --git a/notebooks/__code/bragg_edge/peak_fitting_evaluation/kropff_fitting_job_handler.py b/notebooks/__code/bragg_edge/peak_fitting_evaluation/kropff_fitting_job_handler.py index 1830ab89a..830f8f86a 100755 --- a/notebooks/__code/bragg_edge/peak_fitting_evaluation/kropff_fitting_job_handler.py +++ b/notebooks/__code/bragg_edge/peak_fitting_evaluation/kropff_fitting_job_handler.py @@ -4,7 +4,11 @@ from lmfit import Model, Parameter from __code.bragg_edge.bragg_edge_peak_fitting_gui_utility import GuiUtility -from __code.bragg_edge.fitting_functions import kropff_bragg_peak_tof, kropff_high_lambda, kropff_low_lambda +from __code.bragg_edge.fitting_functions import ( + kropff_bragg_peak_tof, + kropff_high_lambda, + kropff_low_lambda, +) class KropffFittingJobHandler: @@ -21,7 +25,10 @@ def prepare(self, kropff_tooldbox="high"): :param kropff_tooldbox: 'high', 'low', 'bragg_peak' """ if kropff_tooldbox == "bragg_peak": - fitting_range = [self.parent.kropff_fitting_range["low"][0], self.parent.kropff_fitting_range["high"][1]] + fitting_range = [ + self.parent.kropff_fitting_range["low"][0], + self.parent.kropff_fitting_range["high"][1], + ] else: fitting_range = self.parent.kropff_fitting_range[kropff_tooldbox] # fitting_range = self.parent.kropff_fitting_range[kropff_tooldbox] @@ -36,7 +43,9 @@ def prepare(self, kropff_tooldbox="high"): for _key in self.parent.fitting_input_dictionary["rois"].keys(): _yaxis = self.parent.fitting_input_dictionary["rois"][_key]["profile"] full_fitting_yaxis = _yaxis[left_xaxis_index:right_xaxis_index] - list_yaxis_to_fit.append(full_fitting_yaxis[fitting_range[0] : fitting_range[1] + 1]) + list_yaxis_to_fit.append( + full_fitting_yaxis[fitting_range[0] : fitting_range[1] + 1] + ) self.list_yaxis_to_fit = list_yaxis_to_fit def run_kropff_high_lambda(self, update_table_ui=False): @@ -66,10 +75,14 @@ def run_kropff_high_lambda(self, update_table_ui=False): "yaxis_fitted": yaxis_fitted, } - self.parent.fitting_input_dictionary["rois"][_index]["fitting"]["kropff"]["high"] = deepcopy(result_dict) + self.parent.fitting_input_dictionary["rois"][_index]["fitting"]["kropff"][ + "high" + ] = deepcopy(result_dict) if update_table_ui: - o_gui.update_kropff_high_lambda_table_ui(row=_index, a0=a0, b0=b0, a0_error=a0_error, b0_error=b0_error) + o_gui.update_kropff_high_lambda_table_ui( + row=_index, a0=a0, b0=b0, a0_error=a0_error, b0_error=b0_error + ) def run_kropff_low_lambda(self, update_table_ui=False): gmodel = Model(kropff_low_lambda, missing="drop", independent_vars=["lda"]) @@ -81,7 +94,9 @@ def run_kropff_low_lambda(self, update_table_ui=False): bhkl_init = float(str(self.parent.kropff_low_lda_bhkl_init.text())) for _row, yaxis in enumerate(self.list_yaxis_to_fit): - _entry = self.parent.fitting_input_dictionary["rois"][_row]["fitting"]["kropff"]["high"] + _entry = self.parent.fitting_input_dictionary["rois"][_row]["fitting"][ + "kropff" + ]["high"] a0 = float(_entry["a0"]) b0 = float(_entry["b0"]) @@ -111,15 +126,23 @@ def run_kropff_low_lambda(self, update_table_ui=False): "yaxis_fitted": yaxis_fitted, } - self.parent.fitting_input_dictionary["rois"][_row]["fitting"]["kropff"]["low"] = deepcopy(result_dict) + self.parent.fitting_input_dictionary["rois"][_row]["fitting"]["kropff"][ + "low" + ] = deepcopy(result_dict) if update_table_ui: o_gui.update_kropff_low_lambda_table_ui( - row=_row, ahkl=ahkl, bhkl=bhkl, ahkl_error=ahkl_error, bhkl_error=bhkl_error + row=_row, + ahkl=ahkl, + bhkl=bhkl, + ahkl_error=ahkl_error, + bhkl_error=bhkl_error, ) def run_bragg_peak(self, update_table_ui=False, list_row_to_fit=None): - gmodel = Model(kropff_bragg_peak_tof, nan_policy="propagate", independent_vars=["lda"]) + gmodel = Model( + kropff_bragg_peak_tof, nan_policy="propagate", independent_vars=["lda"] + ) lda = self.xaxis_to_fit o_gui = GuiUtility(parent=self.parent) @@ -133,11 +156,15 @@ def run_bragg_peak(self, update_table_ui=False, list_row_to_fit=None): if _row not in list_row_to_fit: continue - _entry_high = self.parent.fitting_input_dictionary["rois"][_row]["fitting"]["kropff"]["high"] + _entry_high = self.parent.fitting_input_dictionary["rois"][_row]["fitting"][ + "kropff" + ]["high"] a0 = float(_entry_high["a0"]) b0 = float(_entry_high["b0"]) - _entry_low = self.parent.fitting_input_dictionary["rois"][_row]["fitting"]["kropff"]["low"] + _entry_low = self.parent.fitting_input_dictionary["rois"][_row]["fitting"][ + "kropff" + ]["low"] ahkl = float(_entry_low["ahkl"]) bhkl = float(_entry_low["bhkl"]) @@ -162,7 +189,9 @@ def run_bragg_peak(self, update_table_ui=False, list_row_to_fit=None): tau = _result.params["tau"].value tau_error = _result.params["tau"].stderr - yaxis_fitted = kropff_bragg_peak_tof(self.xaxis_to_fit, a0, b0, ahkl, bhkl, ldahkl, sigma, tau) + yaxis_fitted = kropff_bragg_peak_tof( + self.xaxis_to_fit, a0, b0, ahkl, bhkl, ldahkl, sigma, tau + ) result_dict = { "ldahkl": ldahkl, @@ -175,9 +204,9 @@ def run_bragg_peak(self, update_table_ui=False, list_row_to_fit=None): "yaxis_fitted": yaxis_fitted, } - self.parent.fitting_input_dictionary["rois"][_row]["fitting"]["kropff"]["bragg_peak"] = deepcopy( - result_dict - ) + self.parent.fitting_input_dictionary["rois"][_row]["fitting"]["kropff"][ + "bragg_peak" + ] = deepcopy(result_dict) if update_table_ui: o_gui.update_kropff_bragg_edge_table_ui( diff --git a/notebooks/__code/bragg_edge/peak_fitting_evaluation/march_dollase.py b/notebooks/__code/bragg_edge/peak_fitting_evaluation/march_dollase.py index 206118b95..7fa91e875 100755 --- a/notebooks/__code/bragg_edge/peak_fitting_evaluation/march_dollase.py +++ b/notebooks/__code/bragg_edge/peak_fitting_evaluation/march_dollase.py @@ -98,13 +98,17 @@ def move_table_row(self, to_row_offset=-1): :param to_row_offset: +1 means moving row to the next row, -1 means moving up by one row """ row_selected = self.get_row_selected() - march_dollase_fitting_history_table = self.parent.march_dollase_fitting_history_table + march_dollase_fitting_history_table = ( + self.parent.march_dollase_fitting_history_table + ) row_to_move = march_dollase_fitting_history_table.pop(row_selected) new_row = row_selected + to_row_offset march_dollase_fitting_history_table.insert(new_row, row_to_move) - self.parent.march_dollase_fitting_history_table = march_dollase_fitting_history_table + self.parent.march_dollase_fitting_history_table = ( + march_dollase_fitting_history_table + ) o_gui = GuiUtility(parent=self.parent) o_gui.fill_march_dollase_table( @@ -167,42 +171,60 @@ def insert_row(self): march_dollase_fitting_history_table = list() new_entry = self.parent.march_dollase_fitting_history_table_default_new_row march_dollase_fitting_history_table.insert(0, new_entry) - self.parent.march_dollase_fitting_history_table = march_dollase_fitting_history_table + self.parent.march_dollase_fitting_history_table = ( + march_dollase_fitting_history_table + ) self.update_table_after_changing_row(changing_row=0) def insert_row_above(self): o_table = TableHandler(table_ui=self.parent.ui.march_dollase_user_input_table) row_selected = o_table.get_row_selected() - march_dollase_fitting_history_table = self.parent.march_dollase_fitting_history_table + march_dollase_fitting_history_table = ( + self.parent.march_dollase_fitting_history_table + ) new_entry = [False for _entry in march_dollase_fitting_history_table[0]] march_dollase_fitting_history_table.insert(row_selected, new_entry) - self.parent.march_dollase_fitting_history_table = march_dollase_fitting_history_table + self.parent.march_dollase_fitting_history_table = ( + march_dollase_fitting_history_table + ) self.update_table_after_changing_row(changing_row=row_selected + 1) def insert_row_below(self): o_table = TableHandler(table_ui=self.parent.ui.march_dollase_user_input_table) row_selected = o_table.get_row_selected() - march_dollase_fitting_history_table = self.parent.march_dollase_fitting_history_table + march_dollase_fitting_history_table = ( + self.parent.march_dollase_fitting_history_table + ) new_entry = [False for _entry in march_dollase_fitting_history_table[0]] march_dollase_fitting_history_table.insert(row_selected + 1, new_entry) - self.parent.march_dollase_fitting_history_table = march_dollase_fitting_history_table + self.parent.march_dollase_fitting_history_table = ( + march_dollase_fitting_history_table + ) self.update_table_after_changing_row(changing_row=row_selected + 2) def duplicate_row(self): o_table = TableHandler(table_ui=self.parent.ui.march_dollase_user_input_table) row_selected = o_table.get_row_selected() - march_dollase_fitting_history_table = self.parent.march_dollase_fitting_history_table + march_dollase_fitting_history_table = ( + self.parent.march_dollase_fitting_history_table + ) new_entry = march_dollase_fitting_history_table[row_selected] march_dollase_fitting_history_table.insert(row_selected, new_entry) - self.parent.march_dollase_fitting_history_table = march_dollase_fitting_history_table + self.parent.march_dollase_fitting_history_table = ( + march_dollase_fitting_history_table + ) self.update_table_after_changing_row(changing_row=row_selected + 1) def delete_row(self): o_table = TableHandler(table_ui=self.parent.ui.march_dollase_user_input_table) row_selected = o_table.get_row_selected() - march_dollase_fitting_history_table = self.parent.march_dollase_fitting_history_table + march_dollase_fitting_history_table = ( + self.parent.march_dollase_fitting_history_table + ) march_dollase_fitting_history_table.pop(row_selected) - self.parent.march_dollase_fitting_history_table = march_dollase_fitting_history_table + self.parent.march_dollase_fitting_history_table = ( + march_dollase_fitting_history_table + ) self.update_table_after_changing_row(changing_row=row_selected) def full_reset(self): @@ -226,13 +248,19 @@ def update_table_after_changing_row(self, changing_row=-1): self.table_clicked(row=changing_row) def advanced_mode_clicked(self): - hide_advanced = not self.parent.ui.march_dollase_advanced_mode_checkBox.isChecked() + hide_advanced = ( + not self.parent.ui.march_dollase_advanced_mode_checkBox.isChecked() + ) o_gui = GuiUtility(parent=self.parent) o_gui.set_columns_hidden( - table_ui=self.parent.ui.march_dollase_user_input_table, list_of_columns=[5, 6], state=hide_advanced + table_ui=self.parent.ui.march_dollase_user_input_table, + list_of_columns=[5, 6], + state=hide_advanced, ) o_gui.set_columns_hidden( - table_ui=self.parent.ui.march_dollase_result_table, list_of_columns=[6, 7, 13, 14], state=hide_advanced + table_ui=self.parent.ui.march_dollase_result_table, + list_of_columns=[6, 7, 13, 14], + state=hide_advanced, ) def update_fitting_plot(self): @@ -277,7 +305,11 @@ def update_fitting_plot(self): if self.parent.march_dollase_fitting_peak_ui: self.parent.ui.fitting.removeItem(self.parent.march_dollase_fitting_peak_ui) self.parent.march_dollase_fitting_peak_ui = pg.LinearRegionItem( - values=local_peak_range, orientation="vertical", brush=None, movable=move_bragg_peak_range, bounds=None + values=local_peak_range, + orientation="vertical", + brush=None, + movable=move_bragg_peak_range, + bounds=None, ) self.parent.march_dollase_fitting_peak_ui.sigRegionChanged.connect( self.parent.march_dollase_fitting_range_changed @@ -289,7 +321,9 @@ def update_fitting_plot(self): def save_table_history_and_initial_parameters(self): march_dollase_fitting_history_table = list() - march_dollase_fitting_initial_parameters = self.parent.march_dollase_fitting_initial_parameters + march_dollase_fitting_initial_parameters = ( + self.parent.march_dollase_fitting_initial_parameters + ) nbr_row = self.history_table_ui.rowCount() nbr_column = self.history_table_ui.columnCount() for _row in np.arange(nbr_row): @@ -311,8 +345,12 @@ def save_table_history_and_initial_parameters(self): march_dollase_fitting_history_table.append(_row_history) - self.parent.march_dollase_fitting_history_table = march_dollase_fitting_history_table - self.parent.march_dollase_fitting_initial_parameters = march_dollase_fitting_initial_parameters + self.parent.march_dollase_fitting_history_table = ( + march_dollase_fitting_history_table + ) + self.parent.march_dollase_fitting_initial_parameters = ( + march_dollase_fitting_initial_parameters + ) def fill_tables_with_fitting_information(self): self.fill_history_table_with_fitting_information() @@ -329,7 +367,9 @@ def fill_result_table_with_fitting_information(self): for _col, _col_name in enumerate(list_columns): _arg_value = _march_entry.get(_col_name) if _arg_value is not None: - self.result_table_ui.item(_row, _col + 1).setText(f"{_arg_value:4.2f}") + self.result_table_ui.item(_row, _col + 1).setText( + f"{_arg_value:4.2f}" + ) def fill_history_table_with_fitting_information(self): o_gui = GuiUtility(parent=self.parent) @@ -345,7 +385,9 @@ def get_initial_parameter_value(self, column=-1): def update_roi_labels(self): logging.info("> marche-dollase | update_roi_labels") [global_left_range, global_right_range] = self.parent.bragg_edge_range - [left_range, right_range] = list(self.parent.march_dollase_fitting_peak_ui.getRegion()) + [left_range, right_range] = list( + self.parent.march_dollase_fitting_peak_ui.getRegion() + ) o_get = Get(parent=self.parent) x_axis_selected = o_get.x_axis_checked() @@ -359,11 +401,13 @@ def update_roi_labels(self): right_index = find_nearest_index(array=xaxis, value=right_range) self.parent.march_dollase_fitting_range_selected = [left_index, right_index] - logging.info(f"-> march_dollase_fitting_range_selected: {self.parent.march_dollase_fitting_range_selected}") + logging.info( + f"-> march_dollase_fitting_range_selected: {self.parent.march_dollase_fitting_range_selected}" + ) - xaxis_in_selected_axis = self.parent.fitting_input_dictionary["xaxis"][x_axis_selected][0][ - global_left_range:global_right_range - ] + xaxis_in_selected_axis = self.parent.fitting_input_dictionary["xaxis"][ + x_axis_selected + ][0][global_left_range:global_right_range] real_left_value = xaxis_in_selected_axis[left_index] real_right_value = xaxis_in_selected_axis[right_index] if x_axis_selected == "lambda": @@ -376,8 +420,12 @@ def update_roi_labels(self): real_right_value = str_format.format(real_right_value) units = Get.units(name=x_axis_selected) - self.parent.ui.march_dollase_bragg_peak_range_from_value.setText(str(real_left_value)) - self.parent.ui.march_dollase_bragg_peak_range_to_value.setText(str(real_right_value)) + self.parent.ui.march_dollase_bragg_peak_range_from_value.setText( + str(real_left_value) + ) + self.parent.ui.march_dollase_bragg_peak_range_to_value.setText( + str(real_right_value) + ) self.parent.ui.march_dollase_from_bragg_peak_range_units.setText(units) self.parent.ui.march_dollase_to_bragg_peak_range_units.setText(units) @@ -402,7 +450,9 @@ def export_data_of_selected_rows(self): str_list_of_rows_selected = [str(_row) for _row in list_of_rows_selected] str_rows = "_".join(str_list_of_rows_selected) - output_file_name = os.path.join(str(_export_folder), f"march_dollase_result_fitting_row{str_rows}.txt") + output_file_name = os.path.join( + str(_export_folder), f"march_dollase_result_fitting_row{str_rows}.txt" + ) fitting_input_dictionary = self.parent.fitting_input_dictionary @@ -413,13 +463,17 @@ def export_data_of_selected_rows(self): # metadata metadata = ["#Marche Dollase Result of Fitting"] - is_advance_mode = self.parent.ui.march_dollase_advanced_mode_checkBox.isChecked() + is_advance_mode = ( + self.parent.ui.march_dollase_advanced_mode_checkBox.isChecked() + ) metadata.append(f"#Using advanced fitting mode: {is_advance_mode}") data_label = "#image index, TOF(micros), Lambda(Angstroms)" temp_data = [] for _row in list_of_rows_selected: - _entry = fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"] + _entry = fitting_input_dictionary["rois"][_row]["fitting"][ + "march_dollase" + ] _top_entry = fitting_input_dictionary["rois"][_row] _line = ( "#-> row {}: x0: {}, y0:{}, width:{}, height:{}, " @@ -457,7 +511,12 @@ def export_data_of_selected_rows(self): str_data += f", {temp_data[_col_index][_index]}" data.append(str_data) - make_ascii_file(metadata=metadata, data=data, output_file_name=output_file_name, dim="1d") + make_ascii_file( + metadata=metadata, + data=data, + output_file_name=output_file_name, + dim="1d", + ) message = f"{output_file_name} has been created!" self.parent.ui.statusbar.showMessage(message, 10000) # 10s diff --git a/notebooks/__code/bragg_edge/peak_fitting_evaluation/march_dollase_fitting_job_handler.py b/notebooks/__code/bragg_edge/peak_fitting_evaluation/march_dollase_fitting_job_handler.py index cc1598acd..5a94f311a 100755 --- a/notebooks/__code/bragg_edge/peak_fitting_evaluation/march_dollase_fitting_job_handler.py +++ b/notebooks/__code/bragg_edge/peak_fitting_evaluation/march_dollase_fitting_job_handler.py @@ -4,7 +4,10 @@ from lmfit import Model from qtpy.QtWidgets import QApplication -from __code.bragg_edge.fitting_functions import march_dollase_advanced_fit, march_dollase_basic_fit +from __code.bragg_edge.fitting_functions import ( + march_dollase_advanced_fit, + march_dollase_basic_fit, +) from __code.bragg_edge.get import Get from __code.bragg_edge.peak_fitting_evaluation.march_dollase import MarchDollase @@ -32,10 +35,14 @@ def initialize_fitting_input_dictionary(self): nbr_column = self.parent.ui.march_dollase_user_input_table.columnCount() list_name_of_parameters = [] for _col in np.arange(nbr_column): - _item = self.parent.ui.march_dollase_user_input_table.horizontalHeaderItem(_col).text() + _item = self.parent.ui.march_dollase_user_input_table.horizontalHeaderItem( + _col + ).text() list_name_of_parameters.append(_item) - march_dollase_fitting_history_table = self.parent.march_dollase_fitting_history_table + march_dollase_fitting_history_table = ( + self.parent.march_dollase_fitting_history_table + ) [d_spacing_flag, sigma_flag, alpha_flag, a1_flag, a2_flag, a5_flag, a6_flag] = ( march_dollase_fitting_history_table[0] ) @@ -47,19 +54,27 @@ def initialize_fitting_input_dictionary(self): self.parent.march_dollase_fitting_initial_parameters["d_spacing"] = d_spacing fitting_input_dictionary = self.parent.fitting_input_dictionary - logging.info(f"fitting_input_dictionary['rois'].keys(): {fitting_input_dictionary['rois'].keys()}") + logging.info( + f"fitting_input_dictionary['rois'].keys(): {fitting_input_dictionary['rois'].keys()}" + ) for _row in fitting_input_dictionary["rois"].keys(): logging.info(f"in _row: {_row}") if not d_spacing_flag: - fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"]["d_spacing"] = d_spacing + fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"][ + "d_spacing" + ] = d_spacing if not sigma_flag: - fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"]["sigma"] = sigma + fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"][ + "sigma" + ] = sigma if not alpha_flag: - fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"]["alpha"] = alpha + fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"][ + "alpha" + ] = alpha # {'left_part': {'lambda_x_axis': [], # 'y_axis': [], @@ -75,27 +90,42 @@ def initialize_fitting_input_dictionary(self): logging.info(f"self.left_center_right_axis: {self.left_center_right_axis}") if self.is_advanced_mode(): a2 = self.get_a2(advanced_mode=self.is_advanced_mode()) - fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"]["a2"] = a2 + fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"][ + "a2" + ] = a2 a5 = self.get_a5() - fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"]["a5"] = a5 + fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"][ + "a5" + ] = a5 a6 = self.get_a6() - fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"]["a6"] = a6 + fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"][ + "a6" + ] = a6 a1 = self.get_a1(advanced_mode=self.is_advanced_mode()) - fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"]["a1"] = a1 + fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"][ + "a1" + ] = a1 else: a1 = self.get_a1(advanced_mode=self.is_advanced_mode()) - fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"]["a1"] = a1 + fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"][ + "a1" + ] = a1 a2 = self.get_a2(advanced_mode=self.is_advanced_mode()) - fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"]["a2"] = a2 + fitting_input_dictionary["rois"][_row]["fitting"]["march_dollase"][ + "a2" + ] = a2 def isolate_left_center_right_axis(self, row=-1): bragg_edge_range = self.parent.fitting_input_dictionary["bragg_edge_range"] - [global_left_index, global_right_index] = [int(bragg_edge_range[0]), int(bragg_edge_range[1])] + [global_left_index, global_right_index] = [ + int(bragg_edge_range[0]), + int(bragg_edge_range[1]), + ] # get full x-axis (lambda) full_lambda_x_axis = self.parent.fitting_input_dictionary["xaxis"]["lambda"][0] lambda_x_axis = full_lambda_x_axis[global_left_index:global_right_index] @@ -107,12 +137,18 @@ def isolate_left_center_right_axis(self, row=-1): [left_index, right_index] = self.parent.march_dollase_fitting_range_selected return { - "left_part": {"lambda_x_axis": lambda_x_axis[0:left_index], "y_axis": y_axis[0:left_index]}, + "left_part": { + "lambda_x_axis": lambda_x_axis[0:left_index], + "y_axis": y_axis[0:left_index], + }, "center_part": { "lambda_x_axis": lambda_x_axis[left_index:right_index], "y_axis": y_axis[left_index:right_index], }, - "right_part": {"lambda_x_axis": lambda_x_axis[right_index:], "y_axis": y_axis[right_index:]}, + "right_part": { + "lambda_x_axis": lambda_x_axis[right_index:], + "y_axis": y_axis[right_index:], + }, } def get_a1(self, advanced_mode=True): @@ -214,7 +250,9 @@ def prepare(self): self.initialize_fitting_input_dictionary() fitting_input_dictionary = self.parent.fitting_input_dictionary - march_dollase_fitting_history_table = self.parent.march_dollase_fitting_history_table + march_dollase_fitting_history_table = ( + self.parent.march_dollase_fitting_history_table + ) _is_advanced_mode = self.is_advanced_mode() if _is_advanced_mode: @@ -224,7 +262,9 @@ def prepare(self): gmodel = Model(march_dollase_basic_fit, nan_policy="propagate") # logging.info(f"-> advanced mode?: {_is_advanced_mode}") - march_dollase_fitting_history_table = self.parent.march_dollase_fitting_history_table + march_dollase_fitting_history_table = ( + self.parent.march_dollase_fitting_history_table + ) nbr_row_in_fitting_scenario = len(march_dollase_fitting_history_table) self.parent.ui.eventProgress.setValue(0) @@ -233,9 +273,15 @@ def prepare(self): self.parent.ui.eventProgress.setVisible(True) def set_params(params_object, name_of_parameter, dict_entry, parameter_flag): - params_object.add(name_of_parameter, value=float(dict_entry[name_of_parameter]), vary=parameter_flag) - - def record_result_into_dict(entry_dict, result_object, name_of_parameter, parameter_flag): + params_object.add( + name_of_parameter, + value=float(dict_entry[name_of_parameter]), + vary=parameter_flag, + ) + + def record_result_into_dict( + entry_dict, result_object, name_of_parameter, parameter_flag + ): # logging.info("-> Recording result of fitting into a dictionary:") if parameter_flag: # logging.info(f"--> name_of_parameter: {name_of_parameter}") @@ -250,7 +296,9 @@ def record_result_into_dict(entry_dict, result_object, name_of_parameter, parame for _roi_row in np.arange(nbr_roi_row): # logging.info(f"--> working on roi_row: {_roi_row}") - _entry = fitting_input_dictionary["rois"][_roi_row]["fitting"]["march_dollase"] + _entry = fitting_input_dictionary["rois"][_roi_row]["fitting"][ + "march_dollase" + ] o_get = Get(parent=self.parent) xaxis = o_get.x_axis_data(x_axis_selected="lambda") @@ -271,10 +319,20 @@ def record_result_into_dict(entry_dict, result_object, name_of_parameter, parame dict["parameters"] = {} - for _history_row, _row_entry in enumerate(march_dollase_fitting_history_table): + for _history_row, _row_entry in enumerate( + march_dollase_fitting_history_table + ): # logging.info(f"--> in _history_row: {_history_row}") - [d_spacing_flag, sigma_flag, alpha_flag, a1_flag, a2_flag, a5_flag, a6_flag] = _row_entry + [ + d_spacing_flag, + sigma_flag, + alpha_flag, + a1_flag, + a2_flag, + a5_flag, + a6_flag, + ] = _row_entry if _is_advanced_mode: a5_flag = _entry["a5"] @@ -345,7 +403,9 @@ def record_result_into_dict(entry_dict, result_object, name_of_parameter, parame record_result_into_dict(_entry, o_result, "a5", a5_flag) record_result_into_dict(_entry, o_result, "a6", a6_flag) - fitting_input_dictionary["rois"][_roi_row]["fitting"]["march_dollase"] = _entry + fitting_input_dictionary["rois"][_roi_row]["fitting"][ + "march_dollase" + ] = _entry self.parent.ui.eventProgress.setValue(_roi_row + 1) QApplication.processEvents() diff --git a/notebooks/__code/bragg_edge/peak_fitting_evaluation/peak_fitting_interface_initialization.py b/notebooks/__code/bragg_edge/peak_fitting_evaluation/peak_fitting_interface_initialization.py index 50495cc91..e07e066ac 100755 --- a/notebooks/__code/bragg_edge/peak_fitting_evaluation/peak_fitting_interface_initialization.py +++ b/notebooks/__code/bragg_edge/peak_fitting_evaluation/peak_fitting_interface_initialization.py @@ -61,7 +61,10 @@ def normalize_images_by_white_beam(self): self.parent.o_norm.data["sample"]["data"][_index_data] = normalized_data def block_signals(self, flag): - list_ui = [self.parent.ui.profile_of_bin_size_slider, self.parent.ui.tab_algorithm] + list_ui = [ + self.parent.ui.profile_of_bin_size_slider, + self.parent.ui.tab_algorithm, + ] for _ui in list_ui: _ui.blockSignals(flag) @@ -105,9 +108,15 @@ def _matplotlib(parent=None, widget=None): widget.setLayout(layout) return sc - self.parent.kropff_high_plot = _matplotlib(parent=self.parent, widget=self.parent.ui.high_widget) - self.parent.kropff_low_plot = _matplotlib(parent=self.parent, widget=self.parent.ui.low_widget) - self.parent.kropff_bragg_peak_plot = _matplotlib(parent=self.parent, widget=self.parent.ui.bragg_peak_widget) + self.parent.kropff_high_plot = _matplotlib( + parent=self.parent, widget=self.parent.ui.high_widget + ) + self.parent.kropff_low_plot = _matplotlib( + parent=self.parent, widget=self.parent.ui.low_widget + ) + self.parent.kropff_bragg_peak_plot = _matplotlib( + parent=self.parent, widget=self.parent.ui.bragg_peak_widget + ) self.parent.march_dollase_plot = _matplotlib( parent=self.parent, widget=self.parent.ui.march_dollase_graph_widget @@ -126,7 +135,13 @@ def pyqtgraph_fitting(self): def kropff_fitting_table(self): ## Kropff # high lambda - column_names = ["x\u2080; y\u2080; width; height", "a\u2080", "b\u2080", "a\u2080_error", "b\u2080_error"] + column_names = [ + "x\u2080; y\u2080; width; height", + "a\u2080", + "b\u2080", + "a\u2080_error", + "b\u2080_error", + ] column_sizes = [150, 100, 100, 100, 100] o_high = TableHandler(table_ui=self.parent.ui.high_lda_tableWidget) for _col_index, _col_name in enumerate(column_names): @@ -135,7 +150,13 @@ def kropff_fitting_table(self): o_high.set_column_sizes(column_sizes=column_sizes) # low lambda - column_names = ["x\u2080; y\u2080; width; height", "a_hkl", "b_hkl", "a_hkl_error", "b_hkl_error"] + column_names = [ + "x\u2080; y\u2080; width; height", + "a_hkl", + "b_hkl", + "a_hkl_error", + "b_hkl_error", + ] column_sizes = [150, 100, 100, 100, 100] o_low = TableHandler(table_ui=self.parent.ui.low_lda_tableWidget) for _col_index, _col_name in enumerate(column_names): @@ -144,7 +165,15 @@ def kropff_fitting_table(self): o_low.set_column_sizes(column_sizes=column_sizes) # bragg edge - column_names = ["x0; y0; width; height", "t_hkl", "tau", "sigma", "t_hkl_error", "tau_error", "sigma_error"] + column_names = [ + "x0; y0; width; height", + "t_hkl", + "tau", + "sigma", + "t_hkl_error", + "tau_error", + "sigma_error", + ] column_sizes = [150, 100, 100, 100, 100, 100, 100] o_bragg = TableHandler(table_ui=self.parent.ui.bragg_edge_tableWidget) for _col_index, _col_name in enumerate(column_names): @@ -153,15 +182,23 @@ def kropff_fitting_table(self): o_bragg.set_column_sizes(column_sizes=column_sizes) def march_dollase(self): - self.parent.march_dollase_history_state_full_reset = copy.deepcopy(self.march_dollase_history_state) + self.parent.march_dollase_history_state_full_reset = copy.deepcopy( + self.march_dollase_history_state + ) # init widgets _file_path = os.path.dirname(__file__) - up_arrow_file = os.path.abspath(os.path.join(_file_path, "../static/up_arrow_black.png")) + up_arrow_file = os.path.abspath( + os.path.join(_file_path, "../static/up_arrow_black.png") + ) self.parent.ui.march_dollase_user_input_up.setIcon(QtGui.QIcon(up_arrow_file)) - down_arrow_file = os.path.abspath(os.path.join(_file_path, "../static/down_arrow_black.png")) - self.parent.ui.march_dollase_user_input_down.setIcon(QtGui.QIcon(down_arrow_file)) + down_arrow_file = os.path.abspath( + os.path.join(_file_path, "../static/down_arrow_black.png") + ) + self.parent.ui.march_dollase_user_input_down.setIcon( + QtGui.QIcon(down_arrow_file) + ) o_gui = GuiUtility(parent=self.parent) o_gui.fill_march_dollase_table( @@ -169,7 +206,9 @@ def march_dollase(self): initial_parameters=self.parent.march_dollase_fitting_initial_parameters, ) - self.parent.march_dollase_fitting_history_table = self.march_dollase_history_state + self.parent.march_dollase_fitting_history_table = ( + self.march_dollase_history_state + ) self.parent.march_dollase_fitting_history_table_default_new_row = copy.deepcopy( self.march_dollase_history_state[0] ) @@ -191,16 +230,36 @@ def march_dollase(self): "A\u2085_error", "A\u2086_error", ] - column_sizes = [150, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100] + column_sizes = [ + 150, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + ] o_march = TableHandler(table_ui=self.parent.ui.march_dollase_result_table) for _col_index, _col_name in enumerate(column_names): o_march.insert_column(_col_index) o_march.set_column_names(column_names=column_names) o_march.set_column_sizes(column_sizes=column_sizes) - state_advanced_columns = not self.parent.ui.march_dollase_advanced_mode_checkBox.isChecked() + state_advanced_columns = ( + not self.parent.ui.march_dollase_advanced_mode_checkBox.isChecked() + ) o_gui.set_columns_hidden( - table_ui=self.parent.ui.march_dollase_user_input_table, list_of_columns=[5, 6], state=state_advanced_columns + table_ui=self.parent.ui.march_dollase_user_input_table, + list_of_columns=[5, 6], + state=state_advanced_columns, ) # table @@ -217,17 +276,29 @@ def labels(self): self.parent.ui.fitting_lambda_radiobutton.setText("\u03bb (\u212b)") def text_fields(self): - self.parent.ui.distance_detector_sample.setText(str(self.distance_detector_sample)) + self.parent.ui.distance_detector_sample.setText( + str(self.distance_detector_sample) + ) self.parent.ui.detector_offset.setText(str(self.detector_offset)) - self.parent.ui.kropff_high_lda_a0_init.setText(str(self.parent.fitting_parameters_init["kropff"]["a0"])) - self.parent.ui.kropff_high_lda_b0_init.setText(str(self.parent.fitting_parameters_init["kropff"]["b0"])) - self.parent.ui.kropff_low_lda_ahkl_init.setText(str(self.parent.fitting_parameters_init["kropff"]["ahkl"])) - self.parent.ui.kropff_low_lda_bhkl_init.setText(str(self.parent.fitting_parameters_init["kropff"]["bhkl"])) + self.parent.ui.kropff_high_lda_a0_init.setText( + str(self.parent.fitting_parameters_init["kropff"]["a0"]) + ) + self.parent.ui.kropff_high_lda_b0_init.setText( + str(self.parent.fitting_parameters_init["kropff"]["b0"]) + ) + self.parent.ui.kropff_low_lda_ahkl_init.setText( + str(self.parent.fitting_parameters_init["kropff"]["ahkl"]) + ) + self.parent.ui.kropff_low_lda_bhkl_init.setText( + str(self.parent.fitting_parameters_init["kropff"]["bhkl"]) + ) self.parent.ui.kropff_bragg_peak_ldahkl_init.setText( str(self.parent.fitting_parameters_init["kropff"]["ldahkl"]) ) - self.parent.ui.kropff_bragg_peak_tau_init.setText(str(self.parent.fitting_parameters_init["kropff"]["tau"])) + self.parent.ui.kropff_bragg_peak_tau_init.setText( + str(self.parent.fitting_parameters_init["kropff"]["tau"]) + ) # list_sigma = self.parent.fitting_parameters_init['kropff']['sigma'] # list_sigma = [str(_value) for _value in list_sigma] # str_list_sigma = ", ".join(list_sigma) @@ -241,7 +312,9 @@ def widgets(self): self.parent.ui.splitter.setSizes([500, 400]) self.parent.ui.roi_size_slider.setMinimum(1) - max_value = np.min([self.parent.image_size["width"], self.parent.image_size["height"]]) + max_value = np.min( + [self.parent.image_size["width"], self.parent.image_size["height"]] + ) self.parent.ui.roi_size_slider.setMaximum(max_value) default_roi_size = int(max_value / 3) self.parent.ui.roi_size_slider.setValue(default_roi_size) diff --git a/notebooks/__code/bragg_edge/peak_fitting_initialization.py b/notebooks/__code/bragg_edge/peak_fitting_initialization.py index 7d8d55a00..5502bcd7c 100755 --- a/notebooks/__code/bragg_edge/peak_fitting_initialization.py +++ b/notebooks/__code/bragg_edge/peak_fitting_initialization.py @@ -2,7 +2,14 @@ import numpy as np -KROPFF_HIGH = {"a0": None, "b0": None, "a0_error": None, "b0_error": None, "xaxis_to_fit": None, "yaxis_fitted": None} +KROPFF_HIGH = { + "a0": None, + "b0": None, + "a0_error": None, + "b0_error": None, + "xaxis_to_fit": None, + "yaxis_fitted": None, +} KROPFF_LOW = { "ahkl": None, "bhkl": None, diff --git a/notebooks/__code/bragg_edge/peak_fitting_interface_initialization.py b/notebooks/__code/bragg_edge/peak_fitting_interface_initialization.py index e750867ae..4e58ab8b5 100755 --- a/notebooks/__code/bragg_edge/peak_fitting_interface_initialization.py +++ b/notebooks/__code/bragg_edge/peak_fitting_interface_initialization.py @@ -105,9 +105,15 @@ def _matplotlib(parent=None, widget=None): widget.setLayout(layout) return sc - self.parent.kropff_high_plot = _matplotlib(parent=self.parent, widget=self.parent.ui.high_widget) - self.parent.kropff_low_plot = _matplotlib(parent=self.parent, widget=self.parent.ui.low_widget) - self.parent.kropff_bragg_peak_plot = _matplotlib(parent=self.parent, widget=self.parent.ui.bragg_peak_widget) + self.parent.kropff_high_plot = _matplotlib( + parent=self.parent, widget=self.parent.ui.high_widget + ) + self.parent.kropff_low_plot = _matplotlib( + parent=self.parent, widget=self.parent.ui.low_widget + ) + self.parent.kropff_bragg_peak_plot = _matplotlib( + parent=self.parent, widget=self.parent.ui.bragg_peak_widget + ) self.parent.march_dollase_plot = _matplotlib( parent=self.parent, widget=self.parent.ui.march_dollase_graph_widget @@ -123,7 +129,13 @@ def pyqtgraph_fitting(self): def kropff_fitting_table(self): ## Kropff # high lambda - column_names = ["x\u2080; y\u2080; width; height", "a\u2080", "b\u2080", "a\u2080_error", "b\u2080_error"] + column_names = [ + "x\u2080; y\u2080; width; height", + "a\u2080", + "b\u2080", + "a\u2080_error", + "b\u2080_error", + ] column_sizes = [150, 100, 100, 100, 100] o_high = TableHandler(table_ui=self.parent.ui.high_lda_tableWidget) for _col_index, _col_name in enumerate(column_names): @@ -132,7 +144,13 @@ def kropff_fitting_table(self): o_high.set_column_sizes(column_sizes=column_sizes) # low lambda - column_names = ["x\u2080; y\u2080; width; height", "a_hkl", "b_hkl", "a_hkl_error", "b_hkl_error"] + column_names = [ + "x\u2080; y\u2080; width; height", + "a_hkl", + "b_hkl", + "a_hkl_error", + "b_hkl_error", + ] column_sizes = [150, 100, 100, 100, 100] o_low = TableHandler(table_ui=self.parent.ui.low_lda_tableWidget) for _col_index, _col_name in enumerate(column_names): @@ -141,7 +159,15 @@ def kropff_fitting_table(self): o_low.set_column_sizes(column_sizes=column_sizes) # bragg edge - column_names = ["x0; y0; width; height", "t_hkl", "tau", "sigma", "t_hkl_error", "tau_error", "sigma_error"] + column_names = [ + "x0; y0; width; height", + "t_hkl", + "tau", + "sigma", + "t_hkl_error", + "tau_error", + "sigma_error", + ] column_sizes = [150, 100, 100, 100, 100, 100, 100] o_bragg = TableHandler(table_ui=self.parent.ui.bragg_edge_tableWidget) for _col_index, _col_name in enumerate(column_names): @@ -150,14 +176,20 @@ def kropff_fitting_table(self): o_bragg.set_column_sizes(column_sizes=column_sizes) def march_dollase(self): - self.parent.march_dollase_history_state_full_reset = copy.deepcopy(self.march_dollase_history_state) + self.parent.march_dollase_history_state_full_reset = copy.deepcopy( + self.march_dollase_history_state + ) # init widgets _file_path = os.path.dirname(__file__) - up_arrow_file = os.path.abspath(os.path.join(_file_path, "../static/up_arrow_black.png")) + up_arrow_file = os.path.abspath( + os.path.join(_file_path, "../static/up_arrow_black.png") + ) self.parent.ui.march_dollase_user_input_up.setIcon(QIcon(up_arrow_file)) - down_arrow_file = os.path.abspath(os.path.join(_file_path, "../static/down_arrow_black.png")) + down_arrow_file = os.path.abspath( + os.path.join(_file_path, "../static/down_arrow_black.png") + ) self.parent.ui.march_dollase_user_input_down.setIcon(QIcon(down_arrow_file)) o_gui = GuiUtility(parent=self.parent) @@ -166,7 +198,9 @@ def march_dollase(self): initial_parameters=self.parent.march_dollase_fitting_initial_parameters, ) - self.parent.march_dollase_fitting_history_table = self.march_dollase_history_state + self.parent.march_dollase_fitting_history_table = ( + self.march_dollase_history_state + ) self.parent.march_dollase_fitting_history_table_default_new_row = copy.deepcopy( self.march_dollase_history_state[0] ) @@ -188,16 +222,36 @@ def march_dollase(self): "A\u2085_error", "A\u2086_error", ] - column_sizes = [150, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100] + column_sizes = [ + 150, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + ] o_march = TableHandler(table_ui=self.parent.ui.march_dollase_result_table) for _col_index, _col_name in enumerate(column_names): o_march.insert_column(_col_index) o_march.set_column_names(column_names=column_names) o_march.set_column_sizes(column_sizes=column_sizes) - state_advanced_columns = not self.parent.ui.march_dollase_advanced_mode_checkBox.isChecked() + state_advanced_columns = ( + not self.parent.ui.march_dollase_advanced_mode_checkBox.isChecked() + ) o_gui.set_columns_hidden( - table_ui=self.parent.ui.march_dollase_user_input_table, list_of_columns=[5, 6], state=state_advanced_columns + table_ui=self.parent.ui.march_dollase_user_input_table, + list_of_columns=[5, 6], + state=state_advanced_columns, ) # table @@ -214,17 +268,29 @@ def labels(self): self.parent.ui.fitting_lambda_radiobutton.setText("\u03bb (\u212b)") def text_fields(self): - self.parent.ui.distance_detector_sample.setText(str(self.distance_detector_sample)) + self.parent.ui.distance_detector_sample.setText( + str(self.distance_detector_sample) + ) self.parent.ui.detector_offset.setText(str(self.detector_offset)) - self.parent.ui.kropff_high_lda_a0_init.setText(str(self.parent.fitting_parameters_init["kropff"]["a0"])) - self.parent.ui.kropff_high_lda_b0_init.setText(str(self.parent.fitting_parameters_init["kropff"]["b0"])) - self.parent.ui.kropff_low_lda_ahkl_init.setText(str(self.parent.fitting_parameters_init["kropff"]["ahkl"])) - self.parent.ui.kropff_low_lda_bhkl_init.setText(str(self.parent.fitting_parameters_init["kropff"]["bhkl"])) + self.parent.ui.kropff_high_lda_a0_init.setText( + str(self.parent.fitting_parameters_init["kropff"]["a0"]) + ) + self.parent.ui.kropff_high_lda_b0_init.setText( + str(self.parent.fitting_parameters_init["kropff"]["b0"]) + ) + self.parent.ui.kropff_low_lda_ahkl_init.setText( + str(self.parent.fitting_parameters_init["kropff"]["ahkl"]) + ) + self.parent.ui.kropff_low_lda_bhkl_init.setText( + str(self.parent.fitting_parameters_init["kropff"]["bhkl"]) + ) self.parent.ui.kropff_bragg_peak_ldahkl_init.setText( str(self.parent.fitting_parameters_init["kropff"]["ldahkl"]) ) - self.parent.ui.kropff_bragg_peak_tau_init.setText(str(self.parent.fitting_parameters_init["kropff"]["tau"])) + self.parent.ui.kropff_bragg_peak_tau_init.setText( + str(self.parent.fitting_parameters_init["kropff"]["tau"]) + ) # list_sigma = self.parent.fitting_parameters_init['kropff']['sigma'] # list_sigma = [str(_value) for _value in list_sigma] # str_list_sigma = ", ".join(list_sigma) @@ -238,7 +304,9 @@ def widgets(self): self.parent.ui.splitter.setSizes([500, 400]) self.parent.ui.roi_size_slider.setMinimum(1) - max_value = np.min([self.parent.image_size["width"], self.parent.image_size["height"]]) + max_value = np.min( + [self.parent.image_size["width"], self.parent.image_size["height"]] + ) self.parent.ui.roi_size_slider.setMaximum(max_value) default_roi_size = int(max_value / 3) self.parent.ui.roi_size_slider.setValue(default_roi_size) diff --git a/notebooks/__code/calculate_water_intake_profile.py b/notebooks/__code/calculate_water_intake_profile.py index 9722de516..59d95aa4c 100755 --- a/notebooks/__code/calculate_water_intake_profile.py +++ b/notebooks/__code/calculate_water_intake_profile.py @@ -16,14 +16,18 @@ class CalculateWaterIntakeProfile: files_data = [] files_metadata = [] - documentation_path = "__docs/calculate_water_intake_profile/water_intake_calculation.pdf" + documentation_path = ( + "__docs/calculate_water_intake_profile/water_intake_calculation.pdf" + ) def __init__(self, working_dir="./"): self.working_dir = working_dir def select_counts_vs_time_files(self): self.list_files_ui = ipywe.fileselector.FileSelectorPanel( - instruction="Select Counts vs Time Files ...", start_dir=self.working_dir, multiple=True + instruction="Select Counts vs Time Files ...", + start_dir=self.working_dir, + multiple=True, ) self.list_files_ui.show() @@ -47,7 +51,9 @@ def retrieve_metadata(self, file_name): return _metadata def retrieve_data(self, file_name, nbr_comment_line=0, col_names=None): - file_0 = pd.read_csv(file_name, skiprows=nbr_comment_line, header=None, names=col_names) + file_0 = pd.read_csv( + file_name, skiprows=nbr_comment_line, header=None, names=col_names + ) return file_0 def format_col_names(self, col_names): @@ -69,7 +75,11 @@ def retrieve_data_and_metadata(self): files_metadata[file] = _metadata _col_names = _metadata["Label"].split(",") _new_col_names = self.format_col_names(_col_names) - _data = self.retrieve_data(file, nbr_comment_line=_metadata["nbr_comment_line"], col_names=_new_col_names) + _data = self.retrieve_data( + file, + nbr_comment_line=_metadata["nbr_comment_line"], + col_names=_new_col_names, + ) files_data[file] = _data index += 1 @@ -120,7 +130,10 @@ def display_intake_vs_profile(file_index): _metadata = metadata[_file] _rebin = float(_metadata["Rebin in y direction"]) _metadata_entry = _metadata["ROI selected (y0, x0, height, width)"] - m = re.match(r"\((?P\d*), (?P\d*), (?P\d*), (?P\d*)\)", _metadata_entry) + m = re.match( + r"\((?P\d*), (?P\d*), (?P\d*), (?P\d*)\)", + _metadata_entry, + ) y0 = float(m.group("y0")) height = float(m.group("height")) @@ -133,7 +146,9 @@ def display_intake_vs_profile(file_index): _ = interact( display_intake_vs_profile, - file_index=widgets.IntSlider(min=0, max=len(list_files) - 1, value=0, description="File Index"), + file_index=widgets.IntSlider( + min=0, max=len(list_files) - 1, value=0, description="File Index" + ), ) def display_water_intake_peak_in_px(self): @@ -147,7 +162,9 @@ def define_pixel_size(self): box = widgets.HBox( [ widgets.Label("Size/Pixel Ratio"), - widgets.FloatText(value=1, step=0.1, layout=widgets.Layout(width="10%")), + widgets.FloatText( + value=1, step=0.1, layout=widgets.Layout(width="10%") + ), widgets.Label("mm/px"), ] ) @@ -167,16 +184,23 @@ def display_water_intake_peak_in_mm(self): def select_output_folder(self): self.output_folder_ui = ipywe.fileselector.FileSelectorPanel( - instruction="Select Output Folder ...", start_dir=self.working_dir, type="directory" + instruction="Select Output Folder ...", + start_dir=self.working_dir, + type="directory", ) self.output_folder_ui.show() def export(self): output_folder = os.path.abspath(self.output_folder_ui.selected) - short_file_name = os.path.basename(os.path.abspath(os.path.dirname(self.list_files[0]))) + ".txt" + short_file_name = ( + os.path.basename(os.path.abspath(os.path.dirname(self.list_files[0]))) + + ".txt" + ) output_file_name = os.path.join(output_folder, short_file_name) - ascii_text = f"# Source data: {os.path.basename(os.path.dirname(self.list_files[0]))}\n" + ascii_text = ( + f"# Source data: {os.path.basename(os.path.dirname(self.list_files[0]))}\n" + ) ascii_text += "# time_stamp(s), water_intake_position (mm)\n" for _index in range(len(self.time_stamps)): _x_value = self.time_stamps[_index] diff --git a/notebooks/__code/calibrated_transmission/calibrated_transmission.py b/notebooks/__code/calibrated_transmission/calibrated_transmission.py index 3ca58efb8..bcc855883 100755 --- a/notebooks/__code/calibrated_transmission/calibrated_transmission.py +++ b/notebooks/__code/calibrated_transmission/calibrated_transmission.py @@ -111,7 +111,9 @@ def init_statusbar(self): def init_table(self): list_files_full_name = self.data_dict["file_name"] - list_files_short_name = [os.path.basename(_file) for _file in list_files_full_name] + list_files_short_name = [ + os.path.basename(_file) for _file in list_files_full_name + ] list_time_stamp = self.timestamp_dict["list_time_stamp"] list_time_stamp_user_format = self.timestamp_dict["list_time_stamp_user_format"] @@ -119,7 +121,9 @@ def init_table(self): for _row, _file in enumerate(list_files_short_name): self.ui.summary_table.insertRow(_row) self.set_item_summary_table(row=_row, col=0, value=_file) - self.set_item_summary_table(row=_row, col=1, value=list_time_stamp_user_format[_row]) + self.set_item_summary_table( + row=_row, col=1, value=list_time_stamp_user_format[_row] + ) _offset = list_time_stamp[_row] - time_0 self.set_item_summary_table(row=_row, col=2, value=f"{_offset:0.2f}") @@ -150,7 +154,10 @@ def init_pyqtgrpah(self): def define_roi(roi_dict, callback_function): cal = pg.RectROI( - [roi_dict["x0"], roi_dict["y0"]], roi_dict["height"], roi_dict["width"], pen=roi_dict["color"] + [roi_dict["x0"], roi_dict["y0"]], + roi_dict["height"], + roi_dict["width"], + pen=roi_dict["color"], ) cal.addScaleHandle([1, 1], [0, 0]) cal.addScaleHandle([0, 0], [1, 1]) @@ -335,7 +342,9 @@ def ratio_calibration(cali_1=True, cali_2=True, input_value=np.nan): cal1_value = self.calibration["1"]["value"] cal2_mean = self.calibration["2"]["mean_counts"] cal2_value = self.calibration["2"]["value"] - return (cal2_value - cal1_value) / (cal2_mean - cal1_mean) * (input_value - cal1_mean) + cal1_value + return (cal2_value - cal1_value) / (cal2_mean - cal1_mean) * ( + input_value - cal1_mean + ) + cal1_value elif cali_1: index = "1" @@ -366,7 +375,9 @@ def ratio_calibration(cali_1=True, cali_2=True, input_value=np.nan): for _data_index, _data in enumerate(self.data_dict["data"]): data_counts = np.nanmean(_data[y0 : y0 + height, x0 : x0 + width]) - real_data_counts = ratio_calibration(cali_1=cali_1, cali_2=cali_2, input_value=data_counts) + real_data_counts = ratio_calibration( + cali_1=cali_1, cali_2=cali_2, input_value=data_counts + ) item = QTableWidgetItem(f"{real_data_counts:.2f}") _measurement_data.append(real_data_counts) @@ -400,7 +411,9 @@ def display_measurement_profiles(self, refresh_calculation=True): for _index, _key in enumerate(self.measurement_dict.keys()): _data = self.measurement_dict[_key] - self.ui.measurement_view.plot(_data, name=f"Region {1+_index}", pen=_color_list[_index]) + self.ui.measurement_view.plot( + _data, name=f"Region {1 + _index}", pen=_color_list[_index] + ) self.ui.measurement_view.setLabel("bottom", "File Index") self.ui.measurement_view.setLabel("left", "Mean Counts Calibrated") @@ -484,7 +497,9 @@ def update_mean_counts(self, row=-1, all=False): def insert_measurement_roi_ui(self, row=-1): default_roi = self.default_measurement_roi new_roi = pg.RectROI( - [default_roi["x0"], default_roi["y0"]], [default_roi["height"], default_roi["width"]], pen="g" + [default_roi["x0"], default_roi["y0"]], + [default_roi["height"], default_roi["width"]], + pen="g", ) new_roi.addScaleHandle([1, 1], [0, 0]) new_roi.addScaleHandle([0, 0], [1, 1]) @@ -740,7 +755,10 @@ def cell_changed(self, row, col): def export_button_clicked(self): _export_folder = QFileDialog.getExistingDirectory( - self, directory=self.working_dir, caption="Select Output Folder", options=QFileDialog.ShowDirsOnly + self, + directory=self.working_dir, + caption="Select Output Folder", + options=QFileDialog.ShowDirsOnly, ) if _export_folder: o_export = ExportCalibration(parent=self, export_folder=_export_folder) @@ -776,26 +794,44 @@ def get_metadata(self): metadata.append("#Calibration Region 1:") metadata.append(f"# x0: {str(self.parent.ui.calibration1_x0.text())}") metadata.append(f"# y0: {str(self.parent.ui.calibration1_y0.text())}") - metadata.append(f"# width: {str(self.parent.ui.calibration1_width.text())}") - metadata.append(f"# height: {str(self.parent.ui.calibration1_height.text())}") - metadata.append(f"# file index: {str(self.parent.ui.calibration1_index.text())}") - metadata.append(f"# value requested: {str(self.parent.ui.calibration1_value.text())}") + metadata.append( + f"# width: {str(self.parent.ui.calibration1_width.text())}" + ) + metadata.append( + f"# height: {str(self.parent.ui.calibration1_height.text())}" + ) + metadata.append( + f"# file index: {str(self.parent.ui.calibration1_index.text())}" + ) + metadata.append( + f"# value requested: {str(self.parent.ui.calibration1_value.text())}" + ) if self.parent.ui.use_calibration2_checkbox.isChecked(): metadata.append("#Calibration Region 2:") metadata.append(f"# x0: {str(self.parent.ui.calibration2_x0.text())}") metadata.append(f"# y0: {str(self.parent.ui.calibration2_y0.text())}") - metadata.append(f"# width: {str(self.parent.ui.calibration2_width.text())}") - metadata.append(f"# height: {str(self.parent.ui.calibration2_height.text())}") - metadata.append(f"# file index: {str(self.parent.ui.calibration2_index.text())}") - metadata.append(f"# value requested: {str(self.parent.ui.calibration2_value.text())}") + metadata.append( + f"# width: {str(self.parent.ui.calibration2_width.text())}" + ) + metadata.append( + f"# height: {str(self.parent.ui.calibration2_height.text())}" + ) + metadata.append( + f"# file index: {str(self.parent.ui.calibration2_index.text())}" + ) + metadata.append( + f"# value requested: {str(self.parent.ui.calibration2_value.text())}" + ) nbr_measurement_region = self.parent.ui.tableWidget.rowCount() _legend = "#File_name, Time_stamp, Relative_time(s)" if nbr_measurement_region > 0: metadata.append("#Measurement Regions:") for _index_region in np.arange(nbr_measurement_region): [x0, y0, width, height] = self.parent.get_item_row(row=_index_region) - metadata.append(f"# region {_index_region}: [x0, y0, width, height]=[{x0}, {y0}, {width}, {height}]") - _legend += f", Mean_counts_of_region {_index_region+1}" + metadata.append( + f"# region {_index_region}: [x0, y0, width, height]=[{x0}, {y0}, {width}, {height}]" + ) + _legend += f", Mean_counts_of_region {_index_region + 1}" metadata.append("#") metadata.append(_legend) return metadata @@ -809,15 +845,26 @@ def run(self): for _row in np.arange(nbr_files): _row_str = [] for _col in np.arange(nbr_col): - _row_str.append(str(self.parent.ui.summary_table.item(_row, _col).text())) + _row_str.append( + str(self.parent.ui.summary_table.item(_row, _col).text()) + ) data.append(",".join(_row_str)) export_file_name = os.path.basename(self.parent.working_dir) - full_export_file_name = os.path.join(self.export_folder, export_file_name + "_calibrated_transmission.txt") + full_export_file_name = os.path.join( + self.export_folder, export_file_name + "_calibrated_transmission.txt" + ) - make_ascii_file(metadata=metadata, data=data, output_file_name=full_export_file_name, dim="1d") + make_ascii_file( + metadata=metadata, + data=data, + output_file_name=full_export_file_name, + dim="1d", + ) QApplication.processEvents() # display name of file exported for 10s - self.parent.ui.statusbar.showMessage(f"File Created: {full_export_file_name}", 10000) + self.parent.ui.statusbar.showMessage( + f"File Created: {full_export_file_name}", 10000 + ) diff --git a/notebooks/__code/circular_profile_of_a_ring/calculate_profiles.py b/notebooks/__code/circular_profile_of_a_ring/calculate_profiles.py index b138696a8..0cb9a4c14 100755 --- a/notebooks/__code/circular_profile_of_a_ring/calculate_profiles.py +++ b/notebooks/__code/circular_profile_of_a_ring/calculate_profiles.py @@ -47,7 +47,14 @@ def run(self): self.parent.eventProgress.setVisible(False) self.parent.ui.export_profiles_button.setEnabled(True) - def get_profile(self, image=None, angle_matrix=None, mask_ring=None, list_angles=None, profile_dictionary=None): + def get_profile( + self, + image=None, + angle_matrix=None, + mask_ring=None, + list_angles=None, + profile_dictionary=None, + ): # # create profile_dictionary # angle_bin = self.parent.angle_bin_horizontalSlider.value()/100 # @@ -60,7 +67,9 @@ def get_profile(self, image=None, angle_matrix=None, mask_ring=None, list_angles x_mask = mask_ring[1] for y, x in zip(y_mask, x_mask, strict=False): angle = angle_matrix[y, x] - bin_angle = CalculateProfiles.get_corresponding_bin_angle(angle=angle, list_angles=list_angles) + bin_angle = CalculateProfiles.get_corresponding_bin_angle( + angle=angle, list_angles=list_angles + ) # print(f"angle:{angle} -> bin_angle:{bin_angle}") profile_dictionary[bin_angle] = image[y, x] @@ -85,9 +94,14 @@ def calculate_matrices(self, display=True): # find all the pixels that are within the ring xv, yv = np.meshgrid(x, y) - distances_power = np.sqrt(np.power(yv - y_central_pixel, 2) + np.power(xv - x_central_pixel, 2)) + distances_power = np.sqrt( + np.power(yv - y_central_pixel, 2) + np.power(xv - x_central_pixel, 2) + ) - mask_ring = np.where((distances_power > inner_radius) & (distances_power < (inner_radius + thickness))) + mask_ring = np.where( + (distances_power > inner_radius) + & (distances_power < (inner_radius + thickness)) + ) # find angles of all the pixels angles_matrix = CalculateProfiles._build_angles_matrix( @@ -109,7 +123,9 @@ def calculate_matrices(self, display=True): return {"angles_matrix": ring_angles_matrix, "mask_ring": mask_ring} @staticmethod - def _build_angles_matrix(image_width=None, image_height=None, x_central_pixel=None, y_central_pixel=None): + def _build_angles_matrix( + image_width=None, image_height=None, x_central_pixel=None, y_central_pixel=None + ): full_angles_matrix = np.zeros((image_height, image_width)) # bottom right corner of matrix @@ -122,7 +138,10 @@ def _build_angles_matrix(image_width=None, image_height=None, x_central_pixel=No xv_right_bottom, yv_right_bottom = np.meshgrid(x_right_bottom, y_right_bottom) angles_right_bottom = 90 + np.rad2deg( - np.arctan((yv_right_bottom - y_central_pixel) / (xv_right_bottom - x_central_pixel)) + np.arctan( + (yv_right_bottom - y_central_pixel) + / (xv_right_bottom - x_central_pixel) + ) ) full_angles_matrix[ @@ -140,7 +159,9 @@ def _build_angles_matrix(image_width=None, image_height=None, x_central_pixel=No xv_right_top, yv_right_top = np.meshgrid(x_right_top, y_right_top) angles_right_top = 90 + np.rad2deg( - np.arctan((yv_right_top - y_central_pixel) / (xv_right_top - x_central_pixel)) + np.arctan( + (yv_right_top - y_central_pixel) / (xv_right_top - x_central_pixel) + ) ) full_angles_matrix[ @@ -157,7 +178,9 @@ def _build_angles_matrix(image_width=None, image_height=None, x_central_pixel=No xv_left_top, yv_left_top = np.meshgrid(x_left_top, y_left_top) - angles_left_top = 270 + np.rad2deg(np.arctan((yv_left_top - y_central_pixel) / (xv_left_top - x_central_pixel))) + angles_left_top = 270 + np.rad2deg( + np.arctan((yv_left_top - y_central_pixel) / (xv_left_top - x_central_pixel)) + ) full_angles_matrix[ int(y_left_top[0]) : int(y_left_top[0]) + len(y_left_top), int(x_left_top[0]) : int(x_left_top[0]) + len(x_left_top), @@ -173,7 +196,9 @@ def _build_angles_matrix(image_width=None, image_height=None, x_central_pixel=No xv_left_bottom, yv_left_bottom = np.meshgrid(x_left_bottom, y_left_bottom) angles_left_bottom = 270 + np.rad2deg( - np.arctan((yv_left_bottom - y_central_pixel) / (xv_left_bottom - x_central_pixel)) + np.arctan( + (yv_left_bottom - y_central_pixel) / (xv_left_bottom - x_central_pixel) + ) ) full_angles_matrix[ int(y_left_bottom[0]) : int(y_left_bottom[0]) + len(y_left_bottom), @@ -200,7 +225,9 @@ def plot_profiles(self): x_profile = self.parent.list_angles y_profile = _profile["y_profile"] - self.parent.profile_plot.axes.plot(x_profile, y_profile, plot_type, label=file) + self.parent.profile_plot.axes.plot( + x_profile, y_profile, plot_type, label=file + ) self.parent.profile_plot.axes.legend() self.parent.profile_plot.draw() # QtGui.QGuiApplication.processEvents() diff --git a/notebooks/__code/circular_profile_of_a_ring/configuration_handler.py b/notebooks/__code/circular_profile_of_a_ring/configuration_handler.py index 1a103aac8..2de51dc35 100755 --- a/notebooks/__code/circular_profile_of_a_ring/configuration_handler.py +++ b/notebooks/__code/circular_profile_of_a_ring/configuration_handler.py @@ -63,7 +63,10 @@ def load(self): self.parent.ui.statusbar.setStyleSheet("color: green") def save(self): - default_file_name = os.path.abspath(os.path.basename(self.parent.working_dir)) + "_configuration.cfg" + default_file_name = ( + os.path.abspath(os.path.basename(self.parent.working_dir)) + + "_configuration.cfg" + ) working_dir = os.path.dirname(self.parent.working_dir) directory = os.path.join(working_dir, default_file_name) config_file_name = QFileDialog.getSaveFileName( @@ -107,7 +110,13 @@ def get_config_dict(self): "thickness": ring_thickness, }, "angle_bin_size": angle_bin_size, - "grid": {"bin_size": bin_size, "red": red, "green": green, "blue": blue, "alpha": alpha}, + "grid": { + "bin_size": bin_size, + "red": red, + "green": green, + "blue": blue, + "alpha": alpha, + }, } return config_dict diff --git a/notebooks/__code/circular_profile_of_a_ring/event_handler.py b/notebooks/__code/circular_profile_of_a_ring/event_handler.py index 34ba4825a..5621dd82e 100755 --- a/notebooks/__code/circular_profile_of_a_ring/event_handler.py +++ b/notebooks/__code/circular_profile_of_a_ring/event_handler.py @@ -8,7 +8,9 @@ def __init__(self, parent=None): def list_images_right_click(self): menu = QMenu(self.parent) - display_selected_radiographs = menu.addAction("Display first radiograph selected") + display_selected_radiographs = menu.addAction( + "Display first radiograph selected" + ) menu.addSeparator() unselect_all = menu.addAction("Unselect all") action = menu.exec_(QtGui.QCursor.pos()) diff --git a/notebooks/__code/circular_profile_of_a_ring/export_profiles.py b/notebooks/__code/circular_profile_of_a_ring/export_profiles.py index 6f35d2116..35d3568c8 100755 --- a/notebooks/__code/circular_profile_of_a_ring/export_profiles.py +++ b/notebooks/__code/circular_profile_of_a_ring/export_profiles.py @@ -14,7 +14,9 @@ def __init__(self, parent=None): self.parent = parent def run(self): - default_file_name = os.path.abspath(os.path.basename(self.parent.working_dir)) + "_profiles.csv" + default_file_name = ( + os.path.abspath(os.path.basename(self.parent.working_dir)) + "_profiles.csv" + ) working_dir = os.path.dirname(self.parent.working_dir) directory = os.path.join(working_dir, default_file_name) export_file_name = QFileDialog.getSaveFileName( @@ -27,7 +29,9 @@ def run(self): if export_file_name[0]: export_file_name = export_file_name[0] - self.parent.ui.statusbar.showMessage(f"Saving {os.path.basename(export_file_name)} ... IN PROGRESS") + self.parent.ui.statusbar.showMessage( + f"Saving {os.path.basename(export_file_name)} ... IN PROGRESS" + ) self.parent.ui.statusbar.setStyleSheet("color: blue") QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) @@ -41,21 +45,28 @@ def run(self): metadata = [f"# working dir: {os.path.abspath(self.parent.working_dir)}"] metadata.append( "# ring central pixel (x, y):({}, {})".format( - dict_config["ring"]["central_pixel"]["x"], dict_config["ring"]["central_pixel"]["y"] + dict_config["ring"]["central_pixel"]["x"], + dict_config["ring"]["central_pixel"]["y"], ) ) - metadata.append("# ring inner radius (pixel): {}".format(dict_config["ring"]["radius"])) - metadata.append("# ring thickness (pixel): {}".format(dict_config["ring"]["thickness"])) + metadata.append( + "# ring inner radius (pixel): {}".format(dict_config["ring"]["radius"]) + ) + metadata.append( + "# ring thickness (pixel): {}".format(dict_config["ring"]["thickness"]) + ) metadata.append("# Label of columns:") nbr_row = len(dict_profile.keys()) list_files = self.parent.list_short_files for _row in np.arange(nbr_row): _file = list_files[_row] - metadata.append(f"# column {_row+1}: {_file}") + metadata.append(f"# column {_row + 1}: {_file}") metadata.append("#") - metadata.append("Angle from top vertical (degrees), mean counts (see label of columns above)") + metadata.append( + "Angle from top vertical (degrees), mean counts (see label of columns above)" + ) data = [] list_angles = self.parent.list_angles @@ -67,9 +78,16 @@ def run(self): _str_entry = ", ".join(_entry) data.append(_str_entry) - make_ascii_file(metadata=metadata, data=data, output_file_name=export_file_name, dim="1d") + make_ascii_file( + metadata=metadata, + data=data, + output_file_name=export_file_name, + dim="1d", + ) - self.parent.ui.statusbar.showMessage(f"{os.path.basename(export_file_name)} ... Saved!", 10000) + self.parent.ui.statusbar.showMessage( + f"{os.path.basename(export_file_name)} ... Saved!", 10000 + ) self.parent.ui.statusbar.setStyleSheet("color: green") QApplication.restoreOverrideCursor() diff --git a/notebooks/__code/circular_profile_of_a_ring/interface_handler.py b/notebooks/__code/circular_profile_of_a_ring/interface_handler.py index 90620e254..f33e8e795 100755 --- a/notebooks/__code/circular_profile_of_a_ring/interface_handler.py +++ b/notebooks/__code/circular_profile_of_a_ring/interface_handler.py @@ -25,7 +25,9 @@ class InterfaceHandler: def __init__(self, working_dir=None, o_norm=None): o_interface = Interface( - data=o_norm.data["sample"]["data"], list_files=o_norm.data["sample"]["file_name"], working_dir=working_dir + data=o_norm.data["sample"]["data"], + list_files=o_norm.data["sample"]["file_name"], + working_dir=working_dir, ) o_interface.show() self.o_interface = o_interface @@ -126,7 +128,12 @@ def click_on_profile_plot(self, event): _pen.setWidthF(0.01) self.angle_line = pg.InfiniteLine( - [x0, y0], pen=_pen, label=f"{angle:.2f}", angle=angle - 90.0, span=(0, 1), labelOpts={"position": 0.9} + [x0, y0], + pen=_pen, + label=f"{angle:.2f}", + angle=angle - 90.0, + span=(0, 1), + labelOpts={"position": 0.9}, ) # self.angle_line.addMarker('o', size=15) self.ui.image_view.addItem(self.angle_line) @@ -147,9 +154,13 @@ def init_crosshair(self): self.hLine = pg.InfiniteLine(pos=y0, angle=0, movable=True) self.vLine.sigDragged.connect(self.manual_circle_center_changed) - self.vLine.sigPositionChangeFinished.connect(self.manual_circle_center_changed_finished) + self.vLine.sigPositionChangeFinished.connect( + self.manual_circle_center_changed_finished + ) self.hLine.sigDragged.connect(self.manual_circle_center_changed) - self.hLine.sigPositionChangeFinished.connect(self.manual_circle_center_changed_finished) + self.hLine.sigPositionChangeFinished.connect( + self.manual_circle_center_changed_finished + ) self.ui.image_view.addItem(self.vLine, ignoreBounds=False) self.ui.image_view.addItem(self.hLine, ignoreBounds=False) @@ -189,7 +200,9 @@ def init_widgets(self): max_ring_value = self.width default_inner_ring_value = int(self.width / 4) default_ring_thickness = 100 - self.ui.ring_inner_radius_slider.setMaximum(max_ring_value * 100) # *100 because slider is int + self.ui.ring_inner_radius_slider.setMaximum( + max_ring_value * 100 + ) # *100 because slider is int self.ui.ring_inner_radius_slider.setValue(default_inner_ring_value * 100) self.ui.ring_inner_radius_doubleSpinBox.setMaximum(max_ring_value) self.ui.ring_inner_radius_doubleSpinBox.setSingleStep(0.01) @@ -268,12 +281,20 @@ def display_grid(self): ) lines = np.array( [line_color for n in np.arange(len(pos))], - dtype=[("red", np.ubyte), ("green", np.ubyte), ("blue", np.ubyte), ("alpha", np.ubyte), ("width", float)], + dtype=[ + ("red", np.ubyte), + ("green", np.ubyte), + ("blue", np.ubyte), + ("alpha", np.ubyte), + ("width", float), + ], ) line_view_binning = pg.GraphItem() self.ui.image_view.addItem(line_view_binning) - line_view_binning.setData(pos=pos, adj=adj, pen=lines, symbol=None, pxMode=False) + line_view_binning.setData( + pos=pos, adj=adj, pen=lines, symbol=None, pxMode=False + ) self.line_view_binning = line_view_binning @@ -316,11 +337,16 @@ def display_inner_and_outer_ring( if self.inner_ring_roi: self.ui.image_view.removeItem(self.inner_ring_roi) self.inner_ring_roi = pg.CircleROI( - [inner_x0, inner_y0], [inner_width_and_height, inner_width_and_height], movable=True, pen=self.ring_pen + [inner_x0, inner_y0], + [inner_width_and_height, inner_width_and_height], + movable=True, + pen=self.ring_pen, ) self.ui.image_view.addItem(self.inner_ring_roi) self.inner_ring_roi.sigRegionChanged.connect(self.manual_inner_ring_changed) - self.inner_ring_roi.sigRegionChangeFinished.connect(self.manual_inner_ring_change_finished) + self.inner_ring_roi.sigRegionChangeFinished.connect( + self.manual_inner_ring_change_finished + ) if self.outer_ring_roi: self.ui.image_view.removeItem(self.outer_ring_roi) @@ -333,7 +359,9 @@ def display_inner_and_outer_ring( ) self.remove_handles(ring_ui=self.outer_ring_roi) self.outer_ring_roi.sigRegionChanged.connect(self.manual_outer_ring_changed) - self.outer_ring_roi.sigRegionChangeFinished.connect(self.manual_outer_ring_change_finished) + self.outer_ring_roi.sigRegionChangeFinished.connect( + self.manual_outer_ring_change_finished + ) def remove_handles(self, ring_ui=None): self.ui.image_view.addItem(ring_ui) @@ -353,10 +381,15 @@ def manual_inner_ring_change_finished(self): def manual_inner_ring_changed(self): self.ui.image_view.removeItem(self.outer_ring_roi) - list_ui = [self.ui.ring_inner_radius_doubleSpinBox, self.ui.ring_inner_radius_slider] + list_ui = [ + self.ui.ring_inner_radius_doubleSpinBox, + self.ui.ring_inner_radius_slider, + ] self.block_signals(list_ui=list_ui, block_status=True) - region = self.inner_ring_roi.getArraySlice(self.current_live_image, self.ui.image_view.imageItem) + region = self.inner_ring_roi.getArraySlice( + self.current_live_image, self.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop y0 = region[0][1].start @@ -373,7 +406,9 @@ def manual_inner_ring_changed(self): if y1 + thickness >= self.height: self.replot_inner_ring(x0=x0, y0=y0, x1=x1, y1=y1) - region = self.inner_ring_roi.getArraySlice(self.current_live_image, self.ui.image_view.imageItem) + region = self.inner_ring_roi.getArraySlice( + self.current_live_image, self.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop y0 = region[0][1].start @@ -393,7 +428,10 @@ def manual_inner_ring_changed(self): ring_thickness = self.ui.ring_thickness_doubleSpinBox.value() outer_ring_circle_width = 2 * (ring_radius + ring_thickness) self.outer_ring_roi = pg.CircleROI( - [x_central_pixel - ring_radius - ring_thickness, y_central_pixel - ring_radius - ring_thickness], + [ + x_central_pixel - ring_radius - ring_thickness, + y_central_pixel - ring_radius - ring_thickness, + ], [outer_ring_circle_width, outer_ring_circle_width], movable=True, resizable=False, @@ -402,7 +440,9 @@ def manual_inner_ring_changed(self): self.ui.image_view.addItem(self.outer_ring_roi) self.remove_handles(ring_ui=self.outer_ring_roi) self.outer_ring_roi.sigRegionChanged.connect(self.manual_outer_ring_changed) - self.outer_ring_roi.sigRegionChangeFinished.connect(self.manual_outer_ring_change_finished) + self.outer_ring_roi.sigRegionChangeFinished.connect( + self.manual_outer_ring_change_finished + ) self.vLine.setValue(x_central_pixel) self.hLine.setValue(y_central_pixel) @@ -417,7 +457,10 @@ def replot_outer_ring(self, x0=None, y0=None, x1=None, y1=None): outer_ring_circle_width = 2 * (ring_radius + ring_thickness) self.ui.image_view.removeItem(self.outer_ring_roi) self.outer_ring_roi = pg.CircleROI( - [x_central_pixel - ring_radius - ring_thickness, y_central_pixel - ring_radius - ring_thickness], + [ + x_central_pixel - ring_radius - ring_thickness, + y_central_pixel - ring_radius - ring_thickness, + ], [outer_ring_circle_width, outer_ring_circle_width], movable=True, resizable=False, @@ -458,7 +501,9 @@ def manual_outer_ring_changed(self): self.block_signals(list_ui=list_ui, block_status=True) # outer ring - region = self.outer_ring_roi.getArraySlice(self.current_live_image, self.ui.image_view.imageItem) + region = self.outer_ring_roi.getArraySlice( + self.current_live_image, self.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop y0 = region[0][1].start @@ -516,7 +561,9 @@ def manual_ring_changed(self): self.block_signals(list_ui=list_ui, block_status=True) # inner ring - region = self.inner_ring_roi.getArraySlice(self.current_live_image, self.ui.image_view.imageItem) + region = self.inner_ring_roi.getArraySlice( + self.current_live_image, self.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop y0 = region[0][1].start @@ -533,7 +580,9 @@ def manual_ring_changed(self): if y1 + thickness >= self.height: self.replot_inner_ring(x0=x0, y0=y0, x1=x1, y1=y1) - region = self.inner_ring_roi.getArraySlice(self.current_live_image, self.ui.image_view.imageItem) + region = self.inner_ring_roi.getArraySlice( + self.current_live_image, self.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop # y0 = region[0][1].start @@ -541,7 +590,9 @@ def manual_ring_changed(self): radius_1 = int(x1 - x0) / 2 # outer ring - region = self.outer_ring_roi.getArraySlice(self.current_live_image, self.ui.image_view.imageItem) + region = self.outer_ring_roi.getArraySlice( + self.current_live_image, self.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop radius_2 = int(x1 - x0) / 2 @@ -751,7 +802,9 @@ def angle_cursor_dial_moved(self, value): value = Interface.format_angle_degrees(value) right_comma_value = self.ui.angle_cursor_dial_2.value() - right_comma_formatted = Interface.format_angle_minutes(right_comma_value) / 100.0 + right_comma_formatted = ( + Interface.format_angle_minutes(right_comma_value) / 100.0 + ) full_value = value + right_comma_formatted self.ui.angle_cursor_value.setText(str(full_value)) @@ -760,7 +813,9 @@ def angle_cursor_dial_moved(self, value): def angle_cursor_dial2_moved(self, value): value = Interface.format_angle_minutes(value) - left_comma_value = Interface.format_angle_degrees(self.ui.angle_cursor_dial.value()) + left_comma_value = Interface.format_angle_degrees( + self.ui.angle_cursor_dial.value() + ) full_value = float(left_comma_value + value / 100.0) self.ui.angle_cursor_value.setText(str(full_value)) diff --git a/notebooks/__code/combine_folders.py b/notebooks/__code/combine_folders.py index aaaf32287..1d842cd2c 100755 --- a/notebooks/__code/combine_folders.py +++ b/notebooks/__code/combine_folders.py @@ -85,7 +85,8 @@ def stop_selecting_folders(self, value): else: display( HTML( - 'Folders must contain the same number' " of images!" + 'Folders must contain the same number' + " of images!" ) ) @@ -147,7 +148,9 @@ def __get_full_list_files(self, folder=""): list_format_to_check = ["tif*", "fits", "txt"] full_list_files_dict = {} for _format in list_format_to_check: - full_list_files_dict[_format] = self.__get_list_files(file_format=_format, folder=folder)["list_files"] + full_list_files_dict[_format] = self.__get_list_files( + file_format=_format, folder=folder + )["list_files"] return full_list_files_dict def check_number_of_files(self, list_folders): @@ -161,7 +164,9 @@ def check_number_of_files(self, list_folders): nbr_files = {} file_format = "" for _folder in self.list_folders: - _local_list_files_dict = self.__get_list_files(file_format=file_format, folder=_folder) + _local_list_files_dict = self.__get_list_files( + file_format=file_format, folder=_folder + ) _list_files = _local_list_files_dict["list_files"] _list_files.sort() @@ -200,9 +205,19 @@ def check_number_of_files(self, list_folders): '15px; color:green">' + _format + "" ) ) - display(HTML(' List of folders selected: ')) + display( + HTML( + ' List of folders selected: ' + ) + ) for _folder in self.list_folders: - display(HTML(' * ' + _folder + "")) + display( + HTML( + ' * ' + + _folder + + "" + ) + ) self.nbr_files_in_each_folder = list(values)[0] @@ -211,11 +226,15 @@ def check_number_of_files(self, list_folders): def how_many_folders(self): nbr_folder = len(self.global_list_of_folders_to_combine) radio_list_string = [str(_index) for _index in np.arange(2, nbr_folder + 1)] - self.bin_size = widgets.RadioButtons(options=radio_list_string, value=radio_list_string[0]) + self.bin_size = widgets.RadioButtons( + options=radio_list_string, value=radio_list_string[0] + ) display(self.bin_size) def how_to_combine(self): - self.combine_method = widgets.RadioButtons(options=["add", "mean", "median"], value="add") + self.combine_method = widgets.RadioButtons( + options=["add", "mean", "median"], value="add" + ) display(self.combine_method) def extra_files(self): @@ -263,7 +282,9 @@ def merging(self, output_folder): elif merging_algo == "median": algorithm = self.__median else: - raise NotImplementedError(f"Algorithm {merging_algo} has not been implemented yet!") + raise NotImplementedError( + f"Algorithm {merging_algo} has not been implemented yet!" + ) logging.info(f"-> merging algorithm: {algorithm}") @@ -276,14 +297,18 @@ def merging(self, output_folder): self.merginc_dict_debugging = merging_dict # create final list of files to merge - final_dict_of_files_to_merge = self.__create_dict_of_files_to_merge(merging_dict) + final_dict_of_files_to_merge = self.__create_dict_of_files_to_merge( + merging_dict + ) self.final_dict_of_files_to_merge_debugging = final_dict_of_files_to_merge final_nbr_folders = len(merging_dict.keys()) folder_level_ui = widgets.HBox( [ widgets.Label("Folder Progress:", layout=widgets.Layout(width="20%")), - widgets.IntProgress(max=final_nbr_folders, layout=widgets.Layout(width="50%")), + widgets.IntProgress( + max=final_nbr_folders, layout=widgets.Layout(width="50%") + ), ] ) display(folder_level_ui) @@ -293,17 +318,25 @@ def merging(self, output_folder): file_level_ui = widgets.HBox( [ widgets.Label("File Progress:", layout=widgets.Layout(width="20%")), - widgets.IntProgress(max=nbr_files_to_merge, layout=widgets.Layout(width="50%")), + widgets.IntProgress( + max=nbr_files_to_merge, layout=widgets.Layout(width="50%") + ), ] ) display(file_level_ui) w2 = file_level_ui.children[1] - for _index_final_folder, _final_folder in enumerate(final_dict_of_files_to_merge.keys()): - file_handler.make_or_reset_folder(os.path.join(output_folder, _final_folder)) + for _index_final_folder, _final_folder in enumerate( + final_dict_of_files_to_merge.keys() + ): + file_handler.make_or_reset_folder( + os.path.join(output_folder, _final_folder) + ) list_files_to_merge = final_dict_of_files_to_merge[_final_folder] - for _index_files_to_merge, _files_to_merge in enumerate(list_files_to_merge): + for _index_files_to_merge, _files_to_merge in enumerate( + list_files_to_merge + ): _files_to_merge = [_file for _file in _files_to_merge] self.files_to_merge_for_testing = _files_to_merge o_load = Normalization() @@ -313,7 +346,9 @@ def merging(self, output_folder): self.combined_data_for_testing = combined_data _base_name_file = os.path.basename(_files_to_merge[0]) - output_file_name = os.path.join(output_folder, _final_folder, _base_name_file) + output_file_name = os.path.join( + output_folder, _final_folder, _base_name_file + ) logging.info(f"_final_folder: {_final_folder}") logging.info(f"_base_name_file: {_base_name_file}") @@ -321,7 +356,9 @@ def merging(self, output_folder): w2.value = _index_files_to_merge + 1 if self.keep_extra_files.value == "yes": - self.move_extra_files_to_output_folder(output_folder=os.path.join(output_folder, _final_folder)) + self.move_extra_files_to_output_folder( + output_folder=os.path.join(output_folder, _final_folder) + ) w1.value = _index_final_folder + 1 @@ -348,7 +385,9 @@ def __create_dict_of_files_to_merge(self, merging_dict): _tmp_list_files_to_merge = [] for _folder in _list_folders_to_add: _tmp_list_files_to_merge.append(list_files_dict[_folder]) - final_dict_of_files_to_merge[_key] = list(zip(*_tmp_list_files_to_merge, strict=False)) + final_dict_of_files_to_merge[_key] = list( + zip(*_tmp_list_files_to_merge, strict=False) + ) return final_dict_of_files_to_merge diff --git a/notebooks/__code/combine_images.py b/notebooks/__code/combine_images.py index e7a040b45..2da148728 100755 --- a/notebooks/__code/combine_images.py +++ b/notebooks/__code/combine_images.py @@ -20,9 +20,9 @@ def __init__(self, working_dir=""): def select_files(self): self.files_list_widget = fileselector.FileSelectorPanel( - instruction="select files to combine", - start_dir=self.input_working_dir, - multiple=True + instruction="select files to combine", + start_dir=self.input_working_dir, + multiple=True, ) self.files_list_widget.show() @@ -45,7 +45,8 @@ def how_to_combine(self): ) self.combine_method = widgets.RadioButtons( - options=["add", "arithmetic mean", "geometric mean", "median"], value="arithmetic mean" + options=["add", "arithmetic mean", "geometric mean", "median"], + value="arithmetic mean", ) vertical = widgets.VBox([alge_box, geo_box, self.combine_method]) @@ -80,17 +81,23 @@ def define_output_filename(self): short_list_files = [os.path.basename(_file) for _file in list_files] merging_algo = self.__get_formated_merging_algo_name() - [default_new_name, ext] = self.__create_merged_file_name(list_files_names=short_list_files) + [default_new_name, ext] = self.__create_merged_file_name( + list_files_names=short_list_files + ) display(widgets.HTML(value="
")) - top_label = widgets.Label("Define the new output file name", layout=widgets.Layout(width="100%")) + top_label = widgets.Label( + "Define the new output file name", layout=widgets.Layout(width="100%") + ) folder_name = os.path.dirname(list_files[0]) box1 = widgets.HBox( [ - widgets.Label("Original folder name", layout=widgets.Layout(width="20%")), + widgets.Label( + "Original folder name", layout=widgets.Layout(width="20%") + ), widgets.Label(f"{folder_name}", layout=widgets.Layout(width="80%")), ] ) @@ -99,8 +106,14 @@ def define_output_filename(self): box = widgets.HBox( [ widgets.Label("File Name", layout=widgets.Layout(width="20%")), - widgets.Text("", placeholder="Your file name here", layout=widgets.Layout(width="60%")), - widgets.Label(f"_{merging_algo}{ext}", layout=widgets.Layout(width="20%")), + widgets.Text( + "", + placeholder="Your file name here", + layout=widgets.Layout(width="60%"), + ), + widgets.Label( + f"_{merging_algo}{ext}", layout=widgets.Layout(width="20%") + ), ] ) self.default_filename_ui = box.children[1] @@ -137,7 +150,10 @@ def merging(self): _metadata = o_load.data["sample"]["metadata"] merging_ui = widgets.HBox( - [widgets.Label("Merging Progress", layout=widgets.Layout(width="20%")), widgets.IntProgress(max=2)] + [ + widgets.Label("Merging Progress", layout=widgets.Layout(width="20%")), + widgets.IntProgress(max=2), + ] ) display(merging_ui) w1 = merging_ui.children[1] @@ -149,7 +165,9 @@ def merging(self): _new_name = self.default_filename_ui.value + self.ext_ui.value output_file_name = os.path.join(output_folder, _new_name) - file_handler.save_data(data=combined_data, filename=output_file_name, metadata=_metadata[0]) + file_handler.save_data( + data=combined_data, filename=output_file_name, metadata=_metadata[0] + ) w1.value = 2 @@ -160,7 +178,13 @@ def merging(self): + "" ) ) - display(HTML('In Folder: ' + output_folder + "")) + display( + HTML( + 'In Folder: ' + + output_folder + + "" + ) + ) def __create_merged_file_name(self, list_files_names=[]): """Create the new base name using a combine name of all the input file diff --git a/notebooks/__code/combine_images_n_by_n/combine_images_n_by_n.py b/notebooks/__code/combine_images_n_by_n/combine_images_n_by_n.py index b38c84f3c..935d6e48d 100755 --- a/notebooks/__code/combine_images_n_by_n/combine_images_n_by_n.py +++ b/notebooks/__code/combine_images_n_by_n/combine_images_n_by_n.py @@ -37,15 +37,21 @@ def __init__(self, working_dir=""): self.timespectra_file_name = None def select_images(self): - o_file_browser = FileFolderBrowser(working_dir=self.working_dir, next_function=self.post_select_images) + o_file_browser = FileFolderBrowser( + working_dir=self.working_dir, next_function=self.post_select_images + ) self.list_files_selected = o_file_browser.select_images_with_search( - instruction="Select images to combine", multiple_flag=True, filters={"TIFF": "*.tif*", "FITS": "*.fits"} + instruction="Select images to combine", + multiple_flag=True, + filters={"TIFF": "*.tif*", "FITS": "*.fits"}, ) def post_select_images(self, list_of_images): if list_of_images: self.input_folder_selected = os.path.dirname(list_of_images[0]) - self.base_working_dir = str(PurePath(Path(self.input_folder_selected).parent).name) + self.base_working_dir = str( + PurePath(Path(self.input_folder_selected).parent).name + ) self.list_files = list_of_images # def select_folder(self): @@ -90,7 +96,9 @@ def sort_by_index(self): list_index = list(dict_list_files_by_index.keys()) list_index.sort() - list_files_sorted_by_index = [dict_list_files_by_index[_key] for _key in list_index] + list_files_sorted_by_index = [ + dict_list_files_by_index[_key] for _key in list_index + ] return list_files_sorted_by_index def sorting_the_files(self): @@ -102,24 +110,36 @@ def sorting_the_files(self): index = split_name[-1] # tab 1 - full name - list_files_base_name_only = [os.path.basename(_file) for _file in self.list_files] + list_files_base_name_only = [ + os.path.basename(_file) for _file in self.list_files + ] vertical_layout_tab1 = widgets.VBox( [ - widgets.HTML(value=f"{base_name}{ext}", layout=widgets.Layout(width="100%")), - widgets.Select(options=list_files_base_name_only, layout=widgets.Layout(width="100%", height="700px")), + widgets.HTML( + value=f"{base_name}{ext}", + layout=widgets.Layout(width="100%"), + ), + widgets.Select( + options=list_files_base_name_only, + layout=widgets.Layout(width="100%", height="700px"), + ), ] ) # tab 2 - file_index self.list_files_by_index = self.sort_by_index() - list_files_by_index_base_name_only = [os.path.basename(_file) for _file in self.list_files_by_index] + list_files_by_index_base_name_only = [ + os.path.basename(_file) for _file in self.list_files_by_index + ] vertical_layout_tab2 = widgets.VBox( [ widgets.HTML( - value=f"{base_name_before_index}_{index}{ext}", layout=widgets.Layout(width="100%") + value=f"{base_name_before_index}_{index}{ext}", + layout=widgets.Layout(width="100%"), ), widgets.Select( - options=list_files_by_index_base_name_only, layout=widgets.Layout(width="100%", height="700px") + options=list_files_by_index_base_name_only, + layout=widgets.Layout(width="100%", height="700px"), ), ] ) @@ -158,7 +178,8 @@ def how_to_combine(self): ) self.combine_method = widgets.RadioButtons( - options=["add", "arithmetic mean", "geometric mean", "median"], value="arithmetic mean" + options=["add", "arithmetic mean", "geometric mean", "median"], + value="arithmetic mean", ) vertical = widgets.VBox([alge_box, geo_box, self.combine_method]) @@ -305,9 +326,13 @@ def get_merging_algorithm_name(self): def preview_result(self): how_to_rename_layout = widgets.VBox( [ - widgets.Label("How to name output files:", layout=widgets.Layout(width="100%")), + widgets.Label( + "How to name output files:", layout=widgets.Layout(width="100%") + ), widgets.RadioButtons( - options=RENAMING_OPTIONS, layout=widgets.Layout(width="100%"), value=RENAMING_OPTIONS[-1] + options=RENAMING_OPTIONS, + layout=widgets.Layout(width="100%"), + value=RENAMING_OPTIONS[-1], ), ] ) @@ -319,21 +344,27 @@ def preview_result(self): # tab1 list_groups = list(self.dict_list_files.keys()) - self.group_dropdown = widgets.Dropdown(options=list_groups, description="Groups") + self.group_dropdown = widgets.Dropdown( + options=list_groups, description="Groups" + ) self.list_files_per_group = widgets.Select( options=self.dict_list_files[list_groups[0]], description="Files", layout=widgets.Layout(width="100%", height="400px"), ) - new_file_name_label = widgets.Label("Output file name:", layout=widgets.Layout(width="150px", height="80px")) + new_file_name_label = widgets.Label( + "Output file name:", layout=widgets.Layout(width="150px", height="80px") + ) self.new_file_name = widgets.Label( - self.dict_list_new_files[0], layout=widgets.Layout(width="400px", height="80px") + self.dict_list_new_files[0], + layout=widgets.Layout(width="400px", height="80px"), ) hori1 = widgets.HBox([new_file_name_label, self.new_file_name]) vbox1 = widgets.VBox( - [self.group_dropdown, self.list_files_per_group, hori1], layout=widgets.Layout(height="600px") + [self.group_dropdown, self.list_files_per_group, hori1], + layout=widgets.Layout(height="600px"), ) accordion_widgets = [vbox1] @@ -341,14 +372,17 @@ def preview_result(self): # tab2 if len(self.bad_dict_list_files.keys()) > 0: list_groups = list(self.bad_dict_list_files.keys()) - self.bad_group_dropdown = widgets.Dropdown(options=list_groups, description="Bad Groups") + self.bad_group_dropdown = widgets.Dropdown( + options=list_groups, description="Bad Groups" + ) self.bad_list_files_per_group = widgets.Select( options=self.bad_dict_list_files[list_groups[0]], description="Files", layout=widgets.Layout(width="100%", height="500px"), ) vbox2 = widgets.VBox( - [self.bad_group_dropdown, self.bad_list_files_per_group], layout=widgets.Layout(height="600px") + [self.bad_group_dropdown, self.bad_list_files_per_group], + layout=widgets.Layout(height="600px"), ) self.bad_group_dropdown.observe(self.bad_group_changed, names="value") accordion_widgets.append(vbox2) @@ -394,7 +428,9 @@ def create_dictionary_of_new_file_names(self): for _key in dict_list_files.keys(): list_files = dict_list_files[_key] base_list_files = [os.path.basename(_file) for _file in list_files] - _common_part = get_beginning_common_part_of_string_from_list(list_of_text=base_list_files) + _common_part = get_beginning_common_part_of_string_from_list( + list_of_text=base_list_files + ) new_file_name = _common_part + f"_{_key:03d}.tiff" dict_list_new_files[_key] = new_file_name @@ -443,7 +479,11 @@ def merging(self, output_folder): horizontal_layout = widgets.HBox( [ widgets.Label("Merging Progress", layout=widgets.Layout(width="20%")), - widgets.IntProgress(max=len(dict_list_files.keys()), value=0, layout=widgets.Layout(width="50%")), + widgets.IntProgress( + max=len(dict_list_files.keys()), + value=0, + layout=widgets.Layout(width="50%"), + ), ] ) global_slider = horizontal_layout.children[1] @@ -456,7 +496,9 @@ def merging(self, output_folder): bin_value=self.bin_value, algo_name=algo_name, ) - output_folder_name = make_or_increment_folder_name(folder_name=output_folder_name) + output_folder_name = make_or_increment_folder_name( + folder_name=output_folder_name + ) output_timespectra_file_name = os.path.join( output_folder_name, CombineImagesNByN.__create_timestamp_file_name() @@ -511,7 +553,10 @@ def merging(self, output_folder): @staticmethod def combine_timespectra( - input_timespectra_file_name=None, output_timespectra_file_name=None, bin_value=2, merging_algorithm=None + input_timespectra_file_name=None, + output_timespectra_file_name=None, + bin_value=2, + merging_algorithm=None, ): if input_timespectra_file_name is None: return @@ -531,18 +576,27 @@ def combine_timespectra( working_count_axis_to_bin = data[index : index + bin_value, 1] time_axis_binned.append( - CombineImagesNByN.merging_algorithm(CombineImagesNByN.arithmetic_mean, working_time_axis_to_bin) + CombineImagesNByN.merging_algorithm( + CombineImagesNByN.arithmetic_mean, working_time_axis_to_bin + ) + ) + count_axis_binned.append( + CombineImagesNByN.merging_algorithm( + merging_algorithm, working_count_axis_to_bin + ) ) - count_axis_binned.append(CombineImagesNByN.merging_algorithm(merging_algorithm, working_count_axis_to_bin)) new_timespectra = list(zip(time_axis_binned, count_axis_binned, strict=False)) np.savetxt(output_timespectra_file_name, new_timespectra, delimiter="\t") @staticmethod - def __create_output_folder_name(output_folder="./", base_file_name="", bin_value=2, algo_name="add"): + def __create_output_folder_name( + output_folder="./", base_file_name="", bin_value=2, algo_name="add" + ): output_folder = os.path.abspath(output_folder) output_folder_name = os.path.join( - output_folder, f"{base_file_name}_files_combined_by_{bin_value:d}_{algo_name}" + output_folder, + f"{base_file_name}_files_combined_by_{bin_value:d}_{algo_name}", ) return output_folder_name diff --git a/notebooks/__code/combine_images_without_outliers/combine_images.py b/notebooks/__code/combine_images_without_outliers/combine_images.py index 282f6b417..9004358e4 100755 --- a/notebooks/__code/combine_images_without_outliers/combine_images.py +++ b/notebooks/__code/combine_images_without_outliers/combine_images.py @@ -9,7 +9,10 @@ from __code._utilities.string import get_beginning_common_part_of_string_from_list from __code.file_folder_browser import FileFolderBrowser -from __code.file_handler import make_or_reset_folder, retrieve_list_of_most_dominant_extension_from_folder +from __code.file_handler import ( + make_or_reset_folder, + retrieve_list_of_most_dominant_extension_from_folder, +) from __code.ipywe import fileselector @@ -84,9 +87,13 @@ def select_data_folder_to_combine(self): select_data.show() def preview_combine_result(self, data_folder): - list_of_input_filenames = retrieve_list_of_most_dominant_extension_from_folder(folder=data_folder)[0] + list_of_input_filenames = retrieve_list_of_most_dominant_extension_from_folder( + folder=data_folder + )[0] self.input_folder = str(Path(list_of_input_filenames[0]).parent) - list_of_input_filenames = [os.path.basename(_file) for _file in list_of_input_filenames] + list_of_input_filenames = [ + os.path.basename(_file) for _file in list_of_input_filenames + ] self.list_of_input_filenames = list_of_input_filenames display( @@ -99,7 +106,8 @@ def preview_combine_result(self, data_folder): verti_layout = widgets.VBox( [ widgets.SelectMultiple( - options=list_of_input_filenames, layout=widgets.Layout(width="100%", height="300px") + options=list_of_input_filenames, + layout=widgets.Layout(width="100%", height="300px"), ) ] ) @@ -109,8 +117,14 @@ def preview_combine_result(self, data_folder): hori_layout = widgets.HBox( [ - widgets.Label("Prefix of new filename will be ->", layout=widgets.Layout(width="20%")), - widgets.Label("Not Enough images selected to correctly combine!", layout=widgets.Layout(width="80%")), + widgets.Label( + "Prefix of new filename will be ->", + layout=widgets.Layout(width="20%"), + ), + widgets.Label( + "Not Enough images selected to correctly combine!", + layout=widgets.Layout(width="80%"), + ), ] ) display(hori_layout) @@ -121,26 +135,37 @@ def preview_combine_result(self, data_folder): new_name_verti_layout = widgets.VBox( [ widgets.Label("New list of files"), - widgets.Select(options=[], layout=widgets.Layout(width="100%", height=select_height)), + widgets.Select( + options=[], + layout=widgets.Layout(width="100%", height=select_height), + ), ], layout=widgets.Layout(width=select_width), ) - new_name_verti_layout.children[1].observe(self.new_list_of_files_selection_changed, names="value") + new_name_verti_layout.children[1].observe( + self.new_list_of_files_selection_changed, names="value" + ) old_name_verti_layout = widgets.VBox( [ widgets.Label("Corresponding combined files"), - widgets.Select(options=[], layout=widgets.Layout(width="100%", height=select_height)), + widgets.Select( + options=[], + layout=widgets.Layout(width="100%", height=select_height), + ), ], layout=widgets.Layout(width=select_width), ) hori_layout_2 = widgets.HBox( - [new_name_verti_layout, widgets.Label(" "), old_name_verti_layout], layout=widgets.Layout(width="900px") + [new_name_verti_layout, widgets.Label(" "), old_name_verti_layout], + layout=widgets.Layout(width="900px"), ) display(hori_layout_2) self.hori_layout_2 = hori_layout_2 hori_layout_2.layout.visibility = self.get_preview_visibility() - self.final_list_of_files_combined_renamed_widget = new_name_verti_layout.children[1] + self.final_list_of_files_combined_renamed_widget = ( + new_name_verti_layout.children[1] + ) self.corresponding_list_of_files_combined = old_name_verti_layout.children[1] def get_preview_visibility(self): @@ -155,7 +180,9 @@ def input_selection_changed(self, value): message = "Not Enough images selected to correctly combine!" self.block_preview = True else: - message = get_beginning_common_part_of_string_from_list(list_of_text=list_of_files_selected) + message = get_beginning_common_part_of_string_from_list( + list_of_text=list_of_files_selected + ) self.block_preview = False self.prefix_filename_widget.value = message self.hori_layout_2.layout.visibility = self.get_preview_visibility() @@ -188,7 +215,9 @@ def update_result_of_combination_summary(self): _index += 1 self.full_combine_dict = full_combine_dict - self.final_list_of_files_combined_renamed_widget.options = final_list_of_combined_images_renamed + self.final_list_of_files_combined_renamed_widget.options = ( + final_list_of_combined_images_renamed + ) self.dict_new_name_old_base_name_match = dict_new_name_old_base_name_match def new_list_of_files_selection_changed(self, value): @@ -197,13 +226,19 @@ def new_list_of_files_selection_changed(self, value): if dict_new_name_old_base_name_match: base_name = dict_new_name_old_base_name_match[new_selection] corresponding_list_of_files = self.full_combine_dict[base_name] - self.corresponding_list_of_files_combined.options = corresponding_list_of_files + self.corresponding_list_of_files_combined.options = ( + corresponding_list_of_files + ) def select_output_folder(self): self.o_folder = FileFolderBrowser( - working_dir=self.working_dir, next_function=self.combine, ipts_folder=self.ipts_folder + working_dir=self.working_dir, + next_function=self.combine, + ipts_folder=self.ipts_folder, + ) + self.o_folder.select_output_folder_with_new( + instruction="Select where to create the combine data folder ..." ) - self.o_folder.select_output_folder_with_new(instruction="Select where to create the combine data folder ...") def combine(self, output_folder): # remove shortcut buttons @@ -215,7 +250,9 @@ def combine(self, output_folder): full_new_folder_name = os.path.join(os.path.abspath(output_folder), new_folder) make_or_reset_folder(full_new_folder_name) - progress_ui = widgets.IntProgress(value=0, min=0, max=len(self.full_combine_dict.keys())) + progress_ui = widgets.IntProgress( + value=0, min=0, max=len(self.full_combine_dict.keys()) + ) display(progress_ui) _index = 0 @@ -224,7 +261,8 @@ def combine(self, output_folder): full_file_name = os.path.join(full_new_folder_name, file_name) list_files_to_combine_fullname = [ - os.path.join(self.input_folder, _file) for _file in self.full_combine_dict[_base_name] + os.path.join(self.input_folder, _file) + for _file in self.full_combine_dict[_base_name] ] o_work = Normalization() diff --git a/notebooks/__code/config.py b/notebooks/__code/config.py index f2f00ae74..c42a98054 100755 --- a/notebooks/__code/config.py +++ b/notebooks/__code/config.py @@ -1,6 +1,11 @@ debugging = False debugger_username = "j35" -debugger_folder = ["/Volumes/JeanHardDrive/", "/Users/j35/", "/Volumes/Buffalo/IPTS/", "/Users/j35/IPTS/"] +debugger_folder = [ + "/Volumes/JeanHardDrive/", + "/Users/j35/", + "/Volumes/Buffalo/IPTS/", + "/Users/j35/IPTS/", +] project_folder = "IPTS_16259" # system_folder = '/Users/j35/' debugger_instrument_folder = { @@ -9,7 +14,11 @@ "SNAP": "/Volumes/JeanHardDrive/SNS/SNAP", "VENUS": "/Volumes/JeanHardDrive/SNS/VENUS", }, - "mac126455": {"CG1D": "/Users/j35/HFIR/CG1D", "SNAP": "/Users/j35/SNS/SNAP", "VENUS": "/Users/j35/SNS/VENUS"}, + "mac126455": { + "CG1D": "/Users/j35/HFIR/CG1D", + "SNAP": "/Users/j35/SNS/SNAP", + "VENUS": "/Users/j35/SNS/VENUS", + }, } # system_folder_SNS = '/SNS/' analysis_machine = "cg1d-analysis2.ornl.gov" diff --git a/notebooks/__code/create_list_of_file_name_vs_time_stamp.py b/notebooks/__code/create_list_of_file_name_vs_time_stamp.py index bcf8df13d..9a608a421 100755 --- a/notebooks/__code/create_list_of_file_name_vs_time_stamp.py +++ b/notebooks/__code/create_list_of_file_name_vs_time_stamp.py @@ -20,7 +20,9 @@ def __init__(self, working_dir="", verbose=False): def select_image_folder(self): self.folder_ui = fileselector.FileSelectorPanel( - instruction="Select Raw Image Folder ...", start_dir=self.working_dir, type="directory" + instruction="Select Raw Image Folder ...", + start_dir=self.working_dir, + type="directory", ) self.folder_ui.show() @@ -32,7 +34,11 @@ def select_image_folder(self): def retrieve_time_stamp(self): self.image_folder = self.folder_ui.selected - [list_files, ext] = file_handler.retrieve_list_of_most_dominant_extension_from_folder(folder=self.image_folder) + [list_files, ext] = ( + file_handler.retrieve_list_of_most_dominant_extension_from_folder( + folder=self.image_folder + ) + ) self.list_files = list_files if ext.lower() in [".tiff", ".tif"]: @@ -44,8 +50,15 @@ def retrieve_time_stamp(self): box = widgets.HBox( [ - widgets.Label("Retrieving Time Stamp", layout=widgets.Layout(width="20%")), - widgets.IntProgress(min=0, max=len(list_files), value=0, layout=widgets.Layout(width="50%")), + widgets.Label( + "Retrieving Time Stamp", layout=widgets.Layout(width="20%") + ), + widgets.IntProgress( + min=0, + max=len(list_files), + value=0, + layout=widgets.Layout(width="50%"), + ), ] ) progress_bar = box.children[1] @@ -86,25 +99,48 @@ def display_selected_image(index, text_x, text_y, pre_text, post_text, color): ax = plt.subplot(gs[0, 0]) im = ax.imshow(self.images_array[index], interpolation="nearest") plt.title(f"image index {index}") - plt.text(text_x, text_y, f"{pre_text} {self.list_time_offset[index]:.2f}{post_text}", fontdict=font) + plt.text( + text_x, + text_y, + f"{pre_text} {self.list_time_offset[index]:.2f}{post_text}", + fontdict=font, + ) fig.colorbar(im) plt.show() - return {"text_x": text_x, "text_y": text_y, "pre_text": pre_text, "post_text": post_text, "color": color} + return { + "text_x": text_x, + "text_y": text_y, + "pre_text": pre_text, + "post_text": post_text, + "color": color, + } self.preview = interact( display_selected_image, - index=widgets.IntSlider(min=0, max=len(self.list_files), continuous_update=False), + index=widgets.IntSlider( + min=0, max=len(self.list_files), continuous_update=False + ), text_x=widgets.IntSlider( - min=0, max=width, value=text_x, description="Text x_offset", continuous_update=False + min=0, + max=width, + value=text_x, + description="Text x_offset", + continuous_update=False, ), text_y=widgets.IntSlider( - min=0, max=height, value=text_y, description="Text y_offset", continuous_upadte=False + min=0, + max=height, + value=text_y, + description="Text y_offset", + continuous_upadte=False, ), pre_text=widgets.Text(value="Time Offset", description="Pre text"), post_text=widgets.Text(value="(s)", description="Post text"), color=widgets.RadioButtons( - options=["red", "blue", "white", "black", "yellow"], value="red", description="Text Color" + options=["red", "blue", "white", "black", "yellow"], + value="red", + description="Text Color", ), ) @@ -138,7 +174,9 @@ def export(self, output_folder): return input_folder_basename = os.path.basename(os.path.abspath(self.image_folder)) - output_file = os.path.abspath(os.path.join(output_folder, input_folder_basename + "_timestamp_infos.txt")) + output_file = os.path.abspath( + os.path.join(output_folder, input_folder_basename + "_timestamp_infos.txt") + ) if os.path.exists(output_file): os.remove(output_file) @@ -156,7 +194,9 @@ def export(self, output_folder): with open(output_file, "w") as f: f.write(text) - display(HTML("File Created: " + os.path.basename(output_file) + "")) + display( + HTML("File Created: " + os.path.basename(output_file) + "") + ) def select_export_folder(self): ipts_folder = self.working_dir diff --git a/notebooks/__code/cylindrical_geometry_correction_embedded_widgets/main.py b/notebooks/__code/cylindrical_geometry_correction_embedded_widgets/main.py index 9a3a6c323..3d2e784af 100755 --- a/notebooks/__code/cylindrical_geometry_correction_embedded_widgets/main.py +++ b/notebooks/__code/cylindrical_geometry_correction_embedded_widgets/main.py @@ -70,7 +70,9 @@ def __init__(self, working_dir="./"): self.working_dir = working_dir def select_images(self): - file_folder_browser = FileFolderBrowser(working_dir=self.working_dir, next_function=self.load_images) + file_folder_browser = FileFolderBrowser( + working_dir=self.working_dir, next_function=self.load_images + ) file_folder_browser.select_images(filters={"TIFF": "*.tif?"}) def load_images(self, list_of_images): @@ -90,11 +92,16 @@ def load_images(self, list_of_images): if self.data: [self.height, self.width] = np.shape(np.squeeze(self.data[0])) - display(HTML("Number of images loaded: " + str(len(list_of_images)) + "")) + display( + HTML( + "Number of images loaded: " + str(len(list_of_images)) + "" + ) + ) def select_config(self): config_browser = FileFolderBrowser( - working_dir=os.path.dirname(self.working_dir), next_function=self.load_config + working_dir=os.path.dirname(self.working_dir), + next_function=self.load_config, ) config_browser.select_images( instruction="Select config file ...", @@ -120,7 +127,12 @@ def plot(image_index): v = interactive( plot, - image_index=widgets.IntSlider(min=0, max=len(self.data) - 1, value=0, layout=widgets.Layout(width="50%")), + image_index=widgets.IntSlider( + min=0, + max=len(self.data) - 1, + value=0, + layout=widgets.Layout(width="50%"), + ), ) display(v) @@ -164,8 +176,12 @@ def plot(rot_value, image_index, vert_guide, profile1_h, profile2_h): # color='g', # linestyle="--") - profile1 = data[profile1_h, vert_guide - profile_margin : vert_guide + profile_margin] - profile2 = data[profile2_h, vert_guide - profile_margin : vert_guide + profile_margin] + profile1 = data[ + profile1_h, vert_guide - profile_margin : vert_guide + profile_margin + ] + profile2 = data[ + profile2_h, vert_guide - profile_margin : vert_guide + profile_margin + ] ax1.cla() ax1.plot(profile1, "b", label="profile 1") @@ -199,7 +215,12 @@ def plot(rot_value, image_index, vert_guide, profile1_h, profile2_h): continuous_update=False, layout=widgets.Layout(width="50%"), ), - image_index=widgets.IntSlider(min=0, max=len(self.data) - 1, value=0, layout=widgets.Layout(width="50%")), + image_index=widgets.IntSlider( + min=0, + max=len(self.data) - 1, + value=0, + layout=widgets.Layout(width="50%"), + ), vert_guide=widgets.IntSlider( min=0, max=self.width - 1, @@ -207,8 +228,12 @@ def plot(rot_value, image_index, vert_guide, profile1_h, profile2_h): layout=widgets.Layout(width="50%"), continuous_update=False, ), - profile1_h=widgets.IntSlider(min=0, max=self.height - 1, continuous_update=False, value=1135), - profile2_h=widgets.IntSlider(min=0, max=self.height - 1, continuous_update=False, value=1794), + profile1_h=widgets.IntSlider( + min=0, max=self.height - 1, continuous_update=False, value=1135 + ), + profile2_h=widgets.IntSlider( + min=0, max=self.height - 1, continuous_update=False, value=1794 + ), ) display(self.v) @@ -262,12 +287,24 @@ def plot(image_index, left, right, top, bottom, profile_mker): self.crop_ui = interactive( plot, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), - left=widgets.IntSlider(min=0, max=width - 1, value=self.config["default_crop"]["x0"]), - right=widgets.IntSlider(min=0, max=width - 1, value=self.config["default_crop"]["x1"]), - top=widgets.IntSlider(min=0, max=height - 1, value=self.config["default_crop"]["y0"]), - bottom=widgets.IntSlider(min=0, max=height - 1, value=self.config["default_crop"]["y1"]), - profile_mker=widgets.IntSlider(min=0, max=height - 1, value=self.config["default_crop"]["marker"]), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), + left=widgets.IntSlider( + min=0, max=width - 1, value=self.config["default_crop"]["x0"] + ), + right=widgets.IntSlider( + min=0, max=width - 1, value=self.config["default_crop"]["x1"] + ), + top=widgets.IntSlider( + min=0, max=height - 1, value=self.config["default_crop"]["y0"] + ), + bottom=widgets.IntSlider( + min=0, max=height - 1, value=self.config["default_crop"]["y1"] + ), + profile_mker=widgets.IntSlider( + min=0, max=height - 1, value=self.config["default_crop"]["marker"] + ), ) display(self.crop_ui) @@ -296,7 +333,9 @@ def export_cropped_images(self): def export_cropped_images_step2(self, output_folder): output_folder = os.path.abspath(output_folder) working_dir = self.working_dir - base_working_dir = os.path.join(output_folder, os.path.basename(working_dir) + "_cropped") + base_working_dir = os.path.join( + output_folder, os.path.basename(working_dir) + "_cropped" + ) base_working_dir = make_or_increment_folder_name(base_working_dir) list_images_corrected = self.cropped_data @@ -313,7 +352,13 @@ def export_cropped_images_step2(self, output_folder): progress_bar.value = index + 1 progress_bar.close() - display(HTML('' + str(nbr_images) + " images created!")) + display( + HTML( + '' + + str(nbr_images) + + " images created!" + ) + ) def background_range_selection(self): if self.cropped_data is None: @@ -335,7 +380,9 @@ def plot(image_index, top, bottom): self.background_limit_ui = interactive( plot, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), top=widgets.IntSlider(min=0, max=height - 1, value=default_top), bottom=widgets.IntSlider(min=0, max=height - 1, value=default_bottom), ) @@ -359,7 +406,9 @@ def plot(image_index, top, bottom): self.sample_limit_ui = interactive( plot, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), top=widgets.IntSlider(min=0, max=height - 1, value=default_top), bottom=widgets.IntSlider(min=0, max=height - 1, value=default_bottom), ) @@ -373,13 +422,16 @@ def remove_background_signal(self): y0_background = self.background_limit_ui.children[1].value y1_background = self.background_limit_ui.children[2].value background_signal_integrated = [ - np.mean(_data[y0_background : y1_background + 1, :], axis=0) for _data in self.cropped_data + np.mean(_data[y0_background : y1_background + 1, :], axis=0) + for _data in self.cropped_data ] y0_sample = self.sample_limit_ui.children[1].value y1_sample = self.sample_limit_ui.children[2].value sample_without_background = [] - for _background, _sample in zip(background_signal_integrated, self.cropped_data, strict=False): + for _background, _sample in zip( + background_signal_integrated, self.cropped_data, strict=False + ): _data = _sample[y0_sample : y1_sample + 1] sample_without_background.append(np.abs(_data - _background)) @@ -392,7 +444,10 @@ def plot(image_index): ax1.imshow(self.sample_without_background[image_index], vmin=0, vmax=1) self.sample_no_background_ui = interactive( - plot, image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0) + plot, + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), ) display(self.sample_no_background_ui) @@ -416,7 +471,9 @@ def plot(image_index, profile_height): v = interactive( plot, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), profile_height=widgets.IntSlider(min=0, max=height - 1, value=0), ) display(v) @@ -443,8 +500,10 @@ def correct_cylinder_geometry(self): expected_array = [] for x_index, x in enumerate(profile): measure = x - number_of_pixels_through_thickness = number_of_pixels_at_that_position1( - position=x_index, radius=radius + number_of_pixels_through_thickness = ( + number_of_pixels_at_that_position1( + position=x_index, radius=radius + ) ) number_of_pixels.append(number_of_pixels_through_thickness) expected_array.append(measure / number_of_pixels_through_thickness) @@ -469,22 +528,32 @@ def plot(image_index, index1, index2, plot_max): self.sample_corrected = interactive( plot, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), - index1=widgets.IntSlider(min=0, max=height - 1, value=int((height - 1) / 3)), - index2=widgets.IntSlider(min=0, max=height - 1, value=2 * int((height - 1) / 3)), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), + index1=widgets.IntSlider( + min=0, max=height - 1, value=int((height - 1) / 3) + ), + index2=widgets.IntSlider( + min=0, max=height - 1, value=2 * int((height - 1) / 3) + ), plot_max=widgets.FloatSlider(min=1e-5, max=1.0, step=0.001, value=0.02), ) display(self.sample_corrected) def export_profiles(self): working_dir = os.path.dirname(self.working_dir) - output_folder_browser = FileFolderBrowser(working_dir=working_dir, next_function=self.export) + output_folder_browser = FileFolderBrowser( + working_dir=working_dir, next_function=self.export + ) output_folder_browser.select_output_folder() def export(self, output_folder): output_folder = os.path.abspath(output_folder) working_dir = self.working_dir - base_working_dir = os.path.join(output_folder, os.path.basename(working_dir) + "_cylindrical_geo_corrected") + base_working_dir = os.path.join( + output_folder, os.path.basename(working_dir) + "_cylindrical_geo_corrected" + ) base_working_dir = make_or_increment_folder_name(base_working_dir) # export images @@ -502,7 +571,13 @@ def export(self, output_folder): progress_bar.value = index + 1 progress_bar.close() - display(HTML('' + str(nbr_images) + " images created!")) + display( + HTML( + '' + + str(nbr_images) + + " images created!" + ) + ) # export profiles @@ -525,8 +600,12 @@ def export(self, output_folder): for index, image in enumerate(list_images_corrected): _name = os.path.basename(list_of_images[index]) base_name_without_suffix = PurePosixPath(_name).stem - base_name_of_ascii_file = str(base_name_without_suffix) + "_profile_corrected.csv" - full_name_of_ascii_file = os.path.join(base_working_dir, base_name_of_ascii_file) + base_name_of_ascii_file = ( + str(base_name_without_suffix) + "_profile_corrected.csv" + ) + full_name_of_ascii_file = os.path.join( + base_working_dir, base_name_of_ascii_file + ) df = pd.DataFrame(image) df.to_csv(full_name_of_ascii_file) @@ -536,17 +615,41 @@ def export(self, output_folder): progress_bar.close() config_filename = os.path.join(output_folder, "config.json") - self.export_config(output_folder=base_working_dir, config_filename=config_filename) + self.export_config( + output_folder=base_working_dir, config_filename=config_filename + ) - display(HTML('' + str(nbr_images) + " ASCII files created!")) + display( + HTML( + '' + + str(nbr_images) + + " ASCII files created!" + ) + ) json_file_name = os.path.join(base_working_dir, "metadata.json") with open(json_file_name, "w") as outfile: json.dump(metadata, outfile) - display(HTML(' metadata json file created (metadata.json)!')) - display(HTML(' config file: ' + config_filename + "")) + display( + HTML( + ' metadata json file created (metadata.json)!' + ) + ) + display( + HTML( + ' config file: ' + + config_filename + + "" + ) + ) - display(HTML(' Output folder: ' + base_working_dir + "!")) + display( + HTML( + ' Output folder: ' + + base_working_dir + + "!" + ) + ) def export_config(self, output_folder=None, config_filename=None): with open(config_filename, "w") as outfile: diff --git a/notebooks/__code/deal.py b/notebooks/__code/deal.py index 6137b173c..83666ede3 100755 --- a/notebooks/__code/deal.py +++ b/notebooks/__code/deal.py @@ -61,7 +61,9 @@ def deal(self, output_folder): def __split(self, list_images=[]): # splitting name using hypothesis that name is defined as /path/image###_####.fits - list_folder = set([os.path.basename(_file).split("_")[0] for _file in list_images]) + list_folder = set( + [os.path.basename(_file).split("_")[0] for _file in list_images] + ) output_folder_dict = {} for _folder in list_folder: output_folder_dict[_folder] = [] @@ -71,7 +73,9 @@ def __split(self, list_images=[]): _key = os.path.basename(_image).split("_")[0] output_folder_dict[_key].append(_image) - nbr_images_per_folder = len(output_folder_dict[list(output_folder_dict.keys())[0]]) + nbr_images_per_folder = len( + output_folder_dict[list(output_folder_dict.keys())[0]] + ) nbr_folders = len(list(output_folder_dict.keys())) return { @@ -81,7 +85,12 @@ def __split(self, list_images=[]): } def __deal( - self, prefix_name="image", nbr_images_per_folder=0, nbr_folders=0, output_folder="", output_folder_dict={} + self, + prefix_name="image", + nbr_images_per_folder=0, + nbr_folders=0, + output_folder="", + output_folder_dict={}, ): """ copy the images into their corresponding new output folder @@ -125,7 +134,9 @@ def __deal( for _input_image in _input_list_images: _basename_image = os.path.basename(_input_image) [part1, part2] = _basename_image.split("_") - new_name = os.path.join(output_folder, _folder, prefix_name + "_" + part2) + new_name = os.path.join( + output_folder, _folder, prefix_name + "_" + part2 + ) shutil.copyfile(_input_image, new_name) _index2 += 1 w1.value = _index2 diff --git a/notebooks/__code/display_and_export_images_with_metadata_profile.py b/notebooks/__code/display_and_export_images_with_metadata_profile.py index 7b3465067..a93a03f56 100755 --- a/notebooks/__code/display_and_export_images_with_metadata_profile.py +++ b/notebooks/__code/display_and_export_images_with_metadata_profile.py @@ -20,7 +20,10 @@ def __init__(self, working_dir=""): def select_input_folder(self): self.folder_ui = FileSelectorPanel( - instruction="Select Input Folder ...", start_dir=self.working_dir, type="directory", multiple=False + instruction="Select Input Folder ...", + start_dir=self.working_dir, + type="directory", + multiple=False, ) self.folder_ui.show() @@ -42,7 +45,9 @@ def __check_inputs(self): # images image_folder = self.folder_ui.selected - list_images = file_handler.retrieve_list_of_most_dominant_extension_from_folder(folder=image_folder) + list_images = file_handler.retrieve_list_of_most_dominant_extension_from_folder( + folder=image_folder + ) list_images = list_images[0] self.nbr_images = len(list_images) @@ -50,7 +55,9 @@ def __check_inputs(self): file_name_vs_metadata_name = self.metadata_file_ui.selected self.file_name_vs_metadata = pd.read_csv(file_name_vs_metadata_name) self.metadata_name = list(self.file_name_vs_metadata.columns.values)[2] - nbr_metadata = len(self.file_name_vs_metadata[self.file_name_vs_metadata.columns[0]]) + nbr_metadata = len( + self.file_name_vs_metadata[self.file_name_vs_metadata.columns[0]] + ) assert self.nbr_images == nbr_metadata @@ -68,7 +75,9 @@ def __preview(self): _metadata_array = np.array(self.file_name_vs_metadata["Metadata"]) _time_array = np.array(self.file_name_vs_metadata["time"]) - for _index, _file in enumerate(np.array(self.file_name_vs_metadata["file_name"])): + for _index, _file in enumerate( + np.array(self.file_name_vs_metadata["file_name"]) + ): metadata_profile[_file] = {} metadata_profile[_file]["metadata"] = _metadata_array[_index] metadata_profile[_file]["time"] = _time_array[_index] @@ -99,13 +108,18 @@ def plot_images_and_profile(file_index=0): preview = interact( plot_images_and_profile, file_index=widgets.IntSlider( - min=0, max=self.nbr_images - 1, description="Image Index", continuous_update=False + min=0, + max=self.nbr_images - 1, + description="Image Index", + continuous_update=False, ), ) def select_export_folder(self): self.output_folder_ui = fileselector.FileSelectorPanel( - instruction="Select output Folder ...", start_dir=self.working_dir, type="directory" + instruction="Select output Folder ...", + start_dir=self.working_dir, + type="directory", ) self.output_folder_ui.show() @@ -113,7 +127,9 @@ def export(self): output_folder = self.output_folder_ui.selected input_folder_basename = os.path.basename(self.folder_ui.selected) - output_folder = os.path.join(output_folder, input_folder_basename + "_vs_metadata_screenshots") + output_folder = os.path.join( + output_folder, input_folder_basename + "_vs_metadata_screenshots" + ) if os.path.exists(output_folder): import shutil @@ -138,7 +154,9 @@ def plot_images_and_profile(file_index=0): plt.xlabel("Time (s)") plt.ylabel(self.metadata_name) - output_file_name = os.path.abspath(os.path.join(output_folder, _short_file + ".png")) + output_file_name = os.path.abspath( + os.path.join(output_folder, _short_file + ".png") + ) plt.savefig(output_file_name) plt.close(fig) @@ -146,7 +164,9 @@ def plot_images_and_profile(file_index=0): box = widgets.HBox( [ widgets.Label("Exporting Images:", layout=widgets.Layout(width="20%")), - widgets.IntProgress(min=0, max=self.nbr_images - 1, layout=widgets.Layout(width="50%")), + widgets.IntProgress( + min=0, max=self.nbr_images - 1, layout=widgets.Layout(width="50%") + ), ] ) progress_bar = box.children[1] @@ -157,4 +177,10 @@ def plot_images_and_profile(file_index=0): progress_bar.value = _index + 1 box.close() - display(HTML('Images created in ' + output_folder + "")) + display( + HTML( + 'Images created in ' + + output_folder + + "" + ) + ) diff --git a/notebooks/__code/display_and_export_images_with_time_stamp.py b/notebooks/__code/display_and_export_images_with_time_stamp.py index 637a87cd2..0d14273bb 100755 --- a/notebooks/__code/display_and_export_images_with_time_stamp.py +++ b/notebooks/__code/display_and_export_images_with_time_stamp.py @@ -19,7 +19,9 @@ def __init__(self, working_dir="", verbose=False): def select_image_folder(self): self.folder_ui = ipywe.fileselector.FileSelectorPanel( - instruction="Select Raw Image Folder ...", start_dir=self.working_dir, type="directory" + instruction="Select Raw Image Folder ...", + start_dir=self.working_dir, + type="directory", ) self.folder_ui.show() @@ -31,7 +33,11 @@ def select_image_folder(self): def retrieve_time_stamp(self): self.image_folder = self.folder_ui.selected - [list_files, ext] = file_handler.retrieve_list_of_most_dominant_extension_from_folder(folder=self.image_folder) + [list_files, ext] = ( + file_handler.retrieve_list_of_most_dominant_extension_from_folder( + folder=self.image_folder + ) + ) self.list_files = list_files if ext.lower() in [".tiff", ".tif"]: @@ -43,8 +49,15 @@ def retrieve_time_stamp(self): box = widgets.HBox( [ - widgets.Label("Retrieving Time Stamp", layout=widgets.Layout(width="20%")), - widgets.IntProgress(min=0, max=len(list_files), value=0, layout=widgets.Layout(width="50%")), + widgets.Label( + "Retrieving Time Stamp", layout=widgets.Layout(width="20%") + ), + widgets.IntProgress( + min=0, + max=len(list_files), + value=0, + layout=widgets.Layout(width="50%"), + ), ] ) progress_bar = box.children[1] @@ -80,31 +93,56 @@ def display_selected_image(index, text_x, text_y, pre_text, post_text, color): ax = plt.subplot(gs[0, 0]) im = ax.imshow(self.images_array[index], interpolation="nearest") plt.title(f"image index {index}") - plt.text(text_x, text_y, f"{pre_text} {self.list_time_offset[index]:.2f}{post_text}", fontdict=font) + plt.text( + text_x, + text_y, + f"{pre_text} {self.list_time_offset[index]:.2f}{post_text}", + fontdict=font, + ) fig.colorbar(im) plt.show() - return {"text_x": text_x, "text_y": text_y, "pre_text": pre_text, "post_text": post_text, "color": color} + return { + "text_x": text_x, + "text_y": text_y, + "pre_text": pre_text, + "post_text": post_text, + "color": color, + } self.preview = interact( display_selected_image, - index=widgets.IntSlider(min=0, max=len(self.list_files), continuous_update=False), + index=widgets.IntSlider( + min=0, max=len(self.list_files), continuous_update=False + ), text_x=widgets.IntSlider( - min=0, max=width, value=text_x, description="Text x_offset", continuous_update=False + min=0, + max=width, + value=text_x, + description="Text x_offset", + continuous_update=False, ), text_y=widgets.IntSlider( - min=0, max=height, value=text_y, description="Text y_offset", continuous_upadte=False + min=0, + max=height, + value=text_y, + description="Text y_offset", + continuous_upadte=False, ), pre_text=widgets.Text(value="Time Offset", description="Pre text"), post_text=widgets.Text(value="(s)", description="Post text"), color=widgets.RadioButtons( - options=["red", "blue", "white", "black", "yellow"], value="red", description="Text Color" + options=["red", "blue", "white", "black", "yellow"], + value="red", + description="Text Color", ), ) def select_export_folder(self): self.output_folder_ui = ipywe.fileselector.FileSelectorPanel( - instruction="Select output Folder ...", start_dir=self.working_dir, type="directory" + instruction="Select output Folder ...", + start_dir=self.working_dir, + type="directory", ) self.output_folder_ui.show() @@ -112,7 +150,9 @@ def export(self): output_folder = self.output_folder_ui.selected input_folder_basename = os.path.basename(self.image_folder) - output_folder = os.path.join(output_folder, input_folder_basename + "_with_timestamp_info") + output_folder = os.path.join( + output_folder, input_folder_basename + "_with_timestamp_info" + ) if os.path.exists(output_folder): import shutil @@ -127,7 +167,9 @@ def export(self): def plot_selected_image(index): _short_file = os.path.basename(self.list_files[index]) - output_file_name = os.path.abspath(os.path.join(output_folder, _short_file + ".png")) + output_file_name = os.path.abspath( + os.path.join(output_folder, _short_file + ".png") + ) font = {"family": "serif", "color": color, "weight": "normal", "size": 16} @@ -136,7 +178,12 @@ def plot_selected_image(index): ax = plt.subplot(gs[0, 0]) im = ax.imshow(self.images_array[index], interpolation="nearest") plt.title(f"image index {index}") - plt.text(text_x, text_y, f"{pre_text}{self.list_time_offset[index]:.2f}{post_text}", fontdict=font) + plt.text( + text_x, + text_y, + f"{pre_text}{self.list_time_offset[index]:.2f}{post_text}", + fontdict=font, + ) fig.colorbar(im) plt.savefig(output_file_name) plt.close(fig) @@ -144,7 +191,11 @@ def plot_selected_image(index): box = widgets.HBox( [ widgets.Label("Exporting Images:", layout=widgets.Layout(width="20%")), - widgets.IntProgress(min=0, max=len(self.list_files) - 1, layout=widgets.Layout(width="50%")), + widgets.IntProgress( + min=0, + max=len(self.list_files) - 1, + layout=widgets.Layout(width="50%"), + ), ] ) progress_bar = box.children[1] diff --git a/notebooks/__code/display_counts_of_region_vs_stack.py b/notebooks/__code/display_counts_of_region_vs_stack.py index f8f6bab93..a1c56fcb8 100755 --- a/notebooks/__code/display_counts_of_region_vs_stack.py +++ b/notebooks/__code/display_counts_of_region_vs_stack.py @@ -19,7 +19,10 @@ def __init__(self, working_dir=""): def select_input_folder(self): self.input_folder_ui = ipywe.fileselector.FileSelectorPanel( - instruction="Select Input Folder", type="directory", start_dir=self.working_dir, multiple=False + instruction="Select Input Folder", + type="directory", + start_dir=self.working_dir, + multiple=False, ) self.input_folder_ui.show() @@ -40,7 +43,8 @@ def __init__(self, parent=None, display_counts_vs_stack=None): QMainWindow.__init__(self, parent=parent) ui_full_path = os.path.join( - os.path.dirname(os.path.dirname(__file__)), os.path.join("ui", "ui_display_counts_of_region_vs_stack.ui") + os.path.dirname(os.path.dirname(__file__)), + os.path.join("ui", "ui_display_counts_of_region_vs_stack.ui"), ) self.ui = load_ui(ui_full_path, baseinstance=self) self.setWindowTitle("Select ROI to display profile over all images.") @@ -110,7 +114,9 @@ def initialize_pyqtgraph(self): self.ui.image_view.ui.roiBtn.hide() # default ROI - self.ui.roi = pg.ROI([0, 0], [20, 20], pen=(62, 13, 244), scaleSnap=True) # blue + self.ui.roi = pg.ROI( + [0, 0], [20, 20], pen=(62, 13, 244), scaleSnap=True + ) # blue self.ui.roi.addScaleHandle([1, 1], [0, 0]) self.ui.image_view.addItem(self.ui.roi) self.ui.roi.sigRegionChanged.connect(self.roi_changed) @@ -126,7 +132,9 @@ def initialize_pyqtgraph(self): self.ui.widget.setLayout(vertical_layout) def roi_changed(self): - region = self.ui.roi.getArraySlice(self.integrated_stack, self.ui.image_view.imageItem) + region = self.ui.roi.getArraySlice( + self.integrated_stack, self.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop - 1 y0 = region[0][1].start @@ -171,7 +179,9 @@ def update_x_axis(self): x_axis_selected = "file_index" b_enable_only_file_index_button = True - self.radio_buttons_status(b_enable_only_file_index_button=b_enable_only_file_index_button) + self.radio_buttons_status( + b_enable_only_file_index_button=b_enable_only_file_index_button + ) self.x_axis["type"] = x_axis_selected if x_axis_selected == "file_index": @@ -208,7 +218,9 @@ def detector_offset_validated(self): def time_spectra_file_browse_button_clicked(self): spectra_file = QFileDialog.getOpenFileName( - caption="Select Time Spectra", directory=self.working_folder, filter="txt (*_Spectra.txt);;All (*.*)" + caption="Select Time Spectra", + directory=self.working_folder, + filter="txt (*_Spectra.txt);;All (*.*)", ) spectra_file = spectra_file[0] if spectra_file: diff --git a/notebooks/__code/display_counts_of_region_vs_stack_vs_theory.py b/notebooks/__code/display_counts_of_region_vs_stack_vs_theory.py index 93ca51983..2e861a60b 100755 --- a/notebooks/__code/display_counts_of_region_vs_stack_vs_theory.py +++ b/notebooks/__code/display_counts_of_region_vs_stack_vs_theory.py @@ -15,7 +15,9 @@ from ImagingReso import _utilities from neutronbraggedge.experiment_handler import * -from __code.ui_resonance_imaging_experiment_vs_theory import Ui_MainWindow as UiMainWindow +from __code.ui_resonance_imaging_experiment_vs_theory import ( + Ui_MainWindow as UiMainWindow, +) class ImageWindow(QMainWindow): @@ -142,7 +144,9 @@ def initialize_pyqtgraph(self): self.ui.image_view.ui.roiBtn.hide() # default ROI - self.ui.roi = pg.ROI([0, 0], [20, 20], pen=(62, 13, 244), scaleSnap=True) # blue + self.ui.roi = pg.ROI( + [0, 0], [20, 20], pen=(62, 13, 244), scaleSnap=True + ) # blue self.ui.roi.addScaleHandle([1, 1], [0, 0]) self.ui.image_view.addItem(self.ui.roi) self.ui.roi.sigRegionChanged.connect(self.roi_changed) @@ -158,7 +162,9 @@ def initialize_pyqtgraph(self): self.ui.widget.setLayout(vertical_layout) def roi_changed(self): - region = self.ui.roi.getArraySlice(self.integrated_stack, self.ui.image_view.imageItem) + region = self.ui.roi.getArraySlice( + self.integrated_stack, self.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop - 1 y0 = region[0][1].start @@ -212,7 +218,9 @@ def update_radio_button_status(self): x_axis_selected = "file_index" b_enable_only_file_index_button = True - self.set_radio_buttons_status(b_enable_only_file_index_button=b_enable_only_file_index_button) + self.set_radio_buttons_status( + b_enable_only_file_index_button=b_enable_only_file_index_button + ) self.b_enable_only_file_index_button = b_enable_only_file_index_button self.update_x_axis() @@ -287,7 +295,9 @@ def detector_offset_validated(self): def time_spectra_file_browse_button_clicked(self): spectra_file = QFileDialog.getOpenFileName( - caption="Select Time Spectra", directory=self.working_folder, filter="txt (*_Spectra.txt);;All (*.*)" + caption="Select Time Spectra", + directory=self.working_folder, + filter="txt (*_Spectra.txt);;All (*.*)", ) if spectra_file: self.ui.time_spectra_file.setText(os.path.basename(spectra_file)) @@ -307,7 +317,9 @@ def init_list_of_things_to_plot(self): list_things_to_plot.append(_layer + " -> " + _element) list_isotopes = stack[_layer][_element]["isotopes"]["list"] for _isotope in list_isotopes: - list_things_to_plot.append(_layer + " -> " + _element + " -> " + _isotope) + list_things_to_plot.append( + _layer + " -> " + _element + " -> " + _isotope + ) self.ui.list_to_plot_widget.addItems(list_things_to_plot) @@ -339,21 +351,31 @@ def plot_selection_changed(self, item): transmission = self.o_reso.stack_signal[_layer]["transmission"] x_axis_ev = self.o_reso.stack_signal[_layer]["energy_eV"] elif _isotope == "": - transmission = self.o_reso.stack_signal[_layer][_element]["transmission"] + transmission = self.o_reso.stack_signal[_layer][_element][ + "transmission" + ] x_axis_ev = self.o_reso.stack_signal[_layer][_element]["energy_eV"] else: - transmission = self.o_reso.stack_signal[_layer][_element][_isotope]["transmission"] - x_axis_ev = self.o_reso.stack_signal[_layer][_element][_isotope]["energy_eV"] + transmission = self.o_reso.stack_signal[_layer][_element][_isotope][ + "transmission" + ] + x_axis_ev = self.o_reso.stack_signal[_layer][_element][_isotope][ + "energy_eV" + ] _elements_to_plot[_text] = {} _elements_to_plot[_text]["y_axis"] = transmission x_axis = [] if x_axis_selected == "lambda": - x_axis = _utilities.convert_x_axis(array=x_axis_ev, from_units="ev", to_units="angstroms") + x_axis = _utilities.convert_x_axis( + array=x_axis_ev, from_units="ev", to_units="angstroms" + ) elif x_axis_selected == "tof": detector_offset = float(self.ui.detector_offset_value.text()) - distance_source_detector = float(self.ui.distance_source_detector_value.text()) + distance_source_detector = float( + self.ui.distance_source_detector_value.text() + ) x_axis = _utilities.convert_x_axis( array=x_axis_ev, from_units="ev", diff --git a/notebooks/__code/display_file_names_vs_time_stamp.py b/notebooks/__code/display_file_names_vs_time_stamp.py index 4cc94a722..af2c94049 100755 --- a/notebooks/__code/display_file_names_vs_time_stamp.py +++ b/notebooks/__code/display_file_names_vs_time_stamp.py @@ -49,8 +49,15 @@ def retrieve_time_stamp(self, result): box = widgets.HBox( [ - widgets.Label("Retrieving Time Stamp", layout=widgets.Layout(width="20%")), - widgets.IntProgress(min=0, max=len(self.list_files), value=0, layout=widgets.Layout(width="50%")), + widgets.Label( + "Retrieving Time Stamp", layout=widgets.Layout(width="20%") + ), + widgets.IntProgress( + min=0, + max=len(self.list_files), + value=0, + layout=widgets.Layout(width="50%"), + ), ] ) progress_bar = box.children[1] @@ -105,7 +112,9 @@ def print_result(self): timestamp_text_area.append(_text) timestamp_text_area = "\n".join(timestamp_text_area) - relative_text_area = ["file name -> (current image acquistion time - previous image acquisition time) \n"] + relative_text_area = [ + "file name -> (current image acquistion time - previous image acquisition time) \n" + ] for _index, _file in enumerate(self.list_files): _short_file = os.path.basename(_file) _relative_time = self.relative_time_offset[_index] @@ -113,7 +122,9 @@ def print_result(self): relative_text_area.append(_text) relative_text_area = "\n".join(relative_text_area) - absolute_text_area = ["file name -> (current image acquistion time - first image acquisition time) \n"] + absolute_text_area = [ + "file name -> (current image acquistion time - first image acquisition time) \n" + ] for _index, _file in enumerate(self.list_files): _short_file = os.path.basename(_file) _absolute_time = self.absolute_time_offset[_index] @@ -162,31 +173,56 @@ def display_selected_image(index, text_x, text_y, pre_text, post_text, color): ax = plt.subplot(gs[0, 0]) im = ax.imshow(self.images_array[index], interpolation="nearest") plt.title(f"image index {index}") - plt.text(text_x, text_y, f"{pre_text} {self.list_time_offset[index]:.2f}{post_text}", fontdict=font) + plt.text( + text_x, + text_y, + f"{pre_text} {self.list_time_offset[index]:.2f}{post_text}", + fontdict=font, + ) fig.colorbar(im) plt.show() - return {"text_x": text_x, "text_y": text_y, "pre_text": pre_text, "post_text": post_text, "color": color} + return { + "text_x": text_x, + "text_y": text_y, + "pre_text": pre_text, + "post_text": post_text, + "color": color, + } self.preview = interact( display_selected_image, - index=widgets.IntSlider(min=0, max=len(self.list_files), continuous_update=False), + index=widgets.IntSlider( + min=0, max=len(self.list_files), continuous_update=False + ), text_x=widgets.IntSlider( - min=0, max=width, value=text_x, description="Text x_offset", continuous_update=False + min=0, + max=width, + value=text_x, + description="Text x_offset", + continuous_update=False, ), text_y=widgets.IntSlider( - min=0, max=height, value=text_y, description="Text y_offset", continuous_upadte=False + min=0, + max=height, + value=text_y, + description="Text y_offset", + continuous_upadte=False, ), pre_text=widgets.Text(value="Time Offset", description="Pre text"), post_text=widgets.Text(value="(s)", description="Post text"), color=widgets.RadioButtons( - options=["red", "blue", "white", "black", "yellow"], value="red", description="Text Color" + options=["red", "blue", "white", "black", "yellow"], + value="red", + description="Text Color", ), ) def select_export_folder(self): self.output_folder_ui = ipywe.fileselector.FileSelectorPanel( - instruction="Select output Folder ...", start_dir=self.working_dir, type="directory" + instruction="Select output Folder ...", + start_dir=self.working_dir, + type="directory", ) self.output_folder_ui.show() @@ -220,7 +256,9 @@ def export(self): return input_folder_basename = os.path.basename(os.path.abspath(self.image_folder)) - output_file = os.path.abspath(os.path.join(output_folder, input_folder_basename + "_timestamp_infos.txt")) + output_file = os.path.abspath( + os.path.join(output_folder, input_folder_basename + "_timestamp_infos.txt") + ) if os.path.exists(output_file): os.remove(output_file) @@ -238,4 +276,6 @@ def export(self): with open(output_file, "w") as f: f.write(text) - display(HTML("File Created: " + os.path.basename(output_file) + "")) + display( + HTML("File Created: " + os.path.basename(output_file) + "") + ) diff --git a/notebooks/__code/display_imaging_resonance_sample_definition.py b/notebooks/__code/display_imaging_resonance_sample_definition.py index b3a963376..b8cdb049b 100755 --- a/notebooks/__code/display_imaging_resonance_sample_definition.py +++ b/notebooks/__code/display_imaging_resonance_sample_definition.py @@ -66,13 +66,21 @@ def validate_table_input_clicked(self): _layer_name = self.get_table_item(_row_index, 0) if _layer_name == "": break - _dict["elements"] = self.format_string_to_array(string=self.get_table_item(_row_index, 1), data_type="str") + _dict["elements"] = self.format_string_to_array( + string=self.get_table_item(_row_index, 1), data_type="str" + ) _dict["stoichiometric_ratio"] = self.format_string_to_array( string=self.get_table_item(_row_index, 2), data_type="float" ) - _dict["thickness"] = {"value": float(self.get_table_item(_row_index, 3)), "units": "mm"} + _dict["thickness"] = { + "value": float(self.get_table_item(_row_index, 3)), + "units": "mm", + } if self.get_table_item(_row_index, 4): - _dict["density"] = {"value": float(self.get_table_item(_row_index, 4)), "units": "g/cm3"} + _dict["density"] = { + "value": float(self.get_table_item(_row_index, 4)), + "units": "g/cm3", + } _table_dictionary[_layer_name] = _dict self.stack = _table_dictionary @@ -80,7 +88,9 @@ def validate_table_input_clicked(self): E_max = float(str(self.ui.Emax_lineEdit.text())) delta_E = float(str(self.ui.deltaE_lineEdit.text())) - o_reso = Resonance(stack=self.stack, energy_min=E_min, energy_max=E_max, energy_step=delta_E) + o_reso = Resonance( + stack=self.stack, energy_min=E_min, energy_max=E_max, energy_step=delta_E + ) self.o_reso = o_reso self.fill_check_groupBox() @@ -127,11 +137,15 @@ def element_combobox_clicked(self, element_selected): return _entry = self.stack[layer_selected][element_selected] - number_of_atoms = float(self.stack[layer_selected]["atoms_per_cm3"][element_selected]) + number_of_atoms = float( + self.stack[layer_selected]["atoms_per_cm3"][element_selected] + ) self.ui.element_number_of_atoms.setText(f"{number_of_atoms:6.3e}") density = str(self.stack[layer_selected][element_selected]["density"]["value"]) self.ui.element_density.setText(f"{float(density):6.3e}") - molar_mass = str(self.stack[layer_selected][element_selected]["molar_mass"]["value"]) + molar_mass = str( + self.stack[layer_selected][element_selected]["molar_mass"]["value"] + ) self.ui.element_molar_mass.setText(f"{float(molar_mass):6.3e}") self.fill_isotopes_table(element_selected) @@ -239,7 +253,9 @@ def define_new_isotopic_ratio(self): list_isotopic_ratio.append(_iso_ratio) self.o_reso.set_isotopic_ratio( - compound=layer_selected, element=element_selected, list_ratio=list_isotopic_ratio + compound=layer_selected, + element=element_selected, + list_ratio=list_isotopic_ratio, ) self.layer_combobox_clicked(layer_selected) diff --git a/notebooks/__code/display_integrated_stack_of_images.py b/notebooks/__code/display_integrated_stack_of_images.py index 087e34ebb..bde3a6c26 100755 --- a/notebooks/__code/display_integrated_stack_of_images.py +++ b/notebooks/__code/display_integrated_stack_of_images.py @@ -11,13 +11,18 @@ def __init__(self, working_dir=""): def select_input_folder(self): self.input_folder_ui = ipywe.fileselector.FileSelectorPanel( - instruction="Select Input Folder", type="directory", start_dir=self.working_dir, multiple=False + instruction="Select Input Folder", + type="directory", + start_dir=self.working_dir, + multiple=False, ) self.input_folder_ui.show() def __retrieve_files(self): input_folder = self.input_folder_ui.selected - list_files = file_handler.retrieve_list_of_most_dominant_extension_from_folder(folder=input_folder)[0] + list_files = file_handler.retrieve_list_of_most_dominant_extension_from_folder( + folder=input_folder + )[0] self.list_files = file_handler.remove_file_from_list( list_files=list_files, regular_expression=".*_SummedImg.fits" ) diff --git a/notebooks/__code/dual_energy/dual_energy.py b/notebooks/__code/dual_energy/dual_energy.py index 3ed0a6e80..a16a246e3 100755 --- a/notebooks/__code/dual_energy/dual_energy.py +++ b/notebooks/__code/dual_energy/dual_energy.py @@ -30,7 +30,10 @@ def load_ob(self, folder_selected): class Interface(QMainWindow): live_image = None # image displayed on the left (integrated sample images) - profile_selection_range = [5, 20] # in index units, the min and max ROI ranges in the right plot (profile plot) + profile_selection_range = [ + 5, + 20, + ] # in index units, the min and max ROI ranges in the right plot (profile plot) profile_selection_range_ui = None # ROI ui of profile # # relative index of the bragg peak only part (kropff and March-Dollase) @@ -47,7 +50,9 @@ class Interface(QMainWindow): selection_roi_rgb = (62, 13, 244) roi_settings = { - "color": QtGui.QColor(selection_roi_rgb[0], selection_roi_rgb[1], selection_roi_rgb[2]), + "color": QtGui.QColor( + selection_roi_rgb[0], selection_roi_rgb[1], selection_roi_rgb[2] + ), "border_width": 0.01, "position": [10, 10], } @@ -55,7 +60,9 @@ class Interface(QMainWindow): bin_roi_rgb = (50, 50, 50, 200) bin_line_settings = { - "color": QtGui.QColor(bin_roi_rgb[0], bin_roi_rgb[1], bin_roi_rgb[2], bin_roi_rgb[3]), + "color": QtGui.QColor( + bin_roi_rgb[0], bin_roi_rgb[1], bin_roi_rgb[2], bin_roi_rgb[3] + ), "width": 0.005, } @@ -81,7 +88,11 @@ class Interface(QMainWindow): # image_size = {'width': None, # 'height': None} # roi_id = None - xaxis_label = {"index": "File index", "tof": "TOF (\u00b5s)", "lambda": "\u03bb (\u212b)"} + xaxis_label = { + "index": "File index", + "tof": "TOF (\u00b5s)", + "lambda": "\u03bb (\u212b)", + } xaxis_units = {"index": "File #", "tof": "\u00b5s", "lambda": "\u212b"} # fitting_rois = {'kropff': {'step1': None, @@ -127,7 +138,8 @@ def __init__(self, parent=None, working_dir="", o_dual=None, spectra_file=None): super(Interface, self).__init__(parent) ui_full_path = os.path.join( - os.path.dirname(os.path.dirname(os.path.dirname(__file__))), os.path.join("ui", "ui_dual_energy.ui") + os.path.dirname(os.path.dirname(os.path.dirname(__file__))), + os.path.join("ui", "ui_dual_energy.ui"), ) self.ui = load_ui(ui_full_path, baseinstance=self) @@ -169,7 +181,9 @@ def update_time_spectra(self): distance_source_detector_m = float(self.ui.distance_detector_sample.text()) self.ui.statusbar.showMessage("", 100) # 10s except ValueError: - self.ui.statusbar.showMessage("distance source detector input is WRONG", 120000) # 2mn + self.ui.statusbar.showMessage( + "distance source detector input is WRONG", 120000 + ) # 2mn self.ui.statusbar.setStyleSheet("color: red") return @@ -177,7 +191,9 @@ def update_time_spectra(self): detector_offset_micros = float(self.ui.detector_offset.text()) self.ui.statusbar.showMessage("", 100) # 10s except ValueError: - self.ui.statusbar.showMessage("detector offset input is WRONG", 120000) # 2mn + self.ui.statusbar.showMessage( + "detector offset input is WRONG", 120000 + ) # 2mn self.ui.statusbar.setStyleSheet("color: red") return @@ -197,7 +213,9 @@ def get_live_image(self): _data = self.o_norm.data["sample"]["data"] nbr_images = len(_data) - list_of_indexes_to_keep = random.sample(list(range(nbr_images)), nbr_data_to_use) + list_of_indexes_to_keep = random.sample( + list(range(nbr_images)), nbr_data_to_use + ) final_array = [] for _index in list_of_indexes_to_keep: @@ -241,7 +259,10 @@ def bragg_edge_range_changed(self): def reset_profile_of_bin_size_slider(self): max_value = np.min( - [int(str(self.ui.profile_of_bin_size_width.text())), int(str(self.ui.profile_of_bin_size_height.text()))] + [ + int(str(self.ui.profile_of_bin_size_width.text())), + int(str(self.ui.profile_of_bin_size_height.text())), + ] ) self.ui.profile_of_bin_size_slider.setMaximum(max_value) self.ui.profile_of_bin_size_slider.setValue(max_value) @@ -315,12 +336,19 @@ def update_vertical_line_in_profile_plot(self): o_get = Get(parent=self) x_axis, x_axis_label = o_get.x_axis() - bragg_edge_range = [x_axis[self.bragg_edge_range[0]], x_axis[self.bragg_edge_range[1]]] + bragg_edge_range = [ + x_axis[self.bragg_edge_range[0]], + x_axis[self.bragg_edge_range[1]], + ] if self.bragg_edge_range_ui: self.ui.profile.removeItem(self.bragg_edge_range_ui) self.bragg_edge_range_ui = pg.LinearRegionItem( - values=bragg_edge_range, orientation=None, brush=None, movable=True, bounds=None + values=bragg_edge_range, + orientation=None, + brush=None, + movable=True, + bounds=None, ) self.bragg_edge_range_ui.sigRegionChanged.connect(self.bragg_edge_range_changed) self.bragg_edge_range_ui.setZValue(-10) diff --git a/notebooks/__code/dual_energy/get.py b/notebooks/__code/dual_energy/get.py index 7459d5d9d..d080c26f8 100755 --- a/notebooks/__code/dual_energy/get.py +++ b/notebooks/__code/dual_energy/get.py @@ -38,7 +38,9 @@ def selection_roi_dimension(self): roi_id = self.parent.roi_id if roi_id: - region = roi_id.getArraySlice(self.parent.live_image, self.parent.ui.image_view.imageItem) + region = roi_id.getArraySlice( + self.parent.live_image, self.parent.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop y0 = region[0][1].start diff --git a/notebooks/__code/dual_energy/interface_initialization.py b/notebooks/__code/dual_energy/interface_initialization.py index 1811b9e17..4acfe6d59 100755 --- a/notebooks/__code/dual_energy/interface_initialization.py +++ b/notebooks/__code/dual_energy/interface_initialization.py @@ -122,9 +122,13 @@ def labels(self): self.parent.ui.selection_lambda_radiobutton.setText("\u03bb (\u212b)") def text_fields(self): - self.parent.ui.distance_detector_sample.setText(str(self.distance_detector_sample)) + self.parent.ui.distance_detector_sample.setText( + str(self.distance_detector_sample) + ) self.parent.ui.detector_offset.setText(str(self.detector_offset)) - self.parent.ui.selection_bin_size_value.setText(str(self.parent.bin_size_value["index"])) + self.parent.ui.selection_bin_size_value.setText( + str(self.parent.bin_size_value["index"]) + ) def widgets(self): self.parent.ui.splitter.setSizes([500, 400]) @@ -133,8 +137,12 @@ def widgets(self): self.parent.ui.calculation_bin_table.cellClicked["int", "int"].connect( self.parent.calculation_table_cell_clicked ) - self.parent.ui.calculation_bin_table.setEditTriggers(QAbstractItemView.NoEditTriggers) - self.parent.ui.verticalLayout_table.addWidget(self.parent.ui.calculation_bin_table) + self.parent.ui.calculation_bin_table.setEditTriggers( + QAbstractItemView.NoEditTriggers + ) + self.parent.ui.verticalLayout_table.addWidget( + self.parent.ui.calculation_bin_table + ) def roi_setup(self): [x0, y0] = self.parent.roi_settings["position"] diff --git a/notebooks/__code/dual_energy/my_table_widget.py b/notebooks/__code/dual_energy/my_table_widget.py index 39462239d..8f863cfce 100755 --- a/notebooks/__code/dual_energy/my_table_widget.py +++ b/notebooks/__code/dual_energy/my_table_widget.py @@ -20,7 +20,11 @@ def focusOutEvent(self, event): return QTableWidget.focusOutEvent(self, event) def event(self, event): - if self.catch and event.type() == QEvent.KeyRelease and event.key() in self.keys: + if ( + self.catch + and event.type() == QEvent.KeyRelease + and event.key() in self.keys + ): self._moveCursor(event.key()) return QTableWidget.event(self, event) diff --git a/notebooks/__code/dual_energy/selection_tab.py b/notebooks/__code/dual_energy/selection_tab.py index 699d53196..55ed42f14 100755 --- a/notebooks/__code/dual_energy/selection_tab.py +++ b/notebooks/__code/dual_energy/selection_tab.py @@ -33,7 +33,11 @@ def update_selection_profile_plot(self): self.parent.ui.profile.plot( x_axis, y_axis, - pen=(self.parent.selection_roi_rgb[0], self.parent.selection_roi_rgb[1], self.parent.selection_roi_rgb[2]), + pen=( + self.parent.selection_roi_rgb[0], + self.parent.selection_roi_rgb[1], + self.parent.selection_roi_rgb[2], + ), ) self.parent.ui.profile.setLabel("bottom", x_axis_label) @@ -46,10 +50,18 @@ def update_selection_profile_plot(self): ] self.parent.profile_selection_range_ui = pg.LinearRegionItem( - values=profile_selection_range, orientation=None, brush=None, movable=True, bounds=None + values=profile_selection_range, + orientation=None, + brush=None, + movable=True, + bounds=None, + ) + self.parent.profile_selection_range_ui.sigRegionChanged.connect( + self.parent.profile_selection_range_changed + ) + self.parent.profile_selection_range_ui.sigRegionChangeFinished.connect( + self.parent.roi_moved ) - self.parent.profile_selection_range_ui.sigRegionChanged.connect(self.parent.profile_selection_range_changed) - self.parent.profile_selection_range_ui.sigRegionChangeFinished.connect(self.parent.roi_moved) self.parent.profile_selection_range_ui.setZValue(-10) self.parent.ui.profile.addItem(self.parent.profile_selection_range_ui) @@ -100,7 +112,9 @@ def calculate_bin_size_in_all_units(self): bin_tof = tof_array_s[current_value] bin_lambda = lambda_array[current_value] elif current_axis_name == "tof": - bin_index = find_nearest_index(array=tof_array_s, value=current_value * 1e-6) + bin_index = find_nearest_index( + array=tof_array_s, value=current_value * 1e-6 + ) bin_tof = current_value bin_lambda = lambda_array[bin_index] elif current_axis_name == "lambda": @@ -108,7 +122,11 @@ def calculate_bin_size_in_all_units(self): bin_tof = tof_array_s[bin_index] bin_lambda = current_value - self.parent.bin_size_value = {"index": bin_index, "tof": bin_tof, "lambda": bin_lambda} + self.parent.bin_size_value = { + "index": bin_index, + "tof": bin_tof, + "lambda": bin_lambda, + } def make_list_of_bins(self): [from_index, to_index] = self.parent.profile_selection_range @@ -139,7 +157,9 @@ def update_all_size_widgets_infos(self): return roi_id = self.parent.roi_id - region = roi_id.getArraySlice(self.parent.final_image, self.parent.ui.image_view.imageItem) + region = roi_id.getArraySlice( + self.parent.final_image, self.parent.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop y0 = region[0][1].start @@ -192,7 +212,9 @@ def update_selection(self, new_value=None, mode="square"): return try: - region = self.parent.roi_id.getArraySlice(self.parent.final_image, self.parent.ui.image_view.imageItem) + region = self.parent.roi_id.getArraySlice( + self.parent.final_image, self.parent.ui.image_view.imageItem + ) except TypeError: return @@ -206,7 +228,9 @@ def update_selection(self, new_value=None, mode="square"): _pen = QtGui.QPen() _pen.setColor(self.parent.roi_settings["color"]) _pen.setWidthF(self.parent.roi_settings["width"]) - self.parent.roi_id = pg.ROI([x0, y0], [new_value, new_value], pen=_pen, scaleSnap=True) + self.parent.roi_id = pg.ROI( + [x0, y0], [new_value, new_value], pen=_pen, scaleSnap=True + ) self.parent.ui.image_view.addItem(self.parent.roi_id) self.parent.roi_id.sigRegionChanged.connect(self.parent.roi_moved) @@ -234,7 +258,11 @@ def update_selection_plot(self): self.parent.ui.profile.plot( x_axis, y_axis, - pen=(self.parent.shrinking_roi_rgb[0], self.parent.shrinking_roi_rgb[1], self.parent.shrinking_roi_rgb[2]), + pen=( + self.parent.shrinking_roi_rgb[0], + self.parent.shrinking_roi_rgb[1], + self.parent.shrinking_roi_rgb[2], + ), ) self.parent.ui.profile.setLabel("bottom", x_axis_label) self.parent.ui.profile.setLabel("left", "Mean transmission") @@ -244,7 +272,11 @@ def update_selection_plot(self): self.parent.ui.profile.plot( x_axis, y_axis, - pen=(self.parent.selection_roi_rgb[0], self.parent.selection_roi_rgb[1], self.parent.selection_roi_rgb[2]), + pen=( + self.parent.selection_roi_rgb[0], + self.parent.selection_roi_rgb[1], + self.parent.selection_roi_rgb[2], + ), ) def profile_of_bin_size_slider_changed(self, new_value): @@ -293,7 +325,9 @@ def update_roi_defined_by_profile_of_bin_size_slider(self): _pen.setColor(self.parent.shrinking_roi_settings["color"]) _pen.setWidthF(self.parent.shrinking_roi_settings["width"]) - self.parent.shrinking_roi_id = pg.ROI([x0, y0], [width, height], pen=_pen, scaleSnap=True, movable=False) + self.parent.shrinking_roi_id = pg.ROI( + [x0, y0], [width, height], pen=_pen, scaleSnap=True, movable=False + ) self.parent.ui.image_view.addItem(self.parent.shrinking_roi_id) def update_profile_of_bin_slider_widget(self): @@ -307,7 +341,9 @@ def update_profile_of_bin_slider_widget(self): "height": fitting_input_dictionary["rois"][_key]["height"], } self.parent.dict_rois_imported = dict_rois_imported - self.parent.ui.profile_of_bin_size_slider.setRange(0, len(dict_rois_imported) - 1) + self.parent.ui.profile_of_bin_size_slider.setRange( + 0, len(dict_rois_imported) - 1 + ) # self.parent.ui.profile_of_bin_size_slider.setMinimum(0) # self.parent.ui.profile_of_bin_size_slider.setMaximum(len(dict_rois_imported)-1) self.parent.ui.profile_of_bin_size_slider.setSingleStep(1) @@ -336,7 +372,9 @@ def calculate_big_table(self): left_bin = list_bin_index[n_bin] right_bin = list_bin_index[n_bin + 1] mean_images_of_bin = np.nanmean(list_data[left_bin:right_bin][:][:], axis=0) - mean_images_of_bin_and_selection = np.nanmean(mean_images_of_bin[y0:y1, x0:x1]) + mean_images_of_bin_and_selection = np.nanmean( + mean_images_of_bin[y0:y1, x0:x1] + ) list_mean_counts_of_bin.append(mean_images_of_bin_and_selection) o_table = TableHandler(table_ui=self.parent.ui.calculation_bin_table) @@ -344,7 +382,9 @@ def calculate_big_table(self): row_and_column_of_max_ratio_value = {"row": [], "column": []} for _row in np.arange(nbr_bin): for _col in np.arange(_row, nbr_bin): - bin_col_divided_by_bin_row = list_mean_counts_of_bin[_col] / list_mean_counts_of_bin[_row] + bin_col_divided_by_bin_row = ( + list_mean_counts_of_bin[_col] / list_mean_counts_of_bin[_row] + ) diff_with_1 = np.abs(1 - bin_col_divided_by_bin_row) if diff_with_1 > max_ratio_value: max_ratio_value = diff_with_1 @@ -362,9 +402,13 @@ def calculate_big_table(self): # change the background of the max_ratio_value_cell for _row, _col in zip( - row_and_column_of_max_ratio_value["row"], row_and_column_of_max_ratio_value["column"], strict=False + row_and_column_of_max_ratio_value["row"], + row_and_column_of_max_ratio_value["column"], + strict=False, ): - o_table.set_background_color(row=_row, column=_col, qcolor=self.background_color_of_max_bin_ratio) + o_table.set_background_color( + row=_row, column=_col, qcolor=self.background_color_of_max_bin_ratio + ) self.fill_summary_table( bin_index_1=row_and_column_of_max_ratio_value["row"][0], @@ -378,11 +422,15 @@ def calculate_big_table(self): @staticmethod def get_file_index_range(bin_index=None, list_bin_positions=None): - return SelectionTab.get_range_for_given_key(key="index", index=bin_index, list_bin_positions=list_bin_positions) + return SelectionTab.get_range_for_given_key( + key="index", index=bin_index, list_bin_positions=list_bin_positions + ) @staticmethod def get_tof_index_range(bin_index=None, list_bin_positions=None): - return SelectionTab.get_range_for_given_key(key="tof", index=bin_index, list_bin_positions=list_bin_positions) + return SelectionTab.get_range_for_given_key( + key="tof", index=bin_index, list_bin_positions=list_bin_positions + ) @staticmethod def get_lambda_index_range(bin_index=None, list_bin_positions=None): @@ -420,14 +468,22 @@ def fill_summary_table(self, bin_index_1=None, bin_index_2=None): (from_tof_index, to_tof_index) = SelectionTab.get_tof_index_range( bin_index=bin_index_1, list_bin_positions=list_bin_positions ) - o_table.insert_item(row=0, column=3, value=from_tof_index * 1e6, format_str="{:f}") - o_table.insert_item(row=0, column=4, value=to_tof_index * 1e6, format_str="{:f}") + o_table.insert_item( + row=0, column=3, value=from_tof_index * 1e6, format_str="{:f}" + ) + o_table.insert_item( + row=0, column=4, value=to_tof_index * 1e6, format_str="{:f}" + ) (from_tof_index, to_tof_index) = SelectionTab.get_tof_index_range( bin_index=bin_index_2, list_bin_positions=list_bin_positions ) - o_table.insert_item(row=1, column=3, value=from_tof_index * 1e6, format_str="{:f}") - o_table.insert_item(row=1, column=4, value=to_tof_index * 1e6, format_str="{:2f}") + o_table.insert_item( + row=1, column=3, value=from_tof_index * 1e6, format_str="{:f}" + ) + o_table.insert_item( + row=1, column=4, value=to_tof_index * 1e6, format_str="{:2f}" + ) # lambda index (from_lambda_index, to_lambda_index) = SelectionTab.get_lambda_index_range( @@ -482,7 +538,10 @@ def display_image_of_best_ratio(self): self.parent.ui.image_ratio_view.setImage(_image) o_table = TableHandler(table_ui=self.parent.ui.calculation_bin_table) - o_table.select_cell(row=optimum_bin_ratio["bin_number_1"], column=optimum_bin_ratio["bin_number_2"]) + o_table.select_cell( + row=optimum_bin_ratio["bin_number_1"], + column=optimum_bin_ratio["bin_number_2"], + ) self.save_widget_enabled(enabled=True) self.parent.calculated_live_image = _image @@ -502,12 +561,16 @@ def display_image_of_selected_cell(self, row=0, column=0): from_index1 = row to_index1 = from_index1 + 1 - image_stack1 = data[list_bin_index[from_index1] : list_bin_index[to_index1]][:][:] + image_stack1 = data[list_bin_index[from_index1] : list_bin_index[to_index1]][:][ + : + ] image1 = np.mean(image_stack1, axis=0) from_index2 = column to_index2 = from_index2 + 1 - image_stack2 = data[list_bin_index[from_index2] : list_bin_index[to_index2]][:][:] + image_stack2 = data[list_bin_index[from_index2] : list_bin_index[to_index2]][:][ + : + ] image2 = np.mean(image_stack2, axis=0) index_of_0 = np.where(image2 == 0) diff --git a/notebooks/__code/extract_evenly_spaced_files/event_handler.py b/notebooks/__code/extract_evenly_spaced_files/event_handler.py index 46535768b..450d7cdb5 100755 --- a/notebooks/__code/extract_evenly_spaced_files/event_handler.py +++ b/notebooks/__code/extract_evenly_spaced_files/event_handler.py @@ -8,7 +8,9 @@ from __code._utilities.list_widget import ListWidget from __code._utilities.status_message import StatusMessageStatus, show_status_message from __code.extract_evenly_spaced_files.load import load_file -from __code.extract_evenly_spaced_files.manual_mode_interface_handler import Interface as ManualModeInterface +from __code.extract_evenly_spaced_files.manual_mode_interface_handler import ( + Interface as ManualModeInterface, +) from __code.extract_evenly_spaced_files.statistics import Statistics @@ -18,7 +20,11 @@ def __init__(self, parent=None): def load_files(self): logging.info("loading files ...") - show_status_message(parent=self.parent, message="Loading ...", status=StatusMessageStatus.working) + show_status_message( + parent=self.parent, + message="Loading ...", + status=StatusMessageStatus.working, + ) list_files = self.parent.list_of_files_that_will_be_extracted nbr_files = len(list_files) @@ -38,8 +44,15 @@ def load_files(self): self.parent.list_data = list_data self.parent.eventProgress.setVisible(False) - logging.info(f"file loaded! np.shape(list_data): {np.shape(self.parent.list_data)}") - show_status_message(parent=self.parent, message="Done loading!", status=StatusMessageStatus.ready, duration_s=5) + logging.info( + f"file loaded! np.shape(list_data): {np.shape(self.parent.list_data)}" + ) + show_status_message( + parent=self.parent, + message="Done loading!", + status=StatusMessageStatus.ready, + duration_s=5, + ) QGuiApplication.processEvents() def select_first_file(self): @@ -65,13 +78,17 @@ def image_selected_changed(self): _view_box.setState(_state) if not first_update: - _histo_widget.setLevels(self.parent.histogram_level[0], self.parent.histogram_level[1]) + _histo_widget.setLevels( + self.parent.histogram_level[0], self.parent.histogram_level[1] + ) def list_files_right_click(self): menu = QMenu(self.parent) remove_action = menu.addAction("Remove") - if len(self.parent.basename_list_of_files_that_will_be_extracted) != len(self.parent.full_raw_list_of_files): + if len(self.parent.basename_list_of_files_that_will_be_extracted) != len( + self.parent.full_raw_list_of_files + ): replace_with_action = menu.addAction("Replace with ...") action = menu.exec_(QtGui.QCursor.pos()) @@ -87,10 +104,14 @@ def remove_this_file_clicked(self): o_list = ListWidget(ui=self.parent.ui.list_of_files_listWidget) index_file_selected = o_list.get_current_row() - del self.parent.basename_list_of_files_that_will_be_extracted[index_file_selected] + del self.parent.basename_list_of_files_that_will_be_extracted[ + index_file_selected + ] del self.parent.list_data[index_file_selected] self.parent.ui.list_of_files_listWidget.clear() - self.parent.ui.list_of_files_listWidget.addItems(self.parent.basename_list_of_files_that_will_be_extracted) + self.parent.ui.list_of_files_listWidget.addItems( + self.parent.basename_list_of_files_that_will_be_extracted + ) o_list.select_element(row=index_file_selected - 1) self.update_manual_interface() diff --git a/notebooks/__code/extract_evenly_spaced_files/get.py b/notebooks/__code/extract_evenly_spaced_files/get.py index a75b945a1..6dd50538e 100755 --- a/notebooks/__code/extract_evenly_spaced_files/get.py +++ b/notebooks/__code/extract_evenly_spaced_files/get.py @@ -28,7 +28,9 @@ def list_of_files_to_extract(self): return list_of_files_to_extract def renamed_basename_list_of_files(self, prefix): - list_of_files_to_extract = self.parent.basename_list_of_files_that_will_be_extracted + list_of_files_to_extract = ( + self.parent.basename_list_of_files_that_will_be_extracted + ) if prefix: prefix += "_" diff --git a/notebooks/__code/extract_evenly_spaced_files/interface_handler.py b/notebooks/__code/extract_evenly_spaced_files/interface_handler.py index e288e18c7..8409a9edb 100755 --- a/notebooks/__code/extract_evenly_spaced_files/interface_handler.py +++ b/notebooks/__code/extract_evenly_spaced_files/interface_handler.py @@ -7,7 +7,9 @@ from __code._utilities.list_widget import ListWidget from __code.extract_evenly_spaced_files.event_handler import EventHandler from __code.extract_evenly_spaced_files.get import Get -from __code.extract_evenly_spaced_files.interface_initialization import InterfaceInitialization +from __code.extract_evenly_spaced_files.interface_initialization import ( + InterfaceInitialization, +) from __code.extract_evenly_spaced_files.statistics import Statistics @@ -46,17 +48,23 @@ def __init__(self, parent=None, o_extract=None): self.parent = parent self.o_extract = o_extract - self.basename_list_of_files_that_will_be_extracted = o_extract.basename_list_of_files_that_will_be_extracted + self.basename_list_of_files_that_will_be_extracted = ( + o_extract.basename_list_of_files_that_will_be_extracted + ) self.list_of_files_that_will_be_extracted = o_extract.list_of_files_to_extract self.full_raw_list_of_files = o_extract.list_files - self.full_base_list_of_files = [os.path.basename(_file) for _file in self.full_raw_list_of_files] + self.full_base_list_of_files = [ + os.path.basename(_file) for _file in self.full_raw_list_of_files + ] self.extracting_value = self.o_extract.extracting_ui.value logging.info( f"number of files to extract: len(list_of_files_to_extract) = " f" {len(self.list_of_files_that_will_be_extracted)}" ) - logging.info(f"number of full list of files: len(full_raw_list_of_files) = {len(self.full_raw_list_of_files)}") + logging.info( + f"number of full list of files: len(full_raw_list_of_files) = {len(self.full_raw_list_of_files)}" + ) super(InterfaceHandler, self).__init__(parent) ui_full_path = os.path.join( @@ -127,8 +135,12 @@ def ok_clicked(self): if self.manual_interface_id: self.manual_interface_id.close() - basename_list_of_files_that_will_be_extracted = self.basename_list_of_files_that_will_be_extracted - self.o_extract.basename_list_of_files_that_will_be_extracted = basename_list_of_files_that_will_be_extracted + basename_list_of_files_that_will_be_extracted = ( + self.basename_list_of_files_that_will_be_extracted + ) + self.o_extract.basename_list_of_files_that_will_be_extracted = ( + basename_list_of_files_that_will_be_extracted + ) self.close() def closeEvent(self, event=None): diff --git a/notebooks/__code/extract_evenly_spaced_files/interface_initialization.py b/notebooks/__code/extract_evenly_spaced_files/interface_initialization.py index 2d7d24d4c..ba10e81a2 100755 --- a/notebooks/__code/extract_evenly_spaced_files/interface_initialization.py +++ b/notebooks/__code/extract_evenly_spaced_files/interface_initialization.py @@ -31,7 +31,9 @@ def pyqtgraph(self): def widgets(self): self.parent.ui.list_of_files_listWidget.blockSignals(True) - base_list_of_files_to_extract = self.parent.basename_list_of_files_that_will_be_extracted + base_list_of_files_to_extract = ( + self.parent.basename_list_of_files_that_will_be_extracted + ) self.parent.ui.list_of_files_listWidget.addItems(base_list_of_files_to_extract) self.parent.ui.list_of_files_listWidget.blockSignals(False) diff --git a/notebooks/__code/extract_evenly_spaced_files/main.py b/notebooks/__code/extract_evenly_spaced_files/main.py index e109bca54..25a1ab29b 100755 --- a/notebooks/__code/extract_evenly_spaced_files/main.py +++ b/notebooks/__code/extract_evenly_spaced_files/main.py @@ -29,7 +29,11 @@ def select_folder(self): self.folder_widget.show() def retrieve_list_of_files(self, folder_selected): - [self.list_files, _] = file_handler.retrieve_list_of_most_dominant_extension_from_folder(folder=folder_selected) + [self.list_files, _] = ( + file_handler.retrieve_list_of_most_dominant_extension_from_folder( + folder=folder_selected + ) + ) self.folder_of_files_to_extract = folder_selected sorting_algorithm = self.sorting_ui.value @@ -43,8 +47,12 @@ def retrieve_list_of_files(self, folder_selected): self.list_files = list_files[index_sorted] self.list_of_files_to_extract = self.list_files - self.basename_list_files = [os.path.basename(_file) for _file in self.list_files] - self.basename_list_of_files_that_will_be_extracted = [os.path.basename(_file) for _file in self.list_files] + self.basename_list_files = [ + os.path.basename(_file) for _file in self.list_files + ] + self.basename_list_of_files_that_will_be_extracted = [ + os.path.basename(_file) for _file in self.list_files + ] self.number_of_files = len(self.list_files) display( HTML( @@ -55,7 +63,9 @@ def retrieve_list_of_files(self, folder_selected): ) def sorting_method(self): - self.sorting_ui = widgets.RadioButtons(options=["Time", "File Name"], value="File Name") + self.sorting_ui = widgets.RadioButtons( + options=["Time", "File Name"], value="File Name" + ) display(self.sorting_ui) def how_to_extract(self): @@ -66,7 +76,9 @@ def how_to_extract(self): [ widgets.Label("Extract 1 over ", layout=widgets.Layout(width="100px")), widgets.Dropdown( - options=np.arange(1, self.number_of_files), value=2, layout=widgets.Layout(width="50px") + options=np.arange(1, self.number_of_files), + value=2, + layout=widgets.Layout(width="50px"), ), widgets.Label("files", layout=widgets.Layout(width="60px")), ] @@ -77,7 +89,8 @@ def how_to_extract(self): hori_layout_2 = widgets.HBox( [ widgets.Label( - self.extract_message.format(o_get.number_of_files_to_extract()), layout=widgets.Layout(width="40%") + self.extract_message.format(o_get.number_of_files_to_extract()), + layout=widgets.Layout(width="40%"), ) ] ) @@ -86,8 +99,13 @@ def how_to_extract(self): # list of files that will be extracted hori_layout_3 = widgets.HBox( [ - widgets.Label("List of files extracted", layout=widgets.Layout(width="20%")), - widgets.Select(options=self.basename_list_files, layout=widgets.Layout(width="80%", height="400px")), + widgets.Label( + "List of files extracted", layout=widgets.Layout(width="20%") + ), + widgets.Select( + options=self.basename_list_files, + layout=widgets.Layout(width="80%", height="400px"), + ), ] ) self.list_of_files_that_will_be_extracted_ui = hori_layout_3.children[1] @@ -103,7 +121,9 @@ def how_to_extract(self): def update_extracting_value(self, _): o_get = Get(parent=self) nbr_files_extracted = o_get.number_of_files_to_extract() - self.extracting_label_ui.value = self.extract_message.format(nbr_files_extracted) + self.extracting_label_ui.value = self.extract_message.format( + nbr_files_extracted + ) list_of_files_that_will_be_extracted = o_get.list_of_files_to_extract() self.list_of_files_to_extract = list_of_files_that_will_be_extracted @@ -111,8 +131,12 @@ def update_extracting_value(self, _): basename_list_of_files_that_will_be_extracted = [ os.path.basename(_file) for _file in list_of_files_that_will_be_extracted ] - self.list_of_files_that_will_be_extracted_ui.options = basename_list_of_files_that_will_be_extracted - self.basename_list_of_files_that_will_be_extracted = basename_list_of_files_that_will_be_extracted + self.list_of_files_that_will_be_extracted_ui.options = ( + basename_list_of_files_that_will_be_extracted + ) + self.basename_list_of_files_that_will_be_extracted = ( + basename_list_of_files_that_will_be_extracted + ) def renaming_checkbox_changed(self, value): if value["new"]: @@ -125,19 +149,27 @@ def renaming_checkbox_changed(self, value): def prefix_changed(self, new_value): o_get = Get(parent=self) new_prefix = new_value["new"] - self.renamed_basename_list_of_files = o_get.renamed_basename_list_of_files(prefix=new_prefix) + self.renamed_basename_list_of_files = o_get.renamed_basename_list_of_files( + prefix=new_prefix + ) self.new_file_name_ui.options = self.renamed_basename_list_of_files def renamed_files(self): o_get = Get(parent=self) - self.basename_list_of_files_that_will_be_extracted = self.basename_list_of_files_that_will_be_extracted + self.basename_list_of_files_that_will_be_extracted = ( + self.basename_list_of_files_that_will_be_extracted + ) question_widget = widgets.Checkbox(value=True, description="Rename files?") question_widget.observe(self.renaming_checkbox_changed, names="value") self.renaming_files_widget = question_widget - self.prefix_file_name = widgets.Text(value="", placeholder="Type prefix here", description="Prefix:") + self.prefix_file_name = widgets.Text( + value="", placeholder="Type prefix here", description="Prefix:" + ) self.prefix_file_name.observe(self.prefix_changed, names="value") - self.renamed_basename_list_of_files = o_get.renamed_basename_list_of_files(prefix=self.prefix_file_name.value) + self.renamed_basename_list_of_files = o_get.renamed_basename_list_of_files( + prefix=self.prefix_file_name.value + ) self.left_vertical_layout = widgets.VBox( [ @@ -153,14 +185,19 @@ def renamed_files(self): [ widgets.Label("After renaming", layout=widgets.Layout(width="100%")), widgets.Select( - options=self.renamed_basename_list_of_files, layout=widgets.Layout(width="90%", height="400px") + options=self.renamed_basename_list_of_files, + layout=widgets.Layout(width="90%", height="400px"), ), ], layout=widgets.Layout(width="40%"), ) self.new_file_name_ui = self.right_vertical_layout.children[1] - horizontal_layout = widgets.HBox([self.left_vertical_layout, self.right_vertical_layout]) - full_vertical_layout = widgets.VBox([question_widget, self.prefix_file_name, horizontal_layout]) + horizontal_layout = widgets.HBox( + [self.left_vertical_layout, self.right_vertical_layout] + ) + full_vertical_layout = widgets.VBox( + [question_widget, self.prefix_file_name, horizontal_layout] + ) display(full_vertical_layout) def select_output_folder(self): @@ -179,15 +216,22 @@ def extract(self, output_folder): list_of_files_to_extract = self.list_of_files_to_extract full_path_to_file = os.path.dirname(list_of_files_to_extract[0]) name_of_parent_folder = os.path.basename(full_path_to_file) - basename_list_of_files_that_will_be_extracted = self.basename_list_of_files_that_will_be_extracted + basename_list_of_files_that_will_be_extracted = ( + self.basename_list_of_files_that_will_be_extracted + ) fullname_list_of_files_that_will_be_extracted = [ - os.path.join(full_path_to_file, _file) for _file in basename_list_of_files_that_will_be_extracted + os.path.join(full_path_to_file, _file) + for _file in basename_list_of_files_that_will_be_extracted ] extracting_value = self.extracting_ui.value - new_folder = name_of_parent_folder + f"_extracted_1_every_{extracting_value}_files" - full_output_folder_name = os.path.abspath(os.path.join(output_folder, new_folder)) + new_folder = ( + name_of_parent_folder + f"_extracted_1_every_{extracting_value}_files" + ) + full_output_folder_name = os.path.abspath( + os.path.join(output_folder, new_folder) + ) file_handler.make_or_reset_folder(full_output_folder_name) if self.renaming_files_widget.value: @@ -227,7 +271,8 @@ def extract(self, output_folder): ) ) file_handler.copy_files_to_folder( - list_files=fullname_list_of_files_that_will_be_extracted, output_folder=full_output_folder_name + list_files=fullname_list_of_files_that_will_be_extracted, + output_folder=full_output_folder_name, ) display( HTML( diff --git a/notebooks/__code/extract_evenly_spaced_files/manual_mode_interface_handler.py b/notebooks/__code/extract_evenly_spaced_files/manual_mode_interface_handler.py index b5ebea8e6..71b0f7c64 100755 --- a/notebooks/__code/extract_evenly_spaced_files/manual_mode_interface_handler.py +++ b/notebooks/__code/extract_evenly_spaced_files/manual_mode_interface_handler.py @@ -61,7 +61,9 @@ def init(self): def update_current_image_name(self): o_list = ListWidget(ui=self.parent.ui.list_of_files_listWidget) index_file_selected = o_list.get_current_row() - file_name = self.parent.basename_list_of_files_that_will_be_extracted[index_file_selected] + file_name = self.parent.basename_list_of_files_that_will_be_extracted[ + index_file_selected + ] self.ui.name_of_current_file.setText(file_name) def close_clicked(self): @@ -78,10 +80,14 @@ def validate_change(self): new_file = self.ui.replace_by_comboBox.currentText() o_list = ListWidget(ui=self.parent.ui.list_of_files_listWidget) - self.parent.basename_list_of_files_that_will_be_extracted[index_file_selected] = os.path.basename(new_file) + self.parent.basename_list_of_files_that_will_be_extracted[ + index_file_selected + ] = os.path.basename(new_file) self.parent.ui.list_of_files_listWidget.clear() - self.parent.ui.list_of_files_listWidget.addItems(self.parent.basename_list_of_files_that_will_be_extracted) + self.parent.ui.list_of_files_listWidget.addItems( + self.parent.basename_list_of_files_that_will_be_extracted + ) self.parent.list_data[index_file_selected] = self.new_data self.parent.image_selected_changed() @@ -119,29 +125,40 @@ def update_replace_by_list(self): # index_file_selected_in_full_list = index_file_selected * extracting_value o_get = Get(parent=self.parent) - base_file_name = self.parent.basename_list_of_files_that_will_be_extracted[index_file_selected] - index_file_selected_in_full_list = o_get.index_of_file_selected_in_full_list(base_file_name) + base_file_name = self.parent.basename_list_of_files_that_will_be_extracted[ + index_file_selected + ] + index_file_selected_in_full_list = o_get.index_of_file_selected_in_full_list( + base_file_name + ) logging.info(f"base_file_name: {base_file_name}") - logging.info(f"index_file_selected_in_full_list: {index_file_selected_in_full_list}") + logging.info( + f"index_file_selected_in_full_list: {index_file_selected_in_full_list}" + ) if index_file_selected == 0: list_of_option_of_files_to_replace_with = full_list_of_files[ - index_file_selected_in_full_list : index_file_selected_in_full_list + extracting_value + index_file_selected_in_full_list : index_file_selected_in_full_list + + extracting_value ] elif index_file_selected == (o_list.get_number_elements() - 1): list_of_option_of_files_to_replace_with = full_list_of_files[ - index_file_selected_in_full_list - extracting_value : index_file_selected_in_full_list + index_file_selected_in_full_list + - extracting_value : index_file_selected_in_full_list ] else: list_of_option_of_files_to_replace_with = [] for _file in full_list_of_files[ - index_file_selected_in_full_list + 1 - extracting_value : index_file_selected_in_full_list + index_file_selected_in_full_list + + 1 + - extracting_value : index_file_selected_in_full_list ]: list_of_option_of_files_to_replace_with.append(_file) for _file in full_list_of_files[ - index_file_selected_in_full_list : index_file_selected_in_full_list + extracting_value + index_file_selected_in_full_list : index_file_selected_in_full_list + + extracting_value ]: list_of_option_of_files_to_replace_with.append(_file) diff --git a/notebooks/__code/extract_evenly_spaced_files/statistics.py b/notebooks/__code/extract_evenly_spaced_files/statistics.py index 3b13d03cb..e5396a68e 100755 --- a/notebooks/__code/extract_evenly_spaced_files/statistics.py +++ b/notebooks/__code/extract_evenly_spaced_files/statistics.py @@ -24,7 +24,11 @@ def full_update(self): def update_statistics(self): logging.info("Updating statistics ...") - show_status_message(parent=self.parent, message="Updating statistics ...", status=StatusMessageStatus.working) + show_status_message( + parent=self.parent, + message="Updating statistics ...", + status=StatusMessageStatus.working, + ) QGuiApplication.processEvents() list_data = self.parent.list_data @@ -45,7 +49,10 @@ def update_statistics(self): logging.info("Statistics plot done!") show_status_message( - parent=self.parent, message="Updated statistics!", status=StatusMessageStatus.ready, duration_s=5 + parent=self.parent, + message="Updated statistics!", + status=StatusMessageStatus.ready, + duration_s=5, ) QGuiApplication.processEvents() @@ -69,9 +76,13 @@ def plot_statistics(self): def init_plot_statistics_threshold(self): max_value = self.parent.max_statistics_error_value - self.parent.threshold_line = pg.InfiniteLine(pos=max_value, angle=0, label="Max threshold", movable=True) + self.parent.threshold_line = pg.InfiniteLine( + pos=max_value, angle=0, label="Max threshold", movable=True + ) self.parent.ui.statistics_plot.addItem(self.parent.threshold_line) - self.parent.threshold_line.sigPositionChanged.connect(self.parent.statistics_max_threshold_moved) + self.parent.threshold_line.sigPositionChanged.connect( + self.parent.statistics_max_threshold_moved + ) def statistics_max_threshold_moved(self): self.parent.max_statistics_error_value = self.parent.threshold_line.value() @@ -82,9 +93,13 @@ def update_list_of_files_listWidget(self): max_statistics_error_value = self.parent.max_statistics_error_value # find where the err is above the threshold - err_above_threshold = [i for i, x in enumerate(list_err) if x > max_statistics_error_value] + err_above_threshold = [ + i for i, x in enumerate(list_err) if x > max_statistics_error_value + ] - logging.info(f"index of files with errors above threshold: {err_above_threshold}") + logging.info( + f"index of files with errors above threshold: {err_above_threshold}" + ) for _row in np.arange(len(list_err)): _item = self.parent.ui.list_of_files_listWidget.item(_row) diff --git a/notebooks/__code/extract_nexus_daslogs/extract.py b/notebooks/__code/extract_nexus_daslogs/extract.py index c539916e2..7f3a61d0d 100755 --- a/notebooks/__code/extract_nexus_daslogs/extract.py +++ b/notebooks/__code/extract_nexus_daslogs/extract.py @@ -25,7 +25,9 @@ def __init__(self, working_dir=""): super(Extract, self).__init__(working_dir=working_dir) def select_nexus(self): - o_file = FileFolderBrowser(working_dir=self.working_dir, next_function=self.select_metadata) + o_file = FileFolderBrowser( + working_dir=self.working_dir, next_function=self.select_metadata + ) o_file.select_images( instruction="Select the NeXus files to extract infos from!", multiple_flag=True, @@ -54,9 +56,16 @@ def display_widgets(self): list_keys_of_first_daslogs_key = list(dict_daslogs_keys[first_daslogs_key]) first_key = list_keys_of_first_daslogs_key[0] with h5py.File(nexus_path, "r") as nxs: - value_of_first_selected_element = list(nxs["entry"]["DASlogs"][first_daslogs_key][first_key]) + value_of_first_selected_element = list( + nxs["entry"]["DASlogs"][first_daslogs_key][first_key] + ) - search_box = widgets.HBox([widgets.Label("Search:"), widgets.Text("", layout=widgets.Layout(width="30%"))]) + search_box = widgets.HBox( + [ + widgets.Label("Search:"), + widgets.Text("", layout=widgets.Layout(width="30%")), + ] + ) search_text_widget = search_box.children[1] search_text_widget.observe(self.search_text_changed, names="value") @@ -111,8 +120,12 @@ def display_widgets(self): self.y_axis_intermediate_value_widget = value_layout.children[3] self.top_keys_widgets.observe(self.top_keys_changed, names="value") - self.x_axis_intermediate_key_widget.observe(self.x_axis_intermediate_value_changed, names="value") - self.y_axis_intermediate_key_widget.observe(self.y_axis_intermediate_value_changed, names="value") + self.x_axis_intermediate_key_widget.observe( + self.x_axis_intermediate_value_changed, names="value" + ) + self.y_axis_intermediate_key_widget.observe( + self.y_axis_intermediate_value_changed, names="value" + ) self.top_keys_widget_value = self.top_keys_widgets.value @@ -121,7 +134,9 @@ def display_widgets(self): self.interpolate_label.disabled = True self.interpolate_value = interpolate_hbox.children[2] self.interpolate_value.disabled = True - self.interpolate_checkbox.observe(self.interpolate_checkbox_changed, names="value") + self.interpolate_checkbox.observe( + self.interpolate_checkbox_changed, names="value" + ) def interpolate_checkbox_changed(self, value): old_state = value["old"] @@ -141,7 +156,9 @@ def x_axis_intermediate_value_changed(self, value=None): new_intermediate_key = value["new"] first_daslogs_key = self.top_keys_widget_value with h5py.File(self.first_nexus_selected, "r") as nxs: - value_of_first_selected_element = list(nxs["entry"]["DASlogs"][first_daslogs_key][new_intermediate_key]) + value_of_first_selected_element = list( + nxs["entry"]["DASlogs"][first_daslogs_key][new_intermediate_key] + ) self.x_axis_intermediate_value_widget.options = value_of_first_selected_element def y_axis_intermediate_value_changed(self, value=None): @@ -149,7 +166,9 @@ def y_axis_intermediate_value_changed(self, value=None): # value_of_first_selected_element = None first_daslogs_key = self.top_keys_widget_value with h5py.File(self.first_nexus_selected, "r") as nxs: - value_of_first_selected_element = list(nxs["entry"]["DASlogs"][first_daslogs_key][new_intermediate_key]) + value_of_first_selected_element = list( + nxs["entry"]["DASlogs"][first_daslogs_key][new_intermediate_key] + ) self.y_axis_intermediate_value_widget.options = value_of_first_selected_element def search_text_changed(self, value): @@ -186,15 +205,34 @@ def makeup_output_file_name(self): y_axis_key = self.y_axis_key return str( - Path(output_folder) / (f"{nbr_nexus}_nexus_metadata_" + f"{top_key}_{x_axis_key}_{y_axis_key}" + ".txt") + Path(output_folder) + / ( + f"{nbr_nexus}_nexus_metadata_" + + f"{top_key}_{x_axis_key}_{y_axis_key}" + + ".txt" + ) ) - def extract(self, nexus_file_name="", top_key_widget_value=None, x_axis_key=None, y_axis_key=None): - top_key_widget_value = top_key_widget_value if top_key_widget_value else self.top_keys_widget_value - x_axis_key = x_axis_key if x_axis_key else self.x_axis_intermediate_key_widget.value - y_axis_key = y_axis_key if y_axis_key else self.y_axis_intermediate_key_widget.value + def extract( + self, + nexus_file_name="", + top_key_widget_value=None, + x_axis_key=None, + y_axis_key=None, + ): + top_key_widget_value = ( + top_key_widget_value if top_key_widget_value else self.top_keys_widget_value + ) + x_axis_key = ( + x_axis_key if x_axis_key else self.x_axis_intermediate_key_widget.value + ) + y_axis_key = ( + y_axis_key if y_axis_key else self.y_axis_intermediate_key_widget.value + ) interpolate_flag = self.interpolate_checkbox.value - interpolate_increment_value = self.interpolate_value.value if interpolate_flag else None + interpolate_increment_value = ( + self.interpolate_value.value if interpolate_flag else None + ) self.top_key_widget_value = top_key_widget_value self.x_axis_key = x_axis_key @@ -215,10 +253,14 @@ def extract(self, nexus_file_name="", top_key_widget_value=None, x_axis_key=None x_axis_entry_path = copy.deepcopy(top_entry_path) x_axis_entry_path.append(x_axis_key) - x_axis_array = get_entry_value(nexus_file_name=nexus_file_name, entry_path=x_axis_entry_path) + x_axis_array = get_entry_value( + nexus_file_name=nexus_file_name, entry_path=x_axis_entry_path + ) y_axis_entry_path = copy.deepcopy(top_entry_path) y_axis_entry_path.append(y_axis_key) - y_axis_array = get_entry_value(nexus_file_name=nexus_file_name, entry_path=y_axis_entry_path) + y_axis_array = get_entry_value( + nexus_file_name=nexus_file_name, entry_path=y_axis_entry_path + ) if interpolate_flag: if len(x_axis_array) == 1: @@ -228,11 +270,15 @@ def extract(self, nexus_file_name="", top_key_widget_value=None, x_axis_key=None if x_min != x_axis_array[0]: x_min += interpolate_increment_value - new_x_axis_array = np.arange(x_min, x_axis_array[-1], interpolate_increment_value) + new_x_axis_array = np.arange( + x_min, x_axis_array[-1], interpolate_increment_value + ) o_interpolation = Interpolation(x_axis=x_axis_array, y_axis=y_axis_array) try: - y_axis_array = o_interpolation.get_new_y_array(new_x_axis=new_x_axis_array) + y_axis_array = o_interpolation.get_new_y_array( + new_x_axis=new_x_axis_array + ) except TypeError: return None @@ -242,12 +288,15 @@ def extract(self, nexus_file_name="", top_key_widget_value=None, x_axis_key=None col4 = {"data": None, "name": "master relative time (s)"} col_legend = f"{x_axis_key}, {y_axis_key}" if use_absolute_time_offset: - starting_time = get_entry_value(nexus_file_name=nexus_file_name, entry_path=["entry", "start_time"]) + starting_time = get_entry_value( + nexus_file_name=nexus_file_name, entry_path=["entry", "start_time"] + ) starting_time = starting_time[0].decode("UTF-8") metadata.append(f"# starting time of this file: {starting_time}") master_starting_time = get_entry_value( - nexus_file_name=self.first_nexus_selected, entry_path=["entry", "start_time"] + nexus_file_name=self.first_nexus_selected, + entry_path=["entry", "start_time"], ) master_starting_time = master_starting_time[0].decode("UTF-8") metadata.append(f"# starting time of first file: {master_starting_time}") @@ -258,12 +307,19 @@ def extract(self, nexus_file_name="", top_key_widget_value=None, x_axis_key=None time_axis = y_axis_array o_absolute = AbsoluteTimeHandler(initial_absolute_time=starting_time[0]) - absolute_time_axis = o_absolute.get_absolute_time_for_this_delta_time_array(delta_time_array=time_axis) + absolute_time_axis = o_absolute.get_absolute_time_for_this_delta_time_array( + delta_time_array=time_axis + ) col3["data"] = absolute_time_axis col_legend += ", {}".format("absolute time") - o_relative = RelativeTimeHandler(master_initial_time=master_starting_time, local_initial_time=starting_time) - master_relative_time_axis = o_relative.get_relative_time_for_this_time_array(time_array=time_axis) + o_relative = RelativeTimeHandler( + master_initial_time=master_starting_time, + local_initial_time=starting_time, + ) + master_relative_time_axis = ( + o_relative.get_relative_time_for_this_time_array(time_array=time_axis) + ) col4["data"] = master_relative_time_axis col_legend += ", {}".format("master relative time (s)") @@ -290,7 +346,11 @@ def export(self): @staticmethod def create_output_file(file_name=None, dictionary=None): if len(dictionary) == 0: - display(HTML('No ASCII file has been created!')) + display( + HTML( + 'No ASCII file has been created!' + ) + ) return with open(file_name, "w") as f: diff --git a/notebooks/__code/file_folder_browser.py b/notebooks/__code/file_folder_browser.py index f1e45548d..a6cb65926 100755 --- a/notebooks/__code/file_folder_browser.py +++ b/notebooks/__code/file_folder_browser.py @@ -9,7 +9,11 @@ def __init__(self, working_dir="", next_function=None, ipts_folder=None): self.ipts_folder = ipts_folder def select_images( - self, instruction="Select Images ...", multiple_flag=True, filters={"All": "*.*"}, default_filter="All" + self, + instruction="Select Images ...", + multiple_flag=True, + filters={"All": "*.*"}, + default_filter="All", ): self.list_images_ui = myfileselector.MyFileSelectorPanel( instruction=instruction, @@ -22,7 +26,11 @@ def select_images( self.list_images_ui.show() def select_images_with_search( - self, instruction="Select Images ...", multiple_flag=True, filters={"All": "*.*"}, default_filter="All" + self, + instruction="Select Images ...", + multiple_flag=True, + filters={"All": "*.*"}, + default_filter="All", ): self.list_images_ui = fileselector.FileSelectorPanel( instruction=instruction, @@ -34,7 +42,9 @@ def select_images_with_search( ) self.list_images_ui.show() - def select_input_folder(self, instruction="Select Input Folder ...", multiple_flag=False): + def select_input_folder( + self, instruction="Select Input Folder ...", multiple_flag=False + ): self.list_input_folders_ui = myfileselector.MyFileSelectorPanel( instruction=instruction, start_dir=self.working_dir, @@ -44,7 +54,9 @@ def select_input_folder(self, instruction="Select Input Folder ...", multiple_fl ) self.list_input_folders_ui.show() - def select_output_folder(self, instruction="Select Output Folder ...", multiple_flag=False): + def select_output_folder( + self, instruction="Select Output Folder ...", multiple_flag=False + ): self.list_output_folders_ui = myfileselector.MyFileSelectorPanel( instruction=instruction, start_dir=self.working_dir, diff --git a/notebooks/__code/file_format_reader.py b/notebooks/__code/file_format_reader.py index 1261db446..d258e88a7 100755 --- a/notebooks/__code/file_format_reader.py +++ b/notebooks/__code/file_format_reader.py @@ -55,7 +55,9 @@ def build_coresponding_file_image_name(self): self.dsc_metadata = _dsc_metadata def make_tif_file_name_the_key(self): - _dict_time_stamp_vs_tiff = defaultdict(lambda: {"time_stamp": 0, "time_stamp_user_format": "N/A"}) + _dict_time_stamp_vs_tiff = defaultdict( + lambda: {"time_stamp": 0, "time_stamp_user_format": "N/A"} + ) _dsc_metadata = self.dsc_metadata for _key in _dsc_metadata.keys(): @@ -63,6 +65,8 @@ def make_tif_file_name_the_key(self): _time_stamp = _dsc_metadata[_key]["time_stamp"] _time_stamp_user_format = _dsc_metadata[_key]["time_stamp_user_format"] _dict_time_stamp_vs_tiff[_tif_name]["time_stamp"] = _time_stamp - _dict_time_stamp_vs_tiff[_tif_name]["time_stamp_user_format"] = _time_stamp_user_format + _dict_time_stamp_vs_tiff[_tif_name]["time_stamp_user_format"] = ( + _time_stamp_user_format + ) self.dict_time_stamp_vs_tiff = _dict_time_stamp_vs_tiff diff --git a/notebooks/__code/file_handler.py b/notebooks/__code/file_handler.py index b8fb345f2..8bebfac3c 100755 --- a/notebooks/__code/file_handler.py +++ b/notebooks/__code/file_handler.py @@ -165,7 +165,9 @@ def copy_files_to_folder(list_files=[], output_folder=""): shutil.copy(_file, output_folder) -def copy_and_rename_files_to_folder(list_files=[], new_list_files_names=[], output_folder=""): +def copy_and_rename_files_to_folder( + list_files=[], new_list_files_names=[], output_folder="" +): for _index_file, _original_file in enumerate(list_files): _new_file = os.path.join(output_folder, new_list_files_names[_index_file]) shutil.copy(_original_file, _new_file) @@ -204,7 +206,9 @@ def make_ascii_file(metadata=[], data=[], output_file_name="", dim="2d", sep="," f.close() -def make_ascii_file_from_2dim_array(metadata=None, col1=None, col2=None, output_file_name=None, sep=", "): +def make_ascii_file_from_2dim_array( + metadata=None, col1=None, col2=None, output_file_name=None, sep=", " +): with open(output_file_name, "w") as f: for _meta in metadata: _line = _meta + "\n" @@ -246,7 +250,9 @@ def retrieve_metadata_from_dsc_list_files(list_files=[]): metadata = {} for _index, _file in enumerate(list_files): - metadata[os.path.basename(_file)] = retrieve_metadata_from_dsc_file(filename=_file) + metadata[os.path.basename(_file)] = retrieve_metadata_from_dsc_file( + filename=_file + ) w.value = _index + 1 return metadata @@ -343,7 +349,11 @@ def _convert_epics_timestamp_to_rfc3339_timestamp(epics_timestamp): def retrieve_time_stamp(list_images, label=""): if not list_images: - return {"list_images": None, "list_time_stamp": None, "list_time_stamp_user_format": None} + return { + "list_images": None, + "list_time_stamp": None, + "list_time_stamp_user_format": None, + } [_, ext] = os.path.splitext(list_images[0]) if ext.lower() in [".tiff", ".tif"]: @@ -357,7 +367,9 @@ def retrieve_time_stamp(list_images, label=""): box = widgets.HBox( [ widgets.Label(message, layout=widgets.Layout(width="20%")), - widgets.IntProgress(min=0, max=len(list_images), value=0, layout=widgets.Layout(width="50%")), + widgets.IntProgress( + min=0, max=len(list_images), value=0, layout=widgets.Layout(width="50%") + ), ] ) progress_bar = box.children[1] @@ -425,21 +437,33 @@ def read_bragg_edge_fitting_ascii_format(full_file_name): regular = r"^#fitting peak range in file index: \[(?P\d+), (?P\d+)\]$" m = re.search(regular, line.strip()) if m: - metadata["bragg_edge_range"] = [int(m.group("left_index")), int(m.group("right_index"))] + metadata["bragg_edge_range"] = [ + int(m.group("left_index")), + int(m.group("right_index")), + ] else: metadata["bragg_edge_range"] = [None, None] line_number += 1 continue if "#distance detector-sample: " in line: - metadata["distance_detector_sample"] = line.split("#distance detector-sample: ")[1].strip() + metadata["distance_detector_sample"] = line.split( + "#distance detector-sample: " + )[1].strip() line_number += 1 continue if "#detector offset: " in line: - metadata["detector_offset"] = line.split("#detector offset: ")[1].strip() + metadata["detector_offset"] = line.split("#detector offset: ")[ + 1 + ].strip() line_number += 1 continue if "#kropff fitting procedure started: " in line: - result = True if line.split("#kropff fitting procedure started: ")[1].strip() == "True" else False + result = ( + True + if line.split("#kropff fitting procedure started: ")[1].strip() + == "True" + else False + ) metadata["kropff fitting procedure started"] = result line_number += 1 continue @@ -455,7 +479,12 @@ def read_bragg_edge_fitting_ascii_format(full_file_name): continue if "#march-dollase fitting procedure started: " in line: result = ( - True if line.split("#march-dollase fitting procedure started: ")[1].strip() == "True" else False + True + if line.split("#march-dollase fitting procedure started: ")[ + 1 + ].strip() + == "True" + else False ) metadata["march-dollase fitting procedure started"] = result line_number += 1 @@ -464,11 +493,17 @@ def read_bragg_edge_fitting_ascii_format(full_file_name): regular = r"^#Bragg peak selection range: \[(?P\d+), (?P\d+)\]$" m = re.search(regular, line.strip()) if m: - metadata["bragg_peak_selection_range"] = [int(m.group("left_index")), int(m.group("right_index"))] + metadata["bragg_peak_selection_range"] = [ + int(m.group("left_index")), + int(m.group("right_index")), + ] line_number += 1 continue if "#kropff " in line: - regular = r"^#kropff (?P\w+) selection range: \[(?P\d+), " r"(?P\d+)\]$" + regular = ( + r"^#kropff (?P\w+) selection range: \[(?P\d+), " + r"(?P\d+)\]$" + ) m = re.search(regular, line.strip()) if m: metadata["kropff_{}".format(m.group("type"))] = [ @@ -478,17 +513,23 @@ def read_bragg_edge_fitting_ascii_format(full_file_name): line_number += 1 continue if "#fitting algorithm selected: " in line: - metadata["fitting_algorithm_selected"] = line.split("#fitting algorithm selected: ")[1].strip() + metadata["fitting_algorithm_selected"] = line.split( + "#fitting algorithm selected: " + )[1].strip() line_number += 1 continue if "#march-dollase history table row " in line: - _row_and_list_flag = line.split("#march-dollase history table row ")[1].strip() + _row_and_list_flag = line.split("#march-dollase history table row ")[ + 1 + ].strip() [_row, list_flag] = _row_and_list_flag.split(":") march_history_table[_row] = list_flag line_number += 1 continue if "#march-dollase history init " in line: - _parameter_and_value = line.split("#march-dollase history init ")[1].strip() + _parameter_and_value = line.split("#march-dollase history init ")[ + 1 + ].strip() [_parameter, _value] = _parameter_and_value.split(":") march_history_init[_parameter] = _value line_number += 1 @@ -579,7 +620,9 @@ def read_bragg_edge_fitting_ascii_format(full_file_name): metadata["march-dollase history table"] = march_history_table metadata["march-dollase history init"] = march_history_init - pd_data = pd.read_csv(full_file_name, skiprows=line_number, header=0, names=col_label) + pd_data = pd.read_csv( + full_file_name, skiprows=line_number, header=0, names=col_label + ) return {"data": pd_data, "metadata": metadata} @@ -592,7 +635,9 @@ def __init__(self, working_dir=""): def get_list_of_files(self): list_of_input_files = glob.glob(os.path.join(self.working_dir, "*")) list_of_input_files.sort() - self.list_of_base_name = [os.path.basename(_file) for _file in list_of_input_files] + self.list_of_base_name = [ + os.path.basename(_file) for _file in list_of_input_files + ] def get_counter_of_extension(self): counter_extension = Counter() @@ -617,7 +662,11 @@ def get_dominant_extension(self): def check_uniqueness_of_dominant_extension(self): # check if there are several ext with the same max number - indices = [i for i, x in enumerate(self.list_of_number_of_ext) if x == self.dominant_number] + indices = [ + i + for i, x in enumerate(self.list_of_number_of_ext) + if x == self.dominant_number + ] if len(indices) > 1: # found several majority ext self.uniqueness = False else: @@ -632,21 +681,34 @@ def calculate(self): def retrieve_parameters(self): if self.uniqueness: - list_of_input_files = glob.glob(os.path.join(self.working_dir, "*" + self.dominant_extension)) + list_of_input_files = glob.glob( + os.path.join(self.working_dir, "*" + self.dominant_extension) + ) list_of_input_files.sort() - self.result = self.Result(list_files=list_of_input_files, ext=self.dominant_extension, uniqueness=True) + self.result = self.Result( + list_files=list_of_input_files, + ext=self.dominant_extension, + uniqueness=True, + ) else: list_of_maj_ext = [ - _ext for _ext in self.counter_extension.keys() if self.counter_extension[_ext] == self.dominant_number + _ext + for _ext in self.counter_extension.keys() + if self.counter_extension[_ext] == self.dominant_number ] box = widgets.HBox( [ - widgets.Label("Select Extension to work with", layout=widgets.Layout(width="20%")), + widgets.Label( + "Select Extension to work with", + layout=widgets.Layout(width="20%"), + ), widgets.Dropdown( - options=list_of_maj_ext, layout=widgets.Layout(width="20%"), value=list_of_maj_ext[0] + options=list_of_maj_ext, + layout=widgets.Layout(width="20%"), + value=list_of_maj_ext[0], ), ] ) @@ -659,7 +721,13 @@ def get_files_of_selected_ext(self): else: _ext_selected = self.dropdown_ui.value - list_of_input_files = glob.glob(os.path.join(self.working_dir, "*" + _ext_selected)) + list_of_input_files = glob.glob( + os.path.join(self.working_dir, "*" + _ext_selected) + ) list_of_input_files.sort() - return self.Result(list_files=list_of_input_files, ext=self.dominant_extension, uniqueness=True) + return self.Result( + list_files=list_of_input_files, + ext=self.dominant_extension, + uniqueness=True, + ) diff --git a/notebooks/__code/file_name_and_metadata_vs_time_stamp.py b/notebooks/__code/file_name_and_metadata_vs_time_stamp.py index 32b1f84f5..c3c892e72 100755 --- a/notebooks/__code/file_name_and_metadata_vs_time_stamp.py +++ b/notebooks/__code/file_name_and_metadata_vs_time_stamp.py @@ -45,7 +45,9 @@ def __init__(self, working_dir): def select_image_folder(self): self.folder_ui = ipywe.fileselector.FileSelectorPanel( - instruction="Select Raw Image Folder ...", start_dir=self.working_dir, type="directory" + instruction="Select Raw Image Folder ...", + start_dir=self.working_dir, + type="directory", ) self.folder_ui.show() @@ -78,7 +80,11 @@ def merging_formated_files(self): metadata_array = metadata_array.set_index("index") time_stamp_metadata_file_name_merged = pd.merge( - file_name_array, metadata_array, left_index=True, right_index=True, how="outer" + file_name_array, + metadata_array, + left_index=True, + right_index=True, + how="outer", ) if self.verbose: @@ -87,9 +93,15 @@ def merging_formated_files(self): # extract file name vs temperature time_stamp_array = np.array(time_stamp_metadata_file_name_merged.index) file_name_array = np.array( - time_stamp_metadata_file_name_merged[time_stamp_metadata_file_name_merged.columns[0]] + time_stamp_metadata_file_name_merged[ + time_stamp_metadata_file_name_merged.columns[0] + ] + ) + metadata_array = np.array( + time_stamp_metadata_file_name_merged[ + time_stamp_metadata_file_name_merged.columns[1] + ] ) - metadata_array = np.array(time_stamp_metadata_file_name_merged[time_stamp_metadata_file_name_merged.columns[1]]) # calculate equivalent metadata for each file self.file_name_vs_metadata_array = [] # 'file_name, metadata, time_stamp @@ -104,7 +116,9 @@ def merging_formated_files(self): if np.isnan(metadata_array[_index]): _metadata = self.__extract_metadata( - index=_index, metadata_array=metadata_array, time_stamp_array=time_stamp_array + index=_index, + metadata_array=metadata_array, + time_stamp_array=time_stamp_array, ) else: _metadata = metadata_array[_index] @@ -117,12 +131,18 @@ def merging_formated_files(self): if self.verbose: pprint(self.file_name_vs_metadata_array) - def __calculate_file_metadata(self, left_meta=-1, right_meta=-1, left_time=-1, right_time=-1, file_time=-1): - coeff = (float(right_meta) - float(left_meta)) / (float(right_time) - float(left_time)) + def __calculate_file_metadata( + self, left_meta=-1, right_meta=-1, left_time=-1, right_time=-1, file_time=-1 + ): + coeff = (float(right_meta) - float(left_meta)) / ( + float(right_time) - float(left_time) + ) part1 = coeff * (float(file_time) - float(left_time)) return part1 + float(left_meta) - def __get_first_metadata_and_index_value(self, index=-1, data_array=[], direction="left"): + def __get_first_metadata_and_index_value( + self, index=-1, data_array=[], direction="left" + ): if direction == "left": coeff = -1 else: @@ -146,13 +166,19 @@ def __extract_metadata(self, index=-1, metadata_array=[], time_stamp_array=[]): file_time = time_stamp_array[index] file_metadata = self.__calculate_file_metadata( - left_meta=left_meta, right_meta=right_meta, left_time=left_time, right_time=right_time, file_time=file_time + left_meta=left_meta, + right_meta=right_meta, + left_time=left_time, + right_time=right_time, + file_time=file_time, ) return file_metadata def format_metadata_infos(self, header=[]): - box = widgets.VBox([widgets.Label("Formatting metadata file .......... IN PROGRESS")]) + box = widgets.VBox( + [widgets.Label("Formatting metadata file .......... IN PROGRESS")] + ) display(box) progress_label = box.children[0] @@ -170,7 +196,10 @@ def format_metadata_infos(self, header=[]): # removing useless columns if self.my_system == "mac": time_stamp_metadata = df.drop( - df.columns[[1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18]], axis=1 + df.columns[ + [1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18] + ], + axis=1, ) else: time_stamp_metadata = df[["time_stamp", r"OT\ Temp"]] @@ -203,15 +232,21 @@ def format_metadata_infos(self, header=[]): progress_label.value = "Formatting metadata file .......... DONE!" def conv(self, x): - return time.mktime(datetime.datetime.strptime(str(x), "%Y-%m-%d %H:%M:%S").timetuple()) + return time.mktime( + datetime.datetime.strptime(str(x), "%Y-%m-%d %H:%M:%S").timetuple() + ) def format_image_infos(self): self.input_folder = self.folder_ui.selected - list_files = file_handler.retrieve_list_of_most_dominant_extension_from_folder(folder=self.input_folder) + list_files = file_handler.retrieve_list_of_most_dominant_extension_from_folder( + folder=self.input_folder + ) list_files = list_files[0] - box = widgets.VBox([widgets.Label("Retrieving Time Stamp .......... IN PROGRESS")]) + box = widgets.VBox( + [widgets.Label("Retrieving Time Stamp .......... IN PROGRESS")] + ) display(box) progress_label = box.children[0] @@ -225,14 +260,18 @@ def format_image_infos(self): progress_label.value = "Retrieving Time Stamp .......... DONE!" data = list(zip(timestamp_array, list_base_file_name, strict=False)) - self.time_stamp_vs_file_name = pd.DataFrame(data, columns=["index", "file_name"]) + self.time_stamp_vs_file_name = pd.DataFrame( + data, columns=["index", "file_name"] + ) self.timestamp_array = timestamp_array self.list_base_file_name = list_base_file_name def prepare_data_for_export(self): self.pandas_format = pd.DataFrame(self.file_name_vs_metadata_array) - self.pandas_format.rename(columns={0: "file_name", 1: "Metadata", 2: "time"}, inplace=True) + self.pandas_format.rename( + columns={0: "file_name", 1: "Metadata", 2: "time"}, inplace=True + ) def preview(self): self.prepare_data_for_export() @@ -246,7 +285,10 @@ def preview(self): def preview_metadata_vs_file_index(self): trace = go.Scatter( - x=self.file_index, y=self.metadata_array, mode="markers", name="Metadata Profile vs File Index" + x=self.file_index, + y=self.metadata_array, + mode="markers", + name="Metadata Profile vs File Index", ) layout = go.Layout( @@ -266,7 +308,10 @@ def preview_metadata_vs_relative_time(self): time_offset_axis = (self.time_array - self.time_array[0]) / 60 trace = go.Scatter( - x=time_offset_axis, y=self.metadata_array, mode="markers", name="Metadata Profile vs Relative Time(s)" + x=time_offset_axis, + y=self.metadata_array, + mode="markers", + name="Metadata Profile vs Relative Time(s)", ) layout = go.Layout( @@ -284,14 +329,18 @@ def preview_metadata_vs_relative_time(self): def select_export_folder(self): self.output_folder_ui = ipywe.fileselector.FileSelectorPanel( - instruction="Select output Folder ...", start_dir=self.working_dir, type="directory" + instruction="Select output Folder ...", + start_dir=self.working_dir, + type="directory", ) self.output_folder_ui.show() def export(self): output_folder = self.output_folder_ui.selected - [short_metatadata_file_name, _] = os.path.splitext(os.path.basename(self.sample_environment_file)) + [short_metatadata_file_name, _] = os.path.splitext( + os.path.basename(self.sample_environment_file) + ) sample_folder_name = os.path.basename(self.input_folder) file_name = f"{sample_folder_name}_vs_{short_metatadata_file_name}.txt" diff --git a/notebooks/__code/fix_images.py b/notebooks/__code/fix_images.py index 967267b87..b1adbc995 100755 --- a/notebooks/__code/fix_images.py +++ b/notebooks/__code/fix_images.py @@ -102,28 +102,41 @@ def get_statistics_of_roi_cleaned(data): nan_values = stat.nbr_nan nan_percentage = stat.percentage_nan - box1 = widgets.HBox([widgets.Label("File Name:"), widgets.Label(_file, layout=widgets.Layout(width="80%"))]) + box1 = widgets.HBox( + [ + widgets.Label("File Name:"), + widgets.Label(_file, layout=widgets.Layout(width="80%")), + ] + ) box2 = widgets.HBox( [ - widgets.Label("Total number of pixels:", layout=widgets.Layout(width="15%")), + widgets.Label( + "Total number of pixels:", layout=widgets.Layout(width="15%") + ), widgets.Label(str(number_of_pixels)), ] ) box3 = widgets.HBox( [ - widgets.Label("Negative values:", layout=widgets.Layout(width="30%")), widgets.Label( - f"{negative_values} pixels ({negative_percentage:.3}%)", layout=widgets.Layout(width="15%") + "Negative values:", layout=widgets.Layout(width="30%") + ), + widgets.Label( + f"{negative_values} pixels ({negative_percentage:.3}%)", + layout=widgets.Layout(width="15%"), ), ] ) box4 = widgets.HBox( [ - widgets.Label("Infinite values:", layout=widgets.Layout(width="30%")), widgets.Label( - f"{infinite_values} pixels ({infinite_percentage:.3}%)", layout=widgets.Layout(width="15%") + "Infinite values:", layout=widgets.Layout(width="30%") + ), + widgets.Label( + f"{infinite_values} pixels ({infinite_percentage:.3}%)", + layout=widgets.Layout(width="15%"), ), ] ) @@ -131,7 +144,10 @@ def get_statistics_of_roi_cleaned(data): box5 = widgets.HBox( [ widgets.Label("NaN values:", layout=widgets.Layout(width="30%")), - widgets.Label(f"{nan_values} pixels ({nan_percentage:.3}%)", layout=widgets.Layout(width="15%")), + widgets.Label( + f"{nan_values} pixels ({nan_percentage:.3}%)", + layout=widgets.Layout(width="15%"), + ), ] ) @@ -141,7 +157,12 @@ def get_statistics_of_roi_cleaned(data): tmp3 = widgets.interact( __give_statistics, index=widgets.IntSlider( - min=0, max=len(list_files) - 1, step=1, value=0, description="File Index", continuous_update=False + min=0, + max=len(list_files) - 1, + step=1, + value=0, + description="File Index", + continuous_update=False, ), ) @@ -160,7 +181,9 @@ def plot(self, refresh=False): _data_corrected = self.data_corrected[index] _file = self.list_files[index] - self.fig, [[self.ax0, self.ax1], [self.ax2, self.ax3]] = plt.subplots(ncols=2, nrows=2, figsize=(15, 10)) + self.fig, [[self.ax0, self.ax1], [self.ax2, self.ax3]] = plt.subplots( + ncols=2, nrows=2, figsize=(15, 10) + ) # plt.title(os.path.basename(_files[index])) cax0 = self.ax0.imshow(_raw_data, cmap="viridis", interpolation=None) @@ -169,7 +192,11 @@ def plot(self, refresh=False): histo_error = False try: - self.ax1.hist(_raw_data.ravel(), range=(np.nanmin(_raw_data), np.nanmax(_raw_data)), bins=256) + self.ax1.hist( + _raw_data.ravel(), + range=(np.nanmin(_raw_data), np.nanmax(_raw_data)), + bins=256, + ) except: histo_error = True self.ax1.set_title("Raw Histogram") @@ -180,7 +207,11 @@ def plot(self, refresh=False): tmp1 = self.fig.colorbar(cax2, ax=self.ax2) # colorbar # self.ax3.hist(_data.ravel(), range=(np.nanmin(_data), np.nanmax(_data)), bins=256) - self.ax3.hist(_data_corrected.ravel(), range=(np.nanmin(_data_corrected), np.nanmax(_data_corrected)), bins=256) + self.ax3.hist( + _data_corrected.ravel(), + range=(np.nanmin(_data_corrected), np.nanmax(_data_corrected)), + bins=256, + ) self.ax3.set_title("New Histogram") self.fig.tight_layout() @@ -192,7 +223,9 @@ def plot(self, refresh=False): if histo_error: display( - HTML('Histogram of raw images can not be displayed!') + HTML( + 'Histogram of raw images can not be displayed!' + ) ) if refresh: @@ -203,7 +236,9 @@ def neg_on_change(self, change): _new_index = change["new"]["index"] _list = change["owner"].options _new_selection = _list[_new_index] - self.dropdown_selection = self.dropdown_selection._replace(neg=_new_selection) + self.dropdown_selection = self.dropdown_selection._replace( + neg=_new_selection + ) self.counter += 1 self.plot(refresh=True) elif self.counter == 4: @@ -216,7 +251,9 @@ def inf_on_change(self, change): _new_index = change["new"]["index"] _list = change["owner"].options _new_selection = _list[_new_index] - self.dropdown_selection = self.dropdown_selection._replace(inf=_new_selection) + self.dropdown_selection = self.dropdown_selection._replace( + inf=_new_selection + ) self.counter += 1 self.plot(refresh=True) elif self.counter == 4: @@ -229,7 +266,9 @@ def nan_on_change(self, change): _new_index = change["new"]["index"] _list = change["owner"].options _new_selection = _list[_new_index] - self.dropdown_selection = self.dropdown_selection._replace(nan=_new_selection) + self.dropdown_selection = self.dropdown_selection._replace( + nan=_new_selection + ) self.counter += 1 self.plot(refresh=True) elif self.counter == 4: @@ -265,8 +304,12 @@ def fix(self): box3 = widgets.HBox( [ - widgets.Label("Replace Negative values by", layout=widgets.Layout(width="20%")), - widgets.Dropdown(options=["NaN", "0"], value=self.dropdown_selection.neg), + widgets.Label( + "Replace Negative values by", layout=widgets.Layout(width="20%") + ), + widgets.Dropdown( + options=["NaN", "0"], value=self.dropdown_selection.neg + ), ] ) @@ -275,8 +318,12 @@ def fix(self): box4 = widgets.HBox( [ - widgets.Label("Replace Infinite values by", layout=widgets.Layout(width="20%")), - widgets.Dropdown(options=["NaN", "0"], value=self.dropdown_selection.inf), + widgets.Label( + "Replace Infinite values by", layout=widgets.Layout(width="20%") + ), + widgets.Dropdown( + options=["NaN", "0"], value=self.dropdown_selection.inf + ), ] ) @@ -285,8 +332,12 @@ def fix(self): box5 = widgets.HBox( [ - widgets.Label("Replace NaN values by", layout=widgets.Layout(width="20%")), - widgets.Dropdown(options=["NaN", "0"], value=self.dropdown_selection.nan), + widgets.Label( + "Replace NaN values by", layout=widgets.Layout(width="20%") + ), + widgets.Dropdown( + options=["NaN", "0"], value=self.dropdown_selection.nan + ), ] ) @@ -328,7 +379,9 @@ def correct_data(self): self.data_corrected = _all_data_corrected def export(self, output_folder): - base_input_folder = os.path.basename(os.path.dirname(os.path.abspath(self.list_files[0]))) + base_input_folder = os.path.basename( + os.path.dirname(os.path.abspath(self.list_files[0])) + ) new_folder_name = base_input_folder + "_cleaned" output_folder = os.path.join(output_folder, new_folder_name) make_folder(output_folder) @@ -342,7 +395,11 @@ def export(self, output_folder): save_data(data=_data, filename=_full_output_file_name) display( - HTML('Files have been created in ' + output_folder + "") + HTML( + 'Files have been created in ' + + output_folder + + "" + ) ) def select_folder_and_export_images(self): diff --git a/notebooks/__code/fix_images_with_negative_pixels.py b/notebooks/__code/fix_images_with_negative_pixels.py index 3afc2c78e..05c130c8c 100755 --- a/notebooks/__code/fix_images_with_negative_pixels.py +++ b/notebooks/__code/fix_images_with_negative_pixels.py @@ -17,7 +17,9 @@ class FixImages(FileFolderBrowser): data = [] full_statistics = {} - Statistics = namedtuple("Statistics", "nbr_pixel_modified percentage_pixel_modified total_pixels") + Statistics = namedtuple( + "Statistics", "nbr_pixel_modified percentage_pixel_modified total_pixels" + ) def __init__(self, working_dir=""): super(FixImages, self).__init__(working_dir=working_dir) @@ -57,7 +59,12 @@ def _plot_images(index): _ = widgets.interact( _plot_images, index=widgets.IntSlider( - min=0, max=len(self.list_files) - 1, step=1, value=0, description="File Index", continuous_update=False + min=0, + max=len(self.list_files) - 1, + step=1, + value=0, + description="File Index", + continuous_update=False, ), ) @@ -93,7 +100,9 @@ def get_statistics_of_roi_cleaned(self, x_left, y_top, height, width, data): percentage_in_roi = (nbr_negative_in_roi / total) * 100 stat = self.Statistics( - nbr_pixel_modified=nbr_negative_in_roi, percentage_pixel_modified=percentage_in_roi, total_pixels=total + nbr_pixel_modified=nbr_negative_in_roi, + percentage_pixel_modified=percentage_in_roi, + total_pixels=total, ) return stat @@ -107,20 +116,36 @@ def display_images(self): def _plot_images(index, x_left, y_top, width, height): _file_name = _files[index] - fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(10, 5), num=os.path.basename(_file_name)) + fig, (ax0, ax1) = plt.subplots( + ncols=2, figsize=(10, 5), num=os.path.basename(_file_name) + ) _stat = _full_statistics[index] # plt.title(os.path.basename(_files[index])) cax0 = ax0.imshow(_data[index], cmap="viridis", interpolation=None) ax0.set_title("Before Correction") tmp1 = fig.colorbar(cax0, ax=ax0) # colorbar - _rectangle1 = patches.Rectangle((x_left, y_top), width, height, edgecolor="white", linewidth=2, fill=False) + _rectangle1 = patches.Rectangle( + (x_left, y_top), + width, + height, + edgecolor="white", + linewidth=2, + fill=False, + ) ax1.add_patch(_rectangle1) cax1 = ax1.imshow(_clean_data[index], cmap="viridis", interpolation=None) ax1.set_title("After Correction") tmp2 = fig.colorbar(cax1, ax=ax1) # colorbar - _rectangle2 = patches.Rectangle((x_left, y_top), width, height, edgecolor="white", linewidth=2, fill=False) + _rectangle2 = patches.Rectangle( + (x_left, y_top), + width, + height, + edgecolor="white", + linewidth=2, + fill=False, + ) ax0.add_patch(_rectangle2) fig.tight_layout() @@ -128,32 +153,63 @@ def _plot_images(index, x_left, y_top, width, height): print("STATISTICS of FULL REGION") print(f"-> Number of pixels corrected: {_stat.nbr_pixel_modified}") print(f"-> Total number of pixels: {_stat.total_pixels}") - print(f"-> Percentage of pixels corrected: {_stat.percentage_pixel_modified:.3}%") + print( + f"-> Percentage of pixels corrected: {_stat.percentage_pixel_modified:.3}%" + ) print("") - _stat_roi = self.get_statistics_of_roi_cleaned(x_left, y_top, height, width, _data[index]) + _stat_roi = self.get_statistics_of_roi_cleaned( + x_left, y_top, height, width, _data[index] + ) print("STATISTICS of SELECTED REGION") print(f"-> Number of pixels corrected: {_stat_roi.nbr_pixel_modified}") print(f"-> Total number of pixels: {_stat_roi.total_pixels}") - print(f"-> Percentage of pixels corrected: {_stat_roi.percentage_pixel_modified:.3}%") + print( + f"-> Percentage of pixels corrected: {_stat_roi.percentage_pixel_modified:.3}%" + ) tmp3 = widgets.interact( _plot_images, index=widgets.IntSlider( - min=0, max=len(self.list_files) - 1, step=1, value=0, description="File Index", continuous_update=False + min=0, + max=len(self.list_files) - 1, + step=1, + value=0, + description="File Index", + continuous_update=False, ), x_left=widgets.IntSlider( - min=0, max=width - 1, step=1, value=0, description="X Left", continuous_update=False + min=0, + max=width - 1, + step=1, + value=0, + description="X Left", + continuous_update=False, ), y_top=widgets.IntSlider( - min=0, max=height - 1, value=0, step=1, description="Y Top", continuous_update=False + min=0, + max=height - 1, + value=0, + step=1, + description="Y Top", + continuous_update=False, ), width=widgets.IntSlider( - min=0, max=width - 1, step=1, value=60, description="Width", continuous_update=False + min=0, + max=width - 1, + step=1, + value=60, + description="Width", + continuous_update=False, ), height=widgets.IntSlider( - min=0, max=height - 1, step=1, value=100, description="Height", continuous_update=False + min=0, + max=height - 1, + step=1, + value=100, + description="Height", + continuous_update=False, ), ) @@ -178,14 +234,21 @@ def _plot_images(index): tmp3 = widgets.interact( _plot_images, index=widgets.IntSlider( - min=0, max=len(self.list_files) - 1, step=1, value=0, description="File Index", continuous_update=False + min=0, + max=len(self.list_files) - 1, + step=1, + value=0, + description="File Index", + continuous_update=False, ), ) def export(self): output_folder = os.path.abspath(self.list_output_folders_ui.selected) - base_input_folder = os.path.basename(os.path.dirname(os.path.abspath(self.list_files[0]))) + base_input_folder = os.path.basename( + os.path.dirname(os.path.abspath(self.list_files[0])) + ) new_folder_name = base_input_folder + "_cleaned" output_folder = os.path.join(output_folder, new_folder_name) make_folder(output_folder) @@ -199,5 +262,9 @@ def export(self): save_data(data=_data, filename=_full_output_file_name) display( - HTML('Files have been created in ' + output_folder + "") + HTML( + 'Files have been created in ' + + output_folder + + "" + ) ) diff --git a/notebooks/__code/frederick_ipts.py b/notebooks/__code/frederick_ipts.py index 60e8030b3..1e0547ea0 100755 --- a/notebooks/__code/frederick_ipts.py +++ b/notebooks/__code/frederick_ipts.py @@ -27,7 +27,9 @@ def __init__(self, working_dir="./"): def select_file_help(self, value): import webbrowser - webbrowser.open("https://neutronimaging.pages.ornl.gov/en/tutorial/notebooks/file_selector/#select_profile") + webbrowser.open( + "https://neutronimaging.pages.ornl.gov/en/tutorial/notebooks/file_selector/#select_profile" + ) def select_files(self): help_ui = widgets.Button(description="HELP", button_style="info") @@ -35,7 +37,10 @@ def select_files(self): display(help_ui) self.files_ui = fileselector.FileSelectorPanel( - instruction="Select Images ...", start_dir=self.working_dir, next=self.load_and_sort, multiple=True + instruction="Select Images ...", + start_dir=self.working_dir, + next=self.load_and_sort, + multiple=True, ) self.files_ui.show() @@ -48,7 +53,9 @@ def load_and_sort(self, list_files): self.__calculate_all_working_images() def __sort_files_using_metadata_in_name(self): - my_re = r"\w*_(?P\w+)_(?P\w+)_\d{4}_(?P\d{4}).tiff$" + my_re = ( + r"\w*_(?P\w+)_(?P\w+)_\d{4}_(?P\d{4}).tiff$" + ) exp_dict = OrderedDict() _new_exp = { "list_of_files": [], @@ -131,7 +138,9 @@ def __load_files(self): hbox = widgets.HBox( [ - widgets.IntProgress(description="FUll Progress", layout=progress_bar_layout), + widgets.IntProgress( + description="FUll Progress", layout=progress_bar_layout + ), widgets.Label(value="", layout=widgets.Layout(width="10%")), ] ) @@ -155,7 +164,7 @@ def __load_files(self): self.exp_dict[_key] = _item w.value = _index + 1 - label.value = f"{_index+1}/{nbr_groups}" + label.value = f"{_index + 1}/{nbr_groups}" hbox.close() display(widgets.Label(value="Done!")) @@ -165,7 +174,9 @@ def __sort_files_using_time_stamp(self, dict_time_stamp): list_files = dict_time_stamp["list_files"].copy() list_time_stamp = dict_time_stamp["list_time_stamp"].copy() - list_time_stamp_user_format = dict_time_stamp["list_time_stamp_user_format"].copy() + list_time_stamp_user_format = dict_time_stamp[ + "list_time_stamp_user_format" + ].copy() list_files = np.array(list_files) time_stamp = np.array(list_time_stamp) @@ -206,6 +217,8 @@ def __calculate_all_working_images(self): else: _list_images = _group["list_of_images"] - _result_working_image = self.__calculate_group_working_image(_list_images) + _result_working_image = self.__calculate_group_working_image( + _list_images + ) _group["working_image"] = _result_working_image self.exp_dict[_group_number] = _group diff --git a/notebooks/__code/from_attenuation_to_concentration.py b/notebooks/__code/from_attenuation_to_concentration.py index 2039d2b02..a2d267c94 100755 --- a/notebooks/__code/from_attenuation_to_concentration.py +++ b/notebooks/__code/from_attenuation_to_concentration.py @@ -14,7 +14,10 @@ def __init__(self, working_dir=""): def select_folder(self): self.folder_list_widget = fileselector.FileSelectorPanel( - instruction="Select data folder", start_dir=self.working_dir, next=self.load_data, type="directory" + instruction="Select data folder", + start_dir=self.working_dir, + next=self.load_data, + type="directory", ) self.folder_list_widget.show() @@ -34,7 +37,9 @@ def define_conversion_formula(self): [ widgets.Label("A(x,y) = "), widgets.Text("0.052", layout=widgets.Layout(width="10%")), - widgets.Dropdown(options=["+", "-"], layout=widgets.Layout(width="5%"), value="+"), + widgets.Dropdown( + options=["+", "-"], layout=widgets.Layout(width="5%"), value="+" + ), widgets.Text("2.55e-5", layout=widgets.Layout(width="10%")), widgets.Label(" * H(x,Y)"), ] @@ -66,7 +71,11 @@ def converting_data(self): progress_bar.close() except: - display(HTML('Make sure the coefficient are floats!')) + display( + HTML( + 'Make sure the coefficient are floats!' + ) + ) def select_output_folder(self): self.output_folder_list_widget = fileselector.FileSelectorPanel( @@ -80,7 +89,9 @@ def select_output_folder(self): def create_concentration_list_of_file_names(self): """will use the original list of files and create the new file name that add the word "concentration" in it""" list_files = self.list_files - base_list_files = [f"concentration_{os.path.basename(_file)}" for _file in list_files] + base_list_files = [ + f"concentration_{os.path.basename(_file)}" for _file in list_files + ] return base_list_files @@ -99,5 +110,9 @@ def export_data(self, output_dir): o_norm.export(folder=new_output_dir, data_type="sample", file_type="tif") display( - HTML('Files have been created in ' + new_output_dir + "") + HTML( + 'Files have been created in ' + + new_output_dir + + "" + ) ) diff --git a/notebooks/__code/from_dsc_time_info_to_ascii_file_vs_time.py b/notebooks/__code/from_dsc_time_info_to_ascii_file_vs_time.py index bf5ae7751..cc061fbdc 100755 --- a/notebooks/__code/from_dsc_time_info_to_ascii_file_vs_time.py +++ b/notebooks/__code/from_dsc_time_info_to_ascii_file_vs_time.py @@ -20,7 +20,11 @@ def __select_input_folder(self): def output_select_dsc_folder(self, folder): self.dsc_folder = folder display( - HTML('You have selected the DSC folder: ' + folder + "") + HTML( + 'You have selected the DSC folder: ' + + folder + + "" + ) ) parent_dsc_folder = os.path.dirname(self.dsc_folder) self.working_dir = parent_dsc_folder @@ -32,7 +36,11 @@ def select_tiff_folder(self): def output_select_tiff_folder(self, folder): self.tiff_folder = folder display( - HTML('You have selected the TIFF folder: ' + folder + "") + HTML( + 'You have selected the TIFF folder: ' + + folder + + "" + ) ) def select_output_folder_and_create_ascii_file(self): @@ -47,20 +55,26 @@ def run(self): # retrive metadata from dsc file folder = self.dsc_folder list_dsc_files = glob.glob(folder + "/*.dsc") - dsc_metadata = file_handler.retrieve_metadata_from_dsc_list_files(list_files=list_dsc_files) + dsc_metadata = file_handler.retrieve_metadata_from_dsc_list_files( + list_files=list_dsc_files + ) # retrieve tiff files tiff_folder = self.tiff_folder list_tiff_files = glob.glob(tiff_folder + "/*.tif*") folder_name = os.path.basename(os.path.dirname(list_tiff_files[0])) - ascii_file_name = os.path.join(os.path.abspath(self.output_folder), folder_name + "_timestamps.txt") + ascii_file_name = os.path.join( + os.path.abspath(self.output_folder), folder_name + "_timestamps.txt" + ) list_keys = list(dsc_metadata.keys()) list_keys.sort() # create list - ascii_array = ["Filename, acquisition_time(os_format), acquisition_time(user_format), acquisition_duration(s)"] + ascii_array = [ + "Filename, acquisition_time(os_format), acquisition_time(user_format), acquisition_duration(s)" + ] for _index, _key in enumerate(list_keys): _line = "{}, {}, {}, {}".format( os.path.basename(list_tiff_files[_index]), @@ -71,4 +85,6 @@ def run(self): ascii_array.append(_line) # export file - file_handler.make_ascii_file(data=ascii_array, output_file_name=ascii_file_name, dim="1d") + file_handler.make_ascii_file( + data=ascii_array, output_file_name=ascii_file_name, dim="1d" + ) diff --git a/notebooks/__code/from_json_to_csv/main.py b/notebooks/__code/from_json_to_csv/main.py index 48231b65d..7ee77ece0 100755 --- a/notebooks/__code/from_json_to_csv/main.py +++ b/notebooks/__code/from_json_to_csv/main.py @@ -44,7 +44,9 @@ def load(self, list_json): def __calculate_image_dimension(self): _image_0 = self.data[0] - [self.image_dimension["height"], self.image_dimension["width"]] = np.shape(_image_0) + [self.image_dimension["height"], self.image_dimension["width"]] = np.shape( + _image_0 + ) def __bin_parameter_changed(self, sender): new_width_bin = int(self.bin_width_para.value) @@ -67,9 +69,16 @@ def select_bin_parameter(self): _height = self.image_dimension["height"] left_widgets = widgets.VBox( [ - widgets.HTML(value="Current Image Size:", layout=widgets.Layout(width="250px")), - widgets.Label(f"Width: {_width} pixels", layout=widgets.Layout(width="100%")), - widgets.Label(f"Height: {_height} pixels", layout=widgets.Layout(width="100%")), + widgets.HTML( + value="Current Image Size:", + layout=widgets.Layout(width="250px"), + ), + widgets.Label( + f"Width: {_width} pixels", layout=widgets.Layout(width="100%") + ), + widgets.Label( + f"Height: {_height} pixels", layout=widgets.Layout(width="100%") + ), ] ) @@ -94,7 +103,9 @@ def select_bin_parameter(self): center_widgets = widgets.VBox( [ - widgets.HTML("Bin Parameter:", layout=widgets.Layout(width="250px")), + widgets.HTML( + "Bin Parameter:", layout=widgets.Layout(width="250px") + ), self.bin_width_para, self.bin_height_para, ] @@ -102,9 +113,15 @@ def select_bin_parameter(self): self.right_widgets = widgets.VBox( [ - widgets.HTML("New Image Size:", layout=widgets.Layout(width="250px")), - widgets.Label(f"Width: {250} pixels", layout=widgets.Layout(width="100%")), - widgets.Label(f"Height: {250} pixels", layout=widgets.Layout(width="100%")), + widgets.HTML( + "New Image Size:", layout=widgets.Layout(width="250px") + ), + widgets.Label( + f"Width: {250} pixels", layout=widgets.Layout(width="100%") + ), + widgets.Label( + f"Height: {250} pixels", layout=widgets.Layout(width="100%") + ), ] ) @@ -147,7 +164,9 @@ def rebin_data(self, data=[]): new_width = int(new_width) _new_data = data[0:new_height, 0:new_width] - _new_data = _new_data.reshape(_nbr_height_bin, height_bin, _nbr_width_bin, width_bin) + _new_data = _new_data.reshape( + _nbr_height_bin, height_bin, _nbr_width_bin, width_bin + ) data_rebinned = _new_data.mean(axis=3).mean(axis=1) return data_rebinned @@ -163,7 +182,9 @@ def export(self, output_folder): # output_folder = os.path.abspath(os.path.join(self.output_folder_ui.selected, # "{}_rebin_by_{}".format(input_folder, self.bin_value))) bin_string = f"{self.bin_height_value}height_{self.bin_width_value}width" - output_folder = os.path.abspath(os.path.join(output_folder, f"{input_folder}_rebin_by_{bin_string}")) + output_folder = os.path.abspath( + os.path.join(output_folder, f"{input_folder}_rebin_by_{bin_string}") + ) utilities.make_dir(dir=output_folder, overwrite=False) w = widgets.IntProgress() @@ -179,4 +200,10 @@ def export(self, output_folder): w.value = _index + 1 - display(HTML('File created in ' + output_folder + "")) + display( + HTML( + 'File created in ' + + output_folder + + "" + ) + ) diff --git a/notebooks/__code/gamma_filtering_tool.py b/notebooks/__code/gamma_filtering_tool.py index 773f81df6..55a93ba2f 100755 --- a/notebooks/__code/gamma_filtering_tool.py +++ b/notebooks/__code/gamma_filtering_tool.py @@ -95,7 +95,9 @@ def init_statusbar(self): hori_layout.addWidget(self.filtered_value) # spacer - spacerItem = QtGui.QSpacerItem(22520, 40, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) + spacerItem = QtGui.QSpacerItem( + 22520, 40, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding + ) hori_layout.addItem(spacerItem) # add status bar in main ui @@ -118,7 +120,9 @@ def __get_filtering_coefficient_value(self): try: float_value = float(value) except: - self.ui.filtering_coefficient_value.setText(str(self.default_filtering_coefficient_value)) + self.ui.filtering_coefficient_value.setText( + str(self.default_filtering_coefficient_value) + ) return self.default_filtering_coefficient_value if float_value < 0: @@ -150,7 +154,9 @@ def fill_table(self): self.ui.tableWidget.setItem(_row, 1, _item) # percentage of pixel corrected - _item = QtGui.QTableWidgetItem(f"{nbr_pixel_corrected*100/total_nbr_pixels:.02f}%") + _item = QtGui.QTableWidgetItem( + f"{nbr_pixel_corrected * 100 / total_nbr_pixels:.02f}%" + ) self.ui.tableWidget.setItem(_row, 2, _item) def get_number_pixel_gamma_corrected(self, data=[]): @@ -178,7 +184,9 @@ def mouse_moved_in_any_image(self, evt, image="raw"): mouse_x = int(mouse_point.x()) mouse_y = int(mouse_point.y()) - if (mouse_x >= 0 and mouse_x < width) and (mouse_y >= 0 and mouse_y < height): + if (mouse_x >= 0 and mouse_x < width) and ( + mouse_y >= 0 and mouse_y < height + ): self.x_value.setText(str(mouse_x)) self.y_value.setText(str(mouse_y)) @@ -237,7 +245,9 @@ def init_pyqtgraph(self): self.raw_vLine.setPos([1000, 1000]) self.raw_hLine.setPos([1000, 1000]) self.raw_proxy = pg.SignalProxy( - self.ui.raw_image_view.view.scene().sigMouseMoved, rateLimit=60, slot=self.mouse_moved_in_raw_image + self.ui.raw_image_view.view.scene().sigMouseMoved, + rateLimit=60, + slot=self.mouse_moved_in_raw_image, ) d1.addWidget(self.ui.raw_image_view) @@ -246,7 +256,9 @@ def init_pyqtgraph(self): d1h.addWidget(self.ui.raw_histogram_plot) # filtered image - self.ui.filtered_image_view = pg.ImageView(view=pg.PlotItem(), name="filtered_image") + self.ui.filtered_image_view = pg.ImageView( + view=pg.PlotItem(), name="filtered_image" + ) self.ui.filtered_image_view.ui.roiBtn.hide() self.ui.filtered_image_view.ui.menuBtn.hide() self.filtered_vLine = pg.InfiniteLine(angle=90, movable=False) @@ -325,14 +337,18 @@ def display_raw_image(self, file_index): self.raw_image_size = np.shape(_image) if not first_update: - _histo_widget.setLevels(self.raw_histogram_level[0], self.raw_histogram_level[1]) + _histo_widget.setLevels( + self.raw_histogram_level[0], self.raw_histogram_level[1] + ) # histogram self.ui.raw_histogram_plot.clear() min = 0 max = np.max(_image) y, x = np.histogram(_image, bins=np.linspace(min, max + 1, self.nbr_histo_bins)) - self.ui.raw_histogram_plot.plot(x, y, stepMode=True, fillLevel=0, brush=(0, 0, 255, 150)) + self.ui.raw_histogram_plot.plot( + x, y, stepMode=True, fillLevel=0, brush=(0, 0, 255, 150) + ) def reset_states(self): _state = self.state_of_raw @@ -375,14 +391,18 @@ def display_corrected_image(self, file_index=0): self.live_filtered_image = _image if not first_update: - _histo_widget.setLevels(self.filtered_histogram_level[0], self.filtered_histogram_level[1]) + _histo_widget.setLevels( + self.filtered_histogram_level[0], self.filtered_histogram_level[1] + ) # histogram self.ui.filtered_histogram_plot.clear() min = 0 max = np.max(_image) y, x = np.histogram(_image, bins=np.linspace(min, max + 1, self.nbr_histo_bins)) - self.ui.filtered_histogram_plot.plot(x, y, stepMode=True, fillLevel=0, brush=(0, 0, 255, 150)) + self.ui.filtered_histogram_plot.plot( + x, y, stepMode=True, fillLevel=0, brush=(0, 0, 255, 150) + ) def apply_clicked(self): self.close() diff --git a/notebooks/__code/group_images_by_cycle_for_grating_experiment/__init__.py b/notebooks/__code/group_images_by_cycle_for_grating_experiment/__init__.py index d440c2f80..8accf5c77 100755 --- a/notebooks/__code/group_images_by_cycle_for_grating_experiment/__init__.py +++ b/notebooks/__code/group_images_by_cycle_for_grating_experiment/__init__.py @@ -1,4 +1,11 @@ -list_fit_procedure = ["EM Fit", "Matrix Fit", "MATRIX", "FFT Fit", "Sinus Fit", "Matrix Fit Advanced"] +list_fit_procedure = [ + "EM Fit", + "Matrix Fit", + "MATRIX", + "FFT Fit", + "Sinus Fit", + "Matrix Fit Advanced", +] class IndexOfColumns: diff --git a/notebooks/__code/group_images_by_cycle_for_grating_experiment/combine_and_move_files.py b/notebooks/__code/group_images_by_cycle_for_grating_experiment/combine_and_move_files.py index 47efeada4..ad68a50d8 100755 --- a/notebooks/__code/group_images_by_cycle_for_grating_experiment/combine_and_move_files.py +++ b/notebooks/__code/group_images_by_cycle_for_grating_experiment/combine_and_move_files.py @@ -23,7 +23,10 @@ def run(self): debug = self.debug output_folder = self.output_folder - output_folder_basename = os.path.basename(self.parent.folder_selected) + "_sorted_for_grating_reconstruction" + output_folder_basename = ( + os.path.basename(self.parent.folder_selected) + + "_sorted_for_grating_reconstruction" + ) output_folder = os.path.join(output_folder, output_folder_basename) output_folder = os.path.abspath(output_folder) @@ -34,7 +37,9 @@ def run(self): self.parent.output_folder = output_folder make_or_reset_folder(output_folder) - dictionary_of_groups_old_names = self.parent.make_dictionary_of_groups_old_names() + dictionary_of_groups_old_names = ( + self.parent.make_dictionary_of_groups_old_names() + ) self.parent.dictionary_of_groups_old_names = dictionary_of_groups_old_names dict_old_files = dictionary_of_groups_old_names @@ -47,14 +52,24 @@ def run(self): hbox1 = widgets.HBox( [ widgets.HTML("Groups", layout=widgets.Layout(width="100px")), - widgets.IntProgress(min=0, max=size_outer_loop - 1, value=0, layout=widgets.Layout(width="300px")), + widgets.IntProgress( + min=0, + max=size_outer_loop - 1, + value=0, + layout=widgets.Layout(width="300px"), + ), ] ) outer_progress_ui = hbox1.children[1] hbox2 = widgets.HBox( [ widgets.HTML("Files", layout=widgets.Layout(width="100px")), - widgets.IntProgress(min=0, max=size_inner_loop - 1, value=0, layout=widgets.Layout(width="300px")), + widgets.IntProgress( + min=0, + max=size_inner_loop - 1, + value=0, + layout=widgets.Layout(width="300px"), + ), ] ) inner_progress_ui = hbox2.children[1] @@ -69,14 +84,20 @@ def run(self): _new_name_list = dict_new_files[_key] inner_progress_ui.value = 0 outer_progress_ui.value = _outer_index - for _inner_index, (_old, _new) in enumerate(zip(_old_name_list, _new_name_list, strict=False)): + for _inner_index, (_old, _new) in enumerate( + zip(_old_name_list, _new_name_list, strict=False) + ): new_full_file_name = os.path.join(output_folder, _new) if debug: print(f"old full file name -> {_old}") if debug: print(f"new full file name -> {_new}") if len(_old) > 1: - combine_images(output_folder=output_folder, list_images=_old, new_file_name=_new) + combine_images( + output_folder=output_folder, + list_images=_old, + new_file_name=_new, + ) else: shutil.copy(_old[0], new_full_file_name) inner_progress_ui.value = _inner_index diff --git a/notebooks/__code/group_images_by_cycle_for_grating_experiment/event_handler.py b/notebooks/__code/group_images_by_cycle_for_grating_experiment/event_handler.py index 5f8223710..be209d8d9 100755 --- a/notebooks/__code/group_images_by_cycle_for_grating_experiment/event_handler.py +++ b/notebooks/__code/group_images_by_cycle_for_grating_experiment/event_handler.py @@ -1,7 +1,9 @@ import os from __code.file_folder_browser import FileFolderBrowser -from __code.group_images_by_cycle_for_grating_experiment.excel_handler import ExcelHandler +from __code.group_images_by_cycle_for_grating_experiment.excel_handler import ( + ExcelHandler, +) from __code.group_images_by_cycle_for_grating_experiment.get import Get @@ -66,7 +68,9 @@ def group_index_changed(self, value): o_get = Get(parent=self.parent) short_list_files = o_get.list_of_files_basename_only(int(new_group_index)) self.parent.list_of_files_ui.options = short_list_files - short_list_new_files = o_get.list_of_new_files_basename_only(int(new_group_index)) + short_list_new_files = o_get.list_of_new_files_basename_only( + int(new_group_index) + ) self.parent.list_of_new_files_ui.options = short_list_new_files self.parent.list_of_new_files_ui.value = short_list_new_files[0] self.parent.list_of_files_ui.value = short_list_files[0] @@ -83,7 +87,9 @@ def list_of_files_changed(self, value): full_file_name_selected = os.path.join(self.parent.data_path, file_selected) string_to_display = "" - for _key, _value in dictionary_file_vs_metadata[full_file_name_selected].items(): + for _key, _value in dictionary_file_vs_metadata[ + full_file_name_selected + ].items(): string_to_display += f"{_key}: {_value}\n" self.parent.metadata_ui.value = string_to_display @@ -98,9 +104,14 @@ def list_of_new_files_changed(self, value): def use_excel_file_clicked(self, state): # self.parent.output_folder = "/Volumes/G-DRIVE/IPTS/IPTS-28730-gratting-CT" #REMOVE_ME - o_file = FileFolderBrowser(working_dir=self.parent.output_folder, next_function=self.load_excel_file) + o_file = FileFolderBrowser( + working_dir=self.parent.output_folder, next_function=self.load_excel_file + ) o_file.select_images( - instruction="Select Excel file ...", multiple_flag=False, filters={"excel": "*.xls"}, default_filter="excel" + instruction="Select Excel file ...", + multiple_flag=False, + filters={"excel": "*.xls"}, + default_filter="excel", ) def load_excel_file(self, file_name): diff --git a/notebooks/__code/group_images_by_cycle_for_grating_experiment/excel_handler.py b/notebooks/__code/group_images_by_cycle_for_grating_experiment/excel_handler.py index 392498781..e235945fa 100755 --- a/notebooks/__code/group_images_by_cycle_for_grating_experiment/excel_handler.py +++ b/notebooks/__code/group_images_by_cycle_for_grating_experiment/excel_handler.py @@ -14,8 +14,12 @@ from __code._utilities.status_message import StatusMessageStatus, show_status_message from __code._utilities.string import format_html_message from __code.group_images_by_cycle_for_grating_experiment import IndexOfColumns -from __code.group_images_by_cycle_for_grating_experiment.excel_table_handler import ExcelTableHandler as TableHandler -from __code.group_images_by_cycle_for_grating_experiment.repeat_widget_change_dialog import RepeatWidgetChangeDialog +from __code.group_images_by_cycle_for_grating_experiment.excel_table_handler import ( + ExcelTableHandler as TableHandler, +) +from __code.group_images_by_cycle_for_grating_experiment.repeat_widget_change_dialog import ( + RepeatWidgetChangeDialog, +) ROW_HEIGHT = 40 @@ -39,22 +43,33 @@ def load_excel(self, excel_file=None): df = pd.read_excel(excel_file, sheet_name="Tabelle1", header=0) nbr_excel_row = len(df) - nbr_notebook_row = len(self.parent.first_last_run_of_each_group_dictionary.keys()) - data_type_to_populate_with_notebook_data = self.parent.sample_or_ob_radio_buttons.value + nbr_notebook_row = len( + self.parent.first_last_run_of_each_group_dictionary.keys() + ) + data_type_to_populate_with_notebook_data = ( + self.parent.sample_or_ob_radio_buttons.value + ) # if we want to populate the sample column, we need to have the same number of sample and ob - if (nbr_excel_row != nbr_notebook_row) and (data_type_to_populate_with_notebook_data == "sample"): + if (nbr_excel_row != nbr_notebook_row) and ( + data_type_to_populate_with_notebook_data == "sample" + ): display( HTML( "Number of rows in Excel document selected and number of group DO NOT " "MATCH!" ) ) - display(HTML("SOLUTION: create a new Excel document!")) + display( + HTML( + "SOLUTION: create a new Excel document!" + ) + ) else: new_df = self._populate_pandas_object( - df=df, data_type_to_populate_with_notebook_data=data_type_to_populate_with_notebook_data + df=df, + data_type_to_populate_with_notebook_data=data_type_to_populate_with_notebook_data, ) o_interface = Interface( @@ -71,10 +86,14 @@ def get_excel_config(self): with open(config_file) as json_file: return json.load(json_file) - def _populate_pandas_object(self, df=None, data_type_to_populate_with_notebook_data="sample"): + def _populate_pandas_object( + self, df=None, data_type_to_populate_with_notebook_data="sample" + ): logging.info("Entering _populate_pandas_object!") - def get_matching_ob_group_index(sample_outer_value=None, dict_group_outer_value=None): + def get_matching_ob_group_index( + sample_outer_value=None, dict_group_outer_value=None + ): """ Using the sample_outer_value as a reference, this method will go over all the keys in ob_outer_value and look at the corresponding value. @@ -92,14 +111,19 @@ def get_matching_ob_group_index(sample_outer_value=None, dict_group_outer_value= return list_keys[-1] output_folder = os.path.abspath(self.parent.output_folder) - first_last_run_of_each_group_dictionary = self.parent.first_last_run_of_each_group_dictionary + first_last_run_of_each_group_dictionary = ( + self.parent.first_last_run_of_each_group_dictionary + ) if data_type_to_populate_with_notebook_data == "sample": logging.info("working with Sample") - for _row_index, _key in enumerate(first_last_run_of_each_group_dictionary.keys()): + for _row_index, _key in enumerate( + first_last_run_of_each_group_dictionary.keys() + ): df.iloc[_row_index, 0] = os.path.join( - output_folder, first_last_run_of_each_group_dictionary[_key]["first"] + output_folder, + first_last_run_of_each_group_dictionary[_key]["first"], ) df.iloc[_row_index, 1] = os.path.join( output_folder, first_last_run_of_each_group_dictionary[_key]["last"] @@ -118,15 +142,18 @@ def get_matching_ob_group_index(sample_outer_value=None, dict_group_outer_value= sample_outer_value = df.iloc[_row, IndexOfColumns.sample_information] logging.info(f"-> sample_outer_value: {sample_outer_value}") ob_group_index = get_matching_ob_group_index( - sample_outer_value=sample_outer_value, dict_group_outer_value=dict_group_outer_value + sample_outer_value=sample_outer_value, + dict_group_outer_value=dict_group_outer_value, ) # logging.info(f"-> ob_group_index: {ob_group_index}") df.iloc[_row, 2] = os.path.join( - output_folder, first_last_run_of_each_group_dictionary[ob_group_index]["first"] + output_folder, + first_last_run_of_each_group_dictionary[ob_group_index]["first"], ) df.iloc[_row, 3] = os.path.join( - output_folder, first_last_run_of_each_group_dictionary[ob_group_index]["last"] + output_folder, + first_last_run_of_each_group_dictionary[ob_group_index]["last"], ) return df @@ -148,27 +175,43 @@ def formatting_angle_value(str_angle_value=""): return f"{int(str_angle_value):03d}" output_folder = os.path.abspath(self.parent.output_folder) - first_last_run_of_each_group_dictionary = self.parent.first_last_run_of_each_group_dictionary + first_last_run_of_each_group_dictionary = ( + self.parent.first_last_run_of_each_group_dictionary + ) dict_group_outer_value = self.parent.dict_group_outer_value excel_config = self.get_excel_config() df_dict = {} if data_type_to_populate_with_notebook_data == "sample": - for _row_index, _key in enumerate(first_last_run_of_each_group_dictionary.keys()): + for _row_index, _key in enumerate( + first_last_run_of_each_group_dictionary.keys() + ): if _row_index == 0: df_dict["first_data_file"] = [ - os.path.join(output_folder, first_last_run_of_each_group_dictionary[_key]["first"]) + os.path.join( + output_folder, + first_last_run_of_each_group_dictionary[_key]["first"], + ) ] df_dict["last_data_file"] = [ - os.path.join(output_folder, first_last_run_of_each_group_dictionary[_key]["last"]) + os.path.join( + output_folder, + first_last_run_of_each_group_dictionary[_key]["last"], + ) ] df_dict["sample_information"] = [dict_group_outer_value[_key]] else: df_dict["first_data_file"].append( - os.path.join(output_folder, first_last_run_of_each_group_dictionary[_key]["first"]) + os.path.join( + output_folder, + first_last_run_of_each_group_dictionary[_key]["first"], + ) ) df_dict["last_data_file"].append( - os.path.join(output_folder, first_last_run_of_each_group_dictionary[_key]["last"]) + os.path.join( + output_folder, + first_last_run_of_each_group_dictionary[_key]["last"], + ) ) df_dict["sample_information"].append(dict_group_outer_value[_key]) @@ -177,31 +220,47 @@ def formatting_angle_value(str_angle_value=""): df_dict["last_ob_file"] = ["None" for _ in np.arange(nbr_row)] else: # ob - for _row_index, _key in enumerate(first_last_run_of_each_group_dictionary.keys()): + for _row_index, _key in enumerate( + first_last_run_of_each_group_dictionary.keys() + ): nbr_row = len(first_last_run_of_each_group_dictionary.keys()) df_dict["first_sample_file"] = ["None" for _ in np.arange(nbr_row)] df_dict["last_sample_file"] = ["None" for _ in np.arange(nbr_row)] if _row_index == 0: df_dict["first_ob_file"] = [ - os.path.join(output_folder, first_last_run_of_each_group_dictionary[_key]["first"]) + os.path.join( + output_folder, + first_last_run_of_each_group_dictionary[_key]["first"], + ) ] df_dict["last_ob_file"] = [ - os.path.join(output_folder, first_last_run_of_each_group_dictionary[_key]["last"]) + os.path.join( + output_folder, + first_last_run_of_each_group_dictionary[_key]["last"], + ) ] else: df_dict["first_ob_file"].append( - os.path.join(output_folder, first_last_run_of_each_group_dictionary[_key]["first"]) + os.path.join( + output_folder, + first_last_run_of_each_group_dictionary[_key]["first"], + ) ) df_dict["last_ob_file"].append( - os.path.join(output_folder, first_last_run_of_each_group_dictionary[_key]["last"]) + os.path.join( + output_folder, + first_last_run_of_each_group_dictionary[_key]["last"], + ) ) # default file_id dict_group_outer_value = self.parent.dict_group_outer_value for _row_index, _key in enumerate(dict_group_outer_value): - angle_value_formatted = formatting_angle_value(str_angle_value=dict_group_outer_value[_key]) + angle_value_formatted = formatting_angle_value( + str_angle_value=dict_group_outer_value[_key] + ) _file_id = f"sample_{angle_value_formatted}" if _row_index == 0: df_dict["file_id"] = [_file_id] @@ -231,7 +290,9 @@ def formatting_angle_value(str_angle_value=""): def new_excel(self): self.parent.excel_info_widget.value = "Working with new excel file!" - data_type_to_populate_with_notebook_data = self.parent.sample_or_ob_radio_buttons.value + data_type_to_populate_with_notebook_data = ( + self.parent.sample_or_ob_radio_buttons.value + ) pandas_object = self._create_pandas_object( data_type_to_populate_with_notebook_data=data_type_to_populate_with_notebook_data @@ -267,7 +328,9 @@ def __init__( ) ) self.grand_parent = grand_parent - self.data_type_to_populate_with_notebook_data = data_type_to_populate_with_notebook_data + self.data_type_to_populate_with_notebook_data = ( + data_type_to_populate_with_notebook_data + ) self.output_folder = self.grand_parent.output_folder self.pandas_object = pandas_object @@ -295,11 +358,15 @@ def widget_state_changed(self, new_value, row=0, column=0): self.repeat_widget_changed_or_not_dialog(row_changed=row, column_changed=column) def repeat_widget_changed_or_not_dialog(self, row_changed=0, column_changed=0): - o_dialog = RepeatWidgetChangeDialog(parent=self, input_row=row_changed, input_column=column_changed) + o_dialog = RepeatWidgetChangeDialog( + parent=self, input_row=row_changed, input_column=column_changed + ) o_dialog.show() @staticmethod - def add_output_folder_to_dictionary(first_last_run_of_each_group_dictionary=None, output_folder=None): + def add_output_folder_to_dictionary( + first_last_run_of_each_group_dictionary=None, output_folder=None + ): for _key in first_last_run_of_each_group_dictionary.keys(): first_last_run_of_each_group_dictionary[_key]["first"] = os.path.join( output_folder, first_last_run_of_each_group_dictionary[_key]["first"] @@ -374,7 +441,9 @@ def set_color_for_that_path_that_must_exist(string, row, column): def is_roi_correct_format(roi): """check if roi has the format [##,##,##,##] where ## are integers""" roi = roi.strip() - result = re.search(r"\[\s*(\d*)\s*,\s*(\d*)\s*,\s*(\d*)\s*,\s*(\d*)\s*\]", roi) + result = re.search( + r"\[\s*(\d*)\s*,\s*(\d*)\s*,\s*(\d*)\s*,\s*(\d*)\s*\]", roi + ) try: if len(result.groups()) != 4: return False @@ -461,7 +530,8 @@ def set_color_for_roi(roi, row, column): if self.at_least_one_error_found: show_status_message( parent=self, - message="At least one issue found in table! Angel will not be able to execute this " "excel!", + message="At least one issue found in table! Angel will not be able to execute this " + "excel!", status=StatusMessageStatus.warning, duration_s=15, ) @@ -502,7 +572,10 @@ def init_statusbar_message(self): ) else: show_status_message( - parent=self, message="Created a new Excel file!", status=StatusMessageStatus.ready, duration_s=10 + parent=self, + message="Created a new Excel file!", + status=StatusMessageStatus.ready, + duration_s=10, ) def fill_table(self): @@ -535,22 +608,34 @@ def fill_table(self): o_table.set_last_ob_file(pandas_entry_for_this_row[3]) o_table.set_first_dc_file(pandas_entry_for_this_row[4]) o_table.set_last_dc_file(pandas_entry_for_this_row[5]) - o_table.set_period(pandas_entry_for_this_row[6], method=self.widget_state_changed) - o_table.set_images_per_step(pandas_entry_for_this_row[7], method=self.widget_state_changed) + o_table.set_period( + pandas_entry_for_this_row[6], method=self.widget_state_changed + ) + o_table.set_images_per_step( + pandas_entry_for_this_row[7], method=self.widget_state_changed + ) o_table.set_rotation(pandas_entry_for_this_row[8]) - o_table.set_fit_procedure(pandas_entry_for_this_row[9], method=self.widget_state_changed) + o_table.set_fit_procedure( + pandas_entry_for_this_row[9], method=self.widget_state_changed + ) o_table.set_roi(pandas_entry_for_this_row[10]) - o_table.set_gamma_filter_data_ob(pandas_entry_for_this_row[11], method=self.widget_state_changed) + o_table.set_gamma_filter_data_ob( + pandas_entry_for_this_row[11], method=self.widget_state_changed + ) o_table.set_data_threshold_3x3(pandas_entry_for_this_row[12]) o_table.set_data_threshold_5x5(pandas_entry_for_this_row[13]) o_table.set_data_threshold_7x7(pandas_entry_for_this_row[14]) o_table.set_data_sigma_log(pandas_entry_for_this_row[15]) - o_table.set_gamma_filter_dc(pandas_entry_for_this_row[16], method=self.widget_state_changed) + o_table.set_gamma_filter_dc( + pandas_entry_for_this_row[16], method=self.widget_state_changed + ) o_table.set_dc_threshold_3x3(pandas_entry_for_this_row[17]) o_table.set_dc_threshold_5x5(pandas_entry_for_this_row[18]) o_table.set_dc_threshold_7x7(pandas_entry_for_this_row[19]) o_table.set_dc_log(pandas_entry_for_this_row[20]) - o_table.set_dc_outlier_removal(pandas_entry_for_this_row[21], method=self.widget_state_changed) + o_table.set_dc_outlier_removal( + pandas_entry_for_this_row[21], method=self.widget_state_changed + ) o_table.set_dc_outlier_value(pandas_entry_for_this_row[22]) o_table.set_result_directory(pandas_entry_for_this_row[23]) o_table.set_file_id(pandas_entry_for_this_row[24]) @@ -572,7 +657,9 @@ def save_as_button_pushed(self): working_dir = self.grand_parent.working_dir folder_selected = self.grand_parent.folder_selected base_folder_name = os.path.basename(folder_selected) - default_file_name = os.path.join(working_dir, base_folder_name + "_angel_excel.xls") + default_file_name = os.path.join( + working_dir, base_folder_name + "_angel_excel.xls" + ) file_and_extension_name = QFileDialog.getSaveFileName( self, "Select or define file name", default_file_name, "Excel (*.xls)" ) @@ -732,7 +819,9 @@ def right_click_table_widget(self, position): if show_copy_content_to_rest_of_column: menu.addSeparator() - copy_content_to_rest_of_column = menu.addAction("Sync column with this value") + copy_content_to_rest_of_column = menu.addAction( + "Sync column with this value" + ) else: copy_content_to_rest_of_column = None @@ -764,36 +853,56 @@ def right_click_table_widget(self, position): ) elif action == copy_content_to_rest_of_column: - self.copy_content_to_rest_of_column(current_row=row_selected, current_column=column_selected) + self.copy_content_to_rest_of_column( + current_row=row_selected, current_column=column_selected + ) self.check_table_content_pushed() def copy_content_to_rest_of_column(self, current_row=0, current_column=0): o_table = TableHandler(table_ui=self.ui.tableWidget) - value_to_copy = o_table.get_item_str_from_cell(row=current_row, column=current_column) + value_to_copy = o_table.get_item_str_from_cell( + row=current_row, column=current_column + ) nbr_row = o_table.row_count() for _row in np.arange(nbr_row): - o_table.set_item_with_str(row=_row, column=current_column, cell_str=value_to_copy) + o_table.set_item_with_str( + row=_row, column=current_column, cell_str=value_to_copy + ) - def browse(self, show_browse_for_folder=False, show_browse_for_file=True, row_selected=[0], column_selected=0): + def browse( + self, + show_browse_for_folder=False, + show_browse_for_file=True, + row_selected=[0], + column_selected=0, + ): folder_selected = self.grand_parent.folder_selected if show_browse_for_file: - file_and_extension_name = QFileDialog.getOpenFileName(self, "Select file ...", folder_selected) + file_and_extension_name = QFileDialog.getOpenFileName( + self, "Select file ...", folder_selected + ) file_selected = file_and_extension_name[0] if file_selected: o_table = TableHandler(table_ui=self.ui.tableWidget) for _row in row_selected: - o_table.set_item_with_str(row=_row, column=column_selected, cell_str=file_selected) + o_table.set_item_with_str( + row=_row, column=column_selected, cell_str=file_selected + ) elif show_browse_for_folder: folder_name = os.path.dirname(folder_selected) - folder = QFileDialog.getExistingDirectory(self, "Select output folder ...", folder_name) + folder = QFileDialog.getExistingDirectory( + self, "Select output folder ...", folder_name + ) if folder: o_table = TableHandler(table_ui=self.ui.tableWidget) for _row in row_selected: - o_table.set_item_with_str(row=_row, column=column_selected, cell_str=folder) + o_table.set_item_with_str( + row=_row, column=column_selected, cell_str=folder + ) self.check_table_content_pushed() diff --git a/notebooks/__code/group_images_by_cycle_for_grating_experiment/excel_table_handler.py b/notebooks/__code/group_images_by_cycle_for_grating_experiment/excel_table_handler.py index 8da862206..a07dafe9f 100755 --- a/notebooks/__code/group_images_by_cycle_for_grating_experiment/excel_table_handler.py +++ b/notebooks/__code/group_images_by_cycle_for_grating_experiment/excel_table_handler.py @@ -3,7 +3,10 @@ from qtpy.QtWidgets import QComboBox, QSpinBox, QTableWidgetItem from __code._utilities.table_handler import TableHandler -from __code.group_images_by_cycle_for_grating_experiment import IndexOfColumns, list_fit_procedure +from __code.group_images_by_cycle_for_grating_experiment import ( + IndexOfColumns, + list_fit_procedure, +) class ExcelTableHandler(TableHandler): @@ -79,7 +82,9 @@ def set_period(self, value, method=None, new=True): period_widget.setValue(int(value)) self.insert_widget(row=self.row_to_set, column=column, widget=period_widget) period_widget.valueChanged.connect( - lambda value, row=self.row_to_set, column=column: method(value, row, column) + lambda value, row=self.row_to_set, column=column: method( + value, row, column + ) ) else: period_widget = self.get_widget(row=self.row_to_set, column=column) @@ -100,9 +105,13 @@ def set_images_per_step(self, value, method=None, new=True): images_per_step.setMinimum(1) images_per_step.setMaximum(10) images_per_step.setValue(images_per_step_value) - self.insert_widget(row=self.row_to_set, column=column, widget=images_per_step) + self.insert_widget( + row=self.row_to_set, column=column, widget=images_per_step + ) images_per_step.valueChanged.connect( - lambda value, row=self.row_to_set, column=column: method(value, row, column) + lambda value, row=self.row_to_set, column=column: method( + value, row, column + ) ) else: images_per_step = self.get_widget(row=self.row_to_set, column=column) @@ -142,7 +151,9 @@ def set_fit_procedure(self, value, method=None, new=True): fit_procedure.setCurrentIndex(index) self.insert_widget(row=self.row_to_set, column=column, widget=fit_procedure) fit_procedure.currentIndexChanged.connect( - lambda value, row=self.row_to_set, column=column: method(value, row, column) + lambda value, row=self.row_to_set, column=column: method( + value, row, column + ) ) else: fit_procedure = self.get_widget(row=self.row_to_set, column=column) @@ -178,9 +189,13 @@ def set_gamma_filter_data_ob(self, value, method=None, new=True): gamma_filter_ui = QComboBox() gamma_filter_ui.addItems(values) gamma_filter_ui.setCurrentText(gamma_filter_value) - self.insert_widget(row=self.row_to_set, column=column, widget=gamma_filter_ui) + self.insert_widget( + row=self.row_to_set, column=column, widget=gamma_filter_ui + ) gamma_filter_ui.currentIndexChanged.connect( - lambda value, row=self.row_to_set, column=column: method(value, row, column) + lambda value, row=self.row_to_set, column=column: method( + value, row, column + ) ) else: gamma_filter_ui = self.get_widget(row=self.row_to_set, column=column) @@ -261,9 +276,13 @@ def set_gamma_filter_dc(self, value, method=None, new=True): gamma_filter_dc_ui = QComboBox() gamma_filter_dc_ui.addItems(values) gamma_filter_dc_ui.setCurrentText(gamma_filter_value) - self.insert_widget(row=self.row_to_set, column=column, widget=gamma_filter_dc_ui) + self.insert_widget( + row=self.row_to_set, column=column, widget=gamma_filter_dc_ui + ) gamma_filter_dc_ui.currentIndexChanged.connect( - lambda value, row=self.row_to_set, column=column: method(value, row, column) + lambda value, row=self.row_to_set, column=column: method( + value, row, column + ) ) else: gamma_filter_dc_ui = self.get_widget(row=self.row_to_set, column=column) @@ -351,7 +370,9 @@ def set_dc_outlier_removal(self, value, method=None, new=True): dc_outlier_ui.setCurrentText(dc_outlier_value) self.insert_widget(row=self.row_to_set, column=column, widget=dc_outlier_ui) dc_outlier_ui.currentIndexChanged.connect( - lambda value, row=self.row_to_set, column=column: method(value, row, column) + lambda value, row=self.row_to_set, column=column: method( + value, row, column + ) ) else: dc_outlier_ui = self.get_widget(row=self.row_to_set, column=column) diff --git a/notebooks/__code/group_images_by_cycle_for_grating_experiment/group_images.py b/notebooks/__code/group_images_by_cycle_for_grating_experiment/group_images.py index fd64e903b..f468eb244 100755 --- a/notebooks/__code/group_images_by_cycle_for_grating_experiment/group_images.py +++ b/notebooks/__code/group_images_by_cycle_for_grating_experiment/group_images.py @@ -8,10 +8,16 @@ from __code import file_handler from __code._utilities.string import format_html_message from __code.file_folder_browser import FileFolderBrowser -from __code.group_images_by_cycle_for_grating_experiment.combine_and_move_files import CombineAndMoveFiles +from __code.group_images_by_cycle_for_grating_experiment.combine_and_move_files import ( + CombineAndMoveFiles, +) from __code.group_images_by_cycle_for_grating_experiment.get import Get -from __code.group_images_by_cycle_for_grating_experiment.group_images_by_cycle import GroupImagesByCycle -from __code.group_images_by_cycle_for_grating_experiment.notebook_widgets import NotebookWidgets +from __code.group_images_by_cycle_for_grating_experiment.group_images_by_cycle import ( + GroupImagesByCycle, +) +from __code.group_images_by_cycle_for_grating_experiment.notebook_widgets import ( + NotebookWidgets, +) from __code.ipywe import myfileselector from .utilities import make_dictionary_of_groups_new_names @@ -70,9 +76,13 @@ def load_config(self): self.config = config def select_data_to_sort(self): - o_file_broswer = FileFolderBrowser(working_dir=self.working_dir, next_function=self.info_files_selected) + o_file_broswer = FileFolderBrowser( + working_dir=self.working_dir, next_function=self.info_files_selected + ) self.files_list_widget = o_file_broswer.select_images_with_search( - instruction="Select Images to Sort ...", multiple_flag=True, filters={"TIFF": "*.tif*"} + instruction="Select Images to Sort ...", + multiple_flag=True, + filters={"TIFF": "*.tif*"}, ) def info_files_selected(self, selected): @@ -88,7 +98,11 @@ def info_files_selected(self, selected): display(format_html_message("Input folder ", selected)) display(format_html_message("Nbr files ", str(len(self.list_images)))) if "tif" not in self.file_extension: - display(format_html_message("This notebook only works with TIFF images!", is_error=True)) + display( + format_html_message( + "This notebook only works with TIFF images!", is_error=True + ) + ) return def define_type_of_data(self): @@ -134,7 +148,9 @@ def group_images(self): o_group.run() self.master_outer_inner_dictionary = o_group.master_outer_inner_dictionary - self.dictionary_of_groups_sorted = self.format_into_dictionary_of_groups(self.master_outer_inner_dictionary) + self.dictionary_of_groups_sorted = self.format_into_dictionary_of_groups( + self.master_outer_inner_dictionary + ) dict_new_names = make_dictionary_of_groups_new_names( self.dictionary_of_groups_sorted, self.dict_group_outer_value ) @@ -195,7 +211,9 @@ def create_first_last_run_of_each_group_dictionary(self): "first": _list_for_this_group[0], "last": _list_for_this_group[-1], } - self.first_last_run_of_each_group_dictionary = first_last_run_of_each_group_dictionary + self.first_last_run_of_each_group_dictionary = ( + first_last_run_of_each_group_dictionary + ) def _get_group_number_selected(self): group_string = self.select_group_ui.value @@ -229,7 +247,9 @@ def make_dictionary_of_groups_old_names(self): return dictionary_of_groups_old_names def copy_combine_and_rename_files(self, output_folder): - o_combine_and_move_files = CombineAndMoveFiles(parent=self, output_folder=output_folder, debug=DEBUG) + o_combine_and_move_files = CombineAndMoveFiles( + parent=self, output_folder=output_folder, debug=DEBUG + ) o_combine_and_move_files.run() def generate_angel_configuration_file(self): diff --git a/notebooks/__code/group_images_by_cycle_for_grating_experiment/group_images_by_cycle.py b/notebooks/__code/group_images_by_cycle_for_grating_experiment/group_images_by_cycle.py index 779029273..5dec48d93 100755 --- a/notebooks/__code/group_images_by_cycle_for_grating_experiment/group_images_by_cycle.py +++ b/notebooks/__code/group_images_by_cycle_for_grating_experiment/group_images_by_cycle.py @@ -4,7 +4,9 @@ class GroupImagesByCycle: - def __init__(self, list_of_files=None, list_of_metadata_key=None, tolerance_value=0.1): + def __init__( + self, list_of_files=None, list_of_metadata_key=None, tolerance_value=0.1 + ): """ :param list_of_files: [file1, file2, file3, ...] diff --git a/notebooks/__code/group_images_by_cycle_for_grating_experiment/notebook_widgets.py b/notebooks/__code/group_images_by_cycle_for_grating_experiment/notebook_widgets.py index da0192a41..58a8be32f 100755 --- a/notebooks/__code/group_images_by_cycle_for_grating_experiment/notebook_widgets.py +++ b/notebooks/__code/group_images_by_cycle_for_grating_experiment/notebook_widgets.py @@ -3,7 +3,9 @@ from ipywidgets import widgets from __code._utilities.metadata_handler import MetadataHandler -from __code.group_images_by_cycle_for_grating_experiment.event_handler import EventHandler +from __code.group_images_by_cycle_for_grating_experiment.event_handler import ( + EventHandler, +) from __code.group_images_by_cycle_for_grating_experiment.get import Get @@ -28,17 +30,27 @@ def select_metadata_to_use_for_sorting(self): [ widgets.Label("Search:"), widgets.Text("", layout=widgets.Layout(width="150px")), - widgets.Button(description="X", button_style="", layout=widgets.Layout(width="10px")), + widgets.Button( + description="X", + button_style="", + layout=widgets.Layout(width="10px"), + ), ] ) self.parent.search_outer_field = search_outer.children[1] - self.parent.search_outer_field.observe(o_event.search_metadata_outer_edited, names="value") + self.parent.search_outer_field.observe( + o_event.search_metadata_outer_edited, names="value" + ) search_outer.children[2].on_click(o_event.reset_search_metadata_outer) result2 = widgets.HBox( [ - widgets.HTML("Metadata Selected:", layout=widgets.Layout(width="200px")), - widgets.Label(metadata_outer_value, layout=widgets.Layout(width="100%")), + widgets.HTML( + "Metadata Selected:", layout=widgets.Layout(width="200px") + ), + widgets.Label( + metadata_outer_value, layout=widgets.Layout(width="100%") + ), ] ) self.parent.metadata_outer_selected_label = result2.children[1] @@ -56,7 +68,9 @@ def select_metadata_to_use_for_sorting(self): ] ) self.parent.list_metadata_outer = metadata_outer.children[2] - self.parent.list_metadata_outer.observe(o_event.metadata_outer_selection_changed, names="value") + self.parent.list_metadata_outer.observe( + o_event.metadata_outer_selection_changed, names="value" + ) # metadata_inner search_inner = widgets.HBox( @@ -65,17 +79,27 @@ def select_metadata_to_use_for_sorting(self): "Search:", ), widgets.Text("", layout=widgets.Layout(width="150px")), - widgets.Button(description="X", button_style="", layout=widgets.Layout(width="10px")), + widgets.Button( + description="X", + button_style="", + layout=widgets.Layout(width="10px"), + ), ] ) self.parent.search_inner_field = search_inner.children[1] - self.parent.search_inner_field.observe(o_event.search_metadata_inner_edited, names="value") + self.parent.search_inner_field.observe( + o_event.search_metadata_inner_edited, names="value" + ) search_inner.children[2].on_click(o_event.reset_search_metadata_inner) result1 = widgets.HBox( [ - widgets.HTML("Metadata Selected:", layout=widgets.Layout(width="200px")), - widgets.Label(metadata_inner_value, layout=widgets.Layout(width="100%")), + widgets.HTML( + "Metadata Selected:", layout=widgets.Layout(width="200px") + ), + widgets.Label( + metadata_inner_value, layout=widgets.Layout(width="100%") + ), ] ) self.parent.metadata_inner_selected_label = result1.children[1] @@ -93,11 +117,20 @@ def select_metadata_to_use_for_sorting(self): ] ) self.parent.list_metadata_inner = metadata_inner.children[2] - self.parent.list_metadata_inner.observe(o_event.metadata_inner_selection_changed, names="value") + self.parent.list_metadata_inner.observe( + o_event.metadata_inner_selection_changed, names="value" + ) - switch_button = widgets.Button(description="Switch", icon="refresh", layout=widgets.Layout(display="center")) + switch_button = widgets.Button( + description="Switch", + icon="refresh", + layout=widgets.Layout(display="center"), + ) switch_layout = widgets.VBox( - [switch_button], layout=widgets.Layout(height="400px", display="center", justify_content="space-around") + [switch_button], + layout=widgets.Layout( + height="400px", display="center", justify_content="space-around" + ), ) switch_button.on_click(o_event.switch_inner_outer_metadata) @@ -119,7 +152,10 @@ def display_groups(self): vbox_left = widgets.VBox( [ widgets.HTML("Select Group:"), - widgets.Select(options=group_label, layout=widgets.Layout(width="150px", height="300px")), + widgets.Select( + options=group_label, + layout=widgets.Layout(width="150px", height="300px"), + ), ] ) select_group_ui = vbox_left.children[1] @@ -130,7 +166,8 @@ def display_groups(self): [ widgets.HTML("Original file names:"), widgets.Select( - options=o_get.list_of_files_basename_only(0), layout=widgets.Layout(width="450px", height="300px") + options=o_get.list_of_files_basename_only(0), + layout=widgets.Layout(width="450px", height="300px"), ), ] ) @@ -157,12 +194,16 @@ def display_groups(self): vbox_right = widgets.VBox( [ widgets.Label("Metadata:"), - widgets.Textarea(value="", layout=widgets.Layout(width="200px", height="300px")), + widgets.Textarea( + value="", layout=widgets.Layout(width="200px", height="300px") + ), ] ) self.parent.metadata_ui = vbox_right.children[1] - o_event.list_of_files_changed(value={"new": o_get.list_of_files_basename_only(0)[0]}) + o_event.list_of_files_changed( + value={"new": o_get.list_of_files_basename_only(0)[0]} + ) hbox = widgets.HBox([vbox_left, vbox_center, vbox_3, vbox_right]) display(hbox) @@ -175,8 +216,12 @@ def display_groups(self): bottom_hbox = widgets.HBox( [ - widgets.HTML("Images are in:", layout=widgets.Layout(width="150px")), - widgets.Label(self.parent.data_path, layout=widgets.Layout(width="90%")), + widgets.HTML( + "Images are in:", layout=widgets.Layout(width="150px") + ), + widgets.Label( + self.parent.data_path, layout=widgets.Layout(width="90%") + ), ] ) self.parent.path_ui = bottom_hbox.children[1] @@ -200,7 +245,9 @@ def generate_angel_configuration_file(self): create_new_excel_ui.on_click(o_event.create_new_excel_clicked) hori_layout = widgets.Layout() - hori_box = widgets.HBox([load_previous_excel_ui, create_new_excel_ui], layout=hori_layout) + hori_box = widgets.HBox( + [load_previous_excel_ui, create_new_excel_ui], layout=hori_layout + ) display(hori_box) self.parent.excel_info_widget = widgets.HTML("") @@ -209,8 +256,15 @@ def generate_angel_configuration_file(self): def define_type_of_data(self): sample_or_ob_ui = widgets.HBox( [ - widgets.HTML("Which type of data are you working with?", layout=widgets.Layout(width="250px")), - widgets.RadioButtons(options=["sample", "ob"], value="sample", layout=widgets.Layout(width="50%")), + widgets.HTML( + "Which type of data are you working with?", + layout=widgets.Layout(width="250px"), + ), + widgets.RadioButtons( + options=["sample", "ob"], + value="sample", + layout=widgets.Layout(width="50%"), + ), widgets.HTML("
"), ] ) diff --git a/notebooks/__code/group_images_by_cycle_for_grating_experiment/repeat_widget_change_dialog.py b/notebooks/__code/group_images_by_cycle_for_grating_experiment/repeat_widget_change_dialog.py index 17eb29733..0cb0e4c3a 100755 --- a/notebooks/__code/group_images_by_cycle_for_grating_experiment/repeat_widget_change_dialog.py +++ b/notebooks/__code/group_images_by_cycle_for_grating_experiment/repeat_widget_change_dialog.py @@ -7,7 +7,9 @@ from __code import load_ui from __code.group_images_by_cycle_for_grating_experiment import IndexOfColumns -from __code.group_images_by_cycle_for_grating_experiment.excel_table_handler import ExcelTableHandler as TableHandler +from __code.group_images_by_cycle_for_grating_experiment.excel_table_handler import ( + ExcelTableHandler as TableHandler, +) class RepeatWidgetChangeDialog(QMainWindow): @@ -26,11 +28,21 @@ def __init__(self, parent=None, input_row=0, input_column=0): self.init_widgets() def init_widgets(self): - statis_file_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "static") - self.do_not_repeat_released_file = os.path.join(statis_file_path, "do_not_repeat_button_released.png") - self.do_not_repeat_pressed_file = os.path.join(statis_file_path, "do_not_repeat_button_pressed.png") - self.repeat_released_file = os.path.join(statis_file_path, "repeat_button_released.png") - self.repeat_pressed_file = os.path.join(statis_file_path, "repeat_button_pressed.png") + statis_file_path = os.path.join( + os.path.dirname(os.path.dirname(__file__)), "static" + ) + self.do_not_repeat_released_file = os.path.join( + statis_file_path, "do_not_repeat_button_released.png" + ) + self.do_not_repeat_pressed_file = os.path.join( + statis_file_path, "do_not_repeat_button_pressed.png" + ) + self.repeat_released_file = os.path.join( + statis_file_path, "repeat_button_released.png" + ) + self.repeat_pressed_file = os.path.join( + statis_file_path, "repeat_button_pressed.png" + ) no_repeat_icon = QIcon(self.do_not_repeat_released_file) self.ui.do_not_repeat_pushButton.setIcon(no_repeat_icon) diff --git a/notebooks/__code/group_images_by_cycle_for_grating_experiment/sort_images_within_each_cycle.py b/notebooks/__code/group_images_by_cycle_for_grating_experiment/sort_images_within_each_cycle.py index bcdd62a94..4b830c197 100755 --- a/notebooks/__code/group_images_by_cycle_for_grating_experiment/sort_images_within_each_cycle.py +++ b/notebooks/__code/group_images_by_cycle_for_grating_experiment/sort_images_within_each_cycle.py @@ -55,7 +55,10 @@ def sort_files(self, list_files=None, dict_how_to_sort=None): result = pd.concat(frames) new_result_sorted = result.sort_values( - by=[dict_how_to_sort["1st_variable"]["name"], dict_how_to_sort["2nd_variable"]["name"]], + by=[ + dict_how_to_sort["1st_variable"]["name"], + dict_how_to_sort["2nd_variable"]["name"], + ], ascending=[ dict_how_to_sort["1st_variable"]["is_ascending"], dict_how_to_sort["2nd_variable"]["is_ascending"], diff --git a/notebooks/__code/group_images_by_cycle_for_grating_experiment/utilities.py b/notebooks/__code/group_images_by_cycle_for_grating_experiment/utilities.py index ab1276284..b2a94ecc9 100755 --- a/notebooks/__code/group_images_by_cycle_for_grating_experiment/utilities.py +++ b/notebooks/__code/group_images_by_cycle_for_grating_experiment/utilities.py @@ -29,7 +29,9 @@ def combine_images(output_folder="./", list_images=None, new_file_name=""): del o_combined -def make_dictionary_of_groups_new_names(dictionary_of_groups_sorted, dict_group_outer_value): +def make_dictionary_of_groups_new_names( + dictionary_of_groups_sorted, dict_group_outer_value +): dict_new_names = OrderedDict() for _group_index in dictionary_of_groups_sorted.keys(): nbr_files = len(dictionary_of_groups_sorted[_group_index]) @@ -40,7 +42,12 @@ def make_dictionary_of_groups_new_names(dictionary_of_groups_sorted, dict_group_ if len(before_and_after_decimal) > 1: before_decimal = int(before_and_after_decimal[0]) before_decimal_str = f"{before_decimal:03d}" - str_outer_value_formatted = "_".join([before_decimal_str, before_and_after_decimal[1]]) - list_new_names = [f"group_{str_outer_value_formatted}_{_index:07d}.tiff" for _index in np.arange(nbr_files)] + str_outer_value_formatted = "_".join( + [before_decimal_str, before_and_after_decimal[1]] + ) + list_new_names = [ + f"group_{str_outer_value_formatted}_{_index:07d}.tiff" + for _index in np.arange(nbr_files) + ] dict_new_names[_group_index] = list_new_names return dict_new_names diff --git a/notebooks/__code/group_images_by_cycle_for_panoramic_stitching/group_images.py b/notebooks/__code/group_images_by_cycle_for_panoramic_stitching/group_images.py index 49adbc60f..1cda3e917 100755 --- a/notebooks/__code/group_images_by_cycle_for_panoramic_stitching/group_images.py +++ b/notebooks/__code/group_images_by_cycle_for_panoramic_stitching/group_images.py @@ -10,8 +10,12 @@ from __code import file_handler from __code._utilities.string import format_html_message from __code.file_handler import copy_and_rename_files_to_folder, make_or_reset_folder -from __code.group_images_by_cycle_for_panoramic_stitching.group_images_by_cycle import GroupImagesByCycle -from __code.group_images_by_cycle_for_panoramic_stitching.sort_images_within_each_cycle import SortImagesWithinEachCycle +from __code.group_images_by_cycle_for_panoramic_stitching.group_images_by_cycle import ( + GroupImagesByCycle, +) +from __code.group_images_by_cycle_for_panoramic_stitching.sort_images_within_each_cycle import ( + SortImagesWithinEachCycle, +) from __code.ipywe import fileselector METADATA_ERROR = 1 # range +/- for which a metadata will be considered identical @@ -77,7 +81,11 @@ def info_folder_selected(self, selected): display(format_html_message("Input folder ", selected)) display(format_html_message("Nbr files ", str(len(self.list_images)))) if "tif" not in self.file_extension: - display(format_html_message("This notebook only works with TIFF images!", is_error=True)) + display( + format_html_message( + "This notebook only works with TIFF images!", is_error=True + ) + ) return # group the images @@ -100,7 +108,9 @@ def group_images(self): ) o_group.run() self.dictionary_of_groups_unsorted = o_group.dictionary_of_groups - dict_new_names = GroupImages.make_dictionary_of_groups_new_names(self.dictionary_of_groups_unsorted) + dict_new_names = GroupImages.make_dictionary_of_groups_new_names( + self.dictionary_of_groups_unsorted + ) self.dictionary_of_groups_new_names = dict_new_names self.dictionary_file_vs_metadata = o_group.master_dictionary @@ -126,7 +136,10 @@ def display_groups(self): vbox_left = widgets.VBox( [ widgets.Label("Select Group:"), - widgets.Select(options=group_label, layout=widgets.Layout(width="100px", height="300px")), + widgets.Select( + options=group_label, + layout=widgets.Layout(width="100px", height="300px"), + ), ] ) select_group_ui = vbox_left.children[1] @@ -147,17 +160,24 @@ def display_groups(self): vbox_right = widgets.VBox( [ widgets.Label("Metadata:"), - widgets.Textarea(value="", layout=widgets.Layout(width="200px", height="300px")), + widgets.Textarea( + value="", layout=widgets.Layout(width="200px", height="300px") + ), ] ) self.metadata_ui = vbox_right.children[1] - self.list_of_files_changed(value={"new": self.get_list_of_files_basename_only(0)[0]}) + self.list_of_files_changed( + value={"new": self.get_list_of_files_basename_only(0)[0]} + ) hbox = widgets.HBox([vbox_left, vbox_center, vbox_right]) display(hbox) bottom_hbox = widgets.HBox( - [widgets.Label("Images are in:"), widgets.Label(self.data_path, layout=widgets.Layout(width="90%"))] + [ + widgets.Label("Images are in:"), + widgets.Label(self.data_path, layout=widgets.Layout(width="90%")), + ] ) self.path_ui = bottom_hbox.children[1] display(bottom_hbox) @@ -178,7 +198,9 @@ def list_of_files_changed(self, value): file_selected = value["new"] full_file_name_selected = os.path.join(self.data_path, file_selected) string_to_display = "" - for _key, _value in dictionary_file_vs_metadata[full_file_name_selected].items(): + for _key, _value in dictionary_file_vs_metadata[ + full_file_name_selected + ].items(): string_to_display += f"{_key}: {_value}\n" self.metadata_ui.value = string_to_display @@ -193,7 +215,9 @@ def how_to_sort_files(self): value=self.metadata_name_to_select[0], layout=widgets.Layout(width="400px", height="50px"), ), - widgets.RadioButtons(options=["Ascending", "Descending"], value="Ascending"), + widgets.RadioButtons( + options=["Ascending", "Descending"], value="Ascending" + ), ] ) name_of_first_metadata_ui = vbox1.children[1] @@ -215,11 +239,15 @@ def how_to_sort_files(self): value=self.metadata_name_to_select[1], layout=widgets.Layout(width="400px", height="50px"), ), - widgets.RadioButtons(options=["Ascending", "Descending"], value="Ascending"), + widgets.RadioButtons( + options=["Ascending", "Descending"], value="Ascending" + ), ] ) name_of_second_metadata_ui = vbox2.children[1] - name_of_second_metadata_ui.observe(self.name_of_second_metadata_changed, "value") + name_of_second_metadata_ui.observe( + self.name_of_second_metadata_changed, "value" + ) self.name_of_second_metadata_ui = name_of_second_metadata_ui sorting_type_var2_ui = vbox2.children[2] sorting_type_var2_ui.observe(self.sorting_algorithm_variable2_changed, "value") @@ -229,7 +257,9 @@ def how_to_sort_files(self): "is_ascending": is_ascending, } - self.how_to_sort_within_cycle["2nd_variable"]["name"] = name_of_second_metadata_ui.value + self.how_to_sort_within_cycle["2nd_variable"]["name"] = ( + name_of_second_metadata_ui.value + ) tab = widgets.Tab([vbox1, vbox2]) [tab.set_title(i, title) for i, title in enumerate(tab_titles)] @@ -239,7 +269,9 @@ def how_to_sort_files(self): hori1 = widgets.HBox( [ widgets.Label("Select Group", layout=widgets.Layout(width="100px")), - widgets.Dropdown(options=self.list_group_label, layout=widgets.Layout(width="150px")), + widgets.Dropdown( + options=self.list_group_label, layout=widgets.Layout(width="150px") + ), ] ) self.select_group_ui = hori1.children[1] @@ -248,14 +280,18 @@ def how_to_sort_files(self): vbox3 = widgets.VBox( [ widgets.Label("Old name", layout=widgets.Layout(width="200px")), - widgets.Select(options="", layout=widgets.Layout(width="400px", height="300px")), + widgets.Select( + options="", layout=widgets.Layout(width="400px", height="300px") + ), ] ) vbox4 = widgets.VBox( [ widgets.Label("New name", layout=widgets.Layout(width="200px")), - widgets.Select(options="", layout=widgets.Layout(width="400px", height="300px")), + widgets.Select( + options="", layout=widgets.Layout(width="400px", height="300px") + ), ] ) @@ -278,7 +314,9 @@ def update_old_name_order(self): group_number_selected = self._get_group_number_selected() list_files_sorted = self.dictionary_of_groups_sorted[group_number_selected] - short_list_files_sorted = [os.path.basename(_file) for _file in list_files_sorted] + short_list_files_sorted = [ + os.path.basename(_file) for _file in list_files_sorted + ] self.old_name_ui.options = short_list_files_sorted def _get_group_number_selected(self): @@ -333,14 +371,21 @@ def create_folder_for_each_cycle(self, output_folder): if not output_folder: return - output_folder_basename = os.path.basename(self.folder_selected) + "_sorted_by_cycle" + output_folder_basename = ( + os.path.basename(self.folder_selected) + "_sorted_by_cycle" + ) output_folder = os.path.join(output_folder, output_folder_basename) output_folder = os.path.abspath(output_folder) dictionary_of_groups_sorted = self.dictionary_of_groups_sorted dictionary_of_groups_new_names = self.dictionary_of_groups_new_names nbr_groups = len(dictionary_of_groups_sorted.keys()) - hbox = widgets.HBox([widgets.IntProgress(value=0, min=0, max=nbr_groups), widgets.Label(f"0/{nbr_groups}")]) + hbox = widgets.HBox( + [ + widgets.IntProgress(value=0, min=0, max=nbr_groups), + widgets.Label(f"0/{nbr_groups}"), + ] + ) progress_ui = hbox.children[0] label_ui = hbox.children[1] display(hbox) @@ -354,8 +399,12 @@ def create_folder_for_each_cycle(self, output_folder): output_folder=full_folder_name, ) progress_ui.value = _group_index + 1 - label_ui.value = f"{_group_index+1}/{nbr_groups}" + label_ui.value = f"{_group_index + 1}/{nbr_groups}" hbox.close() message = f"{nbr_groups} folders have been created" - display(format_html_message(pre_message=message, spacer=" in ", message=output_folder)) + display( + format_html_message( + pre_message=message, spacer=" in ", message=output_folder + ) + ) diff --git a/notebooks/__code/group_images_by_cycle_for_panoramic_stitching/group_images_by_cycle.py b/notebooks/__code/group_images_by_cycle_for_panoramic_stitching/group_images_by_cycle.py index 54b782ab1..634e1ca11 100755 --- a/notebooks/__code/group_images_by_cycle_for_panoramic_stitching/group_images_by_cycle.py +++ b/notebooks/__code/group_images_by_cycle_for_panoramic_stitching/group_images_by_cycle.py @@ -5,7 +5,9 @@ class GroupImagesByCycle: - def __init__(self, list_of_files=None, list_of_metadata_key=None, tolerance_value=0.1): + def __init__( + self, list_of_files=None, list_of_metadata_key=None, tolerance_value=0.1 + ): """ :param list_of_files: [file1, file2, file3, ...] @@ -27,7 +29,9 @@ def create_master_dictionary(self): list_key_value = self.list_of_metadata_key_value_number print(f"list_key_value: {list_key_value}") master_dictionary = MetadataHandler.retrieve_value_of_metadata_key( - list_files=self.list_of_files, list_key=list_key_value, is_from_notebook=True + list_files=self.list_of_files, + list_key=list_key_value, + is_from_notebook=True, ) clean_master_dictionary = OrderedDict() @@ -62,7 +66,9 @@ def group(self): list_of_metadata_for_that_file.append(_value) if not is_this_list_already_in_those_lists_within_tolerance( - list_of_metadata_for_that_file, full_list_of_metadata_list, tolerance=self.tolerance_value + list_of_metadata_for_that_file, + full_list_of_metadata_list, + tolerance=self.tolerance_value, ): full_list_of_metadata_list.append(list_of_metadata_for_that_file) list_files_in_that_group.append(_file) diff --git a/notebooks/__code/group_images_by_cycle_for_panoramic_stitching/sort_images_within_each_cycle.py b/notebooks/__code/group_images_by_cycle_for_panoramic_stitching/sort_images_within_each_cycle.py index bcdd62a94..4b830c197 100755 --- a/notebooks/__code/group_images_by_cycle_for_panoramic_stitching/sort_images_within_each_cycle.py +++ b/notebooks/__code/group_images_by_cycle_for_panoramic_stitching/sort_images_within_each_cycle.py @@ -55,7 +55,10 @@ def sort_files(self, list_files=None, dict_how_to_sort=None): result = pd.concat(frames) new_result_sorted = result.sort_values( - by=[dict_how_to_sort["1st_variable"]["name"], dict_how_to_sort["2nd_variable"]["name"]], + by=[ + dict_how_to_sort["1st_variable"]["name"], + dict_how_to_sort["2nd_variable"]["name"], + ], ascending=[ dict_how_to_sort["1st_variable"]["is_ascending"], dict_how_to_sort["2nd_variable"]["is_ascending"], diff --git a/notebooks/__code/gui_widgets.py b/notebooks/__code/gui_widgets.py index c0f0c4d2b..feea247a0 100755 --- a/notebooks/__code/gui_widgets.py +++ b/notebooks/__code/gui_widgets.py @@ -13,7 +13,9 @@ def gui_dname(dir=None, message=""): """Select files""" if message == "": message = "Select Folder ..." - dirname = QFileDialog.getExistingDirectory(None, message, dir, QFileDialog.ShowDirsOnly) + dirname = QFileDialog.getExistingDirectory( + None, message, dir, QFileDialog.ShowDirsOnly + ) if platform.system() == "Linux": return dirname @@ -60,7 +62,9 @@ def gui_single_file(dir=None): """Select one o file via a dialog and returns the file name.""" if dir is None: dir = "/" - fname = QFileDialog.getOpenFileName(None, "Select file...", dir, filter="Spectra File (*_Spectra.txt)") + fname = QFileDialog.getOpenFileName( + None, "Select file...", dir, filter="Spectra File (*_Spectra.txt)" + ) return fname @@ -69,5 +73,7 @@ def gui_csv_fname(dir=None): """Select one or more file via a dialog and returns the file name.""" if dir is None: dir = "/" - fname = QFileDialog.getOpenFileNames(None, "Select file(s)...", dir, filter="Fits files(*.csv)") + fname = QFileDialog.getOpenFileNames( + None, "Select file(s)...", dir, filter="Fits files(*.csv)" + ) return fname diff --git a/notebooks/__code/hfir_reactor_element_analysis/event_handler.py b/notebooks/__code/hfir_reactor_element_analysis/event_handler.py index 36247c610..981c0981b 100755 --- a/notebooks/__code/hfir_reactor_element_analysis/event_handler.py +++ b/notebooks/__code/hfir_reactor_element_analysis/event_handler.py @@ -61,7 +61,9 @@ def list_of_images_selection_changed(self): plot_type = "." for _index, _y_axis in enumerate(list_y_axis): - self.parent.profiles_plot.axes.plot(x_axis, _y_axis, plot_type, label=list_file_selected[_index]) + self.parent.profiles_plot.axes.plot( + x_axis, _y_axis, plot_type, label=list_file_selected[_index] + ) self.parent.profiles_plot.axes.legend() self.parent.profiles_plot.axes.set_title("Profile of selected images") @@ -75,7 +77,9 @@ def list_of_images_selection_changed(self): list_mean_position_of_elements = self.get_list_mean_position_of_elements() if list_mean_position_of_elements is not None: for _value in list_mean_position_of_elements: - self.parent.profiles_plot.axes.axvline(_value, linestyle="-", color="blue") + self.parent.profiles_plot.axes.axvline( + _value, linestyle="-", color="blue" + ) # thresholds threshold = self.parent.ui.threshold_slider.value() @@ -133,12 +137,17 @@ def calculate_list_of_local_max(self, working_y_axis=None): list_of_y_max = [] local_y_max = 0 x_of_local_max = 0 - minimum_number_of_points_to_high_region = self.parent.ui.minimum_number_of_points_spinBox.value() + minimum_number_of_points_to_high_region = ( + self.parent.ui.minimum_number_of_points_spinBox.value() + ) number_of_points_in_current_high_region = 0 for _x_value, _y_value in zip(working_x_axis, working_y_axis, strict=False): if _y_value < threshold: if local_y_max != 0: - if number_of_points_in_current_high_region >= minimum_number_of_points_to_high_region: + if ( + number_of_points_in_current_high_region + >= minimum_number_of_points_to_high_region + ): list_of_x_of_local_max.append(x_of_local_max) list_of_y_max.append(local_y_max) local_y_max = 0 @@ -182,7 +191,9 @@ def plot_average_position(self): ymin = 0 ymax = len(self.parent.list_of_images) for _value in list_mean_position_of_elements: - self.parent.elements_position.axes.vlines(_value, ymin, ymax, colors=(1, 0, 0, 1)) + self.parent.elements_position.axes.vlines( + _value, ymin, ymax, colors=(1, 0, 0, 1) + ) def populate_elements_position_table(self): global_list_of_xy_max = self.parent.global_list_of_xy_max @@ -231,7 +242,9 @@ def display_background_error_in_elements_position_table(self): self.parent.list_ideal_position_of_elements = list_mean_position_of_elements tolerance = self.parent.ui.tolerance_value_doubleSpinBox.value() - mean_angle_offset_between_elements = self.get_mean_angle_offset_between_elements() + mean_angle_offset_between_elements = ( + self.get_mean_angle_offset_between_elements() + ) list_average = np.arange( list_mean_position_of_elements[0], nbr_column * mean_angle_offset_between_elements, @@ -255,8 +268,12 @@ def display_background_error_in_elements_position_table(self): _error_row.append(0) break - while (_element_position < (list_average[_reference_index_element] - tolerance)) or ( - _element_position > (list_average[_reference_index_element] + tolerance) + while ( + _element_position + < (list_average[_reference_index_element] - tolerance) + ) or ( + _element_position + > (list_average[_reference_index_element] + tolerance) ): if _element_position < list_average[_reference_index_element]: _error_row.append(0) @@ -271,7 +288,9 @@ def display_background_error_in_elements_position_table(self): if found_flag: # find the closest place where this _element_position goes - nearest_index = get_closest_index(array=list_average, value=_element_position) + nearest_index = get_closest_index( + array=list_average, value=_element_position + ) ideal_table_data_error[_row_index, nearest_index] = 1 _reference_index_element += 1 @@ -289,7 +308,9 @@ def display_background_error_in_elements_position_table(self): for _column, _state in enumerate(error_table[_row]): if _state == 0: if o_table.is_item(row=_row, column=_column): - o_table.set_background_color(row=_row, column=_column, qcolor=QtGui.QColor(150, 0, 0)) + o_table.set_background_color( + row=_row, column=_column, qcolor=QtGui.QColor(150, 0, 0) + ) def display_missing_peaks_table(self): ideal_table_data_error = self.parent.ideal_table_data_error @@ -322,21 +343,32 @@ def populate_error_table_backup(self): nbr_column = int(median_number_of_elements) elements_position_error_table = np.zeros((nbr_row, nbr_column)) # mean_angle_offset_between_elements = self.get_mean_angle_offset_between_elements() - self.parent.list_mean_position_of_elements = self.get_list_mean_position_of_elements() + self.parent.list_mean_position_of_elements = ( + self.get_list_mean_position_of_elements() + ) self.tolerance_value = self.parent.ui.tolerance_value_doubleSpinBox.value() for _row_index in np.arange(nbr_row): - for _col_index, _col_value in enumerate(self.parent.elements_position_formatted_raw_table[_row_index]): - col_index = self.where_value_found_within_expected_tolerance(value=_col_value) + for _col_index, _col_value in enumerate( + self.parent.elements_position_formatted_raw_table[_row_index] + ): + col_index = self.where_value_found_within_expected_tolerance( + value=_col_value + ) if col_index >= 0: - elements_position_error_table[_row_index, col_index] += 1 # we found an element at that position + elements_position_error_table[_row_index, col_index] += ( + 1 # we found an element at that position + ) # if a value > 1 is found later on when checking the table, an error will be displayed for that # cell elif col_index == -1: closer_col_index = get_closest_index( - value=_col_value, array=self.parent.list_mean_position_of_elements + value=_col_value, + array=self.parent.list_mean_position_of_elements, ) - elements_position_error_table[_row_index, closer_col_index] = -10 # very low value to make sure + elements_position_error_table[ + _row_index, closer_col_index + ] = -10 # very low value to make sure # an error will show up in this cell o_table = TableHandler(table_ui=self.parent.error_tableWidget) @@ -348,9 +380,13 @@ def populate_error_table_backup(self): o_table.insert_empty_column(_col_index) o_table.insert_empty_row(_row_index) _value = elements_position_error_table[_row_index, _col_index] - o_table.insert_item_with_float(row=_row_index, column=_col_index, float_value=_value) + o_table.insert_item_with_float( + row=_row_index, column=_col_index, float_value=_value + ) - def is_value_within_expected_tolerance(self, value_to_check=None, value_expected=None): + def is_value_within_expected_tolerance( + self, value_to_check=None, value_expected=None + ): tolerance_value = self.tolerance_value if (value_to_check >= (value_expected - tolerance_value)) and ( value_to_check <= (value_expected + tolerance_value) @@ -367,7 +403,9 @@ def where_value_found_within_expected_tolerance(self, value=np.nan): list_of_ideal_elements_position = self.parent.list_mean_position_of_elements for _index, ideal_position in enumerate(list_of_ideal_elements_position): - if (value >= (ideal_position - tolerance_value)) and (value <= (ideal_position + tolerance_value)): + if (value >= (ideal_position - tolerance_value)) and ( + value <= (ideal_position + tolerance_value) + ): return _index return -1 @@ -401,7 +439,9 @@ def get_list_mean_position_of_elements(self): [nbr_row, nbr_column] = np.shape(table) - number_of_outliers_to_reject = int((self.parent.percent_of_outliers_to_reject / 100) * nbr_row) + number_of_outliers_to_reject = int( + (self.parent.percent_of_outliers_to_reject / 100) * nbr_row + ) # list_mean_value = [] # for _column_index in np.arange(nbr_column): # _col_value = table[:, _column_index] @@ -410,7 +450,9 @@ def get_list_mean_position_of_elements(self): # mean of first element _col_value = table[:, 0] - _col_value_without_outliers = reject_n_outliers(array=_col_value, n=number_of_outliers_to_reject) + _col_value_without_outliers = reject_n_outliers( + array=_col_value, n=number_of_outliers_to_reject + ) mean_first_element = np.nanmean(_col_value) # trying to calculate manually ideal list mean @@ -430,7 +472,9 @@ def get_median_number_of_elements(self): return np.median(list_number_of_elements) def format_table(self, raw_table=None): - max_number_of_elements = np.max([len(list_of_elements) for list_of_elements in raw_table]) + max_number_of_elements = np.max( + [len(list_of_elements) for list_of_elements in raw_table] + ) number_of_rows = len(raw_table) formatted_table = np.empty((number_of_rows, max_number_of_elements)) formatted_table[:] = np.nan diff --git a/notebooks/__code/hfir_reactor_element_analysis/export_data.py b/notebooks/__code/hfir_reactor_element_analysis/export_data.py index 389428b75..10d0eb122 100755 --- a/notebooks/__code/hfir_reactor_element_analysis/export_data.py +++ b/notebooks/__code/hfir_reactor_element_analysis/export_data.py @@ -11,7 +11,10 @@ def __init__(self, parent=None): self.parent = parent def run(self): - default_file_name = os.path.abspath(os.path.basename(self.parent.working_dir)) + "_elements_position.csv" + default_file_name = ( + os.path.abspath(os.path.basename(self.parent.working_dir)) + + "_elements_position.csv" + ) working_dir = os.path.dirname(self.parent.working_dir) directory = os.path.join(working_dir, default_file_name) export_file_name = QFileDialog.getSaveFileName( @@ -24,7 +27,9 @@ def run(self): if export_file_name[0]: export_file_name = export_file_name[0] - self.parent.ui.statusbar.showMessage(f"Saving {os.path.basename(export_file_name)} ... IN PROGRESS") + self.parent.ui.statusbar.showMessage( + f"Saving {os.path.basename(export_file_name)} ... IN PROGRESS" + ) self.parent.ui.statusbar.setStyleSheet("color: blue") QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) QtGui.QGuiApplication.processEvents() @@ -44,9 +49,16 @@ def run(self): data.append(_str_entry) metadata.append("#") - make_ascii_file(data=data, metadata=metadata, output_file_name=export_file_name, dim="1d") + make_ascii_file( + data=data, + metadata=metadata, + output_file_name=export_file_name, + dim="1d", + ) - self.parent.ui.statusbar.showMessage(f"{os.path.basename(export_file_name)} ... Saved!", 10000) + self.parent.ui.statusbar.showMessage( + f"{os.path.basename(export_file_name)} ... Saved!", 10000 + ) self.parent.ui.statusbar.setStyleSheet("color: green") QApplication.restoreOverrideCursor() QtGui.QGuiApplication.processEvents() diff --git a/notebooks/__code/hfir_reactor_element_analysis/hfir_reactor_element_analysis.py b/notebooks/__code/hfir_reactor_element_analysis/hfir_reactor_element_analysis.py index a7b5bbcbe..9ea4c1f04 100755 --- a/notebooks/__code/hfir_reactor_element_analysis/hfir_reactor_element_analysis.py +++ b/notebooks/__code/hfir_reactor_element_analysis/hfir_reactor_element_analysis.py @@ -24,7 +24,11 @@ def select_ascii_file(self): ascii_file_ui.show() def load_ascii(self, ascii_file_name): - display(HTML('Loading data set ... PROCESSING!')) + display( + HTML( + 'Loading data set ... PROCESSING!' + ) + ) # retrieving metadata and column names ascii_contain = read_ascii(filename=ascii_file_name) @@ -44,8 +48,17 @@ def load_ascii(self, ascii_file_name): # retrieving data with pandas self.pandas_obj = pd.read_csv( - ascii_file_name, skiprows=_line_number + 2, delimiter=", ", names=column_labels, dtype=float, index_col=0 + ascii_file_name, + skiprows=_line_number + 2, + delimiter=", ", + names=column_labels, + dtype=float, + index_col=0, ) clear_output(wait=False) - display(HTML('Loading data set ... DONE!')) + display( + HTML( + 'Loading data set ... DONE!' + ) + ) diff --git a/notebooks/__code/hfir_reactor_element_analysis/initialization.py b/notebooks/__code/hfir_reactor_element_analysis/initialization.py index 79f812fa4..4b5f92797 100755 --- a/notebooks/__code/hfir_reactor_element_analysis/initialization.py +++ b/notebooks/__code/hfir_reactor_element_analysis/initialization.py @@ -25,15 +25,23 @@ def _matplotlib(parent=None, widget=None): widget.setLayout(layout) return sc - self.parent.profiles_plot = _matplotlib(parent=self.parent, widget=self.parent.ui.profiles_widget) + self.parent.profiles_plot = _matplotlib( + parent=self.parent, widget=self.parent.ui.profiles_widget + ) - self.parent.elements_position = _matplotlib(parent=self.parent, widget=self.parent.ui.elements_position_widget) + self.parent.elements_position = _matplotlib( + parent=self.parent, widget=self.parent.ui.elements_position_widget + ) - self.parent.elements_position.mpl_connect("button_press_event", self.parent.click_on_elements_position_plot) + self.parent.elements_position.mpl_connect( + "button_press_event", self.parent.click_on_elements_position_plot + ) def widgets(self): list_of_images = self.parent.list_of_images - list_n_random_int = get_n_random_int_of_max_value_m(n=10, max=len(list_of_images)) + list_n_random_int = get_n_random_int_of_max_value_m( + n=10, max=len(list_of_images) + ) pandas_obj = self.parent.o_pandas list_max = [] list_min = [] @@ -64,7 +72,9 @@ def widgets(self): o_table = TableHandler(table_ui=self.parent.ui.metadata_tableWidget) o_table.set_column_sizes(column_sizes=[200, 200]) - self.parent.ui.number_of_elements_spinBox.setValue(self.parent.NUMBER_OF_FUEL_ELEMENTS) + self.parent.ui.number_of_elements_spinBox.setValue( + self.parent.NUMBER_OF_FUEL_ELEMENTS + ) def statusbar(self): self.parent.eventProgress = QProgressBar(self.parent.ui.statusbar) @@ -97,4 +107,6 @@ def table_of_metadata(self): for _row_index, _row_value in enumerate(formatted_data): o_table.insert_empty_row(_row_index) for _col_index, _value in enumerate(_row_value): - o_table.insert_item(row=_row_index, column=_col_index, value=_value, editable=False) + o_table.insert_item( + row=_row_index, column=_col_index, value=_value, editable=False + ) diff --git a/notebooks/__code/hfir_reactor_element_analysis/interface_handler.py b/notebooks/__code/hfir_reactor_element_analysis/interface_handler.py index 0057d417e..de0d35264 100755 --- a/notebooks/__code/hfir_reactor_element_analysis/interface_handler.py +++ b/notebooks/__code/hfir_reactor_element_analysis/interface_handler.py @@ -4,11 +4,15 @@ from qtpy.QtWidgets import QMainWindow from __code import load_ui -from __code.hfir_reactor_element_analysis.all_peaks_found_event_handler import AllPeaksFoundEventHandler +from __code.hfir_reactor_element_analysis.all_peaks_found_event_handler import ( + AllPeaksFoundEventHandler, +) from __code.hfir_reactor_element_analysis.event_handler import EventHandler from __code.hfir_reactor_element_analysis.export_data import ExportData from __code.hfir_reactor_element_analysis.initialization import Initialization -from __code.hfir_reactor_element_analysis.missing_peaks_event_handler import MissingPeaksEventHandler +from __code.hfir_reactor_element_analysis.missing_peaks_event_handler import ( + MissingPeaksEventHandler, +) class InterfaceHandler: @@ -21,7 +25,9 @@ def __init__(self, working_dir=None, o_selection=None): class Interface(QMainWindow): NUMBER_OF_FUEL_ELEMENTS = 369 MINIMUM_NUMBER_OF_ANGLE_DATA_POINTS = 50 - ELEMENTS_POSITION_OUTLIERS = 10 # number of data points to remove before calculating mean x position + ELEMENTS_POSITION_OUTLIERS = ( + 10 # number of data points to remove before calculating mean x position + ) profiles_plot = None elements_position_plot = None @@ -39,7 +45,9 @@ def __init__(self, parent=None, o_selection=None, working_dir=None): self.o_pandas = o_selection.pandas_obj self.working_dir = working_dir self.list_angles = self.o_pandas.index - self.list_of_images = [_image.strip() for _image in o_selection.column_labels[1:]] + self.list_of_images = [ + _image.strip() for _image in o_selection.column_labels[1:] + ] super(Interface, self).__init__(parent) diff --git a/notebooks/__code/ibeatles_strain_mapping_ascii_loader/main.py b/notebooks/__code/ibeatles_strain_mapping_ascii_loader/main.py index a067d7474..3aea24c1e 100755 --- a/notebooks/__code/ibeatles_strain_mapping_ascii_loader/main.py +++ b/notebooks/__code/ibeatles_strain_mapping_ascii_loader/main.py @@ -49,7 +49,10 @@ def __init__(self, working_dir=""): def select_ascii_file(self): self.file_ui = fileselector.FileSelectorPanel( - instruction="Select Ascii file", start_dir=self.working_dir, multiple=False, next=self.load + instruction="Select Ascii file", + start_dir=self.working_dir, + multiple=False, + next=self.load, ) self.file_ui.show() @@ -58,7 +61,9 @@ def load(self, filename): self.import_ascii() display( HTML( - '' + str(os.path.basename(filename)) + " " + '' + + str(os.path.basename(filename)) + + " " "has been loaded !" ) ) @@ -131,15 +136,25 @@ def plot_lambda(min_value, max_value, colormap, interpolation_method): self.ax0.cla() self.im0 = self.ax0.imshow( - data, interpolation=interpolation_method, cmap=colormap, vmin=min_value, vmax=max_value + data, + interpolation=interpolation_method, + cmap=colormap, + vmin=min_value, + vmax=max_value, ) self.cb0 = plt.colorbar(self.im0, ax=self.ax0) v = interactive( plot_lambda, - min_value=widgets.FloatSlider(min=minimum, max=maximum, value=minimum, step=step), - max_value=widgets.FloatSlider(min=minimum, max=maximum, value=maximum, step=step), - colormap=widgets.Dropdown(options=CMAPS, value=DEFAULT_CMAPS, layout=widgets.Layout(width="300px")), + min_value=widgets.FloatSlider( + min=minimum, max=maximum, value=minimum, step=step + ), + max_value=widgets.FloatSlider( + min=minimum, max=maximum, value=maximum, step=step + ), + colormap=widgets.Dropdown( + options=CMAPS, value=DEFAULT_CMAPS, layout=widgets.Layout(width="300px") + ), interpolation_method=widgets.Dropdown( options=INTERPOLATION_METHODS, value=DEFAULT_INTERPOLATION, @@ -172,7 +187,11 @@ def plot_d(min_value, max_value, colormap, interpolation_method): self.ax1.cla() self.im1 = self.ax1.imshow( - data, interpolation=interpolation_method, cmap=colormap, vmin=min_value, vmax=max_value + data, + interpolation=interpolation_method, + cmap=colormap, + vmin=min_value, + vmax=max_value, ) self.cb1 = plt.colorbar(self.im1, ax=self.ax1) @@ -200,9 +219,15 @@ def plot_d(min_value, max_value, colormap, interpolation_method): v = interactive( plot_d, - min_value=widgets.FloatSlider(min=minimum, max=maximum, value=minimum, step=step), - max_value=widgets.FloatSlider(min=minimum, max=maximum, value=maximum, step=step), - colormap=widgets.Dropdown(options=CMAPS, value=DEFAULT_CMAPS, layout=widgets.Layout(width="300px")), + min_value=widgets.FloatSlider( + min=minimum, max=maximum, value=minimum, step=step + ), + max_value=widgets.FloatSlider( + min=minimum, max=maximum, value=maximum, step=step + ), + colormap=widgets.Dropdown( + options=CMAPS, value=DEFAULT_CMAPS, layout=widgets.Layout(width="300px") + ), interpolation_method=widgets.Dropdown( options=INTERPOLATION_METHODS, value=DEFAULT_INTERPOLATION, @@ -235,7 +260,11 @@ def plot_microstrain(min_value, max_value, colormap, interpolation_method): self.ax2.cla() self.im2 = self.ax2.imshow( - data, interpolation=interpolation_method, cmap=colormap, vmin=min_value, vmax=max_value + data, + interpolation=interpolation_method, + cmap=colormap, + vmin=min_value, + vmax=max_value, ) self.cb2 = plt.colorbar(self.im2, ax=self.ax2) @@ -277,9 +306,15 @@ def format_coord(x, y): v = interactive( plot_microstrain, - min_value=widgets.FloatSlider(min=minimum, max=maximum, value=minimum, step=step), - max_value=widgets.FloatSlider(min=minimum, max=maximum, value=maximum, step=step), - colormap=widgets.Dropdown(options=CMAPS, value=DEFAULT_CMAPS, layout=widgets.Layout(width="300px")), + min_value=widgets.FloatSlider( + min=minimum, max=maximum, value=minimum, step=step + ), + max_value=widgets.FloatSlider( + min=minimum, max=maximum, value=maximum, step=step + ), + colormap=widgets.Dropdown( + options=CMAPS, value=DEFAULT_CMAPS, layout=widgets.Layout(width="300px") + ), interpolation_method=widgets.Dropdown( options=INTERPOLATION_METHODS, value=DEFAULT_INTERPOLATION, diff --git a/notebooks/__code/ibeatles_strain_mapping_hdf5_loader/hdf5_handler.py b/notebooks/__code/ibeatles_strain_mapping_hdf5_loader/hdf5_handler.py index b4e475257..01f72cc44 100755 --- a/notebooks/__code/ibeatles_strain_mapping_hdf5_loader/hdf5_handler.py +++ b/notebooks/__code/ibeatles_strain_mapping_hdf5_loader/hdf5_handler.py @@ -19,11 +19,18 @@ def import_hdf5(self): entry = f["entry"] # integrated image - self.parent.integrated_normalized_radiographs = entry["integrated normalized radiographs"]["2D array"][:] + self.parent.integrated_normalized_radiographs = entry[ + "integrated normalized radiographs" + ]["2D array"][:] # metadata self.parent.metadata = {} - list_key = ["detector_offset", "distance_source_detector", "hkl_value", "material_name"] + list_key = [ + "detector_offset", + "distance_source_detector", + "hkl_value", + "material_name", + ] for _key in list_key: self.parent.metadata[_key] = entry["metadata"][_key][()].decode("utf-8") self.parent.metadata["d0"] = entry["metadata"]["d0"][()] @@ -47,12 +54,18 @@ def import_hdf5(self): "x1": key_entry["bin coordinates"]["x1"][()], "y0": key_entry["bin coordinates"]["y0"][()], "y1": key_entry["bin coordinates"]["y1"][()], - "row_index": entry["fitting"]["kropff"][_key]["fitted"]["row_index"][()], - "column_index": entry["fitting"]["kropff"][_key]["fitted"]["column_index"][()], + "row_index": entry["fitting"]["kropff"][_key]["fitted"]["row_index"][ + () + ], + "column_index": entry["fitting"]["kropff"][_key]["fitted"][ + "column_index" + ][()], } list_row.append(entry["fitting"]["kropff"][_key]["fitted"]["row_index"][()]) - list_column.append(entry["fitting"]["kropff"][_key]["fitted"]["column_index"][()]) + list_column.append( + entry["fitting"]["kropff"][_key]["fitted"]["column_index"][()] + ) self.parent.bin[_key] = _key_dict self.parent.bin_size = entry["metadata"]["bin_size"][()] @@ -69,4 +82,6 @@ def import_hdf5(self): self.parent.lambda_hkl = {} for _key in kropff_entry.keys(): - self.parent.lambda_hkl[_key] = kropff_entry[_key]["fitted"]["lambda_hkl"]["val"][()] + self.parent.lambda_hkl[_key] = kropff_entry[_key]["fitted"]["lambda_hkl"][ + "val" + ][()] diff --git a/notebooks/__code/ibeatles_strain_mapping_hdf5_loader/main.py b/notebooks/__code/ibeatles_strain_mapping_hdf5_loader/main.py index 0be5e4c4b..dacc7268e 100755 --- a/notebooks/__code/ibeatles_strain_mapping_hdf5_loader/main.py +++ b/notebooks/__code/ibeatles_strain_mapping_hdf5_loader/main.py @@ -79,7 +79,9 @@ def load(self, filename): self.import_hdf5() display( HTML( - '' + str(os.path.basename(filename)) + " " + '' + + str(os.path.basename(filename)) + + " " "has been loaded !" ) ) @@ -125,8 +127,12 @@ def process_data(self): compact_lambda_2d[row_index, column_index] = self.lambda_hkl[_key] lambda_2d[y0:y1, x0:x1] = self.lambda_hkl[_key] - strain_mapping_2d[y0:y1, x0:x1] = self.strain_mapping[_key]["val"] # to go to microstrain - compact_strain_mapping[row_index, column_index] = self.strain_mapping[_key]["val"] + strain_mapping_2d[y0:y1, x0:x1] = self.strain_mapping[_key][ + "val" + ] # to go to microstrain + compact_strain_mapping[row_index, column_index] = self.strain_mapping[_key][ + "val" + ] d_2d[y0:y1, x0:x1] = self.d[_key] @@ -208,7 +214,9 @@ def display_lambda(self): fig = plt.figure(figsize=(4, 4), num="\u03bb (\u212b)") self.ax0 = fig.add_subplot(111) - self.ax0.imshow(self.integrated_normalized_radiographs, vmin=0, vmax=1, cmap="gray") + self.ax0.imshow( + self.integrated_normalized_radiographs, vmin=0, vmax=1, cmap="gray" + ) # self.im0 = self.ax0.imshow(self.compact_lambda_2d, cmap='jet', alpha=0.5) self.im0 = self.ax0.imshow(self.lambda_hkl_2d, cmap="jet", alpha=0.5) @@ -232,17 +240,29 @@ def plot_lambda(min_value, max_value, colormap, interpolation_method): # data = self.compact_lambda_2d self.ax0.cla() - self.ax0.imshow(self.integrated_normalized_radiographs, vmin=0, vmax=1, cmap="gray") + self.ax0.imshow( + self.integrated_normalized_radiographs, vmin=0, vmax=1, cmap="gray" + ) self.im0 = self.ax0.imshow( - data, interpolation=interpolation_method, cmap=colormap, vmin=min_value, vmax=max_value + data, + interpolation=interpolation_method, + cmap=colormap, + vmin=min_value, + vmax=max_value, ) self.cb0 = plt.colorbar(self.im0, ax=self.ax0) v = interactive( plot_lambda, - min_value=widgets.FloatSlider(min=minimum, max=maximum, value=minimum, step=step), - max_value=widgets.FloatSlider(min=minimum, max=maximum, value=maximum, step=step), - colormap=widgets.Dropdown(options=CMAPS, value=DEFAULT_CMAPS, layout=widgets.Layout(width="300px")), + min_value=widgets.FloatSlider( + min=minimum, max=maximum, value=minimum, step=step + ), + max_value=widgets.FloatSlider( + min=minimum, max=maximum, value=maximum, step=step + ), + colormap=widgets.Dropdown( + options=CMAPS, value=DEFAULT_CMAPS, layout=widgets.Layout(width="300px") + ), interpolation_method=widgets.Dropdown( options=INTERPOLATION_METHODS, value=DEFAULT_INTERPOLATION, @@ -256,7 +276,9 @@ def display_d(self): fig = plt.figure(figsize=(4, 4), num="d") self.ax1 = fig.add_subplot(111) - self.ax1.imshow(self.integrated_normalized_radiographs, vmin=0, vmax=1, cmap="gray") + self.ax1.imshow( + self.integrated_normalized_radiographs, vmin=0, vmax=1, cmap="gray" + ) # self.im0 = self.ax0.imshow(self.compact_lambda_2d, cmap='jet', alpha=0.5) self.im1 = self.ax1.imshow(self.d_2d, cmap="jet", alpha=0.5) @@ -278,17 +300,29 @@ def plot_d(min_value, max_value, colormap, interpolation_method): data = self.d_2d self.ax1.cla() - self.ax1.imshow(self.integrated_normalized_radiographs, vmin=0, vmax=1, cmap="gray") + self.ax1.imshow( + self.integrated_normalized_radiographs, vmin=0, vmax=1, cmap="gray" + ) self.im1 = self.ax1.imshow( - data, interpolation=interpolation_method, cmap=colormap, vmin=min_value, vmax=max_value + data, + interpolation=interpolation_method, + cmap=colormap, + vmin=min_value, + vmax=max_value, ) self.cb1 = plt.colorbar(self.im1, ax=self.ax1) v = interactive( plot_d, - min_value=widgets.FloatSlider(min=minimum, max=maximum, value=minimum, step=step), - max_value=widgets.FloatSlider(min=minimum, max=maximum, value=maximum, step=step), - colormap=widgets.Dropdown(options=CMAPS, value=DEFAULT_CMAPS, layout=widgets.Layout(width="300px")), + min_value=widgets.FloatSlider( + min=minimum, max=maximum, value=minimum, step=step + ), + max_value=widgets.FloatSlider( + min=minimum, max=maximum, value=maximum, step=step + ), + colormap=widgets.Dropdown( + options=CMAPS, value=DEFAULT_CMAPS, layout=widgets.Layout(width="300px") + ), interpolation_method=widgets.Dropdown( options=INTERPOLATION_METHODS, value=DEFAULT_INTERPOLATION, @@ -302,7 +336,9 @@ def display_microstrain(self): fig = plt.figure(figsize=(4, 4), num="microstrain") self.ax2 = fig.add_subplot(111) - self.ax2.imshow(self.integrated_normalized_radiographs, vmin=0, vmax=1, cmap="gray") + self.ax2.imshow( + self.integrated_normalized_radiographs, vmin=0, vmax=1, cmap="gray" + ) self.im2 = self.ax2.imshow(self.strain_2d, cmap="jet", alpha=0.5) self.cb2 = plt.colorbar(self.im2, ax=self.ax2) @@ -321,17 +357,29 @@ def plot_strain(min_value, max_value, colormap, interpolation_method): data = self.strain_2d self.ax2.cla() - self.ax2.imshow(self.integrated_normalized_radiographs, vmin=0, vmax=1, cmap="gray") + self.ax2.imshow( + self.integrated_normalized_radiographs, vmin=0, vmax=1, cmap="gray" + ) self.im2 = self.ax2.imshow( - data, interpolation=interpolation_method, cmap=colormap, vmin=min_value, vmax=max_value + data, + interpolation=interpolation_method, + cmap=colormap, + vmin=min_value, + vmax=max_value, ) self.cb2 = plt.colorbar(self.im2, ax=self.ax2) v = interactive( plot_strain, - min_value=widgets.FloatSlider(min=minimum, max=maximum, value=minimum, step=step), - max_value=widgets.FloatSlider(min=minimum, max=maximum, value=maximum, step=step), - colormap=widgets.Dropdown(options=CMAPS, value=DEFAULT_CMAPS, layout=widgets.Layout(width="300px")), + min_value=widgets.FloatSlider( + min=minimum, max=maximum, value=minimum, step=step + ), + max_value=widgets.FloatSlider( + min=minimum, max=maximum, value=maximum, step=step + ), + colormap=widgets.Dropdown( + options=CMAPS, value=DEFAULT_CMAPS, layout=widgets.Layout(width="300px") + ), interpolation_method=widgets.Dropdown( options=INTERPOLATION_METHODS, value=DEFAULT_INTERPOLATION, @@ -378,7 +426,9 @@ def display_microstrain_with_interpolation(self): scale_factor = self.bin_size out_dimensions = (grid.shape[0] * scale_factor, grid.shape[1] * scale_factor) - fig1, axs = plt.subplots(nrows=4, num="microstrain interpolated", figsize=[5, 20]) + fig1, axs = plt.subplots( + nrows=4, num="microstrain interpolated", figsize=[5, 20] + ) transform = Affine2D().scale(scale_factor, scale_factor) # Have to get an image to be able to resample @@ -400,9 +450,13 @@ def display_microstrain_with_interpolation(self): [y0, x0] = self.top_left_corner_of_roi inter_height, inter_width = np.shape(interpolated) - interpolated_strain_mapping_2d[y0 : y0 + inter_height, x0 : x0 + inter_width] = interpolated + interpolated_strain_mapping_2d[ + y0 : y0 + inter_height, x0 : x0 + inter_width + ] = interpolated - axs[3].imshow(self.integrated_normalized_radiographs, vmin=0, vmax=1, cmap="gray") + axs[3].imshow( + self.integrated_normalized_radiographs, vmin=0, vmax=1, cmap="gray" + ) im = axs[3].imshow(interpolated_strain_mapping_2d, interpolation="gaussian") self.cb = plt.colorbar(im, ax=axs[3]) @@ -417,26 +471,34 @@ def plot_interpolated(min_value, max_value, colormap, interpolation_method): axs[0].imshow(grid, cmap=colormap) axs[1].cla() - img1 = axs[1].imshow(grid, interpolation=interpolation_method, cmap=colormap) + img1 = axs[1].imshow( + grid, interpolation=interpolation_method, cmap=colormap + ) interpolated = _resample(img1, grid, out_dimensions, transform=transform) axs[2].cla() axs[2].imshow(interpolated, vmin=min_value, vmax=max_value, cmap=colormap) # with overlap - interpolated_strain_mapping_2d = np.empty((self.image_height, self.image_width)) + interpolated_strain_mapping_2d = np.empty( + (self.image_height, self.image_width) + ) interpolated_strain_mapping_2d[:] = np.nan [y0, x0] = self.top_left_corner_of_roi inter_height, inter_width = np.shape(interpolated) - interpolated_strain_mapping_2d[y0 : y0 + inter_height, x0 : x0 + inter_width] = interpolated + interpolated_strain_mapping_2d[ + y0 : y0 + inter_height, x0 : x0 + inter_width + ] = interpolated if self.cb: self.cb.remove() axs[3].cla() - axs[3].imshow(self.integrated_normalized_radiographs, vmin=0, vmax=1, cmap="gray") + axs[3].imshow( + self.integrated_normalized_radiographs, vmin=0, vmax=1, cmap="gray" + ) im = axs[3].imshow( interpolated_strain_mapping_2d * 1e6, interpolation=interpolation_method, @@ -448,9 +510,23 @@ def plot_interpolated(min_value, max_value, colormap, interpolation_method): v = interactive( plot_interpolated, - min_value=widgets.FloatSlider(min=minimum, max=maximum, value=minimum, step=step, description="min (x1e6)"), - max_value=widgets.FloatSlider(min=minimum, max=maximum, value=maximum, step=step, description="min (x1e6)"), - colormap=widgets.Dropdown(options=CMAPS, value=DEFAULT_CMAPS, layout=widgets.Layout(width="300px")), + min_value=widgets.FloatSlider( + min=minimum, + max=maximum, + value=minimum, + step=step, + description="min (x1e6)", + ), + max_value=widgets.FloatSlider( + min=minimum, + max=maximum, + value=maximum, + step=step, + description="min (x1e6)", + ), + colormap=widgets.Dropdown( + options=CMAPS, value=DEFAULT_CMAPS, layout=widgets.Layout(width="300px") + ), interpolation_method=widgets.Dropdown( options=INTERPOLATION_METHODS, value=DEFAULT_INTERPOLATION, diff --git a/notebooks/__code/icons/icons_rc.py b/notebooks/__code/icons/icons_rc.py index 5283caacb..6671cf608 100755 --- a/notebooks/__code/icons/icons_rc.py +++ b/notebooks/__code/icons/icons_rc.py @@ -133,11 +133,15 @@ def qInitResources(): - QtCore.qRegisterResourceData(rcc_version, qt_resource_struct, qt_resource_name, qt_resource_data) + QtCore.qRegisterResourceData( + rcc_version, qt_resource_struct, qt_resource_name, qt_resource_data + ) def qCleanupResources(): - QtCore.qUnregisterResourceData(rcc_version, qt_resource_struct, qt_resource_name, qt_resource_data) + QtCore.qUnregisterResourceData( + rcc_version, qt_resource_struct, qt_resource_name, qt_resource_data + ) qInitResources() diff --git a/notebooks/__code/image_profile_interface_template/dual_energy.py b/notebooks/__code/image_profile_interface_template/dual_energy.py index ca1294fe2..b013d75c3 100755 --- a/notebooks/__code/image_profile_interface_template/dual_energy.py +++ b/notebooks/__code/image_profile_interface_template/dual_energy.py @@ -19,7 +19,9 @@ from __code.bragg_edge.kropff import Kropff from __code.bragg_edge.kropff_fitting_job_handler import KropffFittingJobHandler from __code.bragg_edge.march_dollase import MarchDollase -from __code.bragg_edge.march_dollase_fitting_job_handler import MarchDollaseFittingJobHandler +from __code.bragg_edge.march_dollase_fitting_job_handler import ( + MarchDollaseFittingJobHandler, +) from __code.bragg_edge.peak_fitting_initialization import PeakFittingInitialization from __code.table_handler import TableHandler from __code.utilities import find_nearest_index @@ -35,7 +37,15 @@ def load_ob(self, folder_selected): class Interface(QMainWindow): fitting_parameters_init = { - "kropff": {"a0": 1, "b0": 1, "ahkl": 1, "bhkl": 1, "ldahkl": 1e-8, "tau": 1, "sigma": [1e-7, 1e-6, 1e-5]} + "kropff": { + "a0": 1, + "b0": 1, + "ahkl": 1, + "bhkl": 1, + "ldahkl": 1e-8, + "tau": 1, + "sigma": [1e-7, 1e-6, 1e-5], + } } bragg_edge_range = [5, 20] @@ -45,13 +55,17 @@ class Interface(QMainWindow): selection_roi_rgb = (62, 13, 244) roi_settings = { - "color": QtGui.QColor(selection_roi_rgb[0], selection_roi_rgb[1], selection_roi_rgb[2]), + "color": QtGui.QColor( + selection_roi_rgb[0], selection_roi_rgb[1], selection_roi_rgb[2] + ), "width": 0.01, "position": [10, 10], } shrinking_roi_rgb = (13, 214, 244) shrinking_roi_settings = { - "color": QtGui.QColor(shrinking_roi_rgb[0], shrinking_roi_rgb[1], shrinking_roi_rgb[2]), + "color": QtGui.QColor( + shrinking_roi_rgb[0], shrinking_roi_rgb[1], shrinking_roi_rgb[2] + ), "width": 0.01, "dashes_pattern": [4, 2], } @@ -62,7 +76,11 @@ class Interface(QMainWindow): previous_roi_selection = {"width": None, "height": None} image_size = {"width": None, "height": None} roi_id = None - xaxis_label = {"index": "File index", "tof": "TOF (\u00b5s)", "lambda": "\u03bb (\u212b)"} + xaxis_label = { + "index": "File index", + "tof": "TOF (\u00b5s)", + "lambda": "\u03bb (\u212b)", + } fitting_rois = { "kropff": { "step1": None, @@ -74,7 +92,11 @@ class Interface(QMainWindow): is_file_imported = False # True only when the import button has been used bragg_edge_range_ui = None - kropff_fitting_range = {"high": [None, None], "low": [None, None], "bragg_peak": [None, None]} + kropff_fitting_range = { + "high": [None, None], + "low": [None, None], + "bragg_peak": [None, None], + } fitting_peak_ui = None # vertical line in fitting view (tab 2) fitting_procedure_started = {"march-dollase": False, "kropff": False} @@ -130,7 +152,9 @@ def update_time_spectra(self): distance_source_detector_m = float(self.ui.distance_detector_sample.text()) self.ui.statusbar.showMessage("", 100) # 10s except ValueError: - self.ui.statusbar.showMessage("distance source detector input is WRONG", 120000) # 2mn + self.ui.statusbar.showMessage( + "distance source detector input is WRONG", 120000 + ) # 2mn self.ui.statusbar.setStyleSheet("color: red") return @@ -138,7 +162,9 @@ def update_time_spectra(self): detector_offset_micros = float(self.ui.detector_offset.text()) self.ui.statusbar.showMessage("", 100) # 10s except ValueError: - self.ui.statusbar.showMessage("detector offset input is WRONG", 120000) # 2mn + self.ui.statusbar.showMessage( + "detector offset input is WRONG", 120000 + ) # 2mn self.ui.statusbar.setStyleSheet("color: red") return @@ -158,7 +184,9 @@ def get_live_image(self): _data = self.o_norm.data["sample"]["data"] nbr_images = len(_data) - list_of_indexes_to_keep = random.sample(list(range(nbr_images)), nbr_data_to_use) + list_of_indexes_to_keep = random.sample( + list(range(nbr_images)), nbr_data_to_use + ) final_array = [] for _index in list_of_indexes_to_keep: @@ -208,7 +236,10 @@ def bragg_edge_range_changed(self): def reset_profile_of_bin_size_slider(self): max_value = np.min( - [int(str(self.ui.profile_of_bin_size_width.text())), int(str(self.ui.profile_of_bin_size_height.text()))] + [ + int(str(self.ui.profile_of_bin_size_width.text())), + int(str(self.ui.profile_of_bin_size_height.text())), + ] ) self.ui.profile_of_bin_size_slider.setMaximum(max_value) self.ui.profile_of_bin_size_slider.setValue(max_value) @@ -263,7 +294,11 @@ def update_dict_profile_to_fit(self): profile_to_fit = { "yaxis": yaxis, - "xaxis": {"index": index_selected, "tof": tof_selected, "lambda": lambda_selected}, + "xaxis": { + "index": index_selected, + "tof": tof_selected, + "lambda": lambda_selected, + }, } self.dict_profile_to_fit = profile_to_fit @@ -273,11 +308,16 @@ def fit_that_selection_pushed_by_program(self, initialize_region=True): dict_regions = o_get.all_russian_doll_region_full_infos() o_init = PeakFittingInitialization(parent=self) - fitting_input_dictionary = o_init.fitting_input_dictionary(nbr_rois=len(dict_regions)) + fitting_input_dictionary = o_init.fitting_input_dictionary( + nbr_rois=len(dict_regions) + ) o_init.set_top_keys_values( - fitting_input_dictionary, {"xaxis": x_axis, "bragg_edge_range": self.bragg_edge_range} + fitting_input_dictionary, + {"xaxis": x_axis, "bragg_edge_range": self.bragg_edge_range}, + ) + self.append_dict_regions_to_fitting_input_dictionary( + dict_regions, fitting_input_dictionary ) - self.append_dict_regions_to_fitting_input_dictionary(dict_regions, fitting_input_dictionary) # fitting_input_dictionary['xaxis'] = x_axis # fitting_input_dictionary['bragg_edge_range'] = self.bragg_edge_range @@ -305,7 +345,9 @@ def fit_that_selection_pushed_by_program(self, initialize_region=True): self.ui.actionExport.setEnabled(True) self.select_first_row_of_all_fitting_table() - def append_dict_regions_to_fitting_input_dictionary(self, dict_regions, fitting_input_dictionary): + def append_dict_regions_to_fitting_input_dictionary( + self, dict_regions, fitting_input_dictionary + ): for _row in dict_regions.keys(): _entry = dict_regions[_row] for _key in _entry.keys(): @@ -446,18 +488,27 @@ def update_profile_of_bin_slider_labels(self): def change_profile_of_bin_slider_signal(self): self.ui.profile_of_bin_size_slider.valueChanged.disconnect() - self.ui.profile_of_bin_size_slider.valueChanged.connect(self.profile_of_bin_size_slider_changed_after_import) + self.ui.profile_of_bin_size_slider.valueChanged.connect( + self.profile_of_bin_size_slider_changed_after_import + ) def update_vertical_line_in_profile_plot(self): o_get = Get(parent=self) x_axis, x_axis_label = o_get.x_axis() - bragg_edge_range = [x_axis[self.bragg_edge_range[0]], x_axis[self.bragg_edge_range[1]]] + bragg_edge_range = [ + x_axis[self.bragg_edge_range[0]], + x_axis[self.bragg_edge_range[1]], + ] if self.bragg_edge_range_ui: self.ui.profile.removeItem(self.bragg_edge_range_ui) self.bragg_edge_range_ui = pg.LinearRegionItem( - values=bragg_edge_range, orientation=None, brush=None, movable=True, bounds=None + values=bragg_edge_range, + orientation=None, + brush=None, + movable=True, + bounds=None, ) self.bragg_edge_range_ui.sigRegionChanged.connect(self.bragg_edge_range_changed) self.bragg_edge_range_ui.setZValue(-10) @@ -508,7 +559,12 @@ def export_button_clicked(self): def roi_radiobuttons_changed(self): if self.ui.square_roi_radiobutton.isChecked(): slider_visible = True - new_width = np.min([int(str(self.ui.roi_width.text())), int(str(self.ui.roi_height.text()))]) + new_width = np.min( + [ + int(str(self.ui.roi_width.text())), + int(str(self.ui.roi_height.text())), + ] + ) mode = "square" else: slider_visible = False @@ -581,30 +637,34 @@ def update_kropff_fit_table_graph(self, fit_region="high"): :param fit_region: 'high', 'low' or 'bragg_peak' """ o_gui = GuiUtility(parent=self) - fit_parameter_selected = o_gui.get_kropff_fit_parameter_selected(fit_region=fit_region) + fit_parameter_selected = o_gui.get_kropff_fit_parameter_selected( + fit_region=fit_region + ) parameter_array = [] parameter_error_array = [] fitting_input_dictionary = self.fitting_input_dictionary for _index in fitting_input_dictionary["rois"].keys(): - _parameter = fitting_input_dictionary["rois"][_index]["fitting"]["kropff"][fit_region][ - fit_parameter_selected - ] - _error = fitting_input_dictionary["rois"][_index]["fitting"]["kropff"][fit_region][ - f"{fit_parameter_selected}_error" - ] + _parameter = fitting_input_dictionary["rois"][_index]["fitting"]["kropff"][ + fit_region + ][fit_parameter_selected] + _error = fitting_input_dictionary["rois"][_index]["fitting"]["kropff"][ + fit_region + ][f"{fit_parameter_selected}_error"] parameter_array.append(_parameter) parameter_error_array.append(_error) plot_ui = o_gui.get_kropff_fit_graph_ui(fit_region=fit_region) x_array = np.arange(len(parameter_array)) - cleaned_parameter_array, cleaned_parameter_error_array = exclude_y_value_when_error_is_nan( - parameter_array, parameter_error_array + cleaned_parameter_array, cleaned_parameter_error_array = ( + exclude_y_value_when_error_is_nan(parameter_array, parameter_error_array) ) plot_ui.axes.cla() if fit_region == "bragg_peak": plot_ui.axes.set_yscale("log") - plot_ui.axes.errorbar(x_array, cleaned_parameter_array, cleaned_parameter_error_array, marker="s") + plot_ui.axes.errorbar( + x_array, cleaned_parameter_array, cleaned_parameter_error_array, marker="s" + ) plot_ui.axes.set_xlabel("Row # (see Table tab)") plot_ui.draw() @@ -640,14 +700,18 @@ def kropff_bragg_peak_right_click(self, position): def march_dollase_table_state_changed(self, state=None, row=None, column=None): o_march = MarchDollase(parent=self) if row == 0: - _widget = self.ui.march_dollase_user_input_table.cellWidget(row, column).children()[-1] + _widget = self.ui.march_dollase_user_input_table.cellWidget( + row, column + ).children()[-1] if (column == 1) or (column == 2): _textedit = _widget _textedit.setText(o_march.get_initial_parameter_value(column=column)) _textedit.setVisible(not state) elif column == 0: _label = _widget - _label.setText(f"{float(o_march.get_initial_parameter_value(column=column)):0.6f}") + _label.setText( + f"{float(o_march.get_initial_parameter_value(column=column)):0.6f}" + ) _label.setVisible(not state) else: _label = _widget diff --git a/notebooks/__code/image_profile_interface_template/interface_initialization.py b/notebooks/__code/image_profile_interface_template/interface_initialization.py index 8db79cb33..9e161b550 100755 --- a/notebooks/__code/image_profile_interface_template/interface_initialization.py +++ b/notebooks/__code/image_profile_interface_template/interface_initialization.py @@ -104,9 +104,15 @@ def _matplotlib(parent=None, widget=None): widget.setLayout(layout) return sc - self.parent.kropff_high_plot = _matplotlib(parent=self.parent, widget=self.parent.ui.high_widget) - self.parent.kropff_low_plot = _matplotlib(parent=self.parent, widget=self.parent.ui.low_widget) - self.parent.kropff_bragg_peak_plot = _matplotlib(parent=self.parent, widget=self.parent.ui.bragg_peak_widget) + self.parent.kropff_high_plot = _matplotlib( + parent=self.parent, widget=self.parent.ui.high_widget + ) + self.parent.kropff_low_plot = _matplotlib( + parent=self.parent, widget=self.parent.ui.low_widget + ) + self.parent.kropff_bragg_peak_plot = _matplotlib( + parent=self.parent, widget=self.parent.ui.bragg_peak_widget + ) self.parent.march_dollase_plot = _matplotlib( parent=self.parent, widget=self.parent.ui.march_dollase_graph_widget @@ -122,7 +128,13 @@ def pyqtgraph_fitting(self): def kropff_fitting_table(self): ## Kropff # high lambda - column_names = ["x\u2080; y\u2080; width; height", "a\u2080", "b\u2080", "a\u2080_error", "b\u2080_error"] + column_names = [ + "x\u2080; y\u2080; width; height", + "a\u2080", + "b\u2080", + "a\u2080_error", + "b\u2080_error", + ] column_sizes = [150, 100, 100, 100, 100] o_high = TableHandler(table_ui=self.parent.ui.high_lda_tableWidget) for _col_index, _col_name in enumerate(column_names): @@ -131,7 +143,13 @@ def kropff_fitting_table(self): o_high.set_column_sizes(column_sizes=column_sizes) # low lambda - column_names = ["x\u2080; y\u2080; width; height", "a_hkl", "b_hkl", "a_hkl_error", "b_hkl_error"] + column_names = [ + "x\u2080; y\u2080; width; height", + "a_hkl", + "b_hkl", + "a_hkl_error", + "b_hkl_error", + ] column_sizes = [150, 100, 100, 100, 100] o_low = TableHandler(table_ui=self.parent.ui.low_lda_tableWidget) for _col_index, _col_name in enumerate(column_names): @@ -140,7 +158,15 @@ def kropff_fitting_table(self): o_low.set_column_sizes(column_sizes=column_sizes) # bragg edge - column_names = ["x0; y0; width; height", "t_hkl", "tau", "sigma", "t_hkl_error", "tau_error", "sigma_error"] + column_names = [ + "x0; y0; width; height", + "t_hkl", + "tau", + "sigma", + "t_hkl_error", + "tau_error", + "sigma_error", + ] column_sizes = [150, 100, 100, 100, 100, 100, 100] o_bragg = TableHandler(table_ui=self.parent.ui.bragg_edge_tableWidget) for _col_index, _col_name in enumerate(column_names): @@ -149,15 +175,23 @@ def kropff_fitting_table(self): o_bragg.set_column_sizes(column_sizes=column_sizes) def march_dollase(self): - self.parent.march_dollase_history_state_full_reset = copy.deepcopy(self.march_dollase_history_state) + self.parent.march_dollase_history_state_full_reset = copy.deepcopy( + self.march_dollase_history_state + ) # init widgets _file_path = os.path.dirname(__file__) - up_arrow_file = os.path.abspath(os.path.join(_file_path, "../static/up_arrow_black.png")) + up_arrow_file = os.path.abspath( + os.path.join(_file_path, "../static/up_arrow_black.png") + ) self.parent.ui.march_dollase_user_input_up.setIcon(QtGui.QIcon(up_arrow_file)) - down_arrow_file = os.path.abspath(os.path.join(_file_path, "../static/down_arrow_black.png")) - self.parent.ui.march_dollase_user_input_down.setIcon(QtGui.QIcon(down_arrow_file)) + down_arrow_file = os.path.abspath( + os.path.join(_file_path, "../static/down_arrow_black.png") + ) + self.parent.ui.march_dollase_user_input_down.setIcon( + QtGui.QIcon(down_arrow_file) + ) o_gui = GuiUtility(parent=self.parent) o_gui.fill_march_dollase_table( @@ -165,7 +199,9 @@ def march_dollase(self): initial_parameters=self.parent.march_dollase_fitting_initial_parameters, ) - self.parent.march_dollase_fitting_history_table = self.march_dollase_history_state + self.parent.march_dollase_fitting_history_table = ( + self.march_dollase_history_state + ) self.parent.march_dollase_fitting_history_table_default_new_row = copy.deepcopy( self.march_dollase_history_state[0] ) @@ -187,16 +223,36 @@ def march_dollase(self): "A\u2085_error", "A\u2086_error", ] - column_sizes = [150, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100] + column_sizes = [ + 150, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + 100, + ] o_march = TableHandler(table_ui=self.parent.ui.march_dollase_result_table) for _col_index, _col_name in enumerate(column_names): o_march.insert_column(_col_index) o_march.set_column_names(column_names=column_names) o_march.set_column_sizes(column_sizes=column_sizes) - state_advanced_columns = not self.parent.ui.march_dollase_advanced_mode_checkBox.isChecked() + state_advanced_columns = ( + not self.parent.ui.march_dollase_advanced_mode_checkBox.isChecked() + ) o_gui.set_columns_hidden( - table_ui=self.parent.ui.march_dollase_user_input_table, list_of_columns=[5, 6], state=state_advanced_columns + table_ui=self.parent.ui.march_dollase_user_input_table, + list_of_columns=[5, 6], + state=state_advanced_columns, ) # table @@ -213,17 +269,29 @@ def labels(self): self.parent.ui.fitting_lambda_radiobutton.setText("\u03bb (\u212b)") def text_fields(self): - self.parent.ui.distance_detector_sample.setText(str(self.distance_detector_sample)) + self.parent.ui.distance_detector_sample.setText( + str(self.distance_detector_sample) + ) self.parent.ui.detector_offset.setText(str(self.detector_offset)) - self.parent.ui.kropff_high_lda_a0_init.setText(str(self.parent.fitting_parameters_init["kropff"]["a0"])) - self.parent.ui.kropff_high_lda_b0_init.setText(str(self.parent.fitting_parameters_init["kropff"]["b0"])) - self.parent.ui.kropff_low_lda_ahkl_init.setText(str(self.parent.fitting_parameters_init["kropff"]["ahkl"])) - self.parent.ui.kropff_low_lda_bhkl_init.setText(str(self.parent.fitting_parameters_init["kropff"]["bhkl"])) + self.parent.ui.kropff_high_lda_a0_init.setText( + str(self.parent.fitting_parameters_init["kropff"]["a0"]) + ) + self.parent.ui.kropff_high_lda_b0_init.setText( + str(self.parent.fitting_parameters_init["kropff"]["b0"]) + ) + self.parent.ui.kropff_low_lda_ahkl_init.setText( + str(self.parent.fitting_parameters_init["kropff"]["ahkl"]) + ) + self.parent.ui.kropff_low_lda_bhkl_init.setText( + str(self.parent.fitting_parameters_init["kropff"]["bhkl"]) + ) self.parent.ui.kropff_bragg_peak_ldahkl_init.setText( str(self.parent.fitting_parameters_init["kropff"]["ldahkl"]) ) - self.parent.ui.kropff_bragg_peak_tau_init.setText(str(self.parent.fitting_parameters_init["kropff"]["tau"])) + self.parent.ui.kropff_bragg_peak_tau_init.setText( + str(self.parent.fitting_parameters_init["kropff"]["tau"]) + ) # list_sigma = self.parent.fitting_parameters_init['kropff']['sigma'] # list_sigma = [str(_value) for _value in list_sigma] # str_list_sigma = ", ".join(list_sigma) @@ -237,7 +305,9 @@ def widgets(self): self.parent.ui.splitter.setSizes([500, 400]) self.parent.ui.roi_size_slider.setMinimum(1) - max_value = np.min([self.parent.image_size["width"], self.parent.image_size["height"]]) + max_value = np.min( + [self.parent.image_size["width"], self.parent.image_size["height"]] + ) self.parent.ui.roi_size_slider.setMaximum(max_value) default_roi_size = int(max_value / 3) self.parent.ui.roi_size_slider.setValue(default_roi_size) diff --git a/notebooks/__code/images_and_metadata_extrapolation_matcher.py b/notebooks/__code/images_and_metadata_extrapolation_matcher.py index f761e7cc1..2594e5325 100755 --- a/notebooks/__code/images_and_metadata_extrapolation_matcher.py +++ b/notebooks/__code/images_and_metadata_extrapolation_matcher.py @@ -40,8 +40,12 @@ def get_merged_dataframe(self): return self.merged_dataframe def load_ascii_files(self): - self.ascii_file_1_dataframe = self.retrieve_dataframe(filename=self.ascii_file_1) - self.ascii_file_2_dataframe = self.retrieve_dataframe(filename=self.ascii_file_2) + self.ascii_file_1_dataframe = self.retrieve_dataframe( + filename=self.ascii_file_1 + ) + self.ascii_file_2_dataframe = self.retrieve_dataframe( + filename=self.ascii_file_2 + ) def retrieve_dataframe(self, filename=""): _dataframe = pd.read_csv(filename) @@ -49,7 +53,9 @@ def retrieve_dataframe(self, filename=""): return _dataframe def remove_white_space_in_column_names(self, dataframe): - clean_column_names = [_old_col_name.strip() for _old_col_name in list(dataframe.columns.values)] + clean_column_names = [ + _old_col_name.strip() for _old_col_name in list(dataframe.columns.values) + ] dataframe.columns = clean_column_names return dataframe @@ -65,7 +71,9 @@ def format_timestamp(self, dataframe): return dataframe def merge_data(self): - if (INDEX_SIMPLE_MERGE in self.ascii_file_1_dataframe) and (INDEX_SIMPLE_MERGE in self.ascii_file_2_dataframe): + if (INDEX_SIMPLE_MERGE in self.ascii_file_1_dataframe) and ( + INDEX_SIMPLE_MERGE in self.ascii_file_2_dataframe + ): self.simple_merge() else: @@ -76,7 +84,10 @@ def simple_merge(self): self.set_index(self.ascii_file_2_dataframe) self.merged_dataframe = pd.merge( - self.ascii_file_1_dataframe, self.ascii_file_2_dataframe, on=INDEX_SIMPLE_MERGE, how="outer" + self.ascii_file_1_dataframe, + self.ascii_file_2_dataframe, + on=INDEX_SIMPLE_MERGE, + how="outer", ) def set_index(self, dataframe, index=INDEX_SIMPLE_MERGE): @@ -87,7 +98,10 @@ def merge_with_extrapolation(self): self.set_index(self.ascii_file_2_dataframe, index=INDEX_EXTRAPOLATION_MERGE) merged_dataframe = pd.merge( - self.ascii_file_1_dataframe, self.ascii_file_2_dataframe, on=INDEX_EXTRAPOLATION_MERGE, how="outer" + self.ascii_file_1_dataframe, + self.ascii_file_2_dataframe, + on=INDEX_EXTRAPOLATION_MERGE, + how="outer", ) merged_dataframe.sort_values(by=INDEX_EXTRAPOLATION_MERGE, inplace=True) self.merged_dataframe = merged_dataframe.reset_index(drop=True) @@ -96,11 +110,21 @@ def merge_with_extrapolation(self): def select_metadata_to_extrapolate(self): list_metadata = self.get_column_names(self.merged_dataframe) - display(HTML('CTRL + Click to select multiple rows!')) + display( + HTML( + 'CTRL + Click to select multiple rows!' + ) + ) box = widgets.HBox( [ - widgets.Label("Select Metadata to Extrapolate:", layout=widgets.Layout(width="30%")), - widgets.SelectMultiple(options=list_metadata, layout=widgets.Layout(width="70%", height="70%")), + widgets.Label( + "Select Metadata to Extrapolate:", + layout=widgets.Layout(width="30%"), + ), + widgets.SelectMultiple( + options=list_metadata, + layout=widgets.Layout(width="70%", height="70%"), + ), ], layout=widgets.Layout(height="250px"), ) @@ -125,7 +149,9 @@ def extrapolate_metadata(self, metadata_name=""): _metadata_value = metadata_array[_index] if np.isnan(_metadata_value): _new_value = Extrapolate.calculate_extrapolated_metadata( - global_index=_index, metadata_array=metadata_array, timestamp_array=timestamp_array + global_index=_index, + metadata_array=metadata_array, + timestamp_array=timestamp_array, ) extrapolated_metadata_array.append(_new_value) extrapolated_timestamp_array.append(timestamp_array[_index]) @@ -144,10 +170,15 @@ def metadata_to_display_init(self): self.metadata_to_display_changed(value) def metadata_to_display_changed(self, name_of_metadata_to_display): - self.extract_known_and_unknown_axis_infos(metadata_name=name_of_metadata_to_display) + self.extract_known_and_unknown_axis_infos( + metadata_name=name_of_metadata_to_display + ) data_known = go.Scatter( - x=self.timestamp_s_metadata_known, y=self.metadata_column, mode="markers", name="Original metadata" + x=self.timestamp_s_metadata_known, + y=self.metadata_column, + mode="markers", + name="Original metadata", ) data_extrapolated = go.Scatter( @@ -174,7 +205,9 @@ def extract_known_and_unknown_axis_infos(self, metadata_name=""): # known metadata values timestamp_metadata_known = self.ascii_file_2_dataframe["timestamp_user_format"] self.timestamp_s_metadata_known = [ - TimestampFormatter.convert_to_second(_time, timestamp_format=TIMESTAMP_FORMAT) + TimestampFormatter.convert_to_second( + _time, timestamp_format=TIMESTAMP_FORMAT + ) for _time in timestamp_metadata_known ] self.metadata_column = self.ascii_file_2_dataframe[metadata_name] @@ -187,7 +220,9 @@ def extract_known_and_unknown_axis_infos(self, metadata_name=""): # for _time in timestamp_metadata_unknown] timestamp_metadata_unknown = self.extrapolated_timestamp_only[metadata_name] self.timestamp_s_metadata_unknown = [ - TimestampFormatter.convert_to_second(_time, timestamp_format=TIMESTAMP_FORMAT) + TimestampFormatter.convert_to_second( + _time, timestamp_format=TIMESTAMP_FORMAT + ) for _time in timestamp_metadata_unknown ] @@ -200,7 +235,9 @@ def get_column_names(self, dataframe): clean_list_columns = [ _name for _name in list_columns - if not self._is_name_in_list(name=_name, list_name=[INDEX_EXTRAPOLATION_MERGE, INDEX_SIMPLE_MERGE]) + if not self._is_name_in_list( + name=_name, list_name=[INDEX_EXTRAPOLATION_MERGE, INDEX_SIMPLE_MERGE] + ) ] return clean_list_columns @@ -223,15 +260,21 @@ def make_and_inform_of_full_output_file_name(self, folder_name): display_html_message(title_message="Output folder name:", message=folder_name) output_file_name = self.get_output_file_name() - display_html_message(title_message="Output file name:", message=output_file_name) + display_html_message( + title_message="Output file name:", message=output_file_name + ) return os.path.join(folder_name, output_file_name) def export_ascii(self, folder_name): - full_output_file_name = self.make_and_inform_of_full_output_file_name(folder_name) + full_output_file_name = self.make_and_inform_of_full_output_file_name( + folder_name + ) self.cleanup_merged_dataframe() self.merged_dataframe.to_csv(full_output_file_name) - display_html_message(title_message="File Created with Success!", message_type="ok") + display_html_message( + title_message="File Created with Success!", message_type="ok" + ) def cleanup_merged_dataframe(self): # keeping only the raws with filename information defined @@ -240,7 +283,9 @@ def cleanup_merged_dataframe(self): class Extrapolate: @staticmethod - def get_first_metadata_and_index_value(index=-1, metadata_array=[], direction="left"): + def get_first_metadata_and_index_value( + index=-1, metadata_array=[], direction="left" + ): if direction == "left": coeff = -1 else: @@ -256,18 +301,30 @@ def get_first_metadata_and_index_value(index=-1, metadata_array=[], direction="l return [metadata_array[index], index] @staticmethod - def calculate_extrapolated_metadata(global_index=-1, metadata_array=[], timestamp_array=[]): - [left_metadata_value, left_index] = Extrapolate.get_first_metadata_and_index_value( - index=global_index, metadata_array=metadata_array, direction="left" + def calculate_extrapolated_metadata( + global_index=-1, metadata_array=[], timestamp_array=[] + ): + [left_metadata_value, left_index] = ( + Extrapolate.get_first_metadata_and_index_value( + index=global_index, metadata_array=metadata_array, direction="left" + ) ) - [right_metadata_value, right_index] = Extrapolate.get_first_metadata_and_index_value( - index=global_index, metadata_array=metadata_array, direction="right" + [right_metadata_value, right_index] = ( + Extrapolate.get_first_metadata_and_index_value( + index=global_index, metadata_array=metadata_array, direction="right" + ) ) - left_timestamp_s_format = TimestampFormatter.convert_to_second(timestamp_array[left_index]) - right_timestamp_s_format = TimestampFormatter.convert_to_second(timestamp_array[right_index]) + left_timestamp_s_format = TimestampFormatter.convert_to_second( + timestamp_array[left_index] + ) + right_timestamp_s_format = TimestampFormatter.convert_to_second( + timestamp_array[right_index] + ) - x_timestamp_s_format = TimestampFormatter.convert_to_second(timestamp_array[global_index]) + x_timestamp_s_format = TimestampFormatter.convert_to_second( + timestamp_array[global_index] + ) extra_value = Extrapolate.extrapolate_value( x=x_timestamp_s_format, diff --git a/notebooks/__code/images_registration_pystackreg/main.py b/notebooks/__code/images_registration_pystackreg/main.py index 1fb7fda52..383f5d643 100755 --- a/notebooks/__code/images_registration_pystackreg/main.py +++ b/notebooks/__code/images_registration_pystackreg/main.py @@ -11,7 +11,10 @@ from pystackreg import StackReg from tqdm import tqdm -from __code._utilities.file import make_or_reset_folder, retrieve_list_of_most_dominant_extension_from_folder +from __code._utilities.file import ( + make_or_reset_folder, + retrieve_list_of_most_dominant_extension_from_folder, +) from __code._utilities.images import read_img_stack from __code._utilities.json import save_json from __code._utilities.time import get_current_time_in_special_file_name_format @@ -61,7 +64,9 @@ def load_images(self, folder_name=None): self.folder_name = folder_name # retrieve list of files - self.list_of_files, ext = retrieve_list_of_most_dominant_extension_from_folder(folder=folder_name) + self.list_of_files, ext = retrieve_list_of_most_dominant_extension_from_folder( + folder=folder_name + ) self.stack = read_img_stack(list_files=self.list_of_files, ext=ext) @@ -70,20 +75,26 @@ def load_images(self, folder_name=None): def display_unregistered(self): def preview_unregistered(image_index, vmin=0.8, vmax=1.2): - fig, ax = plt.subplots(ncols=3, nrows=1, num="Unregistered images", figsize=(15, 5)) + fig, ax = plt.subplots( + ncols=3, nrows=1, num="Unregistered images", figsize=(15, 5) + ) ax[0].imshow(self.stack[0], vmin=0, vmax=1) ax[0].set_title("First image") ax[1].imshow(self.stack[image_index], vmin=0, vmax=1) ax[1].set_title(f"Image #{image_index}") - image = ax[2].imshow(np.divide(self.stack[image_index], self.stack[0]), vmin=vmin, vmax=vmax) + image = ax[2].imshow( + np.divide(self.stack[image_index], self.stack[0]), vmin=vmin, vmax=vmax + ) ax[2].set_title(f"Image[{image_index}] / First image") cb = plt.colorbar(image, ax=ax[2]) v = interactive( preview_unregistered, - image_index=widgets.IntSlider(min=0, max=len(self.list_of_files) - 1, value=1), + image_index=widgets.IntSlider( + min=0, max=len(self.list_of_files) - 1, value=1 + ), vmin=widgets.FloatSlider(min=0, max=2, value=0.8), vmax=widgets.FloatSlider(min=0, max=2, value=1.2), ) @@ -142,7 +153,9 @@ def _get_crop_region(self, selector): return x0, x1, y0, y1, data_have_been_cropped def perform_cropping(self): - x0, x1, y0, y1, data_have_been_cropped = self._get_crop_region(self.selector_unregistered) + x0, x1, y0, y1, data_have_been_cropped = self._get_crop_region( + self.selector_unregistered + ) self.crop["before registration"] = {"x0": x0, "x1": x1, "y0": y0, "y1": y1} if data_have_been_cropped: @@ -197,7 +210,9 @@ def preview_registered(image_index, vmin=0.8, vmax=1.2): if self.fig2: self.fig2.clear() - self.fig2, ax2 = plt.subplots(ncols=3, nrows=1, num="Registered images", figsize=(15, 5)) + self.fig2, ax2 = plt.subplots( + ncols=3, nrows=1, num="Registered images", figsize=(15, 5) + ) ax2[0].imshow(self.registered_stack[0], vmin=0, vmax=1) ax2[0].set_title("First image") @@ -206,7 +221,9 @@ def preview_registered(image_index, vmin=0.8, vmax=1.2): ax2[1].set_title(f"Image #{image_index}") image = ax2[2].imshow( - np.divide(self.registered_stack[image_index], self.registered_stack[0]), vmin=vmin, vmax=vmax + np.divide(self.registered_stack[image_index], self.registered_stack[0]), + vmin=vmin, + vmax=vmax, ) ax2[2].set_title(f"Image[{image_index}] / First image") cb = plt.colorbar(image, ax=ax2[2]) @@ -214,7 +231,9 @@ def preview_registered(image_index, vmin=0.8, vmax=1.2): v2 = interactive( preview_registered, - image_index=widgets.IntSlider(min=0, max=len(self.list_of_files) - 1, value=1), + image_index=widgets.IntSlider( + min=0, max=len(self.list_of_files) - 1, value=1 + ), vmin=widgets.FloatSlider(min=0, max=2, value=0.8), vmax=widgets.FloatSlider(min=0, max=2, value=1.2), ) @@ -252,7 +271,9 @@ def _select_callback(eclick, erelease): ax.set_title("Click and drag to select region to crop") def perform_cropping_for_export(self): - x0, x1, y0, y1, data_have_been_cropped = self._get_crop_region(self.selector_registered) + x0, x1, y0, y1, data_have_been_cropped = self._get_crop_region( + self.selector_registered + ) self.crop["after registration"] = {"x0": x0, "x1": x1, "y0": y0, "y1": y1} if data_have_been_cropped: @@ -295,12 +316,16 @@ def export_images(self, output_folder): # create output folder source_folder = os.path.basename(os.path.dirname(list_file_names[0])) time_stamp = get_current_time_in_special_file_name_format() - full_output_folder_name = os.path.join(output_folder, f"{source_folder}_{time_stamp}") + full_output_folder_name = os.path.join( + output_folder, f"{source_folder}_{time_stamp}" + ) make_or_reset_folder(full_output_folder_name) for i, file_name in tqdm(enumerate(list_file_names)): short_file_name = os.path.basename(file_name) - full_output_file_name = os.path.join(full_output_folder_name, short_file_name) + full_output_file_name = os.path.join( + full_output_folder_name, short_file_name + ) _image = Image.fromarray(registered_crop_stack[i]) _image.save(full_output_file_name) @@ -309,10 +334,17 @@ def export_images(self, output_folder): "input folder": source_folder, "number of files": len(list_file_names), "crop": self.crop, - "registration": {"type": self.algo_options.value, "image of reference": self.reference_options.value}, + "registration": { + "type": self.algo_options.value, + "image of reference": self.reference_options.value, + }, } json_file_name = os.path.join(full_output_folder_name, "config.json") save_json(json_file_name, metadata) - self.output_label.value = f"DONE! (Registered files have been created in {full_output_folder_name})" - display(HTML(f"Registered files have been created in {full_output_folder_name}")) + self.output_label.value = ( + f"DONE! (Registered files have been created in {full_output_folder_name})" + ) + display( + HTML(f"Registered files have been created in {full_output_folder_name}") + ) diff --git a/notebooks/__code/integrated_roi_counts_vs_file_name_and_time_stamp.py b/notebooks/__code/integrated_roi_counts_vs_file_name_and_time_stamp.py index 72adf8015..1bc91e6a2 100755 --- a/notebooks/__code/integrated_roi_counts_vs_file_name_and_time_stamp.py +++ b/notebooks/__code/integrated_roi_counts_vs_file_name_and_time_stamp.py @@ -25,7 +25,9 @@ def _fromUtf8(s): from __code._utilities.color import Color from __code.decorators import wait_cursor from __code.file_handler import make_ascii_file, retrieve_time_stamp -from __code.ui_integrated_roi_counts_vs_file_name_and_time_stamp import Ui_MainWindow as UiMainWindow +from __code.ui_integrated_roi_counts_vs_file_name_and_time_stamp import ( + Ui_MainWindow as UiMainWindow, +) class IntegratedRoiUi(QMainWindow): @@ -145,7 +147,7 @@ def display_counts(self): profile_list = list() for _row in np.arange(nbr_row): [x_axis, profile] = self.get_profile(profile_roi_row=_row) - _label = f" ROI #{_row+1}" + _label = f" ROI #{_row + 1}" _color = list_rgb_profile_color[_row] self.ui.profile_view.plot(x_axis, profile, name=_label, pen=_color) @@ -166,7 +168,9 @@ def update_all_plots(self): nbr_profile = len(list_index_profile_selected) nbr_file_selected = len(list_index_file_selected) color = Color() - list_rgb_profile_color = color.get_list_rgb(nbr_color=(nbr_profile * nbr_file_selected)) + list_rgb_profile_color = color.get_list_rgb( + nbr_color=(nbr_profile * nbr_file_selected) + ) self.ui.all_plots_view.clear() if nbr_profile == 0: return @@ -180,10 +184,16 @@ def update_all_plots(self): for _color_index_file, _index_file in enumerate(list_index_file_selected): _data = self.data_dict["data"][_index_file] - for _color_index_profile, _index_profile in enumerate(list_index_profile_selected): + for _color_index_profile, _index_profile in enumerate( + list_index_profile_selected + ): legend = f"File #{_index_file} - Profile #{_index_profile}" - _color = list_rgb_profile_color[_color_index_file + _color_index_profile * nbr_file_selected] - [x_axis, y_axis] = self.get_profile(image=np.transpose(_data), profile_roi_row=_index_profile) + _color = list_rgb_profile_color[ + _color_index_file + _color_index_profile * nbr_file_selected + ] + [x_axis, y_axis] = self.get_profile( + image=np.transpose(_data), profile_roi_row=_index_profile + ) self.ui.all_plots_view.plot(x_axis, y_axis, name=legend, pen=_color) def display_image(self, recalculate_image=False): @@ -252,7 +262,9 @@ def is_row_enabled(self, row=-1): def update_guide_table_using_guide_rois(self): for _row, _roi in enumerate(self.list_guide_pyqt_roi): if self.is_row_enabled(row=_row): - region = _roi.getArraySlice(self.live_image, self.ui.image_view.imageItem) + region = _roi.getArraySlice( + self.live_image, self.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop @@ -320,7 +332,9 @@ def rename_all_plots_profiles_table(self): """rename all the profile name""" nbr_row = self.ui.tableWidget.rowCount() for _row in np.arange(nbr_row): - self.ui.all_plots_profiles_table.item(_row, 0).setText(f"Profile # {_row+1}") + self.ui.all_plots_profiles_table.item(_row, 0).setText( + f"Profile # {_row + 1}" + ) # setter def set_item_all_plots_profile_table(self, row=0): @@ -346,12 +360,16 @@ def set_item_all_plots_profile_table(self, row=0): def set_item_main_table(self, row=0, col=0, value=""): if col == 0: - spacerItem_left = QtGui.QSpacerItem(408, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) + spacerItem_left = QtGui.QSpacerItem( + 408, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding + ) widget = QtGui.QCheckBox() widget.blockSignals(True) self.list_table_widget_checkbox.insert(row, widget) widget.stateChanged.connect(self.guide_state_changed) - spacerItem_right = QtGui.QSpacerItem(408, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) + spacerItem_right = QtGui.QSpacerItem( + 408, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding + ) hori_layout = QtGui.QHBoxLayout() hori_layout.addItem(spacerItem_left) hori_layout.addWidget(widget) @@ -377,7 +395,9 @@ def get_profile_dimensions(self, row=-1): y_top = int(y0) y_bottom = int(y0) + int(height) - Profile = collections.namedtuple("Profile", ["x_left", "x_right", "y_top", "y_bottom"]) + Profile = collections.namedtuple( + "Profile", ["x_left", "x_right", "y_top", "y_bottom"] + ) result = Profile(x_left, x_right, y_top, y_bottom) return result @@ -403,7 +423,9 @@ def get_profile(self, profile_roi_row=-1): x_axis = np.arange(len(self.data_dict["file_name"])) for _data in self.data_dict["data"]: - _roi_counts = _data[y_top:y_bottom, x_left:x_right] # because pyqtgrpah display transpose images + _roi_counts = _data[ + y_top:y_bottom, x_left:x_right + ] # because pyqtgrpah display transpose images if inte_algo == "add": _counts = np.sum(_roi_counts) elif inte_algo == "mean": @@ -470,7 +492,9 @@ def highlight_guide_profile_pyqt_rois(self, row=-1): return try: - self._highlights_guide_profile_pyqt_roi(row=previous_active_row, status="deactivated") + self._highlights_guide_profile_pyqt_roi( + row=previous_active_row, status="deactivated" + ) self._highlights_guide_profile_pyqt_roi(row=row, status="activated") except: pass @@ -624,7 +648,10 @@ def profile_along_axis_changed(self): def export_button_clicked(self): _export_folder = QFileDialog.getExistingDirectory( - self, directory=self.working_dir, caption="Select Output Folder", options=QFileDialog.ShowDirsOnly + self, + directory=self.working_dir, + caption="Select Output Folder", + options=QFileDialog.ShowDirsOnly, ) if _export_folder: o_export = ExportProfiles(parent=self, export_folder=_export_folder) @@ -658,7 +685,10 @@ def __init__(self, parent=None, export_folder=""): def _create_output_file_name(self): base_name = os.path.basename(self.parent.working_dir) nbr_profile = self.parent.ui.tableWidget.rowCount() - output_file_name = os.path.join(self.export_folder, f"{base_name}_{nbr_profile}_integrated_counts_regions.txt") + output_file_name = os.path.join( + self.export_folder, + f"{base_name}_{nbr_profile}_integrated_counts_regions.txt", + ) return output_file_name def _create_metadata(self): @@ -675,7 +705,9 @@ def _create_metadata(self): x_right = profile_dimension.x_right y_top = profile_dimension.y_top y_bottom = profile_dimension.y_bottom - metadata.append(f"#ROI #{_profile_index}: [x0, y0, x1, y1] = [{x_left}, {y_top}, {x_right}, {y_bottom}]") + metadata.append( + f"#ROI #{_profile_index}: [x0, y0, x1, y1] = [{x_left}, {y_top}, {x_right}, {y_bottom}]" + ) axis.append(f"ROI #{_profile_index}") metadata.append("#") @@ -686,7 +718,9 @@ def _create_data(self, profile_index=0): all_profiles = [] x_axis = [] for _data in self.parent.data_dict["data"]: - [x_axis, profile] = self.parent.get_profile(image=np.transpose(_data), profile_roi_row=profile_index) + [x_axis, profile] = self.parent.get_profile( + image=np.transpose(_data), profile_roi_row=profile_index + ) all_profiles.append(list(profile)) data = [] @@ -710,7 +744,9 @@ def _format_data(self): _time_stamp = str(self.parent.ui.summary_table.item(_row, 1).text()) _formated_row_value = " ,".join(_row_value) - _formated_row = f"{_file_index}, {_file_name}, {_time_stamp}, " + _formated_row_value + _formated_row = ( + f"{_file_index}, {_file_name}, {_time_stamp}, " + _formated_row_value + ) _data.append(_formated_row) return _data @@ -721,7 +757,9 @@ def run(self): # create output file name _output_file_name = self._create_output_file_name() - make_ascii_file(metadata=metadata, data=data, output_file_name=_output_file_name, dim="1d") + make_ascii_file( + metadata=metadata, data=data, output_file_name=_output_file_name, dim="1d" + ) class GuideAndProfileRoisHandler: @@ -740,14 +778,19 @@ def add(self): self.parent.list_profile_pyqt_roi.insert(self.row, self.__profile) def update(self): - self.parent.ui.image_view.removeItem(self.parent.list_profile_pyqt_roi[self.row]) + self.parent.ui.image_view.removeItem( + self.parent.list_profile_pyqt_roi[self.row] + ) self.parent.list_profile_pyqt_roi[self.row] = self.__profile def _define_guide(self): """define the guide""" guide_roi = pg.RectROI( [self.parent.default_guide_roi["x0"], self.parent.default_guide_roi["y0"]], - [self.parent.default_guide_roi["width"], self.parent.default_guide_roi["height"]], + [ + self.parent.default_guide_roi["width"], + self.parent.default_guide_roi["height"], + ], pen=self.parent.default_guide_roi["color_activated"], ) guide_roi.addScaleHandle([1, 1], [0, 0]) @@ -770,15 +813,21 @@ def timestamp_dict(self): def table(self): # init the summary table list_files_full_name = self.parent.data_dict["file_name"] - list_files_short_name = [os.path.basename(_file) for _file in list_files_full_name] + list_files_short_name = [ + os.path.basename(_file) for _file in list_files_full_name + ] list_time_stamp = self.parent.timestamp_dict["list_time_stamp"] - list_time_stamp_user_format = self.parent.timestamp_dict["list_time_stamp_user_format"] + list_time_stamp_user_format = self.parent.timestamp_dict[ + "list_time_stamp_user_format" + ] time_0 = list_time_stamp[0] for _row, _file in enumerate(list_files_short_name): self.parent.ui.summary_table.insertRow(_row) self.set_item_summary_table(row=_row, col=0, value=_file) - self.set_item_summary_table(row=_row, col=1, value=list_time_stamp_user_format[_row]) + self.set_item_summary_table( + row=_row, col=1, value=list_time_stamp_user_format[_row] + ) _offset = list_time_stamp[_row] - time_0 self.set_item_summary_table(row=_row, col=2, value=f"{_offset:0.2f}") @@ -789,7 +838,9 @@ def parameters(self): self.parent.default_guide_roi["height"] = int(height / 5) self.parent.default_guide_roi["x0"] = int(width / 2) self.parent.default_guide_roi["y0"] = int(height / 2) - self.parent.default_profile_width_values = [str(_value) for _value in self.parent.default_profile_width_values] + self.parent.default_profile_width_values = [ + str(_value) for _value in self.parent.default_profile_width_values + ] def widgets(self): self.parent.ui.splitter_2.setSizes([250, 50]) @@ -801,7 +852,9 @@ def widgets(self): # update size of summary table nbr_columns = self.parent.ui.summary_table.columnCount() for _col in range(nbr_columns): - self.parent.ui.summary_table.setColumnWidth(_col, self.parent.summary_table_width[_col]) + self.parent.ui.summary_table.setColumnWidth( + _col, self.parent.summary_table_width[_col] + ) self.parent.display_ui = [ self.parent.ui.display_size_label, @@ -852,7 +905,8 @@ def get_image_selected(self, recalculate_image=False): angle = self.parent.rotation_angle # rotate all images self.parent.data_dict["data"] = [ - transform.rotate(_image, angle) for _image in self.parent.data_dict_raw["data"] + transform.rotate(_image, angle) + for _image in self.parent.data_dict_raw["data"] ] _image = self.parent.data_dict["data"][slider_index] @@ -876,7 +930,9 @@ def display_images(self): _view_box.setState(_state) if not first_update: - _histo_widget.setLevels(self.parent.histogram_level[0], self.parent.histogram_level[1]) + _histo_widget.setLevels( + self.parent.histogram_level[0], self.parent.histogram_level[1] + ) def calculate_matrix_grid(self, grid_size=1, height=1, width=1): """calculate the matrix that defines the vertical and horizontal lines @@ -926,12 +982,17 @@ def display_grid(self): grid_size = self.parent.ui.grid_size_slider.value() [width, height] = np.shape(self.parent.live_image) - pos_adj_dict = self.calculate_matrix_grid(grid_size=grid_size, height=height, width=width) + pos_adj_dict = self.calculate_matrix_grid( + grid_size=grid_size, height=height, width=width + ) pos = pos_adj_dict["pos"] adj = pos_adj_dict["adj"] line_color = self.parent.grid_view["color"] - _transparency_value = 255 - (float(str(self.parent.ui.transparency_slider.value())) / 100) * 255 + _transparency_value = ( + 255 + - (float(str(self.parent.ui.transparency_slider.value())) / 100) * 255 + ) _list_line_color = list(line_color) _list_line_color[3] = _transparency_value line_color = tuple(_list_line_color) diff --git a/notebooks/__code/ipywe/_version.py b/notebooks/__code/ipywe/_version.py index c2e986222..f99b78f7b 100755 --- a/notebooks/__code/ipywe/_version.py +++ b/notebooks/__code/ipywe/_version.py @@ -6,7 +6,9 @@ version_info[0], version_info[1], version_info[2], - "" if version_info[3] == "final" else _specifier_[version_info[3]] + str(version_info[4]), + "" + if version_info[3] == "final" + else _specifier_[version_info[3]] + str(version_info[4]), ) __frontend_version__ = "^0.1.3-alpha.2" diff --git a/notebooks/__code/ipywe/example.py b/notebooks/__code/ipywe/example.py index b87da7a47..194887af1 100755 --- a/notebooks/__code/ipywe/example.py +++ b/notebooks/__code/ipywe/example.py @@ -28,7 +28,9 @@ class HelloWorld(widgets.DOMWidget): def get_js(): import os - js = open(os.path.join(os.path.dirname(__file__), "..", "js", "src", "example.js")).read() + js = open( + os.path.join(os.path.dirname(__file__), "..", "js", "src", "example.js") + ).read() return js.decode("UTF-8") def run_js(): diff --git a/notebooks/__code/ipywe/fileselector.py b/notebooks/__code/ipywe/fileselector.py index a72db5188..1c505713a 100755 --- a/notebooks/__code/ipywe/fileselector.py +++ b/notebooks/__code/ipywe/fileselector.py @@ -21,11 +21,18 @@ class FileSelectorPanel: # statement should change the width of the file selector. "width=" # doesn't appear to work in earlier versions. select_layout = ipyw.Layout(width="99%", height="260px") - select_multiple_layout = ipyw.Layout(width="99%", height="360px") # , display="flex", flex_flow="column") + select_multiple_layout = ipyw.Layout( + width="99%", height="360px" + ) # , display="flex", flex_flow="column") button_layout = ipyw.Layout(margin="5px 40px", border="1px solid gray") - toolbar_button_layout = ipyw.Layout(margin="5px 10px", width="100px", border="1px solid gray") + toolbar_button_layout = ipyw.Layout( + margin="5px 10px", width="100px", border="1px solid gray" + ) toolbar_box_layout = ipyw.Layout( - border="1px solid lightgrey", padding="3px", margin="5px 50px 5px 5px", width="100%" + border="1px solid lightgrey", + padding="3px", + margin="5px 50px 5px 5px", + width="100%", ) label_layout = ipyw.Layout(width="100%") layout = ipyw.Layout() @@ -136,7 +143,9 @@ def createBody(self, curdir): left_vbox = ipyw.VBox(left_widgets, layout=ipyw.Layout(width="80%")) # right # change directory button - self.changedir = ipyw.Button(description="Change directory", layout=self.button_layout) + self.changedir = ipyw.Button( + description="Change directory", layout=self.button_layout + ) self.changedir.on_click(self.handle_changedir) # select button ok_layout = cloneLayout(self.button_layout) @@ -147,7 +156,9 @@ def createBody(self, curdir): right_vbox = ipyw.VBox(children=[self.changedir, self.ok]) select_panel = ipyw.HBox( children=[left_vbox, right_vbox], - layout=ipyw.Layout(border="1px solid lightgrey", margin="5px", padding="10px"), + layout=ipyw.Layout( + border="1px solid lightgrey", margin="5px", padding="10px" + ), ) body = ipyw.VBox(children=[toolbar, select_panel], layout=self.layout) self.footer.value = "" @@ -158,20 +169,34 @@ def createToolbar(self): # "jump to" curdir = self.curdir self.jumpto_input = jumpto_input = ipyw.Text( - value=curdir, placeholder="", description="Location: ", layout=ipyw.Layout(width="100%") + value=curdir, + placeholder="", + description="Location: ", + layout=ipyw.Layout(width="100%"), + ) + jumpto_button = ipyw.Button( + description="Jump", layout=self.toolbar_button_layout ) - jumpto_button = ipyw.Button(description="Jump", layout=self.toolbar_button_layout) jumpto_button.on_click(self.handle_jumpto) - jumpto = ipyw.HBox(children=[jumpto_input, jumpto_button], layout=self.toolbar_box_layout) + jumpto = ipyw.HBox( + children=[jumpto_input, jumpto_button], layout=self.toolbar_box_layout + ) self.jumpto_button = jumpto_button if self.newdir_toolbar_button: # "new dir" self.newdir_input = newdir_input = ipyw.Text( - value="", placeholder="new dir name", description="New subdir: ", layout=ipyw.Layout(width="180px") + value="", + placeholder="new dir name", + description="New subdir: ", + layout=ipyw.Layout(width="180px"), + ) + newdir_button = ipyw.Button( + description="Create", layout=self.toolbar_button_layout ) - newdir_button = ipyw.Button(description="Create", layout=self.toolbar_button_layout) newdir_button.on_click(self.handle_newdir) - newdir = ipyw.HBox(children=[newdir_input, newdir_button], layout=self.toolbar_box_layout) + newdir = ipyw.HBox( + children=[newdir_input, newdir_button], layout=self.toolbar_box_layout + ) toolbar = ipyw.HBox(children=[jumpto, newdir]) else: toolbar = ipyw.HBox(children=[jumpto]) @@ -185,7 +210,9 @@ def getEntries(self): entries_files = self.getFilteredEntries() else: entries_files = sorted(os.listdir(curdir)) - entries_files = [_f for _f in entries_files if (self.searching_string in _f)] + entries_files = [ + _f for _f in entries_files if (self.searching_string in _f) + ] # # if self.sort_by_alphabetical: @@ -212,11 +239,19 @@ def createSelectWidget(self): if self.multiple: value = [] self.select = ipyw.SelectMultiple( - value=value, options=entries, description="Select", layout=self.select_multiple_layout + value=value, + options=entries, + description="Select", + layout=self.select_multiple_layout, ) else: value = entries[0] - self.select = ipyw.Select(value=value, options=entries, description="Select", layout=self.select_layout) + self.select = ipyw.Select( + value=value, + options=entries, + description="Select", + layout=self.select_layout, + ) """When ipywidgets 7.0 is released, the old way that the select or select multiple widget was set up (see below) should work so long as self.select_layout is changed to include the display="flex" and flex_flow="column" statements. In ipywidgets 6.0, @@ -231,13 +266,17 @@ def createSelectWidget(self): def createSearchWidget(self): label = ipyw.Label("Search:") - self.search_text = ipyw.Text(self.searching_string, layout=ipyw.Layout(width="50%")) + self.search_text = ipyw.Text( + self.searching_string, layout=ipyw.Layout(width="50%") + ) self.search_text.on_submit(self.handle_search_changed) elements = [label, self.search_text] if self.filter_widget: elements.append(self.filter_widget) - self.search = ipyw.HBox(elements, layout=ipyw.Layout(justify_content="flex-end")) + self.search = ipyw.HBox( + elements, layout=ipyw.Layout(justify_content="flex-end") + ) def handle_search_changed(self, sender): self.searching_string = sender.value.strip() @@ -249,7 +288,9 @@ def createFilterWidget(self): self.filters.update(All=["*.*"]) self.cur_filter = self.cur_filter or self.filters[self.default_filter or "All"] self.filter_widget = ipyw.Dropdown( - options=self.filters, value=self.cur_filter, layout=ipyw.Layout(align_self="flex-end", width="15%") + options=self.filters, + value=self.cur_filter, + layout=ipyw.Layout(align_self="flex-end", width="15%"), ) self.filter_widget.observe(self.handle_filter_changed, names="value") return @@ -266,7 +307,9 @@ def getFilteredEntries(self): # filter out dirs, they will be added below list_files = filter(lambda o: not os.path.isdir(o), list_files) list_files = list(map(os.path.basename, list_files)) - list_dirs = [o for o in os.listdir(curdir) if os.path.isdir(os.path.join(curdir, o))] + list_dirs = [ + o for o in os.listdir(curdir) if os.path.isdir(os.path.join(curdir, o)) + ] self.footer.value += "

" + " ".join(list_dirs) + "

" entries = list_dirs + list_files entries.sort() diff --git a/notebooks/__code/ipywe/imagedisplay.py b/notebooks/__code/ipywe/imagedisplay.py index 61201fad9..774c63dfc 100755 --- a/notebooks/__code/ipywe/imagedisplay.py +++ b/notebooks/__code/ipywe/imagedisplay.py @@ -59,7 +59,11 @@ def __init__(self, image, width, height, init_roi=None): def createImg(self): if self._img_min >= self._img_max: self._img_max = self._img_min + abs(self._img_max - self._img_min) * 1e-5 - img = ((self.curr_img_data - self._img_min) / (self._img_max - self._img_min) * (2**8 - 1)).astype("uint8") + img = ( + (self.curr_img_data - self._img_min) + / (self._img_max - self._img_min) + * (2**8 - 1) + ).astype("uint8") size = np.max(img.shape) view_size = np.max((self.width, self.height)) if size > view_size: @@ -129,7 +133,9 @@ def zoomImg(self, change): self._extracols = 0 extrarows_top = np.full((addtop, self._ncols), 1) extrarows_bottom = np.full((addbottom, self._ncols), 1) - self.curr_img_data = np.vstack((extrarows_top, self.curr_img_data, extrarows_bottom)) + self.curr_img_data = np.vstack( + (extrarows_top, self.curr_img_data, extrarows_bottom) + ) else: diff = self._nrows - self._ncols if diff % 2 == 0: @@ -146,9 +152,15 @@ def zoomImg(self, change): self._extracols = diff extrarows_left = np.full((self._nrows, addleft), 1) extrarows_right = np.full((self._nrows, addright), 1) - self.curr_img_data = np.hstack((extrarows_left, self.curr_img_data, extrarows_right)) - self._xcoord_max_roi = self._xcoord_absolute + self._ncols_currimg - self._extracols - self._ycoord_max_roi = self._ycoord_absolute + self._nrows_currimg - self._extrarows + self.curr_img_data = np.hstack( + (extrarows_left, self.curr_img_data, extrarows_right) + ) + self._xcoord_max_roi = ( + self._xcoord_absolute + self._ncols_currimg - self._extracols + ) + self._ycoord_max_roi = ( + self._ycoord_absolute + self._nrows_currimg - self._extrarows + ) self._b64value = self.createImg() return diff --git a/notebooks/__code/ipywe/imageslider.py b/notebooks/__code/ipywe/imageslider.py index 3b56dead0..0444444f8 100755 --- a/notebooks/__code/ipywe/imageslider.py +++ b/notebooks/__code/ipywe/imageslider.py @@ -163,7 +163,11 @@ def getimg_bytes(self): # apply intensity range self.curr_img_data[self.curr_img_data < self._img_min] = self._img_min self.curr_img_data[self.curr_img_data > self._img_max] = self._img_max - img = ((self.curr_img_data - self._img_min) / (self._img_max - self._img_min) * (2**8 - 1)).astype("uint8") + img = ( + (self.curr_img_data - self._img_min) + / (self._img_max - self._img_min) + * (2**8 - 1) + ).astype("uint8") size = np.max(img.shape) view_size = np.max((self.width, self.height)) # resample if necessary @@ -323,7 +327,9 @@ def update_image_div_data_with_zoom(self): self._extracols = 0 extrarows_top = np.full((addtop, self._ncols), 1) extrarows_bottom = np.full((addbottom, self._ncols), 1) - self.curr_img_data = np.vstack((extrarows_top, self.curr_img_data, extrarows_bottom)) + self.curr_img_data = np.vstack( + (extrarows_top, self.curr_img_data, extrarows_bottom) + ) else: diff = self._nrows - self._ncols if diff % 2 == 0: @@ -340,8 +346,14 @@ def update_image_div_data_with_zoom(self): self._extracols = diff extrarows_left = np.full((self._nrows, addleft), 1) extrarows_right = np.full((self._nrows, addright), 1) - self.curr_img_data = np.hstack((extrarows_left, self.curr_img_data, extrarows_right)) - self._xcoord_max_roi = self._xcoord_absolute + self._ncols_currimg - self._extracols - self._ycoord_max_roi = self._ycoord_absolute + self._nrows_currimg - self._extrarows + self.curr_img_data = np.hstack( + (extrarows_left, self.curr_img_data, extrarows_right) + ) + self._xcoord_max_roi = ( + self._xcoord_absolute + self._ncols_currimg - self._extracols + ) + self._ycoord_max_roi = ( + self._ycoord_absolute + self._nrows_currimg - self._extrarows + ) self._b64value = self.getimg_bytes() return diff --git a/notebooks/__code/ipywe/imgdatagraph.py b/notebooks/__code/ipywe/imgdatagraph.py index e07f54dc6..51f617852 100755 --- a/notebooks/__code/ipywe/imgdatagraph.py +++ b/notebooks/__code/ipywe/imgdatagraph.py @@ -63,7 +63,10 @@ def __init__(self, image, width, height, uformat="png"): self.height = height self._format = uformat self._nrows, self._ncols = self.img_data.shape - self._img_min, self._img_max = int(np.min(self.img_data)), int(np.max(self.img_data)) + self._img_min, self._img_max = ( + int(np.min(self.img_data)), + int(np.max(self.img_data)), + ) self._b64value = self.getimg_bytes() super(ImageDataGraph, self).__init__() return @@ -71,7 +74,11 @@ def __init__(self, image, width, height, uformat="png"): def getimg_bytes(self): """Encodes the image's data into Base64.""" - img = ((self.img_data - self._img_min) / (self._img_max - self._img_min) * (2**8 - 1)).astype("uint8") + img = ( + (self.img_data - self._img_min) + / (self._img_max - self._img_min) + * (2**8 - 1) + ).astype("uint8") size = np.max(img.shape) view_size = np.max((self.width, self.height)) if size > view_size: @@ -215,7 +222,9 @@ def width_graph(self): elif p1y_abs != p2y_abs and p1x_abs == p2x_abs: dists, vals, bar_width = self.get_data_vertical(p1x_abs, p1y_abs, p2y_abs) else: - dists, vals, bar_width = self.get_data_diagonal(p1x_abs, p1y_abs, p2x_abs, p2y_abs) + dists, vals, bar_width = self.get_data_diagonal( + p1x_abs, p1y_abs, p2x_abs, p2y_abs + ) plt.bar(dists, vals, width=bar_width) plt.xlabel("Distance from Initial Point") plt.ylabel("Value") @@ -277,7 +286,9 @@ def get_data_horizontal(self, x_init, y_init, x_fin): ind = bins.index(b) if ind < len(bins) - 1: if x >= b + x0 and x < bins[ind + 1] + x0: - intensities[ind] = intensities[ind] + self.img_data[int(y), int(x)] + intensities[ind] = ( + intensities[ind] + self.img_data[int(y), int(x)] + ) num_binvals[ind] = num_binvals[ind] + 1 break for val, num in np.nditer([intensities, num_binvals]): @@ -328,7 +339,9 @@ def get_data_vertical(self, x_init, y_init, y_fin): ind = bins.index(b) if ind < len(bins) - 1: if y >= b + y0 and y < bins[ind + 1] + y0: - intensities[ind] = intensities[ind] + self.img_data[int(y), int(x)] + intensities[ind] = ( + intensities[ind] + self.img_data[int(y), int(x)] + ) num_binvals[ind] = num_binvals[ind] + 1 break for val, num in np.nditer([intensities, num_binvals]): @@ -413,7 +426,9 @@ def get_data_diagonal(self, x_init, y_init, x_fin, y_fin): ind = bin_borders.index(b) if ind < len(bin_borders) - 1: if p >= b and p < bin_borders[ind + 1]: - intensities[ind] = intensities[ind] + self.img_data[int(y), int(x)] + intensities[ind] = ( + intensities[ind] + self.img_data[int(y), int(x)] + ) num_binvals[ind] = num_binvals[ind] + 1 break for i, n in np.nditer([intensities, num_binvals]): diff --git a/notebooks/__code/ipywe/myfileselector.py b/notebooks/__code/ipywe/myfileselector.py index 8b2afab0a..120817e2f 100755 --- a/notebooks/__code/ipywe/myfileselector.py +++ b/notebooks/__code/ipywe/myfileselector.py @@ -17,11 +17,18 @@ class MyFileSelectorPanel: # statement should change the width of the file selector. "width=" # doesn't appear to work in earlier versions. select_layout = ipyw.Layout(width="99%", height="260px") - select_multiple_layout = ipyw.Layout(width="99%", height="260px", display="flex", flex_flow="column") + select_multiple_layout = ipyw.Layout( + width="99%", height="260px", display="flex", flex_flow="column" + ) button_layout = ipyw.Layout(margin="5px 40px", border="1px solid gray") - toolbar_button_layout = ipyw.Layout(margin="5px 10px", width="100px", border="1px solid gray") + toolbar_button_layout = ipyw.Layout( + margin="5px 10px", width="100px", border="1px solid gray" + ) toolbar_box_layout = ipyw.Layout( - border="1px solid lightgrey", padding="3px", margin="5px 50px 5px 5px", width="100%" + border="1px solid lightgrey", + padding="3px", + margin="5px 50px 5px 5px", + width="100%", ) label_layout = ipyw.Layout(width="250px") layout = ipyw.Layout() @@ -99,20 +106,34 @@ def createBody(self, curdir): # toolbar on the top # "jump to" self.jumpto_input = jumpto_input = ipyw.Text( - value=curdir, placeholder="", description="Location: ", layout=ipyw.Layout(width="100%") + value=curdir, + placeholder="", + description="Location: ", + layout=ipyw.Layout(width="100%"), + ) + jumpto_button = ipyw.Button( + description="Jump", layout=self.toolbar_button_layout ) - jumpto_button = ipyw.Button(description="Jump", layout=self.toolbar_button_layout) jumpto_button.on_click(self.handle_jumpto) - jumpto = ipyw.HBox(children=[jumpto_input, jumpto_button], layout=self.toolbar_box_layout) + jumpto = ipyw.HBox( + children=[jumpto_input, jumpto_button], layout=self.toolbar_box_layout + ) self.jumpto_button = jumpto_button if self.newdir_toolbar_button: # "new dir" self.newdir_input = newdir_input = ipyw.Text( - value="", placeholder="new dir name", description="New subdir: ", layout=ipyw.Layout(width="180px") + value="", + placeholder="new dir name", + description="New subdir: ", + layout=ipyw.Layout(width="180px"), + ) + newdir_button = ipyw.Button( + description="Create", layout=self.toolbar_button_layout ) - newdir_button = ipyw.Button(description="Create", layout=self.toolbar_button_layout) newdir_button.on_click(self.handle_newdir) - newdir = ipyw.HBox(children=[newdir_input, newdir_button], layout=self.toolbar_box_layout) + newdir = ipyw.HBox( + children=[newdir_input, newdir_button], layout=self.toolbar_box_layout + ) toolbar = ipyw.HBox(children=[jumpto, newdir]) else: toolbar = ipyw.HBox(children=[jumpto]) @@ -137,11 +158,19 @@ def createBody(self, curdir): if self.multiple: value = [] self.select = ipyw.SelectMultiple( - value=value, options=entries, description="Select", layout=self.select_multiple_layout + value=value, + options=entries, + description="Select", + layout=self.select_multiple_layout, ) else: value = entries[0] - self.select = ipyw.Select(value=value, options=entries, description="Select", layout=self.select_layout) + self.select = ipyw.Select( + value=value, + options=entries, + description="Select", + layout=self.select_layout, + ) """When ipywidgets 7.0 is released, the old way that the select or select multiple widget was set up (see below) should work so long as self.select_layout is changed to include the display="flex" and flex_flow="column" statements. In ipywidgets 6.0, @@ -169,7 +198,9 @@ def createBody(self, curdir): left_vbox = ipyw.VBox(left_widgets, layout=ipyw.Layout(width="80%")) # right # change directory button - self.changedir = ipyw.Button(description="Change directory", layout=self.button_layout) + self.changedir = ipyw.Button( + description="Change directory", layout=self.button_layout + ) self.changedir.on_click(self.handle_changedir) # select button ok_layout = cloneLayout(self.button_layout) @@ -180,7 +211,9 @@ def createBody(self, curdir): right_vbox = ipyw.VBox(children=[self.changedir, self.ok]) select_panel = ipyw.HBox( children=[left_vbox, right_vbox], - layout=ipyw.Layout(border="1px solid lightgrey", margin="5px", padding="10px"), + layout=ipyw.Layout( + border="1px solid lightgrey", margin="5px", padding="10px" + ), ) body = ipyw.VBox(children=[toolbar, select_panel], layout=self.layout) self.footer.value = "" @@ -191,7 +224,9 @@ def createFilterWidget(self): self.filters.update(All=["*.*"]) self.cur_filter = self.cur_filter or self.filters[self.default_filter or "All"] self.filter_widget = ipyw.Dropdown( - options=self.filters, value=self.cur_filter, layout=ipyw.Layout(align_self="flex-end", width="15%") + options=self.filters, + value=self.cur_filter, + layout=ipyw.Layout(align_self="flex-end", width="15%"), ) self.filter_widget.observe(self.handle_filter_changed, names="value") return @@ -207,7 +242,9 @@ def getFilteredEntries(self): # filter out dirs, they will be added below list_files = filter(lambda o: not os.path.isdir(o), list_files) list_files = list(map(os.path.basename, list_files)) - list_dirs = [o for o in os.listdir(curdir) if os.path.isdir(os.path.join(curdir, o))] + list_dirs = [ + o for o in os.listdir(curdir) if os.path.isdir(os.path.join(curdir, o)) + ] self.footer.value += "

" + " ".join(list_dirs) + "

" entries = list_dirs + list_files return entries @@ -418,7 +455,15 @@ def del_ftime(file_label): class FileSelection: next = None - def __init__(self, working_dir="./", filter="", default_filter=None, next=None, instructions=None, multiple=True): + def __init__( + self, + working_dir="./", + filter="", + default_filter=None, + next=None, + instructions=None, + multiple=True, + ): self.working_dir = working_dir self.instuctions = instructions self.filter = filter @@ -431,7 +476,9 @@ def __init__(self, working_dir="./", filter="", default_filter=None, next=None, def select_file_help(self, value): import webbrowser - webbrowser.open("https://neutronimaging.pages.ornl.gov/en/tutorial/notebooks/file_selector/#select_profile") + webbrowser.open( + "https://neutronimaging.pages.ornl.gov/en/tutorial/notebooks/file_selector/#select_profile" + ) def load_files(self, files): o_norm = Normalization() @@ -471,7 +518,10 @@ def select_data(self, check_shape=True): ) else: self.files_ui = fileselector.FileSelectorPanel( - instruction=instructions, start_dir=self.working_dir, next=next, multiple=self.multiple + instruction=instructions, + start_dir=self.working_dir, + next=next, + multiple=self.multiple, ) else: @@ -487,7 +537,10 @@ def select_data(self, check_shape=True): ) else: self.files_ui = fileselector.FileSelectorPanel( - instruction=instructions, start_dir=self.working_dir, next=next, multiple=self.multiple + instruction=instructions, + start_dir=self.working_dir, + next=next, + multiple=self.multiple, ) self.files_ui.show() @@ -554,14 +607,18 @@ def display_file_selector_from_home(ev): list_buttons = [] if show_jump_to_share: share_button = widgets.Button( - description=f"Jump to {ipts} Shared Folder", button_style="success", layout=button_layout + description=f"Jump to {ipts} Shared Folder", + button_style="success", + layout=button_layout, ) share_button.on_click(display_file_selector_from_shared) list_buttons.append(share_button) if show_jump_to_home: home_button = widgets.Button( - description="Jump to My Home Folder", button_style="success", layout=button_layout + description="Jump to My Home Folder", + button_style="success", + layout=button_layout, ) home_button.on_click(display_file_selector_from_home) list_buttons.append(home_button) diff --git a/notebooks/__code/list_metadata_and_time_with_oncat.py b/notebooks/__code/list_metadata_and_time_with_oncat.py index 9acbe0a0f..68f9f9244 100755 --- a/notebooks/__code/list_metadata_and_time_with_oncat.py +++ b/notebooks/__code/list_metadata_and_time_with_oncat.py @@ -21,17 +21,31 @@ def __init__(self): self.oncat_session = _oncat.authentication() if self.oncat_session is None: - display(HTML('Wrong Password!')) + display( + HTML('Wrong Password!') + ) else: - display(HTML('Valid Password!')) + display( + HTML( + 'Valid Password!' + ) + ) def select_metadata(self, system=None, list_of_files=[]): if not list_of_files: - display(HTML('You need to select at least one file!')) + display( + HTML( + 'You need to select at least one file!' + ) + ) return if not system: - display(HTML('No input folder selected!')) + display( + HTML( + 'No input folder selected!' + ) + ) return self.instrument = system.System.get_instrument_selected() @@ -40,12 +54,19 @@ def select_metadata(self, system=None, list_of_files=[]): self.first_file = list_of_files[0] self.list_metadata_with_examples = self.retrieve_list_metadata_with_examples() - display(HTML('CTRL + Click to select multiple rows!')) + display( + HTML( + 'CTRL + Click to select multiple rows!' + ) + ) box1 = widgets.HBox( [ - widgets.Label("Select Metadata To Retrieve", layout=widgets.Layout(width="20%")), + widgets.Label( + "Select Metadata To Retrieve", layout=widgets.Layout(width="20%") + ), widgets.SelectMultiple( - options=self.list_metadata_with_examples, layout=widgets.Layout(width="80%", height="100%") + options=self.list_metadata_with_examples, + layout=widgets.Layout(width="80%", height="100%"), ), ], layout=widgets.Layout(height="500px"), @@ -56,7 +77,9 @@ def select_metadata(self, system=None, list_of_files=[]): def retrieve_list_metadata_with_examples(self): list_metadata = self.retrieve_list_metadata() raw_data = self.raw_oncat_metadata - list_metadata_with_examples = ListMetadata.format_list_metadata_with_examples(list_metadata, raw_data) + list_metadata_with_examples = ListMetadata.format_list_metadata_with_examples( + list_metadata, raw_data + ) return list_metadata_with_examples @staticmethod @@ -69,7 +92,10 @@ def format_list_metadata_with_examples(list_metadata, raw_data): def retrieve_list_metadata(self): _data = oncat.GetEverything( - instrument=self.instrument, facility=self.facility, run=self.first_file, oncat=self.oncat_session + instrument=self.instrument, + facility=self.facility, + run=self.first_file, + oncat=self.oncat_session, ) self.raw_oncat_data = _data.datafiles @@ -89,7 +115,9 @@ def create_sorted_dict_metadata(self, _data): def export_ascii(self, output_folder): list_files = self.list_of_files projection = self.create_projection() - output_ascii_file_name = ListMetadata.create_output_ascii_name(list_files, output_folder) + output_ascii_file_name = ListMetadata.create_output_ascii_name( + list_files, output_folder + ) o_metadata_selected = oncat.GetProjection( instrument=self.instrument, @@ -103,7 +131,12 @@ def export_ascii(self, output_folder): name_metadata = self.create_metadata_name_row() value_metadata = self.create_metadata_value_rows(list_files, metadata_selected) - make_ascii_file(metadata=name_metadata, data=value_metadata, output_file_name=output_ascii_file_name, dim="1d") + make_ascii_file( + metadata=name_metadata, + data=value_metadata, + output_file_name=output_ascii_file_name, + dim="1d", + ) print("Done!") display( HTML( @@ -116,10 +149,14 @@ def export_ascii(self, output_folder): def create_metadata_value_rows(self, list_files, metadata_selected): value_metadata = [] for _file in list_files: - time_stamp = self.unify_timestamp_format(metadata_selected[_file]["ingested"]) + time_stamp = self.unify_timestamp_format( + metadata_selected[_file]["ingested"] + ) _metadata = [] for _metadata_name in self.get_list_metadata_selected(): - _metadata.append(str(metadata_selected[_file]["metadata"][_metadata_name])) + _metadata.append( + str(metadata_selected[_file]["metadata"][_metadata_name]) + ) row_string = "{}, {}, {}".format(_file, time_stamp, ", ".join(_metadata)) value_metadata.append(row_string) return value_metadata @@ -130,12 +167,17 @@ def unify_timestamp_format(self, old_timestamp): return new_timestamp def create_metadata_name_row(self): - name_metadata = ["#filename, timestamp_user_format, " + ", ".join(self.get_list_metadata_selected())] + name_metadata = [ + "#filename, timestamp_user_format, " + + ", ".join(self.get_list_metadata_selected()) + ] return name_metadata @staticmethod def create_output_ascii_name(list_files, output_folder): - output_ascii_file_name = os.path.basename(os.path.dirname(list_files[0]) + "_metadata_report_from_oncat.txt") + output_ascii_file_name = os.path.basename( + os.path.dirname(list_files[0]) + "_metadata_report_from_oncat.txt" + ) output_folder = os.path.abspath(output_folder) return os.path.join(output_folder, output_ascii_file_name) diff --git a/notebooks/__code/load_images.py b/notebooks/__code/load_images.py index c1ea5cdd7..6abc542e2 100755 --- a/notebooks/__code/load_images.py +++ b/notebooks/__code/load_images.py @@ -37,7 +37,10 @@ def select_images(self, use_next=False, virtual_load=False): # display(HTML('Select the images you want to work on!')) self.list_images_ui = fileselector.FileSelectorPanel( - instruction="Select Images...", multiple=True, next=next, start_dir=self.working_dir + instruction="Select Images...", + multiple=True, + next=next, + start_dir=self.working_dir, ) self.list_images_ui.show() diff --git a/notebooks/__code/match_images_shapes/load_images.py b/notebooks/__code/match_images_shapes/load_images.py index 6ca060f35..9d43e33a9 100755 --- a/notebooks/__code/match_images_shapes/load_images.py +++ b/notebooks/__code/match_images_shapes/load_images.py @@ -27,9 +27,16 @@ def select_images(self, use_next=False): next = self.load_images else: next = None - display(HTML('Select the images you want to work on!')) + display( + HTML( + 'Select the images you want to work on!' + ) + ) self.list_images_ui = fileselector.FileSelectorPanel( - instruction="Select Images...", multiple=True, next=next, start_dir=self.working_dir + instruction="Select Images...", + multiple=True, + next=next, + start_dir=self.working_dir, ) self.list_images_ui.show() diff --git a/notebooks/__code/match_images_shapes/main.py b/notebooks/__code/match_images_shapes/main.py index 185b8be32..8e8bbd1dd 100755 --- a/notebooks/__code/match_images_shapes/main.py +++ b/notebooks/__code/match_images_shapes/main.py @@ -33,7 +33,10 @@ def display_available_shapes(self): self.dict_shapes[f"{_height}, {_width}"] = _shape vertical_layout = widgets.VBox( - [widgets.Label("Available shapes (height, width)"), widgets.RadioButtons(options=self.dict_shapes.keys())] + [ + widgets.Label("Available shapes (height, width)"), + widgets.RadioButtons(options=self.dict_shapes.keys()), + ] ) display(vertical_layout) self.shape_dropdown_ui = vertical_layout.children[1] @@ -46,7 +49,10 @@ def select_output_folder(self): ) self.output_folder_ui = fileselector.FileSelectorPanel( - instruction="Select Output Folder ...", start_dir=self.working_dir, type="directory", next=self.export + instruction="Select Output Folder ...", + start_dir=self.working_dir, + type="directory", + next=self.export, ) self.output_folder_ui.show() @@ -60,7 +66,9 @@ def export(self, output_folder): format_selected = self.shape_dropdown_ui.value height, width = self.dict_shapes[format_selected] - new_output_folder = os.path.join(output_folder, f"{source_folder_name}_height{height}px_width{width}px") + new_output_folder = os.path.join( + output_folder, f"{source_folder_name}_height{height}px_width{width}px" + ) make_or_reset_folder(new_output_folder) @@ -89,4 +97,10 @@ def export(self, output_folder): w.value = _index + 1 w.close() - display(HTML('Images created in ' + new_output_folder + "")) + display( + HTML( + 'Images created in ' + + new_output_folder + + "" + ) + ) diff --git a/notebooks/__code/math_images.py b/notebooks/__code/math_images.py index 21bc8be12..7746a0238 100755 --- a/notebooks/__code/math_images.py +++ b/notebooks/__code/math_images.py @@ -17,18 +17,24 @@ def __init__(self, working_dir=""): def select_files(self): self.files_list_widget = fileselector.FileSelectorPanel( - instruction="select images to operate on", start_dir=self.working_dir, multiple=True + instruction="select images to operate on", + start_dir=self.working_dir, + multiple=True, ) self.files_list_widget.show() def select_target_image(self): self.target_file = fileselector.FileSelectorPanel( - instruction="select images to use in operation", start_dir=self.working_dir, multiple=False + instruction="select images to use in operation", + start_dir=self.working_dir, + multiple=False, ) self.target_file.show() def which_math(self): - self.math_method = widgets.RadioButtons(options=["substract", "add"], value="substract") + self.math_method = widgets.RadioButtons( + options=["substract", "add"], value="substract" + ) display(self.math_method) def recap(self): diff --git a/notebooks/__code/mcp_chips_corrector/alignment.py b/notebooks/__code/mcp_chips_corrector/alignment.py index bf59db860..37b095c65 100755 --- a/notebooks/__code/mcp_chips_corrector/alignment.py +++ b/notebooks/__code/mcp_chips_corrector/alignment.py @@ -3,7 +3,12 @@ import numpy as np from scipy import interpolate -from __code.mcp_chips_corrector import CHIP_CORRECTION, CHIP_GAP, MCP_LOW_MODE, NBR_OF_EDGES_PIXEL_TO_NOT_USE +from __code.mcp_chips_corrector import ( + CHIP_CORRECTION, + CHIP_GAP, + MCP_LOW_MODE, + NBR_OF_EDGES_PIXEL_TO_NOT_USE, +) class Alignment: @@ -33,30 +38,65 @@ def fill_gaps(self, moved_image=None): logging.info("--> filling gaps") - self._fix_vertical_gap(moved_image=moved_image, first_chip_index=1, second_chip_index=2, image_mode="low") - - self._fix_vertical_gap(moved_image=moved_image, first_chip_index=3, second_chip_index=4, image_mode="low") - - self._fix_horizontal_gap(moved_image=moved_image, first_chip_index=1, second_chip_index=3, image_mode="low") - - self._fix_horizontal_gap(moved_image=moved_image, first_chip_index=2, second_chip_index=4, image_mode="low") + self._fix_vertical_gap( + moved_image=moved_image, + first_chip_index=1, + second_chip_index=2, + image_mode="low", + ) + + self._fix_vertical_gap( + moved_image=moved_image, + first_chip_index=3, + second_chip_index=4, + image_mode="low", + ) + + self._fix_horizontal_gap( + moved_image=moved_image, + first_chip_index=1, + second_chip_index=3, + image_mode="low", + ) + + self._fix_horizontal_gap( + moved_image=moved_image, + first_chip_index=2, + second_chip_index=4, + image_mode="low", + ) return moved_image - def _fix_vertical_gap(self, moved_image=None, first_chip_index=1, second_chip_index=2, image_mode="low"): - logging.info(f"---> working on vertical gap chip{first_chip_index}/chip{second_chip_index}") + def _fix_vertical_gap( + self, + moved_image=None, + first_chip_index=1, + second_chip_index=2, + image_mode="low", + ): + logging.info( + f"---> working on vertical gap chip{first_chip_index}/chip{second_chip_index}" + ) chip_a = self.get_chip(chip_index=first_chip_index) chip_b = self.get_chip(chip_index=second_chip_index) size_of_gap = CHIP_GAP[image_mode] - x_axis_left = np.zeros(self.chip_height) + self.chip_width - 1 - NBR_OF_EDGES_PIXEL_TO_NOT_USE + x_axis_left = ( + np.zeros(self.chip_height) + + self.chip_width + - 1 + - NBR_OF_EDGES_PIXEL_TO_NOT_USE + ) y_axis_left = np.arange(self.chip_height) if (first_chip_index == 1) and (second_chip_index == 2): global_y_axis_left = y_axis_left elif (first_chip_index == 3) and (second_chip_index == 4): - global_y_axis_left = np.arange(self.chip_height) + self.chip_height + size_of_gap["yoffset"] + global_y_axis_left = ( + np.arange(self.chip_height) + self.chip_height + size_of_gap["yoffset"] + ) x_axis_right = np.zeros(self.chip_height) + NBR_OF_EDGES_PIXEL_TO_NOT_USE y_axis_right = y_axis_left @@ -79,26 +119,45 @@ def _fix_vertical_gap(self, moved_image=None, first_chip_index=1, second_chip_in # logging.debug(f"-----> x0:{x0}, x1:{x1}, value_x0:{intensity_left}, value_x1:{intensity_right}") # logging.debug(f"-----> list_x_gap: {list_x_gap}") list_intensity_gap = Alignment.get_interpolated_value( - x0=x0, x1=x1, value_x0=intensity_left, value_x1=intensity_right, list_value_x=list_x_gap + x0=x0, + x1=x1, + value_x0=intensity_left, + value_x1=intensity_right, + list_value_x=list_x_gap, ) # logging.debug(f"------> list_intensity_gap: {list_intensity_gap}") for _x, _intensity in zip(list_x_gap, list_intensity_gap, strict=False): moved_image[y, _x] = _intensity - def _fix_horizontal_gap(self, moved_image=None, first_chip_index=1, second_chip_index=3, image_mode="low"): - logging.info(f"---> working on horizontal gap chip{first_chip_index}/chip{second_chip_index}") + def _fix_horizontal_gap( + self, + moved_image=None, + first_chip_index=1, + second_chip_index=3, + image_mode="low", + ): + logging.info( + f"---> working on horizontal gap chip{first_chip_index}/chip{second_chip_index}" + ) chip_a = self.get_chip(chip_index=first_chip_index) chip_b = self.get_chip(chip_index=second_chip_index) size_of_gap = CHIP_GAP[image_mode] x_axis_top = np.arange(self.chip_width) - y_axis_top = np.zeros(self.chip_width) + self.chip_height - 1 - NBR_OF_EDGES_PIXEL_TO_NOT_USE + y_axis_top = ( + np.zeros(self.chip_width) + + self.chip_height + - 1 + - NBR_OF_EDGES_PIXEL_TO_NOT_USE + ) if (first_chip_index == 1) and (second_chip_index == 3): global_x_axis_top = x_axis_top elif (first_chip_index == 2) and (second_chip_index == 4): - global_x_axis_top = np.arange(self.chip_width) + self.chip_width + size_of_gap["xoffset"] + global_x_axis_top = ( + np.arange(self.chip_width) + self.chip_width + size_of_gap["xoffset"] + ) x_axis_bottom = x_axis_top y_axis_bottom = np.zeros(self.chip_width) + NBR_OF_EDGES_PIXEL_TO_NOT_USE @@ -118,10 +177,16 @@ def _fix_horizontal_gap(self, moved_image=None, first_chip_index=1, second_chip_ y1 = y_top + size_of_gap["yoffset"] + 1 + 2 * NBR_OF_EDGES_PIXEL_TO_NOT_USE list_y_gap = np.arange(y0 + 1, y1) - logging.debug(f"-----> y0:{y0}, y1:{y1}, value_y0:{intensity_top}, value_y1:{intensity_bottom}") + logging.debug( + f"-----> y0:{y0}, y1:{y1}, value_y0:{intensity_top}, value_y1:{intensity_bottom}" + ) logging.debug(f"-----> list_y_gap: {list_y_gap}") list_intensity_gap = Alignment.get_interpolated_value( - x0=y0, x1=y1, value_x0=intensity_top, value_x1=intensity_bottom, list_value_x=list_y_gap + x0=y0, + x1=y1, + value_x0=intensity_top, + value_x1=intensity_bottom, + list_value_x=list_y_gap, ) logging.debug(f"------> list_intensity_gap: {list_intensity_gap}") for _y, _intensity in zip(list_y_gap, list_intensity_gap, strict=False): @@ -133,7 +198,10 @@ def move_chips(self, input_image=None): if input_image is None: raise ValueError("no input image provided to move_chips!") - image_height, image_width = self.parent.image_size.height, self.parent.image_size.width + image_height, image_width = ( + self.parent.image_size.height, + self.parent.image_size.width, + ) if image_height == MCP_LOW_MODE: mode = "low" @@ -150,26 +218,46 @@ def move_chips(self, input_image=None): chip3 = self.get_chip(chip_index=3) chip4 = self.get_chip(chip_index=4) - list_xoffset = [CHIP_CORRECTION[mode][key]["xoffset"] for key in CHIP_CORRECTION[mode].keys()] + list_xoffset = [ + CHIP_CORRECTION[mode][key]["xoffset"] + for key in CHIP_CORRECTION[mode].keys() + ] max_xoffset = np.max(list_xoffset) - list_yoffset = [CHIP_CORRECTION[mode][key]["yoffset"] for key in CHIP_CORRECTION[mode].keys()] + list_yoffset = [ + CHIP_CORRECTION[mode][key]["yoffset"] + for key in CHIP_CORRECTION[mode].keys() + ] max_yoffset = np.max(list_yoffset) new_image = np.zeros((image_height + max_yoffset, image_width + max_xoffset)) new_image[0:chip_height, 0:chip_width] = chip1 new_image[ - CHIP_CORRECTION[mode][2]["yoffset"] : CHIP_CORRECTION[mode][2]["yoffset"] + chip_height, - chip_width + CHIP_CORRECTION[mode][2]["xoffset"] : CHIP_CORRECTION[mode][2]["xoffset"] + 2 * chip_width, + CHIP_CORRECTION[mode][2]["yoffset"] : CHIP_CORRECTION[mode][2]["yoffset"] + + chip_height, + chip_width + CHIP_CORRECTION[mode][2]["xoffset"] : CHIP_CORRECTION[mode][2][ + "xoffset" + ] + + 2 * chip_width, ] = chip2 new_image[ - CHIP_CORRECTION[mode][3]["yoffset"] + chip_height : CHIP_CORRECTION[mode][3]["yoffset"] + 2 * chip_height, - CHIP_CORRECTION[mode][3]["xoffset"] : CHIP_CORRECTION[mode][3]["xoffset"] + chip_width, + CHIP_CORRECTION[mode][3]["yoffset"] + chip_height : CHIP_CORRECTION[mode][ + 3 + ]["yoffset"] + + 2 * chip_height, + CHIP_CORRECTION[mode][3]["xoffset"] : CHIP_CORRECTION[mode][3]["xoffset"] + + chip_width, ] = chip3 new_image[ - CHIP_CORRECTION[mode][4]["yoffset"] + chip_height : CHIP_CORRECTION[mode][4]["yoffset"] + 2 * chip_height, - CHIP_CORRECTION[mode][4]["xoffset"] + chip_width : CHIP_CORRECTION[mode][4]["xoffset"] + 2 * chip_width, + CHIP_CORRECTION[mode][4]["yoffset"] + chip_height : CHIP_CORRECTION[mode][ + 4 + ]["yoffset"] + + 2 * chip_height, + CHIP_CORRECTION[mode][4]["xoffset"] + chip_width : CHIP_CORRECTION[mode][4][ + "xoffset" + ] + + 2 * chip_width, ] = chip4 logging.debug(f"---> np.shape(new_image): {np.shape(new_image)}") @@ -179,7 +267,10 @@ def move_chips(self, input_image=None): def get_chip(self, chip_index=1): raw_image = self.raw_image - image_height, image_width = self.parent.image_size.height, self.parent.image_size.width + image_height, image_width = ( + self.parent.image_size.height, + self.parent.image_size.width, + ) chip_width = self.chip_width chip_height = self.chip_height @@ -197,7 +288,10 @@ def get_chip(self, chip_index=1): raise ValueError("chip index does not exist!") def get_chip_size(self): - image_height, image_width = self.parent.image_size.height, self.parent.image_size.width + image_height, image_width = ( + self.parent.image_size.height, + self.parent.image_size.width, + ) mid_width = int(image_width / 2) mid_height = int(image_height / 2) @@ -205,7 +299,9 @@ def get_chip_size(self): return {"width": mid_width, "height": mid_height} @staticmethod - def get_interpolated_value(x0=0, x1=1, value_x0=5, value_x1=10, list_value_x=[np.nan]): + def get_interpolated_value( + x0=0, x1=1, value_x0=5, value_x1=10, list_value_x=[np.nan] + ): # logging.debug(f"x0:{x0}, x1:{x1}. value_x0:{value_x0}, value_x1:{value_x1}, list_value:{list_value_x}") A = (x0, x1) B = (value_x0, value_x1) diff --git a/notebooks/__code/mcp_chips_corrector/event_handler.py b/notebooks/__code/mcp_chips_corrector/event_handler.py index dee46bff1..fd01cb7d3 100755 --- a/notebooks/__code/mcp_chips_corrector/event_handler.py +++ b/notebooks/__code/mcp_chips_corrector/event_handler.py @@ -53,13 +53,16 @@ def mcp_alignment_correction(self): if not first_update: _histo_widget.setLevels( - self.parent.alignment_view_histogram_level[0], self.parent.alignment_view_histogram_level[1] + self.parent.alignment_view_histogram_level[0], + self.parent.alignment_view_histogram_level[1], ) _view_box.setState(_state) def check_auto_fill_checkBox_widget(self): - self.parent.ui.auto_fill_gaps_checkBox.setEnabled(self.parent.ui.apply_chips_alignment_correction.isChecked()) + self.parent.ui.auto_fill_gaps_checkBox.setEnabled( + self.parent.ui.apply_chips_alignment_correction.isChecked() + ) def display_setup_image(self): setup_image = self.parent.o_corrector.integrated_data @@ -95,7 +98,13 @@ def display_chip_border(self, chip_index=0): _pen = QPen() _pen.setColor(COLOR_CONTOUR) _pen.setWidthF(0.01) - _roi_id = pg.ROI([x0, y0], [contour_width, contour_height], pen=_pen, scaleSnap=True, movable=False) + _roi_id = pg.ROI( + [x0, y0], + [contour_width, contour_height], + pen=_pen, + scaleSnap=True, + movable=False, + ) self.parent.setup_image_view.addItem(_roi_id) self.parent.contour_id = _roi_id @@ -140,7 +149,9 @@ def plot_profile(self): y0 = self.parent.profile[profile_type]["y0"] width = self.parent.profile[profile_type]["width"] height = self.parent.profile[profile_type]["height"] - nbr_pixels_to_exclude_on_each_side_of_chips_gap = self.parent.nbr_pixels_to_exclude_on_each_side_of_chips_gap + nbr_pixels_to_exclude_on_each_side_of_chips_gap = ( + self.parent.nbr_pixels_to_exclude_on_each_side_of_chips_gap + ) data = self.parent.integrated_data[y0 : y0 + height, x0 : x0 + width] if profile_type == "horizontal": @@ -162,14 +173,24 @@ def plot_profile(self): index_of_chip = self.o_get.get_index_of_chip_to_correct() color_pen = Get.get_color_of_pen( - gap_index=gap_index, index_of_chip=index_of_chip, profile_type=profile_type, x0=x0, y0=y0, x_axis=x_axis + gap_index=gap_index, + index_of_chip=index_of_chip, + profile_type=profile_type, + x0=x0, + y0=y0, + x_axis=x_axis, ) self.coefficient_corrector_can_be_calculated = False if len(where_is_gap_in_x_axis[0] > 0): "the inter chips space falls within the profile selected" - x_axis_other_chip, x_axis_working_chip, y_axis_other_chip, y_axis_working_chip = Get.get_x_y_ranges( + ( + x_axis_other_chip, + x_axis_working_chip, + y_axis_other_chip, + y_axis_working_chip, + ) = Get.get_x_y_ranges( index_of_chip, profile_data, profile_type, @@ -183,19 +204,27 @@ def plot_profile(self): self.x_axis_working_chip = x_axis_working_chip self.x_axis_other_chip = x_axis_other_chip - self.parent.profile_view.plot(x_axis_working_chip, y_axis_working_chip, pen=color_pen, symbol="o") - self.parent.profile_view.plot(x_axis_other_chip, y_axis_other_chip, pen="w", symbol="o") + self.parent.profile_view.plot( + x_axis_working_chip, y_axis_working_chip, pen=color_pen, symbol="o" + ) + self.parent.profile_view.plot( + x_axis_other_chip, y_axis_other_chip, pen="w", symbol="o" + ) if color_pen == "r": self.coefficient_corrector_can_be_calculated = True else: - self.parent.profile_view.plot(x_axis, profile_data, pen=color_pen, symbol="o") + self.parent.profile_view.plot( + x_axis, profile_data, pen=color_pen, symbol="o" + ) pen = QPen() pen.setColor(INTER_CHIPS) pen.setWidthF(0.3) - line = pg.InfiniteLine(pos=self.parent.image_size.width / 2, angle=90, pen=pen, label="Inter Chips") + line = pg.InfiniteLine( + pos=self.parent.image_size.width / 2, angle=90, pen=pen, label="Inter Chips" + ) self.parent.profile_view.addItem(line) def calculate_coefficient_corrector(self): @@ -243,7 +272,9 @@ def with_correction_tab(self): else: self.parent.ui.contrast_tabWidget.setTabEnabled(1, True) - image_corrected = self.calculate_contrast_image(raw_image=self.parent.setup_live_image) + image_corrected = self.calculate_contrast_image( + raw_image=self.parent.setup_live_image + ) self.parent.corrected_live_image = image_corrected self.display_contrast_image() @@ -251,7 +282,9 @@ def calculate_contrast_image(self, raw_image=None): setup_image = copy.deepcopy(raw_image) if self.parent.ui.apply_contrast_correction_checkBox.isChecked(): - coefficient = float(str(self.parent.ui.coefficient_corrector_lineEdit.text())) + coefficient = float( + str(self.parent.ui.coefficient_corrector_lineEdit.text()) + ) index_of_chip_to_correct = self.o_get.get_index_of_chip_to_correct() gap_index = self.parent.image_size.gap_index @@ -298,13 +331,18 @@ def display_contrast_image(self): _view_box.setState(_state) if not first_update: - _histo_widget.setLevels(self.parent.corrected_histogram_level[0], self.parent.corrected_histogram_level[1]) + _histo_widget.setLevels( + self.parent.corrected_histogram_level[0], + self.parent.corrected_histogram_level[1], + ) def update_result_tab(self): if str(self.parent.ui.coefficient_corrector_lineEdit.text()) == "N/A": image_corrected = self.parent.setup_live_image else: - image_corrected = self.calculate_contrast_image(raw_image=self.parent.setup_live_image) + image_corrected = self.calculate_contrast_image( + raw_image=self.parent.setup_live_image + ) o_align = Alignment(parent=self.parent, raw_image=image_corrected) _image = o_align.correct() _image = np.transpose(_image) diff --git a/notebooks/__code/mcp_chips_corrector/export.py b/notebooks/__code/mcp_chips_corrector/export.py index fc6f2975e..0e8b3ce85 100755 --- a/notebooks/__code/mcp_chips_corrector/export.py +++ b/notebooks/__code/mcp_chips_corrector/export.py @@ -25,8 +25,12 @@ def correct_all_images(self): QGuiApplication.processEvents() # to close QFileDialog if export_folder: - base_input_folder = os.path.basename(os.path.abspath(self.parent.o_corrector.input_working_folder)) - export_folder = os.path.join(export_folder, base_input_folder + "_corrected") + base_input_folder = os.path.basename( + os.path.abspath(self.parent.o_corrector.input_working_folder) + ) + export_folder = os.path.join( + export_folder, base_input_folder + "_corrected" + ) make_or_reset_folder(export_folder) logging.info("exporting all corrected images:") @@ -48,12 +52,18 @@ def correct_all_images(self): short_file_name = os.path.basename(working_list_files[_index_file]) file_extension = get_file_extension(short_file_name) o_norm.data["sample"]["file_name"] = [short_file_name] - o_norm.export(folder=export_folder, data_type="sample", file_type=file_extension) + o_norm.export( + folder=export_folder, data_type="sample", file_type=file_extension + ) self.parent.eventProgress.setValue(_index_file + 1) QGuiApplication.processEvents() - logging.info(f"-> exported file: {self.parent.o_corrector.working_list_files[_index_file]}") + logging.info( + f"-> exported file: {self.parent.o_corrector.working_list_files[_index_file]}" + ) - self.parent.ui.statusbar.showMessage(f"Corrected images are in folder {export_folder}", 10000) + self.parent.ui.statusbar.showMessage( + f"Corrected images are in folder {export_folder}", 10000 + ) self.parent.ui.statusbar.setStyleSheet("color: blue") self.parent.eventProgress.setVisible(False) diff --git a/notebooks/__code/mcp_chips_corrector/get.py b/notebooks/__code/mcp_chips_corrector/get.py index 76d581f85..4c8e81ca1 100755 --- a/notebooks/__code/mcp_chips_corrector/get.py +++ b/notebooks/__code/mcp_chips_corrector/get.py @@ -80,10 +80,17 @@ def get_x_y_ranges( x_axis_other_chip = x_axis[0 : where_is_gap - delta] y_axis_other_chip = profile_data[0 : where_is_gap - delta] - return x_axis_other_chip, x_axis_working_chip, y_axis_other_chip, y_axis_working_chip + return ( + x_axis_other_chip, + x_axis_working_chip, + y_axis_other_chip, + y_axis_working_chip, + ) @staticmethod - def get_color_of_pen(gap_index=0, index_of_chip=0, profile_type="horizontal", x0=0, y0=0, x_axis=None): + def get_color_of_pen( + gap_index=0, index_of_chip=0, profile_type="horizontal", x0=0, y0=0, x_axis=None + ): """ This method will give the color of the pen to use 'w' (white) or 'r' (red) according to the position of the profile. diff --git a/notebooks/__code/mcp_chips_corrector/interface.py b/notebooks/__code/mcp_chips_corrector/interface.py index c89515783..68cec0fb5 100755 --- a/notebooks/__code/mcp_chips_corrector/interface.py +++ b/notebooks/__code/mcp_chips_corrector/interface.py @@ -60,7 +60,8 @@ def __init__(self, parent=None, working_dir="", o_corrector=None): super(Interface, self).__init__(parent) ui_full_path = os.path.join( - os.path.dirname(os.path.dirname(os.path.dirname(__file__))), os.path.join("ui", "ui_mcp_chips_corrector.ui") + os.path.dirname(os.path.dirname(os.path.dirname(__file__))), + os.path.join("ui", "ui_mcp_chips_corrector.ui"), ) self.ui = load_ui(ui_full_path, baseinstance=self) diff --git a/notebooks/__code/mcp_chips_corrector/mcp_chips_corrector.py b/notebooks/__code/mcp_chips_corrector/mcp_chips_corrector.py index 3548ea34f..9c21c0a27 100755 --- a/notebooks/__code/mcp_chips_corrector/mcp_chips_corrector.py +++ b/notebooks/__code/mcp_chips_corrector/mcp_chips_corrector.py @@ -31,7 +31,9 @@ def load_data(self, folder_selected): full_list_files = glob.glob(os.path.join(folder_selected, "*.tif*")) full_list_files.sort() - working_list_files = [file for file in full_list_files if "_SummedImg.fits" not in file] + working_list_files = [ + file for file in full_list_files if "_SummedImg.fits" not in file + ] o_norm = Normalization() o_norm.load(file=working_list_files, notebook=True) diff --git a/notebooks/__code/metadata_ascii_parser.py b/notebooks/__code/metadata_ascii_parser.py index b284f477e..25f388f58 100755 --- a/notebooks/__code/metadata_ascii_parser.py +++ b/notebooks/__code/metadata_ascii_parser.py @@ -9,7 +9,11 @@ from IPython.display import HTML, display from ipywidgets import widgets -from __code.file_handler import force_file_extension, get_file_extension, make_ascii_file_from_string +from __code.file_handler import ( + force_file_extension, + get_file_extension, + make_ascii_file_from_string, +) from __code.ipywe import fileselector @@ -21,7 +25,10 @@ def __init__(self, working_dir="./"): def select_folder(self, instruction="Select Input Folder ...", next=None): self.input_folder_ui = fileselector.FileSelectorPanel( - instruction=instruction, start_dir=self.working_dir, type="directory", next=next + instruction=instruction, + start_dir=self.working_dir, + type="directory", + next=next, ) self.input_folder_ui.show() @@ -31,7 +38,9 @@ def save_metadata_file(self, filename): def select_metadata_file(self): _instruction = "Select Metadata File ..." self.metadata_ui = fileselector.FileSelectorPanel( - instruction=_instruction, start_dir=self.working_dir, next=self.save_metadata_file + instruction=_instruction, + start_dir=self.working_dir, + next=self.save_metadata_file, ) self.metadata_ui.show() @@ -46,17 +55,25 @@ def __init__( end_of_metadata_after_how_many_lines_from_reference_line=1, ): self.filename = filename - self.reference_line_showing_end_of_metadata = reference_line_showing_end_of_metadata + self.reference_line_showing_end_of_metadata = ( + reference_line_showing_end_of_metadata + ) self.end_of_metadata_after_how_many_lines_from_reference_line = ( end_of_metadata_after_how_many_lines_from_reference_line ) def calculate_nbr_row_metadata(self): - file_handler = codecs.open(self.filename, "r", encoding="utf_8", errors="ignore") + file_handler = codecs.open( + self.filename, "r", encoding="utf_8", errors="ignore" + ) for _row_index, _row in enumerate(file_handler.readlines()): if self.reference_line_showing_end_of_metadata in _row: - self.nbr_row_metadata = _row_index + self.end_of_metadata_after_how_many_lines_from_reference_line + 1 + self.nbr_row_metadata = ( + _row_index + + self.end_of_metadata_after_how_many_lines_from_reference_line + + 1 + ) class TimeInfoColumn: @@ -99,7 +116,9 @@ def __init__( end_of_metadata_after_how_many_lines_from_reference_line=1, ): self.filename = filename - self.reference_line_showing_end_of_metadata = reference_line_showing_end_of_metadata + self.reference_line_showing_end_of_metadata = ( + reference_line_showing_end_of_metadata + ) self.end_of_metadata_after_how_many_lines_from_reference_line = ( end_of_metadata_after_how_many_lines_from_reference_line ) @@ -115,15 +134,22 @@ def __init__( def add_acquisition_started_time_to_timestamp(self): str_acquisition_time = self.metadata_dict["Acquisition started on"]["value"] - timestamp = time.mktime(datetime.datetime.strptime(str_acquisition_time, "%m/%d/%Y %H:%M:%S").timetuple()) + timestamp = time.mktime( + datetime.datetime.strptime( + str_acquisition_time, "%m/%d/%Y %H:%M:%S" + ).timetuple() + ) new_column_values = self.o_pd.index.values + timestamp self.o_pd = self.o_pd.set_index(new_column_values) # user friendly time stamp format user_format = [ - datetime.datetime.fromtimestamp(_time).strftime("%m/%d/%Y %H:%M:%S") for _time in self.o_pd.index.values + datetime.datetime.fromtimestamp(_time).strftime("%m/%d/%Y %H:%M:%S") + for _time in self.o_pd.index.values ] - self.o_pd["timestamp_user_format"] = pd.Series(user_format, index=self.o_pd.index) + self.o_pd["timestamp_user_format"] = pd.Series( + user_format, index=self.o_pd.index + ) def set_time_info_as_index(self): time_info_column = self.time_info_column @@ -209,11 +235,15 @@ def read_specific_metadata(self): for _keys in self.metadata_dict.keys(): for _line in self.metadata: if _keys in _line: - result = _line.split(self.metadata_dict[_keys]["split1"]) # 1st split + result = _line.split( + self.metadata_dict[_keys]["split1"] + ) # 1st split if not self.metadata_dict[_keys]["split2"]: self.metadata_dict[_keys]["value"] = result[1].strip() else: # 2nd split - [value, units] = result[1].strip().split(self.metadata_dict[_keys]["split2"]) + [value, units] = ( + result[1].strip().split(self.metadata_dict[_keys]["split2"]) + ) self.metadata_dict[_keys]["value"] = value.strip() self.metadata_dict[_keys]["units"] = units.strip() @@ -264,8 +294,12 @@ def get_list_columns(self): def parse(self): if self.meta_type == "mpt": - time_info_column = TimeInfoColumn(label=self.time_label, index=self.time_index) - o_mpt = MPTFileParser(filename=self.filename, time_info_column=time_info_column) + time_info_column = TimeInfoColumn( + label=self.time_label, index=self.time_index + ) + o_mpt = MPTFileParser( + filename=self.filename, time_info_column=time_info_column + ) self.meta = o_mpt else: raise NotImplementedError("This file format is not supported!") @@ -275,7 +309,9 @@ def keep_only_columns_of_data_of_interest(self, list_columns_names=[]): list_columns_names = list(self.box.children[1].value) if list_columns_names: - self.data_to_keep = self.meta.keep_only_columns_of_data_of_interest(list_columns_names=list_columns_names) + self.data_to_keep = self.meta.keep_only_columns_of_data_of_interest( + list_columns_names=list_columns_names + ) else: self.data_to_keep = [] @@ -310,7 +346,10 @@ def select_data_to_keep(self, default_selection=[-1]): layout=widgets.Layout(width="30%"), ), widgets.SelectMultiple( - options=list_columns, value=default_value, rows=10, layout=widgets.Layout(width="30%") + options=list_columns, + value=default_value, + rows=10, + layout=widgets.Layout(width="30%"), ), ] ) @@ -321,7 +360,9 @@ def select_output_location(self, default_filename=""): [filename, ext] = os.path.splitext(self.short_filename) [_, nbr_columns] = np.shape(self.data_to_keep) p = inflect.engine() - default_filename = filename + f"_{nbr_columns}" + p.plural("column", nbr_columns) + default_filename = ( + filename + f"_{nbr_columns}" + p.plural("column", nbr_columns) + ) self.box2 = widgets.HBox( [ @@ -333,7 +374,9 @@ def select_output_location(self, default_filename=""): display(self.box2) o_folder = MetadataAsciiParser(working_dir=self.working_dir) - o_folder.select_folder(instruction="Select Output Folder:", next=self.__export_table) + o_folder.select_folder( + instruction="Select Output Folder:", next=self.__export_table + ) def __export_table(self, folder): display( @@ -344,7 +387,11 @@ def __export_table(self, folder): ) ) - display(HTML('Work in progress! ... ')) + display( + HTML( + 'Work in progress! ... ' + ) + ) output_filename = self.box2.children[1].value output_filename = force_file_extension(output_filename, ".txt") @@ -353,11 +400,15 @@ def __export_table(self, folder): # record metadata selected metadata_name_selected = np.array(self.box.children[1].value) - metadata_name_selected = np.append(metadata_name_selected, "timestamp_user_format") + metadata_name_selected = np.append( + metadata_name_selected, "timestamp_user_format" + ) data = self.get_data() self.data_to_export = data[metadata_name_selected] - self.export_table(data=self.data_to_export, folder=folder, filename=output_filename) + self.export_table( + data=self.data_to_export, folder=folder, filename=output_filename + ) def export_table(self, data=None, folder="", filename=""): full_output_filename = os.path.join(os.path.abspath(folder), filename) @@ -375,7 +426,11 @@ def export_table(self, data=None, folder="", filename=""): display(HTML('Done!')) display( - HTML('Output file created: ' + full_output_filename + "") + HTML( + 'Output file created: ' + + full_output_filename + + "" + ) ) diff --git a/notebooks/__code/metadata_handler.py b/notebooks/__code/metadata_handler.py index 4489882f5..679132e43 100755 --- a/notebooks/__code/metadata_handler.py +++ b/notebooks/__code/metadata_handler.py @@ -21,7 +21,11 @@ def get_time_stamp(file_name="", ext="tif"): except: time_stamp = o_dict[65000] - time_stamp = MetadataHandler._convert_epics_timestamp_to_rfc3339_timestamp(time_stamp) + time_stamp = ( + MetadataHandler._convert_epics_timestamp_to_rfc3339_timestamp( + time_stamp + ) + ) except: time_stamp = os.path.getmtime(file_name) @@ -103,7 +107,9 @@ def retrieve_metadata(list_files=[], list_metadata=[], using_enum_object=False): _dict = OrderedDict() for _file in list_files: _meta = MetadataHandler.get_metadata( - filename=_file, list_metadata=list_metadata, using_enum_object=using_enum_object + filename=_file, + list_metadata=list_metadata, + using_enum_object=using_enum_object, ) _dict[_file] = _meta @@ -129,7 +135,9 @@ def get_value_of_metadata_key(filename="", list_key=None): return result @staticmethod - def retrieve_value_of_metadata_key(list_files=[], list_key=[], is_from_notebook=False): + def retrieve_value_of_metadata_key( + list_files=[], list_key=[], is_from_notebook=False + ): if list_files == []: return {} @@ -139,7 +147,9 @@ def retrieve_value_of_metadata_key(list_files=[], list_key=[], is_from_notebook= _dict = OrderedDict() for _index, _file in enumerate(list_files): - _meta = MetadataHandler.get_value_of_metadata_key(filename=_file, list_key=list_key) + _meta = MetadataHandler.get_value_of_metadata_key( + filename=_file, list_key=list_key + ) _dict[_file] = _meta if is_from_notebook: progress_bar.value = _index diff --git a/notebooks/__code/metadata_overlapping_images/advanced_table_handler.py b/notebooks/__code/metadata_overlapping_images/advanced_table_handler.py index 8c1b644a1..538e8b62a 100755 --- a/notebooks/__code/metadata_overlapping_images/advanced_table_handler.py +++ b/notebooks/__code/metadata_overlapping_images/advanced_table_handler.py @@ -38,7 +38,9 @@ def add_metadata(self): try: value = float(value_str) except ValueError: - self.ui.statusbar.showMessage("This metadata can not be used - not a float value!", 10000) + self.ui.statusbar.showMessage( + "This metadata can not be used - not a float value!", 10000 + ) self.ui.statusbar.setStyleSheet("color: red") return else: @@ -59,7 +61,9 @@ def coefficient_changed(self): def update_tableWidget(self): list_metadata_index_selected = self.list_metatata_index_selected list_files = self.parent.data_dict["file_name"] - list_lineedit_ui_in_formula_tableWidget = self.list_lineedit_ui_in_formula_tableWidget + list_lineedit_ui_in_formula_tableWidget = ( + self.list_lineedit_ui_in_formula_tableWidget + ) o_table = TableHandler(table_ui=self.ui.tableWidget) for _row, _file in enumerate(list_files): @@ -79,21 +83,29 @@ def update_tableWidget(self): try: value = float(value_str) except ValueError: - self.ui.statusbar.showMessage("This metadata can not be used - not a float value!", 10000) + self.ui.statusbar.showMessage( + "This metadata can not be used - not a float value!", 10000 + ) self.ui.statusbar.setStyleSheet("color: red") return value = float(value) try: - coefficient = float(list_lineedit_ui_in_formula_tableWidget[_column].text()) + coefficient = float( + list_lineedit_ui_in_formula_tableWidget[_column].text() + ) except ValueError: - self.ui.statusbar.showMessage(f"Coefficient in column {_column} is wrong!", 10000) + self.ui.statusbar.showMessage( + f"Coefficient in column {_column} is wrong!", 10000 + ) self.ui.statusbar.setStyleSheet("color: red") return global_value += coefficient * value if there_is_at_least_one_column: - o_table.insert_item(row=_row, column=1, value=global_value, editable=False) + o_table.insert_item( + row=_row, column=1, value=global_value, editable=False + ) self.ui.statusbar.showMessage("Table refreshed with new formula!", 10000) self.ui.statusbar.setStyleSheet("color: green") @@ -174,7 +186,9 @@ def list_of_metadata(self): def file_name_value_table(self): o_table = TableHandler(table_ui=self.parent.ui.tableWidget) list_files_full_name = self.top_parent.data_dict["file_name"] - list_files_short_name = [os.path.basename(_file) for _file in list_files_full_name] + list_files_short_name = [ + os.path.basename(_file) for _file in list_files_full_name + ] o_table.insert_empty_column(0) o_table.insert_empty_column(1) diff --git a/notebooks/__code/metadata_overlapping_images/display.py b/notebooks/__code/metadata_overlapping_images/display.py index 847157843..7f87e9e4a 100755 --- a/notebooks/__code/metadata_overlapping_images/display.py +++ b/notebooks/__code/metadata_overlapping_images/display.py @@ -20,7 +20,8 @@ def get_image_selected(self, recalculate_image=False): angle = self.parent.rotation_angle # rotate all images self.parent.data_dict["data"] = [ - transform.rotate(_image, angle) for _image in self.parent.data_dict_raw["data"] + transform.rotate(_image, angle) + for _image in self.parent.data_dict_raw["data"] ] _image = self.parent.data_dict["data"][slider_index] @@ -44,7 +45,9 @@ def images(self): _view_box.setState(_state) if not first_update: - _histo_widget.setLevels(self.parent.histogram_level[0], self.parent.histogram_level[1]) + _histo_widget.setLevels( + self.parent.histogram_level[0], self.parent.histogram_level[1] + ) class DisplayScalePyqtUi: @@ -81,7 +84,10 @@ def run(self, save_it=True): adj = [] x0 = self.parent.ui.scale_position_x.value() - y0 = self.parent.ui.scale_position_y.maximum() - self.parent.ui.scale_position_y.value() + y0 = ( + self.parent.ui.scale_position_y.maximum() + - self.parent.ui.scale_position_y.value() + ) one_edge = [x0, y0] if self.parent.ui.scale_horizontal_orientation.isChecked(): @@ -109,7 +115,13 @@ def run(self, save_it=True): line_color = tuple(list_line_color) lines = np.array( [line_color for n in np.arange(len(pos))], - dtype=[("red", np.ubyte), ("green", np.ubyte), ("blue", np.ubyte), ("alpha", np.ubyte), ("width", float)], + dtype=[ + ("red", np.ubyte), + ("green", np.ubyte), + ("blue", np.ubyte), + ("alpha", np.ubyte), + ("width", float), + ], ) scale = pg.GraphItem() @@ -125,7 +137,11 @@ def run(self, save_it=True): legend = o_get.scale_legend() color = o_get.color(source="scale", color_type="html") text = pg.TextItem( - html='
' + legend + "
", + html='
' + + legend + + "
", angle=angle, ) view.addItem(text) @@ -190,7 +206,10 @@ def display_text(self, save_it=True, metadata_index=1): font_size = self.list_ui[metadata_index]["font_size_slider"].value() x0 = self.list_ui[metadata_index]["position_x"].value() - y0 = self.list_ui[metadata_index]["position_y"].maximum() - self.list_ui[metadata_index]["position_y"].value() + y0 = ( + self.list_ui[metadata_index]["position_y"].maximum() + - self.list_ui[metadata_index]["position_y"].value() + ) o_get = Get(parent=self.parent) metadata_text = o_get.metadata_text(metadata_index=metadata_index) @@ -221,18 +240,26 @@ def display_text(self, save_it=True, metadata_index=1): def clean_and_format_x_axis(self, x_axis=None): x_axis_column_index = self.parent.x_axis_column_index metadata_operation = self.parent.metadata_operation[x_axis_column_index] - x_axis = self.clean_and_format_axis(metadata_operation=metadata_operation, input_axis=x_axis) + x_axis = self.clean_and_format_axis( + metadata_operation=metadata_operation, input_axis=x_axis + ) return x_axis def clean_and_format_y_axis(self, y_axis=None): y_axis_column_index = self.parent.y_axis_column_index metadata_operation = self.parent.metadata_operation[y_axis_column_index] - y_axis = self.clean_and_format_axis(metadata_operation=metadata_operation, input_axis=y_axis) + y_axis = self.clean_and_format_axis( + metadata_operation=metadata_operation, input_axis=y_axis + ) return y_axis def clean_and_format_axis(self, metadata_operation=None, input_axis=None): - first_part_of_string_to_remove = metadata_operation["first_part_of_string_to_remove"] - last_part_of_string_to_remove = metadata_operation["last_part_of_string_to_remove"] + first_part_of_string_to_remove = metadata_operation[ + "first_part_of_string_to_remove" + ] + last_part_of_string_to_remove = metadata_operation[ + "last_part_of_string_to_remove" + ] math_1 = metadata_operation["math_1"] math_2 = metadata_operation["math_2"] value_1 = metadata_operation["value_1"] @@ -250,7 +277,11 @@ def clean_and_format_axis(self, metadata_operation=None, input_axis=None): return None value_cleaned_math = linear_operation( - input_parameter=value_cleaned, math_1=math_1, math_2=math_2, value_1=value_1, value_2=value_2 + input_parameter=value_cleaned, + math_1=math_1, + math_2=math_2, + value_1=value_1, + value_2=value_2, ) if value_cleaned_math == "": @@ -315,9 +346,7 @@ def display_graph(self, save_it=True): x_name = self.parent.ui.graph_x_axis_name.text() x_unit = self.parent.ui.graph_x_axis_units.text() if x_unit: - x_axis_label = ( - f'

{x_name} ({x_unit})' - ) + x_axis_label = f'

{x_name} ({x_unit})' else: x_axis_label = f'

{x_name}' @@ -344,11 +373,16 @@ def display_graph(self, save_it=True): ) if self.parent.ui.display_red_vertical_marker_checkbox.isChecked(): - _inf_line = pg.InfiniteLine(clean_and_format_x_axis[current_index], pen=_pen) + _inf_line = pg.InfiniteLine( + clean_and_format_x_axis[current_index], pen=_pen + ) graph.addItem(_inf_line) x0 = self.parent.ui.graph_position_x.value() - y0 = self.parent.ui.graph_position_y.maximum() - self.parent.ui.graph_position_y.value() + y0 = ( + self.parent.ui.graph_position_y.maximum() + - self.parent.ui.graph_position_y.value() + ) view.addItem(graph) graph.setPos(x0, y0) diff --git a/notebooks/__code/metadata_overlapping_images/event_handler.py b/notebooks/__code/metadata_overlapping_images/event_handler.py index 3cfad8f8f..b14b44c95 100755 --- a/notebooks/__code/metadata_overlapping_images/event_handler.py +++ b/notebooks/__code/metadata_overlapping_images/event_handler.py @@ -43,7 +43,9 @@ def right_click(self, position=None): action = menu.exec_(QtGui.QCursor.pos()) if action == _set_new_metadata: - o_selector = MetadataSelectorHandler(parent=self.parent, column=column_selected) + o_selector = MetadataSelectorHandler( + parent=self.parent, column=column_selected + ) o_selector.show() elif action == _x_axis: @@ -68,7 +70,9 @@ def metadata_list_changed(self, index, column): o_dict = dict(o_image.tag_v2) value = o_dict[float(key_selected)] - new_value = self.perform_cleaning_and_math_on_metadata(column=column, value=value) + new_value = self.perform_cleaning_and_math_on_metadata( + column=column, value=value + ) self.parent.ui.tableWidget.item(row, column).setText(f"{new_value}") self.parent.eventProgress.setValue(row + 1) @@ -80,8 +84,12 @@ def metadata_list_changed(self, index, column): def perform_cleaning_and_math_on_metadata(self, column=1, value=""): metadata_operation = self.parent.metadata_operation - first_part_of_string_to_remove = metadata_operation[column]["first_part_of_string_to_remove"] - last_part_of_string_to_remove = metadata_operation[column]["last_part_of_string_to_remove"] + first_part_of_string_to_remove = metadata_operation[column][ + "first_part_of_string_to_remove" + ] + last_part_of_string_to_remove = metadata_operation[column][ + "last_part_of_string_to_remove" + ] string_cleaned = string_cleaning( first_part_of_string_to_remove=first_part_of_string_to_remove, last_part_of_string_to_remove=last_part_of_string_to_remove, @@ -90,11 +98,17 @@ def perform_cleaning_and_math_on_metadata(self, column=1, value=""): value_1 = metadata_operation[column]["value_1"] value_2 = metadata_operation[column]["value_2"] - if is_linear_operation_valid(input_parameter=string_cleaned, value_1=value_1, value_2=value_2): + if is_linear_operation_valid( + input_parameter=string_cleaned, value_1=value_1, value_2=value_2 + ): math_1 = metadata_operation[column]["math_1"] math_2 = metadata_operation[column]["math_2"] result_linear_operation = linear_operation( - input_parameter=string_cleaned, math_1=math_1, value_1=value_1, math_2=math_2, value_2=value_2 + input_parameter=string_cleaned, + math_1=math_1, + value_1=value_1, + math_2=math_2, + value_2=value_2, ) else: return string_cleaned diff --git a/notebooks/__code/metadata_overlapping_images/export_images.py b/notebooks/__code/metadata_overlapping_images/export_images.py index 2c9eeb132..4cf00a612 100755 --- a/notebooks/__code/metadata_overlapping_images/export_images.py +++ b/notebooks/__code/metadata_overlapping_images/export_images.py @@ -36,8 +36,12 @@ def run(self): exporter = pyqtgraph.exporters.ImageExporter(self.parent.ui.image_view.view) - exporter.params.param("width").setValue(2024, blockSignal=exporter.widthChanged) - exporter.params.param("height").setValue(2014, blockSignal=exporter.heightChanged) + exporter.params.param("width").setValue( + 2024, blockSignal=exporter.widthChanged + ) + exporter.params.param("height").setValue( + 2014, blockSignal=exporter.heightChanged + ) exporter.export(output_file_name) diff --git a/notebooks/__code/metadata_overlapping_images/export_table.py b/notebooks/__code/metadata_overlapping_images/export_table.py index 1add654a1..2151d4a68 100755 --- a/notebooks/__code/metadata_overlapping_images/export_table.py +++ b/notebooks/__code/metadata_overlapping_images/export_table.py @@ -16,14 +16,21 @@ def _create_output_file_name(self): working_dir = self.parent.working_dir base_working_dir = os.path.basename(working_dir) - full_file_name = os.path.join(self.export_folder, base_working_dir + "_metadata_table.txt") + full_file_name = os.path.join( + self.export_folder, base_working_dir + "_metadata_table.txt" + ) return full_file_name def run(self): full_output_file_name = self._create_output_file_name() metadata = self.create_metadata_array() data = self.create_data_array() - make_ascii_file(metadata=metadata, data=data, output_file_name=full_output_file_name, dim="1d") + make_ascii_file( + metadata=metadata, + data=data, + output_file_name=full_output_file_name, + dim="1d", + ) show_status_message( parent=self.parent, @@ -69,9 +76,13 @@ def format_math(metadata_operation, metadata_axis="x_axis"): metadata.append(f"# Metadata {metadata_axis} operation: None") else: if value_2 == "": - metadata.append(f"# Metadata {metadata_axis} operation: {math_1} {value_1}") + metadata.append( + f"# Metadata {metadata_axis} operation: {math_1} {value_1}" + ) else: - metadata.append(f"# Metadata {metadata_axis} operation: {math_1} {value_1} {math_2} {value_2}") + metadata.append( + f"# Metadata {metadata_axis} operation: {math_1} {value_1} {math_2} {value_2}" + ) format_math(x_axis_metadata_operation, metadata_axis="x_axis") format_math(y_axis_metadata_operation, metadata_axis="y_axis") diff --git a/notebooks/__code/metadata_overlapping_images/get.py b/notebooks/__code/metadata_overlapping_images/get.py index ed91330c9..e0c64e73e 100755 --- a/notebooks/__code/metadata_overlapping_images/get.py +++ b/notebooks/__code/metadata_overlapping_images/get.py @@ -47,7 +47,9 @@ def metadata_column(self): try: _row_value = float(_row_str) except: - self.parent.ui.statusbar.showMessage("Error Displaying Metadata Graph!", 10000) + self.parent.ui.statusbar.showMessage( + "Error Displaying Metadata Graph!", 10000 + ) self.parent.ui.statusbar.setStyleSheet("color: red") return [] @@ -101,7 +103,9 @@ def metadata_text(self, metadata_index=1): slider_index = self.parent.ui.file_slider.value() index_of_y_axis = self.parent.y_axis_column_index - metadata_value = str(self.parent.ui.tableWidget.item(slider_index, index_of_y_axis).text()) + metadata_value = str( + self.parent.ui.tableWidget.item(slider_index, index_of_y_axis).text() + ) if metadata_name.strip() == "": return f"{metadata_value} {metadata_units}" else: @@ -123,7 +127,9 @@ def raw_metadata_column(self): def color(self, color_type="html", source="metadata"): if source == "metadata": - color_selected = self.parent.ui.metadata_color_combobox.currentText().lower() + color_selected = ( + self.parent.ui.metadata_color_combobox.currentText().lower() + ) elif source == "graph": color_selected = self.parent.ui.graph_color_combobox.currentText().lower() else: diff --git a/notebooks/__code/metadata_overlapping_images/initialization.py b/notebooks/__code/metadata_overlapping_images/initialization.py index 81d4f385c..ef37dacc0 100755 --- a/notebooks/__code/metadata_overlapping_images/initialization.py +++ b/notebooks/__code/metadata_overlapping_images/initialization.py @@ -6,7 +6,10 @@ from qtpy.QtWidgets import QProgressBar, QTableWidgetItem, QVBoxLayout from __code.file_handler import retrieve_time_stamp -from __code.metadata_overlapping_images.general_classes import MetadataSettings, ScaleSettings +from __code.metadata_overlapping_images.general_classes import ( + MetadataSettings, + ScaleSettings, +) from .get import Get @@ -33,7 +36,9 @@ def statusbar(self): def table(self): # init the summary table list_files_full_name = self.parent.data_dict["file_name"] - list_files_short_name = [os.path.basename(_file) for _file in list_files_full_name] + list_files_short_name = [ + os.path.basename(_file) for _file in list_files_full_name + ] self.parent.ui.tableWidget.blockSignals(True) for _row, _file in enumerate(list_files_short_name): @@ -62,7 +67,9 @@ def widgets(self): # update size of table columns nbr_columns = self.parent.ui.tableWidget.columnCount() for _col in range(nbr_columns): - self.parent.ui.tableWidget.setColumnWidth(_col, self.parent.guide_table_width[_col]) + self.parent.ui.tableWidget.setColumnWidth( + _col, self.parent.guide_table_width[_col] + ) # populate list of metadata if file is a tiff o_get = Get(parent=self.parent) @@ -73,7 +80,9 @@ def widgets(self): self.parent.ui.select_metadata_combobox.setVisible(False) # list of scale available - self.parent.ui.scale_units_combobox.addItems(self.parent.list_scale_units["string"]) + self.parent.ui.scale_units_combobox.addItems( + self.parent.list_scale_units["string"] + ) # pixel size range [height, width] = np.shape(self.parent.data_dict["data"][0]) diff --git a/notebooks/__code/metadata_overlapping_images/metadata_overlapping_images.py b/notebooks/__code/metadata_overlapping_images/metadata_overlapping_images.py index b237fbdd9..2d26006f9 100755 --- a/notebooks/__code/metadata_overlapping_images/metadata_overlapping_images.py +++ b/notebooks/__code/metadata_overlapping_images/metadata_overlapping_images.py @@ -85,7 +85,10 @@ class MetadataOverlappingImagesUi(QMainWindow): list_metadata = [] dict_list_metadata = OrderedDict() # {0: '10', 1: 'hfir', ...} list_scale_units = ["mm", "\u00b5m", "nm"] - list_scale_units = {"string": ["mm", "\u00b5m", "nm"], "html": ["mm", "µm", "nm"]} + list_scale_units = { + "string": ["mm", "\u00b5m", "nm"], + "html": ["mm", "µm", "nm"], + } rgba_color = { "white": (255, 255, 255, 255, None), @@ -103,7 +106,13 @@ class MetadataOverlappingImagesUi(QMainWindow): "black": (0, 0, 0), } - html_color = {"white": "#FFF", "red": "#F00", "green": "#0F0", "blue": "#00F", "black": "#000"} + html_color = { + "white": "#FFF", + "red": "#F00", + "green": "#0F0", + "blue": "#00F", + "black": "#000", + } # ui of pop up window that allows to define metadata column value (format it) metadata_string_format_ui = None diff --git a/notebooks/__code/metadata_overlapping_images/metadata_selector_handler.py b/notebooks/__code/metadata_overlapping_images/metadata_selector_handler.py index 302f09752..ee5caad7f 100755 --- a/notebooks/__code/metadata_overlapping_images/metadata_selector_handler.py +++ b/notebooks/__code/metadata_overlapping_images/metadata_selector_handler.py @@ -32,14 +32,24 @@ def initialization(self): self.ui.select_metadata_combobox.addItems(list_metadata) metadata_operation = self.parent.metadata_operation[self.column] - self.ui.select_metadata_combobox.setCurrentIndex(metadata_operation["index_of_metadata"]) - self.ui.first_part_lineEdit.setText(metadata_operation["first_part_of_string_to_remove"]) - self.ui.second_part_lineEdit.setText(metadata_operation["last_part_of_string_to_remove"]) + self.ui.select_metadata_combobox.setCurrentIndex( + metadata_operation["index_of_metadata"] + ) + self.ui.first_part_lineEdit.setText( + metadata_operation["first_part_of_string_to_remove"] + ) + self.ui.second_part_lineEdit.setText( + metadata_operation["last_part_of_string_to_remove"] + ) self.ui.linear_operation_lineEdit_1.setText(metadata_operation["value_1"]) self.ui.linear_operation_lineEdit_2.setText(metadata_operation["value_2"]) - math_1_index = self.ui.linear_operation_comboBox_1.findText(metadata_operation["math_1"]) + math_1_index = self.ui.linear_operation_comboBox_1.findText( + metadata_operation["math_1"] + ) self.ui.linear_operation_comboBox_1.setCurrentIndex(math_1_index) - math_2_index = self.ui.linear_operation_comboBox_2.findText(metadata_operation["math_2"]) + math_2_index = self.ui.linear_operation_comboBox_2.findText( + metadata_operation["math_2"] + ) self.ui.linear_operation_comboBox_2.setCurrentIndex(math_2_index) def string_cleaning_changed(self, new_text=None): @@ -74,7 +84,9 @@ def linear_operation_lineedit_changed(self, new_string=None): self.ui.linear_operation_value_after.setText("N/A") return - input_parameter = float(str(self.ui.linear_operation_value_before.text()).strip()) + input_parameter = float( + str(self.ui.linear_operation_value_before.text()).strip() + ) math_1 = str(self.ui.linear_operation_comboBox_1.currentText()) value_1 = str(self.ui.linear_operation_lineEdit_1.text()).strip() math_2 = str(self.ui.linear_operation_comboBox_2.currentText()) @@ -95,7 +107,10 @@ def linear_operation_combobox_changed(self, new_string=None): self.update_final_result() def update_final_result(self): - if self.is_before_linear_operation_is_valid() and self.is_linear_operation_valid(): + if ( + self.is_before_linear_operation_is_valid() + and self.is_linear_operation_valid() + ): result = self.ui.linear_operation_value_after.text() else: result = self.ui.linear_operation_value_before.text() @@ -161,8 +176,12 @@ def result_of_checking_operation(ui=None): is_error_in_operation = True return is_error_in_operation - is_error_operation_1 = result_of_checking_operation(ui=self.ui.linear_operation_lineEdit_1) - is_error_operation_2 = result_of_checking_operation(ui=self.ui.linear_operation_lineEdit_2) + is_error_operation_1 = result_of_checking_operation( + ui=self.ui.linear_operation_lineEdit_1 + ) + is_error_operation_2 = result_of_checking_operation( + ui=self.ui.linear_operation_lineEdit_2 + ) self.ui.error_label_1.setVisible(is_error_operation_1) self.ui.error_label_2.setVisible(is_error_operation_2) diff --git a/notebooks/__code/metadata_overlapping_images/metadata_string_format_handler.py b/notebooks/__code/metadata_overlapping_images/metadata_string_format_handler.py index 3de4eaf60..87b5810e0 100755 --- a/notebooks/__code/metadata_overlapping_images/metadata_string_format_handler.py +++ b/notebooks/__code/metadata_overlapping_images/metadata_string_format_handler.py @@ -94,14 +94,18 @@ def init_table_size(self): def init_table(self): list_files_full_name = self.parent.data_dict["file_name"] - list_files_short_name = [os.path.basename(_file) for _file in list_files_full_name] + list_files_short_name = [ + os.path.basename(_file) for _file in list_files_full_name + ] main_table_metadata_column = self.parent.get_raw_metadata_column() for _row, _file in enumerate(list_files_short_name): self.ui.tableWidget.insertRow(_row) self.set_item_table(row=_row, col=0, value=_file) - self.set_item_table(row=_row, col=1, value=main_table_metadata_column[_row], editable=True) + self.set_item_table( + row=_row, col=1, value=main_table_metadata_column[_row], editable=True + ) def set_item_table(self, row=0, col=0, value="", editable=False): item = QtGui.QTableWidgetItem(str(value)) diff --git a/notebooks/__code/metadata_overlapping_images/table_loader.py b/notebooks/__code/metadata_overlapping_images/table_loader.py index af2acd4b5..2505f2317 100755 --- a/notebooks/__code/metadata_overlapping_images/table_loader.py +++ b/notebooks/__code/metadata_overlapping_images/table_loader.py @@ -10,7 +10,9 @@ def __init__(self, parent=None, filename=""): self.filename = filename def load_table(self): - table = pd.read_csv(self.filename, sep=",", comment="#", names=["filename", "metadata"]) + table = pd.read_csv( + self.filename, sep=",", comment="#", names=["filename", "metadata"] + ) table_dict = {} for _row in table.values: _key, _value = _row diff --git a/notebooks/__code/metadata_overlapping_images/utilities.py b/notebooks/__code/metadata_overlapping_images/utilities.py index e0575a51d..2fd177948 100755 --- a/notebooks/__code/metadata_overlapping_images/utilities.py +++ b/notebooks/__code/metadata_overlapping_images/utilities.py @@ -3,7 +3,11 @@ from . import LIST_FUNNY_CHARACTERS -def string_cleaning(first_part_of_string_to_remove="", last_part_of_string_to_remove="", string_to_clean=""): +def string_cleaning( + first_part_of_string_to_remove="", + last_part_of_string_to_remove="", + string_to_clean="", +): first_part_of_string_to_remove = first_part_of_string_to_remove _clean_first_part = "" for _c in first_part_of_string_to_remove: @@ -31,7 +35,9 @@ def string_cleaning(first_part_of_string_to_remove="", last_part_of_string_to_re def linear_operation(input_parameter="", math_1="", value_1="", math_2="", value_2=""): - if is_linear_operation_valid(input_parameter=input_parameter, value_1=value_1, value_2=value_2): + if is_linear_operation_valid( + input_parameter=input_parameter, value_1=value_1, value_2=value_2 + ): operation_to_eval = f"{input_parameter}" if value_1: operation_to_eval += f" {math_1} {float(value_1)}" diff --git a/notebooks/__code/normalization.py b/notebooks/__code/normalization.py index a85c7fa40..82c5429bf 100755 --- a/notebooks/__code/normalization.py +++ b/notebooks/__code/normalization.py @@ -138,7 +138,10 @@ def __top_panel(self): title_ui = widgets.HBox( [ widgets.Label("Instructions:", layout=widgets.Layout(width="20%")), - widgets.Label("Select Samples Images and click NEXT", layout=widgets.Layout(width="50%")), + widgets.Label( + "Select Samples Images and click NEXT", + layout=widgets.Layout(width="50%"), + ), ] ) @@ -148,7 +151,9 @@ def __top_panel(self): widgets.Label("None", layout=widgets.Layout(width="50%")), ] ) - self.title = title_ui.children[1] # "Select [Samples/OB/DF] Images [and click NEXT] + self.title = title_ui.children[ + 1 + ] # "Select [Samples/OB/DF] Images [and click NEXT] self.label = label_ui.children[1] # number of samples selected self.top_panel = widgets.VBox(children=[title_ui, label_ui], layout=self.layout) @@ -172,7 +177,9 @@ def __bottom_panel(self): self.prev_button_ui.on_click(self.prev_button_clicked) list_ui.append(self.prev_button_ui) - self.current_state_label_ui = widgets.Label(" ", layout=widgets.Layout(width="70%")) + self.current_state_label_ui = widgets.Label( + " ", layout=widgets.Layout(width="70%") + ) list_ui.append(self.current_state_label_ui) if self.next_button: @@ -208,7 +215,9 @@ def nextStep(self): class WizardPanel: - label_layout = Layout(border="1px lighgray solide", height="35px", padding="8px", width="300px") + label_layout = Layout( + border="1px lighgray solide", height="35px", padding="8px", width="300px" + ) sample_panel = None def __init__(self, sample_panel=None): @@ -223,9 +232,14 @@ class SampleSelectionPanel(Panel): files = None o_norm = None - def __init__(self, prev_button=False, next_button=True, working_dir="", top_object=None): + def __init__( + self, prev_button=False, next_button=True, working_dir="", top_object=None + ): super(SampleSelectionPanel, self).__init__( - prev_button=prev_button, next_button=next_button, working_dir=working_dir, top_object=top_object + prev_button=prev_button, + next_button=next_button, + working_dir=working_dir, + top_object=top_object, ) # def __init__(self, prev_button=False, next_button=True, working_dir='', top_object=None, gamma_coefficient=None): @@ -250,13 +264,17 @@ def __init__(self, working_dir="", top_object=None): def next_button_clicked(self, event): self.remove() - _panel = DFSelectionPanel(working_dir=self.working_dir, top_object=self.top_object) + _panel = DFSelectionPanel( + working_dir=self.working_dir, top_object=self.top_object + ) _panel.init_ui(files=self.files) _panel.show() def prev_button_clicked(self, event): self.remove() - _panel = SampleSelectionPanel(working_dir=self.working_dir, top_object=self.top_object) + _panel = SampleSelectionPanel( + working_dir=self.working_dir, top_object=self.top_object + ) _panel.init_ui(files=self.files) _panel.show() @@ -265,19 +283,27 @@ class DFSelectionPanel(Panel): def __init__(self, working_dir="", top_object=None): self.working_dir = working_dir super(DFSelectionPanel, self).__init__( - prev_button=True, next_button=True, state="df", working_dir=working_dir, top_object=top_object + prev_button=True, + next_button=True, + state="df", + working_dir=working_dir, + top_object=top_object, ) def prev_button_clicked(self, event): self.remove() - _panel = OBSelectionPanel(working_dir=self.working_dir, top_object=self.top_object) + _panel = OBSelectionPanel( + working_dir=self.working_dir, top_object=self.top_object + ) _panel.init_ui(files=self.files) _panel.show() def next_button_clicked(self, event): self.remove() o_norm_handler = NormalizationHandler( - files=self.files, working_dir=self.working_dir, gamma_threshold=self.gamma_threshold + files=self.files, + working_dir=self.working_dir, + gamma_threshold=self.gamma_threshold, ) o_norm_handler.load_data() self.top_object.o_norm_handler = o_norm_handler @@ -365,11 +391,17 @@ def with_or_without_roi(self): "Do you want to select a region of interest (ROI) that will make sure that the " + "sample background matches the OB background" ) - label2 = widgets.Label("-> Make sure your selection do not overlap your sample!") + label2 = widgets.Label( + "-> Make sure your selection do not overlap your sample!" + ) box = widgets.HBox( [ widgets.Label("With or Without ROI?"), - widgets.RadioButtons(options=["yes", "no"], value="yes", layout=widgets.Layout(width="50%")), + widgets.RadioButtons( + options=["yes", "no"], + value="yes", + layout=widgets.Layout(width="50%"), + ), ] ) self.with_or_without_radio_button = box.children[1] @@ -378,11 +410,15 @@ def with_or_without_roi(self): def select_sample_roi(self): if self.with_or_without_radio_button.value == "no": - label2 = widgets.Label("-> You chose not to select any ROI! Next step: Normalization") + label2 = widgets.Label( + "-> You chose not to select any ROI! Next step: Normalization" + ) display(label2) return - label2 = widgets.Label("-> Make sure your selection do not overlap your sample!") + label2 = widgets.Label( + "-> Make sure your selection do not overlap your sample!" + ) display(label2) if self.integrated_sample == []: @@ -396,20 +432,51 @@ def plot_roi(x_left, y_top, width, height): ax_img = plt.subplot(111) ax_img.imshow(_integrated_sample, cmap="viridis", interpolation=None) - _rectangle = patches.Rectangle((x_left, y_top), width, height, edgecolor="white", linewidth=2, fill=False) + _rectangle = patches.Rectangle( + (x_left, y_top), + width, + height, + edgecolor="white", + linewidth=2, + fill=False, + ) ax_img.add_patch(_rectangle) return [x_left, y_top, width, height] self.roi_selection = widgets.interact( plot_roi, - x_left=widgets.IntSlider(min=0, max=width, step=1, value=0, description="X Left", continuous_update=False), - y_top=widgets.IntSlider(min=0, max=height, value=0, step=1, description="Y Top", continuous_update=False), + x_left=widgets.IntSlider( + min=0, + max=width, + step=1, + value=0, + description="X Left", + continuous_update=False, + ), + y_top=widgets.IntSlider( + min=0, + max=height, + value=0, + step=1, + description="Y Top", + continuous_update=False, + ), width=widgets.IntSlider( - min=0, max=width - 1, step=1, value=60, description="Width", continuous_update=False + min=0, + max=width - 1, + step=1, + value=60, + description="Width", + continuous_update=False, ), height=widgets.IntSlider( - min=0, max=height - 1, step=1, value=100, description="Height", continuous_update=False + min=0, + max=height - 1, + step=1, + value=100, + description="Height", + continuous_update=False, ), ) @@ -478,9 +545,15 @@ def display_file_selector_from_home(ev): hbox = widgets.HBox( [ widgets.Button( - description=f"Jump to {ipts} Shared Folder", button_style="success", layout=button_layout + description=f"Jump to {ipts} Shared Folder", + button_style="success", + layout=button_layout, + ), + widgets.Button( + description="Jump to My Home Folder", + button_style="success", + layout=button_layout, ), - widgets.Button(description="Jump to My Home Folder", button_style="success", layout=button_layout), ] ) go_to_shared_button_ui = hbox.children[0] @@ -495,13 +568,20 @@ def display_file_selector_from_home(ev): def display_file_selector(self, start_dir=""): self.output_folder_ui = fileselector.FileSelectorPanel( - instruction="Select Output Folder", start_dir=start_dir, multiple=False, type="directory" + instruction="Select Output Folder", + start_dir=start_dir, + multiple=False, + type="directory", ) self.output_folder_ui.show() def export(self): - base_folder = os.path.basename(os.path.dirname(self.list_file_names[0])) + "_normalized" - output_folder = os.path.abspath(os.path.join(self.output_folder_ui.selected, base_folder)) + base_folder = ( + os.path.basename(os.path.dirname(self.list_file_names[0])) + "_normalized" + ) + output_folder = os.path.abspath( + os.path.join(self.output_folder_ui.selected, base_folder) + ) utilities.make_dir(dir=output_folder) w = widgets.IntProgress() @@ -536,7 +616,10 @@ def select_gamma_coefficient(self): [ widgets.Label("Gamma Coefficient:", layout=widgets.Layout(width="20%")), widgets.FloatSlider( - value=gamma_filtering_coefficient, min=0, max=1, layout=widgets.Layout(width="50%") + value=gamma_filtering_coefficient, + min=0, + max=1, + layout=widgets.Layout(width="50%"), ), ] ) diff --git a/notebooks/__code/normalization/__init__.py b/notebooks/__code/normalization/__init__.py index 001fedd24..bf0439278 100755 --- a/notebooks/__code/normalization/__init__.py +++ b/notebooks/__code/normalization/__init__.py @@ -1,6 +1,8 @@ LOG_FILENAME = ".normalization_with_simplify_selection.log" -TEMPORARY_ROI_BUTTON_DESCRIPTION = "Check UI that popped up! (May be hidden behind this browser!)" +TEMPORARY_ROI_BUTTON_DESCRIPTION = ( + "Check UI that popped up! (May be hidden behind this browser!)" +) TEMPORARY_ROI_ICON = "" ROI_BUTTON_DESCRIPTION = "Selection of region of interest (ROI) - OPTIONAL" ROI_ICON = "gear" diff --git a/notebooks/__code/normalization/get.py b/notebooks/__code/normalization/get.py index 10cd67c36..4e58d1172 100755 --- a/notebooks/__code/normalization/get.py +++ b/notebooks/__code/normalization/get.py @@ -8,7 +8,11 @@ from . import ROI_BUTTON_DESCRIPTION, ROI_ICON -LIST_METADATA_NOT_INSTRUMENT_RELATED = ["filename", "time_stamp", "time_stamp_user_format"] +LIST_METADATA_NOT_INSTRUMENT_RELATED = [ + "filename", + "time_stamp", + "time_stamp_user_format", +] class Get(TopGet): @@ -36,15 +40,18 @@ def active_tab_config_key(self): return current_config_tab.get_title(current_config_tab_index) def time_before_and_after_of_this_config(self, current_config=None): - [time_before_selected_ui, time_after_selected_ui] = self.time_before_and_after_ui_of_this_config( - current_config=current_config + [time_before_selected_ui, time_after_selected_ui] = ( + self.time_before_and_after_ui_of_this_config(current_config=current_config) ) return [time_before_selected_ui.value, time_after_selected_ui.value] def time_before_and_after_ui_of_this_config(self, current_config=None): if current_config is None: current_config = self.current_config_of_widgets_id() - return [current_config["time_slider_before_experiment"], current_config["time_slider_after_experiment"]] + return [ + current_config["time_slider_before_experiment"], + current_config["time_slider_after_experiment"], + ] def time_before_and_after_message_ui_of_this_config(self): current_config = self.current_config_of_widgets_id() @@ -80,7 +87,9 @@ def max_time_elapse_before_experiment(self): dict_for_this_config = final_full_master_dict[acquisition_key][config_key] # retrieve first and last sample file for this config and for this acquisition - first_sample_image_time_stamp = dict_for_this_config["first_images"]["sample"]["time_stamp"] + first_sample_image_time_stamp = dict_for_this_config["first_images"]["sample"][ + "time_stamp" + ] first_ob = dict_for_this_config["first_images"]["ob"]["time_stamp"] if first_ob > first_sample_image_time_stamp: @@ -101,7 +110,9 @@ def max_time_elapse_after_experiment(self): dict_for_this_config = final_full_master_dict[acquisition_key][config_key] # retrieve first and last sample file for this config and for this acquisition - last_sample_images_time_stamp = dict_for_this_config["last_images"]["sample"]["time_stamp"] + last_sample_images_time_stamp = dict_for_this_config["last_images"]["sample"][ + "time_stamp" + ] last_ob = dict_for_this_config["last_images"]["ob"]["time_stamp"] if last_ob < last_sample_images_time_stamp: @@ -113,7 +124,10 @@ def full_layout_for_this_config(self, dict_config): config_widgets_id_dict = {} def _make_list_basename_file(list_name="list_sample"): - return [os.path.basename(_entry["filename"]) for _entry in dict_config[list_name]] + return [ + os.path.basename(_entry["filename"]) + for _entry in dict_config[list_name] + ] def _make_full_file_name(list_name="list_sample"): return [_entry["filename"] for _entry in dict_config[list_name]] @@ -124,20 +138,32 @@ def _make_full_file_name(list_name="list_sample"): # normalize or not this configuration use_this_config_widget = widgets.Checkbox( - description="Normalize this configuration", value=True, layout=widgets.Layout(width="100%") + description="Normalize this configuration", + value=True, + layout=widgets.Layout(width="100%"), + ) + use_this_config_widget.observe( + self.parent.update_use_this_config_widget, names="value" ) - use_this_config_widget.observe(self.parent.update_use_this_config_widget, names="value") config_widgets_id_dict["use_this_config"] = use_this_config_widget # use custom time range check box check_box_user_time_range = widgets.Checkbox( - description="Use selected OB & DF from custom time range", value=False, layout=widgets.Layout(width="35%") + description="Use selected OB & DF from custom time range", + value=False, + layout=widgets.Layout(width="35%"), + ) + config_widgets_id_dict["use_custom_time_range_checkbox"] = ( + check_box_user_time_range + ) + check_box_user_time_range.observe( + self.parent.update_config_widgets, names="value" ) - config_widgets_id_dict["use_custom_time_range_checkbox"] = check_box_user_time_range - check_box_user_time_range.observe(self.parent.update_config_widgets, names="value") [max_time_elapse_before_experiment, max_time_elapse_after_experiment] = ( - self.parent.calculate_max_time_before_and_after_exp_for_this_config(dict_config) + self.parent.calculate_max_time_before_and_after_exp_for_this_config( + dict_config + ) ) hori_layout1 = widgets.HBox( @@ -151,7 +177,10 @@ def _make_full_file_name(list_name="list_sample"): readout=False, layout=widgets.Layout(width="30%", visibility="hidden"), ), - widgets.Label(" <<< EXPERIMENT >>> ", layout=widgets.Layout(width="20%", visibility="hidden")), + widgets.Label( + " <<< EXPERIMENT >>> ", + layout=widgets.Layout(width="20%", visibility="hidden"), + ), widgets.FloatSlider( value=max_time_elapse_before_experiment + 0.1, min=0, @@ -166,10 +195,18 @@ def _make_full_file_name(list_name="list_sample"): self.parent.time_before_slider = hori_layout1.children[1] self.parent.time_after_slider = hori_layout1.children[3] self.parent.experiment_label = hori_layout1.children[2] - self.parent.time_after_slider.observe(self.parent.update_time_range_event, names="value") - self.parent.time_before_slider.observe(self.parent.update_time_range_event, names="value") - config_widgets_id_dict["time_slider_before_experiment"] = hori_layout1.children[1] - config_widgets_id_dict["time_slider_after_experiment"] = hori_layout1.children[3] + self.parent.time_after_slider.observe( + self.parent.update_time_range_event, names="value" + ) + self.parent.time_before_slider.observe( + self.parent.update_time_range_event, names="value" + ) + config_widgets_id_dict["time_slider_before_experiment"] = hori_layout1.children[ + 1 + ] + config_widgets_id_dict["time_slider_after_experiment"] = hori_layout1.children[ + 3 + ] config_widgets_id_dict["experiment_label"] = hori_layout1.children[2] nbr_sample = len(list_sample) @@ -181,9 +218,13 @@ def get_html_table(): how_to_combine = how_to_ui.value if force_combine == "yes": - description = f"OBs will be combined using {how_to_combine}" + description = ( + f"OBs will be combined using {how_to_combine}" + ) else: - description = "OBs won't be combined! Each sample will use 1 OB" + description = ( + "OBs won't be combined! Each sample will use 1 OB" + ) html_table = ( f"" @@ -216,25 +257,38 @@ def get_html_table(): force_ui_disabled = False html_string = "" force_ui = widgets.RadioButtons( - options=["yes", "no"], value="yes", disabled=force_ui_disabled, layout=widgets.Layout(width="200px") + options=["yes", "no"], + value="yes", + disabled=force_ui_disabled, + layout=widgets.Layout(width="200px"), ) force_ui.observe(self.parent.do_you_want_to_combine_changed, names="value") combine_or_no_ui = widgets.VBox( - [widgets.HTML("Do you want to combine the OBs?"), force_ui, widgets.HTML(html_string)] + [ + widgets.HTML("Do you want to combine the OBs?"), + force_ui, + widgets.HTML(html_string), + ] ) config_widgets_id_dict["force_combine"] = force_ui config_widgets_id_dict["force_combine_message"] = combine_or_no_ui.children[2] # how to combine widgets how_to_ui = widgets.RadioButtons( - options=["median", "mean"], value="median", layout=widgets.Layout(width="200px") + options=["median", "mean"], + value="median", + layout=widgets.Layout(width="200px"), ) how_to_ui.observe(self.parent.how_to_combine_changed, names="value") - how_to_combine_ui = widgets.VBox([widgets.HTML("How to combine the OBs?"), how_to_ui]) + how_to_combine_ui = widgets.VBox( + [widgets.HTML("How to combine the OBs?"), how_to_ui] + ) config_widgets_id_dict["how_to_combine"] = how_to_ui # table - table_title = widgets.HTML("
S U M M A R Y
") + table_title = widgets.HTML( + "
S U M M A R Y
" + ) html_table = "" table = widgets.HTML(value=html_table) @@ -253,43 +307,65 @@ def get_html_table(): config_widgets_id_dict["time_slider_before_message"] = hori_layout2.children[1] # table of metadata - [metadata_table_label, metadata_table] = self.parent.populate_metadata_table(dict_config) + [metadata_table_label, metadata_table] = self.parent.populate_metadata_table( + dict_config + ) select_width = "100%" sample_list_of_runs = widgets.VBox( [ - widgets.HTML("List of Sample runs (ALL RUNS listed here will be " "used!"), - widgets.Select(options=list_sample, layout=widgets.Layout(width=select_width, height="300px")), + widgets.HTML( + "List of Sample runs (ALL RUNS listed here will be used!" + ), + widgets.Select( + options=list_sample, + layout=widgets.Layout(width=select_width, height="300px"), + ), ], layout=widgets.Layout(width="100%"), ) # self.list_of_runs_ui = box0.children[1] ob_list_of_runs = widgets.VBox( [ - widgets.HTML("List of OBs. Only the selected images will be used!"), + widgets.HTML( + "List of OBs. Only the selected images will be used!" + ), widgets.SelectMultiple( - options=list_ob, value=list_ob, layout=widgets.Layout(width=select_width, height="300px") + options=list_ob, + value=list_ob, + layout=widgets.Layout(width=select_width, height="300px"), ), ], layout=widgets.Layout(width="100%"), ) - ob_list_of_runs.children[1].observe(self.parent.selection_of_ob_changed, names="value") + ob_list_of_runs.children[1].observe( + self.parent.selection_of_ob_changed, names="value" + ) df_list_of_runs = widgets.VBox( [ - widgets.HTML("List of DCs.Only the selected images will be used!"), + widgets.HTML( + "List of DCs.Only the selected images will be used!" + ), widgets.SelectMultiple( - options=list_df, value=list_df, layout=widgets.Layout(width=select_width, height="300px") + options=list_df, + value=list_df, + layout=widgets.Layout(width=select_width, height="300px"), ), ], layout=widgets.Layout(width="100%"), ) red_hr_line = widgets.HTML("
") - black_hr_line = widgets.HTML("
") + black_hr_line = widgets.HTML( + "
" + ) # select ROI select_roi_button = widgets.Button( - description=ROI_BUTTON_DESCRIPTION, button_style="", layout=widgets.Layout(width="100%"), icon=ROI_ICON + description=ROI_BUTTON_DESCRIPTION, + button_style="", + layout=widgets.Layout(width="100%"), + icon=ROI_ICON, ) select_roi_button.style.button_color = "lightgreen" select_roi_button.style.font_weight = "bold" @@ -330,11 +406,16 @@ def get_html_table(): ] ) - return {"verti_layout": verti_layout, "config_widgets_id_dict": config_widgets_id_dict} + return { + "verti_layout": verti_layout, + "config_widgets_id_dict": config_widgets_id_dict, + } @staticmethod def list_of_tiff_files(folder=""): - list_of_tiff_files = file_handler.get_list_of_files(folder=folder, extension="tiff") + list_of_tiff_files = file_handler.get_list_of_files( + folder=folder, extension="tiff" + ) return list_of_tiff_files @staticmethod diff --git a/notebooks/__code/normalization/metadata_handler.py b/notebooks/__code/normalization/metadata_handler.py index 0a1dcaa88..8d9cf35cb 100755 --- a/notebooks/__code/normalization/metadata_handler.py +++ b/notebooks/__code/normalization/metadata_handler.py @@ -58,14 +58,21 @@ def retrieve_metadata(list_of_files=None, display_infos=False, label=""): _dict = file_handler.retrieve_time_stamp(list_of_files, label=label) _time_metadata_dict = MetadataHandler._reformat_dict(dictionary=_dict) - _beamline_metadata_dict = MetadataHandler.retrieve_beamline_metadata(list_of_files) + _beamline_metadata_dict = MetadataHandler.retrieve_beamline_metadata( + list_of_files + ) _metadata_dict = combine_dictionaries( - master_dictionary=_time_metadata_dict, servant_dictionary=_beamline_metadata_dict + master_dictionary=_time_metadata_dict, + servant_dictionary=_beamline_metadata_dict, ) if display_infos: display( - HTML('Nbr of images: ' + str(len(_metadata_dict)) + "Nbr of images: ' + + str(len(_metadata_dict)) + + "{len(b)} Sample files selected")) + display( + HTML( + f"{len(b)} Sample files selected" + ) + ) print(f"{len(b)} sample files selected") def select_ob_runs(self): @@ -74,7 +79,11 @@ def select_ob_runs(self): def ob_next_function(self, b): notebook_logging.info(f"OB files selected: {b}") self.ob_runs = b - display(HTML(f"{len(b)} OB files selected")) + display( + HTML( + f"{len(b)} OB files selected" + ) + ) print(f"{len(b)} OB files selected") def select_dc_runs(self): @@ -89,15 +98,21 @@ def select_dc_runs(self): def dc_next_function(self, b): notebook_logging.info(f"DC files selected: {b}") self.dc_runs = b - display(HTML(f"{len(b)} DC files selected")) + display( + HTML( + f"{len(b)} DC files selected" + ) + ) print(f"{len(b)} DC files selected") - def select_data(self, instruction="Select data runs", - next_function=None, - start_dir=None, - multiple=True, - newdir_toolbar_button=False): - + def select_data( + self, + instruction="Select data runs", + next_function=None, + start_dir=None, + multiple=True, + newdir_toolbar_button=False, + ): self.list_input_folders_ui = MyFileSelectorPanel( instruction=instruction, start_dir=start_dir, @@ -139,14 +154,15 @@ def normalization_settings(self): list_files.ob = self.ob_runs list_files.df = self.dc_runs - self.o_norm_handler = NormalizationHandler(list_files=list_files, - working_dir=self.working_dir, - sample_data=self.data_array) + self.o_norm_handler = NormalizationHandler( + list_files=list_files, + working_dir=self.working_dir, + sample_data=self.data_array, + ) self.o_norm_handler.load_data() self.o_norm_handler.settings() def select_output_folder(self): - self.list_input_folders_ui = MyFileSelectorPanel( instruction="Select output folder", start_dir=self.working_dir, @@ -161,7 +177,11 @@ def select_output_folder(self): def normalized_and_export(self, output_folder): notebook_logging.info(f"Output folder selected: {output_folder}") - display(HTML(f"Output folder selected: {output_folder}")) + display( + HTML( + f"Output folder selected: {output_folder}" + ) + ) self.output_folder = output_folder def export_normalized_data(self): @@ -181,22 +201,18 @@ def export_normalized_data(self): def legend(cls) -> None: display(HTML("
")) display(HTML("

Legend

")) - display(HTML("
    " - "
  • Mandatory steps must be performed to ensure proper data processing.
  • " - "
  • Optional but recommended steps are not mandatory but should be performed to ensure proper data processing.
  • " - "
  • Optional steps are not mandatory but highly recommended to improve the quality of your data processing.
  • " - "
")) + display( + HTML( + "
    " + "
  • Mandatory steps must be performed to ensure proper data processing.
  • " + "
  • Optional but recommended steps are not mandatory but should be performed to ensure proper data processing.
  • " + "
  • Optional steps are not mandatory but highly recommended to improve the quality of your data processing.
  • " + "
" + ) + ) display(HTML("
")) - - - - - - - - class NormalizationHandler: data = None integrated_sample = [] @@ -205,16 +221,18 @@ class NormalizationHandler: normalized_data_array = [] - def __init__(self, list_files: ListFiles = None, - working_dir: str = "", - gamma_threshold: float = 0.9, - sample_data: list = None): - + def __init__( + self, + list_files: ListFiles = None, + working_dir: str = "", + gamma_threshold: float = 0.9, + sample_data: list = None, + ): self.files = list_files self.working_dir = working_dir self.data = Data() if sample_data is not None: - self.data.sample = sample_data + self.data.sample = sample_data self.gamma_threshold = gamma_threshold @@ -379,7 +397,11 @@ def widgets_changed(): self.how_to_ui.disabled = False else: accordion_children = [self.force_ui, self.how_to_ui, table] - accordion_title = [force_combine_title, how_to_combine_title, table_title] + accordion_title = [ + force_combine_title, + how_to_combine_title, + table_title, + ] self.how_to_ui.disabled = False table.value = get_html_table() accordion.children = accordion_children @@ -392,9 +414,13 @@ def get_html_table(): how_to_combine = self.how_to_ui.value if force_combine == "yes": - description = f"OBs will be combined using {how_to_combine}" + description = ( + f"OBs will be combined using {how_to_combine}" + ) else: - description = "OBs won't be combined! Each sample will use 1 OB" + description = ( + "OBs won't be combined! Each sample will use 1 OB" + ) html_table = ( f"
" @@ -418,13 +444,18 @@ def get_html_table(): accordion_title = list() self.force_ui = widgets.RadioButtons( - options=["yes", "no"], value="yes", disabled=False, layout=widgets.Layout(width="200px") + options=["yes", "no"], + value="yes", + disabled=False, + layout=widgets.Layout(width="200px"), ) accordion_children.append(self.force_ui) self.force_ui.observe(force_combining_changed, names="value") self.how_to_ui = widgets.RadioButtons( - options=["median", "mean"], value="median", layout=widgets.Layout(width="200px") + options=["median", "mean"], + value="median", + layout=widgets.Layout(width="200px"), ) accordion_children.append(self.how_to_ui) self.how_to_ui.observe(how_to_combine_changed, names="value") @@ -445,7 +476,9 @@ def get_html_table(): table.value = get_html_table() - accordion = widgets.Accordion(children=accordion_children, title=accordion_title) + accordion = widgets.Accordion( + children=accordion_children, title=accordion_title + ) for _index, _title in enumerate(accordion_title): accordion.set_title(_index, _title) @@ -465,13 +498,18 @@ def run_normalization(self, dict_roi=None): elif how_to_combine == "median": force_median_ob = True else: - raise NotImplementedError(f"How to combine OB algorithm ({how_to_combine}) not implemented!") + raise NotImplementedError( + f"How to combine OB algorithm ({how_to_combine}) not implemented!" + ) if dict_roi is None: # try: self.o_norm.df_correction() self.o_norm.normalization( - notebook=True, force_median_ob=force_median_ob, force_mean_ob=force_mean_ob, force=True + notebook=True, + force_median_ob=force_median_ob, + force_mean_ob=force_mean_ob, + force=True, ) self.normalized_data_array = self.o_norm.get_normalized_data() self.normalized_metadata_array = self.o_norm.data["sample"]["metadata"] @@ -514,7 +552,7 @@ def run_normalization(self, dict_roi=None): force_median_ob=force_median_ob, force_mean_ob=force_mean_ob, force=True, - ) + ) # except ValueError: # display( # HTML( @@ -551,9 +589,15 @@ def display_file_selector_from_home(ev): hbox = widgets.HBox( [ widgets.Button( - description=f"Jump to {ipts} Shared Folder", button_style="success", layout=button_layout + description=f"Jump to {ipts} Shared Folder", + button_style="success", + layout=button_layout, + ), + widgets.Button( + description="Jump to My Home Folder", + button_style="success", + layout=button_layout, ), - widgets.Button(description="Jump to My Home Folder", button_style="success", layout=button_layout), ] ) go_to_shared_button_ui = hbox.children[0] @@ -568,12 +612,17 @@ def display_file_selector_from_home(ev): def display_file_selector(self, start_dir=""): self.output_folder_ui = fileselector.FileSelectorPanel( - instruction="Select Output Folder", start_dir=start_dir, multiple=False, type="directory" + instruction="Select Output Folder", + start_dir=start_dir, + multiple=False, + type="directory", ) self.output_folder_ui.show() def export(self, output_folder): - base_folder = os.path.basename(os.path.dirname(self.list_file_names[0])) + "_normalized" + base_folder = ( + os.path.basename(os.path.dirname(self.list_file_names[0])) + "_normalized" + ) output_folder = os.path.join(output_folder, base_folder) output_folder = make_or_increment_folder_name(output_folder) @@ -609,7 +658,10 @@ def select_gamma_coefficient(self): [ widgets.Label("Gamma Coefficient:", layout=widgets.Layout(width="20%")), widgets.FloatSlider( - value=gamma_filtering_coefficient, min=0, max=1, layout=widgets.Layout(width="50%") + value=gamma_filtering_coefficient, + min=0, + max=1, + layout=widgets.Layout(width="50%"), ), ] ) @@ -619,16 +671,6 @@ def get_coefficient(self): return self.gamma_coeff_ui.children[1].value - - - - - - - - - - def close(w): "recursively close a widget" if hasattr(w, "children"): @@ -754,7 +796,10 @@ def __top_panel(self): title_ui = widgets.HBox( [ widgets.Label("Instructions:", layout=widgets.Layout(width="20%")), - widgets.Label("Select Samples Images and click NEXT", layout=widgets.Layout(width="50%")), + widgets.Label( + "Select Samples Images and click NEXT", + layout=widgets.Layout(width="50%"), + ), ] ) @@ -764,7 +809,9 @@ def __top_panel(self): # widgets.Label("None", layout=widgets.Layout(width="50%")), # ] # ) - self.title = title_ui.children[1] # "Select [Samples/OB/DF] Images [and click NEXT] + self.title = title_ui.children[ + 1 + ] # "Select [Samples/OB/DF] Images [and click NEXT] # self.label = label_ui.children[1] # number of samples selected # self.top_panel = widgets.VBox(children=[title_ui, label_ui], layout=self.layout) @@ -789,7 +836,9 @@ def __bottom_panel(self): self.prev_button_ui.on_click(self.prev_button_clicked) list_ui.append(self.prev_button_ui) - self.current_state_label_ui = widgets.Label(" ", layout=widgets.Layout(width="70%")) + self.current_state_label_ui = widgets.Label( + " ", layout=widgets.Layout(width="70%") + ) list_ui.append(self.current_state_label_ui) if self.next_button: @@ -826,7 +875,9 @@ def nextStep(self): class WizardPanel: - label_layout = Layout(border="1px lighgray solide", height="35px", padding="8px", width="300px") + label_layout = Layout( + border="1px lighgray solide", height="35px", padding="8px", width="300px" + ) sample_panel = None def __init__(self, sample_panel=None): @@ -900,6 +951,3 @@ def __init__(self, sample_panel=None): # o_norm_handler.load_data() # self.top_object.o_norm_handler = o_norm_handler # self.top_object.o_norm = o_norm_handler.o_norm - - - diff --git a/notebooks/__code/normalization/normalization_with_simplify_selection.py b/notebooks/__code/normalization/normalization_with_simplify_selection.py index c0ce7beb7..03b6cbd08 100755 --- a/notebooks/__code/normalization/normalization_with_simplify_selection.py +++ b/notebooks/__code/normalization/normalization_with_simplify_selection.py @@ -12,16 +12,29 @@ from __code.ipywe import myfileselector from __code.normalization import utilities from __code.normalization.get import Get -from __code.normalization.metadata_handler import METADATA_KEYS, MetadataHandler, MetadataName +from __code.normalization.metadata_handler import ( + METADATA_KEYS, + MetadataHandler, + MetadataName, +) from __code.roi_selection_ui import Interface -from . import ROI_BUTTON_DESCRIPTION, ROI_ICON, TEMPORARY_ROI_BUTTON_DESCRIPTION, TEMPORARY_ROI_ICON +from . import ( + ROI_BUTTON_DESCRIPTION, + ROI_ICON, + TEMPORARY_ROI_BUTTON_DESCRIPTION, + TEMPORARY_ROI_ICON, +) JSON_DEBUGGING = False MAX_DF_COUNTS_ALLOWED = 900 METADATA_ERROR_ALLOWED = 1 -LIST_METADATA_NOT_INSTRUMENT_RELATED = ["filename", "time_stamp", "time_stamp_user_format"] +LIST_METADATA_NOT_INSTRUMENT_RELATED = [ + "filename", + "time_stamp", + "time_stamp_user_format", +] class NormalizationWithSimplifySelection: @@ -95,14 +108,22 @@ def select_sample_folder(self): def retrieve_sample_metadata_from_sample_folder(self, sample_folder): logging.info(f"select sample folder: {sample_folder}") - [list_of_images, _] = file_handler.retrieve_list_of_most_dominant_extension_from_folder(folder=sample_folder) + [list_of_images, _] = ( + file_handler.retrieve_list_of_most_dominant_extension_from_folder( + folder=sample_folder + ) + ) can_we_continue = self.images_files_found_in_list(list_of_images) if can_we_continue: logging.info(f"-> number of images found: {len(list_of_images)}") self.retrieve_sample_metadata(list_of_images) else: logging.info("-> No images found!") - display(HTML('No images found in the folder selected!')) + display( + HTML( + 'No images found in the folder selected!' + ) + ) def images_files_found_in_list(self, list_of_images): for _file in list_of_images: @@ -127,19 +148,27 @@ def retrieve_sample_metadata(self, list_of_images): # self.display_time_range_selection_widgets() def select_ob_folder(self): - self.select_folder(message="open beam", next_function=self.retrieve_ob_metadata()) + self.select_folder( + message="open beam", next_function=self.retrieve_ob_metadata() + ) def retrieve_ob_metadata(self, selected_folder): list_of_ob_files = Get.list_of_tiff_files(folder=selected_folder) - self.ob_metadata_dict = MetadataHandler.retrieve_metadata(list_of_files=list_of_ob_files) + self.ob_metadata_dict = MetadataHandler.retrieve_metadata( + list_of_files=list_of_ob_files + ) def auto_retrieve_ob_metadata(self): logging.info("> auto_retrieve_ob_metadata") folder = os.path.join(self.working_dir, "raw", "ob") logging.info(f"-> folder: {folder}") - list_of_ob_files = file_handler.get_list_of_all_files_in_subfolders(folder=folder, extensions=["tiff", "tif"]) + list_of_ob_files = file_handler.get_list_of_all_files_in_subfolders( + folder=folder, extensions=["tiff", "tif"] + ) logging.info(f"-> nbr of ob files found: {len(list_of_ob_files)}") - self.ob_metadata_dict = MetadataHandler.retrieve_metadata(list_of_files=list_of_ob_files, label="ob") + self.ob_metadata_dict = MetadataHandler.retrieve_metadata( + list_of_files=list_of_ob_files, label="ob" + ) # logging.info(f"ob metadata dict") # logging.info(f"-> {self.ob_metadata_dict}") @@ -155,11 +184,15 @@ def select_folder(self, message="", next_function=None): folder_widget.show() def select_df_folder(self): - self.select_folder(message="dark current", next_function=self.retrieve_df_metadata()) + self.select_folder( + message="dark current", next_function=self.retrieve_df_metadata() + ) def retrieve_df_metadata(self, selected_folder): list_of_df_files = Get.list_of_tiff_files(folder=selected_folder) - self.df_metadata_dict = MetadataHandler.retrieve_metadata(list_of_files=list_of_df_files) + self.df_metadata_dict = MetadataHandler.retrieve_metadata( + list_of_files=list_of_df_files + ) def auto_retrieve_df_metadata(self): folder_df = os.path.join(self.working_dir, "raw", "df") @@ -172,7 +205,9 @@ def auto_retrieve_df_metadata(self): ) list_of_files = list_of_df_files + list_of_dc_files logging.info(f"-> nbr of dc files found: {len(list_of_files)}") - self.df_metadata_dict = MetadataHandler.retrieve_metadata(list_of_files=list_of_files, label="df") + self.df_metadata_dict = MetadataHandler.retrieve_metadata( + list_of_files=list_of_files, label="df" + ) def match_files(self): """This is where the files will be associated with their respective OB, DC by using the metadata""" @@ -201,14 +236,26 @@ def match_ob(self): list_of_sample_acquisition = final_full_master_dict.keys() for _index_ob in list_ob_dict.keys(): - _all_ob_instrument_metadata = Get.get_instrument_metadata_only(list_ob_dict[_index_ob]) - _ob_instrument_metadata = utilities.isolate_instrument_metadata(_all_ob_instrument_metadata) - _acquisition_time = _all_ob_instrument_metadata[MetadataName.EXPOSURE_TIME.value]["value"] + _all_ob_instrument_metadata = Get.get_instrument_metadata_only( + list_ob_dict[_index_ob] + ) + _ob_instrument_metadata = utilities.isolate_instrument_metadata( + _all_ob_instrument_metadata + ) + _acquisition_time = _all_ob_instrument_metadata[ + MetadataName.EXPOSURE_TIME.value + ]["value"] if _acquisition_time in list_of_sample_acquisition: for _config_id in final_full_master_dict[_acquisition_time].keys(): - _sample_metadata_infos = final_full_master_dict[_acquisition_time][_config_id]["metadata_infos"] - if utilities.all_metadata_match(_sample_metadata_infos, _ob_instrument_metadata): - final_full_master_dict[_acquisition_time][_config_id]["list_ob"].append(list_ob_dict[_index_ob]) + _sample_metadata_infos = final_full_master_dict[_acquisition_time][ + _config_id + ]["metadata_infos"] + if utilities.all_metadata_match( + _sample_metadata_infos, _ob_instrument_metadata + ): + final_full_master_dict[_acquisition_time][_config_id][ + "list_ob" + ].append(list_ob_dict[_index_ob]) self.final_full_master_dict = final_full_master_dict @@ -224,20 +271,30 @@ def match_df(self): list_of_sample_acquisition = final_full_master_dict.keys() for _index_df in list_df_dict.keys(): - _all_df_instrument_metadata = Get.get_instrument_metadata_only(list_df_dict[_index_df]) - _df_instrument_metadata = utilities.isolate_instrument_metadata(_all_df_instrument_metadata) - _acquisition_time = _all_df_instrument_metadata[MetadataName.EXPOSURE_TIME.value]["value"] + _all_df_instrument_metadata = Get.get_instrument_metadata_only( + list_df_dict[_index_df] + ) + _df_instrument_metadata = utilities.isolate_instrument_metadata( + _all_df_instrument_metadata + ) + _acquisition_time = _all_df_instrument_metadata[ + MetadataName.EXPOSURE_TIME.value + ]["value"] if _acquisition_time in list_of_sample_acquisition: for _config_id in final_full_master_dict[_acquisition_time].keys(): - _sample_metadata_infos = final_full_master_dict[_acquisition_time][_config_id]["metadata_infos"] + _sample_metadata_infos = final_full_master_dict[_acquisition_time][ + _config_id + ]["metadata_infos"] if utilities.all_metadata_match( _sample_metadata_infos, _df_instrument_metadata, list_key_to_check=[METADATA_KEYS["df"][1].value], ): - final_full_master_dict[_acquisition_time][_config_id]["list_df"].append(list_df_dict[_index_df]) + final_full_master_dict[_acquisition_time][_config_id][ + "list_df" + ].append(list_df_dict[_index_df]) self.final_full_master_dict = final_full_master_dict @@ -253,8 +310,12 @@ def create_master_sample_dict(self): _dict_file_index = sample_metadata_dict[_file_index] _sample_file = _dict_file_index["filename"] - _acquisition_time = _dict_file_index[MetadataName.EXPOSURE_TIME.value]["value"] - _instrument_metadata = utilities.isolate_instrument_metadata(_dict_file_index) + _acquisition_time = _dict_file_index[MetadataName.EXPOSURE_TIME.value][ + "value" + ] + _instrument_metadata = utilities.isolate_instrument_metadata( + _dict_file_index + ) _sample_time_stamp = _dict_file_index["time_stamp"] # find which image was first and which image was last @@ -264,7 +325,9 @@ def create_master_sample_dict(self): last_sample_image = _dict_file_index # first entry or first time seeing that acquisition time - if (len(final_full_master_dict) == 0) or _acquisition_time not in final_full_master_dict.keys(): + if ( + len(final_full_master_dict) == 0 + ) or _acquisition_time not in final_full_master_dict.keys(): _first_images_dict = {"sample": first_sample_image, "ob": {}, "df": {}} _last_images_dict = {"sample": last_sample_image, "ob": {}, "df": {}} _temp_dict = { @@ -275,7 +338,9 @@ def create_master_sample_dict(self): "list_df": [], "time_range_s_selected": {"before": np.nan, "after": np.nan}, "time_range_s": {"before": np.nan, "after": np.nan}, - "metadata_infos": Get.get_instrument_metadata_only(_instrument_metadata), + "metadata_infos": Get.get_instrument_metadata_only( + _instrument_metadata + ), } final_full_master_dict[_acquisition_time] = {} final_full_master_dict[_acquisition_time]["config0"] = _temp_dict @@ -283,25 +348,44 @@ def create_master_sample_dict(self): # check that all the metadata_infos match for the first group of that acquisition time, # otherwise check the next one or create a group if _acquisition_time in final_full_master_dict.keys(): - _dict_for_this_acquisition_time = final_full_master_dict[_acquisition_time] + _dict_for_this_acquisition_time = final_full_master_dict[ + _acquisition_time + ] _found_a_match = False for _config_key in _dict_for_this_acquisition_time.keys(): _config = _dict_for_this_acquisition_time[_config_key] if utilities.all_metadata_match( - metadata_1=_config["metadata_infos"], metadata_2=_instrument_metadata + metadata_1=_config["metadata_infos"], + metadata_2=_instrument_metadata, ): _config["list_sample"].append(_dict_file_index) - _first_images_dict = {"sample": first_sample_image, "ob": {}, "df": {}} - _last_images_dict = {"sample": last_sample_image, "ob": {}, "df": {}} + _first_images_dict = { + "sample": first_sample_image, + "ob": {}, + "df": {}, + } + _last_images_dict = { + "sample": last_sample_image, + "ob": {}, + "df": {}, + } _config["first_images"] = _first_images_dict _config["last_images"] = _last_images_dict _found_a_match = True if not _found_a_match: - _first_images_dict = {"sample": first_sample_image, "ob": {}, "df": {}} - _last_images_dict = {"sample": last_sample_image, "ob": {}, "df": {}} + _first_images_dict = { + "sample": first_sample_image, + "ob": {}, + "df": {}, + } + _last_images_dict = { + "sample": last_sample_image, + "ob": {}, + "df": {}, + } _temp_dict = { "list_sample": [_dict_file_index], @@ -309,16 +393,31 @@ def create_master_sample_dict(self): "last_images": _last_images_dict, "list_ob": [], "list_df": [], - "time_range_s_selected": {"before": np.nan, "after": np.nan}, + "time_range_s_selected": { + "before": np.nan, + "after": np.nan, + }, "time_range_s": {"before": np.nan, "after": np.nan}, - "metadata_infos": Get.get_instrument_metadata_only(_instrument_metadata), + "metadata_infos": Get.get_instrument_metadata_only( + _instrument_metadata + ), } nbr_config = len(_dict_for_this_acquisition_time.keys()) - _dict_for_this_acquisition_time[f"config{nbr_config}"] = _temp_dict + _dict_for_this_acquisition_time[f"config{nbr_config}"] = ( + _temp_dict + ) else: - _first_images_dict = {"sample": first_sample_image, "ob": {}, "df": {}} - _last_images_dict = {"sample": last_sample_image, "ob": {}, "df": {}} + _first_images_dict = { + "sample": first_sample_image, + "ob": {}, + "df": {}, + } + _last_images_dict = { + "sample": last_sample_image, + "ob": {}, + "df": {}, + } _temp_dict = { "list_sample": [_dict_file_index], @@ -328,7 +427,9 @@ def create_master_sample_dict(self): "list_df": [], "time_range_s_selected": {"before": np.nan, "after": np.nan}, "time_range_s": {"before": np.nan, "after": np.nan}, - "metadata_infos": Get.get_instrument_metadata_only(_instrument_metadata), + "metadata_infos": Get.get_instrument_metadata_only( + _instrument_metadata + ), } final_full_master_dict[_acquisition_time] = {} final_full_master_dict[_acquisition_time]["config0"] = _temp_dict @@ -375,19 +476,29 @@ def calculate_time_range(self): for _config in current_acquisition_dict.keys(): current_acquisition_config_dict = current_acquisition_dict[_config] - first_sample_image = current_acquisition_config_dict["first_images"]["sample"] + first_sample_image = current_acquisition_config_dict["first_images"][ + "sample" + ] first_ob_image = current_acquisition_config_dict["first_images"]["ob"] - delta_time_before = first_sample_image.get("time_stamp", 0) - first_ob_image.get("time_stamp", 0) + delta_time_before = first_sample_image.get( + "time_stamp", 0 + ) - first_ob_image.get("time_stamp", 0) _time_range_s_before = delta_time_before if delta_time_before > 0 else 0 last_sample_image = current_acquisition_config_dict["last_images"]["sample"] last_ob_image = current_acquisition_config_dict["last_images"]["ob"] - delta_time_after = last_ob_image.get("time_stamp", 0) - last_sample_image.get("time_stamp", 0) + delta_time_after = last_ob_image.get( + "time_stamp", 0 + ) - last_sample_image.get("time_stamp", 0) _time_range_s_after = delta_time_after if delta_time_after > 0 else 0 - _final_full_master_dict[_acquisition][_config]["time_range_s"]["before"] = _time_range_s_before - _final_full_master_dict[_acquisition][_config]["time_range_s"]["after"] = _time_range_s_after + _final_full_master_dict[_acquisition][_config]["time_range_s"]["before"] = ( + _time_range_s_before + ) + _final_full_master_dict[_acquisition][_config]["time_range_s"]["after"] = ( + _time_range_s_after + ) def display_time_range_selection_widgets(self): _final_full_master_dict = self.final_full_master_dict @@ -396,7 +507,9 @@ def display_time_range_selection_widgets(self): o_get = Get(parent=self) - for _acquisition_index, _acquisition in enumerate(_final_full_master_dict.keys()): + for _acquisition_index, _acquisition in enumerate( + _final_full_master_dict.keys() + ): _dict_of_this_acquisition = _final_full_master_dict[_acquisition] _config_tab = widgets.Tab() @@ -411,8 +524,12 @@ def display_time_range_selection_widgets(self): _current_acquisition_tab_widgets_id[_index] = _config_widgets_id_dict _config_tab_dict[_acquisition_index] = _current_acquisition_tab_widgets_id - _acquisition_tabs.children += (_config_tab,) # add all the config tab to top acquisition tab - _acquisition_tabs.set_title(_acquisition_index, f"Acquisition: {_acquisition}s") + _acquisition_tabs.children += ( + _config_tab, + ) # add all the config tab to top acquisition tab + _acquisition_tabs.set_title( + _acquisition_index, f"Acquisition: {_acquisition}s" + ) _config_tab display(_acquisition_tabs) @@ -423,8 +540,12 @@ def display_time_range_selection_widgets(self): def calculate_max_time_before_and_after_exp_for_this_config(self, dict_config): max_time_before = 0 - first_sample_image_time_stamp = dict_config["first_images"]["sample"]["time_stamp"] - first_ob_image_time_stamp = dict_config["first_images"]["ob"].get("time_stamp", 0) + first_sample_image_time_stamp = dict_config["first_images"]["sample"][ + "time_stamp" + ] + first_ob_image_time_stamp = dict_config["first_images"]["ob"].get( + "time_stamp", 0 + ) if first_ob_image_time_stamp > first_sample_image_time_stamp: max_time_before = 0 @@ -433,7 +554,9 @@ def calculate_max_time_before_and_after_exp_for_this_config(self, dict_config): max_time_after = 0 - last_sample_image_time_stamp = dict_config["last_images"]["sample"]["time_stamp"] + last_sample_image_time_stamp = dict_config["last_images"]["sample"][ + "time_stamp" + ] last_ob_image_time_stamp = dict_config["last_images"]["ob"].get("time_stamp", 0) if last_ob_image_time_stamp < last_sample_image_time_stamp: @@ -445,11 +568,16 @@ def calculate_max_time_before_and_after_exp_for_this_config(self, dict_config): def populate_metadata_table(self, current_config): metadata_config = current_config["metadata_infos"] - table_label = widgets.Label("List of Metadata used to match data set", layout=widgets.Layout(width="30%")) + table_label = widgets.Label( + "List of Metadata used to match data set", + layout=widgets.Layout(width="30%"), + ) table_value = "
" for _key, _value in metadata_config.items(): - table_value += "".format(_value["name"], _value["value"]) + table_value += "".format( + _value["name"], _value["value"] + ) table_value += "
{}{}
{}{}
" table = widgets.HTML(value=table_value) @@ -470,7 +598,9 @@ def update_config_widgets(self, state): visibility = "visible" o_get = Get(parent=self) - [time_before_selected_ui, time_after_selected_ui] = o_get.time_before_and_after_ui_of_this_config() + [time_before_selected_ui, time_after_selected_ui] = ( + o_get.time_before_and_after_ui_of_this_config() + ) experiment_label_ui = o_get.experiment_label_ui_of_this_config() experiment_label_ui.layout.visibility = visibility @@ -489,10 +619,16 @@ def show_or_not_before_and_after_sliders(self): self.calculate_max_time_before_and_after_exp_for_this_config(current_config) ) - slider_before_visibility = "visible" if max_time_elapse_before_experiment > 0 else "hidden" - slider_after_visibility = "visible" if max_time_elapse_after_experiment > 0 else "hidden" + slider_before_visibility = ( + "visible" if max_time_elapse_before_experiment > 0 else "hidden" + ) + slider_after_visibility = ( + "visible" if max_time_elapse_after_experiment > 0 else "hidden" + ) - [time_before_selected_ui, time_after_selected_ui] = o_get.time_before_and_after_ui_of_this_config() + [time_before_selected_ui, time_after_selected_ui] = ( + o_get.time_before_and_after_ui_of_this_config() + ) time_before_selected_ui.layout.visibility = slider_before_visibility time_after_selected_ui.layout.visibility = slider_after_visibility @@ -515,7 +651,9 @@ def update_list_of_files_in_widgets_using_new_time_range(self): # retrieve list of ob and df for this config for this acquisition final_full_master_dict = self.final_full_master_dict - dict_for_this_config = final_full_master_dict[float(acquisition_key)][config_key] + dict_for_this_config = final_full_master_dict[float(acquisition_key)][ + config_key + ] list_ob = dict_for_this_config["list_ob"] # no need to do anything more if user wants to use all the files @@ -524,22 +662,30 @@ def update_list_of_files_in_widgets_using_new_time_range(self): else: # retrieve first and last sample file for this config and for this acquisition - first_sample_image_time_stamp = dict_for_this_config["first_images"]["sample"]["time_stamp"] - last_sample_images_time_stamp = dict_for_this_config["last_images"]["sample"]["time_stamp"] + first_sample_image_time_stamp = dict_for_this_config["first_images"][ + "sample" + ]["time_stamp"] + last_sample_images_time_stamp = dict_for_this_config["last_images"][ + "sample" + ]["time_stamp"] # retrieve time before and after selected - [time_before_selected, time_after_selected] = o_get.time_before_and_after_of_this_config() + [time_before_selected, time_after_selected] = ( + o_get.time_before_and_after_of_this_config() + ) # calculate list of ob that are within that time range list_ob_to_keep = [] for _ob_file in list_ob: _ob_time_stamp = _ob_file["time_stamp"] if (_ob_time_stamp < first_sample_image_time_stamp) and ( - (first_sample_image_time_stamp - _ob_time_stamp) <= np.abs(time_before_selected) + (first_sample_image_time_stamp - _ob_time_stamp) + <= np.abs(time_before_selected) ): list_ob_to_keep.append(_ob_file["filename"]) elif (_ob_time_stamp > last_sample_images_time_stamp) and ( - (_ob_time_stamp - last_sample_images_time_stamp) <= np.abs(time_after_selected) + (_ob_time_stamp - last_sample_images_time_stamp) + <= np.abs(time_after_selected) ): list_ob_to_keep.append(_ob_file["filename"]) @@ -549,9 +695,13 @@ def update_list_of_ob_for_current_config_tab(self, list_ob=[]): o_get = Get(parent=self) [active_acquisition, active_config] = o_get.active_tabs() # short_version_list_ob = NormalizationWithSimplifySelection.keep_basename_only(list_files=list_ob) - self.config_tab_dict[active_acquisition][active_config]["list_of_ob"].options = list_ob + self.config_tab_dict[active_acquisition][active_config][ + "list_of_ob" + ].options = list_ob # select everything by default - self.config_tab_dict[active_acquisition][active_config]["list_of_ob"].value = list_ob + self.config_tab_dict[active_acquisition][active_config][ + "list_of_ob" + ].value = list_ob def update_time_range_message(self, value): o_get = Get(parent=self) @@ -564,7 +714,9 @@ def update_time_range_message(self, value): # "OBs and DFs " \ # "matching the samples images" else: - [time_before_selected, time_after_selected] = o_get.time_before_and_after_of_this_config() + [time_before_selected, time_after_selected] = ( + o_get.time_before_and_after_of_this_config() + ) time_before_selected = np.abs(time_before_selected) @@ -585,7 +737,9 @@ def _format_time(_time_s): str_time_before = _format_time(time_before_selected) str_time_after = _format_time(time_after_selected) - logging.info(f"str_time_before: {time_before_selected} -> {str_time_before}") + logging.info( + f"str_time_before: {time_before_selected} -> {str_time_before}" + ) _message = ( "Use OB taken up to " + str_time_before + " " @@ -594,7 +748,9 @@ def _format_time(_time_s): "after experiment!" ) - time_before_and_after_message_ui = o_get.time_before_and_after_message_ui_of_this_config() + time_before_and_after_message_ui = ( + o_get.time_before_and_after_message_ui_of_this_config() + ) time_before_and_after_message_ui.value = _message def do_you_want_to_combine_changed(self, value): @@ -606,7 +762,9 @@ def do_you_want_to_combine_changed(self, value): o_get = Get(parent=self) [active_acquisition, active_config] = o_get.active_tabs() - self.config_tab_dict[active_acquisition][active_config]["how_to_combine"].disabled = disabled_how_to_combine + self.config_tab_dict[active_acquisition][active_config][ + "how_to_combine" + ].disabled = disabled_how_to_combine self.update_this_config_table() def how_to_combine_changed(self, value): @@ -617,20 +775,44 @@ def update_this_config_table(self): [active_acquisition, active_config] = o_get.active_tabs() table_ui = self.config_tab_dict[active_acquisition][active_config]["table"] - nbr_ob = len(self.config_tab_dict[active_acquisition][active_config]["list_of_ob"].value) - nbr_sample = len(self.config_tab_dict[active_acquisition][active_config]["list_of_sample_runs"].options) - nbr_df = len(self.config_tab_dict[active_acquisition][active_config]["list_of_df"].value) + nbr_ob = len( + self.config_tab_dict[active_acquisition][active_config]["list_of_ob"].value + ) + nbr_sample = len( + self.config_tab_dict[active_acquisition][active_config][ + "list_of_sample_runs" + ].options + ) + nbr_df = len( + self.config_tab_dict[active_acquisition][active_config]["list_of_df"].value + ) - force_combine_disabled_state = self.config_tab_dict[active_acquisition][active_config]["force_combine"].disabled - force_combine_value = self.config_tab_dict[active_acquisition][active_config]["force_combine"].value - how_to_combine_value = self.config_tab_dict[active_acquisition][active_config]["how_to_combine"].value + force_combine_disabled_state = self.config_tab_dict[active_acquisition][ + active_config + ]["force_combine"].disabled + force_combine_value = self.config_tab_dict[active_acquisition][active_config][ + "force_combine" + ].value + how_to_combine_value = self.config_tab_dict[active_acquisition][active_config][ + "how_to_combine" + ].value if force_combine_value == "yes": - description = "OBs will be combined using " + how_to_combine_value + " method!" + description = ( + "OBs will be combined using " + + how_to_combine_value + + " method!" + ) elif force_combine_disabled_state: - description = "OBs will be combined using " + how_to_combine_value + " method!" + description = ( + "OBs will be combined using " + + how_to_combine_value + + " method!" + ) else: - description = "OBs won't be combined! Each sample will use only 1 OB!" + description = ( + "OBs won't be combined! Each sample will use only 1 OB!" + ) html_table = ( f"" @@ -656,15 +838,25 @@ def selection_of_ob_changed(self, value): nbr_ob = len(list_ob_selected) o_get = Get(parent=self) [active_acquisition, active_config] = o_get.active_tabs() - list_sample = self.config_tab_dict[active_acquisition][active_config]["list_of_sample_runs"].options + list_sample = self.config_tab_dict[active_acquisition][active_config][ + "list_of_sample_runs" + ].options nbr_sample = len(list_sample) if nbr_sample == nbr_ob: - self.config_tab_dict[active_acquisition][active_config]["force_combine"].disabled = False - self.config_tab_dict[active_acquisition][active_config]["force_combine_message"].value = "" + self.config_tab_dict[active_acquisition][active_config][ + "force_combine" + ].disabled = False + self.config_tab_dict[active_acquisition][active_config][ + "force_combine_message" + ].value = "" else: - self.config_tab_dict[active_acquisition][active_config]["force_combine"].disabled = True - self.config_tab_dict[active_acquisition][active_config]["force_combine_message"].value = ( + self.config_tab_dict[active_acquisition][active_config][ + "force_combine" + ].disabled = True + self.config_tab_dict[active_acquisition][active_config][ + "force_combine_message" + ].value = ( "INFO: the option to combine or not is disabled as the number of " "sample " "and " @@ -681,20 +873,28 @@ def create_final_json(self): _config_tab_dict = self.config_tab_dict _final_json_dict = {} - for _acquisition_index, _acquisition in enumerate(_final_full_master_dict.keys()): + for _acquisition_index, _acquisition in enumerate( + _final_full_master_dict.keys() + ): _final_json_for_this_acquisition = {} _config_of_this_acquisition = _config_tab_dict[_acquisition_index] _dict_of_this_acquisition = _final_full_master_dict[_acquisition] for _config_index, _config in enumerate(_dict_of_this_acquisition.keys()): - this_config_tab_dict = _config_tab_dict[_acquisition_index][_config_index] + this_config_tab_dict = _config_tab_dict[_acquisition_index][ + _config_index + ] normalize_flag = this_config_tab_dict["use_this_config"] list_sample = this_config_tab_dict["list_of_sample_runs"].options list_ob = this_config_tab_dict["list_of_ob"].value list_df = this_config_tab_dict["list_of_df"].value - force_combine_disabled_state = this_config_tab_dict["force_combine"].disabled # True or false - force_combine_value = this_config_tab_dict["force_combine"].value # 'yes' or 'no' + force_combine_disabled_state = this_config_tab_dict[ + "force_combine" + ].disabled # True or false + force_combine_value = this_config_tab_dict[ + "force_combine" + ].value # 'yes' or 'no' how_to_combine_value = this_config_tab_dict["how_to_combine"].value roi = this_config_tab_dict.get("roi_selected", None) @@ -723,16 +923,24 @@ def create_final_json(self): def roi_button_clicked(self, value): o_get = Get(parent=self) [active_acquisition, active_config] = o_get.active_tabs() - list_sample = self.config_tab_dict[active_acquisition][active_config]["list_of_sample_runs"].options + list_sample = self.config_tab_dict[active_acquisition][active_config][ + "list_of_sample_runs" + ].options self.config_tab_dict[active_acquisition][active_config][ "select_roi_button" ].description = TEMPORARY_ROI_BUTTON_DESCRIPTION - self.config_tab_dict[active_acquisition][active_config]["select_roi_button"].icon = TEMPORARY_ROI_ICON - self.config_tab_dict[active_acquisition][active_config]["select_roi_button"].disabled = True + self.config_tab_dict[active_acquisition][active_config][ + "select_roi_button" + ].icon = TEMPORARY_ROI_ICON + self.config_tab_dict[active_acquisition][active_config][ + "select_roi_button" + ].disabled = True o_gui = Interface( - list_of_files=list_sample, callback=self.returning_from_roi_selection, display_info_message=False + list_of_files=list_sample, + callback=self.returning_from_roi_selection, + display_info_message=False, ) o_gui.show() QtGui.QGuiApplication.processEvents() @@ -740,12 +948,18 @@ def roi_button_clicked(self, value): def returning_from_roi_selection(self, roi_selected): o_get = Get(parent=self) [active_acquisition, active_config] = o_get.active_tabs() - self.config_tab_dict[active_acquisition][active_config]["roi_selected"] = roi_selected + self.config_tab_dict[active_acquisition][active_config]["roi_selected"] = ( + roi_selected + ) self.config_tab_dict[active_acquisition][active_config][ "select_roi_button" ].description = ROI_BUTTON_DESCRIPTION - self.config_tab_dict[active_acquisition][active_config]["select_roi_button"].icon = ROI_ICON - self.config_tab_dict[active_acquisition][active_config]["select_roi_button"].disabled = False + self.config_tab_dict[active_acquisition][active_config][ + "select_roi_button" + ].icon = ROI_ICON + self.config_tab_dict[active_acquisition][active_config][ + "select_roi_button" + ].disabled = False def normalization_recap(self): """this will show all the config that will be run and if they have the minimum requirements or not, @@ -798,7 +1012,7 @@ def normalization_recap(self): display(table_ui) def select_output_folder(self): - #self.output_folder_ui = myfileselector.MyFileSelectorPanel( + # self.output_folder_ui = myfileselector.MyFileSelectorPanel( self.output_folder_ui = myfileselector.FileSelectorPanelWithJumpFolders( instruction="select where to create the " + "normalized folders", start_dir=self.working_dir, @@ -809,7 +1023,7 @@ def select_output_folder(self): newdir_toolbar_button=True, ) # display(self.output_folder_ui) - + def normalization(self, output_folder): display( HTML( @@ -825,8 +1039,14 @@ def normalization(self, output_folder): horizontal_layout = widgets.HBox( [ - widgets.Label("Normalization progress", layout=widgets.Layout(width="20%")), - widgets.IntProgress(max=number_of_normalization + 1, value=0, layout=widgets.Layout(width="50%")), + widgets.Label( + "Normalization progress", layout=widgets.Layout(width="20%") + ), + widgets.IntProgress( + max=number_of_normalization + 1, + value=0, + layout=widgets.Layout(width="50%"), + ), ] ) normalization_progress = horizontal_layout.children[1] @@ -848,13 +1068,17 @@ def normalization(self, output_folder): continue list_sample = _current_config["list_sample"] - full_output_normalization_folder_name = utilities.make_full_output_normalization_folder_name( - output_folder=output_folder, - first_sample_file_name=list_sample[0], - name_acquisition=_name_acquisition, - name_config=_name_config, + full_output_normalization_folder_name = ( + utilities.make_full_output_normalization_folder_name( + output_folder=output_folder, + first_sample_file_name=list_sample[0], + name_acquisition=_name_acquisition, + name_config=_name_config, + ) + ) + list_full_output_normalization_folder_name.append( + full_output_normalization_folder_name ) - list_full_output_normalization_folder_name.append(full_output_normalization_folder_name) list_df = _current_config["list_df"] o_load = Normalization() @@ -872,7 +1096,12 @@ def normalization(self, output_folder): list_roi = [] for _key in roi.keys(): _roi_item = roi[_key] - _roi = ROI(x0=_roi_item["x0"], y0=_roi_item["y0"], x1=_roi_item["x1"], y1=_roi_item["y1"]) + _roi = ROI( + x0=_roi_item["x0"], + y0=_roi_item["y0"], + x1=_roi_item["x1"], + y1=_roi_item["y1"], + ) list_roi.append(_roi) else: list_roi = None @@ -885,16 +1114,28 @@ def normalization(self, output_folder): else: o_load.normalization(force_median_ob=True, roi=list_roi) - o_load.export(folder=full_output_normalization_folder_name, file_type="tif") + o_load.export( + folder=full_output_normalization_folder_name, file_type="tif" + ) del o_load normalization_progress.value += 1 horizontal_layout.close() - display(HTML('The following folders have been created:')) + display( + HTML( + 'The following folders have been created:' + ) + ) for _folder in list_full_output_normalization_folder_name: _folder = _folder if _folder else "None" - display(HTML(' -> ' + _folder + "")) + display( + HTML( + ' -> ' + + _folder + + "" + ) + ) - print("Normalization is done!") \ No newline at end of file + print("Normalization is done!") diff --git a/notebooks/__code/normalization/utilities.py b/notebooks/__code/normalization/utilities.py index 7b3142fd2..6095574bc 100755 --- a/notebooks/__code/normalization/utilities.py +++ b/notebooks/__code/normalization/utilities.py @@ -14,8 +14,12 @@ def make_full_output_normalization_folder_name( ): basename_sample_folder = os.path.basename(os.path.dirname(first_sample_file_name)) basename_sample_folder += f"_{name_acquisition}_{name_config}" - full_basename_sample_folder = os.path.abspath(os.path.join(output_folder, basename_sample_folder)) - full_basename_sample_folder = make_or_increment_folder_name(full_basename_sample_folder) + full_basename_sample_folder = os.path.abspath( + os.path.join(output_folder, basename_sample_folder) + ) + full_basename_sample_folder = make_or_increment_folder_name( + full_basename_sample_folder + ) return full_basename_sample_folder @@ -78,7 +82,12 @@ def all_metadata_match(metadata_1={}, metadata_2={}, list_key_to_check=None): for _key in list_key: try: - if np.abs(float(metadata_1[_key]["value"]) - float(metadata_2[_key]["value"])) > METADATA_ERROR_ALLOWED: + if ( + np.abs( + float(metadata_1[_key]["value"]) - float(metadata_2[_key]["value"]) + ) + > METADATA_ERROR_ALLOWED + ): return False except ValueError: if metadata_1[_key]["value"] != metadata_2[_key]["value"]: @@ -129,7 +138,10 @@ def isolate_infos_from_file_index(index=-1, dictionary=None, all_keys=False): for _image in dictionary["list_images"].keys(): _time_image = dictionary["list_time_stamp"][index] _user_format_time_image = dictionary["list_time_stamp_user_format"][index] - result_dictionary[_image] = {"system_time": _time_image, "user_format_time": _user_format_time_image} + result_dictionary[_image] = { + "system_time": _time_image, + "user_format_time": _user_format_time_image, + } else: _image = dictionary["list_images"][index] _time_image = dictionary["list_time_stamp"][index] diff --git a/notebooks/__code/normalization_batch.py b/notebooks/__code/normalization_batch.py index 30475ccaf..db1b2cdca 100755 --- a/notebooks/__code/normalization_batch.py +++ b/notebooks/__code/normalization_batch.py @@ -83,7 +83,14 @@ class Panel: df_panel = None top_object = None - def __init__(self, prev_button=False, next_button=True, state="sample", working_dir="", top_object=None): + def __init__( + self, + prev_button=False, + next_button=True, + state="sample", + working_dir="", + top_object=None, + ): self.prev_button = prev_button self.next_button = next_button self.state = state @@ -124,7 +131,10 @@ def __top_panel(self): title_ui = widgets.HBox( [ widgets.Label("Instructions:", layout=widgets.Layout(width="20%")), - widgets.Label("Select Samples Images and click NEXT", layout=widgets.Layout(width="50%")), + widgets.Label( + "Select Samples Images and click NEXT", + layout=widgets.Layout(width="50%"), + ), ] ) @@ -134,7 +144,9 @@ def __top_panel(self): widgets.Label("None", layout=widgets.Layout(width="50%")), ] ) - self.title = title_ui.children[1] # "Select [Samples/OB/DF] Images [and click NEXT] + self.title = title_ui.children[ + 1 + ] # "Select [Samples/OB/DF] Images [and click NEXT] self.label = label_ui.children[1] # number of samples selected self.top_panel = widgets.VBox(children=[title_ui, label_ui], layout=self.layout) @@ -158,7 +170,9 @@ def __bottom_panel(self): self.prev_button_ui.on_click(self.prev_button_clicked) list_ui.append(self.prev_button_ui) - self.current_state_label_ui = widgets.Label(" ", layout=widgets.Layout(width="70%")) + self.current_state_label_ui = widgets.Label( + " ", layout=widgets.Layout(width="70%") + ) list_ui.append(self.current_state_label_ui) if self.next_button: @@ -194,7 +208,9 @@ def nextStep(self): class WizardPanel: - label_layout = Layout(border="1px lighgray solide", height="35px", padding="8px", width="300px") + label_layout = Layout( + border="1px lighgray solide", height="35px", padding="8px", width="300px" + ) sample_panel = None def __init__(self, sample_panel=None): @@ -210,9 +226,14 @@ class SampleSelectionPanel(Panel): files = None o_norm = None - def __init__(self, prev_button=False, next_button=True, working_dir="", top_object=None): + def __init__( + self, prev_button=False, next_button=True, working_dir="", top_object=None + ): super(SampleSelectionPanel, self).__init__( - prev_button=prev_button, next_button=next_button, working_dir=working_dir, top_object=top_object + prev_button=prev_button, + next_button=next_button, + working_dir=working_dir, + top_object=top_object, ) def next_button_clicked(self, event): @@ -230,13 +251,17 @@ def __init__(self, working_dir="", top_object=None): def next_button_clicked(self, event): self.remove() - _panel = DFSelectionPanel(working_dir=self.working_dir, top_object=self.top_object) + _panel = DFSelectionPanel( + working_dir=self.working_dir, top_object=self.top_object + ) _panel.init_ui(files=self.files) _panel.show() def prev_button_clicked(self, event): self.remove() - _panel = SampleSelectionPanel(working_dir=self.working_dir, top_object=self.top_object) + _panel = SampleSelectionPanel( + working_dir=self.working_dir, top_object=self.top_object + ) _panel.init_ui(files=self.files) _panel.show() @@ -245,18 +270,26 @@ class DFSelectionPanel(Panel): def __init__(self, working_dir="", top_object=None): self.working_dir = working_dir super(DFSelectionPanel, self).__init__( - prev_button=True, next_button=True, state="df", working_dir=working_dir, top_object=top_object + prev_button=True, + next_button=True, + state="df", + working_dir=working_dir, + top_object=top_object, ) def prev_button_clicked(self, event): self.remove() - _panel = OBSelectionPanel(working_dir=self.working_dir, top_object=self.top_object) + _panel = OBSelectionPanel( + working_dir=self.working_dir, top_object=self.top_object + ) _panel.init_ui(files=self.files) _panel.show() def next_button_clicked(self, event): self.remove() - o_norm_handler = NormalizationHandler(files=self.files, working_dir=self.working_dir) + o_norm_handler = NormalizationHandler( + files=self.files, working_dir=self.working_dir + ) o_norm_handler.load_data() self.top_object.o_norm_handler = o_norm_handler self.top_object.o_norm = o_norm_handler.o_norm @@ -324,9 +357,15 @@ def display_file_selector_from_home(ev): self.hbox = widgets.HBox( [ widgets.Button( - description=f"Jump to {ipts} Shared Folder", button_style="success", layout=button_layout + description=f"Jump to {ipts} Shared Folder", + button_style="success", + layout=button_layout, + ), + widgets.Button( + description="Jump to My Home Folder", + button_style="success", + layout=button_layout, ), - widgets.Button(description="Jump to My Home Folder", button_style="success", layout=button_layout), ] ) go_to_shared_button_ui = self.hbox.children[0] @@ -352,8 +391,12 @@ def remove_buttons(ev): self.output_folder_ui.show() def export(self, rois={}): - base_folder = os.path.basename(os.path.dirname(self.list_file_names[0])) + "_normalized" - output_folder = os.path.abspath(os.path.join(self.output_folder_ui.selected, base_folder)) + base_folder = ( + os.path.basename(os.path.dirname(self.list_file_names[0])) + "_normalized" + ) + output_folder = os.path.abspath( + os.path.join(self.output_folder_ui.selected, base_folder) + ) utilities.make_dir(dir=output_folder) self.normalized(rois=rois, output_folder=output_folder) diff --git a/notebooks/__code/normalization_resonance/normalization_for_timepix.py b/notebooks/__code/normalization_resonance/normalization_for_timepix.py index 245f8c22a..c4d9b694c 100644 --- a/notebooks/__code/normalization_resonance/normalization_for_timepix.py +++ b/notebooks/__code/normalization_resonance/normalization_for_timepix.py @@ -73,7 +73,9 @@ def _worker(fl): return (imread(fl).astype(LOAD_DTYPE)).swapaxes(0, 1) -def load_data_using_multithreading(list_tif: list = None, combine_tof: bool = False) -> np.ndarray: +def load_data_using_multithreading( + list_tif: list = None, combine_tof: bool = False +) -> np.ndarray: """load data using multithreading""" with mp.Pool(processes=40) as pool: data = pool.map(_worker, list_tif) @@ -92,7 +94,9 @@ def retrieve_list_of_tif(folder: str) -> list: def create_x_axis_file( - lambda_array: np.ndarray = None, energy_array: np.ndarray = None, output_folder: str = "./" + lambda_array: np.ndarray = None, + energy_array: np.ndarray = None, + output_folder: str = "./", ) -> str: """create x axis file with lambda, energy and tof arrays""" x_axis_data = { @@ -160,10 +164,16 @@ def normalization_with_list_of_runs( export_corrected_stack_of_sample_data = export_mode.get("sample_stack", False) export_corrected_stack_of_ob_data = export_mode.get("ob_stack", False) - export_corrected_stack_of_normalized_data = export_mode.get("normalized_stack", False) - export_corrected_integrated_sample_data = export_mode.get("sample_integrated", False) + export_corrected_stack_of_normalized_data = export_mode.get( + "normalized_stack", False + ) + export_corrected_integrated_sample_data = export_mode.get( + "sample_integrated", False + ) export_corrected_integrated_ob_data = export_mode.get("ob_integrated", False) - export_corrected_integrated_normalized_data = export_mode.get("normalized_integrated", False) + export_corrected_integrated_normalized_data = export_mode.get( + "normalized_integrated", False + ) export_x_axis = export_mode.get("x_axis", True) logging.info(f"{export_corrected_stack_of_sample_data = }") @@ -181,13 +191,18 @@ def normalization_with_list_of_runs( nexus_root_path=nexus_path, ) ob_master_dict, ob_status_metadata = create_master_dict( - list_run_numbers=ob_run_numbers, data_type=DataType.ob, instrument=instrument, nexus_root_path=nexus_path + list_run_numbers=ob_run_numbers, + data_type=DataType.ob, + instrument=instrument, + nexus_root_path=nexus_path, ) # only for SNAP if instrument == "SNAP": for _run in sample_master_dict.keys(): - sample_master_dict[_run][MasterDictKeys.detector_delay_us] = detector_delay_us + sample_master_dict[_run][MasterDictKeys.detector_delay_us] = ( + detector_delay_us + ) for _run in ob_master_dict.keys(): ob_master_dict[_run][MasterDictKeys.detector_delay_us] = detector_delay_us @@ -197,25 +212,32 @@ def normalization_with_list_of_runs( logging.info(f"loading ob# {_ob_run_number} ... ") if verbose: display(HTML(f"Loading ob# {_ob_run_number} ...")) - ob_master_dict[_ob_run_number][MasterDictKeys.data] = load_data_using_multithreading( - ob_master_dict[_ob_run_number][MasterDictKeys.list_tif], combine_tof=False + ob_master_dict[_ob_run_number][MasterDictKeys.data] = ( + load_data_using_multithreading( + ob_master_dict[_ob_run_number][MasterDictKeys.list_tif], + combine_tof=False, + ) ) logging.info(f"ob# {_ob_run_number} loaded!") logging.info(f"{ob_master_dict[_ob_run_number][MasterDictKeys.data].shape = }") if verbose: display(HTML(f"ob# {_ob_run_number} loaded!")) - display(HTML(f"{ob_master_dict[_ob_run_number][MasterDictKeys.data].shape = }")) + display( + HTML(f"{ob_master_dict[_ob_run_number][MasterDictKeys.data].shape = }") + ) if proton_charge_flag: normalized_by_proton_charge = ( - sample_status_metadata.all_proton_charge_found and ob_status_metadata.all_proton_charge_found + sample_status_metadata.all_proton_charge_found + and ob_status_metadata.all_proton_charge_found ) else: normalized_by_proton_charge = False if shutter_counts_flag: normalized_by_shutter_counts = ( - sample_status_metadata.all_shutter_counts_found and ob_status_metadata.all_shutter_counts_found + sample_status_metadata.all_shutter_counts_found + and ob_status_metadata.all_shutter_counts_found ) else: normalized_by_shutter_counts = False @@ -248,7 +270,9 @@ def normalization_with_list_of_runs( export_corrected_stack_of_ob_data, export_corrected_integrated_ob_data, ob_data_combined, - spectra_file_name=ob_master_dict[_ob_run_number][MasterDictKeys.spectra_file_name], + spectra_file_name=ob_master_dict[_ob_run_number][ + MasterDictKeys.spectra_file_name + ], ) # load sample images @@ -256,22 +280,33 @@ def normalization_with_list_of_runs( logging.info(f"loading sample# {_sample_run_number} ... ") if verbose: display(HTML(f"Loading sample# {_sample_run_number} ...")) - sample_master_dict[_sample_run_number][MasterDictKeys.data] = load_data_using_multithreading( - sample_master_dict[_sample_run_number][MasterDictKeys.list_tif], combine_tof=False + sample_master_dict[_sample_run_number][MasterDictKeys.data] = ( + load_data_using_multithreading( + sample_master_dict[_sample_run_number][MasterDictKeys.list_tif], + combine_tof=False, + ) ) logging.info(f"sample# {_sample_run_number} loaded!") - logging.info(f"{sample_master_dict[_sample_run_number][MasterDictKeys.data].shape = }") + logging.info( + f"{sample_master_dict[_sample_run_number][MasterDictKeys.data].shape = }" + ) if verbose: display(HTML(f"sample# {_sample_run_number} loaded!")) - display(HTML(f"{sample_master_dict[_sample_run_number][MasterDictKeys.data].shape = }")) + display( + HTML( + f"{sample_master_dict[_sample_run_number][MasterDictKeys.data].shape = }" + ) + ) if correct_chips_alignment_flag: logging.info("Correcting chips alignment ...") if verbose: display(HTML("Correcting chips alignment ...")) for _sample_run_number in sample_master_dict.keys(): - sample_master_dict[_sample_run_number][MasterDictKeys.data] = correct_chips_alignment( - sample_master_dict[_sample_run_number][MasterDictKeys.data] + sample_master_dict[_sample_run_number][MasterDictKeys.data] = ( + correct_chips_alignment( + sample_master_dict[_sample_run_number][MasterDictKeys.data] + ) ) logging.info("Chips alignment corrected!") if verbose: @@ -303,17 +338,25 @@ def normalization_with_list_of_runs( logging.info("**********************************") if normalized_by_proton_charge: - proton_charge = sample_master_dict[_sample_run_number][MasterDictKeys.proton_charge] + proton_charge = sample_master_dict[_sample_run_number][ + MasterDictKeys.proton_charge + ] _sample_data = _sample_data / proton_charge if normalized_by_shutter_counts: list_shutter_values_for_each_image = produce_list_shutter_for_each_image( - list_time_spectra=ob_master_dict[_ob_run_number][MasterDictKeys.list_spectra], - list_shutter_counts=sample_master_dict[_sample_run_number][MasterDictKeys.shutter_counts], + list_time_spectra=ob_master_dict[_ob_run_number][ + MasterDictKeys.list_spectra + ], + list_shutter_counts=sample_master_dict[_sample_run_number][ + MasterDictKeys.shutter_counts + ], ) sample_data = [] - for _sample, _shutter_value in zip(_sample_data, list_shutter_values_for_each_image, strict=False): + for _sample, _shutter_value in zip( + _sample_data, list_shutter_values_for_each_image, strict=False + ): sample_data.append(_sample / _shutter_value) _sample_data = np.array(sample_data) @@ -323,14 +366,19 @@ def normalization_with_list_of_runs( logging.info(f"{ob_data_combined.dtype = }") # export sample data after correction if requested - if export_corrected_stack_of_sample_data or export_corrected_integrated_sample_data: + if ( + export_corrected_stack_of_sample_data + or export_corrected_integrated_sample_data + ): export_sample_images( output_folder, export_corrected_stack_of_sample_data, export_corrected_integrated_sample_data, _sample_run_number, _sample_data, - spectra_file_name=sample_master_dict[_sample_run_number][MasterDictKeys.spectra_file_name], + spectra_file_name=sample_master_dict[_sample_run_number][ + MasterDictKeys.spectra_file_name + ], ) # _sample_data = np.divide(_sample_data, ob_data_combined, out=np.zeros_like(_sample_data), where=ob_data_combined!=0) @@ -350,8 +398,12 @@ def normalization_with_list_of_runs( logging.info(f"{normalized_data[_sample_run_number].shape = }") logging.info(f"{normalized_data[_sample_run_number].dtype = }") - detector_delay_us = sample_master_dict[_sample_run_number][MasterDictKeys.detector_delay_us] - time_spectra = sample_master_dict[_sample_run_number][MasterDictKeys.list_spectra] + detector_delay_us = sample_master_dict[_sample_run_number][ + MasterDictKeys.detector_delay_us + ] + time_spectra = sample_master_dict[_sample_run_number][ + MasterDictKeys.list_spectra + ] lambda_array = convert_array_from_time_to_lambda( time_array=time_spectra, @@ -374,11 +426,15 @@ def normalization_with_list_of_runs( if preview: # display preview of normalized data - fig, axs1 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) + fig, axs1 = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) sample_data_integrated = np.nanmean(_sample_data, axis=0) im0 = axs1[0].imshow(sample_data_integrated, cmap="gray") plt.colorbar(im0, ax=axs1[0]) - axs1[0].set_title(f"Sample data: {_sample_run_number} | detector delay: {detector_delay_us:.2f} us") + axs1[0].set_title( + f"Sample data: {_sample_run_number} | detector delay: {detector_delay_us:.2f} us" + ) sample_integrated1 = np.nansum(_sample_data, axis=1) sample_integrated = np.nansum(sample_integrated1, axis=1) @@ -387,7 +443,9 @@ def normalization_with_list_of_runs( axs1[1].set_ylabel("mean of full image") plt.tight_layout - fig, axs2 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) + fig, axs2 = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) ob_data_integrated = np.nanmean(ob_data_combined, axis=0) im1 = axs2[0].imshow(ob_data_integrated, cmap="gray") plt.colorbar(im1, ax=axs2[0]) @@ -400,8 +458,12 @@ def normalization_with_list_of_runs( axs2[1].set_ylabel("mean of full image") plt.tight_layout() - fig, axs3 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) - normalized_data_integrated = np.nanmean(normalized_data[_sample_run_number], axis=0) + fig, axs3 = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) + normalized_data_integrated = np.nanmean( + normalized_data[_sample_run_number], axis=0 + ) im2 = axs3[0].imshow(normalized_data_integrated, cmap="gray") plt.colorbar(im2, ax=axs3[0]) axs3[0].set_title(f"Normalized data {_sample_run_number}") @@ -413,7 +475,9 @@ def normalization_with_list_of_runs( axs3[1].set_ylabel("mean of full image") plt.tight_layout() - fig, axs4 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) + fig, axs4 = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) axs4[0].plot(lambda_array, profile, "*") axs4[0].set_xlabel("Lambda (A)") axs4[0].set_ylabel("mean of full image") @@ -426,24 +490,36 @@ def normalization_with_list_of_runs( plt.show() - if export_corrected_integrated_normalized_data or export_corrected_stack_of_normalized_data: + if ( + export_corrected_integrated_normalized_data + or export_corrected_stack_of_normalized_data + ): # make up new output folder name list_ob_runs = list(ob_master_dict.keys()) - str_ob_runs = "_".join([str(_ob_run_number) for _ob_run_number in list_ob_runs]) + str_ob_runs = "_".join( + [str(_ob_run_number) for _ob_run_number in list_ob_runs] + ) full_output_folder = os.path.join( - output_folder, f"normalized_sample_{_sample_run_number}_obs_{str_ob_runs}" + output_folder, + f"normalized_sample_{_sample_run_number}_obs_{str_ob_runs}", ) # issue for WEI here ! full_output_folder = os.path.abspath(full_output_folder) os.makedirs(full_output_folder, exist_ok=True) if export_corrected_integrated_normalized_data: # making up the integrated sample data - sample_data_integrated = np.nanmean(normalized_data[_sample_run_number], axis=0) + sample_data_integrated = np.nanmean( + normalized_data[_sample_run_number], axis=0 + ) full_file_name = os.path.join(full_output_folder, "integrated.tif") - logging.info(f"\t -> Exporting integrated normalized data to {full_file_name} ...") + logging.info( + f"\t -> Exporting integrated normalized data to {full_file_name} ..." + ) make_tiff(data=sample_data_integrated, filename=full_file_name) - logging.info(f"\t -> Exporting integrated normalized data to {full_file_name} is done!") + logging.info( + f"\t -> Exporting integrated normalized data to {full_file_name} is done!" + ) if export_corrected_stack_of_normalized_data: output_stack_folder = os.path.join(full_output_folder, "stack") @@ -451,12 +527,20 @@ def normalization_with_list_of_runs( os.makedirs(output_stack_folder, exist_ok=True) for _index, _data in enumerate(normalized_data[_sample_run_number]): - _output_file = os.path.join(output_stack_folder, f"image{_index:04d}.tif") + _output_file = os.path.join( + output_stack_folder, f"image{_index:04d}.tif" + ) make_tiff(data=_data, filename=_output_file) - logging.info(f"\t -> Exporting normalized data to {output_stack_folder} is done!") + logging.info( + f"\t -> Exporting normalized data to {output_stack_folder} is done!" + ) print(f"Exported normalized tif images are in: {output_stack_folder}!") - spectra_file = sample_master_dict[_sample_run_number][MasterDictKeys.spectra_file_name] - logging.info(f"Exported time spectra file {spectra_file} to {output_stack_folder}!") + spectra_file = sample_master_dict[_sample_run_number][ + MasterDictKeys.spectra_file_name + ] + logging.info( + f"Exported time spectra file {spectra_file} to {output_stack_folder}!" + ) shutil.copy(spectra_file, output_stack_folder) # create x-axis file @@ -475,7 +559,9 @@ def get_detector_offset_from_nexus(nexus_path: str) -> float: """get the detector offset from the nexus file""" with h5py.File(nexus_path, "r") as hdf5_data: try: - detector_offset_micros = hdf5_data["entry"]["DASlogs"]["BL10:Det:TH:DSPT1:TIDelay"]["value"][0] + detector_offset_micros = hdf5_data["entry"]["DASlogs"][ + "BL10:Det:TH:DSPT1:TIDelay" + ]["value"][0] except KeyError: detector_offset_micros = None return detector_offset_micros @@ -504,7 +590,9 @@ def export_sample_images( make_tiff(data=_data, filename=_output_file) logging.info(f"\t -> Exporting sample data to {output_stack_folder} is done!") shutil.copy(spectra_file_name, os.path.join(output_stack_folder)) - logging.info(f"\t -> Exporting spectra file {spectra_file_name} to {output_stack_folder} is done!") + logging.info( + f"\t -> Exporting spectra file {spectra_file_name} to {output_stack_folder} is done!" + ) if export_corrected_integrated_sample_data: # making up the integrated sample data @@ -512,7 +600,9 @@ def export_sample_images( full_file_name = os.path.join(sample_output_folder, "integrated.tif") logging.info(f"\t -> Exporting integrated sample data to {full_file_name} ...") make_tiff(data=sample_data_integrated, filename=full_file_name) - logging.info(f"\t -> Exporting integrated sample data to {full_file_name} is done!") + logging.info( + f"\t -> Exporting integrated sample data to {full_file_name} is done!" + ) display(HTML(f"Created folder {output_stack_folder} for sample outputs!")) @@ -529,10 +619,13 @@ def export_ob_images( logging.info(f"> Exporting combined ob images to {output_folder} ...") logging.info(f"\t{ob_run_numbers = }") list_ob_runs_number_only = [ - str(isolate_run_number_from_full_path(_ob_run_number)) for _ob_run_number in ob_run_numbers + str(isolate_run_number_from_full_path(_ob_run_number)) + for _ob_run_number in ob_run_numbers ] if len(list_ob_runs_number_only) == 1: - ob_output_folder = os.path.join(output_folder, f"ob_{list_ob_runs_number_only[0]}") + ob_output_folder = os.path.join( + output_folder, f"ob_{list_ob_runs_number_only[0]}" + ) else: str_list_ob_runs = "_".join(list_ob_runs_number_only) ob_output_folder = os.path.join(output_folder, f"ob_{str_list_ob_runs}") @@ -561,12 +654,16 @@ def export_ob_images( logging.info(f"\t -> Exporting ob data to {output_stack_folder} is done!") # copy spectra file to the output folder shutil.copy(spectra_file_name, os.path.join(output_stack_folder)) - logging.info(f"\t -> Exported spectra file {spectra_file_name} to {output_stack_folder}!") + logging.info( + f"\t -> Exported spectra file {spectra_file_name} to {output_stack_folder}!" + ) display(HTML(f"Created folder {output_stack_folder} for OB outputs!")) -def normalization(sample_folder=None, ob_folder=None, output_folder="./", verbose=False): +def normalization( + sample_folder=None, ob_folder=None, output_folder="./", verbose=False +): pass @@ -646,7 +743,9 @@ def update_dict_with_shutter_counts(master_dict: dict) -> tuple[dict, bool]: if _value == "0": break list_shutter_counts.append(float(_value)) - master_dict[run_number][MasterDictKeys.shutter_counts] = list_shutter_counts + master_dict[run_number][MasterDictKeys.shutter_counts] = ( + list_shutter_counts + ) return master_dict, status_all_shutter_counts_found @@ -677,7 +776,9 @@ def update_dict_with_proton_charge(master_dict: dict) -> tuple[dict, bool]: _nexus_path = master_dict[_run_number][MasterDictKeys.nexus_path] try: with h5py.File(_nexus_path, "r") as hdf5_data: - proton_charge = hdf5_data["entry"][MasterDictKeys.proton_charge][0] / 1e12 + proton_charge = ( + hdf5_data["entry"][MasterDictKeys.proton_charge][0] / 1e12 + ) except KeyError: proton_charge = None status_all_proton_charge_found = False @@ -688,7 +789,9 @@ def update_dict_with_proton_charge(master_dict: dict) -> tuple[dict, bool]: def update_dict_with_list_of_images(master_dict: dict) -> dict: """update the master dict with list of images""" for _run_number in master_dict.keys(): - list_tif = retrieve_list_of_tif(master_dict[_run_number][MasterDictKeys.data_path]) + list_tif = retrieve_list_of_tif( + master_dict[_run_number][MasterDictKeys.data_path] + ) master_dict[_run_number][MasterDictKeys.list_tif] = list_tif @@ -699,7 +802,9 @@ def get_list_run_number(data_folder: str) -> list: return list_run_number -def update_dict_with_nexus_full_path(nexus_root_path: str, instrument: str, master_dict: dict) -> dict: +def update_dict_with_nexus_full_path( + nexus_root_path: str, instrument: str, master_dict: dict +) -> dict: """create dict of nexus path for each run number""" for run_number in master_dict.keys(): master_dict[run_number][MasterDictKeys.nexus_path] = os.path.join( @@ -717,7 +822,9 @@ def update_with_nexus_metadata(master_dict: dict) -> dict: def update_dict_with_data_full_path(data_root_path: str, master_dict: dict) -> dict: """create dict of data path for each run number""" for run_number in master_dict.keys(): - master_dict[run_number][MasterDictKeys.data_path] = os.path.join(data_root_path, f"Run_{run_number}") + master_dict[run_number][MasterDictKeys.data_path] = os.path.join( + data_root_path, f"Run_{run_number}" + ) def create_master_dict( @@ -775,7 +882,9 @@ def create_master_dict( return master_dict, status_metadata -def produce_list_shutter_for_each_image(list_time_spectra: list = None, list_shutter_counts: list = None) -> list: +def produce_list_shutter_for_each_image( + list_time_spectra: list = None, list_shutter_counts: list = None +) -> list: """produce list of shutter counts for each image""" delat_time_spectra = list_time_spectra[1] - list_time_spectra[0] @@ -785,18 +894,26 @@ def produce_list_shutter_for_each_image(list_time_spectra: list = None, list_shu logging.info(f"\t{list_index_jump = }") logging.info(f"\t{list_shutter_counts = }") - list_shutter_values_for_each_image = np.zeros(len(list_time_spectra), dtype=np.float32) + list_shutter_values_for_each_image = np.zeros( + len(list_time_spectra), dtype=np.float32 + ) if len(list_shutter_counts) == 1: # resonance mode list_shutter_values_for_each_image.fill(list_shutter_counts[0]) return list_shutter_values_for_each_image - list_shutter_values_for_each_image[0 : list_index_jump[0] + 1].fill(list_shutter_counts[0]) + list_shutter_values_for_each_image[0 : list_index_jump[0] + 1].fill( + list_shutter_counts[0] + ) for _index in range(1, len(list_index_jump)): _start = list_index_jump[_index - 1] _end = list_index_jump[_index] - list_shutter_values_for_each_image[_start + 1 : _end + 1].fill(list_shutter_counts[_index]) + list_shutter_values_for_each_image[_start + 1 : _end + 1].fill( + list_shutter_counts[_index] + ) - list_shutter_values_for_each_image[list_index_jump[-1] + 1 :] = list_shutter_counts[-1] + list_shutter_values_for_each_image[list_index_jump[-1] + 1 :] = list_shutter_counts[ + -1 + ] return list_shutter_values_for_each_image @@ -817,7 +934,9 @@ def combine_ob_images( for _ob_run_number in ob_master_dict.keys(): logging.info(f"Combining ob# {_ob_run_number} ...") - ob_data = np.array(ob_master_dict[_ob_run_number][MasterDictKeys.data], dtype=np.float32) + ob_data = np.array( + ob_master_dict[_ob_run_number][MasterDictKeys.data], dtype=np.float32 + ) # get statistics of ob data data_shape = ob_data.shape @@ -826,7 +945,9 @@ def combine_ob_images( number_of_zeros = np.sum(ob_data == 0) logging.info(f"\t ob data shape: {data_shape}") logging.info(f"\t Number of zeros in ob data: {number_of_zeros}") - logging.info(f"\t Percentage of zeros in ob data: {number_of_zeros / (data_shape[0] * nbr_pixels) * 100:.2f}%") + logging.info( + f"\t Percentage of zeros in ob data: {number_of_zeros / (data_shape[0] * nbr_pixels) * 100:.2f}%" + ) logging.info(f"\t Mean of ob data: {np.mean(ob_data)}") logging.info(f"\t maximum of ob data: {np.max(ob_data)}") logging.info(f"\t minimum of ob data: {np.min(ob_data)}") @@ -842,14 +963,20 @@ def combine_ob_images( logging.info("\t -> Normalized by shutter counts") list_shutter_values_for_each_image = produce_list_shutter_for_each_image( - list_time_spectra=ob_master_dict[_ob_run_number][MasterDictKeys.list_spectra], - list_shutter_counts=ob_master_dict[_ob_run_number][MasterDictKeys.shutter_counts], + list_time_spectra=ob_master_dict[_ob_run_number][ + MasterDictKeys.list_spectra + ], + list_shutter_counts=ob_master_dict[_ob_run_number][ + MasterDictKeys.shutter_counts + ], ) logging.info(f"{list_shutter_values_for_each_image.shape = }") temp_ob_data = np.empty_like(ob_data, dtype=np.float32) for _index in range(len(list_shutter_values_for_each_image)): - temp_ob_data[_index] = ob_data[_index] / list_shutter_values_for_each_image[_index] + temp_ob_data[_index] = ( + ob_data[_index] / list_shutter_values_for_each_image[_index] + ) logging.info(f"{temp_ob_data.shape = }") ob_data = temp_ob_data.copy() @@ -897,9 +1024,15 @@ def combine_ob_images( formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) - parser.add_argument("--sample", type=str, nargs=1, help="Full path to sample run number") - parser.add_argument("--ob", type=str, nargs=1, help="Full path to the ob run number") - parser.add_argument("--output", type=str, nargs=1, help="Path to the output folder", default="./") + parser.add_argument( + "--sample", type=str, nargs=1, help="Full path to sample run number" + ) + parser.add_argument( + "--ob", type=str, nargs=1, help="Full path to the ob run number" + ) + parser.add_argument( + "--output", type=str, nargs=1, help="Path to the output folder", default="./" + ) args = parser.parse_args() logging.info(f"{args = }") @@ -948,7 +1081,9 @@ def combine_ob_images( # normalization(sample_folder=sample_folder, ob_folder=ob_folder, output_folder=output_folder) - print(f"Normalization is done! Check the log file {log_file_name} for more details!") + print( + f"Normalization is done! Check the log file {log_file_name} for more details!" + ) print(f"Exported data to {output_folder}") # sample = /SNS/VENUS/IPTS-34808/shared/autoreduce/mcp/November17_Sample6_UA_H_Batteries_1_5_Angs_min_30Hz_5C diff --git a/notebooks/__code/normalization_resonance/normalization_for_timepix1_timepix3.py b/notebooks/__code/normalization_resonance/normalization_for_timepix1_timepix3.py index 0e59f51bb..0d80e774a 100644 --- a/notebooks/__code/normalization_resonance/normalization_for_timepix1_timepix3.py +++ b/notebooks/__code/normalization_resonance/normalization_for_timepix1_timepix3.py @@ -14,7 +14,6 @@ from IPython.display import HTML, display from PIL import Image from skimage.io import imread -from scipy.ndimage import median_filter # from enum import Enum # from scipy.constants import h, c, electron_volt, m_n @@ -82,7 +81,9 @@ def _worker(fl): return (imread(fl).astype(LOAD_DTYPE)).swapaxes(0, 1) -def load_data_using_multithreading(list_tif: list = None, combine_tof: bool = False) -> np.ndarray: +def load_data_using_multithreading( + list_tif: list = None, combine_tof: bool = False +) -> np.ndarray: """load data using multithreading""" with mp.Pool(processes=40) as pool: data = pool.map(_worker, list_tif) @@ -101,7 +102,9 @@ def retrieve_list_of_tif(folder: str) -> list: def create_x_axis_file( - lambda_array: np.ndarray = None, energy_array: np.ndarray = None, output_folder: str = "./" + lambda_array: np.ndarray = None, + energy_array: np.ndarray = None, + output_folder: str = "./", ) -> str: """create x axis file with lambda, energy and tof arrays""" x_axis_data = { @@ -139,6 +142,7 @@ def correct_chips_alignment(data: np.ndarray, config: dict) -> np.ndarray: # return data_corrected return data + def normalization_with_list_of_full_path( sample_dict: dict = None, ob_dict: dict = None, @@ -208,10 +212,16 @@ def normalization_with_list_of_full_path( export_corrected_stack_of_sample_data = export_mode.get("sample_stack", False) export_corrected_stack_of_ob_data = export_mode.get("ob_stack", False) - export_corrected_stack_of_normalized_data = export_mode.get("normalized_stack", False) - export_corrected_integrated_sample_data = export_mode.get("sample_integrated", False) + export_corrected_stack_of_normalized_data = export_mode.get( + "normalized_stack", False + ) + export_corrected_integrated_sample_data = export_mode.get( + "sample_integrated", False + ) export_corrected_integrated_ob_data = export_mode.get("ob_integrated", False) - export_corrected_integrated_normalized_data = export_mode.get("normalized_integrated", False) + export_corrected_integrated_normalized_data = export_mode.get( + "normalized_integrated", False + ) export_x_axis = export_mode.get("x_axis", True) logging.info(f"{export_corrected_stack_of_sample_data = }") @@ -238,32 +248,40 @@ def normalization_with_list_of_full_path( logging.info(f"loading ob# {_ob_run_number} ... ") if verbose: display(HTML(f"Loading ob# {_ob_run_number} ...")) - ob_master_dict[_ob_run_number][MasterDictKeys.data] = load_data_using_multithreading( - ob_master_dict[_ob_run_number][MasterDictKeys.list_tif], combine_tof=False + ob_master_dict[_ob_run_number][MasterDictKeys.data] = ( + load_data_using_multithreading( + ob_master_dict[_ob_run_number][MasterDictKeys.list_tif], + combine_tof=False, + ) ) logging.info(f"ob# {_ob_run_number} loaded!") logging.info(f"{ob_master_dict[_ob_run_number][MasterDictKeys.data].shape = }") if verbose: display(HTML(f"ob# {_ob_run_number} loaded!")) - display(HTML(f"{ob_master_dict[_ob_run_number][MasterDictKeys.data].shape = }")) + display( + HTML(f"{ob_master_dict[_ob_run_number][MasterDictKeys.data].shape = }") + ) if proton_charge_flag: normalized_by_proton_charge = ( - sample_status_metadata.all_proton_charge_found and ob_status_metadata.all_proton_charge_found + sample_status_metadata.all_proton_charge_found + and ob_status_metadata.all_proton_charge_found ) else: normalized_by_proton_charge = False if monitor_counts_flag: normalized_by_monitor_counts = ( - sample_status_metadata.all_monitor_counts_found and ob_status_metadata.all_monitor_counts_found + sample_status_metadata.all_monitor_counts_found + and ob_status_metadata.all_monitor_counts_found ) else: normalized_by_monitor_counts = False if shutter_counts_flag: normalized_by_shutter_counts = ( - sample_status_metadata.all_shutter_counts_found and ob_status_metadata.all_shutter_counts_found + sample_status_metadata.all_shutter_counts_found + and ob_status_metadata.all_shutter_counts_found ) else: normalized_by_shutter_counts = False @@ -280,9 +298,15 @@ def normalization_with_list_of_full_path( max_iterations=max_iterations, ) logging.info(f"{ob_data_combined.shape = }") - logging.info(f"number of NaN in ob_data_combined data: {np.sum(np.isnan(ob_data_combined))}") - logging.info(f"number of inf in ob_data_combined data: {np.sum(np.isinf(ob_data_combined))}") - logging.info(f"number of zeros in ob_data_combined data: {np.sum(ob_data_combined == 0)} ") + logging.info( + f"number of NaN in ob_data_combined data: {np.sum(np.isnan(ob_data_combined))}" + ) + logging.info( + f"number of inf in ob_data_combined data: {np.sum(np.isinf(ob_data_combined))}" + ) + logging.info( + f"number of zeros in ob_data_combined data: {np.sum(ob_data_combined == 0)} " + ) if verbose: display(HTML(f"{ob_data_combined.shape = }")) @@ -291,7 +315,9 @@ def normalization_with_list_of_full_path( logging.info("Correcting chips alignment ...") if verbose: display(HTML("Correcting chips alignment ...")) - ob_data_combined = correct_chips_alignment(ob_data_combined, correct_chips_alignment_config) + ob_data_combined = correct_chips_alignment( + ob_data_combined, correct_chips_alignment_config + ) logging.info("Chips alignment corrected!") if verbose: display(HTML("Chips alignment corrected!")) @@ -304,7 +330,9 @@ def normalization_with_list_of_full_path( export_corrected_stack_of_ob_data, export_corrected_integrated_ob_data, ob_data_combined, - spectra_file_name=ob_master_dict[_ob_run_number][MasterDictKeys.spectra_file_name], + spectra_file_name=ob_master_dict[_ob_run_number][ + MasterDictKeys.spectra_file_name + ], ) # load dc images @@ -312,39 +340,56 @@ def normalization_with_list_of_full_path( logging.info(f"loading dc# {_dc_run_number} ... ") if verbose: display(HTML(f"Loading dc# {_dc_run_number} ...")) - dc_master_dict[_dc_run_number][MasterDictKeys.data] = load_data_using_multithreading( - dc_master_dict[_dc_run_number][MasterDictKeys.list_tif], combine_tof=False + dc_master_dict[_dc_run_number][MasterDictKeys.data] = ( + load_data_using_multithreading( + dc_master_dict[_dc_run_number][MasterDictKeys.list_tif], + combine_tof=False, + ) ) logging.info(f"dc# {_dc_run_number} loaded!") logging.info(f"{dc_master_dict[_dc_run_number][MasterDictKeys.data].shape = }") if verbose: display(HTML(f"dc# {_dc_run_number} loaded!")) - display(HTML(f"{dc_master_dict[_dc_run_number][MasterDictKeys.data].shape = }")) + display( + HTML(f"{dc_master_dict[_dc_run_number][MasterDictKeys.data].shape = }") + ) # combine all ob images dc_data_combined = combine_dc_images(dc_master_dict) - + # load sample images for _sample_run_number in sample_master_dict.keys(): logging.info(f"loading sample# {_sample_run_number} ... ") if verbose: display(HTML(f"Loading sample# {_sample_run_number} ...")) - sample_master_dict[_sample_run_number][MasterDictKeys.data] = load_data_using_multithreading( - sample_master_dict[_sample_run_number][MasterDictKeys.list_tif], combine_tof=False + sample_master_dict[_sample_run_number][MasterDictKeys.data] = ( + load_data_using_multithreading( + sample_master_dict[_sample_run_number][MasterDictKeys.list_tif], + combine_tof=False, + ) ) logging.info(f"sample# {_sample_run_number} loaded!") - logging.info(f"{sample_master_dict[_sample_run_number][MasterDictKeys.data].shape = }") + logging.info( + f"{sample_master_dict[_sample_run_number][MasterDictKeys.data].shape = }" + ) if verbose: display(HTML(f"sample# {_sample_run_number} loaded!")) - display(HTML(f"{sample_master_dict[_sample_run_number][MasterDictKeys.data].shape = }")) + display( + HTML( + f"{sample_master_dict[_sample_run_number][MasterDictKeys.data].shape = }" + ) + ) if correct_chips_alignment_flag: logging.info("Correcting chips alignment ...") if verbose: display(HTML("Correcting chips alignment ...")) for _sample_run_number in sample_master_dict.keys(): - sample_master_dict[_sample_run_number][MasterDictKeys.data] = correct_chips_alignment( - sample_master_dict[_sample_run_number][MasterDictKeys.data], correct_chips_alignment_config + sample_master_dict[_sample_run_number][MasterDictKeys.data] = ( + correct_chips_alignment( + sample_master_dict[_sample_run_number][MasterDictKeys.data], + correct_chips_alignment_config, + ) ) logging.info("Chips alignment corrected!") if verbose: @@ -369,7 +414,9 @@ def normalization_with_list_of_full_path( logging.info(f"\t sample data shape: {data_shape}") logging.info(f"\t data type of _sample_data: {_sample_data.dtype}") logging.info(f"\t Number of zeros in sample data: {number_of_zeros}") - logging.info(f"\t Number of nan in sample data: {np.sum(np.isnan(_sample_data))}") + logging.info( + f"\t Number of nan in sample data: {np.sum(np.isnan(_sample_data))}" + ) logging.info( f"\t Percentage of zeros in sample data: {number_of_zeros / (data_shape[0] * nbr_pixels) * 100:.2f}%" ) @@ -380,7 +427,9 @@ def normalization_with_list_of_full_path( if normalized_by_proton_charge: logging.info("\t -> Normalized by proton charge") - proton_charge = sample_master_dict[_sample_run_number][MasterDictKeys.proton_charge] + proton_charge = sample_master_dict[_sample_run_number][ + MasterDictKeys.proton_charge + ] logging.info(f"\t\t proton charge: {proton_charge} C") logging.info(f"\t\t{type(proton_charge) = }") logging.info(f"\t\tbefore division: {_sample_data.dtype = }") @@ -389,7 +438,9 @@ def normalization_with_list_of_full_path( if normalized_by_monitor_counts: logging.info("\t -> Normalized by monitor counts") - monitor_counts = sample_master_dict[_sample_run_number][MasterDictKeys.monitor_counts] + monitor_counts = sample_master_dict[_sample_run_number][ + MasterDictKeys.monitor_counts + ] logging.info(f"\t\t monitor counts: {monitor_counts}") logging.info(f"\t\t{type(monitor_counts) = }") _sample_data = _sample_data / monitor_counts @@ -397,12 +448,18 @@ def normalization_with_list_of_full_path( if normalized_by_shutter_counts: list_shutter_values_for_each_image = produce_list_shutter_for_each_image( - list_time_spectra=ob_master_dict[_ob_run_number][MasterDictKeys.list_spectra], - list_shutter_counts=sample_master_dict[_sample_run_number][MasterDictKeys.shutter_counts], + list_time_spectra=ob_master_dict[_ob_run_number][ + MasterDictKeys.list_spectra + ], + list_shutter_counts=sample_master_dict[_sample_run_number][ + MasterDictKeys.shutter_counts + ], ) sample_data = [] - for _sample, _shutter_value in zip(_sample_data, list_shutter_values_for_each_image, strict=False): + for _sample, _shutter_value in zip( + _sample_data, list_shutter_values_for_each_image, strict=False + ): sample_data.append(_sample / _shutter_value) _sample_data = np.array(sample_data) @@ -412,52 +469,74 @@ def normalization_with_list_of_full_path( logging.info(f"{ob_data_combined.dtype = }") # export sample data after correction if requested - if export_corrected_stack_of_sample_data or export_corrected_integrated_sample_data: + if ( + export_corrected_stack_of_sample_data + or export_corrected_integrated_sample_data + ): export_sample_images( output_folder, export_corrected_stack_of_sample_data, export_corrected_integrated_sample_data, _sample_run_number, _sample_data, - spectra_file_name=sample_master_dict[_sample_run_number][MasterDictKeys.spectra_file_name], + spectra_file_name=sample_master_dict[_sample_run_number][ + MasterDictKeys.spectra_file_name + ], ) if dc_data_combined is not None: - logging.info(f"normalization with DC subtraction") - _normalized_data = np.divide(np.subtract(_sample_data, dc_data_combined), np.subtract(ob_data_combined, dc_data_combined), - out=np.zeros_like(_sample_data), - where=(ob_data_combined - dc_data_combined)!=0) + logging.info("normalization with DC subtraction") + _normalized_data = np.divide( + np.subtract(_sample_data, dc_data_combined), + np.subtract(ob_data_combined, dc_data_combined), + out=np.zeros_like(_sample_data), + where=(ob_data_combined - dc_data_combined) != 0, + ) else: - logging.info(f"normalization without DC subtraction") - _normalized_data = np.divide(_sample_data, ob_data_combined, - out=np.zeros_like(_sample_data), - where=ob_data_combined!=0) - + logging.info("normalization without DC subtraction") + _normalized_data = np.divide( + _sample_data, + ob_data_combined, + out=np.zeros_like(_sample_data), + where=ob_data_combined != 0, + ) + _normalized_data[ob_data_combined == 0] = 0 normalized_data[_sample_run_number] = _normalized_data # normalized_data[_sample_run_number] = np.array(np.divide(_sample_data, ob_data_combined)) logging.info(f"{normalized_data[_sample_run_number].shape = }") logging.info(f"{normalized_data[_sample_run_number].dtype = }") - logging.info(f"number of NaN in normalized data: {np.sum(np.isnan(normalized_data[_sample_run_number]))}") - logging.info(f"number of inf in normalized data: {np.sum(np.isinf(normalized_data[_sample_run_number]))}") + logging.info( + f"number of NaN in normalized data: {np.sum(np.isnan(normalized_data[_sample_run_number]))}" + ) + logging.info( + f"number of inf in normalized data: {np.sum(np.isinf(normalized_data[_sample_run_number]))}" + ) - detector_delay_us = sample_master_dict[_sample_run_number][MasterDictKeys.detector_delay_us] - time_spectra = sample_master_dict[_sample_run_number][MasterDictKeys.list_spectra] + detector_delay_us = sample_master_dict[_sample_run_number][ + MasterDictKeys.detector_delay_us + ] + time_spectra = sample_master_dict[_sample_run_number][ + MasterDictKeys.list_spectra + ] if time_spectra is None: - logging.info("Time spectra is None, cannot convert to lambda or energy arrays") + logging.info( + "Time spectra is None, cannot convert to lambda or energy arrays" + ) lambda_array = None energy_array = None - - else: - logging.info(f"We have a time_spectra!") + else: + logging.info("We have a time_spectra!") logging.info(f"time spectra shape: {time_spectra.shape}") - + if detector_delay_us is None: detector_delay_us = 0.0 - logging.info(f"detector delay is None, setting it to {detector_delay_us} us") + logging.info( + f"detector delay is None, setting it to {detector_delay_us} us" + ) logging.info(f"we have a detector delay of {detector_delay_us} us") @@ -487,26 +566,29 @@ def normalization_with_list_of_full_path( logging.info(f"Preview: {preview = }") if preview: - # display preview of normalized data - fig, axs1 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) + fig, axs1 = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) sample_data_integrated = np.nanmean(_sample_data, axis=0) im0 = axs1[0].imshow(sample_data_integrated, cmap="gray") plt.colorbar(im0, ax=axs1[0]) display(HTML(f"

Preview of run {_sample_run_number}

")) display(HTML(f"detector delay: {detector_delay_us:.2f} us")) - - axs1[0].set_title(f"Integrated Sample data") + + axs1[0].set_title("Integrated Sample data") sample_integrated1 = np.nansum(_sample_data, axis=1) sample_integrated = np.nansum(sample_integrated1, axis=1) - axs1[1].plot(sample_integrated, 'o') + axs1[1].plot(sample_integrated, "o") axs1[1].set_xlabel("File image index") axs1[1].set_ylabel("mean of full image") plt.tight_layout() - fig, axs2 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) + fig, axs2 = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) ob_data_integrated = np.nanmean(ob_data_combined, axis=0) im1 = axs2[0].imshow(ob_data_integrated, cmap="gray") plt.colorbar(im1, ax=axs2[0]) @@ -514,13 +596,15 @@ def normalization_with_list_of_full_path( ob_integrated1 = np.nansum(ob_data_combined, axis=1) ob_integrated = np.nansum(ob_integrated1, axis=1) - axs2[1].plot(ob_integrated, 'o') + axs2[1].plot(ob_integrated, "o") axs2[1].set_xlabel("File image index") axs2[1].set_ylabel("mean of full image") plt.tight_layout() if dc_data_combined is not None: - fig, axs_dc = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) + fig, axs_dc = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) dc_data_integrated = np.nanmean(dc_data_combined, axis=0) im_dc = axs_dc[0].imshow(dc_data_integrated, cmap="gray") plt.colorbar(im_dc, ax=axs_dc[0]) @@ -528,26 +612,32 @@ def normalization_with_list_of_full_path( dc_integrated1 = np.nansum(dc_data_combined, axis=1) dc_integrated = np.nansum(dc_integrated1, axis=1) - axs_dc[1].plot(dc_integrated, 'o') + axs_dc[1].plot(dc_integrated, "o") axs_dc[1].set_xlabel("File image index") axs_dc[1].set_ylabel("mean of full image") plt.tight_layout() - fig, axs3 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) - normalized_data_integrated = np.nanmean(normalized_data[_sample_run_number], axis=0) + fig, axs3 = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) + normalized_data_integrated = np.nanmean( + normalized_data[_sample_run_number], axis=0 + ) im2 = axs3[0].imshow(normalized_data_integrated, cmap="gray") plt.colorbar(im2, ax=axs3[0]) - axs3[0].set_title(f"Integrated Normalized data") + axs3[0].set_title("Integrated Normalized data") profile_step1 = np.nanmean(normalized_data[_sample_run_number], axis=1) profile = np.nanmean(profile_step1, axis=1) - axs3[1].plot(profile, 'o') + axs3[1].plot(profile, "o") axs3[1].set_xlabel("File image index") axs3[1].set_ylabel("mean of full image") plt.tight_layout() if lambda_array is not None: - fig, axs4 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) + fig, axs4 = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) logging.info(f"{np.shape(profile) = }") axs4[0].plot(lambda_array, profile, "*") @@ -562,24 +652,36 @@ def normalization_with_list_of_full_path( plt.show() - if export_corrected_integrated_normalized_data or export_corrected_stack_of_normalized_data: + if ( + export_corrected_integrated_normalized_data + or export_corrected_stack_of_normalized_data + ): # make up new output folder name list_ob_runs = list(ob_master_dict.keys()) - str_ob_runs = "_".join([str(_ob_run_number) for _ob_run_number in list_ob_runs]) + str_ob_runs = "_".join( + [str(_ob_run_number) for _ob_run_number in list_ob_runs] + ) full_output_folder = os.path.join( - output_folder, f"normalized_sample_{_sample_run_number}_obs_{str_ob_runs}" + output_folder, + f"normalized_sample_{_sample_run_number}_obs_{str_ob_runs}", ) # issue for WEI here ! full_output_folder = os.path.abspath(full_output_folder) os.makedirs(full_output_folder, exist_ok=True) if export_corrected_integrated_normalized_data: # making up the integrated sample data - sample_data_integrated = np.nanmean(normalized_data[_sample_run_number], axis=0) + sample_data_integrated = np.nanmean( + normalized_data[_sample_run_number], axis=0 + ) full_file_name = os.path.join(full_output_folder, "integrated.tif") - logging.info(f"\t -> Exporting integrated normalized data to {full_file_name} ...") + logging.info( + f"\t -> Exporting integrated normalized data to {full_file_name} ..." + ) make_tiff(data=sample_data_integrated, filename=full_file_name) - logging.info(f"\t -> Exporting integrated normalized data to {full_file_name} is done!") + logging.info( + f"\t -> Exporting integrated normalized data to {full_file_name} is done!" + ) if export_corrected_stack_of_normalized_data: output_stack_folder = os.path.join(full_output_folder, "stack") @@ -587,14 +689,22 @@ def normalization_with_list_of_full_path( os.makedirs(output_stack_folder, exist_ok=True) for _index, _data in enumerate(normalized_data[_sample_run_number]): - _output_file = os.path.join(output_stack_folder, f"image{_index:04d}.tif") + _output_file = os.path.join( + output_stack_folder, f"image{_index:04d}.tif" + ) make_tiff(data=_data, filename=_output_file) - logging.info(f"\t -> Exporting normalized data to {output_stack_folder} is done!") + logging.info( + f"\t -> Exporting normalized data to {output_stack_folder} is done!" + ) print(f"Exported normalized tif images are in: {output_stack_folder}!") - spectra_file = sample_master_dict[_sample_run_number][MasterDictKeys.spectra_file_name] + spectra_file = sample_master_dict[_sample_run_number][ + MasterDictKeys.spectra_file_name + ] if spectra_file and Path(spectra_file).exists(): - logging.info(f"Exported time spectra file {spectra_file} to {output_stack_folder}!") + logging.info( + f"Exported time spectra file {spectra_file} to {output_stack_folder}!" + ) shutil.copy(spectra_file, output_stack_folder) # create x-axis file @@ -613,7 +723,9 @@ def get_detector_offset_from_nexus(nexus_path: str) -> float: """get the detector offset from the nexus file""" with h5py.File(nexus_path, "r") as hdf5_data: try: - detector_offset_micros = hdf5_data["entry"]["DASlogs"]["BL10:Det:TH:DSPT1:TIDelay"]["value"][0] + detector_offset_micros = hdf5_data["entry"]["DASlogs"][ + "BL10:Det:TH:DSPT1:TIDelay" + ]["value"][0] except KeyError: detector_offset_micros = None return detector_offset_micros @@ -642,7 +754,9 @@ def export_sample_images( make_tiff(data=_data, filename=_output_file) logging.info(f"\t -> Exporting sample data to {output_stack_folder} is done!") shutil.copy(spectra_file_name, os.path.join(output_stack_folder)) - logging.info(f"\t -> Exporting spectra file {spectra_file_name} to {output_stack_folder} is done!") + logging.info( + f"\t -> Exporting spectra file {spectra_file_name} to {output_stack_folder} is done!" + ) if export_corrected_integrated_sample_data: # making up the integrated sample data @@ -650,7 +764,9 @@ def export_sample_images( full_file_name = os.path.join(sample_output_folder, "integrated.tif") logging.info(f"\t -> Exporting integrated sample data to {full_file_name} ...") make_tiff(data=sample_data_integrated, filename=full_file_name) - logging.info(f"\t -> Exporting integrated sample data to {full_file_name} is done!") + logging.info( + f"\t -> Exporting integrated sample data to {full_file_name} is done!" + ) display(HTML(f"Created folder {output_stack_folder} for sample outputs!")) @@ -667,10 +783,13 @@ def export_ob_images( logging.info(f"> Exporting combined ob images to {output_folder} ...") logging.info(f"\t{ob_run_numbers = }") list_ob_runs_number_only = [ - str(isolate_run_number_from_full_path(_ob_run_number)) for _ob_run_number in ob_run_numbers + str(isolate_run_number_from_full_path(_ob_run_number)) + for _ob_run_number in ob_run_numbers ] if len(list_ob_runs_number_only) == 1: - ob_output_folder = os.path.join(output_folder, f"ob_{list_ob_runs_number_only[0]}") + ob_output_folder = os.path.join( + output_folder, f"ob_{list_ob_runs_number_only[0]}" + ) else: str_list_ob_runs = "_".join(list_ob_runs_number_only) ob_output_folder = os.path.join(output_folder, f"ob_{str_list_ob_runs}") @@ -699,7 +818,9 @@ def export_ob_images( logging.info(f"\t -> Exporting ob data to {output_stack_folder} is done!") # copy spectra file to the output folder shutil.copy(spectra_file_name, os.path.join(output_stack_folder)) - logging.info(f"\t -> Exported spectra file {spectra_file_name} to {output_stack_folder}!") + logging.info( + f"\t -> Exported spectra file {spectra_file_name} to {output_stack_folder}!" + ) display(HTML(f"Created folder {output_stack_folder} for OB outputs!")) @@ -790,7 +911,9 @@ def update_dict_with_shutter_counts(master_dict: dict) -> tuple[dict, bool]: if _value == "0": break list_shutter_counts.append(float(_value)) - master_dict[run_number][MasterDictKeys.shutter_counts] = list_shutter_counts + master_dict[run_number][MasterDictKeys.shutter_counts] = ( + list_shutter_counts + ) return master_dict, status_all_shutter_counts_found @@ -827,11 +950,15 @@ def update_dict_with_proton_charge(master_dict: dict) -> tuple[dict, bool]: try: with h5py.File(_nexus_path, "r") as hdf5_data: - proton_charge = hdf5_data["entry"][MasterDictKeys.proton_charge][0] / 1e12 + proton_charge = ( + hdf5_data["entry"][MasterDictKeys.proton_charge][0] / 1e12 + ) except KeyError: proton_charge = None status_all_proton_charge_found = False - master_dict[_run_number][MasterDictKeys.proton_charge] = np.float32(proton_charge) + master_dict[_run_number][MasterDictKeys.proton_charge] = np.float32( + proton_charge + ) return status_all_proton_charge_found @@ -852,14 +979,18 @@ def update_dict_with_monitor_counts(master_dict: dict) -> bool: except KeyError: monitor_counts = None status_all_monitor_counts_found = False - master_dict[_run_number][MasterDictKeys.monitor_counts] = np.float32(monitor_counts) + master_dict[_run_number][MasterDictKeys.monitor_counts] = np.float32( + monitor_counts + ) return status_all_monitor_counts_found def update_dict_with_list_of_images(master_dict: dict) -> dict: """update the master dict with list of images""" for _run_number in master_dict.keys(): - list_tif = retrieve_list_of_tif(master_dict[_run_number][MasterDictKeys.data_path]) + list_tif = retrieve_list_of_tif( + master_dict[_run_number][MasterDictKeys.data_path] + ) logging.info(f"Retrieved {len(list_tif)} tif files for run {_run_number}!") master_dict[_run_number][MasterDictKeys.list_tif] = list_tif @@ -871,7 +1002,9 @@ def get_list_run_number(data_folder: str) -> list: return list_run_number -def update_dict_with_nexus_full_path(nexus_root_path: str, instrument: str, master_dict: dict) -> dict: +def update_dict_with_nexus_full_path( + nexus_root_path: str, instrument: str, master_dict: dict +) -> dict: """create dict of nexus path for each run number""" for run_number in master_dict.keys(): master_dict[run_number][MasterDictKeys.nexus_path] = os.path.join( @@ -892,7 +1025,9 @@ def update_with_nexus_metadata(master_dict: dict) -> dict: def update_dict_with_data_full_path(data_root_path: str, master_dict: dict) -> dict: """create dict of data path for each run number""" for run_number in master_dict.keys(): - master_dict[run_number][MasterDictKeys.data_path] = os.path.join(data_root_path, f"Run_{run_number}") + master_dict[run_number][MasterDictKeys.data_path] = os.path.join( + data_root_path, f"Run_{run_number}" + ) def create_master_dict( @@ -942,7 +1077,9 @@ def create_master_dict( return master_dict, status_metadata -def produce_list_shutter_for_each_image(list_time_spectra: list = None, list_shutter_counts: list = None) -> list: +def produce_list_shutter_for_each_image( + list_time_spectra: list = None, list_shutter_counts: list = None +) -> list: """produce list of shutter counts for each image""" delat_time_spectra = list_time_spectra[1] - list_time_spectra[0] @@ -952,31 +1089,41 @@ def produce_list_shutter_for_each_image(list_time_spectra: list = None, list_shu logging.info(f"\t{list_index_jump = }") logging.info(f"\t{list_shutter_counts = }") - list_shutter_values_for_each_image = np.zeros(len(list_time_spectra), dtype=np.float32) + list_shutter_values_for_each_image = np.zeros( + len(list_time_spectra), dtype=np.float32 + ) if len(list_shutter_counts) == 1: # resonance mode list_shutter_values_for_each_image.fill(list_shutter_counts[0]) return list_shutter_values_for_each_image - list_shutter_values_for_each_image[0 : list_index_jump[0] + 1].fill(list_shutter_counts[0]) + list_shutter_values_for_each_image[0 : list_index_jump[0] + 1].fill( + list_shutter_counts[0] + ) for _index in range(1, len(list_index_jump)): _start = list_index_jump[_index - 1] _end = list_index_jump[_index] - list_shutter_values_for_each_image[_start + 1 : _end + 1].fill(list_shutter_counts[_index]) + list_shutter_values_for_each_image[_start + 1 : _end + 1].fill( + list_shutter_counts[_index] + ) - list_shutter_values_for_each_image[list_index_jump[-1] + 1 :] = list_shutter_counts[-1] + list_shutter_values_for_each_image[list_index_jump[-1] + 1 :] = list_shutter_counts[ + -1 + ] return list_shutter_values_for_each_image -def replace_zero_with_local_median(data: np.ndarray, - kernel_size: Tuple[int, int, int] = (3, 3, 3), - max_iterations: int = 10) -> np.ndarray: +def replace_zero_with_local_median( + data: np.ndarray, + kernel_size: Tuple[int, int, int] = (3, 3, 3), + max_iterations: int = 10, +) -> np.ndarray: """ Replace 0 values in a 3D array using local median filtering. This function ONLY processes small neighborhoods around 0 pixels, avoiding expensive computation on the entire dataset. - + Parameters: ----------- data : np.ndarray @@ -987,7 +1134,7 @@ def replace_zero_with_local_median(data: np.ndarray, max_iterations : int Maximum number of iterations to replace 0 values Default is 10 - + Returns: -------- np.ndarray @@ -1006,7 +1153,7 @@ def replace_zero_with_local_median(data: np.ndarray, # Calculate padding for kernel pad_h, pad_w, pad_d = [k // 2 for k in kernel_size] - + for iteration in range(max_iterations): # Find current 0 locations zero_coords = np.argwhere(result == 0) @@ -1016,13 +1163,15 @@ def replace_zero_with_local_median(data: np.ndarray, logging.info(f"All 0 values replaced after {iteration} iterations") break - logging.info(f"Iteration {iteration + 1}: {current_zero_count} 0 values remaining") + logging.info( + f"Iteration {iteration + 1}: {current_zero_count} 0 values remaining" + ) # Process each 0 pixel individually replaced_count = 0 for coord in zero_coords: y, x, z = coord - + # Define the local neighborhood bounds y_min = max(0, y - pad_h) y_max = min(result.shape[0], y + pad_h + 1) @@ -1030,13 +1179,13 @@ def replace_zero_with_local_median(data: np.ndarray, x_max = min(result.shape[1], x + pad_w + 1) z_min = max(0, z - pad_d) z_max = min(result.shape[2], z + pad_d + 1) - + # Extract the local neighborhood neighborhood = result[y_min:y_max, x_min:x_max, z_min:z_max] - + # Get non-NaN values in the neighborhood valid_values = neighborhood[~np.isnan(neighborhood)] - + # If we have valid values, compute median and replace if len(valid_values) > 0: median_value = np.median(valid_values) @@ -1048,30 +1197,34 @@ def replace_zero_with_local_median(data: np.ndarray, # If no progress was made, break if replaced_count == 0: remaining_zero_count = np.sum(result == 0) - logging.info(f"No progress made. {remaining_zero_count} zero values could not be replaced") + logging.info( + f"No progress made. {remaining_zero_count} zero values could not be replaced" + ) logging.info("(These may be in regions with no valid neighbors)") break final_zero_count = np.sum(result == 0) logging.info(f"Final zero count: {final_zero_count}") - logging.info(f"Successfully replaced {initial_zero_count - final_zero_count} zero values") + logging.info( + f"Successfully replaced {initial_zero_count - final_zero_count} zero values" + ) return result def combine_dc_images(dc_master_dict: dict) -> np.ndarray: """combine all dc images - + Parameters: ----------- dc_master_dict : dict master dict of dc run numbers - + Returns: -------- np.ndarray combined dc data - + """ logging.info("Combining all dark current images") full_dc_data = [] @@ -1082,7 +1235,9 @@ def combine_dc_images(dc_master_dict: dict) -> np.ndarray: for _dc_run_number in dc_master_dict.keys(): logging.info(f"Combining dc# {_dc_run_number} ...") - dc_data = np.array(dc_master_dict[_dc_run_number][MasterDictKeys.data], dtype=np.float32) + dc_data = np.array( + dc_master_dict[_dc_run_number][MasterDictKeys.data], dtype=np.float32 + ) full_dc_data.append(dc_data) logging.info(f"{np.shape(full_dc_data) = }") @@ -1101,11 +1256,11 @@ def combine_ob_images( use_shutter_counts: bool = False, replace_ob_zeros_by_nan: bool = False, replace_ob_zeros_by_local_median: bool = False, - kernel_size_for_local_median: Tuple[int, int, int] = (3, 3, 3), + kernel_size_for_local_median: Tuple[int, int, int] = (3, 3, 3), max_iterations: int = 10, ) -> np.ndarray: """combine all ob images and correct by proton charge and shutter counts - + Parameters: ----------- ob_master_dict : dict @@ -1124,12 +1279,12 @@ def combine_ob_images( kernel size for local median filtering max_iterations : int maximum number of iterations for local median filtering - + Returns: -------- np.ndarray combined ob data - + """ logging.info("Combining all open beam images") @@ -1137,15 +1292,21 @@ def combine_ob_images( logging.info(f"\tcorrecting by monitor counts: {use_monitor_counts}") logging.info(f"\tshutter counts: {use_shutter_counts}") logging.info(f"\treplace ob zeros by nan: {replace_ob_zeros_by_nan}") - logging.info(f"\treplace ob zeros by local median: {replace_ob_zeros_by_local_median}") - logging.info(f"\tkernel size for local median: y:{kernel_size_for_local_median[0]}, " - f"x:{kernel_size_for_local_median[1]}, " - f"tof:{kernel_size_for_local_median[2]}") + logging.info( + f"\treplace ob zeros by local median: {replace_ob_zeros_by_local_median}" + ) + logging.info( + f"\tkernel size for local median: y:{kernel_size_for_local_median[0]}, " + f"x:{kernel_size_for_local_median[1]}, " + f"tof:{kernel_size_for_local_median[2]}" + ) full_ob_data_corrected = [] for _ob_run_number in ob_master_dict.keys(): logging.info(f"Combining ob# {_ob_run_number} ...") - ob_data = np.array(ob_master_dict[_ob_run_number][MasterDictKeys.data], dtype=np.float32) + ob_data = np.array( + ob_master_dict[_ob_run_number][MasterDictKeys.data], dtype=np.float32 + ) # get statistics of ob data data_shape = ob_data.shape @@ -1154,7 +1315,9 @@ def combine_ob_images( number_of_zeros = np.sum(ob_data == 0) logging.info(f"\t ob data shape: {data_shape}") logging.info(f"\t Number of zeros in ob data: {number_of_zeros}") - logging.info(f"\t Percentage of zeros in ob data: {number_of_zeros / (data_shape[0] * nbr_pixels) * 100:.2f}%") + logging.info( + f"\t Percentage of zeros in ob data: {number_of_zeros / (data_shape[0] * nbr_pixels) * 100:.2f}%" + ) logging.info(f"\t Mean of ob data: {np.mean(ob_data)}") logging.info(f"\t maximum of ob data: {np.max(ob_data)}") logging.info(f"\t minimum of ob data: {np.min(ob_data)}") @@ -1172,7 +1335,9 @@ def combine_ob_images( if use_monitor_counts: logging.info("\t -> Normalized by monitor counts") - monitor_counts = ob_master_dict[_ob_run_number][MasterDictKeys.monitor_counts] + monitor_counts = ob_master_dict[_ob_run_number][ + MasterDictKeys.monitor_counts + ] logging.info(f"\t\t monitor counts: {monitor_counts}") logging.info(f"\t\t{type(monitor_counts) = }") ob_data = ob_data / monitor_counts @@ -1182,14 +1347,20 @@ def combine_ob_images( logging.info("\t -> Normalized by shutter counts") list_shutter_values_for_each_image = produce_list_shutter_for_each_image( - list_time_spectra=ob_master_dict[_ob_run_number][MasterDictKeys.list_spectra], - list_shutter_counts=ob_master_dict[_ob_run_number][MasterDictKeys.shutter_counts], + list_time_spectra=ob_master_dict[_ob_run_number][ + MasterDictKeys.list_spectra + ], + list_shutter_counts=ob_master_dict[_ob_run_number][ + MasterDictKeys.shutter_counts + ], ) logging.info(f"{list_shutter_values_for_each_image.shape = }") temp_ob_data = np.empty_like(ob_data, dtype=np.float32) for _index in range(len(list_shutter_values_for_each_image)): - temp_ob_data[_index] = ob_data[_index] / list_shutter_values_for_each_image[_index] + temp_ob_data[_index] = ( + ob_data[_index] / list_shutter_values_for_each_image[_index] + ) logging.info(f"{temp_ob_data.shape = }") ob_data = temp_ob_data.copy() @@ -1197,9 +1368,11 @@ def combine_ob_images( # logging.info(f"{ob_data_combined.shape = }") if replace_ob_zeros_by_local_median: - ob_data = replace_zero_with_local_median(ob_data, - kernel_size=kernel_size_for_local_median, - max_iterations=max_iterations) + ob_data = replace_zero_with_local_median( + ob_data, + kernel_size=kernel_size_for_local_median, + max_iterations=max_iterations, + ) full_ob_data_corrected.append(ob_data) logging.info(f"{np.shape(full_ob_data_corrected) = }") @@ -1224,9 +1397,15 @@ def combine_ob_images( formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) - parser.add_argument("--sample", type=str, nargs=1, help="Full path to sample run number") - parser.add_argument("--ob", type=str, nargs=1, help="Full path to the ob run number") - parser.add_argument("--output", type=str, nargs=1, help="Path to the output folder", default="./") + parser.add_argument( + "--sample", type=str, nargs=1, help="Full path to sample run number" + ) + parser.add_argument( + "--ob", type=str, nargs=1, help="Full path to the ob run number" + ) + parser.add_argument( + "--output", type=str, nargs=1, help="Path to the output folder", default="./" + ) args = parser.parse_args() logging.info(f"{args = }") @@ -1275,7 +1454,9 @@ def combine_ob_images( # normalization(sample_folder=sample_folder, ob_folder=ob_folder, output_folder=output_folder) - print(f"Normalization is done! Check the log file {log_file_name} for more details!") + print( + f"Normalization is done! Check the log file {log_file_name} for more details!" + ) print(f"Exported data to {output_folder}") # sample = /SNS/VENUS/IPTS-34808/shared/autoreduce/mcp/November17_Sample6_UA_H_Batteries_1_5_Angs_min_30Hz_5C diff --git a/notebooks/__code/normalization_resonance/normalization_resonance.py b/notebooks/__code/normalization_resonance/normalization_resonance.py index 75ad3d2c7..0f3c428fd 100644 --- a/notebooks/__code/normalization_resonance/normalization_resonance.py +++ b/notebooks/__code/normalization_resonance/normalization_resonance.py @@ -1,42 +1,29 @@ -import glob import logging import logging as notebook_logging import os -from pathlib import Path -import numpy as np import ipywidgets as widgets import matplotlib.pyplot as plt from IPython.display import HTML, display from ipywidgets import interactive -from PIL import Image -from pleiades.processing.normalization import normalization as normalization_with_pleaides +from pleiades.processing.normalization import ( + normalization as normalization_with_pleaides, +) from pleiades.processing import Roi as PleiadesRoi from pleiades.processing import Facility -from __code._utilities.list import extract_list_of_runs_from_string -from __code._utilities.nexus import extract_file_path_from_nexus # from __code.ipywe.myfileselector import MyFileSelectorPanel from __code.normalization_tof.normalization_tof import NormalizationTof -from __code.ipywe.fileselector import FileSelectorPanel as MyFileSelectorPanel -from __code.normalization_tof import DetectorType, autoreduce_dir, distance_source_detector_m, raw_dir -from __code.normalization_tof.config import DEBUG_DATA, timepix1_config, timepix3_config -from __code.normalization_tof.normalization_for_timepix1_timepix3 import ( - load_data_using_multithreading, - # normalization, - normalization_with_list_of_full_path, - retrieve_list_of_tif, -) +from __code.normalization_tof import DetectorType class NormalizationResonance(NormalizationTof): - sample_folder = None sample_run_numbers = None sample_run_numbers_selected = None - + ob_folder = None ob_run_numbers = None ob_run_numbers_selected = None @@ -46,7 +33,7 @@ class NormalizationResonance(NormalizationTof): dc_run_numbers_selected = None output_folder = None - + # {'full_path_data': {'data': None, 'nexus': None}} dict_sample = {} dict_ob = {} @@ -75,11 +62,13 @@ def initialize(self): def retrieve_nexus_file_path(self): """ Retrieve the NeXus file paths for sample, OB and DC. - + This function assumes that the NeXus files are named in a specific format""" all_nexus_files_found = True - notebook_logging.info("Retrieving NeXus file paths for sample, OB and DC runs...") + notebook_logging.info( + "Retrieving NeXus file paths for sample, OB and DC runs..." + ) notebook_logging.info("\tworking with sample runs:") for full_path in self.dict_sample.keys(): @@ -143,7 +132,6 @@ def retrieve_nexus_file_path(self): return all_nexus_files_found def settings(self): - self.select_roi_widget = widgets.Checkbox( description="Select ROI on the detector", value=True, @@ -169,20 +157,22 @@ def settings(self): self.select_roi_widget, self.combine_mode_widget, ], - layout=widgets.Layout(padding="10px", - border="solid 1px", - width="620px"), + layout=widgets.Layout(padding="10px", border="solid 1px", width="620px"), ) display(verti_layout) def normalization(self): - display(HTML("Running normalization ...")) + display( + HTML( + "Running normalization ..." + ) + ) sample_folders = list(self.dict_sample.keys()) ob_folders = list(self.dict_ob.keys()) facility = Facility.ornl nexus_full_path = os.path.join(self.nexus_folder) - + if self.roi: left = self.roi.left top = self.roi.top @@ -190,23 +180,29 @@ def normalization(self): height = self.roi.height roi = PleiadesRoi(x1=left, y1=top, width=width, height=height) - logging.info(f"normalization:") + logging.info("normalization:") logging.info(f"\tsample_folders: {sample_folders}") logging.info(f"\tob_folders: {ob_folders}") logging.info(f"\roi: {roi}") logging.info(f"\tnexus_path: {nexus_full_path}") - self.transmission = normalization_with_pleaides(list_sample_folders=sample_folders, - list_obs_folders=ob_folders, - nexus_path=nexus_full_path, - facility=facility, - combine_mode=self.combine_mode_widget.value, - roi=roi, - pc_uncertainty=0.005, - output_folder=self.output_folder,) - + self.transmission = normalization_with_pleaides( + list_sample_folders=sample_folders, + list_obs_folders=ob_folders, + nexus_path=nexus_full_path, + facility=facility, + combine_mode=self.combine_mode_widget.value, + roi=roi, + pc_uncertainty=0.005, + output_folder=self.output_folder, + ) + logging.info("Done with normalization.") - display(HTML("Done with normalization.")) + display( + HTML( + "Done with normalization." + ) + ) self.display_normalization_results() @@ -218,10 +214,9 @@ def display_normalization_results(self): sample_folders = list(self.dict_sample.keys()) default_xmin = 0 - default_xmax = len(transmission[0].energy)-1 + default_xmax = len(transmission[0].energy) - 1 def plot_transmission(_index, xrange): - _transmission = transmission[_index] xmin, xmax = xrange @@ -237,25 +232,33 @@ def plot_transmission(_index, xrange): y_axis = _transmission.transmission[::-1] y_error = _transmission.uncertainty[::-1] - ax.errorbar(x_axis[xmin:xmax], y_axis[xmin:xmax], yerr=y_error[xmin:xmax], fmt="-o", markersize=2, label="Transmission") + ax.errorbar( + x_axis[xmin:xmax], + y_axis[xmin:xmax], + yerr=y_error[xmin:xmax], + fmt="-o", + markersize=2, + label="Transmission", + ) ax.set_xlabel("Energy (eV)") ax.set_ylabel("Transmission") ax.set_title("Normalized Transmission Spectrum") plt.show() - display(HTML(f"Metadata:")) + display(HTML("Metadata:")) - n_dead_pixels = _transmission.metadata['n_dead_pixels'] - n_valid_pixels = _transmission.metadata['n_valid_pixels'] - sample_proton_charge = _transmission.metadata['sample_proton_charge'] - ob_proton_charge = _transmission.metadata['ob_proton_charge'] - pc_uncertainty_sample = _transmission.metadata['pc_uncertainty_sample'] - pc_uncertainty_ob = _transmission.metadata['pc_uncertainty_ob'] - method_used = _transmission.metadata['method'] - sample_folder = _transmission.metadata['sample_folder'] - ob_folders = _transmission.metadata['ob_folders'] + n_dead_pixels = _transmission.metadata["n_dead_pixels"] + n_valid_pixels = _transmission.metadata["n_valid_pixels"] + sample_proton_charge = _transmission.metadata["sample_proton_charge"] + ob_proton_charge = _transmission.metadata["ob_proton_charge"] + pc_uncertainty_sample = _transmission.metadata["pc_uncertainty_sample"] + pc_uncertainty_ob = _transmission.metadata["pc_uncertainty_ob"] + method_used = _transmission.metadata["method"] + sample_folder = _transmission.metadata["sample_folder"] + ob_folders = _transmission.metadata["ob_folders"] - display(HTML(f"""
+ display( + HTML(f"""
@@ -266,10 +269,17 @@ def plot_transmission(_index, xrange): -
ParameterValue
Number of Dead Pixels{n_dead_pixels}
Number of Valid Pixels{n_valid_pixels}
OB Folders{ob_folders}
Output Folder{self.output_folder}
ROI Selected{self.roi}
""")) + """) + ) - file_created = os.path.join(self.output_folder, os.path.basename(sample_folder), "_transmission.txt") - display(HTML(f"Transmission file created: {file_created}")) + file_created = os.path.join( + self.output_folder, os.path.basename(sample_folder), "_transmission.txt" + ) + display( + HTML( + f"Transmission file created: {file_created}" + ) + ) display_plot_transmisison = interactive( plot_transmission, @@ -280,16 +290,16 @@ def plot_transmission(_index, xrange): step=1, description="File index:", continuous_update=False, - disabled=True if len(transmission) == 1 else False, + disabled=True if len(transmission) == 1 else False, + layout=widgets.Layout(width="600px"), + ), + xrange=widgets.IntRangeSlider( + value=[default_xmin, default_xmax], + min=default_xmin, + max=default_xmax, + description="E range:", + continuous_update=True, layout=widgets.Layout(width="600px"), ), - xrange=widgets.IntRangeSlider(value=[default_xmin, default_xmax], - min=default_xmin, - max=default_xmax, - description="E range:", - continuous_update=True, - layout=widgets.Layout(width="600px")), - ) display(display_plot_transmisison) - diff --git a/notebooks/__code/normalization_resonance/units.py b/notebooks/__code/normalization_resonance/units.py index 8acb982f1..43c1bc965 100644 --- a/notebooks/__code/normalization_resonance/units.py +++ b/notebooks/__code/normalization_resonance/units.py @@ -135,7 +135,9 @@ def convert_to_cross_section(from_unit, to_unit): return conversion_factors[from_unit] / conversion_factors[to_unit] -def convert_from_wavelength_to_energy_ev(wavelength, unit_from=DistanceUnitOptions.angstrom): +def convert_from_wavelength_to_energy_ev( + wavelength, unit_from=DistanceUnitOptions.angstrom +): """Convert wavelength to energy based on the given units. Args: @@ -177,15 +179,21 @@ def convert_array_from_time_to_lambda( np.ndarray: Array of wavelength values. """ time_array_s = time_array * convert_time_units(time_unit, TimeUnitOptions.s) - detector_offset_s = detector_offset * convert_time_units(detector_offset_unit, TimeUnitOptions.s) + detector_offset_s = detector_offset * convert_time_units( + detector_offset_unit, TimeUnitOptions.s + ) distance_source_detector_m = distance_source_detector * convert_distance_units( distance_source_detector_unit, DistanceUnitOptions.m ) h_over_mn = h / m_n - lambda_m = h_over_mn * (time_array_s + detector_offset_s) / distance_source_detector_m + lambda_m = ( + h_over_mn * (time_array_s + detector_offset_s) / distance_source_detector_m + ) - lambda_converted = lambda_m * convert_distance_units(DistanceUnitOptions.m, lambda_unit) + lambda_converted = lambda_m * convert_distance_units( + DistanceUnitOptions.m, lambda_unit + ) return lambda_converted @@ -225,13 +233,22 @@ def convert_array_from_time_to_energy( detector_units_factor = convert_time_units(detector_offset_unit, TimeUnitOptions.s) detector_offset = detector_units_factor * detector_offset - distance_source_detector_factor = convert_distance_units(distance_source_detector_unit, DistanceUnitOptions.m) - distance_source_detector_m = distance_source_detector * distance_source_detector_factor + distance_source_detector_factor = convert_distance_units( + distance_source_detector_unit, DistanceUnitOptions.m + ) + distance_source_detector_m = ( + distance_source_detector * distance_source_detector_factor + ) # Calculate the energy in eV using the formula E_ev = 1/2 m_n (L/t_tof)^2 / electron_volt full_time_array_s = time_array_s + detector_offset - energy_array_ev = 0.5 * m_n * (distance_source_detector_m / full_time_array_s) ** 2 / electron_volt + energy_array_ev = ( + 0.5 + * m_n + * (distance_source_detector_m / full_time_array_s) ** 2 + / electron_volt + ) energy_array_factor = convert_to_energy(EnergyUnitOptions.eV, energy_unit) energy_array = energy_array_ev * energy_array_factor diff --git a/notebooks/__code/normalization_script.py b/notebooks/__code/normalization_script.py index 69f0b77ec..89a9847a1 100755 --- a/notebooks/__code/normalization_script.py +++ b/notebooks/__code/normalization_script.py @@ -5,9 +5,15 @@ parser = argparse.ArgumentParser(description="Neutron Imaging Normalization") parser.add_argument("-o", "--output", help="output folder", type=str) -parser.add_argument("-sf", "--sample_files", help="comma separated list of samples", type=str) -parser.add_argument("-of", "--ob_files", help="comma separated list of open beams", type=str) -parser.add_argument("-dc", "--dc_files", help="comma separated list of dark current", type=str) +parser.add_argument( + "-sf", "--sample_files", help="comma separated list of samples", type=str +) +parser.add_argument( + "-of", "--ob_files", help="comma separated list of open beams", type=str +) +parser.add_argument( + "-dc", "--dc_files", help="comma separated list of dark current", type=str +) parser.add_argument("-rois", help="colon string of each roi: x0,y0,x1,y1") diff --git a/notebooks/__code/normalization_tof/__init__.py b/notebooks/__code/normalization_tof/__init__.py index 7f54b5b43..e5acfdcef 100644 --- a/notebooks/__code/normalization_tof/__init__.py +++ b/notebooks/__code/normalization_tof/__init__.py @@ -17,8 +17,8 @@ class DataType: ob = "ob" dc = "dc" -class Roi: +class Roi: def __init__(self, left: int = 0, top: int = 0, width: int = 1, height: int = 1): self.left: int = left self.top: int = top diff --git a/notebooks/__code/normalization_tof/config.py b/notebooks/__code/normalization_tof/config.py index 0b23330bf..5da06151a 100644 --- a/notebooks/__code/normalization_tof/config.py +++ b/notebooks/__code/normalization_tof/config.py @@ -13,6 +13,7 @@ class DEBUG_DATA: container_roi = [150, 150, 40, 40] detector_type = DetectorType.tpx1 # timepix1 or timepix3 + ## timepix3 # class DEBUG_DATA: # ipts = "IPTS-35167" diff --git a/notebooks/__code/normalization_tof/normalization_for_timepix1_timepix3.py b/notebooks/__code/normalization_tof/normalization_for_timepix1_timepix3.py index 1774b4ff9..0e7316fda 100644 --- a/notebooks/__code/normalization_tof/normalization_for_timepix1_timepix3.py +++ b/notebooks/__code/normalization_tof/normalization_for_timepix1_timepix3.py @@ -1,21 +1,9 @@ -import argparse -import glob import logging -import multiprocessing as mp import os -import shutil -from pathlib import Path from typing import Tuple -from annotated_types import Not -import h5py -import matplotlib.pyplot as plt import numpy as np -import pandas as pd from IPython.display import HTML, display -from PIL import Image -from skimage.io import imread -from scipy.ndimage import median_filter from __code.normalization_tof.utilities import * @@ -25,11 +13,12 @@ MARKERSIZE = 2 + class NormalizedData: - data= {} - lambda_array= None - tof_array= None - energy_array= None + data = {} + lambda_array = None + tof_array = None + energy_array = None from __code.normalization_tof.units import ( @@ -46,15 +35,18 @@ class NormalizedData: PROTON_CHARGE_TOLERANCE = 0.1 + def initialize_logging(): """initialize logging""" file_name, ext = os.path.splitext(os.path.basename(__file__)) user_name = os.getlogin() # add user name to the log file name log_file_name = os.path.join(LOG_PATH, f"{user_name}_{file_name}.log") - logging.basicConfig(filename=log_file_name, - filemode='w', - format='[%(levelname)s] - %(asctime)s - %(message)s', - level=logging.INFO) + logging.basicConfig( + filename=log_file_name, + filemode="w", + format="[%(levelname)s] - %(asctime)s - %(message)s", + level=logging.INFO, + ) logging.info(f"*** Starting a new script {file_name} ***") @@ -81,11 +73,12 @@ def normalization_with_list_of_full_path( correct_chips_alignment_flag: bool = True, correct_chips_alignment_config: dict = None, export_mode: dict = None, - roi = None, - container_roi = None, - container_roi_file = None) -> NormalizedData: + roi=None, + container_roi=None, + container_roi_file=None, +) -> NormalizedData: """normalize the sample data with ob data using proton charge and shutter counts - + Args: sample_dict (dict): dictionary with sample run numbers and their data {base_name_run1: {'full_path': full_path, 'nexus': nexus_path}, @@ -118,7 +111,7 @@ def normalization_with_list_of_full_path( correct_chips_alignment_config (dict): configuration for chips alignment correction export_mode (dict): dictionary with export options roi (Roi): region of interest for full spectrum normalization - container_roi (Roi): region of interest for container only normalization + container_roi (Roi): region of interest for container only normalization container_roi_file (str): file path to container ROI file (scitiff format) (will take precedence over container_roi if both are provided) Returns: @@ -143,33 +136,39 @@ def normalization_with_list_of_full_path( export_corrected_stack_of_sample_data = export_mode.get("sample_stack", False) export_corrected_stack_of_ob_data = export_mode.get("ob_stack", False) - export_corrected_stack_of_normalized_data = export_mode.get("normalized_stack", False) + export_corrected_stack_of_normalized_data = export_mode.get( + "normalized_stack", False + ) # export_corrected_stack_of_combined_normalized_data = export_mode.get("combined_normalized_stack", False) - export_corrected_integrated_sample_data = export_mode.get("sample_integrated", False) + export_corrected_integrated_sample_data = export_mode.get( + "sample_integrated", False + ) export_corrected_integrated_ob_data = export_mode.get("ob_integrated", False) - export_corrected_integrated_normalized_data = export_mode.get("normalized_integrated", False) + export_corrected_integrated_normalized_data = export_mode.get( + "normalized_integrated", False + ) # export_corrected_integrated_combined_normalized_data = export_mode.get("combined_normalized_integrated", False) export_x_axis = export_mode.get("x_axis", True) logging.info("Input parameters:") - + logging.info(f"\t{sample_dict = }") logging.info(f"\t{combine_samples =}") logging.info(f"\t{ob_dict = }") logging.info(f"\t{dc_dict = }") logging.info(f"{spectra_array = }") - - logging.info(f"") - logging.info(f"- export mode:") + + logging.info("") + logging.info("- export mode:") logging.info(f"\t{export_corrected_stack_of_sample_data = }") logging.info(f"\t{export_corrected_stack_of_ob_data = }") logging.info(f"\t{export_corrected_stack_of_normalized_data = }") logging.info(f"\t{export_corrected_integrated_sample_data = }") logging.info(f"\t{export_corrected_integrated_ob_data = }") logging.info(f"\t{export_corrected_integrated_normalized_data = }") - - logging.info(f"") + + logging.info("") logging.info(f"{roi =}") logging.info(f"{export_x_axis = }") logging.info(f"{proton_charge_flag = }") @@ -179,34 +178,35 @@ def normalization_with_list_of_full_path( logging.info(f"{max_iterations = }") logging.info(f"{correct_chips_alignment_flag = }") logging.info(f"{distance_source_detector_m = }") - logging.info(f"") - + logging.info("") + sample_master_dict, sample_status_metadata = create_master_dict( - data_dictionary=sample_dict, - data_type=DataType.sample, + data_dictionary=sample_dict, + data_type=DataType.sample, instrument=instrument, spectra_array=spectra_array, ) ob_master_dict, ob_status_metadata = create_master_dict( - data_dictionary=ob_dict, - data_type=DataType.ob, + data_dictionary=ob_dict, + data_type=DataType.ob, instrument=instrument, spectra_array=spectra_array, ) dc_master_dict, dc_status_metadata = create_master_dict( - data_dictionary=dc_dict, - data_type=DataType.dc, + data_dictionary=dc_dict, + data_type=DataType.dc, instrument=instrument, spectra_array=spectra_array, ) # load ob images =============================== load_images(master_dict=ob_master_dict, data_type=DataType.ob, verbose=verbose) - + if proton_charge_flag: normalized_by_proton_charge = ( - sample_status_metadata.all_proton_charge_found and ob_status_metadata.all_proton_charge_found + sample_status_metadata.all_proton_charge_found + and ob_status_metadata.all_proton_charge_found ) else: normalized_by_proton_charge = False @@ -214,26 +214,36 @@ def normalization_with_list_of_full_path( # combine all ob images ob_data_combined, ob_sum_proton_charge = combine_images( - data_type=DataType.ob, - master_dict=ob_master_dict, - use_proton_charge=normalized_by_proton_charge, - replace_zeros_by_nan=replace_ob_zeros_by_nan_flag, - replace_zeros_by_local_median=replace_ob_zeros_by_local_median_flag, - kernel_size_for_local_median=kernel_size_for_local_median, - max_iterations=max_iterations, - ) + data_type=DataType.ob, + master_dict=ob_master_dict, + use_proton_charge=normalized_by_proton_charge, + replace_zeros_by_nan=replace_ob_zeros_by_nan_flag, + replace_zeros_by_local_median=replace_ob_zeros_by_local_median_flag, + kernel_size_for_local_median=kernel_size_for_local_median, + max_iterations=max_iterations, + ) logging.info(f"{ob_data_combined.shape = }") logging.info(f"{ob_sum_proton_charge = }") - logging.info(f"number of NaN in ob_data_combined data: {np.sum(np.isnan(ob_data_combined))}") - logging.info(f"number of inf in ob_data_combined data: {np.sum(np.isinf(ob_data_combined))}") - logging.info(f"number of zeros in ob_data_combined data: {np.sum(ob_data_combined == 0)} ") + logging.info( + f"number of NaN in ob_data_combined data: {np.sum(np.isnan(ob_data_combined))}" + ) + logging.info( + f"number of inf in ob_data_combined data: {np.sum(np.isinf(ob_data_combined))}" + ) + logging.info( + f"number of zeros in ob_data_combined data: {np.sum(ob_data_combined == 0)} " + ) if correct_chips_alignment_flag: - correct_chips_alignment(ob_data_combined, correct_chips_alignment_config, verbose=verbose) + correct_chips_alignment( + ob_data_combined, correct_chips_alignment_config, verbose=verbose + ) - ob_data_combined_for_spectrum = calculate_ob_data_combined_used_by_spectrum_normalization(roi=roi, - ob_data_combined=ob_data_combined, - verbose=verbose) + ob_data_combined_for_spectrum = ( + calculate_ob_data_combined_used_by_spectrum_normalization( + roi=roi, ob_data_combined=ob_data_combined, verbose=verbose + ) + ) # export ob data if requested first_ob_run_number = list(ob_master_dict.keys())[0] @@ -244,133 +254,166 @@ def normalization_with_list_of_full_path( export_corrected_stack_of_ob_data, export_corrected_integrated_ob_data, ob_data_combined, - spectra_file_name=ob_master_dict[first_ob_run_number][MasterDictKeys.spectra_file_name], + spectra_file_name=ob_master_dict[first_ob_run_number][ + MasterDictKeys.spectra_file_name + ], spectra_array=spectra_array, ) # load dc images ================================ - dc_master_dict = load_images(master_dict=dc_master_dict, data_type=DataType.dc, verbose=verbose) + dc_master_dict = load_images( + master_dict=dc_master_dict, data_type=DataType.dc, verbose=verbose + ) # combine all dc images dc_data_combined = combine_dc_images(dc_master_dict) if correct_chips_alignment_flag: - dc_data_combined = correct_chips_alignment(dc_data_combined, correct_chips_alignment_config, verbose=verbose) + dc_data_combined = correct_chips_alignment( + dc_data_combined, correct_chips_alignment_config, verbose=verbose + ) if (dc_data_combined is not None) and (roi is not None): - dc_data_combined_for_spectrum = [np.sum(np.sum(_data, axis=0), axis=0) for _data in dc_data_combined] + dc_data_combined_for_spectrum = [ + np.sum(np.sum(_data, axis=0), axis=0) for _data in dc_data_combined + ] logging.info(f"\t{np.shape(dc_data_combined) = }") logging.info(f"\t{np.shape(dc_data_combined_for_spectrum) = }") else: - logging.info(f"\tno roi provided! Skipping the normalization of spectrum.") + logging.info("\tno roi provided! Skipping the normalization of spectrum.") dc_data_combined_for_spectrum = None # load sample images =============================== - load_images(master_dict=sample_master_dict, data_type=DataType.sample, verbose=verbose) + load_images( + master_dict=sample_master_dict, data_type=DataType.sample, verbose=verbose + ) if correct_chips_alignment_flag: - correct_all_samples_chips_alignment(sample_master_dict, correct_chips_alignment_config, verbose=verbose) + correct_all_samples_chips_alignment( + sample_master_dict, correct_chips_alignment_config, verbose=verbose + ) normalized_data = {} integrated_normalized_data = {} spectrum_normalized_data = {} if combine_samples: - - # combine all sample images and then perform normalization + # combine all sample images and then perform normalization sample_data_combined, sample_sum_proton_charge = combine_images( - data_type=DataType.sample, - master_dict=sample_master_dict, - use_proton_charge=normalized_by_proton_charge, - # use_monitor_counts=normalized_by_monitor_counts, - replace_zeros_by_nan=False, - replace_zeros_by_local_median=replace_ob_zeros_by_local_median_flag, - kernel_size_for_local_median=kernel_size_for_local_median, - max_iterations=max_iterations, - ) + data_type=DataType.sample, + master_dict=sample_master_dict, + use_proton_charge=normalized_by_proton_charge, + # use_monitor_counts=normalized_by_monitor_counts, + replace_zeros_by_nan=False, + replace_zeros_by_local_median=replace_ob_zeros_by_local_median_flag, + kernel_size_for_local_median=kernel_size_for_local_median, + max_iterations=max_iterations, + ) logging.info("**********************************") list_run_number = list(sample_master_dict.keys()) - str_list_run_number = '_'.join([str(r) for r in list_run_number]) + str_list_run_number = "_".join([str(r) for r in list_run_number]) logging.info(f"normalization of combined sample runs {list_run_number}") if verbose: display(HTML(f"Normalization of combined sample runs {list_run_number}")) - + # get statistics of sample data - logging_statistics_of_data(data=sample_data_combined, data_type=DataType.sample_combined) - + logging_statistics_of_data( + data=sample_data_combined, data_type=DataType.sample_combined + ) + if normalized_by_proton_charge: - logging.info(f"Normalizing by proton charge") + logging.info("Normalizing by proton charge") logging.info(f"\t{sample_sum_proton_charge = }") if verbose: - display(HTML(f"Normalizing by proton charge")) - sample_data_combined /= sample_sum_proton_charge + display(HTML("Normalizing by proton charge")) + sample_data_combined /= sample_sum_proton_charge if (container_roi is not None) or (container_roi_file is not None): - logging.info(f"Applying container normalization:") - logging.info(f"\t {container_roi = }") - logging.info(f"\t {container_roi_file = }") - if verbose: - display(HTML(f"Applying container normalization:")) - - sample_data_combined, container_roi_file = normalize_by_container_roi( - sample_data=sample_data_combined, - container_roi=container_roi, - container_roi_file=container_roi_file, - output_folder=output_folder, - sample_run_number=str_list_run_number, - ) - if verbose and (container_roi_file is not None): - display(HTML(f"Container roi file created: {container_roi_file}.")) + logging.info("Applying container normalization:") + logging.info(f"\t {container_roi = }") + logging.info(f"\t {container_roi_file = }") + if verbose: + display(HTML("Applying container normalization:")) + + sample_data_combined, container_roi_file = normalize_by_container_roi( + sample_data=sample_data_combined, + container_roi=container_roi, + container_roi_file=container_roi_file, + output_folder=output_folder, + sample_run_number=str_list_run_number, + ) + if verbose and (container_roi_file is not None): + display(HTML(f"Container roi file created: {container_roi_file}.")) # export sample data after correction if requested - if export_corrected_stack_of_sample_data or export_corrected_integrated_sample_data: + if ( + export_corrected_stack_of_sample_data + or export_corrected_integrated_sample_data + ): export_sample_images( output_folder, export_corrected_stack_of_sample_data, export_corrected_integrated_sample_data, str_list_run_number, sample_data_combined, - spectra_file_name=sample_master_dict[list_run_number[0]][MasterDictKeys.spectra_file_name], + spectra_file_name=sample_master_dict[list_run_number[0]][ + MasterDictKeys.spectra_file_name + ], spectra_array=spectra_array, ) - _normalized_dict = perform_normalization(sample_data_combined, ob_data_combined, dc_data_combined) - _normalized_data = _normalized_dict['normalized_data'] - _integrated_normalized_data = _normalized_dict['integrated_normalized_data'] + _normalized_dict = perform_normalization( + sample_data_combined, ob_data_combined, dc_data_combined + ) + _normalized_data = _normalized_dict["normalized_data"] + _integrated_normalized_data = _normalized_dict["integrated_normalized_data"] integrated_normalized_data[str_list_run_number] = _integrated_normalized_data normalized_data[str_list_run_number] = _normalized_data - _spectrum_normalized_data = perform_spectrum_normalization(roi=roi, - sample_data=sample_data_combined, - ob_data_combined_for_spectrum=ob_data_combined_for_spectrum, - dc_data_combined=dc_data_combined, - dc_data_combined_for_spectrum=dc_data_combined_for_spectrum) + _spectrum_normalized_data = perform_spectrum_normalization( + roi=roi, + sample_data=sample_data_combined, + ob_data_combined_for_spectrum=ob_data_combined_for_spectrum, + dc_data_combined=dc_data_combined, + dc_data_combined_for_spectrum=dc_data_combined_for_spectrum, + ) spectrum_normalized_data[str_list_run_number] = _spectrum_normalized_data # normalized_data[_sample_run_number] = np.array(np.divide(_sample_data, ob_data_combined)) logging.info(f"{normalized_data[str_list_run_number].shape = }") logging.info(f"{normalized_data[str_list_run_number].dtype = }") - logging.info(f"number of NaN in normalized data: {np.sum(np.isnan(normalized_data[str_list_run_number]))}") - logging.info(f"number of inf in normalized data: {np.sum(np.isinf(normalized_data[str_list_run_number]))}") + logging.info( + f"number of NaN in normalized data: {np.sum(np.isnan(normalized_data[str_list_run_number]))}" + ) + logging.info( + f"number of inf in normalized data: {np.sum(np.isinf(normalized_data[str_list_run_number]))}" + ) - detector_delay_us = sample_master_dict[list_run_number[0]][MasterDictKeys.detector_delay_us] - time_spectra = sample_master_dict[list_run_number[0]][MasterDictKeys.list_spectra] + detector_delay_us = sample_master_dict[list_run_number[0]][ + MasterDictKeys.detector_delay_us + ] + time_spectra = sample_master_dict[list_run_number[0]][ + MasterDictKeys.list_spectra + ] dict_to_return.tof_array = time_spectra if time_spectra is None: - logging.info("Time spectra is None, cannot convert to lambda or energy arrays") + logging.info( + "Time spectra is None, cannot convert to lambda or energy arrays" + ) lambda_array = None energy_array = None - - else: - logging.info(f"We have a time_spectra!") + else: + logging.info("We have a time_spectra!") logging.info(f"time spectra shape: {time_spectra.shape}") - + if detector_delay_us is None: detector_delay_us = 0.0 - logging.info(f"detector delay is None, setting it to {detector_delay_us} us") + logging.info( + f"detector delay is None, setting it to {detector_delay_us} us" + ) logging.info(f"we have a detector delay of {detector_delay_us} us") @@ -403,42 +446,46 @@ def normalization_with_list_of_full_path( logging.info(f"Preview: {preview = }") if preview: - preview_normalized_data(sample_data_combined, - ob_data_combined, - dc_data_combined, - normalized_data, - lambda_array, - energy_array, - detector_delay_us, - str_list_run_number, - combine_samples, - _spectrum_normalized_data, - roi, - ) - - if export_corrected_integrated_normalized_data or export_corrected_stack_of_normalized_data: - - export_normalized_data(ob_master_dict=ob_master_dict, - sample_master_dict=sample_master_dict, + preview_normalized_data( + sample_data_combined, + ob_data_combined, + dc_data_combined, + normalized_data, + lambda_array, + energy_array, + detector_delay_us, + str_list_run_number, + combine_samples, + _spectrum_normalized_data, + roi, + ) + + if ( + export_corrected_integrated_normalized_data + or export_corrected_stack_of_normalized_data + ): + export_normalized_data( + ob_master_dict=ob_master_dict, + sample_master_dict=sample_master_dict, _sample_run_number=str_list_run_number, - normalized_data=normalized_data, + normalized_data=normalized_data, integrated_normalized_data=integrated_normalized_data, _spectrum_normalized_data=_spectrum_normalized_data, - lambda_array=lambda_array, - energy_array=energy_array, - output_folder=output_folder, + lambda_array=lambda_array, + energy_array=energy_array, + output_folder=output_folder, export_corrected_stack_of_normalized_data=export_corrected_stack_of_normalized_data, export_corrected_integrated_normalized_data=export_corrected_integrated_normalized_data, roi=roi, spectra_array=spectra_array, - spectra_file=sample_master_dict[list_run_number[0]][MasterDictKeys.spectra_file_name]) + spectra_file=sample_master_dict[list_run_number[0]][ + MasterDictKeys.spectra_file_name + ], + ) else: - - # normalize the sample data for _sample_run_number in sample_master_dict.keys(): - logging.info("**********************************") logging.info(f"normalization of run {_sample_run_number}") if verbose: @@ -448,21 +495,21 @@ def normalization_with_list_of_full_path( # get statistics of sample data logging_statistics_of_data(data=_sample_data, data_type=DataType.sample) - + if normalized_by_proton_charge: if verbose: - display(HTML(f"Normalizing by proton charge")) - _sample_data = normalize_by_proton_charge(sample_master_dict, - _sample_run_number, - _sample_data) + display(HTML("Normalizing by proton charge")) + _sample_data = normalize_by_proton_charge( + sample_master_dict, _sample_run_number, _sample_data + ) if (container_roi is not None) or (container_roi_file is not None): - logging.info(f"Applying container normalization:") + logging.info("Applying container normalization:") logging.info(f"\t {container_roi = }") logging.info(f"\t {container_roi_file = }") if verbose: - display(HTML(f"Applying container normalization:")) - + display(HTML("Applying container normalization:")) + _sample_data, container_roi_file = normalize_by_container_roi( sample_data=_sample_data, container_roi=container_roi, @@ -479,54 +526,74 @@ def normalization_with_list_of_full_path( logging.info(f"{ob_data_combined.dtype = }") # export sample data after correction if requested - if export_corrected_stack_of_sample_data or export_corrected_integrated_sample_data: + if ( + export_corrected_stack_of_sample_data + or export_corrected_integrated_sample_data + ): export_sample_images( output_folder, export_corrected_stack_of_sample_data, export_corrected_integrated_sample_data, _sample_run_number, _sample_data, - spectra_file_name=sample_master_dict[_sample_run_number][MasterDictKeys.spectra_file_name], + spectra_file_name=sample_master_dict[_sample_run_number][ + MasterDictKeys.spectra_file_name + ], spectra_array=spectra_array, ) - _normalized_dict = perform_normalization(_sample_data, ob_data_combined, dc_data_combined) - _normalized_data = _normalized_dict['normalized_data'] - _integrated_normalized_data = _normalized_dict['integrated_normalized_data'] + _normalized_dict = perform_normalization( + _sample_data, ob_data_combined, dc_data_combined + ) + _normalized_data = _normalized_dict["normalized_data"] + _integrated_normalized_data = _normalized_dict["integrated_normalized_data"] integrated_normalized_data[_sample_run_number] = _integrated_normalized_data normalized_data[_sample_run_number] = _normalized_data - _spectrum_normalized_data = perform_spectrum_normalization(roi=roi, - sample_data=_sample_data, - ob_data_combined_for_spectrum=ob_data_combined_for_spectrum, - dc_data_combined=dc_data_combined, - dc_data_combined_for_spectrum=dc_data_combined_for_spectrum) + _spectrum_normalized_data = perform_spectrum_normalization( + roi=roi, + sample_data=_sample_data, + ob_data_combined_for_spectrum=ob_data_combined_for_spectrum, + dc_data_combined=dc_data_combined, + dc_data_combined_for_spectrum=dc_data_combined_for_spectrum, + ) spectrum_normalized_data[_sample_run_number] = _spectrum_normalized_data # normalized_data[_sample_run_number] = np.array(np.divide(_sample_data, ob_data_combined)) logging.info(f"{normalized_data[_sample_run_number].shape = }") logging.info(f"{normalized_data[_sample_run_number].dtype = }") - logging.info(f"number of NaN in normalized data: {np.sum(np.isnan(normalized_data[_sample_run_number]))}") - logging.info(f"number of inf in normalized data: {np.sum(np.isinf(normalized_data[_sample_run_number]))}") + logging.info( + f"number of NaN in normalized data: {np.sum(np.isnan(normalized_data[_sample_run_number]))}" + ) + logging.info( + f"number of inf in normalized data: {np.sum(np.isinf(normalized_data[_sample_run_number]))}" + ) - detector_delay_us = sample_master_dict[_sample_run_number][MasterDictKeys.detector_delay_us] - time_spectra = sample_master_dict[_sample_run_number][MasterDictKeys.list_spectra] + detector_delay_us = sample_master_dict[_sample_run_number][ + MasterDictKeys.detector_delay_us + ] + time_spectra = sample_master_dict[_sample_run_number][ + MasterDictKeys.list_spectra + ] dict_to_return.tof_array = time_spectra if time_spectra is None: - logging.info("Time spectra is None, cannot convert to lambda or energy arrays") + logging.info( + "Time spectra is None, cannot convert to lambda or energy arrays" + ) lambda_array = None energy_array = None - - else: - logging.info(f"We have a time_spectra!") + else: + logging.info("We have a time_spectra!") logging.info(f"time spectra shape: {time_spectra.shape}") - + if detector_delay_us is None: detector_delay_us = 0.0 - logging.info(f"detector delay is None, setting it to {detector_delay_us} us") + logging.info( + f"detector delay is None, setting it to {detector_delay_us} us" + ) logging.info(f"we have a detector delay of {detector_delay_us} us") @@ -559,36 +626,43 @@ def normalization_with_list_of_full_path( logging.info(f"Preview: {preview = }") if preview: - preview_normalized_data(_sample_data, - ob_data_combined, - dc_data_combined, - normalized_data, - lambda_array, - energy_array, - detector_delay_us, - _sample_run_number, - combine_samples, - _spectrum_normalized_data, - roi, - ) - - if export_corrected_integrated_normalized_data or export_corrected_stack_of_normalized_data: - - export_normalized_data(ob_master_dict=ob_master_dict, - sample_master_dict=sample_master_dict, + preview_normalized_data( + _sample_data, + ob_data_combined, + dc_data_combined, + normalized_data, + lambda_array, + energy_array, + detector_delay_us, + _sample_run_number, + combine_samples, + _spectrum_normalized_data, + roi, + ) + + if ( + export_corrected_integrated_normalized_data + or export_corrected_stack_of_normalized_data + ): + export_normalized_data( + ob_master_dict=ob_master_dict, + sample_master_dict=sample_master_dict, _sample_run_number=_sample_run_number, - normalized_data=normalized_data, + normalized_data=normalized_data, integrated_normalized_data=integrated_normalized_data, _spectrum_normalized_data=_spectrum_normalized_data, - lambda_array=lambda_array, - energy_array=energy_array, - output_folder=output_folder, + lambda_array=lambda_array, + energy_array=energy_array, + output_folder=output_folder, export_corrected_stack_of_normalized_data=export_corrected_stack_of_normalized_data, export_corrected_integrated_normalized_data=export_corrected_integrated_normalized_data, roi=roi, spectra_array=spectra_array, - spectra_file=sample_master_dict[_sample_run_number][MasterDictKeys.spectra_file_name]) - + spectra_file=sample_master_dict[_sample_run_number][ + MasterDictKeys.spectra_file_name + ], + ) + # if combine_samples: # # combine all normalized data @@ -602,7 +676,7 @@ def normalization_with_list_of_full_path( # # if preview, display the combined normalized data # if preview: - + # fig, axs3 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) # normalized_data_integrated = np.nanmean(combined_normalized_data, axis=0) # im2 = axs3[0].imshow(normalized_data_integrated, cmap="gray") @@ -616,7 +690,7 @@ def normalization_with_list_of_full_path( # else: # profile_step1 = np.nanmean(combined_normalized_data, axis=1) # profile = np.nanmean(profile_step1, axis=1) - + # axs3[1].plot(profile, 'o', markersize=MARKERSIZE, label=_label) # axs3[1].set_xlabel("File image index") # axs3[1].set_ylabel("Transmission (a.u.)") @@ -648,15 +722,15 @@ def normalization_with_list_of_full_path( # fig, axs5 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) # logging.info(f"{np.shape(profile) = }") - # axs5[0].plot(lambda_array, combined_spectrum_normalized_data, "r*", - # markersize=MARKERSIZE, + # axs5[0].plot(lambda_array, combined_spectrum_normalized_data, "r*", + # markersize=MARKERSIZE, # label="spectrum normalization of ROI") # axs5[0].set_xlabel("Lambda (A)") # axs5[0].set_ylabel("mean of full image") # axs5[0].legend() # axs5[1].plot(energy_array, combined_spectrum_normalized_data, "r*", - # markersize=MARKERSIZE, + # markersize=MARKERSIZE, # label="spectrum normalization of ROI") # axs5[1].set_xlabel("Energy (eV)") # axs5[1].set_ylabel("Transmission (a.u.)") @@ -675,9 +749,9 @@ def normalization_with_list_of_full_path( # export_corrected_stack_of_combined_normalized_data=export_corrected_stack_of_combined_normalized_data, # lambda_array=lambda_array, # energy_array=energy_array, - # output_folder=output_folder, + # output_folder=output_folder, # spectra_array=spectra_array) - + # else: # dict_to_return.data = normalized_data @@ -688,4 +762,3 @@ def normalization_with_list_of_full_path( display(HTML("Normalization and export is done!")) return dict_to_return - diff --git a/notebooks/__code/normalization_tof/normalization_tof.py b/notebooks/__code/normalization_tof/normalization_tof.py index 485f82364..7f4d0e610 100644 --- a/notebooks/__code/normalization_tof/normalization_tof.py +++ b/notebooks/__code/normalization_tof/normalization_tof.py @@ -1,10 +1,8 @@ import glob import logging import logging as notebook_logging -from multiprocessing.util import debug import os from pathlib import Path -from arrow import get import numpy as np import pandas as pd @@ -20,12 +18,16 @@ from __code._utilities.list import extract_list_of_runs_from_string from __code._utilities.nexus import extract_file_path_from_nexus from __code.normalization_tof import DataType -from __code._utilities.time import get_current_time_in_special_file_name_format -from __code._utilities.json import save_json, load_json +from __code._utilities.json import load_json # from __code.ipywe.myfileselector import MyFileSelectorPanel from __code.ipywe.fileselector import FileSelectorPanel as MyFileSelectorPanel -from __code.normalization_tof import DetectorType, autoreduce_dir, distance_source_detector_m, raw_dir +from __code.normalization_tof import ( + DetectorType, + autoreduce_dir, + distance_source_detector_m, + raw_dir, +) from __code.normalization_tof.config import DEBUG_DATA, timepix1_config, timepix3_config from __code.normalization_tof.normalization_for_timepix1_timepix3 import ( load_data_using_multithreading, @@ -39,9 +41,9 @@ class NormalizationTof: sample_folder = None sample_run_numbers = None sample_run_numbers_selected = None - + # if the spectra file is missing, the program will create the spectra array on the fly - spectra_array = None + spectra_array = None spectra_file_found = True list_spectra_file_found = [] @@ -58,7 +60,7 @@ class NormalizationTof: dc_run_numbers_selected = None output_folder = None - + # {'full_path_data': {'data': None, 'nexus': None}} dict_sample = {} dict_ob = {} @@ -72,15 +74,15 @@ class NormalizationTof: dict_dc_data = None roi = None # full spectrum ROI - + # container - container_roi = None # container only ROI + container_roi = None # container only ROI default_roi = Roi(left=50, top=50, width=200, height=200) default_container_roi = Roi(left=150, top=150, width=40, height=40) we_need_to_automatically_save_the_container_roi = False rect_container = None container_roi_file = None - + def initialize(self): LOG_PATH = "/SNS/VENUS/shared/log/" file_name, ext = os.path.splitext(os.path.basename(__file__)) @@ -98,21 +100,26 @@ def initialize(self): # logging.info(f"Creating instance of {cls.__name__}") def __init__(self, working_dir=None, debug=False): - self.initialize() if debug: self.working_dir = DEBUG_DATA.working_dir self.shared_dir = self.working_dir + "/shared" self.output_dir = DEBUG_DATA.output_folder - self.default_roi = Roi(left=DEBUG_DATA.roi[0], top=DEBUG_DATA.roi[1], - width=DEBUG_DATA.roi[2], height=DEBUG_DATA.roi[3]) - self.default_container_roi = Roi(left=DEBUG_DATA.container_roi[0], - top=DEBUG_DATA.container_roi[1], - width=DEBUG_DATA.container_roi[2], - height=DEBUG_DATA.container_roi[3]) + self.default_roi = Roi( + left=DEBUG_DATA.roi[0], + top=DEBUG_DATA.roi[1], + width=DEBUG_DATA.roi[2], + height=DEBUG_DATA.roi[3], + ) + self.default_container_roi = Roi( + left=DEBUG_DATA.container_roi[0], + top=DEBUG_DATA.container_roi[1], + width=DEBUG_DATA.container_roi[2], + height=DEBUG_DATA.container_roi[3], + ) self.detector_type = DEBUG_DATA.detector_type - + else: self.working_dir = working_dir self.shared_dir = os.path.join(self.working_dir, "shared") @@ -137,7 +144,9 @@ def __init__(self, working_dir=None, debug=False): notebook_logging.info(f"nexus folder: {self.nexus_folder}") notebook_logging.info(f"Shared dir: {self.shared_dir}") - display(HTML("Select detector type")) + display( + HTML("Select detector type") + ) self.detector_type_widget = widgets.Dropdown( options=[DetectorType.tpx1_legacy, DetectorType.tpx1, DetectorType.tpx3], value=self.detector_type, @@ -145,7 +154,7 @@ def __init__(self, working_dir=None, debug=False): disabled=False, ) display(self.detector_type_widget) - + def reset_sample_dicts(self): self.dict_sample = {} self.dict_short_name_full_path["sample"] = {} @@ -168,7 +177,9 @@ def reset_dc_dicts(self): def setup_default_paths(self): notebook_logging.info("Setting up default paths...") self.detector_type = self.detector_type_widget.value - self.raw_dir = Path(raw_dir[self.instrument][self.detector_type][0]) / str(self.ipts) + self.raw_dir = Path(raw_dir[self.instrument][self.detector_type][0]) / str( + self.ipts + ) self.autoreduce_dir = ( Path(autoreduce_dir[self.instrument][self.detector_type][0]) / str(self.ipts) @@ -200,7 +211,9 @@ def select_sample_run_numbers(self): ) self.sample_run_numbers_widget = widgets.Textarea( - value=str_sample_run_numbers, placeholder="", layout=widgets.Layout(width="400px") + value=str_sample_run_numbers, + placeholder="", + layout=widgets.Layout(width="400px"), ) vertical_layout = widgets.VBox( [ @@ -230,9 +243,13 @@ def retrieve_file_path_from_nexus(self, run_number): Retrieve the full path to the NeXus file for the given run number. This function should be implemented to read the NeXus file and extract the path. """ - notebook_logging.info(f"Retrieving file path from NeXus for run number: {run_number}") + notebook_logging.info( + f"Retrieving file path from NeXus for run number: {run_number}" + ) # Placeholder implementation, replace with actual logic to read NeXus file - nexus_file_path = Path(self.nexus_folder) / f"{self.instrument.upper()}_{run_number}.nxs.h5" + nexus_file_path = ( + Path(self.nexus_folder) / f"{self.instrument.upper()}_{run_number}.nxs.h5" + ) notebook_logging.info(f"\tNeXus file path: {nexus_file_path}") if nexus_file_path.exists(): return extract_file_path_from_nexus(nexus_file_path) @@ -243,7 +260,9 @@ def extract_full_path(self, run_number=None): """ Extract the full path to the run number based on the detector type. """ - notebook_logging.info(f"Extracting full path for run number: {run_number} with detector type: {self.detector_type}") + notebook_logging.info( + f"Extracting full path for run number: {run_number} with detector type: {self.detector_type}" + ) if run_number is None: raise ValueError("Run number must be provided") @@ -261,7 +280,7 @@ def extract_full_path(self, run_number=None): file_path = Path(self.raw_dir) / file_path if file_path is None: raise ValueError(f"No full path file found for run number {run_number}") - + return file_path else: @@ -283,19 +302,21 @@ def display_infos(self, input_full_path=None, spectra_file_found=True): spectra_cell_color = "green" if spectra_file_found else "red" # present result in a table - display(HTML(f""" + display( + HTML(f"""

Information for run: {os.path.basename(input_full_path)}

Nbr TIFFImages heightImages widthData TypeSpectra File Found
{nbr_tiff}{shape[0]}{shape[1]}{dtype}{spectra_file_found}
- """)) + """) + ) def _is_spectra_file_found_and_list(self, full_path): list_files = glob.glob(os.path.join(full_path, "*_Spectra.txt")) if len(list_files) == 0: return False, None - + return os.path.exists(list_files[0]), list_files[0] def check_sample(self): @@ -308,7 +329,9 @@ def check_sample(self): display(HTML("Sample run numbers selected:")) if self.sample_run_numbers_widget.value.strip() != "": - list_of_runs = extract_list_of_runs_from_string(self.sample_run_numbers_widget.value) + list_of_runs = extract_list_of_runs_from_string( + self.sample_run_numbers_widget.value + ) notebook_logging.info(f"\t{list_of_runs = }") list_of_sample_full_path = [] @@ -317,46 +340,79 @@ def check_sample(self): _full_path = self.extract_full_path(run_number=_run) list_of_sample_full_path.append(_full_path) except TypeError as e: - notebook_logging.error(f"Error extracting full path for run number {_run}: {e}") - display(HTML(f"Error extracting full path for run number {_run}: File not found!")) + notebook_logging.error( + f"Error extracting full path for run number {_run}: {e}" + ) + display( + HTML( + f"Error extracting full path for run number {_run}: File not found!" + ) + ) continue logging.info(f"\t{list_of_sample_full_path = }") for _file_full_path in list_of_sample_full_path: - if os.path.exists(_file_full_path): - notebook_logging.info(f"\tSample run number {_file_full_path} - FOUND") - is_valid_run, report_dict = self.check_folder_is_valid(_file_full_path) + notebook_logging.info( + f"\tSample run number {_file_full_path} - FOUND" + ) + is_valid_run, report_dict = self.check_folder_is_valid( + _file_full_path + ) if is_valid_run: nbr_tiff = report_dict["nbr_tiff"] self.check_nbr_tiff[DataType.sample].append(nbr_tiff) - notebook_logging.info(f"\tSample run number {_file_full_path} - FOUND with {nbr_tiff} tif* files") - display(HTML(f"{_file_full_path} - OK")) + notebook_logging.info( + f"\tSample run number {_file_full_path} - FOUND with {nbr_tiff} tif* files" + ) + display( + HTML( + f"{_file_full_path} - OK" + ) + ) self.dict_sample[_file_full_path] = {} - self.dict_short_name_full_path["sample"][os.path.basename(_file_full_path)] = _file_full_path - - _is_spectra_file_found, spectra_file_name = self._is_spectra_file_found_and_list(_file_full_path) + self.dict_short_name_full_path["sample"][ + os.path.basename(_file_full_path) + ] = _file_full_path + + _is_spectra_file_found, spectra_file_name = ( + self._is_spectra_file_found_and_list(_file_full_path) + ) if not _is_spectra_file_found: self.spectra_file_found = False else: self.list_spectra_file_found.append(spectra_file_name) - - self.display_infos(input_full_path=_file_full_path, - spectra_file_found=self.spectra_file_found) + + self.display_infos( + input_full_path=_file_full_path, + spectra_file_found=self.spectra_file_found, + ) else: - display(HTML(f"{_file_full_path} - EMPTY!")) + display( + HTML( + f"{_file_full_path} - EMPTY!" + ) + ) else: - notebook_logging.info(f"\tSample run number {_file_full_path} - NOT FOUND") - display(HTML(f"{_file_full_path} - NOT FOUND!")) + notebook_logging.info( + f"\tSample run number {_file_full_path} - NOT FOUND" + ) + display( + HTML( + f"{_file_full_path} - NOT FOUND!" + ) + ) else: - notebook_logging.info(f"Sample run numbers selected: {self.sample_run_numbers_selected}") + notebook_logging.info( + f"Sample run numbers selected: {self.sample_run_numbers_selected}" + ) if self.sample_run_numbers_selected is None: - display(HTML(f"No sample runs selected!")) + display(HTML("No sample runs selected!")) return - + for _run in self.sample_run_numbers_selected: _run = os.path.abspath(_run) if os.path.exists(_run): @@ -367,35 +423,54 @@ def check_sample(self): nbr_tiff = report_dict["nbr_tiff"] self.check_nbr_tiff[DataType.sample].append(nbr_tiff) display(HTML(f"{_run} - OK")) - notebook_logging.info(f"\tfolder seems to be a valid folder containing {nbr_tiff} tif* files") + notebook_logging.info( + f"\tfolder seems to be a valid folder containing {nbr_tiff} tif* files" + ) self.dict_sample[_run] = {} - self.dict_short_name_full_path["sample"][os.path.basename(_run)] = _run - - _is_spectra_file_found, spectra_file_name = self._is_spectra_file_found_and_list(_run) + self.dict_short_name_full_path["sample"][ + os.path.basename(_run) + ] = _run + + _is_spectra_file_found, spectra_file_name = ( + self._is_spectra_file_found_and_list(_run) + ) if not _is_spectra_file_found: self.spectra_file_found = False else: self.list_spectra_file_found.append(spectra_file_name) - - self.display_infos(input_full_path=_run, - spectra_file_found=self.spectra_file_found) + + self.display_infos( + input_full_path=_run, + spectra_file_found=self.spectra_file_found, + ) else: display(HTML(f"{_run} - EMPTY!")) else: - display(HTML(f"{_run} - NOT FOUND! - ERROR!")) + display( + HTML( + f"{_run} - NOT FOUND! - ERROR!" + ) + ) notebook_logging.info(f"\tSample run number {_run} - NOT FOUND!") - + self.sample_run_numbers_selected = None if len(set(self.check_nbr_tiff[DataType.sample])) > 1: - display(HTML(f"Warning: Different number of TIFF files found in selected sample runs: {self.check_nbr_tiff[DataType.sample]}")) - notebook_logging.info(f"WARNING:Different number of TIFF files found in selected sample runs: {self.check_nbr_tiff[DataType.sample]}") + display( + HTML( + f"Warning: Different number of TIFF files found in selected sample runs: {self.check_nbr_tiff[DataType.sample]}" + ) + ) + notebook_logging.info( + f"WARNING:Different number of TIFF files found in selected sample runs: {self.check_nbr_tiff[DataType.sample]}" + ) def select_ob_folder(self): - self.select_folder(instruction="Browse ob top folder", next_function=self.ob_folder_selected) + self.select_folder( + instruction="Browse ob top folder", next_function=self.ob_folder_selected + ) def select_ob_run_numbers(self): - if self.debug: ob_runs = DEBUG_DATA.ob_runs_selected ob_run_numbers_list = [] @@ -409,10 +484,14 @@ def select_ob_run_numbers(self): else: str_ob_run_numbers = "" - ob_label = widgets.HTML(value="List of ob run numbers (ex: 8705, 8707)") + ob_label = widgets.HTML( + value="List of ob run numbers (ex: 8705, 8707)" + ) self.ob_run_numbers_widget = widgets.Textarea( - value=str_ob_run_numbers, placeholder="", layout=widgets.Layout(width="400px") + value=str_ob_run_numbers, + placeholder="", + layout=widgets.Layout(width="400px"), ) vertical_layout = widgets.VBox( [ @@ -441,7 +520,9 @@ def check_ob(self): self.reset_ob_dicts() if self.ob_run_numbers_widget.value.strip() != "": - list_of_runs = extract_list_of_runs_from_string(self.ob_run_numbers_widget.value) + list_of_runs = extract_list_of_runs_from_string( + self.ob_run_numbers_widget.value + ) notebook_logging.info(f"\t{list_of_runs = }") list_of_ob_full_path = [] @@ -450,43 +531,75 @@ def check_ob(self): _full_path = self.extract_full_path(run_number=_run) list_of_ob_full_path.append(_full_path) except TypeError as e: - notebook_logging.error(f"Error extracting full path for run number {_run}: {e}") - display(HTML(f"Error extracting full path for run number {_run}: File not found!")) + notebook_logging.error( + f"Error extracting full path for run number {_run}: {e}" + ) + display( + HTML( + f"Error extracting full path for run number {_run}: File not found!" + ) + ) continue for _file_full_path in list_of_ob_full_path: if os.path.exists(_file_full_path): notebook_logging.info(f"\tOB run number {_file_full_path} - FOUND") - is_valid_run, report_dict = self.check_folder_is_valid(_file_full_path) + is_valid_run, report_dict = self.check_folder_is_valid( + _file_full_path + ) if is_valid_run: nbr_tiff = report_dict["nbr_tiff"] self.check_nbr_tiff[DataType.ob].append(nbr_tiff) - notebook_logging.info(f"\tOB run number {_file_full_path} - FOUND with {nbr_tiff} tif* files") - display(HTML(f"{_file_full_path} - OK")) + notebook_logging.info( + f"\tOB run number {_file_full_path} - FOUND with {nbr_tiff} tif* files" + ) + display( + HTML( + f"{_file_full_path} - OK" + ) + ) self.dict_ob[_file_full_path] = {} - self.dict_short_name_full_path["ob"][os.path.basename(_file_full_path)] = _file_full_path - - _is_spectra_file_found, spectra_file_name = self._is_spectra_file_found_and_list(_file_full_path) + self.dict_short_name_full_path["ob"][ + os.path.basename(_file_full_path) + ] = _file_full_path + + _is_spectra_file_found, spectra_file_name = ( + self._is_spectra_file_found_and_list(_file_full_path) + ) if not _is_spectra_file_found: self.spectra_file_found = False else: self.list_spectra_file_found.append(spectra_file_name) - - self.display_infos(input_full_path=_file_full_path, - spectra_file_found=self.spectra_file_found) - + + self.display_infos( + input_full_path=_file_full_path, + spectra_file_found=self.spectra_file_found, + ) + else: - display(HTML(f"{_file_full_path} - EMPTY!")) + display( + HTML( + f"{_file_full_path} - EMPTY!" + ) + ) else: - notebook_logging.info(f"\tOB run number {_file_full_path} - NOT FOUND") - display(HTML(f"{_file_full_path} - NOT FOUND!")) + notebook_logging.info( + f"\tOB run number {_file_full_path} - NOT FOUND" + ) + display( + HTML( + f"{_file_full_path} - NOT FOUND!" + ) + ) else: - notebook_logging.info(f"OB run numbers selected: {self.ob_run_numbers_selected}") + notebook_logging.info( + f"OB run numbers selected: {self.ob_run_numbers_selected}" + ) if self.ob_run_numbers_selected is None: - display(HTML(f"No OB runs selected!")) + display(HTML("No OB runs selected!")) return - + for _run in self.ob_run_numbers_selected: _run = os.path.abspath(_run) if os.path.exists(_run): @@ -497,40 +610,66 @@ def check_ob(self): nbr_tiff = report_dict["nbr_tiff"] self.check_nbr_tiff[DataType.ob].append(nbr_tiff) display(HTML(f"{_run} - OK")) - notebook_logging.info(f"\tfolder seems to be a valid folder containing {nbr_tiff} tif* files") - self.dict_short_name_full_path["ob"][os.path.basename(_run)] = _run + notebook_logging.info( + f"\tfolder seems to be a valid folder containing {nbr_tiff} tif* files" + ) + self.dict_short_name_full_path["ob"][os.path.basename(_run)] = ( + _run + ) self.dict_ob[_run] = {} - _is_spectra_file_found, spectra_file_name = self._is_spectra_file_found_and_list(_run) + _is_spectra_file_found, spectra_file_name = ( + self._is_spectra_file_found_and_list(_run) + ) if not _is_spectra_file_found: self.spectra_file_found = False else: self.list_spectra_file_found.append(spectra_file_name) - - self.display_infos(input_full_path=_run, - spectra_file_found=self.spectra_file_found) - + + self.display_infos( + input_full_path=_run, + spectra_file_found=self.spectra_file_found, + ) + else: display(HTML(f"{_run} - EMPTY!")) else: - display(HTML(f"{_run} - NOT FOUND! - ERROR!")) + display( + HTML( + f"{_run} - NOT FOUND! - ERROR!" + ) + ) notebook_logging.info(f"\tOB run number {_run} - NOT FOUND!") self.ob_run_numbers_selected = None if len(set(self.check_nbr_tiff[DataType.ob])) > 1: - display(HTML(f"Warning: Different number of TIFF files found in selected OB runs: {self.check_nbr_tiff[DataType.ob]}")) - notebook_logging.info(f"WARNING: Different number of TIFF files found in selected OB runs: {self.check_nbr_tiff[DataType.ob]}") + display( + HTML( + f"Warning: Different number of TIFF files found in selected OB runs: {self.check_nbr_tiff[DataType.ob]}" + ) + ) + notebook_logging.info( + f"WARNING: Different number of TIFF files found in selected OB runs: {self.check_nbr_tiff[DataType.ob]}" + ) else: - if self.check_nbr_tiff[DataType.ob][0] != self.check_nbr_tiff[DataType.sample][0]: - display(HTML(f"Not valid OB runs found (different number of OB and sample TIFF files)!")) + if ( + self.check_nbr_tiff[DataType.ob][0] + != self.check_nbr_tiff[DataType.sample][0] + ): + display( + HTML( + "Not valid OB runs found (different number of OB and sample TIFF files)!" + ) + ) notebook_logging.info("WARNING: Not valid OB runs found!") def select_dc_run_numbers(self): - self.select_folder(instruction="Browse dc top folder", next_function=self.dc_folder_selected) + self.select_folder( + instruction="Browse dc top folder", next_function=self.dc_folder_selected + ) def select_dc_run_numbers(self): - if self.debug: dc_runs = DEBUG_DATA.dc_runs_selected if dc_runs: @@ -545,10 +684,14 @@ def select_dc_run_numbers(self): else: str_dc_run_numbers = "" - dc_label = widgets.HTML(value="List of dc run numbers (ex: 8705, 8707)") + dc_label = widgets.HTML( + value="List of dc run numbers (ex: 8705, 8707)" + ) self.dc_run_numbers_widget = widgets.Textarea( - value=str_dc_run_numbers, placeholder="", layout=widgets.Layout(width="400px") + value=str_dc_run_numbers, + placeholder="", + layout=widgets.Layout(width="400px"), ) vertical_layout = widgets.VBox( [ @@ -577,7 +720,9 @@ def check_dc(self): self.reset_dc_dicts() if self.dc_run_numbers_widget.value.strip() != "": - list_of_runs = extract_list_of_runs_from_string(self.dc_run_numbers_widget.value) + list_of_runs = extract_list_of_runs_from_string( + self.dc_run_numbers_widget.value + ) notebook_logging.info(f"\t{list_of_runs = }") list_of_dc_full_path = [] @@ -588,25 +733,47 @@ def check_dc(self): for _file_full_path in list_of_dc_full_path: if os.path.exists(_file_full_path): notebook_logging.info(f"\tDC run number {_file_full_path} - FOUND") - is_valid_run, report_dict = self.check_folder_is_valid(_file_full_path) + is_valid_run, report_dict = self.check_folder_is_valid( + _file_full_path + ) if is_valid_run: nbr_tiff = report_dict["nbr_tiff"] self.check_nbr_tiff[DataType.dc].append(nbr_tiff) - notebook_logging.info(f"\tDC run number {_file_full_path} - FOUND with {nbr_tiff} tif* files") - display(HTML(f"{_file_full_path} - OK")) + notebook_logging.info( + f"\tDC run number {_file_full_path} - FOUND with {nbr_tiff} tif* files" + ) + display( + HTML( + f"{_file_full_path} - OK" + ) + ) self.dict_dc[_file_full_path] = {} - self.dict_short_name_full_path["dc"][os.path.basename(_file_full_path)] = _file_full_path + self.dict_short_name_full_path["dc"][ + os.path.basename(_file_full_path) + ] = _file_full_path self.display_infos(input_full_path=_file_full_path) else: - display(HTML(f"{_file_full_path} - EMPTY!")) + display( + HTML( + f"{_file_full_path} - EMPTY!" + ) + ) else: - notebook_logging.info(f"\tDC run number {_file_full_path} - NOT FOUND") - display(HTML(f"{_file_full_path} - NOT FOUND!")) + notebook_logging.info( + f"\tDC run number {_file_full_path} - NOT FOUND" + ) + display( + HTML( + f"{_file_full_path} - NOT FOUND!" + ) + ) else: - notebook_logging.info(f"DC run numbers selected: {self.dc_run_numbers_selected}") + notebook_logging.info( + f"DC run numbers selected: {self.dc_run_numbers_selected}" + ) if self.dc_run_numbers_selected is None: - display(HTML(f"No DC runs selected!")) + display(HTML("No DC runs selected!")) return for _run in self.dc_run_numbers_selected: @@ -619,24 +786,45 @@ def check_dc(self): nbr_tiff = report_dict["nbr_tiff"] self.check_nbr_tiff[DataType.dc].append(nbr_tiff) display(HTML(f"{_run} - OK")) - notebook_logging.info(f"\tfolder seems to be a valid folder containing {nbr_tiff} tif* files") - self.dict_short_name_full_path["dc"][os.path.basename(_run)] = _run + notebook_logging.info( + f"\tfolder seems to be a valid folder containing {nbr_tiff} tif* files" + ) + self.dict_short_name_full_path["dc"][os.path.basename(_run)] = ( + _run + ) self.dict_dc[_run] = {} self.display_infos(input_full_path=_run) else: display(HTML(f"{_run} - EMPTY!")) else: - display(HTML(f"{_run} - NOT FOUND! - ERROR!")) + display( + HTML( + f"{_run} - NOT FOUND! - ERROR!" + ) + ) notebook_logging.info(f"\tDC run number {_run} - NOT FOUND!") self.dc_run_numbers_selected = None if len(set(self.check_nbr_tiff[DataType.dc])) > 1: - display(HTML(f"Warning: Different number of TIFF files found in selected DC runs: {self.check_nbr_tiff[DataType.dc]}")) - notebook_logging.info(f"WARNING: Different number of TIFF files found in selected DC runs: {self.check_nbr_tiff[DataType.dc]}") + display( + HTML( + f"Warning: Different number of TIFF files found in selected DC runs: {self.check_nbr_tiff[DataType.dc]}" + ) + ) + notebook_logging.info( + f"WARNING: Different number of TIFF files found in selected DC runs: {self.check_nbr_tiff[DataType.dc]}" + ) else: - if self.check_nbr_tiff[DataType.dc][0] != self.check_nbr_tiff[DataType.sample][0]: - display(HTML(f"No valid DC runs found (different number of DC and sample TIFF files)")) + if ( + self.check_nbr_tiff[DataType.dc][0] + != self.check_nbr_tiff[DataType.sample][0] + ): + display( + HTML( + "No valid DC runs found (different number of DC and sample TIFF files)" + ) + ) notebook_logging.info("WARNING: No valid DC runs found!") def _load_and_get_integrated_ob(self, full_path): @@ -650,11 +838,17 @@ def _load_and_get_integrated_ob(self, full_path): if self.dict_ob[full_path].get("data") is None: notebook_logging.info("No data found for this OB run, loading it now...") # load the data from the OB run - notebook_logging.info(f"\tFull path to OB run: {os.path.basename(full_path)}") + notebook_logging.info( + f"\tFull path to OB run: {os.path.basename(full_path)}" + ) list_tiff = retrieve_list_of_tif(full_path) notebook_logging.info(f"\tNumber of TIFF files found: {len(list_tiff)}") if len(list_tiff) == 0: - display(HTML(f"No TIFF files found in {full_path}!")) + display( + HTML( + f"No TIFF files found in {full_path}!" + ) + ) notebook_logging.error(f"No TIFF files found in {full_path}!") return None data = load_data_using_multithreading(list_tiff, combine_tof=True) @@ -691,7 +885,9 @@ def preview_ob_runs(self): im = ax.imshow(integrated_ob, cmap="viridis", aspect="auto") ax.set_aspect("equal") ax.set_title(f"Integrated OB run: {list_ob_short_runs[0]}") - fig.colorbar(im, ax=ax, orientation="vertical", label="Intensity", shrink=0.8) + fig.colorbar( + im, ax=ax, orientation="vertical", label="Intensity", shrink=0.8 + ) plt.show() else: @@ -730,30 +926,46 @@ def display_ob_run(short_name): def checking_spectra_files(self): if not self.spectra_file_found: - if len(self.list_spectra_file_found) > 0: - display(HTML("Some spectra files were NOT found but on the good side, some spectra files were found. The first one of those spectra file will be used for all!")) - notebook_logging.info("Some spectra files were NOT found but on the good side, some spectra files were found. The first one of those spectra file will be used for all!") + display( + HTML( + "Some spectra files were NOT found but on the good side, some spectra files were found. The first one of those spectra file will be used for all!" + ) + ) + notebook_logging.info( + "Some spectra files were NOT found but on the good side, some spectra files were found. The first one of those spectra file will be used for all!" + ) spectra_file_to_use = self.list_spectra_file_found[0] pd_spectra = pd.read_csv(spectra_file_to_use, sep=",", header=0) self.spectra_array = np.array(pd_spectra["shutter_time"].values) - notebook_logging.info(f"Using spectra file: {spectra_file_to_use} with {len(self.spectra_array)} TOF channels.") + notebook_logging.info( + f"Using spectra file: {spectra_file_to_use} with {len(self.spectra_array)} TOF channels." + ) else: - display(HTML("Error: No spectra files were found in the selected runs! We need to create the spectra file.")) - notebook_logging.error("Error: No spectra files were found in the selected runs! Manually creating the spectra file needed!") - + display( + HTML( + "Error: No spectra files were found in the selected runs! We need to create the spectra file." + ) + ) + notebook_logging.error( + "Error: No spectra files were found in the selected runs! Manually creating the spectra file needed!" + ) + self.manually_create_spectra_array() - + else: - display(HTML("All selected runs have the spectra file.")) + display( + HTML( + "All selected runs have the spectra file." + ) + ) notebook_logging.info("All selected runs have the spectra file.") def manually_create_spectra_array(self): - - label = widgets.Label(f"Enter the TOF bins size (in nS)") + label = widgets.Label("Enter the TOF bins size (in nS)") self.tof_bin_size_widget = widgets.IntText( value=700, min=100, @@ -774,37 +986,56 @@ def manually_create_spectra_array(self): def create_spectra_arrays_clicked(self, b): tof_bin_size = self.tof_bin_size_widget.value - notebook_logging.info(f"Creating spectra arrays with TOF bin size: {tof_bin_size} nS") + notebook_logging.info( + f"Creating spectra arrays with TOF bin size: {tof_bin_size} nS" + ) # get the number of TOF channels from the first sample run first_sample_run = list(self.dict_sample.keys())[0] list_tiff = retrieve_list_of_tif(first_sample_run) - if len(list_tiff) == 0: - display(HTML(f"No TIFF files found in {first_sample_run}!")) + if len(list_tiff) == 0: + display( + HTML( + f"No TIFF files found in {first_sample_run}!" + ) + ) notebook_logging.error(f"No TIFF files found in {first_sample_run}!") return nbr_files = len(list_tiff) tof_bin_size_in_s = tof_bin_size * 1e-9 # convert nS to seconds - - spectra_array = np.arange(0.0001e-7, nbr_files * tof_bin_size_in_s, tof_bin_size_in_s) # do not start at 0 to avoid log binning issues in iBeatles + + spectra_array = np.arange( + 0.0001e-7, nbr_files * tof_bin_size_in_s, tof_bin_size_in_s + ) # do not start at 0 to avoid log binning issues in iBeatles self.spectra_array = spectra_array - display(HTML(f"Created spectra arrays with TOF bin size: {tof_bin_size} nS!")) - notebook_logging.info(f"Created spectra arrays with TOF bin size: {tof_bin_size} nS and {len(spectra_array) = } ... Done!") + display( + HTML( + f"Created spectra arrays with TOF bin size: {tof_bin_size} nS!" + ) + ) + notebook_logging.info( + f"Created spectra arrays with TOF bin size: {tof_bin_size} nS and {len(spectra_array) = } ... Done!" + ) def select_output_folder(self): - if (self.spectra_file_found is False) and (self.spectra_array is None): - display(HTML("You need to create the spectra arrays before selecting the output folder!")) - notebook_logging.error("You need to create the spectra arrays before selecting the output folder!") + display( + HTML( + "You need to create the spectra arrays before selecting the output folder!" + ) + ) + notebook_logging.error( + "You need to create the spectra arrays before selecting the output folder!" + ) return if self.debug: self.output_folder_selected(DEBUG_DATA.output_folder) else: self.select_folder( - instruction="Select output folder", - start_dir=self.output_dir, + instruction="Select output folder", + start_dir=self.output_dir, next_function=self.output_folder_selected, newdir_toolbar_button=True, ) @@ -812,11 +1043,13 @@ def select_output_folder(self): def retrieve_nexus_file_path(self): """ Retrieve the NeXus file paths for sample, OB and DC. - + This function assumes that the NeXus files are named in a specific format""" all_nexus_files_found = True - notebook_logging.info("Retrieving NeXus file paths for sample, OB and DC runs...") + notebook_logging.info( + "Retrieving NeXus file paths for sample, OB and DC runs..." + ) notebook_logging.info("\tworking with sample runs:") for full_path in self.dict_sample.keys(): @@ -880,8 +1113,8 @@ def retrieve_nexus_file_path(self): return all_nexus_files_found def _on_remove_container_flag_change(self, change): - if change['new']: - # enable the widgets + if change["new"]: + # enable the widgets disable_widgets = False else: # disable the widgets @@ -889,10 +1122,13 @@ def _on_remove_container_flag_change(self, change): self.remove_container_options_flag.disabled = disable_widgets def settings(self): - # check here that the user selected a folder for output if self.output_folder is None: - display(HTML("You forgot to select an output folder!")) + display( + HTML( + "You forgot to select an output folder!" + ) + ) return all_nexus_found = self.retrieve_nexus_file_path() @@ -901,43 +1137,70 @@ def settings(self): tpx3_disabled_flag = True if self.detector_type == DetectorType.tpx3 else False # regular normalization - display(HTML("Normalization pixel by pixel")) - display(widgets.Checkbox(description="Normalization of images pixel by pixel", - value=True, - disabled=True, - layout=widgets.Layout(width="600px"))) + display( + HTML( + "Normalization pixel by pixel" + ) + ) + display( + widgets.Checkbox( + description="Normalization of images pixel by pixel", + value=True, + disabled=True, + layout=widgets.Layout(width="600px"), + ) + ) display(HTML("
")) # normalization of full spectrum of ROI - display(HTML("Normalization of full spectrum of ROI")) - display(HTML("If checked, normalization will be done as follows. After selecting a region of interest (ROI), for each image, the total counts of that region of the sample will be divided by the total" \ - " counts of the same region of the OB. This will produce a profile of this normalization value for each image.")) - self.full_spectrum_roi_flag = widgets.Checkbox(description="Work on full spectrum of ROI", value=False) + display( + HTML( + "Normalization of full spectrum of ROI" + ) + ) + display( + HTML( + "If checked, normalization will be done as follows. After selecting a region of interest (ROI), for each image, the total counts of that region of the sample will be divided by the total" + " counts of the same region of the OB. This will produce a profile of this normalization value for each image." + ) + ) + self.full_spectrum_roi_flag = widgets.Checkbox( + description="Work on full spectrum of ROI", value=False + ) display(self.full_spectrum_roi_flag) display(HTML("
")) # how to combine sample runs if more than 1 sample provided self.combine_sample_runs_flag = widgets.Checkbox( - description="Combine sample runs (all sample will produce one normalization output)", - value=False, + description="Combine sample runs (all sample will produce one normalization output)", + value=False, disabled=False, layout=widgets.Layout(width="600px"), ) if len(self.dict_sample) > 1: - display(HTML("How to treat the sample runs")) + display( + HTML( + "How to treat the sample runs" + ) + ) display(self.combine_sample_runs_flag) display(HTML("
")) # remove container option - display(HTML("Remove container")) - self.remove_container_flag = widgets.Checkbox(description="Do you want to remove container signal?", - value=False, - layout=widgets.Layout(width="600px")) - self.remove_container_flag.observe(self._on_remove_container_flag_change, names='value') + display( + HTML("Remove container") + ) + self.remove_container_flag = widgets.Checkbox( + description="Do you want to remove container signal?", + value=False, + layout=widgets.Layout(width="600px"), + ) + self.remove_container_flag.observe( + self._on_remove_container_flag_change, names="value" + ) display(self.remove_container_flag) - white_space = widgets.Label("\t\t", - layout=widgets.Layout(width="150px")) + white_space = widgets.Label("\t\t", layout=widgets.Layout(width="150px")) self.remove_container_options_flag = widgets.RadioButtons( options=[ "Select a ROI of the sample containing only the container signal", @@ -947,39 +1210,44 @@ def settings(self): disabled=True, layout=widgets.Layout(width="500px"), ) - - hori_layout = widgets.HBox([white_space, - self.remove_container_options_flag], - layout=widgets.Layout(align_items="center", - width="100%")) + + hori_layout = widgets.HBox( + [white_space, self.remove_container_options_flag], + layout=widgets.Layout(align_items="center", width="100%"), + ) display(hori_layout) display(HTML("
")) # normalization options - display(HTML("What to take into account for the normalization")) + display( + HTML( + "What to take into account for the normalization" + ) + ) if all_nexus_found: _value = True - _disabled=False + _disabled = False else: _value = False _disabled = True - self.proton_charge_flag = widgets.Checkbox(description="Proton charge", - value=_value, - disabled=_disabled) - - self.monitor_counts_flag = widgets.Checkbox(description="Monitor counts", - value=False, - disabled=_disabled) - - + self.proton_charge_flag = widgets.Checkbox( + description="Proton charge", value=_value, disabled=_disabled + ) + + self.monitor_counts_flag = widgets.Checkbox( + description="Monitor counts", value=False, disabled=_disabled + ) + ## FIXME shutter_counts_value = False # shutter_counts_value = not tpx3_disabled_flag - + self.shutter_counts_flag = widgets.Checkbox( - description="Shutter counts", value=shutter_counts_value, disabled=tpx3_disabled_flag + description="Shutter counts", + value=shutter_counts_value, + disabled=tpx3_disabled_flag, ) self.correct_chips_alignment_flag = widgets.Checkbox( description="Correct chips alignment", disabled=True, value=False @@ -997,43 +1265,76 @@ def settings(self): display(HTML("
")) - display(HTML("How to handle OB zeros - May take much more time!")) - - display(widgets.Checkbox(description="Ignore zeros in OB during normalization", - value=True, - disabled=True, - layout=widgets.Layout(width="600px"))) - - self.replace_ob_zeros_by_local_median_flag = widgets.Checkbox(description="Replace zeros by local median", - value=False, - layout=widgets.Layout(width="500px")) - self.replace_ob_zeros_by_local_median_flag.observe(self._on_replace_ob_zeros_by_local_median_flag_change, - names='value') + display( + HTML( + "How to handle OB zeros - May take much more time!" + ) + ) + + display( + widgets.Checkbox( + description="Ignore zeros in OB during normalization", + value=True, + disabled=True, + layout=widgets.Layout(width="600px"), + ) + ) + + self.replace_ob_zeros_by_local_median_flag = widgets.Checkbox( + description="Replace zeros by local median", + value=False, + layout=widgets.Layout(width="500px"), + ) + self.replace_ob_zeros_by_local_median_flag.observe( + self._on_replace_ob_zeros_by_local_median_flag_change, names="value" + ) # self.correct_chips_alignment_flag = widgets.Checkbox( # description="Correct chips alignment", disabled=False, value=True # ) display(self.replace_ob_zeros_by_local_median_flag) - kernel_size_label = widgets.Label(value="Kernel size for local median (odd number):", - layout=widgets.Layout(width="300")) - self.kernel_size_for_local_median_y = widgets.BoundedIntText(description="y axis:", - value=3, min=1, max=99, step=2, layout=widgets.Layout(width="150px") + kernel_size_label = widgets.Label( + value="Kernel size for local median (odd number):", + layout=widgets.Layout(width="300"), ) - self.kernel_size_for_local_median_x = widgets.BoundedIntText(description="x axis:", - value=3, min=1, max=99, step=2, layout=widgets.Layout(width="150px") + self.kernel_size_for_local_median_y = widgets.BoundedIntText( + description="y axis:", + value=3, + min=1, + max=99, + step=2, + layout=widgets.Layout(width="150px"), + ) + self.kernel_size_for_local_median_x = widgets.BoundedIntText( + description="x axis:", + value=3, + min=1, + max=99, + step=2, + layout=widgets.Layout(width="150px"), ) - self.kernel_size_for_local_median_tof = widgets.BoundedIntText(description="tof axis:", - value=1, min=1, max=99, step=2, layout=widgets.Layout(width="150px") + self.kernel_size_for_local_median_tof = widgets.BoundedIntText( + description="tof axis:", + value=1, + min=1, + max=99, + step=2, + layout=widgets.Layout(width="150px"), + ) + hori_layout = widgets.HBox( + [ + kernel_size_label, + self.kernel_size_for_local_median_y, + self.kernel_size_for_local_median_x, + self.kernel_size_for_local_median_tof, + ], + hori_layout=widgets.Layout(align_items="center", width="100%"), ) - hori_layout = widgets.HBox([kernel_size_label, - self.kernel_size_for_local_median_y, - self.kernel_size_for_local_median_x, - self.kernel_size_for_local_median_tof], - hori_layout=widgets.Layout(align_items="center", - width="100%")) display(hori_layout) - _label = widgets.Label(value="Maximum number of iterations:", layout=widgets.Layout(width="300px")) + _label = widgets.Label( + value="Maximum number of iterations:", layout=widgets.Layout(width="300px") + ) self.maximum_iterations_ui = widgets.BoundedIntText( value=2, min=1, @@ -1041,23 +1342,32 @@ def settings(self): step=1, layout=widgets.Layout(width="50px"), ) - hori_layout = widgets.HBox([_label, self.maximum_iterations_ui], - hori_layout=widgets.Layout(align_items="center", - width="100%")) + hori_layout = widgets.HBox( + [_label, self.maximum_iterations_ui], + hori_layout=widgets.Layout(align_items="center", width="100%"), + ) display(hori_layout) display(HTML("
")) - label = widgets.Label(value="Distance source detector (m)", layout=widgets.Layout(width="200px")) + label = widgets.Label( + value="Distance source detector (m)", layout=widgets.Layout(width="200px") + ) self.distance_source_detector = widgets.FloatText( - value=distance_source_detector_m[self.instrument], disabled=False, layout=widgets.Layout(width="50px") + value=distance_source_detector_m[self.instrument], + disabled=False, + layout=widgets.Layout(width="50px"), ) hori_layout = widgets.HBox([label, self.distance_source_detector]) display(hori_layout) if self.instrument == "SNAP": - label = widgets.Label(value="Detector offset (us)", layout=widgets.Layout(width="200px")) - self.detector_offset_us = widgets.FloatText(value=0.0, disabled=False, layout=widgets.Layout(width="50px")) + label = widgets.Label( + value="Detector offset (us)", layout=widgets.Layout(width="200px") + ) + self.detector_offset_us = widgets.FloatText( + value=0.0, disabled=False, layout=widgets.Layout(width="50px") + ) hori_layout = widgets.HBox([label, self.detector_offset_us]) display(hori_layout) @@ -1075,8 +1385,8 @@ def select_container_from_file(self): self.roi_container_file = MyFileSelectorPanel( instruction="Select ROI container file", start_dir=self.output_dir, - filters={"ROI container files": ["*_container_roi.tiff"]}, # scitiff file - type='file', + filters={"ROI container files": ["*_container_roi.tiff"]}, # scitiff file + type="file", multiple=False, next=self.load_container_roi_from_file, ) @@ -1085,24 +1395,33 @@ def select_container_from_file(self): def load_container_roi_from_file(self, file_path): self.container_roi_file = file_path notebook_logging.info(f"Loading container ROI from file: {file_path} ...") - display(HTML(f"Will use the container ROI file: {file_path}!")) - + display( + HTML( + f"Will use the container ROI file: {file_path}!" + ) + ) + master_dict = load_json(file_path) self.container_integrated_image = master_dict["integrated_image"] - self.container_roi = Roi(left=master_dict["container_roi"]["left"], - top=master_dict["container_roi"]["top"], - width=master_dict["container_roi"]["width"], - height=master_dict["container_roi"]["height"]) - - def select_container(self): + self.container_roi = Roi( + left=master_dict["container_roi"]["left"], + top=master_dict["container_roi"]["top"], + width=master_dict["container_roi"]["width"], + height=master_dict["container_roi"]["height"], + ) - # load first sample and display integrated image to select ROI + def select_container(self): + # load first sample and display integrated image to select ROI if not self.dict_sample: display(HTML("No sample runs selected!")) return - display(HTML("Select ROI of ONLY the container!")) - logging.info(f"Selecting ROI of ONLY the container ...") + display( + HTML( + "Select ROI of ONLY the container!" + ) + ) + logging.info("Selecting ROI of ONLY the container ...") if self.integrated_data is None: self.integrated_data = self.get_integrated_data(self.dict_sample) @@ -1117,61 +1436,89 @@ def select_container(self): vmin = 0 vmax = int(np.max(integrated_data)) self.vrange_container = [vmin, vmax] - + def container_roi_selection(vrange, left_right, top_bottom): - fig, ax = plt.subplots(figsize=(10, 10)) - im = ax.imshow(integrated_data, cmap="viridis", aspect="auto", vmin=vrange[0], vmax=vrange[1]) - cbar = plt.colorbar(im, ax=ax, orientation="vertical", label="Intensity", shrink=0.5) + im = ax.imshow( + integrated_data, + cmap="viridis", + aspect="auto", + vmin=vrange[0], + vmax=vrange[1], + ) + cbar = plt.colorbar( + im, ax=ax, orientation="vertical", label="Intensity", shrink=0.5 + ) logging.info("Updating rectangle ...") if self.rect_container: self.rect_container.remove() - - self.rect_container = patches.Rectangle((left_right[0], top_bottom[0]), left_right[1]-left_right[0], top_bottom[1]-top_bottom[0], linewidth=1, edgecolor='r', facecolor='none') + + self.rect_container = patches.Rectangle( + (left_right[0], top_bottom[0]), + left_right[1] - left_right[0], + top_bottom[1] - top_bottom[0], + linewidth=1, + edgecolor="r", + facecolor="none", + ) ax.add_patch(self.rect_container) - ax.set_title(f"Select ROI containing only the container") - - self.container_roi = Roi(left=left_right[0], top=top_bottom[0], width=left_right[1]-left_right[0], height=top_bottom[1]-top_bottom[0]) + ax.set_title("Select ROI containing only the container") + + self.container_roi = Roi( + left=left_right[0], + top=top_bottom[0], + width=left_right[1] - left_right[0], + height=top_bottom[1] - top_bottom[0], + ) widgets_width = "800px" self.interactive_plot = interactive( container_roi_selection, - vrange = widgets.IntRangeSlider(min=0, - max=int(np.max(integrated_data)), - step=1, - value=[0, int(np.max(integrated_data))], - description="vrange", - layout=widgets.Layout(width=widgets_width)), - left_right = widgets.IntRangeSlider(min=0, - max=integrated_data.shape[1]-1, - step=1, - value=[default_left, default_left+default_width], - description="left_right", - layout=widgets.Layout(width=widgets_width)), - top_bottom = widgets.IntRangeSlider(min=0, - max=integrated_data.shape[0]-1, - step=1, - value=[default_top, default_top+default_height], - description="top_bottom", - layout=widgets.Layout(width=widgets_width)), + vrange=widgets.IntRangeSlider( + min=0, + max=int(np.max(integrated_data)), + step=1, + value=[0, int(np.max(integrated_data))], + description="vrange", + layout=widgets.Layout(width=widgets_width), + ), + left_right=widgets.IntRangeSlider( + min=0, + max=integrated_data.shape[1] - 1, + step=1, + value=[default_left, default_left + default_width], + description="left_right", + layout=widgets.Layout(width=widgets_width), + ), + top_bottom=widgets.IntRangeSlider( + min=0, + max=integrated_data.shape[0] - 1, + step=1, + value=[default_top, default_top + default_height], + description="top_bottom", + layout=widgets.Layout(width=widgets_width), + ), ) display(self.interactive_plot) def select_roi(self): - - logging.info(f"Selecting ROI for full spectrum normalization...") + logging.info("Selecting ROI for full spectrum normalization...") # load first sample and display integrated image to select ROI if not self.dict_sample: display(HTML("No sample runs selected!")) return - display(HTML("Select ROI for full spectrum normalization!")) + display( + HTML( + "Select ROI for full spectrum normalization!" + ) + ) if self.integrated_data is None: self.integrated_data = self.get_integrated_data(self.dict_sample) - + integrated_data = self.integrated_data default_left = self.default_roi.left @@ -1179,112 +1526,143 @@ def select_roi(self): default_width = self.default_roi.width default_height = self.default_roi.height - self.roi = Roi(left=default_left, top=default_top, - width=default_width, height=default_height) + self.roi = Roi( + left=default_left, + top=default_top, + width=default_width, + height=default_height, + ) - def roi_selection(vrange, left_right, top_bottom): - left, right = left_right width = right - left - + top, bottom = top_bottom height = bottom - top - + vmin, vmax = vrange - + fig, ax = plt.subplots(figsize=(10, 10)) - ax.imshow(integrated_data, cmap="viridis", aspect="auto", vmin=vmin, vmax=vmax) - rect = patches.Rectangle((left, top), width, height, linewidth=1, edgecolor='r', facecolor='none') + ax.imshow( + integrated_data, cmap="viridis", aspect="auto", vmin=vmin, vmax=vmax + ) + rect = patches.Rectangle( + (left, top), width, height, linewidth=1, edgecolor="r", facecolor="none" + ) ax.add_patch(rect) - ax.set_title(f"Select ROI for full spectrum normalization") + ax.set_title("Select ROI for full spectrum normalization") plt.show() - logging.info(f"Selected ROI - left: {left}, top: {top}, width: {width}, height: {height}") + logging.info( + f"Selected ROI - left: {left}, top: {top}, width: {width}, height: {height}" + ) self.roi = Roi(left=left, top=top, width=width, height=height) widgets_width = "800px" interactive_plot = interactive( roi_selection, - vrange = widgets.IntRangeSlider(min=0, - max=int(np.max(integrated_data)), - step=1, - value=[0, int(np.max(integrated_data))], - description="vrange", - layout=widgets.Layout(width=widgets_width)), - left_right=widgets.IntRangeSlider(min=0, - max=integrated_data.shape[1]-1, - step=1, - value=[default_left, default_left+default_width], - description="left_right", - layout=widgets.Layout(width=widgets_width)), - top_bottom=widgets.IntRangeSlider(min=0, - max=integrated_data.shape[0]-1, - step=1, - value=[default_top, default_top+default_height], - description="top_bottom", - layout=widgets.Layout(width=widgets_width)), - ) - + vrange=widgets.IntRangeSlider( + min=0, + max=int(np.max(integrated_data)), + step=1, + value=[0, int(np.max(integrated_data))], + description="vrange", + layout=widgets.Layout(width=widgets_width), + ), + left_right=widgets.IntRangeSlider( + min=0, + max=integrated_data.shape[1] - 1, + step=1, + value=[default_left, default_left + default_width], + description="left_right", + layout=widgets.Layout(width=widgets_width), + ), + top_bottom=widgets.IntRangeSlider( + min=0, + max=integrated_data.shape[0] - 1, + step=1, + value=[default_top, default_top + default_height], + description="top_bottom", + layout=widgets.Layout(width=widgets_width), + ), + ) + display(interactive_plot) def post_settings(self): - at_least_one_option = False if self.full_spectrum_roi_flag.value: self.select_roi() at_least_one_option = True if self.remove_container_flag.value: - if self.full_spectrum_roi_flag.value: - display(HTML("
")) # to improve readability + display(HTML("
")) # to improve readability - if self.remove_container_options_flag.value == "Use previously saved ROI containing only the container signal": + if ( + self.remove_container_options_flag.value + == "Use previously saved ROI containing only the container signal" + ): self.select_container_from_file() self.container_roi_from_file = True - + else: self.select_container() self.container_roi_from_file = False self.we_need_to_automatically_save_the_container_roi = True - + at_least_one_option = True if not at_least_one_option: self.roi = None self.container_roi = None - display(HTML("Info: You are good to go, nothing to do here!")) + display( + HTML( + "Info: You are good to go, nothing to do here!" + ) + ) def preview_roi_selection_container_imported(self): - # preview of the roi selected + # preview of the roi selected if self.container_roi is not None: - if self.container_roi_from_file: - - display(HTML("Preview of the loaded ROI from file ...")) + display( + HTML( + "Preview of the loaded ROI from file ..." + ) + ) integrated_data = self.container_integrated_image fig, ax = plt.subplots(figsize=(5, 5)) im = ax.imshow(integrated_data, cmap="viridis", aspect="auto") - cbar = plt.colorbar(im, ax=ax, orientation="vertical", label="Intensity", shrink=0.5) + cbar = plt.colorbar( + im, ax=ax, orientation="vertical", label="Intensity", shrink=0.5 + ) - rect = patches.Rectangle((self.container_roi.left, self.container_roi.top), - self.container_roi.width, - self.container_roi.height, - linewidth=1, edgecolor='r', facecolor='none') + rect = patches.Rectangle( + (self.container_roi.left, self.container_roi.top), + self.container_roi.width, + self.container_roi.height, + linewidth=1, + edgecolor="r", + facecolor="none", + ) ax.add_patch(rect) - ax.set_title(f"Loaded ROI containing only the container from file") + ax.set_title("Loaded ROI containing only the container from file") plt.show() - + else: - display(HTML("ROI container selected within that notebook (no need to preview again)!")) - + display( + HTML( + "ROI container selected within that notebook (no need to preview again)!" + ) + ) + else: display(HTML("No container ROI selected!")) def _on_replace_ob_zeros_by_local_median_flag_change(self, change): - if change['new']: + if change["new"]: self.kernel_size_for_local_median_y.disabled = False self.kernel_size_for_local_median_x.disabled = False self.kernel_size_for_local_median_tof.disabled = False @@ -1296,19 +1674,20 @@ def _on_replace_ob_zeros_by_local_median_flag_change(self, change): self.maximum_iterations_ui.disabled = True def what_to_export(self): - if self.combine_sample_runs_flag.value: combined_flag = True else: combined_flag = False - + display(HTML("Stack of images")) self.export_corrected_stack_of_sample_data = widgets.Checkbox( - description="Export corrected stack of sample data", layout=widgets.Layout(width="100%"), value=False + description="Export corrected stack of sample data", + layout=widgets.Layout(width="100%"), + value=False, ) self.export_corrected_stack_of_ob_data = widgets.Checkbox( - description="Export corrected stack of ob data", - layout=widgets.Layout(width="100%"), + description="Export corrected stack of ob data", + layout=widgets.Layout(width="100%"), value=False, disabled=True, ) @@ -1327,37 +1706,39 @@ def what_to_export(self): # disabled=True, # ) - list_widget_to_display = [ - self.export_corrected_stack_of_sample_data, - # self.export_corrected_stack_of_ob_data, - self.export_corrected_stack_of_normalized_data, + list_widget_to_display = [ + self.export_corrected_stack_of_sample_data, + # self.export_corrected_stack_of_ob_data, + self.export_corrected_stack_of_normalized_data, ] # if self.combine_sample_runs_flag.value: # list_widget_to_display.append(self.export_corrected_stack_of_combined_normalized_data) - label = widgets.Label(value="Note: Any of the stacks exported will also contain the original spectra file") + label = widgets.Label( + value="Note: Any of the stacks exported will also contain the original spectra file" + ) list_widget_to_display.append(label) - vertical_layout = widgets.VBox( - list_widget_to_display - ) + vertical_layout = widgets.VBox(list_widget_to_display) display(vertical_layout) - display(HTML("Integrated images")) + display( + HTML("Integrated images") + ) self.export_corrected_integrated_sample_data = widgets.Checkbox( - description="Export corrected integrated sample data", - layout=widgets.Layout(width="100%"), - value=False + description="Export corrected integrated sample data", + layout=widgets.Layout(width="100%"), + value=False, ) self.export_corrected_integrated_ob_data = widgets.Checkbox( - description="Export corrected integrated ob data", - layout=widgets.Layout(width="100%"), + description="Export corrected integrated ob data", + layout=widgets.Layout(width="100%"), value=False, disabled=True, ) self.export_corrected_integrated_normalized_data = widgets.Checkbox( - description="Export integrated normalized data (integrated sample divide by integrated ob)", - layout=widgets.Layout(width="100%"), - value=False + description="Export integrated normalized data (integrated sample divide by integrated ob)", + layout=widgets.Layout(width="100%"), + value=False, ) self.export_corrected_integrated_combined_normalized_data = widgets.Checkbox( @@ -1367,13 +1748,15 @@ def what_to_export(self): disabled=False, ) - list_widget_to_display = [ - self.export_corrected_integrated_sample_data, - # self.export_corrected_integrated_ob_data, - self.export_corrected_integrated_normalized_data, + list_widget_to_display = [ + self.export_corrected_integrated_sample_data, + # self.export_corrected_integrated_ob_data, + self.export_corrected_integrated_normalized_data, ] if self.combine_sample_runs_flag.value: - list_widget_to_display.append(self.export_corrected_integrated_combined_normalized_data) + list_widget_to_display.append( + self.export_corrected_integrated_combined_normalized_data + ) vertical_layout = widgets.VBox( list_widget_to_display, @@ -1381,23 +1764,27 @@ def what_to_export(self): display(vertical_layout) - if not (self.spectra_array is None) or (self.we_need_to_automatically_save_the_container_roi): + if self.spectra_array is not None or ( + self.we_need_to_automatically_save_the_container_roi + ): display(HTML("Others")) - + if self.spectra_array is None: self.export_spectra_file = widgets.Checkbox( - description="Export spectra file used for normalization", - layout=widgets.Layout(width="100%"), - value=True) + description="Export spectra file used for normalization", + layout=widgets.Layout(width="100%"), + value=True, + ) display(self.export_spectra_file) - + if self.we_need_to_automatically_save_the_container_roi: self.export_container_roi = widgets.Checkbox( - description="Export container ROI file used for normalization", - layout=widgets.Layout(width="100%"), - value=True) + description="Export container ROI file used for normalization", + layout=widgets.Layout(width="100%"), + value=True, + ) display(self.export_container_roi) - + def check_folder_is_valid(self, full_path): list_tiff = glob.glob(os.path.join(full_path, "*.tif*")) if list_tiff: @@ -1407,37 +1794,60 @@ def check_folder_is_valid(self, full_path): def sample_folder_selected(self, folder_selected): self.sample_folder = folder_selected - display(HTML(f"Sample folder selected: {folder_selected}")) + display( + HTML( + f"Sample folder selected: {folder_selected}" + ) + ) def ob_folder_selected(self, folder_selected): self.ob_folder = folder_selected - display(HTML(f"Open beam folder selected: {folder_selected}")) + display( + HTML( + f"Open beam folder selected: {folder_selected}" + ) + ) def dc_folder_selected(self, folder_selected): self.dc_folder = folder_selected - display(HTML(f"Dark current folder selected: {folder_selected}")) + display( + HTML( + f"Dark current folder selected: {folder_selected}" + ) + ) def save_ob_run_numbers_selected(self, folder_selected): self.ob_run_numbers_selected = folder_selected - + def save_dc_run_numbers_selected(self, folder_selected): self.dc_run_numbers_selected = folder_selected - + def output_folder_selected(self, folder_selected): self.output_folder = folder_selected display(HTML("Output folder selected:")) if os.path.exists(folder_selected): - display(HTML(f"{folder_selected} - FOUND!")) + display( + HTML(f"{folder_selected} - FOUND!") + ) notebook_logging.info(f"Output folder selected: {folder_selected} - FOUND") else: - display(HTML(f"{folder_selected} - DOES NOT EXIST and will be CREATED!")) - notebook_logging.info(f"Output folder selected: {folder_selected} - NOT FOUND and will be CREATED!") - - def select_folder(self, instruction="Select a folder", - next_function=None, - start_dir=None, - multiple=False, - newdir_toolbar_button=False): + display( + HTML( + f"{folder_selected} - DOES NOT EXIST and will be CREATED!" + ) + ) + notebook_logging.info( + f"Output folder selected: {folder_selected} - NOT FOUND and will be CREATED!" + ) + + def select_folder( + self, + instruction="Select a folder", + next_function=None, + start_dir=None, + multiple=False, + newdir_toolbar_button=False, + ): # go straight to autoreduce/mcp folder if start_dir is None: start_dir = self.autoreduce_dir @@ -1525,9 +1935,11 @@ def run_normalization_with_list_of_runs(self, preview=False): # shutter_counts_flag=self.shutter_counts_flag.value, # replace_ob_zeros_by_nan_flag=self.replace_ob_zeros_by_nan_flag.value, replace_ob_zeros_by_local_median_flag=self.replace_ob_zeros_by_local_median_flag.value, - kernel_size_for_local_median=(self.kernel_size_for_local_median_y.value, - self.kernel_size_for_local_median_x.value, - self.kernel_size_for_local_median_tof.value), + kernel_size_for_local_median=( + self.kernel_size_for_local_median_y.value, + self.kernel_size_for_local_median_x.value, + self.kernel_size_for_local_median_tof.value, + ), max_iterations=self.maximum_iterations_ui.value, correct_chips_alignment_flag=self.correct_chips_alignment_flag.value, correct_chips_alignment_config=correct_chips_alignment_config, @@ -1540,12 +1952,12 @@ def run_normalization_with_list_of_runs(self, preview=False): combine_samples=self.combine_sample_runs_flag.value, roi=self.roi, container_roi=self.container_roi, - container_roi_file=self.container_roi_file + container_roi_file=self.container_roi_file, ) - + display(HTML("Normalization completed")) # display(HTML("Log file: /SNS/VENUS/shared/logs/normalization_for_timepix.log")) - + def profile_of_roi(self): normalized_data = self.normalized_dict.data @@ -1554,13 +1966,14 @@ def profile_of_roi(self): tof_array = self.normalized_dict.tof_array def plot_normalized_profile_of_roi(index, left=0, top=0, width=50, height=50): - _normalized_data = normalized_data[index] _integrated = np.nanmean(_normalized_data, axis=0) - _profile = np.nanmean(_normalized_data[:, top : top + height, left : left + width], axis=0) - fig, axs = plt.subplots(ncols=2, nrows=2,figsize=(10, 6)) - im = axs[0,0].imshow(_integrated, cmap="viridis") - axs[0,0].add_patch( + _profile = np.nanmean( + _normalized_data[:, top : top + height, left : left + width], axis=0 + ) + fig, axs = plt.subplots(ncols=2, nrows=2, figsize=(10, 6)) + im = axs[0, 0].imshow(_integrated, cmap="viridis") + axs[0, 0].add_patch( patches.Rectangle( (left, top), width, @@ -1571,21 +1984,56 @@ def plot_normalized_profile_of_roi(index, left=0, top=0, width=50, height=50): ) ) - axs[0,0].set_title(f"Integrated normalized data - {index}") - fig.colorbar(im, ax=axs[0,0], orientation="vertical", label="Intensity") - axs[1,0].plot(lambda_array, _profile) - axs[1,0].set_title(f"Profile of ROI - {index}") - axs[1,0].set_xlabel("lambda_array") - axs[1,0].set_ylabel("Intensity (a.u.)") - - _plot_normalized = interactive(widgets.Dropdown(options=list(normalized_data.keys()), - description="Sample run:", - layout=widgets.Layout(width="300px")), - left=widgets.BoundedIntText(value=0, min=0, max=512, step=1, description="left:", layout=widgets.Layout(width="200px")), - top=widgets.BoundedIntText(value=0, min=0, max=512, step=1, description="top:", layout=widgets.Layout(width="200px")), - width=widgets.BoundedIntText(value=50, min=1, max=512, step=1, description="width:", layout=widgets.Layout(width="200px")), - height=widgets.BoundedIntText(value=50, min=1, max=512, step=1, description="height:", layout=widgets.Layout(width="200px")), - function=widgets.Dropdown(options=["mean", "median"], description="Function:", layout=widgets.Layout(width="200px")), + axs[0, 0].set_title(f"Integrated normalized data - {index}") + fig.colorbar(im, ax=axs[0, 0], orientation="vertical", label="Intensity") + axs[1, 0].plot(lambda_array, _profile) + axs[1, 0].set_title(f"Profile of ROI - {index}") + axs[1, 0].set_xlabel("lambda_array") + axs[1, 0].set_ylabel("Intensity (a.u.)") + + _plot_normalized = interactive( + widgets.Dropdown( + options=list(normalized_data.keys()), + description="Sample run:", + layout=widgets.Layout(width="300px"), + ), + left=widgets.BoundedIntText( + value=0, + min=0, + max=512, + step=1, + description="left:", + layout=widgets.Layout(width="200px"), + ), + top=widgets.BoundedIntText( + value=0, + min=0, + max=512, + step=1, + description="top:", + layout=widgets.Layout(width="200px"), + ), + width=widgets.BoundedIntText( + value=50, + min=1, + max=512, + step=1, + description="width:", + layout=widgets.Layout(width="200px"), + ), + height=widgets.BoundedIntText( + value=50, + min=1, + max=512, + step=1, + description="height:", + layout=widgets.Layout(width="200px"), + ), + function=widgets.Dropdown( + options=["mean", "median"], + description="Function:", + layout=widgets.Layout(width="200px"), + ), ) display(_plot_normalized) @@ -1593,9 +2041,13 @@ def plot_normalized_profile_of_roi(index, left=0, top=0, width=50, height=50): def legend(cls) -> None: display(HTML("
")) display(HTML("

Legend

")) - display(HTML("
    " - "
  • Mandatory steps must be performed to ensure proper data preparation and reconstruction.
  • " - "
  • Optional but recommended steps are not mandatory but should be performed to ensure proper data preparation and reconstruction.
  • " - "
  • Optional steps are not mandatory but highly recommended to improve the quality of your reconstruction.
  • " - "
")) + display( + HTML( + "
    " + "
  • Mandatory steps must be performed to ensure proper data preparation and reconstruction.
  • " + "
  • Optional but recommended steps are not mandatory but should be performed to ensure proper data preparation and reconstruction.
  • " + "
  • Optional steps are not mandatory but highly recommended to improve the quality of your reconstruction.
  • " + "
" + ) + ) display(HTML("
")) diff --git a/notebooks/__code/normalization_tof/units.py b/notebooks/__code/normalization_tof/units.py index 8acb982f1..43c1bc965 100644 --- a/notebooks/__code/normalization_tof/units.py +++ b/notebooks/__code/normalization_tof/units.py @@ -135,7 +135,9 @@ def convert_to_cross_section(from_unit, to_unit): return conversion_factors[from_unit] / conversion_factors[to_unit] -def convert_from_wavelength_to_energy_ev(wavelength, unit_from=DistanceUnitOptions.angstrom): +def convert_from_wavelength_to_energy_ev( + wavelength, unit_from=DistanceUnitOptions.angstrom +): """Convert wavelength to energy based on the given units. Args: @@ -177,15 +179,21 @@ def convert_array_from_time_to_lambda( np.ndarray: Array of wavelength values. """ time_array_s = time_array * convert_time_units(time_unit, TimeUnitOptions.s) - detector_offset_s = detector_offset * convert_time_units(detector_offset_unit, TimeUnitOptions.s) + detector_offset_s = detector_offset * convert_time_units( + detector_offset_unit, TimeUnitOptions.s + ) distance_source_detector_m = distance_source_detector * convert_distance_units( distance_source_detector_unit, DistanceUnitOptions.m ) h_over_mn = h / m_n - lambda_m = h_over_mn * (time_array_s + detector_offset_s) / distance_source_detector_m + lambda_m = ( + h_over_mn * (time_array_s + detector_offset_s) / distance_source_detector_m + ) - lambda_converted = lambda_m * convert_distance_units(DistanceUnitOptions.m, lambda_unit) + lambda_converted = lambda_m * convert_distance_units( + DistanceUnitOptions.m, lambda_unit + ) return lambda_converted @@ -225,13 +233,22 @@ def convert_array_from_time_to_energy( detector_units_factor = convert_time_units(detector_offset_unit, TimeUnitOptions.s) detector_offset = detector_units_factor * detector_offset - distance_source_detector_factor = convert_distance_units(distance_source_detector_unit, DistanceUnitOptions.m) - distance_source_detector_m = distance_source_detector * distance_source_detector_factor + distance_source_detector_factor = convert_distance_units( + distance_source_detector_unit, DistanceUnitOptions.m + ) + distance_source_detector_m = ( + distance_source_detector * distance_source_detector_factor + ) # Calculate the energy in eV using the formula E_ev = 1/2 m_n (L/t_tof)^2 / electron_volt full_time_array_s = time_array_s + detector_offset - energy_array_ev = 0.5 * m_n * (distance_source_detector_m / full_time_array_s) ** 2 / electron_volt + energy_array_ev = ( + 0.5 + * m_n + * (distance_source_detector_m / full_time_array_s) ** 2 + / electron_volt + ) energy_array_factor = convert_to_energy(EnergyUnitOptions.eV, energy_unit) energy_array = energy_array_ev * energy_array_factor diff --git a/notebooks/__code/normalization_tof/utilities.py b/notebooks/__code/normalization_tof/utilities.py index 43d38b705..115f4f219 100644 --- a/notebooks/__code/normalization_tof/utilities.py +++ b/notebooks/__code/normalization_tof/utilities.py @@ -1,4 +1,3 @@ -import argparse import glob import logging import multiprocessing as mp @@ -8,34 +7,26 @@ from typing import Tuple import h5py -from matplotlib import container import matplotlib.pyplot as plt import numpy as np import pandas as pd from IPython.display import HTML, display from PIL import Image from skimage.io import imread -from scipy.ndimage import median_filter from __code.normalization_tof import Roi from __code._utilities.json import load_json, save_json MARKERSIZE = 2 + class NormalizedData: - data= {} - lambda_array= None - tof_array= None - energy_array= None - - -from __code.normalization_tof.units import ( - DistanceUnitOptions, - EnergyUnitOptions, - TimeUnitOptions, - convert_array_from_time_to_energy, - convert_array_from_time_to_lambda, -) + data = {} + lambda_array = None + tof_array = None + energy_array = None + + # from __code.normalization_tof.normalization_for_timepix import create_master_dict LOAD_DTYPE = np.uint16 @@ -50,6 +41,7 @@ class PLOT_SIZE: SPECTRA_FILE_PREFIX = "Spectra.txt" + class DataType: sample = "sample" ob = "ob" @@ -82,12 +74,14 @@ class StatusMetadata: def _worker(fl): -# return (imread(fl).astype(LOAD_DTYPE)).swapaxes(0, 1) + # return (imread(fl).astype(LOAD_DTYPE)).swapaxes(0, 1) return (imread(fl).astype(np.float32)).swapaxes(0, 1) - #return (imread(fl).astype(np.float32)) + # return (imread(fl).astype(np.float32)) -def load_data_using_multithreading(list_tif: list = None, combine_tof: bool = False) -> np.ndarray: +def load_data_using_multithreading( + list_tif: list = None, combine_tof: bool = False +) -> np.ndarray: """load data using multithreading""" with mp.Pool(processes=40) as pool: data = pool.map(_worker, list_tif) @@ -106,7 +100,9 @@ def retrieve_list_of_tif(folder: str) -> list: def create_x_axis_file( - lambda_array: np.ndarray = None, energy_array: np.ndarray = None, output_folder: str = "./" + lambda_array: np.ndarray = None, + energy_array: np.ndarray = None, + output_folder: str = "./", ) -> str: """create x axis file with lambda, energy and tof arrays""" x_axis_data = { @@ -122,7 +118,6 @@ def create_x_axis_file( def load_images(master_dict=None, data_type=DataType.sample, verbose=False): - logging.info(f"Loading {data_type} data ...") for _run_number in master_dict.keys(): logging.info(f"\tloading {data_type}# {_run_number} ... ") @@ -138,21 +133,25 @@ def load_images(master_dict=None, data_type=DataType.sample, verbose=False): display(HTML(f"{master_dict[_run_number][MasterDictKeys.data].shape = }")) -def calculate_ob_data_combined_used_by_spectrum_normalization(roi=None, ob_data_combined=None, verbose=False): - - logging.info(f"Calculating the ob_data_combined for spectrum normalization") +def calculate_ob_data_combined_used_by_spectrum_normalization( + roi=None, ob_data_combined=None, verbose=False +): + logging.info("Calculating the ob_data_combined for spectrum normalization") if roi is not None: logging.info(f"\t{roi =}") x0 = roi.left y0 = roi.top width = roi.width height = roi.height - ob_data_combined_for_spectrum = [np.sum(np.sum(_data[y0:y0 + height, x0:x0 + width], axis=0), axis=0) for _data in ob_data_combined] + ob_data_combined_for_spectrum = [ + np.sum(np.sum(_data[y0 : y0 + height, x0 : x0 + width], axis=0), axis=0) + for _data in ob_data_combined + ] logging.info(f"\t{np.shape(ob_data_combined_for_spectrum) = }") logging.info(f"\t{np.shape(ob_data_combined) = }") else: - logging.info(f"\tno roi provided! Skipping the normalization of spectrum.") + logging.info("\tno roi provided! Skipping the normalization of spectrum.") ob_data_combined_for_spectrum = None if verbose: @@ -161,7 +160,9 @@ def calculate_ob_data_combined_used_by_spectrum_normalization(roi=None, ob_data return ob_data_combined_for_spectrum -def correct_chips_alignment(data_combined=None, correct_chips_alignment_config=None, verbose=False): +def correct_chips_alignment( + data_combined=None, correct_chips_alignment_config=None, verbose=False +): """ correct the chips position (fill the gaps between the chips) using the dedicated library timepix_geometry_correction (https://github.com/ornlneutronimaging/timepix_geometry_correction) @@ -179,19 +180,23 @@ def correct_chips_alignment(data_combined=None, correct_chips_alignment_config=N # do the math here on the data_combined logging.info("Chips alignment corrected!") - + if verbose: display(HTML("Chips alignment corrected!")) - + return data_combined -def correct_all_samples_chips_alignment(sample_master_dict=None, correct_chips_alignment_config=None, verbose=False): +def correct_all_samples_chips_alignment( + sample_master_dict=None, correct_chips_alignment_config=None, verbose=False +): for _sample_run_number in sample_master_dict.keys(): - sample_master_dict[_sample_run_number][MasterDictKeys.data] = correct_chips_alignment( - sample_master_dict[_sample_run_number][MasterDictKeys.data], - correct_chips_alignment_config, - verbose=verbose + sample_master_dict[_sample_run_number][MasterDictKeys.data] = ( + correct_chips_alignment( + sample_master_dict[_sample_run_number][MasterDictKeys.data], + correct_chips_alignment_config, + verbose=verbose, + ) ) @@ -217,15 +222,19 @@ def normalize_by_monitor_counts(master_dict=None, run_number=None, data=None): return data - -def preview_normalized_data(_sample_data, ob_data_combined, dc_data_combined, - normalized_data, - lambda_array, energy_array, - detector_delay_us, _sample_run_number, - combine_samples=False, - _spectrum_normalized_data=None, - roi=None): - +def preview_normalized_data( + _sample_data, + ob_data_combined, + dc_data_combined, + normalized_data, + lambda_array, + energy_array, + detector_delay_us, + _sample_run_number, + combine_samples=False, + _spectrum_normalized_data=None, + roi=None, +): """preview normalized data""" # display preview of normalized data @@ -236,12 +245,12 @@ def preview_normalized_data(_sample_data, ob_data_combined, dc_data_combined, display(HTML(f"

Preview of run {_sample_run_number}

")) display(HTML(f"detector delay: {detector_delay_us:.2f} us")) - - axs1[0].set_title(f"Integrated Sample data") + + axs1[0].set_title("Integrated Sample data") sample_integrated1 = np.nansum(_sample_data, axis=1) sample_integrated = np.nansum(sample_integrated1, axis=1) - axs1[1].plot(sample_integrated, 'o') + axs1[1].plot(sample_integrated, "o") axs1[1].set_xlabel("File image index") axs1[1].set_ylabel("Transmission (a.u.)") plt.tight_layout() @@ -254,13 +263,15 @@ def preview_normalized_data(_sample_data, ob_data_combined, dc_data_combined, ob_integrated1 = np.nansum(ob_data_combined, axis=1) ob_integrated = np.nansum(ob_integrated1, axis=1) - axs2[1].plot(ob_integrated, 'o') + axs2[1].plot(ob_integrated, "o") axs2[1].set_xlabel("File image index") axs2[1].set_ylabel("Transmission (a.u.)") plt.tight_layout() if dc_data_combined is not None: - fig, axs_dc = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) + fig, axs_dc = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) dc_data_integrated = np.nanmean(dc_data_combined, axis=0) im_dc = axs_dc[0].imshow(dc_data_integrated, cmap="gray") plt.colorbar(im_dc, ax=axs_dc[0]) @@ -268,7 +279,7 @@ def preview_normalized_data(_sample_data, ob_data_combined, dc_data_combined, dc_integrated1 = np.nansum(dc_data_combined, axis=1) dc_integrated = np.nansum(dc_integrated1, axis=1) - axs_dc[1].plot(dc_integrated, 'o') + axs_dc[1].plot(dc_integrated, "o") axs_dc[1].set_xlabel("File image index") axs_dc[1].set_ylabel("Transmission (a.u.)") plt.tight_layout() @@ -278,16 +289,21 @@ def preview_normalized_data(_sample_data, ob_data_combined, dc_data_combined, normalized_data_integrated = np.nanmean(normalized_data[_sample_run_number], axis=0) im2 = axs3[0].imshow(normalized_data_integrated, cmap="gray") plt.colorbar(im2, ax=axs3[0]) - axs3[0].set_title(f"Integrated Normalized data") + axs3[0].set_title("Integrated Normalized data") if roi is not None: x0 = roi.left y0 = roi.top width = roi.width height = roi.height - axs3[0].add_patch(plt.Rectangle((x0, y0), width, height, fill=False, color="red", lw=2)) + axs3[0].add_patch( + plt.Rectangle((x0, y0), width, height, fill=False, color="red", lw=2) + ) - profile_step1 = np.nanmean(normalized_data[_sample_run_number][:, y0:y0+height, x0:x0+width], axis=1) + profile_step1 = np.nanmean( + normalized_data[_sample_run_number][:, y0 : y0 + height, x0 : x0 + width], + axis=1, + ) profile = np.nanmean(profile_step1, axis=1) _label = "pixel by pixel normalization profile of ROI" @@ -296,7 +312,7 @@ def preview_normalized_data(_sample_data, ob_data_combined, dc_data_combined, profile = np.nanmean(profile_step1, axis=1) _label = "pixel by pixel normalization profile of full image" - axs3[1].plot(profile, 'o', label=_label) + axs3[1].plot(profile, "o", label=_label) axs3[1].set_xlabel("File image index") axs3[1].set_ylabel("Transmission (a.u.)") axs3[1].legend() @@ -320,20 +336,29 @@ def preview_normalized_data(_sample_data, ob_data_combined, dc_data_combined, plt.tight_layout() if _spectrum_normalized_data is not None: - - fig, axs6 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) + fig, axs6 = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) logging.info(f"{np.shape(profile) = }") - axs6[0].plot(lambda_array, _spectrum_normalized_data, "r*", - markersize=MARKERSIZE, - label="spectrum normalization of ROI") + axs6[0].plot( + lambda_array, + _spectrum_normalized_data, + "r*", + markersize=MARKERSIZE, + label="spectrum normalization of ROI", + ) axs6[0].set_xlabel("Lambda (A)") axs6[0].set_ylabel("Transmission (a.u.)") axs6[0].legend() - axs6[1].plot(energy_array, _spectrum_normalized_data, "r*", - markersize=MARKERSIZE, - label="spectrum normalization of ROI") + axs6[1].plot( + energy_array, + _spectrum_normalized_data, + "r*", + markersize=MARKERSIZE, + label="spectrum normalization of ROI", + ) axs6[1].set_xlabel("Energy (eV)") axs6[1].set_ylabel("Transmission (a.u.)") axs6[1].set_xscale("log") @@ -343,11 +368,14 @@ def preview_normalized_data(_sample_data, ob_data_combined, dc_data_combined, plt.show() + def get_detector_offset_from_nexus(nexus_path: str) -> float: """get the detector offset from the nexus file""" with h5py.File(nexus_path, "r") as hdf5_data: try: - detector_offset_micros = hdf5_data["entry"]["DASlogs"]["BL10:Det:TH:DSPT1:TIDelay"]["value"][0] + detector_offset_micros = hdf5_data["entry"]["DASlogs"][ + "BL10:Det:TH:DSPT1:TIDelay" + ]["value"][0] # detector_offset_micros = hdf5_data["entry"]["DASlogs"]["BL10:Det:DSP1:Trig2:Delay"]["value"][0] except KeyError: detector_offset_micros = None @@ -363,6 +391,7 @@ def get_run_number_from_nexus(nexus_path: str) -> int: run_number = None return run_number + def export_sample_images( output_folder, export_corrected_stack_of_sample_data, @@ -370,7 +399,7 @@ def export_sample_images( _sample_run_number, _sample_data, spectra_file_name=None, - spectra_array=None + spectra_array=None, ): logging.info(f"> Exporting sample corrected images to {output_folder} ...") @@ -393,30 +422,37 @@ def export_sample_images( if spectra_array is not None: # manually create the file for spectra - spectra_file_name = os.path.join(output_stack_folder, f"manually_created_{SPECTRA_FILE_PREFIX}") + spectra_file_name = os.path.join( + output_stack_folder, f"manually_created_{SPECTRA_FILE_PREFIX}" + ) _full_counts_array = np.empty_like(spectra_array) for _index, _data in enumerate(_sample_data): _full_counts_array[_index] = np.nansum(_data) - pd_spectra = pd.DataFrame({ - "shutter_time": spectra_array, - "counts": _full_counts_array - }) + pd_spectra = pd.DataFrame( + {"shutter_time": spectra_array, "counts": _full_counts_array} + ) pd_spectra.to_csv(spectra_file_name, index=False, sep=",") - logging.info(f"\t -> Exporting manually created spectra file to {spectra_file_name} is done!") + logging.info( + f"\t -> Exporting manually created spectra file to {spectra_file_name} is done!" + ) else: shutil.copy(spectra_file_name, os.path.join(output_stack_folder)) - logging.info(f"\t -> Exporting spectra file {spectra_file_name} to {output_stack_folder} is done!") - + logging.info( + f"\t -> Exporting spectra file {spectra_file_name} to {output_stack_folder} is done!" + ) + display(HTML(f"Created folder {output_stack_folder} for sample outputs!")) - + if export_corrected_integrated_sample_data: # making up the integrated sample data sample_data_integrated = np.nanmean(_sample_data, axis=0) full_file_name = os.path.join(sample_output_folder, "integrated.tif") logging.info(f"\t -> Exporting integrated sample data to {full_file_name} ...") make_tiff(data=sample_data_integrated, filename=full_file_name) - logging.info(f"\t -> Exporting integrated sample data to {full_file_name} is done!") + logging.info( + f"\t -> Exporting integrated sample data to {full_file_name} is done!" + ) def export_ob_images( @@ -432,10 +468,13 @@ def export_ob_images( logging.info(f"> Exporting combined ob images to {output_folder} ...") logging.info(f"\t{ob_run_numbers = }") list_ob_runs_number_only = [ - str(isolate_run_number_from_full_path(_ob_run_number)) for _ob_run_number in ob_run_numbers + str(isolate_run_number_from_full_path(_ob_run_number)) + for _ob_run_number in ob_run_numbers ] if len(list_ob_runs_number_only) == 1: - ob_output_folder = os.path.join(output_folder, f"ob_{list_ob_runs_number_only[0]}") + ob_output_folder = os.path.join( + output_folder, f"ob_{list_ob_runs_number_only[0]}" + ) else: str_list_ob_runs = "_".join(list_ob_runs_number_only) ob_output_folder = os.path.join(output_folder, f"ob_{str_list_ob_runs}") @@ -462,24 +501,29 @@ def export_ob_images( _output_file = os.path.join(output_stack_folder, f"image{_index:04d}.tif") make_tiff(data=_data, filename=_output_file) logging.info(f"\t -> Exporting ob data to {output_stack_folder} is done!") - + if spectra_array is not None: # manually create the file for spectra - spectra_file_name = os.path.join(output_stack_folder, f"manually_created_{SPECTRA_FILE_PREFIX}") + spectra_file_name = os.path.join( + output_stack_folder, f"manually_created_{SPECTRA_FILE_PREFIX}" + ) _full_counts_array = np.empty_like(spectra_array) for _index, _data in enumerate(ob_data_combined): _full_counts_array[_index] = np.nansum(_data) - pd_spectra = pd.DataFrame({ - "shutter_time": spectra_array, - "counts": _full_counts_array - }) + pd_spectra = pd.DataFrame( + {"shutter_time": spectra_array, "counts": _full_counts_array} + ) pd_spectra.to_csv(spectra_file_name, index=False, sep=",") - logging.info(f"\t -> Exporting manually created spectra file to {spectra_file_name} is done!") + logging.info( + f"\t -> Exporting manually created spectra file to {spectra_file_name} is done!" + ) else: # copy spectra file to the output folder shutil.copy(spectra_file_name, os.path.join(output_stack_folder)) - logging.info(f"\t -> Exported spectra file {spectra_file_name} to {output_stack_folder}!") + logging.info( + f"\t -> Exported spectra file {spectra_file_name} to {output_stack_folder}!" + ) display(HTML(f"Created folder {output_stack_folder} for OB outputs!")) @@ -571,34 +615,40 @@ def update_dict_with_shutter_counts(master_dict: dict) -> tuple[dict, bool]: if _value == "0": break list_shutter_counts.append(float(_value)) - master_dict[run_number][MasterDictKeys.shutter_counts] = list_shutter_counts - + master_dict[run_number][MasterDictKeys.shutter_counts] = ( + list_shutter_counts + ) + return master_dict, status_all_shutter_counts_found -def update_dict_with_spectra_files(master_dict: dict, spectra_array: np.ndarray = None) -> tuple[dict, bool]: +def update_dict_with_spectra_files( + master_dict: dict, spectra_array: np.ndarray = None +) -> tuple[dict, bool]: """update the master dict with spectra values from spectra file""" status_all_spectra_found = True for _run_number in master_dict.keys(): - if spectra_array is not None: master_dict[_run_number][MasterDictKeys.list_spectra] = spectra_array - master_dict[_run_number][MasterDictKeys.spectra_file_name] = "Provided array" - - else: + master_dict[_run_number][MasterDictKeys.spectra_file_name] = ( + "Provided array" + ) + else: data_path = master_dict[_run_number][MasterDictKeys.data_path] _list_files = glob.glob(os.path.join(data_path, f"*_{SPECTRA_FILE_PREFIX}")) - + if len(_list_files) == 0: logging.info(f"Spectra file not found for run {_run_number}!") master_dict[_run_number][MasterDictKeys.list_spectra] = None status_all_spectra_found = False continue - + else: spectra_file = _list_files[0] - master_dict[_run_number][MasterDictKeys.spectra_file_name] = spectra_file + master_dict[_run_number][MasterDictKeys.spectra_file_name] = ( + spectra_file + ) pd_spectra = pd.read_csv(spectra_file, sep=",", header=0) shutter_time = pd_spectra["shutter_time"].values master_dict[_run_number][MasterDictKeys.list_spectra] = shutter_time @@ -619,11 +669,15 @@ def update_dict_with_proton_charge(master_dict: dict) -> tuple[dict, bool]: try: with h5py.File(_nexus_path, "r") as hdf5_data: - proton_charge = hdf5_data["entry"][MasterDictKeys.proton_charge][0] / 1e12 + proton_charge = ( + hdf5_data["entry"][MasterDictKeys.proton_charge][0] / 1e12 + ) except KeyError: proton_charge = None status_all_proton_charge_found = False - master_dict[_run_number][MasterDictKeys.proton_charge] = np.float32(proton_charge) + master_dict[_run_number][MasterDictKeys.proton_charge] = np.float32( + proton_charge + ) return status_all_proton_charge_found @@ -644,14 +698,18 @@ def update_dict_with_monitor_counts(master_dict: dict) -> bool: except KeyError: monitor_counts = None status_all_monitor_counts_found = False - master_dict[_run_number][MasterDictKeys.monitor_counts] = np.float32(monitor_counts) + master_dict[_run_number][MasterDictKeys.monitor_counts] = np.float32( + monitor_counts + ) return status_all_monitor_counts_found def update_dict_with_list_of_images(master_dict: dict) -> dict: """update the master dict with list of images""" for _run_number in master_dict.keys(): - list_tif = retrieve_list_of_tif(master_dict[_run_number][MasterDictKeys.data_path]) + list_tif = retrieve_list_of_tif( + master_dict[_run_number][MasterDictKeys.data_path] + ) logging.info(f"Retrieved {len(list_tif)} tif files for run {_run_number}!") master_dict[_run_number][MasterDictKeys.list_tif] = list_tif @@ -663,7 +721,9 @@ def get_list_run_number(data_folder: str) -> list: return list_run_number -def update_dict_with_nexus_full_path(nexus_root_path: str, instrument: str, master_dict: dict) -> dict: +def update_dict_with_nexus_full_path( + nexus_root_path: str, instrument: str, master_dict: dict +) -> dict: """create dict of nexus path for each run number""" for run_number in master_dict.keys(): master_dict[run_number][MasterDictKeys.nexus_path] = os.path.join( @@ -687,7 +747,9 @@ def update_with_nexus_metadata(master_dict: dict) -> dict: def update_dict_with_data_full_path(data_root_path: str, master_dict: dict) -> dict: """create dict of data path for each run number""" for run_number in master_dict.keys(): - master_dict[run_number][MasterDictKeys.data_path] = os.path.join(data_root_path, f"Run_{run_number}") + master_dict[run_number][MasterDictKeys.data_path] = os.path.join( + data_root_path, f"Run_{run_number}" + ) def create_master_dict( @@ -719,7 +781,9 @@ def create_master_dict( # if all_shutter_counts_found: logging.info("updating with spectra values!") - master_dict, all_spectra_found = update_dict_with_spectra_files(master_dict, spectra_array=spectra_array) + master_dict, all_spectra_found = update_dict_with_spectra_files( + master_dict, spectra_array=spectra_array + ) if not all_spectra_found: status_metadata.all_spectra_found = False logging.info(f"{master_dict = }") @@ -742,7 +806,9 @@ def create_master_dict( return master_dict, status_metadata -def produce_list_shutter_for_each_image(list_time_spectra: list = None, list_shutter_counts: list = None) -> list: +def produce_list_shutter_for_each_image( + list_time_spectra: list = None, list_shutter_counts: list = None +) -> list: """produce list of shutter counts for each image""" delat_time_spectra = list_time_spectra[1] - list_time_spectra[0] @@ -752,31 +818,41 @@ def produce_list_shutter_for_each_image(list_time_spectra: list = None, list_shu logging.info(f"\t{list_index_jump = }") logging.info(f"\t{list_shutter_counts = }") - list_shutter_values_for_each_image = np.zeros(len(list_time_spectra), dtype=np.float32) + list_shutter_values_for_each_image = np.zeros( + len(list_time_spectra), dtype=np.float32 + ) if len(list_shutter_counts) == 1: # resonance mode list_shutter_values_for_each_image.fill(list_shutter_counts[0]) return list_shutter_values_for_each_image - list_shutter_values_for_each_image[0 : list_index_jump[0] + 1].fill(list_shutter_counts[0]) + list_shutter_values_for_each_image[0 : list_index_jump[0] + 1].fill( + list_shutter_counts[0] + ) for _index in range(1, len(list_index_jump)): _start = list_index_jump[_index - 1] _end = list_index_jump[_index] - list_shutter_values_for_each_image[_start + 1 : _end + 1].fill(list_shutter_counts[_index]) + list_shutter_values_for_each_image[_start + 1 : _end + 1].fill( + list_shutter_counts[_index] + ) - list_shutter_values_for_each_image[list_index_jump[-1] + 1 :] = list_shutter_counts[-1] + list_shutter_values_for_each_image[list_index_jump[-1] + 1 :] = list_shutter_counts[ + -1 + ] return list_shutter_values_for_each_image -def replace_zero_with_local_median(data: np.ndarray, - kernel_size: Tuple[int, int, int] = (3, 3, 3), - max_iterations: int = 10) -> np.ndarray: +def replace_zero_with_local_median( + data: np.ndarray, + kernel_size: Tuple[int, int, int] = (3, 3, 3), + max_iterations: int = 10, +) -> np.ndarray: """ Replace 0 values in a 3D array using local median filtering. This function ONLY processes small neighborhoods around 0 pixels, avoiding expensive computation on the entire dataset. - + Parameters: ----------- data : np.ndarray @@ -787,7 +863,7 @@ def replace_zero_with_local_median(data: np.ndarray, max_iterations : int Maximum number of iterations to replace 0 values Default is 10 - + Returns: -------- np.ndarray @@ -806,7 +882,7 @@ def replace_zero_with_local_median(data: np.ndarray, # Calculate padding for kernel pad_h, pad_w, pad_d = [k // 2 for k in kernel_size] - + for iteration in range(max_iterations): # Find current 0 locations zero_coords = np.argwhere(result == 0) @@ -816,13 +892,15 @@ def replace_zero_with_local_median(data: np.ndarray, logging.info(f"All 0 values replaced after {iteration} iterations") break - logging.info(f"Iteration {iteration + 1}: {current_zero_count} 0 values remaining") + logging.info( + f"Iteration {iteration + 1}: {current_zero_count} 0 values remaining" + ) # Process each 0 pixel individually replaced_count = 0 for coord in zero_coords: y, x, z = coord - + # Define the local neighborhood bounds y_min = max(0, y - pad_h) y_max = min(result.shape[0], y + pad_h + 1) @@ -830,13 +908,13 @@ def replace_zero_with_local_median(data: np.ndarray, x_max = min(result.shape[1], x + pad_w + 1) z_min = max(0, z - pad_d) z_max = min(result.shape[2], z + pad_d + 1) - + # Extract the local neighborhood neighborhood = result[y_min:y_max, x_min:x_max, z_min:z_max] - + # Get non-NaN values in the neighborhood valid_values = neighborhood[~np.isnan(neighborhood)] - + # If we have valid values, compute median and replace if len(valid_values) > 0: median_value = np.median(valid_values) @@ -848,30 +926,34 @@ def replace_zero_with_local_median(data: np.ndarray, # If no progress was made, break if replaced_count == 0: remaining_zero_count = np.sum(result == 0) - logging.info(f"No progress made. {remaining_zero_count} zero values could not be replaced") + logging.info( + f"No progress made. {remaining_zero_count} zero values could not be replaced" + ) logging.info("(These may be in regions with no valid neighbors)") break final_zero_count = np.sum(result == 0) logging.info(f"Final zero count: {final_zero_count}") - logging.info(f"Successfully replaced {initial_zero_count - final_zero_count} zero values") + logging.info( + f"Successfully replaced {initial_zero_count - final_zero_count} zero values" + ) return result def combine_dc_images(dc_master_dict: dict) -> np.ndarray: """combine all dc images - + Parameters: ----------- dc_master_dict : dict master dict of dc run numbers - + Returns: -------- np.ndarray combined dc data - + """ logging.info("Combining all dark current images") full_dc_data = [] @@ -882,7 +964,9 @@ def combine_dc_images(dc_master_dict: dict) -> np.ndarray: for _dc_run_number in dc_master_dict.keys(): logging.info(f"Combining dc# {_dc_run_number} ...") - dc_data = np.array(dc_master_dict[_dc_run_number][MasterDictKeys.data], dtype=np.float32) + dc_data = np.array( + dc_master_dict[_dc_run_number][MasterDictKeys.data], dtype=np.float32 + ) full_dc_data.append(dc_data) logging.info(f"{np.shape(full_dc_data) = }") @@ -901,11 +985,11 @@ def combine_ob_images( use_shutter_counts: bool = False, replace_ob_zeros_by_nan: bool = False, replace_ob_zeros_by_local_median: bool = False, - kernel_size_for_local_median: Tuple[int, int, int] = (3, 3, 3), + kernel_size_for_local_median: Tuple[int, int, int] = (3, 3, 3), max_iterations: int = 10, ) -> Tuple[np.ndarray, float]: """combine all ob images and correct by proton charge and shutter counts - + Parameters: ----------- ob_master_dict : dict @@ -924,14 +1008,14 @@ def combine_ob_images( kernel size for local median filtering max_iterations : int maximum number of iterations for local median filtering - + Returns: -------- np.ndarray combined ob data float total proton charge used for correction - + """ logging.info("Combining all open beam images") @@ -939,10 +1023,14 @@ def combine_ob_images( # logging.info(f"\tcorrecting by monitor counts: {use_monitor_counts}") logging.info(f"\tshutter counts: {use_shutter_counts}") logging.info(f"\treplace ob zeros by nan: {replace_ob_zeros_by_nan}") - logging.info(f"\treplace ob zeros by local median: {replace_ob_zeros_by_local_median}") - logging.info(f"\tkernel size for local median: y:{kernel_size_for_local_median[0]}, " - f"x:{kernel_size_for_local_median[1]}, " - f"tof:{kernel_size_for_local_median[2]}") + logging.info( + f"\treplace ob zeros by local median: {replace_ob_zeros_by_local_median}" + ) + logging.info( + f"\tkernel size for local median: y:{kernel_size_for_local_median[0]}, " + f"x:{kernel_size_for_local_median[1]}, " + f"tof:{kernel_size_for_local_median[2]}" + ) full_ob_data_corrected = [] if use_proton_charge: @@ -961,7 +1049,9 @@ def combine_ob_images( for _ob_run_number in ob_master_dict.keys(): logging.info(f"Combining ob# {_ob_run_number} ...") - ob_data = np.array(ob_master_dict[_ob_run_number][MasterDictKeys.data], dtype=np.float32) + ob_data = np.array( + ob_master_dict[_ob_run_number][MasterDictKeys.data], dtype=np.float32 + ) # get statistics of ob data data_shape = ob_data.shape @@ -970,7 +1060,9 @@ def combine_ob_images( number_of_zeros = np.sum(ob_data == 0) logging.info(f"\t ob data shape: {data_shape}") logging.info(f"\t Number of zeros in ob data: {number_of_zeros}") - logging.info(f"\t Percentage of zeros in ob data: {number_of_zeros / (data_shape[0] * nbr_pixels) * 100:.2f}%") + logging.info( + f"\t Percentage of zeros in ob data: {number_of_zeros / (data_shape[0] * nbr_pixels) * 100:.2f}%" + ) logging.info(f"\t Mean of ob data: {np.mean(ob_data)}") logging.info(f"\t maximum of ob data: {np.max(ob_data)}") logging.info(f"\t minimum of ob data: {np.min(ob_data)}") @@ -982,7 +1074,7 @@ def combine_ob_images( logging.info(f"\t\t proton charge: {proton_charge} C") logging.info(f"\t\t{type(proton_charge) = }") logging.info(f"\t\tbefore division: {proton_charge.dtype = }") - ob_data *= (proton_charge / sum_proton_charge) # weighted sum + ob_data *= proton_charge / sum_proton_charge # weighted sum logging.info(f"\t\tafter division: {ob_data.dtype = }") logging.info(f"{ob_data.shape = }") @@ -998,14 +1090,20 @@ def combine_ob_images( logging.info("\t -> Normalized by shutter counts") list_shutter_values_for_each_image = produce_list_shutter_for_each_image( - list_time_spectra=ob_master_dict[_ob_run_number][MasterDictKeys.list_spectra], - list_shutter_counts=ob_master_dict[_ob_run_number][MasterDictKeys.shutter_counts], + list_time_spectra=ob_master_dict[_ob_run_number][ + MasterDictKeys.list_spectra + ], + list_shutter_counts=ob_master_dict[_ob_run_number][ + MasterDictKeys.shutter_counts + ], ) logging.info(f"{list_shutter_values_for_each_image.shape = }") temp_ob_data = np.empty_like(ob_data, dtype=np.float32) for _index in range(len(list_shutter_values_for_each_image)): - temp_ob_data[_index] = ob_data[_index] / list_shutter_values_for_each_image[_index] + temp_ob_data[_index] = ( + ob_data[_index] / list_shutter_values_for_each_image[_index] + ) logging.info(f"{temp_ob_data.shape = }") ob_data = temp_ob_data.copy() @@ -1013,9 +1111,11 @@ def combine_ob_images( # logging.info(f"{ob_data_combined.shape = }") if replace_ob_zeros_by_local_median: - ob_data = replace_zero_with_local_median(ob_data, - kernel_size=kernel_size_for_local_median, - max_iterations=max_iterations) + ob_data = replace_zero_with_local_median( + ob_data, + kernel_size=kernel_size_for_local_median, + max_iterations=max_iterations, + ) full_ob_data_corrected.append(ob_data) logging.info(f"{np.shape(full_ob_data_corrected) = }") @@ -1026,7 +1126,7 @@ def combine_ob_images( ob_data_combined = np.array(full_ob_data_corrected).sum(axis=0) else: ob_data_combined = np.array(full_ob_data_corrected).mean(axis=0) - + logging.info(f"\tafter: {ob_data_combined.shape = }") # remove zeros @@ -1044,11 +1144,11 @@ def combine_images( # use_shutter_counts: bool = False, replace_zeros_by_nan: bool = False, replace_zeros_by_local_median: bool = False, - kernel_size_for_local_median: Tuple[int, int, int] = (3, 3, 3), + kernel_size_for_local_median: Tuple[int, int, int] = (3, 3, 3), max_iterations: int = 10, ) -> Tuple[np.ndarray, float]: """combine all images and correct by proton charge and shutter counts - + Parameters: ----------- master_dict : dict @@ -1067,14 +1167,14 @@ def combine_images( kernel size for local median filtering max_iterations : int maximum number of iterations for local median filtering - + Returns: -------- np.ndarray combined data float total proton charge used for correction - + """ logging.info(f"Combining all {data_type} images") @@ -1083,9 +1183,11 @@ def combine_images( # logging.info(f"\tshutter counts: {use_shutter_counts}") logging.info(f"\treplace zeros by nan: {replace_zeros_by_nan}") logging.info(f"\treplace zeros by local median: {replace_zeros_by_local_median}") - logging.info(f"\tkernel size for local median: y:{kernel_size_for_local_median[0]}, " - f"x:{kernel_size_for_local_median[1]}, " - f"tof:{kernel_size_for_local_median[2]}") + logging.info( + f"\tkernel size for local median: y:{kernel_size_for_local_median[0]}, " + f"x:{kernel_size_for_local_median[1]}, " + f"tof:{kernel_size_for_local_median[2]}" + ) full_data_corrected = [] if use_proton_charge: @@ -1095,10 +1197,14 @@ def combine_images( for _run_number in master_dict.keys(): proton_charge = master_dict[_run_number][MasterDictKeys.proton_charge] list_proton_charges.append(proton_charge) - logging.info(f"\t {data_type}# {_run_number}: proton charge = {proton_charge} C") + logging.info( + f"\t {data_type}# {_run_number}: proton charge = {proton_charge} C" + ) sum_proton_charge = np.sum(list_proton_charges) - logging.info(f"\t Total proton charge of all {data_type} runs: {sum_proton_charge} C") + logging.info( + f"\t Total proton charge of all {data_type} runs: {sum_proton_charge} C" + ) else: sum_proton_charge = 1.0 # dummy value to avoid division by zero @@ -1113,7 +1219,9 @@ def combine_images( number_of_zeros = np.sum(data == 0) logging.info(f"\t {data_type} data shape: {data_shape}") logging.info(f"\t Number of zeros in {data_type} data: {number_of_zeros}") - logging.info(f"\t Percentage of zeros in {data_type} data: {number_of_zeros / (data_shape[0] * nbr_pixels) * 100:.2f}%") + logging.info( + f"\t Percentage of zeros in {data_type} data: {number_of_zeros / (data_shape[0] * nbr_pixels) * 100:.2f}%" + ) logging.info(f"\t Mean of {data_type} data: {np.mean(data)}") logging.info(f"\t maximum of {data_type} data: {np.max(data)}") logging.info(f"\t minimum of {data_type} data: {np.min(data)}") @@ -1125,14 +1233,16 @@ def combine_images( logging.info(f"\t\t proton charge: {proton_charge} C") logging.info(f"\t\t{type(proton_charge) = }") logging.info(f"\t\tbefore division: {proton_charge.dtype = }") - data *= (proton_charge / sum_proton_charge) # weighted sum + data *= proton_charge / sum_proton_charge # weighted sum logging.info(f"\t\tafter division: {data.dtype = }") logging.info(f"{data.shape = }") if replace_zeros_by_local_median: - data = replace_zero_with_local_median(data, - kernel_size=kernel_size_for_local_median, - max_iterations=max_iterations) + data = replace_zero_with_local_median( + data, + kernel_size=kernel_size_for_local_median, + max_iterations=max_iterations, + ) full_data_corrected.append(data) logging.info(f"{np.shape(full_data_corrected) = }") @@ -1143,7 +1253,7 @@ def combine_images( data_combined = np.array(full_data_corrected).sum(axis=0) else: data_combined = np.array(full_data_corrected).mean(axis=0) - + logging.info(f"\tafter: {data_combined.shape = }") # remove zeros @@ -1161,13 +1271,13 @@ def combine_images( # ): # """ # Normalize sample data by shutter counts for each image. - + # This function normalizes sample data by dividing each image by its corresponding # shutter count value. The shutter count values are determined by mapping the time # spectra from the open beam data to the shutter counts recorded for the sample. # Images with zero shutter counts are replaced with NaN values to avoid division # by zero errors. - + # Parameters # ---------- # sample_master_dict : dict, optional @@ -1182,13 +1292,13 @@ def combine_images( # Expected to have structure: {run_number: {MasterDictKeys.list_spectra: list, ...}} # first_ob_run_number : str or int, optional # The run number key to access the time spectra from the first open beam run - + # Returns # ------- # numpy.ndarray # Normalized sample data array with same shape as input _sample_data. # Images corresponding to zero shutter counts are set to NaN. - + # Notes # ----- # The normalization process involves: @@ -1197,10 +1307,10 @@ def combine_images( # 3. Mapping shutter count values to each image based on time spectra # 4. Dividing each sample image by its corresponding shutter count # 5. Setting images with zero shutter counts to NaN - + # This function is typically used in neutron imaging data processing where # shutter counts represent the exposure time or beam intensity for each image. - + # Examples # -------- # >>> normalized_data = normalization_by_shutter_counts( @@ -1211,7 +1321,7 @@ def combine_images( # ... first_ob_run_number="Run_12340" # ... ) # """ - + # list_shutter_values_for_each_image = produce_list_shutter_for_each_image( # list_time_spectra=ob_master_dict[first_ob_run_number][MasterDictKeys.list_spectra], # list_shutter_counts=sample_master_dict[_sample_run_number][MasterDictKeys.shutter_counts], @@ -1228,40 +1338,63 @@ def combine_images( # return _sample_data -def perform_normalization(_sample_data=None, ob_data_combined=None, dc_data_combined=None): - +def perform_normalization( + _sample_data=None, ob_data_combined=None, dc_data_combined=None +): # working on each image (TOF) independently if dc_data_combined is not None: - logging.info(f"normalization with DC subtraction") - _normalized_data = np.divide(np.subtract(_sample_data, dc_data_combined), np.subtract(ob_data_combined, dc_data_combined), - out=np.zeros_like(_sample_data), - where=(ob_data_combined - dc_data_combined)!=0) + logging.info("normalization with DC subtraction") + _normalized_data = np.divide( + np.subtract(_sample_data, dc_data_combined), + np.subtract(ob_data_combined, dc_data_combined), + out=np.zeros_like(_sample_data), + where=(ob_data_combined - dc_data_combined) != 0, + ) else: - logging.info(f"normalization without DC subtraction") - _normalized_data = np.divide(_sample_data, ob_data_combined, - out=np.zeros_like(_sample_data), - where=ob_data_combined!=0) + logging.info("normalization without DC subtraction") + _normalized_data = np.divide( + _sample_data, + ob_data_combined, + out=np.zeros_like(_sample_data), + where=ob_data_combined != 0, + ) _normalized_data[ob_data_combined == 0] = 0 - + # Integration of sample, dc and ob and then division if dc_data_combined is not None: - logging.info(f"normalization with DC subtraction - integrated") - _integrated_normalized_data = np.divide(np.subtract(np.sum(_sample_data, axis=0), np.sum(dc_data_combined, axis=0)), - np.subtract(np.sum(ob_data_combined, axis=0), np.sum(dc_data_combined, axis=0)), - out=np.zeros_like(np.sum(_sample_data, axis=0)), - where=(np.sum(ob_data_combined, axis=0) - np.sum(dc_data_combined, axis=0))!=0) + logging.info("normalization with DC subtraction - integrated") + _integrated_normalized_data = np.divide( + np.subtract(np.sum(_sample_data, axis=0), np.sum(dc_data_combined, axis=0)), + np.subtract( + np.sum(ob_data_combined, axis=0), np.sum(dc_data_combined, axis=0) + ), + out=np.zeros_like(np.sum(_sample_data, axis=0)), + where=(np.sum(ob_data_combined, axis=0) - np.sum(dc_data_combined, axis=0)) + != 0, + ) else: - logging.info(f"normalization without DC subtraction - integrated") - _integrated_normalized_data = np.divide(np.sum(_sample_data, axis=0), np.sum(ob_data_combined, axis=0), - out=np.zeros_like(np.sum(_sample_data, axis=0)), - where=np.sum(ob_data_combined, axis=0)!=0) + logging.info("normalization without DC subtraction - integrated") + _integrated_normalized_data = np.divide( + np.sum(_sample_data, axis=0), + np.sum(ob_data_combined, axis=0), + out=np.zeros_like(np.sum(_sample_data, axis=0)), + where=np.sum(ob_data_combined, axis=0) != 0, + ) - return {'normalized_data': _normalized_data, - 'integrated_normalized_data': _integrated_normalized_data} + return { + "normalized_data": _normalized_data, + "integrated_normalized_data": _integrated_normalized_data, + } -def perform_spectrum_normalization(roi=None, sample_data=None, ob_data_combined_for_spectrum=None, dc_data_combined=None, dc_data_combined_for_spectrum=None): +def perform_spectrum_normalization( + roi=None, + sample_data=None, + ob_data_combined_for_spectrum=None, + dc_data_combined=None, + dc_data_combined_for_spectrum=None, +): _spectrum_normalized_data = None if roi is not None: x0 = roi.left @@ -1269,36 +1402,50 @@ def perform_spectrum_normalization(roi=None, sample_data=None, ob_data_combined_ width = roi.width height = roi.height - _sample_data_combined_for_spectrum = [np.sum(np.sum(_data[y0: y0+height, x0: x0+width], axis=0), axis=0) for _data in sample_data] + _sample_data_combined_for_spectrum = [ + np.sum(np.sum(_data[y0 : y0 + height, x0 : x0 + width], axis=0), axis=0) + for _data in sample_data + ] if dc_data_combined is not None: - _spectrum_normalized_data = np.divide(np.subtract(_sample_data_combined_for_spectrum, dc_data_combined_for_spectrum), - np.subtract(ob_data_combined_for_spectrum, dc_data_combined_for_spectrum), - out=np.zeros_like(_sample_data_combined_for_spectrum), - where=(ob_data_combined_for_spectrum - dc_data_combined_for_spectrum)!=0) + _spectrum_normalized_data = np.divide( + np.subtract( + _sample_data_combined_for_spectrum, dc_data_combined_for_spectrum + ), + np.subtract( + ob_data_combined_for_spectrum, dc_data_combined_for_spectrum + ), + out=np.zeros_like(_sample_data_combined_for_spectrum), + where=(ob_data_combined_for_spectrum - dc_data_combined_for_spectrum) + != 0, + ) else: - _spectrum_normalized_data = np.divide(_sample_data_combined_for_spectrum, ob_data_combined_for_spectrum, - out=np.zeros_like(_sample_data_combined_for_spectrum), - where=ob_data_combined_for_spectrum!=0) + _spectrum_normalized_data = np.divide( + _sample_data_combined_for_spectrum, + ob_data_combined_for_spectrum, + out=np.zeros_like(_sample_data_combined_for_spectrum), + where=ob_data_combined_for_spectrum != 0, + ) logging.info(f"{np.shape(_spectrum_normalized_data) = }") return _spectrum_normalized_data -def export_normalized_data(ob_master_dict=None, - sample_master_dict=None, - _sample_run_number=None, - normalized_data=None, - integrated_normalized_data=None, - _spectrum_normalized_data=None, - lambda_array=None, - energy_array=None, - output_folder="./", - export_corrected_stack_of_normalized_data=False, - export_corrected_integrated_normalized_data=False, - roi=None, - spectra_array=None, - spectra_file=None,): - +def export_normalized_data( + ob_master_dict=None, + sample_master_dict=None, + _sample_run_number=None, + normalized_data=None, + integrated_normalized_data=None, + _spectrum_normalized_data=None, + lambda_array=None, + energy_array=None, + output_folder="./", + export_corrected_stack_of_normalized_data=False, + export_corrected_integrated_normalized_data=False, + roi=None, + spectra_array=None, + spectra_file=None, +): logging.info("Exporting normalized data ...") list_ob_runs = list(ob_master_dict.keys()) @@ -1310,38 +1457,52 @@ def export_normalized_data(ob_master_dict=None, os.makedirs(full_output_folder, exist_ok=True) if roi is not None: - logging.info(f"\t -> exporting the spectrum normalization") + logging.info("\t -> exporting the spectrum normalization") logging.info(f"{roi =}") x0 = roi.left y0 = roi.top width = roi.width height = roi.height - full_file_name = os.path.join(full_output_folder, "spectrum_normalization_profile.txt") - pd_dataframe = pd.DataFrame({ - "file_index": np.arange(len(lambda_array)), - "lambda (Angstroms)": lambda_array, - "energy (eV)": energy_array, - "spectrum normalization": _spectrum_normalized_data - }) - pd_dataframe.attrs['roi [left, top, width, height]'] = f"{x0}, {y0}, {width}, {height}" - - with open(full_file_name, 'w') as f: + full_file_name = os.path.join( + full_output_folder, "spectrum_normalization_profile.txt" + ) + pd_dataframe = pd.DataFrame( + { + "file_index": np.arange(len(lambda_array)), + "lambda (Angstroms)": lambda_array, + "energy (eV)": energy_array, + "spectrum normalization": _spectrum_normalized_data, + } + ) + pd_dataframe.attrs["roi [left, top, width, height]"] = ( + f"{x0}, {y0}, {width}, {height}" + ) + + with open(full_file_name, "w") as f: # Write metadata as comments for key, value in pd_dataframe.attrs.items(): f.write(f"# {key}: {value}\n") - + # Write the DataFrame - pd_dataframe.to_csv(f, index=False) - + pd_dataframe.to_csv(f, index=False) + pd_dataframe.to_csv(full_file_name, index=False, sep=",") - logging.info(f"\t -> Exporting the spectrum normalization profile to {full_file_name}") + logging.info( + f"\t -> Exporting the spectrum normalization profile to {full_file_name}" + ) if export_corrected_integrated_normalized_data: # making up the integrated sample data full_file_name = os.path.join(full_output_folder, "normalized_integrated.tif") - logging.info(f"\t -> Exporting integrated normalized data to {full_file_name} ...") - make_tiff(data=integrated_normalized_data[_sample_run_number], filename=full_file_name) - logging.info(f"\t -> Exporting integrated normalized data to {full_file_name} is done!") + logging.info( + f"\t -> Exporting integrated normalized data to {full_file_name} ..." + ) + make_tiff( + data=integrated_normalized_data[_sample_run_number], filename=full_file_name + ) + logging.info( + f"\t -> Exporting integrated normalized data to {full_file_name} is done!" + ) if export_corrected_stack_of_normalized_data: output_stack_folder = os.path.join(full_output_folder, "stack") @@ -1351,15 +1512,18 @@ def export_normalized_data(ob_master_dict=None, for _index, _data in enumerate(normalized_data[_sample_run_number]): _output_file = os.path.join(output_stack_folder, f"image{_index:04d}.tif") make_tiff(data=_data, filename=_output_file) - logging.info(f"\t -> Exporting normalized data to {output_stack_folder} is done!") + logging.info( + f"\t -> Exporting normalized data to {output_stack_folder} is done!" + ) print(f"Exported normalized tif images are in: {output_stack_folder}!") - - # spectra_file = sample_master_dict[_sample_run_number][MasterDictKeys.spectra_file_name] - export_spectra_file(spectra_array=spectra_array, - spectra_file=spectra_file, - output_stack_folder=output_stack_folder, - normalized_data=normalized_data[_sample_run_number]) + # spectra_file = sample_master_dict[_sample_run_number][MasterDictKeys.spectra_file_name] + export_spectra_file( + spectra_array=spectra_array, + spectra_file=spectra_file, + output_stack_folder=output_stack_folder, + normalized_data=normalized_data[_sample_run_number], + ) # create x-axis file create_x_axis_file( @@ -1369,49 +1533,58 @@ def export_normalized_data(ob_master_dict=None, ) -def manually_create_and_export_spectra_file(spectra_array=None, output_folder=None, normalized_data=None): - # manually create the file for spectra - spectra_file_name = os.path.join(output_folder, f"manually_created_{SPECTRA_FILE_PREFIX}") - _full_counts_array = np.empty_like(spectra_array) - for _index, _data in enumerate(normalized_data): - _full_counts_array[_index] = np.nansum(_data) - pd_spectra = pd.DataFrame({ - "shutter_time": spectra_array, - "counts": _full_counts_array - }) - pd_spectra.to_csv(spectra_file_name, index=False, sep=",") - logging.info(f"\t -> Exporting manually created spectra file to {spectra_file_name} is done!") - - -def export_spectra_file(spectra_array=None, - spectra_file=None, - output_stack_folder=None, - normalized_data=None): - +def manually_create_and_export_spectra_file( + spectra_array=None, output_folder=None, normalized_data=None +): + # manually create the file for spectra + spectra_file_name = os.path.join( + output_folder, f"manually_created_{SPECTRA_FILE_PREFIX}" + ) + _full_counts_array = np.empty_like(spectra_array) + for _index, _data in enumerate(normalized_data): + _full_counts_array[_index] = np.nansum(_data) + pd_spectra = pd.DataFrame( + {"shutter_time": spectra_array, "counts": _full_counts_array} + ) + pd_spectra.to_csv(spectra_file_name, index=False, sep=",") + logging.info( + f"\t -> Exporting manually created spectra file to {spectra_file_name} is done!" + ) + + +def export_spectra_file( + spectra_array=None, + spectra_file=None, + output_stack_folder=None, + normalized_data=None, +): if spectra_array is not None: - manually_create_and_export_spectra_file(spectra_array=spectra_array, - output_folder=output_stack_folder, - normalized_data=normalized_data) + manually_create_and_export_spectra_file( + spectra_array=spectra_array, + output_folder=output_stack_folder, + normalized_data=normalized_data, + ) else: - if spectra_file and Path(spectra_file).exists(): - logging.info(f"Exported time spectra file {spectra_file} to {output_stack_folder}!") + logging.info( + f"Exported time spectra file {spectra_file} to {output_stack_folder}!" + ) shutil.copy(spectra_file, output_stack_folder) -def export_corrected_normalized_data(sample_master_dict=None, - ob_master_dict=None, - combined_normalized_data=None, - integrated_normalized_data=None, - export_corrected_integrated_combined_normalized_data=False, - export_corrected_stack_of_combined_normalized_data=False, - lambda_array=None, - energy_array=None, - output_folder="./", - spectra_array=None +def export_corrected_normalized_data( + sample_master_dict=None, + ob_master_dict=None, + combined_normalized_data=None, + integrated_normalized_data=None, + export_corrected_integrated_combined_normalized_data=False, + export_corrected_stack_of_combined_normalized_data=False, + lambda_array=None, + energy_array=None, + output_folder="./", + spectra_array=None, ): - list_sample_runs = list(sample_master_dict.keys()) _sample_str = "" for _run in list_sample_runs: @@ -1432,9 +1605,13 @@ def export_corrected_normalized_data(sample_master_dict=None, # making up the integrated sample data data_integrated = np.nanmean(combined_normalized_data, axis=0) full_file_name = os.path.join(full_output_folder, "integrated.tif") - logging.info(f"\t -> Exporting integrated combined normalized data to {full_file_name} ...") + logging.info( + f"\t -> Exporting integrated combined normalized data to {full_file_name} ..." + ) make_tiff(data=data_integrated, filename=full_file_name) - logging.info(f"\t -> Exporting integrated combined normalized data to {full_file_name} is done!") + logging.info( + f"\t -> Exporting integrated combined normalized data to {full_file_name} is done!" + ) if export_corrected_stack_of_combined_normalized_data: output_stack_folder = os.path.join(full_output_folder, "stack") @@ -1444,21 +1621,28 @@ def export_corrected_normalized_data(sample_master_dict=None, for _index, _data in enumerate(combined_normalized_data): _output_file = os.path.join(output_stack_folder, f"image{_index:04d}.tif") make_tiff(data=_data, filename=_output_file) - logging.info(f"\t -> Exporting combined normalized data to {output_stack_folder} is done!") + logging.info( + f"\t -> Exporting combined normalized data to {output_stack_folder} is done!" + ) print(f"Exported combined normalized tif images are in: {output_stack_folder}!") - - export_spectra_file(spectra_array=spectra_array, - spectra_file=spectra_file, - output_stack_folder=output_stack_folder, - normalized_data=combined_normalized_data) + export_spectra_file( + spectra_array=spectra_array, + spectra_file=spectra_file, + output_stack_folder=output_stack_folder, + normalized_data=combined_normalized_data, + ) # copy one of the spectra file to the output folder, or the manually defined one - spectra_file = sample_master_dict[list_sample_runs[0]][MasterDictKeys.spectra_file_name] - export_spectra_file(spectra_array=spectra_array, - spectra_file=spectra_file, - output_stack_folder=output_stack_folder, - normalized_data=combined_normalized_data) + spectra_file = sample_master_dict[list_sample_runs[0]][ + MasterDictKeys.spectra_file_name + ] + export_spectra_file( + spectra_array=spectra_array, + spectra_file=spectra_file, + output_stack_folder=output_stack_folder, + normalized_data=combined_normalized_data, + ) # create x-axis file create_x_axis_file( @@ -1469,57 +1653,69 @@ def export_corrected_normalized_data(sample_master_dict=None, def read_container_roi_file(container_roi_file=None) -> tuple[int, int, int, int]: - master_dict = load_json(container_roi_file) - list_container_values = master_dict['list_container_values'] - return list_container_values - - -def save_container_roi_file(output_folder:str, - sample_run_number: str, - container_roi: Roi, - list_container_values: list, - integrated_image: np.ndarray): + master_dict = load_json(container_roi_file) + list_container_values = master_dict["list_container_values"] + return list_container_values + + +def save_container_roi_file( + output_folder: str, + sample_run_number: str, + container_roi: Roi, + list_container_values: list, + integrated_image: np.ndarray, +): # container_roi_file = os.path.join(output_folder, f"container_roi_of_run_{sample_run_number}.tiff") - container_roi_file = os.path.join(output_folder, f"container_roi_of_run_{sample_run_number}.json") - + container_roi_file = os.path.join( + output_folder, f"container_roi_of_run_{sample_run_number}.json" + ) + logging.info(f"Saving container roi file to {container_roi_file}") # scitiff_dict = {'container_roi': container_roi, # 'list_container_values': list_container_values, # } - + integrated_image = integrated_image.astype(float) list_container_values = [float(_value) for _value in list_container_values] - master_dict = {'integrated_image': integrated_image.tolist(), - 'container_roi': {'left': float(container_roi.left), - 'top': float(container_roi.top), - 'width': float(container_roi.width), - 'height': float(container_roi.height)}, - 'list_container_values': list_container_values} - + master_dict = { + "integrated_image": integrated_image.tolist(), + "container_roi": { + "left": float(container_roi.left), + "top": float(container_roi.top), + "width": float(container_roi.width), + "height": float(container_roi.height), + }, + "list_container_values": list_container_values, + } + save_json(container_roi_file, master_dict) return container_roi_file - - -def normalize_by_container_roi(sample_data: np.ndarray, - container_roi: Roi, - container_roi_file: str, - output_folder: str, - sample_run_number: str) -> np.ndarray: + + +def normalize_by_container_roi( + sample_data: np.ndarray, + container_roi: Roi, + container_roi_file: str, + output_folder: str, + sample_run_number: str, +) -> np.ndarray: """normalize sample data subtracting by container roi""" - logging.info(f"in normalize_by_container_roi:") + logging.info("in normalize_by_container_roi:") if container_roi_file is not None: logging.info(f"\t {container_roi_file = }") - _container_value_array: float = read_container_roi_file(container_roi_file=container_roi_file) + _container_value_array: float = read_container_roi_file( + container_roi_file=container_roi_file + ) logging.info(f"\t{_container_value_array =}") - + _normalized_sample = np.empty_like(sample_data) for i, _sample in enumerate(sample_data): _container_value = _container_value_array[i] _log_sample = -np.log(_sample) _log_container_value = -np.log(_container_value) _log_normalized_sample = _log_sample - _log_container_value - _normalized_sample[i] = np.exp(- _log_normalized_sample) + _normalized_sample[i] = np.exp(-_log_normalized_sample) else: logging.info(f"\t {container_roi = }") @@ -1527,38 +1723,44 @@ def normalize_by_container_roi(sample_data: np.ndarray, y0: int = container_roi.top width: int = container_roi.width height: int = container_roi.height - + _normalized_sample = np.empty_like(sample_data) list_container_values = [] for i, _sample in enumerate(sample_data): - _container_value = np.mean(np.mean(_sample[y0:y0 + height, x0:x0 + width], axis=0), axis=0) + _container_value = np.mean( + np.mean(_sample[y0 : y0 + height, x0 : x0 + width], axis=0), axis=0 + ) list_container_values.append(_container_value) _log_sample = -np.log(_sample) _log_container_value = -np.log(_container_value) _log_normalized_sample = _log_sample - _log_container_value - _normalized_sample[i] = np.exp(- _log_normalized_sample) - + _normalized_sample[i] = np.exp(-_log_normalized_sample) + # save the container roi file - container_roi_file = save_container_roi_file(output_folder=output_folder, - sample_run_number=sample_run_number, - container_roi=container_roi, - list_container_values=list_container_values, - integrated_image=np.sum(sample_data, axis=0)) - + container_roi_file = save_container_roi_file( + output_folder=output_folder, + sample_run_number=sample_run_number, + container_roi=container_roi, + list_container_values=list_container_values, + integrated_image=np.sum(sample_data, axis=0), + ) + return _normalized_sample, container_roi_file def logging_statistics_of_data(data=None, data_type=DataType.sample): - data_shape = data.shape - nbr_pixels = data_shape[1] * data_shape[2] - logging.info(f" **** Statistics of {data_type} data *****") - number_of_zeros = np.sum(data == 0) - logging.info(f"\t {data_type} data shape: {data_shape}") - logging.info(f"\t data type of _sample_data: {data.dtype}") - logging.info(f"\t Number of zeros in {data_type} data: {number_of_zeros}") - logging.info(f"\t Number of nan in {data_type} data: {np.sum(np.isnan(data))}") - logging.info(f"\t Percentage of zeros in {data_type} data: {number_of_zeros / (data_shape[0] * nbr_pixels) * 100:.2f}%") - logging.info(f"\t Mean of {data_type} data: {np.mean(data)}") - logging.info(f"\t maximum of {data_type} data: {np.max(data)}") - logging.info(f"\t minimum of {data_type} data: {np.min(data)}") - logging.info("**********************************") \ No newline at end of file + data_shape = data.shape + nbr_pixels = data_shape[1] * data_shape[2] + logging.info(f" **** Statistics of {data_type} data *****") + number_of_zeros = np.sum(data == 0) + logging.info(f"\t {data_type} data shape: {data_shape}") + logging.info(f"\t data type of _sample_data: {data.dtype}") + logging.info(f"\t Number of zeros in {data_type} data: {number_of_zeros}") + logging.info(f"\t Number of nan in {data_type} data: {np.sum(np.isnan(data))}") + logging.info( + f"\t Percentage of zeros in {data_type} data: {number_of_zeros / (data_shape[0] * nbr_pixels) * 100:.2f}%" + ) + logging.info(f"\t Mean of {data_type} data: {np.mean(data)}") + logging.info(f"\t maximum of {data_type} data: {np.max(data)}") + logging.info(f"\t minimum of {data_type} data: {np.min(data)}") + logging.info("**********************************") diff --git a/notebooks/__code/oncat.py b/notebooks/__code/oncat.py index ed55b60a7..089766018 100755 --- a/notebooks/__code/oncat.py +++ b/notebooks/__code/oncat.py @@ -25,7 +25,10 @@ def __init__(self): def authentication(self): try: - self.oncat.login(self.username, str(getpass.getpass(f"Enter Password for {self.username}:"))) + self.oncat.login( + self.username, + str(getpass.getpass(f"Enter Password for {self.username}:")), + ) except: self.oncat = None @@ -36,7 +39,9 @@ class GetEverything: def __init__(self, instrument="CG1D", facility="HFIR", run="", oncat=None): run = self.__remove_leading_backslash(run) - self.datafiles = oncat.Datafile.retrieve(run, facility=facility, instrument=instrument) + self.datafiles = oncat.Datafile.retrieve( + run, facility=facility, instrument=instrument + ) def __remove_leading_backslash(self, run): return run[1:] @@ -44,15 +49,25 @@ def __remove_leading_backslash(self, run): class GetProjection: def __init__( - self, instrument="CG1D", facility="HFIR", list_files=[], oncat=None, projection=[], with_progressbar=False + self, + instrument="CG1D", + facility="HFIR", + list_files=[], + oncat=None, + projection=[], + with_progressbar=False, ): projection.append("ingested") if with_progressbar: box1 = widgets.HBox( [ - widgets.Label("Retrieving Metadata ...", layout=widgets.Layout(width="30%")), - widgets.IntProgress(max=len(list_files), layout=widgets.Layout(width="70%")), + widgets.Label( + "Retrieving Metadata ...", layout=widgets.Layout(width="30%") + ), + widgets.IntProgress( + max=len(list_files), layout=widgets.Layout(width="70%") + ), ] ) display(box1) diff --git a/notebooks/__code/outliers_filtering/algorithm.py b/notebooks/__code/outliers_filtering/algorithm.py index a8c47e506..03fa3ceaf 100755 --- a/notebooks/__code/outliers_filtering/algorithm.py +++ b/notebooks/__code/outliers_filtering/algorithm.py @@ -20,7 +20,9 @@ def __init__(self, parent=None, data=None): self.parent = parent self.data = copy.deepcopy(data) self.processed_data = copy.deepcopy(data) - self.total_number_of_pixels = self.parent.image_size[0] * self.parent.image_size[1] + self.total_number_of_pixels = ( + self.parent.image_size[0] * self.parent.image_size[1] + ) if self.parent.ui.fix_dead_pixels_checkBox.isChecked(): self.is_dead_pixel_activated = True @@ -49,7 +51,9 @@ def dead_pixels(self): if mask: nbr_pixels = len(mask[0]) self.dead_pixel_stats["number"] = nbr_pixels - self.dead_pixel_stats["percentage"] = (nbr_pixels / self.total_number_of_pixels) * 100 + self.dead_pixel_stats["percentage"] = ( + nbr_pixels / self.total_number_of_pixels + ) * 100 self.data[mask] = self.median_data[mask] def high_counts(self): @@ -58,7 +62,9 @@ def high_counts(self): if where_above_threshold: nbr_pixels = len(where_above_threshold[0]) self.high_counts_stats["number"] = nbr_pixels - self.high_counts_stats["percentage"] = (nbr_pixels / self.total_number_of_pixels) * 100 + self.high_counts_stats["percentage"] = ( + nbr_pixels / self.total_number_of_pixels + ) * 100 self.data[where_above_threshold] = self.median_data[where_above_threshold] def get_dead_pixels_stats(self): diff --git a/notebooks/__code/outliers_filtering/display.py b/notebooks/__code/outliers_filtering/display.py index baeb291fc..f8bc0b201 100755 --- a/notebooks/__code/outliers_filtering/display.py +++ b/notebooks/__code/outliers_filtering/display.py @@ -25,14 +25,20 @@ def raw_image(self, data): self.parent.live_raw_image = _image if not first_update: - _histo_widget.setLevels(self.parent.raw_histogram_level[0], self.parent.raw_histogram_level[1]) + _histo_widget.setLevels( + self.parent.raw_histogram_level[0], self.parent.raw_histogram_level[1] + ) # histogram self.parent.ui.raw_histogram_plot.clear() min = 0 max = np.max(_image) - y, x = np.histogram(_image, bins=np.linspace(min, max + 1, self.parent.nbr_histo_bins)) - self.parent.ui.raw_histogram_plot.plot(x, y, stepMode=True, fillLevel=0, brush=(0, 0, 255, 150)) + y, x = np.histogram( + _image, bins=np.linspace(min, max + 1, self.parent.nbr_histo_bins) + ) + self.parent.ui.raw_histogram_plot.plot( + x, y, stepMode=True, fillLevel=0, brush=(0, 0, 255, 150) + ) def filtered_image(self, data): _view = self.parent.ui.filtered_image_view.getView() @@ -54,14 +60,21 @@ def filtered_image(self, data): self.parent.live_filtered_image = _image if not first_update: - _histo_widget.setLevels(self.parent.filtered_histogram_level[0], self.parent.filtered_histogram_level[1]) + _histo_widget.setLevels( + self.parent.filtered_histogram_level[0], + self.parent.filtered_histogram_level[1], + ) # histogram self.parent.ui.filtered_histogram_plot.clear() min = 0 max = np.max(_image) - y, x = np.histogram(_image, bins=np.linspace(min, max + 1, self.parent.nbr_histo_bins)) - self.parent.ui.filtered_histogram_plot.plot(x, y, stepMode=True, fillLevel=0, brush=(0, 0, 255, 150)) + y, x = np.histogram( + _image, bins=np.linspace(min, max + 1, self.parent.nbr_histo_bins) + ) + self.parent.ui.filtered_histogram_plot.plot( + x, y, stepMode=True, fillLevel=0, brush=(0, 0, 255, 150) + ) # re-attaching the x and y axis self.parent.ui.raw_image_view.view.getViewBox().setYLink("filtered_image") diff --git a/notebooks/__code/outliers_filtering/event_handler.py b/notebooks/__code/outliers_filtering/event_handler.py index a09d8acf8..a2553cb6d 100755 --- a/notebooks/__code/outliers_filtering/event_handler.py +++ b/notebooks/__code/outliers_filtering/event_handler.py @@ -11,7 +11,9 @@ def __init__(self, parent=None): self.parent = parent def algorithm_changed(self): - high_intensity_status = self.parent.ui.fix_high_intensity_counts_checkBox.isChecked() + high_intensity_status = ( + self.parent.ui.fix_high_intensity_counts_checkBox.isChecked() + ) self.parent.ui.median_thresholding_frame.setEnabled(high_intensity_status) self.reset_table_infos() self.table_selection_changed() @@ -28,13 +30,18 @@ def table_selection_changed(self): row_selected = o_table.get_row_selected() short_file_name = self.parent.list_short_file_name[row_selected] if self.parent.data.get(short_file_name, None) is None: - self.parent.data[short_file_name] = {"raw": self.load_raw_data(row=row_selected), "filtered": None} + self.parent.data[short_file_name] = { + "raw": self.load_raw_data(row=row_selected), + "filtered": None, + } if self.parent.image_size is None: [height, width] = np.shape(self.parent.data[short_file_name]["raw"]) self.parent.image_size = [height, width] - filtered_data = self.calculate_filtered_data(raw_data=self.parent.data[short_file_name]["raw"]) + filtered_data = self.calculate_filtered_data( + raw_data=self.parent.data[short_file_name]["raw"] + ) o_display = Display(parent=self.parent) self.parent.data[short_file_name]["filtered"] = filtered_data @@ -63,18 +70,34 @@ def calculate_filtered_data(self, raw_data=None): high_counts_stats = o_algo.get_high_counts_stats() o_table.insert_item( - row=row_selected, column=1, editable=False, value=high_counts_stats["number"], format_str="{:d}" + row=row_selected, + column=1, + editable=False, + value=high_counts_stats["number"], + format_str="{:d}", ) o_table.insert_item( - row=row_selected, column=2, editable=False, value=high_counts_stats["percentage"], format_str="{:.2f}" + row=row_selected, + column=2, + editable=False, + value=high_counts_stats["percentage"], + format_str="{:.2f}", ) dead_pixels_stats = o_algo.get_dead_pixels_stats() o_table.insert_item( - row=row_selected, column=3, editable=False, value=dead_pixels_stats["number"], format_str="{:d}" + row=row_selected, + column=3, + editable=False, + value=dead_pixels_stats["number"], + format_str="{:d}", ) o_table.insert_item( - row=row_selected, column=4, editable=False, value=dead_pixels_stats["percentage"], format_str="{:.2f}" + row=row_selected, + column=4, + editable=False, + value=dead_pixels_stats["percentage"], + format_str="{:.2f}", ) return o_algo.get_processed_data() diff --git a/notebooks/__code/outliers_filtering/export.py b/notebooks/__code/outliers_filtering/export.py index 401147a34..468eee336 100755 --- a/notebooks/__code/outliers_filtering/export.py +++ b/notebooks/__code/outliers_filtering/export.py @@ -21,7 +21,9 @@ def export(self): ) if _export_folder: - export_folder_name = os.path.join(_export_folder, str(base_folder.name) + "_outliers_corrected") + export_folder_name = os.path.join( + _export_folder, str(base_folder.name) + "_outliers_corrected" + ) export_folder_name = make_or_increment_folder_name(export_folder_name) list_file = self.parent.list_files o_event = EventHandler(parent=self.parent) @@ -31,7 +33,9 @@ def export(self): self.parent.eventProgress.setVisible(True) for _row, _file in enumerate(list_file): o_norm = o_event.load_data_object(file_name=_file) - o_algo = Algorithm(parent=self.parent, data=np.squeeze(o_norm.data["sample"]["data"])) + o_algo = Algorithm( + parent=self.parent, data=np.squeeze(o_norm.data["sample"]["data"]) + ) o_algo.run() data_corrected = o_algo.get_processed_data() o_norm.data["sample"]["data"][0] = data_corrected diff --git a/notebooks/__code/outliers_filtering/initialization.py b/notebooks/__code/outliers_filtering/initialization.py index d56361daf..f881e651f 100755 --- a/notebooks/__code/outliers_filtering/initialization.py +++ b/notebooks/__code/outliers_filtering/initialization.py @@ -36,7 +36,9 @@ def pyqtgraph(self): area.moveDock(d2, "above", d2h) # raw image - self.parent.ui.raw_image_view = pg.ImageView(view=pg.PlotItem(), name="raw_image") + self.parent.ui.raw_image_view = pg.ImageView( + view=pg.PlotItem(), name="raw_image" + ) self.parent.ui.raw_image_view.ui.roiBtn.hide() self.parent.ui.raw_image_view.ui.menuBtn.hide() self.parent.ui.raw_image_view.view.setAutoVisible(y=True) @@ -58,13 +60,19 @@ def pyqtgraph(self): d1h.addWidget(self.parent.ui.raw_histogram_plot) # filtered image - self.parent.ui.filtered_image_view = pg.ImageView(view=pg.PlotItem(), name="filtered_image") + self.parent.ui.filtered_image_view = pg.ImageView( + view=pg.PlotItem(), name="filtered_image" + ) self.parent.ui.filtered_image_view.ui.roiBtn.hide() self.parent.ui.filtered_image_view.ui.menuBtn.hide() self.parent.filtered_vLine = pg.InfiniteLine(angle=90, movable=False) self.parent.filtered_hLine = pg.InfiniteLine(angle=0, movable=False) - self.parent.ui.filtered_image_view.addItem(self.parent.filtered_vLine, ignoreBounds=True) - self.parent.ui.filtered_image_view.addItem(self.parent.filtered_hLine, ignoreBounds=True) + self.parent.ui.filtered_image_view.addItem( + self.parent.filtered_vLine, ignoreBounds=True + ) + self.parent.ui.filtered_image_view.addItem( + self.parent.filtered_hLine, ignoreBounds=True + ) self.parent.filtered_vLine.setPos([1000, 1000]) self.parent.filtered_hLine.setPos([1000, 1000]) self.parent.filtered_proxy = pg.SignalProxy( diff --git a/notebooks/__code/outliers_filtering/main.py b/notebooks/__code/outliers_filtering/main.py index 8f8c7ac0e..dd00d6459 100755 --- a/notebooks/__code/outliers_filtering/main.py +++ b/notebooks/__code/outliers_filtering/main.py @@ -13,7 +13,9 @@ class InterfaceHandler(FileFolderBrowser): def __init__(self, working_dir=""): - super(InterfaceHandler, self).__init__(working_dir=working_dir, next_function=self.display_status) + super(InterfaceHandler, self).__init__( + working_dir=working_dir, next_function=self.display_status + ) def get_list_of_files(self): return self.list_images_ui.selected @@ -23,7 +25,13 @@ def select_all_images(self): def display_status(self, list_of_files): nbr_images = str(len(list_of_files)) - display(HTML('You have selected ' + nbr_images + " images ")) + display( + HTML( + 'You have selected ' + + nbr_images + + " images " + ) + ) class Interface(QMainWindow): @@ -135,7 +143,9 @@ def file_index_changed(self): def help_clicked(self): import webbrowser - webbrowser.open("https://neutronimaging.ornl.gov/tutorials/imaging-notebooks/outliers-filtering-tool/") + webbrowser.open( + "https://neutronimaging.ornl.gov/tutorials/imaging-notebooks/outliers-filtering-tool/" + ) def display_image(self, image): self.ui.image_view.setImage(image) diff --git a/notebooks/__code/overlay_images/event_handler.py b/notebooks/__code/overlay_images/event_handler.py index e4a882168..2a7634bc9 100755 --- a/notebooks/__code/overlay_images/event_handler.py +++ b/notebooks/__code/overlay_images/event_handler.py @@ -15,11 +15,13 @@ def __init__(self, parent=None): def update_views(self, row_selected=0): self.update_view( - image_resolution="high_res", data=self.parent.o_norm_high_res.data["sample"]["data"][row_selected] + image_resolution="high_res", + data=self.parent.o_norm_high_res.data["sample"]["data"][row_selected], ) self.update_view( - image_resolution="low_res", data=self.parent.o_norm_low_res.data["sample"]["data"][row_selected] + image_resolution="low_res", + data=self.parent.o_norm_low_res.data["sample"]["data"][row_selected], ) if self.parent.resize_and_overlay_images: @@ -147,7 +149,9 @@ def update_profile_markers_and_target(self, with_profile=False): line_view_binning = pg.GraphItem() image_view.addItem(line_view_binning) - line_view_binning.setData(pos=pos, adj=adj, pen=lines, symbol=None, pxMode=False) + line_view_binning.setData( + pos=pos, adj=adj, pen=lines, symbol=None, pxMode=False + ) self.parent.markers["overlay"]["1"]["target_ui"] = line_view_binning else: if self.parent.markers["overlay"]["1"]["target_ui"]: @@ -158,7 +162,9 @@ def update_target(self, image_resolution="high_res", target_index="1"): image_view = self.parent.image_view[image_resolution] if self.parent.markers[image_resolution][target_index]["target_ui"] is not None: - image_view.removeItem(self.parent.markers[image_resolution][target_index]["target_ui"]) + image_view.removeItem( + self.parent.markers[image_resolution][target_index]["target_ui"] + ) width = self.parent.markers["width"] height = self.parent.markers["height"] @@ -193,12 +199,22 @@ def update_target(self, image_resolution="high_res", target_index="1"): line_color = self.parent.markers["target"]["color"][target_index] lines = np.array( [line_color for _ in np.arange(len(pos))], - dtype=[("red", np.ubyte), ("green", np.ubyte), ("blue", np.ubyte), ("alpha", np.ubyte), ("width", float)], + dtype=[ + ("red", np.ubyte), + ("green", np.ubyte), + ("blue", np.ubyte), + ("alpha", np.ubyte), + ("width", float), + ], ) line_view_binning = pg.GraphItem() image_view.addItem(line_view_binning) - line_view_binning.setData(pos=pos, adj=adj, pen=lines, symbol=None, pxMode=False) - self.parent.markers[image_resolution][target_index]["target_ui"] = line_view_binning + line_view_binning.setData( + pos=pos, adj=adj, pen=lines, symbol=None, pxMode=False + ) + self.parent.markers[image_resolution][target_index]["target_ui"] = ( + line_view_binning + ) def get_marker_index_parameters(self, region_index="1"): region = { @@ -232,11 +248,15 @@ def overlay_stack_of_images_clicked(self): scaling_factor = distance_h / distance_l self.parent.ui.scaling_factor_lineEdit.setText(f"{scaling_factor:.2f}") - [image_height, image_width] = np.shape(self.parent.o_norm_low_res.data["sample"]["data"][0]) + [image_height, image_width] = np.shape( + self.parent.o_norm_low_res.data["sample"]["data"][0] + ) new_image_height = int(image_height * scaling_factor) new_image_width = int(image_width * scaling_factor) - self.parent.eventProgress.setMaximum(len(self.parent.o_norm_high_res.data["sample"]["data"])) + self.parent.eventProgress.setMaximum( + len(self.parent.o_norm_high_res.data["sample"]["data"]) + ) self.parent.eventProgress.setValue(0) self.parent.eventProgress.setVisible(True) QtGui.QGuiApplication.processEvents() @@ -265,8 +285,14 @@ def overlay_stack_of_images_clicked(self): resize_hres_images = [] resize_lres_images = [] - for _row, _low_res_image in enumerate(self.parent.o_norm_low_res.data["sample"]["data"]): - new_image = np.array(Image.fromarray(_low_res_image).resize((new_image_width, new_image_height))) + for _row, _low_res_image in enumerate( + self.parent.o_norm_low_res.data["sample"]["data"] + ): + new_image = np.array( + Image.fromarray(_low_res_image).resize( + (new_image_width, new_image_height) + ) + ) resize_lres_images.append(copy.deepcopy(new_image)) high_res_image = self.get_full_high_res_image( @@ -281,14 +307,18 @@ def overlay_stack_of_images_clicked(self): resize_hres_images.append(high_res_image) if _row == 0: - self.parent.rescaled_low_res_height, self.parent.rescaled_low_res_width = np.shape(new_image) + ( + self.parent.rescaled_low_res_height, + self.parent.rescaled_low_res_width, + ) = np.shape(new_image) resize_and_overlay_modes.append("Auto") o_table.set_item_with_str(row=_row, column=2, cell_str="Auto") # add high resolution image new_working_image = copy.deepcopy(new_image) new_working_image[ - y_index_array_resized_array : y_index_array_resized_array + image_height, + y_index_array_resized_array : y_index_array_resized_array + + image_height, x_index_array_resized_array : x_index_array_resized_array + image_width, ] = high_res_images[_row] @@ -296,7 +326,10 @@ def overlay_stack_of_images_clicked(self): self.parent.eventProgress.setValue(_row + 1) QtGui.QGuiApplication.processEvents() - self.parent.resize_hres_lres_images = {"lres": resize_lres_images, "hres": resize_hres_images} + self.parent.resize_hres_lres_images = { + "lres": resize_lres_images, + "hres": resize_hres_images, + } self.parent.resize_and_overlay_images = resize_and_overlay_images self.parent.resize_and_overlay_modes = resize_and_overlay_modes @@ -321,19 +354,27 @@ def manual_overlay_of_selected_image_only(self): self.parent.resize_and_overlay_modes[row_selected] = "Manual" o_table.set_item_with_str(row=row_selected, column=2, cell_str="Manual") - [image_height, image_width] = np.shape(self.parent.o_norm_low_res.data["sample"]["data"][0]) + [image_height, image_width] = np.shape( + self.parent.o_norm_low_res.data["sample"]["data"][0] + ) new_image_height = int(image_height * scaling_factor) new_image_width = int(image_width * scaling_factor) x_index_array_resized_array = int(str(self.parent.ui.xoffset_lineEdit.text())) y_index_array_resized_array = int(str(self.parent.ui.yoffset_lineEdit.text())) resize_and_overlay_images = self.parent.resize_and_overlay_images - _high_res_image = self.parent.o_norm_high_res.data["sample"]["data"][row_selected] + _high_res_image = self.parent.o_norm_high_res.data["sample"]["data"][ + row_selected + ] _low_res_image = self.parent.o_norm_low_res.data["sample"]["data"][row_selected] - new_image = np.array(Image.fromarray(_low_res_image).resize((new_image_width, new_image_height))) + new_image = np.array( + Image.fromarray(_low_res_image).resize((new_image_width, new_image_height)) + ) # self.parent.rescaled_low_res_height, self.parent.rescaled_low_res_width = np.shape(new_image) - self.parent.resize_hres_lres_images["hres"][row_selected] = copy.deepcopy(new_image) + self.parent.resize_hres_lres_images["hres"][row_selected] = copy.deepcopy( + new_image + ) high_res_image = self.get_full_high_res_image( _high_res_image, image_height, @@ -377,11 +418,15 @@ def get_full_high_res_image( def manual_overlay_stack_of_images_clicked(self): scaling_factor = float(str(self.parent.ui.scaling_factor_lineEdit.text())) - [image_height, image_width] = np.shape(self.parent.o_norm_low_res.data["sample"]["data"][0]) + [image_height, image_width] = np.shape( + self.parent.o_norm_low_res.data["sample"]["data"][0] + ) new_image_height = int(image_height * scaling_factor) new_image_width = int(image_width * scaling_factor) - self.parent.eventProgress.setMaximum(len(self.parent.o_norm_high_res.data["sample"]["data"])) + self.parent.eventProgress.setMaximum( + len(self.parent.o_norm_high_res.data["sample"]["data"]) + ) self.parent.eventProgress.setValue(0) self.parent.eventProgress.setVisible(True) QtGui.QGuiApplication.processEvents() @@ -397,12 +442,21 @@ def manual_overlay_stack_of_images_clicked(self): resize_lres_images = [] high_res_images = self.parent.o_norm_high_res.data["sample"]["data"] - for _row, _low_res_image in enumerate(self.parent.o_norm_low_res.data["sample"]["data"]): - new_image = np.array(Image.fromarray(_low_res_image).resize((new_image_width, new_image_height))) + for _row, _low_res_image in enumerate( + self.parent.o_norm_low_res.data["sample"]["data"] + ): + new_image = np.array( + Image.fromarray(_low_res_image).resize( + (new_image_width, new_image_height) + ) + ) resize_lres_images.append(copy.deepcopy(new_image)) if _row == 0: - self.parent.rescaled_low_res_height, self.parent.rescaled_low_res_width = np.shape(new_image) + ( + self.parent.rescaled_low_res_height, + self.parent.rescaled_low_res_width, + ) = np.shape(new_image) resize_and_overlay_modes.append("Manual") o_table.set_item_with_str(row=_row, column=2, cell_str="Manual") @@ -420,7 +474,8 @@ def manual_overlay_stack_of_images_clicked(self): # add high resolution image new_working_image = copy.deepcopy(new_image) new_working_image[ - y_index_array_resized_array : y_index_array_resized_array + image_height, + y_index_array_resized_array : y_index_array_resized_array + + image_height, x_index_array_resized_array : x_index_array_resized_array + image_width, ] = high_res_images[_row] resize_and_overlay_images.append(new_working_image) @@ -430,7 +485,10 @@ def manual_overlay_stack_of_images_clicked(self): self.parent.resize_and_overlay_images = resize_and_overlay_images self.parent.resize_and_overlay_modes = resize_and_overlay_modes - self.parent.resize_hres_lres_images = {"lres": resize_lres_images, "hres": resize_hres_images} + self.parent.resize_hres_lres_images = { + "lres": resize_lres_images, + "hres": resize_hres_images, + } row_selected = o_table.get_row_selected() @@ -458,15 +516,21 @@ def check_xoffset_manual_button_status(self): status_minus_minus_button = False elif xoffset_value < self.parent.DOUBLE_OFFSET: status_minus_minus_button = False - elif xoffset_value == (self.parent.rescaled_low_res_width - self.parent.high_res_image_width): + elif xoffset_value == ( + self.parent.rescaled_low_res_width - self.parent.high_res_image_width + ): status_plus_button = False status_plus_plus_button = False elif xoffset_value > ( - self.parent.rescaled_low_res_width - self.parent.high_res_image_width - self.parent.DOUBLE_OFFSET + self.parent.rescaled_low_res_width + - self.parent.high_res_image_width + - self.parent.DOUBLE_OFFSET ): status_plus_plus_button = False - self.parent.ui.xoffset_minus_minus_pushButton.setEnabled(status_minus_minus_button) + self.parent.ui.xoffset_minus_minus_pushButton.setEnabled( + status_minus_minus_button + ) self.parent.ui.xoffset_minus_pushButton.setEnabled(status_minus_button) self.parent.ui.xoffset_plus_pushButton.setEnabled(status_plus_button) self.parent.ui.xoffset_plus_plus_pushButton.setEnabled(status_plus_plus_button) @@ -483,15 +547,21 @@ def check_yoffset_manual_button_status(self): status_minus_minus_button = False elif yoffset_value < self.parent.DOUBLE_OFFSET: status_minus_minus_button = False - elif yoffset_value == (self.parent.rescaled_low_res_height - self.parent.high_res_image_height): + elif yoffset_value == ( + self.parent.rescaled_low_res_height - self.parent.high_res_image_height + ): status_plus_button = False status_plus_plus_button = False elif yoffset_value > ( - self.parent.rescaled_low_res_height - self.parent.high_res_image_height - self.parent.DOUBLE_OFFSET + self.parent.rescaled_low_res_height + - self.parent.high_res_image_height + - self.parent.DOUBLE_OFFSET ): status_plus_plus_button = False - self.parent.ui.yoffset_minus_minus_pushButton.setEnabled(status_minus_minus_button) + self.parent.ui.yoffset_minus_minus_pushButton.setEnabled( + status_minus_minus_button + ) self.parent.ui.yoffset_minus_pushButton.setEnabled(status_minus_button) self.parent.ui.yoffset_plus_pushButton.setEnabled(status_plus_button) self.parent.ui.yoffset_plus_plus_pushButton.setEnabled(status_plus_plus_button) @@ -502,7 +572,9 @@ def update_profile_plots(self): return o_get = Get(parent=self.parent) - overlay_1_dict = o_get.marker_location(image_resolution="overlay", target_index="1") + overlay_1_dict = o_get.marker_location( + image_resolution="overlay", target_index="1" + ) width = self.parent.markers["width"] height = self.parent.markers["height"] @@ -546,7 +618,9 @@ def update_profile_plots(self): self.parent.horizontal_profile_plot.axes.plot( x_axis, horizontal_profile_high_res, "-b", label="high resolution" ) - self.parent.horizontal_profile_plot.axes.plot(x_axis, horizontal_profile_low_res, "--b", label="low resolution") + self.parent.horizontal_profile_plot.axes.plot( + x_axis, horizontal_profile_low_res, "--b", label="low resolution" + ) self.parent.horizontal_profile_plot.axes.legend() self.parent.horizontal_profile_plot.draw() @@ -559,8 +633,12 @@ def update_profile_plots(self): self.parent.vertical_profile_plot.axes.clear() self.parent.vertical_profile_plot.draw() - self.parent.vertical_profile_plot.axes.plot(y_axis, vertical_profile_high_res, "-r", label="high resolution") - self.parent.vertical_profile_plot.axes.plot(y_axis, vertical_profile_low_res, "--r", label="low resolution") + self.parent.vertical_profile_plot.axes.plot( + y_axis, vertical_profile_high_res, "-r", label="high resolution" + ) + self.parent.vertical_profile_plot.axes.plot( + y_axis, vertical_profile_low_res, "--r", label="low resolution" + ) self.parent.vertical_profile_plot.axes.legend() self.parent.vertical_profile_plot.draw() @@ -578,7 +656,11 @@ def save_overlay_parameters(self): sf = str(self.parent.ui.scaling_factor_lineEdit.text()) xoffset = str(self.parent.ui.xoffset_lineEdit.text()) yoffset = str(self.parent.ui.yoffset_lineEdit.text()) - self.parent.parameters_used_on_all_images = {"scaling_factor": sf, "xoffset": xoffset, "yoffset": yoffset} + self.parent.parameters_used_on_all_images = { + "scaling_factor": sf, + "xoffset": xoffset, + "yoffset": yoffset, + } self.parent.ui.export_pushButton.setEnabled(True) def check_export_button_status(self): diff --git a/notebooks/__code/overlay_images/export.py b/notebooks/__code/overlay_images/export.py index 572c14a5e..e601b917c 100755 --- a/notebooks/__code/overlay_images/export.py +++ b/notebooks/__code/overlay_images/export.py @@ -13,12 +13,18 @@ def __init__(self, parent=None): def run(self): working_dir = os.path.abspath(os.path.dirname(self.parent.working_dir)) - export_folder = QFileDialog.getExistingDirectory(self.parent, caption="Select folder", directory=working_dir) + export_folder = QFileDialog.getExistingDirectory( + self.parent, caption="Select folder", directory=working_dir + ) if export_folder: # make own folder where the data will be exported - short_high_res_input_folder = os.path.basename(self.parent.high_res_input_folder) - short_low_res_input_folder = os.path.basename(self.parent.low_res_input_folder) + short_high_res_input_folder = os.path.basename( + self.parent.high_res_input_folder + ) + short_low_res_input_folder = os.path.basename( + self.parent.low_res_input_folder + ) output_folder = f"{short_low_res_input_folder}_and_{short_high_res_input_folder}_overlaid" full_output_folder = os.path.join(export_folder, output_folder) make_or_reset_folder(full_output_folder) diff --git a/notebooks/__code/overlay_images/initialization.py b/notebooks/__code/overlay_images/initialization.py index 991730431..4ff6420b8 100755 --- a/notebooks/__code/overlay_images/initialization.py +++ b/notebooks/__code/overlay_images/initialization.py @@ -20,9 +20,13 @@ def __init__(self, parent=None): def dictionaries(self): list_high_res_files = self.parent.o_norm_high_res.data["sample"]["file_name"] - list_high_res_files_basename = [os.path.basename(_file) for _file in list_high_res_files] + list_high_res_files_basename = [ + os.path.basename(_file) for _file in list_high_res_files + ] list_low_res_files = self.parent.o_norm_low_res.data["sample"]["file_name"] - list_low_res_files_basename = [os.path.basename(_file) for _file in list_low_res_files] + list_low_res_files_basename = [ + os.path.basename(_file) for _file in list_low_res_files + ] dict_offsets = OrderedDict() for _index, _filename in enumerate(list_high_res_files_basename): @@ -42,8 +46,12 @@ def widgets(self): list_high_res_files = self.parent.o_norm_high_res.data["sample"]["file_name"] list_low_res_files = self.parent.o_norm_low_res.data["sample"]["file_name"] - list_high_res_files_basename = [os.path.basename(_file) for _file in list_high_res_files] - list_low_res_files_basename = [os.path.basename(_file) for _file in list_low_res_files] + list_high_res_files_basename = [ + os.path.basename(_file) for _file in list_high_res_files + ] + list_low_res_files_basename = [ + os.path.basename(_file) for _file in list_low_res_files + ] resize_and_overlay_modes = [] @@ -53,7 +61,9 @@ def widgets(self): list_high_res_files_basename, list_low_res_files_basename, strict=False ): o_table.insert_empty_row(row=_row) - o_table.insert_item(row=_row, column=0, value=_high_res_file, editable=False) + o_table.insert_item( + row=_row, column=0, value=_high_res_file, editable=False + ) o_table.insert_item(row=_row, column=1, value=_low_res_file, editable=False) o_table.insert_item(row=_row, column=2, value="None") resize_and_overlay_modes.append("None") @@ -116,7 +126,14 @@ def pyqtgraph(self): image_layout.addWidget(self.parent.image_view["overlay"]) self.parent.ui.overlay_widget.setLayout(image_layout) - def _design_marker(self, image_resolution=None, target_index=None, pen=None, image_view=None, method=None): + def _design_marker( + self, + image_resolution=None, + target_index=None, + pen=None, + image_view=None, + method=None, + ): x = self.parent.markers[image_resolution][target_index]["x"] y = self.parent.markers[image_resolution][target_index]["y"] image_view = self.parent.image_view[image_resolution] @@ -139,19 +156,31 @@ def markers(self): blue_pen.setWidthF(0.05) self.parent.markers["high_res"]["1"]["ui"] = self._design_marker( - image_resolution="high_res", target_index="1", pen=red_pen, method=self.parent.markers_changed + image_resolution="high_res", + target_index="1", + pen=red_pen, + method=self.parent.markers_changed, ) self.parent.markers["high_res"]["2"]["ui"] = self._design_marker( - image_resolution="high_res", target_index="2", pen=blue_pen, method=self.parent.markers_changed + image_resolution="high_res", + target_index="2", + pen=blue_pen, + method=self.parent.markers_changed, ) self.parent.markers["low_res"]["1"]["ui"] = self._design_marker( - image_resolution="low_res", target_index="1", pen=red_pen, method=self.parent.markers_changed + image_resolution="low_res", + target_index="1", + pen=red_pen, + method=self.parent.markers_changed, ) self.parent.markers["low_res"]["2"]["ui"] = self._design_marker( - image_resolution="low_res", target_index="2", pen=blue_pen, method=self.parent.markers_changed + image_resolution="low_res", + target_index="2", + pen=blue_pen, + method=self.parent.markers_changed, ) o_event = EventHandler(parent=self.parent) diff --git a/notebooks/__code/overlay_images/interface_handler.py b/notebooks/__code/overlay_images/interface_handler.py index 742b952fb..9deccdf89 100755 --- a/notebooks/__code/overlay_images/interface_handler.py +++ b/notebooks/__code/overlay_images/interface_handler.py @@ -13,9 +13,15 @@ class InterfaceHandler: def __init__(self, working_dir=None, o_norm_high_res=None, o_norm_low_res=None): - assert len(o_norm_low_res.data["sample"]["file_name"]) == len(o_norm_high_res.data["sample"]["file_name"]) + assert len(o_norm_low_res.data["sample"]["file_name"]) == len( + o_norm_high_res.data["sample"]["file_name"] + ) - o_interface = Interface(o_norm_high_res=o_norm_high_res, o_norm_low_res=o_norm_low_res, working_dir=working_dir) + o_interface = Interface( + o_norm_high_res=o_norm_high_res, + o_norm_low_res=o_norm_low_res, + working_dir=working_dir, + ) o_interface.show() self.o_interface = o_interface @@ -77,23 +83,34 @@ class Interface(QMainWindow): # if any of the current parameter is different from this, the EXPORT button becomes unavailable parameters_used_on_all_images = {"scaling_factor": 0, "xoffset": 0, "yoffset": 0} - def __init__(self, parent=None, o_norm_high_res=None, o_norm_low_res=None, working_dir=None): + def __init__( + self, parent=None, o_norm_high_res=None, o_norm_low_res=None, working_dir=None + ): self.o_norm_high_res = o_norm_high_res self.o_norm_low_res = o_norm_low_res self.working_dir = working_dir if working_dir else "./" - self.high_res_image_height, self.high_res_image_width = np.shape(o_norm_high_res.data["sample"]["data"][0]) - self.low_res_image_height, self.low_res_image_width = np.shape(o_norm_low_res.data["sample"]["data"][0]) + self.high_res_image_height, self.high_res_image_width = np.shape( + o_norm_high_res.data["sample"]["data"][0] + ) + self.low_res_image_height, self.low_res_image_width = np.shape( + o_norm_low_res.data["sample"]["data"][0] + ) self.rescaled_low_res_height, self.rescaled_low_res_width = None, None self.list_of_high_res_filename = o_norm_high_res.data["sample"]["file_name"] - self.high_res_input_folder = os.path.dirname(o_norm_high_res.data["sample"]["file_name"][0]) - self.low_res_input_folder = os.path.dirname(o_norm_low_res.data["sample"]["file_name"][0]) + self.high_res_input_folder = os.path.dirname( + o_norm_high_res.data["sample"]["file_name"][0] + ) + self.low_res_input_folder = os.path.dirname( + o_norm_low_res.data["sample"]["file_name"][0] + ) super(Interface, self).__init__(parent) ui_full_path = os.path.join( - os.path.dirname(os.path.dirname(os.path.dirname(__file__))), os.path.join("ui", "ui_overlay.ui") + os.path.dirname(os.path.dirname(os.path.dirname(__file__))), + os.path.join("ui", "ui_overlay.ui"), ) self.ui = load_ui(ui_full_path, baseinstance=self) self.setWindowTitle("Overlay images with scaling") @@ -126,19 +143,27 @@ def list_files_table_selection_changed(self): def markers_changed(self): o_get = Get(parent=self) - high_res_1_dict = o_get.marker_location(image_resolution="high_res", target_index="1") + high_res_1_dict = o_get.marker_location( + image_resolution="high_res", target_index="1" + ) self.markers["high_res"]["1"]["x"] = high_res_1_dict["x"] self.markers["high_res"]["1"]["y"] = high_res_1_dict["y"] - high_res_2_dict = o_get.marker_location(image_resolution="high_res", target_index="2") + high_res_2_dict = o_get.marker_location( + image_resolution="high_res", target_index="2" + ) self.markers["high_res"]["2"]["x"] = high_res_2_dict["x"] self.markers["high_res"]["2"]["y"] = high_res_2_dict["y"] - low_res_1_dict = o_get.marker_location(image_resolution="low_res", target_index="1") + low_res_1_dict = o_get.marker_location( + image_resolution="low_res", target_index="1" + ) self.markers["low_res"]["1"]["x"] = low_res_1_dict["x"] self.markers["low_res"]["1"]["y"] = low_res_1_dict["y"] - low_res_2_dict = o_get.marker_location(image_resolution="low_res", target_index="2") + low_res_2_dict = o_get.marker_location( + image_resolution="low_res", target_index="2" + ) self.markers["low_res"]["2"]["x"] = low_res_2_dict["x"] self.markers["low_res"]["2"]["y"] = low_res_2_dict["y"] @@ -268,7 +293,9 @@ def profile_tool_clicked(self): def profile_region_moved(self): o_get = Get(parent=self) - overlay_1_dict = o_get.marker_location(image_resolution="overlay", target_index="1") + overlay_1_dict = o_get.marker_location( + image_resolution="overlay", target_index="1" + ) self.markers["overlay"]["1"]["x"] = overlay_1_dict["x"] self.markers["overlay"]["1"]["y"] = overlay_1_dict["y"] diff --git a/notebooks/__code/overlay_images/overlay_images.py b/notebooks/__code/overlay_images/overlay_images.py index 7adfac768..e65af1f22 100755 --- a/notebooks/__code/overlay_images/overlay_images.py +++ b/notebooks/__code/overlay_images/overlay_images.py @@ -18,7 +18,7 @@ def __init__(self, working_dir=""): def select_input_folder(self, data_type="high resolution"): self.current_data_type = data_type list_folder_widget = fileselector.FileSelectorPanel( - instruction=f"select folder containing {data_type} " "images!", + instruction=f"select folder containing {data_type} images!", start_dir=self.working_dir, type="directory", next=self.folder_selected, diff --git a/notebooks/__code/panoramic_stitching/automatically_stitch.py b/notebooks/__code/panoramic_stitching/automatically_stitch.py index 50e6faedd..ac9b70498 100755 --- a/notebooks/__code/panoramic_stitching/automatically_stitch.py +++ b/notebooks/__code/panoramic_stitching/automatically_stitch.py @@ -21,7 +21,9 @@ def run(self): group_selected = o_get.get_combobox_folder_selected() # first calculate the long and lift position versus pixel coefficient from the ref. group - group_reference_offset_dictionary = self.parent.offset_dictionary[group_selected] + group_reference_offset_dictionary = self.parent.offset_dictionary[ + group_selected + ] group_reference_data_dictionary = self.parent.data_dictionary[group_selected] list_files = group_reference_offset_dictionary.keys() @@ -29,8 +31,12 @@ def run(self): list_pixel_vs_motor_lift_axis_value = [] for _file_index, _file in enumerate(list_files): - long_axis_value = group_reference_data_dictionary[_file].metadata["MotLongAxis.RBV"] - lift_axis_value = group_reference_data_dictionary[_file].metadata["MotLiftTable.RBV"] + long_axis_value = group_reference_data_dictionary[_file].metadata[ + "MotLongAxis.RBV" + ] + lift_axis_value = group_reference_data_dictionary[_file].metadata[ + "MotLiftTable.RBV" + ] if _file_index == 0: long_axis_reference_value = long_axis_value @@ -75,15 +81,29 @@ def run(self): # get xoffset and yofffset pixel/motor position of each image of reference group for _file_index, _file in enumerate(list_files): if _file_index == 0: - long_axis_value_image_reference = data_dictionary[_file].metadata["MotLongAxis.RBV"] - lift_axis_value_image_reference = data_dictionary[_file].metadata["MotLiftTable.RBV"] + long_axis_value_image_reference = data_dictionary[_file].metadata[ + "MotLongAxis.RBV" + ] + lift_axis_value_image_reference = data_dictionary[_file].metadata[ + "MotLiftTable.RBV" + ] continue - long_axis_value = data_dictionary[_file].metadata["MotLongAxis.RBV"] - long_axis_value_image_reference - lift_axis_value = data_dictionary[_file].metadata["MotLiftTable.RBV"] - lift_axis_value_image_reference - - xoffset_of_this_file = int(long_axis_value * list_pixel_vs_motor_long_axis_value[_file_index]) - yoffset_of_this_file = int(lift_axis_value * list_pixel_vs_motor_lift_axis_value[_file_index]) + long_axis_value = ( + data_dictionary[_file].metadata["MotLongAxis.RBV"] + - long_axis_value_image_reference + ) + lift_axis_value = ( + data_dictionary[_file].metadata["MotLiftTable.RBV"] + - lift_axis_value_image_reference + ) + + xoffset_of_this_file = int( + long_axis_value * list_pixel_vs_motor_long_axis_value[_file_index] + ) + yoffset_of_this_file = int( + lift_axis_value * list_pixel_vs_motor_lift_axis_value[_file_index] + ) group_offset_dictionary[_file]["xoffset"] = xoffset_of_this_file group_offset_dictionary[_file]["yoffset"] = yoffset_of_this_file diff --git a/notebooks/__code/panoramic_stitching/config_buttons.py b/notebooks/__code/panoramic_stitching/config_buttons.py index db128a6ae..4344da715 100755 --- a/notebooks/__code/panoramic_stitching/config_buttons.py +++ b/notebooks/__code/panoramic_stitching/config_buttons.py @@ -1,13 +1,40 @@ button = { - "left": {"pressed": "left_arrow_v2_pressed.png", "released": "left_arrow_v2_released.png"}, - "right": {"pressed": "right_arrow_v2_pressed.png", "released": "right_arrow_v2_released.png"}, - "left_left": {"pressed": "left_left_arrow_v2_pressed.png", "released": "left_left_arrow_v2_released.png"}, - "right_right": {"pressed": "right_right_arrow_v2_pressed.png", "released": "right_right_arrow_v2_released.png"}, - "up": {"pressed": "up_arrow_v2_pressed.png", "released": "up_arrow_v2_released.png"}, - "down": {"pressed": "down_arrow_v2_pressed.png", "released": "down_arrow_v2_released.png"}, - "up_up": {"pressed": "up_up_arrow_v2_pressed.png", "released": "up_up_arrow_v2_released.png"}, - "down_down": {"pressed": "down_down_arrow_v2_pressed.png", "released": "down_down_arrow_v2_released.png"}, - "remote_control": {"pressed": "remote_control_pressed.png", "released": "remote_control_released.png"}, + "left": { + "pressed": "left_arrow_v2_pressed.png", + "released": "left_arrow_v2_released.png", + }, + "right": { + "pressed": "right_arrow_v2_pressed.png", + "released": "right_arrow_v2_released.png", + }, + "left_left": { + "pressed": "left_left_arrow_v2_pressed.png", + "released": "left_left_arrow_v2_released.png", + }, + "right_right": { + "pressed": "right_right_arrow_v2_pressed.png", + "released": "right_right_arrow_v2_released.png", + }, + "up": { + "pressed": "up_arrow_v2_pressed.png", + "released": "up_arrow_v2_released.png", + }, + "down": { + "pressed": "down_arrow_v2_pressed.png", + "released": "down_arrow_v2_released.png", + }, + "up_up": { + "pressed": "up_up_arrow_v2_pressed.png", + "released": "up_up_arrow_v2_released.png", + }, + "down_down": { + "pressed": "down_down_arrow_v2_pressed.png", + "released": "down_down_arrow_v2_released.png", + }, + "remote_control": { + "pressed": "remote_control_pressed.png", + "released": "remote_control_released.png", + }, "bring_to_focus": { "pressed": "panoramic_bring_into_focus_pressed.png", "released": "panoramic_bring_into_focus_released.png", diff --git a/notebooks/__code/panoramic_stitching/data_initialization.py b/notebooks/__code/panoramic_stitching/data_initialization.py index 213cebe19..322fb2ba9 100755 --- a/notebooks/__code/panoramic_stitching/data_initialization.py +++ b/notebooks/__code/panoramic_stitching/data_initialization.py @@ -35,7 +35,9 @@ def offset_table(self): xoffset = 0 yoffset = 0 else: - if current_metadata.get(X_METADATA_NAME, 0) < previous_metadata.get(X_METADATA_NAME, 0): + if current_metadata.get(X_METADATA_NAME, 0) < previous_metadata.get( + X_METADATA_NAME, 0 + ): xoffset = 0 yoffset += image_height else: diff --git a/notebooks/__code/panoramic_stitching/event_handler.py b/notebooks/__code/panoramic_stitching/event_handler.py index 3da6e17f6..95b27253b 100755 --- a/notebooks/__code/panoramic_stitching/event_handler.py +++ b/notebooks/__code/panoramic_stitching/event_handler.py @@ -6,7 +6,16 @@ import pyqtgraph as pg from qtpy import QtCore, QtGui from qtpy.QtGui import QIcon -from qtpy.QtWidgets import QApplication, QCheckBox, QFileDialog, QHBoxLayout, QMenu, QSizePolicy, QSpacerItem, QWidget +from qtpy.QtWidgets import ( + QApplication, + QCheckBox, + QFileDialog, + QHBoxLayout, + QMenu, + QSizePolicy, + QSpacerItem, + QWidget, +) from __code._utilities.table_handler import TableHandler from __code._utilities.widgets_handler import WidgetsHandler @@ -15,7 +24,10 @@ from __code.panoramic_stitching.gui_handler import GuiHandler from __code.panoramic_stitching.gui_initialization import GuiInitialization from __code.panoramic_stitching.image_handler import ImageHandler -from __code.panoramic_stitching.status_message_config import StatusMessageStatus, show_status_message +from __code.panoramic_stitching.status_message_config import ( + StatusMessageStatus, + show_status_message, +) from __code.panoramic_stitching.utilities import make_full_file_name_to_static_folder_of @@ -78,11 +90,18 @@ def list_folder_combobox_value_changed(self, new_folder_selected=None): else: editable_flag = editable_columns_boolean[_column_index] - o_table.insert_item(row=_row_index, column=_column_index, value=_text, editable=editable_flag) + o_table.insert_item( + row=_row_index, + column=_column_index, + value=_text, + editable=editable_flag, + ) # checkbox to turn on/off visibility of the row hori_layout = QHBoxLayout() - spacer_item_left = QSpacerItem(408, 20, QSizePolicy.Expanding, QSizePolicy.Expanding) + spacer_item_left = QSpacerItem( + 408, 20, QSizePolicy.Expanding, QSizePolicy.Expanding + ) hori_layout.addItem(spacer_item_left) check_box = QCheckBox() if offset_file_entry["visible"]: @@ -92,10 +111,14 @@ def list_folder_combobox_value_changed(self, new_folder_selected=None): check_box.setCheckState(_state) check_box.stateChanged.connect( - lambda state=0, row=_row_index: self.parent.visibility_checkbox_changed(state=state, row=row) + lambda state=0, row=_row_index: self.parent.visibility_checkbox_changed( + state=state, row=row + ) ) hori_layout.addWidget(check_box) - spacer_item_right = QSpacerItem(408, 20, QSizePolicy.Expanding, QSizePolicy.Expanding) + spacer_item_right = QSpacerItem( + 408, 20, QSizePolicy.Expanding, QSizePolicy.Expanding + ) hori_layout.addItem(spacer_item_right) cell_widget = QWidget() cell_widget.setLayout(hori_layout) @@ -121,7 +144,9 @@ def check_status_of_from_to_checkbox(self): self.parent.ui.from_to_button.setEnabled(False) self.parent.ui.from_to_error_label.setVisible(False) if self.parent.remote_control_id: - self.parent.remote_control_id.ui.move_active_image_pushButton.setEnabled(False) + self.parent.remote_control_id.ui.move_active_image_pushButton.setEnabled( + False + ) else: if row_selected == 0: state = False @@ -144,7 +169,9 @@ def roi_box_changed( self, roi_id=None, ): - region = roi_id.getArraySlice(self.parent.current_live_image, self.parent.ui.image_view.imageItem) + region = roi_id.getArraySlice( + self.parent.current_live_image, self.parent.ui.image_view.imageItem + ) x0 = region[0][0].start y0 = region[0][1].start @@ -183,12 +210,16 @@ def from_to_button_pushed(self): o_table = TableHandler(table_ui=self.parent.ui.tableWidget) row_selected = o_table.get_row_selected() - current_xoffset_of_selected_row = int(o_table.get_item_str_from_cell(row=row_selected, column=1)) + current_xoffset_of_selected_row = int( + o_table.get_item_str_from_cell(row=row_selected, column=1) + ) new_xoffset = int(current_xoffset_of_selected_row - delta_x) self.parent.ui.tableWidget.item(row_selected, 1).setText(str(new_xoffset)) self.save_table_offset_of_this_cell(row=row_selected, column=1) - current_yoffset_of_selected_row = int(o_table.get_item_str_from_cell(row=row_selected, column=2)) + current_yoffset_of_selected_row = int( + o_table.get_item_str_from_cell(row=row_selected, column=2) + ) new_yoffset = current_yoffset_of_selected_row - delta_y self.parent.ui.tableWidget.item(row_selected, 2).setText(str(new_yoffset)) self.save_table_offset_of_this_cell(row=row_selected, column=2) @@ -283,7 +314,9 @@ def manual_offset_changed(self, direction="horizontal", nbr_pixel=1): current_offset = o_table.get_item_str_from_cell(row=row_selected, column=column) new_offset = int(current_offset) + nbr_pixel - o_table.set_item_with_str(row=row_selected, column=column, cell_str=str(new_offset)) + o_table.set_item_with_str( + row=row_selected, column=column, cell_str=str(new_offset) + ) self.parent.table_of_offset_cell_changed(row_selected, column) @@ -422,7 +455,9 @@ def export_table(self, table_file_name=""): self.parent.save_as_table_file_name = table_file_name self.parent.ui.actionSave_Table.setEnabled(True) - self.parent.ui.actionSave_Table.setText(f"Save ({os.path.basename(table_file_name)})") + self.parent.ui.actionSave_Table.setText( + f"Save ({os.path.basename(table_file_name)})" + ) def make_table_dict(self): o_table = TableHandler(table_ui=self.parent.ui.tableWidget) @@ -430,7 +465,10 @@ def make_table_dict(self): nbr_column = 2 my_dictionary = {} for _row in np.arange(nbr_row): - local_list = [o_table.get_item_str_from_cell(_row, _column) for _column in (np.arange(nbr_column) + 1)] + local_list = [ + o_table.get_item_str_from_cell(_row, _column) + for _column in (np.arange(nbr_column) + 1) + ] my_dictionary[str(_row)] = local_list return my_dictionary @@ -440,10 +478,14 @@ def update_remote_ui(self): @staticmethod def button_pressed(ui=None, name="left"): - full_file = make_full_file_name_to_static_folder_of(config.button[name]["pressed"]) + full_file = make_full_file_name_to_static_folder_of( + config.button[name]["pressed"] + ) ui.setIcon(QIcon(full_file)) @staticmethod def button_released(ui=None, name="left"): - full_file = make_full_file_name_to_static_folder_of(config.button[name]["released"]) + full_file = make_full_file_name_to_static_folder_of( + config.button[name]["released"] + ) ui.setIcon(QIcon(full_file)) diff --git a/notebooks/__code/panoramic_stitching/export.py b/notebooks/__code/panoramic_stitching/export.py index c07f11398..f349f8b76 100755 --- a/notebooks/__code/panoramic_stitching/export.py +++ b/notebooks/__code/panoramic_stitching/export.py @@ -27,7 +27,8 @@ def select_output_folder(self): output_folder = QFileDialog.getExistingDirectory( self.parent, directory=self.parent.working_dir, - caption="Select where the folder containing the " "panoramic images will be created!", + caption="Select where the folder containing the " + "panoramic images will be created!", options=QFileDialog.ShowDirsOnly, ) if output_folder: @@ -72,43 +73,58 @@ def create_panoramic_images(self, output_folder=None): if _file_index == 0: panoramic_image[ - yoffset + VERTICAL_MARGIN : yoffset + image_height + VERTICAL_MARGIN, - xoffset + HORIZONTAL_MARGIN : xoffset + image_width + HORIZONTAL_MARGIN, + yoffset + VERTICAL_MARGIN : yoffset + + image_height + + VERTICAL_MARGIN, + xoffset + HORIZONTAL_MARGIN : xoffset + + image_width + + HORIZONTAL_MARGIN, ] = image continue temp_big_image = np.zeros((panoramic_height, panoramic_width)) temp_big_image[ - yoffset + VERTICAL_MARGIN : yoffset + image_height + VERTICAL_MARGIN, - xoffset + HORIZONTAL_MARGIN : xoffset + image_width + HORIZONTAL_MARGIN, + yoffset + VERTICAL_MARGIN : yoffset + + image_height + + VERTICAL_MARGIN, + xoffset + HORIZONTAL_MARGIN : xoffset + + image_width + + HORIZONTAL_MARGIN, ] = image # where_panoramic_image_has_value_only = np.where((panoramic_image != 0) & (temp_big_image == 0)) - where_temp_big_image_has_value_only = np.where((temp_big_image != 0) & (panoramic_image == 0)) - where_both_images_overlap = np.where((panoramic_image != 0) & (temp_big_image != 0)) + where_temp_big_image_has_value_only = np.where( + (temp_big_image != 0) & (panoramic_image == 0) + ) + where_both_images_overlap = np.where( + (panoramic_image != 0) & (temp_big_image != 0) + ) if stitching_algorithm == StitchingAlgorithmType.minimum: - panoramic_image[where_temp_big_image_has_value_only] = temp_big_image[ - where_temp_big_image_has_value_only - ] + panoramic_image[where_temp_big_image_has_value_only] = ( + temp_big_image[where_temp_big_image_has_value_only] + ) panoramic_image[where_both_images_overlap] = np.minimum( - panoramic_image[where_both_images_overlap], temp_big_image[where_both_images_overlap] + panoramic_image[where_both_images_overlap], + temp_big_image[where_both_images_overlap], ) elif stitching_algorithm == StitchingAlgorithmType.maximum: - panoramic_image[where_temp_big_image_has_value_only] = temp_big_image[ - where_temp_big_image_has_value_only - ] + panoramic_image[where_temp_big_image_has_value_only] = ( + temp_big_image[where_temp_big_image_has_value_only] + ) panoramic_image[where_both_images_overlap] = np.maximum( - panoramic_image[where_both_images_overlap], temp_big_image[where_both_images_overlap] + panoramic_image[where_both_images_overlap], + temp_big_image[where_both_images_overlap], ) elif stitching_algorithm == StitchingAlgorithmType.mean: - panoramic_image[where_temp_big_image_has_value_only] = temp_big_image[ - where_temp_big_image_has_value_only - ] + panoramic_image[where_temp_big_image_has_value_only] = ( + temp_big_image[where_temp_big_image_has_value_only] + ) panoramic_image[where_both_images_overlap] = ( - panoramic_image[where_both_images_overlap] + temp_big_image[where_both_images_overlap] + panoramic_image[where_both_images_overlap] + + temp_big_image[where_both_images_overlap] ) / 2 panoramic_images_dict[_group_name] = panoramic_image @@ -122,9 +138,14 @@ def create_panoramic_images(self, output_folder=None): def export_images(self, output_folder=None): stitching_algorithm = self.parent.stitching_algorithm - new_folder_name = os.path.basename(self.parent.working_dir) + f"_panoramic_{stitching_algorithm}" + new_folder_name = ( + os.path.basename(self.parent.working_dir) + + f"_panoramic_{stitching_algorithm}" + ) self.parent.ui.statusbar.setStyleSheet("color: blue") - self.parent.ui.statusbar.showMessage(f"Exporting images in folder {new_folder_name}") + self.parent.ui.statusbar.showMessage( + f"Exporting images in folder {new_folder_name}" + ) QtGui.QGuiApplication.processEvents() new_output_folder_name = os.path.join(output_folder, new_folder_name) @@ -142,7 +163,9 @@ def export_images(self, output_folder=None): o_norm.data["sample"]["filename"] = list_filename o_norm.export(new_output_folder_name, data_type="sample") self.parent.ui.statusbar.setStyleSheet("color: green") - self.parent.ui.statusbar.showMessage(f"{new_output_folder_name} has been created!", 10000) # 10s + self.parent.ui.statusbar.showMessage( + f"{new_output_folder_name} has been created!", 10000 + ) # 10s class SelectStitchingAlgorithm(QDialog): @@ -162,7 +185,9 @@ def __init__(self, top_parent=None, parent=None): def display_plot(self): self.top_parent.ui.statusbar.setStyleSheet("color: blue") - self.top_parent.ui.statusbar.showMessage("Calculating previews of current working group ...") + self.top_parent.ui.statusbar.showMessage( + "Calculating previews of current working group ..." + ) QtGui.QGuiApplication.processEvents() o_get = Get(parent=self.top_parent) @@ -192,16 +217,28 @@ def display_plot(self): if _file_index == 0: minimum_panoramic_image[ - yoffset + VERTICAL_MARGIN : yoffset + image_height + VERTICAL_MARGIN, - xoffset + HORIZONTAL_MARGIN : xoffset + image_width + HORIZONTAL_MARGIN, + yoffset + VERTICAL_MARGIN : yoffset + + image_height + + VERTICAL_MARGIN, + xoffset + HORIZONTAL_MARGIN : xoffset + + image_width + + HORIZONTAL_MARGIN, ] = image maximum_panoramic_image[ - yoffset + VERTICAL_MARGIN : yoffset + image_height + VERTICAL_MARGIN, - xoffset + HORIZONTAL_MARGIN : xoffset + image_width + HORIZONTAL_MARGIN, + yoffset + VERTICAL_MARGIN : yoffset + + image_height + + VERTICAL_MARGIN, + xoffset + HORIZONTAL_MARGIN : xoffset + + image_width + + HORIZONTAL_MARGIN, ] = image mean_panoramic_image[ - yoffset + VERTICAL_MARGIN : yoffset + image_height + VERTICAL_MARGIN, - xoffset + HORIZONTAL_MARGIN : xoffset + image_width + HORIZONTAL_MARGIN, + yoffset + VERTICAL_MARGIN : yoffset + + image_height + + VERTICAL_MARGIN, + xoffset + HORIZONTAL_MARGIN : xoffset + + image_width + + HORIZONTAL_MARGIN, ] = image continue @@ -212,29 +249,36 @@ def display_plot(self): ] = image # where_panoramic_image_has_value_only = np.where((panoramic_image != 0) & (temp_big_image == 0)) - where_temp_big_image_has_value_only = np.where((temp_big_image != 0) & (minimum_panoramic_image == 0)) - where_both_images_overlap = np.where((minimum_panoramic_image != 0) & (temp_big_image != 0)) + where_temp_big_image_has_value_only = np.where( + (temp_big_image != 0) & (minimum_panoramic_image == 0) + ) + where_both_images_overlap = np.where( + (minimum_panoramic_image != 0) & (temp_big_image != 0) + ) # minimum algorithm - minimum_panoramic_image[where_temp_big_image_has_value_only] = temp_big_image[ - where_temp_big_image_has_value_only - ] + minimum_panoramic_image[where_temp_big_image_has_value_only] = ( + temp_big_image[where_temp_big_image_has_value_only] + ) minimum_panoramic_image[where_both_images_overlap] = np.minimum( - minimum_panoramic_image[where_both_images_overlap], temp_big_image[where_both_images_overlap] + minimum_panoramic_image[where_both_images_overlap], + temp_big_image[where_both_images_overlap], ) # maximum algorithm - maximum_panoramic_image[where_temp_big_image_has_value_only] = temp_big_image[ - where_temp_big_image_has_value_only - ] + maximum_panoramic_image[where_temp_big_image_has_value_only] = ( + temp_big_image[where_temp_big_image_has_value_only] + ) maximum_panoramic_image[where_both_images_overlap] = np.maximum( - maximum_panoramic_image[where_both_images_overlap], temp_big_image[where_both_images_overlap] + maximum_panoramic_image[where_both_images_overlap], + temp_big_image[where_both_images_overlap], ) # mean algorithm mean_panoramic_image[where_temp_big_image_has_value_only] = temp_big_image[ where_temp_big_image_has_value_only ] mean_panoramic_image[where_both_images_overlap] = ( - mean_panoramic_image[where_both_images_overlap] + temp_big_image[where_both_images_overlap] + mean_panoramic_image[where_both_images_overlap] + + temp_big_image[where_both_images_overlap] ) / 2 self.top_parent.eventProgress.setValue(_file_index + 1) @@ -334,4 +378,6 @@ def _get_stitching_algorithm_selected(self): elif self.ui.use_linear_integration_radioButton.isChecked(): return StitchingAlgorithmType.linear_integration else: - raise NotImplementedError("Stitching algorithm has not been implemented yet!") + raise NotImplementedError( + "Stitching algorithm has not been implemented yet!" + ) diff --git a/notebooks/__code/panoramic_stitching/gui_initialization.py b/notebooks/__code/panoramic_stitching/gui_initialization.py index 0e0d1cd94..cfc5c76b2 100755 --- a/notebooks/__code/panoramic_stitching/gui_initialization.py +++ b/notebooks/__code/panoramic_stitching/gui_initialization.py @@ -12,7 +12,10 @@ from __code.panoramic_stitching.config_buttons import button from __code.panoramic_stitching.gui_handler import GuiHandler from __code.panoramic_stitching.mplcanvas import MplCanvas -from __code.panoramic_stitching.utilities import make_full_file_name_to_static_folder_of, set_widgets_size +from __code.panoramic_stitching.utilities import ( + make_full_file_name_to_static_folder_of, + set_widgets_size, +) class GuiInitialization: @@ -80,7 +83,9 @@ def widgets(self): self.parent.ui.from_to_error_label.setVisible(False) # remote control - remote_control = make_full_file_name_to_static_folder_of("remote_control_released.png") + remote_control = make_full_file_name_to_static_folder_of( + "remote_control_released.png" + ) self.parent.ui.remote_control_widget.setIcon(QIcon(remote_control)) set_widgets_size( widgets=[self.parent.ui.remote_control_widget], @@ -91,22 +96,38 @@ def widgets(self): # move buttons _file_path = os.path.dirname(__file__) - up_up_arrow_file = make_full_file_name_to_static_folder_of(button["up_up"]["released"]) + up_up_arrow_file = make_full_file_name_to_static_folder_of( + button["up_up"]["released"] + ) self.parent.ui.up_up_button.setIcon(QIcon(up_up_arrow_file)) - up_arrow_file = make_full_file_name_to_static_folder_of(button["up"]["released"]) + up_arrow_file = make_full_file_name_to_static_folder_of( + button["up"]["released"] + ) self.parent.ui.up_button.setIcon(QIcon(up_arrow_file)) - left_left_arrow_file = make_full_file_name_to_static_folder_of(button["left_left"]["released"]) + left_left_arrow_file = make_full_file_name_to_static_folder_of( + button["left_left"]["released"] + ) self.parent.ui.left_left_button.setIcon(QIcon(left_left_arrow_file)) - left_arrow_file = make_full_file_name_to_static_folder_of(button["left"]["released"]) + left_arrow_file = make_full_file_name_to_static_folder_of( + button["left"]["released"] + ) self.parent.ui.left_button.setIcon(QIcon(left_arrow_file)) - right_arrow_file = make_full_file_name_to_static_folder_of(button["right"]["released"]) + right_arrow_file = make_full_file_name_to_static_folder_of( + button["right"]["released"] + ) self.parent.ui.right_button.setIcon(QIcon(right_arrow_file)) - right_right_arrow_file = make_full_file_name_to_static_folder_of(button["right_right"]["released"]) + right_right_arrow_file = make_full_file_name_to_static_folder_of( + button["right_right"]["released"] + ) self.parent.ui.right_right_button.setIcon(QIcon(right_right_arrow_file)) - down_arrow_file = make_full_file_name_to_static_folder_of(button["down"]["released"]) + down_arrow_file = make_full_file_name_to_static_folder_of( + button["down"]["released"] + ) self.parent.ui.down_button.setIcon(QIcon(down_arrow_file)) - down_down_arrow_file = make_full_file_name_to_static_folder_of(button["down_down"]["released"]) + down_down_arrow_file = make_full_file_name_to_static_folder_of( + button["down_down"]["released"] + ) self.parent.ui.down_down_button.setIcon(QIcon(down_down_arrow_file)) list_ui = [self.parent.ui.left_button, self.parent.ui.right_button] @@ -137,14 +158,21 @@ def widgets(self): height=self.button_size["double_vertical_arrow"]["height"], ) - state_hori_matplotlib = self.parent.ui.enable_horizontal_profile_checkbox.isChecked() + state_hori_matplotlib = ( + self.parent.ui.enable_horizontal_profile_checkbox.isChecked() + ) o_gui = GuiHandler(parent=self.parent) o_gui.enabled_horizontal_profile_widgets(enabled=state_hori_matplotlib) - state_verti_matplotlib = self.parent.ui.enable_vertical_profile_checkbox.isChecked() + state_verti_matplotlib = ( + self.parent.ui.enable_vertical_profile_checkbox.isChecked() + ) o_gui.enabled_vertical_profile_widgets(enabled=state_verti_matplotlib) - profile_sliders = [self.parent.ui.horizontal_profile_width_slider, self.parent.ui.vertical_profile_width_slider] + profile_sliders = [ + self.parent.ui.horizontal_profile_width_slider, + self.parent.ui.vertical_profile_width_slider, + ] for _slider in profile_sliders: _slider.setMinimum(self.parent.width_profile["min"]) _slider.setMaximum(self.parent.width_profile["max"]) diff --git a/notebooks/__code/panoramic_stitching/image_handler.py b/notebooks/__code/panoramic_stitching/image_handler.py index cbd1a08a7..a3806603e 100755 --- a/notebooks/__code/panoramic_stitching/image_handler.py +++ b/notebooks/__code/panoramic_stitching/image_handler.py @@ -25,7 +25,9 @@ def update_contour_plot(self): o_table = TableHandler(table_ui=self.parent.ui.tableWidget) row_selected = o_table.get_row_selected() - name_of_file_selected = o_table.get_item_str_from_cell(row=row_selected, column=0) + name_of_file_selected = o_table.get_item_str_from_cell( + row=row_selected, column=0 + ) o_get = Get(parent=self.parent) folder_selected = o_get.get_combobox_folder_selected() @@ -104,8 +106,12 @@ def update_current_panoramic_image(self): yoffset = offset_dictionary[_file]["yoffset"] panoramic_image[ - yoffset + VERTICAL_MARGIN : yoffset + image_height + VERTICAL_MARGIN, - xoffset + HORIZONTAL_MARGIN : xoffset + image_width + HORIZONTAL_MARGIN, + yoffset + VERTICAL_MARGIN : yoffset + + image_height + + VERTICAL_MARGIN, + xoffset + HORIZONTAL_MARGIN : xoffset + + image_width + + HORIZONTAL_MARGIN, ] = _image self.parent.panoramic_images[folder_selected] = panoramic_image @@ -117,13 +123,19 @@ def update_current_panoramic_image(self): _view_box.setState(_state) if not first_update: - _histo_widget.setLevels(self.parent.histogram_level[0], self.parent.histogram_level[1]) + _histo_widget.setLevels( + self.parent.histogram_level[0], self.parent.histogram_level[1] + ) def get_max_offset(self, folder_selected=None): offset_dictionary = self.parent.offset_dictionary[folder_selected] - list_xoffset = [offset_dictionary[_key]["xoffset"] for _key in offset_dictionary.keys()] - list_yoffset = [offset_dictionary[_key]["yoffset"] for _key in offset_dictionary.keys()] + list_xoffset = [ + offset_dictionary[_key]["xoffset"] for _key in offset_dictionary.keys() + ] + list_yoffset = [ + offset_dictionary[_key]["yoffset"] for _key in offset_dictionary.keys() + ] return int(np.max(list_yoffset)), int(np.max(list_xoffset)) @@ -135,16 +147,24 @@ def update_from_to_roi(self, state=False): from_roi = self.parent.from_roi x = from_roi["x"] y = from_roi["y"] - self.parent.from_roi_id = pg.ROI([x, y], [ROI_WIDTH, ROI_HEIGHT], scaleSnap=True) + self.parent.from_roi_id = pg.ROI( + [x, y], [ROI_WIDTH, ROI_HEIGHT], scaleSnap=True + ) self.parent.ui.image_view.addItem(self.parent.from_roi_id) - self.parent.from_roi_id.sigRegionChanged.connect(self.parent.from_roi_box_changed) + self.parent.from_roi_id.sigRegionChanged.connect( + self.parent.from_roi_box_changed + ) to_roi = self.parent.to_roi x = to_roi["x"] y = to_roi["y"] - self.parent.to_roi_id = pg.ROI([x, y], [ROI_WIDTH, ROI_HEIGHT], scaleSnap=True) + self.parent.to_roi_id = pg.ROI( + [x, y], [ROI_WIDTH, ROI_HEIGHT], scaleSnap=True + ) self.parent.ui.image_view.addItem(self.parent.to_roi_id) - self.parent.to_roi_id.sigRegionChanged.connect(self.parent.to_roi_box_changed) + self.parent.to_roi_id.sigRegionChanged.connect( + self.parent.to_roi_box_changed + ) self.update_from_label() self.update_from_cross_line() @@ -170,12 +190,18 @@ def update_validity_of_from_to_button(self): o_table = TableHandler(table_ui=self.parent.ui.tableWidget) row_selected = o_table.get_row_selected() - name_of_file_selected = o_table.get_item_str_from_cell(row=row_selected, column=0) + name_of_file_selected = o_table.get_item_str_from_cell( + row=row_selected, column=0 + ) offset_dictionary = self.parent.offset_dictionary[folder_selected] - xoffset_of_selected_image = offset_dictionary[name_of_file_selected]["xoffset"] + HORIZONTAL_MARGIN - yoffset_of_selected_image = offset_dictionary[name_of_file_selected]["yoffset"] + VERTICAL_MARGIN + xoffset_of_selected_image = ( + offset_dictionary[name_of_file_selected]["xoffset"] + HORIZONTAL_MARGIN + ) + yoffset_of_selected_image = ( + offset_dictionary[name_of_file_selected]["yoffset"] + VERTICAL_MARGIN + ) if ( (x < xoffset_of_selected_image) @@ -192,7 +218,9 @@ def update_validity_of_from_to_button(self): self.parent.ui.from_to_error_label.setVisible(from_to_error_label) if self.parent.remote_control_id: - self.parent.remote_control_id.ui.move_active_image_pushButton.setEnabled(from_to_button_status) + self.parent.remote_control_id.ui.move_active_image_pushButton.setEnabled( + from_to_button_status + ) def update_from_to_line_label_changed(self): from_to_roi = self.parent.from_to_roi @@ -230,11 +258,19 @@ def update_cross_line(self, roi_cross_id=None, roi=None): line_color = (255, 0, 0, 255, 1) lines = np.array( [line_color for _ in np.arange(len(pos))], - dtype=[("red", np.ubyte), ("green", np.ubyte), ("blue", np.ubyte), ("alpha", np.ubyte), ("width", float)], + dtype=[ + ("red", np.ubyte), + ("green", np.ubyte), + ("blue", np.ubyte), + ("alpha", np.ubyte), + ("width", float), + ], ) line_view_binning = pg.GraphItem() self.parent.ui.image_view.addItem(line_view_binning) - line_view_binning.setData(pos=pos, adj=adj, pen=lines, symbol=None, pxMode=False) + line_view_binning.setData( + pos=pos, adj=adj, pen=lines, symbol=None, pxMode=False + ) return line_view_binning @@ -242,13 +278,17 @@ def update_from_cross_line(self): from_roi_cross_id = self.parent.from_roi_cross_id from_roi = self.parent.from_roi - self.parent.from_roi_cross_id = self.update_cross_line(roi_cross_id=from_roi_cross_id, roi=from_roi) + self.parent.from_roi_cross_id = self.update_cross_line( + roi_cross_id=from_roi_cross_id, roi=from_roi + ) def update_to_cross_line(self): to_roi_cross_id = self.parent.to_roi_cross_id to_roi = self.parent.to_roi - self.parent.to_roi_cross_id = self.update_cross_line(roi_cross_id=to_roi_cross_id, roi=to_roi) + self.parent.to_roi_cross_id = self.update_cross_line( + roi_cross_id=to_roi_cross_id, roi=to_roi + ) def update_label(self, label_id=None, roi=None, text=""): if label_id: @@ -267,9 +307,13 @@ def update_label(self, label_id=None, roi=None, text=""): def update_from_label(self): label_id = self.parent.from_label_id roi = self.parent.from_roi - self.parent.from_label_id = self.update_label(label_id=label_id, roi=roi, text="from") + self.parent.from_label_id = self.update_label( + label_id=label_id, roi=roi, text="from" + ) def update_to_label(self): label_id = self.parent.to_label_id roi = self.parent.to_roi - self.parent.to_label_id = self.update_label(label_id=label_id, roi=roi, text="to") + self.parent.to_label_id = self.update_label( + label_id=label_id, roi=roi, text="to" + ) diff --git a/notebooks/__code/panoramic_stitching/load_data.py b/notebooks/__code/panoramic_stitching/load_data.py index f9b20dd28..8df0b5630 100755 --- a/notebooks/__code/panoramic_stitching/load_data.py +++ b/notebooks/__code/panoramic_stitching/load_data.py @@ -30,7 +30,7 @@ def keep_only_metadata_defined_in_config(self, list_key=None): metadata_to_keep[_name] = float(_value) except KeyError: continue - + self.metadata = metadata_to_keep @@ -72,14 +72,18 @@ def run(self): # record size of images if _folder_index == 0: - self.parent.image_height, self.parent.image_width = np.shape(o_norm.data["sample"]["data"][0]) + self.parent.image_height, self.parent.image_width = np.shape( + o_norm.data["sample"]["data"][0] + ) local_dict = OrderedDict() for _index, _file in enumerate(list_files): _metadatadata = MetadataData() _metadatadata.data = o_norm.data["sample"]["data"][_index] _metadatadata.metadata = o_norm.data["sample"]["metadata"][_index] - _metadatadata.keep_only_metadata_defined_in_config(list_key=self.metadata_key_to_keep) + _metadatadata.keep_only_metadata_defined_in_config( + list_key=self.metadata_key_to_keep + ) local_dict[_file] = _metadatadata diff --git a/notebooks/__code/panoramic_stitching/panoramic_stitching.py b/notebooks/__code/panoramic_stitching/panoramic_stitching.py index 59ce9f42a..7ee37b2b1 100755 --- a/notebooks/__code/panoramic_stitching/panoramic_stitching.py +++ b/notebooks/__code/panoramic_stitching/panoramic_stitching.py @@ -6,7 +6,9 @@ from __code import load_ui from __code._utilities.error import NoFilesFound -from __code._utilities.folder import get_list_of_folders_with_specified_file_type_and_same_number_of_files +from __code._utilities.folder import ( + get_list_of_folders_with_specified_file_type_and_same_number_of_files, +) from __code._utilities.string import format_html_message from __code.ipywe import fileselector from __code.panoramic_stitching.automatically_stitch import AutomaticallyStitch @@ -14,7 +16,11 @@ from __code.panoramic_stitching.event_handler import EventHandler from __code.panoramic_stitching.export import Export from __code.panoramic_stitching.gui_initialization import GuiInitialization -from __code.panoramic_stitching.image_handler import HORIZONTAL_MARGIN, VERTICAL_MARGIN, ImageHandler +from __code.panoramic_stitching.image_handler import ( + HORIZONTAL_MARGIN, + VERTICAL_MARGIN, + ImageHandler, +) from __code.panoramic_stitching.load_data import LoadData from __code.panoramic_stitching.profile import Profile from __code.panoramic_stitching.remote_control_handler import RemoteControlHandler @@ -31,7 +37,7 @@ def __init__(self, working_dir=""): def select_input_folders(self): self.list_folder_widget = fileselector.FileSelectorPanel( - instruction="select all the folders of images to " "stitch", + instruction="select all the folders of images to stitch", start_dir=self.working_dir, type="directory", next=self.folder_selected, @@ -42,11 +48,16 @@ def select_input_folders(self): def folder_selected(self, folder_selected): final_list_folders, list_folders_rejected = ( get_list_of_folders_with_specified_file_type_and_same_number_of_files( - list_of_folders_to_check=folder_selected, file_extension=self.file_extension + list_of_folders_to_check=folder_selected, + file_extension=self.file_extension, ) ) - self.working_dir = os.path.dirname(final_list_folders[0]) if final_list_folders else self.working_dir + self.working_dir = ( + os.path.dirname(final_list_folders[0]) + if final_list_folders + else self.working_dir + ) print("there") if not final_list_folders: str_list_ext = ", ".join(self.file_extension) @@ -61,18 +72,24 @@ def folder_selected(self, folder_selected): final_list_folders.sort() nbr_folder = len(final_list_folders) - display(format_html_message(pre_message=f"Notebook is about to work with {nbr_folder} folders!", spacer="")) + display( + format_html_message( + pre_message=f"Notebook is about to work with {nbr_folder} folders!", + spacer="", + ) + ) self.final_list_folders = final_list_folders self.list_folders_rejected = list_folders_rejected def start_panoramic_stitching(self): - final_list_folders = self.final_list_folders list_folders_rejected = self.list_folders_rejected # gui initialization - o_interface = Interface(list_folders=final_list_folders, list_folders_rejected=list_folders_rejected) + o_interface = Interface( + list_folders=final_list_folders, list_folders_rejected=list_folders_rejected + ) o_interface.show() try: o_interface.load_data() @@ -235,15 +252,21 @@ def initialization_after_loading_data(self): # event handler def remote_control_widget_pressed(self): - EventHandler.button_pressed(ui=self.ui.remote_control_widget, name="remote_control") + EventHandler.button_pressed( + ui=self.ui.remote_control_widget, name="remote_control" + ) RemoteControlHandler(parent=self) def remote_control_widget_released(self): - EventHandler.button_released(ui=self.ui.remote_control_widget, name="remote_control") + EventHandler.button_released( + ui=self.ui.remote_control_widget, name="remote_control" + ) def list_folder_combobox_value_changed(self, new_folder_selected=None): o_event = EventHandler(parent=self) - o_event.list_folder_combobox_value_changed(new_folder_selected=new_folder_selected) + o_event.list_folder_combobox_value_changed( + new_folder_selected=new_folder_selected + ) def visibility_checkbox_changed(self, state=None, row=-1): o_event = EventHandler(parent=self) @@ -319,7 +342,9 @@ def enable_vertical_profile_checked(self, state): def left_left_button_pressed(self): EventHandler.button_pressed(ui=self.ui.left_left_button, name="left_left") o_event = EventHandler(parent=self) - o_event.manual_offset_changed(direction="horizontal", nbr_pixel=-DOUBLE_MANUAL_PIXEL_CHANGE) + o_event.manual_offset_changed( + direction="horizontal", nbr_pixel=-DOUBLE_MANUAL_PIXEL_CHANGE + ) self.horizontal_profile_changed() def left_left_button_released(self): @@ -328,7 +353,9 @@ def left_left_button_released(self): def left_button_pressed(self): EventHandler.button_pressed(ui=self.ui.left_button, name="left") o_event = EventHandler(parent=self) - o_event.manual_offset_changed(direction="horizontal", nbr_pixel=-SIMPLE_MANUAL_PIXEL_CHANGE) + o_event.manual_offset_changed( + direction="horizontal", nbr_pixel=-SIMPLE_MANUAL_PIXEL_CHANGE + ) self.horizontal_profile_changed() def left_button_released(self): @@ -337,7 +364,9 @@ def left_button_released(self): def right_right_button_pressed(self): EventHandler.button_pressed(ui=self.ui.right_right_button, name="right_right") o_event = EventHandler(parent=self) - o_event.manual_offset_changed(direction="horizontal", nbr_pixel=DOUBLE_MANUAL_PIXEL_CHANGE) + o_event.manual_offset_changed( + direction="horizontal", nbr_pixel=DOUBLE_MANUAL_PIXEL_CHANGE + ) self.horizontal_profile_changed() def right_right_button_released(self): @@ -346,7 +375,9 @@ def right_right_button_released(self): def right_button_pressed(self): EventHandler.button_pressed(ui=self.ui.right_button, name="right") o_event = EventHandler(parent=self) - o_event.manual_offset_changed(direction="horizontal", nbr_pixel=SIMPLE_MANUAL_PIXEL_CHANGE) + o_event.manual_offset_changed( + direction="horizontal", nbr_pixel=SIMPLE_MANUAL_PIXEL_CHANGE + ) self.horizontal_profile_changed() def right_button_released(self): @@ -355,7 +386,9 @@ def right_button_released(self): def up_up_button_pressed(self): EventHandler.button_pressed(ui=self.ui.up_up_button, name="up_up") o_event = EventHandler(parent=self) - o_event.manual_offset_changed(direction="vertical", nbr_pixel=-DOUBLE_MANUAL_PIXEL_CHANGE) + o_event.manual_offset_changed( + direction="vertical", nbr_pixel=-DOUBLE_MANUAL_PIXEL_CHANGE + ) self.vertical_profile_changed() def up_up_button_released(self): @@ -364,7 +397,9 @@ def up_up_button_released(self): def up_button_pressed(self): EventHandler.button_pressed(ui=self.ui.up_button, name="up") o_event = EventHandler(parent=self) - o_event.manual_offset_changed(direction="vertical", nbr_pixel=-SIMPLE_MANUAL_PIXEL_CHANGE) + o_event.manual_offset_changed( + direction="vertical", nbr_pixel=-SIMPLE_MANUAL_PIXEL_CHANGE + ) self.vertical_profile_changed() def up_button_released(self): @@ -373,7 +408,9 @@ def up_button_released(self): def down_down_button_pressed(self): EventHandler.button_pressed(ui=self.ui.down_down_button, name="down_down") o_event = EventHandler(parent=self) - o_event.manual_offset_changed(direction="vertical", nbr_pixel=DOUBLE_MANUAL_PIXEL_CHANGE) + o_event.manual_offset_changed( + direction="vertical", nbr_pixel=DOUBLE_MANUAL_PIXEL_CHANGE + ) self.vertical_profile_changed() def down_down_button_released(self): @@ -382,7 +419,9 @@ def down_down_button_released(self): def down_button_pressed(self): EventHandler.button_pressed(ui=self.ui.down_button, name="down") o_event = EventHandler(parent=self) - o_event.manual_offset_changed(direction="vertical", nbr_pixel=SIMPLE_MANUAL_PIXEL_CHANGE) + o_event.manual_offset_changed( + direction="vertical", nbr_pixel=SIMPLE_MANUAL_PIXEL_CHANGE + ) self.vertical_profile_changed() def down_button_released(self): diff --git a/notebooks/__code/panoramic_stitching/profile.py b/notebooks/__code/panoramic_stitching/profile.py index ab3dc352f..7d936282e 100755 --- a/notebooks/__code/panoramic_stitching/profile.py +++ b/notebooks/__code/panoramic_stitching/profile.py @@ -15,7 +15,9 @@ def __init__(self, parent=None): def horizontal_profile_changed(self): if self.parent.ui.enable_horizontal_profile_checkbox.isChecked(): roi_id = self.parent.horizontal_profile["id"] - horizontal_roi_dimensions = Profile.get_x_y_width_height_of_roi(roi_id=roi_id) + horizontal_roi_dimensions = Profile.get_x_y_width_height_of_roi( + roi_id=roi_id + ) self.plot_profile( x=horizontal_roi_dimensions["x"], y=horizontal_roi_dimensions["y"], @@ -36,7 +38,9 @@ def vertical_profile_changed(self): profile_type="vertical", ) - def plot_profile(self, x=None, y=None, width=None, height=None, profile_type="horizontal"): + def plot_profile( + self, x=None, y=None, width=None, height=None, profile_type="horizontal" + ): if profile_type == "horizontal": plot_ui = self.parent.horizontal_profile_plot dim_to_keep = 0 @@ -88,15 +92,23 @@ def plot_profile(self, x=None, y=None, width=None, height=None, profile_type="ho # find part of profile that is inside image x_left_for_profile = np.max([x, left_of_image]) - left_of_image - x_right_for_profile = np.min([x + width, left_of_image + image_width]) - left_of_image + x_right_for_profile = ( + np.min([x + width, left_of_image + image_width]) - left_of_image + ) y_top_for_profile = np.max([y, top_of_image]) - top_of_image - y_bottom_for_profile = np.min([y + height, top_of_image + image_height]) - top_of_image + y_bottom_for_profile = ( + np.min([y + height, top_of_image + image_height]) - top_of_image + ) if profile_type == "horizontal": - x_axis_of_profile = np.arange(x_left_for_profile, x_right_for_profile) + left_of_image + x_axis_of_profile = ( + np.arange(x_left_for_profile, x_right_for_profile) + left_of_image + ) else: - x_axis_of_profile = np.arange(y_top_for_profile, y_bottom_for_profile) + top_of_image + x_axis_of_profile = ( + np.arange(y_top_for_profile, y_bottom_for_profile) + top_of_image + ) y_axis_of_profile = data_dictionary[_file].data[ y_top_for_profile:y_bottom_for_profile, diff --git a/notebooks/__code/panoramic_stitching/remote_control_handler.py b/notebooks/__code/panoramic_stitching/remote_control_handler.py index f09d66f24..2e0b19661 100755 --- a/notebooks/__code/panoramic_stitching/remote_control_handler.py +++ b/notebooks/__code/panoramic_stitching/remote_control_handler.py @@ -8,7 +8,10 @@ from __code.panoramic_stitching.config_buttons import button from __code.panoramic_stitching.event_handler import EventHandler from __code.panoramic_stitching.image_handler import ImageHandler -from __code.panoramic_stitching.utilities import make_full_file_name_to_static_folder_of, set_widget_size +from __code.panoramic_stitching.utilities import ( + make_full_file_name_to_static_folder_of, + set_widget_size, +) BORDER_RANGE = 50 @@ -41,7 +44,9 @@ def __init__(self, parent=None): def initialize_widget(self): _file_path = os.path.dirname(__file__) - bring_to_focus_released = make_full_file_name_to_static_folder_of(button["bring_to_focus"]["released"]) + bring_to_focus_released = make_full_file_name_to_static_folder_of( + button["bring_to_focus"]["released"] + ) self.ui.bring_to_focus.setIcon(QIcon(bring_to_focus_released)) set_widget_size(widget=self.ui.bring_to_focus, width=500, height=203) self.check_previous_next_buttons_status() @@ -89,7 +94,9 @@ def bring_to_focus_method(self): horizontal_profile["y"] = y0 + BORDER_RANGE self.parent.horizontal_profile = horizontal_profile - is_horizontal_profile_enabled = self.parent.ui.enable_horizontal_profile_checkbox.isChecked() + is_horizontal_profile_enabled = ( + self.parent.ui.enable_horizontal_profile_checkbox.isChecked() + ) o_event = EventHandler(parent=self.parent) o_event.horizontal_profile(enabled=is_horizontal_profile_enabled) @@ -106,7 +113,9 @@ def bring_to_focus_method(self): vertical_profile["x"] = x0 + BORDER_RANGE self.parent.vertical_profile = vertical_profile - is_vertical_profile_enabled = self.parent.ui.enable_vertical_profile_checkbox.isChecked() + is_vertical_profile_enabled = ( + self.parent.ui.enable_vertical_profile_checkbox.isChecked() + ) o_event = EventHandler(parent=self.parent) o_event.vertical_profile(enabled=is_vertical_profile_enabled) diff --git a/notebooks/__code/panoramic_stitching/status_message_config.py b/notebooks/__code/panoramic_stitching/status_message_config.py index 50759a850..000ea7672 100755 --- a/notebooks/__code/panoramic_stitching/status_message_config.py +++ b/notebooks/__code/panoramic_stitching/status_message_config.py @@ -9,7 +9,9 @@ class StatusMessageStatus: warning = "QStatusBar{padding-left:8px;background:rgba(236,236,236,75);color:red;font-weight:normal;}" -def show_status_message(parent=None, message="", status=StatusMessageStatus.ready, duration_s=None): +def show_status_message( + parent=None, message="", status=StatusMessageStatus.ready, duration_s=None +): parent.ui.statusbar.setStyleSheet(status) if duration_s: parent.ui.statusbar.showMessage(message, duration_s * 1000) diff --git a/notebooks/__code/panoramic_stitching_OLD/config.py b/notebooks/__code/panoramic_stitching_OLD/config.py index 2397e0657..a6f56d921 100755 --- a/notebooks/__code/panoramic_stitching_OLD/config.py +++ b/notebooks/__code/panoramic_stitching_OLD/config.py @@ -1,10 +1,34 @@ button = { - "left": {"pressed": "left_arrow_v2_pressed.png", "released": "left_arrow_v2_released.png"}, - "right": {"pressed": "right_arrow_v2_pressed.png", "released": "right_arrow_v2_released.png"}, - "left_left": {"pressed": "left_left_arrow_v2_pressed.png", "released": "left_left_arrow_v2_released.png"}, - "right_right": {"pressed": "right_right_arrow_v2_pressed.png", "released": "right_right_arrow_v2_released.png"}, - "up": {"pressed": "up_arrow_v2_pressed.png", "released": "up_arrow_v2_released.png"}, - "down": {"pressed": "down_arrow_v2_pressed.png", "released": "down_arrow_v2_released.png"}, - "up_up": {"pressed": "up_up_arrow_v2_pressed.png", "released": "up_up_arrow_v2_released.png"}, - "down_down": {"pressed": "down_down_arrow_v2_pressed.png", "released": "down_down_arrow_v2_released.png"}, + "left": { + "pressed": "left_arrow_v2_pressed.png", + "released": "left_arrow_v2_released.png", + }, + "right": { + "pressed": "right_arrow_v2_pressed.png", + "released": "right_arrow_v2_released.png", + }, + "left_left": { + "pressed": "left_left_arrow_v2_pressed.png", + "released": "left_left_arrow_v2_released.png", + }, + "right_right": { + "pressed": "right_right_arrow_v2_pressed.png", + "released": "right_right_arrow_v2_released.png", + }, + "up": { + "pressed": "up_arrow_v2_pressed.png", + "released": "up_arrow_v2_released.png", + }, + "down": { + "pressed": "down_arrow_v2_pressed.png", + "released": "down_arrow_v2_released.png", + }, + "up_up": { + "pressed": "up_up_arrow_v2_pressed.png", + "released": "up_up_arrow_v2_released.png", + }, + "down_down": { + "pressed": "down_down_arrow_v2_pressed.png", + "released": "down_down_arrow_v2_released.png", + }, } diff --git a/notebooks/__code/panoramic_stitching_OLD/gui_initialization.py b/notebooks/__code/panoramic_stitching_OLD/gui_initialization.py index 81ee7ce17..e87de30d9 100755 --- a/notebooks/__code/panoramic_stitching_OLD/gui_initialization.py +++ b/notebooks/__code/panoramic_stitching_OLD/gui_initialization.py @@ -94,16 +94,22 @@ def table(self): # reference image _combobox_ref = QtGui.QComboBox() _combobox_ref.blockSignals(True) - _combobox_ref.currentIndexChanged.connect(self.parent.table_widget_reference_image_changed) + _combobox_ref.currentIndexChanged.connect( + self.parent.table_widget_reference_image_changed + ) _combobox_ref.addItems(self.parent.list_reference["basename_files"]) - _combobox_ref.setCurrentIndex(_dict_of_this_row["reference_combobox_file_index"]) + _combobox_ref.setCurrentIndex( + _dict_of_this_row["reference_combobox_file_index"] + ) _combobox_ref.blockSignals(False) self.parent.ui.tableWidget.setCellWidget(_row, 0, _combobox_ref) # target image _combobox = QtGui.QComboBox() _combobox.blockSignals(True) - _combobox.currentIndexChanged.connect(self.parent.table_widget_target_image_changed) + _combobox.currentIndexChanged.connect( + self.parent.table_widget_target_image_changed + ) _combobox.addItems(self.parent.list_target["basename_files"]) _combobox.setCurrentIndex(_dict_of_this_row["target_combobox_file_index"]) _combobox.blockSignals(False) @@ -126,38 +132,54 @@ def widgets(self): config.button["up_up"]["released"] ) self.parent.ui.up_up_button.setIcon(QtGui.QIcon(up_up_arrow_file)) - up_arrow_file = GuiInitialization.__make_full_file_name_to_static_folder_of(config.button["up"]["released"]) + up_arrow_file = GuiInitialization.__make_full_file_name_to_static_folder_of( + config.button["up"]["released"] + ) self.parent.ui.up_button.setIcon(QtGui.QIcon(up_arrow_file)) - left_left_arrow_file = GuiInitialization.__make_full_file_name_to_static_folder_of( - config.button["left_left"]["released"] + left_left_arrow_file = ( + GuiInitialization.__make_full_file_name_to_static_folder_of( + config.button["left_left"]["released"] + ) ) self.parent.ui.left_left_button.setIcon(QtGui.QIcon(left_left_arrow_file)) - left_arrow_file = GuiInitialization.__make_full_file_name_to_static_folder_of(config.button["left"]["released"]) + left_arrow_file = GuiInitialization.__make_full_file_name_to_static_folder_of( + config.button["left"]["released"] + ) self.parent.ui.left_button.setIcon(QtGui.QIcon(left_arrow_file)) right_arrow_file = GuiInitialization.__make_full_file_name_to_static_folder_of( config.button["right"]["released"] ) self.parent.ui.right_button.setIcon(QtGui.QIcon(right_arrow_file)) - right_right_arrow_file = GuiInitialization.__make_full_file_name_to_static_folder_of( - config.button["right_right"]["released"] + right_right_arrow_file = ( + GuiInitialization.__make_full_file_name_to_static_folder_of( + config.button["right_right"]["released"] + ) ) self.parent.ui.right_right_button.setIcon(QtGui.QIcon(right_right_arrow_file)) - down_arrow_file = GuiInitialization.__make_full_file_name_to_static_folder_of(config.button["down"]["released"]) + down_arrow_file = GuiInitialization.__make_full_file_name_to_static_folder_of( + config.button["down"]["released"] + ) self.parent.ui.down_button.setIcon(QtGui.QIcon(down_arrow_file)) - down_down_arrow_file = GuiInitialization.__make_full_file_name_to_static_folder_of( - config.button["down_down"]["released"] + down_down_arrow_file = ( + GuiInitialization.__make_full_file_name_to_static_folder_of( + config.button["down_down"]["released"] + ) ) self.parent.ui.down_down_button.setIcon(QtGui.QIcon(down_down_arrow_file)) list_ui = [self.parent.ui.left_button, self.parent.ui.right_button] GuiInitialization.__set_widgets_size( - widgets=list_ui, width=BUTTON_SIZE["single_arrow"]["width"], height=BUTTON_SIZE["single_arrow"]["height"] + widgets=list_ui, + width=BUTTON_SIZE["single_arrow"]["width"], + height=BUTTON_SIZE["single_arrow"]["height"], ) list_ui = [self.parent.ui.left_left_button, self.parent.ui.right_right_button] GuiInitialization.__set_widgets_size( - widgets=list_ui, width=BUTTON_SIZE["double_arrow"]["width"], height=BUTTON_SIZE["double_arrow"]["height"] + widgets=list_ui, + width=BUTTON_SIZE["double_arrow"]["width"], + height=BUTTON_SIZE["double_arrow"]["height"], ) list_ui = [self.parent.ui.up_button, self.parent.ui.down_button] @@ -210,10 +232,14 @@ def load_configuration(self): "width": int(roi_row_reference["width"]), "height": int(roi_row_reference["height"]), } - master_dict_row["reference_combobox_file_index"] = int(roi_row_reference["file_index"]) + master_dict_row["reference_combobox_file_index"] = int( + roi_row_reference["file_index"] + ) roi_row_target = configuration_roi_row["target"] - master_dict_row["target_combobox_file_index"] = int(roi_row_target["file_index"]) + master_dict_row["target_combobox_file_index"] = int( + roi_row_target["file_index"] + ) master_dict_row["target_roi"] = { "x0": int(roi_row_target["x0"]), "y0": int(roi_row_target["y0"]), diff --git a/notebooks/__code/panoramic_stitching_OLD/panoramic_stitching.py b/notebooks/__code/panoramic_stitching_OLD/panoramic_stitching.py index 436417bb8..5b94a206a 100755 --- a/notebooks/__code/panoramic_stitching_OLD/panoramic_stitching.py +++ b/notebooks/__code/panoramic_stitching_OLD/panoramic_stitching.py @@ -61,7 +61,9 @@ def __init__(self, parent=None, o_norm=None, configuration=""): self.o_norm = o_norm self.list_files = self.o_norm.data["sample"]["file_name"] - self.basename_list_files = [os.path.basename(_file) for _file in self.list_files] + self.basename_list_files = [ + os.path.basename(_file) for _file in self.list_files + ] self.list_data = self.o_norm.data["sample"]["data"] @@ -121,7 +123,12 @@ def save_roi_changed(self, data_type="reference"): height = np.max([y0, y1]) - y0 o_utilities.set_roi_to_master_dict( - row=row_selected, data_type=data_type, x0=x0, y0=y0, width=width, height=height + row=row_selected, + data_type=data_type, + x0=x0, + y0=y0, + width=width, + height=height, ) # we need to make sure the target roi has the proper size @@ -141,8 +148,12 @@ def table_widget_selection_changed(self): row_selected = o_utilities.get_reference_selected(key="index") # +1 because the target file starts at the second file - target_file_index_selected = o_utilities.get_target_index_selected_from_row(row=row_selected) - reference_file_index_selected = o_utilities.get_reference_index_selected_from_row(row=row_selected) + target_file_index_selected = o_utilities.get_target_index_selected_from_row( + row=row_selected + ) + reference_file_index_selected = ( + o_utilities.get_reference_index_selected_from_row(row=row_selected) + ) reference_data = self.list_reference["data"][reference_file_index_selected] target_data = self.list_target["data"][target_file_index_selected] @@ -183,7 +194,9 @@ def display_data(self, data_type="reference", data=[]): _view_box.setState(_state) if not first_update: - _histo_widget.setLevels(self.histogram_level[data_type][0], self.histogram_level[data_type][1]) + _histo_widget.setLevels( + self.histogram_level[data_type][0], self.histogram_level[data_type][1] + ) def display_roi(self, data_type="reference"): o_utilities = Utilities(parent=self) @@ -231,7 +244,9 @@ def check_status_of_stitching_button(self): continue if _target_file in list_target_file: - o_utilities.set_status_of_this_row_to_message(row=_row, message="Already used!") + o_utilities.set_status_of_this_row_to_message( + row=_row, message="Already used!" + ) list_target_file.add(_target_file) if len(list_target_file) == len(self.list_target["files"]) - 1: diff --git a/notebooks/__code/panoramic_stitching_OLD/stiching.py b/notebooks/__code/panoramic_stitching_OLD/stiching.py index 325ad74b2..55542ac05 100755 --- a/notebooks/__code/panoramic_stitching_OLD/stiching.py +++ b/notebooks/__code/panoramic_stitching_OLD/stiching.py @@ -16,16 +16,28 @@ def run_fft(self): o_utilities = Utilities(parent=self.parent) for _row in master_dict.keys(): - _data_reference = o_utilities.get_image_for_this_row(data_type="reference", row=_row) - _data_target = o_utilities.get_image_for_this_row(data_type="target", row=_row) + _data_reference = o_utilities.get_image_for_this_row( + data_type="reference", row=_row + ) + _data_target = o_utilities.get_image_for_this_row( + data_type="target", row=_row + ) reference_roi = master_dict[_row]["reference_roi"] - [ref_x0, ref_y0, ref_width, ref_height] = Stitching.retrieve_roi_parameters(roi_dict=reference_roi) + [ref_x0, ref_y0, ref_width, ref_height] = Stitching.retrieve_roi_parameters( + roi_dict=reference_roi + ) target_roi = master_dict[_row]["target_roi"] - [target_x0, target_y0, _, _] = Stitching.retrieve_roi_parameters(roi_dict=target_roi) + [target_x0, target_y0, _, _] = Stitching.retrieve_roi_parameters( + roi_dict=target_roi + ) - _data_reference_roi = _data_reference[ref_y0 : ref_y0 + ref_height, ref_x0 : ref_x0 + ref_width] - _data_target_roi = _data_target[target_y0 : target_y0 + ref_height, target_x0 : target_x0 + ref_width] + _data_reference_roi = _data_reference[ + ref_y0 : ref_y0 + ref_height, ref_x0 : ref_x0 + ref_width + ] + _data_target_roi = _data_target[ + target_y0 : target_y0 + ref_height, target_x0 : target_x0 + ref_width + ] f_reference = np.fft.fft2(_data_reference_roi) f_target = np.fft.fft2(_data_target_roi) @@ -37,7 +49,10 @@ def run_fft(self): optimum_x0 = pos[1][0] optimum_y0 = pos[0][0] - displacement = {"x": target_x0 - ref_x0 + optimum_x0, "y": target_y0 - ref_y0 + optimum_y0} + displacement = { + "x": target_x0 - ref_x0 + optimum_x0, + "y": target_y0 - ref_y0 + optimum_y0, + } master_dict[_row]["displacement"]["x"] = displacement["x"] master_dict[_row]["displacement"]["y"] = displacement["y"] @@ -56,14 +71,18 @@ def run(self): _data_target = list_target_file["data"][_target_file_index] reference_roi = master_dict[_row]["reference_roi"] - [ref_x0, ref_y0, ref_width, ref_height] = Stitching.retrieve_roi_parameters(roi_dict=reference_roi) + [ref_x0, ref_y0, ref_width, ref_height] = Stitching.retrieve_roi_parameters( + roi_dict=reference_roi + ) target_roi = master_dict[_row]["target_roi"] - [starting_target_x0, starting_target_y0, target_width, target_height] = Stitching.retrieve_roi_parameters( - roi_dict=target_roi + [starting_target_x0, starting_target_y0, target_width, target_height] = ( + Stitching.retrieve_roi_parameters(roi_dict=target_roi) ) - _data_reference_of_roi = _data_reference[ref_y0 : ref_y0 + ref_height, ref_x0 : ref_x0 + ref_width] + _data_reference_of_roi = _data_reference[ + ref_y0 : ref_y0 + ref_height, ref_x0 : ref_x0 + ref_width + ] # where to start from moving_target_x0 = starting_target_x0 @@ -82,11 +101,15 @@ def run(self): print("Reference:") print(f"x0:{ref_x0}, y0:{ref_y0}, width:{ref_width}, height:{ref_height}") print("target:") - print(f"x0:{starting_target_x0}, y0:{starting_target_y0}, width:{target_width}, height:{target_height}") + print( + f"x0:{starting_target_x0}, y0:{starting_target_y0}, width:{target_width}, height:{target_height}" + ) if DEBUG_JSON: o_utilities = Utilities(parent=self.parent) - _reference_file_index = o_utilities.get_reference_index_selected_from_row(row=_row) + _reference_file_index = ( + o_utilities.get_reference_index_selected_from_row(row=_row) + ) roi_to_export[str(_row)] = { "reference": { @@ -108,13 +131,19 @@ def run(self): counts_and_x0_position_dict = defaultdict(list) counts_and_y0_position_dict = defaultdict(list) - counts_3d = np.zeros((final_target_y0 - moving_target_y0 + 1, final_target_x0 - moving_target_x0 + 1)) + counts_3d = np.zeros( + ( + final_target_y0 - moving_target_y0 + 1, + final_target_x0 - moving_target_x0 + 1, + ) + ) x = 0 y = 0 while moving_target_y0 <= final_target_y0: _data_target_of_roi = _data_target[ - moving_target_y0 : moving_target_y0 + ref_height, moving_target_x0 : moving_target_x0 + ref_width + moving_target_y0 : moving_target_y0 + ref_height, + moving_target_x0 : moving_target_x0 + ref_width, ] _diff_array = np.abs(_data_target_of_roi - _data_reference_of_roi) @@ -149,7 +178,8 @@ def run(self): self.parent.debug_big_array_roi_ref = _data_reference_of_roi self.parent.debug_big_array_roi_target = _data_target[ - optimum_y0 : optimum_y0 + ref_height, optimum_x0 : optimum_x0 + ref_width + optimum_y0 : optimum_y0 + ref_height, + optimum_x0 : optimum_x0 + ref_width, ] if DEBUG_JSON: diff --git a/notebooks/__code/panoramic_stitching_OLD/utilities.py b/notebooks/__code/panoramic_stitching_OLD/utilities.py index d7e7db73f..0ef5a96e1 100755 --- a/notebooks/__code/panoramic_stitching_OLD/utilities.py +++ b/notebooks/__code/panoramic_stitching_OLD/utilities.py @@ -22,7 +22,15 @@ def get_roi_from_master_dict(self, full_file_name=""): height = master_dict["height"] return [x0, y0, width, height] - def set_roi_to_master_dict(self, row=0, data_type="reference", x0=None, y0=None, width=np.nan, height=np.nan): + def set_roi_to_master_dict( + self, + row=0, + data_type="reference", + x0=None, + y0=None, + width=np.nan, + height=np.nan, + ): roi_key = f"{data_type}_roi" roi_dict = self.parent.master_dict[row][roi_key] if x0: @@ -54,7 +62,9 @@ def get_reference_selected(self, key="files"): def get_image_for_this_row(self, data_type="reference", row=0): if data_type == "reference": - combobox_index_selected = self.get_reference_index_selected_from_row(row=row) + combobox_index_selected = self.get_reference_index_selected_from_row( + row=row + ) else: combobox_index_selected = self.get_target_index_selected_from_row(row=row) return self.parent.list_reference["data"][combobox_index_selected] @@ -99,12 +109,16 @@ def reset_all_status(self): @staticmethod def button_pressed(ui=None, name="left"): - full_file = Utilities.__make_full_file_name_to_static_folder_of(config.button[name]["pressed"]) + full_file = Utilities.__make_full_file_name_to_static_folder_of( + config.button[name]["pressed"] + ) ui.setIcon(QtGui.QIcon(full_file)) @staticmethod def button_released(ui=None, name="left"): - full_file = Utilities.__make_full_file_name_to_static_folder_of(config.button[name]["released"]) + full_file = Utilities.__make_full_file_name_to_static_folder_of( + config.button[name]["released"] + ) ui.setIcon(QtGui.QIcon(full_file)) @staticmethod diff --git a/notebooks/__code/panoramic_stitching_for_tof/automatically_stitch.py b/notebooks/__code/panoramic_stitching_for_tof/automatically_stitch.py index 50e6faedd..ac9b70498 100755 --- a/notebooks/__code/panoramic_stitching_for_tof/automatically_stitch.py +++ b/notebooks/__code/panoramic_stitching_for_tof/automatically_stitch.py @@ -21,7 +21,9 @@ def run(self): group_selected = o_get.get_combobox_folder_selected() # first calculate the long and lift position versus pixel coefficient from the ref. group - group_reference_offset_dictionary = self.parent.offset_dictionary[group_selected] + group_reference_offset_dictionary = self.parent.offset_dictionary[ + group_selected + ] group_reference_data_dictionary = self.parent.data_dictionary[group_selected] list_files = group_reference_offset_dictionary.keys() @@ -29,8 +31,12 @@ def run(self): list_pixel_vs_motor_lift_axis_value = [] for _file_index, _file in enumerate(list_files): - long_axis_value = group_reference_data_dictionary[_file].metadata["MotLongAxis.RBV"] - lift_axis_value = group_reference_data_dictionary[_file].metadata["MotLiftTable.RBV"] + long_axis_value = group_reference_data_dictionary[_file].metadata[ + "MotLongAxis.RBV" + ] + lift_axis_value = group_reference_data_dictionary[_file].metadata[ + "MotLiftTable.RBV" + ] if _file_index == 0: long_axis_reference_value = long_axis_value @@ -75,15 +81,29 @@ def run(self): # get xoffset and yofffset pixel/motor position of each image of reference group for _file_index, _file in enumerate(list_files): if _file_index == 0: - long_axis_value_image_reference = data_dictionary[_file].metadata["MotLongAxis.RBV"] - lift_axis_value_image_reference = data_dictionary[_file].metadata["MotLiftTable.RBV"] + long_axis_value_image_reference = data_dictionary[_file].metadata[ + "MotLongAxis.RBV" + ] + lift_axis_value_image_reference = data_dictionary[_file].metadata[ + "MotLiftTable.RBV" + ] continue - long_axis_value = data_dictionary[_file].metadata["MotLongAxis.RBV"] - long_axis_value_image_reference - lift_axis_value = data_dictionary[_file].metadata["MotLiftTable.RBV"] - lift_axis_value_image_reference - - xoffset_of_this_file = int(long_axis_value * list_pixel_vs_motor_long_axis_value[_file_index]) - yoffset_of_this_file = int(lift_axis_value * list_pixel_vs_motor_lift_axis_value[_file_index]) + long_axis_value = ( + data_dictionary[_file].metadata["MotLongAxis.RBV"] + - long_axis_value_image_reference + ) + lift_axis_value = ( + data_dictionary[_file].metadata["MotLiftTable.RBV"] + - lift_axis_value_image_reference + ) + + xoffset_of_this_file = int( + long_axis_value * list_pixel_vs_motor_long_axis_value[_file_index] + ) + yoffset_of_this_file = int( + lift_axis_value * list_pixel_vs_motor_lift_axis_value[_file_index] + ) group_offset_dictionary[_file]["xoffset"] = xoffset_of_this_file group_offset_dictionary[_file]["yoffset"] = yoffset_of_this_file diff --git a/notebooks/__code/panoramic_stitching_for_tof/best_contrast_tab_handler.py b/notebooks/__code/panoramic_stitching_for_tof/best_contrast_tab_handler.py index 8a5c23d89..345e201d8 100755 --- a/notebooks/__code/panoramic_stitching_for_tof/best_contrast_tab_handler.py +++ b/notebooks/__code/panoramic_stitching_for_tof/best_contrast_tab_handler.py @@ -13,7 +13,9 @@ def __init__(self, parent=None): self.parent = parent def display_selected_folder(self): - folder_name = os.path.basename(self.parent.ui.list_folders_combobox.currentText()) + folder_name = os.path.basename( + self.parent.ui.list_folders_combobox.currentText() + ) if self.parent.ui.raw_image_radioButton.isChecked(): image = self.parent.integrated_images[folder_name].data @@ -24,7 +26,9 @@ def display_selected_folder(self): # histogram_level = self.parent.histogram_level_best_contrast status_best_contrast_button = True - self.parent.ui.best_contrast_bin_size_value.setEnabled(status_best_contrast_button) + self.parent.ui.best_contrast_bin_size_value.setEnabled( + status_best_contrast_button + ) self.parent.ui.bin_size_label.setEnabled(status_best_contrast_button) # _view = self.parent.ui.image_view_best_contrast.getView() @@ -98,7 +102,9 @@ def calculate_best_contrast(self): list_mean_counts_of_bin = [] while left_bin_index < len(list_bin) - 1: - data_bin = all_data_of_folder[list_bin[left_bin_index] : list_bin[left_bin_index + 1]] + data_bin = all_data_of_folder[ + list_bin[left_bin_index] : list_bin[left_bin_index + 1] + ] mean_data_bin = np.nanmean(data_bin) list_mean_counts_of_bin.append(mean_data_bin) left_bin_index += 1 @@ -107,7 +113,8 @@ def calculate_best_contrast(self): for _bin_index_numerator in np.arange(len(list_bin) - 1): for _bin_index_denominator in np.arange(len(list_bin) - 1): bin_ratio = ( - list_mean_counts_of_bin[_bin_index_numerator] / list_mean_counts_of_bin[_bin_index_denominator] + list_mean_counts_of_bin[_bin_index_numerator] + / list_mean_counts_of_bin[_bin_index_denominator] ) diff_with_1 = np.abs(1 - bin_ratio) @@ -126,12 +133,16 @@ def calculate_best_contrast(self): all_data_of_folder = all_data[folder_key] image1 = all_data_of_folder[ - list_bin[best_bin_index["numerator"]] : list_bin[best_bin_index["numerator"] + 1] + list_bin[best_bin_index["numerator"]] : list_bin[ + best_bin_index["numerator"] + 1 + ] ] image_numerator_mean = np.mean(image1, axis=0) image2 = all_data_of_folder[ - list_bin[best_bin_index["denominator"]] : list_bin[best_bin_index["denominator"] + 1] + list_bin[best_bin_index["denominator"]] : list_bin[ + best_bin_index["denominator"] + 1 + ] ] image_denominator_mean = np.mean(image2, axis=0) @@ -151,5 +162,7 @@ def calculate_best_contrast(self): self.parent.eventProgress.setVisible(False) self.parent.ui.best_contrast_image_radioButton.setEnabled(True) - self.parent.ui.statusbar.showMessage("Done calculating the best contrast images!", 10000) + self.parent.ui.statusbar.showMessage( + "Done calculating the best contrast images!", 10000 + ) QApplication.restoreOverrideCursor() diff --git a/notebooks/__code/panoramic_stitching_for_tof/coarse_tab_handler.py b/notebooks/__code/panoramic_stitching_for_tof/coarse_tab_handler.py index 03a35db1e..842f98685 100755 --- a/notebooks/__code/panoramic_stitching_for_tof/coarse_tab_handler.py +++ b/notebooks/__code/panoramic_stitching_for_tof/coarse_tab_handler.py @@ -25,7 +25,9 @@ def initialize_table(self): o_table.insert_empty_column(_col) _widget = QComboBox() _widget.addItems(short_list_folders) - _widget.currentIndexChanged.connect(self.parent.coarse_alignment_table_combobox_changed) + _widget.currentIndexChanged.connect( + self.parent.coarse_alignment_table_combobox_changed + ) o_table.insert_widget(row=_row, column=_col, widget=_widget) column_width = [200 for _ in np.arange(nbr_column)] @@ -63,8 +65,12 @@ def combobox_changed(self): panoramic_height = height # number of empty rows and columns before first file selected - nbr_empty_rows = self.get_number_of_empty_rows_from_top(nbr_row=nbr_row, nbr_column=nbr_column) - nbr_empty_columns = self.get_number_of_empty_columns_from_left(nbr_row=nbr_row, nbr_column=nbr_column) + nbr_empty_rows = self.get_number_of_empty_rows_from_top( + nbr_row=nbr_row, nbr_column=nbr_column + ) + nbr_empty_columns = self.get_number_of_empty_columns_from_left( + nbr_row=nbr_row, nbr_column=nbr_column + ) panoramic_image = np.zeros((panoramic_height, panoramic_width)) for _row in np.arange(nbr_row): diff --git a/notebooks/__code/panoramic_stitching_for_tof/data_initialization.py b/notebooks/__code/panoramic_stitching_for_tof/data_initialization.py index 6fbcaf510..584624df4 100755 --- a/notebooks/__code/panoramic_stitching_for_tof/data_initialization.py +++ b/notebooks/__code/panoramic_stitching_for_tof/data_initialization.py @@ -28,8 +28,12 @@ def offset_table(self): o_coarse = CoarseTabHandler(parent=self.parent) nbr_row = self.parent.ui.coarse_alignment_tableWidget.rowCount() nbr_column = self.parent.ui.coarse_alignment_tableWidget.columnCount() - nbr_empty_rows = o_coarse.get_number_of_empty_rows_from_top(nbr_row=nbr_row, nbr_column=nbr_column) - nbr_empty_columns = o_coarse.get_number_of_empty_columns_from_left(nbr_row=nbr_row, nbr_column=nbr_column) + nbr_empty_rows = o_coarse.get_number_of_empty_rows_from_top( + nbr_row=nbr_row, nbr_column=nbr_column + ) + nbr_empty_columns = o_coarse.get_number_of_empty_columns_from_left( + nbr_row=nbr_row, nbr_column=nbr_column + ) nbr_row = nbr_column = nbr_folders for _row in np.arange(nbr_row): @@ -39,7 +43,11 @@ def offset_table(self): if not folder_name == "": xoffset = (_column - nbr_empty_columns) * image_width yoffset = (_row - nbr_empty_rows) * image_height - _offset_dict = {"xoffset": xoffset, "yoffset": yoffset, "visible": True} + _offset_dict = { + "xoffset": xoffset, + "yoffset": yoffset, + "visible": True, + } offset_dictionary[folder_name] = _offset_dict self.parent.offset_dictionary = offset_dictionary diff --git a/notebooks/__code/panoramic_stitching_for_tof/event_handler.py b/notebooks/__code/panoramic_stitching_for_tof/event_handler.py index c3c719469..cef0a3bf7 100755 --- a/notebooks/__code/panoramic_stitching_for_tof/event_handler.py +++ b/notebooks/__code/panoramic_stitching_for_tof/event_handler.py @@ -42,7 +42,9 @@ def check_validate_coarse_alignment_button(self): if len(list_of_files) == 0: error_message = "Select the position of the images!" validate_button = False - elif (len(list_of_files) == len(list_folders)) and (len(set(list_of_files)) == len(list_folders)): + elif (len(list_of_files) == len(list_folders)) and ( + len(set(list_of_files)) == len(list_folders) + ): validate_button = True else: validate_button = False diff --git a/notebooks/__code/panoramic_stitching_for_tof/export.py b/notebooks/__code/panoramic_stitching_for_tof/export.py index dc86ed55b..08416da5a 100755 --- a/notebooks/__code/panoramic_stitching_for_tof/export.py +++ b/notebooks/__code/panoramic_stitching_for_tof/export.py @@ -8,7 +8,10 @@ from qtpy.QtWidgets import QFileDialog from __code.file_handler import copy_and_rename_files_to_folder, make_or_reset_folder -from __code.panoramic_stitching_for_tof.image_handler import HORIZONTAL_MARGIN, VERTICAL_MARGIN +from __code.panoramic_stitching_for_tof.image_handler import ( + HORIZONTAL_MARGIN, + VERTICAL_MARGIN, +) FILE_PREFIX = "image_" @@ -21,7 +24,8 @@ def run(self): output_folder = QFileDialog.getExistingDirectory( self.parent, directory=self.parent.working_dir, - caption="Select where the folder containing the " "panoramic images will be created!", + caption="Select where the folder containing the " + "panoramic images will be created!", options=QFileDialog.ShowDirsOnly, ) if output_folder: @@ -63,26 +67,39 @@ def create_panoramic_images(self, output_folder=None): if _folder_index == 0: panoramic_image[ - yoffset + VERTICAL_MARGIN : yoffset + image_height + VERTICAL_MARGIN, - xoffset + HORIZONTAL_MARGIN : xoffset + image_width + HORIZONTAL_MARGIN, + yoffset + VERTICAL_MARGIN : yoffset + + image_height + + VERTICAL_MARGIN, + xoffset + HORIZONTAL_MARGIN : xoffset + + image_width + + HORIZONTAL_MARGIN, ] = image continue temp_big_image = np.zeros((panoramic_height, panoramic_width)) temp_big_image[ - yoffset + VERTICAL_MARGIN : yoffset + image_height + VERTICAL_MARGIN, - xoffset + HORIZONTAL_MARGIN : xoffset + image_width + HORIZONTAL_MARGIN, + yoffset + VERTICAL_MARGIN : yoffset + + image_height + + VERTICAL_MARGIN, + xoffset + HORIZONTAL_MARGIN : xoffset + + image_width + + HORIZONTAL_MARGIN, ] = image - where_temp_big_image_has_value_only = np.where((temp_big_image != 0) & (panoramic_image == 0)) - where_both_images_overlap = np.where((panoramic_image != 0) & (temp_big_image != 0)) + where_temp_big_image_has_value_only = np.where( + (temp_big_image != 0) & (panoramic_image == 0) + ) + where_both_images_overlap = np.where( + (panoramic_image != 0) & (temp_big_image != 0) + ) panoramic_image[where_temp_big_image_has_value_only] = temp_big_image[ where_temp_big_image_has_value_only ] panoramic_image[where_both_images_overlap] = ( - panoramic_image[where_both_images_overlap] + temp_big_image[where_both_images_overlap] + panoramic_image[where_both_images_overlap] + + temp_big_image[where_both_images_overlap] ) / 2 file_name = FILE_PREFIX + f"{_file_index:04d}.tiff" @@ -96,7 +113,9 @@ def create_panoramic_images(self, output_folder=None): def export_images(self, output_folder=None): new_folder_name = os.path.basename(self.parent.working_dir) + "_panoramic" - self.parent.ui.statusbar.showMessage(f"Exporting images in folder {new_folder_name}") + self.parent.ui.statusbar.showMessage( + f"Exporting images in folder {new_folder_name}" + ) QtGui.QGuiApplication.processEvents() new_output_folder_name = os.path.join(output_folder, new_folder_name) @@ -117,7 +136,9 @@ def export_images(self, output_folder=None): self.copy_txt_files_to_output_folder(output_folder=new_output_folder_name) - self.parent.ui.statusbar.showMessage(f"{new_output_folder_name} has been created!", 10000) # 10s + self.parent.ui.statusbar.showMessage( + f"{new_output_folder_name} has been created!", 10000 + ) # 10s QtGui.QGuiApplication.processEvents() def copy_txt_files_to_output_folder(self, output_folder=None): @@ -133,5 +154,7 @@ def copy_txt_files_to_output_folder(self, output_folder=None): list_new_file_names.append(new_name) copy_and_rename_files_to_folder( - list_files=list_txt_files, new_list_files_names=list_new_file_names, output_folder=output_folder + list_files=list_txt_files, + new_list_files_names=list_new_file_names, + output_folder=output_folder, ) diff --git a/notebooks/__code/panoramic_stitching_for_tof/fine_tab_handler.py b/notebooks/__code/panoramic_stitching_for_tof/fine_tab_handler.py index ec1ac54b0..d7e7a7895 100755 --- a/notebooks/__code/panoramic_stitching_for_tof/fine_tab_handler.py +++ b/notebooks/__code/panoramic_stitching_for_tof/fine_tab_handler.py @@ -36,11 +36,18 @@ def initialize_table_of_offset(self): else: editable_flag = editable_columns_boolean[_column_index] - o_table.insert_item(row=_row_index, column=_column_index, value=_text, editable=editable_flag) + o_table.insert_item( + row=_row_index, + column=_column_index, + value=_text, + editable=editable_flag, + ) # checkbox to turn on/off the visibility of the row hori_layout = QHBoxLayout() - spacer_item_left = QSpacerItem(408, 20, QSizePolicy.Expanding, QSizePolicy.Expanding) + spacer_item_left = QSpacerItem( + 408, 20, QSizePolicy.Expanding, QSizePolicy.Expanding + ) hori_layout.addItem(spacer_item_left) check_box = QCheckBox() if offset_entry["visible"]: @@ -50,10 +57,15 @@ def initialize_table_of_offset(self): check_box.setCheckState(_state) check_box.stateChanged.connect( - lambda state=0, row=_row_index: self.parent.visibility_checkbox_changed(state=state, row=row) + lambda state=0, + row=_row_index: self.parent.visibility_checkbox_changed( + state=state, row=row + ) ) hori_layout.addWidget(check_box) - spacer_item_right = QSpacerItem(408, 20, QSizePolicy.Expanding, QSizePolicy.Expanding) + spacer_item_right = QSpacerItem( + 408, 20, QSizePolicy.Expanding, QSizePolicy.Expanding + ) hori_layout.addItem(spacer_item_right) cell_widget = QWidget() cell_widget.setLayout(hori_layout) @@ -106,7 +118,9 @@ def roi_box_changed( self, roi_id=None, ): - region = roi_id.getArraySlice(self.parent.current_live_image, self.parent.ui.image_view.imageItem) + region = roi_id.getArraySlice( + self.parent.current_live_image, self.parent.ui.image_view.imageItem + ) x0 = region[0][0].start y0 = region[0][1].start @@ -143,13 +157,17 @@ def from_to_button_pushed(self): o_table = TableHandler(table_ui=self.parent.ui.tableWidget) row_selected = o_table.get_row_selected() - current_xoffset_of_selected_row = int(o_table.get_item_str_from_cell(row=row_selected, column=1)) + current_xoffset_of_selected_row = int( + o_table.get_item_str_from_cell(row=row_selected, column=1) + ) new_xoffset = int(current_xoffset_of_selected_row - delta_x) self.parent.ui.tableWidget.item(row_selected, 1).setText(str(new_xoffset)) o_event = TOFEventHandler(parent=self.parent) o_event.save_table_offset_of_this_cell(row=row_selected, column=1) - current_yoffset_of_selected_row = int(o_table.get_item_str_from_cell(row=row_selected, column=2)) + current_yoffset_of_selected_row = int( + o_table.get_item_str_from_cell(row=row_selected, column=2) + ) new_yoffset = current_yoffset_of_selected_row - delta_y self.parent.ui.tableWidget.item(row_selected, 2).setText(str(new_yoffset)) o_event.save_table_offset_of_this_cell(row=row_selected, column=2) diff --git a/notebooks/__code/panoramic_stitching_for_tof/gui_initialization.py b/notebooks/__code/panoramic_stitching_for_tof/gui_initialization.py index 19ba16553..90988f864 100755 --- a/notebooks/__code/panoramic_stitching_for_tof/gui_initialization.py +++ b/notebooks/__code/panoramic_stitching_for_tof/gui_initialization.py @@ -12,7 +12,10 @@ from __code.panoramic_stitching.config_buttons import button from __code.panoramic_stitching.gui_handler import GuiHandler from __code.panoramic_stitching.mplcanvas import MplCanvas -from __code.panoramic_stitching.utilities import make_full_file_name_to_static_folder_of, set_widgets_size +from __code.panoramic_stitching.utilities import ( + make_full_file_name_to_static_folder_of, + set_widgets_size, +) from __code.panoramic_stitching_for_tof.coarse_tab_handler import CoarseTabHandler @@ -56,7 +59,9 @@ def splitter(self): def pyqtgraph(self): # calculate best contrast images _view1 = pg.PlotItem() - self.parent.ui.image_view_best_contrast = pg.ImageView(view=_view1, name="view1") + self.parent.ui.image_view_best_contrast = pg.ImageView( + view=_view1, name="view1" + ) self.parent.ui.image_view_best_contrast.ui.roiBtn.hide() self.parent.ui.image_view_best_contrast.ui.menuBtn.hide() image_layout_best_contrast = QVBoxLayout() @@ -69,7 +74,9 @@ def pyqtgraph(self): self.parent.ui.image_view_coarse_alignment.ui.roiBtn.hide() self.parent.ui.image_view_coarse_alignment.ui.menuBtn.hide() image_layout_coarse_alignment = QVBoxLayout() - image_layout_coarse_alignment.addWidget(self.parent.ui.image_view_coarse_alignment) + image_layout_coarse_alignment.addWidget( + self.parent.ui.image_view_coarse_alignment + ) self.parent.ui.coarse_alignment_widget.setLayout(image_layout_coarse_alignment) # stitch images @@ -110,22 +117,38 @@ def widgets(self): # move buttons _file_path = os.path.dirname(__file__) - up_up_arrow_file = make_full_file_name_to_static_folder_of(button["up_up"]["released"]) + up_up_arrow_file = make_full_file_name_to_static_folder_of( + button["up_up"]["released"] + ) self.parent.ui.up_up_button.setIcon(QIcon(up_up_arrow_file)) - up_arrow_file = make_full_file_name_to_static_folder_of(button["up"]["released"]) + up_arrow_file = make_full_file_name_to_static_folder_of( + button["up"]["released"] + ) self.parent.ui.up_button.setIcon(QIcon(up_arrow_file)) - left_left_arrow_file = make_full_file_name_to_static_folder_of(button["left_left"]["released"]) + left_left_arrow_file = make_full_file_name_to_static_folder_of( + button["left_left"]["released"] + ) self.parent.ui.left_left_button.setIcon(QIcon(left_left_arrow_file)) - left_arrow_file = make_full_file_name_to_static_folder_of(button["left"]["released"]) + left_arrow_file = make_full_file_name_to_static_folder_of( + button["left"]["released"] + ) self.parent.ui.left_button.setIcon(QIcon(left_arrow_file)) - right_arrow_file = make_full_file_name_to_static_folder_of(button["right"]["released"]) + right_arrow_file = make_full_file_name_to_static_folder_of( + button["right"]["released"] + ) self.parent.ui.right_button.setIcon(QIcon(right_arrow_file)) - right_right_arrow_file = make_full_file_name_to_static_folder_of(button["right_right"]["released"]) + right_right_arrow_file = make_full_file_name_to_static_folder_of( + button["right_right"]["released"] + ) self.parent.ui.right_right_button.setIcon(QIcon(right_right_arrow_file)) - down_arrow_file = make_full_file_name_to_static_folder_of(button["down"]["released"]) + down_arrow_file = make_full_file_name_to_static_folder_of( + button["down"]["released"] + ) self.parent.ui.down_button.setIcon(QIcon(down_arrow_file)) - down_down_arrow_file = make_full_file_name_to_static_folder_of(button["down_down"]["released"]) + down_down_arrow_file = make_full_file_name_to_static_folder_of( + button["down_down"]["released"] + ) self.parent.ui.down_down_button.setIcon(QIcon(down_down_arrow_file)) list_ui = [self.parent.ui.left_button, self.parent.ui.right_button] @@ -156,14 +179,21 @@ def widgets(self): height=self.button_size["double_vertical_arrow"]["height"], ) - state_hori_matplotlib = self.parent.ui.enable_horizontal_profile_checkbox.isChecked() + state_hori_matplotlib = ( + self.parent.ui.enable_horizontal_profile_checkbox.isChecked() + ) o_gui = GuiHandler(parent=self.parent) o_gui.enabled_horizontal_profile_widgets(enabled=state_hori_matplotlib) - state_verti_matplotlib = self.parent.ui.enable_vertical_profile_checkbox.isChecked() + state_verti_matplotlib = ( + self.parent.ui.enable_vertical_profile_checkbox.isChecked() + ) o_gui.enabled_vertical_profile_widgets(enabled=state_verti_matplotlib) - profile_sliders = [self.parent.ui.horizontal_profile_width_slider, self.parent.ui.vertical_profile_width_slider] + profile_sliders = [ + self.parent.ui.horizontal_profile_width_slider, + self.parent.ui.vertical_profile_width_slider, + ] for _slider in profile_sliders: _slider.setMinimum(self.parent.width_profile["min"]) _slider.setMaximum(self.parent.width_profile["max"]) @@ -187,7 +217,10 @@ def after_loading_data(self): self.parent.best_contrast_list_folders_combobox_changed() # bin size of best contrast (nbr of images / 100 by default) - bin_size = int(self.parent.nbr_files_per_folder / self.parent.default_best_contrast_bin_size_divider) + bin_size = int( + self.parent.nbr_files_per_folder + / self.parent.default_best_contrast_bin_size_divider + ) self.parent.ui.best_contrast_bin_size_value.setText(str(bin_size)) self.parent.bin_size_text_field_return_pressed() diff --git a/notebooks/__code/panoramic_stitching_for_tof/image_handler.py b/notebooks/__code/panoramic_stitching_for_tof/image_handler.py index cd273a7f4..22dfd5f46 100755 --- a/notebooks/__code/panoramic_stitching_for_tof/image_handler.py +++ b/notebooks/__code/panoramic_stitching_for_tof/image_handler.py @@ -25,7 +25,9 @@ def update_contour_plot(self): o_table = TableHandler(table_ui=self.parent.ui.tableWidget) row_selected = o_table.get_row_selected() - name_of_file_selected = o_table.get_item_str_from_cell(row=row_selected, column=0) + name_of_file_selected = o_table.get_item_str_from_cell( + row=row_selected, column=0 + ) offset_dictionary = self.parent.offset_dictionary @@ -102,8 +104,12 @@ def update_current_panoramic_image(self): yoffset = offset_dictionary[_folder]["yoffset"] panoramic_image[ - yoffset + VERTICAL_MARGIN : yoffset + image_height + VERTICAL_MARGIN, - xoffset + HORIZONTAL_MARGIN : xoffset + image_width + HORIZONTAL_MARGIN, + yoffset + VERTICAL_MARGIN : yoffset + + image_height + + VERTICAL_MARGIN, + xoffset + HORIZONTAL_MARGIN : xoffset + + image_width + + HORIZONTAL_MARGIN, ] = _image self.parent.panoramic_images = panoramic_image @@ -115,13 +121,19 @@ def update_current_panoramic_image(self): _view_box.setState(_state) if not first_update: - _histo_widget.setLevels(self.parent.histogram_level[0], self.parent.histogram_level[1]) + _histo_widget.setLevels( + self.parent.histogram_level[0], self.parent.histogram_level[1] + ) def get_max_offset(self): offset_dictionary = self.parent.offset_dictionary - list_xoffset = [offset_dictionary[_key]["xoffset"] for _key in offset_dictionary.keys()] - list_yoffset = [offset_dictionary[_key]["yoffset"] for _key in offset_dictionary.keys()] + list_xoffset = [ + offset_dictionary[_key]["xoffset"] for _key in offset_dictionary.keys() + ] + list_yoffset = [ + offset_dictionary[_key]["yoffset"] for _key in offset_dictionary.keys() + ] return int(np.max(list_yoffset)), int(np.max(list_xoffset)) @@ -138,16 +150,24 @@ def update_from_to_roi(self, state=False): from_roi = self.parent.from_roi x = from_roi["x"] y = from_roi["y"] - self.parent.from_roi_id = pg.ROI([x, y], [ROI_WIDTH, ROI_HEIGHT], scaleSnap=True) + self.parent.from_roi_id = pg.ROI( + [x, y], [ROI_WIDTH, ROI_HEIGHT], scaleSnap=True + ) self.parent.ui.image_view.addItem(self.parent.from_roi_id) - self.parent.from_roi_id.sigRegionChanged.connect(self.parent.from_roi_box_changed) + self.parent.from_roi_id.sigRegionChanged.connect( + self.parent.from_roi_box_changed + ) to_roi = self.parent.to_roi x = to_roi["x"] y = to_roi["y"] - self.parent.to_roi_id = pg.ROI([x, y], [ROI_WIDTH, ROI_HEIGHT], scaleSnap=True) + self.parent.to_roi_id = pg.ROI( + [x, y], [ROI_WIDTH, ROI_HEIGHT], scaleSnap=True + ) self.parent.ui.image_view.addItem(self.parent.to_roi_id) - self.parent.to_roi_id.sigRegionChanged.connect(self.parent.to_roi_box_changed) + self.parent.to_roi_id.sigRegionChanged.connect( + self.parent.to_roi_box_changed + ) self.update_from_label() self.update_from_cross_line() @@ -162,12 +182,18 @@ def update_validity_of_from_to_button(self): o_table = TableHandler(table_ui=self.parent.ui.tableWidget) row_selected = o_table.get_row_selected() - name_of_file_selected = o_table.get_item_str_from_cell(row=row_selected, column=0) + name_of_file_selected = o_table.get_item_str_from_cell( + row=row_selected, column=0 + ) offset_dictionary = self.parent.offset_dictionary - xoffset_of_selected_image = offset_dictionary[name_of_file_selected]["xoffset"] + HORIZONTAL_MARGIN - yoffset_of_selected_image = offset_dictionary[name_of_file_selected]["yoffset"] + VERTICAL_MARGIN + xoffset_of_selected_image = ( + offset_dictionary[name_of_file_selected]["xoffset"] + HORIZONTAL_MARGIN + ) + yoffset_of_selected_image = ( + offset_dictionary[name_of_file_selected]["yoffset"] + VERTICAL_MARGIN + ) if ( (x < xoffset_of_selected_image) @@ -219,11 +245,19 @@ def update_cross_line(self, roi_cross_id=None, roi=None): line_color = (255, 0, 0, 255, 1) lines = np.array( [line_color for _ in np.arange(len(pos))], - dtype=[("red", np.ubyte), ("green", np.ubyte), ("blue", np.ubyte), ("alpha", np.ubyte), ("width", float)], + dtype=[ + ("red", np.ubyte), + ("green", np.ubyte), + ("blue", np.ubyte), + ("alpha", np.ubyte), + ("width", float), + ], ) line_view_binning = pg.GraphItem() self.parent.ui.image_view.addItem(line_view_binning) - line_view_binning.setData(pos=pos, adj=adj, pen=lines, symbol=None, pxMode=False) + line_view_binning.setData( + pos=pos, adj=adj, pen=lines, symbol=None, pxMode=False + ) return line_view_binning @@ -231,13 +265,17 @@ def update_from_cross_line(self): from_roi_cross_id = self.parent.from_roi_cross_id from_roi = self.parent.from_roi - self.parent.from_roi_cross_id = self.update_cross_line(roi_cross_id=from_roi_cross_id, roi=from_roi) + self.parent.from_roi_cross_id = self.update_cross_line( + roi_cross_id=from_roi_cross_id, roi=from_roi + ) def update_to_cross_line(self): to_roi_cross_id = self.parent.to_roi_cross_id to_roi = self.parent.to_roi - self.parent.to_roi_cross_id = self.update_cross_line(roi_cross_id=to_roi_cross_id, roi=to_roi) + self.parent.to_roi_cross_id = self.update_cross_line( + roi_cross_id=to_roi_cross_id, roi=to_roi + ) def update_label(self, label_id=None, roi=None, text=""): if label_id: @@ -256,9 +294,13 @@ def update_label(self, label_id=None, roi=None, text=""): def update_from_label(self): label_id = self.parent.from_label_id roi = self.parent.from_roi - self.parent.from_label_id = self.update_label(label_id=label_id, roi=roi, text="from") + self.parent.from_label_id = self.update_label( + label_id=label_id, roi=roi, text="from" + ) def update_to_label(self): label_id = self.parent.to_label_id roi = self.parent.to_roi - self.parent.to_label_id = self.update_label(label_id=label_id, roi=roi, text="to") + self.parent.to_label_id = self.update_label( + label_id=label_id, roi=roi, text="to" + ) diff --git a/notebooks/__code/panoramic_stitching_for_tof/load_data.py b/notebooks/__code/panoramic_stitching_for_tof/load_data.py index e1a1b62fa..d2c1ba324 100755 --- a/notebooks/__code/panoramic_stitching_for_tof/load_data.py +++ b/notebooks/__code/panoramic_stitching_for_tof/load_data.py @@ -38,7 +38,9 @@ def run(self): master_dict = OrderedDict() integrated_images_dict = OrderedDict() for _folder_index, _folder in enumerate(self.list_folders): - self.parent.ui.statusbar.showMessage(f"Loading data from folder {os.path.basename(_folder)}") + self.parent.ui.statusbar.showMessage( + f"Loading data from folder {os.path.basename(_folder)}" + ) QtGui.QGuiApplication.processEvents() o_norm = Normalization() @@ -49,7 +51,9 @@ def run(self): # record size of images if _folder_index == 0: - self.parent.image_height, self.parent.image_width = np.shape(o_norm.data["sample"]["data"][0]) + self.parent.image_height, self.parent.image_width = np.shape( + o_norm.data["sample"]["data"][0] + ) local_dict = OrderedDict() self.parent.nbr_files_per_folder = len(list_files) @@ -76,10 +80,14 @@ def run(self): coarse_images_dictionary = OrderedDict() for _folder in self.parent.integrated_images.keys(): - coarse_images_dictionary[os.path.basename(_folder)] = self.parent.integrated_images[_folder] + coarse_images_dictionary[os.path.basename(_folder)] = ( + self.parent.integrated_images[_folder] + ) self.parent.coarse_images_dictionary = coarse_images_dictionary - self.parent.ui.statusbar.showMessage(f"Done Loading data from {nbr_folder} folders!", 5000) + self.parent.ui.statusbar.showMessage( + f"Done Loading data from {nbr_folder} folders!", 5000 + ) QApplication.restoreOverrideCursor() @staticmethod diff --git a/notebooks/__code/panoramic_stitching_for_tof/panoramic_stitching_for_tof.py b/notebooks/__code/panoramic_stitching_for_tof/panoramic_stitching_for_tof.py index 0e247f0c0..a30bb4a46 100755 --- a/notebooks/__code/panoramic_stitching_for_tof/panoramic_stitching_for_tof.py +++ b/notebooks/__code/panoramic_stitching_for_tof/panoramic_stitching_for_tof.py @@ -11,7 +11,9 @@ from __code.panoramic_stitching.event_handler import EventHandler from __code.panoramic_stitching.image_handler import HORIZONTAL_MARGIN, VERTICAL_MARGIN from __code.panoramic_stitching_for_tof.automatically_stitch import AutomaticallyStitch -from __code.panoramic_stitching_for_tof.best_contrast_tab_handler import BestContrastTabHandler +from __code.panoramic_stitching_for_tof.best_contrast_tab_handler import ( + BestContrastTabHandler, +) from __code.panoramic_stitching_for_tof.coarse_tab_handler import CoarseTabHandler from __code.panoramic_stitching_for_tof.data_initialization import DataInitialization from __code.panoramic_stitching_for_tof.event_handler import TOFEventHandler @@ -33,7 +35,7 @@ def __init__(self, working_dir=""): def select_input_folders(self): self.list_folder_widget = fileselector.FileSelectorPanel( - instruction="select the folders of images to " "stitch", + instruction="select the folders of images to stitch", start_dir=self.working_dir, type="directory", next=self.folder_selected, @@ -49,9 +51,8 @@ def folder_selected(self, folder_selected): str_list_ext = ", ".join(self.file_extension) display( format_html_message( - pre_message="None of the folder selected contains the file of extension " "requested ({}}".format( - str_list_ext - ), + pre_message="None of the folder selected contains the file of extension " + "requested ({}}".format(str_list_ext), spacer="", ) ) @@ -59,7 +60,12 @@ def folder_selected(self, folder_selected): final_list_folders.sort() nbr_folder = len(final_list_folders) - display(format_html_message(pre_message=f"Notebook is about to work with {nbr_folder} folders!", spacer="")) + display( + format_html_message( + pre_message=f"Notebook is about to work with {nbr_folder} folders!", + spacer="", + ) + ) # gui initialization o_interface = Interface(list_folders=final_list_folders) @@ -277,7 +283,9 @@ def enable_vertical_profile_checked(self, state): def left_left_button_pressed(self): EventHandler.button_pressed(ui=self.ui.left_left_button, name="left_left") o_event = EventHandler(parent=self) - o_event.manual_offset_changed(direction="horizontal", nbr_pixel=-DOUBLE_MANUAL_PIXEL_CHANGE) + o_event.manual_offset_changed( + direction="horizontal", nbr_pixel=-DOUBLE_MANUAL_PIXEL_CHANGE + ) self.horizontal_profile_changed() def left_left_button_released(self): @@ -286,7 +294,9 @@ def left_left_button_released(self): def left_button_pressed(self): EventHandler.button_pressed(ui=self.ui.left_button, name="left") o_event = EventHandler(parent=self) - o_event.manual_offset_changed(direction="horizontal", nbr_pixel=-SIMPLE_MANUAL_PIXEL_CHANGE) + o_event.manual_offset_changed( + direction="horizontal", nbr_pixel=-SIMPLE_MANUAL_PIXEL_CHANGE + ) self.horizontal_profile_changed() def left_button_released(self): @@ -295,7 +305,9 @@ def left_button_released(self): def right_right_button_pressed(self): EventHandler.button_pressed(ui=self.ui.right_right_button, name="right_right") o_event = EventHandler(parent=self) - o_event.manual_offset_changed(direction="horizontal", nbr_pixel=DOUBLE_MANUAL_PIXEL_CHANGE) + o_event.manual_offset_changed( + direction="horizontal", nbr_pixel=DOUBLE_MANUAL_PIXEL_CHANGE + ) self.horizontal_profile_changed() def right_right_button_released(self): @@ -304,7 +316,9 @@ def right_right_button_released(self): def right_button_pressed(self): EventHandler.button_pressed(ui=self.ui.right_button, name="right") o_event = EventHandler(parent=self) - o_event.manual_offset_changed(direction="horizontal", nbr_pixel=SIMPLE_MANUAL_PIXEL_CHANGE) + o_event.manual_offset_changed( + direction="horizontal", nbr_pixel=SIMPLE_MANUAL_PIXEL_CHANGE + ) self.horizontal_profile_changed() def right_button_released(self): @@ -313,7 +327,9 @@ def right_button_released(self): def up_up_button_pressed(self): EventHandler.button_pressed(ui=self.ui.up_up_button, name="up_up") o_event = EventHandler(parent=self) - o_event.manual_offset_changed(direction="vertical", nbr_pixel=-DOUBLE_MANUAL_PIXEL_CHANGE) + o_event.manual_offset_changed( + direction="vertical", nbr_pixel=-DOUBLE_MANUAL_PIXEL_CHANGE + ) self.vertical_profile_changed() def up_up_button_released(self): @@ -322,7 +338,9 @@ def up_up_button_released(self): def up_button_pressed(self): EventHandler.button_pressed(ui=self.ui.up_button, name="up") o_event = EventHandler(parent=self) - o_event.manual_offset_changed(direction="vertical", nbr_pixel=-SIMPLE_MANUAL_PIXEL_CHANGE) + o_event.manual_offset_changed( + direction="vertical", nbr_pixel=-SIMPLE_MANUAL_PIXEL_CHANGE + ) self.vertical_profile_changed() def up_button_released(self): @@ -331,7 +349,9 @@ def up_button_released(self): def down_down_button_pressed(self): EventHandler.button_pressed(ui=self.ui.down_down_button, name="down_down") o_event = EventHandler(parent=self) - o_event.manual_offset_changed(direction="vertical", nbr_pixel=DOUBLE_MANUAL_PIXEL_CHANGE) + o_event.manual_offset_changed( + direction="vertical", nbr_pixel=DOUBLE_MANUAL_PIXEL_CHANGE + ) self.vertical_profile_changed() def down_down_button_released(self): @@ -340,7 +360,9 @@ def down_down_button_released(self): def down_button_pressed(self): EventHandler.button_pressed(ui=self.ui.down_button, name="down") o_event = EventHandler(parent=self) - o_event.manual_offset_changed(direction="vertical", nbr_pixel=SIMPLE_MANUAL_PIXEL_CHANGE) + o_event.manual_offset_changed( + direction="vertical", nbr_pixel=SIMPLE_MANUAL_PIXEL_CHANGE + ) self.vertical_profile_changed() def down_button_released(self): diff --git a/notebooks/__code/panoramic_stitching_for_tof/profile.py b/notebooks/__code/panoramic_stitching_for_tof/profile.py index 5c8a852b5..3997a9c05 100755 --- a/notebooks/__code/panoramic_stitching_for_tof/profile.py +++ b/notebooks/__code/panoramic_stitching_for_tof/profile.py @@ -2,7 +2,10 @@ from __code._utilities.table_handler import TableHandler from __code.panoramic_stitching_for_tof.get import Get -from __code.panoramic_stitching_for_tof.image_handler import HORIZONTAL_MARGIN, VERTICAL_MARGIN +from __code.panoramic_stitching_for_tof.image_handler import ( + HORIZONTAL_MARGIN, + VERTICAL_MARGIN, +) COLOR_WORKING_ROW = "red" COLOR_NONE_WORKING_ROW = "black" @@ -15,7 +18,9 @@ def __init__(self, parent=None): def horizontal_profile_changed(self): if self.parent.ui.enable_horizontal_profile_checkbox.isChecked(): roi_id = self.parent.horizontal_profile["id"] - horizontal_roi_dimensions = Profile.get_x_y_width_height_of_roi(roi_id=roi_id) + horizontal_roi_dimensions = Profile.get_x_y_width_height_of_roi( + roi_id=roi_id + ) self.plot_profile( x=horizontal_roi_dimensions["x"], y=horizontal_roi_dimensions["y"], @@ -36,7 +41,9 @@ def vertical_profile_changed(self): profile_type="vertical", ) - def plot_profile(self, x=None, y=None, width=None, height=None, profile_type="horizontal"): + def plot_profile( + self, x=None, y=None, width=None, height=None, profile_type="horizontal" + ): if profile_type == "horizontal": plot_ui = self.parent.horizontal_profile_plot dim_to_keep = 0 @@ -87,15 +94,23 @@ def plot_profile(self, x=None, y=None, width=None, height=None, profile_type="ho # find part of profile that is inside image x_left_for_profile = np.max([x, left_of_image]) - left_of_image - x_right_for_profile = np.min([x + width, left_of_image + image_width]) - left_of_image + x_right_for_profile = ( + np.min([x + width, left_of_image + image_width]) - left_of_image + ) y_top_for_profile = np.max([y, top_of_image]) - top_of_image - y_bottom_for_profile = np.min([y + height, top_of_image + image_height]) - top_of_image + y_bottom_for_profile = ( + np.min([y + height, top_of_image + image_height]) - top_of_image + ) if profile_type == "horizontal": - x_axis_of_profile = np.arange(x_left_for_profile, x_right_for_profile) + left_of_image + x_axis_of_profile = ( + np.arange(x_left_for_profile, x_right_for_profile) + left_of_image + ) else: - x_axis_of_profile = np.arange(y_top_for_profile, y_bottom_for_profile) + top_of_image + x_axis_of_profile = ( + np.arange(y_top_for_profile, y_bottom_for_profile) + top_of_image + ) y_axis_of_profile = data_dictionary[_file].data[ y_top_for_profile:y_bottom_for_profile, diff --git a/notebooks/__code/profile/display.py b/notebooks/__code/profile/display.py index c2f7d0430..e81be9e37 100755 --- a/notebooks/__code/profile/display.py +++ b/notebooks/__code/profile/display.py @@ -17,7 +17,8 @@ def get_image_selected(self, recalculate_image=False): angle = self.parent.rotation_angle # rotate all images self.parent.data_dict["data"] = [ - transform.rotate(_image, angle) for _image in self.parent.data_dict_raw["data"] + transform.rotate(_image, angle) + for _image in self.parent.data_dict_raw["data"] ] _image = self.parent.data_dict["data"][slider_index] @@ -48,7 +49,9 @@ def display_images(self): _view_box.setState(_state) if not first_update: - _histo_widget.setLevels(self.parent.histogram_level[0], self.parent.histogram_level[1]) + _histo_widget.setLevels( + self.parent.histogram_level[0], self.parent.histogram_level[1] + ) def calculate_matrix_grid(self, grid_size=1, height=1, width=1): """calculate the matrix that defines the vertical and horizontal lines @@ -98,12 +101,17 @@ def display_grid(self): grid_size = self.parent.ui.grid_size_slider.value() [height, width] = np.shape(self.parent.live_image) - pos_adj_dict = self.calculate_matrix_grid(grid_size=grid_size, height=height, width=width) + pos_adj_dict = self.calculate_matrix_grid( + grid_size=grid_size, height=height, width=width + ) pos = pos_adj_dict["pos"] adj = pos_adj_dict["adj"] line_color = self.parent.grid_view["color"] - _transparency_value = 255 - (float(str(self.parent.ui.transparency_slider.value())) / 100) * 255 + _transparency_value = ( + 255 + - (float(str(self.parent.ui.transparency_slider.value())) / 100) * 255 + ) _list_line_color = list(line_color) _list_line_color[3] = _transparency_value line_color = tuple(_list_line_color) diff --git a/notebooks/__code/profile/export.py b/notebooks/__code/profile/export.py index cebcb946f..ed8238570 100755 --- a/notebooks/__code/profile/export.py +++ b/notebooks/__code/profile/export.py @@ -30,7 +30,9 @@ def _create_metadata(self, profile_index=0): y_top = profile_dimension.y_top y_bottom = profile_dimension.y_bottom metadata.append("#Profile dimension:") - metadata.append(f"# * [x0, y0, x1, y1] = [{x_left}, {y_top}, {x_right}, {y_bottom}]") + metadata.append( + f"# * [x0, y0, x1, y1] = [{x_left}, {y_top}, {x_right}, {y_bottom}]" + ) if is_x_profile_direction: metadata.append("# * integrated over y_axis") table_axis = ["#x_axis"] @@ -50,7 +52,9 @@ def _create_data(self, profile_index=0): all_profiles = [] x_axis = [] for _data in self.parent.data_dict["data"]: - [x_axis, profile] = self.parent.get_profile(image=np.transpose(_data), profile_roi_row=profile_index) + [x_axis, profile] = self.parent.get_profile( + image=np.transpose(_data), profile_roi_row=profile_index + ) all_profiles.append(list(profile)) data = [] @@ -63,10 +67,17 @@ def _create_data(self, profile_index=0): def run(self): _nbr_profiles = self.parent.ui.tableWidget.rowCount() for _profile_index in np.arange(_nbr_profiles): - _output_file_name = self._create_output_file_name(profile_index=_profile_index) + _output_file_name = self._create_output_file_name( + profile_index=_profile_index + ) metadata = self._create_metadata(profile_index=_profile_index) data = self._create_data(profile_index=_profile_index) - make_ascii_file(metadata=metadata, data=data, output_file_name=_output_file_name, dim="1d") + make_ascii_file( + metadata=metadata, + data=data, + output_file_name=_output_file_name, + dim="1d", + ) display(HTML(f"Exported Profile file {_output_file_name}")) @@ -78,7 +89,9 @@ def __init__(self, parent=None, export_folder=""): def _create_output_file_name(self, roi_index=0): base_name = os.path.basename(self.parent.working_dir) - output_file_name = os.path.join(self.export_folder, f"{base_name}_profile_{roi_index + 1}.txt") + output_file_name = os.path.join( + self.export_folder, f"{base_name}_profile_{roi_index + 1}.txt" + ) return output_file_name # def _create_metadata(self, profile_index=0): diff --git a/notebooks/__code/profile/guide_and_profile_rois_handler.py b/notebooks/__code/profile/guide_and_profile_rois_handler.py index 13487a807..82a3eb954 100755 --- a/notebooks/__code/profile/guide_and_profile_rois_handler.py +++ b/notebooks/__code/profile/guide_and_profile_rois_handler.py @@ -18,14 +18,19 @@ def add(self): def update(self): self._define_profile() - self.parent.ui.image_view.removeItem(self.parent.list_profile_pyqt_roi[self.row]) + self.parent.ui.image_view.removeItem( + self.parent.list_profile_pyqt_roi[self.row] + ) self.parent.list_profile_pyqt_roi[self.row] = self.__profile def _define_guide(self): """define the guide""" guide_roi = pg.RectROI( [self.parent.default_guide_roi["x0"], self.parent.default_guide_roi["y0"]], - [self.parent.default_guide_roi["width"], self.parent.default_guide_roi["height"]], + [ + self.parent.default_guide_roi["width"], + self.parent.default_guide_roi["height"], + ], pen=self.parent.default_guide_roi["color_activated"], ) guide_roi.addScaleHandle([1, 1], [0, 0]) @@ -82,7 +87,13 @@ def _define_profile(self): line_color = tuple(_list_line_color) lines = np.array( [line_color for n in np.arange(len(pos))], - dtype=[("red", np.ubyte), ("green", np.ubyte), ("blue", np.ubyte), ("alpha", np.ubyte), ("width", float)], + dtype=[ + ("red", np.ubyte), + ("green", np.ubyte), + ("blue", np.ubyte), + ("alpha", np.ubyte), + ("width", float), + ], ) profile = pg.GraphItem() diff --git a/notebooks/__code/profile/initialization.py b/notebooks/__code/profile/initialization.py index e271b156a..5564abd54 100755 --- a/notebooks/__code/profile/initialization.py +++ b/notebooks/__code/profile/initialization.py @@ -27,20 +27,28 @@ def timestamp_dict(self): def table(self): # init the summary table list_files_full_name = self.parent.data_dict["file_name"] - list_files_short_name = [os.path.basename(_file) for _file in list_files_full_name] + list_files_short_name = [ + os.path.basename(_file) for _file in list_files_full_name + ] list_time_stamp = self.parent.timestamp_dict["list_time_stamp"] - list_time_stamp_user_format = self.parent.timestamp_dict["list_time_stamp_user_format"] + list_time_stamp_user_format = self.parent.timestamp_dict[ + "list_time_stamp_user_format" + ] time_0 = list_time_stamp[0] for _row, _file in enumerate(list_files_short_name): self.parent.ui.summary_table.insertRow(_row) self.set_item_summary_table(row=_row, col=0, value=_file) - self.set_item_summary_table(row=_row, col=1, value=list_time_stamp_user_format[_row]) + self.set_item_summary_table( + row=_row, col=1, value=list_time_stamp_user_format[_row] + ) _offset = list_time_stamp[_row] - time_0 self.set_item_summary_table(row=_row, col=2, value=f"{_offset:0.2f}") self.parent.ui.all_plots_file_name_table.insertRow(_row) - self.set_item_all_plot_file_name_table(row=_row, value=os.path.basename(_file)) + self.set_item_all_plot_file_name_table( + row=_row, value=os.path.basename(_file) + ) def parameters(self): # init the position of the measurement ROI @@ -49,7 +57,9 @@ def parameters(self): self.parent.default_guide_roi["height"] = int(height / 5) self.parent.default_guide_roi["x0"] = int(width / 2) self.parent.default_guide_roi["y0"] = int(height / 2) - self.parent.default_profile_width_values = [str(_value) for _value in self.parent.default_profile_width_values] + self.parent.default_profile_width_values = [ + str(_value) for _value in self.parent.default_profile_width_values + ] def widgets(self): _file_path = os.path.dirname(__file__) @@ -57,28 +67,32 @@ def widgets(self): os.path.join(_file_path, "../static/profile/button_rotation_left_fast.png") ) self.parent.ui.left_rotation_button_fast.setStyleSheet( - "background-image: " "url('" + left_rotation_fast_file + "'); " + "background-repeat: no-repeat" + "background-image: " + "url('" + left_rotation_fast_file + "'); " + "background-repeat: no-repeat" ) right_rotation_fast_file = os.path.abspath( os.path.join(_file_path, "../static/profile/button_rotation_right_fast.png") ) self.parent.ui.right_rotation_button_fast.setStyleSheet( - "background-image: " "url('" + right_rotation_fast_file + "'); " + "background-repeat: no-repeat" + "background-image: " + "url('" + right_rotation_fast_file + "'); " + "background-repeat: no-repeat" ) left_rotation_slow_file = os.path.abspath( os.path.join(_file_path, "../static/profile/button_rotation_left_slow.png") ) self.parent.ui.left_rotation_button_slow.setStyleSheet( - "background-image: " "url('" + left_rotation_slow_file + "'); " + "background-repeat: no-repeat" + "background-image: " + "url('" + left_rotation_slow_file + "'); " + "background-repeat: no-repeat" ) right_rotation_slow_file = os.path.abspath( os.path.join(_file_path, "../static/profile/button_rotation_right_slow.png") ) self.parent.ui.right_rotation_button_slow.setStyleSheet( - "background-image: " "url('" + right_rotation_slow_file + "'); " + "background-repeat: no-repeat" + "background-image: " + "url('" + right_rotation_slow_file + "'); " + "background-repeat: no-repeat" ) self.parent.ui.splitter_2.setSizes([250, 50]) @@ -92,12 +106,16 @@ def widgets(self): # update size of table columns nbr_columns = self.parent.ui.tableWidget.columnCount() for _col in range(nbr_columns): - self.parent.ui.tableWidget.setColumnWidth(_col, self.parent.guide_table_width[_col]) + self.parent.ui.tableWidget.setColumnWidth( + _col, self.parent.guide_table_width[_col] + ) # update size of summary table nbr_columns = self.parent.ui.summary_table.columnCount() for _col in range(nbr_columns): - self.parent.ui.summary_table.setColumnWidth(_col, self.parent.summary_table_width[_col]) + self.parent.ui.summary_table.setColumnWidth( + _col, self.parent.summary_table_width[_col] + ) self.parent.display_ui = [ self.parent.ui.display_size_label, diff --git a/notebooks/__code/profile/profile.py b/notebooks/__code/profile/profile.py index 6c095879f..ed15465e8 100755 --- a/notebooks/__code/profile/profile.py +++ b/notebooks/__code/profile/profile.py @@ -82,7 +82,8 @@ def __init__(self, parent=None, working_dir="", data_dict=None): QMainWindow.__init__(self, parent=parent) ui_full_path = os.path.join( - os.path.dirname(os.path.dirname(os.path.dirname(__file__))), os.path.join("ui", "ui_profile.ui") + os.path.dirname(os.path.dirname(os.path.dirname(__file__))), + os.path.join("ui", "ui_profile.ui"), ) self.ui = load_ui(ui_full_path, baseinstance=self) self.setWindowTitle("Profile") @@ -165,7 +166,9 @@ def update_all_plots(self): nbr_profile = len(list_index_profile_selected) nbr_file_selected = len(list_index_file_selected) color = Color() - list_rgb_profile_color = color.get_list_rgb(nbr_color=(nbr_profile * nbr_file_selected)) + list_rgb_profile_color = color.get_list_rgb( + nbr_color=(nbr_profile * nbr_file_selected) + ) self.ui.all_plots_view.clear() if nbr_profile == 0: @@ -175,10 +178,16 @@ def update_all_plots(self): for _color_index_file, _index_file in enumerate(list_index_file_selected): _data = self.data_dict["data"][_index_file] - for _color_index_profile, _index_profile in enumerate(list_index_profile_selected): + for _color_index_profile, _index_profile in enumerate( + list_index_profile_selected + ): legend = f"File #{_index_file} - Profile #{_index_profile}" - _color = list_rgb_profile_color[_color_index_file + _color_index_profile * nbr_file_selected] - [x_axis, y_axis] = self.get_profile(image=np.transpose(_data), profile_roi_row=_index_profile) + _color = list_rgb_profile_color[ + _color_index_file + _color_index_profile * nbr_file_selected + ] + [x_axis, y_axis] = self.get_profile( + image=np.transpose(_data), profile_roi_row=_index_profile + ) self.ui.all_plots_view.plot(x_axis, y_axis, name=legend, pen=_color) def display_image(self, recalculate_image=False): @@ -247,7 +256,9 @@ def is_row_enabled(self, row=-1): def update_guide_table_using_guide_rois(self): for _row, _roi in enumerate(self.list_guide_pyqt_roi): if self.is_row_enabled(row=_row): - region = _roi.getArraySlice(self.live_image, self.ui.image_view.imageItem) + region = _roi.getArraySlice( + self.live_image, self.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop @@ -320,7 +331,9 @@ def rename_all_plots_profiles_table(self): """rename all the profile name""" nbr_row = self.ui.tableWidget.rowCount() for _row in np.arange(nbr_row): - self.ui.all_plots_profiles_table.item(_row, 0).setText(f"Profile # {_row + 1}") + self.ui.all_plots_profiles_table.item(_row, 0).setText( + f"Profile # {_row + 1}" + ) # setter def set_item_all_plots_profile_table(self, row=0): @@ -329,12 +342,16 @@ def set_item_all_plots_profile_table(self, row=0): self.ui.all_plots_profiles_table.setItem(row, 0, item) def set_item_profile_table(self, row=0): - spacerItem_left = QSpacerItem(408, 20, QSizePolicy.Expanding, QSizePolicy.Expanding) + spacerItem_left = QSpacerItem( + 408, 20, QSizePolicy.Expanding, QSizePolicy.Expanding + ) widget = QComboBox() widget.addItems(self.default_profile_width_values) widget.blockSignals(True) widget.currentIndexChanged.connect(self.profile_width_changed) - spacerItem_right = QSpacerItem(408, 20, QSizePolicy.Expanding, QSizePolicy.Expanding) + spacerItem_right = QSpacerItem( + 408, 20, QSizePolicy.Expanding, QSizePolicy.Expanding + ) hori_layout = QHBoxLayout() hori_layout.addItem(spacerItem_left) hori_layout.addWidget(widget) @@ -346,12 +363,16 @@ def set_item_profile_table(self, row=0): def set_item_main_table(self, row=0, col=0, value=""): if col == 0: - spacerItem_left = QSpacerItem(408, 20, QSizePolicy.Expanding, QSizePolicy.Expanding) + spacerItem_left = QSpacerItem( + 408, 20, QSizePolicy.Expanding, QSizePolicy.Expanding + ) widget = QCheckBox() widget.blockSignals(True) self.list_table_widget_checkbox.insert(row, widget) widget.stateChanged.connect(self.guide_state_changed) - spacerItem_right = QSpacerItem(408, 20, QSizePolicy.Expanding, QSizePolicy.Expanding) + spacerItem_right = QSpacerItem( + 408, 20, QSizePolicy.Expanding, QSizePolicy.Expanding + ) hori_layout = QHBoxLayout() hori_layout.addItem(spacerItem_left) hori_layout.addWidget(widget) @@ -399,7 +420,9 @@ def get_profile_dimensions(self, row=-1): y_top = y0 y_bottom = y0 + height - Profile = collections.namedtuple("Profile", ["x_left", "x_right", "y_top", "y_bottom", "profile_center"]) + Profile = collections.namedtuple( + "Profile", ["x_left", "x_right", "y_top", "y_bottom", "profile_center"] + ) result = Profile(x_left, x_right, y_top, y_bottom, profile_center) return result @@ -420,7 +443,9 @@ def get_profile(self, image=[], profile_roi_row=-1): mean_axis = 0 x_axis = np.arange(y_top, y_bottom) - _data = image[x_left:x_right, y_top:y_bottom] # because pyqtgrpah display transpose images + _data = image[ + x_left:x_right, y_top:y_bottom + ] # because pyqtgrpah display transpose images profile = np.mean(_data, axis=mean_axis) return [x_axis, profile] @@ -479,7 +504,9 @@ def highlight_guide_profile_pyqt_rois(self, row=-1): return try: - self._highlights_guide_profile_pyqt_roi(row=previous_active_row, status="deactivated") + self._highlights_guide_profile_pyqt_roi( + row=previous_active_row, status="deactivated" + ) self._highlights_guide_profile_pyqt_roi(row=row, status="activated") except: pass @@ -634,7 +661,10 @@ def profile_along_axis_changed(self): def export_button_clicked(self): _export_folder = QFileDialog.getExistingDirectory( - self, directory=self.working_dir, caption="Select Output Folder", options=QFileDialog.ShowDirsOnly + self, + directory=self.working_dir, + caption="Select Output Folder", + options=QFileDialog.ShowDirsOnly, ) if _export_folder: o_export = ExportProfiles(parent=self, export_folder=_export_folder) @@ -656,7 +686,9 @@ def next_image_button_clicked(self): def help_button_clicked(self): import webbrowser - webbrowser.open("https://neutronimaging.pages.ornl.gov/en/tutorial/notebooks/profile/") + webbrowser.open( + "https://neutronimaging.pages.ornl.gov/en/tutorial/notebooks/profile/" + ) def closeEvent(self, event=None): pass diff --git a/notebooks/__code/profile_vs_file_index.py b/notebooks/__code/profile_vs_file_index.py index 2747f0088..75e202f29 100755 --- a/notebooks/__code/profile_vs_file_index.py +++ b/notebooks/__code/profile_vs_file_index.py @@ -34,7 +34,9 @@ def __init__(self, working_dir="./"): def select_images(self): self.folder_ui = ipywe.fileselector.FileSelectorPanel( - instruction="Select Input Images ...", start_dir=self.working_dir, multiple=True + instruction="Select Input Images ...", + start_dir=self.working_dir, + multiple=True, ) self.folder_ui.show() @@ -43,7 +45,11 @@ def load_images(self): try: self.list_data_files = self.folder_ui.selected except: - display(HTML('Please Select a set of Images First!')) + display( + HTML( + 'Please Select a set of Images First!' + ) + ) return self.o_load = Normalization() @@ -71,26 +77,62 @@ def select_profile(self, roi_left=0, roi_top=0, roi_height=-1, roi_width=-1): if roi_width == -1: roi_width = self.width - 1 - def plot_images_with_roi(x_left, y_top, width, height, contrast_min, contrast_max): + def plot_images_with_roi( + x_left, y_top, width, height, contrast_min, contrast_max + ): plt.figure(figsize=(5, 5)) ax_img = plt.subplot(111) ax_img.imshow( - self.integrated_data, cmap="rainbow", interpolation=None, vmin=contrast_min, vmax=contrast_max + self.integrated_data, + cmap="rainbow", + interpolation=None, + vmin=contrast_min, + vmax=contrast_max, ) - ax_img.add_patch(patches.Rectangle((x_left, y_top), width, height, fill=False)) + ax_img.add_patch( + patches.Rectangle((x_left, y_top), width, height, fill=False) + ) return [x_left, y_top, width, height] self.profile = interact( plot_images_with_roi, - x_left=widgets.IntSlider(min=0, max=self.width - 1, step=1, value=roi_left, continuous_update=False), - y_top=widgets.IntSlider(min=0, max=self.height - 1, step=1, value=roi_top, continuous_update=False), - width=widgets.IntSlider(min=0, max=self.width - 1, step=1, value=roi_width, continuous_update=False), - height=widgets.IntSlider(min=0, max=self.height - 1, step=1, value=roi_height, continuous_update=False), - contrast_min=widgets.FloatSlider(min=0, max=1, step=0.1, value=0, continuous_update=False), - contrast_max=widgets.FloatSlider(min=0, max=2, value=1, step=0.1, continuous_update=False), + x_left=widgets.IntSlider( + min=0, + max=self.width - 1, + step=1, + value=roi_left, + continuous_update=False, + ), + y_top=widgets.IntSlider( + min=0, + max=self.height - 1, + step=1, + value=roi_top, + continuous_update=False, + ), + width=widgets.IntSlider( + min=0, + max=self.width - 1, + step=1, + value=roi_width, + continuous_update=False, + ), + height=widgets.IntSlider( + min=0, + max=self.height - 1, + step=1, + value=roi_height, + continuous_update=False, + ), + contrast_min=widgets.FloatSlider( + min=0, max=1, step=0.1, value=0, continuous_update=False + ), + contrast_max=widgets.FloatSlider( + min=0, max=2, value=1, step=0.1, continuous_update=False + ), ) def calculate_integrated_profile(self): @@ -108,7 +150,9 @@ def calculate_integrated_profile(self): profile_array = [] for _image in sample_data: - _profile_image = _image[roi_top : roi_top + roi_height, roi_left : roi_left + roi_width] + _profile_image = _image[ + roi_top : roi_top + roi_height, roi_left : roi_left + roi_width + ] _value = np.mean(_profile_image) profile_array.append(_value) w.value = index @@ -121,7 +165,9 @@ def select_vertical_pixel_binning(self): [ widgets.Label("Vertical Binning", layout=widgets.Layout(width="20%")), widgets.Dropdown( - options=["1", "2", "3", "4", "5", "6", "7", "8", "9"], value="1", layout=widgets.Layout(width="20%") + options=["1", "2", "3", "4", "5", "6", "7", "8", "9"], + value="1", + layout=widgets.Layout(width="20%"), ), ] ) @@ -142,9 +188,13 @@ def calculate_profile(self): profile_1d = [] for _index, _array in enumerate(sample_data): - _roi_array = _array[roi_top : roi_top + roi_height, roi_left : roi_left + roi_width] + _roi_array = _array[ + roi_top : roi_top + roi_height, roi_left : roi_left + roi_width + ] _width_profile = np.mean(_roi_array, 1) - rebin_width_profile = [np.mean(_width_profile[x : x + self.rebin]) for x in self.rebin_range] + rebin_width_profile = [ + np.mean(_width_profile[x : x + self.rebin]) for x in self.rebin_range + ] profile_1d.append(rebin_width_profile) _index += 1 w.value = _index @@ -171,19 +221,30 @@ def plot_profile(file_index): ax_img = plt.subplot(212) ax_img.imshow(data_2d, cmap="rainbow", interpolation=None) - ax_img.add_patch(patches.Rectangle((roi_left, roi_top), roi_width, roi_height, fill=False)) + ax_img.add_patch( + patches.Rectangle( + (roi_left, roi_top), roi_width, roi_height, fill=False + ) + ) number_of_files = len(self.sample_data) _ = interact( plot_profile, file_index=widgets.IntSlider( - min=0, max=number_of_files - 1, value=0, step=1, description="Image Index", continuous_update=False + min=0, + max=number_of_files - 1, + value=0, + step=1, + description="Image Index", + continuous_update=False, ), ) def select_file_name_vs_time_stamp(self): self.file_ui = ipywe.fileselector.FileSelectorPanel( - instruction="Select file_name_vs_time_stamp File ...", start_dir=self.working_dir, multiple=False + instruction="Select file_name_vs_time_stamp File ...", + start_dir=self.working_dir, + multiple=False, ) self.file_ui.show() @@ -204,12 +265,16 @@ def load_file_name_vs_time_stamp_file(self): def select_output_folder(self): self.output_folder_ui = ipywe.fileselector.FileSelectorPanel( - instruction="Select Output Folder ...", start_dir=self.working_dir, type="directory" + instruction="Select Output Folder ...", + start_dir=self.working_dir, + type="directory", ) self.output_folder_ui.show() def __get_time_stamp(self, file_name): - _index_time_stamp = self.list_data_files_short.index(os.path.basename(file_name)) + _index_time_stamp = self.list_data_files_short.index( + os.path.basename(file_name) + ) return self.list_time_stamp[_index_time_stamp] def output_profiles(self): @@ -225,7 +290,9 @@ def output_profiles(self): [roi_left, roi_top, roi_width, roi_height] = self.roi - self.list_data_files_short = [os.path.basename(_file) for _file in self.df["#filename"]] + self.list_data_files_short = [ + os.path.basename(_file) for _file in self.df["#filename"] + ] time_0 = self.__get_time_stamp(self.list_data_files[0]) w = widgets.IntProgress() @@ -243,7 +310,9 @@ def output_profiles(self): ) metadata.append(f"#Rebin in y direction: {self.rebin}") - _time_stamp_str = datetime.datetime.fromtimestamp(_time_stamp).strftime("%Y-%m-%d %H:%M:%S") + _time_stamp_str = datetime.datetime.fromtimestamp(_time_stamp).strftime( + "%Y-%m-%d %H:%M:%S" + ) metadata.append(f"#Time Stamp: {_time_stamp_str}") _delta_time = _time_stamp - time_0 @@ -260,10 +329,21 @@ def output_profiles(self): [base, _] = os.path.splitext(_base_file_name) output_file_name = os.path.join(output_folder, base + ".txt") - make_ascii_file(metadata=metadata, data=data, output_file_name=output_file_name, dim="1d") + make_ascii_file( + metadata=metadata, + data=data, + output_file_name=output_file_name, + dim="1d", + ) w.value = _index + 1 w.close() - display(HTML('Files created in folder ' + output_folder + "")) + display( + HTML( + 'Files created in folder ' + + output_folder + + "" + ) + ) diff --git a/notebooks/__code/radial_profile/display.py b/notebooks/__code/radial_profile/display.py index 63de6c014..7e5c05e2c 100755 --- a/notebooks/__code/radial_profile/display.py +++ b/notebooks/__code/radial_profile/display.py @@ -58,11 +58,19 @@ def grid(self): ) lines = np.array( [line_color for n in np.arange(len(pos))], - dtype=[("red", np.ubyte), ("green", np.ubyte), ("blue", np.ubyte), ("alpha", np.ubyte), ("width", float)], + dtype=[ + ("red", np.ubyte), + ("green", np.ubyte), + ("blue", np.ubyte), + ("alpha", np.ubyte), + ("width", float), + ], ) line_view_binning = pg.GraphItem() self.parent.ui.image_view.addItem(line_view_binning) - line_view_binning.setData(pos=pos, adj=adj, pen=lines, symbol=None, pxMode=False) + line_view_binning.setData( + pos=pos, adj=adj, pen=lines, symbol=None, pxMode=False + ) self.parent.line_view_binning = line_view_binning diff --git a/notebooks/__code/radial_profile/event_handler.py b/notebooks/__code/radial_profile/event_handler.py index 69b91eb56..cad6dd438 100755 --- a/notebooks/__code/radial_profile/event_handler.py +++ b/notebooks/__code/radial_profile/event_handler.py @@ -27,7 +27,9 @@ def file_index_changed(self): _view_box.setState(_state) if not first_update: - _histo_widget.setLevels(self.parent.histogram_level[0], self.parent.histogram_level[1]) + _histo_widget.setLevels( + self.parent.histogram_level[0], self.parent.histogram_level[1] + ) def guide_color_changed(self): red = self.parent.ui.guide_red_slider.value() @@ -69,14 +71,22 @@ def circle_center_changed(self): lines = np.array( [(255, 0, 0, 255, 2), (255, 0, 0, 0, 1), (255, 0, 0, 255, 2)], - dtype=[("red", np.ubyte), ("green", np.ubyte), ("blue", np.ubyte), ("alpha", np.ubyte), ("width", float)], + dtype=[ + ("red", np.ubyte), + ("green", np.ubyte), + ("blue", np.ubyte), + ("alpha", np.ubyte), + ("width", float), + ], ) if self.parent.sector_g: self.parent.ui.image_view.removeItem(self.parent.sector_g) self.parent.sector_g = pg.GraphItem() self.parent.ui.image_view.addItem(self.parent.sector_g) - self.parent.sector_g.setData(pos=pos, adj=adj, pen=lines, size=1, symbol=symbols, pxMode=False) + self.parent.sector_g.setData( + pos=pos, adj=adj, pen=lines, size=1, symbol=symbols, pxMode=False + ) def update_angle_label_position(self): x0 = int(str(self.parent.ui.circle_x.text())) diff --git a/notebooks/__code/radial_profile/initialization.py b/notebooks/__code/radial_profile/initialization.py index 5f3ddc77e..490da4245 100755 --- a/notebooks/__code/radial_profile/initialization.py +++ b/notebooks/__code/radial_profile/initialization.py @@ -1,6 +1,15 @@ import pyqtgraph as pg from qtpy import QtCore -from qtpy.QtWidgets import QHBoxLayout, QLabel, QProgressBar, QSizePolicy, QSlider, QSpacerItem, QVBoxLayout, QWidget +from qtpy.QtWidgets import ( + QHBoxLayout, + QLabel, + QProgressBar, + QSizePolicy, + QSlider, + QSpacerItem, + QVBoxLayout, + QWidget, +) from __code._utilities.parent import Parent from __code.radial_profile.event_handler import EventHandler @@ -64,9 +73,15 @@ def widgets(self): # self.parent.ui.lineEdit.setText(str(self.parent.grid_size)) self.parent.ui.guide_red_slider.setValue(self.parent.guide_color_slider["red"]) - self.parent.ui.guide_green_slider.setValue(self.parent.guide_color_slider["green"]) - self.parent.ui.guide_blue_slider.setValue(self.parent.guide_color_slider["blue"]) - self.parent.ui.guide_alpha_slider.setValue(self.parent.guide_color_slider["alpha"]) + self.parent.ui.guide_green_slider.setValue( + self.parent.guide_color_slider["green"] + ) + self.parent.ui.guide_blue_slider.setValue( + self.parent.guide_color_slider["blue"] + ) + self.parent.ui.guide_alpha_slider.setValue( + self.parent.guide_color_slider["alpha"] + ) self.parent.ui.sector_from_value.setText(str(self.parent.sector_range["from"])) self.parent.ui.sector_to_value.setText(str(self.parent.sector_range["to"])) diff --git a/notebooks/__code/radial_profile/radial_profile.py b/notebooks/__code/radial_profile/radial_profile.py index 74d216f3a..e56e27bfc 100755 --- a/notebooks/__code/radial_profile/radial_profile.py +++ b/notebooks/__code/radial_profile/radial_profile.py @@ -36,7 +36,9 @@ def __init__(self, parent_ui=None, data=None, list_files=None, working_dir=""): self.short_list_files = [os.path.basename(_file) for _file in list_files] color = Color() - self.list_rgb_profile_color = color.get_list_rgb(nbr_color=len(self.working_data)) + self.list_rgb_profile_color = color.get_list_rgb( + nbr_color=len(self.working_data) + ) def calculate(self, center=None, angle_range=None, max_radius=None): QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) @@ -68,7 +70,9 @@ def calculate(self, center=None, angle_range=None, max_radius=None): for _index in np.arange(nbr_files): o_calculation = CalculateRadialProfile(data=self.working_data[_index]) - o_calculation.add_params(center=center, angle_range=angle_range, radius=max_radius) + o_calculation.add_params( + center=center, angle_range=angle_range, radius=max_radius + ) o_calculation.calculate() _short_file_name = self.short_list_files[_index] @@ -105,13 +109,18 @@ def export(self, output_folder=""): make_or_reset_folder(output_folder) for _index, _file in enumerate(self.list_images): - time_stamp_of_that_file = MetadataHandler.get_time_stamp(file_name=_file) + time_stamp_of_that_file = MetadataHandler.get_time_stamp( + file_name=_file + ) [input_image_base_name, ext] = os.path.splitext(os.path.basename(_file)) output_file_name = os.path.join( output_folder, input_image_base_name + "_profile_c_x{}_y{}_angle_{}_to_{}".format( - self.center["x0"], self.center["y0"], self.angle_range["from"], self.angle_range["to"] + self.center["x0"], + self.center["y0"], + self.angle_range["from"], + self.angle_range["to"], ), ) if self.max_radius: @@ -121,7 +130,11 @@ def export(self, output_folder=""): text = [f"# source image: {_file}"] text.append(f"# timestamp: {time_stamp_of_that_file}") - text.append("# center [x0, y0]: [{},{}]".format(self.center["x0"], self.center["y0"])) + text.append( + "# center [x0, y0]: [{},{}]".format( + self.center["x0"], self.center["y0"] + ) + ) text.append( "# angular range from {}degrees to {}degrees".format( self.angle_range["from"], self.angle_range["to"] @@ -129,11 +142,21 @@ def export(self, output_folder=""): ) text.append("") text.append("#pixel_from_center, Average_counts") - data = list(zip(np.arange(len(self.profile_data[_index])), self.profile_data[_index], strict=False)) + data = list( + zip( + np.arange(len(self.profile_data[_index])), + self.profile_data[_index], + strict=False, + ) + ) - file_handler.make_ascii_file(metadata=text, data=data, output_file_name=output_file_name) + file_handler.make_ascii_file( + metadata=text, data=data, output_file_name=output_file_name + ) - self.parent_ui.ui.statusbar.showMessage(f"Profiles Exported in {output_folder}!", 10000) + self.parent_ui.ui.statusbar.showMessage( + f"Profiles Exported in {output_folder}!", 10000 + ) self.parent_ui.ui.statusbar.setStyleSheet("color: green") @@ -152,7 +175,12 @@ class SelectRadialParameters(QMainWindow): sector_range = {"from": 0, "to": 90} - corners = {"top_right": np.nan, "bottom_right": np.nan, "bottom_left": np.nan, "top_left": np.nan} + corners = { + "top_right": np.nan, + "bottom_right": np.nan, + "bottom_left": np.nan, + "top_left": np.nan, + } hLine = None vLine = None @@ -184,7 +212,8 @@ def __init__(self, parent=None, working_dir="", data_dict=None): super(QMainWindow, self).__init__(parent) ui_full_path = os.path.join( - os.path.dirname(os.path.dirname(os.path.dirname(__file__))), os.path.join("ui", "ui_radial_profile.ui") + os.path.dirname(os.path.dirname(os.path.dirname(__file__))), + os.path.join("ui", "ui_radial_profile.ui"), ) self.ui = load_ui(ui_full_path, baseinstance=self) self.setWindowTitle("Define center and sector of profile") @@ -226,7 +255,9 @@ def __init__(self, parent=None, working_dir="", data_dict=None): def help_button_clicked(self): import webbrowser - webbrowser.open("https://neutronimaging.pages.ornl.gov/tutorial/notebooks/radial_profile/") + webbrowser.open( + "https://neutronimaging.pages.ornl.gov/tutorial/notebooks/radial_profile/" + ) def grid_slider_moved(self, value): self.grid_size_changed() @@ -282,10 +313,19 @@ def grid_size_changed(self): def calculate_profiles_clicked(self): o_profile = RadialProfile( - parent_ui=self, data=self.working_data, list_files=self.list_images, working_dir=self.working_dir + parent_ui=self, + data=self.working_data, + list_files=self.list_images, + working_dir=self.working_dir, + ) + radius = ( + self.ui.max_radius_slider.value() + if self.ui.max_radius_radioButton.isChecked() + else None + ) + o_profile.calculate( + center=self.center, angle_range=self.angle_range, max_radius=radius ) - radius = self.ui.max_radius_slider.value() if self.ui.max_radius_radioButton.isChecked() else None - o_profile.calculate(center=self.center, angle_range=self.angle_range, max_radius=radius) self.profile_data = o_profile.profile_data self.o_profile = o_profile @@ -293,7 +333,10 @@ def calculate_profiles_clicked(self): def export_profiles_clicked(self): _export_folder = QFileDialog.getExistingDirectory( - self, directory=self.working_dir, caption="Select Output Folder", options=QFileDialog.ShowDirsOnly + self, + directory=self.working_dir, + caption="Select Output Folder", + options=QFileDialog.ShowDirsOnly, ) QApplication.processEvents() if _export_folder: diff --git a/notebooks/__code/registration/calculate.py b/notebooks/__code/registration/calculate.py index 0f9b37d48..a9545d5e4 100755 --- a/notebooks/__code/registration/calculate.py +++ b/notebooks/__code/registration/calculate.py @@ -59,7 +59,10 @@ def intermediates_points(p1, p2): x_spacing = (p2[0] - p1[0]) / (nb_points + 1) y_spacing = (p2[1] - p1[1]) / (nb_points + 1) - full_array = [[int(p1[0] + i * x_spacing), int(p1[1] + i * y_spacing)] for i in range(1, nb_points + 1)] + full_array = [ + [int(p1[0] + i * x_spacing), int(p1[1] + i * y_spacing)] + for i in range(1, nb_points + 1) + ] clean_array = [] for _points in full_array: diff --git a/notebooks/__code/registration/calculate_profiles_difference.py b/notebooks/__code/registration/calculate_profiles_difference.py index 61de22391..6b6ce45db 100755 --- a/notebooks/__code/registration/calculate_profiles_difference.py +++ b/notebooks/__code/registration/calculate_profiles_difference.py @@ -38,7 +38,9 @@ def run(self): for _key in self.roi["horizontal"]["profiles"].keys(): _profile = self.roi["horizontal"]["profiles"][_key]["profile"] offset_found = CalculateProfilesDifference.calculate_pixel_offset( - profile_reference=reference_profile, working_profile=_profile, max_pixel_range=MAX_PIXEL_RANGE + profile_reference=reference_profile, + working_profile=_profile, + max_pixel_range=MAX_PIXEL_RANGE, ) self.parent.offset["horizontal"].append(-offset_found) @@ -49,7 +51,9 @@ def run(self): for _key in self.roi["vertical"]["profiles"].keys(): _profile = self.roi["vertical"]["profiles"][_key]["profile"] offset_found = CalculateProfilesDifference.calculate_pixel_offset( - profile_reference=reference_profile, working_profile=_profile, max_pixel_range=MAX_PIXEL_RANGE + profile_reference=reference_profile, + working_profile=_profile, + max_pixel_range=MAX_PIXEL_RANGE, ) self.parent.offset["vertical"].append(-offset_found) @@ -60,14 +64,18 @@ def sum_abs_diff(profile_a, profile_b): return np.sum(abs_list_diff) @staticmethod - def calculate_pixel_offset(profile_reference=None, working_profile=None, max_pixel_range=20): + def calculate_pixel_offset( + profile_reference=None, working_profile=None, max_pixel_range=20 + ): list_profiles = [] for _offset in np.arange(-max_pixel_range, max_pixel_range): list_profiles.append(np.roll(working_profile, _offset)) list_sum_abs_diff = [] for _profile in list_profiles: - list_sum_abs_diff.append(CalculateProfilesDifference.sum_abs_diff(_profile, profile_reference)) + list_sum_abs_diff.append( + CalculateProfilesDifference.sum_abs_diff(_profile, profile_reference) + ) min_value = np.min(list_sum_abs_diff) min_index = np.where(min_value == list_sum_abs_diff)[0][0] diff --git a/notebooks/__code/registration/display.py b/notebooks/__code/registration/display.py index d56bf2151..5dfeec193 100755 --- a/notebooks/__code/registration/display.py +++ b/notebooks/__code/registration/display.py @@ -19,7 +19,9 @@ def image(self): _image = o_get.image_selected() else: # display selected images according to slider position # retrieve slider infos - slider_index = self.parent.ui.opacity_selection_slider.sliderPosition() / 100 + slider_index = ( + self.parent.ui.opacity_selection_slider.sliderPosition() / 100 + ) from_index = int(slider_index) to_index = int(slider_index + 1) @@ -33,7 +35,9 @@ def image(self): _from_coefficient = np.abs(to_index - slider_index) _to_coefficient = np.abs(slider_index - from_index) - _image = _from_image * _from_coefficient + _to_image * _to_coefficient + _image = ( + _from_image * _from_coefficient + _to_image * _to_coefficient + ) else: # only 1 row selected _image = o_get.image_selected() @@ -54,7 +58,9 @@ def image(self): _histo_widget = self.parent.ui.image_view.getHistogramWidget() self.parent.histogram_level = _histo_widget.getLevels() - _opacity_coefficient = self.parent.ui.opacity_slider.value() # betwween 0 and 100 + _opacity_coefficient = ( + self.parent.ui.opacity_slider.value() + ) # betwween 0 and 100 _opacity_image = _opacity_coefficient / 100.0 _image = np.transpose(_image) * _opacity_image @@ -67,7 +73,9 @@ def image(self): _view_box.setState(_state) if not first_update: - _histo_widget.setLevels(self.parent.histogram_level[0], self.parent.histogram_level[1]) + _histo_widget.setLevels( + self.parent.histogram_level[0], self.parent.histogram_level[1] + ) def display_only_reference_image(self): self.parent.ui.selection_reference_opacity_groupBox.setVisible(False) @@ -88,7 +96,9 @@ def display_only_reference_image(self): _view_box.setState(_state) if not first_update: - _histo_widget.setLevels(self.parent.histogram_level[0], self.parent.histogram_level[1]) + _histo_widget.setLevels( + self.parent.histogram_level[0], self.parent.histogram_level[1] + ) def live_image(self): """no calculation will be done. This will only display the reference image @@ -108,7 +118,9 @@ def live_image(self): _view_box.setState(_state) if not first_update: - _histo_widget.setLevels(self.parent.histogram_level[0], self.parent.histogram_level[1]) + _histo_widget.setLevels( + self.parent.histogram_level[0], self.parent.histogram_level[1] + ) # we do not want a grid on top if self.parent.grid_view["item"]: @@ -120,14 +132,22 @@ def live_image(self): grid_size = self.parent.ui.grid_size_slider.value() [width, height] = np.shape(live_image) - pos_adj_dict = Calculate.calculate_matrix_grid(grid_size=grid_size, height=height, width=width) + pos_adj_dict = Calculate.calculate_matrix_grid( + grid_size=grid_size, height=height, width=width + ) pos = pos_adj_dict["pos"] adj = pos_adj_dict["adj"] line_color = self.parent.grid_view["color"] lines = np.array( [line_color for n in np.arange(len(pos))], - dtype=[("red", np.ubyte), ("green", np.ubyte), ("blue", np.ubyte), ("alpha", np.ubyte), ("width", float)], + dtype=[ + ("red", np.ubyte), + ("green", np.ubyte), + ("blue", np.ubyte), + ("alpha", np.ubyte), + ("width", float), + ], ) grid = pg.GraphItem() diff --git a/notebooks/__code/registration/event_handler.py b/notebooks/__code/registration/event_handler.py index db2ff873a..5e5e56934 100755 --- a/notebooks/__code/registration/event_handler.py +++ b/notebooks/__code/registration/event_handler.py @@ -68,7 +68,9 @@ def paste_value_copied(self, column=1): self.parent.ui.tableWidget.blockSignals(True) for _row in row_selected: - o_table.set_item_with_str(row=_row, column=column, cell_str=self.parent.value_to_copy) + o_table.set_item_with_str( + row=_row, column=column, cell_str=self.parent.value_to_copy + ) self.parent.ui.tableWidget.blockSignals(False) @@ -77,7 +79,9 @@ def update_table_according_to_filter(self): o_table = TableHandler(table_ui=self.parent.ui.tableWidget) - def should_row_be_visible(row_value=None, filter_algo_selected="<=", filter_value=None): + def should_row_be_visible( + row_value=None, filter_algo_selected="<=", filter_value=None + ): if is_float(filter_value): o_table.set_all_row_hidden(False) return @@ -91,7 +95,9 @@ def should_row_be_visible(row_value=None, filter_algo_selected="<=", filter_valu if filter_flag: # select only rows according to filter - filter_column_selected = self.parent.ui.filter_column_name_comboBox.currentText() + filter_column_selected = ( + self.parent.ui.filter_column_name_comboBox.currentText() + ) filter_algo_selected = self.parent.ui.filter_logic_comboBox.currentText() filter_value = self.parent.ui.filter_value.text() @@ -106,9 +112,13 @@ def should_row_be_visible(row_value=None, filter_algo_selected="<=", filter_valu nbr_row = o_table.row_count() for _row in np.arange(nbr_row): - _row_value = float(o_table.get_item_str_from_cell(row=_row, column=filter_column_index)) + _row_value = float( + o_table.get_item_str_from_cell(row=_row, column=filter_column_index) + ) _should_row_be_visible = should_row_be_visible( - row_value=_row_value, filter_algo_selected=filter_algo_selected, filter_value=filter_value + row_value=_row_value, + filter_algo_selected=filter_algo_selected, + filter_value=filter_value, ) o_table.set_row_hidden(_row, not _should_row_be_visible) else: @@ -144,7 +154,9 @@ def profile_line_moved(self): self.parent.legend = self.parent.ui.profile.addLegend() - region = self.parent.ui.profile_line.getArraySlice(self.parent.live_image, self.parent.ui.image_view.imageItem) + region = self.parent.ui.profile_line.getArraySlice( + self.parent.live_image, self.parent.ui.image_view.imageItem + ) x0 = region[0][0].start + 3 x1 = region[0][0].stop - 3 @@ -169,20 +181,36 @@ def profile_line_moved(self): continue _data = np.transpose(self.parent.data_dict["data"][_index]) - _filename = os.path.basename(self.parent.data_dict["file_name"][_index]) - _profile = [_data[_point[0], _point[1]] for _point in intermediate_points] + _filename = os.path.basename( + self.parent.data_dict["file_name"][_index] + ) + _profile = [ + _data[_point[0], _point[1]] for _point in intermediate_points + ] self.parent.ui.profile.plot( - xaxis, _profile, name=_filename, pen=self.parent.list_rgb_profile_color[_index] + xaxis, + _profile, + name=_filename, + pen=self.parent.list_rgb_profile_color[_index], ) else: # selection slider - slider_index = self.parent.ui.opacity_selection_slider.sliderPosition() / 100 + slider_index = ( + self.parent.ui.opacity_selection_slider.sliderPosition() / 100 + ) from_index = int(slider_index) _data = np.transpose(self.parent.data_dict["data"][from_index]) - _filename = os.path.basename(self.parent.data_dict["file_name"][from_index]) - _profile = [_data[_point[0], _point[1]] for _point in intermediate_points] + _filename = os.path.basename( + self.parent.data_dict["file_name"][from_index] + ) + _profile = [ + _data[_point[0], _point[1]] for _point in intermediate_points + ] self.parent.ui.profile.plot( - xaxis, _profile, name=_filename, pen=self.parent.list_rgb_profile_color[from_index] + xaxis, + _profile, + name=_filename, + pen=self.parent.list_rgb_profile_color[from_index], ) if from_index == slider_index: @@ -191,10 +219,17 @@ def profile_line_moved(self): else: to_index = int(slider_index + 1) _data = np.transpose(self.parent.data_dict["data"][to_index]) - _filename = os.path.basename(self.parent.data_dict["file_name"][to_index]) - _profile = [_data[_point[0], _point[1]] for _point in intermediate_points] + _filename = os.path.basename( + self.parent.data_dict["file_name"][to_index] + ) + _profile = [ + _data[_point[0], _point[1]] for _point in intermediate_points + ] self.parent.ui.profile.plot( - xaxis, _profile, name=_filename, pen=self.parent.list_rgb_profile_color[to_index] + xaxis, + _profile, + name=_filename, + pen=self.parent.list_rgb_profile_color[to_index], ) else: @@ -205,10 +240,17 @@ def profile_line_moved(self): if not row_selected == self.parent.reference_image_index: _data = np.transpose(self.parent.data_dict["data"][row_selected]) - _filename = os.path.basename(self.parent.data_dict["file_name"][row_selected]) - _profile = [_data[_point[0], _point[1]] for _point in intermediate_points] + _filename = os.path.basename( + self.parent.data_dict["file_name"][row_selected] + ) + _profile = [ + _data[_point[0], _point[1]] for _point in intermediate_points + ] self.parent.ui.profile.plot( - xaxis, _profile, name=_filename, pen=self.parent.list_rgb_profile_color[row_selected] + xaxis, + _profile, + name=_filename, + pen=self.parent.list_rgb_profile_color[row_selected], ) # selected_image = self.parent.live_image @@ -219,11 +261,18 @@ def profile_line_moved(self): # Always display profile reference reference_image = np.transpose(self.parent.reference_image) - profile_reference = [reference_image[_point[0], _point[1]] for _point in intermediate_points] + profile_reference = [ + reference_image[_point[0], _point[1]] for _point in intermediate_points + ] - reference_file_name = os.path.basename(self.parent.data_dict["file_name"][self.parent.reference_image_index]) + reference_file_name = os.path.basename( + self.parent.data_dict["file_name"][self.parent.reference_image_index] + ) self.parent.ui.profile.plot( - xaxis, profile_reference, pen=self.parent.color_reference_profile, name=f"Ref.: {reference_file_name}" + xaxis, + profile_reference, + pen=self.parent.color_reference_profile, + name=f"Ref.: {reference_file_name}", ) def modified_images(self, list_row=[], all_row=False): diff --git a/notebooks/__code/registration/export.py b/notebooks/__code/registration/export.py index 996f74b92..0ed82d197 100755 --- a/notebooks/__code/registration/export.py +++ b/notebooks/__code/registration/export.py @@ -17,15 +17,24 @@ def __init__(self, parent=None): def run(self): _export_folder = QFileDialog.getExistingDirectory( - self.parent, directory=self.working_dir, caption="Select Output Folder", options=QFileDialog.ShowDirsOnly + self.parent, + directory=self.working_dir, + caption="Select Output Folder", + options=QFileDialog.ShowDirsOnly, ) if _export_folder: # add custom folder name working_dir_basename = os.path.basename(self.working_dir) # append "registered" and "time_stamp" - full_output_folder_name = os.path.join(_export_folder, working_dir_basename + "_registered") - full_output_folder_name = make_or_increment_folder_name(full_output_folder_name) - o_export = ExportRegistration(parent=self.parent, export_folder=full_output_folder_name) + full_output_folder_name = os.path.join( + _export_folder, working_dir_basename + "_registered" + ) + full_output_folder_name = make_or_increment_folder_name( + full_output_folder_name + ) + o_export = ExportRegistration( + parent=self.parent, export_folder=full_output_folder_name + ) o_export.run() QApplication.processEvents() @@ -47,12 +56,19 @@ def run(self): for _row, _data in enumerate(data_dict_raw["data"]): _filename = list_file_names[_row] if not _row == self.parent.reference_image_index: - _xoffset = int(np.floor(float(self.parent.ui.tableWidget.item(_row, 1).text()))) - _yoffset = int(np.floor(float(self.parent.ui.tableWidget.item(_row, 2).text()))) + _xoffset = int( + np.floor(float(self.parent.ui.tableWidget.item(_row, 1).text())) + ) + _yoffset = int( + np.floor(float(self.parent.ui.tableWidget.item(_row, 2).text())) + ) _rotation = float(self.parent.ui.tableWidget.item(_row, 3).text()) _data_registered = self.registered_data( - raw_data=_data, xoffset=_xoffset, yoffset=_yoffset, rotation=_rotation + raw_data=_data, + xoffset=_xoffset, + yoffset=_yoffset, + rotation=_rotation, ) else: _data_registered = _data diff --git a/notebooks/__code/registration/file_selection.py b/notebooks/__code/registration/file_selection.py index 2145a3ec9..979ce6f5b 100755 --- a/notebooks/__code/registration/file_selection.py +++ b/notebooks/__code/registration/file_selection.py @@ -12,7 +12,9 @@ def __init__(self, working_dir="./"): self.working_dir = working_dir def select_file_help(self, value): - webbrowser.open("https://neutronimaging.ornl.gov/tutorials/imaging-notebooks/file-selector-tool/") + webbrowser.open( + "https://neutronimaging.ornl.gov/tutorials/imaging-notebooks/file-selector-tool/" + ) def load_files(self, files): files.sort() @@ -26,7 +28,10 @@ def select_data(self): display(help_ui) self.files_ui = fileselector.FileSelectorPanel( - instruction="Select Images ...", start_dir=self.working_dir, next=self.load_files, multiple=True + instruction="Select Images ...", + start_dir=self.working_dir, + next=self.load_files, + multiple=True, ) self.files_ui.show() diff --git a/notebooks/__code/registration/get.py b/notebooks/__code/registration/get.py index a31394dd2..50bc0c16e 100755 --- a/notebooks/__code/registration/get.py +++ b/notebooks/__code/registration/get.py @@ -14,7 +14,9 @@ def get_list_short_file_selected(self): list_row_selected = self.list_row_selected() full_list_files = np.array(self.parent.data_dict["file_name"]) list_file_selected = full_list_files[list_row_selected] - list_short_file_selected = [os.path.basename(_file) for _file in list_file_selected] + list_short_file_selected = [ + os.path.basename(_file) for _file in list_file_selected + ] return list_short_file_selected def list_row_selected(self): @@ -40,7 +42,9 @@ def image_selected(self): return [] table_selection = table_selection[0] - top_row = table_selection.topRow() # offset because first image is reference image + top_row = ( + table_selection.topRow() + ) # offset because first image is reference image bottom_row = table_selection.bottomRow() + 1 _image = np.mean(self.parent.data_dict["data"][top_row:bottom_row], axis=0) diff --git a/notebooks/__code/registration/initialization.py b/notebooks/__code/registration/initialization.py index d15c876c6..bbcee9ea4 100755 --- a/notebooks/__code/registration/initialization.py +++ b/notebooks/__code/registration/initialization.py @@ -50,7 +50,9 @@ def pyqtgrpah(self): self.parent.ui.profile_line = pg.LineSegmentROI([[50, 50], [100, 100]], pen="r") self.parent.ui.image_view.addItem(self.parent.ui.profile_line) d1.addWidget(self.parent.ui.image_view) - self.parent.ui.profile_line.sigRegionChanged.connect(self.parent.profile_line_moved) + self.parent.ui.profile_line.sigRegionChanged.connect( + self.parent.profile_line_moved + ) # profile self.parent.ui.profile = pg.PlotWidget(title="Profile") @@ -71,7 +73,9 @@ def widgets(self): # update size of table columns nbr_columns = self.parent.ui.tableWidget.columnCount() for _col in range(nbr_columns): - self.parent.ui.tableWidget.setColumnWidth(_col, self.parent.table_column_width[_col]) + self.parent.ui.tableWidget.setColumnWidth( + _col, self.parent.table_column_width[_col] + ) # update slide widget of files nbr_files = len(self.parent.data_dict["file_name"]) diff --git a/notebooks/__code/registration/manual.py b/notebooks/__code/registration/manual.py index 6a44120ae..f34b532d4 100755 --- a/notebooks/__code/registration/manual.py +++ b/notebooks/__code/registration/manual.py @@ -38,7 +38,8 @@ def __init__(self, parent=None): super(QMainWindow, self).__init__(parent) ui_full_path = os.path.join( - os.path.dirname(os.path.dirname(os.path.dirname(__file__))), os.path.join("ui", "ui_registration_tool.ui") + os.path.dirname(os.path.dirname(os.path.dirname(__file__))), + os.path.join("ui", "ui_registration_tool.ui"), ) self.ui = load_ui(ui_full_path, baseinstance=self) self.parent = parent @@ -48,45 +49,72 @@ def __init__(self, parent=None): def initialize_widgets(self): _file_path = os.path.dirname(__file__) - up_arrow_file = os.path.abspath(os.path.join(_file_path, "../static/up_arrow.png")) + up_arrow_file = os.path.abspath( + os.path.join(_file_path, "../static/up_arrow.png") + ) self.ui.up_button.setIcon(QIcon(up_arrow_file)) - down_arrow_file = os.path.abspath(os.path.join(_file_path, "../static/down_arrow.png")) + down_arrow_file = os.path.abspath( + os.path.join(_file_path, "../static/down_arrow.png") + ) self.ui.down_button.setIcon(QIcon(down_arrow_file)) - right_arrow_file = os.path.abspath(os.path.join(_file_path, "../static/right_arrow.png")) + right_arrow_file = os.path.abspath( + os.path.join(_file_path, "../static/right_arrow.png") + ) self.ui.right_button.setIcon(QIcon(right_arrow_file)) - left_arrow_file = os.path.abspath(os.path.join(_file_path, "../static/left_arrow.png")) + left_arrow_file = os.path.abspath( + os.path.join(_file_path, "../static/left_arrow.png") + ) self.ui.left_button.setIcon(QIcon(left_arrow_file)) - rotate_left_file = os.path.abspath(os.path.join(_file_path, "../static/rotate_left.png")) + rotate_left_file = os.path.abspath( + os.path.join(_file_path, "../static/rotate_left.png") + ) self.ui.rotate_left_button.setIcon(QIcon(rotate_left_file)) - rotate_right_file = os.path.abspath(os.path.join(_file_path, "../static/rotate_right.png")) + rotate_right_file = os.path.abspath( + os.path.join(_file_path, "../static/rotate_right.png") + ) self.ui.rotate_right_button.setIcon(QIcon(rotate_right_file)) - small_rotate_left_file = os.path.abspath(os.path.join(_file_path, "../static/small_rotate_left.png")) + small_rotate_left_file = os.path.abspath( + os.path.join(_file_path, "../static/small_rotate_left.png") + ) self.ui.small_rotate_left_button.setIcon(QIcon(small_rotate_left_file)) - small_rotate_right_file = os.path.abspath(os.path.join(_file_path, "../static/small_rotate_right.png")) + small_rotate_right_file = os.path.abspath( + os.path.join(_file_path, "../static/small_rotate_right.png") + ) self.ui.small_rotate_right_button.setIcon(QIcon(small_rotate_right_file)) - self.list_arrow_widgets = [self.ui.up_button, self.ui.down_button, self.ui.left_button, self.ui.right_button] + self.list_arrow_widgets = [ + self.ui.up_button, + self.ui.down_button, + self.ui.left_button, + self.ui.right_button, + ] self._set_widgets_size( widgets=self.list_arrow_widgets, width=self.button_size["arrow"]["width"], height=self.button_size["arrow"]["height"], ) - self.list_rotate_widgets = [self.ui.rotate_left_button, self.ui.rotate_right_button] + self.list_rotate_widgets = [ + self.ui.rotate_left_button, + self.ui.rotate_right_button, + ] self._set_widgets_size( widgets=self.list_rotate_widgets, width=self.button_size["rotate"]["width"], height=self.button_size["rotate"]["height"], ) - self.list_small_rotate_widgets = [self.ui.small_rotate_left_button, self.ui.small_rotate_right_button] + self.list_small_rotate_widgets = [ + self.ui.small_rotate_left_button, + self.ui.small_rotate_right_button, + ] self._set_widgets_size( widgets=self.list_small_rotate_widgets, width=self.button_size["small_rotate"]["width"], diff --git a/notebooks/__code/registration/marker_handler.py b/notebooks/__code/registration/marker_handler.py index 2a02b17ce..f8b92d57a 100755 --- a/notebooks/__code/registration/marker_handler.py +++ b/notebooks/__code/registration/marker_handler.py @@ -13,8 +13,12 @@ def display_markers(self, all=False): return if all is False: - _current_tab = self.parent.registration_markers_ui.ui.tabWidget.currentIndex() - _tab_title = self.parent.registration_markers_ui.ui.tabWidget.tabText(_current_tab) + _current_tab = ( + self.parent.registration_markers_ui.ui.tabWidget.currentIndex() + ) + _tab_title = self.parent.registration_markers_ui.ui.tabWidget.tabText( + _current_tab + ) self.display_markers_of_tab(marker_name=_tab_title) else: for _index, _marker_name in enumerate(self.parent.markers_table.keys()): @@ -60,11 +64,19 @@ def display_markers_of_tab(self, marker_name=""): _marker_ui.removeHandle(0) _marker_ui.sigRegionChanged.connect(self.parent.marker_has_been_moved) - if nbr_file_selected > 1: # more than 1 file selected, we need to add the index of the file + if ( + nbr_file_selected > 1 + ): # more than 1 file selected, we need to add the index of the file text_ui = self.parent.add_marker_label( - file_index=list_row_selected[_index], marker_index=marker_name, x=x, y=y, color=_color_marker + file_index=list_row_selected[_index], + marker_index=marker_name, + x=x, + y=y, + color=_color_marker, + ) + self.parent.markers_table[marker_name]["data"][_file]["label_ui"] = ( + text_ui ) - self.parent.markers_table[marker_name]["data"][_file]["label_ui"] = text_ui _marker_data["marker_ui"] = _marker_ui @@ -81,7 +93,9 @@ def marker_has_been_moved(self): _marker_data = self.parent.markers_table[_marker_name]["data"][_file] marker_ui = _marker_data["marker_ui"] - region = marker_ui.getArraySlice(self.parent.live_image, self.parent.ui.image_view.imageItem) + region = marker_ui.getArraySlice( + self.parent.live_image, self.parent.ui.image_view.imageItem + ) x0 = region[0][0].start y0 = region[0][1].start @@ -89,7 +103,9 @@ def marker_has_been_moved(self): self.parent.markers_table[_marker_name]["data"][_file]["x"] = x0 self.parent.markers_table[_marker_name]["data"][_file]["y"] = y0 - self.parent.registration_markers_ui.update_markers_table_entry(marker_name=_marker_name, file=_file) + self.parent.registration_markers_ui.update_markers_table_entry( + marker_name=_marker_name, file=_file + ) if nbr_file_selected > 1: _label_ui = _marker_data["label_ui"] @@ -102,12 +118,19 @@ def marker_has_been_moved(self): color=_color_marker, ) self.parent.ui.image_view.addItem(_label_ui) - self.parent.markers_table[_marker_name]["data"][_file]["label_ui"] = _label_ui + self.parent.markers_table[_marker_name]["data"][_file][ + "label_ui" + ] = _label_ui def add_marker_label(self, file_index=0, marker_index=1, x=0, y=0, color="white"): html_color = MarkerDefaultSettings.color_html[color] html_text = '
Marker#:' - html_text += '' + str(int(marker_index) + 1) + html_text += ( + '' + + str(int(marker_index) + 1) + ) html_text += " - File#:" html_text += '' + str(file_index) html_text += "" diff --git a/notebooks/__code/registration/registration.py b/notebooks/__code/registration/registration.py index 9101b31d6..a5b927a2f 100755 --- a/notebooks/__code/registration/registration.py +++ b/notebooks/__code/registration/registration.py @@ -18,7 +18,9 @@ from __code.registration.manual import ManualLauncher from __code.registration.marker_handler import MarkerHandler from __code.registration.registration_auto import RegistrationAuto -from __code.registration.registration_auto_confirmation import RegistrationAutoConfirmationLauncher +from __code.registration.registration_auto_confirmation import ( + RegistrationAutoConfirmationLauncher, +) from __code.registration.registration_marker import RegistrationMarkersLauncher from __code.registration.registration_profile import RegistrationProfileLauncher from __code.registration.table_handler import TableHandler @@ -85,7 +87,8 @@ def __init__(self, parent=None, data_dict=None): ) ) ui_full_path = os.path.join( - os.path.dirname(os.path.dirname(os.path.dirname(__file__))), os.path.join("ui", "ui_registration.ui") + os.path.dirname(os.path.dirname(os.path.dirname(__file__))), + os.path.join("ui", "ui_registration.ui"), ) self.ui = load_ui(ui_full_path, baseinstance=self) @@ -100,7 +103,9 @@ def __init__(self, parent=None, data_dict=None): self.data_dict_raw = copy.deepcopy(data_dict) self.reference_image = self.data_dict["data"][self.reference_image_index] self.working_dir = os.path.dirname(self.data_dict["file_name"][0]) - self.reference_image_short_name = str(os.path.basename(self.data_dict["file_name"][0])) + self.reference_image_short_name = str( + os.path.basename(self.data_dict["file_name"][0]) + ) # initialization o_init = Initialization(parent=self) @@ -116,7 +121,11 @@ def __init__(self, parent=None, data_dict=None): ) # because by default first row = reference selected def filter_checkbox_clicked(self): - list_ui = [self.ui.filter_column_name_comboBox, self.ui.filter_logic_comboBox, self.ui.filter_value] + list_ui = [ + self.ui.filter_column_name_comboBox, + self.ui.filter_logic_comboBox, + self.ui.filter_value, + ] for _ui in list_ui: _ui.setEnabled(self.ui.filter_checkBox.isChecked()) @@ -230,7 +239,9 @@ def slider_file_changed(self, index_selected): self.ui.tableWidget.blockSignals(False) def help_button_clicked(self): - webbrowser.open("https://neutronimaging.pages.ornl.gov/tutorial/notebooks/registration/") + webbrowser.open( + "https://neutronimaging.pages.ornl.gov/tutorial/notebooks/registration/" + ) def ok_button_clicked(self): self.close() @@ -258,7 +269,11 @@ def next_image_button_clicked(self): def selection_all_clicked(self): _is_checked = self.ui.selection_all.isChecked() - list_widgets = [self.ui.top_row_label, self.ui.bottom_row_label, self.ui.opacity_selection_slider] + list_widgets = [ + self.ui.top_row_label, + self.ui.bottom_row_label, + self.ui.opacity_selection_slider, + ] for _widget in list_widgets: _widget.setEnabled(not _is_checked) self.display_image() @@ -294,7 +309,9 @@ def manual_registration_button_clicked(self): ) def auto_registration_button_clicked(self): - o_registration_auto_confirmed = RegistrationAutoConfirmationLauncher(parent=self) + o_registration_auto_confirmed = RegistrationAutoConfirmationLauncher( + parent=self + ) def markers_registration_button_clicked(self): o_markers_registration = RegistrationMarkersLauncher(parent=self) @@ -320,7 +337,9 @@ def profiler_registration_button_clicked(self): def start_auto_registration(self): o_auto_register = RegistrationAuto( - parent=self, reference_image=self.reference_image, floating_images=self.data_dict["data"] + parent=self, + reference_image=self.reference_image, + floating_images=self.data_dict["data"], ) o_auto_register.auto_align() diff --git a/notebooks/__code/registration/registration_auto.py b/notebooks/__code/registration/registration_auto.py index 8582cbfc2..b5893b4f8 100755 --- a/notebooks/__code/registration/registration_auto.py +++ b/notebooks/__code/registration/registration_auto.py @@ -25,7 +25,9 @@ def auto_align(self): self.parent.eventProgress.setVisible(True) for _row, _image in enumerate(_list_images): - [yoffset, xoffset], error, diffphase = register_translation(_ref_image, _image) + [yoffset, xoffset], error, diffphase = register_translation( + _ref_image, _image + ) if not _row == self.parent.reference_image_index: self.parent.set_item(row=_row, col=1, value=xoffset) self.parent.set_item(row=_row, col=2, value=yoffset) diff --git a/notebooks/__code/registration/registration_auto_confirmation.py b/notebooks/__code/registration/registration_auto_confirmation.py index 0d83070dd..e884120ab 100755 --- a/notebooks/__code/registration/registration_auto_confirmation.py +++ b/notebooks/__code/registration/registration_auto_confirmation.py @@ -35,7 +35,9 @@ def __init__(self, parent=None): def initialize_widgets(self): _file_path = os.path.dirname(__file__) - warning_image_file = os.path.abspath(os.path.join(_file_path, "../static/warning_icon.png")) + warning_image_file = os.path.abspath( + os.path.join(_file_path, "../static/warning_icon.png") + ) warning_image = QPixmap(warning_image_file) self.ui.warning_label.setPixmap(warning_image) diff --git a/notebooks/__code/registration/registration_marker.py b/notebooks/__code/registration/registration_marker.py index 7a59d279d..685b3f766 100755 --- a/notebooks/__code/registration/registration_marker.py +++ b/notebooks/__code/registration/registration_marker.py @@ -70,7 +70,9 @@ def resizing_column(self, index_column, old_size, new_size): _table_ui = self.parent.markers_table[_key]["ui"] if not (_table_ui == _live_table_ui): for _col, _size in enumerate(self.parent.markers_table_column_width): - _table_ui.setColumnWidth(_col, self.parent.markers_table_column_width[_col]) + _table_ui.setColumnWidth( + _col, self.parent.markers_table_column_width[_col] + ) def init_widgets(self): if self.parent.markers_table == {}: @@ -109,14 +111,20 @@ def populate_using_markers_table(self): selected_row = o_main_table.get_row_selected() for _col, _size in enumerate(self.parent.markers_table_column_width): - _table.setColumnWidth(_col, self.parent.markers_table_column_width[_col]) + _table.setColumnWidth( + _col, self.parent.markers_table_column_width[_col] + ) _table.horizontalHeader().sectionResized.connect(self.resizing_column) _table.cellClicked.connect( - lambda row=0, column=0, tab_index=_key_tab_name: self.table_row_clicked(row, column, tab_index) + lambda row=0, column=0, tab_index=_key_tab_name: self.table_row_clicked( + row, column, tab_index + ) ) _table.itemSelectionChanged.connect( - lambda key_tab_name=_key_tab_name: self.cell_clicked(key_tab_name=_key_tab_name) + lambda key_tab_name=_key_tab_name: self.cell_clicked( + key_tab_name=_key_tab_name + ) ) _data_dict = self.parent.markers_table[_key_tab_name]["data"] @@ -251,7 +259,9 @@ def copy_cell(self, row_selected=-1, column_selected=-1): cell_value = str(table_ui.item(row_selected, column_selected).text()) self.parent.marker_table_buffer_cell = cell_value - def paste_cell(self, top_row_selected=-1, bottom_row_selected=-1, column_selected=-1): + def paste_cell( + self, top_row_selected=-1, bottom_row_selected=-1, column_selected=-1 + ): cell_contain_to_copy = self.parent.marker_table_buffer_cell table_ui = self.get_current_table_ui() markers_table = self.parent.markers_table @@ -262,7 +272,9 @@ def paste_cell(self, top_row_selected=-1, bottom_row_selected=-1, column_selecte marker_axis = "y" for _row in np.arange(top_row_selected, bottom_row_selected + 1): _file = str(table_ui.item(_row, 0).text()) - markers_table[marker_name]["data"][_file][marker_axis] = cell_contain_to_copy + markers_table[marker_name]["data"][_file][marker_axis] = ( + cell_contain_to_copy + ) table_ui.item(_row, column_selected).setText(str(cell_contain_to_copy)) self.parent.markers_table = markers_table @@ -328,7 +340,9 @@ def table_right_click(self, position): menu.addSeparator() self.start_marker = menu.addAction("Set marker interpolation initial position") - self.end_marker = menu.addAction("Set marker interpolation final position and process intermediate markers") + self.end_marker = menu.addAction( + "Set marker interpolation final position and process intermediate markers" + ) if self.parent.markers_initial_position["row"] is None: self.end_marker.setEnabled(False) @@ -338,7 +352,9 @@ def table_right_click(self, position): action = menu.exec_(QtGui.QCursor.pos()) if action == copy_cell: - self.copy_cell(row_selected=top_row_selected, column_selected=left_column_selected) + self.copy_cell( + row_selected=top_row_selected, column_selected=left_column_selected + ) elif action == paste_cell: self.paste_cell( @@ -359,7 +375,9 @@ def start_marker_initialized(self): self.parent.markers_initial_position["row"] = row_selected self.parent.markers_initial_position["tab_name"] = tab_selected o_table = TableHandler(table_ui=self.parent.markers_table[tab_selected]["ui"]) - o_table.set_item_with_str(row=row_selected, column=3, cell_str="Interpolation starting position") + o_table.set_item_with_str( + row=row_selected, column=3, cell_str="Interpolation starting position" + ) def end_marker_initialized(self): tab_selected = self.get_current_active_tab() @@ -384,10 +402,14 @@ def end_marker_initialized(self): coeff = 1 for _row in np.arange(from_row + 1, to_row): xoffset_value = int(np.round(xoffset_from + coeff * delta_xoffset)) - o_table.set_item_with_str(row=_row, column=1, cell_str=str(xoffset_value)) + o_table.set_item_with_str( + row=_row, column=1, cell_str=str(xoffset_value) + ) yoffset_value = int(np.round(yoffset_from + coeff * delta_yoffset)) - o_table.set_item_with_str(row=_row, column=2, cell_str=str(yoffset_value)) + o_table.set_item_with_str( + row=_row, column=2, cell_str=str(yoffset_value) + ) coeff += 1 self.parent.markers_initial_position["row"] = None @@ -443,7 +465,12 @@ def add_marker_button_clicked(self): x = self.parent.o_MarkerDefaultSettings.x y = self.parent.o_MarkerDefaultSettings.y self.__populate_table_row(table, _row, _short_file, x, y) - _data_dict[_short_file] = {"x": x, "y": y, "marker_ui": None, "label_ui": None} + _data_dict[_short_file] = { + "x": x, + "y": y, + "marker_ui": None, + "label_ui": None, + } _marker_dict["data"] = _data_dict @@ -452,7 +479,9 @@ def add_marker_button_clicked(self): self.ui.tabWidget.setCurrentIndex(number_of_tabs - 1) table.itemChanged.connect(self.table_cell_modified) table.itemSelectionChanged.connect( - lambda key_tab_name=new_marker_name: self.cell_clicked(key_tab_name=new_marker_name) + lambda key_tab_name=new_marker_name: self.cell_clicked( + key_tab_name=new_marker_name + ) ) self.parent.markers_table[new_marker_name] = _marker_dict @@ -520,8 +549,12 @@ def run_registration_button_clicked(self): for _marker in markers_table.keys(): _list_files = markers_table[_marker]["data"] for _file in _list_files: - markers_list[_file]["x"].append(markers_table[_marker]["data"][_file]["x"]) - markers_list[_file]["y"].append(markers_table[_marker]["data"][_file]["y"]) + markers_list[_file]["x"].append( + markers_table[_marker]["data"][_file]["x"] + ) + markers_list[_file]["y"].append( + markers_table[_marker]["data"][_file]["y"] + ) step += 1 self.parent.eventProgress.setValue(step) diff --git a/notebooks/__code/registration/registration_profile.py b/notebooks/__code/registration/registration_profile.py index 45ad5ed71..62bf10917 100755 --- a/notebooks/__code/registration/registration_profile.py +++ b/notebooks/__code/registration/registration_profile.py @@ -28,7 +28,9 @@ from __code._utilities.color import Color from __code._utilities.file import make_or_reset_folder from __code.registration import interact_me_style, normal_style -from __code.registration.calculate_profiles_difference import CalculateProfilesDifference +from __code.registration.calculate_profiles_difference import ( + CalculateProfilesDifference, +) class RegistrationProfileLauncher: @@ -370,7 +372,9 @@ def init_reference_image(self): self.reference_image_short_name = self.parent.reference_image_short_name else: self.reference_image = self.data_dict["data"][self.reference_image_index] - self.reference_image_short_name = os.path.basename(self.data_dict["file_name"][self.reference_image_index]) + self.reference_image_short_name = os.path.basename( + self.data_dict["file_name"][self.reference_image_index] + ) def init_table(self): data_dict = self.data_dict @@ -554,7 +558,9 @@ def replot_profile_lines(self, is_horizontal=False): def update_selected_file_profile_plots(self, is_horizontal=True): index_selected = self._get_selected_row() - self.update_single_profile(file_selected=index_selected, is_horizontal=is_horizontal) + self.update_single_profile( + file_selected=index_selected, is_horizontal=is_horizontal + ) def update_single_profile(self, file_selected=-1, is_horizontal=True): if is_horizontal: @@ -568,7 +574,9 @@ def update_single_profile(self, file_selected=-1, is_horizontal=True): profile_2d_ui.clear() # always display the reference image - [xaxis, ref_profile] = self.get_profile(image_index=self.reference_image_index, is_horizontal=is_horizontal) + [xaxis, ref_profile] = self.get_profile( + image_index=self.reference_image_index, is_horizontal=is_horizontal + ) try: profile_2d_ui.plot(xaxis, ref_profile, pen=self.roi[label]["color-peak"]) @@ -576,10 +584,16 @@ def update_single_profile(self, file_selected=-1, is_horizontal=True): pass if file_selected != self.reference_image_index: - [xaxis, selected_profile] = self.get_profile(image_index=file_selected, is_horizontal=is_horizontal) + [xaxis, selected_profile] = self.get_profile( + image_index=file_selected, is_horizontal=is_horizontal + ) try: - profile_2d_ui.plot(xaxis, selected_profile, pen=self.list_rgb_profile_color[file_selected]) + profile_2d_ui.plot( + xaxis, + selected_profile, + pen=self.list_rgb_profile_color[file_selected], + ) except Exception: pass @@ -628,10 +642,14 @@ def calculate_all_profiles(self): def calculate_profile(self, file_index=-1, is_horizontal=True): if is_horizontal: - [xaxis, profile] = self.get_profile(image_index=file_index, is_horizontal=True) + [xaxis, profile] = self.get_profile( + image_index=file_index, is_horizontal=True + ) label = "horizontal" else: - [xaxis, profile] = self.get_profile(image_index=file_index, is_horizontal=False) + [xaxis, profile] = self.get_profile( + image_index=file_index, is_horizontal=False + ) label = "vertical" _profile = {} @@ -744,7 +762,9 @@ def register_images(self): self.eventProgress.setVisible(False) QApplication.processEvents() - def calculate_and_display_current_peak(self, force_recalculation=True, is_horizontal=True): + def calculate_and_display_current_peak( + self, force_recalculation=True, is_horizontal=True + ): if is_horizontal: label = "horizontal" else: @@ -765,8 +785,12 @@ def calculate_and_display_current_peak(self, force_recalculation=True, is_horizo self.display_current_peak(is_horizontal=is_horizontal) if force_recalculation: - self.calculate_profile(file_index=index_selected, is_horizontal=is_horizontal) - self.recalculate_current_peak(file_index=index_selected, is_horizontal=is_horizontal) + self.calculate_profile( + file_index=index_selected, is_horizontal=is_horizontal + ) + self.recalculate_current_peak( + file_index=index_selected, is_horizontal=is_horizontal + ) self.display_current_peak(is_horizontal=is_horizontal) @@ -802,8 +826,12 @@ def display_current_peak(self, is_horizontal=True): self.verti_infinite_line_ui = infinite_line_ui def calculate_and_display_hori_and_verti_peaks(self, force_recalculation=True): - self.calculate_and_display_current_peak(force_recalculation=force_recalculation, is_horizontal=True) - self.calculate_and_display_current_peak(force_recalculation=force_recalculation, is_horizontal=False) + self.calculate_and_display_current_peak( + force_recalculation=force_recalculation, is_horizontal=True + ) + self.calculate_and_display_current_peak( + force_recalculation=force_recalculation, is_horizontal=False + ) def copy_register_parameters_to_main_table(self): nbr_row = self.ui.tableWidget.rowCount() @@ -825,7 +853,9 @@ def full_reset(self): def vertical_roi_moved(self): """when the vertical roi is moved, we need to make sure the width stays within the max we defined and we need refresh the peak calculation""" - region = self.vertical_profile.getArraySlice(self.live_image, self.ui.image_view.imageItem) + region = self.vertical_profile.getArraySlice( + self.live_image, self.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop @@ -846,7 +876,9 @@ def vertical_roi_moved(self): def horizontal_roi_moved(self): """when the horizontal roi is moved, we need to make sure the height stays within the max we defined and we need to refresh the peak calculation""" - region = self.horizontal_profile.getArraySlice(self.live_image, self.ui.image_view.imageItem) + region = self.horizontal_profile.getArraySlice( + self.live_image, self.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop @@ -891,7 +923,9 @@ def calculate_profiles_differences(self): def help_button_clicked(self): import webbrowser - webbrowser.open("https://neutronimaging.ornl.gov/tutorials/imaging-notebooks/registration/") + webbrowser.open( + "https://neutronimaging.ornl.gov/tutorials/imaging-notebooks/registration/" + ) def slider_file_changed(self, value): self._select_table_row(value) @@ -939,10 +973,17 @@ def export_button_clicked(self): """save registered images back to the main UI""" # self.registered_all_images_button_clicked() _export_folder = QFileDialog.getExistingDirectory( - self, directory=self.working_dir, caption="Select Output Folder", options=QFileDialog.ShowDirsOnly + self, + directory=self.working_dir, + caption="Select Output Folder", + options=QFileDialog.ShowDirsOnly, ) if _export_folder: - o_export = ExportRegistration(parent=self, input_working_dir=self.working_dir, export_folder=_export_folder) + o_export = ExportRegistration( + parent=self, + input_working_dir=self.working_dir, + export_folder=_export_folder, + ) o_export.run() QApplication.processEvents() @@ -1058,7 +1099,9 @@ def run(self): self.parent.eventProgress.setValue(0) self.parent.eventProgress.setVisible(True) - export_folder = os.path.join(self.export_folder, self.input_dir_name + "_registered") + export_folder = os.path.join( + self.export_folder, self.input_dir_name + "_registered" + ) make_or_reset_folder(export_folder) for _row, _data in enumerate(data_dict["data"]): diff --git a/notebooks/__code/rename_files.py b/notebooks/__code/rename_files.py index 4bb93a70c..4016b4a00 100755 --- a/notebooks/__code/rename_files.py +++ b/notebooks/__code/rename_files.py @@ -55,8 +55,12 @@ def __init__(self, o_format=None): self.working_dir = o_format.working_dir if self.list_files: - _random_input_list = utilities.get_n_random_element(input_list=self.list_files, n=10) - self.random_input_list = [os.path.basename(_file) for _file in _random_input_list] + _random_input_list = utilities.get_n_random_element( + input_list=self.list_files, n=10 + ) + self.random_input_list = [ + os.path.basename(_file) for _file in _random_input_list + ] self.basename = os.path.basename(self.list_files[0]) @@ -127,16 +131,22 @@ def show(self): # current schema name self.box2 = widgets.HBox( [ - widgets.Label("Pre. Index Separator", layout=widgets.Layout(width="15%")), + widgets.Label( + "Pre. Index Separator", layout=widgets.Layout(width="15%") + ), widgets.Text(value="_", layout=widgets.Layout(width="5%")), ] ) self.box2b = widgets.HBox( [ - widgets.Label("Untouched filename part:", layout=widgets.Layout(width="20%")), + widgets.Label( + "Untouched filename part:", layout=widgets.Layout(width="20%") + ), widgets.Label("", layout=widgets.Layout(width="40%")), - widgets.IntRangeSlider(value=[0, 2], min=0, max=len(self.basename), step=1), + widgets.IntRangeSlider( + value=[0, 2], min=0, max=len(self.basename), step=1 + ), ] ) self.int_range_slider = self.box2b.children[2] @@ -145,11 +155,17 @@ def show(self): self.box4 = widgets.HBox( [ - widgets.Label("Current Name Schema: ", layout=widgets.Layout(width="20%")), - widgets.Label(self.current_naming_schema(), layout=widgets.Layout(width="30%")), + widgets.Label( + "Current Name Schema: ", layout=widgets.Layout(width="20%") + ), + widgets.Label( + self.current_naming_schema(), layout=widgets.Layout(width="30%") + ), widgets.Label("Random Input:", layout=widgets.Layout(width="15%")), widgets.Dropdown( - options=self.random_input_list, value=self.random_input_list[0], layout=widgets.Layout(width="50%") + options=self.random_input_list, + value=self.random_input_list[0], + layout=widgets.Layout(width="50%"), ), ] ) @@ -157,36 +173,54 @@ def show(self): self.box2.children[1].on_trait_change(self.pre_index_text_changed, "value") before = widgets.VBox([self.box2, self.box2b, self.box4]) self.random_input_checkbox = self.box4.children[3] - self.random_input_checkbox.observe(self.random_input_checkbox_value_changed, "value") + self.random_input_checkbox.observe( + self.random_input_checkbox_value_changed, "value" + ) # new naming schema box_text_width = "10%" self.box1 = widgets.HBox( [ - widgets.Label("New prefix File Name", layout=widgets.Layout(width="10%")), + widgets.Label( + "New prefix File Name", layout=widgets.Layout(width="10%") + ), widgets.Checkbox( - value=True, description="Use previous prefix name", layout=widgets.Layout(width="30%") + value=True, + description="Use previous prefix name", + layout=widgets.Layout(width="30%"), ), ] ) self.use_previous_prefix_widget = self.box1.children[1] - self.box1.children[1].observe(self.changed_use_previous_prefix_name, names="value") + self.box1.children[1].observe( + self.changed_use_previous_prefix_name, names="value" + ) self.box1b = widgets.HBox( [ widgets.Label("", layout=widgets.Layout(width="10%")), - widgets.Checkbox(value=False, description="Use new prefix", layout=widgets.Layout(width="20%")), - widgets.Text(value="image", disabled=True, layout=widgets.Layout(width="25%")), + widgets.Checkbox( + value=False, + description="Use new prefix", + layout=widgets.Layout(width="20%"), + ), + widgets.Text( + value="image", disabled=True, layout=widgets.Layout(width="25%") + ), ] ) self.box1b.children[2].observe(self.changed_use_new_prefix_name, names="value") self.new_prefix_text_widget = self.box1b.children[2] self.user_new_prefix_widget = self.box1b.children[1] - self.user_new_prefix_widget.observe(self.changed_use_new_prefix_name, names="value") + self.user_new_prefix_widget.observe( + self.changed_use_new_prefix_name, names="value" + ) self.box5 = widgets.HBox( [ - widgets.Label("New Index Separator", layout=widgets.Layout(width="15%")), + widgets.Label( + "New Index Separator", layout=widgets.Layout(width="15%") + ), widgets.Text(value="_", layout=widgets.Layout(width=box_text_width)), ] ) @@ -208,7 +242,9 @@ def show(self): self.box6 = widgets.HBox( [ widgets.Label("New Name Schema: ", layout=widgets.Layout(width="20%")), - widgets.Label(self.new_naming_schema(), layout=widgets.Layout(width="40%")), + widgets.Label( + self.new_naming_schema(), layout=widgets.Layout(width="40%") + ), ] ) @@ -217,13 +253,17 @@ def show(self): self.box7.children[1].on_trait_change(self.post_text_changed, "value") self.box8.children[1].on_trait_change(self.post_text_changed, "value") - after = widgets.VBox([self.box1, self.box1b, self.box5, self.box7, self.box8, self.box6]) + after = widgets.VBox( + [self.box1, self.box1b, self.box5, self.box7, self.box8, self.box6] + ) accordion = widgets.Accordion(children=[before, after]) accordion.set_title(0, "Current Schema Name") accordion.set_title(1, "New Naming Schema") - output_ui_1 = widgets.HBox([widgets.Label("Example of naming: ", layout=widgets.Layout(width="20%"))]) + output_ui_1 = widgets.HBox( + [widgets.Label("Example of naming: ", layout=widgets.Layout(width="20%"))] + ) self.output_ui_2 = widgets.HBox( [ @@ -240,7 +280,9 @@ def show(self): ) self.output_ui_3.children[1].add_class("result_label") - vbox = widgets.VBox([accordion, output_ui_1, self.output_ui_2, self.output_ui_3]) + vbox = widgets.VBox( + [accordion, output_ui_1, self.output_ui_2, self.output_ui_3] + ) display(vbox) self.demo_output_file_name() @@ -248,7 +290,9 @@ def show(self): self.changed_use_new_prefix_name() def demo_output_file_name(self): - input_file = self.get_basename_of_current_dropdown_selected_file(is_with_ext=True) + input_file = self.get_basename_of_current_dropdown_selected_file( + is_with_ext=True + ) self.output_ui_2.children[1].value = input_file old_index_separator = self.get_old_index_separator() @@ -340,7 +384,12 @@ def generate_new_file_name( try: _index = int(_name_separated[-1]) + offset - new_name = prefix + new_index_separator + "{:0{}}".format(_index, new_number_of_digits) + self.ext + new_name = ( + prefix + + new_index_separator + + "{:0{}}".format(_index, new_number_of_digits) + + self.ext + ) except ValueError: _index = _name_separated[-1] new_name = prefix + new_index_separator + _index + self.ext @@ -361,7 +410,9 @@ def get_dict_old_new_filenames(self): new_number_of_digits = self.get_new_number_of_digits() offset = self.box8.children[1].value - list_of_input_basename_files = [os.path.basename(_file) for _file in list_of_input_files] + list_of_input_basename_files = [ + os.path.basename(_file) for _file in list_of_input_files + ] new_list = {} for _file_index, _file in enumerate(list_of_input_basename_files): @@ -392,7 +443,11 @@ def select_export_folder(self): type="directory", ) else: - display(HTML('You need to fix the namig convention first!')) + display( + HTML( + 'You need to fix the namig convention first!' + ) + ) def export(self, selected): self.output_folder_ui.shortcut_buttons.close() @@ -401,7 +456,9 @@ def export(self, selected): new_output_folder = os.path.abspath(selected) utilities.copy_files( - dict_old_new_names=dict_old_new_names, new_output_folder=new_output_folder, overwrite=False + dict_old_new_names=dict_old_new_names, + new_output_folder=new_output_folder, + overwrite=False, ) self.new_list_files = dict_old_new_names @@ -420,7 +477,9 @@ def display_renaming_result(self, selected): [ widgets.Label("Renmaing results: ", layout=widgets.Layout(width="20%")), widgets.Dropdown( - options=self.renaming_result, value=self.renaming_result[0], layout=widgets.Layout(width="80%") + options=self.renaming_result, + value=self.renaming_result[0], + layout=widgets.Layout(width="80%"), ), ] ) diff --git a/notebooks/__code/rename_files/rename_files.py b/notebooks/__code/rename_files/rename_files.py index 6dd23a9a4..2654779ec 100755 --- a/notebooks/__code/rename_files/rename_files.py +++ b/notebooks/__code/rename_files/rename_files.py @@ -15,7 +15,9 @@ def __init__(self, working_dir=""): def select_input_files(self): self.input_files_ui = fileselector.FileSelectorPanel( - instruction="Select List of Files", start_dir=self.working_dir, multiple=True + instruction="Select List of Files", + start_dir=self.working_dir, + multiple=True, ) self.input_files_ui.show() @@ -80,8 +82,12 @@ def __init__(self, o_format=None): raise ValueError("FormatFileNameIndex object is missing!") if self.list_files: - _random_input_list = utilities.get_n_random_element(input_list=self.list_files, n=10) - self.random_input_list = [os.path.basename(_file) for _file in _random_input_list] + _random_input_list = utilities.get_n_random_element( + input_list=self.list_files, n=10 + ) + self.random_input_list = [ + os.path.basename(_file) for _file in _random_input_list + ] self.basename = os.path.basename(self.list_files[0]) @@ -172,16 +178,22 @@ def show(self): # current schema name self.box2 = widgets.HBox( [ - widgets.Label("Pre. Index Separator", layout=widgets.Layout(width="15%")), + widgets.Label( + "Pre. Index Separator", layout=widgets.Layout(width="15%") + ), widgets.Text(value="_", layout=widgets.Layout(width="5%")), ] ) self.box2b = widgets.HBox( [ - widgets.Label("Untouched filename part:", layout=widgets.Layout(width="20%")), + widgets.Label( + "Untouched filename part:", layout=widgets.Layout(width="20%") + ), widgets.Label("", layout=widgets.Layout(width="40%")), - widgets.IntRangeSlider(value=[0, 2], min=0, max=len(self.basename), step=1), + widgets.IntRangeSlider( + value=[0, 2], min=0, max=len(self.basename), step=1 + ), ] ) self.int_range_slider = self.box2b.children[2] @@ -190,11 +202,17 @@ def show(self): self.box4 = widgets.HBox( [ - widgets.Label("Current Name Schema: ", layout=widgets.Layout(width="20%")), - widgets.Label(self.current_naming_schema(), layout=widgets.Layout(width="30%")), + widgets.Label( + "Current Name Schema: ", layout=widgets.Layout(width="20%") + ), + widgets.Label( + self.current_naming_schema(), layout=widgets.Layout(width="30%") + ), widgets.Label("Random Input:", layout=widgets.Layout(width="15%")), widgets.Dropdown( - options=self.random_input_list, value=self.random_input_list[0], layout=widgets.Layout(width="50%") + options=self.random_input_list, + value=self.random_input_list[0], + layout=widgets.Layout(width="50%"), ), ] ) @@ -202,57 +220,93 @@ def show(self): self.box2.children[1].on_trait_change(self.pre_index_text_changed, "value") before = widgets.VBox([self.box2, self.box2b, self.box4]) self.random_input_checkbox = self.box4.children[3] - self.random_input_checkbox.observe(self.random_input_checkbox_value_changed, "value") + self.random_input_checkbox.observe( + self.random_input_checkbox_value_changed, "value" + ) # new naming schema box_text_width = "10%" self.box1 = widgets.HBox( [ - widgets.Label("New prefix File Name", layout=widgets.Layout(width="20%")), + widgets.Label( + "New prefix File Name", layout=widgets.Layout(width="20%") + ), widgets.Checkbox( - value=True, description="Use previous prefix name", layout=widgets.Layout(width="30%") + value=True, + description="Use previous prefix name", + layout=widgets.Layout(width="30%"), ), ] ) self.use_previous_prefix_widget = self.box1.children[1] - self.box1.children[1].observe(self.changed_use_previous_prefix_name, names="value") + self.box1.children[1].observe( + self.changed_use_previous_prefix_name, names="value" + ) self.box1b = widgets.HBox( [ widgets.Label("", layout=widgets.Layout(width="20%")), - widgets.Checkbox(value=False, description="Use new prefix", layout=widgets.Layout(width="20%")), - widgets.Text(value="image", disabled=True, layout=widgets.Layout(width="25%")), + widgets.Checkbox( + value=False, + description="Use new prefix", + layout=widgets.Layout(width="20%"), + ), + widgets.Text( + value="image", disabled=True, layout=widgets.Layout(width="25%") + ), ] ) self.box1b.children[2].observe(self.changed_use_new_prefix_name, names="value") self.new_prefix_text_widget = self.box1b.children[2] self.user_new_prefix_widget = self.box1b.children[1] - self.user_new_prefix_widget.observe(self.changed_use_new_prefix_name, names="value") + self.user_new_prefix_widget.observe( + self.changed_use_new_prefix_name, names="value" + ) self.suffix_box1 = widgets.HBox( [ - widgets.Label("New suffix File Name", layout=widgets.Layout(width="20%")), - widgets.Checkbox(value=True, description="Use digit suffix", layout=widgets.Layout(width="30%")), + widgets.Label( + "New suffix File Name", layout=widgets.Layout(width="20%") + ), + widgets.Checkbox( + value=True, + description="Use digit suffix", + layout=widgets.Layout(width="30%"), + ), ] ) self.use_digit_suffix_widget = self.suffix_box1.children[1] - self.suffix_box1.children[1].observe(self.changed_use_digit_suffix_name, names="value") + self.suffix_box1.children[1].observe( + self.changed_use_digit_suffix_name, names="value" + ) self.suffix_box2 = widgets.HBox( [ widgets.Label("", layout=widgets.Layout(width="20%")), - widgets.Checkbox(value=False, description="Use new suffix", layout=widgets.Layout(width="20%")), - widgets.Text(value="", disabled=True, layout=widgets.Layout(width="25%")), + widgets.Checkbox( + value=False, + description="Use new suffix", + layout=widgets.Layout(width="20%"), + ), + widgets.Text( + value="", disabled=True, layout=widgets.Layout(width="25%") + ), ] ) - self.suffix_box2.children[2].observe(self.changed_use_new_suffix_name, names="value") + self.suffix_box2.children[2].observe( + self.changed_use_new_suffix_name, names="value" + ) self.user_new_suffix_widget = self.suffix_box2.children[1] self.new_suffix_text_widget = self.suffix_box2.children[2] - self.user_new_suffix_widget.observe(self.changed_use_new_suffix_name, names="value") + self.user_new_suffix_widget.observe( + self.changed_use_new_suffix_name, names="value" + ) self.box5 = widgets.HBox( [ - widgets.Label("New Index Separator", layout=widgets.Layout(width="15%")), + widgets.Label( + "New Index Separator", layout=widgets.Layout(width="15%") + ), widgets.Text(value="_", layout=widgets.Layout(width=box_text_width)), ] ) @@ -275,14 +329,18 @@ def show(self): self.box9 = widgets.HBox( [ widgets.Label("Extension", layout=widgets.Layout(width="15%")), - widgets.Text(value=current_ext, layout=widgets.Layout(width=box_text_width)), + widgets.Text( + value=current_ext, layout=widgets.Layout(width=box_text_width) + ), ] ) self.box6 = widgets.HBox( [ widgets.Label("New Name Schema: ", layout=widgets.Layout(width="20%")), - widgets.Label(self.new_naming_schema(), layout=widgets.Layout(width="40%")), + widgets.Label( + self.new_naming_schema(), layout=widgets.Layout(width="40%") + ), ] ) @@ -310,7 +368,9 @@ def show(self): accordion.set_title(0, "Current Schema Name") accordion.set_title(1, "New Naming Schema") - output_ui_1 = widgets.HBox([widgets.Label("Example of naming: ", layout=widgets.Layout(width="20%"))]) + output_ui_1 = widgets.HBox( + [widgets.Label("Example of naming: ", layout=widgets.Layout(width="20%"))] + ) self.output_ui_2 = widgets.HBox( [ @@ -327,7 +387,9 @@ def show(self): ) self.output_ui_3.children[1].add_class("result_label") - vbox = widgets.VBox([accordion, output_ui_1, self.output_ui_2, self.output_ui_3]) + vbox = widgets.VBox( + [accordion, output_ui_1, self.output_ui_2, self.output_ui_3] + ) display(vbox) self.demo_output_file_name() @@ -335,7 +397,9 @@ def show(self): self.changed_use_new_prefix_name() def demo_output_file_name(self): - input_file = self.get_basename_of_current_dropdown_selected_file(is_with_ext=False) + input_file = self.get_basename_of_current_dropdown_selected_file( + is_with_ext=False + ) self.output_ui_2.children[1].value = input_file new_name = self.box6.children[1].value @@ -452,7 +516,12 @@ def generate_new_file_name( if suffix_flag: new_name = prefix + new_index_separator + suffix + ext else: - new_name = prefix + new_index_separator + "{:0{}}".format(file_index, new_number_of_digits) + ext + new_name = ( + prefix + + new_index_separator + + "{:0{}}".format(file_index, new_number_of_digits) + + ext + ) # except ValueError: # # print(f"Could not parse index from {_name_separated[-1]}") @@ -484,7 +553,9 @@ def get_dict_old_new_filenames(self): else: suffix = "" - list_of_input_basename_files = [os.path.basename(_file) for _file in list_of_input_files] + list_of_input_basename_files = [ + os.path.basename(_file) for _file in list_of_input_files + ] new_list = {} for _file_index, _file in enumerate(list_of_input_basename_files): @@ -511,8 +582,14 @@ def get_dict_old_new_filenames(self): def check_new_names(self): dict_old_new_names = self.get_dict_old_new_filenames() - old_names_new_names = [f"{os.path.basename(_key)} -> {_value}" for _key, _value in dict_old_new_names.items()] - select_widget = widgets.Select(options=old_names_new_names, layout=widgets.Layout(width="100%", height="400px")) + old_names_new_names = [ + f"{os.path.basename(_key)} -> {_value}" + for _key, _value in dict_old_new_names.items() + ] + select_widget = widgets.Select( + options=old_names_new_names, + layout=widgets.Layout(width="100%", height="400px"), + ) display(select_widget) def select_export_folder(self): @@ -527,7 +604,11 @@ def select_export_folder(self): type="directory", ) else: - display(HTML('You need to fix the namig convention first!')) + display( + HTML( + 'You need to fix the namig convention first!' + ) + ) def export(self, selected): input_folder = os.path.abspath(self.input_folder) @@ -535,10 +616,14 @@ def export(self, selected): self.output_folder_ui.shortcut_buttons.close() dict_old_new_names = self.get_dict_old_new_filenames() - new_output_folder = os.path.join(os.path.abspath(selected), input_folder_renamed) + new_output_folder = os.path.join( + os.path.abspath(selected), input_folder_renamed + ) utilities.copy_files( - dict_old_new_names=dict_old_new_names, new_output_folder=new_output_folder, overwrite=False + dict_old_new_names=dict_old_new_names, + new_output_folder=new_output_folder, + overwrite=False, ) self.new_list_files = dict_old_new_names diff --git a/notebooks/__code/resonance_fitting/__init__.py b/notebooks/__code/resonance_fitting/__init__.py index 6accc17d3..a60c77f11 100644 --- a/notebooks/__code/resonance_fitting/__init__.py +++ b/notebooks/__code/resonance_fitting/__init__.py @@ -44,11 +44,12 @@ class DetectorType: }, } -VENUS_RES_FUNC=Path("/SNS/VENUS/shared/instrument/resonance/_fts_bl10_0p5meV_1keV_25pts.txt") -SAMMY_EXE_PATH=Path("/SNS/software/sammy/bin/sammy") +VENUS_RES_FUNC = Path( + "/SNS/VENUS/shared/instrument/resonance/_fts_bl10_0p5meV_1keV_25pts.txt" +) +SAMMY_EXE_PATH = Path("/SNS/software/sammy/bin/sammy") -class Parent: +class Parent: def __init__(self, parent=None): self.parent = parent - \ No newline at end of file diff --git a/notebooks/__code/resonance_fitting/get.py b/notebooks/__code/resonance_fitting/get.py index 79e325bb3..bc301e6f9 100644 --- a/notebooks/__code/resonance_fitting/get.py +++ b/notebooks/__code/resonance_fitting/get.py @@ -2,19 +2,18 @@ class Get(Parent): - def full_name_of_element_from_abreviation(self, abbreviation: str) -> str: """Get the full name of an element from its abbreviation. Args: - abbreviation (str): The abbreviation of the element (e.g., 'H' for Hydrogen). - + abbreviation (str): The abbreviation of the element (e.g., 'H' for Hydrogen). + Returns: str: The full name of the element. """ dict_elements = self.parent.dict_elements for _element_name in dict_elements.keys(): - _abbreviation = dict_elements[_element_name]['symbol'] + _abbreviation = dict_elements[_element_name]["symbol"] if _abbreviation == abbreviation: return _element_name raise ValueError(f"Element with abbreviation '{abbreviation}' not found.") diff --git a/notebooks/__code/resonance_fitting/normalization_for_timepix.py b/notebooks/__code/resonance_fitting/normalization_for_timepix.py index 245f8c22a..c4d9b694c 100644 --- a/notebooks/__code/resonance_fitting/normalization_for_timepix.py +++ b/notebooks/__code/resonance_fitting/normalization_for_timepix.py @@ -73,7 +73,9 @@ def _worker(fl): return (imread(fl).astype(LOAD_DTYPE)).swapaxes(0, 1) -def load_data_using_multithreading(list_tif: list = None, combine_tof: bool = False) -> np.ndarray: +def load_data_using_multithreading( + list_tif: list = None, combine_tof: bool = False +) -> np.ndarray: """load data using multithreading""" with mp.Pool(processes=40) as pool: data = pool.map(_worker, list_tif) @@ -92,7 +94,9 @@ def retrieve_list_of_tif(folder: str) -> list: def create_x_axis_file( - lambda_array: np.ndarray = None, energy_array: np.ndarray = None, output_folder: str = "./" + lambda_array: np.ndarray = None, + energy_array: np.ndarray = None, + output_folder: str = "./", ) -> str: """create x axis file with lambda, energy and tof arrays""" x_axis_data = { @@ -160,10 +164,16 @@ def normalization_with_list_of_runs( export_corrected_stack_of_sample_data = export_mode.get("sample_stack", False) export_corrected_stack_of_ob_data = export_mode.get("ob_stack", False) - export_corrected_stack_of_normalized_data = export_mode.get("normalized_stack", False) - export_corrected_integrated_sample_data = export_mode.get("sample_integrated", False) + export_corrected_stack_of_normalized_data = export_mode.get( + "normalized_stack", False + ) + export_corrected_integrated_sample_data = export_mode.get( + "sample_integrated", False + ) export_corrected_integrated_ob_data = export_mode.get("ob_integrated", False) - export_corrected_integrated_normalized_data = export_mode.get("normalized_integrated", False) + export_corrected_integrated_normalized_data = export_mode.get( + "normalized_integrated", False + ) export_x_axis = export_mode.get("x_axis", True) logging.info(f"{export_corrected_stack_of_sample_data = }") @@ -181,13 +191,18 @@ def normalization_with_list_of_runs( nexus_root_path=nexus_path, ) ob_master_dict, ob_status_metadata = create_master_dict( - list_run_numbers=ob_run_numbers, data_type=DataType.ob, instrument=instrument, nexus_root_path=nexus_path + list_run_numbers=ob_run_numbers, + data_type=DataType.ob, + instrument=instrument, + nexus_root_path=nexus_path, ) # only for SNAP if instrument == "SNAP": for _run in sample_master_dict.keys(): - sample_master_dict[_run][MasterDictKeys.detector_delay_us] = detector_delay_us + sample_master_dict[_run][MasterDictKeys.detector_delay_us] = ( + detector_delay_us + ) for _run in ob_master_dict.keys(): ob_master_dict[_run][MasterDictKeys.detector_delay_us] = detector_delay_us @@ -197,25 +212,32 @@ def normalization_with_list_of_runs( logging.info(f"loading ob# {_ob_run_number} ... ") if verbose: display(HTML(f"Loading ob# {_ob_run_number} ...")) - ob_master_dict[_ob_run_number][MasterDictKeys.data] = load_data_using_multithreading( - ob_master_dict[_ob_run_number][MasterDictKeys.list_tif], combine_tof=False + ob_master_dict[_ob_run_number][MasterDictKeys.data] = ( + load_data_using_multithreading( + ob_master_dict[_ob_run_number][MasterDictKeys.list_tif], + combine_tof=False, + ) ) logging.info(f"ob# {_ob_run_number} loaded!") logging.info(f"{ob_master_dict[_ob_run_number][MasterDictKeys.data].shape = }") if verbose: display(HTML(f"ob# {_ob_run_number} loaded!")) - display(HTML(f"{ob_master_dict[_ob_run_number][MasterDictKeys.data].shape = }")) + display( + HTML(f"{ob_master_dict[_ob_run_number][MasterDictKeys.data].shape = }") + ) if proton_charge_flag: normalized_by_proton_charge = ( - sample_status_metadata.all_proton_charge_found and ob_status_metadata.all_proton_charge_found + sample_status_metadata.all_proton_charge_found + and ob_status_metadata.all_proton_charge_found ) else: normalized_by_proton_charge = False if shutter_counts_flag: normalized_by_shutter_counts = ( - sample_status_metadata.all_shutter_counts_found and ob_status_metadata.all_shutter_counts_found + sample_status_metadata.all_shutter_counts_found + and ob_status_metadata.all_shutter_counts_found ) else: normalized_by_shutter_counts = False @@ -248,7 +270,9 @@ def normalization_with_list_of_runs( export_corrected_stack_of_ob_data, export_corrected_integrated_ob_data, ob_data_combined, - spectra_file_name=ob_master_dict[_ob_run_number][MasterDictKeys.spectra_file_name], + spectra_file_name=ob_master_dict[_ob_run_number][ + MasterDictKeys.spectra_file_name + ], ) # load sample images @@ -256,22 +280,33 @@ def normalization_with_list_of_runs( logging.info(f"loading sample# {_sample_run_number} ... ") if verbose: display(HTML(f"Loading sample# {_sample_run_number} ...")) - sample_master_dict[_sample_run_number][MasterDictKeys.data] = load_data_using_multithreading( - sample_master_dict[_sample_run_number][MasterDictKeys.list_tif], combine_tof=False + sample_master_dict[_sample_run_number][MasterDictKeys.data] = ( + load_data_using_multithreading( + sample_master_dict[_sample_run_number][MasterDictKeys.list_tif], + combine_tof=False, + ) ) logging.info(f"sample# {_sample_run_number} loaded!") - logging.info(f"{sample_master_dict[_sample_run_number][MasterDictKeys.data].shape = }") + logging.info( + f"{sample_master_dict[_sample_run_number][MasterDictKeys.data].shape = }" + ) if verbose: display(HTML(f"sample# {_sample_run_number} loaded!")) - display(HTML(f"{sample_master_dict[_sample_run_number][MasterDictKeys.data].shape = }")) + display( + HTML( + f"{sample_master_dict[_sample_run_number][MasterDictKeys.data].shape = }" + ) + ) if correct_chips_alignment_flag: logging.info("Correcting chips alignment ...") if verbose: display(HTML("Correcting chips alignment ...")) for _sample_run_number in sample_master_dict.keys(): - sample_master_dict[_sample_run_number][MasterDictKeys.data] = correct_chips_alignment( - sample_master_dict[_sample_run_number][MasterDictKeys.data] + sample_master_dict[_sample_run_number][MasterDictKeys.data] = ( + correct_chips_alignment( + sample_master_dict[_sample_run_number][MasterDictKeys.data] + ) ) logging.info("Chips alignment corrected!") if verbose: @@ -303,17 +338,25 @@ def normalization_with_list_of_runs( logging.info("**********************************") if normalized_by_proton_charge: - proton_charge = sample_master_dict[_sample_run_number][MasterDictKeys.proton_charge] + proton_charge = sample_master_dict[_sample_run_number][ + MasterDictKeys.proton_charge + ] _sample_data = _sample_data / proton_charge if normalized_by_shutter_counts: list_shutter_values_for_each_image = produce_list_shutter_for_each_image( - list_time_spectra=ob_master_dict[_ob_run_number][MasterDictKeys.list_spectra], - list_shutter_counts=sample_master_dict[_sample_run_number][MasterDictKeys.shutter_counts], + list_time_spectra=ob_master_dict[_ob_run_number][ + MasterDictKeys.list_spectra + ], + list_shutter_counts=sample_master_dict[_sample_run_number][ + MasterDictKeys.shutter_counts + ], ) sample_data = [] - for _sample, _shutter_value in zip(_sample_data, list_shutter_values_for_each_image, strict=False): + for _sample, _shutter_value in zip( + _sample_data, list_shutter_values_for_each_image, strict=False + ): sample_data.append(_sample / _shutter_value) _sample_data = np.array(sample_data) @@ -323,14 +366,19 @@ def normalization_with_list_of_runs( logging.info(f"{ob_data_combined.dtype = }") # export sample data after correction if requested - if export_corrected_stack_of_sample_data or export_corrected_integrated_sample_data: + if ( + export_corrected_stack_of_sample_data + or export_corrected_integrated_sample_data + ): export_sample_images( output_folder, export_corrected_stack_of_sample_data, export_corrected_integrated_sample_data, _sample_run_number, _sample_data, - spectra_file_name=sample_master_dict[_sample_run_number][MasterDictKeys.spectra_file_name], + spectra_file_name=sample_master_dict[_sample_run_number][ + MasterDictKeys.spectra_file_name + ], ) # _sample_data = np.divide(_sample_data, ob_data_combined, out=np.zeros_like(_sample_data), where=ob_data_combined!=0) @@ -350,8 +398,12 @@ def normalization_with_list_of_runs( logging.info(f"{normalized_data[_sample_run_number].shape = }") logging.info(f"{normalized_data[_sample_run_number].dtype = }") - detector_delay_us = sample_master_dict[_sample_run_number][MasterDictKeys.detector_delay_us] - time_spectra = sample_master_dict[_sample_run_number][MasterDictKeys.list_spectra] + detector_delay_us = sample_master_dict[_sample_run_number][ + MasterDictKeys.detector_delay_us + ] + time_spectra = sample_master_dict[_sample_run_number][ + MasterDictKeys.list_spectra + ] lambda_array = convert_array_from_time_to_lambda( time_array=time_spectra, @@ -374,11 +426,15 @@ def normalization_with_list_of_runs( if preview: # display preview of normalized data - fig, axs1 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) + fig, axs1 = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) sample_data_integrated = np.nanmean(_sample_data, axis=0) im0 = axs1[0].imshow(sample_data_integrated, cmap="gray") plt.colorbar(im0, ax=axs1[0]) - axs1[0].set_title(f"Sample data: {_sample_run_number} | detector delay: {detector_delay_us:.2f} us") + axs1[0].set_title( + f"Sample data: {_sample_run_number} | detector delay: {detector_delay_us:.2f} us" + ) sample_integrated1 = np.nansum(_sample_data, axis=1) sample_integrated = np.nansum(sample_integrated1, axis=1) @@ -387,7 +443,9 @@ def normalization_with_list_of_runs( axs1[1].set_ylabel("mean of full image") plt.tight_layout - fig, axs2 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) + fig, axs2 = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) ob_data_integrated = np.nanmean(ob_data_combined, axis=0) im1 = axs2[0].imshow(ob_data_integrated, cmap="gray") plt.colorbar(im1, ax=axs2[0]) @@ -400,8 +458,12 @@ def normalization_with_list_of_runs( axs2[1].set_ylabel("mean of full image") plt.tight_layout() - fig, axs3 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) - normalized_data_integrated = np.nanmean(normalized_data[_sample_run_number], axis=0) + fig, axs3 = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) + normalized_data_integrated = np.nanmean( + normalized_data[_sample_run_number], axis=0 + ) im2 = axs3[0].imshow(normalized_data_integrated, cmap="gray") plt.colorbar(im2, ax=axs3[0]) axs3[0].set_title(f"Normalized data {_sample_run_number}") @@ -413,7 +475,9 @@ def normalization_with_list_of_runs( axs3[1].set_ylabel("mean of full image") plt.tight_layout() - fig, axs4 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) + fig, axs4 = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) axs4[0].plot(lambda_array, profile, "*") axs4[0].set_xlabel("Lambda (A)") axs4[0].set_ylabel("mean of full image") @@ -426,24 +490,36 @@ def normalization_with_list_of_runs( plt.show() - if export_corrected_integrated_normalized_data or export_corrected_stack_of_normalized_data: + if ( + export_corrected_integrated_normalized_data + or export_corrected_stack_of_normalized_data + ): # make up new output folder name list_ob_runs = list(ob_master_dict.keys()) - str_ob_runs = "_".join([str(_ob_run_number) for _ob_run_number in list_ob_runs]) + str_ob_runs = "_".join( + [str(_ob_run_number) for _ob_run_number in list_ob_runs] + ) full_output_folder = os.path.join( - output_folder, f"normalized_sample_{_sample_run_number}_obs_{str_ob_runs}" + output_folder, + f"normalized_sample_{_sample_run_number}_obs_{str_ob_runs}", ) # issue for WEI here ! full_output_folder = os.path.abspath(full_output_folder) os.makedirs(full_output_folder, exist_ok=True) if export_corrected_integrated_normalized_data: # making up the integrated sample data - sample_data_integrated = np.nanmean(normalized_data[_sample_run_number], axis=0) + sample_data_integrated = np.nanmean( + normalized_data[_sample_run_number], axis=0 + ) full_file_name = os.path.join(full_output_folder, "integrated.tif") - logging.info(f"\t -> Exporting integrated normalized data to {full_file_name} ...") + logging.info( + f"\t -> Exporting integrated normalized data to {full_file_name} ..." + ) make_tiff(data=sample_data_integrated, filename=full_file_name) - logging.info(f"\t -> Exporting integrated normalized data to {full_file_name} is done!") + logging.info( + f"\t -> Exporting integrated normalized data to {full_file_name} is done!" + ) if export_corrected_stack_of_normalized_data: output_stack_folder = os.path.join(full_output_folder, "stack") @@ -451,12 +527,20 @@ def normalization_with_list_of_runs( os.makedirs(output_stack_folder, exist_ok=True) for _index, _data in enumerate(normalized_data[_sample_run_number]): - _output_file = os.path.join(output_stack_folder, f"image{_index:04d}.tif") + _output_file = os.path.join( + output_stack_folder, f"image{_index:04d}.tif" + ) make_tiff(data=_data, filename=_output_file) - logging.info(f"\t -> Exporting normalized data to {output_stack_folder} is done!") + logging.info( + f"\t -> Exporting normalized data to {output_stack_folder} is done!" + ) print(f"Exported normalized tif images are in: {output_stack_folder}!") - spectra_file = sample_master_dict[_sample_run_number][MasterDictKeys.spectra_file_name] - logging.info(f"Exported time spectra file {spectra_file} to {output_stack_folder}!") + spectra_file = sample_master_dict[_sample_run_number][ + MasterDictKeys.spectra_file_name + ] + logging.info( + f"Exported time spectra file {spectra_file} to {output_stack_folder}!" + ) shutil.copy(spectra_file, output_stack_folder) # create x-axis file @@ -475,7 +559,9 @@ def get_detector_offset_from_nexus(nexus_path: str) -> float: """get the detector offset from the nexus file""" with h5py.File(nexus_path, "r") as hdf5_data: try: - detector_offset_micros = hdf5_data["entry"]["DASlogs"]["BL10:Det:TH:DSPT1:TIDelay"]["value"][0] + detector_offset_micros = hdf5_data["entry"]["DASlogs"][ + "BL10:Det:TH:DSPT1:TIDelay" + ]["value"][0] except KeyError: detector_offset_micros = None return detector_offset_micros @@ -504,7 +590,9 @@ def export_sample_images( make_tiff(data=_data, filename=_output_file) logging.info(f"\t -> Exporting sample data to {output_stack_folder} is done!") shutil.copy(spectra_file_name, os.path.join(output_stack_folder)) - logging.info(f"\t -> Exporting spectra file {spectra_file_name} to {output_stack_folder} is done!") + logging.info( + f"\t -> Exporting spectra file {spectra_file_name} to {output_stack_folder} is done!" + ) if export_corrected_integrated_sample_data: # making up the integrated sample data @@ -512,7 +600,9 @@ def export_sample_images( full_file_name = os.path.join(sample_output_folder, "integrated.tif") logging.info(f"\t -> Exporting integrated sample data to {full_file_name} ...") make_tiff(data=sample_data_integrated, filename=full_file_name) - logging.info(f"\t -> Exporting integrated sample data to {full_file_name} is done!") + logging.info( + f"\t -> Exporting integrated sample data to {full_file_name} is done!" + ) display(HTML(f"Created folder {output_stack_folder} for sample outputs!")) @@ -529,10 +619,13 @@ def export_ob_images( logging.info(f"> Exporting combined ob images to {output_folder} ...") logging.info(f"\t{ob_run_numbers = }") list_ob_runs_number_only = [ - str(isolate_run_number_from_full_path(_ob_run_number)) for _ob_run_number in ob_run_numbers + str(isolate_run_number_from_full_path(_ob_run_number)) + for _ob_run_number in ob_run_numbers ] if len(list_ob_runs_number_only) == 1: - ob_output_folder = os.path.join(output_folder, f"ob_{list_ob_runs_number_only[0]}") + ob_output_folder = os.path.join( + output_folder, f"ob_{list_ob_runs_number_only[0]}" + ) else: str_list_ob_runs = "_".join(list_ob_runs_number_only) ob_output_folder = os.path.join(output_folder, f"ob_{str_list_ob_runs}") @@ -561,12 +654,16 @@ def export_ob_images( logging.info(f"\t -> Exporting ob data to {output_stack_folder} is done!") # copy spectra file to the output folder shutil.copy(spectra_file_name, os.path.join(output_stack_folder)) - logging.info(f"\t -> Exported spectra file {spectra_file_name} to {output_stack_folder}!") + logging.info( + f"\t -> Exported spectra file {spectra_file_name} to {output_stack_folder}!" + ) display(HTML(f"Created folder {output_stack_folder} for OB outputs!")) -def normalization(sample_folder=None, ob_folder=None, output_folder="./", verbose=False): +def normalization( + sample_folder=None, ob_folder=None, output_folder="./", verbose=False +): pass @@ -646,7 +743,9 @@ def update_dict_with_shutter_counts(master_dict: dict) -> tuple[dict, bool]: if _value == "0": break list_shutter_counts.append(float(_value)) - master_dict[run_number][MasterDictKeys.shutter_counts] = list_shutter_counts + master_dict[run_number][MasterDictKeys.shutter_counts] = ( + list_shutter_counts + ) return master_dict, status_all_shutter_counts_found @@ -677,7 +776,9 @@ def update_dict_with_proton_charge(master_dict: dict) -> tuple[dict, bool]: _nexus_path = master_dict[_run_number][MasterDictKeys.nexus_path] try: with h5py.File(_nexus_path, "r") as hdf5_data: - proton_charge = hdf5_data["entry"][MasterDictKeys.proton_charge][0] / 1e12 + proton_charge = ( + hdf5_data["entry"][MasterDictKeys.proton_charge][0] / 1e12 + ) except KeyError: proton_charge = None status_all_proton_charge_found = False @@ -688,7 +789,9 @@ def update_dict_with_proton_charge(master_dict: dict) -> tuple[dict, bool]: def update_dict_with_list_of_images(master_dict: dict) -> dict: """update the master dict with list of images""" for _run_number in master_dict.keys(): - list_tif = retrieve_list_of_tif(master_dict[_run_number][MasterDictKeys.data_path]) + list_tif = retrieve_list_of_tif( + master_dict[_run_number][MasterDictKeys.data_path] + ) master_dict[_run_number][MasterDictKeys.list_tif] = list_tif @@ -699,7 +802,9 @@ def get_list_run_number(data_folder: str) -> list: return list_run_number -def update_dict_with_nexus_full_path(nexus_root_path: str, instrument: str, master_dict: dict) -> dict: +def update_dict_with_nexus_full_path( + nexus_root_path: str, instrument: str, master_dict: dict +) -> dict: """create dict of nexus path for each run number""" for run_number in master_dict.keys(): master_dict[run_number][MasterDictKeys.nexus_path] = os.path.join( @@ -717,7 +822,9 @@ def update_with_nexus_metadata(master_dict: dict) -> dict: def update_dict_with_data_full_path(data_root_path: str, master_dict: dict) -> dict: """create dict of data path for each run number""" for run_number in master_dict.keys(): - master_dict[run_number][MasterDictKeys.data_path] = os.path.join(data_root_path, f"Run_{run_number}") + master_dict[run_number][MasterDictKeys.data_path] = os.path.join( + data_root_path, f"Run_{run_number}" + ) def create_master_dict( @@ -775,7 +882,9 @@ def create_master_dict( return master_dict, status_metadata -def produce_list_shutter_for_each_image(list_time_spectra: list = None, list_shutter_counts: list = None) -> list: +def produce_list_shutter_for_each_image( + list_time_spectra: list = None, list_shutter_counts: list = None +) -> list: """produce list of shutter counts for each image""" delat_time_spectra = list_time_spectra[1] - list_time_spectra[0] @@ -785,18 +894,26 @@ def produce_list_shutter_for_each_image(list_time_spectra: list = None, list_shu logging.info(f"\t{list_index_jump = }") logging.info(f"\t{list_shutter_counts = }") - list_shutter_values_for_each_image = np.zeros(len(list_time_spectra), dtype=np.float32) + list_shutter_values_for_each_image = np.zeros( + len(list_time_spectra), dtype=np.float32 + ) if len(list_shutter_counts) == 1: # resonance mode list_shutter_values_for_each_image.fill(list_shutter_counts[0]) return list_shutter_values_for_each_image - list_shutter_values_for_each_image[0 : list_index_jump[0] + 1].fill(list_shutter_counts[0]) + list_shutter_values_for_each_image[0 : list_index_jump[0] + 1].fill( + list_shutter_counts[0] + ) for _index in range(1, len(list_index_jump)): _start = list_index_jump[_index - 1] _end = list_index_jump[_index] - list_shutter_values_for_each_image[_start + 1 : _end + 1].fill(list_shutter_counts[_index]) + list_shutter_values_for_each_image[_start + 1 : _end + 1].fill( + list_shutter_counts[_index] + ) - list_shutter_values_for_each_image[list_index_jump[-1] + 1 :] = list_shutter_counts[-1] + list_shutter_values_for_each_image[list_index_jump[-1] + 1 :] = list_shutter_counts[ + -1 + ] return list_shutter_values_for_each_image @@ -817,7 +934,9 @@ def combine_ob_images( for _ob_run_number in ob_master_dict.keys(): logging.info(f"Combining ob# {_ob_run_number} ...") - ob_data = np.array(ob_master_dict[_ob_run_number][MasterDictKeys.data], dtype=np.float32) + ob_data = np.array( + ob_master_dict[_ob_run_number][MasterDictKeys.data], dtype=np.float32 + ) # get statistics of ob data data_shape = ob_data.shape @@ -826,7 +945,9 @@ def combine_ob_images( number_of_zeros = np.sum(ob_data == 0) logging.info(f"\t ob data shape: {data_shape}") logging.info(f"\t Number of zeros in ob data: {number_of_zeros}") - logging.info(f"\t Percentage of zeros in ob data: {number_of_zeros / (data_shape[0] * nbr_pixels) * 100:.2f}%") + logging.info( + f"\t Percentage of zeros in ob data: {number_of_zeros / (data_shape[0] * nbr_pixels) * 100:.2f}%" + ) logging.info(f"\t Mean of ob data: {np.mean(ob_data)}") logging.info(f"\t maximum of ob data: {np.max(ob_data)}") logging.info(f"\t minimum of ob data: {np.min(ob_data)}") @@ -842,14 +963,20 @@ def combine_ob_images( logging.info("\t -> Normalized by shutter counts") list_shutter_values_for_each_image = produce_list_shutter_for_each_image( - list_time_spectra=ob_master_dict[_ob_run_number][MasterDictKeys.list_spectra], - list_shutter_counts=ob_master_dict[_ob_run_number][MasterDictKeys.shutter_counts], + list_time_spectra=ob_master_dict[_ob_run_number][ + MasterDictKeys.list_spectra + ], + list_shutter_counts=ob_master_dict[_ob_run_number][ + MasterDictKeys.shutter_counts + ], ) logging.info(f"{list_shutter_values_for_each_image.shape = }") temp_ob_data = np.empty_like(ob_data, dtype=np.float32) for _index in range(len(list_shutter_values_for_each_image)): - temp_ob_data[_index] = ob_data[_index] / list_shutter_values_for_each_image[_index] + temp_ob_data[_index] = ( + ob_data[_index] / list_shutter_values_for_each_image[_index] + ) logging.info(f"{temp_ob_data.shape = }") ob_data = temp_ob_data.copy() @@ -897,9 +1024,15 @@ def combine_ob_images( formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) - parser.add_argument("--sample", type=str, nargs=1, help="Full path to sample run number") - parser.add_argument("--ob", type=str, nargs=1, help="Full path to the ob run number") - parser.add_argument("--output", type=str, nargs=1, help="Path to the output folder", default="./") + parser.add_argument( + "--sample", type=str, nargs=1, help="Full path to sample run number" + ) + parser.add_argument( + "--ob", type=str, nargs=1, help="Full path to the ob run number" + ) + parser.add_argument( + "--output", type=str, nargs=1, help="Path to the output folder", default="./" + ) args = parser.parse_args() logging.info(f"{args = }") @@ -948,7 +1081,9 @@ def combine_ob_images( # normalization(sample_folder=sample_folder, ob_folder=ob_folder, output_folder=output_folder) - print(f"Normalization is done! Check the log file {log_file_name} for more details!") + print( + f"Normalization is done! Check the log file {log_file_name} for more details!" + ) print(f"Exported data to {output_folder}") # sample = /SNS/VENUS/IPTS-34808/shared/autoreduce/mcp/November17_Sample6_UA_H_Batteries_1_5_Angs_min_30Hz_5C diff --git a/notebooks/__code/resonance_fitting/normalization_for_timepix1_timepix3.py b/notebooks/__code/resonance_fitting/normalization_for_timepix1_timepix3.py index 0e59f51bb..0d80e774a 100644 --- a/notebooks/__code/resonance_fitting/normalization_for_timepix1_timepix3.py +++ b/notebooks/__code/resonance_fitting/normalization_for_timepix1_timepix3.py @@ -14,7 +14,6 @@ from IPython.display import HTML, display from PIL import Image from skimage.io import imread -from scipy.ndimage import median_filter # from enum import Enum # from scipy.constants import h, c, electron_volt, m_n @@ -82,7 +81,9 @@ def _worker(fl): return (imread(fl).astype(LOAD_DTYPE)).swapaxes(0, 1) -def load_data_using_multithreading(list_tif: list = None, combine_tof: bool = False) -> np.ndarray: +def load_data_using_multithreading( + list_tif: list = None, combine_tof: bool = False +) -> np.ndarray: """load data using multithreading""" with mp.Pool(processes=40) as pool: data = pool.map(_worker, list_tif) @@ -101,7 +102,9 @@ def retrieve_list_of_tif(folder: str) -> list: def create_x_axis_file( - lambda_array: np.ndarray = None, energy_array: np.ndarray = None, output_folder: str = "./" + lambda_array: np.ndarray = None, + energy_array: np.ndarray = None, + output_folder: str = "./", ) -> str: """create x axis file with lambda, energy and tof arrays""" x_axis_data = { @@ -139,6 +142,7 @@ def correct_chips_alignment(data: np.ndarray, config: dict) -> np.ndarray: # return data_corrected return data + def normalization_with_list_of_full_path( sample_dict: dict = None, ob_dict: dict = None, @@ -208,10 +212,16 @@ def normalization_with_list_of_full_path( export_corrected_stack_of_sample_data = export_mode.get("sample_stack", False) export_corrected_stack_of_ob_data = export_mode.get("ob_stack", False) - export_corrected_stack_of_normalized_data = export_mode.get("normalized_stack", False) - export_corrected_integrated_sample_data = export_mode.get("sample_integrated", False) + export_corrected_stack_of_normalized_data = export_mode.get( + "normalized_stack", False + ) + export_corrected_integrated_sample_data = export_mode.get( + "sample_integrated", False + ) export_corrected_integrated_ob_data = export_mode.get("ob_integrated", False) - export_corrected_integrated_normalized_data = export_mode.get("normalized_integrated", False) + export_corrected_integrated_normalized_data = export_mode.get( + "normalized_integrated", False + ) export_x_axis = export_mode.get("x_axis", True) logging.info(f"{export_corrected_stack_of_sample_data = }") @@ -238,32 +248,40 @@ def normalization_with_list_of_full_path( logging.info(f"loading ob# {_ob_run_number} ... ") if verbose: display(HTML(f"Loading ob# {_ob_run_number} ...")) - ob_master_dict[_ob_run_number][MasterDictKeys.data] = load_data_using_multithreading( - ob_master_dict[_ob_run_number][MasterDictKeys.list_tif], combine_tof=False + ob_master_dict[_ob_run_number][MasterDictKeys.data] = ( + load_data_using_multithreading( + ob_master_dict[_ob_run_number][MasterDictKeys.list_tif], + combine_tof=False, + ) ) logging.info(f"ob# {_ob_run_number} loaded!") logging.info(f"{ob_master_dict[_ob_run_number][MasterDictKeys.data].shape = }") if verbose: display(HTML(f"ob# {_ob_run_number} loaded!")) - display(HTML(f"{ob_master_dict[_ob_run_number][MasterDictKeys.data].shape = }")) + display( + HTML(f"{ob_master_dict[_ob_run_number][MasterDictKeys.data].shape = }") + ) if proton_charge_flag: normalized_by_proton_charge = ( - sample_status_metadata.all_proton_charge_found and ob_status_metadata.all_proton_charge_found + sample_status_metadata.all_proton_charge_found + and ob_status_metadata.all_proton_charge_found ) else: normalized_by_proton_charge = False if monitor_counts_flag: normalized_by_monitor_counts = ( - sample_status_metadata.all_monitor_counts_found and ob_status_metadata.all_monitor_counts_found + sample_status_metadata.all_monitor_counts_found + and ob_status_metadata.all_monitor_counts_found ) else: normalized_by_monitor_counts = False if shutter_counts_flag: normalized_by_shutter_counts = ( - sample_status_metadata.all_shutter_counts_found and ob_status_metadata.all_shutter_counts_found + sample_status_metadata.all_shutter_counts_found + and ob_status_metadata.all_shutter_counts_found ) else: normalized_by_shutter_counts = False @@ -280,9 +298,15 @@ def normalization_with_list_of_full_path( max_iterations=max_iterations, ) logging.info(f"{ob_data_combined.shape = }") - logging.info(f"number of NaN in ob_data_combined data: {np.sum(np.isnan(ob_data_combined))}") - logging.info(f"number of inf in ob_data_combined data: {np.sum(np.isinf(ob_data_combined))}") - logging.info(f"number of zeros in ob_data_combined data: {np.sum(ob_data_combined == 0)} ") + logging.info( + f"number of NaN in ob_data_combined data: {np.sum(np.isnan(ob_data_combined))}" + ) + logging.info( + f"number of inf in ob_data_combined data: {np.sum(np.isinf(ob_data_combined))}" + ) + logging.info( + f"number of zeros in ob_data_combined data: {np.sum(ob_data_combined == 0)} " + ) if verbose: display(HTML(f"{ob_data_combined.shape = }")) @@ -291,7 +315,9 @@ def normalization_with_list_of_full_path( logging.info("Correcting chips alignment ...") if verbose: display(HTML("Correcting chips alignment ...")) - ob_data_combined = correct_chips_alignment(ob_data_combined, correct_chips_alignment_config) + ob_data_combined = correct_chips_alignment( + ob_data_combined, correct_chips_alignment_config + ) logging.info("Chips alignment corrected!") if verbose: display(HTML("Chips alignment corrected!")) @@ -304,7 +330,9 @@ def normalization_with_list_of_full_path( export_corrected_stack_of_ob_data, export_corrected_integrated_ob_data, ob_data_combined, - spectra_file_name=ob_master_dict[_ob_run_number][MasterDictKeys.spectra_file_name], + spectra_file_name=ob_master_dict[_ob_run_number][ + MasterDictKeys.spectra_file_name + ], ) # load dc images @@ -312,39 +340,56 @@ def normalization_with_list_of_full_path( logging.info(f"loading dc# {_dc_run_number} ... ") if verbose: display(HTML(f"Loading dc# {_dc_run_number} ...")) - dc_master_dict[_dc_run_number][MasterDictKeys.data] = load_data_using_multithreading( - dc_master_dict[_dc_run_number][MasterDictKeys.list_tif], combine_tof=False + dc_master_dict[_dc_run_number][MasterDictKeys.data] = ( + load_data_using_multithreading( + dc_master_dict[_dc_run_number][MasterDictKeys.list_tif], + combine_tof=False, + ) ) logging.info(f"dc# {_dc_run_number} loaded!") logging.info(f"{dc_master_dict[_dc_run_number][MasterDictKeys.data].shape = }") if verbose: display(HTML(f"dc# {_dc_run_number} loaded!")) - display(HTML(f"{dc_master_dict[_dc_run_number][MasterDictKeys.data].shape = }")) + display( + HTML(f"{dc_master_dict[_dc_run_number][MasterDictKeys.data].shape = }") + ) # combine all ob images dc_data_combined = combine_dc_images(dc_master_dict) - + # load sample images for _sample_run_number in sample_master_dict.keys(): logging.info(f"loading sample# {_sample_run_number} ... ") if verbose: display(HTML(f"Loading sample# {_sample_run_number} ...")) - sample_master_dict[_sample_run_number][MasterDictKeys.data] = load_data_using_multithreading( - sample_master_dict[_sample_run_number][MasterDictKeys.list_tif], combine_tof=False + sample_master_dict[_sample_run_number][MasterDictKeys.data] = ( + load_data_using_multithreading( + sample_master_dict[_sample_run_number][MasterDictKeys.list_tif], + combine_tof=False, + ) ) logging.info(f"sample# {_sample_run_number} loaded!") - logging.info(f"{sample_master_dict[_sample_run_number][MasterDictKeys.data].shape = }") + logging.info( + f"{sample_master_dict[_sample_run_number][MasterDictKeys.data].shape = }" + ) if verbose: display(HTML(f"sample# {_sample_run_number} loaded!")) - display(HTML(f"{sample_master_dict[_sample_run_number][MasterDictKeys.data].shape = }")) + display( + HTML( + f"{sample_master_dict[_sample_run_number][MasterDictKeys.data].shape = }" + ) + ) if correct_chips_alignment_flag: logging.info("Correcting chips alignment ...") if verbose: display(HTML("Correcting chips alignment ...")) for _sample_run_number in sample_master_dict.keys(): - sample_master_dict[_sample_run_number][MasterDictKeys.data] = correct_chips_alignment( - sample_master_dict[_sample_run_number][MasterDictKeys.data], correct_chips_alignment_config + sample_master_dict[_sample_run_number][MasterDictKeys.data] = ( + correct_chips_alignment( + sample_master_dict[_sample_run_number][MasterDictKeys.data], + correct_chips_alignment_config, + ) ) logging.info("Chips alignment corrected!") if verbose: @@ -369,7 +414,9 @@ def normalization_with_list_of_full_path( logging.info(f"\t sample data shape: {data_shape}") logging.info(f"\t data type of _sample_data: {_sample_data.dtype}") logging.info(f"\t Number of zeros in sample data: {number_of_zeros}") - logging.info(f"\t Number of nan in sample data: {np.sum(np.isnan(_sample_data))}") + logging.info( + f"\t Number of nan in sample data: {np.sum(np.isnan(_sample_data))}" + ) logging.info( f"\t Percentage of zeros in sample data: {number_of_zeros / (data_shape[0] * nbr_pixels) * 100:.2f}%" ) @@ -380,7 +427,9 @@ def normalization_with_list_of_full_path( if normalized_by_proton_charge: logging.info("\t -> Normalized by proton charge") - proton_charge = sample_master_dict[_sample_run_number][MasterDictKeys.proton_charge] + proton_charge = sample_master_dict[_sample_run_number][ + MasterDictKeys.proton_charge + ] logging.info(f"\t\t proton charge: {proton_charge} C") logging.info(f"\t\t{type(proton_charge) = }") logging.info(f"\t\tbefore division: {_sample_data.dtype = }") @@ -389,7 +438,9 @@ def normalization_with_list_of_full_path( if normalized_by_monitor_counts: logging.info("\t -> Normalized by monitor counts") - monitor_counts = sample_master_dict[_sample_run_number][MasterDictKeys.monitor_counts] + monitor_counts = sample_master_dict[_sample_run_number][ + MasterDictKeys.monitor_counts + ] logging.info(f"\t\t monitor counts: {monitor_counts}") logging.info(f"\t\t{type(monitor_counts) = }") _sample_data = _sample_data / monitor_counts @@ -397,12 +448,18 @@ def normalization_with_list_of_full_path( if normalized_by_shutter_counts: list_shutter_values_for_each_image = produce_list_shutter_for_each_image( - list_time_spectra=ob_master_dict[_ob_run_number][MasterDictKeys.list_spectra], - list_shutter_counts=sample_master_dict[_sample_run_number][MasterDictKeys.shutter_counts], + list_time_spectra=ob_master_dict[_ob_run_number][ + MasterDictKeys.list_spectra + ], + list_shutter_counts=sample_master_dict[_sample_run_number][ + MasterDictKeys.shutter_counts + ], ) sample_data = [] - for _sample, _shutter_value in zip(_sample_data, list_shutter_values_for_each_image, strict=False): + for _sample, _shutter_value in zip( + _sample_data, list_shutter_values_for_each_image, strict=False + ): sample_data.append(_sample / _shutter_value) _sample_data = np.array(sample_data) @@ -412,52 +469,74 @@ def normalization_with_list_of_full_path( logging.info(f"{ob_data_combined.dtype = }") # export sample data after correction if requested - if export_corrected_stack_of_sample_data or export_corrected_integrated_sample_data: + if ( + export_corrected_stack_of_sample_data + or export_corrected_integrated_sample_data + ): export_sample_images( output_folder, export_corrected_stack_of_sample_data, export_corrected_integrated_sample_data, _sample_run_number, _sample_data, - spectra_file_name=sample_master_dict[_sample_run_number][MasterDictKeys.spectra_file_name], + spectra_file_name=sample_master_dict[_sample_run_number][ + MasterDictKeys.spectra_file_name + ], ) if dc_data_combined is not None: - logging.info(f"normalization with DC subtraction") - _normalized_data = np.divide(np.subtract(_sample_data, dc_data_combined), np.subtract(ob_data_combined, dc_data_combined), - out=np.zeros_like(_sample_data), - where=(ob_data_combined - dc_data_combined)!=0) + logging.info("normalization with DC subtraction") + _normalized_data = np.divide( + np.subtract(_sample_data, dc_data_combined), + np.subtract(ob_data_combined, dc_data_combined), + out=np.zeros_like(_sample_data), + where=(ob_data_combined - dc_data_combined) != 0, + ) else: - logging.info(f"normalization without DC subtraction") - _normalized_data = np.divide(_sample_data, ob_data_combined, - out=np.zeros_like(_sample_data), - where=ob_data_combined!=0) - + logging.info("normalization without DC subtraction") + _normalized_data = np.divide( + _sample_data, + ob_data_combined, + out=np.zeros_like(_sample_data), + where=ob_data_combined != 0, + ) + _normalized_data[ob_data_combined == 0] = 0 normalized_data[_sample_run_number] = _normalized_data # normalized_data[_sample_run_number] = np.array(np.divide(_sample_data, ob_data_combined)) logging.info(f"{normalized_data[_sample_run_number].shape = }") logging.info(f"{normalized_data[_sample_run_number].dtype = }") - logging.info(f"number of NaN in normalized data: {np.sum(np.isnan(normalized_data[_sample_run_number]))}") - logging.info(f"number of inf in normalized data: {np.sum(np.isinf(normalized_data[_sample_run_number]))}") + logging.info( + f"number of NaN in normalized data: {np.sum(np.isnan(normalized_data[_sample_run_number]))}" + ) + logging.info( + f"number of inf in normalized data: {np.sum(np.isinf(normalized_data[_sample_run_number]))}" + ) - detector_delay_us = sample_master_dict[_sample_run_number][MasterDictKeys.detector_delay_us] - time_spectra = sample_master_dict[_sample_run_number][MasterDictKeys.list_spectra] + detector_delay_us = sample_master_dict[_sample_run_number][ + MasterDictKeys.detector_delay_us + ] + time_spectra = sample_master_dict[_sample_run_number][ + MasterDictKeys.list_spectra + ] if time_spectra is None: - logging.info("Time spectra is None, cannot convert to lambda or energy arrays") + logging.info( + "Time spectra is None, cannot convert to lambda or energy arrays" + ) lambda_array = None energy_array = None - - else: - logging.info(f"We have a time_spectra!") + else: + logging.info("We have a time_spectra!") logging.info(f"time spectra shape: {time_spectra.shape}") - + if detector_delay_us is None: detector_delay_us = 0.0 - logging.info(f"detector delay is None, setting it to {detector_delay_us} us") + logging.info( + f"detector delay is None, setting it to {detector_delay_us} us" + ) logging.info(f"we have a detector delay of {detector_delay_us} us") @@ -487,26 +566,29 @@ def normalization_with_list_of_full_path( logging.info(f"Preview: {preview = }") if preview: - # display preview of normalized data - fig, axs1 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) + fig, axs1 = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) sample_data_integrated = np.nanmean(_sample_data, axis=0) im0 = axs1[0].imshow(sample_data_integrated, cmap="gray") plt.colorbar(im0, ax=axs1[0]) display(HTML(f"

Preview of run {_sample_run_number}

")) display(HTML(f"detector delay: {detector_delay_us:.2f} us")) - - axs1[0].set_title(f"Integrated Sample data") + + axs1[0].set_title("Integrated Sample data") sample_integrated1 = np.nansum(_sample_data, axis=1) sample_integrated = np.nansum(sample_integrated1, axis=1) - axs1[1].plot(sample_integrated, 'o') + axs1[1].plot(sample_integrated, "o") axs1[1].set_xlabel("File image index") axs1[1].set_ylabel("mean of full image") plt.tight_layout() - fig, axs2 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) + fig, axs2 = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) ob_data_integrated = np.nanmean(ob_data_combined, axis=0) im1 = axs2[0].imshow(ob_data_integrated, cmap="gray") plt.colorbar(im1, ax=axs2[0]) @@ -514,13 +596,15 @@ def normalization_with_list_of_full_path( ob_integrated1 = np.nansum(ob_data_combined, axis=1) ob_integrated = np.nansum(ob_integrated1, axis=1) - axs2[1].plot(ob_integrated, 'o') + axs2[1].plot(ob_integrated, "o") axs2[1].set_xlabel("File image index") axs2[1].set_ylabel("mean of full image") plt.tight_layout() if dc_data_combined is not None: - fig, axs_dc = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) + fig, axs_dc = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) dc_data_integrated = np.nanmean(dc_data_combined, axis=0) im_dc = axs_dc[0].imshow(dc_data_integrated, cmap="gray") plt.colorbar(im_dc, ax=axs_dc[0]) @@ -528,26 +612,32 @@ def normalization_with_list_of_full_path( dc_integrated1 = np.nansum(dc_data_combined, axis=1) dc_integrated = np.nansum(dc_integrated1, axis=1) - axs_dc[1].plot(dc_integrated, 'o') + axs_dc[1].plot(dc_integrated, "o") axs_dc[1].set_xlabel("File image index") axs_dc[1].set_ylabel("mean of full image") plt.tight_layout() - fig, axs3 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) - normalized_data_integrated = np.nanmean(normalized_data[_sample_run_number], axis=0) + fig, axs3 = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) + normalized_data_integrated = np.nanmean( + normalized_data[_sample_run_number], axis=0 + ) im2 = axs3[0].imshow(normalized_data_integrated, cmap="gray") plt.colorbar(im2, ax=axs3[0]) - axs3[0].set_title(f"Integrated Normalized data") + axs3[0].set_title("Integrated Normalized data") profile_step1 = np.nanmean(normalized_data[_sample_run_number], axis=1) profile = np.nanmean(profile_step1, axis=1) - axs3[1].plot(profile, 'o') + axs3[1].plot(profile, "o") axs3[1].set_xlabel("File image index") axs3[1].set_ylabel("mean of full image") plt.tight_layout() if lambda_array is not None: - fig, axs4 = plt.subplots(1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height)) + fig, axs4 = plt.subplots( + 1, 2, figsize=(2 * PLOT_SIZE.width, PLOT_SIZE.height) + ) logging.info(f"{np.shape(profile) = }") axs4[0].plot(lambda_array, profile, "*") @@ -562,24 +652,36 @@ def normalization_with_list_of_full_path( plt.show() - if export_corrected_integrated_normalized_data or export_corrected_stack_of_normalized_data: + if ( + export_corrected_integrated_normalized_data + or export_corrected_stack_of_normalized_data + ): # make up new output folder name list_ob_runs = list(ob_master_dict.keys()) - str_ob_runs = "_".join([str(_ob_run_number) for _ob_run_number in list_ob_runs]) + str_ob_runs = "_".join( + [str(_ob_run_number) for _ob_run_number in list_ob_runs] + ) full_output_folder = os.path.join( - output_folder, f"normalized_sample_{_sample_run_number}_obs_{str_ob_runs}" + output_folder, + f"normalized_sample_{_sample_run_number}_obs_{str_ob_runs}", ) # issue for WEI here ! full_output_folder = os.path.abspath(full_output_folder) os.makedirs(full_output_folder, exist_ok=True) if export_corrected_integrated_normalized_data: # making up the integrated sample data - sample_data_integrated = np.nanmean(normalized_data[_sample_run_number], axis=0) + sample_data_integrated = np.nanmean( + normalized_data[_sample_run_number], axis=0 + ) full_file_name = os.path.join(full_output_folder, "integrated.tif") - logging.info(f"\t -> Exporting integrated normalized data to {full_file_name} ...") + logging.info( + f"\t -> Exporting integrated normalized data to {full_file_name} ..." + ) make_tiff(data=sample_data_integrated, filename=full_file_name) - logging.info(f"\t -> Exporting integrated normalized data to {full_file_name} is done!") + logging.info( + f"\t -> Exporting integrated normalized data to {full_file_name} is done!" + ) if export_corrected_stack_of_normalized_data: output_stack_folder = os.path.join(full_output_folder, "stack") @@ -587,14 +689,22 @@ def normalization_with_list_of_full_path( os.makedirs(output_stack_folder, exist_ok=True) for _index, _data in enumerate(normalized_data[_sample_run_number]): - _output_file = os.path.join(output_stack_folder, f"image{_index:04d}.tif") + _output_file = os.path.join( + output_stack_folder, f"image{_index:04d}.tif" + ) make_tiff(data=_data, filename=_output_file) - logging.info(f"\t -> Exporting normalized data to {output_stack_folder} is done!") + logging.info( + f"\t -> Exporting normalized data to {output_stack_folder} is done!" + ) print(f"Exported normalized tif images are in: {output_stack_folder}!") - spectra_file = sample_master_dict[_sample_run_number][MasterDictKeys.spectra_file_name] + spectra_file = sample_master_dict[_sample_run_number][ + MasterDictKeys.spectra_file_name + ] if spectra_file and Path(spectra_file).exists(): - logging.info(f"Exported time spectra file {spectra_file} to {output_stack_folder}!") + logging.info( + f"Exported time spectra file {spectra_file} to {output_stack_folder}!" + ) shutil.copy(spectra_file, output_stack_folder) # create x-axis file @@ -613,7 +723,9 @@ def get_detector_offset_from_nexus(nexus_path: str) -> float: """get the detector offset from the nexus file""" with h5py.File(nexus_path, "r") as hdf5_data: try: - detector_offset_micros = hdf5_data["entry"]["DASlogs"]["BL10:Det:TH:DSPT1:TIDelay"]["value"][0] + detector_offset_micros = hdf5_data["entry"]["DASlogs"][ + "BL10:Det:TH:DSPT1:TIDelay" + ]["value"][0] except KeyError: detector_offset_micros = None return detector_offset_micros @@ -642,7 +754,9 @@ def export_sample_images( make_tiff(data=_data, filename=_output_file) logging.info(f"\t -> Exporting sample data to {output_stack_folder} is done!") shutil.copy(spectra_file_name, os.path.join(output_stack_folder)) - logging.info(f"\t -> Exporting spectra file {spectra_file_name} to {output_stack_folder} is done!") + logging.info( + f"\t -> Exporting spectra file {spectra_file_name} to {output_stack_folder} is done!" + ) if export_corrected_integrated_sample_data: # making up the integrated sample data @@ -650,7 +764,9 @@ def export_sample_images( full_file_name = os.path.join(sample_output_folder, "integrated.tif") logging.info(f"\t -> Exporting integrated sample data to {full_file_name} ...") make_tiff(data=sample_data_integrated, filename=full_file_name) - logging.info(f"\t -> Exporting integrated sample data to {full_file_name} is done!") + logging.info( + f"\t -> Exporting integrated sample data to {full_file_name} is done!" + ) display(HTML(f"Created folder {output_stack_folder} for sample outputs!")) @@ -667,10 +783,13 @@ def export_ob_images( logging.info(f"> Exporting combined ob images to {output_folder} ...") logging.info(f"\t{ob_run_numbers = }") list_ob_runs_number_only = [ - str(isolate_run_number_from_full_path(_ob_run_number)) for _ob_run_number in ob_run_numbers + str(isolate_run_number_from_full_path(_ob_run_number)) + for _ob_run_number in ob_run_numbers ] if len(list_ob_runs_number_only) == 1: - ob_output_folder = os.path.join(output_folder, f"ob_{list_ob_runs_number_only[0]}") + ob_output_folder = os.path.join( + output_folder, f"ob_{list_ob_runs_number_only[0]}" + ) else: str_list_ob_runs = "_".join(list_ob_runs_number_only) ob_output_folder = os.path.join(output_folder, f"ob_{str_list_ob_runs}") @@ -699,7 +818,9 @@ def export_ob_images( logging.info(f"\t -> Exporting ob data to {output_stack_folder} is done!") # copy spectra file to the output folder shutil.copy(spectra_file_name, os.path.join(output_stack_folder)) - logging.info(f"\t -> Exported spectra file {spectra_file_name} to {output_stack_folder}!") + logging.info( + f"\t -> Exported spectra file {spectra_file_name} to {output_stack_folder}!" + ) display(HTML(f"Created folder {output_stack_folder} for OB outputs!")) @@ -790,7 +911,9 @@ def update_dict_with_shutter_counts(master_dict: dict) -> tuple[dict, bool]: if _value == "0": break list_shutter_counts.append(float(_value)) - master_dict[run_number][MasterDictKeys.shutter_counts] = list_shutter_counts + master_dict[run_number][MasterDictKeys.shutter_counts] = ( + list_shutter_counts + ) return master_dict, status_all_shutter_counts_found @@ -827,11 +950,15 @@ def update_dict_with_proton_charge(master_dict: dict) -> tuple[dict, bool]: try: with h5py.File(_nexus_path, "r") as hdf5_data: - proton_charge = hdf5_data["entry"][MasterDictKeys.proton_charge][0] / 1e12 + proton_charge = ( + hdf5_data["entry"][MasterDictKeys.proton_charge][0] / 1e12 + ) except KeyError: proton_charge = None status_all_proton_charge_found = False - master_dict[_run_number][MasterDictKeys.proton_charge] = np.float32(proton_charge) + master_dict[_run_number][MasterDictKeys.proton_charge] = np.float32( + proton_charge + ) return status_all_proton_charge_found @@ -852,14 +979,18 @@ def update_dict_with_monitor_counts(master_dict: dict) -> bool: except KeyError: monitor_counts = None status_all_monitor_counts_found = False - master_dict[_run_number][MasterDictKeys.monitor_counts] = np.float32(monitor_counts) + master_dict[_run_number][MasterDictKeys.monitor_counts] = np.float32( + monitor_counts + ) return status_all_monitor_counts_found def update_dict_with_list_of_images(master_dict: dict) -> dict: """update the master dict with list of images""" for _run_number in master_dict.keys(): - list_tif = retrieve_list_of_tif(master_dict[_run_number][MasterDictKeys.data_path]) + list_tif = retrieve_list_of_tif( + master_dict[_run_number][MasterDictKeys.data_path] + ) logging.info(f"Retrieved {len(list_tif)} tif files for run {_run_number}!") master_dict[_run_number][MasterDictKeys.list_tif] = list_tif @@ -871,7 +1002,9 @@ def get_list_run_number(data_folder: str) -> list: return list_run_number -def update_dict_with_nexus_full_path(nexus_root_path: str, instrument: str, master_dict: dict) -> dict: +def update_dict_with_nexus_full_path( + nexus_root_path: str, instrument: str, master_dict: dict +) -> dict: """create dict of nexus path for each run number""" for run_number in master_dict.keys(): master_dict[run_number][MasterDictKeys.nexus_path] = os.path.join( @@ -892,7 +1025,9 @@ def update_with_nexus_metadata(master_dict: dict) -> dict: def update_dict_with_data_full_path(data_root_path: str, master_dict: dict) -> dict: """create dict of data path for each run number""" for run_number in master_dict.keys(): - master_dict[run_number][MasterDictKeys.data_path] = os.path.join(data_root_path, f"Run_{run_number}") + master_dict[run_number][MasterDictKeys.data_path] = os.path.join( + data_root_path, f"Run_{run_number}" + ) def create_master_dict( @@ -942,7 +1077,9 @@ def create_master_dict( return master_dict, status_metadata -def produce_list_shutter_for_each_image(list_time_spectra: list = None, list_shutter_counts: list = None) -> list: +def produce_list_shutter_for_each_image( + list_time_spectra: list = None, list_shutter_counts: list = None +) -> list: """produce list of shutter counts for each image""" delat_time_spectra = list_time_spectra[1] - list_time_spectra[0] @@ -952,31 +1089,41 @@ def produce_list_shutter_for_each_image(list_time_spectra: list = None, list_shu logging.info(f"\t{list_index_jump = }") logging.info(f"\t{list_shutter_counts = }") - list_shutter_values_for_each_image = np.zeros(len(list_time_spectra), dtype=np.float32) + list_shutter_values_for_each_image = np.zeros( + len(list_time_spectra), dtype=np.float32 + ) if len(list_shutter_counts) == 1: # resonance mode list_shutter_values_for_each_image.fill(list_shutter_counts[0]) return list_shutter_values_for_each_image - list_shutter_values_for_each_image[0 : list_index_jump[0] + 1].fill(list_shutter_counts[0]) + list_shutter_values_for_each_image[0 : list_index_jump[0] + 1].fill( + list_shutter_counts[0] + ) for _index in range(1, len(list_index_jump)): _start = list_index_jump[_index - 1] _end = list_index_jump[_index] - list_shutter_values_for_each_image[_start + 1 : _end + 1].fill(list_shutter_counts[_index]) + list_shutter_values_for_each_image[_start + 1 : _end + 1].fill( + list_shutter_counts[_index] + ) - list_shutter_values_for_each_image[list_index_jump[-1] + 1 :] = list_shutter_counts[-1] + list_shutter_values_for_each_image[list_index_jump[-1] + 1 :] = list_shutter_counts[ + -1 + ] return list_shutter_values_for_each_image -def replace_zero_with_local_median(data: np.ndarray, - kernel_size: Tuple[int, int, int] = (3, 3, 3), - max_iterations: int = 10) -> np.ndarray: +def replace_zero_with_local_median( + data: np.ndarray, + kernel_size: Tuple[int, int, int] = (3, 3, 3), + max_iterations: int = 10, +) -> np.ndarray: """ Replace 0 values in a 3D array using local median filtering. This function ONLY processes small neighborhoods around 0 pixels, avoiding expensive computation on the entire dataset. - + Parameters: ----------- data : np.ndarray @@ -987,7 +1134,7 @@ def replace_zero_with_local_median(data: np.ndarray, max_iterations : int Maximum number of iterations to replace 0 values Default is 10 - + Returns: -------- np.ndarray @@ -1006,7 +1153,7 @@ def replace_zero_with_local_median(data: np.ndarray, # Calculate padding for kernel pad_h, pad_w, pad_d = [k // 2 for k in kernel_size] - + for iteration in range(max_iterations): # Find current 0 locations zero_coords = np.argwhere(result == 0) @@ -1016,13 +1163,15 @@ def replace_zero_with_local_median(data: np.ndarray, logging.info(f"All 0 values replaced after {iteration} iterations") break - logging.info(f"Iteration {iteration + 1}: {current_zero_count} 0 values remaining") + logging.info( + f"Iteration {iteration + 1}: {current_zero_count} 0 values remaining" + ) # Process each 0 pixel individually replaced_count = 0 for coord in zero_coords: y, x, z = coord - + # Define the local neighborhood bounds y_min = max(0, y - pad_h) y_max = min(result.shape[0], y + pad_h + 1) @@ -1030,13 +1179,13 @@ def replace_zero_with_local_median(data: np.ndarray, x_max = min(result.shape[1], x + pad_w + 1) z_min = max(0, z - pad_d) z_max = min(result.shape[2], z + pad_d + 1) - + # Extract the local neighborhood neighborhood = result[y_min:y_max, x_min:x_max, z_min:z_max] - + # Get non-NaN values in the neighborhood valid_values = neighborhood[~np.isnan(neighborhood)] - + # If we have valid values, compute median and replace if len(valid_values) > 0: median_value = np.median(valid_values) @@ -1048,30 +1197,34 @@ def replace_zero_with_local_median(data: np.ndarray, # If no progress was made, break if replaced_count == 0: remaining_zero_count = np.sum(result == 0) - logging.info(f"No progress made. {remaining_zero_count} zero values could not be replaced") + logging.info( + f"No progress made. {remaining_zero_count} zero values could not be replaced" + ) logging.info("(These may be in regions with no valid neighbors)") break final_zero_count = np.sum(result == 0) logging.info(f"Final zero count: {final_zero_count}") - logging.info(f"Successfully replaced {initial_zero_count - final_zero_count} zero values") + logging.info( + f"Successfully replaced {initial_zero_count - final_zero_count} zero values" + ) return result def combine_dc_images(dc_master_dict: dict) -> np.ndarray: """combine all dc images - + Parameters: ----------- dc_master_dict : dict master dict of dc run numbers - + Returns: -------- np.ndarray combined dc data - + """ logging.info("Combining all dark current images") full_dc_data = [] @@ -1082,7 +1235,9 @@ def combine_dc_images(dc_master_dict: dict) -> np.ndarray: for _dc_run_number in dc_master_dict.keys(): logging.info(f"Combining dc# {_dc_run_number} ...") - dc_data = np.array(dc_master_dict[_dc_run_number][MasterDictKeys.data], dtype=np.float32) + dc_data = np.array( + dc_master_dict[_dc_run_number][MasterDictKeys.data], dtype=np.float32 + ) full_dc_data.append(dc_data) logging.info(f"{np.shape(full_dc_data) = }") @@ -1101,11 +1256,11 @@ def combine_ob_images( use_shutter_counts: bool = False, replace_ob_zeros_by_nan: bool = False, replace_ob_zeros_by_local_median: bool = False, - kernel_size_for_local_median: Tuple[int, int, int] = (3, 3, 3), + kernel_size_for_local_median: Tuple[int, int, int] = (3, 3, 3), max_iterations: int = 10, ) -> np.ndarray: """combine all ob images and correct by proton charge and shutter counts - + Parameters: ----------- ob_master_dict : dict @@ -1124,12 +1279,12 @@ def combine_ob_images( kernel size for local median filtering max_iterations : int maximum number of iterations for local median filtering - + Returns: -------- np.ndarray combined ob data - + """ logging.info("Combining all open beam images") @@ -1137,15 +1292,21 @@ def combine_ob_images( logging.info(f"\tcorrecting by monitor counts: {use_monitor_counts}") logging.info(f"\tshutter counts: {use_shutter_counts}") logging.info(f"\treplace ob zeros by nan: {replace_ob_zeros_by_nan}") - logging.info(f"\treplace ob zeros by local median: {replace_ob_zeros_by_local_median}") - logging.info(f"\tkernel size for local median: y:{kernel_size_for_local_median[0]}, " - f"x:{kernel_size_for_local_median[1]}, " - f"tof:{kernel_size_for_local_median[2]}") + logging.info( + f"\treplace ob zeros by local median: {replace_ob_zeros_by_local_median}" + ) + logging.info( + f"\tkernel size for local median: y:{kernel_size_for_local_median[0]}, " + f"x:{kernel_size_for_local_median[1]}, " + f"tof:{kernel_size_for_local_median[2]}" + ) full_ob_data_corrected = [] for _ob_run_number in ob_master_dict.keys(): logging.info(f"Combining ob# {_ob_run_number} ...") - ob_data = np.array(ob_master_dict[_ob_run_number][MasterDictKeys.data], dtype=np.float32) + ob_data = np.array( + ob_master_dict[_ob_run_number][MasterDictKeys.data], dtype=np.float32 + ) # get statistics of ob data data_shape = ob_data.shape @@ -1154,7 +1315,9 @@ def combine_ob_images( number_of_zeros = np.sum(ob_data == 0) logging.info(f"\t ob data shape: {data_shape}") logging.info(f"\t Number of zeros in ob data: {number_of_zeros}") - logging.info(f"\t Percentage of zeros in ob data: {number_of_zeros / (data_shape[0] * nbr_pixels) * 100:.2f}%") + logging.info( + f"\t Percentage of zeros in ob data: {number_of_zeros / (data_shape[0] * nbr_pixels) * 100:.2f}%" + ) logging.info(f"\t Mean of ob data: {np.mean(ob_data)}") logging.info(f"\t maximum of ob data: {np.max(ob_data)}") logging.info(f"\t minimum of ob data: {np.min(ob_data)}") @@ -1172,7 +1335,9 @@ def combine_ob_images( if use_monitor_counts: logging.info("\t -> Normalized by monitor counts") - monitor_counts = ob_master_dict[_ob_run_number][MasterDictKeys.monitor_counts] + monitor_counts = ob_master_dict[_ob_run_number][ + MasterDictKeys.monitor_counts + ] logging.info(f"\t\t monitor counts: {monitor_counts}") logging.info(f"\t\t{type(monitor_counts) = }") ob_data = ob_data / monitor_counts @@ -1182,14 +1347,20 @@ def combine_ob_images( logging.info("\t -> Normalized by shutter counts") list_shutter_values_for_each_image = produce_list_shutter_for_each_image( - list_time_spectra=ob_master_dict[_ob_run_number][MasterDictKeys.list_spectra], - list_shutter_counts=ob_master_dict[_ob_run_number][MasterDictKeys.shutter_counts], + list_time_spectra=ob_master_dict[_ob_run_number][ + MasterDictKeys.list_spectra + ], + list_shutter_counts=ob_master_dict[_ob_run_number][ + MasterDictKeys.shutter_counts + ], ) logging.info(f"{list_shutter_values_for_each_image.shape = }") temp_ob_data = np.empty_like(ob_data, dtype=np.float32) for _index in range(len(list_shutter_values_for_each_image)): - temp_ob_data[_index] = ob_data[_index] / list_shutter_values_for_each_image[_index] + temp_ob_data[_index] = ( + ob_data[_index] / list_shutter_values_for_each_image[_index] + ) logging.info(f"{temp_ob_data.shape = }") ob_data = temp_ob_data.copy() @@ -1197,9 +1368,11 @@ def combine_ob_images( # logging.info(f"{ob_data_combined.shape = }") if replace_ob_zeros_by_local_median: - ob_data = replace_zero_with_local_median(ob_data, - kernel_size=kernel_size_for_local_median, - max_iterations=max_iterations) + ob_data = replace_zero_with_local_median( + ob_data, + kernel_size=kernel_size_for_local_median, + max_iterations=max_iterations, + ) full_ob_data_corrected.append(ob_data) logging.info(f"{np.shape(full_ob_data_corrected) = }") @@ -1224,9 +1397,15 @@ def combine_ob_images( formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) - parser.add_argument("--sample", type=str, nargs=1, help="Full path to sample run number") - parser.add_argument("--ob", type=str, nargs=1, help="Full path to the ob run number") - parser.add_argument("--output", type=str, nargs=1, help="Path to the output folder", default="./") + parser.add_argument( + "--sample", type=str, nargs=1, help="Full path to sample run number" + ) + parser.add_argument( + "--ob", type=str, nargs=1, help="Full path to the ob run number" + ) + parser.add_argument( + "--output", type=str, nargs=1, help="Path to the output folder", default="./" + ) args = parser.parse_args() logging.info(f"{args = }") @@ -1275,7 +1454,9 @@ def combine_ob_images( # normalization(sample_folder=sample_folder, ob_folder=ob_folder, output_folder=output_folder) - print(f"Normalization is done! Check the log file {log_file_name} for more details!") + print( + f"Normalization is done! Check the log file {log_file_name} for more details!" + ) print(f"Exported data to {output_folder}") # sample = /SNS/VENUS/IPTS-34808/shared/autoreduce/mcp/November17_Sample6_UA_H_Batteries_1_5_Angs_min_30Hz_5C diff --git a/notebooks/__code/resonance_fitting/resonance_fitting.py b/notebooks/__code/resonance_fitting/resonance_fitting.py index 65fdc9de1..f11331fb3 100644 --- a/notebooks/__code/resonance_fitting/resonance_fitting.py +++ b/notebooks/__code/resonance_fitting/resonance_fitting.py @@ -1,7 +1,5 @@ -import glob from dotenv import load_dotenv import logging as notebook_logging -from logging.handlers import RotatingFileHandler import os from loguru import logger from pathlib import Path @@ -11,16 +9,17 @@ import ipywidgets as widgets import matplotlib.pyplot as plt from IPython.display import HTML, display -from ipywidgets import interactive -from PIL import Image import periodictable import ipysheet -from ipysheet import sheet, cell, row, column, from_dataframe, to_array, calculation +from ipysheet import from_dataframe, to_array # from pleiades.processing.normalization import normalization as normalization_with_pleaides # from pleiades.processing import Roi as PleiadesRoi # from pleiades.processing import Facility -from pleiades.sammy.io.data_manager import convert_csv_to_sammy_twenty, validate_sammy_twenty_format +from pleiades.sammy.io.data_manager import ( + convert_csv_to_sammy_twenty, + validate_sammy_twenty_format, +) from pleiades.sammy.io.json_manager import JsonManager from pleiades.sammy.io.inp_manager import InpManager from pleiades.sammy.backends.local import LocalSammyRunner @@ -29,26 +28,18 @@ from pleiades.sammy.results.manager import ResultsManager from __code.resonance_fitting import VENUS_RES_FUNC, SAMMY_EXE_PATH -from __code._utilities.list import extract_list_of_runs_from_string -from __code._utilities.nexus import extract_file_path_from_nexus from __code._utilities.logger import display_dictionary_in_logging # from __code.ipywe.myfileselector import MyFileSelectorPanel -from __code.resonance_fitting.config import DEBUG_DATA, timepix1_config, timepix3_config +from __code.resonance_fitting.config import DEBUG_DATA from __code.resonance_fitting.get import Get from __code.normalization_tof.normalization_tof import NormalizationTof from __code.ipywe.fileselector import FileSelectorPanel as FileSelectorPanel -from __code.normalization_tof import DetectorType, autoreduce_dir, distance_source_detector_m, raw_dir -from __code.normalization_tof.normalization_for_timepix1_timepix3 import ( - load_data_using_multithreading, - # normalization, - normalization_with_list_of_full_path, - retrieve_list_of_tif, -) FONT_SIZE = 14 + class FilesPaths: logging = None transmission = None @@ -56,6 +47,7 @@ class FilesPaths: json_path = None sammy_files_multi_mode = None + class FolderPaths: working = None stagging = None @@ -69,18 +61,17 @@ class FolderPaths: class ResonanceFitting(NormalizationTof): - df_to_use = None - horizontal_box = None # total abundance display box + horizontal_box = None # total abundance display box def __init__(self, working_dir=None, debug=False): self.folder_paths = FolderPaths() self.files_paths = FilesPaths() - + self.initialize_logging() load_dotenv(".envrc") - + if debug: self.folder_paths.working = Path(DEBUG_DATA.working_dir) self.folder_paths.output = Path(DEBUG_DATA.output_folder) @@ -105,7 +96,9 @@ def __init__(self, working_dir=None, debug=False): # self.autoreduce_dir = autoreduce_dir[_beamline][0] + str(ipts) + autoreduce_dir[_beamline][1] # self.shared_dir = str(Path(shared_dir[self.instrument][0]) / str(ipts) / shared_dir[self.instrument][1]) - self.folder_paths.shared = Path("/") / _facility / self.instrument / str(ipts) / "shared" + self.folder_paths.shared = ( + Path("/") / _facility / self.instrument / str(ipts) / "shared" + ) notebook_logging.info(f"Instrument: {self.instrument}") notebook_logging.info(f"Working dir: {self.folder_paths.working}") @@ -115,16 +108,17 @@ def __init__(self, working_dir=None, debug=False): notebook_logging.info(f"Shared dir: {self.folder_paths.shared}") def initialize_logging(self): - - logger.remove() # Remove default logger + logger.remove() # Remove default logger LOG_PATH = "/SNS/VENUS/shared/log/" base_file_name = Path(__file__).name file_name_without_extension = Path(base_file_name).stem user_name = os.getlogin() # add user name to the log file name - log_file_name = LOG_PATH / Path(f"{user_name}_{str(file_name_without_extension)}.log") + log_file_name = LOG_PATH / Path( + f"{user_name}_{str(file_name_without_extension)}.log" + ) self.files_paths.logging = log_file_name - + # rotating_handler = RotatingFileHandler(log_file_name, # maxBytes=50*1024*1024, # 50 MB # backupCount=5) @@ -140,8 +134,14 @@ def initialize_logging(self): format="[%(levelname)s] - %(asctime)s - %(message)s", level=notebook_logging.INFO, ) - notebook_logging.info(f"*** Starting a new script {file_name_without_extension} ***") - display(HTML(f"Log file: {log_file_name}")) + notebook_logging.info( + f"*** Starting a new script {file_name_without_extension} ***" + ) + display( + HTML( + f"Log file: {log_file_name}" + ) + ) def select_normalized_text_file(self): self.file_selector = FileSelectorPanel( @@ -169,7 +169,7 @@ def select_isotope_and_abundance(self): list_elements = periodictable.elements dict_elements = {} for _el in list_elements: - dict_elements[_el.name.capitalize()] = {'symbol': _el.symbol} + dict_elements[_el.name.capitalize()] = {"symbol": _el.symbol} list_elements_names = list(dict_elements.keys()) list_elements_names.sort() self.dict_elements = dict_elements @@ -177,13 +177,17 @@ def select_isotope_and_abundance(self): if self.debug: default_symbol_selected = DEBUG_DATA.isotope_element for _el_name in dict_elements.keys(): - if dict_elements[_el_name]['symbol'] == default_symbol_selected: + if dict_elements[_el_name]["symbol"] == default_symbol_selected: default_element_selected = _el_name break else: default_element_selected = "Hydrogen" - display(HTML(f"Select element/isotopes to use:")) + display( + HTML( + f"Select element/isotopes to use:" + ) + ) self.list_elements_widget = widgets.Dropdown( options=list_elements_names, value=default_element_selected, @@ -191,12 +195,12 @@ def select_isotope_and_abundance(self): disabled=False, ) display(self.list_elements_widget) - self.list_elements_widget.observe(self._on_element_change, names='value') - + self.list_elements_widget.observe(self._on_element_change, names="value") + self._display_tables_and_buttons() # empty stylesheet table for now - _df = pd.DataFrame({'Isotope': [None], 'Abundance (%)': [0]}) + _df = pd.DataFrame({"Isotope": [None], "Abundance (%)": [0]}) self.isotope_to_use_sheet = from_dataframe(_df) self.df_to_use = _df display(self.isotope_to_use_sheet) @@ -210,8 +214,12 @@ def perform_fitting(self): self._sammy_files_multi_mode() self._local_sammy_config() self._multi_isotope_sammy_execution() - - display(HTML(f"SAMMY input files created in: {self.folder_paths.sammy_working}!") ) + + display( + HTML( + f"SAMMY input files created in: {self.folder_paths.sammy_working}!" + ) + ) def display_results(self): self._results_analysis() @@ -226,7 +234,11 @@ def _output_folder_selected(self, folder_path): def _transmitted_text_file_selected(self, file_path): file_path = Path(file_path) notebook_logging.info(f"Transmitted text file selected: {file_path}") - display(HTML(f"Transmission file: {file_path.name} ... selected!")) + display( + HTML( + f"Transmission file: {file_path.name} ... selected!" + ) + ) self.files_paths.transmission = file_path @@ -234,36 +246,43 @@ def _transmitted_text_file_selected(self, file_path): def _display_transmitted_data(self): notebook_logging.info("Displaying transmitted data ...") - df = pd.read_csv(self.files_paths.transmission, - delim_whitespace=True, - names=['Energy (eV)', 'Transmission', 'Uncertainty'], - index_col=False, - skiprows=1) - display(HTML(f"Transmitted data preview:")) + df = pd.read_csv( + self.files_paths.transmission, + delim_whitespace=True, + names=["Energy (eV)", "Transmission", "Uncertainty"], + index_col=False, + skiprows=1, + ) + display( + HTML( + f"Transmitted data preview:" + ) + ) display(df.head(10)) - + fig, ax = plt.subplots(figsize=(10, 6)) - ax.errorbar(df['Energy (eV)'], - df['Transmission'], - yerr=df['Uncertainty'], - fmt='o', - markersize=3, - label=str(self.files_paths.transmission.name), - color='blue', - ecolor='lightgray', - elinewidth=1, - capsize=2) - ax.set_xlabel('Energy (eV)') - ax.set_xscale('log') - ax.set_yscale('log') + ax.errorbar( + df["Energy (eV)"], + df["Transmission"], + yerr=df["Uncertainty"], + fmt="o", + markersize=3, + label=str(self.files_paths.transmission.name), + color="blue", + ecolor="lightgray", + elinewidth=1, + capsize=2, + ) + ax.set_xlabel("Energy (eV)") + ax.set_xscale("log") + ax.set_yscale("log") ax.grid(True, which="both", ls="--", lw=0.5) - ax.set_ylabel('Transmission') - ax.set_title(f'Transmitted Data with Uncertainty') + ax.set_ylabel("Transmission") + ax.set_title("Transmitted Data with Uncertainty") ax.legend() plt.show() def _stagging_folders_setup(self, file_path): - # set up various stagging folder for SAMMY self.folder_paths.output = Path(file_path) self.folder_paths.stagging = self.folder_paths.output / "hf_analysis" @@ -279,32 +298,64 @@ def _stagging_folders_setup(self, file_path): Path(self.folder_paths.sammy_working).mkdir(parents=True, exist_ok=True) Path(self.folder_paths.sammy_output).mkdir(parents=True, exist_ok=True) - - notebook_logging.info("\nStagging folders setup:") - notebook_logging.info(f"output folder: {self.folder_paths.output} ... {self.folder_paths.output.is_dir()} ") - notebook_logging.info(f"Stagging folder: {self.folder_paths.stagging} ... {self.folder_paths.stagging.is_dir()}") - notebook_logging.info(f"Spectra folder: {self.folder_paths.spectra} ... {self.folder_paths.spectra.is_dir()}") - notebook_logging.info(f"Twenty folder: {self.folder_paths.twenty} ... {self.folder_paths.twenty.is_dir()}") - notebook_logging.info(f"SAMMY working folder: {self.folder_paths.sammy_working} ... {self.folder_paths.sammy_working.is_dir()}") - notebook_logging.info(f"SAMMY output folder: {self.folder_paths.sammy_output} ... {self.folder_paths.sammy_output.is_dir()}") - display(HTML(f"Stagging folders created!")) + notebook_logging.info("\nStagging folders setup:") + notebook_logging.info( + f"output folder: {self.folder_paths.output} ... {self.folder_paths.output.is_dir()} " + ) + notebook_logging.info( + f"Stagging folder: {self.folder_paths.stagging} ... {self.folder_paths.stagging.is_dir()}" + ) + notebook_logging.info( + f"Spectra folder: {self.folder_paths.spectra} ... {self.folder_paths.spectra.is_dir()}" + ) + notebook_logging.info( + f"Twenty folder: {self.folder_paths.twenty} ... {self.folder_paths.twenty.is_dir()}" + ) + notebook_logging.info( + f"SAMMY working folder: {self.folder_paths.sammy_working} ... {self.folder_paths.sammy_working.is_dir()}" + ) + notebook_logging.info( + f"SAMMY output folder: {self.folder_paths.sammy_output} ... {self.folder_paths.sammy_output.is_dir()}" + ) + display( + HTML( + f"Stagging folders created!" + ) + ) notebook_logging.info("") def _converting_transmission_to_twenty_format(self): - notebook_logging.info("Converting transmission data .txt to .twenty format for SAMMY ...") - twenty_file = self.folder_paths.twenty / self.files_paths.transmission.name.replace(".txt", ".twenty") + notebook_logging.info( + "Converting transmission data .txt to .twenty format for SAMMY ..." + ) + twenty_file = ( + self.folder_paths.twenty + / self.files_paths.transmission.name.replace(".txt", ".twenty") + ) convert_csv_to_sammy_twenty(self.files_paths.transmission, twenty_file) if validate_sammy_twenty_format(twenty_file): - notebook_logging.info(f"Conversion successful! Twenty file created at: {twenty_file}") - display(HTML(f"Twenty file created at: {twenty_file}")) + notebook_logging.info( + f"Conversion successful! Twenty file created at: {twenty_file}" + ) + display( + HTML( + f"Twenty file created at: {twenty_file}" + ) + ) else: - notebook_logging.error("Conversion failed! The generated .twenty file is not valid.") - display(HTML(f"Conversion failed! The generated .twenty file is not valid.")) + notebook_logging.error( + "Conversion failed! The generated .twenty file is not valid." + ) + display( + HTML( + f"Conversion failed! The generated .twenty file is not valid." + ) + ) def _on_element_change(self, change): notebook_logging.info(f"Element selected: {change['new']}") self.isotope_sheet.close() - element_symbol = self.dict_elements[change['new']]['symbol'] + element_symbol = self.dict_elements[change["new"]]["symbol"] self._create_and_display_isotope_table(element_symbol=element_symbol) self.isotope_to_use_sheet.close() @@ -321,29 +372,29 @@ def _on_element_change(self, change): def _get_dict_isotopes(self, element_symbol): """ Get a dictionary of isotopes and their abundances for a given element. - + dict = {[isotope_name]: {'abundance': None, 'mass': None}} - + return dict """ # element = periodictable.elements.symbol(element_name) _dict = {} for _el in getattr(periodictable, element_symbol): - _dict[str(_el)] = {'abundance': _el.abundance, - 'mass': _el.mass} + _dict[str(_el)] = {"abundance": _el.abundance, "mass": _el.mass} notebook_logging.info(f"in get_dict_isotopes: {element_symbol = }, {_dict = }") return _dict def _create_and_display_isotope_table(self, element_symbol): - dict_isotopes = self._get_dict_isotopes(element_symbol) list_isotopes_for_this_element = dict_isotopes.keys() notebook_logging.info(f"{list_isotopes_for_this_element}") # list_mass_isotopes = [dict_isotopes[iso]['mass'] for iso in list_isotopes_for_this_element] - list_abundance_isotopes = [dict_isotopes[iso]['abundance'] for iso in list_isotopes_for_this_element] - + list_abundance_isotopes = [ + dict_isotopes[iso]["abundance"] for iso in list_isotopes_for_this_element + ] + # create a boolean array of the same length as list_isotopes_for_this_element list_use_it = np.array([False for _ in list_isotopes_for_this_element]) @@ -351,10 +402,12 @@ def _create_and_display_isotope_table(self, element_symbol): if value > 0: list_use_it[_index] = True - temp_dict = {'Isotope': np.array(list_isotopes_for_this_element), - 'Abundance (%)': np.array(list_abundance_isotopes), - 'use it': list_use_it} - + temp_dict = { + "Isotope": np.array(list_isotopes_for_this_element), + "Abundance (%)": np.array(list_abundance_isotopes), + "use it": list_use_it, + } + df = pd.DataFrame(temp_dict) self.isotope_sheet = from_dataframe(df) @@ -370,27 +423,39 @@ def on_validate_isotope_selection(self, b): array = to_array(self.isotope_sheet) notebook_logging.info(f"Isotope selection as array:\n{array}") - + for _index, row in enumerate(array): - notebook_logging.info(f"at {_index =}, {row[0] = }, {row[1] = }, {row[2] = }") + notebook_logging.info( + f"at {_index =}, {row[0] = }, {row[1] = }, {row[2] = }" + ) # retrieve the content of the isotope_to_use_sheet df_to_use = ipysheet.to_dataframe(self.isotope_to_use_sheet) # remove any row with 'Isotope' = None - df_to_use = df_to_use[df_to_use['Isotope'].notna()] + df_to_use = df_to_use[df_to_use["Isotope"].notna()] notebook_logging.info(f"Current isotopes to use:\n{df_to_use}") # add it the new isotopes selected with 'use it' = True for _index, row in enumerate(array): notebook_logging.info(f"Processing row {_index}: {row}, {row[2] =}") - if str(row[2]) == 'True': # 'use it' is True + if str(row[2]) == "True": # 'use it' is True isotope_name = row[0] abundance = f"{float(str(row[1])):.4f}" - notebook_logging.info(f"Adding isotope: {isotope_name} with abundance: {abundance}") - df_to_use = pd.concat([df_to_use, pd.DataFrame({'Isotope': [isotope_name], 'Abundance (%)': [abundance]})], ignore_index=True) - - # remove duplicates - self.df_to_use = df_to_use.drop_duplicates(subset='Isotope') + notebook_logging.info( + f"Adding isotope: {isotope_name} with abundance: {abundance}" + ) + df_to_use = pd.concat( + [ + df_to_use, + pd.DataFrame( + {"Isotope": [isotope_name], "Abundance (%)": [abundance]} + ), + ], + ignore_index=True, + ) + + # remove duplicates + self.df_to_use = df_to_use.drop_duplicates(subset="Isotope") self.isotope_to_use_sheet.close() self.validate_isotope_button.close() @@ -400,29 +465,30 @@ def on_validate_isotope_selection(self, b): # listen to all events in this table for cell in self.isotope_to_use_sheet.cells: - cell.observe(self._on_isotope_to_use_table_change, names='value') + cell.observe(self._on_isotope_to_use_table_change, names="value") self._display_tables_and_buttons() display(self.isotope_to_use_sheet) - + self._update_total_abundance_of_isotopes_to_use() # disable button (to make sure only 1 element is added at a time) - # self.validate_isotope_button.disabled = True + # self.validate_isotope_button.disabled = True def _on_isotope_to_use_table_change(self, change): self._update_total_abundance_of_isotopes_to_use() def _update_total_abundance_of_isotopes_to_use(self): - if self.horizontal_box: self.horizontal_box.close() df_to_use = ipysheet.to_dataframe(self.isotope_to_use_sheet) - list_abundances = df_to_use['Abundance (%)'].tolist() + list_abundances = df_to_use["Abundance (%)"].tolist() list_abundances_float = [float(_value) for _value in list_abundances] total_abundance = sum(list_abundances_float) - notebook_logging.info(f"Total abundance of isotopes to use: {total_abundance} %") + notebook_logging.info( + f"Total abundance of isotopes to use: {total_abundance} %" + ) if total_abundance > 100.0: color = "red" @@ -436,16 +502,15 @@ def _update_total_abundance_of_isotopes_to_use(self): display(self.horizontal_box) def _display_tables_and_buttons(self): - """display the isotope table. the button to validate the selection as well as the table of isotopes to use - """ - + """display the isotope table. the button to validate the selection as well as the table of isotopes to use""" + dict_elements = self.dict_elements full_name_of_element = self.list_elements_widget.value - element_symbol = dict_elements[full_name_of_element]['symbol'] + element_symbol = dict_elements[full_name_of_element]["symbol"] self._create_and_display_isotope_table(element_symbol=element_symbol) - + self.validate_isotope_button = widgets.Button( description="Add to list of elements/isotopes to consider", layout=widgets.Layout(width="100%"), @@ -464,11 +529,11 @@ def _reformat_list_isotopes(self, list_isotopes): """ list_reformatted = [] for _iso in list_isotopes: - if '-' not in _iso: + if "-" not in _iso: list_reformatted.append(_iso) continue - - parts = _iso.split('-') + + parts = _iso.split("-") if len(parts) == 2: reformatted = f"{parts[1]}-{parts[0]}" list_reformatted.append(reformatted) @@ -486,10 +551,10 @@ def _create_json_manager(self): # retrieve isotopes and abundances to use df_to_use = ipysheet.to_dataframe(self.isotope_to_use_sheet) - list_isotopes = df_to_use['Isotope'].tolist() + list_isotopes = df_to_use["Isotope"].tolist() list_isotopes_reformatted = self._reformat_list_isotopes(list_isotopes) - list_abundances = df_to_use['Abundance (%)'].tolist() - list_abundances_float = [float(_value)*0.01 for _value in list_abundances] + list_abundances = df_to_use["Abundance (%)"].tolist() + list_abundances_float = [float(_value) * 0.01 for _value in list_abundances] notebook_logging.info(f"\t{list_isotopes = }") notebook_logging.info(f"\t{list_isotopes_reformatted =}") @@ -501,23 +566,32 @@ def _create_json_manager(self): isotopes=list_isotopes_reformatted, abundances=list_abundances_float, working_dir=self.folder_paths.stagging, - custom_global_settings={"forceRMoore": "yes", - "purgeSpinGroups": "yes", - "fudge": "0.7"} + custom_global_settings={ + "forceRMoore": "yes", + "purgeSpinGroups": "yes", + "fudge": "0.7", + }, ) self.files_paths.json_path = json_path notebook_logging.info(f"Configuration file created at: {json_path}") - endf_files = [f for f in os.listdir(self.folder_paths.stagging) if f.endswith('.par')] - notebook_logging.info(f"ENDf files found in working directory: {len(endf_files)} files") + endf_files = [ + f for f in os.listdir(self.folder_paths.stagging) if f.endswith(".par") + ] + notebook_logging.info( + f"ENDf files found in working directory: {len(endf_files)} files" + ) for f in sorted(endf_files): notebook_logging.info(f"\t- {f}") - display(HTML(f"Configuration file created at: {json_path}")) + display( + HTML( + f"Configuration file created at: {json_path}" + ) + ) notebook_logging.info("") def _setup_element_manager(self): - # from all the elements selected, let's find out the one with the most abundant isotope table_array = to_array(self.isotope_to_use_sheet) max_abundance = 0.0 @@ -526,92 +600,128 @@ def _setup_element_manager(self): if _float_abundance > max_abundance: max_abundance = _float_abundance most_abundant_isotope = _element - - notebook_logging.info(f"Most abundant isotope selected: {most_abundant_isotope}") - mass_number_str, element_symbol = most_abundant_isotope.split('-') + notebook_logging.info( + f"Most abundant isotope selected: {most_abundant_isotope}" + ) + + mass_number_str, element_symbol = most_abundant_isotope.split("-") my_periodic_table_element = getattr(periodictable, element_symbol) self.most_abundant_element_symbol = element_symbol - display(HTML(f"Most Abundant Element Selected: {element_symbol}")) + display( + HTML( + f"Most Abundant Element Selected: {element_symbol}" + ) + ) label_width = "160px" text_width = "80px" # mass number of the element selected - _label_left = widgets.HTML("
Mass number:
", - layout=widgets.Layout(width=label_width)) - self.mass_number_widget = widgets.IntText(value=int(mass_number_str), - disabled=False, - layout=widgets.Layout(width=text_width)) - _hori_layout_1 = widgets.HBox([_label_left, - self.mass_number_widget]) + _label_left = widgets.HTML( + "
Mass number:
", + layout=widgets.Layout(width=label_width), + ) + self.mass_number_widget = widgets.IntText( + value=int(mass_number_str), + disabled=False, + layout=widgets.Layout(width=text_width), + ) + _hori_layout_1 = widgets.HBox([_label_left, self.mass_number_widget]) display(_hori_layout_1) # density (g/cm^3) - - _label_left = widgets.HTML("
Density (g/cm3):
", - layout=widgets.Layout(width=label_width)) - self.density_widget = widgets.FloatText(value=my_periodic_table_element.density, - disabled=False, - layout=widgets.Layout(width=text_width)) + + _label_left = widgets.HTML( + "
Density (g/cm3):
", + layout=widgets.Layout(width=label_width), + ) + self.density_widget = widgets.FloatText( + value=my_periodic_table_element.density, + disabled=False, + layout=widgets.Layout(width=text_width), + ) _hori_layout_2 = widgets.HBox([_label_left, self.density_widget]) display(_hori_layout_2) # thickness (mm) - _label_left = widgets.HTML("
Thickness (mm):
", - layout=widgets.Layout(width=label_width)) - self.thickness_widget = widgets.FloatText(value=0.05, - disabled=False, - layout=widgets.Layout(width=text_width)) + _label_left = widgets.HTML( + "
Thickness (mm):
", + layout=widgets.Layout(width=label_width), + ) + self.thickness_widget = widgets.FloatText( + value=0.05, disabled=False, layout=widgets.Layout(width=text_width) + ) _hori_layout_3 = widgets.HBox([_label_left, self.thickness_widget]) display(_hori_layout_3) # atomic mass amu - _label_left = widgets.HTML("
Atomic mass (amu):
", - layout=widgets.Layout(width=label_width)) - self.atomic_mass_widget = widgets.FloatText(value=my_periodic_table_element.mass, - disabled=False, - layout=widgets.Layout(width=text_width)) + _label_left = widgets.HTML( + "
Atomic mass (amu):
", + layout=widgets.Layout(width=label_width), + ) + self.atomic_mass_widget = widgets.FloatText( + value=my_periodic_table_element.mass, + disabled=False, + layout=widgets.Layout(width=text_width), + ) _hori_layout_4 = widgets.HBox([_label_left, self.atomic_mass_widget]) - display(_hori_layout_4) + display(_hori_layout_4) # abundance (%) - _label_left = widgets.HTML("
Abundance (%):
", - layout=widgets.Layout(width=label_width)) - self.abundance_widget = widgets.FloatSlider(value=100.0, min=0, max=100, step=0.1, disabled=False) + _label_left = widgets.HTML( + "
Abundance (%):
", + layout=widgets.Layout(width=label_width), + ) + self.abundance_widget = widgets.FloatSlider( + value=100.0, min=0, max=100, step=0.1, disabled=False + ) _hori_layout_5 = widgets.HBox([_label_left, self.abundance_widget]) display(_hori_layout_5) # energy range (ev) - _label_left = widgets.HTML("
Energy range (eV):
", - layout=widgets.Layout(width=label_width)) - self.energy_range_widget = widgets.FloatRangeSlider(value=[1.0, 200.0], min=0, max=2000, step=0.1, disabled=False) + _label_left = widgets.HTML( + "
Energy range (eV):
", + layout=widgets.Layout(width=label_width), + ) + self.energy_range_widget = widgets.FloatRangeSlider( + value=[1.0, 200.0], min=0, max=2000, step=0.1, disabled=False + ) _hori_layout_6 = widgets.HBox([_label_left, self.energy_range_widget]) display(_hori_layout_6) # temperature (K) - _label_left = widgets.HTML("
Temperature (K):
", - layout=widgets.Layout(width=label_width)) - self.temperature_widget = widgets.FloatSlider(value=293.6, min=0, max=1000, step=0.1, disabled=False) + _label_left = widgets.HTML( + "
Temperature (K):
", + layout=widgets.Layout(width=label_width), + ) + self.temperature_widget = widgets.FloatSlider( + value=293.6, min=0, max=1000, step=0.1, disabled=False + ) _hori_layout_7 = widgets.HBox([_label_left, self.temperature_widget]) display(_hori_layout_7) # title - _label_left = widgets.HTML("
Title:
", - layout=widgets.Layout(width=label_width)) + _label_left = widgets.HTML( + "
Title:
", + layout=widgets.Layout(width=label_width), + ) o_get = Get(parent=self) - _element_name = o_get.full_name_of_element_from_abreviation(self.most_abundant_element_symbol) + _element_name = o_get.full_name_of_element_from_abreviation( + self.most_abundant_element_symbol + ) _ipts = self.ipts _instrument = self.instrument _title = f"{_element_name} multi-isotope transmission analysis - {_instrument} {_ipts}" - self.title_widget = widgets.Text(value=_title, - disabled=False, - layout=widgets.Layout(width="100%")) - _hori_layout_8 = widgets.HBox([_label_left, self.title_widget], layout=widgets.Layout(width="100%")) + self.title_widget = widgets.Text( + value=_title, disabled=False, layout=widgets.Layout(width="100%") + ) + _hori_layout_8 = widgets.HBox( + [_label_left, self.title_widget], layout=widgets.Layout(width="100%") + ) display(_hori_layout_8) def _create_multi_isotope_inp(self): - notebook_logging.info("Creating SAMMY input file for resonance fitting ...") _element = self.most_abundant_element_symbol mass_number = self.mass_number_widget.value @@ -626,18 +736,19 @@ def _create_multi_isotope_inp(self): title = self.title_widget.value material_props = { - 'element': _element, - 'mass_number': mass_number, - 'density_g_cm3': density, - 'thickness_mm': thickness, - 'atomic_mass_amu': atomic_mass, - 'abundance': abundance, - 'min_energy_eV': min_energy, - 'max_energy_eV': max_energy, - 'temperature_K': temperature} - - notebook_logging.info(f"calling InpManager.create_multi_isotope_inp ...") - notebook_logging.info(f"material_props:") + "element": _element, + "mass_number": mass_number, + "density_g_cm3": density, + "thickness_mm": thickness, + "atomic_mass_amu": atomic_mass, + "abundance": abundance, + "min_energy_eV": min_energy, + "max_energy_eV": max_energy, + "temperature_K": temperature, + } + + notebook_logging.info("calling InpManager.create_multi_isotope_inp ...") + notebook_logging.info("material_props:") display_dictionary_in_logging(material_props) inp_file = self.folder_paths.stagging / "hf_fitting.inp" self.files_paths.inp_file = inp_file @@ -653,13 +764,18 @@ def _create_multi_isotope_inp(self): ) notebook_logging.info(f"SAMMY input file created at: {inp_file}") - notebook_logging.info(f"") + notebook_logging.info("") def _sammy_files_multi_mode(self): - notebook_logging.info("Creating SAMMY files for multi-isotope resonance fitting ...") + notebook_logging.info( + "Creating SAMMY files for multi-isotope resonance fitting ..." + ) transmission_file_path = self.files_paths.transmission.name notebook_logging.info(f"\t{transmission_file_path = }") - data_file = self.folder_paths.twenty / f"{transmission_file_path.replace('.txt', '.twenty')}" + data_file = ( + self.folder_paths.twenty + / f"{transmission_file_path.replace('.txt', '.twenty')}" + ) notebook_logging.info(f"\t{data_file = }") notebook_logging.info(f"\tjson-config: {self.files_paths.json_path = }") @@ -667,11 +783,13 @@ def _sammy_files_multi_mode(self): input_file=self.files_paths.inp_file, json_config_file=self.files_paths.json_path, data_file=data_file, - endf_directory=self.folder_paths.stagging + endf_directory=self.folder_paths.stagging, ) self.files_paths.sammy_files_multi_mode = files notebook_logging.info(f"\t{files = }") - notebook_logging.info("SAMMY files for multi-isotope resonance fitting created.") + notebook_logging.info( + "SAMMY files for multi-isotope resonance fitting created." + ) notebook_logging.info("") def _local_sammy_config(self): @@ -683,9 +801,11 @@ def _local_sammy_config(self): notebook_logging.info(f"\t{working_directory = }") notebook_logging.info(f"\t{output_directory = }") - config = LocalSammyConfig(sammy_executable=sammy_executable, - working_dir=working_directory, - output_dir=output_directory) + config = LocalSammyConfig( + sammy_executable=sammy_executable, + working_dir=working_directory, + output_dir=output_directory, + ) notebook_logging.info(f"\t{config = }") self.runner = LocalSammyRunner(config=config) @@ -693,22 +813,32 @@ def _local_sammy_config(self): notebook_logging.info("") def _multi_isotope_sammy_execution(self): - notebook_logging.info("Starting multi-isotope SAMMY resonance fitting execution ...") + notebook_logging.info( + "Starting multi-isotope SAMMY resonance fitting execution ..." + ) notebook_logging.info(f"\t{self.files_paths.sammy_files_multi_mode = }") self.runner.prepare_environment(self.files_paths.sammy_files_multi_mode) result = self.runner.execute_sammy(self.files_paths.sammy_files_multi_mode) notebook_logging.info(f"\tresult: {result =}") - + notebook_logging.info("SAMMY resonance fitting execution completed.") notebook_logging.info(f"Execution status: {result.success}") notebook_logging.info(f"Runtime: {result.runtime_seconds} seconds") - + if result.error_message: notebook_logging.error(f"Error message: {result.error_message}") - display(HTML(f"Error during SAMMY execution: {result.error_message}") ) + display( + HTML( + f"Error during SAMMY execution: {result.error_message}" + ) + ) else: - display(HTML(f"SAMMY execution completed successfully!") ) - + display( + HTML( + f"SAMMY execution completed successfully!" + ) + ) + self.runner.collect_outputs(result=result) self.runner.cleanup() @@ -721,20 +851,33 @@ def _results_analysis(self): lst_file_path = self.folder_paths.sammy_output / "SAMMY.LST" self.results_manager = ResultsManager( - lpt_file_path=lpt_file_path, - lst_file_path=lst_file_path + lpt_file_path=lpt_file_path, lst_file_path=lst_file_path ) data = self.results_manager.get_data() # self.results_manager - notebook_logging.info(f"\t energy range: {data.energy.min():.3e} eV to {data.energy.max():.3e} eV") + notebook_logging.info( + f"\t energy range: {data.energy.min():.3e} eV to {data.energy.max():.3e} eV" + ) notebook_logging.info(f"\t data points: {len(data.energy)}") - display(HTML(f"Results summary:")) - display(HTML(f"  Energy range: {data.energy.min():.3e} eV to {data.energy.max():.3e} eV")) - display(HTML(f"  Data points: {len(data.energy)}")) + display( + HTML( + f"Results summary:" + ) + ) + display( + HTML( + f"  Energy range: {data.energy.min():.3e} eV to {data.energy.max():.3e} eV" + ) + ) + display( + HTML( + f"  Data points: {len(data.energy)}" + ) + ) fig = self.results_manager.plot_transmission( figsize=(12, 8), @@ -753,52 +896,68 @@ def _results_analysis(self): notebook_logging.info("") def _multi_isotope_fitting_quality_metrics(self): - results_manager = self.results_manager - + if results_manager.run_results.fit_results: print(f"Fit iterations: {len(results_manager.run_results.fit_results)}") - + for i, fit_result in enumerate(results_manager.run_results.fit_results): - print(f"\nIteration {i+1}:") - + print(f"\nIteration {i + 1}:") + chi_sq = fit_result.get_chi_squared_results() if chi_sq.chi_squared is not None: print(f" Chi-squared: {chi_sq.chi_squared:.4f}") print(f" Data points: {chi_sq.dof}") print(f" Reduced chi-squared: {chi_sq.reduced_chi_squared:.6f}") - + physics = fit_result.get_physics_data() - if hasattr(physics, 'broadening_parameters'): + if hasattr(physics, "broadening_parameters"): broadening = physics.broadening_parameters - if hasattr(broadening, 'thick') and broadening.thick is not None: + if hasattr(broadening, "thick") and broadening.thick is not None: print(f" Number density: {broadening.thick:.6e} atoms/barn-cm") print(f" Temperature: {broadening.temp:.2f} K") - + # Multi-isotope abundances nuclear = fit_result.get_nuclear_data() - if hasattr(nuclear, 'isotopes') and nuclear.isotopes: + if hasattr(nuclear, "isotopes") and nuclear.isotopes: print("\n Isotopic abundances:") - hf_isotopes = ["Hf-174", "Hf-176", "Hf-177", "Hf-178", "Hf-179", "Hf-180"] - natural_abundances = [0.0016, 0.0526, 0.1860, 0.2728, 0.1362, 0.3508] - + hf_isotopes = [ + "Hf-174", + "Hf-176", + "Hf-177", + "Hf-178", + "Hf-179", + "Hf-180", + ] + natural_abundances = [ + 0.0016, + 0.0526, + 0.1860, + 0.2728, + 0.1362, + 0.3508, + ] + for j, isotope in enumerate(nuclear.isotopes): - if j < len(hf_isotopes) and hasattr(isotope, 'abundance'): + if j < len(hf_isotopes) and hasattr(isotope, "abundance"): fitted = isotope.abundance natural = natural_abundances[j] ratio = fitted / natural if natural > 0 else 0 - print(f" {hf_isotopes[j]}: fitted={fitted:.6f}, natural={natural:.4f}, ratio={ratio:.3f}") + print( + f" {hf_isotopes[j]}: fitted={fitted:.6f}, natural={natural:.4f}, ratio={ratio:.3f}" + ) # Final results if len(results_manager.run_results.fit_results) > 0: final_fit = results_manager.run_results.fit_results[-1] final_chi = final_fit.get_chi_squared_results() final_phys = final_fit.get_physics_data() - + print("\nFinal multi-isotope fit results:") if final_chi.reduced_chi_squared: print(f" Reduced chi-squared: {final_chi.reduced_chi_squared:.6f}") - if hasattr(final_phys, 'broadening_parameters'): - if hasattr(final_phys.broadening_parameters, 'thick'): - print(f" Number density: {final_phys.broadening_parameters.thick:.6e} atoms/barn-cm") - \ No newline at end of file + if hasattr(final_phys, "broadening_parameters"): + if hasattr(final_phys.broadening_parameters, "thick"): + print( + f" Number density: {final_phys.broadening_parameters.thick:.6e} atoms/barn-cm" + ) diff --git a/notebooks/__code/resonance_fitting/units.py b/notebooks/__code/resonance_fitting/units.py index 8acb982f1..43c1bc965 100644 --- a/notebooks/__code/resonance_fitting/units.py +++ b/notebooks/__code/resonance_fitting/units.py @@ -135,7 +135,9 @@ def convert_to_cross_section(from_unit, to_unit): return conversion_factors[from_unit] / conversion_factors[to_unit] -def convert_from_wavelength_to_energy_ev(wavelength, unit_from=DistanceUnitOptions.angstrom): +def convert_from_wavelength_to_energy_ev( + wavelength, unit_from=DistanceUnitOptions.angstrom +): """Convert wavelength to energy based on the given units. Args: @@ -177,15 +179,21 @@ def convert_array_from_time_to_lambda( np.ndarray: Array of wavelength values. """ time_array_s = time_array * convert_time_units(time_unit, TimeUnitOptions.s) - detector_offset_s = detector_offset * convert_time_units(detector_offset_unit, TimeUnitOptions.s) + detector_offset_s = detector_offset * convert_time_units( + detector_offset_unit, TimeUnitOptions.s + ) distance_source_detector_m = distance_source_detector * convert_distance_units( distance_source_detector_unit, DistanceUnitOptions.m ) h_over_mn = h / m_n - lambda_m = h_over_mn * (time_array_s + detector_offset_s) / distance_source_detector_m + lambda_m = ( + h_over_mn * (time_array_s + detector_offset_s) / distance_source_detector_m + ) - lambda_converted = lambda_m * convert_distance_units(DistanceUnitOptions.m, lambda_unit) + lambda_converted = lambda_m * convert_distance_units( + DistanceUnitOptions.m, lambda_unit + ) return lambda_converted @@ -225,13 +233,22 @@ def convert_array_from_time_to_energy( detector_units_factor = convert_time_units(detector_offset_unit, TimeUnitOptions.s) detector_offset = detector_units_factor * detector_offset - distance_source_detector_factor = convert_distance_units(distance_source_detector_unit, DistanceUnitOptions.m) - distance_source_detector_m = distance_source_detector * distance_source_detector_factor + distance_source_detector_factor = convert_distance_units( + distance_source_detector_unit, DistanceUnitOptions.m + ) + distance_source_detector_m = ( + distance_source_detector * distance_source_detector_factor + ) # Calculate the energy in eV using the formula E_ev = 1/2 m_n (L/t_tof)^2 / electron_volt full_time_array_s = time_array_s + detector_offset - energy_array_ev = 0.5 * m_n * (distance_source_detector_m / full_time_array_s) ** 2 / electron_volt + energy_array_ev = ( + 0.5 + * m_n + * (distance_source_detector_m / full_time_array_s) ** 2 + / electron_volt + ) energy_array_factor = convert_to_energy(EnergyUnitOptions.eV, energy_unit) energy_array = energy_array_ev * energy_array_factor diff --git a/notebooks/__code/roi_selection_ui.py b/notebooks/__code/roi_selection_ui.py index 492ae61ae..642bd8866 100755 --- a/notebooks/__code/roi_selection_ui.py +++ b/notebooks/__code/roi_selection_ui.py @@ -7,7 +7,13 @@ from IPython.display import HTML, display from NeuNorm.normalization import Normalization from qtpy import QtGui -from qtpy.QtWidgets import QMainWindow, QProgressBar, QTableWidgetItem, QTableWidgetSelectionRange, QVBoxLayout +from qtpy.QtWidgets import ( + QMainWindow, + QProgressBar, + QTableWidgetItem, + QTableWidgetSelectionRange, + QVBoxLayout, +) from __code import load_ui from __code.config import ( @@ -65,7 +71,9 @@ def __init__( self.list_of_files = list_of_files if percentage_of_data_to_use is None: - percentage_of_data_to_use = percentage_of_images_to_use_for_roi_selection + percentage_of_data_to_use = ( + percentage_of_images_to_use_for_roi_selection + ) self.percentage_of_data_to_use = percentage_of_data_to_use # method called when leaving the application, if any @@ -73,7 +81,8 @@ def __init__( super(QMainWindow, self).__init__(parent) ui_full_path = os.path.join( - os.path.dirname(os.path.dirname(__file__)), os.path.join("ui", "ui_roi_selection.ui") + os.path.dirname(os.path.dirname(__file__)), + os.path.join("ui", "ui_roi_selection.ui"), ) self.ui = load_ui(ui_full_path, baseinstance=self) @@ -134,7 +143,9 @@ def __built_html_table_row_3_columns(self, name, nbr, height, width): def recap(self): """Display nbr of files loaded and size. This can be used to figure why a normalization failed""" - [nbr_sample, height_sample, width_sample] = self.__get_recap(self.o_norm.data["sample"]["data"]) + [nbr_sample, height_sample, width_sample] = self.__get_recap( + self.o_norm.data["sample"]["data"] + ) [nbr_ob, height_ob, width_ob] = self.__get_recap(self.o_norm.data["ob"]["data"]) [nbr_df, height_df, width_df] = self.__get_recap(self.o_norm.data["df"]["data"]) @@ -142,7 +153,9 @@ def recap(self): '" ) - html += self.__built_html_table_row_3_columns("sample", nbr_sample, height_sample, width_sample) + html += self.__built_html_table_row_3_columns( + "sample", nbr_sample, height_sample, width_sample + ) html += self.__built_html_table_row_3_columns("ob", nbr_ob, height_ob, width_ob) html += self.__built_html_table_row_3_columns("df", nbr_df, height_df, width_df) html += "
TypeNumber' + "Size (height*width)
" @@ -169,7 +182,9 @@ def integrate_images(self): random_list = random.sample(range(0, nbr_files), nbr_files_to_use) if self.o_norm: - list_data_to_use = [self.o_norm.data["sample"]["data"][_index] for _index in random_list] + list_data_to_use = [ + self.o_norm.data["sample"]["data"][_index] for _index in random_list + ] else: o_norm = Normalization() list_of_files = np.array(self.list_of_files) @@ -367,7 +382,9 @@ def roi_manually_moved(self): _roi = list_roi[_row] roi_id = _roi["id"] - region = roi_id.getArraySlice(self.integrated_image, self.ui.image_view.imageItem) + region = roi_id.getArraySlice( + self.integrated_image, self.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop @@ -441,7 +458,9 @@ def add_roi_button_clicked(self): width_int = np.abs(x0_int - int(_x1)) height_int = np.abs(y0_int - int(_y1)) - _roi_id = self.init_roi(x0=x0_int, y0=y0_int, width=width_int, height=height_int) + _roi_id = self.init_roi( + x0=x0_int, y0=y0_int, width=width_int, height=height_int + ) _roi["id"] = _roi_id list_roi[_row] = _roi diff --git a/notebooks/__code/roi_statistics_vs_stack/display.py b/notebooks/__code/roi_statistics_vs_stack/display.py index 2111f2e43..36b8c1bf7 100755 --- a/notebooks/__code/roi_statistics_vs_stack/display.py +++ b/notebooks/__code/roi_statistics_vs_stack/display.py @@ -26,7 +26,8 @@ def update_image_view(self, slider_value=0): if not first_update: _histo_widget.setLevels( - self.parent.image_view_histogram_level[0], self.parent.image_view_histogram_level[1] + self.parent.image_view_histogram_level[0], + self.parent.image_view_histogram_level[1], ) def get_x_axis(self): @@ -43,27 +44,37 @@ def update_statistics_plot(self): nbr_plot = 0 if self.parent.ui.mean_checkBox.isChecked(): y_axis_mean = self.parent.y_axis["mean"] - self.parent.statistics_plot.axes.plot(x_axis, y_axis_mean, "bv", label="mean") + self.parent.statistics_plot.axes.plot( + x_axis, y_axis_mean, "bv", label="mean" + ) nbr_plot += 1 if self.parent.ui.min_checkBox.isChecked(): y_axis_mean = self.parent.y_axis["min"] - self.parent.statistics_plot.axes.plot(x_axis, y_axis_mean, "r*", label="min") + self.parent.statistics_plot.axes.plot( + x_axis, y_axis_mean, "r*", label="min" + ) nbr_plot += 1 if self.parent.ui.max_checkBox.isChecked(): y_axis_mean = self.parent.y_axis["max"] - self.parent.statistics_plot.axes.plot(x_axis, y_axis_mean, "r+", label="max") + self.parent.statistics_plot.axes.plot( + x_axis, y_axis_mean, "r+", label="max" + ) nbr_plot += 1 if self.parent.ui.median_checkBox.isChecked(): y_axis_mean = self.parent.y_axis["median"] - self.parent.statistics_plot.axes.plot(x_axis, y_axis_mean, "gp", label="median") + self.parent.statistics_plot.axes.plot( + x_axis, y_axis_mean, "gp", label="median" + ) nbr_plot += 1 if self.parent.ui.std_checkBox.isChecked(): y_axis_mean = self.parent.y_axis["std"] - self.parent.statistics_plot.axes.plot(x_axis, y_axis_mean, "cx", label="std") + self.parent.statistics_plot.axes.plot( + x_axis, y_axis_mean, "cx", label="std" + ) nbr_plot += 1 if nbr_plot > 0: diff --git a/notebooks/__code/roi_statistics_vs_stack/event_handler.py b/notebooks/__code/roi_statistics_vs_stack/event_handler.py index f7b08ada1..e6c0d9761 100755 --- a/notebooks/__code/roi_statistics_vs_stack/event_handler.py +++ b/notebooks/__code/roi_statistics_vs_stack/event_handler.py @@ -10,7 +10,9 @@ def __init__(self, parent=None): self.parent = parent def recalculate_table(self): - region = self.parent.ui.roi.getArraySlice(self.parent.live_image, self.parent.ui.image_view.imageItem) + region = self.parent.ui.roi.getArraySlice( + self.parent.live_image, self.parent.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop - 1 @@ -62,10 +64,24 @@ def update_table(self): o_table = TableHandler(table_ui=self.parent.ui.tableWidget) for _row in data_dict.keys(): _entry = data_dict[_row] - o_table.insert_item(row=_row, column=StatisticsColumnIndex.min, value=_entry["min"], editable=False) - o_table.insert_item(row=_row, column=StatisticsColumnIndex.max, value=_entry["max"], editable=False) o_table.insert_item( - row=_row, column=StatisticsColumnIndex.mean, value=_entry["mean"], format_str="{:0.2f}", editable=False + row=_row, + column=StatisticsColumnIndex.min, + value=_entry["min"], + editable=False, + ) + o_table.insert_item( + row=_row, + column=StatisticsColumnIndex.max, + value=_entry["max"], + editable=False, + ) + o_table.insert_item( + row=_row, + column=StatisticsColumnIndex.mean, + value=_entry["mean"], + format_str="{:0.2f}", + editable=False, ) o_table.insert_item( row=_row, @@ -75,7 +91,11 @@ def update_table(self): editable=False, ) o_table.insert_item( - row=_row, column=StatisticsColumnIndex.std, value=_entry["std"], format_str="{:0.2f}", editable=False + row=_row, + column=StatisticsColumnIndex.std, + value=_entry["std"], + format_str="{:0.2f}", + editable=False, ) def reset_table_plot(self): diff --git a/notebooks/__code/roi_statistics_vs_stack/export.py b/notebooks/__code/roi_statistics_vs_stack/export.py index 62373bd8c..2430f1cbb 100755 --- a/notebooks/__code/roi_statistics_vs_stack/export.py +++ b/notebooks/__code/roi_statistics_vs_stack/export.py @@ -14,12 +14,16 @@ def __init__(self, parent=None): def export(self): base_folder = os.path.basename(self.parent.working_folder) _export_folder = QFileDialog.getExistingDirectory( - self.parent, directory=os.path.dirname(base_folder), caption="Select Output Folder" + self.parent, + directory=os.path.dirname(base_folder), + caption="Select Output Folder", ) if _export_folder: output_base_file_name = str(base_folder) + "_statistics.txt" - full_output_base_file_name = os.path.join(_export_folder, output_base_file_name) + full_output_base_file_name = os.path.join( + _export_folder, output_base_file_name + ) x_axis = self.parent.x_axis time_offset_array = x_axis["time_offset"] @@ -37,10 +41,13 @@ def export(self): metadata = ["# Statistics created with roi_statistics_vs_stack notebook"] metadata.append(f"# working dir: {self.parent.working_folder}") metadata.append( - f"# roi selected: x0:{roi['x0']}, y0:{roi['y0']}, " f"width:{roi['width']}, height:{roi['height']}" + f"# roi selected: x0:{roi['x0']}, y0:{roi['y0']}, " + f"width:{roi['width']}, height:{roi['height']}" ) metadata.append("#") - metadata.append("#file index, file name, time offset (s), min, max, mean, median, standard deviation") + metadata.append( + "#file index, file name, time offset (s), min, max, mean, median, standard deviation" + ) data = [] for _row in np.arange(len(list_of_images)): @@ -53,12 +60,26 @@ def export(self): _median = median_array[_row] _std = std_array[_row] - _row = [_file_index, _file_name, _time_offset, _min, _max, _mean, _median, _std] + _row = [ + _file_index, + _file_name, + _time_offset, + _min, + _max, + _mean, + _median, + _std, + ] _row_str = [str(_entry) for _entry in _row] _row_str_formatted = ",".join(_row_str) data.append(_row_str_formatted) - make_ascii_file(metadata=metadata, data=data, output_file_name=full_output_base_file_name, dim="1d") + make_ascii_file( + metadata=metadata, + data=data, + output_file_name=full_output_base_file_name, + dim="1d", + ) show_status_message( parent=self.parent, diff --git a/notebooks/__code/roi_statistics_vs_stack/initialization.py b/notebooks/__code/roi_statistics_vs_stack/initialization.py index ee192c665..e101bb7fc 100755 --- a/notebooks/__code/roi_statistics_vs_stack/initialization.py +++ b/notebooks/__code/roi_statistics_vs_stack/initialization.py @@ -36,7 +36,9 @@ def pyqtgraph(self): y0 = roi["y0"] width = roi["width"] height = roi["height"] - self.parent.ui.roi = pg.ROI([x0, y0], [width, height], pen=(62, 13, 244), scaleSnap=True) # blue + self.parent.ui.roi = pg.ROI( + [x0, y0], [width, height], pen=(62, 13, 244), scaleSnap=True + ) # blue self.parent.ui.roi.addScaleHandle([1, 1], [0, 0]) self.parent.ui.image_view.addItem(self.parent.ui.roi) self.parent.ui.roi.sigRegionChanged.connect(self.parent.roi_changed) @@ -63,7 +65,12 @@ def table(self): for _row in np.arange(len(self.parent.list_of_images)): o_table.insert_empty_row(_row) short_file_name = os.path.basename(self.parent.list_of_images[_row]) - o_table.insert_item(row=_row, column=StatisticsColumnIndex.file_name, value=short_file_name, editable=False) + o_table.insert_item( + row=_row, + column=StatisticsColumnIndex.file_name, + value=short_file_name, + editable=False, + ) o_table.insert_item( row=_row, column=StatisticsColumnIndex.time_offset, @@ -95,4 +102,6 @@ def _matplotlib(parent=None, widget=None): widget.setLayout(layout) return sc - self.parent.statistics_plot = _matplotlib(parent=self.parent, widget=self.parent.ui.plot_widget) + self.parent.statistics_plot = _matplotlib( + parent=self.parent, widget=self.parent.ui.plot_widget + ) diff --git a/notebooks/__code/roi_statistics_vs_stack/load.py b/notebooks/__code/roi_statistics_vs_stack/load.py index 42adfaa72..55253882a 100755 --- a/notebooks/__code/roi_statistics_vs_stack/load.py +++ b/notebooks/__code/roi_statistics_vs_stack/load.py @@ -29,7 +29,9 @@ def data(self): o_norm = Normalization() o_norm.load(file=_file, auto_gamma_filter=False, manual_gamma_filter=False) data = np.squeeze(o_norm.data["sample"]["data"][0]) - time_stamp = MetadataHandler.get_time_stamp(file_name=_file, ext=file_extension) + time_stamp = MetadataHandler.get_time_stamp( + file_name=_file, ext=file_extension + ) if acquisition_time_of_first_image == -1: acquisition_time_of_first_image = time_stamp time_stamp = 0 diff --git a/notebooks/__code/roi_statistics_vs_stack/main.py b/notebooks/__code/roi_statistics_vs_stack/main.py index 0dcc9d80f..36bd5574a 100755 --- a/notebooks/__code/roi_statistics_vs_stack/main.py +++ b/notebooks/__code/roi_statistics_vs_stack/main.py @@ -16,13 +16,17 @@ class FileHandler(FileFolderBrowser): def __init__(self, working_dir=""): - super(FileHandler, self).__init__(working_dir=working_dir, next_function=self.display_status) + super(FileHandler, self).__init__( + working_dir=working_dir, next_function=self.display_status + ) def get_list_of_files(self): return self.list_images_ui.selected def select_folder(self): - self.select_input_folder(instruction="Select folder containing images to process ...") + self.select_input_folder( + instruction="Select folder containing images to process ..." + ) def display_status(self, folder): o_list = ListMostDominantExtension(working_dir=folder) @@ -30,7 +34,13 @@ def display_status(self, folder): result = o_list.get_files_of_selected_ext() self.list_of_images = result.list_files nbr_images = str(len(self.list_of_images)) - display(HTML('You have selected ' + nbr_images + " images ")) + display( + HTML( + 'You have selected ' + + nbr_images + + " images " + ) + ) class ImageWindow(QMainWindow): diff --git a/notebooks/__code/roi_statistics_vs_stack/table.py b/notebooks/__code/roi_statistics_vs_stack/table.py index 9e0033aed..03e9ad0f0 100755 --- a/notebooks/__code/roi_statistics_vs_stack/table.py +++ b/notebooks/__code/roi_statistics_vs_stack/table.py @@ -13,8 +13,33 @@ def reset(self): reset_value = "NaN" for _row in data_dict.keys(): _entry = data_dict[_row] - o_table.insert_item(row=_row, column=StatisticsColumnIndex.min, value=reset_value, editable=False) - o_table.insert_item(row=_row, column=StatisticsColumnIndex.max, value=reset_value, editable=False) - o_table.insert_item(row=_row, column=StatisticsColumnIndex.mean, value=reset_value, editable=False) - o_table.insert_item(row=_row, column=StatisticsColumnIndex.median, value=reset_value, editable=False) - o_table.insert_item(row=_row, column=StatisticsColumnIndex.std, value=reset_value, editable=False) + o_table.insert_item( + row=_row, + column=StatisticsColumnIndex.min, + value=reset_value, + editable=False, + ) + o_table.insert_item( + row=_row, + column=StatisticsColumnIndex.max, + value=reset_value, + editable=False, + ) + o_table.insert_item( + row=_row, + column=StatisticsColumnIndex.mean, + value=reset_value, + editable=False, + ) + o_table.insert_item( + row=_row, + column=StatisticsColumnIndex.median, + value=reset_value, + editable=False, + ) + o_table.insert_item( + row=_row, + column=StatisticsColumnIndex.std, + value=reset_value, + editable=False, + ) diff --git a/notebooks/__code/rotate_and_crop_images/rotate_and_crop_images.py b/notebooks/__code/rotate_and_crop_images/rotate_and_crop_images.py index c728f7fbd..8ac496974 100755 --- a/notebooks/__code/rotate_and_crop_images/rotate_and_crop_images.py +++ b/notebooks/__code/rotate_and_crop_images/rotate_and_crop_images.py @@ -69,7 +69,8 @@ def __init__(self, parent=None, o_load=None): QMainWindow.__init__(self, parent=parent) ui_full_path = os.path.join( - os.path.dirname(os.path.dirname(os.path.dirname(__file__))), os.path.join("ui", "ui_rotate_and_crop.ui") + os.path.dirname(os.path.dirname(os.path.dirname(__file__))), + os.path.join("ui", "ui_rotate_and_crop.ui"), ) self.ui = load_ui(ui_full_path, baseinstance=self) self.init_statusbar() @@ -150,7 +151,9 @@ def get_selected_image(self, file_index=None): file_index = self.ui.file_index_slider.value() if self.data_dict[file_index][DataDictKeys.data] is None: - data = RotateAndCropImages.load_data(filename=self.data_dict[file_index][DataDictKeys.filename]) + data = RotateAndCropImages.load_data( + filename=self.data_dict[file_index][DataDictKeys.filename] + ) self.data_dict[file_index][DataDictKeys.data] = data else: @@ -222,12 +225,20 @@ def display_grid(self): line_color = (255, 0, 0, 155, 0.2) lines = np.array( [line_color for n in np.arange(len(pos))], - dtype=[("red", np.ubyte), ("green", np.ubyte), ("blue", np.ubyte), ("alpha", np.ubyte), ("width", float)], + dtype=[ + ("red", np.ubyte), + ("green", np.ubyte), + ("blue", np.ubyte), + ("alpha", np.ubyte), + ("width", float), + ], ) line_view_binning = pg.GraphItem() self.ui.image_view.addItem(line_view_binning) - line_view_binning.setData(pos=pos, adj=adj, pen=lines, symbol=None, pxMode=False) + line_view_binning.setData( + pos=pos, adj=adj, pen=lines, symbol=None, pxMode=False + ) self.line_view_binning = line_view_binning @@ -244,7 +255,9 @@ def get_or_load_data(self, file_index=0): array. If it's already there, just return the array """ if self.data_dict[file_index][DataDictKeys.data] is None: - data = RotateAndCropImages.load_data(self.data_dict[file_index][DataDictKeys.filename]) + data = RotateAndCropImages.load_data( + self.data_dict[file_index][DataDictKeys.filename] + ) self.data_dict[file_index][DataDictKeys.data] = data return data else: @@ -267,7 +280,9 @@ def rotation_value_changed(self): def get_crop_region(self): data = self.get_selected_image() # data = self.live_data - region = self.roi.getArraySlice(np.transpose(data), self.ui.image_view.imageItem) + region = self.roi.getArraySlice( + np.transpose(data), self.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop - 1 @@ -295,7 +310,9 @@ def rotate_and_crop_all(self): rotated_data = data[y0:y1, x0:x1] self.rotated_data_dict[file_index] = { - DataDictKeys.filename: self.data_dict[file_index][DataDictKeys.filename], + DataDictKeys.filename: self.data_dict[file_index][ + DataDictKeys.filename + ], DataDictKeys.data: rotated_data, } @@ -338,7 +355,10 @@ def select_folder(self): ) self.output_folder_ui = fileselector.FileSelectorPanel( - instruction="Select Output Folder ...", start_dir=self.working_dir, type="directory", next=self.export + instruction="Select Output Folder ...", + start_dir=self.working_dir, + type="directory", + next=self.export, ) self.output_folder_ui.show() @@ -369,4 +389,10 @@ def export(self, output_folder): w.close() display(HTML("")) - display(HTML('Files created in ' + full_output_folder + "")) + display( + HTML( + 'Files created in ' + + full_output_folder + + "" + ) + ) diff --git a/notebooks/__code/sans/extract.py b/notebooks/__code/sans/extract.py index 4d38f63d7..523f8dab4 100755 --- a/notebooks/__code/sans/extract.py +++ b/notebooks/__code/sans/extract.py @@ -15,7 +15,10 @@ from __code.sans.sans_config import biosans_parameters, gpsans_parameters STARTING_ENTRIES = ["entry", "DASlogs"] -LIST_SANS_INSTRUMENTS = {"GP-SANS (CG2)": {"unix_name": "CG2"}, "BIO-SANS (CG3)": {"unix_name": "CG3"}} +LIST_SANS_INSTRUMENTS = { + "GP-SANS (CG2)": {"unix_name": "CG2"}, + "BIO-SANS (CG3)": {"unix_name": "CG3"}, +} class Initializer: @@ -23,8 +26,13 @@ def select_instrument(self): list_instruments = list(LIST_SANS_INSTRUMENTS.keys()) instrument_ui = widgets.HBox( [ - widgets.Label("Select your instrument", layout=widgets.Layout(width="15%")), - widgets.Select(options=list_instruments, layout=widgets.Layout(width="30%", height="50px")), + widgets.Label( + "Select your instrument", layout=widgets.Layout(width="15%") + ), + widgets.Select( + options=list_instruments, + layout=widgets.Layout(width="30%", height="50px"), + ), ] ) display(instrument_ui) @@ -78,19 +86,30 @@ def display_metadata(self, list_nexus): self.list_nexus = list_nexus self.list_keys = self.retrieve_left_widget_list_keys() - self.list_values = self.retrieve_right_widget_list_keys(left_widget_key_selected=list(self.list_keys)[0]) + self.list_values = self.retrieve_right_widget_list_keys( + left_widget_key_selected=list(self.list_keys)[0] + ) # search box - search_box = widgets.HBox([widgets.Label("Search:"), widgets.Text("", layout=widgets.Layout(width="30%"))]) + search_box = widgets.HBox( + [ + widgets.Label("Search:"), + widgets.Text("", layout=widgets.Layout(width="30%")), + ] + ) # search_text_widget = search_box.children[1] # search_text_widget.observe(self.search_text_changed, names='value') # list of keys hori_box = widgets.HBox( [ - widgets.Select(options=self.list_keys, layout=widgets.Layout(width="400px", height=self.widget_height)), + widgets.Select( + options=self.list_keys, + layout=widgets.Layout(width="400px", height=self.widget_height), + ), widgets.SelectMultiple( - options=self.list_values, layout=widgets.Layout(width="400px", height=self.widget_height) + options=self.list_values, + layout=widgets.Layout(width="400px", height=self.widget_height), ), ], ) @@ -99,7 +118,11 @@ def display_metadata(self, list_nexus): self.left_widget_ui.observe(self.left_widget_changed, names="value") self.right_widget_ui.observe(self.right_widget_changed, names="value") - display(widgets.Label("Command + Click: to select more than 1 element in the right widget")) + display( + widgets.Label( + "Command + Click: to select more than 1 element in the right widget" + ) + ) def left_widget_changed(self, new_value): value_selected = new_value["new"] @@ -170,10 +193,16 @@ def reformat_dict(full_dict): return new_full_dict def extract_all_in_one(self, output_folder): - display(HTML('Work in progress ... ')) + display( + HTML( + 'Work in progress ... ' + ) + ) self.output_folder_ui.shortcut_buttons.close() - output_file_name = Extract.create_output_file_name(output_folder=output_folder, nbr_nexus=len(self.list_nexus)) + output_file_name = Extract.create_output_file_name( + output_folder=output_folder, nbr_nexus=len(self.list_nexus) + ) full_list_selected = self.full_list_selected # get list of path @@ -194,7 +223,9 @@ def extract_all_in_one(self, output_folder): if _index == 0: label_of_columns.append("nexus name") - reduction_log_dict = Extract.get_entry_value(nexus_file_name=_nexus, list_entry_path=list_entry_path) + reduction_log_dict = Extract.get_entry_value( + nexus_file_name=_nexus, list_entry_path=list_entry_path + ) for _key in reduction_log_dict.keys(): _value = reduction_log_dict[_key] # print(f"-> _key:{_key}: {_value}") diff --git a/notebooks/__code/sans/sans_config.py b/notebooks/__code/sans/sans_config.py index 71f26c5cb..5aaef869e 100755 --- a/notebooks/__code/sans/sans_config.py +++ b/notebooks/__code/sans/sans_config.py @@ -186,7 +186,12 @@ gpsans_parameters = { "special_parameters": { - "list": ["sample_transmission", "background_transmission", "beam_center", "transmission_radius_used (mm)"], + "list": [ + "sample_transmission", + "background_transmission", + "beam_center", + "transmission_radius_used (mm)", + ], "path": ["reduction_information", "special_parameters"], }, "sample_logs": { diff --git a/notebooks/__code/scale_overlapping_images.py b/notebooks/__code/scale_overlapping_images.py index 6907276fd..7ef2451f9 100755 --- a/notebooks/__code/scale_overlapping_images.py +++ b/notebooks/__code/scale_overlapping_images.py @@ -141,7 +141,7 @@ def display_profiles(self): for _row in np.arange(nbr_row): [x_axis, profile] = self.get_profile(image=image, profile_roi_row=_row) - _label = f" Profile #{_row+1}" + _label = f" Profile #{_row + 1}" _color = list_rgb_profile_color[_row] self.ui.profile_view.plot(x_axis, profile, name=_label, pen=_color) @@ -151,7 +151,9 @@ def update_all_plots(self): nbr_profile = len(list_index_profile_selected) nbr_file_selected = len(list_index_file_selected) color = Color() - list_rgb_profile_color = color.get_list_rgb(nbr_color=(nbr_profile * nbr_file_selected)) + list_rgb_profile_color = color.get_list_rgb( + nbr_color=(nbr_profile * nbr_file_selected) + ) self.ui.all_plots_view.clear() if nbr_profile == 0: return @@ -165,10 +167,16 @@ def update_all_plots(self): for _color_index_file, _index_file in enumerate(list_index_file_selected): _data = self.data_dict["data"][_index_file] - for _color_index_profile, _index_profile in enumerate(list_index_profile_selected): + for _color_index_profile, _index_profile in enumerate( + list_index_profile_selected + ): legend = f"File #{_index_file} - Profile #{_index_profile}" - _color = list_rgb_profile_color[_color_index_file + _color_index_profile * nbr_file_selected] - [x_axis, y_axis] = self.get_profile(image=np.transpose(_data), profile_roi_row=_index_profile) + _color = list_rgb_profile_color[ + _color_index_file + _color_index_profile * nbr_file_selected + ] + [x_axis, y_axis] = self.get_profile( + image=np.transpose(_data), profile_roi_row=_index_profile + ) self.ui.all_plots_view.plot(x_axis, y_axis, name=legend, pen=_color) def display_image(self, recalculate_image=False): @@ -237,7 +245,9 @@ def is_row_enabled(self, row=-1): def update_guide_table_using_guide_rois(self): for _row, _roi in enumerate(self.list_guide_pyqt_roi): if self.is_row_enabled(row=_row): - region = _roi.getArraySlice(self.live_image, self.ui.image_view.imageItem) + region = _roi.getArraySlice( + self.live_image, self.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop @@ -310,7 +320,9 @@ def rename_all_plots_profiles_table(self): """rename all the profile name""" nbr_row = self.ui.tableWidget.rowCount() for _row in np.arange(nbr_row): - self.ui.all_plots_profiles_table.item(_row, 0).setText(f"Profile # {_row+1}") + self.ui.all_plots_profiles_table.item(_row, 0).setText( + f"Profile # {_row + 1}" + ) # setter def set_item_all_plots_profile_table(self, row=0): @@ -319,12 +331,16 @@ def set_item_all_plots_profile_table(self, row=0): self.ui.all_plots_profiles_table.setItem(row, 0, item) def set_item_profile_table(self, row=0): - spacerItem_left = QtGui.QSpacerItem(408, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) + spacerItem_left = QtGui.QSpacerItem( + 408, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding + ) widget = QtGui.QComboBox() widget.addItems(self.default_profile_width_values) widget.blockSignals(True) widget.currentIndexChanged.connect(self.profile_width_changed) - spacerItem_right = QtGui.QSpacerItem(408, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) + spacerItem_right = QtGui.QSpacerItem( + 408, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding + ) hori_layout = QtGui.QHBoxLayout() hori_layout.addItem(spacerItem_left) hori_layout.addWidget(widget) @@ -336,12 +352,16 @@ def set_item_profile_table(self, row=0): def set_item_main_table(self, row=0, col=0, value=""): if col == 0: - spacerItem_left = QtGui.QSpacerItem(408, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) + spacerItem_left = QtGui.QSpacerItem( + 408, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding + ) widget = QtGui.QCheckBox() widget.blockSignals(True) self.list_table_widget_checkbox.insert(row, widget) widget.stateChanged.connect(self.guide_state_changed) - spacerItem_right = QtGui.QSpacerItem(408, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) + spacerItem_right = QtGui.QSpacerItem( + 408, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding + ) hori_layout = QtGui.QHBoxLayout() hori_layout.addItem(spacerItem_left) hori_layout.addWidget(widget) @@ -379,7 +399,9 @@ def get_profile_dimensions(self, row=-1): y_top = y0 y_bottom = y0 + height - Profile = collections.namedtuple("Profile", ["x_left", "x_right", "y_top", "y_bottom", "profile_center"]) + Profile = collections.namedtuple( + "Profile", ["x_left", "x_right", "y_top", "y_bottom", "profile_center"] + ) result = Profile(x_left, x_right, y_top, y_bottom, profile_center) return result @@ -400,7 +422,9 @@ def get_profile(self, image=[], profile_roi_row=-1): mean_axis = 0 x_axis = np.arange(y_top, y_bottom) - _data = image[x_left:x_right, y_top:y_bottom] # because pyqtgrpah display transpose images + _data = image[ + x_left:x_right, y_top:y_bottom + ] # because pyqtgrpah display transpose images profile = np.mean(_data, axis=mean_axis) return [x_axis, profile] @@ -459,7 +483,9 @@ def highlight_guide_profile_pyqt_rois(self, row=-1): return try: - self._highlights_guide_profile_pyqt_roi(row=previous_active_row, status="deactivated") + self._highlights_guide_profile_pyqt_roi( + row=previous_active_row, status="deactivated" + ) self._highlights_guide_profile_pyqt_roi(row=row, status="activated") except: pass @@ -611,7 +637,10 @@ def profile_along_axis_changed(self): def export_button_clicked(self): _export_folder = QFileDialog.getExistingDirectory( - self, directory=self.working_dir, caption="Select Output Folder", options=QFileDialog.ShowDirsOnly + self, + directory=self.working_dir, + caption="Select Output Folder", + options=QFileDialog.ShowDirsOnly, ) if _export_folder: o_export = ExportProfiles(parent=self, export_folder=_export_folder) @@ -629,7 +658,9 @@ def next_image_button_clicked(self): def help_button_clicked(self): import webbrowser - webbrowser.open("https://neutronimaging.pages.ornl.gov/en/tutorial/notebooks/profile/") + webbrowser.open( + "https://neutronimaging.pages.ornl.gov/en/tutorial/notebooks/profile/" + ) def closeEvent(self, event=None): pass @@ -642,7 +673,9 @@ def __init__(self, parent=None, export_folder=""): def _create_output_file_name(self, profile_index=0): base_name = os.path.basename(self.parent.working_dir) - output_file_name = os.path.join(self.export_folder, f"{base_name}_profile_{profile_index+1}.txt") + output_file_name = os.path.join( + self.export_folder, f"{base_name}_profile_{profile_index + 1}.txt" + ) return output_file_name def _create_metadata(self, profile_index=0): @@ -655,7 +688,9 @@ def _create_metadata(self, profile_index=0): y_top = profile_dimension.y_top y_bottom = profile_dimension.y_bottom metadata.append("#Profile dimension:") - metadata.append(f"# * [x0, y0, x1, y1] = [{x_left}, {y_top}, {x_right}, {y_bottom}]") + metadata.append( + f"# * [x0, y0, x1, y1] = [{x_left}, {y_top}, {x_right}, {y_bottom}]" + ) if is_x_profile_direction: metadata.append("# * integrated over y_axis") table_axis = ["#x_axis"] @@ -665,8 +700,8 @@ def _create_metadata(self, profile_index=0): nbr_files = len(self.parent.data_dict["file_name"]) metadata.append(f"#List of files ({nbr_files} files)") for _index, _file in enumerate(self.parent.data_dict["file_name"]): - metadata.append(f"# * {_file} -> col{_index+1}") - table_axis.append(f"# col.{_index+1}") + metadata.append(f"# * {_file} -> col{_index + 1}") + table_axis.append(f"# col.{_index + 1}") metadata.append("#") metadata.append("#" + ",".join(table_axis)) return metadata @@ -675,7 +710,9 @@ def _create_data(self, profile_index=0): all_profiles = [] x_axis = [] for _data in self.parent.data_dict["data"]: - [x_axis, profile] = self.parent.get_profile(image=np.transpose(_data), profile_roi_row=profile_index) + [x_axis, profile] = self.parent.get_profile( + image=np.transpose(_data), profile_roi_row=profile_index + ) all_profiles.append(list(profile)) data = [] @@ -688,10 +725,17 @@ def _create_data(self, profile_index=0): def run(self): _nbr_profiles = self.parent.ui.tableWidget.rowCount() for _profile_index in np.arange(_nbr_profiles): - _output_file_name = self._create_output_file_name(profile_index=_profile_index) + _output_file_name = self._create_output_file_name( + profile_index=_profile_index + ) metadata = self._create_metadata(profile_index=_profile_index) data = self._create_data(profile_index=_profile_index) - make_ascii_file(metadata=metadata, data=data, output_file_name=_output_file_name, dim="1d") + make_ascii_file( + metadata=metadata, + data=data, + output_file_name=_output_file_name, + dim="1d", + ) display(HTML(f"Exported Profile file {_output_file_name}")) @@ -712,14 +756,19 @@ def add(self): def update(self): self._define_profile() - self.parent.ui.image_view.removeItem(self.parent.list_profile_pyqt_roi[self.row]) + self.parent.ui.image_view.removeItem( + self.parent.list_profile_pyqt_roi[self.row] + ) self.parent.list_profile_pyqt_roi[self.row] = self.__profile def _define_guide(self): """define the guide""" guide_roi = pg.RectROI( [self.parent.default_guide_roi["x0"], self.parent.default_guide_roi["y0"]], - [self.parent.default_guide_roi["width"], self.parent.default_guide_roi["height"]], + [ + self.parent.default_guide_roi["width"], + self.parent.default_guide_roi["height"], + ], pen=self.parent.default_guide_roi["color_activated"], ) guide_roi.addScaleHandle([1, 1], [0, 0]) @@ -776,7 +825,13 @@ def _define_profile(self): line_color = tuple(_list_line_color) lines = np.array( [line_color for n in np.arange(len(pos))], - dtype=[("red", np.ubyte), ("green", np.ubyte), ("blue", np.ubyte), ("alpha", np.ubyte), ("width", float)], + dtype=[ + ("red", np.ubyte), + ("green", np.ubyte), + ("blue", np.ubyte), + ("alpha", np.ubyte), + ("width", float), + ], ) profile = pg.GraphItem() @@ -797,20 +852,28 @@ def timestamp_dict(self): def table(self): # init the summary table list_files_full_name = self.parent.data_dict["file_name"] - list_files_short_name = [os.path.basename(_file) for _file in list_files_full_name] + list_files_short_name = [ + os.path.basename(_file) for _file in list_files_full_name + ] list_time_stamp = self.parent.timestamp_dict["list_time_stamp"] - list_time_stamp_user_format = self.parent.timestamp_dict["list_time_stamp_user_format"] + list_time_stamp_user_format = self.parent.timestamp_dict[ + "list_time_stamp_user_format" + ] time_0 = list_time_stamp[0] for _row, _file in enumerate(list_files_short_name): self.parent.ui.summary_table.insertRow(_row) self.set_item_summary_table(row=_row, col=0, value=_file) - self.set_item_summary_table(row=_row, col=1, value=list_time_stamp_user_format[_row]) + self.set_item_summary_table( + row=_row, col=1, value=list_time_stamp_user_format[_row] + ) _offset = list_time_stamp[_row] - time_0 self.set_item_summary_table(row=_row, col=2, value=f"{_offset:0.2f}") self.parent.ui.all_plots_file_name_table.insertRow(_row) - self.set_item_all_plot_file_name_table(row=_row, value=os.path.basename(_file)) + self.set_item_all_plot_file_name_table( + row=_row, value=os.path.basename(_file) + ) def parameters(self): # init the position of the measurement ROI @@ -819,7 +882,9 @@ def parameters(self): self.parent.default_guide_roi["height"] = int(height / 5) self.parent.default_guide_roi["x0"] = int(width / 2) self.parent.default_guide_roi["y0"] = int(height / 2) - self.parent.default_profile_width_values = [str(_value) for _value in self.parent.default_profile_width_values] + self.parent.default_profile_width_values = [ + str(_value) for _value in self.parent.default_profile_width_values + ] def widgets(self): _file_path = os.path.dirname(__file__) @@ -827,28 +892,32 @@ def widgets(self): os.path.join(_file_path, "static/profile/button_rotation_left_fast.png") ) self.parent.ui.left_rotation_button_fast.setStyleSheet( - "background-image: " "url('" + left_rotation_fast_file + "'); " + "background-repeat: no-repeat" + "background-image: " + "url('" + left_rotation_fast_file + "'); " + "background-repeat: no-repeat" ) right_rotation_fast_file = os.path.abspath( os.path.join(_file_path, "static/profile/button_rotation_right_fast.png") ) self.parent.ui.right_rotation_button_fast.setStyleSheet( - "background-image: " "url('" + right_rotation_fast_file + "'); " + "background-repeat: no-repeat" + "background-image: " + "url('" + right_rotation_fast_file + "'); " + "background-repeat: no-repeat" ) left_rotation_slow_file = os.path.abspath( os.path.join(_file_path, "static/profile/button_rotation_left_slow.png") ) self.parent.ui.left_rotation_button_slow.setStyleSheet( - "background-image: " "url('" + left_rotation_slow_file + "'); " + "background-repeat: no-repeat" + "background-image: " + "url('" + left_rotation_slow_file + "'); " + "background-repeat: no-repeat" ) right_rotation_slow_file = os.path.abspath( os.path.join(_file_path, "static/profile/button_rotation_right_slow.png") ) self.parent.ui.right_rotation_button_slow.setStyleSheet( - "background-image: " "url('" + right_rotation_slow_file + "'); " + "background-repeat: no-repeat" + "background-image: " + "url('" + right_rotation_slow_file + "'); " + "background-repeat: no-repeat" ) self.parent.ui.splitter_2.setSizes([250, 50]) @@ -862,12 +931,16 @@ def widgets(self): # update size of table columns nbr_columns = self.parent.ui.tableWidget.columnCount() for _col in range(nbr_columns): - self.parent.ui.tableWidget.setColumnWidth(_col, self.parent.guide_table_width[_col]) + self.parent.ui.tableWidget.setColumnWidth( + _col, self.parent.guide_table_width[_col] + ) # update size of summary table nbr_columns = self.parent.ui.summary_table.columnCount() for _col in range(nbr_columns): - self.parent.ui.summary_table.setColumnWidth(_col, self.parent.summary_table_width[_col]) + self.parent.ui.summary_table.setColumnWidth( + _col, self.parent.summary_table_width[_col] + ) self.parent.display_ui = [ self.parent.ui.display_size_label, @@ -926,7 +999,8 @@ def get_image_selected(self, recalculate_image=False): angle = self.parent.rotation_angle # rotate all images self.parent.data_dict["data"] = [ - transform.rotate(_image, angle) for _image in self.parent.data_dict_raw["data"] + transform.rotate(_image, angle) + for _image in self.parent.data_dict_raw["data"] ] _image = self.parent.data_dict["data"][slider_index] @@ -950,7 +1024,9 @@ def display_images(self): _view_box.setState(_state) if not first_update: - _histo_widget.setLevels(self.parent.histogram_level[0], self.parent.histogram_level[1]) + _histo_widget.setLevels( + self.parent.histogram_level[0], self.parent.histogram_level[1] + ) def calculate_matrix_grid(self, grid_size=1, height=1, width=1): """calculate the matrix that defines the vertical and horizontal lines @@ -1000,12 +1076,17 @@ def display_grid(self): grid_size = self.parent.ui.grid_size_slider.value() [height, width] = np.shape(self.parent.live_image) - pos_adj_dict = self.calculate_matrix_grid(grid_size=grid_size, height=height, width=width) + pos_adj_dict = self.calculate_matrix_grid( + grid_size=grid_size, height=height, width=width + ) pos = pos_adj_dict["pos"] adj = pos_adj_dict["adj"] line_color = self.parent.grid_view["color"] - _transparency_value = 255 - (float(str(self.parent.ui.transparency_slider.value())) / 100) * 255 + _transparency_value = ( + 255 + - (float(str(self.parent.ui.transparency_slider.value())) / 100) * 255 + ) _list_line_color = list(line_color) _list_line_color[3] = _transparency_value line_color = tuple(_list_line_color) diff --git a/notebooks/__code/select_files_and_folders.py b/notebooks/__code/select_files_and_folders.py index 066ede305..11b8c7dc1 100755 --- a/notebooks/__code/select_files_and_folders.py +++ b/notebooks/__code/select_files_and_folders.py @@ -14,7 +14,9 @@ class SelectFiles(FileFolderBrowser): def __init__(self, system=None): working_dir = system.System.get_working_dir() - super(SelectFiles, self).__init__(working_dir=working_dir, next_function=self.retrieve_list_of_files) + super(SelectFiles, self).__init__( + working_dir=working_dir, next_function=self.retrieve_list_of_files + ) filters = {"TIFF": "*.tif"} default_filter = "TIFF" @@ -25,7 +27,9 @@ def retrieve_list_of_files(self, list_of_files=""): class SelectFolderWithDefaultPaths(FileFolderBrowser): - def __init__(self, system=None, working_dir="", next_function=None, multiple_flag=False): + def __init__( + self, system=None, working_dir="", next_function=None, multiple_flag=False + ): if working_dir == "": if system is None: working_dir = "/" @@ -33,7 +37,9 @@ def __init__(self, system=None, working_dir="", next_function=None, multiple_fla working_dir = system.System.get_working_dir() super(SelectFolderWithDefaultPaths, self).__init__( - working_dir=working_dir, next_function=next_function, multiple_flag=multiple_flag + working_dir=working_dir, + next_function=next_function, + multiple_flag=multiple_flag, ) ipts = os.path.basename(self.working_dir) @@ -43,9 +49,15 @@ def __init__(self, system=None, working_dir="", next_function=None, multiple_fla hbox = widgets.HBox( [ widgets.Button( - description=f"Jump to {ipts} Shared Folder", button_style="success", layout=button_layout + description=f"Jump to {ipts} Shared Folder", + button_style="success", + layout=button_layout, + ), + widgets.Button( + description="Jump to My Home Folder", + button_style="success", + layout=button_layout, ), - widgets.Button(description="Jump to My Home Folder", button_style="success", layout=button_layout), ] ) go_to_shared_button_ui = hbox.children[0] @@ -83,11 +95,20 @@ def display_file_selector(self, start_dir=""): class SelectFolder(FileFolderBrowser): - def __init__(self, system=None, next_function=None, is_input_folder=False, working_dir="", multiple_flags=False): + def __init__( + self, + system=None, + next_function=None, + is_input_folder=False, + working_dir="", + multiple_flags=False, + ): if not working_dir: working_dir = system.System.get_working_dir() - super(SelectFolder, self).__init__(working_dir=working_dir, next_function=next_function) + super(SelectFolder, self).__init__( + working_dir=working_dir, next_function=next_function + ) if is_input_folder: self.select_input_folder(multiple_flag=multiple_flags) else: @@ -100,15 +121,24 @@ class SelectAsciiFile(FileFolderBrowser): def __init__(self, system=None, instruction=""): working_dir = system.System.get_working_dir() - super(SelectAsciiFile, self).__init__(working_dir=working_dir, next_function=self.done_message) + super(SelectAsciiFile, self).__init__( + working_dir=working_dir, next_function=self.done_message + ) if not instruction: instruction = "Select ASCII File!" filters = {"Text": "*.txt"} default_filter = "Text" - self.select_images(filters=filters, instruction=instruction, multiple_flag=False, default_filter=default_filter) + self.select_images( + filters=filters, + instruction=instruction, + multiple_flag=False, + default_filter=default_filter, + ) def done_message(self, file_selected): self.ascii_file = file_selected - display_html_message(title_message="Selected Ascii File:", message=file_selected) + display_html_message( + title_message="Selected Ascii File:", message=file_selected + ) diff --git a/notebooks/__code/select_metadata_to_display.py b/notebooks/__code/select_metadata_to_display.py index 421552661..445a2071d 100755 --- a/notebooks/__code/select_metadata_to_display.py +++ b/notebooks/__code/select_metadata_to_display.py @@ -28,7 +28,11 @@ def display_metadata_list(self): self.box1 = widgets.HBox( [ widgets.Label("Select Metadata:", layout=widgets.Layout(width="10%")), - widgets.Dropdown(options=display_format, value=display_format[0], layout=widgets.Layout(width="50%")), + widgets.Dropdown( + options=display_format, + value=display_format[0], + layout=widgets.Layout(width="50%"), + ), ] ) display(self.box1) @@ -64,7 +68,12 @@ def export(self): parent_folder = self.list_images[0].split(os.path.sep)[-2] metadata_name = f"metadata#{self.key}" - output_file_name = os.path.join(output_folder, f"{parent_folder}_{metadata_name}.txt") + output_file_name = os.path.join( + output_folder, f"{parent_folder}_{metadata_name}.txt" + ) file_handler.make_ascii_file( - metadata=["#Metadata: " + self.key], data=self.export_txt, dim="1d", output_file_name=output_file_name + metadata=["#Metadata: " + self.key], + data=self.export_txt, + dim="1d", + output_file_name=output_file_name, ) diff --git a/notebooks/__code/sequential_combine_images_using_metadata.py b/notebooks/__code/sequential_combine_images_using_metadata.py index 754b759c4..88c3a4cdf 100755 --- a/notebooks/__code/sequential_combine_images_using_metadata.py +++ b/notebooks/__code/sequential_combine_images_using_metadata.py @@ -45,7 +45,13 @@ def select_folder(self): self.files_list_widget.show() def info_folder_selected(self, selected): - display(HTML('You selected folder: ' + selected + "")) + display( + HTML( + 'You selected folder: ' + + selected + + "" + ) + ) self.folder_selected = selected def record_file_extension(self, filename=""): @@ -107,7 +113,8 @@ def how_to_combine(self): ) self.combine_method = widgets.RadioButtons( - options=["add", "arithmetic mean", "geometric mean"], value="arithmetic mean" + options=["add", "arithmetic mean", "geometric mean"], + value="arithmetic mean", ) vertical = widgets.VBox([alge_box, geo_box, self.combine_method]) @@ -140,8 +147,15 @@ def create_merging_list(self, list_of_files=[]): create_list_progress = widgets.HBox( [ - widgets.Label("Creating Merging List:", layout=widgets.Layout(width="20%")), - widgets.IntProgress(max=len(self.list_images), min=1, value=1, layout=widgets.Layout(width="80%")), + widgets.Label( + "Creating Merging List:", layout=widgets.Layout(width="20%") + ), + widgets.IntProgress( + max=len(self.list_images), + min=1, + value=1, + layout=widgets.Layout(width="80%"), + ), ] ) display(create_list_progress) @@ -164,16 +178,20 @@ def create_merging_list(self, list_of_files=[]): _list_files = [list_of_files[0]] _dict_metadata = {} - _previous_metadata = MetadataHandler.get_metata(filename=list_of_files[0], list_metadata=list_of_tag_selected) + _previous_metadata = MetadataHandler.get_metata( + filename=list_of_files[0], list_metadata=list_of_tag_selected + ) _previous_run = self.isolate_run_text_from_filename(list_of_files[0]) for _index, _file in enumerate(list_of_files[1:]): - _current_metadata = MetadataHandler.get_metata(filename=_file, list_metadata=list_of_tag_selected) + _current_metadata = MetadataHandler.get_metata( + filename=_file, list_metadata=list_of_tag_selected + ) _current_run = self.isolate_run_text_from_filename(_file) - if self.are_metadata_within_error_range(_current_metadata, _previous_metadata) and ( - _previous_run == _current_run - ): + if self.are_metadata_within_error_range( + _current_metadata, _previous_metadata + ) and (_previous_run == _current_run): _list_files.append(_file) else: str_position_counter = f"{position_counter:04d}" @@ -187,7 +205,9 @@ def create_merging_list(self, list_of_files=[]): _list_files = [_file] if _previous_run != _current_run: - master_list_images_to_combine[_previous_run] = list_images_to_combine + master_list_images_to_combine[_previous_run] = ( + list_images_to_combine + ) list_images_to_combine = collections.OrderedDict() _previous_metadata = _current_metadata @@ -233,7 +253,9 @@ def isolate_value_from_metadata(self, metadata_string): def isolate_run_text_from_filename(self, full_file_name): basename = os.path.basename(full_file_name) - regular_expression = self.extension_to_regular_expression_dict[self.file_extension] + regular_expression = self.extension_to_regular_expression_dict[ + self.file_extension + ] m = re.search(regular_expression, basename) if m is not None: return m.group("run") @@ -267,7 +289,10 @@ def recap_merging_list(self): box2 = widgets.VBox( [ - widgets.Label("List of Files for this position", layout=widgets.Layout(width="100%")), + widgets.Label( + "List of Files for this position", + layout=widgets.Layout(width="100%"), + ), widgets.Select( options=self.get_list_of_files_for_selected_run_position(), layout=widgets.Layout(width="100%", height="500px"), @@ -286,13 +311,19 @@ def recap_merging_list(self): layout=widgets.Layout(width="300px"), ) - str_metadata = self.get_str_metadata(metadata_dict=self.get_metadata_for_selected_run_position()) + str_metadata = self.get_str_metadata( + metadata_dict=self.get_metadata_for_selected_run_position() + ) self.metadata_recap_textarea = box3.children[1] self.metadata_recap_textarea.value = str_metadata - hori_box = widgets.HBox([box0, box1, box2, box3], layout=widgets.Layout(width="100%")) + hori_box = widgets.HBox( + [box0, box1, box2, box3], layout=widgets.Layout(width="100%") + ) - self.list_of_positions_ui.on_trait_change(self.recap_positions_changed, name="value") + self.list_of_positions_ui.on_trait_change( + self.recap_positions_changed, name="value" + ) self.list_of_runs_ui.on_trait_change(self.recap_runs_changed, name="value") display(hori_box) @@ -325,10 +356,14 @@ def recap_positions_changed(self): position_selected = self.list_of_positions_ui.value run_selected = self.list_of_runs_ui.value - list_files_of_files = self.master_list_images_to_combine[run_selected][position_selected]["list_of_files"] + list_files_of_files = self.master_list_images_to_combine[run_selected][ + position_selected + ]["list_of_files"] self.list_of_files_ui.options = list_files_of_files - str_metadata = self.get_str_metadata(metadata_dict=self.get_metadata_for_selected_run_position()) + str_metadata = self.get_str_metadata( + metadata_dict=self.get_metadata_for_selected_run_position() + ) self.metadata_recap_textarea.value = str_metadata def recap_runs_changed(self): @@ -406,7 +441,9 @@ def merge(self, output_folder): merging_ui = widgets.HBox( [ widgets.Label("Merging Progress", layout=widgets.Layout(width="20%")), - widgets.IntProgress(max=len(merging_list.keys()), layout=widgets.Layout(width="80%")), + widgets.IntProgress( + max=len(merging_list.keys()), layout=widgets.Layout(width="80%") + ), ] ) display(merging_ui) @@ -426,14 +463,20 @@ def merge(self, output_folder): _data = o_load.data["sample"]["data"] _metadata = o_load.data["sample"]["metadata"][0] - combined_data = SequentialCombineImagesUsingMetadata._merging_algorithm(algorithm, _data) + combined_data = SequentialCombineImagesUsingMetadata._merging_algorithm( + algorithm, _data + ) _new_name = self._define_merged_file_name( - output_folder=output_folder, run_label=_run, position_label=_position + output_folder=output_folder, + run_label=_run, + position_label=_position, ) output_file_name = os.path.join(output_folder, _new_name) - file_handler.save_data(data=combined_data, filename=output_file_name, metadata=_metadata) + file_handler.save_data( + data=combined_data, filename=output_file_name, metadata=_metadata + ) _run_index += 1 progress_bar_ui.value = _run_index @@ -442,17 +485,25 @@ def merge(self, output_folder): del merging_ui display( - HTML('Files have been created in : ' + output_folder + "") + HTML( + 'Files have been created in : ' + + output_folder + + "" + ) ) def make_output_folder(self, output_folder): algorithm_selected = self.__get_formated_merging_algo_name() folder_selected = os.path.basename(os.path.dirname(self.folder_selected)) - output_folder = os.path.join(output_folder, f"{folder_selected}_{algorithm_selected}") + output_folder = os.path.join( + output_folder, f"{folder_selected}_{algorithm_selected}" + ) file_handler.make_folder(output_folder) return output_folder - def _define_merged_file_name(self, output_folder="", run_label="", position_label=""): + def _define_merged_file_name( + self, output_folder="", run_label="", position_label="" + ): """Create the new merged file name using the run, position labels ex: run_label = "run1" diff --git a/notebooks/__code/shifting_time_offset.py b/notebooks/__code/shifting_time_offset.py index b97da0b76..2d32cf31d 100755 --- a/notebooks/__code/shifting_time_offset.py +++ b/notebooks/__code/shifting_time_offset.py @@ -50,7 +50,11 @@ def built_list_of_fits_files(self, input_folder): ) self.list_of_fits_files = list_of_fits_files else: - display(HTML('No FITS files Found!')) + display( + HTML( + 'No FITS files Found!' + ) + ) def retrieve_parent_folder(self, folder): self.working_dir = Path(folder).parent @@ -64,7 +68,13 @@ def selected_other_folders(self, list_of_other_folders): ) ) for _folder in list_of_other_folders: - display(HTML(' - ' + _folder + " FITS files to process!")) + display( + HTML( + ' - ' + + _folder + + " FITS files to process!" + ) + ) def retrieve_name_of_timestamp_file(self, input_folder): timestamp_files = list(Path(input_folder).glob("*_Spectra.txt")) @@ -79,7 +89,11 @@ def retrieve_name_of_timestamp_file(self, input_folder): ) self.timestamp_file = timestamp_file else: - display(HTML('Time stamp not Found')) + display( + HTML( + 'Time stamp not Found' + ) + ) def load_timestamp_file(self, timestamp_file): counts_vs_time_array = pd.read_csv(timestamp_file, sep="\t") @@ -104,7 +118,10 @@ def plot_cutoff(index): return index self.index_slider = interact( - plot_cutoff, index=widgets.IntSlider(min=0, max=x_index_axis[-1], value=0, continuous_update=False) + plot_cutoff, + index=widgets.IntSlider( + min=0, max=x_index_axis[-1], value=0, continuous_update=False + ), ) def get_file_prefix(self, file_name): @@ -125,7 +142,9 @@ def offset_images(self): nbr_folder = len(list_of_folders) - progress_bar = widgets.IntProgress(max=nbr_folder, layout=widgets.Layout(width="50%")) + progress_bar = widgets.IntProgress( + max=nbr_folder, layout=widgets.Layout(width="50%") + ) display(progress_bar) offset_index = self.index_slider.widget.result @@ -134,7 +153,9 @@ def offset_images(self): for _index, _current_working_folder in enumerate(list_of_folders): # get full list of FITS files - list_of_fits_files = np.array(self.get_list_of_fits_files(_current_working_folder)) + list_of_fits_files = np.array( + self.get_list_of_fits_files(_current_working_folder) + ) if list_of_fits_files == []: continue @@ -145,7 +166,9 @@ def offset_images(self): self.retrieve_name_of_timestamp_file(_current_working_folder) timestamp_file = self.timestamp_file if not Path(timestamp_file).exists(): - list_folder_with_error.append(f"Error in {_current_working_folder}. Timestamp file missing!") + list_folder_with_error.append( + f"Error in {_current_working_folder}. Timestamp file missing!" + ) continue # rename all files starting by file at index offset_index which will become index 0 @@ -155,7 +178,9 @@ def offset_images(self): new_output_dir = current_working_dir + "_timeoffset_corrected" self.copy_and_renamed_fits_files( - output_dir=new_output_dir, original_list_of_files=new_list_of_fits_files, prefix=prefix + output_dir=new_output_dir, + original_list_of_files=new_list_of_fits_files, + prefix=prefix, ) # modify timestamp file @@ -163,7 +188,9 @@ def offset_images(self): output_dir=new_output_dir, old_timestamp_filename=timestamp_file ) self.create_new_timestamp_file( - timestamp_file=timestamp_file, offset=offset_index, new_timestamp_filename=new_timestamp_filename + timestamp_file=timestamp_file, + offset=offset_index, + new_timestamp_filename=new_timestamp_filename, ) progress_bar.value = _index + 1 @@ -176,7 +203,9 @@ def create_new_timestamp_filename(self, output_dir="./", old_timestamp_filename= short_old_timestamp_filename = str(Path(old_timestamp_filename).name) return str(Path(output_dir).joinpath(short_old_timestamp_filename)) - def create_new_timestamp_file(self, timestamp_file="", offset=0, new_timestamp_filename=""): + def create_new_timestamp_file( + self, timestamp_file="", offset=0, new_timestamp_filename="" + ): timestamp_array = self.load_timestamp_file(timestamp_file) time_axis = timestamp_array[:, 0] new_counts_axis = np.roll(np.array(timestamp_array[:, 1]), -offset) @@ -188,18 +217,28 @@ def create_new_timestamp_file(self, timestamp_file="", offset=0, new_timestamp_f # bring back axis together combined_array = np.stack((new_time_axis, new_counts_axis)).T # print("new timesamp_filename is {}".format(new_timestamp_filename)) - make_ascii_file(data=combined_array, output_file_name=new_timestamp_filename, sep="\t") + make_ascii_file( + data=combined_array, output_file_name=new_timestamp_filename, sep="\t" + ) def display_errors(self, list_folder_with_error=[]): for _line in list_folder_with_error: - display(HTML('' + _line + "!")) + display( + HTML('' + _line + "!") + ) - def copy_and_renamed_fits_files(self, output_dir="./", original_list_of_files=[], prefix="test"): + def copy_and_renamed_fits_files( + self, output_dir="./", original_list_of_files=[], prefix="test" + ): current_working_dir = str(Path(original_list_of_files[0]).parent) make_or_reset_folder(output_dir) log_file = str(Path(output_dir).joinpath("renaming_log.txt")) - renaming_log_file = [f"Renaming schema of folder {current_working_dir}", "old name -> new name", ""] + renaming_log_file = [ + f"Renaming schema of folder {current_working_dir}", + "old name -> new name", + "", + ] for index, _file in enumerate(original_list_of_files): old_name = Path(_file).name new_name = Path(output_dir).joinpath(prefix + f"_{index:05d}.fits") diff --git a/notebooks/__code/super_user.py b/notebooks/__code/super_user.py index b808afedb..a4145d2b1 100755 --- a/notebooks/__code/super_user.py +++ b/notebooks/__code/super_user.py @@ -8,7 +8,9 @@ from .config import debugger_folder as list_debugging_folder from .config import debugging from .config import password_to_unlock_config as PASSWORD -from .config import percentage_of_images_to_use_for_roi_selection as PERCENTAGE_OF_IMAGES +from .config import ( + percentage_of_images_to_use_for_roi_selection as PERCENTAGE_OF_IMAGES, +) THIS_FILE_PATH = os.path.dirname(__file__) CONFIG_FILE = os.path.join(THIS_FILE_PATH, "config.py") @@ -19,15 +21,24 @@ def __init__(self): self.launch_ui() def launch_ui(self): - password = widgets.Password(value="", placeholder="Enter password", description="Password", diabled=False) + password = widgets.Password( + value="", + placeholder="Enter password", + description="Password", + diabled=False, + ) password.observe(self.password_entered, names="value") # ---- # debugging mode - self.debugging_mode = widgets.Checkbox(value=debugging, description="Debugging Mode", disabled=True) + self.debugging_mode = widgets.Checkbox( + value=debugging, description="Debugging Mode", disabled=True + ) - self.debugging_folder_label = widgets.HTML("List of folders to look for when running in debugging mode.") + self.debugging_folder_label = widgets.HTML( + "List of folders to look for when running in debugging mode." + ) self.debugging_folder = widgets.Select( options=list_debugging_folder, value=list_debugging_folder[0], @@ -46,21 +57,37 @@ def launch_ui(self): ) self.remove_entry.on_click(self.remove_entry_clicked) - self.new_entry_text = widgets.Text(value="", description="New folder", disabled=True) + self.new_entry_text = widgets.Text( + value="", description="New folder", disabled=True + ) self.add_entry = widgets.Button( - description="", disabled=True, button_style="", icon="plus-square", layout=widgets.Layout(width="95px") + description="", + disabled=True, + button_style="", + icon="plus-square", + layout=widgets.Layout(width="95px"), ) self.add_entry.on_click(self.add_entry_clicked) hori_layout_percentage = widgets.HBox([self.new_entry_text, self.add_entry]) # percentage of images to use for roi selection - self.percentage_roi_label = widgets.HTML("Percentage of images to use for ROI selection", disabled=True) + self.percentage_roi_label = widgets.HTML( + "Percentage of images to use for ROI selection", disabled=True + ) percentage_of_images = PERCENTAGE_OF_IMAGES * 100 self.percentage_roi_value = widgets.FloatText( - value=percentage_of_images, disabled=True, layout=widgets.Layout(width="50px") + value=percentage_of_images, + disabled=True, + layout=widgets.Layout(width="50px"), ) self.percentage_units = widgets.HTML("%", disabled=True) - hori_layout = widgets.HBox([self.percentage_roi_label, self.percentage_roi_value, self.percentage_units]) + hori_layout = widgets.HBox( + [ + self.percentage_roi_label, + self.percentage_roi_value, + self.percentage_units, + ] + ) # ---- self.save_changes = widgets.Button( @@ -159,7 +186,9 @@ def save_button_clicked(self, value): elif "debugger_folder = " in _line: ascii_after.append(f"debugger_folder = {str_list_folders_formatted}") elif "percentage_of_images_to_use_for_roi_selection = " in _line: - ascii_after.append(f"percentage_of_images_to_use_for_roi_selection = {percentage_roi_selection}") + ascii_after.append( + f"percentage_of_images_to_use_for_roi_selection = {percentage_roi_selection}" + ) else: ascii_after.append(_line) diff --git a/notebooks/__code/system.py b/notebooks/__code/system.py index 952886029..ed393498c 100755 --- a/notebooks/__code/system.py +++ b/notebooks/__code/system.py @@ -45,7 +45,9 @@ def select_working_dir( """) ) - full_list_instruments = cls.get_full_list_instrument(instrument_to_exclude=instrument_to_exclude) + full_list_instruments = cls.get_full_list_instrument( + instrument_to_exclude=instrument_to_exclude + ) full_list_instruments.sort() if instrument in full_list_instruments: default_instrument = instrument @@ -53,16 +55,22 @@ def select_working_dir( default_instrument = full_list_instruments[0] start_path = cls.get_start_path( - debugger_folder=debugger_folder, system_folder=system_folder, instrument=default_instrument + debugger_folder=debugger_folder, + system_folder=system_folder, + instrument=default_instrument, ) cls.start_path = start_path select_instrument_ui = widgets.HBox( [ - widgets.Label("Select Instrument", layout=widgets.Layout(width="20%")), + widgets.Label( + "Select Instrument", layout=widgets.Layout(width="20%") + ), widgets.Select( - options=full_list_instruments, value=default_instrument, layout=widgets.Layout(width="20%") + options=full_list_instruments, + value=default_instrument, + layout=widgets.Layout(width="20%"), ), ] ) @@ -76,7 +84,9 @@ def select_working_dir( [ widgets.Label("IPTS-"), widgets.Text(value="", layout=widgets.Layout(width="10%")), - widgets.Label("DOES NOT EXIST!", layout=widgets.Layout(width="20%")), + widgets.Label( + "DOES NOT EXIST!", layout=widgets.Layout(width="20%") + ), ] ) cls.result_label = top_hbox.children[2] @@ -92,12 +102,16 @@ def select_working_dir( [ widgets.Label("Select Folder", layout=widgets.Layout(width="20%")), widgets.Select( - options=user_list_folders, value=default_value, layout=widgets.Layout(height="300px") + options=user_list_folders, + value=default_value, + layout=widgets.Layout(height="300px"), ), ] ) cls.user_list_folders = user_list_folders - box = widgets.VBox([select_instrument_ui, top_hbox, or_label, bottom_hbox, help_ui]) + box = widgets.VBox( + [select_instrument_ui, top_hbox, or_label, bottom_hbox, help_ui] + ) display(box) cls.working_dir_ui = bottom_hbox.children[1] @@ -114,7 +128,11 @@ def select_working_dir( except: cls.working_dir = os.path.expanduser("~") display( - HTML('working dir set to -> ' + cls.working_dir + "") + HTML( + 'working dir set to -> ' + + cls.working_dir + + "" + ) ) cls.log_use(notebook=notebook) @@ -151,13 +169,21 @@ def get_list_folders(cls, start_path=""): cls.start_path = start_path list_folders = sorted(glob.glob(os.path.join(start_path, "*")), reverse=True) - short_list_folders = [os.path.basename(_folder) for _folder in list_folders if os.path.isdir(_folder)] + short_list_folders = [ + os.path.basename(_folder) + for _folder in list_folders + if os.path.isdir(_folder) + ] # short_list_folders = sorted(short_list_folders) # if user mode, only display folder user can access default_value = "" if not debugging: - user_list_folders = [os.path.basename(_folder) for _folder in list_folders if os.access(_folder, os.R_OK)] + user_list_folders = [ + os.path.basename(_folder) + for _folder in list_folders + if os.access(_folder, os.R_OK) + ] if len(user_list_folders) > 0: default_value = user_list_folders[0] else: # debugging @@ -193,7 +219,9 @@ def get_computer_name(cls): @classmethod def get_facility_selected(cls): - return cls.get_facility_from_instrument(instrument=cls.get_instrument_selected()) + return cls.get_facility_from_instrument( + instrument=cls.get_instrument_selected() + ) @classmethod def get_start_path(cls, debugger_folder="", system_folder="", instrument=""): @@ -241,7 +269,9 @@ def get_start_path(cls, debugger_folder="", system_folder="", instrument=""): def select_ipts_help(cls, value): import webbrowser - webbrowser.open("https://neutronimaging.pages.ornl.gov/tutorial/notebooks/select_ipts/") + webbrowser.open( + "https://neutronimaging.pages.ornl.gov/tutorial/notebooks/select_ipts/" + ) @classmethod def check_instrument_input(cls, value_dict): diff --git a/notebooks/__code/table_handler.py b/notebooks/__code/table_handler.py index 9be94414d..749177c78 100755 --- a/notebooks/__code/table_handler.py +++ b/notebooks/__code/table_handler.py @@ -12,7 +12,9 @@ def __init__(self, table_ui=None): def select_everything(self, state): nbr_row = self.table_ui.rowCount() nbr_column = self.table_ui.columnCount() - selection_range = QtGui.QTableWidgetSelectionRange(0, 0, nbr_row - 1, nbr_column - 1) + selection_range = QtGui.QTableWidgetSelectionRange( + 0, 0, nbr_row - 1, nbr_column - 1 + ) self.table_ui.setRangeSelected(selection_range, state) def select_rows(self, list_of_rows=None): @@ -21,7 +23,9 @@ def select_rows(self, list_of_rows=None): nbr_column = self.table_ui.columnCount() for _row in list_of_rows: - selection_range = QtGui.QTableWidgetSelectionRange(_row, 0, _row, nbr_column - 1) + selection_range = QtGui.QTableWidgetSelectionRange( + _row, 0, _row, nbr_column - 1 + ) self.table_ui.setRangeSelected(selection_range, True) def remove_all_rows(self): diff --git a/notebooks/__code/template_ui.py b/notebooks/__code/template_ui.py index b7c6dd9ce..c2a29cf38 100755 --- a/notebooks/__code/template_ui.py +++ b/notebooks/__code/template_ui.py @@ -61,7 +61,9 @@ def __init__(self, parent=None, o_norm=None): self.ui.slider.valueChanged.connect(self.file_index_changed) # spacer - spacer = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) + spacer = QtGui.QSpacerItem( + 40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum + ) bottom_layout.addWidget(label_1) bottom_layout.addWidget(self.ui.slider) diff --git a/notebooks/__code/time_utility.py b/notebooks/__code/time_utility.py index 725ead851..bb5521051 100755 --- a/notebooks/__code/time_utility.py +++ b/notebooks/__code/time_utility.py @@ -25,7 +25,13 @@ def format_time_stamp(file_name=None, time_stamp=None): [hours, minutes, seconds] = hours.split(":") _dict_time = {"hours": hours, "minutes": minutes, "seconds": seconds} - _dict_time_stamp = {"week_day": week_day, "month": month, "day": day, "hours": _dict_time, "year": year} + _dict_time_stamp = { + "week_day": week_day, + "month": month, + "day": day, + "hours": _dict_time, + "year": year, + } return [_short_file_name, _dict_time_stamp] @@ -99,8 +105,10 @@ def __init__(self, folder="", files=[], is_notebook=False): self.__is_notebook = is_notebook def _run(self): - [list_files, ext] = file_handler.retrieve_list_of_most_dominant_extension_from_folder( - folder=self.folder, files=self.input_list_files + [list_files, ext] = ( + file_handler.retrieve_list_of_most_dominant_extension_from_folder( + folder=self.folder, files=self.input_list_files + ) ) self.output_list_files = list_files @@ -114,8 +122,15 @@ def _run(self): if self.__is_notebook: box = widgets.HBox( [ - widgets.Label("Retrieving Time Stamp", layout=widgets.Layout(width="20%")), - widgets.IntProgress(min=0, max=len(list_files), value=0, layout=widgets.Layout(width="50%")), + widgets.Label( + "Retrieving Time Stamp", layout=widgets.Layout(width="20%") + ), + widgets.IntProgress( + min=0, + max=len(list_files), + value=0, + layout=widgets.Layout(width="50%"), + ), ] ) progress_bar = box.children[1] @@ -152,7 +167,12 @@ class TimestampFormatter: "%Y-%m-%dT%I:%M:%S-", ] - def __init__(self, timestamp="", input_timestamp_format=None, output_timestamp_format=TIMESTAMP_FORMAT): + def __init__( + self, + timestamp="", + input_timestamp_format=None, + output_timestamp_format=TIMESTAMP_FORMAT, + ): self.timestamp = timestamp if input_timestamp_format is None: self.input_timestamp_format = self.list_input_timestamp @@ -189,7 +209,9 @@ def convert_timestamp(self, timestamp): o_time = None for _input_timestamp_format in input_timestamp_format: # print("trying this format {} with this {}".format(_input_timestamp_format, timestamp)) - o_time = TimestampFormatter.get_time_dict(timestamp=timestamp, input_time_format=_input_timestamp_format) + o_time = TimestampFormatter.get_time_dict( + timestamp=timestamp, input_time_format=_input_timestamp_format + ) if o_time: break @@ -228,11 +250,15 @@ def convert_to_second(timestamp_value, timestamp_format=TIMESTAMP_FORMAT): class AbsoluteTimeHandler: def __init__(self, initial_absolute_time=None): if initial_absolute_time is None: - raise ValueError("Please provide an initial absolute time format as 'YYYY-MM-DDTHH:MM:SS.SSSSSS-05:00") + raise ValueError( + "Please provide an initial absolute time format as 'YYYY-MM-DDTHH:MM:SS.SSSSSS-05:00" + ) self.formatted_initial_absolute_time = parse(initial_absolute_time) - def get_absolute_time_for_this_delta_time_array(self, delta_time_array=None, units="seconds"): + def get_absolute_time_for_this_delta_time_array( + self, delta_time_array=None, units="seconds" + ): """ :param delta_time_array: list of time offset @@ -254,7 +280,10 @@ def get_absolute_time_for_this_delta_time_array(self, delta_time_array=None, uni self.delta_time_formated = delta_time_formated - absolute_time = [delta_time + self.formatted_initial_absolute_time for delta_time in delta_time_formated] + absolute_time = [ + delta_time + self.formatted_initial_absolute_time + for delta_time in delta_time_formated + ] return absolute_time @@ -268,7 +297,9 @@ class RelativeTimeHandler: def __init__(self, master_initial_time=None, local_initial_time=None): if (master_initial_time is None) or (local_initial_time is None): - raise ValueError("Please provide an initial absolute time format as 'YYYY-MM-DDTHH:MM:SS.SSSSSS-05:00") + raise ValueError( + "Please provide an initial absolute time format as 'YYYY-MM-DDTHH:MM:SS.SSSSSS-05:00" + ) formatted_master_initial_time = parse(master_initial_time) formatted_local_initial_time = parse(local_initial_time) @@ -276,7 +307,9 @@ def __init__(self, master_initial_time=None, local_initial_time=None): if formatted_local_initial_time < formatted_master_initial_time: raise ValueError("Master time should be before local time!") - time_offset_calculated = formatted_local_initial_time - formatted_master_initial_time + time_offset_calculated = ( + formatted_local_initial_time - formatted_master_initial_time + ) self.time_offset_calculated_s = time_offset_calculated.seconds def get_relative_time_for_this_time_array(self, time_array=None): diff --git a/notebooks/__code/timepix3_event_hdf5_he3_detector/timepix3_event_hdf5_he3_detector.py b/notebooks/__code/timepix3_event_hdf5_he3_detector/timepix3_event_hdf5_he3_detector.py index 53f66e857..77f0d7c7c 100755 --- a/notebooks/__code/timepix3_event_hdf5_he3_detector/timepix3_event_hdf5_he3_detector.py +++ b/notebooks/__code/timepix3_event_hdf5_he3_detector/timepix3_event_hdf5_he3_detector.py @@ -67,14 +67,20 @@ def rebin_and_display_h3_data(self): bin_size = hbox.children[1] - fig, ax = plt.subplots(figsize=(8, 8), nrows=1, ncols=1, num="Histogram of He3 detector") + fig, ax = plt.subplots( + figsize=(8, 8), nrows=1, ncols=1, num="Histogram of He3 detector" + ) - def plot_rebinned_data(x_axis="TOF", nbrs_bins=2, dSD_m=19.855, offset_micros=0, element="Ni"): + def plot_rebinned_data( + x_axis="TOF", nbrs_bins=2, dSD_m=19.855, offset_micros=0, element="Ni" + ): if element == "Ni": _handler = BraggEdgeLibrary(material=[element], number_of_bragg_edges=5) else: # Ta _handler = BraggEdgeLibrary( - new_material=[{"name": "Ta", "lattice": 3.3058, "crystal_structure": "BCC"}], + new_material=[ + {"name": "Ta", "lattice": 3.3058, "crystal_structure": "BCC"} + ], number_of_bragg_edges=5, ) @@ -132,11 +138,20 @@ def plot_rebinned_data(x_axis="TOF", nbrs_bins=2, dSD_m=19.855, offset_micros=0, options=["TOF", "lambda"], value="lambda", ), - nbrs_bins=widgets.IntSlider(value=10000, min=1, max=100000, continuous_update=False), + nbrs_bins=widgets.IntSlider( + value=10000, min=1, max=100000, continuous_update=False + ), dSD_m=widgets.FloatSlider( - value=19.855, min=15, max=25, step=0.001, continuous_update=False, readout_format=".3f" + value=19.855, + min=15, + max=25, + step=0.001, + continuous_update=False, + readout_format=".3f", + ), + offset_micros=widgets.IntSlider( + value=0, min=0, max=15000, continuous_update=False ), - offset_micros=widgets.IntSlider(value=0, min=0, max=15000, continuous_update=False), element=widgets.RadioButtons(options=["Ni", "Ta"], value="Ni"), ) display(v) diff --git a/notebooks/__code/timepix3_from_event_to_histo_hdf5/timepix3_from_event_to_histo_hdf5.py b/notebooks/__code/timepix3_from_event_to_histo_hdf5/timepix3_from_event_to_histo_hdf5.py index 0b0c3a0f8..73f15edbb 100755 --- a/notebooks/__code/timepix3_from_event_to_histo_hdf5/timepix3_from_event_to_histo_hdf5.py +++ b/notebooks/__code/timepix3_from_event_to_histo_hdf5/timepix3_from_event_to_histo_hdf5.py @@ -103,7 +103,9 @@ def display_infos(self): vbox = widgets.VBox( [ widgets.Label("Metadata"), - widgets.Textarea(value=metadata, disabled=True, layout=widgets.Layout(height="200px")), + widgets.Textarea( + value=metadata, disabled=True, layout=widgets.Layout(height="200px") + ), ] ) display(vbox) @@ -111,14 +113,18 @@ def display_infos(self): def define_detector(self): self.width_ui = widgets.IntText(value=1024, description="Width") self.height_ui = widgets.IntText(value=1024, description="Height") - vbox = widgets.VBox([widgets.Label("MCP detector size:"), self.height_ui, self.width_ui]) + vbox = widgets.VBox( + [widgets.Label("MCP detector size:"), self.height_ui, self.width_ui] + ) display(vbox) def select_binning_parameter(self): self.nbr_bin_ui = widgets.IntText(value=1000, description="Nbr of bins:") display(self.nbr_bin_ui) - self.range_to_use = widgets.IntSlider(value=50, max=100, min=1, description="% to use") + self.range_to_use = widgets.IntSlider( + value=50, max=100, min=1, description="% to use" + ) display(self.range_to_use) def bins(self): @@ -227,15 +233,21 @@ def define_output_filename(self): input_nexus_filename = os.path.basename(self.input_nexus_file_name) export_id = widgets.HBox( [ - widgets.Label("Output file name:", layout=widgets.Layout(width="150px")), - widgets.Text(value=input_nexus_filename, layout=widgets.Layout(width="300px")), + widgets.Label( + "Output file name:", layout=widgets.Layout(width="150px") + ), + widgets.Text( + value=input_nexus_filename, layout=widgets.Layout(width="300px") + ), ] ) display(export_id) self.output_file_name_id = export_id.children[1] def select_output_location(self): - o_output_folder = FileFolderBrowser(working_dir=self.working_dir, next_function=self.export_h5) + o_output_folder = FileFolderBrowser( + working_dir=self.working_dir, next_function=self.export_h5 + ) o_output_folder.select_output_folder(instruction="Select output folder ...") def export_h5(self, output_folder): @@ -260,10 +272,16 @@ def export_h5(self, output_folder): f.create_dataset("entry/histo/number_of_bins", data=self.nbr_bins) f.create_dataset("entry/histo/tof_ns", data=self.bins_tof) f.create_group("entry/infos") - f.create_dataset("entry/infos/input_nexus_filename", data=self.input_nexus_file_name) + f.create_dataset( + "entry/infos/input_nexus_filename", data=self.input_nexus_file_name + ) display(HTML("Writing HDF5 file .... Done!")) display( - HTML('hdf5 file created:' + full_output_filename + "!") + HTML( + 'hdf5 file created:' + + full_output_filename + + "!" + ) ) logging.info(f"hdf5 file created: {full_output_filename}") diff --git a/notebooks/__code/timepix3_histo_hdf5_mcp_detector/fit_regions.py b/notebooks/__code/timepix3_histo_hdf5_mcp_detector/fit_regions.py index b7eea468e..0c96b2323 100755 --- a/notebooks/__code/timepix3_histo_hdf5_mcp_detector/fit_regions.py +++ b/notebooks/__code/timepix3_histo_hdf5_mcp_detector/fit_regions.py @@ -140,7 +140,10 @@ def high_lambda(self): self.fit_dict["a0"] = {"value": a0_value, "error": a0_error} self.fit_dict["b0"] = {"value": b0_value, "error": b0_error} - self.fit_dict[FittingRegions.high_lambda] = {"xaxis": xaxis, "yaxis": yaxis_fitted} + self.fit_dict[FittingRegions.high_lambda] = { + "xaxis": xaxis, + "yaxis": yaxis_fitted, + } def low_lambda(self): logging.info("fitting low lambda:") @@ -181,12 +184,19 @@ def low_lambda(self): logging.info(f"\t{bhkl_error =}") yaxis_fitted = kropff_low_lambda( - xaxis, self.fit_dict["a0"]["value"], self.fit_dict["b0"]["value"], ahkl_value, bhkl_value + xaxis, + self.fit_dict["a0"]["value"], + self.fit_dict["b0"]["value"], + ahkl_value, + bhkl_value, ) self.fit_dict["ahkl"] = {"value": ahkl_value, "error": ahkl_error} self.fit_dict["bhkl"] = {"value": bhkl_value, "error": bhkl_error} - self.fit_dict[FittingRegions.low_lambda] = {"xaxis": xaxis, "yaxis": yaxis_fitted} + self.fit_dict[FittingRegions.low_lambda] = { + "xaxis": xaxis, + "yaxis": yaxis_fitted, + } def bragg_peak(self): self.bragg_peak_fix_lambda() @@ -285,7 +295,9 @@ def bragg_peak(self): def bragg_peak_fix_lambda(self): logging.info("Fitting bragg peak with a fixed initial lambda_hkl:") - gmodel = Model(kropff_bragg_peak_tof, nan_policy="propagate", independent_vars=["lda"]) + gmodel = Model( + kropff_bragg_peak_tof, nan_policy="propagate", independent_vars=["lda"] + ) lambda_hkl = self.lambdahkl tau = self.tau @@ -294,8 +306,12 @@ def bragg_peak_fix_lambda(self): left_peak_index = self.left_edge_index right_peak_index = self.right_edge_index - xaxis = copy.deepcopy(self.x_axis_to_fit)[left_peak_index : right_peak_index + 1] - yaxis = copy.deepcopy(self.y_axis_to_fit)[left_peak_index : right_peak_index + 1] + xaxis = copy.deepcopy(self.x_axis_to_fit)[ + left_peak_index : right_peak_index + 1 + ] + yaxis = copy.deepcopy(self.y_axis_to_fit)[ + left_peak_index : right_peak_index + 1 + ] yaxis = -np.log(yaxis) logging.info(f"{xaxis =}") @@ -331,9 +347,14 @@ def bragg_peak_fix_lambda(self): logging.info(f"\t{tau_value =}") logging.info(f"\t{tau_error =}") - yaxis_fitted = kropff_bragg_peak_tof(xaxis, a0, b0, ahkl, bhkl, ldahkl_value, sigma_value, tau_value) + yaxis_fitted = kropff_bragg_peak_tof( + xaxis, a0, b0, ahkl, bhkl, ldahkl_value, sigma_value, tau_value + ) self.fit_dict["lambdahkl"] = {"value": ldahkl_value, "error": ldahkl_error} self.fit_dict["sigma"] = {"value": sigma_value, "error": sigma_error} self.fit_dict["tau"] = {"value": tau_value, "error": tau_error} - self.fit_dict[FittingRegions.bragg_peak] = {"xaxis": xaxis, "yaxis": yaxis_fitted} + self.fit_dict[FittingRegions.bragg_peak] = { + "xaxis": xaxis, + "yaxis": yaxis_fitted, + } diff --git a/notebooks/__code/timepix3_histo_hdf5_mcp_detector/timepix3_histo_hdf5_mcp_detector.py b/notebooks/__code/timepix3_histo_hdf5_mcp_detector/timepix3_histo_hdf5_mcp_detector.py index e53c64234..ede647cd8 100755 --- a/notebooks/__code/timepix3_histo_hdf5_mcp_detector/timepix3_histo_hdf5_mcp_detector.py +++ b/notebooks/__code/timepix3_histo_hdf5_mcp_detector/timepix3_histo_hdf5_mcp_detector.py @@ -49,7 +49,9 @@ class Timepix3HistoHdf5McpDetector: default_parameters = { JSONKeys.dSD_m: 19.855, - JSONKeys.rois_selected: {0: {JSONKeys.x0: 467, JSONKeys.y0: 99, JSONKeys.x1: 975, JSONKeys.y1: 429}}, + JSONKeys.rois_selected: { + 0: {JSONKeys.x0: 467, JSONKeys.y0: 99, JSONKeys.x1: 975, JSONKeys.y1: 429} + }, JSONKeys.offset_micros: 0, JSONKeys.time_shift: 0, JSONKeys.element: "Ni", @@ -103,7 +105,11 @@ def hdf5_or_config_file_input(self): ) display(self.toggle_button) - validate_button = widgets.Button(description="SELECT", icon="folder-open", layout=widgets.Layout(width="310px")) + validate_button = widgets.Button( + description="SELECT", + icon="folder-open", + layout=widgets.Layout(width="310px"), + ) display(validate_button) validate_button.on_click(self.input_selection_made) @@ -174,11 +180,19 @@ def load_config(self, config_file_name): self.default_parameters[JSONKeys.fitting_parameters][JSONKeys.a0] = float(a0) self.default_parameters[JSONKeys.fitting_parameters][JSONKeys.b0] = float(b0) - self.default_parameters[JSONKeys.fitting_parameters][JSONKeys.ahkl] = float(ahkl) - self.default_parameters[JSONKeys.fitting_parameters][JSONKeys.bhkl] = float(bhkl) - self.default_parameters[JSONKeys.fitting_parameters][JSONKeys.lambdahkl] = float(lambdahkl) + self.default_parameters[JSONKeys.fitting_parameters][JSONKeys.ahkl] = float( + ahkl + ) + self.default_parameters[JSONKeys.fitting_parameters][JSONKeys.bhkl] = float( + bhkl + ) + self.default_parameters[JSONKeys.fitting_parameters][JSONKeys.lambdahkl] = ( + float(lambdahkl) + ) self.default_parameters[JSONKeys.fitting_parameters][JSONKeys.tau] = float(tau) - self.default_parameters[JSONKeys.fitting_parameters][JSONKeys.sigma] = float(sigma) + self.default_parameters[JSONKeys.fitting_parameters][JSONKeys.sigma] = float( + sigma + ) display( HTML( @@ -197,7 +211,9 @@ def load_nexus(self, nexus_file_name=None): with h5py.File(nexus_file_name, "r") as f: self.stack = np.array(f["entry"]["histo"]["stack"]) - self.time_spectra = np.array(f["entry"]["histo"]["tof_ns"]) / 1000 # to convert to micros + self.time_spectra = ( + np.array(f["entry"]["histo"]["tof_ns"]) / 1000 + ) # to convert to micros def preview_integrated_stack(self): self.integrated_stack = self.stack.sum(axis=0) @@ -260,7 +276,12 @@ def calculate_and_display_profile(self): total_pixels_in_rois += width * height _rect = patches.Rectangle( - (x0, y0), x1 - x0, y1 - y0, linewidth=1, edgecolor=list_matplotlib_colors[_roi_index], facecolor="none" + (x0, y0), + x1 - x0, + y1 - y0, + linewidth=1, + edgecolor=list_matplotlib_colors[_roi_index], + facecolor="none", ) rect_array.append(_rect) @@ -273,7 +294,9 @@ def calculate_and_display_profile(self): x1 = rois_selected[_roi_index]["x1"] y1 = rois_selected[_roi_index]["y1"] - total_counts_for_this_image += np.nansum(_image[y0 : y1 + 1, x0 : x1 + 1]) + total_counts_for_this_image += np.nansum( + _image[y0 : y1 + 1, x0 : x1 + 1] + ) profile.append(total_counts_for_this_image / total_pixels_in_rois) @@ -296,7 +319,9 @@ def plot_profile(x_axis, dSD_m, offset_micros, time_shift, element): _handler = BraggEdgeLibrary(material=[element], number_of_bragg_edges=6) else: # Ta _handler = BraggEdgeLibrary( - new_material=[{"name": "Ta", "lattice": 3.3058, "crystal_structure": "BCC"}], + new_material=[ + {"name": "Ta", "lattice": 3.3058, "crystal_structure": "BCC"} + ], number_of_bragg_edges=6, ) @@ -363,7 +388,10 @@ def plot_profile(x_axis, dSD_m, offset_micros, time_shift, element): readout_format=".3f", ), offset_micros=widgets.IntSlider( - value=self.default_parameters[JSONKeys.offset_micros], min=0, max=15000, continuous_update=False + value=self.default_parameters[JSONKeys.offset_micros], + min=0, + max=15000, + continuous_update=False, ), time_shift=widgets.IntSlider( value=self.default_parameters[JSONKeys.time_shift], @@ -372,7 +400,9 @@ def plot_profile(x_axis, dSD_m, offset_micros, time_shift, element): step=1, continuous_update=False, ), - element=widgets.RadioButtons(options=LIST_ELEMENTS, value=self.default_parameters[JSONKeys.element]), + element=widgets.RadioButtons( + options=LIST_ELEMENTS, value=self.default_parameters[JSONKeys.element] + ), ) display(self.v) @@ -382,7 +412,11 @@ def select_peak_to_fit(self): 'Full range of peak to fit (left_range, right_range)' ) ) - display(HTML('Peak threshold (left_peak, right_peak)')) + display( + HTML( + 'Peak threshold (left_peak, right_peak)' + ) + ) lambda_x_axis, profile_shifted = self.prepare_data() self.lambda_x_axis = lambda_x_axis @@ -476,17 +510,25 @@ def prepare_data(self): def fitting(self): # setup parameters - display(HTML('Init parameters')) + display( + HTML('Init parameters') + ) text_width = "80px" # px display(HTML('High lambda')) default_a0 = self.default_parameters[JSONKeys.fitting_parameters][JSONKeys.a0] self.a0_layout = widgets.HBox( - [widgets.Label("a\u2080"), widgets.IntText(default_a0, layout=widgets.Layout(width=text_width))] + [ + widgets.Label("a\u2080"), + widgets.IntText(default_a0, layout=widgets.Layout(width=text_width)), + ] ) default_b0 = self.default_parameters[JSONKeys.fitting_parameters][JSONKeys.b0] self.b0_layout = widgets.HBox( - [widgets.Label("b\u2080"), widgets.IntText(default_b0, layout=widgets.Layout(width=text_width))] + [ + widgets.Label("b\u2080"), + widgets.IntText(default_b0, layout=widgets.Layout(width=text_width)), + ] ) high_layout = widgets.VBox([self.a0_layout, self.b0_layout]) display(high_layout) @@ -494,14 +536,18 @@ def fitting(self): display(HTML("")) display(HTML('Low lambda')) - default_ahkl = self.default_parameters[JSONKeys.fitting_parameters][JSONKeys.ahkl] + default_ahkl = self.default_parameters[JSONKeys.fitting_parameters][ + JSONKeys.ahkl + ] self.ahkl_layout = widgets.HBox( [ widgets.Label("a\u2095\u2096\u2097"), widgets.IntText(default_ahkl, layout=widgets.Layout(width=text_width)), ] ) - default_bhkl = self.default_parameters[JSONKeys.fitting_parameters][JSONKeys.bhkl] + default_bhkl = self.default_parameters[JSONKeys.fitting_parameters][ + JSONKeys.bhkl + ] self.bhkl_layout = widgets.HBox( [ widgets.Label("b\u2095\u2096\u2097"), @@ -514,27 +560,45 @@ def fitting(self): display(HTML("")) display(HTML('Bragg peak')) - default_lambdahkl = self.default_parameters[JSONKeys.fitting_parameters][JSONKeys.lambdahkl] + default_lambdahkl = self.default_parameters[JSONKeys.fitting_parameters][ + JSONKeys.lambdahkl + ] self.lambdahkl_layout = widgets.HBox( [ widgets.Label("\u03bb\u2095\u2096\u2097"), - widgets.FloatText(default_lambdahkl, layout=widgets.Layout(width=text_width)), + widgets.FloatText( + default_lambdahkl, layout=widgets.Layout(width=text_width) + ), ] ) default_tau = self.default_parameters[JSONKeys.fitting_parameters][JSONKeys.tau] self.tau_layout = widgets.HBox( - [widgets.Label("\u03c4"), widgets.FloatText(default_tau, layout=widgets.Layout(width=text_width))] + [ + widgets.Label("\u03c4"), + widgets.FloatText(default_tau, layout=widgets.Layout(width=text_width)), + ] ) - default_sigma = self.default_parameters[JSONKeys.fitting_parameters][JSONKeys.sigma] + default_sigma = self.default_parameters[JSONKeys.fitting_parameters][ + JSONKeys.sigma + ] self.sigma_layout = widgets.HBox( - [widgets.Label("\u03c3"), widgets.FloatText(default_sigma, layout=widgets.Layout(width=text_width))] + [ + widgets.Label("\u03c3"), + widgets.FloatText( + default_sigma, layout=widgets.Layout(width=text_width) + ), + ] + ) + bragg_peak_layout = widgets.VBox( + [self.lambdahkl_layout, self.tau_layout, self.sigma_layout] ) - bragg_peak_layout = widgets.VBox([self.lambdahkl_layout, self.tau_layout, self.sigma_layout]) display(bragg_peak_layout) display(widgets.HTML(" index: {left_peak}") - logging.info(f"\tpeak right_range: {right_lambda_range}" + "\u212b " + f"-> index: {right_peak}") - logging.info(f"\tedge left_range: {left_lambda_edge}" + "\u212b " + f"-> index: {left_edge}") - logging.info(f"\tedge right_range: {right_lambda_edge}" + "\u212b " + f"-> index: {right_edge}") + logging.info( + f"\tpeak left_range: {left_lambda_range}" + + "\u212b " + + f"-> index: {left_peak}" + ) + logging.info( + f"\tpeak right_range: {right_lambda_range}" + + "\u212b " + + f"-> index: {right_peak}" + ) + logging.info( + f"\tedge left_range: {left_lambda_edge}" + + "\u212b " + + f"-> index: {left_edge}" + ) + logging.info( + f"\tedge right_range: {right_lambda_edge}" + + "\u212b " + + f"-> index: {right_edge}" + ) logging.info(f"\tlambda_x_axis: {lambda_x_axis}") logging.info(f"\tsize of profile: {len(profile_shifted)}") @@ -604,7 +684,9 @@ def fit_peak(self, _): # display full spectrum list_matplotlib_colors = Color.list_matplotlib - ax4.plot(x_axis_to_fit, -np.log(y_axis_to_fit), "*", color=list_matplotlib_colors[0]) + ax4.plot( + x_axis_to_fit, -np.log(y_axis_to_fit), "*", color=list_matplotlib_colors[0] + ) max_counts = 0 dict_of_fit_dict = {} @@ -630,13 +712,21 @@ def fit_peak(self, _): # display fitting # high lambda - x_axis_fitted_high_lambda = o_fit_regions.fit_dict[FittingRegions.high_lambda]["xaxis"] - y_axis_fitted_high_lambda = o_fit_regions.fit_dict[FittingRegions.high_lambda]["yaxis"] + x_axis_fitted_high_lambda = o_fit_regions.fit_dict[FittingRegions.high_lambda][ + "xaxis" + ] + y_axis_fitted_high_lambda = o_fit_regions.fit_dict[FittingRegions.high_lambda][ + "yaxis" + ] ax4.plot(x_axis_fitted_high_lambda, y_axis_fitted_high_lambda, "r-") # low lambda - x_axis_fitted_low_lambda = o_fit_regions.fit_dict[FittingRegions.low_lambda]["xaxis"] - y_axis_fitted_low_lambda = o_fit_regions.fit_dict[FittingRegions.low_lambda]["yaxis"] + x_axis_fitted_low_lambda = o_fit_regions.fit_dict[FittingRegions.low_lambda][ + "xaxis" + ] + y_axis_fitted_low_lambda = o_fit_regions.fit_dict[FittingRegions.low_lambda][ + "yaxis" + ] ax4.plot(x_axis_fitted_low_lambda, y_axis_fitted_low_lambda, "y-") # bragg peak @@ -666,7 +756,10 @@ def fit_peak(self, _): _handler = BraggEdgeLibrary(material=[element], number_of_bragg_edges=6) else: # Ta _handler = BraggEdgeLibrary( - new_material=[{"name": "Ta", "lattice": 3.3058, "crystal_structure": "BCC"}], number_of_bragg_edges=6 + new_material=[ + {"name": "Ta", "lattice": 3.3058, "crystal_structure": "BCC"} + ], + number_of_bragg_edges=6, ) self.bragg_edges = _handler.bragg_edges @@ -688,7 +781,9 @@ def fit_peak(self, _): def saving_session(self): # select output location - o_output_folder = FileFolderBrowser(working_dir=self.working_dir, next_function=self.export_session) + o_output_folder = FileFolderBrowser( + working_dir=self.working_dir, next_function=self.export_session + ) o_output_folder.select_output_folder(instruction="Select output folder ...") def export_session(self, output_folder=None): @@ -699,7 +794,9 @@ def export_session(self, output_folder=None): base, _ = os.path.splitext(os.path.basename(input_nexus_filename)) current_time = get_current_time_in_special_file_name_format() - output_file_name = os.path.abspath(os.path.join(output_folder, f"config_{base}_{current_time}.cfg")) + output_file_name = os.path.abspath( + os.path.join(output_folder, f"config_{base}_{current_time}.cfg") + ) # record all parameters rois_selected = self.rois_selected diff --git a/notebooks/__code/timepix3_image_statistics/main.py b/notebooks/__code/timepix3_image_statistics/main.py index 7ac3a57ef..b3dd2b450 100644 --- a/notebooks/__code/timepix3_image_statistics/main.py +++ b/notebooks/__code/timepix3_image_statistics/main.py @@ -7,13 +7,11 @@ import numpy as np import logging as notebook_logging import matplotlib.pyplot as plt -from matplotlib.patches import Rectangle from ipywidgets import interactive from ipywidgets import widgets from PIL import Image from __code.ipywe import fileselector -from __code._utilities.file import get_full_log_file_name from __code.timepix3_image_statistics import config # Setup plotting @@ -24,11 +22,9 @@ class Timepix3ImageStatistics: - - def __init__(self, working_dir=None, debug=False): self.working_dir = working_dir - self.debug = debug + self.debug = debug self.initialize() def initialize(self): @@ -72,11 +68,11 @@ def load_data(self): def display_integrated_image(self): fig, ax = plt.subplots(figsize=(8, 8)) - ax.imshow(self.integrated_image, cmap='viridis', origin='lower') - ax.set_title('Integrated Image') - ax.set_xlabel('X (pixels)') - ax.set_ylabel('Y (pixels)') - fig.colorbar(ax.images[0], ax=ax, label='Counts') + ax.imshow(self.integrated_image, cmap="viridis", origin="lower") + ax.set_title("Integrated Image") + ax.set_xlabel("X (pixels)") + ax.set_ylabel("Y (pixels)") + fig.colorbar(ax.images[0], ax=ax, label="Counts") plt.show() def display_chips(self): @@ -84,89 +80,89 @@ def display_chips(self): self.chip2 = self.integrated_image[0:256, 0:256] self.chip3 = self.integrated_image[256:, 0:256] self.chip4 = self.integrated_image[256:, 256:] - - cmap = 'viridis' # 'gray', 'viridis', 'plasma', 'inferno', 'magma', 'cividis' + + cmap = "viridis" # 'gray', 'viridis', 'plasma', 'inferno', 'magma', 'cividis' fig, axs = plt.subplots(2, 2, figsize=(10, 8)) im01 = axs[0, 1].imshow(self.chip1, cmap=cmap, vmin=0) fig.colorbar(im01, ax=axs[0, 1]) - axs[0, 1].set_title('Chip 1') + axs[0, 1].set_title("Chip 1") im02 = axs[0, 0].imshow(self.chip2, cmap=cmap, vmin=0) fig.colorbar(im02, ax=axs[0, 0]) - axs[0, 0].set_title('Chip 2') + axs[0, 0].set_title("Chip 2") im03 = axs[1, 0].imshow(self.chip3, cmap=cmap, vmin=0) fig.colorbar(im03, ax=axs[1, 0]) - axs[1, 0].set_title('Chip 3') + axs[1, 0].set_title("Chip 3") im04 = axs[1, 1].imshow(self.chip4, cmap=cmap, vmin=0) fig.colorbar(im04, ax=axs[1, 1]) - axs[1, 1].set_title('Chip 4') + axs[1, 1].set_title("Chip 4") fig.tight_layout() # compare histograms of each chips fig, ax = plt.subplots(figsize=(8, 6)) - ax.hist(self.chip1.ravel(), bins=256, alpha=0.5, label='Chip 1') - ax.hist(self.chip2.ravel(), bins=256, alpha=0.5, label='Chip 2') - ax.hist(self.chip3.ravel(), bins=256, alpha=0.5, label='Chip 3') - ax.hist(self.chip4.ravel(), bins=256, alpha=0.5, label='Chip 4') - ax.set_yscale('log') - ax.set_xlabel('Pixel Value') - ax.set_ylabel('Frequency') + ax.hist(self.chip1.ravel(), bins=256, alpha=0.5, label="Chip 1") + ax.hist(self.chip2.ravel(), bins=256, alpha=0.5, label="Chip 2") + ax.hist(self.chip3.ravel(), bins=256, alpha=0.5, label="Chip 3") + ax.hist(self.chip4.ravel(), bins=256, alpha=0.5, label="Chip 4") + ax.set_yscale("log") + ax.set_xlabel("Pixel Value") + ax.set_ylabel("Frequency") ax.legend() plt.show() def process_chips(self): - stat = {'min': [], 'max': [], 'mean': [], 'median': [], 'std': [], 'sum': []} + stat = {"min": [], "max": [], "mean": [], "median": [], "std": [], "sum": []} Timepix3ImageStatistics.chip_stats(self.chip1, stat) Timepix3ImageStatistics.chip_stats(self.chip2, stat) Timepix3ImageStatistics.chip_stats(self.chip3, stat) Timepix3ImageStatistics.chip_stats(self.chip4, stat) fig, axs = plt.subplots(2, 3, figsize=(10, 7)) - axs[0][0].plot(stat['min'], marker='o') - axs[0][0].set_title('Min') + axs[0][0].plot(stat["min"], marker="o") + axs[0][0].set_title("Min") axs[0][0].set_xticks([0, 1, 2, 3]) - axs[0][0].set_xticklabels(['Chip 1', 'Chip 2', 'Chip 3', 'Chip 4']) + axs[0][0].set_xticklabels(["Chip 1", "Chip 2", "Chip 3", "Chip 4"]) - axs[0][1].plot(stat['max'], marker='o') - axs[0][1].set_title('Max') + axs[0][1].plot(stat["max"], marker="o") + axs[0][1].set_title("Max") axs[0][1].set_xticks([0, 1, 2, 3]) - axs[0][1].set_xticklabels(['Chip 1', 'Chip 2', 'Chip 3', 'Chip 4']) + axs[0][1].set_xticklabels(["Chip 1", "Chip 2", "Chip 3", "Chip 4"]) - axs[0][2].plot(stat['mean'], marker='o') - axs[0][2].set_title('Mean') + axs[0][2].plot(stat["mean"], marker="o") + axs[0][2].set_title("Mean") axs[0][2].set_xticks([0, 1, 2, 3]) - axs[0][2].set_xticklabels(['Chip 1', 'Chip 2', 'Chip 3', 'Chip 4']) + axs[0][2].set_xticklabels(["Chip 1", "Chip 2", "Chip 3", "Chip 4"]) - axs[1][0].plot(stat['median'], marker='o') - axs[1][0].set_title('Median') + axs[1][0].plot(stat["median"], marker="o") + axs[1][0].set_title("Median") axs[1][0].set_xticks([0, 1, 2, 3]) - axs[1][0].set_xticklabels(['Chip 1', 'Chip 2', 'Chip 3', 'Chip 4']) + axs[1][0].set_xticklabels(["Chip 1", "Chip 2", "Chip 3", "Chip 4"]) - axs[1][1].plot(stat['std'], marker='o') - axs[1][1].set_title('Standard Deviation') + axs[1][1].plot(stat["std"], marker="o") + axs[1][1].set_title("Standard Deviation") axs[1][1].set_xticks([0, 1, 2, 3]) - axs[1][1].set_xticklabels(['Chip 1', 'Chip 2', 'Chip 3', 'Chip 4']) + axs[1][1].set_xticklabels(["Chip 1", "Chip 2", "Chip 3", "Chip 4"]) - axs[1][2].plot(stat['sum'], marker='o') - axs[1][2].set_title('Sum') + axs[1][2].plot(stat["sum"], marker="o") + axs[1][2].set_title("Sum") axs[1][2].set_xticks([0, 1, 2, 3]) - axs[1][2].set_xticklabels(['Chip 1', 'Chip 2', 'Chip 3', 'Chip 4']) + axs[1][2].set_xticklabels(["Chip 1", "Chip 2", "Chip 3", "Chip 4"]) fig.tight_layout() # let's get the statistics of each chip @staticmethod def chip_stats(chip, stat): - stat['min'].append(np.min(chip)) - stat['max'].append(np.max(chip)) - stat['mean'].append(np.mean(chip)) - stat['median'].append(np.median(chip)) - stat['std'].append(np.std(chip)) - stat['sum'].append(np.sum(chip)) + stat["min"].append(np.min(chip)) + stat["max"].append(np.max(chip)) + stat["mean"].append(np.mean(chip)) + stat["median"].append(np.median(chip)) + stat["std"].append(np.std(chip)) + stat["sum"].append(np.sum(chip)) def locate_dead_pixels(self): # locate all the dead pixels (value = 0 ) @@ -175,7 +171,8 @@ def locate_dead_pixels(self): dead_pixels_chip3 = np.where(self.chip3 == 0) dead_pixels_chip4 = np.where(self.chip4 == 0) - display(HTML(f""" + display( + HTML(f"""

Dead Pixels Information

@@ -184,128 +181,159 @@ def locate_dead_pixels(self):
ChipNumber of Dead Pixels
Chip 3{len(dead_pixels_chip3[0])}
Chip 4{len(dead_pixels_chip4[0])}
- """)) + """) + ) # highlight the dead pixels in each chip - cmap = 'viridis' + cmap = "viridis" fig, axs = plt.subplots(2, 2, figsize=(10, 8)) im01 = axs[0, 1].imshow(self.chip1, cmap=cmap) - axs[0, 1].scatter(dead_pixels_chip1[1], dead_pixels_chip1[0], color='y', s=1) + axs[0, 1].scatter(dead_pixels_chip1[1], dead_pixels_chip1[0], color="y", s=1) fig.colorbar(im01, ax=axs[0, 1]) - axs[0, 1].set_title(f'Chip 1 ({len(dead_pixels_chip1[0])} dead pixels)') + axs[0, 1].set_title(f"Chip 1 ({len(dead_pixels_chip1[0])} dead pixels)") im02 = axs[0, 0].imshow(self.chip2, cmap=cmap) - axs[0, 0].scatter(dead_pixels_chip2[1], dead_pixels_chip2[0], color='y', s=1) + axs[0, 0].scatter(dead_pixels_chip2[1], dead_pixels_chip2[0], color="y", s=1) fig.colorbar(im02, ax=axs[0, 0]) - axs[0, 0].set_title(f'Chip 2 ({len(dead_pixels_chip2[0])} dead pixels)') + axs[0, 0].set_title(f"Chip 2 ({len(dead_pixels_chip2[0])} dead pixels)") im03 = axs[1, 0].imshow(self.chip3, cmap=cmap) - axs[1, 0].scatter(dead_pixels_chip3[1], dead_pixels_chip3[0], color='y', s=1) - fig.colorbar(im03, ax=axs[1, 0]) - axs[1, 0].set_title(f'Chip 3 ({len(dead_pixels_chip3[0])} dead pixels)') + axs[1, 0].scatter(dead_pixels_chip3[1], dead_pixels_chip3[0], color="y", s=1) + fig.colorbar(im03, ax=axs[1, 0]) + axs[1, 0].set_title(f"Chip 3 ({len(dead_pixels_chip3[0])} dead pixels)") im04 = axs[1, 1].imshow(self.chip4, cmap=cmap) - axs[1, 1].scatter(dead_pixels_chip4[1], dead_pixels_chip4[0], color='y', s=1) + axs[1, 1].scatter(dead_pixels_chip4[1], dead_pixels_chip4[0], color="y", s=1) fig.colorbar(im04, ax=axs[1, 1]) - axs[1, 1].set_title(f'Chip 4 ({len(dead_pixels_chip4[0])} dead pixels)') + axs[1, 1].set_title(f"Chip 4 ({len(dead_pixels_chip4[0])} dead pixels)") plt.show() def locate_high_pixels(self): - default_threshold = 0.1 * np.max(self.integrated_image) # 10% of max value + default_threshold = 0.1 * np.max(self.integrated_image) # 10% of max value def display_high_pixels(threshold): - fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(10, 8)) - im1 = axs[0, 1].imshow(self.chip1, cmap='viridis', origin='lower') + im1 = axs[0, 1].imshow(self.chip1, cmap="viridis", origin="lower") high_pixels1 = np.where(self.chip1 >= threshold) - display(HTML(f"Number of high pixels in chip1 (>= {threshold}): {len(high_pixels1[0])}")) - axs[0, 1].scatter(high_pixels1[1], high_pixels1[0], color='r', s=1) - axs[0, 1].set_title(f'High Pixels (>= {threshold})') - axs[0, 1].set_xlabel('X (pixels)') - axs[0, 1].set_ylabel('Y (pixels)') - axs[0, 1].set_title('Chip 1') + display( + HTML( + f"Number of high pixels in chip1 (>= {threshold}): {len(high_pixels1[0])}" + ) + ) + axs[0, 1].scatter(high_pixels1[1], high_pixels1[0], color="r", s=1) + axs[0, 1].set_title(f"High Pixels (>= {threshold})") + axs[0, 1].set_xlabel("X (pixels)") + axs[0, 1].set_ylabel("Y (pixels)") + axs[0, 1].set_title("Chip 1") fig.colorbar(im1, ax=axs[0, 1]) high_pixels2 = np.where(self.chip2 >= threshold) - display(HTML(f"Number of high pixels in chip2 (>= {threshold}): {len(high_pixels2[0])}")) - im2 = axs[0, 0].imshow(self.chip2, cmap='viridis', origin='lower') - axs[0, 0].scatter(high_pixels2[1], high_pixels2[0], color='r', s=1) - axs[0, 0].set_title(f'High Pixels (>= {threshold})') - axs[0, 0].set_xlabel('X (pixels)') - axs[0, 0].set_ylabel('Y (pixels)') - axs[0, 0].set_title('Chip 2') + display( + HTML( + f"Number of high pixels in chip2 (>= {threshold}): {len(high_pixels2[0])}" + ) + ) + im2 = axs[0, 0].imshow(self.chip2, cmap="viridis", origin="lower") + axs[0, 0].scatter(high_pixels2[1], high_pixels2[0], color="r", s=1) + axs[0, 0].set_title(f"High Pixels (>= {threshold})") + axs[0, 0].set_xlabel("X (pixels)") + axs[0, 0].set_ylabel("Y (pixels)") + axs[0, 0].set_title("Chip 2") fig.colorbar(im2, ax=axs[0, 0]) - im3 = axs[1, 0].imshow(self.chip3, cmap='viridis', origin='lower') + im3 = axs[1, 0].imshow(self.chip3, cmap="viridis", origin="lower") high_pixels3 = np.where(self.chip3 >= threshold) - display(HTML(f"Number of high pixels in chip3 (>= {threshold}): {len(high_pixels3[0])}")) - axs[1, 0].scatter(high_pixels3[1], high_pixels3[0], color='r', s=1) - axs[1, 0].set_title(f'High Pixels (>= {threshold})') - axs[1, 0].set_xlabel('X (pixels)') - axs[1, 0].set_ylabel('Y (pixels)') - axs[1, 0].set_title('Chip 3') + display( + HTML( + f"Number of high pixels in chip3 (>= {threshold}): {len(high_pixels3[0])}" + ) + ) + axs[1, 0].scatter(high_pixels3[1], high_pixels3[0], color="r", s=1) + axs[1, 0].set_title(f"High Pixels (>= {threshold})") + axs[1, 0].set_xlabel("X (pixels)") + axs[1, 0].set_ylabel("Y (pixels)") + axs[1, 0].set_title("Chip 3") fig.colorbar(im3, ax=axs[1, 0]) - im4 = axs[1, 1].imshow(self.chip4, cmap='viridis', origin='lower') + im4 = axs[1, 1].imshow(self.chip4, cmap="viridis", origin="lower") high_pixels4 = np.where(self.chip4 >= threshold) - display(HTML(f"Number of high pixels in chip4 (>= {threshold}): {len(high_pixels4[0])}")) - axs[1, 1].scatter(high_pixels4[1], high_pixels4[0], color='r', s=1) - axs[1, 1].set_title(f'High Pixels (>= {threshold})') - axs[1, 1].set_xlabel('X (pixels)') - axs[1, 1].set_ylabel('Y (pixels)') - axs[1, 1].set_title('Chip 4') + display( + HTML( + f"Number of high pixels in chip4 (>= {threshold}): {len(high_pixels4[0])}" + ) + ) + axs[1, 1].scatter(high_pixels4[1], high_pixels4[0], color="r", s=1) + axs[1, 1].set_title(f"High Pixels (>= {threshold})") + axs[1, 1].set_xlabel("X (pixels)") + axs[1, 1].set_ylabel("Y (pixels)") + axs[1, 1].set_title("Chip 4") fig.colorbar(im4, ax=axs[1, 1]) plt.tight_layout() plt.show() - display_plot = interactive(display_high_pixels, - threshold=widgets.IntSlider(min=0, - max=default_threshold, - value=np.max(self.integrated_image), - ), - ) + display_plot = interactive( + display_high_pixels, + threshold=widgets.IntSlider( + min=0, + max=default_threshold, + value=np.max(self.integrated_image), + ), + ) display(display_plot) - def after_tpx3_file_selection(self, folder_name): logging.info(f"TPX3 folder selected: {folder_name}") sub_folder_name = self.make_sure_its_the_correct_folder(folder_name) - logging.info(f"done running make_sure_its_the_correct_folder, Using folder: {sub_folder_name =}") - if not (folder_name is None): - self.get_file_infos(original_folder_name=folder_name, - sub_folder_name=sub_folder_name) + logging.info( + f"done running make_sure_its_the_correct_folder, Using folder: {sub_folder_name =}" + ) + if folder_name is not None: + self.get_file_infos( + original_folder_name=folder_name, sub_folder_name=sub_folder_name + ) # self.processing_tpx3(file_name=file_name) def make_sure_its_the_correct_folder(self, folder_name): list_of_tif_files = glob.glob(os.path.join(folder_name, "*.tif*")) if len(list_of_tif_files) > 0: - logging.info(f"Folder {folder_name} contains .tif files. We are good to go!") + logging.info( + f"Folder {folder_name} contains .tif files. We are good to go!" + ) return folder_name else: # trying one folder deeper - list_of_sub_folders = [f.path for f in os.scandir(folder_name) if f.is_dir()] + list_of_sub_folders = [ + f.path for f in os.scandir(folder_name) if f.is_dir() + ] for sub_folder in list_of_sub_folders: list_of_tif_files = glob.glob(os.path.join(sub_folder, "*.tif*")) if len(list_of_tif_files) > 0: - logging.info(f"Folder {folder_name} does not contain .tif files. Using sub-folder {sub_folder} instead") - display(HTML(f"Folder {folder_name} does not contain .tif files. Using sub-folder {sub_folder} instead")) + logging.info( + f"Folder {folder_name} does not contain .tif files. Using sub-folder {sub_folder} instead" + ) + display( + HTML( + f"Folder {folder_name} does not contain .tif files. Using sub-folder {sub_folder} instead" + ) + ) return sub_folder - logging.info(f"Folder {folder_name} does not contain .tif files. Please select another folder") + logging.info( + f"Folder {folder_name} does not contain .tif files. Please select another folder" + ) self.select_tpx3_folder() return None - + def get_file_infos(self, original_folder_name=None, sub_folder_name=None): - if sub_folder_name: logging.info(f"Getting folder info for: {sub_folder_name}") - + # Simulate getting file info self.file_info = { "original_folder_name": original_folder_name, "sub_folder_name": sub_folder_name, "base name": os.path.basename(sub_folder_name), - "path" : os.path.dirname(sub_folder_name), - "size": f"{os.path.getsize(sub_folder_name) / (1024*1024):.2f} MB", + "path": os.path.dirname(sub_folder_name), + "size": f"{os.path.getsize(sub_folder_name) / (1024 * 1024):.2f} MB", } # get modified time in human readable format @@ -318,18 +346,24 @@ def get_file_infos(self, original_folder_name=None, sub_folder_name=None): self.file_info["number of .tif files"] = len(list_of_tiff_files) # size of images total_size = sum(os.path.getsize(f) for f in list_of_tiff_files) - self.file_info["total size of .tif files"] = f"{total_size / (1024*1024):.2f} MB" + self.file_info["total size of .tif files"] = ( + f"{total_size / (1024 * 1024):.2f} MB" + ) # size of first image if len(list_of_tiff_files) > 0: - self.file_info["size of each .tif file"] = f"{os.path.getsize(list_of_tiff_files[0]) / (1024*1024):.2f} MB" + self.file_info["size of each .tif file"] = ( + f"{os.path.getsize(list_of_tiff_files[0]) / (1024 * 1024):.2f} MB" + ) else: self.file_info["size of each .tif file"] = "N/A" - logging.info(f"File info:") + logging.info("File info:") for _key in self.file_info: logging.info(f"\t{_key}: {self.file_info[_key]}") - display(HTML(""" + display( + HTML( + """

TPX3 Folder Information

@@ -343,26 +377,31 @@ def get_file_infos(self, original_folder_name=None, sub_folder_name=None):
PropertyValue
Total size of .tif files{}
Size of each .tif file{}
- """.format(self.file_info["original_folder_name"], - self.file_info["sub_folder_name"], - self.file_info["base name"], - self.file_info["path"], - self.file_info["size"], - self.file_info["modified"], - self.file_info["number of .tif files"], - self.file_info["total size of .tif files"], - self.file_info["size of each .tif file"]))) + """.format( + self.file_info["original_folder_name"], + self.file_info["sub_folder_name"], + self.file_info["base name"], + self.file_info["path"], + self.file_info["size"], + self.file_info["modified"], + self.file_info["number of .tif files"], + self.file_info["total size of .tif files"], + self.file_info["size of each .tif file"], + ) + ) + ) else: logging.info(f"Folder not found: {sub_folder_name}") def processing_tpx3(self, file_name=None): - - logging.info(f"Processing TPX3 file: {os.path.basename(file_name)} ... (be patient!)") + logging.info( + f"Processing TPX3 file: {os.path.basename(file_name)} ... (be patient!)" + ) start_time = time.time() - + # loading tpx3 file - hits_view = tdc.process_tpx3(file_name, parallel=True) + hits_view = tdc.process_tpx3(file_name, parallel=True) hits = np.array(hits_view, copy=False) self.hits = hits @@ -372,9 +411,11 @@ def processing_tpx3(self, file_name=None): display(HTML(f"Processing completed in {processing_time:.2f} seconds.")) # display result as a table - from_tof = hits['tof'].min() * 25 / 1e6 - to_tof = hits['tof'].max() * 25 / 1e6 - display(HTML(""" + from_tof = hits["tof"].min() * 25 / 1e6 + to_tof = hits["tof"].max() * 25 / 1e6 + display( + HTML( + """

TPX3 Processing Results

@@ -383,25 +424,35 @@ def processing_tpx3(self, file_name=None):
InfosValue
Y range{} - {}
TOF range (ms){:.3f} - {:.3f}
- """.format(len(hits), hits['x'].min(), hits['x'].max(), hits['y'].min(), hits['y'].max(), - from_tof, to_tof))) + """.format( + len(hits), + hits["x"].min(), + hits["x"].max(), + hits["y"].min(), + hits["y"].max(), + from_tof, + to_tof, + ) + ) + ) unique_chips, chip_counts = np.unique(hits["chip_id"], return_counts=True) - _text = '

Chips distribution (hits per chips)

' + \ - '' + _text = ( + "

Chips distribution (hits per chips)

" + + '
' + ) for chip, count in zip(unique_chips, chip_counts): percentage = 100 * count / len(hits) - _text += f'' - _text += '
Chip {chip}{count:,} hits ({percentage:.1f}%)
' + _text += f"Chip {chip}{count:,} hits ({percentage:.1f}%)" + _text += "" display(HTML(_text)) def select_sampling_percentage(self): if len(self.hits) > 100_000: label = widgets.Label("Select sampling percentage:") - self.sampling_percentage_ui = widgets.FloatSlider(min=0.01, - max=100, - value=0.1, - step=0.01) + self.sampling_percentage_ui = widgets.FloatSlider( + min=0.01, max=100, value=0.1, step=0.01 + ) hori_layout = widgets.HBox([label, self.sampling_percentage_ui]) self.apply_sampling = True display(hori_layout) @@ -410,32 +461,36 @@ def select_sampling_percentage(self): self.apply_sampling = False def display_image_with_roi(self): - self.generate_2d_hit_map() + self.generate_2d_hit_map() def generate_2d_hit_map(self): - sample_fraction = self.sampling_percentage_ui.value / 100. + sample_fraction = self.sampling_percentage_ui.value / 100.0 if self.apply_sampling: n_sample = int(len(self.hits) * sample_fraction) - sample_indices = np.random.choice(len(self.hits), size=n_sample, replace=False) + sample_indices = np.random.choice( + len(self.hits), size=n_sample, replace=False + ) hits_for_viz = self.hits[sample_indices] - display(HTML(f"Using {n_sample:,} sampled hits ({sample_fraction*100:.1f}%) for visualization")) + display( + HTML( + f"Using {n_sample:,} sampled hits ({sample_fraction * 100:.1f}%) for visualization" + ) + ) else: hits_for_viz = self.hits display(HTML(f"Using all {len(self.hits):,} hits for visualization")) - + # Create 2D histogram (bin by detector pixels) x_bins = np.arange(0, 515, 1) # 0 to 514 pixels y_bins = np.arange(0, 515, 1) # 0 to 514 pixels self.hist2d, self.x_edges, self.y_edges = np.histogram2d( - hits_for_viz["x"], hits_for_viz["y"], bins=[x_bins, y_bins]) + hits_for_viz["x"], hits_for_viz["y"], bins=[x_bins, y_bins] + ) def select_roi(self): pass - + def generate_histogram_and_select_roi(self): self.generate_2d_hit_map() self.select_roi() - - - diff --git a/notebooks/__code/timepix3_raw_to_profile_of_roi/main.py b/notebooks/__code/timepix3_raw_to_profile_of_roi/main.py index 67acebf74..d3f9216b9 100644 --- a/notebooks/__code/timepix3_raw_to_profile_of_roi/main.py +++ b/notebooks/__code/timepix3_raw_to_profile_of_roi/main.py @@ -5,8 +5,6 @@ import time import numpy as np import matplotlib.pyplot as plt -from matplotlib.patches import Rectangle -from ipywidgets import interactive from ipywidgets import widgets from __code.ipywe import fileselector @@ -21,7 +19,6 @@ class Timepix3RawToProfileOfRoi: - apply_sampling = False hist2d = None @@ -42,7 +39,6 @@ def __init__(self, working_dir=None, debug=False): logging.info("*** Starting a new session ***") logging.info(f"Debug mode is {self.debug}") print(f"log file: {self.log_file_name}") - def select_tpx3(self): if self.debug: @@ -58,34 +54,34 @@ def select_tpx3(self): ) self.nexus_ui.show() - def after_tpx3_file_selection(self, file_name): + def after_tpx3_file_selection(self, file_name): self.get_file_infos(file_name=file_name) self.processing_tpx3(file_name=file_name) def get_file_infos(self, file_name=None): - if file_name: logging.info(f"Getting file info for: {file_name}") # Simulate getting file info self.file_info = { "base name": os.path.basename(file_name), - "path" : os.path.dirname(file_name), - "size": f"{os.path.getsize(file_name) / (1024*1024):.2f} MB", + "path": os.path.dirname(file_name), + "size": f"{os.path.getsize(file_name) / (1024 * 1024):.2f} MB", "modified": os.path.getmtime(file_name), } - logging.info(f"File info:") + logging.info("File info:") for _key in self.file_info: logging.info(f"\t{_key}: {self.file_info[_key]}") else: logging.info(f"File not found: {file_name}") def processing_tpx3(self, file_name=None): - - logging.info(f"Processing TPX3 file: {os.path.basename(file_name)} ... (be patient!)") + logging.info( + f"Processing TPX3 file: {os.path.basename(file_name)} ... (be patient!)" + ) start_time = time.time() - + # loading tpx3 file - hits_view = tdc.process_tpx3(file_name, parallel=True) + hits_view = tdc.process_tpx3(file_name, parallel=True) hits = np.array(hits_view, copy=False) self.hits = hits @@ -95,9 +91,11 @@ def processing_tpx3(self, file_name=None): display(HTML(f"Processing completed in {processing_time:.2f} seconds.")) # display result as a table - from_tof = hits['tof'].min() * 25 / 1e6 - to_tof = hits['tof'].max() * 25 / 1e6 - display(HTML(""" + from_tof = hits["tof"].min() * 25 / 1e6 + to_tof = hits["tof"].max() * 25 / 1e6 + display( + HTML( + """

TPX3 Processing Results

@@ -106,25 +104,35 @@ def processing_tpx3(self, file_name=None):
InfosValue
Y range{} - {}
TOF range (ms){:.3f} - {:.3f}
- """.format(len(hits), hits['x'].min(), hits['x'].max(), hits['y'].min(), hits['y'].max(), - from_tof, to_tof))) + """.format( + len(hits), + hits["x"].min(), + hits["x"].max(), + hits["y"].min(), + hits["y"].max(), + from_tof, + to_tof, + ) + ) + ) unique_chips, chip_counts = np.unique(hits["chip_id"], return_counts=True) - _text = '

Chips distribution (hits per chips)

' + \ - '' + _text = ( + "

Chips distribution (hits per chips)

" + + '
' + ) for chip, count in zip(unique_chips, chip_counts): percentage = 100 * count / len(hits) - _text += f'' - _text += '
Chip {chip}{count:,} hits ({percentage:.1f}%)
' + _text += f"Chip {chip}{count:,} hits ({percentage:.1f}%)" + _text += "" display(HTML(_text)) def select_sampling_percentage(self): if len(self.hits) > 100_000: label = widgets.Label("Select sampling percentage:") - self.sampling_percentage_ui = widgets.FloatSlider(min=0.01, - max=100, - value=0.1, - step=0.01) + self.sampling_percentage_ui = widgets.FloatSlider( + min=0.01, max=100, value=0.1, step=0.01 + ) hori_layout = widgets.HBox([label, self.sampling_percentage_ui]) self.apply_sampling = True display(hori_layout) @@ -133,32 +141,36 @@ def select_sampling_percentage(self): self.apply_sampling = False def display_image_with_roi(self): - self.generate_2d_hit_map() + self.generate_2d_hit_map() def generate_2d_hit_map(self): - sample_fraction = self.sampling_percentage_ui.value / 100. + sample_fraction = self.sampling_percentage_ui.value / 100.0 if self.apply_sampling: n_sample = int(len(self.hits) * sample_fraction) - sample_indices = np.random.choice(len(self.hits), size=n_sample, replace=False) + sample_indices = np.random.choice( + len(self.hits), size=n_sample, replace=False + ) hits_for_viz = self.hits[sample_indices] - display(HTML(f"Using {n_sample:,} sampled hits ({sample_fraction*100:.1f}%) for visualization")) + display( + HTML( + f"Using {n_sample:,} sampled hits ({sample_fraction * 100:.1f}%) for visualization" + ) + ) else: hits_for_viz = self.hits display(HTML(f"Using all {len(self.hits):,} hits for visualization")) - + # Create 2D histogram (bin by detector pixels) x_bins = np.arange(0, 515, 1) # 0 to 514 pixels y_bins = np.arange(0, 515, 1) # 0 to 514 pixels self.hist2d, self.x_edges, self.y_edges = np.histogram2d( - hits_for_viz["x"], hits_for_viz["y"], bins=[x_bins, y_bins]) + hits_for_viz["x"], hits_for_viz["y"], bins=[x_bins, y_bins] + ) def select_roi(self): pass - + def generate_histogram_and_select_roi(self): self.generate_2d_hit_map() self.select_roi() - - - diff --git a/notebooks/__code/truncate_file_names/truncate_file_names.py b/notebooks/__code/truncate_file_names/truncate_file_names.py index 5738d351b..eaac3f9d9 100755 --- a/notebooks/__code/truncate_file_names/truncate_file_names.py +++ b/notebooks/__code/truncate_file_names/truncate_file_names.py @@ -16,7 +16,9 @@ def __init__(self, working_dir=""): def select_input_files(self): self.input_files_ui = fileselector.FileSelectorPanel( - instruction="Select List of Files", start_dir=self.working_dir, multiple=True + instruction="Select List of Files", + start_dir=self.working_dir, + multiple=True, ) self.input_files_ui.show() @@ -39,8 +41,12 @@ def __init__(self, o_truncate=None): raise ValueError("TruncateFileNames is missing!") if self.list_of_files: - _random_input_list = utilities.get_n_random_element(input_list=self.list_of_files, n=10) - self.random_input_list = [os.path.basename(_file) for _file in _random_input_list] + _random_input_list = utilities.get_n_random_element( + input_list=self.list_of_files, n=10 + ) + self.random_input_list = [ + os.path.basename(_file) for _file in _random_input_list + ] self.basename = os.path.basename(self.list_of_files[0]) self.working_dir = os.path.dirname(os.path.dirname(self.list_of_files[0])) @@ -62,28 +68,40 @@ def show(self): [ widgets.Label("Random Input:", layout=widgets.Layout(width="15%")), widgets.Dropdown( - options=self.random_input_list, value=self.random_input_list[0], layout=widgets.Layout(width="50%") + options=self.random_input_list, + value=self.random_input_list[0], + layout=widgets.Layout(width="50%"), ), ] ) self.random_input_checkbox = self.box1.children[1] - self.random_input_checkbox.observe(self.random_input_checkbox_value_changed, "value") + self.random_input_checkbox.observe( + self.random_input_checkbox_value_changed, "value" + ) - self.box2 = widgets.HBox([widgets.Label("String to remove:", layout=widgets.Layout(width="30%"))]) + self.box2 = widgets.HBox( + [widgets.Label("String to remove:", layout=widgets.Layout(width="30%"))] + ) self.box6 = widgets.HBox( [ - widgets.Label(value=" On the left:", layout=widgets.Layout(width="40%")), + widgets.Label( + value=" On the left:", layout=widgets.Layout(width="40%") + ), widgets.Text(value="", layout=widgets.Layout(width="60%")), ] ) self.box7 = widgets.HBox( [ - widgets.Label(value=" On the right:", layout=widgets.Layout(width="40%")), + widgets.Label( + value=" On the right:", layout=widgets.Layout(width="40%") + ), widgets.Text(value="", layout=widgets.Layout(width="60%")), ] ) - self.box8 = widgets.VBox([self.box6, self.box7], layout=widgets.Layout(width="50%")) + self.box8 = widgets.VBox( + [self.box6, self.box7], layout=widgets.Layout(width="50%") + ) self.left_part_to_remove_text = self.box6.children[1] self.right_part_to_remove_text = self.box7.children[1] self.left_part_to_remove_text.observe(self.left_part_text_changed, "value") @@ -99,7 +117,9 @@ def show(self): separate_line = widgets.HTML(value="
", layout=widgets.Layout(width="100%")) - vbox = widgets.VBox([self.box1, separate_line, self.box2, self.box8, separate_line, self.box9]) + vbox = widgets.VBox( + [self.box1, separate_line, self.box2, self.box8, separate_line, self.box9] + ) display(vbox) self.update_new_file_name() @@ -127,8 +147,14 @@ def update_new_file_name(self): def check_new_names(self): dict_old_new_names = self.create_dict_old_new_filenames() - old_names_new_names = [f"{os.path.basename(_key)} -> {_value}" for _key, _value in dict_old_new_names.items()] - select_widget = widgets.Select(options=old_names_new_names, layout=widgets.Layout(width="100%", height="400px")) + old_names_new_names = [ + f"{os.path.basename(_key)} -> {_value}" + for _key, _value in dict_old_new_names.items() + ] + select_widget = widgets.Select( + options=old_names_new_names, + layout=widgets.Layout(width="100%", height="400px"), + ) display(select_widget) def create_dict_old_new_filenames(self): @@ -165,7 +191,9 @@ def rename_and_export_files(self, output_folder=None): input_folder = os.path.abspath(self.input_folder) input_folder_renamed = os.path.basename(input_folder) + "_renamed" self.output_folder_ui.shortcut_buttons.close() - new_output_folder = os.path.join(os.path.abspath(output_folder), input_folder_renamed) + new_output_folder = os.path.join( + os.path.abspath(output_folder), input_folder_renamed + ) dict_old_new_names = self.create_dict_old_new_filenames() utilities.copy_files( diff --git a/notebooks/__code/ui_addie.py b/notebooks/__code/ui_addie.py index d72ac0304..990f234cd 100755 --- a/notebooks/__code/ui_addie.py +++ b/notebooks/__code/ui_addie.py @@ -102,7 +102,9 @@ def setupUi(self, MainWindow): self.verticalLayout_2 = QtWidgets.QVBoxLayout() self.verticalLayout_2.setObjectName("verticalLayout_2") self.treeWidget = QtWidgets.QTreeWidget(self.centralwidget) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Expanding) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Expanding + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.treeWidget.sizePolicy().hasHeightForWidth()) @@ -111,7 +113,9 @@ def setupUi(self, MainWindow): self.treeWidget.setMaximumSize(QtCore.QSize(250, 16777215)) self.treeWidget.setObjectName("treeWidget") self.verticalLayout_2.addWidget(self.treeWidget) - spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout_2.addItem(spacerItem) self.horizontalLayout.addLayout(self.verticalLayout_2) MainWindow.setCentralWidget(self.centralwidget) @@ -124,7 +128,9 @@ def setupUi(self, MainWindow): MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) - self.h3_table.customContextMenuRequested["QPoint"].connect(MainWindow.h3_table_right_click) + self.h3_table.customContextMenuRequested["QPoint"].connect( + MainWindow.h3_table_right_click + ) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): @@ -173,4 +179,6 @@ def retranslateUi(self, MainWindow): item = self.h3_table.item(0, 7) item.setText(_translate("MainWindow", "8")) self.h3_table.setSortingEnabled(__sortingEnabled) - self.treeWidget.headerItem().setText(0, _translate("MainWindow", "Columns Visibility")) + self.treeWidget.headerItem().setText( + 0, _translate("MainWindow", "Columns Visibility") + ) diff --git a/notebooks/__code/ui_addie_save_config.py b/notebooks/__code/ui_addie_save_config.py index 44c84d489..0c27ee22a 100755 --- a/notebooks/__code/ui_addie_save_config.py +++ b/notebooks/__code/ui_addie_save_config.py @@ -11,7 +11,9 @@ class Ui_Dialog: def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(401, 89) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(Dialog.sizePolicy().hasHeightForWidth()) @@ -32,7 +34,9 @@ def setupUi(self, Dialog): self.verticalLayout.addLayout(self.horizontalLayout) self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_2.addItem(spacerItem) self.cancel_button = QtWidgets.QPushButton(Dialog) self.cancel_button.setObjectName("cancel_button") diff --git a/notebooks/__code/ui_bragg_edge_peak_fitting.py b/notebooks/__code/ui_bragg_edge_peak_fitting.py index 0cef6be37..d4e91c1d8 100755 --- a/notebooks/__code/ui_bragg_edge_peak_fitting.py +++ b/notebooks/__code/ui_bragg_edge_peak_fitting.py @@ -22,7 +22,9 @@ def setupUi(self, MainWindow): self.verticalLayout = QtWidgets.QVBoxLayout(self.tab) self.verticalLayout.setObjectName("verticalLayout") self.splitter = QtWidgets.QSplitter(self.tab) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.splitter.sizePolicy().hasHeightForWidth()) @@ -41,7 +43,9 @@ def setupUi(self, MainWindow): self.label = QtWidgets.QLabel(self.tab) self.label.setMinimumSize(QtCore.QSize(200, 0)) self.label.setMaximumSize(QtCore.QSize(200, 16777215)) - self.label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter) + self.label.setAlignment( + QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter + ) self.label.setObjectName("label") self.gridLayout.addWidget(self.label, 0, 0, 1, 1) self.lineEdit = QtWidgets.QLineEdit(self.tab) @@ -57,7 +61,9 @@ def setupUi(self, MainWindow): self.label_4 = QtWidgets.QLabel(self.tab) self.label_4.setMinimumSize(QtCore.QSize(200, 0)) self.label_4.setMaximumSize(QtCore.QSize(200, 16777215)) - self.label_4.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter) + self.label_4.setAlignment( + QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter + ) self.label_4.setObjectName("label_4") self.gridLayout.addWidget(self.label_4, 1, 0, 1, 1) self.lineEdit_2 = QtWidgets.QLineEdit(self.tab) @@ -71,7 +77,9 @@ def setupUi(self, MainWindow): self.label_3.setObjectName("label_3") self.gridLayout.addWidget(self.label_3, 1, 2, 1, 1) self.horizontalLayout_2.addLayout(self.gridLayout) - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_2.addItem(spacerItem) self.verticalLayout.addLayout(self.horizontalLayout_2) self.tabWidget.addTab(self.tab, "") @@ -127,7 +135,9 @@ def setupUi(self, MainWindow): self.cancel_button = QtWidgets.QPushButton(self.centralwidget) self.cancel_button.setObjectName("cancel_button") self.horizontalLayout.addWidget(self.cancel_button) - spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem1 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem1) self.apply_button = QtWidgets.QPushButton(self.centralwidget) self.apply_button.setObjectName("apply_button") @@ -155,7 +165,9 @@ def retranslateUi(self, MainWindow): self.label_2.setText(_translate("MainWindow", "m")) self.label_4.setText(_translate("MainWindow", "detector offset")) self.label_3.setText(_translate("MainWindow", "micros")) - self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Selection")) + self.tabWidget.setTabText( + self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Selection") + ) self.groupBox.setTitle(_translate("MainWindow", "List of algorithms")) self.radioButton_3.setText(_translate("MainWindow", "algorithm 3")) self.radioButton_2.setText(_translate("MainWindow", "algorithm 2")) @@ -170,6 +182,8 @@ def retranslateUi(self, MainWindow): item.setText(_translate("MainWindow", "coeff2 value")) item = self.tableWidget.horizontalHeaderItem(4) item.setText(_translate("MainWindow", "coeff2 error")) - self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Fitting")) + self.tabWidget.setTabText( + self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Fitting") + ) self.cancel_button.setText(_translate("MainWindow", "Cancel")) self.apply_button.setText(_translate("MainWindow", "Use this peak")) diff --git a/notebooks/__code/ui_calibrated_transmission.py b/notebooks/__code/ui_calibrated_transmission.py index e4881d35f..ee05fd2bb 100755 --- a/notebooks/__code/ui_calibrated_transmission.py +++ b/notebooks/__code/ui_calibrated_transmission.py @@ -38,10 +38,14 @@ def setupUi(self, MainWindow): self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.pyqtgraph_widget = QtWidgets.QWidget(self.layoutWidget) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) - sizePolicy.setHeightForWidth(self.pyqtgraph_widget.sizePolicy().hasHeightForWidth()) + sizePolicy.setHeightForWidth( + self.pyqtgraph_widget.sizePolicy().hasHeightForWidth() + ) self.pyqtgraph_widget.setSizePolicy(sizePolicy) self.pyqtgraph_widget.setObjectName("pyqtgraph_widget") self.horizontalLayout_2.addWidget(self.pyqtgraph_widget) @@ -87,7 +91,9 @@ def setupUi(self, MainWindow): self.use_calibration1_checkbox.setChecked(True) self.use_calibration1_checkbox.setObjectName("use_calibration1_checkbox") self.horizontalLayout_12.addWidget(self.use_calibration1_checkbox) - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_12.addItem(spacerItem) self.verticalLayout_5.addLayout(self.horizontalLayout_12) self.gridLayout = QtWidgets.QGridLayout() @@ -113,7 +119,9 @@ def setupUi(self, MainWindow): brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush) self.calibration1_x0_label.setPalette(palette) - self.calibration1_x0_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter) + self.calibration1_x0_label.setAlignment( + QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter + ) self.calibration1_x0_label.setObjectName("calibration1_x0_label") self.gridLayout.addWidget(self.calibration1_x0_label, 0, 0, 1, 1) self.calibration1_x0 = QtWidgets.QLineEdit(self.tab_3) @@ -202,7 +210,9 @@ def setupUi(self, MainWindow): brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush) self.calibration1_y0_label.setPalette(palette) - self.calibration1_y0_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter) + self.calibration1_y0_label.setAlignment( + QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter + ) self.calibration1_y0_label.setObjectName("calibration1_y0_label") self.gridLayout.addWidget(self.calibration1_y0_label, 1, 0, 1, 1) self.calibration1_y0 = QtWidgets.QLineEdit(self.tab_3) @@ -294,9 +304,13 @@ def setupUi(self, MainWindow): self.calibration1_index.setPalette(palette) self.calibration1_index.setObjectName("calibration1_index") self.horizontalLayout_4.addWidget(self.calibration1_index) - self.calibration1_display_this_file_button = QtWidgets.QPushButton(self.calibration1_groupbox) + self.calibration1_display_this_file_button = QtWidgets.QPushButton( + self.calibration1_groupbox + ) self.calibration1_display_this_file_button.setMinimumSize(QtCore.QSize(0, 40)) - self.calibration1_display_this_file_button.setMaximumSize(QtCore.QSize(16777215, 40)) + self.calibration1_display_this_file_button.setMaximumSize( + QtCore.QSize(16777215, 40) + ) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(26, 30, 220)) brush.setStyle(QtCore.Qt.SolidPattern) @@ -317,7 +331,9 @@ def setupUi(self, MainWindow): brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush) self.calibration1_display_this_file_button.setPalette(palette) - self.calibration1_display_this_file_button.setObjectName("calibration1_display_this_file_button") + self.calibration1_display_this_file_button.setObjectName( + "calibration1_display_this_file_button" + ) self.horizontalLayout_4.addWidget(self.calibration1_display_this_file_button) self.verticalLayout.addLayout(self.horizontalLayout_4) self.label_2 = QtWidgets.QLabel(self.calibration1_groupbox) @@ -349,9 +365,13 @@ def setupUi(self, MainWindow): self.label_2.setAlignment(QtCore.Qt.AlignCenter) self.label_2.setObjectName("label_2") self.verticalLayout.addWidget(self.label_2) - self.calibration1_use_current_file_button = QtWidgets.QPushButton(self.calibration1_groupbox) + self.calibration1_use_current_file_button = QtWidgets.QPushButton( + self.calibration1_groupbox + ) self.calibration1_use_current_file_button.setMinimumSize(QtCore.QSize(0, 40)) - self.calibration1_use_current_file_button.setMaximumSize(QtCore.QSize(16777215, 40)) + self.calibration1_use_current_file_button.setMaximumSize( + QtCore.QSize(16777215, 40) + ) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(26, 30, 220)) brush.setStyle(QtCore.Qt.SolidPattern) @@ -372,9 +392,13 @@ def setupUi(self, MainWindow): brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush) self.calibration1_use_current_file_button.setPalette(palette) - self.calibration1_use_current_file_button.setObjectName("calibration1_use_current_file_button") + self.calibration1_use_current_file_button.setObjectName( + "calibration1_use_current_file_button" + ) self.verticalLayout.addWidget(self.calibration1_use_current_file_button) - spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem1 = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout.addItem(spacerItem1) self.verticalLayout_5.addWidget(self.calibration1_groupbox) self.tabWidget_2.addTab(self.tab_3, "") @@ -388,7 +412,9 @@ def setupUi(self, MainWindow): self.use_calibration2_checkbox.setChecked(True) self.use_calibration2_checkbox.setObjectName("use_calibration2_checkbox") self.horizontalLayout_13.addWidget(self.use_calibration2_checkbox) - spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem2 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_13.addItem(spacerItem2) self.verticalLayout_6.addLayout(self.horizontalLayout_13) self.gridLayout_2 = QtWidgets.QGridLayout() @@ -405,7 +431,9 @@ def setupUi(self, MainWindow): brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush) self.calibration2_x0_label.setPalette(palette) - self.calibration2_x0_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter) + self.calibration2_x0_label.setAlignment( + QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter + ) self.calibration2_x0_label.setObjectName("calibration2_x0_label") self.gridLayout_2.addWidget(self.calibration2_x0_label, 0, 0, 1, 1) self.calibration2_x0 = QtWidgets.QLineEdit(self.tab_4) @@ -467,7 +495,9 @@ def setupUi(self, MainWindow): brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush) self.calibration2_y0_label.setPalette(palette) - self.calibration2_y0_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter) + self.calibration2_y0_label.setAlignment( + QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter + ) self.calibration2_y0_label.setObjectName("calibration2_y0_label") self.gridLayout_2.addWidget(self.calibration2_y0_label, 1, 0, 1, 1) self.calibration2_y0 = QtWidgets.QLineEdit(self.tab_4) @@ -532,9 +562,13 @@ def setupUi(self, MainWindow): self.calibration2_index.setPalette(palette) self.calibration2_index.setObjectName("calibration2_index") self.horizontalLayout_5.addWidget(self.calibration2_index) - self.calibration2_display_this_file_button = QtWidgets.QPushButton(self.calibration2_groupbox) + self.calibration2_display_this_file_button = QtWidgets.QPushButton( + self.calibration2_groupbox + ) self.calibration2_display_this_file_button.setMinimumSize(QtCore.QSize(0, 40)) - self.calibration2_display_this_file_button.setMaximumSize(QtCore.QSize(16777215, 40)) + self.calibration2_display_this_file_button.setMaximumSize( + QtCore.QSize(16777215, 40) + ) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(193, 22, 45)) brush.setStyle(QtCore.Qt.SolidPattern) @@ -546,7 +580,9 @@ def setupUi(self, MainWindow): brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush) self.calibration2_display_this_file_button.setPalette(palette) - self.calibration2_display_this_file_button.setObjectName("calibration2_display_this_file_button") + self.calibration2_display_this_file_button.setObjectName( + "calibration2_display_this_file_button" + ) self.horizontalLayout_5.addWidget(self.calibration2_display_this_file_button) self.verticalLayout_9.addLayout(self.horizontalLayout_5) self.label_4 = QtWidgets.QLabel(self.calibration2_groupbox) @@ -569,9 +605,13 @@ def setupUi(self, MainWindow): self.label_4.setAlignment(QtCore.Qt.AlignCenter) self.label_4.setObjectName("label_4") self.verticalLayout_9.addWidget(self.label_4) - self.calibration2_use_current_file_button = QtWidgets.QPushButton(self.calibration2_groupbox) + self.calibration2_use_current_file_button = QtWidgets.QPushButton( + self.calibration2_groupbox + ) self.calibration2_use_current_file_button.setMinimumSize(QtCore.QSize(0, 40)) - self.calibration2_use_current_file_button.setMaximumSize(QtCore.QSize(16777215, 40)) + self.calibration2_use_current_file_button.setMaximumSize( + QtCore.QSize(16777215, 40) + ) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(193, 22, 45)) brush.setStyle(QtCore.Qt.SolidPattern) @@ -583,10 +623,14 @@ def setupUi(self, MainWindow): brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush) self.calibration2_use_current_file_button.setPalette(palette) - self.calibration2_use_current_file_button.setObjectName("calibration2_use_current_file_button") + self.calibration2_use_current_file_button.setObjectName( + "calibration2_use_current_file_button" + ) self.verticalLayout_9.addWidget(self.calibration2_use_current_file_button) self.verticalLayout_6.addWidget(self.calibration2_groupbox) - spacerItem3 = QtWidgets.QSpacerItem(20, 305, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem3 = QtWidgets.QSpacerItem( + 20, 305, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout_6.addItem(spacerItem3) self.tabWidget_2.addTab(self.tab_4, "") self.verticalLayout_11.addWidget(self.tabWidget_2) @@ -619,7 +663,9 @@ def setupUi(self, MainWindow): self.remove_row.setFont(font) self.remove_row.setObjectName("remove_row") self.horizontalLayout_6.addWidget(self.remove_row) - spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem4 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_6.addItem(spacerItem4) self.add_row = QtWidgets.QPushButton(self.page_2) self.add_row.setMinimumSize(QtCore.QSize(50, 40)) @@ -660,7 +706,9 @@ def setupUi(self, MainWindow): self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setObjectName("pushButton") self.horizontalLayout.addWidget(self.pushButton) - spacerItem5 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem5 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem5) self.export_button = QtWidgets.QPushButton(self.centralwidget) self.export_button.setObjectName("export_button") @@ -699,25 +747,55 @@ def setupUi(self, MainWindow): self.tabWidget_2.setCurrentIndex(0) self.file_slider.sliderMoved["int"].connect(MainWindow.slider_file_changed) self.file_slider.valueChanged["int"].connect(MainWindow.slider_file_changed) - self.previous_image_button.clicked.connect(MainWindow.previous_image_button_clicked) + self.previous_image_button.clicked.connect( + MainWindow.previous_image_button_clicked + ) self.next_image_button.clicked.connect(MainWindow.next_image_button_clicked) self.export_button.clicked.connect(MainWindow.export_button_clicked) - self.use_calibration1_checkbox.clicked.connect(MainWindow.use_calibration1_checked) + self.use_calibration1_checkbox.clicked.connect( + MainWindow.use_calibration1_checked + ) self.remove_row.clicked.connect(MainWindow.remove_row_button_clicked) self.add_row.clicked.connect(MainWindow.add_row_button_clicked) - self.calibration1_use_current_file_button.clicked.connect(MainWindow.use_current_calibration1_file) - self.calibration2_use_current_file_button.clicked.connect(MainWindow.use_current_calibration2_file) - self.calibration1_display_this_file_button.clicked.connect(MainWindow.display_this_cal1_file) - self.calibration2_display_this_file_button.clicked.connect(MainWindow.display_this_cal2_file) - self.calibration1_x0.returnPressed.connect(MainWindow.calibration1_widgets_changed) - self.calibration1_width.returnPressed.connect(MainWindow.calibration1_widgets_changed) - self.calibration1_y0.returnPressed.connect(MainWindow.calibration1_widgets_changed) - self.calibration1_height.returnPressed.connect(MainWindow.calibration1_widgets_changed) - self.calibration2_x0.returnPressed.connect(MainWindow.calibration2_widgets_changed) - self.calibration2_width.returnPressed.connect(MainWindow.calibration2_widgets_changed) - self.calibration2_y0.returnPressed.connect(MainWindow.calibration2_widgets_changed) - self.calibration2_height.returnPressed.connect(MainWindow.calibration2_widgets_changed) - self.use_calibration2_checkbox.clicked.connect(MainWindow.use_calibration2_checked) + self.calibration1_use_current_file_button.clicked.connect( + MainWindow.use_current_calibration1_file + ) + self.calibration2_use_current_file_button.clicked.connect( + MainWindow.use_current_calibration2_file + ) + self.calibration1_display_this_file_button.clicked.connect( + MainWindow.display_this_cal1_file + ) + self.calibration2_display_this_file_button.clicked.connect( + MainWindow.display_this_cal2_file + ) + self.calibration1_x0.returnPressed.connect( + MainWindow.calibration1_widgets_changed + ) + self.calibration1_width.returnPressed.connect( + MainWindow.calibration1_widgets_changed + ) + self.calibration1_y0.returnPressed.connect( + MainWindow.calibration1_widgets_changed + ) + self.calibration1_height.returnPressed.connect( + MainWindow.calibration1_widgets_changed + ) + self.calibration2_x0.returnPressed.connect( + MainWindow.calibration2_widgets_changed + ) + self.calibration2_width.returnPressed.connect( + MainWindow.calibration2_widgets_changed + ) + self.calibration2_y0.returnPressed.connect( + MainWindow.calibration2_widgets_changed + ) + self.calibration2_height.returnPressed.connect( + MainWindow.calibration2_widgets_changed + ) + self.use_calibration2_checkbox.clicked.connect( + MainWindow.use_calibration2_checked + ) self.pushButton.clicked.connect(MainWindow.help_button_clicked) QtCore.QMetaObject.connectSlotsByName(MainWindow) @@ -727,33 +805,58 @@ def retranslateUi(self, MainWindow): self.previous_image_button.setText(_translate("MainWindow", "Prev. Image")) self.image_slider_value.setText(_translate("MainWindow", "0")) self.next_image_button.setText(_translate("MainWindow", "Next Image")) - self.use_calibration1_checkbox.setText(_translate("MainWindow", "Use Calibration")) + self.use_calibration1_checkbox.setText( + _translate("MainWindow", "Use Calibration") + ) self.calibration1_x0_label.setText(_translate("MainWindow", "X0")) self.calibration1_width_label.setText(_translate("MainWindow", "width")) self.calibration1_value_label.setText(_translate("MainWindow", "Value")) self.calibration1_y0_label.setText(_translate("MainWindow", "y0")) self.calibration1_height_label.setText(_translate("MainWindow", "height")) - self.calibration1_groupbox.setTitle(_translate("MainWindow", "File to Use for this Calibration")) + self.calibration1_groupbox.setTitle( + _translate("MainWindow", "File to Use for this Calibration") + ) self.label_38.setText(_translate("MainWindow", "Index")) self.calibration1_index.setText(_translate("MainWindow", "0")) - self.calibration1_display_this_file_button.setText(_translate("MainWindow", "Display This File")) + self.calibration1_display_this_file_button.setText( + _translate("MainWindow", "Display This File") + ) self.label_2.setText(_translate("MainWindow", "or")) - self.calibration1_use_current_file_button.setText(_translate("MainWindow", "Use Current File")) - self.tabWidget_2.setTabText(self.tabWidget_2.indexOf(self.tab_3), _translate("MainWindow", "Calibration 1")) - self.use_calibration2_checkbox.setText(_translate("MainWindow", "Use Calibration")) + self.calibration1_use_current_file_button.setText( + _translate("MainWindow", "Use Current File") + ) + self.tabWidget_2.setTabText( + self.tabWidget_2.indexOf(self.tab_3), + _translate("MainWindow", "Calibration 1"), + ) + self.use_calibration2_checkbox.setText( + _translate("MainWindow", "Use Calibration") + ) self.calibration2_x0_label.setText(_translate("MainWindow", "X0")) self.calibration2_width_label.setText(_translate("MainWindow", "width")) self.calibration2_value_label.setText(_translate("MainWindow", "Value")) self.calibration2_y0_label.setText(_translate("MainWindow", "y0")) self.calibration2_height_label.setText(_translate("MainWindow", "height")) - self.calibration2_groupbox.setTitle(_translate("MainWindow", "File to Use for this Calibration")) + self.calibration2_groupbox.setTitle( + _translate("MainWindow", "File to Use for this Calibration") + ) self.label_45.setText(_translate("MainWindow", "Index")) self.calibration2_index.setText(_translate("MainWindow", "0")) - self.calibration2_display_this_file_button.setText(_translate("MainWindow", "Display This File")) + self.calibration2_display_this_file_button.setText( + _translate("MainWindow", "Display This File") + ) self.label_4.setText(_translate("MainWindow", "or")) - self.calibration2_use_current_file_button.setText(_translate("MainWindow", "Use Current File")) - self.tabWidget_2.setTabText(self.tabWidget_2.indexOf(self.tab_4), _translate("MainWindow", "Calibration 2")) - self.toolBox.setItemText(self.toolBox.indexOf(self.page), _translate("MainWindow", "Calibration Regions")) + self.calibration2_use_current_file_button.setText( + _translate("MainWindow", "Use Current File") + ) + self.tabWidget_2.setTabText( + self.tabWidget_2.indexOf(self.tab_4), + _translate("MainWindow", "Calibration 2"), + ) + self.toolBox.setItemText( + self.toolBox.indexOf(self.page), + _translate("MainWindow", "Calibration Regions"), + ) item = self.tableWidget.horizontalHeaderItem(0) item.setText(_translate("MainWindow", "X0")) item = self.tableWidget.horizontalHeaderItem(1) @@ -764,20 +867,31 @@ def retranslateUi(self, MainWindow): item.setText(_translate("MainWindow", "Height")) self.remove_row.setText(_translate("MainWindow", "-")) self.add_row.setText(_translate("MainWindow", "+")) - self.toolBox.setItemText(self.toolBox.indexOf(self.page_2), _translate("MainWindow", "Measurement Regions")) - self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Measurement")) + self.toolBox.setItemText( + self.toolBox.indexOf(self.page_2), + _translate("MainWindow", "Measurement Regions"), + ) + self.tabWidget.setTabText( + self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Measurement") + ) item = self.summary_table.horizontalHeaderItem(0) item.setText(_translate("MainWindow", "Files Name")) item = self.summary_table.horizontalHeaderItem(1) item.setText(_translate("MainWindow", "Time Stamp")) item = self.summary_table.horizontalHeaderItem(2) item.setText(_translate("MainWindow", "Relative Time (s)")) - self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Summary")) + self.tabWidget.setTabText( + self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Summary") + ) self.pushButton.setText(_translate("MainWindow", "Help")) - self.export_button.setText(_translate("MainWindow", "Export Calibrated Transmission ...")) + self.export_button.setText( + _translate("MainWindow", "Export Calibrated Transmission ...") + ) self.actionExport_Profile.setText(_translate("MainWindow", "Profiles ...")) self.actionWater_Intake.setText(_translate("MainWindow", "Water Intake ...")) - self.actionImportedFilesMetadata.setText(_translate("MainWindow", "Imported Files and Metadata ...")) + self.actionImportedFilesMetadata.setText( + _translate("MainWindow", "Imported Files and Metadata ...") + ) self.actionBy_Time_Stamp.setText(_translate("MainWindow", "by Time Stamp")) self.actionBy_File_Name.setText(_translate("MainWindow", "by File Name")) self.actionDsc_files.setText(_translate("MainWindow", "dsc files ...")) diff --git a/notebooks/__code/ui_display_counts_of_region_vs_stack.py b/notebooks/__code/ui_display_counts_of_region_vs_stack.py index e8da981ab..944338b5d 100755 --- a/notebooks/__code/ui_display_counts_of_region_vs_stack.py +++ b/notebooks/__code/ui_display_counts_of_region_vs_stack.py @@ -17,7 +17,9 @@ def setupUi(self, MainWindow): self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget) self.verticalLayout.setObjectName("verticalLayout") self.widget = QtWidgets.QWidget(self.centralwidget) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth()) @@ -49,12 +51,16 @@ def setupUi(self, MainWindow): self.distance_source_detector_value = QtWidgets.QLineEdit(self.centralwidget) self.distance_source_detector_value.setMinimumSize(QtCore.QSize(80, 0)) self.distance_source_detector_value.setMaximumSize(QtCore.QSize(80, 16777215)) - self.distance_source_detector_value.setObjectName("distance_source_detector_value") + self.distance_source_detector_value.setObjectName( + "distance_source_detector_value" + ) self.horizontalLayout_2.addWidget(self.distance_source_detector_value) self.label_2 = QtWidgets.QLabel(self.centralwidget) self.label_2.setObjectName("label_2") self.horizontalLayout_2.addWidget(self.label_2) - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_2.addItem(spacerItem) self.label_3 = QtWidgets.QLabel(self.centralwidget) self.label_3.setObjectName("label_3") @@ -67,7 +73,9 @@ def setupUi(self, MainWindow): self.detector_offset_units = QtWidgets.QLabel(self.centralwidget) self.detector_offset_units.setObjectName("detector_offset_units") self.horizontalLayout_2.addWidget(self.detector_offset_units) - spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem1 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_2.addItem(spacerItem1) self.horizontalLayout_7.addLayout(self.horizontalLayout_2) self.verticalLayout.addLayout(self.horizontalLayout_7) @@ -83,7 +91,9 @@ def setupUi(self, MainWindow): self.time_spectra_file_browse_button = QtWidgets.QPushButton(self.centralwidget) self.time_spectra_file_browse_button.setMinimumSize(QtCore.QSize(100, 0)) self.time_spectra_file_browse_button.setMaximumSize(QtCore.QSize(100, 16777215)) - self.time_spectra_file_browse_button.setObjectName("time_spectra_file_browse_button") + self.time_spectra_file_browse_button.setObjectName( + "time_spectra_file_browse_button" + ) self.horizontalLayout_3.addWidget(self.time_spectra_file_browse_button) self.verticalLayout.addLayout(self.horizontalLayout_3) self.horizontalLayout = QtWidgets.QHBoxLayout() @@ -103,9 +113,15 @@ def setupUi(self, MainWindow): self.retranslateUi(MainWindow) self.done_button.clicked.connect(MainWindow.done_button_clicked) - self.distance_source_detector_value.editingFinished.connect(MainWindow.distance_source_detector_validated) - self.detector_offset_value.returnPressed.connect(MainWindow.detector_offset_validated) - self.time_spectra_file_browse_button.clicked.connect(MainWindow.time_spectra_file_browse_button_clicked) + self.distance_source_detector_value.editingFinished.connect( + MainWindow.distance_source_detector_validated + ) + self.detector_offset_value.returnPressed.connect( + MainWindow.detector_offset_validated + ) + self.time_spectra_file_browse_button.clicked.connect( + MainWindow.time_spectra_file_browse_button_clicked + ) self.file_index_ratio_button.clicked.connect(MainWindow.radio_button_clicked) self.tof_radio_button.clicked.connect(MainWindow.radio_button_clicked) self.lambda_radio_button.clicked.connect(MainWindow.radio_button_clicked) @@ -124,5 +140,7 @@ def retranslateUi(self, MainWindow): self.detector_offset_units.setText(_translate("MainWindow", "us")) self.label_5.setText(_translate("MainWindow", "Time Spectra File:")) self.time_spectra_file.setText(_translate("MainWindow", "N/A")) - self.time_spectra_file_browse_button.setText(_translate("MainWindow", "Browse ...")) + self.time_spectra_file_browse_button.setText( + _translate("MainWindow", "Browse ...") + ) self.done_button.setText(_translate("MainWindow", "DONE")) diff --git a/notebooks/__code/ui_dual_energy.py b/notebooks/__code/ui_dual_energy.py index f57e15ba7..c043ab355 100755 --- a/notebooks/__code/ui_dual_energy.py +++ b/notebooks/__code/ui_dual_energy.py @@ -16,7 +16,9 @@ def setupUi(self, MainWindow): self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget) self.verticalLayout.setObjectName("verticalLayout") self.widget = QtWidgets.QWidget(self.centralwidget) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth()) @@ -50,12 +52,16 @@ def setupUi(self, MainWindow): self.distance_source_detector_value = QtWidgets.QLineEdit(self.groupBox_4) self.distance_source_detector_value.setMinimumSize(QtCore.QSize(80, 0)) self.distance_source_detector_value.setMaximumSize(QtCore.QSize(80, 16777215)) - self.distance_source_detector_value.setObjectName("distance_source_detector_value") + self.distance_source_detector_value.setObjectName( + "distance_source_detector_value" + ) self.horizontalLayout_4.addWidget(self.distance_source_detector_value) self.label_2 = QtWidgets.QLabel(self.groupBox_4) self.label_2.setObjectName("label_2") self.horizontalLayout_4.addWidget(self.label_2) - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_4.addItem(spacerItem) self.label_3 = QtWidgets.QLabel(self.groupBox_4) self.label_3.setObjectName("label_3") @@ -68,7 +74,9 @@ def setupUi(self, MainWindow): self.detector_offset_units = QtWidgets.QLabel(self.groupBox_4) self.detector_offset_units.setObjectName("detector_offset_units") self.horizontalLayout_4.addWidget(self.detector_offset_units) - spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem1 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_4.addItem(spacerItem1) self.horizontalLayout_7.addWidget(self.groupBox_4) self.verticalLayout.addLayout(self.horizontalLayout_7) @@ -79,17 +87,23 @@ def setupUi(self, MainWindow): self.horizontalLayout_6 = QtWidgets.QHBoxLayout(self.groupBox_2) self.horizontalLayout_6.setObjectName("horizontalLayout_6") self.time_spectra_file = QtWidgets.QLabel(self.groupBox_2) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) - sizePolicy.setHeightForWidth(self.time_spectra_file.sizePolicy().hasHeightForWidth()) + sizePolicy.setHeightForWidth( + self.time_spectra_file.sizePolicy().hasHeightForWidth() + ) self.time_spectra_file.setSizePolicy(sizePolicy) self.time_spectra_file.setObjectName("time_spectra_file") self.horizontalLayout_6.addWidget(self.time_spectra_file) self.time_spectra_file_browse_button = QtWidgets.QPushButton(self.groupBox_2) self.time_spectra_file_browse_button.setMinimumSize(QtCore.QSize(100, 0)) self.time_spectra_file_browse_button.setMaximumSize(QtCore.QSize(100, 16777215)) - self.time_spectra_file_browse_button.setObjectName("time_spectra_file_browse_button") + self.time_spectra_file_browse_button.setObjectName( + "time_spectra_file_browse_button" + ) self.horizontalLayout_6.addWidget(self.time_spectra_file_browse_button) self.horizontalLayout_5.addWidget(self.groupBox_2) self.groupBox_3 = QtWidgets.QGroupBox(self.centralwidget) @@ -128,12 +142,18 @@ def setupUi(self, MainWindow): self.retranslateUi(MainWindow) self.done_button.clicked.connect(MainWindow.done_button_clicked) - self.distance_source_detector_value.editingFinished.connect(MainWindow.distance_source_detector_validated) - self.detector_offset_value.returnPressed.connect(MainWindow.detector_offset_validated) + self.distance_source_detector_value.editingFinished.connect( + MainWindow.distance_source_detector_validated + ) + self.detector_offset_value.returnPressed.connect( + MainWindow.detector_offset_validated + ) self.file_index_ratio_button.clicked.connect(MainWindow.radio_button_clicked) self.tof_radio_button.clicked.connect(MainWindow.radio_button_clicked) self.lambda_radio_button.clicked.connect(MainWindow.radio_button_clicked) - self.time_spectra_file_browse_button.clicked.connect(MainWindow.time_spectra_file_browse_button_clicked) + self.time_spectra_file_browse_button.clicked.connect( + MainWindow.time_spectra_file_browse_button_clicked + ) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): @@ -150,7 +170,9 @@ def retranslateUi(self, MainWindow): self.detector_offset_units.setText(_translate("MainWindow", "us")) self.groupBox_2.setTitle(_translate("MainWindow", "Time Spectra File")) self.time_spectra_file.setText(_translate("MainWindow", "N/A")) - self.time_spectra_file_browse_button.setText(_translate("MainWindow", "Browse ...")) + self.time_spectra_file_browse_button.setText( + _translate("MainWindow", "Browse ...") + ) self.groupBox_3.setTitle(_translate("MainWindow", "Bin ")) self.label_4.setText(_translate("MainWindow", "Size")) self.bin_units.setText(_translate("MainWindow", "files")) diff --git a/notebooks/__code/ui_file_metadata_display.py b/notebooks/__code/ui_file_metadata_display.py index 67a3ae726..24b5986af 100755 --- a/notebooks/__code/ui_file_metadata_display.py +++ b/notebooks/__code/ui_file_metadata_display.py @@ -21,7 +21,9 @@ def setupUi(self, MainWindow): self.verticalLayout.setObjectName("verticalLayout") self.horizontalLayout_3 = QtWidgets.QHBoxLayout() self.horizontalLayout_3.setObjectName("horizontalLayout_3") - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_3.addItem(spacerItem) self.para_1_label = QtWidgets.QLabel(self.centralwidget) self.para_1_label.setMinimumSize(QtCore.QSize(30, 0)) @@ -39,14 +41,18 @@ def setupUi(self, MainWindow): self.para_2_value = QtWidgets.QLabel(self.centralwidget) self.para_2_value.setObjectName("para_2_value") self.horizontalLayout_3.addWidget(self.para_2_value) - spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem1 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_3.addItem(spacerItem1) self.verticalLayout.addLayout(self.horizontalLayout_3) self.widget_2 = QtWidgets.QWidget(self.centralwidget) self.widget_2.setObjectName("widget_2") self.verticalLayout.addWidget(self.widget_2) self.widget = QtWidgets.QWidget(self.centralwidget) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth()) @@ -70,7 +76,9 @@ def setupUi(self, MainWindow): self.tableWidget.setMinimumSize(QtCore.QSize(300, 0)) self.tableWidget.setMaximumSize(QtCore.QSize(300, 16777215)) self.tableWidget.setAlternatingRowColors(True) - self.tableWidget.setSelectionMode(QtWidgets.QAbstractItemView.ContiguousSelection) + self.tableWidget.setSelectionMode( + QtWidgets.QAbstractItemView.ContiguousSelection + ) self.tableWidget.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows) self.tableWidget.setObjectName("tableWidget") self.tableWidget.setColumnCount(3) @@ -88,7 +96,9 @@ def setupUi(self, MainWindow): self.export_button = QtWidgets.QPushButton(self.centralwidget) self.export_button.setObjectName("export_button") self.horizontalLayout.addWidget(self.export_button) - spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem2 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem2) self.close_button = QtWidgets.QPushButton(self.centralwidget) self.close_button.setObjectName("close_button") @@ -104,7 +114,9 @@ def setupUi(self, MainWindow): MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) - self.tableWidget.itemSelectionChanged.connect(MainWindow.table_widget_selection_changed) + self.tableWidget.itemSelectionChanged.connect( + MainWindow.table_widget_selection_changed + ) self.close_button.clicked.connect(MainWindow.close_clicked) self.group_slider.sliderMoved["int"].connect(MainWindow.refresh_pyqtgraph) self.export_button.clicked.connect(MainWindow.export_button_clicked) diff --git a/notebooks/__code/ui_gamma_filtering_tool.py b/notebooks/__code/ui_gamma_filtering_tool.py index db9403823..cb490e700 100755 --- a/notebooks/__code/ui_gamma_filtering_tool.py +++ b/notebooks/__code/ui_gamma_filtering_tool.py @@ -20,7 +20,9 @@ def setupUi(self, MainWindow): self.splitter.setOrientation(QtCore.Qt.Vertical) self.splitter.setObjectName("splitter") self.image_widget = QtWidgets.QWidget(self.splitter) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.image_widget.sizePolicy().hasHeightForWidth()) @@ -49,7 +51,9 @@ def setupUi(self, MainWindow): self.file_index_value.setObjectName("file_index_value") self.horizontalLayout_2.addWidget(self.file_index_value) self.horizontalLayout_4.addLayout(self.horizontalLayout_2) - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_4.addItem(spacerItem) self.label_3 = QtWidgets.QLabel(self.layoutWidget) self.label_3.setMinimumSize(QtCore.QSize(120, 0)) @@ -65,7 +69,9 @@ def setupUi(self, MainWindow): self.horizontalLayout_4.addWidget(self.filtering_coefficient_value) self.verticalLayout.addLayout(self.horizontalLayout_4) self.tableWidget = QtWidgets.QTableWidget(self.layoutWidget) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.tableWidget.sizePolicy().hasHeightForWidth()) @@ -90,7 +96,9 @@ def setupUi(self, MainWindow): self.cancel_button = QtWidgets.QPushButton(self.layoutWidget) self.cancel_button.setObjectName("cancel_button") self.horizontalLayout.addWidget(self.cancel_button) - spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem1 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem1) self.apply_button = QtWidgets.QPushButton(self.layoutWidget) self.apply_button.setObjectName("apply_button") @@ -111,7 +119,9 @@ def setupUi(self, MainWindow): self.apply_button.clicked.connect(MainWindow.apply_clicked) self.file_index_slider.sliderPressed.connect(MainWindow.slider_clicked) self.file_index_slider.valueChanged["int"].connect(MainWindow.slider_moved) - self.filtering_coefficient_value.returnPressed.connect(MainWindow.filtering_coefficient_changed) + self.filtering_coefficient_value.returnPressed.connect( + MainWindow.filtering_coefficient_changed + ) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): diff --git a/notebooks/__code/ui_integrated_roi_counts_vs_file_name_and_time_stamp.py b/notebooks/__code/ui_integrated_roi_counts_vs_file_name_and_time_stamp.py index 0b4972134..b76381d6b 100755 --- a/notebooks/__code/ui_integrated_roi_counts_vs_file_name_and_time_stamp.py +++ b/notebooks/__code/ui_integrated_roi_counts_vs_file_name_and_time_stamp.py @@ -54,10 +54,14 @@ def setupUi(self, MainWindow): self.horizontalLayout_6.addWidget(self.display_size_label) self.grid_size_slider = QtWidgets.QSlider(self.groupBox) self.grid_size_slider.setEnabled(False) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) - sizePolicy.setHeightForWidth(self.grid_size_slider.sizePolicy().hasHeightForWidth()) + sizePolicy.setHeightForWidth( + self.grid_size_slider.sizePolicy().hasHeightForWidth() + ) self.grid_size_slider.setSizePolicy(sizePolicy) self.grid_size_slider.setMaximumSize(QtCore.QSize(16777215, 16777215)) self.grid_size_slider.setMinimum(1) @@ -125,7 +129,9 @@ def setupUi(self, MainWindow): self.verticalLayout_5.setObjectName("verticalLayout_5") self.horizontalLayout_7 = QtWidgets.QHBoxLayout() self.horizontalLayout_7.setObjectName("horizontalLayout_7") - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_7.addItem(spacerItem) self.label = QtWidgets.QLabel(self.groupBox_2) self.label.setObjectName("label") @@ -215,7 +221,9 @@ def setupUi(self, MainWindow): self.remove_roi_button.setFont(font) self.remove_roi_button.setObjectName("remove_roi_button") self.horizontalLayout_4.addWidget(self.remove_roi_button) - spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem1 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_4.addItem(spacerItem1) self.add_roi_button = QtWidgets.QPushButton(self.groupBox_2) font = QtGui.QFont() @@ -255,7 +263,9 @@ def setupUi(self, MainWindow): self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setObjectName("pushButton") self.horizontalLayout.addWidget(self.pushButton) - spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem2 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem2) self.export_button = QtWidgets.QPushButton(self.centralwidget) self.export_button.setObjectName("export_button") @@ -292,7 +302,9 @@ def setupUi(self, MainWindow): self.tabWidget.setCurrentIndex(0) self.file_slider.sliderMoved["int"].connect(MainWindow.slider_file_changed) self.file_slider.valueChanged["int"].connect(MainWindow.slider_file_changed) - self.previous_image_button.clicked.connect(MainWindow.previous_image_button_clicked) + self.previous_image_button.clicked.connect( + MainWindow.previous_image_button_clicked + ) self.next_image_button.clicked.connect(MainWindow.next_image_button_clicked) self.export_button.clicked.connect(MainWindow.export_button_clicked) self.pushButton.clicked.connect(MainWindow.help_button_clicked) @@ -300,12 +312,24 @@ def setupUi(self, MainWindow): self.add_roi_button.clicked.connect(MainWindow.add_row_button_clicked) self.grid_display_checkBox.clicked.connect(MainWindow.display_grid_clicked) self.grid_size_slider.sliderPressed.connect(MainWindow.grid_size_slider_clicked) - self.grid_size_slider.sliderMoved["int"].connect(MainWindow.grid_size_slider_moved) - self.transparency_slider.sliderPressed.connect(MainWindow.transparency_slider_clicked) - self.transparency_slider.sliderMoved["int"].connect(MainWindow.transparency_slider_moved) - self.tableWidget.itemSelectionChanged.connect(MainWindow.table_widget_selection_changed) - self.tableWidget.cellChanged["int", "int"].connect(MainWindow.table_widget_cell_changed) - self.grid_size_slider.sliderReleased.connect(MainWindow.grid_size_slider_released) + self.grid_size_slider.sliderMoved["int"].connect( + MainWindow.grid_size_slider_moved + ) + self.transparency_slider.sliderPressed.connect( + MainWindow.transparency_slider_clicked + ) + self.transparency_slider.sliderMoved["int"].connect( + MainWindow.transparency_slider_moved + ) + self.tableWidget.itemSelectionChanged.connect( + MainWindow.table_widget_selection_changed + ) + self.tableWidget.cellChanged["int", "int"].connect( + MainWindow.table_widget_cell_changed + ) + self.grid_size_slider.sliderReleased.connect( + MainWindow.grid_size_slider_released + ) self.tabWidget.currentChanged["int"].connect(MainWindow.tab_changed) self.add_radioButton.clicked.connect(MainWindow.algo_changed) self.mean_readioButton.clicked.connect(MainWindow.algo_changed) @@ -317,7 +341,9 @@ def retranslateUi(self, MainWindow): self.groupBox.setTitle(_translate("MainWindow", "Grid")) self.grid_display_checkBox.setText(_translate("MainWindow", "Display")) self.display_size_label.setText(_translate("MainWindow", "Size")) - self.display_transparency_label.setText(_translate("MainWindow", "Transparency")) + self.display_transparency_label.setText( + _translate("MainWindow", "Transparency") + ) self.previous_image_button.setText(_translate("MainWindow", "Prev. Image")) self.image_slider_value.setText(_translate("MainWindow", "0")) self.next_image_button.setText(_translate("MainWindow", "Next Image")) @@ -335,22 +361,32 @@ def retranslateUi(self, MainWindow): item.setText(_translate("MainWindow", "Width")) item = self.tableWidget.horizontalHeaderItem(4) item.setText(_translate("MainWindow", "Height")) - self.label_3.setText(_translate("MainWindow", "ROI of selected row is displayed in RED")) + self.label_3.setText( + _translate("MainWindow", "ROI of selected row is displayed in RED") + ) self.remove_roi_button.setText(_translate("MainWindow", "-")) self.add_roi_button.setText(_translate("MainWindow", "+")) - self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Measurement")) + self.tabWidget.setTabText( + self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Measurement") + ) item = self.summary_table.horizontalHeaderItem(0) item.setText(_translate("MainWindow", "Files Name")) item = self.summary_table.horizontalHeaderItem(1) item.setText(_translate("MainWindow", "Time Stamp")) item = self.summary_table.horizontalHeaderItem(2) item.setText(_translate("MainWindow", "Relative Time (s)")) - self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Summary")) + self.tabWidget.setTabText( + self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Summary") + ) self.pushButton.setText(_translate("MainWindow", "Help")) - self.export_button.setText(_translate("MainWindow", "Export Counts vs File Name and Time Stamp ...")) + self.export_button.setText( + _translate("MainWindow", "Export Counts vs File Name and Time Stamp ...") + ) self.actionExport_Profile.setText(_translate("MainWindow", "Profiles ...")) self.actionWater_Intake.setText(_translate("MainWindow", "Water Intake ...")) - self.actionImportedFilesMetadata.setText(_translate("MainWindow", "Imported Files and Metadata ...")) + self.actionImportedFilesMetadata.setText( + _translate("MainWindow", "Imported Files and Metadata ...") + ) self.actionBy_Time_Stamp.setText(_translate("MainWindow", "by Time Stamp")) self.actionBy_File_Name.setText(_translate("MainWindow", "by File Name")) self.actionDsc_files.setText(_translate("MainWindow", "dsc files ...")) diff --git a/notebooks/__code/ui_linear_profile.py b/notebooks/__code/ui_linear_profile.py index 6c7f37e4a..d9c4d8afd 100755 --- a/notebooks/__code/ui_linear_profile.py +++ b/notebooks/__code/ui_linear_profile.py @@ -19,7 +19,9 @@ def setupUi(self, MainWindow): self.splitter.setOrientation(QtCore.Qt.Vertical) self.splitter.setObjectName("splitter") self.widget = QtWidgets.QWidget(self.splitter) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth()) @@ -40,7 +42,9 @@ def setupUi(self, MainWindow): self.file_index_slider.setOrientation(QtCore.Qt.Horizontal) self.file_index_slider.setObjectName("file_index_slider") self.horizontalLayout_2.addWidget(self.file_index_slider) - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_2.addItem(spacerItem) self.verticalLayout.addLayout(self.horizontalLayout_2) self.table_profile = QtWidgets.QTableWidget(self.layoutWidget) @@ -87,7 +91,9 @@ def setupUi(self, MainWindow): self.add_profile_2.setFont(font) self.add_profile_2.setObjectName("add_profile_2") self.horizontalLayout.addWidget(self.add_profile_2) - spacerItem1 = QtWidgets.QSpacerItem(408, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem1 = QtWidgets.QSpacerItem( + 408, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem1) self.pushButton = QtWidgets.QPushButton(self.layoutWidget) self.pushButton.setMinimumSize(QtCore.QSize(53, 46)) @@ -96,7 +102,9 @@ def setupUi(self, MainWindow): self.horizontalLayout.addWidget(self.pushButton) self.verticalLayout.addLayout(self.horizontalLayout) self.verticalLayout_2.addWidget(self.splitter) - spacerItem2 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem2 = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout_2.addItem(spacerItem2) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) @@ -144,4 +152,6 @@ def retranslateUi(self, MainWindow): self.add_profile_2.setText(_translate("MainWindow", "-")) self.pushButton.setText(_translate("MainWindow", "OK")) self.menuFile.setTitle(_translate("MainWindow", "File")) - self.actionExport_Profile.setText(_translate("MainWindow", "Export Profile ...")) + self.actionExport_Profile.setText( + _translate("MainWindow", "Export Profile ...") + ) diff --git a/notebooks/__code/ui_metadata_overlapping_images.py b/notebooks/__code/ui_metadata_overlapping_images.py index ae1c65734..5e9b33018 100755 --- a/notebooks/__code/ui_metadata_overlapping_images.py +++ b/notebooks/__code/ui_metadata_overlapping_images.py @@ -28,10 +28,14 @@ def setupUi(self, MainWindow): self.horizontalLayout_7 = QtWidgets.QHBoxLayout() self.horizontalLayout_7.setObjectName("horizontalLayout_7") self.pyqtgraph_widget = QtWidgets.QWidget(self.layoutWidget) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) - sizePolicy.setHeightForWidth(self.pyqtgraph_widget.sizePolicy().hasHeightForWidth()) + sizePolicy.setHeightForWidth( + self.pyqtgraph_widget.sizePolicy().hasHeightForWidth() + ) self.pyqtgraph_widget.setSizePolicy(sizePolicy) self.pyqtgraph_widget.setObjectName("pyqtgraph_widget") self.horizontalLayout_7.addWidget(self.pyqtgraph_widget) @@ -91,7 +95,9 @@ def setupUi(self, MainWindow): self.scale_vertical_orientation = QtWidgets.QRadioButton(self.scale_groupbox) self.scale_vertical_orientation.setObjectName("scale_vertical_orientation") self.horizontalLayout_2.addWidget(self.scale_vertical_orientation) - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_2.addItem(spacerItem) self.verticalLayout_2.addLayout(self.horizontalLayout_2) self.horizontalLayout_3 = QtWidgets.QHBoxLayout() @@ -132,7 +138,9 @@ def setupUi(self, MainWindow): self.scale_color_combobox.addItem("") self.scale_color_combobox.addItem("") self.horizontalLayout_5.addWidget(self.scale_color_combobox) - spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem1 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_5.addItem(spacerItem1) self.label = QtWidgets.QLabel(self.scale_groupbox) self.label.setObjectName("label") @@ -147,7 +155,9 @@ def setupUi(self, MainWindow): self.horizontalLayout_11.addWidget(self.scale_groupbox) self.verticalLayout_5 = QtWidgets.QVBoxLayout() self.verticalLayout_5.setObjectName("verticalLayout_5") - spacerItem2 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem2 = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout_5.addItem(spacerItem2) self.scale_position_label = QtWidgets.QLabel(self.layoutWidget1) self.scale_position_label.setEnabled(False) @@ -181,7 +191,9 @@ def setupUi(self, MainWindow): self.label_9.setAlignment(QtCore.Qt.AlignCenter) self.label_9.setObjectName("label_9") self.verticalLayout_5.addWidget(self.scale_position_frame) - spacerItem3 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem3 = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout_5.addItem(spacerItem3) self.horizontalLayout_11.addLayout(self.verticalLayout_5) self.verticalLayout_4.addLayout(self.horizontalLayout_11) @@ -259,12 +271,16 @@ def setupUi(self, MainWindow): self.metadata_color_combobox.addItem("") self.metadata_color_combobox.addItem("") self.horizontalLayout_6.addWidget(self.metadata_color_combobox) - spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem4 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_6.addItem(spacerItem4) self.verticalLayout.addLayout(self.horizontalLayout_6) self.horizontalLayout_14 = QtWidgets.QHBoxLayout() self.horizontalLayout_14.setObjectName("horizontalLayout_14") - spacerItem5 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem5 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_14.addItem(spacerItem5) self.import_table_button = QtWidgets.QPushButton(self.metadata_groupbox) self.import_table_button.setObjectName("import_table_button") @@ -273,7 +289,9 @@ def setupUi(self, MainWindow): self.horizontalLayout_13.addWidget(self.metadata_groupbox) self.verticalLayout_8 = QtWidgets.QVBoxLayout() self.verticalLayout_8.setObjectName("verticalLayout_8") - spacerItem6 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem6 = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout_8.addItem(spacerItem6) self.metadata_position_label = QtWidgets.QLabel(self.layoutWidget1) self.metadata_position_label.setEnabled(False) @@ -311,7 +329,9 @@ def setupUi(self, MainWindow): self.label_11.setAlignment(QtCore.Qt.AlignCenter) self.label_11.setObjectName("label_11") self.verticalLayout_8.addWidget(self.metadata_position_frame) - spacerItem7 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem7 = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout_8.addItem(spacerItem7) self.enable_graph_checkbox = QtWidgets.QCheckBox(self.layoutWidget1) self.enable_graph_checkbox.setEnabled(False) @@ -372,7 +392,9 @@ def setupUi(self, MainWindow): self.graph_color_combobox.addItem("") self.graph_color_combobox.addItem("") self.horizontalLayout_10.addWidget(self.graph_color_combobox) - spacerItem8 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem8 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_10.addItem(spacerItem8) self.verticalLayout_9.addLayout(self.horizontalLayout_10) self.horizontalLayout_12 = QtWidgets.QHBoxLayout() @@ -388,11 +410,15 @@ def setupUi(self, MainWindow): self.metadata_graph_size_slider.setOrientation(QtCore.Qt.Horizontal) self.metadata_graph_size_slider.setObjectName("metadata_graph_size_slider") self.horizontalLayout_12.addWidget(self.metadata_graph_size_slider) - spacerItem9 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem9 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_12.addItem(spacerItem9) self.verticalLayout_9.addLayout(self.horizontalLayout_12) self.verticalLayout_8.addWidget(self.graph_groupBox) - spacerItem10 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem10 = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout_8.addItem(spacerItem10) self.horizontalLayout_13.addLayout(self.verticalLayout_8) self.verticalLayout_6.addLayout(self.horizontalLayout_13) @@ -402,7 +428,9 @@ def setupUi(self, MainWindow): self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setObjectName("pushButton") self.horizontalLayout.addWidget(self.pushButton) - spacerItem11 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem11 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem11) self.export_button = QtWidgets.QPushButton(self.centralwidget) self.export_button.setObjectName("export_button") @@ -439,41 +467,93 @@ def setupUi(self, MainWindow): self.export_button.clicked.connect(MainWindow.export_button_clicked) self.pushButton.clicked.connect(MainWindow.help_button_clicked) self.scale_checkbox.clicked["bool"].connect(MainWindow.scale_checkbox_clicked) - self.metadata_checkbox.clicked["bool"].connect(MainWindow.metadata_checkbox_clicked) - self.previous_image_button.clicked.connect(MainWindow.previous_image_button_clicked) + self.metadata_checkbox.clicked["bool"].connect( + MainWindow.metadata_checkbox_clicked + ) + self.previous_image_button.clicked.connect( + MainWindow.previous_image_button_clicked + ) self.next_image_button.clicked.connect(MainWindow.next_image_button_clicked) self.file_slider.sliderPressed.connect(MainWindow.slider_file_clicked) self.file_slider.valueChanged["int"].connect(MainWindow.slider_file_changed) - self.select_metadata_checkbox.clicked["bool"].connect(MainWindow.select_metadata_checkbox_clicked) - self.select_metadata_combobox.currentIndexChanged["int"].connect(MainWindow.metadata_list_changed) - self.scale_horizontal_orientation.clicked.connect(MainWindow.scale_orientation_clicked) - self.scale_vertical_orientation.clicked.connect(MainWindow.scale_orientation_clicked) - self.scale_thickness.valueChanged["int"].connect(MainWindow.scale_thickness_value_changed) - self.scale_color_combobox.currentIndexChanged["int"].connect(MainWindow.scale_color_changed) - self.scale_size_spinbox.valueChanged["int"].connect(MainWindow.scale_size_changed) + self.select_metadata_checkbox.clicked["bool"].connect( + MainWindow.select_metadata_checkbox_clicked + ) + self.select_metadata_combobox.currentIndexChanged["int"].connect( + MainWindow.metadata_list_changed + ) + self.scale_horizontal_orientation.clicked.connect( + MainWindow.scale_orientation_clicked + ) + self.scale_vertical_orientation.clicked.connect( + MainWindow.scale_orientation_clicked + ) + self.scale_thickness.valueChanged["int"].connect( + MainWindow.scale_thickness_value_changed + ) + self.scale_color_combobox.currentIndexChanged["int"].connect( + MainWindow.scale_color_changed + ) + self.scale_size_spinbox.valueChanged["int"].connect( + MainWindow.scale_size_changed + ) self.scale_real_size.returnPressed.connect(MainWindow.scale_real_size_changed) - self.scale_units_combobox.currentIndexChanged["int"].connect(MainWindow.scale_units_changed) - self.scale_position_x.sliderMoved["int"].connect(MainWindow.scale_position_moved) + self.scale_units_combobox.currentIndexChanged["int"].connect( + MainWindow.scale_units_changed + ) + self.scale_position_x.sliderMoved["int"].connect( + MainWindow.scale_position_moved + ) self.scale_position_x.sliderPressed.connect(MainWindow.scale_position_clicked) - self.metadata_position_x.sliderMoved["int"].connect(MainWindow.metadata_position_moved) - self.metadata_position_x.sliderPressed.connect(MainWindow.metadata_position_clicked) - self.metadata_position_y.sliderMoved["int"].connect(MainWindow.metadata_position_moved) - self.metadata_position_y.sliderPressed.connect(MainWindow.metadata_position_clicked) - self.scale_position_y.sliderMoved["int"].connect(MainWindow.scale_position_moved) + self.metadata_position_x.sliderMoved["int"].connect( + MainWindow.metadata_position_moved + ) + self.metadata_position_x.sliderPressed.connect( + MainWindow.metadata_position_clicked + ) + self.metadata_position_y.sliderMoved["int"].connect( + MainWindow.metadata_position_moved + ) + self.metadata_position_y.sliderPressed.connect( + MainWindow.metadata_position_clicked + ) + self.scale_position_y.sliderMoved["int"].connect( + MainWindow.scale_position_moved + ) self.scale_position_y.sliderPressed.connect(MainWindow.scale_position_clicked) - self.metadata_color_combobox.currentIndexChanged["int"].connect(MainWindow.metadata_color_changed) - self.manual_metadata_name.returnPressed.connect(MainWindow.metadata_name_return_pressed) - self.manual_metadata_units.returnPressed.connect(MainWindow.metadata_name_return_pressed) - self.metadata_graph_size_slider.sliderPressed.connect(MainWindow.metadata_graph_size_pressed) - self.metadata_graph_size_slider.sliderMoved["int"].connect(MainWindow.metadata_graph_size_moved) + self.metadata_color_combobox.currentIndexChanged["int"].connect( + MainWindow.metadata_color_changed + ) + self.manual_metadata_name.returnPressed.connect( + MainWindow.metadata_name_return_pressed + ) + self.manual_metadata_units.returnPressed.connect( + MainWindow.metadata_name_return_pressed + ) + self.metadata_graph_size_slider.sliderPressed.connect( + MainWindow.metadata_graph_size_pressed + ) + self.metadata_graph_size_slider.sliderMoved["int"].connect( + MainWindow.metadata_graph_size_moved + ) self.import_table_button.clicked.connect(MainWindow.import_table_pressed) - self.tableWidget.customContextMenuRequested["QPoint"].connect(MainWindow.metadata_table_right_click) + self.tableWidget.customContextMenuRequested["QPoint"].connect( + MainWindow.metadata_table_right_click + ) self.graph_position_y.sliderPressed.connect(MainWindow.graph_position_clicked) self.graph_position_x.sliderPressed.connect(MainWindow.graph_position_clicked) - self.graph_position_x.sliderMoved["int"].connect(MainWindow.graph_position_moved) - self.graph_position_y.sliderMoved["int"].connect(MainWindow.graph_position_moved) - self.enable_graph_checkbox.stateChanged["int"].connect(MainWindow.enable_graph_button_clicked) - self.graph_color_combobox.currentIndexChanged["int"].connect(MainWindow.graph_color_changed) + self.graph_position_x.sliderMoved["int"].connect( + MainWindow.graph_position_moved + ) + self.graph_position_y.sliderMoved["int"].connect( + MainWindow.graph_position_moved + ) + self.enable_graph_checkbox.stateChanged["int"].connect( + MainWindow.enable_graph_button_clicked + ) + self.graph_color_combobox.currentIndexChanged["int"].connect( + MainWindow.graph_color_changed + ) self.graph_position_x.sliderReleased.connect(MainWindow.graph_position_clicked) self.graph_position_y.sliderReleased.connect(MainWindow.graph_position_clicked) QtCore.QMetaObject.connectSlotsByName(MainWindow) @@ -486,7 +566,9 @@ def retranslateUi(self, MainWindow): self.next_image_button.setText(_translate("MainWindow", "Next Image")) self.scale_checkbox.setText(_translate("MainWindow", "Scale")) self.label_2.setText(_translate("MainWindow", "Orientation:")) - self.scale_horizontal_orientation.setText(_translate("MainWindow", "horizontal")) + self.scale_horizontal_orientation.setText( + _translate("MainWindow", "horizontal") + ) self.scale_vertical_orientation.setText(_translate("MainWindow", "vertical")) self.label_3.setText(_translate("MainWindow", "Size:")) self.label_4.setText(_translate("MainWindow", "pixels ==")) @@ -502,7 +584,9 @@ def retranslateUi(self, MainWindow): self.label_8.setText(_translate("MainWindow", "x")) self.label_9.setText(_translate("MainWindow", "y")) self.metadata_checkbox.setText(_translate("MainWindow", "Metadata")) - self.select_metadata_checkbox.setText(_translate("MainWindow", "Select metadata")) + self.select_metadata_checkbox.setText( + _translate("MainWindow", "Select metadata") + ) self.meta_label.setText(_translate("MainWindow", "Legend:")) self.label_12.setText(_translate("MainWindow", "... [Value] ...")) item = self.tableWidget.horizontalHeaderItem(0) @@ -520,7 +604,9 @@ def retranslateUi(self, MainWindow): self.label_10.setText(_translate("MainWindow", "x")) self.label_11.setText(_translate("MainWindow", "y")) self.enable_graph_checkbox.setText(_translate("MainWindow", "Enable Graph")) - self.metadata_position_label_4.setText(_translate("MainWindow", "Graph Position")) + self.metadata_position_label_4.setText( + _translate("MainWindow", "Graph Position") + ) self.label_15.setText(_translate("MainWindow", "x")) self.label_16.setText(_translate("MainWindow", "y")) self.label_13.setText(_translate("MainWindow", "Color:")) @@ -534,7 +620,9 @@ def retranslateUi(self, MainWindow): self.export_button.setText(_translate("MainWindow", "Export Images ...")) self.actionExport_Profile.setText(_translate("MainWindow", "Profiles ...")) self.actionWater_Intake.setText(_translate("MainWindow", "Water Intake ...")) - self.actionImportedFilesMetadata.setText(_translate("MainWindow", "Imported Files and Metadata ...")) + self.actionImportedFilesMetadata.setText( + _translate("MainWindow", "Imported Files and Metadata ...") + ) self.actionBy_Time_Stamp.setText(_translate("MainWindow", "by Time Stamp")) self.actionBy_File_Name.setText(_translate("MainWindow", "by File Name")) self.actionDsc_files.setText(_translate("MainWindow", "dsc files ...")) diff --git a/notebooks/__code/ui_metadata_overlapping_images_string_format.py b/notebooks/__code/ui_metadata_overlapping_images_string_format.py index 33855e321..4683aca22 100755 --- a/notebooks/__code/ui_metadata_overlapping_images_string_format.py +++ b/notebooks/__code/ui_metadata_overlapping_images_string_format.py @@ -47,12 +47,16 @@ def setupUi(self, MainWindow): self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_2.setObjectName("pushButton_2") self.horizontalLayout.addWidget(self.pushButton_2) - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem) self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_3.setObjectName("pushButton_3") self.horizontalLayout.addWidget(self.pushButton_3) - spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem1 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem1) self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setObjectName("pushButton") @@ -68,8 +72,12 @@ def setupUi(self, MainWindow): MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) - self.first_part_lineEdit.textChanged["QString"].connect(MainWindow.string_format_changed) - self.second_part_lineEdit.textChanged["QString"].connect(MainWindow.string_format_changed) + self.first_part_lineEdit.textChanged["QString"].connect( + MainWindow.string_format_changed + ) + self.second_part_lineEdit.textChanged["QString"].connect( + MainWindow.string_format_changed + ) self.pushButton_3.clicked.connect(MainWindow.launch_help) self.pushButton_2.clicked.connect(MainWindow.cancel) self.pushButton.clicked.connect(MainWindow.ok) diff --git a/notebooks/__code/ui_panoramic_stitching.py b/notebooks/__code/ui_panoramic_stitching.py index 3b7986d00..5ca958a06 100755 --- a/notebooks/__code/ui_panoramic_stitching.py +++ b/notebooks/__code/ui_panoramic_stitching.py @@ -28,8 +28,12 @@ def setupUi(self, MainWindow): self.verticalLayout_4.setObjectName("verticalLayout_4") self.splitter_between_previews_and_table = QtWidgets.QSplitter(self.tab) self.splitter_between_previews_and_table.setOrientation(QtCore.Qt.Vertical) - self.splitter_between_previews_and_table.setObjectName("splitter_between_previews_and_table") - self.splitter_between_previews = QtWidgets.QSplitter(self.splitter_between_previews_and_table) + self.splitter_between_previews_and_table.setObjectName( + "splitter_between_previews_and_table" + ) + self.splitter_between_previews = QtWidgets.QSplitter( + self.splitter_between_previews_and_table + ) self.splitter_between_previews.setOrientation(QtCore.Qt.Horizontal) self.splitter_between_previews.setObjectName("splitter_between_previews") self.groupBox_2 = QtWidgets.QGroupBox(self.splitter_between_previews) @@ -52,7 +56,9 @@ def setupUi(self, MainWindow): self.target_widget.setObjectName("target_widget") self.horizontalLayout_2.addWidget(self.target_widget) self.verticalLayout.addLayout(self.horizontalLayout_2) - self.tableWidget = QtWidgets.QTableWidget(self.splitter_between_previews_and_table) + self.tableWidget = QtWidgets.QTableWidget( + self.splitter_between_previews_and_table + ) self.tableWidget.setMaximumSize(QtCore.QSize(16777215, 200)) self.tableWidget.setAlternatingRowColors(True) self.tableWidget.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection) @@ -108,7 +114,9 @@ def setupUi(self, MainWindow): self.up_button.setFlat(True) self.up_button.setObjectName("up_button") self.gridLayout.addWidget(self.up_button, 1, 3, 1, 1) - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.gridLayout.addItem(spacerItem, 2, 0, 1, 1) self.left_left_button = QtWidgets.QPushButton(self.groupBox_4) self.left_left_button.setStyleSheet("border: None") @@ -125,7 +133,9 @@ def setupUi(self, MainWindow): self.left_button.setFlat(True) self.left_button.setObjectName("left_button") self.gridLayout.addWidget(self.left_button, 2, 2, 1, 1) - spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum) + spacerItem1 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum + ) self.gridLayout.addItem(spacerItem1, 2, 3, 1, 1) self.right_button = QtWidgets.QPushButton(self.groupBox_4) self.right_button.setStyleSheet("border: None") @@ -139,7 +149,9 @@ def setupUi(self, MainWindow): self.right_right_button.setFlat(True) self.right_right_button.setObjectName("right_right_button") self.gridLayout.addWidget(self.right_right_button, 2, 5, 1, 1) - spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem2 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.gridLayout.addItem(spacerItem2, 2, 6, 1, 1) self.down_button = QtWidgets.QPushButton(self.groupBox_4) self.down_button.setStyleSheet("border: None") @@ -157,7 +169,9 @@ def setupUi(self, MainWindow): self.verticalLayout_7.addWidget(self.groupBox_4) self.horizontalLayout_4 = QtWidgets.QHBoxLayout() self.horizontalLayout_4.setObjectName("horizontalLayout_4") - spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem3 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_4.addItem(spacerItem3) self.label_2 = QtWidgets.QLabel(self.tab_2) self.label_2.setObjectName("label_2") @@ -202,7 +216,9 @@ def setupUi(self, MainWindow): self.cancel_button = QtWidgets.QPushButton(self.centralwidget) self.cancel_button.setObjectName("cancel_button") self.horizontalLayout.addWidget(self.cancel_button) - spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem4 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem4) self.export_button = QtWidgets.QPushButton(self.centralwidget) self.export_button.setObjectName("export_button") @@ -225,8 +241,12 @@ def setupUi(self, MainWindow): self.tabWidget.setCurrentIndex(0) self.cancel_button.clicked.connect(MainWindow.cancel_clicked) # type: ignore self.export_button.clicked.connect(MainWindow.apply_clicked) # type: ignore - self.tableWidget.itemSelectionChanged.connect(MainWindow.table_widget_selection_changed) # type: ignore - self.run_stitching_button.clicked.connect(MainWindow.run_stitching_button_clicked) # type: ignore + self.tableWidget.itemSelectionChanged.connect( + MainWindow.table_widget_selection_changed + ) # type: ignore + self.run_stitching_button.clicked.connect( + MainWindow.run_stitching_button_clicked + ) # type: ignore self.left_button.pressed.connect(MainWindow.left_button_pressed) # type: ignore self.left_button.released.connect(MainWindow.left_button_released) # type: ignore self.left_left_button.pressed.connect(MainWindow.left_left_button_pressed) # type: ignore @@ -257,12 +277,16 @@ def retranslateUi(self, MainWindow): item = self.tableWidget.horizontalHeaderItem(2) item.setText(_translate("MainWindow", "Status")) self.run_stitching_button.setText(_translate("MainWindow", "Run Stitching ")) - self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Step 1")) + self.tabWidget.setTabText( + self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Step 1") + ) self.label.setText(_translate("MainWindow", "File Names")) self.groupBox_4.setTitle(_translate("MainWindow", "Manual Mode")) self.label_2.setText(_translate("MainWindow", "->")) self.label_5.setText(_translate("MainWindow", "->>")) - self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Step 2")) + self.tabWidget.setTabText( + self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Step 2") + ) self.groupBox_3.setTitle(_translate("MainWindow", "Stiched Image")) self.cancel_button.setText(_translate("MainWindow", "Close")) self.export_button.setText(_translate("MainWindow", "Export ...")) diff --git a/notebooks/__code/ui_profile.py b/notebooks/__code/ui_profile.py index 1b0126809..ac1441169 100755 --- a/notebooks/__code/ui_profile.py +++ b/notebooks/__code/ui_profile.py @@ -55,10 +55,14 @@ def setupUi(self, MainWindow): self.horizontalLayout_6.addWidget(self.display_size_label) self.grid_size_slider = QtWidgets.QSlider(self.groupBox) self.grid_size_slider.setEnabled(False) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) - sizePolicy.setHeightForWidth(self.grid_size_slider.sizePolicy().hasHeightForWidth()) + sizePolicy.setHeightForWidth( + self.grid_size_slider.sizePolicy().hasHeightForWidth() + ) self.grid_size_slider.setSizePolicy(sizePolicy) self.grid_size_slider.setMaximumSize(QtCore.QSize(16777215, 16777215)) self.grid_size_slider.setMinimum(1) @@ -132,7 +136,9 @@ def setupUi(self, MainWindow): self.label_6.setObjectName("label_6") self.horizontalLayout_8.addWidget(self.label_6) self.filename = QtWidgets.QLabel(self.layoutWidget) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.filename.sizePolicy().hasHeightForWidth()) @@ -268,7 +274,9 @@ def setupUi(self, MainWindow): self.pushButton_4.setFont(font) self.pushButton_4.setObjectName("pushButton_4") self.horizontalLayout_4.addWidget(self.pushButton_4) - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_4.addItem(spacerItem) self.groupBox_2 = QtWidgets.QGroupBox(self.layoutWidget1) self.groupBox_2.setObjectName("groupBox_2") @@ -283,7 +291,9 @@ def setupUi(self, MainWindow): self.profile_direction_y_axis.setObjectName("profile_direction_y_axis") self.horizontalLayout_7.addWidget(self.profile_direction_y_axis) self.horizontalLayout_4.addWidget(self.groupBox_2) - spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem1 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_4.addItem(spacerItem1) self.pushButton_5 = QtWidgets.QPushButton(self.layoutWidget1) font = QtGui.QFont() @@ -316,10 +326,16 @@ def setupUi(self, MainWindow): self.all_plots_verti_splitter = QtWidgets.QSplitter(self.layoutWidget2) self.all_plots_verti_splitter.setOrientation(QtCore.Qt.Vertical) self.all_plots_verti_splitter.setObjectName("all_plots_verti_splitter") - self.all_plots_file_name_table = QtWidgets.QTableWidget(self.all_plots_verti_splitter) + self.all_plots_file_name_table = QtWidgets.QTableWidget( + self.all_plots_verti_splitter + ) self.all_plots_file_name_table.setAlternatingRowColors(True) - self.all_plots_file_name_table.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection) - self.all_plots_file_name_table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows) + self.all_plots_file_name_table.setSelectionMode( + QtWidgets.QAbstractItemView.MultiSelection + ) + self.all_plots_file_name_table.setSelectionBehavior( + QtWidgets.QAbstractItemView.SelectRows + ) self.all_plots_file_name_table.setObjectName("all_plots_file_name_table") self.all_plots_file_name_table.setColumnCount(1) self.all_plots_file_name_table.setRowCount(0) @@ -327,10 +343,16 @@ def setupUi(self, MainWindow): self.all_plots_file_name_table.setHorizontalHeaderItem(0, item) self.all_plots_file_name_table.horizontalHeader().setStretchLastSection(True) self.all_plots_file_name_table.verticalHeader().setStretchLastSection(False) - self.all_plots_profiles_table = QtWidgets.QTableWidget(self.all_plots_verti_splitter) + self.all_plots_profiles_table = QtWidgets.QTableWidget( + self.all_plots_verti_splitter + ) self.all_plots_profiles_table.setAlternatingRowColors(True) - self.all_plots_profiles_table.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection) - self.all_plots_profiles_table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows) + self.all_plots_profiles_table.setSelectionMode( + QtWidgets.QAbstractItemView.MultiSelection + ) + self.all_plots_profiles_table.setSelectionBehavior( + QtWidgets.QAbstractItemView.SelectRows + ) self.all_plots_profiles_table.setObjectName("all_plots_profiles_table") self.all_plots_profiles_table.setColumnCount(1) self.all_plots_profiles_table.setRowCount(0) @@ -366,7 +388,9 @@ def setupUi(self, MainWindow): self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setObjectName("pushButton") self.horizontalLayout.addWidget(self.pushButton) - spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem2 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem2) self.export_button = QtWidgets.QPushButton(self.centralwidget) self.export_button.setObjectName("export_button") @@ -403,7 +427,9 @@ def setupUi(self, MainWindow): self.tabWidget.setCurrentIndex(0) self.file_slider.sliderMoved["int"].connect(MainWindow.slider_file_changed) # type: ignore self.file_slider.valueChanged["int"].connect(MainWindow.slider_file_changed) # type: ignore - self.previous_image_button.clicked.connect(MainWindow.previous_image_button_clicked) # type: ignore + self.previous_image_button.clicked.connect( + MainWindow.previous_image_button_clicked + ) # type: ignore self.next_image_button.clicked.connect(MainWindow.next_image_button_clicked) # type: ignore self.export_button.clicked.connect(MainWindow.export_button_clicked) # type: ignore self.pushButton.clicked.connect(MainWindow.help_button_clicked) # type: ignore @@ -411,22 +437,52 @@ def setupUi(self, MainWindow): self.pushButton_5.clicked.connect(MainWindow.add_row_button_clicked) # type: ignore self.grid_display_checkBox.clicked.connect(MainWindow.display_grid_clicked) # type: ignore self.grid_size_slider.sliderPressed.connect(MainWindow.grid_size_slider_clicked) # type: ignore - self.grid_size_slider.sliderMoved["int"].connect(MainWindow.grid_size_slider_moved) # type: ignore - self.transparency_slider.sliderPressed.connect(MainWindow.transparency_slider_clicked) # type: ignore - self.transparency_slider.sliderMoved["int"].connect(MainWindow.transparency_slider_moved) # type: ignore - self.right_rotation_button_fast.clicked.connect(MainWindow.right_rotation_fast_clicked) # type: ignore - self.right_rotation_button_slow.clicked.connect(MainWindow.right_rotation_slow_clicked) # type: ignore - self.left_rotation_button_fast.clicked.connect(MainWindow.left_rotation_fast_clicked) # type: ignore - self.left_rotation_button_slow.clicked.connect(MainWindow.left_rotation_slow_clicked) # type: ignore - self.tableWidget.itemSelectionChanged.connect(MainWindow.table_widget_selection_changed) # type: ignore - self.tableWidget_2.itemSelectionChanged.connect(MainWindow.table_widget_2_selection_changed) # type: ignore - self.tableWidget.cellChanged["int", "int"].connect(MainWindow.table_widget_cell_changed) # type: ignore - self.grid_size_slider.sliderReleased.connect(MainWindow.grid_size_slider_released) # type: ignore - self.profile_direction_x_axis.clicked.connect(MainWindow.profile_along_axis_changed) # type: ignore - self.profile_direction_y_axis.clicked.connect(MainWindow.profile_along_axis_changed) # type: ignore + self.grid_size_slider.sliderMoved["int"].connect( + MainWindow.grid_size_slider_moved + ) # type: ignore + self.transparency_slider.sliderPressed.connect( + MainWindow.transparency_slider_clicked + ) # type: ignore + self.transparency_slider.sliderMoved["int"].connect( + MainWindow.transparency_slider_moved + ) # type: ignore + self.right_rotation_button_fast.clicked.connect( + MainWindow.right_rotation_fast_clicked + ) # type: ignore + self.right_rotation_button_slow.clicked.connect( + MainWindow.right_rotation_slow_clicked + ) # type: ignore + self.left_rotation_button_fast.clicked.connect( + MainWindow.left_rotation_fast_clicked + ) # type: ignore + self.left_rotation_button_slow.clicked.connect( + MainWindow.left_rotation_slow_clicked + ) # type: ignore + self.tableWidget.itemSelectionChanged.connect( + MainWindow.table_widget_selection_changed + ) # type: ignore + self.tableWidget_2.itemSelectionChanged.connect( + MainWindow.table_widget_2_selection_changed + ) # type: ignore + self.tableWidget.cellChanged["int", "int"].connect( + MainWindow.table_widget_cell_changed + ) # type: ignore + self.grid_size_slider.sliderReleased.connect( + MainWindow.grid_size_slider_released + ) # type: ignore + self.profile_direction_x_axis.clicked.connect( + MainWindow.profile_along_axis_changed + ) # type: ignore + self.profile_direction_y_axis.clicked.connect( + MainWindow.profile_along_axis_changed + ) # type: ignore self.tabWidget.currentChanged["int"].connect(MainWindow.tab_changed) # type: ignore - self.all_plots_file_name_table.itemSelectionChanged.connect(MainWindow.update_all_plots) # type: ignore - self.all_plots_profiles_table.itemSelectionChanged.connect(MainWindow.update_all_plots) # type: ignore + self.all_plots_file_name_table.itemSelectionChanged.connect( + MainWindow.update_all_plots + ) # type: ignore + self.all_plots_profiles_table.itemSelectionChanged.connect( + MainWindow.update_all_plots + ) # type: ignore QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): @@ -435,7 +491,9 @@ def retranslateUi(self, MainWindow): self.groupBox.setTitle(_translate("MainWindow", "Grid")) self.grid_display_checkBox.setText(_translate("MainWindow", "Display")) self.display_size_label.setText(_translate("MainWindow", "Size")) - self.display_transparency_label.setText(_translate("MainWindow", "Transparency")) + self.display_transparency_label.setText( + _translate("MainWindow", "Transparency") + ) self.previous_image_button.setText(_translate("MainWindow", "Prev. Image")) self.image_slider_value.setText(_translate("MainWindow", "0")) self.next_image_button.setText(_translate("MainWindow", "Next Image")) @@ -455,20 +513,30 @@ def retranslateUi(self, MainWindow): item.setText(_translate("MainWindow", "Height")) item = self.tableWidget_2.horizontalHeaderItem(0) item.setText(_translate("MainWindow", "Width")) - self.label_3.setText(_translate("MainWindow", "ROI of selected row is displayed in RED")) + self.label_3.setText( + _translate("MainWindow", "ROI of selected row is displayed in RED") + ) self.pushButton_4.setText(_translate("MainWindow", "-")) self.groupBox_2.setTitle(_translate("MainWindow", "Profile Direction")) self.profile_direction_x_axis.setText(_translate("MainWindow", "x-axis")) self.profile_direction_y_axis.setText(_translate("MainWindow", "y-axis")) self.pushButton_5.setText(_translate("MainWindow", "+")) - self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Measurement")) - self.label_5.setText(_translate("MainWindow", "Select the FILE(s) and the PROFILE(s) you want to display!")) + self.tabWidget.setTabText( + self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Measurement") + ) + self.label_5.setText( + _translate( + "MainWindow", + "Select the FILE(s) and the PROFILE(s) you want to display!", + ) + ) item = self.all_plots_file_name_table.horizontalHeaderItem(0) item.setText(_translate("MainWindow", "File Names")) item = self.all_plots_profiles_table.horizontalHeaderItem(0) item.setText(_translate("MainWindow", "Profiles")) self.tabWidget.setTabText( - self.tabWidget.indexOf(self.tab_3), _translate("MainWindow", "All Profiles / All Images") + self.tabWidget.indexOf(self.tab_3), + _translate("MainWindow", "All Profiles / All Images"), ) item = self.summary_table.horizontalHeaderItem(0) item.setText(_translate("MainWindow", "Files Name")) @@ -476,12 +544,16 @@ def retranslateUi(self, MainWindow): item.setText(_translate("MainWindow", "Time Stamp")) item = self.summary_table.horizontalHeaderItem(2) item.setText(_translate("MainWindow", "Relative Time (s)")) - self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Summary")) + self.tabWidget.setTabText( + self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Summary") + ) self.pushButton.setText(_translate("MainWindow", "Help")) self.export_button.setText(_translate("MainWindow", "Export Profiles ...")) self.actionExport_Profile.setText(_translate("MainWindow", "Profiles ...")) self.actionWater_Intake.setText(_translate("MainWindow", "Water Intake ...")) - self.actionImportedFilesMetadata.setText(_translate("MainWindow", "Imported Files and Metadata ...")) + self.actionImportedFilesMetadata.setText( + _translate("MainWindow", "Imported Files and Metadata ...") + ) self.actionBy_Time_Stamp.setText(_translate("MainWindow", "by Time Stamp")) self.actionBy_File_Name.setText(_translate("MainWindow", "by File Name")) self.actionDsc_files.setText(_translate("MainWindow", "dsc files ...")) diff --git a/notebooks/__code/ui_radial_profile.py b/notebooks/__code/ui_radial_profile.py index fa41659c2..0cceda0d6 100755 --- a/notebooks/__code/ui_radial_profile.py +++ b/notebooks/__code/ui_radial_profile.py @@ -24,7 +24,9 @@ def setupUi(self, MainWindow): self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.widget = QtWidgets.QWidget(self.tab_2) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth()) @@ -34,7 +36,9 @@ def setupUi(self, MainWindow): self.verticalLayout_3 = QtWidgets.QVBoxLayout() self.verticalLayout_3.setObjectName("verticalLayout_3") self.groupBox_2 = QtWidgets.QGroupBox(self.tab_2) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Preferred) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Preferred + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.groupBox_2.sizePolicy().hasHeightForWidth()) @@ -65,7 +69,8 @@ def setupUi(self, MainWindow): self.verticalLayout_3.addWidget(self.groupBox_2) self.groupBox_3 = QtWidgets.QGroupBox(self.tab_2) sizePolicy = QtWidgets.QSizePolicy( - QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.MinimumExpanding + QtWidgets.QSizePolicy.MinimumExpanding, + QtWidgets.QSizePolicy.MinimumExpanding, ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) @@ -91,7 +96,9 @@ def setupUi(self, MainWindow): self.sector_from_value.setMinimumSize(QtCore.QSize(30, 0)) self.sector_from_value.setMaximumSize(QtCore.QSize(30, 16777215)) self.sector_from_value.setText("") - self.sector_from_value.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter) + self.sector_from_value.setAlignment( + QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter + ) self.sector_from_value.setObjectName("sector_from_value") self.gridLayout_2.addWidget(self.sector_from_value, 0, 2, 1, 1) self.sector_to_label = QtWidgets.QLabel(self.groupBox_3) @@ -102,10 +109,14 @@ def setupUi(self, MainWindow): self.gridLayout_2.addWidget(self.sector_from_label, 0, 0, 1, 1) self.from_angle_slider = QtWidgets.QScrollBar(self.groupBox_3) self.from_angle_slider.setEnabled(False) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) - sizePolicy.setHeightForWidth(self.from_angle_slider.sizePolicy().hasHeightForWidth()) + sizePolicy.setHeightForWidth( + self.from_angle_slider.sizePolicy().hasHeightForWidth() + ) self.from_angle_slider.setSizePolicy(sizePolicy) self.from_angle_slider.setMaximum(360) self.from_angle_slider.setOrientation(QtCore.Qt.Horizontal) @@ -113,7 +124,9 @@ def setupUi(self, MainWindow): self.gridLayout_2.addWidget(self.from_angle_slider, 0, 1, 1, 1) self.sector_to_value = QtWidgets.QLabel(self.groupBox_3) self.sector_to_value.setText("") - self.sector_to_value.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter) + self.sector_to_value.setAlignment( + QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter + ) self.sector_to_value.setObjectName("sector_to_value") self.gridLayout_2.addWidget(self.sector_to_value, 1, 2, 1, 1) self.to_angle_slider = QtWidgets.QScrollBar(self.groupBox_3) @@ -182,12 +195,16 @@ def setupUi(self, MainWindow): self.grid_size_slider.setObjectName("grid_size_slider") self.gridLayout_3.addWidget(self.grid_size_slider, 4, 1, 1, 1) self.verticalLayout_3.addWidget(self.groupBox_4) - spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout_3.addItem(spacerItem) self.horizontalLayout_2.addLayout(self.verticalLayout_3) self.verticalLayout_4.addLayout(self.horizontalLayout_2) self.groupBox = QtWidgets.QGroupBox(self.tab_2) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.groupBox.sizePolicy().hasHeightForWidth()) @@ -200,7 +217,9 @@ def setupUi(self, MainWindow): self.verticalLayout.setObjectName("verticalLayout") self.textBrowser = QtWidgets.QTextBrowser(self.groupBox) self.textBrowser.setEnabled(False) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.textBrowser.sizePolicy().hasHeightForWidth()) @@ -219,10 +238,14 @@ def setupUi(self, MainWindow): self.calculate_profiles_button.setObjectName("calculate_profiles_button") self.verticalLayout_6.addWidget(self.calculate_profiles_button) self.widget_profile = QtWidgets.QWidget(self.tab) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) - sizePolicy.setHeightForWidth(self.widget_profile.sizePolicy().hasHeightForWidth()) + sizePolicy.setHeightForWidth( + self.widget_profile.sizePolicy().hasHeightForWidth() + ) self.widget_profile.setSizePolicy(sizePolicy) self.widget_profile.setObjectName("widget_profile") self.verticalLayout_6.addWidget(self.widget_profile) @@ -237,7 +260,9 @@ def setupUi(self, MainWindow): self.help_button = QtWidgets.QPushButton(self.centralwidget) self.help_button.setObjectName("help_button") self.horizontalLayout.addWidget(self.help_button) - spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem1 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem1) self.cancel_button = QtWidgets.QPushButton(self.centralwidget) self.cancel_button.setObjectName("cancel_button") @@ -258,16 +283,30 @@ def setupUi(self, MainWindow): self.sector_full_circle.clicked.connect(MainWindow.sector_radio_button_changed) self.sector_sector.clicked.connect(MainWindow.sector_radio_button_changed) self.guide_red_slider.sliderMoved["int"].connect(MainWindow.guide_color_changed) - self.guide_green_slider.sliderMoved["int"].connect(MainWindow.guide_color_changed) - self.guide_blue_slider.sliderMoved["int"].connect(MainWindow.guide_color_changed) - self.guide_alpha_slider.sliderMoved["int"].connect(MainWindow.guide_color_changed) + self.guide_green_slider.sliderMoved["int"].connect( + MainWindow.guide_color_changed + ) + self.guide_blue_slider.sliderMoved["int"].connect( + MainWindow.guide_color_changed + ) + self.guide_alpha_slider.sliderMoved["int"].connect( + MainWindow.guide_color_changed + ) self.grid_size_slider.sliderMoved["int"].connect(MainWindow.grid_slider_moved) self.grid_size_slider.sliderPressed.connect(MainWindow.grid_slider_pressed) - self.from_angle_slider.sliderMoved["int"].connect(MainWindow.sector_from_angle_moved) - self.to_angle_slider.sliderMoved["int"].connect(MainWindow.sector_to_angle_moved) - self.from_angle_slider.sliderPressed.connect(MainWindow.sector_from_angle_clicked) + self.from_angle_slider.sliderMoved["int"].connect( + MainWindow.sector_from_angle_moved + ) + self.to_angle_slider.sliderMoved["int"].connect( + MainWindow.sector_to_angle_moved + ) + self.from_angle_slider.sliderPressed.connect( + MainWindow.sector_from_angle_clicked + ) self.to_angle_slider.sliderPressed.connect(MainWindow.sector_to_angle_clicked) - self.from_angle_slider.sliderReleased.connect(MainWindow.sector_from_angle_clicked) + self.from_angle_slider.sliderReleased.connect( + MainWindow.sector_from_angle_clicked + ) self.to_angle_slider.sliderReleased.connect(MainWindow.sector_to_angle_clicked) self.guide_red_slider.sliderPressed.connect(MainWindow.guide_color_clicked) self.guide_red_slider.sliderReleased.connect(MainWindow.guide_color_released) @@ -278,9 +317,15 @@ def setupUi(self, MainWindow): self.guide_alpha_slider.sliderPressed.connect(MainWindow.guide_color_clicked) self.guide_alpha_slider.sliderReleased.connect(MainWindow.guide_color_released) self.grid_size_slider.sliderReleased.connect(MainWindow.grid_slider_pressed) - self.from_angle_slider.valueChanged["int"].connect(MainWindow.sector_from_angle_moved) - self.to_angle_slider.valueChanged["int"].connect(MainWindow.sector_to_angle_moved) - self.calculate_profiles_button.clicked.connect(MainWindow.calculate_profiles_clicked) + self.from_angle_slider.valueChanged["int"].connect( + MainWindow.sector_from_angle_moved + ) + self.to_angle_slider.valueChanged["int"].connect( + MainWindow.sector_to_angle_moved + ) + self.calculate_profiles_button.clicked.connect( + MainWindow.calculate_profiles_clicked + ) self.export_profiles_button.clicked.connect(MainWindow.export_profiles_clicked) self.help_button.clicked.connect(MainWindow.help_button_clicked) QtCore.QMetaObject.connectSlotsByName(MainWindow) @@ -317,9 +362,18 @@ def retranslateUi(self, MainWindow): '

* Export profiles

', ) ) - self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Initialization")) - self.calculate_profiles_button.setText(_translate("MainWindow", "Calculate Profiles")) - self.export_profiles_button.setText(_translate("MainWindow", "Export Profiles ...")) - self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Profile")) + self.tabWidget.setTabText( + self.tabWidget.indexOf(self.tab_2), + _translate("MainWindow", "Initialization"), + ) + self.calculate_profiles_button.setText( + _translate("MainWindow", "Calculate Profiles") + ) + self.export_profiles_button.setText( + _translate("MainWindow", "Export Profiles ...") + ) + self.tabWidget.setTabText( + self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Profile") + ) self.help_button.setText(_translate("MainWindow", "Help")) self.cancel_button.setText(_translate("MainWindow", "Done")) diff --git a/notebooks/__code/ui_registration.py b/notebooks/__code/ui_registration.py index ba0d89d55..5c1a3a370 100755 --- a/notebooks/__code/ui_registration.py +++ b/notebooks/__code/ui_registration.py @@ -22,31 +22,41 @@ def setupUi(self, MainWindow): self.groupBox_2.setObjectName("groupBox_2") self.horizontalLayout_5 = QtWidgets.QHBoxLayout(self.groupBox_2) self.horizontalLayout_5.setObjectName("horizontalLayout_5") - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_5.addItem(spacerItem) self.auto_registration_button = QtWidgets.QPushButton(self.groupBox_2) self.auto_registration_button.setMinimumSize(QtCore.QSize(200, 0)) self.auto_registration_button.setObjectName("auto_registration_button") self.horizontalLayout_5.addWidget(self.auto_registration_button) - spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem1 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_5.addItem(spacerItem1) self.manual_registration_button = QtWidgets.QPushButton(self.groupBox_2) self.manual_registration_button.setMinimumSize(QtCore.QSize(200, 0)) self.manual_registration_button.setObjectName("manual_registration_button") self.horizontalLayout_5.addWidget(self.manual_registration_button) - spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem2 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_5.addItem(spacerItem2) self.marker_registration_button = QtWidgets.QPushButton(self.groupBox_2) self.marker_registration_button.setMinimumSize(QtCore.QSize(200, 0)) self.marker_registration_button.setObjectName("marker_registration_button") self.horizontalLayout_5.addWidget(self.marker_registration_button) - spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem3 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_5.addItem(spacerItem3) self.profiler_registration_button = QtWidgets.QPushButton(self.groupBox_2) self.profiler_registration_button.setMinimumSize(QtCore.QSize(200, 0)) self.profiler_registration_button.setObjectName("profiler_registration_button") self.horizontalLayout_5.addWidget(self.profiler_registration_button) - spacerItem4 = QtWidgets.QSpacerItem(73, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem4 = QtWidgets.QSpacerItem( + 73, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_5.addItem(spacerItem4) self.verticalLayout_4.addWidget(self.groupBox_2) self.groupBox = QtWidgets.QGroupBox(self.centralwidget) @@ -66,10 +76,14 @@ def setupUi(self, MainWindow): self.label_5.setObjectName("label_5") self.horizontalLayout_6.addWidget(self.label_5) self.grid_size_slider = QtWidgets.QSlider(self.groupBox) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) - sizePolicy.setHeightForWidth(self.grid_size_slider.sizePolicy().hasHeightForWidth()) + sizePolicy.setHeightForWidth( + self.grid_size_slider.sizePolicy().hasHeightForWidth() + ) self.grid_size_slider.setSizePolicy(sizePolicy) self.grid_size_slider.setMaximumSize(QtCore.QSize(16777215, 16777215)) self.grid_size_slider.setMinimum(1) @@ -131,27 +145,43 @@ def setupUi(self, MainWindow): self.verticalLayout_5.addWidget(self.bottom_row_label) self.horizontalLayout_2.addWidget(self.selection_groupBox) self.pyqtgraph_widget = QtWidgets.QWidget(self.layoutWidget) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) - sizePolicy.setHeightForWidth(self.pyqtgraph_widget.sizePolicy().hasHeightForWidth()) + sizePolicy.setHeightForWidth( + self.pyqtgraph_widget.sizePolicy().hasHeightForWidth() + ) self.pyqtgraph_widget.setSizePolicy(sizePolicy) self.pyqtgraph_widget.setObjectName("pyqtgraph_widget") self.horizontalLayout_2.addWidget(self.pyqtgraph_widget) - self.selection_reference_opacity_groupBox = QtWidgets.QGroupBox(self.layoutWidget) + self.selection_reference_opacity_groupBox = QtWidgets.QGroupBox( + self.layoutWidget + ) self.selection_reference_opacity_groupBox.setMinimumSize(QtCore.QSize(70, 0)) - self.selection_reference_opacity_groupBox.setMaximumSize(QtCore.QSize(60, 16777215)) - self.selection_reference_opacity_groupBox.setLayoutDirection(QtCore.Qt.LeftToRight) + self.selection_reference_opacity_groupBox.setMaximumSize( + QtCore.QSize(60, 16777215) + ) + self.selection_reference_opacity_groupBox.setLayoutDirection( + QtCore.Qt.LeftToRight + ) self.selection_reference_opacity_groupBox.setTitle("") self.selection_reference_opacity_groupBox.setAlignment(QtCore.Qt.AlignCenter) - self.selection_reference_opacity_groupBox.setObjectName("selection_reference_opacity_groupBox") - self.verticalLayout = QtWidgets.QVBoxLayout(self.selection_reference_opacity_groupBox) + self.selection_reference_opacity_groupBox.setObjectName( + "selection_reference_opacity_groupBox" + ) + self.verticalLayout = QtWidgets.QVBoxLayout( + self.selection_reference_opacity_groupBox + ) self.verticalLayout.setContentsMargins(-1, -1, 19, -1) self.verticalLayout.setObjectName("verticalLayout") self.label_2 = QtWidgets.QLabel(self.selection_reference_opacity_groupBox) self.label_2.setObjectName("label_2") self.verticalLayout.addWidget(self.label_2) - self.opacity_slider = QtWidgets.QSlider(self.selection_reference_opacity_groupBox) + self.opacity_slider = QtWidgets.QSlider( + self.selection_reference_opacity_groupBox + ) self.opacity_slider.setCursor(QtGui.QCursor(QtCore.Qt.SizeVerCursor)) self.opacity_slider.setAccessibleDescription("") self.opacity_slider.setLayoutDirection(QtCore.Qt.RightToLeft) @@ -208,7 +238,9 @@ def setupUi(self, MainWindow): self.horizontalLayout_4.addWidget(self.pushButton_3) self.verticalLayout_3.addLayout(self.horizontalLayout_4) self.tableWidget = QtWidgets.QTableWidget(self.layoutWidget1) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.tableWidget.sizePolicy().hasHeightForWidth()) @@ -220,7 +252,9 @@ def setupUi(self, MainWindow): self.tableWidget.setFrameShadow(QtWidgets.QFrame.Plain) self.tableWidget.setDefaultDropAction(QtCore.Qt.IgnoreAction) self.tableWidget.setAlternatingRowColors(True) - self.tableWidget.setSelectionMode(QtWidgets.QAbstractItemView.ContiguousSelection) + self.tableWidget.setSelectionMode( + QtWidgets.QAbstractItemView.ContiguousSelection + ) self.tableWidget.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows) self.tableWidget.setGridStyle(QtCore.Qt.SolidLine) self.tableWidget.setObjectName("tableWidget") @@ -243,7 +277,9 @@ def setupUi(self, MainWindow): self.help_button.setMaximumSize(QtCore.QSize(100, 30)) self.help_button.setObjectName("help_button") self.horizontalLayout.addWidget(self.help_button) - spacerItem5 = QtWidgets.QSpacerItem(408, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem5 = QtWidgets.QSpacerItem( + 408, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem5) self.export_button = QtWidgets.QPushButton(self.layoutWidget1) self.export_button.setMinimumSize(QtCore.QSize(100, 30)) @@ -286,19 +322,39 @@ def setupUi(self, MainWindow): self.file_slider.valueChanged["int"].connect(MainWindow.slider_file_changed) self.tableWidget.itemSelectionChanged.connect(MainWindow.table_row_clicked) self.opacity_slider.valueChanged["int"].connect(MainWindow.opacity_changed) - self.previous_image_button.clicked.connect(MainWindow.previous_image_button_clicked) + self.previous_image_button.clicked.connect( + MainWindow.previous_image_button_clicked + ) self.next_image_button.clicked.connect(MainWindow.next_image_button_clicked) self.selection_all.clicked.connect(MainWindow.selection_all_clicked) - self.opacity_selection_slider.sliderPressed.connect(MainWindow.selection_slider_changed) - self.opacity_selection_slider.sliderMoved["int"].connect(MainWindow.selection_slider_moved) - self.manual_registration_button.clicked.connect(MainWindow.manual_registration_button_clicked) - self.tableWidget.cellChanged["int", "int"].connect(MainWindow.table_cell_modified) - self.auto_registration_button.clicked.connect(MainWindow.auto_registration_button_clicked) - self.grid_display_checkBox.clicked.connect(MainWindow.grid_display_checkBox_clicked) - self.grid_size_slider.sliderMoved["int"].connect(MainWindow.grid_size_slider_moved) + self.opacity_selection_slider.sliderPressed.connect( + MainWindow.selection_slider_changed + ) + self.opacity_selection_slider.sliderMoved["int"].connect( + MainWindow.selection_slider_moved + ) + self.manual_registration_button.clicked.connect( + MainWindow.manual_registration_button_clicked + ) + self.tableWidget.cellChanged["int", "int"].connect( + MainWindow.table_cell_modified + ) + self.auto_registration_button.clicked.connect( + MainWindow.auto_registration_button_clicked + ) + self.grid_display_checkBox.clicked.connect( + MainWindow.grid_display_checkBox_clicked + ) + self.grid_size_slider.sliderMoved["int"].connect( + MainWindow.grid_size_slider_moved + ) self.grid_size_slider.sliderPressed.connect(MainWindow.grid_size_slider_pressed) - self.marker_registration_button.clicked.connect(MainWindow.markers_registration_button_clicked) - self.profiler_registration_button.clicked.connect(MainWindow.profiler_registration_button_clicked) + self.marker_registration_button.clicked.connect( + MainWindow.markers_registration_button_clicked + ) + self.profiler_registration_button.clicked.connect( + MainWindow.profiler_registration_button_clicked + ) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): @@ -308,7 +364,9 @@ def retranslateUi(self, MainWindow): self.auto_registration_button.setText(_translate("MainWindow", "Auto ...")) self.manual_registration_button.setText(_translate("MainWindow", "Manual ...")) self.marker_registration_button.setText(_translate("MainWindow", "Markers ...")) - self.profiler_registration_button.setText(_translate("MainWindow", "Profiler ...")) + self.profiler_registration_button.setText( + _translate("MainWindow", "Profiler ...") + ) self.groupBox.setTitle(_translate("MainWindow", "Grid")) self.grid_display_checkBox.setText(_translate("MainWindow", "Display")) self.label_5.setText(_translate("MainWindow", "Size")) @@ -335,7 +393,9 @@ def retranslateUi(self, MainWindow): self.export_button.setText(_translate("MainWindow", "Export ...")) self.actionExport_Profile.setText(_translate("MainWindow", "Profiles ...")) self.actionWater_Intake.setText(_translate("MainWindow", "Water Intake ...")) - self.actionImportedFilesMetadata.setText(_translate("MainWindow", "Imported Files and Metadata ...")) + self.actionImportedFilesMetadata.setText( + _translate("MainWindow", "Imported Files and Metadata ...") + ) self.actionBy_Time_Stamp.setText(_translate("MainWindow", "by Time Stamp")) self.actionBy_File_Name.setText(_translate("MainWindow", "by File Name")) self.actionDsc_files.setText(_translate("MainWindow", "dsc files ...")) diff --git a/notebooks/__code/ui_registration_auto_confirmation.py b/notebooks/__code/ui_registration_auto_confirmation.py index d3ad6d140..4fee04693 100755 --- a/notebooks/__code/ui_registration_auto_confirmation.py +++ b/notebooks/__code/ui_registration_auto_confirmation.py @@ -21,7 +21,9 @@ def setupUi(self, Dialog): self.label.setTextFormat(QtCore.Qt.AutoText) self.label.setObjectName("label") self.verticalLayout.addWidget(self.label) - spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout.addItem(spacerItem) self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") @@ -37,14 +39,18 @@ def setupUi(self, Dialog): self.textEdit.setObjectName("textEdit") self.horizontalLayout_2.addWidget(self.textEdit) self.verticalLayout.addLayout(self.horizontalLayout_2) - spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem1 = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout.addItem(spacerItem1) self.horizontalLayout = QtWidgets.QHBoxLayout() self.horizontalLayout.setObjectName("horizontalLayout") self.pushButton_2 = QtWidgets.QPushButton(Dialog) self.pushButton_2.setObjectName("pushButton_2") self.horizontalLayout.addWidget(self.pushButton_2) - spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem2 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem2) self.pushButton = QtWidgets.QPushButton(Dialog) self.pushButton.setObjectName("pushButton") @@ -59,7 +65,12 @@ def setupUi(self, Dialog): def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Auto. Registration")) - self.label.setText(_translate("Dialog", "Are you sure you want to automatically realign ALL the images!")) + self.label.setText( + _translate( + "Dialog", + "Are you sure you want to automatically realign ALL the images!", + ) + ) self.textEdit.setHtml( _translate( "Dialog", diff --git a/notebooks/__code/ui_registration_markers.py b/notebooks/__code/ui_registration_markers.py index 39593bbe3..9fef94ab2 100755 --- a/notebooks/__code/ui_registration_markers.py +++ b/notebooks/__code/ui_registration_markers.py @@ -24,7 +24,9 @@ def setupUi(self, Dialog): self.add_marker_button = QtWidgets.QPushButton(Dialog) self.add_marker_button.setObjectName("add_marker_button") self.verticalLayout.addWidget(self.add_marker_button) - spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout.addItem(spacerItem) self.remove_marker_button = QtWidgets.QPushButton(Dialog) self.remove_marker_button.setObjectName("remove_marker_button") @@ -45,7 +47,9 @@ def setupUi(self, Dialog): self.marker_color_widget.addItem("") self.marker_color_widget.addItem("") self.horizontalLayout_2.addWidget(self.marker_color_widget) - spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem1 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_2.addItem(spacerItem1) self.run_registration_button = QtWidgets.QPushButton(Dialog) self.run_registration_button.setObjectName("run_registration_button") @@ -56,9 +60,13 @@ def setupUi(self, Dialog): self.tabWidget.setCurrentIndex(-1) self.add_marker_button.clicked.connect(Dialog.add_marker_button_clicked) self.remove_marker_button.clicked.connect(Dialog.remove_marker_button_clicked) - self.marker_color_widget.currentIndexChanged["QString"].connect(Dialog.marker_color_changed) + self.marker_color_widget.currentIndexChanged["QString"].connect( + Dialog.marker_color_changed + ) self.tabWidget.currentChanged["int"].connect(Dialog.marker_tab_changed) - self.run_registration_button.clicked.connect(Dialog.run_registration_button_clicked) + self.run_registration_button.clicked.connect( + Dialog.run_registration_button_clicked + ) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): @@ -73,4 +81,6 @@ def retranslateUi(self, Dialog): self.marker_color_widget.setItemText(3, _translate("Dialog", "red")) self.marker_color_widget.setItemText(4, _translate("Dialog", "blue")) self.marker_color_widget.setItemText(5, _translate("Dialog", "black")) - self.run_registration_button.setText(_translate("Dialog", "Align Images Using Markers")) + self.run_registration_button.setText( + _translate("Dialog", "Align Images Using Markers") + ) diff --git a/notebooks/__code/ui_registration_profile.py b/notebooks/__code/ui_registration_profile.py index 19e6d6cd0..472faed98 100755 --- a/notebooks/__code/ui_registration_profile.py +++ b/notebooks/__code/ui_registration_profile.py @@ -30,27 +30,43 @@ def setupUi(self, MainWindow): self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.pyqtgraph_widget = QtWidgets.QWidget(self.layoutWidget) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) - sizePolicy.setHeightForWidth(self.pyqtgraph_widget.sizePolicy().hasHeightForWidth()) + sizePolicy.setHeightForWidth( + self.pyqtgraph_widget.sizePolicy().hasHeightForWidth() + ) self.pyqtgraph_widget.setSizePolicy(sizePolicy) self.pyqtgraph_widget.setObjectName("pyqtgraph_widget") self.horizontalLayout_2.addWidget(self.pyqtgraph_widget) - self.selection_reference_opacity_groupBox = QtWidgets.QGroupBox(self.layoutWidget) + self.selection_reference_opacity_groupBox = QtWidgets.QGroupBox( + self.layoutWidget + ) self.selection_reference_opacity_groupBox.setMinimumSize(QtCore.QSize(70, 0)) - self.selection_reference_opacity_groupBox.setMaximumSize(QtCore.QSize(60, 16777215)) - self.selection_reference_opacity_groupBox.setLayoutDirection(QtCore.Qt.LeftToRight) + self.selection_reference_opacity_groupBox.setMaximumSize( + QtCore.QSize(60, 16777215) + ) + self.selection_reference_opacity_groupBox.setLayoutDirection( + QtCore.Qt.LeftToRight + ) self.selection_reference_opacity_groupBox.setTitle("") self.selection_reference_opacity_groupBox.setAlignment(QtCore.Qt.AlignCenter) - self.selection_reference_opacity_groupBox.setObjectName("selection_reference_opacity_groupBox") - self.verticalLayout = QtWidgets.QVBoxLayout(self.selection_reference_opacity_groupBox) + self.selection_reference_opacity_groupBox.setObjectName( + "selection_reference_opacity_groupBox" + ) + self.verticalLayout = QtWidgets.QVBoxLayout( + self.selection_reference_opacity_groupBox + ) self.verticalLayout.setContentsMargins(-1, -1, 19, -1) self.verticalLayout.setObjectName("verticalLayout") self.label_2 = QtWidgets.QLabel(self.selection_reference_opacity_groupBox) self.label_2.setObjectName("label_2") self.verticalLayout.addWidget(self.label_2) - self.opacity_slider = QtWidgets.QSlider(self.selection_reference_opacity_groupBox) + self.opacity_slider = QtWidgets.QSlider( + self.selection_reference_opacity_groupBox + ) self.opacity_slider.setCursor(QtGui.QCursor(QtCore.Qt.SizeVerCursor)) self.opacity_slider.setAccessibleDescription("") self.opacity_slider.setLayoutDirection(QtCore.Qt.RightToLeft) @@ -110,7 +126,9 @@ def setupUi(self, MainWindow): self.calculate_markers_button.setObjectName("calculate_markers_button") self.verticalLayout_3.addWidget(self.calculate_markers_button) self.tableWidget = QtWidgets.QTableWidget(self.layoutWidget1) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.tableWidget.sizePolicy().hasHeightForWidth()) @@ -145,7 +163,9 @@ def setupUi(self, MainWindow): self.full_reset_button = QtWidgets.QPushButton(self.layoutWidget1) self.full_reset_button.setObjectName("full_reset_button") self.horizontalLayout.addWidget(self.full_reset_button) - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem) self.pushButton = QtWidgets.QPushButton(self.layoutWidget1) self.pushButton.setObjectName("pushButton") @@ -209,16 +229,24 @@ def setupUi(self, MainWindow): self.file_slider.sliderMoved["int"].connect(MainWindow.slider_file_changed) self.file_slider.valueChanged["int"].connect(MainWindow.slider_file_changed) self.next_image_button.clicked.connect(MainWindow.next_image_button_clicked) - self.previous_image_button.clicked.connect(MainWindow.previous_image_button_clicked) + self.previous_image_button.clicked.connect( + MainWindow.previous_image_button_clicked + ) self.opacity_slider.sliderMoved["int"].connect(MainWindow.opacity_slider_moved) self.export_button.clicked.connect(MainWindow.export_button_clicked) - self.calculate_markers_button.clicked.connect(MainWindow.calculate_markers_button_clicked) + self.calculate_markers_button.clicked.connect( + MainWindow.calculate_markers_button_clicked + ) self.tableWidget.itemSelectionChanged.connect(MainWindow.table_row_clicked) self.actionSettings_3.triggered.connect(MainWindow.settings_clicked) - self.registered_all_images_button.clicked.connect(MainWindow.registered_all_images_button_clicked) + self.registered_all_images_button.clicked.connect( + MainWindow.registered_all_images_button_clicked + ) self.full_reset_button.clicked.connect(MainWindow.full_reset) self.actionTutorial.triggered.connect(MainWindow.help_button_clicked) - self.pushButton.clicked.connect(MainWindow.registered_all_images_and_return_to_main_ui_button_clicked) + self.pushButton.clicked.connect( + MainWindow.registered_all_images_and_return_to_main_ui_button_clicked + ) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): @@ -231,7 +259,9 @@ def retranslateUi(self, MainWindow): self.label.setText(_translate("MainWindow", "Reference Image:")) self.reference_image_label.setText(_translate("MainWindow", "N/A")) self.pushButton_3.setText(_translate("MainWindow", "Change ...")) - self.calculate_markers_button.setText(_translate("MainWindow", "Calculate Peaks Position from Profiles")) + self.calculate_markers_button.setText( + _translate("MainWindow", "Calculate Peaks Position from Profiles") + ) item = self.tableWidget.horizontalHeaderItem(0) item.setText(_translate("MainWindow", "File Name")) item = self.tableWidget.horizontalHeaderItem(1) @@ -243,14 +273,22 @@ def retranslateUi(self, MainWindow): item = self.tableWidget.horizontalHeaderItem(4) item.setText(_translate("MainWindow", "Y offset")) self.full_reset_button.setText(_translate("MainWindow", "Full Reset")) - self.pushButton.setText(_translate("MainWindow", "Registered Images and Return to Main UI")) - self.registered_all_images_button.setText(_translate("MainWindow", "Register Images")) - self.export_button.setText(_translate("MainWindow", "Register Images and Export ...")) + self.pushButton.setText( + _translate("MainWindow", "Registered Images and Return to Main UI") + ) + self.registered_all_images_button.setText( + _translate("MainWindow", "Register Images") + ) + self.export_button.setText( + _translate("MainWindow", "Register Images and Export ...") + ) self.menuOptions.setTitle(_translate("MainWindow", "Options")) self.menuHelp.setTitle(_translate("MainWindow", "Help")) self.actionExport_Profile.setText(_translate("MainWindow", "Profiles ...")) self.actionWater_Intake.setText(_translate("MainWindow", "Water Intake ...")) - self.actionImportedFilesMetadata.setText(_translate("MainWindow", "Imported Files and Metadata ...")) + self.actionImportedFilesMetadata.setText( + _translate("MainWindow", "Imported Files and Metadata ...") + ) self.actionBy_Time_Stamp.setText(_translate("MainWindow", "by Time Stamp")) self.actionBy_File_Name.setText(_translate("MainWindow", "by File Name")) self.actionDsc_files.setText(_translate("MainWindow", "dsc files ...")) diff --git a/notebooks/__code/ui_registration_profile_settings.py b/notebooks/__code/ui_registration_profile_settings.py index 6043210a8..36831844c 100755 --- a/notebooks/__code/ui_registration_profile_settings.py +++ b/notebooks/__code/ui_registration_profile_settings.py @@ -35,7 +35,9 @@ def setupUi(self, MainWindow): self.verticalLayout.addLayout(self.horizontalLayout) self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_2.addItem(spacerItem) self.ok_button = QtWidgets.QPushButton(self.centralwidget) self.ok_button.setObjectName("ok_button") diff --git a/notebooks/__code/ui_registration_tool.py b/notebooks/__code/ui_registration_tool.py index 3c7e7878d..97ed6f4da 100755 --- a/notebooks/__code/ui_registration_tool.py +++ b/notebooks/__code/ui_registration_tool.py @@ -12,7 +12,9 @@ class Ui_MainWindow: def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(610, 532) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth()) @@ -29,7 +31,9 @@ def setupUi(self, MainWindow): self.verticalLayout_3.setObjectName("verticalLayout_3") self.horizontalLayout = QtWidgets.QHBoxLayout() self.horizontalLayout.setObjectName("horizontalLayout") - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem) self.up_button = QtWidgets.QPushButton(self.centralwidget) self.up_button.setMaximumSize(QtCore.QSize(100, 16777215)) @@ -37,21 +41,27 @@ def setupUi(self, MainWindow): self.up_button.setFlat(True) self.up_button.setObjectName("up_button") self.horizontalLayout.addWidget(self.up_button) - spacerItem1 = QtWidgets.QSpacerItem(20, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem1 = QtWidgets.QSpacerItem( + 20, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem1) self.verticalLayout_3.addLayout(self.horizontalLayout) self.horizontalLayout_3 = QtWidgets.QHBoxLayout() self.horizontalLayout_3.setObjectName("horizontalLayout_3") self.verticalLayout = QtWidgets.QVBoxLayout() self.verticalLayout.setObjectName("verticalLayout") - spacerItem2 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem2 = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout.addItem(spacerItem2) self.left_button = QtWidgets.QPushButton(self.centralwidget) self.left_button.setText("") self.left_button.setFlat(True) self.left_button.setObjectName("left_button") self.verticalLayout.addWidget(self.left_button) - spacerItem3 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem3 = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout.addItem(spacerItem3) self.horizontalLayout_3.addLayout(self.verticalLayout) self.rotate_right_button = QtWidgets.QPushButton(self.centralwidget) @@ -76,20 +86,26 @@ def setupUi(self, MainWindow): self.horizontalLayout_3.addWidget(self.rotate_left_button) self.verticalLayout_2 = QtWidgets.QVBoxLayout() self.verticalLayout_2.setObjectName("verticalLayout_2") - spacerItem4 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem4 = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout_2.addItem(spacerItem4) self.right_button = QtWidgets.QPushButton(self.centralwidget) self.right_button.setText("") self.right_button.setFlat(True) self.right_button.setObjectName("right_button") self.verticalLayout_2.addWidget(self.right_button) - spacerItem5 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem5 = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout_2.addItem(spacerItem5) self.horizontalLayout_3.addLayout(self.verticalLayout_2) self.verticalLayout_3.addLayout(self.horizontalLayout_3) self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") - spacerItem6 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem6 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_2.addItem(spacerItem6) self.down_button = QtWidgets.QPushButton(self.centralwidget) self.down_button.setMaximumSize(QtCore.QSize(100, 16777215)) @@ -97,7 +113,9 @@ def setupUi(self, MainWindow): self.down_button.setFlat(True) self.down_button.setObjectName("down_button") self.horizontalLayout_2.addWidget(self.down_button) - spacerItem7 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem7 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_2.addItem(spacerItem7) self.verticalLayout_3.addLayout(self.horizontalLayout_2) self.verticalLayout_4.addLayout(self.verticalLayout_3) @@ -117,8 +135,12 @@ def setupUi(self, MainWindow): self.left_button.clicked.connect(MainWindow.left_button_clicked) self.rotate_left_button.clicked.connect(MainWindow.rotate_left_button_clicked) self.rotate_right_button.clicked.connect(MainWindow.rotate_right_button_clicked) - self.small_rotate_right_button.clicked.connect(MainWindow.small_rotate_right_button_clicked) - self.small_rotate_left_button.clicked.connect(MainWindow.small_rotate_left_button_clicked) + self.small_rotate_right_button.clicked.connect( + MainWindow.small_rotate_right_button_clicked + ) + self.small_rotate_left_button.clicked.connect( + MainWindow.small_rotate_left_button_clicked + ) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): diff --git a/notebooks/__code/ui_resonance_imaging_experiment_vs_theory.py b/notebooks/__code/ui_resonance_imaging_experiment_vs_theory.py index 359346bc4..8e4478d8a 100755 --- a/notebooks/__code/ui_resonance_imaging_experiment_vs_theory.py +++ b/notebooks/__code/ui_resonance_imaging_experiment_vs_theory.py @@ -20,7 +20,9 @@ def setupUi(self, MainWindow): self.splitter.setOrientation(QtCore.Qt.Vertical) self.splitter.setObjectName("splitter") self.widget = QtWidgets.QWidget(self.splitter) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Maximum) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Maximum + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth()) @@ -54,7 +56,9 @@ def setupUi(self, MainWindow): self.energy_radio_button.setObjectName("energy_radio_button") self.horizontalLayout_8.addWidget(self.energy_radio_button) self.verticalLayout_4.addWidget(self.groupBox) - spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout_4.addItem(spacerItem) self.horizontalLayout_4.addLayout(self.verticalLayout_4) self.verticalLayout = QtWidgets.QVBoxLayout() @@ -64,17 +68,25 @@ def setupUi(self, MainWindow): self.label_4.setObjectName("label_4") self.verticalLayout.addWidget(self.label_4) self.list_to_plot_widget = QtWidgets.QListWidget(self.widget1) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) - sizePolicy.setHeightForWidth(self.list_to_plot_widget.sizePolicy().hasHeightForWidth()) + sizePolicy.setHeightForWidth( + self.list_to_plot_widget.sizePolicy().hasHeightForWidth() + ) self.list_to_plot_widget.setSizePolicy(sizePolicy) self.list_to_plot_widget.setMinimumSize(QtCore.QSize(200, 100)) self.list_to_plot_widget.setMaximumSize(QtCore.QSize(300, 300)) - self.list_to_plot_widget.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection) + self.list_to_plot_widget.setSelectionMode( + QtWidgets.QAbstractItemView.MultiSelection + ) self.list_to_plot_widget.setObjectName("list_to_plot_widget") self.verticalLayout.addWidget(self.list_to_plot_widget) - spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem1 = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout.addItem(spacerItem1) self.horizontalLayout_4.addLayout(self.verticalLayout) self.verticalLayout_3 = QtWidgets.QVBoxLayout() @@ -87,12 +99,16 @@ def setupUi(self, MainWindow): self.distance_source_detector_value = QtWidgets.QLineEdit(self.widget1) self.distance_source_detector_value.setMinimumSize(QtCore.QSize(80, 0)) self.distance_source_detector_value.setMaximumSize(QtCore.QSize(80, 16777215)) - self.distance_source_detector_value.setObjectName("distance_source_detector_value") + self.distance_source_detector_value.setObjectName( + "distance_source_detector_value" + ) self.horizontalLayout.addWidget(self.distance_source_detector_value) self.label_2 = QtWidgets.QLabel(self.widget1) self.label_2.setObjectName("label_2") self.horizontalLayout.addWidget(self.label_2) - spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem2 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem2) self.verticalLayout_3.addLayout(self.horizontalLayout) self.horizontalLayout_2 = QtWidgets.QHBoxLayout() @@ -108,7 +124,9 @@ def setupUi(self, MainWindow): self.detector_offset_units = QtWidgets.QLabel(self.widget1) self.detector_offset_units.setObjectName("detector_offset_units") self.horizontalLayout_2.addWidget(self.detector_offset_units) - spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem3 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_2.addItem(spacerItem3) self.verticalLayout_3.addLayout(self.horizontalLayout_2) self.horizontalLayout_3 = QtWidgets.QHBoxLayout() @@ -123,10 +141,14 @@ def setupUi(self, MainWindow): self.time_spectra_file_browse_button = QtWidgets.QPushButton(self.widget1) self.time_spectra_file_browse_button.setMinimumSize(QtCore.QSize(100, 0)) self.time_spectra_file_browse_button.setMaximumSize(QtCore.QSize(100, 16777215)) - self.time_spectra_file_browse_button.setObjectName("time_spectra_file_browse_button") + self.time_spectra_file_browse_button.setObjectName( + "time_spectra_file_browse_button" + ) self.horizontalLayout_3.addWidget(self.time_spectra_file_browse_button) self.verticalLayout_3.addLayout(self.horizontalLayout_3) - spacerItem4 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem4 = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout_3.addItem(spacerItem4) self.verticalLayout_2 = QtWidgets.QVBoxLayout() self.verticalLayout_2.setObjectName("verticalLayout_2") @@ -147,13 +169,21 @@ def setupUi(self, MainWindow): self.retranslateUi(MainWindow) self.done_button.clicked.connect(MainWindow.done_button_clicked) - self.distance_source_detector_value.editingFinished.connect(MainWindow.distance_source_detector_validated) - self.detector_offset_value.returnPressed.connect(MainWindow.detector_offset_validated) - self.time_spectra_file_browse_button.clicked.connect(MainWindow.time_spectra_file_browse_button_clicked) + self.distance_source_detector_value.editingFinished.connect( + MainWindow.distance_source_detector_validated + ) + self.detector_offset_value.returnPressed.connect( + MainWindow.detector_offset_validated + ) + self.time_spectra_file_browse_button.clicked.connect( + MainWindow.time_spectra_file_browse_button_clicked + ) self.file_index_ratio_button.clicked.connect(MainWindow.radio_button_clicked) self.tof_radio_button.clicked.connect(MainWindow.radio_button_clicked) self.lambda_radio_button.clicked.connect(MainWindow.radio_button_clicked) - self.list_to_plot_widget.itemClicked["QListWidgetItem*"].connect(MainWindow.plot_selection_changed) + self.list_to_plot_widget.itemClicked["QListWidgetItem*"].connect( + MainWindow.plot_selection_changed + ) self.energy_radio_button.clicked.connect(MainWindow.radio_button_clicked) QtCore.QMetaObject.connectSlotsByName(MainWindow) @@ -172,5 +202,7 @@ def retranslateUi(self, MainWindow): self.detector_offset_units.setText(_translate("MainWindow", "us")) self.label_5.setText(_translate("MainWindow", "Time Spectra File:")) self.time_spectra_file.setText(_translate("MainWindow", "N/A")) - self.time_spectra_file_browse_button.setText(_translate("MainWindow", "Browse ...")) + self.time_spectra_file_browse_button.setText( + _translate("MainWindow", "Browse ...") + ) self.done_button.setText(_translate("MainWindow", "DONE")) diff --git a/notebooks/__code/ui_resonance_imaging_layers_input.py b/notebooks/__code/ui_resonance_imaging_layers_input.py index a9d86a67d..000b18c00 100755 --- a/notebooks/__code/ui_resonance_imaging_layers_input.py +++ b/notebooks/__code/ui_resonance_imaging_layers_input.py @@ -107,7 +107,9 @@ def setupUi(self, MainWindow): self.label_8 = QtWidgets.QLabel(self.layer_groupBox) self.label_8.setObjectName("label_8") self.horizontalLayout_9.addWidget(self.label_8) - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_9.addItem(spacerItem) self.label_9 = QtWidgets.QLabel(self.layer_groupBox) self.label_9.setObjectName("label_9") @@ -120,7 +122,9 @@ def setupUi(self, MainWindow): self.label_10 = QtWidgets.QLabel(self.layer_groupBox) self.label_10.setObjectName("label_10") self.horizontalLayout_9.addWidget(self.label_10) - spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem1 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_9.addItem(spacerItem1) self.deltaE_label = QtWidgets.QLabel(self.layer_groupBox) self.deltaE_label.setObjectName("deltaE_label") @@ -134,7 +138,9 @@ def setupUi(self, MainWindow): self.label_16.setObjectName("label_16") self.horizontalLayout_9.addWidget(self.label_16) self.horizontalLayout.addLayout(self.horizontalLayout_9) - spacerItem2 = QtWidgets.QSpacerItem(17, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem2 = QtWidgets.QSpacerItem( + 17, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem2) self.validate_table_iinputs = QtWidgets.QPushButton(self.layer_groupBox) self.validate_table_iinputs.setObjectName("validate_table_iinputs") @@ -156,7 +162,9 @@ def setupUi(self, MainWindow): self.layer_name_combobox = QtWidgets.QComboBox(self.check_groupBox) self.layer_name_combobox.setObjectName("layer_name_combobox") self.horizontalLayout_4.addWidget(self.layer_name_combobox) - spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem3 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_4.addItem(spacerItem3) self.verticalLayout_2.addLayout(self.horizontalLayout_4) self.horizontalLayout_2 = QtWidgets.QHBoxLayout() @@ -185,7 +193,9 @@ def setupUi(self, MainWindow): self.verticalLayout_2.addLayout(self.horizontalLayout_2) self.horizontalLayout_5 = QtWidgets.QHBoxLayout() self.horizontalLayout_5.setObjectName("horizontalLayout_5") - spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem4 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_5.addItem(spacerItem4) self.label_11 = QtWidgets.QLabel(self.check_groupBox) self.label_11.setObjectName("label_11") @@ -193,7 +203,9 @@ def setupUi(self, MainWindow): self.element_name_combobox = QtWidgets.QComboBox(self.check_groupBox) self.element_name_combobox.setObjectName("element_name_combobox") self.horizontalLayout_5.addWidget(self.element_name_combobox) - spacerItem5 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem5 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_5.addItem(spacerItem5) self.verticalLayout_2.addLayout(self.horizontalLayout_5) self.gridLayout_2 = QtWidgets.QGridLayout() @@ -247,7 +259,9 @@ def setupUi(self, MainWindow): self.verticalLayout_3.addWidget(self.element_table) self.horizontalLayout_8 = QtWidgets.QHBoxLayout() self.horizontalLayout_8.setObjectName("horizontalLayout_8") - spacerItem6 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem6 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_8.addItem(spacerItem6) self.label_3 = QtWidgets.QLabel(self.check_groupBox) self.label_3.setObjectName("label_3") @@ -255,16 +269,22 @@ def setupUi(self, MainWindow): self.total_iso_ratio = QtWidgets.QLabel(self.check_groupBox) self.total_iso_ratio.setObjectName("total_iso_ratio") self.horizontalLayout_8.addWidget(self.total_iso_ratio) - spacerItem7 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem7 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_8.addItem(spacerItem7) self.verticalLayout_3.addLayout(self.horizontalLayout_8) self.horizontalLayout_6.addLayout(self.verticalLayout_3) self.verticalLayout_4.addWidget(self.check_groupBox) - spacerItem8 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem8 = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout_4.addItem(spacerItem8) self.horizontalLayout_7 = QtWidgets.QHBoxLayout() self.horizontalLayout_7.setObjectName("horizontalLayout_7") - spacerItem9 = QtWidgets.QSpacerItem(17, 17, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem9 = QtWidgets.QSpacerItem( + 17, 17, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_7.addItem(spacerItem9) self.ok_button = QtWidgets.QPushButton(self.centralwidget) self.ok_button.setEnabled(False) @@ -282,10 +302,18 @@ def setupUi(self, MainWindow): MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) - self.validate_table_iinputs.clicked.connect(MainWindow.validate_table_input_clicked) - self.layer_name_combobox.currentIndexChanged["QString"].connect(MainWindow.layer_combobox_clicked) - self.element_name_combobox.currentIndexChanged["QString"].connect(MainWindow.element_combobox_clicked) - self.element_table.cellChanged["int", "int"].connect(MainWindow.element_table_edited) + self.validate_table_iinputs.clicked.connect( + MainWindow.validate_table_input_clicked + ) + self.layer_name_combobox.currentIndexChanged["QString"].connect( + MainWindow.layer_combobox_clicked + ) + self.element_name_combobox.currentIndexChanged["QString"].connect( + MainWindow.element_combobox_clicked + ) + self.element_table.cellChanged["int", "int"].connect( + MainWindow.element_table_edited + ) self.ok_button.clicked.connect(MainWindow.ok_button_clicked) QtCore.QMetaObject.connectSlotsByName(MainWindow) @@ -355,7 +383,9 @@ def retranslateUi(self, MainWindow): self.deltaE_label.setText(_translate("MainWindow", "DE")) self.deltaE_lineEdit.setText(_translate("MainWindow", "0.01")) self.label_16.setText(_translate("MainWindow", "eV")) - self.validate_table_iinputs.setText(_translate("MainWindow", "Validate Table Inputs")) + self.validate_table_iinputs.setText( + _translate("MainWindow", "Validate Table Inputs") + ) self.check_groupBox.setTitle(_translate("MainWindow", "Check")) self.label.setText(_translate("MainWindow", "Layer's Name")) self.label_2.setText(_translate("MainWindow", "Thickness")) diff --git a/notebooks/__code/ui_roi_selection.py b/notebooks/__code/ui_roi_selection.py index a95c33133..d01524d93 100755 --- a/notebooks/__code/ui_roi_selection.py +++ b/notebooks/__code/ui_roi_selection.py @@ -20,7 +20,9 @@ def setupUi(self, MainWindow): self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.widget = QtWidgets.QWidget(self.centralwidget) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth()) @@ -32,7 +34,9 @@ def setupUi(self, MainWindow): self.verticalLayout = QtWidgets.QVBoxLayout() self.verticalLayout.setObjectName("verticalLayout") self.table_roi = QtWidgets.QTableWidget(self.centralwidget) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.table_roi.sizePolicy().hasHeightForWidth()) @@ -75,13 +79,17 @@ def setupUi(self, MainWindow): self.add_roi_button.setObjectName("add_roi_button") self.horizontalLayout_3.addWidget(self.add_roi_button) self.verticalLayout.addLayout(self.horizontalLayout_3) - spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout.addItem(spacerItem) self.instruction = QtWidgets.QLabel(self.centralwidget) self.instruction.setText("") self.instruction.setObjectName("instruction") self.verticalLayout.addWidget(self.instruction) - spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem1 = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout.addItem(spacerItem1) self.horizontalLayout_4.addLayout(self.verticalLayout) self.verticalLayout_2.addLayout(self.horizontalLayout_4) @@ -90,7 +98,9 @@ def setupUi(self, MainWindow): self.cancel_button = QtWidgets.QPushButton(self.centralwidget) self.cancel_button.setObjectName("cancel_button") self.horizontalLayout.addWidget(self.cancel_button) - spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem2 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem2) self.apply_button = QtWidgets.QPushButton(self.centralwidget) self.apply_button.setObjectName("apply_button") @@ -110,7 +120,9 @@ def setupUi(self, MainWindow): self.apply_button.clicked.connect(MainWindow.apply_clicked) self.remove_roi_button.clicked.connect(MainWindow.remove_roi_button_clicked) self.add_roi_button.clicked.connect(MainWindow.add_roi_button_clicked) - self.table_roi.itemChanged["QTableWidgetItem*"].connect(MainWindow.update_table_roi) + self.table_roi.itemChanged["QTableWidgetItem*"].connect( + MainWindow.update_table_roi + ) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): diff --git a/notebooks/__code/ui_rotate_and_crop.py b/notebooks/__code/ui_rotate_and_crop.py index f8954a611..3853c2079 100755 --- a/notebooks/__code/ui_rotate_and_crop.py +++ b/notebooks/__code/ui_rotate_and_crop.py @@ -18,7 +18,9 @@ def setupUi(self, MainWindow): self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.centralwidget) self.verticalLayout_2.setObjectName("verticalLayout_2") self.widget = QtWidgets.QWidget(self.centralwidget) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth()) @@ -26,7 +28,9 @@ def setupUi(self, MainWindow): self.widget.setObjectName("widget") self.verticalLayout_2.addWidget(self.widget) self.groupBox = QtWidgets.QGroupBox(self.centralwidget) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.groupBox.sizePolicy().hasHeightForWidth()) @@ -38,7 +42,9 @@ def setupUi(self, MainWindow): self.verticalLayout.setObjectName("verticalLayout") self.textBrowser = QtWidgets.QTextBrowser(self.groupBox) self.textBrowser.setEnabled(False) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.textBrowser.sizePolicy().hasHeightForWidth()) @@ -53,7 +59,9 @@ def setupUi(self, MainWindow): self.cancel_button = QtWidgets.QPushButton(self.centralwidget) self.cancel_button.setObjectName("cancel_button") self.horizontalLayout.addWidget(self.cancel_button) - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem) self.apply_button = QtWidgets.QPushButton(self.centralwidget) self.apply_button.setObjectName("apply_button") diff --git a/notebooks/__code/ui_scale_overlapping_images.py b/notebooks/__code/ui_scale_overlapping_images.py index 39582ba84..a3d01a95b 100755 --- a/notebooks/__code/ui_scale_overlapping_images.py +++ b/notebooks/__code/ui_scale_overlapping_images.py @@ -48,10 +48,14 @@ def setupUi(self, MainWindow): self.horizontalLayout_6.addWidget(self.display_size_label) self.grid_size_slider = QtWidgets.QSlider(self.groupBox) self.grid_size_slider.setEnabled(False) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) - sizePolicy.setHeightForWidth(self.grid_size_slider.sizePolicy().hasHeightForWidth()) + sizePolicy.setHeightForWidth( + self.grid_size_slider.sizePolicy().hasHeightForWidth() + ) self.grid_size_slider.setSizePolicy(sizePolicy) self.grid_size_slider.setMaximumSize(QtCore.QSize(16777215, 16777215)) self.grid_size_slider.setMinimum(1) @@ -114,7 +118,9 @@ def setupUi(self, MainWindow): self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setObjectName("pushButton") self.horizontalLayout.addWidget(self.pushButton) - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem) self.horizontalLayout_4 = QtWidgets.QHBoxLayout() self.horizontalLayout_4.setObjectName("horizontalLayout_4") @@ -130,7 +136,9 @@ def setupUi(self, MainWindow): self.comboBox.addItem("") self.horizontalLayout_4.addWidget(self.comboBox) self.horizontalLayout.addLayout(self.horizontalLayout_4) - spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem1 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem1) self.export_button = QtWidgets.QPushButton(self.centralwidget) self.export_button.setObjectName("export_button") @@ -168,12 +176,22 @@ def setupUi(self, MainWindow): self.pushButton.clicked.connect(MainWindow.help_button_clicked) self.grid_display_checkBox.clicked.connect(MainWindow.display_grid_clicked) self.grid_size_slider.sliderPressed.connect(MainWindow.grid_size_slider_clicked) - self.grid_size_slider.sliderMoved["int"].connect(MainWindow.grid_size_slider_moved) - self.transparency_slider.sliderPressed.connect(MainWindow.transparency_slider_clicked) - self.transparency_slider.sliderMoved["int"].connect(MainWindow.transparency_slider_moved) - self.grid_size_slider.sliderReleased.connect(MainWindow.grid_size_slider_released) + self.grid_size_slider.sliderMoved["int"].connect( + MainWindow.grid_size_slider_moved + ) + self.transparency_slider.sliderPressed.connect( + MainWindow.transparency_slider_clicked + ) + self.transparency_slider.sliderMoved["int"].connect( + MainWindow.transparency_slider_moved + ) + self.grid_size_slider.sliderReleased.connect( + MainWindow.grid_size_slider_released + ) self.file_slider.sliderMoved["int"].connect(MainWindow.slider_file_changed) - self.previous_image_button.clicked.connect(MainWindow.previous_image_button_clicked) + self.previous_image_button.clicked.connect( + MainWindow.previous_image_button_clicked + ) self.file_slider.valueChanged["int"].connect(MainWindow.slider_file_changed) self.next_image_button.clicked.connect(MainWindow.next_image_button_clicked) QtCore.QMetaObject.connectSlotsByName(MainWindow) @@ -184,7 +202,9 @@ def retranslateUi(self, MainWindow): self.groupBox.setTitle(_translate("MainWindow", "Grid")) self.grid_display_checkBox.setText(_translate("MainWindow", "Display")) self.display_size_label.setText(_translate("MainWindow", "Size")) - self.display_transparency_label.setText(_translate("MainWindow", "Transparency")) + self.display_transparency_label.setText( + _translate("MainWindow", "Transparency") + ) self.previous_image_button.setText(_translate("MainWindow", "Prev. Image")) self.image_slider_value.setText(_translate("MainWindow", "0")) self.next_image_button.setText(_translate("MainWindow", "Next Image")) @@ -195,7 +215,9 @@ def retranslateUi(self, MainWindow): self.export_button.setText(_translate("MainWindow", "Export Images ...")) self.actionExport_Profile.setText(_translate("MainWindow", "Profiles ...")) self.actionWater_Intake.setText(_translate("MainWindow", "Water Intake ...")) - self.actionImportedFilesMetadata.setText(_translate("MainWindow", "Imported Files and Metadata ...")) + self.actionImportedFilesMetadata.setText( + _translate("MainWindow", "Imported Files and Metadata ...") + ) self.actionBy_Time_Stamp.setText(_translate("MainWindow", "by Time Stamp")) self.actionBy_File_Name.setText(_translate("MainWindow", "by File Name")) self.actionDsc_files.setText(_translate("MainWindow", "dsc files ...")) diff --git a/notebooks/__code/ui_stitching_images.py b/notebooks/__code/ui_stitching_images.py index 0761b78a6..f8623e068 100755 --- a/notebooks/__code/ui_stitching_images.py +++ b/notebooks/__code/ui_stitching_images.py @@ -82,7 +82,9 @@ def setupUi(self, MainWindow): self.cancel_button = QtWidgets.QPushButton(self.centralwidget) self.cancel_button.setObjectName("cancel_button") self.horizontalLayout.addWidget(self.cancel_button) - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem) self.apply_button = QtWidgets.QPushButton(self.centralwidget) self.apply_button.setObjectName("apply_button") diff --git a/notebooks/__code/ui_template.py b/notebooks/__code/ui_template.py index 930a9d1c8..4e8cb591e 100755 --- a/notebooks/__code/ui_template.py +++ b/notebooks/__code/ui_template.py @@ -18,7 +18,9 @@ def setupUi(self, MainWindow): self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.widget = QtWidgets.QWidget(self.centralwidget) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth()) @@ -45,7 +47,9 @@ def setupUi(self, MainWindow): self.cancel_button = QtWidgets.QPushButton(self.centralwidget) self.cancel_button.setObjectName("cancel_button") self.horizontalLayout.addWidget(self.cancel_button) - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem) self.apply_button = QtWidgets.QPushButton(self.centralwidget) self.apply_button.setObjectName("apply_button") diff --git a/notebooks/__code/ui_water_intake_profile.py b/notebooks/__code/ui_water_intake_profile.py index 305a36601..a5b43b29c 100755 --- a/notebooks/__code/ui_water_intake_profile.py +++ b/notebooks/__code/ui_water_intake_profile.py @@ -27,7 +27,9 @@ def setupUi(self, MainWindow): self.splitter.setHandleWidth(22) self.splitter.setObjectName("splitter") self.widget = QtWidgets.QWidget(self.splitter) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth()) @@ -42,7 +44,9 @@ def setupUi(self, MainWindow): self.verticalLayout_4 = QtWidgets.QVBoxLayout() self.verticalLayout_4.setObjectName("verticalLayout_4") self.tableWidget = QtWidgets.QTableWidget(self.layoutWidget) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.tableWidget.sizePolicy().hasHeightForWidth()) @@ -60,7 +64,9 @@ def setupUi(self, MainWindow): self.verticalLayout_4.addWidget(self.tableWidget) self.horizontalLayout_3 = QtWidgets.QHBoxLayout() self.horizontalLayout_3.setObjectName("horizontalLayout_3") - spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_3.addItem(spacerItem) self.export_table_button = QtWidgets.QPushButton(self.layoutWidget) self.export_table_button.setObjectName("export_table_button") @@ -72,7 +78,9 @@ def setupUi(self, MainWindow): self.verticalLayout_3.setSizeConstraint(QtWidgets.QLayout.SetMinimumSize) self.verticalLayout_3.setObjectName("verticalLayout_3") self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum + ) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.groupBox_2.sizePolicy().hasHeightForWidth()) @@ -102,11 +110,15 @@ def setupUi(self, MainWindow): self.x_axis_integration_radioButton = QtWidgets.QRadioButton(self.groupBox_4) self.x_axis_integration_radioButton.setEnabled(True) self.x_axis_integration_radioButton.setChecked(True) - self.x_axis_integration_radioButton.setObjectName("x_axis_integration_radioButton") + self.x_axis_integration_radioButton.setObjectName( + "x_axis_integration_radioButton" + ) self.verticalLayout_5.addWidget(self.x_axis_integration_radioButton) self.y_axis_integration_radioButton = QtWidgets.QRadioButton(self.groupBox_4) self.y_axis_integration_radioButton.setEnabled(True) - self.y_axis_integration_radioButton.setObjectName("y_axis_integration_radioButton") + self.y_axis_integration_radioButton.setObjectName( + "y_axis_integration_radioButton" + ) self.verticalLayout_5.addWidget(self.y_axis_integration_radioButton) self.verticalLayout_3.addWidget(self.groupBox_4) self.groupBox_6 = QtWidgets.QGroupBox(self.centralwidget) @@ -116,7 +128,9 @@ def setupUi(self, MainWindow): self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.groupBox_6) self.horizontalLayout_4.setObjectName("horizontalLayout_4") self.label = QtWidgets.QLabel(self.groupBox_6) - sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred) + sizePolicy = QtWidgets.QSizePolicy( + QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred + ) sizePolicy.setHorizontalStretch(2) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth()) @@ -148,7 +162,9 @@ def setupUi(self, MainWindow): self.change_point_checkBox.setObjectName("change_point_checkBox") self.verticalLayout_6.addWidget(self.change_point_checkBox) self.verticalLayout_3.addWidget(self.groupBox_5) - spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + spacerItem1 = QtWidgets.QSpacerItem( + 20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding + ) self.verticalLayout_3.addItem(spacerItem1) self.horizontalLayout_2.addLayout(self.verticalLayout_3) self.verticalLayout_7.addLayout(self.horizontalLayout_2) @@ -160,10 +176,14 @@ def setupUi(self, MainWindow): self.horizontalLayout_7.setObjectName("horizontalLayout_7") self.sort_files_by_time_radioButton = QtWidgets.QRadioButton(self.groupBox_3) self.sort_files_by_time_radioButton.setChecked(True) - self.sort_files_by_time_radioButton.setObjectName("sort_files_by_time_radioButton") + self.sort_files_by_time_radioButton.setObjectName( + "sort_files_by_time_radioButton" + ) self.horizontalLayout_7.addWidget(self.sort_files_by_time_radioButton) self.sort_files_by_name_radioButton = QtWidgets.QRadioButton(self.groupBox_3) - self.sort_files_by_name_radioButton.setObjectName("sort_files_by_name_radioButton") + self.sort_files_by_name_radioButton.setObjectName( + "sort_files_by_name_radioButton" + ) self.horizontalLayout_7.addWidget(self.sort_files_by_name_radioButton) self.time_between_runs_label = QtWidgets.QLabel(self.groupBox_3) self.time_between_runs_label.setEnabled(False) @@ -184,7 +204,9 @@ def setupUi(self, MainWindow): self.time_between_runs_units_label.setEnabled(False) self.time_between_runs_units_label.setMinimumSize(QtCore.QSize(20, 0)) self.time_between_runs_units_label.setMaximumSize(QtCore.QSize(20, 16777215)) - self.time_between_runs_units_label.setObjectName("time_between_runs_units_label") + self.time_between_runs_units_label.setObjectName( + "time_between_runs_units_label" + ) self.horizontalLayout_7.addWidget(self.time_between_runs_units_label) self.horizontalLayout_6.addWidget(self.groupBox_3) self.groupBox = QtWidgets.QGroupBox(self.centralwidget) @@ -220,7 +242,9 @@ def setupUi(self, MainWindow): self.ignore_first_image_checkbox.setChecked(True) self.ignore_first_image_checkbox.setObjectName("ignore_first_image_checkbox") self.horizontalLayout_6.addWidget(self.ignore_first_image_checkbox) - spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem2 = QtWidgets.QSpacerItem( + 40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout_6.addItem(spacerItem2) self.verticalLayout_7.addLayout(self.horizontalLayout_6) self.horizontalLayout = QtWidgets.QHBoxLayout() @@ -230,7 +254,9 @@ def setupUi(self, MainWindow): self.help_button.setMaximumSize(QtCore.QSize(100, 30)) self.help_button.setObjectName("help_button") self.horizontalLayout.addWidget(self.help_button) - spacerItem3 = QtWidgets.QSpacerItem(408, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + spacerItem3 = QtWidgets.QSpacerItem( + 408, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum + ) self.horizontalLayout.addItem(spacerItem3) self.ok_button = QtWidgets.QPushButton(self.centralwidget) self.ok_button.setMinimumSize(QtCore.QSize(100, 30)) @@ -281,24 +307,46 @@ def setupUi(self, MainWindow): self.ok_button.clicked.connect(MainWindow.ok_button_clicked) # type: ignore self.help_button.clicked.connect(MainWindow.help_button_clicked) # type: ignore self.actionExport_Profile.triggered.connect(MainWindow.export_profile_clicked) # type: ignore - self.actionWater_Intake.triggered.connect(MainWindow.export_water_intake_clicked) # type: ignore + self.actionWater_Intake.triggered.connect( + MainWindow.export_water_intake_clicked + ) # type: ignore self.add_radioButton.clicked.connect(MainWindow.profile_algo_changed) # type: ignore self.mean_radioButton.clicked.connect(MainWindow.profile_algo_changed) # type: ignore self.median_radioButton.clicked.connect(MainWindow.profile_algo_changed) # type: ignore - self.sort_files_by_time_radioButton.clicked.connect(MainWindow.sorting_files_checkbox_clicked) # type: ignore - self.sort_files_by_name_radioButton.clicked.connect(MainWindow.sorting_files_checkbox_clicked) # type: ignore - self.time_between_runs_spinBox.editingFinished.connect(MainWindow.time_between_runs_spinBox_changed) # type: ignore - self.pixel_radioButton.clicked.connect(MainWindow._water_intake_yaxis_checkbox_changed) # type: ignore - self.distance_radioButton.clicked.connect(MainWindow._water_intake_yaxis_checkbox_changed) # type: ignore - self.pixel_size_spinBox.editingFinished.connect(MainWindow._pixel_size_spinBox_changed) # type: ignore + self.sort_files_by_time_radioButton.clicked.connect( + MainWindow.sorting_files_checkbox_clicked + ) # type: ignore + self.sort_files_by_name_radioButton.clicked.connect( + MainWindow.sorting_files_checkbox_clicked + ) # type: ignore + self.time_between_runs_spinBox.editingFinished.connect( + MainWindow.time_between_runs_spinBox_changed + ) # type: ignore + self.pixel_radioButton.clicked.connect( + MainWindow._water_intake_yaxis_checkbox_changed + ) # type: ignore + self.distance_radioButton.clicked.connect( + MainWindow._water_intake_yaxis_checkbox_changed + ) # type: ignore + self.pixel_size_spinBox.editingFinished.connect( + MainWindow._pixel_size_spinBox_changed + ) # type: ignore self.actionDsc_files.triggered.connect(MainWindow.import_dsc_clicked) # type: ignore self.export_table_button.pressed.connect(MainWindow.export_table_button_clicked) # type: ignore self.actionProfiles.triggered.connect(MainWindow.export_profile_clicked) # type: ignore - self.actionWater_Intake_2.triggered.connect(MainWindow.export_water_intake_clicked) # type: ignore + self.actionWater_Intake_2.triggered.connect( + MainWindow.export_water_intake_clicked + ) # type: ignore self.actionDsc.triggered.connect(MainWindow.import_dsc_clicked) # type: ignore - self.x_axis_integration_radioButton.clicked.connect(MainWindow.integration_direction_changed) # type: ignore - self.y_axis_integration_radioButton.clicked.connect(MainWindow.integration_direction_changed) # type: ignore - self.ignore_first_image_checkbox.clicked.connect(MainWindow.ignore_first_image_checkbox_clicked) # type: ignore + self.x_axis_integration_radioButton.clicked.connect( + MainWindow.integration_direction_changed + ) # type: ignore + self.y_axis_integration_radioButton.clicked.connect( + MainWindow.integration_direction_changed + ) # type: ignore + self.ignore_first_image_checkbox.clicked.connect( + MainWindow.ignore_first_image_checkbox_clicked + ) # type: ignore self.sliding_average_checkBox.clicked.connect(MainWindow.algorithm_changed) # type: ignore self.error_function_checkBox.clicked.connect(MainWindow.algorithm_changed) # type: ignore self.rebin_spinBox.valueChanged["int"].connect(MainWindow.rebin_slider_changed) # type: ignore @@ -320,25 +368,39 @@ def retranslateUi(self, MainWindow): self.mean_radioButton.setText(_translate("MainWindow", "Mean")) self.median_radioButton.setText(_translate("MainWindow", "Median")) self.groupBox_4.setTitle(_translate("MainWindow", "Integration Direction")) - self.x_axis_integration_radioButton.setText(_translate("MainWindow", "along x-axis")) - self.y_axis_integration_radioButton.setText(_translate("MainWindow", "along y-axis")) + self.x_axis_integration_radioButton.setText( + _translate("MainWindow", "along x-axis") + ) + self.y_axis_integration_radioButton.setText( + _translate("MainWindow", "along y-axis") + ) self.groupBox_6.setTitle(_translate("MainWindow", "Rebin")) self.label.setText(_translate("MainWindow", "Pixels")) self.groupBox_5.setTitle(_translate("MainWindow", "Algorithm")) - self.sliding_average_checkBox.setText(_translate("MainWindow", "Sliding Average")) + self.sliding_average_checkBox.setText( + _translate("MainWindow", "Sliding Average") + ) self.error_function_checkBox.setText(_translate("MainWindow", "Error Function")) self.change_point_checkBox.setText(_translate("MainWindow", "Change Point")) self.groupBox_3.setTitle(_translate("MainWindow", "Sorting Files")) - self.sort_files_by_time_radioButton.setText(_translate("MainWindow", "by Time Stamp")) + self.sort_files_by_time_radioButton.setText( + _translate("MainWindow", "by Time Stamp") + ) self.sort_files_by_name_radioButton.setText(_translate("MainWindow", "by Name")) - self.time_between_runs_label.setText(_translate("MainWindow", "-> Time Between Runs")) + self.time_between_runs_label.setText( + _translate("MainWindow", "-> Time Between Runs") + ) self.time_between_runs_units_label.setText(_translate("MainWindow", "s")) self.groupBox.setTitle(_translate("MainWindow", "Water Intake Y Axis")) self.pixel_radioButton.setText(_translate("MainWindow", "Pixel")) self.distance_radioButton.setText(_translate("MainWindow", "Distance")) - self.water_intake_distance_label.setText(_translate("MainWindow", "-> 1 pixel = ")) + self.water_intake_distance_label.setText( + _translate("MainWindow", "-> 1 pixel = ") + ) self.pixel_size_units.setText(_translate("MainWindow", "mm")) - self.ignore_first_image_checkbox.setText(_translate("MainWindow", "Ignore first image")) + self.ignore_first_image_checkbox.setText( + _translate("MainWindow", "Ignore first image") + ) self.help_button.setText(_translate("MainWindow", "HELP")) self.ok_button.setText(_translate("MainWindow", "OK")) self.menuFile_2.setTitle(_translate("MainWindow", "File")) @@ -346,7 +408,9 @@ def retranslateUi(self, MainWindow): self.menuExport.setTitle(_translate("MainWindow", "Export")) self.actionExport_Profile.setText(_translate("MainWindow", "Profiles ...")) self.actionWater_Intake.setText(_translate("MainWindow", "Water Intake ...")) - self.actionImportedFilesMetadata.setText(_translate("MainWindow", "Imported Files and Metadata ...")) + self.actionImportedFilesMetadata.setText( + _translate("MainWindow", "Imported Files and Metadata ...") + ) self.actionBy_Time_Stamp.setText(_translate("MainWindow", "by Time Stamp")) self.actionBy_File_Name.setText(_translate("MainWindow", "by File Name")) self.actionDsc_files.setText(_translate("MainWindow", "dsc files ...")) diff --git a/notebooks/__code/utilities.py b/notebooks/__code/utilities.py index 9cd2feb40..19787d607 100755 --- a/notebooks/__code/utilities.py +++ b/notebooks/__code/utilities.py @@ -13,7 +13,9 @@ from ipywidgets import widgets -def calculate_file_temperature(left_T=-1, right_T=-1, left_time=-1, right_time=-1, file_time=-1): +def calculate_file_temperature( + left_T=-1, right_T=-1, left_time=-1, right_time=-1, file_time=-1 +): coeff = (float(right_T) - float(left_T)) / (float(right_time) - float(left_time)) part1 = coeff * (float(file_time) - float(left_time)) return part1 + float(left_T) @@ -44,13 +46,19 @@ def extract_temperature(index=-1, temperature_array=[], time_stamp_array=[]): file_time = time_stamp_array[index] file_temperature = calculate_file_temperature( - left_T=left_T, right_T=right_T, left_time=left_time, right_time=right_time, file_time=file_time + left_T=left_T, + right_T=right_T, + left_time=left_time, + right_time=right_time, + file_time=file_time, ) return file_temperature -def retrieve_T_from_file_vs_temperature_array(file_name="", file_array=[], temperature_array=[]): +def retrieve_T_from_file_vs_temperature_array( + file_name="", file_array=[], temperature_array=[] +): index = file_array.index(file_name) return temperature_array[index] @@ -228,7 +236,9 @@ def rename_files(dict_old_new_names={}, new_output_folder=""): w1.value = _index + 1 -def copy_files(dict_old_new_names={}, input_folder_name=None, new_output_folder="", overwrite=True): +def copy_files( + dict_old_new_names={}, input_folder_name=None, new_output_folder="", overwrite=True +): make_dir(dir=new_output_folder, overwrite=overwrite) nbr_files = len(dict_old_new_names.keys()) @@ -311,7 +321,9 @@ def new_runs(self, list_runs=[]): # remove the runs from list_runs and list_current_runs clean_list_runs = list(list_runs - _list_runs_to_remove) - clean_list_current_runs = list(set(self.list_current_runs) - _list_runs_to_remove) + clean_list_current_runs = list( + set(self.list_current_runs) - _list_runs_to_remove + ) new_list_current_runs = clean_list_runs + clean_list_current_runs self.list_current_runs = new_list_current_runs @@ -356,7 +368,9 @@ def match_list(reference_list=[], our_list=[]): # print("new list: {}".format(_our_list)) while _our_list: - _ref_index = match_list(reference_list=_list_full_reference, our_list=_our_list) + _ref_index = match_list( + reference_list=_list_full_reference, our_list=_our_list + ) _group = [_our_list[0], _our_list[_ref_index - 1]] # print("_group: {}".format(_group)) diff --git a/notebooks/__code/venus_chop_tcero_monitor_counts/main.py b/notebooks/__code/venus_chop_tcero_monitor_counts/main.py index f70a5c6b9..7b9752b33 100755 --- a/notebooks/__code/venus_chop_tcero_monitor_counts/main.py +++ b/notebooks/__code/venus_chop_tcero_monitor_counts/main.py @@ -54,9 +54,9 @@ def load_data(self, list_nexus_file_name): for _nexus in list_nexus_file_name: with h5py.File(_nexus, "r") as nxs: try: - _average_phase_delay = nxs["entry"]["DASlogs"]["BL10:CHOP:TCERO:PhaseDelaySP"]["average_value"][()][ - 0 - ] + _average_phase_delay = nxs["entry"]["DASlogs"][ + "BL10:CHOP:TCERO:PhaseDelaySP" + ]["average_value"][()][0] _monitor_counts = nxs["entry"]["monitor1"]["total_counts"][()][0] except KeyError: self.list_nexus_with_missing_key.append(_nexus) @@ -73,7 +73,9 @@ def load_data(self, list_nexus_file_name): def display(self): minimum_counts = np.nanmin(self.monitor_counts_array) - index_of_minimum = int(np.where(self.monitor_counts_array == minimum_counts)[0][0]) + index_of_minimum = int( + np.where(self.monitor_counts_array == minimum_counts)[0][0] + ) value_of_phase_delay_at_that_minimum = self.phase_delay_array[index_of_minimum] @@ -108,5 +110,7 @@ def display(self): print(f"Start acquisition time: {start_time[0].decode('utf-8')}") print("") - _list_nexus_with_missing_key = [os.path.basename(_file) for _file in self.list_nexus_with_missing_key] + _list_nexus_with_missing_key = [ + os.path.basename(_file) for _file in self.list_nexus_with_missing_key + ] print(f"List of nexus that had missing key: {_list_nexus_with_missing_key}") diff --git a/notebooks/__code/venus_display_metadata_from_hdf5/main.py b/notebooks/__code/venus_display_metadata_from_hdf5/main.py index 7a8ba28e6..44ad75475 100755 --- a/notebooks/__code/venus_display_metadata_from_hdf5/main.py +++ b/notebooks/__code/venus_display_metadata_from_hdf5/main.py @@ -38,7 +38,10 @@ def select_event_nexus(self, display_average_metadata=False): self.display_average_metadata = display_average_metadata self.legend_ui = widgets.Text( - value="", placeholder="Type your legend here", description="Legend:", disabled=False + value="", + placeholder="Type your legend here", + description="Legend:", + disabled=False, ) display(self.legend_ui) @@ -69,7 +72,9 @@ def mean_value_of_pv_path(h5file, list_pv_path): return np.mean(np.array(nxs["value"][:])) def load_metadata(self, list_nexus_file_name): - dict_data, list_mean_data, list_nexus = self._load_metadata(list_nexus_file_name) + dict_data, list_mean_data, list_nexus = self._load_metadata( + list_nexus_file_name + ) self.dict_data = dict_data self.list_mean_data = list_mean_data self.list_nexus = list_nexus @@ -87,11 +92,17 @@ def display_metadata(self): ) for _index, _pv_key in enumerate(self.list_mean_data.keys()): - axs[_index, 0].plot(self.list_mean_data[_pv_key], "o-", label=self.legend_ui.value) + axs[_index, 0].plot( + self.list_mean_data[_pv_key], "o-", label=self.legend_ui.value + ) axs[_index, 0].set_title(f"Mean {list_pvs[_pv_key]['label']}") axs[_index, 0].set_ylabel(list_pvs[_pv_key]["label"]) axs[_index, 0].set_xlabel("Nexus file index") - axs[_index, 0].set_xticks(range(len(self.list_mean_data[_pv_key])), labels=self.list_nexus, rotation=90) + axs[_index, 0].set_xticks( + range(len(self.list_mean_data[_pv_key])), + labels=self.list_nexus, + rotation=90, + ) axs[_index, 0].legend() for _nexus in self.dict_data.keys(): @@ -116,21 +127,32 @@ def display_first_and_second_set(self): ) for _index, _pv_key in enumerate(self.list_mean_data.keys()): - axs[_index, 0].plot(self.list_mean_data[_pv_key], "o-", label=self.legend_ui.value) + axs[_index, 0].plot( + self.list_mean_data[_pv_key], "o-", label=self.legend_ui.value + ) axs[_index, 0].set_title(f"Mean {list_pvs[_pv_key]['label']}") axs[_index, 0].set_ylabel(list_pvs[_pv_key]["label"]) axs[_index, 0].set_xlabel("Nexus file index") - axs[_index, 0].set_xticks(range(len(self.list_mean_data[_pv_key])), labels=self.list_nexus, rotation=90) + axs[_index, 0].set_xticks( + range(len(self.list_mean_data[_pv_key])), + labels=self.list_nexus, + rotation=90, + ) axs[_index, 0].legend() axs[_index, 1].plot( - self.list_mean_data_second_set[_pv_key], "o-", color="red", label=self.legend_second_set_ui.value + self.list_mean_data_second_set[_pv_key], + "o-", + color="red", + label=self.legend_second_set_ui.value, ) axs[_index, 1].set_title(f"Mean {list_pvs[_pv_key]['label']}") axs[_index, 1].set_ylabel(list_pvs[_pv_key]["label"]) axs[_index, 1].set_xlabel("Nexus file index") axs[_index, 1].set_xticks( - range(len(self.list_mean_data_second_set[_pv_key])), labels=self.list_nexus_second_set, rotation=90 + range(len(self.list_mean_data_second_set[_pv_key])), + labels=self.list_nexus_second_set, + rotation=90, ) axs[_index, 1].legend() @@ -146,11 +168,17 @@ def display_average_metadata(self): ) for _index, _pv_key in enumerate(self.list_mean_data.keys()): - axs[_index].plot(self.list_mean_data[_pv_key], "o-", label=self.legend_ui.value) + axs[_index].plot( + self.list_mean_data[_pv_key], "o-", label=self.legend_ui.value + ) axs[_index].set_title(f"Mean {list_pvs[_pv_key]['label']}") axs[_index].set_ylabel(list_pvs[_pv_key]["label"]) axs[_index].set_xlabel("Nexus file index") - axs[_index].set_xticks(range(len(self.list_mean_data[_pv_key])), labels=self.list_nexus, rotation=90) + axs[_index].set_xticks( + range(len(self.list_mean_data[_pv_key])), + labels=self.list_nexus, + rotation=90, + ) axs[_index].legend() plt.tight_layout() @@ -160,7 +188,10 @@ def select_event_nexus_second_set(self, display_average_metadata=False): self.display_average_metadata = display_average_metadata self.legend_second_set_ui = widgets.Text( - value="", placeholder="Type your legend here", description="Legend:", disabled=False + value="", + placeholder="Type your legend here", + description="Legend:", + disabled=False, ) display(self.legend_second_set_ui) @@ -179,7 +210,9 @@ def select_event_nexus_second_set(self, display_average_metadata=False): self.nexus_ui.show() def load_metadata_second_set(self, list_nexus_file_name): - dict_data, list_mean_data, list_nexus = self._load_metadata(list_nexus_file_name) + dict_data, list_mean_data, list_nexus = self._load_metadata( + list_nexus_file_name + ) self.dict_data_second_set = dict_data self.list_mean_data_second_set = list_mean_data self.list_nexus_second_set = list_nexus @@ -193,7 +226,10 @@ def _load_metadata(self, list_nexus_file_name): list_nexus = [] list_mean_data = {_key: [] for _key in list_pvs.keys()} - dict_data = {os.path.basename(_nexus).split(".")[0]: {} for _nexus in list_nexus_file_name} + dict_data = { + os.path.basename(_nexus).split(".")[0]: {} + for _nexus in list_nexus_file_name + } for _nexus in list_nexus_file_name: _base_nexus = os.path.basename(_nexus) @@ -205,8 +241,12 @@ def _load_metadata(self, list_nexus_file_name): _pv_path = list_pvs[_pv_key]["path"] _pv_label = list_pvs[_pv_key]["label"] try: - _value = VenusDisplayMetadataFromHdf5.value_of_pv_path(_nexus, _pv_path) - _mean_value = VenusDisplayMetadataFromHdf5.mean_value_of_pv_path(_nexus, _pv_path) + _value = VenusDisplayMetadataFromHdf5.value_of_pv_path( + _nexus, _pv_path + ) + _mean_value = VenusDisplayMetadataFromHdf5.mean_value_of_pv_path( + _nexus, _pv_path + ) list_mean_data[_pv_key].append(_mean_value) dict_data[_short_name_nexus][_pv_key] = _value logging.info(f"{_pv_label}: {_mean_value}") diff --git a/notebooks/__code/venus_display_nexus_tpx3/main.py b/notebooks/__code/venus_display_nexus_tpx3/main.py index b9dec6fc9..ad896cf79 100644 --- a/notebooks/__code/venus_display_nexus_tpx3/main.py +++ b/notebooks/__code/venus_display_nexus_tpx3/main.py @@ -57,12 +57,15 @@ def load_data(self, nexus_full_path): display(HTML("
")) def _load_data_single_bank(self, nexus_full_path, bank=100): - # Use the output widget context to capture all output with self.output: with h5py.File(nexus_full_path, "r") as hdf5_data: - event_time_offset_original = hdf5_data["entry"][f"bank{bank}_events"]["event_time_offset"][:] - event_id_original = hdf5_data["entry"][f"bank{bank}_events"]["event_id"][:] + event_time_offset_original = hdf5_data["entry"][f"bank{bank}_events"][ + "event_time_offset" + ][:] + event_id_original = hdf5_data["entry"][f"bank{bank}_events"][ + "event_id" + ][:] offset_value = 1000000 # Offset value event_id_original -= offset_value @@ -70,7 +73,9 @@ def _load_data_single_bank(self, nexus_full_path, bank=100): event_id = event_id_original hist, bin_edges = np.histogram( - event_time_offset_original, bins=self.bin_size.value, range=(0, self.max_time.value) + event_time_offset_original, + bins=self.bin_size.value, + range=(0, self.max_time.value), ) # First plot - histogram diff --git a/notebooks/__code/venus_list_of_nexus_with_pc_above_threshold/main.py b/notebooks/__code/venus_list_of_nexus_with_pc_above_threshold/main.py index be9f86fce..931c1ce16 100755 --- a/notebooks/__code/venus_list_of_nexus_with_pc_above_threshold/main.py +++ b/notebooks/__code/venus_list_of_nexus_with_pc_above_threshold/main.py @@ -34,7 +34,9 @@ def __init__(self, working_dir=None): def proton_charge_threshold(self): pc_ui = widgets.VBox( [ - widgets.Label("Proton charge threshold (C):", layout=widgets.Layout(width="50%")), + widgets.Label( + "Proton charge threshold (C):", layout=widgets.Layout(width="50%") + ), widgets.FloatText(1, layout=widgets.Layout(width="200px")), ] ) diff --git a/notebooks/__code/venus_monitor_hdf5/main.py b/notebooks/__code/venus_monitor_hdf5/main.py index ba6889e51..b1e6bee7d 100755 --- a/notebooks/__code/venus_monitor_hdf5/main.py +++ b/notebooks/__code/venus_monitor_hdf5/main.py @@ -56,8 +56,12 @@ def load_data(self, list_nexus_file_name): try: with h5py.File(_nexus, "r") as nxs: _index = np.array(nxs["entry"]["monitor1"]["event_index"]) - _time_offset = np.array(nxs["entry"]["monitor1"]["event_time_offset"]) - _time_zero = np.array(nxs["entry"]["monitor1"]["event_time_zero"]) * 10 # to convert to 100ns units + _time_offset = np.array( + nxs["entry"]["monitor1"]["event_time_offset"] + ) + _time_zero = ( + np.array(nxs["entry"]["monitor1"]["event_time_zero"]) * 10 + ) # to convert to 100ns units _total_counts = nxs["entry"]["monitor1"]["total_counts"][()] list_data[_nexus] = { @@ -68,7 +72,11 @@ def load_data(self, list_nexus_file_name): } except KeyError: - display(HTML("Error loading the monitor data!
Entry is missing!
")) + display( + HTML( + "Error loading the monitor data!
Entry is missing!
" + ) + ) list_data[_nexus] = None self.list_data = list_data @@ -83,14 +91,23 @@ def define_settings(self): # distance distance_lock = widgets.Checkbox(False, layout=widgets.Layout(width="150px")) - distance_label = widgets.Label("Distance source_monitor", layout=widgets.Layout(width="200px"), disabled=True) + distance_label = widgets.Label( + "Distance source_monitor", + layout=widgets.Layout(width="200px"), + disabled=True, + ) self.distance_source_detector_ui = widgets.FloatText( value=25, layout=widgets.Layout(width="100px"), disabled=True ) distance_units_label = widgets.Label("m") distance_layout = widgets.HBox( - [distance_lock, distance_label, self.distance_source_detector_ui, distance_units_label] + [ + distance_lock, + distance_label, + self.distance_source_detector_ui, + distance_units_label, + ] ) def lock_changed(changes): @@ -100,7 +117,9 @@ def lock_changed(changes): distance_lock.observe(lock_changed, names="value") # monitor offset - monitor_label = widgets.Label("Monitor offset", layout=widgets.Layout(width="100px")) + monitor_label = widgets.Label( + "Monitor offset", layout=widgets.Layout(width="100px") + ) self.monitor_offset_ui = widgets.FloatText( value=0, layout=widgets.Layout(width="100px"), @@ -123,7 +142,9 @@ def record_settings(self): def calculate_data_tof(self, time_offset=None, max_bin=2 * 16666): bins_size = self.bins_size - histo_tof, bins_tof = np.histogram(time_offset, bins=np.arange(0, max_bin, bins_size)) + histo_tof, bins_tof = np.histogram( + time_offset, bins=np.arange(0, max_bin, bins_size) + ) detector_offset_micros = self.detector_offset_micros _bins_tof = np.empty_like(bins_tof) for _index, _bin in enumerate(bins_tof): @@ -136,13 +157,16 @@ def calculate_data_lambda(self, bins_tof=None): distance_source_detector_cm = self.distance_source_detector_m * 100.0 lambda_axis_angstroms = VenusMonitorHdf5.from_micros_to_lambda( - tof_axis_micros=tof_axis, distance_source_detector_cm=distance_source_detector_cm + tof_axis_micros=tof_axis, + distance_source_detector_cm=distance_source_detector_cm, ) return lambda_axis_angstroms def calculate_data_energy(self): lambda_axis_angstroms = self.lambda_axis_angstroms - self.energy_axis_ev = 1000 * (ENERGY_CONVERSION_FACTOR / (lambda_axis_angstroms * lambda_axis_angstroms)) + self.energy_axis_ev = 1000 * ( + ENERGY_CONVERSION_FACTOR / (lambda_axis_angstroms * lambda_axis_angstroms) + ) @staticmethod def from_micros_to_lambda(tof_axis_micros, distance_source_detector_cm=2372.6): @@ -185,7 +209,9 @@ def plot_data(max_bin): continue time_offset = list_data[_key]["event_time_offset"] - bins_tof, histo_tof = self.calculate_data_tof(time_offset=time_offset, max_bin=max_bin) + bins_tof, histo_tof = self.calculate_data_tof( + time_offset=time_offset, max_bin=max_bin + ) self.list_data[_key]["bins_tof"] = bins_tof self.list_data[_key]["histo_tof"] = histo_tof lambda_axis_angstroms = self.calculate_data_lambda(bins_tof=bins_tof) @@ -209,9 +235,14 @@ def plot_data(max_bin): display(HTML("

Total counts:

")) for _key in list_data.keys(): - print(f"{os.path.basename(_key)}: {self.list_data[_key]['total_counts'][0]}") + print( + f"{os.path.basename(_key)}: {self.list_data[_key]['total_counts'][0]}" + ) - display_all = interactive(plot_data, max_bin=widgets.IntSlider(min=16666, max=5 * 16666, value=3 * 16666)) + display_all = interactive( + plot_data, + max_bin=widgets.IntSlider(min=16666, max=5 * 16666, value=3 * 16666), + ) display(display_all) # def display_all_at_once(self): @@ -277,7 +308,9 @@ def export(self, output_folder=None): base_nexus_file_name = os.path.basename(_nexus) split_nexus_file_name = base_nexus_file_name.split(".") time_stamp = get_current_time_in_special_file_name_format() - output_file_name = f"{split_nexus_file_name[0]}_monitor_data_{time_stamp}.txt" + output_file_name = ( + f"{split_nexus_file_name[0]}_monitor_data_{time_stamp}.txt" + ) full_output_file_name = os.path.join(output_folder, output_file_name) tof_axis = self.list_data[_nexus]["bins_tof"] / 10 diff --git a/notebooks/__code/water_intake_profile_calculator.py b/notebooks/__code/water_intake_profile_calculator.py index 98143141e..fcedd6d38 100755 --- a/notebooks/__code/water_intake_profile_calculator.py +++ b/notebooks/__code/water_intake_profile_calculator.py @@ -69,7 +69,12 @@ class WaterIntakeHandler: # fitting by error function requires that the signal goes from max to min values is_data_from_max_to_min = True - def __init__(self, dict_profiles={}, ignore_first_image=True, algorithm_selected="sliding_average"): + def __init__( + self, + dict_profiles={}, + ignore_first_image=True, + algorithm_selected="sliding_average", + ): self.dict_profiles = dict_profiles self.ignore_first_image = ignore_first_image @@ -265,7 +270,8 @@ def __init__(self, parent=None, dict_data={}): QMainWindow.__init__(self, parent=parent) ui_full_path = os.path.join( - os.path.dirname(os.path.dirname(__file__)), os.path.join("ui", "ui_water_intake_profile.ui") + os.path.dirname(os.path.dirname(__file__)), + os.path.join("ui", "ui_water_intake_profile.ui"), ) self.ui = load_ui(ui_full_path, baseinstance=self) @@ -316,7 +322,9 @@ def init_statusbar(self): hori_layout.addWidget(self.height_value) # spacer - spacerItem = QSpacerItem(22520, 40, QSizePolicy.Expanding, QSizePolicy.Expanding) + spacerItem = QSpacerItem( + 22520, 40, QSizePolicy.Expanding, QSizePolicy.Expanding + ) hori_layout.addItem(spacerItem) # progress bar @@ -379,7 +387,9 @@ def _init_pyqtgraph(self): self.water_intake = pg.PlotWidget(title="Water Intake") self.water_intake.plot() self.ui.water_intake_refresh_button = QPushButton("Refresh Water Intake Plot") - self.ui.water_intake_refresh_button.clicked.connect(self.refresh_water_intake_plot_clicked) + self.ui.water_intake_refresh_button.clicked.connect( + self.refresh_water_intake_plot_clicked + ) vertical_layout = QVBoxLayout() vertical_layout.addWidget(self.water_intake) vertical_layout.addWidget(self.ui.water_intake_refresh_button) @@ -483,7 +493,9 @@ def update_infos_tab(self): _time_stamp_unix = f"{_time_stamp_unix:.2f}" _time_stamp_user = list_time_stamp_user[_row] - self.__insert_information_in_table(row=_row, col0=_short_name, col1=_time_stamp_unix, col2=_time_stamp_user) + self.__insert_information_in_table( + row=_row, col0=_short_name, col1=_time_stamp_unix, col2=_time_stamp_user + ) def __clear_infos_table(self): nbr_row = self.ui.tableWidget.rowCount() @@ -577,9 +589,13 @@ def update_water_intake_plot(self): elif algorithm_selected == "error_function": peak = o_water_intake_handler.water_intake_peaks_erf self.water_intake_peaks_erf = peak.copy() - self.dict_error_function_parameters = o_water_intake_handler.dict_error_function_parameters + self.dict_error_function_parameters = ( + o_water_intake_handler.dict_error_function_parameters + ) delta_time = o_water_intake_handler.water_intake_deltatime - self.is_data_from_max_to_min = o_water_intake_handler.is_data_from_max_to_min + self.is_data_from_max_to_min = ( + o_water_intake_handler.is_data_from_max_to_min + ) # due to the fact that the pixel reported may be from the right if self.is_inte_along_x_axis: @@ -630,11 +646,21 @@ def update_water_intake_plot(self): self.water_intake.addItem(err) self.water_intake.plot( - delta_time, peak, symbolPen=None, pen=None, symbol="o", symbolBruch=(200, 200, 200, 50) + delta_time, + peak, + symbolPen=None, + pen=None, + symbol="o", + symbolBruch=(200, 200, 200, 50), ) else: self.water_intake.plot( - delta_time, peak, symbolPen=None, pen=None, symbol="o", symbolBruch=(200, 200, 200, 50) + delta_time, + peak, + symbolPen=None, + pen=None, + symbol="o", + symbolBruch=(200, 200, 200, 50), ) self.water_intake.setLabel("left", y_label) @@ -663,14 +689,26 @@ def update_profile_plot_water_intake_peak(self): is_data_from_max_to_min = self.is_data_from_max_to_min if self.is_inte_along_x_axis: if is_data_from_max_to_min: - peak_value = _water_intake_peaks[index_selected] + int(self.roi["y0"]) + peak_value = _water_intake_peaks[index_selected] + int( + self.roi["y0"] + ) else: - peak_value = int(self.roi["height"] + self.roi["y0"] - _water_intake_peaks[index_selected]) + peak_value = int( + self.roi["height"] + + self.roi["y0"] + - _water_intake_peaks[index_selected] + ) else: if is_data_from_max_to_min: - peak_value = _water_intake_peaks[index_selected] + int(self.roi["x0"]) + peak_value = _water_intake_peaks[index_selected] + int( + self.roi["x0"] + ) else: - peak_value = int(self.roi["width"] + self.roi["x0"] - _water_intake_peaks[index_selected]) + peak_value = int( + self.roi["width"] + + self.roi["x0"] + - _water_intake_peaks[index_selected] + ) elif algorithm_selected == "change_point": _water_intake_peaks = self.water_intake_peaks_change_point.copy() if self.is_inte_along_x_axis: @@ -694,7 +732,11 @@ def update_profile_plot_water_intake_peak(self): xdata = self.live_x_axis fitting_xdata = np.arange(len(xdata)) ydata = WaterIntakeHandler.fitting_function( - fitting_xdata, _fit_parameters["c"], _fit_parameters["w"], _fit_parameters["m"], _fit_parameters["n"] + fitting_xdata, + _fit_parameters["c"], + _fit_parameters["w"], + _fit_parameters["m"], + _fit_parameters["n"], ) if not is_data_from_max_to_min: @@ -728,7 +770,10 @@ def get_profile(self, image): def export_water_intake_clicked(self): _export_folder = QFileDialog.getExistingDirectory( - self, directory=self.working_dir, caption="Select Output Folder", options=QFileDialog.ShowDirsOnly + self, + directory=self.working_dir, + caption="Select Output Folder", + options=QFileDialog.ShowDirsOnly, ) if _export_folder: export_folder = os.path.abspath(_export_folder) @@ -769,7 +814,9 @@ def export_water_intake_clicked(self): metadata = [] metadata.append("# Water Intake Signal ") - metadata.append(f"# roi [x0, y0, width, height]: [{x0}, {y0}, {width}, {height}]") + metadata.append( + f"# roi [x0, y0, width, height]: [{x0}, {y0}, {width}, {height}]" + ) metadata.append(f"# integration direction: {inte_direction}") metadata.append(f"# input folder: {full_input_folder}") metadata.append(f"# algorithm used: {_algo_used}") @@ -777,21 +824,33 @@ def export_water_intake_clicked(self): metadata.append("# ") metadata.append(f"# Time(s), {yaxis_label}, error") - export_file_name = f"water_intake_of_{short_input_folder}_with_{nbr_files}input_files.txt" + export_file_name = ( + f"water_intake_of_{short_input_folder}_with_{nbr_files}input_files.txt" + ) full_export_file_name = os.path.join(export_folder, export_file_name) if _master_algo_used == "error_function": metadata.append(f"# Time(s), {yaxis_label}, error") data = [ f"{_x_axis}, {_y_axis}, {_error}" - for _x_axis, _y_axis, _error in zip(x_axis, y_axis, error, strict=False) + for _x_axis, _y_axis, _error in zip( + x_axis, y_axis, error, strict=False + ) ] else: metadata.append(f"# Time(s), {yaxis_label}") - data = [f"{_x_axis}, {_y_axis}" for _x_axis, _y_axis, _error in zip(x_axis, y_axis, strict=False)] + data = [ + f"{_x_axis}, {_y_axis}" + for _x_axis, _y_axis, _error in zip(x_axis, y_axis, strict=False) + ] display(HTML(f"Exported water intake file: {full_export_file_name}")) - make_ascii_file(metadata=metadata, data=data, output_file_name=full_export_file_name, dim="1d") + make_ascii_file( + metadata=metadata, + data=data, + output_file_name=full_export_file_name, + dim="1d", + ) def __get_water_intake_yaxis_label(self): if self.ui.pixel_radioButton.isChecked(): @@ -800,7 +859,9 @@ def __get_water_intake_yaxis_label(self): return "distance" def roi_moved(self): - region = self.roi_id.getArraySlice(self.current_image, self.ui.image_view.imageItem) + region = self.roi_id.getArraySlice( + self.current_image, self.ui.image_view.imageItem + ) x0 = region[0][0].start x1 = region[0][0].stop @@ -897,7 +958,9 @@ def __sort_files(self): dict_data = self.dict_data list_images = np.array(dict_data["list_images"].copy()) list_time_stamp = np.array(dict_data["list_time_stamp"].copy()) - list_time_stamp_user_format = np.array(dict_data["list_time_stamp_user_format"].copy()) + list_time_stamp_user_format = np.array( + dict_data["list_time_stamp_user_format"].copy() + ) list_data = np.array(dict_data["list_data"].copy()) if is_by_name: @@ -912,7 +975,9 @@ def __sort_files(self): dict_data["list_images"] = list(sorted_list_images) dict_data["list_time_stamp"] = list(sorted_list_time_stamp) - dict_data["list_time_stamp_user_format"] = list(sorted_list_time_stamp_user_format) + dict_data["list_time_stamp_user_format"] = list( + sorted_list_time_stamp_user_format + ) dict_data["list_data"] = sorted_list_data self.dict_data = dict_data @@ -960,7 +1025,10 @@ def profile_algo_changed(self): def export_table_button_clicked(self): _export_folder = QFileDialog.getExistingDirectory( - self, directory=self.working_dir, caption="Select Output Folder", options=QFileDialog.ShowDirsOnly + self, + directory=self.working_dir, + caption="Select Output Folder", + options=QFileDialog.ShowDirsOnly, ) if _export_folder: @@ -970,7 +1038,9 @@ def export_table_button_clicked(self): list_images = dict_data["list_images"] input_folder = os.path.basename(os.path.dirname(list_images[0])) - output_file_name = os.path.join(export_folder, input_folder + "_sorting_table.txt") + output_file_name = os.path.join( + export_folder, input_folder + "_sorting_table.txt" + ) list_time = dict_data["list_time_stamp"] list_time_stamp_user_format = dict_data["list_time_stamp_user_format"] @@ -989,12 +1059,20 @@ def export_table_button_clicked(self): ) ] - make_ascii_file(metadata=metadata, data=data, output_file_name=output_file_name, dim="1d") + make_ascii_file( + metadata=metadata, + data=data, + output_file_name=output_file_name, + dim="1d", + ) display(HTML(f"Table has been exported in {output_file_name}")) def import_dsc_clicked(self): _dsc_folder = QFileDialog.getExistingDirectory( - self, directory=self.working_dir, caption="Select Output Folder", options=QFileDialog.ShowDirsOnly + self, + directory=self.working_dir, + caption="Select Output Folder", + options=QFileDialog.ShowDirsOnly, ) if _dsc_folder: dsc_folder = os.path.abspath(_dsc_folder) @@ -1022,10 +1100,14 @@ def match_dsc_timestamp_with_original_dict_data(self): for _key in list_images: _short_key = os.path.basename(_key) new_list_time_stamp.append(dict_disc[_short_key]["time_stamp"]) - new_list_time_stamp_user_format.append(dict_disc[_short_key]["time_stamp_user_format"]) + new_list_time_stamp_user_format.append( + dict_disc[_short_key]["time_stamp_user_format"] + ) self.dict_data_raw["list_time_stamp"] = new_list_time_stamp.copy() - self.dict_data_raw["list_time_stamp_user_format"] = new_list_time_stamp_user_format.copy() + self.dict_data_raw["list_time_stamp_user_format"] = ( + new_list_time_stamp_user_format.copy() + ) def algorithm_changed(self): self.update_plots() @@ -1044,7 +1126,9 @@ def ignore_first_image_checkbox_clicked(self): def help_button_clicked(self): import webbrowser - webbrowser.open("https://neutronimaging.pages.ornl.gov/en/tutorial/notebooks/water_intake_profile_calculator/") + webbrowser.open( + "https://neutronimaging.pages.ornl.gov/en/tutorial/notebooks/water_intake_profile_calculator/" + ) def ok_button_clicked(self): # do soemthing here @@ -1071,7 +1155,9 @@ def __init__(self, working_dir="./"): def select_file_help(self, value): import webbrowser - webbrowser.open("https://neutronimaging.pages.ornl.gov/en/tutorial/notebooks/file_selector/#select_profile") + webbrowser.open( + "https://neutronimaging.pages.ornl.gov/en/tutorial/notebooks/file_selector/#select_profile" + ) def select_data(self): help_ui = widgets.Button(description="HELP", button_style="info") @@ -1079,7 +1165,10 @@ def select_data(self): display(help_ui) self.files_ui = fileselector.FileSelectorPanel( - instruction="Select Images ...", start_dir=self.working_dir, next=self.load_and_plot, multiple=True + instruction="Select Images ...", + start_dir=self.working_dir, + next=self.load_and_plot, + multiple=True, ) self.files_ui.show() @@ -1109,7 +1198,9 @@ def __sort_files_using_time_stamp(self, dict_time_stamp): list_images = dict_time_stamp["list_images"].copy() list_time_stamp = dict_time_stamp["list_time_stamp"].copy() - list_time_stamp_user_format = dict_time_stamp["list_time_stamp_user_format"].copy() + list_time_stamp_user_format = dict_time_stamp[ + "list_time_stamp_user_format" + ].copy() list_images = np.array(list_images) time_stamp = np.array(list_time_stamp) @@ -1190,7 +1281,9 @@ def __calculate_individual_profile(self, index_file=0, save=False): if save: list_time_stamp = self.parent.dict_data["list_time_stamp"] - is_sorting_by_name = self.parent.ui.sort_files_by_name_radioButton.isChecked() + is_sorting_by_name = ( + self.parent.ui.sort_files_by_name_radioButton.isChecked() + ) if is_sorting_by_name: time_stamp_first_file = 0 else: @@ -1206,7 +1299,11 @@ def __calculate_individual_profile(self, index_file=0, save=False): x_axis = x_axis[: len(self._profile)] - self.parent.dict_profiles[str(index_file)] = {"data": _profile, "x_axis": x_axis, "delta_time": delta_time} + self.parent.dict_profiles[str(index_file)] = { + "data": _profile, + "x_axis": x_axis, + "delta_time": delta_time, + } def plot(self): self.parent.profile.clear() @@ -1300,16 +1397,22 @@ def export(self): for index in np.arange(first_image, nbr_images): _short_file_name = os.path.basename(list_images[index]) [_basename, _] = os.path.splitext(_short_file_name) - output_file_name = os.path.join(export_folder, _basename + "_profile.txt") + output_file_name = os.path.join( + export_folder, _basename + "_profile.txt" + ) metadata = [] metadata.append("# Profile over ROI selected integrated along x-axis") - metadata.append(f"# roi [x0, y0, width, height]: [{x0}, {y0}, {width}, {height}]") + metadata.append( + f"# roi [x0, y0, width, height]: [{x0}, {y0}, {width}, {height}]" + ) metadata.append(f"# integration direction: {inte_direction}") metadata.append(f"# folder: {input_folder}") metadata.append(f"# filename: {_short_file_name}") metadata.append(f"# timestamp (unix): {list_time_stamp[index]}") - metadata.append(f"# timestamp (user format): {list_time_stamp_user_format[index]}") + metadata.append( + f"# timestamp (user format): {list_time_stamp_user_format[index]}" + ) metadata.append(f"# algorithm used: {_algo_used}") metadata.append("# ") metadata.append("# pixel, counts") @@ -1323,9 +1426,16 @@ def export(self): _line = f"{_pixel_index + y0}, {_counts}" data.append(_line) - make_ascii_file(metadata=metadata, data=data, output_file_name=output_file_name, dim="1d") + make_ascii_file( + metadata=metadata, + data=data, + output_file_name=output_file_name, + dim="1d", + ) self.parent.eventProgress.setValue(index) self.parent.eventProgress.setVisible(False) - display(HTML(f"Exported Profiles files ({nbr_images} files) in {export_folder}")) + display( + HTML(f"Exported Profiles files ({nbr_images} files) in {export_folder}") + ) diff --git a/notebooks/__code/wave_front_dynamics/algorithms.py b/notebooks/__code/wave_front_dynamics/algorithms.py index 6d29dd76b..a7014a6c5 100755 --- a/notebooks/__code/wave_front_dynamics/algorithms.py +++ b/notebooks/__code/wave_front_dynamics/algorithms.py @@ -59,7 +59,11 @@ class Algorithms: progress_bar_ui = None # progress bar ui def __init__( - self, list_data=None, ignore_first_dataset=True, algorithm_selected="sliding_average", progress_bar_ui=None + self, + list_data=None, + ignore_first_dataset=True, + algorithm_selected="sliding_average", + progress_bar_ui=None, ): self.list_data = list_data self.ignore_first_dataset = ignore_first_dataset diff --git a/notebooks/__code/wave_front_dynamics/display.py b/notebooks/__code/wave_front_dynamics/display.py index c2c7b020d..d88d06ff0 100755 --- a/notebooks/__code/wave_front_dynamics/display.py +++ b/notebooks/__code/wave_front_dynamics/display.py @@ -31,8 +31,12 @@ def update_prepare_data_plot(self): new_data = Algorithms.bin_data(data=data, bin_size=bin_size, bin_type=bin_type) self.parent.ui.prepare_data_plot.axes.plot(new_data) - self.parent.ui.prepare_data_plot.axes.axvline(min_data_range_for_plot, linestyle="--", color="blue") - self.parent.ui.prepare_data_plot.axes.axvline(max_data_range_for_plot, linestyle="--", color="red") + self.parent.ui.prepare_data_plot.axes.axvline( + min_data_range_for_plot, linestyle="--", color="blue" + ) + self.parent.ui.prepare_data_plot.axes.axvline( + max_data_range_for_plot, linestyle="--", color="red" + ) self.parent.ui.prepare_data_plot.draw() def display_current_selected_profile_and_edge_position(self): @@ -60,9 +64,13 @@ def display_current_selected_profile_and_edge_position(self): peak_value_array = self.parent.peak_value_arrays[edge_calculation_algorithm] edge_position = peak_value_array[file_index_selected] - self.parent.ui.calculated_edges_plot.axes.axvline(edge_position, linestyle="--", color=color) + self.parent.ui.calculated_edges_plot.axes.axvline( + edge_position, linestyle="--", color=color + ) - self.parent.ui.calculated_edges_plot.axes.set_xlabel("Pixel (relative position) ") + self.parent.ui.calculated_edges_plot.axes.set_xlabel( + "Pixel (relative position) " + ) self.parent.ui.calculated_edges_plot.axes.set_ylabel("Mean counts") self.parent.ui.calculated_edges_plot.draw() self.parent.ui.calculated_edges_widget.setEnabled(True) @@ -87,14 +95,22 @@ def display_all_edge_positions(self): color = algorithms_colors[edge_calculation_algorithm] self.parent.ui.recap_edges_plot.axes.plot( - relative_timestamp, peak_value_array, "*", color=color, label=edge_calculation_algorithm + relative_timestamp, + peak_value_array, + "*", + color=color, + label=edge_calculation_algorithm, ) self.parent.ui.recap_edges_plot.axes.plot( - relative_timestamp[file_index_selected], peak_value_array[file_index_selected], "+" + relative_timestamp[file_index_selected], + peak_value_array[file_index_selected], + "+", ) self.parent.ui.recap_edges_plot.axes.set_xlabel("Relative time (s)") - self.parent.ui.recap_edges_plot.axes.set_ylabel("Wave front position (relative pixel position)") + self.parent.ui.recap_edges_plot.axes.set_ylabel( + "Wave front position (relative pixel position)" + ) self.parent.ui.recap_edges_plot.axes.legend() self.parent.ui.recap_edges_plot.draw() self.parent.ui.recap_edges_widget.setEnabled(True) diff --git a/notebooks/__code/wave_front_dynamics/event_handler.py b/notebooks/__code/wave_front_dynamics/event_handler.py index 80b7733c9..9d12ede49 100755 --- a/notebooks/__code/wave_front_dynamics/event_handler.py +++ b/notebooks/__code/wave_front_dynamics/event_handler.py @@ -34,7 +34,9 @@ def prepare_all_data(self): if not boolean_list_of_files_to_use[_index]: continue _data = o_get.working_range_of_data(data=_data) - prepared_data = Algorithms.bin_data(data=_data, bin_size=bin_size, bin_type=bin_type) + prepared_data = Algorithms.bin_data( + data=_data, bin_size=bin_size, bin_type=bin_type + ) list_of_data_prepared.append(prepared_data) self.parent.event_progress.setValue(_index + 1) QtGui.QGuiApplication.processEvents() @@ -50,12 +52,16 @@ def prepare_data_file_index_slider_changed(self, slider_value=None): self.parent.ui.file_index_value_label.setText(str(slider_value)) boolean_list_of_files_to_use = self.parent.boolean_list_of_files_to_use - self.parent.ui.use_this_file_checkBox.setChecked(boolean_list_of_files_to_use[slider_value]) + self.parent.ui.use_this_file_checkBox.setChecked( + boolean_list_of_files_to_use[slider_value] + ) def use_this_file_clicked(self): file_index = self.parent.ui.file_index_horizontalSlider.value() boolean_list_of_files_to_use = self.parent.boolean_list_of_files_to_use - boolean_list_of_files_to_use[file_index] = self.parent.ui.use_this_file_checkBox.isChecked() + boolean_list_of_files_to_use[file_index] = ( + self.parent.ui.use_this_file_checkBox.isChecked() + ) self.parent.boolean_list_of_files_to_use = boolean_list_of_files_to_use def prepare_data_bin_size_changed(self, slider_value=None): @@ -67,7 +73,9 @@ def prepare_data_bin_size_changed(self, slider_value=None): def check_state_of_prepare_data_plot(self): boolean_list_of_files_to_use = self.parent.boolean_list_of_files_to_use file_index = self.parent.ui.file_index_horizontalSlider.value() - self.parent.ui.prepare_data_widget.setEnabled(boolean_list_of_files_to_use[file_index]) + self.parent.ui.prepare_data_widget.setEnabled( + boolean_list_of_files_to_use[file_index] + ) def calculate_edge_position(self): o_get = Get(parent=self.parent) @@ -78,7 +86,11 @@ def calculate_edge_position(self): o_display.clear_plots() self.reset_peak_value_arrays() - list_algo = [ListAlgorithm.sliding_average, ListAlgorithm.error_function, ListAlgorithm.change_point] + list_algo = [ + ListAlgorithm.sliding_average, + ListAlgorithm.error_function, + ListAlgorithm.change_point, + ] for _algo in list_algo: self.running_algo(algorithm=_algo) # self.check_status_of_edge_calculation_checkboxes() @@ -90,7 +102,11 @@ def calculate_edge_position(self): def running_algo(self, algorithm=None): list_of_data_prepared = self.parent.list_of_data_prepared - show_status_message(parent=self.parent, message=f"Running {algorithm} ...", status=StatusMessageStatus.working) + show_status_message( + parent=self.parent, + message=f"Running {algorithm} ...", + status=StatusMessageStatus.working, + ) o_algo = Algorithms( list_data=list_of_data_prepared, @@ -98,11 +114,18 @@ def running_algo(self, algorithm=None): algorithm_selected=algorithm, progress_bar_ui=self.parent.event_progress, ) - self.parent.peak_value_arrays[algorithm] = o_algo.get_peak_value_array(algorithm_selected=algorithm) - self.parent.data_have_been_reversed_in_calculation = o_algo.data_have_been_reversed_in_calculation + self.parent.peak_value_arrays[algorithm] = o_algo.get_peak_value_array( + algorithm_selected=algorithm + ) + self.parent.data_have_been_reversed_in_calculation = ( + o_algo.data_have_been_reversed_in_calculation + ) show_status_message( - parent=self.parent, message=f"Running {algorithm}: Done", status=StatusMessageStatus.ready, duration_s=10 + parent=self.parent, + message=f"Running {algorithm}: Done", + status=StatusMessageStatus.ready, + duration_s=10, ) def edge_calculation_file_index_slider_changed(self, slider_value=None): @@ -123,9 +146,15 @@ def check_status_of_edge_calculation_buttons(self): if self.parent.peak_value_arrays[ListAlgorithm.error_function] is None: enable_error_function_button = False - self.parent.ui.plot_edge_calculation_sliding_average.setEnabled(enable_sliding_average_button) - self.parent.ui.plot_edge_calculation_error_function.setEnabled(enable_error_function_button) - self.parent.ui.plot_edge_calculation_change_point.setEnabled(enable_change_point_button) + self.parent.ui.plot_edge_calculation_sliding_average.setEnabled( + enable_sliding_average_button + ) + self.parent.ui.plot_edge_calculation_error_function.setEnabled( + enable_error_function_button + ) + self.parent.ui.plot_edge_calculation_change_point.setEnabled( + enable_change_point_button + ) if enable_sliding_average_button: self.parent.ui.plot_edge_calculation_sliding_average.setChecked(True) @@ -169,7 +198,9 @@ def update_list_of_relative_timestamp_of_prepared_data(self): relative_time = float(list_timestamp[_index]) - t0 list_of_timestamp_of_data_prepared.append(relative_time) - self.parent.list_of_timestamp_of_data_prepared = list_of_timestamp_of_data_prepared + self.parent.list_of_timestamp_of_data_prepared = ( + list_of_timestamp_of_data_prepared + ) def update_list_of_original_image_files_to_use(self): list_of_original_image_files = self.parent.list_of_original_image_files @@ -178,6 +209,10 @@ def update_list_of_original_image_files_to_use(self): list_of_original_image_files_to_use = [] for _index, _state in enumerate(boolean_list_of_files_to_use): if _state: - list_of_original_image_files_to_use.append(list_of_original_image_files[_index]) + list_of_original_image_files_to_use.append( + list_of_original_image_files[_index] + ) - self.parent.list_of_original_image_files_to_use = list_of_original_image_files_to_use + self.parent.list_of_original_image_files_to_use = ( + list_of_original_image_files_to_use + ) diff --git a/notebooks/__code/wave_front_dynamics/export.py b/notebooks/__code/wave_front_dynamics/export.py index 05355b667..6146bb5ae 100755 --- a/notebooks/__code/wave_front_dynamics/export.py +++ b/notebooks/__code/wave_front_dynamics/export.py @@ -19,14 +19,23 @@ def run(self): options=QFileDialog.ShowDirsOnly, ) if output_folder: - output_file_name = self.make_up_output_file_name(output_folder=output_folder) + output_file_name = self.make_up_output_file_name( + output_folder=output_folder + ) o_get = Get(parent=self.parent) - list_edge_calculation_algorithm = o_get.edge_calculation_algorithms_to_plot() + list_edge_calculation_algorithm = ( + o_get.edge_calculation_algorithms_to_plot() + ) metadata = self.retrieving_metadata(list_edge_calculation_algorithm) data = self.retrieve_data(list_edge_calculation_algorithm) - make_ascii_file(output_file_name=output_file_name, data=data, metadata=metadata, dim="1d") + make_ascii_file( + output_file_name=output_file_name, + data=data, + metadata=metadata, + dim="1d", + ) show_status_message( parent=self.parent, @@ -63,7 +72,9 @@ def retrieving_metadata(self, list_edge_calculation_algorithm): for edge_calculation_algorithm in list_edge_calculation_algorithm: list_algo.append(edge_calculation_algorithm + " (pixels number)") - metadata = ["# Position of wave edge in nbr of pixels from center of profile using various algorithms"] + metadata = [ + "# Position of wave edge in nbr of pixels from center of profile using various algorithms" + ] metadata.append("# list of image files") for _file in self.parent.list_of_original_image_files_to_use: _str = "# " + _file diff --git a/notebooks/__code/wave_front_dynamics/get.py b/notebooks/__code/wave_front_dynamics/get.py index 6c6808334..75dd61c34 100755 --- a/notebooks/__code/wave_front_dynamics/get.py +++ b/notebooks/__code/wave_front_dynamics/get.py @@ -27,7 +27,9 @@ def edge_calculation_algorithms(self): elif self.parent.ui.edge_calculation_all.isChecked(): return ListAlgorithm.all else: - raise NotImplementedError("edge calculation algorithms not implemented yet!") + raise NotImplementedError( + "edge calculation algorithms not implemented yet!" + ) def edge_calculation_file_index_selected(self): return self.parent.ui.edge_calculation_file_index_slider.value() diff --git a/notebooks/__code/wave_front_dynamics/initialization.py b/notebooks/__code/wave_front_dynamics/initialization.py index 08ee32501..ca9bf7808 100755 --- a/notebooks/__code/wave_front_dynamics/initialization.py +++ b/notebooks/__code/wave_front_dynamics/initialization.py @@ -28,7 +28,9 @@ def widgets(self): self.parent.ui.recap_edges_widget.setEnabled(False) self.parent.ui.calculated_edges_widget.setEnabled(False) - self.parent.ui.edge_calculation_file_index_slider.setMaximum(self.parent.nbr_files - 1) + self.parent.ui.edge_calculation_file_index_slider.setMaximum( + self.parent.nbr_files - 1 + ) data_0 = self.parent.list_of_data[0] nbr_points = len(data_0) @@ -47,8 +49,12 @@ def _matplotlib(parent=None, widget=None): widget.setLayout(layout) return sc - self.parent.prepare_data_plot = _matplotlib(parent=self.parent, widget=self.parent.ui.prepare_data_widget) - self.parent.recap_edges_plot = _matplotlib(parent=self.parent, widget=self.parent.ui.recap_edges_widget) + self.parent.prepare_data_plot = _matplotlib( + parent=self.parent, widget=self.parent.ui.prepare_data_widget + ) + self.parent.recap_edges_plot = _matplotlib( + parent=self.parent, widget=self.parent.ui.recap_edges_widget + ) self.parent.calculated_edges_plot = _matplotlib( parent=self.parent, widget=self.parent.ui.calculated_edges_widget ) diff --git a/notebooks/__code/wave_front_dynamics/loader.py b/notebooks/__code/wave_front_dynamics/loader.py index 54255a326..4fc66b7a9 100755 --- a/notebooks/__code/wave_front_dynamics/loader.py +++ b/notebooks/__code/wave_front_dynamics/loader.py @@ -82,11 +82,22 @@ def loading_radial_profile_file(list_of_ascii_files): list_of_original_image_files = [] list_of_timestamp = [] for _file in list_of_ascii_files: - _data = pd.read_csv(_file, skiprows=6, delimiter=",", names=["mean counts"], dtype=float, index_col=0) + _data = pd.read_csv( + _file, + skiprows=6, + delimiter=",", + names=["mean counts"], + dtype=float, + index_col=0, + ) list_of_data.append(_data) - _original_image_file = retrieve_metadata_value_from_ascii_file(filename=_file, metadata_name="# source image") + _original_image_file = retrieve_metadata_value_from_ascii_file( + filename=_file, metadata_name="# source image" + ) list_of_original_image_files.append(_original_image_file) - _time_stamp = retrieve_metadata_value_from_ascii_file(filename=_file, metadata_name="# timestamp") + _time_stamp = retrieve_metadata_value_from_ascii_file( + filename=_file, metadata_name="# timestamp" + ) list_of_timestamp.append(_time_stamp) nbr_files = str(len(list_of_ascii_files)) diff --git a/notebooks/__code/wave_front_dynamics/wave_front_dynamics.py b/notebooks/__code/wave_front_dynamics/wave_front_dynamics.py index 5080e8ea2..68441f4b5 100755 --- a/notebooks/__code/wave_front_dynamics/wave_front_dynamics.py +++ b/notebooks/__code/wave_front_dynamics/wave_front_dynamics.py @@ -12,7 +12,10 @@ from __code.wave_front_dynamics.event_handler import EventHandler from __code.wave_front_dynamics.export import Export from __code.wave_front_dynamics.initialization import Initialization -from __code.wave_front_dynamics.loader import loading_linear_profile_file, loading_radial_profile_file +from __code.wave_front_dynamics.loader import ( + loading_linear_profile_file, + loading_radial_profile_file, +) class InputASCIIType: @@ -42,10 +45,16 @@ def __init__(self, working_dir="~/"): def load_data(self): list_of_ascii_files = self.list_data_widget.selected - self.input_ascii_file_type = self.determine_input_ascii_file_type(list_of_ascii_files[0]) + self.input_ascii_file_type = self.determine_input_ascii_file_type( + list_of_ascii_files[0] + ) if self.input_ascii_file_type == InputASCIIType.linear_profile: if len(list_of_ascii_files) > 1: - display(HTML("Select only 1 ASCII file generated by the linear profile notebook!")) + display( + HTML( + "Select only 1 ASCII file generated by the linear profile notebook!" + ) + ) return result_dict = loading_linear_profile_file(list_of_ascii_files[0]) display(HTML("Loaded using linear profile method .... Done!")) @@ -75,7 +84,9 @@ def save_list_of_files(self, list_of_files): self.list_of_ascii_files = list_of_files def select_data(self): - instructions = "Select linear profile ASCII or Radial Profile list of ascii files ..." + instructions = ( + "Select linear profile ASCII or Radial Profile list of ascii files ..." + ) self.list_data_widget = fileselector.FileSelectorPanel( instruction=instructions, start_dir=self.working_dir, @@ -116,26 +127,40 @@ class WaveFrontDynamicsUI(QMainWindow): def __init__(self, parent=None, working_dir="./", wave_front_dynamics=None): display( - HTML('Launched UI! ' "(maybe hidden behind this browser!)") + HTML( + 'Launched UI! ' + "(maybe hidden behind this browser!)" + ) ) self.working_dir = working_dir self.wave_front_dynamics = wave_front_dynamics - self.input_ascii_file_type = wave_front_dynamics.input_ascii_file_type # linear or radial profile + self.input_ascii_file_type = ( + wave_front_dynamics.input_ascii_file_type + ) # linear or radial profile self.list_of_data = wave_front_dynamics.list_of_data self.list_timestamp = wave_front_dynamics.list_timestamp - self.list_of_original_image_files = wave_front_dynamics.list_of_original_image_files - self.list_of_original_image_files_to_use = copy.deepcopy(wave_front_dynamics.list_of_original_image_files) + self.list_of_original_image_files = ( + wave_front_dynamics.list_of_original_image_files + ) + self.list_of_original_image_files_to_use = copy.deepcopy( + wave_front_dynamics.list_of_original_image_files + ) if self.list_of_data is None: - display(HTML('No ASCII file loaded!')) + display( + HTML( + 'No ASCII file loaded!' + ) + ) return self.nbr_files = len(self.list_of_data) super(QMainWindow, self).__init__(parent) ui_full_path = os.path.join( - os.path.dirname(os.path.dirname(os.path.dirname(__file__))), os.path.join("ui", "ui_wave_front_dynamics.ui") + os.path.dirname(os.path.dirname(os.path.dirname(__file__))), + os.path.join("ui", "ui_wave_front_dynamics.ui"), ) self.ui = load_ui(ui_full_path, baseinstance=self) self.setWindowTitle("Define center and sector of profile") @@ -148,7 +173,9 @@ def __init__(self, parent=None, working_dir="./", wave_front_dynamics=None): # first file used as reference t0 = float(self.list_timestamp[0]) - self.list_relative_timestamp = [float(_time) - t0 for _time in self.list_timestamp] + self.list_relative_timestamp = [ + float(_time) - t0 for _time in self.list_timestamp + ] o_event = EventHandler(parent=self) o_event.update_list_of_relative_timestamp_of_prepared_data() diff --git a/notebooks/__code/widgets/examples_of_qrangeslider.py b/notebooks/__code/widgets/examples_of_qrangeslider.py index 275be2660..3a2f8e318 100755 --- a/notebooks/__code/widgets/examples_of_qrangeslider.py +++ b/notebooks/__code/widgets/examples_of_qrangeslider.py @@ -12,8 +12,12 @@ rs1.show() rs1.setWindowTitle("example 1") rs1.setRange(15, 35) -rs1.setBackgroundStyle("background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #222, stop:1 #333);") -rs1.setSpanStyle("background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #282, stop:1 #393);") +rs1.setBackgroundStyle( + "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #222, stop:1 #333);" +) +rs1.setSpanStyle( + "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #282, stop:1 #393);" +) # Example 2 rs2 = QRangeSlider() @@ -58,7 +62,9 @@ rs3.setMin(0) rs3.setMax(2000) rs3.setRange(500, 1253) -rs3.setBackgroundStyle("background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #aaa, stop:1 #777);") +rs3.setBackgroundStyle( + "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #aaa, stop:1 #777);" +) rs3.handle.setStyleSheet("background: url(data/sin.png) repeat-x; border: 0px;") rs3.setStyleSheet(""" QRangeSlider > QSplitter::handle { diff --git a/notebooks/__code/widgets/qrangeslider.py b/notebooks/__code/widgets/qrangeslider.py index 2e534809c..1d008bf81 100755 --- a/notebooks/__code/widgets/qrangeslider.py +++ b/notebooks/__code/widgets/qrangeslider.py @@ -61,7 +61,14 @@ import sys from qtpy import QtCore, QtGui -from qtpy.QtWidgets import QApplication, QGridLayout, QGroupBox, QHBoxLayout, QSplitter, QWidget +from qtpy.QtWidgets import ( + QApplication, + QGridLayout, + QGroupBox, + QHBoxLayout, + QSplitter, + QWidget, +) try: _fromUtf8 = QtCore.QString.fromUtf8 @@ -517,6 +524,10 @@ def _unlockWidth(widget): rs = QRangeSlider() rs.show() rs.setRange(15, 35) - rs.setBackgroundStyle("background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #222, stop:1 #333);") - rs.handle.setStyleSheet("background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #282, stop:1 #393);") + rs.setBackgroundStyle( + "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #222, stop:1 #333);" + ) + rs.handle.setStyleSheet( + "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #282, stop:1 #393);" + ) app.exec_() diff --git a/notebooks/_before_and_after_github_script.py b/notebooks/_before_and_after_github_script.py index a4eda503b..bdc4910ac 100755 --- a/notebooks/_before_and_after_github_script.py +++ b/notebooks/_before_and_after_github_script.py @@ -9,12 +9,23 @@ parser = argparse.ArgumentParser(description="Before Push or After Pull from GitHub") parser.add_argument( - "-b", "--before_push", action="store_true", help="Convert all .ipynb to .py in converted_notebooks folder" + "-b", + "--before_push", + action="store_true", + help="Convert all .ipynb to .py in converted_notebooks folder", ) parser.add_argument( - "-a", "--after_pull", action="store_true", help="Convert all .py from converted_notebooks folder to .ipynb" + "-a", + "--after_pull", + action="store_true", + help="Convert all .py from converted_notebooks folder to .ipynb", +) +parser.add_argument( + "-d", + "--use_debugging_mode", + action="store_true", + help="Change the debugging mode on/off", ) -parser.add_argument("-d", "--use_debugging_mode", action="store_true", help="Change the debugging mode on/off") def run(): diff --git a/notebooks/converted_notebooks/CT_reconstruction.py b/notebooks/converted_notebooks/CT_reconstruction.py index ff2614576..e4c16f7b6 100755 --- a/notebooks/converted_notebooks/CT_reconstruction.py +++ b/notebooks/converted_notebooks/CT_reconstruction.py @@ -23,13 +23,19 @@ # # Input Settings -data_folder = "/Volumes/G-DRIVE/IPTS/IPTS-25519-iMars3D-command-line/raw/ct_scans/2021_07_21_1in/" -ob_folder = "/Volumes/G-DRIVE/IPTS/IPTS-25519-iMars3D-command-line/raw/ob/2021_07_21_1in/" +data_folder = ( + "/Volumes/G-DRIVE/IPTS/IPTS-25519-iMars3D-command-line/raw/ct_scans/2021_07_21_1in/" +) +ob_folder = ( + "/Volumes/G-DRIVE/IPTS/IPTS-25519-iMars3D-command-line/raw/ob/2021_07_21_1in/" +) df_folder = "/Volumes/G-DRIVE/IPTS/IPTS-25519-iMars3D-command-line/raw/df/2021_07_19/" ct_sig = "treated_1inch" ct_scan_root = "/Volumes/G-DRIVE/IPTS/IPTS-25519-iMars3D-command-line/raw/ct_scans/" -ct_dir = "/Volumes/G-DRIVE/IPTS/IPTS-25519-iMars3D-command-line/raw/ct_scans/2021_07_21_1in/" +ct_dir = ( + "/Volumes/G-DRIVE/IPTS/IPTS-25519-iMars3D-command-line/raw/ct_scans/2021_07_21_1in/" +) iptsdir = "/Volumes/G-DRIVE/IPTS/IPTS-25519-iMars3D-command-line/" outdir = "/Volumes/G-DRIVE/IPTS/IPTS-25519-iMars3D-command-line/shared/processed_data/2021_07_21_1in" instrument = "CG1D" diff --git a/notebooks/converted_notebooks/TESTING_images_and_metadata_extrapolation_matcher.py b/notebooks/converted_notebooks/TESTING_images_and_metadata_extrapolation_matcher.py index b4e6ada45..91b7a81b1 100755 --- a/notebooks/converted_notebooks/TESTING_images_and_metadata_extrapolation_matcher.py +++ b/notebooks/converted_notebooks/TESTING_images_and_metadata_extrapolation_matcher.py @@ -22,7 +22,9 @@ import pandas as pd from __code import time_utility -from __code.images_and_metadata_extrapolation_matcher import ImagesAndMetadataExtrapolationMatcher +from __code.images_and_metadata_extrapolation_matcher import ( + ImagesAndMetadataExtrapolationMatcher, +) # - @@ -31,7 +33,9 @@ ascii1 = "/Volumes/my_book_thunderbolt_duo/IPTS/IPTS-20444-Regina/TESTING_SET/images_timestamp_infos.txt" ascii2 = "/Volumes/my_book_thunderbolt_duo/IPTS/IPTS-20444-Regina/TESTING_SET/Sep_17_metadata_report_from_oncat.txt" -o_matcher = ImagesAndMetadataExtrapolationMatcher(ascii_file_1=ascii1, ascii_file_2=ascii2) +o_matcher = ImagesAndMetadataExtrapolationMatcher( + ascii_file_1=ascii1, ascii_file_2=ascii2 +) # + # pprint.pprint("ascii1") @@ -52,7 +56,9 @@ ascii3 = "/Volumes/my_book_thunderbolt_duo/IPTS/IPTS-20444-Regina/TESTING_SET/A49_3_at80C_4p1mm_120s_2_03_OCV_C03_2columns.txt" -o_matcher = ImagesAndMetadataExtrapolationMatcher(ascii_file_1=ascii1, ascii_file_2=ascii3) +o_matcher = ImagesAndMetadataExtrapolationMatcher( + ascii_file_1=ascii1, ascii_file_2=ascii3 +) # + # print("ascii1") @@ -70,7 +76,12 @@ ascii_file_2_dataframe.set_index("timestamp_user_format") # - -merged_dataframe = pd.merge(ascii_file_1_dataframe, ascii_file_2_dataframe, on="timestamp_user_format", how="outer") +merged_dataframe = pd.merge( + ascii_file_1_dataframe, + ascii_file_2_dataframe, + on="timestamp_user_format", + how="outer", +) merged_dataframe.sort_values(by="timestamp_user_format", inplace=True) merged_dataframe = merged_dataframe.reset_index(drop=True) merged_dataframe @@ -104,7 +115,9 @@ def convert_to_second(timestamp_value, timestamp_format="%Y-%m-%d %I:%M:%S"): # + -def calculate_extrapolated_metadata(global_index=-1, metadata_array=[], timestamp_array=[]): +def calculate_extrapolated_metadata( + global_index=-1, metadata_array=[], timestamp_array=[] +): # print("calculate_extrapolated_metadata") # print("metadata_array: {}".format(metadata_array)) # print("timestamp_array: {}".format(timestamp_array)) @@ -169,7 +182,9 @@ def extrapolate_value(x=1, x_left=1, x_right=1, y_left=1, y_right=1): _metadata_value = metadata_array[_index] if np.isnan(_metadata_value): _new_value = calculate_extrapolated_metadata( - global_index=_index, metadata_array=metadata_array, timestamp_array=timestamp_array + global_index=_index, + metadata_array=metadata_array, + timestamp_array=timestamp_array, ) voltage_extrapolated_array.append(_new_value) else: @@ -183,7 +198,10 @@ def extrapolate_value(x=1, x_left=1, x_right=1, y_left=1, y_right=1): # - time_column = timestamp_array -time_column_s = [convert_to_second(_time, timestamp_format="%Y-%m-%d %I:%M:%S") for _time in time_column] +time_column_s = [ + convert_to_second(_time, timestamp_format="%Y-%m-%d %I:%M:%S") + for _time in time_column +] # %matplotlib notebook @@ -194,7 +212,8 @@ def extrapolate_value(x=1, x_left=1, x_right=1, y_left=1, y_right=1): timestamp_with_voltage_known = ascii_file_2_dataframe["timestamp_user_format"] time_column_voltage_known = timestamp_with_voltage_known time_column_s_known = [ - convert_to_second(_time, timestamp_format="%Y-%m-%d %I:%M:%S") for _time in time_column_voltage_known + convert_to_second(_time, timestamp_format="%Y-%m-%d %I:%M:%S") + for _time in time_column_voltage_known ] voltage_column = ascii_file_2_dataframe["Voltage"] @@ -208,7 +227,8 @@ def extrapolate_value(x=1, x_left=1, x_right=1, y_left=1, y_right=1): # time_column_voltage_unknown = ascii_file_1_dataframe['timestamp_user_format'] # time_column_voltage_unknown = time_column_voltage_unknown time_column_s_unknown = [ - convert_to_second(_time, timestamp_format="%Y-%m-%d %I:%M:%S") for _time in time_column_voltage_unknown + convert_to_second(_time, timestamp_format="%Y-%m-%d %I:%M:%S") + for _time in time_column_voltage_unknown ] @@ -221,7 +241,12 @@ def extrapolate_value(x=1, x_left=1, x_right=1, y_left=1, y_right=1): for _vl in time_column_s_unknown: ax.axvline(x=_vl, color="r", linestyle="--") -ax.plot(time_column_s_unknown, voltage_extrapolated_array, "*g", label="Extrapolated metadata") +ax.plot( + time_column_s_unknown, + voltage_extrapolated_array, + "*g", + label="Extrapolated metadata", +) # ax.axvline(x=time_column_s_unknown[0], color='r', linestyle='--', label="Regina's metadata") ax.legend() @@ -249,7 +274,9 @@ def extrapolate_value(x=1, x_left=1, x_right=1, y_left=1, y_right=1): _dataframe1 time_column = np.asarray(_dataframe1[" timestamp_user_format"]) -time_column_s = [get_seconds(_time, time_format="%Y-%m-%d %I:%M:%S") for _time in time_column] +time_column_s = [ + get_seconds(_time, time_format="%Y-%m-%d %I:%M:%S") for _time in time_column +] # + fig, ax = plt.subplots() @@ -318,7 +345,9 @@ def format_time(old_format): if "##filename" in _dataframe: print("yes") -o_matcher = ImagesAndMetadataExtrapolationMatcher(filename_vs_timestamp=ascii1, metadata_ascii_file=ascii2) +o_matcher = ImagesAndMetadataExtrapolationMatcher( + filename_vs_timestamp=ascii1, metadata_ascii_file=ascii2 +) # output file diff --git a/notebooks/converted_notebooks/TEST__registration.py b/notebooks/converted_notebooks/TEST__registration.py index 6e0313895..dcdeac7fc 100755 --- a/notebooks/converted_notebooks/TEST__registration.py +++ b/notebooks/converted_notebooks/TEST__registration.py @@ -98,7 +98,9 @@ import glob -list_files = glob.glob("/Volumes/my_book_thunderbolt_duo/IPTS/IPTS-19921-Charles/registration_test_set/*.tif") +list_files = glob.glob( + "/Volumes/my_book_thunderbolt_duo/IPTS/IPTS-19921-Charles/registration_test_set/*.tif" +) [os.path.exists(_file) for _file in list_files] diff --git a/notebooks/converted_notebooks/TEST_cylindrical_geometry_correction.py b/notebooks/converted_notebooks/TEST_cylindrical_geometry_correction.py index eb6bac887..4a0d8c98e 100755 --- a/notebooks/converted_notebooks/TEST_cylindrical_geometry_correction.py +++ b/notebooks/converted_notebooks/TEST_cylindrical_geometry_correction.py @@ -37,7 +37,10 @@ o_selection = FileSelection() o_selection.load_files(list_files) -o_profile = ProfileUi(working_dir=os.path.dirname(list_files[0]), data_dict=o_selection.data_dict["sample"]) +o_profile = ProfileUi( + working_dir=os.path.dirname(list_files[0]), + data_dict=o_selection.data_dict["sample"], +) o_profile.show() diff --git a/notebooks/converted_notebooks/TEST_figuring_out_how_to_control_size_of_pyqtgraph.py b/notebooks/converted_notebooks/TEST_figuring_out_how_to_control_size_of_pyqtgraph.py index eceda0278..1a988ff38 100755 --- a/notebooks/converted_notebooks/TEST_figuring_out_how_to_control_size_of_pyqtgraph.py +++ b/notebooks/converted_notebooks/TEST_figuring_out_how_to_control_size_of_pyqtgraph.py @@ -131,7 +131,14 @@ def save_state(): imv.setImage(data, xvals=np.linspace(1.0, 3.0, data.shape[0])) ## Set a custom color map -colors = [(0, 0, 0), (45, 5, 61), (84, 42, 55), (150, 87, 60), (208, 171, 141), (255, 255, 255)] +colors = [ + (0, 0, 0), + (45, 5, 61), + (84, 42, 55), + (150, 87, 60), + (208, 171, 141), + (255, 255, 255), +] cmap = pg.ColorMap(pos=np.linspace(0.0, 1.0, 6), color=colors) imv.setColorMap(cmap) diff --git a/notebooks/converted_notebooks/TEST_metadata_ascii_parser_IPTS_20444.py b/notebooks/converted_notebooks/TEST_metadata_ascii_parser_IPTS_20444.py index d60982891..2b254ea05 100755 --- a/notebooks/converted_notebooks/TEST_metadata_ascii_parser_IPTS_20444.py +++ b/notebooks/converted_notebooks/TEST_metadata_ascii_parser_IPTS_20444.py @@ -53,7 +53,11 @@ def select_input_folder(self): def select_input_folder_done(self, folder): self.data_folder = folder - display(HTML('Folder Selected: ' + folder)) + display( + HTML( + 'Folder Selected: ' + folder + ) + ) def select_metadata_file(self): _instruction = "Select Metadata File ..." @@ -66,7 +70,12 @@ def select_metadata_file(self): def select_metadata_file_done(self, metadata_file): self.metadata_file = metadata_file - display(HTML('Metadata File Selected: ' + metadata_file)) + display( + HTML( + 'Metadata File Selected: ' + + metadata_file + ) + ) def retrieve_time_stamp(self): o_retriever = RetrieveTimeStamp(folder=self.data_folder) @@ -85,7 +94,9 @@ def load_metadata(self): # + run_control={"frozen": false, "read_only": false} # o_match = FilenameMetadataMatch(working_dir=system.System.get_working_dir()) -o_match = FilenameMetadataMatch(working_dir="/Volumes/my_book_thunderbolt_duo/IPTS/IPTS-20444-Regina/") +o_match = FilenameMetadataMatch( + working_dir="/Volumes/my_book_thunderbolt_duo/IPTS/IPTS-20444-Regina/" +) o_match.select_input_folder() # + [markdown] run_control={"frozen": false, "read_only": false} diff --git a/notebooks/converted_notebooks/TEST_metadata_overlapping_images.py b/notebooks/converted_notebooks/TEST_metadata_overlapping_images.py index ded8941e1..bc79a7345 100755 --- a/notebooks/converted_notebooks/TEST_metadata_overlapping_images.py +++ b/notebooks/converted_notebooks/TEST_metadata_overlapping_images.py @@ -48,7 +48,8 @@ o_selection.load_files(list_files) o_profile = MetadataOverlappingImagesUi( - working_dir=os.path.dirname(list_files[0]), data_dict=o_selection.data_dict["sample"] + working_dir=os.path.dirname(list_files[0]), + data_dict=o_selection.data_dict["sample"], ) o_profile.show() diff --git a/notebooks/converted_notebooks/TEST_profile.py b/notebooks/converted_notebooks/TEST_profile.py index 44f61097e..bc375aa28 100755 --- a/notebooks/converted_notebooks/TEST_profile.py +++ b/notebooks/converted_notebooks/TEST_profile.py @@ -52,7 +52,10 @@ # # Launch UI # + run_control={"frozen": false, "read_only": false} -o_profile = ProfileUi(working_dir=system.System.get_working_dir(), data_dict=o_selection.data_dict["sample"]) +o_profile = ProfileUi( + working_dir=system.System.get_working_dir(), + data_dict=o_selection.data_dict["sample"], +) o_profile.show() # + [markdown] run_control={"frozen": false, "read_only": false} @@ -81,7 +84,10 @@ o_selection = FileSelection() o_selection.load_files(list_files) -o_profile = ProfileUi(working_dir=os.path.dirname(list_files[0]), data_dict=o_selection.data_dict["sample"]) +o_profile = ProfileUi( + working_dir=os.path.dirname(list_files[0]), + data_dict=o_selection.data_dict["sample"], +) o_profile.show() diff --git a/notebooks/converted_notebooks/TEST_radial_profile.py b/notebooks/converted_notebooks/TEST_radial_profile.py index a04e87fd4..6c656aa25 100755 --- a/notebooks/converted_notebooks/TEST_radial_profile.py +++ b/notebooks/converted_notebooks/TEST_radial_profile.py @@ -34,6 +34,8 @@ o_selection = FileSelection() o_selection.load_files(list_files) -o_select = SelectRadialParameters(working_dir=file_dir, data_dict=o_selection.data_dict["sample"]) +o_select = SelectRadialParameters( + working_dir=file_dir, data_dict=o_selection.data_dict["sample"] +) o_select.show() # - diff --git a/notebooks/converted_notebooks/TEST_scale_overlapping_images.py b/notebooks/converted_notebooks/TEST_scale_overlapping_images.py index 933fcda8e..c2af80f3d 100755 --- a/notebooks/converted_notebooks/TEST_scale_overlapping_images.py +++ b/notebooks/converted_notebooks/TEST_scale_overlapping_images.py @@ -46,7 +46,8 @@ o_selection.load_files(list_files) o_scale = ScaleOverlappingImagesUi( - working_dir=os.path.dirname(list_files[0]), data_dict=o_selection.data_dict["sample"] + working_dir=os.path.dirname(list_files[0]), + data_dict=o_selection.data_dict["sample"], ) o_scale.show() diff --git a/notebooks/converted_notebooks/TEST_time_stamp.py b/notebooks/converted_notebooks/TEST_time_stamp.py index 7defddc90..e09c21f13 100755 --- a/notebooks/converted_notebooks/TEST_time_stamp.py +++ b/notebooks/converted_notebooks/TEST_time_stamp.py @@ -54,7 +54,11 @@ def get_time_stamp(file_name="", ext="tif"): except: time_stamp = o_dict[65000] - time_stamp = MetadataHandler._convert_epics_timestamp_to_rfc3339_timestamp(time_stamp) + time_stamp = ( + MetadataHandler._convert_epics_timestamp_to_rfc3339_timestamp( + time_stamp + ) + ) except: time_stamp = os.path.getctime(file_name) elif ext == "fits": @@ -105,7 +109,10 @@ def _convert_epics_timestamp_to_rfc3339_timestamp(epics_timestamp): for _index, _file in enumerate(list_files): _time_stamp = MetadataHandler.get_time_stamp(file_name=_file, ext="tif") - assert MetadataHandler.convert_to_human_readable_format(_time_stamp) == expected_user_time_stamp[_index] + assert ( + MetadataHandler.convert_to_human_readable_format(_time_stamp) + == expected_user_time_stamp[_index] + ) # Testing the type of the metadata diff --git a/notebooks/converted_notebooks/TEST_water_intake_profile_calculator.py b/notebooks/converted_notebooks/TEST_water_intake_profile_calculator.py index 3fd04cfad..30b2ca2c1 100755 --- a/notebooks/converted_notebooks/TEST_water_intake_profile_calculator.py +++ b/notebooks/converted_notebooks/TEST_water_intake_profile_calculator.py @@ -27,7 +27,10 @@ o_builder = UiBuilder(ui_name="ui_water_intake_profile.ui") from __code import system -from __code.water_intake_profile_calculator import WaterIntakeProfileCalculator, WaterIntakeProfileSelector +from __code.water_intake_profile_calculator import ( + WaterIntakeProfileCalculator, + WaterIntakeProfileSelector, +) system.System.select_working_dir() from __code.__all import custom_style @@ -59,7 +62,10 @@ # + run_control={"frozen": false, "read_only": false} from __code import system -from __code.water_intake_profile_calculator import WaterIntakeProfileCalculator, WaterIntakeProfileSelector +from __code.water_intake_profile_calculator import ( + WaterIntakeProfileCalculator, + WaterIntakeProfileSelector, +) # + run_control={"frozen": false, "read_only": false} # %gui qt diff --git a/notebooks/converted_notebooks/TOPAZ_config_generator.py b/notebooks/converted_notebooks/TOPAZ_config_generator.py index e82e38fda..ce4587e91 100755 --- a/notebooks/converted_notebooks/TOPAZ_config_generator.py +++ b/notebooks/converted_notebooks/TOPAZ_config_generator.py @@ -46,7 +46,10 @@ # # Define Parameters # + run_control={"frozen": false, "read_only": false} -_cfg = TopazConfigGenerator(working_dir=system.System.get_working_dir(), config_dict_loaded=o_config_loader.config_dict) +_cfg = TopazConfigGenerator( + working_dir=system.System.get_working_dir(), + config_dict_loaded=o_config_loader.config_dict, +) # + [markdown] run_control={"frozen": false, "read_only": false} # # Export the Config File diff --git a/notebooks/converted_notebooks/bragg_edge_modeling.py b/notebooks/converted_notebooks/bragg_edge_modeling.py index 5f03b168b..1e29e46d8 100755 --- a/notebooks/converted_notebooks/bragg_edge_modeling.py +++ b/notebooks/converted_notebooks/bragg_edge_modeling.py @@ -117,7 +117,9 @@ def parse_cif_upload(content): plt.plot(wavelengths, inc_el_xs, label="incoherent elastic") plt.plot(wavelengths, inel_xs, label="inelastic") plt.plot(wavelengths, abs_xs, label="absorption") -plt.plot(wavelengths, abs_xs + coh_el_xs + inc_el_xs + inel_xs + inc_inel_xs, label="sum") +plt.plot( + wavelengths, abs_xs + coh_el_xs + inc_el_xs + inel_xs + inc_inel_xs, label="sum" +) plt.legend() # # With texture diff --git a/notebooks/converted_notebooks/bragg_edge_normalization.py b/notebooks/converted_notebooks/bragg_edge_normalization.py index 66fc0dd9a..7882ca894 100755 --- a/notebooks/converted_notebooks/bragg_edge_normalization.py +++ b/notebooks/converted_notebooks/bragg_edge_normalization.py @@ -23,7 +23,9 @@ from __code import system from __code.bragg_edge.bragg_edge_normalization import BraggEdge -system.System.select_working_dir(facility="SNS", instrument="SNAP", notebook="bragg_edge_normalization") +system.System.select_working_dir( + facility="SNS", instrument="SNAP", notebook="bragg_edge_normalization" +) from __code.__all import custom_style custom_style.style() diff --git a/notebooks/converted_notebooks/bragg_edge_normalization_and_profile_extractor.py b/notebooks/converted_notebooks/bragg_edge_normalization_and_profile_extractor.py index b096a7875..8bae5c5d3 100755 --- a/notebooks/converted_notebooks/bragg_edge_normalization_and_profile_extractor.py +++ b/notebooks/converted_notebooks/bragg_edge_normalization_and_profile_extractor.py @@ -97,7 +97,9 @@ # ## Calculate signal of sample region # o_bragg.calculate_counts_vs_file_index_of_regions_selected(list_roi=o_interface.list_roi) -o_bragg.calculate_counts_vs_file_index_of_regions_selected(list_roi=o_interface_sample.roi_selected) +o_bragg.calculate_counts_vs_file_index_of_regions_selected( + list_roi=o_interface_sample.roi_selected +) o_bragg.load_time_spectra() # + [markdown] run_control={"frozen": false, "read_only": false} diff --git a/notebooks/converted_notebooks/bragg_edge_normalized_sample_and_powder.py b/notebooks/converted_notebooks/bragg_edge_normalized_sample_and_powder.py index 3ea7b2716..80e6b8863 100755 --- a/notebooks/converted_notebooks/bragg_edge_normalized_sample_and_powder.py +++ b/notebooks/converted_notebooks/bragg_edge_normalized_sample_and_powder.py @@ -24,7 +24,11 @@ from __code import system from __code.bragg_edge.bragg_edge import BraggEdge, Interface -system.System.select_working_dir(facility="SNS", instrument="SNAP", notebook="bragg_edge_normalized_sample_and_powder") +system.System.select_working_dir( + facility="SNS", + instrument="SNAP", + notebook="bragg_edge_normalized_sample_and_powder", +) from __code.__all import custom_style custom_style.style() @@ -79,7 +83,9 @@ # # Display Bragg Edges vs Signal # + run_control={"frozen": false, "read_only": false} -o_bragg.calculate_counts_vs_file_index_of_regions_selected(list_roi=o_interface.list_roi) +o_bragg.calculate_counts_vs_file_index_of_regions_selected( + list_roi=o_interface.list_roi +) o_bragg.load_time_spectra() o_bragg.plot() # - diff --git a/notebooks/converted_notebooks/bragg_edge_peak_fitting.py b/notebooks/converted_notebooks/bragg_edge_peak_fitting.py index 00009ecc5..03bafb75c 100755 --- a/notebooks/converted_notebooks/bragg_edge_peak_fitting.py +++ b/notebooks/converted_notebooks/bragg_edge_peak_fitting.py @@ -82,7 +82,9 @@ o_bragg = BraggEdge(working_dir=data_path) o_bragg.load_data(data_path) -o_interface = Interface(o_bragg=o_bragg, working_dir=data_path, spectra_file=spectra_file) +o_interface = Interface( + o_bragg=o_bragg, working_dir=data_path, spectra_file=spectra_file +) o_interface.show() # # DEBUGGING using import straight diff --git a/notebooks/converted_notebooks/bragg_edge_peak_fitting_evaluation.py b/notebooks/converted_notebooks/bragg_edge_peak_fitting_evaluation.py index a491e5663..98ae4fd9e 100755 --- a/notebooks/converted_notebooks/bragg_edge_peak_fitting_evaluation.py +++ b/notebooks/converted_notebooks/bragg_edge_peak_fitting_evaluation.py @@ -72,7 +72,10 @@ import os from __code import system -from __code.bragg_edge.peak_fitting_evaluation.bragg_edge_peak_fitting import BraggEdge, Interface +from __code.bragg_edge.peak_fitting_evaluation.bragg_edge_peak_fitting import ( + BraggEdge, + Interface, +) # small data set # data_path = "/Volumes/G-DRIVE/IPTS/VENUS/shared/testing_normalized/" @@ -93,17 +96,24 @@ o_bragg.load_data(data_path) # - -o_interface = Interface(o_bragg=o_bragg, working_dir=data_path, spectra_file=spectra_file) +o_interface = Interface( + o_bragg=o_bragg, working_dir=data_path, spectra_file=spectra_file +) o_interface.show() # # DEBUGGING using import straight from __code import system -from __code.bragg_edge.peak_fitting_evaluation.bragg_edge_peak_fitting import BraggEdge, Interface +from __code.bragg_edge.peak_fitting_evaluation.bragg_edge_peak_fitting import ( + BraggEdge, + Interface, +) # %gui qt -data_path = "/Volumes/G-Drive/IPTS/SNAP/Si_normalized/Si_powder_1_Angs_20C_corrected_normalized" +data_path = ( + "/Volumes/G-Drive/IPTS/SNAP/Si_normalized/Si_powder_1_Angs_20C_corrected_normalized" +) o_interface = Interface(working_dir=data_path) o_interface.show() diff --git a/notebooks/converted_notebooks/bragg_edge_profile.py b/notebooks/converted_notebooks/bragg_edge_profile.py index 19e28058a..c79dd9d83 100755 --- a/notebooks/converted_notebooks/bragg_edge_profile.py +++ b/notebooks/converted_notebooks/bragg_edge_profile.py @@ -24,7 +24,9 @@ from __code.bragg_edge.bragg_edge import Interface from __code.bragg_edge.bragg_edge_normalization import BraggEdge -system.System.select_working_dir(facility="SNS", instrument="VENUS", notebook="bragg_edge_profile") +system.System.select_working_dir( + facility="SNS", instrument="VENUS", notebook="bragg_edge_profile" +) from __code.__all import custom_style custom_style.style() @@ -72,7 +74,9 @@ # ## Calculate # o_bragg.calculate_counts_vs_file_index_of_regions_selected(list_roi=o_interface.list_roi) -o_bragg.calculate_counts_vs_file_index_of_regions_selected(list_roi=o_interface_sample.roi_selected) +o_bragg.calculate_counts_vs_file_index_of_regions_selected( + list_roi=o_interface_sample.roi_selected +) o_bragg.load_time_spectra() # + [markdown] run_control={"frozen": false, "read_only": false} diff --git a/notebooks/converted_notebooks/bragg_edge_raw_sample_and_powder.py b/notebooks/converted_notebooks/bragg_edge_raw_sample_and_powder.py index 0db0f2655..24089e52f 100755 --- a/notebooks/converted_notebooks/bragg_edge_raw_sample_and_powder.py +++ b/notebooks/converted_notebooks/bragg_edge_raw_sample_and_powder.py @@ -25,7 +25,9 @@ from __code.bragg_edge.bragg_edge import Interface from __code.bragg_edge.bragg_edge_raw_sample_and_powder import BraggEdge -system.System.select_working_dir(facility="SNS", instrument="SNAP", notebook="bragg_edge_raw_sample_and_powder") +system.System.select_working_dir( + facility="SNS", instrument="SNAP", notebook="bragg_edge_raw_sample_and_powder" +) from __code.__all import custom_style custom_style.style() diff --git a/notebooks/converted_notebooks/bragg_edge_sample_and_powder.py b/notebooks/converted_notebooks/bragg_edge_sample_and_powder.py index 0044b7174..e5a005036 100755 --- a/notebooks/converted_notebooks/bragg_edge_sample_and_powder.py +++ b/notebooks/converted_notebooks/bragg_edge_sample_and_powder.py @@ -90,7 +90,9 @@ # # Calculate Bragg Edges Data -o_bragg.calculate_counts_vs_file_index_of_regions_selected(list_roi=o_interface.list_roi) +o_bragg.calculate_counts_vs_file_index_of_regions_selected( + list_roi=o_interface.list_roi +) o_bragg.load_time_spectra() # + [markdown] run_control={"frozen": false, "read_only": false} diff --git a/notebooks/converted_notebooks/bragg_edge_signal_vs_powder_peaks.py b/notebooks/converted_notebooks/bragg_edge_signal_vs_powder_peaks.py index 784c9e044..9177e32a6 100755 --- a/notebooks/converted_notebooks/bragg_edge_signal_vs_powder_peaks.py +++ b/notebooks/converted_notebooks/bragg_edge_signal_vs_powder_peaks.py @@ -78,7 +78,9 @@ # # Display Bragg Edges vs Signal # + run_control={"frozen": false, "read_only": false} -o_bragg.calculate_counts_vs_file_index_of_regions_selected(list_roi=o_interface.list_roi) +o_bragg.calculate_counts_vs_file_index_of_regions_selected( + list_roi=o_interface.list_roi +) o_bragg.load_time_spectra() o_bragg.plot() # - diff --git a/notebooks/converted_notebooks/bragg_edge_tof_to_lambda_conversion.py b/notebooks/converted_notebooks/bragg_edge_tof_to_lambda_conversion.py index 0ee34de34..6bdd3f828 100755 --- a/notebooks/converted_notebooks/bragg_edge_tof_to_lambda_conversion.py +++ b/notebooks/converted_notebooks/bragg_edge_tof_to_lambda_conversion.py @@ -80,7 +80,9 @@ _hkl_string.append(_hkl_s) _hkl_formated[_material] = _hkl_string -trace = go.Scatter(x=o_bragg.lambda_array, y=o_bragg.counts_vs_file_index, mode="markers") +trace = go.Scatter( + x=o_bragg.lambda_array, y=o_bragg.counts_vs_file_index, mode="markers" +) layout = go.Layout( width="100%", diff --git a/notebooks/converted_notebooks/calibrated_transmission.py b/notebooks/converted_notebooks/calibrated_transmission.py index 0fb231110..bda49866f 100755 --- a/notebooks/converted_notebooks/calibrated_transmission.py +++ b/notebooks/converted_notebooks/calibrated_transmission.py @@ -23,7 +23,9 @@ # + run_control={"frozen": false, "read_only": false} from __code import system -from __code.calibrated_transmission.calibrated_transmission import CalibratedTransmissionUi +from __code.calibrated_transmission.calibrated_transmission import ( + CalibratedTransmissionUi, +) from __code.ipywe.myfileselector import FileSelection system.System.select_working_dir(notebook="calibrated_transmission") @@ -49,7 +51,8 @@ # + run_control={"frozen": false, "read_only": false} o_calibration = CalibratedTransmissionUi( - working_dir=system.System.get_working_dir(), data_dict=o_selection.data_dict["sample"] + working_dir=system.System.get_working_dir(), + data_dict=o_selection.data_dict["sample"], ) o_calibration.show() diff --git a/notebooks/converted_notebooks/circular_profile_of_a_ring.py b/notebooks/converted_notebooks/circular_profile_of_a_ring.py index 6ac6cd9c5..591ab8e4d 100755 --- a/notebooks/converted_notebooks/circular_profile_of_a_ring.py +++ b/notebooks/converted_notebooks/circular_profile_of_a_ring.py @@ -27,7 +27,9 @@ warnings.filterwarnings("ignore") from __code import system -from __code.circular_profile_of_a_ring.circular_profile_of_a_ring import CircularProfileOfARing +from __code.circular_profile_of_a_ring.circular_profile_of_a_ring import ( + CircularProfileOfARing, +) from __code.circular_profile_of_a_ring.interface_handler import InterfaceHandler system.System.select_working_dir(notebook="circular_profile_of_a_ring") @@ -49,5 +51,7 @@ # # Launch User Interface # + run_control={"frozen": false, "read_only": false} -o_select = InterfaceHandler(working_dir=system.System.get_working_dir(), o_norm=o_selection.o_norm) +o_select = InterfaceHandler( + working_dir=system.System.get_working_dir(), o_norm=o_selection.o_norm +) # - diff --git a/notebooks/converted_notebooks/combine_images_n_by_n.py b/notebooks/converted_notebooks/combine_images_n_by_n.py index 63398546f..afa17c669 100755 --- a/notebooks/converted_notebooks/combine_images_n_by_n.py +++ b/notebooks/converted_notebooks/combine_images_n_by_n.py @@ -21,7 +21,9 @@ # + run_control={"frozen": false, "read_only": false} from __code import system -from __code.combine_images_n_by_n.combine_images_n_by_n import CombineImagesNByN as CombineImages +from __code.combine_images_n_by_n.combine_images_n_by_n import ( + CombineImagesNByN as CombineImages, +) system.System.select_working_dir(notebook="combine_images_n_by_n") from __code.__all import custom_style diff --git a/notebooks/converted_notebooks/combine_images_without_outliers.py b/notebooks/converted_notebooks/combine_images_without_outliers.py index 97da18c72..a4abe30e3 100755 --- a/notebooks/converted_notebooks/combine_images_without_outliers.py +++ b/notebooks/converted_notebooks/combine_images_without_outliers.py @@ -23,7 +23,9 @@ from __code import system from __code.combine_images_without_outliers.combine_images import Interface -system.System.select_working_dir(facility="SNS", instrument="VENUS", notebook="combine_images_without_outliers") +system.System.select_working_dir( + facility="SNS", instrument="VENUS", notebook="combine_images_without_outliers" +) from __code.__all import custom_style custom_style.style() diff --git a/notebooks/converted_notebooks/cylindrical_geometry_correction.py b/notebooks/converted_notebooks/cylindrical_geometry_correction.py index 1b6fbf4ac..a7c85848f 100755 --- a/notebooks/converted_notebooks/cylindrical_geometry_correction.py +++ b/notebooks/converted_notebooks/cylindrical_geometry_correction.py @@ -52,7 +52,10 @@ # # Launch UI # + run_control={"frozen": false, "read_only": false} -o_profile = ProfileUi(working_dir=system.System.get_working_dir(), data_dict=o_selection.data_dict["sample"]) +o_profile = ProfileUi( + working_dir=system.System.get_working_dir(), + data_dict=o_selection.data_dict["sample"], +) o_profile.show() # + run_control={"frozen": false, "read_only": false} diff --git a/notebooks/converted_notebooks/cylindrical_geometry_correction_embedded_widgets.py b/notebooks/converted_notebooks/cylindrical_geometry_correction_embedded_widgets.py index a8fe94c86..900c61619 100755 --- a/notebooks/converted_notebooks/cylindrical_geometry_correction_embedded_widgets.py +++ b/notebooks/converted_notebooks/cylindrical_geometry_correction_embedded_widgets.py @@ -41,9 +41,13 @@ warnings.filterwarnings("ignore") from __code import system -from __code.cylindrical_geometry_correction_embedded_widgets.main import CylindricalGeometryCorrectionEmbeddedWidgets +from __code.cylindrical_geometry_correction_embedded_widgets.main import ( + CylindricalGeometryCorrectionEmbeddedWidgets, +) -system.System.select_working_dir(notebook="cylindrical_geometry_correction_embedded_widgets") +system.System.select_working_dir( + notebook="cylindrical_geometry_correction_embedded_widgets" +) from __code.__all import custom_style @@ -52,7 +56,9 @@ # # Select Images -o_ipts = CylindricalGeometryCorrectionEmbeddedWidgets(working_dir=system.System.get_working_dir()) +o_ipts = CylindricalGeometryCorrectionEmbeddedWidgets( + working_dir=system.System.get_working_dir() +) o_ipts.select_images() # # Use config file (optional) diff --git a/notebooks/converted_notebooks/display_and_export_images_with_metadata_profile.py b/notebooks/converted_notebooks/display_and_export_images_with_metadata_profile.py index 450818d34..5e533c7a7 100755 --- a/notebooks/converted_notebooks/display_and_export_images_with_metadata_profile.py +++ b/notebooks/converted_notebooks/display_and_export_images_with_metadata_profile.py @@ -20,9 +20,13 @@ # + run_control={"frozen": false, "read_only": false} from __code import system -from __code.display_and_export_images_with_metadata_profile import DisplayExportScreenshots +from __code.display_and_export_images_with_metadata_profile import ( + DisplayExportScreenshots, +) -system.System.select_working_dir(notebook="display_and_export_images_with_metadata_profile") +system.System.select_working_dir( + notebook="display_and_export_images_with_metadata_profile" +) from __code.__all import custom_style custom_style.style() diff --git a/notebooks/converted_notebooks/display_file_names_vs_time_stamp.py b/notebooks/converted_notebooks/display_file_names_vs_time_stamp.py index e5d242900..b9cc5c50b 100755 --- a/notebooks/converted_notebooks/display_file_names_vs_time_stamp.py +++ b/notebooks/converted_notebooks/display_file_names_vs_time_stamp.py @@ -35,7 +35,9 @@ # # Select Images # + run_control={"frozen": false, "read_only": false} -o_file_time_stamp = DisplayFileNamesVsTimeStamp(working_dir=system.System.get_working_dir()) +o_file_time_stamp = DisplayFileNamesVsTimeStamp( + working_dir=system.System.get_working_dir() +) o_file_time_stamp.select_image_folder() # + [markdown] run_control={"frozen": false, "read_only": false} diff --git a/notebooks/converted_notebooks/display_integrated_stack_of_images.py b/notebooks/converted_notebooks/display_integrated_stack_of_images.py index d9c45e511..77768859c 100755 --- a/notebooks/converted_notebooks/display_integrated_stack_of_images.py +++ b/notebooks/converted_notebooks/display_integrated_stack_of_images.py @@ -32,7 +32,9 @@ # # Select Input Folder # - -o_integrated = DisplayIntegratedStackOfImages(working_dir=system.System.get_working_dir()) +o_integrated = DisplayIntegratedStackOfImages( + working_dir=system.System.get_working_dir() +) o_integrated.select_input_folder() # + [markdown] run_control={"frozen": false, "read_only": false} diff --git a/notebooks/converted_notebooks/extract_sans_reductionlog_metadata.py b/notebooks/converted_notebooks/extract_sans_reductionlog_metadata.py index b9c3f5749..401a29a9a 100755 --- a/notebooks/converted_notebooks/extract_sans_reductionlog_metadata.py +++ b/notebooks/converted_notebooks/extract_sans_reductionlog_metadata.py @@ -31,7 +31,9 @@ # # Select your ReductionLog files and then the Metadata to extract working_dir = initializer.get_working_dir() -o_extract = extract.Extract(working_dir=working_dir, instrument=initializer.get_instrument()) +o_extract = extract.Extract( + working_dir=working_dir, instrument=initializer.get_instrument() +) o_extract.select_reductionlog() # # Select output folder diff --git a/notebooks/converted_notebooks/file_name_and_metadata_vs_time_stamp.py b/notebooks/converted_notebooks/file_name_and_metadata_vs_time_stamp.py index 54f598b8c..be86bbdc4 100755 --- a/notebooks/converted_notebooks/file_name_and_metadata_vs_time_stamp.py +++ b/notebooks/converted_notebooks/file_name_and_metadata_vs_time_stamp.py @@ -84,7 +84,9 @@ # # Select Image Folder # + run_control={"frozen": false, "read_only": false} -o_meta_file_time = FileNameMetadataTimeStamp(working_dir=system.System.get_working_dir()) +o_meta_file_time = FileNameMetadataTimeStamp( + working_dir=system.System.get_working_dir() +) o_meta_file_time.select_image_folder() # + [markdown] run_control={"frozen": false, "read_only": false} diff --git a/notebooks/converted_notebooks/group_images_by_cycle_for_grating_experiment.py b/notebooks/converted_notebooks/group_images_by_cycle_for_grating_experiment.py index 5068bd06a..6de75dd40 100755 --- a/notebooks/converted_notebooks/group_images_by_cycle_for_grating_experiment.py +++ b/notebooks/converted_notebooks/group_images_by_cycle_for_grating_experiment.py @@ -26,7 +26,9 @@ from __code import system from __code.group_images_by_cycle_for_grating_experiment.group_images import GroupImages -system.System.select_working_dir(notebook="group_images_by_cycle_for_grating_experiment") +system.System.select_working_dir( + notebook="group_images_by_cycle_for_grating_experiment" +) from __code.__all import custom_style custom_style.style() diff --git a/notebooks/converted_notebooks/group_images_by_cycle_for_panoramic_stitching.py b/notebooks/converted_notebooks/group_images_by_cycle_for_panoramic_stitching.py index 537dd775f..d1af4829f 100755 --- a/notebooks/converted_notebooks/group_images_by_cycle_for_panoramic_stitching.py +++ b/notebooks/converted_notebooks/group_images_by_cycle_for_panoramic_stitching.py @@ -20,9 +20,13 @@ # + run_control={"frozen": false, "read_only": false} from __code import system -from __code.group_images_by_cycle_for_panoramic_stitching.group_images import GroupImages +from __code.group_images_by_cycle_for_panoramic_stitching.group_images import ( + GroupImages, +) -system.System.select_working_dir(notebook="group_images_by_cycle_for_panoramic_stitching") +system.System.select_working_dir( + notebook="group_images_by_cycle_for_panoramic_stitching" +) from __code.__all import custom_style custom_style.style() diff --git a/notebooks/converted_notebooks/hfir_reactor_element_analysis.py b/notebooks/converted_notebooks/hfir_reactor_element_analysis.py index 7f5800a84..077c649e3 100755 --- a/notebooks/converted_notebooks/hfir_reactor_element_analysis.py +++ b/notebooks/converted_notebooks/hfir_reactor_element_analysis.py @@ -27,7 +27,9 @@ warnings.filterwarnings("ignore") from __code import system -from __code.hfir_reactor_element_analysis.hfir_reactor_element_analysis import HfirReactorElementAnalysis +from __code.hfir_reactor_element_analysis.hfir_reactor_element_analysis import ( + HfirReactorElementAnalysis, +) from __code.hfir_reactor_element_analysis.interface_handler import InterfaceHandler system.System.select_working_dir(notebook="hfir_reactor_element_analysis") @@ -48,7 +50,9 @@ o_selection.select_ascii_file() # - -o_select = InterfaceHandler(working_dir=system.System.get_working_dir(), o_selection=o_selection) +o_select = InterfaceHandler( + working_dir=system.System.get_working_dir(), o_selection=o_selection +) # # DEBUG @@ -56,7 +60,9 @@ # + import os -from __code.hfir_reactor_element_analysis.hfir_reactor_element_analysis import HfirReactorElementAnalysis +from __code.hfir_reactor_element_analysis.hfir_reactor_element_analysis import ( + HfirReactorElementAnalysis, +) from __code.hfir_reactor_element_analysis.interface_handler import InterfaceHandler ascii_file_name = "/Users/j35/IPTS/HFIR-Reactor/HFIR-Reactor_profiles_top.csv" @@ -69,5 +75,7 @@ o_selection = HfirReactorElementAnalysis(working_dir="/Users/j35/IPTS/HFIR-Reactor/") o_selection.load_ascii(ascii_file_name) -o_select = InterfaceHandler(working_dir="/Users/j35/IPTS/HFIR-Reactor/", o_selection=o_selection) +o_select = InterfaceHandler( + working_dir="/Users/j35/IPTS/HFIR-Reactor/", o_selection=o_selection +) # - diff --git a/notebooks/converted_notebooks/images_and_metadata_extrapolation_matcher.py b/notebooks/converted_notebooks/images_and_metadata_extrapolation_matcher.py index ed387d40d..78f9c28fb 100755 --- a/notebooks/converted_notebooks/images_and_metadata_extrapolation_matcher.py +++ b/notebooks/converted_notebooks/images_and_metadata_extrapolation_matcher.py @@ -20,7 +20,9 @@ # %matplotlib notebook from __code import system -from __code.images_and_metadata_extrapolation_matcher import ImagesAndMetadataExtrapolationMatcher +from __code.images_and_metadata_extrapolation_matcher import ( + ImagesAndMetadataExtrapolationMatcher, +) from __code.select_files_and_folders import SelectAsciiFile, SelectFolder system.System.select_working_dir(notebook="images_and_metadata_extrapolation_matcher") diff --git a/notebooks/converted_notebooks/integrated_roi_counts_vs_file_name_and_time_stamp.py b/notebooks/converted_notebooks/integrated_roi_counts_vs_file_name_and_time_stamp.py index c36f6358f..90c4248d9 100755 --- a/notebooks/converted_notebooks/integrated_roi_counts_vs_file_name_and_time_stamp.py +++ b/notebooks/converted_notebooks/integrated_roi_counts_vs_file_name_and_time_stamp.py @@ -39,7 +39,9 @@ from __code.integrated_roi_counts_vs_file_name_and_time_stamp import IntegratedRoiUi from __code.ipywe.myfileselector import FileSelection -system.System.select_working_dir(notebook="integrated_roi_counts_vs_file_name_and_time_stamp") +system.System.select_working_dir( + notebook="integrated_roi_counts_vs_file_name_and_time_stamp" +) from __code.__all import custom_style custom_style.style() @@ -55,7 +57,10 @@ # # Launch UI # + run_control={"frozen": false, "read_only": false} -o_integration = IntegratedRoiUi(working_dir=system.System.get_working_dir(), data_dict=o_selection.data_dict["sample"]) +o_integration = IntegratedRoiUi( + working_dir=system.System.get_working_dir(), + data_dict=o_selection.data_dict["sample"], +) o_integration.show() # + run_control={"frozen": false, "read_only": false} diff --git a/notebooks/converted_notebooks/match_images_shapes.py b/notebooks/converted_notebooks/match_images_shapes.py index 3dc8c176f..f130c85cd 100755 --- a/notebooks/converted_notebooks/match_images_shapes.py +++ b/notebooks/converted_notebooks/match_images_shapes.py @@ -42,7 +42,9 @@ # # Shapes available o_main = Main( - working_data=o_load.working_data, list_images=o_load.list_images, working_metadata=o_load.working_metadata + working_data=o_load.working_data, + list_images=o_load.list_images, + working_metadata=o_load.working_metadata, ) o_main.display_available_shapes() diff --git a/notebooks/converted_notebooks/metadata_overlapping_images.py b/notebooks/converted_notebooks/metadata_overlapping_images.py index b3d7f656e..2189c6e42 100755 --- a/notebooks/converted_notebooks/metadata_overlapping_images.py +++ b/notebooks/converted_notebooks/metadata_overlapping_images.py @@ -24,7 +24,9 @@ # + run_control={"frozen": false, "read_only": false} from __code import system from __code.ipywe.myfileselector import FileSelection -from __code.metadata_overlapping_images.metadata_overlapping_images import MetadataOverlappingImagesUi +from __code.metadata_overlapping_images.metadata_overlapping_images import ( + MetadataOverlappingImagesUi, +) system.System.select_working_dir(notebook="metadata_overlapping_images") from __code.__all import custom_style @@ -49,7 +51,8 @@ # + run_control={"frozen": false, "read_only": false} o_profile = MetadataOverlappingImagesUi( - working_dir=system.System.get_working_dir(), data_dict=o_selection.data_dict["sample"] + working_dir=system.System.get_working_dir(), + data_dict=o_selection.data_dict["sample"], ) o_profile.show() # - diff --git a/notebooks/converted_notebooks/normalization_with_simplify_selection.py b/notebooks/converted_notebooks/normalization_with_simplify_selection.py index f4a6d0c76..8d5d7de01 100755 --- a/notebooks/converted_notebooks/normalization_with_simplify_selection.py +++ b/notebooks/converted_notebooks/normalization_with_simplify_selection.py @@ -21,7 +21,9 @@ # + run_control={"frozen": false, "read_only": false} from __code import system -from __code.normalization.normalization_with_simplify_selection import NormalizationWithSimplifySelection +from __code.normalization.normalization_with_simplify_selection import ( + NormalizationWithSimplifySelection, +) system.System.select_working_dir(notebook="normalization_with_simplify_selection") from __code.__all import custom_style @@ -39,7 +41,9 @@ # # Change the time range if needed! -o_which = NormalizationWithSimplifySelection(working_dir=system.System.get_working_dir()) +o_which = NormalizationWithSimplifySelection( + working_dir=system.System.get_working_dir() +) o_which.select_sample_folder() # # Normalization workflow summary diff --git a/notebooks/converted_notebooks/overlay_images.py b/notebooks/converted_notebooks/overlay_images.py index 6c540bf2c..00260a216 100755 --- a/notebooks/converted_notebooks/overlay_images.py +++ b/notebooks/converted_notebooks/overlay_images.py @@ -55,6 +55,8 @@ # + run_control={"frozen": false, "read_only": false} o_interface = InterfaceHandler( - o_norm_high_res=o_data.o_norm_high_res, o_norm_low_res=o_data.o_norm_low_res, working_dir=o_data.working_dir + o_norm_high_res=o_data.o_norm_high_res, + o_norm_low_res=o_data.o_norm_low_res, + working_dir=o_data.working_dir, ) # - diff --git a/notebooks/converted_notebooks/panoramic_stitching_for_tof.py b/notebooks/converted_notebooks/panoramic_stitching_for_tof.py index 2ce37885b..389c46299 100755 --- a/notebooks/converted_notebooks/panoramic_stitching_for_tof.py +++ b/notebooks/converted_notebooks/panoramic_stitching_for_tof.py @@ -24,7 +24,9 @@ warnings.filterwarnings("ignore") from __code import system -from __code.panoramic_stitching_for_tof.panoramic_stitching_for_tof import PanoramicStitching +from __code.panoramic_stitching_for_tof.panoramic_stitching_for_tof import ( + PanoramicStitching, +) system.System.select_working_dir(notebook="panoramic_stitching_for_tof") from __code.__all import custom_style diff --git a/notebooks/converted_notebooks/profile.py b/notebooks/converted_notebooks/profile.py index cf123e249..46036d0a2 100755 --- a/notebooks/converted_notebooks/profile.py +++ b/notebooks/converted_notebooks/profile.py @@ -48,7 +48,10 @@ # # Launch UI # + run_control={"frozen": false, "read_only": false} -o_profile = ProfileUi(working_dir=system.System.get_working_dir(), data_dict=o_selection.data_dict["sample"]) +o_profile = ProfileUi( + working_dir=system.System.get_working_dir(), + data_dict=o_selection.data_dict["sample"], +) o_profile.show() # + run_control={"frozen": false, "read_only": false} diff --git a/notebooks/converted_notebooks/radial_profile.py b/notebooks/converted_notebooks/radial_profile.py index 0c949cfee..6484a521d 100755 --- a/notebooks/converted_notebooks/radial_profile.py +++ b/notebooks/converted_notebooks/radial_profile.py @@ -53,7 +53,8 @@ # + run_control={"frozen": false, "read_only": false} o_select = SelectRadialParameters( - working_dir=system.System.get_working_dir(), data_dict=o_selection.data_dict["sample"] + working_dir=system.System.get_working_dir(), + data_dict=o_selection.data_dict["sample"], ) o_select.show() # - diff --git a/notebooks/converted_notebooks/resonance_imaging_experiment_vs_theory.py b/notebooks/converted_notebooks/resonance_imaging_experiment_vs_theory.py index cb772f9b8..a81dbc6e3 100755 --- a/notebooks/converted_notebooks/resonance_imaging_experiment_vs_theory.py +++ b/notebooks/converted_notebooks/resonance_imaging_experiment_vs_theory.py @@ -64,7 +64,10 @@ # + format="tab" run_control={"frozen": false, "read_only": false} input_folder_ui = ipywe.fileselector.FileSelectorPanel( - instruction="Select Input Folder", type="directory", start_dir=working_dir, multiple=False + instruction="Select Input Folder", + type="directory", + start_dir=working_dir, + multiple=False, ) input_folder_ui.show() @@ -89,7 +92,9 @@ # + run_control={"frozen": false, "read_only": false} o_reso = _sample.o_reso -_image = ImageWindow(stack=(o_norm.data["sample"]["data"]), working_folder=working_folder, o_reso=o_reso) +_image = ImageWindow( + stack=(o_norm.data["sample"]["data"]), working_folder=working_folder, o_reso=o_reso +) _image.show() # + [markdown] run_control={"frozen": false, "read_only": false} diff --git a/notebooks/converted_notebooks/rotate_and_crop_images.py b/notebooks/converted_notebooks/rotate_and_crop_images.py index 602d4d127..4903948df 100755 --- a/notebooks/converted_notebooks/rotate_and_crop_images.py +++ b/notebooks/converted_notebooks/rotate_and_crop_images.py @@ -25,7 +25,10 @@ from __code import system from __code.load_images import LoadImages -from __code.rotate_and_crop_images.rotate_and_crop_images import Export, RotateAndCropImages +from __code.rotate_and_crop_images.rotate_and_crop_images import ( + Export, + RotateAndCropImages, +) system.System.select_working_dir(notebook="rotate_and_crop_images") from __code.__all import custom_style @@ -60,7 +63,9 @@ rotation_angle = o_crop.rotation_angle o_output_folder = Export( - working_dir=system.System.get_working_dir(), data_dict=o_crop.rotated_data_dict, rotation_angle=rotation_angle + working_dir=system.System.get_working_dir(), + data_dict=o_crop.rotated_data_dict, + rotation_angle=rotation_angle, ) o_output_folder.select_folder() diff --git a/notebooks/converted_notebooks/scale_overlapping_images.py b/notebooks/converted_notebooks/scale_overlapping_images.py index 405dc867f..52ee803c5 100755 --- a/notebooks/converted_notebooks/scale_overlapping_images.py +++ b/notebooks/converted_notebooks/scale_overlapping_images.py @@ -53,7 +53,10 @@ # # Launch UI # + run_control={"frozen": false, "read_only": false} -o_profile = ProfileUi(working_dir=system.System.get_working_dir(), data_dict=o_selection.data_dict["sample"]) +o_profile = ProfileUi( + working_dir=system.System.get_working_dir(), + data_dict=o_selection.data_dict["sample"], +) o_profile.show() # + run_control={"frozen": false, "read_only": false} diff --git a/notebooks/converted_notebooks/sequential_combine_images_using_metadata.py b/notebooks/converted_notebooks/sequential_combine_images_using_metadata.py index 6941589b7..2daf99dbd 100755 --- a/notebooks/converted_notebooks/sequential_combine_images_using_metadata.py +++ b/notebooks/converted_notebooks/sequential_combine_images_using_metadata.py @@ -20,7 +20,9 @@ # + run_control={"frozen": false, "read_only": false} from __code import system -from __code.sequential_combine_images_using_metadata import SequentialCombineImagesUsingMetadata +from __code.sequential_combine_images_using_metadata import ( + SequentialCombineImagesUsingMetadata, +) system.System.select_working_dir(notebook="sequential_combine_images_using_metadata") from __code.__all import custom_style @@ -31,7 +33,9 @@ # # Select Folder containing all images to merge # + run_control={"frozen": false, "read_only": false} -o_merge = SequentialCombineImagesUsingMetadata(working_dir=system.System.get_working_dir()) +o_merge = SequentialCombineImagesUsingMetadata( + working_dir=system.System.get_working_dir() +) o_merge.select_folder() # - diff --git a/notebooks/converted_notebooks/shifting_time_offset.py b/notebooks/converted_notebooks/shifting_time_offset.py index 0ddc4c39b..909c3dd6f 100755 --- a/notebooks/converted_notebooks/shifting_time_offset.py +++ b/notebooks/converted_notebooks/shifting_time_offset.py @@ -32,7 +32,9 @@ # # Select Folder o_shift = ShiftTimeOffset() -o_select = SelectFolder(system=system, is_input_folder=True, next_function=o_shift.display_counts_vs_time) +o_select = SelectFolder( + system=system, is_input_folder=True, next_function=o_shift.display_counts_vs_time +) # # Repeat on other folders? diff --git a/notebooks/converted_notebooks/timepix3_event_hdf5-he3-detector.py b/notebooks/converted_notebooks/timepix3_event_hdf5-he3-detector.py index 65fdd7865..0aacb8460 100755 --- a/notebooks/converted_notebooks/timepix3_event_hdf5-he3-detector.py +++ b/notebooks/converted_notebooks/timepix3_event_hdf5-he3-detector.py @@ -23,7 +23,9 @@ # + run_control={"frozen": false, "read_only": false} from __code import system -from __code.timepix3_event_hdf5_he3_detector.timepix3_event_hdf5_he3_detector import Timepix3EventHdf5 +from __code.timepix3_event_hdf5_he3_detector.timepix3_event_hdf5_he3_detector import ( + Timepix3EventHdf5, +) system.System.select_working_dir(facility="SNS", instrument="SNAP") from __code.__all import custom_style diff --git a/notebooks/converted_notebooks/timepix3_from_event_to_histo_hdf5.py b/notebooks/converted_notebooks/timepix3_from_event_to_histo_hdf5.py index ae05b1ccf..07a8f62b9 100755 --- a/notebooks/converted_notebooks/timepix3_from_event_to_histo_hdf5.py +++ b/notebooks/converted_notebooks/timepix3_from_event_to_histo_hdf5.py @@ -25,7 +25,9 @@ import numpy as np from __code import system -from __code.timepix3_from_event_to_histo_hdf5.timepix3_from_event_to_histo_hdf5 import Timepix3FromEventToHistoHdf5 +from __code.timepix3_from_event_to_histo_hdf5.timepix3_from_event_to_histo_hdf5 import ( + Timepix3FromEventToHistoHdf5, +) system.System.select_working_dir(facility="SNS", instrument="SNAP") from __code.__all import custom_style diff --git a/notebooks/converted_notebooks/timepix3_histo_hdf5_mcp_detector.py b/notebooks/converted_notebooks/timepix3_histo_hdf5_mcp_detector.py index ecac1d2bc..a5101f31d 100755 --- a/notebooks/converted_notebooks/timepix3_histo_hdf5_mcp_detector.py +++ b/notebooks/converted_notebooks/timepix3_histo_hdf5_mcp_detector.py @@ -22,7 +22,9 @@ # + run_control={"frozen": false, "read_only": false} from __code import system -from __code.timepix3_histo_hdf5_mcp_detector.timepix3_histo_hdf5_mcp_detector import Timepix3HistoHdf5McpDetector +from __code.timepix3_histo_hdf5_mcp_detector.timepix3_histo_hdf5_mcp_detector import ( + Timepix3HistoHdf5McpDetector, +) system.System.select_working_dir(facility="SNS", instrument="SNAP") from __code.__all import custom_style diff --git a/notebooks/converted_notebooks/venus_list_of_nexus_with_pc_above_threshold.py b/notebooks/converted_notebooks/venus_list_of_nexus_with_pc_above_threshold.py index 754fb8dad..b7b1accc9 100755 --- a/notebooks/converted_notebooks/venus_list_of_nexus_with_pc_above_threshold.py +++ b/notebooks/converted_notebooks/venus_list_of_nexus_with_pc_above_threshold.py @@ -23,7 +23,9 @@ # + from __code import system -from __code.venus_list_of_nexus_with_pc_above_threshold.main import VenusNexusListPCAboveThreshold +from __code.venus_list_of_nexus_with_pc_above_threshold.main import ( + VenusNexusListPCAboveThreshold, +) system.System.select_working_dir(facility="SNS", instrument="VENUS") from __code.__all import custom_style diff --git a/notebooks/converted_notebooks/water_intake_profile_calculator.py b/notebooks/converted_notebooks/water_intake_profile_calculator.py index 76368f285..9f5625d22 100755 --- a/notebooks/converted_notebooks/water_intake_profile_calculator.py +++ b/notebooks/converted_notebooks/water_intake_profile_calculator.py @@ -23,7 +23,10 @@ # + run_control={"frozen": false, "read_only": false} from __code import system -from __code.water_intake_profile_calculator import WaterIntakeProfileCalculator, WaterIntakeProfileSelector +from __code.water_intake_profile_calculator import ( + WaterIntakeProfileCalculator, + WaterIntakeProfileSelector, +) system.System.select_working_dir(notebook="water_intake_profile_calculator") from __code.__all import custom_style diff --git a/notebooks/converted_notebooks/wave_front_dynamics.py b/notebooks/converted_notebooks/wave_front_dynamics.py index de2fe0ecf..60998afa9 100755 --- a/notebooks/converted_notebooks/wave_front_dynamics.py +++ b/notebooks/converted_notebooks/wave_front_dynamics.py @@ -26,7 +26,10 @@ # warnings.filterwarnings('ignore') from __code import system -from __code.wave_front_dynamics.wave_front_dynamics import WaveFrontDynamics, WaveFrontDynamicsUI +from __code.wave_front_dynamics.wave_front_dynamics import ( + WaveFrontDynamics, + WaveFrontDynamicsUI, +) system.System.select_working_dir(notebook="wave_front_dynamics") from __code.__all import custom_style @@ -57,6 +60,8 @@ # # Launch User Interface # + run_control={"frozen": false, "read_only": false} -o_ui = WaveFrontDynamicsUI(working_dir=system.System.get_working_dir(), wave_front_dynamics=o_wave) +o_ui = WaveFrontDynamicsUI( + working_dir=system.System.get_working_dir(), wave_front_dynamics=o_wave +) o_ui.show() # - diff --git a/notebooks/tests/_utilities/test_file.py b/notebooks/tests/_utilities/test_file.py index 7bcb0c035..4f3492d70 100755 --- a/notebooks/tests/_utilities/test_file.py +++ b/notebooks/tests/_utilities/test_file.py @@ -6,7 +6,9 @@ class TestFolders: def setup_method(self): data_path = Path(__file__).parent.parent - self.ascii_file_name = str(Path(data_path) / "data" / "ascii" / "bragg_edge_fitting_all_regions.txt") + self.ascii_file_name = str( + Path(data_path) / "data" / "ascii" / "bragg_edge_fitting_all_regions.txt" + ) def test_retrieving_metadata(self): metadata_value_1 = "#base folder" diff --git a/notebooks/tests/_utilities/test_folder.py b/notebooks/tests/_utilities/test_folder.py index bd6cf005b..4edf8b517 100755 --- a/notebooks/tests/_utilities/test_folder.py +++ b/notebooks/tests/_utilities/test_folder.py @@ -16,14 +16,17 @@ def setup_method(self): def test_list_of_folders_to_check_is_list(self): list_of_folders_to_check = "file_name" with pytest.raises(ValueError): - folder.get_list_of_folders_with_specified_file_type(list_of_folders_to_check=list_of_folders_to_check) + folder.get_list_of_folders_with_specified_file_type( + list_of_folders_to_check=list_of_folders_to_check + ) def test_file_extension_is_list(self): list_of_folders_to_check = ["folder1", "folder2"] file_extension = "file extension" with pytest.raises(ValueError): folder.get_list_of_folders_with_specified_file_type( - list_of_folders_to_check=list_of_folders_to_check, file_extension=file_extension + list_of_folders_to_check=list_of_folders_to_check, + file_extension=file_extension, ) def test_folder_rejected_when_do_not_exist(self): diff --git a/notebooks/tests/_utilities/test_list.py b/notebooks/tests/_utilities/test_list.py index 16b6a82b4..5c4c23137 100755 --- a/notebooks/tests/_utilities/test_list.py +++ b/notebooks/tests/_utilities/test_list.py @@ -34,15 +34,21 @@ def test_are_those_two_lists_identical_within_tolerance(): list1 = [1, 2, 3] list2 = [1, 2.1, 3] - assert list.are_those_two_lists_identical_within_tolerance(list1, list2, tolerance=0.2) + assert list.are_those_two_lists_identical_within_tolerance( + list1, list2, tolerance=0.2 + ) list1 = [1, 2, 3] list2 = [1, 2.1, 3] - assert not list.are_those_two_lists_identical_within_tolerance(list1, list2, tolerance=0.1) + assert not list.are_those_two_lists_identical_within_tolerance( + list1, list2, tolerance=0.1 + ) list1 = [1, 2, 3, 4, 5, 6, 7] list2 = [1, 2.1, 3, 4.1, 5, 6.1, 7] - assert list.are_those_two_lists_identical_within_tolerance(list1, list2, tolerance=0.15) + assert list.are_those_two_lists_identical_within_tolerance( + list1, list2, tolerance=0.15 + ) def test_are_those_two_lists_of_lists_identical_within_tolerance(): @@ -53,7 +59,9 @@ def test_are_those_two_lists_of_lists_identical_within_tolerance(): list1 = [[1, 2]] list2 = [[1, 2], [3, 4]] - assert not list.are_those_two_lists_of_lists_identical_within_tolerance(list1, list2) + assert not list.are_those_two_lists_of_lists_identical_within_tolerance( + list1, list2 + ) list1 = [[1, 2], [3, 4]] assert list.are_those_two_lists_of_lists_identical_within_tolerance(list1, list1) @@ -68,11 +76,15 @@ def test_are_those_two_lists_of_lists_identical_within_tolerance(): list1 = [[1, 2.1], [3, 4], [5, 6.1, 7]] list2 = [[1, 2], [3, 4.1], [5, 6, 7]] - assert list.are_those_two_lists_of_lists_identical_within_tolerance(list1, list2, tolerance=0.15) + assert list.are_those_two_lists_of_lists_identical_within_tolerance( + list1, list2, tolerance=0.15 + ) list1 = [[1, 2.1], [3, 4], [5, 6.1, 7]] list2 = [[1, 2], [3, 4.2], [5, 6, 7]] - assert not list.are_those_two_lists_of_lists_identical_within_tolerance(list1, list2, tolerance=0.15) + assert not list.are_those_two_lists_of_lists_identical_within_tolerance( + list1, list2, tolerance=0.15 + ) def test_is_this_list_already_in_those_lists_within_tolerance(): @@ -91,4 +103,6 @@ def test_is_this_list_already_in_those_lists_within_tolerance(): list1 = [10, 25] list2 = [[10, 20], [30, 40]] - assert list.is_this_list_already_in_those_lists_within_tolerance(list1, list2, tolerance=5.1) + assert list.is_this_list_already_in_those_lists_within_tolerance( + list1, list2, tolerance=5.1 + ) diff --git a/notebooks/tests/_utilities/test_math.py b/notebooks/tests/_utilities/test_math.py index 118a5b685..32f837744 100755 --- a/notebooks/tests/_utilities/test_math.py +++ b/notebooks/tests/_utilities/test_math.py @@ -19,6 +19,9 @@ def test_get_distance_needs_right_arguments(self): ) def test_get_distance(self, from_pixel, to_pixel, distance_expected): distance_calculated = math.get_distance_between_two_points( - from_x=from_pixel["x"], from_y=from_pixel["y"], to_x=to_pixel["x"], to_y=to_pixel["y"] + from_x=from_pixel["x"], + from_y=from_pixel["y"], + to_x=to_pixel["x"], + to_y=to_pixel["y"], ) assert distance_calculated == pytest.approx(distance_expected, 1e-2) diff --git a/notebooks/tests/_utilities/test_string.py b/notebooks/tests/_utilities/test_string.py index d64df92d8..07969f1d8 100755 --- a/notebooks/tests/_utilities/test_string.py +++ b/notebooks/tests/_utilities/test_string.py @@ -6,7 +6,9 @@ class TestGetBeginningCommonPartOfStringFromList: def test_no_argument_raises_error(self): with pytest.raises(ValueError): - common_part_returned = string.get_beginning_common_part_of_string_from_list() + common_part_returned = ( + string.get_beginning_common_part_of_string_from_list() + ) def test_get_beginning_common_part_of_string_from_list(self): list_of_string = [ @@ -18,7 +20,9 @@ def test_get_beginning_common_part_of_string_from_list(self): common_part_returned = string.get_beginning_common_part_of_string_from_list( list_of_text=list_of_string, filename_spacer="_" ) - common_part_expected = "/Users/j35/IPTS/IPTS-24959/renamed_files/20202020_Femur_0010_000_000" + common_part_expected = ( + "/Users/j35/IPTS/IPTS-24959/renamed_files/20202020_Femur_0010_000_000" + ) assert common_part_expected == common_part_returned def test_no_match(self): @@ -39,5 +43,7 @@ def test_get_beginning_common_part_of_string_from_list_case2(self): common_part_returned = string.get_beginning_common_part_of_string_from_list( list_of_text=list_of_string, filename_spacer="_" ) - common_part_expected = "/Users/j35/IPTS/IPTS-24959/renamed_files/20202020_Femur_0010_000" + common_part_expected = ( + "/Users/j35/IPTS/IPTS-24959/renamed_files/20202020_Femur_0010_000" + ) assert common_part_expected == common_part_returned diff --git a/notebooks/tests/_utilities/test_time.py b/notebooks/tests/_utilities/test_time.py index f7f8bc27e..1a2222336 100755 --- a/notebooks/tests/_utilities/test_time.py +++ b/notebooks/tests/_utilities/test_time.py @@ -51,13 +51,17 @@ def test_error_raises_if_wrong_unit_provided(self): o_time = time.AbsoluteTimeHandler(initial_absolute_time=self.starting_time) with pytest.raises(NotImplementedError): - o_time.get_absolute_time_for_this_delta_time_array(delta_time_array=[1, 2, 3], units="lightyear") + o_time.get_absolute_time_for_this_delta_time_array( + delta_time_array=[1, 2, 3], units="lightyear" + ) def test_delta_time_array_correctly_formated(self): o_time = time.AbsoluteTimeHandler(initial_absolute_time=self.starting_time) delta_time_array = [1, 2, 3, 4] units = "seconds" - o_time.get_absolute_time_for_this_delta_time_array(delta_time_array=delta_time_array, units=units) + o_time.get_absolute_time_for_this_delta_time_array( + delta_time_array=delta_time_array, units=units + ) delta_time_formated = o_time.delta_time_formated list_hours_calculated = [] @@ -91,7 +95,9 @@ def test_absolute_time_array_correctly_calculated(self): class TestRelativeTimeHandler: def setup_method(self): self.first_file_starting_time = b"2020-01-10T10:15:33.303238667-05:00" - self.second_file_staring_time = b"2020-01-10T10:20:33.303238667-05:00" # 5mn later + self.second_file_staring_time = ( + b"2020-01-10T10:20:33.303238667-05:00" # 5mn later + ) def test_minimum_arguments_provided(self): with pytest.raises(ValueError): @@ -102,35 +108,42 @@ def test_arguments_have_right_format(self): wrong_local_format_time = "2020-01-10Tbad_format_as_well" with pytest.raises(ValueError): time.RelativeTimeHandler( - master_initial_time=wrong_master_format_time, local_initial_time=wrong_local_format_time + master_initial_time=wrong_master_format_time, + local_initial_time=wrong_local_format_time, ) def test_make_sure_local_is_after_master(self): with pytest.raises(ValueError): time.RelativeTimeHandler( - master_initial_time=self.second_file_staring_time, local_initial_time=self.first_file_starting_time + master_initial_time=self.second_file_staring_time, + local_initial_time=self.first_file_starting_time, ) def test_time_offset_calculated_is_right(self): o_time = time.RelativeTimeHandler( - master_initial_time=self.first_file_starting_time, local_initial_time=self.second_file_staring_time + master_initial_time=self.first_file_starting_time, + local_initial_time=self.second_file_staring_time, ) time_offset_calculated = o_time.time_offset_calculated_s assert 300 == time_offset_calculated def test_minimum_arguments_to_get_relative_time_method(self): o_time = time.RelativeTimeHandler( - master_initial_time=self.first_file_starting_time, local_initial_time=self.second_file_staring_time + master_initial_time=self.first_file_starting_time, + local_initial_time=self.second_file_staring_time, ) with pytest.raises(ValueError): o_time.get_relative_time_for_this_time_array() def test_relative_time_array_correctly_calculated(self): o_time = time.RelativeTimeHandler( - master_initial_time=self.first_file_starting_time, local_initial_time=self.second_file_staring_time + master_initial_time=self.first_file_starting_time, + local_initial_time=self.second_file_staring_time, ) time_array = [0, 10, 20, 30, 40] - relative_time_calculated = o_time.get_relative_time_for_this_time_array(time_array=time_array) + relative_time_calculated = o_time.get_relative_time_for_this_time_array( + time_array=time_array + ) relative_time_expected = [300, 310, 320, 330, 340] assert relative_time_calculated == relative_time_expected diff --git a/notebooks/tests/group_images_by_cycle_for_panoramic_stitching/test_group_images_by_cycle.py b/notebooks/tests/group_images_by_cycle_for_panoramic_stitching/test_group_images_by_cycle.py index 0027ff268..3c55c4f2e 100755 --- a/notebooks/tests/group_images_by_cycle_for_panoramic_stitching/test_group_images_by_cycle.py +++ b/notebooks/tests/group_images_by_cycle_for_panoramic_stitching/test_group_images_by_cycle.py @@ -1,7 +1,9 @@ import glob from pathlib import Path -from notebooks.__code.group_images_by_cycle_for_panoramic_stitching.group_images_by_cycle import GroupImagesByCycle +from notebooks.__code.group_images_by_cycle_for_panoramic_stitching.group_images_by_cycle import ( + GroupImagesByCycle, +) class TestGroupImagesByCycle: @@ -14,7 +16,9 @@ def setup_method(self): list_of_files.sort() self.list_of_files = list_of_files - full_tiff_path = Path(data_path) / "data" / "images" / "data_with_acquisition_cycle" + full_tiff_path = ( + Path(data_path) / "data" / "images" / "data_with_acquisition_cycle" + ) full_list_of_files = glob.glob(str(full_tiff_path) + "/*.tif") full_list_of_files.sort() self.full_list_of_files = full_list_of_files @@ -22,7 +26,10 @@ def setup_method(self): self.list_of_metadata_key = [65045, 65041] def test_create_master_dictionary(self): - o_group = GroupImagesByCycle(list_of_files=self.list_of_files, list_of_metadata_key=self.list_of_metadata_key) + o_group = GroupImagesByCycle( + list_of_files=self.list_of_files, + list_of_metadata_key=self.list_of_metadata_key, + ) o_group.create_master_dictionary() dict_expected = { @@ -74,7 +81,8 @@ def test_create_master_dictionary(self): def test_group_dictionary(self): o_group = GroupImagesByCycle( - list_of_files=self.full_list_of_files, list_of_metadata_key=self.list_of_metadata_key + list_of_files=self.full_list_of_files, + list_of_metadata_key=self.list_of_metadata_key, ) o_group.create_master_dictionary() o_group.group() @@ -84,11 +92,15 @@ def test_group_dictionary(self): expected_list_group0 = self.full_list_of_files[:9] assert len(o_group.dictionary_of_groups[0]) == len(expected_list_group0) - for _file_returned, _file_expected in zip(o_group.dictionary_of_groups[0], expected_list_group0, strict=False): + for _file_returned, _file_expected in zip( + o_group.dictionary_of_groups[0], expected_list_group0, strict=False + ): assert _file_expected == _file_returned expected_list_group1 = self.full_list_of_files[9:18] assert len(o_group.dictionary_of_groups[1]) == len(expected_list_group1) - for _file_returned, _file_expected in zip(o_group.dictionary_of_groups[1], expected_list_group1, strict=False): + for _file_returned, _file_expected in zip( + o_group.dictionary_of_groups[1], expected_list_group1, strict=False + ): assert _file_expected == _file_returned diff --git a/notebooks/tests/group_images_by_cycle_for_panoramic_stitching/test_sort_images_within_cycles.py b/notebooks/tests/group_images_by_cycle_for_panoramic_stitching/test_sort_images_within_cycles.py index 1c8bdea12..31bbf7a32 100755 --- a/notebooks/tests/group_images_by_cycle_for_panoramic_stitching/test_sort_images_within_cycles.py +++ b/notebooks/tests/group_images_by_cycle_for_panoramic_stitching/test_sort_images_within_cycles.py @@ -1,7 +1,9 @@ import glob from pathlib import Path -from notebooks.__code.group_images_by_cycle_for_panoramic_stitching.group_images_by_cycle import GroupImagesByCycle +from notebooks.__code.group_images_by_cycle_for_panoramic_stitching.group_images_by_cycle import ( + GroupImagesByCycle, +) from notebooks.__code.group_images_by_cycle_for_panoramic_stitching.sort_images_within_each_cycle import ( SortImagesWithinEachCycle, ) @@ -16,7 +18,9 @@ def setup_method(self): list_of_files.sort() self.list_of_files = list_of_files - full_tiff_path = Path(data_path) / "data" / "images" / "data_with_acquisition_cycle" + full_tiff_path = ( + Path(data_path) / "data" / "images" / "data_with_acquisition_cycle" + ) full_list_of_files = glob.glob(str(full_tiff_path) + "/*.tif") full_list_of_files.sort() self.full_list_of_files = full_list_of_files @@ -25,7 +29,8 @@ def setup_method(self): def test_sort_dictionary(self): o_group = GroupImagesByCycle( - list_of_files=self.full_list_of_files, list_of_metadata_key=self.list_of_metadata_key + list_of_files=self.full_list_of_files, + list_of_metadata_key=self.list_of_metadata_key, ) o_group.run() @@ -38,7 +43,8 @@ def test_sort_dictionary(self): } o_sort = SortImagesWithinEachCycle( - dict_groups_filename=dictionary_of_groups, dict_filename_metadata=dictionary_of_filename_metadata + dict_groups_filename=dictionary_of_groups, + dict_filename_metadata=dictionary_of_filename_metadata, ) o_sort.sort(dict_how_to_sort=dict_how_to_sort) @@ -47,5 +53,7 @@ def test_sort_dictionary(self): index_expected = [4, 5, 6, 3, 8, 7, 2, 1, 0] group0_sorted_expected = [raw_group0[_index] for _index in index_expected] - for _calculated, _expected in zip(group0_sorted_calculated, group0_sorted_expected, strict=False): + for _calculated, _expected in zip( + group0_sorted_calculated, group0_sorted_expected, strict=False + ): assert _calculated == _expected diff --git a/notebooks/tests/test_combine_images_without_outliners.py b/notebooks/tests/test_combine_images_without_outliners.py index 5841416ce..890141b60 100755 --- a/notebooks/tests/test_combine_images_without_outliners.py +++ b/notebooks/tests/test_combine_images_without_outliners.py @@ -1,7 +1,9 @@ import numpy as np import pytest -from notebooks.__code.combine_images_without_outliers.combine_images import CombineImagesAlgorithm +from notebooks.__code.combine_images_without_outliers.combine_images import ( + CombineImagesAlgorithm, +) class TestCombineImagesWithOutliners: @@ -17,7 +19,9 @@ def test_raises_error_if_less_than_3_arrays(self): def test_make_sure_arrays_have_the_same_size(self): list_array = [np.arange(10), np.arange(9), np.arange(9)] with pytest.raises(ValueError): - CombineImagesAlgorithm.check_arrays_have_same_size_and_dimensions(list_array=list_array) + CombineImagesAlgorithm.check_arrays_have_same_size_and_dimensions( + list_array=list_array + ) def test_make_sure_arrays_have_the_same_dimensions(self): list_array = [ @@ -26,13 +30,17 @@ def test_make_sure_arrays_have_the_same_dimensions(self): np.array([[1, 2, 3], [1, 2]], dtype=object), ] with pytest.raises(ValueError): - CombineImagesAlgorithm.check_arrays_have_same_size_and_dimensions(list_array=list_array) + CombineImagesAlgorithm.check_arrays_have_same_size_and_dimensions( + list_array=list_array + ) def test_mean_without_outliers_3_arrays(self): array1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) array2 = np.array([[5, 5, 5], [5, 5, 5], [5, 5, 5]]) array3 = np.array([[10, 2, 5], [10, 10, 10], [3, 3, 3]]) - mean_array_calculated = CombineImagesAlgorithm.mean_without_outliers(list_array=[array1, array2, array3]) + mean_array_calculated = CombineImagesAlgorithm.mean_without_outliers( + list_array=[array1, array2, array3] + ) mean_array_expected = np.array([[5, 2, 5], [5, 5, 6], [5, 5, 5]]) [nbr_row, nbr_col] = np.shape(mean_array_expected) for _row in np.arange(nbr_row): diff --git a/notebooks/tests/test_file_handler.py b/notebooks/tests/test_file_handler.py index 041c8724c..673e9e2b8 100755 --- a/notebooks/tests/test_file_handler.py +++ b/notebooks/tests/test_file_handler.py @@ -10,7 +10,9 @@ class TestReadBraggEdgeFittingAsciiFormat: def setup_method(self): data_path = Path(__file__).parent self.data_path = data_path - self.ascii_file_name = str(Path(data_path) / "data" / "ascii" / "bragg_edge_fitting_all_regions.txt") + self.ascii_file_name = str( + Path(data_path) / "data" / "ascii" / "bragg_edge_fitting_all_regions.txt" + ) def test_file_does_not_exist(self): ascii_file = "does_not_exist" @@ -18,7 +20,9 @@ def test_file_does_not_exist(self): read_bragg_edge_fitting_ascii_format(full_file_name=ascii_file) def test_retrieving_metadata_columns(self): - result = read_bragg_edge_fitting_ascii_format(full_file_name=self.ascii_file_name) + result = read_bragg_edge_fitting_ascii_format( + full_file_name=self.ascii_file_name + ) metadata_column = result["metadata"]["columns"] assert metadata_column["3"] == { @@ -62,18 +66,26 @@ def test_retrieving_metadata_columns(self): assert len(metadata_column) == 86 def test_retrieving_data(self): - result = read_bragg_edge_fitting_ascii_format(full_file_name=self.ascii_file_name) + result = read_bragg_edge_fitting_ascii_format( + full_file_name=self.ascii_file_name + ) pd_data = result["data"] data_col3 = np.array(pd_data["3"]) print(data_col3[0:4]) - assert np.allclose(data_col3[0:4], [0.00078657, 0.00066687, 0.00100886, 0.00068397], atol=1e-5) + assert np.allclose( + data_col3[0:4], [0.00078657, 0.00066687, 0.00100886, 0.00068397], atol=1e-5 + ) def test_retrieving_xaxis(self): - result = read_bragg_edge_fitting_ascii_format(full_file_name=self.ascii_file_name) + result = read_bragg_edge_fitting_ascii_format( + full_file_name=self.ascii_file_name + ) pd_data = result["data"] data_tof = np.array(pd_data["tof"]) assert np.allclose(data_tof[0:4], [0.96, 11.2, 21.44, 31.68]) data_lambda = np.array(pd_data["lambda"]) - assert np.allclose(data_lambda[0:4], [0.0197830, 0.01981425, 0.019845414, 0.01987658]) + assert np.allclose( + data_lambda[0:4], [0.0197830, 0.01981425, 0.019845414, 0.01987658] + ) data_index = np.array(pd_data["index"]) assert np.all(data_index[0:4] == [0, 1, 2, 3]) diff --git a/notebooks/tests/test_interpolation_utilities.py b/notebooks/tests/test_interpolation_utilities.py index 42a8bb2d4..7e517143d 100755 --- a/notebooks/tests/test_interpolation_utilities.py +++ b/notebooks/tests/test_interpolation_utilities.py @@ -28,18 +28,53 @@ def test_raises_error_if_no_new_array(self): @pytest.mark.parametrize( "x_axis, y_axis, new_x_axis, new_y_axis", [ - (np.arange(10), np.arange(10) + 5, np.arange(0, 10, 2), np.arange(0, 10, 2) + 5), + ( + np.arange(10), + np.arange(10) + 5, + np.arange(0, 10, 2), + np.arange(0, 10, 2) + 5, + ), ( np.arange(10), np.arange(10) + 5, np.arange(0.3, 9, 0.6), - [5.3, 5.9, 6.5, 7.1, 7.7, 8.3, 8.9, 9.5, 10.1, 10.7, 11.3, 11.9, 12.5, 13.1], + [ + 5.3, + 5.9, + 6.5, + 7.1, + 7.7, + 8.3, + 8.9, + 9.5, + 10.1, + 10.7, + 11.3, + 11.9, + 12.5, + 13.1, + ], ), ( np.arange(10) + 0.1, np.arange(10) + 5, np.arange(0.2, 9, 0.5), - [5.1, 5.6, 6.1, 6.6, 7.1, 7.6, 8.1, 8.6, 9.1, 9.6, 10.1, 10.6, 11.1, 11.6], + [ + 5.1, + 5.6, + 6.1, + 6.6, + 7.1, + 7.6, + 8.1, + 8.6, + 9.1, + 9.6, + 10.1, + 10.6, + 11.1, + 11.6, + ], ), ], ) @@ -47,7 +82,12 @@ def test_right_new_x_axis_generated(self, x_axis, y_axis, new_x_axis, new_y_axis o_interpolation = Interpolation(x_axis=x_axis, y_axis=y_axis) y_axis_calculated = o_interpolation.get_new_y_array(new_x_axis=new_x_axis) - assert all([abs(a - b) < EPS for a, b in zip(new_y_axis, y_axis_calculated, strict=False)]) + assert all( + [ + abs(a - b) < EPS + for a, b in zip(new_y_axis, y_axis_calculated, strict=False) + ] + ) def test_not_enough_x_axis_data_raises_error(self): x_axis = [0, 80, 81] @@ -57,7 +97,9 @@ def test_not_enough_x_axis_data_raises_error(self): with pytest.raises(TypeError): o_interpolation.get_new_y_array(new_x_axis=new_x_axis) - @pytest.mark.parametrize("new_x_axis", [np.arange(-1, 2), np.arange(2, 15), np.arange(-3, 17)]) + @pytest.mark.parametrize( + "new_x_axis", [np.arange(-1, 2), np.arange(2, 15), np.arange(-3, 17)] + ) def test_new_x_axis_within_old_x_range(self, new_x_axis): x_axis = np.arange(10) y_axis = np.arange(10) + 5 diff --git a/notebooks/tests/test_metadata_handler.py b/notebooks/tests/test_metadata_handler.py index 8629c9edc..3774b9498 100755 --- a/notebooks/tests/test_metadata_handler.py +++ b/notebooks/tests/test_metadata_handler.py @@ -30,6 +30,8 @@ def test_correct_full_dict_returned(self): def test_correct_requested_metadata_dict_returned(self): list_key = [65062, 65063] list_files = glob.glob(str(self.tiff_path) + "/*.tif") - dict_returned = MetadataHandler.retrieve_value_of_metadata_key(list_files=list_files, list_key=list_key) + dict_returned = MetadataHandler.retrieve_value_of_metadata_key( + list_files=list_files, list_key=list_key + ) assert dict_returned[list_files[0]][65062] == "MotAperture.RBV:220.000000" assert dict_returned[list_files[1]][65062] == "MotAperture.RBV:220.000000" diff --git a/notebooks/tests/test_nexus_handler.py b/notebooks/tests/test_nexus_handler.py index 1e2b56ace..58b246699 100755 --- a/notebooks/tests/test_nexus_handler.py +++ b/notebooks/tests/test_nexus_handler.py @@ -28,7 +28,8 @@ def test_raising_error_if_wrong_entry(self): """make sure error is raised if entry is wrong""" with pytest.raises(KeyError): _ = nexus_handler.get_list_entries( - nexus_file_name=self.nexus_file_name, starting_entries=["entry_does_not_exist"] + nexus_file_name=self.nexus_file_name, + starting_entries=["entry_does_not_exist"], ) def test_data_retrieved(self): @@ -37,7 +38,12 @@ def test_data_retrieved(self): nexus_file_name=self.nexus_file_name, starting_entries=["entry", "daslogs"] ) - assert result["pv1"] == ["average_value", "maximum_value", "minimum_value", "value"] + assert result["pv1"] == [ + "average_value", + "maximum_value", + "minimum_value", + "value", + ] class TestGetEntryValue: @@ -58,9 +64,13 @@ def test_raise_error_if_file_do_not_exist(self): def test_raising_attribute_error_if_wrong_entry(self): entry_path = ["wrong", "path"] with pytest.raises(AttributeError): - _ = nexus_handler.get_entry_value(nexus_file_name=self.nexus_file_name, entry_path=entry_path) + _ = nexus_handler.get_entry_value( + nexus_file_name=self.nexus_file_name, entry_path=entry_path + ) def test_data_retrieved(self): entry_path = ["entry", "daslogs", "pv1", "value"] - result = nexus_handler.get_entry_value(nexus_file_name=self.nexus_file_name, entry_path=entry_path) + result = nexus_handler.get_entry_value( + nexus_file_name=self.nexus_file_name, entry_path=entry_path + ) assert result == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] diff --git a/notebooks/tests/test_selection_region_utilities.py b/notebooks/tests/test_selection_region_utilities.py index 2db3c509e..f45dceed5 100755 --- a/notebooks/tests/test_selection_region_utilities.py +++ b/notebooks/tests/test_selection_region_utilities.py @@ -21,9 +21,11 @@ def test_correctly_initialized(self): (5, 10, 5, 1, 6, 10, 3, 1), ], ) - def test_next_doll_region(self, x0, y0, width, height, new_x0, new_y0, new_width, new_height): - x0_returned, y0_returned, width_returned, height_returned = SelectionRegionUtilities.produce_next_doll_region( - x0, y0, width, height + def test_next_doll_region( + self, x0, y0, width, height, new_x0, new_y0, new_width, new_height + ): + x0_returned, y0_returned, width_returned, height_returned = ( + SelectionRegionUtilities.produce_next_doll_region(x0, y0, width, height) ) assert x0_returned == new_x0 assert y0_returned == new_y0 diff --git a/notebooks/users_notebooks/converted_notebooks/golden_ratio_table_scan_generator.py b/notebooks/users_notebooks/converted_notebooks/golden_ratio_table_scan_generator.py index 1e064b2df..38894809e 100755 --- a/notebooks/users_notebooks/converted_notebooks/golden_ratio_table_scan_generator.py +++ b/notebooks/users_notebooks/converted_notebooks/golden_ratio_table_scan_generator.py @@ -67,7 +67,9 @@ def make_ascii_file(data=[], output_file_name=""): for angle in angle_values: angle_value_as_list = angle.split(".") - angle_values_as_list_padded = [f"{int(_value):03d}" for _value in angle_value_as_list] + angle_values_as_list_padded = [ + f"{int(_value):03d}" for _value in angle_value_as_list + ] angle_value = "_".join(angle_values_as_list_padded) for pro in np.arange(nbr_projections_per_angle): file_name = f"image_{angle_value}" diff --git a/notebooks/users_notebooks/ipts_27939_code/ipts_27939.py b/notebooks/users_notebooks/ipts_27939_code/ipts_27939.py index 68847b50e..4a3dee55a 100755 --- a/notebooks/users_notebooks/ipts_27939_code/ipts_27939.py +++ b/notebooks/users_notebooks/ipts_27939_code/ipts_27939.py @@ -61,7 +61,9 @@ def select_images(self): list_of_images = glob.glob(os.path.join(self.working_dir, "*.tif*")) self.load_images(list_of_images=list_of_images) else: - file_folder_browser = FileFolderBrowser(working_dir=self.working_dir, next_function=self.load_images) + file_folder_browser = FileFolderBrowser( + working_dir=self.working_dir, next_function=self.load_images + ) file_folder_browser.select_images(filters={"TIFF": "*.tif?"}) def load_images(self, list_of_images): @@ -81,7 +83,11 @@ def load_images(self, list_of_images): if self.data: [self.height, self.width] = np.shape(np.squeeze(self.data[0])) - display(HTML("Number of images loaded: " + str(len(list_of_images)) + "")) + display( + HTML( + "Number of images loaded: " + str(len(list_of_images)) + "" + ) + ) def visualize_raw_images(self): fig, ax1 = plt.subplots(num="Raw Images") @@ -92,7 +98,12 @@ def plot(image_index): v = interactive( plot, - image_index=widgets.IntSlider(min=0, max=len(self.data) - 1, value=0, layout=widgets.Layout(width="50%")), + image_index=widgets.IntSlider( + min=0, + max=len(self.data) - 1, + value=0, + layout=widgets.Layout(width="50%"), + ), ) display(v) @@ -134,8 +145,12 @@ def plot(rot_value, image_index, vert_guide, profile1_h, profile2_h): # color='g', # linestyle="--") - profile1 = data[profile1_h, vert_guide - profile_margin : vert_guide + profile_margin] - profile2 = data[profile2_h, vert_guide - profile_margin : vert_guide + profile_margin] + profile1 = data[ + profile1_h, vert_guide - profile_margin : vert_guide + profile_margin + ] + profile2 = data[ + profile2_h, vert_guide - profile_margin : vert_guide + profile_margin + ] ax1.cla() ax1.plot(profile1, "b", label="profile 1") @@ -148,9 +163,17 @@ def plot(rot_value, image_index, vert_guide, profile1_h, profile2_h): self.v = interactive( plot, rot_value=widgets.FloatSlider( - min=-5.0, max=5.0, value=default_rotate_angle, layout=widgets.Layout(width="50%") + min=-5.0, + max=5.0, + value=default_rotate_angle, + layout=widgets.Layout(width="50%"), + ), + image_index=widgets.IntSlider( + min=0, + max=len(self.data) - 1, + value=0, + layout=widgets.Layout(width="50%"), ), - image_index=widgets.IntSlider(min=0, max=len(self.data) - 1, value=0, layout=widgets.Layout(width="50%")), vert_guide=widgets.IntSlider( min=0, max=self.width - 1, @@ -206,12 +229,24 @@ def plot(image_index, left, right, top, bottom, profile_mker): self.crop_ui = interactive( plot, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), - left=widgets.IntSlider(min=0, max=width - 1, value=self.config["default_crop"]["x0"]), - right=widgets.IntSlider(min=0, max=width - 1, value=self.config["default_crop"]["x1"]), - top=widgets.IntSlider(min=0, max=height - 1, value=self.config["default_crop"]["y0"]), - bottom=widgets.IntSlider(min=0, max=height - 1, value=self.config["default_crop"]["y1"]), - profile_mker=widgets.IntSlider(min=0, max=height - 1, value=self.config["default_crop"]["marker"]), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), + left=widgets.IntSlider( + min=0, max=width - 1, value=self.config["default_crop"]["x0"] + ), + right=widgets.IntSlider( + min=0, max=width - 1, value=self.config["default_crop"]["x1"] + ), + top=widgets.IntSlider( + min=0, max=height - 1, value=self.config["default_crop"]["y0"] + ), + bottom=widgets.IntSlider( + min=0, max=height - 1, value=self.config["default_crop"]["y1"] + ), + profile_mker=widgets.IntSlider( + min=0, max=height - 1, value=self.config["default_crop"]["marker"] + ), ) display(self.crop_ui) @@ -229,7 +264,12 @@ def plot(image_index): ax1.imshow(data, vmin=0, vmax=1) # plt.tight_layout() - v = interactive(plot, image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0)) + v = interactive( + plot, + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), + ) display(v) def export_cropped_images(self): @@ -247,7 +287,9 @@ def export_cropped_images(self): def export_cropped_images_step2(self, output_folder): output_folder = os.path.abspath(output_folder) working_dir = self.working_dir - base_working_dir = os.path.join(output_folder, os.path.basename(working_dir) + "_cropped") + base_working_dir = os.path.join( + output_folder, os.path.basename(working_dir) + "_cropped" + ) base_working_dir = make_or_increment_folder_name(base_working_dir) list_images_corrected = self.cropped_data @@ -264,7 +306,13 @@ def export_cropped_images_step2(self, output_folder): progress_bar.value = index + 1 progress_bar.close() - display(HTML('' + str(nbr_images) + " images created!")) + display( + HTML( + '' + + str(nbr_images) + + " images created!" + ) + ) def background_range_selection(self): fig, ax1 = plt.subplots(num="Select top and bottom of background range") @@ -283,7 +331,9 @@ def plot(image_index, top, bottom): self.background_limit_ui = interactive( plot, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), top=widgets.IntSlider(min=0, max=height - 1, value=default_top), bottom=widgets.IntSlider(min=0, max=height - 1, value=default_bottom), ) @@ -307,7 +357,9 @@ def plot(image_index, top, bottom): self.sample_limit_ui = interactive( plot, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), top=widgets.IntSlider(min=0, max=height - 1, value=default_top), bottom=widgets.IntSlider(min=0, max=height - 1, value=default_bottom), ) @@ -321,13 +373,16 @@ def remove_background_signal(self): y0_background = self.background_limit_ui.children[1].value y1_background = self.background_limit_ui.children[2].value background_signal_integrated = [ - np.mean(_data[y0_background : y1_background + 1, :], axis=0) for _data in self.cropped_data + np.mean(_data[y0_background : y1_background + 1, :], axis=0) + for _data in self.cropped_data ] y0_sample = self.sample_limit_ui.children[1].value y1_sample = self.sample_limit_ui.children[2].value sample_without_background = [] - for _background, _sample in zip(background_signal_integrated, self.cropped_data, strict=False): + for _background, _sample in zip( + background_signal_integrated, self.cropped_data, strict=False + ): _data = _sample[y0_sample : y1_sample + 1] sample_without_background.append(np.abs(_data - _background)) @@ -340,7 +395,10 @@ def plot(image_index): ax1.imshow(self.sample_without_background[image_index], vmin=0, vmax=1) self.sample_no_background_ui = interactive( - plot, image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0) + plot, + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), ) display(self.sample_no_background_ui) @@ -364,7 +422,9 @@ def plot(image_index, profile_height): v = interactive( plot, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), profile_height=widgets.IntSlider(min=0, max=height - 1, value=0), ) display(v) @@ -391,8 +451,10 @@ def correct_cylinder_geometry(self): expected_array = [] for x_index, x in enumerate(profile): measure = x - number_of_pixels_through_thickness = number_of_pixels_at_that_position1( - position=x_index, radius=radius + number_of_pixels_through_thickness = ( + number_of_pixels_at_that_position1( + position=x_index, radius=radius + ) ) number_of_pixels.append(number_of_pixels_through_thickness) expected_array.append(measure / number_of_pixels_through_thickness) @@ -417,22 +479,32 @@ def plot(image_index, index1, index2, plot_max): self.sample_corrected = interactive( plot, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), - index1=widgets.IntSlider(min=0, max=height - 1, value=int((height - 1) / 3)), - index2=widgets.IntSlider(min=0, max=height - 1, value=2 * int((height - 1) / 3)), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), + index1=widgets.IntSlider( + min=0, max=height - 1, value=int((height - 1) / 3) + ), + index2=widgets.IntSlider( + min=0, max=height - 1, value=2 * int((height - 1) / 3) + ), plot_max=widgets.FloatSlider(min=1e-5, max=1.0, step=0.001, value=0.02), ) display(self.sample_corrected) def export_profiles(self): working_dir = os.path.dirname(self.working_dir) - output_folder_browser = FileFolderBrowser(working_dir=working_dir, next_function=self.export) + output_folder_browser = FileFolderBrowser( + working_dir=working_dir, next_function=self.export + ) output_folder_browser.select_output_folder() def export(self, output_folder): output_folder = os.path.abspath(output_folder) working_dir = self.working_dir - base_working_dir = os.path.join(output_folder, os.path.basename(working_dir) + "_cylindrical_geo_corrected") + base_working_dir = os.path.join( + output_folder, os.path.basename(working_dir) + "_cylindrical_geo_corrected" + ) base_working_dir = make_or_increment_folder_name(base_working_dir) # export images @@ -450,7 +522,13 @@ def export(self, output_folder): progress_bar.value = index + 1 progress_bar.close() - display(HTML('' + str(nbr_images) + " images created!")) + display( + HTML( + '' + + str(nbr_images) + + " images created!" + ) + ) # export profiles @@ -473,8 +551,12 @@ def export(self, output_folder): for index, image in enumerate(list_images_corrected): _name = os.path.basename(list_of_images[index]) base_name_without_suffix = PurePosixPath(_name).stem - base_name_of_ascii_file = str(base_name_without_suffix) + "_profile_corrected.csv" - full_name_of_ascii_file = os.path.join(base_working_dir, base_name_of_ascii_file) + base_name_of_ascii_file = ( + str(base_name_without_suffix) + "_profile_corrected.csv" + ) + full_name_of_ascii_file = os.path.join( + base_working_dir, base_name_of_ascii_file + ) df = pd.DataFrame(image) df.to_csv(full_name_of_ascii_file) @@ -483,11 +565,27 @@ def export(self, output_folder): progress_bar.close() - display(HTML('' + str(nbr_images) + " ASCII files created!")) + display( + HTML( + '' + + str(nbr_images) + + " ASCII files created!" + ) + ) json_file_name = os.path.join(base_working_dir, "metadata.json") with open(json_file_name, "w") as outfile: json.dump(metadata, outfile) - display(HTML(' metadata json file created (metadata.json)!')) + display( + HTML( + ' metadata json file created (metadata.json)!' + ) + ) - display(HTML(' Output folder: ' + base_working_dir + "!")) + display( + HTML( + ' Output folder: ' + + base_working_dir + + "!" + ) + ) diff --git a/notebooks/users_notebooks/ipts_28402_code/ipts_28402.py b/notebooks/users_notebooks/ipts_28402_code/ipts_28402.py index 9ead3a5d4..8135a5df0 100755 --- a/notebooks/users_notebooks/ipts_28402_code/ipts_28402.py +++ b/notebooks/users_notebooks/ipts_28402_code/ipts_28402.py @@ -21,7 +21,10 @@ from __code._utilities.folder import make_folder from __code.file_folder_browser import FileFolderBrowser -from .cylindrical_geometry_correction import number_of_pixel_at_that_position2, number_of_pixels_at_that_position1 +from .cylindrical_geometry_correction import ( + number_of_pixel_at_that_position2, + number_of_pixels_at_that_position1, +) class IPTS_28402: @@ -55,7 +58,9 @@ def select_images(self): list_of_images = glob.glob(os.path.join(self.working_dir, "*.tif*")) self.load_images(list_of_images=list_of_images) else: - file_folder_browser = FileFolderBrowser(working_dir=self.working_dir, next_function=self.load_images) + file_folder_browser = FileFolderBrowser( + working_dir=self.working_dir, next_function=self.load_images + ) file_folder_browser.select_images(filters={"TIFF": "*.tif?"}) def load_images(self, list_of_images): @@ -75,7 +80,11 @@ def load_images(self, list_of_images): if self.data: [self.height, self.width] = np.shape(np.squeeze(self.data[0])) - display(HTML("Number of images loaded: " + str(len(list_of_images)) + "")) + display( + HTML( + "Number of images loaded: " + str(len(list_of_images)) + "" + ) + ) def visualize_raw_images(self): fig, ax1 = plt.subplots(num="Raw Images after 90degrees rotation") @@ -86,7 +95,12 @@ def plot(image_index): v = interactive( plot, - image_index=widgets.IntSlider(min=0, max=len(self.data) - 1, value=0, layout=widgets.Layout(width="50%")), + image_index=widgets.IntSlider( + min=0, + max=len(self.data) - 1, + value=0, + layout=widgets.Layout(width="50%"), + ), ) display(v) @@ -111,11 +125,21 @@ def plot(image_index, left, right, top, bottom): self.crop_ui = interactive( plot, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), - left=widgets.IntSlider(min=0, max=width, value=self.config["default_crop"]["x0"]), - right=widgets.IntSlider(min=0, max=width, value=self.config["default_crop"]["x1"]), - top=widgets.IntSlider(min=0, max=height, value=self.config["default_crop"]["y0"]), - bottom=widgets.IntSlider(min=0, max=height, value=self.config["default_crop"]["y1"]), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), + left=widgets.IntSlider( + min=0, max=width, value=self.config["default_crop"]["x0"] + ), + right=widgets.IntSlider( + min=0, max=width, value=self.config["default_crop"]["x1"] + ), + top=widgets.IntSlider( + min=0, max=height, value=self.config["default_crop"]["y0"] + ), + bottom=widgets.IntSlider( + min=0, max=height, value=self.config["default_crop"]["y1"] + ), ) display(self.crop_ui) @@ -133,7 +157,12 @@ def plot(image_index): ax1 = plt.imshow(data, vmin=0, vmax=1) plt.tight_layout() - v = interactive(plot, image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0)) + v = interactive( + plot, + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), + ) display(v) def selection_of_profiles_limit(self): @@ -154,7 +183,9 @@ def plot(image_index, top, bottom): self.profile_limit_ui = interactive( plot, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), top=widgets.IntSlider(min=0, max=height - 1, value=default_top), bottom=widgets.IntSlider(min=0, max=height - 1, value=default_bottom), ) @@ -183,7 +214,12 @@ def plot(image_index): ax1.plot(data, ".") plt.tight_layout() - v = interactive(plot, image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0)) + v = interactive( + plot, + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), + ) display(v) def cylinders_positions(self): @@ -246,10 +282,16 @@ def plot(image_index, center, inner_radius, outer_radius): self.cylinders_positions_ui = interactive( plot, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), center=widgets.IntSlider(min=0, max=width, value=default_center), - inner_radius=widgets.IntSlider(min=0, max=width, value=default_inner_radius), - outer_radius=widgets.IntSlider(min=0, max=width, value=default_outer_radius), + inner_radius=widgets.IntSlider( + min=0, max=width, value=default_inner_radius + ), + outer_radius=widgets.IntSlider( + min=0, max=width, value=default_outer_radius + ), ) display(self.cylinders_positions_ui) @@ -266,7 +308,9 @@ def cleaning_edges(self): self.config["profiles_plot"]["left_inner_cylinder"] = left_inner_edge self.config["profiles_plot"]["right_inner_cylinder"] = right_inner_edge - profiles_cleaned = [_profile[left_outer:right_outer] for _profile in self.profiles] + profiles_cleaned = [ + _profile[left_outer:right_outer] for _profile in self.profiles + ] self.profiles_cleaned = profiles_cleaned fig, ax1 = plt.subplots(num="Outer and Inner cylinders profiles") @@ -277,16 +321,23 @@ def plot_profiles_cleaned(image_index): plt.axhline(y=1, color="green") plot_cleaning_edges_ui = interactive( - plot_profiles_cleaned, image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0) + plot_profiles_cleaned, + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), ) display(plot_cleaning_edges_ui) def switching_to_attenuation_mode(self): threshold_value = 1 - profiles_attenuation_mode = [(threshold_value - _profile) for _profile in self.profiles_cleaned] + profiles_attenuation_mode = [ + (threshold_value - _profile) for _profile in self.profiles_cleaned + ] self.profiles_attenuation_mode = profiles_attenuation_mode - fig_attenuation, ax_attenuation = plt.subplots(num="Profiles in attenuation mode") + fig_attenuation, ax_attenuation = plt.subplots( + num="Profiles in attenuation mode" + ) def plot_profiles_attenuation(image_index): ax_attenuation.cla() @@ -294,7 +345,10 @@ def plot_profiles_attenuation(image_index): plt.axhline(y=1, color="green") plot_attenuation_ui = interactive( - plot_profiles_attenuation, image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0) + plot_profiles_attenuation, + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), ) display(plot_attenuation_ui) @@ -363,22 +417,30 @@ def outer_cylinder_geometry_correction(self, sampling_nbr_of_points=3): intensity_of_left_ring_array = [] for x in x_axis_of_left_ring_array: measure = _profile[x] - number_of_pixels_through_thickness = number_of_pixels_at_that_position1(position=x, radius=outer_radius) + number_of_pixels_through_thickness = number_of_pixels_at_that_position1( + position=x, radius=outer_radius + ) value = measure / number_of_pixels_through_thickness intensity_of_left_ring_array.append(value) intensity_of_ring_array.append(value) - intensity_of_left_ring_array_for_all_images.append(intensity_of_left_ring_array) + intensity_of_left_ring_array_for_all_images.append( + intensity_of_left_ring_array + ) intensity_of_right_ring_array = [] for x in x_axis_of_right_ring_array: measure = _profile[x] - number_of_pixels_through_thickness = number_of_pixels_at_that_position1(position=x, radius=outer_radius) + number_of_pixels_through_thickness = number_of_pixels_at_that_position1( + position=x, radius=outer_radius + ) value = measure / number_of_pixels_through_thickness intensity_of_right_ring_array.append(value) intensity_of_ring_array.append(value) - intensity_of_right_ring_array_for_all_images.append(intensity_of_right_ring_array) + intensity_of_right_ring_array_for_all_images.append( + intensity_of_right_ring_array + ) median_intensity_for_all_images.append(np.median(intensity_of_ring_array)) @@ -389,12 +451,18 @@ def outer_cylinder_geometry_correction(self, sampling_nbr_of_points=3): def plot_profiles_with_outer_cylinder_corrected(image_index): ax_outer.cla() - plt.plot(x_axis_of_left_ring_array, intensity_of_left_ring_array_for_all_images[image_index], ".r") + plt.plot( + x_axis_of_left_ring_array, + intensity_of_left_ring_array_for_all_images[image_index], + ".r", + ) plt.plot(x_axis_of_right_ring_array, intensity_of_right_ring_array, ".r") plot_profile_outer_ui = interactive( plot_profiles_with_outer_cylinder_corrected, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), ) display(plot_profile_outer_ui) @@ -415,15 +483,23 @@ def full_profile_with_only_outer_cylinder_corrected(self): position=x, inner_circle_r=inner_radius, outer_circle_r=outer_radius ) - shape_of_ring_signal.append(nbr_pixels * median_intensity_for_all_images[_profile_index]) + shape_of_ring_signal.append( + nbr_pixels * median_intensity_for_all_images[_profile_index] + ) - full_shape_of_ring_signal = np.concatenate((shape_of_ring_signal[::-1], shape_of_ring_signal[:-1:])) + full_shape_of_ring_signal = np.concatenate( + (shape_of_ring_signal[::-1], shape_of_ring_signal[:-1:]) + ) shape_of_ring_signal_for_all_images.append(full_shape_of_ring_signal) # new profile with outer cylinder removed - new_profile_with_outer_cylinder_removed.append(_profile - full_shape_of_ring_signal) + new_profile_with_outer_cylinder_removed.append( + _profile - full_shape_of_ring_signal + ) - self.profile_with_outer_cylinder_removed = new_profile_with_outer_cylinder_removed + self.profile_with_outer_cylinder_removed = ( + new_profile_with_outer_cylinder_removed + ) def plot_shape_ring_signal(image_index): trace = go.Scatter(y=shape_of_ring_signal_for_all_images[image_index]) @@ -442,14 +518,19 @@ def plot_shape_ring_signal(image_index): marker=dict(color="Blue", line=dict(color="Red", width=1)), ) layout = go.Layout( - title="shape of ring with outside cylinder corrected", xaxis=dict(title=""), yaxis=dict(title="") + title="shape of ring with outside cylinder corrected", + xaxis=dict(title=""), + yaxis=dict(title=""), ) figure = go.Figure(data=[trace], layout=layout) iplot(figure) plot_shape_ring_signal_ui = interactive( - plot_shape_ring_signal, image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0) + plot_shape_ring_signal, + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), ) display(plot_shape_ring_signal_ui) @@ -475,9 +556,19 @@ def plot_crop_inner(image_index, left_edge, right_edge): self.plot_crop_inner_ui = interactive( plot_crop_inner, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), - left_edge=widgets.IntSlider(min=0, max=len(self.profile_with_outer_cylinder_removed[0]), value=left_edge), - right_edge=widgets.IntSlider(min=0, max=len(self.profile_with_outer_cylinder_removed[0]), value=right_edge), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), + left_edge=widgets.IntSlider( + min=0, + max=len(self.profile_with_outer_cylinder_removed[0]), + value=left_edge, + ), + right_edge=widgets.IntSlider( + min=0, + max=len(self.profile_with_outer_cylinder_removed[0]), + value=right_edge, + ), ) display(self.plot_crop_inner_ui) @@ -527,13 +618,17 @@ def plot_final_inner(image_index): self.plot_final_inner_ui = interactive( plot_final_inner, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), ) display(self.plot_final_inner_ui) def export_profiles(self): working_dir = self.working_dir - output_folder_browser = FileFolderBrowser(working_dir=working_dir, next_function=self.export) + output_folder_browser = FileFolderBrowser( + working_dir=working_dir, next_function=self.export + ) output_folder_browser.select_output_folder() def export(self, output_folder): @@ -550,8 +645,12 @@ def export(self, output_folder): base_name = os.path.basename(_image) base_name_without_suffix = PurePosixPath(base_name).stem - base_name_of_ascii_file = str(base_name_without_suffix) + "_profile_corrected.csv" - full_name_of_ascii_file = os.path.join(output_folder, base_name_of_ascii_file) + base_name_of_ascii_file = ( + str(base_name_without_suffix) + "_profile_corrected.csv" + ) + full_name_of_ascii_file = os.path.join( + output_folder, base_name_of_ascii_file + ) list_of_ascii_file_created.append(full_name_of_ascii_file) metadata = [f"# input working file: {_image}"] @@ -580,9 +679,17 @@ def export(self, output_folder): metadata.append("# pixel, counts per pixels") - data_array = [f"{x}, {y}" for (x, y) in zip(pixel_index, array_for_this_image, strict=False)] + data_array = [ + f"{x}, {y}" + for (x, y) in zip(pixel_index, array_for_this_image, strict=False) + ] - make_ascii_file(metadata=metadata, data=data_array, output_file_name=full_name_of_ascii_file, dim="1d") + make_ascii_file( + metadata=metadata, + data=data_array, + output_file_name=full_name_of_ascii_file, + dim="1d", + ) display( HTML( @@ -591,4 +698,10 @@ def export(self, output_folder): ) ) for _ascii_file in list_of_ascii_file_created: - display(HTML(' - ' + _ascii_file + "")) + display( + HTML( + ' - ' + + _ascii_file + + "" + ) + ) diff --git a/notebooks/users_notebooks/ipts_30610_code/ipts_30610.py b/notebooks/users_notebooks/ipts_30610_code/ipts_30610.py index f77d7db91..6c60e99d0 100755 --- a/notebooks/users_notebooks/ipts_30610_code/ipts_30610.py +++ b/notebooks/users_notebooks/ipts_30610_code/ipts_30610.py @@ -61,7 +61,9 @@ def select_images(self): list_of_images = glob.glob(os.path.join(self.working_dir, "*.tif*")) self.load_images(list_of_images=list_of_images) else: - file_folder_browser = FileFolderBrowser(working_dir=self.working_dir, next_function=self.load_images) + file_folder_browser = FileFolderBrowser( + working_dir=self.working_dir, next_function=self.load_images + ) file_folder_browser.select_images(filters={"TIFF": "*.tif?"}) def load_images(self, list_of_images): @@ -81,7 +83,11 @@ def load_images(self, list_of_images): if self.data: [self.height, self.width] = np.shape(np.squeeze(self.data[0])) - display(HTML("Number of images loaded: " + str(len(list_of_images)) + "")) + display( + HTML( + "Number of images loaded: " + str(len(list_of_images)) + "" + ) + ) def visualize_raw_images(self): fig, ax1 = plt.subplots(num="Raw Images") @@ -92,7 +98,12 @@ def plot(image_index): v = interactive( plot, - image_index=widgets.IntSlider(min=0, max=len(self.data) - 1, value=0, layout=widgets.Layout(width="50%")), + image_index=widgets.IntSlider( + min=0, + max=len(self.data) - 1, + value=0, + layout=widgets.Layout(width="50%"), + ), ) display(v) @@ -134,8 +145,12 @@ def plot(rot_value, image_index, vert_guide, profile1_h, profile2_h): # color='g', # linestyle="--") - profile1 = data[profile1_h, vert_guide - profile_margin : vert_guide + profile_margin] - profile2 = data[profile2_h, vert_guide - profile_margin : vert_guide + profile_margin] + profile1 = data[ + profile1_h, vert_guide - profile_margin : vert_guide + profile_margin + ] + profile2 = data[ + profile2_h, vert_guide - profile_margin : vert_guide + profile_margin + ] ax1.cla() ax1.plot(profile1, "b", label="profile 1") @@ -148,9 +163,17 @@ def plot(rot_value, image_index, vert_guide, profile1_h, profile2_h): self.v = interactive( plot, rot_value=widgets.FloatSlider( - min=-5.0, max=5.0, value=default_rotate_angle, layout=widgets.Layout(width="50%") + min=-5.0, + max=5.0, + value=default_rotate_angle, + layout=widgets.Layout(width="50%"), + ), + image_index=widgets.IntSlider( + min=0, + max=len(self.data) - 1, + value=0, + layout=widgets.Layout(width="50%"), ), - image_index=widgets.IntSlider(min=0, max=len(self.data) - 1, value=0, layout=widgets.Layout(width="50%")), vert_guide=widgets.IntSlider( min=0, max=self.width - 1, @@ -206,12 +229,24 @@ def plot(image_index, left, right, top, bottom, profile_mker): self.crop_ui = interactive( plot, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), - left=widgets.IntSlider(min=0, max=width - 1, value=self.config["default_crop"]["x0"]), - right=widgets.IntSlider(min=0, max=width - 1, value=self.config["default_crop"]["x1"]), - top=widgets.IntSlider(min=0, max=height - 1, value=self.config["default_crop"]["y0"]), - bottom=widgets.IntSlider(min=0, max=height - 1, value=self.config["default_crop"]["y1"]), - profile_mker=widgets.IntSlider(min=0, max=height - 1, value=self.config["default_crop"]["marker"]), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), + left=widgets.IntSlider( + min=0, max=width - 1, value=self.config["default_crop"]["x0"] + ), + right=widgets.IntSlider( + min=0, max=width - 1, value=self.config["default_crop"]["x1"] + ), + top=widgets.IntSlider( + min=0, max=height - 1, value=self.config["default_crop"]["y0"] + ), + bottom=widgets.IntSlider( + min=0, max=height - 1, value=self.config["default_crop"]["y1"] + ), + profile_mker=widgets.IntSlider( + min=0, max=height - 1, value=self.config["default_crop"]["marker"] + ), ) display(self.crop_ui) @@ -229,7 +264,12 @@ def plot(image_index): ax1.imshow(data, vmin=0, vmax=1) # plt.tight_layout() - v = interactive(plot, image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0)) + v = interactive( + plot, + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), + ) display(v) def export_cropped_images(self): @@ -247,7 +287,9 @@ def export_cropped_images(self): def export_cropped_images_step2(self, output_folder): output_folder = os.path.abspath(output_folder) working_dir = self.working_dir - base_working_dir = os.path.join(output_folder, os.path.basename(working_dir) + "_cropped") + base_working_dir = os.path.join( + output_folder, os.path.basename(working_dir) + "_cropped" + ) base_working_dir = make_or_increment_folder_name(base_working_dir) list_images_corrected = self.cropped_data @@ -264,7 +306,13 @@ def export_cropped_images_step2(self, output_folder): progress_bar.value = index + 1 progress_bar.close() - display(HTML('' + str(nbr_images) + " images created!")) + display( + HTML( + '' + + str(nbr_images) + + " images created!" + ) + ) def background_range_selection(self): fig, ax1 = plt.subplots(num="Select top and bottom of background range") @@ -283,7 +331,9 @@ def plot(image_index, top, bottom): self.background_limit_ui = interactive( plot, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), top=widgets.IntSlider(min=0, max=height - 1, value=default_top), bottom=widgets.IntSlider(min=0, max=height - 1, value=default_bottom), ) @@ -307,7 +357,9 @@ def plot(image_index, top, bottom): self.sample_limit_ui = interactive( plot, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), top=widgets.IntSlider(min=0, max=height - 1, value=default_top), bottom=widgets.IntSlider(min=0, max=height - 1, value=default_bottom), ) @@ -321,13 +373,16 @@ def remove_background_signal(self): y0_background = self.background_limit_ui.children[1].value y1_background = self.background_limit_ui.children[2].value background_signal_integrated = [ - np.mean(_data[y0_background : y1_background + 1, :], axis=0) for _data in self.cropped_data + np.mean(_data[y0_background : y1_background + 1, :], axis=0) + for _data in self.cropped_data ] y0_sample = self.sample_limit_ui.children[1].value y1_sample = self.sample_limit_ui.children[2].value sample_without_background = [] - for _background, _sample in zip(background_signal_integrated, self.cropped_data, strict=False): + for _background, _sample in zip( + background_signal_integrated, self.cropped_data, strict=False + ): _data = _sample[y0_sample : y1_sample + 1] sample_without_background.append(np.abs(_data - _background)) @@ -340,7 +395,10 @@ def plot(image_index): ax1.imshow(self.sample_without_background[image_index], vmin=0, vmax=1) self.sample_no_background_ui = interactive( - plot, image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0) + plot, + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), ) display(self.sample_no_background_ui) @@ -364,7 +422,9 @@ def plot(image_index, profile_height): v = interactive( plot, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), profile_height=widgets.IntSlider(min=0, max=height - 1, value=0), ) display(v) @@ -391,8 +451,10 @@ def correct_cylinder_geometry(self): expected_array = [] for x_index, x in enumerate(profile): measure = x - number_of_pixels_through_thickness = number_of_pixels_at_that_position1( - position=x_index, radius=radius + number_of_pixels_through_thickness = ( + number_of_pixels_at_that_position1( + position=x_index, radius=radius + ) ) number_of_pixels.append(number_of_pixels_through_thickness) expected_array.append(measure / number_of_pixels_through_thickness) @@ -417,22 +479,32 @@ def plot(image_index, index1, index2, plot_max): self.sample_corrected = interactive( plot, - image_index=widgets.IntSlider(min=0, max=self.number_of_images - 1, value=0), - index1=widgets.IntSlider(min=0, max=height - 1, value=int((height - 1) / 3)), - index2=widgets.IntSlider(min=0, max=height - 1, value=2 * int((height - 1) / 3)), + image_index=widgets.IntSlider( + min=0, max=self.number_of_images - 1, value=0 + ), + index1=widgets.IntSlider( + min=0, max=height - 1, value=int((height - 1) / 3) + ), + index2=widgets.IntSlider( + min=0, max=height - 1, value=2 * int((height - 1) / 3) + ), plot_max=widgets.FloatSlider(min=1e-5, max=1.0, step=0.001, value=0.02), ) display(self.sample_corrected) def export_profiles(self): working_dir = os.path.dirname(self.working_dir) - output_folder_browser = FileFolderBrowser(working_dir=working_dir, next_function=self.export) + output_folder_browser = FileFolderBrowser( + working_dir=working_dir, next_function=self.export + ) output_folder_browser.select_output_folder() def export(self, output_folder): output_folder = os.path.abspath(output_folder) working_dir = self.working_dir - base_working_dir = os.path.join(output_folder, os.path.basename(working_dir) + "_cylindrical_geo_corrected") + base_working_dir = os.path.join( + output_folder, os.path.basename(working_dir) + "_cylindrical_geo_corrected" + ) base_working_dir = make_or_increment_folder_name(base_working_dir) # export images @@ -450,7 +522,13 @@ def export(self, output_folder): progress_bar.value = index + 1 progress_bar.close() - display(HTML('' + str(nbr_images) + " images created!")) + display( + HTML( + '' + + str(nbr_images) + + " images created!" + ) + ) # export profiles @@ -473,8 +551,12 @@ def export(self, output_folder): for index, image in enumerate(list_images_corrected): _name = os.path.basename(list_of_images[index]) base_name_without_suffix = PurePosixPath(_name).stem - base_name_of_ascii_file = str(base_name_without_suffix) + "_profile_corrected.csv" - full_name_of_ascii_file = os.path.join(base_working_dir, base_name_of_ascii_file) + base_name_of_ascii_file = ( + str(base_name_without_suffix) + "_profile_corrected.csv" + ) + full_name_of_ascii_file = os.path.join( + base_working_dir, base_name_of_ascii_file + ) df = pd.DataFrame(image) df.to_csv(full_name_of_ascii_file) @@ -483,11 +565,27 @@ def export(self, output_folder): progress_bar.close() - display(HTML('' + str(nbr_images) + " ASCII files created!")) + display( + HTML( + '' + + str(nbr_images) + + " ASCII files created!" + ) + ) json_file_name = os.path.join(base_working_dir, "metadata.json") with open(json_file_name, "w") as outfile: json.dump(metadata, outfile) - display(HTML(' metadata json file created (metadata.json)!')) + display( + HTML( + ' metadata json file created (metadata.json)!' + ) + ) - display(HTML(' Output folder: ' + base_working_dir + "!")) + display( + HTML( + ' Output folder: ' + + base_working_dir + + "!" + ) + ) diff --git a/util/.pre_commit.py b/util/.pre_commit.py index 5b19bb740..422832b92 100755 --- a/util/.pre_commit.py +++ b/util/.pre_commit.py @@ -7,7 +7,9 @@ # this will clean up all the ipynb files by running the nbstripout program # in the specified folder -parser = argparse.ArgumentParser(description="Preparing notebooks for deploymenet to analysis machine") +parser = argparse.ArgumentParser( + description="Preparing notebooks for deploymenet to analysis machine" +) parser.add_argument("-i", "--input", help="Input folder to clean.", type=str) diff --git a/util/installation_check.py b/util/installation_check.py index 4cfc8d641..857b2173e 100755 --- a/util/installation_check.py +++ b/util/installation_check.py @@ -48,7 +48,9 @@ if success: print("All required packages installed") else: - print("Please install these missing packages " "to be able to run the Imaging Notebooks.") + print( + "Please install these missing packages to be able to run the Imaging Notebooks." + ) missing = [k for k, v in import_result.items() if not v] print("\t" + "\n\t".join(missing)) @@ -63,7 +65,9 @@ def version_checker(package_name, version, nbextension=None): if nbextension is None: nbextension = package_name if not good_version: - print(f"\n**** Please upgrade {package_name} to version {version_check_packages[package_name]} by running:") + print( + f"\n**** Please upgrade {package_name} to version {version_check_packages[package_name]} by running:" + ) print(f" conda remove --force {package_name} # if you use conda") print(f" pip install --pre --upgrade {package_name}") print(f" jupyter nbextension enable --py {nbextension}")