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

Working with flat files

parent 7202ac4c
No related branches found
No related tags found
No related merge requests found
Showing
with 1395 additions and 1330 deletions
...@@ -136,6 +136,8 @@ def FolderToAtlasSpace(folder, QUINT_alignment, pixelID=[0, 0, 0], nonLinear=Tru ...@@ -136,6 +136,8 @@ def FolderToAtlasSpace(folder, QUINT_alignment, pixelID=[0, 0, 0], nonLinear=Tru
points.extend(SegmentationToAtlasSpace(current_slice, SegmentationPath, pixelID, nonLinear)) points.extend(SegmentationToAtlasSpace(current_slice, SegmentationPath, pixelID, nonLinear))
return np.array(points) return np.array(points)
# points.append would make list of lists, keeping sections separate.
#related to coordinate extraction #related to coordinate extraction
#this function returns an array of points #this function returns an array of points
......
import sys
for arg in sys.argv:
if arg.startswith("flat="):
flatfile=arg[len("flat="):]
if arg.startswith("label="):
labelfile=arg[len("label="):]
if arg.startswith("json="):
jsonfile=arg[len("json="):]
if arg.startswith("output="):
outfile=arg[len("output="):]
if "flatfile" not in vars():
print("flat=<some .flat file> parameter missing")
sys.exit()
if "outfile" not in vars():
print("output=<new .png file> parameter missing")
sys.exit()
palette=False
if "labelfile" in vars():
import re
palette=[]
with open(labelfile) as f:
for line in f:
lbl=re.match(r'\s*\d+\s+(\d+)\s+(\d+)\s+(\d+)',line)
if lbl:
palette.append((int(lbl[1]),int(lbl[2]),int(lbl[3])))
print(f"{len(palette)} labels parsed")
elif "jsonfile" in vars():
import json
with open(jsonfile) as f:
palette=[(i["red"],i["green"],i["blue"]) for i in json.load(f)]
print(f"{len(palette)} labels loaded")
import struct
with open(flatfile,"rb") as f:
b,w,h=struct.unpack(">BII",f.read(9))
data=struct.unpack(">"+("xBH"[b]*(w*h)),f.read(b*w*h))
print(f"{b} bytes per pixel, {w} x {h} resolution")
import PIL.Image
image=PIL.Image.new("RGB" if palette else "L",(w,h))
for y in range(h):
for x in range(w):
image.putpixel((x,y),palette[data[x+y*w]] if palette else data[x+y*w] & 255)
image.save(outfile,"PNG")
\ No newline at end of file
base= r"../test_data\ttA_2877_NOP_atlasmaps\2877_NOP_tTA_lacZ_Xgal_s037_nl.flat"
import numpy as np
import struct
with open(base,"rb") as f:
b,w,h=struct.unpack(">BII",f.read(9))
data=struct.unpack(">"+("xBH"[b]*(w*h)),f.read(b*w*h))
from PIL import Image
import random
image= Image.new("RGB",(w,h))
for y in range(h):
for x in range(w):
image.putpixel((x,y),palette[d])
image_arr = np.array(image)
image_arr.min()
\ No newline at end of file
This diff is collapsed.
...@@ -20,7 +20,8 @@ reformat_allen_label("../annotation_volumes/itksnap_label_description_2022.txt", ...@@ -20,7 +20,8 @@ reformat_allen_label("../annotation_volumes/itksnap_label_description_2022.txt",
"""reformat AllenMouseBrain_atlas_CCF_2017.label""" """reformat AllenMouseBrain_atlas_CCF_2017.label"""
def reformat_label(inputpath, outputpath): def reformat_label(inputpath, outputpath):
df = pd.read_csv(inputpath, sep = "\t", header=None, skiprows=15 ,names=["idx", "r", "g", "b", "a", "VIS", "MSH", "name"] ) df = pd.read_csv(inputpath, sep = "\t", header=None, skiprows=15 ,names=["idx", "r", "g", "b", "a", "VIS", "MSH", "name"] )
df = df.append({"idx": 0, "name": "Clear Label", "r": 0, "g": 0, "b": 0, "a": 1.0, "VIS":1.0, "MSH":1.0}, ignore_index=True) df_clear = pd.DataFrame({"idx": 0, "name": "Clear Label", "r": 0, "g": 0, "b": 0, "a": 1.0, "VIS":1.0, "MSH":1.0}, ignore_index=True)
df = pd.concat([df_clear, df])
df.to_csv(outputpath, index=False) df.to_csv(outputpath, index=False)
reformat_label("../annotation_volumes/AllenMouseBrain_Atlas_CCF_2017.label","../annotation_volumes/allen2017_colours.csv") reformat_label("../annotation_volumes/AllenMouseBrain_Atlas_CCF_2017.label","../annotation_volumes/allen2017_colours.csv")
......
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment