Skip to content

Commit

Permalink
Added documentation to similarity.py
Browse files Browse the repository at this point in the history
  • Loading branch information
yingtluo committed Dec 12, 2015
1 parent 3613059 commit 683cad2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
42 changes: 42 additions & 0 deletions code/stat159lambda/reproduction/similarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@


def pearson_r(X, Y):
"""
Calculates the correlation between every row of two matrices. Assumes the
two matrices given are the same shape.
Parameters
----------
X : array representation of an (n x n) matrix
Y : array representation of an (n x n) matrix
Returns
-------
r : vector of length n, where each element is the correlation of rows X_n
and Y_n
"""
X_centered = X - np.mean(X, axis=1)[:, np.newaxis]
Y_centered = Y - np.mean(Y, axis=1)[:, np.newaxis]
return inner1d(X_centered, Y_centered) / (np.linalg.norm(X_centered,
Expand All @@ -22,6 +36,19 @@ def pearson_r(X, Y):


def correlation(subj_a_data, subj_b_data):
"""
Calculates the averaged correlation using every pair of data points between two
subjects.
Parameters
----------
subj_a_data : array
subj_b_data : array
Returns
-------
correlations : float
"""
run_split_a_data = np.split(subj_a_data, RUN_DIVISIONS[:-1], axis=1)
run_split_b_data = np.split(subj_b_data, RUN_DIVISIONS[:-1], axis=1)
correlations = np.zeros(NUM_VOXELS)
Expand All @@ -32,6 +59,21 @@ def correlation(subj_a_data, subj_b_data):


def calculate_and_save_correlation(subj_1_num, subj_2_num):
"""
Calculates correlation using smoothed 2-D data with 8 full width half
maximum mm, and saves values into a designated correlation_path. If a file
with calculated correlations already exists, uses that cached version
instead.
Parameters
----------
subj_1_num : int
subj_2_num : int
Returns
-------
None
"""
correlation_path = dp.get_correlation_path(subj_1_num, subj_2_num)
if not exists(correlation_path) or not USE_CACHED_DATA:
subj_1_data = np.load(dp.get_smoothed_2d_path(subj_1_num, 8))
Expand Down
2 changes: 1 addition & 1 deletion code/stat159lambda/utils/data_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_concatenated_path(subj_num):
def get_smoothed_path(subj_num, fwhm_mm):
"""
Derives the absolute path to the smoothed data for a particular subject and
particular smoothing and particular full width half maximum smoothed version
particular full width half maximum smoothed version
Parameters
----------
Expand Down

0 comments on commit 683cad2

Please sign in to comment.