1
- import os
2
- os .environ ["VIPS_CONCURRENCY" ] = "20"
3
1
import argparse
4
- import logging
5
2
import json
3
+ import logging
4
+ import os
6
5
import pathlib
7
6
8
- from preprocessing_utils .prepare_data import prepare_data
9
7
from assembly_utils .detect_configuration import detect_configuration
10
- from pythostitcher_utils .preprocess import preprocess
11
- from pythostitcher_utils .optimize_stitch import optimize_stitch
8
+ from preprocessing_utils .prepare_data import prepare_data
12
9
from pythostitcher_utils .fragment_class import Fragment
13
- from pythostitcher_utils .get_resname import get_resname
14
10
from pythostitcher_utils .full_resolution import generate_full_res
11
+ from pythostitcher_utils .get_resname import get_resname
12
+ from pythostitcher_utils .optimize_stitch import optimize_stitch
13
+ from pythostitcher_utils .preprocess import preprocess
14
+
15
+ os .environ ["VIPS_CONCURRENCY" ] = "20"
15
16
16
17
17
- def load_parameter_configuration (data_dir , save_dir , patient_idx ):
18
+ def load_parameter_configuration (data_dir , save_dir , output_res ):
18
19
"""
19
20
Convenience function to load all the PythoStitcher parameters and pack them up
20
21
in a dictionary for later use.
21
22
"""
22
23
23
24
# Verify its existence
24
- config_file = pathlib .Path ("./ config/parameter_config.json" )
25
+ config_file = pathlib .Path (). absolute (). parent . joinpath ( " config/parameter_config.json" )
25
26
assert config_file .exists (), "parameter config file not found"
26
27
27
28
# Load main parameter config
28
29
with open (config_file ) as f :
29
30
parameters = json .load (f )
30
31
32
+ # Convert model weight paths to absolute paths
33
+ parameters ["weights_fragment_classifier" ] = pathlib .Path ().absolute ().parent .joinpath (parameters ["weights_fragment_classifier" ])
34
+ parameters ["weights_jigsawnet" ] = pathlib .Path ().absolute ().parent .joinpath (parameters ["weights_jigsawnet" ])
35
+
31
36
# Insert parsed arguments
32
37
parameters ["data_dir" ] = data_dir
33
38
parameters ["save_dir" ] = save_dir
34
- parameters ["patient_idx" ] = patient_idx
39
+ parameters ["patient_idx" ] = data_dir .name
40
+ parameters ["output_res" ] = output_res
35
41
parameters ["fragment_names" ] = [i .name for i in data_dir .joinpath ("raw_images" ).iterdir () if i .is_dir ()]
36
42
parameters ["n_fragments" ] = len (parameters ["fragment_names" ])
37
43
parameters ["resolution_scaling" ] = [i / parameters ["resolutions" ][0 ] for i in parameters ["resolutions" ]]
@@ -69,32 +75,43 @@ def collect_arguments():
69
75
description = "Stitch prostate histopathology images into a pseudo whole-mount image"
70
76
)
71
77
parser .add_argument (
72
- "--datadir" , required = True , help = "General data directory with all patients"
78
+ "--datadir" ,
79
+ required = True ,
80
+ type = pathlib .Path ,
81
+ help = "Path to the case to stitch"
73
82
)
74
83
parser .add_argument (
75
- "--savedir" , required = True , help = "Directory to save the results"
84
+ "--savedir" ,
85
+ required = True ,
86
+ type = pathlib .Path ,
87
+ help = "Directory to save the results"
76
88
)
77
89
parser .add_argument (
78
- "--patient" , required = True , help = "Patient to process"
90
+ "--resolution" ,
91
+ required = True ,
92
+ default = 0.25 ,
93
+ type = float ,
94
+ help = "Output resolution (µm/pixel) of the reconstructed image. Should be roughly "
95
+ "in range of 0.25-20."
79
96
)
80
97
args = parser .parse_args ()
81
98
82
99
# Extract arguments
83
- patient_idx = args .patient
84
- data_dir = pathlib .Path (args .datadir ).joinpath (patient_idx )
85
- save_dir = pathlib . Path ( args .savedir ). joinpath ( patient_idx )
100
+ data_dir = pathlib . Path ( args .datadir )
101
+ save_dir = pathlib .Path (args .savedir ).joinpath (data_dir . name )
102
+ resolution = args .resolution
86
103
87
- assert pathlib .Path (args .datadir ).is_dir (), "provided data directory doesn't exist"
88
- assert data_dir .is_dir (), "provided patient could not be found in data directory"
104
+ assert data_dir .is_dir (), "provided patient directory doesn't exist"
89
105
assert data_dir .joinpath ("raw_images" ).is_dir (), "patient has no 'raw_images' directory"
90
106
assert len (list (data_dir .joinpath ("raw_images" ).iterdir ())) > 0 , "no images found in 'raw_images' directory"
107
+ assert resolution > 0 , "output resolution cannot be negative"
91
108
92
109
print (f"\n Running job with following parameters:"
93
- f"\n - Data dir: { args . datadir } "
110
+ f"\n - Data dir: { data_dir } "
94
111
f"\n - Save dir: { save_dir } "
95
- f"\n - Patient : { patient_idx } " )
112
+ f"\n - Output resolution : { resolution } µm/pixel \n " )
96
113
97
- return data_dir , save_dir , patient_idx
114
+ return data_dir , save_dir , resolution
98
115
99
116
100
117
def main ():
@@ -115,7 +132,7 @@ def main():
115
132
/data
116
133
/{Patient_identifier}
117
134
/raw_images
118
- {fragment_name}.mrxs
135
+ {fragment_name}.mrxs§
119
136
{fragment_name}.mrxs
120
137
/raw_masks
121
138
{fragment_name}.tif
@@ -126,8 +143,8 @@ def main():
126
143
127
144
### ARGUMENT CONFIGURATION ###
128
145
# Collect arguments
129
- data_dir , save_dir , patient_idx = collect_arguments ()
130
- parameters = load_parameter_configuration (data_dir , save_dir , patient_idx )
146
+ data_dir , save_dir , output_res = collect_arguments ()
147
+ parameters = load_parameter_configuration (data_dir , save_dir , output_res )
131
148
132
149
# Initiate logging file
133
150
logfile = save_dir .joinpath ("pythostitcher_log.log" )
@@ -144,9 +161,10 @@ def main():
144
161
log = logging .getLogger ("pythostitcher" )
145
162
146
163
parameters ["log" ] = log
147
- parameters ["log" ].log (parameters ["my_level" ], f"Running job with following parameters:"
148
- f"\n - Data dir: { parameters ['data_dir' ]} "
149
- f"\n - Save dir: { parameters ['save_dir' ]} \n " )
164
+ parameters ["log" ].log (parameters ["my_level" ], f"Running job with following parameters:" )
165
+ parameters ["log" ].log (parameters ["my_level" ], f" - Data dir: { parameters ['data_dir' ]} " )
166
+ parameters ["log" ].log (parameters ["my_level" ], f" - Save dir: { parameters ['save_dir' ]} " )
167
+ parameters ["log" ].log (parameters ["my_level" ], f" - Output resolution: { parameters ['output_res' ]} \n " )
150
168
151
169
if not data_dir .joinpath ("raw_masks" ).is_dir ():
152
170
parameters ["log" ].log (
@@ -155,7 +173,8 @@ def main():
155
173
f"PythoStitcher with pregenerated tissuemasks, please put these files in "
156
174
f"[{ data_dir .joinpath ('raw_masks' )} ]. If no tissuemasks are supplied, "
157
175
f"PythoStitcher will use a generic tissue segmentation which may not perform "
158
- f"as well as the AI-generated masks." )
176
+ f"as well as the AI-generated masks. In addition, PythoStitcher will not "
177
+ f"be able to generate the full resolution end result." )
159
178
160
179
### MAIN PYTHOSTITCHER #s##
161
180
# Preprocess data
@@ -182,7 +201,7 @@ def main():
182
201
fragments = []
183
202
for im_path , fragment_name in sol .items ():
184
203
fragments .append (Fragment (
185
- im_path = im_path ,
204
+ im_path = im_path ,
186
205
fragment_name = fragment_name ,
187
206
kwargs = parameters )
188
207
)
@@ -198,7 +217,7 @@ def main():
198
217
199
218
parameters ["log" ].log (
200
219
parameters ["my_level" ],
201
- f"Succesfully stitched solution { count_sol } "
220
+ f"### Succesfully stitched solution { count_sol } ### \n "
202
221
)
203
222
204
223
return
0 commit comments