def define_pixel_sizes(args): """ Defines the pixel sizes based on metadata file, or CLI flags :param args: amap argument object :return: args updated """ if None in [args.x_pixel_um, args.y_pixel_um, args.z_pixel_um]: if args.metadata is None: raise CommandLineInputError( "Not all pixel sizes are defined on the command line, but " "no metadata file has been supplied") try: metadata = get_acquisition_metadata(args.metadata) except NotImplementedError: raise CommandLineInputError( "Not all pixel sizes are defined on the command line, but the" " metadata file cannot be read.") for dim in ["x", "y", "z"]: dim_attribute = "{}_pixel_um".format(dim) if getattr(args, dim_attribute) is None: try: setattr(args, dim_attribute, getattr(metadata, dim_attribute)) except ArgumentTypeError: raise CommandLineInputError( "No {} pixel size was defined on the command line, and it " "cannot be parsed from the metadata".format(dim)) return args
def check_signal_overlap(signal_ids): signal_unique, repeated_signal_ch = tools.check_unique_list(signal_ids) if not signal_unique: logging.error( "Non-unique values for '--signal-channel-ids' ({}) given. ID:" " {} given more than once.".format(signal_ids, repeated_signal_ch)) raise CommandLineInputError( "Non-unique values for '--signal-channel-ids' ({}) given. ID:" " {} given more than once.".format(signal_ids, repeated_signal_ch))
def check_correct_number_signal_channels(signal_ids, num_signal_channels): num_signal_ids = len(signal_ids) if num_signal_channels != num_signal_ids: logging.error( "Number of signal channel IDs ({}) does not match the number of " "signal channels ({})".format(num_signal_ids, num_signal_channels)) raise CommandLineInputError( "Number of signal channel IDs ({}) does not match the number of " "signal channels ({})".format(num_signal_ids, num_signal_channels))
def xml_scale( xml_file, x_scale=1, y_scale=1, z_scale=1, output_directory=None, integer=True, ): # TODO: add a csv option """ To rescale the cell positions within an XML file. For compatibility with other software, or if data has been scaled after cell detection. :param xml_file: Any cellfinder xml file :param x_scale: Rescaling factor in the first dimension :param y_scale: Rescaling factor in the second dimension :param z_scale: Rescaling factor in the third dimension :param output_directory: Directory to save the rescaled XML file. Defaults to the same directory as the input XML file :param integer: Force integer cell positions (default: True) :return: """ if x_scale == y_scale == z_scale == 1: raise CommandLineInputError( "All rescaling factors are 1, " "please check the input." ) else: input_file = Path(xml_file) start_time = datetime.now() cells = cell_io.get_cells(xml_file) for cell in cells: cell.transform( x_scale=x_scale, y_scale=y_scale, z_scale=z_scale, integer=integer, ) if output_directory: output_directory = Path(output_directory) else: output_directory = input_file.parent ensure_directory_exists(output_directory) output_filename = output_directory / (input_file.stem + "_rescaled") output_filename = output_filename.with_suffix(input_file.suffix) cell_io.save_cells(cells, output_filename) print( "Finished. Total time taken: {}".format( datetime.now() - start_time ) )
def catch_input_file_error(path): """ Catches if an input path doesn't exist, and returns an informative error :param path: Input file path default) """ try: check_path_exists(path) except FileNotFoundError: message = ("File path: '{}' cannot be found. Please check your input " "arguments.".format(path)) raise CommandLineInputError(message)
def check_sig_bg_id_overlap(signal_ids, background_id): common_id_result, intersection = tools.common_member( signal_ids, background_id) if common_id_result: logging.error( "Non-unique values for '--signal-channel-ids' ({}) and for " "'--background-channel-id' ({}) given. ID: {} was given in" " both.".format(signal_ids, background_id, intersection)) raise CommandLineInputError( "Non-unique values for '--signal-channel-ids' ({}) and for " "'--background-channel-id' ({}) given. ID: {} was given in" " both.".format(signal_ids, background_id, intersection))