Skip to contents

iv_count_relates() is similar to iv_count_overlaps(), but it counts a specific set of relations developed by James Allen in the paper: Maintaining Knowledge about Temporal Intervals.

Usage

iv_count_relates(
  needles,
  haystack,
  ...,
  type,
  missing = "equals",
  no_match = 0L
)

Arguments

needles, haystack

[iv]

Interval vectors used for relation matching.

  • Each element of needles represents the interval to search for.

  • haystack represents the intervals to search in.

Prior to comparison, needles and haystack are coerced to the same type.

...

These dots are for future extensions and must be empty.

type

[character(1)]

The type of relationship to find. See the Allen's Interval Algebra section for a complete description of each type. One of:

  • "precedes"

  • "preceded-by"

  • "meets"

  • "met-by"

  • "overlaps"

  • "overlapped-by"

  • "starts"

  • "started-by"

  • "during"

  • "contains"

  • "finishes"

  • "finished-by"

  • "equals"

missing

[integer(1) / "equals" / "error"]

Handling of missing intervals in needles.

  • "equals" considers missing intervals in needles as exactly equal to missing intervals in haystack when determining if there is a matching relationship between them.

  • "error" throws an error if any intervals in needles are missing.

  • If a single integer value is provided, this represents the count returned for a missing interval in needles. Use 0L to force missing intervals to never match.

no_match

[integer(1) / "error"]

Handling of needles without a match.

  • "error" throws an error if any needles have zero matches.

  • If a single integer is provided, this represents the count returned for a needle with zero matches. The default value gives unmatched needles a count of 0L.

Value

An integer vector the same size as needles.

Allen's Interval Algebra

The interval algebra developed by James Allen serves as a basis and inspiration for iv_locate_overlaps(), iv_locate_precedes(), and iv_locate_follows(). The original algebra is composed of 13 relations which have the following properties:

  • Distinct: No pair of intervals can be related by more than one type.

  • Exhaustive: All pairs of intervals are described by one of the types.

  • Qualitative: No numeric intervals are considered. The relationships are computed by purely qualitative means.

Take the notation that x and y represent two intervals. Now assume that x can be represented as [x_s, x_e), where x_s is the start of the interval and x_e is the end of it. Additionally, assume that x_s < x_e. With this notation, the 13 relations are as follows:

  • Precedes:

    x_e < y_s

  • Preceded-by:

    x_s > y_e

  • Meets:

    x_e == y_s

  • Met-by:

    x_s == y_e

  • Overlaps:

    (x_s < y_s) & (x_e > y_s) & (x_e < y_e)

  • Overlapped-by:

    (x_e > y_e) & (x_s < y_e) & (x_s > y_s)

  • Starts:

    (x_s == y_s) & (x_e < y_e)

  • Started-by:

    (x_s == y_s) & (x_e > y_e)

  • Finishes:

    (x_s > y_s) & (x_e == y_e)

  • Finished-by:

    (x_s < y_s) & (x_e == y_e)

  • During:

    (x_s > y_s) & (x_e < y_e)

  • Contains:

    (x_s < y_s) & (x_e > y_e)

  • Equals:

    (x_s == y_s) & (x_e == y_e)

Note that when missing = "equals", missing intervals will only match the type = "equals" relation. This ensures that the distinct property of the algebra is maintained.

Connection to other functions

Note that some of the above relations are fairly restrictive. For example, "overlaps" only detects cases where x straddles y_s. It does not consider the case where x and y are equal to be an overlap (as this is "equals") nor does it consider when x straddles y_e to be an overlap (as this is "overlapped-by"). This makes the relations extremely useful from a theoretical perspective, because they can be combined without fear of duplicating relations, but they don't match our typical expectations for what an "overlap" is.

iv_locate_overlaps(), iv_locate_precedes(), and iv_locate_follows() use more intuitive types that aren't distinct, but typically match your expectations better. They can each be expressed in terms of Allen's relations:

  • iv_locate_overlaps():

    • "any":

      overlaps | overlapped-by | starts | started-by | finishes | finished-by | during | contains | equals

    • "contains":

      contains | started-by | finished-by | equals

    • "within":

      during | starts | finishes | equals

    • "starts":

      starts | started-by | equals

    • "ends":

      finishes | finished-by | equals

    • "equals":

      equals

  • iv_locate_precedes():

    precedes | meets

  • iv_locate_follows():

    preceded-by | met-by

Examples

x <- iv(1, 3)
y <- iv(3, 4)

# `"precedes"` is strict, and doesn't let the endpoints match
iv_count_relates(x, y, type = "precedes")
#> [1] 0

# Since that is what `"meets"` represents
iv_count_relates(x, y, type = "meets")
#> [1] 1

# `"overlaps"` is a very specific type of overlap where an interval in
# `needles` straddles the start of an interval in `haystack`
x <- iv_pairs(c(1, 4), c(1, 3), c(0, 3), c(2, 5))
y <- iv(1, 4)

# It doesn't match equality, or when the starts match, or when the end
# of the interval in `haystack` is straddled instead
iv_count_relates(x, y, type = "overlaps")
#> [1] 0 0 1 0