discrete_bayesian_parameter_estimation_sample.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 # Author: Yuki Furuta <furushchev@jsk.imi.i.u-tokyo.ac.jp>
00004 
00005 
00006 import os
00007 import pprint
00008 import rospkg
00009 import rospy
00010 from pgm_learner.srv import DiscreteParameterEstimation, DiscreteParameterEstimationRequest
00011 from pgm_learner.msg import GraphEdge, DiscreteGraphState, DiscreteNodeState
00012 
00013 from libpgm.graphskeleton import GraphSkeleton
00014 from libpgm.nodedata import NodeData
00015 from libpgm.discretebayesiannetwork import DiscreteBayesianNetwork
00016 
00017 PKG_PATH = rospkg.RosPack().get_path("pgm_learner")
00018 PP = pprint.PrettyPrinter(indent=2)
00019 
00020 if __name__ == '__main__':
00021     rospy.init_node("pgm_learner_sample_discrete")
00022 
00023     param_estimate = rospy.ServiceProxy("pgm_learner/discrete/parameter_estimation", DiscreteParameterEstimation)
00024 
00025     req = DiscreteParameterEstimationRequest()
00026 
00027     dpath = os.path.join(PKG_PATH, "test", "graph-test.txt")
00028     tpath = dpath
00029 
00030     # load graph structure
00031     skel = GraphSkeleton()
00032     skel.load(dpath)
00033     req.graph.nodes = skel.V
00034     req.graph.edges = [GraphEdge(k, v) for k,v in skel.E]
00035     skel.toporder()
00036 
00037     # generate trial data
00038     teacher_nd = NodeData()
00039     teacher_nd.load(dpath)
00040     bn = DiscreteBayesianNetwork(skel, teacher_nd)
00041     data = bn.randomsample(200)
00042     for v in data:
00043         gs = DiscreteGraphState()
00044         for k_s, v_s in v.items():
00045             gs.node_states.append(DiscreteNodeState(node=k_s, state=v_s))
00046         req.states.append(gs)
00047 
00048     PP.pprint(param_estimate(req).nodes)


pgm_learner
Author(s): Yuki Furuta
autogenerated on Wed Jul 10 2019 03:24:11