In [5]:
%matplotlib inline
In [8]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
# import seaborn as sns
# sns.set_style('darkgrid')
# sns.set_context('notebook')
from lightning.image.controller import ImageController
from lightning_flash import image as flash
from test.resources import demo_resources as demo
Since we don't have a database up and running, we will pass the ImageController the information it needs manually. As soon as the database is set up, the Provider will make queries to the database and provide individual ImageControllers the information they need.
In [7]:
imcontroller = ImageController(demo.image_info)
demo.image_info.items()
Out[7]:
Normally the image controller would be passed images directly, but for now, we have to load them from disk by calling generate_im_obj
In [4]:
imcontroller.generate_image_obj()
Out[4]:
From above, it is possible to inspect basic properties such as whether or not the ImageController's image is running in local or Spark mode, its type and shape. (3, 2048, 2048) indicates that our image is 2048 x 2048 with 3 channels. Let's look at some of the other information housed by the ImageController:
In [5]:
imcontroller.channels
imcontroller
print('Numerical Labels that index image: ')
print(imcontroller.image_obj.labels)
print('Channels: ')
print(imcontroller.channels)
print('Image types: ' + str(imcontroller.feat_data.keys()))
print ('Features: ')
print(imcontroller.feat_data['IF'].keys())
print('ER features: ')
print(imcontroller.feat_data['IF']['intensity']['ER'].keys())
xy = imcontroller.feat_data['IF']['xy']
x = xy[:,0]
a = ((xy[:, 0] > 100) & (xy[:, 1] < 400))
print sum(a)
# print np.where([xy[:, 0] > 100], [xy[:, 1] < 300])
# print xy[:,0]
b = [True, False, True]
def generate_patches():
num_patches = 5
patches = np.zeros((num_patches, 3, 256, 256))
for i in xrange(num_patches):
a = np.ones((3, 256, 256))
patches[i] = a
return patches, np.ones(num_patches)
patches = []
labels = []
for i in xrange(5):
pats, labs =generate_patches()
patches.append(pats)
labels.append(labs)
print np.vstack(patches).shape
print np.hstack(labels).shape
# X = np.vstack(zip(*patches_labels)[0])
# a = np.ones((3, 256, 256))
# b = np.ones((3, 256, 256))
# aug_patches = []
# aug_patches2 = []
# aug_patches.append(a)
# aug_patches.append(b)
# print type(aug_patches)
# aug_patches2.append(a)
# aug_patches2.append(b)
# aug_patches = np.stack(aug_patches, axis=0)
# aug_patches2 = np.stack(aug_patches2, axis=0)
# print aug_patches.shape
# print aug_patches2.shape
# c = []
# c.append((aug_patches,1))
# c.append((aug_patches2,2))
# cz = zip(*c)
# print np.vstack(cz[0]).shape
# d = [(1, 3),(2, 4)]
# e = zip(*d)
# print e[0]
# f = np.stack(e[0], axis=0)
# print f
# print f.shape
Question: I am trying to transition from storing data in nested dictionaries to ndarrays - you will notice that I have moved the images to this format but not the features. Thoughts? The image arrays can be stored in an RDD but not the features - I'd like to fix this.
From above we have one imaging modalitity IF with three subchanels: ER, PR and HER2. Let's look at the ER subchannel:
In [6]:
ER = imcontroller.get_image_channel(('IF','ER'))
flash.show_channel(ER) # Note: this is the unadjusted image
In [7]:
print imcontroller.image_obj
flash.show_first_three_channels(imcontroller.image_obj)
In [8]:
patch_shape = (3, 256, 256)
imcontroller.patchify(patch_shape=patch_shape, overlap=64)
print imcontroller.patches.shape
patches = imcontroller.patches[0].reshape((-1,) + patch_shape)
print patches.shape
print np.stack(patches, axis=0).shape
class_num = 2
labels = np.ones(60) * int(class_num)
labels2 = np.ones(60) * int(class_num)
l = [labels, labels2]
print np.hstack(l)
print
plt.imshow(imcontroller.patches[0,1,0,0,:, :])
# plt.show()
plt.imshow(patches[8, 0,:, :])
# plt.show()
for k in xrange(64):
for i in xrange(8):
for j in xrange(8):
a = sum(sum(patches[k,0,:, :] == imcontroller.patches[0,i,j,0,:, :]))
if a > 60000:
print (i, j, k)
print'(%d, %d, %d)' % ((k / imcontroller.patches.shape[1]), (k % imcontroller.patches.shape[2]), k)
print 'found match'
print 'done'
p1 = patches
p2 = patches
p3 = patches
p = [p1, p2, p3]
In [343]:
# flash.show_subset_patches(imcontroller.patches, (8,8), 0)
In [20]:
imcontroller.patchify(patch_shape=(512, 512, 3),overlap=200)
flash.show_subset_patches(imcontroller.patches, (4,4), 0)
Questions:
In [ ]: