From 11892cb65c3ecfcdc35b5013803f9611c7f21380 Mon Sep 17 00:00:00 2001 From: Harry Carey <harry.carey95@gmail.com> Date: Wed, 24 May 2023 16:28:53 +0200 Subject: [PATCH] created function get_coordinates and added the ability to provide settings json --- PyNutil/coordinate_extraction.py | 10 +++--- PyNutil/main.py | 54 ++++++++++++++++++++++++++------ test/test1.json | 10 +++--- test/test4_2017.json | 12 +++---- testOOP.py | 11 ++----- 5 files changed, 62 insertions(+), 35 deletions(-) diff --git a/PyNutil/coordinate_extraction.py b/PyNutil/coordinate_extraction.py index 4f6c2b7..ff51c5e 100644 --- a/PyNutil/coordinate_extraction.py +++ b/PyNutil/coordinate_extraction.py @@ -2,9 +2,9 @@ import numpy as np import pandas as pd from DeepSlice.coord_post_processing.spacing_and_indexing import number_sections import json -from read_and_write import loadVisuAlignJson -from counting_and_load import labelPoints -from visualign_deformations import triangulate, transform_vec +from .read_and_write import loadVisuAlignJson +from .counting_and_load import labelPoints +from .visualign_deformations import triangulate, transform_vec from glob import glob from tqdm import tqdm import cv2 @@ -108,7 +108,7 @@ def SegmentationToAtlasSpace(slice, SegmentationPath, pixelID="auto", nonLinear= triangulation = triangulate(RegWidth, RegHeight, slice["markers"]) newX, newY = transform_vec(triangulation, scaledX, scaledY) 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 else: newX, newY = scaledX, scaledY @@ -230,7 +230,7 @@ def SegmentationToAtlasSpaceMultiThreaded( triangulation = triangulate(RegWidth, RegHeight, slice["markers"]) newX, newY = transform_vec(triangulation, scaledX, scaledY) 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 else: newX, newY = scaledX, scaledY diff --git a/PyNutil/main.py b/PyNutil/main.py index 71fd2b3..f87b633 100644 --- a/PyNutil/main.py +++ b/PyNutil/main.py @@ -1,24 +1,44 @@ from .metadata import metadata_loader from .read_and_write import readAtlasVolume - +from .coordinate_extraction import FolderToAtlasSpaceMultiThreaded +import json class PyNutil: def __init__( self, - segmentation_folder, - json_file, - colour, - atlas, + segmentation_folder=None, + alignment_json=None, + colour=None, + volume_path=None, + settings_file=None, ) -> None: 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( - 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.json_file = json_file + self.alignment_json = alignment_json self.colour = colour - self.atlas = atlas + self.atlas = volume_path # load the metadata json as well as the path to stored data files def build_quantifier(self): @@ -27,7 +47,21 @@ class PyNutil: current_atlas_path = self.config["annotation_volumes"][self.atlas]["volume"] print("loading atlas volume") 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"): 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 + + + diff --git a/test/test1.json b/test/test1.json index c8f58ec..6ce2ef4 100644 --- a/test/test1.json +++ b/test/test1.json @@ -1,9 +1,9 @@ -{ "volume_path": "../annotation_volumes/annotation_10_reoriented_2017.nrrd", +{ "volume_path": "allen2017", "label_path": "../annotation_volumes/allen2017_colours.csv", - "segmentation_folder": "../test_data/ttA_2877_NOP_segmentations", - "alignment_json": "../test_data/ttA_2877_NOP_horizontal_final_2017.json", + "segmentation_folder": "test_data/ttA_2877_NOP_segmentations", + "alignment_json": "test_data/ttA_2877_NOP_horizontal_final_2017.json", "nonlinear": true, "colour": [0, 0, 255], - "points_json_path": "../outputs/test1_points.json", - "counts_per_label_name": "../outputs/test1_counts_per_allenID_2017.csv" + "points_json_path": "outputs/test1_points.json", + "counts_per_label_name": "outputs/test1_counts_per_allenID_2017.csv" } \ No newline at end of file diff --git a/test/test4_2017.json b/test/test4_2017.json index 9589000..bdb3959 100644 --- a/test/test4_2017.json +++ b/test/test4_2017.json @@ -1,9 +1,9 @@ -{ "volume_path": "../annotation_volumes/annotation_10_reoriented_2017.nrrd", - "label_path": "../annotation_volumes/allen2017_colours.csv", - "segmentation_folder": "../test_data/oneSection15", - "alignment_json": "../test_data/C68_nonlinear_no_markers.json", +{ "volume_path": "allen2017", + "label_path": "annotation_volumes/allen2017_colours.csv", + "segmentation_folder": "test_data/oneSection15", + "alignment_json": "test_data/C68_nonlinear_no_markers.json", "nonlinear": true, "colour": [255, 0, 255], - "points_json_path": "../outputs/test4_points.json", - "counts_per_label_name": "../outputs/test4_counts_per_allen2017.csv" + "points_json_path": "outputs/test4_points.json", + "counts_per_label_name": "outputs/test4_counts_per_allen2017.csv" } \ No newline at end of file diff --git a/testOOP.py b/testOOP.py index 2c6c0e3..92ee5f3 100644 --- a/testOOP.py +++ b/testOOP.py @@ -1,15 +1,8 @@ from PyNutil import PyNutil - - - - pnt = PyNutil( - 'test', - 'test', - [0,0,0], - "allen2017") - + settings_file=r"test/test4_2017.json" + ) pnt.build_quantifier() pnt.get_coordinates() -- GitLab