def test_settings_2(self, mock_ip): # - if index is None, field and value are valid, it'll set for all IPs test_wm = WorkflowManager([u'a', u'b', u'c']) self.assertEqual(3, mock_ip.call_count) # to make sure we're using the mock, not real IP test_wm.settings(None, u'filter repeats', True) for i in xrange(3): self.assertEqual(True, test_wm._settings[i][u'filter repeats'])
def test_intervs_1(self, mock_guc, mock_rfa, mock_rep): """Ensure _intervs() calls everything in the right order, with the right args & settings. This test uses all the default settings.""" test_settings = {'simple or compound': 'compound', 'quality': False} test_pieces = [MagicMock(spec_set=IndexedPiece) for _ in range(3)] returns = ['get_data() {}'.format(i) for i in range(len(test_pieces))] for piece in test_pieces: piece.get_data.side_effect = lambda *x: returns.pop(0) expected = ['get_data() {}'.format(i) for i in range(len(test_pieces))] exp_analyzers = [noterest.NoteRestIndexer, interval.IntervalIndexer] test_wc = WorkflowManager(test_pieces) test_wc.settings(None, 'include rests', True) actual = test_wc._intervs() # pylint: disable=protected-access self.assertEqual(0, mock_guc.call_count) self.assertEqual(len(test_pieces), len(expected), len(actual)) self.assertEqual(0, mock_rep.call_count) mock_rfa.assert_called_once_with('interval.IntervalIndexer') for piece in test_pieces: piece.get_data.assert_called_once_with(exp_analyzers, test_settings) for i in range(len(actual)): # NB: in real use, _run_freq_agg() would aggregate a piece's voice pairs and save it in # self._result... but since that method's mocked out, we have to check here the # return of each piece's get_data() call self.assertSequenceEqual(expected[i], actual[i])
def test_intervs_3(self, mock_guc, mock_rfa, mock_rep): """Ensure _intervs() calls everything in the right order, with the right args & settings. This uses the default *except* requires removing rests, so it's more complex.""" test_settings = {'simple or compound': 'compound', 'quality': False} test_pieces = [MagicMock(spec_set=IndexedPiece) for _ in range(3)] returns = [ pandas.DataFrame([pandas.Series(['3', 'Rest', '5'])], index=[['interval.IntervalIndexer'], ['0,1']]).T for i in range(len(test_pieces)) ] for piece in test_pieces: piece.get_data.side_effect = lambda *x: returns.pop(0) exp_series_ind = ('interval.IntervalIndexer', '0,1') expected_df = pandas.DataFrame( {exp_series_ind: pandas.Series(['3', '5'], index=[0, 2])}) exp_analyzers = [noterest.NoteRestIndexer, interval.IntervalIndexer] test_wc = WorkflowManager(test_pieces) test_wc.settings(None, 'count frequency', False) actual = test_wc._intervs() # pylint: disable=protected-access self.assertEqual(0, mock_guc.call_count) self.assertEqual(len(test_pieces), len(actual)) self.assertEqual(0, mock_rfa.call_count) self.assertEqual(0, mock_rep.call_count) for piece in test_pieces: piece.get_data.assert_called_once_with(exp_analyzers, test_settings) for i in range(len(actual)): self.assertSequenceEqual(list(expected_df.columns), list(actual[i].columns)) self.assertSequenceEqual(list(expected_df[exp_series_ind].index), list(actual[i][exp_series_ind].index)) self.assertSequenceEqual(list(expected_df[exp_series_ind].values), list(actual[i][exp_series_ind].values))
def test_intervs_3(self, mock_guc, mock_rfa, mock_rep): """Ensure _intervs() calls everything in the right order, with the right args & settings. This uses the default *except* requires removing rests, so it's more complex.""" test_settings = {'simple or compound': 'compound', 'quality': False} test_pieces = [MagicMock(spec_set=IndexedPiece) for _ in range(3)] returns = [pandas.DataFrame([pandas.Series(['3', 'Rest', '5'])], index=[['interval.IntervalIndexer'], ['0,1']]).T for i in range(len(test_pieces))] for piece in test_pieces: piece.get_data.side_effect = lambda *x: returns.pop(0) exp_series_ind = ('interval.IntervalIndexer', '0,1') expected_df = pandas.DataFrame({exp_series_ind: pandas.Series(['3', '5'], index=[0, 2])}) exp_analyzers = [noterest.NoteRestIndexer, interval.IntervalIndexer] test_wc = WorkflowManager(test_pieces) test_wc.settings(None, 'count frequency', False) actual = test_wc._intervs() # pylint: disable=protected-access self.assertEqual(0, mock_guc.call_count) self.assertEqual(len(test_pieces), len(actual)) self.assertEqual(0, mock_rfa.call_count) self.assertEqual(0, mock_rep.call_count) for piece in test_pieces: piece.get_data.assert_called_once_with(exp_analyzers, test_settings) for i in range(len(actual)): self.assertSequenceEqual(list(expected_df.columns), list(actual[i].columns)) self.assertSequenceEqual(list(expected_df[exp_series_ind].index), list(actual[i][exp_series_ind].index)) self.assertSequenceEqual(list(expected_df[exp_series_ind].values), list(actual[i][exp_series_ind].values))
def test_table_3(self): ''' _make_table(): - "count frequency" is False - file extension not on "pathname" - there are several IndexedPiece objects ''' test_wm = WorkflowManager([]) test_wm.settings(None, 'count frequency', False) test_wm._result = [mock.MagicMock(spec_set=pandas.DataFrame) for _ in range(5)] test_wm._data = ['boop' for _ in range(len(test_wm._result))] top_x = None threshold = None pathname = 'test_path' test_wm._make_table('CSV', pathname, top_x, threshold) # pylint: disable=protected-access test_wm._make_table('Excel', pathname, top_x, threshold) # pylint: disable=protected-access test_wm._make_table('Stata', pathname, top_x, threshold) # pylint: disable=protected-access test_wm._make_table('HTML', pathname, top_x, threshold) # pylint: disable=protected-access for i in range(len(test_wm._result)): test_wm._result[i].to_csv.assert_called_once_with('{}-{}{}'.format(pathname, i, '.csv')) test_wm._result[i].to_excel.assert_called_once_with('{}-{}{}'.format(pathname, i, '.xlsx')) test_wm._result[i].to_stata.assert_called_once_with('{}-{}{}'.format(pathname, i, '.dta')) test_wm._result[i].to_html.assert_called_once_with('{}-{}{}'.format(pathname, i, '.html'))
def test_table_1(self, mock_fdf): ''' _make_table(): - "count frequency" is True - file extension on "pathname" with period ''' test_wm = WorkflowManager([]) test_wm.settings(None, 'count frequency', True) # just to be 100% clear mock_fdf.return_value = mock.MagicMock(spec_set=pandas.DataFrame) test_wm._result = mock_fdf.return_value # to avoid a RuntimeError test_wm._previous_exp = 'intervals' # to get the proper "exp_name" top_x = None threshold = None exp_name = 'Interval Frequency' pathname = 'test_path' test_wm._make_table('CSV', pathname + '.csv', top_x, threshold) # pylint: disable=protected-access test_wm._make_table('Excel', pathname + '.xlsx', top_x, threshold) # pylint: disable=protected-access test_wm._make_table('Stata', pathname + '.dta', top_x, threshold) # pylint: disable=protected-access test_wm._make_table('HTML', pathname + '.html', top_x, threshold) # pylint: disable=protected-access mock_fdf.return_value.to_csv.assert_called_once_with('test_path.csv') mock_fdf.return_value.to_stata.assert_called_once_with('test_path.dta') mock_fdf.return_value.to_excel.assert_called_once_with('test_path.xlsx') mock_fdf.return_value.to_html.assert_called_once_with('test_path.html') self.assertSequenceEqual([mock.call(top_x=top_x, threshold=threshold, name=exp_name) for _ in range(4)], mock_fdf.call_args_list)
def test_lilypond_2(self): """one piece with one part; specified pathname; and there's a NaN in the Series!""" mock_ips = [MagicMock(spec_set=IndexedPiece)] mock_ips[0].get_data.return_value = ['ready for LilyPond'] result = [pandas.DataFrame({('analyzer', 'clarinet'): pandas.Series(range(10))})] result[0][('analyzer', 'clarinet')].iloc[0] = NaN exp_series = pandas.Series(range(1, 10), index=range(1, 10)) # to test dropna() was run pathname = 'this_path' expected = ['this_path.ly'] exp_get_data_calls = [mock.call([lilypond_ind.AnnotationIndexer, lilypond_exp.AnnotateTheNoteExperimenter, lilypond_exp.PartNotesExperimenter], {'part_names': ['analyzer: clarinet'], 'column': 'lilypond.AnnotationIndexer'}, [mock.ANY]), mock.call([lilypond_exp.LilyPondExperimenter], {'run_lilypond': True, 'annotation_part': ['ready for LilyPond'], 'output_pathname': expected[0]})] test_wm = WorkflowManager(mock_ips) test_wm._result = result test_wm.settings(None, 'count frequency', False) test_wm._make_lilypond(pathname) self.assertEqual(2, mock_ips[0].get_data.call_count) self.assertSequenceEqual(exp_get_data_calls, mock_ips[0].get_data.call_args_list) # check that dropna() was run actual_series = mock_ips[0].get_data.call_args_list[0][0][2][0] self.assertSequenceEqual(list(exp_series.index), list(actual_series.index)) self.assertSequenceEqual(list(exp_series.values), list(actual_series.values))
def test_lilypond_1b(self): """error conditions: if the lengths are different, (but 'count frequency' is okay)""" test_wm = WorkflowManager(['fake piece']) test_wm._data = ['fake IndexedPiece'] test_wm._result = ['fake results', 'more fake results', 'so many fake results'] test_wm.settings(None, 'count frequency', False) with self.assertRaises(RuntimeError) as run_err: test_wm._make_lilypond(['paths']) self.assertEqual(WorkflowManager._COUNT_FREQUENCY_MESSAGE, run_err.exception.args[0])
def test_settings_9(self, mock_ip): # - if trying to set 'offset interval' to 0, it should actually be set to None test_wm = WorkflowManager([u'a', u'b', u'c']) self.assertEqual(3, mock_ip.call_count) # to make sure we're using the mock, not real IP # "None" is default value, so first set to non-zero test_wm.settings(1, u'offset interval', 4.0) self.assertEqual(4.0, test_wm._settings[1][u'offset interval']) # now run our test test_wm.settings(1, u'offset interval', 0) self.assertEqual(None, test_wm._settings[1][u'offset interval'])
def test_lilypond_1b(self): # error conditions: if the lengths are different, (but 'count frequency' is okay) test_wm = WorkflowManager(['fake piece']) test_wm._data = ['fake IndexedPiece'] test_wm._result = ['fake results', 'more fake results', 'so many fake results'] test_wm.settings(None, 'count frequency', False) self.assertRaises(RuntimeError, test_wm._make_lilypond, ['paths']) try: test_wm._make_lilypond(['paths']) except RuntimeError as the_err: self.assertEqual(WorkflowManager._COUNT_FREQUENCY_MESSAGE, the_err.message)
def test_intervals_2(self): # test all combinations of bwv77 test_wm = WorkflowManager(['vis/tests/corpus/bwv77.mxl']) test_wm.load('pieces') test_wm.settings(0, 'voice combinations', 'all pairs') actual = test_wm.run('intervals') exp_ind = list(IntervalsTests.EXPECTED_2.index) act_ind = list(actual.index) for ind_item in exp_ind: self.assertTrue(ind_item in act_ind) for ind_item in exp_ind: self.assertEqual(IntervalsTests.EXPECTED_2[ind_item], actual[ind_item])
def test_intervals_3(self): # test all combinations of madrigal51 without rests test_wm = WorkflowManager(['vis/tests/corpus/madrigal51.mxl']) test_wm.load('pieces') test_wm.settings(0, 'voice combinations', 'all pairs') test_wm.settings(None, 'include rests', False) actual = test_wm.run('intervals') exp_ind = list(IntervalsTests.EXPECTED_3.index) act_ind = list(actual.index) for ind_item in exp_ind: self.assertTrue(ind_item in act_ind) for ind_item in exp_ind: self.assertEqual(IntervalsTests.EXPECTED_3[ind_item], actual[ind_item])
def test_run_off_rep_5(self, mock_off, mock_rep): # run neither indexer; input a dict # setup workm = WorkflowManager(['', '', '']) workm._data = [None, MagicMock(spec=IndexedPiece), None] workm.settings(1, 'offset interval', 0) workm.settings(1, 'filter repeats', False) in_val = {'b': 43, 'a': 42, 'c': 44} # run actual = workm._run_off_rep(1, in_val) # test self.assertSequenceEqual(in_val, actual) self.assertEqual(0, workm._data[1].get_data.call_count)
def test_ngrams_6(self): # test madrigal51 with all-voice 2-grams and rests test_wm = WorkflowManager(['vis/tests/corpus/madrigal51.mxl']) test_wm.settings(0, 'voice combinations', 'all') test_wm.settings(None, 'include rests', True) test_wm.load('pieces') actual = test_wm.run('interval n-grams') exp_ind = list(NGramsTests.EXPECTED_6.index) act_ind = list(actual.index) for ind_item in exp_ind: self.assertTrue(ind_item in act_ind) for ind_item in exp_ind: self.assertEqual(NGramsTests.EXPECTED_6[ind_item], actual[ind_item])
def test_ngrams_3(self): # test all voices of bwv77; 1-grams test_wm = WorkflowManager(['vis/tests/corpus/bwv77.mxl']) test_wm.load('pieces') test_wm.settings(0, 'voice combinations', 'all') test_wm.settings(0, 'n', 1) actual = test_wm.run('interval n-grams') exp_ind = list(NGramsTests.EXPECTED_3.index) act_ind = list(actual.index) for ind_item in exp_ind: self.assertTrue(ind_item in act_ind) for ind_item in exp_ind: self.assertEqual(NGramsTests.EXPECTED_3[ind_item], actual[ind_item])
def test_run_off_rep_1(self): """run neither indexer""" # setup workm = WorkflowManager(['', '', '']) workm._data = [None, MagicMock(spec=IndexedPiece), None] workm.settings(1, 'offset interval', 0) workm.settings(1, 'filter repeats', False) in_val = 42 # run actual = workm._run_off_rep(1, in_val) # test self.assertEqual(in_val, actual) self.assertEqual(0, workm._data[1].get_data.call_count)
def test_ngrams_8(self): # test_ngrams_7 *but* with part combinations specified rather than 'all pairs' test_wm = WorkflowManager(['vis/tests/corpus/vis_Test_Piece.xml']) test_wm.load('pieces') test_wm.settings(0, 'voice combinations', '[[0,1], [0,2], [0,3], [1,2], [1,3], [2,3]]') test_wm.settings(0, 'n', 2) actual = test_wm.run('interval n-grams') exp_ind = list(NGramsTests.EXPECTED_7.index) # yes, this *should* be EXPECTED_7 act_ind = list(actual.index) self.assertEqual(len(exp_ind), len(act_ind)) for ind_item in exp_ind: self.assertTrue(ind_item in act_ind) self.assertEqual(NGramsTests.EXPECTED_7[ind_item], actual[ind_item])
def test_ngrams_7(self): # test all two-part combinations of the test piece; 2-grams test_wm = WorkflowManager(['vis/tests/corpus/vis_Test_Piece.xml']) test_wm.load('pieces') test_wm.settings(0, 'voice combinations', 'all pairs') test_wm.settings(0, 'n', 2) actual = test_wm.run('interval n-grams') exp_ind = list(NGramsTests.EXPECTED_7.index) act_ind = list(actual.index) self.assertEqual(len(exp_ind), len(act_ind)) for ind_item in exp_ind: self.assertTrue(ind_item in act_ind) self.assertEqual(NGramsTests.EXPECTED_7[ind_item], actual[ind_item])
def test_run_off_rep_3(self, mock_off, mock_rep): # run repeat indexer # setup workm = WorkflowManager(['', '', '']) workm._data = [None, MagicMock(spec=IndexedPiece), None] workm._data[1].get_data.return_value = 24 workm.settings(1, 'offset interval', 0) workm.settings(1, 'filter repeats', True) in_val = 42 # run actual = workm._run_off_rep(1, in_val) # test self.assertEqual(workm._data[1].get_data.return_value, actual) workm._data[1].get_data.assert_called_once_with([mock_rep], {}, in_val)
def test_intervals_2(self): """test all combinations of bwv77""" test_wm = WorkflowManager([os.path.join(VIS_PATH, 'tests', 'corpus', 'bwv77.mxl')]) test_wm.load('pieces') test_wm.settings(0, 'voice combinations', 'all pairs') actual = test_wm.run('intervals') self.assertEqual(1, len(actual.columns)) actual = actual['aggregator.ColumnAggregator'] exp_ind = list(IntervalsTests.EXPECTED_2.index) act_ind = list(actual.index) for ind_item in exp_ind: self.assertTrue(ind_item in act_ind) for ind_item in exp_ind: self.assertEqual(IntervalsTests.EXPECTED_2[ind_item], actual[ind_item])
def test_interval_ngrams_2(self, mock_two, mock_all, mock_var, mock_rfa): # --> same as test_interval_ngrams_1(), but with "count frequency" set to False # 1.) prepare mocks ind_pieces = [MagicMock(spec=IndexedPiece) for _ in xrange(3)] mock_rfa.return_value = u'mock_rfa() return value' mock_two.return_value = [u'mock_two() return value'] mock_all.return_value = [u'mock_all() return value'] mock_var.return_value = [u'mock_var() return value'] expected = [mock_all.return_value, mock_two.return_value, mock_var.return_value] # 2.) run the test test_wm = WorkflowManager(ind_pieces) test_wm.settings(0, u'voice combinations', u'all') test_wm.settings(1, u'voice combinations', u'all pairs') test_wm.settings(2, u'voice combinations', u'[[0, 1]]') test_wm.settings(None, 'count frequency', False) actual = test_wm._interval_ngrams() # 3.) verify the mocks # NB: in actual use, _run_freq_agg() would have the final say on the value of # test_wm._result... but it's mocked out, which means we can test whether # _interval_ngrams() puts the right stuff there self.assertEqual(3, len(test_wm._result)) for ret_val in expected: self.assertTrue(ret_val in test_wm._result) mock_two.assert_called_once_with(1) mock_all.assert_called_once_with(0) mock_var.assert_called_once_with(2) self.assertEqual(0, mock_rfa.call_count) self.assertEqual(expected, actual) self.assertSequenceEqual(expected, test_wm._result)
def test_run_1(self): # properly deals with "intervals" experiment # also tests that the user can pass a custom string to the continuer setting mock_path = u'vis.workflow.WorkflowManager._intervs' with mock.patch(mock_path) as mock_meth: mock_meth.return_value = u'the final countdown' test_wc = WorkflowManager([]) test_wc._loaded = True test_wc.settings(None, 'continuer', 'Unisonus') test_wc.run(u'intervals') mock_meth.assert_called_once_with() self.assertEqual(mock_meth.return_value, test_wc._result) self.assertEqual(u'intervals', test_wc._previous_exp) self.assertEqual('Unisonus', test_wc.settings(None, 'continuer'))
def test_intervals_3(self): """test all combinations of madrigal51 without rests""" test_wm = WorkflowManager([os.path.join(VIS_PATH, 'tests', 'corpus', 'madrigal51.mxl')]) test_wm.load('pieces') test_wm.settings(0, 'voice combinations', 'all pairs') test_wm.settings(None, 'include rests', False) actual = test_wm.run('intervals') self.assertEqual(1, len(actual.columns)) actual = actual['aggregator.ColumnAggregator'] exp_ind = list(IntervalsTests.EXPECTED_3.index) act_ind = list(actual.index) for ind_item in exp_ind: self.assertTrue(ind_item in act_ind) for ind_item in exp_ind: self.assertEqual(IntervalsTests.EXPECTED_3[ind_item], actual[ind_item])
def test_run_off_rep_3(self): """run repeat indexer""" # setup workm = WorkflowManager(['', '', '']) workm._data = [None, MagicMock(spec=IndexedPiece), None] workm._data[1].get_data.return_value = 24 workm.settings(1, 'offset interval', 0) workm.settings(1, 'filter repeats', True) in_val = 42 exp_analyzers = [repeat.FilterByRepeatIndexer] # run actual = workm._run_off_rep(1, in_val) # test self.assertEqual(workm._data[1].get_data.return_value, actual) workm._data[1].get_data.assert_called_once_with(exp_analyzers, {}, in_val)
def test_intervals_2(self): """test all combinations of bwv77""" test_wm = WorkflowManager( [os.path.join(VIS_PATH, 'tests', 'corpus', 'bwv77.mxl')]) test_wm.load('pieces') test_wm.settings(0, 'voice combinations', 'all pairs') actual = test_wm.run('intervals') self.assertEqual(1, len(actual.columns)) actual = actual['aggregator.ColumnAggregator'] exp_ind = list(IntervalsTests.EXPECTED_2.index) act_ind = list(actual.index) for ind_item in exp_ind: self.assertTrue(ind_item in act_ind) for ind_item in exp_ind: self.assertEqual(IntervalsTests.EXPECTED_2[ind_item], actual[ind_item])
def test_intervals_6(self): # TODO: add a frequency-counted test """test Soprano and Alto of bwv77; with quality; not counting frequency""" # NB: the "expected" was hand-counted expected = pandas.read_csv(os.path.join(VIS_PATH, 'tests', 'expecteds', 'bwv77', 'SA_intervals.csv'), comment='#', index_col=0, header=[0, 1], quotechar="'") test_wm = WorkflowManager([os.path.join(VIS_PATH, 'tests', 'corpus', 'bwv77.mxl')]) test_wm.load('pieces') test_wm.settings(0, 'voice combinations', '[[0, 1]]') test_wm.settings(None, 'count frequency', False) test_wm.settings(None, 'interval quality', True) actual = test_wm.run('intervals') self.assertEqual(1, len(actual)) actual = actual[0].dropna() self.assertDataFramesEqual(expected, actual)
def test_run_off_rep_6(self, mock_off, mock_rep): # run offset indexer with is_horizontal set to True # setup workm = WorkflowManager(['', '', '']) workm._data = [None, MagicMock(spec=IndexedPiece), None] workm._data[1].get_data.return_value = 24 workm.settings(1, 'offset interval', 0.5) workm.settings(1, 'filter repeats', False) in_val = 42 # run actual = workm._run_off_rep(1, in_val, True) # test self.assertEqual(workm._data[1].get_data.return_value, actual) workm._data[1].get_data.assert_called_once_with([mock_off], {'quarterLength': 0.5, 'method': None}, in_val)
def test_run_off_rep_4(self, mock_off, mock_rep): # run offset and repeat indexer # setup workm = WorkflowManager(['', '', '']) workm._data = [None, MagicMock(spec=IndexedPiece), None] workm._data[1].get_data.return_value = 24 workm.settings(1, 'offset interval', 0.5) workm.settings(1, 'filter repeats', True) in_val = 42 # run actual = workm._run_off_rep(1, in_val) # test self.assertEqual(workm._data[1].get_data.return_value, actual) self.assertEqual(2, workm._data[1].get_data.call_count) workm._data[1].get_data.assert_any_call([mock_off], {'quarterLength': 0.5}, in_val) workm._data[1].get_data.assert_any_call([mock_rep], {}, workm._data[1].get_data.return_value)
def test_intervals_3(self): """test all combinations of madrigal51 without rests""" test_wm = WorkflowManager( [os.path.join(VIS_PATH, 'tests', 'corpus', 'madrigal51.mxl')]) test_wm.load('pieces') test_wm.settings(0, 'voice combinations', 'all pairs') test_wm.settings(None, 'include rests', False) actual = test_wm.run('intervals') self.assertEqual(1, len(actual.columns)) actual = actual['aggregator.ColumnAggregator'] exp_ind = list(IntervalsTests.EXPECTED_3.index) act_ind = list(actual.index) for ind_item in exp_ind: self.assertTrue(ind_item in act_ind) for ind_item in exp_ind: self.assertEqual(IntervalsTests.EXPECTED_3[ind_item], actual[ind_item])
def test_intervs_4(self): """Ensure _intervs() fails when given an impossible 'voice pair'.""" test_pieces = [MagicMock(spec_set=IndexedPiece) for _ in range(3)] returns = ['get_data() {}'.format(i) for i in range(len(test_pieces))] for piece in test_pieces: piece.get_data.side_effect = lambda *x: returns.pop(0) exp_err_msg = WorkflowManager._REQUIRE_PAIRS_ERROR.format(3) # pylint: disable=protected-access test_wc = WorkflowManager(test_pieces) test_wc.settings(None, 'include rests', True) test_wc.settings(None, 'voice combinations', '[[0, 1, 2]]') # that's not a pair! self.assertRaises(RuntimeError, test_wc._intervs) # pylint: disable=protected-access try: test_wc._intervs() # pylint: disable=protected-access except RuntimeError as run_err: self.assertEqual(exp_err_msg, run_err.args[0])
def test_run_2b(self): # same as 2a but 'interval quality' is set to False mock_path = u'vis.workflow.WorkflowManager._interval_ngrams' with mock.patch(mock_path) as mock_meth: mock_meth.return_value = u'the final countdown' test_wc = WorkflowManager([]) test_wc._loaded = True test_wc.settings(None, 'interval quality', False) def the_side_effect(): assert '1' == test_wc.settings(None, 'continuer') return mock.DEFAULT mock_meth.side_effect = the_side_effect test_wc.run(u'interval n-grams') mock_meth.assert_called_once_with() self.assertEqual(mock_meth.return_value, test_wc._result) self.assertEqual(u'interval n-grams', test_wc._previous_exp) self.assertEqual('dynamic quality', test_wc.settings(None, 'continuer'))
def test_run_off_rep_4(self): """run offset and repeat indexer""" # setup workm = WorkflowManager(['', '', '']) workm._data = [None, MagicMock(spec=IndexedPiece), None] workm._data[1].get_data.return_value = 24 workm.settings(1, 'offset interval', 0.5) workm.settings(1, 'filter repeats', True) in_val = 42 exp_an_off = [offset.FilterByOffsetIndexer] exp_an_rep = [repeat.FilterByRepeatIndexer] # run actual = workm._run_off_rep(1, in_val) # test self.assertEqual(workm._data[1].get_data.return_value, actual) self.assertEqual(2, workm._data[1].get_data.call_count) workm._data[1].get_data.assert_any_call(exp_an_off, {'quarterLength': 0.5}, in_val) workm._data[1].get_data.assert_any_call(exp_an_rep, {}, workm._data[1].get_data.return_value)
def test_intervals_5(self): """test all combinations of vis_Test_Piece""" test_wm = WorkflowManager([os.path.join(VIS_PATH, 'tests', 'corpus', 'vis_Test_Piece.xml')]) test_wm.load('pieces') test_wm.settings(0, 'voice combinations', 'all pairs') test_wm.settings(None, 'include rests', True) expected = pandas.read_csv(os.path.join(VIS_PATH, 'tests', 'corpus', 'test_intervals_5.csv'), index_col=0) actual = test_wm.run('intervals') self.assertEqual(1, len(actual.columns)) self.assertSequenceEqual(list(expected.columns), list(actual.columns)) for col_name in expected.columns: self.assertEqual(len(expected[col_name]), len(actual[col_name])) for each_interval in expected[col_name].index: # NOTE: for whatever reason, the "expected" file always imports with an Int64Index, # so .loc() won't find things properly unless we give the int64 index to the # expected and the str index to the actual self.assertEqual(expected[col_name].loc[each_interval], actual[col_name].loc[str(each_interval)])
def test_intervals_6(self): # TODO: add a frequency-counted test """test Soprano and Alto of bwv77; with quality; not counting frequency""" # NB: the "expected" was hand-counted expected = pandas.read_csv(os.path.join(VIS_PATH, 'tests', 'expecteds', 'bwv77', 'SA_intervals.csv'), comment='#', index_col=0, header=[0, 1], quotechar="'") test_wm = WorkflowManager( [os.path.join(VIS_PATH, 'tests', 'corpus', 'bwv77.mxl')]) test_wm.load('pieces') test_wm.settings(0, 'voice combinations', '[[0, 1]]') test_wm.settings(None, 'count frequency', False) test_wm.settings(None, 'interval quality', True) actual = test_wm.run('intervals') self.assertEqual(1, len(actual)) actual = actual[0].dropna() self.assertDataFramesEqual(expected, actual)
def test_intervals_5(self): """test all combinations of vis_Test_Piece""" test_wm = WorkflowManager( [os.path.join(VIS_PATH, 'tests', 'corpus', 'vis_Test_Piece.xml')]) test_wm.load('pieces') test_wm.settings(0, 'voice combinations', 'all pairs') test_wm.settings(None, 'include rests', True) expected = pandas.read_csv(os.path.join(VIS_PATH, 'tests', 'corpus', 'test_intervals_5.csv'), index_col=0) actual = test_wm.run('intervals') self.assertEqual(1, len(actual.columns)) self.assertSequenceEqual(list(expected.columns), list(actual.columns)) for col_name in expected.columns: self.assertEqual(len(expected[col_name]), len(actual[col_name])) for each_interval in expected[col_name].index: # NOTE: for whatever reason, the "expected" file always imports with an Int64Index, # so .loc() won't find things properly unless we give the int64 index to the # expected and the str index to the actual self.assertEqual(expected[col_name].loc[each_interval], actual[col_name].loc[str(each_interval)])
def test_intervs_2(self, mock_guc, mock_rfa, mock_rep): """Ensure _intervs() calls everything in the right order, with the right args & settings. Same as test_intervs_1() but: - calls _remove_extra_pairs(), and - doesn't call _run_freq_agg().""" mock_guc.return_value = [[0, 1]] voice_combos = str(mock_guc.return_value) exp_voice_combos = ['0,1'] test_settings = {'simple or compound': 'compound', 'quality': False} test_pieces = [MagicMock(spec_set=IndexedPiece) for _ in range(3)] returns = ['get_data() {}'.format(i) for i in range(len(test_pieces))] for piece in test_pieces: piece.get_data.side_effect = lambda *x: returns.pop(0) exp_into_mock_rep = [ 'get_data() {}'.format(i) for i in range(len(test_pieces)) ] mock_rep_returns = [ 'IndP-{} no pairs'.format(i) for i in range(len(test_pieces)) ] mock_rep.side_effect = lambda *x: mock_rep_returns.pop(0) expected = [ 'IndP-{} no pairs'.format(i) for i in range(len(test_pieces)) ] exp_analyzers = [noterest.NoteRestIndexer, interval.IntervalIndexer] exp_mock_guc = [mock.call(i) for i in range(len(test_pieces))] test_wc = WorkflowManager(test_pieces) test_wc.settings(None, 'include rests', True) test_wc.settings(None, 'count frequency', False) test_wc.settings(None, 'voice combinations', voice_combos) actual = test_wc._intervs() # pylint: disable=protected-access self.assertSequenceEqual(exp_mock_guc, mock_guc.call_args_list) self.assertEqual(len(test_pieces), len(expected), len(actual)) self.assertEqual(0, mock_rfa.call_count) self.assertEqual(len(test_pieces), mock_rep.call_count) for i in range(len(test_pieces)): mock_rep.assert_any_call(exp_into_mock_rep[i], exp_voice_combos) for piece in test_pieces: piece.get_data.assert_called_once_with(exp_analyzers, test_settings) for i in range(len(actual)): self.assertSequenceEqual(expected[i], actual[i])
class WorkflowWrapper(QAbstractTableModel): """ This is a wrapper class for the :class:`vis.workflow.WorkflowManager` that allows its use as a :class:`QAbstractTableModel` for PyQt4. This class only wraps :meth:`metadata` and :meth:`setting`, which are those methods needed by PyQt. For all other :class:`Workflowmanager` methods, access the internally-stored instance with :meth:`get_workflow_manager`. ** How to Use the WorkflowWrapper: ** The :class:`WorkflowWrapper` always returns valid values, and will not raise exceptions of its own. However, "valid" values are not always "correct" or "expected." We recommend you use the :class:`WorkflowWrapper` like this: #. Instantiate the object. #. Set the object as the model for its view. #. Call :meth:`insertRows` with the total number of pieces to be added. #. Call :meth:`setData` once per piece to set the pathname. #. Once each row has a pathname, the object will instantiate its internal :class:`WorkflowManager` instance and call its :meth:`load` method to import the score, run the :class:`NoteRestIndexer`, and save its metadata. #. Subsequent calls to :meth:`data` will return the most correct information available. ** How not to Use the WorkflowWrapper: ** We recommend you do not use the :class:`WorkflowWrapper` like this: * Do not add pieces by calling :meth:`insertRows` then :meth:`setData` with the pathname, then :meth:`insertRows` then :meth:`setData` with the pathname, and so on. In this case, the :class:`WorkflowWrapper` would create a new :class:`WorkflowManager` after each call to :meth:`setData`, which would import each piece many times. * Do not add pieces after you modify a metadata or setting field. When you add a piece, a new :class:`WorkflowManager` instance is created. The new instance replaces any customized settings and metadata with the default values. * Do not call :meth:`data` before you add the pathnames of all pieces. Real metadata is only available after the :class:`WorkflowManager` is created, which happens after all the pieces have a pathname. If you call :meth:`data` when there is no :class:`WorkflowManager`, the return value will always be ``None``. ** Columns in the Data Model: ** The :class:`WorkflowWrapper` creates a two-dimensional data model, where each row represents an :class:`IndexedPiece` stored in the :class:`WorkflowManager` and each column represents either a setting or a metadatum field. The following six fields are different for each piece, and should be displayed in the :class:`QTableView` widget: * filename * title * parts_list * offset_interval * parts_combinations * repeat_identical The :class:`WorkflowManager` additionally wraps these data fields, which are shared by all pieces, and will therefore not apear in the :class:`QTableView` widget: * quality * simple_ints Use class properties with those names to specify columns to :meth:`data` and :meth:`getData`. For example: >>> workm.data((0, WorkflowWrapper.title), Qt.DisplayRole) u'02_eleva' >>> workm.setData((0, WorkflowWrapper.title), u'Elevator Love Letter', Qt.EditRole) >>> workm.data((0, WorkflowWrapper.parts_list), Qt.DisplayRole) [u'Amy Milan', u'Torquil Campbell', u'Evan Cranley', u'Chris Seligman', u'Pat McGee'] """ # Public class variables to track which column has which data # NOTE: Update _num_cols whenever you change the number of columns, # since this variable is used by columnCount(). # NOTE: Update _header_names whenever you change the number or definition of # columns, since this variale is used by headerData(). _num_cols = 6 _header_names = [ 'Path', 'Title', 'List of Part Names', 'Offset Interval', 'Part Combinations', 'Repeat Identical' ] # displayed fields filename = 0 title = 1 parts_list = 2 offset_interval = 3 parts_combinations = 4 repeat_identical = 5 # non-displayed fields quality = 100 simple_ints = 101 # instead of DisplayRole; used to tell data() to return the list of part names as a list ListRole = 4000 # when a value hasn't been set, return a QVariant with this in it default_value = u'(unset)' def __init__(self, parent=QModelIndex()): """ Create a new :class:`WorkflowWrapper` instance. """ super(WorkflowWrapper, self).__init__() self._pathnames = [ ] # hold a list of pathnames for before the WorkflowManager self._workm = None # hold the WorkflowManager self._settings_changed = False # whether setData() was called (for settings_changed()) self._when_done_import = None # method to call when we're finished importing def rowCount(self, parent=QModelIndex()): """ Return the number of pieces in this list. If the internal :class:`WorkflowManager` exists, this is the number of piece stored there; otherwise it is the number of places for pathnames. :returns: The number of pieces in this list. :rtype: ``int`` """ if self._workm is None: return len(self._pathnames) else: return len(self._workm) def columnCount(self, parent=QModelIndex()): "Return the number of columns in this WorkflowWrapper." return WorkflowWrapper._num_cols def data(self, index, role): """ Get the data for the piece and metadatum or setting specified. Only the "parts_list" column responds to the WorkflowWrapper.ListRole, in which case a list of strings is returned instead of a comma-separated list of part names. :param index: The row-and-column index you wish to access. Either you can use a :class:`QModelIndex` or a 2-tuple where the first element is an ``int`` representing the index of the piece in the models, and the second element is one of the class properties described above in "Columns in the Data Model." :type index: :class:`QModelIndex` or 2-tuple of ``int`` :param role: Either Qt.DisplayRole or WorkflowWrapper.ListRole :type role: ``int`` :returns: The requested data or, if ``index`` or ``role`` is invalid, ``None``. :rtype: :class:`QVariant` .. note:: The method always returns a :class:`QVariant`. Access the Python object with the :meth:`toPyObject` method. .. note:: If the internal :class:`WorkflowManager` has not been instantiated, the return value is always ``None``. .. note:: This method never actually returns ``None``, but rather an empty :class:`QVariant` that will be ``None`` when you call :meth:`toPyObject` on it. """ if self._workm is None or (Qt.DisplayRole != role and WorkflowWrapper.ListRole != role): return QVariant() # Set the row and column row = None column = None if isinstance(index, QModelIndex): # if the QModelIndex is invalid, we won't bother with it if not index.isValid(): return QVariant() # otherwise, get the row and column from the QModelIndex row = index.row() column = index.column() else: row = index[0] column = index[1] # Verify the row and column if row >= self.rowCount() or column >= self._num_cols and \ (column != WorkflowWrapper.quality and column != WorkflowWrapper.simple_ints): return QVariant() post = None if Qt.DisplayRole == role: # displayed fields if WorkflowWrapper.filename == column: post = self._workm.metadata(row, u'pathname') elif WorkflowWrapper.title == column: post = self._workm.metadata(row, u'title') elif WorkflowWrapper.parts_list == column: post = u', '.join(self._workm.metadata(row, u'parts')) elif WorkflowWrapper.offset_interval == column: post = self._workm.settings(row, u'offset interval') elif WorkflowWrapper.parts_combinations == column: post = self._workm.settings(row, u'voice combinations') elif WorkflowWrapper.repeat_identical == column: # the wording in the GUI and WorkflowManager has opposite meanings post = not self._workm.settings(row, u'filter repeats') # non-displayed fields elif WorkflowWrapper.quality == column: post = self._workm.settings(None, u'interval quality') elif WorkflowWrapper.simple_ints == column: post = self._workm.settings(None, u'simple intervals') else: post = QVariant() elif WorkflowWrapper.ListRole == role: if WorkflowWrapper.parts_list == column: post = self._workm.metadata(row, u'parts') else: post = QVariant() else: post = QVariant() if not isinstance(post, QVariant): if post is None: post = WorkflowWrapper.default_value post = QVariant(post) return post def headerData(self, section, orientation, role): """ Return the column names for a WorkflowWrapper instance. Arguments: - section: the index of the column you want the name of - orientation: should be Qt.Horizontal; Qt.Vertical is ignored - role: should be Qt.DisplayRole; others are ignored If the section index is out of range, or the orientation or role is different than expected, an empty QVariant is returned. """ # All of the column titles are stored as class variables. I decided to # use the class name here, rather than "self," just in case they were # accidentally changed in this instance. We do not want to allow that. if Qt.Horizontal == orientation and Qt.DisplayRole == role and \ 0 <= section < WorkflowWrapper._num_cols: return WorkflowWrapper._header_names[section] else: return QVariant() def setData(self, index, value, role): """ Set the data for the piece and metadatum or setting specified. If the internal :class:`WorkflowManager` has not yet been created and this is the last pathname added, instantiate the :class:`WorkflowManager` and call :meth:`load`. :param index: The row-and-column index you wish to access. Either you can use a :class:`QModelIndex` or a 2-tuple where the first element is an ``int`` representing the index of the piece in the models, and the second element is one of the class properties described above in "Columns in the Data Model." :type index: :class:`QModelIndex` or 2-tuple of ``int`` :param value: The desired value of the setting or metadatum. If you submit a :class:`QVariant`, we will call :meth:`toPyObject` before sending to the :class:`WorkflowManager`. :type value: :class:`QVariant` or any :param role: This should be Qt.EditRole. :type role: :class:`EditRole` :returns: Whether the data was successfully set. :rtype: ``True`` or ``False`` .. note:: If the internal :class:`WorkflowManager` has not been instantiated, you can only set the ``pathname`` field. All other calls to :meth:`setData` will fail. """ if Qt.EditRole != role: return False # Set the row and column row = None column = None if isinstance(index, QModelIndex): # if the QModelIndex is invalid, we won't bother with it if not index.isValid(): return False # otherwise, get the row and column from the QModelIndex row = index.row() column = index.column() else: row = index[0] column = index[1] index = self.createIndex(row, column) # Verify the row and column if row >= self.rowCount() or column >= self._num_cols and \ (column != WorkflowWrapper.quality and column != WorkflowWrapper.simple_ints): return False set_val = value.toPyObject() if isinstance(value, QVariant) else value # ensure we're trying to set a valid thing if self._workm is None: if WorkflowWrapper.filename != column: return False else: self._pathnames[row] = set_val ch_ind_1, ch_ind_2 = None, None if all(self._pathnames): self._workm = WorkflowManager(self._pathnames) self._workm.load(u'pieces') # now that we imported, all the data's changed ch_ind_1 = self.createIndex(0, 0) ch_ind_2 = self.createIndex(len(self), self._num_cols - 1) # let the GUI know self._when_done_import() else: # only one cell has changed ch_ind_1 = ch_ind_2 = self.createIndex(row, column) self.dataChanged.emit(ch_ind_1, ch_ind_2) return True # displayed fields if WorkflowWrapper.filename == column: self._workm.metadata(row, u'pathname', set_val) elif WorkflowWrapper.title == column: self._workm.metadata(row, u'title', set_val) elif WorkflowWrapper.parts_list == column: self._workm.metadata(row, u'parts', set_val) elif WorkflowWrapper.offset_interval == column: self._workm.settings(row, u'offset interval', set_val) elif WorkflowWrapper.parts_combinations == column: self._workm.settings(row, u'voice combinations', set_val) elif WorkflowWrapper.repeat_identical == column: # the wording in the GUI and WorkflowManager has opposite meanings self._workm.settings(row, u'filter repeats', not set_val) # non-displayed fields elif WorkflowWrapper.quality == column: self._workm.settings(None, u'interval quality', set_val) elif WorkflowWrapper.simple_ints == column: self._workm.settings(None, u'simple intervals', set_val) else: return False self._settings_changed = True self.dataChanged.emit(index, index) return True def insertRows(self, row, count, parent=QModelIndex()): """ Append new rows to the data model. Yes---append, not insert. :param row: An argument that will be ignored. :type row: any :param count: The number of rows you want to append. :type count: ``int`` .. note:: If the internal :class:`WorkflowManager` already exists, it is destroyed and all metadata and settings are lost. .. note:: We recommend you add all the rows you will need before you call :meth:`setData` to set the pathnames of the newly-added rows. """ if self._workm is not None: self._workm = None self.beginInsertRows(parent, len(self._pathnames), len(self._pathnames) + count - 1) self._pathnames.extend([None for _ in xrange(count)]) self.endInsertRows() def removeRows(self, row, count, parent=QModelIndex()): """ This is the opposite of insertRows(), and the arguments work in the same way. """ pass def __len__(self): "Alias for rowCount()." return self.rowCount() def __getitem__(self, index): "It's __getitem__(), what do you want?!" return self._workm[index] def connect_workflow_signals(self, finished_import): """ Connect the WorkflowManager's PyQt signals to the methods or functions given here. "finished_import" is for the WorkM's "finished_import" signal. """ print('--> type: ' + str(finished_import)) # DEBUG self._when_done_import = finished_import #self._workm.finished_import.connect(finished_import) #pass def get_workflow_manager(self): """ Get the internal :class:`WorkflowManager` instance. """ return self._workm def settings_changed(self): """ Know whether there has been a call to :meth:`setData` since the last time this method was called. """ ret = self._settings_changed self._settings_changed = False return ret