From b4928f2beb9d89cfc2a4fdf1ee2ca1b11b03f162 Mon Sep 17 00:00:00 2001 From: Hendrik Buschmeier <hbuschme@uni-bielefeld.de> Date: Sat, 5 Apr 2014 00:41:18 +0200 Subject: [PATCH] Refactoring: Adapted imports. --- primo/densities.py | 7 +++--- primo/networks.py | 36 +++++++++++++++---------------- primo/nodes.py | 13 ++++++----- primo/reasoning/particlefilter.py | 7 +++--- 4 files changed, 31 insertions(+), 32 deletions(-) diff --git a/primo/densities.py b/primo/densities.py index 06c9268..f2e608f 100644 --- a/primo/densities.py +++ b/primo/densities.py @@ -7,8 +7,7 @@ import random import numpy from scipy.stats -from primo.nodes import ContinuousNode - +from primo.nodes class Density(object): '''TODO: write doc''' @@ -46,6 +45,7 @@ class ExponentialParameters(object): #a dict (node,coefficient) that holds the weights that define the depency on each node self.b=b + class GaussParameters(object): ''' This represents the parameters for the Gauss-density class. @@ -58,6 +58,7 @@ class GaussParameters(object): #the variance self.var=var + class NDGaussParameters(object): def __init__(self, mu, cov): self.mu=mu @@ -591,7 +592,7 @@ class Gauss(Density): def add_variable(self, variable): - if not isinstance(variable, ContinuousNode): + if not isinstance(variable, primo.nodes.ContinuousNode): raise Exception("Tried to add Variable into Gaussian densitiy, but variable is not continuous") self.b[variable]=0.0 diff --git a/primo/networks.py b/primo/networks.py index 74ee197..2d7d828 100644 --- a/primo/networks.py +++ b/primo/networks.py @@ -2,7 +2,7 @@ import networkx as nx import primo.nodes -class BayesNet(object): +class BayesianNetwork(object): def __init__(self): self.graph = nx.DiGraph() @@ -15,7 +15,7 @@ class BayesNet(object): 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") + raise Exception("Can only add 'Node' and its subclasses as nodes into the BayesianNetwork") def add_edge(self, node_from, node_to): if node_from in self.graph.nodes() and node_to in self.graph.nodes(): @@ -38,14 +38,14 @@ class BayesNet(object): 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("Tried to remove an edge which does not exist in the BayesianNetwork") #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") + raise Exception("There is no node with name "+node_name+" in the BayesianNetwork") def get_all_nodes(self): return self.graph.nodes() @@ -137,7 +137,7 @@ class BayesNet(object): return len(self.graph) -class BayesianDecisionNetwork(BayesNet): +class BayesianDecisionNetwork(BayesianNetwork): def __init__(self): super(BayesianDecisionNetwork, self).__init__() @@ -180,7 +180,7 @@ class BayesianDecisionNetwork(BayesNet): 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") + raise Exception("Can only add 'Node' and its subclasses as nodes into the BayesianNetwork") def get_all_nodes(self): '''Returns all RandomNodes''' @@ -234,7 +234,7 @@ class BayesianDecisionNetwork(BayesNet): self.partialOrdering = partialOrder -class DynamicBayesNet(BayesNet): +class DynamicBayesianNetwork(BayesianNetwork): ''' This is the implementation of a dynamic Bayesian network (also called temporal Bayesian network). @@ -248,8 +248,8 @@ class DynamicBayesNet(BayesNet): ''' def __init__(self): - super(DynamicBayesNet, self).__init__() - self._B0 = BayesNet() + super(DynamicBayesianNetwork, self).__init__() + self._B0 = BayesianNetwork() self._twoTBN = TwoTBN() @property @@ -260,12 +260,12 @@ class DynamicBayesNet(BayesNet): @B0.setter def B0(self, value): ''' Set the Bayesian network representing the initial distribution.''' - if isinstance(value, BayesNet): + if isinstance(value, BayesianNetwork): if not value.is_valid(): - raise Exception("BayesNet is not valid.") + raise Exception("BayesianNetwork is not valid.") self._B0 = value else: - raise Exception("Can only set 'BayesNet' and its subclasses as " + + raise Exception("Can only set 'BayesianNetwork' and its subclasses as " + "B0 of a DBN.") @property @@ -278,7 +278,7 @@ class DynamicBayesNet(BayesNet): ''' Set the 2-time-slice Bayesian network.''' if isinstance(value, TwoTBN): if not value.is_valid(): - raise Exception("BayesNet is not valid.") + raise Exception("BayesianNetwork is not valid.") self._twoTBN = value else: raise Exception("Can only set 'TwoTBN' and its subclasses as " + @@ -294,18 +294,18 @@ class DynamicBayesNet(BayesNet): " not found in TwoTBN!") return False; - return super(DynamicBayesNet, self).is_valid() + return super(DynamicBayesianNetwork, self).is_valid() -class TwoTBN(BayesNet): +class TwoTBN(BayesianNetwork): ''' This is the implementation of a 2-time-slice Bayesian network (2-TBN). ''' def __init__(self, bayesnet=None): - BayesNet.__init__(self) + BayesianNetwork.__init__(self) if bayesnet: - if not isinstance(bayesnet, BayesNet): - raise Exception("Parameter 'bayesnet' is not a instance of class BayesNet.") + if not isinstance(bayesnet, BayesianNetwork): + raise Exception("Parameter 'bayesnet' is not a instance of class BayesianNetwork.") self.graph = bayesnet.graph self.node_lookup = bayesnet.node_lookup self.__initial_nodes = [] diff --git a/primo/nodes.py b/primo/nodes.py index 8fdd77c..be6e079 100644 --- a/primo/nodes.py +++ b/primo/nodes.py @@ -2,10 +2,9 @@ import abc import random import re -import scipy +import scipy.stats -from primo.decision.UtilityTable import UtilityTable -import primo.reasoning.density +import primo.reasoning.densities class Node(object): @@ -99,7 +98,7 @@ class DiscreteNode(RandomNode): super(DiscreteNode, self).__init__(name) self.value_range = value_range - self.cpd = primo.reasoning.density.ProbabilityTable() + self.cpd = primo.reasoning.densities.ProbabilityTable() self.cpd.add_variable(self) def __str__(self): @@ -280,7 +279,7 @@ class ContinuousNodeFactory(object): name, (-float("Inf"), float("Inf")), - primo.reasoning.density.Gauss) + primo.reasoning.densities.Gauss) def createExponentialNode(self, name): ''' @@ -291,7 +290,7 @@ class ContinuousNodeFactory(object): return self.createContinuousNode( name, (0,float("Inf")), - primo.reasoning.density.Exponential) + primo.reasoning.densities.Exponential) def createBetaNode(self, name): ''' @@ -302,7 +301,7 @@ class ContinuousNodeFactory(object): return self.createContinuousNode( name, (0, 1), - primo.reasoning.density.Beta) + primo.reasoning.densities.Beta) def createContinuousNode(self,name,value_range,density_class): ''' diff --git a/primo/reasoning/particlefilter.py b/primo/reasoning/particlefilter.py index e34e6b7..c31d9a7 100644 --- a/primo/reasoning/particlefilter.py +++ b/primo/reasoning/particlefilter.py @@ -4,8 +4,7 @@ import copy import random import time -from primo.networks import BayesNet -from primo.networks import DynamicBayesNet +import primo.networks class Particle(object): ''' @@ -91,7 +90,7 @@ def weighted_sample(network, evidence = {}): ''' w = 1.0 state = {} - if not isinstance(network, BayesNet): + if not isinstance(network, primo.network.BayesianNetwork): raise Exception("The given network is not an instance of BayesNet.") nodes = network.get_nodes_in_topological_sort() @@ -132,7 +131,7 @@ def particle_filtering_DBN(network, N, T, get_evidence_function, particle_class Returns a list of N samples ''' - if not isinstance(network, DynamicBayesNet): + if not isinstance(network, primo.networks.DynamicBayesianNetwork): raise Exception("The given network is not an instance of DynamicBayesNet.") if not network.is_valid(): -- GitLab