Commit e0fbbfe3 authored by Sean Fitzgibbon's avatar Sean Fitzgibbon
Browse files

some refactoring

parent 1934cca3
......@@ -130,15 +130,15 @@ def register_chart_to_slide(chart, slide, slide_res, outdir, boundary_key=None,
# initial scaling based on boundng boxes
init = init_scale(img, slide_res, edge_crds)
init_xfm = init_scale(img, slide_res, edge_crds)
print(init)
print(init_xfm)
print(
f"Rotation: {init.rotation}, Translation: {init.translation}, Scale: {init.scale}"
f"Rotation: {init_xfm.rotation}, Translation: {init_xfm.translation}, Scale: {init_xfm.scale}"
)
# calculate normal line to boundary points
xfm_edge_coords = apply_xfm(init, edge_crds)
xfm_edge_coords = apply_xfm(init_xfm, edge_crds)
init_nrmls = normal(xfm_edge_coords)
# refine edge_coords (to image)
......@@ -147,20 +147,20 @@ def register_chart_to_slide(chart, slide, slide_res, outdir, boundary_key=None,
)
# estimate opimised affine transform
opt = transform.SimilarityTransform()
opt.estimate(edge_crds[exclude_mask, :], refined_edge_coords[exclude_mask, :])
print(opt)
opt_xfm = transform.SimilarityTransform()
opt_xfm.estimate(edge_crds[exclude_mask, :], refined_edge_coords[exclude_mask, :])
print(opt_xfm)
print(
f"Rotation: {opt.rotation}, Translation: {opt.translation}, Scale: {opt.scale}"
f"Rotation: {opt_xfm.rotation}, Translation: {opt_xfm.translation}, Scale: {opt_xfm.scale}"
)
# # save opt transform
# np.savetxt(f"{out}/chart-to-image.xfm", opt.params)
np.savetxt(f"{outdir}/chart-to-image.xfm", opt_xfm.params)
# apply opt-xfm to contours and cells and save
contour_xfm = [
(contour.name, apply_xfm(opt, contour.points[:, :2] * [1, -1]).tolist(), contour.closed) for contour in chart.contours
(contour.name, apply_xfm(opt_xfm, contour.points[:, :2] * [1, -1]).tolist(), contour.closed) for contour in chart.contours
]
if do_plots:
......@@ -202,7 +202,7 @@ def register_chart_to_slide(chart, slide, slide_res, outdir, boundary_key=None,
if chart.n_cells > 0:
cells = np.concatenate([cell.point[:2][np.newaxis, :] for cell in chart.cells]) * [1, -1]
cells_xfm = apply_xfm(init, cells )
cells_xfm = apply_xfm(init_xfm, cells )
with open(f"{outdir}/cells.json", "w") as fp:
json.dump(cells_xfm.tolist(), fp, indent=4)
......@@ -296,15 +296,9 @@ def refine_edge_coord(img, img_res, edge_coords, normals, do_plots=True):
line_int = brainmask[line_y, line_x]
# line_int = img[line_y, line_x]
# exlude constant rows
constant_idx = np.all(line_int == line_int[:, 0][:, np.newaxis], axis=1)
# line_int = line_int[~constant_idx, :]
# print(line_int)
min_idx = np.argmax(np.abs(np.diff(line_int, axis=-1)), axis=-1)
......@@ -316,12 +310,6 @@ def refine_edge_coord(img, img_res, edge_coords, normals, do_plots=True):
axis=-1,
)
print(constant_idx.shape)
print(refined_edge_coords.shape)
# refined_edge_coords = refined_edge_coords[~constant_idx, :]
print(refined_edge_coords.shape)
# TODO: plot edge_coords + refined_edge_coords
if do_plots:
fig, ax = plt.subplots(figsize=(20, 30))
......
......@@ -238,7 +238,7 @@ def parse_contour(c0):
return c0_dict
def parse_cell(cell):
def parse_asterisk(cell):
'''Convert cell (asterisk) section into a dictionary'''
cell_dict = {}
......@@ -257,18 +257,18 @@ def parse_cell(cell):
def to_data(sections):
# convert sections to dicts
cells = []
contour = []
asterisks = []
contours = []
for section in sections:
if section[0] == 'Asterisk': # Cell
cells.append(parse_cell(section))
asterisks.append(parse_asterisk(section))
else:
contour.append(parse_contour(section))
contours.append(parse_contour(section))
if len(cells)>0: cells = np.asarray(cells)
if len(asterisks)>0: asterisks = np.asarray(asterisks)
return contour, cells
return contours, asterisks
def read(file):
......@@ -277,7 +277,7 @@ def read(file):
with open(file, encoding='utf-8', errors='replace') as fd:
sections = _parse_sections(fd)
contour, cells = to_data(sections)
return contour, cells
contour, asterisk = to_data(sections)
return contour, asterisk
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