From 83bc3fd222f0d05843bb87a5f8dc6bb7495f3a73 Mon Sep 17 00:00:00 2001 From: sharoncy <s.c.yates@medisin.uio.no> Date: Tue, 23 May 2023 11:05:14 +0200 Subject: [PATCH] FilesinDirectory function Create a list of flat file names --- ...segmentations_to_meshview_multithreaded.py | 15 +++++-- PyNutil/load_workflow.py | 13 +++++- PyNutil/read_and_write.py | 15 +++++++ PyNutil/testing.py | 23 ---------- PyNutil/testing_sharon.py | 43 +++++++++++++++++++ 5 files changed, 82 insertions(+), 27 deletions(-) delete mode 100644 PyNutil/testing.py create mode 100644 PyNutil/testing_sharon.py diff --git a/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py b/PyNutil/folder_of_segmentations_to_meshview_multithreaded.py index b9ba254..9e5625d 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/test1.json', 'r') as f: #import our function for converting a folder of segmentations to points from coordinate_extraction import FolderToAtlasSpace, FolderToAtlasSpaceMultiThreaded -from read_and_write import SaveDataframeasCSV, WritePointsToMeshview +from read_and_write import SaveDataframeasCSV, WritePointsToMeshview, FilesinDirectory from counting_and_load import PixelCountPerRegion, labelPoints startTime = datetime.now() @@ -36,7 +36,7 @@ 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 +#save points to a meshview jsonv WritePointsToMeshview(points, labels, input["points_json_path"], label_df) df_counts_per_label_name = PixelCountPerRegion(labels, input["label_path"]) @@ -50,4 +50,13 @@ print(f"overall time taken was: {time_taken}") #get centroids and areas returns a list of objects and the center coordinate. #we need to deform the center coordinate according to visualign deformations¨ #we need to then transform the coordinate into atlas space -#and then save labels like before. \ No newline at end of file +#and then save labels like before. + + + + + +# Create a list of flat file names from a directory: + +#flatfiles = FilesinDirectory('../test_data/ttA_2877_NOP_atlasmaps') +#print(flatfiles) \ No newline at end of file diff --git a/PyNutil/load_workflow.py b/PyNutil/load_workflow.py index ccacb26..7960ac8 100644 --- a/PyNutil/load_workflow.py +++ b/PyNutil/load_workflow.py @@ -20,4 +20,15 @@ values = image_arr[coordsx, coordsy] # assign x,y coords from image_array into v lbidx = labelfile['idx'].values allen_id_image = lbidx[values.astype(int)] # assign allen IDs into image array -plt.imshow(allen_id_image) \ No newline at end of file +plt.imshow(allen_id_image) + + +# Count area per unique label +# Scale up to size of corresponding segmentation +# calculate segmentation value per slice +# divide segmentation value per idx per slice by area per idx per slice +# output reports +# also do for whole brain + +#Also to do: +# fix position of clear label in label files diff --git a/PyNutil/read_and_write.py b/PyNutil/read_and_write.py index 7d11ea1..a701ebf 100644 --- a/PyNutil/read_and_write.py +++ b/PyNutil/read_and_write.py @@ -3,6 +3,7 @@ import numpy as np import struct import pandas as pd import matplotlib.pyplot as plt +import os #related to read and write @@ -88,3 +89,17 @@ def LabeltoArray(label_path, image_array): allen_id_image = lbidx[values.astype(int)] # assign allen IDs into image array return allen_id_image + +def FilesinDirectory(directory): + """return list of flat file names in a directory""" + list_of_files = [] + for file in os.scandir(directory): + if file.path.endswith(".flat") and file.is_file: + #print(filename.path) + #newfilename, file_ext = os.path.splitext(filename) + #print(newfilename) + filename = os.path.basename(file) + newfilename, file_ext = os.path.splitext(filename) + list_of_files.append(newfilename) + return list_of_files + diff --git a/PyNutil/testing.py b/PyNutil/testing.py deleted file mode 100644 index e38dda5..0000000 --- a/PyNutil/testing.py +++ /dev/null @@ -1,23 +0,0 @@ - -import subprocess -import sys -import os -import glob - -directory = ('../test') - -""" -for filename in os.scandir(directory): - if filename.is_file: - print(filename.path) -""" - -directory = ('../test') - -for filename in os.scandir(directory): - if filename.path.endswith(".json") and filename.is_file: - print(filename.path) - - - - diff --git a/PyNutil/testing_sharon.py b/PyNutil/testing_sharon.py new file mode 100644 index 0000000..0a4b188 --- /dev/null +++ b/PyNutil/testing_sharon.py @@ -0,0 +1,43 @@ + +import subprocess +import sys +import os +import glob + +dir = ('../test_data/ttA_2877_NOP_atlasmaps') +"""fetch file names in a directory""" + +def FilesinDirectory(directory): + for file in os.scandir(directory): + if file.path.endswith(".flat") and file.is_file: + #print(filename.path) + #newfilename, file_ext = os.path.splitext(filename) + #print(newfilename) + filename = os.path.basename(file) + newfilename, file_ext = os.path.splitext(filename) + return newfilename + +#Question: how to return multiple file names into a list? + + +files = [] +newfiles = files.append(FilesinDirectory('../test_data/ttA_2877_NOP_atlasmaps')) +print(files) + + + + + +import os + +# file name with extension +file_name = os.path.basename('../test_data/ttA_2877_NOP_atlasmaps') + +print(file_name) + +# file name without extension +print(os.path.splitext(file_name)[0]) + + + + -- GitLab