Skip to content

Commit

Permalink
Merge pull request #57 from SimoneMartino98/main
Browse files Browse the repository at this point in the history
@SimoneMartino98 added normalization options to compute_rdf function
  • Loading branch information
andrewtarzia authored Feb 5, 2025
2 parents 6616612 + 5d6cc9b commit 8170c44
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 32 deletions.
10 changes: 9 additions & 1 deletion src/dynsight/_internal/analysis/rdf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Literal

if TYPE_CHECKING:
import MDAnalysis
Expand All @@ -17,6 +17,7 @@ def compute_rdf(
s2: str = "all",
exclusion_block: list[int] | None = None,
nbins: int = 200,
norm: Literal["rdf", "density", "none"] = "rdf",
start: int | None = None,
stop: int | None = None,
step: int = 1,
Expand Down Expand Up @@ -52,6 +53,12 @@ def compute_rdf(
nbins:
The number of bins used to divide the distance range for
histogramming the RDF.
norm :
Type of normalization to apply:
- **'rdf'**: Standard RDF normalization (default).
- **'density'**: Normalize with respect to system density.
- **'none'**: No normalization applied.
start:
Initial molecular dynamics step.
stop:
Expand Down Expand Up @@ -106,6 +113,7 @@ def compute_rdf(
nbins=nbins,
range=distances_range,
exclusion_block=exclusion_block,
norm=norm,
)

rdf.run(verbose=True, start=start, stop=stop, step=step)
Expand Down
Empty file added tests/analysis/__init__.py
Empty file.
Empty file added tests/analysis/rdf/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions tests/analysis/rdf/case_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from dataclasses import dataclass
from typing import Literal


@dataclass(frozen=True, slots=True)
class RDFCaseData:
topology_filename: str
trajectory_filename: str
expected_bins: str
expected_rdf: str
norm: Literal["rdf", "density", "none"]
name: str
38 changes: 38 additions & 0 deletions tests/analysis/rdf/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import pytest

from .case_data import RDFCaseData


@pytest.fixture(
scope="session",
params=(
lambda name: RDFCaseData(
topology_filename="test_coex.gro",
trajectory_filename="test_coex.xtc",
expected_bins="test_bins_rdf.npy",
expected_rdf="test_rdf_rdf.npy",
norm="rdf",
name=name,
),
lambda name: RDFCaseData(
topology_filename="test_coex.gro",
trajectory_filename="test_coex.xtc",
expected_bins="test_bins_density.npy",
expected_rdf="test_rdf_density.npy",
norm="density",
name=name,
),
lambda name: RDFCaseData(
topology_filename="test_coex.gro",
trajectory_filename="test_coex.xtc",
expected_bins="test_bins_none.npy",
expected_rdf="test_rdf_none.npy",
norm="none",
name=name,
),
),
)
def case_data(request: pytest.FixtureRequest) -> RDFCaseData:
return request.param(
f"{request.fixturename}{request.param_index}", # type: ignore [attr-defined]
)
44 changes: 44 additions & 0 deletions tests/analysis/rdf/test_rdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Pytest for dynsight.analysis.compute_rdf."""

from pathlib import Path

import MDAnalysis
import numpy as np
import pytest

from dynsight.analysis import compute_rdf
from tests.analysis.rdf.case_data import RDFCaseData


def test_compute_rdf(case_data: RDFCaseData) -> None:
original_dir = Path(__file__).resolve().parent
topology_file = (
original_dir / "../../systems/coex" / case_data.topology_filename
)
trajectory_file = (
original_dir / "../../systems/coex" / case_data.trajectory_filename
)
expected_bins = original_dir / "test_rdf" / case_data.expected_bins
expected_rdf = original_dir / "test_rdf" / case_data.expected_rdf

u = MDAnalysis.Universe(topology_file, trajectory_file)

selection = "type O"

test_bins, test_rdf = compute_rdf(
universe=u,
s1=selection,
s2=selection,
distances_range=[0.0, 5.0],
norm=case_data.norm,
)

if not expected_bins.exists() or not expected_rdf.exists():
np.save(expected_bins, test_bins)
np.save(expected_rdf, test_rdf)
pytest.fail("RDF test files were not present. They have been created.")

exp_bins = np.load(expected_bins)
exp_rdf = np.load(expected_rdf)
assert np.array_equal(exp_rdf, test_rdf)
assert np.array_equal(exp_bins, test_bins)
File renamed without changes.
Binary file added tests/analysis/rdf/test_rdf/test_bins_none.npy
Binary file not shown.
Binary file added tests/analysis/rdf/test_rdf/test_bins_rdf.npy
Binary file not shown.
Binary file added tests/analysis/rdf/test_rdf/test_rdf_density.npy
Binary file not shown.
Binary file added tests/analysis/rdf/test_rdf/test_rdf_none.npy
Binary file not shown.
File renamed without changes.
31 changes: 0 additions & 31 deletions tests/analysis/test_rdf.py

This file was deleted.

Binary file modified tests/onion/output_multi/labels.npy
Binary file not shown.

0 comments on commit 8170c44

Please sign in to comment.