piso.adjacency_matrix#

piso.adjacency_matrix(interval_array, *interval_arrays, edges='intersect', include_index=True)#

Returns a 2D array (or dataframe) of boolean values indicating edges between nodes in a graph.

The nodes correspond to sets and the edges are defined by the relationship defined by the edges parameter.

What is considered a set is determined by the number of positional arguments used, that is, determined by the size of interval_arrays.

If interval_arrays is empty then the sets are considered to be the intervals contained in interval_array.

If interval_arrays is not empty then the sets are considered to be interval_array and the elements in interval_arrays. Each of these arrays is assumed to contain disjoint intervals (and satisfy the definition of a set). Any array containing overlaps between intervals will be mapped to one with disjoint intervals via a union operation.

Note that the diagonal is defined with False values by default.

Parameters
interval_arraypandas.arrays.IntervalArray or pandas.IntervalIndex

The first (and possibly only) operand.

*interval_arraysargument list of pandas.IntervalIndex or pandas.arrays.IntervalArray

Must contain at least one argument.

edges{“intersect”, “disjoint”}, default “intersect”

Defines the relationship that edges between nodes represent.

include_indexbool, default True

If True then a pandas.DataFrame, indexed by the intervals, is returned. If False then a numpy.ndarray is returned.

Returns
pandas.DataFrame or numpy.ndarray

Boolean valued, symmetrical, with False along diagonal.

Examples

>>> import pandas as pd
>>> import piso
>>> arr = pd.arrays.IntervalArray.from_tuples(
...    [(0,4), (3,6), (5, 7), (8,9), (9,10)],
...    closed="both",
... )
>>> piso.adjacency_matrix(arr)
         [0, 4]  [3, 6]  [5, 7]  [8, 9]  [9, 10]
[0, 4]    False    True   False   False    False
[3, 6]     True   False    True   False    False
[5, 7]    False    True   False   False    False
[8, 9]    False   False   False   False     True
[9, 10]   False   False   False    True    False
>>> piso.adjacency_matrix(arr, include_index=False)
array([[False,  True, False, False, False],
       [ True, False,  True, False, False],
       [False,  True, False, False, False],
       [False, False, False, False,  True],
       [False, False, False,  True, False]])
>>> piso.adjacency_matrix(arr, edges="disjoint")
         [0, 4]  [3, 6]  [5, 7]  [8, 9]  [9, 10]
[0, 4]    False   False    True    True     True
[3, 6]    False   False   False    True     True
[5, 7]     True   False   False    True     True
[8, 9]     True    True    True   False    False
[9, 10]    True    True    True   False    False
>>> ii1 = pd.IntervalIndex.from_tuples([(0,3), (2,8), (11,15)], closed="left")
>>> ii2 = pd.IntervalIndex.from_tuples([(3,5), (7,12), (16,20)], closed="left")
>>> ii3 = pd.IntervalIndex.from_tuples([(9,11), (25,26)], closed="left")
>>> ii4 = pd.IntervalIndex.from_tuples([(23,24)], closed="left")
>>> piso.adjacency_matrix(ii1,ii2,ii3,ii4)
            0           1           2       3
0   False   True    False   False
1   True    False   True    False
2   False   True    False   False
3   False   False   False   False
>>> piso.adjacency_matrix(ii1,ii2,ii3,ii4, edges="disjoint", include_index=False)
array([[False, False,  True,  True],
       [False, False, False,  True],
       [ True, False, False,  True],
       [ True,  True,  True, False]])