GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: draw random trajectory noise from a private generator · cslht11/diffct@a805bb5

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • diffct
    • geometry.py
  • tests
    • test_geometry_input_validation.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎diffct/geometry.py‎

Copy file name to clipboardExpand all lines: diffct/geometry.py
+22-5Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,25 @@ def random_trajectory_3d(n_views, sid_mean, sdd_mean, sid_std=0.0, pos_std=0.0,
274274
"""
275275
import math
276276

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
277285
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)
279296

280297
# Start with circular trajectory
281298
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,
285302
# Add random perturbations
286303
if sid_std > 0.0:
287304
# Random SID variations
288-
sid_perturbations = torch.randn(n_views, device=device, dtype=dtype) * sid_std
305+
sid_perturbations = noise((n_views,)) * sid_std
289306
sdd_perturbations = sid_perturbations.clone() # Keep SDD-SID distance approximately constant
290307

291308
# 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,
306323

307324
if pos_std > 0.0:
308325
# 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
311328

312329
if angle_std > 0.0:
313330
# 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
315332
cos_perturb = torch.cos(angle_perturbations)
316333
sin_perturb = torch.sin(angle_perturbations)
317334

Collapse file

‎tests/test_geometry_input_validation.py‎

Copy file name to clipboardExpand all lines: tests/test_geometry_input_validation.py
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,35 @@ def test_circular_trajectories_accept_explicit_angle_tensors(
177177
for component in trajectory:
178178
assert component.shape[0] == len(angle_values)
179179
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)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.