graph_coloring¶
tissue_simulator.graph_coloring ¶
Graph coloring module for assigning cell types based on network statistics.
This module integrates simulated annealing-based graph coloring to assign cell types to tissue networks based on target statistical properties.
GraphColorizer ¶
GraphColorizer(source_graph: Graph = None, target_graph: Graph = None, colors: list = None, target_statistics: Dict = None, seed: Optional[int] = None)
Imposes network statistics from target data onto a graph by finding an optimal node coloring using Simulated Annealing.
The statistics matched are: 1. Node color counts. 2. Pairwise edge counts between all combinations of colors. 3. Neighbor color distribution (average number of neighbors of color X for a node of color Y).
To accelerate repeated runs (e.g. generating tissue replicates),
:meth:colorize supports warm-starting from a caller-supplied initial
coloring (initial_coloring).
Initializes the GraphColorizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_graph
|
Graph
|
Graph with 'color' attribute (optional if target_statistics provided) |
None
|
target_graph
|
Graph
|
Graph to be colored |
None
|
colors
|
list
|
List of possible color strings (e.g., ['cancer', 'immune', 'stroma']) |
None
|
target_statistics
|
Dict
|
Pre-calculated target statistics (optional) |
None
|
seed
|
Optional[int]
|
Optional integer seed for the simulated-annealing RNG. When
provided, the colorizer uses an instance-bound
|
None
|
Source code in tissue_simulator/graph_coloring.py
cost_terms ¶
Unweighted squared-error components of the cost for stats.
Keys: edge (edge counts), neighbor (mean neighbor counts) and
spatial (per-bin color counts; 0 without a spatial target).
Source code in tissue_simulator/graph_coloring.py
colorize ¶
colorize(initial_temp=100.0, final_temp=0.1, cooling_rate=0.995, max_iterations=100000, verbose=True, initial_coloring=None, patience=None, min_delta=1e-09, return_history=False)
Performs the simulated annealing process to find the optimal coloring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial_temp
|
Starting temperature |
100.0
|
|
final_temp
|
Temperature at which to stop |
0.1
|
|
cooling_rate
|
Rate at which temperature decreases (e.g., 0.99 -> slow, 0.9 -> fast) |
0.995
|
|
max_iterations
|
Maximum number of iterations |
100000
|
|
verbose
|
If True, prints progress updates |
True
|
|
initial_coloring
|
Optional dict mapping nodes to colors to use as the
starting (warm-start) coloring instead of the random
node-counts-based shuffle. Every value must be one of
|
None
|
|
patience
|
Optional adaptive-stopping budget. When set, the search
stops early once |
None
|
|
min_delta
|
Minimum |
1e-09
|
|
return_history
|
When True, return |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Best coloring found for the target graph (or |
|
|
|
Source code in tissue_simulator/graph_coloring.py
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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | |
color_graph_to_targets ¶
color_graph_to_targets(target_graph: Graph, colors: List[str], target_statistics: Dict, seed: Optional[int] = None, initial_coloring: Optional[Dict] = None, return_cost: bool = False, **colorize_kwargs)
Colorize target_graph to match target_statistics via simulated annealing.
Thin shared wrapper around :class:GraphColorizer so callers
(ReplicateGenerator, TissueNetworkWorkflow) use a single code path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_graph
|
Graph
|
NetworkX graph to color. |
required |
colors
|
List[str]
|
List of color / cell-type names. |
required |
target_statistics
|
Dict
|
GraphColorizer-format target dict
( |
required |
seed
|
Optional[int]
|
Optional RNG seed for bit-reproducible annealing. |
None
|
initial_coloring
|
Optional[Dict]
|
Optional warm-start coloring passed to |
None
|
return_cost
|
bool
|
When True, also return the final cost of the returned coloring (useful for picking the best of several restarts). |
False
|
**colorize_kwargs
|
Forwarded to :meth: |
{}
|
Returns:
| Type | Description |
|---|---|
|
dict mapping node -> color (the best coloring found), or |
|
|
|
Source code in tissue_simulator/graph_coloring.py
calculate_graph_statistics ¶
Calculate node and edge count statistics for a colored graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
NetworkX graph |
required |
colors_map
|
Dict
|
Dictionary mapping node IDs to colors |
required |
color_names
|
List[str]
|
List of all possible color names |
required |
Returns:
| Type | Description |
|---|---|
Dict
|
Dictionary of statistics |
Source code in tissue_simulator/graph_coloring.py
compare_graph_statistics ¶
Calculate and optionally print the percent difference between two sets of statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_stats
|
Dict
|
Statistics from source/target graph |
required |
target_stats
|
Dict
|
Statistics from generated graph |
required |
verbose
|
bool
|
Whether to print comparison details |
True
|
Returns:
| Type | Description |
|---|---|
Dict
|
Dictionary of differences for each statistic |
Source code in tissue_simulator/graph_coloring.py
load_target_statistics_from_csv ¶
Load target statistics from a CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to CSV file with statistics |
required |
color_names
|
List[str]
|
List of color names to use |
required |
Returns:
| Type | Description |
|---|---|
Dict
|
Dictionary with target statistics in GraphColorizer format |
Source code in tissue_simulator/graph_coloring.py
export_colored_graph_statistics ¶
export_colored_graph_statistics(graph: Graph, colors_map: Dict, color_names: List[str], filename: str)
Export graph statistics to CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
NetworkX graph |
required |
colors_map
|
Dict
|
Dictionary mapping node IDs to colors |
required |
color_names
|
List[str]
|
List of color names |
required |
filename
|
str
|
Output CSV filename |
required |
Source code in tissue_simulator/graph_coloring.py
visualize_colored_graph ¶
visualize_colored_graph(graph: Graph, colors_map: Dict, color_palette: Dict = None, layout: str = 'spring', title: str = 'Colored Graph', save_path: str = None, figsize: Tuple[int, int] = (12, 10))
Visualize a colored graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
NetworkX graph |
required |
colors_map
|
Dict
|
Dictionary mapping node IDs to colors |
required |
color_palette
|
Dict
|
Dictionary mapping color names to RGB values |
None
|
layout
|
str
|
Layout algorithm ("spring", "kamada_kawai", "circular") |
'spring'
|
title
|
str
|
Plot title |
'Colored Graph'
|
save_path
|
str
|
If provided, save figure to this path |
None
|
figsize
|
Tuple[int, int]
|
Figure size |
(12, 10)
|
Source code in tissue_simulator/graph_coloring.py
visualize_graph_comparison ¶
visualize_graph_comparison(source_graph: Graph, source_colors: Dict, target_graph: Graph, target_colors: Dict, color_palette: Dict = None, save_path: str = 'graph_comparison.png', figsize: Tuple[int, int] = (16, 8))
Visualize source and target graphs side by side.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_graph
|
Graph
|
Source graph with target statistics |
required |
source_colors
|
Dict
|
Color mapping for source graph |
required |
target_graph
|
Graph
|
Target graph after coloring |
required |
target_colors
|
Dict
|
Color mapping for target graph |
required |
color_palette
|
Dict
|
Dictionary mapping color names to RGB values |
None
|
save_path
|
str
|
Path to save visualization |
'graph_comparison.png'
|
figsize
|
Tuple[int, int]
|
Figure size |
(16, 8)
|