Skip to content

Commit 9add3e7

Browse files
authored
Update examples and add BOLD signal example (#445)
* Doc: Increase width of the body * Maint: update examples * Test examples with python 3.8 * Doc: Add BOLD signal example * Doc: use 100dvw instead of 1000px max width * Add carpet_plot to BOLD example
1 parent 3978712 commit 9add3e7

File tree

2 files changed

+120
-4
lines changed

2 files changed

+120
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright 2018-2021
2+
# Institute of Neuroscience and Medicine (INM-1), Forschungszentrum Jülich GmbH
3+
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
"""
17+
Parcellation-based functional data
18+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19+
20+
`siibra` provides access to parcellation-averaged functional data such as
21+
blood-oxygen-level-dependent (BOLD) signals.
22+
"""
23+
24+
# %%
25+
import siibra
26+
# sphinx_gallery_thumbnail_number = 1
27+
28+
# %%
29+
# We start by selecting an atlas parcellation.
30+
jubrain = siibra.parcellations.get("julich 2.9")
31+
32+
# %%
33+
# The matrices are queried as expected, using `siibra.features.get`,
34+
# passing the parcellation as a concept.
35+
# Here, we query for structural connectivity matrices.
36+
features = siibra.features.get(jubrain, siibra.features.functional.RegionalBOLD)
37+
print(f"Found {len(features)} parcellation-based BOLD signals for {jubrain}.")
38+
39+
# %%
40+
# We fetch the first result, which is a specific `RegionalBOLD` object.
41+
bold = features[0]
42+
print(f"RegionalBOLD features reflects {bold.modality} of {bold.cohort} cohort.")
43+
print(bold.name)
44+
print("\n" + bold.description)
45+
46+
# Subjects are encoded via anonymized ids:
47+
print(bold.subjects)
48+
49+
50+
# %%
51+
# The parcellation-based functional data are provided as pandas DataFrames
52+
# with region objects as columns and indices as time step.
53+
subject = bold.subjects[0]
54+
table = bold.get_table(subject)
55+
print(f"Timestep: {bold.timestep}")
56+
table[jubrain.get_region("hOc3v left")]
57+
58+
# %%
59+
# We can visualize the signal strength per region by time via a carpet plot.
60+
# In fact, `plot_carpet` method can take a list of regions to display the
61+
# data for selected regions only.
62+
selected_regions = [
63+
'SF (Amygdala) left', 'SF (Amygdala) right', 'Area Ph2 (PhG) left',
64+
'Area Ph2 (PhG) right', 'Area Fo4 (OFC) left', 'Area Fo4 (OFC) right',
65+
'Area 7A (SPL) left', 'Area 7A (SPL) right', 'CA1 (Hippocampus) left',
66+
'CA1 (Hippocampus) right', 'CA1 (Hippocampus) left', 'CA1 (Hippocampus) right'
67+
]
68+
bold.plot_carpet(subject=bold.subjects[0], regions=selected_regions)
69+
# %%
70+
# Alternatively, we can visualize the mean signal strength per region:
71+
bold.plot(subject=bold.subjects[0], regions=selected_regions)

siibra/features/tabular/regional_timeseries_activity.py

+49-4
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
from .. import anchor as _anchor
1919

2020
from ...commons import logger, QUIET, siibra_tqdm
21-
from ...core import region as _region
2221
from ...locations import pointset
2322
from ...retrieval.repositories import RepositoryConnector
2423

25-
from typing import Callable, Dict, Union
24+
from typing import Callable, Dict, List
2625
import pandas as pd
2726
import numpy as np
2827

@@ -196,11 +195,57 @@ def _array_to_dataframe(self, array: np.ndarray) -> pd.DataFrame:
196195
df = df.rename(columns=remapper)
197196
return df
198197

199-
def plot(self, subject: str = None, *args, backend="matplotlib", **kwargs):
200-
table = self.get_table(subject)
198+
def plot(
199+
self, subject: str = None, regions: List[str] = None, *args,
200+
backend="matplotlib", **kwargs
201+
):
202+
"""
203+
Create a bar plot of averaged timeseries data per region.
204+
205+
Parameters
206+
----------
207+
regions: str, Region
208+
subject: str, default: None
209+
If None, returns the subject averaged table.
210+
args and kwargs:
211+
takes arguments and keyword arguments for the desired plotting
212+
backend.
213+
"""
214+
if regions is None:
215+
regions = self.regions
216+
indices = [self.regions.index(r) for r in regions]
217+
table = self.get_table(subject).iloc[:, indices]
201218
table.columns = [str(r) for r in table.columns]
202219
return table.mean().plot(kind="bar", *args, backend=backend, **kwargs)
203220

221+
def plot_carpet(
222+
self, subject: str = None, regions: List[str] = None, *args,
223+
backend="plotly", **kwargs
224+
):
225+
"""
226+
Create a carpet plot ofthe timeseries data per region.
227+
228+
Parameters
229+
----------
230+
regions: str, Region
231+
subject: str, default: None
232+
If None, returns the subject averaged table.
233+
args and kwargs:
234+
takes arguments and keyword arguments for `plotly.express.imshow`
235+
"""
236+
if backend != "plotly":
237+
raise NotImplementedError("Currently, carpet plot is only implemented with `plotly`.")
238+
if regions is None:
239+
regions = self.regions
240+
indices = [self.regions.index(r) for r in regions]
241+
table = self.get_table(subject).iloc[:, indices]
242+
table.columns = [str(r) for r in table.columns]
243+
from plotly.express import imshow
244+
return imshow(
245+
table.T,
246+
title=f"{self.modality}" + f" for subject={subject}" if subject else ""
247+
)
248+
204249

205250
class RegionalBOLD(
206251
RegionalTimeseriesActivity,

0 commit comments

Comments
 (0)