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

Clean up code to make sure we can recreate figures #12

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions activ/clustering/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,6 @@ def plot_real_foc_results(path, ax=None, max1d_cutoff=False, ci=None, n_sigma=1,
ax = ax or plt.gca()
real_noc, real_foc, real_accuracy, real_chance = read_clustering_results(path)

real_data_est = get_real_noc(np.arange(2, 51), real_foc,
smooth=True, use_median=False,
spread_asm=True, spread_foc=True)


x_noc, lower, med, upper = flatten_summarize(np.arange(2, 51),
real_foc, smooth=True,
Expand All @@ -585,7 +581,7 @@ def plot_real_foc_results(path, ax=None, max1d_cutoff=False, ci=None, n_sigma=1,
noc_idx = get_noc_max1d_smooth(np.arange(2, 51), real_foc)
else:
noc_idx = get_noc(np.arange(2, 51), real_foc, plot_fit=True, plot_asm=False, fit_summary=True,
ttest_cutoff=False, iqr=False, ax=ax, use_median=False, spread_asm=True, spread_foc=True,
ttest_cutoff=False, iqr=True, ax=ax, use_median=False, spread_asm=True, spread_foc=True,
ci=ci, n_sigma=n_sigma)

est = np.arange(2, 51)[noc_idx]
Expand Down Expand Up @@ -668,7 +664,7 @@ def entropy_across_clusterings(variable, labels):
return np.array(ret)


def plot_entropy_across_clusters(features, emb, ax=None, cmap='Greys'):
def plot_entropy_across_clusters(features, emb, ax=None, cmap='Greys', fontsize='x-large'):
"""
Plot average feature entropy as a function of the number of clusters
for every feature in *features*
Expand Down Expand Up @@ -698,20 +694,20 @@ def plot_entropy_across_clusters(features, emb, ax=None, cmap='Greys'):
order = np.argsort(init)[::-1]
for var_i in order:
ax.plot(x, y[var_i]/init[var_i], c=cmap(init[var_i]))
ax.set_ylabel("Percent of initial entropy", fontsize='x-large')
ax.set_xlabel("Number of clusters", fontsize='x-large')
ax.set_ylabel("Percent of initial entropy", fontsize=fontsize)
ax.set_xlabel("Number of clusters", fontsize=fontsize)

yticks = np.array([0.2, 0.4, 0.6, 0.8, 1.0])

ax.set_yticks(yticks)
ax.set_yticklabels((yticks*100).astype(int), fontsize='x-large')
ax.set_yticklabels((yticks*100).astype(int), fontsize=fontsize)

xticks = np.arange(6)*10
ax.set_xticks(xticks)
ax.set_xticklabels(xticks, fontsize='x-large')
ax.set_xticklabels(xticks, fontsize=fontsize)

cbar = ax.figure.colorbar(cm.ScalarMappable(norm=mpc.Normalize(vmin=np.min(init), vmax=np.max(init)), cmap=cmap), ax=ax)
cbar.set_label("Initial entropy", rotation=-90, va="bottom", fontsize='x-large')
cbar.set_label("Initial entropy", rotation=-90, va="bottom", fontsize=fontsize)
cbar_yticks = [1, 2, 3, 4]

cbar.ax.set_yticklabels(cbar_yticks, fontsize='x-large')
cbar.ax.set_yticklabels(cbar_yticks, fontsize=fontsize)
7 changes: 7 additions & 0 deletions activ/nmf/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@ def plot_weights(weights, colors=None, factor_order=None, ax=None, labels=None,
ax_bar.spines['right'].set_visible(False)
ax_bar.spines['top'].set_visible(False)
ax_bar.set_ylabel('Fraction of\npatients', fontsize=fontsize)

yticks = np.array([100, 200])
ax_bar2 = ax_bar.twinx()
ax_bar2.set_yticks(yticks / weights.shape[0], labels=yticks, fontsize=labelsize)
ax_bar2.set_ylabel('Number of\npatients', fontsize=fontsize)
ax_bar2.spines['top'].set_visible(False)
ax_bar.spines['left'].set_visible(False)
ax = ax[1]
else:
ax = ax[0]
Expand Down
77 changes: 77 additions & 0 deletions activ/paper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Ellipse
import matplotlib.transforms as transforms


def draw_ellipse(x, y, width, height, ax=None, color='blue', linewidth=1, alpha=0.1):

#ax = ax or plt.gca()
face = Ellipse((x, y),
width=width,
height=height,
facecolor=color,
angle=45,
alpha=alpha)
edge = Ellipse((x, y),
width=width,
height=height,
angle=45,
edgecolor=color,
facecolor='none',
linewidth=linewidth)
ax.add_patch(face)
ax.add_patch(edge)


def get_cuts(left, right, num_circles):
cuts = np.linspace(left, right, num_circles*2 + 1)
center_idx = np.arange(num_circles*2 + 1)[1:-1:2]
return cuts, center_idx


def add_circles(left, right, num_circles, color, num_levels=1, current_level=0, minor_d_frac=1.0, ax=None):
if current_level >= num_levels:
return
#ax = ax or plt.gca()
cuts, center_idx = get_cuts(left, right, num_circles)
line_length = right-left
d = np.sqrt(2) * line_length / num_circles
for i, c_i in enumerate(center_idx):
center = cuts[c_i]
col = color
if current_level == 0 and isinstance(color, (list, np.ndarray)):
col = color[i]
draw_ellipse(center, center, d, d*minor_d_frac, color=col, ax=ax, linewidth=0.5)
add_circles(cuts[c_i-1], cuts[c_i+1], 2, col, num_levels, current_level+1, minor_d_frac=minor_d_frac, ax=ax)


def plot_cat2cont(ax=None, fontsize='x-large', marker_size=None):
#ax = ax or plt.gca()

major_d = 2
minor_d = major_d/4

ax.set_xlim(0, major_d)
ax.set_ylim(0, major_d)

ellipse_l = (major_d-np.sqrt(2))/2
ellipse_r = major_d - ellipse_l

draw_ellipse(1, 1, major_d*1.1, minor_d, color='black', linewidth=0.5, alpha=0.3, ax=ax)

colors = ['blue', 'green', 'red']
add_circles(ellipse_l, ellipse_r, 3, colors, 3, minor_d_frac=0.75, ax=ax)
ax.set_xticks([])
ax.set_xticklabels([])
ax.set_yticks([])
ax.set_yticklabels([])

p_pos = 0.8
p_lw = 4
ax.axhline(p_pos, xmax=p_pos/major_d, linewidth=p_lw, c='k')
ax.axvline(p_pos, ymax=p_pos/major_d, linewidth=p_lw, c='k')
ax.scatter([p_pos], [p_pos], marker='*', color='yellow', edgecolor='black', s=marker_size, alpha=1.0, zorder=10, label="Individual patient")
ax.legend(loc='upper left', fontsize=fontsize, frameon=False, borderaxespad=0.1)
ax.set_xlabel('Intake features', fontsize=fontsize)
ax.set_ylabel('Outcomes', fontsize=fontsize)
2 changes: 1 addition & 1 deletion activ/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def get_nmf_labels():
# ])

bm_bases_labels = np.array([
'Sociomedical factors',
'Other sociomedical\nfactors',
'PEMC',
'PEMHC',
'Intracranial injury',
Expand Down
595 changes: 91 additions & 504 deletions notebooks/CCA-Figures.ipynb

Large diffs are not rendered by default.

1,692 changes: 585 additions & 1,107 deletions notebooks/CV_BIC.ipynb

Large diffs are not rendered by default.

495 changes: 348 additions & 147 deletions notebooks/ClusteringFigure.ipynb

Large diffs are not rendered by default.

586 changes: 277 additions & 309 deletions notebooks/LatentFeaturesFigures.ipynb

Large diffs are not rendered by default.

49 changes: 31 additions & 18 deletions notebooks/Summary.ipynb

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ h5py
matplotlib
seaborn
umap-learn
hdbscan
pandas
latex
git+https://github.com/ajtritt/concavity
git+https://github.com/BouchardLab/concavity