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

This commit refactors the PyNutil class to make it more modular and easier to...

This commit refactors the PyNutil class to make it more modular and easier to read and understand. The load_atlas_data method has been extracted to a separate function, and the coordinate extraction, pixel labeling and counting, and writing of points to Meshview have been extracted to separate methods. These changes make the code more modular and easier to maintain.
parent 300429c4
No related branches found
No related tags found
No related merge requests found
......@@ -30,4 +30,5 @@ quantifier.get_objects()
quantifier.get_loads()
quantifier.save_segmentation_atlas_overlays()
......@@ -43,20 +43,23 @@ class PyNutil:
self.alignment_json = alignment_json
self.colour = colour
self.atlas = volume_path
self.atlas_volume, self.atlas_labels = self.load_atlas_data()
def load_atlas_data(self):
# load the metadata json as well as the path to stored data files
# this could potentially be moved into init
atlas_root_path = self.config["annotation_volume_directory"]
current_atlas_path = self.config["annotation_volumes"][self.atlas]["volume"]
print("loading atlas volume")
startTime = datetime.now()
self.atlas_volume = readAtlasVolume(atlas_root_path + current_atlas_path)
atlas_volume = readAtlasVolume(atlas_root_path + current_atlas_path)
time_taken = datetime.now() - startTime
print(f"atlas volume loaded in: {time_taken}")
atlas_label_path = self.config["annotation_volumes"][self.atlas]["labels"]
print("loading atlas labels")
self.atlas_labels = pd.read_csv(atlas_root_path + atlas_label_path)
atlas_labels = pd.read_csv(atlas_root_path + atlas_label_path)
print("atlas labels loaded ✅")
return atlas_volume, atlas_labels
def get_coordinates(self, nonLinear=True, method="all"):
if not hasattr(self, "atlas_volume"):
......@@ -68,14 +71,17 @@ class PyNutil:
f"method {method} not recognised, valid methods are: per_pixel, per_object, or all"
)
print("extracting coordinates")
pixel_points = FolderToAtlasSpace(
pixel_points = self.extract_coordinates(nonLinear, method)
self.pixel_points = pixel_points
def extract_coordinates(self, nonLinear, method):
return FolderToAtlasSpace(
self.segmentation_folder,
self.alignment_json,
pixelID=self.colour,
nonLinear=nonLinear,
method=method,
)
self.pixel_points = pixel_points
def quantify_coordinates(self):
if not hasattr(self, "pixel_points"):
......@@ -83,14 +89,18 @@ class PyNutil:
"Please run get_coordinates before running quantify_coordinates"
)
print("quantifying coordinates")
labeled_points = labelPoints(
self.pixel_points, self.atlas_volume, scale_factor=2.5
)
self.label_df = PixelCountPerRegion(labeled_points, self.atlas_labels)
labeled_points = self.label_points()
self.label_df = self.count_pixels_per_region(labeled_points)
self.labeled_points = labeled_points
print("quantification complete ✅")
def label_points(self):
return labelPoints(self.pixel_points, self.atlas_volume, scale_factor=2.5)
def count_pixels_per_region(self, labeled_points):
return PixelCountPerRegion(labeled_points, self.atlas_labels)
def save_analysis(self, output_folder):
if not hasattr(self, "pixel_points"):
raise ValueError("Please run get_coordinates before running save_analysis")
......@@ -106,10 +116,13 @@ class PyNutil:
)
else:
WritePointsToMeshview(
self.pixel_points,
self.labeled_points,
output_folder + "/pixels_meshview.json",
self.atlas_labels,
)
self.write_points_to_meshview(output_folder)
print("analysis saved ✅")
def write_points_to_meshview(self, output_folder):
WritePointsToMeshview(
self.pixel_points,
self.labeled_points,
output_folder + "/pixels_meshview.json",
self.atlas_labels,
)
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