From d42152d2fafa22160ba23dbd15bd084f0cbfa7f8 Mon Sep 17 00:00:00 2001 From: Ben Miller Date: Sun, 22 Sep 2024 00:48:06 -0700 Subject: [PATCH] formatting with black --- audalign/__init__.py | 15 ++++++-- audalign/config/__init__.py | 1 - audalign/config/correlation.py | 2 +- audalign/config/visual.py | 2 +- audalign/datalign.py | 9 ++--- audalign/filehandler.py | 36 ++++++++++++------- audalign/recognizers/__init__.py | 6 +++- audalign/recognizers/fingerprint/__init__.py | 26 ++++++++++---- .../recognizers/fingerprint/fingerprinter.py | 4 ++- audalign/recognizers/visrecognize/__init__.py | 2 ++ .../recognizers/visrecognize/visrecognize.py | 1 + tests/test_align.py | 30 ++++++++++++---- tests/test_audalign.py | 5 ++- tests/test_recognize.py | 19 +++++++--- 14 files changed, 112 insertions(+), 46 deletions(-) diff --git a/audalign/__init__.py b/audalign/__init__.py index 70382b5..29f2a90 100644 --- a/audalign/__init__.py +++ b/audalign/__init__.py @@ -63,7 +63,9 @@ def wrapper_decorator(*args, **kwargs): results = func(*args, **kwargs) if results is None: return results - assert results.get("rankings") is not None #This should run after rankings are added + assert ( + results.get("rankings") is not None + ) # This should run after rankings are added close_seconds_filter = BaseConfig.close_seconds_filter if kwargs.get("recognizer") is not None: close_seconds_filter = kwargs.get("recognizer").config.close_seconds_filter @@ -73,8 +75,10 @@ def wrapper_decorator(*args, **kwargs): return __filter_close_seconds_alignment(results, close_seconds_filter) else: return __filter_close_seconds(results, close_seconds_filter) + return wrapper_decorator + @filter_close_seconds @add_rankings def recognize( @@ -542,6 +546,7 @@ def pretty_print_alignment(results, match_keys="both"): print("No Matches Found") print() + def __filter_close_seconds(results: dict, close_seconds_filter: float): results_iterable_keys = [] # all list items in against_filename dictionary values @@ -559,7 +564,7 @@ def __filter_close_seconds(results: dict, close_seconds_filter: float): match = False for unfiltered_val in unfiltered_offset_seconds: if abs(abs(val) - abs(unfiltered_val)) <= close_seconds_filter: - iter_index_pop.append(i+1) + iter_index_pop.append(i + 1) match = True break if not match: @@ -571,6 +576,7 @@ def __filter_close_seconds(results: dict, close_seconds_filter: float): against_dict[key] = temp_list return results + def __filter_close_seconds_alignment(results: dict, close_seconds_filter: float): match_keys = ["match_info"] if results.get("fine_match_info") is not None: @@ -724,7 +730,10 @@ def write_shifts_from_results( if isinstance(read_from_dir, str): print("Finding audio files") read_from_dir = filehandler.get_audio_files_directory( - read_from_dir, full_path=True, can_read_extensions=config.can_read_extensions, cant_read_extensions=config.cant_read_extensions + read_from_dir, + full_path=True, + can_read_extensions=config.can_read_extensions, + cant_read_extensions=config.cant_read_extensions, ) if read_from_dir is not None: results_files = {} diff --git a/audalign/config/__init__.py b/audalign/config/__init__.py index a5735f7..834e6ce 100644 --- a/audalign/config/__init__.py +++ b/audalign/config/__init__.py @@ -80,7 +80,6 @@ class BaseConfig(ABC): # below extention lists will cause a crash fail_on_decode_error = True - # cant_write_extensions = [".mov", ".mp4", ".m4a"] cant_read_extensions = [".txt", ".md", ".pkf", ".py", ".pyc"] can_read_extensions = [ diff --git a/audalign/config/correlation.py b/audalign/config/correlation.py index 8430736..97b5467 100644 --- a/audalign/config/correlation.py +++ b/audalign/config/correlation.py @@ -71,4 +71,4 @@ class CorrelationConfig(BaseConfig): (0.65, -3), (0.1, -4), (0.0, 0), - ) \ No newline at end of file + ) diff --git a/audalign/config/visual.py b/audalign/config/visual.py index 822092a..24ec3c7 100644 --- a/audalign/config/visual.py +++ b/audalign/config/visual.py @@ -72,4 +72,4 @@ class VisualConfig(BaseConfig): (20, 2), (30, 1), (99999999999, 0), - ) \ No newline at end of file + ) diff --git a/audalign/datalign.py b/audalign/datalign.py index 1804936..7d52956 100644 --- a/audalign/datalign.py +++ b/audalign/datalign.py @@ -113,13 +113,10 @@ def event_a_is_closer(offset_a: int, offset_b: int) -> bool: return offset_a > offset_b -def distance_from_event(): - ... +def distance_from_event(): ... -def angle_two_events(): - ... +def angle_two_events(): ... -def which_is_first(): - ... +def which_is_first(): ... diff --git a/audalign/filehandler.py b/audalign/filehandler.py index 658f556..7d5586b 100644 --- a/audalign/filehandler.py +++ b/audalign/filehandler.py @@ -20,6 +20,7 @@ # Optional dependency ... + def _import_optional_dependencies(func): @wraps(func) def wrapper_decorator(*args, **kwargs): @@ -118,11 +119,12 @@ def create_audiosegment( return audiofile -def get_audio_files_directory(directory_path: str, full_path: bool = False, - can_read_extensions: list[str] = BaseConfig.can_read_extensions, - cant_read_extensions: list[str] = BaseConfig.cant_read_extensions, - - ) -> list: +def get_audio_files_directory( + directory_path: str, + full_path: bool = False, + can_read_extensions: list[str] = BaseConfig.can_read_extensions, + cant_read_extensions: list[str] = BaseConfig.cant_read_extensions, +) -> list: """returns a list of the file paths in directory that are audio Args: @@ -133,7 +135,11 @@ def get_audio_files_directory(directory_path: str, full_path: bool = False, """ aud_list = [] for file_path, ext in find_files(directory_path): - if check_is_audio_file(file_path=file_path, can_read_extensions=can_read_extensions, cant_read_extensions=cant_read_extensions): + if check_is_audio_file( + file_path=file_path, + can_read_extensions=can_read_extensions, + cant_read_extensions=cant_read_extensions, + ): if full_path is False: aud_list += [os.path.basename(file_path)] else: @@ -142,10 +148,10 @@ def get_audio_files_directory(directory_path: str, full_path: bool = False, def check_is_audio_file( - file_path: str, - can_read_extensions: list[str] = BaseConfig.can_read_extensions, - cant_read_extensions: list[str] = BaseConfig.cant_read_extensions, - ) -> bool: + file_path: str, + can_read_extensions: list[str] = BaseConfig.can_read_extensions, + cant_read_extensions: list[str] = BaseConfig.cant_read_extensions, +) -> bool: ext = os.path.splitext(file_path)[1] try: if ext in [".txt", ".json"] or ext in cant_read_extensions: @@ -336,7 +342,10 @@ def _remove_noise( file_name = os.path.basename(file_path) destination_name = os.path.join(destination_directory, file_name) - if os.path.splitext(destination_name)[1].lower() in base_config.cant_write_extensions: + if ( + os.path.splitext(destination_name)[1].lower() + in base_config.cant_write_extensions + ): destination_name = os.path.splitext(destination_name)[0] + ".wav" if write_extension is not None: @@ -475,7 +484,10 @@ def _uniform_level( file_name = os.path.basename(file_path) if len(os.path.splitext(destination_name)[1]) == 0: destination_name = os.path.join(destination_name, file_name) - if os.path.splitext(destination_name)[1].lower() in base_config.cant_write_extensions: + if ( + os.path.splitext(destination_name)[1].lower() + in base_config.cant_write_extensions + ): destination_name = os.path.splitext(destination_name)[0] + ".wav" if write_extension is not None: diff --git a/audalign/recognizers/__init__.py b/audalign/recognizers/__init__.py index 4a63148..ae2dac3 100644 --- a/audalign/recognizers/__init__.py +++ b/audalign/recognizers/__init__.py @@ -47,7 +47,11 @@ def align_get_file_names( file_names = [os.path.basename(x) for x in file_list] elif file_dir: file_names = filehandler.get_audio_files_directory( - file_dir, False, self.config.can_read_extensions, self.config.cant_read_extensions) + file_dir, + False, + self.config.can_read_extensions, + self.config.cant_read_extensions, + ) elif fine_aud_file_dict: file_names = [os.path.basename(x) for x in fine_aud_file_dict.keys()] else: diff --git a/audalign/recognizers/fingerprint/__init__.py b/audalign/recognizers/fingerprint/__init__.py index 3adbb18..9f741c7 100644 --- a/audalign/recognizers/fingerprint/__init__.py +++ b/audalign/recognizers/fingerprint/__init__.py @@ -101,7 +101,12 @@ def align_get_file_names( fine_aud_file_dict: typing.Optional[dict], ) -> list: if target_aligning or file_dir: - file_names = filehandler.get_audio_files_directory(file_dir, full_path=True, can_read_extensions=self.config.can_read_extensions, cant_read_extensions=self.config.cant_read_extensions) + file_names = filehandler.get_audio_files_directory( + file_dir, + full_path=True, + can_read_extensions=self.config.can_read_extensions, + cant_read_extensions=self.config.cant_read_extensions, + ) elif fine_aud_file_dict: file_names = fine_aud_file_dict.keys() for name, fingerprints in zip(self.file_names, self.fingerprinted_files): @@ -172,14 +177,19 @@ def recognize( if against_path is not None: if os.path.isdir(against_path): for path in filehandler.get_audio_files_directory( - against_path, full_path=True, can_read_extensions=self.config.can_read_extensions, cant_read_extensions=self.config.cant_read_extensions + against_path, + full_path=True, + can_read_extensions=self.config.can_read_extensions, + cant_read_extensions=self.config.cant_read_extensions, ): if path not in self.file_names and path not in to_fingerprint: to_fingerprint += [path] elif os.path.isfile(against_path): - if filehandler.check_is_audio_file(against_path, - self.config.can_read_extensions, - self.config.cant_read_extensions): + if filehandler.check_is_audio_file( + against_path, + self.config.can_read_extensions, + self.config.cant_read_extensions, + ): to_fingerprint += [against_path] if len(to_fingerprint) > 0: self.fingerprint_directory(to_fingerprint) @@ -268,7 +278,9 @@ def _fingerprint_directory( else: print("Directory contains 0 files or could not be found") if self.config.fail_on_decode_error: - raise CouldntDecodeError("Directory contains 0 files or could not be found") + raise CouldntDecodeError( + "Directory contains 0 files or could not be found" + ) return if _file_audsegs is not None: @@ -306,7 +318,7 @@ def _fingerprint_directory( result = [] for filename in filenames_to_fingerprint: - if isinstance(filename, str): # fine alignments are tuples with offsets + if isinstance(filename, str): # fine alignments are tuples with offsets file_name = os.path.basename(filename) if file_name in self.file_names: print(f"{file_name} already fingerprinted, continuing...") diff --git a/audalign/recognizers/fingerprint/fingerprinter.py b/audalign/recognizers/fingerprint/fingerprinter.py index f8adfff..5819c9f 100644 --- a/audalign/recognizers/fingerprint/fingerprinter.py +++ b/audalign/recognizers/fingerprint/fingerprinter.py @@ -57,7 +57,9 @@ def _fingerprint_worker( if config.fail_on_decode_error: raise e return None, None - except IndexError: # Pydub throws IndexErrors for some files on Ubuntu (json, txt, others?) + except ( + IndexError + ): # Pydub throws IndexErrors for some files on Ubuntu (json, txt, others?) print(f'File "{file_name}" could not be decoded') return None, None elif type(file_path) == tuple: diff --git a/audalign/recognizers/visrecognize/__init__.py b/audalign/recognizers/visrecognize/__init__.py index 1ab9c18..fafade6 100644 --- a/audalign/recognizers/visrecognize/__init__.py +++ b/audalign/recognizers/visrecognize/__init__.py @@ -11,6 +11,7 @@ from functools import partial from functools import wraps + def _import_optional_dependencies(func): @wraps(func) def wrapper_decorator(*args, **kwargs): @@ -23,6 +24,7 @@ def wrapper_decorator(*args, **kwargs): return wrapper_decorator + class VisualRecognizer(BaseRecognizer): config: VisualConfig diff --git a/audalign/recognizers/visrecognize/visrecognize.py b/audalign/recognizers/visrecognize/visrecognize.py index 6b660dc..914d8fa 100644 --- a/audalign/recognizers/visrecognize/visrecognize.py +++ b/audalign/recognizers/visrecognize/visrecognize.py @@ -12,6 +12,7 @@ from audalign.filehandler import find_files, get_shifted_file, read from PIL import Image from pydub.exceptions import CouldntDecodeError + try: from skimage.metrics import mean_squared_error from skimage.metrics import structural_similarity as ssim diff --git a/tests/test_align.py b/tests/test_align.py index 5c5fb1e..a6582a4 100644 --- a/tests/test_align.py +++ b/tests/test_align.py @@ -14,15 +14,20 @@ test_folder_eig = "test_audio/test_shifts/" -def ensure_close_seconds_filter(result, close_seconds_filter, initial_filter="match_info"): +def ensure_close_seconds_filter( + result, close_seconds_filter, initial_filter="match_info" +): for target_file in list(result.get(initial_filter).values()): for against_file in list(target_file["match_info"].values()): offset_list = sorted(against_file["offset_seconds"]) start = offset_list[0] for i in offset_list[1:]: - assert i - start > close_seconds_filter # results within close_seconds_filter + assert ( + i - start > close_seconds_filter + ) # results within close_seconds_filter start = i + class TestAlign: fingerprint_recognizer = ad.FingerprintRecognizer( load_fingerprints_file="tests/test_fingerprints.json" @@ -103,7 +108,9 @@ def test_align_cor_spec_options(self, tmpdir): assert result is not None ad.pretty_print_alignment(result) - @pytest.mark.skipif(skimage is None, reason="visrecognize optional dependencies not installed") + @pytest.mark.skipif( + skimage is None, reason="visrecognize optional dependencies not installed" + ) def test_align_vis(self, tmpdir): recognizer = ad.VisualRecognizer() recognizer.config.volume_threshold = 214 @@ -153,7 +160,9 @@ def test_align_files_load_fingerprints(self): ) assert result - @pytest.mark.skipif(skimage is None, reason="visrecognize optional dependencies not installed") + @pytest.mark.skipif( + skimage is None, reason="visrecognize optional dependencies not installed" + ) def test_align_files_vis(self, tmpdir): recognizer = ad.VisualRecognizer() recognizer.config.volume_threshold = 214 @@ -220,8 +229,11 @@ def test_align_close_seconds_filter(self, tmpdir): assert result ensure_close_seconds_filter(result, close_seconds_filter) + class TestTargetAlign: - @pytest.mark.skipif(skimage is None, reason="visrecognize optional dependencies not installed") + @pytest.mark.skipif( + skimage is None, reason="visrecognize optional dependencies not installed" + ) def test_target_align_vis(self, tmpdir): recognizer = ad.VisualRecognizer() recognizer.config.volume_threshold = 214 @@ -242,7 +254,9 @@ def test_target_align_vis(self, tmpdir): ) assert result is not None - @pytest.mark.skipif(skimage is None, reason="visrecognize optional dependencies not installed") + @pytest.mark.skipif( + skimage is None, reason="visrecognize optional dependencies not installed" + ) def test_target_align_vis_mse(self, tmpdir): recognizer = ad.VisualRecognizer() recognizer.config.volume_threshold = 214 @@ -361,7 +375,9 @@ def test_fine_align_load_fingerprints(self): assert result is not None ad.pretty_print_alignment(result, match_keys="match_info") - @pytest.mark.skipif(skimage is None, reason="visrecognize optional dependencies not installed") + @pytest.mark.skipif( + skimage is None, reason="visrecognize optional dependencies not installed" + ) def test_fine_align_visual(self, tmpdir): recognizer = ad.VisualRecognizer() recognizer.config.volume_threshold = 210 diff --git a/tests/test_audalign.py b/tests/test_audalign.py index 12e8673..bbdfdda 100644 --- a/tests/test_audalign.py +++ b/tests/test_audalign.py @@ -9,6 +9,7 @@ except ImportError: noisereduce = None + def test_always_true(): assert True @@ -166,7 +167,9 @@ def test_uniform_level_file_average(self, tmpdir): ) -@pytest.mark.skipif(noisereduce is None, reason="noisereduce optional dependencies not installed") +@pytest.mark.skipif( + noisereduce is None, reason="noisereduce optional dependencies not installed" +) class TestRemoveNoise: test_file = "test_audio/testers/test.mp3" diff --git a/tests/test_recognize.py b/tests/test_recognize.py index 2517b89..000858f 100644 --- a/tests/test_recognize.py +++ b/tests/test_recognize.py @@ -1,6 +1,7 @@ import audalign as ad import os import pytest + try: import skimage except ImportError: @@ -176,7 +177,9 @@ def test_recognize_locality_max_lags(self): self.fingerprint_recognizer.config = ad.config.fingerprint.FingerprintConfig() @pytest.mark.smoke - @pytest.mark.skipif(skimage is None, reason="visrecognize optional dependencies not installed") + @pytest.mark.skipif( + skimage is None, reason="visrecognize optional dependencies not installed" + ) def test_visrecognize(self): recognizer = ad.VisualRecognizer() recognizer.config.img_width = 0.5 @@ -188,7 +191,9 @@ def test_visrecognize(self): ) assert results - @pytest.mark.skipif(skimage is None, reason="visrecognize optional dependencies not installed") + @pytest.mark.skipif( + skimage is None, reason="visrecognize optional dependencies not installed" + ) def test_visrecognize_single_threaded(self): recognizer = ad.VisualRecognizer() @@ -203,7 +208,9 @@ def test_visrecognize_single_threaded(self): ) assert results - @pytest.mark.skipif(skimage is None, reason="visrecognize optional dependencies not installed") + @pytest.mark.skipif( + skimage is None, reason="visrecognize optional dependencies not installed" + ) def test_visrecognize_options(self): recognizer = ad.VisualRecognizer() recognizer.config.img_width = 0.5 @@ -220,7 +227,9 @@ def test_visrecognize_options(self): assert results assert results["match_info"]["test.mp3"]["mse"][0] == 20000000.0 - @pytest.mark.skipif(skimage is None, reason="visrecognize optional dependencies not installed") + @pytest.mark.skipif( + skimage is None, reason="visrecognize optional dependencies not installed" + ) def test_visrecognize_directory(self): recognizer = ad.VisualRecognizer() recognizer.config.img_width = 0.5 @@ -391,4 +400,4 @@ def test_correcognize_spectrogram_locality_max_lags(self): "offset_seconds" ] assert min(offset_seconds) < _max_lags - assert max(offset_seconds) < _max_lags \ No newline at end of file + assert max(offset_seconds) < _max_lags