Commit 5f9d8206 authored by Somnath, Suhas's avatar Somnath, Suhas
Browse files

Fixed bugs related to plot_utils, SVD, and kMeans changes

parent 20a25952
......@@ -21,7 +21,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!pip install -U numpy scipy skimage h5py matplotlib Ipython ipywidgets pycroscopy\n",
......@@ -75,7 +77,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"image_path = px.io.uiGetFile('*.png *PNG *TIFF * TIF *tif *tiff *BMP *bmp','Images')\n",
......@@ -103,7 +107,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Check if an HDF5 file with the chosen image already exists.\n",
......@@ -151,7 +157,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('Datasets and datagroups within the file:')\n",
......@@ -181,7 +189,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Initialize the windowing class\n",
......@@ -209,14 +219,13 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"fig, axis = plt.subplots(figsize=(10,10))\n",
"img = axis.imshow(raw_image_mat,cmap=px.plot_utils.cmap_jet_white_center(), origin='lower');\n",
"divider = make_axes_locatable(axis)\n",
"cax = divider.append_axes(\"right\", size=\"5%\", pad=0.2)\n",
"plt.colorbar(img, cax=cax)\n",
"px.plot_utils.plot_map(axis, raw_image_mat, cmap=px.plot_utils.cmap_jet_white_center())\n",
"axis.set_title('Raw Image', fontsize=16);"
]
},
......@@ -230,7 +239,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"num_peaks = 2\n",
......@@ -242,7 +253,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Uncomment this line if you need to manually specify a window size\n",
......@@ -251,15 +264,13 @@
"# plot a single window\n",
"row_offset = int(0.5*(num_x-win_size))\n",
"col_offset = int(0.5*(num_y-win_size))\n",
"plt.figure()\n",
"plt.imshow(raw_image_mat[row_offset:row_offset+win_size,\n",
" col_offset:col_offset+win_size], \n",
" cmap=px.plot_utils.cmap_jet_white_center(),\n",
" origin='lower');\n",
"\n",
"fig, axis = plt.subplots(figsize=(5, 5))\n",
"px.plot_utils.plot_map(axis, raw_image_mat[row_offset:row_offset+win_size,\n",
" col_offset:col_offset+win_size], \n",
" cmap=px.plot_utils.cmap_jet_white_center())\n",
"# the result should be about the size of a unit cell\n",
"# if it is the wrong size, just choose on manually by setting the win_size\n",
"plt.show()"
"axis.set_title('Example window', fontsize=18);"
]
},
{
......@@ -273,7 +284,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"windowing_parms = {\n",
......@@ -310,7 +323,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Peek at a few random windows\n",
......@@ -322,7 +337,7 @@
" example_wins[:, :, rand_ind] = np.reshape(h5_wins[rand_pos], (windowing_parms['win_x'], windowing_parms['win_y']))\n",
" \n",
"px.plot_utils.plot_map_stack(example_wins, heading='Example Windows', cmap=px.plot_utils.cmap_jet_white_center(),\n",
" title=['Window # ' + str(win_pos) for win_pos in rand_positions]);"
" title=['Window # ' + str(win_pos) for win_pos in rand_positions], fig_title_yoffset=0.93);"
]
},
{
......@@ -338,7 +353,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# check to make sure number of components is correct:\n",
......@@ -346,15 +363,14 @@
"num_comp = min(num_comp, \n",
" min(h5_wins.shape)*len(h5_wins.dtype))\n",
"\n",
"proc = px.SVD(h5_main, num_comps=num_comp)\n",
"proc = px.processing.SVD(h5_wins, num_components=num_comp)\n",
"\n",
"# First check if SVD was already computed on this dataset:\n",
"if proc.duplicate_h5_groups is None:\n",
" print('SVD was either not performed or was performed with different parameters')\n",
" print('SVD not performed with these parameters')\n",
" h5_svd = proc.compute()\n",
"else:\n",
" print('Taking previous SVD results already present in file')\n",
" h5_svd = proc.duplicate_h5_groups[-1]\n",
" print('Taking existing results!')\n",
" h5_svd = proc.duplicate_h5_groups \n",
" \n",
"h5_U = h5_svd['U']\n",
"h5_S = h5_svd['S']\n",
......@@ -392,10 +408,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"fig_S, ax_S = px.plot_utils.plotScree(h5_S[()]);"
"fig_S, ax_S = px.plot_utils.plot_scree(h5_S[()]);"
]
},
{
......@@ -410,7 +428,8 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
"collapsed": false,
"scrolled": false
},
"outputs": [],
"source": [
......@@ -431,7 +450,8 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
"collapsed": false,
"scrolled": false
},
"outputs": [],
"source": [
......@@ -456,7 +476,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"clean_components = range(36) # np.append(range(5,9),(17,18))\n",
......@@ -486,7 +508,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Building a stack of images from here:\n",
......@@ -522,7 +546,8 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
"collapsed": false,
"scrolled": false
},
"outputs": [],
"source": [
......@@ -544,6 +569,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [],
......@@ -608,19 +634,16 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"num_comps = 12\n",
"num_comps = 24\n",
"\n",
"fig, axis = plt.subplots(figsize=(7, 7))\n",
"clean_image_mat = image_components[:, :, num_comps]\n",
"img_clean = axis.imshow(clean_image_mat, cmap=px.plot_utils.cmap_jet_white_center(), origin='lower')\n",
"mean_val = np.mean(clean_image_mat)\n",
"std_val = np.std(clean_image_mat)\n",
"img_clean.set_clim(vmin=mean_val-img_stdevs*std_val, vmax=mean_val+img_stdevs*std_val)\n",
"axis.get_yaxis().set_visible(False)\n",
"axis.get_xaxis().set_visible(False)\n",
"_ = px.plot_utils.plot_map(axis, clean_image_mat, cmap=px.plot_utils.cmap_jet_white_center())\n",
"axis.set_title('Cleaned Image', fontsize=16);"
]
},
......@@ -641,25 +664,22 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [],
"source": [
"clean_components = 32\n",
"num_clusters = 32\n",
"num_clusters = 4\n",
"estimator = px.Cluster(h5_U, KMeans(n_clusters=num_clusters), num_comps=num_comps)\n",
"\n",
"proc = px.Cluster(h5_U, KMeans(n_clusters=num_clusters), num_comps=clean_components)\n",
"\n",
"# Check for existing Clustering results\n",
"if proc.duplicate_h5_groups is None:\n",
" print('No k-Means computation found. Doing K-Means now')\n",
"if estimator.duplicate_h5_groups is None:\n",
" t0 = time()\n",
" h5_kmeans = proc.compute()\n",
" h5_kmeans = estimator.compute()\n",
" print('kMeans took {} seconds.'.format(round(time()-t0, 2)))\n",
"else:\n",
" print('Taking existing results of previous K-Means computation')\n",
" h5_kmeans = proc.duplicate_h5_groups[-1]\n",
"\n",
" h5_kmeans = estimator.duplicate_h5_groups[-1]\n",
" print( 'Using existing results.') \n",
" \n",
"print( 'Clustering results in {}.'.format(h5_kmeans.name))\n",
"\n",
"half_wind = int(win_size*0.5)\n",
......@@ -691,14 +711,16 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Plot dendrogram here\n",
"# Get the distrance between cluster means \n",
"#Get the distrance between cluster means \n",
"distance_mat = pdist(h5_kmeans['Mean_Response'][()]) \n",
" \n",
"# get hierachical pairings of clusters \n",
"#get hierachical pairings of clusters \n",
"linkage_pairing = linkage(distance_mat,'weighted') \n",
"\n",
"# Normalize the pairwise distance with the maximum distance\n",
......@@ -708,7 +730,7 @@
"fig = plt.figure(figsize=(10,3)) \n",
"retval = dendrogram(linkage_pairing, count_sort=True, \n",
" distance_sort=True, leaf_rotation=90) \n",
"# fig.axes[0].set_title('Dendrogram') \n",
"#fig.axes[0].set_title('Dendrogram') \n",
"fig.axes[0].set_xlabel('Cluster number', fontsize=20) \n",
"fig.axes[0].set_ylabel('Cluster separation', fontsize=20)\n",
"px.plot_utils.set_tick_font_size(fig.axes[0], 12)"
......@@ -728,6 +750,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"scrolled": false
},
"outputs": [],
......@@ -827,6 +850,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"scrolled": false
},
"outputs": [],
......@@ -882,7 +906,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"motif_match_coeffs = list()\n",
......@@ -915,7 +941,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"show_legend = True\n",
......@@ -962,6 +990,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"scrolled": false
},
"outputs": [],
......@@ -1036,7 +1065,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print(thresholds)\n",
......@@ -1061,7 +1092,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# overlay atom positions on original image\n",
......@@ -1101,7 +1134,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"h5_file.close()"
......@@ -1110,7 +1145,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
......@@ -1119,7 +1156,7 @@
"anaconda-cloud": {},
"celltoolbar": "Raw Cell Format",
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
......@@ -1133,61 +1170,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
},
"widgets": {
"state": {
"29604cba705348bebab931e8c5a8f33b": {
"views": [
{
"cell_index": 41.0
}
]
},
"3148e6a4894e42d2856f6c31aa70805a": {
"views": [
{
"cell_index": 33.0
}
]
},
"4c6e4e0c659542ca937b2ca85f7f33cf": {
"views": [
{
"cell_index": 49.0
}
]
},
"9236753466b34d9eb471907b4a33fd73": {
"views": [
{
"cell_index": 38.0
}
]
},
"ba247d838fef4411b7c74d7055033284": {
"views": [
{
"cell_index": 38.0
}
]
},
"bafd596f9f254b4a8652bbf8e7bdc096": {
"views": [
{
"cell_index": 41.0
}
]
},
"e687973fb1ff41949de8de5587ad6461": {
"views": [
{
"cell_index": 38.0
}
]
}
},
"version": "1.2.0"
"version": "3.5.2"
}
},
"nbformat": 4,
......
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