IdMap

class rectools.dataset.identifiers.IdMap(to_internal: Series)[source]

Bases: object

Mapping between external and internal object ids.

External ids may be any unique hashable values, internal - always integers from 0 to n_objects-1.

Usually you do not need to create this object directly, use from_values class method instead.

Parameters

to_internal (pd.Series) – Mapping external -> internal ids.

Inherited-members

Methods

convert_to_external(internal[, strict])

Convert any sequence of internal ids to array of external ids (map internal -> external).

convert_to_internal(external[, strict])

Convert any sequence of external ids to array of internal ids (map external -> internal).

from_dict(mapping)

Create IdMap from dict of external id -> internal id mappings.

from_values(values)

Create IdMap from list of external ids.

get_external_sorted_by_internal()

Return array of external ids sorted by internal ids.

get_sorted_internal()

Return array of sorted internal ids.

Attributes

to_internal

external_ids

Array of external ids.

internal_ids

Array of internal ids.

to_external

Map internal->external.

convert_to_external(internal: Union[Sequence[int], ndarray], strict: bool = True) ndarray[source]

Convert any sequence of internal ids to array of external ids (map internal -> external).

Parameters
  • internal (sequence(int)) – Sequence of internal ids to convert.

  • strict (bool, default True) –

    Defines behaviour when some of given internal ids do not exist in mapping.
    • If True, KeyError will be raised;

    • If False, nonexistent ids will be skipped.

Returns

Array of external ids.

Return type

np.ndarray

Raises

KeyError – If some of given internal ids do not exist in mapping and strict flag is True.

convert_to_internal(external: Union[Sequence[Hashable], ndarray], strict: bool = True) ndarray[source]

Convert any sequence of external ids to array of internal ids (map external -> internal).

Parameters
  • external (sequence(hashable)) – Sequence of external ids to convert.

  • strict (bool, default True) –

    Defines behaviour when some of given external ids do not exist in mapping.
    • If True, KeyError will be raised;

    • If False, nonexistent ids will be skipped.

Returns

Array of internal ids.

Return type

np.ndarray

Raises

KeyError – If some of given external ids do not exist in mapping and strict flag is True.

property external_ids: ndarray

Array of external ids.

classmethod from_dict(mapping: Dict[Hashable, int]) IdMap[source]

Create IdMap from dict of external id -> internal id mappings. Could be used if mappings were previously defined somewhere else.

Parameters

mapping (dict(hashable, int) :) – Dict of mappings from external ids to internal ids.

Return type

IdMap

classmethod from_values(values: Union[Sequence[Hashable], ndarray]) IdMap[source]

Create IdMap from list of external ids.

Parameters

values (iterable(hashable) :) – List of external ids (may be not unique).

Return type

IdMap

get_external_sorted_by_internal() ndarray[source]

Return array of external ids sorted by internal ids.

Return type

ndarray

get_sorted_internal() ndarray[source]

Return array of sorted internal ids.

Return type

ndarray

property internal_ids: ndarray

Array of internal ids.

property to_external: Series

Map internal->external.