From ac5ae422226145edf277c89f7e6fb6c2c73b08da Mon Sep 17 00:00:00 2001 From: David Mayerich Date: Wed, 9 Aug 2017 17:37:46 -0500 Subject: [PATCH] added digital staining functions --- python/digitalstain.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+), 0 deletions(-) create mode 100644 python/digitalstain.py diff --git a/python/digitalstain.py b/python/digitalstain.py new file mode 100644 index 0000000..9406846 --- /dev/null +++ b/python/digitalstain.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +""" +Created on Tue Jul 25 16:28:37 2017 + +@author: david +""" + +import spectral +import envi +import classify +import numpy +import scipy +import scipy.misc +import sklearn +import sklearn.naive_bayes +import sklearn.neural_network +import glob +import matplotlib.pyplot as plt +import random + +def generate_stain(envifile, stainfile, N=5000, batch_size=10000, validate=True): + E = envi.envi(envifile) + mask = classify.random_mask(E.mask, N) + + Ft = E.loadmask(mask).transpose() + + stain = numpy.rollaxis(scipy.misc.imread(stainfile), 2) + Tt = spectral.sift2(stain, mask).transpose() + + CLASS = sklearn.neural_network.MLPRegressor(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(), random_state=1, verbose=True) + CLASS.fit(Ft, Tt) + + if validate == False: + return CLASS + + plt.ion() + Fv = E.loadbatch(batch_size) #load the first batch + n = 0 + while not Fv == []: #loop until an empty batch is returned + if n == 0: + Tv = CLASS.predict(Fv.transpose()).transpose() + else: + Tv = numpy.append(Tv, CLASS.predict(Fv.transpose()).transpose(), 1) #append the predicted labels from this batch to those of previous batches + COLORS = spectral.unsift2(Tv, E.batchmask()) #convert the matrix of class labels to a 2D array + RGB = numpy.rollaxis(COLORS, 0, 3).astype(numpy.ubyte) + plt.imshow(RGB) #display it + plt.pause(0.05) + Fv = E.loadbatch(batch_size) #load the next batch + n = n + 1 + return CLASS, RGB -- libgit2 0.21.4