From d55ca5b58a6f9dfb06919db5a7915e424d20519f Mon Sep 17 00:00:00 2001 From: Markus Hofbauer Date: Fri, 11 Mar 2022 12:39:28 +0100 Subject: [PATCH] release 0.0.6 --- .pre-commit-config.yaml | 2 +- examples/evaluate_wav.py | 8 ++++++++ examples/white_noise.py | 9 +++++++++ requirements-dev.txt | 1 + setup.py | 2 +- test/metrics/test_spqi.py | 8 ++++++++ test/metrics/test_stsim.py | 10 ++++++++-- test/signal/test_transform.py | 8 ++++++++ vibromaf/metrics/stsim.py | 2 +- vibromaf/signal/spectrum.py | 7 +++++-- vibromaf/signal/transform.py | 4 ++-- 11 files changed, 52 insertions(+), 9 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index dfac2e9..095ad98 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,5 +1,5 @@ default_stages: [commit] - +exclude: '^data/' ci: autoupdate_commit_msg: "chore(deps): pre-commit.ci autoupdate" autoupdate_schedule: "monthly" diff --git a/examples/evaluate_wav.py b/examples/evaluate_wav.py index 847d0f4..f79f4b5 100644 --- a/examples/evaluate_wav.py +++ b/examples/evaluate_wav.py @@ -1,9 +1,17 @@ """Example to evaluate WAV files""" +import sys from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, FileType +from pathlib import Path from scipy.io import wavfile +# This is to make the local vibromaf package available +try: + sys.path.append(str(Path(__file__).absolute().parents[1])) +except IndexError: + pass + from vibromaf.metrics.snr import snr from vibromaf.metrics.spqi import spqi from vibromaf.metrics.stsim import st_sim diff --git a/examples/white_noise.py b/examples/white_noise.py index 768849b..e2e4615 100644 --- a/examples/white_noise.py +++ b/examples/white_noise.py @@ -1,7 +1,16 @@ """Simple get started example with white noise signals""" +import sys +from pathlib import Path + import numpy as np +# This is to make the local vibromaf package available +try: + sys.path.append(str(Path(__file__).absolute().parents[1])) +except IndexError: + pass + from vibromaf.metrics.snr import snr from vibromaf.metrics.spqi import spqi from vibromaf.metrics.stsim import st_sim diff --git a/requirements-dev.txt b/requirements-dev.txt index 700dae1..036a308 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -10,3 +10,4 @@ matplotlib tikzplotlib mkdocs mkdocstrings +pandas diff --git a/setup.py b/setup.py index cd6bcce..6af3725 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setuptools.setup( name="vibromaf", - version="0.0.5", + version="0.0.6", author="Markus Hofbauer, Andreas Noll", author_email="name.surname@tum.de", description="Vibrotactile quality metrics and metric fusion", diff --git a/test/metrics/test_spqi.py b/test/metrics/test_spqi.py index 174258d..a12cf27 100644 --- a/test/metrics/test_spqi.py +++ b/test/metrics/test_spqi.py @@ -44,3 +44,11 @@ def test_spqi__dist_shorter_than_ref__should_throw(self): dist = np.linspace(0, 1, 2800) with self.assertRaisesRegex(ValueError, r"Distorted .* must not be shorter"): spqi(dist, signal) + + def test_spqi__input_signals_identical_zero__should_be_valid(self): + signal = np.zeros(1024) + distorted = np.zeros(1024) + signal[-1] = 0.00001 + result = spqi(distorted, signal) + self.assertGreaterEqual(1, result) + self.assertGreaterEqual(result, 0) diff --git a/test/metrics/test_stsim.py b/test/metrics/test_stsim.py index 4d6b382..4344381 100644 --- a/test/metrics/test_stsim.py +++ b/test/metrics/test_stsim.py @@ -1,5 +1,5 @@ """Spectral Temporal SIMilarity Tests""" - +import math import unittest import numpy as np @@ -60,7 +60,13 @@ def test_compute_sim__values_larger_than_one_possible(self): ref_block = np.array([[0.5] * 4]) dist_block = np.array([[0.5, 0.5, 0.5, 1]]) result = STSIM.compute_sim(ref_block, dist_block) - self.assertEqual(1.25, result) + self.assertAlmostEqual(1.25, result) + + def test_compute_sim__input_signals_identical_zero__should_be_not_nan(self): + signal = np.zeros((4, 1024)) + distorted = np.zeros((4, 1024)) + result = STSIM.compute_sim(distorted, signal) + self.assertFalse(math.isnan(result)) def test_st_sim_init__eta_grater_one__should_throw(self): with self.assertRaisesRegex(ValueError, "Eta must be between 0 and 1."): diff --git a/test/signal/test_transform.py b/test/signal/test_transform.py index 6d5acd4..0b0a86d 100644 --- a/test/signal/test_transform.py +++ b/test/signal/test_transform.py @@ -158,6 +158,14 @@ def test_divide_and_normalize__multiple_blocks__correct_reshaped(self): np.array_equal(np.array([[-1, 1], [-1, 1], [-1, 1], [-1, 1]]), result) ) + def test_divide_and_normalize__input_signals_identical_zero__should_be_not_nan( + self, + ): + input_signal = np.zeros(10) + unit = BlockBuilder(5) + result = unit.divide_and_normalize(input_signal) + self.assertFalse(np.isnan(result).any()) + class PerceptualSpectrumBuilderTest(unittest.TestCase): """Perceptual Spectrum Builder Test""" diff --git a/vibromaf/metrics/stsim.py b/vibromaf/metrics/stsim.py index f6efe6d..9d9edd0 100644 --- a/vibromaf/metrics/stsim.py +++ b/vibromaf/metrics/stsim.py @@ -64,7 +64,7 @@ def compute_sim(reference: np.array, distorted: np.array) -> float: return float( np.mean( np.sum(reference * distorted, axis=1) - / np.sum(np.power(reference, 2), axis=1) + / (np.sum(np.power(reference, 2), axis=1) + np.finfo(float).eps) ) ) diff --git a/vibromaf/signal/spectrum.py b/vibromaf/signal/spectrum.py index d38abe4..2b066e4 100644 --- a/vibromaf/signal/spectrum.py +++ b/vibromaf/signal/spectrum.py @@ -30,7 +30,7 @@ def mag2db(power: np.array) -> np.array: def signal_energy(signal: np.array) -> np.array: """Calculate the signal energy""" - return np.sum(np.power(signal, 2)) + return np.sum(np.square(signal, dtype=np.float64)) def compute_normalized_spectral_difference( @@ -40,7 +40,10 @@ def compute_normalized_spectral_difference( difference = np.sum( np.abs(db2pow(reference_spectrum) - db2pow(distorted_spectrum)), axis=1 ) - return pow2db(difference / np.sum(np.abs(db2pow(reference_spectrum)), axis=1)) + return pow2db( + difference + / (np.sum(np.abs(db2pow(reference_spectrum)), axis=1) + np.finfo(float).eps) + ) def compute_spectral_support(spectrum: np.array, scale: float = 12) -> np.array: diff --git a/vibromaf/signal/transform.py b/vibromaf/signal/transform.py index 2659b40..ceaa0f6 100644 --- a/vibromaf/signal/transform.py +++ b/vibromaf/signal/transform.py @@ -76,7 +76,7 @@ def divide_and_normalize(self, signal: np.array) -> np.array: blocks = self.divide(signal) means = np.apply_along_axis(np.mean, 1, blocks).reshape((blocks.shape[0], 1)) stds = np.apply_along_axis(np.std, 1, blocks).reshape((blocks.shape[0], 1)) - return (blocks - means) / stds + return (blocks - means) / (stds + np.finfo(float).eps) @dataclass(frozen=True) @@ -84,7 +84,7 @@ class PerceptualSpectrumBuilder: """Calculate perceptual spectrum""" block_builder: BlockBuilder = BlockBuilder(512) - perceptual_threshold: PerceptualThreshold = PerceptualThreshold(2800) + perceptual_threshold: PerceptualThreshold = PerceptualThreshold(8000) block_transform_strategy: Callable[[np.array], np.array] = compute_block_dct def compute_perceptual_spectrum(self, signal: np.array) -> np.array: