Skip to content

Preserve category order when groupby is a list - #1735

Merged
gokceneraslan merged 6 commits into
masterfrom
gokceneraslan-fixcatorder-preparedf
Mar 11, 2021
Merged

gokceneraslan merged 6 commits into
masterfrom
gokceneraslan-fixcatorder-preparedf

Conversation

@gokceneraslan

Copy link
Copy Markdown
Contributor

Let's preserve categories if groupby parameter in _prepare_dataframe is a list:

import scanpy as sc
import numpy as np

ad = sc.datasets.paul15()

sc.pp.log1p(ad)
sc.pp.pca(ad)
sc.pp.neighbors(ad)
sc.tl.leiden(ad)

cats = ad.obs.leiden.cat.categories.tolist()
np.random.seed(1)
np.random.shuffle(cats)
ad.obs.leiden.cat.reorder_categories(cats, inplace=True)
ad.obs.leiden.cat.categories

Prints:
Index(['2', '3', '4', '9', '1', '6', '0', '7', '10', '8', '5'], dtype='object')

sc.pl.dotplot(ad, ['Cst3', 'Malat1'], groupby=['leiden'])
sc.pl.dotplot(ad, ['Cst3', 'Malat1'], groupby=['leiden', 'paul15_clusters'])

Outputs:

image

image

which is very annoying because we lost the category order of leiden. After this PR the output is:

image
image

gokceneraslan and others added 2 commits March 10, 2021 17:16
Let's preserve categories if groupby parameter in _prepare_dataframe is a list
@gokceneraslan
gokceneraslan enabled auto-merge (squash) March 10, 2021 22:32
@codecov

codecov Bot commented Mar 10, 2021 •

Copy link
Copy Markdown

Codecov Report

Merging #1735 (82f53b3) into master (da66e15) will decrease coverage by 1.24%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #1735      +/-   ##
==========================================
- Coverage   71.25%   70.00%   -1.25%     
==========================================
  Files          91       91              
  Lines       11090    11092       +2     
==========================================
- Hits         7902     7765     -137     
- Misses       3188     3327     +139     
Impacted Files Coverage Δ
scanpy/plotting/_anndata.py 77.36% <100.00%> (-6.81%) ⬇️
scanpy/plotting/_tools/paga.py 59.92% <0.00%> (-10.71%) ⬇️
scanpy/plotting/_preprocessing.py 85.71% <0.00%> (-2.05%) ⬇️
scanpy/plotting/_utils.py 52.11% <0.00%> (-1.82%) ⬇️
scanpy/tools/_rank_genes_groups.py 92.55% <0.00%> (-1.49%) ⬇️
scanpy/plotting/_tools/__init__.py 75.14% <0.00%> (-1.13%) ⬇️

@gokceneraslan
gokceneraslan disabled auto-merge March 10, 2021 22:54
@gokceneraslan
gokceneraslan requested a review from fidelram March 10, 2021 23:59
@gokceneraslan

gokceneraslan commented Mar 11, 2021 •

Copy link
Copy Markdown
Contributor Author

@fidelram Since we are not using tight layout when we save figures for the plotting tests, axis labels are cut off. I enabled it to avoid that with the following modification:

image

Here is the new plot testing this PR without the tight layout:

master_dotplot_groupby_list_catorder

which is pretty bad because what really matters in this plot for this PR is the labels of the x axis. Here is the same plot with the tight layout:

master_dotplot_groupby_list_catorder-tightlayout

However, many plotting tests fail now due to this change :/ Do you mind helping me with the failing tests?

@ivirshup ivirshup left a comment

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.

Question

Does this interact with group colors and dendrograms at all?

Test change

All of the plots will start failing because this will change the output for every test. I have a few concerns here:

  • I'm worried about repo bloat from the plotting tests. Ideally we could just store the reference images outside of git (git lfs maybe?). Updating all the plots with tight_layout would increase repo size by 10%
  • Is tight_layout deterministic (matplotlib/matplotlib#11809 (comment))? Also, is matplotlib trying to replace it with constrained_layout?
  • Does globally adding tight_layout add to test times? My impression was that it basically rendered the plot, fixed the borders, then rendered it again.

Proposed solution:

Can we just explicitly extend the borders for this test? At a later point we can move plots to a different storage system, then have much more freedom in making changes to how they render.

Comment thread scanpy/plotting/_anndata.py Resolved
@ivirshup ivirshup added this to the 1.7.2 milestone Mar 11, 2021
@ivirshup ivirshup mentioned this pull request Mar 11, 2021
@gokceneraslan

Copy link
Copy Markdown
Contributor Author

Question

Does this interact with group colors and dendrograms at all?

Dendrogram and colors seem not affected.

Test change

All of the plots will start failing because this will change the output for every test. I have a few concerns here:

* I'm worried about repo bloat from the plotting tests. Ideally we could just store the reference images outside of git (git lfs maybe?). Updating all the plots with `tight_layout` would increase repo size by 10%

* Is `tight_layout` deterministic ([matplotlib/matplotlib#11809 (comment)](https://github.com/matplotlib/matplotlib/issues/11809#issuecomment-432726600))? Also, is matplotlib trying to replace it with [`constrained_layout`](https://matplotlib.org/stable/tutorials/intermediate/constrainedlayout_guide.html)?

* Does globally adding `tight_layout` add to test times? My impression was that it basically rendered the plot, fixed the borders, then rendered it again.

Proposed solution:

Can we just explicitly extend the borders for this test? At a later point we can move plots to a different storage system, then have much more freedom in making changes to how they render.

Sounds great, I think I managed to do that.

@gokceneraslan
gokceneraslan merged commit af8f323 into master Mar 11, 2021
@gokceneraslan

Copy link
Copy Markdown
Contributor Author

@meeseeksdev backport 1.7.x

meeseeksmachine pushed a commit to meeseeksmachine/scanpy that referenced this pull request Mar 11, 2021
@gokceneraslan
gokceneraslan deleted the gokceneraslan-fixcatorder-preparedf branch March 11, 2021 15:36
gokceneraslan added a commit that referenced this pull request Mar 11, 2021
Zethson pushed a commit that referenced this pull request Mar 15, 2021
Preserve category order when groupby is a list
ivirshup added a commit that referenced this pull request Mar 18, 2021
* add flake8 pre-commit

Signed-off-by: Zethson <[email protected]>

* fix pre-commit

Signed-off-by: Zethson <[email protected]>

* add E402 to flake8 ignore

Signed-off-by: Zethson <[email protected]>

* revert neighbors

Signed-off-by: Zethson <[email protected]>

* fix flake8

Signed-off-by: Zethson <[email protected]>

* address review

Signed-off-by: Zethson <[email protected]>

* fix comment character in .flake8

Signed-off-by: Zethson <[email protected]>

* fix test

Signed-off-by: Zethson <[email protected]>

* black

Signed-off-by: Zethson <[email protected]>

* review round 2

Signed-off-by: Zethson <[email protected]>

* review round 3

Signed-off-by: Zethson <[email protected]>

* readded double comments

Signed-off-by: Zethson <[email protected]>

* Ignoring E262 & reverted comment

Signed-off-by: Zethson <[email protected]>

* using self for obs_tidy

Signed-off-by: Zethson <[email protected]>

* Restore setup.py

* rm call of black test (#1690)

* Fix print_versions for python<3.8 (#1691)

* add codecov so we can have a badge to point to (#1693)

* Attempt server-side search (#1672)

* Fix paga_path (#1047)

Fix paga_path

Co-authored-by: Isaac Virshup <[email protected]>

* Switch to flit

This reverts commit d645790

* add setup.py while leaving it ignored

* Update install instructions

* Circumvent new pip check (see pypa/pip#9628)

* Go back to regular pip (#1702)

* Go back to regular flit

Co-authored-by: Isaac Virshup <[email protected]>

* codecov comment (#1704)

* Use joblib for parallelism in regress_out (#1695)

* Use joblib for parallism in regress_out

* release note

* fix link in release notes

* Add todo for resource test

* Add sparsificiation step before sparse-dependent Scrublet calls (#1707)

* Add sparsificiation step before sparse-dependent Scrublet calls

* Apply sparsification suggestion

Co-authored-by: Isaac Virshup <[email protected]>

* Fix imports

Co-authored-by: Isaac Virshup <[email protected]>

* Fix version on Travis (#1713)

By default, Travis does `git clone --depth=50` which means the version can’t be detected from the git tag.

* `sc.metrics` module (add confusion matrix & Geary's C methods) (#915)

* Add `sc.metrics` with `gearys_c`

Add a module for computing useful metrics. Started off with Geary's C since I'm using it and finding it useful. I've also got a fairly fast way to calculate it worked out.

Unfortunatly my implementation runs into some issues with some global configs set by umap (see lmcinnes/umap#306), so I'm going to see if that can be resolved before changing it.

* Add sc.metrics.confusion_matrix

* Better tests and output for confusion_matrix

* Workaround umap<0.4 and increase numerical stability of gearys_c

* Work around lmcinnes/umap#306 by not
  calling out to kernel function. That code has been kept, but commented
  out.
* Increase numerical stability by casting data to system width. Tests
  were failing due to instability.

* Split up gearys_c tests

* Improved unexpected error message

* gearys_c working again. Sadly, a bit slower

* One option for doc strings

* Simplify implementation to use single dispatch

* release notes

* Fix clipped images in docs (#1717)

* Cleanup normalize_total (#1667)

* Cleanup normalize_total

* Add modification tests and copy kwarg for normalize_total

* Test that 'layers' argument is deprecated

* Added more mutation checks for normalize_total

* release note

* Error message

* deprecate scvi (#1703)

* deprecate scvi

* Update .azure-pipelines.yml

Co-authored-by: Isaac Virshup <[email protected]>

* remove :func: links to scvi in release notes

* remove tildes in front of scvi in release notes

* Update docs/release-notes/1.5.0.rst

Co-authored-by: Michael Jayasuriya <[email protected]>
Co-authored-by: Isaac Virshup <[email protected]>

* updated ecosystem.rst to add triku (#1722)

* Minor addition to contributing docs (#1726)

* Preserve category order when groupby is a list (#1735)

Preserve category order when groupby is a list

* Asymmetrical diverging colormaps and vcenter (#1551)

Add vcenter and norm arguments to plotting functions

* add flake8 pre-commit

Signed-off-by: Zethson <[email protected]>

* add E402 to flake8 ignore

Signed-off-by: Zethson <[email protected]>

* revert neighbors

Signed-off-by: Zethson <[email protected]>

* address review

Signed-off-by: Zethson <[email protected]>

* black

Signed-off-by: Zethson <[email protected]>

* using self for obs_tidy

Signed-off-by: Zethson <[email protected]>

* rebased

Signed-off-by: Zethson <[email protected]>

* rebasing

Signed-off-by: Zethson <[email protected]>

* rebasing

Signed-off-by: Zethson <[email protected]>

* rebasing

Signed-off-by: Zethson <[email protected]>

* add flake8 to dev docs

Signed-off-by: Zethson <[email protected]>

* add autopep8 to pre-commits

Signed-off-by: Zethson <[email protected]>

* add flake8 ignore docs

Signed-off-by: Zethson <[email protected]>

* add exception todos

Signed-off-by: Zethson <[email protected]>

* add ignore directories

Signed-off-by: Zethson <[email protected]>

* reinstated lambdas

Signed-off-by: Zethson <[email protected]>

* fix tests

Signed-off-by: Zethson <[email protected]>

* fix tests

Signed-off-by: Zethson <[email protected]>

* fix tests

Signed-off-by: Zethson <[email protected]>

* fix tests

Signed-off-by: Zethson <[email protected]>

* fix tests

Signed-off-by: Zethson <[email protected]>

* Add E741 to allowed flake8 violations.

Co-authored-by: Isaac Virshup <[email protected]>

* Add F811 flake8 ignore for tests

Co-authored-by: Isaac Virshup <[email protected]>

* Fix mask comparison

Co-authored-by: Isaac Virshup <[email protected]>

* Fix mask comparison

Co-authored-by: Isaac Virshup <[email protected]>

* fix flake8 config file

Signed-off-by: Zethson <[email protected]>

* readded autopep8

Signed-off-by: Zethson <[email protected]>

* import Literal

Signed-off-by: Zethson <[email protected]>

* revert literal import

Signed-off-by: Zethson <[email protected]>

* fix scatterplot pca import

Signed-off-by: Zethson <[email protected]>

* false comparison & unused vars

Signed-off-by: Zethson <[email protected]>

* Add cleaner level determination

Co-authored-by: Isaac Virshup <[email protected]>

* Fix comment formatting

Co-authored-by: Isaac Virshup <[email protected]>

* Add smoother dev documentation

Co-authored-by: Isaac Virshup <[email protected]>

* fix flake8

Signed-off-by: Zethson <[email protected]>

* Readd long comment

Co-authored-by: Isaac Virshup <[email protected]>

* Assuming X as array like

Co-authored-by: Isaac Virshup <[email protected]>

* fix flake8

Signed-off-by: Zethson <[email protected]>

* fix flake8 config

Signed-off-by: Zethson <[email protected]>

* reverted rank_genes

Signed-off-by: Zethson <[email protected]>

* fix disp_mean_bin formatting

Co-authored-by: Isaac Virshup <[email protected]>

* fix formatting

Signed-off-by: Zethson <[email protected]>

* add final todos

Signed-off-by: Zethson <[email protected]>

* boolean checks with is

Signed-off-by: Zethson <[email protected]>

* _dpt formatting

Signed-off-by: Zethson <[email protected]>

* literal fixes

Signed-off-by: Zethson <[email protected]>

* links to leafs

Signed-off-by: Zethson <[email protected]>

* revert paga variable naming

Co-authored-by: Philipp A <[email protected]>
Co-authored-by: Sergei Rybakov <[email protected]>
Co-authored-by: Isaac Virshup <[email protected]>
Co-authored-by: Jonathan Manning <[email protected]>
Co-authored-by: mjayasur <[email protected]>
Co-authored-by: Michael Jayasuriya <[email protected]>
Co-authored-by: Alex M. Ascensión <[email protected]>
Co-authored-by: Gökçen Eraslan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants