Commit 28ce07d7 authored by Jiri Borovec's avatar Jiri Borovec

examples

parent b26ec07d
__author__ = 'Jiri Borovec'
import os, sys, glob
import numpy as np
import pandas
from skimage import io
import matplotlib.pylab as plt
import matplotlib.gridspec as gridspec
sys.path.append(os.path.abspath(os.path.join('..','..'))) # Add path to root
# import src.ownUtils.toolDataIO as tD
import generateDataset as genAPD
import dictionaryLearning as DL
import patternWeights as encode
import similarityMetric as metric
import logging
logger = logging.getLogger(__name__)
def experiment_pipelineALPE (atlas, imgs, encoding) :
initAtlas_org = DL.initialiseAtlas_deformOriginal(atlas)
initAtlas_rnd = DL.initialiseAtlas_random(atlas.shape, np.max(atlas))
initAtlas_msc = DL.initialiseAtlas_mosaic(atlas.shape, np.max(atlas))
initEncode_rnd = DL.initialiseWeights_random(len(imgs), np.max(atlas))
pOut = os.path.join('..','..','output')
DL.pipeline_estimAtlasLearningPatternWeights(imgs, initAtlas=initAtlas_msc, maxIter=9, reInit=False, outDir=pOut, outPrefix='d1')
# _ = input('wait')
DL.pipeline_estimAtlasLearningPatternWeights(imgs, initAtlas=initAtlas_msc, maxIter=9, reInit=True, outDir=pOut, outPrefix='d2')
# DL.pipeline_estimAtlasLearningPatternWeights(imgs, initEncode=initEncode_rnd, outDir=pOut)
return None
def getSimpleAtlas () :
atlas = np.zeros((20,20))
atlas[2:8,2:8] = 1
atlas[12:18,12:18] = 2
atlas[2:8,12:18] = 3
return atlas
def getSampleImages (atlas) :
img1 = atlas.copy()
img1[img1>=2] = 0
img2 = atlas.copy()
img2[img2<=1] = 0
img2[img2>0] = 1
return img1, img2
def test_simpleCase () :
# implement simple case just with 2 images and 2/3 classes in atlas
atlas = getSimpleAtlas()
atlas2 = atlas.copy()
atlas2[atlas2>2] = 0
img1, img2 = getSampleImages(atlas)
w1, w2 = [1,0,0], [0,1,0]
w3, w4 = [1,0,1], [0,1,1]
imgs = [img1, img2]
for j, ws in enumerate([[w1, w2],[w3, w4]]) :
plt.figure()
plt.title('w: {}'.format(repr(ws)))
gs = gridspec.GridSpec(2,4)
# plt.subplot(gs[0, 1]), plt.imshow(atlas, interpolation='nearest'), plt.title('atlas')
# plt.subplot(gs[0, 2]), plt.imshow(atlas2, interpolation='nearest'), plt.title('atlas')
plt.subplot(gs[0, 1]), plt.imshow(img1, cmap='gray', interpolation='nearest'), plt.title('w:{}'.format(ws[0]))
plt.subplot(gs[0, 2]), plt.imshow(img2, cmap='gray', interpolation='nearest'), plt.title('w:{}'.format(ws[1]))
uc = DL.computeRelativePenalyFromImagesWeights(imgs, np.array(ws))
uc = uc.reshape(atlas.shape+uc.shape[2:])
# logger.debug(ws)
for i in range(uc.shape[2]):
plt.subplot(gs[1, i]), plt.imshow(uc[:,:,i], vmin=0, vmax=1, interpolation='nearest')
plt.title('cost lb #{}'.format(i)), plt.colorbar()
# logger.debug(uc)
return None
def main () :
atlas = genAPD.dataset_createAtlas()
# plt.imshow(atlas)
imgs = genAPD.dataset_loadImages()
# plt.imshow(imgs[0])
encoding = genAPD.dataset_loadEncoding()
# logger.info('encoding : {}'.format(encoding))
experiment_pipelineALPE(atlas, imgs, encoding)
return None
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
logger.info('running...')
# test_encoding(atlas, imgs, encoding)
# test_atlasLearning(atlas, imgs, encoding)
# test_simpleCase()
main()
logger.info('DONE')
plt.show()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment