physicell_reader¶
tissue_simulator.physicell_reader ¶
PhysiCell snapshot reader and spatial statistics computation.
This module parses PhysiCell agent-based model output (CSV snapshots and
.mat snapshots) and recomputes the same node/edge/neighbor schema
used internally by GraphColorizer (node_counts, edge_counts,
neighbor_dist). It is intended for comparing ABM-evolved tissue states
against initial-condition targets generated by this package.
The :func:stats_to_target_statistics adapter converts the output of
:meth:PhysiCellReader.compute_spatial_stats into the
:class:~tissue_simulator.replicate_generator.TargetStatistics dataclass
expected by :class:~tissue_simulator.replicate_generator.ReplicateGenerator.
CREDITS¶
The PhysiCell .mat snapshot layout and the cell-definition XML schema
used by :meth:PhysiCellReader.load_snapshot_mat are documented and
reference-implemented by the PhysiCell-Tools python-loader project
(pyMCDS / pcdl), distributed under the BSD-3-Clause license:
https://github.com/PhysiCell-Tools/python-loader
When the optional pyMCDS / pcdl package is installed, the reader
delegates to it for full-fidelity parsing. Otherwise it falls back to a
direct scipy.io.loadmat parser based on the documented PhysiCell 1.10+
default cells-matrix column layout (see load_snapshot_mat). Neither
pyMCDS nor pcdl is a required dependency of tissue_simulator;
both are optional and only used when present.
PhysiCellReader ¶
Reader for PhysiCell snapshot output.
Parses CSV and (optionally) .mat snapshots emitted by PhysiCell and
recomputes the spatial statistics consumed by tissue_simulator
(node_counts, edge_counts, neighbor_dist).
Initialize the reader.
Parameters¶
default_radius : float
Radius to assign when neither radius nor volume is
present in the snapshot. Defaults to a PhysiCell-like value
(~8.41 micrometers, the radius of a 2494 cubic micrometer cell).
Source code in tissue_simulator/physicell_reader.py
load_snapshot_csv ¶
Load a PhysiCell CSV snapshot.
Parameters¶
csv_path : str
Path to a CSV file with at least the columns x, y, z,
and cell_type. Optional columns: radius or volume,
timestep, ID.
Returns¶
list of dict
One dict per cell with keys x, y, z, radius,
cell_type, and optionally timestep, id, volume.
Source code in tissue_simulator/physicell_reader.py
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 | |
load_snapshot_mat ¶
Load a PhysiCell .mat snapshot.
Two backends are attempted, in order:
- pyMCDS / pcdl (optional dependency). If either of these
packages is importable, the reader delegates to it for
full-fidelity parsing.
pyMCDS(xml_basename, output_path)is preferred because the matchingoutput*.xmlis required to recover the cell-type name mapping. When onlymat_pathis provided, this step is skipped (pyMCDS requires the XML). - Direct
scipy.io.loadmatfallback following the documented PhysiCell 1.10+ default cells-matrix layout:
=== =================== col meaning === =================== 0 ID 1 position x 2 position y 3 position z 4 total volume 5 cell type (integer) === ===================
The matrix is conventionally stored as
(n_signals, n_cells), so axis 0 is the signals axis by
default. We only treat axis 1 as the signals axis when axis 0
is shorter than the six documented signals and axis 1 is long
enough to hold them (i.e. the file was saved transposed). If
neither axis can hold the standard six signals a
ValueError is raised.
Parameters¶
mat_path : str
Path to a PhysiCell output*_cells.mat (or similar) file.
xml_path : str, optional
Path to the matching output*.xml. When provided, the XML
is parsed for <cell_definitions> to map integer cell-type
IDs to human-readable names. Without it, cell types are
stringified integer IDs (e.g. "0", "1") and a warning
is emitted via :mod:warnings.
Returns¶
list of dict
Cell records in the same schema as :meth:load_snapshot_csv:
keys x, y, z, radius, cell_type, and
optionally volume, id.
Raises¶
FileNotFoundError
If mat_path does not exist.
ValueError
If the file cannot be parsed as a MATLAB file, no cells
matrix can be located, or its shape is incompatible with the
documented PhysiCell layout.
Notes¶
The pyMCDS / pcdl package (BSD-3-Clause,
https://github.com/PhysiCell-Tools/python-loader) is an optional
dependency that provides reference-implementation parsing and is
preferred when available. The direct fallback implements only
the documented PhysiCell 1.10+ default layout and may not
correctly decode custom-built PhysiCell binaries that emit a
non-default signal ordering.
Source code in tissue_simulator/physicell_reader.py
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 | |
load_time_series ¶
load_time_series(output_dir: str, pattern: str = 'snapshot_*.csv') -> List[Tuple[float, List[Dict]]]
Load a directory of PhysiCell snapshot CSVs as a time series.
Parameters¶
output_dir : str
Directory containing snapshot CSV files.
pattern : str
Glob pattern relative to output_dir matching snapshot files.
Returns¶
list of (float, list of dict)
Tuples of (timestep, cells) sorted by timestep ascending.
The timestep is extracted from the numeric token in each
filename (e.g. snapshot_00010.csv -> 10.0).
Source code in tissue_simulator/physicell_reader.py
compute_spatial_stats ¶
compute_spatial_stats(cells: List[Dict], mode: str = 'contact', radius_threshold: Optional[float] = None) -> Dict
Compute spatial statistics matching the tissue_simulator schema.
Parameters¶
cells : list of dict
Cell records as returned by :meth:load_snapshot_csv. Each
must contain x, y, z, radius, and cell_type.
mode : str
"contact" to connect cells whose surfaces touch
(distance <= r_i + r_j with a 1% tolerance matching
SpatialNetworkAnalyzer) or "radius" to connect cells
within radius_threshold of each other (center to center).
radius_threshold : float, optional
Distance threshold for radius mode. Required when
mode == "radius".
Returns¶
dict
Dictionary with keys node_counts, edge_counts, and
neighbor_dist.
- ``node_counts`` : ``Dict[str, int]`` mapping cell type to
cell count.
- ``edge_counts`` : ``Dict[str, int]`` mapping a sorted
``"type_a-type_b"`` key to the number of contacts.
- ``neighbor_dist`` : ``Dict[str, Dict[str, float]]`` mapping
``type_a`` to a dict of ``type_b`` to the average number of
``type_b`` neighbors for a ``type_a`` cell.
Source code in tissue_simulator/physicell_reader.py
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 | |
compute_stats_time_series ¶
compute_stats_time_series(time_series: List[Tuple[float, List[Dict]]], mode: str = 'contact', radius_threshold: Optional[float] = None) -> List[Tuple[float, Dict]]
Apply :meth:compute_spatial_stats over a time series.
Parameters¶
time_series : list of (float, list of dict)
Output of :meth:load_time_series.
mode : str
See :meth:compute_spatial_stats.
radius_threshold : float, optional
See :meth:compute_spatial_stats.
Returns¶
list of (float, dict)
Tuples of (timestep, stats) where stats is the dict
returned by :meth:compute_spatial_stats.
Source code in tissue_simulator/physicell_reader.py
read_physicell_output ¶
Read PhysiCell output from a file or directory.
Convenience entry point that dispatches based on path:
- If
pathis a directory, returns a time series via :meth:PhysiCellReader.load_time_series. - If
pathends in.csv, returns a single snapshot list via :meth:PhysiCellReader.load_snapshot_csv. - If
pathends in.mat, dispatches to :meth:PhysiCellReader.load_snapshot_mat.
Parameters¶
path : str
Path to a snapshot file or an output directory.
**kwargs
Forwarded to the dispatched loader (e.g. pattern for
directories, xml_path for .mat files,
default_radius for the reader itself).
Returns¶
list
Either a list of cell dicts (single snapshot) or a list of
(timestep, cells) tuples (directory).
Source code in tissue_simulator/physicell_reader.py
stats_to_target_statistics ¶
stats_to_target_statistics(stats: Dict, target_cell_count: Optional[int] = None, target_density: Optional[float] = None)
Convert a reader stats dict to a :class:TargetStatistics dataclass.
The dict returned by :meth:PhysiCellReader.compute_spatial_stats uses
the schema accepted internally by :class:GraphColorizer. The
:class:~tissue_simulator.replicate_generator.ReplicateGenerator expects
a different shape: a list of
:class:~tissue_simulator.spatial_analysis.InteractionStatistics plus
cell-type proportions. This adapter performs that conversion.
Average and median distances are not present in the reader's output and
are set to 0.0 on the synthesized interaction records; callers that
need real distance statistics should compute them separately.
Parameters¶
stats : dict
Output of :meth:PhysiCellReader.compute_spatial_stats. Must
contain node_counts, edge_counts and neighbor_dist.
target_cell_count : int, optional
Optional total cell count to attach to the resulting
TargetStatistics instance. When omitted, the sum of
node_counts is used.
target_density : float, optional
Optional target packing fraction to attach.
Returns¶
TargetStatistics
The converted dataclass, ready to pass to
:class:~tissue_simulator.replicate_generator.ReplicateGenerator.
Source code in tissue_simulator/physicell_reader.py
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 | |