Row feature index
Feature index abstractions for SCDL.
This module defines an abstract base RowFeatureIndex
and two concrete
implementations:
ObservedFeatureIndex
: row-oriented features, where a lookup returns one scalar per selected feature for a given row.VariableFeatureIndex
: column-oriented features, where a lookup returns full feature arrays for the block containing a given row.
Data are stored in blocks of feature dictionaries. _cumulative_sum_index
tracks row boundaries between blocks. The feature dictionarires are stroed in _feature_arr
.
ObservedFeatureIndex
Bases: RowFeatureIndex
Feature index for observed (row) features.
Each block is a dictionary mapping feature name to the full column array. A lookup at a row returns one scalar per selected feature and the block label. Successive blocks with identical schemas (same keys) are merged by concatenating column arrays.
Source code in bionemo/scdl/index/row_feature_index.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
|
__getitem__(idx)
Access one row or a slice of rows for observed features (.obs).
- If
idx
is an int, returns(values, label)
for that row:values
is a list of scalar values (one per selected feature)label
is the block label
- If
idx
is a slice, returns(blocks, labels)
:blocks
is a list of feature dictionaries, one per intersected block, with arrays sliced to the selected rows for that block.labels
is a list of block labels in the same order.
Source code in bionemo/scdl/index/row_feature_index.py
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
|
__init__()
Create an observed (row) feature index.
Source code in bionemo/scdl/index/row_feature_index.py
321 322 323 |
|
append_features(features, label=None)
Append features, delegating validation and merge behavior to subclasses.
Source code in bionemo/scdl/index/row_feature_index.py
440 441 442 443 444 445 446 447 |
|
concat(other_row_index)
Concatenates the other ObservedFeatureIndex to this one.
Returns the new, updated index. Warning: modifies this index in-place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other_row_index
|
ObservedFeatureIndex
|
another ObservedFeatureIndex |
required |
Returns:
Type | Description |
---|---|
ObservedFeatureIndex
|
self, the RowIndexFeature after the concatenations. |
Source code in bionemo/scdl/index/row_feature_index.py
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
|
load(datapath)
staticmethod
Load a observed (row) feature index from a directory.
This will load the parquet files in sorted order. In the SCDL use case, this expects a directory with
parquet files named dataframe_
Source code in bionemo/scdl/index/row_feature_index.py
361 362 363 364 365 366 367 368 |
|
lookup(row, select_features=None)
Return scalar feature values and block label for a given row.
Source code in bionemo/scdl/index/row_feature_index.py
343 344 345 346 347 348 349 350 351 352 353 354 |
|
RowFeatureIndex
Bases: ABC
Abstract base for ragged feature indices.
Represents datasets where the number and/or shape of features can differ per row. Data are organized in blocks (feature dictionaries), with a cumulative sum index delineating block boundaries.
Attributes:
Name | Type | Description |
---|---|---|
_cumulative_sum_index |
array
|
Cumulative row counts that delineate block
boundaries. For example, with |
_feature_arr |
list[dict[str, ndarray]]
|
List of feature dictionaries, one per block. |
_num_entries_per_row |
list[int]
|
Per-block counts used by |
_labels |
list[Optional[str]]
|
Optional label per block (e.g., dataset ID or name). |
_version |
Version string for the dataset. |
Source code in bionemo/scdl/index/row_feature_index.py
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
|
__init__()
Instantiates the index.
Source code in bionemo/scdl/index/row_feature_index.py
90 91 92 93 94 95 96 |
|
__len__()
Return the number of blocks (feature dictionaries).
Source code in bionemo/scdl/index/row_feature_index.py
175 176 177 |
|
append_features(*args, **kwargs)
abstractmethod
Append features, delegating validation and merge behavior to subclasses.
May or may not have n_obs as an argument, depending on subclass.
Source code in bionemo/scdl/index/row_feature_index.py
279 280 281 282 283 284 285 |
|
column_dims()
Return per-block feature counts.
For ObservedFeatureIndex
, this is the number of feature columns per
block. For VariableFeatureIndex
, this is the per-row array length per
block.
Source code in bionemo/scdl/index/row_feature_index.py
215 216 217 218 219 220 221 222 |
|
concat(other_row_index)
abstractmethod
Concatenate another FeatureIndex to this one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other_row_index
|
'RowFeatureIndex'
|
another FeatureIndex |
required |
Returns:
Type | Description |
---|---|
'RowFeatureIndex'
|
self (updated). |
Source code in bionemo/scdl/index/row_feature_index.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
|
number_of_rows()
The number of rows in the index.
Returns:
Type | Description |
---|---|
int
|
An integer corresponding to the number or rows in the index |
Source code in bionemo/scdl/index/row_feature_index.py
287 288 289 290 291 292 293 |
|
number_of_values()
Return total value counts per block.
For each block, (rows in block) * (per-block feature count)
. Returns
[0]
when there are no blocks.
Source code in bionemo/scdl/index/row_feature_index.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
|
number_vars_at_row(row)
Return the number of variables for the block that contains row
.
Source code in bionemo/scdl/index/row_feature_index.py
210 211 212 213 |
|
save(datapath)
Saves the FeatureIndex to a given path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
datapath
|
str
|
path to save the index |
required |
Source code in bionemo/scdl/index/row_feature_index.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
|
version()
Returns a version number.
(following
Source code in bionemo/scdl/index/row_feature_index.py
168 169 170 171 172 173 |
|
VariableFeatureIndex
Bases: RowFeatureIndex
Feature index for variables (columns).
Lookup returns full arrays for the block that contains a given row. When
successive blocks have identical feature dictionaries, they are merged and
only _cumulative_sum_index
advances.
Source code in bionemo/scdl/index/row_feature_index.py
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 |
|
__init__()
Create a variable (column) feature index.
Source code in bionemo/scdl/index/row_feature_index.py
458 459 460 |
|
append_features(n_obs, features, label=None)
Append a new block, or merge into the last block when possible.
Source code in bionemo/scdl/index/row_feature_index.py
493 494 495 496 497 |
|
concat(other_row_index)
Concatenates the other VariableFeatureIndex to this one.
Returns the new, updated index. Warning: modifies this index in-place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other_row_index
|
VariableFeatureIndex
|
another VariableFeatureIndex |
required |
Returns:
Type | Description |
---|---|
VariableFeatureIndex
|
self, the VariableFeatureIndex after the concatenations. |
Source code in bionemo/scdl/index/row_feature_index.py
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 |
|
load(datapath)
staticmethod
Load a variable (column) feature index from a directory.
This will load the parquet files in sorted order. In the SCDL use case, this expects a directory with
parquet files named dataframe_
Source code in bionemo/scdl/index/row_feature_index.py
477 478 479 480 481 482 483 484 |
|
lookup(row, select_features=None)
Return feature arrays and the block label for the block containing row
.
Source code in bionemo/scdl/index/row_feature_index.py
486 487 488 489 490 491 |
|
are_dicts_equal(dict1, dict2)
Compare two dictionaries with string keys and numpy.ndarray values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dict1
|
dict[str, ndarray]
|
The first dictionary to compare. |
required |
dict2
|
dict[str, ndarray]
|
The second dictionary to compare. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the dictionaries have the same keys and all corresponding numpy arrays are equal; False otherwise. |
Source code in bionemo/scdl/index/row_feature_index.py
57 58 59 60 61 62 63 64 65 66 67 68 |
|