Skip to content

Commit

Permalink
Update absorption and scattering test and ran pre-commit hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
kdreher committed Aug 13, 2024
1 parent 6edef3d commit 7a9f54b
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 120 deletions.
2 changes: 1 addition & 1 deletion simpa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
__version__ = version("simpa")
except PackageNotFoundError:
__version__ = "unknown version"

from .core.simulation_modules.volume_creation_module.volume_creation_module_model_based_adapter import \
ModelBasedVolumeCreationAdapter
from .core.simulation_modules.volume_creation_module.volume_creation_module_segmentation_based_adapter import \
Expand Down
2 changes: 1 addition & 1 deletion simpa/core/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def simulate(simulation_pipeline: list, settings: Settings, digital_device_twin:
simpa_output_path = path + settings[Tags.VOLUME_NAME]

settings[Tags.SIMPA_OUTPUT_PATH] = simpa_output_path + ".hdf5"

simpa_output[Tags.SIMPA_VERSION] = __version__
simpa_output[Tags.SETTINGS] = settings
simpa_output[Tags.DIGITAL_DEVICE] = digital_device_twin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def reconstruction_algorithm(self, time_series_sensor_data, detection_geometry):

matlab_binary_path = self.component_settings[Tags.ACOUSTIC_MODEL_BINARY_PATH]
cmd = generate_matlab_cmd(matlab_binary_path, time_reversal_script, acoustic_path, self.get_additional_flags())

cur_dir = os.getcwd()
os.chdir(self.global_settings[Tags.SIMULATION_PATH])
self.logger.info(cmd)
Expand Down
2 changes: 1 addition & 1 deletion simpa/utils/matlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def generate_matlab_cmd(matlab_binary_path: str, simulation_script_path: str, da
:return: list of command parts
:rtype: List[str]
"""

# get path of calling script to add to matlab path
base_script_path = os.path.dirname(os.path.abspath(inspect.stack()[1].filename))
# ensure data path is an absolute path
Expand Down
2 changes: 1 addition & 1 deletion simpa_examples/minimal_optical_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def __init__(self):

device = ExampleDeviceSlitIlluminationLinearDetector()

sp.simulate(pipeline, settings, device)
sp.simulate(pipeline, settings, device, logging_level=Tags.LOGGER_ERROR)

if Tags.WAVELENGTH in settings:
WAVELENGTH = settings[Tags.WAVELENGTH]
Expand Down
4 changes: 2 additions & 2 deletions simpa_examples/optical_and_acoustic_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def create_example_tissue():
Tags.DIM_VOLUME_Y_MM: VOLUME_PLANAR_DIM_IN_MM,
Tags.VOLUME_CREATOR: Tags.VOLUME_CREATOR_VERSATILE,
Tags.GPU: True,
Tags.WAVELENGTHS: [700, 800],
Tags.WAVELENGTHS: [700],
Tags.DO_FILE_COMPRESSION: True,
Tags.DO_IPASC_EXPORT: True
}
Expand All @@ -115,7 +115,7 @@ def create_example_tissue():
})

settings.set_acoustic_settings({
Tags.ACOUSTIC_SIMULATION_3D: False,
# 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",
Expand Down
33 changes: 21 additions & 12 deletions simpa_tests/automatic_tests/test_additional_flags.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
# SPDX-FileCopyrightText: 2021 Division of Intelligent Medical Systems, DKFZ
# SPDX-FileCopyrightText: 2021 Janek Groehl
# SPDX-License-Identifier: MIT

import unittest
import numpy as np

from simpa import MCXAdapterReflectance, MCXAdapter, KWaveAdapter, TimeReversalAdapter, Tags, Settings
from simpa.utils.matlab import generate_matlab_cmd


class TestAdditionalFlags(unittest.TestCase):
def setUp(self) -> None:
self.additional_flags = ('-l', '-a')
self.settings = Settings()

def test_get_cmd_mcx_reflectance_adapter(self):
self.settings.set_optical_settings({
Tags.OPTICAL_MODEL_BINARY_PATH: '.',
Tags.ADDITIONAL_FLAGS: self.additional_flags
})
mcx_reflectance_adapter = MCXAdapterReflectance(global_settings=self.settings)
mcx_reflectance_adapter = MCXAdapterReflectance(global_settings=self.settings)
cmd = mcx_reflectance_adapter.get_command()
for flag in self.additional_flags:
self.assertIn(flag, cmd, f"{flag} was not in command returned by mcx reflectance adapter but was defined as additional flag")

self.assertIn(
flag, cmd, f"{flag} was not in command returned by mcx reflectance adapter but was defined as additional flag")

def test_get_cmd_mcx_adapter(self):
self.settings.set_optical_settings({
Tags.OPTICAL_MODEL_BINARY_PATH: '.',
Expand All @@ -27,27 +33,30 @@ def test_get_cmd_mcx_adapter(self):
mcx_adapter = MCXAdapter(global_settings=self.settings)
cmd = mcx_adapter.get_command()
for flag in self.additional_flags:
self.assertIn(flag, cmd, f"{flag} was not in command returned by mcx adapter but was defined as additional flag")

self.assertIn(
flag, cmd, f"{flag} was not in command returned by mcx adapter but was defined as additional flag")

def test_get_cmd_kwave_adapter(self):
self.settings.set_acoustic_settings({
Tags.ADDITIONAL_FLAGS: self.additional_flags
})
kwave_adapter = KWaveAdapter(global_settings=self.settings)
cmd = generate_matlab_cmd("./matlab.exe", "simulate_2D.m", "my_hdf5.mat", kwave_adapter.get_additional_flags())
for flag in self.additional_flags:
self.assertIn(flag, cmd, f"{flag} was not in command returned by kwave adapter but was defined as additional flag")
self.assertIn(
flag, cmd, f"{flag} was not in command returned by kwave adapter but was defined as additional flag")

def test_get_cmd_time_reversal_adapter(self):
self.settings.set_reconstruction_settings({
Tags.ADDITIONAL_FLAGS: self.additional_flags
})
time_reversal_adapter = TimeReversalAdapter(global_settings=self.settings)
cmd = generate_matlab_cmd("./matlab.exe", "time_reversal_2D.m", "my_hdf5.mat", time_reversal_adapter.get_additional_flags())
cmd = generate_matlab_cmd("./matlab.exe", "time_reversal_2D.m", "my_hdf5.mat",
time_reversal_adapter.get_additional_flags())
for flag in self.additional_flags:
self.assertIn(flag, cmd, f"{flag} was not in command returned by time reversal adapter but was defined as additional flag")

self.assertIn(
flag, cmd, f"{flag} was not in command returned by time reversal adapter but was defined as additional flag")


if __name__ == '__main__':
unittest.main()
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def visualise_result(self, show_figure_on_screen=True, save_path=None):
sp.visualise_device(sp.RSOMExplorerP50(device_position_mm=np.asarray([50, 10, 0])),
figure_save_path[2])


if __name__ == "__main__":
test = DeviceVisualisationTest()
test.run_test(show_figure_on_screen=False)
33 changes: 18 additions & 15 deletions simpa_tests/manual_tests/executables/MATLABAdditionalFlags.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def setup(self):
"""

path_manager = PathManager()

self.settings = Settings({
Tags.WAVELENGTHS: [800],
Tags.WAVELENGTH: 800,
Expand Down Expand Up @@ -79,12 +79,12 @@ def setup(self):
0]))
self.device.add_illumination_geometry(PencilBeamIlluminationGeometry())
self.device.set_detection_geometry(LinearArrayDetectionGeometry(device_position_mm=self.device.device_position_mm,
pitch_mm=0.25,
number_detector_elements=100,
field_of_view_extent_mm=np.asarray([-15, 15, 0, 0, 0, 20])))
pitch_mm=0.25,
number_detector_elements=100,
field_of_view_extent_mm=np.asarray([-15, 15, 0, 0, 0, 20])))

output_name = f'{os.path.join(self.settings[Tags.SIMULATION_PATH], self.settings[Tags.VOLUME_NAME])}'
self.output_file_name = f'{output_name}.log'
self.output_file_name = f'{output_name}.log'

def run_simulation(self):
# run pipeline including volume creation and optical mcx simulation and acoustic matlab kwave simulation
Expand All @@ -100,36 +100,38 @@ def test_execution_of_additional_flag(self):
:raises FileNotFoundError: if log file does not exist at expected location
"""
# perform cleaning before test

# perform cleaning before test
if os.path.exists(self.output_file_name):
os.remove(self.output_file_name)

# run simulation
self.settings.get_acoustic_settings()[Tags.ADDITIONAL_FLAGS] = ['-logfile', self.output_file_name]
self.run_simulation()

# checking if file exists afterwards
if not os.path.exists(self.output_file_name):
raise FileNotFoundError(f"Log file wasn't created at expected path {self.output_file_name}")

def test_if_last_flag_is_used(self):
"""Tests if log file is created with correct last given name by setting multiple additional parameters
:raises FileNotFoundError: if correct log file does not exist at expected location
"""

# perform cleaning before test
if os.path.exists(self.output_file_name):
os.remove(self.output_file_name)

# run simulation
self.settings.get_acoustic_settings()[Tags.ADDITIONAL_FLAGS] = ['-logfile', 'temp_name', '-logfile', self.output_file_name]
self.settings.get_acoustic_settings()[Tags.ADDITIONAL_FLAGS] = [
'-logfile', 'temp_name', '-logfile', self.output_file_name]
self.run_simulation()

# checking if file exists afterwards
if not os.path.exists(self.output_file_name):
raise FileNotFoundError(f"Log file wasn't created with correct last given name at expected path {self.output_file_name}")
raise FileNotFoundError(
f"Log file wasn't created with correct last given name at expected path {self.output_file_name}")

def perform_test(self):
"""
Expand All @@ -139,8 +141,9 @@ def perform_test(self):
self.test_if_last_flag_is_used()

def visualise_result(self, show_figure_on_screen=True, save_path=None):
pass # no figures are created that could be visualized

pass # no figures are created that could be visualized


if __name__ == '__main__':
test = MATLABAdditionalFlags()
test.run_test(show_figure_on_screen=False)
26 changes: 14 additions & 12 deletions simpa_tests/manual_tests/executables/MCXAdditionalFlags.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def setup(self):
"""

path_manager = PathManager()

self.settings = Settings({
Tags.WAVELENGTHS: [800],
Tags.WAVELENGTH: 800,
Expand Down Expand Up @@ -63,7 +63,7 @@ def setup(self):
self.device.add_illumination_geometry(PencilBeamIlluminationGeometry())

self.output_name = f'{os.path.join(self.settings[Tags.SIMULATION_PATH], self.settings[Tags.VOLUME_NAME])}_output'
self.output_file_name = f'{self.output_name}.log'
self.output_file_name = f'{self.output_name}.log'

def run_simulation(self):
# run pipeline including volume creation and optical mcx simulation
Expand All @@ -78,38 +78,39 @@ def test_execution_of_additional_flag(self):
:raises FileNotFoundError: if log file does not exist at expected location
"""
# perform cleaning before test

# perform cleaning before test
if os.path.exists(self. output_file_name):
os.remove(self.output_file_name)

# run simulation
self.settings.get_optical_settings()[Tags.ADDITIONAL_FLAGS] = ['-l', 1, '-s', self.output_name]
self.run_simulation()

# checking if file exists afterwards
if not os.path.exists(self.output_file_name):
raise FileNotFoundError(f"Log file wasn't created at expected path {self.output_file_name}")

def test_if_last_flag_is_used(self):
"""Tests if log file is created with correct last given name by setting multiple additional parameters
:raises FileNotFoundError: if correct log file does not exist at expected location
"""
output_name = f'{os.path.join(self.settings[Tags.SIMULATION_PATH], self.settings[Tags.VOLUME_NAME])}_output'
output_file_name = f'{output_name}.log'
output_file_name = f'{output_name}.log'

# perform cleaning before test
# perform cleaning before test
if os.path.exists(output_file_name):
os.remove(output_file_name)

# run simulation
self.settings.get_optical_settings()[Tags.ADDITIONAL_FLAGS] = ['-l', 1, '-s', 'temp_name', '-s', output_name]
self.run_simulation()

# checking if file exists afterwards
if not os.path.exists(output_file_name):
raise FileNotFoundError(f"Log file wasn't created with correct last given name at expected path {output_file_name}")
raise FileNotFoundError(
f"Log file wasn't created with correct last given name at expected path {output_file_name}")

def perform_test(self):
"""
Expand All @@ -119,8 +120,9 @@ def perform_test(self):
self.test_if_last_flag_is_used()

def visualise_result(self, show_figure_on_screen=True, save_path=None):
pass # no figures are created that could be visualized

pass # no figures are created that could be visualized


if __name__ == '__main__':
test = MCXAdditionalFlags()
test.run_test(show_figure_on_screen=False)
Loading

0 comments on commit 7a9f54b

Please sign in to comment.