Commit 17df1f4e authored by Somnath, Suhas's avatar Somnath, Suhas
Browse files

Using the new formulation of the Cluster and SVD classes

parent 5b24d814
...@@ -25,10 +25,12 @@ ...@@ -25,10 +25,12 @@
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import matplotlib.patches as patches import matplotlib.patches as patches
from IPython.display import display, HTML from IPython.display import display, HTML
import ipywidgets as widgets import ipywidgets as widgets
from sklearn.cluster import KMeans
import pycroscopy as px import pycroscopy as px
# set up notebook to show plots within the notebook # set up notebook to show plots within the notebook
% matplotlib notebook % matplotlib notebook
...@@ -167,18 +169,19 @@ ...@@ -167,18 +169,19 @@
Here, SVD essentially compares every single ronchigram with every other ronchigram to find statistically significant trends in the dataset. Such correlation would be infeasible if the ronchigrams were averaged to bright-field and dark-field scalar values. Here, SVD essentially compares every single ronchigram with every other ronchigram to find statistically significant trends in the dataset. Such correlation would be infeasible if the ronchigrams were averaged to bright-field and dark-field scalar values.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
proc = px.SVD(h5_main, num_comps=256)
# First check if SVD was already computed on this dataset: # First check if SVD was already computed on this dataset:
h5_svd_group = px.hdf_utils.findH5group(h5_main, 'SVD') if proc.duplicate_h5_groups is None:
if len(h5_svd_group) == 0:
print('No prior SVD results found - doing SVD now') print('No prior SVD results found - doing SVD now')
h5_svd_group = px.doSVD(h5_main, num_comps=256) h5_svd_group = proc.compute()
else: else:
print('Taking previous SVD results already present in file') print('Taking previous SVD results already present in file')
h5_svd_group = h5_svd_group[-1] h5_svd_group = proc.duplicate_h5_groups[-1]
h5_u = h5_svd_group['U'] h5_u = h5_svd_group['U']
h5_v = h5_svd_group['V'] h5_v = h5_svd_group['V']
h5_s = h5_svd_group['S'] h5_s = h5_svd_group['S']
num_comps = 16 num_comps = 16
...@@ -241,20 +244,22 @@ ...@@ -241,20 +244,22 @@
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Attempt to find any previous computation # Attempt to find any previous computation
h5_kmeans_group = px.hdf_utils.findH5group(h5_u, 'Cluster') num_clusters = 32
if len(h5_kmeans_group) == 0: spectral_components = 128
estimator = KMeans(n_clusters=num_clusters)
proc = px.Cluster(h5_u, estimator, num_comps=spectral_components)
if proc.duplicate_h5_groups is None:
print('No k-Means computation found. Doing K-Means now') print('No k-Means computation found. Doing K-Means now')
num_clusters = 32 h5_kmeans_group = proc.compute()
num_comps_for_clustering = 128
estimator = px.Cluster(h5_u, 'KMeans', num_comps=num_comps_for_clustering, n_clusters=num_clusters)
h5_kmeans_group = estimator.do_cluster()
else: else:
print('Taking existing results of previous K-Means computation') print('Taking existing results of previous K-Means computation')
h5_kmeans_group = h5_kmeans_group[-1] h5_kmeans_group = proc.duplicate_h5_groups[-1]
h5_labels = h5_kmeans_group['Labels'] h5_labels = h5_kmeans_group['Labels']
h5_centroids = h5_kmeans_group['Mean_Response'] h5_centroids = h5_kmeans_group['Mean_Response']
# In case we take existing results, we need to get these parameters # In case we take existing results, we need to get these parameters
...@@ -281,11 +286,11 @@ ...@@ -281,11 +286,11 @@
The vertical length of the branches indicates the relative separation between neighboring clusters. The vertical length of the branches indicates the relative separation between neighboring clusters.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
e_vals = np.reshape(h5_u[:, :num_comps_for_clustering], e_vals = np.reshape(h5_u[:, :spectral_components],
(num_rows, num_cols, -1)) (num_rows, num_cols, -1))
fig = px.plot_utils.plot_cluster_dendrogram(label_mat, e_vals, fig = px.plot_utils.plot_cluster_dendrogram(label_mat, e_vals,
num_comps_for_clustering, num_comps_for_clustering,
num_clusters, num_clusters,
last=num_clusters); last=num_clusters);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment