def test_register(tmpdir, test_config_path): output_directory = os.path.join(str(tmpdir), "output") amap_args = [ "amap", data_dir, output_directory, "-x", x_pix, "-y", y_pix, "-z", z_pix, "--n-free-cpus", "0", "--registration-config", test_config_path, "-d", data_dir, ] sys.argv = amap_args amap_run() image_list = [ "annotations.nii", "boundaries.nii", "brain_filtered.nii", "control_point_file.nii", "downsampled.nii", "hemispheres.nii", "inverse_control_point_file.nii", "registered_atlas.nii", "registered_hemispheres.nii", "downsampled_brain.nii", ] for image in image_list: are_images_equal(image, output_directory, test_output_dir) assert get_text_lines( os.path.join(output_directory, "affine_matrix.txt") ) == get_text_lines(os.path.join(test_output_dir, "affine_matrix.txt")) assert get_text_lines( os.path.join(output_directory, "invert_affine_matrix.txt") ) == get_text_lines( os.path.join(test_output_dir, "invert_affine_matrix.txt") ) assert ( ( pd.read_csv(os.path.join(output_directory, "volumes.csv")) == pd.read_csv(os.path.join(test_output_dir, "volumes.csv")) ) .all() .all() )
def get_sorted_file_paths(file_path, file_extension=None, encoding=None): """ Sorts file paths with numbers "naturally" (i.e. 1, 2, 10, a, b), not lexiographically (i.e. 1, 10, 2, a, b). :param str file_path: File containing file_paths in a text file, or as a list. :param str file_extension: Optional file extension (if a directory is passed) :param encoding: If opening a text file, what encoding it has. Default: None (platform dependent) :return: Sorted list of file paths """ if isinstance(file_path, list): return natsorted(file_path) # assume if not a list, is a file path file_path = Path(file_path) if file_path.suffix == ".txt": return get_text_lines(file_path, sort=True, encoding=encoding) elif file_path.is_dir(): if file_extension is None: file_path = glob.glob(os.path.join(file_path, "*")) else: file_path = glob.glob(os.path.join(file_path, "*" + file_extension)) return natsorted(file_path) else: message = ( "Input file path is not a recognised format. Please check it " "is a list of file paths, a text file of these paths, or a " "directory containing image files.") raise NotImplementedError(message)
def test_get_sorted_file_paths(): # test list shuffled = sorted_cubes_dir.copy() shuffle(shuffled) assert system.get_sorted_file_paths(shuffled) == sorted_cubes_dir # test dir assert system.get_sorted_file_paths(cubes_dir) == sorted_cubes_dir assert (system.get_sorted_file_paths( cubes_dir, file_extension=".tif") == sorted_cubes_dir) # test text file # specifying utf8, as written on linux assert system.get_sorted_file_paths( jabberwocky, encoding="utf8") == get_text_lines(jabberwocky_sorted, encoding="utf8") # test unsupported with pytest.raises(NotImplementedError): system.get_sorted_file_paths(shuffled[0])
def test_register(tmpdir, test_config_path): output_directory = os.path.join(str(tmpdir), "output") cellfinder_args = [ "cellfinder", "-s", data_dir, "-b", data_dir, "-o", output_directory, "-x", x_pix, "-y", y_pix, "-z", z_pix, "--n-free-cpus", "0", "--registration-config", test_config_path, "--register", "--no-detection", "--no-classification", ] sys.argv = cellfinder_args cellfinder_run() output_directory = os.path.join(output_directory, "registration") # a hack because testing on linux on travis is 100% identical to local, # but windows is not if platform.system() == "Linux": image_list = [ "annotations.nii", "boundaries.nii", "brain_filtered.nii", "control_point_file.nii", "downsampled.nii", "hemispheres.nii", "inverse_control_point_file.nii", "registered_atlas.nii", "registered_hemispheres.nii", "downsampled_channel_0.nii", ] else: image_list = [ "annotations.nii", # "boundaries.nii", "brain_filtered.nii", "control_point_file.nii", "downsampled.nii", "hemispheres.nii", "inverse_control_point_file.nii", # "registered_atlas.nii", "registered_hemispheres.nii", "downsampled_channel_0.nii", ] for image in image_list: are_images_equal(image, output_directory, test_output_dir) assert get_text_lines(os.path.join( output_directory, "affine_matrix.txt")) == get_text_lines( os.path.join(test_output_dir, "affine_matrix.txt")) assert get_text_lines( os.path.join(output_directory, "invert_affine_matrix.txt")) == get_text_lines( os.path.join(test_output_dir, "invert_affine_matrix.txt")) pd.testing.assert_frame_equal( pd.read_csv(os.path.join(output_directory, "volumes.csv")), pd.read_csv(os.path.join(test_output_dir, "volumes.csv")), check_exact=False, check_less_precise=check_less_precise_pd, )
def test_get_string_lines(): assert string.get_text_lines(jabberwocky) == jabberwocky_list assert (string.get_text_lines(jabberwocky, return_lines=8) == jabberwocky_list[8])