Skip to content

Commit 25425b8

Browse files
committed
Added tsr conversion methods for JSON and YAML.
1 parent aa63713 commit 25425b8

File tree

1 file changed

+80
-17
lines changed

1 file changed

+80
-17
lines changed

src/prpy/tsr/tsr.py

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2626
# POSSIBILITY OF SUCH DAMAGE.
2727

28-
#!/usr/bin/env python
29-
# -*- coding: utf-8 -*-
30-
31-
# @package libherb.tsr Utilities for TSRs and TSR chains.
32-
3328
import openravepy
3429
import numpy
3530
import numpy.random
@@ -146,7 +141,8 @@ def serialize(self):
146141
SerializeTransform12Col(self.Tw_e),
147142
SerializeArray(self.Bw))
148143

149-
def serialize_dict(self):
144+
def to_dict(self):
145+
""" Convert this TSR to a python dict. """
150146
return {
151147
'T0_w': self.T0_w.tolist(),
152148
'Tw_e': self.Tw_e.tolist(),
@@ -156,15 +152,49 @@ def serialize_dict(self):
156152
}
157153

158154
@staticmethod
159-
def deserialize_dict(x):
155+
def from_dict(x):
156+
""" Construct a TSR from a python dict. """
160157
return TSR(
161-
T0_w = numpy.array(x['T0_w']),
162-
Tw_e = numpy.array(x['Tw_e']),
163-
Bw = numpy.array(x['Bw']),
164-
manip = numpy.array(x['manipindex']),
165-
bodyandlink = numpy.array(x['bodyandlink'])
158+
T0_w=numpy.array(x['T0_w']),
159+
Tw_e=numpy.array(x['Tw_e']),
160+
Bw=numpy.array(x['Bw']),
161+
manip=numpy.array(x.get('manipindex', -1)),
162+
bodyandlink=numpy.array(x.get('bodyandlink', 'NULL'))
166163
)
167164

165+
def to_json(self):
166+
""" Convert this TSR to a JSON string. """
167+
import json
168+
return json.dumps(self.to_dict())
169+
170+
@staticmethod
171+
def from_json(x, *args, **kw_args):
172+
"""
173+
Construct a TSR from a JSON string.
174+
175+
This method internally forwards all arguments to `json.loads`.
176+
"""
177+
import json
178+
x_dict = json.loads(x, *args, **kw_args)
179+
return TSR.from_dict(x_dict)
180+
181+
def to_yaml(self):
182+
""" Convert this TSR to a YAML string. """
183+
import yaml
184+
return yaml.dumps(self.to_dict())
185+
186+
@staticmethod
187+
def from_yaml(x, *args, **kw_args):
188+
"""
189+
Construct a TSR from a YAML string.
190+
191+
This method internally forwards all arguments to `yaml.safe_load`.
192+
"""
193+
import yaml
194+
x_dict = yaml.safe_load(x, *args, **kw_args)
195+
return TSR.from_dict(x_dict)
196+
197+
168198
class TSRChain(object):
169199

170200
def __init__(self, sample_start=False, sample_goal=False, constrain=False,
@@ -218,28 +248,61 @@ def serialize(self):
218248
SerializeArray(self.mimicbodyjoints))
219249
return outstring
220250

221-
def serialize_dict(self):
251+
def to_dict(self):
252+
""" Construct a TSR chain from a python dict. """
222253
return {
223254
'sample_goal': self.sample_goal,
224255
'sample_start': self.sample_start,
225256
'constrain': self.constrain,
226257
'mimicbodyname': self.mimicbodyname,
227258
'mimicbodyjoints': self.mimicbodyjoints,
228-
'tsrs': [ tsr.serialize_dict() for tsr in self.TSRs ],
259+
'tsrs': [tsr.serialize_dict() for tsr in self.TSRs],
229260
}
230261

231-
232262
@staticmethod
233-
def deserialize_dict(x):
263+
def from_dict(x):
264+
""" Construct a TSR chain from a python dict. """
234265
return TSRChain(
235266
sample_start=x['sample_start'],
236267
sample_goal=x['sample_goal'],
237268
constrain=x['constrain'],
238-
TSRs=[ TSR.deserialize_dict(tsr) for tsr in x['tsrs'] ],
269+
TSRs=[TSR.from_dict(tsr) for tsr in x['tsrs']],
239270
mimicbodyname=x['mimicbodyname'],
240271
mimicbodyjoints=x['mimicbodyjoints'],
241272
)
242273

274+
def to_json(self):
275+
""" Convert this TSR chain to a JSON string. """
276+
import json
277+
return json.dumps(self.to_dict())
278+
279+
@staticmethod
280+
def from_json(x, *args, **kw_args):
281+
"""
282+
Construct a TSR chain from a JSON string.
283+
284+
This method internally forwards all arguments to `json.loads`.
285+
"""
286+
import json
287+
x_dict = json.loads(x, *args, **kw_args)
288+
return TSR.from_dict(x_dict)
289+
290+
def to_yaml(self):
291+
""" Convert this TSR chain to a YAML string. """
292+
import yaml
293+
return yaml.dumps(self.to_dict())
294+
295+
@staticmethod
296+
def from_yaml(x, *args, **kw_args):
297+
"""
298+
Construct a TSR chain from a YAML string.
299+
300+
This method internally forwards all arguments to `yaml.safe_load`.
301+
"""
302+
import yaml
303+
x_dict = yaml.safe_load(x, *args, **kw_args)
304+
return TSR.from_dict(x_dict)
305+
243306
def sample(self):
244307
if len(self.TSRs) == 0:
245308
return None

0 commit comments

Comments
 (0)