def test_nikon_getfiles(self): temp_dir = self.get_temp_dir() for filename in ('channel.tif', 'multi1.tif', 'multi2.tif'): _write_image(os.path.join(temp_dir, filename), 300, 300) images = io_utils.nikon_getfiles(temp_dir, 'channel') self.assertListEqual(images, ['channel.tif']) multi_images = io_utils.nikon_getfiles(temp_dir, 'multi') self.assertListEqual(multi_images, ['multi1.tif', 'multi2.tif']) no_images = io_utils.nikon_getfiles(temp_dir, 'bad_channel_name') self.assertListEqual(no_images, [])
def load_training_images_3d(direc_name, training_direcs, raw_image_direc, channel_names, image_size, num_frames, montage_mode=False): """Load each image in the training_direcs into a numpy array. # Arguments direc_name: directory containing folders of training data training_direcs: list of directories of images inside direc_name. raw_image_direc: directory name inside each training dir with raw images channel_names: Loads all raw images with a channel_name in the filename image_size: size of each image as tuple (x, y) num_frames: number of frames to load from each training directory montage_mode: load masks from "montaged" subdirs inside annotation_direc """ is_channels_first = K.image_data_format() == 'channels_first' image_size_x, image_size_y = image_size # flatten list of lists X_dirs = [os.path.join(direc_name, t, raw_image_direc) for t in training_direcs] if montage_mode: X_dirs = [os.path.join(t, p) for t in X_dirs for p in os.listdir(t)] X_dirs = sorted_nicely(X_dirs) # Initialize training data array if is_channels_first: X_shape = (len(X_dirs), len(channel_names), num_frames, image_size_x, image_size_y) else: X_shape = (len(X_dirs), num_frames, image_size_x, image_size_y, len(channel_names)) X = np.zeros(X_shape, dtype=K.floatx()) # Load 3D training images for b, direc in enumerate(X_dirs): for c, channel in enumerate(channel_names): imglist = nikon_getfiles(direc, channel) for i, img in enumerate(imglist): if i >= num_frames: print('Skipped final {skip} frames of {dir}, as num_frames ' 'is {num} but there are {total} total frames'.format( skip=len(imglist) - num_frames, dir=direc, num=num_frames, total=len(imglist))) break image_data = np.asarray(get_image(os.path.join(direc, img))) if is_channels_first: X[b, c, i, :, :] = image_data else: X[b, i, :, :, c] = image_data return X
def load_annotated_images_3d(direc_name, training_direcs, annotation_direc, annotation_name, image_size, num_frames, montage_mode=False): """Load each annotated image in the training_direcs into a numpy array. # Arguments direc_name: directory containing folders of training data training_direcs: list of directories of images inside direc_name. annotation_direc: directory name inside each training dir with masks annotation_name: Loads all masks with annotation_name in the filename image_size: size of each image as tuple (x, y) num_frames: number of frames to load from each training directory montage_mode: load masks from "montaged" subdirs inside annotation_direc """ is_channels_first = K.image_data_format() == 'channels_first' image_size_x, image_size_y = image_size # wrapping single annotation name in list for consistency if not isinstance(annotation_name, list): annotation_name = [annotation_name] y_dirs = [os.path.join(direc_name, t, annotation_direc) for t in training_direcs] if montage_mode: y_dirs = [os.path.join(t, p) for t in y_dirs for p in os.listdir(t)] y_dirs = sorted_nicely(y_dirs) if is_channels_first: y_shape = (len(y_dirs), len(annotation_name), num_frames, image_size_x, image_size_y) else: y_shape = (len(y_dirs), num_frames, image_size_x, image_size_y, len(annotation_name)) y = np.zeros(y_shape, dtype='int32') for b, direc in enumerate(y_dirs): for c, name in enumerate(annotation_name): imglist = nikon_getfiles(direc, name) for z, img_file in enumerate(imglist): if z >= num_frames: print('Skipped final {skip} frames of {dir}, as num_frames ' 'is {num} but there are {total} total frames'.format( skip=len(imglist) - num_frames, dir=direc, num=num_frames, total=len(imglist))) break annotation_img = get_image(os.path.join(direc, img_file)) if is_channels_first: y[b, c, z, :, :] = annotation_img else: y[b, z, :, :, c] = annotation_img return y