1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
class _IndexingMixin:
def _unpack_index(self, key):
if isinstance(key, tuple):
if len(key) == 2:
row, col = key
elif len(key) == 1:
row, col = key[0], slice(None)
else:
raise IndexError("invalid number of indices")
else:
row, col = key, slice(None)
return row, col
def _isintlike(self, num):
try:
int(num)
except (TypeError, ValueError):
return False
return True
def _process_slice(self, s, nmax):
if isinstance(s, slice):
if s.step not in (1, None):
raise ValueError("slicing with step != 1 not supported")
i0, i1 = s.start, s.stop
if i0 is None:
i0 = 0
elif i0 < 0:
i0 = nmax + i0
if i1 is None:
i1 = nmax
elif i1 < 0:
i1 = nmax + i1
return i0, i1
elif self._isintlike(s):
if s < 0:
s += nmax
if s >= nmax:
raise IndexError("index is out of bounds")
return int(s), int(s + 1)
else:
raise TypeError("expected slice or scalar")
class RangeSelector1D(_IndexingMixin):
"""
Selector for out-of-core tabular data. Provides DataFrame-like selection of
columns and list-like access to rows.
Examples
--------
Passing a column name or list of column names as subscript returns a new
selector.
>>> sel[ ['A', 'B'] ] # doctest: +SKIP
>>> sel['C']
Passing a scalar or slice as subscript invokes the slicer.
>>> sel[0] # doctest: +SKIP
>>> sel['A'][50:100]
Calling the fetch method invokes the fetcher to parse the input into an
integer range and then invokes the slicer.
>>> sel.fetch('chr3:10,000,000-12,000,000') # doctest: +SKIP
>>> sel.fetch(('chr3', 10000000, 12000000))
"""
def __init__(self, fields, slicer, fetcher, nmax):
self.fields = fields
self._slice = slicer
self._fetch = fetcher
self._shape = (nmax,)
@property
def shape(self):
return self._shape
@property
def columns(self):
return self._slice(self.fields, 0, 0).columns
@property
def dtypes(self):
return self._slice(self.fields, 0, 0).dtypes
def keys(self):
return list(self.columns)
def __len__(self):
return self._shape[0]
def __contains__(self, key):
return key in self.columns
def __getitem__(self, key):
# requesting a subset of columns
if isinstance(key, (list, str)):
return self.__class__(key, self._slice, self._fetch, self._shape[0])
# requesting an interval of rows
if isinstance(key, tuple):
if len(key) == 1:
key = key[0]
else:
raise IndexError("too many indices for table")
lo, hi = self._process_slice(key, self._shape[0])
return self._slice(self.fields, lo, hi)
def fetch(self, *args, **kwargs):
if self._fetch is not None:
lo, hi = self._fetch(*args, **kwargs)
return self._slice(self.fields, lo, hi)
else:
raise NotImplementedError
class RangeSelector2D(_IndexingMixin):
"""
Selector for out-of-core sparse matrix data. Supports 2D scalar and slice
subscript indexing.
"""
def __init__(self, field, slicer, fetcher, shape):
self.field = field
self._slice = slicer
self._fetch = fetcher
self._shape = shape
@property
def shape(self):
return self._shape
def __len__(self):
return self._shape[0]
def __getitem__(self, key):
s1, s2 = self._unpack_index(key)
i0, i1 = self._process_slice(s1, self._shape[0])
j0, j1 = self._process_slice(s2, self._shape[1])
return self._slice(self.field, i0, i1, j0, j1)
def fetch(self, *args, **kwargs):
if self._fetch is not None:
i0, i1, j0, j1 = self._fetch(*args, **kwargs)
return self._slice(self.field, i0, i1, j0, j1)
else:
raise NotImplementedError
|