Example #1
0
 def test_sorted_nicely(self):
     # test image file sorting
     expected = ['test_001_dapi', 'test_002_dapi', 'test_003_dapi']
     unsorted = ['test_003_dapi', 'test_001_dapi', 'test_002_dapi']
     self.assertListEqual(expected, misc_utils.sorted_nicely(unsorted))
     # test montage folder sorting
     expected = ['test_0_0', 'test_1_0', 'test_1_1']
     unsorted = ['test_1_1', 'test_0_0', 'test_1_0']
     self.assertListEqual(expected, misc_utils.sorted_nicely(unsorted))
Example #2
0
def trk_folder_to_trks(dirname, trks_filename):
    """Compiles a directory of trk files into one trks_file.

    Args:
        dirname: full path to the directory containing multiple trk files
        trks_filename: desired filename (the name should end in .trks)

    Returns:
        Nothing
    """
    lineages = []
    raw = []
    tracked = []

    file_list = os.listdir(dirname)
    file_list_sorted = sorted_nicely(file_list)

    for filename in file_list_sorted:
        trk = load_trks(os.path.join(dirname, filename))
        lineages.append(trk['lineages'][0])  # this is loading a single track
        raw.append(trk['X'])
        tracked.append(trk['y'])

    file_path = os.path.join(os.path.dirname(dirname), trks_filename)

    save_trks(file_path, lineages, raw, tracked)
Example #3
0
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
Example #4
0
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
Example #5
0
def nikon_getfiles(direc_name, channel_name):
    """
    Return all image filenames in direc_name with
    channel_name in the filename
    """
    imglist = os.listdir(direc_name)
    imgfiles = [i for i in imglist if channel_name in i]
    imgfiles = sorted_nicely(imgfiles)
    return imgfiles
Example #6
0
def nikon_getfiles(direc_name, channel_name):
    """Return a sorted list of files inside direc_name
    with channel_name in the filename.

    Args:
        direc_name (str): directory to find image files
        channel_name (str): wildcard filter for filenames

    Returns:
        list: sorted list of files inside direc_name.
    """
    imglist = os.listdir(direc_name)
    imgfiles = [i for i in imglist if channel_name in i]
    imgfiles = sorted_nicely(imgfiles)
    return imgfiles