def test(): import glob import analysis.experiment as exp reload(exp) fn = exp.filename() print fn fn = filename(dtype = 'img') print fn print glob.glob(fn) data = exp.load(wid = 0); print data.shape import matplotlib.pyplot as plt plt.figure(1); plt.clf(); img = exp.load_img(t=200000); plt.imshow(img, cmap = 'gray') #animate movie import time fig, ax = plt.subplots() figimg = ax.imshow(img, cmap = 'gray'); plt.show(); for t in range(200000, 300000): figimg.set_data(exp.load_img(t=t)); ax.set_title('t=%d' % t); time.sleep(0.001) fig.canvas.draw() fig.canvas.flush_events() reload(exp) sbins = exp.stage_bins(wid = [0,1]) d = exp.load_stage_binned(wid = [0,1], nbins_per_stage=10) a = exp.load_aligned(wid = all, align='time', dtype = 'speed') a_th = np.nanpercentile(a, 85); a[a> a_th] = a_th; a[np.isnan(a)] = -1.0; import analysis.plot as fplt; fplt.plot_array(a) a = exp.load_aligned(wid = all, align='L2', dtype = 'speed') a_th = np.nanpercentile(a, 85); a[a> a_th] = a_th; a[np.isnan(a)] = -1.0; import analysis.plot as fplt; fplt.plot_array(a)
def test(): import glob import analysis.experiment as exp reload(exp) fn = exp.filename() print fn fn = filename(dtype='img') print fn print glob.glob(fn) data = exp.load(wid=0) print data.shape import matplotlib.pyplot as plt plt.figure(1) plt.clf() img = exp.load_img(t=200000) plt.imshow(img, cmap='gray') #animate movie import time fig, ax = plt.subplots() figimg = ax.imshow(img, cmap='gray') plt.show() for t in range(200000, 300000): figimg.set_data(exp.load_img(t=t)) ax.set_title('t=%d' % t) time.sleep(0.001) fig.canvas.draw() fig.canvas.flush_events() reload(exp) sbins = exp.stage_bins(wid=[0, 1]) d = exp.load_stage_binned(wid=[0, 1], nbins_per_stage=10) a = exp.load_aligned(wid=all, align='time', dtype='speed') a_th = np.nanpercentile(a, 85) a[a > a_th] = a_th a[np.isnan(a)] = -1.0 import analysis.plot as fplt fplt.plot_array(a) a = exp.load_aligned(wid=all, align='L2', dtype='speed') a_th = np.nanpercentile(a, 85) a[a > a_th] = a_th a[np.isnan(a)] = -1.0 import analysis.plot as fplt fplt.plot_array(a)
def get_image(self, stride = 1, random = False, sigma = 1.0): if random: wc = np.random.randint(0, self.nwids); t = np.random.randint(self.tmins[wc], self.tmaxs[wc]); img = exp.load_img(strain = self.strains[wc], wid = self.wids[wc], t = t, sigma = sigma); else: t = self.t_counter; wid = self.wids[self.wid_counter]; img = exp.load_img(wid = wid, t = t); self.increase_counter(stride = stride); img.shape = (1,) + img.shape + (1,); return img;
def get_image(self, stride = 1, random = False, sigma = 1.0): if random: wc = np.random.randint(0, self.nwids); t = np.random.randint(self.tmins[wc], self.tmaxs[wc]); img = exp.load_img(strain = self.strains[wc], wid = self.wids[wc], t = t, sigma = sigma); else: t = self.t_counter; wid = self.wids[self.wid_counter]; img = exp.load_img(wid = wid, t = t); self.increase_counter(stride = stride); #img.shape = (1,) + img.shape + (1,); return img;
def analyse_shape_at_time(t): print t; img = exp.load_img(wid = wid, t = t); ws = aw.WormModel(npoints = npoints); ws.from_image(img, verbose = True, save = figfile % t); sdat = np.load(shapefile, mmap_mode = 'r+'); sdat[t,:] = ws.to_array(); mdat = np.load(measurefile, mmap_mode = 'r+'); mdat[t,:] = ws.measure(); return ws;
def analyse_shape_at_time(t): print t img = exp.load_img(wid=wid, t=t) ws = aw.WormModel(npoints=npoints) ws.from_image(img, verbose=True, save=figfile % t) sdat = np.load(shapefile, mmap_mode='r+') sdat[t, :] = ws.to_array() mdat = np.load(measurefile, mmap_mode='r+') mdat[t, :] = ws.measure() return ws
def process(i): img = exp.load_img(t=i) data = [np.load(fn, mmap_mode='r+') for fn in data_files] if i % 1000 == 0: print 'processing %d/%d...' % (i, ntimes) try: w.from_image(img, verbose=False) results = list(w.theta()) results.append(w.center) results.append(w.width) for d, r in zip(data, results): d[i] = r return True except: data[-1][i] = -1 return False
### Failed frames nfailed = np.sum(data[3] == -1) print 'percentage failed %f' % (100.0 * nfailed / data[3].shape[0]) # plot some of the failed images failed_ids = np.where(data[3] == -1)[0] good_ids = data[3] != -1 plt.figure(1) plt.clf() iids = np.array(np.linspace(0, 8000, 36), dtype=int) for i, j in enumerate(iids): plt.subplot(6, 6, i + 1) img = exp.load_img(t=failed_ids[j]) plt.imshow(img) fig_dir = '/home/ckirst/Desktop/labmeeting' os.mkdir(fig_dir) #make some plots of failed cases for i in failed_ids[iids]: fig = plt.figure(2) plt.clf() plt.imshow(exp.load_img(t=i)) plt.tight_layout() fig.savefig( os.path.join(fig_dir, 'sample_shape_detection_failed_%d.png' % i))
def plot(curve, *args, **kwargs): plt.plot(curve[:,0], curve[:,1], *args, **kwargs); plt.scatter(curve[:,0], curve[:,1], c = 'k', s = 40); ### Initialize Worm from Image npoints = 21; nobs = npoints*2-2; #d differences from the sides / head tail only once worm = wmod.WormModel(npoints = npoints); nparameter = worm.nparameter; #full number of parameter nobs = worm.ndistances; t0 = 500000; threshold_factor = 0.9; absolute_threshold = None; img = exp.load_img(wid = 80, t = t0); imgs = filters.gaussian_filter(np.asarray(img, float), 1.0); worm.from_image(img, absolute_threshold = absolute_threshold, threshold_factor = threshold_factor, verbose = False); success, center, left, right, width = wgeo.shape_from_image(img, absolute_threshold = absolute_threshold, threshold_factor = threshold_factor, npoints = npoints, verbose = False, save = False); plt.figure(1); plt.clf(); #worm.plot(image = imgs) worm.plot(image = imgs) plot(center, 'r'); ### Compare Worm to Contour
Created on Thu Oct 27 00:23:54 2016 @author: ckirst """ # image processing pipeline adn worm shape import matplotlib.pyplot as plt import analysis.experiment as exp import analysis.plot as fplt import worm.model as wmod import worm.geometry as wgeo t0 = 401700; img = exp.load_img(t= t0, sigma = None) fig = plt.figure(1); plt.clf(); plt.imshow(img, cmap = 'gray', interpolation = 'none') plt.axis('off') fig.savefig('pipeline_input.png', facecolor = 'white') #smooth fig = plt.figure(2); plt.clf(); img = exp.load_img(t= t0, sigma=1.0) plt.imshow(img, cmap = 'gray', interpolation = 'none') plt.axis('off')
Network structure img -> conv -> conv -> hidden -> worm parameter """ __license__ = 'MIT License <http://www.opensource.org/licenses/mit-license.php>' __author__ = 'Christoph Kirst <*****@*****.**>' __docformat__ = 'rest' import numpy as np import matplotlib.pyplot as plt import analysis.experiment as exp import analysis.plot as aplt import worm.model as wm # load image img = exp.load_img(wid=80, t=500000, smooth=1.0) aplt.plot_array(img) from skimage.filters import threshold_otsu threshold_factor = 0.95 level = threshold_factor * threshold_otsu(img) from imageprocessing.masking import mask_to_phi phi_img = mask_to_phi(img < level) ### worm -> Phi w = wm.WormModel(npoints=20)
reload(wgeo) reload(wm) import analysis.experiment as exp import scipy.ndimage.filters as filters from interpolation.curve import Curve from interpolation.resampling import resample as resample_curve import worm.costs as wc reload(wc) ### Gradient descent t0 = 500000 img = exp.load_img(wid=80, t=t0) #imgs = filters.gaussian_filter(np.asarray(img, float), 1.0); w = wm.WormModel(npoints=10) w.from_image(img, verbose=False) w.move_forward(0.1) w.rotate(0.1) contours = wgeo.contours_from_image(img, sigma=1, absolute_threshold=None, threshold_factor=0.9, verbose=False, save=None) contour = Curve(resample_curve(contours[0], 100), nparameter=50)
reload(wgeo); reload(wm); import analysis.experiment as exp import scipy.ndimage.filters as filters from interpolation.curve import Curve from interpolation.resampling import resample as resample_curve import worm.costs as wc reload(wc); ### Gradient descent t0 = 500000; img = exp.load_img(wid = 80, t= t0); #imgs = filters.gaussian_filter(np.asarray(img, float), 1.0); w = wm.WormModel(npoints = 10); w.from_image(img, verbose = False); w.move_forward(0.1); w.rotate(0.1); contours = wgeo.contours_from_image(img, sigma = 1, absolute_threshold = None, threshold_factor = 0.9, verbose = False, save = None); contour = Curve(resample_curve(contours[0], 100), nparameter = 50); head_tail_xy = wgeo.head_tail_from_contour_discrete(contour, ncontour = all, delta = 0.3, smooth = 1.0, with_index = False,
### Generate from image import numpy as np import matplotlib.pyplot as plt; import copy import time import worm.model as aw; reload(aw); import analysis.experiment as exp import scipy.ndimage.filters as filters # load image img = exp.load_img(wid = 80, t= 500000); imgs = filters.gaussian_filter(np.asarray(img, float), 1.0); ws = aw.WormModel(nparameter = 20, npoints = 31); ws.from_image(img, verbose = True, threshold_factor = .95, smooth = 1.0, ncontour = 100, sigma = 1.0, nneighbours = 5); plt.figure(1); plt.clf(); ws.plot(image = imgs) ### Self intersections ws = aw.WormModel(theta = np.hstack([np.linspace(0.1, 0.8, 10)*13,13* np.linspace(0.9, 0.1, 11)]) , length = 150); mask = ws.mask(); phi = ws.phi();
ws = analyse_shape_at_time(105820) analyse_shape_at_time(105816) analyse_shape_at_time(151870) analyse_shape_at_time(151873) analyse_shape_at_time(164166) plt.figure(100); plt.clf(); plt.imshow(exp.load_img(wid = wid, t=167169)) from joblib import Parallel, delayed, cpu_count Parallel(n_jobs = cpu_count())( delayed(analyse_shape_at_time)(t) for t in xrange(167870, ntime))
### Generate from image import numpy as np import matplotlib.pyplot as plt import copy import time import worm.model as aw reload(aw) import analysis.experiment as exp import scipy.ndimage.filters as filters # load image img = exp.load_img(wid=80, t=500000) imgs = filters.gaussian_filter(np.asarray(img, float), 1.0) ws = aw.WormModel(nparameter=20, npoints=31) ws.from_image(img, verbose=True, threshold_factor=.95, smooth=1.0, ncontour=100, sigma=1.0, nneighbours=5) plt.figure(1) plt.clf() ws.plot(image=imgs)
return ws ws = analyse_shape_at_time(105820) analyse_shape_at_time(105816) analyse_shape_at_time(151870) analyse_shape_at_time(151873) analyse_shape_at_time(164166) plt.figure(100) plt.clf() plt.imshow(exp.load_img(wid=wid, t=167169)) from joblib import Parallel, delayed, cpu_count Parallel(n_jobs=cpu_count())(delayed(analyse_shape_at_time)(t) for t in xrange(167870, ntime)) import matplotlib.cm as cm def plotTrajectory(xydata, colordata='time', cmap=cm.jet, size=20, ax=None, line=True):
import numpy as np import matplotlib.pyplot as plt import analysis.experiment as exp import analysis.plot as aplt import worm.model as wm; # load image img = exp.load_img(wid = 80, t= 500000, smooth = 1.0); aplt.plot_array(img); from skimage.filters import threshold_otsu threshold_factor = 0.95; level = threshold_factor * threshold_otsu(img); from imageprocessing.masking import mask_to_phi phi_img = mask_to_phi(img < level); ### worm -> Phi w = wm.WormModel(npoints = 20);
import copy import time import worm.model as wm; import worm.geometry as wg; #%% load data t0 = 513478; t0 = 524273 - 3; import analysis.experiment as exp img = exp.load_img(wid = 80, t= t0); plt.imshow(img) #%% preprocess - smmoth import scipy.ndimage.filters as filters imgs = filters.gaussian_filter(np.asarray(img, float), sigma = 1.0); plt.figure(2); plt.clf(); plt.imshow(imgs)
""" import numpy as np import matplotlib.pyplot as plt import scipy.ndimage.filters as filters import analysis.experiment as exp import worm.model as aw; t = 0; # load image near an intersection t = t+1; print(t); img = exp.load_img(wid = 80, t= 524700+914+t); imgs = filters.gaussian_filter(np.asarray(img, float), 1.0); #imgs = img; th = 90; imgs[imgs > th] = th; #imgs =- th -imgs; plt.figure(1); plt.clf(); plt.imshow(imgs); # find worm model ws = aw.WormModel(); ws.from_image(imgs, verbose = False, sigma = None, threshold_factor = 0.95);
reload(wgeo) reload(wmod) ### Initialize Worm from Image npoints = 11 nobs = npoints * 2 - 2 #d differences from the sides / head tail only once worm = wmod.WormModel(npoints=npoints) nparameter = worm.nparameter #full number of parameter t0 = 500000 threshold_factor = 0.9 absolute_threshold = None img = exp.load_img(wid=80, t=t0) imgs = filters.gaussian_filter(np.asarray(img, float), 1.0) worm.from_image(img, absolute_threshold=absolute_threshold, threshold_factor=threshold_factor, verbose=True) worm0 = worm.copy() plt.figure(1) plt.subplot(2, 3, 2) worm0.plot(image=imgs) worm0.plot() ### Initialize Contour to match the worm to from another image img2 = exp.load_img(wid=80, t=t0 + 10)
print('>') print(xy[t0]) print(xy[t0+1]) t0 = 517742; # shfit down v print('v') print(xy[t0]) print(xy[t0+1]) w1 = exp.load_img(t = t0); w2 = exp.load_img(t = t0+1); w3 = exp.load_img(t = t0+2); plt.figure(1); plt.clf(); plt.imshow(w1) plt.imshow(w2) plt.imshow(w1[0:, :-1]) plt.imshow(w2[:, 1:]) plt.imshow(w3)
reload(wgeo); reload(wm); import analysis.experiment as exp import scipy.ndimage.filters as filters from interpolation.curve import Curve from interpolation.resampling import resample as resample_curve import worm.costs as wc reload(wc); ### Gradient descent t0 = 500000; img = exp.load_img(wid = 80, t= t0, sigma = 1.0); #imgs = filters.gaussian_filter(np.asarray(img, float), 1.0); w = wm.WormModel(npoints = 10); w.from_image(img, verbose = False); w.move_forward(0.1); w.rotate(0.1); plt.figure(1); plt.clf(); w.plot(image = img) contour.plot(with_points=False); par = w.get_parameter() npar = par.shape[0]
### Initialize Worm from Image npoints = 21 nobs = npoints * 2 - 2 #d differences from the sides / head tail only once worm = wmod.WormModel(npoints=npoints) nparameter = worm.nparameter #full number of parameter nobs = worm.ndistances t0 = 500000 threshold_factor = 0.9 absolute_threshold = None img = exp.load_img(wid=80, t=t0) imgs = filters.gaussian_filter(np.asarray(img, float), 1.0) worm.from_image(img, absolute_threshold=absolute_threshold, threshold_factor=threshold_factor, verbose=False) success, center, left, right, width = wgeo.shape_from_image( img, absolute_threshold=absolute_threshold, threshold_factor=threshold_factor, npoints=npoints, verbose=False, save=False) plt.figure(1)
from interpolation.resampling import resample as resample_curve import analysis.experiment as exp reload(wgeo); reload(wmod); ### Initialize Worm from Image npoints = 11; nobs = npoints*2-2; #d differences from the sides / head tail only once worm = wmod.WormModel(npoints = npoints); nparameter = worm.nparameter; #full number of parameter t0 = 500000; threshold_factor = 0.9; absolute_threshold = None; img = exp.load_img(wid = 80, t = t0); imgs = filters.gaussian_filter(np.asarray(img, float), 1.0); worm.from_image(img, absolute_threshold = absolute_threshold, threshold_factor = threshold_factor, verbose = True); worm0 = worm.copy(); plt.figure(1); plt.subplot(2,3,2) worm0.plot(image = imgs) worm0.plot() ### Initialize Contour to match the worm to from another image img2 = exp.load_img(wid = 80, t = t0+10); plt.figure(2); plt.clf(); cntrs = wgeo.contour_from_image(img2, sigma = 1, absolute_threshold = absolute_threshold, threshold_factor = threshold_factor,
h_pos_a[ii, 1], c=ii, cmap='spectral', edgecolor='face', s=10) plt.axis('equal') dd = np.linalg.norm(h_pos_a[ii] - t_pos_a[ii], axis=1) plt.figure(70) plt.clf() plt.plot(dd) #show the movie imgs = exp.load_img(t=range(t0, t1)) #imgs = exp.load_img(t=ii); import pyqtgraph as pg pg.show(np.transpose(imgs, [0, 1, 2])) ### pca on oriented head / tail data center = data["center"].copy() for i in range(ntimes): if reverse[i]: center[i] = center[i, ::-1] center_file = os.path.join(data_path, 'oriented_center.npy')
import numpy as np import matplotlib.pyplot as plt import copy import time import worm.model as wm import worm.geometry as wg #%% load data t0 = 513478 t0 = 524273 - 3 import analysis.experiment as exp img = exp.load_img(wid=80, t=t0) plt.imshow(img) #%% preprocess - smmoth import scipy.ndimage.filters as filters imgs = filters.gaussian_filter(np.asarray(img, float), sigma=1.0) plt.figure(2) plt.clf() plt.imshow(imgs) #%% gradient
_,sx,sy,si = content.shape; sii = int(np.ceil(np.sqrt(si))); mat = np.zeros((sii * sx, sii * sy)); for i in range(sii): for j in range(sii): k = i * sii + j; if k < si: ix = i * sx; jy = j * sy; mat[ix:(ix+sx), jy:(jy+sy)] = content[0,:,:,k]; return mat; import scripts.plot as fplt; img = exp.load_img(t = 500000, sigma=1.0); # make color?? imgc = img[:,:,np.newaxis] imgc = np.concatenate([imgc]*3, axis = 2) shape = (1,) + imgc.shape; image = tf.placeholder('float', shape=shape); net, mean_pixel = vgg.net('/home/ckirst/Science/Projects/CElegansBehaviour/Analysis/imagenet-vgg-verydeep-19', image); layer = 'relu4_2' layers = ['relu1_2', 'relu2_1', 'relu2_2', 'relu3_1', 'relu3_2', 'relu3_3', 'relu3_4',
plt.plot(t_pos_a[ii,0], t_pos_a[ii,1], 'gray', linewidth = 1) plt.plot(h_pos_a[ii,0], h_pos_a[ii,1], 'k', linewidth = 1) plt.scatter(h_pos_a[ii,0], h_pos_a[ii,1], c = ii, cmap = 'spectral', edgecolor= 'face', s = 10) plt.axis('equal') dd = np.linalg.norm(h_pos_a[ii]-t_pos_a[ii], axis = 1) plt.figure(70); plt.clf(); plt.plot(dd) #show the movie imgs = exp.load_img(t=range(t0,t1)); #imgs = exp.load_img(t=ii); import pyqtgraph as pg pg.show(np.transpose(imgs, [0,1,2])) ### pca on oriented head / tail data center = data["center"].copy();
# -*- coding: utf-8 -*- """ Worm raw image data alignment and compression module """ import numpy as np import matplotlib.pyplot as plt import analysis.experiment as exp import scipy.ndimage.filters as filters t0 = 250000 img1 = exp.load_img(wid = 80, t= t0); img1 = filters.gaussian_filter(np.asarray(img1, float), 1.0); img2 = exp.load_img(wid = 80, t= t0+1); img2 = filters.gaussian_filter(np.asarray(img2, float), 1.0); plt.figure(1); plt.clf(); plt.subplot(3,2,1); plt.imshow(img1); plt.subplot(3,2,2); plt.imshow(img2); plt.subplot(3,2,3); plt.hist(img1.flatten(),bins = 256) plt.subplot(3,2,4); plt.hist(img2.flatten(), bins = 256) def preprocess(img, threshold = 90):
_, sx, sy, si = content.shape sii = int(np.ceil(np.sqrt(si))) mat = np.zeros((sii * sx, sii * sy)) for i in range(sii): for j in range(sii): k = i * sii + j if k < si: ix = i * sx jy = j * sy mat[ix:(ix + sx), jy:(jy + sy)] = content[0, :, :, k] return mat import scripts.plot as fplt img = exp.load_img(t=500000, sigma=1.0) # make color?? imgc = img[:, :, np.newaxis] imgc = np.concatenate([imgc] * 3, axis=2) shape = (1, ) + imgc.shape image = tf.placeholder('float', shape=shape) net, mean_pixel = vgg.net( '/home/ckirst/Science/Projects/CElegansBehaviour/Analysis/imagenet-vgg-verydeep-19', image) layer = 'relu4_2' layers = [
import analysis.experiment as exp ### Initialize Worm from Image npoints = 21; nobs = npoints*2-2; #d differences from the sides / head tail only once worm = wmod.WormModel(npoints = npoints); nparameter = worm.nparameter; #full number of parameter t0 = 500000; t0 = 529202; threshold_factor = 0.9; absolute_threshold = None; img = exp.load_img(wid = 80, t = t0); plt.figure(1); plt.clf(); worm.from_image(img, absolute_threshold = absolute_threshold, threshold_factor = threshold_factor, verbose = True); worm0 = worm.copy(); plt.subplot(2,3,2) worm0.plot(image = img) worm0.plot() ### Test Skeletonization from skimage.morphology import skeletonize