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

Support for input in json format, continued

parent 5dac7235
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,7 @@ import cv2 ...@@ -9,6 +9,7 @@ import cv2
from skimage import measure from skimage import measure
import threading import threading
#related to coordinate_extraction
def getCentroidsAndArea(Segmentation, pixelCutOff=0): def getCentroidsAndArea(Segmentation, pixelCutOff=0):
"""this function returns the center coordinate of each object in the segmentation. """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""" 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): ...@@ -26,18 +27,14 @@ def getCentroidsAndArea(Segmentation, pixelCutOff=0):
coords = np.array([label.coords for label in labelsInfo]) coords = np.array([label.coords for label in labelsInfo])
return centroids, area, coords return centroids, area, coords
# related to coordinate extraction
def transformToRegistration(SegHeight, SegWidth, RegHeight, RegWidth): def transformToRegistration(SegHeight, SegWidth, RegHeight, RegWidth):
"""this function returns the scaling factors to transform the segmentation to the registration space""" """this function returns the scaling factors to transform the segmentation to the registration space"""
Yscale = RegHeight/SegHeight Yscale = RegHeight/SegHeight
Xscale = RegWidth/SegWidth Xscale = RegWidth/SegWidth
return Yscale,Xscale return Yscale,Xscale
# related to coordinate extraction
def findMatchingPixels(Segmentation, id): def findMatchingPixels(Segmentation, id):
"""this function returns the Y and X coordinates of all the pixels in the segmentation that match the id provided""" """this function returns the Y and X coordinates of all the pixels in the segmentation that match the id provided"""
mask = Segmentation==id mask = Segmentation==id
...@@ -46,6 +43,7 @@ def findMatchingPixels(Segmentation, id): ...@@ -46,6 +43,7 @@ def findMatchingPixels(Segmentation, id):
idY, idX = id_positions[0], id_positions[1] idY, idX = id_positions[0], id_positions[1]
return idY,idX return idY,idX
#related to coordinate extraction
def scalePositions(idY, idX, Yscale, Xscale): def scalePositions(idY, idX, Yscale, Xscale):
"""this function scales the Y and X coordinates to the registration space. """this function scales the Y and X coordinates to the registration space.
(the Yscale and Xscale are the output of transformToRegistration)""" (the Yscale and Xscale are the output of transformToRegistration)"""
...@@ -53,6 +51,7 @@ def scalePositions(idY, idX, Yscale, Xscale): ...@@ -53,6 +51,7 @@ def scalePositions(idY, idX, Yscale, Xscale):
idX = idX * Xscale idX = idX * Xscale
return idY,idX return idY,idX
#related to coordinate extraction
def transformToAtlasSpace(anchoring, Y, X, RegHeight, RegWidth): def transformToAtlasSpace(anchoring, Y, X, RegHeight, RegWidth):
"""transform to atlas space using the QuickNII anchoring vector""" """transform to atlas space using the QuickNII anchoring vector"""
O = anchoring[0:3] O = anchoring[0:3]
...@@ -71,12 +70,14 @@ def transformToAtlasSpace(anchoring, Y, X, RegHeight, RegWidth): ...@@ -71,12 +70,14 @@ def transformToAtlasSpace(anchoring, Y, X, RegHeight, RegWidth):
O = np.reshape(O, (3,1)) O = np.reshape(O, (3,1))
return (O+XYZU+XYZV).T return (O+XYZU+XYZV).T
#related to read and write
def loadVisuAlignJson(filename): def loadVisuAlignJson(filename):
with open(filename) as f: with open(filename) as f:
vafile = json.load(f) vafile = json.load(f)
slices = vafile["slices"] slices = vafile["slices"]
return slices return slices
# related to coordinate extraction
def SegmentationToAtlasSpace(slice, SegmentationPath, pixelID='auto', nonLinear=True): def SegmentationToAtlasSpace(slice, SegmentationPath, pixelID='auto', nonLinear=True):
"""combines many functions to convert a segmentation to atlas space. It takes care """combines many functions to convert a segmentation to atlas space. It takes care
of deformations""" of deformations"""
...@@ -114,7 +115,7 @@ def SegmentationToAtlasSpace(slice, SegmentationPath, pixelID='auto', nonLinear= ...@@ -114,7 +115,7 @@ def SegmentationToAtlasSpace(slice, SegmentationPath, pixelID='auto', nonLinear=
# points = points.reshape(-1) # points = points.reshape(-1)
return np.array(points) return np.array(points)
# related to coordinate extraction
def FolderToAtlasSpace(folder, QUINT_alignment, pixelID=[0, 0, 0], nonLinear=True): def FolderToAtlasSpace(folder, QUINT_alignment, pixelID=[0, 0, 0], nonLinear=True):
"apply Segmentation to atlas space to all segmentations in a folder" "apply Segmentation to atlas space to all segmentations in a folder"
slices = loadVisuAlignJson(QUINT_alignment) slices = loadVisuAlignJson(QUINT_alignment)
...@@ -133,6 +134,7 @@ def FolderToAtlasSpace(folder, QUINT_alignment, pixelID=[0, 0, 0], nonLinear=Tru ...@@ -133,6 +134,7 @@ def FolderToAtlasSpace(folder, QUINT_alignment, pixelID=[0, 0, 0], nonLinear=Tru
points.extend(SegmentationToAtlasSpace(current_slice, SegmentationPath, pixelID, nonLinear)) points.extend(SegmentationToAtlasSpace(current_slice, SegmentationPath, pixelID, nonLinear))
return np.array(points) return np.array(points)
#related to coordinate extraction
def FolderToAtlasSpaceMultiThreaded(folder, QUINT_alignment, pixelID=[0, 0, 0], nonLinear=True): def FolderToAtlasSpaceMultiThreaded(folder, QUINT_alignment, pixelID=[0, 0, 0], nonLinear=True):
"apply Segmentation to atlas space to all segmentations in a folder" "apply Segmentation to atlas space to all segmentations in a folder"
slices = loadVisuAlignJson(QUINT_alignment) slices = loadVisuAlignJson(QUINT_alignment)
...@@ -160,7 +162,7 @@ def FolderToAtlasSpaceMultiThreaded(folder, QUINT_alignment, pixelID=[0, 0, 0], ...@@ -160,7 +162,7 @@ def FolderToAtlasSpaceMultiThreaded(folder, QUINT_alignment, pixelID=[0, 0, 0],
points = [item for sublist in pointsList for item in sublist] points = [item for sublist in pointsList for item in sublist]
return np.array(points) return np.array(points)
# related to coordinate extraction
def SegmentationToAtlasSpaceMultiThreaded(slice, SegmentationPath, pixelID='auto', nonLinear=True, pointsList=None, index=None): 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 """combines many functions to convert a segmentation to atlas space. It takes care
of deformations""" of deformations"""
...@@ -198,11 +200,13 @@ def SegmentationToAtlasSpaceMultiThreaded(slice, SegmentationPath, pixelID='auto ...@@ -198,11 +200,13 @@ def SegmentationToAtlasSpaceMultiThreaded(slice, SegmentationPath, pixelID='auto
# points = points.reshape(-1) # points = points.reshape(-1)
pointsList[index] = np.array(points) pointsList[index] = np.array(points)
#related to coordinate extraction
def createRegionDict(points, regions): def createRegionDict(points, regions):
"""points is a list of points and regions is an id for each point""" """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)} regionDict = {region:points[regions==region].flatten().tolist() for region in np.unique(regions)}
return regionDict return regionDict
#related to read and write
def WritePoints(pointsDict, filename, infoFile): def WritePoints(pointsDict, filename, infoFile):
"""write a series of points to a meshview json file. pointsDict is a dictionary with the points. """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""" 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): ...@@ -222,11 +226,14 @@ def WritePoints(pointsDict, filename, infoFile):
with open(filename, "w") as f: with open(filename, "w") as f:
json.dump(meshview, f) json.dump(meshview, f)
# related to read and write
def WritePointsToMeshview(points, pointNames, filename, infoFile): def WritePointsToMeshview(points, pointNames, filename, infoFile):
"""this is the function you call more often as it combines the other functions for writing meshview""" """this is the function you call more often as it combines the other functions for writing meshview"""
regionDict = createRegionDict(points, pointNames) regionDict = createRegionDict(points, pointNames)
WritePoints(regionDict, filename, infoFile) WritePoints(regionDict, filename, infoFile)
# related to object_counting
def labelPoints(points, label_volume, scale_factor=1): 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. """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. These regions will just be the values in the regionVolume at the points.
......
...@@ -10,17 +10,18 @@ import json ...@@ -10,17 +10,18 @@ import json
from datetime import datetime from datetime import datetime
#import json, use to define input parameters #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) input = json.load(f)
#print(input) #print(input)
#import our function for converting a folder of segmentations to points #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
#from read_and_write import
startTime = datetime.now() startTime = datetime.now()
#now we can use our function to convert the folder of segmentations to points #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 time_taken = datetime.now() - startTime
......
{"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
{"label_path": "../annotation_volumes//allen2022_colours.csv", { "volume_path": "../annotation_volumes/annotation_10_reoriented.nrrd",
"colour": "[255, 0, 255]", "label_path": "../annotation_volumes/allen2022_colours.csv",
"volume_path": "../annotation_volumes//annotation_10_reoriented.nrrd", "allen_colours": "../annotation_volumes/allen2022_colours.csv",
"data, header": "nrrd.read(volume_path)",
"points_json_path": "../outputs/points.json",
"segmentation_folder": "../test_data/oneSection15", "segmentation_folder": "../test_data/oneSection15",
"alignment_json": "../test_data/C68_nonlinear_no_markers.json", "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" "counts_per_label_name": "../outputs/counts_per_allenID.csv"
} }
\ No newline at end of file
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