Skip to content

Commit c8ae870

Browse files
Adjust code and fix script saving after changes
1 parent 03aa397 commit c8ae870

10 files changed

Lines changed: 56 additions & 50 deletions

File tree

MDANSE/Src/MDANSE/Chemistry/ChemicalSystem.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
import itertools as it
2020
from collections import defaultdict
2121
from collections.abc import Iterable
22-
from functools import reduce
2322
from pathlib import Path
2423
from typing import TYPE_CHECKING, Any, SupportsInt
2524

2625
import h5py
26+
import more_itertools
2727
import networkx as nx
2828
import numpy as np
2929
from more_itertools import padded
@@ -439,7 +439,7 @@ def find_clusters_from_bonds(self):
439439
while len(atom_pool) > 0:
440440
last_atom = atom_pool.pop()
441441
temp_dict = nx.dfs_successors(total_graph, last_atom)
442-
others = reduce(list.__iadd__, temp_dict.values(), [])
442+
others = list(more_itertools.flatten(temp_dict.values()))
443443
for atom in others:
444444
atom_pool.remove(atom)
445445
molecule = [last_atom, *others]

MDANSE/Src/MDANSE/Framework/Configurators/HDFTrajectoryConfigurator.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#
1616
from __future__ import annotations
1717

18+
import bisect
1819
from pathlib import Path
1920

2021
import h5py
@@ -23,7 +24,7 @@
2324
from MDANSE import PLATFORM
2425
from MDANSE.Framework.Configurators.IConfigurator import IConfigurator
2526
from MDANSE.Framework.Configurators.InputFileConfigurator import InputFileConfigurator
26-
from MDANSE.MolecularDynamics.Trajectory import Trajectory, check_hdf5_driver
27+
from MDANSE.MolecularDynamics.Trajectory import Trajectory
2728

2829
TIME_STEP_TOL = 1e-8
2930
DATASET_CACHE_SIZE = 2**24
@@ -98,10 +99,8 @@ def next_prime(nchunks: int) -> int:
9899
int
99100
A prime number larger than the input number.
100101
"""
101-
import bisect
102-
103-
ip = bisect.bisect_right(nchunks, PRIMES)
104-
return nchunks if ip == len(PRIMES) else PRIMES[ip]
102+
ip = bisect.bisect_right(PRIMES, nchunks)
103+
return nchunks if ip == len(PRIMES) else PRIMES[ip]
105104

106105

107106
def guess_hdf5_trajectory_parameters(
@@ -160,12 +159,12 @@ def configure(self, value):
160159
driver = None
161160
rdcc_nbytes, rdcc_nslots = guess_hdf5_trajectory_parameters(value)
162161
rdcc_w0 = None
163-
case (str(), str(), int(), int(), int()):
162+
case (str() | Path(), str(), int(), int(), float()):
164163
file_name, driver, rdcc_nbytes, rdcc_nslots, rdcc_w0 = value
165164
driver = driver if driver in HDF5_DRIVERS else None
166165
case _:
167166
self.error_status = f"Invalid value {value!r}"
168-
return
167+
return
169168

170169
self["driver"] = driver
171170
self["rdcc_nbytes"] = rdcc_nbytes

MDANSE/Src/MDANSE/Framework/Jobs/TrajectoryEditor.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
#
1616
from __future__ import annotations
1717

18-
from functools import reduce
19-
18+
import more_itertools
2019
import numpy as np
2120

2221
from MDANSE.Chemistry.ChemicalSystem import (
@@ -160,16 +159,17 @@ def initialize(self):
160159
com_conf = AbsoluteConfiguration(
161160
coords,
162161
)
163-
self.grouped_indices = list(more_itertools.flatten(new_chemical_system.clusters.values()))
162+
self.grouped_indices = list(
163+
more_itertools.flatten(new_chemical_system.clusters.values())
164+
)
164165
coords = com_conf.contiguous_configuration(self.grouped_indices).coordinates
165166
else:
166167
assign_molecules_after_atom_selection(
167168
self._indices, self._input_chemical_system, new_chemical_system
168169
)
169-
self.grouped_indices = reduce(
170-
list.__add__, new_chemical_system.clusters.values(), []
170+
self.grouped_indices = list(
171+
more_itertools.flatten(new_chemical_system.clusters.values())
171172
)
172-
173173
# The output trajectory is opened for writing.
174174
self._output_trajectory = TrajectoryWriter(
175175
self.configuration["output_files"]["file"],

MDANSE/Src/MDANSE/MolecularDynamics/Configuration.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
import abc
1919
import copy
2020
from collections.abc import Sequence
21-
from functools import reduce
2221
from typing import TYPE_CHECKING, Any
2322

23+
import more_itertools
2424
import networkx as nx
2525
import numpy as np
26+
import typing_extensions
2627

2728
from MDANSE.MLogging import LOG
2829

@@ -199,7 +200,7 @@ def continuous_coordinates(
199200
while len(atom_pool) > 0:
200201
last_atom = atom_pool.pop()
201202
temp_dict = nx.dfs_successors(total_graph, last_atom)
202-
others = reduce(list.__add__, temp_dict.values(), [])
203+
others = list(more_itertools.flatten(temp_dict.values()))
203204
for atom in others:
204205
atom_pool.pop(atom_pool.index(atom))
205206
segment = [last_atom, *others]
@@ -291,7 +292,6 @@ class _Configuration(metaclass=abc.ABCMeta):
291292
is_periodic: bool
292293

293294
def __init__(self, coords: ArrayLike, **variables):
294-
295295
self._variables = {}
296296

297297
self["coordinates"] = np.array(coords, dtype=float)
@@ -416,7 +416,7 @@ def __init__(
416416
raise ValueError("Invalid unit cell dimensions")
417417
self._unit_cell = unit_cell
418418

419-
def clone(self) -> _PeriodicConfiguration:
419+
def clone(self) -> typing_extensions.Self:
420420
"""Return a deep copy of this configuration."""
421421

422422
unit_cell = copy.deepcopy(self._unit_cell)

MDANSE/Src/MDANSE/MolecularDynamics/Trajectory.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import copy
1919
import html
20-
import math
2120
import os
2221
from collections import Counter, defaultdict
2322
from enum import auto
@@ -177,9 +176,10 @@ def __init__(self,
177176
filename,
178177
trajectory_format: ValidFormats | None = None,
179178
hdf5_driver: str | None = None,
179+
*,
180180
rdcc_nbytes: int | None = None,
181-
rdcc_w0: float | None = None,
182181
rdcc_nslots: int | None = None,
182+
rdcc_w0: float | None = None,
183183
fast_load: bool = False):
184184
self._filename = filename
185185
self._hdf5_driver = hdf5_driver
@@ -192,9 +192,9 @@ def __init__(self,
192192

193193
self._trajectory = self.open_trajectory(self._format,
194194
self._hdf5_driver,
195-
self._rdcc_nbytes,
196-
self._rdcc_w0,
197-
self._rdcc_nslots,
195+
rdcc_nbytes=self._rdcc_nbytes,
196+
rdcc_nslots=self._rdcc_nslots,
197+
rdcc_w0=self._rdcc_w0,
198198
fast_load = fast_load
199199
)
200200
self._min_span = None
@@ -469,9 +469,10 @@ def guess_correct_format(self) -> ValidFormats:
469469
def open_trajectory(self,
470470
trajectory_format,
471471
hdf5_driver,
472-
rdcc_nbytes,
473-
rdcc_w0,
474-
rdcc_nslots,
472+
*,
473+
rdcc_nbytes: int | None = None,
474+
rdcc_nslots: int | None = None,
475+
rdcc_w0: float | None = None,
475476
fast_load: bool = False
476477
):
477478
trajectory_class = available_formats[trajectory_format]
@@ -510,7 +511,12 @@ def __getstate__(self):
510511

511512
def __setstate__(self, state):
512513
self.__dict__ = state
513-
self._trajectory = self.open_trajectory(self._format, self._hdf5_driver, self._rdcc_nbytes, self._rdcc_w0, self._rdcc_nslots, fast_load=True)
514+
self._trajectory = self.open_trajectory(self._format,
515+
self._hdf5_driver,
516+
rdcc_nbytes=self._rdcc_nbytes,
517+
rdcc_nslots=self._rdcc_nslots,
518+
rdcc_w0=self._rdcc_w0,
519+
fast_load=True)
514520

515521
def __len__(self):
516522
return len(self._trajectory)
@@ -1026,10 +1032,12 @@ def __init__(
10261032
chemical_system: ChemicalSystem,
10271033
n_steps,
10281034
selected_atoms=None,
1035+
*,
10291036
positions_dtype=np.float64,
10301037
chunking_limit=(1,128),
10311038
compression="none",
10321039
initial_charges=None,
1040+
meta_block_size: int = 65536
10331041
):
10341042
"""Constructor.
10351043
@@ -1044,7 +1052,7 @@ def __init__(
10441052
"""
10451053
self._h5_filename = Path(h5_filename)
10461054
PLATFORM.create_directory(self._h5_filename.parent)
1047-
self._h5_file = h5py.File(self._h5_filename, "w", meta_block_size=65536)
1055+
self._h5_file = h5py.File(self._h5_filename, "w", meta_block_size=meta_block_size)
10481056

10491057
self._chemical_system = chemical_system
10501058
self._last_configuration = None

MDANSE/Src/MDANSE/MolecularDynamics/TrajectoryUtils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
#
1616
from __future__ import annotations
1717

18-
from functools import reduce
1918
from itertools import pairwise
2019
from typing import TYPE_CHECKING
2120

2221
import numpy as np
23-
from more_itertools import chunked_even
22+
from more_itertools import chunked_even, flatten
2423

2524
if TYPE_CHECKING:
2625
from MDANSE.Chemistry.ChemicalSystem import ChemicalSystem
@@ -130,7 +129,7 @@ def group_cluster_indices(chemical_system: ChemicalSystem) -> list[list[int]]:
130129
list[list[int]]
131130
A list of atom index lists, one per molecule instance.
132131
"""
133-
return reduce(list.__add__, chemical_system.clusters.values(), [])
132+
return list(flatten(chemical_system.clusters.values()))
134133

135134

136135
def group_atom_indices(

MDANSE/Src/MDANSE/Trajectory/H5MDTrajectory.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717

1818
from collections import ChainMap
1919
from enum import Enum
20-
from functools import reduce
2120
from pathlib import Path
2221
from typing import TYPE_CHECKING
2322

2423
import h5py
24+
import more_itertools
2525
import numpy as np
2626
import numpy.typing as npt
2727

@@ -135,9 +135,10 @@ def __init__(
135135
self,
136136
h5_filename: Path | str,
137137
hdf5_driver: str | None = None,
138+
*,
138139
rdcc_nbytes: int | None = None,
139-
rdcc_w0: float | None = None,
140140
rdcc_nslots: int | None = None,
141+
rdcc_w0: float | None = None,
141142
fast_load: bool = False,
142143
):
143144
"""Constructor.
@@ -158,8 +159,8 @@ def __init__(
158159
"r",
159160
driver=hdf5_driver,
160161
rdcc_nbytes=rdcc_nbytes,
161-
rdcc_w0=rdcc_w0,
162162
rdcc_nslots=rdcc_nslots,
163+
rdcc_w0=rdcc_w0,
163164
)
164165

165166
particle_types = self._h5_file["/particles/all/species"]
@@ -204,8 +205,8 @@ def __init__(
204205

205206
if self._chemical_system.rdkit_mol.GetNumBonds() > 0:
206207
configuration = self.configuration(0)
207-
grouped_indices = reduce(
208-
list.__add__, self._chemical_system.clusters.values(), []
208+
grouped_indices = list(
209+
more_itertools.flatten(self._chemical_system.clusters.values())
209210
)
210211
contiguous_configuration = configuration.contiguous_configuration(
211212
grouped_indices

MDANSE/Src/MDANSE/Trajectory/MdanseTrajectory.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
#
1616
from __future__ import annotations
1717

18-
from collections import ChainMap, defaultdict
19-
from collections.abc import Iterable, Mapping
20-
from functools import cached_property, reduce
18+
from collections import ChainMap
19+
from collections.abc import Mapping
20+
from functools import cached_property
2121
from pathlib import Path
2222

2323
import h5py
2424
import numpy as np
2525
import numpy.typing as npt
26-
from more_itertools import first
26+
from more_itertools import first, flatten
2727

2828
import MDANSE
2929
from MDANSE.Chemistry import ATOMS_DATABASE
@@ -65,9 +65,10 @@ def __init__(
6565
self,
6666
h5_filename: Path | str,
6767
hdf5_driver: str | None = None,
68+
*,
6869
rdcc_nbytes: int | None = None,
69-
rdcc_w0: float | None = None,
7070
rdcc_nslots: int | None = None,
71+
rdcc_w0: float | None = None,
7172
fast_load: bool = False,
7273
):
7374
"""Open the file and build a trajectory.
@@ -94,8 +95,8 @@ def __init__(
9495
"r",
9596
driver=hdf5_driver,
9697
rdcc_nbytes=rdcc_nbytes,
97-
rdcc_w0=rdcc_w0,
9898
rdcc_nslots=rdcc_nslots,
99+
rdcc_w0=rdcc_w0,
99100
)
100101
self._has_database = "atom_database" in self._h5_file
101102
self._has_atoms = []
@@ -113,9 +114,7 @@ def __init__(
113114

114115
if self._chemical_system.rdkit_mol.GetNumBonds() > 0:
115116
configuration = self.configuration(0)
116-
grouped_indices = reduce(
117-
list.__add__, self._chemical_system.clusters.values(), []
118-
)
117+
grouped_indices = list(flatten(self._chemical_system.clusters.values()))
119118
contiguous_configuration = configuration.contiguous_configuration(
120119
grouped_indices
121120
)

MDANSE_GUI/Src/MDANSE_GUI/InputWidgets/HDFTrajectoryWidget.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,11 @@ def get_widget_value(self):
162162
else:
163163
self._label.setToolTip(self._tooltip)
164164
hdf5_driver = self._driver_widget.currentText()
165-
hdf5_driver = None if hdf5_driver == "default" else hdf5_driver
166165
rdcc = {}
167166
for name, widget in self._extra_widgets.items():
168167
rdcc[name] = widget.value() if widget.isEnabled() else None
169168
return (
170-
result,
169+
str(result),
171170
hdf5_driver,
172171
rdcc["rdcc_nbytes"] * 1024**2 if rdcc["rdcc_nbytes"] is not None else None,
173172
rdcc["rdcc_nslots"],

MDANSE_GUI/Src/MDANSE_GUI/InputWidgets/OutputTrajectoryWidget.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ def __init__(self, *args, **kwargs):
6969
self.chunk_atom_box = QSpinBox(self._base)
7070
self.chunk_frame_box = QSpinBox(self._base)
7171
for typ, chunk_box in zip(
72-
("atoms", "frames"),
73-
(self.chunk_atom_box, self.chunk_frame_box)
72+
("atoms", "frames"),
73+
(self.chunk_atom_box, self.chunk_frame_box),
74+
strict=True,
7475
):
7576
chunk_box.setMinimum(1)
7677
chunk_box.setMaximum(0xFFFF)

0 commit comments

Comments
 (0)