diff --git a/PyNutil/coordinate_extraction.py b/PyNutil/coordinate_extraction.py index 1edb063f6406a5154362ce6fbec85da9edcb1974..270fc37106027a6bd6ac4b7e11bdee29e21578c1 100644 --- a/PyNutil/coordinate_extraction.py +++ b/PyNutil/coordinate_extraction.py @@ -4,7 +4,7 @@ import pandas as pd from DeepSlice.coord_post_processing.spacing_and_indexing import number_sections import json from read_and_write import loadVisuAlignJson -from object_counting import labelPoints +from counting_and_load import labelPoints from visualign_deformations import triangulate, transform_vec from glob import glob from tqdm import tqdm @@ -216,7 +216,7 @@ def SegmentationToAtlasSpaceMultiThreaded(slice, SegmentationPath, pixelID='auto pointsList[index] = np.array(points) -#related to coordinate extraction or object_counting +#related to counting_and_load def createRegionDict(points, regions): """points is a list of points and regions is an id for each point""" regionDict = {region:points[regions==region].flatten().tolist() for region in np.unique(regions)} @@ -224,7 +224,6 @@ def createRegionDict(points, regions): #related to read and write: WritePoints - def WritePoints(pointsDict, filename, infoFile): meshview = [ @@ -246,68 +245,14 @@ def WritePoints(pointsDict, filename, infoFile): # related to read and write: WritePointsToMeshview - def WritePointsToMeshview(points, pointNames, filename, infoFile): regionDict = createRegionDict(points, pointNames) WritePoints(regionDict, filename, infoFile) -# related to object_counting: labelPoints -def labelPoints(points, label_volume, scale_factor=1): - """this function takes a list of points and assigns them to a region based on the regionVolume. - These regions will just be the values in the regionVolume at the points. - it returns a dictionary with the region as the key and the points as the value""" - #first convert the points to 3 columns - points = np.reshape(points, (-1,3)) - #scale the points - points = points * scale_factor - #round the points to the nearest whole number - points = np.round(points).astype(int) - x = points[:,0] - y = points[:,1] - z = points[:,2] - #get the label value for each point - 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): - """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) - return df_counts_per_label_name - - -#def SaveDataframeasCSV(df_to_save): - #df_to_save.to_csv(output_csv, sep=";", na_rep='', index= False) +# related to counting_and_load: labelPoints. +# this has been moved successfully. +# related to counting_and_load: PixelCountPerRegion +# This has been moved successfully. diff --git a/PyNutil/counting_and_load.py b/PyNutil/counting_and_load.py new file mode 100644 index 0000000000000000000000000000000000000000..8e4887b988d9ff7db63c3bdb2d9527c3e9f85883 --- /dev/null +++ b/PyNutil/counting_and_load.py @@ -0,0 +1,57 @@ +import numpy as np +import pandas as pd + +# related to counting and load +def labelPoints(points, label_volume, scale_factor=1): + """this function takes a list of points and assigns them to a region based on the regionVolume. + These regions will just be the values in the regionVolume at the points. + it returns a dictionary with the region as the key and the points as the value""" + #first convert the points to 3 columns + points = np.reshape(points, (-1,3)) + #scale the points + points = points * scale_factor + #round the points to the nearest whole number + points = np.round(points).astype(int) + x = points[:,0] + y = points[:,1] + z = points[:,2] + #get the label value for each point + labels = label_volume[x,y,z] + return labels + + +# related to counting_and_load +def PixelCountPerRegion(labelsDict, label_colours): + """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) + return df_counts_per_label_name + diff --git a/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py b/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py index 7c1d6955dc25ec06ea38a49c5b02d5497ea1ff28..8705412833664e321b5adfa0417e673aedb288ab 100644 --- a/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py +++ b/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py @@ -15,8 +15,9 @@ with open('../test/test2.json', 'r') as f: #print(input) #import our function for converting a folder of segmentations to points -from coordinate_extraction import FolderToAtlasSpace, labelPoints, WritePointsToMeshview, FolderToAtlasSpaceMultiThreaded, PixelCountPerRegion +from coordinate_extraction import FolderToAtlasSpace, WritePointsToMeshview, FolderToAtlasSpaceMultiThreaded from read_and_write import SaveDataframeasCSV +from counting_and_load import PixelCountPerRegion, labelPoints startTime = datetime.now() @@ -41,16 +42,13 @@ df_counts_per_label_name = PixelCountPerRegion(labels, input["allen_colours"]) SaveDataframeasCSV(df_counts_per_label_name, 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"overall time taken was: {time_taken}") +#while we havent added it here it would be good to next quantify the number of cells for each label. #get centroids and areas returns a list of objects and the center coordinate. - #we need to deform the center coordinate according to visualign deformations¨ - #we need to then transform the coordinate into atlas space - #and then save labels like before. \ No newline at end of file diff --git a/PyNutil/object_counting.py b/PyNutil/object_counting.py deleted file mode 100644 index e237457fc11240e2f6f77e4b57f33de0c2927236..0000000000000000000000000000000000000000 --- a/PyNutil/object_counting.py +++ /dev/null @@ -1,17 +0,0 @@ -# related to object_counting: labelPoints -def labelPoints(points, label_volume, scale_factor=1): - """this function takes a list of points and assigns them to a region based on the regionVolume. - These regions will just be the values in the regionVolume at the points. - it returns a dictionary with the region as the key and the points as the value""" - #first convert the points to 3 columns - points = np.reshape(points, (-1,3)) - #scale the points - points = points * scale_factor - #round the points to the nearest whole number - points = np.round(points).astype(int) - x = points[:,0] - y = points[:,1] - z = points[:,2] - #get the label value for each point - labels = label_volume[x,y,z] - return labels \ No newline at end of file