Skip to content

Tightened type-checking of color input #393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Dec 19, 2024
Merged

Tightened type-checking of color input #393

merged 11 commits into from
Dec 19, 2024

Conversation

LucaMarconato
Copy link
Member

@LucaMarconato LucaMarconato commented Dec 17, 2024

Small fix. I while ago I started getting an error here:

if isinstance(colors[params_copy.color].dtype, pd.CategoricalDtype):

because colors[params_copy.color].dtype was a dataframe instead of a series, and therefore dtype was not present. I lost the code I used to reproduce this because I had put together a solution quickly while being busy in another PR.

The behavior is due to the fact that colors = sc.get.obs_df(some_adata, some_keys) is a pd.DataFrame if some_keys is an Iterable, while it is a pd.Series when some_keys is a str (this is how sc.get.obs_df() behaves).

@LucaMarconato
Copy link
Member Author

When tests pass I am going to update the changelog.

@@ -950,8 +950,10 @@ def show(

if wanted_labels_on_this_cs:
if (table := params_copy.table_name) is not None:
colors = sc.get.obs_df(sdata[table], params_copy.color)
if isinstance(colors[params_copy.color].dtype, pd.CategoricalDtype):
keys = [params_copy.color] if not isinstance(params_copy.color, list) else params_copy.color
Copy link
Member Author

Choose a reason for hiding this comment

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

I got lost on the type of params_copy.color. Is this always a str? Or is it sometimes an Iterable? Can it be of len() > 1 here? The tests pass but if we know it's always a string we can write better code.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm pretty sure it can never be an Iterable, but would like to check more carefully before really relying on this

Copy link
Contributor

@melonora melonora Dec 18, 2024

Choose a reason for hiding this comment

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

it can never be an interable, it is always either color like or it corresponds to a particular column that then contains the color. This is the case on when it is provided by the user.

@@ -453,9 +453,11 @@ def _render_points(
)

if col_for_color is not None:
cols = sc.get.obs_df(adata, col_for_color)
keys = [col_for_color] if not isinstance(col_for_color, list) else col_for_color
Copy link
Member Author

Choose a reason for hiding this comment

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

Same comment as above for col_for_color, which is defined as render_params.col_for_color

@codecov-commenter
Copy link

codecov-commenter commented Dec 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.54%. Comparing base (e6ed752) to head (355d22a).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #393      +/-   ##
==========================================
+ Coverage   83.52%   83.54%   +0.01%     
==========================================
  Files           8        8              
  Lines        1687     1689       +2     
==========================================
+ Hits         1409     1411       +2     
  Misses        278      278              
Files with missing lines Coverage Δ
src/spatialdata_plot/pl/basic.py 89.26% <100.00%> (+0.05%) ⬆️
src/spatialdata_plot/pl/render.py 89.83% <100.00%> (+0.02%) ⬆️

@LucaMarconato
Copy link
Member Author

LucaMarconato commented Dec 17, 2024

@timtreis @Sonja-Stockhaus @melonora very small PR (most of the changes are from a pre-commit) that fixes a bug I got locally a while ago, can you please have a look?

@LucaMarconato
Copy link
Member Author

LucaMarconato commented Dec 17, 2024

Test pass, so for the sake of a release I'd be inclined to merge soon and eventually come back to this.

Copy link
Collaborator

@Sonja-Stockhaus Sonja-Stockhaus left a comment

Choose a reason for hiding this comment

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

Lgtm, if you want to merge soon @LucaMarconato, we can check the str/Iterable thing in a later PR

Copy link
Contributor

@melonora melonora left a comment

Choose a reason for hiding this comment

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

Can you please provide first a small reproducible example that shows the issue is still there on main because I think I already implemented a fix for this a while back. Main reason was that the scanpy function being used had a different return type. Not too certain about it but I would like to see an example first.

@LucaMarconato
Copy link
Member Author

Thanks for the review, I will try to find the plot that was causing the problem.

@LucaMarconato
Copy link
Member Author

Ok I looked more into this. What caused the problem is due to scanpy==1.10.3, now fixed in 1.10.4. Shown is the diff between scanpy 1.10.3 and 1.10.4. The left version leads to a pd.DataFrame when the argument to get.obs_df() is a list[str], while it leads to a pd.Series when a str is passed. The second always passes a list[str], so the return is consistently a pd.DataFrame.

We could also fix this by requiring 1.10.4 as a min version. I'll think about it and update this PR.
image

@LucaMarconato
Copy link
Member Author

LucaMarconato commented Dec 18, 2024

I tested it against scanpy 1.10.3 and 1.10.4 and the problem is addressed. I added some extra tests because I found another bug, but I'll discuss this in a separate issue+PR.

@LucaMarconato
Copy link
Member Author

Merging since the problem is now addressed.

@LucaMarconato LucaMarconato merged commit 49434a3 into main Dec 19, 2024
5 checks passed
@LucaMarconato LucaMarconato deleted the fix-isinstance branch December 19, 2024 11:05
@timtreis timtreis changed the title fix isinstance Tightened type-checking of color input Jan 20, 2025
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.

4 participants