In [26]:
# import multi tracker analysis
In [27]:
# load hdf5 data as a pandas dataframe, and load the configuration
# change the argument to match your data path!
pd, config = mta.read_hdf5_file_to_pandas.load_and_preprocess_data('/home/lab/demo/demo_1/retracked/20170711_150010_N1/20170717_105053_N1_trackedobjects.hdf5')
In [28]:
# pandas dataframe structure: each row is a measurement corresponding to one of the objects for each frame
pd.keys()
Out[28]:
In [29]:
# show the object id's
import numpy as np
np.unique(pd.objid)
Out[29]:
In [30]:
position_for_10 = pd[pd.objid==10].position_x.values
position_for_10
Out[30]:
In [31]:
# wrap pandas dataframe into a dataset object for intuitive processing
# note: using copy=True creates a copy of the data
# this allows you to add attributes to trajecties and pickle the dataset structure for future use
dataset = mta.read_hdf5_file_to_pandas.Dataset(pd, config=config)
dataset.load_keys() # load the object ids
trajec = dataset.trajec(dataset.keys[7]) # load the 7th trajectory
trajec.position_x # show the position x data
Out[31]:
In [32]:
import matplotlib.pyplot as plt
%matplotlib inline
In [33]:
# plot the trajectory; this requires downloading my plotting library: https://github.com/florisvb/FlyPlotLib
# make a heatmap of the trajectories
bgimg = mta.plot.get_filename(config.path, 'bgimg')
binsx, binsy = mta.plot.get_bins_from_backgroundimage(bgimg)
mta.plot.plot_trajectories(pd, binsx, binsy)
In [34]:
mta.plot.plot_heatmap(pd, binsx, binsy)
Out[34]:
In [ ]:
#probability of crossing to lf or rt v. time
#each objid doesn't have a recorded positon for entire duration of video
#access time and align array? add 0s in the gaps
rt_crossed = []
lf_crossed = []
for i in range(len(np.unique(pd.objid))):
trajec = dataset.trajec(dataset.keys[i])
rt_crossing=[]
lf_crossing=[]
for i in range(len(trajec.position_x)):
cross = []
if trajec.position_x[i]<700:
cross.extend(0)
elif trajec.position_x[i]>700:
cross.extend(1)
for i in range(len(cross))
if cross[i+1]-cross[i]=0
rt_crossing.extend(0)
lf_crossing.extend(0)
if cross[i+1]-cross[i]=1
rt_crossing.extend(1)
lt_crossing.extend(0)
elif cross[i+1]-cross[i]=-1
rt_crossing.extend(0)
lf_crossing.extend(1)
rt_crossed.append[rt_crossing]
lf_crossed.append[lf_crossing]
prob_rt=[]
prob_lf=[]
for i in range(len(cross)):
for ii in range(len(cross)):
sum_rt = 0
sum_lf = 0
sum_rt = sum_rt + rt_crossed[ii][i]
sum_lf = sum_lf + lf_crossed[ii][i]
prob_cross_rt = 0
prob_cross_lf = 0
prob_cross_rt = sum_rt/len(crossed)
prob_cross_lf = -1* sum_lf/len(crossed)
prob_rt.extend(prob_cross_rt)
prob_lf.extend(prob_cross_lf)
plt.plot(prob_rt)
plt.plot(prob_lf)
plt.ylabel('probability of crossing (+)right (-)left')
plt.xlabel('time')
odor_pulse = []
lf_edge=0
rt_edge=2
for i in range(int(math.ceil(frames/30))):
lf_edge= lf_edge+28
rt_edge= rt_edge+28
plt.axvspan(lf_edge, rt_edge, 0, 1, alpha=0.5, color= 'yellow')
plt.show()
In [ ]:
#lf & rt occupancy v. time
#same problem as above
b = []
position = []
trajec = dataset.trajec(dataset.keys[5])
time = len(trajec.position_x)
for i in range(len(np.unique(pd.objid))):
trajec = dataset.trajec(dataset.keys[i])
position.append(trajec.position_x)
for i in range (time):
rt = (np.sum(position[:,i]<700))
lf = len(position)-rt
b.append([lf,rt])
rt =0
lf =0
#plot blobs on lf or rt w/ odor pulses
plt.plot(b)
plt.ylabel('flies')
plt.xlabel('time')
odor_pulse = []
lf_edge=0
rt_edge=2
#y_max = max(b)
#print y_max
for i in range(int(math.ceil(frames/30))):
lf_edge= lf_edge+28
rt_edge= rt_edge+28
plt.axvspan(lf_edge, rt_edge, 0, 40, alpha=0.5, color= 'yellow')
plt.show()
In [ ]:
#avg walking speed v. time
#use .speed?
#assuming that once an object get tracked, it stays until end of video
speed = []
max_len = 0
for i in range (len(np.unique(pd.objid))):
trajec = dataset.trajec(dataset.keys[i])
speed.append(trajec.speed)
for i in range (len(speed)):
curr_len = len(speed[i ,:])
if curr_len > max_len:
max_len = curr_len