cytoscape / py4cytoscape

Python library for calling Cytoscape Automation via CyREST
https://Py4Cytoscape.readthedocs.io
Other
69 stars 15 forks source link

Reverse color palettes? #62

Closed AlexanderPico closed 3 years ago

AlexanderPico commented 3 years ago

When using palette_color_brewer_* palettes like d_RdBu for example, the mapping is going to wrong 99% of the time, i.e., red for negative values and blue for positive. A couple suggestions:

  1. reverse these palettes by default
  2. offer a reverse Boolean parameter that gets passed down to _palette_color_brewer
AlexanderPico commented 3 years ago

In RCy3, this was necessarily implemented slightly differently (since R can't do that cool ** thing) and there are paletteColorBrewer* functions that return color vectors. These are reversed by default, but can also be reversed by the user with the commonly used rev()function in R.

E.g., https://github.com/cytoscape/RCy3/blob/master/R/StyleMappings.R#L2539

AlexanderPico commented 3 years ago

Specific use case:

p4c.open_session()
p4c.set_node_color_mapping(**p4c.gen_node_color_map('gal80Rexp', p4c.palette_color_brewer_d_RdBu(), style_name='galFiltered Style'))

RAP1 has a positive expression value and GCR1 has a negative, but the typical color mapping here is reversed and I can't seem to correct it: Screen Shot 2021-08-14 at 11 27 25 AM

bdemchak commented 3 years ago

Thanks, Alex … this is a nice gift.

I’ll work on this in the next couple of days and see if there’s something I can do that makes sense to a biologist, which is the most important standard.

Code-wise, no problem … but broken is broken.

From: Alexander Pico @.> Sent: Saturday, August 14, 2021 11:29 AM To: cytoscape/py4cytoscape @.> Cc: Subscribed @.***> Subject: Re: [cytoscape/py4cytoscape] Reverse color palettes? (#62)

Specific use case:

p4c.open_session() p4c.set_node_color_mapping(**p4c.gen_node_color_map('gal80Rexp', p4c.palette_color_brewer_d_RdBu(), style_name='galFiltered Style'))

RAP1 has a positive expression value and GCR1 has a negative, but the typical color mapping here is reversed and I can't seem to correct it: https://urldefense.proofpoint.com/v2/url?u=https-3A__user-2Dimages.githubusercontent.com_1418193_129456844-2D4f2b4fc6-2D4e88-2D4a28-2Da9bc-2D0d93a00f1fbb.png&d=DwMCaQ&c=-35OiAkTchMrZOngvJPOeA&r=CsLWmDiVCCVSLXhL9WA-Kh4UUOkFEEy643k8U5VIUxE&m=t-gQn7FSyR_vcP12VXk-2LgJkK9NnpOs_zGnMZ0ToR8&s=-PeOrM3ZXTg4iZp1Mo-Udh_AJYgPSas95LUWKoU70I4&e=

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_cytoscape_py4cytoscape_issues_62-23issuecomment-2D898941812&d=DwMCaQ&c=-35OiAkTchMrZOngvJPOeA&r=CsLWmDiVCCVSLXhL9WA-Kh4UUOkFEEy643k8U5VIUxE&m=t-gQn7FSyR_vcP12VXk-2LgJkK9NnpOs_zGnMZ0ToR8&s=yls3GcIydoy02UnFzraJVYChHzCXr46D2MmYh36YGP0&e= , or unsubscribe https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_notifications_unsubscribe-2Dauth_AA4GLXRTY6BS3YNA6UCWHPTT42YV7ANCNFSM5CFKFYGQ&d=DwMCaQ&c=-35OiAkTchMrZOngvJPOeA&r=CsLWmDiVCCVSLXhL9WA-Kh4UUOkFEEy643k8U5VIUxE&m=t-gQn7FSyR_vcP12VXk-2LgJkK9NnpOs_zGnMZ0ToR8&s=K0xU1-5Vnv2YYoOFVCDSlwwThbiviPj0ew2pz7r2xBY&e= . Triage notifications on the go with GitHub Mobile for iOS https://urldefense.proofpoint.com/v2/url?u=https-3A__apps.apple.com_app_apple-2Dstore_id1477376905-3Fct-3Dnotification-2Demail-26mt-3D8-26pt-3D524675&d=DwMCaQ&c=-35OiAkTchMrZOngvJPOeA&r=CsLWmDiVCCVSLXhL9WA-Kh4UUOkFEEy643k8U5VIUxE&m=t-gQn7FSyR_vcP12VXk-2LgJkK9NnpOs_zGnMZ0ToR8&s=zMKmhxUTA81juec-8rYHupCeJhAVe9MVyDVbjhVSORI&e= or Android https://urldefense.proofpoint.com/v2/url?u=https-3A__play.google.com_store_apps_details-3Fid-3Dcom.github.android-26utm-5Fcampaign-3Dnotification-2Demail&d=DwMCaQ&c=-35OiAkTchMrZOngvJPOeA&r=CsLWmDiVCCVSLXhL9WA-Kh4UUOkFEEy643k8U5VIUxE&m=t-gQn7FSyR_vcP12VXk-2LgJkK9NnpOs_zGnMZ0ToR8&s=5f4y4yC_rh8XZDGaWu0hY13ZKI-k-TuPpvzITcgKkhg&e= . https://github.com/notifications/beacon/AA4GLXUET55CXJVGB2G57MLT42YV7A5CNFSM5CFKFYG2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOGWKMG5A.gif

bdemchak commented 3 years ago

Hi, Alex ...

I don't think I understand, though your example seems pretty clear.

So, GCR1 has a value of -0.53 and RAP1 has a value of 0.234 ... all as you say. Given this, I would expect GCR1 to have more of a redish color than RAP1 ... and indeed this would be the case if GCR1's color is to the left of RAP1's color on this palette. (See RdBu below)

image

Can you explain more about what a biologist would expect in this situation?

I have no problem with a "reverse" parameter. All things being equal, there isn't a Cytoscape operation that duplicates this situation because Cytoscape presents only the qualitative colors (?) and not the Diverging colors (which contains RdBu).

But using the same principle of lesser values being to the left of the spectrum and greater values being to the right, I tried Cytoscape out with the Paired palette (below). Indeed, Cytoscape generated colors according to principle.

image

image

So, I'm guessing there must be another principle that allows for applying a palette according to attribute values.

I'm guessing that it's more like ... 1) give me the palette I want, 2) let me choose whether the mapping is forward or reverse.

Can you explain more??

Thanks!

AlexanderPico commented 3 years ago

Typical divergent mapping for RdBu goes: Blue for negative, White for zero, Red for positive. In Cytoscape GUI, you can open galFiltered and choose the RdBu divergent palette for node fill color and you'll see this by default:

Screen Shot 2021-08-16 at 10 54 35 AM

IIRC, Scooter simply reversed all the defaults for the set of divergent palettes (like I did in RCy3). In the GUI, users can manually swap the mappings along the palette glyph if they want the reverse.

bdemchak commented 3 years ago

Hi, Alex --

OK ... I think I see. If I look at the RCy3 StyleMappings.R code, I see that the default operation has Continuous and Sequential palettes as Brewer Colors and the Divergent palettes as the reverse of Brewer Colors.

One thing I was missing was how to apply Brewer palettes for continuous mappings in Cytoscape (i.e., double-click on the palette and choose the Current Palette selector). I had thought that palette generators happened in Cytoscape only for Discrete mappings. (This is my own fault, as Scooter has mentioned this many times and I just didn't focus on what he meant.)

So, I'll implement all of this by passing each palette generation function a reverse= parameter, which will default to True for Discrete palettes and False for Continuous and Sequential palettes. Pretty simple, and the caller can always explicitly pass reverse= if he/she cares to.

I think this captures all intents in the simplest way ... please let me know if you disagree.

I'll let you know when I have checked this in.

AlexanderPico commented 3 years ago

Sounds right. Happy to test when ready.

On Aug 16, 2021, at 4:49 PM, Barry Demchak @.***> wrote:

 Hi, Alex --

OK ... I think I see. If I look at the RCy3 StyleMappings.R code, I see that the default operation has Continuous and Sequential palettes as Brewer Colors and the Divergent palettes as the reverse of Brewer Colors.

One thing I was missing was how to apply Brewer palettes for continuous mappings in Cytoscape (i.e., double-click on the palette and choose the Current Palette selector). I had thought that palette generators happened in Cytoscape only for Discrete mappings. (This is my own fault, as Scooter has mentioned this many times and I just didn't focus on what he meant.)

So, I'll implement all of this by passing each palette generation function a reverse= parameter, which will default to True for Discrete palettes and False for Continuous and Sequential palettes. Pretty simple, and the caller can always explicitly pass reverse= if he/she cares to.

I'll let you know when I have checked this in.

I think this captures all intents in the simplest way ... please let me know if you disagree.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

bdemchak commented 3 years ago

Hi, Alex –

OK … I think I have done this …

Each Brewer palette function now has a reverse= parameter that defaults to False.

The Divergent palettes are reversed from the get-go, so they should produce the colors you’d expect. To get the original Brewer palette, the caller would use reverse=True.

Would you mind taking a look??

Thanks!

From: Alexander Pico @.> Sent: Monday, August 16, 2021 5:07 PM To: cytoscape/py4cytoscape @.> Cc: Barry Demchak @.>; Comment @.> Subject: Re: [cytoscape/py4cytoscape] Reverse color palettes? (#62)

Sounds right. Happy to test when ready.

On Aug 16, 2021, at 4:49 PM, Barry Demchak @. <mailto:@.> > wrote:

 Hi, Alex --

OK ... I think I see. If I look at the RCy3 StyleMappings.R code, I see that the default operation has Continuous and Sequential palettes as Brewer Colors and the Divergent palettes as the reverse of Brewer Colors.

One thing I was missing was how to apply Brewer palettes for continuous mappings in Cytoscape (i.e., double-click on the palette and choose the Current Palette selector). I had thought that palette generators happened in Cytoscape only for Discrete mappings. (This is my own fault, as Scooter has mentioned this many times and I just didn't focus on what he meant.)

So, I'll implement all of this by passing each palette generation function a reverse= parameter, which will default to True for Discrete palettes and False for Continuous and Sequential palettes. Pretty simple, and the caller can always explicitly pass reverse= if he/she cares to.

I'll let you know when I have checked this in.

I think this captures all intents in the simplest way ... please let me know if you disagree.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_cytoscape_py4cytoscape_issues_62-23issuecomment-2D899896351&d=DwMFaQ&c=-35OiAkTchMrZOngvJPOeA&r=CsLWmDiVCCVSLXhL9WA-Kh4UUOkFEEy643k8U5VIUxE&m=Cjxyh6EHGH2QroAWwGSDHpTRtw-v375h-GViUamGlMs&s=T4QngbeI6INL_sTlm-xxukK_Vt_JsQ9J6_L4p8PohT0&e= , or unsubscribe https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_notifications_unsubscribe-2Dauth_AA4GLXRO6IHUEFSUAZ5CBZLT5GR2JANCNFSM5CFKFYGQ&d=DwMFaQ&c=-35OiAkTchMrZOngvJPOeA&r=CsLWmDiVCCVSLXhL9WA-Kh4UUOkFEEy643k8U5VIUxE&m=Cjxyh6EHGH2QroAWwGSDHpTRtw-v375h-GViUamGlMs&s=lILnHJ_r8St3xxaaxYRj4M9HNDN7FS2kkT5WPpflDX4&e= . Triage notifications on the go with GitHub Mobile for iOS https://urldefense.proofpoint.com/v2/url?u=https-3A__apps.apple.com_app_apple-2Dstore_id1477376905-3Fct-3Dnotification-2Demail-26mt-3D8-26pt-3D524675&d=DwMFaQ&c=-35OiAkTchMrZOngvJPOeA&r=CsLWmDiVCCVSLXhL9WA-Kh4UUOkFEEy643k8U5VIUxE&m=Cjxyh6EHGH2QroAWwGSDHpTRtw-v375h-GViUamGlMs&s=wwBcved7eOe2vQEWzQslbsilwSpHRHZwQ97iN9Teeuk&e= or Android https://urldefense.proofpoint.com/v2/url?u=https-3A__play.google.com_store_apps_details-3Fid-3Dcom.github.android-26utm-5Fcampaign-3Dnotification-2Demail&d=DwMFaQ&c=-35OiAkTchMrZOngvJPOeA&r=CsLWmDiVCCVSLXhL9WA-Kh4UUOkFEEy643k8U5VIUxE&m=Cjxyh6EHGH2QroAWwGSDHpTRtw-v375h-GViUamGlMs&s=_BD-NsVkj6YjwHmboobRY8h83pQTdd44TTi-T_ojmTU&e= . https://github.com/notifications/beacon/AA4GLXVA4LXMRIFSPTZPPRLT5GR2JA5CNFSM5CFKFYG2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOGWRVIHY.gif

AlexanderPico commented 3 years ago

Trying to install dev code in notebook, but I think I'm missing something... Followed this part of INSTALL: https://github.com/cytoscape/py4cytoscape/blob/master/INSTALL.rst#install-the-development-version-python-console

But is the git clone step sufficient? I'm getting this error on subequent steps:

_AttributeError: module 'py4cytoscape' has no attribute 'get_browser_clientjs'

and

_AttributeError: module 'py4cytoscape' has no attribute 'cytoscape_versioninfo'

By the way, I'm testing the node color mapping with this tutorial run via the colab link :D https://github.com/cytoscape/cytoscape-automation/blob/master/for-scripters/Python/basic-data-visualization.ipynb

bdemchak commented 3 years ago

Hmm ... I think you're executing installation instructions intended for the Python command line, not a Notebook. I don't think I've run these instructions in a while, but my first thought would be to use the instructions for Notebook use. There's a link in the Note box at the beginning of the Install section ... https://py4cytoscape.readthedocs.io/en/latest/concepts.html#jupyter-notebook ... I haven't tried these instructions in a while, either. I think it should resolve here: https://py4cytoscape.readthedocs.io/en/latest/concepts.html#jupyter-notebook

But jumping right to the example you're using, I'd say that you should execute the first cell (with the %%capture) only for a local notebook or command line Python. And you should execute the second cell (with the exec call) if you're running on a remote notebook. The two cells are mutually exclusive, either the first cell or the second cell, but not both.

In your case, there's a little more because you want to use a py4cytoscape in a Github branch. See here for the first cell you'll need: https://colab.research.google.com/github/bdemchak/cytoscape-jupyter/blob/main/gangsu/basic_protocol_2.ipynb#scrollTo=9RVNEFryzJR3 ...

_PY4CYTOSCAPE = 'git+https://github.com/cytoscape/py4cytoscape@0.0.10' # optional import requests exec(requests.get("https://raw.githubusercontent.com/cytoscape/jupyter-bridge/master/client/p4c_init.py").text) IPython.display.Javascript(_PY4CYTOSCAPE_BROWSER_CLIENT_JS) # Start browser client

In truth, this should work for either a local or remote notebook, thereby enabling a single development for both.

Kozo's tutorials do this.

A subtle point ... the documentation for 0.0.10 has this (though not well enough, I think). You can see more here: https://py4cytoscape.readthedocs.io/en/0.0.10/concepts.html#jupyter-notebook ... this will be published when 0.0.10 becomes master.

I just tried using the above snippet in the second cell of your basic viz notebook (... not executing the first cell at all), and it seems to work well.

Let's see if you can duplicate this.