-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathensemble_model_demo.py
72 lines (60 loc) · 2.23 KB
/
ensemble_model_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# ---
# jupyter:
# jupytext:
# cell_metadata_filter: -all
# formats: ipynb,py
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.3.2
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# Encoding with NeuroQuery using an ensemble model
# ================================================
#
# The model used here uses the random subspaces bagging method:
# it consists of 30 NeuroQuery models trained on randomly subsampled
# vocabularies and averaged.
# The output maps are predicted densities hence their scale does
# not have a particular meaning.
# See also: [the neuroquery website](https://neuroquery.org).
# ## Encode a query into a statistical map of the brain
from neuroquery import fetch_neuroquery_model
from neuroquery.encoding import SimpleEncoder
from neuroquery.tokenization import get_html_highlighted_text
from nilearn.plotting import plot_img, view_img
import ipywidgets as widgets
from IPython.display import display, display_html, Markdown
import utils
EXAMPLE_QUERY = (
"Theory of mind is the ability to attribute mental states — beliefs, "
"intents, desires, emotions, knowledge, etc. — to oneself, and to others, "
"and to understand that others have beliefs, desires, intentions, and "
"perspectives that are different from one's own. (from wikipedia)")
# %%capture
encoder = SimpleEncoder.from_data_dir(
fetch_neuroquery_model(model_name="ensemble_model_2020-02-12"))
query = widgets.Textarea(value=EXAMPLE_QUERY)
button = widgets.Button(description="Run query")
display(widgets.HBox([query, button]))
output = widgets.Output()
display(output)
def run_and_display_query(_):
result = encoder(query.value)
with output:
output.clear_output()
display_html(
get_html_highlighted_text(result["highlighted_text"]), raw=True)
display_html(
view_img(result["brain_map"], threshold="97%",
colorbar=False).get_iframe(),
raw=True)
display_html(
utils.download_img_link(result["brain_map"], query.value),
raw=True)
button.on_click(run_and_display_query)
run_and_display_query(None)