Sample
Sample
Bases: BaseModel
A base model class for serializing, recording, and manipulating arbitray data.
It was designed to be extensible, flexible, yet strongly typed. In addition to supporting any json API out of the box, it can be used to represent arbitrary action and observation spaces in robotics and integrates seemlessly with H5, Gym, Arrow, PyTorch, DSPY, numpy, and HuggingFace.
Methods:
Name | Description |
---|---|
schema |
Get a simplified json schema of your data. |
to |
Convert the Sample instance to a different container type: |
default_value |
Get the default value for the Sample instance. |
unflatten |
Unflatten a one-dimensional array or dictionary into a Sample instance. |
flatten |
Flatten the Sample instance into a one-dimensional array or dictionary. |
space_for |
Default Gym space generation for a given value. |
init_from |
Initialize a Sample instance from a given value. |
from_space |
Generate a Sample instance from a Gym space. |
pack_from |
Pack a list of samples into a single sample with lists for attributes. |
unpack |
Unpack the packed Sample object into a list of Sample objects or dictionaries. |
dict |
Return the Sample object as a dictionary with None values excluded. |
model_field_info |
Get the FieldInfo for a given attribute key. |
space |
Return the corresponding Gym space for the Sample instance based on its instance attributes. |
random_sample |
Generate a random Sample instance based on its instance attributes. |
Examples:
>>> sample = Sample(x=1, y=2, z={"a": 3, "b": 4}, extra_field=5)
>>> flat_list = sample.flatten()
>>> print(flat_list)
[1, 2, 3, 4, 5]
>>> schema = sample.schema()
{'type': 'object', 'properties': {'x': {'type': 'number'}, 'y': {'type': 'number'}, 'z': {'type': 'object', 'properties': {'a': {'type': 'number'}, 'b': {'type': 'number'}}}, 'extra_field': {'type': 'number'}}}
>>> unflattened_sample = Sample.unflatten(flat_list, schema)
>>> print(unflattened_sample)
Sample(x=1, y=2, z={'a': 3, 'b': 4}, extra_field=5)
Source code in mbodied/types/sample.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
|
__hash__()
Return a hash of the Sample instance.
Source code in mbodied/types/sample.py
95 96 97 |
|
__init__(datum=None, **data)
Accepts an arbitrary datum as well as keyword arguments.
Source code in mbodied/types/sample.py
84 85 86 87 88 89 90 91 92 93 |
|
__str__()
Return a string representation of the Sample instance.
Source code in mbodied/types/sample.py
99 100 101 |
|
default_sample(output_type='Sample')
classmethod
Generate a default Sample instance from its class attributes. Useful for padding.
This is the "no-op" instance and should be overriden as needed.
Source code in mbodied/types/sample.py
515 516 517 518 519 520 521 522 523 |
|
default_space()
classmethod
Return the Gym space for the Sample class based on its class attributes.
Source code in mbodied/types/sample.py
510 511 512 513 |
|
default_value()
classmethod
Get the default value for the Sample instance.
Returns:
Name | Type | Description |
---|---|---|
Sample |
Sample
|
The default value for the Sample instance. |
Source code in mbodied/types/sample.py
333 334 335 336 337 338 339 340 |
|
dict(exclude_none=True, exclude=None)
Return the Sample object as a dictionary with None values excluded.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exclude_none
|
bool
|
Whether to exclude None values. Defaults to True. |
True
|
exclude
|
set[str]
|
Set of attribute names to exclude. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: Dictionary representation of the Sample object. |
Source code in mbodied/types/sample.py
103 104 105 106 107 108 109 110 111 112 113 |
|
from_flat_dict(flat_dict, schema=None)
classmethod
Initialize a Sample instance from a flattened dictionary.
Source code in mbodied/types/sample.py
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
|
from_space(space)
classmethod
Generate a Sample instance from a Gym space.
Source code in mbodied/types/sample.py
449 450 451 452 453 454 455 456 457 458 459 |
|
model_field_info(key)
Get the FieldInfo for a given attribute key.
Source code in mbodied/types/sample.py
525 526 527 528 529 530 531 532 533 534 |
|
obj_to_schema(value)
staticmethod
Generates a simplified JSON schema from a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any
|
An object to generate a schema for. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
Dict
|
A simplified JSON schema representing the structure of the dictionary. |
Source code in mbodied/types/sample.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
|
pack_from(samples)
classmethod
Pack a list of samples into a single sample with lists for attributes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
samples
|
List[Union[Sample, Dict]]
|
List of samples or dictionaries. |
required |
Returns:
Name | Type | Description |
---|---|---|
Sample |
Sample
|
Packed sample with lists for attributes. |
Source code in mbodied/types/sample.py
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 |
|
random_sample()
Generate a random Sample instance based on its instance attributes. Omits None values.
Override this method in subclasses to customize the sample generation.
Source code in mbodied/types/sample.py
549 550 551 552 553 554 |
|
read(data)
classmethod
Read a Sample instance from a JSON string or dictionary or path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Any
|
The JSON string or dictionary to read. |
required |
Returns:
Name | Type | Description |
---|---|---|
Sample |
Sample
|
The read Sample instance. |
Source code in mbodied/types/sample.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
|
schema(resolve_refs=True, include_descriptions=False)
Returns a simplified json schema.
Removing additionalProperties, selecting the first type in anyOf, and converting numpy schema to the desired type. Optionally resolves references.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
resolve_refs
|
bool
|
Whether to resolve references in the schema. Defaults to True. |
True
|
include_descriptions
|
bool
|
Whether to include descriptions in the schema. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Dict
|
A simplified JSON schema. |
Source code in mbodied/types/sample.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
|
space()
Return the corresponding Gym space for the Sample instance based on its instance attributes. Omits None values.
Override this method in subclasses to customize the space generation.
Source code in mbodied/types/sample.py
536 537 538 539 540 541 542 543 544 545 546 547 |
|
space_for(value, max_text_length=1000, info=None)
classmethod
Default Gym space generation for a given value.
Only used for subclasses that do not override the space method.
Source code in mbodied/types/sample.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
|
to(container)
Convert the Sample instance to a different container type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
container
|
Any
|
The container type to convert to. Supported types are 'dict', 'list', 'np', 'pt' (pytorch), 'space' (gym.space), 'schema', 'json', 'hf' (datasets.Dataset) and any subtype of Sample. |
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The converted container. |
Source code in mbodied/types/sample.py
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
|
unflatten(one_d_array_or_dict, schema=None)
classmethod
Unflatten a one-dimensional array or dictionary into a Sample instance.
If a dictionary is provided, its keys are ignored.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
one_d_array_or_dict
|
A one-dimensional array or dictionary to unflatten. |
required | |
schema
|
A dictionary representing the JSON schema. Defaults to using the class's schema. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Sample |
Sample
|
The unflattened Sample instance. |
Examples:
>>> sample = Sample(x=1, y=2, z={"a": 3, "b": 4}, extra_field=5)
>>> flat_list = sample.flatten()
>>> print(flat_list)
[1, 2, 3, 4, 5]
>>> Sample.unflatten(flat_list, sample.schema())
Sample(x=1, y=2, z={'a': 3, 'b': 4}, extra_field=5)
Source code in mbodied/types/sample.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
unpack(to_dicts=False)
Unpack the packed Sample object into a list of Sample objects or dictionaries.
Source code in mbodied/types/sample.py
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 |
|