Skip to content

Commit dd43291

Browse files
Renaming KernelParticle to ParticleSetView
1 parent be6d1be commit dd43291

4 files changed

Lines changed: 48 additions & 48 deletions

File tree

src/parcels/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
Variable,
1818
Particle,
1919
ParticleClass,
20-
KernelParticle, # ? remove?
20+
ParticleSetView, # ? remove?
2121
)
2222
from parcels._core.field import Field, VectorField
2323
from parcels._core.basegrid import BaseGrid
@@ -88,7 +88,7 @@
8888
"download_example_dataset",
8989
"list_example_datasets",
9090
# (marked for potential removal)
91-
"KernelParticle",
91+
"ParticleSetView",
9292
]
9393

9494
_stdlib_warnings.warn(

src/parcels/_core/field.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
_unitconverters_map,
1515
)
1616
from parcels._core.index_search import GRID_SEARCH_ERROR, LEFT_OUT_OF_BOUNDS, RIGHT_OUT_OF_BOUNDS, _search_time_index
17-
from parcels._core.particle import KernelParticle
17+
from parcels._core.particle import ParticleSetView
1818
from parcels._core.statuscodes import (
1919
AllParcelsErrorCodes,
2020
StatusCode,
@@ -35,9 +35,9 @@
3535

3636

3737
def _deal_with_errors(error, key, vector_type: VectorType):
38-
if isinstance(key, KernelParticle):
38+
if isinstance(key, ParticleSetView):
3939
key.state = AllParcelsErrorCodes[type(error)]
40-
elif isinstance(key[-1], KernelParticle):
40+
elif isinstance(key[-1], ParticleSetView):
4141
key[-1].state = AllParcelsErrorCodes[type(error)]
4242
else:
4343
raise RuntimeError(f"{error}. Error could not be handled because particles was not part of the Field Sampling.")
@@ -229,7 +229,7 @@ def eval(self, time: datetime, z, y, x, particles=None, applyConversion=True):
229229
def __getitem__(self, key):
230230
self._check_velocitysampling()
231231
try:
232-
if isinstance(key, KernelParticle):
232+
if isinstance(key, ParticleSetView):
233233
return self.eval(key.time, key.z, key.lat, key.lon, key)
234234
else:
235235
return self.eval(*key)
@@ -330,7 +330,7 @@ def eval(self, time: datetime, z, y, x, particles=None, applyConversion=True):
330330

331331
def __getitem__(self, key):
332332
try:
333-
if isinstance(key, KernelParticle):
333+
if isinstance(key, ParticleSetView):
334334
return self.eval(key.time, key.z, key.lat, key.lon, key)
335335
else:
336336
return self.eval(*key)

src/parcels/_core/particle.py

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from parcels._core.utils.time import TimeInterval
1212
from parcels._reprs import _format_list_items_multiline
1313

14-
__all__ = ["KernelParticle", "Particle", "ParticleClass", "Variable"]
14+
__all__ = ["Particle", "ParticleClass", "ParticleSetView", "Variable"]
1515
_TO_WRITE_OPTIONS = [True, False, "once"]
1616

1717

@@ -116,7 +116,7 @@ def add_variable(self, variable: Variable | list[Variable]):
116116
return ParticleClass(variables=self.variables + variable)
117117

118118

119-
class KernelParticle:
119+
class ParticleSetView:
120120
"""Class to be used in a kernel that links a particle (on the kernel level) to a particle dataset."""
121121

122122
def __init__(self, data, index):
@@ -129,15 +129,15 @@ def __getattr__(self, name):
129129
# enables constructs like `particles.dlon[mask] += vals` to update
130130
# the parent arrays rather than temporary copies.
131131
if name in self._data:
132-
# If this KernelParticle represents a single particle (integer
132+
# If this ParticleSetView represents a single particle (integer
133133
# index), return the underlying scalar directly to preserve
134134
# user-facing semantics (e.g., `pset[0].time` should be a number).
135135
if isinstance(self._index, (int, np.integer)):
136136
return self._data[name][self._index]
137137
# For 0-d numpy integer scalars
138138
if isinstance(self._index, np.ndarray) and self._index.ndim == 0:
139139
return self._data[name][int(self._index)]
140-
return KernelParticleArray(self._data, self._index, name)
140+
return ParticleSetViewArray(self._data, self._index, name)
141141
return self._data[name][self._index]
142142

143143
def __setattr__(self, name, value):
@@ -166,7 +166,7 @@ def __getitem__(self, index):
166166
raise ValueError(
167167
f"Boolean index has incompatible length {arr.size} for selection of size {int(np.sum(base))}"
168168
)
169-
return KernelParticle(self._data, new_index)
169+
return ParticleSetView(self._data, new_index)
170170

171171
# Integer array / list of indices relative to local view
172172
if isinstance(index, (np.ndarray, list)):
@@ -186,7 +186,7 @@ def __getitem__(self, index):
186186
base_arr = np.asarray(base)
187187
sel = base_arr[idx_arr]
188188
new_index[sel] = True
189-
return KernelParticle(self._data, new_index)
189+
return ParticleSetView(self._data, new_index)
190190

191191
# Slice or single integer index relative to local view
192192
if isinstance(index, slice) or isinstance(index, int):
@@ -198,23 +198,23 @@ def __getitem__(self, index):
198198
base_arr = np.asarray(base)
199199
sel = base_arr[index]
200200
new_index[sel] = True
201-
return KernelParticle(self._data, new_index)
201+
return ParticleSetView(self._data, new_index)
202202

203203
# Fallback: try to assign directly (preserves previous behaviour for other index types)
204204
try:
205205
new_index[base] = index
206-
return KernelParticle(self._data, new_index)
206+
return ParticleSetView(self._data, new_index)
207207
except Exception as e:
208-
raise TypeError(f"Unsupported index type for KernelParticle.__getitem__: {type(index)!r}") from e
208+
raise TypeError(f"Unsupported index type for ParticleSetView.__getitem__: {type(index)!r}") from e
209209

210210
# def __setitem__(self, index, value):
211211
# """Assign to a subset of particles represented by `index` relative to
212-
# this KernelParticle's current selection.
212+
# this ParticleSetView's current selection.
213213

214214
# The incoming `index` is interpreted in the same way as for
215215
# `__getitem__`: it indexes into the subset defined by `self._index`.
216216

217-
# `value` may be another KernelParticle (in which case common variables
217+
# `value` may be another ParticleSetView (in which case common variables
218218
# are copied), or a dict mapping variable names to arrays/scalars which
219219
# will be written into the parent arrays at the computed positions.
220220
# """
@@ -228,8 +228,8 @@ def __getitem__(self, index):
228228
# # write into parent array at positions new_index
229229
# self._data[varname][new_index] = src
230230

231-
# # Case: assign from another KernelParticle-like object
232-
# if isinstance(value, KernelParticle):
231+
# # Case: assign from another ParticleSetView-like object
232+
# if isinstance(value, ParticleSetView):
233233
# # copy across common fields
234234
# for k in set(self._data.keys()).intersection(value._data.keys()):
235235
# _assign(k, value._data[k][value._index])
@@ -245,13 +245,13 @@ def __getitem__(self, index):
245245

246246
# # Otherwise, if a scalar/array is provided, assign it to all variables
247247
# # is ambiguous: raise TypeError to avoid surprising behaviour.
248-
# raise TypeError("Unsupported value for KernelParticle.__setitem__; provide a KernelParticle or dict of variable values")
248+
# raise TypeError("Unsupported value for ParticleSetView.__setitem__; provide a ParticleSetView or dict of variable values")
249249

250250
def __len__(self):
251251
return len(self._index)
252252

253253

254-
class KernelParticleArray:
254+
class ParticleSetViewArray:
255255
"""Array-like proxy for a particle variable that writes through to the
256256
parent arrays when mutated.
257257
@@ -366,7 +366,7 @@ def __getitem__(self, subindex):
366366
return local[subindex]
367367

368368
new_index = self._to_global_index(subindex)
369-
return KernelParticleArray(self._data, new_index, self._name)
369+
return ParticleSetViewArray(self._data, new_index, self._name)
370370

371371
def __setitem__(self, subindex, value):
372372
tgt = self._to_global_index(subindex)
@@ -375,43 +375,43 @@ def __setitem__(self, subindex, value):
375375
# in-place ops must write back into the parent array
376376
def __iadd__(self, other):
377377
vals = self._data[self._name][self._index] + (
378-
other.__array__() if isinstance(other, KernelParticleArray) else other
378+
other.__array__() if isinstance(other, ParticleSetViewArray) else other
379379
)
380380
self._data[self._name][self._index] = vals
381381
return self
382382

383383
def __isub__(self, other):
384384
vals = self._data[self._name][self._index] - (
385-
other.__array__() if isinstance(other, KernelParticleArray) else other
385+
other.__array__() if isinstance(other, ParticleSetViewArray) else other
386386
)
387387
self._data[self._name][self._index] = vals
388388
return self
389389

390390
def __imul__(self, other):
391391
vals = self._data[self._name][self._index] * (
392-
other.__array__() if isinstance(other, KernelParticleArray) else other
392+
other.__array__() if isinstance(other, ParticleSetViewArray) else other
393393
)
394394
self._data[self._name][self._index] = vals
395395
return self
396396

397397
# Provide simple numpy-like evaluation for binary ops by delegating to ndarray
398398
def __add__(self, other):
399-
return self.__array__() + (other.__array__() if isinstance(other, KernelParticleArray) else other)
399+
return self.__array__() + (other.__array__() if isinstance(other, ParticleSetViewArray) else other)
400400

401401
def __sub__(self, other):
402-
return self.__array__() - (other.__array__() if isinstance(other, KernelParticleArray) else other)
402+
return self.__array__() - (other.__array__() if isinstance(other, ParticleSetViewArray) else other)
403403

404404
def __mul__(self, other):
405-
return self.__array__() * (other.__array__() if isinstance(other, KernelParticleArray) else other)
405+
return self.__array__() * (other.__array__() if isinstance(other, ParticleSetViewArray) else other)
406406

407407
def __truediv__(self, other):
408-
return self.__array__() / (other.__array__() if isinstance(other, KernelParticleArray) else other)
408+
return self.__array__() / (other.__array__() if isinstance(other, ParticleSetViewArray) else other)
409409

410410
def __floordiv__(self, other):
411-
return self.__array__() // (other.__array__() if isinstance(other, KernelParticleArray) else other)
411+
return self.__array__() // (other.__array__() if isinstance(other, ParticleSetViewArray) else other)
412412

413413
def __pow__(self, other):
414-
return self.__array__() ** (other.__array__() if isinstance(other, KernelParticleArray) else other)
414+
return self.__array__() ** (other.__array__() if isinstance(other, ParticleSetViewArray) else other)
415415

416416
def __neg__(self):
417417
return -self.__array__()
@@ -422,71 +422,71 @@ def __pos__(self):
422422
def __abs__(self):
423423
return abs(self.__array__())
424424

425-
# Right-hand operations to handle cases like `scalar - KernelParticleArray`
425+
# Right-hand operations to handle cases like `scalar - ParticleSetViewArray`
426426
def __radd__(self, other):
427-
return (other.__array__() if isinstance(other, KernelParticleArray) else other) + self.__array__()
427+
return (other.__array__() if isinstance(other, ParticleSetViewArray) else other) + self.__array__()
428428

429429
def __rsub__(self, other):
430-
return (other.__array__() if isinstance(other, KernelParticleArray) else other) - self.__array__()
430+
return (other.__array__() if isinstance(other, ParticleSetViewArray) else other) - self.__array__()
431431

432432
def __rmul__(self, other):
433-
return (other.__array__() if isinstance(other, KernelParticleArray) else other) * self.__array__()
433+
return (other.__array__() if isinstance(other, ParticleSetViewArray) else other) * self.__array__()
434434

435435
def __rtruediv__(self, other):
436-
return (other.__array__() if isinstance(other, KernelParticleArray) else other) / self.__array__()
436+
return (other.__array__() if isinstance(other, ParticleSetViewArray) else other) / self.__array__()
437437

438438
def __rfloordiv__(self, other):
439-
return (other.__array__() if isinstance(other, KernelParticleArray) else other) // self.__array__()
439+
return (other.__array__() if isinstance(other, ParticleSetViewArray) else other) // self.__array__()
440440

441441
def __rpow__(self, other):
442-
return (other.__array__() if isinstance(other, KernelParticleArray) else other) ** self.__array__()
442+
return (other.__array__() if isinstance(other, ParticleSetViewArray) else other) ** self.__array__()
443443

444444
# Comparison operators should return plain numpy boolean arrays so that
445445
# expressions like `mask = particles.gridID == gid` produce an ndarray
446-
# usable for indexing (rather than another KernelParticleArray).
446+
# usable for indexing (rather than another ParticleSetViewArray).
447447
def __eq__(self, other):
448448
left = np.asarray(self.__array__())
449-
if isinstance(other, KernelParticleArray):
449+
if isinstance(other, ParticleSetViewArray):
450450
right = np.asarray(other.__array__())
451451
else:
452452
right = other
453453
return left == right
454454

455455
def __ne__(self, other):
456456
left = np.asarray(self.__array__())
457-
if isinstance(other, KernelParticleArray):
457+
if isinstance(other, ParticleSetViewArray):
458458
right = np.asarray(other.__array__())
459459
else:
460460
right = other
461461
return left != right
462462

463463
def __lt__(self, other):
464464
left = np.asarray(self.__array__())
465-
if isinstance(other, KernelParticleArray):
465+
if isinstance(other, ParticleSetViewArray):
466466
right = np.asarray(other.__array__())
467467
else:
468468
right = other
469469
return left < right
470470

471471
def __le__(self, other):
472472
left = np.asarray(self.__array__())
473-
if isinstance(other, KernelParticleArray):
473+
if isinstance(other, ParticleSetViewArray):
474474
right = np.asarray(other.__array__())
475475
else:
476476
right = other
477477
return left <= right
478478

479479
def __gt__(self, other):
480480
left = np.asarray(self.__array__())
481-
if isinstance(other, KernelParticleArray):
481+
if isinstance(other, ParticleSetViewArray):
482482
right = np.asarray(other.__array__())
483483
else:
484484
right = other
485485
return left > right
486486

487487
def __ge__(self, other):
488488
left = np.asarray(self.__array__())
489-
if isinstance(other, KernelParticleArray):
489+
if isinstance(other, ParticleSetViewArray):
490490
right = np.asarray(other.__array__())
491491
else:
492492
right = other

src/parcels/_core/particleset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from parcels._core.converters import _convert_to_flat_array
1313
from parcels._core.kernel import Kernel
14-
from parcels._core.particle import KernelParticle, Particle, create_particle_data
14+
from parcels._core.particle import Particle, ParticleSetView, create_particle_data
1515
from parcels._core.statuscodes import StatusCode
1616
from parcels._core.utils.time import (
1717
TimeInterval,
@@ -166,7 +166,7 @@ def __getattr__(self, name):
166166

167167
def __getitem__(self, index):
168168
"""Get a single particle by index."""
169-
return KernelParticle(self._data, index=index)
169+
return ParticleSetView(self._data, index=index)
170170

171171
def __setattr__(self, name, value):
172172
if name in ["_data"]:

0 commit comments

Comments
 (0)