linear_gaussian_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 import os
6 import pprint
7 import rospkg
8 import rospy
9 from pgm_learner.srv import LinearGaussianParameterEstimation, LinearGaussianParameterEstimationRequest
10 from pgm_learner.msg import GraphEdge, LinearGaussianGraphState, LinearGaussianNodeState
11 
12 from libpgm.graphskeleton import GraphSkeleton
13 from libpgm.nodedata import NodeData
14 from libpgm.lgbayesiannetwork import LGBayesianNetwork
15 
16 PKG_PATH = rospkg.RosPack().get_path("pgm_learner")
17 PP = pprint.PrettyPrinter(indent=2)
18 
19 if __name__ == '__main__':
20  rospy.init_node("pgm_learner_sample_linear_gaussian")
21 
22  param_estimate = rospy.ServiceProxy("pgm_learner/linear_gaussian/parameter_estimation", LinearGaussianParameterEstimation)
23 
24  req = LinearGaussianParameterEstimationRequest()
25 
26  dpath = os.path.join(PKG_PATH, "test", "graph-test.txt")
27  tpath = os.path.join(PKG_PATH, "test", "graph-lg-test.txt")
28 
29  # load graph structure
30  skel = GraphSkeleton()
31  skel.load(dpath)
32  req.graph.nodes = skel.V
33  req.graph.edges = [GraphEdge(k, v) for k,v in skel.E]
34  skel.toporder()
35 
36  # generate trial data
37  teacher_nd = NodeData()
38  teacher_nd.load(tpath)
39  bn = LGBayesianNetwork(skel, teacher_nd)
40  data = bn.randomsample(200)
41 
42  for v in data:
43  gs = LinearGaussianGraphState()
44  for k_s, v_s in v.items():
45  gs.node_states.append(LinearGaussianNodeState(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