Commit b57258c6 authored by Oleg Dzhimiev's avatar Oleg Dzhimiev

pack tile class

parent e99ffdcc
#!/usr/bin/env python3
__copyright__ = "Copyright 2018, Elphel, Inc."
__license__ = "GPL-3.0+"
__email__ = "oleg@elphel.com"
import numpy as np
import xml.etree.ElementTree as ET
import ast
class PackingTable:
def __init__(self,filename,layers_of_interest):
e = ET.parse(filename).getroot()
#print(ET.tostring(e))
#reparsed = minidom.parseString(ET.tostring(e,""))
#print(reparsed.toprettyxml(indent="\t"))
# Parse xml:
# td = tmp_dict
td = {}
for table in e:
layer = table.get('layer')
td[layer] = []
for row in table:
# safe evaluation
td[layer].append(ast.literal_eval(row.text))
# order
LUT = []
for layer in layers_of_interest:
LUT.append(td[layer])
self.lut = LUT
# A tile consists of layers
# layer is packed from 9x9 to 25x1
def pack_layer(layer,lut_row):
#print(layer.shape)
t = layer.flatten()
out = np.array([])
# iterate through rows
for i in range(len(lut_row)):
val = 0
# process row value
for j in lut_row[i]:
if np.isnan(t[j[0]]):
val = np.nan
break
val += t[j[0]]*j[1]
out = np.append(out,val)
return out
# tile and lut already ordered and indices match
def pack_tile(tile,lut):
out = np.array([])
for i in range(len(lut)):
layer = pack_layer(tile[:,:,i],lut[i])
out = np.append(out,layer)
return out
#!/usr/bin/env python3
__copyright__ = "Copyright 2018, Elphel, Inc."
__license__ = "GPL-3.0+"
__email__ = "oleg@elphel.com"
'''
Open all tiffs in a folder, combine a single tiff from randomly selected
tiles from originals
'''
from PIL import Image
import os
import sys
import glob
import imagej_tiff as ijt
import pack_tile as pile
import numpy as np
import itertools
# USAGE: python3 test_3.py some-path
VALUES_LAYER_NAME = 'other'
try:
src = sys.argv[1]
except IndexError:
src = "."
tlist = glob.glob(src+"/*.tiff")
print("Found "+str(len(tlist))+" tiff files:")
print("\n".join(tlist))
''' WARNING, assuming:
- timestamps and part of names match
- layer order and names are identical
'''
# CONSTANTS
RADIUS = 1
LAYERS_OF_INTEREST = ['diagm-pair','diago-pair']
# open the first one to get dimensions and other info
tiff = ijt.imagej_tiff(tlist[0])
#del tlist[0]
# shape as tiles? make a copy or make writeable
# (242, 324, 9, 9, 5)
# get labels
labels = tiff.labels.copy()
labels.remove(VALUES_LAYER_NAME)
print("Image data layers: "+str(labels))
print("Values layer: "+str([VALUES_LAYER_NAME]))
# create copies
tiles = np.copy(tiff.getstack(labels,shape_as_tiles=True))
tiles_bkp = np.copy(tiles)
values = np.copy(tiff.getvalues(label=VALUES_LAYER_NAME))
print("Tiled tiff shape: "+str(tiles.shape))
# now generate a layer of indices to get other tiles
indices = np.random.random_integers(0,len(tlist)-1,size=(tiles.shape[0],tiles.shape[1]))
#print(indices.shape)
# counts tiles from a certain tiff
shuffle_counter = np.zeros(len(tlist),np.int32)
shuffle_counter[0] = tiles.shape[0]*tiles.shape[1]
for i in range(1,len(tlist)):
#print(tlist[i])
tmp_tiff = ijt.imagej_tiff(tlist[i])
tmp_tiles = tmp_tiff.getstack(labels,shape_as_tiles=True)
tmp_vals = tmp_tiff.getvalues(label=VALUES_LAYER_NAME)
#tmp_tiles =
#tiles[indices==i] = tmp_tiff[indices==i]
# straight and clear
# can do quicker?
for y,x in itertools.product(range(indices.shape[0]),range(indices.shape[1])):
if indices[y,x]==i:
tiles[y,x] = tmp_tiles[y,x]
values[y,x] = tmp_vals[y,x]
shuffle_counter[i] +=1
# check shuffle counter
for i in range(1,len(shuffle_counter)):
shuffle_counter[0] -= shuffle_counter[i]
print("Tiffs shuffle counter = "+str(shuffle_counter))
# test later
# now pack from 9x9 to 1x25
# tiles and values
# Parse packing table
# packing table name
ptab_name = "tile_packing_table.xml"
pt = pile.PackingTable(ptab_name,LAYERS_OF_INTEREST).lut
...@@ -12,79 +12,9 @@ import imagej_tiff as ijt ...@@ -12,79 +12,9 @@ import imagej_tiff as ijt
#tiff.show_images(['X-corr','Y-corr',0,2]) #tiff.show_images(['X-corr','Y-corr',0,2])
#plt.show() #plt.show()
import json
import xml.etree.ElementTree as ET
import xml.dom.minidom as minidom
import ast
import itertools import itertools
class PackingTable: import pack_tile as pile
def __init__(self,filename,layers_of_interest):
e = ET.parse(filename).getroot()
#print(ET.tostring(e))
#reparsed = minidom.parseString(ET.tostring(e,""))
#print(reparsed.toprettyxml(indent="\t"))
# Parse xml:
# td = tmp_dict
td = {}
for table in e:
layer = table.get('layer')
td[layer] = []
for row in table:
# safe evaluation
td[layer].append(ast.literal_eval(row.text))
# order
LUT = []
for layer in layers_of_interest:
LUT.append(td[layer])
self.lut = LUT
# A tile consists of layers
# layer is packed from 9x9 to 25x1
def pack_layer(layer,lut_row):
#print(layer.shape)
t = layer.flatten()
out = np.array([])
# iterate through rows
for i in range(len(lut_row)):
val = 0
# process row value
for j in lut_row[i]:
if np.isnan(t[j[0]]):
val = np.nan
break
val += t[j[0]]*j[1]
out = np.append(out,val)
return out
# tile and lut already ordered and indices match
def pack_tile(tile,lut):
out = np.array([])
for i in range(len(lut)):
layer = pack_layer(tile[:,:,i],lut[i])
out = np.append(out,layer)
return out
# hard coded layers names # hard coded layers names
#lst = ['diagm-pair','diago-pair'] #lst = ['diagm-pair','diago-pair']
...@@ -150,7 +80,7 @@ LAYERS_OF_INTEREST = ['diagm-pair','diago-pair'] ...@@ -150,7 +80,7 @@ LAYERS_OF_INTEREST = ['diagm-pair','diago-pair']
# MAIN # MAIN
# get packing table # get packing table
pt = PackingTable(ptab_name,LAYERS_OF_INTEREST).lut pt = pile.PackingTable(ptab_name,LAYERS_OF_INTEREST).lut
# get tiff # get tiff
tiff = ijt.imagej_tiff(tiff_name) tiff = ijt.imagej_tiff(tiff_name)
...@@ -170,7 +100,7 @@ for y,x in itertools.product(range(l.shape[0]),range(l.shape[1])): ...@@ -170,7 +100,7 @@ for y,x in itertools.product(range(l.shape[0]),range(l.shape[1])):
print(l) print(l)
l_packed = pack_layer(l,pt[0]) l_packed = pile.pack_layer(l,pt[0])
#print(l_packed.shape) #print(l_packed.shape)
#print(l_packed) #print(l_packed)
...@@ -191,7 +121,7 @@ ls = np.dstack((l1,l2)) ...@@ -191,7 +121,7 @@ ls = np.dstack((l1,l2))
#print(ls) #print(ls)
#print(ls.shape) #print(ls.shape)
l_packed = pack_tile(ls,pt) l_packed = pile.pack_tile(ls,pt)
#print(l_packed) #print(l_packed)
......
Markdown is supported
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