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
|
# Authors: Yousra Bekhti <yousra.bekhti@gmail.com>
# Mark Wronkiewicz <wronk@uw.edu>
#
# License: BSD (3-clause)
import numpy as np
from scipy.linalg import norm
# TODO: Add more localization accuracy functions. For example, distance between
# true dipole position (in simulated stc) and the centroid of the
# estimated activity.
def _check_stc(stc1, stc2):
"""Check that stcs are compatible."""
if stc1.data.shape != stc2.data.shape:
raise ValueError('Data in stcs must have the same size')
if np.all(stc1.times != stc2.times):
raise ValueError('Times of two stcs must match.')
def source_estimate_quantification(stc1, stc2, metric='rms'):
"""Calculate matrix similarities.
Parameters
----------
stc1 : SourceEstimate
First source estimate for comparison.
stc2 : SourceEstimate
Second source estimate for comparison.
metric : str
Metric to calculate, 'rms' or 'cosine'.
Returns
-------
score : float | array
Calculated metric.
Notes
-----
Metric calculation has multiple options:
* rms: Root mean square of difference between stc data matrices.
* cosine: Normalized correlation of all elements in stc data matrices.
.. versionadded:: 0.10.0
"""
known_metrics = ['rms', 'cosine']
if metric not in known_metrics:
raise ValueError('metric must be a str from the known metrics: '
'"rms" or "cosine"')
# This is checking that the datas are having the same size meaning
# no comparison between distributed and sparse can be done so far.
_check_stc(stc1, stc2)
data1, data2 = stc1.data, stc2.data
# Calculate root mean square difference between two matrices
if metric == 'rms':
score = np.sqrt(np.mean((data1 - data2) ** 2))
# Calculate correlation coefficient between matrix elements
elif metric == 'cosine':
score = 1. - (np.dot(data1.flatten(), data2.flatten()) /
(norm(data1) * norm(data2)))
return score
|