Skip to content
Snippets Groups Projects
Commit 11892cb6 authored by Harry Carey's avatar Harry Carey
Browse files

created function get_coordinates and added the ability to provide settings json

parent c63310dc
No related branches found
No related tags found
No related merge requests found
...@@ -2,9 +2,9 @@ import numpy as np ...@@ -2,9 +2,9 @@ import numpy as np
import pandas as pd import pandas as pd
from DeepSlice.coord_post_processing.spacing_and_indexing import number_sections from DeepSlice.coord_post_processing.spacing_and_indexing import number_sections
import json import json
from read_and_write import loadVisuAlignJson from .read_and_write import loadVisuAlignJson
from counting_and_load import labelPoints from .counting_and_load import labelPoints
from visualign_deformations import triangulate, transform_vec from .visualign_deformations import triangulate, transform_vec
from glob import glob from glob import glob
from tqdm import tqdm from tqdm import tqdm
import cv2 import cv2
...@@ -108,7 +108,7 @@ def SegmentationToAtlasSpace(slice, SegmentationPath, pixelID="auto", nonLinear= ...@@ -108,7 +108,7 @@ def SegmentationToAtlasSpace(slice, SegmentationPath, pixelID="auto", nonLinear=
triangulation = triangulate(RegWidth, RegHeight, slice["markers"]) triangulation = triangulate(RegWidth, RegHeight, slice["markers"])
newX, newY = transform_vec(triangulation, scaledX, scaledY) newX, newY = transform_vec(triangulation, scaledX, scaledY)
else: else:
print(f"no markers found for " + slice["filename"]) print(f"no markers found for {slice['filename']}, result for section will be linear")
newX, newY = scaledX, scaledY newX, newY = scaledX, scaledY
else: else:
newX, newY = scaledX, scaledY newX, newY = scaledX, scaledY
...@@ -230,7 +230,7 @@ def SegmentationToAtlasSpaceMultiThreaded( ...@@ -230,7 +230,7 @@ def SegmentationToAtlasSpaceMultiThreaded(
triangulation = triangulate(RegWidth, RegHeight, slice["markers"]) triangulation = triangulate(RegWidth, RegHeight, slice["markers"])
newX, newY = transform_vec(triangulation, scaledX, scaledY) newX, newY = transform_vec(triangulation, scaledX, scaledY)
else: else:
print(f"no markers found for " + slice["filename"]) print(f"no markers found for {slice['filename']}, result for section will be linear")
newX, newY = scaledX, scaledY newX, newY = scaledX, scaledY
else: else:
newX, newY = scaledX, scaledY newX, newY = scaledX, scaledY
......
from .metadata import metadata_loader from .metadata import metadata_loader
from .read_and_write import readAtlasVolume from .read_and_write import readAtlasVolume
from .coordinate_extraction import FolderToAtlasSpaceMultiThreaded
import json
class PyNutil: class PyNutil:
def __init__( def __init__(
self, self,
segmentation_folder, segmentation_folder=None,
json_file, alignment_json=None,
colour, colour=None,
atlas, volume_path=None,
settings_file=None,
) -> None: ) -> None:
self.config, self.metadata_path = metadata_loader.load_config() self.config, self.metadata_path = metadata_loader.load_config()
if atlas not in self.config["annotation_volumes"]: if settings_file is not None:
with open(settings_file, "r") as f:
settings = json.load(f)
try:
segmentation_folder = settings["segmentation_folder"]
alignment_json = settings["alignment_json"]
colour = settings["colour"]
volume_path = settings["volume_path"]
except KeyError as exc:
raise KeyError(
"settings file must contain segmentation_folder, alignment_json, colour, and volume_path"
) from exc
# check if any values are None
if None in [segmentation_folder, alignment_json, colour, volume_path]:
raise ValueError(
"segmentation_folder, alignment_json, colour, and volume_path must all be specified and not be None"
)
if volume_path not in self.config["annotation_volumes"]:
raise ValueError( raise ValueError(
f"Atlas {atlas} not found in config file, valid atlases are: \n{' , '.join(list(self.config['annotation_volumes'].keys()))}" f"Atlas {volume_path} not found in config file, valid atlases are: \n{' , '.join(list(self.config['annotation_volumes'].keys()))}"
) )
self.segmentation_folder = segmentation_folder self.segmentation_folder = segmentation_folder
self.json_file = json_file self.alignment_json = alignment_json
self.colour = colour self.colour = colour
self.atlas = atlas self.atlas = volume_path
# load the metadata json as well as the path to stored data files # load the metadata json as well as the path to stored data files
def build_quantifier(self): def build_quantifier(self):
...@@ -27,7 +47,21 @@ class PyNutil: ...@@ -27,7 +47,21 @@ class PyNutil:
current_atlas_path = self.config["annotation_volumes"][self.atlas]["volume"] current_atlas_path = self.config["annotation_volumes"][self.atlas]["volume"]
print("loading atlas volume") print("loading atlas volume")
self.atlas_volume = readAtlasVolume(atlas_root_path + current_atlas_path) self.atlas_volume = readAtlasVolume(atlas_root_path + current_atlas_path)
print("atlas volume loaded")
def get_coordinates(self): def get_coordinates(self, nonLinear=True, method="all"):
if not hasattr(self, "atlas_volume"): if not hasattr(self, "atlas_volume"):
raise ValueError("Please run build_quantifier before running get_coordinates") raise ValueError("Please run build_quantifier before running get_coordinates")
if method not in ["per_pixel", "per_object", "all"]:
raise ValueError(f"method {method} not recognised, valid methods are: per_pixel, per_object, or all")
print("extracting coordinates")
points = FolderToAtlasSpaceMultiThreaded(
self.segmentation_folder,
self.alignment_json,
pixelID=self.colour,
nonLinear=nonLinear
)
self.points = points
{ "volume_path": "../annotation_volumes/annotation_10_reoriented_2017.nrrd", { "volume_path": "allen2017",
"label_path": "../annotation_volumes/allen2017_colours.csv", "label_path": "../annotation_volumes/allen2017_colours.csv",
"segmentation_folder": "../test_data/ttA_2877_NOP_segmentations", "segmentation_folder": "test_data/ttA_2877_NOP_segmentations",
"alignment_json": "../test_data/ttA_2877_NOP_horizontal_final_2017.json", "alignment_json": "test_data/ttA_2877_NOP_horizontal_final_2017.json",
"nonlinear": true, "nonlinear": true,
"colour": [0, 0, 255], "colour": [0, 0, 255],
"points_json_path": "../outputs/test1_points.json", "points_json_path": "outputs/test1_points.json",
"counts_per_label_name": "../outputs/test1_counts_per_allenID_2017.csv" "counts_per_label_name": "outputs/test1_counts_per_allenID_2017.csv"
} }
\ No newline at end of file
{ "volume_path": "../annotation_volumes/annotation_10_reoriented_2017.nrrd", { "volume_path": "allen2017",
"label_path": "../annotation_volumes/allen2017_colours.csv", "label_path": "annotation_volumes/allen2017_colours.csv",
"segmentation_folder": "../test_data/oneSection15", "segmentation_folder": "test_data/oneSection15",
"alignment_json": "../test_data/C68_nonlinear_no_markers.json", "alignment_json": "test_data/C68_nonlinear_no_markers.json",
"nonlinear": true, "nonlinear": true,
"colour": [255, 0, 255], "colour": [255, 0, 255],
"points_json_path": "../outputs/test4_points.json", "points_json_path": "outputs/test4_points.json",
"counts_per_label_name": "../outputs/test4_counts_per_allen2017.csv" "counts_per_label_name": "outputs/test4_counts_per_allen2017.csv"
} }
\ No newline at end of file
from PyNutil import PyNutil from PyNutil import PyNutil
pnt = PyNutil( pnt = PyNutil(
'test', settings_file=r"test/test4_2017.json"
'test', )
[0,0,0],
"allen2017")
pnt.build_quantifier() pnt.build_quantifier()
pnt.get_coordinates() pnt.get_coordinates()
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment