Skip to content

update var by dist normalization - #1065

Open
LLehner wants to merge 3 commits into
mainfrom
update_var_by_dist_normalization
Open

update var by dist normalization#1065
LLehner wants to merge 3 commits into
mainfrom
update_var_by_dist_normalization

Conversation

@LLehner

@LLehner LLehner commented Nov 20, 2025

Copy link
Copy Markdown
Member

Distances to anchor point are now actually normalized for each slide respectively.

@codecov

codecov Bot commented Nov 20, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.61%. Comparing base (e276aa4) to head (56fd8b1).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1065      +/-   ##
==========================================
- Coverage   78.62%   78.61%   -0.02%     
==========================================
  Files          63       63              
  Lines        9330     9325       -5     
  Branches     1557     1557              
==========================================
- Hits         7336     7331       -5     
  Misses       1422     1422              
  Partials      572      572              
Files with missing lines Coverage Δ
src/squidpy/tl/_var_by_distance.py 65.89% <100.00%> (-1.28%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/squidpy/tl/_var_by_distance.py Outdated
df["obs"] = adata.obs_names

# store dataframes by (slide, anchor) combination and also the corresponding maximum distance for normalization
# store dataframes by (slide, anchor) combinationq

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

@timtreis

timtreis commented Nov 22, 2025

Copy link
Copy Markdown
Member

Can you describe the issue you're solving here better and then also translate this into a test?

@LLehner

LLehner commented Nov 23, 2025

Copy link
Copy Markdown
Member Author

Can you describe the issue you're solving here better and then also translate this into a test?

Previous normalization was useless as it just changed the the distance values to be within [0,1] without actually changing the scale. Now the distances from categories with a lower maximum are stretched to be comparable. This doesn't affect the raw distances which are still provided when running this function. I will add a test.

@timtreis

timtreis commented Dec 1, 2025

Copy link
Copy Markdown
Member

Don't fully understand the PR, the code changes seem equivalent? Good to get rid of the dependency for a fairly simple calculation, but still:

import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler

distances = np.array([0.0, 4.2, 10.0, np.nan, 3.3])
design_matrix = pd.DataFrame({
    "old": distances,
    "new": distances,

})

# old
anchor_point = "old"
scaler = MinMaxScaler()
scaler.fit(design_matrix[[anchor_point]].values)
design_matrix[anchor_point] = scaler.transform(design_matrix[[anchor_point]].values)

# new
anchor_point = "new"
design_matrix[anchor_point] = design_matrix[anchor_point] / design_matrix[anchor_point].max()

design_matrix
image

Replace the per-slide sklearn MinMaxScaler with a plain divide-by-max. The
preceding loop already forces each slide's minimum distance to 0, so
(x - min) / (max - min) reduces to x / max: the output is identical, minus
the sklearn.preprocessing dependency and the unused max_distances bookkeeping.

Also fix the stale comment and lock the per-slide normalization contract
(every slide stretched to a max distance of 1) with a regression test that
would fail under global cross-slide normalization.

Forward-ports #1065 onto current main.

Co-authored-by: LLehner <LLehner@users.noreply.github.com>
@timtreis
timtreis force-pushed the update_var_by_dist_normalization branch from f7b8b2e to 6245dc2 Compare August 12, 2026 23:06
pre-commit-ci Bot and others added 2 commits August 12, 2026 23:07
The v0.8.0 template sync (#1257) deleted docs/extensions/typed_returns.py,
which renders numpy-style Returns type annotations as clean cross-references.
Without it, public functions annotated '-> NDArray' emit an unresolvable
numpy._typing._array_like.NDArray reference, and the docs build (run with -W)
fails. This is why main's Read the Docs build is currently red.

Restore the extension and re-register it in conf.py. Verified green with a
full 'sphinx-build -M html docs docs/_build -W'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants