diff --git a/PyNutil/coordinate_extraction.py b/PyNutil/coordinate_extraction.py index 4f6c2b756a622c68786a8d6bd8894b0e6ac5baf4..ff51c5e9ab3e91136ba30b90a1a7547c07f13077 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 71fd2b39100531cddb863663d7a294dfc50c57b1..f87b63395f8578c63fcc9b533b43d8ff2993e0e1 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 c8f58ec3c54cc8e5649e636deb7c52b2ae2cbd58..6ce2ef414a40e9a0f0d6c1f34e30d93551379ee7 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 9589000abca943cf75c693aee4958d46e26c42e1..bdb39595a190b78c31207f8efd7b4bdfbd56d29d 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 2c6c0e3ba52a7ddcae2fbc45374bee9724d6d036..92ee5f3e95348aa7346527f631f2a3796c332a1e 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()