From 63073f738066d66cb261aa84a45ea2d0f5242d3a Mon Sep 17 00:00:00 2001 From: sharoncy <s.c.yates@medisin.uio.no> Date: Thu, 11 May 2023 14:36:09 +0200 Subject: [PATCH] Move functions to read_and_write.py --- PyNutil/coordinate_extraction.py | 53 +------------------ ...segmentations_to_meshview_multithreaded.py | 9 ++-- PyNutil/read_and_write.py | 19 +++++-- 3 files changed, 20 insertions(+), 61 deletions(-) diff --git a/PyNutil/coordinate_extraction.py b/PyNutil/coordinate_extraction.py index 270fc37..088c52f 100644 --- a/PyNutil/coordinate_extraction.py +++ b/PyNutil/coordinate_extraction.py @@ -78,17 +78,6 @@ def transformToAtlasSpace(anchoring, Y, X, RegHeight, RegWidth): return (O+XYZU+XYZV).T -#related to read and write: loadVisuAlignJson -# this has been moved successfully to read_and_write -""" -def loadVisuAlignJson(filename): - with open(filename) as f: - vafile = json.load(f) - slices = vafile["slices"] - return slices -""" - - # related to coordinate extraction def SegmentationToAtlasSpace(slice, SegmentationPath, pixelID='auto', nonLinear=True): """combines many functions to convert a segmentation to atlas space. It takes care @@ -149,6 +138,7 @@ def FolderToAtlasSpace(folder, QUINT_alignment, pixelID=[0, 0, 0], nonLinear=Tru #related to coordinate extraction +#this function returns an array of points def FolderToAtlasSpaceMultiThreaded(folder, QUINT_alignment, pixelID=[0, 0, 0], nonLinear=True): "apply Segmentation to atlas space to all segmentations in a folder" slices = loadVisuAlignJson(QUINT_alignment) @@ -178,6 +168,7 @@ def FolderToAtlasSpaceMultiThreaded(folder, QUINT_alignment, pixelID=[0, 0, 0], # related to coordinate extraction +# this function returns an array of points def SegmentationToAtlasSpaceMultiThreaded(slice, SegmentationPath, pixelID='auto', nonLinear=True, pointsList=None, index=None): """combines many functions to convert a segmentation to atlas space. It takes care of deformations""" @@ -216,43 +207,3 @@ def SegmentationToAtlasSpaceMultiThreaded(slice, SegmentationPath, pixelID='auto pointsList[index] = np.array(points) -#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)} - return regionDict - - -#related to read and write: WritePoints -def WritePoints(pointsDict, filename, infoFile): - - meshview = [ - { - "idx": idx, - "count": len(pointsDict[name])//3, - "name" :str(infoFile["name"].values[infoFile["allenID"]==name][0]), - "triplets": pointsDict[name], - "r": str(infoFile["r"].values[infoFile["allenID"]==name][0]), - "g": str(infoFile["g"].values[infoFile["allenID"]==name][0]), - "b": str(infoFile["b"].values[infoFile["allenID"]==name][0]) - } - for name, idx in zip(pointsDict.keys(), range(len(pointsDict.keys()))) - ] - #write meshview json - with open(filename, "w") as f: - json.dump(meshview, f) - - - -# related to read and write: WritePointsToMeshview -def WritePointsToMeshview(points, pointNames, filename, infoFile): - regionDict = createRegionDict(points, pointNames) - WritePoints(regionDict, filename, infoFile) - - - -# 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/folder_of_segmentations_to_meshview_multithreaded.py b/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py index 8705412..c7f43cd 100644 --- a/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py +++ b/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py @@ -9,14 +9,14 @@ import json from datetime import datetime -#import json into "input" variable, use to define input parameters +#import json, use to define input parameters 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, WritePointsToMeshview, FolderToAtlasSpaceMultiThreaded -from read_and_write import SaveDataframeasCSV +from coordinate_extraction import FolderToAtlasSpace, FolderToAtlasSpaceMultiThreaded +from read_and_write import SaveDataframeasCSV, WritePointsToMeshview from counting_and_load import PixelCountPerRegion, labelPoints startTime = datetime.now() @@ -35,14 +35,13 @@ label_df = pd.read_csv(input["label_path"]) data, header = nrrd.read(input["volume_path"]) #now we can get the labels for each point labels = labelPoints(points, data, scale_factor=2.5) + #save points to a meshview json WritePointsToMeshview(points, labels, input["points_json_path"], label_df) df_counts_per_label_name = PixelCountPerRegion(labels, input["allen_colours"]) - SaveDataframeasCSV(df_counts_per_label_name, input["counts_per_label_name"]) - time_taken = datetime.now() - startTime print(f"overall time taken was: {time_taken}") diff --git a/PyNutil/read_and_write.py b/PyNutil/read_and_write.py index e791c78..574e3e6 100644 --- a/PyNutil/read_and_write.py +++ b/PyNutil/read_and_write.py @@ -1,7 +1,9 @@ import json +import numpy as np -#related to read and write: loadVisuAlignJson +#related to read and write +# this function reads a VisuAlign JSON and returns the slices def loadVisuAlignJson(filename): with open(filename) as f: vafile = json.load(f) @@ -9,10 +11,18 @@ def loadVisuAlignJson(filename): return slices +#related to read_and_write, used in WritePointsToMeshview +# this function returns a dictionary of region names +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)} + return regionDict + + #related to read and write: WritePoints +# this function writes the region dictionary to a meshview json def WritePoints(pointsDict, filename, infoFile): - """write a series of points to a meshview json file. pointsDict is a dictionary with the points. - pointsDict is created by createRegionDict. infoFile is a csv file with the information about the regions""" + meshview = [ { "idx": idx, @@ -31,9 +41,8 @@ def WritePoints(pointsDict, filename, infoFile): # related to read and write: WritePointsToMeshview -# this uses createRegionDict in coordinate_extraction.py +# this function combines createRegionDict and WritePoints functions def WritePointsToMeshview(points, pointNames, filename, infoFile): - """this is the function you call more often as it combines the other functions for writing meshview""" regionDict = createRegionDict(points, pointNames) WritePoints(regionDict, filename, infoFile) -- GitLab