Skip to content

Commit

Permalink
Improve docstrings, add all_indices property
Browse files Browse the repository at this point in the history
  • Loading branch information
MBartkowiakSTFC committed Mar 4, 2025
1 parent baac644 commit 05e0bd8
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 17 deletions.
7 changes: 6 additions & 1 deletion MDANSE/Src/MDANSE/Chemistry/ChemicalSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#

from __future__ import annotations
from typing import List, Tuple, Dict, Any
from typing import List, Tuple, Dict, Any, Set
import copy
from functools import reduce

Expand Down Expand Up @@ -229,6 +229,11 @@ def number_of_atoms(self) -> int:
"""The number of non-ghost atoms in the ChemicalSystem."""
return self._total_number_of_atoms

@property
def all_indices(self) -> Set[int]:
"""The number of non-ghost atoms in the ChemicalSystem."""
return set(self._atom_indices)

@property
def total_number_of_atoms(self) -> int:
"""The number of all atoms in the ChemicalSystem, including ghost ones."""
Expand Down
4 changes: 2 additions & 2 deletions MDANSE/Src/MDANSE/Framework/AtomSelector/atom_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def select_atoms(
index_slice: Sequence[int] = None,
atom_types: Sequence[str] = (),
atom_names: Sequence[str] = (),
**kwargs: str,
**_kwargs: str,
) -> Set[int]:
"""Selects specific atoms in the trajectory. These can be selected based
on indices, atom type or trajectory-specific atom name.
Expand Down Expand Up @@ -60,7 +60,7 @@ def select_atoms(
system = trajectory.chemical_system
element_list = system.atom_list
name_list = system.name_list
indices = set(range(len(element_list)))
indices = system.all_indices
if index_list is not None:
selection |= indices & set(index_list)
if index_range is not None:
Expand Down
10 changes: 5 additions & 5 deletions MDANSE/Src/MDANSE/Framework/AtomSelector/general_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from MDANSE.MolecularDynamics.Trajectory import Trajectory


def select_all(trajectory: Trajectory, **kwargs: str) -> Set[int]:
def select_all(trajectory: Trajectory, **_kwargs: str) -> Set[int]:
"""Selects all the atoms in the trajectory.
Parameters
Expand All @@ -31,10 +31,10 @@ def select_all(trajectory: Trajectory, **kwargs: str) -> Set[int]:
Set[int]
Set of all the atom indices
"""
return set(range(len(trajectory.chemical_system.atom_list)))
return trajectory.chemical_system.all_indices


def select_none(_trajectory: Trajectory, **kwargs: str) -> Set[int]:
def select_none(_trajectory: Trajectory, **_kwargs: str) -> Set[int]:
"""Returns an empty selection.
Parameters
Expand All @@ -51,7 +51,7 @@ def select_none(_trajectory: Trajectory, **kwargs: str) -> Set[int]:


def invert_selection(
trajectory: Trajectory, selection: Set[int], **kwargs: str
trajectory: Trajectory, selection: Set[int], **_kwargs: str
) -> Set[int]:
"""Returns a set of all the indices that are present in the trajectory
and were not included in the input selection.
Expand All @@ -66,7 +66,7 @@ def invert_selection(
Returns
-------
Set[int]
set of all the indices in the trajectory which were not in the selection
set of all the indices in the trajectory which were not in the input selection
"""
all_indices = select_all(trajectory)
inverted = all_indices - selection
Expand Down
4 changes: 2 additions & 2 deletions MDANSE/Src/MDANSE/Framework/AtomSelector/group_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


def select_labels(
trajectory: Trajectory, atom_labels: Sequence[str] = (), **kwargs: str
trajectory: Trajectory, atom_labels: Sequence[str] = (), **_kwargs: str
) -> Set[int]:
"""Selects atoms with a specific label in the trajectory.
A residue name can be read as a label by MDANSE.
Expand All @@ -46,7 +46,7 @@ def select_labels(


def select_pattern(
trajectory: Trajectory, rdkit_pattern: str = "", **kwargs: str
trajectory: Trajectory, rdkit_pattern: str = "", **_kwargs: str
) -> Set[int]:
"""Selects atoms according to the SMARTS string given as input.
This will only work if molecules and bonds have been detected in the system.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


def select_molecules(
trajectory: Trajectory, molecule_names: Sequence[str] = (), **kwargs: str
trajectory: Trajectory, molecule_names: Sequence[str] = (), **_kwargs: str
) -> Set[int]:
"""Selects all the atoms belonging to the specified molecule types.
Expand Down
28 changes: 26 additions & 2 deletions MDANSE/Src/MDANSE/Framework/AtomSelector/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def __init__(self) -> None:
self.reset()

def reset(self):
"""Initialises the attributes to an empty list of operations."""
self.system = None
self.trajectory = None
self.all_idxs = set()
Expand All @@ -71,6 +72,15 @@ def reset(self):
def set_selection(
self, number: Union[int, None] = None, function_parameters: Dict[str, Any] = {}
):
"""Appends a new selection operation, or overwrites an existing one.
Parameters
----------
number : Union[int, None], optional
the position of the new selection in the sequence of operations, by default None
function_parameters : Dict[str, Any], optional
the dictionary of keyword arguments defining a selection operation, by default {}
"""
if number is None:
number = len(self.operations)
else:
Expand Down Expand Up @@ -132,8 +142,21 @@ def validate_selection_string(
return False

def select_in_trajectory(self, trajectory: Trajectory) -> Set[int]:
"""Applies all the selection operations in sequence to the
input trajectory, and returns the resulting set of indices.
Parameters
----------
trajectory : Trajectory
trajectory object in which the atoms will be selected
Returns
-------
Set[int]
set of atom indices that have been selected in the input trajectory
"""
selection = set()
self.all_idxs = set(range(len(trajectory.chemical_system.atom_list)))
self.all_idxs = trajectory.chemical_system.all_indices
sequence = sorted(map(int, self.operations))
if not sequence:
return self.all_idxs
Expand Down Expand Up @@ -169,7 +192,8 @@ def convert_to_json(self) -> str:
return json.dumps(self.operations)

def load_from_json(self, json_string: str):
"""_summary_
"""Loads the atom selection operations from a JSON string.
Adds the operations to the selection sequence.
Parameters
----------
Expand Down
4 changes: 2 additions & 2 deletions MDANSE/Src/MDANSE/Framework/AtomSelector/spatial_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def select_positions(
frame_number: int = 0,
position_minimum: Union[Sequence[float], None] = None,
position_maximum: Union[Sequence[float], None] = None,
**kwargs: str,
**_kwargs: str,
) -> Set[int]:
"""Selects atoms based on their positions at a specified frame number.
Lower and upper limits of x, y and z coordinates can be given as input.
Expand Down Expand Up @@ -70,7 +70,7 @@ def select_sphere(
frame_number: int = 0,
sphere_centre: Sequence[float],
sphere_radius: float,
**kwargs: str,
**_kwargs: str,
) -> Set[int]:
"""Selects atoms within a distance from a fixed point in space,
based on coordinates at a specific frame number.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def __init__(
self.selection_model = SelectionModel(self.trajectory)
self._field = field
self.atm_full_names = self.system.name_list
self.molecule_names = list(map(str, self.system._clusters))
self.molecule_names = self.system.unique_molecules()
self.labels = list(map(str, self.system._labels))

self.selection_textbox = QPlainTextEdit()
Expand Down
2 changes: 1 addition & 1 deletion MDANSE_GUI/Src/MDANSE_GUI/Widgets/SelectionWidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def __init__(
):
self.molecule_names = []
if trajectory:
self.molecule_names = list(trajectory.chemical_system._clusters.keys())
self.molecule_names = trajectory.chemical_system.unique_molecules()
super().__init__(parent, widget_label)

def add_specific_widgets(self):
Expand Down

0 comments on commit 05e0bd8

Please sign in to comment.