Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
PRIMO
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Admin message
Looking for advice? Join the
Matrix channel for GitLab users in Bielefeld
!
Show more breadcrumbs
Social Cognitive Systems
PRIMO
Commits
0fbbb4f6
Commit
0fbbb4f6
authored
12 years ago
by
Lukas Kettenbach
Browse files
Options
Downloads
Patches
Plain Diff
Some new graph methods
parent
05d1b792
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
ExampleBayesNet.py
+7
-1
7 additions, 1 deletion
ExampleBayesNet.py
core/BayesNet.py
+21
-6
21 additions, 6 deletions
core/BayesNet.py
core/__init__.py
+2
-0
2 additions, 0 deletions
core/__init__.py
core/tests/BayesNet_test.py
+25
-4
25 additions, 4 deletions
core/tests/BayesNet_test.py
with
55 additions
and
11 deletions
ExampleBayesNet.py
+
7
−
1
View file @
0fbbb4f6
...
@@ -18,10 +18,16 @@ ns = bn.get_nodes(["Node2","Node1"])
...
@@ -18,10 +18,16 @@ ns = bn.get_nodes(["Node2","Node1"])
for
n
in
ns
:
for
n
in
ns
:
print
n
.
name
print
n
.
name
print
bn
.
get_parents
(
n2
)
print
"
Removing existing edge
"
print
"
Removing existing edge
"
bn
.
remove_edge
(
n1
,
n2
)
bn
.
remove_edge
(
n1
,
n2
)
print
"
nach edge remove
"
print
bn
.
get_parents
(
n2
)
print
"
Removing not existing edge
"
print
"
Removing not existing edge
"
bn
.
remove_edge
(
n1
,
n2
)
bn
.
remove_edge
(
n1
,
n2
)
bn
.
draw
()
bn
.
draw
()
This diff is collapsed.
Click to expand it.
core/BayesNet.py
+
21
−
6
View file @
0fbbb4f6
...
@@ -31,7 +31,14 @@ class BayesNet(object):
...
@@ -31,7 +31,14 @@ class BayesNet(object):
raise
Exception
(
"
Tried to add an Edge between two Nodes of which at least one was not contained in the Bayesnet
"
)
raise
Exception
(
"
Tried to add an Edge between two Nodes of which at least one was not contained in the Bayesnet
"
)
def
remove_node
(
self
,
node
):
def
remove_node
(
self
,
node
):
raise
Exception
(
"
Called unimplemented function
"
)
if
node
.
name
not
in
self
.
node_lookup
.
keys
():
raise
Exception
(
"
Node
"
+
node
.
name
+
"
does not exists
"
)
else
:
try
:
self
.
graph
.
remove_node
(
node
)
except
nx
.
exception
.
NetworkXError
:
raise
Exception
(
"
Tried to remove a node which does not exists
"
)
del
self
.
node_lookup
[
node
.
name
]
def
remove_edge
(
self
,
node_from
,
node_to
):
def
remove_edge
(
self
,
node_from
,
node_to
):
try
:
try
:
...
@@ -44,22 +51,30 @@ class BayesNet(object):
...
@@ -44,22 +51,30 @@ class BayesNet(object):
try
:
try
:
return
self
.
node_lookup
[
node_name
]
return
self
.
node_lookup
[
node_name
]
except
KeyError
:
except
KeyError
:
raise
Exception
(
"
There is no node with name
"
+
node_name
+
"
in the bayesnet
"
)
raise
Exception
(
"
There is no node with name
"
+
node_name
+
"
in the bayesnet
"
)
def
get_nodes
(
self
,
node_names
):
def
get_nodes
(
self
,
node_names
):
nodes
=
[]
nodes
=
[]
if
not
node_names
:
if
not
node_names
:
nodes
=
self
.
bn
.
nodes
()
nodes
=
self
.
graph
.
nodes
()
else
:
else
:
for
node_name
in
node_names
:
for
node_name
in
node_names
:
nodes
.
append
(
self
.
get_node
(
node_name
))
nodes
.
append
(
self
.
get_node
(
node_name
))
return
nodes
return
nodes
def
get_parents
(
self
,
node
):
def
get_parents
(
self
,
node
):
raise
Exception
(
"
Called unimplemented function
"
)
if
node
.
name
not
in
self
.
node_lookup
.
keys
():
raise
Exception
(
"
Node
"
+
node
.
name
+
"
does not exists
"
)
else
:
return
self
.
graph
.
predecessors
(
node
)
def
get_children
(
self
,
node
):
def
get_children
(
self
,
node
):
raise
Exception
(
"
Called unimplemented function
"
)
if
node
.
name
not
in
self
.
node_lookup
.
keys
():
raise
Exception
(
"
Node
"
+
node
.
name
+
"
does not exists
"
)
else
:
return
self
.
graph
.
successors
(
node
)
def
get_markov_blanket
(
self
,
node
):
def
get_markov_blanket
(
self
,
node
):
raise
Exception
(
"
Called unimplemented function
"
)
raise
Exception
(
"
Called unimplemented function
"
)
...
@@ -70,4 +85,4 @@ class BayesNet(object):
...
@@ -70,4 +85,4 @@ class BayesNet(object):
def
draw
(
self
):
def
draw
(
self
):
import
matplotlib.pyplot
as
plt
import
matplotlib.pyplot
as
plt
nx
.
draw
(
self
.
graph
)
nx
.
draw
(
self
.
graph
)
plt
.
show
()
plt
.
show
()
This diff is collapsed.
Click to expand it.
core/__init__.py
+
2
−
0
View file @
0fbbb4f6
from
BayesNet
import
BayesNet
from
Node
import
Node
This diff is collapsed.
Click to expand it.
core/tests/BayesNet_test.py
+
25
−
4
View file @
0fbbb4f6
...
@@ -11,7 +11,7 @@ class NodeAddAndRemoveTestCase(unittest.TestCase):
...
@@ -11,7 +11,7 @@ class NodeAddAndRemoveTestCase(unittest.TestCase):
def
tearDown
(
self
):
def
tearDown
(
self
):
self
.
bn
=
None
self
.
bn
=
None
def
test_add
(
self
):
def
test_add
_node
(
self
):
n
=
Node
(
"
Some Node
"
)
n
=
Node
(
"
Some Node
"
)
self
.
bn
.
add_node
(
n
)
self
.
bn
.
add_node
(
n
)
self
.
assertEqual
(
n
,
self
.
bn
.
get_node
(
"
Some Node
"
))
self
.
assertEqual
(
n
,
self
.
bn
.
get_node
(
"
Some Node
"
))
...
@@ -20,12 +20,33 @@ class NodeAddAndRemoveTestCase(unittest.TestCase):
...
@@ -20,12 +20,33 @@ class NodeAddAndRemoveTestCase(unittest.TestCase):
node_with_same_name
=
Node
(
"
Some Node
"
)
node_with_same_name
=
Node
(
"
Some Node
"
)
self
.
assertRaises
(
Exception
,
self
.
bn
.
add_node
,
node_with_same_name
)
self
.
assertRaises
(
Exception
,
self
.
bn
.
add_node
,
node_with_same_name
)
def
test_remove
(
self
):
def
test_remove
_node
(
self
):
n
=
Node
(
"
Some Node to remove
"
)
n
=
Node
(
"
Some Node to remove
"
)
self
.
bn
.
add_node
(
n
)
self
.
bn
.
add_node
(
n
)
self
.
bn
.
remove_node
(
n
)
self
.
bn
.
remove_node
(
n
)
self
.
assertFalse
(
n
in
self
.
bn
.
get_nodes
())
self
.
assertFalse
(
n
in
self
.
bn
.
get_nodes
([]))
def
test_add_edge
(
self
):
n1
=
Node
(
"
1
"
)
n2
=
Node
(
"
2
"
)
self
.
bn
.
add_node
(
n1
)
self
.
bn
.
add_node
(
n2
)
self
.
bn
.
add_edge
(
n1
,
n2
)
self
.
assertTrue
(
n1
in
self
.
bn
.
get_parents
(
n2
))
self
.
assertTrue
(
n2
in
self
.
bn
.
get_children
(
n1
))
self
.
bn
.
remove_node
(
n1
)
self
.
bn
.
remove_node
(
n2
)
def
test_remove_edge
(
self
):
n1
=
Node
(
"
1
"
)
n2
=
Node
(
"
2
"
)
self
.
bn
.
add_node
(
n1
)
self
.
bn
.
add_node
(
n2
)
self
.
bn
.
add_edge
(
n1
,
n2
)
self
.
bn
.
remove_edge
(
n1
,
n2
)
self
.
assertEqual
([],
self
.
bn
.
get_parents
(
n2
))
self
.
bn
.
remove_node
(
n1
)
self
.
bn
.
remove_node
(
n2
)
#include this so you can run this test without nose
#include this so you can run this test without nose
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
...
...
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