Commit d832900d authored by amichaut's avatar amichaut
Browse files

integrating new subset filtering in notebook

parent 12a1c688
Pipeline #55201 passed with stages
in 10 seconds
This diff is collapsed.
This diff is collapsed.
......@@ -142,22 +142,24 @@ def plot_traj(df, frame, data_dir, groups=None, image={'image_fn': None, 't_dim'
plot_dir = osp.join(data_dir, 'traj')
tpr.safe_mkdir(plot_dir)
if plot3D:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
xmin, xmax, ymin, ymax = ax.axis('off')
else:
bkgd = tpr.get_background(image=image, frame=frame, df=df, no_bkg=no_bkg,
image_size=image_size,axis_on=show_axis, dpi=plot_config['dpi'])
fig = bkgd['fig']
ax = bkgd['ax']
xmin = bkgd['xmin']
ymin = bkgd['ymin']
xmax = bkgd['xmax']
ymax = bkgd['ymax']
no_bkg = bkgd['no_bkg']
# 3D PLOTTING. not supported anymore
# if plot3D:
# fig = plt.figure()
# ax = fig.add_subplot(111, projection='3d')
# xmin, xmax, ymin, ymax = ax.axis('off')
# else:
# Get background image
bkgd = tpr.get_background(image=image, frame=frame, df=df, no_bkg=no_bkg,
image_size=image_size,axis_on=show_axis, dpi=plot_config['dpi'])
fig = bkgd['fig']
ax = bkgd['ax']
xmin = bkgd['xmin']
ymin = bkgd['ymin']
xmax = bkgd['xmax']
ymax = bkgd['ymax']
no_bkg = bkgd['no_bkg']
# plotting
# get frame dataframe
groups = df.groupby('frame') if groups is None else groups
group = groups.get_group(frame).reset_index(drop=True)
......@@ -183,8 +185,9 @@ def plot_traj(df, frame, data_dir, groups=None, image={'image_fn': None, 't_dim'
colors = tpr.get_cmap_color(t, cmap, vmin=cmap_lim[0], vmax=cmap_lim[1])
elif color_code == "ROI":
if 'group' in group.columns:
colors = [color_list[i % len(color_list)] for i in group['group'].values]
# check there are subsets in df
if 'subset' in group.columns:
colors = [color_list[i % len(color_list)] for i in group['subset_order'].values] # if too many colors repeat cycle
else:
colors = color_list[0] # if color_coded by group but there's none, use only one color
......@@ -203,11 +206,8 @@ def plot_traj(df, frame, data_dir, groups=None, image={'image_fn': None, 't_dim'
color_ = 'k' if no_bkg else 'w'
ax.text(x, y, group['track_lab'].values, fontsize=lab_size, color=color_)
# plot
if not plot3D:
ax.scatter(x, y, s=ms, color=colors)
else:
pass
# plot points
ax.scatter(x, y, s=ms, color=colors)
#### plotting trajectories as lines
if show_tail:
......@@ -227,8 +227,8 @@ def plot_traj(df, frame, data_dir, groups=None, image={'image_fn': None, 't_dim'
elif color_code == "t":
colors = tpr.get_cmap_color(t, cmap, vmin=cmap_lim[0], vmax=cmap_lim[1])
elif color_code == "ROI":
if 'group' in traj.columns:
colors = color_list[traj['group'].values[0] % len(color_list)]
if 'subset' in traj.columns:
colors = color_list[traj['subset_order'].values[0] % len(color_list)]
else:
colors = color_list[0] # if color_coded by group but there's none, use only one color
elif color_code == "random":
......@@ -239,28 +239,11 @@ def plot_traj(df, frame, data_dir, groups=None, image={'image_fn': None, 't_dim'
colors = color_list[0]
if traj_length > 1:
if not plot3D:
if color_code == "z" or color_code == "t":
for j in range(1, traj_length):
ax.plot([X[j - 1], X[j]], [Y[j - 1], Y[j]], lw=lw, ls='-', color=colors[j])
else:
ax.plot(X, Y, lw=lw, ls='-', color=colors)
if color_code == "z" or color_code == "t":
for j in range(1, traj_length):
ax.plot([X[j - 1], X[j]], [Y[j - 1], Y[j]], lw=lw, ls='-', color=colors[j])
else:
pass
# if z_labeling:
# X=traj['x'].values;Y=traj['y'].values;Z=traj['z_rel'].values; #convert to numpy to optimize speed
# for j in range(1,traj_length):
# ax.plot([X[j-1],X[j]],[Y[j-1],Y[j]],[Z[j-1],Z[j]],lw=lw,ls='-',color=tpr.get_cmap_color(Z[j],mpl.cm.plasma,vmin=z_lim[0],vmax=z_lim[1]))
# ax.scatter(X[traj_length-1],Y[traj_length-1],Z[traj_length-1],s=10,color=tpr.get_cmap_color(Z[j],mpl.cm.plasma,vmin=z_lim[0],vmax=z_lim[1]))
# elif multiple_groups:
# ax.plot(traj['x'],traj['y'],lw=lw,ls='-',color=color_list[k%len(color_list)])
# ax.plot(traj['x'].values[-1],traj['y'].values[-1],ms=ms,marker='.',color=color_list[k%len(color_list)])
# else:
# ax.plot(traj['x'],traj['y'],lw=lw,ls='-',color=color_list[track%len(color_list)])
# ax.plot(traj['x'].values[-1],traj['y'].values[-1],ms=ms,marker='.',color=color_list[track%len(color_list)])
# ax.axis([xmin,xmax,ymin,ymax])
# ax.view_init(elev = elevation, azim=angle)
# plt.axis('off')
ax.plot(X, Y, lw=lw, ls='-', color=colors)
if invert_yaxis:
ax.axis([xmin, xmax, ymax, ymin])
......@@ -395,14 +378,14 @@ def plot_vector_field(data_dir, df, data, field, frame, plot_on_field=None, dim=
bkgd = tpr.get_background(image=image, frame=frame, df=df, no_bkg=no_bkg,
image_size=image_size, axis_on=show_axis,
dpi=plot_config['dpi'])
fig = bkgd['fig']
fig = bkgd['fig']
ax = bkgd['ax']
xmin = bkgd['xmin']
ymin = bkgd['ymin']
xmax = bkgd['xmax']
ymax = bkgd['ymax']
no_bkg = bkgd['no_bkg']
# extract data
dimensions = ['x', 'y', 'z'] if dim == 3 else ['x', 'y']
vdata = [field + d for d in dimensions] # eg ['vx','vy'] or ['ax','ay','az']
......@@ -864,9 +847,7 @@ def plot_all_traj(data_dir, df, image={'image_fn': None, 't_dim': None, 'z_dim':
tpr.safe_mkdir(plot_dir)
info = tpr.get_info(data_dir)
groups = df.groupby('frame')
plot_config = make_plot_config() if plot_config is None else plot_config
# color map
......@@ -885,6 +866,19 @@ def plot_all_traj(data_dir, df, image={'image_fn': None, 't_dim': None, 'z_dim':
label = tpr.make_param_label(param, l_unit=info["length_unit"], t_unit=info["time_unit"])
plot_cmap(plot_dir, label, traj_parameters['cmap'], traj_parameters['cmap_lim'][0],
traj_parameters['cmap_lim'][1])
# make a colmuns of indices to be used for color_cylce
elif color_code == "ROI":
subset_order = traj_parameters['subset_order']
if 'subset' in df.columns:
# check subset order
if subset_order is None:
subset_order = df['subset'].unique()
else:
for sub in df['subset'].unique():
if sub not in subset_order: # if missing subset in subset_oder, adding it
subset_order.append(sub)
df['subset_order'] = df['subset'].apply(lambda s: subset_order.index(s)) # column with indices in subset_order
# # ensure 'z' is not color code for 2D data
# if dim == 2 and traj_parameters['color_code'] == 'z':
......@@ -894,6 +888,7 @@ def plot_all_traj(data_dir, df, image={'image_fn': None, 't_dim': None, 'z_dim':
num_cores = multiprocessing.cpu_count()
# Parallel(n_jobs=num_cores)(delayed(plot_cells)(df_list,groups_list,frame,data_dir,plot_traj,z_lim,hide_labels,no_bkg,lengthscale) for frame in df['frame'].unique())
else:
groups = df.groupby('frame')
for frame in df['frame'].unique():
frame = int(frame)
plot_traj(df, frame, data_dir, groups=groups, image=image, plot_dir=plot_dir,
......@@ -1198,11 +1193,11 @@ def plot_total_traj(data_dir, df, dim=3, plot_dir=None, plot_fn=None, plot_confi
elif color_code == "none":
colors = color_list[0]
elif color_code == "ROI":
if 'group' in df.columns:
group = traj.loc[0, 'group']
colors = color_list[group % len(color_list)]
if 'subset' in df.columns:
subset = traj.loc[0, 'subset']
colors = color_list[subset % len(color_list)]
else:
colors = color_list[0] # if color_coded by group but there's none, use only one color
colors = color_list[0] # if color_coded by subset but there's none, use only one color
else:
colors = color_list[0]
......
......@@ -50,7 +50,8 @@ def make_traj_config(data_dir=None, export_config=True):
'show_axis': False, # to show the plot axes (by default just image)
'plot3D': False, # plot in 3D
'elevation': None, # 3D paramater
'angle': None # 3D paramater
'angle': None, # 3D paramater
'subset_order': None, # if color code is ROI, order of ROI in color cycle
}
MSD_config = {'run': True, # run plot_all_MSD
......@@ -146,12 +147,15 @@ def traj_analysis(data_dir, data=None, image=None, refresh=False, parallelize=Fa
total_traj_config = traj_config["total_traj_config"]
### Filter data
filters = tpr.init_filters() if filters is None else filters
filters = tpr.init_filters(data_dir=data_dir, export_to_config=True) if filters is None else filters
subset_analysis = filters['subset'] # how to deal with subsets
filters_ = filters['filters_list']
df = tpr.select_sub_data(df, filters=filters_)
if subset_analysis == 'together':
df_list = [df] # a single df is kept
# force color coding trajectory plotting
traj_config_['color_code'] = 'ROI'
total_traj_config['color_code'] = 'ROI'
elif subset_analysis == 'separately':
df_list = [df[df['subset'] == sub] for sub in df['subset'].unique()] # a list of df filtered by subset
......@@ -174,19 +178,25 @@ def traj_analysis(data_dir, data=None, image=None, refresh=False, parallelize=Fa
csv_fn = osp.join(sub_dir, 'all_data.csv')
df.to_csv(csv_fn)
# compute mean track properties
mean_fn = osp.join(sub_dir, 'track_prop.csv')
df_prop = tca.compute_track_prop(df, dimensions)
df_prop.to_csv(mean_fn)
# save pipeline parameters
# filename = osp.join(sub_dir, 'analysis_parameters.csv')
config_dir = osp.join(sub_dir, 'config')
tpr.safe_mkdir(config_dir)
fn = osp.join(config_dir, 'filters.csv')
tpr.write_dict(filters_[i], fn)
for key in traj_config.keys():
fn = osp.join(config_dir, key + '.csv')
tpr.write_dict(traj_config[key], fn)
# if filters[i]['ROI'] is not None:
# filters[i]['ROI']['coord'] = ROI_list[i]
# params_d = [filters[i], traj_config_, MSD_config, plot_config]
# params_n = ['filters', 'traj parameters', 'MSD parameters', 'plotting parameters']
# tpr.write_dict(params_d, filename, dict_names=params_n)
# compute mean track properties
mean_fn = osp.join(sub_dir, 'track_prop.csv')
df_prop = tca.compute_track_prop(df, dimensions)
df_prop.to_csv(mean_fn)
# analysis and plotting
if subset_analysis == 'together':
hue = 'subset'
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment