| 1 |
""" |
| 2 |
Test that Python exceptions in fitting callbacks are properly caught and reported. |
| 3 |
|
| 4 |
This test creates a simulation builder function that raises an OverflowError. |
| 5 |
The test verifies that: |
| 6 |
1. The exception is caught (no segfault) |
| 7 |
2. A proper error message is produced |
| 8 |
|
| 9 |
Before the fix in j.h branch, this would cause a segfault. |
| 10 |
After the fix, it should raise a RuntimeError with a meaningful message. |
| 11 |
""" |
| 12 |
|
| 13 |
import unittest |
| 14 |
import bornagain as ba |
| 15 |
from bornagain import deg, nm, nm2 |
| 16 |
|
| 17 |
|
| 18 |
def get_sample(): |
| 19 |
"""Simple sample with a cylinder.""" |
| 20 |
vacuum = ba.Vacuum() |
| 21 |
substrate_color = (0.55, 0.55, 0.55) |
| 22 |
substrate_mat = ba.RefractiveMaterial("Substrate", substrate_color, 6e-6, 2e-8) |
| 23 |
particle_color = (0.86, 0.24, 0.18) |
| 24 |
particle_mat = ba.RefractiveMaterial("Particle", particle_color, 6e-4, 2e-8) |
| 25 |
|
| 26 |
ff = ba.Cylinder(5*nm, 5*nm) |
| 27 |
particle = ba.Particle(particle_mat, ff) |
| 28 |
|
| 29 |
vacuum_layer = ba.Layer(vacuum) |
| 30 |
vacuum_layer.deposit2D(ba.Dilute2D(0.001/nm2, particle)) |
| 31 |
substrate_layer = ba.Layer(substrate_mat) |
| 32 |
|
| 33 |
sample = ba.Sample() |
| 34 |
sample.addLayer(vacuum_layer) |
| 35 |
sample.addLayer(substrate_layer) |
| 36 |
return sample |
| 37 |
|
| 38 |
|
| 39 |
def get_simulation_with_overflow(P): |
| 40 |
""" |
| 41 |
Simulation builder that triggers Python OverflowError. |
| 42 |
This mimics what happens when lg_background grows unbounded. |
| 43 |
|
| 44 |
Note: P is a dict with float values when called from FitObjective. |
| 45 |
""" |
| 46 |
|
| 47 |
|
| 48 |
lg_bg = P['lg_background'] |
| 49 |
background_value = 10.0 ** lg_bg |
| 50 |
|
| 51 |
beam = ba.Beam(1e8, 0.1*nm, 0.2*deg) |
| 52 |
det = ba.SphericalDetector(5, -1*deg, 1*deg, 5, 0, 2*deg) |
| 53 |
sample = get_sample() |
| 54 |
|
| 55 |
simulation = ba.ScatteringSimulation(beam, sample, det) |
| 56 |
simulation.setBackground(ba.ConstantBackground(background_value)) |
| 57 |
return simulation |
| 58 |
|
| 59 |
|
| 60 |
class PythonCallbackExceptionTest(unittest.TestCase): |
| 61 |
|
| 62 |
def test_overflow_in_simulation_builder(self): |
| 63 |
""" |
| 64 |
Verify that a Python OverflowError in a simulation builder callback |
| 65 |
is caught and reported as a RuntimeError, not a segfault. |
| 66 |
""" |
| 67 |
|
| 68 |
P_true = {'lg_background': 1.0} |
| 69 |
sim_true = get_simulation_with_overflow(P_true) |
| 70 |
exp_data = sim_true.simulate() |
| 71 |
|
| 72 |
|
| 73 |
fit_objective = ba.FitObjective() |
| 74 |
fit_objective.addFitPair(get_simulation_with_overflow, exp_data) |
| 75 |
|
| 76 |
|
| 77 |
P = {'lg_background': 309.0} |
| 78 |
|
| 79 |
|
| 80 |
with self.assertRaises(RuntimeError) as context: |
| 81 |
fit_objective.evaluate(P) |
| 82 |
|
| 83 |
|
| 84 |
error_msg = str(context.exception) |
| 85 |
self.assertTrue( |
| 86 |
"OverflowError" in error_msg or "Python" in error_msg, |
| 87 |
f"Expected error about Python/Overflow, got: {error_msg}" |
| 88 |
) |
| 89 |
|
| 90 |
|
| 91 |
if __name__ == '__main__': |
| 92 |
unittest.main() |