Skip to content

Commit

Permalink
[src&install] Drop librosa and refactor requirements files (#386)
Browse files Browse the repository at this point in the history
Fix #378 (switch to Julius)

Co-authored-by: Pariente Manuel <[email protected]>
  • Loading branch information
jonashaag and mpariente authored Jan 8, 2021
1 parent 57b2781 commit 91b9e4d
Show file tree
Hide file tree
Showing 19 changed files with 1,785 additions and 1,733 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/test_asteroid_package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ jobs:
run: |
python -m pip install --upgrade --user pip --quiet
python -m pip install numpy Cython --upgrade-strategy only-if-needed --quiet
python -m pip install pytest coverage codecov --upgrade-strategy only-if-needed --quiet
if [ $TORCH_INSTALL == "1.6.0" ]; then
INSTALL="torch==1.6.0 torchaudio==0.6.0"
elif [ $TORCH_INSTALL == "1.7.0" ]; then
Expand All @@ -42,7 +41,7 @@ jobs:
INSTALL="--pre torch torchaudio -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html"
fi
python -m pip install $INSTALL
python -m pip install -r requirements.txt --quiet
python -m pip install -r requirements/dev.txt --upgrade-strategy only-if-needed --quiet
python --version
pip --version
python -m pip list
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/test_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ jobs:
run: |
python -m pip install --upgrade --user pip --quiet
python -m pip install numpy Cython --upgrade-strategy only-if-needed --quiet
python -m pip install -r requirements.txt --quiet
python -m pip install -r docs/requirements.txt
python -m pip install -r requirements/docs.txt
python --version
pip --version
python -m pip list
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/test_model_consistency.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ jobs:
run: |
python -m pip install --upgrade --user pip --quiet
python -m pip install numpy Cython --upgrade-strategy only-if-needed --quiet
python -m pip install pytest coverage codecov --upgrade-strategy only-if-needed --quiet
if [ $TORCH_INSTALL == "1.6.0" ]; then
INSTALL="torch==1.6.0 torchaudio==0.6.0"
elif [ $TORCH_INSTALL == "1.7.0" ]; then
Expand All @@ -48,7 +47,7 @@ jobs:
INSTALL="--pre torch torchvision torchaudio -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html"
fi
python -m pip install $INSTALL
python -m pip install -r requirements.txt --quiet
python -m pip install -r requirements/dev.txt --quiet
python --version
pip --version
python -m pip list
Expand Down
18 changes: 16 additions & 2 deletions .github/workflows/test_torch_hub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,25 @@ jobs:
with:
python-version: 3.8

# Timeout: https://stackoverflow.com/a/59076067/4521646
timeout-minutes: 10
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install libnsdfile
run: |
sudo apt update
sudo apt install libsndfile1-dev libsndfile1
- name: Install python dependencies
run: |
python -m pip install --upgrade pip --quiet
python -m pip install torch --quiet
python -m pip install numpy scipy asteroid-filterbanks requests filelock --quiet
python -m pip install numpy Cython --upgrade-strategy only-if-needed --quiet
python -m pip install -r requirements/torchhub.txt --quiet
python --version
pip --version
python -m pip list
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ To do that, install asteroid in develop mode either with pip

To avoid formatting roundtrips in PRs, Asteroid relies on [`black`](https://github.com/psf/black)
and [`pre-commit-hooks`](https://github.com/pre-commit/pre-commit-hooks) to handle formatting
for us. You'll need to install `requirements.txt` and install git hooks with
for us. You'll need to install `requirements/dev.txt` and install git hooks with
`pre-commit install`.

Here is a summary:
Expand All @@ -24,7 +24,7 @@ Here is a summary:
### Install
git clone your_fork_url
cd asteroid
pip install -r requirements.txt
pip install -r requirements/dev.txt
pip install -e .
pre-commit install # To run black before commit

Expand Down
101 changes: 71 additions & 30 deletions asteroid/separate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import warnings
import torch
import numpy as np
import warnings
import soundfile as sf

try:
from typing import Protocol
Expand Down Expand Up @@ -120,50 +121,90 @@ def file_separate(
**kwargs,
) -> None:
"""Filename interface to `separate`."""
import soundfile as sf

if not hasattr(model, "sample_rate"):
raise TypeError(
f"This function requires your model ({type(model).__name__}) to have a "
"'sample_rate' attribute. See `BaseModel.sample_rate` for details."
)

# Estimates will be saved as filename_est1.wav etc...
base, _ = os.path.splitext(filename)
if output_dir is not None:
base = os.path.join(output_dir, os.path.basename(base))
save_name_template = base + "_est{}.wav"

# Bail out early if an estimate file already exists and we shall not overwrite.
est1_filename = save_name_template.format(1)
if os.path.isfile(est1_filename) and not force_overwrite:
warnings.warn(
f"File {est1_filename} already exists, pass `force_overwrite=True` to overwrite it",
UserWarning,
)
return

# SoundFile wav shape: [time, n_chan]
wav, fs = sf.read(filename, dtype="float32", always_2d=True)
wav, fs = _load_audio(filename)
if wav.shape[-1] > 1:
warnings.warn(
f"Received multichannel signal with {wav.shape[-1]} signals, "
f"using the first channel only."
)
# FIXME: support only single-channel files for now.
if fs != model.sample_rate:
if resample:
from librosa import resample

wav = resample(wav[:, 0], orig_sr=fs, target_sr=model.sample_rate)[:, None]
else:
raise RuntimeError(
f"Received a signal with a sampling rate of {fs}Hz for a model "
f"of {model.sample_rate}Hz. You can pass `resample=True` to resample automatically."
)
if resample:
wav = _resample(wav[:, 0], orig_sr=fs, target_sr=int(model.sample_rate))[:, None]
elif fs != model.sample_rate:
raise RuntimeError(
f"Received a signal with a sampling rate of {fs}Hz for a model "
f"of {model.sample_rate}Hz. You can pass `resample=True` to resample automatically."
)
# Pass wav as [batch, n_chan, time]; here: [1, 1, time]
wav = wav[:, 0][None, None]
(to_save,) = numpy_separate(model, wav, **kwargs)
(est_srcs,) = numpy_separate(model, wav, **kwargs)
# Resample to original sr
est_srcs = [
_resample(est_src, orig_sr=int(model.sample_rate), target_sr=fs) for est_src in est_srcs
]

# Save wav files to filename_est1.wav etc...
for src_idx, est_src in enumerate(to_save):
base = ".".join(filename.split(".")[:-1])
save_name = base + "_est{}.".format(src_idx + 1) + filename.split(".")[-1]
if output_dir is not None:
save_name = os.path.join(output_dir, save_name.split("/")[-1])
if os.path.isfile(save_name) and not force_overwrite:
warnings.warn(
f"File {save_name} already exists, pass `force_overwrite=True` to overwrite it",
UserWarning,
)
return
if fs != model.sample_rate:
from librosa import resample

est_src = resample(est_src, orig_sr=model.sample_rate, target_sr=fs)
sf.write(save_name, est_src, fs)
for src_idx, est_src in enumerate(est_srcs, 1):
sf.write(save_name_template.format(src_idx), est_src, fs)


def _resample(wav, orig_sr, target_sr, _resamplers={}):
from julius import ResampleFrac

if orig_sr == target_sr:
return wav

# Cache ResampleFrac instance to speed up resampling if we're repeatedly
# resampling between the same two sample rates.
try:
resampler = _resamplers[(orig_sr, target_sr)]
except KeyError:
resampler = _resamplers[(orig_sr, target_sr)] = ResampleFrac(orig_sr, target_sr)

return resampler(torch.from_numpy(wav)).numpy()


def _load_audio(filename):
try:
return sf.read(filename, dtype="float32", always_2d=True)
except Exception as sf_err:
# If soundfile fails to load the file, try with librosa next, which uses
# the 'audioread' library to support a wide range of audio formats.
# We try with soundfile first because librosa takes a long time to import.
try:
import librosa
except ModuleNotFoundError:
raise RuntimeError(
f"Could not load file {filename!r} with soundfile. "
"Install 'librosa' to be able to load more file types."
) from sf_err

wav, sr = librosa.load(filename, dtype="float32", sr=None)
# Always return wav of shape [time, n_chan]
if wav.ndim == 1:
return wav[:, None], sr
else:
return wav.T, sr
2 changes: 1 addition & 1 deletion docs/source/_templates/theme_variables.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'tutorials': '',
'previous_pytorch_versions': 'https://pytorch-lightning.rtfd.io/en/latest/',
'home': 'https://mpariente.github.io/asteroid/',
'get_started': 'https://colab.research.google.com/github/mpariente/asteroid/blob/master/notebooks/01_AsteroidGettingStarted.ipynb',
'get_started': 'https://colab.research.google.com/github/mpariente/asteroid/blob/master/notebooks/00_GettingStarted.ipynb',
'features': 'https://mpariente.github.io/asteroid/',
'blog': 'https://mpariente.github.io/asteroid/',
'resources': 'https://mpariente.github.io/asteroid/',
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def setup(app):
# https://stackoverflow.com/questions/15889621/sphinx-how-to-exclude-imports-in-automodule

MOCK_REQUIRE_PACKAGES = []
with open(os.path.join(PATH_ROOT, "requirements.txt"), "r") as fp:
with open(os.path.join(PATH_ROOT, "requirements", "docs.txt"), "r") as fp:
for ln in fp.readlines():
found = [ln.index(ch) for ch in list(",=<>#") if ch in ln]
pkg = ln[: min(found)] if found else ln
Expand Down
2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ large range of datasets and architectures, and a set of recipes to reproduce som
:maxdepth: 1
:caption: Notebooks and Tutorials

Getting started with Asteroid <http://colab.research.google.com/github/asteroid-team/asteroid/blob/master/notebooks/00_AsteroidGettingStarted.ipynb>
Getting started with Asteroid <http://colab.research.google.com/github/asteroid-team/asteroid/blob/master/notebooks/00_GettingStarted.ipynb>
Introduction and Overview <http://colab.research.google.com/github/asteroid-team/asteroid/blob/master/notebooks/01_AsteroidOverview.ipynb>
Understanding the Filterbank API <http://colab.research.google.com/github/asteroid-team/asteroid/blob/master/notebooks/02_Filterbank.ipynb>
Our PITLossWrapper explained <http://colab.research.google.com/github/asteroid-team/asteroid/blob/master/notebooks/03_PITLossWrapper.ipynb>
Expand Down
2 changes: 1 addition & 1 deletion egs/wham/DPTNet/utils/prepare_python_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ fi
$pip_path install soundfile
$pip_path install -e $asteroid_root
#$pip_path install ${asteroid_root}/\[""evaluate""\]
echo -e "\nAsteroid has been installed in editable mode. Feel free to apply your changes !"
echo -e "\nAsteroid has been installed in editable mode. Feel free to apply your changes !"
Loading

0 comments on commit 91b9e4d

Please sign in to comment.