-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathml2d_avg_nissl.py
executable file
·167 lines (140 loc) · 4.67 KB
/
ml2d_avg_nissl.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
161
162
163
164
165
166
167
#!/usr/bin/env python
# Copyright 2021, Blue Brain Project, EPFL
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Machine Learning Registration Nissl/Avg Brain."""
import logging
import sys
import numpy as np
import utils
from warpme.ml_utils import load_model, merge_global_local
from atlannot.ants import stack_2d_transforms, transform
from atlannot.utils import load_volume
# Parameters
description = """\
Machine Learning registration with Nissl/Avg Brain:
fixed = Avg Brain
moving = Nissl
"""
experiment_name = utils.get_script_file_name()
v2_atlas_path = utils.get_v2_atlas_fine_path()
nissl_path = utils.get_nissl_path()
avg_path = utils.get_avg_brain_path()
thickness = 2
# Initialize the logger
logger = logging.getLogger(experiment_name)
script_info = """
Goal: computing machine learning 2D registration between the reference and
moving images/volumes.
Assumptions:
- The input images/volumes have to have the same shape.
Steps:
- Loading input images/volumes.
- For each 2D images, machine learning 2D registration.
- Applying resulting transformations to the original moving image.
- Saving results.
"""
def main():
"""Machine Learning Registration Nissl/Avg Brain."""
# Paths
output_dir = utils.get_results_dir() / experiment_name
if not utils.can_write_to_dir(output_dir):
print("Cannot write to output directory. Stopping")
return 1
# Load data
logger.info("Loading data")
avg_volume = load_volume(avg_path)
nissl_volume = load_volume(nissl_path)
v2_atlas = load_volume(v2_atlas_path, normalize=False)
# Preprocess data
logger.info("Preprocessing data")
ml_input = preprocess_volumes(
avg_volume,
nissl_volume,
)
logger.info("Loading ML models...")
model_merged = machine_learning_model()
# Registration
logger.info("Starting registration")
dfs = []
for _dim, x in enumerate(ml_input):
img_reg, delta_xy = model_merged.predict(
np.expand_dims(x, axis=0), batch_size=1
)
dfs.append(delta_xy)
df_3d = stack_2d_transforms(dfs)
# Warping
logger.info("Warping volumes")
warped_atlas = transform(
v2_atlas.astype(np.float32),
df_3d,
interpolator="genericLabel",
)
warped_atlas = warped_atlas.astype(v2_atlas.dtype)
warped_nissl = transform(nissl_volume, df_3d)
# Write output
logger.info("Saving results")
# metadata
with open(output_dir / "description.txt", "w") as fp:
fp.write(description)
with open(output_dir / "fixed_path.txt", "w") as fp:
fp.write(str(avg_path) + "\n")
with open(output_dir / "moving_path.txt", "w") as fp:
fp.write(str(nissl_path) + "\n")
with open(output_dir / "v2_atlas_path.txt", "w") as fp:
fp.write(str(v2_atlas_path) + "\n")
# volumes
np.save(output_dir / "warped_atlas", warped_atlas)
np.save(output_dir / "warped_nissl", warped_nissl)
np.save(output_dir / "df", df_3d)
logger.info(f"Finished. The results were saved to {output_dir}")
def preprocess_volumes(*volumes):
"""Preprocess volumes.
Parameters
----------
volumes : Iterable of np.ndarray
All volumes to preprocess.
Returns
-------
new_volumes : Iterable of np.ndarray
Preprocessed volumes.
"""
volumes_pre = []
for volume in volumes:
if len(volume.shape) == 2:
volume = np.expand_dims(volume, axis=0)
if len(volume.shape) == 3:
volume = np.expand_dims(volume, axis=3)
volumes_pre.append(volume)
ml_input = np.concatenate(volumes_pre, axis=3)
return ml_input
def machine_learning_model():
"""Load machine learning model.
Returns
-------
model_merged:
Machine Learning model.
"""
model_global = load_model(
"/gpfs/bbp.cscs.ch/project/proj101/"
"pretrained_models/global/calm_camel/calm_camel.h5"
)
model_local = load_model(
"/gpfs/bbp.cscs.ch/project/proj101/"
"pretrained_models/local/cute_cat/cute_cat.h5"
)
model_merged = merge_global_local(model_global, model_local)
return model_merged
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
sys.exit(main())