Skip to content
Snippets Groups Projects
Commit 2c107d9f authored by Tobias Schmidt's avatar Tobias Schmidt
Browse files

added IsEmpty function to table class

parent 5d85966b
No related branches found
No related tags found
No related merge requests found
...@@ -823,14 +823,17 @@ class Table: ...@@ -823,14 +823,17 @@ class Table:
except: except:
return None return None
def Count(self, col): def Count(self, col, ignore_nan=True):
""" """
Count the number of cells in column that are not equal to None. Count the number of cells in column that are not equal to None.
""" """
count=0 count=0
idx=self.GetColIndex(col) idx=self.GetColIndex(col)
for r in self.rows: for r in self.rows:
if r[idx]!=None: if ignore_nan:
if r[idx]!=None:
count+=1
else:
count+=1 count+=1
return count return count
...@@ -1095,8 +1098,42 @@ class Table: ...@@ -1095,8 +1098,42 @@ class Table:
LogError("Function needs numpy, but I could not import it.") LogError("Function needs numpy, but I could not import it.")
raise raise
def IsEmpty(self, col_name=None, ignore_nan=True):
'''
Checks if a table is empty.
If no column name is specified, the whole table is checked for being empty,
whereas if a column name is specified, only this column is checked.
By default, all NAN (or None) values are ignored, and thus, a table
containing only NAN values is considered as empty. By specifying the
option ignore_nan=False, NAN values are counted as 'normal' values.
'''
# table with no columns and no rows
if len(self.col_names)==0:
if col_name:
raise ValueError('Table has no column named "%s"' % col_name)
return True
# column name specified
if col_name:
if self.Count(col_name, ignore_nan=ignore_nan)==0:
return True
else:
return False
# no column name specified -> test whole table
else:
for row in self.rows:
for cell in row:
if ignore_nan:
if cell!=None:
return False
else:
return False
return True
def Merge(table1, table2, by, only_matching=False): def Merge(table1, table2, by, only_matching=False):
""" """
...@@ -1195,4 +1232,6 @@ def Merge(table1, table2, by, only_matching=False): ...@@ -1195,4 +1232,6 @@ def Merge(table1, table2, by, only_matching=False):
for common1_index, common2_index in zip(common1_indices, common2_indices): for common1_index, common2_index in zip(common1_indices, common2_indices):
row[common1_index]=v[common2_index] row[common1_index]=v[common2_index]
new_tab.AddRow(row) new_tab.AddRow(row)
return new_tab return new_tab
\ No newline at end of file
\ No newline at end of file
...@@ -131,6 +131,7 @@ class TestTable(unittest.TestCase): ...@@ -131,6 +131,7 @@ class TestTable(unittest.TestCase):
tab = Table() tab = Table()
self.CompareColCount(tab, 0) self.CompareColCount(tab, 0)
self.CompareRowCount(tab, 0) self.CompareRowCount(tab, 0)
self.assertRaises(ValueError, tab.GetColIndex, 'a')
def testTableInitSingleColEmpty(self): def testTableInitSingleColEmpty(self):
''' '''
...@@ -831,6 +832,10 @@ class TestTable(unittest.TestCase): ...@@ -831,6 +832,10 @@ class TestTable(unittest.TestCase):
self.assertEquals(tab.Count('second'),2) self.assertEquals(tab.Count('second'),2)
self.assertEquals(tab.Count('third'),2) self.assertEquals(tab.Count('third'),2)
self.assertEquals(tab.Count('fourth'),3) self.assertEquals(tab.Count('fourth'),3)
self.assertEquals(tab.Count('first', ignore_nan=False),3)
self.assertEquals(tab.Count('second', ignore_nan=False),3)
self.assertEquals(tab.Count('third', ignore_nan=False),3)
self.assertEquals(tab.Count('fourth', ignore_nan=False),3)
self.assertRaises(ValueError,tab.Count,'fifth') self.assertRaises(ValueError,tab.Count,'fifth')
def testCalcEnrichment(self): def testCalcEnrichment(self):
...@@ -974,6 +979,55 @@ class TestTable(unittest.TestCase): ...@@ -974,6 +979,55 @@ class TestTable(unittest.TestCase):
self.assertRaises(RuntimeError, tab.GetOptimalPrefactors, 'c','a','b',weight='d') self.assertRaises(RuntimeError, tab.GetOptimalPrefactors, 'c','a','b',weight='d')
self.assertRaises(RuntimeError, tab.GetOptimalPrefactors, 'c',weights='d') self.assertRaises(RuntimeError, tab.GetOptimalPrefactors, 'c',weights='d')
def testIsEmpty(self):
tab = Table()
self.assertTrue(tab.IsEmpty())
self.assertTrue(tab.IsEmpty(ignore_nan=False))
self.assertRaises(ValueError, tab.IsEmpty, 'a')
# empty table
tab = Table(['a','b','c'], 'fff')
self.assertTrue(tab.IsEmpty())
self.assertTrue(tab.IsEmpty('a'))
self.assertTrue(tab.IsEmpty('b'))
self.assertTrue(tab.IsEmpty('c'))
self.assertTrue(tab.IsEmpty(ignore_nan=False))
self.assertTrue(tab.IsEmpty('a', ignore_nan=False))
self.assertTrue(tab.IsEmpty('b', ignore_nan=False))
self.assertTrue(tab.IsEmpty('c', ignore_nan=False))
self.assertRaises(ValueError, tab.IsEmpty, 'd')
# fill row with NAN values
tab.AddRow([None,None,None])
self.assertTrue(tab.IsEmpty())
self.assertTrue(tab.IsEmpty('a'))
self.assertTrue(tab.IsEmpty('b'))
self.assertTrue(tab.IsEmpty('c'))
self.assertFalse(tab.IsEmpty(ignore_nan=False))
self.assertFalse(tab.IsEmpty('a', ignore_nan=False))
self.assertFalse(tab.IsEmpty('b', ignore_nan=False))
self.assertFalse(tab.IsEmpty('c', ignore_nan=False))
# fill some values into column 'c' only
tab.AddRow([None,None,1.0])
self.assertFalse(tab.IsEmpty())
self.assertTrue(tab.IsEmpty('a'))
self.assertTrue(tab.IsEmpty('b'))
self.assertFalse(tab.IsEmpty('c'))
self.assertFalse(tab.IsEmpty('a', ignore_nan=False))
self.assertFalse(tab.IsEmpty('b', ignore_nan=False))
self.assertFalse(tab.IsEmpty('c', ignore_nan=False))
# fill some values into all columns
tab.AddRow([2.0,3.0,1.0])
self.assertFalse(tab.IsEmpty())
self.assertFalse(tab.IsEmpty('a'))
self.assertFalse(tab.IsEmpty('b'))
self.assertFalse(tab.IsEmpty('c'))
self.assertFalse(tab.IsEmpty('a', ignore_nan=False))
self.assertFalse(tab.IsEmpty('b', ignore_nan=False))
self.assertFalse(tab.IsEmpty('c', ignore_nan=False))
if __name__ == "__main__": if __name__ == "__main__":
try: try:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment