Skip to content

Commit

Permalink
Save selection as array of bool values
Browse files Browse the repository at this point in the history
  • Loading branch information
MBartkowiakSTFC committed Feb 14, 2025
1 parent edaedc7 commit 795a7b6
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 24 deletions.
8 changes: 6 additions & 2 deletions MDANSE/Src/MDANSE/Framework/AtomSelector/atom_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
from MDANSE.MolecularDynamics.Trajectory import Trajectory


def select_atoms(trajectory: Trajectory, **function_parameters: Dict[str, Any]) -> Set[int]:
def select_atoms(
trajectory: Trajectory, **function_parameters: Dict[str, Any]
) -> Set[int]:
"""Selects all the atoms in the trajectory.
Parameters
Expand All @@ -46,7 +48,9 @@ def select_atoms(trajectory: Trajectory, **function_parameters: Dict[str, Any])
if index_list is not None:
selection = selection.union(indices.intersection(index_list))
if index_range is not None:
selection = selection.union(indices.intersection(range(index_range[0], index_range[1])))
selection = selection.union(
indices.intersection(range(index_range[0], index_range[1]))
)
if index_slice is not None:
selection = selection.union(
indices.intersection(range(index_slice[0], index_slice[1], index_slice[2]))
Expand Down
8 changes: 6 additions & 2 deletions MDANSE/Src/MDANSE/Framework/AtomSelector/general_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
from MDANSE.MolecularDynamics.Trajectory import Trajectory


def select_all(trajectory: Trajectory, **function_parameters: Dict[str, Any]) -> Set[int]:
def select_all(
trajectory: Trajectory, **function_parameters: Dict[str, Any]
) -> Set[int]:
"""Selects all the atoms in the trajectory.
Parameters
Expand All @@ -37,7 +39,9 @@ def select_all(trajectory: Trajectory, **function_parameters: Dict[str, Any]) ->
return set(range(len(trajectory.chemical_system.atom_list)))


def select_none(trajectory: Trajectory, **function_parameters: Dict[str, Any]) -> Set[int]:
def select_none(
trajectory: Trajectory, **function_parameters: Dict[str, Any]
) -> Set[int]:
"""Returns an empty selection.
Parameters
Expand Down
8 changes: 6 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,9 @@
from MDANSE.MolecularDynamics.Trajectory import Trajectory


def select_labels(trajectory: Trajectory, **function_parameters: Dict[str, Any]) -> Set[int]:
def select_labels(
trajectory: Trajectory, **function_parameters: Dict[str, Any]
) -> Set[int]:
"""Selects all the atoms in the trajectory.
Parameters
Expand All @@ -45,7 +47,9 @@ def select_labels(trajectory: Trajectory, **function_parameters: Dict[str, Any])
return selection


def select_pattern(trajectory: Trajectory, **function_parameters: Dict[str, Any]) -> Set[int]:
def select_pattern(
trajectory: Trajectory, **function_parameters: Dict[str, Any]
) -> Set[int]:
"""Selects all the atoms in the trajectory.
Parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
from MDANSE.MolecularDynamics.Trajectory import Trajectory


def select_molecules(trajectory: Trajectory, **function_parameters: Dict[str, Any]) -> Set[int]:
def select_molecules(
trajectory: Trajectory, **function_parameters: Dict[str, Any]
) -> Set[int]:
"""Selects all the atoms in the trajectory.
Parameters
Expand All @@ -41,5 +43,7 @@ def select_molecules(trajectory: Trajectory, **function_parameters: Dict[str, An
molecule_names = function_parameters.get("molecule_names", None)
for molecule in molecule_names:
if molecule in system._clusters:
selection = selection.union(reduce(list.__add__, system._clusters[molecule]))
selection = selection.union(
reduce(list.__add__, system._clusters[molecule])
)
return selection
10 changes: 8 additions & 2 deletions MDANSE/Src/MDANSE/Framework/AtomSelector/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,15 @@ def validate_selection_string(
selection = selection.difference(temp_selection)
else:
selection = temp_selection
if len(selection.difference(current_selection)) > 0 and operation_type == "union":
if (
len(selection.difference(current_selection)) > 0
and operation_type == "union"
):
return True
elif len(current_selection.difference(selection)) > 0 and operation_type != "union":
elif (
len(current_selection.difference(selection)) > 0
and operation_type != "union"
):
return True
return False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AtomSelectionConfigurator(IConfigurator):
The defaults selection setting.
"""

_default = '{}'
_default = "{}"

def configure(self, value: str) -> None:
"""Configure an input value.
Expand All @@ -43,6 +43,8 @@ def configure(self, value: str) -> None:
value : str
The selection setting in a json readable format.
"""
self._original_input = value

trajConfig = self._configurable[self._dependencies["trajectory"]]
self.selector = ReusableSelection()

Expand All @@ -67,6 +69,7 @@ def configure(self, value: str) -> None:
self["flatten_indices"] = sorted(list(indices))

atoms = trajConfig["instance"].chemical_system.atom_list
self["total_number_of_atoms"] = len(atoms)
selectedAtoms = [atoms[idx] for idx in self["flatten_indices"]]

self["selection_length"] = len(self["flatten_indices"])
Expand All @@ -93,7 +96,7 @@ def get_natoms(self) -> dict[str, int]:
A dictionary of the number of atom per element.
"""
names, counts = np.unique(self["names"], return_counts=True)
nAtomsPerElement = {names[n]:counts[n] for n in range(len(names))}
nAtomsPerElement = {names[n]: counts[n] for n in range(len(names))}

return nAtomsPerElement

Expand Down Expand Up @@ -132,7 +135,7 @@ def get_information(self) -> str:

return "\n".join(info) + "\n"

def get_selector(self) -> 'ReusableSelection':
def get_selector(self) -> "ReusableSelection":
"""
Returns
-------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ def __init__(self, trajectory: Trajectory) -> None:
self._new_map = {}
self._current_trajectory = trajectory

def apply_transmutation(
self, selection_string: str, symbol: str
) -> None:
def apply_transmutation(self, selection_string: str, symbol: str) -> None:
"""With the selection dictionary update selector and then
update the transmutation map.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ def __init__(self, trajectory: Trajectory) -> None:
self._original_map[at_num] = 0.0
self._new_map = {}

def update_charges(
self, selection_string: str, charge: float
) -> None:
def update_charges(self, selection_string: str, charge: float) -> None:
"""With the selection dictionary update the selector and then
update the partial charge map.
Expand Down
16 changes: 16 additions & 0 deletions MDANSE/Src/MDANSE/Framework/Jobs/IJob.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@ def initialize(self):
)
except KeyError:
LOG.error("IJob did not find 'write_logs' in output_files")
if "atom_selection" in self.configuration:
try:
array_length = self.configuration["atom_selection"][
"total_number_of_atoms"
]
except KeyError:
LOG.warning(
"Job could not find total number of atoms in atom selection."
)
else:
valid_indices = self.configuration["atom_selection"]["flatten_indices"]
self._outputData.add(
"selected_atoms",
"LineOutputVariable",
[index in valid_indices for index in range(array_length)],
)

@abc.abstractmethod
def run_step(self, index):
Expand Down
8 changes: 3 additions & 5 deletions MDANSE_GUI/Src/MDANSE_GUI/Widgets/SelectionWidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,8 @@ def __init__(
self.atom_names = []
if trajectory:
self.atom_types = list(np.unique(trajectory.chemical_system.atom_list))
if trajectory.chemical_system._atom_names:
self.atom_names = list(
np.unique(trajectory.chemical_system._atom_names)
)
if trajectory.chemical_system.name_list:
self.atom_names = list(np.unique(trajectory.chemical_system.name_list))
self.selection_types = []
self.selection_keyword = ""
if self.atom_types:
Expand Down Expand Up @@ -127,7 +125,7 @@ def switch_mode(self, new_mode: str):
self.selection_field.addItems(self.atom_types)
self.selection_keyword = "atom_types"
elif new_mode == "name":
self.selection_field.addItems(self.atom_types)
self.selection_field.addItems(self.atom_names)
self.selection_keyword = "atom_names"

def parameter_dictionary(self):
Expand Down

0 comments on commit 795a7b6

Please sign in to comment.