def setUp(self): """ || 0| 1| 2| 3| ========================= 0 || 11| 12| 13| 14| 1 || 21| 22| 23| 24| 2 || 31| 32| 33| 34| """ self.a = Matrix( 3, 4, list(range(11, 15)) + list(range(21, 25)) + list(range(31, 35))) self.singlecol = Matrix(3, 1, [1, 2, 3]) self.singlerow = Matrix(1, 3, [1, 2, 3])
def test_simsilarity_arg_forwarding(self): with patch.object(HPOSet, '_sim_score', return_value=Matrix(1, 1, [1])) as mock_simscore: set1 = HPOSet([self.terms[0]]) set2 = HPOSet([self.terms[1]]) _ = set1.similarity(set2) mock_simscore.assert_called_once_with(set1, set2, '', '') mock_simscore.reset_mock() _ = set1.similarity(set2, 'foo') mock_simscore.assert_called_once_with(set1, set2, 'foo', '') mock_simscore.reset_mock() _ = set1.similarity(set2, kind='foo') mock_simscore.assert_called_once_with(set1, set2, 'foo', '') mock_simscore.reset_mock() _ = set1.similarity(set2, 'foo', 'bar') mock_simscore.assert_called_once_with(set1, set2, 'foo', 'bar') mock_simscore.reset_mock() _ = set1.similarity(set2, kind='foo', method='bar') mock_simscore.assert_called_once_with(set1, set2, 'foo', 'bar') mock_simscore.reset_mock() _ = set1.similarity(set2, method='bar') mock_simscore.assert_called_once_with(set1, set2, '', 'bar') mock_simscore.reset_mock() _ = set1.similarity(set2, None, 'bar') mock_simscore.assert_called_once_with(set1, set2, None, 'bar') mock_simscore.reset_mock()
def setUp(self): """ || 0| 1| 2| 3| ========================= 0 || 11| 12| 13| 14| 1 || 21| 22| 23| 24| 2 || 31| 32| 33| 34| """ # a will be edited self.a = Matrix( 3, 4, list(range(11, 15)) + list(range(21, 25)) + list(range(31, 35))) # b will be used as reference self.b = Matrix( 3, 4, list(range(11, 15)) + list(range(21, 25)) + list(range(31, 35)))
def _sim_score(set1: 'HPOSet', set2: 'HPOSet', kind: str = '', method: str = '') -> Matrix: """ Calculates similarity matrix between HPOSets .. warning:: This method should not be used by itself. Use :func:`pyhpo.set.HPOSet.similarity` instead. Parameters ---------- set1: HPOSet One HPOSet to measure the similarity from set2: HPOSet Another HPOSet to measure the similarity to kind: str Which kind of information content should be calculated. See :function:`pyhpo.HPOTerm.similarity_score` for options method: string The method to use to calculate the similarity. See :function:`pyhpo.HPOTerm.similarity_score` for options Returns ------- float The one-way similarity from one to the other HPOSet """ if not len(set1) or not len(set2): return Matrix(0, 0) scores = [] for set1_term in set1: for set2_term in set2: scores.append( set1_term.similarity_score(set2_term, kind, method)) return Matrix(len(set1), len(set2), scores)
def test_invalid_combine_method(self): with patch.object(HPOSet, '_sim_score', return_value=Matrix( 2, 4, [1, 0.5, 2, 4, 2, 3, 1, 1])) as mock_simscore: with self.assertRaises(RuntimeError) as context: set1 = HPOSet([self.terms[0]]) set2 = HPOSet([self.terms[1]]) _ = set1.similarity(set2, combine='invalid') mock_simscore.assert_called_once_with(set1, set2, '', '') self.assertEqual(str(context.exception), 'Invalid combine method specified')
def test_empty_sets(self): with patch.object(HPOSet, '_sim_score', return_value=Matrix(0, 0, [])) as mock_simscore: """ Row maxes: 4, 3 ==> 7 ==> 7 Col maxes: 2, 3, 2, 4 ==> 11 ==> 11 ==> 18 / 6 """ set1 = HPOSet([self.terms[0]]) set2 = HPOSet([self.terms[1]]) res = set1.similarity(set2, combine='BMA') mock_simscore.assert_called_once_with(set1, set2, '', '') self.assertEqual(res, 0)
def test_funSimMax(self): with patch.object(HPOSet, '_sim_score', return_value=Matrix( 2, 4, [1, 0.5, 2, 4, 2, 3, 1, 1])) as mock_simscore: """ Row maxes: 4, 3 ==> 7 ==> 7/2 = 3.5 Col maxes: 2, 3, 2, 4 ==> 11 ==> 11/4 = 2.75 """ set1 = HPOSet([self.terms[0]]) set2 = HPOSet([self.terms[1]]) res = set1.similarity(set2, combine='funSimMax') mock_simscore.assert_called_once_with(set1, set2, '', '') self.assertEqual(res, 3.5)
def test_init(self): x = Matrix(2, 3) self.assertEqual(x._data, [None] * 6) self.assertEqual(x.n_rows, 2) self.assertEqual(x.n_cols, 3)
def test_string(self): a = Matrix(2, 3, range(6)) self.assertEqual( str(a), ' || 0| 1| 2|\n=================\n0 || 0| 1| 2|\n1 || 3| 4| 5|' # noqa: E501 )
def test_init_error_checking(self): with self.assertRaises(RuntimeError) as context: a = Matrix(2, 4, [1, 2, 3]) # noqa: F841 self.assertEqual('Wrong number of data items in `data`', str(context.exception))