GridSample#
- class GridSample(grid_size=0.05, hash_type='fnv', mode='train', return_inverse=False, return_grid_coord=False, return_min_coord=False, return_displacement=False, project_displacement=False, sum_keys=None, min_keys=None)[source]#
Bases:
objectVoxel-downsample a point cloud onto a hash grid.
Floors
coord / grid_sizeto integer voxel indices, hashes them, and deduplicates points per voxel. Inmode="train"it keeps one random point per occupied voxel and subsamples every per-point key inindex_valid_keysviaindex_operator, returning a single dict. Inmode="test"it instead returns a list of fragment dicts that together cover all points (one point per voxel per fragment), each carrying anindexkey. Optionally aggregates selected keys per voxel (sum or min) and emits integergrid_coordfor sparse convolutions. Requirescoord(asserted). Registered asGridSample– use this string as thetypein atransform=[...]config list.- Parameters:
grid_size (float | Sequence[float]) – voxel edge length in (normalized) coordinate units; smaller keeps more points. Defaults to
0.05.hash_type (str) – voxel hash,
"fnv"(FNV64-1A) or otherwise the ravel/raster hash. Defaults to"fnv".mode (str) –
"train"(one random point per voxel, single dict) or"test"(list of covering fragments). Defaults to"train".return_inverse (bool) – if
True, store aninversearray mapping each original point to its voxel. Defaults toFalse.return_grid_coord (bool) – if
True, store integergrid_coordand append it toindex_valid_keys. Defaults toFalse.return_min_coord (bool) – if
True, store the per-samplemin_coord(grid origin, shape[1, 3]). Defaults toFalse.return_displacement (bool) – if
True, store each point’sdisplacementfrom its voxel center in[-0.5, 0.5]. Defaults toFalse.project_displacement (bool) – if
True, project the displacement onto the pointnormalto a scalar (requiresnormal). Defaults toFalse.sum_keys (Sequence[str], optional) – keys summed over the points in each voxel (e.g. pooling energy).
Nonemeans none. Defaults toNone.min_keys (Sequence[str], optional) – keys reduced by per-voxel minimum.
Nonemeans none. Defaults toNone.
Note
Only keys in
index_valid_keysare downsampled together; any point-aligned array missing from that list keeps its original length and silently desynchronizes fromcoord.grid_sizeis in the same frame ascoord– afterNormalizeCoordit is in normalized units.mode="test"returns a list of dicts, not a single dict, so it is normally the terminal geometric step of a test pipeline.Example
>>> import numpy as np >>> from pimm.datasets.transform import GridSample >>> np.random.seed(0) >>> # 1000 points landing on a 5x5x5 integer lattice -> many coincide per voxel >>> coord = np.floor(np.random.rand(1000, 3) * 5).astype("f4") >>> data = {"coord": coord.copy(), "energy": np.random.rand(1000, 1).astype("f4")} >>> out = GridSample(grid_size=1.0, mode="train", return_grid_coord=True)(data) >>> len(out["coord"]) # one point kept per occupied voxel: 1000 -> 5**3 voxels 125 >>> out["grid_coord"].shape, out["grid_coord"].dtype # integer voxel indices added ((125, 3), dtype('int64'))