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

Create flat to array function

parent f9f2acd2
No related branches found
No related tags found
No related merge requests found
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]
......
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
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
......@@ -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))
......
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