Color Space¶
Color mapping from gene set scores to RGB (pipeline steps 2–3).
Both blend_to_rgb and reduce_to_rgb return an RGBResult object that wraps
the RGB array with metadata (method, gene set names, colors). This metadata is
auto-detected by the plotting functions.
RGBResult¶
RGBResult
dataclass
¶
RGBResult(
rgb: NDArray,
method: str,
gene_set_names: list[str],
colors: list[tuple[float, float, float]] | None = None,
prefix: str = SCORE_PREFIX,
suffix: str = "",
)
Container for RGB mapping results with metadata.
Returned by :func:blend_to_rgb and :func:reduce_to_rgb. Carries
the (n_cells, 3) RGB array together with metadata that downstream
plotting functions can use automatically.
The object supports the numpy array protocol, so np.asarray(result)
returns the underlying RGB array and indexing/slicing works transparently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rgb
|
NDArray
|
|
required |
method
|
str
|
|
required |
gene_set_names
|
list[str]
|
Human-readable gene set labels, derived from score column names. |
required |
colors
|
list[tuple[float, float, float]] | None
|
Base colours used for blending (only set for |
None
|
prefix
|
str
|
Column name prefix used to identify score columns (default |
SCORE_PREFIX
|
suffix
|
str
|
Column name suffix stripped from gene set names (default |
''
|
Blending (2–3 gene sets)¶
blend_to_rgb
¶
blend_to_rgb(
scores: DataFrame,
*,
colors: list[tuple[float, float, float]] | None = None,
prefix: str = SCORE_PREFIX,
suffix: str = "",
) -> RGBResult
Map gene set scores to RGB via multiplicative blending from white.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scores
|
DataFrame
|
DataFrame returned by :func: |
required |
colors
|
list[tuple[float, float, float]] | None
|
One |
None
|
prefix
|
str
|
Column name prefix identifying score columns (default |
SCORE_PREFIX
|
suffix
|
str
|
Column name suffix identifying score columns (default |
''
|
Returns:
| Type | Description |
|---|---|
RGBResult
|
Contains the |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than 2 or more than 3 gene sets are present, or if the number of supplied colours does not match expectations. |
Source code in src/multiscoresplot/_colorspace.py
Dimensionality Reduction (2+ gene sets)¶
reduce_to_rgb
¶
reduce_to_rgb(
scores: DataFrame,
*,
method: str | Callable[..., NDArray] = "pca",
n_components: int = 3,
component_prefix: str | None = None,
prefix: str = SCORE_PREFIX,
suffix: str = "",
**kwargs: object,
) -> RGBResult
Map gene set scores to RGB via dimensionality reduction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scores
|
DataFrame
|
DataFrame returned by :func: |
required |
method
|
str | Callable[..., NDArray]
|
Reduction method. Can be a string ( |
'pca'
|
n_components
|
int
|
Number of components to retain (max 3 for RGB). |
3
|
component_prefix
|
str | None
|
Label prefix for legend axes (e.g. |
None
|
prefix
|
str
|
Column name prefix identifying score columns (default |
SCORE_PREFIX
|
suffix
|
str
|
Column name suffix identifying score columns (default |
''
|
**kwargs
|
object
|
Extra keyword arguments forwarded to the reducer function. |
{}
|
Returns:
| Type | Description |
|---|---|
RGBResult
|
Contains the |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than 2 gene sets are present or method is an unknown string. |
TypeError
|
If method is neither a string nor a callable. |
Notes
For one-off custom reductions, passing a callable directly is more
convenient than :func:register_reducer. For reusable methods that
should appear in error messages and be available by name, use
:func:register_reducer instead.
Color interpretation caveat: Reduction methods project the full score
matrix into just 3 dimensions. Only the top 3 axes of variation are
captured — all other differences are collapsed. Two cells with very
different gene set score profiles can appear the same color if their
differences lie along dropped components. Additionally, the R, G, and B
channels represent learned linear combinations of all gene set scores, not
individual gene sets. For more interpretable components use
method="nmf" or :func:blend_to_rgb for ≤ 3 gene sets.
Source code in src/multiscoresplot/_colorspace.py
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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 | |
Custom Reducers¶
register_reducer
¶
register_reducer(
name: str,
fn: Callable[..., NDArray],
*,
component_prefix: str | None = None,
) -> None
Register a dimensionality reduction method for use with reduce_to_rgb.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Short identifier (e.g. |
required |
fn
|
Callable[..., NDArray]
|
Callable with signature |
required |
component_prefix
|
str | None
|
Label prefix for legend axes (e.g. |
None
|
Source code in src/multiscoresplot/_colorspace.py
Utility¶
get_component_labels
¶
Return ["<prefix>1", "<prefix>2", "<prefix>3"] for a registered method.