Spaces:
Sleeping
Sleeping
Update cellemetry/tools/statistics.py
Browse files- cellemetry/tools/statistics.py +22 -42
cellemetry/tools/statistics.py
CHANGED
|
@@ -1,61 +1,41 @@
|
|
| 1 |
"""
|
| 2 |
Statistical analysis tools for morphology and spatial metrics.
|
| 3 |
"""
|
|
|
|
| 4 |
from google.adk.tools.tool_context import ToolContext
|
| 5 |
-
|
| 6 |
from ..services import analysis
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
tool_context: ToolContext
|
| 12 |
) -> dict:
|
| 13 |
"""
|
| 14 |
-
Calculate
|
| 15 |
|
| 16 |
Args:
|
| 17 |
-
|
|
|
|
| 18 |
tool_context: Automatically injected by ADK
|
| 19 |
|
| 20 |
Returns:
|
| 21 |
-
dict
|
|
|
|
| 22 |
"""
|
| 23 |
pixel_scale = tool_context.state.get("app:pixel_size_microns")
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
"""
|
| 32 |
-
Calculate spatial distribution stats (NND, density, neighbor count).
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
Returns:
|
| 39 |
-
dict with spatial metrics
|
| 40 |
-
"""
|
| 41 |
-
pixel_scale = tool_context.state.get("app:pixel_size_microns")
|
| 42 |
-
return analysis.get_spatial_stats(filename, pixel_scale=pixel_scale)
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
cell_file: str,
|
| 47 |
-
nuc_file: str,
|
| 48 |
-
tool_context: ToolContext
|
| 49 |
-
) -> dict:
|
| 50 |
-
"""
|
| 51 |
-
Analyze cell-nucleus relationships (overlap ratios).
|
| 52 |
-
|
| 53 |
-
Args:
|
| 54 |
-
cell_file: Path to cell masks .npz
|
| 55 |
-
nuc_file: Path to nucleus masks .npz
|
| 56 |
-
tool_context: Automatically injected by ADK
|
| 57 |
-
|
| 58 |
-
Returns:
|
| 59 |
-
dict with matched_pairs, avg_ratio, std_ratio
|
| 60 |
-
"""
|
| 61 |
-
return analysis.analyze_relationships(cell_file, nuc_file)
|
|
|
|
| 1 |
"""
|
| 2 |
Statistical analysis tools for morphology and spatial metrics.
|
| 3 |
"""
|
| 4 |
+
from typing import Optional
|
| 5 |
from google.adk.tools.tool_context import ToolContext
|
|
|
|
| 6 |
from ..services import analysis
|
| 7 |
|
| 8 |
+
def compute_comprehensive_stats(
|
| 9 |
+
cell_file: Optional[str] = None,
|
| 10 |
+
nuc_file: Optional[str] = None,
|
| 11 |
+
tool_context: ToolContext = None
|
| 12 |
) -> dict:
|
| 13 |
"""
|
| 14 |
+
Calculate morphology, spatial, and relational statistics in a single pass.
|
| 15 |
|
| 16 |
Args:
|
| 17 |
+
cell_file: Path to the .npz mask file for Cells (optional)
|
| 18 |
+
nuc_file: Path to the .npz mask file for Nuclei (optional)
|
| 19 |
tool_context: Automatically injected by ADK
|
| 20 |
|
| 21 |
Returns:
|
| 22 |
+
Nested dict containing 'cell_stats', 'nuc_stats', 'spatial_stats',
|
| 23 |
+
and 'relational_stats' where applicable.
|
| 24 |
"""
|
| 25 |
pixel_scale = tool_context.state.get("app:pixel_size_microns")
|
| 26 |
+
results = {}
|
|
|
|
| 27 |
|
| 28 |
+
# 1. Analyze Cells (Morphology + Spatial)
|
| 29 |
+
if cell_file:
|
| 30 |
+
results["cell_stats"] = analysis.get_basic_stats(cell_file, pixel_scale=pixel_scale)
|
| 31 |
+
results["spatial_stats"] = analysis.get_spatial_stats(cell_file, pixel_scale=pixel_scale)
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# 2. Analyze Nuclei (Morphology only)
|
| 34 |
+
if nuc_file:
|
| 35 |
+
results["nuc_stats"] = analysis.get_basic_stats(nuc_file, pixel_scale=pixel_scale)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
# 3. Analyze Relationships (Overlap)
|
| 38 |
+
if cell_file and nuc_file:
|
| 39 |
+
results["relational_stats"] = analysis.analyze_relationships(cell_file, nuc_file)
|
| 40 |
|
| 41 |
+
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|