From 37b10118747e82b0a984308e841bb23c9da2bdde Mon Sep 17 00:00:00 2001 From: sharoncy <s.c.yates@medisin.uio.no> Date: Mon, 22 May 2023 17:18:21 +0200 Subject: [PATCH] Flat to array fix --- PyNutil/load_workflow.py | 5 +++-- PyNutil/read_and_write.py | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/PyNutil/load_workflow.py b/PyNutil/load_workflow.py index 07c363e..ccacb26 100644 --- a/PyNutil/load_workflow.py +++ b/PyNutil/load_workflow.py @@ -3,16 +3,17 @@ import matplotlib.pyplot as plt import pandas as pd import numpy as np -from read_and_write import FlattoArray +from read_and_write import FlattoArray, LabeltoArray base= r"../test_data/ttA_2877_NOP_atlasmaps/2877_NOP_tTA_lacZ_Xgal_s037_nl.flat" +label = r"../annotation_volumes\allen2017_colours.csv" image_arr = FlattoArray(base) plt.imshow(FlattoArray(base)) """assign label file values into image array""" -labelfile = pd.read_csv("../annotation_volumes/allen2017_colours.csv") +labelfile = pd.read_csv(r"../annotation_volumes\allen2017_colours.csv") allen_id_image = np.zeros((h,w)) # create an empty image array coordsy, coordsx = np.meshgrid(list(range(w)), list(range(h))) values = image_arr[coordsx, coordsy] # assign x,y coords from image_array into values diff --git a/PyNutil/read_and_write.py b/PyNutil/read_and_write.py index e474329..7d11ea1 100644 --- a/PyNutil/read_and_write.py +++ b/PyNutil/read_and_write.py @@ -56,7 +56,7 @@ def SaveDataframeasCSV(df_to_save, output_csv): def FlattoArray(flatfile): - """Read flat file and write into an np array""" + """Read flat file and write into an np array, return array""" with open(flatfile,"rb") as f: #i dont know what b is, w and h are the width and height that we get from the #flat file header @@ -75,4 +75,16 @@ def FlattoArray(flatfile): image[y,x] = imagedata[x+y*w] image_arr = np.array(image) - return image_arr \ No newline at end of file + return image_arr + + +def LabeltoArray(label_path, image_array): + """assign label file values into image array, return array""" + labelfile = pd.read_csv(label_path) + allen_id_image = np.zeros((h,w)) # create an empty image array + coordsy, coordsx = np.meshgrid(list(range(w)), list(range(h))) + values = image_array[coordsx, coordsy] # assign x,y coords from image_array into values + lbidx = labelfile['idx'].values + allen_id_image = lbidx[values.astype(int)] # assign allen IDs into image array + return allen_id_image + -- GitLab