25
25
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
26
# POSSIBILITY OF SUCH DAMAGE.
27
27
28
- #!/usr/bin/env python
29
- # -*- coding: utf-8 -*-
30
-
31
- # @package libherb.tsr Utilities for TSRs and TSR chains.
32
-
33
28
import openravepy
34
29
import numpy
35
30
import numpy .random
@@ -146,7 +141,8 @@ def serialize(self):
146
141
SerializeTransform12Col (self .Tw_e ),
147
142
SerializeArray (self .Bw ))
148
143
149
- def serialize_dict (self ):
144
+ def to_dict (self ):
145
+ """ Convert this TSR to a python dict. """
150
146
return {
151
147
'T0_w' : self .T0_w .tolist (),
152
148
'Tw_e' : self .Tw_e .tolist (),
@@ -156,15 +152,49 @@ def serialize_dict(self):
156
152
}
157
153
158
154
@staticmethod
159
- def deserialize_dict (x ):
155
+ def from_dict (x ):
156
+ """ Construct a TSR from a python dict. """
160
157
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' ) )
166
163
)
167
164
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
+
168
198
class TSRChain (object ):
169
199
170
200
def __init__ (self , sample_start = False , sample_goal = False , constrain = False ,
@@ -218,28 +248,61 @@ def serialize(self):
218
248
SerializeArray (self .mimicbodyjoints ))
219
249
return outstring
220
250
221
- def serialize_dict (self ):
251
+ def to_dict (self ):
252
+ """ Construct a TSR chain from a python dict. """
222
253
return {
223
254
'sample_goal' : self .sample_goal ,
224
255
'sample_start' : self .sample_start ,
225
256
'constrain' : self .constrain ,
226
257
'mimicbodyname' : self .mimicbodyname ,
227
258
'mimicbodyjoints' : self .mimicbodyjoints ,
228
- 'tsrs' : [ tsr .serialize_dict () for tsr in self .TSRs ],
259
+ 'tsrs' : [tsr .serialize_dict () for tsr in self .TSRs ],
229
260
}
230
261
231
-
232
262
@staticmethod
233
- def deserialize_dict (x ):
263
+ def from_dict (x ):
264
+ """ Construct a TSR chain from a python dict. """
234
265
return TSRChain (
235
266
sample_start = x ['sample_start' ],
236
267
sample_goal = x ['sample_goal' ],
237
268
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' ]],
239
270
mimicbodyname = x ['mimicbodyname' ],
240
271
mimicbodyjoints = x ['mimicbodyjoints' ],
241
272
)
242
273
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
+
243
306
def sample (self ):
244
307
if len (self .TSRs ) == 0 :
245
308
return None
0 commit comments