From 5822aa00fb0fdc8b3594df08562b545c15482def Mon Sep 17 00:00:00 2001 From: Walter Simson Date: Mon, 3 Jun 2024 21:42:30 -0700 Subject: [PATCH] use recorder for fourierShift test --- .../matlab_collectors/collect_fourierShift.m | 24 +++++++++---------- .../python_testers/fourierShift_test.py | 24 +++++++++---------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/tests/matlab_test_data_collectors/matlab_collectors/collect_fourierShift.m b/tests/matlab_test_data_collectors/matlab_collectors/collect_fourierShift.m index 4252fde7..e103036a 100644 --- a/tests/matlab_test_data_collectors/matlab_collectors/collect_fourierShift.m +++ b/tests/matlab_test_data_collectors/matlab_collectors/collect_fourierShift.m @@ -8,9 +8,9 @@ [5, 10, 5, 4] ... # 4D }; shifts = [0, 0.12, 0.29, 0.5, 0.52, 0.85, 1.0]; -output_folder = 'collectedValues/fourierShift'; +output_file = 'collectedValues/fourierShift.mat'; idx = 0; - +recorder = utils.TestRecorder(output_file); for data_dim_idx=1:size(dims, 2) data_dim = data_dims{data_dim_idx}; data = rand(data_dim); @@ -19,22 +19,22 @@ shifted_data = fourierShift(data, shift); % Save output - idx_padded = sprintf('%06d', idx); - filename = [output_folder '/' idx_padded '.mat']; - save(filename, 'data', 'shift', 'shifted_data'); - idx = idx + 1; + recorder.recordVariable('data', data); + recorder.recordVariable('shift', shift); + recorder.recordVariable('shifted_data', shifted_data); + recorder.increment(); for shift_dim=1:size(data_dim, 2) shifted_data = fourierShift(data, shift, shift_dim); - - % Save output - idx_padded = sprintf('%06d', idx); - filename = [output_folder '/' idx_padded '.mat']; - save(filename, 'data', 'shift', 'shift_dim', 'shifted_data'); - idx = idx + 1; + recorder.recordVariable('data', data); + recorder.recordVariable('shift', shift); + recorder.recordVariable('shift_dim', shift_dim); + recorder.recordVariable('shifted_data', shifted_data); + recorder.increment(); end end end +recorder.saveRecordsToDisk(); diff --git a/tests/matlab_test_data_collectors/python_testers/fourierShift_test.py b/tests/matlab_test_data_collectors/python_testers/fourierShift_test.py index 22605087..388a11b4 100644 --- a/tests/matlab_test_data_collectors/python_testers/fourierShift_test.py +++ b/tests/matlab_test_data_collectors/python_testers/fourierShift_test.py @@ -3,27 +3,25 @@ from pathlib import Path import numpy as np -from scipy.io import loadmat from kwave.utils.math import fourier_shift +from tests.matlab_test_data_collectors.python_testers.utils.record_reader import TestRecordReader def test_fourier_shift(): - collected_values_folder = os.path.join(Path(__file__).parent, "collectedValues/fourierShift") - num_collected_values = len(os.listdir(collected_values_folder)) + collected_values_folder = os.path.join(Path(__file__).parent, "collectedValues/fourierShift.mat") + reader = TestRecordReader(collected_values_folder) - for i in range(num_collected_values): + for i in range(len(reader)): # Read recorded data - filepath = os.path.join(collected_values_folder, f"{i:06d}.mat") - recorded_data = loadmat(filepath) - - data = recorded_data["data"] - shift = float(recorded_data["shift"]) - if "shift_dim" in recorded_data: - shift_dim = int(recorded_data["shift_dim"]) - else: + + data = reader.expected_value_of("data") + shift = reader.expected_value_of("shift") + try: + shift_dim = reader.expected_value_of("shift_dim") + except KeyError: shift_dim = None - expected_shifted_data = recorded_data["shifted_data"] + expected_shifted_data = reader.expected_value_of("shifted_data") # Execute implementation shifted_data = fourier_shift(data, shift, shift_dim)