-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze.py
114 lines (93 loc) · 3.21 KB
/
analyze.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
import os
import argparse
import pandas as pd
from glob import glob
from skimage import io
from tqdm import tqdm
import functions
from modules.pearson import pearson
from modules.manders import manders
IMG_DIR = "data/images"
MASK_DIR = "data/masks"
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Analyze colocalization in images.",
epilog="Turku BioImaging - Image Data Team - https://bioimaging.fi",
)
parser.add_argument(
"--disable-pearson",
dest="pearson",
action="store_false",
help="Calculate Pearson correlation coefficient",
)
parser.add_argument(
"--disable-manders-otsu",
dest="manders_otsu",
action="store_false",
help="Calculate Manders using Otsu thresholding",
)
parser.add_argument(
"--manders-costes",
dest="manders_costes",
action="store_true",
help="Calculate Manders using Costes auto-thresholding",
)
parser.add_argument(
"--disable-masks",
dest="with_masks",
action="store_false",
help="Use masks to subtract background",
)
args = parser.parse_args()
# read images and masks
# first_img_paths = sorted(glob("data/first_images/*"))
# second_img_paths = sorted(glob("data/second_images/*"))
img_paths = sorted(glob(f"{IMG_DIR}/*"))
mask_paths = sorted(glob(f"{MASK_DIR}/*"))
if args.with_masks == True:
mask_paths = sorted(glob("data/masks/*"))
else:
mask_paths = None
results = []
# loop through images
for idx, p in tqdm(enumerate(img_paths), total=len(img_paths)):
# split the image channels
img = io.imread(p)
mask = io.imread(mask_paths[idx])
first_img = img[:, 0, :, :]
second_img = img[:, 1, :, :]
first_img_fname = os.path.basename(p).replace(".tif", "_c1.tif")
second_img_fname = os.path.basename(p).replace(".tif", "_c2.tif")
result = {
"first_image_fname": first_img_fname,
"second_image_fname": second_img_fname,
}
if mask_paths is not None:
mask = io.imread(mask_paths[idx])
else:
mask = None
if args.pearson == True:
pearson_r = pearson(img1=first_img, img2=second_img, mask=mask)
result["pearson_r"] = pearson_r
if args.manders_otsu == True:
(
otsu_manders_m1,
otsu_manders_m2,
otsu_img1,
otsu_img2,
) = functions.manders_otsu(first_img, second_img, mask=mask)
result["otsu_manders_m1"] = otsu_manders_m1
result["otsu_manders_m2"] = otsu_manders_m2
if args.manders_costes == True:
(
costes_m1,
costes_m2,
costes_img1_thresholded,
costes_img2_thresholded,
) = manders(img1=first_img, img2=second_img, mask=mask)
result["costes_manders_m1"] = costes_m1
result["costes_manders_m2"] = costes_m2
results.append(result)
df = pd.DataFrame(results)
os.makedirs("results", exist_ok=True)
df.to_csv("results/colocalization_results.csv")