-
Notifications
You must be signed in to change notification settings - Fork 0
/
step9.py
141 lines (107 loc) · 4.46 KB
/
step9.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue May 28 00:21:37 2024
@author: samarth
"""
import os
import h5py
import numpy as np
import scipy.io as sio
from skimage.morphology import thin
from skimage.filters import threshold_otsu
from skimage.measure import label
import matplotlib.pyplot as plt
# Initialize variables
pos = 'Pos0_2'
path = '/Users/samarth/Documents/MirandaLabs/tracking_algo/FungAi_tracking/Tracking_toydata_Tracks'
sav_path = '/Users/samarth/Documents/MirandaLabs/tracking_algo/FungAi_tracking/Tracking_toydata_Tracks'
def load_mat(filename):
try:
return sio.loadmat(filename)
except NotImplementedError:
# Load using h5py for MATLAB v7.3 files
data = {}
with h5py.File(filename, 'r') as f:
for key in f.keys():
data[key] = np.array(f[key])
return data
def resolve_h5py_reference(data, f):
if isinstance(data, h5py.Reference):
return f[data][()]
return data
# Load ART and MAT tracks
art_track_path = os.path.join(path)
a_file_list = [f for f in os.listdir(art_track_path) if 'ART_Track1_DS' in f]
art = load_mat(os.path.join(path, a_file_list[0]))
mat_track_path = os.path.join(path)
m_file_list = file_list = [f for f in os.listdir(mat_track_path) if 'MAT_16_18_Track1_DS' in f]
mat = load_mat(os.path.join(path, m_file_list[0]))
# Obtain the gametes indexes that give rise to the mating cell (zygote)
Mask3 = []
with h5py.File(os.path.join(path, a_file_list[0]), 'r') as f:
for i in range(len(f['Mask3'])):
masks_refs = f['Mask3'][i]
for ref in masks_refs:
mask = resolve_h5py_reference(ref, f)
Mask3.append(mask)
# Reading Mat Tracks from mat
MTrack = []
with h5py.File(os.path.join(path, m_file_list[0]), 'r') as f:
for i in range(len(f['Matmasks'])):
masks_refs = f['Matmasks'][i]
for ref in masks_refs:
mask = resolve_h5py_reference(ref, f)
MTrack.append(mask)
gamete = np.zeros((3, int(mat['no_obj'][0, 0])))
cell_data = mat['cell_data']
shock_period = mat['shock_period']
for iv in range(int(mat['no_obj'][0, 0])):
# iv = 0
tp_mat_start = int(mat['cell_data'][0, iv]) # First appearance of mating event "iv"
M1 = MTrack[tp_mat_start-1] # Mat Track at tp_mat_start
for its in range(tp_mat_start - 1, int(mat['shock_period'][1, 0]), -1): # Loop through time from 1 tp before mating to one time point after shock
# its = 154
A1 = Mask3[its].astype(float).T
# plt.figure()
# plt.imshow(A1, cmap='gray')
# plt.title('A1')
# plt.show()
M2 = (M1 == iv + 1).astype(float).T
Im1 = (M2 > threshold_otsu(M2)).astype(float)
Im2 = thin(Im1, 10).astype(float)
Im3 = A1 * Im2
# plt.figure()
# plt.imshow(Im2, cmap='gray')
# plt.title('Im2')
# plt.show()
# plt.figure()
# plt.imshow(Im3, cmap='gray')
# plt.title('Im3')
# plt.show()
pix2 = np.unique(A1[Im3 != 0])
if pix2.size == 2: # captures mature mating
r = np.sum(Im3 == pix2[0]) / np.sum(Im3 == pix2[1])
if (2/8) <= r <= (8/2): # 4/6 to 9/6
gamete[0, iv] = pix2[0]
gamete[1, iv] = pix2[1]
gamete[2, iv] = its
for iv in range(int(mat['no_obj'][0, 0])):
if gamete[0, iv] == 0 and gamete[1, iv] == 0:
tp_mat_start = int(mat['cell_data'][iv, 0]) # First appearance of mating event "iv"
M1 = MTrack[tp_mat_start-1] # Mat Track at tp_mat_start
for its in range(tp_mat_start - 1, int(mat['shock_period'][1, 0]), -1): # Loop through time from 1 tp before mating to one time point after shock
A1 = Mask3[its].astype(float).T
M2 = (M1 == iv + 1).astype(float).T
Im1 = (M2 > threshold_otsu(M2)).astype(float)
Im2 = thin(Im1, 10).astype(float)
Im3 = A1 * Im2
# plt.figure()
# plt.imshow(Im3, cmap='gray')
# plt.title('Im3_2')
# plt.show()
pix2 = np.unique(A1[Im3 != 0])
if pix2.size == 1: # captures ascus mating
gamete[0, iv] = pix2[0]
gamete[2, iv] = its
sio.savemat(os.path.join(sav_path, f'{pos}_gametes.mat'), {"gamete": gamete}, do_compression=True)