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

Develop #20

Merged
merged 4 commits into from
Aug 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/eeg_meg/meg_inverse_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

# Define a brain object and add the data to the mesh :
b_obj = BrainObj('inflated', translucent=False, hemisphere='left')
b_obj.add_activation(data=data, vertices=vertices, smoothing_steps=5,
b_obj.add_activation(data=data, vertices=vertices, smoothing_steps=15,
clim=(13., 22.), hide_under=13., cmap='plasma',
hemisphere='left')

Expand Down
59 changes: 42 additions & 17 deletions visbrain/objects/brain_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,17 @@ def add_activation(self, data=None, vertices=None, smoothing_steps=5,
data : array_like | None
Vector array of data of shape (n_data,).
vertices : array_like | None
Vector array of vertices of shape (n_vtx). Must be an array of
integers.
Vector array of vertex indices of shape (n_vtx).
Must be an array of integers. If hemisphere is 'left' or 'right'
indexation is done with respect to the specified hemisphere.
smoothing_steps : int | 20
Number of smoothing steps (smoothing is used if n_data < n_vtx)
Number of smoothing steps (smoothing is used if n_data < n_vtx).
If None or 0, no smoothing is performed.
file : string | None
Full path to the overlay file.
hemisphrere : {None, 'both', 'left', 'right'}
The hemisphere to use to add the overlay. If None, the method try
to inferred the hemisphere from the file name.
The hemisphere to use to add the overlay. If None, the method tries
to infer the hemisphere from the file name.
hide_under : float | None
Hide activations under a certain threshold.
n_contours : int | None
Expand All @@ -332,23 +334,46 @@ def add_activation(self, data=None, vertices=None, smoothing_steps=5,
self._default_cblabel = "Activation"
# ============================= METHOD =============================
if isinstance(data, np.ndarray):
if not isinstance(vertices, np.ndarray):
vertices = np.arange(len(data))
if hemisphere is None and file is None:
logger.info('Using both hemispheres')
hemisphere = 'both'
# Hemisphere :
_, activ_vert = self._hemisphere_from_file(hemisphere, file)
activ_vert_idx = np.where(activ_vert)[0]

is_do_smoothing = True

if vertices is None:
# Data are defined on a dense grid
assert len(activ_vert_idx) == len(data)
vertices = np.arange(len(activ_vert_idx))
is_do_smoothing = False
if smoothing_steps:
logger.warning(
'Data defined on a dense grid; ignore smoothing.')
else:
assert len(vertices) == len(data)

logger.info("Add data to specific vertices.")
assert (data.ndim == 1) and (vertices.ndim == 1)
assert (data.ndim == 1) and (np.asarray(vertices).ndim == 1)
assert smoothing_steps is None or isinstance(smoothing_steps, int)

# Get smoothed vertices // data :
if isinstance(smoothing_steps, int):
if hemisphere != 'both':
# Transform to indexing with respect to the whole brain
vert_whole = activ_vert_idx[vertices]
else:
vert_whole = vertices

if smoothing_steps and is_do_smoothing:
edges = mesh_edges(self.mesh._faces)
sm_mat = smoothing_matrix(vertices, edges, smoothing_steps)
sc = data[sm_mat.col]
rows = sm_mat.row
sm_mat = smoothing_matrix(vert_whole, edges, smoothing_steps)
sc = sm_mat * data # actual data smoothing
if hemisphere != 'both':
sc = sc[activ_vert]
else:
sc = data
rows = vertices
# Hemisphere :
_, hemi_idx = self._hemisphere_from_file(hemisphere, file)
activ_vert = np.where(hemi_idx)[0][rows]
sc = np.zeros_like(sm_data[activ_vert])
sc[vertices] = data
elif isinstance(file, str):
assert os.path.isfile(file)
logger.info("Add overlay to the {} brain template "
Expand Down
4 changes: 4 additions & 0 deletions visbrain/visuals/brain_visual.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ def add_overlay(self, data, vertices=None, to_overlay=None, mask_data=None,
# Check input variables :
if vertices is None:
vertices = np.ones((len(self),), dtype=bool)
if not len(vertices):
logger.warning('Vertices array is empty. Abandoning.')
return

data = np.asarray(data)
to_overlay = self._n_overlay if to_overlay is None else to_overlay
data_lim = (data.min(), data.max())
Expand Down