Skip to content

Conversation

@makkostya
Copy link
Contributor

@agramfort @massich @mathurinm
This PR add the new function to produce random parcellation of the cortex. It grows a number (input parameter) of random not intersecting cortical regions which cover the whole surface.

Here is example of use (you should change annotation file names):

from pyface.qt import QtGui, QtCore
%matplotlib qt

import copy
import mne
from mne.datasets import sample
from surfer import Brain

subject_dir = sample.data_path() + '/subjects'
subject = 'sample'

n_parcel = 150
hemis = [0, 1]

# Parcellation
labels = mne.random_parcellation(subject, n_parcel, hemis, subject_dir, surface='white')

# Labels to annotations
annot_fname_lh = '/user/kmaksyme/home/tmp/lh.test.annot'
mne.label.write_labels_to_annot(labels,annot_fname=annot_fname_lh,hemi='lh',overwrite=True)
annot_fname_rh = '/user/kmaksyme/home/tmp/rh.test.annot'
mne.label.write_labels_to_annot(labels,annot_fname=annot_fname_rh,hemi='rh',overwrite=True)

# Visu
brain = Brain(subjects_dir=subject_dir,subject_id = subject,hemi="both",surf='inflated')
brain.add_annotation(annot_fname_lh, hemi='lh', borders=True)
brain.add_annotation(annot_fname_rh, hemi='rh', borders=True, remove_existing=False)

@codecov
Copy link

codecov bot commented Feb 22, 2018

Codecov Report

Merging #4955 into master will increase coverage by 0.01%.
The diff coverage is 98.16%.

@@            Coverage Diff             @@
##           master    #4955      +/-   ##
==========================================
+ Coverage   87.85%   87.87%   +0.01%     
==========================================
  Files         353      353              
  Lines       62988    63177     +189     
  Branches    10783    10811      +28     
==========================================
+ Hits        55337    55515     +178     
- Misses       4909     4913       +4     
- Partials     2742     2749       +7

mne/label.py Outdated
Name of the subject as in SUBJECTS_DIR.
n_parcel : int
Total number of cortical parcels
hemis : array | int
Copy link
Member

Choose a reason for hiding this comment

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

it should be called hemi and be a string. Following the API of pysurfer:

https://github.com/nipy/PySurfer/blob/master/surfer/viz.py#L301

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used the doc of the function "grow_labels". If need to change it, probably should do it for grow_labels too.

mne/label.py Outdated
"""Random cortex parcellation."""
labels = []
parcel_size = np.floor((len(vertices_[hemis[0]]) +
len(vertices_[hemis[1]])) / n_parcel)
Copy link
Member

Choose a reason for hiding this comment

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

I think

len(vertices_[hemis[0]]) + len(vertices_[hemis[1]])) // n_parcel

is nicer and is the same

mne/label.py Outdated

# prepare parcellation
parc = np.empty(n_vertices, dtype='int32')
parc[:] = -1
Copy link
Member

Choose a reason for hiding this comment

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

mne/label.py Outdated
parc[:] = -1

# initialize active sources
s = 0
Copy link
Member

Choose a reason for hiding this comment

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

line is useless

mne/label.py Outdated
name = 'label_' + str(i)
label_ = Label(vertices, hemi=hemi, name=name, subject=subject)
labels.append(label_)
parc_both += [parc]
Copy link
Member

Choose a reason for hiding this comment

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

is this the same algo as in brainstorm?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I followed the same logic as brainsotm code:

  • define number of vertices per parcel
  • grow parcels till there are no free neighbor vertices or till riches max size
  • merge smallest parcels till rich the needed number

mne/label.py Outdated


def random_parcellation(subject, n_parcel, hemis, subjects_dir=None,
surface='white'):
Copy link
Member

Choose a reason for hiding this comment

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

you should use np.random and have a random_state parameter to make the result reproducible.

git grep random_state

to see how we do it.

@agramfort
Copy link
Member

for the lazy curious is the output of the script shared by @makkostya

screen shot 2018-02-23 at 18 45 14

@larsoner
Copy link
Member

Oh the shame of having to run the example yourself to produce this output. It should be done by CircleCI and sphinx-gallery :)

@agramfort
Copy link
Member

agramfort commented Feb 25, 2018 via email

@makkostya
Copy link
Contributor Author

Ok, I'll change it. And can do PR for "grow_labels" too

@agramfort
Copy link
Member

agramfort commented Feb 25, 2018 via email

@makkostya
Copy link
Contributor Author

hum indeed. I would recommend to use the surfer way and we'll deprecate the hemis param in grow_label in another PR.

grow_label function has parameter hemis which is different from pysurf because we can associate the hemisphere to each source seed. So may be it makes sense to keep it like it is?
But I changed it in random_parcellation.

mne/label.py Outdated
parc = np.full(n_vertices, -1, dtype='int32')

# initialize active sources
rng = check_random_state(random_state)
Copy link
Member

Choose a reason for hiding this comment

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

I would have put the check_random_state outside of the for loop on hemi no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, done.

@agramfort agramfort changed the title [WIP] Add function for random parcellation [MRG+1] Add function for random parcellation Feb 27, 2018
@agramfort
Copy link
Member

LGTM

any other review?

@larsoner
Copy link
Member

LGTM but @makkostya your git / GitHub config appears to be incorrect. I should be able to click on the little icon to the left of your commits to get to your GitHub profile. It might be a matter of adding your git config email address to your GitHub account, not sure. Do you want to fix it before merge?

@makkostya
Copy link
Contributor Author

@larsoner , I added my git config email address to my GitHub account, is it Ok now?

@larsoner larsoner merged commit 8ef95c9 into mne-tools:master Feb 28, 2018
@larsoner
Copy link
Member

Yep all better, thanks @makkostya

britta-wstnr pushed a commit to britta-wstnr/mne-python that referenced this pull request Jul 5, 2018
* Add functions to create random parcellation

* add test for random_parcellation

* add random_parcellation to python_reference

* fixe pep8

* fixe pep8 mne-tools#2

* fixe pep mne-tools#3

* add random_state, small corrections

* fixes random_state

* FIX: Alphabetical
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