Skip to content

Commit 7ed5401

Browse files
Add boolean mask indexing for Modes class
Implement `__getitem__` method to support selecting a subset of modes using a boolean mask. This allows users to easily filter modes based on specific criteria.
1 parent f54981d commit 7ed5401

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

pyMMF/modes.py

+40
Original file line numberDiff line numberDiff line change
@@ -541,3 +541,43 @@ def fromFile(cls, filename: str):
541541
obj = cls()
542542
obj.load(filename)
543543
return obj
544+
545+
def __getitem__(self, mask):
546+
"""
547+
Allows selection of a subset of modes using a boolean mask.
548+
549+
Parameters
550+
----------
551+
mask : array-like
552+
A boolean array of size `self.number` indicating which modes to select.
553+
554+
Returns
555+
-------
556+
Modes
557+
A new Modes instance containing only the selected modes.
558+
"""
559+
if len(mask) != self.number:
560+
raise ValueError("Mask size must match the number of modes.")
561+
562+
# Create a new Modes instance
563+
new_modes = Modes()
564+
565+
# Copy the selected elements to the new instance
566+
new_modes.betas = [b for b, m in zip(self.betas, mask) if m]
567+
new_modes.u = [u for u, m in zip(self.u, mask) if m]
568+
new_modes.w = [w for w, m in zip(self.w, mask) if m]
569+
new_modes.m = [m for m, msk in zip(self.m, mask) if msk]
570+
new_modes.l = [l for l, msk in zip(self.l, mask) if msk]
571+
new_modes.profiles = [p for p, m in zip(self.profiles, mask) if m]
572+
new_modes.modesList = [ml for ml, m in zip(self.modesList, mask) if m]
573+
new_modes.data = [d for d, m in zip(self.data, mask) if m]
574+
575+
# Copy other attributes
576+
new_modes.number = sum(mask)
577+
new_modes.indexProfile = self.indexProfile
578+
new_modes.modeMatrix = self.modeMatrix
579+
new_modes.wl = self.wl
580+
new_modes.curvature = self.curvature
581+
new_modes.poisson = self.poisson
582+
583+
return new_modes

0 commit comments

Comments
 (0)