packing¶
tissue_simulator.packing ¶
Sphere packing algorithm for cell placement.
SpatialHashGrid ¶
Uniform hash grid over cell centers for short-range neighbor queries.
Space is divided into cubic buckets of side cell_size. A query returns
every index stored within reach buckets of the query point, which is a
superset of all stored points closer than reach * cell_size.
Source code in tissue_simulator/packing.py
key ¶
insert ¶
remove ¶
move ¶
Re-bucket index after its point moved.
Source code in tissue_simulator/packing.py
neighbors ¶
Yield indices stored within reach buckets of point.
Source code in tissue_simulator/packing.py
SpherePacker ¶
SpherePacker(bounds: Tuple[float, float, float], cell_radii_config: Dict[str, Tuple[float, float]], min_spacing: float = 0.5, allow_boundary_cells: bool = True, seed: Optional[int] = None)
Random sphere packing algorithm for placing cells in tissue.
Initialize sphere packer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bounds
|
Tuple[float, float, float]
|
(height, width, thickness) of tissue |
required |
cell_radii_config
|
Dict[str, Tuple[float, float]]
|
Dict mapping cell types to (min, max) radii |
required |
min_spacing
|
float
|
Minimum spacing between cell surfaces |
0.5
|
allow_boundary_cells
|
bool
|
Allow cells extending beyond bounds |
True
|
seed
|
Optional[int]
|
Optional integer seed for the instance RNG. When provided, the packing process is deterministic; when None the RNG is seeded from system entropy (previous default behavior). |
None
|
Source code in tissue_simulator/packing.py
pack ¶
Pack cells into tissue using random placement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_attempts
|
int
|
Maximum placement attempts before stopping |
1000
|
Returns:
| Type | Description |
|---|---|
List[Cell]
|
List of successfully placed Cell objects |
Source code in tissue_simulator/packing.py
pack_with_progress ¶
Pack cells with progress callback for GUI updates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_attempts
|
int
|
Maximum placement attempts before stopping |
1000
|
callback
|
Function called with (cells_placed, total_attempts) |
None
|
Returns:
| Type | Description |
|---|---|
List[Cell]
|
List of successfully placed Cell objects |
Source code in tissue_simulator/packing.py
PackingReport
dataclass
¶
PackingReport(n_target: int, n_placed: int, n_rsa: int, n_inserted: int, n_relaxed: int, relaxation_iterations: int, max_displacement: float, overlap_fraction: float, target_overlap_fraction: float, saturated_bins: int, bin_size: float, bin_targets: ndarray, bin_achieved: ndarray)
Diagnostics of one :class:InhomogeneousPacker run.
Attributes:
| Name | Type | Description |
|---|---|---|
n_target |
int
|
Cells requested by the layout. |
n_placed |
int
|
Cells placed (always |
n_rsa |
int
|
Cells placed by hard-core random sequential addition. |
n_inserted |
int
|
Cells inserted into saturated bins at best clearance. |
n_relaxed |
int
|
Cells moved by overlap relaxation. |
relaxation_iterations |
int
|
Relaxation sweeps performed. |
max_displacement |
float
|
Largest relaxation displacement in µm. |
overlap_fraction |
float
|
Fraction of cells whose nearest neighbor is closer
than the hard core |
target_overlap_fraction |
float
|
The same fraction in the source region. |
saturated_bins |
int
|
Bins where addition stalled. |
bin_size |
float
|
Bin side in µm. |
bin_targets |
ndarray
|
Cell quota per bin, shape |
bin_achieved |
ndarray
|
Cells per bin after packing. |
bin_correlation
property
¶
Pearson correlation of achieved against target cells per bin.
InhomogeneousPacker ¶
InhomogeneousPacker(bounds: Tuple[float, float, float], layout: Layout, allow_boundary_cells: bool = True, seed=None, bin_size: Optional[float] = None, max_failures: int = 100, insertion_candidates: int = 20, max_relax_iterations: int = 100, displacement_cap: Optional[float] = None, placeholder_type: str = 'default')
Pack cells so that local density follows a :class:~tissue_simulator.density.Layout.
- The window is split into square bins; each bin's quota is proportional to the layout intensity it covers (largest-remainder rounding).
- One ticket per quota cell is shuffled and filled by random sequential
addition inside its bin, with radii from the layout's
density-conditioned marks and hard core
kappa * (r_i + r_j). A bin that rejectsmax_failuresconsecutive candidates is saturated. - Tickets left over in saturated bins are inserted at the best-clearance
position of
insertion_candidatesdraws, then overlaps in those bins and a one-bin halo are relaxed by soft-sphere pushes. No cell moves more thandisplacement_capfrom where it was placed, and relaxation stops once the overlap fraction reaches the source region's.
The z coordinate is uniform through the thickness; use a thin slab (thickness below one cell diameter) to mirror a 2D source region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bounds
|
Tuple[float, float, float]
|
(height, width, thickness) of the tissue; height and width must match the layout window. |
required |
layout
|
Layout
|
Target maps from :meth: |
required |
allow_boundary_cells
|
bool
|
Allow cells extending beyond the x/y bounds. |
True
|
seed
|
Integer seed, SeedSequence or Generator for the packer RNG. |
None
|
|
bin_size
|
Optional[float]
|
Quota bin side in µm; defaults to
|
None
|
max_failures
|
int
|
Consecutive rejections before a bin is saturated. |
100
|
insertion_candidates
|
int
|
Positions tried per insertion. |
20
|
max_relax_iterations
|
int
|
Cap on relaxation sweeps. |
100
|
displacement_cap
|
Optional[float]
|
Largest relaxation move in µm; defaults to half the median radius. |
None
|
placeholder_type
|
str
|
Cell type given to placed cells (labels are normally assigned afterwards). |
'default'
|
Source code in tissue_simulator/packing.py
pack ¶
Place layout.n_target cells and store a :class:PackingReport.