Skip to content

Commit

Permalink
Fill with zeros if tiles are missing
Browse files Browse the repository at this point in the history
  • Loading branch information
bradleysamuel committed Sep 10, 2024
1 parent 1c68ecf commit a702f10
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 55 deletions.
101 changes: 55 additions & 46 deletions zetastitcher/align/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,52 +145,61 @@ def worker(item, overlap_dict, channel, max_dz, max_dy, max_dx):
axis = item['axis']
overlap = overlap_dict[axis]

a = InputFile(aname)
b = InputFile(bname)

z_min = z_frame - max_dz
z_max = z_frame + max_dz + 1

aslice = a.zslice(z_min, z_max, copy=True)
if a.nchannels > 1:
if channel is not None:
aslice = aslice[:, channel]
else:
aslice = np.sum(aslice.astype(np.float32), axis=1)
if axis == 2:
aslice = np.rot90(aslice, axes=(-1, -2))
aslice = aslice[..., -(overlap + max_dy):, :]

bframe = b.zslice_idx(z_frame, copy=True)
if b.nchannels > 1:
if channel is not None:
bframe = bframe[:, channel]
else:
bframe = np.sum(bframe.astype(np.float32), axis=1)
if axis == 2:
bframe = np.rot90(bframe, axes=(-1, -2))
bframe = bframe[..., :overlap - max_dy, :]

aslice = aslice.astype(np.float32)
bframe = bframe.astype(np.float32)

output_shape = np.array(aslice.shape) + np.array((0, 0, 2 * max_dx)) - np.array(bframe.shape) + 1
output_shape[0] = aslice.shape[0]
xcorr = np.zeros(output_shape)

for i in range(xcorr.shape[0]):
cc, max_loc = align_dog(aslice[i], bframe[0], 0, max_dx)
xcorr[i] = cc

shift = list(np.unravel_index(np.argmax(xcorr), xcorr.shape))
score = xcorr[tuple(shift)]
if score < 0 or score > 1:
score = 0

item['score'] = score
item['dz'] = shift[0]
item['dy'] = shift[1]
item['dx'] = shift[2]
if aname.startswith("Empty") or bname.startswith("Empty"):

item['score'] = 0
item['dz'] = 0
item['dy'] = 0
item['dx'] = 0

else:

a = InputFile(aname)
b = InputFile(bname)

z_min = z_frame - max_dz
z_max = z_frame + max_dz + 1

aslice = a.zslice(z_min, z_max, copy=True)
if a.nchannels > 1:
if channel is not None:
aslice = aslice[:, channel]
else:
aslice = np.sum(aslice.astype(np.float32), axis=1)
if axis == 2:
aslice = np.rot90(aslice, axes=(-1, -2))
aslice = aslice[..., -(overlap + max_dy):, :]

bframe = b.zslice_idx(z_frame, copy=True)
if b.nchannels > 1:
if channel is not None:
bframe = bframe[:, channel]
else:
bframe = np.sum(bframe.astype(np.float32), axis=1)
if axis == 2:
bframe = np.rot90(bframe, axes=(-1, -2))
bframe = bframe[..., :overlap - max_dy, :]

aslice = aslice.astype(np.float32)
bframe = bframe.astype(np.float32)

output_shape = np.array(aslice.shape) + np.array((0, 0, 2 * max_dx)) - np.array(bframe.shape) + 1
output_shape[0] = aslice.shape[0]
xcorr = np.zeros(output_shape)

for i in range(xcorr.shape[0]):
cc, max_loc = align_dog(aslice[i], bframe[0], 0, max_dx)
xcorr[i] = cc

shift = list(np.unravel_index(np.argmax(xcorr), xcorr.shape))
score = xcorr[tuple(shift)]
if score < 0 or score > 1:
score = 0

item['score'] = score
item['dz'] = shift[0]
item['dy'] = shift[1]
item['dx'] = shift[2]

return item

Expand Down
42 changes: 39 additions & 3 deletions zetastitcher/align/filematrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import json
import yaml
import itertools

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -181,9 +182,43 @@ def process_data_frame(self):
n_of_files = len(df.index)

if xsize * ysize != n_of_files:
msg = 'Mosaic is {}x{} tiles, but there are {} files!'.format(
xsize, ysize, n_of_files)
raise ValueError(msg)
logger.info('Mosaic is {}x{} tiles, but there are {} files! Adding empty tiles to fill spaces!'.format(
xsize, ysize, n_of_files))
x_values = df['X'].unique()
y_values = df['Y'].unique()

combinations = list(itertools.product(x_values, y_values))

existing_combinations = df[['X', 'Y']].apply(tuple, axis=1)
missing_combinations = [comb for comb in combinations if comb not in existing_combinations.tolist()]

missing_data = pd.DataFrame(missing_combinations, columns=['X', 'Y'])
missing_data['filename'] = None
nfrms_value = df['nfrms'].max()
missing_data['nfrms'] = nfrms_value
missing_data['Z'] = 0.0
missing_data['ysize'] = 0
missing_data['xsize'] = 0

df = pd.concat([df.reset_index(), missing_data], ignore_index=True)

sorted_x_values = sorted(df['X'].unique())
sorted_y_values = sorted(df['Y'].unique())

def compute_size(value, sorted_values, column_name):
return df.loc[df['X'] == value, column_name].max() if column_name == 'xsize' else df.loc[df['Y'] == value, column_name].max()

added_rows = df['filename'].isna()

df.loc[added_rows, 'xsize'] = df.loc[added_rows, 'X'].apply(lambda x: compute_size(x, sorted_x_values, 'xsize'))
df.loc[added_rows, 'ysize'] = df.loc[added_rows, 'Y'].apply(lambda y: compute_size(y, sorted_y_values, 'ysize'))

def generate_filename(row):
return f"Empty_x_{int(row['X'])}_y_{int(row['Y'])}_z_{int(row['Z'])}"

df.loc[added_rows, 'filename'] = df.loc[added_rows].apply(generate_filename, axis=1)

df.set_index('filename', inplace=True)

keys = ['X', 'Y', 'Z']
df[keys] -= df[keys].min()
Expand All @@ -194,6 +229,7 @@ def process_data_frame(self):
df[key] -= df[key].min()

df = df.sort_values(['Z', 'Y', 'X'])
self.data_frame = df
self.compute_end_pos()
self.name_array = np.array(df.index.values).reshape(self.Ny, self.Nx)

Expand Down
28 changes: 22 additions & 6 deletions zetastitcher/fuse/virtual_fused_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ def __init__(self, file_or_matrix):

self._debug = False

infile = os.path.join(self.path, self.fm.data_frame.iloc[0].name)
i = 0

while self.fm.data_frame.iloc[i].name.startswith("Empty"):
i += 1

infile = os.path.join(self.path, self.fm.data_frame.iloc[i].name)
with InputFile(infile) as f:
self.dtype = f.dtype
self.nchannels = f.nchannels
Expand All @@ -97,7 +102,12 @@ def shape(self):
"""
thickness = self.fm.full_thickness

infile = os.path.join(self.path, self.fm.data_frame.iloc[0].name)
i = 0

while self.fm.data_frame.iloc[i].name.startswith("Empty"):
i += 1

infile = os.path.join(self.path, self.fm.data_frame.iloc[i].name)
with InputFile(infile) as f:
output_shape = list(f.shape)

Expand Down Expand Up @@ -233,10 +243,16 @@ def __getitem__(self, item):

for index, Xs, sl in self._my_gen(df, X_min, X_stop, steps, myitem[:]):
logger.info('loading {}\t{}'.format(index, sl))
with InputFile(os.path.join(self.path, index)) as f:
temp_shape = list(f.shape)
f.squeeze = False
sl_a = np.copy(f[tuple(sl)]).astype(dtype)

if index.startswith("Empty"):
temp_shape = df.loc[index, ['nfrms', 'ysize', 'xsize']].astype(int).tolist()
shape = tuple((s.stop - s.start) // (s.step or 1) for s in sl)
sl_a = np.zeros(shape)
else:
with InputFile(os.path.join(self.path, index)) as f:
temp_shape = list(f.shape)
f.squeeze = False
sl_a = np.copy(f[tuple(sl)]).astype(dtype)

z_from = sl[0].start
z_to = sl[0].stop
Expand Down

0 comments on commit a702f10

Please sign in to comment.