Skip to content

Commit

Permalink
added convolution, naming: read->load
Browse files Browse the repository at this point in the history
  • Loading branch information
wolearyc committed Aug 3, 2024
1 parent 3bb747b commit 15af2f8
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 27 deletions.
2 changes: 1 addition & 1 deletion docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ API
.. autosummary::
:toctree: generated

ramannoodle.io.vasp
ramannoodle.io
ramannoodle.dynamics
ramannoodle.polarizability
ramannoodle.spectrum
Expand Down
13 changes: 13 additions & 0 deletions docs/source/generated/ramannoodle.io.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ramannoodle.io
==============

.. automodule:: ramannoodle.io


.. rubric:: Functions

.. autosummary::

read_phonons
read_positions_and_polarizability
read_structural_symmetry
6 changes: 3 additions & 3 deletions docs/source/generated/ramannoodle.io.vasp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

.. autosummary::

load_phonons_from_outcar
load_positions_and_polarizability_from_outcar
load_structural_symmetry_from_outcar
read_phonons_from_outcar
read_positions_and_polarizability_from_outcar
read_structural_symmetry_from_outcar
1 change: 0 additions & 1 deletion docs/source/generated/ramannoodle.polarizability.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@

.. autosummary::

InterpolationPolarizabilityModel
PolarizabilityModel
10 changes: 10 additions & 0 deletions docs/source/ramannoodle.polarizability.interpolation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ramannoodle.polarizability.interpolation package
================================================

Module contents
---------------

.. automodule:: ramannoodle.polarizability.interpolation
:members:
:undoc-members:
:show-inheritance:
18 changes: 9 additions & 9 deletions ramannoodle/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
from . import vasp

# These dictionaries map between file_format's and appropriate loading functions.
_PHONON_LOADERS = {"outcar": vasp.load_phonons_from_outcar}
_PHONON_LOADERS = {"outcar": vasp.read_phonons_from_outcar}
_POSITION_AND_POLARIZABILITY_LOADERS = {
"outcar": vasp.load_positions_and_polarizability_from_outcar
"outcar": vasp.read_positions_and_polarizability_from_outcar
}
_STRUCTURAL_SYMMETRY_LOADERS = {"outcar": vasp.load_structural_symmetry_from_outcar}
_STRUCTURAL_SYMMETRY_LOADERS = {"outcar": vasp.read_structural_symmetry_from_outcar}


def load_phonons(filepath: str | Path, file_format: str) -> Phonons:
"""Extract phonons from a file.
def read_phonons(filepath: str | Path, file_format: str) -> Phonons:
"""Read phonons from a file.
Parameters
----------
Expand All @@ -39,11 +39,11 @@ def load_phonons(filepath: str | Path, file_format: str) -> Phonons:
raise ValueError(f"unsupported format: {file_format}") from exc


def load_positions_and_polarizability(
def read_positions_and_polarizability(
filepath: str | Path,
file_format: str,
) -> tuple[NDArray[np.float64], NDArray[np.float64]]:
"""Extract fractional positions and polarizability from a file.
"""Read fractional positions and polarizability from a file.
Parameters
----------
Expand All @@ -67,10 +67,10 @@ def load_positions_and_polarizability(
raise ValueError(f"unsupported format: {file_format}") from exc


def load_structural_symmetry(
def read_structural_symmetry(
filepath: str | Path, file_format: str
) -> StructuralSymmetry:
"""Extract structural symmetry from a file.
"""Read structural symmetry from a file.
Parameters
----------
Expand Down
6 changes: 3 additions & 3 deletions ramannoodle/io/vasp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from ...exceptions import NoMatchingLineFoundException, InvalidFileException


def load_phonons_from_outcar(filepath: str | Path) -> Phonons:
def read_phonons_from_outcar(filepath: str | Path) -> Phonons:
"""Extract phonons from a VASP OUTCAR file.
Parameters
Expand Down Expand Up @@ -76,7 +76,7 @@ def load_phonons_from_outcar(filepath: str | Path) -> Phonons:
return Phonons(wavenumbers, cartesian_displacements)


def load_positions_and_polarizability_from_outcar(
def read_positions_and_polarizability_from_outcar(
filepath: str | Path,
) -> tuple[NDArray[np.float64], NDArray[np.float64]]:
"""Extract fractional positions and polarizability from a VASP OUTCAR file.
Expand Down Expand Up @@ -107,7 +107,7 @@ def load_positions_and_polarizability_from_outcar(
return positions, polarizability


def load_structural_symmetry_from_outcar(
def read_structural_symmetry_from_outcar(
filepath: str | Path,
) -> StructuralSymmetry:
"""Extract structural symmetry from a VASP OUTCAR file.
Expand Down
2 changes: 1 addition & 1 deletion ramannoodle/polarizability/interpolation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def add_dof_from_files(
polarizabilities = []
filepaths = pathify_as_list(filepaths)
for filepath in filepaths:
fractional_positions, polarizability = io.load_positions_and_polarizability(
fractional_positions, polarizability = io.read_positions_and_polarizability(
filepath, file_format
)
displacement = calculate_displacement(
Expand Down
37 changes: 37 additions & 0 deletions ramannoodle/spectrum/spectrum_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,40 @@ def get_laser_correction(
"""
return ((wavenumbers - laser_wavenumber) / 10000) ** 4 / wavenumbers


def convolve_intensities(
wavenumbers: NDArray[np.float64],
intensities: NDArray[np.float64],
function: str = "gaussian",
width: float = 5,
out_wavenumbers: NDArray[np.float64] | None = None,
) -> tuple[NDArray[np.float64], NDArray[np.float64]]:
"""Convolves and smooths intensities."""
if out_wavenumbers is None:
out_wavenumbers = np.linspace(np.min(wavenumbers), np.max(wavenumbers), 1000)

out_wavenumbers = np.array(out_wavenumbers)
convolved_intensities = out_wavenumbers * 0
for wavenumber, intensity in zip(wavenumbers, intensities):
factor = 0
if function == "gaussian":
factor = (
1
/ width
* 1
/ np.sqrt(2 * np.pi)
* np.exp(-((wavenumber - out_wavenumbers) ** 2) / (2 * width**2))
)
elif function == "lorentzian":
factor = (
1
/ np.pi
* 0.5
* width
/ ((wavenumber - out_wavenumbers) ** 2 + (0.5 * width) ** 2)
)
else:
raise ValueError(f"unsupported convolution type: {type}")
convolved_intensities += factor * intensity
return (out_wavenumbers, convolved_intensities)
8 changes: 4 additions & 4 deletions test/tests/test_polarizability.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from ramannoodle.polarizability.polarizability_utils import find_duplicates
from ramannoodle.polarizability.interpolation import InterpolationPolarizabilityModel
from ramannoodle.io.vasp import load_structural_symmetry_from_outcar
from ramannoodle.io.vasp import read_structural_symmetry_from_outcar
from ramannoodle.exceptions import InvalidDOFException

# pylint: disable=protected-access
Expand Down Expand Up @@ -42,7 +42,7 @@ def test_add_dof(
known_dof_added: int,
) -> None:
"""Test."""
symmetry = load_structural_symmetry_from_outcar(outcar_path_fixture)
symmetry = read_structural_symmetry_from_outcar(outcar_path_fixture)
model = InterpolationPolarizabilityModel(symmetry, np.zeros((3, 3)))
displacement = symmetry._fractional_positions * 0
displacement[displaced_atom_index][0] = 1.0
Expand All @@ -66,7 +66,7 @@ def test_overspecified_dof(
amplitudes: NDArray[np.float64],
) -> None:
"""Test."""
symmetry = load_structural_symmetry_from_outcar(outcar_path_fixture)
symmetry = read_structural_symmetry_from_outcar(outcar_path_fixture)
model = InterpolationPolarizabilityModel(symmetry, np.zeros((3, 3)))
displacement = symmetry._fractional_positions * 0
displacement[displaced_atom_index][0] = 1.0
Expand All @@ -79,7 +79,7 @@ def test_overspecified_dof(

def test_get_polarizability() -> None:
"""Test."""
symmetry = load_structural_symmetry_from_outcar(
symmetry = read_structural_symmetry_from_outcar(
Path("test/data/TiO2/PHONON_OUTCAR")
)
model = InterpolationPolarizabilityModel(symmetry, np.diag([2, 2, 2]))
Expand Down
4 changes: 2 additions & 2 deletions test/tests/test_symmetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
is_orthogonal_to_all,
get_fractional_positions_permutation_matrix,
)
from ramannoodle.io.vasp import load_structural_symmetry_from_outcar
from ramannoodle.io.vasp import read_structural_symmetry_from_outcar


@pytest.mark.parametrize(
Expand Down Expand Up @@ -67,7 +67,7 @@ def test_structural_symmetry(
) -> None:
"""Test."""
# Equivalent atoms test
symmetry = load_structural_symmetry_from_outcar(outcar_path_fixture)
symmetry = read_structural_symmetry_from_outcar(outcar_path_fixture)
assert symmetry.get_num_nonequivalent_atoms() == known_nonequivalent_atoms

# Equivalent displacement test
Expand Down
6 changes: 3 additions & 3 deletions test/tests/test_vasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest

from ramannoodle.io import (
load_phonons,
read_phonons,
)
from ramannoodle.globals import ATOMIC_WEIGHTS

Expand All @@ -28,15 +28,15 @@
],
indirect=["outcar_path_fixture"],
)
def test_load_phonons_from_outcar(
def test_read_phonons_from_outcar(
outcar_path_fixture: Path,
known_num_atoms: int,
known_wavenumbers: NDArray[np.float64],
known_first_displacement: NDArray[np.float64],
known_last_displacement: NDArray[np.float64],
) -> None:
"""Test."""
phonons = load_phonons(outcar_path_fixture, file_format="outcar")
phonons = read_phonons(outcar_path_fixture, file_format="outcar")

known_degrees_of_freedom = known_num_atoms * 3
assert phonons.get_wavenumbers().shape == (known_degrees_of_freedom,)
Expand Down

0 comments on commit 15af2f8

Please sign in to comment.