diff --git a/PyNutil/coordinate_extraction.py b/PyNutil/coordinate_extraction.py index 453fa27dc85e9bf598b2635589f0004f806ce707..e1ac91f443da27d78543829323d4f5656644da1b 100644 --- a/PyNutil/coordinate_extraction.py +++ b/PyNutil/coordinate_extraction.py @@ -9,6 +9,7 @@ import cv2 from skimage import measure import threading +#related to coordinate_extraction def getCentroidsAndArea(Segmentation, pixelCutOff=0): """this function returns the center coordinate of each object in the segmentation. You can set a pixelCutOff to remove objects that are smaller than that number of pixels""" @@ -26,18 +27,14 @@ def getCentroidsAndArea(Segmentation, pixelCutOff=0): coords = np.array([label.coords for label in labelsInfo]) return centroids, area, coords - - - - - +# related to coordinate extraction def transformToRegistration(SegHeight, SegWidth, RegHeight, RegWidth): """this function returns the scaling factors to transform the segmentation to the registration space""" Yscale = RegHeight/SegHeight Xscale = RegWidth/SegWidth return Yscale,Xscale - +# related to coordinate extraction def findMatchingPixels(Segmentation, id): """this function returns the Y and X coordinates of all the pixels in the segmentation that match the id provided""" mask = Segmentation==id @@ -46,6 +43,7 @@ def findMatchingPixels(Segmentation, id): idY, idX = id_positions[0], id_positions[1] return idY,idX +#related to coordinate extraction def scalePositions(idY, idX, Yscale, Xscale): """this function scales the Y and X coordinates to the registration space. (the Yscale and Xscale are the output of transformToRegistration)""" @@ -53,6 +51,7 @@ def scalePositions(idY, idX, Yscale, Xscale): idX = idX * Xscale return idY,idX +#related to coordinate extraction def transformToAtlasSpace(anchoring, Y, X, RegHeight, RegWidth): """transform to atlas space using the QuickNII anchoring vector""" O = anchoring[0:3] @@ -71,12 +70,14 @@ def transformToAtlasSpace(anchoring, Y, X, RegHeight, RegWidth): O = np.reshape(O, (3,1)) return (O+XYZU+XYZV).T +#related 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 of deformations""" @@ -114,7 +115,7 @@ def SegmentationToAtlasSpace(slice, SegmentationPath, pixelID='auto', nonLinear= # points = points.reshape(-1) return np.array(points) - +# related to coordinate extraction def FolderToAtlasSpace(folder, QUINT_alignment, pixelID=[0, 0, 0], nonLinear=True): "apply Segmentation to atlas space to all segmentations in a folder" slices = loadVisuAlignJson(QUINT_alignment) @@ -133,6 +134,7 @@ def FolderToAtlasSpace(folder, QUINT_alignment, pixelID=[0, 0, 0], nonLinear=Tru points.extend(SegmentationToAtlasSpace(current_slice, SegmentationPath, pixelID, nonLinear)) return np.array(points) +#related to coordinate extraction 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) @@ -160,7 +162,7 @@ def FolderToAtlasSpaceMultiThreaded(folder, QUINT_alignment, pixelID=[0, 0, 0], points = [item for sublist in pointsList for item in sublist] return np.array(points) - +# related to coordinate extraction 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""" @@ -198,11 +200,13 @@ def SegmentationToAtlasSpaceMultiThreaded(slice, SegmentationPath, pixelID='auto # points = points.reshape(-1) pointsList[index] = np.array(points) +#related to coordinate extraction 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 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""" @@ -222,11 +226,14 @@ def WritePoints(pointsDict, filename, infoFile): with open(filename, "w") as f: json.dump(meshview, f) + +# related to read and write 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) +# related to object_counting 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. diff --git a/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py b/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py index 47fc06ab0b53f4c454ac045d1761faccb2d0a304..74d36152aca1d33b6220b86583d369e9b9732e71 100644 --- a/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py +++ b/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py @@ -10,17 +10,18 @@ import json from datetime import datetime #import json, use to define input parameters -with open('../input.json', 'r') as f: +with open('../input/input.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 read_and_write import startTime = datetime.now() #now we can use our function to convert the folder of segmentations to points -points = FolderToAtlasSpaceMultiThreaded(input["segmentation_folder"],input["alignment_json"], pixelID=[255,0,255], nonLinear=True) +points = FolderToAtlasSpaceMultiThreaded(input["segmentation_folder"],input["alignment_json"], pixelID=input["colour"], nonLinear=input["nonlinear"]) time_taken = datetime.now() - startTime diff --git a/input.json b/input.json deleted file mode 100644 index a3596365e91c1f9c2968b31ea2ceb138be62c940..0000000000000000000000000000000000000000 --- a/input.json +++ /dev/null @@ -1,10 +0,0 @@ -{"label_path": "../annotation_volumes//allen2022_colours.csv", - "colour": "[255, 0, 255]", - "nonlinear": "True", - "volume_path": "../annotation_volumes//annotation_10_reoriented.nrrd", - "points_json_path": "../outputs/points.json", - "segmentation_folder": "../test_data/oneSection15", - "alignment_json": "../test_data/C68_nonlinear_no_markers.json", - "allen_colours": "../annotation_volumes//allen2022_colours.csv", - "counts_per_label_name": "../outputs/counts_per_allenID.csv" -} \ No newline at end of file diff --git a/input/input.json b/input/input.json index 68434ceda0d756a31b8998958b8e3e0cb85d53aa..479bdd3228cca9bf88c1b1becb68e60d1ed57b84 100644 --- a/input/input.json +++ b/input/input.json @@ -1,10 +1,10 @@ -{"label_path": "../annotation_volumes//allen2022_colours.csv", - "colour": "[255, 0, 255]", - "volume_path": "../annotation_volumes//annotation_10_reoriented.nrrd", - "data, header": "nrrd.read(volume_path)", - "points_json_path": "../outputs/points.json", +{ "volume_path": "../annotation_volumes/annotation_10_reoriented.nrrd", + "label_path": "../annotation_volumes/allen2022_colours.csv", + "allen_colours": "../annotation_volumes/allen2022_colours.csv", "segmentation_folder": "../test_data/oneSection15", "alignment_json": "../test_data/C68_nonlinear_no_markers.json", - "allen_colours": "../annotation_volumes//allen2022_colours.csv", + "nonlinear": true, + "colour": [255, 0, 255], + "points_json_path": "../outputs/points.json", "counts_per_label_name": "../outputs/counts_per_allenID.csv" } \ No newline at end of file