pysrx.field¶
pysrx.field
¶
World model: a multiresolution hash-grid neural field fitted through a forward operator.
HashGrid
¶
Bases: Module
Multi-resolution hash encoding over a box of extent_um (z, y, x), physical coordinates. One grid scale for
all axes (cells are cubes; the box is anisotropic), so resolution is isotropic in um.
NeuralField
¶
Bases: Module
Hash encoding + tiny MLP -> emission density s softplus(.).
fit_field(views, psf, shift_px, z_um, pixel_um, gain=None, background=0.0, cfg=None, x_init=None, callback=None)
¶
Fit a NeuralField to the raw views (V, Y, X) through the calibrated projector; see the module docstring.
grid_points(z_um, ny, nx, pixel_um, z0, z1, device, origin_z)
¶
(n, 3) physical positions (z, y, x) um of recon-grid voxels in planes z0..z1 (z from the first plane).
load_field(path, device='cuda')
¶
Trained field + its frame (extent, z_um, pixel) from a fit_field state saved with torch.save.
render_grid(field_, grids, z_um, ny, nx, pixel_um, chunk=16, weights=None)
¶
Field sampled on the recon grid -> (Z, Y, X) (no grad).
render_points(field_, pts, chunk=2 ** 22)
¶
Field at arbitrary positions (N, 3) um, in the field's frame (z from the first recon plane).
spatial_hash(ix, iy, iz, log2_table, kind='mix')
¶
Table row of integer corners. "ngp": Instant-NGP, (ix p1 xor iy p2 xor iz p3) mod T. Its low bits are separable, so two (y, z) rows that collide at one x collide along the whole x line (and whole planes collide), which shows up as streaks when the table is much smaller than the grid. "mix": the same xor, then the splitmix64 finaliser (xor-shift, multiply, twice; logical shifts on int64), so every output bit depends on every input bit and collisions scatter as independent points.
hashgrid
¶
Implicit neural representation ("NeRF-style world model") of the fluorescent volume, fitted through the calibrated light-field forward model.
Field: x(p) = s softplus( MLP( enc(p) ) ), p = physical position (um), x = emission density (>= 0) enc: Instant-NGP multi-resolution hash encoding (Mueller et al. 2022) in plain PyTorch: L levels with geometrically growing resolution N_l = floor(N_min b^l); at each level the 8 grid corners around p look up F learned features in a table of T entries (directly when the level has <= T nodes, else through the spatial hash (Instant-NGP xor of corner x primes, then splitmix-mixed, see spatial_hash), and the features are trilinearly interpolated; the L x F features are concatenated and fed to a tiny MLP. Render: fluorescence is emission-only (no absorption, no occlusion), so a lenslet view is LINEAR in the field: the NeRF "volume rendering" integral along camera rays becomes our calibrated projector y_v = g_v (H x)_v + B_v (forward.Projector: per-view defocus blur + exact parallax shift = the "camera" of view v), with x the field sampled on ZEN's grid. Each lenslet is a calibrated camera whose pose is its sub-aperture position in the pupil (parallax tilt) and whose lens is the defocus blur of its focal group. Loss: Poisson deviance of y vs the 37 raw views + per-view smooth haze B_v (as joint.map_recon) + optional small 3D TV / L1 on x. Adam on hash tables, MLP and haze.
GPU implementation: the field is only ever needed at (a) the regular recon grid (training) and (b) arbitrary points (digital confocal). Each level's hashed table is therefore gathered once per step into a dense per-level node grid (table[hash(nodes)], exactly the corner features Instant-NGP would fetch) and the trilinear interpolation is done with F.grid_sample (fused CUDA kernel). Training uses a detached-volume two-pass step, so the 31.7 M-voxel field never needs a full autograd graph: (1) render x without grad, (2) loss and dL/dx through the projector, (3) re-render chunk by chunk with grad and back-propagate dL/dx into the dense grids and the MLP, (4) push the dense-grid gradients into the hash tables (scatter-add through the gather).
HashGrid
¶
Bases: Module
Multi-resolution hash encoding over a box of extent_um (z, y, x), physical coordinates. One grid scale for
all axes (cells are cubes; the box is anisotropic), so resolution is isotropic in um.
NeuralField
¶
Bases: Module
Hash encoding + tiny MLP -> emission density s softplus(.).
spatial_hash(ix, iy, iz, log2_table, kind='mix')
¶
Table row of integer corners. "ngp": Instant-NGP, (ix p1 xor iy p2 xor iz p3) mod T. Its low bits are separable, so two (y, z) rows that collide at one x collide along the whole x line (and whole planes collide), which shows up as streaks when the table is much smaller than the grid. "mix": the same xor, then the splitmix64 finaliser (xor-shift, multiply, twice; logical shifts on int64), so every output bit depends on every input bit and collisions scatter as independent points.
grid_points(z_um, ny, nx, pixel_um, z0, z1, device, origin_z)
¶
(n, 3) physical positions (z, y, x) um of recon-grid voxels in planes z0..z1 (z from the first plane).
render_grid(field_, grids, z_um, ny, nx, pixel_um, chunk=16, weights=None)
¶
Field sampled on the recon grid -> (Z, Y, X) (no grad).
render_points(field_, pts, chunk=2 ** 22)
¶
Field at arbitrary positions (N, 3) um, in the field's frame (z from the first recon plane).
fit_field(views, psf, shift_px, z_um, pixel_um, gain=None, background=0.0, cfg=None, x_init=None, callback=None)
¶
Fit a NeuralField to the raw views (V, Y, X) through the calibrated projector; see the module docstring.
load_field(path, device='cuda')
¶
Trained field + its frame (extent, z_um, pixel) from a fit_field state saved with torch.save.