# here we make fixtures of toy data
# real parameters are stored and accessed from config


import pytest
import librosa
import os
import numpy as np

from hydra.experimental import compose, initialize


@pytest.fixture(scope="session")
def cfg():
  with initialize(config_path="../", job_name="test_app"):
    config = compose(config_name="config")
    config.dataset = compose(config_name="tests/test_dataset_config")
    config.train = compose(config_name="tests/test_train_config")

    return config

@pytest.fixture(scope="session")
def sample_rate(cfg):
  return cfg.data.sample_rate

@pytest.fixture(scope="session")
def example_wav(sample_rate):
  wav, sr = librosa.load(
    os.path.dirname(__file__) + "/data/example.mp3",
    sr=sample_rate, dtype=np.float32,
  )
  return { 'wav': wav, 'sr': sr }

@pytest.fixture(scope="session")
def n_fft(cfg):
  return cfg.data.n_fft

@pytest.fixture(scope="session")
def hop_length(cfg):
  return cfg.data.hop_length

@pytest.fixture(scope="session")
def win_length(cfg):
  return cfg.data.win_length

@pytest.fixture(scope="session")
def f_min(cfg):
  return cfg.data.f_min

@pytest.fixture(scope="session")
def f_max(cfg):
  return cfg.data.f_max

@pytest.fixture(scope="session")
def hop_ms(example_wav, hop_length):
  return 1e3 * hop_length / example_wav['sr']

@pytest.fixture(scope="session")
def n_frames(example_wav, hop_length):
  return (example_wav['wav'].shape[-1] - 1) // hop_length + 1

# It is not clear if we should cleanup the test directories
# or leave them for debugging
# https://github.com/pytest-dev/pytest/issues/3051
@pytest.fixture(autouse=True, scope='session')
def clear_files_teardown():
  yield None
  os.system("rm -r tests/test_dataset tests/test_experiment tests/test_logs")
[((205, 236), 'pytest.fixture', 'pytest.fixture', ([], {'scope': '"""session"""'}), "(scope='session')\n", (219, 236), False, 'import pytest\n'), ((507, 538), 'pytest.fixture', 'pytest.fixture', ([], {'scope': '"""session"""'}), "(scope='session')\n", (521, 538), False, 'import pytest\n'), ((593, 624), 'pytest.fixture', 'pytest.fixture', ([], {'scope': '"""session"""'}), "(scope='session')\n", (607, 624), False, 'import pytest\n'), ((812, 843), 'pytest.fixture', 'pytest.fixture', ([], {'scope': '"""session"""'}), "(scope='session')\n", (826, 843), False, 'import pytest\n'), ((886, 917), 'pytest.fixture', 'pytest.fixture', ([], {'scope': '"""session"""'}), "(scope='session')\n", (900, 917), False, 'import pytest\n'), ((970, 1001), 'pytest.fixture', 'pytest.fixture', ([], {'scope': '"""session"""'}), "(scope='session')\n", (984, 1001), False, 'import pytest\n'), ((1054, 1085), 'pytest.fixture', 'pytest.fixture', ([], {'scope': '"""session"""'}), "(scope='session')\n", (1068, 1085), False, 'import pytest\n'), ((1128, 1159), 'pytest.fixture', 'pytest.fixture', ([], {'scope': '"""session"""'}), "(scope='session')\n", (1142, 1159), False, 'import pytest\n'), ((1202, 1233), 'pytest.fixture', 'pytest.fixture', ([], {'scope': '"""session"""'}), "(scope='session')\n", (1216, 1233), False, 'import pytest\n'), ((1319, 1350), 'pytest.fixture', 'pytest.fixture', ([], {'scope': '"""session"""'}), "(scope='session')\n", (1333, 1350), False, 'import pytest\n'), ((1595, 1640), 'pytest.fixture', 'pytest.fixture', ([], {'autouse': '(True)', 'scope': '"""session"""'}), "(autouse=True, scope='session')\n", (1609, 1640), False, 'import pytest\n'), ((1684, 1759), 'os.system', 'os.system', (['"""rm -r tests/test_dataset tests/test_experiment tests/test_logs"""'], {}), "('rm -r tests/test_dataset tests/test_experiment tests/test_logs')\n", (1693, 1759), False, 'import os\n'), ((255, 305), 'hydra.experimental.initialize', 'initialize', ([], {'config_path': '"""../"""', 'job_name': '"""test_app"""'}), "(config_path='../', job_name='test_app')\n", (265, 305), False, 'from hydra.experimental import compose, initialize\n'), ((320, 349), 'hydra.experimental.compose', 'compose', ([], {'config_name': '"""config"""'}), "(config_name='config')\n", (327, 349), False, 'from hydra.experimental import compose, initialize\n'), ((371, 419), 'hydra.experimental.compose', 'compose', ([], {'config_name': '"""tests/test_dataset_config"""'}), "(config_name='tests/test_dataset_config')\n", (378, 419), False, 'from hydra.experimental import compose, initialize\n'), ((439, 485), 'hydra.experimental.compose', 'compose', ([], {'config_name': '"""tests/test_train_config"""'}), "(config_name='tests/test_train_config')\n", (446, 485), False, 'from hydra.experimental import compose, initialize\n'), ((685, 710), 'os.path.dirname', 'os.path.dirname', (['__file__'], {}), '(__file__)\n', (700, 710), False, 'import os\n')]