示例#1
0
def dice_numpy(u, v, axis=None):
    """Return the Dice coefficients between two ndarrays. Perfect Dice is 1.0.

                     2 * | intersection(A, B) |
        Dice(A, B) = --------------------------
                            |A| + |B|

    Args:
        u, v: `ndarray`, input boolean ndarrays.
        axis: `int` or `tuple`, the dimension(s) along which to compute Dice.

    Returns:
        `float` Dice coefficient or `ndarray` of Dice coefficients.

    Notes:
        This functions is similar to `scipy.spatial.distance.dice` but returns
        `1 - scipy.spatial.distance.dice(u, v)`.
    """
    u = np.asarray(u)
    v = np.asarray(v)
    _check_shapes_equal(u, v)

    subset = (0, 1)  # Values must be 0 or 1.
    if u.dtype != bool:
        _check_all_x_in_subset_numpy(u, subset)
    if v.dtype != bool:
        _check_all_x_in_subset_numpy(v, subset)

    numerator = 2 * (u * v).sum(axis=axis)
    denominator = u.sum(axis=axis) + v.sum(axis=axis)
    return numerator / denominator
示例#2
0
def remove_empty_slices_(mask_arr, other_arr, axis=(1, 2, 3)):
    """Return `mask_arr` and `other_arr` with empty blocks in `mask_arr`
    removed.
    """
    _check_shapes_equal(mask_arr, other_arr)
    mask = mask_arr.any(axis=axis)
    return mask_arr[mask, ...], other_arr[mask, ...]
示例#3
0
def test__check_shapes_equal():
    shape = (2, 2, 2)
    x1 = np.zeros(shape)
    x2 = np.zeros(shape)

    util._check_shapes_equal(x1, x2)

    with pytest.raises(ValueError):
        util._check_shapes_equal(x1, x2.flatten())
示例#4
0
def itervolumes(filepaths,
                block_shape,
                x_dtype,
                y_dtype,
                strides=(1, 1, 1),
                shuffle=False,
                normalizer=None):
    """Yield tuples of numpy arrays `(features, labels)` from a list of
    filepaths to neuroimaging files.
    """
    filepaths = copy.deepcopy(filepaths)

    if shuffle:
        random.shuffle(filepaths)

    for idx, (features_fp, labels_fp) in enumerate(filepaths):
        try:
            features = read_volume(features_fp, dtype=x_dtype)
            labels = read_volume(labels_fp, dtype=y_dtype)
        except Exception:
            tf.logging.fatal(
                "Error reading at least one input file: {} {}".format(
                    features_fp, labels_fp))
            raise

        if normalizer is not None:
            features, labels = normalizer(features, labels)

        _check_shapes_equal(features, labels)
        feature_gen = iterblocks_3d(arr=features,
                                    kernel_size=block_shape,
                                    strides=strides)
        label_gen = iterblocks_3d(arr=labels,
                                  kernel_size=block_shape,
                                  strides=strides)

        for ff, ll in zip(feature_gen, label_gen):
            yield ff[..., np.newaxis], ll
示例#5
0
文件: volume.py 项目: leej3/nobrainer
def itervolumes(filepaths,
                block_shape,
                x_dtype=np.float32,
                y_dtype=np.int32,
                strides=None,
                shuffle=True,
                normalizer=None):
    """Yield tuples of numpy arrays `(features, labels)` from a list of
    filepaths to neuroimaging files.

    Args:
        filepaths: nested list of tuples, where each tuple has length two. The
            first item in each tuple is the path to the volume of features
            (e.g., an anatomical scan), and the second item is the path to the
            volume of features (e.g., FreeSurfer's aparc+aseg.mgz).
        block_shape: tuple of len 3 or None, the shape of blocks to take from
            the features and labels. This is useful if a full volume cannot fit
            into GPU memory. If `block_shape` is `None`, full volumes are
            yielded. Use `(None, None, None)` if yielding full volumes and
            volumes have different shapes.
        x_dtype: dtype object or string, data type of features.
        y_dtype: dtype object or string, data type of labels.
        strides: tuple or None, strides to take between blocks. If None,
            strides will be equal to `block_shape`, which will generate
            non-overlapping blocks.
        shuffle: bool, if true, shuffle the list of filepaths. Pairs of
            `(features, labels)` filepaths are maintained.
        normalizer: callable, function that accepts two arrays (`features` and
            `labels`) and returns two arrays (`features` and `labels`).

    Yields:
        Tuple of `(features, labels)`. If `block_shape` is a tuple of integers,
        the shape of `features` is `(*block_shape, 1)`, and the shape of
        `labels` is `block_shape`. If `block_shape` is `None` or
        `(None, None, None)`, the shape of `features` is `(*volume_shape, 1)`,
        and the shape of `labels` is `volume_shape`.
    """
    filepaths = copy.deepcopy(filepaths)

    # Common error is to pass the CSV filepath as `filepaths` argument.
    if isinstance(filepaths, str):
        raise ValueError("`filepaths` must be a nested sequence of filepaths.")

    if any((len(i) != 2 for i in filepaths)):
        raise ValueError("Found sequence with len != 2 in `filepaths`.")

    if shuffle:
        random.shuffle(filepaths)

    for idx, (features_fp, labels_fp) in enumerate(filepaths):
        try:
            features = read_volume(features_fp, dtype=x_dtype)
            labels = read_volume(labels_fp, dtype=y_dtype)
        except Exception:
            tf.logging.fatal(
                "Error reading at least one input file: {} {}".format(
                    features_fp, labels_fp))
            raise

        if normalizer is not None:
            features, labels = normalizer(features, labels)

        _check_shapes_equal(features, labels)

        # Yield full volumes.
        if block_shape is None or block_shape == (None, None, None):
            yield (features[..., np.newaxis].astype(x_dtype),
                   labels.astype(y_dtype))

        # Yield blocks of volumes.
        else:
            feature_gen = iterblocks_3d(arr=features,
                                        kernel_size=block_shape,
                                        strides=strides)
            label_gen = iterblocks_3d(arr=labels,
                                      kernel_size=block_shape,
                                      strides=strides)

            for ff, ll in zip(feature_gen, label_gen):
                yield ff[..., np.newaxis].astype(x_dtype), ll.astype(y_dtype)