msg_utils.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 rospy
7 from pgm_learner.msg import DiscreteNode, LinearGaussianNode, ConditionalProbability, GraphStructure, GraphEdge
8 
9 from libpgm.graphskeleton import GraphSkeleton
10 from libpgm.nodedata import NodeData
11 
13  skel = GraphSkeleton()
14  skel.V = []
15  skel.E = []
16  for name, v in nd.Vdata.items():
17  skel.V += [name]
18  skel.E += [[name, c] for c in v["children"]]
19  return skel
20 
21 def graph_skeleton_from_ros(graph_structure):
22  skel = GraphSkeleton()
23  skel.V = graph_structure.nodes
24  skel.E = [[e.node_from, e.node_to] for e in graph_structure.edges]
25  return skel
26 
28  graph = GraphStructure()
29  if skel.V and len(skel.V) > 0:
30  graph.nodes = map(str, skel.V)
31  if skel.E and len(skel.E) > 0:
32  graph.edges = [GraphEdge(str(e[0]),str(e[1])) for e in skel.E]
33  return graph
34 
35 def graph_state_dict_from_ros(graph_state):
36  data = {}
37  for s in graph_state.node_states:
38  data[s.node] = s.state
39  return data
40 
41 def graph_states_dict_from_ros(graph_states):
42  return [graph_state_dict_from_ros(gs) for gs in graph_states]
43 
45  n = DiscreteNode()
46  n.name = str(name)
47  n.outcomes = map(str, d["vals"])
48  if d["parents"]:
49  n.parents = map(str, d["parents"])
50  if d["children"]:
51  n.children = map(str, d["children"])
52  cprob = d["cprob"]
53  if isinstance(cprob, dict):
54  n.CPT = [ConditionalProbability(values=eval(k), probabilities=v) for k,v in (d["cprob"]).items()]
55  else:
56  n.CPT = [ConditionalProbability(values=map(str, d["vals"]), probabilities=cprob)]
57  return n
58 
60  return [discrete_node_from_dict(k, v) for k,v in d.items()]
61 
63  d = {}
64  d["vals"] = msg.outcomes
65  d["numoutcomes"] = len(msg.outcomes)
66  d["parents"] = msg.parents
67  d["children"] = msg.children
68  if len(msg.CPT) == 1:
69  d["cprob"] = msg.CPT[0].probabilities
70  else:
71  d["cprob"] = {str(p.values): p.probabilities for p in msg.CPT}
72  return d
73 
74 
76  nd = NodeData()
77  nd.Vdata = {n.name: dict_from_ros_discrete_node(n) for n in nodes}
78  return nd
79 
81  n = LinearGaussianNode()
82  n.name = str(name)
83  if d["parents"]:
84  n.parents = map(str, d["parents"])
85  if d["children"]:
86  n.children = map(str, d["children"])
87  n.mean = d["mean_base"]
88  n.variance = d["variance"]
89  n.mean_scalar = d["mean_scal"]
90  return n
91 
93  return [linear_gaussian_node_from_dict(k,v) for k,v in d.items()]
def discrete_nodedata_from_ros(nodes)
Definition: msg_utils.py:75
def graph_skeleton_from_ros(graph_structure)
Definition: msg_utils.py:21
def linear_gaussian_node_from_dict(name, d)
Definition: msg_utils.py:80
def graph_skeleton_to_ros(skel)
Definition: msg_utils.py:27
def linear_gaussian_nodes_to_ros(d)
Definition: msg_utils.py:92
def graph_skeleton_from_node_data(nd)
Definition: msg_utils.py:12
def graph_state_dict_from_ros(graph_state)
Definition: msg_utils.py:35
def discrete_nodes_to_ros(d)
Definition: msg_utils.py:59
def discrete_node_from_dict(name, d)
Definition: msg_utils.py:44
def graph_states_dict_from_ros(graph_states)
Definition: msg_utils.py:41
def dict_from_ros_discrete_node(msg)
Definition: msg_utils.py:62


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