Skip to content

feat: add Harmony to scanpy#3953

Open
Intron7 wants to merge 18 commits intomainfrom
add-harmony
Open

feat: add Harmony to scanpy#3953
Intron7 wants to merge 18 commits intomainfrom
add-harmony

Conversation

@Intron7
Copy link
Copy Markdown
Member

@Intron7 Intron7 commented Jan 26, 2026

After recent changes from Harmonypy its necessary to move HarmonyBatchcorrection to scanpy. This function is based on harmony-pytorch and rapids-singlecell

@Intron7 Intron7 requested a review from flying-sheep January 26, 2026 09:50
@Intron7 Intron7 added this to the 1.12.1 milestone Jan 26, 2026
@Intron7 Intron7 marked this pull request as draft January 26, 2026 09:51
@codecov
Copy link
Copy Markdown

codecov bot commented Jan 26, 2026

❌ 2 Tests Failed:

Tests completed Failed Passed Skipped
1868 2 1866 906
View the top 2 failed test(s) by shortest run time
tests/test_harmony.py::test_harmony_integrate_algos[float32]
Stack Traces | 0.587s run time
contains 2 failed subtests
tests/test_harmony.py::test_harmony_integrate_algos[float64]
Stack Traces | 0.985s run time
contains 2 failed subtests

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

Copy link
Copy Markdown
Member

@flying-sheep flying-sheep left a comment

Choose a reason for hiding this comment

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

Looks good! I fixed the docs and made the code a bit nicer, but the reference tests have been failing from the start. Any idea why? You said they work locally for you?

@Intron7 Intron7 marked this pull request as ready for review March 2, 2026 09:49
@Intron7
Copy link
Copy Markdown
Member Author

Intron7 commented Mar 2, 2026

so 0.05 for L2 was way to low harmony-pytorch is at 0.0511. we were at 0.0501. I also ran harmoypy once and it was at 0.063. so I set L2 to 0.1

@Intron7 Intron7 requested a review from flying-sheep March 2, 2026 10:07
Copy link
Copy Markdown
Member

@flying-sheep flying-sheep left a comment

Choose a reason for hiding this comment

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

looks good! Some API considerations.

Please

  • add parameter descriptions and
  • rename the parameters (probably a good idea, this is how we do things in scanpy, but we might have to add a wrapper in .external for this)
  • add a towncrier fragment

Comment on lines +19 to +20
basis: str = "X_pca",
adjusted_basis: str = "X_pca_harmony",
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.

these will become refs in 2.0, but we might release harmony before that.

Comment on lines +22 to +32
theta: float | Sequence[float] | None = None,
sigma: float = 0.1,
n_clusters: int | None = None,
max_iter_harmony: int = 10,
max_iter_clustering: int = 200,
tol_harmony: float = 1e-4,
tol_clustering: float = 1e-5,
ridge_lambda: float = 1.0,
correction_method: Literal["fast", "original"] = "original",
block_proportion: float = 0.05,
tau: int = 0,
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.

theta, lambda, sigma, tau … we should probably

  1. rename these to make sense
  2. fix the docs:
  • tau “Discounting factor” – this one has a great description about what it does, let’s do that for the rest! e.g.
  • “Diversity penalty weight … default 2 for each batch variable” – “diversity” might be a good component of the param name, but what is it and what does “2” mean compared to smaller or bigger values?
  • “ridge regression regularization … default 1” – what does increasing and decreasing do, respectively?

Comment thread src/scanpy/preprocessing/_harmony_integrate.py Outdated
Comment thread tests/test_harmony.py
Comment thread src/scanpy/preprocessing/_harmony_integrate.py Outdated
@flying-sheep flying-sheep modified the milestones: 1.12.1, 1.12.2 Apr 10, 2026
@flying-sheep flying-sheep added this to the 1.13.0 milestone Apr 10, 2026
@flying-sheep flying-sheep changed the title Add Harmony to scanpy feat: add Harmony to scanpy Apr 20, 2026
@flying-sheep
Copy link
Copy Markdown
Member

OK, I got rid of random_state. Let’s see if the tests are robust against different k-means clustering.

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.

scanpy.external.pp.harmony_integrate incompatible with newly updated harmonypy>=0.1.0

3 participants