Skip to content

Commit

Permalink
Merge pull request #341 from IMSY-DKFZ/T338_device_at_beginning
Browse files Browse the repository at this point in the history
T338 msot acuity usable for hetero tissues
  • Loading branch information
frisograce authored Jul 5, 2024
2 parents 06657ea + 783f543 commit 4c4eb45
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
27 changes: 21 additions & 6 deletions simpa/core/device_digital_twins/pa_devices/ithera_msot_acuity.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SPDX-FileCopyrightText: 2021 Division of Intelligent Medical Systems, DKFZ
# SPDX-FileCopyrightText: 2021 Janek Groehl
# SPDX-License-Identifier: MIT
import torch
import torch.nn.functional as F

from simpa.core.device_digital_twins import PhotoacousticDevice, \
CurvedArrayDetectionGeometry, MSOTAcuityIlluminationGeometry
Expand Down Expand Up @@ -105,6 +107,8 @@ def update_settings_for_use_of_model_based_volume_creator(self, global_settings)
probe_size_mm = self.probe_height_mm
mediprene_layer_height_mm = self.mediprene_membrane_height_mm
heavy_water_layer_height_mm = probe_size_mm - mediprene_layer_height_mm
spacing_mm = global_settings[Tags.SPACING_MM]
old_volume_height_pixels = int(global_settings[Tags.DIM_VOLUME_Z_MM] / spacing_mm)

if Tags.US_GEL in volume_creator_settings and volume_creator_settings[Tags.US_GEL]:
us_gel_thickness = np.random.normal(0.4, 0.1)
Expand All @@ -122,13 +126,10 @@ def update_settings_for_use_of_model_based_volume_creator(self, global_settings)
# 1 voxel is added (0.5 on both sides) to make sure no rounding errors lead to a detector element being outside
# of the simulated volume.

if global_settings[Tags.DIM_VOLUME_X_MM] < round(self.detection_geometry.probe_width_mm) + \
global_settings[Tags.SPACING_MM]:
width_shift_for_structures_mm = (round(self.detection_geometry.probe_width_mm) +
global_settings[Tags.SPACING_MM] -
if global_settings[Tags.DIM_VOLUME_X_MM] < round(self.detection_geometry.probe_width_mm) + spacing_mm:
width_shift_for_structures_mm = (round(self.detection_geometry.probe_width_mm) + spacing_mm -
global_settings[Tags.DIM_VOLUME_X_MM]) / 2
global_settings[Tags.DIM_VOLUME_X_MM] = round(self.detection_geometry.probe_width_mm) + \
global_settings[Tags.SPACING_MM]
global_settings[Tags.DIM_VOLUME_X_MM] = round(self.detection_geometry.probe_width_mm) + spacing_mm
self.logger.debug(f"Changed Tags.DIM_VOLUME_X_MM to {global_settings[Tags.DIM_VOLUME_X_MM]}")
else:
width_shift_for_structures_mm = 0
Expand All @@ -139,6 +140,20 @@ def update_settings_for_use_of_model_based_volume_creator(self, global_settings)
self.logger.debug("Adjusting " + str(structure_key))
structure_dict = volume_creator_settings[Tags.STRUCTURES][structure_key]
if Tags.STRUCTURE_START_MM in structure_dict:
for molecule in structure_dict[Tags.MOLECULE_COMPOSITION]:
old_volume_fraction = getattr(molecule, "volume_fraction")
if isinstance(old_volume_fraction, torch.Tensor):
struc_start_vox = torch.tensor(structure_dict[Tags.STRUCTURE_START_MM])
struc_end_vox = torch.tensor(structure_dict[Tags.STRUCTURE_END_MM])
structure_size = (struc_end_vox - struc_start_vox) / spacing_mm
if old_volume_fraction.shape[2] == old_volume_height_pixels:
width_shift_pixels = int(width_shift_for_structures_mm / spacing_mm)
z_shift_pixels = int(z_dim_position_shift_mm / spacing_mm)
padding_height = (z_shift_pixels, 0, 0, 0, 0, 0)
padding_width = ((width_shift_pixels, width_shift_pixels), (0, 0), (0, 0))
padded_up = F.pad(old_volume_fraction, padding_height, mode='constant', value=0)
padded_vol = np.pad(padded_up.numpy(), padding_width, mode='edge')
setattr(molecule, "volume_fraction", torch.from_numpy(padded_vol))
structure_dict[Tags.STRUCTURE_START_MM][0] = structure_dict[Tags.STRUCTURE_START_MM][
0] + width_shift_for_structures_mm
structure_dict[Tags.STRUCTURE_START_MM][2] = structure_dict[Tags.STRUCTURE_START_MM][
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def update_molecule_volume_fractions(self, single_structure_settings):
structure_size = structure_end_voxels - structure_start_voxels

if self.volume_dimensions_voxels[2] != old_vol.shape[2]:
if (self.volume_dimensions_voxels[2] == structure_size[2]).any:
if self.volume_dimensions_voxels[2] == structure_size[2]:
pad_start = structure_start_voxels.flip(dims=[0])
pad_end = ((torch.from_numpy(self.volume_dimensions_voxels) - structure_end_voxels)
.flip(dims=[0]))
Expand Down

0 comments on commit 4c4eb45

Please sign in to comment.