Skip to content
This repository has been archived by the owner on Oct 6, 2021. It is now read-only.

Commit

Permalink
Support multiple regions (#26)
Browse files Browse the repository at this point in the history
* points to brainrender

* update testing

* refactor

* refactor

* badges

* notes

* initial inj finder commit (#11)

* formatting with black

* add .DS_store to gitignore

* typo

* update imports

* update imports

* deal with no obj path given

* error typo

* move utils too imlib

* remove amap imports

* optional logging to file

* tidy

* registration moved to imlib

* keep largest object

* reduce RAM usage

* remove unnecessary logging args

* adds lesion and track estimation and generic tools

* add missing dir support for cell export

* add missing dir support for cell export

* reduce mem requirements

* remove old code

* remove old code

* refactor scaling

* fix failing heatmap test

* update docs

* ensure output directory exists

* make pixel sizes mandatory

* make pixel sizes mandatory (#17)

* move functionality to imlib

* remove hard coded smoothing

* Refactor (#19)

* make pixel sizes mandatory

* move functionality to imlib

* remove hard coded smoothing

* update docs

* Injectionsite (#20)

* refactors code from SL to import from neuro and imlib only

* moves amap_vis to neuro and removes amap and cellfinder dependencies;

* format with black

* amap_vis

* bump

* remove cellfinder dependency

* cli entry point for fibre track segmentation

* refactor seg tools

* start manual seg

* remove hardocoding in manual region segmentation

* refactor paths

* bump

* update docs

* update docs

* update docs

* update docs

* support multiple objects

* color objects

* bump

* update reqs

Co-authored-by: Federico Claudi <[email protected]>
Co-authored-by: stephen <[email protected]>
  • Loading branch information
3 people authored Apr 20, 2020
1 parent c61036e commit 2e02dd2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 11 deletions.
44 changes: 41 additions & 3 deletions neuro/brain_render_tools.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import numpy as np
from pathlib import Path
from brainrender.Utils.image import reorient_image, marching_cubes_to_obj
from brainrender.scene import Scene
from skimage import measure

from imlib.general.pathlib import append_to_pathlib_stem

from neuro.atlas_tools.custom_atlas_structures import (
get_arbitrary_structure_mask_from_custom_atlas,
)
Expand Down Expand Up @@ -44,21 +48,55 @@ def volume_to_vector_array_to_obj_file(
voxel_size=10,
orientation="coronal",
step_size=1,
threshold=0,
deal_with_regions_separately=False,
):

oriented_binary = reorient_image(
image, invert_axes=invert_axes, orientation=orientation
)

#
if deal_with_regions_separately:
for label_id in np.unique(oriented_binary):
filename = append_to_pathlib_stem(
Path(output_path), "_" + str(label_id)
)
image = oriented_binary == label_id
extract_and_save_object(
image,
filename,
voxel_size=voxel_size,
threshold=threshold,
step_size=step_size,
)
else:
extract_and_save_object(
oriented_binary,
output_path,
voxel_size=voxel_size,
threshold=threshold,
step_size=step_size,
)


def extract_and_save_object(
image, output_file_name, voxel_size=10, threshold=0, step_size=1
):
verts, faces, normals, values = measure.marching_cubes_lewiner(
oriented_binary, 0, step_size=step_size
image, threshold, step_size=step_size
)
verts, faces = convert_obj_to_br(verts, faces, voxel_size=voxel_size)
marching_cubes_to_obj(
(verts, faces, normals, values), str(output_file_name)
)


def convert_obj_to_br(verts, faces, voxel_size=10):
if voxel_size is not 1:
verts = verts * voxel_size

faces = faces + 1
marching_cubes_to_obj((verts, faces, normals, values), str(output_path))
return verts, faces


def visualize_obj(obj_path, *args, color="lightcoral", **kwargs):
Expand Down
34 changes: 28 additions & 6 deletions neuro/segmentation/manual_region_segmentation/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

from pathlib import Path
import numpy as np
from glob import glob

from PySide2.QtWidgets import QApplication
from brainrender.scene import Scene
from imlib.general.system import delete_temp
from imlib.general.system import delete_temp, ensure_directory_exists
from imlib.plotting.colors import get_random_vtkplotter_color


from neuro.generic_neuro_tools import (
transform_image_to_standard_space,
Expand All @@ -27,8 +30,13 @@ def __init__(self, registration_output_folder, downsampled_image):
self.registration_output_folder = Path(registration_output_folder)
self.downsampled_image = self.join(downsampled_image)

self.regions_object_file = self.join("regions.obj")
self.regions_image_file = self.join("regions.nii")
self.regions_directory = self.join("segmented_regions")

self.regions_object_file_basename = (
self.regions_directory / "region.obj"
)

self.regions_image_file = self.regions_directory / "regions.nii"

self.tmp__inverse_transformed_image = self.join(
"image_standard_space.nii"
Expand All @@ -40,9 +48,14 @@ def __init__(self, registration_output_folder, downsampled_image):
"inverse_transform_error.txt"
)

self.prep()

def join(self, filename):
return self.registration_output_folder / filename

def prep(self):
ensure_directory_exists(self.regions_directory)


def run(
image,
Expand Down Expand Up @@ -88,11 +101,15 @@ def run(

@viewer.bind_key("Control-S")
def add_region(viewer):
print(f"\nSaving regions to: {paths.regions_object_file}")
print(f"\nSaving regions to: {paths.regions_directory}")
# return image back to original orientation (reoriented for napari)
data = np.swapaxes(labels_layer.data, 2, 0)

volume_to_vector_array_to_obj_file(data, paths.regions_object_file)
volume_to_vector_array_to_obj_file(
data,
paths.regions_object_file_basename,
deal_with_regions_separately=True,
)
if save_segmented_image:
save_brain(
data, paths.downsampled_image, paths.regions_image_file,
Expand All @@ -106,9 +123,14 @@ def add_region(viewer):
delete_temp(paths.registration_output_folder, paths)

if preview:

print("\nPreviewing in brainrender")
scene = Scene()
scene.add_from_file(str(paths.regions_object_file), c="coral")
obj_files = glob(str(paths.regions_directory) + "/*.obj")
for obj_file in obj_files:
act = scene.add_from_file(
obj_file, c=get_random_vtkplotter_color(), alpha=0.8
)
scene.render()


Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
"pandas<=0.25.3,>=0.25.1",
"napari",
"brainrender",
"imlib",
"imlib >= 0.0.18",
"brainio",
]


setup(
name="neuro",
version="0.0.8rc0",
version="0.0.8rc1",
description="Visualisation and analysis of brain imaging data",
install_requires=requirements,
extras_require={
Expand Down

0 comments on commit 2e02dd2

Please sign in to comment.