-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathim_utils.py
More file actions
1846 lines (1626 loc) · 58.5 KB
/
Copy pathim_utils.py
File metadata and controls
1846 lines (1626 loc) · 58.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
"""
Utilities for image analysis
Author: Porfirio Quintero-Cadena
"""
from matplotlib import pyplot as plt
import matplotlib.gridspec as gridspec
import seaborn as sns
import pandas as pd
import numpy as np
import warnings
import glob
import os
from tqdm import tqdm
import skimage
from skimage import io, morphology, segmentation
from skimage.filters import threshold_local
from skimage.draw import circle_perimeter
from skimage.external.tifffile import TiffFile
from scipy import ndimage
from numba import jit
from joblib import Parallel, delayed
import multiprocessing
import matplotlib as mpl
# matplotlib style
rc= {'axes.edgecolor': '0.3', 'axes.labelcolor': '0.3', 'text.color':'0.3',
'axes.spines.top': 'False','axes.spines.right': 'False',
'xtick.color': '0.3', 'ytick.color': '0.3', 'font.size':'35',
'savefig.bbox':'tight', 'savefig.transparent':'True', 'savefig.dpi':'500'}
for param in rc:
mpl.rcParams[param] = rc[param]
def ecdf(data, conventional=False, buff=0.1, min_x=None, max_x=None):
"""
Computes the x and y values for an ECDF of a one-dimensional
data set.
Parameters
----------
data : array_like
Array of data to be plotted as an ECDF.
conventional : bool, default False
If True, generates x,y values for "conventional" ECDF, which
give staircase style ECDF when plotted as plt.plot(x, y, '-').
Otherwise, gives points x,y corresponding to the concave
corners of the conventional ECDF, plotted as
plt.plot(x, y, '.').
buff : float, default 0.1
How long the tails at y = 0 and y = 1 should extend as a
fraction of the total range of the data. Ignored if
`coneventional` is False.
min_x : float, default -np.inf
If min_x is greater than extent computed from `buff`, tail at
y = 0 extends to min_x. Ignored if `coneventional` is False.
max_x : float, default -np.inf
If max_x is less than extent computed from `buff`, tail at
y = 0 extends to max_x. Ignored if `coneventional` is False.
Returns
-------
x : array_like, shape (n_data, )
The x-values for plotting the ECDF.
y : array_like, shape (n_data, )
The y-values for plotting the ECDF.
"""
# Get x and y values for data points
x, y = np.sort(data), np.arange(1, len(data)+1) / len(data)
if conventional:
# Set defaults for min and max tails
if min_x is None:
min_x = -np.inf
if max_x is None:
max_x = np.inf
# Set up output arrays
x_conv = np.empty(2*(len(x) + 1))
y_conv = np.empty(2*(len(x) + 1))
# y-values for steps
y_conv[:2] = 0
y_conv[2::2] = y
y_conv[3::2] = y
# x- values for steps
x_conv[0] = max(min_x, x[0] - (x[-1] - x[0])*buff)
x_conv[1] = x[0]
x_conv[2::2] = x
x_conv[3:-1:2] = x[1:]
x_conv[-1] = min(max_x, x[-1] + (x[-1] - x[0])*buff)
return x_conv, y_conv
return x, y
def split_project(im_col):
"""
Split stack by channels and z-project each channel
Arguments
---------
im_col: skimage image collection
Returns
---------
gfp, rfp: array_like
split image collection and z-projected based on maximum value
"""
# convert image collection to numpy array
stack = np.copy(im_col)
num = len(stack)/2
# gfp channel first half, rfp second
gfp = stack[:num]
rfp = stack[num:]
gfp = z_project(gfp)
rfp = z_project(rfp)
return gfp, rfp
def z_project(stack, project='max', mindim=2):
"""
Z-project stack based on maximum value.
Arguments
---------
stack: array_like.
input image stack
project: str
which value to project: maximum (max), minimum (min)
mindim: int
minimum number of dimensions in stack to project, otherwise return stack
Returns
---------
z_im: z-projection of image
"""
if stack.ndim<=mindim:
return stack
if project == 'max':
z_im = np.maximum.reduce([z for z in stack])
if project == 'min':
z_im = np.minimum.reduce([z for z in stack])
# np.mean does not have reduce
#if project == 'mean':
# z_im = np.mean.reduce([z for z in stack])
return z_im
def plot_gallery(images, n_row=3, n_col=4, reshape=None,
cmap='viridis', titles=None, fig_title=None):
"""
Helper function to plot a gallery of images
Arguments
---------
images: tuple or list of array_like objects
images to plot
n_row, n_col: integer
number of rows and columns in plot
If less than number of images, truncates to plot n_row * n_col
reshape: None or tuple of two height and width int
reshape each image to h and w
cmap: string
name of matplotlib colormap
titles, fig_title: string
optional subaxes and figure title
Returns
---------
None (only plots gallery)
"""
fig = plt.figure(figsize=(1.8 * n_col, 2.4 * n_row))
for i in range(n_row * n_col):
ax = fig.add_subplot(n_row, n_col, i + 1)
if reshape:
h, w = reshape
ax.imshow(images[i].reshape((h, w)), cmap=cmap)
else:
ax.imshow(images[i], cmap=cmap)
if titles:
ax.set_title(titles[i], size=12)
plt.xticks(())
plt.yticks(())
if fig_title: fig.suptitle(fig_title+'\n', fontsize=20)
plt.tight_layout()
return None
def fakeRGB2gray(im):
"""
Check if an RGB image is grayscale and convert if necessary.
Useful because sometimes grayscale images are saved as redundant RGB
Returns an error if image is not grayscale.
Returns the same image if already grayscale and with single channel.
Arguments
---------
im: array_like, shape (h,w,3)
image to convert
Returns
---------
im: array_like, shape (h,w,1)
grayscale image, if indeed grayscale.
"""
try:
# If the three channels are the same, it is grayscale, get only one
if ((im[:,:,0] == im[:,:,1]) & ( im[:,:,2]== im[:,:,0])).all():
return im[:,:,0]
else: raise ValueError('Channels are not identical')
except IndexError:
warnings.warn('Image has only one channel')
return im
def im_hist(im, ax=None):
"""
Plot image pixel intensity histogram
Arguments
---------
im: array_like
image to plot
Returns
---------
None (only plots histogram)
"""
hist, bins = skimage.exposure.histogram(im)
if ax is None: fig, ax = plt.subplots()
with sns.axes_style('darkgrid'):
ax.fill_between(bins, hist, lw=0.25, alpha=0.4)
ax.set(yscale='log', xlabel='normalized pixel value', ylabel='count')
return None
def rectangleROI(im, pad=0, thresh=None):
"""
Identify coordinates of rectangular ROI in an image with mostly dark irrelevant pixels
The returned ROI excludes all continuous zero columns and rows after thresholding
Arguments
---------
im: array_like
input image
thresh: int or float
threshold to use to discard background (
Returns
---------
roi_coords: slice object containing roi coordinates.
Can be directly used as numpy slice: im[roi_coords]
"""
if not thresh:
thresh = skimage.filters.threshold_li(im)
# threshold to get boolean mask
im_thresh = im > thresh
# check that something remains after thresholding
if not np.sum(im_thresh): raise RuntimeError('No pixels pass specified threshold')
# get all zero rows and columns
rows = np.nonzero(np.sum(im_thresh, 1))[0]
cols = np.nonzero(np.sum(im_thresh, 0))[0]
# remove only continuous zero rows and columns and store coordinates in
# slice object
# avoid negative values and slices bigger than original image
roi_coords = (slice(max(0, min(rows)-pad), min(im.shape[0], max(rows)+1+pad)),
slice(max(0, min(cols)-pad), min(im.shape[1], max(cols)+1+pad)))
return roi_coords
def label_sizesel(im, im_mask, maxint_lim, minor_ax_lim,
major_ax_lim, area_lim, watershed=False):
"""
Create and label markers from image mask,
filter by area and compute region properties
Arguments
---------
im: array_like
input image
im_mask: boolean array
image mask
watershed: boolean
whether to perform watershed on markers
feature_lim: iterable of two
minimum and maximum bounds for each feature, inclusive
Returns
---------
markers: array_like
labeled image, where each object has unique value and background is 0
nuclei: list of region props objects
list of region properties of each labeled object
"""
markers = skimage.morphology.label(im_mask)
if watershed:
# harsh erosion to get basins for watershed
im_mask_eroded = skimage.measure.label(\
skimage.morphology.binary_erosion(im_mask,
selem=skimage.morphology.diamond(8)))
# watershed transform using eroded cells as basins
markers = skimage.morphology.watershed(markers,
im_mask_eroded, mask=im_mask)
nuclei = skimage.measure.regionprops(markers, im, coordinates='xy')
# get only markers within area bounds, above intensity thersh and
# not oversaturated
all_labels = np.unique(markers)
sel_labels = [n.label for n in nuclei if \
minor_ax_lim[0] <= n.minor_axis_length <= minor_ax_lim[1]
and major_ax_lim[0] <= n.major_axis_length <= major_ax_lim[1] \
and area_lim[0] <= n.area <= area_lim[1] \
and maxint_lim[0] <= n.max_intensity <= maxint_lim[1]]
rem_labels = [l for l in all_labels if l not in sel_labels]
# remove unselected markers
for l in rem_labels:
markers[np.where(np.isclose(markers,l))] = 0
nuclei = [n for n in nuclei if n.label in sel_labels]
return markers, nuclei
def int_sel(nuclei, min_int):
"""
Drop nuclei by intensity threshold
Arguments
---------
nuclei: list of region props objects
list of region properties of each labeled object
min_int: int or float
minimum intensity threshold
Returns
---------
markers: array_like
labeled image, where each object has unique value and background is 0
nuclei: list of region props objects
list of region properties of each labeled object
"""
sel_labels = [n.label for n in nuclei if min_int < n.mean_intensity]
nuclei = [n for n in nuclei if n.label in sel_labels]
int_ = np.array([n.mean_intensity for n in nuclei])
return nuclei, int_
def circle_nuclei(nuclei, im, diam=(10, 12, 14)):
"""
Draw circles around identified segments for plotting
Arguments are region props objects, image and circle diameter (more than one
draws multiple circles around each centroid)
Arguments
---------
nuclei: list of region props objects
list of region properties of each labeled object
im: array_like
corresponding image
diam: tuple of int
diameter of circles to be drawn around each object;
also determines the number of circles.
Returns
---------
im_plot: array_like
copy of the image with circles around each object
"""
nuclei_c = [n.centroid for n in nuclei]
circles = []
for d in diam:
circles += [circle_perimeter(int(r), int(c), d, shape=im.shape) for (r,c) in nuclei_c]
im_plot = im.copy()
for circle in circles:
im_plot[circle] = np.max(im_plot)
return im_plot
def plot_nuclei_int(im_plot_r, im_plot_g, int_ratio):
"""
Plot two channels and intensity ratio side by side
Arguments
---------
im_plot_r, im_plot_g: array_like
images to plot
int_ratio: list of int or float
intensity ratios
Returns
---------
None (only plots)
"""
gs = gridspec.GridSpec(1, 7)
ax1 = plt.subplot(gs[0:3])
ax2 = plt.subplot(gs[3:6])
ax3 = plt.subplot(gs[6])
ax1.imshow(im_plot_r, plt.cm.viridis)
ax1.set_title('red channel nuclei', fontsize=20)
ax2.imshow(im_plot_g, plt.cm.viridis)
ax2.set_title('green channel nuclei', fontsize=20)
sns.stripplot(int_ratio, orient='v', size=10, alpha=0.5, cmap='viridis', ax=ax3)
ax3.set_ylabel('intensity ratio (gfp/rfp)', fontsize=20)
plt.tight_layout()
return None
def mask_image(im, im_thresh=None, min_size=15, block_size=101, selem=skimage.morphology.disk(15),
clear_border=True):
"""
Create a binary mask to segment nuclei using adaptive threshold.
Useful to find nuclei of varying intensities.
Remove small objects, fill holes and perform binary opening (erosion
followed by a dilation. Opening can remove small bright spots (i.e. “salt”)
and connect small dark cracks. This tends to “open” up (dark) gaps between
(bright) features.)
Arguments
---------
im: array_like
input image
thresh: array_like, optional
thresholded image
min_size: float or int
minimum size of objects to retain
block_size: odd int
block size for adaptive threshold, must be provided if im_thresh is None
Returns
---------
im_thresh: array_like
thresholded binary image
"""
if im_thresh is None:
im_thresh = im>skimage.filters.threshold_local(im, block_size)
im_thresh = skimage.morphology.remove_small_objects(im_thresh, min_size=min_size)
im_thresh = ndimage.morphology.binary_fill_holes(im_thresh, morphology.disk(1.8))
im_thresh = morphology.binary_opening(im_thresh, selem=selem)
im_thresh = skimage.morphology.remove_small_objects(im_thresh, min_size=min_size)
if clear_border:
im_thresh = skimage.segmentation.clear_border(im_thresh)
return im_thresh
def manual_sel(im_r, markers_r, nuclei_r, im_g, markers_g, nuclei_g):
"""
Manual (click) confirmatory selection of nuclei
After automatic segmentation, allows user to select objects of interest by
single click on them.
Useful if automatic segmentation is difficult.
Arguments
---------
im_r, im_g: array_like
original reference images
markers_r, markers_g, array_like
segmented, labeled images
nuclei_r, nuclei_g: region props object
region properties of labeled images
Returns
---------
nuclei_r, markers_r, nuclei_g, markers_g: updated (clicked on) objects
"""
coords_rg = []
for i in (1,3):
# click on nuclei
fig, axes = plt.subplots(1,4, figsize=(25,10))
axes[0].imshow(im_r, plt.cm.viridis)
axes[1].imshow(markers_r, plt.cm.Paired)
axes[2].imshow(im_g, plt.cm.viridis)
axes[3].imshow(markers_g, plt.cm.Paired)
axes[i].set_title('Select markers here\n(Press Alt+Click when done)', fontsize=20)
coords = plt.ginput(100, show_clicks=True)
coords = [(int(c1), int(c2)) for (c2, c1) in coords]
plt.close('all')
coords_rg.append(coords)
coords_r, coords_g = coords_rg
def update_sel(markers, nuclei, coords):
all_labels = np.unique(markers)
sel_labels = [markers[c] for c in coords]
rem_labels = [l for l in all_labels if l not in sel_labels]
# get selected nuclei
nuclei = [n for n in nuclei if n.label in sel_labels]
# remove unselected markers
for l in rem_labels:
markers[np.where(np.isclose(markers,l))] = 0
return markers, nuclei
markers_r, nuclei_r = update_sel(markers_r, nuclei_r, coords_r)
markers_g, nuclei_g = update_sel(markers_g, nuclei_g, coords_g)
return nuclei_r, markers_r, nuclei_g, markers_g
def clickselect_plot(event, selected, fig, axes, heart=True):
"""
Event handler for button_press_event.
Save index of image clicked on, useful to view multiple images in
subplots, and select which ones to keep.
Has to be called as follows:
cid = fig.canvas.mpl_connect('button_press_event',
lambda event: clickselect_plot(event, selected, fig, axes))
Arguments
---------
event: matplotlib event
from fig.canvas.mpl_connect('button_press_event', onclick)
selected: empty set
store unique indices of clicked images
figure: matplotlib figure
figure containing axis to click on
ax: matplotlib axis
axis to click on
Returns
---------
None
Adds indices to selected in place
"""
for i, ax in enumerate(axes):
if ax == event.inaxes:
# Show which image is selected
ax.set_title('Liked!')
if heart:
draw_heart(ax)
# update plot and save selection
fig.canvas.draw()
selected.add(i)
def draw_heart(ax):
"""
Draw a red heart on ax.
Arguments
---------
ax: matplotlib axis
axis to draw heart on
Returns
---------
None
Just plots heart for image selection tool (click_select_plot)
"""
x_lim = ax.get_xlim()
y_lim = ax.get_ylim()
xheart = np.mean(x_lim)
yheart = np.mean(y_lim)
t = np.linspace(0, 2 * np.pi, 200)
x = -10*(16 * np.sin(t)**3) + xheart
y = -10*(13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)) + yheart
ax.fill_between(x, y, color='red', alpha=0.3)
ax.set_xlim(x_lim)
ax.set_ylim(y_lim)
def burn_scale_bar(im, width=6, white=True, zoom=40, pixel_dist='leica'):
"""
Burn a horizontal scale bar
Use interpixel_dist function to compute interpixel distance for other setup
Arguments
---------
im: array_like
image to copy and draw scale bar on
width: int
width of the scale bar
white: boolean
color of scale bar (maximum or minimum)
zoom: int
optical zoom/lens used
pixel_dist: dict or string (default only)
dictionary with key, value pairs for scale bar dimensions
as zoom:(length_pixels, length_microns)
Default settings are for Leica microscope (Ca+2 imaging)
Returns
--------
im_out: array_like
copy of im with labeled scale bar
legend_x, legend_y: int
coordinates for scale bar legend
legend: string
label for scale bar (e.g. 10 microns, 20 microns...)
"""
# Modify a copy of the image just in case
im_out = im.copy()
# Position of scale bar (top left corner)
j_pos = legend_x = im.shape[1]*0.08
i_pos = im.shape[0] * 0.9
# Position of scale bar length label (above bar)
legend_y = im.shape[0]*0.88
# Dictionary with interpixel distance calibrated for Leica
# key value pairs are: zoom:(length_pixels, length_microns)
if pixel_dist == 'leica':
pixel_dist = {10:(75,'100'), 20:(70,'50'), 40:(56,'20'), 63:(40, '10')}
length, legend = pixel_dist[zoom]
if white: pixel_val = np.max(im)
else: pixel_val = np.min(im)
# burn scale bar, not vector graphics, in case image is improperly reshaped
im_out[i_pos-(width//2):i_pos+(width//2), j_pos:j_pos+length] = pixel_val
return im_out, (legend_x, legend_y, str(legend))
def interpixel_dist(im, ref_length):
"""
Compute interpixel distance by measuring an object of known length.
Prompts the image and allows the user to click on the two ends of the object.
Arguments
---------
im: array_like
image with reference object
ref_length: int or float
length of the object
Returns
---------
interpixel_distance: float
number of pixels per unit of reference length
"""
io.imshow(im)
xy = plt.ginput(2)
feature_length = np.sqrt((xy[1][1] - xy[0][1])**2 + (xy[1][0] - xy[0][0])**2)
interpixel_distance = feature_length / ref_length
return interpixel_distance
def mult_im_selection(data_dir, has_dic=True, project='max', ext='.tif', limit=100,
heart=True, save=False, crop_roi=False, overlay=0.9, plot=False):
"""
Widget for image selection, z_projection, ROI cropping, and DIC-GFP overlay
from multiple samples stored in different directories.
Shows images from nested directories sequentially, and allows user to click
on those to keep.
Arguments
---------
datadir: string
parent directory containing image folders
has_dic: boolean
whether image stacks have DIC, should be first in stack
project: string
project z-stacks based on max, mean or min value.
ext: string
extension of image files to look for
limit: integer
upper limit of the number of directories to look at
heart: boolean
whether to draw a heart on liked images
save: boolean
whether to save selected images to disk
crop_roi: string or False
whether to find rectangular ROI using dic, gfp, or nothing
overlay: float
alpha value to overlay GFP on DIC. 1 means completely block DIC
plot: boolean
whether to plot selection of images and save to pdf
Returns
---------
im_selection: dictionary
Dictionary with selected images and respective directory name
"""
data_dirs = glob.glob(data_dir + '*')
# dictionary to store sample name and selected images as key:values
all_im = {}
im_selection = {}
# counter to limit number of dictionaries
n=1
# start interactive mode
plt.ion()
# show images by directory/strain
for d in data_dirs:
im_dirs = glob.glob(d + '/*' + ext)
try:
fig, axes = plt.subplots(3, int(len(im_dirs)/3))
except IndexError:
fig, axes = plt.subplots(1)
# get strain number
strain = d.split('/')[-1].split('_')[0]
curr_ims = []
for (im_dir, ax) in zip(im_dirs, np.ravel(axes)):
# get image name
im_name = im_dir.split('/')[-1]
# get zoom, if encoded in image name
if 'x' in im_name:
zoom = int(im_name.split('_')[-1].split('x')[0])
else: zoom = 40
# load image
im_stack = io.imread_collection(im_dir)
# Get channels, DIC is always first array
dic = im_stack[0]
if has_dic:
gfp = im_stack[1:]
else: gfp = im_stack[0:]
# Project gfp based on maximum value
gfp = z_project(gfp, project=project)
# Find and crop ROI
if crop_roi == 'gfp':
roi = rectangleROI(gfp)
elif crop_roi == 'dic':
roi = rectangleROI(dic)
if crop_roi:
dic = dic[roi]
gfp = gfp[roi]
# save it for later, DIC goes first
curr_ims.append((dic, gfp, zoom))
# Plot DIC and overlay GFP
ax.imshow(dic)
ax.imshow(gfp, alpha=overlay, cmap=plt.cm.viridis)
ax.set_title(im_name)
ax.set_xticks([])
ax.set_yticks([])
# set to store indices of selected images
selected = set()
# select images by clicking on them
cid = fig.canvas.mpl_connect('button_press_event',
lambda event: clickselect_plot(event, selected, fig, np.ravel(axes), heart))
# Stop after 100 clicks or until the user is done
fig.suptitle('Click to like, right click (Alt+click) when done\nsample:{}\n'.format(strain), fontsize=15)
#plt.tight_layout()
plt.ginput(100, timeout=0, show_clicks=True)
fig.canvas.mpl_disconnect(cid)
plt.close('all')
im_selection[strain] = [im for (i, im) in enumerate(curr_ims) if i in selected]
n+=1
if n>limit: break
if save: save_imdict('./favorite_worms/', im_selection, has_dic)
if plot: mult_im_plot(im_selection, save=True)
return im_selection
def save_imdict(save_dir, im_selection, has_dic):
"""
Save a dictionary of images to structured directory
Arguments
---------
save_dir: string
root directory to save
im_selection: dict
dictionary with sample:(images, zoom)
Returns
--------
None
Saves images and prints save_dir
"""
os.mkdir(save_dir)
for sample in im_selection:
save_subdir = save_dir + str(sample) + '/'
os.mkdir(save_subdir)
for i, image in enumerate(im_selection[sample], start=1):
# each entry in im_selection is (dic, gfp, zoom)
if has_dic:
im_ = np.stack(image[:-1])
else: im_ = image[1]
zoom = image[-1]
io.imsave(save_subdir + str(i) + '_' + str(zoom) + 'x.tif', im_)
print('Images saved to {}'.format(save_dir))
def imdict_fromdir(data_dir):
"""
Load images from multiple subdirs in data_dir to dictionary
Arguments
---------
data_dir: string
root directory containing subirectories with images
Returns
---------
im_collection: dictionary
dict containing sample:(dic, gfp, zoom)
"""
data_dirs = glob.glob(data_dir + '*')
im_collection = {}
for d in data_dirs:
strain = d.split('/')[-1].split('_')[0]
im_dirs = glob.glob(d + '/*' + '.tif')
ims = []
for im_dir in im_dirs:
im_name = im_dir.split('/')[-1]
im_stack = io.imread_collection(im_dir)
# Get channels, DIC is always first array
dic = im_stack[0]
gfp = im_stack[1]
# Get zoom
if 'x' in im_name:
zoom = int(im_name.split('_')[-1].split('x')[0])
else: zoom = 40
ims.append((dic, gfp, zoom))
im_collection[strain] = ims
return im_collection
def mult_im_plot(im_dict, n_row='auto', n_col='auto', fig_title=None, sort=True,
overlay=0.7, scale_bar=True, scale_font=8, save=False):
"""
Helper function to plot a gallery of images stored in dictionary
(output from mult_im_selection function)
Arguments
---------
im_dict: dictionary or str
Dictionary with images to plot. Values must be pairs of (DIC, GFP)
Also takes path to image directories that can be loaded with imdict_fromdir function
n_row, n_col: integer or str
number of rows and columns in plot
If less than number of images, truncates to plot n_row * n_col
If auto, automatically determine
fig_title: string
optional figure title
sort: boolean
whether to plot images sorted by key
overlay: float
alpha value for GFP channel (0-1). If 1, then completely hide DIC
save: boolean
whether to save plot to pdf
Returns
---------
None (only plots gallery)
"""
# Load image dictionary if required
if isinstance(im_dict, str):
# add slash if not included in path
if im_dict[-1] != '/': im_dict += '/'
im_dict = imdict_fromdir(im_dict)
# get appropiate number of rows and columns for plot
if n_row and n_col == 'auto':
num_ims = len([im for k in im_dict for im in im_dict[k]])
if num_ims > 4:
n_col = num_ims//2
else: n_col = num_ims
# compute number of rows required
if num_ims % n_col ==0:
n_row = (num_ims // n_col)
else: n_row = (num_ims // n_col) + (num_ims % n_col)
# whether to sort by name
if sort: keys = sorted(im_dict)
else: keys = im_dict.keys()
plt.ion()
fig = plt.figure(figsize=(1.8 * n_col, 2.4 * n_row))
# counter to add axes
j=1
for sample in keys:
for (i, im) in enumerate(im_dict[sample], start=j):
try:
# get zoom for scale bar; if not specified, use default
# regardless of whether or not it is drawn (specified later)
dic, gfp, zoom = im
except ValueError:
dic, gfp = im
# default zoom
zoom = 40
if scale_bar:
gfp, (scale_x, scale_y, scale_legend) = burn_scale_bar(gfp, zoom=zoom)
# Create subplot, plot DIC and overlay GFP
ax = fig.add_subplot(n_row, n_col, i)
ax.imshow(dic, cmap=plt.cm.gray)
ax.imshow(gfp, alpha=overlay, cmap=plt.cm.viridis)
if scale_bar:
# Add scale bar label (microns)
ax.text(scale_x, scale_y, r'$' +scale_legend + ' \mu m$', color='yellow', fontsize=scale_font)
ax.set_title(sample)
plt.xticks(())
plt.yticks(())
j+=1
if fig_title: fig.suptitle(fig_title+'\n', fontsize=20)
plt.tight_layout()
if save: plt.savefig('./favorite_worms.pdf', transparent=True, bbox_inches='tight')
def zoom2roi(ax):
"""
Identify coordinates of zoomed-in/moved axis in interactive mode
Arguments
---------
ax: matplotlib axis
zoomed-in/moved axis
Returns
---------
zoom_coords: tuple of slice objects
coordinates of zoomed in box, use as im[zoom_coords]
"""
# get x and y limits
xlim = [int(x) for x in ax.get_xlim()]
ylim = [int(y) for y in ax.get_ylim()]
# make and return slice objects
return (slice(ylim[1],ylim[0]), slice(xlim[0],xlim[1]))
def show_movie(stack, delay=0.5, cmap='viridis', h=10, w=6, time=None,
loop=False):
"""
Show movie from stack
Arguments
---------
stack: numpy stack
collection of 2D frames (movie)
delay: float
delay in between frames
h, w: int
height and width of movie display
time: int or None
inter-frame time to display on top of movie, in seconds
loop: boolean
whether to loop the movie
Returns
---------
None
plays movie
"""
fig = plt.figure(figsize=(w, h))
while True:
for n, frame in enumerate(stack):
try:
mov.set_data(frame)
except NameError:
mov = plt.imshow(frame, cmap=cmap)
plt.tight_layout()
plt.xticks(())
plt.yticks(())
plt.draw()
plt.pause(delay)
if time:
plt.title('t {}s'.format(n*time))
else:
plt.title('frame {}'.format(n+1))
if not loop: break
def resize_frame(frame, h, w, fillvalue='min'):
"""
Resize frame to larger shape (h,w) by filling with 'fillvalue'
Arguments
---------
frame: numpy array
h, w: int
new height and width
fillvalue: str or number
'min', 'zeros' or number, value to fill empty spaces
Returns
---------
new_frame: numpy array
resized frame
"""
if fillvalue=='min':
new_frame = np.full((h,w), np.min(frame))
elif fillvalue=='zeros':
new_frame = np.zeros((h,w))
else:
new_frame = np.full((h,w), fillvalue)
new_frame[:frame.shape[0], :frame.shape[1]] = frame
return new_frame
def concat_movies(movies, ncols=4, norm=True):
"""
Concatenate a set movies frame by frame to play multiple simultaneously.
Will add pixels to frames and frames to movies as necessary to make
max-movie-length square arrays.
Arguments
---------
movies: iterable of numpy stacks
movies to show
ncols: int
number of columns to display movies into,
norm: bool
whether to normalize/scale each image