From 4464958b35c41d4b033df5a7fa993ab4d6a019f4 Mon Sep 17 00:00:00 2001 From: Harry Carey <harry.carey95@gmail.com> Date: Thu, 25 May 2023 16:40:58 +0200 Subject: [PATCH] removed redundant functions introduced by copilot --- PyNutil/counting_and_load.py | 2 +- PyNutil/main.py | 143 ++++++++++++++++++++++++++++++----- PyNutil/read_and_write.py | 2 +- 3 files changed, 124 insertions(+), 23 deletions(-) diff --git a/PyNutil/counting_and_load.py b/PyNutil/counting_and_load.py index b31daa8..efb8017 100644 --- a/PyNutil/counting_and_load.py +++ b/PyNutil/counting_and_load.py @@ -92,4 +92,4 @@ def flat_to_array(flat_file): with open(base+".flat","rb") as f: b,w,h=struct.unpack(">BII",f.read(9)) data=struct.unpack(">"+("xBH"[b]*(w*h)),f.read(b*w*h)) -""" \ No newline at end of file +""" diff --git a/PyNutil/main.py b/PyNutil/main.py index b5a8a82..af5a7e8 100644 --- a/PyNutil/main.py +++ b/PyNutil/main.py @@ -8,6 +8,68 @@ from datetime import datetime class PyNutil: + """A utility class for working with brain atlases and segmentation data. + + Parameters + ---------- + segmentation_folder : str + The path to the folder containing the segmentation data. + alignment_json : str + The path to the alignment JSON file. + colour : int + The colour of the segmentation data to extract. + volume_path : str + The name of the atlas volume to use. + settings_file : str, optional + The path to a JSON file containing the above parameters. + + Raises + ------ + ValueError + If any of the required parameters are None. + + Attributes + ---------- + segmentation_folder : str + The path to the folder containing the segmentation data. + alignment_json : str + The path to the alignment JSON file. + colour : int + The colour of the segmentation data to extract. + atlas : str + The name of the atlas volume being used. + atlas_volume : numpy.ndarray + The 3D array representing the atlas volume. + atlas_labels : pandas.DataFrame + A DataFrame containing the labels for the atlas volume. + pixel_points : numpy.ndarray + An array of pixel coordinates extracted from the segmentation data. + labeled_points : numpy.ndarray + An array of labeled pixel coordinates. + label_df : pandas.DataFrame + A DataFrame containing the pixel counts per region. + + Methods + ------- + load_atlas_data() + Loads the atlas volume and labels from disk. + get_coordinates(non_linear=True, method='all') + Extracts pixel coordinates from the segmentation data. + extract_coordinates(non_linear, method) + Extracts pixel coordinates from the segmentation data but is only used internally. + quantify_coordinates() + Quantifies the pixel coordinates by region. + label_points() + Labels the pixel coordinates by region but is only used internally. + count_pixels_per_region(labeled_points) + Counts the number of pixels per region but is only used internally. + save_analysis(output_folder) + Saves the pixel coordinates and pixel counts to disk. + write_points_to_meshview(output_folder) + Writes the pixel coordinates and labels to a JSON file for visualization but is only used internally. + + """ + def __init__( self, segmentation_folder=None, @@ -46,6 +108,14 @@ class PyNutil: self.atlas_volume, self.atlas_labels = self.load_atlas_data() def load_atlas_data(self): + """Loads the atlas volume and labels from disk. + + Returns + ------- + tuple + A tuple containing the atlas volume as a numpy.ndarray and the atlas labels as a pandas.DataFrame. + + """ # 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"] @@ -62,6 +132,22 @@ class PyNutil: return atlas_volume, atlas_labels def get_coordinates(self, non_linear=True, method="all"): + """Extracts pixel coordinates from the segmentation data. + + Parameters + ---------- + non_linear : bool, optional + Whether to use non-linear registration. Default is True. + method : str, optional + The method to use for extracting coordinates. Valid options are 'per_pixel', 'per_object', or 'all'. + Default is 'all'. + + Raises + ------ + ValueError + If the specified method is not recognized. + + """ if not hasattr(self, "atlas_volume"): raise ValueError( "Please run build_quantifier before running get_coordinates" @@ -71,37 +157,55 @@ class PyNutil: f"method {method} not recognised, valid methods are: per_pixel, per_object, or all" ) print("extracting coordinates") - pixel_points = self.extract_coordinates(non_linear, method) - self.pixel_points = pixel_points - - def extract_coordinates(self, non_linear, method): - return folder_to_atlas_space( + pixel_points = folder_to_atlas_space( self.segmentation_folder, self.alignment_json, pixel_id=self.colour, non_linear=non_linear, method=method, ) + self.pixel_points = pixel_points + + def quantify_coordinates(self): + """Quantifies the pixel coordinates by region. + + Raises + ------ + ValueError + If the pixel coordinates have not been extracted. + + """ if not hasattr(self, "pixel_points"): raise ValueError( "Please run get_coordinates before running quantify_coordinates" ) print("quantifying coordinates") - labeled_points = self.label_points() - self.label_df = self.count_pixels_per_region(labeled_points) + labeled_points = label_points(self.pixel_points, self.atlas_volume, scale_factor=1) + + self.label_df = pixel_count_per_region(labeled_points, self.atlas_labels) self.labeled_points = labeled_points print("quantification complete ✅") - def label_points(self): - return label_points(self.pixel_points, self.atlas_volume, scale_factor=1) - def count_pixels_per_region(self, labeled_points): - return pixel_count_per_region(labeled_points, self.atlas_labels) def save_analysis(self, output_folder): + """Saves the pixel coordinates and pixel counts to different files in the specified + output folder. + + Parameters + ---------- + output_folder : str + The path to the output folder. + + Raises + ------ + ValueError + If the pixel coordinates have not been extracted. + + """ if not hasattr(self, "pixel_points"): raise ValueError("Please run get_coordinates before running save_analysis") @@ -116,13 +220,10 @@ class PyNutil: ) else: - self.write_points_to_meshview(output_folder) - print("analysis saved ✅") - - def write_points_to_meshview(self, output_folder): - write_points_to_meshview( - self.pixel_points, - self.labeled_points, - output_folder + "/pixels_meshview.json", - self.atlas_labels, - ) \ No newline at end of file + write_points_to_meshview( + self.pixel_points, + self.labeled_points, + output_folder + "/pixels_meshview.json", + self.atlas_labels, + ) + print("analysis saved ✅") \ No newline at end of file diff --git a/PyNutil/read_and_write.py b/PyNutil/read_and_write.py index 8de8ce0..a662371 100644 --- a/PyNutil/read_and_write.py +++ b/PyNutil/read_and_write.py @@ -112,4 +112,4 @@ def files_in_directory(directory): def read_atlas_volume(atlas_volume_path): data, header = nrrd.read(atlas_volume_path) - return data \ No newline at end of file + return data -- GitLab