HardExampleCrop#

class HardExampleCrop(point_max=80000, sample_rate=None, hard_labels=(2, 3), min_hard_points=1, attempts=5, fallback='none', p=0.5)[source]#

Bases: object

Spherical crop biased toward rare (“hard”) segment labels.

Like SphereCrop, but when the cloud is over budget it centers the crop on a point whose segment is one of hard_labels, retrying up to attempts times until the kept set contains at least min_hard_points hard points (otherwise the last attempt is used). Only fires with probability p; sub-budget clouds are returned unchanged. If no hard points are present, behaviour follows fallback. Keeps the closest point_max (or sample_rate-fraction) points via index_operator. Requires coord (asserted) and reads segment when present. Registered as HardExampleCrop – use this string as the type in a transform=[...] config list.

Parameters:
  • point_max (int) – maximum number of points to keep when sample_rate is None. Defaults to 80000.

  • sample_rate (float, optional) – if set, the budget is sample_rate * num_points instead of point_max. Defaults to None.

  • hard_labels (Sequence[int]) – segment label values treated as hard examples to center on. Defaults to (2, 3).

  • min_hard_points (int) – minimum hard points the crop must contain to accept an attempt early. Defaults to 1.

  • attempts (int) – number of random hard-centered crops to try. Defaults to 5.

  • fallback (str) – behaviour when no hard points exist, one of "random", "center", or "none" (return unchanged). Defaults to "none".

  • p (float) – probability of applying the crop at all. Defaults to 0.5.

Note

With no segment key all points are treated as non-hard, so the fallback path is taken.

Example

>>> import numpy as np
>>> from pimm.datasets.transform import HardExampleCrop
>>> np.random.seed(0)
>>> coord = np.random.rand(100, 3).astype("f4")
>>> segment = np.zeros(100, dtype=int); segment[[5, 50]] = 3  # two rare points
>>> data = {"coord": coord.copy(), "segment": segment.copy()}
>>> # over budget: crop centers on a hard (label 3) point, keeping it in-frame
>>> out = HardExampleCrop(point_max=10, hard_labels=(2, 3),
...                       min_hard_points=1, p=1.0)(data)
>>> len(out["coord"])  # 100 -> 10 points (closest to a hard center)
10
>>> int(np.isin(out["segment"], (2, 3)).sum()) >= 1  # at least one hard point survives
True