Skip to content

Commit 4b0160f

Browse files
authored
Merge pull request #153 from sp-nitech/doctest
Add doctest
2 parents 1b9ff08 + 003890b commit 4b0160f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

134 files changed

+662
-465
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ jobs:
2121
torch: 2.3.1
2222
torchaudio: 2.3.1
2323
- python: "3.13"
24-
torch: 2.8.0
25-
torchaudio: 2.8.0
24+
torch: 2.9.0
25+
torchaudio: 2.9.0
2626

2727
steps:
2828
- name: Clone
29-
uses: actions/checkout@v4
29+
uses: actions/checkout@v5
3030

3131
- name: Python
32-
uses: actions/setup-python@v5
32+
uses: actions/setup-python@v6
3333
with:
3434
python-version: ${{ matrix.python }}
3535
architecture: x64
@@ -46,7 +46,7 @@ jobs:
4646
run: make check
4747

4848
- name: Test
49-
run: make test OPT="--dtype double"
49+
run: make test-all OPT="--dtype double"
5050

5151
- name: Coverage
5252
uses: codecov/codecov-action@v5

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,18 @@ format: tool
6161
./tools/taplo/taplo fmt *.toml
6262
./tools/yamlfmt/yamlfmt *.cff *.yml .github/workflows/*.yml
6363

64+
test-all: test-example test
65+
6466
test: tool
65-
[ -n "$(MODULE)" ] && module=tests/test_$(MODULE).py || module=; \
67+
[ -n "$(MODULE)" ] && module="--no-cov tests/test_$(MODULE).py" || module=; \
6668
. .venv/bin/activate && export PATH=./tools/SPTK/bin:$$PATH NUMBA_CUDA_LOW_OCCUPANCY_WARNINGS=0 PYLSTRAIGHT_DEBUG=1 && \
6769
python -m pytest $$module $(OPT)
6870

71+
test-example: tool
72+
[ -n "$(MODULE)" ] && module=modules/$(MODULE).py || module=; \
73+
. .venv/bin/activate && export NUMBA_CUDA_LOW_OCCUPANCY_WARNINGS=0 && \
74+
python -m pytest --doctest-modules --no-cov --ignore=diffsptk/third_party diffsptk/$$module
75+
6976
test-clean:
7077
rm -rf tests/__pycache__
7178
rm -rf *.png *.wav

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
*diffsptk* is a differentiable version of [SPTK](https://github.com/sp-nitech/SPTK) based on the PyTorch framework.
44

5-
[![Manual](https://img.shields.io/badge/docs-stable-blue.svg)](https://sp-nitech.github.io/diffsptk/3.3.1/)
5+
[![Manual](https://img.shields.io/badge/docs-stable-blue.svg)](https://sp-nitech.github.io/diffsptk/3.4.0/)
66
[![Downloads](https://static.pepy.tech/badge/diffsptk)](https://pepy.tech/project/diffsptk)
77
[![ClickPy](https://img.shields.io/badge/downloads-clickpy-yellow.svg)](https://clickpy.clickhouse.com/dashboard/diffsptk)
8+
[![Advisor](https://snyk.io/advisor/python/diffsptk/badge.svg)](https://snyk.io/advisor/python/diffsptk)
89
[![Python Version](https://img.shields.io/pypi/pyversions/diffsptk.svg)](https://pypi.python.org/pypi/diffsptk)
9-
[![PyTorch Version](https://img.shields.io/badge/pytorch-2.3.1%20%7C%202.8.0-orange.svg)](https://pypi.python.org/pypi/diffsptk)
10+
[![PyTorch Version](https://img.shields.io/badge/pytorch-2.3.1%20%7C%202.9.0-orange.svg)](https://pypi.python.org/pypi/diffsptk)
1011
[![PyPI Version](https://img.shields.io/pypi/v/diffsptk.svg)](https://pypi.python.org/pypi/diffsptk)
1112
[![Codecov](https://codecov.io/gh/sp-nitech/diffsptk/branch/master/graph/badge.svg)](https://app.codecov.io/gh/sp-nitech/diffsptk)
1213
[![License](https://img.shields.io/github/license/sp-nitech/diffsptk.svg)](https://github.com/sp-nitech/diffsptk/blob/master/LICENSE)
@@ -20,7 +21,7 @@
2021

2122
## Documentation
2223

23-
- Online [documentation](https://sp-nitech.github.io/diffsptk/3.3.1/) for the reference manual
24+
- Online [documentation](https://sp-nitech.github.io/diffsptk/3.4.0/) for the reference manual
2425
- Conference [paper](https://www.isca-archive.org/ssw_2023/yoshimura23_ssw.html) on the ISCA Archive
2526
- Hands-on [tutorial](https://colab.research.google.com/drive/1xAoUKqXadvJXJ7RzN0OceB6y7q5i7Sn6?usp=drive_link) on Google Colab
2627

diffsptk/functional.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -444,11 +444,12 @@ def dht(x: Tensor, dht_type: int = 2) -> Tensor:
444444

445445
def drc(
446446
x: Tensor,
447+
*,
448+
sample_rate: int,
447449
threshold: float,
448450
ratio: float,
449451
attack_time: float,
450452
release_time: float,
451-
sample_rate: int,
452453
makeup_gain: float = 0,
453454
abs_max: float = 1,
454455
) -> Tensor:
@@ -459,6 +460,9 @@ def drc(
459460
x : Tensor [shape=(..., T)]
460461
The input waveform.
461462
463+
sample_rate : int >= 1
464+
The sample rate in Hz.
465+
462466
threshold : float <= 0
463467
The threshold in dB.
464468
@@ -471,9 +475,6 @@ def drc(
471475
release_time : float > 0
472476
The release time in msec.
473477
474-
sample_rate : int >= 1
475-
The sample rate in Hz.
476-
477478
makeup_gain : float >= 0
478479
The make-up gain in dB.
479480
@@ -571,36 +572,27 @@ def dtw(
571572
)
572573

573574

574-
def dtw_merge(x: Tensor, y: Tensor, indices: Tensor) -> tuple[Tensor, Tensor]:
575-
"""Align two vector sequences according to the given path.
575+
def dtw_merge(x: Tensor, y: Tensor, indices: Tensor) -> Tensor:
576+
"""Merge two sequences according to the given indices.
576577
577578
Parameters
578579
----------
579-
x : Tensor [shape=(T1, ...)]
580+
x : Tensor [shape=(T1, D) or (T1,)]
580581
The query vector sequence.
581582
582-
y : Tensor [shape=(T2, ...)]
583+
y : Tensor [shape=(T2, D) or (T2,)]
583584
The reference vector sequence.
584585
585586
indices : Tensor [shape=(T, 2)]
586-
The indices of the path.
587+
The indices of the viterbi path.
587588
588589
Returns
589590
-------
590-
x_align : Tensor [shape=(T, ...)]
591-
The aligned query vector sequence.
592-
593-
y_align : Tensor [shape=(T, ...)]
594-
The aligned reference vector sequence.
591+
z : Tensor [shape=(T, 2D) or (T, 2)]
592+
The merged vector sequence.
595593
596594
"""
597-
if x.dim() != y.dim():
598-
raise ValueError("x and y must have the same number of dimensions.")
599-
if indices.dim() != 2 or indices.size(-1) != 2:
600-
raise ValueError("The shape of indices must be (T, 2).")
601-
x_align = x[indices[:, 0]]
602-
y_align = y[indices[:, 1]]
603-
return x_align, y_align
595+
return nn.DynamicTimeWarping.merge(x=x, y=y, indices=indices)
604596

605597

606598
def entropy(p: Tensor, out_format: str = "nat") -> Tensor:

diffsptk/modules/acorr.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
6262
6363
Examples
6464
--------
65-
>>> x = diffsptk.ramp(4)
65+
>>> import diffsptk
6666
>>> acorr = diffsptk.Autocorrelation(5, 3)
67+
>>> x = diffsptk.ramp(4)
6768
>>> r = acorr(x)
6869
>>> r
6970
tensor([30.0000, 20.0000, 11.0000, 4.0000])

diffsptk/modules/acr2csm.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,13 @@ def forward(self, r: torch.Tensor) -> torch.Tensor:
7373
7474
Examples
7575
--------
76-
>>> x = diffsptk.nrand(4)
77-
>>> x
78-
tensor([ 0.0165, -2.3693, 0.1375, -0.2262, 1.3307])
76+
>>> import diffsptk
7977
>>> acorr = diffsptk.Autocorrelation(5, 3)
8078
>>> acr2csm = diffsptk.AutocorrelationToCompositeSinusoidalModelCoefficients(3)
79+
>>> x = diffsptk.ramp(4)
8180
>>> c = acr2csm(acorr(x))
8281
>>> c
83-
tensor([0.9028, 2.5877, 3.8392, 3.6153])
82+
tensor([ 0.5261, 2.1403, 25.7668, 4.2332])
8483
8584
"""
8685
check_size(r.size(-1), self.in_dim, "dimension of autocorrelation")
@@ -137,12 +136,6 @@ def _precompute(
137136

138137
@staticmethod
139138
def _forward(r: torch.Tensor, C: torch.Tensor) -> torch.Tensor:
140-
if r.dtype != torch.double or C.dtype != torch.double:
141-
raise ValueError(
142-
"Only double precision is supported "
143-
f"(input: {r.dtype}, module: {C.dtype})."
144-
)
145-
146139
u = torch.matmul(r, C)
147140
u1, u2 = torch.tensor_split(u, 2, dim=-1)
148141

diffsptk/modules/alaw.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
5757
5858
Examples
5959
--------
60-
>>> x = diffsptk.ramp(4)
60+
>>> import diffsptk
6161
>>> alaw = diffsptk.ALawCompression(4)
62+
>>> x = diffsptk.ramp(4)
6263
>>> y = alaw(x)
6364
>>> y
6465
tensor([0.0000, 2.9868, 3.4934, 3.7897, 4.0000])

diffsptk/modules/ap.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,16 @@ def forward(self, x: torch.Tensor, f0: torch.Tensor) -> torch.Tensor:
138138
139139
Examples
140140
--------
141-
>>> x = diffsptk.sin(1000, 80)
142-
>>> pitch = diffsptk.Pitch(160, 8000, out_format="f0")
141+
>>> import diffsptk
142+
>>> pitch = diffsptk.Pitch(160, 16000, out_format="f0")
143+
>>> aperiodicity = diffsptk.Aperiodicity(160, 16000, 1024)
144+
>>> x = diffsptk.sin(2000, 80)
143145
>>> f0 = pitch(x)
144146
>>> f0.shape
145-
torch.Size([7])
146-
>>> aperiodicity = diffsptk.Aperiodicity(160, 16000, 1024)
147+
torch.Size([13])
147148
>>> ap = aperiodicity(x, f0)
148149
>>> ap.shape
149-
torch.Size([7, 513])
150+
torch.Size([13, 513])
150151
151152
"""
152153
d = x.dim()

diffsptk/modules/b2mc.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,13 @@ def forward(self, b: torch.Tensor) -> torch.Tensor:
7777
7878
Examples
7979
--------
80-
>>> b = diffsptk.ramp(4)
80+
>>> import diffsptk
8181
>>> mc2b = diffsptk.MelCepstrumToMLSADigitalFilterCoefficients(4, 0.3)
8282
>>> b2mc = diffsptk.MLSADigitalFilterCoefficientsToMelCepstrum(4, 0.3)
83+
>>> b = diffsptk.ramp(1, 5)
8384
>>> b2 = mc2b(b2mc(b))
8485
>>> b2
85-
tensor([0.0000, 1.0000, 2.0000, 3.0000, 4.0000])
86+
tensor([1.0000, 2.0000, 3.0000, 4.0000, 5.0000])
8687
8788
"""
8889
check_size(b.size(-1), self.in_dim, "dimension of cepstrum")

diffsptk/modules/c2acr.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ def forward(self, c: torch.Tensor) -> torch.Tensor:
6161
6262
Examples
6363
--------
64-
>>> c = diffsptk.nrand(4)
65-
>>> c
66-
tensor([-0.1751, 0.1950, -0.3211, 0.3523, -0.5453])
67-
>>> c2acr = diffsptk.CepstrumToAutocorrelation(4, 4, 16)
64+
>>> import diffsptk
65+
>>> import torch
66+
>>> c2acr = diffsptk.CepstrumToAutocorrelation(4, 3)
67+
>>> c = torch.tensor([0.5, -0.3, 0.2, -0.1, 0.05])
6868
>>> r = c2acr(c)
6969
>>> r
70-
tensor([ 1.0672, -0.0485, -0.1564, 0.2666, -0.4551])
70+
tensor([ 3.2404, -1.1920, 0.9037, -0.5838])
7171
7272
"""
7373
check_size(c.size(-1), self.in_dim, "dimension of cepstrum")

0 commit comments

Comments
 (0)