-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpet.py
160 lines (138 loc) · 5.79 KB
/
simpet.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import os
import sys
import shutil
from pathlib import Path
from os.path import join, exists
from omegaconf import DictConfig, OmegaConf
from pyprojroot import here
from utils import tools
from src.simset import simset_sim as sim
class SimPET(object):
"""
This class provides main SimPET functions.
You have to initialize the class with a params file.
Before using SimPET, check out the README.
"""
def __init__(self, cfg: DictConfig) -> None:
self.cfg = OmegaConf.to_container(cfg)
self.simpet_dir = here()
self.config = {k: v for k, v in self.cfg.items() if k != "params"}
self.params = self.cfg["params"]
self.sim_type = self.params["sim_type"]
self.scanner = self.params["scanner"]
self.scanner_model = (
self.params["scanner"]["scanner_name"].replace(" ", "_").lower()
)
if self.cfg["dir_data_path"] is not None:
self.dir_data = Path(self.cfg["dir_data_path"])
else:
self.dir_data = here().joinpath("Data")
if self.cfg["dir_results_path"] is not None:
self.dir_results = Path(self.cfg["dir_results_path"])
else:
self.dir_results = here().joinpath("Data")
self.act_map = self.dir_data.joinpath(self.params["patient_dirname"]).joinpath(
self.params["act_map"]
)
self.att_map = self.dir_data.joinpath(self.params["patient_dirname"]).joinpath(
self.params["att_map"]
)
self.output_dir = self.dir_results.joinpath(self.params["output_dir"])
self.log_file = self.output_dir.joinpath("logfile.log")
self.maps_dir = self.output_dir.joinpath("Maps")
make_dirs = [self.dir_data, self.dir_results, self.output_dir, self.maps_dir]
for dir_ in make_dirs:
dir_.mkdir(parents=True, exist_ok=True)
def simset_simulation(self, act_map, att_map):
projections_dir = str(
self.output_dir.joinpath("SimSET_Sim_" + self.scanner_model)
)
if self.params.get("do_simulation") == 1:
if exists(projections_dir):
if self.config.get("interactive_mode") == 1:
print(
"The introduced output dir already has a SimSET simulation.Proceeding will delete it."
)
remove = input(" Write 'Y' to delete it: ")
print(
"You can disable this prompt by deactivating interactive mode in the config file."
)
if remove == "Y":
shutil.rmtree(projections_dir)
else:
raise Exception("The simulation was aborted.")
## Place some logging here
sys.exit(1)
else:
shutil.rmtree(projections_dir)
os.makedirs(projections_dir)
my_simulation = sim.SimSET_Simulation(
self.params,
self.config,
act_map,
att_map,
self.scanner,
projections_dir,
)
my_simulation.run()
if self.params.get("do_reconstruction") == 1:
reconstruction_type = self.scanner.get("recons_type")
if not exists(projections_dir):
raise Exception(
"The projections directory does not exist. Run your simulation first."
)
## Place some logging here
sys.exit(1)
postprocess_log = join(projections_dir, "postprocessing.log")
# If it is a new simulation or if the trues.hdr were not added previously, it makes the postprocessing
if not exists(postprocess_log):
print(
"Your simulation SimSET outputs were not processed previously. We will try it now..."
)
my_simulation = sim.SimSET_Simulation(
self.params,
self.config,
act_map,
att_map,
self.scanner,
projections_dir,
)
my_simulation.simulation_postprocessing()
reconstruction_dir = join(projections_dir, reconstruction_type)
if exists(reconstruction_dir):
if self.config.get("interactive_mode") == 1:
print(
"The introduced output dir already has a %s reconstruction.Proceeding will delete it."
% reconstruction_type
)
remove = input(" Write 'Y' to delete it: ")
print(
"You can disable this prompt by deactivating interactive mode in the config file."
)
if remove == "Y":
shutil.rmtree(reconstruction_dir)
else:
raise Exception("The simulation was aborted.")
## Place some logging here
sys.exit(1)
else:
shutil.rmtree(reconstruction_dir)
my_reconstruction = sim.SimSET_Reconstruction(
self.params,
self.config,
projections_dir,
self.scanner,
reconstruction_dir,
reconstruction_type,
)
my_reconstruction.run()
def run(self):
act_map, att_map = tools.convert_map_values(
str(self.act_map),
str(self.att_map),
str(self.maps_dir),
str(self.log_file),
mode=self.sim_type,
)
if self.sim_type == "SimSET":
self.simset_simulation(act_map, att_map)