# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import sklearn.datasets as data
# %matplotlib inline
sns.set_context('poster')
sns.set_style('white')
sns.set_color_codes()
plot_kwds = {'alpha' : 0.5, 's' : 80, 'linewidths':0}

import hdbscan

# +
filename = '/home/garner1/wsi-data/npy/clusterable_embedding.merge.primary.ilastik.npy'
test_data = np.load(filename)

idx = np.random.randint(test_data.shape[0], size=100000)
XX = test_data[:,:3]
# -

XX.shape

clusterer = hdbscan.HDBSCAN(min_samples=300, min_cluster_size=5000, gen_min_span_tree=True)
clusterer.fit(XX)

clusterer.condensed_tree_.plot(select_clusters=True, selection_palette=sns.color_palette("Set2",len(clusterer.labels_)))

# +
from plotly.graph_objs import *
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

df = pd.DataFrame(data=test_data,columns=['x','y','z','labels'])#.iloc[idx]

df['labels'] = df['labels'].astype(int).apply(str)

df['cluster'] = clusterer.labels_
df['cluster'] = df['cluster'].apply(str)

#df['alpha'] = clusterer.probabilities_
#df['alpha'] = df['alpha'].apply(str)

df_filtered = df[df.cluster != str(-1)]

fig = px.scatter_3d(df_filtered, x="x", y="y", z="z", color="cluster", hover_name="labels", 
                        color_discrete_sequence=px.colors.qualitative.Set2)
fig.update_traces(marker=dict(size=1,opacity=0.75),selector=dict(mode='markers'))
fig.update_layout(margin=dict(l=0, r=0, b=0, t=0),
                      legend=go.layout.Legend(
                          x=0,
                          y=1,
                          traceorder='normal',
                          font=dict(family='sans-serif',size=20,color='black'),
                          itemsizing='constant' #to show larget legend markers
                      ))

fig.write_html('adeno.primary.html', auto_open=True)
# -

'''
Evaluate the distribution of sample over the clusters
'''
sample_profiles = []
for sample in set(df['labels']):
    #the first value is for the unassigned points
    sample_profiles.append(df[df.labels == sample]['cluster'].value_counts(normalize=False).sort_index().tolist())
print(len(sample_profiles))

profile_df = pd.DataFrame.from_records(sample_profiles).transpose()
labeled_profile_df = profile_df.drop([0]) # remove unlabeled points

labeled_profile_df.shape

labeled_profile_df.columns = list(set(df['labels']))

labeled_profile_df

#for sample in set(df['labels']):
labeled_profile_df.plot(figsize=(20,20), subplots=True,kind='bar', width=1.0, legend=False, 
                             layout=(5, 3), sharex=True, sharey=True)
#plt.tight_layout()

#plt.figure(figsize=(20,20));
labeled_profile_df.plot.bar(stacked=True,figsize=(10,10))



StarcoderdataJupyterScriptsDedupFiltered