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
Show more breadcrumbs
Social Cognitive Systems
PRIMO
Commits
e2e84ac0
Commit
e2e84ac0
authored
12 years ago
by
mbaumBielefeld
Browse files
Options
Downloads
Patches
Plain Diff
Bayesnet um Basisfunktionalität erweitert
parent
9e9ecb4f
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
ExampleBayesNet.py
+22
-3
22 additions, 3 deletions
ExampleBayesNet.py
core/BayesNet.py
+56
-5
56 additions, 5 deletions
core/BayesNet.py
core/Node.py
+4
-1
4 additions, 1 deletion
core/Node.py
with
82 additions
and
9 deletions
ExampleBayesNet.py
+
22
−
3
View file @
e2e84ac0
...
...
@@ -2,7 +2,26 @@ from core.BayesNet import *
from
core.Node
import
*
bn
=
BayesNet
()
n
=
Node
()
n1
=
Node
(
"
Node1
"
)
n2
=
Node
(
"
Node2
"
)
n3
=
Node
(
"
Node3
"
)
bn
.
add_node
(
n
)
bn
.
add_node
(
"
lol
"
)
bn
.
add_node
(
n1
)
bn
.
add_node
(
n2
)
bn
.
add_edge
(
n1
,
n2
)
n
=
bn
.
get_node
(
"
Node1
"
)
print
n
.
name
ns
=
bn
.
get_nodes
([
"
Node2
"
,
"
Node1
"
])
for
n
in
ns
:
print
n
.
name
print
"
Removing existing edge
"
bn
.
remove_edge
(
n1
,
n2
)
print
"
Removing not existing edge
"
bn
.
remove_edge
(
n1
,
n2
)
bn
.
draw
()
This diff is collapsed.
Click to expand it.
core/BayesNet.py
+
56
−
5
View file @
e2e84ac0
import
sys
sys
.
path
.
append
(
"
../lib/networkx-1.7-py2.7.egg
"
)
sys
.
path
.
append
(
"
lib/networkx-1.7-py2.7.egg
"
)
import
networkx
as
nx
import
Node.
Node
from
core.Node
import
Node
class
BayesNet
(
object
):
graph
=
nx
.
DiGraph
()
node_lookup
=
{}
def
__init__
(
self
):
p
rint
"
lol
"
p
ass
def
add_node
(
self
,
node
):
if
isinstance
(
node
,
Node
.
Node
):
if
isinstance
(
node
,
Node
):
if
node
.
name
in
self
.
node_lookup
.
keys
():
raise
Exception
(
"
Node name already exists in Bayesnet:
"
+
node
.
name
)
self
.
node_lookup
[
node
.
name
]
=
node
self
.
graph
.
add_node
(
node
)
else
:
raise
Exception
(
"
Can only add
'
Node
'
and its subclasses as nodes into the BayesNet
"
)
def
add_edge
(
self
,
node_from
,
node_to
):
if
node_from
in
self
.
graph
.
nodes
()
and
node_to
in
self
.
graph
.
nodes
():
self
.
graph
.
add_edge
(
node_from
,
node_to
)
#raise Exception("Fixme: Adapt CPD of child-node")
else
:
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
):
raise
Exception
(
"
Called unimplemented function
"
)
def
remove_edge
(
self
,
node_from
,
node_to
):
try
:
self
.
graph
.
remove_edge
(
node_from
,
node_to
)
except
nx
.
exception
.
NetworkXError
:
raise
Exception
(
"
Tried to remove an Edge which does not exist in the BayesNet
"
)
#raise Exception("Fixme: Adapt CPD of child-node")
def
get_node
(
self
,
node_name
):
try
:
return
self
.
node_lookup
[
node_name
]
except
KeyError
:
raise
Exception
(
"
There is no node with name
"
+
node_name
+
"
in the bayesnet
"
)
def
get_nodes
(
self
,
node_names
):
for
node_name
in
node_names
:
yield
self
.
get_node
(
node_name
)
def
get_parents
(
self
,
node
):
raise
Exception
(
"
Called unimplemented function
"
)
def
get_children
(
self
,
node
):
raise
Exception
(
"
Called unimplemented function
"
)
def
get_markov_blanket
(
self
,
node
):
raise
Exception
(
"
Called unimplemented function
"
)
def
is_dag
(
self
):
raise
Exception
(
"
Called unimplemented function
"
)
def
draw
(
self
):
import
matplotlib.pyplot
as
plt
nx
.
draw
(
self
.
graph
)
plt
.
show
()
This diff is collapsed.
Click to expand it.
core/Node.py
+
4
−
1
View file @
e2e84ac0
class
Node
(
object
):
name
=
"
SomeNode
"
name
=
"
UninitializedName
"
def
__init__
(
self
,
node_name
):
self
.
name
=
node_name
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