Skip to content

Commit

Permalink
multiprocessing added
Browse files Browse the repository at this point in the history
  • Loading branch information
raghavchhetri committed Apr 1, 2022
1 parent 0921ef4 commit a89135a
Show file tree
Hide file tree
Showing 94 changed files with 190 additions and 396 deletions.
19 changes: 7 additions & 12 deletions GerchbergSaxton_Matrix.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Computation\n",
"### Compute\n",
"---\n",
"Convention for `movebeams_um`\n",
"\n",
Expand All @@ -86,8 +86,9 @@
" near = beams3-6-9 \n",
" +ve values for `movebeams_um` moves the beam UP in sample space i.e., along +Z in the microscope\n",
" \n",
" 1. Define target pattern `target_pattern()`\n",
" 2. Compute phase mask to generate the above target pattern when applied to an SLM `generate_mask`"
" Process:\n",
" 1. Define target pattern: target_pattern()\n",
" 2. Compute phase mask to generate the above target pattern when applied to an SLM: generate_mask()"
]
},
{
Expand Down Expand Up @@ -286,16 +287,10 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Multiprocessing\n",
"---"
"##### To Multiprocess\n",
"\n",
"See <code>GerchbergSaxton_Matrix_MultiProcess.ipynb </code>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
139 changes: 139 additions & 0 deletions GerchbergSaxton_Matrix_MultiProcess.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "a918bec5",
"metadata": {},
"source": [
"## <code>GerchbergSaxton_Matrix_MultiProcess.ipynb </code> \n",
" - 03-31-2022\n",
" - Author: Raghav K. Chhetri\n",
" - Multiprocessed Phase Retrievel via Gerchberg-Saxton algorithm to generate phase masks for an SLM \n",
" - Using \"LightPipes for Python\""
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "1c5fd314",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invoking __init__.py for matrixpack\n"
]
}
],
"source": [
"from matrixpack import mask_func, move_func\n",
"from functools import partial\n",
"from LightPipes import nm\n",
"import multiprocessing\n",
"import matplotlib\n",
"%matplotlib inline\n",
"import pylab as plt\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "40f6a823",
"metadata": {},
"outputs": [],
"source": [
"lamda=488*nm\n",
"niter=50\n",
"dtag='MATRIX'\n",
"path='D:\\\\masks'\n",
"positions = move_func.move(3)\n",
"#position = move_func.move(3)[0:8]\n",
"num_workers = multiprocessing.cpu_count()\n",
"print(num_workers)"
]
},
{
"cell_type": "markdown",
"id": "5e864059",
"metadata": {},
"source": [
"---\n",
"### Compute\n",
"\n",
" If each beam were allowed three choices: -step, 0, or +step, then 3^9 = 19683 possible combinations!\n",
" Alternatively, I'm only selecting a few cases that follow these rules:\n",
" - Each beam has two choices: 0 or +step, then 2^9 = 512 combinations\n",
" - Each beam has two choices: 0 or -step (512-1 combinations: all zeros is already counted)\n",
" - Each beam has two choices: +step or -step (512-2 combinations: all +step already counted, all -step already counted)\n",
" \n",
" So, only need to consider 1533 combinations. \n",
" Note: 9 -bit representation of a number between 0 and 2^9-1= 511 can be obtained by f'{j:09b}'\n",
" \n",
" #adg corresponds to beams 147 = far\n",
" #beh corresponds beams 258 = mid\n",
" #cfi corresponds beams 369 = near\n",
" \n",
" Process:\n",
" 1. Auto-generate a list of beam positions using the rules above: list_move_um()\n",
" 2. Multiprocess mask_func.mask() to generate phase masks for all beam positions on the list "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "8b89208f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Took 135.3 sec\n"
]
}
],
"source": [
"t0 = time.time()\n",
"if __name__ == '__main__':\n",
" pool = multiprocessing.Pool(processes=num_workers) \n",
" poolfun = partial(mask_func.mask, wavelength=lamda, Niter=niter, tag=dtag, outPath=path)\n",
" pool.map(poolfun, positions)\n",
" pool.close()\n",
" pool.join() \n",
"print('Took', round(time.time()-t0,2), 'sec')"
]
},
{
"cell_type": "raw",
"id": "b0a21662",
"metadata": {},
"source": [
"# 12 iterations, 1533 masks took 11.5 hours in D13 --> if linear, 1533 masks, 50 iterations = 2 days\n",
"# 50 iterations, 8 masks took 34 minutes in D13 --> if linear, 1533 masks, 50 iterations = 4.5 days"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Binary file removed masks/488nm_Niter100_MATRIX.bmp
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
179 changes: 0 additions & 179 deletions matrixSLM.py

This file was deleted.

13 changes: 8 additions & 5 deletions maskfun.py → matrixpack/mask_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import pylab as plt
from skimage import io
from os.path import join
import targetfun
from matrixpack import target_func
from matrixpack.constants import dx, dz, MagAM, fSLM, SLMpixel, SLMheight, SLMwidth, beamD
from LightPipes import PipFFT, SubIntensity, Phase, nm

def mask(movebeams_um=[0,0,0,0,0,0,0,0,0], wavelength=488*nm, Niter=21, tag='MATRIX'):
[Field, target, footer] = targetfun.tar(movebeams_um, tag)
outPath = os.getcwd()+'\masks'
def mask(movebeams_um=[0,0,0,0,0,0,0,0,0], wavelength=488*nm, Niter=21, tag='MATRIX', outPath = os.getcwd()+'\masks'):
[Field, target, footer] = target_func.tar(movebeams_um, tag)

#Iteration loop to get phase distribution
#Field = SLMfield
gridpixels = len(target) #same size as the Target
Expand Down Expand Up @@ -43,4 +43,7 @@ def mask(movebeams_um=[0,0,0,0,0,0,0,0,0], wavelength=488*nm, Niter=21, tag='MAT

#Save mask to be applied to SLM
outfilename= str(int(wavelength/nm))+'nm_Niter'+str(Niter)+'_' +tag +footer
io.imsave(join(outPath, outfilename), phaseMask8)
io.imsave(join(outPath, outfilename), phaseMask8)

if __name__ == '__main__':
mask()
Loading

0 comments on commit a89135a

Please sign in to comment.