Skip to content
Snippets Groups Projects
Commit d59df514 authored by polarbean's avatar polarbean
Browse files

fixed issue where every point was labeled clear

parent c2a6b684
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,7 @@ def get_centroids_and_area(segmentation, pixel_cut_off=0): ...@@ -20,6 +20,7 @@ def get_centroids_and_area(segmentation, pixel_cut_off=0):
labels = measure.label(segmentation) labels = measure.label(segmentation)
# This finds all the objects in the image # This finds all the objects in the image
labels_info = measure.regionprops(labels) labels_info = measure.regionprops(labels)
# Remove objects that are less than pixel_cut_off # Remove objects that are less than pixel_cut_off
labels_info = [label for label in labels_info if label.area > pixel_cut_off] labels_info = [label for label in labels_info if label.area > pixel_cut_off]
# Get the centre points of the objects # Get the centre points of the objects
...@@ -84,7 +85,12 @@ def transform_to_atlas_space(anchoring, y, x, reg_height, reg_width): ...@@ -84,7 +85,12 @@ def transform_to_atlas_space(anchoring, y, x, reg_height, reg_width):
# related to coordinate extraction # related to coordinate extraction
# This function returns an array of points # This function returns an array of points
def folder_to_atlas_space( def folder_to_atlas_space(
folder, quint_alignment, pixel_id=[0, 0, 0], non_linear=True, method="all", object_cutoff=0 folder,
quint_alignment,
pixel_id=[0, 0, 0],
non_linear=True,
method="all",
object_cutoff=0,
): ):
"""Apply Segmentation to atlas space to all segmentations in a folder.""" """Apply Segmentation to atlas space to all segmentations in a folder."""
...@@ -118,7 +124,7 @@ def folder_to_atlas_space( ...@@ -118,7 +124,7 @@ def folder_to_atlas_space(
centroids_list, centroids_list,
index, index,
method, method,
object_cutoff object_cutoff,
), ),
) )
threads.append(x) threads.append(x)
...@@ -131,10 +137,23 @@ def folder_to_atlas_space( ...@@ -131,10 +137,23 @@ def folder_to_atlas_space(
points_len = [len(points) for points in points_list] points_len = [len(points) for points in points_list]
centroids_len = [len(centroids) for centroids in centroids_list] centroids_len = [len(centroids) for centroids in centroids_list]
points = [item for sublist in points_list for item in sublist] points = np.concatenate(points_list)
centroids_list = [item for sublist in centroids_list for item in sublist] print("points shape: ",points.shape)
centroids = np.concatenate(centroids_list)
return np.array(points), np.array(centroids_list), points_len, centroids_len print("centroids shape: ",centroids.shape)
print("Number of points: ", len(points))
print("Number of centroids: ", len(centroids))
print("points_len: ", points_len)
print("points: ", points)
print("points first: ", points[0])
return (
np.array(points),
np.array(centroids),
points_len,
centroids_len,
segmentations,
)
def segmentation_to_atlas_space( def segmentation_to_atlas_space(
...@@ -146,7 +165,7 @@ def segmentation_to_atlas_space( ...@@ -146,7 +165,7 @@ def segmentation_to_atlas_space(
centroids_list=None, centroids_list=None,
index=None, index=None,
method="per_pixel", method="per_pixel",
object_cutoff=0 object_cutoff=0,
): ):
"""Combines many functions to convert a segmentation to atlas space. It takes care """Combines many functions to convert a segmentation to atlas space. It takes care
of deformations.""" of deformations."""
...@@ -174,9 +193,9 @@ def segmentation_to_atlas_space( ...@@ -174,9 +193,9 @@ def segmentation_to_atlas_space(
centroids, scaled_centroidsX, scaled_centroidsY = get_centroids( centroids, scaled_centroidsX, scaled_centroidsY = get_centroids(
segmentation, pixel_id, y_scale, x_scale, object_cutoff segmentation, pixel_id, y_scale, x_scale, object_cutoff
) )
print("Number of objects: ", len(scaled_centroidsY))
if method in ["per_pixel", "all"]: if method in ["per_pixel", "all"]:
scaled_y, scaled_x = get_scaled_pixels(segmentation, pixel_id, y_scale, x_scale) scaled_y, scaled_x = get_scaled_pixels(segmentation, pixel_id, y_scale, x_scale)
print(f"{segmentation_path} \nNumber of objects: {len(scaled_centroidsY)}\nNumber of pixels: {len(scaled_y)}")
if non_linear: if non_linear:
if "markers" in slice: if "markers" in slice:
...@@ -210,6 +229,7 @@ def segmentation_to_atlas_space( ...@@ -210,6 +229,7 @@ def segmentation_to_atlas_space(
centroids = transform_to_atlas_space( centroids = transform_to_atlas_space(
slice["anchoring"], centroids_new_y, centroids_new_x, reg_height, reg_width slice["anchoring"], centroids_new_y, centroids_new_x, reg_height, reg_width
) )
print(f"Finished and points len is: {len(points)} and centroids len is: {len(centroids)}")
points_list[index] = np.array(points) points_list[index] = np.array(points)
centroids_list[index] = np.array(centroids) centroids_list[index] = np.array(centroids)
...@@ -217,9 +237,11 @@ def segmentation_to_atlas_space( ...@@ -217,9 +237,11 @@ def segmentation_to_atlas_space(
def get_centroids(segmentation, pixel_id, y_scale, x_scale, object_cutoff=0): def get_centroids(segmentation, pixel_id, y_scale, x_scale, object_cutoff=0):
binary_seg = segmentation == pixel_id binary_seg = segmentation == pixel_id
binary_seg = np.all(binary_seg, axis=2) binary_seg = np.all(binary_seg, axis=2)
centroids, area, coords = get_centroids_and_area(binary_seg, pixel_cut_off=object_cutoff) centroids, area, coords = get_centroids_and_area(
centroidsY = centroids[:, 1] binary_seg, pixel_cut_off=object_cutoff
centroidsX = centroids[:, 0] )
centroidsX = centroids[:, 1]
centroidsY = centroids[:, 0]
scaled_centroidsY, scaled_centroidsX = scale_positions( scaled_centroidsY, scaled_centroidsX = scale_positions(
centroidsY, centroidsX, y_scale, x_scale centroidsY, centroidsX, y_scale, x_scale
) )
......
...@@ -17,8 +17,23 @@ def label_points(points, label_volume, scale_factor=1): ...@@ -17,8 +17,23 @@ def label_points(points, label_volume, scale_factor=1):
x = points[:, 0] x = points[:, 0]
y = points[:, 1] y = points[:, 1]
z = points[:, 2] z = points[:, 2]
print(x[0], y[0], z[0])
#make sure the points are within the volume
x[x < 0] = 0
y[y < 0] = 0
z[z < 0] = 0
mask = (x > label_volume.shape[0] - 1) | (y > label_volume.shape[1] - 1) | (z > label_volume.shape[2] - 1)
x[mask] = 0
y[mask] = 0
z[mask] = 0
print('total mask: ')
print(np.sum(mask))
# Get the label value for each point # Get the label value for each point
print(x[0], y[0], z[0])
labels = label_volume[x, y, z] labels = label_volume[x, y, z]
print('label sum: ')
print(np.sum(labels))
return labels return labels
......
...@@ -123,12 +123,12 @@ class PyNutil: ...@@ -123,12 +123,12 @@ 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")
start_time = datetime.now() start_time = datetime.now()
atlas_volume = read_atlas_volume(atlas_root_path + current_atlas_path) atlas_volume = read_atlas_volume(f"{atlas_root_path}{current_atlas_path}")
time_taken = datetime.now() - start_time time_taken = datetime.now() - start_time
print(f"atlas volume loaded in: {time_taken}") print(f"atlas volume loaded in: {time_taken}")
atlas_label_path = self.config["annotation_volumes"][self.atlas]["labels"] atlas_label_path = self.config["annotation_volumes"][self.atlas]["labels"]
print("loading atlas labels") print("loading atlas labels")
atlas_labels = pd.read_csv(atlas_root_path + atlas_label_path) atlas_labels = pd.read_csv(f"{atlas_root_path}{atlas_label_path}")
print("atlas labels loaded ✅") print("atlas labels loaded ✅")
return atlas_volume, atlas_labels return atlas_volume, atlas_labels
...@@ -160,7 +160,13 @@ class PyNutil: ...@@ -160,7 +160,13 @@ class PyNutil:
f"method {method} not recognised, valid methods are: per_pixel, per_object, or all" f"method {method} not recognised, valid methods are: per_pixel, per_object, or all"
) )
print("extracting coordinates") print("extracting coordinates")
pixel_points, centroids, points_len, centroids_len = folder_to_atlas_space( (
pixel_points,
centroids,
points_len,
centroids_len,
segmentation_filenames,
) = folder_to_atlas_space(
self.segmentation_folder, self.segmentation_folder,
self.alignment_json, self.alignment_json,
pixel_id=self.colour, pixel_id=self.colour,
...@@ -174,6 +180,7 @@ class PyNutil: ...@@ -174,6 +180,7 @@ class PyNutil:
##This will be used to split the data up later into per section files ##This will be used to split the data up later into per section files
self.points_len = points_len self.points_len = points_len
self.centroids_len = centroids_len self.centroids_len = centroids_len
self.segmentation_filenames = segmentation_filenames
def quantify_coordinates(self): def quantify_coordinates(self):
"""Quantifies the pixel coordinates by region. """Quantifies the pixel coordinates by region.
...@@ -203,8 +210,24 @@ class PyNutil: ...@@ -203,8 +210,24 @@ class PyNutil:
self.label_df = pixel_count_per_region( self.label_df = pixel_count_per_region(
labeled_points, labeled_points_centroids, self.atlas_labels labeled_points, labeled_points_centroids, self.atlas_labels
) )
prev_pl = 0
per_section_df = []
current_centroids = None
current_points = None
for pl in self.points_len:
if hasattr(self, "centroids"):
current_centroids = labeled_points_centroids[prev_pl : prev_pl + pl]
if hasattr(self, "pixel_points"):
current_points = labeled_points[prev_pl : prev_pl + pl]
current_df = pixel_count_per_region(
current_points, current_centroids, self.atlas_labels
)
per_section_df.append(current_df)
prev_pl += pl
self.labeled_points = labeled_points self.labeled_points = labeled_points
self.labeled_points_centroids = labeled_points_centroids self.labeled_points_centroids = labeled_points_centroids
self.per_section_df = per_section_df
print("quantification complete ✅") print("quantification complete ✅")
...@@ -226,30 +249,66 @@ class PyNutil: ...@@ -226,30 +249,66 @@ class PyNutil:
if not os.path.exists(output_folder): if not os.path.exists(output_folder):
os.makedirs(output_folder) os.makedirs(output_folder)
if not hasattr(self, "pixel_points"):
raise ValueError("Please run get_coordinates before running save_analysis")
if not hasattr(self, "label_df"): if not hasattr(self, "label_df"):
print("no quantification found so we will only save the coordinates") print("no quantification found so we will only save the coordinates")
print( print(
"if you want to save the quantification please run quantify_coordinates" "if you want to save the quantification please run quantify_coordinates"
) )
else:
self.label_df.to_csv(
f"{output_folder}/counts.csv", sep=";", na_rep="", index=False
)
if not os.path.exists(f"{output_folder}/per_section_meshview"):
os.makedirs(f"{output_folder}/per_section_meshview")
if not os.path.exists(f"{output_folder}/per_section_reports"):
os.makedirs(f"{output_folder}/per_section_reports")
self.label_df.to_csv( prev_pl = 0
output_folder + "/counts.csv", sep=";", na_rep="", index=False prev_cl = 0
)
for pl, cl, fn, df in zip(
self.points_len,
write_points_to_meshview( self.centroids_len,
self.pixel_points, self.segmentation_filenames,
self.labeled_points, self.per_section_df,
output_folder + "/pixels_meshview.json", ):
self.atlas_labels,
) split_fn = fn.split("/")[-1].split(".")[0]
write_points_to_meshview( df.to_csv(
self.centroids, f"{output_folder}/per_section_reports/{split_fn}.csv",
self.labeled_points_centroids, sep=";",
output_folder + "/objects_meshview.json", na_rep="",
self.atlas_labels, index=False,
) )
if hasattr(self, "pixel_points"):
write_points_to_meshview(
self.pixel_points[prev_pl : pl + prev_pl],
self.labeled_points[prev_pl : pl + prev_pl],
f"{output_folder}/per_section_meshview/{split_fn}_pixels.json",
self.atlas_labels,
)
if hasattr(self, "centroids"):
write_points_to_meshview(
self.centroids[prev_cl : cl + prev_cl],
self.labeled_points_centroids[prev_cl : cl + prev_cl],
f"{output_folder}/per_section_meshview/{split_fn}_centroids.json",
self.atlas_labels,
)
prev_cl += cl
prev_pl += pl
if hasattr(self, "pixel_points"):
write_points_to_meshview(
self.pixel_points,
self.labeled_points,
f"{output_folder}/pixels_meshview.json",
self.atlas_labels,
)
if hasattr(self, "centroids"):
write_points_to_meshview(
self.centroids,
self.labeled_points_centroids,
f"{output_folder}/objects_meshview.json",
self.atlas_labels,
)
print("analysis saved ✅") print("analysis saved ✅")
{ "volume_path": "allen2017",
"label_path": "annotation_volumes/allen2017_colours.csv",
"segmentation_folder": "test_data/ext-d000033_PVMouseExtraction_pub-Nutil_Quantifier_analysis-81264-Input_dir",
"alignment_json": "test_data/PVMouse_81264_nonlin.json",
"nonlinear": true,
"colour": [255, 0, 0]
}
\ No newline at end of file
{ "volume_path": "allen2017", { "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,
......
{ "volume_path": "../annotation_volumes/annotation_10_reoriented.nrrd", { "volume_path": "allen2017",
"label_path": "../annotation_volumes/allen2022_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.json", "alignment_json": "test_data/C68_nonlinear.json",
"nonlinear": true, "nonlinear": true,
"colour": [255, 0, 255], "colour": [255, 0, 255],
"points_json_path": "../outputs/test3_points.json", "points_json_path": "outputs/test3_points.json",
"counts_per_label_name": "../outputs/test3_counts_per_idx.csv" "counts_per_label_name": "outputs/test3_counts_per_idx.csv"
} }
\ No newline at end of file
from PyNutil import PyNutil from PyNutil import PyNutil
pnt = PyNutil(settings_file=r"test/test4_2017.json") pnt = PyNutil(settings_file=r"test/PVMouse_81264_test.json")
# pnt = PyNutil(settings_file=r"test/test3.json")
# pnt.build_quantifier() # pnt.build_quantifier()
pnt.get_coordinates(object_cutoff=0) pnt.get_coordinates(object_cutoff=0)
......
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