def query_extract_region(fixed_image: sitk.Image, moving_image: sitk.Image, transform: sitk.Transform, registration_method: sitk.ImageRegistrationMethod): do_extract = util.query_yes_no( 'Do you wish to extract a sub-region to register based on? [y/n] >> ') if do_extract: itkplt.plot_overlay(fixed_image, moving_image, transform=transform) size = util.query_int('Enter the region size >> ') origin = [] for dim in range(len(fixed_image.GetSpacing())): origin_in_dim = util.query_float( 'Enter the origin in dimension {0} >> '.format(dim)) origin.append(origin_in_dim) origin = np.array(origin) fixed_region = extract_region(fixed_image, size, origin) moving_region = extract_region(moving_image, size * 1.1, origin - 0.05 * size, transform) query_registration_sampling_change(registration_method) return fixed_region, moving_region, do_extract else: return fixed_image, moving_image, do_extract
def query_rotation_change(fixed_image: sitk.Image, moving_image: sitk.Image, initial_transform: sitk.Transform): """Ask if the user wants a new 2D ITK origin based on image overlay""" itkplt.plot_overlay(fixed_image, moving_image, initial_transform) change_rotation = util.query_yes_no( 'Do you want to change the rotation? [y/n] >> ') if change_rotation: while True: rotation = util.query_float('Enter new rotation (degrees): ') tran.set_transform_rotation(initial_transform, rotation) itkplt.plot_overlay(fixed_image, moving_image, initial_transform, rotation) # bug: The image does not show up till after the question if util.query_yes_no('Is this rotation good? [y/n] >> '): break
def query_good_registration(transform: sitk.Transform, metric, stop): print('\nFinal metric value: {0}'.format(metric)) print('\n{0}'.format(stop)) transform_params = transform.GetParameters() matrix = np.array([transform_params[0:2], transform_params[2:4]]) translation = np.array(transform_params[4:6]) print('\nTransform Matrix: \n{0}'.format(matrix)) print('\nTransform Translation: \n{0}'.format(translation)) return util.query_yes_no('Is this registration good? [y/n] >>> ')
def _check_for_output(self): output_path = Path( self.fuse_args['output_file_directory'].replace('[', '').replace( ']', ''), self.output_name) if output_path.is_file(): if self.overwrite_tif is not None: return not self.overwrite_tif else: return util.query_yes_no( '{} already exists. Skip image fusion? >> '.format( output_path)) else: return False
def query_translation_change(fixed_image: sitk.Image, moving_image: sitk.Image, transform: sitk.Transform): """Ask if the user wants a new 2D ITK translation based on image overlay""" change_origin = util.query_yes_no( 'Do you want to change the initial translation? [y/n] >> ') translation = tran.get_translation(transform) if change_origin: while True: print('Current physical shift: ' + str(-1 * np.array(translation))) new_translation = [] for dim in range(len(translation)): new_dim_translation = -1 * util.query_float( 'Enter the new shift in dimension {0} >> '.format( str(dim))) new_translation.append(new_dim_translation) tran.set_translation(transform, new_translation) itkplt.plot_overlay(fixed_image, moving_image, transform) # bug: The image does not show up till after the question if util.query_yes_no('Is this translation good? [y/n] >>> '): break
def _check_for_xml(self): """ Check for the dataset.xml file and ask if the user wants to skip reading/resaving the .mat files. :return: boolean whether to skip dataset definition or not. """ if self.intermediate_save_dir is not None: xml_path = Path(self.intermediate_save_dir, 'dataset.xml') if xml_path.is_file(): if self.overwrite_dataset is None: return util.query_yes_no( 'XML file already exists. Skip reading .mat files? >> ' ) else: return not self.overwrite_dataset else: return False
def query_registration_sampling_change( registration_method: sitk.ImageRegistrationMethod): """ Ask the user if they want to change the registration sampling rate, and to what if they do. :param registration_method: The registration method being used in the registration algorithm :return: """ do_change = util.query_yes_no( 'Do you wish to change the registration sampling rate (default 0.01)? [y/n] >> ' ) if do_change: while True: new_rate = util.query_float('Please enter a rate >0 and <= 1 >> ') if new_rate > 0 and new_rate <= 1: break print('{0} is an invalid rate'.format(new_rate)) registration_method.SetMetricSamplingPercentage(new_rate)
def setup_image(path_image: Path, unit_workspace: str = 'microns', write_changes: bool = True, dimensions: int = 2): """ Read in an itk image and ensure that its spacing is in the right units/has been set in the first place :param path_image: path to the image file :param unit_workspace: unit that the workspace is working in :param write_changes: Whether to save new metadata :dimensions: Number of spatial dimensions for the image type :return: """ image = sitk.ReadImage(str(path_image)) metadata = read_metadata(path_image) if metadata is None: print('{0} has no metadata.'.format(str(path_image.name))) image.SetMetaData('Unit', unit_workspace) current_spacing = image.GetSpacing() change_spacing = util.query_yes_no( 'Change the spacing? Current spacing is {0} [y/n] >> '.format( current_spacing)) if change_spacing: spacing = _query_spacing(unit_workspace, dimensions) image.SetSpacing(spacing) if write_changes: write_image(image, path_image) else: for key in metadata: image.SetMetaData(key, metadata[key]) if len(image.GetSpacing()) > dimensions: # todo: nd to rgb image = three_d_to_rgb(image) return image
def test_bad_input_then_yes(self, monkeypatch, user_input_fixture): user_inputs = user_input_fixture(['5', 'y', 'test']) assert util.query_yes_no(user_inputs) is True
def test_yes_no_inputs(self, input, expected, monkeypatch): monkeypatch.setattr('builtins.input', lambda x: input) assert util.query_yes_no('') is expected