-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvolutionTree_Example1.py
119 lines (96 loc) · 3.88 KB
/
EvolutionTree_Example1.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
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 28 10:34:16 2022
@author: xiangyu066
Notes:
1. The advance analysis (e.g. size measurement) can visit BacterialContour_Example4.py
2. If labell with red tag, then you have to specially process it.
"""
#%%
print("Running...")
import os, glob, pickle
import numpy as np
from skimage import io
import matplotlib.pyplot as plt
import time
import warnings
warnings.simplefilter("ignore")
#
import MoPy.EvolutionTree as ET
import MoPy as mo
print("MoPy package is "+mo.__version__+".")
print("------------------------------------------------------------------")
#%%
# process parameters
th=75 # peak thershold
# working directory
# inputdir=r'.\dataSets\EvolutionTree\E. coli\HM06 (from CYC)\Gelpad\1220_PC test'
inputdir=r'.\dataSets\EvolutionTree\V. alginolyicus\VIO5\Gelpad\20170615'
#%% Initialization
print('Initializing...')
listing=glob.glob(inputdir+'\\*.tif')
nFiles=len(listing)
#%%
TrackTrees=[]
TrackLists=[]
for nFile in range(nFiles):
print('Load files...(current file: '+str(nFile+1)+' / total files: '+str(nFiles)+')')
inputfile=listing[nFile]
origina_=io.imread(inputfile)
nFrames=origina_.shape[0]
# batch processing
Cs_list=[]
branches=[]
for nFrame in range(112,113):
print('Processing...(current frame: '+str(nFrame+1)+' / total frames: '+str(nFrames)+')')
# Create a single directory
outputdir_name=inputfile.replace(inputdir+'\\','')
outputdir_name=outputdir_name.replace('.tif','')
outputdir=inputdir+'\\Analyzed\\'+outputdir_name
if not os.path.exists(outputdir): os.makedirs(outputdir)
tic=time.time()
origina=origina_[nFrame,:,:]
# segmentation
Cs_,L_ns=ET.seg_ecoli_ph100_ixon(origina,th,bright_to_bg_factor=1.2,is_check=True)
Cs_list.append(Cs_)
print("--- %s seconds ---" % (time.time()-tic))
# show
plt.figure()
plt.imshow(origina,cmap=plt.cm.gray,origin='lower')
for n in range(len(Cs_)):
Cs=Cs_[n]
plt.plot(Cs[:,0],Cs[:,1],color=np.random.rand(1,3),lw=0.3)
# auxiliary segmentation
is_human_check=ET.concave_detector(Cs,is_check=False)
if is_human_check==True:
plt.text(np.mean(Cs[:,0]),np.mean(Cs[:,1]),str(n),
color='black',fontsize=4,
horizontalalignment='center',verticalalignment='center',
bbox=dict(boxstyle="square",
ec='none',
fc=(1., 0.8, 0.8),alpha=0.7))
else:
plt.text(np.mean(Cs[:,0]),np.mean(Cs[:,1]),str(n),
color='white',fontsize=4,
horizontalalignment='center',
verticalalignment='center')
plt.title('nFrame = %d'%(nFrame))
plt.savefig(outputdir+'\\'+str(nFrame)+'.png',bbox_inches='tight')
plt.show()
# create connetion between current and previous
if (nFrame==0):
branches.append(ET.cell_conncetion(origina,Cs_,[]))
else:
branches.append(ET.cell_conncetion(origina,Cs_,Cs_list[nFrame-1]))
# build evolution tree
Tree,_=ET.cell_connection_chaincode(branches) # [nCell,ndT]
TrackTrees.append(Tree) # [nFile]
TrackLists.append(Cs_list)
#%% save database
print('Save analyzed datasets into the computer....')
outputdir=inputdir+'\\Analyzed'
outputfile=outputdir+'\\Seg_label_results'
Results=[TrackTrees,TrackLists]
with open(outputfile,'wb') as f: pickle.dump(Results,f)
#%%
print('Done.')