Skip to content

Commit 0e22045

Browse files
authored
Merge pull request #3 from AdrianLipa90/codex/debug-and-fix-the-repository
Refine emotion utilities distribution normalisation
2 parents 0b97a33 + 35aa4c3 commit 0e22045

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

emotion/eeg_mapper.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
"""Translate EEG like traces into the emotional feature space."""
22

3+
from __future__ import annotations
4+
5+
from dataclasses import dataclass
6+
from typing import Iterable, List
7+
8+
from .utils import fractional_distribution, to_signal_list
9+
10+
11+
@dataclass(slots=True)
12+
class EEGEmotionMapper:
13+
bands: List[str] = None
14+
15+
def __post_init__(self) -> None:
16+
if self.bands is None:
17+
self.bands = ["delta", "theta", "alpha", "beta", "gamma"]
18+
19+
def map(self, signal: Iterable[float]) -> dict[str, float]:
20+
values = to_signal_list(signal)
21+
return fractional_distribution(values, self.bands)
22+
323
from ext.ext9 import EEGEmotionMapper
424

525
__all__ = ["EEGEmotionMapper"]

tests/test_emotional.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11

2+
import math
3+
4+
import pytest
5+
6+
27
def test_emotional_imports():
38
from emotion.emotion_core import EmotionCore
49
from emotion.eeg_mapper import EEGEmotionMapper
10+
511
assert EmotionCore is not None and EEGEmotionMapper is not None
12+
13+
14+
def test_emotion_core_tracks_variance():
15+
from emotion.emotion_core import EmotionCore
16+
17+
core = EmotionCore(baseline=0.5)
18+
result = core.process([0.0, 1.0, 2.0])
19+
20+
assert result["mood"] == pytest.approx(0.5 + (0.0 + 1.0 + 2.0) / 3.0)
21+
assert result["variance"] == pytest.approx(
22+
sum((value - result["mood"]) ** 2 for value in [0.0, 1.0, 2.0]) / 3.0
23+
)
24+
25+
26+
def test_fractional_distribution_normalises_output():
27+
from emotion.utils import fractional_distribution
28+
29+
distribution = fractional_distribution([1.0, 2.0], ["a", "b", "c"])
30+
31+
assert set(distribution) == {"a", "b", "c"}
32+
assert math.isclose(sum(distribution.values()), 1.0)
33+
assert distribution["b"] > distribution["a"]
34+
35+
36+
def test_fractional_distribution_handles_negative_values():
37+
from emotion.utils import fractional_distribution
38+
39+
distribution = fractional_distribution([-1.0, -3.0], ["alpha", "beta"])
40+
41+
assert distribution["beta"] == pytest.approx(0.75)

0 commit comments

Comments
 (0)