Skip to content

TimeDelta

timedelta

Classes:

  • TimeDeltaDG

    Represents the time granularity for a temporal index in a dynamic graph.

TimeDeltaDG dataclass

TimeDeltaDG(unit: str, value: int = 1)

Represents the time granularity for a temporal index in a dynamic graph.

This class is used to define the resolution at which events or interactions are indexed in a dynamic/temporal graph. It supports both standard temporal units (e.g., seconds, minutes, days) and a special event-ordered unit for strictly sequential indices.

Parameters:

  • unit (str) –

    The time unit, e.g., 's', 'm', 'h', 'D', or 'r' for event-ordered.

  • value (int, default: 1 ) –

    Multiplier for the unit. Must be a positive integer.

Raises:

  • ValueError

    If value is not a positive integer.

  • ValueError

    If unit is event-ordered and value != 1.

  • ValueError

    If unit is not recognized among allowed temporal units.

Note

For event-ordered units ('r'), only value = 1 is permitted.

Methods:

  • convert

    Convert this granularity into the scale of another time delta.

  • is_coarser_than

    Return True if this granularity is strictly coarser than other.

Attributes:

is_event_ordered property
is_event_ordered: bool

Return True if this is the special event-ordered unit ('r').

is_time_ordered property
is_time_ordered: bool

Return True if this is not special event-ordered unit ('r').

convert
convert(time_delta: str | TimeDeltaDG) -> float

Convert this granularity into the scale of another time delta.

Parameters:

  • time_delta (str | TimeDeltaDG) –

    Target time delta to convert into.

Returns:

  • float ( float ) –

    Ratio of self to target granularity.

Raises:

  • EventOrderedConversionError

    If either self or target granularity is event-ordered.

Source code in tgm/core/timedelta.py
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def convert(self, time_delta: str | TimeDeltaDG) -> float:
    """Convert this granularity into the scale of another time delta.

    Args:
        time_delta (str | TimeDeltaDG): Target time delta to convert into.

    Returns:
        float: Ratio of self to target granularity.

    Raises:
        EventOrderedConversionError: If either self or target granularity is event-ordered.
    """
    if isinstance(time_delta, str):
        time_delta = TimeDeltaDG(time_delta)
    if self.is_event_ordered or time_delta.is_event_ordered:
        raise EventOrderedConversionError(
            'Cannot compare granularity for event-ordered TimeDeltaDG'
        )
    return self._convert(time_delta)
is_coarser_than
is_coarser_than(other: str | TimeDeltaDG) -> bool

Return True if this granularity is strictly coarser than other.

Parameters:

Raises:

  • EventOrderedConversionError

    If either self or other is event-ordered.

Source code in tgm/core/timedelta.py
68
69
70
71
72
73
74
75
76
77
def is_coarser_than(self, other: str | TimeDeltaDG) -> bool:
    """Return True if this granularity is strictly coarser than `other`.

    Args:
        other (str | TimeDeltaDG): The time delta to compare against.

    Raises:
        EventOrderedConversionError: If either self or `other` is event-ordered.
    """
    return self.convert(other) > 1