Skip to content

Instantly share code, notes, and snippets.

@sriharijayaram5
Last active June 11, 2021 07:45
Show Gist options
  • Save sriharijayaram5/2d7929eb90e4fe77e17f79f1b8ae8d9d to your computer and use it in GitHub Desktop.
Save sriharijayaram5/2d7929eb90e4fe77e17f79f1b8ae8d9d to your computer and use it in GitHub Desktop.
OS-SIM algorithm implementaion in python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import cv2\n",
"from PIL import Image\n",
"import datetime"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def read_tiff(path):\n",
" \"\"\"\n",
" path - Path to the multipage-tiff file\n",
" \"\"\"\n",
" img = Image.open(path)\n",
" images = []\n",
" for i in range(img.n_frames):\n",
" img.seek(i)\n",
" images.append(np.array(img))\n",
" return np.array(images, dtype='float')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"loc = '20210514-1642-35_120ms.tif'\n",
"raw = read_tiff(loc)\n",
"print(raw.shape)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def maxmin(raw):\n",
" mms = []\n",
" for i in range(raw.shape[0]//3):\n",
" mm = np.max(raw[0+i*3:3+i*3:1], axis=0) - np.min(raw[0+i*3:3+i*3:1], axis=0)\n",
" mms.append(mm)\n",
" mm = np.mean(np.asarray(mms), axis=0)\n",
" return mm\n",
"\n",
"def homodyne0(raw, angles, phases):\n",
" s = raw[0]\n",
" x = np.arange(1,4)\n",
" def g(o):\n",
" return np.exp(2*1j*np.pi/3*o)\n",
" mms = []\n",
" for i in range(angles):\n",
" mmp = []\n",
" for j in range(phases):\n",
" mmp.append(raw[j+i*phases]*g(np.ones_like(s)*(j+1)))\n",
" mm = np.abs(np.sum(np.asarray(mmp), axis=0))\n",
" mms.append(mm)\n",
" mm = np.mean(np.asarray(mms), axis=0)\n",
" return mm\n",
"\n",
"def homodyne1(raw):\n",
" s = raw[0]\n",
" x = np.arange(1,4)\n",
" def g(o):\n",
" return np.exp(2*1j*np.pi/3*o)\n",
" mms = []\n",
" for i in range(raw.shape[0]//3):\n",
" mm = np.abs(raw[0+i*3]*g(np.ones_like(s)) + raw[1+i*3]*g(np.ones_like(s)*2) +raw[2+i*3]*g(np.ones_like(s)*3))\n",
" mms.append(mm)\n",
" mm = np.mean(np.asarray(mms), axis=0)\n",
" return mm\n",
"\n",
"def homodyne2(raw):\n",
" mms = [] #- np.mean(raw, axis=0)\n",
" for i in range(raw.shape[0]//3):\n",
" mm = np.sqrt((np.square((raw[0+3*i]-raw[1+3*i])) + np.square((raw[0+3*i]-raw[2+3*i])) + np.square((raw[1+3*i]-raw[2+3*i]))))\n",
" mms.append(mm)\n",
" mm = np.mean(np.asarray(mms), axis=0)\n",
"# mm = mms[0]\n",
" return mm"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"timestamp = datetime.datetime.now()\n",
"t = timestamp.strftime(\"%Y%m%d-%H%M-%S\")\n",
"fn = homodyne0\n",
"Image.fromarray(fn(raw, 3, 6)).save(f'{fn.__name__}_{t}_'+loc)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"loc = '20210531-2231-27_YBCO_10.5K_0.5mW_2r_-17dBm_SIM_ODMR_data'\n",
"directory = 'C:/Users/yy3/Work/Gpufit_binary/Gpufit_1.1.0_win64_cublas_build201804131033/Gpufit_1.1.0_win64_cublas_build201804131033/python/examples/Data/06_2021/'\n",
"all_data = np.load(directory+loc+'_ch0_sweep.npz')['sweep_images'].squeeze()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(all_data.shape)\n",
"final_data = np.empty((2,36, 471,572))\n",
"s = all_data.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for f in range(s[2]):\n",
" for o in range(s[1]):\n",
" final_data[o,f,:,:] = homodyne0(all_data[:,o,f,:,:], 3, 3)\n",
"np.savez_compressed(directory+loc+'_ch0_sweep_simmed.npz', sweep_images=(final_data))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment