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

Unable to compute the cosine with stats.compare_images #103

Closed
2 of 4 tasks
me-pic opened this issue Apr 18, 2023 · 2 comments
Closed
2 of 4 tasks

Unable to compute the cosine with stats.compare_images #103

me-pic opened this issue Apr 18, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@me-pic
Copy link

me-pic commented Apr 18, 2023

Issue summary

Error encountered while trying to use the cosine to compute stats.compare_images.

Detailed issue description

Hello Neuromaps team,

I encountered an error while I was trying to reproduce the tutorial Comparing brain maps specifying cosine as the chosen metric (see also steps below).

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[16], line 1
----> 1 cossim = 1 - compare_images(ns_res, aba_res, metric=cosine)

File ~/Documents/master_analysis/brainmaps/lib/python3.9/site-packages/neuromaps/stats.py:61, in compare_images(src, trg, metric, ignore_zero, nulls, nan_policy)
     59     else:
     60         if not isinstance(metric([1, 1], [1, 1]), float):
---> 61             raise ValueError('Provided callable `metric` must accept two '
     62                              'inputs and return single value.')
     64 srcdata, trgdata = load_data(src), load_data(trg)
     66 mask = np.zeros(len(srcdata), dtype=bool)

ValueError: Provided callable `metric` must accept two inputs and return single value.

The problem seems to be with the verification of the variable method in stats.py (L60). Indeed, if I run cosine([1, 1], [1, 1]) I obtain 0 (int), which lead to not isinstance(cosine([1, 1], [1, 1]), float) equally True, thus raising the ValueError. For the moment I just bypass the problem by changing L60 for if not isinstance(metric([1, 2], [1, 1]), float):.

Don't know if this info is relevant, but I'm using scipy==1.10.1

Thank you for your help and for this really cool tool !

Steps to reproduce issue

>>> from neuromaps.datasets import fetch_annotation
>>> neurosynth = fetch_annotation(source='neurosynth')
>>> abagen = fetch_annotation(source='abagen')
>>> from neuromaps.resampling import resample_images
>>> ns_res, aba_res = resample_images(src=neurosynth, trg=abagen,
...                                   src_space='MNI152', trg_space='fsaverage',
...                                   method='linear', resampling='downsample_only')
>>> from scipy.spatial.distance import cosine
>>> cossim = 1 - compare_images(ns_res, aba_res, metric=cosine)

Software version

0.0.3

What operating system were you using when you encountered this issue?

  • macOS
  • Windows
  • Linux

Code of Conduct

  • I agree to follow the neuromaps Code of Conduct
@me-pic me-pic added the bug Something isn't working label Apr 18, 2023
@zhangjb35
Copy link

It is simple. Just change one number. change stats.py:

isinstance(metric([1, 1], [0, 1]), float)

@justinehansen
Copy link
Contributor

Sorry it took me so long to get to this 🥲
The issue is that neuromaps.stats.compare_images is double checking that the metric you use (if it isn't pearsonr or spearmanr) does accept two array inputs and returns a single float. To test this we use two dummy arrays which are [1, 1] and [1, 1]. This issue surfaces because cosine([1, 1], [1, 1]) is 0 (int) so it fails the condition. So as a fix I am changing it to double check that the output of the metric is a scalar (instead of a float). (The proposed solution above would also work.)

(scipy.spatial.distance.cosine should probably be fixed such that the output of cosine([1, 1], [1, 1]) is 0.0 instead of 0, to be consistent).

Hope this is helpful even after many months.

Justine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants