Skip to content
Snippets Groups Projects
Commit 593cddc3 authored by Sharon Yates's avatar Sharon Yates
Browse files

Functions flat_to_array

parent 2f6ef7c9
No related branches found
No related tags found
No related merge requests found
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))
"""
......@@ -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"""
......
......@@ -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
......@@ -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)))
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment