From 346b9cb2d06a7d4cdf7e77a6ca5f7992bc941472 Mon Sep 17 00:00:00 2001 From: Sooyoung Cheong <64125280+c-sooyoung@users.noreply.github.com> Date: Wed, 22 Jul 2026 17:48:22 +0900 Subject: [PATCH] 260414 --- 260414-radius-test-wrong/260414.ipynb | 280 ++++++++++++++++++++++++++ 1 file changed, 280 insertions(+) create mode 100644 260414-radius-test-wrong/260414.ipynb diff --git a/260414-radius-test-wrong/260414.ipynb b/260414-radius-test-wrong/260414.ipynb new file mode 100644 index 0000000..59efc7a --- /dev/null +++ b/260414-radius-test-wrong/260414.ipynb @@ -0,0 +1,280 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "405952b4", + "metadata": {}, + "outputs": [], + "source": [ + "# THIS METHOD IS WRONG FOR IDENTIFYING ATOMS" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "368a5090", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "from matplotlib import pyplot as plt\n", + "import tifffile\n", + "import os\n", + "from scipy.ndimage import gaussian_filter, binary_opening, label\n", + "from skimage import exposure, filters, morphology, measure\n", + "from scipy.signal import convolve2d\n", + "from skimage.morphology import disk\n", + "from skimage.feature import peak_local_max\n", + "\n", + "from tqdm.notebook import tqdm\n", + "\n", + "plt.rcParams['font.family'] = 'sans-serif'\n", + "plt.rcParams['font.sans-serif'] = ['Inter Variable ss02']\n", + "plt.rcParams['figure.titlesize'] = 10\n", + "plt.rcParams['axes.titlesize'] = 10" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "2912e633", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['bto02_objp_zstack_crop_08bit_iter1000.tif',\n", + " 'bto02_objp_zsum_crop_08bit_iter1000.tif',\n", + " 'bto05_objp_zstack_crop_08bit_iter1000.tif',\n", + " 'bto05_objp_zsum_crop_08bit_iter1000.tif',\n", + " 'bto08_objp_zstack_crop_08bit_iter0100.tif',\n", + " 'bto08_objp_zsum_crop_08bit_iter0100.tif',\n", + " '260414.ipynb']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.listdir()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "2d3b2fa3", + "metadata": {}, + "outputs": [], + "source": [ + "ptycho_stack = tifffile.imread('bto02_objp_zstack_crop_08bit_iter1000.tif') / 255\n", + "# ptycho_sum = tifffile.imread('bto02_objp_zsum_crop_08bit_iter1000.tif') / 255\n", + "ptycho_slice = ptycho_stack[13]\n", + "# bf = plt.imread('bf.png').mean(axis=2)\n", + "# bf = 1 - (bf - bf.min()) / (bf.max() - bf.min())\n", + "# haadf = plt.imread('haadf.png').mean(axis=2)\n", + "# haadf = (haadf - haadf.min()) / (haadf.max() - haadf.min()) \n", + "# laadf = plt.imread('laadf.png').mean(axis=2)\n", + "# laadf = (laadf - laadf.min()) / (laadf.max() - laadf.min())\n", + "\n", + "images = [ptycho_slice]\n", + "# names = ['Ptycho Sum', 'Ptycho Slice', 'Bright Field (Inverted)', 'HAADF', 'LAADF']\n", + "\n", + "# fig, axs = plt.subplots(1, len(images), dpi=300)\n", + "# for i, ax in enumerate(axs):\n", + "# ax.axis('off')\n", + "# ax.imshow(images[i], cmap='gray')\n", + "# ax.set_title(names[i], size=8)\n", + "\n", + "# plt.tight_layout()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "a52ce371", + "metadata": {}, + "outputs": [], + "source": [ + "def feature_count_vs_radius(img, radii, high_pass=5):\n", + " if high_pass:\n", + " img = img - gaussian_filter(img, sigma=high_pass)\n", + "\n", + " counts = []\n", + " peak_locs = []\n", + "\n", + " for r in tqdm(radii):\n", + " # ---- 1. Build a disc kernel ------------------------------------\n", + " # (skimage.disk returns a binary mask; we normalize it to sum = 1)\n", + " kernel = disk(r).astype(float)\n", + " kernel /= kernel.sum()\n", + "\n", + " # ---- 2. Convolve -------------------------------------------------\n", + " conv = convolve2d(img, kernel, mode='same', boundary='symm')\n", + "\n", + " # ---- 3. Find local maxima ---------------------------------------\n", + " # * min_distance ensures that peaks are at least r pixels apart\n", + " # * threshold_abs picks peaks that stand out above the background\n", + " peaks = peak_local_max(conv,\n", + " min_distance=int(r),\n", + " threshold_abs=conv.mean() + 2*conv.std(),\n", + " num_peaks=np.inf)\n", + "\n", + " counts.append(len(peaks))\n", + " peak_locs.append(peaks)\n", + " \n", + " return radii, counts, peak_locs" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "6d8b25e3", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "9599a4de81f440cb9c9cdbf3d1925c78", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + " 0%| | 0/32 [00:00" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "idxs = [6, 14, 20]\n", + "i = 0\n", + "\n", + "plt.figure(dpi=72)\n", + "plt.imshow(images[i], cmap='gray')\n", + "for idx in idxs:\n", + " plt.scatter(\n", + " pl[i][idx][:,1][pl[i][idx][:,1] < 300],\n", + " pl[i][idx][:,0][pl[i][idx][:,1] < 300],\n", + " s=30,)\n", + "plt.axis('off')\n", + "\n", + "plt.xlim(200, 400)\n", + "plt.ylim(200, 400)\n", + "\n", + "# plt.legend(['Ba', 'O', 'Ti'], loc='lower right')\n", + "\n", + "x = 385\n", + "L = 55\n", + "plt.plot([x - L, x], [215] *2, 'w-', lw=10)\n", + "plt.text(x - L/2, 220, '1 nm', color='w', ha='center', va='bottom', fontsize=30)\n", + "\n", + "plt.show()\n", + "plt.close()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8f1b5ac6", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "lemon", + "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.12.12" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}