Skip to content

bugfix in _calculate_neighborhood_profile in gr/_niche.py#1229

Open
shashkat wants to merge 1 commit into
scverse:mainfrom
shashkat:bugfix_nhood_profile
Open

bugfix in _calculate_neighborhood_profile in gr/_niche.py#1229
shashkat wants to merge 1 commit into
scverse:mainfrom
shashkat:bugfix_nhood_profile

Conversation

@shashkat

Copy link
Copy Markdown

Description

I am currently working on refactoring the niche code and API to make more modular similar to how it was done for graph construction strategies in #1147 and discussed in #1192. However, before that, in a separate, smaller PR, I wanted to make aware of a bug in the niche code, and fix it. This is that PR.

The bug was in the function _calculate_neighborhood_profile() in gr/_niche.py. This is called when calculate_niche() is called with flavor = 'neighborhood', inside _get_nhood_profile_niches(). The goal of _calculate_neighborhood_profile() is to get a dataframe of shape num_cells x num_categories (categories could be something like celltype), where each entry stores the number of neighbors of a cell of a particular category. However, it wasn't working correctly when different cells had different number of neighbors. I believe it was written to be meant for grid-based data only.
Now, it handles correctly the case when cells have different number of neighbors.

How has this been tested?

I created a new dummy adata object (dummy_adata2 in tests/conftest.py) with explicitly defined locations of the cells, not necessarily organized in a grid. Then, I determined how the neighborhood profile for this object would look like and wrote a test function accordingly in tests/graph/test_niche.py called test_calculate_neighborhood_profile(). It failed with the original version of _calculate_neighborhood_profile() due to incorrect values and passed with the new version.

Other points

  • The purpose of this PR is to just make aware of this bug and fix it first. But it is not gonna be the final version of this function as there would be many changes with the niche-refactor PR.
  • I also believe that dummy_adata2 object can be useful for writing other tests cases too in the future for niche code and possibly graph building code too.

@codecov

codecov Bot commented Jun 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.30435% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.84%. Comparing base (ae5ceb4) to head (f2d1e31).

Files with missing lines Patch % Lines
src/squidpy/gr/_niche.py 91.30% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1229      +/-   ##
==========================================
+ Coverage   76.81%   76.84%   +0.03%     
==========================================
  Files          63       63              
  Lines        9267     9273       +6     
  Branches     1565     1569       +4     
==========================================
+ Hits         7118     7126       +8     
+ Misses       1547     1543       -4     
- Partials      602      604       +2     
Files with missing lines Coverage Δ
src/squidpy/gr/_niche.py 38.01% <91.30%> (+0.48%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@selmanozleyen selmanozleyen self-assigned this Jun 25, 2026
@selmanozleyen selmanozleyen self-requested a review June 26, 2026 22:38
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