diff --git a/PyNutil/counting_and_load.py b/PyNutil/counting_and_load.py index 09b80e950817b30649d2532ba8bab52a06810d68..77f19a4607e59d6be394c59a16912a7a8b68e523 100644 --- a/PyNutil/counting_and_load.py +++ b/PyNutil/counting_and_load.py @@ -1,5 +1,6 @@ import numpy as np import pandas as pd +import struct # related to counting and load def labelPoints(points, label_volume, scale_factor=1): @@ -56,7 +57,31 @@ def PixelCountPerRegion(labelsDict, label_colours): return df_counts_per_label_name +"""read flat file and write into an np array""" +def flat_to_array(flatfile): + 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 + b,w,h=struct.unpack(">BII",f.read(9)) + #data is a one dimensional list of values + #it has the shape width times height + data =struct.unpack(">"+("xBH"[b]*(w*h)),f.read(b*w*h)) + + #convert flat file data into an array, previously data was a tuple + imagedata = np.array(data) + + #create an empty image array in the right shape, write imagedata into image_array + image = np.zeros((h,w)) + for x in range(w): + for y in range(h): + image[y,x] = imagedata[x+y*w] + + image_arr = np.array(image) + return image_arr + + # import flat files, count pixels per label, np.unique... etc. nitrc.org/plugins/mwiki/index.php?title=visualign:Deformation + """ base=slice["filename"][:-4] diff --git a/PyNutil/load_workflow.py b/PyNutil/load_workflow.py new file mode 100644 index 0000000000000000000000000000000000000000..07c363e25cf6a7d0afab153b0378617c36f34c7c --- /dev/null +++ b/PyNutil/load_workflow.py @@ -0,0 +1,22 @@ +import struct +import matplotlib.pyplot as plt +import pandas as pd +import numpy as np + +from read_and_write import FlattoArray + +base= r"../test_data/ttA_2877_NOP_atlasmaps/2877_NOP_tTA_lacZ_Xgal_s037_nl.flat" + +image_arr = FlattoArray(base) + +plt.imshow(FlattoArray(base)) + +"""assign label file values into image array""" +labelfile = pd.read_csv("../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 +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 diff --git a/PyNutil/read_and_write.py b/PyNutil/read_and_write.py index dc5fff0baaa769d6cb5d3dfd22027ebdba75f896..e4743294aabad54030064b73dc8daf6c82e13f54 100644 --- a/PyNutil/read_and_write.py +++ b/PyNutil/read_and_write.py @@ -1,5 +1,8 @@ import json import numpy as np +import struct +import pandas as pd +import matplotlib.pyplot as plt #related to read and write @@ -49,4 +52,27 @@ def WritePointsToMeshview(points, pointNames, 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 + df_to_save.to_csv(output_csv, sep=";", na_rep='', index= False) + + +def FlattoArray(flatfile): + """Read flat file and write into an np 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 + b,w,h=struct.unpack(">BII",f.read(9)) + #data is a one dimensional list of values + #it has the shape width times height + data =struct.unpack(">"+("xBH"[b]*(w*h)),f.read(b*w*h)) + + #convert flat file data into an array, previously data was a tuple + imagedata = np.array(data) + + #create an empty image array in the right shape, write imagedata into image_array + image = np.zeros((h,w)) + for x in range(w): + for y in range(h): + image[y,x] = imagedata[x+y*w] + + image_arr = np.array(image) + return image_arr \ No newline at end of file diff --git a/PyNutil/testing_openflatfile.py b/PyNutil/testing_openflatfile.py index a8ff861821a22e44ef70e176ba0edc80de61aa32..59bc5f332d4601e3c8e7066cbe33bb432a2dea83 100644 --- a/PyNutil/testing_openflatfile.py +++ b/PyNutil/testing_openflatfile.py @@ -12,40 +12,42 @@ with open(base,"rb") as f: b,w,h=struct.unpack(">BII",f.read(9)) #data is a one dimensional list of values #it has the shape width times height - data=struct.unpack(">"+("xBH"[b]*(w*h)),f.read(b*w*h)) + data =struct.unpack(">"+("xBH"[b]*(w*h)),f.read(b*w*h)) -#convert data into an array(this may be unnecessary) -#previously data was a tuple -data = np.array(data) + #convert flat file data into an array, previously data was a tuple + imagedata = np.array(data) -#here we create an empty image in the right shape -image = np.zeros((h,w)) -#pallette = dict(zip(np.unique(data), np.random.randint(0,255,len(np.unique(data))))) -#and here we go pixel by pixel placing the value from the flat file -for x in range(w): - for y in range(h): - image[y,x] = data[x+y*w] + #create an empty image array in the right shape, + image = np.zeros((h,w)) + #pallette = dict(zip(np.unique(data), np.random.randint(0,255,len(np.unique(data))))) -image_arr = np.array(image) + #assign values from flat file into the image array + for x in range(w): + for y in range(h): + image[y,x] = imagedata[x+y*w] -# show an image corresponding to the flat file (unique colour per idx) -plt.imshow(image_arr) + image_arr = np.array(image) + +# show image with plt.imshow(image_arr) + +"""assign label file values into image array""" labelfile = pd.read_csv(r"../annotation_volumes\allen2017_colours.csv") -allen_id_image = np.zeros((h,w)) -plt.imshow(allen_id_image) +allen_id_image = np.zeros((h,w)) # create an empty image array + """for ph in range(h): for pw in range(w): value_in_data_at_pixel = int(image_arr[ph,pw]) allen_id_image[ph, pw] = labelfile.loc[value_in_data_at_pixel, 'idx']""" -"""for efficiency, vectorize instead of using for loop""" +"""for efficiency, vectorize instead of using the for loops above""" coordsy, coordsx = np.meshgrid(list(range(w)), list(range(h))) - -values = image_arr[coordsx, coordsy] +values = image_arr[coordsx, coordsy] # assign x,y coords from image_array into values lbidx = labelfile['idx'].values -allen_id_image = lbidx[values.astype(int)] +allen_id_image = lbidx[values.astype(int)] # assign allen IDs into image array + +"""count pixels for unique idx""" unique_ids, counts = np.unique(allen_id_image, return_counts=True) area_per_label = list(zip(unique_ids,counts))