def test_count_image_files(self): # no montage_mode test_extensions = [ '.tif', '.tiff', '.TIF', '.TIFF', '.png', '.PNG', ] temp_dir = self.get_temp_dir() for i, e in enumerate(test_extensions): img_name = 'phase_{}{}'.format(i, e) _write_image(os.path.join(temp_dir, img_name), 30, 30) self.assertEqual( io_utils.count_image_files(temp_dir, montage_mode=False), 6) # with montage mode temp_dir = self.get_temp_dir() # create subdirs os.makedirs(os.path.join(temp_dir, 'a')) os.makedirs(os.path.join(temp_dir, 'b')) # write each image in both directories for i, e in enumerate(test_extensions): img_name = 'phase_{}{}'.format(i, e) _write_image(os.path.join(temp_dir, 'a', img_name), 30, 30) _write_image(os.path.join(temp_dir, 'b', img_name), 30, 30) # write extra images in A that will be ignored for i, e in enumerate(test_extensions): img_name = 'phase2_{}{}'.format(i, e) _write_image(os.path.join(temp_dir, 'a', img_name), 30, 30) self.assertEqual( io_utils.count_image_files(temp_dir, montage_mode=True), 6)
def make_training_data_3d(direc_name, file_name_save, channel_names, training_direcs=None, annotation_name='corrected', raw_image_direc='raw', annotation_direc='annotated', reshape_size=None, num_frames=None, montage_mode=True): """Read all images in training directories and save as npz file. 3D image sets are "stacks" of images. For annotation purposes, these images have been sliced into "montages", where a section of each stack has been sliced for efficient annotated by humans. The raw_image_direc should be a specific montage (e.g. montage_0_0) and the annotation is the corresponding annotated montage. Args: direc_name (str): directory containing folders of training data file_name_save (str): full filepath for npz file where the data will be saved training_direcs (str[]): directories of images located inside direc_name. If None, all directories in direc_name are used. annotation_name (str): Loads all masks with annotation_name in the filename channel_names (str[]): Loads all raw images with a channel_name in the filename raw_image_direc (str): directory name inside each training dir with raw images annotation_direc (str): directory name inside each training dir with masks reshape_size (int): If provided, reshapes the images to the given size. num_frames (int): number of frames to load from each training directory montage_mode (bool): load masks from "montaged" subdirs inside annotation_direc """ # Load one file to get image sizes rand_train_dir = os.path.join(direc_name, random.choice(training_direcs), raw_image_direc) if montage_mode: rand_train_dir = os.path.join( rand_train_dir, random.choice(os.listdir(rand_train_dir))) image_size = get_image_sizes(rand_train_dir, channel_names) if num_frames is None: raw = [ os.path.join(direc_name, t, raw_image_direc) for t in training_direcs ] ann = [ os.path.join(direc_name, t, annotation_direc) for t in training_direcs ] if montage_mode: raw = [os.path.join(r, d) for r in raw for d in os.listdir(r)] ann = [os.path.join(a, d) for a in ann for d in os.listdir(a)] # use all images if not set # will select the first N images where N is the smallest # number of images in each subdir of all training_direcs num_frames_raw = min([count_image_files(f) for f in raw]) num_frames_ann = min([count_image_files(f) for f in ann]) num_frames = min(num_frames_raw, num_frames_ann) X = load_training_images_3d(direc_name, training_direcs, raw_image_direc=raw_image_direc, channel_names=channel_names, image_size=image_size, num_frames=num_frames, montage_mode=montage_mode) y = load_annotated_images_3d(direc_name, training_direcs, annotation_direc=annotation_direc, annotation_name=annotation_name, image_size=image_size, num_frames=num_frames, montage_mode=montage_mode) # Reshape X and y if reshape_size is not None: X, y = reshape_movie(X, y, reshape_size=reshape_size) np.savez(file_name_save, X=X, y=y)