-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstitchMstClasses.py
More file actions
1297 lines (1046 loc) · 46.5 KB
/
stitchMstClasses.py
File metadata and controls
1297 lines (1046 loc) · 46.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
STITCH
------
using MINIMUM SPANNING TREE for mosaic assembly
Class for stitching a regularly-spaced square grid of images
built on Mike's stitching code,
the window-based covariance checking is replaced with
phase correlation over the full overlapping region
REQUIRES stage positions to be provided for every image being stitched
Currently, this is found in the .xml or .inf files associated with each image
other useful parameters for our microscopes:
scalefactor = 7.678 # nemo & dory
theta = 0.0275 # nemo
theta = -0.0435 # dory
nigel 10 apr 2019
added minimum spanning tree and hdf5 image storage - 15 aug 19 nigel
License and readme found in https://github.com/khchenLab/split-fish
"""
import numpy as np
import os
import json
import h5py
import re
import pprint as pp
from collections import defaultdict
import datetime
from typing import Union, Tuple
import pandas as pd
# plotting
import seaborn as sns
import matplotlib.pyplot as plt
# SciPy
from scipy.spatial import cKDTree
import scipy.ndimage.interpolation as interpolation
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import minimum_spanning_tree
# SciKit-Image
# from skimage.feature import register_translation
from utils.registrationFunctions import register_translation
# for File Dialog Window
import tkinter as tk
from tkinter import filedialog
from utils.readClasses import readDoryImg, readSpongebobImg
from utils.writeClasses import DaxWriter
from fileparsing.filesClasses import getFileParser
from fileparsing.fovGridFunctions import generateFovGrid
class Stitch(object):
def __init__(self,
microscope_type: str = "Dory", # either "Dory" or "spongebob", case insensitive
data_path: str = None, # path where all the image files are stored
output_subdir: str = "output", # subfolder where output (hdf5file, shifts.csv etc.) will be stored
processed_path: str = None,
basebit: Union[int, None] = None,
basetype: Union[str, None] = None, # the base type to use for stitching
basehyb: Union[int, None] = None, # the base hyb to use for stitching
roi: Union[int, None] = None,
stage_pixel_matrix: np.ndarray = np.array([[0, -8], [-8, 0]]),
# scaling factors for Nemo & Dory are in num pixels per micron - equivalent to 130 nm / pixel
theta: float = 0, # camera angle: -0.016 dory default, 0.0275 for nemo
include_fovs: list = None, # only use these fovs (if given, exclude fovs are ignored)
exclude_fovs: list = None, # exclude these fovs but use all others
):
self.microscope_type = microscope_type.lower()
self.data_path = data_path
self.processed_path = processed_path
self.roi = roi
self.stage_pixel_matrix = stage_pixel_matrix
self.theta = theta
# Specify which bit or hyb/type to stitch by
# ------------------------------------------
self.basebit = basebit
self.basetype = basetype
self.basehyb = basehyb
if basebit is not None:
bit_str = f"bit{basebit}"
elif basetype is not None and basehyb is not None:
bit_str = f"hyb{basehyb}_{basetype}_"
else:
raise ValueError(
f"Need to choose which image to stitch by. "
f"To specify this, provide either\n"
f" (1) bit \n"
f" (2) hyb + type\n"
)
# Set stitched mosaic output path
# -------------------------------
self.script_time = datetime.datetime.now()
time_str = self.script_time.strftime("%Y%m%d_%H%M")
self.stitched_path = os.path.join(
self.data_path, output_subdir, f"stitched_" + bit_str + time_str
)
if not os.path.isdir(self.stitched_path):
os.mkdir(self.stitched_path)
# initialize h5py file
h5_filepath = os.path.join(self.stitched_path, "stitched.hdf5")
self.h5file = h5py.File(h5_filepath, "a")
# initialize attributes that will be filled later
# -----------------------------------------------
self.have_read_images = False
self.edge_dict = None
self.min_span_tree = None
self.mst_by_row = None
self.stitched_fovcoord_df = None # dataframe of FOV coordinates
print(
"-" * 90 +
f"\nInitializing stitch object ({bit_str})...\n"
+ "-" * 90 + "\n"
)
# Parser object to parse filenames in directory
# ---------------------------------------------
self.parser = getFileParser(data_path, self.microscope_type, )
self.files_df_roi = self.parser.roiSubsetOfFilesDF(self.roi)
self.files_dict = {}
if basebit is not None:
# Find all imagedata.hdf5 files in data folder (if using bit)
# -----------------------------------------------------------
imagedata_pattern = re.compile(
r"fov_(\d+|\d+_\d+)_imagedata_iter(\d+).hdf5", flags=re.IGNORECASE,
)
for file in os.listdir(processed_path):
match = re.match(imagedata_pattern, file)
if match:
imagedata_filepath = os.path.join(processed_path, file)
self.files_dict[match.group(1)] = (imagedata_filepath, None)
# check if we found the files
assert len(self.files_dict) > 0, (
f"No valid imagedata hdf5 files found in folder"
)
# set base hyb and type for grid generation
# (choose first entry of dataframe if not provided)
if self.basehyb is None:
self.basehyb = int(self.files_df_roi["hyb"].values[0])
if self.basetype is None:
self.basetype = str(self.files_df_roi["type"].values[0])
else:
# Filter the dataframe (if using hyb/type combo)
# ----------------------------------------------
# include just the rows matching the basetype and hyb we want
files_df_stitching_subset = self.files_df_roi[
(self.files_df_roi["type"] == self.basetype) &
(self.files_df_roi["hyb"] == self.basehyb)
]
print(f"Truncated files dictionary:\n {files_df_stitching_subset}")
# format: {FOV: (relative filepath, tiff frame), ...}
for index, row in files_df_stitching_subset.iterrows():
self.files_dict[f"{row['fov']}"] = (
os.path.join(data_path, row["file_name"]),
row["tiff_frame"],
)
# check if we found the files
assert len(self.files_dict) > 0, (
f"No Files matching type {self.basetype} and hyb {self.basehyb} "
f"found in data folder!\n\n"
f"Alternatives to try: \n"
f"types:\t{self.parser.files_df['type'].unique()}\n"
f"hybs:\t{self.parser.files_df['hyb'].unique()}\n"
)
# Get the subset of FOVs to be stitched
# -------------------------------------
# list of all FOVs in data path
self.fovs = list(self.files_dict.keys())
if include_fovs is not None:
self.fov_subset = include_fovs
elif exclude_fovs is not None:
self.fov_subset = [
fov for fov in self.fovs if fov not in exclude_fovs
]
else:
self.fov_subset = self.fovs
print(
f"List of FOVs in data folder:\n{self.fovs}\n"
f"List of FOVs to stitch:\n{self.fov_subset}\n"
)
assert len(self.fov_subset) > 0, (
f"FOVs selected do not match any in folder"
)
print(
"Files to process:\n", json.dumps(self.files_dict, indent=2), "\n"
)
# Get FOV grid and stage/image coordinates
# ----------------------------------------
(self.fov_grid,
self.image_coords,
self.stage_coords) = generateFovGrid(
self.files_df_roi,
stage_pixel_matrix,
fov_subset=self.fov_subset,
hyb=self.basehyb,
colour_type=self.basetype,
plot_grid=False,
)
# flat version of fov grid for edge matrix
self.fov_grid_flat = self.fov_grid.flat
# maximum extents in x and y of the grid
self.y_gridmax = self.fov_grid.shape[0]
self.x_gridmax = self.fov_grid.shape[1]
# Read the images and record image dimensions
# -------------------------------------------
self.imgs = self.readImages()
# (assume all images have same dimensions)
self.ydim = self.imgs[self.fovs[0]].shape[0] # y dimensions of images
self.xdim = self.imgs[self.fovs[0]].shape[1] # x dimensions of images
print(f"Images have dimensions {self.ydim} x {self.xdim}\n")
def __enter__(self):
"""
return the instance
"""
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""
close hdf5 file and save stitched FOV coordinates as a .csv file.
"""
self.h5file.close()
# save the calculated FOV coordinates to a csv file
if self.stitched_fovcoord_df is not None:
self.saveFovCoords()
def readImages(self,
verbose: bool = True,
) -> dict:
"""
returns a dictionary of references
to newly-created h5py datasets for each FOV
{FOV: h5py dataset, ...}
"""
images_dict = {}
for fov in self.fovs:
filepath = self.files_dict[fov][0]
dataset_found = ""
if self.basebit is not None:
# Read basebit of imagedatafile
with h5py.File(filepath, "a") as imgdata_h5:
img = None
datasets_to_check = ["fieldcorr", "raw"]
for dataset in datasets_to_check:
if dataset in imgdata_h5:
img = np.array(imgdata_h5[dataset][..., self.basebit])
# max intensity projection along all frames
# (currently should only have 1 frame)
img = np.nanmax(img, axis=0)
dataset_found = dataset
break
if img is None:
raise FileNotFoundError(
f"Could not find any of {datasets_to_check} arrays "
f"in {filepath}.\n"
)
else:
# For Dory/Nemo | .dax file format images
# ---------------------------------------
if self.microscope_type in ["dory", "nemo"]:
img = readDoryImg(filepath)
# For spongebob | .tiff multiframe file format images
# ------------------------------------------------
elif self.microscope_type == "spongebob":
tiff_frame = self.files_dict[fov][1]
img = readSpongebobImg(filepath, tiff_frame)
else:
raise ValueError(
f"Microscope type {self.microscope_type} not recognised. "
f"Must be dory, nemo or spongebob.\n"
)
# write image to hdf5 file
# ------------------------
images_dict[fov] = self.h5file.create_dataset(
fov, data=img,
)
if verbose:
print(f"Read: {self.files_dict[fov]}\n"
f" at FOV {fov} {dataset_found} which has\n"
f" -stage coords {self.stage_coords[fov]}\n"
f" -image coords {self.image_coords[fov]}\n")
assert len(images_dict) > 0, "Unable to load images"
self.have_read_images = True
return images_dict
#
# ---------------------------------------------------------------------------------------
# Image pairs Alignment
# ---------------------------------------------------------------------------------------
#
def getShift(self,
fov1: str, fov2: str, # the FOVs to be aligned
vertical: bool = False,
verbose: bool = True,
check_alignment: bool = False,
):
"""
get the relative shift between 2 fovs
starting with shifts derived from stage movement
and performing registration to correct for stage errors
"""
return self._correctShift(
np.array(self.imgs[fov1]),
np.array(self.imgs[fov2]),
*self._findShifts(fov1, fov2),
check_alignment=check_alignment,
vertical=vertical,
verbose=verbose,
)
def _findShifts(self,
fov1: str, fov2: str, # the FOVs to be aligned
verbose: bool = True,
) -> Tuple[float, float]:
"""
Find expected shift from STAGE POSITIONS *before* image registration
calculates the shift between 2 fovs,
with correction for slight camera rotation (theta)
returns a tuple of (y-shift, x-shift)
"""
yshift = self.image_coords[fov2][0] - self.image_coords[fov1][0]
xshift = self.image_coords[fov2][1] - self.image_coords[fov1][1]
# correct for rotation
# --------------------
# (positive theta is anticlockwise)
xshift_corrected = int(round(xshift * np.cos(self.theta) - yshift * np.sin(self.theta)))
yshift_corrected = int(round(xshift * np.sin(self.theta) + yshift * np.cos(self.theta)))
if verbose:
print(f"___ Finding shifts between {fov1} and FOV {fov2}: ___\n"
f"raw shifts:\t\tY = {yshift}, X = {xshift}\n"
f"corrected shifts:\tY = {yshift_corrected}, X = {xshift_corrected}")
return yshift_corrected, xshift_corrected
def _getOvelapRegions(self,
img1, img2,
yshiftstart, xshiftstart,
vertical=False,
check_dims_match=True,
):
"""
_______________
| |#| |
| img1 |#| img2 |
| |#| |
|______|_|______| vertical = False
______
| img1 |
|______|
|######|
|------|
| img2 |
|______| vertical = True
"""
# This is a special case which is the same for vertical and horizontal
if xshiftstart > 0 and yshiftstart > 0:
overlap_region_1 = img1[yshiftstart:, xshiftstart:]
overlap_region_2 = img2[:-yshiftstart, :-xshiftstart]
else:
if vertical: # img2 BELOW img 1
assert yshiftstart > 0, "image2 must be below image1"
if xshiftstart == 0:
overlap_region_1 = img1[yshiftstart:, :]
overlap_region_2 = img2[:-yshiftstart, :]
elif xshiftstart < 0:
overlap_region_1 = img1[yshiftstart:, 0:xshiftstart]
overlap_region_2 = img2[:-yshiftstart, -xshiftstart:]
else: # img2 to the RIGHT of img1
assert xshiftstart > 0, "image2 must be to the right of image1"
if yshiftstart == 0:
overlap_region_1 = img1[:, xshiftstart:]
overlap_region_2 = img2[:, :-xshiftstart]
elif yshiftstart < 0:
overlap_region_1 = img1[0:yshiftstart, xshiftstart:]
overlap_region_2 = img2[-yshiftstart:, :-xshiftstart]
if check_dims_match:
assert np.array_equal(overlap_region_1.shape,
overlap_region_2.shape), (
f"Overlapping region dimensions {overlap_region_1.shape} "
f"and {overlap_region_2.shape} do not match")
return overlap_region_1, overlap_region_2
def _correctShift(self,
img1, img2, # the 2 images
yshiftstart, xshiftstart, # the estimated y and x shifts to start with
vertical=False,
verbose=True,
check_alignment=True,
display_vmax=20000,
):
"""
refines the xshift and yshift between two images
using some image registration algorithm
currently implemented:
(1) Phase correlation (Scikit image version, slightly modded)
starts with an initial shift estimate (from stage shifts)
image2 must be to the right of image1
________
_______|_ |
| || img2 |
| img1 ||_______|
|________| | y shift
-------> x shift v
"""
(overlap_region_1,
overlap_region_2) = self._getOvelapRegions(img1, img2,
yshiftstart, xshiftstart,
vertical=vertical,
check_dims_match=True)
shift, error, diffphase = register_translation(overlap_region_1,
overlap_region_2,
50)
if verbose:
print("Shift between overlap regions: {}, {}".format(*shift))
if check_alignment:
# _____ original regions _____
if vertical:
figure_overlap, axes_overlap = plt.subplots(4, 1,
figsize=(9, 9))
else:
figure_overlap, axes_overlap = plt.subplots(1, 4,
figsize=(11, 9))
axes_overlap.flat[0].imshow(overlap_region_1, vmax=display_vmax)
axes_overlap.flat[1].imshow(overlap_region_2, vmax=display_vmax)
# _____ corrected regions _____
yshift_corrected = yshiftstart + int(shift[0])
xshift_corrected = xshiftstart + int(shift[1])
(overlap_region_1_updated,
overlap_region_2_updated) = self._getOvelapRegions(img1, img2,
yshift_corrected,
xshift_corrected,
vertical=vertical,
check_dims_match=True)
axes_overlap.flat[2].imshow(overlap_region_1_updated, vmax=display_vmax)
axes_overlap.flat[3].imshow(overlap_region_2_updated, vmax=display_vmax)
figure_overlap.suptitle("Original (1/2) and Corrected (3/4) overlap regions")
return shift + (yshiftstart, xshiftstart), error
#
# ---------------------------------------------------------------------------------------
# Mosaic Assembly methods
# ---------------------------------------------------------------------------------------
#
def _getNeighbours(self,
grid_position, # array-like, length 2
):
"""
get surrounding filled grid positions
for a FOV in the FOV grid
returns: (top, bottom, left, right)
FOV reference str, or
None if at the edge or if no FOV present at that position
"""
assert len(grid_position) == 2, (
f"Grid position was given {len(grid_position)} arguments."
f"Must be (y position, x position)"
)
neighbours = [None, ] * 4
# Top
# ---
if grid_position[0] != 0:
neighbours[0] = self.fov_grid[grid_position[0] - 1,
grid_position[1]]
# Bottom
# ------
if grid_position[0] != self.y_gridmax - 1:
neighbours[1] = self.fov_grid[grid_position[0] + 1,
grid_position[1]]
# Left
# ----
if grid_position[1] != 0:
neighbours[2] = self.fov_grid[grid_position[0],
grid_position[1] - 1]
# Right
# -----
if grid_position[1] != self.x_gridmax - 1:
neighbours[3] = self.fov_grid[grid_position[0],
grid_position[1] + 1]
# if no-image detected in adjacent position, change id to None
return [nb if nb != "noimage" else None for nb in neighbours]
def _getAllNeighbours(self,
verbose=True,
):
"""
generate a dictionary of
connected neighbours for every filled FOV
format: { fov : [top, bottom, left, right], ... }
"""
nb_dict = {}
for y_pos in range(self.y_gridmax):
for x_pos in range(self.x_gridmax):
fov = self.fov_grid[y_pos, x_pos]
if fov != "noimage":
nb_dict[fov] = self._getNeighbours((y_pos, x_pos))
if verbose:
print("____ Neighbours dictionary: ____")
pp.pprint(nb_dict)
return nb_dict
def _plotEdges(self,
edge_dict, # dictionary of edges
pairs: list, # list of pairs
ax, # axes to plot on
weight_norm=1, # normalize weights by this
linecolour="blue",
linestyle="-",
alpha=0.8,
):
"""
Plot registration error metric as edges between FOVs
on a given axes (ax)
"""
for pair in pairs:
coord1 = self.image_coords[pair[0]]
coord2 = self.image_coords[pair[1]]
if pair in edge_dict:
weight = edge_dict[pair][1] / weight_norm * 2
elif (pair[1], pair[0]) in edge_dict:
weight = edge_dict[(pair[1], pair[0])][1] / weight_norm * 2
else:
raise ValueError(f"could not find pair {pair} in edge dictionary")
ax.plot([coord1[1], coord2[1]], # x start, end
[coord1[0], coord2[0]], # y start, end
linestyle=linestyle,
color=linecolour,
linewidth=weight,
marker="o",
markersize=10,
markeredgecolor="red",
markeredgewidth=2,
alpha=alpha,
)
def getAllShiftPairs(self,
plot_edges=True,
verbose=True,
):
"""
get all shifts between valid pairs
in the form of a dictionary of FOV pair : (shift, error_metric)
example entry:
('000_000', '000_001'): (array([2654.24, -24.64]), 3860283169.9943023)
"""
edge_dict = {}
nb_dict = self._getAllNeighbours(verbose=True, )
for fov in nb_dict:
fov_below = nb_dict[fov][1]
if fov_below is not None:
edge_dict[(fov, fov_below)] = self.getShift(fov, fov_below,
vertical=True)
fov_right = nb_dict[fov][3]
if fov_right is not None:
edge_dict[(fov, fov_right)] = self.getShift(fov, fov_right,
vertical=False)
errors = np.array([edge_dict[pair][1] for pair in edge_dict])
print(errors)
min_err = np.amin(errors)
max_err = np.amax(errors)
row, col, weight = [], [], []
for pair in edge_dict:
row.append(np.where(self.fov_grid_flat == pair[0])[0][0])
col.append(np.where(self.fov_grid_flat == pair[1])[0][0])
weight.append(-1 * edge_dict[pair][1])
if verbose:
print(f"Rows:\n{row}"
f"Columns:\n{col}"
f"Weight:\n{weight}\n")
num_fovs = self.fov_grid.size
self.weight_graph = csr_matrix(
(np.array(weight), (np.array(row), np.array(col))),
shape=(num_fovs, num_fovs),
)
# print(f"Full weight graph = \n{self.weight_graph.toarray()}")
self.min_span_tree = minimum_spanning_tree(self.weight_graph)
# symmetrize the min_span_graph,
# then split into rows
# for easy referencing when calculating coordinates
# FIXME: Seems to be not sparse efficient
msg_rows, msg_cols = self.min_span_tree.nonzero()
self.min_span_tree[msg_cols, msg_rows] = self.min_span_tree[msg_rows, msg_cols]
self.mst_by_row = np.split(self.min_span_tree.indices,
self.min_span_tree.indptr)[1:-1]
print(f"min span tree by row:\n{self.mst_by_row}")
fig_graph, ax_graph = plt.subplots(nrows=1, ncols=2,
figsize=(12, 8))
ax_graph[0].imshow(self.weight_graph.toarray())
ax_graph[1].imshow(self.min_span_tree.toarray())
mingraph_rows, mingraph_cols = self.min_span_tree.nonzero()
mingraph_pairs = [
(self.fov_grid_flat[row], self.fov_grid_flat[col])
for (row, col)
in zip(mingraph_rows, mingraph_cols)
]
firstvalue = self.min_span_tree[mingraph_rows[0], mingraph_cols[0]]
print(f"Full weight graph num edges= {self.weight_graph.nnz}\n"
f"Min graph num edges = {self.min_span_tree.nnz}\n"
f"data:\n{self.min_span_tree.data}\n"
f"rows = {mingraph_rows}\n"
f"cols = {mingraph_cols}\n"
f"firstvalue = {firstvalue}\n")
if plot_edges:
sns.set_style("darkgrid")
fig, ax = plt.subplots()
self._plotEdges(edge_dict, list(edge_dict.keys()),
ax,
weight_norm=min_err,
)
self._plotEdges(edge_dict, mingraph_pairs,
ax,
weight_norm=min_err,
linecolour="red", linestyle="-",
alpha=0.9,
)
ax.set_aspect('equal', 'box')
# flip the y axis
# ---------------
limits = ax.axis()
ax.axis([limits[0], limits[1], # x limits stay the same
limits[3], limits[2]], # y limits swapped
)
self.edge_dict = edge_dict
return edge_dict
def getAllCoords(self,
verbose=True,
):
"""
get the coords of all FOVs
as a pandas dataframe:
index : the FOV reference string
col 1 : the y pixel-coordinate position
col 2 : the x pixel-coordinate position
zeros all positions according to the top-left FOV
"""
# get starting FOV by finding the best-registered edge
# and using one of the 2 FOVs from that edge
fov_start_idx = self.min_span_tree.argmin() // self.fov_grid.size
fov_start = self.fov_grid_flat[fov_start_idx]
coord_list = [[fov_start, 0.0, 0.0], ]
if verbose:
print(f"\nStarting with FOV {fov_start} "
f"with index {fov_start_idx} ...\n"
f"coord list intialized as {coord_list}\n")
# recursively run through all branches of the spanning tree
coord_list = self._runVertex(coord_list,
fov_start_idx,
(0.0, 0.0),
verbose=verbose,
)
if verbose:
print(f"Final coord list:")
pp.pprint(coord_list)
# Convert list of lists to Dataframe
stitched_fovcoord_df = pd.DataFrame(coord_list, # list of [fov, y ,x] lists
columns=["fov", "y_coord", "x_coord"],
)
if verbose:
print(f"\nCoord dataframe original:\n{stitched_fovcoord_df}\n")
# zero all coordinates by the top left FOV
# i.e. top left of canvas is 0
# no image here
# __|_____________0
# | #######
# | ####### |
# no image |############ |
# here |######## V
# |########
# 0 ---->
stitched_fovcoord_df["y_coord"] -= stitched_fovcoord_df["y_coord"].min()
stitched_fovcoord_df["x_coord"] -= stitched_fovcoord_df["x_coord"].min()
if verbose:
print(f"Coord dataframe zeroed:\n{stitched_fovcoord_df}")
self.stitched_fovcoord_df = stitched_fovcoord_df
return stitched_fovcoord_df
def _runVertex(self,
coord_list, # list of [fov,y-coord, x-coord]
current_fov_idx,
current_fov_coord,
previous_fov_idx=None,
verbose=False,
):
"""
Note: each vertex is an FOV
A recursive function that
either:
returns the same coord list if it has reached a terminal branch
or:
checks any new vertices attached to the current vertex,
adds the cumulative position of the new vertex
and runs the same function on that vertex
"""
current_fov = self.fov_grid_flat[current_fov_idx]
current_y, current_x = current_fov_coord
if previous_fov_idx is not None:
previous_fov = self.fov_grid_flat[previous_fov_idx]
else:
previous_fov = None
# list/numpy array of vertices connected to the current vertex
vertices = self.mst_by_row[
np.where(self.fov_grid_flat == current_fov)[0][0]
]
if verbose:
print(f"Current FOV: {current_fov}\t|\t"
f"Connected vertices: {vertices}\n")
# decide whether to check vertices
# or just return coord_list (i.e. reached end of branch)
check_new = (vertices.shape[0] > 1
or
(previous_fov == None
and vertices.shape[0] > 0))
if check_new:
for fov_idx in vertices:
if fov_idx != previous_fov_idx:
next_fov = self.fov_grid_flat[fov_idx]
if (current_fov, next_fov) in self.edge_dict:
y_shift, x_shift = tuple(
self.edge_dict[(current_fov, next_fov)][0]
)
elif (next_fov, current_fov) in self.edge_dict:
y_shift, x_shift = tuple(
self.edge_dict[(next_fov, current_fov)][0] * -1
)
else:
raise KeyError(f"Pair ({current_fov}, {next_fov})"
f" not found in edge dictionary")
fov_y = current_y + y_shift
fov_x = current_x + x_shift
coord_list.append([next_fov, fov_y, fov_x])
coord_list = self._runVertex(coord_list,
fov_idx, (fov_y, fov_x),
previous_fov_idx=current_fov_idx,
verbose=verbose,
)
return coord_list
def saveFovCoords(self,
filename: str = "FOV_globalcoords.tsv",
) -> None:
"""
save a list of global coords for each FOV as a .tsv file
"""
assert isinstance(self.stitched_fovcoord_df, pd.DataFrame), (
"FOV stitched coordinates dataframe is not a pandas dataframe.\n"
"Unable to save."
)
self.stitched_fovcoord_df.to_csv(
os.path.join(self.stitched_path, filename), sep="\t"
)
def showStitched(self,
h5_dataset_name: str = "stitched", # h5py dataset reference
downsample: int = 4,
display_vmax: Union[int, float] = 40000,
title="Stitched image",
) -> None:
"""
show stitched image
"""
full_canvas = self.h5file[h5_dataset_name]
sns.set_style("white")
fig_canvas, ax_canvas = plt.subplots(figsize=(8, 8))
ax_canvas.imshow(np.array(full_canvas[::downsample, ::downsample]),
vmax=display_vmax)
fig_canvas.tight_layout(rect=(0, 0, 1, 0.95))
fig_canvas.suptitle(title)
def assembleCanvas(self,
subpixel: bool = True,
subpixel_interp_order: int = 1,
verbose: bool = True,
):
"""
Using shifts from coords dataframe
Insert the images in approprate place in an empty matrix
Parameters
----------
subpixel:
whether to do subpixel interpolation
subpixel_interp_order:
default to linear interpolation (0-NN, 1-linear, 2-cubic)
"""
assert self.stitched_fovcoord_df is not None, (
f"Coords Dataframe not found.\n"
f"need to calculate shifts first."
)
max_shift_y = self.stitched_fovcoord_df["y_coord"].max()
max_shift_x = self.stitched_fovcoord_df["x_coord"].max()
# Initialize large empty hdf5 dataset as a canvas to place images in
# ------------------------------------------------------------------
full_canvas = self.h5file.create_dataset(
"stitched",
(int(max_shift_y) + 1 + self.ydim,
int(max_shift_x) + 1 + self.xdim),
dtype="f",
)
if verbose:
print(f"Full canvas\t-\tshape: {full_canvas.shape}, "
f"datatype: {full_canvas.dtype}\n")
for df_row in range(len(self.stitched_fovcoord_df.index)):
fov, ycoord, xcoord = self.stitched_fovcoord_df.loc[
df_row, ["fov", "y_coord", "x_coord"]
]
top_index = int(ycoord)
left_index = int(xcoord)
bottom_index = top_index + self.ydim
right_index = left_index + self.xdim
if verbose:
print(f"FOV {fov}:\n"
f"top={top_index}, left={left_index}, "
f"bottom={bottom_index}, right={right_index}\n")
# Insert into canvas
# ------------------
img = np.array(self.imgs[fov])
if subpixel: # apply subpixel shift
img = interpolation.shift(img,
(ycoord % 1, xcoord % 1),
order=subpixel_interp_order,
mode="constant",
cval=0.0,
prefilter=True)
full_canvas[
top_index: bottom_index, left_index:right_index
] = np.maximum(
img,
full_canvas[
top_index: bottom_index, left_index:right_index
]
)
# FIXME: the above is inefficent (lots of maximizing over zero regions)
# FIXME: but it should work for now
return full_canvas
def saveDax(self,
full_canvas: h5py.Dataset = None, # h5py dataset reference
filename: str = "stitched_",
add_timestr: bool = True,
):
"""
save the stitched image as a .dax file
"""
if full_canvas is None:
full_canvas = self.h5file["stitched"]
timestr = ""
if add_timestr:
timestr += datetime.datetime.now().strftime("_%Y%m%d_%H%M")
full_savepath = os.path.join(self.stitched_path,
filename + timestr + ".dax")