discrete_bayesian_parameter_estimation_sample.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # Author: Yuki Furuta <furushchev@jsk.imi.i.u-tokyo.ac.jp>
4 
5 
6 import os
7 import pprint
8 import rospkg
9 import rospy
10 from pgm_learner.srv import DiscreteParameterEstimation, DiscreteParameterEstimationRequest
11 from pgm_learner.msg import GraphEdge, DiscreteGraphState, DiscreteNodeState
12 
13 from libpgm.graphskeleton import GraphSkeleton
14 from libpgm.nodedata import NodeData
15 from libpgm.discretebayesiannetwork import DiscreteBayesianNetwork
16 
17 PKG_PATH = rospkg.RosPack().get_path("pgm_learner")
18 PP = pprint.PrettyPrinter(indent=2)
19 
20 if __name__ == '__main__':
21  rospy.init_node("pgm_learner_sample_discrete")
22 
23  param_estimate = rospy.ServiceProxy("pgm_learner/discrete/parameter_estimation", DiscreteParameterEstimation)
24 
25  req = DiscreteParameterEstimationRequest()
26 
27  dpath = os.path.join(PKG_PATH, "test", "graph-test.txt")
28  tpath = dpath
29 
30  # load graph structure
31  skel = GraphSkeleton()
32  skel.load(dpath)
33  req.graph.nodes = skel.V
34  req.graph.edges = [GraphEdge(k, v) for k,v in skel.E]
35  skel.toporder()
36 
37  # generate trial data
38  teacher_nd = NodeData()
39  teacher_nd.load(dpath)
40  bn = DiscreteBayesianNetwork(skel, teacher_nd)
41  data = bn.randomsample(200)
42  for v in data:
43  gs = DiscreteGraphState()
44  for k_s, v_s in v.items():
45  gs.node_states.append(DiscreteNodeState(node=k_s, state=v_s))
46  req.states.append(gs)
47 
48  PP.pprint(param_estimate(req).nodes)


pgm_learner
Author(s): Yuki Furuta
autogenerated on Wed Jul 10 2019 03:47:12