forked from ant-trullo/SegmentTrack_v4.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNucleiSpotsConnection.py
76 lines (53 loc) · 3.11 KB
/
NucleiSpotsConnection.py
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
"""Given tracked spots and tracked nuclei, this function generates the false colored video."""
import numpy as np
from skimage.measure import label, regionprops_table
from PyQt5 import QtWidgets
class NucleiSpotsConnection:
"""Only one clss, does all the job"""
def __init__(self, spots_tracked, nuclei_tracked):
nuclei_active = np.sign(nuclei_tracked).astype(np.int)
idx = np.unique(spots_tracked)[1:] # list of all the values in the spots matrix. The zero, which is the first, is removed
pbar = ProgressBar(total1=idx.size)
pbar.show()
i = 0
rgp_spts = regionprops_table(spots_tracked.astype(np.int32), properties=["label", "coords"]) # dictionary with label and coordinate of each non zero pixel
for k in idx:
pbar.update_progressbar(i)
# aa = (spots_tracked == k).sum(2).sum(1)
# t_steps = np.where(aa != 0)[0]
coord_idx = np.where(rgp_spts["label"] == k)[0][0] # search the dictionary location with the corresponding label, generally is k + 1, but like this is 100% sure
t_steps = np.unique(rgp_spts["coords"][coord_idx][:, 0]) # array with the t coordinate of each pixel of the spot tracked. np.unique to have only once the number of the frame in which the i += 1
for tt in t_steps:
nuclei_active[tt, :, :] += (nuclei_tracked[tt, :, :] == k)
pbar.close()
nuclei_active3c = np.zeros((nuclei_tracked.shape[0], nuclei_tracked.shape[1], nuclei_tracked.shape[2], 3))
nuclei_active3c[:, :, :, 0] = (nuclei_active == 2)
nuclei_active3c[:, :, :, 2] = (nuclei_active == 1)
nuclei_active3c[:, :, :, 1] = np.sign(spots_tracked)
nuclei_active3c *= 255
n_active_vector = np.zeros(nuclei_active[:, 0, 0].size)
for t in range(n_active_vector.size):
l1 = label((nuclei_active[t, :, :] == 2) * nuclei_tracked[t, :, :], connectivity=1)
n_active_vector[t] = l1.max()
self.nuclei_active = nuclei_active
self.nuclei_active3c = nuclei_active3c
self.n_active_vector = n_active_vector
self.popt = 0
self.perr = 0
class ProgressBar(QtWidgets.QWidget):
"""Simple progress bar widget"""
def __init__(self, parent=None, total1=20):
super(ProgressBar, self).__init__(parent)
self.name_line1 = QtWidgets.QLineEdit()
self.progressbar1 = QtWidgets.QProgressBar()
self.progressbar1.setMinimum(1)
self.progressbar1.setMaximum(total1)
main_layout = QtWidgets.QGridLayout()
main_layout.addWidget(self.progressbar1, 0, 0)
self.setLayout(main_layout)
self.setWindowTitle("Progress")
self.setGeometry(500, 300, 300, 50)
def update_progressbar(self, val1):
"""Progressbar updater"""
self.progressbar1.setValue(val1)
QtWidgets.qApp.processEvents()