diff --git a/modules/base/pymod/table.py b/modules/base/pymod/table.py index 8697d63d08eb8e6f3101002a2973663981a99675..4277edf949edaab289882cf9bd2cd9b66dc915bb 100644 --- a/modules/base/pymod/table.py +++ b/modules/base/pymod/table.py @@ -2348,16 +2348,19 @@ Statistics for column %(col)s if file_opened: stream.close() - - def GetNumpyMatrix(self, *args): + def GetNumpyMatrixAsArray(self, *args): ''' - Returns a numpy matrix containing the selected columns from the table as - columns in the matrix. + Returns a numpy array containing the selected columns from the table as + columns as a matrix. Only columns of type *int* or *float* are supported. *NA* values in the table will be converted to *None* values. - :param \*args: column names to include in numpy matrix + Originally the function used the numpy matrix class but that is going to be + deprecated in the future. Numpy itself suggests replacing numpy matrix by + numpy array. + + :param \*args: column names to include in numpy array :warning: The function depends on *numpy* ''' @@ -2372,16 +2375,40 @@ Statistics for column %(col)s idx = self.GetColIndex(arg) col_type = self.col_types[idx] if col_type!='int' and col_type!='float': - raise TypeError("Numpy matrix can only be generated from numeric column types") + raise TypeError("Numpy matrix can only be generated from numeric "+\ + "column types") idxs.append(idx) - m = np.matrix([list(self[i]) for i in idxs]) - return m.T + + a = np.array([list(self[i]) for i in idxs]) + return a.T except ImportError: LogError("Function needs numpy, but I could not import it.") raise - + def GetNumpyMatrix(self, *args): + ''' + *Caution*: Numpy is deprecating the use of the numpy matrix class. + + Returns a numpy matrix containing the selected columns from the table as + columns in the matrix. + + Only columns of type *int* or *float* are supported. *NA* values in the + table will be converted to *None* values. + + :param \*args: column names to include in numpy matrix + + :warning: The function depends on *numpy* + ''' + LogWarning("table.GetNumpyMatrix is deprecated, please use "+ + "table.GetNumpyMatrixAsArray instead") + try: + import numpy as np + m = self.GetNumpyMatrixAsArray(*args) + return np.matrix(m) + except ImportError: + LogError("Function needs numpy, but I could not import it.") + raise def GaussianSmooth(self, col, std=1.0, na_value=0.0, padding='reflect', c=0.0): @@ -2502,20 +2529,20 @@ Statistics for column %(col)s if len(args)==0: raise RuntimeError("At least one column must be specified.") - b = self.GetNumpyMatrix(ref_col) - a = self.GetNumpyMatrix(*args) - + b = self.GetNumpyMatrixAsArray(ref_col) + a = self.GetNumpyMatrixAsArray(*args) + if len(kwargs)!=0: if 'weights' in kwargs: - w = self.GetNumpyMatrix(kwargs['weights']) + w = self.GetNumpyMatrixAsArray(kwargs['weights']) b = np.multiply(b,w) a = np.multiply(a,w) else: raise RuntimeError("specified unrecognized kwargs, use weights as key") - k = (a.T*a).I*a.T*b - return list(np.array(k.T).reshape(-1)) + k = np.linalg.inv(a.T@a)@a.T@b + return list(k.T.reshape(-1)) except ImportError: LogError("Function needs numpy, but I could not import it.") @@ -3202,3 +3229,5 @@ def Merge(table1, table2, by, only_matching=False): new_tab.AddRow(row) return new_tab + +# LocalWords: numpy Numpy diff --git a/modules/base/tests/test_table.py b/modules/base/tests/test_table.py index 5d56f67ee49732ce6f875ecfc169908162d81d5f..52dee4de4d2dea7aba14fea4f299047156c7f0d6 100644 --- a/modules/base/tests/test_table.py +++ b/modules/base/tests/test_table.py @@ -1472,7 +1472,7 @@ class TestTable(unittest.TestCase): mcc = tab.ComputeMCC(score_col='prediction2', class_col='reference') self.assertAlmostEqual(mcc, 0.882089673321) - def testTableAsNumpyMatrix(self): + def testTableAsNumpyMatrixAsArray(self): if not HAS_NUMPY: return @@ -1488,24 +1488,24 @@ class TestTable(unittest.TestCase): tab = self.CreateTestTable() tab.AddCol('fourth','b',[True, False, False]) - m = tab.GetNumpyMatrix('second') - mc = np.matrix([[3],[None],[9]]) + m = tab.GetNumpyMatrixAsArray('second') + mc = np.array([[3],[None],[9]]) self.assertTrue(np.all(m==mc)) - mc = np.matrix([[3],[None],[10]]) + mc = np.array([[3],[None],[10]]) self.assertFalse(np.all(m==mc)) - m = tab.GetNumpyMatrix('third') - mc = np.matrix([[None],[2.200],[3.300]]) + m = tab.GetNumpyMatrixAsArray('third') + mc = np.array([[None],[2.200],[3.300]]) self.assertTrue(np.all(m==mc)) - m = tab.GetNumpyMatrix('second','third') - mc = np.matrix([[3, None],[None, 2.200],[9, 3.300]]) + m = tab.GetNumpyMatrixAsArray('second','third') + mc = np.array([[3, None],[None, 2.200],[9, 3.300]]) self.assertTrue(np.all(m==mc)) - m = tab.GetNumpyMatrix('third','second') - mc = np.matrix([[None, 3],[2.200, None],[3.300, 9]]) + m = tab.GetNumpyMatrixAsArray('third','second') + mc = np.array([[None, 3],[2.200, None],[3.300, 9]]) self.assertTrue(np.all(m==mc)) - self.assertRaises(TypeError, tab.GetNumpyMatrix, 'fourth') - self.assertRaises(TypeError, tab.GetNumpyMatrix, 'first') - self.assertRaises(RuntimeError, tab.GetNumpyMatrix) + self.assertRaises(TypeError, tab.GetNumpyMatrixAsArray, 'fourth') + self.assertRaises(TypeError, tab.GetNumpyMatrixAsArray, 'first') + self.assertRaises(RuntimeError, tab.GetNumpyMatrixAsArray) def testOptimalPrefactors(self): if not HAS_NUMPY: