Skip to content

Commit

Permalink
Merge pull request #152 from carlthome/simplify-algorithms-imports
Browse files Browse the repository at this point in the history
Provide example of how to add a custom segmenter implementation from outside the package
  • Loading branch information
urinieto authored Nov 26, 2023
2 parents 906ca88 + 7db6386 commit dc9b519
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
21 changes: 21 additions & 0 deletions examples/custom_segmenter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import argparse

import msaf

import segmenter

# Inject custom segmenter.
setattr(msaf.algorithms, segmenter.algo_id, segmenter)
msaf.algorithms.__all__.append(segmenter.algo_id)


def main():
parser = argparse.ArgumentParser()
parser.add_argument("in_path", help="audio file")
args = parser.parse_args()
results = msaf.run.process(args.in_path, boundaries_id=segmenter.algo_id)
print(results)


if __name__ == "__main__":
main()
File renamed without changes.
File renamed without changes.
File renamed without changes.
42 changes: 11 additions & 31 deletions msaf/algorithms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,12 @@
"""Initialisation file for all the algorithms contained in MSAF."""
import glob
import importlib
import os

from . import interface

# Get current path
curr_path = os.path.dirname(os.path.realpath(__file__))
files = glob.glob(os.path.join(curr_path, "*"))

# Get all modules in the current path, which should be the algorithms
# available in MSAF
module_names = []
for file in files:
if os.path.isdir(file):
if os.path.isfile(os.path.join(file, "__init__.py")):
module_names.append(__name__ + "." + os.path.basename(file))

# Import all the algorithms in this folder
[importlib.import_module(module_name) for module_name in module_names]

# Also init the __all__ var in case they want to use "*" to import all
__all__ = [module_name.split(".")[-1] for module_name in module_names]

# Clean up variable space
del curr_path
del files
del module_names
del os
del glob
del file
from . import cnmf, fmc2d, foote, olda, scluster, sf, vmo

__all__ = [
"cnmf",
"fmc2d",
"foote",
"olda",
"scluster",
"sf",
"vmo",
]

0 comments on commit dc9b519

Please sign in to comment.