From eb80e04c1e0b06b67628197f5b803ab557599bc7 Mon Sep 17 00:00:00 2001
From: sharoncy <s.c.yates@medisin.uio.no>
Date: Thu, 11 May 2023 12:47:34 +0200
Subject: [PATCH] Create SaveDataframeasCSV function

---
 PyNutil/coordinate_extraction.py                          | 7 +++++--
 .../folder_of_segmentations_to_meshview_multithreaded.py  | 6 ++++--
 PyNutil/read_and_write.py                                 | 8 +++++++-
 3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/PyNutil/coordinate_extraction.py b/PyNutil/coordinate_extraction.py
index fecb68f..1edb063 100644
--- a/PyNutil/coordinate_extraction.py
+++ b/PyNutil/coordinate_extraction.py
@@ -273,7 +273,7 @@ def labelPoints(points, label_volume, scale_factor=1):
 
 # related to object_counting
 # consider separating out writing to CSV in future
-def PixelCountPerRegion(labelsDict, label_colours, output_csv): 
+def PixelCountPerRegion(labelsDict, label_colours): 
     """Function for counting no. of pixels per region and writing to CSV based on 
     a dictionary with the region as the key and the points as the value, """
     counted_labels, label_counts = np.unique(labelsDict, return_counts=True)
@@ -305,6 +305,9 @@ def PixelCountPerRegion(labelsDict, label_colours, output_csv):
         new_rows.append(row)
 
     df_counts_per_label_name = pd.DataFrame(new_rows)
+    return df_counts_per_label_name
     
-    df_counts_per_label_name.to_csv(output_csv, sep=";", na_rep='', index= False)
+   
+#def SaveDataframeasCSV(df_to_save):
+    #df_to_save.to_csv(output_csv, sep=";", na_rep='', index= False)
 
diff --git a/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py b/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py
index 001cc7d..7c1d695 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('../test/test2.json', 'r') as f:
 
 #import our function for converting a folder of segmentations to points
 from coordinate_extraction import FolderToAtlasSpace, labelPoints, WritePointsToMeshview, FolderToAtlasSpaceMultiThreaded, PixelCountPerRegion
-#from read_and_write import WritePointsToMeshview
+from read_and_write import SaveDataframeasCSV
 
 startTime = datetime.now()
 
@@ -37,7 +37,9 @@ labels = labelPoints(points, data, scale_factor=2.5)
 #save points to a meshview json
 WritePointsToMeshview(points, labels, input["points_json_path"], label_df)
 
-PixelCountPerRegion(labels, input["allen_colours"], input["counts_per_label_name"])
+df_counts_per_label_name = PixelCountPerRegion(labels, input["allen_colours"])
+
+SaveDataframeasCSV(df_counts_per_label_name, input["counts_per_label_name"])
 
 #while we havent added it here it would be good to next quantify the number of cells for each label.
 
diff --git a/PyNutil/read_and_write.py b/PyNutil/read_and_write.py
index 205de7a..e791c78 100644
--- a/PyNutil/read_and_write.py
+++ b/PyNutil/read_and_write.py
@@ -1,5 +1,6 @@
 import json
 
+
 #related to read and write: loadVisuAlignJson
 def loadVisuAlignJson(filename):
     with open(filename) as f:
@@ -34,4 +35,9 @@ def WritePoints(pointsDict, 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)
-    WritePoints(regionDict, filename, infoFile)
\ No newline at end of file
+    WritePoints(regionDict, filename, infoFile)
+
+
+def SaveDataframeasCSV(df_to_save, output_csv):
+    """Function for saving a df as a CSV file"""
+    df_to_save.to_csv(output_csv, sep=";", na_rep='', index= False)
\ No newline at end of file
-- 
GitLab