-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestFolder.py
137 lines (112 loc) · 4.09 KB
/
testFolder.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
import random
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages
# start JVM for compatibility with VSI files
import javabridge
import bioformats
javabridge.start_vm(class_path=bioformats.JARS)
from settings import Settings
from singleCompositeImage import singleCompositeImage
from commonFunctions import *
settings = Settings()
# BK siRNA experiments
folder = 6 # July 31, 2019 # 4 (July 2020, Discard) # 3 (Feb 2020) # 1 (Sept 2020)
debug = False
model = loadKerasModel(settings.kerasModel)
# retrieve settings using 'folder'
root = settings.folder_dicts[folder]['root']
pattern = settings.folder_dicts[folder]['pattern']
files = find(pattern, root)
dapi_ch = settings.folder_dicts[folder]['dapi_ch']
dapi_gamma = settings.folder_dicts[folder]['dapi_gamma']
o4_ch = settings.folder_dicts[folder]['o4_ch']
o4_gamma = settings.folder_dicts[folder]['o4_gamma']
marker_index = settings.folder_dicts[folder]['marker_index']
thres = settings.folder_dicts[folder]['thres']
# start analysis
print(f"Found {len(files)} matching '{pattern}' in '{root}'")
print("***************************")
# select file sample
if debug:
print("Running debug on a few images...")
# select five files at random
# files = list(files[i] for i in random.sample(list(range(len(files))), 1))
# select five files to do manual count comparisons
files = list(files[i] for i in range(3,4))
else:
print("Starting to analyze images")
results = []
def parseFileName(imgFile):
"""Extract stage, well and image position from file name."""
imgFile_split = imgFile.split('_')
if(imgFile_split[0].upper().find('PRE')>0):
stage = "PRE"
elif(imgFile_split[0].upper().find('POST')>0):
stage = "POST"
else:
stage = None
well_position = imgFile_split[1].split('-')
well = well_position[0]
try:
position = well_position[1]
except:
print(f"Error parsing: {imgFile}")
position = None
return [stage, well, position]
with PdfPages('results_folder_' + str(folder) + '.pdf') as export_pdf:
for file in files:
path = file['path']
imgFile = file['name']
print(f"Processing: {path}\\{imgFile}")
# parse file names
stage, well, position = parseFileName(imgFile)
try:
sCI = singleCompositeImage(path, imgFile, dapi_ch, o4_ch=o4_ch, scalefactor=1, debug=debug, dapi_gamma=dapi_gamma, o4_gamma=o4_gamma)
sCI.processDAPI(threshold_method=thres, debug=debug) # based on manual counts (see OneNote)
if debug:
sCI.reportResults()
if "model" in locals():
sCI.processCells()
sCI.getPredictions(model)
sCI.processPredictions(export_pdf)
results.append({
'path': sCI.path,
'imgFile': sCI.imgFile,
'stage': stage,
'well': well,
'position': position,
'nucleiCount': sCI.nucleiCount,
'o4pos_count': sCI.o4pos_count,
'o4neg_count': sCI.o4neg_count,
'o4%': "{:.2%}".format(sCI.o4pos_count/(sCI.o4pos_count+sCI.o4neg_count)),
})
else:
results.append({
'path': sCI.path,
'imgFile': sCI.imgFile,
'stage': stage,
'well': well,
'position': position,
'nucleiCount': sCI.nucleiCount,
})
except:
print(f"Failed on path '{path}'. Image: {imgFile}")
raise
# output results as csv
import csv
filename = 'results_folder_' + str(folder) + '.csv'
with open(filename,'w',newline='') as f:
# report analysis settings
w = csv.writer(f)
w.writerow([
'dapi_gamma', dapi_gamma,
'o4_gamma', o4_gamma,
'thres', thres,
])
w.writerow('')
# results
w = csv.DictWriter(f, results[0].keys())
w.writeheader()
w.writerows(results)
javabridge.kill_vm()
print('All Done')