Code Monkey home page Code Monkey logo

python-ggseg's Introduction

main Coverage Status MIT License pypi version

python-ggseg

Python module for ggseg-like visualizations.

Dependencies

Requires matplotlib>=3.4 and numpy>=1.21.

Install

pip install ggseg

Usage

In order to work with python-ggseg, the data should be prepared as a dictionary where each item is one region of a given atlas assigned with some numeric value. The current version includes three atlases: the Desikan-Killiany (DK) atlas , the Johns Hopkins University (JHU) white-matter atlas and the FreeSurfer aseg atlas.

Cortical ROIs (Desikan-Killiany)

Cortical ROI data such as using the DK atlas may be structured as follows:

{'bankssts_left': 1.1,
 'caudalanteriorcingulate_left': 1.0,
 'caudalmiddlefrontal_left': 2.6,
 'cuneus_left': 2.6,
 'entorhinal_left': 0.6,
 ...}

Then be passed to the ggseg.plot_dk function:

import ggseg
ggseg.plot_dk(data, cmap='Spectral', figsize=(15,15),
              background='k', edgecolor='w', bordercolor='gray',
              ylabel='Cortical thickness (mm)', title='Title of the figure')

DK

The comprehensive list of applicable regions can be found in this folder.

Subcortical regions (FreeSurfer aseg atlas)

data = {'Left-Lateral-Ventricle': 12289.6,
        'Left-Thalamus': 8158.3,
        'Left-Caudate': 3463.3,
        'Left-Putamen': 4265.3,
        'Left-Pallidum': 1620.9,
        '3rd-Ventricle': 1635.6,
        '4th-Ventricle': 1115.6,
        ...}
ggseg.plot_aseg(data, cmap='Spectral',
                background='k', edgecolor='w', bordercolor='gray',
                ylabel='Volume (mm3)', title='Title of the figure')

aseg

The comprehensive list of applicable regions can be found in this folder.

White-matter fiber tracts (Johns Hopkins University)

data = {'Anterior thalamic radiation L': 0.3004812598228455,
        'Anterior thalamic radiation R': 0.2909256815910339,
        'Corticospinal tract L': 0.3517134189605713,
        'Corticospinal tract R': 0.3606771230697632,
        'Cingulum (cingulate gyrus) L': 0.3149917721748352,
        'Cingulum (cingulate gyrus) R': 0.3126821517944336,
        ...}
ggseg.plot_jhu(data, background='k', edgecolor='w', cmap='Spectral',
               bordercolor='gray', ylabel='Mean Fractional Anisotropy',
               title='Title of the figure')

JHU

The comprehensive list of applicable regions can be found in this folder.

Tests

The current development version of python-ggseg has a coverage rate close to 100%. The corresponding tests can be found in this folder.

Examples

A Jupyter Notebook with examples can be found there.

python-ggseg's People

Contributors

sipv avatar xgrg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

python-ggseg's Issues

Error using ggseg.plot_dk()

Hello !

First thanks for this very nice plotting tool ! I just have an error popping out when trying to use ggseg.plot_dk() with ggseg version 0.1, on Ubuntu 22.04.1 LTS, matplotlib version 3.5.2 and numpy 1.22.3.

I am initializing a dictionary with all DK labels, assigning mostly zeros and a few ones here and there. And then calling the ggseg.plot_dk() function, I get an error saying:

File ~/Software/anaconda3/envs/code_testing/lib/python3.9/site-packages/ggseg/__init__.py:168, in plot_dk(data, cmap, background, edgecolor, ylabel, figsize, bordercolor, vminmax, title, fontsize)
    166 NA = set(dkt_regions).difference(data_regions).difference(whole_reg)
    167 files = [open(op.join(wd, e)).read() for e in NA]
--> 168 _render_regions_(files, ax, 'gray', edgecolor)
    170 # A colorbar is added
    171 _add_colorbar_(ax, cmap, norm, edgecolor, fontsize*0.75, ylabel)

File ~/Software/anaconda3/envs/code_testing/lib/python3.9/site-packages/ggseg/__init__.py:92, in _render_regions_(files, ax, facecolor, edgecolor)
     89 from matplotlib.path import Path
     90 import matplotlib.patches as patches
---> 92 codes, verts = _svg_parse_(' '.join(files))
     93 path = Path(verts, codes)
     95 ax.add_patch(patches.PathPatch(path, facecolor=facecolor,
     96                                edgecolor=edgecolor, lw=1))

File ~/Software/anaconda3/envs/code_testing/lib/python3.9/site-packages/ggseg/__init__.py:32, in _svg_parse_(path)
     30         codes.append(commands[cmd.upper()])
     31     vertices.append(points)
---> 32 return np.array(codes), np.concatenate(vertices)

File <__array_function__ internals>:180, in concatenate(*args, **kwargs)

ValueError: need at least one array to concatenate

If I remove one entry of the dictionary (eg superiorparietal_left), then the code runs, but the displayed figure lacks some unrelated regions, as the rostralanteriorcingulate, cuneus, or inferior parietal for example. I tried removing different regions from the dictionary but the plot is always lacking the same regions... I also tried assigning a random value to all entries instead of zeros and ones, but the error pops out again. Is anybody having the same issue ? Would you know how to fix it ?

Here is the code I am running:
import ggseg
ggseg_dict={'NA_left': 0.0,
'NA_right': 0.0,
'bankssts_left': 0.0,
'bankssts_right': 0.0,
'caudalanteriorcingulate_left': 1.0,
'caudalanteriorcingulate_right': 1.0,
'caudalmiddlefrontal_left': 0.0,
'caudalmiddlefrontal_right': 0.0,
'corpuscallosum_left': 0.0,
'corpuscallosum_right': 0.0,
'cuneus_left': 0.0,
'cuneus_right': 0.0,
'entorhinal_left': 0.0,
'entorhinal_right': 0.0,
'fusiform_left': 0.0,
'fusiform_right': 0.0,
'inferiorparietal_left': 0.0,
'inferiorparietal_right': 0.0,
'inferiortemporal_left': 0.0,
'inferiortemporal_right': 0.0,
'insula_left': 0.0,
'insula_right': 0.0,
'isthmuscingulate_left': 0.0,
'isthmuscingulate_right': 0.0,
'lateral_left': 0.0,
'lateral_right': 0.0,
'lateraloccipital_left': 0.0,
'lateraloccipital_right': 0.0,
'lateralorbitofrontal_left': 0.0,
'lateralorbitofrontal_right': 0.0,
'lingual_left': 0.0,
'lingual_right': 0.0,
'medial_left': 0.0,
'medial_right': 0.0,
'medialorbitofrontal_left': 0.0,
'medialorbitofrontal_right': 0.0,
'middletemporal_left': 0.0,
'middletemporal_right': 0.0,
'paracentral_left': 0.0,
'paracentral_right': 0.0,
'parahippocampal_left': 1.0,
'parahippocampal_right': 1.0,
'parsopercularis_left': 0.0,
'parsopercularis_right': 0.0,
'parsorbitalis_left': 0.0,
'parsorbitalis_right': 0.0,
'parstriangularis_left': 0.0,
'parstriangularis_right': 0.0,
'pericalcarine_left': 0.0,
'pericalcarine_right': 0.0,
'postcentral_left': 0.0,
'postcentral_right': 0.0,
'posteriorcingulate_left': 0.0,
'posteriorcingulate_right': 0.0,
'precentral_left': 0.0,
'precentral_right': 0.0,
'precuneus_left': 0.0,
'precuneus_right': 0.0,
'rostralanteriorcingulate_left': 0.0,
'rostralanteriorcingulate_right': 0.0,
'rostralmiddlefrontal_left': 0.0,
'rostralmiddlefrontal_right': 0.0,
'superiorfrontal_left': 1.0,
'superiorfrontal_right': 1.0,
'superiorparietal_left': 0.0,
'superiorparietal_right': 0.0,
'superiortemporal_left': 0.0,
'superiortemporal_right': 0.0,
'supramarginal_left': 0.0,
'supramarginal_right': 0.0,
'transversetemporal_left': 0.0,
'transversetemporal_right':0.0}
ggseg.plot_dk(ggseg_dict,cmap='Spectral', figsize=(15,15),
              background='k', edgecolor='w', bordercolor='gray',
              ylabel='Cortical thickness (mm)', title='Title of the figure')

new atlases

Hi! I already saw one closed issue about adding new atlases, but I just wanted to ask, if you could elaborate on how you generated these svg files for all DK ROIs? For example, Do you still have the script for creating this and could share this?

Erroneous labeling of 'superiorparietal_left' and 'superiorfrontal_left' in ggseg.plot_dk()

Hi again !

I think I found a bug in the labeling of 'superiorparietal_left' and 'superiorfrontal_left' ROIs in Desikan-Killiany atlas.

I have a dictionary {'superiorparietal_left': 1}, and running ggseg.plot_dk() also labels the medial superiorfrontal_left cortex:

image

Similarly, when setting 'superiorfrontal_left' to 1.0, only the lateral part gets labelled (the largest medial part is not labelled):

image

Labelling of right ROIs works fine.

Here is the code I am running in case you want to have a look:

import ggseg
ggseg.plot_dk({'superiorparietal_left': 1.0},cmap='Spectral', figsize=(15,15),
              background='k', edgecolor='w', bordercolor='gray',
              ylabel='Cortical thickness (mm)', title='Title of the figure')
ggseg.plot_dk({'superiorfrontal_left': 1.0},cmap='Spectral', figsize=(15,15),
              background='k', edgecolor='w', bordercolor='gray',
              ylabel='Cortical thickness (mm)', title='Title of the figure')

Set colorbar limits

Thank you so much for that great module!
I wondered if it is possible to set colorbar limits as well? As I would like to make several plots with the same colorbar range.

Thank you very much in advance!

Erroneus labels in ggseg.plot_aseg()

Good evening,

Thanks again for the great plotting tool ! I think I also found some bugs in ggseg.plot_aseg().

Shouldn't the brain stem, cerebellum white matter and cerebellum gray matter appear in light gray when not specified in the dictionary ? In the example plot for instance, the 3 structures appear in some very dark gray. Might be related to the fact that they are also labeled with this same dark color when specifying a value for them in the dictionary ? Here is the plot I get when I set them to 1, 2, and 3 respectively:

image

Otherwise, labelling the Left-Caudate creates a figure with the Right-Caudate:

image

and labelling the Right-Caudate creates a figure with the Left-Caudate:

image

It's a small detail, but you probably want the input regions to map to the correct structures in the plot ;)

Cheers !
David

PS: here's some code in case you want to replicate the plots:

import ggseg
ggseg.plot_aseg({'Brain-Stem': 1, 'Cerebellum-Cortex': 2, 'Cerebellum-White-Matter': 3.0}, cmap='Spectral', background='k', edgecolor='w', bordercolor='gray', ylabel='Volume (mm3)', title='Title of the figure')
ggseg.plot_aseg({'Left-Caudate': 1.0}, cmap='Spectral', background='k', edgecolor='w', bordercolor='gray', ylabel='Volume (mm3)', title='Title of the figure')
ggseg.plot_aseg({'Right-Caudate': 1.0}, cmap='Spectral', background='k', edgecolor='w', bordercolor='gray', ylabel='Volume (mm3)', title='Title of the figure')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.