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)
  
Пример #2
0
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)
fig = plt.figure(180)
plt.clf()
for i in range(9):
    tt = pca_comp[i]
    #tta = tt - np.mean(tt);
    plt.subplot(3, 3, i + 1)
    plt.plot(tt, linewidth=2, c='r')
    plt.title('PCA %d' % (i + 1))
    plt.ylim(-0.6, 0.6)

fig.savefig(os.path.join(fig_dir, 'theta_pca_9_plot.png'), facecolor='white')

pcs = pca.Y
pcs.shape

aplt.plot_array(pcs.T)

fig = plt.figure(190)
plt.clf()
#plt.imshow(pca.Wt, interpolation = 'none', aspect = 'auto', cmap = 'viridis')
#plt.colorbar(pad = 0.01,fraction = 0.01)
#plt.title('pca vectors');
plt.plot(np.cumsum(pca.fracs), 'k', linewidth=1)
plt.ylim(0, 1)

fig.savefig(os.path.join(fig_dir, 'theta_pca_variance.png'), facecolor='white')

## time evolution of components

fig = plt.figure(200)
plt.clf()
#  v = exp.load(strain = strain, wid = wid, dtype = 'speed')
#  #transitions_id_fine[wid][1:-1] = find_transitions(v, transitions_id[wid][1:-1], window = 20000, bin_width = 3 * 60)
#  transitions_id_fine[wid][1:-1] = find_transitions(v, transitions_id[wid][1:-1], window = 10000, bin_width = 3 * 60 * 15)

# not fine tuning really required !

#%% Plot original data with transition points

r_full_plt = r_full.copy();
r_full_plt[np.isnan(r_full)] = 0;
r_th = np.percentile(r_full_plt.flat, [95]);
r_full_plt[r_full_plt > r_th] = r_th;
for wid in range(nworms):
  for s in range(6):
    r_full_plt[wid, max(0, transitions_id_fine[wid,s]-200):min(r_full_plt.shape[1], transitions_id_fine[wid,s]+200)] = 1.2*r_th
fplt.plot_array(r_full_plt)

v_full_plt = v_full.copy();
v_full_plt[np.isnan(v_full)] = 0;
v_th = np.percentile(v_full_plt.flat, [95]);
v_full_plt[v_full_plt > v_th] = v_th;
for wid in range(nworms):
  for s in range(6):
    v_full_plt[wid, max(0, transitions_id_fine[wid,s]-200):min(v_full_plt.shape[1], transitions_id_fine[wid,s]+200)] = 1.2*v_th
fplt.plot_array(v_full_plt)



#%%############################################################################
### Save data
r_full = np.zeros((nworms, lr));
for wid in range(nworms):
  v = exp.load(strain = strain, dtype = 'speed', wid = wid, memmap = None);
  nv = len(v);
  v_full[wid,:nv] = v;
  
  r = exp.load(strain = strain, dtype = 'rotation', wid = wid, memmap = None);
  nr = r.shape[0];
  r_full[wid,:nr] = r;


#%%
r_plt = r_all.copy();
r_plt[np.isnan(r_plt)] = 0.0;

fplt.plot_array(r_plt)


#%% Determine Lethargus phases

l_pks_0 = [];
k = 0;
verbose = True;

for wid in range(nworms):
  r = exp.load(strain = strain, wid = wid, dtype = 'rotation')
  r_mean = average(r, nbins);
  
  # smooth roaming curve
  rf = sig.savgol_filter(r_mean, 51, 7)
plt.clf()
plt.hist(curv, bins=256)

plt.figure(1)
plt.clf()
plt.hist(bend, bins=256)

# resolve for stages etc

mt = np.max(np.abs(theta), axis=1)
idx = mt < 1

theta_red = theta[idx]
theta_red.shape

aplt.plot_array(theta_red.T)

### PCA
plt.figure(12)
plt.clf()
aplt.plot_pca(theta)

pca = aplt.PCA(theta)

pca_comp = pca.Wt

import worm.model as wm
import worm.geometry as wgeo
w = wm.WormModel(npoints=22)

fig = plt.figure(17)
strain = 'daf7'
nworms, exp_names, dir_names = f.filenames(strain = strain);


#%%

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)
  #tta = tt - np.mean(tt);
  plt.subplot(3,3,i+1)
  plt.plot(tt, linewidth = 2, c = 'r')
  plt.title('PCA %d' % (i+1))
  plt.ylim(-0.6, 0.6)

fig.savefig(os.path.join(fig_dir, 'theta_pca_9_plot.png'),  facecolor='white')
      



pcs = pca.Y;
pcs.shape


aplt.plot_array(pcs.T)

fig = plt.figure(190); plt.clf();
#plt.imshow(pca.Wt, interpolation = 'none', aspect = 'auto', cmap = 'viridis')
#plt.colorbar(pad = 0.01,fraction = 0.01)
#plt.title('pca vectors');
plt.plot(np.cumsum(pca.fracs), 'k', linewidth = 1)  
plt.ylim(0,1)
  
fig.savefig(os.path.join(fig_dir, 'theta_pca_variance.png'),  facecolor='white')  
  
  

## time evolution of components

fig = plt.figure(200); plt.clf();
Пример #9
0
"""

__author__ = 'Christoph Kirst <*****@*****.**>'
__license__ = 'MIT License <http://www.opensource.org/licenses/mit-license.php>'
__docformat__ = 'rest'

import numpy as np
import scipy.io as io

import analysis.experiment as exp
import analysis.plot as plt

dat = exp.load_stage_binned(dtype='roam', nbins_per_stage=75)
dat[np.isnan(dat)] = 0

plt.plot_array(dat)

mdict = {'roaming': dat}

io.savemat(
    '/home/ckirst/Science/Projects/CElegansBehaviour/Analysis/WormBehaviour/Data/roaming_auto_stages.mat',
    mdict)

dat = exp.load_stage_binned(dtype='roam', nbins_per_stage=10)
dat[np.isnan(dat)] = 0

plt.plot_array(dat)

mdict = {'roaming': dat}

io.savemat(


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);

w.from_image(img, sigma = None)
w.plot(image = img);
Пример #11
0
__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)

w.from_image(img, sigma=None)
strain = 'daf7'
nworms, exp_names, dir_names = f.filenames(strain=strain)

#%%

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])