Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
takenori-y committed Apr 17, 2022
1 parent 776f8ad commit e27cfaf
Show file tree
Hide file tree
Showing 44 changed files with 710 additions and 890 deletions.
27 changes: 12 additions & 15 deletions tests/test_acorr.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# ------------------------------------------------------------------------ #

import pytest
import torch

import diffsptk
import tests.utils as U
Expand All @@ -24,9 +23,6 @@
@pytest.mark.parametrize("device", ["cpu", "cuda"])
@pytest.mark.parametrize("o", [0, 1, 2, 3])
def test_compatibility(device, o, M=3, L=14, B=2):
if device == "cuda" and not torch.cuda.is_available():
return

if o == 0:
opt = {"norm": False}
elif o == 1:
Expand All @@ -35,17 +31,18 @@ def test_compatibility(device, o, M=3, L=14, B=2):
opt = {"acf": "biased"}
elif o == 3:
opt = {"acf": "unbiased"}
acorr = diffsptk.AutocorrelationAnalysis(M, L, **opt).to(device)
x = torch.from_numpy(U.call(f"nrand -l {B*L}").reshape(-1, L)).to(device)
y = U.call(f"nrand -l {B*L} | acorr -l {L} -m {M} -o {o}").reshape(-1, M + 1)
U.check_compatibility(y, acorr, x)

acorr = diffsptk.AutocorrelationAnalysis(M, L, **opt)

@pytest.mark.parametrize("device", ["cpu", "cuda"])
def test_differentiable(device, M=3, L=14, B=2):
if device == "cuda" and not torch.cuda.is_available():
return
U.check_compatibility(
device,
acorr,
[],
f"nrand -l {B*L}",
f"acorr -l {L} -m {M} -o {o}",
[],
dx=L,
dy=M + 1,
)

acorr = diffsptk.AutocorrelationAnalysis(M, L).to(device)
x = torch.randn(B, L, requires_grad=True, device=device)
U.check_differentiable(acorr, x)
U.check_differentiable(device, acorr, [B, L])
32 changes: 14 additions & 18 deletions tests/test_b2mc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,24 @@
# ------------------------------------------------------------------------ #

import pytest
import torch

import diffsptk
import tests.utils as U


@pytest.mark.parametrize("device", ["cpu", "cuda"])
def test_compatibility(device, M=9, alpha=0.1, B=2):
if device == "cuda" and not torch.cuda.is_available():
return

b2mc = diffsptk.MLSADigitalFilterCoefficientsToMelCepstrum(M, alpha).to(device)
x = torch.from_numpy(U.call(f"nrand -l {B*(M+1)}").reshape(-1, M + 1)).to(device)
y = U.call(f"nrand -l {B*(M+1)} | b2mc -m {M} -a {alpha}").reshape(-1, M + 1)
U.check_compatibility(y, b2mc, x)


@pytest.mark.parametrize("device", ["cpu", "cuda"])
def test_differentiable(device, M=9, alpha=0.1, B=2):
if device == "cuda" and not torch.cuda.is_available():
return

b2mc = diffsptk.MLSADigitalFilterCoefficientsToMelCepstrum(M, alpha).to(device)
x = torch.randn(B, M + 1, requires_grad=True, device=device)
U.check_differentiable(b2mc, x)
b2mc = diffsptk.MLSADigitalFilterCoefficientsToMelCepstrum(M, alpha)

U.check_compatibility(
device,
b2mc,
[],
f"nrand -l {B*(M+1)}",
f"b2mc -m {M} -a {alpha}",
[],
dx=M + 1,
dy=M + 1,
)

U.check_differentiable(device, b2mc, [B, M + 1])
34 changes: 15 additions & 19 deletions tests/test_c2acr.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,24 @@
# ------------------------------------------------------------------------ #

import pytest
import torch

import diffsptk
import tests.utils as U


@pytest.mark.parametrize("device", ["cpu", "cuda"])
def test_compatibility(device, M=4, L=16, B=2):
if device == "cuda" and not torch.cuda.is_available():
return

c2acr = diffsptk.CepstrumToAutocorrelation(M, L).to(device)
x = torch.from_numpy(U.call(f"nrand -l {B*(M+1)}").reshape(-1, M + 1)).to(device)
y = U.call(f"nrand -l {B*(M+1)} | c2acr -m {M} -M {M} -l {L}").reshape(-1, M + 1)
U.check_compatibility(y, c2acr, x)


@pytest.mark.parametrize("device", ["cpu", "cuda"])
def test_differentiable(device, M=4, L=16, B=2):
if device == "cuda" and not torch.cuda.is_available():
return

c2acr = diffsptk.CepstrumToAutocorrelation(M, L).to(device)
x = torch.randn(B, M + 1, requires_grad=True, device=device)
U.check_differentiable(c2acr, x)
def test_compatibility(device, m=4, M=5, L=16, B=2):
c2acr = diffsptk.CepstrumToAutocorrelation(M, L)

U.check_compatibility(
device,
c2acr,
[],
f"nrand -l {B*(m+1)}",
f"c2acr -m {m} -M {M} -l {L}",
[],
dx=m + 1,
dy=M + 1,
)

U.check_differentiable(device, c2acr, [B, m + 1])
32 changes: 14 additions & 18 deletions tests/test_c2mpir.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,24 @@
# ------------------------------------------------------------------------ #

import pytest
import torch

import diffsptk
import tests.utils as U


@pytest.mark.parametrize("device", ["cpu", "cuda"])
def test_compatibility(device, M=19, N=30, B=2):
if device == "cuda" and not torch.cuda.is_available():
return

c2mpir = diffsptk.CepstrumToImpulseResponse(M, N).to(device)
x = torch.from_numpy(U.call(f"nrand -l {B*(M+1)}").reshape(-1, M + 1)).to(device)
y = U.call(f"nrand -l {B*(M+1)} | c2mpir -m {M} -l {N}").reshape(-1, N)
U.check_compatibility(y, c2mpir, x)


@pytest.mark.parametrize("device", ["cpu", "cuda"])
def test_differentiable(device, M=19, N=30, B=2):
if device == "cuda" and not torch.cuda.is_available():
return

c2mpir = diffsptk.CepstrumToImpulseResponse(M, N).to(device)
x = torch.randn(B, M + 1, requires_grad=True, device=device)
U.check_differentiable(c2mpir, x)
c2mpir = diffsptk.CepstrumToImpulseResponse(M, N)

U.check_compatibility(
device,
c2mpir,
[],
f"nrand -l {B*(M+1)}",
f"c2mpir -m {M} -l {N}",
[],
dx=M + 1,
dy=N,
)

U.check_differentiable(device, c2mpir, [B, M + 1])
32 changes: 14 additions & 18 deletions tests/test_c2ndps.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,24 @@
# ------------------------------------------------------------------------ #

import pytest
import torch

import diffsptk
import tests.utils as U


@pytest.mark.parametrize("device", ["cpu", "cuda"])
def test_compatibility(device, M=8, L=16, B=2):
if device == "cuda" and not torch.cuda.is_available():
return

c2ndps = diffsptk.CepstrumToNegativeDerivativeOfPhaseSpectrum(M, L).to(device)
x = torch.from_numpy(U.call(f"nrand -l {B*(M+1)}").reshape(-1, M + 1)).to(device)
y = U.call(f"nrand -l {B*(M+1)} | c2ndps -m {M} -l {L}").reshape(-1, L // 2 + 1)
U.check_compatibility(y, c2ndps, x)


@pytest.mark.parametrize("device", ["cpu", "cuda"])
def test_differentiable(device, M=4, L=16, B=2):
if device == "cuda" and not torch.cuda.is_available():
return

c2ndps = diffsptk.CepstrumToNegativeDerivativeOfPhaseSpectrum(M, L).to(device)
x = torch.randn(B, M + 1, requires_grad=True, device=device)
U.check_differentiable(c2ndps, x)
c2ndps = diffsptk.CepstrumToNegativeDerivativeOfPhaseSpectrum(M, L)

U.check_compatibility(
device,
c2ndps,
[],
f"nrand -l {B*(M+1)}",
f"c2ndps -m {M} -l {L}",
[],
dx=M + 1,
dy=L // 2 + 1,
)

U.check_differentiable(device, c2ndps, [B, M + 1])
38 changes: 13 additions & 25 deletions tests/test_cdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# ------------------------------------------------------------------------ #

import pytest
import torch

import diffsptk
import tests.utils as U
Expand All @@ -24,30 +23,19 @@
@pytest.mark.parametrize("device", ["cpu", "cuda"])
@pytest.mark.parametrize("reduction", ["none", "batchmean"])
def test_compatibility(device, reduction, B=2, M=19):
if device == "cuda" and not torch.cuda.is_available():
return
cdist = diffsptk.CepstralDistance(full=True, reduction=reduction)

opt = "-f" if reduction == "none" else ""
tmp1 = "cdist.tmp1"
tmp2 = "cdist.tmp2"
U.call(f"nrand -s 1 -l {B*(M+1)} > {tmp1}", get=False)
U.call(f"nrand -s 2 -l {B*(M+1)} > {tmp2}", get=False)

cdist = diffsptk.CepstralDistance(full=True, reduction=reduction).to(device)
x1 = torch.from_numpy(U.call(f"cat {tmp1}").reshape(-1, M + 1)).to(device)
x2 = torch.from_numpy(U.call(f"cat {tmp2}").reshape(-1, M + 1)).to(device)

opt = "-f" if reduction == "none" else ""
y = U.call(f"cdist {opt} -m {M} {tmp1} {tmp2}")
U.call(f"rm {tmp1} {tmp2}", get=False)
U.check_compatibility(y, cdist, x1, x2)


@pytest.mark.parametrize("device", ["cpu", "cuda"])
def test_differentiable(device, B=2, M=19):
if device == "cuda" and not torch.cuda.is_available():
return

cdist = diffsptk.CepstralDistance().to(device)
x1 = torch.randn(B, M + 1, requires_grad=True, device=device)
x2 = torch.randn(B, M + 1, requires_grad=True, device=device)
U.check_differentiable(cdist, x1, x2)
U.check_compatibility(
device,
cdist,
[f"nrand -s 1 -l {B*(M+1)} > {tmp1}", f"nrand -s 2 -l {B*(M+1)} > {tmp2}"],
[f"cat {tmp1}", f"cat {tmp2}"],
f"cdist -m {M} {opt} {tmp1} {tmp2}",
[f"rm {tmp1} {tmp2}"],
dx=M + 1,
)

U.check_differentiable(device, cdist, [(B, M + 1), (B, M + 1)])
32 changes: 14 additions & 18 deletions tests/test_dct.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,24 @@
# ------------------------------------------------------------------------ #

import pytest
import torch

import diffsptk
import tests.utils as U


@pytest.mark.parametrize("device", ["cpu", "cuda"])
def test_compatibility(device, L=8, B=2):
if device == "cuda" and not torch.cuda.is_available():
return

dct = diffsptk.DCT(L).to(device)
x = torch.from_numpy(U.call(f"nrand -l {B*L}").reshape(-1, L)).to(device)
y = U.call(f"nrand -l {B*L} | dct -l {L}").reshape(-1, L)
U.check_compatibility(y, dct, x)


@pytest.mark.parametrize("device", ["cpu", "cuda"])
def test_differentiable(device, L=8):
if device == "cuda" and not torch.cuda.is_available():
return

dct = diffsptk.DCT(L).to(device)
x = torch.randn(L, requires_grad=True, device=device)
U.check_differentiable(dct, x)
dct = diffsptk.DCT(L)

U.check_compatibility(
device,
dct,
[],
f"nrand -l {B*L}",
f"dct -l {L}",
[],
dx=L,
dy=L,
)

U.check_differentiable(device, dct, [B, L])
33 changes: 15 additions & 18 deletions tests/test_decimate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,25 @@
# ------------------------------------------------------------------------ #

import pytest
import torch

import diffsptk
import tests.utils as U


@pytest.mark.parametrize("device", ["cpu", "cuda"])
def test_compatibility(device, P=2, S=1, T=20, L=4):
if device == "cuda" and not torch.cuda.is_available():
return

decimate = diffsptk.Decimation(P, S).to(device)
x = torch.arange(T * L).view(T, L).to(device)
y = U.call(f"ramp -l {T*L} | decimate -l {L} -p {P} -s {S}").reshape(-1, L)
U.check_compatibility(y, decimate, x, opt={"dim": 0})


@pytest.mark.parametrize("device", ["cpu", "cuda"])
def test_differentiable(device, P=3, S=1, B=2, T=20):
if device == "cuda" and not torch.cuda.is_available():
return

decimate = diffsptk.Decimation(P, S).to(device)
x = torch.randn(B, T, requires_grad=True, device=device)
U.check_differentiable(decimate, x, opt={"dim": -1})
decimate = diffsptk.Decimation(P, S)

U.check_compatibility(
device,
decimate,
[],
f"ramp -l {T*L}",
f"decimate -l {L} -p {P} -s {S}",
[],
dx=L,
dy=L,
opt={"dim": 0},
)

U.check_differentiable(device, decimate, [T])
Loading

0 comments on commit e27cfaf

Please sign in to comment.