nipy / PySurfer

Cortical neuroimaging visualization in Python
https://pysurfer.github.io/
BSD 3-Clause "New" or "Revised" License
241 stars 97 forks source link

Put 0 as transparent in correlation map with both negative and positive values + divergent colormap #280

Closed mattvan83 closed 4 years ago

mattvan83 commented 4 years ago

Dear PySurfer,

I would like to save a correlation map with both negative and positive values. I would like also that 0 to be considered as transparent value, but I didn't manage to do it by using either thresh or either transparent argument of the add_data method.

Please find below the code I used and attached in .zip file the raw data and correlation map of the left hemisphere saved with 0 coded as white colour instead of transparent:

 """
Get paths and load overlay arrays.
 """
overlay_file_lh = "lh.palm.fdr0.Corr_dpv_pcc_thr.mgh"
overlay_array_lh = nib.load(overlay_file_lh)
overlay_array_lh = overlay_array_lh.get_fdata()
overlay_array_lh = np.squeeze(overlay_array_lh)

"""
Bring up the visualization.
"""
brain = Brain("fsaverage", "lh", "white", background="black")
brain.add_data(overlay_array_lh, 0, 0.641, center=0, colormap="RdBu", hemi="lh", alpha=1)
brain.save_montage("lh.montage.correlation.Corr.vertex.cerebellum.start0_dur4.fdr0.tiff", order=['lateral', 'medial'], orientation='h', border_size=15, colorbar=[0], row=-1, col=-1)

Could you help me to solve this problem?

Best, Matthieu

lh.montage.correlation.Corr.vertex.cerebellum.start0_dur4.fdr0.tiff.zip

larsoner commented 4 years ago

Like this?

from surfer import Brain
import numpy as np
overlay_array_lh = np.random.RandomState(0).randn(10242)
vertices = np.arange(10242)
brain = Brain("fsaverage", "lh", "white", background="black")
brain.add_data(
    overlay_array_lh, min=0.5, mid=1.25, max=2, center=0, colormap="RdBu",
    hemi="lh", transparent=True, vertices=vertices, smoothing_steps=3)
Screen Shot 2020-01-14 at 10 10 19
mattvan83 commented 4 years ago

This worked for the example you shown above. But when using my particular data with your syntax trying to put transparent only 0 values nothing appeared:

brain.add_data(overlay_array_lh, min=0.001, mid=0.01, max=0.641, center=0, colormap="seismic", hemi="lh", transparent=True)
brain.save_montage(os.path.join(outdir,"lh." + filename + ".tiff"), order=['lateral', 'medial'], orientation='h', border_size=15, colorbar=[0], row=-1, col=-1)

Could you donwload my specific data upload in my previous post and test with my command lines above?

Thanks

larsoner commented 4 years ago

The snippet:

from surfer import Brain
import numpy as np
import nibabel as nib
overlay_file_lh = "lh.palm.fdr0.Corr_dpv_pcc_thr.mgh"
overlay_array_lh = nib.load(overlay_file_lh)
overlay_array_lh = overlay_array_lh.get_fdata()
overlay_array_lh = np.squeeze(overlay_array_lh)
brain = Brain("fsaverage", "lh", "white", background="black")
brain.add_data(overlay_array_lh, min=0.001, mid=0.01, max=0.641, center=0, colormap="seismic", hemi="lh", transparent=True)
brain.save_montage("lh.png", order=['lateral', 'medial'], orientation='h', border_size=15, colorbar=[0], row=-1, col=-1)

produces:

lh

I got the same image with a .tiff extension, too, but GitHub does not allow uploading those, hence the .png above.

Are you on latest master of PySurfer? Maybe there is some graphics card problem.

mattvan83 commented 4 years ago

Indeed it's surprising...

How to know whether I'm using the latest master of PySurfer? My actual one is 0.9.0 that I have installed via conda install pysurfer (conda-forge deposit).

1) How could I clean an old installation with dependencies and start a new one without perturbing my conda environment? Is pip installation is better and manages installation of dependencies of pysurfer?

2) Is is better to install the development version?

3) How to know whether I have some graphics card problem?

Thanks

larsoner commented 4 years ago

How could I clean an old installation with dependencies and start a new one without perturbing my conda environment? Is pip installation is better and manages installation of dependencies of pysurfer?

There are many ways to do this, but for example you could do:

$ conda create -n pysurfer numpy scipy mayavi
$ conda activate pysurfer
$ pip install https://api.github.com/repos/nipy/PySurfer/zipball/master

Instead of the last line if you plan to switch branches and use git it's better to do:

git clone https://github.com/nipy/PySurfer.git
cd PySurfer
pip install -ve .

Either way you should end up with a pysurfer env with latest master PySurfer. The second one just allows you to eventually switch and test other branches more easily.

As for next steps, the first thing to check is probably whether any of the examples look correct on your system:

https://pysurfer.github.io/auto_examples/index.html

Knowing which ones (if any) do look correct and which ones don't would help determine if it's an OpenGL rendering problem of some sort. PySurfer uses overlapping drawn surfaces, which sometimes are a pain to render properly using OpenGL.

Also, if you pip install mne then do python -c "import mne; mne.sys_info()" what does it say? It should give information about your system, mayavi, etc. that could be useful.

mattvan83 commented 4 years ago

I performed the second way you explained above.

When launching python -c "import mne; mne.sys_info()", it says:

Platform:      Linux-3.10.0-957.5.1.el7.x86_64-x86_64-with-glibc2.10
Python:        3.8.1 | packaged by conda-forge | (default, Jan  5 2020, 20:58:18)  [GCC 7.3.0]
Executable:    /homes_unix/mvanhoutte/Soft/anaconda3/envs/pysurfer/bin/python
CPU:           x86_64: 56 cores
Memory:        Unavailable (requires "psutil" package)
mne:           0.19.2
numpy:         1.17.3 {blas=blas, lapack=blas}
scipy:         1.4.1
matplotlib:    3.1.2 {backend=Qt5Agg}

sklearn:       Not found
numba:         Not found
nibabel:       3.0.0
cupy:          Not found
pandas:        Not found
dipy:          Not found
mayavi:        4.7.1 {qt_api=pyqt5, PyQt5=5.12.3}
pyvista:       Not found
vtk:           8.2.0

I am going to test the examples with this new installation and will come back if any problem.

mattvan83 commented 4 years ago

I finally obtained the same results as before, i.e. nothing appeared... (see image below)

How to control if there is a problem with OpenGL?

montage correlation Corr vertex cerebellum start0_dur4 fdr0

larsoner commented 4 years ago

56 cores, is this a remote workstation of some sort? It can be tricky to get OpenGL working well on those.

The only ideas I have for fixing this:

mattvan83 commented 4 years ago

Yes, I work on a remote multi-core workstation. Normally, OpenGL has to work according network engineers.

I tried to add alpha with values you mentioned, but it didn't work. I would like to test parameters on the toolbar via brain.toggle_toolbars(True) but the simple plot just appeared then disappeared. How could I freeze the plot in order to test OpenGL properties?

larsoner commented 4 years ago

but the simple plot just appeared then disappeared.

Are you storing the brain object so that it does not get garbage collected? Doing:

Brain(...)

it could appear and disappear, but doing

brain = Brain(...)

the brain object shouldn't get GC'ed and should stay around.

Also you probably want to make sure you're running with python -i or something that will not just close at the end of execution.

mattvan83 commented 4 years ago

@larsoner ok I will try it on my remote workstation.

I tested the code with my local machine and it worked...

mattvan83 commented 4 years ago

Ok it worked with python -i.

I close this issue.