Skip to content
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

Asymmetrical diverging colormaps and vcenter #1551

Merged
merged 39 commits into from
Mar 14, 2021
Merged

Asymmetrical diverging colormaps and vcenter #1551

merged 39 commits into from
Mar 14, 2021

Conversation

gokceneraslan
Copy link
Collaborator

Hi,

I started playing around with vcenter and adding support for it in plotting functions. This allows us to do things like:

import scanpy as sc

adata = sc.datasets.paul15()

sc.pp.log1p(adata)
sc.pp.scale(adata)
genes = ['Cst3', 'Car1', 'Cd34', 'Apoe', 'Top2a', 'Ccl5', 'Ctsw']
sc.pl.matrixplot(adata, genes, groupby='paul15_clusters', vmin=-2, vmax=5, vcenter=0, cmap='RdBu_r')

image

I haven't gone further before asking you if it makes sense at all, or not. If yes, I can try to write tests too. What do you think?

@ivirshup
Copy link
Member

ivirshup commented Dec 19, 2020

I like this idea.

  • While we should be conservative about adding new keywords, this fits well with vmin and vmax
  • The docs for this argument should mention that users should pass a diverging palette with it, and probably have an example
  • If norm is passed along at the same time, an error should be thrown
  • It looks like there is a bunch of repeated code handling generating the norm, could this get put into a common utility function?

@ivirshup
Copy link
Member

If norm is passed along at the same time, an error should be thrown

Following up on this a bit, I realized I didn't actually know what matplotlib would do if you passed norm and a bound, so I checked it out. Turns out they currently allow it, but it's deprecated, so throwing an error is the right thing to do.

import vega_datasets
import matplotlib as mpl, matplotlib.pyplot as plt

iris = vega_datasets.data.iris()

norm = mpl.colors.LogNorm()

plt.scatter(
    iris["sepalLength"],
    iris["sepalWidth"],
    c=iris["petalLength"],
    norm=norm,
    vmin=3,
)
plt.colorbar()
 MatplotlibDeprecationWarning: Passing parameters norm and vmin/vmax 
simultaneously is deprecated since 3.3 and will become an error two minor releases 
later. Please pass vmin/vmax directly to the norm when creating it.

@gokceneraslan
Copy link
Collaborator Author

 MatplotlibDeprecationWarning: Passing parameters norm and vmin/vmax 
simultaneously is deprecated since 3.3 and will become an error two minor releases 
later. Please pass vmin/vmax directly to the norm when creating it.

Yeah, that actually re-ignited my idea of adding support for vcenter after upgrading mpl :)

@gokceneraslan
Copy link
Collaborator Author

gokceneraslan commented Dec 26, 2020

@fidelram Some help with the plotting tests is very much appreciated :) I don't get why only Python 3.6 test passes and others fail.

For example; why does this plot which is produced by this code where the color legend is defined here as Mean expression in group have the old title which is Expression level in group? If the "ground truth plots" in the "_images" folder are outdated, aren't the tests supposed to fail since the this commit i.e. Expression level in group -> Mean expression in group, but they don't fail, why?

Another example: I have updated this file but not that file which seem both outdated. But Python 3.6 test still seems to pass. How come? Are plotting tests disabled in Python3.6?

Plotting tests are extremely tricky and make it pretty difficult to contribute I must say :/ I think twice or three times when I want to change anything about plotting... (not a complaint addressed to @fidelram but something we should consider as a team)

Copy link
Member

@ivirshup ivirshup left a comment

Choose a reason for hiding this comment

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

Thanks for addressing my initial comments. I would definitely like @fidelram to take a look at it as well.

One addition I would like to see is a documented example with the expected plot and some prose. Maybe one for DE plots, one for embedding plots? This might be at home in the plotting tutorials.

I've requested some more changes in comments on the code

scanpy/plotting/_anndata.py Show resolved Hide resolved
scanpy/plotting/_tools/scatterplots.py Outdated Show resolved Hide resolved
scanpy/plotting/_utils.py Outdated Show resolved Hide resolved
scanpy/plotting/_baseplot_class.py Outdated Show resolved Hide resolved
scanpy/plotting/_tools/scatterplots.py Outdated Show resolved Hide resolved
scanpy/plotting/_stacked_violin.py Outdated Show resolved Hide resolved
scanpy/plotting/_tools/scatterplots.py Outdated Show resolved Hide resolved
scanpy/plotting/_utils.py Outdated Show resolved Hide resolved
scanpy/plotting/_anndata.py Show resolved Hide resolved
scanpy/plotting/_docs.py Outdated Show resolved Hide resolved
@fidelram
Copy link
Collaborator

@gokceneraslan Regarding the tests: yes, they are annoying particularly because is not possible to actually check why a test failed on the server while passes locally. I agree that this limits contribution because the mountain of work to get the tests working puts one off.

For the particular question about the title difference: the test may be passing because of the 'threshold' used to call the images as different. Why we use a threshold? This is to avoid tests from failing due to small differences between matplotlib or other graphic libraries versions or fonts installed. However, sometimes the threshold may be masking some small problems, although in general I am quite happy because important differences not missed.

BTW: The image that you point out is clearly wrong but I updated it recently for other reason (PR #1584).

Regarding the issue about adding norm as explicit parameter. I would suggest to add it if this just mean changing very few lines but I know this is lot of work (do we want tests for this?) for something that is already working.

Besides the very good review by Isaac I don't have much to add and will be happy to merge once some of the changes are taken care.

@ivirshup ivirshup added this to the 1.8.0 milestone Feb 4, 2021
@codecov
Copy link

codecov bot commented Mar 11, 2021

Codecov Report

Merging #1551 (cebeefc) into master (da66e15) will increase coverage by 0.08%.
The diff coverage is 95.45%.

@@            Coverage Diff             @@
##           master    #1551      +/-   ##
==========================================
+ Coverage   71.25%   71.33%   +0.08%     
==========================================
  Files          91       91              
  Lines       11090    11116      +26     
==========================================
+ Hits         7902     7930      +28     
+ Misses       3188     3186       -2     
Impacted Files Coverage Δ
scanpy/plotting/_baseplot_class.py 84.46% <83.33%> (+0.65%) ⬆️
scanpy/plotting/_utils.py 54.79% <86.66%> (+0.87%) ⬆️
scanpy/plotting/_anndata.py 84.47% <100.00%> (+0.29%) ⬆️
scanpy/plotting/_docs.py 100.00% <100.00%> (ø)
scanpy/plotting/_dotplot.py 86.79% <100.00%> (ø)
scanpy/plotting/_matrixplot.py 97.87% <100.00%> (ø)
scanpy/plotting/_stacked_violin.py 83.67% <100.00%> (-0.09%) ⬇️
scanpy/plotting/_tools/__init__.py 76.27% <100.00%> (ø)
scanpy/plotting/_tools/scatterplots.py 86.95% <100.00%> (+0.15%) ⬆️

@gokceneraslan gokceneraslan merged commit dfcb0f6 into master Mar 14, 2021
Zethson pushed a commit that referenced this pull request Mar 15, 2021
Add vcenter and norm arguments to plotting functions
@gokceneraslan gokceneraslan deleted the vcenter_v2 branch March 15, 2021 12:41
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants