python_utils
A set of utility functions for general python usage
Recreatable
Simple class that provides an abstract interface automatically saving init args of the classes inheriting it.
Source code in omnigibson/utils/python_utils.py
get_init_info()
Grabs relevant initialization information for this class instance. Useful for directly reloading an object from this information, using @create_object_from_init_info.
Returns:
Name | Type | Description |
---|---|---|
dict |
Nested dictionary that contains this object's initialization information |
Source code in omnigibson/utils/python_utils.py
RecreatableAbcMeta
Bases: RecreatableMeta
, ABCMeta
A composite metaclass of both RecreatableMeta and ABCMeta.
Adding in ABCMeta to resolve metadata conflicts.
RecreatableMeta
Bases: type
Simple metaclass that automatically saves init args of the instances it creates.
Source code in omnigibson/utils/python_utils.py
Registerable
Simple class template that provides an abstract interface for registering classes.
Source code in omnigibson/utils/python_utils.py
__init_subclass__(**kwargs)
Registers all subclasses as part of this registry. This is useful to decouple internal codebase from external user additions. This way, users can add their custom subclasses by simply extending this class, and it will automatically be registered internally. This allows users to then specify their classes directly in string-form in e.g., their config files, without having to manually set the str-to-class mapping in our code.
Source code in omnigibson/utils/python_utils.py
Serializable
Simple class that provides an abstract interface to dump / load states, optionally with serialized functionality as well.
Source code in omnigibson/utils/python_utils.py
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 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 |
|
state_size
property
Returns:
Name | Type | Description |
---|---|---|
int |
Size of this object's serialized state |
deserialize(state)
De-serializes flattened 1D numpy array @state into nested dictionary state. Should be implemented by subclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state |
n-array
|
encoded + serialized, 1D numerical np.array capturing this object's state |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
Keyword-mapped states of this object. Should match structure of output from self._dump_state() |
Source code in omnigibson/utils/python_utils.py
dump_state(serialized=False)
Dumps the state of this object in either dictionary of flattened numerical form.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
serialized |
bool
|
If True, will return the state of this object as a 1D numpy array. Otherewise, will return a (potentially nested) dictionary of states for this object |
False
|
Returns:
Type | Description |
---|---|
dict or n-array: Either: - Keyword-mapped states of this object, or - encoded + serialized, 1D numerical np.array capturing this object's state, where n is @self.state_size |
Source code in omnigibson/utils/python_utils.py
load_state(state, serialized=False)
Deserializes and loads this object's state based on @state
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state |
dict or n-array
|
Either: - Keyword-mapped states of this object, or - encoded + serialized, 1D numerical np.array capturing this object's state, where n is @self.state_size |
required |
serialized |
bool
|
If True, will interpret @state as a 1D numpy array. Otherewise, will assume the input is a (potentially nested) dictionary of states for this object |
False
|
Source code in omnigibson/utils/python_utils.py
serialize(state)
Serializes nested dictionary state @state into a flattened 1D numpy array for encoding efficiency. Should be implemented by subclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state |
dict
|
Keyword-mapped states of this object to encode. Should match structure of output from self._dump_state() |
required |
Returns:
Type | Description |
---|---|
n-array: encoded + serialized, 1D numerical np.array capturing this object's state |
Source code in omnigibson/utils/python_utils.py
SerializableNonInstance
Identical to Serializable, but intended for non-instanceable classes
Source code in omnigibson/utils/python_utils.py
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 |
|
deserialize(state)
classmethod
De-serializes flattened 1D numpy array @state into nested dictionary state. Should be implemented by subclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state |
n-array
|
encoded + serialized, 1D numerical np.array capturing this object's state |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
Keyword-mapped states of this object. Should match structure of output from self._dump_state() |
Source code in omnigibson/utils/python_utils.py
dump_state(serialized=False)
classmethod
Dumps the state of this object in either dictionary of flattened numerical form.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
serialized |
bool
|
If True, will return the state of this object as a 1D numpy array. Otherewise, will return a (potentially nested) dictionary of states for this object |
False
|
Returns:
Type | Description |
---|---|
dict or n-array: Either: - Keyword-mapped states of this object, or - encoded + serialized, 1D numerical np.array capturing this object's state, where n is @self.state_size |
Source code in omnigibson/utils/python_utils.py
load_state(state, serialized=False)
classmethod
Deserializes and loads this object's state based on @state
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state |
dict or n-array
|
Either: - Keyword-mapped states of this object, or - encoded + serialized, 1D numerical np.array capturing this object's state, where n is @self.state_size |
required |
serialized |
bool
|
If True, will interpret @state as a 1D numpy array. Otherewise, will assume the input is a (potentially nested) dictionary of states for this object |
False
|
Source code in omnigibson/utils/python_utils.py
serialize(state)
classmethod
Serializes nested dictionary state @state into a flattened 1D numpy array for encoding efficiency. Should be implemented by subclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state |
dict
|
Keyword-mapped states of this object to encode. Should match structure of output from self._dump_state() |
required |
Returns:
Type | Description |
---|---|
n-array: encoded + serialized, 1D numerical np.array capturing this object's state |
Source code in omnigibson/utils/python_utils.py
UniquelyNamed
Simple class that implements a name property, that must be implemented by a subclass. Note that any @Named entity must be UNIQUE!
Source code in omnigibson/utils/python_utils.py
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 |
|
name
property
Returns:
Name | Type | Description |
---|---|---|
str |
Name of this instance. Must be unique! |
remove_names(include_all_owned=True, skip_ids=None)
Checks if self.name exists in the global NAMES registry, and deletes it if so. Possibly also iterates through all owned member variables and checks for their corresponding names if @include_all_owned is True.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
include_all_owned |
bool
|
If True, will iterate through all owned members of this instance and remove their names as well, if they are UniquelyNamed |
True
|
skip_ids |
None or set of int
|
If specified, will skip over any ids in the specified set that are matched to any attributes found (this compares id(attr) to @skip_ids). |
None
|
Source code in omnigibson/utils/python_utils.py
UniquelyNamedNonInstance
Identical to UniquelyNamed, but intended for non-instanceable classes
Source code in omnigibson/utils/python_utils.py
Wrapper
Base class for all wrappers in OmniGibson
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
any
|
Arbitrary python object instance to wrap |
required |
Source code in omnigibson/utils/python_utils.py
unwrapped
property
Grabs unwrapped object
Returns:
Name | Type | Description |
---|---|---|
any |
The unwrapped object instance |
assert_valid_key(key, valid_keys, name=None)
Helper function that asserts that @key is in dictionary @valid_keys keys. If not, it will raise an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
any
|
key to check for in dictionary @dic's keys |
required |
valid_keys |
Iterable
|
contains keys should be checked with @key |
required |
name |
str or None
|
if specified, is the name associated with the key that will be printed out if the key is not found. If None, default is "value" |
None
|
Source code in omnigibson/utils/python_utils.py
camel_case_to_snake_case(camel_case_text)
Helper function to convert a camel case text to snake case, e.g. "StrawberrySmoothie" -> "strawberry_smoothie"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
camel_case_text |
str
|
Text in camel case |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
snake case text |
Source code in omnigibson/utils/python_utils.py
clear()
create_class_from_registry_and_config(cls_name, cls_registry, cfg, cls_type_descriptor)
Helper function to create a class with str type @cls_name, which should be a valid entry in @cls_registry, using kwargs in dictionary form @cfg to pass to the constructor, with @cls_type_name specified for debugging
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls_name |
str
|
Name of the class to create. This should correspond to the actual class type, in string form |
required |
cls_registry |
dict
|
Class registry. This should map string names of valid classes to create to the actual class type itself |
required |
cfg |
dict
|
Any keyword arguments to pass to the class constructor |
required |
cls_type_descriptor |
str
|
Description of the class type being created. This can be any string and is used solely for debugging purposes |
required |
Returns:
Name | Type | Description |
---|---|---|
any |
Created class instance |
Source code in omnigibson/utils/python_utils.py
create_object_from_init_info(init_info)
Create a new object based on given init info.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
init_info |
dict
|
Nested dictionary that contains an object's init information. |
required |
Returns:
Name | Type | Description |
---|---|---|
any |
Newly created object. |
Source code in omnigibson/utils/python_utils.py
extract_class_init_kwargs_from_dict(cls, dic, copy=False)
Helper function to return a dictionary of key-values that specifically correspond to @cls class's init constructor method, from @dic which may or may not contain additional, irrelevant kwargs. Note that @dic may possibly be missing certain kwargs as specified by cls.init. No error will be raised.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
object
|
Class from which to grab init kwargs that will be be used as filtering keys for @dic |
required |
dic |
dict
|
Dictionary containing multiple key-values |
required |
copy |
bool
|
If True, will deepcopy all values corresponding to the specified @keys |
False
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Extracted subset dictionary possibly containing only the specified keys from cls.init and their corresponding values |
Source code in omnigibson/utils/python_utils.py
extract_subset_dict(dic, keys, copy=False)
Helper function to extract a subset of dictionary key-values from a current dictionary. Optionally (deep)copies the values extracted from the original @dic if @copy is True.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dic |
dict
|
Dictionary containing multiple key-values |
required |
keys |
Iterable
|
Specific keys to extract from @dic. If the key doesn't exist in @dic, then the key is skipped |
required |
copy |
bool
|
If True, will deepcopy all values corresponding to the specified @keys |
False
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Extracted subset dictionary containing only the specified @keys and their corresponding values |
Source code in omnigibson/utils/python_utils.py
get_class_init_kwargs(cls)
Helper function to return a list of all valid keyword arguments (excluding "self") for the given @cls class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
object
|
Class from which to grab init kwargs |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
All keyword arguments (excluding "self") specified by @cls init constructor method |
Source code in omnigibson/utils/python_utils.py
get_uuid(name, n_digits=8)
Helper function to create a unique @n_digits uuid given a unique @name
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name of the object or class |
required |
n_digits |
int
|
Number of digits of the uuid, default is 8 |
8
|
Returns:
Name | Type | Description |
---|---|---|
int |
uuid |
Source code in omnigibson/utils/python_utils.py
meets_minimum_version(test_version, minimum_version)
Verify that @test_version meets the @minimum_version
Parameters:
Name | Type | Description | Default |
---|---|---|---|
test_version |
str
|
Python package version. Should be, e.g., 0.26.1 |
required |
minimum_version |
str
|
Python package version to test against. Should be, e.g., 0.27.2 |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
Whether @test_version meets @minimum_version |
Source code in omnigibson/utils/python_utils.py
merge_nested_dicts(base_dict, extra_dict, inplace=False, verbose=False)
Iteratively updates @base_dict with values from @extra_dict. Note: This generates a new dictionary!
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_dict |
dict
|
Nested base dictionary, which should be updated with all values from @extra_dict |
required |
extra_dict |
dict
|
Nested extra dictionary, whose values will overwrite corresponding ones in @base_dict |
required |
inplace |
bool
|
Whether to modify @base_dict in place or not |
False
|
verbose |
bool
|
If True, will print when keys are mismatched |
False
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Updated dictionary |
Source code in omnigibson/utils/python_utils.py
save_init_info(func)
Decorator to save the init info of an object to object._init_info.
_init_info contains class name and class constructor's input args.
Source code in omnigibson/utils/python_utils.py
snake_case_to_camel_case(snake_case_text)
Helper function to convert a snake case text to camel case, e.g. "strawberry_smoothie" -> "StrawberrySmoothie"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
snake_case_text |
str
|
Text in snake case |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
camel case text |
Source code in omnigibson/utils/python_utils.py
subclass_factory(name, base_classes, __init__=None, **kwargs)
Programmatically generates a new class type with name @name, subclassing from base classes @base_classes, with corresponding init call @init.
NOTE: If init is None (default), the init call from @base_classes will be used instead.
cf. https://stackoverflow.com/questions/15247075/how-can-i-dynamically-create-derived-classes-from-a-base-class
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Generated class name |
required |
base_classes |
type, or list of type
|
Base class(es) to use for generating the subclass |
required |
__init__ |
None or function
|
Init call to use for the base class when it is instantiated. If None if specified, the newly generated class will automatically inherit the init call from @base_classes |
None
|
**kwargs |
any
|
keyword-mapped parameters to override / set in the child class, where the keys represent the class / instance attribute to modify and the values represent the functions / value to set |
{}
|