Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default adapter settings #358

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repos:
types: [python]
args:
- --license-filepath
- license_header.txt # defaults to: LICENSE.txt
- pre_commit_configs/license_header.txt # defaults to: LICENSE.txt

- repo: https://github.com/jorisroovers/gitlint # Uses MIT License (MIT compatible)
rev: v0.19.1
Expand All @@ -36,6 +36,10 @@ repos:
- id: markdown-link-check # checks if links in markdown files work
exclude: docs/
types: [markdown]
args:
- -c
- pre_commit_configs/link-config.json
always_run: true

# toggle comment to perform git config user email check. Note that you have to place the check-email.sh script in your .git/hooks/ folder
# - repo: local
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions pre_commit_configs/link-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ignorePatterns": [
{
"pattern": "https://doi.org/10.1117/1.JBO.27.8.083010"
}
]
}
26 changes: 24 additions & 2 deletions simpa/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from simpa.core.device_digital_twins import DigitalDeviceTwinBase
from simpa.log import Logger
from simpa.utils import Settings
from simpa.utils import Settings, Tags, PathManager
from simpa.utils.processing_device import get_processing_device

class PipelineModule:
Expand All @@ -18,9 +18,31 @@ def __init__(self, global_settings: Settings):
:type global_settings: Settings
"""
self.logger = Logger()
self.global_settings = global_settings
self.global_settings = self.get_default_global_settings()
self.global_settings.update(global_settings) # add and if necessary overrides default global settings by user given settings
self.torch_device = get_processing_device(self.global_settings)

def get_default_global_settings(self) -> Settings:
"""Return the default global settings

:return: default global settings
:rtype: Settings
"""
path_manager = PathManager()

default_global_settings = {
Tags.RANDOM_SEED: 4711,
Tags.VOLUME_NAME: "CompletePipelineExample_4711",
Tags.SIMULATION_PATH: path_manager.get_hdf5_file_save_path(),
Tags.GPU: True,
Tags.DO_FILE_COMPRESSION: True,
Tags.DO_IPASC_EXPORT: True,
Tags.CONTINUE_SIMULATION: False
}

return Settings(default_global_settings)


@abstractmethod
def run(self, digital_device_twin: DigitalDeviceTwinBase):
"""
Expand Down
1 change: 1 addition & 0 deletions simpa/core/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def simulate(simulation_pipeline: list, settings: Settings, digital_device_twin:

for pipeline_element in simulation_pipeline:
logger.debug(f"Running {type(pipeline_element)}")
pipeline_element.global_settings = settings
pipeline_element.run(digital_device_twin)

logger.debug(f"Running pipeline for wavelength {wavelength}nm... [Done]")
Expand Down
21 changes: 18 additions & 3 deletions simpa/core/simulation_modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,28 @@ def __init__(self, global_settings: Settings):
:type global_settings: Settings
"""
super(SimulationModule, self).__init__(global_settings=global_settings)
self.component_settings = self.load_component_settings()

self.component_settings = self.get_default_component_settings()
self.component_settings.update(self.get_user_set_component_settings()) # adds and if necessary overrides default component settings by user given settings
if self.component_settings is None:
raise ValueError("The component settings should not be None at this point")

@abstractmethod
def load_component_settings(self) -> Settings:
def get_user_set_component_settings(self) -> Settings:
"""
:return: Loads component settings corresponding to this simulation component set by the user
"""
:return: Loads component settings corresponding to this simulation component
pass

@abstractmethod
def get_default_component_settings(self) -> Settings:
"""Return the default component settings corresponding to this simulation component

:return: default component settings of this component
:rtype: Settings
"""
pass




Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ class AcousticForwardModelBaseAdapter(SimulationModule):
def __init__(self, global_settings: Settings):
super(AcousticForwardModelBaseAdapter, self).__init__(global_settings=global_settings)

def load_component_settings(self) -> Settings:
"""Implements abstract method to serve acoustic settings as component settings
def get_user_set_component_settings(self) -> Settings:
"""Implements abstract method to serve user set acoustic settings as component settings

:return: Settings: acoustic component settings
"""
return self.global_settings.get_acoustic_settings()

@abstractmethod
def forward_model(self, detection_geometry) -> np.ndarray:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
# SPDX-License-Identifier: MIT

import numpy as np
from simpa.utils import Tags
from simpa.utils import Tags, Settings
from simpa.core.simulation_modules.acoustic_forward_module import AcousticForwardModelBaseAdapter


class AcousticForwardModelTestAdapter(AcousticForwardModelBaseAdapter):

def get_default_component_settings(self) -> Settings:
"""
:return: Loads default acoustic component settings
"""

default_settings = {}
return Settings(default_settings)

def forward_model(self, device) -> np.ndarray:

if Tags.ACOUSTIC_SIMULATION_3D in self.component_settings \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ class KWaveAdapter(AcousticForwardModelBaseAdapter):

"""

def get_default_component_settings(self) -> Settings:
"""
:return: Loads default acoustic component settings
"""

path_manager = PathManager()

default_settings = {
Tags.ACOUSTIC_SIMULATION_3D: False,
Tags.ACOUSTIC_MODEL_BINARY_PATH: path_manager.get_matlab_binary_path(),
Tags.KWAVE_PROPERTY_ALPHA_POWER: 0.00,
Tags.KWAVE_PROPERTY_SENSOR_RECORD: "p",
Tags.KWAVE_PROPERTY_PMLInside: False,
Tags.KWAVE_PROPERTY_PMLSize: [31, 32],
Tags.KWAVE_PROPERTY_PMLAlpha: 1.5,
Tags.KWAVE_PROPERTY_PlotPML: False,
Tags.RECORDMOVIE: False,
Tags.MOVIENAME: "visualization_log",
Tags.ACOUSTIC_LOG_SCALE: True
}

return Settings(default_settings)


def forward_model(self, detection_geometry: DetectionGeometryBase) -> np.ndarray:
"""
Runs the acoustic forward model and performs reading parameters and values from an hdf5 file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ def __init__(self, global_settings: Settings):
self.nz = None
self.temporary_output_files = []

def load_component_settings(self) -> Settings:
"""Implements abstract method to serve optical settings as component settings
def get_user_set_component_settings(self) -> Settings:
"""Implements abstract method to serve user set optical settings as component settings

:return: Settings: optical component settings
"""
return self.global_settings.get_optical_settings()


@abstractmethod
def forward_model(self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import numpy as np
import subprocess
from simpa.utils import Tags, Settings
from simpa.utils import Tags, Settings, PathManager
from simpa.core.simulation_modules.optical_simulation_module import OpticalForwardModuleBase
from simpa.core.device_digital_twins.illumination_geometries import IlluminationGeometryBase
import json
Expand All @@ -13,6 +13,7 @@
from typing import List, Dict, Tuple



class MCXAdapter(OpticalForwardModuleBase):
"""
This class implements a bridge to the mcx framework to integrate mcx into SIMPA. This adapter only allows for
Expand All @@ -38,6 +39,22 @@ def __init__(self, global_settings: Settings):
self.frames = None
self.mcx_output_suffixes = {'mcx_volumetric_data_file': '.jnii'}

def get_default_component_settings(self) -> Settings:
"""
:return: Loads default optical component settings
"""

path_manager = PathManager()

default_settings = {
Tags.MCX_ASSUMED_ANISOTROPY: 0.9,
Tags.OPTICAL_MODEL_BINARY_PATH: path_manager.get_mcx_binary_path(),
Tags.COMPUTE_DIFFUSE_REFLECTANCE: False,
Tags.COMPUTE_PHOTON_DIRECTION_AT_EXIT: False
}

return Settings(default_settings)

def forward_model(self,
absorption_cm: np.ndarray,
scattering_cm: np.ndarray,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import os
from typing import List, Tuple, Dict, Union

from simpa.utils import Tags, Settings
from simpa.utils import Tags, Settings, PathManager
from simpa.core.simulation_modules.optical_simulation_module.optical_forward_model_mcx_adapter import MCXAdapter
from simpa.core.device_digital_twins import IlluminationGeometryBase, PhotoacousticDevice

Expand Down Expand Up @@ -41,6 +41,22 @@ def __init__(self, global_settings: Settings):
self.mcx_output_suffixes = {'mcx_volumetric_data_file': '.jnii',
'mcx_photon_data_file': '_detp.jdat'}

def get_default_component_settings(self) -> Settings:
"""
:return: Loads default optical component settings
"""

path_manager = PathManager()

default_settings = {
Tags.MCX_ASSUMED_ANISOTROPY: 0.9,
Tags.OPTICAL_MODEL_BINARY_PATH: path_manager.get_mcx_binary_path(),
Tags.COMPUTE_DIFFUSE_REFLECTANCE: True,
Tags.COMPUTE_PHOTON_DIRECTION_AT_EXIT: False
}

return Settings(default_settings)

def forward_model(self,
absorption_cm: np.ndarray,
scattering_cm: np.ndarray,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@
# SPDX-License-Identifier: MIT

from simpa.core.simulation_modules.optical_simulation_module import OpticalForwardModuleBase
from simpa import Tags
from simpa import Tags, Settings


class OpticalForwardModelTestAdapter(OpticalForwardModuleBase):
"""
This Adapter was created for testing purposes and only
"""

def get_default_component_settings(self) -> Settings:
"""
:return: Loads default optical component settings
"""

default_settings = {}
return Settings(default_settings)

def forward_model(self, absorption_cm, scattering_cm, anisotropy, illumination_geometry):
results = {Tags.DATA_FIELD_FLUENCE: absorption_cm / ((1 - anisotropy) * scattering_cm)}
return results
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class ReconstructionAdapterBase(SimulationModule):
def __init__(self, global_settings: Settings):
super(ReconstructionAdapterBase, self).__init__(global_settings=global_settings)

def load_component_settings(self) -> Settings:
"""Implements abstract method to serve reconstruction settings as component settings
def get_user_set_component_settings(self) -> Settings:
"""Implements abstract method to serve user set reconstruction settings as component settings

:return: Settings: reconstruction component settings
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-FileCopyrightText: 2021 Janek Groehl
# SPDX-License-Identifier: MIT

from simpa.utils import Tags
from simpa.utils import Tags, Settings
from simpa.core.simulation_modules.reconstruction_module import ReconstructionAdapterBase
import numpy as np
import torch
Expand All @@ -14,6 +14,23 @@

class DelayAndSumAdapter(ReconstructionAdapterBase):

def get_default_component_settings(self) -> Settings:
"""
:return: Loads default reconstruction component settings
"""

default_settings = {
Tags.RECONSTRUCTION_PERFORM_BANDPASS_FILTERING: True,
Tags.TUKEY_WINDOW_ALPHA: 0.5,
Tags.BANDPASS_CUTOFF_LOWPASS_IN_HZ: int(8e6),
Tags.BANDPASS_CUTOFF_HIGHPASS_IN_HZ: int(0.1e6),
Tags.RECONSTRUCTION_BMODE_METHOD: Tags.RECONSTRUCTION_BMODE_METHOD_HILBERT_TRANSFORM,
Tags.RECONSTRUCTION_APODIZATION_METHOD: Tags.RECONSTRUCTION_APODIZATION_BOX,
Tags.RECONSTRUCTION_MODE: Tags.RECONSTRUCTION_MODE_PRESSURE,
}

return Settings(default_settings)

def reconstruction_algorithm(self, time_series_sensor_data, detection_geometry: DetectionGeometryBase):
"""
Applies the Delay and Sum beamforming algorithm [1] to the time series sensor data (2D numpy array where the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-FileCopyrightText: 2021 Janek Groehl
# SPDX-License-Identifier: MIT

from simpa.utils import Tags
from simpa.utils import Tags, Settings
from simpa.core.simulation_modules.reconstruction_module import ReconstructionAdapterBase
from simpa.core.device_digital_twins import DetectionGeometryBase
import numpy as np
Expand All @@ -14,6 +14,23 @@

class DelayMultiplyAndSumAdapter(ReconstructionAdapterBase):

def get_default_component_settings(self) -> Settings:
"""
:return: Loads default reconstruction component settings
"""

default_settings = {
Tags.RECONSTRUCTION_PERFORM_BANDPASS_FILTERING: True,
Tags.TUKEY_WINDOW_ALPHA: 0.5,
Tags.BANDPASS_CUTOFF_LOWPASS_IN_HZ: int(8e6),
Tags.BANDPASS_CUTOFF_HIGHPASS_IN_HZ: int(0.1e6),
Tags.RECONSTRUCTION_BMODE_METHOD: Tags.RECONSTRUCTION_BMODE_METHOD_HILBERT_TRANSFORM,
Tags.RECONSTRUCTION_APODIZATION_METHOD: Tags.RECONSTRUCTION_APODIZATION_BOX,
Tags.RECONSTRUCTION_MODE: Tags.RECONSTRUCTION_MODE_PRESSURE,
}

return Settings(default_settings)

def reconstruction_algorithm(self, time_series_sensor_data, detection_geometry: DetectionGeometryBase):
"""
Applies the Delay Multiply and Sum beamforming algorithm [1] to the time series sensor data (2D numpy array where the
Expand Down
Loading