md-exafs 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
md_exafs/__init__.py ADDED
@@ -0,0 +1,56 @@
1
+ """MD-EXAFS Core Engine - Trajectory analysis and path-resolved EXAFS reconstruction."""
2
+
3
+ from pathlib import Path
4
+
5
+ from .constants import ETOK, HBAR2_OVER_2M_EV_ANGSTROM2
6
+ from .exafs_data import PathAggregator, PathContribution
7
+ from .feff_input import FeffConfig, build_feff_inp, normalize_tag
8
+ from .hdf5 import (
9
+ ArchiveReader,
10
+ BatchShardReader,
11
+ BatchShardWriter,
12
+ EnsembleReader,
13
+ EnsembleWriter,
14
+ ExafsHDF5Store,
15
+ )
16
+ from .paths import PathResult, make_path_key, path_chi
17
+ from .potentials import PotentialsManager
18
+ from .selection import (
19
+ parse_frame_slice,
20
+ resolve_frame_absorbers,
21
+ resolve_trajectory_absorbers,
22
+ )
23
+ from .spectra import average_chi_arrays, format_chi_ascii, xftf_arrays
24
+
25
+ __version__ = "0.2.0"
26
+
27
+ # Default cache directory for FEFF calculations and results
28
+ DEFAULT_CACHE_DIR = Path.home() / ".larch_cache"
29
+
30
+ __all__ = [
31
+ "__version__",
32
+ "DEFAULT_CACHE_DIR",
33
+ "HBAR2_OVER_2M_EV_ANGSTROM2",
34
+ "ETOK",
35
+ "FeffConfig",
36
+ "build_feff_inp",
37
+ "normalize_tag",
38
+ "PathResult",
39
+ "path_chi",
40
+ "make_path_key",
41
+ "xftf_arrays",
42
+ "average_chi_arrays",
43
+ "format_chi_ascii",
44
+ "resolve_frame_absorbers",
45
+ "resolve_trajectory_absorbers",
46
+ "parse_frame_slice",
47
+ "PotentialsManager",
48
+ "BatchShardWriter",
49
+ "BatchShardReader",
50
+ "EnsembleWriter",
51
+ "EnsembleReader",
52
+ "ArchiveReader",
53
+ "ExafsHDF5Store",
54
+ "PathAggregator",
55
+ "PathContribution",
56
+ ]
md_exafs/__main__.py ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env python3
2
+ """Entry point for the larch CLI wrapper."""
3
+
4
+ from larch_cli_wrapper.cli import app
5
+
6
+ if __name__ == "__main__":
7
+ app()
@@ -0,0 +1,84 @@
1
+ """Cache utilities for EXAFS processing pipeline."""
2
+
3
+ import hashlib
4
+ import pickle
5
+ import warnings
6
+ from pathlib import Path
7
+ from typing import Any
8
+
9
+ from ase import Atoms
10
+
11
+
12
+ def get_structure_hash(atoms: Atoms) -> str:
13
+ """Generate a hash for atomic structure for caching purposes."""
14
+ return hashlib.md5( # noqa: S324
15
+ str(atoms.get_positions()).encode() + str(atoms.get_chemical_symbols()).encode()
16
+ ).hexdigest()[:16]
17
+
18
+
19
+ def load_from_cache(
20
+ cache_key: str, cache_dir: str | Path | None, force_recalculate: bool = False
21
+ ) -> tuple[Any, Any] | None:
22
+ """Load cached results if available and not forcing recalculation.
23
+
24
+ Args:
25
+ cache_key: Unique identifier for the cached data
26
+ cache_dir: Directory where cache files are stored
27
+ force_recalculate: If True, ignore existing cache and return None
28
+
29
+ Returns:
30
+ Tuple of (k, chi) arrays if cache hit, None if cache miss or error
31
+
32
+ Note:
33
+ The returned chi array is complex (EXAFS chi(k) data) and k array
34
+ is real-valued k-space values.
35
+ """
36
+ if not cache_dir:
37
+ return None
38
+ cache_file = Path(cache_dir) / f"{cache_key}.pkl"
39
+ if cache_file.exists() and not force_recalculate:
40
+ try:
41
+ with open(cache_file, "rb") as f:
42
+ cached_data = pickle.load(f) # noqa: S301 - controlled usage
43
+ return cached_data["k"], cached_data["chi"]
44
+ except (EOFError, pickle.UnpicklingError, KeyError):
45
+ try:
46
+ cache_file.unlink()
47
+ except OSError:
48
+ # If we can't delete the cache file, just let the user know
49
+ warnings.warn(
50
+ f"Failed to delete cache file {cache_file}",
51
+ UserWarning,
52
+ stacklevel=2,
53
+ )
54
+ return None
55
+
56
+
57
+ def save_to_cache(
58
+ cache_key: str, chi: Any, k: Any, cache_dir: str | Path | None
59
+ ) -> None:
60
+ """Save processing results to cache for future use.
61
+
62
+ Args:
63
+ cache_key: Unique identifier for the cached data
64
+ chi: EXAFS chi(k) data (complex numpy array)
65
+ k: k-space values (real numpy array)
66
+ cache_dir: Directory where cache files are stored
67
+
68
+ Note:
69
+ Always stores complex chi data directly. Silently continues on cache
70
+ write failures with a warning.
71
+ """
72
+ if not cache_dir:
73
+ return
74
+ cache_file = Path(cache_dir) / f"{cache_key}.pkl"
75
+ cache_file.parent.mkdir(parents=True, exist_ok=True)
76
+ cached_data = {"chi": chi, "k": k}
77
+ try:
78
+ with open(cache_file, "wb") as f:
79
+ pickle.dump(cached_data, f)
80
+ except OSError:
81
+ # If we can't write to cache, continue without caching but inform the user
82
+ warnings.warn(
83
+ f"Failed to write cache file {cache_file}", UserWarning, stacklevel=2
84
+ )