def norm(i): print 'scale %d' % i fres = exp.load('%s_%s_scales_mean.npy' % (strain, feat), memmap='r') fres_tn = exp.load('%s_%s_time_normalized_scales_mean.npy' % (strain, feat), memmap='r+') fres_tn[:, i, :] = exp.bin_data(fres[:, i, :], sbins)
fig.savefig( os.path.join(save_fig, '%s_%s.png' % (strain, 'stage_durations'))) ### Mean feature accross worms and stages d = getattr(data, feat) #average+order dm = np.sum(d, axis=1) * 1.0 / data.total_time dorder = np.argsort(dm) exp.save(dorder, '%s_%s_order.npy' % (strain, feat)) #stage resolved averages sbins = exp.stage_bins(data, nbins=1) dms = exp.bin_data(d, sbins) dms_shuffle = dms.copy() for i in range(dms.shape[1]): dms_shuffle[:, i] = np.random.permutation(dms_shuffle[:, i]) dms_data = [dms, dms_shuffle] fig = plt.figure(20 + i) plt.clf() plt.subplot(3, 2, 1) plt.plot(dm[dorder]) for s in range(data.nstages): plt.plot(dm[np.argsort(dms[:, s])]) plt.title('%s %s - total mean' % (strain, feat)) for s in range(data.nstages): plt.subplot(3, 2, s + 2)
dat_bin = {} dat_mean = {} dat_var = {} dat = {} stage_bins = {} dat_bin_s = {} for strain in strains: print 'processing %s...' % strain data[strain] = exp.load_data(strain) dat[strain] = getattr(data[strain], feat) sbinsb = exp.stage_bins(data[strain], nbins=sbins) dat_bin[strain] = exp.bin_data(dat[strain], sbinsb) dat_mean[strain] = np.mean(dat_bin[strain], axis=0) dat_var[strain] = np.var(dat_bin[strain], axis=0) stage_bins[strain] = exp.stage_bins(data[strain], nbins=1) dat_bin_s[strain] = exp.bin_data(dat[strain], stage_bins[strain]) #%% Order by activity order = {} for strain in strains: order[strain] = np.argsort(np.sum(dat[strain], axis=1)) #%% Plot Data # plot roaming fraction
# calculate roaming a and dwelling intervalls -> ideally after smoothing with HHM ? data = exp.load_data(strain); dat = getattr(data, feat); #dat_scales = exp.load('%s_%s_time_normalized_scales_mean.npy' % (strain , feat)); dbins = 7; sbins = 50; nbins = 5 * sbins; sbins = exp.stage_bins(data, nbins= sbins); dat_bin = exp.bin_data(dat, sbins); dat_mean = np.mean(dat_bin, axis = 0); dat_var = np.var(dat_bin, axis = 0); dist = np.zeros((dbins, nbins)); for b in range(nbins): dist[:,b] += np.histogram(dat_bin[:,b], range = (0, 0.9), bins = dbins)[0]; dist[:,b] /= np.sum(dist[:,b]); plt.figure(6); plt.clf(); fplt.plot_image_array((dist,), names = ['dist_%s' % feat], invert_y = True, nplots = 3); plt.subplot(3,1,2); plt.plot(dat_mean, 'r'); plt.title('%s_%s mean' % (strain, feat)) plt.subplot(3,1,3)
import plot as fplt strain = 'N2' feat = 'roam' nbins = 2**13 save_fig = True data = exp.load_data(strain) ### Bin data sbins = exp.stage_bins(data, nbins=nbins) d = getattr(data, feat) tn = exp.bin_data(d, sbins) exp.save(tn, '%s_%s_time_normalized.npy' % (strain, feat)) #fig = plt.figure(1); plt.clf(); #plt.imshow(tn, aspect = 'auto'); #plt.tight_layout(); #plt.title('%s %s time normalized' % (strain, feat)); #if save_fig: # fig.savefig(exp.figname('%s_%s_time_normalized.png'% (strain, feat))); fig = fplt.plot_array(tn, title='%s %s time normalized' % (strain, feat)) fplt.savefig(fig, exp.figname('%s_%s_time_normalized.png' % (strain, feat)), width=2500)
sbins_start, sbins_end = exp.stage_bins(data, nbins=2**5) nbins_total = sbins_start.shape[1] #nparam = nstates * (nstates-1) + nstates; #transition parameter + emission prob for 1 nparam = 6 nworms = d.shape[0] probs = np.zeros((nparam, nbins_total)) for i in range(nbins_total): print '%d/%d' % (i, nbins_total) model = hmm(nstates=2) dd = [d[:, sbins_start[j][i]:sbins_end[j][i]] for j in range(nworms)] model.fit(dd, max_iterations=500, stop_threshold=10e-5, n_jobs=12) probs[:, i] = model_to_param(model) probs[3, :] = 1 - probs[3, :] droam = exp.bin_data(d, (sbins_start, sbins_end)) plt.figure(2) plt.clf() plt.subplot(4, 1, 1) plt.imshow(probs[:2, :], aspect='auto', interpolation='none', vmin=0.8) plt.subplot(4, 1, 2) plt.imshow(probs[2:4, :], aspect='auto', interpolation='none', vmin=0.85) plt.subplot(4, 1, 3) plt.imshow(probs[4:, :], aspect='auto', interpolation='none') plt.subplot(4, 1, 4) plt.plot(np.mean(droam, axis=0)) plt.xlim(0, droam.shape[1]) plt.tight_layout() np.save('%s_hmm.npy' % strain, probs)