Skip to content

Commit

Permalink
Report distance from track origin (#158)
Browse files Browse the repository at this point in the history
* Report distance from track origin

* Update docstring

Co-authored-by: Alessandro Felder <[email protected]>

---------

Co-authored-by: Alessandro Felder <[email protected]>
  • Loading branch information
adamltyson and alessandrofelder committed Dec 18, 2023
1 parent fdef40d commit 0ca1f2e
Show file tree
Hide file tree
Showing 4 changed files with 1,045 additions and 1,018 deletions.
33 changes: 30 additions & 3 deletions brainglobe_segmentation/tracks/analysis.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import pandas as pd
from scipy.spatial.distance import euclidean

from brainglobe_segmentation.tracks.fit import spline_fit

Expand Down Expand Up @@ -103,6 +104,18 @@ def run_track_analysis(
return spline


def get_distances(spline, voxel_size=10):
"""
For a given spline, calculate the distance between each point.
Assumes a customisable isotropic voxel size (default 10) in microns.
"""
distances = [0]
for i in range(len(spline) - 1):
distance = round(euclidean(spline[i], spline[i + 1]) * voxel_size, 3)
distances.append(distances[i] + distance)
return distances


def analyse_track_anatomy(annotations_layer_image, atlas, spline, file_path):
"""
For a given spline, find the atlas region that each
Expand All @@ -124,8 +137,16 @@ def analyse_track_anatomy(annotations_layer_image, atlas, spline, file_path):
except KeyError:
spline_regions.append(None)

distances = get_distances(spline, voxel_size=atlas.resolution[0])

df = pd.DataFrame(
columns=["Position", "Region ID", "Region acronym", "Region name"]
columns=[
"Index",
"Distance from first position [um]",
"Region ID",
"Region acronym",
"Region name",
]
)
for idx, spline_region in enumerate(spline_regions):
if spline_region is None:
Expand All @@ -135,7 +156,10 @@ def analyse_track_anatomy(annotations_layer_image, atlas, spline, file_path):
pd.DataFrame(
[
{
"Position": idx,
"Index": idx,
"Distance from first position [um]": distances[
idx
],
"Region ID": "Not found in brain",
"Region acronym": "Not found in brain",
"Region name": "Not found in brain",
Expand All @@ -152,7 +176,10 @@ def analyse_track_anatomy(annotations_layer_image, atlas, spline, file_path):
pd.DataFrame(
[
{
"Position": idx,
"Index": idx,
"Distance from first position [um]": distances[
idx
],
"Region ID": spline_region["id"],
"Region acronym": spline_region["acronym"],
"Region name": spline_region["name"],
Expand Down
Loading

0 comments on commit 0ca1f2e

Please sign in to comment.