Skip to content
Snippets Groups Projects
Commit ded95650 authored by François  LAURENT's avatar François LAURENT
Browse files

4-quick-landscapes.ipynb released on 210810

parent de258ddb
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id:d4080a30 tags: %% Cell type:code id:d4080a30 tags:
``` python ``` python
from tramway.analyzer import * from tramway.analyzer import *
a = RWAnalyzer() a = RWAnalyzer()
a.spt_data = spt_data.from_ascii_files(r'../data/interim/210503_Glyb-Eos/210503_Glyb-Eosmov[0-9][0-9][0-9].txt') a.spt_data = spt_data.from_ascii_files(r'../data/interim/210503_Glyb-Eos/210503_Glyb-Eosmov[0-9][0-9][0-9].txt')
a.spt_data.localization_precision = sigma = .0106 a.spt_data.localization_precision = sigma = .0106
a.spt_data.frame_interval = dt = .015 a.spt_data.frame_interval = dt = .015
``` ```
%% Cell type:markdown id:2ff19c3a tags: %% Cell type:markdown id:2ff19c3a tags:
# Define ROI # Define ROI
%% Cell type:code id:e7857aa3 tags: %% Cell type:code id:e7857aa3 tags:
``` python ``` python
import numpy as np import numpy as np
import pandas as pd import pandas as pd
def roi_as_dataframe(bounding_boxes, columns=None): def roi_as_dataframe(bounding_boxes, columns=None):
bb = np.stack([ np.r_[lb, ub] for lb, ub in bounding_boxes ], axis=0) bb = np.stack([ np.r_[lb, ub] for lb, ub in bounding_boxes ], axis=0)
if columns is None: if columns is None:
if bb.shape[1] == 4: if bb.shape[1] == 4:
columns = ['x', 'y'] columns = ['x', 'y']
elif bb.shape[1] == 6: elif bb.shape[1] == 6:
columns = ['x', 'y', 't'] columns = ['x', 'y', 't']
else: else:
raise ValueError('undefined column names') raise ValueError('undefined column names')
return pd.DataFrame(bb, columns=[ ' '.join((coord, bound)) for coord in columns for bound in ('min', 'max') ]) return pd.DataFrame(bb, columns=[ ' '.join((coord, bound)) for coord in columns for bound in ('min', 'max') ])
``` ```
%% Cell type:code id:32804b7d tags: %% Cell type:code id:32804b7d tags:
``` python ``` python
from tramway.analyzer.roi.utils import * from tramway.analyzer.roi.utils import *
side = 1. side = 1.
min_transloc = 200 min_transloc = 200
for f in a.spt_data: for f in a.spt_data:
df = f.dataframe df = f.dataframe
roi_centers = density_based_roi(df, .04) # candidate ROI roi_centers = density_based_roi(df, .04) # candidate ROI
b = RWAnalyzer() b = RWAnalyzer()
b.spt_data = spt_data.from_dataframe(df[['n', 'x', 'y', 't']]) b.spt_data = spt_data.from_dataframe(df[['n', 'x', 'y', 't']])
b.spt_data.localization_error = a.spt_data.localization_error b.spt_data.localization_error = a.spt_data.localization_error
b.roi = roi.from_squares(roi_centers, side) b.roi = roi.from_squares(roi_centers, side)
selected_roi = [] selected_roi = []
for r in b.roi: for r in b.roi:
r_df = r.crop() r_df = r.crop()
r_df = r.discard_static_trajectories(r_df) r_df = r.discard_static_trajectories(r_df)
if min_transloc <= len(r_df): if min_transloc <= len(r_df):
selected_roi.append(r.bounding_box) selected_roi.append(r.bounding_box)
if selected_roi: if selected_roi:
c = RWAnalyzer() c = RWAnalyzer()
c.spt_data = spt_data.from_ascii_file(f.source) c.spt_data = spt_data.from_ascii_file(f.source)
c.roi = roi.from_bounding_boxes(roi_as_dataframe(selected_roi)) c.roi = roi.from_bounding_boxes(roi_as_dataframe(selected_roi))
c.roi.to_ascii_file(f.source[:-4]+'-roi.txt') c.roi.to_ascii_file(f.source[:-4]+'-roi.txt')
else: else:
print(f'no roi for file {f.source}') print(f'no roi for file {f.source}')
``` ```
%% Output %% Output
few roi found: 6
few roi found: 5
few roi found: 5
no roi for file ../data/raw/210503_Glyb-Eos/210503_Glyb-Eosmov018.txt
few roi found: 8
no roi for file ../data/raw/210503_Glyb-Eos/210503_Glyb-Eosmov006.txt
few roi found: 4
no roi for file ../data/raw/210503_Glyb-Eos/210503_Glyb-Eosmov020.txt
few roi found: 6
no roi for file ../data/raw/210503_Glyb-Eos/210503_Glyb-Eosmov003.txt
few roi found: 4
few roi found: 5
no roi for file ../data/raw/210503_Glyb-Eos/210503_Glyb-Eosmov015.txt
few roi found: 7
no roi for file ../data/raw/210503_Glyb-Eos/210503_Glyb-Eosmov012.txt
no roi for file ../data/raw/210503_Glyb-Eos/210503_Glyb-Eosmov016.txt
global max density is below the threshold: 0.04466814859471522 < 0.05
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last) KeyboardInterrupt Traceback (most recent call last)
<ipython-input-3-6e2a6552cfe5> in <module> <ipython-input-3-9a96db07a9a3> in <module>
7 df = f.dataframe 7 df = f.dataframe
8 8
----> 9 roi_centers = density_based_roi(df, .05) # candidate ROI ----> 9 roi_centers = density_based_roi(df, .04) # candidate ROI
10 10
11 b = RWAnalyzer() 11 b = RWAnalyzer()
~/github/TRamWAy/tramway/analyzer/roi/utils.py in density_based_roi(locations, min_kernel_density, target_pattern_size, step_size_factor, dr, kernel_density) ~/github/TRamWAy/tramway/analyzer/roi/utils.py in density_based_roi(locations, min_kernel_density, target_pattern_size, step_size_factor, dr, kernel_density)
66 print('few roi found: {}'.format(len(roi_centers))) 39 grid = np.c_[_x.reshape((-1,1)),_y.reshape((-1,1))]
67 else: 40
---> 68 raise RuntimeError('no roi found') ---> 41 log_density = epanechnikov_density(xy, grid, target_pattern_size)
69 42 log_density = log_density.reshape(len(grid_x),len(grid_y))
70 return roi_centers 43
RuntimeError: no roi found ~/github/TRamWAy/tramway/analyzer/roi/utils.py in epanechnikov_density(xy, eval_at, target_pattern_size)
16
17 estimator.fit(xy)
---> 18 log_density = estimator.score_samples(eval_at)
19
20 return log_density
~/.local/lib/python3.8/site-packages/sklearn/neighbors/_kde.py in score_samples(self, X)
199 N = self.tree_.sum_weight
200 atol_N = self.atol * N
--> 201 log_density = self.tree_.kernel_density(
202 X, h=self.bandwidth, kernel=self.kernel, atol=atol_N,
203 rtol=self.rtol, breadth_first=self.breadth_first, return_log=True)
KeyboardInterrupt:
%% Cell type:markdown id:37585b0e tags: %% Cell type:markdown id:37585b0e tags:
# Reload and inspect a few ROI # Reload and inspect a few ROI
%% Cell type:code id:7064300b tags: %% Cell type:code id:7064300b tags:
``` python ``` python
a.roi = roi.from_ascii_files() a.roi = roi.from_ascii_files()
a.time = time.TimeWindow(duration=window_duration, shift=window_shift) a.time = time.TimeWindow(duration=window_duration, shift=window_shift)
a.tesseller = tessellers.KMeans a.tesseller = tessellers.KMeans
a.tesseller.resolution = .1 a.tesseller.resolution = .1
``` ```
%% Cell type:code id:f1248422 tags: %% Cell type:code id:f1248422 tags:
``` python ``` python
for r in a.roi: for r in a.roi:
df = r.crop() df = r.crop()
df = r.discard_static_trajectories(df) df = r.discard_static_trajectories(df)
assignment = a.sampler.sample(df) assignment = a.sampler.sample(df)
a a
a.tesseller.mpl.animate(assignment) a.tesseller.mpl.animate(assignment)
``` ```
%% Cell type:code id:3e8a5658 tags: %% Cell type:code id:3e8a5658 tags:
``` python ``` python
help(a.tesseller.mpl.animate) help(a.tesseller.mpl.animate)
``` ```
%% Cell type:code id:b4d78654 tags: %% Cell type:code id:b4d78654 tags:
``` python ``` python
``` ```
......
Source diff could not be displayed: it is too large. Options to address this: view the blob.
Source diff could not be displayed: it is too large. Options to address this: view the blob.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment