a b/utils/utils.py
1
import numpy as np
2
import scipy.stats as st
3
4
5
def cutCube(X, center, shape, padd=0): #center is a 3d coord (zyx)
6
    center = center.astype(int)
7
    hlz = np.round(shape[0] / 2)
8
    hly = np.round(shape[1] / 2)
9
    hlx = np.round(shape[2] / 2)
10
11
    #add padding if out of bounds
12
    if ((center - np.array([hlz,hly,hlx])) < 0).any() or (
13
        (center + np.array([hlz,hly,hlx]) + 1) > np.array(X.shape)).any():  # if cropping is out of bounds, add padding
14
        Xn = np.ones(np.array(X.shape) + shape * 2) * padd
15
        Xn[shape[0]:(shape[0] + X.shape[0]), shape[1]:(shape[1] + X.shape[1]), shape[2]:(shape[2] + X.shape[2])] = X
16
        centern = center + shape
17
        cube = Xn[int(centern[0] - hlz):int(centern[0] - hlz + shape[0]),
18
               int(centern[1] - hly):int(centern[1] - hly + shape[1]),
19
               int(centern[2] - hlx):int(centern[2] - hlx + shape[2])]
20
        return np.copy(cube)
21
    else:
22
        cube = X[int(center[0] - hlz):int(center[0] - hlz + shape[0]), int(center[1] - hly):int(center[1] - hly + shape[1]),
23
               int(center[2] - hlx):int(center[2] - hlx + shape[2])]
24
        return np.copy(cube)
25
26
27
def pasteCube(X, cube, center, padd=0):  #center is a 3d coord (zyx)
28
    center = center.astype(int)
29
    hlz = np.round(cube.shape[0] / 2)
30
    hlx = np.round(cube.shape[1] / 2)
31
    hly = np.round(cube.shape[2] / 2)
32
    Xn = np.copy(X)
33
34
    #add padding if out of bounds
35
    if ((center - hlz) < 0).any() or ((center - hlx) < 0).any() or ((center - hly) < 0).any()  or ((center + hlz + 1) > np.array(X.shape)).any() or ((center + hlx + 1) > np.array(X.shape)).any() or ((center + hly + 1) > np.array(X.shape)).any():  # if cropping is out of bounds, add padding
36
        Xn = np.ones(np.array(X.shape) + np.max(cube.shape) * 2) * padd
37
        Xn[cube.shape[0]:(cube.shape[0] + X.shape[0]), cube.shape[1]:(cube.shape[1] + X.shape[1]), cube.shape[2]:(cube.shape[2] + X.shape[2])] = X
38
        center = center + np.array(cube.shape)
39
        Xn[int(center[0] - hlz):int(center[0] - hlz + cube.shape[0]), int(center[1] - hlx):int(center[1] - hlx + cube.shape[1]),
40
        int(center[2] - hly):int(center[2] - hly + cube.shape[2])] = cube
41
        Xn = Xn[cube.shape[0]:(Xn.shape[0] - cube.shape[0]), cube.shape[1]:(Xn.shape[1] - cube.shape[1]), cube.shape[2]:(Xn.shape[2] - cube.shape[2])]
42
    else:
43
        Xn[int(center[0] - hlz):int(center[0] - hlz + cube.shape[0]), int(center[1] - hlx):int(center[1] - hlx + cube.shape[1]),
44
        int(center[2] - hly):int(center[2] - hly + cube.shape[2])] = cube
45
    return Xn
46
47
def gkern(kernlen=21, nsig=3):
48
    """Returns a 2D Gaussian kernel array."""
49
50
    interval = (2*nsig+1.)/(kernlen)
51
    x = np.linspace(-nsig-interval/2., nsig+interval/2., kernlen+1)
52
    kern1d = np.diff(st.norm.cdf(x))
53
    kernel_raw = np.sqrt(np.outer(kern1d, kern1d))
54
    kernel = kernel_raw/kernel_raw.sum()
55
    return kernel
56
57
def kern01(kernlen,nsig):
58
    k = gkern(kernlen, nsig)
59
    return (k - np.min(k)) / (np.max(k) - np.min(k))
60
61
def sigmoid(x, derivative=False):
62
    sigm = 1. / (1. + np.exp(-x))
63
    if derivative:
64
        return sigm * (1. - sigm)
65
    return sigm