Commit ac5ae422226145edf277c89f7e6fb6c2c73b08da

Authored by David Mayerich
1 parent 27fd135f

added digital staining functions

Showing 1 changed file with 50 additions and 0 deletions   Show diff stats
python/digitalstain.py 0 → 100644
  1 +# -*- coding: utf-8 -*-
  2 +"""
  3 +Created on Tue Jul 25 16:28:37 2017
  4 +
  5 +@author: david
  6 +"""
  7 +
  8 +import spectral
  9 +import envi
  10 +import classify
  11 +import numpy
  12 +import scipy
  13 +import scipy.misc
  14 +import sklearn
  15 +import sklearn.naive_bayes
  16 +import sklearn.neural_network
  17 +import glob
  18 +import matplotlib.pyplot as plt
  19 +import random
  20 +
  21 +def generate_stain(envifile, stainfile, N=5000, batch_size=10000, validate=True):
  22 + E = envi.envi(envifile)
  23 + mask = classify.random_mask(E.mask, N)
  24 +
  25 + Ft = E.loadmask(mask).transpose()
  26 +
  27 + stain = numpy.rollaxis(scipy.misc.imread(stainfile), 2)
  28 + Tt = spectral.sift2(stain, mask).transpose()
  29 +
  30 + CLASS = sklearn.neural_network.MLPRegressor(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(), random_state=1, verbose=True)
  31 + CLASS.fit(Ft, Tt)
  32 +
  33 + if validate == False:
  34 + return CLASS
  35 +
  36 + plt.ion()
  37 + Fv = E.loadbatch(batch_size) #load the first batch
  38 + n = 0
  39 + while not Fv == []: #loop until an empty batch is returned
  40 + if n == 0:
  41 + Tv = CLASS.predict(Fv.transpose()).transpose()
  42 + else:
  43 + Tv = numpy.append(Tv, CLASS.predict(Fv.transpose()).transpose(), 1) #append the predicted labels from this batch to those of previous batches
  44 + COLORS = spectral.unsift2(Tv, E.batchmask()) #convert the matrix of class labels to a 2D array
  45 + RGB = numpy.rollaxis(COLORS, 0, 3).astype(numpy.ubyte)
  46 + plt.imshow(RGB) #display it
  47 + plt.pause(0.05)
  48 + Fv = E.loadbatch(batch_size) #load the next batch
  49 + n = n + 1
  50 + return CLASS, RGB
... ...