|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "46a0a71e", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Image Identification using \"pyimagej\"" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": 6, |
| 14 | + "id": "ca680db4", |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "from FIJI_Marco_For_Particle_Identification_V_stable import process_NF_particles as pnp\n", |
| 19 | + "import glob\n", |
| 20 | + "import multiprocessing as mp\n", |
| 21 | + "import numpy as np \n", |
| 22 | + "import pandas as pd\n", |
| 23 | + "import scipy.io as sio\n", |
| 24 | + "import shutil\n", |
| 25 | + "import time \n", |
| 26 | + "import gc\n", |
| 27 | + "import os" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "markdown", |
| 32 | + "id": "e56c6e8b", |
| 33 | + "metadata": {}, |
| 34 | + "source": [ |
| 35 | + "# Legacy sections " |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": 7, |
| 41 | + "id": "33a87513", |
| 42 | + "metadata": {}, |
| 43 | + "outputs": [], |
| 44 | + "source": [ |
| 45 | + "mainpath = r'D:\\phi_star_measurement\\4.2mm\\7um'\n", |
| 46 | + "#experimentPath = glob.glob(os.path.normpath(mainPath)+'\\*_files');\n", |
| 47 | + "experimentPath = glob.glob(mainpath+'\\**\\*_files')" |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "code", |
| 52 | + "execution_count": 8, |
| 53 | + "id": "b8266dfa", |
| 54 | + "metadata": {}, |
| 55 | + "outputs": [ |
| 56 | + { |
| 57 | + "data": { |
| 58 | + "text/plain": [ |
| 59 | + "['D:\\\\phi_star_measurement\\\\4.2mm\\\\7um\\\\1445\\\\16h05_fluo_2x2x_180V_2_exp_3db_342fps_files',\n", |
| 60 | + " 'D:\\\\phi_star_measurement\\\\4.2mm\\\\7um\\\\300\\\\16h42_fluo_2x2x_180V_2_exp_3db_342fps_files',\n", |
| 61 | + " 'D:\\\\phi_star_measurement\\\\4.2mm\\\\7um\\\\400\\\\16h42_fluo_2x2x_180V_2_exp_3db_342fps_files',\n", |
| 62 | + " 'D:\\\\phi_star_measurement\\\\4.2mm\\\\7um\\\\570\\\\16h42_fluo_2x2x_180V_2_exp_3db_342fps_files',\n", |
| 63 | + " 'D:\\\\phi_star_measurement\\\\4.2mm\\\\7um\\\\700\\\\16h36_fluo_2x2x_180V_2_exp_0db_342fps_files',\n", |
| 64 | + " 'D:\\\\phi_star_measurement\\\\4.2mm\\\\7um\\\\795\\\\15h54_fluo_2x2x_180V_2_exp_0db_342fps_files',\n", |
| 65 | + " 'D:\\\\phi_star_measurement\\\\4.2mm\\\\7um\\\\875\\\\15h12_fluo_2x2x_180V_2_exp_0db_342fps_files',\n", |
| 66 | + " 'D:\\\\phi_star_measurement\\\\4.2mm\\\\7um\\\\975\\\\16h25_fluo_2x2x_180V_2_exp_3db_342fps_files']" |
| 67 | + ] |
| 68 | + }, |
| 69 | + "execution_count": 8, |
| 70 | + "metadata": {}, |
| 71 | + "output_type": "execute_result" |
| 72 | + } |
| 73 | + ], |
| 74 | + "source": [ |
| 75 | + "experimentPath" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": null, |
| 81 | + "id": "19d09fd7", |
| 82 | + "metadata": { |
| 83 | + "code_folding": [] |
| 84 | + }, |
| 85 | + "outputs": [ |
| 86 | + { |
| 87 | + "name": "stdout", |
| 88 | + "output_type": "stream", |
| 89 | + "text": [ |
| 90 | + "time taken by D:/phi_star_measurement/4.2mm/7um/1445/16h05_fluo_2x2x_180V_2_exp_3db_342fps_files/Images/ is 35 minutes\n", |
| 91 | + "time taken by D:/phi_star_measurement/4.2mm/7um/300/16h42_fluo_2x2x_180V_2_exp_3db_342fps_files/Images/ is 29 minutes\n", |
| 92 | + "time taken by D:/phi_star_measurement/4.2mm/7um/400/16h42_fluo_2x2x_180V_2_exp_3db_342fps_files/Images/ is 32 minutes\n", |
| 93 | + "time taken by D:/phi_star_measurement/4.2mm/7um/570/16h42_fluo_2x2x_180V_2_exp_3db_342fps_files/Images/ is 27 minutes\n" |
| 94 | + ] |
| 95 | + } |
| 96 | + ], |
| 97 | + "source": [ |
| 98 | + "# parallelization code set\n", |
| 99 | + "# Create the file architecture \n", |
| 100 | + "# add the main path \n", |
| 101 | + "\n", |
| 102 | + "mainpath = r'D:\\phi_star_measurement\\4.2mm\\7um'\n", |
| 103 | + "#experimentPath = glob.glob(os.path.normpath(mainPath)+'\\*_files');\n", |
| 104 | + "experimentPath = glob.glob(mainpath+'\\**\\*_files')\n", |
| 105 | + "\n", |
| 106 | + "# add the Images \n", |
| 107 | + "experimentPath = [s + '\\\\Images\\\\' for s in experimentPath];\n", |
| 108 | + "experimentPath = [s .replace('\\\\','/') for s in experimentPath];\n", |
| 109 | + "\n", |
| 110 | + "results = [];\n", |
| 111 | + "for Path in experimentPath:\n", |
| 112 | + " # ~~~~~~~~~~~ Count the total number of frames within the \"Image\" folder ~~~~~~~~~~~~~\n", |
| 113 | + " Total_Frames = len(glob.glob(Path +'*.tif'));\n", |
| 114 | + " if (os.path.normpath(glob.glob(Path +'*.tif')[-1]).split(os.sep)[-1] == 'ImageBackground.tif'):\n", |
| 115 | + " Total_Frames -= 1;\n", |
| 116 | + " #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", |
| 117 | + "\n", |
| 118 | + " # ~~~~~~~~~~~~ read the total number of images ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", |
| 119 | + " N = 10; # specify the number of workers ... do this judiciously \n", |
| 120 | + " Ram_size = 30;\n", |
| 121 | + " memory_allocation = \"-Xmx\" + str(int (Ram_size/N)) + \"g\"; # N*memory alloc. not exceed the RAM size-1\n", |
| 122 | + " pool = mp.Pool(processes=N);\n", |
| 123 | + " #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", |
| 124 | + "\n", |
| 125 | + " #~~~~~~~~~~~~~~~~create the chunk firsts and lasts (tested robust) ~~~~~~~~~~~~~~~~~~~\n", |
| 126 | + " Chunk = (Total_Frames/N);\n", |
| 127 | + " frame_start = [];\n", |
| 128 | + " frame_finish =[];\n", |
| 129 | + " inp_for_starmap =[];\n", |
| 130 | + "\n", |
| 131 | + " for i in range(N):\n", |
| 132 | + " frame_start.append(int(np.floor(i*Chunk)+1));\n", |
| 133 | + " frame_finish.append(int(np.floor((i+1)*Chunk+1)));\n", |
| 134 | + " inp_for_starmap.append(tuple([Path,frame_start[i],frame_finish[i],memory_allocation]));\n", |
| 135 | + "\n", |
| 136 | + " #~~~~~~~~~~~~~create a \"main\" function for running the parallization code~~~~~~~~~~~~~\n", |
| 137 | + " #~~~~ the __main__ function is not necessary on mac and linux .. window 's crap!~~~~~~\n", |
| 138 | + "\n", |
| 139 | + " if __name__==\"__main__\":\n", |
| 140 | + "\n", |
| 141 | + " # ~~~~~~~~~~~~~~~~~~~ create a temporary folder in C: Drive ~~~~~~~~~~~~~~~~~~~~~~\n", |
| 142 | + " os.mkdir('C:/Temporary')\n", |
| 143 | + "\n", |
| 144 | + " start = time.perf_counter(); \n", |
| 145 | + " # creating N processes and executing them\n", |
| 146 | + " results = pool.starmap(pnp, inp_for_starmap);\n", |
| 147 | + " \n", |
| 148 | + " # convert every float64 to float32\n", |
| 149 | + " #for c in range(N):\n", |
| 150 | + " # results[c][results[c].select_dtypes(np.float64).columns] = results[c].select_dtypes(np.float64).astype(np.float32);\n", |
| 151 | + "\n", |
| 152 | + " # measure the run time\n", |
| 153 | + " finish = time.perf_counter();\n", |
| 154 | + "\n", |
| 155 | + " # delete the temprorary folder in C: drive\n", |
| 156 | + " shutil.rmtree('C:/Temporary');\n", |
| 157 | + " \n", |
| 158 | + " # closing the pool is very important!! creates problem with multiple running \n", |
| 159 | + " pool.close()\n", |
| 160 | + "\n", |
| 161 | + " # Transform the path name to the position for saving the .mat output\n", |
| 162 | + " parts = os.path.normpath(Path).split(os.sep)[:-1];\n", |
| 163 | + " parts.extend(['Analysis','1_Positions','Positions_NF.mat']);\n", |
| 164 | + " mat_file_name = '/'.join(parts);\n", |
| 165 | + "\n", |
| 166 | + " # Save the data in a .mat file \n", |
| 167 | + " dictn = {\"XYF\":np.concatenate(results)};\n", |
| 168 | + " sio.savemat(mat_file_name, dictn); \n", |
| 169 | + " \n", |
| 170 | + " # remove the large datasets from the memory and collect garbage \n", |
| 171 | + " del(results);\n", |
| 172 | + " del(dictn);\n", |
| 173 | + " gc.collect( );\n", |
| 174 | + " gc.collect(1);\n", |
| 175 | + " gc.collect(2);\n", |
| 176 | + " print(\"time taken by\",Path,\" is \", round((finish - start)/60),\"minutes\");" |
| 177 | + ] |
| 178 | + }, |
| 179 | + { |
| 180 | + "cell_type": "code", |
| 181 | + "execution_count": null, |
| 182 | + "id": "c6c95ca0", |
| 183 | + "metadata": {}, |
| 184 | + "outputs": [], |
| 185 | + "source": [] |
| 186 | + } |
| 187 | + ], |
| 188 | + "metadata": { |
| 189 | + "kernelspec": { |
| 190 | + "display_name": "Python 3 (ipykernel)", |
| 191 | + "language": "python", |
| 192 | + "name": "python3" |
| 193 | + }, |
| 194 | + "language_info": { |
| 195 | + "codemirror_mode": { |
| 196 | + "name": "ipython", |
| 197 | + "version": 3 |
| 198 | + }, |
| 199 | + "file_extension": ".py", |
| 200 | + "mimetype": "text/x-python", |
| 201 | + "name": "python", |
| 202 | + "nbconvert_exporter": "python", |
| 203 | + "pygments_lexer": "ipython3", |
| 204 | + "version": "3.9.7" |
| 205 | + }, |
| 206 | + "latex_envs": { |
| 207 | + "LaTeX_envs_menu_present": true, |
| 208 | + "autoclose": true, |
| 209 | + "autocomplete": true, |
| 210 | + "bibliofile": "biblio.bib", |
| 211 | + "cite_by": "apalike", |
| 212 | + "current_citInitial": 1, |
| 213 | + "eqLabelWithNumbers": true, |
| 214 | + "eqNumInitial": 1, |
| 215 | + "hotkeys": { |
| 216 | + "equation": "Ctrl-E", |
| 217 | + "itemize": "Ctrl-I" |
| 218 | + }, |
| 219 | + "labels_anchors": false, |
| 220 | + "latex_user_defs": false, |
| 221 | + "report_style_numbering": false, |
| 222 | + "user_envs_cfg": false |
| 223 | + } |
| 224 | + }, |
| 225 | + "nbformat": 4, |
| 226 | + "nbformat_minor": 5 |
| 227 | +} |
0 commit comments