diff --git a/PyNutil/counting_and_load.py b/PyNutil/counting_and_load.py index f5b1f8dba50e81fab7d13622bf2db89a5de5fd06..77a0fd98e31206649e10f32b21b4cc72b66ae2f7 100644 --- a/PyNutil/counting_and_load.py +++ b/PyNutil/counting_and_load.py @@ -1,6 +1,9 @@ import numpy as np import pandas as pd import struct +import matplotlib.pyplot as plt +import os +import nrrd # related to counting and load @@ -114,7 +117,7 @@ def pixel_count_per_region( """Read flat file and write into an np array""" -def flat_to_array(flat_file): +def flat_to_array(flat_file, labelfile): with open(flat_file, "rb") as f: # I don't know what b is, w and h are the width and height that we get from the # flat file header @@ -133,8 +136,28 @@ def flat_to_array(flat_file): image[y, x] = image_data[x + y * w] image_arr = np.array(image) - return image_arr + #return image_arr + """assign label file values into image array""" + labelfile = pd.read_csv(labelfile) + 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)] + return allen_id_image + +#def count_per_uniqueidx() + + """count pixels for unique idx""" + unique_ids, counts = np.unique(allen_id_image, return_counts=True) + + area_per_label = list(zip(unique_ids, counts)) + # create a list of unique regions and pixel counts per region + + df_area_per_label = pd.DataFrame(area_per_label, columns=["idx", "area_count"]) + # create a pandas df with regions and pixel counts + return(df_area_per_label) # Import flat files, count pixels per label, np.unique... etc. nitrc.org/plugins/mwiki/index.php?title=visualign:Deformation @@ -146,3 +169,4 @@ def flat_to_array(flat_file): b,w,h=struct.unpack(">BII",f.read(9)) data=struct.unpack(">"+("xBH"[b]*(w*h)),f.read(b*w*h)) """ + diff --git a/messing_around_files/load_workflow.py b/PyNutil/load_workflow.py similarity index 77% rename from messing_around_files/load_workflow.py rename to PyNutil/load_workflow.py index 1d7d4131b27fd525829313c9180f9223936ea640..ca271c976ae2ae5d0bf5a29ba0b7bb9fe589e994 100644 --- a/messing_around_files/load_workflow.py +++ b/PyNutil/load_workflow.py @@ -5,26 +5,18 @@ import matplotlib.pyplot as plt import pandas as pd import numpy as np -from read_and_write import FlattoArray, LabeltoArray +#from read_and_write import flat_to_array, label_to_array +from counting_and_load import flat_to_array base = r"../test_data/tTA_2877_NOP_s037_atlasmap/2877_NOP_tTA_lacZ_Xgal_s037_nl.flat" label = r"../annotation_volumes\allen2017_colours.csv" -image_arr = FlattoArray(base) +#image_arr = flat_to_array(base, label) +plt.imshow(flat_to_array(base, label)) -plt.imshow(FlattoArray(base)) -"""assign label file values into image array""" -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 -lbidx = labelfile["idx"].values -allen_id_image = lbidx[values.astype(int)] # assign allen IDs into image array -plt.imshow(allen_id_image) - -"""count pixels for unique idx""" +"""count pixels in np array for unique idx, return pd df""" unique_ids, counts = np.unique(allen_id_image, return_counts=True) area_per_label = list(zip(unique_ids, counts)) @@ -34,6 +26,7 @@ df_area_per_label = pd.DataFrame(area_per_label, columns=["idx", "area_count"]) # create a pandas df with regions and pixel counts + """add region name and colours corresponding to each idx into dataframe. This could be a separate function""" diff --git a/PyNutil/read_and_write.py b/PyNutil/read_and_write.py index a662371a333bfb5f30d8f859829e2f712ff741f8..db9be5db3597a394623781e8249df8b808769d60 100644 --- a/PyNutil/read_and_write.py +++ b/PyNutil/read_and_write.py @@ -60,8 +60,8 @@ def save_dataframe_as_csv(df_to_save, output_csv): df_to_save.to_csv(output_csv, sep=";", na_rep="", index=False) -def flat_to_array(flatfile): - """Read flat file and write into an np array, return array""" +def flat_to_array(flatfile, labelfile): + """Read flat file, write into an np array, assign label file values, 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 @@ -80,7 +80,16 @@ def flat_to_array(flatfile): image[y, x] = imagedata[x + y * w] image_arr = np.array(image) - return image_arr + #return image_arr + + """assign label file values into image array""" + labelfile = pd.read_csv(labelfile) + 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)] + return allen_id_image def label_to_array(label_path, image_array): @@ -111,5 +120,6 @@ def files_in_directory(directory): def read_atlas_volume(atlas_volume_path): + """return data from atlas volume""" data, header = nrrd.read(atlas_volume_path) return data diff --git a/messing_around_files/testing_openflatfile.py b/messing_around_files/testing_openflatfile.py index 183e403e213e8a6754851414b08f634325c79d2b..1b076bac733d8eab0f14ca1cd27879730a22992e 100644 --- a/messing_around_files/testing_openflatfile.py +++ b/messing_around_files/testing_openflatfile.py @@ -38,7 +38,7 @@ with open(base, "rb") as f: """assign label file values into image array""" -labelfile = pd.read_csv(r"metadata/annotation_volumes\allen2017_colours.csv") +labelfile = pd.read_csv(r"annotation_volumes\allen2017_colours.csv") #labelfile[] print(list(zip(val,count)))