Skip to content

Commit

Permalink
better testing for interaction.py
Browse files Browse the repository at this point in the history
  • Loading branch information
cpmpercussion committed Aug 12, 2024
1 parent 0b77e7b commit bcb4efe
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
21 changes: 8 additions & 13 deletions impsy/interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,27 @@
import click
from .utils import mdrnn_config, get_config_data
import impsy.impsio as impsio
from pathlib import Path

np.set_printoptions(precision=2)


def setup_logging(dimension, location="logs/"):
def setup_logging(dimension: int, location="logs"):
"""Setup a log file and logging, requires a dimension parameter"""
log_file = (
datetime.datetime.now().isoformat().replace(":", "-")[:19]
+ "-"
+ str(dimension)
+ "d"
+ "-mdrnn.log"
) # Log file name.
log_file = location + log_file
log_date = datetime.datetime.now().isoformat().replace(":", "-")[:19]
log_name = f"{log_date}-{dimension}d-mdrnn.log"
log_file = Path(location) / log_name
log_format = "%(message)s"
logging.basicConfig(filename=log_file, level=logging.INFO, format=log_format)
click.secho(f"Logging enabled: {log_file}", fg="green")
click.secho(f"Logging enabled: {log_name}", fg="green")


def build_network(config):
def build_network(config: dict):
"""Build the MDRNN, uses a high-level size parameter and dimension."""
from . import mdrnn

click.secho(f"MDRNN: Using {config['model']['size']} model.", fg="green")
model_config = mdrnn_config(config["model"]["size"])
mdrnn.MODEL_DIR = "./models/"
net = mdrnn.PredictiveMusicMDRNN(
mode=mdrnn.NET_MODE_RUN,
dimension=config["model"]["dimension"],
Expand Down Expand Up @@ -327,7 +322,7 @@ def serve_forever(self):
for sender in self.senders:
sender.disconnect()
finally:
click.secho("\nDone, shutting down.", fg="red")
click.secho("\nIMPSY has shut down. Bye!", fg="red")


@click.command(name="run")
Expand Down
1 change: 0 additions & 1 deletion impsy/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def build_network(dimension=4, units=64, mixes=5, layers=2):
"""Build an MDRNN model."""
from . import mdrnn

mdrnn.MODEL_DIR = "./models/"
net = mdrnn.PredictiveMusicMDRNN(
mode=mdrnn.NET_MODE_RUN,
dimension=dimension,
Expand Down
51 changes: 46 additions & 5 deletions impsy/tests/test_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from impsy import utils
import pytest
from pathlib import Path
import time
import numpy as np

@pytest.fixture(scope="session")
def default_config():
Expand All @@ -11,11 +13,50 @@ def default_config():
return(config)


def test_interaction_server(default_config):
"""Just tests creation of an interaction server object"""
interaction_server = interaction.InteractionServer(default_config)
@pytest.fixture(scope="session")
def dimension(default_config):
return default_config["model"]["dimension"]


def test_logging():
def test_logging(dimension):
"""Just sets up logging"""
interaction.setup_logging(2)
interaction.setup_logging(dimension)


@pytest.fixture(scope="session")
def neural_network(default_config):
net = interaction.build_network(default_config)
return net


def test_build_network(neural_network):
pass


@pytest.fixture(scope="session")
def interaction_server(default_config):
interaction_server = interaction.InteractionServer(default_config)
return interaction_server


def test_monitor_user_action(interaction_server):
"""Just tests creation of an interaction server object"""
interaction_server.monitor_user_action()


def test_make_prediction(interaction_server, neural_network):
interaction_server.make_prediction(neural_network)


def test_input_list(interaction_server):
interaction_server.construct_input_list(0,0.0)


def test_dense_callback(interaction_server, dimension):
values = np.random.rand(dimension - 1)
interaction_server.dense_callback(values)


def test_send_values(interaction_server, dimension):
values = np.random.rand(dimension - 1)
interaction_server.send_back_values(values)

0 comments on commit bcb4efe

Please sign in to comment.