From 995a7dc285c63502ba9b928f572b6115fdaab042 Mon Sep 17 00:00:00 2001 From: Ryan Roussel Date: Mon, 24 Feb 2025 09:48:07 -0800 Subject: [PATCH 1/3] modify emittance fitting to use MAE instead of MSE --- lcls_tools/common/data/emittance.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lcls_tools/common/data/emittance.py b/lcls_tools/common/data/emittance.py index a6dfd0fb..376c4c3f 100644 --- a/lcls_tools/common/data/emittance.py +++ b/lcls_tools/common/data/emittance.py @@ -112,7 +112,7 @@ def loss_torch(params): params = torch.reshape(params, [*beamsize_squared.shape[:-2], 3]) sig = torch.stack(beam_matrix_tuple(params), dim=-1).unsqueeze(-1) # sig should now be shape batchshape x 3 x 1 (column vectors) - total_squared_error = (amat @ sig - beamsize_squared).pow(2).sum() + total_squared_error = (amat @ sig - beamsize_squared).abs().sum() return total_squared_error def loss_jacobian(params): @@ -130,7 +130,7 @@ def loss(params): params = np.reshape(params, [*beamsize_squared.shape[:-2], 3]) sig = np.expand_dims(np.stack(beam_matrix_tuple(params), axis=-1), axis=-1) # sig should now be shape batchshape x 3 x 1 (column vectors) - total_squared_error = np.sum((amat @ sig - beamsize_squared) ** 2) + total_squared_error = np.sum(np.abs(amat @ sig - beamsize_squared)) return total_squared_error loss_jacobian = None From 8bf9df3d92c59b857d825e4e634af3bddd39744d Mon Sep 17 00:00:00 2001 From: Ryan Roussel Date: Mon, 24 Feb 2025 09:48:16 -0800 Subject: [PATCH 2/3] fix failing test --- .../common/measurements/test_screen_beamsize_measurement.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit_tests/lcls_tools/common/measurements/test_screen_beamsize_measurement.py b/tests/unit_tests/lcls_tools/common/measurements/test_screen_beamsize_measurement.py index 00cbf2e8..2de3514f 100644 --- a/tests/unit_tests/lcls_tools/common/measurements/test_screen_beamsize_measurement.py +++ b/tests/unit_tests/lcls_tools/common/measurements/test_screen_beamsize_measurement.py @@ -30,7 +30,7 @@ def test_measure(self): assert result.rms_sizes.shape == (1, 2) assert result.total_intensities.shape == (1,) assert np.allclose(result.rms_sizes, np.array([8.0347, 8.0347])) - assert np.allclose(result.centroids, np.array([50, 50])) + assert np.allclose(result.centroids.flatten(), np.array([49.5, 49.5])) assert np.allclose(result.total_intensities, np.array([102000.0])) assert result.metadata == self.measurement.model_dump() From 97107835405b6936b0848ed1c91b3f58e75877f8 Mon Sep 17 00:00:00 2001 From: Ryan Roussel Date: Mon, 24 Feb 2025 13:04:09 -0800 Subject: [PATCH 3/3] fix bugs in tests --- tests/unit_tests/lcls_tools/common/image/test_image_fit.py | 2 +- .../common/measurements/test_emittance_measurement.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit_tests/lcls_tools/common/image/test_image_fit.py b/tests/unit_tests/lcls_tools/common/image/test_image_fit.py index e4f760e6..45c44e95 100644 --- a/tests/unit_tests/lcls_tools/common/image/test_image_fit.py +++ b/tests/unit_tests/lcls_tools/common/image/test_image_fit.py @@ -10,7 +10,7 @@ def test_fit_and_visualization(self): result = ImageProjectionFit().fit_image(test_image) - assert np.allclose(result.centroid, [5, 5]) + assert np.allclose(result.centroid, [4.5, 4.5]) assert np.allclose(result.rms_size, [1.16, 1.16]) assert np.allclose(result.total_intensity, 1020.0) assert np.allclose(result.image, test_image) diff --git a/tests/unit_tests/lcls_tools/common/measurements/test_emittance_measurement.py b/tests/unit_tests/lcls_tools/common/measurements/test_emittance_measurement.py index a854a52a..28a48b51 100644 --- a/tests/unit_tests/lcls_tools/common/measurements/test_emittance_measurement.py +++ b/tests/unit_tests/lcls_tools/common/measurements/test_emittance_measurement.py @@ -175,10 +175,10 @@ def mock_function(scan_settings, function): # check outputs based on nans in the data assert np.equal( - result.quadrupole_pv_values[0], np.concat((k[:6], k[7:])) + result.quadrupole_pv_values[0], np.concatenate((k[:6], k[7:])) ).all() assert np.equal( - result.quadrupole_pv_values[1], np.concat((k[:1], k[3:])) + result.quadrupole_pv_values[1], np.concatenate((k[:1], k[3:])) ).all() assert np.allclose(