-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcellSegmentation.py
More file actions
107 lines (91 loc) · 3.81 KB
/
cellSegmentation.py
File metadata and controls
107 lines (91 loc) · 3.81 KB
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
from types import SimpleNamespace
from get_dapi import extract_dapi_channel
from segment_nuclei import segment_whole_image
from number_of_nuclei import count_nuclei
from mask_segmentation_with_dapi import apply_tissue_mask
from nuclei_size_filtering import filter_nuclei_by_size
from skimage import io, color
import tifffile as tiff
from convert_tif_to_xml import segmentation_to_polygon_xml
from convert_xml_to_json import xml_to_json
import tiffslide
import numpy as np
from skimage.color import rgb2gray
import time
def main(args):
# Step 1: Extract DAPI channel
if args.image_type == 'DAPI':
try:
start_time = time.time()
dapi = extract_dapi_channel(args.reg_path, args.dapi_channel)
end_time = time.time()
print(f"DAPI loading time: {end_time - start_time:.2f} seconds")
except (FileNotFoundError, ValueError) as e:
print(f"Error: {e}")
# Step 2: Run segmentation on dapi
segmentation = segment_whole_image(
image = dapi,
n_gpus= args.n_gpus,
channels= args.channels,
diameter= args.diameter,
batch_size= args.batch_size,
flow_threshold= args.flow_threshold,
cellprob_threshold= args.cellprob_threshold,
min_size= args.min_size
)
# Step 3: Mask segmentation with DAPI to remove the nuclei outside the tissue section
if args.maskSeg:
segmentation = apply_tissue_mask(dapi, segmentation)
# Step 4: Filter nuclei by size
if args.filterBySize:
filter_nuclei_by_size(segmentation, args.outputdir + 'nuclei.xml', args.lower_area_percentile, args.upper_area_percentile)
else:
segmentation_to_polygon_xml(segmentation, args.outputdir + 'nuclei.xml')
# Step 5: Filter nuclei by intensity
if args.filterByIntensity:
filtered_tree, removed_tree = process_intensity_filtering(
xml_file= args.outputdir + 'nuclei.xml',
tif_file= args.reg_path,
top_percentage= args.top_percentage,
px= args.px_padding
)
# Output XML paths
filtered_output_xml = args.outputdir + "filtered_output.xml" # Replace with desired output path
removed_output_xml = args.outputdir + "removed_output.xml" # Replace with desired output path
# Save the filtered XML file
filtered_tree.write(filtered_output_xml, pretty_print=True)
print(f"Filtered XML saved to: {filtered_output_xml}")
# Save the removed XML file
removed_tree.write(removed_output_xml, pretty_print=True)
print(f"Removed XML saved to: {removed_output_xml}")
print("Intensity filtering process completed.")
# Convert and update JSON
xml_to_json(filtered_output_xml, output_json = args.outputdir + "nuclei.json")
else:
xml_to_json(args.outputdir + 'nuclei.xml', args.outputdir + "nuclei.json")
if __name__ == '__main__':
args_dict = {
'image_type': 'DAPI',
'reg_path': '/blue/pinaki.sarder/cell-segmentation/Data/Lung/D016-RLL-11B2-6/D016-RLL-11B2-6_Reg.tiff',
'dapi_channel': 0,
'n_gpus' : 1,
'channels' : [0, 0],
'diameter' : None, # Auto-estimate object diameter
'batch_size' : 256, # Batch size for Cellpose
'flow_threshold' : 0.4,
'cellprob_threshold' : 0.0,
'min_size' : 17,
'maskSeg' : False,
'filterBySize': False,
'filterByIntensity' : False,
'lower_area_percentile': 0.0,
'upper_area_percentile': 100.0,
'outputdir': '/blue/pinaki.sarder/cell-segmentation/Data/Lung/D016-RLL-11B2-6/outputtest/',
'top_percentage' : 5,
'px_padding' : 5
}
args = SimpleNamespace(**args_dict)
#print(type(args))
#print(args.reg_path)
main(args)
#main(CLIArgumentParser().parse_args())