Skip to content

Commit

Permalink
Rename REs (#181)
Browse files Browse the repository at this point in the history
Improve abbreviated RE names.

Fixes #180
  • Loading branch information
oerc0122 authored Dec 5, 2024
1 parent 208155d commit 9fe6e0c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 26 deletions.
10 changes: 5 additions & 5 deletions castep_outputs/parsers/castep_file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,7 @@ def _process_atreg_block(block: Block) -> AtomPropBlock:
return {
atreg_to_index(match): to_type(match.group("x", "y", "z"), float)
for line in block
if (match := REs.ATDAT3VEC.search(line))
if (match := REs.ATOMIC_DATA_3VEC.search(line))
}


Expand Down Expand Up @@ -1471,7 +1471,7 @@ def _process_hirshfeld(block: Block) -> dict[AtomIndex, float]:
"""Process Hirshfeld block to dict of charges."""
return {
atreg_to_index(match): float(match["charge"]) for line in block
if (match := re.match(rf"\s+{REs.ATREG}\s+(?P<charge>{REs.FNUMBER_RE})", line))
if (match := re.match(rf"\s+{REs.ATOM_RE}\s+(?P<charge>{REs.FNUMBER_RE})", line))
}


Expand Down Expand Up @@ -1684,7 +1684,7 @@ def _process_forces(block: Block) -> tuple[str, AtomPropBlock]:

accum = {atreg_to_index(match): to_type(match.group("x", "y", "z"), float)
for line in block
if (match := REs.FORCES_ATDAT.search(line))}
if (match := REs.ATOMIC_DATA_FORCE.search(line))}

return ftype, accum

Expand Down Expand Up @@ -1712,7 +1712,7 @@ def _process_initial_spins(block: Block) -> dict[AtomIndex, InitialSpin]:
"""Process a set of initial spins into appropriate dict."""
accum: dict[AtomIndex, InitialSpin] = {}
for line in block:
if match := re.match(rf"\s*\|\s*{REs.ATREG}\s*"
if match := re.match(rf"\s*\|\s*{REs.ATOM_RE}\s*"
rf"{labelled_floats(('spin', 'magmom'))}\s*"
r"(?P<fix>[TF])\s*\|", line):
val = match.groupdict()
Expand Down Expand Up @@ -1987,7 +1987,7 @@ def _process_symmetry(block: Block) -> tuple[SymmetryReport, ConstraintsReport]:

elif cons_block := Block.from_re(line, block, r"constraints\.{5}", r"\s*x+\.{4}\s*"):
con["ionic_constraints"] = defaultdict(list)
for match in re.finditer(rf"{REs.ATREG}\s*[xyz]\s*" +
for match in re.finditer(rf"{REs.ATOM_RE}\s*[xyz]\s*" +
labelled_floats(("pos",), counts=(3,)),
str(cons_block)):
val = match.groupdict()
Expand Down
2 changes: 1 addition & 1 deletion castep_outputs/parsers/cell_param_file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def _parse_nonlinear_constraints(block: Block) -> dict[AtomIndex, ThreeVector]:
key, line = line.split(maxsplit=1)
cons = {"key": key,
"atoms": {atreg_to_index(match): to_type(match.group("x", "y", "z"), int)
for match in REs.ATDAT3VEC.finditer(line)}}
for match in REs.ATOMIC_DATA_3VEC.finditer(line)}}
accum.append(cons)

return accum
Expand Down
4 changes: 2 additions & 2 deletions castep_outputs/parsers/md_geom_file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections import defaultdict
from typing import TextIO, TypedDict

from ..utilities.castep_res import ATDATTAG, TAG_RE, get_numbers
from ..utilities.castep_res import ATOMIC_DATA_TAG, TAG_RE, get_numbers
from ..utilities.constants import FST_D, TAG_ALIASES
from ..utilities.datatypes import AtomIndex, ThreeByThreeMatrix, ThreeVector
from ..utilities.utility import add_aliases, atreg_to_index, to_type
Expand Down Expand Up @@ -99,7 +99,7 @@ def parse_md_geom_file(md_geom_file: TextIO) -> list[MDGeomTimestepInfo]:
elif not TAG_RE.search(line): # Timestep
curr["time"] = to_type(get_numbers(line)[0], float)

elif match := ATDATTAG.match(line):
elif match := ATOMIC_DATA_TAG.match(line):
ion = atreg_to_index(match)
if ion not in curr["ions"]:
curr["ions"][ion] = {}
Expand Down
2 changes: 1 addition & 1 deletion castep_outputs/parsers/parse_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def parse_regular_header(block: Block,
data["unit_cell"] = [to_type(next(block).split(), float)
for _ in range(3)]

elif match := REs.ATDAT3VEC.search(line):
elif match := REs.ATOMIC_DATA_3VEC.search(line):
ind = atreg_to_index(match)
coords[ind] = to_type(match.group("x", "y", "z"), float)

Expand Down
4 changes: 2 additions & 2 deletions castep_outputs/parsers/ts_file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections import defaultdict
from typing import TextIO, TypedDict

from ..utilities.castep_res import ATDATTAG, TAG_RE, get_numbers, labelled_floats
from ..utilities.castep_res import ATOMIC_DATA_TAG, TAG_RE, get_numbers, labelled_floats
from ..utilities.constants import FST_D, TAG_ALIASES, TS_TYPES
from ..utilities.datatypes import ThreeByThreeMatrix
from ..utilities.filewrapper import Block
Expand Down Expand Up @@ -72,7 +72,7 @@ def parse_ts_file(ts_file: TextIO) -> TSFileInfo:
curr["reaction_coordinate"] = to_type(match["reaction_coordinate"], float)

for blk_line in block:
if match := ATDATTAG.search(blk_line):
if match := ATOMIC_DATA_TAG.search(blk_line):
ion = atreg_to_index(match)
if ion not in curr:
curr[ion] = {}
Expand Down
30 changes: 15 additions & 15 deletions castep_outputs/utilities/castep_res.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,19 @@ def get_atom_parts(spec: str) -> dict[str, str]:
#: CASTEP Atom RegEx with optional index.
#:
#: :meta hide-value:
ATREG = rf"(?P<spec>{ATOM_NAME_RE})\s+(?P<index>\d+)"
ATOM_RE = rf"(?P<spec>{ATOM_NAME_RE})\s+(?P<index>\d+)"


# Atom reference with 3-vector
#: CASTEP atom followed by 3D vector.
#:
#: :meta hide-value:
ATDAT3VEC = re.compile(ATREG + labelled_floats(FST_D))
FORCES_ATDAT = re.compile(ATREG + labelled_floats(FST_D, suffix=r"(?:\s*\([^)]+\))?"))
ATDATTAG = re.compile(rf"\s*{ATDAT3VEC.pattern}\s*{TAG_RE.pattern}")
ATOMIC_DATA_3VEC = re.compile(ATOM_RE + labelled_floats(FST_D))
ATOMIC_DATA_FORCE = re.compile(ATOM_RE + labelled_floats(FST_D, suffix=r"(?:\s*\([^)]+\))?"))
ATOMIC_DATA_TAG = re.compile(rf"\s*{ATOMIC_DATA_3VEC.pattern}\s*{TAG_RE.pattern}")

# Labelled positions
LABELLED_POS_RE = re.compile(ATDAT3VEC.pattern + r"\s{5}(?P<label>\S{0,8})")
LABELLED_POS_RE = re.compile(ATOMIC_DATA_3VEC.pattern + r"\s{5}(?P<label>\S{0,8})")

# VCA atoms
MIXTURE_LINE_1_RE = re.compile(rf"""
Expand Down Expand Up @@ -391,7 +391,7 @@ def get_atom_parts(spec: str) -> dict[str, str]:
}

# Orbital population
ORBITAL_POPN_RE = re.compile(rf"\s*{ATREG}\s*(?P<orb>[SPDF][xyz]?)"
ORBITAL_POPN_RE = re.compile(rf"\s*{ATOM_RE}\s*(?P<orb>[SPDF][xyz]?)"
rf"\s*{labelled_floats(('charge',))}")

# Regexp to identify phonon block in .castep file
Expand Down Expand Up @@ -439,7 +439,7 @@ def get_atom_parts(spec: str) -> dict[str, str]:

THERMODYNAMICS_DATA_RE = re.compile(labelled_floats(("t", "e", "f", "s", "cv")))
ATOMIC_DISP_RE = re.compile(labelled_floats(("temperature",)) + r"\s*" +
ATREG + r"\s*" +
ATOM_RE + r"\s*" +
labelled_floats(("displacement",), counts=(6,)))

MINIMISERS_RE = f"(?:{'|'.join(x.upper() for x in MINIMISERS)})"
Expand All @@ -455,7 +455,7 @@ def get_atom_parts(spec: str) -> dict[str, str]:


# Regexp to identify Mulliken ppoulation analysis line
POPN_RE = re.compile(rf"\s*{ATREG}\s*(?P<spin_sep>up:)?" +
POPN_RE = re.compile(rf"\s*{ATOM_RE}\s*(?P<spin_sep>up:)?" +
labelled_floats((*SHELLS, "total", "charge", "spin")) +
"?", # Spin is optional
)
Expand All @@ -465,24 +465,24 @@ def get_atom_parts(spec: str) -> dict[str, str]:
)

# Regexp for born charges
BORN_RE = re.compile(rf"\s+{ATREG}(?P<charges>(?:\s*{FNUMBER_RE}){{3}})(?:\s*ID=(?P<label>\S+))?")
BORN_RE = re.compile(rf"\s+{ATOM_RE}(?P<charges>(?:\s*{FNUMBER_RE}){{3}})(?:\s*ID=(?P<label>\S+))?")

# MagRes REs
MAGRES_RE = (
# "Chemical Shielding Tensor" 0
re.compile(rf"\s*\|\s*{ATREG}{labelled_floats(('iso','aniso'))}\s*"
re.compile(rf"\s*\|\s*{ATOM_RE}{labelled_floats(('iso','aniso'))}\s*"
rf"(?P<asym>{FNUMBER_RE}|N/A)\s*\|\s*"),
# "Chemical Shielding and Electric Field Gradient Tensor" 1
re.compile(rf"\s*\|\s*{ATREG}{labelled_floats(('iso','aniso'))}\s*"
re.compile(rf"\s*\|\s*{ATOM_RE}{labelled_floats(('iso','aniso'))}\s*"
rf"(?P<asym>{FNUMBER_RE}|N/A)"
rf"{labelled_floats(('cq', 'eta'))}\s*\|\s*"),
# "Electric Field Gradient Tensor" 2
re.compile(rf"\s*\|\s*{ATREG}{labelled_floats(('cq',))}\s*"
re.compile(rf"\s*\|\s*{ATOM_RE}{labelled_floats(('cq',))}\s*"
rf"(?P<asym>{FNUMBER_RE}|N/A)\s*\|\s*"),
# "(?:I|Ani)sotropic J-coupling" 3
re.compile(rf"\s*\|\**\s*{ATREG}{labelled_floats(('fc','sd','para','dia','tot'))}\s*\|\s*"),
re.compile(rf"\s*\|\**\s*{ATOM_RE}{labelled_floats(('fc','sd','para','dia','tot'))}\s*\|\s*"),
# "Hyperfine Tensor" 4
re.compile(rf"\s*\|\s*{ATREG}{labelled_floats(('iso',))}\s*\|\s*"),
re.compile(rf"\s*\|\s*{ATOM_RE}{labelled_floats(('iso',))}\s*\|\s*"),
)

# MagRes Tasks
Expand Down Expand Up @@ -526,7 +526,7 @@ def get_atom_parts(spec: str) -> dict[str, str]:
\s*$
""", re.IGNORECASE | re.VERBOSE)

IONIC_CONSTRAINTS_RE = re.compile(rf"^\s*\d\s+{ATREG}{THREEVEC_RE}")
IONIC_CONSTRAINTS_RE = re.compile(rf"^\s*\d\s+{ATOM_RE}{THREEVEC_RE}")
POSITIONS_LINE_RE = re.compile(rf"^\s*(?P<spec>{SPECIES_RE})"
rf"(?P<pos>(?:\s+{FLOAT_RAT_RE.pattern}){{3}})")
POSITIONS_SPIN_RE = re.compile(rf"(?:spin|magmom)\s*[= :\t]\s*(?P<spin>{FLOAT_RAT_RE.pattern})",
Expand Down

0 comments on commit 9fe6e0c

Please sign in to comment.