Ejemplo n.º 1
0
def safe_get_times_3d(times: CachingDataStructure,
                      i: int, j: int, k: int) -> float:
    """
    Returns the saved time value in 3D array if is a valid index,
    otherwise infinity
    """
    return INF if not times.valid_index(i, j, k) else times[i, j, k]
def total_dist_from_point(data: CachingDataStructure, start_point: tuple,
                          max_prop_level: int = INF) -> tuple:
    """
    For some CachingDataType, it calculates the distance in linear
    space for all different propagation levels. Returns a tuple of three
    arrays; (1) an array of propagation level, (2) summed linear distance and
    (3) summed linear distance divided by the number of pixels/voxels at that
    propagation level
    """
    assert data.dim == len(start_point), \
        "Dimensions of data and start point don't match"
    assert data.dim in [2, 3], "Data should be 2- or 3-dimensional"

    start_internal_index = data.internal_index(*start_point)
    get_indices = indices_for_prop_level_2d \
        if data.dim == 2 \
        else indices_for_prop_level_3d

    prop_level_end = propagation_level_end(start_point, data.shape)
    prop_levels = range(min(prop_level_end, max_prop_level))
    ys1, ys2 = [], []
    summ, num_of_voxels = 0, 0
    for prop_level in prop_levels:
        for key in get_indices(prop_level, start_point):
            if data.valid_index(*key):
                summ += abs(data.internal_index(*key) - start_internal_index)
                num_of_voxels += 1
        ys1.append(summ)
        ys2.append(summ / num_of_voxels)
    return np.array(prop_levels), np.array(ys1), np.array(ys2)
Ejemplo n.º 3
0
def get_neighbors_2d(status: CachingDataStructure, i: int, j: int) -> list:
    """
    Returns a list of tuples of all coordinates that are direct neighbors,
    meaning the index is valid and they are not KNOWN
    """
    coords = []
    for (x, y) in [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]:
        if status.valid_index(x, y) and not status[x, y]:  # Not known
            coords.append((x, y))
    return coords
Ejemplo n.º 4
0
def get_neighbors_general(status: CachingDataStructure, key: tuple) -> list:
    """
    Returns a list of tuples of all coordinates that are direct neighbors,
    meaning the index is valid and they are not KNOWN
    """
    coords = []
    for key in get_direct_neighbour_coords_general(key):
        if status.valid_index(*key) and not status[key]:  # Not known
            coords.append(key)
    return coords
def convolve_pixel(picture: CachingDataStructure, kernel: np.ndarray,
                   kernel_dim: int, pad: int,
                   x: int, y: int, cval: float) -> CachingDataStructure:
    """Performs spatial convolution at a single pixel in a 2D image"""
    pixel = 0.0
    for i in range(kernel_dim):
        for j in range(kernel_dim):
            x_val, y_val = x + i - pad, y + j - pad
            if picture.valid_index(x_val, y_val):
                pixel += kernel[i, j] * picture[x_val, y_val]
            else:
                pixel += kernel[i, j] * cval
    return pixel
def convolve_voxel(picture: CachingDataStructure, kernel: np.ndarray,
                   kernel_dim: int, pad: int,
                   x: int, y: int, z: int, cval: float) -> float:
    """Performs spatial convolution at a single voxel in a 3D image"""
    voxel = 0.0
    for i in range(kernel_dim):
        for j in range(kernel_dim):
            for h in range(kernel_dim):
                x_val, y_val, z_val = x + i - pad, y + j - pad, z + h - pad
                if picture.valid_index(x_val, y_val, z_val):
                    voxel += kernel[i, j, h] * picture[x_val, y_val, z_val]
                else:
                    voxel += kernel[i, j, h] * cval
    return voxel
Ejemplo n.º 7
0
def safe_get_times_general(times: CachingDataStructure, key: tuple) -> float:
    """
    Returns the saved time value in an n-dimensional array if is a valid index,
    otherwise infinity
    """
    return INF if not times.valid_index(*key) else times[key]