Skip to content

Commit

Permalink
Merge pull request #5 from brainglobe/tests
Browse files Browse the repository at this point in the history
Add tests and fix bugs
  • Loading branch information
adamltyson committed Aug 26, 2020
2 parents a8c84bf + 4cf06a7 commit ea574bc
Show file tree
Hide file tree
Showing 20 changed files with 38,689 additions and 49 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
[![Python Version](https://img.shields.io/pypi/pyversions/brainreg-segment.svg)](https://pypi.org/project/brainreg-segment)
[![PyPI](https://img.shields.io/pypi/v/brainreg-segment.svg)](https://pypi.org/project/brainreg-segment)
[![Wheel](https://img.shields.io/pypi/wheel/brainreg-segment.svg)](https://pypi.org/project/brainreg-segment)
[![Development Status](https://img.shields.io/pypi/status/brainreg-segment.svg)](https://github.com/brainglobe/brainreg-segment)
[![Travis](https://img.shields.io/travis/com/brainglobe/brainreg-segment?label=Travis%20CI)](
https://travis-ci.com/brainglobe/brainreg-segment)
[![Coverage Status](https://coveralls.io/repos/github/brainglobe/brainreg-segment/badge.svg?branch=master)](https://coveralls.io/github/brainglobe/brainreg-segment?branch=master)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/python/black)
[![Gitter](https://badges.gitter.im/cellfinder/brainreg.svg)](https://gitter.im/cellfinder/brainreg?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)

# brainreg-segment
Segmentation of 3D brain structures in a common anatomical space
27 changes: 23 additions & 4 deletions brainreg_segment/atlas/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,33 @@


def lateralise_atlas_image(
atlas, hemispheres, left_hemisphere_value=1, right_hemisphere_value=2
masked_atlas_annotations,
hemispheres,
left_hemisphere_value=1,
right_hemisphere_value=2,
):
atlas_left = atlas[hemispheres == left_hemisphere_value]
atlas_right = atlas[hemispheres == right_hemisphere_value]
return atlas_left, atlas_right
"""
:param masked_atlas_annotations: Masked image of atlas annotations
:param hemispheres: Hemispheres image
:param left_hemisphere_value: Value encoded in hemispheres image
:param right_hemisphere_value: Value encoded in hemispheres image
:return: Tuple (left, right) of numpy arrays of values within
each hemisphere
"""
annotation_left = masked_atlas_annotations[
hemispheres == left_hemisphere_value
]
annotation_right = masked_atlas_annotations[
hemispheres == right_hemisphere_value
]
return annotation_left, annotation_right


def get_available_atlases():
"""
Get the available brainglobe atlases
:return: Dict of available atlases (["name":version])
"""
available_atlases = utils.conf_from_url(
descriptors.remote_url_base.format("last_versions.conf")
)
Expand Down
2 changes: 1 addition & 1 deletion brainreg_segment/image/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

def create_KDTree_from_image(image, value=0):
"""
Create a KDTree of points equally a given value
Create a KDTree of points equalling a given value
:param image: Image to be converted to points
:param value: Value of image to be used
:return: scipy.spatial.cKDTree object
Expand Down
32 changes: 11 additions & 21 deletions brainreg_segment/regions/IO.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import imio

import tifffile
import numpy as np

from pathlib import Path
Expand Down Expand Up @@ -90,11 +89,14 @@ def save_label_layers(regions_directory, label_layers):
save_regions_to_file(label_layer, regions_directory)


def export_label_layers(regions_directory, label_layers):
def export_label_layers(
regions_directory, label_layers, voxel_size, obj_ext=".obj"
):
print(f"Exporting regions to: {regions_directory}")
regions_directory.mkdir(parents=True, exist_ok=True)
for label_layer in label_layers:
export_regions_to_file(label_layer, regions_directory)
filename = regions_directory / (label_layer.name + obj_ext)
export_regions_to_file(label_layer.data, filename, voxel_size)


def save_regions_to_file(
Expand All @@ -118,28 +120,16 @@ def save_regions_to_file(
name = label_layer.name

filename = destination_directory / (name + image_extension)
imio.to_tiff(data.astype(np.int16), filename)
tifffile.imsave(filename, data.astype(np.int16))


def export_regions_to_file(
label_layer, destination_directory, ignore_empty=True, obj_ext=".obj",
):
def export_regions_to_file(image, filename, voxel_size, ignore_empty=True):
"""
Export regions as .obj for brainrender
:param label_layer: napari labels layer (with segmented regions)
:param destination_directory: Where to save files to
:param ignore_empty: If True, don't attempt to save empty images
:param obj_ext: File extension for the obj files
:param image_extension: File extension fo the image files
"""
data = label_layer.data
if ignore_empty:
if data.sum() == 0:
if image.sum() == 0:
return

name = label_layer.name

filename = destination_directory / (name + obj_ext)
volume_to_vector_array_to_obj_file(
data, filename,
)
volume_to_vector_array_to_obj_file(image, filename, voxel_size=voxel_size)
4 changes: 2 additions & 2 deletions brainreg_segment/regions/layers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import imio
import tifffile

import numpy as np

Expand Down Expand Up @@ -61,7 +61,7 @@ def add_existing_label_layers(
:return label_layer: napari labels layer
"""
label_file = Path(label_file)
labels = imio.load_any(label_file)
labels = tifffile.imread(label_file)
label_layer = viewer.add_labels(
labels, num_colors=num_colors, name=label_file.stem
)
Expand Down
3 changes: 2 additions & 1 deletion brainreg_segment/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ def initialise_track_tracing(self):
self.track_panel.setVisible(True)
self.region_panel.setVisible(True)
self.splines = None
self.spline_names = None

def add_track(self):
print("Adding a new track\n")
Expand Down Expand Up @@ -545,7 +546,7 @@ def export_all(
max_axis_2,
):
if label_layers:
export_label_layers(regions_directory, label_layers)
export_label_layers(regions_directory, label_layers, resolution)

if splines:
export_splines(
Expand Down
13 changes: 7 additions & 6 deletions brainreg_segment/tracks/IO.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ def save_track_layers(
tracks_directory.mkdir(parents=True, exist_ok=True)

for points_layer in points_layers:
save_single_track_layer(
points_layer,
save_single_track(
points_layer.data,
points_layer.name,
tracks_directory,
track_file_extension=track_file_extension,
)


def save_single_track_layer(
layer, output_directory, track_file_extension=".points",
def save_single_track(
points, name, output_directory, track_file_extension=".points",
):
output_filename = output_directory / (layer.name + track_file_extension)
points = pd.DataFrame(layer.data)
output_filename = output_directory / (name + track_file_extension)
points = pd.DataFrame(points)
points.to_hdf(output_filename, key="df", mode="w")


Expand Down
65 changes: 51 additions & 14 deletions brainreg_segment/tracks/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ def track_analysis(
spline_names = []

for track_layer in track_layers:
spline = spline_fit(
spline = run_track_analysis(
track_layer.data,
smoothing=spline_smoothing,
k=fit_degree,
n_points=spline_points,
track_layer.name,
tracks_directory,
atlas,
summarise_track=summarise_track,
spline_smoothing=spline_smoothing,
spline_points=spline_points,
fit_degree=fit_degree,
)

splines.append(spline)
if summarise_track:
summary_csv_file = tracks_directory / (track_layer.name + ".csv")
analyse_track_anatomy(atlas, spline, summary_csv_file)

viewer.add_points(
spline,
Expand All @@ -49,18 +51,55 @@ def track_analysis(
return splines, spline_names


def analyse_track_anatomy(atlas, spline, file_path, verbose=True):
def run_track_analysis(
points,
track_name,
tracks_directory,
atlas,
spline_smoothing=0.05,
spline_points=100,
fit_degree=3,
summarise_track=True,
):
"""
For each set of points, run a spline fit, and (if required) determine which
atlas region each part of the spline fit is within
:param points: 3D numpy array of points
:param track_name: Name of the set of points (used for saving results)
:param tracks_directory: Where to save the results to
:param atlas: brainglobe atlas class
:param spline_smoothing: spline fit smoothing factor
:param spline_points: How many points used to define the resulting
interpolated path
:param fit_degree: spline fit degree
:param summarise_track: If True, save a csv with the atlas region for
all parts of the spline fit
:return np.array: spline fit
"""

spline = spline_fit(
points,
smoothing=spline_smoothing,
k=fit_degree,
n_points=spline_points,
)
if summarise_track:
summary_csv_file = tracks_directory / (track_name + ".csv")
analyse_track_anatomy(atlas, spline, summary_csv_file)

return spline


def analyse_track_anatomy(atlas, spline, file_path):
"""
For a given spline, and brainrender scene, find the brain region that each
For a given spline, find the atlas region that each
"segment" is in, and save to csv.
:param scene: brainrender scene object
:param atlas: brainglobe atlas class
:param spline: numpy array defining the spline interpolation
:param file_path: path to save the results to
:param bool verbose: Whether to print the progress
"""
if verbose:
print("Determining the brain region for each segment of the spline")
spline_regions = []
for p in spline.tolist():
try:
Expand Down Expand Up @@ -94,6 +133,4 @@ def analyse_track_anatomy(atlas, spline, file_path, verbose=True):
},
ignore_index=True,
)
if verbose:
print(f"Saving results to: {file_path}")
df.to_csv(file_path, index=False)
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

requirements = [
"numpy",
"tables",
"scikit-image>=0.14.0,<0.17.0",
"pandas>=0.25.1,<=0.25.3",
"napari[pyqt5]",
Expand Down

0 comments on commit ea574bc

Please sign in to comment.