Skip to content

Commit 2222caf

Browse files
authored
Merge pull request neuroquery#1 from jeromedockes/update_urls
misc updates
2 parents 3587b62 + dc712c5 commit 2222caf

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ projection on the [DiFuMo atlas](https://parietal-inria.github.io/DiFuMo/).
1313
Unlike NeuroQuery, NeuroQuery Image Search isn't hosted anywhere online. However
1414
we provide a [Voilà](https://github.com/voila-dashboards/voila) dashboard that
1515
can be run for example on `mybinder` by following [this
16-
link](https://mybinder.org/v2/gh/neuroquery/neuroquery_apps/master?urlpath=%2Fvoila%2Frender%2Fimage_search.py),
16+
link](https://mybinder.org/v2/gh/neuroquery/neuroquery_apps/main?urlpath=%2Fvoila%2Frender%2Fimage_search.py),
1717
or locally by cloning [this
1818
repository](https://github.com/neuroquery/neuroquery_apps).
1919

@@ -58,6 +58,8 @@ optional arguments:
5858
--transform {absolute_value,identity,positive_part}
5959
Transform to apply to the image. As NeuroQuery ignores the direction of activations by default the absolute value of the
6060
input map is compared to activation patterns in the literature. (default: absolute_value)
61+
--no_rescaling Disable rescaling the similarities. By default they
62+
are mapped to the [0, 1] range. (default: False)
6163
```
6264

6365
## As a Python package

src/neuroquery_image_search/_searching.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ def default(self, obj):
119119
return json.JSONEncoder.default(self, obj)
120120

121121

122+
def _rescale_similarities(similarities):
123+
if (similarities == 0).all():
124+
return similarities
125+
similarities = similarities - similarities.min()
126+
similarities = similarities / similarities.max()
127+
return similarities
128+
129+
122130
class NeuroQueryImageSearch:
123131
"""Search for studies and terms with activation maps similar to an image.
124132
@@ -133,11 +141,17 @@ class NeuroQueryImageSearch:
133141
"image" (`nibabel.Nifti1Image` containing the input image).
134142
135143
"""
144+
136145
def __init__(self):
137146
self.data = fetch_data()
138147

139148
def __call__(
140-
self, query_img, n_studies=50, n_terms=20, transform="absolute_value"
149+
self,
150+
query_img,
151+
n_studies=50,
152+
n_terms=20,
153+
transform="absolute_value",
154+
rescale_similarities=True,
141155
):
142156
"""Search for studies and terms with activation maps similar to an image
143157
@@ -156,6 +170,9 @@ def __call__(
156170
helpful when comparing to the absolute value of the input image.
157171
"absolute_value" is the default.
158172
173+
rescale_similarities : if `True` (the default), similarities are
174+
rescaled to span the range [0, 1]
175+
159176
Returns
160177
-------
161178
results : dictionary with keys "image", "studies", "terms".
@@ -189,8 +206,9 @@ def __call__(
189206

190207
similarities = self.data["studies_loadings"].dot(query)
191208
most_similar = np.argsort(similarities)[::-1][:n_studies]
192-
if (similarities > 0).any():
193-
similarities /= similarities.max()
209+
210+
if rescale_similarities:
211+
similarities = _rescale_similarities(similarities)
194212
study_results = (
195213
self.data["studies_info"]
196214
.iloc[most_similar]
@@ -204,8 +222,8 @@ def __call__(
204222
1 + self.data["document_frequencies"]["document_frequency"].values
205223
)
206224
most_similar = np.argsort(similarities)[::-1][:n_terms]
207-
if (similarities > 0).any():
208-
similarities /= similarities.max()
225+
if rescale_similarities:
226+
similarities = _rescale_similarities(similarities)
209227
term_results = (
210228
self.data["document_frequencies"]
211229
.iloc[most_similar]
@@ -262,6 +280,12 @@ def _get_parser():
262280
"direction of activations by default the absolute value of the "
263281
"input map is compared to activation patterns in the literature.",
264282
)
283+
parser.add_argument(
284+
"--no_rescaling",
285+
action="store_true",
286+
help="Disable rescaling the similarities. "
287+
"By default they are mapped to the [0, 1] range.",
288+
)
265289
return parser
266290

267291

@@ -281,6 +305,7 @@ def image_search(args=None):
281305
n_studies=args.n_studies,
282306
n_terms=args.n_terms,
283307
transform=args.transform,
308+
rescale_similarities=(not args.no_rescaling)
284309
)
285310
if args.output is None:
286311
results_to_html(results, image_name).open_in_browser()

tests/test_searching.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,14 @@ def test_json_encoder():
4747

4848
def test_neuroquery_image_search(fake_img):
4949
search = _searching.NeuroQueryImageSearch()
50-
results = search(fake_img, 20, transform="identity")["studies"]
50+
51+
results = search(fake_img, 20, transform="identity", rescale_similarities=False)
52+
assert results["studies"]["similarity"].max() != pytest.approx(1.)
53+
54+
results = search(fake_img, 20, transform="identity")
55+
assert results["terms"]["similarity"].min() == pytest.approx(0.)
56+
assert results["studies"]["similarity"].max() == pytest.approx(1.)
57+
results = results["studies"]
5158
neg_img = image.new_img_like(fake_img, image.get_data(fake_img) * -1.0)
5259
neg_results = search(neg_img, 20, transform="identity")["studies"]
5360
assert (neg_results["pmid"].values == results["pmid"].values[::-1]).all()

0 commit comments

Comments
 (0)