|
| 1 | +from types import SimpleNamespace |
| 2 | +from unittest.mock import MagicMock |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import pytest |
| 6 | + |
| 7 | +from CodeEntropy.entropy.configurational import ConformationalEntropy |
| 8 | + |
| 9 | + |
| 10 | +def test_find_histogram_peaks_empty_histogram_returns_empty(): |
| 11 | + ce = ConformationalEntropy() |
| 12 | + phi = np.zeros(100, dtype=float) |
| 13 | + |
| 14 | + peaks = ce._find_histogram_peaks(phi, bin_width=30) |
| 15 | + |
| 16 | + assert isinstance(peaks, np.ndarray) |
| 17 | + assert peaks.dtype == float |
| 18 | + |
| 19 | + |
| 20 | +def test_find_histogram_peaks_returns_empty_for_empty_phi(): |
| 21 | + ce = ConformationalEntropy() |
| 22 | + phi = np.array([], dtype=float) |
| 23 | + |
| 24 | + peaks = ce._find_histogram_peaks(phi, bin_width=30) |
| 25 | + |
| 26 | + assert isinstance(peaks, np.ndarray) |
| 27 | + assert peaks.size == 0 |
| 28 | + |
| 29 | + |
| 30 | +def test_assign_nearest_peaks_with_single_peak_assigns_all_zero(): |
| 31 | + ce = ConformationalEntropy() |
| 32 | + phi = np.array([0.0, 10.0, 20.0], dtype=float) |
| 33 | + peak_values = np.array([15.0], dtype=float) |
| 34 | + |
| 35 | + states = ce._assign_nearest_peaks(phi, peak_values) |
| 36 | + |
| 37 | + assert np.all(states == 0) |
| 38 | + |
| 39 | + |
| 40 | +def test_assign_conformation_no_peaks_returns_all_zero(): |
| 41 | + ce = ConformationalEntropy() |
| 42 | + |
| 43 | + data_container = SimpleNamespace(trajectory=[]) |
| 44 | + dihedral = MagicMock() |
| 45 | + |
| 46 | + states = ce.assign_conformation( |
| 47 | + data_container=data_container, |
| 48 | + dihedral=dihedral, |
| 49 | + number_frames=0, |
| 50 | + bin_width=30, |
| 51 | + start=0, |
| 52 | + end=0, |
| 53 | + step=1, |
| 54 | + ) |
| 55 | + |
| 56 | + assert states.size == 0 |
| 57 | + |
| 58 | + |
| 59 | +def test_assign_conformation_fallback_when_peak_finder_returns_empty(monkeypatch): |
| 60 | + ce = ConformationalEntropy() |
| 61 | + data_container = SimpleNamespace(trajectory=list(range(5))) |
| 62 | + dihedral = MagicMock() |
| 63 | + dihedral.value.return_value = 10.0 |
| 64 | + |
| 65 | + monkeypatch.setattr( |
| 66 | + ce, "_find_histogram_peaks", lambda phi, bw: np.array([], dtype=float) |
| 67 | + ) |
| 68 | + |
| 69 | + states = ce.assign_conformation( |
| 70 | + data_container=data_container, |
| 71 | + dihedral=dihedral, |
| 72 | + number_frames=5, |
| 73 | + bin_width=30, |
| 74 | + start=0, |
| 75 | + end=5, |
| 76 | + step=1, |
| 77 | + ) |
| 78 | + assert np.all(states == 0) |
| 79 | + |
| 80 | + |
| 81 | +def test_assign_conformation_detects_multiple_states(): |
| 82 | + ce = ConformationalEntropy() |
| 83 | + |
| 84 | + values = [0.0] * 50 + [180.0] * 50 |
| 85 | + data_container = SimpleNamespace(trajectory=list(range(len(values)))) |
| 86 | + dihedral = MagicMock() |
| 87 | + dihedral.value.side_effect = values |
| 88 | + |
| 89 | + states = ce.assign_conformation( |
| 90 | + data_container=data_container, |
| 91 | + dihedral=dihedral, |
| 92 | + number_frames=len(values), |
| 93 | + bin_width=30, |
| 94 | + start=0, |
| 95 | + end=len(values), |
| 96 | + step=1, |
| 97 | + ) |
| 98 | + |
| 99 | + assert len(np.unique(states)) >= 2 |
| 100 | + |
| 101 | + |
| 102 | +def test_conformational_entropy_empty_returns_zero(): |
| 103 | + ce = ConformationalEntropy() |
| 104 | + assert ce.conformational_entropy_calculation([], number_frames=10) == 0.0 |
| 105 | + |
| 106 | + |
| 107 | +def test_conformational_entropy_single_state_returns_zero(): |
| 108 | + ce = ConformationalEntropy() |
| 109 | + assert ce.conformational_entropy_calculation([0, 0, 0], number_frames=3) == 0.0 |
| 110 | + |
| 111 | + |
| 112 | +def test_conformational_entropy_known_distribution_matches_expected(): |
| 113 | + ce = ConformationalEntropy() |
| 114 | + states = np.array([0, 0, 1, 1, 1, 2]) |
| 115 | + |
| 116 | + probs = np.array([2 / 6, 3 / 6, 1 / 6], dtype=float) |
| 117 | + expected = -ce._GAS_CONST * float(np.sum(probs * np.log(probs))) |
| 118 | + |
| 119 | + got = ce.conformational_entropy_calculation(states, number_frames=6) |
| 120 | + assert got == pytest.approx(expected) |
0 commit comments