spatial_analysis¶
tissue_simulator.spatial_analysis ¶
Spatial analysis module for tissue simulator.
This module provides network-based spatial analysis of cell-cell interactions using NetworkX. It can analyze both 3D tissues and 2D slices.
NetworkStatistics
dataclass
¶
NetworkStatistics(total_nodes: int, total_edges: int, avg_degree: float, network_density: float, avg_clustering: float, transitivity: float, avg_path_length: Optional[float], diameter: Optional[int], is_connected: bool, num_components: int)
Container for network analysis statistics.
Attributes:
| Name | Type | Description |
|---|---|---|
total_nodes |
int
|
Total number of nodes (cells) in network |
total_edges |
int
|
Total number of edges (connections) |
avg_degree |
float
|
Average number of connections per cell |
network_density |
float
|
Ratio of actual to possible connections |
avg_clustering |
float
|
Average clustering coefficient |
transitivity |
float
|
Global clustering coefficient |
avg_path_length |
Optional[float]
|
Average shortest path length (if connected) |
diameter |
Optional[int]
|
Maximum shortest path length (if connected) |
is_connected |
bool
|
Whether the network is fully connected |
num_components |
int
|
Number of connected components |
CellTypeStatistics
dataclass
¶
CellTypeStatistics(cell_type: str, count: int, avg_degree: float, avg_clustering: float, degree_centrality: float, betweenness_centrality: float, closeness_centrality: float)
Statistics for a specific cell type in the network.
Attributes:
| Name | Type | Description |
|---|---|---|
cell_type |
str
|
Name of the cell type |
count |
int
|
Number of cells of this type |
avg_degree |
float
|
Average number of connections |
avg_clustering |
float
|
Average clustering coefficient |
degree_centrality |
float
|
Average degree centrality |
betweenness_centrality |
float
|
Average betweenness centrality |
closeness_centrality |
float
|
Average closeness centrality |
InteractionStatistics
dataclass
¶
InteractionStatistics(type_a: str, type_b: str, num_interactions: int, normalized_interactions: float, avg_distance: float, median_distance: float)
Statistics for interactions between two cell types.
Attributes:
| Name | Type | Description |
|---|---|---|
type_a |
str
|
First cell type |
type_b |
str
|
Second cell type |
num_interactions |
int
|
Number of connections between types |
normalized_interactions |
float
|
Interactions normalized by cell counts |
avg_distance |
float
|
Average distance of connections |
median_distance |
float
|
Median distance of connections |
SpatialNetworkAnalyzer ¶
Analyze spatial relationships between cells using network analysis.
Creates a network where nodes are cells and edges represent spatial relationships (contact or proximity).
Initialize the analyzer.
Source code in tissue_simulator/spatial_analysis.py
build_network_from_tissue ¶
build_network_from_tissue(tissue: TissueSection, mode: str = 'contact', radius: Optional[float] = None) -> nx.Graph
Build a spatial network from a 3D tissue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tissue
|
TissueSection
|
TissueSection to analyze |
required |
mode
|
str
|
"contact" for touching cells, "radius" for proximity |
'contact'
|
radius
|
Optional[float]
|
Distance threshold for "radius" mode (in micrometers) |
None
|
Returns:
| Type | Description |
|---|---|
Graph
|
NetworkX graph with cells as nodes and spatial relationships as edges |
Source code in tissue_simulator/spatial_analysis.py
build_network_from_slice ¶
build_network_from_slice(slicer: TissueSlicer, mode: str = 'contact', radius: Optional[float] = None) -> nx.Graph
Build a spatial network from a 2D slice.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slicer
|
TissueSlicer
|
TissueSlicer with computed slice |
required |
mode
|
str
|
"contact" for touching cells, "radius" for proximity |
'contact'
|
radius
|
Optional[float]
|
Distance threshold for "radius" mode (in micrometers) |
None
|
Returns:
| Type | Description |
|---|---|
Graph
|
NetworkX graph with cells as nodes and spatial relationships as edges |
Source code in tissue_simulator/spatial_analysis.py
compute_global_statistics ¶
Compute global network statistics.
Returns:
| Type | Description |
|---|---|
NetworkStatistics
|
NetworkStatistics object with global metrics |
Source code in tissue_simulator/spatial_analysis.py
compute_cell_type_statistics ¶
Compute statistics for each cell type.
Returns:
| Type | Description |
|---|---|
Dict[str, CellTypeStatistics]
|
Dictionary mapping cell type to CellTypeStatistics |
Source code in tissue_simulator/spatial_analysis.py
compute_interaction_statistics ¶
Compute pairwise interaction statistics between cell types.
Returns:
| Type | Description |
|---|---|
List[InteractionStatistics]
|
List of InteractionStatistics for each cell type pair |
Source code in tissue_simulator/spatial_analysis.py
get_comprehensive_analysis ¶
Get a comprehensive analysis of the spatial network.
Returns:
| Type | Description |
|---|---|
Dict
|
Dictionary containing all statistics |
Source code in tissue_simulator/spatial_analysis.py
export_network ¶
Export the network to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Output filename |
required |
format
|
str
|
"graphml", "gexf", "gml", or "edgelist" |
'graphml'
|
Source code in tissue_simulator/spatial_analysis.py
export_statistics_csv ¶
Export all statistics to CSV files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_filename
|
str
|
Base name for output files (without extension) |
required |
Source code in tissue_simulator/spatial_analysis.py
visualize_network ¶
visualize_network(figsize: Tuple[int, int] = (12, 10), layout: str = 'spring', save_path: Optional[str] = None)
Visualize the spatial network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
figsize
|
Tuple[int, int]
|
Figure size |
(12, 10)
|
layout
|
str
|
"spring", "kamada_kawai", or "spatial" (use actual positions) |
'spring'
|
save_path
|
Optional[str]
|
If provided, save figure to this path |
None
|
Source code in tissue_simulator/spatial_analysis.py
analyze_tissue_network ¶
analyze_tissue_network(tissue: TissueSection, mode: str = 'contact', radius: Optional[float] = None) -> Dict
Convenience function to analyze a tissue's spatial network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tissue
|
TissueSection
|
TissueSection to analyze |
required |
mode
|
str
|
"contact" or "radius" |
'contact'
|
radius
|
Optional[float]
|
Distance threshold for "radius" mode |
None
|
Returns:
| Type | Description |
|---|---|
Dict
|
Comprehensive analysis dictionary |
Source code in tissue_simulator/spatial_analysis.py
analyze_slice_network ¶
analyze_slice_network(slicer: TissueSlicer, mode: str = 'contact', radius: Optional[float] = None) -> Dict
Convenience function to analyze a slice's spatial network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slicer
|
TissueSlicer
|
TissueSlicer with computed slice |
required |
mode
|
str
|
"contact" or "radius" |
'contact'
|
radius
|
Optional[float]
|
Distance threshold for "radius" mode |
None
|
Returns:
| Type | Description |
|---|---|
Dict
|
Comprehensive analysis dictionary |