In [1]:
%matplotlib inline
In [2]:
from numpy import array
import matplotlib.pyplot as plt
#import seaborn as sns
In [3]:
import thunder
from showit import image, tile
import matplotlib.animation as animation
In [4]:
from os.path import join, exists
from os import mkdir, makedirs
In [5]:
from numpy import save
In [6]:
from skimage.io import imsave, imread
In [7]:
from lightning import Lightning
In [8]:
from regional import many
from numpy import random
In [9]:
server = 'http://kafka1.int.janelia.org:3000'
#server = 'http://localhost:3000'
lgn = Lightning(server, ipython=True)
In [10]:
def normalize(oim):
# normalizes 3D image across first axis
assert oim.ndim == 3
means = oim.mean(axis=(1, 2), dtype='float32')
return array([oim[i]/means[i]/4 for i in range(oim.shape[0])]).clip(0, 1)
In [11]:
directory = '/tier2/freeman/Nick/lfov.calibration'
In [12]:
key = '2016-04-18-long'
name = 'anm-0330549'
In [13]:
path = join(directory, 'reprocessed', name, key)
print exists(path)
In [14]:
savepath = join(path, 'sources')
if not exists(savepath):
makedirs(savepath)
In [15]:
data = imread(join(path, 'summary', 'rsqOverlayLC.tif'), plugin='tifffile')
data = data.astype('float32')/255
In [16]:
#norm = normalize(data)
In [17]:
image(data, size=12)
Out[17]:
In [18]:
img = data
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [56]:
plane = 0
img = norm[plane]
In [205]:
with open(join(path, 'sources', 'sources-%04d.tif' % plane)) as fid:
sources = many([x['coordinates'] for x in json.load(fid)])
In [19]:
viz = lgn.imagepoly(img)
viz
In [ ]:
X = viz.points(z=plane)
In [ ]:
X
In [ ]:
In [58]:
sources = many(viz.points(z=plane))
In [65]:
from numpy import maximum, tile
In [77]:
base.shape
Out[77]:
In [74]:
sources[0]
Out[74]:
In [78]:
base = tile(norm,(3,1,1,1)).transpose(1,2,3,0).transpose(1,2,0,3)
masks = sources.mask((512,512,4), background='black', fill='blue', stroke='orange')
blend = maximum(base, masks)
In [81]:
fig = plt.figure(figsize=[12,12])
ax = plt.axes()
im = image(blend[:,:,0], ax=ax)
plt.xlim([0, blend.shape[1]]);
plt.ylim([blend.shape[0], 0]);
#for s in range(sources.count):
# plt.annotate(s=str(s), xy=[sources.center[s][1],sources.center[s][0]], color='w', size = 20);
In [82]:
imsave(join(path, 'sources', 'sources.tif'), (255*blend).astype('uint8'), plugin='tifffile', photometric='rgb')
#imsave(join(path, 'sources', 'sources.tif'), (255*blend).astype('uint8'), plugin='tifffile', photometric='rgb')
In [203]:
imsave(join(path, 'sources', 'sources-%04d.tif' % plane), (255*blend).astype('uint8'), plugin='tifffile', photometric='rgb')
In [207]:
###multi dim nature of this ...
In [ ]:
foo = sources.masks((512,512,4), color='blue', base=X)
image(foo[:,:,1,:])
In [227]:
from numpy import concatenate
In [231]:
x = many([[concatenate((x, [plane])) for x in roi.coordinates] for roi in sources])
In [235]:
if norm.ndim == 2:
base = tile(norm,(3,1,1)).transpose(1,2,0)
masks = sources.mask(norm.shape, background='black', fill='blue', stroke='orange')
blend = maximum(base, masks)
else:
base = tile(norm,(3,1,1,1)).transpose(1,2,3,0)
masks = [sources.mask(norm.shape, background='black', fill='blue', stroke='orange')
In [ ]:
def mask(sources, shape, **kwargs):
if len(shape) == 3:
else:
return sources.mask(shape, **kwargs)
In [1]:
import regional
In [2]:
regional.__version__
Out[2]:
In [19]:
a = [1, 18, 35]
b = [2, 20, 40]
In [5]:
import numpy as np
In [20]:
x = np.stack((a, b), axis = 1)
In [21]:
x
Out[21]:
In [23]:
x[:,0]
Out[23]:
In [ ]:
In [21]:
from sima.misc.imagej import read_imagej_roi_zip
In [25]:
X = read_imagej_roi_zip('/Users/sofroniewn/github/regional/RoiSet.zip')
In [33]:
X[0]
Out[33]:
In [38]:
Y[0]
Out[38]:
In [35]:
Y = read_roi_zip('/Users/sofroniewn/github/regional/RoiSet.zip')
In [34]:
# Copyright: Luis Pedro Coelho <luis@luispedro.org>, 2012
# License: MIT
import numpy as np
def read_roi(fileobj):
'''
points = read_roi(fileobj)
Read ImageJ's ROI format
'''
# This is based on:
# http://rsbweb.nih.gov/ij/developer/source/ij/io/RoiDecoder.java.html
# http://rsbweb.nih.gov/ij/developer/source/ij/io/RoiEncoder.java.html
SPLINE_FIT = 1
DOUBLE_HEADED = 2
OUTLINE = 4
OVERLAY_LABELS = 8
OVERLAY_NAMES = 16
OVERLAY_BACKGROUNDS = 32
OVERLAY_BOLD = 64
SUB_PIXEL_RESOLUTION = 128
DRAW_OFFSET = 256
pos = [4]
def get8():
pos[0] += 1
s = fileobj.read(1)
if not s:
raise IOError('readroi: Unexpected EOF')
return ord(s)
def get16():
b0 = get8()
b1 = get8()
return (b0 << 8) | b1
def get32():
s0 = get16()
s1 = get16()
return (s0 << 16) | s1
def getfloat():
v = np.int32(get32())
return v.view(np.float32)
magic = fileobj.read(4)
if magic != 'Iout':
raise IOError('Magic number not found')
version = get16()
# It seems that the roi type field occupies 2 Bytes, but only one is used
roi_type = get8()
# Discard second Byte:
get8()
if not (0 <= roi_type < 11):
raise ValueError('roireader: ROI type %s not supported' % roi_type)
if roi_type != 7:
raise ValueError('roireader: ROI type %s not supported (!= 7)' % roi_type)
top = get16()
left = get16()
bottom = get16()
right = get16()
n_coordinates = get16()
x1 = getfloat()
y1 = getfloat()
x2 = getfloat()
y2 = getfloat()
stroke_width = get16()
shape_roi_size = get32()
stroke_color = get32()
fill_color = get32()
subtype = get16()
if subtype != 0:
raise ValueError('roireader: ROI subtype %s not supported (!= 0)' % subtype)
options = get16()
arrow_style = get8()
arrow_head_size = get8()
rect_arc_size = get16()
position = get32()
header2offset = get32()
if options & SUB_PIXEL_RESOLUTION:
getc = getfloat
points = np.empty((n_coordinates, 2), dtype=np.float32)
else:
getc = get16
points = np.empty((n_coordinates, 2), dtype=np.int16)
points[:,1] = [getc() for i in xrange(n_coordinates)]
points[:,0] = [getc() for i in xrange(n_coordinates)]
points[:,1] += left
points[:,0] += top
points -= 1
return points
def read_roi_zip(fname):
import zipfile
with zipfile.ZipFile(fname) as zf:
return [read_roi(zf.open(n))
for n in zf.namelist()]
In [ ]: