-
Notifications
You must be signed in to change notification settings - Fork 46
Fix: Stop mutation of source and sensor by kspaceFirstOrder #614
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
Open
waltsims
wants to merge
5
commits into
master
Choose a base branch
from
fix-test-syntax-bug-600
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5844867
Fix: Correct SyntaxError in test_kspaceFirstOrder3D_state.py
google-labs-jules[bot] ecfc3ee
Fix tests
waltsims 59a3eb4
Fix walrus bug
waltsims e3e76d4
Merge branch 'master' into fix-test-syntax-bug-600
waltsims 791a65c
Merge branch 'master' into fix-test-syntax-bug-600
waltsims File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| from copy import deepcopy | ||
| from pathlib import Path | ||
| from tempfile import TemporaryDirectory | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from kwave.data import Vector | ||
| from kwave.kgrid import kWaveGrid | ||
| from kwave.kmedium import kWaveMedium | ||
| from kwave.ksensor import kSensor | ||
| from kwave.ksource import kSource | ||
| from kwave.kspaceFirstOrder3D import kspaceFirstOrder3D | ||
| from kwave.options.simulation_execution_options import SimulationExecutionOptions | ||
| from kwave.options.simulation_options import SimulationOptions | ||
| from kwave.utils.filters import smooth | ||
| from kwave.utils.mapgen import make_ball | ||
|
|
||
|
|
||
| def make_simulation_parameters(directory: Path): | ||
| # create the computational grid | ||
| PML_size = 10 # size of the PML in grid points | ||
| N = Vector([32, 64, 64]) - 2 * PML_size # number of grid points | ||
| d = Vector([0.2e-3, 0.2e-3, 0.2e-3]) # grid point spacing [m] | ||
| kgrid = kWaveGrid(N, d) | ||
|
|
||
| # define the properties of the propagation medium | ||
| medium = kWaveMedium(sound_speed=1500) # [m/s] | ||
|
|
||
| # create initial pressure distribution using makeBall | ||
| ball_magnitude = 10 # [Pa] | ||
| ball_radius = 3 # [grid points] | ||
| p0_array = ball_magnitude * make_ball(N, N / 2, ball_radius) | ||
| p0_array = smooth(p0_array, restore_max=True) | ||
|
|
||
| source = kSource() | ||
| source.p0 = p0_array | ||
|
|
||
| # define a binary planar sensor | ||
| sensor = kSensor() | ||
| sensor_mask_array = np.zeros(N) | ||
| sensor_mask_array[0, :, :] = 1 # Corrected to be a plane for 3D | ||
| sensor.mask = sensor_mask_array | ||
|
|
||
| input_filename = directory / "kwave_input.h5" | ||
| output_filename = directory / "kwave_output.h5" | ||
| checkpoint_filename = directory / "kwave_checkpoint.h5" | ||
|
|
||
| simulation_options = SimulationOptions( | ||
| save_to_disk=True, # Must be true for kspaceFirstOrder3D | ||
| pml_size=PML_size, | ||
| pml_inside=False, | ||
| smooth_p0=False, # p0 is already smoothed | ||
| data_cast="single", | ||
| input_filename=input_filename, | ||
| output_filename=output_filename, | ||
| ) | ||
waltsims marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| checkpoint_timesteps = 300 | ||
|
|
||
| execution_options = SimulationExecutionOptions( | ||
| is_gpu_simulation=False, # Assuming CPU for basic test | ||
| checkpoint_file=checkpoint_filename, | ||
| checkpoint_timesteps=checkpoint_timesteps, | ||
| verbose_level=0, # Keep test output clean | ||
| ) | ||
| return kgrid, medium, source, sensor, simulation_options, execution_options | ||
|
|
||
|
|
||
| def test_kspaceFirstOrder3D_input_state_preservation(): | ||
| with TemporaryDirectory() as tmpdir_str: | ||
| tmpdir = Path(tmpdir_str) | ||
| kgrid, medium, source, sensor, simulation_options, execution_options = make_simulation_parameters(tmpdir) | ||
|
|
||
| # Store original states of critical attributes for comparison | ||
| original_source_p0 = deepcopy(source.p0) | ||
| original_sensor_mask = deepcopy(sensor.mask) | ||
|
|
||
|
|
||
| # If source.p or source.u were time-varying, store their initial states too. | ||
| # For this test, p0 is the main source attribute. | ||
|
|
||
| # First run | ||
| try: | ||
| _ = kspaceFirstOrder3D( | ||
| kgrid=kgrid, | ||
| medium=medium, | ||
| source=source, | ||
| sensor=sensor, | ||
| simulation_options=simulation_options, | ||
| execution_options=execution_options, | ||
| ) | ||
| except Exception as e: | ||
| pytest.fail(f"First call to kspaceFirstOrder3D failed: {e}") | ||
|
|
||
| # Check if original source and sensor attributes are unchanged | ||
| assert np.array_equal(source.p0, original_source_p0), "source.p0 was modified after first run" | ||
| assert np.array_equal(sensor.mask, original_sensor_mask), "sensor.mask was modified after first run" | ||
|
|
||
| # Second run (should not fail if state is preserved) | ||
| # For the second run, we need new input/output filenames or to ensure the C++ code can overwrite. | ||
| # Easiest is to use new filenames for the test. | ||
| simulation_options_run2 = deepcopy(simulation_options) | ||
| simulation_options_run2.input_filename = tmpdir / "kwave_input_run2.h5" | ||
| simulation_options_run2.output_filename = tmpdir / "kwave_output_run2.h5" | ||
|
|
||
| execution_options_run2 = deepcopy(execution_options) | ||
| if execution_options_run2.checkpoint_file: # Only change if it exists | ||
| execution_options_run2.checkpoint_file = tmpdir / "kwave_checkpoint_run2.h5" | ||
|
|
||
| try: | ||
| _ = kspaceFirstOrder3D( | ||
| kgrid=kgrid, | ||
| medium=medium, | ||
| source=source, | ||
| sensor=sensor, | ||
| simulation_options=simulation_options_run2, | ||
| execution_options=execution_options_run2, | ||
| ) | ||
| except Exception as e: | ||
| pytest.fail(f"Second call to kspaceFirstOrder3D with original objects failed: {e}") | ||
|
|
||
| # Final check that attributes are still the same as the initial state | ||
| assert np.array_equal(source.p0, original_source_p0), "source.p0 was modified after second run" | ||
| assert np.array_equal(sensor.mask, original_sensor_mask), "sensor.mask was modified after second run" | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.