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

Move functions to read_and_write.py

parent 8d8a9e54
No related branches found
No related tags found
No related merge requests found
...@@ -78,17 +78,6 @@ def transformToAtlasSpace(anchoring, Y, X, RegHeight, RegWidth): ...@@ -78,17 +78,6 @@ def transformToAtlasSpace(anchoring, Y, X, RegHeight, RegWidth):
return (O+XYZU+XYZV).T 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 # 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
...@@ -149,6 +138,7 @@ def FolderToAtlasSpace(folder, QUINT_alignment, pixelID=[0, 0, 0], nonLinear=Tru ...@@ -149,6 +138,7 @@ def FolderToAtlasSpace(folder, QUINT_alignment, pixelID=[0, 0, 0], nonLinear=Tru
#related to coordinate extraction #related to coordinate extraction
#this function returns an array of points
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)
...@@ -178,6 +168,7 @@ def FolderToAtlasSpaceMultiThreaded(folder, QUINT_alignment, pixelID=[0, 0, 0], ...@@ -178,6 +168,7 @@ def FolderToAtlasSpaceMultiThreaded(folder, QUINT_alignment, pixelID=[0, 0, 0],
# related to coordinate extraction # related to coordinate extraction
# this function returns an array of points
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"""
...@@ -216,43 +207,3 @@ def SegmentationToAtlasSpaceMultiThreaded(slice, SegmentationPath, pixelID='auto ...@@ -216,43 +207,3 @@ def SegmentationToAtlasSpaceMultiThreaded(slice, SegmentationPath, pixelID='auto
pointsList[index] = np.array(points) 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.
...@@ -9,14 +9,14 @@ import json ...@@ -9,14 +9,14 @@ import json
from datetime import datetime 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: with open('../test/test2.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, WritePointsToMeshview, FolderToAtlasSpaceMultiThreaded from coordinate_extraction import FolderToAtlasSpace, FolderToAtlasSpaceMultiThreaded
from read_and_write import SaveDataframeasCSV from read_and_write import SaveDataframeasCSV, WritePointsToMeshview
from counting_and_load import PixelCountPerRegion, labelPoints from counting_and_load import PixelCountPerRegion, labelPoints
startTime = datetime.now() startTime = datetime.now()
...@@ -35,14 +35,13 @@ label_df = pd.read_csv(input["label_path"]) ...@@ -35,14 +35,13 @@ label_df = pd.read_csv(input["label_path"])
data, header = nrrd.read(input["volume_path"]) data, header = nrrd.read(input["volume_path"])
#now we can get the labels for each point #now we can get the labels for each point
labels = labelPoints(points, data, scale_factor=2.5) labels = labelPoints(points, data, scale_factor=2.5)
#save points to a meshview json #save points to a meshview json
WritePointsToMeshview(points, labels, input["points_json_path"], label_df) WritePointsToMeshview(points, labels, input["points_json_path"], label_df)
df_counts_per_label_name = PixelCountPerRegion(labels, input["allen_colours"]) df_counts_per_label_name = PixelCountPerRegion(labels, input["allen_colours"])
SaveDataframeasCSV(df_counts_per_label_name, input["counts_per_label_name"]) SaveDataframeasCSV(df_counts_per_label_name, input["counts_per_label_name"])
time_taken = datetime.now() - startTime time_taken = datetime.now() - startTime
print(f"overall time taken was: {time_taken}") print(f"overall time taken was: {time_taken}")
......
import json 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): def loadVisuAlignJson(filename):
with open(filename) as f: with open(filename) as f:
vafile = json.load(f) vafile = json.load(f)
...@@ -9,10 +11,18 @@ def loadVisuAlignJson(filename): ...@@ -9,10 +11,18 @@ def loadVisuAlignJson(filename):
return slices 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 #related to read and write: WritePoints
# this function writes the region dictionary to a meshview json
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.
pointsDict is created by createRegionDict. infoFile is a csv file with the information about the regions"""
meshview = [ meshview = [
{ {
"idx": idx, "idx": idx,
...@@ -31,9 +41,8 @@ def WritePoints(pointsDict, filename, infoFile): ...@@ -31,9 +41,8 @@ def WritePoints(pointsDict, filename, infoFile):
# related to read and write: WritePointsToMeshview # 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): 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) regionDict = createRegionDict(points, pointNames)
WritePoints(regionDict, filename, infoFile) WritePoints(regionDict, filename, infoFile)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment