diff --git a/PyNutil/coordinate_extraction.py b/PyNutil/coordinate_extraction.py index e1ac91f443da27d78543829323d4f5656644da1b..c3cb1f3a671d88d913c6cafaabed04c5cd58fa2b 100644 --- a/PyNutil/coordinate_extraction.py +++ b/PyNutil/coordinate_extraction.py @@ -2,6 +2,8 @@ import numpy as np 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 visualign_deformations import triangulate, transform_vec from glob import glob from tqdm import tqdm @@ -27,6 +29,7 @@ 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""" @@ -34,6 +37,7 @@ def transformToRegistration(SegHeight, SegWidth, RegHeight, RegWidth): 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""" @@ -43,6 +47,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. @@ -51,6 +56,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""" @@ -70,12 +76,17 @@ def transformToAtlasSpace(anchoring, Y, X, RegHeight, RegWidth): O = np.reshape(O, (3,1)) return (O+XYZU+XYZV).T -#related to read and write + +#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): @@ -115,6 +126,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" @@ -134,6 +146,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" @@ -162,6 +175,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 @@ -200,16 +214,18 @@ def SegmentationToAtlasSpaceMultiThreaded(slice, SegmentationPath, pixelID='auto # points = points.reshape(-1) pointsList[index] = np.array(points) -#related to coordinate extraction + +#related to coordinate extraction or object_counting 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 + +#related to read and write: WritePoints + 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, @@ -227,13 +243,16 @@ def WritePoints(pointsDict, filename, infoFile): json.dump(meshview, f) -# related to read and write + +# related to read and write: WritePointsToMeshview + 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 + + +# 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. diff --git a/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py b/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py index 74d36152aca1d33b6220b86583d369e9b9732e71..1103e73112f3ee2951fdf61acd791b44c0c873d5 100644 --- a/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py +++ b/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py @@ -16,7 +16,7 @@ with open('../input/input.json', 'r') as f: #import our function for converting a folder of segmentations to points from coordinate_extraction import FolderToAtlasSpace, labelPoints, WritePointsToMeshview, FolderToAtlasSpaceMultiThreaded -#from read_and_write import +#from read_and_write import WritePointsToMeshview startTime = datetime.now() diff --git a/PyNutil/object_counting.py b/PyNutil/object_counting.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..e237457fc11240e2f6f77e4b57f33de0c2927236 100644 --- a/PyNutil/object_counting.py +++ b/PyNutil/object_counting.py @@ -0,0 +1,17 @@ +# 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 diff --git a/PyNutil/read_and_write.py b/PyNutil/read_and_write.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..205de7a2d5961de0bd71136bf285e9cde981a7ad 100644 --- a/PyNutil/read_and_write.py +++ b/PyNutil/read_and_write.py @@ -0,0 +1,37 @@ +import json + +#related to read and write: loadVisuAlignJson +def loadVisuAlignJson(filename): + with open(filename) as f: + vafile = json.load(f) + slices = vafile["slices"] + return slices + + +#related to read and write: WritePoints +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, + "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 +# this uses createRegionDict in coordinate_extraction.py +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) \ No newline at end of file