Core Functions
src.same.run_same(ref_df, aligned_df, commonCT, outprefix=None, aligned_delaunay=None, aligned_delaunay_vertex_col=None, optim_params=None, gurobi_params=None, ignore_precomputed_triangulation=False)
Find optimal spatial matches between aligned and reference cells using MIP.
This is the core SAME optimization function. It formulates cell matching as a Mixed Integer Program (MIP) that minimizes cell type distance and coordinate distance while preserving spatial relationships through Delaunay triangle orientation constraints.
The function supports both "eager" mode (all constraints upfront) and "lazy" mode (constraints added on-demand via callbacks), with lazy mode being more memory-efficient for large problems.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref_df
|
DataFrame
|
Reference dataset with required columns:
|
required |
aligned_df
|
DataFrame or MetaCell
|
Aligned/moving dataset to match against reference. Same column
requirements as |
required |
commonCT
|
list of str
|
Names of columns containing cell type probabilities or one-hot encodings. These are used to compute cell type distance. |
required |
outprefix
|
str
|
Output directory. If provided, saves:
|
None
|
aligned_delaunay
|
(array - like, shape(n_triangles, 3))
|
Precomputed Delaunay triangulation. If None, computed automatically. Useful when using metacells with pre-filtered triangulation. |
None
|
aligned_delaunay_vertex_col
|
str
|
Column name containing vertex IDs that correspond to indices in
|
None
|
optim_params
|
dict
|
Optimization parameters from
|
None
|
gurobi_params
|
dict
|
Gurobi solver parameters from
|
None
|
ignore_precomputed_triangulation
|
bool
|
If True, compute fresh Delaunay even if precomputed one provided. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
matches_df |
DataFrame
|
Matched pairs with columns:
|
var_out |
dict
|
Optimization diagnostics including:
|
Notes
Objective Function:
The solver minimizes:
.. math::
\sum_{(i,j)} c_{ij} x_{ij} + \alpha \sum_j p_j + \beta \sum_i s_i n_i + \gamma \sum_t w_t q_t
where:
- :math:c_{ij} = cell type distance + coordinate distance
- :math:p_j = penalty for ref j matched more than once
- :math:n_i = penalty for aligned i not matched, weighted by size :math:s_i
- :math:q_t = penalty for triangle t orientation flip, weighted by :math:w_t
Lazy Constraints:
When lazy_constraints=True, triangle orientation constraints are added
on-demand during optimization. This reduces memory from O(n×k³) to O(n)
and is recommended for large problems (>1000 cells).
Space-Tearing:
Triangle orientation flips indicate space-tearing events where the spatial
relationship between cells changes (e.g., due to tissue deformation or
missing cells). The delaunay_penalty controls how strongly these are
penalized.
Examples:
Basic usage:
>>> from src import run_same
>>> matches, var_out = run_same(
... ref_df=ref_df,
... aligned_df=aligned_df,
... commonCT=['TypeA', 'TypeB', 'TypeC'],
... outprefix='results/'
... )
>>> print(f"Found {len(matches)} matches")
With custom parameters:
>>> from src import run_same, init_optim_params, init_gurobi_params
>>> optim = init_optim_params(radius=500, lazy_constraints=True)
>>> gurobi = init_gurobi_params(time_limit=3600, mip_gap=0.01)
>>> matches, var_out = run_same(
... ref_df=ref_df,
... aligned_df=aligned_df,
... commonCT=commonCT,
... optim_params=optim,
... gurobi_params=gurobi
... )
See Also
sliding_window_matching : Process large datasets in windows. init_optim_params : Create optimization parameters. init_gurobi_params : Create Gurobi parameters. greedy_triangle_collapse : Create metacells for faster optimization.
src.same.sliding_window_matching(ref, moving, commonCT=None, outprefix=None, moving_delaunay=None, moving_delaunay_vertex_col=None, optim_params=None, gurobi_params=None, ignore_precomputed_triangulation=False)
Match cells between reference and moving datasets using a sliding window approach.
This function divides the spatial domain into overlapping windows and runs the SAME optimization on each window. Results from overlapping regions are resolved using bipartite matching to ensure unique assignments.
Supports resumption from partial results when outprefix is provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref
|
DataFrame or MetaCell
|
Reference dataset with columns ['X', 'Y', 'cell_type'] plus cell type
probability columns. Can also be a MetaCell object with |
required |
moving
|
DataFrame or MetaCell
|
Moving/aligned dataset to be matched to reference. Same column
requirements as |
required |
commonCT
|
list of str
|
List of cell type column names (probability/one-hot columns). If None, inferred from unique values of 'cell_type' column. |
None
|
outprefix
|
str
|
Output directory path. If provided, intermediate results are saved and the function can resume from partial runs. |
None
|
moving_delaunay
|
array - like
|
Precomputed Delaunay triangulation for moving data as (n, 3) array of vertex indices. If moving is a MetaCell, this is extracted automatically. |
None
|
moving_delaunay_vertex_col
|
str
|
Column name in moving DataFrame containing vertex IDs that correspond
to indices in |
None
|
optim_params
|
dict
|
Optimization parameters. See
|
None
|
gurobi_params
|
dict
|
Gurobi solver parameters. See |
None
|
ignore_precomputed_triangulation
|
bool
|
If True, compute fresh Delaunay triangulation even if precomputed one is available. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Matched pairs with columns:
|
Examples:
Basic usage:
>>> from src import sliding_window_matching, init_optim_params
>>> matches = sliding_window_matching(
... ref=ref_df,
... moving=aligned_df,
... commonCT=['TypeA', 'TypeB', 'TypeC'],
... outprefix='results/'
... )
With custom parameters:
>>> optim = init_optim_params(window_size=500, overlap=100, radius=300)
>>> matches = sliding_window_matching(
... ref=ref_df,
... moving=aligned_df,
... optim_params=optim
... )
With metacells:
>>> from src import MetaCell
>>> mc_aligned = MetaCell(aligned_df, max_metacell_size=10)
>>> matches = sliding_window_matching(ref=ref_df, moving=mc_aligned)
See Also
run_same : Core optimization for a single region. init_optim_params : Create optimization parameter dictionary. init_gurobi_params : Create Gurobi parameter dictionary. merge_window_matches_unique_ref : Merge overlapping window results.
src.same.init_optim_params(**overrides)
Create default optimization parameters for SAME matching.
Returns a dictionary of parameters controlling the matching problem formulation, spatial constraints, and sliding window behavior. Use keyword arguments to override specific defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**overrides
|
dict
|
Keyword arguments to override default values. |
{}
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary of optimization parameters with the following keys: Sliding window parameters:
Matching problem parameters:
Objective function coefficients:
Output labeling:
Constraint/behavior flags:
Triangle quality filtering:
|
Examples:
See Also
init_gurobi_params : Create Gurobi solver parameters. run_same : Main optimization function using these parameters. sliding_window_matching : Sliding window approach for large datasets.
src.same.init_gurobi_params(**overrides)
Create default Gurobi solver parameters for SAME optimization.
Returns a dictionary of Gurobi-related parameters that control solver behavior, time limits, and lazy constraint generation. Use keyword arguments to override specific defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**overrides
|
dict
|
Keyword arguments to override default values. |
{}
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary of Gurobi parameters with the following keys: Core solve controls:
Gurobi tuning knobs:
MIP start / initialization:
Lazy constraints:
|
Examples:
See Also
init_optim_params : Create optimization parameters. run_same : Main optimization function using these parameters.