Examples¶
Apply a filter¶
Keep only the vocal songs (i.e., drop instrumental pieces):
from MTCFeatures import MTCFeatureLoader
fl = MTCFeatureLoader('MTC-FS-INST-2.0')
seq_iter = fl.applyFilter('vocal')
for seq in seq_iter:
dosomething(seq)
If a filter has arguments, these should be provided with the filter name as tuple:
seq_iter = fl.applyFilter( ('afteryear', 1950) )
seq_iter = fl.applyFilter( ('betweenyears', 1850, 1900) )
Keep only songs in tune families with more than 10 members:
seq_iter = fl.minClassSizeFilter('tunefamily', 10)
A filter can be inverted by setting argument invert to True:
seq_iter = fl.applyFilter( ('afteryear', 1950), invert=True )
A chain of filters can be applied with the applyFilters method. The filters will be applied in the order provided.
seq_iter = fl.applyFilters(
[
{'mfilter':'vocal'},
{'mfilter':'freemeter', 'invert':True},
{'mfilter':('afteryear',1850)}
]
)
Register a filter¶
To register a new filter, use method registerMelodyFilter in the MTCFeatureLoader class. The filter should be a function returning True if the melody should be kept.
fl.registerFilter('vocal', lambda x: x['type'] == 'vocal')
Register a filter with arguments:
fl.registerFilter('afteryear', lambda y: lambda x: x['year'] > y )
Various Examples¶
Only use the midipitch from all songs in MTC-ANN-2.0.1:
from MTCFeatures import MTCFeatureLoader
fl = MTCFeatureLoader('MTC-ANN-2.0.1')
seq_iter = fl.selectFeatures(['midipitch'])
Use midipitch and duration from all songs in MTC-ANN-2.0.1:
fl = MTCFeatureLoader('MTC-ANN-2.0.1')
seq_iter = fl.selectFeatures(['midipitch', 'duration'])
Use intervals and inter onset interval ratios from all songs in MTC-ANN-2.0.1 and get rid of the None values for the first note:
fl = MTCFeatureLoader('MTC-ANN-2.0.1')
seq_iter = fl.selectFeatures(['chromaticinterval', 'IOR'])
seq_iter = fl.replaceNone(seq_iter=seq_iter)
Use scale degree, metric contour and beat position from all songs in MTC-ANN-2.0.1:
fl = MTCFeatureLoader('MTC-ANN-2.0.1')
seq_iter = fl.applyFeatureExtractor('full_beat_str')
seq_iter = fl.selectFeatures(
['scaledegree','metriccontour','full_beat_str'],
seq_iter=seq_iter
)
Get backgroundcorpus for MTC-ANN from MTC-FS-INST:
fl = MTCFeatureLoader('MTC-FS-INST-2.0')
seq_iter = fl.applyFilter('ann_bgcorpus')
Get labeled songs in Onder de groene linde:
fl = MTCFeatureLoader('MTC-FS-INST-2.0')
seq_iter = fl.applyFilters(
[
{'mfilter':'inOGL'},
{'mfilter':'labeled'}
]
)
Keep only those in tune families with more than 2 melodies:
seq_iter = fl.minClassSizeFilter('tunefamily', 2, seq_iter=seq_iter)
Use labeled 17th and 18th century fiddle music only:
fl = MTCFeatureLoader('MTC-FS-INST-2.0')
sel_instr = fl.applyFilter('instrumental')
sel_17th18th_c = fl.applyFilter(
('betweenyears', 1600, 1800),
seq_iter=sel_instr
)
sel_labeled = fl.applyFilter('labeled', seq_iter=sel_17th18th_c)
or:
seq_iter = fl.applyFilters(
[
{'mfilter':'instrumental'},
{'mfilter':'labeled'},
{'mfilter':('betweenyears', 1600, 1800)}
]
)
Use big tune families (>=20 melodies):
fl = MTCFeatureLoader('MTC-FS-INST-2.0')
sel_big = fl.minClassSizeFilter('tunefamily', 20)
Use small tune families (<=5 melodies) only:
fl = MTCFeatureLoader('MTC-FS-INST-2.0')
sel_small = fl.maxClassSizeFilter('tunefamily', 5)
Use only melodies with given identifiers:
fl = MTCFeatureLoader('MTC-FS-INST-2.0')
id_list = [
'NLB125814_01',
'NLB125815_01',
'NLB125817_01',
'NLB125818_01',
'NLB125822_01',
'NLB125823_01'
]
sel_list = fl.applyFilter( ('inNLBIDs', id_list) )
Use only instrumental melodies from tune family 2805_0:
fl = MTCFeatureLoader('MTC-FS-INST-2.0')
tf_list = ['2805_0']
sel_list = fl.applyFilter(
('inTuneFamilies', tf_list),
seq_iter=fl.applyFilter('instrumental')
)
Write the result to a gzipped .jsonl file:
fl.writeJSON('2805_0.jsonl.gz', seq_iter=sel_list)
Only keep ESSEN melodies from The Netherlands (For the exact names of the origins, consult the directory tree of the **kern distribution of the Essen Folksong Collection.):
fl = MTCFeatureLoader('ESSEN')
sel_dut = fl.applyFilter( ('origin', 'europa/nederlan') )
Get vocal melodies that have a meter:
fl = MTCFeatureLoader('MTC-FS-INST-2.0')
seq_iter = fl.applyFilters(
[
{'mfilter':'vocal'},
{'mfilter':'freemeter', 'invert':True}
]
)