Commit a559bf9d authored by amichaut's avatar amichaut
Browse files

moved all config function to prepare + make_all_config

parent 0ffdd025
Pipeline #56645 failed with stage
in 11 seconds
......@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {
"scrolled": false
},
......@@ -47,7 +47,7 @@
"<IPython.core.display.HTML object>"
]
},
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
......@@ -76,8 +76,8 @@
"from track_analyzer import prepare as tpr\n",
"from track_analyzer import plotting as tpl\n",
"from track_analyzer import calculate as tca\n",
"from track_analyzer.scripts.analyze_tracks import traj_analysis, make_traj_config\n",
"from track_analyzer.scripts.analyze_maps import map_analysis, make_map_config\n",
"from track_analyzer.scripts.analyze_tracks import traj_analysis\n",
"from track_analyzer.scripts.analyze_maps import map_analysis\n",
"\n",
"warnings.filterwarnings('ignore')\n",
"\n",
......@@ -117,7 +117,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {
"scrolled": false
},
......@@ -137,7 +137,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5995c8180028482ebe6287e00639e23c",
"model_id": "8afc81c7f6fa4fb48bafa7942a2935c0",
"version_major": 2,
"version_minor": 0
},
......@@ -151,7 +151,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c7b539313b274d3eb3d55613bdcdd18c",
"model_id": "d4ec483c2b674cedbecedcac97f9ed8e",
"version_major": 2,
"version_minor": 0
},
......@@ -165,7 +165,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b40a695017b6410c8d1833dd2fa5f916",
"model_id": "d12f6a13a689442abee9aef834af9585",
"version_major": 2,
"version_minor": 0
},
......@@ -193,7 +193,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {
"scrolled": true
},
......@@ -215,7 +215,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d940457d23774df3a7de1e8910ac15d3",
"model_id": "635d3341cfec42ba81a26a6f60bc0e94",
"version_major": 2,
"version_minor": 0
},
......@@ -251,7 +251,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"metadata": {
"scrolled": false
},
......@@ -260,35 +260,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"You have loaded a 4D image: (708x512) pixels with 195 time steps and 35 z slices\n"
"You have loaded a 3D image: (708x512) pixels with 195 time steps\n"
]
},
{
"data": {
"text/markdown": [
"If there is an error between t and z dimension, you can swap these dimensions"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2e0a21dd11614db1a5bb3162a8959932",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"ToggleButton(value=False, description='Swap z and t')"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/markdown": [
......@@ -316,7 +290,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7a8f398b9d1c4ee8b48f563144092ed6",
"model_id": "eedb8e5266e845ca8bad3e5d188d0d25",
"version_major": 2,
"version_minor": 0
},
......@@ -342,7 +316,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c3116952904e43bf94bc045a35c435d9",
"model_id": "00b19e58ac634a5484a5f67827084e4e",
"version_major": 2,
"version_minor": 0
},
......@@ -389,7 +363,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"metadata": {
"code_folding": [],
"scrolled": true
......@@ -454,7 +428,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 8,
"metadata": {
"code_folding": [],
"scrolled": true
......@@ -489,7 +463,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 9,
"metadata": {
"scrolled": false
},
......@@ -663,7 +637,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 10,
"metadata": {
"scrolled": true
},
......@@ -695,7 +669,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d31bb60d26a747ebb232755b2e6f30d8",
"model_id": "1ebea262f69e4f5280c38fa67abcdd88",
"version_major": 2,
"version_minor": 0
},
......@@ -723,7 +697,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "819ebe9faa984d2c98fdb3a4b86663ba",
"model_id": "f4168c62210d4585bf8da419941ee49b",
"version_major": 2,
"version_minor": 0
},
......@@ -769,7 +743,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"metadata": {
"scrolled": false
},
......@@ -813,7 +787,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "34003840462c4adb8890898a67e1fbcf",
"model_id": "b3cb00b4b1a448cb835fc2c1063d2c63",
"version_major": 2,
"version_minor": 0
},
......@@ -839,7 +813,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1c5f27d39ceb4d83874a9682f69e8ef5",
"model_id": "cc911719d94047c1b25024e1d2df112c",
"version_major": 2,
"version_minor": 0
},
......@@ -866,7 +840,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4e85571fee9c41a09c4a08d75f6cba0d",
"model_id": "26ee6d2efc0d49b6a6b502ab34dd6aee",
"version_major": 2,
"version_minor": 0
},
......@@ -920,7 +894,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 12,
"metadata": {
"scrolled": false
},
......@@ -936,6 +910,15 @@
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Draw points or rectangles, then press ENTER and close the image viewer\n",
"You have selected 1 point(s) and 0 rectangle(s)\n",
"Is the selection correct? [y]/n: y\n"
]
}
],
"source": [
......@@ -973,7 +956,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 13,
"metadata": {
"scrolled": false
},
......@@ -993,7 +976,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "563f8bd48d32472a98cfb84470d78b46",
"model_id": "b90ebb4d3fd54a42992b46e213011fc1",
"version_major": 2,
"version_minor": 0
},
......@@ -1007,7 +990,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c6bf8a8e69624fbe85f0c2cf8270310e",
"model_id": "064a5a8cfb4d4b7daccfe6b34d69da11",
"version_major": 2,
"version_minor": 0
},
......@@ -1021,7 +1004,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "be944afadc1f4f0ea277b48a4655985f",
"model_id": "120ab0f110ee4e878e15410f2a6385f5",
"version_major": 2,
"version_minor": 0
},
......@@ -1035,7 +1018,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "74f2e3eb7b38405f9cd59172d6e8ab2c",
"model_id": "6385355192ce4bbbb5bebac6267ce763",
"version_major": 2,
"version_minor": 0
},
......@@ -1061,7 +1044,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "23e3c7c651854112bf06317757ac026f",
"model_id": "2bf5f8540b224bbb9f37bd4a1c68c44a",
"version_major": 2,
"version_minor": 0
},
......@@ -1087,7 +1070,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "f4ca5babc85c434a8707dd8ba03721da",
"model_id": "9e1e168857d047f7926eb15dddd5afe9",
"version_major": 2,
"version_minor": 0
},
......@@ -1113,7 +1096,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "ec34c7b7d2bc4cc4a9c80b915fe3df35",
"model_id": "73170aeb22c74af09fc3abf75645a89e",
"version_major": 2,
"version_minor": 0
},
......@@ -1157,7 +1140,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 14,
"metadata": {},
"outputs": [
{
......@@ -1186,7 +1169,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 15,
"metadata": {},
"outputs": [
{
......@@ -2673,7 +2656,7 @@
"\n",
"# Prepare config\n",
"\n",
"traj_config = make_traj_config(export_config=False)\n",
"traj_config = tpr.make_traj_config(export_config=False)\n",
"\n",
"#traj_config_\n",
"traj_config[\"traj_config_\"][\"run\"] = traj_module_wid.value\n",
......@@ -3055,7 +3038,7 @@
"\n",
"## Prepare config\n",
"\n",
"map_config = make_map_config(export_config=False)\n",
"map_config = tpr.make_map_config(export_config=False)\n",
"\n",
"# grid_param\n",
"map_config[\"grid_param\"] = {'x_num':None,\n",
......
%% Cell type:code id: tags:
``` python
import os
import os.path as osp
import pickle
import napari
from skimage import io
from skimage.color import rgb2gray
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
import seaborn as sns
import pandas as pd
import warnings
import ipywidgets as widgets
from ipywidgets import HBox, VBox, interact, interact_manual, TwoByTwoLayout, GridspecLayout, Label, AppLayout
from ipyfilechooser import FileChooser
from IPython.display import HTML, Markdown, display, clear_output
from traitlets import traitlets
from track_analyzer import prepare as tpr
from track_analyzer import plotting as tpl
from track_analyzer import calculate as tca
from track_analyzer.scripts.analyze_tracks import traj_analysis, make_traj_config
from track_analyzer.scripts.analyze_maps import map_analysis, make_map_config
from track_analyzer.scripts.analyze_tracks import traj_analysis
from track_analyzer.scripts.analyze_maps import map_analysis
warnings.filterwarnings('ignore')
%matplotlib inline
def printmd(string):
display(Markdown(string))
cwd = os.getcwd() # working directory
plot_param = tpl.make_plot_config() # some config parameters
color_list = plot_param['color_list'] # a list of colors often used
# Hide code
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')
```
%%%% Output: stream
_ _ _
| |_ _ __ __ _ ___| | __ __ _ _ __ __ _| |_ _ _______ _ __
| __| '__/ _` |/ __| |/ / / _` | '_ \ / _` | | | | |_ / _ \ '__|
| |_| | | (_| | (__| < | (_| | | | | (_| | | |_| |/ / __/ |
\__|_| \__,_|\___|_|\_\ \__,_|_| |_|\__,_|_|\__, /___\___|_|
|___/
Track Analyzer - Quantification and visualization of tracking data.
Developed and maintained by Arthur Michaut: arthur.michaut@gmail.com
%%%% Output: execute_result
<IPython.core.display.HTML object>
%% Cell type:markdown id: tags:
# Preparation module
## Loading data
%% Cell type:code id: tags:
``` python
#choose positions file
fc_table = FileChooser(cwd)
fc_table.use_dir_icons = True
fc_table.title = '<b>Tracking data file</b>'
sep_wid = widgets.Dropdown(options=[',',';', 'tab', ' '],value=',',description='column separator:',style={'description_width': 'initial'})
header_wid = widgets.Dropdown(options=['yes','no'],value='yes',description='first row = column names?',style={'description_width': 'initial'})
printmd("""**Browse your file system to the table of tracked data (only .txt and .csv are supported)**""")
display(fc_table,sep_wid,header_wid)
```
%%%% Output: display_data
**Browse your file system to the table of tracked data (only .txt and .csv are supported)**
%%%% Output: display_data
%%%% Output: display_data
%%%% Output: display_data
%% Cell type:code id: tags:
``` python
# get position file path
data_dir = fc_table.selected_path
data_file = fc_table.selected
#data_dir = '/Users/amichaut/Desktop/Fluo-N3DH-CE/'
#data_file = "/Users/amichaut/Desktop/Fluo-N3DH-CE/positions.csv"
if data_file is None:
raise Exception("**ERROR: no data table has been selected**")
# choose image file
printmd("""**(Optional) Browse your file system to the image file**
You can plot your data on your image. The image can be a single image or a stack (a 2D time series or a 3D time series).
Only tif images are supported. """)
fc_im = FileChooser(data_dir)
fc_im.use_dir_icons = True
fc_im.title = '<b>Image file</b>'
display(fc_im)
```
%%%% Output: display_data
**(Optional) Browse your file system to the image file**
You can plot your data on your image. The image can be a single image or a stack (a 2D time series or a 3D time series).
Only tif images are supported.
%%%% Output: display_data
%% Cell type:code id: tags:
``` python
# get image file path
im_file = fc_im.selected
#im_file = "/Users/amichaut/Desktop/Fluo-N3DH-CE/stack.tif"
# analyze image
y_size,x_size = [512,512] #default size of an image to inialize the make info widget
image = tpr.get_image(data_dir,filename=im_file,verbose=True)
if image['image_size'] is not None:
y_size,x_size = image['image_size']
# swap z and t dimensions if needed
check_swap_wid = False # bool to retrieve swap_wid value if necessary
if image['t_dim'] is not None and image['z_dim'] is not None:
check_swap_wid=True
printmd("If there is an error between t and z dimension, you can swap these dimensions")
swap_wid = widgets.ToggleButton(value=False,description='Swap z and t')
display(swap_wid)
# refresh database and info if needed
database_fn=osp.join(data_dir,'data_base.p')
info_fn=osp.join(data_dir,'info.txt')
printmd("---")
if osp.exists(database_fn):
printmd('The database already exists, do you want to refresh it?')
refresh_db_wid=widgets.ToggleButton(value=False,description='Refresh database')
display(refresh_db_wid)
if osp.exists(info_fn):
printmd("The info.txt file already exists, do you want to refresh it?")
refresh_info_wid=widgets.ToggleButton(value=False,description='Refresh info')
display(refresh_info_wid)
```
%%%% Output: stream
You have loaded a 4D image: (708x512) pixels with 195 time steps and 35 z slices
%%%% Output: display_data
If there is an error between t and z dimension, you can swap these dimensions
%%%% Output: display_data
You have loaded a 3D image: (708x512) pixels with 195 time steps
%%%% Output: display_data
---
%%%% Output: display_data
The database already exists, do you want to refresh it?
%%%% Output: display_data
%%%% Output: display_data
The info.txt file already exists, do you want to refresh it?
%%%% Output: display_data
%% Cell type:code id: tags:
``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible
#swap z and t
if check_swap_wid:
if swap_wid.value:
t_dim = image['t_dim']
z_dim = image['z_dim']
image['t_dim'] = z_dim
image['z_dim'] = t_dim
printmd("**z and t swapped!**")
im = io.imread(image['image_fn'])
printmd("4D image with {} time steps and {} z slices".format(im.shape[image['t_dim']],im.shape[image['z_dim']]))
del im # free memory
# retrieve refresh widgets values
refresh_db=refresh_db_wid.value if osp.exists(database_fn) else True
refresh_info=refresh_info_wid.value if osp.exists(info_fn) else True
# get info
if refresh_info:
length_unit_wid=widgets.Dropdown(options=['um', 'mm', 'au'],value='um',description='Length unit:',style={'description_width': 'initial'})
time_unit_wid=widgets.Dropdown(options=['min', 's', 'hr', 'au'],value='min',description='Time unit:',style={'description_width': 'initial'})
length_sc_wid=widgets.BoundedFloatText(value=1.0,min=0,max=1e4,description='Pixel size:',style={'description_width': 'initial'})
z_sc_wid=widgets.BoundedFloatText(value=0,min=0,max=1e4,description='z step:',style={'description_width': 'initial'})
time_sc_wid=widgets.BoundedFloatText(value=1.0,min=0,max=1e4,description='Frame interval:',style={'description_width': 'initial'})
width_wid=widgets.BoundedIntText(value=x_size,min=0,max=1e4,description='Image width (px):',style={'description_width': 'initial'})
height_wid=widgets.BoundedIntText(value=y_size,min=0,max=1e4,description='Image height (px):',style={'description_width': 'initial'})
left_box = VBox([length_unit_wid, time_unit_wid,width_wid])
right_box = VBox([length_sc_wid, time_sc_wid,height_wid])
box = HBox([left_box, right_box])
printmd("**Information about the data**")
display(box)
printmd("In the data table, are the positions given in pixels or in the length unit (given above)?")
table_unit_wid=widgets.Dropdown(options=['px', 'unit'],value='px',description='Data unit:',style={'description_width': 'initial'})
display(table_unit_wid)
printmd("If the lengthscale in z is different from the xy lengthscale, enter the z step (in length unit). If not, leave it to zero.")
display(z_sc_wid)
wid_list = [length_unit_wid,time_unit_wid,length_sc_wid,time_sc_wid,width_wid,height_wid,table_unit_wid,z_sc_wid]
param_names = ['length_unit','time_unit','lengthscale','timescale','image_width','image_height','table_unit','z_step']
```
%%%% Output: display_data
**Don't forget to run this cell!**
%% Cell type:code id: tags:
``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible
# save info as txt file
if refresh_info:
info = {}
with open(info_fn,'w+') as f:
for couple in zip(param_names,wid_list):
info[couple[0]]=couple[1].value
f.write('{}:{}\n'.format(couple[0],couple[1].value))
else:
info=tpr.get_info(data_dir)
```
%%%% Output: display_data
**Don't forget to run this cell!**
%% Cell type:code id: tags:
``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible
# Set columns identity
if refresh_db:
sep = sep_wid.value if sep_wid.value !='tab' else '\t'
header = None if header_wid.value=='no' else 0
df = pd.read_csv(data_file,sep=sep,header=header)
printmd("**Here are the first rows of the input data table**")
display(df.head(10))
```
%%%% Output: display_data
**Don't forget to run this cell!**
%%%% Output: display_data
**Here are the first rows of the input data table**
%%%% Output: display_data
%% Cell type:code id: tags:
``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible
if refresh_db:
wid_list = []
left_list=[]
right_list=[]
param_list = ['x','y','z','frame','track','none']
# display the df columns as two columns of widgets
for i,col in enumerate(df.columns):
wid=widgets.Dropdown(options=param_list,value='none',description='column {}:'.format(col),style={'description_width': 'initial'})
wid_list.append(wid)
if i<len(df.columns)/2:
left_list.append(wid)
else:
right_list.append(wid)
printmd("**Select the columns to be used in the analysis: track,frame,x,y,(z). Leave to 'none' the other ones.**")
left_box = VBox(left_list)
right_box = VBox(right_list)
display(HBox([left_box, right_box]))
# deal with gaps in trajectories
printmd("""**Some tracking softwares support to miss objects at some frames. This results in tracks with gaps.
However, this analysis pipeline requires to have continuous tracks. How do you want to handle tracks with gaps:
fill the gaps by linear interpolation or split the track in different tracks?**""")
split_wid=widgets.Dropdown(options=['interpolate','split'],value='interpolate',description='gap resolution:'.format(col),style={'description_width': 'initial'})
display(split_wid)
```
%%%% Output: display_data
**Don't forget to run this cell!**
%%%% Output: display_data
**Select the columns to be used in the analysis: track,frame,x,y,(z). Leave to 'none' the other ones.**
%%%% Output: display_data
%%%% Output: display_data
**Some tracking softwares support to miss objects at some frames. This results in tracks with gaps.
However, this analysis pipeline requires to have continuous tracks. How do you want to handle tracks with gaps:
fill the gaps by linear interpolation or split the track in different tracks?**
%%%% Output: display_data
%% Cell type:code id: tags:
``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible
if refresh_db:
col_values = [wid.value for wid in wid_list]
for param_ in ['x','y','frame','track']: # mandatory columns
if param_ not in col_values:
print("Warning: you MUST select a column for "+param_)
df.columns=col_values # rename columns
# ditch none columns
col_values=np.array(col_values)
new_cols=col_values[col_values!='none']
df=df[new_cols]
# retrieve split traj widget value
split_traj=True if split_wid.value=='split' else False
#get dimension
dim_list = ['x','y','z'] if 'z' in df.columns else ['x','y']
# coordinates origin
printmd("""**Do you want to set a custom origin to the coordinates?**""")
printmd("""Select a new origin by drawing on the image (you can choose which dimension to reset)""")
ori_onimage_wid=widgets.ToggleButton(value=False,description='Select on image')
reset_dim_wid=widgets.SelectMultiple(options=dim_list,value=['x','y'],description='Dimensions to reset',style={'description_width': 'initial'})
display(HBox([ori_onimage_wid,reset_dim_wid]))
printmd("""Or directly type in the new origin (in px)""")
origin_coord_wid_list=[]
for dim in dim_list:
origin_coord_wid_list.append(widgets.FloatSlider(value=0,min=0,max=df[dim].max(),step=0.1,description=dim,style={'description_width': 'initial'}))
display(HBox(origin_coord_wid_list))
# axes signs
printmd("""**Do you want to invert the axes?**
Default orientation: x: left->right, y: top->bottom, z: slice number""")
invert_axes_wid=widgets.SelectMultiple(options=dim_list,value=[],description='Axes to invert',style={'description_width': 'initial'})
display(invert_axes_wid)
```
%%%% Output: display_data
**Don't forget to run this cell!**
%%%% Output: display_data
**Do you want to set a custom origin to the coordinates?**
%%%% Output: display_data
Select a new origin by drawing on the image (you can choose which dimension to reset)
%%%% Output: display_data
%%%% Output: display_data
Or directly type in the new origin (in px)
%%%% Output: display_data
%%%% Output: display_data
**Do you want to invert the axes?**
Default orientation: x: left->right, y: top->bottom, z: slice number
%%%% Output: display_data
%% Cell type:code id: tags:
``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible
if refresh_db:
if not ori_onimage_wid.value:
origin_coord={}
all_zeros=True
for d,wid in enumerate(origin_coord_wid_list):
origin_coord[dim_list[d]]=wid.value
if wid.value>0:
all_zeros=False
if all_zeros: # if no change of origin
origin_coord = False
set_origin_ = origin_coord
else:
set_origin_ = True
#remove None tracks
df = df[df['track']!='None'] # remove
data = tpr.get_data(data_dir,df=df,refresh=refresh_db,split_traj=split_traj,
set_origin_=set_origin_,image=image,reset_dim=reset_dim_wid.value,invert_axes=invert_axes_wid.value)
else:
# reload from database
data = tpr.get_data(data_dir,df=None,refresh=refresh_db)
# useful variables
df = data['df']
lengthscale = data['lengthscale']
timescale = data['timescale']
dim = data['dim']
dimensions = data['dimensions']
```
%%%% Output: display_data
**Don't forget to run this cell!**
%%%% Output: stream
Draw points or rectangles, then press ENTER and close the image viewer
You have selected 1 point(s) and 0 rectangle(s)
Is the selection correct? [y]/n: y
%% Cell type:code id: tags:
``` python
# general plotting configuration
printmd("## General plotting configuration")
fig_w_wid = widgets.BoundedIntText(value=plot_param['figsize'][0],min=0,max=20,
description='Figure width (inches):',style={'description_width': 'initial'})
fig_h_wid = widgets.BoundedIntText(value=plot_param['figsize'][1],min=0,max=20,
description='Figure height (inches):',style={'description_width': 'initial'})
fig_dpi_wid = widgets.BoundedIntText(value=plot_param['dpi'],min=50,max=1e4,
description='Figure resolution (dpi):',style={'description_width': 'initial'})
fig_format_wid = widgets.Dropdown(options=['.png','.svg'],value='.png',
description='Figure format',style={'description_width': 'initial'})
despine_wid = widgets.ToggleButton(value=plot_param['despine'],
description='despine figure')
replace_color_wid = widgets.BoundedIntText(value=0,min=0,max=20,
description='Number of colors:',style={'description_width': 'initial'})
add_replace_wid = widgets.Dropdown(options=['add','replace'],value='add',
description='add or replace?',style={'description_width': 'initial'})
invert_yaxis_wid = widgets.ToggleButton(value=True,description='y axis origin: top')
export_data_pts_wid = widgets.ToggleButton(value=True,description='export data points')
display(HBox([fig_w_wid,fig_h_wid]),fig_dpi_wid,fig_format_wid,despine_wid)
printmd('Do you want to add/replace the first default colors used for plotting? Give the number of colors you want to select:')
display(HBox([replace_color_wid,add_replace_wid]))
printmd('How do you want to display the y-axis (standard orientation: origin at top)')
display(invert_yaxis_wid)
printmd('Do you want to export the data points of your plots as .csv files?')
display(export_data_pts_wid)
```
%%%% Output: display_data
## General plotting configuration
%%%% Output: display_data
%%%% Output: display_data
%%%% Output: display_data
%%%% Output: display_data
%%%% Output: display_data
Do you want to add/replace the first default colors used for plotting? Give the number of colors you want to select:
%%%% Output: display_data
%%%% Output: display_data
How do you want to display the y-axis (standard orientation: origin at top)
%%%% Output: display_data
%%%% Output: display_data
Do you want to export the data points of your plots as .csv files?
%%%% Output: display_data
%% Cell type:code id: tags:
``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible
# replace colors
color_wid_list=[]
for i in range(replace_color_wid.value):
color_wid=widgets.ColorPicker(description='Pick color #{}'.format(i),value=color_list[i])
color_wid_list.append(color_wid)
display(*color_wid_list)
```
%%%% Output: display_data
**Don't forget to run this cell!**
%% Cell type:code id: tags:
``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible
# retreive plotting configuration
color_list_=list(color_list)
new_color_list = [color_wid_list[i].value for i in range(replace_color_wid.value)]
if add_replace_wid.value=='replace':
color_list_[:replace_color_wid.value]=new_color_list
else:
color_list_=new_color_list+color_list_
plot_param={'figsize':(fig_w_wid.value,fig_h_wid.value),
'dpi':fig_dpi_wid.value,
'color_list':color_list_,
'format':fig_format_wid.value,
'despine':despine_wid.value,
'invert_yaxis':invert_yaxis_wid.value,
'export_data_pts':export_data_pts_wid.value
}
```
%%%% Output: display_data
**Don't forget to run this cell!**
%% Cell type:markdown id: tags:
# Filter data
%% Cell type:code id: tags:
``` python
z_step = info['z_step']
if z_step == 0:
z_step = lengthscale # if z_step not given, same as lengthscale
printmd('**View trajectories on a Napari viewer before plotting**')
printmd('If not working, please check Napari installation: https://napari.org/')
viewer_wid = widgets.Button(value=True, description='Show viewer!')
viewer_wid.on_click(lambda obj: tpl.view_traj(df, image=image, z_step=info['z_step']))
display(viewer_wid)
```
%%%% Output: display_data
**View trajectories on a Napari viewer before plotting**
%%%% Output: display_data
If not working, please check Napari installation: https://napari.org/
%%%% Output: display_data
%% Cell type:code id: tags:
``` python
printmd("Your data can be filtered into subsets. How many subsets do you want to analyze?")
subset_num_wid = widgets.BoundedIntText(value=1,min=1,max=10,description='Number of subsets:',style={'description_width': 'initial'})
display(subset_num_wid)
printmd("If you define several subsets, do you want to analyze them separately or together?")
printmd("If analyzed together, subsets will be plotted together. If analyzed separately, each subset will be plotted on individual plots.")
separate_widget = widgets.ToggleButtons(options=['separately', 'together'], description='Analysis',style={'description_width': 'initial'})
display(separate_widget)
```
%%%% Output: display_data
Your data can be filtered into subsets. How many subsets do you want to analyze?
%%%% Output: display_data
%%%% Output: display_data
If you define several subsets, do you want to analyze them separately or together?
%%%% Output: display_data
If analyzed together, subsets will be plotted together. If analyzed separately, each subset will be plotted on individual plots.
%%%% Output: display_data
%% Cell type:code id: tags:
``` python
# prepare default values for initializing widgets
xlim = [df['x'].min(), df['x'].max()] # maybe use image dimensions instead
ylim = [df['y'].min(), df['y'].max()]
zlim = [df['z'].min(), df['z'].max()] if 'z' in df.columns else []
frame_list = df['frame'].unique()
frame_min = df['frame'].min()
frame_max = df['frame'].max()
# cropping widget lists
xlim_wid_list = []
ylim_wid_list = []
zlim_wid_list = []
drawer_wid_list = []
frame_subset_wid_list = []
min_length_wid_list = []
max_length_wid_list = []
name_wid_list = []
# retrieve drawer coordinates
class LoadedButton(widgets.Button):
"""A button that can holds a value as a attribute."""
def __init__(self, value=None, *args, **kwargs):
super(LoadedButton, self).__init__(*args, **kwargs)
# Create the value attribute.
self.add_traits(value=traitlets.Any(value))
def get_image_coord(ex):
"""Call get_coordinates, display instruction and store coordinates in widget value """
ex.value = tpr.get_coordinates(image,verbose=False,interactive=False)
# create a set of filtering widget for each subset
for i in range(subset_num_wid.value):
printmd("""### Subset #{}""".format(i + 1))
# subset name
printmd("""You can give it a custom name that will be used for saving data""")
name_wid = widgets.Text(value='', placeholder='optional', description='Subset name:',
style={'description_width': 'initial'})
name_wid_list.append(name_wid)
display(name_wid)
# spatial filtering
xlim_wid = widgets.FloatRangeSlider(value=xlim, min=xlim[0], max=xlim[1], step=1,
description='x range (px):', style={'description_width': 'initial'})
ylim_wid = widgets.FloatRangeSlider(value=ylim, min=ylim[0], max=ylim[1], step=1,
description='y range (px):', style={'description_width': 'initial'})
if len(zlim) > 0:
zlim_wid = widgets.FloatRangeSlider(value=zlim, min=zlim[0], max=zlim[1], step=1,
description='z range (px):', style={'description_width': 'initial'})
drawer_wid = LoadedButton(description="Draw ROI", value={})
drawer_wid.on_click(get_image_coord)
# store widgets
xlim_wid_list.append(xlim_wid)
ylim_wid_list.append(ylim_wid)
drawer_wid_list.append(drawer_wid)
if len(zlim) > 0:
zlim_wid_list.append(zlim_wid)
else:
zlim_wid_list.append(None)
#display
printmd("**Crop a region**")
printmd("You can draw it on the image or select it with the slider (values in pixels)."
"If you do both, only the drawn region will be kept. If you select several regions on the image"
", they will be divided into several subsets")
printmd("Draw using the rectangle shape, hit ENTER while you're done. Then close the image viewer. "
"If you want more details about the email viewer please visit https://napari.org/")
if len(zlim) > 0:
display(VBox([drawer_wid, HBox([xlim_wid, ylim_wid, zlim_wid])]))
else:
display(VBox([drawer_wid, HBox([xlim_wid, ylim_wid])]))
# time filtering
frame_subset_wid = widgets.IntRangeSlider(value=[frame_min, frame_max], min=frame_min, max=frame_max, step=1,
description='Frame subset:', style={'description_width': 'initial'})
min_length_wid = widgets.IntSlider(value=frame_min, min=frame_min, max=frame_max + 1, step=1,
description='Minimum traj length:', style={'description_width': 'initial'})
max_length_wid = widgets.IntSlider(value=frame_max + 1, min=frame_min, max=frame_max + 1, step=1,
description='Maximum traj length:', style={'description_width': 'initial'})
# store widgets
frame_subset_wid_list.append(frame_subset_wid)
min_length_wid_list.append(min_length_wid)
max_length_wid_list.append(max_length_wid)
# display
printmd("**Select data based on trajectories duration or frame subset**")
display(HBox([frame_subset_wid, min_length_wid, max_length_wid]))
```
%%%% Output: display_data
### Subset #1
%%%% Output: display_data
You can give it a custom name that will be used for saving data
%%%% Output: display_data
%%%% Output: display_data
**Crop a region**
%%%% Output: display_data
You can draw it on the image or select it with the slider (values in pixels).If you do both, only the drawn region will be kept. If you select several regions on the image, they will be divided into several subsets
%%%% Output: display_data
Draw using the rectangle shape, hit ENTER while you're done. Then close the image viewer. If you want more details about the email viewer please visit https://napari.org/
%%%% Output: display_data
%%%% Output: display_data
**Select data based on trajectories duration or frame subset**
%%%% Output: display_data
%% Cell type:code id: tags:
``` python
# retrieve filters values
def retrieve_region_wid_values(drawer_wid,xlim_wid,ylim_wid,zlim_wid):
"""
Get values of region selection giving priority to drawer widgets over sliders
"""
# xylim
use_sliders = False
drawer_value = drawer_wid.value
if drawer_value: #if not empty
if len(drawer_value['rectangle']) > 0:
#get list of xlim, ylim in case of several ROIs selected
xlim_list = [r['xlim'] for r in drawer_value['rectangle']]
ylim_list = [r['ylim'] for r in drawer_value['rectangle']]
else:
use_sliders = True
else:
use_sliders = True
if use_sliders:
xlim_list = [xlim_wid.value]
ylim_list = [ylim_wid.value]
# zlim
if zlim_wid is None:
zlim = None
else:
zlim = zlim_wid.value
return xlim_list, ylim_list, zlim
filters=[]
for i in range(subset_num_wid.value):
xlim_list, ylim_list, zlim_ = retrieve_region_wid_values(drawer_wid_list[i],
xlim_wid_list[i],
ylim_wid_list[i],
zlim_wid_list[i])
# if several subsets get values of xlim, ylim, while keeping other filters constant
for j in range(len(xlim_list)):
#name
if len(xlim_list) > 1: # if several subsets, modify name: name_number if name is empty: number
name = name_wid_list[i].value + '_{}'.format(j) if name_wid_list[i].value != '' else '{}'.format(j)
else:
name = name_wid_list[i].value
# initialize filter dict
default_filter = tpr.init_filters(data_dir=None, export_to_config=False)
filt_ = default_filter['filters_list'][0]
# fill info
filt_['xlim'] = xlim_list[j]
filt_['ylim'] = ylim_list[j]
filt_['zlim'] = zlim_
filt_['frame_subset'] = frame_subset_wid_list[i].value
filt_['min_traj_len'] = min_length_wid_list[i].value
filt_['max_traj_len'] = max_length_wid_list[i].value
filt_['name'] = name
# add to filter list
filters.append(filt_)
printmd("**Select specific trajectories**")
printmd("You can select specific sets of trajectories. "
"This tool can be useful to perform fate mapping or retrospective mapping. "
"If you selected several subsets before, this specific set will be applied to all of the subsets."
)
printmd("How many sets of trajectories do you want to select?")
set_num_wid = widgets.BoundedIntText(value=0,min=0,max=10,description='Number of sets:',style={'description_width': 'initial'})
display(set_num_wid)
#or by selecting trajectories in a specific region at a specific frame. "
#just by giving a list of ids,
```
%%%% Output: display_data
**Select specific trajectories**
%%%% Output: display_data
You can select specific sets of trajectories. This tool can be useful to perform fate mapping or retrospective mapping. If you selected several subsets before, this specific set will be applied to all of the subsets.
%%%% Output: display_data
How many sets of trajectories do you want to select?
%%%% Output: display_data
%% Cell type:code id: tags:
``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible
# select specific trajectories
# track selection widget lists
track_name_wid_list = []
track_xlim_wid_list = []
track_ylim_wid_list = []
track_zlim_wid_list = []
track_drawer_wid_list = []
track_frame_subset_wid_list = []
track_list_wid_list = []
for i in range(set_num_wid.value):
printmd("""### Set #{}""".format(i + 1))
# subset name
printmd("""You can give it a custom name that will be used for saving data""")
track_name_wid = widgets.Text(value='', placeholder='optional', description='Set name:',
style={'description_width': 'initial'})
track_name_wid_list.append(track_name_wid)
display(track_name_wid)
track_list_wid = widgets.Text(value='', placeholder='comma separated ids', description='Id list:')
track_xlim_wid = widgets.FloatRangeSlider(value=xlim, min=xlim[0], max=xlim[1], step=1,
description='x range (px):', style={'description_width': 'initial'})
track_ylim_wid = widgets.FloatRangeSlider(value=ylim, min=ylim[0], max=ylim[1], step=1,
description='y range (px):', style={'description_width': 'initial'})
if len(zlim) > 0:
track_zlim_wid = widgets.FloatRangeSlider(value=zlim, min=zlim[0], max=zlim[1], step=1,
description='z range (px):', style={'description_width': 'initial'})
track_frame_subset_wid = widgets.IntRangeSlider(value=[frame_min, frame_max], min=frame_min, max=frame_max, step=1,
description='Frame intervalle:', style={'description_width': 'initial'})
track_drawer_wid = LoadedButton(description="Draw ROI", value={})
track_drawer_wid.on_click(get_image_coord)
# store widgets
track_list_wid_list.append(track_list_wid)
track_xlim_wid_list.append(track_xlim_wid)
track_ylim_wid_list.append(track_ylim_wid)
track_drawer_wid_list.append(track_drawer_wid)
track_frame_subset_wid_list.append(track_frame_subset_wid)
if len(zlim) > 0:
track_zlim_wid_list.append(track_zlim_wid)
else:
track_zlim_wid_list.append(None)
#display
printmd("Give a list of ids or select trajectories in a specific region during a specific frame intervalle using the viewer.")
printmd("Use the ids given by the track analyzer, you need to generate the database once to access them, "
"(visualizing the trajectories with the printed labels can be helpful)")
if len(zlim) > 0:
display(VBox([track_list_wid,track_drawer_wid, HBox([track_xlim_wid, track_ylim_wid, track_zlim_wid]),track_frame_subset_wid]))
else:
display(VBox([track_list_wid,track_drawer_wid, HBox([track_xlim_wid, track_ylim_wid]),track_frame_subset_wid]))
```
%%%% Output: display_data
**Don't forget to run this cell!**
%% Cell type:code id: tags:
``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible
if set_num_wid.value == 0:
new_filters = list(filters)
else:
new_filters = []
# for each set of filters, copy them to new sets of filters modified by the trajectory filters
for filt in filters:
for i in range(set_num_wid.value):
# trajectory list selection
track_list_val = track_list_wid_list[i].value
if track_list_val == '':
track_list_val = None
else:
try:
track_list_val = [int(e) for e in track_list_val.split(',')]
except:
print("ERROR: a value in the list is not a number")
track_list_val = None
# ROI selection
track_xlim_list, track_ylim_list, track_zlim_ = retrieve_region_wid_values(track_drawer_wid_list[i],
track_xlim_wid_list[i],
track_ylim_wid_list[i],
track_zlim_wid_list[i])
# if several sets, get values of xlim, ylim, while keeping other filters constant
for j in range(len(track_xlim_list)):
ROI = {'xlim': track_xlim_list[j],
'ylim': track_ylim_list[j],
'zlim': track_zlim_,
'frame_lim': track_frame_subset_wid_list[i].value}
#name
if len(track_xlim_list) > 1: # if several subsets, modify name
name = track_name_wid_list[i].value + '_{}'.format(j)
else:
name = track_name_wid_list[i].value
# add new filters to existing filters
new_filt = dict(filt) # make copy of old filters
new_filt['track_list'] = track_list_val
new_filt['track_ROI'] = ROI
if new_filt['name'] == '':
new_filt['name'] = name # name = setname
else:
new_filt['name'] += '_'+name # name = subsetname_setname
# store in a new list of filters
new_filters.append(new_filt)
printmd('You set {} subsets. Edit their names and order to be plotted if needed.'.format(len(new_filters)))