Ejemplo n.º 1
0
def test_classifier_per(clf, imask, number, params=None):

    #tests the classifier trained on a number of an image's pixels. returns the average dice coefficients the number of pixels trained on and

    if params == None:
        params = ps.trainableParameters()

    images = imask[0]
    masks = imask[1]

    output, clf, clock = ClusterPercent(images[0],
                                        masks[0],
                                        clf,
                                        number,
                                        params=params)

    output = []
    for i in range(10):
        output.append(ps.ClassifierSegment(clf, images[i], parameters=params))

    alldice = np.zeros((2, 2, 10))
    for i in range(10):
        #tempp = ps.toggle_channels(output[i]) #for image saving
        output[i] = 2 - output[i]
        masks[i] = 2 - masks[i]

        alldice[:, :, i] = ac.dice(output[i], masks[i])

    #avdice = np.zeros((2,2))
    #avdice[:,:] = alldice[:,:,:].mean(axis=2)

    return alldice, clock
Ejemplo n.º 2
0
def make_results_au(clff):
    perlist = [
        2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4192, 8000, 16000,
        32000
    ]
    if str(clff) == 'KNeighborsClassifier()':
        perlist = [
            8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4192, 8000, 16000, 32000
        ]

    data = open('results/acc{}.csv'.format(str(clff)), 'w')
    data.write('pixels,ln(pix),,time,die00,die01,die10,die11\n')

    p = ps.trainableParameters()
    p.setGlobalSigma(4)
    p.setDiffGaussian(prefilter=False, high_sigma=64)

    for i in range(len(perlist)):
        die, tim = test_classifier_per('AuGe TEM', clff, perlist[i], params=p)
        data.write('{},,,{},{},{},{},{}\n'.format(perlist[i], tim, die[0, 0],
                                                  die[0, 1], die[1, 0],
                                                  die[1, 1]))

        print(i)
    data.close()
    return
Ejemplo n.º 3
0
def ClusterPercent(image, labels, classifier, number, params=None):
    #just does one image trained on a random percentage of the image pixels

    if params == None:
        params = ps.trainableParameters()

    if len(labels.shape) != 2:
        labels = ps.toggle_channels(labels)

    #makes sure labels aren't empty
    if (labels != 0).any() == True:
        thin_mask = labels.astype(np.float64)
        image = image.data

        features = ps.CreateFeatures(image, parameters=params)

        area = image.shape[0] * image.shape[1]
        indexes = np.random.randint(0, high=area, size=number)
        frac_array = np.zeros(area)
        frac_array[indexes] = 1
        frac_array = np.reshape(
            frac_array, (image.shape[0], image.shape[1])).astype(np.bool)

        features = np.rot90(np.rot90(features, axes=(2, 0)), axes=(1, 2))
        #features are num/x/ymini

        training_data = features[:, frac_array].T
        #training data is number of labeled pixels by number of features
        training_labels = thin_mask[frac_array].ravel()
        training_labels = training_labels.astype('int')
        #training labels is labelled pixels in 1D array

        tic = time.perf_counter()
        classifier.fit(training_data, training_labels)
        #will crash for one image with no labels
        toc = time.perf_counter() - tic

        thin_mask = labels.astype(np.float64)
        output = np.copy(thin_mask)
        if (labels == 0).any() == True:
            #train classifier on  labelled data
            data = features[:, thin_mask == 0].T
            #unlabelled data
            pred_labels = classifier.predict(data)
            #predict labels for rest of image

            output[thin_mask == 0] = pred_labels
            #adds predicted labels to unlabelled data

    return output, classifier, toc
Ejemplo n.º 4
0
def batch_test(clff, pix, imask, output):
    die = np.zeros((2, 2, 10, 4))
    tim = np.zeros((4, 1))

    die[:, :, :, 1], tim[1, :] = spr.test_classifier_per(clff, imask[1], pix)
    print(.1, end='\r')
    die[:, :, :, 2], tim[2, :] = spr.test_classifier_per(clff, imask[2], pix)
    print(.2, end='\r')
    p = ps.trainableParameters()
    p.setGlobalSigma(4)
    p.setDiffGaussian(prefilter=False, high_sigma=64)
    die[:, :, :, 3], tim[3, :] = spr.test_classifier_per(clff,
                                                         imask[3],
                                                         pix,
                                                         params=p)
    print(.3, end='\r')
    die[:, :, :, 0], tim[0, :] = spr.test_classifier_per(clff,
                                                         imask[0],
                                                         pix,
                                                         params=p)
    print(.4, end='\r')
    dietim = (die, tim)
    output.put(dietim)
Ejemplo n.º 5
0
def test_classifier_mult(afolder, clf, n=1, parameters=None):

    folderi = f"Ground truth/{afolder}/image"
    folderm = f"Ground truth/{afolder}/mask"

    if parameters == None:
        params = ps.trainableParameters()

    images = []
    masks = []
    names = []

    for mask in os.listdir((folderm)):
        names.append(mask)
        maskfile = np.asarray(Image.open(os.path.join(folderm, mask)))
        if afolder == 'AuGe TEM':
            image = mask[:-7] + ".tiff"

            thing = np.zeros_like(maskfile)
            thing[maskfile == 255] = 1
            thing = thing + 1
        else:
            image = mask[:-7] + ".dm4"

            thing = ps.toggle_channels(maskfile[:, :, :3],
                                       colors=['#0000ff', '#ff0000'])

        imagefile = hs.load(os.path.join(folderi, image))
        import matplotlib.pyplot as plt
        im = imagefile.data
        normed = (im - im.min()) * (1000 / (im.max() - im.min()))

        images.append(normed)
        masks.append(thing)

    data = open(f"results/dicedata{afolder}{str(clf)}.csv", 'w')
    data.write("pixel accuracy,dice01,dice11,dice00,dice10\n")

    tic = time.perf_counter()
    output, clf = ps.ClusterTrained(images[:n],
                                    masks[:n],
                                    clf,
                                    parameters=parameters)
    #output, clf = ps.ClusterTrained(images[:n], masks[:n], clf, membrane = [1,1,1,1,1,1], texture= True, minimum = True,sigma = p[0], high_sigma = p[1], disk_size = p[2])
    toc = time.perf_counter() - tic
    print('trained classifier in {} seconds'.format(toc))
    data.write('{}\n'.format(toc))
    tic = time.perf_counter()

    if n == 1:
        output = [output]
    for i in range(n, len(masks)):
        output.append(
            ps.ClassifierSegment(clf, images[i], parameters=parameters))
        #output.append(ps.ClassifierSegment(clf, images[i], membrane = [1,1,1,1,1,1], texture= True, minimum = True, sigma = p[0], high_sigma = p[1], disk_size = p[2]))

    toc = time.perf_counter() - tic
    data.write('{}\n'.format(toc))
    print('classified images in {} seconds'.format(toc))

    for i in range(len(masks)):

        output[i] = 2 - output[i]
        masks[i] = 2 - masks[i]

        tempp = ps.toggle_channels(2 - output[i])
        maskim = Image.fromarray(tempp)
        maskim.save(f'results/{names[i]}')

        pixacc = ac.check_ground(output[i], masks[i])
        print("accuracy: {}".format(pixacc))
        dice = ac.dice(output[i], masks[i])
        print("dice {} {}\n     {} {}".format(dice[0, 1], dice[1, 1],
                                              dice[0, 0], dice[1, 0]))
        data.write("{},{},{},{},{}\n".format(pixacc, dice[0, 1], dice[1, 1],
                                             dice[0, 0], dice[1, 0]))

    data.close()
    return
Ejemplo n.º 6
0
    for i in range(len(masks)):

        pixacc = ac.check_ground(masks[i], masks2[i])
        dice = ac.dice(masks[i], masks2[i])
        data.write("{},{},{},{},{}\n".format(pixacc, dice[0, 1], dice[1, 1],
                                             dice[0, 0], dice[1, 0]))

    data.close()
    return


if __name__ == '__main__':

    #this code will generate dice coefficients for each image for each data set, over 4 different classifiers
    ims = ['AuGe TEM', 'PdC TEM', 'Pt ADF', 'PdPtNiAu ADF']
    clfs = [
        GaussianNB(),
        QuadraticDiscriminantAnalysis(),
        RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
        KNeighborsClassifier()
    ]
    for clf in clfs:
        for im in ims:
            if im == 'AuGe TEM' or im == 'PdC TEM':
                p = ps.trainableParameters()
                p.setDiffGaussian(prefilter=False, high_sigma=64)
                p.setGlobalSigma(4)
            else:
                p = ps.trainableParameters()
            test_classifier_mult(im, clf, parameters=p)
Ejemplo n.º 7
0
def make_graph(im_type, num=500, params=None):
    if params == None:
        params = ps.trainableParameters()
    lab_feat = create_smol_features(im_type, num, params)
    filter_names = make_names(params)[1:]

    nf = lab_feat.shape[1] - 1
    planes = gen_pairs(nf)

    fig, axs = plt.subplots(nf - 1,
                            nf - 1,
                            figsize=(2.5 * (nf - 1), 2.5 * (nf - 1)))
    fig.subplots_adjust(hspace=0, wspace=0)
    #fig.suptitle(f'Kernel Planes for {im_type}', y=0.9, fontsize=12, fontweight="bold")
    cmap = mpl.colors.ListedColormap(['#F64141', '#2560A4'])

    for i in range(nf - 1):
        for ii in range(nf - 1):

            for L in range(len(planes)):
                pair = planes[L]
                if i == (pair[0] - 1) and ii == (pair[1] - 2):
                    x = 1
                    break
                    #notes to plot a graph at this i,ii coord
            if x == 1:

                axs[i, ii].scatter(lab_feat[:, pair[1]],
                                   lab_feat[:, pair[0]],
                                   c=lab_feat[:, 0],
                                   cmap=cmap,
                                   s=5)
                axs[i, ii].tick_params(labelbottom=False,
                                       labelleft=False,
                                       bottom=False,
                                       left=False)
                axs[i, ii].locator_params(tight=True, nbins=5)
                #plots the scatter on the right one

                if i == ii:
                    axs[i, ii].locator_params(tight=True, nbins=5)
                    axs[i, ii].tick_params(axis='both',
                                           labelsize=7,
                                           labelbottom=False,
                                           labelleft=False,
                                           bottom=False,
                                           left=False)
                    axs[i, ii].text(-0.5,
                                    0.5,
                                    filter_names[i],
                                    va='center',
                                    ha='center',
                                    fontsize=18,
                                    rotation=-45,
                                    transform=axs[i, ii].transAxes)

                    if i == 7:
                        axs[i, ii].text(0.5,
                                        -0.5,
                                        filter_names[i + 1],
                                        va='center',
                                        ha='center',
                                        fontsize=18,
                                        rotation=-45,
                                        transform=axs[i, ii].transAxes)

                    axs[i, ii].set_yticklabels([])
                    axs[i, ii].set_xticklabels([])
                    #adds labels along diagonals
                x = 0

            else:
                axs[i, ii].set_axis_off()
                #deletes lower triangle plots

    #plt.tight_layout()
    plt.savefig(f'{folder}/Kernel Planes/{im_type} {nf} kernelplanes.svg')
    plt.savefig(f'{folder}/Kernel Planes/{im_type} {nf} kernelplanes.png')

    plt.show()
Ejemplo n.º 8
0
def pearsonplotmulti(im_type=['AuGe TEM', 'PdC TEM', 'PdPtNiAu ADF', 'Pt ADF'],
                     num=100000,
                     params=None):

    if params == None:
        params = ps.trainableParameters()
    filter_names = make_names(params)
    print(filter_names)
    covari = []
    for i in range(4):
        if im_type[i][-3:] == 'TEM':
            p = [4, 64, 20]
        lab_feat = create_smol_features(im_type[i], num, params)
        lab_feat = lab_feat[:, 1:]
        lab_feat = scale(lab_feat, axis=0)

        #covari.append(np.cov(lab_feat.T))
        covari.append(np.absolute(np.corrcoef(lab_feat.T)))

    fig, axs = plt.subplots(2,
                            3,
                            figsize=(covari[0].shape[0] * 3 / 2,
                                     covari[0].shape[1] * 2 / 2))

    letters = ['A', 'B', 'C', 'D']

    axs[0, 2].get_xaxis().set_visible(False)
    axs[0, 2].get_yaxis().set_visible(False)
    axs[0, 2].axis("off")
    axs[1, 2].get_xaxis().set_visible(False)
    axs[1, 2].get_yaxis().set_visible(False)
    axs[1, 2].axis("off")
    fnt = 12
    count = 0
    for i in range(2):
        for ii in range(2):
            ax = axs[i, ii]
            cov = covari[count]
            #ax.set_title(letters[count], loc='left')
            ax.get_xaxis().set_visible(False)
            ax.get_yaxis().set_visible(False)

            cmap = plt.cm.YlGn
            norm = plt.Normalize(cov.min(), cov.max())
            matrix = cmap(norm(cov))
            matrix[range(cov.shape[0]),
                   range(cov.shape[1]), :3] = 0.5, 0.5, 0.5
            im = ax.imshow(matrix, interpolation='nearest', vmin=0, vmax=1)

            if letters[count] == 'A' or letters[count] == 'C':
                ax.set_yticks(range(cov.shape[0]))
                ax.get_yaxis().set_visible(True)
                ax.set_yticklabels(filter_names[1:], fontsize=fnt)
            if letters[count] == 'A' or letters[count] == 'B':
                ax.get_xaxis().set_visible(True)
                ax.set_xticks(range(cov.shape[0]))
                ax.set_xticklabels(filter_names[1:], fontsize=fnt, rotation=90)
                ax.tick_params(top=True,
                               bottom=False,
                               labeltop=True,
                               labelbottom=False)
            for x in range(cov.shape[0]):
                for y in range(cov.shape[1]):
                    if x != y:
                        c = (str(cov[x, y])[:5]) if cov[x, y] < 0 else (str(
                            cov[x, y])[:4])
                        ax.text(x, y, c, va='center', ha='center', fontsize=10)
            count += 1

    fig.subplots_adjust(hspace=0.1, wspace=0.1)
    cbaxes = fig.add_axes([0.75, 0.1, 0.03, 0.6])
    cbar = fig.colorbar(plt.cm.ScalarMappable(norm=norm, cmap=cmap),
                        ax=axs,
                        cax=cbaxes,
                        shrink=0.7,
                        fraction=0.5)
    cbar.set_label('Absolute PCC', fontsize=fnt)

    plt.tight_layout()
    plt.savefig(f'{folder}/Covariance/covariance {lab_feat.shape[1]}.svg')
    plt.savefig(f'{folder}/Covariance/covariance {lab_feat.shape[1]}.png')
    plt.show()
    return
Ejemplo n.º 9
0
def ksztest(im_type=['AuGe TEM', 'PdC TEM', 'PdPtNiAu ADF', 'PT ADF'],
            n=1000,
            params=None):

    if params == None:
        parameters = ps.trainableParameters()
    filter_names = make_names(params)
    filter_names = filter_names[1:]
    numfilt = len(filter_names)
    stat_array = np.zeros((numfilt, 3, len(im_type)))

    for i in range(len(im_type)):
        if i < 2:
            temp_params = ps.trainableParameters()
            temp_params.setGlobalSigma(4)
            temp_params.setGlobalPrefilter(4)
            temp_params.diff_gaussian[3] = 64
            temp_params.membrane = [[False, 1], True, True, True, True, True,
                                    True]
            temp_params.hessian[0] = True
            temp_params.laplacian[0] = True
        else:
            temp_params = params

        lab_feat = create_smol_features(im_type[i], n, temp_params)

        index0 = lab_feat[:, 0] == 1
        index1 = lab_feat[:, 0] == 2

        phile = open(f'{folder}/KSZ test{im_type[i]}.csv', 'w')

        for ii in range(numfilt):
            data_0 = lab_feat[index0, ii + 1]
            data_1 = lab_feat[index1, ii + 1]

            sep = ks_2samp(data_0, data_1)
            z_stat = abs(data_0.mean() - data_1.mean()) / (
                m.sqrt(data_0.std()**2 + data_1.std()**2))

            stat_array[ii, :, i] = [sep.statistic, sep.pvalue, z_stat]
            phile.write(
                f'{filter_names[ii]},{sep.statistic},{sep.pvalue},{z_stat}\n')

        phile.close()

    x = np.arange(numfilt)
    width = 0.5
    letters = ['a', 'b', 'c', 'd']
    count = 0
    fig, axs = plt.subplots(2, 2, figsize=(1 * numfilt, 10))

    for j in range(2):
        for jj in range(2):
            axs[j, jj].bar(x,
                           stat_array[:, 0, count],
                           width,
                           label=letters[count],
                           color='#2560A4')
            axs[j, jj].set_xticks(x)
            axs[j, jj].set_ylabel('KS Statistic', fontsize=12)
            axs[j, jj].set_ylim([0, 1])
            axs[j, jj].tick_params(axis='y', which='major', labelsize=12)
            #axs[j,jj].text(-0.1,0.9,letters[count], fontsize = 14)
            axs[j, jj].set_xticklabels(filter_names,
                                       fontsize=12,
                                       rotation=45,
                                       ha='right')
            axs[j, jj].set_ylabel('KS Statistic', fontsize=12)

            count += 1
    fig.tight_layout()
    plt.savefig(f'{folder}/KSZ test/KSZ.svg')
    plt.savefig(f'{folder}/KSZ test/KSZ.png')
    plt.show()
    return
Ejemplo n.º 10
0
            else:
                axs[i, ii].set_axis_off()
                #deletes lower triangle plots

    #plt.tight_layout()
    plt.savefig(f'{folder}/Kernel Planes/{im_type} {nf} kernelplanes.svg')
    plt.savefig(f'{folder}/Kernel Planes/{im_type} {nf} kernelplanes.png')

    plt.show()


i = 1

if i == 0:
    params = ps.trainableParameters()
    pearsonplotmulti(params=params)
    params.membrane = [[False, 1], True, True, True, True, True, True]
    params.gaussian[0] = False
    params.diff_gaussian[0] = False
    params.median[0] = False
    params.minimum[0] = False
    params.maximum[0] = False
    params.sobel[0] = False
    pearsonplotmulti(params=params)

elif i == 1:
    params = ps.trainableParameters()
    params.membrane = [[False, 1], True, True, True, True, True, True]
    params.hessian[0] = True
    params.laplacian[0] = True