morpheus.utils.type_utils
Utility functions for working with types.
Functions
cpp_mode_to_exec_mode () |
Return the execution mode based on the configuration of the global morpheus.config.CppConfig singleton. |
df_type_str_to_exec_mode (df_type_str) |
Return the appropriate execution mode based on the DataFrame type string. |
df_type_str_to_pkg (df_type_str) |
Import and return the appropriate DataFrame package based on the DataFrame type string. |
exec_mode_to_df_type_str (execution_mode) |
Return the appropriate DataFrame type string based on the execution mode. |
flatten_types (type_list) |
Flattens a list of types, removing any union and typing.Any types. |
get_array_pkg ([execution_mode]) |
Return the appropriate array package (CuPy for GPU, NumPy for CPU) based on the execution mode. |
get_df_class () |
Return the appropriate DataFrame selector which can be either an ExecutionMode instance, a DataFrame type string, or None . |
get_df_pkg () |
Return the appropriate DataFrame package based on selector which can be either an ExecutionMode instance, a DataFrame type string, or None . |
get_df_pkg_from_obj (obj) |
Return the appropriate DataFrame package based on a given object (DataFrame, Series, RangeIndex etc...). |
get_full_qualname (klass) |
Returns the fully qualified name of a class. |
greatest_ancestor (*cls_list) |
Returns the greatest common ancestor of the classes in the class list |
is_cudf_type (obj) |
Check if a given object (DataFrame, Series, RangeIndex etc...) is a cuDF type. |
is_dataframe (obj) |
Check if a given object is a pandas or cudf DataFrame. |
is_union_type (type_) |
Returns True if the type is a typing.Union or a types.UnionType . |
pretty_print_type_name (type_) |
Determines a good label to use for a type. |
unpack_tuple () |
|
unpack_union () |
- cpp_mode_to_exec_mode()[source]
Return the execution mode based on the configuration of the global
morpheus.config.CppConfig
singleton.- Returns
morpheus.config.ExecutionMode
The execution mode.
- df_type_str_to_exec_mode(df_type_str)[source]
Return the appropriate execution mode based on the DataFrame type string.
- Parameters
- df_type_str
morpheus.utils.type_aliases.DataFrameModule
The DataFrame type string.
- df_type_str
- Returns
morpheus.config.ExecutionMode
The associated execution mode based on the DataFrame type string.
- df_type_str_to_pkg(df_type_str)[source]
Import and return the appropriate DataFrame package based on the DataFrame type string.
- Parameters
- df_type_str
morpheus.utils.type_aliases.DataFrameModule
The DataFrame type string.
- df_type_str
- Returns
types.ModuleType
The associated DataFrame package based on the DataFrame type string.
- exec_mode_to_df_type_str(execution_mode)[source]
Return the appropriate DataFrame type string based on the execution mode.
- Parameters
- execution_mode
morpheus.config.ExecutionMode
The execution mode.
- execution_mode
- Returns
morpheus.utils.type_aliases.DataFrameModule
The associated DataFrame type string based on the execution mode.
- flatten_types(type_list)[source]
Flattens a list of types, removing any union and
typing.Any
types.
- get_array_pkg(execution_mode=None)[source]
Return the appropriate array package (CuPy for GPU, NumPy for CPU) based on the execution mode.
When
None
the execution mode is determined by the globalmorpheus.config.CppConfig
singleton.- Parameters
- execution_mode
morpheus.config.ExecutionMode
, optional The execution mode, by default
None
.
- execution_mode
- Returns
types.ModuleType
The associated array package based on the execution mode.
- get_df_class(selector: Literal['cudf', 'pandas'] = None) → type[DataFrameType][source]
- get_df_class(selector: morpheus.config.ExecutionMode = None) → type[DataFrameType]
Return the appropriate DataFrame
selector
which can be either anExecutionMode
instance, a DataFrame type string, orNone
.When
None
the execution mode is determined by the globalmorpheus.config.CppConfig
singleton.This method is best used within code which needs to construct a DataFrame in both CPU and GPU modes. Example usage:
from morpheus.utils.type_utils import get_df_class df_class = get_df_class() df = df_class({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
- Parameters
- selector
morpheus.utils.type_aliases.DataFrameModule
|morpheus.config.ExecutionMode
| None, optional The selector to determine the DataFrame class, by default None.
- selector
- Returns
type[DataFrameType]
- get_df_pkg(selector: Literal['cudf', 'pandas'] = None) → module[source]
- get_df_pkg(selector: morpheus.config.ExecutionMode = None) → module
Return the appropriate DataFrame package based on
selector
which can be either anExecutionMode
instance, a DataFrame type string, orNone
.When
None
the execution mode is determined by the globalmorpheus.config.CppConfig
singleton.This method is best used within code which needs to operate in both CPU and GPU modes, where simply importing
cudf
would cause an import error if the user is not using a GPU. Example usage:from morpheus.utils.type_utils import get_df_pkg df_pkg = get_df_pkg() ser = df_pkg.Series([1,2,3])
- Parameters
- selector
morpheus.utils.type_aliases.DataFrameModule
|morpheus.config.ExecutionMode
| None, optional The selector to determine the DataFrame package, by default None.
- selector
- Returns
types.ModuleType
The associated DataFrame package based on the selector.
- get_df_pkg_from_obj(obj)[source]
Return the appropriate DataFrame package based on a given object (DataFrame, Series, RangeIndex etc…).
- Parameters
- obj
typing.Any
The object to check.
- obj
- Returns
types.ModuleType
The associated DataFrame package based on the object.
- get_full_qualname(klass)[source]
Returns the fully qualified name of a class.
- greatest_ancestor(*cls_list)[source]
Returns the greatest common ancestor of the classes in the class list
- is_cudf_type(obj)[source]
Check if a given object (DataFrame, Series, RangeIndex etc…) is a cuDF type.
- Parameters
- obj
typing.Any
The object to check.
- obj
- Returns
- is_dataframe(obj)[source]
Check if a given object is a pandas or cudf DataFrame.
- Parameters
- obj
typing.Any
The object to check.
- obj
- Returns
- is_union_type(type_)[source]
Returns True if the type is a
typing.Union
or atypes.UnionType
.
- pretty_print_type_name(type_)[source]
Determines a good label to use for a type. Keeps the strings shorter.
- unpack_tuple(cls_1: Type[morpheus.utils.type_utils.T]) → Tuple[Type[morpheus.utils.type_utils.T]][source]
- unpack_tuple(cls_1: Type[morpheus.utils.type_utils.T1], cls_2: Type[morpheus.utils.type_utils.T2]) → Tuple[Type[morpheus.utils.type_utils.T1], Type[morpheus.utils.type_utils.T2]]
- unpack_tuple(cls_1: Type[morpheus.utils.type_utils.T1], cls_2: Type[morpheus.utils.type_utils.T2], cls_3: Type[morpheus.utils.type_utils.T3]) → Tuple[Type[morpheus.utils.type_utils.T1], Type[morpheus.utils.type_utils.T2], Type[morpheus.utils.type_utils.T3]]
- unpack_union(cls_1: Type[morpheus.utils.type_utils.T]) → Type[morpheus.utils.type_utils.T][source]
- unpack_union(cls_1: Type[morpheus.utils.type_utils.T1], cls_2: Type[morpheus.utils.type_utils.T2]) → Union[Type[morpheus.utils.type_utils.T1], Type[morpheus.utils.type_utils.T2]]
- unpack_union(cls_1: Type[morpheus.utils.type_utils.T1], cls_2: Type[morpheus.utils.type_utils.T2], cls_3: Type[morpheus.utils.type_utils.T3]) → Union[Type[morpheus.utils.type_utils.T1], Type[morpheus.utils.type_utils.T2], Type[morpheus.utils.type_utils.T3]]