-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from earmingol/new_version
Updates for v0.6.2
- Loading branch information
Showing
9 changed files
with
263 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,4 @@ | |
from cell2cell import tensor | ||
from cell2cell import utils | ||
|
||
__version__ = "0.6.1" | ||
__version__ = "0.6.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from scipy.signal import savgol_filter | ||
|
||
|
||
def smooth_curve(values, window_length=None, polyorder=3, **kwargs): | ||
'''Apply a Savitzky-Golay filter to an array to smooth the curve. | ||
Parameters | ||
---------- | ||
values : array-like | ||
An array or list of values. | ||
window_length : int, default=None | ||
Size of the window of values to use too smooth the curve. | ||
polyorder : int, default=3 | ||
The order of the polynomial used to fit the samples. | ||
**kwargs : dict | ||
Extra arguments for the scipy.signal.savgol_filter function. | ||
Returns | ||
------- | ||
smooth_values : array-like | ||
An array or list of values representing the smooth curvee. | ||
''' | ||
size = len(values) | ||
if window_length is None: | ||
window_length = int(size / min([2, size])) | ||
if window_length % 2 == 0: | ||
window_length += 1 | ||
assert(polyorder < window_length), "polyorder must be less than window_length." | ||
smooth_values = savgol_filter(values, window_length, polyorder, **kwargs) | ||
return smooth_values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
||
from itertools import combinations | ||
|
||
# Authors: Hratch Baghdassarian <[email protected]>, Erick Armingol <[email protected]> | ||
# similarity metrics for tensor decompositions | ||
|
@@ -122,4 +125,55 @@ def _compute_correlation_index(x1, x2, tol=5e-16): | |
score = (1 / (n_elements)) * (np.sum(np.abs(np.max(c_prod_mtx, 1) - 1)) + np.sum(np.abs(np.max(c_prod_mtx, 0) - 1))) | ||
if score < tol: | ||
score = 0 | ||
return score | ||
return score | ||
|
||
|
||
def pairwise_correlation_index(factors, tol=5e-16, method='stacked'): | ||
''' | ||
Computes the CorrIndex between all pairs of factors | ||
Parameters | ||
---------- | ||
factors : list | ||
List with multiple Ordered dictionaries, each containing a dataframe with | ||
the factor loadings for each dimension/order of the tensor. This is the | ||
result from a tensor decomposition, it can be found as the attribute | ||
`factors` in any tensor class derived from the class BaseTensor | ||
(e.g. BaseTensor.factors). | ||
tol : float, default=5e-16 | ||
Precision threshold below which to call the CorrIndex score 0. | ||
method : str, default='stacked' | ||
Method to obtain the CorrIndex by comparing the A matrices from two decompositions. | ||
Possible options are: | ||
- 'stacked' : The original method implemented in [1]. Here all A matrices from the same decomposition are | ||
vertically concatenated, building a big A matrix for each decomposition. | ||
- 'max_score' : This computes the CorrIndex for each pair of A matrices (i.e. between A_1 in factors_1 and | ||
factors_2, between A_2 in factors_1 and factors_2, and so on). Then the max score is | ||
selected (the most conservative approach). In other words, it selects the max score among the | ||
CorrIndexes computed dimension-wise. | ||
- 'min_score' : Similar to 'max_score', but the min score is selected (the least conservative approach). | ||
- 'avg_score' : Similar to 'max_score', but the avg score is selected. | ||
Returns | ||
------- | ||
scores : pd.DataFrame | ||
Dataframe with CorrIndex metric for each pair of decompositions. | ||
This metric bounds are [0,1]; lower score indicates higher similarity between matrices | ||
''' | ||
N = len(factors) | ||
idxs = list(range(N)) | ||
pairs = list(combinations(idxs, 2)) | ||
scores = pd.DataFrame(np.zeros((N, N)),index=idxs, columns=idxs) | ||
for p1, p2 in pairs: | ||
corrindex = correlation_index(factors_1=factors[p1], | ||
factors_2=factors[p2], | ||
tol=tol, | ||
method=method | ||
) | ||
|
||
scores.at[p1, p2] = corrindex | ||
scores.at[p2, p1] = corrindex | ||
return scores |
Oops, something went wrong.