-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #182 from ahoust17/main
fixed atom_tools
- Loading branch information
Showing
5 changed files
with
284 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Basics of reading an MRC file with 4D STEM data from the Spectra300 at UTK\n", | ||
"## By Austin Houston\n", | ||
"### Last updated 2024-09-14" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import os\n", | ||
"import sys\n", | ||
"\n", | ||
"%matplotlib ipympl\n", | ||
"import numpy as np\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"\n", | ||
"sys.path.insert(0, '/Users/austin/Documents/GitHub/SciFiReaders/')\n", | ||
"import SciFiReaders\n", | ||
"\n", | ||
"sys.path.insert(0, '/Users/austin/Documents/GitHub/pyTEMlib/')\n", | ||
"import pyTEMlib\n", | ||
"import pyTEMlib.file_tools as ft\n", | ||
"\n", | ||
"print(\"SciFiReaders version: \", SciFiReaders.__version__)\n", | ||
"print(\"pyTEMlib version: \", pyTEMlib.__version__)\n", | ||
"\n", | ||
"# for beginning analysis\n", | ||
"from sklearn.cluster import KMeans\n", | ||
"from sklearn.decomposition import PCA\n", | ||
"from sklearn.cluster import KMeans\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"mrc_filepath = '/Users/austin/Dropbox/GaTech_colabs/SnSe_MgO/2024_06_19_data/4D_STEM/'\n", | ||
"\n", | ||
"files = os.listdir(mrc_filepath)\n", | ||
"files = [f for f in files if f.endswith('.mrc')]\n", | ||
"\n", | ||
"# Load the first file\n", | ||
"dset = ft.open_file(mrc_filepath + files[1])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"data = dset['Channel_000']\n", | ||
"\n", | ||
"view = data.plot()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"mrc_array = np.array(data)\n", | ||
"N, M, height, width = data.shape\n", | ||
"datacube_flat = mrc_array.reshape(N * M, -1)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Perform KMeans clustering\n", | ||
"clusters = 3 \n", | ||
"kmeans = KMeans(n_clusters=clusters, random_state=0).fit(datacube_flat)\n", | ||
"labels = kmeans.labels_\n", | ||
"cluster_centers = kmeans.cluster_centers_\n", | ||
"\n", | ||
"# Reduce the data to 3D using PCA\n", | ||
"pca = PCA(n_components=3)\n", | ||
"datacube_reduced = pca.fit_transform(datacube_flat)\n", | ||
"cluster_centers_reduced = pca.transform(cluster_centers)\n", | ||
"\n", | ||
"# Create a 3D plot\n", | ||
"fig = plt.figure()\n", | ||
"ax = fig.add_subplot(111, projection='3d')\n", | ||
"scatter = ax.scatter(datacube_reduced[:, 0], datacube_reduced[:, 1], datacube_reduced[:, 2], c=labels, cmap='viridis', marker='o')\n", | ||
"ax.set_xlabel('PCA Component 1')\n", | ||
"ax.set_ylabel('PCA Component 2')\n", | ||
"ax.set_zlabel('PCA Component 3')\n", | ||
"ax.set_xticks([])\n", | ||
"ax.set_yticks([])\n", | ||
"ax.set_zticks([])\n", | ||
"plt.show()\n", | ||
"\n", | ||
"\n", | ||
"label_image = labels.reshape((M, N))\n", | ||
"\n", | ||
"plt.figure()\n", | ||
"plt.imshow(label_image, cmap='viridis')\n", | ||
"plt.colorbar()\n", | ||
"plt.show()\n", | ||
"\n", | ||
"# Reshape cluster centers back to original image dimensions\n", | ||
"cluster_center_images = cluster_centers.reshape((kmeans.n_clusters, height, width))\n", | ||
"\n", | ||
"# Plot the average images\n", | ||
"fig, axes = plt.subplots(1, kmeans.n_clusters, figsize=(15, 5))\n", | ||
"\n", | ||
"for i, ax in enumerate(axes):\n", | ||
" ax.imshow(cluster_center_images[i], cmap='viridis')\n", | ||
" ax.set_title(f'Cluster Center {i+1}')\n", | ||
" ax.axis('off')\n", | ||
"\n", | ||
"plt.show()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "pytemlib", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.0" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.