Skip to content

Commit 92b29d5

Browse files
committed
Merge branch 'master' of https://github.com/tfunck/brainbuilder
2 parents fc21c4a + a38b659 commit 92b29d5

5 files changed

Lines changed: 231 additions & 181 deletions

File tree

brainbuilder/interp/acqvolume.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ def thicken_sections_within_chunk(
9393
tissue_type: str = "",
9494
gaussian_sd: float = 0,
9595
width: int = None,
96-
use_adjust_section_means: bool = True,
9796
) -> None:
9897
"""Thicken sections within a chunk. A thickened section is simply a section that is expanded along the y axis to the resolution of the reconstruction.
9998
@@ -114,13 +113,6 @@ def thicken_sections_within_chunk(
114113
"Error: source volume for thickening sections is empty\n" + source_image_fn
115114
)
116115

117-
if use_adjust_section_means:
118-
adj_factors_dict = calculate_section_adjustment_factors(
119-
chunk_sect_info["nl_2d_rsl"].values,
120-
chunk_sect_info["sample"].values,
121-
os.path.dirname(thickened_fn),
122-
)
123-
124116
if width is None:
125117
width = get_thicken_width(resolution, section_thickness, scale=1)
126118

@@ -137,9 +129,6 @@ def thicken_sections_within_chunk(
137129
nl_2d_rsl = row["nl_2d_rsl"]
138130
section = nib.load(nl_2d_rsl).get_fdata().copy()
139131

140-
if use_adjust_section_means:
141-
section += adj_factors_dict[y]
142-
143132
if np.sum(section) == 0:
144133
print(f"Warning: empty frame {row_i} {row}\n")
145134

@@ -153,6 +142,7 @@ def thicken_sections_within_chunk(
153142
rep = np.repeat(section.reshape(dim), y1 - y0, axis=1)
154143

155144
rec_vol[:, y0:y1, :] += rep
145+
156146
n[:, y0:y1, :] += 1
157147

158148
# normalize by number of sections within range
@@ -393,7 +383,6 @@ def create_thickened_volumes(
393383
chunk_sect_info,
394384
resolution,
395385
gaussian_sd=gaussian_sd,
396-
use_adjust_section_means=True,
397386
width=width,
398387
)
399388

brainbuilder/interp/volinterp.py

Lines changed: 7 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import brainbuilder.utils.ants_nibabel as nib
1313
from brainbuilder.interp.acqvolume import create_thickened_volumes
14-
from brainbuilder.utils.nl_deformation_flow import nl_deformation_flow_3d
14+
from brainbuilder.utils.nl_deformation_flow import nlflow_isometric
1515
from brainbuilder.volalign import align_3d, verify_chunk_limits
1616

1717

@@ -80,81 +80,6 @@ def _interpolate_missing_sections(
8080
return vol_interp
8181

8282

83-
def nl_deformation_flow_nii(acq_fin, output_dir, interp_acq_fin, clobber=False):
84-
if not os.path.exists(interp_acq_fin) or clobber:
85-
"""
86-
Process the acq volume by applying non-linear deformation flow.
87-
88-
Parameters
89-
----------
90-
acq_fin : str
91-
Path to the acq volume file.
92-
output_dir : str
93-
Directory to save the output files.
94-
clobber : bool, optional
95-
If True, overwrite existing files. The default is False.
96-
97-
Returns
98-
-------
99-
str
100-
Path to the processed acq volume file.
101-
"""
102-
acq_img = nib.load(acq_fin)
103-
acq_vol = acq_img.get_fdata()
104-
acq_vol[acq_vol < 0] = 0
105-
106-
origin = list(acq_img.affine[[0, 2], 3])
107-
spacing = list(acq_img.affine[[0, 2], [0, 2]])
108-
109-
interp_acq_vol = nl_deformation_flow_3d(
110-
acq_vol,
111-
output_dir + "/nl_flow/",
112-
origin=origin,
113-
spacing=spacing,
114-
clobber=clobber,
115-
)
116-
117-
interp_acq_img = nib.Nifti1Image(
118-
interp_acq_vol, acq_img.affine, direction_order="lpi"
119-
)
120-
interp_acq_img.to_filename(interp_acq_fin)
121-
122-
123-
def resample_interp_vol_to_resolution(
124-
interp_acq_orig_fin,
125-
interp_acq_iso_fin: str,
126-
resolution: float,
127-
clobber: bool = False,
128-
) -> np.array:
129-
"""Resample the interpolated volume to the specified resolution.
130-
Parameters
131-
----------
132-
interp_acq_vol : np.array
133-
Interpolated volume.
134-
acq_img : nib.Nifti1Image
135-
136-
"""
137-
if not os.path.exists(interp_acq_iso_fin) or clobber:
138-
interp_acq_img = nib.load(interp_acq_orig_fin) # type:ignore[assignment]
139-
interp_acq_vol = interp_acq_img.get_fdata()
140-
141-
slice_thickness = interp_acq_img.affine[1, 1]
142-
143-
y_new = int(np.round(interp_acq_vol.shape[1] / (resolution / slice_thickness)))
144-
145-
interp_acq_vol = resize(
146-
interp_acq_vol,
147-
(interp_acq_vol.shape[0], y_new, interp_acq_vol.shape[2]),
148-
order=1,
149-
)
150-
151-
aff_iso = interp_acq_img.affine
152-
153-
aff_iso[1, 1] = resolution
154-
155-
nib.Nifti1Image(interp_acq_vol, aff_iso, direction_order="lpi").to_filename(
156-
interp_acq_iso_fin
157-
)
15883

15984

16085
def volumetric_interpolation(
@@ -210,20 +135,15 @@ def volumetric_interpolation(
210135
chunk_info_thickened = pd.read_csv(chunk_info_thickened_csv)
211136

212137
print(f"Interpolation Chunk: {chunk}, Acquisition: {acq}")
138+
cls_fin = chunk_info_thickened["thickened_cls"].values[0]
213139
acq_fin = chunk_info_thickened["thickened"].values[0]
214140

215-
interp_acq_iso_fin = acq_fin.replace("thickened", "interp-vol_iso")
216-
interp_acq_fin = acq_fin.replace("thickened", "interp-vol_orig")
141+
interp_acq_iso_fin, nlflow_tfm_list = nlflow_isometric(acq_fin, curr_output_dir, resolution, clobber=clobber)
217142

218-
print("Resampling", interp_acq_fin)
219-
nl_deformation_flow_nii(
220-
acq_fin, curr_output_dir, interp_acq_fin, clobber=clobber
221-
)
143+
interp_cls_iso_fin = nlflow_isometric(
144+
cls_fin, curr_output_dir, resolution, tfm_list = nlflow_tfm_list, clobber=clobber
145+
)
222146

223-
print("Resampling", interp_acq_iso_fin)
224-
resample_interp_vol_to_resolution(
225-
interp_acq_fin, interp_acq_iso_fin, resolution, clobber=clobber
226-
)
227147

228148
row = pd.DataFrame(
229149
{
@@ -232,6 +152,7 @@ def volumetric_interpolation(
232152
"chunk": [chunk],
233153
"acquisition": [acq],
234154
"interp_nat": [interp_acq_iso_fin],
155+
"interp_cls_nat": [interp_cls_iso_fin],
235156
}
236157
)
237158

brainbuilder/qc/validate_interp_error.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ def plot_validation_interp_error(
685685
print(np.mean(df["error_2d"]), "+/-", np.std(df["error_2d"]))
686686
print(np.mean(df["error"]), "+/-", np.std(df["error"]))
687687
plt.suptitle("Validation of 2D & 3D Interpolation Error", fontsize=16)
688-
plt.savefig(f"{output_dir}/validation_interp_error.png")
688+
plt.savefig(f"{output_dir}/validation_interp_error.png", dpi=300)
689689
print(f"{output_dir}/validation_interp_error.png")
690690
plt.close()
691691
plt.cla()
@@ -706,7 +706,7 @@ def plot_validation_interp_error(
706706
# plt.ylabel('Interpolation Error %')
707707
# plt.xlabel('Volume Change %')
708708

709-
plt.savefig(f"{output_dir}/validation_interp_error_vs_volume.png")
709+
plt.savefig(f"{output_dir}/validation_interp_error_vs_volume.png", dpi=300)
710710
print(f"{output_dir}/validation_interp_error_vs_volume.png")
711711

712712
# plt.subplot(2, 2, 4)

0 commit comments

Comments
 (0)