Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
openstructure
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
schwede
openstructure
Commits
2c107d9f
Commit
2c107d9f
authored
14 years ago
by
Tobias Schmidt
Browse files
Options
Downloads
Patches
Plain Diff
added IsEmpty function to table class
parent
5d85966b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
modules/base/pymod/table.py
+44
-5
44 additions, 5 deletions
modules/base/pymod/table.py
modules/base/tests/test_table.py
+54
-0
54 additions, 0 deletions
modules/base/tests/test_table.py
with
98 additions
and
5 deletions
modules/base/pymod/table.py
+
44
−
5
View file @
2c107d9f
...
@@ -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
This diff is collapsed.
Click to expand it.
modules/base/tests/test_table.py
+
54
−
0
View file @
2c107d9f
...
@@ -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
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment