def load(self): """Returns worm data as memmap or array""" return exp.load(strain=self.strain, dtype=self.dtype, wid=self.wid, stage=self.stage, label=self.label, valid_only=self.valid_only, replace_invalid=self.replace_invalid, memmap=self.memmap)
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 align(wid=0, verbose=True): rv = rd_speed[wid][:rd_data.stage_switch[wid][-1]].copy() #iv = im_speed[wid]; iv = exp.load(strain='n2', dtype='speed', wid=wid) print 'worm %d: length: img: %d, rd: %d' % (wid, len(iv), len(rv)) print 'worm %d: invalid img: %d, rd: %d' % (wid, np.sum( np.isnan(iv)), np.sum(np.isnan(rv))) # do Shays averaging ivm, am = alg.average_invalidation_wrong(iv) # align data set aa = alg.align_indices(ivm, rv, skip=10, window=500, search=50, start=all) a = am[aa] if verbose: ivmall = alg.average_invalidation_wrong(iv, invalid=False) fig = plt.figure(10) plt.clf() ax = plt.subplot(3, 1, 1) plt.plot(ivmall[a]) plt.plot(rv) plt.title('worm %d, exp %s' % (wid, fo.experiment_names[wid])) plt.subplot(3, 1, 2, sharex=ax) plt.plot(ivmall[a] - rv) plt.subplot(3, 1, 3) plt.plot(iv / 1.0, 'gray') pp = np.ones(len(ivmall)) * np.nan sd = np.sort(np.setdiff1d(range(len(ivmall)), a)) pp[sd] = ivmall[sd] plt.plot(ivmall) plt.plot(pp, 'r') sd2 = np.sort(np.setdiff1d(range(len(ivmall)), am)) pp = np.ones(len(ivmall)) * np.nan pp[sd2] = ivmall[sd2] plt.plot(pp, 'k') plt.ylim(-1, 20) plt.pause(0.01) fig.savefig('align_%d.png' % wid) return a
def align(wid = 0, verbose = True): rv = rd_speed[wid][:rd_data.stage_switch[wid][-1]].copy(); #iv = im_speed[wid]; iv = exp.load(strain = 'n2', dtype = 'speed', wid = wid); print 'worm %d: length: img: %d, rd: %d' % (wid, len(iv), len(rv)) print 'worm %d: invalid img: %d, rd: %d' % (wid, np.sum(np.isnan(iv)), np.sum(np.isnan(rv))) # do Shays averaging ivm, am = alg.average_invalidation_wrong(iv); # align data set aa = alg.align_indices(ivm, rv, skip = 10, window = 500, search = 50, start = all) a = am[aa]; if verbose: ivmall = alg.average_invalidation_wrong(iv, invalid = False); fig = plt.figure(10); plt.clf(); ax = plt.subplot(3,1,1); plt.plot(ivmall[a]); plt.plot(rv) plt.title('worm %d, exp %s' % (wid, fo.experiment_names[wid])) plt.subplot(3,1,2, sharex = ax); plt.plot(ivmall[a] - rv) plt.subplot(3,1,3); plt.plot(iv/1.0, 'gray') pp = np.ones(len(ivmall)) * np.nan; sd = np.sort(np.setdiff1d(range(len(ivmall)), a)); pp[sd] = ivmall[sd]; plt.plot(ivmall); plt.plot(pp, 'r') sd2 = np.sort(np.setdiff1d(range(len(ivmall)), am)); pp = np.ones(len(ivmall)) * np.nan; pp[sd2] = ivmall[sd2]; plt.plot(pp, 'k') plt.ylim(-1,20) plt.pause(0.01); fig.savefig('align_%d.png' % wid); return a;
def plot_shape_trajectory(tstart = 500000, tend = ntime, nfig = 100): xy = exp.load(wid = wid); # head tail positions xycht = np.load(posfile); print xycht.shape # reduce to time window of interest xy = xy[tstart:tend,:]; xycht = xycht[tstart:tend,:]; plt.figure(nfig); plt.clf(); plotTrajectory(xy, cmap = cm.Greys, line = 'gray'); off= [75,75] plotTrajectory(xycht[:,0,:] + xy - off, cmap = cm.Reds, line = 'red'); plotTrajectory(xycht[:,1,:] + xy - off, cmap = cm.Blues, line = None); plotTrajectory(xycht[:,2,:] + xy - off, cmap = cm.Greens, line = None);
def plot_shape_trajectory(tstart=500000, tend=ntime, nfig=100): xy = exp.load(wid=wid) # head tail positions xycht = np.load(posfile) print xycht.shape # reduce to time window of interest xy = xy[tstart:tend, :] xycht = xycht[tstart:tend, :] plt.figure(nfig) plt.clf() plotTrajectory(xy, cmap=cm.Greys, line='gray') off = [75, 75] plotTrajectory(xycht[:, 0, :] + xy - off, cmap=cm.Reds, line='red') plotTrajectory(xycht[:, 1, :] + xy - off, cmap=cm.Blues, line=None) plotTrajectory(xycht[:, 2, :] + xy - off, cmap=cm.Greens, line=None)
strain = 'n2' # 'tph1', 'npr1' strain = 'daf7' nworms, exp_names, dir_names = f.filenames(strain = strain); #%% Convert Worm Images for wid in range(0,nworms): #file_data = os.path.join(dir_names[wid], 'short%s-%s.mat' % ('%04d')); file_data = np.sort(np.unique(np.array(glob.glob(os.path.join(dir_names[wid], 'short*.mat'))))); nf = len(file_data); file_save = os.path.join(exp.data_directory, 'Images/%s_img_w=%d_s=all.npy' % (strain, wid)); wxy = exp.load(strain = strain, wid = wid); n = wxy.shape[0]; si = 0; sf = 0; dat = scipy.io.loadmat(file_data[sf])['mydata'][0]; imgs = np.zeros((n, 151, 151), dtype = 'uint8') for i in range(n): print 'wid: %d step: %d/%d fid:%d,%d' % (wid, i,n, sf, si) img = dat[si]; #check for format otherwise pad if img.ndim != 3: print 'image has different dimensionas: %s, step: %d/%d fid:%d,%d' % (str(img.ndim), i,n, sf, si) img = np.nan * np.ones((151,151,3), dtype = 'uint8');
import numpy as np import analysis.experiment as exp; import matplotlib.pyplot as plt; plt.rcParams['axes.facecolor'] = 'w' plt.rcParams['figure.facecolor'] = 'w' import analysis.video as v; reload(v); #%% Gui reload(v) td = exp.load(wid = 35, dtype = 'roam'); td[np.isnan(td)] = 0.5; #td = np.random.rand(td.shape[0]); dt = 3; g = v.WormGui(strain = 'n2', wid = 35, time = 400000, trajectory_delta=dt, trajectory_length = 3*60*60 / dt, trajectory_data=td, trajectory_cmap=plt.cm.bwr, trajectory_vmin=0, trajectory_vmax=1); #%% #t = 381500; td = exp.load(wid = 35, dtype = 'roam'); td[np.isnan(td)] = 0;
#%% straind = 'daf-7' strain = 'daf7' rd_data = dexp.load_data(strain=straind) rd_speed = rd_data.speed rd_speed_th = rd_speed.copy() th = np.nanpercentile(rd_speed, 95) rd_speed_th[rd_speed_th > th] = th fplt.plot_array(rd_speed_th) v = [] for wid in range(nworms): v.append(exp.load(strain=strain, dtype='speed', wid=wid, memmap=None)) ntimes = max([len(vv) for vv in v]) #v = []; #for wid in range(nworms): # print '%d / %d' % (wid, nworms); # xy = exp.load(strain = 'N2', dtype = 'xy', wid = wid, memmap = None); # xy = xy[np.logical_not(np.any(np.isnan(xy), axis = 1))] # vv = np.linalg.norm(xy[3:,:] - xy[:-3,:], axis = 1); # v.append(vv) ntimes = max([len(u) for u in v]) im_speed = np.ones((nworms, ntimes)) * np.nan for wid, u in enumerate(v): #print '%d / %d' % (wid, nworms)
nstrains = 16; ntot = 16 * 5 + 14; strains = ['n2'] * nstrains; strains.extend(['tph1'] * nstrains); strains.extend(['tdc1'] * nstrains); strains.extend(['npr1'] * nstrains); strains.extend(['daf7'] * 14); strains.extend(['cat2'] * nstrains); #wids = np.array(range(nstrains) * 4)[:ntot]; # roamng fraction wids= []; for s in ['n2','tph1','tdc1', 'npr1','daf7','cat2']: roam = exp.load(strain = s, wid = all, dtype = 'speed'); means = np.array([np.nanmean(r) for r in roam]); ids = np.argsort(means); print ids; nworms = len(ids); if nworms <= 16: wids.append(ids); else: sel = np.array(np.linspace(0, nworms-1, np.min([nworms, 18])), dtype = int); sel = sel[1:-1]; assert(len(sel) == 16) wids.append(ids[sel]); wids = np.hstack(wids); nworms = len(wids);
inv = np.logical_and(xy[:,0] == 1.0, xy[:,1] == 1.0); xy[inv,:] = np.nan; print 'worm %d: len = %d' % (wid, xy.shape[0]) fn_out = exp.filename(strain = strain, wid = wid); np.save(fn_out, xy); #%% Precalculate Speed import analysis.features as feat; delta = 3; for wid in range(nworms): print 'processing worm %d/%d' % (wid, nworms); xy = exp.load(strain = strain, wid = wid, memmap = None); v = feat.speed(xy, delta = delta); fn_out = exp.filename(strain = strain, wid = wid, dtype = 'speed'); np.save(fn_out, v); r = feat.rotation(xy, delta = delta); fn_out = exp.filename(strain = strain, wid = wid, dtype = 'rotation'); np.save(fn_out, r);
straind = 'daf-7' strain = 'daf7'; rd_data = dexp.load_data(strain = straind); rd_speed = rd_data.speed; rd_speed_th = rd_speed.copy(); th = np.nanpercentile(rd_speed, 95); rd_speed_th[rd_speed_th > th] = th; fplt.plot_array(rd_speed_th) v = []; for wid in range(nworms): v.append(exp.load(strain = strain, dtype = 'speed', wid = wid, memmap = None)); ntimes = max([len(vv) for vv in v]) #v = []; #for wid in range(nworms): # print '%d / %d' % (wid, nworms); # xy = exp.load(strain = 'N2', dtype = 'xy', wid = wid, memmap = None); # xy = xy[np.logical_not(np.any(np.isnan(xy), axis = 1))] # vv = np.linalg.norm(xy[3:,:] - xy[:-3,:], axis = 1); # v.append(vv) ntimes = max([len(u) for u in v]) im_speed = np.ones((nworms, ntimes)) * np.nan; for wid, u in enumerate(v):
plt.rcParams['axes.facecolor'] = 'w' plt.rcParams['figure.facecolor'] = 'w' import analysis.video as v; reload(v); #%% All worms wids = np.arange(124); wids[90] = 124; # wid = 90 is corrupted data ! # sort via average roaming fraction roaming = exp.load(strain = 'n2', wid = wids, dtype = 'roam'); roaming_mean = np.array([np.nanmean(r) for r in roaming]); rorder = np.argsort(roaming_mean); wids = np.array(wids)[rorder]; #%% persitence based ordering wids = np.array(np.genfromtxt('/home/ckirst/Science/Projects/CElegans/Analysis/WormBehaviour/Data/persistance_median_order.csv'), dtype = int); wids = wids - 1;
return x_mean; #%% Parameter rate = 3; # Hz sample rate time_bin = 5*60; # [sec] for averaging # derived nbins = rate * time_bin; # number of time bins to calculate mean ### Create Data Matrices r_all_0 = []; nmax = 0; for wid in range(nworms): r = exp.load(strain = strain, wid = wid, dtype = 'rotation') r_mean = average(r, nbins); r_all_0.append(r_mean); nmax = max(nmax, len(r_mean)); r_all = np.zeros((nworms, nmax)); for wid in range(nworms): r_all[wid,:len(r_all_0[wid])] = r_all_0[wid]; v_all_0 = []; nmax = 0; for wid in range(nworms): v = exp.load(strain = strain, wid = wid, dtype = 'speed') v_mean = average(v, nbins); v_all_0.append(v_mean);
import os import numpy as np import matplotlib.pyplot as plt import copy import time import imageprocessing.active_worm as aw import analysis.experiment as exp wid = 80 xydata = exp.load(wid=wid) ntime = xydata.shape[0] #create results data array for traces, points basedir = '/home/ckirst/Science/Projects/CElegansBehaviour/Analysis/Data/2016_07_23_Wormshapes/' shapefile = os.path.join(basedir, "shape_wid=%d_shapes.npy" % wid) measurefile = os.path.join(basedir, "shape_wid=%d_measures.npy" % wid) figfile = os.path.join(basedir, "shape_wid=%d_t=%s.png" % (wid, "%d")) # create the files npoints = 21 #shapedata = np.zeros((ntime,3*npoints)); #np.save(shapefile, shapedata);
def find_plate(strain, wid=0, verbose=True, save=None, thresholds=[160, 175]): fns = np.sort( np.unique(glob.glob(os.path.join(dir_names[wid], 'Traj*.mat')))) img_plate = scipy.io.loadmat(fns[-1])['Trace'] xy = exp.load(strain=strain, wid=wid, dtype='xy') ### Detect circles import cv2 gray = 2**8 - np.array(np.sum(img_plate, axis=2) / 3, dtype='uint8') th1 = thresholds[0] th2 = thresholds[1] gray[gray < th1] = th1 gray[gray > th2] = th2 gray = np.array(gray, dtype='uint8') #plt.figure(8); plt.clf(); #plt.subplot(1,2,1); #plt.imshow(gray, cmap = plt.cm.gray) #nn = 1500; #plt.scatter(np.round(xy[-nn:,0]-1), np.round(xy[-nn:,1]-1)) #plt.subplot(1,2,2); #plt.imshow(gray > 200) #cimg = cv2.cvtColor(gray,cv2.COLOR_GRAY2BGR) circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, dp=2, minDist=100, minRadius=485, maxRadius=500, param1=40, param2=50) # detect the correct dish center = circles[0, :, :2] radius = circles[0, :, 2] xy = exp.load(strain=strain, wid=wid, dtype='xy') xyv = xy[np.logical_not(np.isnan(xy[:, 0]))] pt = xyv[-1] if verbose: if circles is None: print 'worm %d / %d: no cirlces found!' % (wid, nworms) else: print 'worm %d / %d: %d circles found !' % (wid, nworms, len(circles[0])) fig = plt.figure(8) plt.clf() plt.imshow(gray, cmap=plt.cm.gray) ax = plt.gcf().gca() for (x, y, r) in circles[0]: # draw the outer circle ax.add_artist( plt.Circle((x, y), r, color='green', fill=False, linewidth=2)) # draw the center of the circle ax.add_artist(plt.Circle((x, y), 2, color='green', fill=True)) plt.title('Worm %s, Experiment: %s' % (wid, exp_names[wid])) plt.show() plate = np.where(np.linalg.norm(center - pt, axis=1) < 1 * radius)[0] #if len(plate) == 0: # plate = np.where(np.linalg.norm(center - pt, axis = 1) < 1.05 * radius)[0]; if len(plate) != 1: print 'could not find plate for worm %d, %s!' % (wid, strain) assert False plate = plate[0] x = center[plate, 0] y = center[plate, 1] r = radius[plate] if verbose: ax.add_artist( plt.Circle((x, y), r, color='red', fill=False, linewidth=2)) ax.add_artist(plt.Circle((x, y), 2, color='red', fill=True)) plt.pause(0.01) if save is not None: fig.savefig(save) return [x, y, r]
nworms = len(exp_times); rate = 3; # Hz sample rate time_bin = 1*30; # s; nbins = rate * time_bin; # number of time bins to calculate mean ntimes = 5 * 24 * 60 * 60 / time_bin; # totla number of time steps in analysis v_24hr = np.zeros((nworms, ntimes)) * np.nan; r_24hr = np.zeros((nworms, ntimes)) * np.nan; # calculate data for wid in range(nworms): xy = exp.load(strain = 'N2', dtype = 'xy', wid = wid, memmap=None); rt = exp.load(strain = 'N2', dtype = 'phi',wid = wid, memmap=None); # invalid positions inv = xy[:,0] == 1.0; xy[inv,:] = np.nan; # calculate speed dxy = xy[1:,:] - xy[:-1,:]; v = np.sqrt(dxy[:,0]**2 + dxy[:,1]**2) * rate; nv = len(v); # bin speed nvm = nv/nbins * nbins; v_mean = v[:nvm]; v_mean = v_mean.reshape([nvm/nbins, nbins]);
rate = 3 # Hz sample rate time_bin = 1 * 60 # s; nbins = rate * time_bin # number of time bins to calculate mean v_data = [] r_data = [] for wid in range(nworms): print 'processing %d / %d' % (wid, nworms) xy = exp.load(strain='N2', dtype='xy', wid=wid, memmap=None) rt = exp.load(strain='N2', dtype='phi', wid=wid, memmap=None) # invalid positions inv = xy[:, 0] == 1.0 xy[inv, :] = np.nan # calculate speed dxy = np.diff(xy, axis=0) v = np.sqrt(dxy[:, 0]**2 + dxy[:, 1]**2) * rate nv = min(len(v), len(rt)) # bin speed nvm = nv / nbins * nbins v_mean = v[:nvm] v_mean = v_mean.reshape([nvm / nbins, nbins])
__author__ = 'Christoph Kirst <*****@*****.**>' __docformat__ = 'rest' import os import glob import matplotlib.pyplot as plt; import scipy.io import numpy as np import analysis.experiment as exp exp.experiment_directory = '/home/ckirst/Science/Projects/CElegansBehaviour/Experiment/ImageData' xy = exp.load(strain = 'N2', dtype = 'xy', wid = 0) ### Create features from xy positions # xy.shape = (ntimes, 2) def dxy(xy, delta = 1): return (xy[delta:] - xy[:-delta]); def speed(xy, delta = 1, dt = 1): # def distance(xy, delta = 1): d = speed(xy, delta, dt = 1);
nstrains = 16 ntot = 16 * 5 + 14 strains = ['n2'] * nstrains strains.extend(['tph1'] * nstrains) strains.extend(['tdc1'] * nstrains) strains.extend(['npr1'] * nstrains) strains.extend(['daf7'] * 14) strains.extend(['cat2'] * nstrains) #wids = np.array(range(nstrains) * 4)[:ntot]; # roamng fraction wids = [] for s in ['n2', 'tph1', 'tdc1', 'npr1', 'daf7', 'cat2']: roam = exp.load(strain=s, wid=all, dtype='speed') means = np.array([np.nanmean(r) for r in roam]) ids = np.argsort(means) print ids nworms = len(ids) if nworms <= 16: wids.append(ids) else: sel = np.array(np.linspace(0, nworms - 1, np.min([nworms, 18])), dtype=int) sel = sel[1:-1] assert (len(sel) == 16) wids.append(ids[sel]) wids = np.hstack(wids) nworms = len(wids)
data_files = [ os.path.join(data_path, 'analysis_2016_10_26_' + n + '.npy') for n in data_names ] fig_dir = '/home/ckirst/Desktop/labmeeting' ### match head and tails data = {n: np.load(f, mmap_mode='r+') for n, f in zip(data_names, data_files)} theta = data["theta"] center = data["center"] orient = data["orientation"] pos = exp.load(wid=80, dtype='xy') #head vs center pos c_pos = pos + data["xy"] h_pos = pos + data["center"][:, 0] t_pos = pos + data["center"][:, -1] valid = data["width"][:, 0] != -1 plt.figure(1) plt.clf() t0 = 0 t1 = 1000 plt.plot(t_pos[t0:t1, 0], t_pos[t0:t1, 1]) plt.plot(h_pos[t0:t1, 0], h_pos[t0:t1, 1]) #match head tail postions
coords.append(xy_part) xy = np.vstack(coords) # (1,1) is invalid coordinates inv = np.logical_and(xy[:, 0] == 1.0, xy[:, 1] == 1.0) xy[inv, :] = np.nan print 'worm %d: len = %d' % (wid, xy.shape[0]) fn_out = exp.filename(strain=strain, wid=wid) np.save(fn_out, xy) #%% Precalculate Speed import analysis.features as feat delta = 3 for wid in range(nworms): print 'processing worm %d/%d' % (wid, nworms) xy = exp.load(strain=strain, wid=wid, memmap=None) v = feat.speed(xy, delta=delta) fn_out = exp.filename(strain=strain, wid=wid, dtype='speed') np.save(fn_out, v) r = feat.rotation(xy, delta=delta) fn_out = exp.filename(strain=strain, wid=wid, dtype='rotation') np.save(fn_out, r)
def find_plate(strain, wid = 0, verbose = True, save = None, thresholds = [160, 175]): fns = np.sort(np.unique(glob.glob(os.path.join(dir_names[wid], 'Traj*.mat')))) img_plate = scipy.io.loadmat(fns[-1])['Trace']; xy = exp.load(strain = strain, wid = wid, dtype = 'xy') ### Detect circles import cv2 gray = 2**8 - np.array(np.sum(img_plate, axis = 2) / 3, dtype = 'uint8'); th1 = thresholds[0]; th2 = thresholds[1]; gray[gray < th1] = th1; gray[gray > th2] = th2; gray = np.array(gray, dtype = 'uint8'); #plt.figure(8); plt.clf(); #plt.subplot(1,2,1); #plt.imshow(gray, cmap = plt.cm.gray) #nn = 1500; #plt.scatter(np.round(xy[-nn:,0]-1), np.round(xy[-nn:,1]-1)) #plt.subplot(1,2,2); #plt.imshow(gray > 200) #cimg = cv2.cvtColor(gray,cv2.COLOR_GRAY2BGR) circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, dp = 2, minDist = 100, minRadius = 485, maxRadius = 500, param1 = 40, param2 = 50) # detect the correct dish center = circles[0, :,:2] radius = circles[0, :, 2] xy = exp.load(strain = strain, wid = wid, dtype = 'xy') xyv = xy[np.logical_not(np.isnan(xy[:,0]))]; pt = xyv[-1] if verbose: if circles is None: print 'worm %d / %d: no cirlces found!' % (wid, nworms) else: print 'worm %d / %d: %d circles found !' % (wid, nworms, len(circles[0])) fig = plt.figure(8); plt.clf(); plt.imshow(gray, cmap = plt.cm.gray) ax = plt.gcf().gca(); for (x, y, r) in circles[0]: # draw the outer circle ax.add_artist(plt.Circle((x,y),r, color = 'green',fill = False, linewidth = 2)) # draw the center of the circle ax.add_artist(plt.Circle((x,y),2, color = 'green', fill = True)) plt.title('Worm %s, Experiment: %s' % (wid, exp_names[wid]) ) plt.show(); plate = np.where(np.linalg.norm(center - pt, axis = 1) < 1 * radius)[0]; #if len(plate) == 0: # plate = np.where(np.linalg.norm(center - pt, axis = 1) < 1.05 * radius)[0]; if len(plate) != 1: print 'could not find plate for worm %d, %s!' % (wid, strain); assert False; plate = plate[0]; x = center[plate,0]; y = center[plate,1]; r = radius[plate] if verbose: ax.add_artist(plt.Circle((x,y),r, color = 'red',fill = False, linewidth = 2)) ax.add_artist(plt.Circle((x,y),2, color = 'red', fill = True)) plt.pause(0.01) if save is not None: fig.savefig(save); return [x,y,r];
time_data = None, time_size = 0.5, time_cmap = plt.cm.Reds, border = 60, vmin = 84, vmax = 92, save = fname, fps = 10, dpi = 360, time_stamp = True, font_size = 8, font_size_title = 12) #%% Make Movie of 100 worms reload(v) wids = range(110); wids[90] = 111; # sort via average roaming fraction roaming = exp.load(strain = 'n2', wid = wids, dtype = 'roam'); roaming_mean = np.array([np.nanmean(r) for r in roaming]); rorder = np.argsort(roaming_mean); wids = np.array(wids)[rorder]; #%% stimes= exp.load_stage_times(strain = 'n2', wid = wids)[:,:-1]; duration = np.sum(np.diff(stimes, axis = 1), axis = 1); duration_min = np.min(duration); dtmin = 1;
n = size[0] * size[1]; return np.array([1.0 * xx.sum() / n, 1.0 * yy.sum() / n]); plt.figure(7); plt.clf(); plt.subplot(1,3,1) plt.imshow(img1t); plt.subplot(1,3,2); plt.imshow(img2t); plt.subplot(1,3,3) plt.imshow(img1t - img2t) ## what is the original position: pos = exp.load(wid = 80) xy10 = pos[t0]; xy20 = pos[t0+1] xy1 = center_of_mass(img1t) xy2 = center_of_mass(img2t) xy10 - xy1 xy20 - xy2 def align_images(img1, img2): def
import matplotlib.pyplot as plt plt.rcParams['axes.facecolor'] = 'w' plt.rcParams['figure.facecolor'] = 'w' import analysis.video as v reload(v) #%% All worms wids = np.arange(124) wids[90] = 124 # wid = 90 is corrupted data ! # sort via average roaming fraction roaming = exp.load(strain='n2', wid=wids, dtype='roam') roaming_mean = np.array([np.nanmean(r) for r in roaming]) rorder = np.argsort(roaming_mean) wids = np.array(wids)[rorder] #%% persitence based ordering wids = np.array(np.genfromtxt( '/home/ckirst/Science/Projects/CElegans/Analysis/WormBehaviour/Data/persistance_median_order.csv' ), dtype=int) wids = wids - 1
import numpy as np import matplotlib.pyplot as plt; import copy import time import imageprocessing.active_worm as aw; import analysis.experiment as exp; wid = 80; xydata = exp.load(wid = wid) ntime = xydata.shape[0]; #create results data array for traces, points basedir = '/home/ckirst/Science/Projects/CElegansBehaviour/Analysis/Data/2016_07_23_Wormshapes/' shapefile = os.path.join(basedir, "shape_wid=%d_shapes.npy" % wid); measurefile = os.path.join(basedir, "shape_wid=%d_measures.npy" % wid); figfile = os.path.join(basedir, "shape_wid=%d_t=%s.png" % (wid, "%d")); # create the files npoints = 21; #shapedata = np.zeros((ntime,3*npoints)); #np.save(shapefile, shapedata);
data_files = [os.path.join(data_path, 'analysis_2016_10_26_' + n + '.npy') for n in data_names]; fig_dir = '/home/ckirst/Desktop/labmeeting' ### match head and tails data = {n : np.load(f, mmap_mode = 'r+') for n,f in zip(data_names, data_files)}; theta = data["theta"]; center = data["center"] orient = data["orientation"] pos = exp.load(wid = 80, dtype = 'xy') #head vs center pos c_pos = pos + data["xy"]; h_pos = pos + data["center"][:,0]; t_pos = pos + data["center"][:,-1]; valid = data["width"][:,0] != -1; plt.figure(1); plt.clf(); t0 = 0; t1 = 1000; plt.plot(t_pos[t0:t1,0], t_pos[t0:t1,1]) plt.plot(h_pos[t0:t1,0], h_pos[t0:t1,1]) #match head tail postions
def load(self): """Returns worm data as memmap or array""" return exp.load(strain = self.strain, dtype = self.dtype, wid = self.wid, stage = self.stage, label = self.label, valid_only = self.valid_only, replace_invalid = self.replace_invalid, memmap = self.memmap);
import os import numpy as np import analysis.experiment as exp import matplotlib.pyplot as plt plt.rcParams['axes.facecolor'] = 'w' plt.rcParams['figure.facecolor'] = 'w' import analysis.video as v reload(v) #%% Gui reload(v) td = exp.load(wid=35, dtype='roam') td[np.isnan(td)] = 0.5 #td = np.random.rand(td.shape[0]); dt = 3 g = v.WormGui(strain='n2', wid=35, time=400000, trajectory_delta=dt, trajectory_length=3 * 60 * 60 / dt, trajectory_data=td, trajectory_cmap=plt.cm.bwr, trajectory_vmin=0, trajectory_vmax=1)