Skip to content

Commit

Permalink
Revert "Revert "Refactor: handling of self._score in Indexer supercla…
Browse files Browse the repository at this point in the history
…ss""

This reverts commit 740db79.
  • Loading branch information
alexandermorgan committed Oct 22, 2016
1 parent 4ed6b10 commit 0b7c880
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 56 deletions.
3 changes: 1 addition & 2 deletions vis/analyzers/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ def __init__(self, score, settings=None):
raise IndexError(Indexer._INIT_INDEX_ERR)
else:
ind_name = score.columns.levels[0][0]
num_parts = len(score[ind_name].columns)
score = [score[ind_name][str(i)].dropna() for i in range(num_parts)]
score = [score.iloc[:, x].dropna() for x in range(len(score.columns))]
elif isinstance(score, stream.Score) and req_s_type is stream.Part:
score = [score.parts[i] for i in range(len(score.parts))]

Expand Down
31 changes: 18 additions & 13 deletions vis/analyzers/indexers/offset.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# Filename: analyzers/indexers/offset.py
# Purpose: Indexer to regularize the observed offsets.
#
# Copyright (C) 2013, 2014 Christopher Antila
# Copyright (C) 2013, 2014, 2016 Christopher Antila, Alexander Morgan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down Expand Up @@ -197,21 +197,25 @@ class FilterByOffsetIndexer(indexer.Indexer):
:type 'mp': boolean
**Example:**
**Examples:**
>>> from vis.models.indexed_piece import Importer
>>> ip = Importer('path_to_piece.xml')
>>> notes = ip.get_data('noterest')
>>> setts = {'quarterLength': 2}
>>> ip.get_data('offset', data=notes, settings=setts)
The ``notes`` variable contains a :class:`pandas.DataFrame`,
but the offset indexer requires a :class:`pandas.Series` as an
argument, thus we have to convert the ``notes`` variable to a
series first:
# Note that other analysis results can be passed to the offset indexer too,
# such as the IntervalIndexer results as in the following example. Note
# also that the original column names (or names of the series if a list of
# series was passed) are retained, though the highest level of the
# columnar multi-index gets overwritten
>>> notes_series = [df.iloc[:, x]
for x in range(len(notes.columns))]
>>> from vis.models.indexed_piece import Importer
>>> ip = Importer('path_to_piece.xml')
>>> intervals = ip.get_data('vertical_interval')
>>> setts = {'quarterLength': 2}
>>> ip.get_data('offset', data=notes_series, settings=setts)
>>> ip.get_data('offset', data=intervals, settings=setts)
"""

Expand All @@ -226,10 +230,11 @@ class FilterByOffsetIndexer(indexer.Indexer):

def __init__(self, score, settings=None):
"""
:param score: A list of Series you wish to filter by offset
values, stored in the Index.
:param score: A DataFrame or list of Series you wish to
filter by offset values, stored in the Index.
:type score: ``list`` of :class:`pandas.Series`
:type score: :class:`pandas.DataFrame` or
``list`` of :class:`pandas.Series`
:param dict settings: There is one required setting.
See :const:`possible_settings`.
Expand Down Expand Up @@ -313,5 +318,5 @@ def run(self):
off_list = list(pandas.Series(range(start_offset, end_offset + step, step)).div(1000.0))
# pylint: disable=C0301
post.append(part.reindex(index=off_list, method=self._settings['method']))
post = self.make_return([six.u(str(x)) for x in range(len(post))], post)
post = self.make_return([ser.name[1] for ser in self._score], post)
return post
93 changes: 52 additions & 41 deletions vis/tests/test_offset.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ def test_init_4(self):

def test_run_1_a(self):
# try statement correctly finds minimum start_offset and whole index when no series is empty.
in_val = [pandas.Series(['A', 'B']), pandas.Series(['B', 'C'], index=[1, 2])]
in_val = [pandas.Series(['A', 'B'], name=('N', '0')),
pandas.Series(['B', 'C'], index=[1, 2], name=('N', '1'))]
settings = {'quarterLength': 1.0, 'method': 'ffill'}
actual = FilterByOffsetIndexer(in_val, settings).run()
self.assertListEqual(list(actual.index), [0.0, 1.0, 2.0])
self.assertEqual(len(actual.columns), 2)

def test_run_1_b(self):
# same as test_run_1_a but with a non-zero starting point and non-overlapping indecies.
in_val = [pandas.Series(['A', 'B'], index=[4, 5]), pandas.Series(['C', 'D'], index=[9, 10])]
in_val = [pandas.Series(['A', 'B'], index=[4, 5], name=('N', '0')),
pandas.Series(['C', 'D'], index=[9, 10], name=('N', '1'))]
settings = {'quarterLength': 1.0, 'method': 'ffill'}
actual = FilterByOffsetIndexer(in_val, settings).run()
self.assertEqual(actual.index[0], 4.0)
Expand All @@ -95,24 +97,24 @@ def test_run_1_b(self):

def test_run_2_a(self):
# checks if the index and number of columns are correct if one of the passed series is empty.
in_val = [pandas.Series(['A', 'B']), pandas.Series()]
in_val = [pandas.Series(['A', 'B'], name=('N', '0')), pandas.Series(name=('N', '1'))]
settings = {'quarterLength': 1.0, 'method': 'ffill'}
actual = FilterByOffsetIndexer(in_val, settings).run()
self.assertListEqual(list(actual.index), [0.0, 1.0])
self.assertEqual(len(actual.columns), 2)

def test_run_2_b(self):
# checks if the index and number of columns are correct if all the passed series are empty.
in_val = [pandas.Series(), pandas.Series()]
in_val = [pandas.Series(name=('N', '0')), pandas.Series(name=('N', '1'))]
settings = {'quarterLength': 1.0, 'method': 'ffill'}
actual = FilterByOffsetIndexer(in_val, settings).run()
self.assertListEqual(list(actual.index), [])
self.assertEqual(len(actual.columns), 2)

def test_offset_1part_1(self):
# 0 length
in_val = [pandas.Series()]
expected = pandas.Series()
in_val = [pandas.Series(name=('Indexer', '0'))]
expected = pandas.Series(name=('Indexer', '0'))
offset_interval = 0.5
ind = FilterByOffsetIndexer(in_val, {u'quarterLength': offset_interval})
actual = ind.run()['offset.FilterByOffsetIndexer']
Expand All @@ -123,8 +125,8 @@ def test_offset_1part_1(self):

def test_offset_1part_2(self):
# input is expected output
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.5, 1.0, 1.5])]
expected = pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.5, 1.0, 1.5])
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.5, 1.0, 1.5], name=('Indexer', '0'))]
expected = pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.5, 1.0, 1.5], name=('Indexer', '0'))
offset_interval = 0.5
ind = FilterByOffsetIndexer(in_val, {u'quarterLength': offset_interval})
actual = ind.run()['offset.FilterByOffsetIndexer']
Expand All @@ -135,8 +137,9 @@ def test_offset_1part_2(self):

def test_offset_1part_3(self):
# already regular offset interval to larger one
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.5, 1.0, 1.5])]
expected = pandas.Series(['a', 'c', 'd'], index=[0.0, 1.0, 2.0])
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.5, 1.0, 1.5],
name=('Indexer', '0'))]
expected = pandas.Series(['a', 'c', 'd'], index=[0.0, 1.0, 2.0], name=('Indexer', '0'))
offset_interval = 1.0
ind = FilterByOffsetIndexer(in_val, {u'quarterLength': offset_interval})
actual = ind.run()['offset.FilterByOffsetIndexer']
Expand All @@ -147,8 +150,9 @@ def test_offset_1part_3(self):

def test_offset_1part_4a(self):
# already regular offset interval to smaller one
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.5, 1.0, 1.5])]
expected = pandas.Series(['a', 'a', 'b', 'b', 'c', 'c', 'd'],
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.5, 1.0, 1.5],
name=('Indexer', '0'))]
expected = pandas.Series(['a', 'a', 'b', 'b', 'c', 'c', 'd'], name=('Indexer', '0'),
index=[0.0, 0.25, 0.5, 0.75, 1.0, 1.25, 1.5])
offset_interval = 0.25
ind = FilterByOffsetIndexer(in_val, {u'quarterLength': offset_interval})
Expand All @@ -160,8 +164,9 @@ def test_offset_1part_4a(self):

def test_offset_1part_4b(self):
# already regular offset interval to a very small one
in_val = [pandas.Series(['a', 'b'], index=[0.0, 0.5])]
expected = pandas.Series(['a', 'a', 'a', 'a', 'b'], index=[0.0, 0.125, 0.25, 0.375, 0.5])
in_val = [pandas.Series(['a', 'b'], index=[0.0, 0.5], name=('Indexer', '0'))]
expected = pandas.Series(['a', 'a', 'a', 'a', 'b'], name=('Indexer', '0'),
index=[0.0, 0.125, 0.25, 0.375, 0.5])
offset_interval = 0.125
ind = FilterByOffsetIndexer(in_val, {u'quarterLength': offset_interval})
actual = ind.run()['offset.FilterByOffsetIndexer']
Expand All @@ -172,8 +177,8 @@ def test_offset_1part_4b(self):

def test_offset_1part_5(self):
# already regular offset interval (but some missing) to larger one
in_val = [pandas.Series(['a', 'b', 'c'], index=[0.0, 0.5, 1.5])]
expected = pandas.Series(['a', 'b', 'c'], index=[0.0, 1.0, 2.0])
in_val = [pandas.Series(['a', 'b', 'c'], index=[0.0, 0.5, 1.5], name=('Indexer', '0'))]
expected = pandas.Series(['a', 'b', 'c'], index=[0.0, 1.0, 2.0], name=('Indexer', '0'))
offset_interval = 1.0
ind = FilterByOffsetIndexer(in_val, {u'quarterLength': offset_interval})
actual = ind.run()['offset.FilterByOffsetIndexer']
Expand All @@ -184,8 +189,8 @@ def test_offset_1part_5(self):

def test_offset_1part_6(self):
# already regular offset interval (but some missing) to smaller one
in_val = [pandas.Series(['a', 'b', 'c'], index=[0.0, 0.5, 1.5])]
expected = pandas.Series(['a', 'a', 'b', 'b', 'b', 'b', 'c'],
in_val = [pandas.Series(['a', 'b', 'c'], index=[0.0, 0.5, 1.5], name=('Indexer', '0'))]
expected = pandas.Series(['a', 'a', 'b', 'b', 'b', 'b', 'c'], name=('Indexer', '0'),
index=[0.0, 0.25, 0.5, 0.75, 1.0, 1.25, 1.5])
offset_interval = 0.25
ind = FilterByOffsetIndexer(in_val, {u'quarterLength': offset_interval})
Expand All @@ -197,8 +202,10 @@ def test_offset_1part_6(self):

def test_offset_1part_7(self):
# irregular offset interval to a large one
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.4, 1.1, 2.1])]
expected = pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 1.0, 2.0, 3.0])
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.4, 1.1, 2.1],
name=('Indexer', '0'))]
expected = pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 1.0, 2.0, 3.0],
name=('Indexer', '0'))
offset_interval = 1.0
ind = FilterByOffsetIndexer(in_val, {u'quarterLength': offset_interval})
actual = ind.run()['offset.FilterByOffsetIndexer']
Expand All @@ -209,9 +216,11 @@ def test_offset_1part_7(self):

def test_offset_1part_8(self):
# irregular offset interval to a small one
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.4, 1.1, 2.1])]
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.4, 1.1, 2.1],
name=('Indexer', '0'))]
expected = pandas.Series(['a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'd'],
index=[0.0, 0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25])
index=[0.0, 0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25],
name=('Indexer', '0'))
offset_interval = 0.25
ind = FilterByOffsetIndexer(in_val, {u'quarterLength': offset_interval})
actual = ind.run()['offset.FilterByOffsetIndexer']
Expand All @@ -222,8 +231,9 @@ def test_offset_1part_8(self):

def test_offset_1part_9(self):
# targeted test for end-of-piece: when last thing lands on an observed offset
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.4, 1.1, 2.0])]
expected = pandas.Series(['a', 'b', 'b', 'c', 'd'], index=[0.0, 0.5, 1.0, 1.5, 2.0])
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.4, 1.1, 2.0], name=('N', '0'))]
expected = pandas.Series(['a', 'b', 'b', 'c', 'd'], index=[0.0, 0.5, 1.0, 1.5, 2.0],
name=('N', '0'))
offset_interval = 0.5
ind = FilterByOffsetIndexer(in_val, {u'quarterLength': offset_interval})
actual = ind.run()['offset.FilterByOffsetIndexer']
Expand All @@ -234,8 +244,8 @@ def test_offset_1part_9(self):

def test_offset_1part_10(self):
# targeted test for end-of-piece: when last thing doesn't land on an observed offset
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.4, 1.1, 2.1])]
expected = pandas.Series(['a', 'b', 'b', 'c', 'c', 'd'],
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.4, 1.1, 2.1], name=('N', '0'))]
expected = pandas.Series(['a', 'b', 'b', 'c', 'c', 'd'], name=('N', '0'),
index=[0.0, 0.5, 1.0, 1.5, 2.0, 2.5])
offset_interval = 0.5
ind = FilterByOffsetIndexer(in_val, {u'quarterLength': offset_interval})
Expand All @@ -249,7 +259,7 @@ def test_offset_1part_10(self):
class TestOffsetIndexerManyParts(unittest.TestCase):
def test_offset_xparts_0a(self):
# 0 length, many parts
in_val = [pandas.Series(), pandas.Series(), pandas.Series(), pandas.Series()]
in_val = [pandas.Series(name=('N', str(i))) for i in range(4)]
expected = pandas.DataFrame({str(i): pandas.Series() for i in range(4)})
offset_interval = 12.0
ind = FilterByOffsetIndexer(in_val, {u'quarterLength': offset_interval})
Expand All @@ -263,8 +273,9 @@ def test_offset_xparts_0a(self):

def test_offset_xparts_0b(self):
# 0 length, many parts, but one part has stuff
in_val = [pandas.Series(), pandas.Series(['a', 'b', 'c'], index=[0.0, 0.5, 1.0]),
pandas.Series(), pandas.Series()]
in_val = [pandas.Series(name=('N', '0')), pandas.Series(['a', 'b', 'c'], name=('N', '1'),
index=[0.0, 0.5, 1.0]),
pandas.Series(name=('N', '2')), pandas.Series(name=('N', '3'))]
expected = {str(i): pandas.Series() for i in [0, 2, 3]}
#print(str(expected)) # DEBUG
expected['1'] = pandas.Series(['a', 'b', 'c'], index=[0.0, 0.5, 1.0])
Expand All @@ -282,8 +293,8 @@ def test_offset_xparts_0b(self):

def test_offset_xparts_1(self):
# input is expected output; 2 parts
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.5, 1.0, 1.5]),
pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.5, 1.0, 1.5])]
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.5, 1.0, 1.5], name=('N', '0')),
pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.5, 1.0, 1.5], name=('N', '1'))]
expected = {'0': pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.5, 1.0, 1.5]),
'1':pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.5, 1.0, 1.5])}
expected = pandas.DataFrame(expected)
Expand All @@ -301,7 +312,7 @@ def test_offset_xparts_2(self):
# input is expected output; 10 parts
letters = ['a', 'b', 'c', 'd']
offsets = [0.0, 0.5, 1.0, 1.5]
in_val = [pandas.Series(letters, index=offsets) for _ in range(10)]
in_val = [pandas.Series(letters, index=offsets, name=('N', str(_))) for _ in range(10)]
expected = {str(i): pandas.Series(letters, index=offsets) for i in range(10)}
expected = pandas.DataFrame(expected)
offset_interval = 0.5
Expand All @@ -316,9 +327,9 @@ def test_offset_xparts_2(self):

def test_offset_xparts_3(self):
# irregular offset interval to 1.0; 3 parts, same offsets
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.4, 1.1, 2.1]),
pandas.Series(['q', 'w', 'e', 'r'], index=[0.0, 0.4, 1.1, 2.1]),
pandas.Series(['t', 'a', 'l', 'l'], index=[0.0, 0.4, 1.1, 2.1])]
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.4, 1.1, 2.1], name=('N', '0')),
pandas.Series(['q', 'w', 'e', 'r'], index=[0.0, 0.4, 1.1, 2.1], name=('N', '1')),
pandas.Series(['t', 'a', 'l', 'l'], index=[0.0, 0.4, 1.1, 2.1], name=('N', '2'))]
expected = {'0': pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 1.0, 2.0, 3.0]),
'1': pandas.Series(['q', 'w', 'e', 'r'], index=[0.0, 1.0, 2.0, 3.0]),
'2': pandas.Series(['t', 'a', 'l', 'l'], index=[0.0, 1.0, 2.0, 3.0])}
Expand All @@ -335,9 +346,9 @@ def test_offset_xparts_3(self):

def test_offset_xparts_4(self):
# irregular offset interval to 1.0; 3 parts, same offsets
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.4, 1.1, 2.9]),
pandas.Series(['q', 'w', 'e', 'r'], index=[0.0, 0.3, 1.4, 2.6]),
pandas.Series(['t', 'a', 'l', 'l'], index=[0.0, 0.2, 1.9, 2.555])]
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.4, 1.1, 2.9], name=('N', '0')),
pandas.Series(['q', 'w', 'e', 'r'], index=[0.0, 0.3, 1.4, 2.6], name=('N', '1')),
pandas.Series(['t', 'a', 'l', 'l'], index=[0.0, 0.2, 1.9, 2.555], name=('N', '2'))]
expected = {'0': pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 1.0, 2.0, 3.0]),
'1': pandas.Series(['q', 'w', 'e', 'r'], index=[0.0, 1.0, 2.0, 3.0]),
'2': pandas.Series(['t', 'a', 'l', 'l'], index=[0.0, 1.0, 2.0, 3.0])}
Expand All @@ -354,9 +365,9 @@ def test_offset_xparts_4(self):

def test_offset_xparts_5(self):
# irregular offset interval to 1.0; 3 parts, same offsets, one much longer
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.4, 1.1, 2.9]),
pandas.Series(['q', 'w', 'e', 'r'], index=[0.0, 0.3, 1.4, 2.6]),
pandas.Series(['t', 'a', 'l', 'l', 'o', 'r', 'd', 'e', 'r'],
in_val = [pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 0.4, 1.1, 2.9], name=('N', '0')),
pandas.Series(['q', 'w', 'e', 'r'], index=[0.0, 0.3, 1.4, 2.6], name=('N', '1')),
pandas.Series(['t', 'a', 'l', 'l', 'o', 'r', 'd', 'e', 'r'], name=('N', '2'),
index=[0.0, 0.2, 1.9, 2.5, 4.0, 5.0, 6.0, 7.0, 8.0])]
expected = {'0': pandas.Series(['a', 'b', 'c', 'd'], index=[0.0, 1.0, 2.0, 3.0]),
'1': pandas.Series(['q', 'w', 'e', 'r'], index=[0.0, 1.0, 2.0, 3.0]),
Expand Down

0 comments on commit 0b7c880

Please sign in to comment.