forked from brainglobe/brainglobe-template-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessing_wing-disc.py
221 lines (199 loc) · 7.91 KB
/
preprocessing_wing-disc.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import argparse
from pathlib import Path
import ants
import numpy as np
import pandas as pd
from brainglobe_utils.IO.image import load_any, save_any
from dask import array as da
from loguru import logger
from brainglobe_template_builder.io import (
file_path_with_suffix,
save_as_asr_nii,
)
from brainglobe_template_builder.preproc.masking import create_mask
from brainglobe_template_builder.preproc.transform_utils import (
downsample_anisotropic_image_stack,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Download source image")
parser.add_argument(
"--source_data_root",
type=str,
help="Path to the source data folder. The source data should contain"
"a subfolder per subject, with picture files within it",
required=True,
)
parser.add_argument(
"--template_building_root",
type=str,
help="Path to the template-building root folder.Results will be "
"written to the rawdata folder.",
required=True,
)
parser.add_argument(
"--target_isotropic_resolution",
type=int,
help="Target isotropic resolution",
required=True,
)
parser.add_argument(
"--data_catalog",
type=str,
help="The full path to the data catalog file",
required=True,
)
parser.add_argument(
"--dataset",
type=str,
help="The dataset used to process",
required=True,
)
args = parser.parse_args()
source_data = Path(args.source_data_root)
template_building_root = Path(args.template_building_root)
target_isotropic_resolution = int(args.target_isotropic_resolution)
in_plane_resolution = 0.55
out_of_plane_resolution = 1
in_plane_factor = int(
np.ceil(target_isotropic_resolution / in_plane_resolution)
)
axial_factor = int(
np.ceil(target_isotropic_resolution / out_of_plane_resolution)
)
template_raw_data = template_building_root / "rawdata"
template_raw_data.mkdir(exist_ok=True, parents=True)
# Load the data catalog from argument
data_catalog_path = Path(args.data_catalog)
data_catalog = pd.read_csv(data_catalog_path)
dataset = args.dataset
# Specified the dataset to process
dataset_catalog = data_catalog[data_catalog["dataset"] == dataset]
logger.debug(f"Loaded {dataset} dataset catalog from {data_catalog_path}.")
# Check if there are right wing discs is in the dataset
right_wingdisc_catalog = dataset_catalog[dataset_catalog["is_left"] == "n"]
if right_wingdisc_catalog.empty:
logger.info(f"No right wing discs found in {dataset} dataset.")
for sample_folder in source_data.iterdir():
# Load the images and specify the filename of processed images
logger.info(f"Downsampling {sample_folder}...")
sample_id = str(sample_folder.name).split("_")[0].lower()
channel = "membrane"
downsampled_filename = (
f"{sample_id}_res-{target_isotropic_resolution}"
f"um_channel-{channel}.tif"
)
assert Path(sample_folder).exists(), f"{sample_folder} not found"
original_file_path = Path(sample_folder) / f"{sample_folder.name}.tif"
assert Path(
original_file_path
).exists(), f"Filepath {original_file_path} not found"
image_array = load_any(original_file_path)
# Do mirroring if the sample is right wing disc
if (
str(sample_folder.name)
in right_wingdisc_catalog["filename"].astype(str).tolist()
):
image_array = np.flip(image_array, axis=2)
logger.info(f"Mirrored {sample_folder.name}.")
# Downsample the image array
image_dask = da.from_array(image_array, chunks={0: 1, 1: -1, 2: -1})
down_sampled_image = downsample_anisotropic_image_stack(
image_dask, in_plane_factor, axial_factor
)
down_sampled_image = down_sampled_image.astype(np.uint16)
# Save the downsampled image as tif
saving_folder = (
template_raw_data
/ f"{source_data.name}"
/ downsampled_filename.split(".")[0]
)
saving_folder.mkdir(exist_ok=True, parents=True)
assert Path(
saving_folder
).exists(), f"Filepath {saving_folder} not found"
saving_path = saving_folder / downsampled_filename
save_any(down_sampled_image, saving_path)
logger.info(
f"{sample_folder} downsampled, saved as {downsampled_filename}"
)
# Save the downsampled image as nifti
nii_path = file_path_with_suffix(
saving_path, "_downsampled", new_ext=".nii.gz"
)
vox_sizes = [
target_isotropic_resolution,
] * 3
save_as_asr_nii(down_sampled_image, vox_sizes, nii_path)
logger.info(f"Saved downsampled image as {nii_path.name}.")
# Generate the wingdisc mask
image_ants = ants.image_read(nii_path.as_posix())
mask_data = create_mask(
image_ants.numpy(),
gauss_sigma=10,
threshold_method="triangle",
closing_size=5,
)
mask_path = file_path_with_suffix(nii_path, "_mask")
mask = image_ants.new_image_like(mask_data.astype(np.uint8))
ants.image_write(mask, mask_path.as_posix())
logger.debug(
f"Generated brain mask with shape: {mask.shape} "
f"and saved as {mask_path.name}."
)
# Plot the mask over the image to check
mask_plot_path = (
saving_folder / f"{sample_id}_downsampled_mask-overlay.png"
)
ants.plot(
image_ants,
mask,
overlay_alpha=0.5,
axis=1,
title="Wingdisc mask over image",
filename=mask_plot_path.as_posix(),
)
logger.debug("Plotted overlay to visually check mask.")
# Write a text file to record the file path to the downsampled image and mask created
with open("brain_path.txt", "a") as f:
f.write(f"{nii_path} + \n")
with open("mask_path.txt", "a") as f:
f.write(f"{mask_path} + \n")
logger.debug(
"Recorded the file path to the brain_path.txt and mask_path.txt created."
)
# Write a text file to record the file path to the downsampled image and mask created
nii_path_str = str(nii_path)
downsampled_txt_file_name = "brain_paths.txt"
downsampled_txt_file_path = (
template_raw_data
/ f"{source_data.name}"
/ downsampled_txt_file_name
)
mask_path_str = str(mask_path)
mask_txt_file_name = "mask_paths.txt"
mask_txt_file_path = (
template_raw_data / f"{source_data.name}" / mask_txt_file_name
)
# Read the file(if exist first) to check if the path already exists
try:
with open(downsampled_txt_file_path, "r", encoding="utf-8") as f:
lines_downsampled = (
f.read().splitlines()
) # Read lines without newlines
with open(mask_txt_file_path, "r", encoding="utf-8") as f:
lines_mask = f.read().splitlines()
except FileNotFoundError:
lines_downsampled = []
lines_mask = [] # If file doesn't exist, start with an empty list
if nii_path_str not in lines_downsampled:
with open(downsampled_txt_file_path, "a", encoding="utf-8") as f:
f.write(f"{nii_path_str}\n")
logger.debug(
f"Recorded {nii_path_str} in {downsampled_txt_file_path}."
)
if mask_path_str not in lines_mask:
with open(mask_txt_file_path, "a", encoding="utf-8") as f:
f.write(f"{mask_path_str}\n")
logger.debug(
f"Recorded {mask_path_str} in {mask_txt_file_path}."
)