GeezerVison is showing github.com — fix: draw random trajectory noise from a private generator · cslht11/diffct@a805bb5
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: diffct/geometry.py +22-5Lines changed: 22 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -274,8 +274,25 @@ def random_trajectory_3d(n_views, sid_mean, sdd_mean, sid_std=0.0, pos_std=0.0, | 274 | 274 | """ | 275 | 275 | import math | 276 | 276 |
| | 277 | + # Seeding the process-wide generator here would also reset every other | | 278 | + # random draw the caller relies on, so draw from a private stream instead. | | 279 | + # int() keeps the seeds torch.manual_seed accepted: a fresh Generator | | 280 | + # rejects the bool, float and 0-dim tensor forms the global one allowed. | | 281 | + # A generator on the target device replays the same stream the process-wide | | 282 | + # one did, so a seeded trajectory stays bit-identical to before this change. | | 283 | + stream_device = torch.device(device) | | 284 | + generator = None | 277 | 285 | if seed is not None: | 278 | | - torch.manual_seed(seed) | | 286 | + try: | | 287 | + generator = torch.Generator(device=device).manual_seed(int(seed)) | | 288 | + except (RuntimeError, TypeError, ValueError): | | 289 | + # Builds without a per-device generator: a host generator still | | 290 | + # keeps the draw private to this call. | | 291 | + generator = torch.Generator().manual_seed(int(seed)) | | 292 | + stream_device = torch.device("cpu") | | 293 | + | | 294 | + def noise(shape): | | 295 | + return torch.randn(shape, generator=generator, dtype=dtype, device=stream_device).to(device) | 279 | 296 |
| 280 | 297 | # Start with circular trajectory | 281 | 298 | src_pos, det_center, det_u_vec, det_v_vec = circular_trajectory_3d( | @@ -285,7 +302,7 @@ def random_trajectory_3d(n_views, sid_mean, sdd_mean, sid_std=0.0, pos_std=0.0, | 285 | 302 | # Add random perturbations | 286 | 303 | if sid_std > 0.0: | 287 | 304 | # Random SID variations | 288 | | - sid_perturbations = torch.randn(n_views, device=device, dtype=dtype) * sid_std | | 305 | + sid_perturbations = noise((n_views,)) * sid_std | 289 | 306 | sdd_perturbations = sid_perturbations.clone() # Keep SDD-SID distance approximately constant | 290 | 307 |
| 291 | 308 | # Compute angles from existing positions | @@ -306,12 +323,12 @@ def random_trajectory_3d(n_views, sid_mean, sdd_mean, sid_std=0.0, pos_std=0.0, | 306 | 323 |
| 307 | 324 | if pos_std > 0.0: | 308 | 325 | # Random 3D position offsets | 309 | | - src_pos += torch.randn_like(src_pos) * pos_std | 310 | | - det_center += torch.randn_like(det_center) * pos_std | | 326 | + src_pos += noise(tuple(src_pos.shape)) * pos_std | | 327 | + det_center += noise(tuple(det_center.shape)) * pos_std | 311 | 328 |
| 312 | 329 | if angle_std > 0.0: | 313 | 330 | # Random angular perturbations (rotate detector orientation) | 314 | | - angle_perturbations = torch.randn(n_views, device=device, dtype=dtype) * angle_std | | 331 | + angle_perturbations = noise((n_views,)) * angle_std | 315 | 332 | cos_perturb = torch.cos(angle_perturbations) | 316 | 333 | sin_perturb = torch.sin(angle_perturbations) | 317 | 334 |
| |
Collapse file Copy file name to clipboardExpand all lines: tests/test_geometry_input_validation.py +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -177,3 +177,35 @@ def test_circular_trajectories_accept_explicit_angle_tensors( | 177 | 177 | for component in trajectory: | 178 | 178 | assert component.shape[0] == len(angle_values) | 179 | 179 | assert component.device.type == "cpu" | | 180 | + | | 181 | + | | 182 | +def test_random_trajectory_seed_leaves_the_global_generator_untouched(): | | 183 | + torch.manual_seed(1234) | | 184 | + expected = torch.randn(4) | | 185 | + torch.manual_seed(1234) | | 186 | + random_trajectory_3d(32, sid_mean=1000.0, sdd_mean=1500.0, sid_std=10.0, | | 187 | + pos_std=1.0, angle_std=1e-3, device="cpu", seed=7) | | 188 | + torch.testing.assert_close(torch.randn(4), expected) | | 189 | + | | 190 | + | | 191 | +def test_random_trajectory_seed_is_reproducible(): | | 192 | + kwargs = dict(sid_mean=1000.0, sdd_mean=1500.0, sid_std=10.0, pos_std=1.0, | | 193 | + angle_std=1e-3, device="cpu", seed=7) | | 194 | + first = random_trajectory_3d(32, **kwargs) | | 195 | + second = random_trajectory_3d(32, **kwargs) | | 196 | + for a, b in zip(first, second): | | 197 | + torch.testing.assert_close(a, b) | | 198 | + | | 199 | + | | 200 | +def test_random_trajectory_seed_still_replays_the_previous_stream(): | | 201 | + """A private generator must not move any trajectory that a seed pinned down. | | 202 | +
| | 203 | + Seeding used to reach the numbers through the process-wide generator, so a | | 204 | + caller could replay them. A generator seeded on the same device produces | | 205 | + that very stream, which is why switching to a private one is value-free. | | 206 | + """ | | 207 | + torch.manual_seed(2026) | | 208 | + expected = [torch.randn(16), torch.randn(16, 3)] | | 209 | + generator = torch.Generator(device="cpu").manual_seed(2026) | | 210 | + for shape, want in zip([(16,), (16, 3)], expected): | | 211 | + torch.testing.assert_close(torch.randn(shape, generator=generator), want) |
|