def scatter_data_test(showFigs=True):
    I = plt.imread('../data/dataset_brains/1_1_t1.tif')
    X1 = I.flatten().T
    X1 = X1.reshape(-1, 1)
    GT = plt.imread('../data/dataset_brains/1_1_gt.tif')
    gt_mask = GT > 0
    Y = gt_mask.flatten()  # labels

    I_blurred = ndimage.gaussian_filter(I, sigma=2)
    X2 = I_blurred.flatten().T
    X2 = X2.reshape(-1, 1)

    X_data = np.concatenate((X1, X2), axis=1)
    features = ('T1 intensity', 'T1 gauss 2'
                )  # Keep track of features you added
    if showFigs:
        util.scatter_data(X_data, Y, 0, 1)

    #------------------------------------------------------------------#
    # Implement a few test cases of with different features
    I_blurred = ndimage.gaussian_filter(I, sigma=10)
    X3 = I_blurred.flatten().T
    X3 = X3.reshape(-1, 1)
    I_blurred = ndimage.gaussian_filter(I, sigma=20)
    X4 = I_blurred.flatten().T
    X4 = X4.reshape(-1, 1)
    X_data = np.concatenate((X1, X2, X3, X4), axis=1)
    if showFigs:
        util.scatter_data(X_data, Y, 0, 1)
    #------------------------------------------------------------------#
    return X_data, Y
Esempio n. 2
0
def scatter_all(DM, LM, FL):
    # scatter 2 features and their correspoding labels
    # DM is matrix with all train_data
    # LM is matrix with all labels_data
    # FL is array with all feature names

    row, column, subject = DM.shape
    subject = 5

    fig, axs = plt.subplots(nrows=column,
                            ncols=column,
                            figsize=(column * 5, column * 5),
                            sharex='all',
                            sharey='all')

    for i in range(column):
        for j in range(column):
            ax = axs[i, j]
            ax.grid(True)
            ax.set_xlim(-2, 2)
            ax.set_ylim(-2, 2)
            # ax.legend(['Background', 'White matter', 'Grey matter', 'CSF'])
            for im in range(subject):
                X = DM[:, :, im]
                Y = LM[:, im]
                # if i == j:
                #     ax.hist(X, bins=100)
                # else:
                util.scatter_data(X, Y, feature0=i, feature1=j, ax=ax)
                if i == column - 1:
                    axs[i, j].set_xlabel(FL[j].format(j + 1), fontsize=18)
                if j == 0:
                    axs[i, j].set_ylabel(FL[i].format(i + 1), fontsize=18)

    plt.show()
Esempio n. 3
0
def scatter_t2_test(showFigs=True):
    I1 = plt.imread('../data/dataset_brains/1_1_t1.tif')
    X1 = I1.flatten().T
    X1 = X1.reshape(-1, 1)

    I2 = plt.imread('../data/dataset_brains/1_1_t2.tif')
    X2 = I2.flatten().T
    X2 = X2.reshape(-1, 1)

    GT = plt.imread('../data/dataset_brains/1_1_gt.tif')
    gt_mask = GT>0
    Y = gt_mask.flatten() # labels

    I1_blurred = ndimage.gaussian_filter(I1, sigma=4)
    X12 = I1_blurred.flatten().T
    X12 = X12.reshape(-1, 1)

    X_data = np.concatenate((X1, X12), axis=1)
    features = ('T1 intensity', 'T1 gauss 2') # Keep track of features you added
    if showFigs:
        util.scatter_data(X_data,Y,0,1)

    #------------------------------------------------------------------#
    # TODO: Extract features from the T2 image and compare them to the T1 features
    #------------------------------------------------------------------#
    return X_data, Y
def easy_hard_data_classifier_test():
    #-------------------------------------------------------------------#
    # generate and classify (using nn_classifier) 2 pairs of datasets (easy and hard)
    # calculate classification error in each case
    trainX, trainY, testX, testY = generate_train_test(2, task='easy')
    util.scatter_data(trainX, trainY)

    D = scipy.spatial.distance.cdist(testX, trainX, metric='euclidean')
    min_index = np.argmin(D, axis=1)
    predicted_labels = trainY[min_index]
    err = util.classification_error(testY, predicted_labels)

    print('True labels:\n{}'.format(testY))
    print('Predicted labels:\n{}'.format(predicted_labels))
    print('Error:\n{}'.format(err))

    trainX, trainY, testX, testY = generate_train_test(2, task='hard')
    util.scatter_data(trainX, trainY)

    D = scipy.spatial.distance.cdist(testX, trainX, metric='euclidean')
    min_index = np.argmin(D, axis=1)
    predicted_labels = trainY[min_index]
    err = util.classification_error(testY, predicted_labels)

    print('True labels:\n{}'.format(testY))
    print('Predicted labels:\n{}'.format(predicted_labels))
    print('Error:\n{}'.format(err))
    #-------------------------------------------------------------------#
    pass
def test_mypca():

    #Generates some toy data in 2D, computes PCA, and plots both datasets
    N = 100
    mu1 = [0, 0]
    mu2 = [2, 0]
    sigma1 = [[2, 1], [1, 1]]
    sigma2 = [[2, 1], [1, 1]]

    XG, YG = seg.generate_gaussian_data(N, mu1, mu2, sigma1, sigma2)

    fig = plt.figure(figsize=(15, 6))
    ax1 = fig.add_subplot(121)

    util.scatter_data(XG, YG, ax=ax1)
    sigma = np.cov(XG, rowvar=False)
    w, v = np.linalg.eig(sigma)
    ax1.plot([0, v[0, 0]], [0, v[1, 0]],
             c='g',
             linewidth=3,
             label='Eigenvector1')
    ax1.plot([0, v[0, 1]], [0, v[1, 1]],
             c='k',
             linewidth=3,
             label='Eigenvector2')
    ax1.set_title('Original data')
    ax_settings(ax1)

    ax2 = fig.add_subplot(122)
    X_pca, v, w, fraction_variance = seg.mypca(XG)
    util.scatter_data(X_pca, YG, ax=ax2)
    sigma2 = np.cov(X_pca, rowvar=False)
    w2, v2 = np.linalg.eig(sigma2)
    ax2.plot([0, v2[0, 0]], [0, v2[1, 0]],
             c='g',
             linewidth=3,
             label='Eigenvector1')
    ax2.plot([0, v2[0, 1]], [0, v2[1, 1]],
             c='k',
             linewidth=3,
             label='Eigenvector2')
    ax2.set_title('My PCA')
    ax_settings(ax2)

    handles, labels = ax2.get_legend_handles_labels()
    plt.figlegend(handles,
                  labels,
                  loc='upper center',
                  bbox_to_anchor=(0.5, -0.05),
                  bbox_transform=plt.gcf().transFigure,
                  ncol=4)

    print(fraction_variance)
def feature_stats_test():
    X, Y = scatter_data_test(showFigs=False)
    I = plt.imread('../data/dataset_brains/1_1_t1.tif')
    c, coord_im = seg.extract_coordinate_feature(I)
    X_data = np.concatenate((X, c), axis=1)
    #------------------------------------------------------------------#
    # Write code to examine the mean and standard deviation of your dataset containing variety of features

    means = np.zeros(X_data.shape[1])
    stds = np.zeros(X_data.shape[1])
    for i in range(X_data.shape[1]):
        means[i] = np.mean(X_data[:, i])
        stds[i] = np.std(X_data[:, i])
    util.scatter_data(X_data, Y, 0, 1)
    print(means)
    print(stds)
def minimum_distance_test(X, Y, C, D):
    #------------------------------------------------------------------#
    #  plot the datasets on top of each other in different colors and visualize the data,
    #  calculate the distances between the datasets,
    #  order the distances (min to max) using the provided code,
    #  calculate how many samples are closest to each of the samples in `C`

    util.scatter_data(X, Y, 0, 1)
    plt.scatter(C[:,0],C[:,1])
    min_index = np.argmin(D, axis=1)
    min_dist = D[:, min_index]
    print('D',D)
    print('min_index', min_index)
    print('min_dist', min_dist)
    print('mindist', min_dist.diagonal())
    print(C[:,0], (min_index == 0).sum())
    print(C[:,1], (min_index == 1).sum())
def kmeans_clustering_test():
    #------------------------------------------------------------------#
    #TODO: Store errors for training data
    X_data, Y, feature_labels_train = util.create_dataset(1, 1, 'brain')
    predicted_labels = seg.kmeans_clustering(X_data, K=2)

    I = plt.imread('../data/dataset_brains/1_1_t1.tif')
    GT = plt.imread('../data/dataset_brains/1_1_gt.tif')
    gt_mask = GT > 0
    gt_labels = gt_mask.flatten()  # labels
    predicted_mask = predicted_labels.reshape(I.shape)

    fig = plt.figure(figsize=(15, 5))
    ax1 = fig.add_subplot(131)
    ax1.imshow(I)
    ax2 = fig.add_subplot(132)
    ax2.imshow(predicted_mask)
    ax3 = fig.add_subplot(133)
    ax3.imshow(gt_mask)
    util.scatter_data(X_data, predicted_labels, 0, 1)
Esempio n. 9
0
def scatter_data_test(showFigs=True):
    I = plt.imread('../data/dataset_brains/1_1_t1.tif')
    X1 = I.flatten().T
    X1 = X1.reshape(-1, 1)
    GT = plt.imread('../data/dataset_brains/1_1_gt.tif')
    gt_mask = GT>0.5
    Y = gt_mask.flatten() # labels

    I_blurred = ndimage.gaussian_filter(I, sigma=2)
    X2 = I_blurred.flatten().T
    X2 = X2.reshape(-1, 1)

    X_data = np.concatenate((X1, X2), axis=1)
    features = ('T1 intensity', 'T1 gauss 2') # Keep track of features you added
    if showFigs:
        util.scatter_data(X_data,Y,0,1)

    #------------------------------------------------------------------#
    # TODO: Implement a few test cases of with different features
    features += ('T1 gauss 5',)
    features += ('T1 gauss 10',)
    features += ('T1 gauss 15',)
    I_blurred_5 = ndimage.gaussian_filter(I, sigma=5)
    X5 = I_blurred_5.flatten().T
    X5 = X5.reshape(-1, 1)
    
    I_blurred_10 = ndimage.gaussian_filter(I, sigma=10)
    X10 = I_blurred_10.flatten().T
    X10 = X10.reshape(-1, 1)
    
    I_blurred_15 = ndimage.gaussian_filter(I, sigma=15)
    X15 = I_blurred_15.flatten().T
    X15 = X15.reshape(-1, 1)
    
    X_data = np.concatenate((X1, X2, X5, X10, X15), axis=1)
    if showFigs: 
        util.scatter_data(X_data,Y,1,3)
    
    
    #------------------------------------------------------------------#
    return X_data, Y
Esempio n. 10
0
def scatter_all(DM, LM, FL):
    # scatter 2 features and their corresponding labels
    # DM is matrix with all train_data
    # LM is matrix with all labels_data
    # FL is array with all feature names

    row, column, subject = DM.shape

    fig, axs = plt.subplots(nrows=column,
                            ncols=column,
                            figsize=(column * 5, column * 5))

    # Make array with appended data from all subjects
    X = DM[:, :, 0]
    Y = LM[:, 0]
    for im in range(1, subject):
        X = np.append(X, DM[:, :, im], axis=0)
        Y = np.append(Y, LM[:, im], axis=0)

    # Now, loop over figures
    for i in range(column):
        for j in range(column):
            ax = axs[i, j]
            ax.grid(True)

            # Plot stacked histograms on diagonals
            if i == j:
                ax.hist(X[:, i], bins=50, density=True)

            # Scatter features on remaining subplots
            else:
                util.scatter_data(X, Y, feature0=i, feature1=j, ax=ax)

            # Set labels on outside plots for readability
            if i == column - 1:
                axs[i, j].set_xlabel(FL[j].format(j + 1), fontsize=18)
            if j == 0:
                axs[i, j].set_ylabel(FL[i].format(i + 1), fontsize=18)

    plt.show()
Esempio n. 11
0
def initialize_cluster_centers(N=100, num_clusters=2):
    # Generate 100 samples per Gaussian class
    X, Y = seg.generate_gaussian_data(N)

    # Select num_clusters rows from X and store in w_initial
    start = np.random.randint(0, 98)
    n_cluster_rows = X[start:(start + num_clusters), :]
    w_initial = np.array(n_cluster_rows)

    ax1 = util.scatter_data(X, Y)
    ax1.scatter(w_initial[:, 0], w_initial[:, 1], c='y')
    plt.show()

    return X, w_initial
Esempio n. 12
0
def minimum_distance_test(X, Y, C, D):
    #------------------------------------------------------------------#
    # TODO: plot the datasets on top of each other in different colors and visualize the data,
    #  calculate the distances between the datasets,
    #  order the distances (min to max) using the provided code,
    #  calculate how many samples are closest to each of the samples in `C`

    ax1 = util.scatter_data(X, Y)
    ax1.scatter(C[:, 0], C[:, 1], c='y')
    plt.show()

    min_index = np.argmin(D, axis=1)
    min_dist = np.min(D, axis=1)
    min_dist = np.asarray([min_dist])
    min_dist = min_dist.T
    print(min_dist)
Esempio n. 13
0
def scatter_data_test(showFigs=True):
    I = plt.imread('../data/dataset_brains/1_1_t1.tif')
    X1 = I.flatten().T
    X1 = X1.reshape(-1, 1)
    GT = plt.imread('../data/dataset_brains/1_1_gt.tif')
    gt_mask = GT > 0
    Y = gt_mask.flatten()  # labels

    I_blurred = ndimage.gaussian_filter(I, sigma=2)
    X2 = I_blurred.flatten().T
    X2 = X2.reshape(-1, 1)

    X_data = np.concatenate((X1, X2), axis=1)

    features = ('T1 intensity', 'T1 gauss 2')  # Keep track of features you added
    if showFigs:
        ax = util.scatter_data(X_data, Y, 0, 1)
        return X_data, Y

    return X_data, Y
def distance_classification_test():
    #------------------------------------------------------------------#
    # Use the provided code to generate training and testing data
    #  Classify the points in test_data, based on their distances d to the points in train_data
    train_data, train_labels = seg.generate_gaussian_data(2);
    test_data, test_labels = seg.generate_gaussian_data(1);
    # train_data=np.array([[1,1],[0,0],[0,1],[1,0]]);
    # train_labels=np.array([[0],[1],[0],[1]])
    util.scatter_data(train_data, train_labels, 0, 1)
    util.scatter_data(test_data, test_labels, 0, 1)

    D = scipy.spatial.distance.cdist(test_data, train_data, metric='euclidean')
    min_index = np.argmin(D, axis=1)
    newlabels=train_labels[min_index]

    print(test_data)
    print(newlabels)
    util.scatter_data(test_data,newlabels,0,1)
Esempio n. 15
0
def PCA_features_demo():
    task = 'tissue'
    n = 5
    all_subjects = np.arange(n)
    train_slice = 1
    tmp_data, tmp_labels, tmp_feature_labels = util.create_dataset(
        1, train_slice, task)
    all_data_matrix = np.empty((tmp_data.shape[0], tmp_data.shape[1], n))
    all_labels_matrix = np.empty((tmp_labels.shape[0], n))

    # Load all datasets once and compile in all_data_matrix
    for i in all_subjects:
        train_data, train_labels, train_feature_labels = util.create_dataset(
            i + 1, train_slice, task)
        all_data_matrix[:, :, i] = train_data
        all_labels_matrix[:, i] = train_labels.ravel()

    # Perform PCA and plots the first patient's features against each other
    # Also calculates tha amount of features needed to account for at least 95% of the variance

    X_pca, v, w, fraction_variance = seg.mypca(all_data_matrix[:, :, 0])

    fig = plt.figure(figsize=(40, 40))
    plot = 1
    for i in range(8):
        for j in range(8):
            ax = plt.subplot(8, 8, plot)
            ax = util.scatter_data(X_pca,
                                   all_labels_matrix[:, 0],
                                   feature0=i,
                                   feature1=j,
                                   ax=ax)
            plot += 1

    needed_features = np.sum((fraction_variance < 0.95).astype(int)) + 1
    print("needed features to account for 95% of variance: {0}".format(
        needed_features))
Esempio n. 16
0
def scatter_data_test(showFigs=True):
    I = plt.imread('../data/dataset_brains/1_1_t1.tif')
    X1 = I.flatten().T
    X1 = X1.reshape(-1, 1)
    GT = plt.imread('../data/dataset_brains/1_1_gt.tif')
    gt_mask = GT > 0
    Y = gt_mask.flatten()  # labels

    I_blurred = ndimage.gaussian_filter(I, sigma=2)
    X2 = I_blurred.flatten().T
    X2 = X2.reshape(-1, 1)

    X_data = np.concatenate((X1, X2), axis=1)
    features = ('T1 intensity', 'T1 gauss 2'
                )  # Keep track of features you added
    if showFigs:
        util.scatter_data(X_data, Y, 0, 1)

    #------------------------------------------------------------------#
    # TODO: Implement a few test cases of with different features
    I_blurred = ndimage.gaussian_filter(
        I, sigma=5
    )  #nieuwe feature gemaakt door andere sigma in gaussische filter te gebruiken
    X3 = I_blurred.flatten().T
    X3 = X3.reshape(-1, 1)

    I_blurred = ndimage.gaussian_filter(I, sigma=10)
    X4 = I_blurred.flatten().T
    X4 = X4.reshape(-1, 1)

    X_data = np.concatenate((X_data, X3), axis=1)
    X_data = np.concatenate((X_data, X4), axis=1)

    if showFigs:
        util.scatter_data(X_data, Y, 1, 2)  #plot feature 2 tegen 3
        util.scatter_data(X_data, Y, 2, 3)  #plot feature 3 tegen 4

    #------------------------------------------------------------------#
    return X_data, Y
Esempio n. 17
0
def scatter_t2_test(showFigs=True):
    I1 = plt.imread('../data/dataset_brains/1_1_t1.tif')
    X1 = I1.flatten().T
    X1 = X1.reshape(-1, 1)

    I2 = plt.imread('../data/dataset_brains/1_1_t2.tif')
    X2 = I2.flatten().T
    X2 = X2.reshape(-1, 1)

    GT = plt.imread('../data/dataset_brains/1_1_gt.tif')
    gt_mask = GT>0
    Y = gt_mask.flatten() # labels

    I1_blurred = ndimage.gaussian_filter(I1, sigma=4)
    X12 = I1_blurred.flatten().T
    X12 = X12.reshape(-1, 1)

    X_data = np.concatenate((X1, X12), axis=1)
    features = ('T1 intensity', 'T1 gauss 2') # Keep track of features you added
#    if showFigs:
#        util.scatter_data(X_data,Y,0,1)

    #------------------------------------------------------------------#
    # TODO: Extract features from the T2 image and compare them to the T1 features
    features += ('T1 gauss 5',)
    features += ('T1 gauss 10',)
    features += ('T1 gauss 0',)
    
    features +=('T2 intensity',)
    features += ('T2 gauss 2',)
    features += ('T2 gauss 5',)
    features += ('T2 gauss 10',)
    features += ('T2 gauss 0',)
    
    #features for I1
    I_blurred_5 = ndimage.gaussian_filter(I1, sigma=5)
    X5 = I_blurred_5.flatten().T
    X5 = X5.reshape(-1, 1)
    
    I_blurred_10 = ndimage.gaussian_filter(I1, sigma=10)
    X10 = I_blurred_10.flatten().T
    X10 = X10.reshape(-1, 1)
    
    I_blurred_15 = ndimage.gaussian_filter(I1, sigma=0)
    X15 = I_blurred_15.flatten().T
    X15 = X15.reshape(-1, 1)
    
    #features for I2
    I2_blurred = ndimage.gaussian_filter(I2, sigma=4)
    X22 = I2_blurred.flatten().T
    X22 = X22.reshape(-1, 1)

    I_blurred_5_2 = ndimage.gaussian_filter(I2, sigma=5)
    X5_2 = I_blurred_5_2.flatten().T
    X5_2 = X5_2.reshape(-1, 1)
    
    I_blurred_10_2 = ndimage.gaussian_filter(I2, sigma=10)
    X10_2 = I_blurred_10_2.flatten().T
    X10_2 = X10_2.reshape(-1, 1)
    
    I_blurred_15_2 = ndimage.gaussian_filter(I2, sigma=0)
    X15_2 = I_blurred_15_2.flatten().T
    X15_2 = X15_2.reshape(-1, 1)
    
#    X_data_all = np.concatenate((X1, X12, X5, X10, X15, 
  #                           X2, X22, X5_2, X10_2, X15_2), axis=1)
    X_data_all = np.concatenate((X1, X2), axis=1)
    
    util.scatter_data(X_data_all,Y,0,1) #X1 and X2
        
    
    #------------------------------------------------------------------#
    return X_data, Y
Esempio n. 18
0
def logistic_regression():
    # dataset preparation
    num_training_samples = 300
    num_validation_samples = 100

    # here we reuse the function from the segmentation practicals
    m1 = [2, 3]
    m2 = [-0, -4]
    s1 = [[8, 7], [7, 8]]
    s2 = [[8, 6], [6, 8]]

    [trainingX,
     trainingY] = seg.generate_gaussian_data(num_training_samples, m1, m2, s1,
                                             s2)
    r, c = trainingX.shape
    print('Training sample shape: {}'.format(trainingX.shape))

    # we need a validation set to monitor for overfitting
    [validationX,
     validationY] = seg.generate_gaussian_data(num_validation_samples, m1, m2,
                                               s1, s2)
    r_val, c_val = validationX.shape
    print('Validation sample shape: {}'.format(validationX.shape))

    validationXones = util.addones(validationX)

    # train a logistic regression model:
    # the learning rate for the gradient descent method
    # (the same as in intensity-based registration)
    mu = 0.001

    # we are actually using stochastic gradient descent
    batch_size = 30

    # initialize the parameters of the model with small random values,
    # we need one parameter for each feature and a bias
    Theta = 0.02 * np.random.rand(c + 1, 1)

    # number of gradient descent iterations
    num_iterations = 300

    # variables to keep the loss and gradient at every iteration
    # (needed for visualization)
    iters = np.arange(num_iterations)
    loss = np.full(iters.shape, np.nan)
    validation_loss = np.full(iters.shape, np.nan)

    # Create base figure
    fig = plt.figure(figsize=(15, 8))
    ax1 = fig.add_subplot(121)
    im1, Xh_ones, num_range_points = util.plot_lr(trainingX, trainingY, Theta,
                                                  ax1)
    seg_util.scatter_data(trainingX, trainingY, ax=ax1)
    ax1.grid()
    ax1.set_xlabel('x_1')
    ax1.set_ylabel('x_2')
    ax1.legend()
    ax1.set_title('Training set')
    text_str1 = '{:.4f};  {:.4f};  {:.4f}'.format(0, 0, 0)
    txt1 = ax1.text(0.3,
                    0.95,
                    text_str1,
                    bbox={
                        'facecolor': 'white',
                        'alpha': 1,
                        'pad': 10
                    },
                    transform=ax1.transAxes)

    ax2 = fig.add_subplot(122)
    ax2.set_xlabel('Iteration')
    ax2.set_ylabel('Loss (average per sample)')
    ax2.set_title('mu = ' + str(mu))
    h1, = ax2.plot(iters, loss, linewidth=2, label='Training loss')
    h2, = ax2.plot(iters,
                   validation_loss,
                   linewidth=2,
                   label='Validation loss')
    ax2.set_ylim(0, 0.7)
    ax2.set_xlim(0, num_iterations)
    ax2.grid()
    ax1.legend()

    text_str2 = 'iter.: {}, loss: {:.3f}, val. loss: {:.3f}'.format(0, 0, 0)
    txt2 = ax2.text(0.3,
                    0.95,
                    text_str2,
                    bbox={
                        'facecolor': 'white',
                        'alpha': 1,
                        'pad': 10
                    },
                    transform=ax2.transAxes)

    # iterate
    for k in np.arange(num_iterations):
        # pick a batch at random
        idx = np.random.randint(r, size=batch_size)

        # the loss function for this particular batch
        loss_fun = lambda Theta: cad.lr_nll(util.addones(trainingX[idx, :]),
                                            trainingY[idx], Theta)

        # gradient descent:
        # here we reuse the code for numerical computation of the gradient
        # of a function
        Theta = Theta - mu * reg.ngradient(loss_fun, Theta)

        # compute the loss for the current model parameters for the
        # training and validation sets
        # note that the loss is divided with the number of samples so
        # it is comparable for different number of samples
        loss[k] = loss_fun(Theta) / batch_size
        validation_loss[k] = cad.lr_nll(validationXones, validationY,
                                        Theta) / r_val

        # upldate the visualization
        ph = cad.sigmoid(Xh_ones.dot(Theta)) > 0.5
        decision_map = ph.reshape(num_range_points, num_range_points)
        decision_map_trns = np.flipud(decision_map)
        im1.set_data(decision_map_trns)
        text_str1 = '{:.4f};  {:.4f};  {:.4f}'.format(Theta[0, 0], Theta[1, 0],
                                                      Theta[2, 0])
        txt1.set_text(text_str1)
        h1.set_ydata(loss)
        h2.set_ydata(validation_loss)
        text_str2 = 'iter.={}, loss={:.3f}, val. loss={:.3f} '.format(
            k, loss[k], validation_loss[k])
        txt2.set_text(text_str2)

        display(fig)
        clear_output(wait=True)
Esempio n. 19
0
def segmentation_demo():
    #only SECTION 2 is needed for what we want to do
    train_subject = 1
    test_subject = 2
    train_slice = 1
    test_slice = 1
    task = 'tissue'
    #SECTION 1 (this seciton has nothing to do with SECTION 2)

    #Load data from a train and testsubject
    train_data, train_labels, train_feature_labels = util.create_dataset(
        train_subject, train_slice, task)
    test_data, test_labels, test_feature_labels = util.create_dataset(
        test_subject, test_slice, task)

    util.scatter_data(train_data, train_labels, 0, 6)
    util.scatter_data(test_data, test_labels, 0, 6)

    predicted_labels = seg.segmentation_atlas(None, train_labels, None)

    err = util.classification_error(test_labels, predicted_labels)
    dice = util.dice_overlap(test_labels, predicted_labels)

    #Display results
    true_mask = test_labels.reshape(240, 240)
    predicted_mask = predicted_labels.reshape(240, 240)

    fig = plt.figure(figsize=(8, 8))
    ax1 = fig.add_subplot(111)
    ax1.imshow(true_mask, 'gray')
    ax1.imshow(predicted_mask, 'viridis', alpha=0.5)
    print('Subject {}, slice {}.\nErr {}, dice {}'.format(
        test_subject, test_slice, err, dice))

    ## SECTION 2:Compare methods
    num_images = 5
    num_methods = 3
    im_size = [240, 240]

    all_errors = np.empty([num_images, num_methods])
    all_errors[:] = np.nan
    all_dice = np.empty([num_images, num_methods])
    all_dice[:] = np.nan

    all_subjects = np.arange(5)  #list of all subjects [0, 1, 2, 3, 4]
    train_slice = 2
    task = 'tissue'
    all_data_matrix = np.empty(
        [train_data.shape[0], train_data.shape[1], num_images])
    all_labels_matrix = np.empty([train_labels.size, num_images])

    #Load datasets once
    print('Loading data for ' + str(num_images) + ' subjects...')

    for i in all_subjects:
        sub = i + 1
        train_data, train_labels, train_feature_labels = util.create_dataset(
            sub, train_slice, task)
        all_data_matrix[:, :, i] = train_data
        all_labels_matrix[:, i] = train_labels.flatten()

    print('Finished loading data.\nStarting segmentation...')

    #Go through each subject, taking i-th subject as the test
    for i in all_subjects:
        sub = i + 1
        #Define training subjects as all, except the test subject
        train_subjects = all_subjects.copy()
        train_subjects = np.delete(train_subjects, i)

        train_data_matrix = all_data_matrix[:, :, train_subjects]
        train_labels_matrix = all_labels_matrix[:, train_subjects]
        test_data = all_data_matrix[:, :, i]
        test_labels = all_labels_matrix[:, i]
        test_shape_1 = test_labels.reshape(im_size[0], im_size[1])

        fig = plt.figure(figsize=(15, 5))

        predicted_labels, predicted_labels2 = seg.segmentation_combined_atlas(
            train_labels_matrix)
        all_errors[i, 0] = util.classification_error(test_labels,
                                                     predicted_labels2)
        all_dice[i, 0] = util.dice_multiclass(test_labels, predicted_labels2)
        predicted_mask_1 = predicted_labels2.reshape(im_size[0], im_size[1])
        ax1 = fig.add_subplot(131)
        ax1.imshow(test_shape_1, 'gray')
        ax1.imshow(predicted_mask_1, 'viridis', alpha=0.5)
        text_str = 'Err {:.4f}, dice {:.4f}'.format(all_errors[i, 0],
                                                    all_dice[i, 0])
        ax1.set_xlabel(text_str)
        ax1.set_title('Subject {}: Combined atlas'.format(sub))

        predicted_labels, predicted_labels2 = seg.segmentation_combined_knn(
            train_data_matrix, train_labels_matrix, test_data)
        all_errors[i, 1] = util.classification_error(test_labels,
                                                     predicted_labels2)
        all_dice[i, 1] = util.dice_multiclass(test_labels, predicted_labels2)
        predicted_mask_2 = predicted_labels2.reshape(im_size[0], im_size[1])
        ax2 = fig.add_subplot(132)
        ax2.imshow(test_shape_1, 'gray')
        ax2.imshow(predicted_mask_2, 'viridis', alpha=0.5)
        text_str = 'Err {:.4f}, dice {:.4f}'.format(all_errors[i, 1],
                                                    all_dice[i, 1])
        ax2.set_xlabel(text_str)
        ax2.set_title('Subject {}: Combined k-NN'.format(sub))

        #OUR METHOD
        #predict the labels using our method
        predicted_labels_mymethod = segmentation_mymethod(train_data_matrix,
                                                          train_labels_matrix,
                                                          test_data,
                                                          num_iter=100,
                                                          mu=0.1)

        #determine error and dice (multiclass, since there are more classes)
        all_errors[i,
                   2] = util.classification_error(test_labels,
                                                  predicted_labels_mymethod)
        all_dice[i, 2] = util.dice_multiclass(test_labels,
                                              predicted_labels_mymethod)

        #reshape the predicted labels in order to plot the results
        predicted_mask_3 = predicted_labels_mymethod.reshape(
            im_size[0], im_size[1])

        #plot the predicted image over the real image
        plt.imshow(predicted_mask_3, 'viridis')
        ax3 = fig.add_subplot(133)
        ax3.imshow(test_shape_1, 'gray')
        ax3.imshow(predicted_mask_3, 'viridis', alpha=0.5)
        text_str = 'Err {:.4f}, dice {:.4f}'.format(all_errors[i, 2],
                                                    all_dice[i, 2])
        ax3.set_xlabel(text_str)
        ax3.set_title('Subject {}: My method'.format(sub))

        #save the figure after every loop (3 subimages/plots)
        fig.savefig("Results for test subject {}".format(sub), )
Esempio n. 20
0
def kmeans(X, labels, num_iter, mu=0.1):
    #    X,_ = seg.normalize_data(X)
    N, M = X.shape
    #Define number of clusters we want
    clusters = 4

    # Cost function used by k-Means
    # fun = lambda w: seg.cost_kmeans(X,w)
    fun = funX(X)

    ## Algorithm
    #Initialize cluster centers
    idx = np.random.randint(N, size=clusters)
    initial_w = X[idx, :]
    w_draw = initial_w
    print(w_draw)

    #Reshape into vector (needed by ngradient)
    w_vector = initial_w.reshape(clusters * M, 1)

    #Vector to store cost
    xx = np.linspace(1, num_iter, num_iter)
    kmeans_cost = np.empty(*xx.shape)
    kmeans_cost[:] = np.nan

    fig = plt.figure(figsize=(14, 6))
    ax1 = fig.add_subplot(121)
    util.scatter_data(X, labels, ax=ax1)

    line1, = ax1.plot(w_draw[:, 0],
                      w_draw[:, 1],
                      "k*",
                      markersize=10,
                      label='W-vector')
    # im3  = ax1.scatter(w_draw[:,0], w_draw[:,1])
    ax1.grid()

    ax2 = fig.add_subplot(122, xlim=(0, num_iter), ylim=(0, 10))

    text_str = 'k={}, g={:.2f}\ncost={:.2f}'.format(0, 0, 0)

    txt2 = ax2.text(0.3,
                    0.95,
                    text_str,
                    bbox={
                        'facecolor': 'green',
                        'alpha': 0.4,
                        'pad': 10
                    },
                    transform=ax2.transAxes)

    #     xx = xx.reshape(1,-1)
    line2, = ax2.plot(xx, kmeans_cost, lw=2)
    ax2.set_xlabel('Iteration')
    ax2.set_ylabel('Cost')
    ax2.grid()

    for k in np.arange(num_iter):

        # gradient ascent
        g = util.ngradient(fun, w_vector)
        w_vector = w_vector - mu * g.T
        # calculate cost for plotting
        kmeans_cost[k] = fun(w_vector)
        text_str = 'k={}, cost={:.2f}'.format(k, kmeans_cost[k])
        txt2.set_text(text_str)
        # plot
        line2.set_ydata(kmeans_cost)
        w_draw_new = w_vector.reshape(clusters, M)
        line1.set_data(w_draw_new[:, 0], w_draw_new[:, 1])
        #        display(fig)
        clear_output(wait=True)
        plt.pause(.005)
    display(fig)
    # TODO: Find distance of each point to each cluster center
    # Then find the minimum distances min_dist and indices min_index
    w_final = w_vector.reshape(clusters, M)

    D = scipy.spatial.distance.cdist(
        X, w_final, metric='euclidean')  #distances between X and C
    min_index = np.argmin(D, axis=1)
    min_dist = np.zeros((len(min_index), 1))
    for i in range(len(min_index)):
        min_dist[i, 0] = D.item((i, min_index[i]))
    # Sort by intensity of cluster center
    sorted_order = np.argsort(w_final[:, 0], axis=0)

    # Update the cluster indices based on the sorted order and return results in
    # predicted_labels
    predicted_labels = np.empty(*min_index.shape)
    predicted_labels[:] = np.nan

    for i in np.arange(len(sorted_order)):
        predicted_labels[min_index == sorted_order[i]] = i

    return kmeans_cost, predicted_labels, w_final
Theta = 0.02*np.random.rand(c+1, 1)

# number of gradient descent iterations
num_iterations = 300

# variables to keep the loss and gradient at every iteration
# (needed for visualization)
iters = np.arange(num_iterations)
loss = np.full(iters.shape, np.nan)
validation_loss = np.full(iters.shape, np.nan)

# Create base figure
fig = plt.figure(figsize=(15,8))
ax1 = fig.add_subplot(121)
im1, Xh_ones, num_range_points = util.plot_lr(trainingX, trainingY, Theta, ax1)
seg_util.scatter_data(trainingX, trainingY, ax=ax1)
ax1.grid()
ax1.set_xlabel('x_1')
ax1.set_ylabel('x_2')
ax1.legend()
ax1.set_title('Training set')
text_str1 = '{:.4f};  {:.4f};  {:.4f}'.format(0, 0, 0)
txt1 = ax1.text(0.3, 0.95, text_str1, bbox={'facecolor': 'white', 'alpha': 1, 'pad': 10}, transform=ax1.transAxes)

ax2 = fig.add_subplot(122)
ax2.set_xlabel('Iteration')
ax2.set_ylabel('Loss (average per sample)')
ax2.set_title('mu = '+str(mu))
h1, = ax2.plot(iters, loss, linewidth=2, label='Training loss')
h2, = ax2.plot(iters, validation_loss, linewidth=2, label='Validation loss')
ax2.set_ylim(0, 0.7)