Skip to content

Commit

Permalink
Merge pull request #75 from CPJKU/feature/more_tests
Browse files Browse the repository at this point in the history
Add tests for features (although not complete yet)
  • Loading branch information
superbock committed Feb 3, 2016
2 parents 14afc8a + 2334642 commit 5596d0d
Show file tree
Hide file tree
Showing 34 changed files with 855 additions and 207 deletions.
7 changes: 4 additions & 3 deletions madmom/features/notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ def expand_notes(notes, duration=0.6, velocity=100):
return notes


def write_notes(notes, filename, sep='\t',
fmt=None):
def write_notes(notes, filename, sep='\t', fmt=None, header=''):
"""
Write the notes to a file (as many columns as given).
Expand All @@ -98,6 +97,8 @@ def write_notes(notes, filename, sep='\t',
Separator for the fields.
fmt : list, optional
Format of the fields (i.e. columns, see notes)
header : str, optional
Header to be written (as a comment).
Returns
-------
Expand All @@ -119,7 +120,7 @@ def write_notes(notes, filename, sep='\t',
# truncate to the number of colums given
fmt = sep.join(fmt[:notes.shape[1]])
# write the notes
write_events(notes, filename, fmt=fmt)
write_events(notes, filename, fmt=fmt, header=header)
# also return them
return notes

Expand Down
2 changes: 2 additions & 0 deletions madmom/features/tempo.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,8 @@ def write_tempo(tempi, filename, mirex=False):
Their relative strength.
"""
# make the given tempi a 2d array
tempi = np.array(tempi, ndmin=2)
# default values
t1, t2, strength = 0., 0., 1.
# only one tempo was detected
Expand Down
142 changes: 102 additions & 40 deletions madmom/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,65 +61,127 @@ def decorator_function(*args, **kwargs):


# file handling routines
def search_files(path, suffix=None):
def filter_files(files, suffix):
"""
Returns a list of files in `path` matching the given `suffix` or filters
the given list to include only those matching the given suffix.
Filter the list to contain only files matching the given `suffix`.
Parameters
----------
files : list
List of files to be filtered.
suffix : str
Return only files matching this suffix.
Returns
-------
list
List of files.
"""
import fnmatch
# make sure files is a list
if not isinstance(files, list):
files = [files]
# no suffix given, return the list unaltered
if suffix is None:
return files
# filter the files with the given suffix
file_list = []
if isinstance(suffix, list):
# a list of suffices is given
for s in suffix:
file_list.extend(fnmatch.filter(files, "*%s" % s))
else:
# a single suffix is given
file_list.extend(fnmatch.filter(files, "*%s" % suffix))
# return the filtered list
return file_list


def search_path(path, recursion_depth=0):
"""
Returns a list of files in a directory (recursively).
Parameters
----------
path : str or list
Path or list of files to be searched / filtered.
Directory to be searched.
recursion_depth : int, optional
Recursively search sub-directories up to this depth.
Returns
-------
list
List of files.
"""
# adapted from http://stackoverflow.com/a/234329
import os
# remove the rightmost path separator (needed for recursion depth count)
path = path.rstrip(os.path.sep)
# we can only handle directories
if not os.path.isdir(path):
raise IOError("%s is not a directory." % path)
# files to be returned
file_list = []
# keep track of the initial recursion depth
initial_depth = path.count(os.path.sep)
for root, dirs, files in os.walk(path):
# add all files of this directory to the list
for f in files:
file_list.append(os.path.join(root, f))
# remove all subdirs exceeding the wanted recursion depth
if initial_depth + recursion_depth <= root.count(os.path.sep):
del dirs[:]
# return the sorted file list
return sorted(file_list)


def search_files(files, suffix=None, recursion_depth=0):
"""
Returns the files matching the given `suffix`.
Parameters
----------
files : str or list
File, path or a list thereof to be searched / filtered.
suffix : str, optional
Return only files matching this suffix.
recursion_depth : int, optional
Recursively search sub-directories up to this depth.
Returns
-------
list
List of files.
Notes
-----
The list of returned files is sorted.
"""
import os
import glob

file_list = []
# determine the files
if isinstance(path, list):
# a list of files or paths is given
file_list = []
# recursively call the function
for f in path:
file_list.extend(search_files(f, suffix))
elif os.path.isdir(path):
# use all files in the given path
if suffix is None:
file_list = glob.glob("%s/*" % path)
elif isinstance(suffix, list):
file_list = []
for s in suffix:
file_list.extend(glob.glob("%s/*%s" % (path, s)))
else:
file_list = glob.glob("%s/*%s" % (path, suffix))
elif os.path.isfile(path):
file_list = []
# no matching needed
if suffix is None:
file_list = [path]
# a list of suffices is given
elif isinstance(suffix, list):
for s in suffix:
if path.endswith(s):
file_list = [path]
# a single suffix is given
elif path.endswith(suffix):
file_list = [path]
if isinstance(files, list):
# a list is given, recursively call the function on each element
for f in files:
file_list.extend(search_files(f))
elif os.path.isdir(files):
# add all files in the given path (up to the given recursion depth)
file_list.extend(search_path(files, recursion_depth))
elif os.path.isfile(files):
# add the given file
file_list.append(files)
else:
raise IOError("%s does not exist." % path)
raise IOError("%s does not exist." % files)
# filter with the given sufix
if suffix is not None:
file_list = filter_files(file_list, suffix)
# remove duplicates
file_list = list(set(file_list))
# sort files
file_list.sort()
# return the file list
return file_list
# return the sorted file list
return sorted(file_list)


def strip_suffix(filename, suffix=None):
Expand Down
6 changes: 6 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@

import os

from madmom import MODELS_PATH

DATA_PATH = os.path.dirname(os.path.realpath(__file__)) + '/data/'
AUDIO_PATH = DATA_PATH + 'audio/'
ACTIVATIONS_PATH = DATA_PATH + 'activations/'
ANNOTATIONS_PATH = DATA_PATH + 'annotations/'
DETECTIONS_PATH = DATA_PATH + 'detections/'
Binary file not shown.
Binary file added tests/data/activations/sample.complexflux.npz
Binary file not shown.
Binary file added tests/data/activations/sample.logfiltspecflux.npz
Binary file not shown.
Binary file not shown.
Binary file added tests/data/activations/sample.superflux.npz
Binary file not shown.
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions tests/data/annotations/stereo_sample.notes.mirex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
0.147 3.470 523.3
1.567 1.790 87.3
2.526 3.456 698.5
2.549 2.760 261.6
2.563 2.765 349.2
2.577 2.811 207.7
3.369 4.149 622.3
3.449 3.721 98.0
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions tests/data/detections/sample.tempo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
176.47 117.65 0.68
Loading

0 comments on commit 5596d0d

Please sign in to comment.