Skip to content

Commit

Permalink
removes one layer of mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
nivlekp committed Aug 18, 2024
1 parent 0a942fb commit 43c312e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 85 deletions.
39 changes: 4 additions & 35 deletions minamidera/library.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,16 @@
import dataclasses
import enum

import numpy.typing as npt

AXIS = enum.Enum("AXIS", ["FREQUENCY", "INTENSITY", "DENSITY"])
FREQUENCY_REGIONS = enum.Enum("FREQUENCY_REGIONS", ["F0", "F1"])
INTENSITY_REGIONS = enum.Enum("INTENSITY_REGIONS", ["G0", "G1"])
DENSITY_REGIONS = enum.Enum("DENSITY_REGIONS", ["D0", "D1"])
DURATION_REGIONS = enum.Enum("DURATION_REGIONS", ["L0", "L1"])


PITCHES_MAP = {
FREQUENCY_REGIONS.F0: {-6, -5, -4, 0, (1, 2), 3},
FREQUENCY_REGIONS.F1: {-32, -30, 24, 27},
}

INTENSITY_MAP = {INTENSITY_REGIONS.G0: {-1, 0}, INTENSITY_REGIONS.G1: {-2, 2}}

DENSITY_MAP = {DENSITY_REGIONS.D0: {0.7}, DENSITY_REGIONS.D1: {3.0}}

DURATION_MAP = {DURATION_REGIONS.L0: {0.3}, DURATION_REGIONS.L1: {1.0}}
PITCHES_SETS = [{-6, -5, -4, 0, (1, 2), 3}, {-32, -30, 24, 27}]

INTENSITY_SETS = [{-1, 0}, {-2, 2}]

@dataclasses.dataclass
class State:
frequency_region: FREQUENCY_REGIONS
intensity_region: INTENSITY_REGIONS
density_region: DENSITY_REGIONS
duration_region: DURATION_REGIONS
DENSITY_SETS = [{0.7}, {3.0}]

@classmethod
def from_state_vector(cls, state_vector: npt.NDArray) -> "State":
if len(dataclasses.fields(cls)) != len(state_vector):
raise ValueError(
f"The shape of the state vector {state_vector} does not match the shape of the state dataclass {dataclasses.fields(cls)}"
)
if any(state not in (0, 1) for state in state_vector):
raise ValueError(
f"The state vector {state_vector} contains value other than 0 and 1"
)
return cls(
FREQUENCY_REGIONS(state_vector[0] + 1),
INTENSITY_REGIONS(state_vector[1] + 1),
DENSITY_REGIONS(state_vector[2] + 1),
DURATION_REGIONS(state_vector[3] + 1),
)
DURATION_SETS = [{0.3}, {1.0}]
22 changes: 16 additions & 6 deletions minamidera/statemapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy.typing as npt
import pang

from .library import DENSITY_MAP, DURATION_MAP, INTENSITY_MAP, PITCHES_MAP, State
from .library import DENSITY_SETS, DURATION_SETS, INTENSITY_SETS, PITCHES_SETS
from .soundpointsgenerators import AtaxicSoundPointsGenerator


Expand All @@ -18,10 +18,20 @@ def map_state(state: npt.NDArray) -> pang.Sequence:
raise NotImplementedError


def map_state_to_sound_points_generator(state: State) -> pang.SoundPointsGenerator:
def map_state_vector_to_sound_points_generator(
state_vector: npt.NDArray,
) -> pang.SoundPointsGenerator:
if len(state_vector) != 4:
raise ValueError(
f"The shape of the state vector {state_vector} does not match the number of state variables"
)
if any(state not in (0, 1) for state in state_vector):
raise ValueError(
f"The state vector {state_vector} contains value other than 0 and 1"
)
return AtaxicSoundPointsGenerator(
PITCHES_MAP[state.frequency_region],
INTENSITY_MAP[state.intensity_region],
DENSITY_MAP[state.density_region],
DURATION_MAP[state.duration_region],
PITCHES_SETS[state_vector[0]],
INTENSITY_SETS[state_vector[1]],
DENSITY_SETS[state_vector[2]],
DURATION_SETS[state_vector[3]],
)
25 changes: 0 additions & 25 deletions tests/test_library.py

This file was deleted.

41 changes: 22 additions & 19 deletions tests/test_statemapper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import pang
import pytest

from minamidera import library, statemapper

Expand All @@ -8,23 +9,25 @@ def test_mapping_empty_sequence():
assert statemapper.map_state_sequence([]) == pang.Sequence.empty_sequence()


def test_mapping_state_to_sound_point_generator():
sound_points_generator = statemapper.map_state_to_sound_points_generator(
library.State.from_state_vector(np.array([0, 1, 0, 1]))
)
assert (
sound_points_generator.pitches_set
== library.PITCHES_MAP[library.FREQUENCY_REGIONS.F0]
)
assert (
sound_points_generator.intensity_set
== library.INTENSITY_MAP[library.INTENSITY_REGIONS.G1]
)
assert (
sound_points_generator.density_set
== library.DENSITY_MAP[library.DENSITY_REGIONS.D0]
)
assert (
sound_points_generator.duration_set
== library.DURATION_MAP[library.DURATION_REGIONS.L1]
def test_mapping_sound_point_generator_from_invalid_state_vector_length():
with pytest.raises(ValueError) as exception_info:
statemapper.map_state_vector_to_sound_points_generator(
np.array([0, 0, 1, 0, 0])
)
assert "does not match" in str(exception_info)


def test_mapping_sound_point_generator_from_invalid_state_vector_value():
with pytest.raises(ValueError) as exception_info:
statemapper.map_state_vector_to_sound_points_generator(np.array([0, 1, 0, 2]))
assert "contains value other than 0 and 1" in str(exception_info)


def test_mapping_state_vector_to_sound_point_generator():
sound_points_generator = statemapper.map_state_vector_to_sound_points_generator(
np.array([0, 1, 0, 1])
)
assert sound_points_generator.pitches_set == library.PITCHES_SETS[0]
assert sound_points_generator.intensity_set == library.INTENSITY_SETS[1]
assert sound_points_generator.density_set == library.DENSITY_SETS[0]
assert sound_points_generator.duration_set == library.DURATION_SETS[1]

0 comments on commit 43c312e

Please sign in to comment.