Commit 3afd6f67 authored by Somnath, Suhas's avatar Somnath, Suhas
Browse files

Merge branch 'cnms_dev' of https://github.com/pycroscopy/pycroscopy into cnms_dev

parents 66447cf0 ea03f082
......@@ -16,7 +16,14 @@
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))
import sys
import mock
MOCK_MODULES = ['numpy', 'scipy', 'matplotlib', 'matplotlib.pyplot',
'scipy.misc', 'scipy.special', 'PyQt4', 'h5py',
'mpl_toolkits.axes_grid1']
for mod_name in MOCK_MODULES:
sys.modules[mod_name] = mock.Mock()
# -- General configuration ------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
......
numpydoc
numpydoc
\ No newline at end of file
from . import utils
from .utils import *
from . import be_sho_model
from .be_sho_model import BESHOmodel
from . import guess_methods
from .guess_methods import GuessMethods
from . import model
from .model import Model
from .utils import *
__all__ = ['GuessMethods', 'Model', 'BESHOmodel', 'utils']
__all__ += utils.__all__
......@@ -51,8 +51,9 @@ class Cluster(object):
self.method_name = method_name
if num_comps is None:
num_comps = self.h5_main.shape[1]
self.num_comps = num_comps
self.num_comps = self.h5_main.shape[1]
else:
self.num_comps = np.min([num_comps, self.h5_main.shape[1]])
self.data_slice = (slice(None), slice(0, num_comps))
# figure out the operation that needs need to be performed to convert to real scalar
......
......@@ -13,7 +13,7 @@ from sklearn.utils import gen_batches
from multiprocessing import cpu_count
from ..io.io_hdf5 import ioHDF5
from ..io.io_utils import getAvailableMem
from ..io.hdf_utils import getH5DsetRefs, copyAttributes, linkRefs
from ..io.hdf_utils import getH5DsetRefs, copyAttributes, linkRefs, findH5group
from ..io.translators.utils import getPositionSlicing, makePositionMat
from ..io.microdata import MicroDataGroup, MicroDataset
......@@ -551,12 +551,11 @@ class ImageWindow(object):
win_name = h5_win.name.split('/')[-1]
try:
pca_name = win_name + '-PCA_000'
win_pca = h5_win.parent[pca_name]
win_svd = findH5group(h5_win, 'SVD')[-1]
h5_S = win_pca['S']
h5_U = win_pca['U']
h5_V = win_pca['V']
h5_S = win_svd['S']
h5_U = win_svd['U']
h5_V = win_svd['V']
except KeyError:
warnstring = 'PCA Results for {dset} were not found in {file}.'.format(dset=win_name, file=self.image_path)
......@@ -621,7 +620,7 @@ class ImageWindow(object):
clean_image[np.isnan(clean_image)] = 0
clean_grp = MicroDataGroup('Cleaned_Image', win_pca.name[1:])
clean_grp = MicroDataGroup('Cleaned_Image', win_svd.name[1:])
ds_clean = MicroDataset('Cleaned_Image', clean_image.flatten())
......
......@@ -96,7 +96,7 @@ if __name__ == '__main__':
fig201, axes201 = plotSpectrograms(np.reshape(h5_V[:plot_comps,:],[-1,num_x,num_x]), num_comps=plot_comps, title='Eigenvectors')
fig201.savefig(os.path.join(folder,pca_name+'_PCA_Eigenvectors.png'), format='png', dpi=300)
plt.close(fig201)
del fig201,axes201
del fig201, axes201
'''
Build a cleaned image from the cleaned windows
......
......@@ -9,12 +9,10 @@ from __future__ import division # int/int = float
from warnings import warn
import os
import h5py
import scipy
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import make_axes_locatable
from scipy.cluster.hierarchy import linkage, dendrogram
from scipy.spatial.distance import pdist
from ..analysis.utils.be_loop import loopFitFunction
......@@ -546,23 +544,38 @@ def plotLoadingMaps(loadings, num_comps=4, stdevs=2, colormap='jet', show_colorb
###############################################################################
def plotKMeansResults(label_mat, cluster_centroids, spec_val=None, cmap=plt.cm.jet,
spec_label='Spectroscopic Value', resp_label='Response'):
def plotClusterResults(label_mat, mean_response, spec_val=None, cmap=plt.cm.jet,
spec_label='Spectroscopic Value', resp_label='Response'):
"""
label_mat : 2D int numpy array or h5py.Dataset object
Spatial map of cluster labels structured as [rows, cols]
cluster_centroids : 2D array or h5py.Dataset object
Centroids arranged as [cluster number, features]
spec_val : (Optional) 1D float numpy array or h5py.Dataset object
X axis to plot the centroids against
If no value is specified, the data is plotted against the index
cmap : plt.cm object (Optional. Default = plt.cm.jet)
Colormap to use for the labels map and the centroid.
Advised to pick a map where the centroid plots show clearly
spec_label : String (Optional. Default = 'Spectroscopic Value')
Label to use for X axis on cluster centroid plot
resp_label : String (Optional. Default = 'Response')
Label to use for Y axis on cluster centroid plot
Plot the cluster labels and mean response for each cluster
Parameters
----------
label_mat : 2D ndarray or h5py.Dataset of ints
Spatial map of cluster labels structured as [rows, cols]
mean_response : 2D ndarray or h5py.Dataset
Mean value of each cluster over all samples
arranged as [cluster number, features]
spec_val : 1D ndarray or h5py.Dataset of floats, optional
X axis to plot the centroids against
If no value is specified, the data is plotted against the index
cmap : plt.cm object or str, optional
Colormap to use for the labels map and the centroid.
Advised to pick a map where the centroid plots show clearly.
Default = matplotlib.pyplot.cm.jet
spec_label : str, optional
Label to use for X axis on cluster centroid plot
Default = 'Spectroscopic Value'
resp_label : str, optional
Label to use for Y axis on cluster centroid plot
Default = 'Response'
Returns
-------
fig : Figure
Figure containing the plots
axes : 1D array_like of axes objects
Axes of the individual plots within `fig`
"""
def __plotCentroids(centroids, ax, spec_val, spec_label, y_label, cmap, title=None):
......@@ -572,32 +585,41 @@ def plotKMeansResults(label_mat, cluster_centroids, spec_val=None, cmap=plt.cm.j
label='Cluster {}'.format(clust),
color=cmap(int(255 * clust / (num_clusters - 1))))
ax.set_ylabel(y_label)
ax.legend(loc='best')
# ax.legend(loc='best')
if title:
ax.set_title(title)
ax.set_xlabel(spec_label)
if type(spec_val) == type(None):
spec_val = np.arange(cluster_centroids.shape[1])
spec_val = np.arange(mean_response.shape[1])
if cluster_centroids.dtype in [np.complex64, np.complex128, np.complex]:
if mean_response.dtype in [np.complex64, np.complex128, np.complex]:
fig = plt.figure(figsize=(12, 8))
ax_map = plt.subplot2grid((2, 10), (0, 0), colspan=6, rowspan=2)
ax_amp = plt.subplot2grid((2, 10), (0, 6), colspan=4)
ax_phase = plt.subplot2grid((2, 10), (1, 6), colspan=4)
ax_map = plt.subplot2grid((2, 12), (0, 0), colspan=6, rowspan=2)
ax_amp = plt.subplot2grid((2, 12), (0, 6), colspan=4)
ax_phase = plt.subplot2grid((2, 12), (1, 6), colspan=4)
axes = [ax_map, ax_amp, ax_phase]
__plotCentroids(np.abs(cluster_centroids), ax_amp, spec_val, spec_label,
resp_label + ' - Amplitude', cmap, 'Centroids')
__plotCentroids(np.angle(cluster_centroids), ax_phase, spec_val, spec_label,
__plotCentroids(np.abs(mean_response), ax_amp, spec_val, spec_label,
resp_label + ' - Amplitude', cmap, 'Mean Response')
__plotCentroids(np.angle(mean_response), ax_phase, spec_val, spec_label,
resp_label + ' - Phase', cmap)
plot_handles, plot_labels = ax_amp.get_legend_handles_labels()
else:
fig, axes = plt.subplots(1, 2, figsize=(12, 6))
ax_map = axes[0]
__plotCentroids(cluster_centroids, axes[1], spec_val, spec_label,
resp_label, cmap, 'Centroids')
fig = plt.figure(figsize=(12, 8))
ax_map = plt.subplot2grid((1, 12), (0, 0), colspan=6)
ax_resp = plt.subplot2grid((1, 12), (0, 6), colspan=4)
axes = [ax_map, ax_resp]
__plotCentroids(mean_response, ax_resp, spec_val, spec_label,
resp_label, cmap, 'Mean Response')
plot_handles, plot_labels = ax_resp.get_legend_handles_labels()
fleg = plt.figlegend(plot_handles, plot_labels, loc='center right',
borderaxespad=0.0)
num_clusters = mean_response.shape[0]
num_clusters = cluster_centroids.shape[0]
if isinstance(label_mat, h5py.Dataset):
"""
Reshape label_mat based on linked positions
......@@ -610,14 +632,12 @@ def plotKMeansResults(label_mat, cluster_centroids, spec_val=None, cmap=plt.cm.j
divider = make_axes_locatable(ax_map)
cax = divider.append_axes("right", size="5%", pad=0.05) # space for colorbar
fig.colorbar(im, cax=cax)
# pcol0 = ax_map.pcolor(label_mat, cmap=cmap)
# fig.colorbar(pcol0, ax=ax_map, ticks=np.arange(num_clusters))
ax_map.axis('tight')
ax_map.set_title('Cluster Label Map')
fig.tight_layout()
fig.suptitle('k-Means result')
fig.canvas.set_window_title('k-Means result')
fig.suptitle('Cluster results')
fig.canvas.set_window_title('Cluster results')
return fig, axes
......@@ -770,14 +790,16 @@ def plotClusterDendrograms(label_mat, e_vals, num_comp, num_cluster, mode='Full'
# Get the distrance between cluster means
distance_mat = pdist(centroid_mat)
distance_mat = scipy.spatial.distance.pdist(centroid_mat)
# get hierachical pairings of clusters
linkage_pairing = linkage(distance_mat, 'weighted')
linkage_pairing = scipy.cluster.hierarchy.linkage(distance_mat, 'weighted')
linkage_pairing[:, 3] = linkage_pairing[:, 3] / max(linkage_pairing[:, 3])
fig = plt.figure()
dendrogram(linkage_pairing, p=last, truncate_mode=mode, count_sort=c_sort, distance_sort=d_sort, leaf_rotation=90)
scipy.cluster.hierarchy.dendrogram(linkage_pairing, p=last, truncate_mode=mode,
count_sort=c_sort, distance_sort=d_sort,
leaf_rotation=90)
fig.axes[0].set_title('Dendrogram')
fig.axes[0].set_xlabel('Index or (cluster size)')
......
Supports Markdown
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