Skip to content

Commit

Permalink
Create utils.py
Browse files Browse the repository at this point in the history
"I'm happy to support a pull request" ok
  • Loading branch information
benjaminpope authored Feb 27, 2024
1 parent 4a15434 commit 9af9fc1
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions zodiax/experimental/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import numpy as np
import scipy.cluster.hierarchy as sch
import matplotlib.pyplot as plt

def cov_to_corr(cov):
# Calculate the standard deviations
std = np.sqrt(np.diag(cov))

# Calculate the correlation matrix
corr = cov / np.outer(std, std)

return corr

def group_corr_inds(corr):

# print(corr)
# X = df.corr().values
X = corr
# print(X.shape)
d = sch.distance.pdist(X) # vector of ('55' choose 2) pairwise distances
L = sch.linkage(d, method='complete')
ind = sch.fcluster(L, 0.5*d.max(), 'distance')
return ind

def visualize_covmat(cov,plot_names):
corr = cov_to_corr(cov)
ind = group_corr_inds(corr)

args = np.argsort(ind)
sorted_labels = [plot_names[i] for i in args]
plt.imshow(corr[args, :][:, args], origin="upper")
plt.xticks(range(len(plot_names)), sorted_labels, rotation=45)
plt.yticks(range(len(plot_names)), sorted_labels)
plt.colorbar()

0 comments on commit 9af9fc1

Please sign in to comment.