Skip to content
Snippets Groups Projects
Commit eedb4afa authored by Sharon Yates's avatar Sharon Yates
Browse files

Create PixelCountPerRegion function

parent add3d2c2
No related branches found
No related tags found
No related merge requests found
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
......@@ -270,3 +271,40 @@ def labelPoints(points, label_volume, scale_factor=1):
labels = label_volume[x,y,z]
return labels
# related to object_counting
# consider separating out writing to CSV in future
def PixelCountPerRegion(labelsDict, label_colours, output_csv):
"""Function for counting no. of pixels per region and writing to CSV based on
a dictionary with the region as the key and the points as the value, """
counted_labels, label_counts = np.unique(labelsDict, return_counts=True)
# which regions have pixels, and how many pixels are there per region
counts_per_label = list(zip(counted_labels,label_counts))
# create a list of unique regions and pixel counts per region
df_counts_per_label = pd.DataFrame(counts_per_label, columns=["allenID","pixel count"])
# create a pandas df with regions and pixel counts
df_label_colours =pd.read_csv(label_colours, sep=",")
# find colours corresponding to each region ID and add to the pandas dataframe
#look up name, r, g, b in df_allen_colours in df_counts_per_label based on "allenID"
new_rows = []
for index, row in df_counts_per_label.iterrows():
mask = df_label_colours["allenID"] == row["allenID"]
current_region_row = df_label_colours[mask]
current_region_name = current_region_row["name"].values
current_region_red = current_region_row["r"].values
current_region_green = current_region_row["g"].values
current_region_blue = current_region_row["b"].values
row["name"] = current_region_name[0]
row["r"] = current_region_red[0]
row["g"] = current_region_green[0]
row["b"] = current_region_blue[0]
new_rows.append(row)
df_counts_per_label_name = pd.DataFrame(new_rows)
df_counts_per_label_name.to_csv(output_csv, sep=";", na_rep='', index= False)
......@@ -10,12 +10,12 @@ import json
from datetime import datetime
#import json into "input" variable, use to define input parameters
with open('../test/test1.json', 'r') as f:
with open('../test/test2.json', 'r') as f:
input = json.load(f)
#print(input)
#import our function for converting a folder of segmentations to points
from coordinate_extraction import FolderToAtlasSpace, labelPoints, WritePointsToMeshview, FolderToAtlasSpaceMultiThreaded
from coordinate_extraction import FolderToAtlasSpace, labelPoints, WritePointsToMeshview, FolderToAtlasSpaceMultiThreaded, PixelCountPerRegion
#from read_and_write import WritePointsToMeshview
startTime = datetime.now()
......@@ -37,58 +37,13 @@ labels = labelPoints(points, data, scale_factor=2.5)
#save points to a meshview json
WritePointsToMeshview(points, labels, input["points_json_path"], label_df)
#SY Task:
# function for counting no. of objects per region
# Make a pandas dataframe
# Column 1: counted_labels
# Column 2: label_counts
# Column 3: region_name (look up by reading Allen2022_colours.csv, look up name and RGB)
# Save dataframe in output as CSV
# next task is to create functions from this.
counted_labels, label_counts = np.unique(labels, return_counts=True)
counts_per_label = list(zip(counted_labels,label_counts))
df_counts_per_label = pd.DataFrame(counts_per_label, columns=["allenID","pixel count"])
df_allen_colours =pd.read_csv(input["allen_colours"], sep=",")
df_allen_colours
#look up name, r, g, b in df_allen_colours in df_counts_per_label based on "allenID"
new_rows = []
for index, row in df_counts_per_label.iterrows():
mask = df_allen_colours["allenID"] == row["allenID"]
current_region_row = df_allen_colours[mask]
current_region_name = current_region_row["name"].values
current_region_red = current_region_row["r"].values
current_region_green = current_region_row["g"].values
current_region_blue = current_region_row["b"].values
row["name"] = current_region_name[0]
row["r"] = current_region_red[0]
row["g"] = current_region_green[0]
row["b"] = current_region_blue[0]
new_rows.append(row)
df_counts_per_label_name = pd.DataFrame(new_rows)
df_counts_per_label_name
# write to csv file
df_counts_per_label_name.to_csv(input["counts_per_label_name"], sep=";", na_rep='', index= False)
#r = df_allen_colours["r"]
#g = df_allen_colours["g"]
#b = df_allen_colours["b"]
#region_name = df_allen_colours["name"]
PixelCountPerRegion(labels, input["allen_colours"], input["counts_per_label_name"])
#while we havent added it here it would be good to next quantify the number of cells for each label.
time_taken = datetime.now() - startTime
print(f"time taken was: {time_taken}")
print(f"overall time taken was: {time_taken}")
#get centroids and areas returns a list of objects and the center coordinate.
......
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