Commit 2ca465b1 authored by amichaut's avatar amichaut
Browse files

rewrote synthetic data script and made command line

parent a5142a94
...@@ -141,7 +141,7 @@ ...@@ -141,7 +141,7 @@
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "63dc4f03181948059a5ce107d59434f6", "model_id": "4343c7f523b84b00b846f4d82ec1a723",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
...@@ -167,7 +167,7 @@ ...@@ -167,7 +167,7 @@
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "69095416971b40e28f4e30f63744e75a", "model_id": "12fd2b6b427e44218f254d13ca5c2d29",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
...@@ -181,7 +181,7 @@ ...@@ -181,7 +181,7 @@
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "2ab4c07e6a7440488912fa5aa8f11b73", "model_id": "f444719f5575454694bfb83db0930806",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
...@@ -195,7 +195,7 @@ ...@@ -195,7 +195,7 @@
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "6f1be96907c44e488f1af8674daa9387", "model_id": "927db3b22ecc48b4871b720c282bebea",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
...@@ -228,7 +228,7 @@ ...@@ -228,7 +228,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 4,
"metadata": { "metadata": {
"scrolled": true "scrolled": true
}, },
...@@ -250,12 +250,12 @@ ...@@ -250,12 +250,12 @@
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "e858eca5942f45eeb0a920dab9518734", "model_id": "279460ff13d3428f9dd484555dafe987",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
"text/plain": [ "text/plain": [
"FileChooser(path='/Users/amichaut/Desktop/20220224_Utag/TA', filename='', title='HTML(value='<b>Image file</b>…" "FileChooser(path='/Users/amichaut/Desktop/data_TA_run', filename='', title='HTML(value='<b>Image file</b>')', …"
] ]
}, },
"metadata": {}, "metadata": {},
...@@ -284,7 +284,7 @@ ...@@ -284,7 +284,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 5,
"metadata": { "metadata": {
"scrolled": false "scrolled": false
}, },
...@@ -293,7 +293,7 @@ ...@@ -293,7 +293,7 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"You have loaded a 3D image: (2859x1028) pixels with 200 time steps\n" "You have loaded a 3D image: (500x500) pixels with 25 time steps\n"
] ]
}, },
{ {
...@@ -307,6 +307,58 @@ ...@@ -307,6 +307,58 @@
}, },
"metadata": {}, "metadata": {},
"output_type": "display_data" "output_type": "display_data"
},
{
"data": {
"text/markdown": [
"The database already exists, do you want to refresh it?"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "94c3c3a7cd274651b1cf2494456b91b6",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"ToggleButton(value=False, description='Refresh database')"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/markdown": [
"The info.txt file already exists, do you want to refresh it?"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c563c43272a04573843c96ebabf9fbc4",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"ToggleButton(value=False, description='Refresh info')"
]
},
"metadata": {},
"output_type": "display_data"
} }
], ],
"source": [ "source": [
......
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import os import os
import os.path as osp import os.path as osp
import pickle import pickle
import napari import napari
from skimage import io from skimage import io
from skimage.color import rgb2gray from skimage.color import rgb2gray
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from matplotlib import cm from matplotlib import cm
import numpy as np import numpy as np
import seaborn as sns import seaborn as sns
import pandas as pd import pandas as pd
import warnings import warnings
import ipywidgets as widgets import ipywidgets as widgets
from ipywidgets import HBox, VBox, interact, interact_manual, TwoByTwoLayout, GridspecLayout, Label, AppLayout from ipywidgets import HBox, VBox, interact, interact_manual, TwoByTwoLayout, GridspecLayout, Label, AppLayout
from ipyfilechooser import FileChooser from ipyfilechooser import FileChooser
from IPython.display import HTML, Markdown, display, clear_output from IPython.display import HTML, Markdown, display, clear_output
from traitlets import traitlets from traitlets import traitlets
from track_analyzer import prepare as tpr from track_analyzer import prepare as tpr
from track_analyzer import plotting as tpl from track_analyzer import plotting as tpl
from track_analyzer import calculate as tca from track_analyzer import calculate as tca
from track_analyzer.scripts.analyze_tracks import traj_analysis from track_analyzer.scripts.analyze_tracks import traj_analysis
from track_analyzer.scripts.analyze_maps import map_analysis from track_analyzer.scripts.analyze_maps import map_analysis
from track_analyzer.scripts.compare_datasets import compare_datasets from track_analyzer.scripts.compare_datasets import compare_datasets
from napari.settings import get_settings from napari.settings import get_settings
get_settings().application.ipy_interactive = False # disable interactive usage of Napari viewer (necessary for tpr.get_coordinates) get_settings().application.ipy_interactive = False # disable interactive usage of Napari viewer (necessary for tpr.get_coordinates)
warnings.filterwarnings('ignore') warnings.filterwarnings('ignore')
%matplotlib inline %matplotlib inline
def printmd(string): def printmd(string):
display(Markdown(string)) display(Markdown(string))
cwd = os.getcwd() # working directory cwd = os.getcwd() # working directory
plot_param = tpl.make_plot_config() # some config parameters plot_param = tpl.make_plot_config() # some config parameters
color_list = plot_param['color_list'] # a list of colors often used color_list = plot_param['color_list'] # a list of colors often used
# Hide code # Hide code
HTML('''<script> HTML('''<script>
code_show=true; code_show=true;
function code_toggle() { function code_toggle() {
if (code_show){ if (code_show){
$('div.input').hide(); $('div.input').hide();
} else { } else {
$('div.input').show(); $('div.input').show();
} }
code_show = !code_show code_show = !code_show
} }
$( document ).ready(code_toggle); $( document ).ready(code_toggle);
</script> </script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''') <form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')
``` ```
%%%% Output: stream %%%% Output: stream
_ _ _ _ _ _
| |_ _ __ __ _ ___| | __ __ _ _ __ __ _| |_ _ _______ _ __ | |_ _ __ __ _ ___| | __ __ _ _ __ __ _| |_ _ _______ _ __
| __| '__/ _` |/ __| |/ / / _` | '_ \ / _` | | | | |_ / _ \ '__| | __| '__/ _` |/ __| |/ / / _` | '_ \ / _` | | | | |_ / _ \ '__|
| |_| | | (_| | (__| < | (_| | | | | (_| | | |_| |/ / __/ | | |_| | | (_| | (__| < | (_| | | | | (_| | | |_| |/ / __/ |
\__|_| \__,_|\___|_|\_\ \__,_|_| |_|\__,_|_|\__, /___\___|_| \__|_| \__,_|\___|_|\_\ \__,_|_| |_|\__,_|_|\__, /___\___|_|
|___/ |___/
Track Analyzer - Quantification and visualization of tracking data. Track Analyzer - Quantification and visualization of tracking data.
Developed and maintained by Arthur Michaut: arthur.michaut@gmail.com Developed and maintained by Arthur Michaut: arthur.michaut@gmail.com
%%%% Output: execute_result %%%% Output: execute_result
<IPython.core.display.HTML object> <IPython.core.display.HTML object>
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Preparation module # Preparation module
## Loading data ## Loading data
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
#choose positions file #choose positions file
fc_table = FileChooser(cwd) fc_table = FileChooser(cwd)
fc_table.use_dir_icons = True fc_table.use_dir_icons = True
fc_table.title = '<b>Tracking data file</b>' fc_table.title = '<b>Tracking data file</b>'
sep_wid = widgets.Dropdown(options=[',',';', 'tab', ' '],value=',',description='column separator:',style={'description_width': 'initial'}) sep_wid = widgets.Dropdown(options=[',',';', 'tab', ' '],value=',',description='column separator:',style={'description_width': 'initial'})
header_wid = widgets.Checkbox(value=True, description='First row = column names') header_wid = widgets.Checkbox(value=True, description='First row = column names')
trackmate_wid = widgets.Checkbox(value=False, description='Trackmate (version 7) csv file') trackmate_wid = widgets.Checkbox(value=False, description='Trackmate (version 7) csv file')
printmd("""**Browse your file system to the table of tracked data (only .txt and .csv are supported)**""") printmd("""**Browse your file system to the table of tracked data (only .txt and .csv are supported)**""")
display(fc_table) display(fc_table)
printmd("""**Give information about the input file format**""") printmd("""**Give information about the input file format**""")
display(sep_wid, header_wid, trackmate_wid) display(sep_wid, header_wid, trackmate_wid)
``` ```
%%%% Output: display_data %%%% Output: display_data
**Browse your file system to the table of tracked data (only .txt and .csv are supported)** **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 %%%% Output: display_data
**Give information about the input file format** **Give information about the input file format**
%%%% Output: display_data %%%% Output: display_data
%%%% Output: display_data %%%% Output: display_data
%%%% Output: display_data %%%% Output: display_data
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# get position file path # get position file path
data_dir = fc_table.selected_path data_dir = fc_table.selected_path
data_file = fc_table.selected data_file = fc_table.selected
if data_file is None: if data_file is None:
raise Exception("**ERROR: no data table has been selected**") raise Exception("**ERROR: no data table has been selected**")
# choose image file # choose image file
printmd("""**(Optional) Browse your file system to the 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). 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. """) Only tif images are supported. """)
fc_im = FileChooser(data_dir) fc_im = FileChooser(data_dir)
fc_im.use_dir_icons = True fc_im.use_dir_icons = True
fc_im.title = '<b>Image file</b>' fc_im.title = '<b>Image file</b>'
display(fc_im) display(fc_im)
``` ```
%%%% Output: display_data %%%% Output: display_data
**(Optional) Browse your file system to the image file** **(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). 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. Only tif images are supported.
%%%% Output: display_data %%%% Output: display_data
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# get image file path # get image file path
im_file = fc_im.selected im_file = fc_im.selected
#im_file = "/Users/amichaut/Desktop/Fluo-N3DH-CE/stack.tif" #im_file = "/Users/amichaut/Desktop/Fluo-N3DH-CE/stack.tif"
# analyze image # analyze image
y_size,x_size = [512,512] #default size of an image to inialize the make info widget 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) image = tpr.get_image(data_dir,filename=im_file,verbose=True)
if image['image_size'] is not None: if image['image_size'] is not None:
y_size,x_size = image['image_size'] y_size,x_size = image['image_size']
get_image_size = False get_image_size = False
else: else:
get_image_size = True # to call get_image again after info.txt is generated get_image_size = True # to call get_image again after info.txt is generated
# swap z and t dimensions if needed # swap z and t dimensions if needed
check_swap_wid = False # bool to retrieve swap_wid value if necessary 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: if image['t_dim'] is not None and image['z_dim'] is not None:
check_swap_wid=True check_swap_wid=True
printmd("If there is an error between t and z dimension, you can swap these dimensions") 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') swap_wid = widgets.ToggleButton(value=False,description='Swap z and t')
display(swap_wid) display(swap_wid)
# refresh database and info if needed # refresh database and info if needed
database_fn=osp.join(data_dir,'data_base.p') database_fn=osp.join(data_dir,'data_base.p')
info_fn=osp.join(data_dir,'info.txt') info_fn=osp.join(data_dir,'info.txt')
printmd("---") printmd("---")
if osp.exists(database_fn): if osp.exists(database_fn):
printmd('The database already exists, do you want to refresh it?') printmd('The database already exists, do you want to refresh it?')
refresh_db_wid=widgets.ToggleButton(value=False,description='Refresh database') refresh_db_wid=widgets.ToggleButton(value=False,description='Refresh database')
display(refresh_db_wid) display(refresh_db_wid)
if osp.exists(info_fn): if osp.exists(info_fn):
printmd("The info.txt file already exists, do you want to refresh it?") printmd("The info.txt file already exists, do you want to refresh it?")
refresh_info_wid=widgets.ToggleButton(value=False,description='Refresh info') refresh_info_wid=widgets.ToggleButton(value=False,description='Refresh info')
display(refresh_info_wid) display(refresh_info_wid)
``` ```
%%%% Output: stream %%%% Output: stream
You have loaded a 3D image: (2859x1028) pixels with 200 time steps You have loaded a 3D image: (500x500) pixels with 25 time steps
%%%% Output: display_data %%%% 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: %% Cell type:code id: tags:
``` python ``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible printmd("**Don't forget to run this cell!**") # no output cell, make it visible
#swap z and t #swap z and t
if check_swap_wid: if check_swap_wid:
if swap_wid.value: if swap_wid.value:
t_dim = image['t_dim'] t_dim = image['t_dim']
z_dim = image['z_dim'] z_dim = image['z_dim']
image['t_dim'] = z_dim image['t_dim'] = z_dim
image['z_dim'] = t_dim image['z_dim'] = t_dim
printmd("**z and t swapped!**") printmd("**z and t swapped!**")
im = io.imread(image['image_fn']) 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']])) printmd("4D image with {} time steps and {} z slices".format(im.shape[image['t_dim']],im.shape[image['z_dim']]))
del im # free memory del im # free memory
# retrieve refresh widgets values # retrieve refresh widgets values
refresh_db = refresh_db_wid.value if osp.exists(database_fn) else True 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 refresh_info = refresh_info_wid.value if osp.exists(info_fn) else True
# get info # get info
if refresh_info: if refresh_info:
length_unit_wid=widgets.Dropdown(options=['um', 'mm', 'au'],value='um',description='Length unit:',style={'description_width': 'initial'}) 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'}) 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'}) 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'}) 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'}) 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'}) 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'}) 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]) left_box = VBox([length_unit_wid, time_unit_wid,width_wid])
right_box = VBox([length_sc_wid, time_sc_wid,height_wid]) right_box = VBox([length_sc_wid, time_sc_wid,height_wid])
box = HBox([left_box, right_box]) box = HBox([left_box, right_box])
printmd("**Information about the data**") printmd("**Information about the data**")
display(box) display(box)
printmd("In the data table, are the positions given in pixels or in the length unit (given above)?") 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'}) table_unit_wid=widgets.Dropdown(options=['px', 'unit'],value='px',description='Data unit:',style={'description_width': 'initial'})
display(table_unit_wid) 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.") 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) 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] 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'] param_names = ['length_unit','time_unit','lengthscale','timescale','image_width','image_height','table_unit','z_step']
``` ```
%%%% Output: display_data %%%% Output: display_data
**Don't forget to run this cell!** **Don't forget to run this cell!**
%%%% Output: display_data %%%% Output: display_data
**Information about the data** **Information about the data**
%%%% Output: display_data %%%% Output: display_data
%%%% Output: display_data %%%% Output: display_data
In the data table, are the positions given in pixels or in the length unit (given above)? In the data table, are the positions given in pixels or in the length unit (given above)?
%%%% Output: display_data %%%% Output: display_data
%%%% Output: display_data %%%% Output: display_data
If the lengthscale in z is different from the xy lengthscale, enter the z step (in length unit). If not, leave it to zero. If the lengthscale in z is different from the xy lengthscale, enter the z step (in length unit). If not, leave it to zero.
%%%% Output: display_data %%%% Output: display_data
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible printmd("**Don't forget to run this cell!**") # no output cell, make it visible
# save info as txt file # save info as txt file
if refresh_info: if refresh_info:
info = {} info = {}
with open(info_fn,'w+') as f: with open(info_fn,'w+') as f:
for couple in zip(param_names,wid_list): for couple in zip(param_names,wid_list):
info[couple[0]]=couple[1].value info[couple[0]]=couple[1].value
f.write('{}:{}\n'.format(couple[0],couple[1].value)) f.write('{}:{}\n'.format(couple[0],couple[1].value))
else: else:
info = tpr.get_info(data_dir) info = tpr.get_info(data_dir)
image = tpr.get_image(data_dir,filename=im_file,verbose=True) image = tpr.get_image(data_dir,filename=im_file,verbose=True)
``` ```
%%%% Output: display_data %%%% Output: display_data
**Don't forget to run this cell!** **Don't forget to run this cell!**
%%%% Output: stream %%%% Output: stream
You have loaded a 3D image: (2859x1028) pixels with 200 time steps You have loaded a 3D image: (2859x1028) pixels with 200 time steps
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible printmd("**Don't forget to run this cell!**") # no output cell, make it visible
# Set columns identity # Set columns identity
if refresh_db: if refresh_db:
sep = sep_wid.value if sep_wid.value !='tab' else '\t' sep = sep_wid.value if sep_wid.value !='tab' else '\t'
header = 0 if header_wid.value else None header = 0 if header_wid.value else None
df = pd.read_csv(data_file,sep=sep,header=header) df = pd.read_csv(data_file,sep=sep,header=header)
if trackmate_wid.value: if trackmate_wid.value:
df = df.loc[3:] # the 3 first rows in trackmate files are metadata df = df.loc[3:] # the 3 first rows in trackmate files are metadata
printmd("**Here are the first rows of the input data table**") printmd("**Here are the first rows of the input data table**")
display(df.head(10)) display(df.head(10))
``` ```
%%%% Output: display_data %%%% Output: display_data
**Don't forget to run this cell!** **Don't forget to run this cell!**
%%%% Output: display_data %%%% Output: display_data
**Here are the first rows of the input data table** **Here are the first rows of the input data table**
%%%% Output: display_data %%%% Output: display_data
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible printmd("**Don't forget to run this cell!**") # no output cell, make it visible
if refresh_db: if refresh_db:
col_wid_list = [] col_wid_list = []
left_list=[] left_list=[]
right_list=[] right_list=[]
param_list = ['discard','x','y','z','frame','track'] param_list = ['discard','x','y','z','frame','track']
var_list = [] var_list = []
var_wid_dict = {} var_wid_dict = {}
default_trackmate = {'POSITION_X':'x', default_trackmate = {'POSITION_X':'x',
'POSITION_Y':'y', 'POSITION_Y':'y',
'POSITION_Z':'z', 'POSITION_Z':'z',
'FRAME':'frame', 'FRAME':'frame',
'TRACK_ID':'track', 'TRACK_ID':'track',
} }
custom_var_num = df.shape[1] - 5 # number of columns apart from 'x','y','z','frame','track' custom_var_num = df.shape[1] - 5 # number of columns apart from 'x','y','z','frame','track'
for i in range(custom_var_num): for i in range(custom_var_num):
param_list.append('var_{}'.format(i+1)) param_list.append('var_{}'.format(i+1))
var_list.append('var_{}'.format(i+1)) var_list.append('var_{}'.format(i+1))
# display the df columns as two columns of widgets # display the df columns as two columns of widgets
for i,col in enumerate(df.columns): for i,col in enumerate(df.columns):
if trackmate_wid.value: if trackmate_wid.value:
default_val = default_trackmate[col] if col in default_trackmate.keys() else 'discard' default_val = default_trackmate[col] if col in default_trackmate.keys() else 'discard'
else: else:
default_val = 'discard' default_val = 'discard'
wid = widgets.Dropdown(options=param_list,value=default_val, wid = widgets.Dropdown(options=param_list,value=default_val,
description='column {}:'.format(col), description='column {}:'.format(col),
style={'description_width': 'initial'}) style={'description_width': 'initial'})
col_wid_list.append(wid) col_wid_list.append(wid)
if i<len(df.columns)/2: if i<len(df.columns)/2:
left_list.append(wid) left_list.append(wid)
else: else:
right_list.append(wid) right_list.append(wid)
printmd("""**Select the columns to be used in the analysis: track,frame,x,y,(z). printmd("""**Select the columns to be used in the analysis: track,frame,x,y,(z).
Leave to "discard" the other ones. If you want to use custom variables, use var_i.**""") Leave to "discard" the other ones. If you want to use custom variables, use var_i.**""")
printmd("""**If your input file is a trackmate file, mandatory columns are selected by default.**""") printmd("""**If your input file is a trackmate file, mandatory columns are selected by default.**""")
left_box = VBox(left_list) left_box = VBox(left_list)
right_box = VBox(right_list) right_box = VBox(right_list)
display(HBox([left_box, right_box])) display(HBox([left_box, right_box]))
# get the custom variable name and unit # get the custom variable name and unit
HBox_list = [] HBox_list = []
for var in var_list: for var in var_list:
wid_name = widgets.Text(value='',placeholder='Variable name') wid_name = widgets.Text(value='',placeholder='Variable name')
wid_unit = widgets.Text(value='',placeholder='Variable unit') wid_unit = widgets.Text(value='',placeholder='Variable unit')
HBox_list.append(HBox([Label(var+':'),wid_name,wid_unit])) HBox_list.append(HBox([Label(var+':'),wid_name,wid_unit]))
var_wid_dict[var] = {'name': wid_name, 'unit': wid_unit} var_wid_dict[var] = {'name': wid_name, 'unit': wid_unit}
printmd("""**If you selected some custom variables, give their name and unit to be displayed on plots. printmd("""**If you selected some custom variables, give their name and unit to be displayed on plots.
You can use Latex, if necessary.**""") You can use Latex, if necessary.**""")
display(VBox(HBox_list)) display(VBox(HBox_list))
# deal with gaps in trajectories # deal with gaps in trajectories
printmd("""**Some tracking softwares support to miss objects at some frames. This results in tracks with gaps. 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: 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?**""") 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'}) split_wid=widgets.Dropdown(options=['interpolate','split'],value='interpolate',description='gap resolution:'.format(col),style={'description_width': 'initial'})
display(split_wid) display(split_wid)
``` ```
%%%% Output: display_data %%%% Output: display_data
**Don't forget to run this cell!** **Don't forget to run this cell!**
%%%% Output: display_data %%%% Output: display_data
**Select the columns to be used in the analysis: track,frame,x,y,(z). **Select the columns to be used in the analysis: track,frame,x,y,(z).
Leave to "discard" the other ones. If you want to use custom variables, use var_i.** Leave to "discard" the other ones. If you want to use custom variables, use var_i.**
%%%% Output: display_data %%%% Output: display_data
**If your input file is a trackmate file, mandatory columns are selected by default.** **If your input file is a trackmate file, mandatory columns are selected by default.**
%%%% Output: display_data %%%% Output: display_data
%%%% Output: display_data %%%% Output: display_data
**If you selected some custom variables, give their name and unit to be displayed on plots. **If you selected some custom variables, give their name and unit to be displayed on plots.
You can use Latex, if necessary.** You can use Latex, if necessary.**
%%%% Output: display_data %%%% Output: display_data
%%%% Output: display_data %%%% Output: display_data
**Some tracking softwares support to miss objects at some frames. This results in tracks with gaps. **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: 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?** fill the gaps by linear interpolation or split the track in different tracks?**
%%%% Output: display_data %%%% Output: display_data
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible printmd("**Don't forget to run this cell!**") # no output cell, make it visible
if refresh_db: if refresh_db:
col_values = [wid.value for wid in col_wid_list] col_values = [wid.value for wid in col_wid_list]
for param_ in ['x','y','frame','track']: # mandatory columns for param_ in ['x','y','frame','track']: # mandatory columns
if param_ not in col_values: if param_ not in col_values:
raise Exception("You MUST select a column for "+param_) raise Exception("You MUST select a column for "+param_)
df.columns = col_values # rename columns df.columns = col_values # rename columns
# discard non-relevant columns # discard non-relevant columns
col_values = np.array(col_values) col_values = np.array(col_values)
new_cols = col_values[col_values!='discard'] new_cols = col_values[col_values!='discard']
for c in new_cols: for c in new_cols:
if np.count_nonzero(new_cols==c) > 1: # if repeated element if np.count_nonzero(new_cols==c) > 1: # if repeated element
raise Exception("You have selected several times the column: "+c) raise Exception("You have selected several times the column: "+c)
df = df[new_cols] df = df[new_cols]
# get names and units of custom variables # get names and units of custom variables
custom_var = {} custom_var = {}
for var in var_list: for var in var_list:
if var in new_cols: if var in new_cols:
custom_var[var] = {'name': var_wid_dict[var]['name'].value, 'unit': var_wid_dict[var]['unit'].value} custom_var[var] = {'name': var_wid_dict[var]['name'].value, 'unit': var_wid_dict[var]['unit'].value}
# retrieve split traj widget value # retrieve split traj widget value
split_traj = True if split_wid.value=='split' else False split_traj = True if split_wid.value=='split' else False
#get dimension #get dimension
dim_list = ['x','y','z'] if 'z' in df.columns else ['x','y'] dim_list = ['x','y','z'] if 'z' in df.columns else ['x','y']
# convert df data type # convert df data type
for d in dim_list: for d in dim_list:
df[d] = df[d].astype(np.float) df[d] = df[d].astype(np.float)
# coordinates origin # coordinates origin
printmd("""**Do you want to set a custom origin to the coordinates?**""") 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)""") 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') 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'}) 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])) display(HBox([ori_onimage_wid,reset_dim_wid]))
printmd("""Or directly type in the new origin (in px)""") printmd("""Or directly type in the new origin (in px)""")
origin_coord_wid_list=[] origin_coord_wid_list=[]
for dim in dim_list: for dim in dim_list:
print(dim) print(dim)
origin_coord_wid_list.append(widgets.FloatSlider(value=0,min=0,max=df[dim].max(),step=0.1,description=dim,style={'description_width': 'initial'})) 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)) display(HBox(origin_coord_wid_list))
# axes signs # axes signs
printmd("""**Do you want to invert the axes?** printmd("""**Do you want to invert the axes?**
Default orientation: x: left->right, y: top->bottom, z: slice number""") 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'}) invert_axes_wid=widgets.SelectMultiple(options=dim_list,value=[],description='Axes to invert',style={'description_width': 'initial'})
display(invert_axes_wid) display(invert_axes_wid)
``` ```
%%%% Output: display_data %%%% Output: display_data
**Don't forget to run this cell!** **Don't forget to run this cell!**
%%%% Output: display_data %%%% Output: display_data
**Do you want to set a custom origin to the coordinates?** **Do you want to set a custom origin to the coordinates?**
%%%% Output: display_data %%%% Output: display_data
Select a new origin by drawing on the image (you can choose which dimension to reset) Select a new origin by drawing on the image (you can choose which dimension to reset)
%%%% Output: display_data %%%% Output: display_data
%%%% Output: display_data %%%% Output: display_data
Or directly type in the new origin (in px) Or directly type in the new origin (in px)
%%%% Output: stream %%%% Output: stream
x x
y y
%%%% Output: display_data %%%% Output: display_data
%%%% Output: display_data %%%% Output: display_data
**Do you want to invert the axes?** **Do you want to invert the axes?**
Default orientation: x: left->right, y: top->bottom, z: slice number Default orientation: x: left->right, y: top->bottom, z: slice number
%%%% Output: display_data %%%% Output: display_data
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible printmd("**Don't forget to run this cell!**") # no output cell, make it visible
if refresh_db: if refresh_db:
if not ori_onimage_wid.value: if not ori_onimage_wid.value:
origin_coord={} origin_coord={}
all_zeros=True all_zeros=True
for d,wid in enumerate(origin_coord_wid_list): for d,wid in enumerate(origin_coord_wid_list):
origin_coord[dim_list[d]]=wid.value origin_coord[dim_list[d]]=wid.value
if wid.value>0: if wid.value>0:
all_zeros=False all_zeros=False
if all_zeros: # if no change of origin if all_zeros: # if no change of origin
origin_coord = False origin_coord = False
set_origin_ = origin_coord set_origin_ = origin_coord
else: else:
set_origin_ = True set_origin_ = True
#remove None tracks #remove None tracks
df = df[df['track']!='None'] # remove df = df[df['track']!='None'] # remove
data = tpr.get_data(data_dir,df=df,refresh=refresh_db,split_traj=split_traj, 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, set_origin_=set_origin_,image=image,reset_dim=reset_dim_wid.value,
invert_axes=invert_axes_wid.value,custom_var=custom_var) invert_axes=invert_axes_wid.value,custom_var=custom_var)
else: else:
# reload from database # reload from database
data = tpr.get_data(data_dir,df=None,refresh=refresh_db) data = tpr.get_data(data_dir,df=None,refresh=refresh_db)
# useful variables # useful variables
df = data['df'] df = data['df']
lengthscale = data['lengthscale'] lengthscale = data['lengthscale']
timescale = data['timescale'] timescale = data['timescale']
dim = data['dim'] dim = data['dim']
dimensions = data['dimensions'] dimensions = data['dimensions']
custom_var = data['custom_var'] custom_var = data['custom_var']
``` ```
%%%% Output: display_data %%%% Output: display_data
**Don't forget to run this cell!** **Don't forget to run this cell!**
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# general plotting configuration # general plotting configuration
printmd("## General plotting configuration") printmd("## General plotting configuration")
fig_w_wid = widgets.BoundedIntText(value=plot_param['figsize'][0],min=0,max=20, fig_w_wid = widgets.BoundedIntText(value=plot_param['figsize'][0],min=0,max=20,
description='Figure width (inches):',style={'description_width': 'initial'}) description='Figure width (inches):',style={'description_width': 'initial'})
fig_h_wid = widgets.BoundedIntText(value=plot_param['figsize'][1],min=0,max=20, fig_h_wid = widgets.BoundedIntText(value=plot_param['figsize'][1],min=0,max=20,
description='Figure height (inches):',style={'description_width': 'initial'}) description='Figure height (inches):',style={'description_width': 'initial'})
fig_dpi_wid = widgets.BoundedIntText(value=plot_param['dpi'],min=50,max=1e4, fig_dpi_wid = widgets.BoundedIntText(value=plot_param['dpi'],min=50,max=1e4,
description='Figure resolution (dpi):',style={'description_width': 'initial'}) description='Figure resolution (dpi):',style={'description_width': 'initial'})
fig_resfac_wid = widgets.FloatSlider(value=1,min=0.01,max=30,step=0.01, fig_resfac_wid = widgets.FloatSlider(value=1,min=0.01,max=30,step=0.01,
description='Figure size factor:',style={'description_width': 'initial'}) description='Figure size factor:',style={'description_width': 'initial'})
fig_format_wid = widgets.Dropdown(options=['.png','.svg'],value='.png', fig_format_wid = widgets.Dropdown(options=['.png','.svg'],value='.png',
description='Single plot format',style={'description_width': 'initial'}) description='Single plot format',style={'description_width': 'initial'})
save_as_stack_wid = widgets.Checkbox(value=True, description='Save as tiff stack') save_as_stack_wid = widgets.Checkbox(value=True, description='Save as tiff stack')
despine_wid = widgets.ToggleButton(value=plot_param['despine'], despine_wid = widgets.ToggleButton(value=plot_param['despine'],
description='despine figure') description='despine figure')
replace_color_wid = widgets.BoundedIntText(value=0,min=0,max=20, replace_color_wid = widgets.BoundedIntText(value=0,min=0,max=20,
description='Number of colors:',style={'description_width': 'initial'}) description='Number of colors:',style={'description_width': 'initial'})
add_replace_wid = widgets.Dropdown(options=['add','replace'],value='add', add_replace_wid = widgets.Dropdown(options=['add','replace'],value='add',
description='add or replace?',style={'description_width': 'initial'}) description='add or replace?',style={'description_width': 'initial'})
invert_yaxis_wid = widgets.ToggleButton(value=True,description='y axis origin: top') invert_yaxis_wid = widgets.ToggleButton(value=True,description='y axis origin: top')
export_data_pts_wid = widgets.ToggleButton(value=True,description='export data points') export_data_pts_wid = widgets.ToggleButton(value=True,description='export data points')
display(HBox([fig_w_wid,fig_h_wid]),HBox([fig_dpi_wid,fig_format_wid]),despine_wid) display(HBox([fig_w_wid,fig_h_wid]),HBox([fig_dpi_wid,fig_format_wid]),despine_wid)
printmd('Adjust figure resolution (if orginal image is too small or too large)') printmd('Adjust figure resolution (if orginal image is too small or too large)')
display(fig_resfac_wid) display(fig_resfac_wid)
printmd('When plotting over timelapse, plot as a multidimensional tiff stack or as a series of individual image') printmd('When plotting over timelapse, plot as a multidimensional tiff stack or as a series of individual image')
display(save_as_stack_wid) display(save_as_stack_wid)
printmd('Do you want to add/replace the first default colors used for plotting? Give the number of colors you want to select:') 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])) display(HBox([replace_color_wid,add_replace_wid]))
printmd('How do you want to display the y-axis (standard orientation: origin at top)') printmd('How do you want to display the y-axis (standard orientation: origin at top)')
display(invert_yaxis_wid) display(invert_yaxis_wid)
printmd('Do you want to export the data points of your plots as .csv files?') printmd('Do you want to export the data points of your plots as .csv files?')
display(export_data_pts_wid) display(export_data_pts_wid)
``` ```
%%%% Output: display_data %%%% Output: display_data
## General plotting configuration ## General plotting configuration
%%%% Output: display_data %%%% Output: display_data
%%%% Output: display_data %%%% Output: display_data
%%%% Output: display_data %%%% Output: display_data
%%%% Output: display_data %%%% Output: display_data
Adjust figure resolution (if orginal image is too small or too large) Adjust figure resolution (if orginal image is too small or too large)
%%%% Output: display_data %%%% Output: display_data
%%%% Output: display_data %%%% Output: display_data
When plotting over timelapse, plot as a multidimensional tiff stack or as a series of individual image When plotting over timelapse, plot as a multidimensional tiff stack or as a series of individual image
%%%% 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: 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
%%%% Output: display_data %%%% Output: display_data
How do you want to display the y-axis (standard orientation: origin at top) How do you want to display the y-axis (standard orientation: origin at top)
%%%% Output: display_data %%%% Output: display_data
%%%% Output: display_data %%%% Output: display_data
Do you want to export the data points of your plots as .csv files? Do you want to export the data points of your plots as .csv files?
%%%% Output: display_data %%%% Output: display_data
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible printmd("**Don't forget to run this cell!**") # no output cell, make it visible
# replace colors # replace colors
color_wid_list=[] color_wid_list=[]
for i in range(replace_color_wid.value): for i in range(replace_color_wid.value):
color_wid=widgets.ColorPicker(description='Pick color #{}'.format(i),value=color_list[i]) color_wid=widgets.ColorPicker(description='Pick color #{}'.format(i),value=color_list[i])
color_wid_list.append(color_wid) color_wid_list.append(color_wid)
display(*color_wid_list) display(*color_wid_list)
``` ```
%%%% Output: display_data %%%% Output: display_data
**Don't forget to run this cell!** **Don't forget to run this cell!**
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible printmd("**Don't forget to run this cell!**") # no output cell, make it visible
# retreive plotting configuration # retreive plotting configuration
color_list_=list(color_list) color_list_=list(color_list)
new_color_list = [color_wid_list[i].value for i in range(replace_color_wid.value)] new_color_list = [color_wid_list[i].value for i in range(replace_color_wid.value)]
if add_replace_wid.value=='replace': if add_replace_wid.value=='replace':
color_list_[:replace_color_wid.value]=new_color_list color_list_[:replace_color_wid.value]=new_color_list
else: else:
color_list_=new_color_list+color_list_ color_list_=new_color_list+color_list_
plot_param={'figsize':(fig_w_wid.value,fig_h_wid.value), plot_param={'figsize':(fig_w_wid.value,fig_h_wid.value),
'dpi':fig_dpi_wid.value, 'dpi':fig_dpi_wid.value,
'figsize_factor':fig_resfac_wid.value, 'figsize_factor':fig_resfac_wid.value,
'color_list':color_list_, 'color_list':color_list_,
'format':fig_format_wid.value, 'format':fig_format_wid.value,
'despine':despine_wid.value, 'despine':despine_wid.value,
'invert_yaxis':invert_yaxis_wid.value, 'invert_yaxis':invert_yaxis_wid.value,
'export_data_pts':export_data_pts_wid.value, 'export_data_pts':export_data_pts_wid.value,
'save_as_stack': save_as_stack_wid.value, 'save_as_stack': save_as_stack_wid.value,
} }
``` ```
%%%% Output: display_data %%%% Output: display_data
**Don't forget to run this cell!** **Don't forget to run this cell!**
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Filter data # Filter data
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
z_step = info['z_step'] z_step = info['z_step']
if z_step == 0: if z_step == 0:
z_step = lengthscale # if z_step not given, same as lengthscale z_step = lengthscale # if z_step not given, same as lengthscale
printmd('**View trajectories on a Napari viewer before plotting**') printmd('**View trajectories on a Napari viewer before plotting**')
printmd('If not working, please check Napari installation: https://napari.org/') printmd('If not working, please check Napari installation: https://napari.org/')
printmd('Warning: large image can lead to rendering issues') printmd('Warning: large image can lead to rendering issues')
if image['z_dim'] is None: if image['z_dim'] is None:
printmd('Warning: you have a 2D+t image, no 3D redering is available then.') printmd('Warning: you have a 2D+t image, no 3D redering is available then.')
viewer_wid = widgets.Button(value=True, description='Show viewer!') 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'])) viewer_wid.on_click(lambda obj: tpl.view_traj(df, image=image, z_step=info['z_step']))
display(viewer_wid) display(viewer_wid)
``` ```
%%%% Output: display_data %%%% Output: display_data
**View trajectories on a Napari viewer before plotting** **View trajectories on a Napari viewer before plotting**
%%%% Output: display_data %%%% Output: display_data
If not working, please check Napari installation: https://napari.org/ If not working, please check Napari installation: https://napari.org/
%%%% Output: display_data %%%% Output: display_data
Warning: large image can lead to rendering issues Warning: large image can lead to rendering issues
%%%% Output: display_data %%%% Output: display_data
Warning: you have a 2D+t image, no 3D redering is available then. Warning: you have a 2D+t image, no 3D redering is available then.
%%%% Output: display_data %%%% Output: display_data
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
printmd("Your data can be filtered into subsets. How many subsets do you want to analyze?") 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'}) subset_num_wid = widgets.BoundedIntText(value=1,min=1,max=10,description='Number of subsets:',style={'description_width': 'initial'})
display(subset_num_wid) display(subset_num_wid)
printmd("If you define several subsets, do you want to analyze them separately or together?") 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.") 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'}) separate_widget = widgets.ToggleButtons(options=['separately', 'together'], description='Analysis',style={'description_width': 'initial'})
display(separate_widget) display(separate_widget)
``` ```
%%%% Output: display_data %%%% Output: display_data
Your data can be filtered into subsets. How many subsets do you want to analyze? Your data can be filtered into subsets. How many subsets do you want to analyze?
%%%% Output: display_data %%%% Output: display_data
%%%% Output: display_data %%%% Output: display_data
If you define several subsets, do you want to analyze them separately or together? If you define several subsets, do you want to analyze them separately or together?
%%%% Output: display_data %%%% Output: display_data
If analyzed together, subsets will be plotted together. If analyzed separately, each subset will be plotted on individual plots. If analyzed together, subsets will be plotted together. If analyzed separately, each subset will be plotted on individual plots.
%%%% Output: display_data %%%% Output: display_data
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# prepare default values for initializing widgets # prepare default values for initializing widgets
xlim = [df['x'].min(), df['x'].max()] # maybe use image dimensions instead xlim = [df['x'].min(), df['x'].max()] # maybe use image dimensions instead
ylim = [df['y'].min(), df['y'].max()] ylim = [df['y'].min(), df['y'].max()]
zlim = [df['z'].min(), df['z'].max()] if 'z' in df.columns else [] zlim = [df['z'].min(), df['z'].max()] if 'z' in df.columns else []
frame_list = df['frame'].unique() frame_list = df['frame'].unique()
frame_min = df['frame'].min() frame_min = df['frame'].min()
frame_max = df['frame'].max() frame_max = df['frame'].max()
# cropping widget lists # cropping widget lists
xlim_wid_list = [] xlim_wid_list = []
ylim_wid_list = [] ylim_wid_list = []
zlim_wid_list = [] zlim_wid_list = []
drawer_wid_list = [] drawer_wid_list = []
frame_subset_wid_list = [] frame_subset_wid_list = []
min_length_wid_list = [] min_length_wid_list = []
max_length_wid_list = [] max_length_wid_list = []
name_wid_list = [] name_wid_list = []
# retrieve drawer coordinates # retrieve drawer coordinates
class LoadedButton(widgets.Button): class LoadedButton(widgets.Button):
"""A button that can holds a value as a attribute.""" """A button that can holds a value as a attribute."""
def __init__(self, value=None, *args, **kwargs): def __init__(self, value=None, *args, **kwargs):
super(LoadedButton, self).__init__(*args, **kwargs) super(LoadedButton, self).__init__(*args, **kwargs)
# Create the value attribute. # Create the value attribute.
self.add_traits(value=traitlets.Any(value)) self.add_traits(value=traitlets.Any(value))
def get_image_coord(ex): def get_image_coord(ex):
"""Call get_coordinates, display instruction and store coordinates in widget value """ """Call get_coordinates, display instruction and store coordinates in widget value """
ex.value = tpr.get_coordinates(image,df=df,verbose=True) ex.value = tpr.get_coordinates(image,df=df,verbose=True)
# create a set of filtering widget for each subset # create a set of filtering widget for each subset
for i in range(subset_num_wid.value): for i in range(subset_num_wid.value):
printmd("""### Subset #{}""".format(i + 1)) printmd("""### Subset #{}""".format(i + 1))
# subset name # subset name
printmd("""You can give it a custom name that will be used for saving data""") 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:', name_wid = widgets.Text(value='', placeholder='optional', description='Subset name:',
style={'description_width': 'initial'}) style={'description_width': 'initial'})
name_wid_list.append(name_wid) name_wid_list.append(name_wid)
display(name_wid) display(name_wid)
# spatial filtering # spatial filtering
xlim_wid = widgets.FloatRangeSlider(value=xlim, min=xlim[0], max=xlim[1], step=1, xlim_wid = widgets.FloatRangeSlider(value=xlim, min=xlim[0], max=xlim[1], step=1,
description='x range (px):', style={'description_width': 'initial'}) description='x range (px):', style={'description_width': 'initial'})
ylim_wid = widgets.FloatRangeSlider(value=ylim, min=ylim[0], max=ylim[1], step=1, ylim_wid = widgets.FloatRangeSlider(value=ylim, min=ylim[0], max=ylim[1], step=1,
description='y range (px):', style={'description_width': 'initial'}) description='y range (px):', style={'description_width': 'initial'})
if len(zlim) > 0: if len(zlim) > 0:
zlim_wid = widgets.FloatRangeSlider(value=zlim, min=zlim[0], max=zlim[1], step=1, zlim_wid = widgets.FloatRangeSlider(value=zlim, min=zlim[0], max=zlim[1], step=1,
description='z range (px):', style={'description_width': 'initial'}) description='z range (px):', style={'description_width': 'initial'})
drawer_wid = LoadedButton(description="Draw ROI", value={}) drawer_wid = LoadedButton(description="Draw ROI", value={})
drawer_wid.on_click(get_image_coord) drawer_wid.on_click(get_image_coord)
# store widgets # store widgets
xlim_wid_list.append(xlim_wid) xlim_wid_list.append(xlim_wid)
ylim_wid_list.append(ylim_wid) ylim_wid_list.append(ylim_wid)
drawer_wid_list.append(drawer_wid) drawer_wid_list.append(drawer_wid)
if len(zlim) > 0: if len(zlim) > 0:
zlim_wid_list.append(zlim_wid) zlim_wid_list.append(zlim_wid)
else: else:
zlim_wid_list.append(None) zlim_wid_list.append(None)
#display #display
printmd("**Crop a region**") printmd("**Crop a region**")
printmd("You can draw it on the image or select it with the slider (values in pixels)." 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" "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") ", they will be divided into several subsets")
printmd("Draw using the rectangle shape, hit ENTER while you're done." printmd("Draw using the rectangle shape, hit ENTER while you're done."
"If you want more details about the email viewer please visit https://napari.org/") "If you want more details about the email viewer please visit https://napari.org/")
if len(zlim) > 0: if len(zlim) > 0:
display(VBox([drawer_wid, HBox([xlim_wid, ylim_wid, zlim_wid])])) display(VBox([drawer_wid, HBox([xlim_wid, ylim_wid, zlim_wid])]))
else: else:
display(VBox([drawer_wid, HBox([xlim_wid, ylim_wid])])) display(VBox([drawer_wid, HBox([xlim_wid, ylim_wid])]))
# time filtering # time filtering
frame_subset_wid = widgets.IntRangeSlider(value=[frame_min, frame_max], min=frame_min, max=frame_max, step=1, 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'}) description='Frame subset:', style={'description_width': 'initial'})
min_length_wid = widgets.IntSlider(value=frame_min, min=frame_min, max=frame_max + 1, step=1, 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'}) 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, 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'}) description='Maximum traj length:', style={'description_width': 'initial'})
# store widgets # store widgets
frame_subset_wid_list.append(frame_subset_wid) frame_subset_wid_list.append(frame_subset_wid)
min_length_wid_list.append(min_length_wid) min_length_wid_list.append(min_length_wid)
max_length_wid_list.append(max_length_wid) max_length_wid_list.append(max_length_wid)
# display # display
printmd("**Select data based on trajectories duration or frame subset**") printmd("**Select data based on trajectories duration or frame subset**")
display(HBox([frame_subset_wid, min_length_wid, max_length_wid])) display(HBox([frame_subset_wid, min_length_wid, max_length_wid]))
``` ```
%%%% Output: display_data %%%% Output: display_data
### Subset #1 ### Subset #1
%%%% Output: display_data %%%% Output: display_data
You can give it a custom name that will be used for saving data You can give it a custom name that will be used for saving data
%%%% Output: display_data %%%% Output: display_data
%%%% Output: display_data %%%% Output: display_data
**Crop a region** **Crop a region**
%%%% Output: display_data %%%% 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 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 %%%% Output: display_data
Draw using the rectangle shape, hit ENTER while you're done.If you want more details about the email viewer please visit https://napari.org/ Draw using the rectangle shape, hit ENTER while you're done.If you want more details about the email viewer please visit https://napari.org/
%%%% Output: display_data %%%% Output: display_data
%%%% Output: display_data %%%% Output: display_data
**Select data based on trajectories duration or frame subset** **Select data based on trajectories duration or frame subset**
%%%% Output: display_data %%%% Output: display_data
%%%% Output: stream %%%% Output: stream
Draw points or rectangles, then press ENTER Draw points or rectangles, then press ENTER
You have selected 0 point(s) and 1 rectangle(s) You have selected 0 point(s) and 1 rectangle(s)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# retrieve filters values # retrieve filters values
def retrieve_region_wid_values(drawer_wid,xlim_wid,ylim_wid,zlim_wid): 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 Get values of region selection giving priority to drawer widgets over sliders
""" """
# xylim # xylim
use_sliders = False use_sliders = False
drawer_value = drawer_wid.value drawer_value = drawer_wid.value
if drawer_value: #if not empty if drawer_value: #if not empty
if len(drawer_value['rectangle']) > 0: if len(drawer_value['rectangle']) > 0:
#get list of xlim, ylim in case of several ROIs selected #get list of xlim, ylim in case of several ROIs selected
xlim_list = [r['xlim'] for r in drawer_value['rectangle']] xlim_list = [r['xlim'] for r in drawer_value['rectangle']]
ylim_list = [r['ylim'] for r in drawer_value['rectangle']] ylim_list = [r['ylim'] for r in drawer_value['rectangle']]
else: else:
use_sliders = True use_sliders = True
else: else:
use_sliders = True use_sliders = True
if use_sliders: if use_sliders:
xlim_list = [xlim_wid.value] xlim_list = [xlim_wid.value]
ylim_list = [ylim_wid.value] ylim_list = [ylim_wid.value]
# zlim # zlim
if zlim_wid is None: if zlim_wid is None:
zlim = None zlim = None
else: else:
zlim = zlim_wid.value zlim = zlim_wid.value
return xlim_list, ylim_list, zlim return xlim_list, ylim_list, zlim
filters=[] filters=[]
for i in range(subset_num_wid.value): for i in range(subset_num_wid.value):
xlim_list, ylim_list, zlim_ = retrieve_region_wid_values(drawer_wid_list[i], xlim_list, ylim_list, zlim_ = retrieve_region_wid_values(drawer_wid_list[i],
xlim_wid_list[i], xlim_wid_list[i],
ylim_wid_list[i], ylim_wid_list[i],
zlim_wid_list[i]) zlim_wid_list[i])
# if several subsets get values of xlim, ylim, while keeping other filters constant # if several subsets get values of xlim, ylim, while keeping other filters constant
for j in range(len(xlim_list)): for j in range(len(xlim_list)):
#name #name
if len(xlim_list) > 1: # if several subsets, modify name: name_number if name is empty: number 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) name = name_wid_list[i].value + '_{}'.format(j) if name_wid_list[i].value != '' else '{}'.format(j)
else: else:
name = name_wid_list[i].value name = name_wid_list[i].value
# initialize filter dict # initialize filter dict
default_filter = tpr.init_filters(data_dir=None, export_config=False) default_filter = tpr.init_filters(data_dir=None, export_config=False)
filt_ = default_filter['filters_list'][0] filt_ = default_filter['filters_list'][0]
# fill info # fill info
filt_['xlim'] = xlim_list[j] filt_['xlim'] = xlim_list[j]
filt_['ylim'] = ylim_list[j] filt_['ylim'] = ylim_list[j]
filt_['zlim'] = zlim_ filt_['zlim'] = zlim_
filt_['frame_subset'] = frame_subset_wid_list[i].value filt_['frame_subset'] = frame_subset_wid_list[i].value
filt_['min_traj_len'] = min_length_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_['max_traj_len'] = max_length_wid_list[i].value
filt_['name'] = name filt_['name'] = name
# add to filter list # add to filter list
filters.append(filt_) filters.append(filt_)
printmd("**Select specific trajectories**") printmd("**Select specific trajectories**")
printmd("You can select specific sets of trajectories. " printmd("You can select specific sets of trajectories. "
"This tool can be useful to perform fate mapping or retrospective mapping. " "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." "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?") 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'}) set_num_wid = widgets.BoundedIntText(value=0,min=0,max=10,description='Number of sets:',style={'description_width': 'initial'})
display(set_num_wid) display(set_num_wid)
#or by selecting trajectories in a specific region at a specific frame. " #or by selecting trajectories in a specific region at a specific frame. "
#just by giving a list of ids, #just by giving a list of ids,
``` ```
%%%% Output: display_data %%%% Output: display_data
**Select specific trajectories** **Select specific trajectories**
%%%% Output: display_data %%%% 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. 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 %%%% Output: display_data
How many sets of trajectories do you want to select? How many sets of trajectories do you want to select?
%%%% Output: display_data %%%% Output: display_data
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible printmd("**Don't forget to run this cell!**") # no output cell, make it visible
# select specific trajectories # select specific trajectories
# track selection widget lists # track selection widget lists
track_name_wid_list = [] track_name_wid_list = []
track_xlim_wid_list = [] track_xlim_wid_list = []
track_ylim_wid_list = [] track_ylim_wid_list = []
track_zlim_wid_list = [] track_zlim_wid_list = []
track_drawer_wid_list = [] track_drawer_wid_list = []
track_frame_subset_wid_list = [] track_frame_subset_wid_list = []
track_list_wid_list = [] track_list_wid_list = []
for i in range(set_num_wid.value): for i in range(set_num_wid.value):
printmd("""### Set #{}""".format(i + 1)) printmd("""### Set #{}""".format(i + 1))
# subset name # subset name
printmd("""You can give it a custom name that will be used for saving data""") 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:', track_name_wid = widgets.Text(value='', placeholder='optional', description='Set name:',
style={'description_width': 'initial'}) style={'description_width': 'initial'})
track_name_wid_list.append(track_name_wid) track_name_wid_list.append(track_name_wid)
display(track_name_wid) display(track_name_wid)
track_list_wid = widgets.Text(value='', placeholder='comma separated ids', description='Id list:') 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, track_xlim_wid = widgets.FloatRangeSlider(value=xlim, min=xlim[0], max=xlim[1], step=1,
description='x range (px):', style={'description_width': 'initial'}) description='x range (px):', style={'description_width': 'initial'})
track_ylim_wid = widgets.FloatRangeSlider(value=ylim, min=ylim[0], max=ylim[1], step=1, track_ylim_wid = widgets.FloatRangeSlider(value=ylim, min=ylim[0], max=ylim[1], step=1,
description='y range (px):', style={'description_width': 'initial'}) description='y range (px):', style={'description_width': 'initial'})
if len(zlim) > 0: if len(zlim) > 0:
track_zlim_wid = widgets.FloatRangeSlider(value=zlim, min=zlim[0], max=zlim[1], step=1, track_zlim_wid = widgets.FloatRangeSlider(value=zlim, min=zlim[0], max=zlim[1], step=1,
description='z range (px):', style={'description_width': 'initial'}) 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, 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'}) description='Frame intervalle:', style={'description_width': 'initial'})
track_drawer_wid = LoadedButton(description="Draw ROI", value={}) track_drawer_wid = LoadedButton(description="Draw ROI", value={})
track_drawer_wid.on_click(get_image_coord) track_drawer_wid.on_click(get_image_coord)
# store widgets # store widgets
track_list_wid_list.append(track_list_wid) track_list_wid_list.append(track_list_wid)
track_xlim_wid_list.append(track_xlim_wid) track_xlim_wid_list.append(track_xlim_wid)
track_ylim_wid_list.append(track_ylim_wid) track_ylim_wid_list.append(track_ylim_wid)
track_drawer_wid_list.append(track_drawer_wid) track_drawer_wid_list.append(track_drawer_wid)
track_frame_subset_wid_list.append(track_frame_subset_wid) track_frame_subset_wid_list.append(track_frame_subset_wid)
if len(zlim) > 0: if len(zlim) > 0:
track_zlim_wid_list.append(track_zlim_wid) track_zlim_wid_list.append(track_zlim_wid)
else: else:
track_zlim_wid_list.append(None) track_zlim_wid_list.append(None)
#display #display
printmd("Give a list of ids or select trajectories in a specific region during a specific frame intervalle using the viewer.") 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, " 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)") "(visualizing the trajectories with the printed labels can be helpful)")
if len(zlim) > 0: 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])) display(VBox([track_list_wid,track_drawer_wid, HBox([track_xlim_wid, track_ylim_wid, track_zlim_wid]),track_frame_subset_wid]))
else: else:
display(VBox([track_list_wid,track_drawer_wid, HBox([track_xlim_wid, track_ylim_wid]),track_frame_subset_wid])) display(VBox([track_list_wid,track_drawer_wid, HBox([track_xlim_wid, track_ylim_wid]),track_frame_subset_wid]))
``` ```
%%%% Output: display_data %%%% Output: display_data
**Don't forget to run this cell!** **Don't forget to run this cell!**
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
printmd("**Don't forget to run this cell!**") # no output cell, make it visible printmd("**Don't forget to run this cell!**") # no output cell, make it visible
if set_num_wid.value == 0: if set_num_wid.value == 0:
new_filters = list(filters) new_filters = list(filters)
else: else:
new_filters = [] new_filters = []
# for each set of filters, copy them to new sets of filters modified by the trajectory filters # for each set of filters, copy them to new sets of filters modified by the trajectory filters
for filt in filters: for filt in filters:
for i in range(set_num_wid.value): for i in range(set_num_wid.value):
# trajectory list selection # trajectory list selection
track_list_val = track_list_wid_list[i].value track_list_val = track_list_wid_list[i].value
if track_list_val == '': if track_list_val == '':
track_list_val = None track_list_val = None
else: else:
try: try:
track_list_val = [int(e) for e in track_list_val.split(',')] track_list_val = [int(e) for e in track_list_val.split(',')]
except: except:
print("ERROR: a value in the list is not a number") print("ERROR: a value in the list is not a number")
track_list_val = None track_list_val = None
# ROI selection # ROI selection
track_xlim_list, track_ylim_list, track_zlim_ = retrieve_region_wid_values(track_drawer_wid_list[i], track_xlim_list, track_ylim_list, track_zlim_ = retrieve_region_wid_values(track_drawer_wid_list[i],
track_xlim_wid_list[i], track_xlim_wid_list[i],
track_ylim_wid_list[i], track_ylim_wid_list[i],
track_zlim_wid_list[i]) track_zlim_wid_list[i])
# if several sets, get values of xlim, ylim, while keeping other filters constant # if several sets, get values of xlim, ylim, while keeping other filters constant
for j in range(len(track_xlim_list)): for j in range(len(track_xlim_list)):
ROI = {'xlim': track_xlim_list[j], ROI = {'xlim': track_xlim_list[j],
'ylim': track_ylim_list[j], 'ylim': track_ylim_list[j],
'zlim': track_zlim_, 'zlim': track_zlim_,
'frame_lim': track_frame_subset_wid_list[i].value} 'frame_lim': track_frame_subset_wid_list[i].value}
#name #name
if len(track_xlim_list) > 1: # if several subsets, modify name if len(track_xlim_list) > 1: # if several subsets, modify name
name = track_name_wid_list[i].value + '_{}'.format(j) name = track_name_wid_list[i].value + '_{}'.format(j)
else: else:
name = track_name_wid_list[i].value name = track_name_wid_list[i].value
# add new filters to existing filters # add new filters to existing filters
new_filt = dict(filt) # make copy of old filters new_filt = dict(filt) # make copy of old filters
new_filt['track_list'] = track_list_val new_filt['track_list'] = track_list_val
new_filt['track_ROI'] = ROI new_filt['track_ROI'] = ROI
if new_filt['name'] == '': if new_filt['name'] == '':
new_filt['name'] = name # name = setname new_filt['name'] = name # name = setname
else: else:
if name != '': if name != '':
new_filt['name'] += '_' + name # name = subsetname_setname new_filt['name'] += '_' + name # name = subsetname_setname
# store in a new list of filters # store in a new list of filters
new_filters.append(new_filt) new_filters.append(new_filt)
printmd('You set {} subsets. Edit their names and order to be plotted if needed.'.format(len(new_filters))) printmd('You set {} subsets. Edit their names and order to be plotted if needed.'.format(len(new_filters)))
filt_names_wid_list = [] filt_names_wid_list = []
order_wid_list =