From 5dac7235e7a464515987637d8e6a227136e1cfa2 Mon Sep 17 00:00:00 2001
From: sharoncy <s.c.yates@medisin.uio.no>
Date: Tue, 9 May 2023 15:14:54 +0200
Subject: [PATCH] Support for input in json formalt

---
 .../folder_of_segmentations_to_meshview.py    |  1 +
 ...segmentations_to_meshview_multithreaded.py | 30 ++++++++-----------
 input.json                                    | 10 +++++++
 input/input.json                              | 10 +++++++
 4 files changed, 33 insertions(+), 18 deletions(-)
 create mode 100644 input.json
 create mode 100644 input/input.json

diff --git a/PyNutil/folder_of_segmentations_to_meshview.py b/PyNutil/folder_of_segmentations_to_meshview.py
index 7abbfd8..8277577 100644
--- a/PyNutil/folder_of_segmentations_to_meshview.py
+++ b/PyNutil/folder_of_segmentations_to_meshview.py
@@ -5,6 +5,7 @@ import pandas as pd
 import nrrd
 import numpy as np
 import csv
+import json
 
 from datetime import datetime
 
diff --git a/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py b/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py
index 20cf032..47fc06a 100644
--- a/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py
+++ b/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py
@@ -5,29 +5,22 @@ import pandas as pd
 import nrrd
 import numpy as np
 import csv
+import json
 
 from datetime import datetime
 
-#import json and use it to define volume_path, segmentation_folder, alignment_json, label_path, colour, allen_colours
-
+#import json, use to define input parameters
+with open('../input.json', 'r') as f:
+  input = json.load(f)
+#print(input)
 
 #import our function for converting a folder of segmentations to points
-from PyNutil import FolderToAtlasSpace, labelPoints, WritePointsToMeshview, FolderToAtlasSpaceMultiThreaded
-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"
-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"
+from coordinate_extraction import FolderToAtlasSpace, labelPoints, WritePointsToMeshview, FolderToAtlasSpaceMultiThreaded
 
 startTime = datetime.now()
 
-
 #now we can use our function to convert the folder of segmentations to points
-points = FolderToAtlasSpaceMultiThreaded(segmentation_folder,alignment_json, pixelID=colour, nonLinear=True)
+points = FolderToAtlasSpaceMultiThreaded(input["segmentation_folder"],input["alignment_json"], pixelID=[255,0,255], nonLinear=True)
 
 time_taken = datetime.now() - startTime
 
@@ -36,12 +29,13 @@ print(f"Folder to atlas took: {time_taken}")
 #then the path to the volume
 
 #read the label files
-label_df = pd.read_csv(label_path)
+label_df = pd.read_csv(input["label_path"])
 #read the annotation volume, it also has a header but we don't need it
+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,points_json_path, label_df)
+WritePointsToMeshview(points, labels, input["points_json_path"], label_df)
 
 #Task:
 # Make a pandas dataframe
@@ -56,7 +50,7 @@ counts_per_label = list(zip(counted_labels,label_counts))
 df_counts_per_label = pd.DataFrame(counts_per_label, columns=["allenID","pixel count"])
 
 
-df_allen_colours =pd.read_csv(allen_colours, sep=",")
+df_allen_colours =pd.read_csv(input["allen_colours"], sep=",")
 df_allen_colours
 
 #look up name, r, g, b in df_allen_colours in df_counts_per_label based on "allenID"
@@ -80,7 +74,7 @@ df_counts_per_label_name = pd.DataFrame(new_rows)
 df_counts_per_label_name
 
 # write to csv file
-df_counts_per_label_name.to_csv(counts_per_label_name, sep=";", na_rep='', index= False)
+df_counts_per_label_name.to_csv(input["counts_per_label_name"], sep=";", na_rep='', index= False)
 
 #r = df_allen_colours["r"]
 #g = df_allen_colours["g"]
diff --git a/input.json b/input.json
new file mode 100644
index 0000000..a359636
--- /dev/null
+++ b/input.json
@@ -0,0 +1,10 @@
+{"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
new file mode 100644
index 0000000..68434ce
--- /dev/null
+++ b/input/input.json
@@ -0,0 +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",
+    "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
-- 
GitLab