txus commited on
Commit
1d1ea0b
·
verified ·
1 Parent(s): 35f2be5

Upload folder using huggingface_hub

Browse files
ballast/__pycache__/experiment.cpython-312.pyc CHANGED
Binary files a/ballast/__pycache__/experiment.cpython-312.pyc and b/ballast/__pycache__/experiment.cpython-312.pyc differ
 
ballast/__pycache__/gp.cpython-312.pyc CHANGED
Binary files a/ballast/__pycache__/gp.cpython-312.pyc and b/ballast/__pycache__/gp.cpython-312.pyc differ
 
ballast/__pycache__/policies.cpython-312.pyc CHANGED
Binary files a/ballast/__pycache__/policies.cpython-312.pyc and b/ballast/__pycache__/policies.cpython-312.pyc differ
 
ballast/experiment.py CHANGED
@@ -64,6 +64,38 @@ class Config:
64
  jnp.linspace(self.y_lo, self.y_hi, self.grid_ny),
65
  )
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  SYNTH_PARAMS = HelmParams(
69
  phi_ls=0.8, phi_var=0.5, psi_ls=0.5, psi_var=0.5, time_ls=2.5, time_var=1.0
@@ -160,11 +192,15 @@ def run_campaign(
160
  k_m = int(round(t_m / cfg.dt))
161
  key, kp, ks, ko = jax.random.split(key, 4)
162
 
163
- # ---- data available at decision time (strictly before/at t_m)
 
164
  past = val_all & (np.asarray(tg)[None, :] <= t_m + 1e-9)
165
- S_obs = jnp.asarray(pos_all[past])
166
- t_obs = jnp.asarray(np.broadcast_to(np.asarray(tg)[None, :], past.shape)[past])
167
- y_obs = jnp.asarray(y_all[past])
 
 
 
168
 
169
  # ---- choose the placement
170
  if m == 0:
@@ -174,41 +210,42 @@ def run_campaign(
174
  elif policy == "sobol":
175
  idx = int(sobol_indices(grid, cfg.n_deploy, seed_offset)[m])
176
  else:
177
- exist_idx = np.arange(m)
178
  j_at = int(round(t_m / cfg.obs_dt)) - 1 # obs index whose time is t_m
179
- # project from the drifters' true positions, not their reported cells
180
- exist_pos = jnp.asarray(raw_all[exist_idx, j_at])
181
- exist_live = jnp.asarray(val_all[exist_idx, j_at])
182
- # drifters that already left contribute nothing: park them outside
183
- exist_pos = jnp.where(
184
- exist_live[:, None], exist_pos, jnp.array([1e6, 1e6])
185
- )
186
 
187
  p_pol, sig_pol = true_params, cfg.sigma_obs
188
  if policy == "ballast_opt":
189
  p_pol, sig_pol = optimise_hypers(
190
- S_obs, t_obs, y_obs, cfg, bounds, true_params
191
  )
192
 
193
  if policy == "eig":
194
- sc = eig_utilities(grid, S_obs, t_obs, t_m, p_pol, sig_pol, cfg.chunk)
 
 
195
  else:
196
  ops_pol = ops_true if policy != "ballast_opt" else make_ops(
197
  grid.R, p_pol, cfg.dt
198
  )
199
  mean, chol = posterior_ext_state(
200
- S_obs, t_obs, y_obs, grid.R, t_m, p_pol, sig_pol
201
  )
202
  if policy == "dist_sep":
203
  sc = dist_sep_scores(
204
  ks, grid, ops_pol, mean, chol, exist_pos, S_obs, t_m,
205
- cfg.T, cfg.dt, cfg.obs_every, cfg.n_samples,
206
  )
207
  else: # ballast_true / ballast_opt
208
  u = ballast_sample_utilities(
209
  ks, grid, ops_pol, S_obs, t_obs, mean, chol, exist_pos,
210
  t_m, cfg.T, cfg.dt, cfg.obs_every, p_pol, sig_pol,
211
- cfg.n_samples, cfg.chunk,
212
  )
213
  sc = jnp.mean(u, axis=0)
214
  idx = int(jnp.argmax(sc))
@@ -232,11 +269,14 @@ def run_campaign(
232
 
233
  # ---- evaluate: all data from the m+1 drifters over the whole campaign
234
  sel = val_all[: m + 1]
235
- S_e = jnp.asarray(pos_all[: m + 1][sel])
236
- t_e = jnp.asarray(np.broadcast_to(np.asarray(tg)[None, :], sel.shape)[sel])
237
- y_e = jnp.asarray(y_all[: m + 1][sel])
 
 
 
238
  mu = posterior_mean_field(
239
- S_e, t_e, y_e, grid.R, t_eval, eval_params, cfg.sigma_obs
240
  )
241
  gt_at_eval = gt_fields[(jnp.round(t_eval / cfg.dt)).astype(int)]
242
  err = float(jnp.mean(jnp.linalg.norm(mu - gt_at_eval, axis=-1)))
 
64
  jnp.linspace(self.y_lo, self.y_hi, self.grid_ny),
65
  )
66
 
67
+ def n_past_max(self, m: int) -> int:
68
+ """Upper bound on observations available at decision time t_m.
69
+
70
+ Drifter i (released at i*deploy_every) has measured
71
+ (t_m - t_i)/obs_dt times by t_m if it never left the region. Summing
72
+ gives a bound that depends only on m -- so padding to it keeps every
73
+ array shape a function of the deployment index alone, and XLA compiles
74
+ each shape once for the whole job instead of once per campaign.
75
+ """
76
+ k = int(round(self.deploy_every / self.obs_dt))
77
+ return k * m * (m + 1) // 2
78
+
79
+ def n_all_max(self, m: int) -> int:
80
+ """Upper bound on observations from drifters 0..m over the whole campaign."""
81
+ k = int(round(self.deploy_every / self.obs_dt))
82
+ return sum(self.n_obs_total - i * k for i in range(m + 1))
83
+
84
+
85
+ def _pad(S, t, y, n):
86
+ """Pad observation arrays to exactly n points; return (S, t, y, mask).
87
+
88
+ Padding points are parked far outside the region so their kernel entries are
89
+ numerically zero anyway; the mask is what actually neutralises them.
90
+ """
91
+ k = S.shape[0]
92
+ assert k <= n, f"more observations ({k}) than the bound ({n})"
93
+ Sp = np.concatenate([S, np.full((n - k, 2), 1e6)])
94
+ tp = np.concatenate([t, np.zeros(n - k)])
95
+ yp = np.concatenate([y, np.zeros((n - k, 2))])
96
+ mask = np.concatenate([np.ones(k, bool), np.zeros(n - k, bool)])
97
+ return (jnp.asarray(Sp), jnp.asarray(tp), jnp.asarray(yp), jnp.asarray(mask))
98
+
99
 
100
  SYNTH_PARAMS = HelmParams(
101
  phi_ls=0.8, phi_var=0.5, psi_ls=0.5, psi_var=0.5, time_ls=2.5, time_var=1.0
 
192
  k_m = int(round(t_m / cfg.dt))
193
  key, kp, ks, ko = jax.random.split(key, 4)
194
 
195
+ # ---- data available at decision time (strictly before/at t_m),
196
+ # padded to a bound that depends only on m (see Config.n_past_max)
197
  past = val_all & (np.asarray(tg)[None, :] <= t_m + 1e-9)
198
+ S_obs, t_obs, y_obs, m_obs = _pad(
199
+ pos_all[past],
200
+ np.broadcast_to(np.asarray(tg)[None, :], past.shape)[past],
201
+ y_all[past],
202
+ cfg.n_past_max(m),
203
+ )
204
 
205
  # ---- choose the placement
206
  if m == 0:
 
210
  elif policy == "sobol":
211
  idx = int(sobol_indices(grid, cfg.n_deploy, seed_offset)[m])
212
  else:
 
213
  j_at = int(round(t_m / cfg.obs_dt)) - 1 # obs index whose time is t_m
214
+ # Pad the existing-drifter set to n_deploy so this shape is also
215
+ # constant; drifters that already left are parked far outside, where
216
+ # advection freezes them and the Gram mask drops their trajectory.
217
+ exist_pos = np.full((cfg.n_deploy, 2), 1e6)
218
+ live = val_all[np.arange(m), j_at]
219
+ exist_pos[np.arange(m)[live]] = raw_all[np.arange(m), j_at][live]
220
+ exist_pos = jnp.asarray(exist_pos)
221
 
222
  p_pol, sig_pol = true_params, cfg.sigma_obs
223
  if policy == "ballast_opt":
224
  p_pol, sig_pol = optimise_hypers(
225
+ S_obs, t_obs, y_obs, cfg, bounds, true_params, mask=m_obs
226
  )
227
 
228
  if policy == "eig":
229
+ sc = eig_utilities(
230
+ grid, S_obs, t_obs, t_m, p_pol, sig_pol, cfg.chunk, mask=m_obs
231
+ )
232
  else:
233
  ops_pol = ops_true if policy != "ballast_opt" else make_ops(
234
  grid.R, p_pol, cfg.dt
235
  )
236
  mean, chol = posterior_ext_state(
237
+ S_obs, t_obs, y_obs, grid.R, t_m, p_pol, sig_pol, mask=m_obs
238
  )
239
  if policy == "dist_sep":
240
  sc = dist_sep_scores(
241
  ks, grid, ops_pol, mean, chol, exist_pos, S_obs, t_m,
242
+ cfg.T, cfg.dt, cfg.obs_every, cfg.n_samples, obs_mask=m_obs,
243
  )
244
  else: # ballast_true / ballast_opt
245
  u = ballast_sample_utilities(
246
  ks, grid, ops_pol, S_obs, t_obs, mean, chol, exist_pos,
247
  t_m, cfg.T, cfg.dt, cfg.obs_every, p_pol, sig_pol,
248
+ cfg.n_samples, cfg.chunk, obs_mask=m_obs,
249
  )
250
  sc = jnp.mean(u, axis=0)
251
  idx = int(jnp.argmax(sc))
 
269
 
270
  # ---- evaluate: all data from the m+1 drifters over the whole campaign
271
  sel = val_all[: m + 1]
272
+ S_e, t_e, y_e, m_e = _pad(
273
+ pos_all[: m + 1][sel],
274
+ np.broadcast_to(np.asarray(tg)[None, :], sel.shape)[sel],
275
+ y_all[: m + 1][sel],
276
+ cfg.n_all_max(m),
277
+ )
278
  mu = posterior_mean_field(
279
+ S_e, t_e, y_e, grid.R, t_eval, eval_params, cfg.sigma_obs, mask=m_e
280
  )
281
  gt_at_eval = gt_fields[(jnp.round(t_eval / cfg.dt)).astype(int)]
282
  err = float(jnp.mean(jnp.linalg.norm(mu - gt_at_eval, axis=-1)))
ballast/gp.py CHANGED
@@ -54,25 +54,47 @@ def logdet_chol(M: jnp.ndarray) -> jnp.ndarray:
54
  return 2.0 * jnp.sum(jnp.log(jnp.diagonal(L, axis1=-2, axis2=-1)), axis=-1)
55
 
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  # --------------------------------------------------------------------------
58
  # Regression
59
  # --------------------------------------------------------------------------
60
 
61
 
62
  def log_marginal_likelihood(
63
- p: HelmParams, S: jnp.ndarray, t: jnp.ndarray, y: jnp.ndarray, sigma: float
64
  ) -> jnp.ndarray:
65
  """Log marginal likelihood of the plain (non-extended) temporal Helmholtz GP.
66
 
67
  y is (n, 2) velocity observations; flattened point-major/component-minor.
 
 
 
68
  """
69
- K = k_thelm_mat(S, t, S, t, p)
70
- n = K.shape[0]
71
- A = K + (sigma**2) * jnp.eye(n)
72
  c, low = cho_factor(A)
73
  yy = y.reshape(-1)
 
 
74
  alpha = cho_solve((c, low), yy)
75
  ld = 2.0 * jnp.sum(jnp.log(jnp.diag(c)))
 
76
  return -0.5 * yy @ alpha - 0.5 * ld - 0.5 * n * jnp.log(2 * jnp.pi)
77
 
78
 
@@ -84,6 +106,7 @@ def posterior_mean_field(
84
  t_eval: jnp.ndarray,
85
  p: HelmParams,
86
  sigma: float,
 
87
  ) -> jnp.ndarray:
88
  """Posterior predictive mean of the velocity field on R x t_eval.
89
 
@@ -91,16 +114,18 @@ def posterior_mean_field(
91
  average L2 error of the posterior mean field over the spatial grid and the
92
  full set of deployment times.
93
  """
94
- K = k_thelm_mat(S, t, S, t, p)
95
- A = K + (sigma**2) * jnp.eye(K.shape[0])
96
  c, low = cho_factor(A)
97
- alpha = cho_solve((c, low), y.reshape(-1))
 
 
 
98
 
99
  N = R.shape[0]
100
  Rr = jnp.tile(R, (t_eval.shape[0], 1)) # (nt*N, 2)
101
  tr = jnp.repeat(t_eval, N)
102
  Kx = k_thelm_mat(S, t, Rr, tr, p) # (2n, 2*nt*N)
103
- mu = Kx.T @ alpha
104
  return mu.reshape(t_eval.shape[0], N, 2)
105
 
106
 
@@ -113,6 +138,7 @@ def posterior_ext_state(
113
  p: HelmParams,
114
  sigma: float,
115
  jitter: float = 1e-8,
 
116
  ):
117
  """Posterior of the extended state f(R, t_m) = [f, d_t f]^T given D_m.
118
 
@@ -123,8 +149,7 @@ def posterior_ext_state(
123
 
124
  Returns (mean (4N,), chol of covariance (4N, 4N)).
125
  """
126
- K = k_thelm_mat(S, t, S, t, p)
127
- A = K + (sigma**2) * jnp.eye(K.shape[0])
128
  c, low = cho_factor(A)
129
 
130
  N = R.shape[0]
@@ -132,7 +157,13 @@ def posterior_ext_state(
132
  K_ot = k_ext_cross(S, t, R, tvec, p) # (2n, 4N)
133
  K_tt = k_ext_full(R, tvec, p) # (4N, 4N)
134
 
135
- mean = K_ot.T @ cho_solve((c, low), y.reshape(-1))
 
 
 
 
 
 
136
  cov = K_tt - K_ot.T @ cho_solve((c, low), K_ot)
137
  cov = 0.5 * (cov + cov.T) + jitter * jnp.eye(cov.shape[0])
138
  return mean, jnp.linalg.cholesky(cov)
 
54
  return 2.0 * jnp.sum(jnp.log(jnp.diagonal(L, axis1=-2, axis2=-1)), axis=-1)
55
 
56
 
57
+ def reg_matrix(S, t, p: HelmParams, sigma, mask=None):
58
+ """A = K(X) + sigma^2 I for regression, with padded points neutralised.
59
+
60
+ Observation arrays are padded to shapes that depend only on the deployment
61
+ index (never on how many drifters happen to still be inside the region), so
62
+ that XLA compiles each shape once and reuses it across all runs. A padded
63
+ row i is turned into e_i and paired with y_i = 0, which leaves
64
+ alpha = A^{-1} y zero there and every downstream quantity untouched.
65
+ """
66
+ K = k_thelm_mat(S, t, S, t, p)
67
+ n = K.shape[0]
68
+ A = K + (sigma**2) * jnp.eye(n)
69
+ if mask is not None:
70
+ m = _expand_mask(mask).astype(A.dtype)
71
+ A = A * m[:, None] * m[None, :] + jnp.diag(1.0 - m)
72
+ return A
73
+
74
+
75
  # --------------------------------------------------------------------------
76
  # Regression
77
  # --------------------------------------------------------------------------
78
 
79
 
80
  def log_marginal_likelihood(
81
+ p: HelmParams, S: jnp.ndarray, t: jnp.ndarray, y: jnp.ndarray, sigma: float, mask=None
82
  ) -> jnp.ndarray:
83
  """Log marginal likelihood of the plain (non-extended) temporal Helmholtz GP.
84
 
85
  y is (n, 2) velocity observations; flattened point-major/component-minor.
86
+ Padded entries contribute a unit diagonal block and zero residual, i.e.
87
+ nothing to the log-likelihood beyond an additive constant (which does not
88
+ move the optimiser's argmax).
89
  """
90
+ A = reg_matrix(S, t, p, sigma, mask)
 
 
91
  c, low = cho_factor(A)
92
  yy = y.reshape(-1)
93
+ if mask is not None:
94
+ yy = yy * _expand_mask(mask).astype(yy.dtype)
95
  alpha = cho_solve((c, low), yy)
96
  ld = 2.0 * jnp.sum(jnp.log(jnp.diag(c)))
97
+ n = A.shape[0]
98
  return -0.5 * yy @ alpha - 0.5 * ld - 0.5 * n * jnp.log(2 * jnp.pi)
99
 
100
 
 
106
  t_eval: jnp.ndarray,
107
  p: HelmParams,
108
  sigma: float,
109
+ mask=None,
110
  ) -> jnp.ndarray:
111
  """Posterior predictive mean of the velocity field on R x t_eval.
112
 
 
114
  average L2 error of the posterior mean field over the spatial grid and the
115
  full set of deployment times.
116
  """
117
+ A = reg_matrix(S, t, p, sigma, mask)
 
118
  c, low = cho_factor(A)
119
+ yy = y.reshape(-1)
120
+ if mask is not None:
121
+ yy = yy * _expand_mask(mask).astype(yy.dtype)
122
+ alpha = cho_solve((c, low), yy)
123
 
124
  N = R.shape[0]
125
  Rr = jnp.tile(R, (t_eval.shape[0], 1)) # (nt*N, 2)
126
  tr = jnp.repeat(t_eval, N)
127
  Kx = k_thelm_mat(S, t, Rr, tr, p) # (2n, 2*nt*N)
128
+ mu = Kx.T @ alpha # padded rows carry alpha = 0 and drop out
129
  return mu.reshape(t_eval.shape[0], N, 2)
130
 
131
 
 
138
  p: HelmParams,
139
  sigma: float,
140
  jitter: float = 1e-8,
141
+ mask=None,
142
  ):
143
  """Posterior of the extended state f(R, t_m) = [f, d_t f]^T given D_m.
144
 
 
149
 
150
  Returns (mean (4N,), chol of covariance (4N, 4N)).
151
  """
152
+ A = reg_matrix(S, t, p, sigma, mask)
 
153
  c, low = cho_factor(A)
154
 
155
  N = R.shape[0]
 
157
  K_ot = k_ext_cross(S, t, R, tvec, p) # (2n, 4N)
158
  K_tt = k_ext_full(R, tvec, p) # (4N, 4N)
159
 
160
+ yy = y.reshape(-1)
161
+ if mask is not None:
162
+ mm = _expand_mask(mask).astype(K_ot.dtype)
163
+ yy = yy * mm
164
+ K_ot = K_ot * mm[:, None] # padded rows must not leak into the posterior
165
+
166
+ mean = K_ot.T @ cho_solve((c, low), yy)
167
  cov = K_tt - K_ot.T @ cho_solve((c, low), K_ot)
168
  cov = 0.5 * (cov + cov.T) + jitter * jnp.eye(cov.shape[0])
169
  return mean, jnp.linalg.cholesky(cov)
ballast/policies.py CHANGED
@@ -80,6 +80,7 @@ def ballast_sample_utilities(
80
  sigma: float,
81
  n_samples: int,
82
  chunk: int = 64,
 
83
  ):
84
  """Per-sample BALLAST utilities: returns (n_samples, N_space).
85
 
@@ -104,9 +105,8 @@ def ballast_sample_utilities(
104
  t_base = jnp.concatenate(
105
  [t_obs, jnp.repeat(t_traj[:, None], n_e, axis=1).reshape(-1)], axis=0
106
  )
107
- m_base = jnp.concatenate(
108
- [jnp.ones(S_obs.shape[0], dtype=bool), eval_.reshape(-1)], axis=0
109
- )
110
  L, ld = base_factor(S_base, t_base, p, sigma, m_base)
111
 
112
  def one(i):
@@ -135,6 +135,7 @@ def true_field_utilities(
135
  p: HelmParams,
136
  sigma: float,
137
  chunk: int = 64,
 
138
  ):
139
  """B(s; true): utilities with trajectories simulated in the ground-truth field.
140
 
@@ -157,9 +158,8 @@ def true_field_utilities(
157
  t_base = jnp.concatenate(
158
  [t_obs, jnp.repeat(t_traj[:, None], n_e, axis=1).reshape(-1)], axis=0
159
  )
160
- m_base = jnp.concatenate(
161
- [jnp.ones(S_obs.shape[0], dtype=bool), eval_.reshape(-1)], axis=0
162
- )
163
  L, ld = base_factor(S_base, t_base, p, sigma, m_base)
164
 
165
  def one(i):
@@ -179,15 +179,16 @@ def true_field_utilities(
179
 
180
 
181
  def eig_utilities(
182
- grid: Grid, S_obs, t_obs, t_m: float, p: HelmParams, sigma: float, chunk: int = 128
 
183
  ):
184
  """logdet(I + sigma^-2 K(X_n u {(s, t_n)})) for every candidate s."""
185
- L, ld = base_factor(S_obs, t_obs, p, sigma, None)
186
  tv = jnp.array([t_m])
187
 
188
  def one(i):
189
  return rank_q_logdet(
190
- L, ld, S_obs, t_obs, None,
191
  grid.R[i][None, :], tv, jnp.ones(1, dtype=bool), p, sigma,
192
  )
193
 
@@ -215,6 +216,7 @@ def dist_sep_scores(
215
  dt: float,
216
  obs_every: int,
217
  n_samples: int,
 
218
  ):
219
  """Rank-average of (i) expected drifter path length and (ii) separation.
220
 
@@ -237,6 +239,8 @@ def dist_sep_scores(
237
  length = jnp.mean(jnp.stack(lengths), axis=0) # (N,)
238
 
239
  d = jnp.linalg.norm(grid.R[:, None, :] - S_obs[None, :, :], axis=-1)
 
 
240
  separation = -jnp.min(d, axis=1) # negative distance to closest observation
241
 
242
  r1 = jnp.argsort(jnp.argsort(length))
 
80
  sigma: float,
81
  n_samples: int,
82
  chunk: int = 64,
83
+ obs_mask=None,
84
  ):
85
  """Per-sample BALLAST utilities: returns (n_samples, N_space).
86
 
 
105
  t_base = jnp.concatenate(
106
  [t_obs, jnp.repeat(t_traj[:, None], n_e, axis=1).reshape(-1)], axis=0
107
  )
108
+ om = jnp.ones(S_obs.shape[0], dtype=bool) if obs_mask is None else obs_mask
109
+ m_base = jnp.concatenate([om, eval_.reshape(-1)], axis=0)
 
110
  L, ld = base_factor(S_base, t_base, p, sigma, m_base)
111
 
112
  def one(i):
 
135
  p: HelmParams,
136
  sigma: float,
137
  chunk: int = 64,
138
+ obs_mask=None,
139
  ):
140
  """B(s; true): utilities with trajectories simulated in the ground-truth field.
141
 
 
158
  t_base = jnp.concatenate(
159
  [t_obs, jnp.repeat(t_traj[:, None], n_e, axis=1).reshape(-1)], axis=0
160
  )
161
+ om = jnp.ones(S_obs.shape[0], dtype=bool) if obs_mask is None else obs_mask
162
+ m_base = jnp.concatenate([om, eval_.reshape(-1)], axis=0)
 
163
  L, ld = base_factor(S_base, t_base, p, sigma, m_base)
164
 
165
  def one(i):
 
179
 
180
 
181
  def eig_utilities(
182
+ grid: Grid, S_obs, t_obs, t_m: float, p: HelmParams, sigma: float,
183
+ chunk: int = 128, mask=None,
184
  ):
185
  """logdet(I + sigma^-2 K(X_n u {(s, t_n)})) for every candidate s."""
186
+ L, ld = base_factor(S_obs, t_obs, p, sigma, mask)
187
  tv = jnp.array([t_m])
188
 
189
  def one(i):
190
  return rank_q_logdet(
191
+ L, ld, S_obs, t_obs, mask,
192
  grid.R[i][None, :], tv, jnp.ones(1, dtype=bool), p, sigma,
193
  )
194
 
 
216
  dt: float,
217
  obs_every: int,
218
  n_samples: int,
219
+ obs_mask=None,
220
  ):
221
  """Rank-average of (i) expected drifter path length and (ii) separation.
222
 
 
239
  length = jnp.mean(jnp.stack(lengths), axis=0) # (N,)
240
 
241
  d = jnp.linalg.norm(grid.R[:, None, :] - S_obs[None, :, :], axis=-1)
242
+ if obs_mask is not None: # padded points sit at 1e6 and must not be "closest"
243
+ d = jnp.where(obs_mask[None, :], d, jnp.inf)
244
  separation = -jnp.min(d, axis=1) # negative distance to closest observation
245
 
246
  r1 = jnp.argsort(jnp.argsort(length))