From 08b37d6941b19145580e0b7a84917f7b3cd1ed65 Mon Sep 17 00:00:00 2001 From: Kazunari Kaizu Date: Wed, 14 Mar 2018 18:03:11 +0900 Subject: [PATCH] Reindent --- bioimaging/NEC_tirfm_script.py | 94 +- bioimaging/analyses_correlation.py | 44 +- bioimaging/analyses_moriga_fitting.py | 155 +- bioimaging/analyses_palm_data.py | 81 +- bioimaging/analyses_sim_fitting.py | 105 +- bioimaging/analyses_sim_hist.py | 167 +- bioimaging/bleaching_configs.py | 14 +- bioimaging/blinking_script.py | 82 +- bioimaging/camera_handler.py | 322 ++- bioimaging/camera_script.py | 36 +- bioimaging/convert_addimages.py | 79 +- bioimaging/convert_csv2txt.py | 77 +- bioimaging/convert_fcs2npy.py | 33 +- bioimaging/convert_npy2image.py | 75 +- bioimaging/convert_temp.py | 147 +- bioimaging/effects_handler.py | 523 +++-- bioimaging/epifm_handler.py | 2849 ++++++++++++------------ bioimaging/epifm_rewrite_script.py | 82 +- bioimaging/epifm_script.py | 90 +- bioimaging/erk102_lscm_script.py | 99 +- bioimaging/fcs_handler.py | 804 ++++--- bioimaging/fcs_script.py | 89 +- bioimaging/flip_handler.py | 441 ++-- bioimaging/flip_rewrite_script.py | 92 +- bioimaging/flip_script.py | 96 +- bioimaging/fluorescence.py | 182 +- bioimaging/frap_handler.py | 441 ++-- bioimaging/frap_rewrite_script.py | 92 +- bioimaging/frap_script.py | 96 +- bioimaging/hiroshima_configs.py | 14 +- bioimaging/hiroshima_handler.py | 762 +++---- bioimaging/hiroshima_rewrite_script.py | 82 +- bioimaging/hiroshima_script.py | 90 +- bioimaging/invitro_lscm_script.py | 57 +- bioimaging/kaizu_rewrite_script.py | 66 +- bioimaging/kaizu_script.py | 74 +- bioimaging/linescan_confm_handler.py | 580 ++--- bioimaging/linescan_confm_script.py | 61 +- bioimaging/lscm_handler.py | 1884 ++++++++-------- bioimaging/lscm_rewrite_script.py | 82 +- bioimaging/lscm_script.py | 72 +- bioimaging/lscm_test_convert.py | 201 +- bioimaging/minDE_handler.py | 121 +- bioimaging/minDE_script.py | 62 +- bioimaging/moriga_handler.py | 873 ++++---- bioimaging/moriga_rewrite_script.py | 54 +- bioimaging/moriga_script.py | 56 +- bioimaging/nikon_oscillator.py | 60 +- bioimaging/nikon_repressilator.py | 82 +- bioimaging/palm_configs.py | 18 +- bioimaging/palm_handler.py | 835 ++++--- bioimaging/palm_rewrite_script.py | 80 +- bioimaging/palm_script.py | 86 +- bioimaging/parameter_configs.py | 14 +- bioimaging/parameter_effects.py | 1 - bioimaging/pmt_handler.py | 296 ++- bioimaging/pmt_script.py | 30 +- bioimaging/pointscan_confm_handler.py | 994 +++++---- bioimaging/pointscan_confm_script.py | 61 +- bioimaging/pten_lscm_script.py | 99 +- bioimaging/tirfm_handler.py | 49 +- bioimaging/tirfm_rewrite_script.py | 68 +- bioimaging/tirfm_script.py | 82 +- bioimaging/tmr_lscm_script.py | 65 +- bioimaging/tmr_tirfm_script.py | 88 +- bioimaging/twostate_tirfm_script.py | 78 +- 66 files changed, 7824 insertions(+), 7940 deletions(-) diff --git a/bioimaging/NEC_tirfm_script.py b/bioimaging/NEC_tirfm_script.py index 7ad9d27..8db3a26 100755 --- a/bioimaging/NEC_tirfm_script.py +++ b/bioimaging/NEC_tirfm_script.py @@ -12,57 +12,55 @@ def test_tirfm(t0, t1, beam, dist=None) : - # create TIRF Microscopy - tirfm = TIRFMConfigs() - tirfm.set_LightSource(source_type='LASER', wave_length=532, flux_density=beam, angle=65.7) - tirfm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') - tirfm.set_DichroicMirror('FF562-Di03-25x36') - tirfm.set_EmissionFilter('FF01-593_40-25') - tirfm.set_Magnification(Mag=100) - #tirfm.set_Magnification(Mag=250) - - # Detector : CMOS Camera - tirfm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ - focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.73) - tirfm.set_ADConverter(bit=16, offset=100, fullwell=30000) -# # Detector : EMCCD Camera -# tirfm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, \ -# focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.92, readout_noise=100, emgain=300) -# tirfm.set_ADConverter(bit=16, offset=2000, fullwell=370000) - - ### Output data - #tifm.set_OutputData(image_file_dir='./images') - #tirfm.set_OutputData(image_file_dir='./numpys_test') - #tirfm.set_OutputData(image_file_dir='./numpys_nec/numpys_nec_2A_%02dw_cmos_%03dnm' % (beam, dist)) - #tirfm.set_OutputData(image_file_dir='./numpys_nec/numpys_nec_2A_%02dw_emccd_%03dnm' % (beam, dist)) - #tirfm.set_OutputData(image_file_dir='./numpys_nec/numpys_nec_100A_%02dw_cmos' % (beam)) - #tirfm.set_OutputData(image_file_dir='./numpys_nec/numpys_nec_100A_%02dw_emccd' % (beam)) - tirfm.set_OutputData(image_file_dir='./numpys_nec_03/numpys_nec_05000A_20w_cmos') - - ### Input data - #tirfm.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_nec_2A', start=t0, end=t1, dist_nm=dist) - tirfm.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_nec_05000A', start=t0, end=t1) - - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0.1) - physics.set_fluorescence(quantum_yield=1.00, abs_coefficient=100000) - #physics.set_photobleaching(tau0=1.8, alpha=0.73) - #physics.set_photoactivation(turn_on_ratio=1000, activation_yield=0.1, frac_preactivation=0.00) - - # create image and movie - create = TIRFMVisualizer(configs=tirfm, effects=physics) - create.output_frames(num_div=16) + # create TIRF Microscopy + tirfm = TIRFMConfigs() + tirfm.set_LightSource(source_type='LASER', wave_length=532, flux_density=beam, angle=65.7) + tirfm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') + tirfm.set_DichroicMirror('FF562-Di03-25x36') + tirfm.set_EmissionFilter('FF01-593_40-25') + tirfm.set_Magnification(Mag=100) + #tirfm.set_Magnification(Mag=250) + + # Detector : CMOS Camera + tirfm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ + focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.73) + tirfm.set_ADConverter(bit=16, offset=100, fullwell=30000) +# # Detector : EMCCD Camera +# tirfm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, \ +# focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.92, readout_noise=100, emgain=300) +# tirfm.set_ADConverter(bit=16, offset=2000, fullwell=370000) + + ### Output data + #tifm.set_OutputData(image_file_dir='./images') + #tirfm.set_OutputData(image_file_dir='./numpys_test') + #tirfm.set_OutputData(image_file_dir='./numpys_nec/numpys_nec_2A_%02dw_cmos_%03dnm' % (beam, dist)) + #tirfm.set_OutputData(image_file_dir='./numpys_nec/numpys_nec_2A_%02dw_emccd_%03dnm' % (beam, dist)) + #tirfm.set_OutputData(image_file_dir='./numpys_nec/numpys_nec_100A_%02dw_cmos' % (beam)) + #tirfm.set_OutputData(image_file_dir='./numpys_nec/numpys_nec_100A_%02dw_emccd' % (beam)) + tirfm.set_OutputData(image_file_dir='./numpys_nec_03/numpys_nec_05000A_20w_cmos') + + ### Input data + #tirfm.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_nec_2A', start=t0, end=t1, dist_nm=dist) + tirfm.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_nec_05000A', start=t0, end=t1) + + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0.1) + physics.set_fluorescence(quantum_yield=1.00, abs_coefficient=100000) + #physics.set_photobleaching(tau0=1.8, alpha=0.73) + #physics.set_photoactivation(turn_on_ratio=1000, activation_yield=0.1, frac_preactivation=0.00) + + # create image and movie + create = TIRFMVisualizer(configs=tirfm, effects=physics) + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - beam = 20#float(sys.argv[3]) - dist = 200#float(sys.argv[4]) - - test_tirfm(t0, t1, beam, dist) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + beam = 20#float(sys.argv[3]) + dist = 200#float(sys.argv[4]) + test_tirfm(t0, t1, beam, dist) diff --git a/bioimaging/analyses_correlation.py b/bioimaging/analyses_correlation.py index 10e40c9..928449e 100644 --- a/bioimaging/analyses_correlation.py +++ b/bioimaging/analyses_correlation.py @@ -8,40 +8,36 @@ def get_auto_corr(I0, It) : - length = len(It[:,2]) - mu = It[:,2].sum()/length - s2 = (It[:,2] - mu)**2 - s = numpy.sqrt(s2.sum()/(length-1)) + length = len(It[:,2]) + mu = It[:,2].sum()/length + s2 = (It[:,2] - mu)**2 + s = numpy.sqrt(s2.sum()/(length-1)) - cor = (I0[:,2] - mu)*(It[:,2] - mu) - avg = cor.sum()/len(cor) + cor = (I0[:,2] - mu)*(It[:,2] - mu) + avg = cor.sum()/len(cor) - C_auto = avg/(mu*mu) + C_auto = avg/(mu*mu) - return C_auto + return C_auto if __name__=='__main__': - file_in = '/home/masaki/bioimaging_4public/images_fcs_2StMD_A200' - #file_in = '/home/masaki/bioimaging_4public/images_fcs_D010_A100' - #file_in = '/home/masaki/bioimaging_4public/images_fcs_D010_A200' - #file_in = '/home/masaki/bioimaging_4public/images_fcs_D100_A100' - #file_in = '/home/masaki/bioimaging_4public/images_fcs_D100_A200' + file_in = '/home/masaki/bioimaging_4public/images_fcs_2StMD_A200' + #file_in = '/home/masaki/bioimaging_4public/images_fcs_D010_A100' + #file_in = '/home/masaki/bioimaging_4public/images_fcs_D010_A200' + #file_in = '/home/masaki/bioimaging_4public/images_fcs_D100_A100' + #file_in = '/home/masaki/bioimaging_4public/images_fcs_D100_A200' - array = numpy.load(file_in + '/image_all.npy') + array = numpy.load(file_in + '/image_all.npy') - N = len(array)/2 - - for i in range(N) : - - I0 = array[0:N] - It = array[i:i+N] - - C_auto= get_auto_corr(I0, It) - - print(It[i,0], It[i,2], C_auto) + N = len(array)/2 + for i in range(N) : + I0 = array[0:N] + It = array[i:i+N] + C_auto= get_auto_corr(I0, It) + print(It[i,0], It[i,2], C_auto) diff --git a/bioimaging/analyses_moriga_fitting.py b/bioimaging/analyses_moriga_fitting.py index 2adb6d5..eba4653 100644 --- a/bioimaging/analyses_moriga_fitting.py +++ b/bioimaging/analyses_moriga_fitting.py @@ -18,126 +18,123 @@ def gaussian(A, x0, y0, width_x, width_y) : - """Returns a gaussian function with the given parameters""" - width_x = float(width_x) - width_y = float(width_y) + """Returns a gaussian function with the given parameters""" + width_x = float(width_x) + width_y = float(width_y) - return lambda x,y: A*numpy.exp(-(((x0-x)/width_x)**2+((y0-y)/width_y)**2)/2) + return lambda x,y: A*numpy.exp(-(((x0-x)/width_x)**2+((y0-y)/width_y)**2)/2) def moments(data) : - """Returns (A, x, y, width_x, width_y) - the gaussian parameters of a 2D distribution by calculating its - moments """ - total = data.sum() + """Returns (A, x, y, width_x, width_y) + the gaussian parameters of a 2D distribution by calculating its + moments """ + total = data.sum() - X, Y = numpy.indices(data.shape) - x = (X*data).sum()/total - y = (Y*data).sum()/total + X, Y = numpy.indices(data.shape) + x = (X*data).sum()/total + y = (Y*data).sum()/total - col = data[:, int(y)] - width_x = numpy.sqrt(abs((numpy.arange(col.size)-y)**2*col).sum()/col.sum()) + col = data[:, int(y)] + width_x = numpy.sqrt(abs((numpy.arange(col.size)-y)**2*col).sum()/col.sum()) - row = data[int(x), :] - width_y = numpy.sqrt(abs((numpy.arange(row.size)-x)**2*row).sum()/row.sum()) + row = data[int(x), :] + width_y = numpy.sqrt(abs((numpy.arange(row.size)-x)**2*row).sum()/row.sum()) - height = data.max() + height = data.max() - return height, x, y, width_x, width_y + return height, x, y, width_x, width_y def convert(file_in, file_out, index=None) : - i = 1 + i = 1 - #image = numpy.zeros(shape=(512,512)) - signal = [] - background = [] - error = [] + #image = numpy.zeros(shape=(512,512)) + signal = [] + background = [] + error = [] - while (True) : - try : - #input_image = numpy.load(file_in + '/image_%07d.npy' % (i)) - input_image = (imread(file_in + '/image_%07d.tif' % (i-1))).astype('int') - except Exception : - break + while (True) : + try : + #input_image = numpy.load(file_in + '/image_%07d.npy' % (i)) + input_image = (imread(file_in + '/image_%07d.tif' % (i-1))).astype('int') + except Exception : + break - image0 = numpy.array(input_image) + image0 = numpy.array(input_image) - for j in range(len(coord)) : + for j in range(len(coord)) : - if (i-1 == coord[j][0]) : + if (i-1 == coord[j][0]) : - x, y = coord[j][1], coord[j][2] + x, y = coord[j][1], coord[j][2] - image1 = input_image[y-8:y+8,x-8:x+8] - image0[y-8:y+8,x-8:x+8] = image0[y-8:y+8,x-8:x+8] - image1 + image1 = input_image[y-8:y+8,x-8:x+8] + image0[y-8:y+8,x-8:x+8] = image0[y-8:y+8,x-8:x+8] - image1 - I_all = float(image1.sum()) - signal.append(I_all) + I_all = float(image1.sum()) + signal.append(I_all) -# params = moments(image0) -# errorfunction = lambda p : numpy.ravel(gaussian(*p)(*numpy.indices(image0.shape)) - image0) -# p, success = optimize.leastsq(errorfunction, params) +# params = moments(image0) +# errorfunction = lambda p : numpy.ravel(gaussian(*p)(*numpy.indices(image0.shape)) - image0) +# p, success = optimize.leastsq(errorfunction, params) - I_bg = [] - array0 = image0.reshape((512*512)) + I_bg = [] + array0 = image0.reshape((512*512)) - for k in range(len(array0)) : + for k in range(len(array0)) : - if (array0[k] > 0) : - I_bg.append(float(array0[k])) + if (array0[k] > 0) : + I_bg.append(float(array0[k])) - I_bg = numpy.array(I_bg) + I_bg = numpy.array(I_bg) - length = len(I_bg) + length = len(I_bg) - b_avg = I_bg.sum()/length - bdev2 = (I_bg - b_avg)**2 - dev_b = numpy.sqrt(bdev2.sum()/length) + b_avg = I_bg.sum()/length + bdev2 = (I_bg - b_avg)**2 + dev_b = numpy.sqrt(bdev2.sum()/length) - background.append(b_avg) - error.append(dev_b) + background.append(b_avg) + error.append(dev_b) - # 16-bit data format - #image_array.astype('uint16') - #toimage(image_array, high=cmax, low=cmin, mode='I').save(output_image) + # 16-bit data format + #image_array.astype('uint16') + #toimage(image_array, high=cmax, low=cmin, mode='I').save(output_image) - # 8-bit data format (for making movie) - #toimage(image, cmin=amin, cmax=410).save(output_image) + # 8-bit data format (for making movie) + #toimage(image, cmin=amin, cmax=410).save(output_image) - i += 1 + i += 1 - # background - background = numpy.array(background) - length = len(background) - b_avg = background.sum()/length + # background + background = numpy.array(background) + length = len(background) + b_avg = background.sum()/length - error = numpy.array(error) - length = len(error) - err_b = error.sum()/length + error = numpy.array(error) + length = len(error) + err_b = error.sum()/length - # signal - signal = numpy.array(signal - 16*16*b_avg) + # signal + signal = numpy.array(signal - 16*16*b_avg) - length = len(signal) - s_avg = signal.sum()/length - dev2 = (signal - s_avg)**2 - err_s = numpy.sqrt(dev2.sum()/length) + length = len(signal) + s_avg = signal.sum()/length + dev2 = (signal - s_avg)**2 + err_s = numpy.sqrt(dev2.sum()/length) - print(210, s_avg, err_s, b_avg, err_b) + print(210, s_avg, err_s, b_avg, err_b) - # 8-bit data format (for making movie) - #toimage(image, cmin=100, cmax=1000).save(file_out + '/image_summed.png') + # 8-bit data format (for making movie) + #toimage(image, cmin=100, cmax=1000).save(file_out + '/image_summed.png') if __name__=='__main__': - file_in = '/home/masaki/bioimaging_4public/Data_fromMoriga_08-05-2015/images_0805/images_tif_210mW' - file_out = '/home/masaki/bioimaging_4public/images_png' - - convert(file_in, file_out) - - + file_in = '/home/masaki/bioimaging_4public/Data_fromMoriga_08-05-2015/images_0805/images_tif_210mW' + file_out = '/home/masaki/bioimaging_4public/images_png' + convert(file_in, file_out) diff --git a/bioimaging/analyses_palm_data.py b/bioimaging/analyses_palm_data.py index 5836c5b..d4d421c 100644 --- a/bioimaging/analyses_palm_data.py +++ b/bioimaging/analyses_palm_data.py @@ -13,65 +13,64 @@ def convert(file_in, file_out, index=None) : - i = 1 + i = 1 - while (True) : - try : - #input_image = numpy.load(file_in + '/image_%07d.npy' % (i)) - input_image_0 = (imread(file_in + '/image_%07d.tif' % (i-1))).astype('int') - input_image_1 = (imread(file_in + '/image_%07d.tif' % (i))).astype('int') - except Exception : - break + while (True) : + try : + #input_image = numpy.load(file_in + '/image_%07d.npy' % (i)) + input_image_0 = (imread(file_in + '/image_%07d.tif' % (i-1))).astype('int') + input_image_1 = (imread(file_in + '/image_%07d.tif' % (i))).astype('int') + except Exception : + break - output_image = file_out + '/image_%07d.png' % (i-1) + output_image = file_out + '/image_%07d.png' % (i-1) - # data for tirfm - diff_image = input_image_0 - input_image_1 - hist_image_1 = input_image_1.reshape((512*512)) + # data for tirfm + diff_image = input_image_0 - input_image_1 + hist_image_1 = input_image_1.reshape((512*512)) - length = len(hist_image_1) + length = len(hist_image_1) - avg = hist_image_1.sum()/length - dev = numpy.sqrt(avg) + avg = hist_image_1.sum()/length + dev = numpy.sqrt(avg) - check_diff = (diff_image > 0).astype('int') - image0 = numpy.abs(diff_image*check_diff) + check_diff = (diff_image > 0).astype('int') + image0 = numpy.abs(diff_image*check_diff) - ####### - length = 512*512 - n = 16 - N = length/(n*n) - image1 = image0.reshape((N,n,n)) + ####### + length = 512*512 + n = 16 + N = length/(n*n) + image1 = image0.reshape((N,n,n)) - avg = image1.sum(axis=1).sum(axis=1)/float(n*n) - check1 = (avg > 0).astype('int') + avg = image1.sum(axis=1).sum(axis=1)/float(n*n) + check1 = (avg > 0).astype('int') - for j in range(N) : - image1[j] = numpy.abs(image1[j]*check1[j]) + for j in range(N) : + image1[j] = numpy.abs(image1[j]*check1[j]) - image = image1.reshape((512,512)) - #image = input_image_0.reshape((512,512)) + image = image1.reshape((512,512)) + #image = input_image_0.reshape((512,512)) - amin = numpy.amin(image) - amax = numpy.amax(image) + amin = numpy.amin(image) + amax = numpy.amax(image) - print(amin, amax) + print(amin, amax) - # 16-bit data format - #image_array.astype('uint16') - #toimage(image_array, high=cmax, low=cmin, mode='I').save(output_image) + # 16-bit data format + #image_array.astype('uint16') + #toimage(image_array, high=cmax, low=cmin, mode='I').save(output_image) - # 8-bit data format (for making movie) - toimage(image, cmin=amin, cmax=500).save(output_image) + # 8-bit data format (for making movie) + toimage(image, cmin=amin, cmax=500).save(output_image) - i += 1 + i += 1 if __name__=='__main__': - file_in = '/home/masaki/bioimaging_4public/Data_fromMoriga_08-05-2015/images_0805/images_tif_220mW' - file_out = '/home/masaki/bioimaging_4public/images_png' - - convert(file_in, file_out) + file_in = '/home/masaki/bioimaging_4public/Data_fromMoriga_08-05-2015/images_0805/images_tif_220mW' + file_out = '/home/masaki/bioimaging_4public/images_png' + convert(file_in, file_out) diff --git a/bioimaging/analyses_sim_fitting.py b/bioimaging/analyses_sim_fitting.py index d484a54..78d4b94 100644 --- a/bioimaging/analyses_sim_fitting.py +++ b/bioimaging/analyses_sim_fitting.py @@ -11,86 +11,85 @@ from scipy.misc import imread, toimage def gaussian(A, x0, y0, width_x, width_y) : - """Returns a gaussian function with the given parameters""" - width_x = float(width_x) - width_y = float(width_y) + """Returns a gaussian function with the given parameters""" + width_x = float(width_x) + width_y = float(width_y) - return lambda x,y: A*numpy.exp(-(((x0-x)/width_x)**2+((y0-y)/width_y)**2)/2) + return lambda x,y: A*numpy.exp(-(((x0-x)/width_x)**2+((y0-y)/width_y)**2)/2) def moments(data) : - """Returns (A, x, y, width_x, width_y) - the gaussian parameters of a 2D distribution by calculating its - moments """ - total = data.sum() + """Returns (A, x, y, width_x, width_y) + the gaussian parameters of a 2D distribution by calculating its + moments """ + total = data.sum() - X, Y = numpy.indices(data.shape) - x = (X*data).sum()/total - y = (Y*data).sum()/total + X, Y = numpy.indices(data.shape) + x = (X*data).sum()/total + y = (Y*data).sum()/total - col = data[:, int(y)] - width_x = numpy.sqrt(abs((numpy.arange(col.size)-y)**2*col).sum()/col.sum()) + col = data[:, int(y)] + width_x = numpy.sqrt(abs((numpy.arange(col.size)-y)**2*col).sum()/col.sum()) - row = data[int(x), :] - width_y = numpy.sqrt(abs((numpy.arange(row.size)-x)**2*row).sum()/row.sum()) + row = data[int(x), :] + width_y = numpy.sqrt(abs((numpy.arange(row.size)-x)**2*row).sum()/row.sum()) - height = data.max() + height = data.max() - return height, x, y, width_x, width_y + return height, x, y, width_x, width_y def convert(file_in, file_out) : - Np = 6.5e-6/89/1e-9 - x_nm, y_nm = 2.5057e-05/1e-9, 1.432e-05/1e-9 - x_px, y_px = 94 + x_nm/Np - 44, 94 + y_nm/Np - 44 + Np = 6.5e-6/89/1e-9 + x_nm, y_nm = 2.5057e-05/1e-9, 1.432e-05/1e-9 + x_px, y_px = 94 + x_nm/Np - 44, 94 + y_nm/Np - 44 - proc = subprocess.Popen(["ls %s" % (file_in)], stdout=subprocess.PIPE, shell=True) - (out, err) = proc.communicate() - filename = out.split('\n') + proc = subprocess.Popen(["ls %s" % (file_in)], stdout=subprocess.PIPE, shell=True) + (out, err) = proc.communicate() + filename = out.split('\n') - for i in range(len(filename)) : + for i in range(len(filename)) : - try : - input_image = numpy.load(file_in + '/' + filename[i]).astype('int') - except Exception : - break + try : + input_image = numpy.load(file_in + '/' + filename[i]).astype('int') + except Exception : + break - array_image = input_image[300-256:300+256,300-256:300+256,1] + array_image = input_image[300-256:300+256,300-256:300+256,1] - # get intensity - temp0 = filename[i].split('_') - temp1 = temp0[1].split('.npy') - intensity = float(temp1[0]) + # get intensity + temp0 = filename[i].split('_') + temp1 = temp0[1].split('.npy') + intensity = float(temp1[0]) - # histogram - hist_image = array_image.reshape((512*512)) + # histogram + hist_image = array_image.reshape((512*512)) - length = len(hist_image) + length = len(hist_image) - B_avg = hist_image.sum()/float(length) - Bdev2 = (hist_image - B_avg)**2 - B_dev = numpy.sqrt(Bdev2.sum()/length) + B_avg = hist_image.sum()/float(length) + Bdev2 = (hist_image - B_avg)**2 + B_dev = numpy.sqrt(Bdev2.sum()/length) - x, y = 394, 247 - image1 = array_image[y-8:y+8,x-8:x+8] - B_avg - check1 = (image1 > 0).astype('int') - image0 = numpy.abs(check1*image1) - I_sum = image0.sum() + x, y = 394, 247 + image1 = array_image[y-8:y+8,x-8:x+8] - B_avg + check1 = (image1 > 0).astype('int') + image0 = numpy.abs(check1*image1) + I_sum = image0.sum() - params = moments(image0) - errorfunction = lambda p : numpy.ravel(gaussian(*p)(*numpy.indices(image0.shape)) - image0) - p, success = optimize.leastsq(errorfunction, params) + params = moments(image0) + errorfunction = lambda p : numpy.ravel(gaussian(*p)(*numpy.indices(image0.shape)) - image0) + p, success = optimize.leastsq(errorfunction, params) - print(intensity, x_px-(x-8), y_px-(y-8), I_sum, p[2], p[1], B_avg, B_dev) - #print intensity, x_px-(x-8), y_px-(y-8), p, success, B_avg, B_dev + print(intensity, x_px-(x-8), y_px-(y-8), I_sum, p[2], p[1], B_avg, B_dev) + #print intensity, x_px-(x-8), y_px-(y-8), p, success, B_avg, B_dev if __name__=='__main__': - file_in = '/home/masaki/bioimaging_4public/numpys_4moriga_210mW' - file_out = '/home/masaki/bioimaging_4public/images_png' - - convert(file_in, file_out) + file_in = '/home/masaki/bioimaging_4public/numpys_4moriga_210mW' + file_out = '/home/masaki/bioimaging_4public/images_png' + convert(file_in, file_out) diff --git a/bioimaging/analyses_sim_hist.py b/bioimaging/analyses_sim_hist.py index 9229c2e..deaa651 100644 --- a/bioimaging/analyses_sim_hist.py +++ b/bioimaging/analyses_sim_hist.py @@ -14,88 +14,88 @@ def get_data(data_array, flux_mW) : - # read txt file - gROOT.Reset() - canvas = TCanvas() - - #### - psfile = './hist_%dmW.pdf' % (flux_mW) - canvas.Print(psfile+"[") - - ntuple = TNtuple("data", "data", "I_true:x_true:y_true:I_reco:x_reco:y_reco:B_avg:B_dev") - - # set data point - for item in data_array : - #item = data.split(',') - ntuple.Fill(item[0], item[1], item[2], item[3], item[4], item[5], item[6], item[7]) - - Nbins = 40 - - #### 1D histogram of photons - hist_dx = TH1F("hist_dx", "%d mW : x_true - x_reco" % (flux_mW), Nbins, -4, 4) - hist_dx.GetXaxis().SetTitle("x_true - x_reco [pixel]") - #hist_dx.GetYaxis().SetTitle("Number") - hist_dx.SetLineColor(1) - hist_dx.SetLineWidth(2) - #hist_dx.SetStats(0) - ntuple.Draw("x_true-x_reco >> hist_dx") - canvas.Print(psfile) - - #### 1D histogram of photons - hist_dy = TH1F("hist_dy", "%d mW : y_true - y_reco" % (flux_mW), Nbins, -4, 4) - hist_dy.GetXaxis().SetTitle("y_true - y_reco [pixel]") - #hist_dy.GetYaxis().SetTitle("Number") - hist_dy.SetLineColor(1) - hist_dy.SetLineWidth(2) - #hist_dy.SetStats(0) - ntuple.Draw("y_true-y_reco >> hist_dy") - canvas.Print(psfile) - - #### 1D histogram of photons - hist_dr = TH1F("hist_dr", "%d mW : r_true - r_reco" % (flux_mW), Nbins, 0, 4) - hist_dr.GetXaxis().SetTitle("r_true - r_reco [pixel]") - #hist_dr.GetYaxis().SetTitle("Number") - hist_dr.SetLineColor(1) - hist_dr.SetLineWidth(2) - #hist_dr.SetStats(0) - ntuple.Draw("sqrt((x_true-x_reco)**2 + (y_true-y_reco)**2) >> hist_dr") - canvas.Print(psfile) - - #### 1D histogram of photons - hist_dI = TH1F("hist_dI", "%d mW : I_true - I_reco [counts]" % (flux_mW), Nbins, -4000, 6000) - hist_dI.GetXaxis().SetTitle("I_true - I_reco [counts]") - #hist_dI.GetYaxis().SetTitle("Number") - hist_dI.SetLineColor(1) - hist_dI.SetLineWidth(2) - #hist_dI.SetStats(0) - ntuple.Draw("I_true-I_reco >> hist_dI") - canvas.Print(psfile) - - ########## - hist_2d_px = TH2F("hist_2d_px", "%d mW : Local precision [pixel] vs Intensity [counts]" % (flux_mW), Nbins, 0, 8000, Nbins, 0, 4) - hist_2d_px.GetYaxis().SetTitle("Local precision [pixel]") - hist_2d_px.GetXaxis().SetTitle("True Intensity [counts]") - #hist_2d_px.SetStats(0) - ntuple.Draw("sqrt((x_true-x_reco)**2 + (y_true-y_reco)**2):I_true >> hist_2d_px", "", "zcol") - canvas.Print(psfile) - - ########## - hist_2d_nm = TH2F("hist_2d_nm", "%d mW : Local precision [nm] vs Intensity [counts]" % (flux_mW), Nbins, 0, 8000, Nbins, 0, 400) - hist_2d_nm.GetYaxis().SetTitle("Local precision [nm]") - hist_2d_nm.GetXaxis().SetTitle("True Intensity [counts]") - #hist_2d_nm.SetStats(0) - ntuple.Draw("sqrt((x_true-x_reco)**2 + (y_true-y_reco)**2)*73:I_true >> hist_2d_nm", "", "zcol") - canvas.Print(psfile) - - ########## - hist_2d = TH2F("hist_2d", "%d mW : Local precision [nm] vs Intensity precision [counts]" % (flux_mW), Nbins, -4000, 4000, Nbins, 0, 400) - hist_2d.GetYaxis().SetTitle("Local precision [nm]") - hist_2d.GetXaxis().SetTitle("Intensity precision [counts]") - #hist_2d.SetStats(0) - ntuple.Draw("sqrt((x_true-x_reco)**2 + (y_true-y_reco)**2)*73:(I_true-I_reco) >> hist_2d", "", "zcol") - canvas.Print(psfile) - - canvas.Print(psfile+']') + # read txt file + gROOT.Reset() + canvas = TCanvas() + + #### + psfile = './hist_%dmW.pdf' % (flux_mW) + canvas.Print(psfile+"[") + + ntuple = TNtuple("data", "data", "I_true:x_true:y_true:I_reco:x_reco:y_reco:B_avg:B_dev") + + # set data point + for item in data_array : + #item = data.split(',') + ntuple.Fill(item[0], item[1], item[2], item[3], item[4], item[5], item[6], item[7]) + + Nbins = 40 + + #### 1D histogram of photons + hist_dx = TH1F("hist_dx", "%d mW : x_true - x_reco" % (flux_mW), Nbins, -4, 4) + hist_dx.GetXaxis().SetTitle("x_true - x_reco [pixel]") + #hist_dx.GetYaxis().SetTitle("Number") + hist_dx.SetLineColor(1) + hist_dx.SetLineWidth(2) + #hist_dx.SetStats(0) + ntuple.Draw("x_true-x_reco >> hist_dx") + canvas.Print(psfile) + + #### 1D histogram of photons + hist_dy = TH1F("hist_dy", "%d mW : y_true - y_reco" % (flux_mW), Nbins, -4, 4) + hist_dy.GetXaxis().SetTitle("y_true - y_reco [pixel]") + #hist_dy.GetYaxis().SetTitle("Number") + hist_dy.SetLineColor(1) + hist_dy.SetLineWidth(2) + #hist_dy.SetStats(0) + ntuple.Draw("y_true-y_reco >> hist_dy") + canvas.Print(psfile) + + #### 1D histogram of photons + hist_dr = TH1F("hist_dr", "%d mW : r_true - r_reco" % (flux_mW), Nbins, 0, 4) + hist_dr.GetXaxis().SetTitle("r_true - r_reco [pixel]") + #hist_dr.GetYaxis().SetTitle("Number") + hist_dr.SetLineColor(1) + hist_dr.SetLineWidth(2) + #hist_dr.SetStats(0) + ntuple.Draw("sqrt((x_true-x_reco)**2 + (y_true-y_reco)**2) >> hist_dr") + canvas.Print(psfile) + + #### 1D histogram of photons + hist_dI = TH1F("hist_dI", "%d mW : I_true - I_reco [counts]" % (flux_mW), Nbins, -4000, 6000) + hist_dI.GetXaxis().SetTitle("I_true - I_reco [counts]") + #hist_dI.GetYaxis().SetTitle("Number") + hist_dI.SetLineColor(1) + hist_dI.SetLineWidth(2) + #hist_dI.SetStats(0) + ntuple.Draw("I_true-I_reco >> hist_dI") + canvas.Print(psfile) + + ########## + hist_2d_px = TH2F("hist_2d_px", "%d mW : Local precision [pixel] vs Intensity [counts]" % (flux_mW), Nbins, 0, 8000, Nbins, 0, 4) + hist_2d_px.GetYaxis().SetTitle("Local precision [pixel]") + hist_2d_px.GetXaxis().SetTitle("True Intensity [counts]") + #hist_2d_px.SetStats(0) + ntuple.Draw("sqrt((x_true-x_reco)**2 + (y_true-y_reco)**2):I_true >> hist_2d_px", "", "zcol") + canvas.Print(psfile) + + ########## + hist_2d_nm = TH2F("hist_2d_nm", "%d mW : Local precision [nm] vs Intensity [counts]" % (flux_mW), Nbins, 0, 8000, Nbins, 0, 400) + hist_2d_nm.GetYaxis().SetTitle("Local precision [nm]") + hist_2d_nm.GetXaxis().SetTitle("True Intensity [counts]") + #hist_2d_nm.SetStats(0) + ntuple.Draw("sqrt((x_true-x_reco)**2 + (y_true-y_reco)**2)*73:I_true >> hist_2d_nm", "", "zcol") + canvas.Print(psfile) + + ########## + hist_2d = TH2F("hist_2d", "%d mW : Local precision [nm] vs Intensity precision [counts]" % (flux_mW), Nbins, -4000, 4000, Nbins, 0, 400) + hist_2d.GetYaxis().SetTitle("Local precision [nm]") + hist_2d.GetXaxis().SetTitle("Intensity precision [counts]") + #hist_2d.SetStats(0) + ntuple.Draw("sqrt((x_true-x_reco)**2 + (y_true-y_reco)**2)*73:(I_true-I_reco) >> hist_2d", "", "zcol") + canvas.Print(psfile) + + canvas.Print(psfile+']') if __name__=='__main__': @@ -105,6 +105,3 @@ def get_data(data_array, flux_mW) : data = numpy.genfromtxt('./text_%dmW.dat' % (flux_mW)) get_data(data, flux_mW) - - - diff --git a/bioimaging/bleaching_configs.py b/bioimaging/bleaching_configs.py index bbcf4b7..b7403ca 100755 --- a/bioimaging/bleaching_configs.py +++ b/bioimaging/bleaching_configs.py @@ -2,7 +2,7 @@ import numpy #----------------------------- -# General +# General #----------------------------- ignore_open_errors = False electron_charge = 1.602e-19 # C @@ -27,19 +27,19 @@ #fluorophore_rgb = numpy.array([(0, 0, 0) for j in range(len(depth))]) #----------------------------- -# Fluorophore PSF +# Fluorophore PSF #----------------------------- psf_wavelength = 600 # nm psf_intensity = 1.00 psf_intensity_noise = 0.50 -psf_width = (200, 200) # Gaussian function (radial width, lateral width) [nm] -psf_cutoff = (400, 100) # cutoff range (radius, depth) -psf_file_name_format = 'psf_%04d.png' # Image file name +psf_width = (200, 200) # Gaussian function (radial width, lateral width) [nm] +psf_cutoff = (400, 100) # cutoff range (radius, depth) +psf_file_name_format = 'psf_%04d.png' # Image file name #----------------------------- # Illumination -#----------------------------- -source_switch = False +#----------------------------- +source_switch = False source_type = 'LASER' source_wavelength = 600. # nm source_radius = 20e-6 # m diff --git a/bioimaging/blinking_script.py b/bioimaging/blinking_script.py index ab52813..7f21311 100755 --- a/bioimaging/blinking_script.py +++ b/bioimaging/blinking_script.py @@ -7,59 +7,57 @@ def test_palm(t0, t1) : -# # create PALM imaging -# palm = PALMConfigs() -# palm.set_ExcitationSource(source_type='LASER', wave_length=561, flux_density=30, angle=65.7) -# palm.set_ActivationSource(source_type='LASER', wave_length=504, flux_density=20, angle=65.7, frame_time=0.5, F_bleach=50) -# palm.set_Fluorophore(fluorophore_type='EGFP') -## palm.set_DichroicMirror('FF562-Di03-25x36') -## palm.set_EmissionFilter('FF01-593_40-25') -# palm.set_Magnification(Mag=100) +# # create PALM imaging +# palm = PALMConfigs() +# palm.set_ExcitationSource(source_type='LASER', wave_length=561, flux_density=30, angle=65.7) +# palm.set_ActivationSource(source_type='LASER', wave_length=504, flux_density=20, angle=65.7, frame_time=0.5, F_bleach=50) +# palm.set_Fluorophore(fluorophore_type='EGFP') +## palm.set_DichroicMirror('FF562-Di03-25x36') +## palm.set_EmissionFilter('FF01-593_40-25') +# palm.set_Magnification(Mag=100) # -# # Detector : CMOS Camera -# palm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ -# focal_point=(0.0,0.5,0.5), exposure_time=0.5, QE=0.73) -# palm.set_ADConverter(bit=16, offset=100, fullwell=30000) +# # Detector : CMOS Camera +# palm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ +# focal_point=(0.0,0.5,0.5), exposure_time=0.5, QE=0.73) +# palm.set_ADConverter(bit=16, offset=100, fullwell=30000) # -## # Detector : EMCCD Camera -## palm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, \ -## focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.92, readout_noise=50, emgain=300) -## palm.set_ADConverter(bit=14, offset=2000, fullwell=800000) +## # Detector : EMCCD Camera +## palm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, \ +## focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.92, readout_noise=50, emgain=300) +## palm.set_ADConverter(bit=14, offset=2000, fullwell=800000) ## -## # Output data -## palm.set_OutputData(image_file_dir='./numpys_palm_01000A') +## # Output data +## palm.set_OutputData(image_file_dir='./numpys_palm_01000A') # -# # Input data -# palm.reset_InputData('/home/masaki/ecell3/latest/data/csv/beads_palm_10000A', start=t0, end=t1, observable="A") -# #palm.set_InputData('/home/masaki/bioimaging_4public/data_palm_01000A', start=t0, end=t1, observable="A") +# # Input data +# palm.reset_InputData('/home/masaki/ecell3/latest/data/csv/beads_palm_10000A', start=t0, end=t1, observable="A") +# #palm.set_InputData('/home/masaki/bioimaging_4public/data_palm_01000A', start=t0, end=t1, observable="A") - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0) - physics.set_fluorescence(photon_budget=200, quantum_yield=0.61, abs_coefficient=83400, alpha=0.10) - physics.set_photoactivation(turn_on_ratio=1000, activation_yield=1.0, frac_preactivation=0.00) - physics.set_photoblinking(t0_on=0.30, a_on=0.58, t0_off=10e-6, a_off=0.48) + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0) + physics.set_fluorescence(photon_budget=200, quantum_yield=0.61, abs_coefficient=83400, alpha=0.10) + physics.set_photoactivation(turn_on_ratio=1000, activation_yield=1.0, frac_preactivation=0.00) + physics.set_photoblinking(t0_on=0.30, a_on=0.58, t0_off=10e-6, a_off=0.48) - dt = 0.2 - n_emit0 = 1 - physics.set_photophysics(t0, t1, dt, 1, 1, n_emit0, 1) - budget = physics.fluorescence_budget[0] - state = physics.fluorescence_state[0] + dt = 0.2 + n_emit0 = 1 + physics.set_photophysics(t0, t1, dt, 1, 1, n_emit0, 1) + budget = physics.fluorescence_budget[0] + state = physics.fluorescence_state[0] - print('#', budget, 'photons', (budget/n_emit0)*dt, 'sec') + print('#', budget, 'photons', (budget/n_emit0)*dt, 'sec') - for i in range(len(state)) : - print(dt*i, state[i], budget) + for i in range(len(state)) : + print(dt*i, state[i], budget) - if (state[i] > 0) : - budget = budget - n_emit0 + if (state[i] > 0) : + budget = budget - n_emit0 if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_palm(t0, t1) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_palm(t0, t1) diff --git a/bioimaging/camera_handler.py b/bioimaging/camera_handler.py index 782add8..d2898a0 100755 --- a/bioimaging/camera_handler.py +++ b/bioimaging/camera_handler.py @@ -58,22 +58,22 @@ def __init__(self, user_configs_dict = None): def _set_data(self, key, val) : - + if val != None: setattr(self, key, val) def set_Detector(self, detector = None, - image_size = None, + image_size = None, pixel_length = None, focal_point = None, base_position = None, exposure_time = None, - QE = None, - readout_noise = None, - dark_count = None, - emgain = None + QE = None, + readout_noise = None, + dark_count = None, + emgain = None ): @@ -89,69 +89,69 @@ def set_Detector(self, detector = None, self._set_data('detector_dark_count', dark_count) self._set_data('detector_emgain', emgain) - print('--- Detector : ', self.detector_type) + print('--- Detector : ', self.detector_type) print('\tImage Size = ', self.detector_image_size[0], 'x', self.detector_image_size[1]) print('\tPixel Size = ', self.detector_pixel_length, 'm/pixel') print('\tFocal Point = ', self.detector_focal_point) print('\tPosition = ', self.detector_base_position) print('\tExposure Time = ', self.detector_exposure_time, 'sec') print('\tQuantum Efficiency = ', 100*self.detector_qeff, '%') - print('\tReadout Noise = ', self.detector_readout_noise, 'electron') + print('\tReadout Noise = ', self.detector_readout_noise, 'electron') print('\tDark Count = ', self.detector_dark_count, 'electron/sec') print('\tEM gain = ', 'x', self.detector_emgain) def set_ADConverter(self, bit = None, - gain = None, - offset = None, - fullwell = None, - fpn_type = None, - fpn_count = None) : - - self._set_data('ADConverter_bit', bit) - self._set_data('ADConverter_gain', (fullwell - 0.0)/(pow(2.0, bit) - offset)) - self._set_data('ADConverter_offset', offset) + gain = None, + offset = None, + fullwell = None, + fpn_type = None, + fpn_count = None) : + + self._set_data('ADConverter_bit', bit) + self._set_data('ADConverter_gain', (fullwell - 0.0)/(pow(2.0, bit) - offset)) + self._set_data('ADConverter_offset', offset) self._set_data('ADConverter_fullwell', fullwell) self._set_data('ADConverter_fpn_type', fpn_type) self._set_data('ADConverter_fpn_count', fpn_count) print('--- A/D Converter : %d-bit' % (self.ADConverter_bit)) - print('\tGain = %.3f count/electron' % (self.ADConverter_gain)) - print('\tOffset = ', self.ADConverter_offset, 'count') + print('\tGain = %.3f count/electron' % (self.ADConverter_gain)) + print('\tOffset = ', self.ADConverter_offset, 'count') print('\tFullwell = ', self.ADConverter_fullwell, 'electron') print('\t%s-Fixed Pattern Noise :' % (self.ADConverter_fpn_type), self.ADConverter_fpn_count, 'count') - # image pixel-size - Nw_pixel = self.detector_image_size[0] - Nh_pixel = self.detector_image_size[1] + # image pixel-size + Nw_pixel = self.detector_image_size[0] + Nh_pixel = self.detector_image_size[1] - # set ADC parameters + # set ADC parameters bit = self.ADConverter_bit fullwell = self.ADConverter_fullwell - ADC0 = self.ADConverter_offset + ADC0 = self.ADConverter_offset - # set Fixed-Pattern noise + # set Fixed-Pattern noise FPN_type = self.ADConverter_fpn_type FPN_count = self.ADConverter_fpn_count - if (FPN_type == None) : - offset = numpy.array([ADC0 for i in range(Nw_pixel*Nh_pixel)]) + if (FPN_type == None) : + offset = numpy.array([ADC0 for i in range(Nw_pixel*Nh_pixel)]) - if (FPN_type == 'pixel') : - offset = numpy.rint(numpy.random.normal(ADC0, FPN_count, Nw_pixel*Nh_pixel)) + if (FPN_type == 'pixel') : + offset = numpy.rint(numpy.random.normal(ADC0, FPN_count, Nw_pixel*Nh_pixel)) - elif (FPN_type == 'column') : - column = numpy.random.normal(ADC0, FPN_count, Nh_pixel) - temporal = numpy.array([column for i in range(Nw_pixel)]) + elif (FPN_type == 'column') : + column = numpy.random.normal(ADC0, FPN_count, Nh_pixel) + temporal = numpy.array([column for i in range(Nw_pixel)]) - offset = numpy.rint(temporal.reshape(Nh_pixel*Nw_pixel)) + offset = numpy.rint(temporal.reshape(Nh_pixel*Nw_pixel)) - # set ADC gain + # set ADC gain gain = numpy.array(map(lambda x : (fullwell - 0.0)/(pow(2.0, bit) - x), offset)) - # reshape + # reshape self.ADConverter_offset = offset.reshape([Nw_pixel, Nh_pixel]) self.ADConverter_gain = gain.reshape([Nw_pixel, Nh_pixel]) @@ -159,202 +159,200 @@ def set_ADConverter(self, bit = None, def set_OutputData(self, image_file_dir = None, - image_file_cleanup_dir=False) : - - if image_file_dir is None : + image_file_cleanup_dir=False) : - image_file_dir = tempfile.mkdtemp(dir=os.getcwd()) - image_file_cleanup_dir = True - - self._set_data('image_file_dir', image_file_dir) - self._set_data('image_file_cleanup_dir', image_file_cleanup_dir) + if image_file_dir is None : + image_file_dir = tempfile.mkdtemp(dir=os.getcwd()) + image_file_cleanup_dir = True + self._set_data('image_file_dir', image_file_dir) + self._set_data('image_file_cleanup_dir', image_file_cleanup_dir) -class CameraVisualizer() : - ''' - Camera Visualization class of e-cell simulator - ''' - def __init__(self, configs=CameraConfigs()) : +class CameraVisualizer() : - assert isinstance(configs, CameraConfigs) - self.configs = configs + ''' + Camera Visualization class of e-cell simulator + ''' - """ - Check and create the folders for image and output files. - """ - if not os.path.exists(self.configs.image_file_dir): - os.makedirs(self.configs.image_file_dir) - #else: - # for file in os.listdir(self.configs.image_file_dir): - # os.remove(os.path.join(self.configs.image_file_dir, file)) + def __init__(self, configs=CameraConfigs()) : + assert isinstance(configs, CameraConfigs) + self.configs = configs + """ + Check and create the folders for image and output files. + """ + if not os.path.exists(self.configs.image_file_dir): + os.makedirs(self.configs.image_file_dir) + #else: + # for file in os.listdir(self.configs.image_file_dir): + # os.remove(os.path.join(self.configs.image_file_dir, file)) - def output_frames(self, index=0, signal=0, background=0): - Nw = self.configs.detector_image_size[0] - Nh = self.configs.detector_image_size[1] - photons = numpy.full([Nw, Nh], background) - photons[int(0.2*Nw):int(0.8*Nw),int(0.2*Nh):int(0.8*Nh)] += numpy.full([int(0.6*Nw),int(0.6*Nh)], signal) + def output_frames(self, index=0, signal=0, background=0): - camera = self.detector_output(photons) + Nw = self.configs.detector_image_size[0] + Nh = self.configs.detector_image_size[1] -# # save data to numpy-binary file -# image_file_name = os.path.join(self.configs.image_file_dir, -# self.configs.image_file_name_format % (index)) -# numpy.save(image_file_name, camera) + photons = numpy.full([Nw, Nh], background) + photons[int(0.2*Nw):int(0.8*Nw),int(0.2*Nh):int(0.8*Nh)] += numpy.full([int(0.6*Nw),int(0.6*Nh)], signal) + camera = self.detector_output(photons) +# # save data to numpy-binary file +# image_file_name = os.path.join(self.configs.image_file_dir, +# self.configs.image_file_name_format % (index)) +# numpy.save(image_file_name, camera) - def prob_EMCCD(self, S, E) : - # get EM gain - M = self.configs.detector_emgain - a = 1.00/M - if (S > 0) : - prob = numpy.sqrt(a*E/S)*numpy.exp(-a*S-E+2*numpy.sqrt(a*E*S))*i1e(2*numpy.sqrt(a*E*S)) - else : - prob = numpy.exp(-E) + def prob_EMCCD(self, S, E) : - return prob + # get EM gain + M = self.configs.detector_emgain + a = 1.00/M + if (S > 0) : + prob = numpy.sqrt(a*E/S)*numpy.exp(-a*S-E+2*numpy.sqrt(a*E*S))*i1e(2*numpy.sqrt(a*E*S)) + else : + prob = numpy.exp(-E) + return prob - def detector_output(self, input_photons) : - Nw_pixel = self.configs.detector_image_size[0] - Nh_pixel = self.configs.detector_image_size[1] - # declear photon distribution for camera image - camera_pixel = numpy.zeros([Nw_pixel, Nh_pixel, 4]) + def detector_output(self, input_photons) : - # set seed for random number - numpy.random.seed() + Nw_pixel = self.configs.detector_image_size[0] + Nh_pixel = self.configs.detector_image_size[1] - # CMOS (readout noise probability ditributions) - if (self.configs.detector_type == "CMOS") : - noise_data = numpy.loadtxt("catalog/detector/RNDist_F40.csv", delimiter=',') - Nr = noise_data[:,0] - p_noise = noise_data[:,1] - p_nsum = p_noise.sum() + # declear photon distribution for camera image + camera_pixel = numpy.zeros([Nw_pixel, Nh_pixel, 4]) + # set seed for random number + numpy.random.seed() - # conversion : photon --> photoelectron --> ADC count - for i in range(Nw_pixel) : - for j in range(Nh_pixel) : + # CMOS (readout noise probability ditributions) + if (self.configs.detector_type == "CMOS") : + noise_data = numpy.loadtxt("catalog/detector/RNDist_F40.csv", delimiter=',') + Nr = noise_data[:,0] + p_noise = noise_data[:,1] + p_nsum = p_noise.sum() - # pixel position - pixel = (i, j) - # Detector : Quantum Efficiency - #index = int(self.configs.psf_wavelength) - int(self.configs.wave_length[0]) - #QE = self.configs.detector_qeff[index] - QE = self.configs.detector_qeff + # conversion : photon --> photoelectron --> ADC count + for i in range(Nw_pixel) : + for j in range(Nh_pixel) : - # get signal (photons) - Photons = input_photons[i][j] + # pixel position + pixel = (i, j) - # get signal (expectation) - Exp = QE*Photons + # Detector : Quantum Efficiency + #index = int(self.configs.psf_wavelength) - int(self.configs.wave_length[0]) + #QE = self.configs.detector_qeff[index] + QE = self.configs.detector_qeff - # select Camera type - if (self.configs.detector_type == "CMOS") : + # get signal (photons) + Photons = input_photons[i][j] - # get signal (poisson distributions) - signal = numpy.random.poisson(Exp, None) + # get signal (expectation) + Exp = QE*Photons - # get detector noise (photoelectrons) - noise = numpy.random.choice(Nr, None, p=p_noise/p_nsum) + # select Camera type + if (self.configs.detector_type == "CMOS") : + # get signal (poisson distributions) + signal = numpy.random.poisson(Exp, None) - elif (self.configs.detector_type == "EMCCD") : + # get detector noise (photoelectrons) + noise = numpy.random.choice(Nr, None, p=p_noise/p_nsum) - # get signal (photoelectrons) - if (Exp > 0) : - # get EM gain - M = self.configs.detector_emgain + elif (self.configs.detector_type == "EMCCD") : - # set probability distributions - s_min = M*int(Exp - 5.0*numpy.sqrt(Exp) - 10) - s_max = M*int(Exp + 5.0*numpy.sqrt(Exp) + 10) + # get signal (photoelectrons) + if (Exp > 0) : - if (s_min < 0) : s_min = 0 + # get EM gain + M = self.configs.detector_emgain - s = numpy.array([k for k in range(s_min, s_max)]) - p_signal = numpy.array(map(lambda x : self.prob_EMCCD(x, Exp), s)) - p_ssum = p_signal.sum() + # set probability distributions + s_min = M*int(Exp - 5.0*numpy.sqrt(Exp) - 10) + s_max = M*int(Exp + 5.0*numpy.sqrt(Exp) + 10) - # get signal (photoelectrons) - signal = numpy.random.choice(s, None, p=p_signal/p_ssum) + if (s_min < 0) : s_min = 0 - else : - signal = 0 + s = numpy.array([k for k in range(s_min, s_max)]) + p_signal = numpy.array(map(lambda x : self.prob_EMCCD(x, Exp), s)) + p_ssum = p_signal.sum() - # get detector noise (photoelectrons) - Nr = self.configs.detector_readout_noise + # get signal (photoelectrons) + signal = numpy.random.choice(s, None, p=p_signal/p_ssum) - if (Nr > 0) : - noise = numpy.random.normal(0, Nr, None) - else : noise = 0 + else : + signal = 0 + # get detector noise (photoelectrons) + Nr = self.configs.detector_readout_noise - elif (self.configs.detector_type == "CCD") : + if (Nr > 0) : + noise = numpy.random.normal(0, Nr, None) + else : noise = 0 - # get signal (poisson distributions) - signal = numpy.random.poisson(Exp, None) - # get detector noise (photoelectrons) - Nr = self.configs.detector_readout_noise + elif (self.configs.detector_type == "CCD") : - if (Nr > 0) : - noise = numpy.random.normal(0, Nr, None) - else : noise = 0 + # get signal (poisson distributions) + signal = numpy.random.poisson(Exp, None) + # get detector noise (photoelectrons) + Nr = self.configs.detector_readout_noise - # get signal (photoelectrons) - PE = signal + noise + if (Nr > 0) : + noise = numpy.random.normal(0, Nr, None) + else : noise = 0 - # A/D converter : Photoelectrons --> ADC counts - ADC = self.get_ADC_value(pixel, PE) - # set data in image array - camera_pixel[i][j] = [Photons, Exp, PE, ADC] + # get signal (photoelectrons) + PE = signal + noise - return camera_pixel + # A/D converter : Photoelectrons --> ADC counts + ADC = self.get_ADC_value(pixel, PE) + # set data in image array + camera_pixel[i][j] = [Photons, Exp, PE, ADC] + return camera_pixel - def get_ADC_value(self, pixel, photoelectron) : - # pixel position - i, j = pixel - # check non-linearity - fullwell = self.configs.ADConverter_fullwell + def get_ADC_value(self, pixel, photoelectron) : - if (photoelectron > fullwell) : - photoelectron = fullwell + # pixel position + i, j = pixel - # convert photoelectron to ADC counts - k = self.configs.ADConverter_gain[i][j] - ADC0 = self.configs.ADConverter_offset[i][j] - ADC_max = 2**self.configs.ADConverter_bit - 1 + # check non-linearity + fullwell = self.configs.ADConverter_fullwell - ADC = photoelectron/k + ADC0 + if (photoelectron > fullwell) : + photoelectron = fullwell - if (ADC > ADC_max) : - ADC = ADC_max + # convert photoelectron to ADC counts + k = self.configs.ADConverter_gain[i][j] + ADC0 = self.configs.ADConverter_offset[i][j] + ADC_max = 2**self.configs.ADConverter_bit - 1 - if (ADC < 0) : - ADC = 0 + ADC = photoelectron/k + ADC0 - return int(ADC) + if (ADC > ADC_max) : + ADC = ADC_max + if (ADC < 0) : + ADC = 0 + return int(ADC) diff --git a/bioimaging/camera_script.py b/bioimaging/camera_script.py index 97f918e..85d9b7c 100755 --- a/bioimaging/camera_script.py +++ b/bioimaging/camera_script.py @@ -8,32 +8,30 @@ def test_camera(index) : - # create Camera configuration - camera = CameraConfigs() + # create Camera configuration + camera = CameraConfigs() - # Detector : CMOS Camera - #camera.set_Detector(detector='CMOS', image_size=(100,100), pixel_length=6.5e-6, \ - # focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.73) - #camera.set_ADConverter(bit=16, offset=100, fullwell=30000) + # Detector : CMOS Camera + #camera.set_Detector(detector='CMOS', image_size=(100,100), pixel_length=6.5e-6, \ + # focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.73) + #camera.set_ADConverter(bit=16, offset=100, fullwell=30000) - # Detector : EMCCD Camera - camera.set_Detector(detector='EMCCD', image_size=(100,100), pixel_length=16e-6, \ - focal_point=(0.0,0.5,0.5), exposure_time=150e-3, QE=0.92, readout_noise=100, emgain=300) - camera.set_ADConverter(bit=16, offset=2000, fullwell=370000) + # Detector : EMCCD Camera + camera.set_Detector(detector='EMCCD', image_size=(100,100), pixel_length=16e-6, \ + focal_point=(0.0,0.5,0.5), exposure_time=150e-3, QE=0.92, readout_noise=100, emgain=300) + camera.set_ADConverter(bit=16, offset=2000, fullwell=370000) - ### Output data - camera.set_OutputData(image_file_dir='./images_camera') + ### Output data + camera.set_OutputData(image_file_dir='./images_camera') - # create image and movie - create = CameraVisualizer(configs=camera) - create.output_frames(index=index, signal=signal[index], background=0.01) + # create image and movie + create = CameraVisualizer(configs=camera) + create.output_frames(index=index, signal=signal[index], background=0.01) if __name__ == "__main__": - index = int(sys.argv[1]) - - test_camera(index) - + index = int(sys.argv[1]) + test_camera(index) diff --git a/bioimaging/convert_addimages.py b/bioimaging/convert_addimages.py index 03e38b8..821785f 100644 --- a/bioimaging/convert_addimages.py +++ b/bioimaging/convert_addimages.py @@ -17,61 +17,60 @@ def convert(file_in0, file_in1, file_out) : - i = 20000 + i = 20000 - while (True) : - try : - #input_image0 = imread(file_in0 + '/image_%07d.png' % (i+1)) - #input_image1 = imread(file_in1 + '/image_%07d.png' % (i)) - input_image0 = numpy.load(file_in0 + '/image_%07d.npy' % (i)) - input_image1 = numpy.load(file_in1 + '/image_%07d.npy' % (i)) + while (True) : + try : + #input_image0 = imread(file_in0 + '/image_%07d.png' % (i+1)) + #input_image1 = imread(file_in1 + '/image_%07d.png' % (i)) + input_image0 = numpy.load(file_in0 + '/image_%07d.npy' % (i)) + input_image1 = numpy.load(file_in1 + '/image_%07d.npy' % (i)) - except Exception : - break + except Exception : + break - output_image = file_out + '/image_%07d.png' % (i-20000) + output_image = file_out + '/image_%07d.png' % (i-20000) - # data for tirfm - #image_array = numpy.zeros(shape=(512,2*512)) - #image_array[:,0:512] = input_image0[:,:,1] - #image_array[:,512:2*512] = input_image1[:,:,1] - image_array = numpy.zeros(shape=(256,2*256)) - image_array[:,0:256] = input_image0[256-128:256+128,306-128:306+128,1] - image_array[:,256:2*256] = input_image1[256-128:256+128,306-128:306+128,1] + # data for tirfm + #image_array = numpy.zeros(shape=(512,2*512)) + #image_array[:,0:512] = input_image0[:,:,1] + #image_array[:,512:2*512] = input_image1[:,:,1] + image_array = numpy.zeros(shape=(256,2*256)) + image_array[:,0:256] = input_image0[256-128:256+128,306-128:306+128,1] + image_array[:,256:2*256] = input_image1[256-128:256+128,306-128:306+128,1] - amax = numpy.amax(image_array) - amin = numpy.amin(image_array) + amax = numpy.amax(image_array) + amin = numpy.amin(image_array) - #print i, amax, amin + #print i, amax, amin - #photons0 = 5.82*(numpy.sum(input_image0[:,:,1])-2000)/300/0.92 - #photons1 = 5.82*(numpy.sum(input_image1[:,:,1])-2000)/300/0.92 - photons0 = 5.82*(numpy.sum(input_image0[256-128:256+128,306-128:306+128,1])-2000)/300/0.92 - photons1 = 5.82*(numpy.sum(input_image1[256-128:256+128,306-128:306+128,1])-2000)/300/0.92 + #photons0 = 5.82*(numpy.sum(input_image0[:,:,1])-2000)/300/0.92 + #photons1 = 5.82*(numpy.sum(input_image1[:,:,1])-2000)/300/0.92 + photons0 = 5.82*(numpy.sum(input_image0[256-128:256+128,306-128:306+128,1])-2000)/300/0.92 + photons1 = 5.82*(numpy.sum(input_image1[256-128:256+128,306-128:306+128,1])-2000)/300/0.92 - print(i*0.150, photons0, photons1) + print(i*0.150, photons0, photons1) - # 16-bit data format - #image_array.astype('uint16') - #toimage(image_array, high=11000, low=100, mode='I').save(output_image) + # 16-bit data format + #image_array.astype('uint16') + #toimage(image_array, high=11000, low=100, mode='I').save(output_image) - # 8-bit data format (for making movie) - #toimage(image_array, cmin=cmin, cmax=cmax).save(output_image) + # 8-bit data format (for making movie) + #toimage(image_array, cmin=cmin, cmax=cmax).save(output_image) - i += 1 + i += 1 if __name__=='__main__': - #file_in0 = '/home/masaki/bioimaging_4public/images_experiment' - #file_in1 = '/home/masaki/bioimaging_4public/images_oligomer_model' - #file_in0 = '/home/masaki/bioimaging_4public/numpys_hiroshima_TEST_on' - #file_in1 = '/home/masaki/bioimaging_4public/numpys_hiroshima_TEST_off' - file_in0 = '/home/masaki/bioimaging_4public/numpys_hiroshima_Olg_001nM_on' - file_in1 = '/home/masaki/bioimaging_4public/numpys_hiroshima_Olg_001nM_off' - file_out = '/home/masaki/bioimaging_4public/images_png' - - convert(file_in0, file_in1, file_out) + #file_in0 = '/home/masaki/bioimaging_4public/images_experiment' + #file_in1 = '/home/masaki/bioimaging_4public/images_oligomer_model' + #file_in0 = '/home/masaki/bioimaging_4public/numpys_hiroshima_TEST_on' + #file_in1 = '/home/masaki/bioimaging_4public/numpys_hiroshima_TEST_off' + file_in0 = '/home/masaki/bioimaging_4public/numpys_hiroshima_Olg_001nM_on' + file_in1 = '/home/masaki/bioimaging_4public/numpys_hiroshima_Olg_001nM_off' + file_out = '/home/masaki/bioimaging_4public/images_png' + convert(file_in0, file_in1, file_out) diff --git a/bioimaging/convert_csv2txt.py b/bioimaging/convert_csv2txt.py index de97897..4d8d64e 100644 --- a/bioimaging/convert_csv2txt.py +++ b/bioimaging/convert_csv2txt.py @@ -7,59 +7,58 @@ def convert(file_in, file_out, start, end, nm, deg) : - max_count = 0 + max_count = 0 - csv_input = file_in + '/pt-input.csv' - shutil.copyfile(csv_input, file_out+ '/pt-input.csv') + csv_input = file_in + '/pt-input.csv' + shutil.copyfile(csv_input, file_out+ '/pt-input.csv') - for i in range(start, end) : + for i in range(start, end) : - time = i*0.1 + time = i*0.1 - output_csv = file_out + '/pt-%09d.0.csv' % (i) + output_csv = file_out + '/pt-%09d.0.csv' % (i) - y0, z0 = 2.50e-6, 2.50e-6 - yyy = [y0] - zzz = [z0] -# r0 = 0.5*nm/numpy.cos(30*numpy.pi/180.) -# yyy = [y0+r0*numpy.cos(90*numpy.pi/180.), y0+r0*numpy.cos(210*numpy.pi/180.), y0+r0*numpy.cos(330*numpy.pi/180.)] -# zzz = [z0+r0*numpy.sin(90*numpy.pi/180.), z0+r0*numpy.sin(210*numpy.pi/180.), z0+r0*numpy.sin(330*numpy.pi/180.)] -# r0 = 0.5*nm -# yyy = [y0+r0*numpy.cos(45*numpy.pi/180.), y0+r0*numpy.cos(135*numpy.pi/180.), y0+r0*numpy.cos(225*numpy.pi/180.), y0+r0*numpy.cos(315*numpy.pi/180.)] -# zzz = [z0+r0*numpy.sin(45*numpy.pi/180.), z0+r0*numpy.sin(135*numpy.pi/180.), z0+r0*numpy.sin(225*numpy.pi/180.), z0+r0*numpy.sin(315*numpy.pi/180.)] + y0, z0 = 2.50e-6, 2.50e-6 + yyy = [y0] + zzz = [z0] +# r0 = 0.5*nm/numpy.cos(30*numpy.pi/180.) +# yyy = [y0+r0*numpy.cos(90*numpy.pi/180.), y0+r0*numpy.cos(210*numpy.pi/180.), y0+r0*numpy.cos(330*numpy.pi/180.)] +# zzz = [z0+r0*numpy.sin(90*numpy.pi/180.), z0+r0*numpy.sin(210*numpy.pi/180.), z0+r0*numpy.sin(330*numpy.pi/180.)] +# r0 = 0.5*nm +# yyy = [y0+r0*numpy.cos(45*numpy.pi/180.), y0+r0*numpy.cos(135*numpy.pi/180.), y0+r0*numpy.cos(225*numpy.pi/180.), y0+r0*numpy.cos(315*numpy.pi/180.)] +# zzz = [z0+r0*numpy.sin(45*numpy.pi/180.), z0+r0*numpy.sin(135*numpy.pi/180.), z0+r0*numpy.sin(225*numpy.pi/180.), z0+r0*numpy.sin(315*numpy.pi/180.)] - with open(output_csv, 'w') as output : - print(output_csv) + with open(output_csv, 'w') as output : + print(output_csv) - j = 0 - for row in csv.reader(open(file_in+'/pt-000000000.0.csv', 'r')) : + j = 0 + for row in csv.reader(open(file_in+'/pt-000000000.0.csv', 'r')) : - line = str(time) + ',' - line += row[1] + ',' - line += str(yyy[j]) + ',' - line += str(zzz[j]) + ',' - line += row[4] + ',' - line += '"' + row[5] + '",' - line += '"' + row[6] + '"\n' + line = str(time) + ',' + line += row[1] + ',' + line += str(yyy[j]) + ',' + line += str(zzz[j]) + ',' + line += row[4] + ',' + line += '"' + row[5] + '",' + line += '"' + row[6] + '"\n' - with open(output_csv, 'a') as output : - output.write(line) + with open(output_csv, 'a') as output : + output.write(line) - j += 1 + j += 1 if __name__=='__main__': - for nm in range(20, 380, 20) : - # for deg in range(0, 390, 30) : - deg = 0 + for nm in range(20, 380, 20) : + # for deg in range(0, 390, 30) : + deg = 0 - file_in = '/home/masaki/wrk/spatiocyte/models/data_moriga/model_001A/index_000' - #file_out = '/home/masaki/wrk/spatiocyte/models/data_moriga/model_002A_%03dnm_%03ddeg' % (nm, deg) - file_out = '/home/masaki/wrk/spatiocyte/models/data_moriga/model_001A_%03dnm' % (nm) + file_in = '/home/masaki/wrk/spatiocyte/models/data_moriga/model_001A/index_000' + #file_out = '/home/masaki/wrk/spatiocyte/models/data_moriga/model_002A_%03dnm_%03ddeg' % (nm, deg) + file_out = '/home/masaki/wrk/spatiocyte/models/data_moriga/model_001A_%03dnm' % (nm) - if not os.path.exists(file_out): - os.makedirs(file_out) - - convert(file_in, file_out, 0, 1000, nm*1e-9, deg) + if not os.path.exists(file_out): + os.makedirs(file_out) + convert(file_in, file_out, 0, 1000, nm*1e-9, deg) diff --git a/bioimaging/convert_fcs2npy.py b/bioimaging/convert_fcs2npy.py index 6867f0a..13ee423 100644 --- a/bioimaging/convert_fcs2npy.py +++ b/bioimaging/convert_fcs2npy.py @@ -8,31 +8,30 @@ def convert(file_in) : - i = 0 - result=numpy.zeros(shape=(2e+5,3)) + i = 0 + result=numpy.zeros(shape=(2e+5,3)) - while (True) : - try : - npy_file = numpy.load(file_in + '/image_%07d.npy' % (i)) - result[i,:] = numpy.array(npy_file) - #print i, npy_file - #result = numpy.append(result, npy_file) + while (True) : + try : + npy_file = numpy.load(file_in + '/image_%07d.npy' % (i)) + result[i,:] = numpy.array(npy_file) + #print i, npy_file + #result = numpy.append(result, npy_file) - except Exception : - break + except Exception : + break - i += 1 - if (i > 2e+5) : break + i += 1 + if (i > 2e+5) : break - sav = numpy.save(file_in + '/image_all.npy', result) + sav = numpy.save(file_in + '/image_all.npy', result) if __name__=='__main__': - file_in = '/home/masaki/bioimaging_4public/images_fcs_2StMD_A200' - #file_in = '/home/masaki/bioimaging_4public/images_fcs_D010_A200' - - convert(file_in) + file_in = '/home/masaki/bioimaging_4public/images_fcs_2StMD_A200' + #file_in = '/home/masaki/bioimaging_4public/images_fcs_D010_A200' + convert(file_in) diff --git a/bioimaging/convert_npy2image.py b/bioimaging/convert_npy2image.py index 83fc5c4..b19ecc9 100644 --- a/bioimaging/convert_npy2image.py +++ b/bioimaging/convert_npy2image.py @@ -16,58 +16,57 @@ def convert(file_in, file_out) : - i = 0 - max_count = 0 + i = 0 + max_count = 0 - while (True) : - try : - input_image = numpy.load(file_in + '/image_%07d.npy' % (i)) - #input_image = numpy.load(file_in + '/' + filename[i]) - except Exception : - break + while (True) : + try : + input_image = numpy.load(file_in + '/image_%07d.npy' % (i)) + #input_image = numpy.load(file_in + '/' + filename[i]) + except Exception : + break - output_image = file_out + '/image_%07d.png' % (i) - #output_image = file_out + '/image_%07d_%02ddeg.png' % (i, angle) - #output_filename = filename[i].split('.npy') - #output_image = file_out + '/%s.png' % (output_filename[0]) + output_image = file_out + '/image_%07d.png' % (i) + #output_image = file_out + '/image_%07d_%02ddeg.png' % (i, angle) + #output_filename = filename[i].split('.npy') + #output_image = file_out + '/%s.png' % (output_filename[0]) - # data for tirfm - #image_array = input_image[256-200:256+200,256-200:256+200,1] - #image_array = input_image[256-76:256+76,256-78:256+78,1] - #image_array = input_image[300-25:300+25,300-100:300-100+50,1] - #image_array = input_image[200-100:200+100,200-100:200+100,1] - #image_array = input_image[300-200:300+200,300-200:300+200,1] - #image_array = input_image[256-100:256+100,256-100:256+100,1] - #image_array = input_image[300-50:300+50,300-50:300+50,1] - image_array = input_image[:,:,1] + # data for tirfm + #image_array = input_image[256-200:256+200,256-200:256+200,1] + #image_array = input_image[256-76:256+76,256-78:256+78,1] + #image_array = input_image[300-25:300+25,300-100:300-100+50,1] + #image_array = input_image[200-100:200+100,200-100:200+100,1] + #image_array = input_image[300-200:300+200,300-200:300+200,1] + #image_array = input_image[256-100:256+100,256-100:256+100,1] + #image_array = input_image[300-50:300+50,300-50:300+50,1] + image_array = input_image[:,:,1] - #image_exp += numpy.array(image_array) + #image_exp += numpy.array(image_array) - amax = numpy.amax(image_array) - amin = numpy.amin(image_array) + amax = numpy.amax(image_array) + amin = numpy.amin(image_array) - if (max_count < amax) : - max_count = amax + if (max_count < amax) : + max_count = amax - print(i, amax, amin) + print(i, amax, amin) - # 16-bit data format - #image_array.astype('uint16') - #toimage(image_array, high=11000, low=100, mode='I').save(output_image) + # 16-bit data format + #image_array.astype('uint16') + #toimage(image_array, high=11000, low=100, mode='I').save(output_image) - # 8-bit data format (for making movie) - toimage(image_array, cmin=cmin, cmax=cmax).save(output_image) + # 8-bit data format (for making movie) + toimage(image_array, cmin=cmin, cmax=cmax).save(output_image) - i += 1 + i += 1 - print('Max count : ', max_count, 'ADC') + print('Max count : ', max_count, 'ADC') if __name__=='__main__': - file_in = '/home/masaki/bioimaging_4public/data_hiroshima_for_calibration/numpys/numpys_Test_00' - file_out = '/home/masaki/bioimaging_4public/data_hiroshima_for_calibration/images_png' - - convert(file_in, file_out) + file_in = '/home/masaki/bioimaging_4public/data_hiroshima_for_calibration/numpys/numpys_Test_00' + file_out = '/home/masaki/bioimaging_4public/data_hiroshima_for_calibration/images_png' + convert(file_in, file_out) diff --git a/bioimaging/convert_temp.py b/bioimaging/convert_temp.py index d19ecf0..aa5f2f4 100644 --- a/bioimaging/convert_temp.py +++ b/bioimaging/convert_temp.py @@ -16,108 +16,107 @@ def convert(file_in, file_out, cmin, cmax) : - i = 0 - max_count = 0 + i = 0 + max_count = 0 - while (True) : - try : - input_image = numpy.load(file_in + '/image_%07d.npy' % (i)) - #input_image = numpy.load(file_in + '/' + filename[i]) - except Exception : - break + while (True) : + try : + input_image = numpy.load(file_in + '/image_%07d.npy' % (i)) + #input_image = numpy.load(file_in + '/' + filename[i]) + except Exception : + break - output_image = file_out + '/image_%07d.png' % (i) - #output_filename = filename[i].split('.npy') - #output_image = file_out + '/%s.png' % (output_filename[0]) + output_image = file_out + '/image_%07d.png' % (i) + #output_filename = filename[i].split('.npy') + #output_image = file_out + '/%s.png' % (output_filename[0]) - # data for tirfm - #image_array = input_image[256-25:256+25,256-25:256+26,1] - #image_array = input_image[256-76:256+76,256-78:256+78,1] - #image_array = input_image[300-25:300+25,300-100:300-100+50,1] - #image_array = input_image[200-100:200+100,200-100:200+100,1] - #image_array = input_image[300-200:300+200,300-200:300+200,1] - #image_array = input_image[512-100:512+100,512-100:512+100,1] - image_array = input_image[300-50:300+50,300-50:300+50,1] - #image_array = input_image[:,:,1] + # data for tirfm + #image_array = input_image[256-25:256+25,256-25:256+26,1] + #image_array = input_image[256-76:256+76,256-78:256+78,1] + #image_array = input_image[300-25:300+25,300-100:300-100+50,1] + #image_array = input_image[200-100:200+100,200-100:200+100,1] + #image_array = input_image[300-200:300+200,300-200:300+200,1] + #image_array = input_image[512-100:512+100,512-100:512+100,1] + image_array = input_image[300-50:300+50,300-50:300+50,1] + #image_array = input_image[:,:,1] - #image_exp += numpy.array(image_array) + #image_exp += numpy.array(image_array) - amax = numpy.amax(image_array) - amin = numpy.amin(image_array) + amax = numpy.amax(image_array) + amin = numpy.amin(image_array) - if (max_count < amax) : - max_count = amax + if (max_count < amax) : + max_count = amax - #print i, amax, amin + #print i, amax, amin - # 16-bit data format - #image_array.astype('uint16') - #toimage(image_array, high=11000, low=100, mode='I').save(output_image) + # 16-bit data format + #image_array.astype('uint16') + #toimage(image_array, high=11000, low=100, mode='I').save(output_image) - # 8-bit data format (for making movie) - toimage(image_array, cmin=cmin, cmax=cmax).save(output_image) + # 8-bit data format (for making movie) + toimage(image_array, cmin=cmin, cmax=cmax).save(output_image) - i += 1 + i += 1 - print('Max count : ', max_count, 'ADC') + print('Max count : ', max_count, 'ADC') if __name__=='__main__': - #file_in = '/home/masaki/bioimaging_4public/data_moriga_01/numpys/model_001A/index_099' - #file_in = '/home/masaki/bioimaging_4public/numpys_palm_aggr00_bleach' - #file_in = '/home/masaki/bioimaging_4public/numpys_palm_aggr05_blink' - #file_in = '/home/masaki/bioimaging_4public/numpys_lscm_019656A' - #file_in = '/home/masaki/bioimaging_4public/numpys_lscm_019656A_bleach' - #file_in = '/home/masaki/bioimaging_4public/numpys_flip_080000A_bleach' - #file_in = '/home/masaki/bioimaging_4public/numpys_frap_019656A_bleach' - #file_in = '/home/masaki/bioimaging_4public/numpys_frap_000100A_bleach' - #file_out = '/home/masaki/bioimaging_4public/images_png' + #file_in = '/home/masaki/bioimaging_4public/data_moriga_01/numpys/model_001A/index_099' + #file_in = '/home/masaki/bioimaging_4public/numpys_palm_aggr00_bleach' + #file_in = '/home/masaki/bioimaging_4public/numpys_palm_aggr05_blink' + #file_in = '/home/masaki/bioimaging_4public/numpys_lscm_019656A' + #file_in = '/home/masaki/bioimaging_4public/numpys_lscm_019656A_bleach' + #file_in = '/home/masaki/bioimaging_4public/numpys_flip_080000A_bleach' + #file_in = '/home/masaki/bioimaging_4public/numpys_frap_019656A_bleach' + #file_in = '/home/masaki/bioimaging_4public/numpys_frap_000100A_bleach' + #file_out = '/home/masaki/bioimaging_4public/images_png' - #convert(file_in, file_out, 0, 20) + #convert(file_in, file_out, 0, 20) - #nm = int(sys.argv[1]) + #nm = int(sys.argv[1]) - I0 = [40, 50, 60 ,70, 80, 90] - Bg = [7, 4, 2, 0] + I0 = [40, 50, 60 ,70, 80, 90] + Bg = [7, 4, 2, 0] - cmin, cmax = 100, 400 + cmin, cmax = 100, 400 - file_out = '/home/masaki/bioimaging_4public/data_moriga_02/images/coordinate_002A_extfunc.csv' + file_out = '/home/masaki/bioimaging_4public/data_moriga_02/images/coordinate_002A_extfunc.csv' - for nm in range(20, 380, 20) : - #for i in range(len(I0)) : - #for j in range(len(Bg)) : + for nm in range(20, 380, 20) : + #for i in range(len(I0)) : + #for j in range(len(Bg)) : - file_in = '/home/masaki/wrk/spatiocyte/models/data_moriga/model_002A_%03dnm/pt-000000000.0.csv' % (nm) -# file_out = '/home/masaki/bioimaging_4public/data_moriga_02/images/model_003A_%03dnm_%02dI0_%02dBg' % (nm, I0[2], Bg[0]) + file_in = '/home/masaki/wrk/spatiocyte/models/data_moriga/model_002A_%03dnm/pt-000000000.0.csv' % (nm) +# file_out = '/home/masaki/bioimaging_4public/data_moriga_02/images/model_003A_%03dnm_%02dI0_%02dBg' % (nm, I0[2], Bg[0]) - j = 0 - for row in csv.reader(open(file_in, 'r')) : + j = 0 + for row in csv.reader(open(file_in, 'r')) : - line = str(nm) + ',' - line += str(46) + ',' - line += str(7) + ',' - line += row[1] + ',' - line += row[2] + ',' - line += row[3] + ',' - line += row[4] + ',' - line += '"' + row[5] + '",' - line += '"' + row[6] + '"\n' + line = str(nm) + ',' + line += str(46) + ',' + line += str(7) + ',' + line += row[1] + ',' + line += row[2] + ',' + line += row[3] + ',' + line += row[4] + ',' + line += '"' + row[5] + '",' + line += '"' + row[6] + '"\n' - with open(file_out, 'a') as output : - output.write(line) + with open(file_out, 'a') as output : + output.write(line) - j += 1 + j += 1 -# print file_in -# print file_out +# print file_in +# print file_out # -# shutil.copy2(file_in, file_out) +# shutil.copy2(file_in, file_out) # -# if not os.path.exists(file_out): -# os.makedirs(file_out) +# if not os.path.exists(file_out): +# os.makedirs(file_out) # -# convert(file_in, file_out, cmin, cmax) - +# convert(file_in, file_out, cmin, cmax) diff --git a/bioimaging/effects_handler.py b/bioimaging/effects_handler.py index f636bb5..dcc26c6 100755 --- a/bioimaging/effects_handler.py +++ b/bioimaging/effects_handler.py @@ -30,9 +30,9 @@ class PhysicalEffects() : ''' Physical effects setting class - Fluorescence - Photo-bleaching - Photo-blinking + Fluorescence + Photo-bleaching + Photo-blinking ''' def __init__(self, user_configs_dict = None): @@ -57,7 +57,7 @@ def __init__(self, user_configs_dict = None): def _set_data(self, key, val) : - + if val != None: setattr(self, key, val) @@ -86,7 +86,7 @@ def set_crosstalk(self, width=None) : def set_fluorescence(self, quantum_yield=None, - abs_coefficient=None) : + abs_coefficient=None) : print('--- Fluorescence :') @@ -99,8 +99,8 @@ def set_fluorescence(self, quantum_yield=None, - def set_photobleaching(self, tau0=None, - alpha=None) : + def set_photobleaching(self, tau0=None, + alpha=None) : print('--- Photobleaching :') @@ -108,14 +108,14 @@ def set_photobleaching(self, tau0=None, self._set_data('photobleaching_tau0', tau0) self._set_data('photobleaching_alpha', alpha) - print('\tPhotobleaching tau0 = ', self.photobleaching_tau0) - print('\tPhotobleaching alpha = ', self.photobleaching_alpha) + print('\tPhotobleaching tau0 = ', self.photobleaching_tau0) + print('\tPhotobleaching alpha = ', self.photobleaching_alpha) def set_photoactivation(self, turn_on_ratio=None, - activation_yield=None, - frac_preactivation=None) : + activation_yield=None, + frac_preactivation=None) : print('--- Photoactivation :') @@ -132,7 +132,7 @@ def set_photoactivation(self, turn_on_ratio=None, def set_photoblinking(self, t0_on=None, a_on=None, - t0_off=None, a_off=None) : + t0_off=None, a_off=None) : print('--- Photo-blinking : ') @@ -142,380 +142,377 @@ def set_photoblinking(self, t0_on=None, a_on=None, self._set_data('photoblinking_t0_off', t0_off) self._set_data('photoblinking_a_off', a_off) - print('\t(ON) t0 = ', self.photoblinking_t0_on, 'sec') - print('\t(ON) a = ', self.photoblinking_a_on) - print('\t(OFF) t0 = ', self.photoblinking_t0_off, 'sec') - print('\t(OFF) a = ', self.photoblinking_a_off) + print('\t(ON) t0 = ', self.photoblinking_t0_on, 'sec') + print('\t(ON) a = ', self.photoblinking_a_on) + print('\t(OFF) t0 = ', self.photoblinking_t0_off, 'sec') + print('\t(OFF) a = ', self.photoblinking_a_off) def prob_levy(self, t, t0, a) : - prob = (a/t0)*(t0/t)**(1+a) - #prob = (t0/t)**(1+a) - - return prob + prob = (a/t0)*(t0/t)**(1+a) + #prob = (t0/t)**(1+a) + + return prob def get_prob_bleach(self, tau, dt) : - # set the photobleaching-time - tau0 = self.photobleaching_tau0 - alpha = self.photobleaching_alpha + # set the photobleaching-time + tau0 = self.photobleaching_tau0 + alpha = self.photobleaching_alpha - # set probability - prob = self.prob_levy(tau, tau0, alpha) - norm = prob.sum() - p_bleach = prob/norm + # set probability + prob = self.prob_levy(tau, tau0, alpha) + norm = prob.sum() + p_bleach = prob/norm - return p_bleach + return p_bleach def get_prob_blink(self, tau_on, tau_off, dt) : - # time scale - t0_on = self.photoblinking_t0_on - a_on = self.photoblinking_a_on - t0_off = self.photoblinking_t0_off - a_off = self.photoblinking_a_off + # time scale + t0_on = self.photoblinking_t0_on + a_on = self.photoblinking_a_on + t0_off = self.photoblinking_t0_off + a_off = self.photoblinking_a_off - # ON-state - prob_on = self.prob_levy(tau_on, t0_on, a_on) - norm_on = prob_on.sum() - p_blink_on = prob_on/norm_on - - # OFF-state - prob_off = self.prob_levy(tau_off, t0_off, a_off) - norm_off = prob_off.sum() - p_blink_off = prob_off/norm_off + # ON-state + prob_on = self.prob_levy(tau_on, t0_on, a_on) + norm_on = prob_on.sum() + p_blink_on = prob_on/norm_on - return p_blink_on, p_blink_off + # OFF-state + prob_off = self.prob_levy(tau_off, t0_off, a_off) + norm_off = prob_off.sum() + p_blink_off = prob_off/norm_off + + return p_blink_on, p_blink_off def get_photobleaching_property(self, dt, n_emit0) : - # set the photobleaching-time - tau0 = self.photobleaching_tau0 - alpha = self.photobleaching_alpha + # set the photobleaching-time + tau0 = self.photobleaching_tau0 + alpha = self.photobleaching_alpha - # set photon budget - photon0 = (tau0/dt)*n_emit0 + # set photon budget + photon0 = (tau0/dt)*n_emit0 - tau_bleach = numpy.array([j*dt + tau0 for j in range(int(1e+7))]) - p_bleach = self.get_prob_bleach(tau_bleach, dt) + tau_bleach = numpy.array([j*dt + tau0 for j in range(int(1e+7))]) + p_bleach = self.get_prob_bleach(tau_bleach, dt) - # get photon budget and photobleaching-time - tau = numpy.random.choice(tau_bleach, 1, p=p_bleach) - budget = (tau/tau0)*photon0 + # get photon budget and photobleaching-time + tau = numpy.random.choice(tau_bleach, 1, p=p_bleach) + budget = (tau/tau0)*photon0 - return tau, budget + return tau, budget def set_photophysics_4epifm(self, time, delta, n_emit0, N_part) : - state = numpy.zeros(shape=(len(delta))) - dt = delta[0] + state = numpy.zeros(shape=(len(delta))) + dt = delta[0] - if (self.photobleaching_switch == True) : - # set the photobleaching-time - tau0 = self.photobleaching_tau0 - alpha = self.photobleaching_alpha + if (self.photobleaching_switch == True) : + # set the photobleaching-time + tau0 = self.photobleaching_tau0 + alpha = self.photobleaching_alpha - # set photon budget - photon0 = (tau0/dt)*n_emit0 + # set photon budget + photon0 = (tau0/dt)*n_emit0 - tau_bleach = numpy.array([j*dt+tau0 for j in range(int(1e+7))]) - prob = self.prob_levy(tau_bleach, tau0, alpha) - norm = prob.sum() - p_bleach = prob/norm + tau_bleach = numpy.array([j*dt+tau0 for j in range(int(1e+7))]) + prob = self.prob_levy(tau_bleach, tau0, alpha) + norm = prob.sum() + p_bleach = prob/norm - # get random number - numpy.random.seed() + # get random number + numpy.random.seed() - # get photon budget and photobleaching-time - tau = numpy.random.choice(tau_bleach, N_part, p=p_bleach) + # get photon budget and photobleaching-time + tau = numpy.random.choice(tau_bleach, N_part, p=p_bleach) - # sequences - budget = [] - state = [] + # sequences + budget = [] + state = [] - for i in range(N_part) : + for i in range(N_part) : - # get photon budget and photobleaching-time - photons = (tau[i]/tau0)*photon0 + # get photon budget and photobleaching-time + photons = (tau[i]/tau0)*photon0 - # bleaching time - state_bleach = numpy.zeros(shape=(len(time))) + # bleaching time + state_bleach = numpy.zeros(shape=(len(time))) - Ni = (numpy.abs(numpy.cumsum(delta) - tau[i])).argmin() + Ni = (numpy.abs(numpy.cumsum(delta) - tau[i])).argmin() - state_bleach[0:Ni] = numpy.ones(shape=(Ni)) + state_bleach[0:Ni] = numpy.ones(shape=(Ni)) - # set photon budget and fluorescence state - budget.append(photons) - state.append(state_bleach) + # set photon budget and fluorescence state + budget.append(photons) + state.append(state_bleach) - ##### - self.fluorescence_budget = numpy.array(budget) - self.fluorescence_state = numpy.array(state) + ##### + self.fluorescence_budget = numpy.array(budget) + self.fluorescence_state = numpy.array(state) def set_photophysics_4palm(self, start, end, dt, f, F, N_part) : - ##### PALM Configuration - NNN = int(1e+7) + ##### PALM Configuration + NNN = int(1e+7) + + # set the photobleaching-time + tau0 = self.photobleaching_tau0 + alpha = self.photobleaching_alpha - # set the photobleaching-time - tau0 = self.photobleaching_tau0 - alpha = self.photobleaching_alpha + # set photon budget + #photon0 = (tau0/dt)*N_emit0 + photon0 = (tau0/dt)*1.0 - # set photon budget - #photon0 = (tau0/dt)*N_emit0 - photon0 = (tau0/dt)*1.0 + tau_bleach = numpy.array([j*dt+tau0 for j in range(NNN)]) + prob = self.prob_levy(tau_bleach, tau0, alpha) + norm = prob.sum() + p_bleach = prob/norm - tau_bleach = numpy.array([j*dt+tau0 for j in range(NNN)]) - prob = self.prob_levy(tau_bleach, tau0, alpha) - norm = prob.sum() - p_bleach = prob/norm + # set photoblinking (ON/OFF) probability density function (PDF) + if (self.photoblinking_switch == True) : - # set photoblinking (ON/OFF) probability density function (PDF) - if (self.photoblinking_switch == True) : + # time scale + t0_on = self.photoblinking_t0_on + a_on = self.photoblinking_a_on + t0_off = self.photoblinking_t0_off + a_off = self.photoblinking_a_off - # time scale - t0_on = self.photoblinking_t0_on - a_on = self.photoblinking_a_on - t0_off = self.photoblinking_t0_off - a_off = self.photoblinking_a_off + tau_on = numpy.array([j*dt+t0_on for j in range(NNN)]) + tau_off = numpy.array([j*dt+t0_off for j in range(NNN)]) - tau_on = numpy.array([j*dt+t0_on for j in range(NNN)]) - tau_off = numpy.array([j*dt+t0_off for j in range(NNN)]) + # ON-state + prob_on = self.prob_levy(tau_on, t0_on, a_on) + norm_on = prob_on.sum() + p_on = prob_on/norm_on - # ON-state - prob_on = self.prob_levy(tau_on, t0_on, a_on) - norm_on = prob_on.sum() - p_on = prob_on/norm_on - - # OFF-state - prob_off = self.prob_levy(tau_off, t0_off, a_off) - norm_off = prob_off.sum() - p_off = prob_off/norm_off + # OFF-state + prob_off = self.prob_levy(tau_off, t0_off, a_off) + norm_off = prob_off.sum() + p_off = prob_off/norm_off - # sequences - budget = [] - state_act = [] + # sequences + budget = [] + state_act = [] - # get random number - numpy.random.seed() + # get random number + numpy.random.seed() - # get photon budget and photobleaching-time - tau = numpy.random.choice(tau_bleach, N_part, p=p_bleach) + # get photon budget and photobleaching-time + tau = numpy.random.choice(tau_bleach, N_part, p=p_bleach) - for i in range(N_part) : + for i in range(N_part) : - # get photon budget and photobleaching-time - photons = (tau[i]/tau0)*photon0 + # get photon budget and photobleaching-time + photons = (tau[i]/tau0)*photon0 - N = int((end - start)/dt) + N = int((end - start)/dt) - time = numpy.array([j*dt + start for j in range(N)]) - state = numpy.zeros(shape=(N)) + time = numpy.array([j*dt + start for j in range(N)]) + state = numpy.zeros(shape=(N)) - #### Photoactivation : overall activation yeild - p = self.photoactivation_activation_yield - q = self.photoactivation_frac_preactivation + #### Photoactivation : overall activation yeild + p = self.photoactivation_activation_yield + q = self.photoactivation_frac_preactivation - r = numpy.random.uniform(0, 1, N/F*f) - s = (r < p).astype('int') + r = numpy.random.uniform(0, 1, N/F*f) + s = (r < p).astype('int') - index = numpy.abs(s - 1).argmin() - t0 = (index/f*F + numpy.remainder(index, f))*dt + start + index = numpy.abs(s - 1).argmin() + t0 = (index/f*F + numpy.remainder(index, f))*dt + start -# t0 = end +# t0 = end # -# for k in range(N/F) : -# for l in range(f) : -# # random -# r = numpy.random.uniform(0, 1) +# for k in range(N/F) : +# for l in range(f) : +# # random +# r = numpy.random.uniform(0, 1) # -# if (r < PE) : -# t0 = (k + l)*dt -# break +# if (r < PE) : +# t0 = (k + l)*dt +# break # -# if (t0 < end) : break +# if (t0 < end) : break - t1 = t0 + tau[i] + t1 = t0 + tau[i] - N0 = (numpy.abs(time - t0)).argmin() - N1 = (numpy.abs(time - t1)).argmin() - state[N0:N1] = numpy.ones(shape=(N1-N0)) + N0 = (numpy.abs(time - t0)).argmin() + N1 = (numpy.abs(time - t1)).argmin() + state[N0:N1] = numpy.ones(shape=(N1-N0)) - if (self.photoblinking_switch == True) : + if (self.photoblinking_switch == True) : - # ON-state - t_on = numpy.random.choice(tau_on, 100, p=p_on) - # OFF-state - t_off = numpy.random.choice(tau_off, 100, p=p_off) + # ON-state + t_on = numpy.random.choice(tau_on, 100, p=p_on) + # OFF-state + t_off = numpy.random.choice(tau_off, 100, p=p_off) - k = (numpy.abs(numpy.cumsum(t_on) - tau[i])).argmin() + k = (numpy.abs(numpy.cumsum(t_on) - tau[i])).argmin() - # merge t_on/off arrays - t_blink = numpy.array([t_on[0:k], t_off[0:k]]) - t = numpy.reshape(t_blink, 2*k, order='F') + # merge t_on/off arrays + t_blink = numpy.array([t_on[0:k], t_off[0:k]]) + t = numpy.reshape(t_blink, 2*k, order='F') - i_on = N0 + i_on = N0 - for j in range(k) : + for j in range(k) : - f_on = i_on + int(t[j]/dt) - i_off = f_on - f_off = i_off + int(t[j+1]/dt) + f_on = i_on + int(t[j]/dt) + i_off = f_on + f_off = i_off + int(t[j+1]/dt) - state[i_on:f_on] = 1 - state[i_off:f_off] = 0 + state[i_on:f_on] = 1 + state[i_off:f_off] = 0 - i_on = f_off + i_on = f_off -# time_blinking = numpy.cumsum(t) +# time_blinking = numpy.cumsum(t) # -# # set state array -# s_on = numpy.ones(shape=(k)) -# s_off = numpy.zeros(shape=(k)) -# s = numpy.array([s_on, s_off]) +# # set state array +# s_on = numpy.ones(shape=(k)) +# s_off = numpy.zeros(shape=(k)) +# s = numpy.array([s_on, s_off]) # -# state_blinking = numpy.reshape(s, 2*k, order='F') -# state_blinking = numpy.reshape(s, 2*k, order='F') +# state_blinking = numpy.reshape(s, 2*k, order='F') +# state_blinking = numpy.reshape(s, 2*k, order='F') # -# # set fluorescence state (with photoblinking) -# budget.append(photons) -# time_act.append(time_blinking) -# state_act.append(state_blinking) +# # set fluorescence state (with photoblinking) +# budget.append(photons) +# time_act.append(time_blinking) +# state_act.append(state_blinking) - # set fluorescence state (without photoblinking) - budget.append(photons) - state_act.append(state) + # set fluorescence state (without photoblinking) + budget.append(photons) + state_act.append(state) - self.fluorescence_bleach = numpy.array(tau) - self.fluorescence_budget = numpy.array(budget) - self.fluorescence_state = numpy.array(state_act) + self.fluorescence_bleach = numpy.array(tau) + self.fluorescence_budget = numpy.array(budget) + self.fluorescence_state = numpy.array(state_act) def set_photophysics_4lscm(self, start, end, dt, N_part) : - ##### LSCM Configuration + ##### LSCM Configuration - NNN = int(1e+7) + NNN = int(1e+7) - # set the photobleaching-time - tau0 = self.photobleaching_tau0 - alpha = self.photobleaching_alpha + # set the photobleaching-time + tau0 = self.photobleaching_tau0 + alpha = self.photobleaching_alpha - # set photon budget - photon0 = (tau0/dt)*1.0 + # set photon budget + photon0 = (tau0/dt)*1.0 - tau_bleach = numpy.array([j*dt+tau0 for j in range(NNN)]) - prob = self.prob_levy(tau_bleach, tau0, alpha) - norm = prob.sum() - p_bleach = prob/norm + tau_bleach = numpy.array([j*dt+tau0 for j in range(NNN)]) + prob = self.prob_levy(tau_bleach, tau0, alpha) + norm = prob.sum() + p_bleach = prob/norm - # set photoblinking (ON/OFF) probability density function (PDF) - if (self.photoblinking_switch == True) : + # set photoblinking (ON/OFF) probability density function (PDF) + if (self.photoblinking_switch == True) : - # time scale - t0_on = self.photoblinking_t0_on - a_on = self.photoblinking_a_on - t0_off = self.photoblinking_t0_off - a_off = self.photoblinking_a_off + # time scale + t0_on = self.photoblinking_t0_on + a_on = self.photoblinking_a_on + t0_off = self.photoblinking_t0_off + a_off = self.photoblinking_a_off - tau_on = numpy.array([j*dt+t0_on for j in range(NNN)]) - tau_off = numpy.array([j*dt+t0_off for j in range(NNN)]) + tau_on = numpy.array([j*dt+t0_on for j in range(NNN)]) + tau_off = numpy.array([j*dt+t0_off for j in range(NNN)]) - # ON-state - prob_on = self.prob_levy(tau_on, t0_on, a_on) - norm_on = prob_on.sum() - p_on = prob_on/norm_on - - # OFF-state - prob_off = self.prob_levy(tau_off, t0_off, a_off) - norm_off = prob_off.sum() - p_off = prob_off/norm_off + # ON-state + prob_on = self.prob_levy(tau_on, t0_on, a_on) + norm_on = prob_on.sum() + p_on = prob_on/norm_on - # sequences - budget = [] - state_act = [] + # OFF-state + prob_off = self.prob_levy(tau_off, t0_off, a_off) + norm_off = prob_off.sum() + p_off = prob_off/norm_off - # get random number - numpy.random.seed() + # sequences + budget = [] + state_act = [] - # get photon budget and photobleaching-time - tau = numpy.random.choice(tau_bleach, N_part, p=p_bleach) + # get random number + numpy.random.seed() - for i in range(N_part) : + # get photon budget and photobleaching-time + tau = numpy.random.choice(tau_bleach, N_part, p=p_bleach) - # get photon budget and photobleaching-time - photons = (tau[i]/tau0)*photon0 + for i in range(N_part) : - N = 10000 #int((end - start)/dt) + # get photon budget and photobleaching-time + photons = (tau[i]/tau0)*photon0 - time = numpy.array([j*dt + start for j in range(N)]) - state = numpy.zeros(shape=(N)) + N = 10000 #int((end - start)/dt) -# #### Photoactivation : overall activation yeild -# p = self.photoactivation_activation_yield -# q = self.photoactivation_frac_preactivation + time = numpy.array([j*dt + start for j in range(N)]) + state = numpy.zeros(shape=(N)) + +# #### Photoactivation : overall activation yeild +# p = self.photoactivation_activation_yield +# q = self.photoactivation_frac_preactivation # -# r = numpy.random.uniform(0, 1, N/F*f) -# s = (r < p).astype('int') +# r = numpy.random.uniform(0, 1, N/F*f) +# s = (r < p).astype('int') # -# index = numpy.abs(s - 1).argmin() -# t0 = (index/f*F + numpy.remainder(index, f))*dt + start - - t0 = start - t1 = t0 + tau[i] - - N0 = (numpy.abs(time - t0)).argmin() - N1 = (numpy.abs(time - t1)).argmin() - state[N0:N1] = numpy.ones(shape=(N1-N0)) - - if (self.photoblinking_switch == True) : +# index = numpy.abs(s - 1).argmin() +# t0 = (index/f*F + numpy.remainder(index, f))*dt + start - # ON-state - t_on = numpy.random.choice(tau_on, 100, p=p_on) - # OFF-state - t_off = numpy.random.choice(tau_off, 100, p=p_off) + t0 = start + t1 = t0 + tau[i] - k = (numpy.abs(numpy.cumsum(t_on) - tau[i])).argmin() + N0 = (numpy.abs(time - t0)).argmin() + N1 = (numpy.abs(time - t1)).argmin() + state[N0:N1] = numpy.ones(shape=(N1-N0)) - # merge t_on/off arrays - t_blink = numpy.array([t_on[0:k], t_off[0:k]]) - t = numpy.reshape(t_blink, 2*k, order='F') + if (self.photoblinking_switch == True) : - i_on = N0 + # ON-state + t_on = numpy.random.choice(tau_on, 100, p=p_on) + # OFF-state + t_off = numpy.random.choice(tau_off, 100, p=p_off) - for j in range(k) : + k = (numpy.abs(numpy.cumsum(t_on) - tau[i])).argmin() - f_on = i_on + int(t[j]/dt) - i_off = f_on - f_off = i_off + int(t[j+1]/dt) + # merge t_on/off arrays + t_blink = numpy.array([t_on[0:k], t_off[0:k]]) + t = numpy.reshape(t_blink, 2*k, order='F') - state[i_on:f_on] = 1 - state[i_off:f_off] = 0 + i_on = N0 - i_on = f_off + for j in range(k) : - # set photon budget and fluorescence state - budget.append(photons) - state_act.append(state) + f_on = i_on + int(t[j]/dt) + i_off = f_on + f_off = i_off + int(t[j+1]/dt) - self.fluorescence_bleach = numpy.array(tau) - self.fluorescence_budget = numpy.array(budget) - self.fluorescence_state = numpy.array(state_act) + state[i_on:f_on] = 1 + state[i_off:f_off] = 0 + i_on = f_off + # set photon budget and fluorescence state + budget.append(photons) + state_act.append(state) + self.fluorescence_bleach = numpy.array(tau) + self.fluorescence_budget = numpy.array(budget) + self.fluorescence_state = numpy.array(state_act) diff --git a/bioimaging/epifm_handler.py b/bioimaging/epifm_handler.py index 07afe16..eab3c73 100755 --- a/bioimaging/epifm_handler.py +++ b/bioimaging/epifm_handler.py @@ -48,9 +48,9 @@ class EPIFMConfigs() : ''' EPIFM Configuration - Wide-field Gaussian Profile - + - Detector : EMCCD/CMOS + Wide-field Gaussian Profile + + + Detector : EMCCD/CMOS ''' def __init__(self, user_configs_dict = None): @@ -64,15 +64,15 @@ def __init__(self, user_configs_dict = None): # 'excitation_switch': bool # 'fluorophore_type': str - # 'psf_wavelength': float - # 'psf_normalization': - # 'psf_width': - # 'psf_cutoff': - # 'psf_file_name_format': + # 'psf_wavelength': float + # 'psf_normalization': + # 'psf_width': + # 'psf_cutoff': + # 'psf_file_name_format': # 'dichroic_switch': bool # 'emission_switch': bool - # 'image_magnification': float + # 'image_magnification': float # 'detector_switch': bool # 'detector_type': str @@ -86,15 +86,15 @@ def __init__(self, user_configs_dict = None): # 'detector_dark_count': int # 'detector_emgain': int - # 'ADConverter_bit': float - # 'ADConverter_gain': float - # 'ADConverter_offset': float + # 'ADConverter_bit': float + # 'ADConverter_gain': float + # 'ADConverter_offset': float # 'ADConverter_fullwell': float - # 'ADConverter_fpn_type': + # 'ADConverter_fpn_type': # 'ADConverter_fpn_count': float - # 'image_file_dir': str - # 'image_file_cleanup_dir': str + # 'image_file_dir': str + # 'image_file_cleanup_dir': str # 'spatiocyte_start_time': float # 'spatiocyte_end_time': float @@ -102,7 +102,7 @@ def __init__(self, user_configs_dict = None): # 'spatiocyte_data': list # 'spatiocyte_species_id': list # [0, 1, ...] # 'spatiocyte_index': list # ['A', 'B', ...] - # 'spatiocyte_radius': list # [1e-8, 1e-8, ...] + # 'spatiocyte_radius': list # [1e-8, 1e-8, ...] # 'spatiocyte_lengths': list[3] # 'spatiocyte_VoxelRadius': float # 'spatiocyte_theNormalizedVoxelRadius': float @@ -143,17 +143,17 @@ def _set_data(self, key, val) : def set_Shutter(self, start_time = None, - end_time = None, - time_open = None, + end_time = None, + time_open = None, time_lapse = None) : self._set_data('shutter_switch', True) self._set_data('shutter_start_time', start_time) self._set_data('shutter_end_time', end_time) - if (time_open == None or time_lapse == None) : - time_open = end_time - start_time - time_lapse = end_time - start_time + if (time_open == None or time_lapse == None) : + time_open = end_time - start_time + time_lapse = end_time - start_time self._set_data('shutter_time_open', time_open) self._set_data('shutter_time_lapse', time_lapse) @@ -167,7 +167,7 @@ def set_Shutter(self, start_time = None, def set_LightSource(self, source_type = None, - wave_length = None, + wave_length = None, flux_density = None, center = None, radius = None, @@ -226,12 +226,12 @@ def set_ExcitationFilter(self, excitation = None) : def set_Fluorophore(self, fluorophore_type = None, wave_length = None, - normalization = None, + normalization = None, width = None, cutoff = None, file_name_format = None ): - if (fluorophore_type == 'Gaussian') : + if (fluorophore_type == 'Gaussian') : print('--- Fluorophore : %s PSF' % (fluorophore_type)) self._set_data('fluorophore_type', fluorophore_type) @@ -241,26 +241,26 @@ def set_Fluorophore(self, fluorophore_type = None, self._set_data('psf_cutoff', cutoff) self._set_data('psf_file_name_format', file_name_format) - index = (numpy.abs(self.wave_length - self.psf_wavelength)).argmin() + index = (numpy.abs(self.wave_length - self.psf_wavelength)).argmin() - self.fluoex_eff[index] = 100 - self.fluoem_eff[index] = 100 + self.fluoex_eff[index] = 100 + self.fluoem_eff[index] = 100 print('\tWave Length = ', self.psf_wavelength, 'nm') - print('\tNormalization = ', self.psf_normalization) + print('\tNormalization = ', self.psf_normalization) print('\tLateral Width = ', self.psf_width[0], 'nm') print('\tAxial Width = ', self.psf_width[1], 'nm') - #print '\tLateral Cutoff = ', self.psf_cutoff[0], 'nm' + #print '\tLateral Cutoff = ', self.psf_cutoff[0], 'nm' #print '\tAxial Cutoff = ', self.psf_cutoff[1], 'nm' - else : - print('--- Fluorophore :') + else : + print('--- Fluorophore :') filename = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'catalog/fluorophore/', fluorophore_type + '.csv') if not os.path.exists(filename): print('Error : ', filename, ' is NOT found') exit() - + with open(filename) as csvfile: lines = [string.rstrip(_) for _ in csvfile.readlines()] @@ -273,19 +273,19 @@ def set_Fluorophore(self, fluorophore_type = None, em_idx = data.index('Emission') fluorophore_excitation = [_.split(',') for _ in data[1:em_idx]] fluorophore_emission = [_.split(',') for _ in data[(em_idx + 1):]] - - #### - self.fluoex_eff = self.set_efficiency(fluorophore_excitation) - self.fluoem_eff = self.set_efficiency(fluorophore_emission) - index_ex = self.fluoex_eff.index(max(self.fluoex_eff)) - index_em = self.fluoem_eff.index(max(self.fluoem_eff)) + #### + self.fluoex_eff = self.set_efficiency(fluorophore_excitation) + self.fluoem_eff = self.set_efficiency(fluorophore_emission) + + index_ex = self.fluoex_eff.index(max(self.fluoex_eff)) + index_em = self.fluoem_eff.index(max(self.fluoem_eff)) - #### for temporary + #### for temporary self._set_data('fluorophore_type', fluorophore_type) - self._set_data('psf_wavelength', self.wave_length[index_em]) - self._set_data('psf_normalization', normalization) + self._set_data('psf_wavelength', self.wave_length[index_em]) + self._set_data('psf_normalization', normalization) self._set_data('psf_file_name_format', file_name_format) self.fluoex_eff[index_ex] = 100 @@ -294,48 +294,48 @@ def set_Fluorophore(self, fluorophore_type = None, print('\tPSF Normalization Factor = ', self.psf_normalization) print('\tExcitation : Wave Length = ', self.wave_length[index_ex], 'nm') print('\tEmission : Wave Length = ', self.psf_wavelength, 'nm') - - # Normalization - norm = sum(self.fluoex_eff) - self.fluoex_norm = numpy.array(self.fluoex_eff)/norm - norm = sum(self.fluoem_eff) - self.fluoem_norm = numpy.array(self.fluoem_eff)/norm + # Normalization + norm = sum(self.fluoex_eff) + self.fluoex_norm = numpy.array(self.fluoex_eff)/norm + + norm = sum(self.fluoem_eff) + self.fluoem_norm = numpy.array(self.fluoem_eff)/norm def set_DichroicMirror(self, dm = None) : print('--- Dichroic Mirror :') - filename = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'catalog/dichroic/') + dm + '.csv' + filename = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'catalog/dichroic/') + dm + '.csv' - try: - csvfile = open(filename) - lines = csvfile.readlines() + try: + csvfile = open(filename) + lines = csvfile.readlines() - header = lines[0:5] - data = lines[6:] + header = lines[0:5] + data = lines[6:] - dichroic_header = [] - dichroic_mirror = [] + dichroic_header = [] + dichroic_mirror = [] - for i in range(len(header)) : - dummy = header[i].split('\r\n') - a_data = dummy[0].split(',') - dichroic_header.append(a_data) - print('\t', a_data) + for i in range(len(header)) : + dummy = header[i].split('\r\n') + a_data = dummy[0].split(',') + dichroic_header.append(a_data) + print('\t', a_data) - for i in range(len(data)) : - dummy0 = data[i].split('\r\n') - a_data = dummy0[0].split(',') - dichroic_mirror.append(a_data) + for i in range(len(data)) : + dummy0 = data[i].split('\r\n') + a_data = dummy0[0].split(',') + dichroic_mirror.append(a_data) except Exception: print('Error : ', filename, ' is NOT found') - exit() + exit() self.dichroic_eff = self.set_efficiency(dichroic_mirror) - self._set_data('dichroic_switch', True) + self._set_data('dichroic_switch', True) @@ -350,14 +350,14 @@ def set_EmissionFilter(self, emission = None) : header = lines[0:5] data = lines[6:] - emission_header = [] - emission_filter = [] + emission_header = [] + emission_filter = [] for i in range(len(header)) : dummy = header[i].split('\r\n') a_data = dummy[0].split(',') emission_header.append(a_data) - print('\t', a_data) + print('\t', a_data) for i in range(len(data)) : dummy0 = data[i].split('\r\n') @@ -375,21 +375,21 @@ def set_EmissionFilter(self, emission = None) : def set_Magnification(self, Mag = None) : - self._set_data('image_magnification', Mag) + self._set_data('image_magnification', Mag) - print('--- Magnification : x', self.image_magnification) + print('--- Magnification : x', self.image_magnification) def set_Detector(self, detector = None, - image_size = None, + image_size = None, pixel_length = None, exposure_time = None, focal_point = None, - QE = None, - readout_noise = None, - dark_count = None, - emgain = None + QE = None, + readout_noise = None, + dark_count = None, + emgain = None ): self._set_data('detector_switch', True) @@ -404,72 +404,72 @@ def set_Detector(self, detector = None, self._set_data('detector_dark_count', dark_count) self._set_data('detector_emgain', emgain) - print('--- Detector : ', self.detector_type) + print('--- Detector : ', self.detector_type) print('\tImage Size = ', self.detector_image_size[0], 'x', self.detector_image_size[1]) print('\tPixel Size = ', self.detector_pixel_length, 'm/pixel') print('\tFocal Point = ', self.detector_focal_point) #print '\tPosition = ', self.detector_base_position print('\tExposure Time = ', self.detector_exposure_time, 'sec') print('\tQuantum Efficiency = ', 100*self.detector_qeff, '%') - print('\tReadout Noise = ', self.detector_readout_noise, 'electron') + print('\tReadout Noise = ', self.detector_readout_noise, 'electron') print('\tDark Count = ', self.detector_dark_count, 'electron/sec') print('\tEM gain = ', 'x', self.detector_emgain) - + def set_ADConverter(self, bit = None, - gain = None, - offset = None, - fullwell = None, - fpn_type = None, - fpn_count = None) : - - self._set_data('ADConverter_bit', bit) - self._set_data('ADConverter_gain', (fullwell - 0.0)/(pow(2.0, bit) - offset)) - self._set_data('ADConverter_offset', offset) + gain = None, + offset = None, + fullwell = None, + fpn_type = None, + fpn_count = None) : + + self._set_data('ADConverter_bit', bit) + self._set_data('ADConverter_gain', (fullwell - 0.0)/(pow(2.0, bit) - offset)) + self._set_data('ADConverter_offset', offset) self._set_data('ADConverter_fullwell', fullwell) self._set_data('ADConverter_fpn_type', fpn_type) self._set_data('ADConverter_fpn_count', fpn_count) print('--- A/D Converter : %d-bit' % (self.ADConverter_bit)) - print('\tGain = %.3f electron/count' % (self.ADConverter_gain)) - print('\tOffset = ', self.ADConverter_offset, 'count') + print('\tGain = %.3f electron/count' % (self.ADConverter_gain)) + print('\tOffset = ', self.ADConverter_offset, 'count') print('\tFullwell = ', self.ADConverter_fullwell, 'electron') print('\t%s-Fixed Pattern Noise :' % (self.ADConverter_fpn_type), self.ADConverter_fpn_count, 'count') - # image pixel-size - Nw_pixel = self.detector_image_size[0] - Nh_pixel = self.detector_image_size[1] + # image pixel-size + Nw_pixel = self.detector_image_size[0] + Nh_pixel = self.detector_image_size[1] - # set ADC parameters + # set ADC parameters bit = self.ADConverter_bit fullwell = self.ADConverter_fullwell - ADC0 = self.ADConverter_offset + ADC0 = self.ADConverter_offset - # set Fixed-Pattern noise + # set Fixed-Pattern noise FPN_type = self.ADConverter_fpn_type FPN_count = self.ADConverter_fpn_count - if (FPN_type == None) : + if (FPN_type == None) : # offset = numpy.empty(Nw_pixel*Nh_pixel) # offset.fill(ADC0) - offset = numpy.array([ADC0 for i in range(Nw_pixel*Nh_pixel)]) - - elif (FPN_type == 'pixel') : - offset = numpy.rint(numpy.random.normal(ADC0, FPN_count, Nw_pixel*Nh_pixel)) + offset = numpy.array([ADC0 for i in range(Nw_pixel*Nh_pixel)]) + + elif (FPN_type == 'pixel') : + offset = numpy.rint(numpy.random.normal(ADC0, FPN_count, Nw_pixel*Nh_pixel)) - elif (FPN_type == 'column') : - column = numpy.random.normal(ADC0, FPN_count, Nh_pixel) - temporal = numpy.array([column for i in range(Nw_pixel)]) + elif (FPN_type == 'column') : + column = numpy.random.normal(ADC0, FPN_count, Nh_pixel) + temporal = numpy.array([column for i in range(Nw_pixel)]) - offset = numpy.rint(temporal.reshape(Nh_pixel*Nw_pixel)) + offset = numpy.rint(temporal.reshape(Nh_pixel*Nw_pixel)) - # set ADC gain + # set ADC gain # gain = numpy.array(map(lambda x : (fullwell - 0.0)/(pow(2.0, bit) - x), offset)) gain = (fullwell - 0.0)/(pow(2.0, bit) - offset) - # reshape + # reshape self.ADConverter_offset = offset.reshape([Nw_pixel, Nh_pixel]) self.ADConverter_gain = gain.reshape([Nw_pixel, Nh_pixel]) @@ -477,11 +477,11 @@ def set_ADConverter(self, bit = None, def set_ShapeFile(self, csv_file_directry) : - print('--- Spatiocyte Cell Shape Data : ', csv_file_directry) + print('--- Spatiocyte Cell Shape Data : ', csv_file_directry) - # get shape data file - f = open(csv_file_directry + '/pt-shape.csv', 'r') - cell_shape = numpy.genfromtxt(f, delimiter=',') + # get shape data file + f = open(csv_file_directry + '/pt-shape.csv', 'r') + cell_shape = numpy.genfromtxt(f, delimiter=',') # get cell shape self._set_data('spatiocyte_shape', cell_shape.tolist()) @@ -491,19 +491,19 @@ def set_ShapeFile(self, csv_file_directry) : def set_InputFile(self, csv_file_directry, observable=None) : - print('--- Input Spatiocyte Data : ', csv_file_directry) + print('--- Input Spatiocyte Data : ', csv_file_directry) - ### header - f = open(csv_file_directry + '/pt-input.csv', 'r') + ### header + f = open(csv_file_directry + '/pt-input.csv', 'r') header = f.readline().rstrip().split(',') header[:5] = [float(_) for _ in header[:5]] f.close() interval, lengths, voxel_r, species_info = header[0], (header[3:0:-1]), header[4], header[5:] - species_id = range(len(species_info)-2) - species_index = [_.split(':')[1].split(']')[0] for _ in species_info[0:len(species_info)-2]] - species_radius = [float(_.split('=')[1]) for _ in species_info[0:len(species_info)-2]] + species_id = range(len(species_info)-2) + species_index = [_.split(':')[1].split(']')[0] for _ in species_info[0:len(species_info)-2]] + species_radius = [float(_.split('=')[1]) for _ in species_info[0:len(species_info)-2]] # get run time self._set_data('spatiocyte_file_directry', csv_file_directry) @@ -512,59 +512,59 @@ def set_InputFile(self, csv_file_directry, observable=None) : # get species properties self._set_data('spatiocyte_species_id', species_id) self._set_data('spatiocyte_index', species_index) - #self._set_data('spatiocyte_diffusion', species_diffusion) - self._set_data('spatiocyte_radius', species_radius) + #self._set_data('spatiocyte_diffusion', species_diffusion) + self._set_data('spatiocyte_radius', species_radius) # get lattice properties #self._set_data('spatiocyte_lattice_id', map(lambda x : x[0], lattice)) self._set_data('spatiocyte_lengths', lengths) self._set_data('spatiocyte_VoxelRadius', voxel_r) self._set_data('spatiocyte_theNormalizedVoxelRadius', 0.5) - + # set observable if observable is None : index = [True for i in range(len(self.spatiocyte_index))] - else : + else : index = map(lambda x : True if x.find(observable) > -1 else False, self.spatiocyte_index) - #index = [False, True] + #index = [False, True] self.spatiocyte_observables = copy.copy(index) print('\tTime Interval =', self.spatiocyte_interval, 'sec') - print('\tVoxel radius =', self.spatiocyte_VoxelRadius, 'm') + print('\tVoxel radius =', self.spatiocyte_VoxelRadius, 'm') print('\tCompartment lengths :', self.spatiocyte_lengths, 'voxels') print('\tSpecies Index :', self.spatiocyte_index) print('\tObservable :', self.spatiocyte_observables) - # Visualization error - if self.spatiocyte_species_id is None: - raise VisualizerError('Cannot find species_id in any given csv files') + # Visualization error + if self.spatiocyte_species_id is None: + raise VisualizerError('Cannot find species_id in any given csv files') - if len(self.spatiocyte_index) == 0 : - raise VisualizerError('Cannot find spatiocyte_index in any given csv files: ' \ - + ', '.join(csv_file_directry)) + if len(self.spatiocyte_index) == 0 : + raise VisualizerError('Cannot find spatiocyte_index in any given csv files: ' \ + + ', '.join(csv_file_directry)) def set_OutputData(self, image_file_dir = None, - image_file_name_format = None, - image_file_cleanup_dir=False) : + image_file_name_format = None, + image_file_cleanup_dir=False) : - if image_file_dir is None : + if image_file_dir is None : - image_file_dir = tempfile.mkdtemp(dir=os.getcwd()) - image_file_cleanup_dir = True - - self._set_data('image_file_dir', image_file_dir) - self._set_data('image_file_file_name_format', image_file_name_format) - self._set_data('image_file_cleanup_dir', image_file_cleanup_dir) + image_file_dir = tempfile.mkdtemp(dir=os.getcwd()) + image_file_cleanup_dir = True + + self._set_data('image_file_dir', image_file_dir) + self._set_data('image_file_file_name_format', image_file_name_format) + self._set_data('image_file_cleanup_dir', image_file_cleanup_dir) def set_efficiency(self, array, index=1) : -# if (len(array[0]) < 3) : index = 1 +# if (len(array[0]) < 3) : index = 1 # # N = len(self.wave_length) # @@ -581,88 +581,88 @@ def set_efficiency(self, array, index=1) : # # if (length/wl == 1) : # efficiency[i] = eff -# +# # return efficiency array = numpy.array(array, dtype = 'float') - array = array[array[:, 0] % 1 == 0,:] + array = array[array[:, 0] % 1 == 0,:] efficiency = numpy.zeros(len(self.wave_length)) idx1 = numpy.in1d(numpy.array(self.wave_length), array[:, 0]) idx2 = numpy.in1d(numpy.array(array[:, 0]), self.wave_length) - + efficiency[idx1] = array[idx2, 1] - + return efficiency.tolist() def set_Optical_path(self) : - # (0) Data : Cell Model Sample - self.set_Time_arrays() - self.set_Spatiocyte_data_arrays() + # (0) Data : Cell Model Sample + self.set_Time_arrays() + self.set_Spatiocyte_data_arrays() - # (1) Illumination path : Light source --> Cell Model Sample - #self.set_Illumination_path() - #exit() + # (1) Illumination path : Light source --> Cell Model Sample + #self.set_Illumination_path() + #exit() - # (2) Detection path : Cell Model Sample --> Detector - self.set_Detection_path() + # (2) Detection path : Cell Model Sample --> Detector + self.set_Detection_path() def set_Time_arrays(self) : - - # set time-arrays - start = self.shutter_start_time - end = self.shutter_end_time - - # set count arrays by spatiocyte interval - interval = self.spatiocyte_interval - N_count = int(round((end - start)/interval)) - c0 = int(round(start/interval)) - - delta_array = numpy.zeros(shape=(N_count)) - delta_array.fill(interval) - time_array = numpy.cumsum(delta_array) + start - count_array = numpy.array([c + c0 for c in range(N_count)]) - - # set index arrays by exposure time - exposure = self.detector_exposure_time - N_index = int(round((end - start)/exposure)) - i0 = int(round(start/exposure)) - -# delta_array = numpy.zeros(shape=(N_index)) -# delta_array.fill(exposure) -# time_array = numpy.cumsum(delta_array) + start - index_array = numpy.array([i + i0 for i in range(N_index)]) - -# if (self.shutter_switch == True) : -# # get shutter time -# time_open = self.shutter_time_open -# time_lapse = self.shutter_time_lapse -# -# # merge time_lapse arrays -# t = numpy.array([time_open, time_lapse-time_open]) -# s = numpy.zeros(shape=(int(round(time_lapse/interval)))) -# -# i_on = 0 -# f_on = i_on + int(t[0]/interval) -# i_off = f_on -# f_off = i_off + int(t[1]/interval) -# -# s[i_on:f_on] = 1 -# s[i_off:f_off] = 0 -# -# ss = numpy.tile(s, int((N_count)/len(s))+1) -# state_shutter = ss[0:N_count] -# -# time_array = time_array[state_shutter > 0] -# delta_array = delta_array[state_shutter > 0] -# count_array = count_array[state_shutter > 0] - - # set time, count and delta arrays + + # set time-arrays + start = self.shutter_start_time + end = self.shutter_end_time + + # set count arrays by spatiocyte interval + interval = self.spatiocyte_interval + N_count = int(round((end - start)/interval)) + c0 = int(round(start/interval)) + + delta_array = numpy.zeros(shape=(N_count)) + delta_array.fill(interval) + time_array = numpy.cumsum(delta_array) + start + count_array = numpy.array([c + c0 for c in range(N_count)]) + + # set index arrays by exposure time + exposure = self.detector_exposure_time + N_index = int(round((end - start)/exposure)) + i0 = int(round(start/exposure)) + +# delta_array = numpy.zeros(shape=(N_index)) +# delta_array.fill(exposure) +# time_array = numpy.cumsum(delta_array) + start + index_array = numpy.array([i + i0 for i in range(N_index)]) + +# if (self.shutter_switch == True) : +# # get shutter time +# time_open = self.shutter_time_open +# time_lapse = self.shutter_time_lapse +# +# # merge time_lapse arrays +# t = numpy.array([time_open, time_lapse-time_open]) +# s = numpy.zeros(shape=(int(round(time_lapse/interval)))) +# +# i_on = 0 +# f_on = i_on + int(t[0]/interval) +# i_off = f_on +# f_off = i_off + int(t[1]/interval) +# +# s[i_on:f_on] = 1 +# s[i_off:f_off] = 0 +# +# ss = numpy.tile(s, int((N_count)/len(s))+1) +# state_shutter = ss[0:N_count] +# +# time_array = time_array[state_shutter > 0] +# delta_array = delta_array[state_shutter > 0] +# count_array = count_array[state_shutter > 0] + + # set time, count and delta arrays self._set_data('shutter_time_array', time_array.tolist()) self._set_data('shutter_delta_array', delta_array.tolist()) self._set_data('shutter_count_array', count_array.tolist()) @@ -672,64 +672,64 @@ def set_Time_arrays(self) : def set_Spatiocyte_data_arrays(self) : - # get spatiocyte file directry - csv_file_directry = self.spatiocyte_file_directry + # get spatiocyte file directry + csv_file_directry = self.spatiocyte_file_directry + + # set data-array + data = [] - # set data-array - data = [] + # get count-array + count_array = numpy.array(self.shutter_count_array) - # get count-array - count_array = numpy.array(self.shutter_count_array) + # read lattice file + for i in range(len(count_array)) : - # read lattice file - for i in range(len(count_array)) : + csv_file_path = csv_file_directry + '/pt-%09d.0.csv' % (count_array[i]) - csv_file_path = csv_file_directry + '/pt-%09d.0.csv' % (count_array[i]) - try : csv_file = open(csv_file_path, 'r') - dataset = [] + dataset = [] - for row in csv.reader(csv_file) : - dataset.append(row) + for row in csv.reader(csv_file) : + dataset.append(row) ### particle data - time = float(dataset[0][0]) + time = float(dataset[0][0]) - particles = [] + particles = [] - for data_id in dataset : - # Coordinate - c_id = (float(data_id[1]), float(data_id[2]), float(data_id[3])) - # Molecule-ID and its state - m_id, s_id = literal_eval(data_id[5]) - # Fluorophore-ID and Compartment-ID - f_id, l_id = literal_eval(data_id[6]) + for data_id in dataset : + # Coordinate + c_id = (float(data_id[1]), float(data_id[2]), float(data_id[3])) + # Molecule-ID and its state + m_id, s_id = literal_eval(data_id[5]) + # Fluorophore-ID and Compartment-ID + f_id, l_id = literal_eval(data_id[6]) - try : - p_state, cyc_id = float(data_id[7]), float(data_id[8]) - except Exception : - p_state, cyc_id = 1.0, float('inf') + try : + p_state, cyc_id = float(data_id[7]), float(data_id[8]) + except Exception : + p_state, cyc_id = 1.0, float('inf') - particles.append((c_id, m_id, s_id, l_id, p_state, cyc_id)) - - data.append([time, particles]) + particles.append((c_id, m_id, s_id, l_id, p_state, cyc_id)) + + data.append([time, particles]) except Exception : print('Error : ', csv_file_path, ' not found') - #exit() + #exit() - data.sort(lambda x, y:cmp(x[0], y[0])) + data.sort(lambda x, y:cmp(x[0], y[0])) - # set data + # set data self._set_data('spatiocyte_data', data) -# if len(self.spatiocyte_data) == 0: -# raise VisualizerError('Cannot find spatiocyte_data in any given csv files: ' \ -# + ', '.join(csv_file_directry)) +# if len(self.spatiocyte_data) == 0: +# raise VisualizerError('Cannot find spatiocyte_data in any given csv files: ' \ +# + ', '.join(csv_file_directry)) @@ -737,87 +737,87 @@ def set_Spatiocyte_data_arrays(self) : # # #r = self.radial # #d = self.depth -# r = numpy.linspace(0, 20000, 20001) -# d = numpy.linspace(0, 20000, 20001) +# r = numpy.linspace(0, 20000, 20001) +# d = numpy.linspace(0, 20000, 20001) # # # (plank const) * (speed of light) [joules meter] # hc = self.hc_const # -# # Illumination : Assume that uniform illumination (No gaussian) -# # flux density [W/cm2 (joules/sec/cm2)] +# # Illumination : Assume that uniform illumination (No gaussian) +# # flux density [W/cm2 (joules/sec/cm2)] # P_0 = self.source_flux_density*1e+4 # # # single photon energy # wave_length = self.source_wavelength*1e-9 # E_wl = hc/wave_length # -# # photon flux density [photons/sec/cm2] +# # photon flux density [photons/sec/cm2] # N_0 = P_0/E_wl # -# ################################################################ -# # Evanescent field -# # -# # Assume uniform beam profile (Not Gaussian) -# # Linear polarization -# # +# ################################################################ +# # Evanescent field +# # +# # Assume uniform beam profile (Not Gaussian) +# # Linear polarization +# # # ################################################################ # -# # Incident beam : Angle -# theta_in = (self.source_angle/180.)*numpy.pi +# # Incident beam : Angle +# theta_in = (self.source_angle/180.)*numpy.pi # -# sin = numpy.sin(theta_in) -# cos = numpy.cos(theta_in) -# sin2 = sin**2 -# cos2 = cos**2 +# sin = numpy.sin(theta_in) +# cos = numpy.cos(theta_in) +# sin2 = sin**2 +# cos2 = cos**2 # -# # index refraction -# n_1 = 1.46 # fused silica -# n_2 = 1.33 # water (objective : water immersion) -# n = n_2/n_1 # must be < 1 -# n2 = n**2 +# # index refraction +# n_1 = 1.46 # fused silica +# n_2 = 1.33 # water (objective : water immersion) +# n = n_2/n_1 # must be < 1 +# n2 = n**2 # -# # Incident beam : Amplitude -# #theta = numpy.pi/2.0 -# A2_Is = N_0#*numpy.cos(theta)**2 -# A2_Ip = N_0#*numpy.sin(theta)**2 +# # Incident beam : Amplitude +# #theta = numpy.pi/2.0 +# A2_Is = N_0#*numpy.cos(theta)**2 +# A2_Ip = N_0#*numpy.sin(theta)**2 # -# if (sin2/n2 > 1) : -# # Evanescent field : Amplitude and Depth -# # Assume that the s-polar direction is parallel to y-axis -# A2_x = A2_Ip*(4*cos2*(sin2 - n2)/(n2**2*cos2 + sin2 - n2)) -# A2_y = A2_Is*(4*cos2/(1 - n2)) -# A2_z = A2_Ip*(4*cos2*sin2/(n2**2*cos2 + sin2 - n2)) +# if (sin2/n2 > 1) : +# # Evanescent field : Amplitude and Depth +# # Assume that the s-polar direction is parallel to y-axis +# A2_x = A2_Ip*(4*cos2*(sin2 - n2)/(n2**2*cos2 + sin2 - n2)) +# A2_y = A2_Is*(4*cos2/(1 - n2)) +# A2_z = A2_Ip*(4*cos2*sin2/(n2**2*cos2 + sin2 - n2)) # -# A2_Tp = A2_x + A2_z -# A2_Ts = A2_y +# A2_Tp = A2_x + A2_z +# A2_Ts = A2_y # -# depth = wave_length/(4.0*numpy.pi*numpy.sqrt(n_1**2*sin2 - n_2**2)) +# depth = wave_length/(4.0*numpy.pi*numpy.sqrt(n_1**2*sin2 - n_2**2)) # -# else : -# # Epifluorescence field : Amplitude and Depth -# cosT = numpy.sqrt(1 - sin2/n2) +# else : +# # Epifluorescence field : Amplitude and Depth +# cosT = numpy.sqrt(1 - sin2/n2) # -# A2_Tp = A2_Ip*(2*cos/(cosT + n*cos))**2 -# A2_Ts = A2_Is*(2*cos/(n*cosT + cos))**2 +# A2_Tp = A2_Ip*(2*cos/(cosT + n*cos))**2 +# A2_Ts = A2_Is*(2*cos/(n*cosT + cos))**2 # -# depth = float('inf') +# depth = float('inf') # -# I_d = numpy.exp(-d*1e-9/depth) -# I_r = numpy.array(map(lambda x : A2_Tp+A2_Ts, r*1e-9)) +# I_d = numpy.exp(-d*1e-9/depth) +# I_r = numpy.array(map(lambda x : A2_Tp+A2_Ts, r*1e-9)) # -# # photon flux density [photon/(sec m^2)] +# # photon flux density [photon/(sec m^2)] # self.source_flux_density = numpy.array(map(lambda x : I_r*x, I_d)) # -# print 'Penetration depth :', depth, 'm' -# print 'Photon Flux Density (Max) :', self.source_flux_density[0][0], '#photon/(sec m^2)' +# print 'Penetration depth :', depth, 'm' +# print 'Photon Flux Density (Max) :', self.source_flux_density[0][0], '#photon/(sec m^2)' def set_Illumination_path(self) : - # get cell-shape data - cell_shape = numpy.array(self.spatiocyte_shape) + # get cell-shape data + cell_shape = numpy.array(self.spatiocyte_shape) # define observational image plane in nm-scale voxel_size = 2.0*self.spatiocyte_VoxelRadius @@ -832,84 +832,84 @@ def set_Illumination_path(self) : b_0 = self.detector_focal_point x_0, y_0, z_0 = numpy.array([Nx, Ny, Nz])*b_0 - # Incident beam : 1st beam angle to basal region of the cell - theta_in = (self.source_angle/180.)*numpy.pi - sin_th1 = numpy.sin(theta_in) - sin2 = sin_th1**2 + # Incident beam : 1st beam angle to basal region of the cell + theta_in = (self.source_angle/180.)*numpy.pi + sin_th1 = numpy.sin(theta_in) + sin2 = sin_th1**2 - # Index of refraction - n_1 = 1.460 # fused silica - n_2 = 1.384 # cell - n_3 = 1.337 # culture medium + # Index of refraction + n_1 = 1.460 # fused silica + n_2 = 1.384 # cell + n_3 = 1.337 # culture medium - r = n_2/n_1 - r2 = r**2 + r = n_2/n_1 + r2 = r**2 - if (sin2/r2 < 1) : + if (sin2/r2 < 1) : - # epi-fluorescence - #sin_th2 = (n_1/n_2)*sin_th1 - #cos_th2 = numpy.sqrt(1 - sin_th2**2) + # epi-fluorescence + #sin_th2 = (n_1/n_2)*sin_th1 + #cos_th2 = numpy.sqrt(1 - sin_th2**2) # find cross point of beam center and cell surface - #rho = numpy.sqrt(Nx**2 + Ny**2) - rho = Nx + #rho = numpy.sqrt(Nx**2 + Ny**2) + rho = Nx + + while (rho > 0) : - while (rho > 0) : + # set beam position + #x_b, y_b, z_b = rho*cos_th2, rho*sin_th2 + y_0, z_0 + x_b, y_b, z_b = rho, y_0, z_0 + r_b = numpy.array([x_b, y_b, z_b]) - # set beam position - #x_b, y_b, z_b = rho*cos_th2, rho*sin_th2 + y_0, z_0 - x_b, y_b, z_b = rho, y_0, z_0 - r_b = numpy.array([x_b, y_b, z_b]) + # evaluate for intersection of beam-line to cell-surface + diff = numpy.sqrt(numpy.sum((cell_shape - r_b)**2, axis=1)) + index = numpy.nonzero(diff < voxel_size)[0] - # evaluate for intersection of beam-line to cell-surface - diff = numpy.sqrt(numpy.sum((cell_shape - r_b)**2, axis=1)) - index = numpy.nonzero(diff < voxel_size)[0] + if (len(index) > 0) : - if (len(index) > 0) : + p_0 = cell_shape[index[0]] + x_b, y_b, z_b = p_0 - p_0 = cell_shape[index[0]] - x_b, y_b, z_b = p_0 + f0 = (x_b/Nx, y_b/Ny, z_b/Nz) - f0 = (x_b/Nx, y_b/Ny, z_b/Nz) + # evaluate for normal vector of cell-surface + diff = numpy.sqrt(numpy.sum((cell_shape - p_0)**2, axis=1)) + k0 = numpy.nonzero(diff < 1.5*voxel_size)[0] + k1 = numpy.nonzero(k0 != diff.argmin())[0] - # evaluate for normal vector of cell-surface - diff = numpy.sqrt(numpy.sum((cell_shape - p_0)**2, axis=1)) - k0 = numpy.nonzero(diff < 1.5*voxel_size)[0] - k1 = numpy.nonzero(k0 != diff.argmin())[0] + r_n = cell_shape[k0[k1]] - r_n = cell_shape[k0[k1]] + # Optimization is definitely required!! + f0_norm = numpy.array([0, 0, 0]) + count = 0 + for kk in range(len(r_n)) : + for ii in range(len(r_n)) : + for jj in range(len(r_n)) : + if (kk!=ii and kk!=jj and ii!=jj) : + t = r_n[ii] - r_n[kk] + s = r_n[jj] - r_n[kk] - # Optimization is definitely required!! - f0_norm = numpy.array([0, 0, 0]) - count = 0 - for kk in range(len(r_n)) : - for ii in range(len(r_n)) : - for jj in range(len(r_n)) : - if (kk!=ii and kk!=jj and ii!=jj) : - t = r_n[ii] - r_n[kk] - s = r_n[jj] - r_n[kk] - - vec = numpy.cross(s, t) - if (vec[0] < 0) : vec = numpy.cross(t, s) - len_vec = numpy.sqrt(numpy.sum(vec**2)) - if (len_vec > 0) : - f0_norm = f0_norm + vec/len_vec - count += 1 + vec = numpy.cross(s, t) + if (vec[0] < 0) : vec = numpy.cross(t, s) + len_vec = numpy.sqrt(numpy.sum(vec**2)) + if (len_vec > 0) : + f0_norm = f0_norm + vec/len_vec + count += 1 - f0_norm = f0_norm/count + f0_norm = f0_norm/count - break + break - rho -= voxel_size/2 + rho -= voxel_size/2 - else : + else : - f0 = b_0 - f0_norm = numpy.array([1, 0, 0]) + f0 = b_0 + f0_norm = numpy.array([1, 0, 0]) - # set focal position + # set focal position self.detector_focal_point = f0 self.detector_focal_norm = f0_norm @@ -922,20 +922,20 @@ def set_Detection_path(self) : wave_length = self.psf_wavelength*1e-9 - # Magnification - Mag = self.image_magnification + # Magnification + Mag = self.image_magnification - # set image scaling factor + # set image scaling factor voxel_radius = self.spatiocyte_VoxelRadius - # set camera's pixel length - pixel_length = self.detector_pixel_length/Mag + # set camera's pixel length + pixel_length = self.detector_pixel_length/Mag - self.image_resolution = pixel_length - self.image_scaling = pixel_length/(2.0*voxel_radius) + self.image_resolution = pixel_length + self.image_scaling = pixel_length/(2.0*voxel_radius) - print('Resolution :', self.image_resolution, 'm') - print('Scaling :', self.image_scaling) + print('Resolution :', self.image_resolution, 'm') + print('Scaling :', self.image_scaling) # Detector PSF self.set_PSF_detector() @@ -959,51 +959,51 @@ def set_PSF_detector(self) : if (self.emission_switch == True) : I = I*0.01*self.emission_eff - # For normalization - norm = map(lambda x : True if x > 1e-4 else False, I) + # For normalization + norm = map(lambda x : True if x > 1e-4 else False, I) - # PSF : Fluorophore - psf_fl = None + # PSF : Fluorophore + psf_fl = None - if (self.fluorophore_type == 'Gaussian') : + if (self.fluorophore_type == 'Gaussian') : N0 = self.psf_normalization Ir = sum(map(lambda x : x*numpy.exp(-0.5*(r/self.psf_width[0])**2), norm)) Iz = sum(map(lambda x : x*numpy.exp(-0.5*(z/self.psf_width[1])**2), norm)) - psf_fl = numpy.sum(I)*N0*numpy.array(map(lambda x : Ir*x, Iz)) + psf_fl = numpy.sum(I)*N0*numpy.array(map(lambda x : Ir*x, Iz)) - else : - # make the norm and wave_length array shorter -# psf_fl = 0 + else : + # make the norm and wave_length array shorter +# psf_fl = 0 # -# for i in range(len(norm)) : +# for i in range(len(norm)) : # -# if norm[i] is True : -# psf_fl += self.get_PSF_fluorophore(r, z, wave_length[i]) +# if norm[i] is True : +# psf_fl += self.get_PSF_fluorophore(r, z, wave_length[i]) # -# psf_fl = psf_fl/sum(norm) - psf_fl = numpy.sum(I)*self.get_PSF_fluorophore(r, z, wave_length) +# psf_fl = psf_fl/sum(norm) + psf_fl = numpy.sum(I)*self.get_PSF_fluorophore(r, z, wave_length) - self.fluorophore_psf = psf_fl + self.fluorophore_psf = psf_fl def get_PSF_fluorophore(self, r, z, wave_length) : - # set Magnification of optical system - M = self.image_magnification + # set Magnification of optical system + M = self.image_magnification - # set Numerical Appature - NA = 1.4#self.objective_NA + # set Numerical Appature + NA = 1.4#self.objective_NA - # set alpha and gamma consts - k = 2.0*numpy.pi/wave_length - alpha = k*NA - gamma = k*(NA/2)**2 + # set alpha and gamma consts + k = 2.0*numpy.pi/wave_length + alpha = k*NA + gamma = k*(NA/2)**2 - # set rho parameters - N = 100 + # set rho parameters + N = 100 drho = 1.0/N rho = numpy.array([(i+1)*drho for i in range(N)]) @@ -1012,57 +1012,57 @@ def get_PSF_fluorophore(self, r, z, wave_length) : I = numpy.array(map(lambda x : x*J0, Y)) I_sum = I.sum(axis=2) - # set normalization factor - Norm = self.psf_normalization + # set normalization factor + Norm = self.psf_normalization - # set PSF + # set PSF psf = Norm*numpy.array(map(lambda x : abs(x)**2, I_sum)) -# for i in range(len(wave_length)) : +# for i in range(len(wave_length)) : # -# I = numpy.array(map(lambda x : x*J0[i], Y[i])) -# I_sum = I.sum(axis=2) -# I_abs = map(lambda x : abs(x)**2, I_sum) +# I = numpy.array(map(lambda x : x*J0[i], Y[i])) +# I_sum = I.sum(axis=2) +# I_abs = map(lambda x : abs(x)**2, I_sum) # -# if (i > 0) : psf += I_abs -# else : psf = I_abs +# if (i > 0) : psf += I_abs +# else : psf = I_abs - return psf + return psf class EPIFMVisualizer() : - ''' - EPIFM Visualization class of e-cell simulator - ''' + ''' + EPIFM Visualization class of e-cell simulator + ''' + + def __init__(self, configs=EPIFMConfigs(), effects=PhysicalEffects()) : + + assert isinstance(configs, EPIFMConfigs) + self.configs = configs - def __init__(self, configs=EPIFMConfigs(), effects=PhysicalEffects()) : - - assert isinstance(configs, EPIFMConfigs) - self.configs = configs + assert isinstance(effects, PhysicalEffects) + self.effects = effects - assert isinstance(effects, PhysicalEffects) - self.effects = effects - - """ - Check and create the folders for image and output files. - """ - if not os.path.exists(self.configs.image_file_dir): - os.makedirs(self.configs.image_file_dir) + """ + Check and create the folders for image and output files. + """ + if not os.path.exists(self.configs.image_file_dir): + os.makedirs(self.configs.image_file_dir) - """ - set Optical path from light source to detector - """ - self.configs.set_Optical_path() + """ + set Optical path from light source to detector + """ + self.configs.set_Optical_path() -# def rewrite_InputData(self, output_file_dir=None) : +# def rewrite_InputData(self, output_file_dir=None) : # -# if not os.path.exists(output_file_dir): -# os.makedirs(output_file_dir) +# if not os.path.exists(output_file_dir): +# os.makedirs(output_file_dir) # # # define observational image plane in nm-scale # voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 @@ -1079,1397 +1079,1394 @@ def __init__(self, configs=EPIFMConfigs(), effects=PhysicalEffects()) : # # beam position : Assuming beam position = focal point for temporary # x_b, y_b, z_b = copy.copy(p_0) # -# # set time, delta and count arrays -# time_array = numpy.array(self.configs.shutter_time_array) -# delta_array = numpy.array(self.configs.shutter_delta_array) -# count_array = numpy.array(self.configs.shutter_count_array) +# # set time, delta and count arrays +# time_array = numpy.array(self.configs.shutter_time_array) +# delta_array = numpy.array(self.configs.shutter_delta_array) +# count_array = numpy.array(self.configs.shutter_count_array) # -# # Snell's law -# amplitude0, penet_depth = self.snells_law(p_0, p_0) +# # Snell's law +# amplitude0, penet_depth = self.snells_law(p_0, p_0) # -# # get the number of emitted photons -# N_emit0 = self.get_emit_photons(amplitude0) +# # get the number of emitted photons +# N_emit0 = self.get_emit_photons(amplitude0) # -# # copy input-file -# csv_input = self.configs.spatiocyte_file_directry + '/pt-input.csv' -# shutil.copyfile(csv_input, output_file_dir + '/pt-input.csv') +# # copy input-file +# csv_input = self.configs.spatiocyte_file_directry + '/pt-input.csv' +# shutil.copyfile(csv_input, output_file_dir + '/pt-input.csv') # -# # copy shape-file -# csv_shape = self.configs.spatiocyte_file_directry + '/pt-shape.csv' -# shutil.copyfile(csv_shape, output_file_dir + '/pt-shape.csv') +# # copy shape-file +# csv_shape = self.configs.spatiocyte_file_directry + '/pt-shape.csv' +# shutil.copyfile(csv_shape, output_file_dir + '/pt-shape.csv') # -# # get the number of particles -# N_particles = 4117 #len(csv_list) +# # get the number of particles +# N_particles = 4117 #len(csv_list) # -# # set fluorescence -# self.effects.set_photophysics_4epifm(time_array, delta_array, N_emit0, N_particles) +# # set fluorescence +# self.effects.set_photophysics_4epifm(time_array, delta_array, N_emit0, N_particles) # -# budget = self.effects.fluorescence_budget -# state_pb = self.effects.fluorescence_state +# budget = self.effects.fluorescence_budget +# state_pb = self.effects.fluorescence_state # -# # set association state -# state_as = numpy.zeros(shape=(N_particles)) +# # set association state +# state_as = numpy.zeros(shape=(N_particles)) # -# for k in range(len(count_array)) : +# for k in range(len(count_array)) : # -# # read input file -# csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (count_array[k]) +# # read input file +# csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (count_array[k]) # -# csv_list = list(csv.reader(open(csv_file_path, 'r'))) -# dataset = numpy.array(csv_list) +# csv_list = list(csv.reader(open(csv_file_path, 'r'))) +# dataset = numpy.array(csv_list) # -# # set new arrays for photobleaching state and photon-budget -# new_state_pb = numpy.zeros(shape=(len(dataset))) -# new_budget = numpy.zeros(shape=(len(dataset))) +# # set new arrays for photobleaching state and photon-budget +# new_state_pb = numpy.zeros(shape=(len(dataset))) +# new_budget = numpy.zeros(shape=(len(dataset))) # # # loop for particles # for j, data_j in enumerate(dataset): -# # set particle position -# #p_i = numpy.array([float(data_j[1]), float(data_j[2]), float(data_j[3])])/1e-9 -# p_i = numpy.array(data_j[1:4]).astype('float')/1e-9 +# # set particle position +# #p_i = numpy.array([float(data_j[1]), float(data_j[2]), float(data_j[3])])/1e-9 +# p_i = numpy.array(data_j[1:4]).astype('float')/1e-9 # -# # Snell's law -# amplitude, penet_depth = self.snells_law(p_i, p_0) +# # Snell's law +# amplitude, penet_depth = self.snells_law(p_i, p_0) # -# # particle coordinate in real(nm) scale -# p_i, radial, depth = self.get_coordinate(p_i, p_0) +# # particle coordinate in real(nm) scale +# p_i, radial, depth = self.get_coordinate(p_i, p_0) # -# # Molecule ID and its state -# m_id, s_id = literal_eval(data_j[5]) -# # Fluorophore ID and compartment ID -# f_id, l_id = literal_eval(data_j[6]) +# # Molecule ID and its state +# m_id, s_id = literal_eval(data_j[5]) +# # Fluorophore ID and compartment ID +# f_id, l_id = literal_eval(data_j[6]) # -# # set association state -# state_as[m_id] = 1 +# # set association state +# state_as[m_id] = 1 # -# sid_array = numpy.array(self.configs.spatiocyte_species_id) -# s_index = (numpy.abs(sid_array - int(s_id))).argmin() +# sid_array = numpy.array(self.configs.spatiocyte_species_id) +# s_index = (numpy.abs(sid_array - int(s_id))).argmin() # -# if self.configs.spatiocyte_observables[s_index] is True : -# state_j = 1 -# else : -# state_j = 0 +# if self.configs.spatiocyte_observables[s_index] is True : +# state_j = 1 +# else : +# state_j = 0 # -# # get exponential amplitude (only for observation at basal surface) -# #amplitide = amplitude*numpy.exp(-depth/pent_depth) +# # get exponential amplitude (only for observation at basal surface) +# #amplitide = amplitude*numpy.exp(-depth/pent_depth) # -# # get the number of emitted photons -# N_emit = self.get_emit_photons(amplitude) +# # get the number of emitted photons +# N_emit = self.get_emit_photons(amplitude) # -# # reset photon-budget -# photons = budget[m_id] - N_emit*state_j +# # reset photon-budget +# photons = budget[m_id] - N_emit*state_j # -# if (photons > 0) : -# budget[m_id] = photons -# state_pb[m_id,k] = state_j -# else : -# budget[m_id] = 0 -# state_pb[m_id,k] = 0 -# #state_pb[m_id,k] = -1 +# if (photons > 0) : +# budget[m_id] = photons +# state_pb[m_id,k] = state_j +# else : +# budget[m_id] = 0 +# state_pb[m_id,k] = 0 +# #state_pb[m_id,k] = -1 # -# new_state_pb[j] = state_pb[m_id,k] -# new_budget[j] = budget[m_id] +# new_state_pb[j] = state_pb[m_id,k] +# new_budget[j] = budget[m_id] # -# state_stack = numpy.column_stack((new_state_pb, (new_budget/N_emit0).astype('int'))) -# new_dataset = numpy.column_stack((dataset, state_stack)) +# state_stack = numpy.column_stack((new_state_pb, (new_budget/N_emit0).astype('int'))) +# new_dataset = numpy.column_stack((dataset, state_stack)) # -# # reset dissociation and photobleaching states -# if (k > 0) : -# # set molecule array -# molecules = numpy.arange(N_particles) +# # reset dissociation and photobleaching states +# if (k > 0) : +# # set molecule array +# molecules = numpy.arange(N_particles) # -# # get state difference -# diff = state_as - old_state_as -# molecules = molecules[diff < 0] +# # get state difference +# diff = state_as - old_state_as +# molecules = molecules[diff < 0] # -# for x in range(len(molecules)) : -# i = molecules[x] +# for x in range(len(molecules)) : +# i = molecules[x] # -# # reset photon budget and photobleaching-time -# tau_i, budget[i] = self.effects.get_bleach_property(dt, N_emit0) +# # reset photon budget and photobleaching-time +# tau_i, budget[i] = self.effects.get_bleach_property(dt, N_emit0) # -# # reset the photobleaching state -# state_pb[i] = numpy.zeros(shape=(len(state_pb[i]))) +# # reset the photobleaching state +# state_pb[i] = numpy.zeros(shape=(len(state_pb[i]))) # -# Ni = (numpy.abs(numpy.cumsum(delta) - tau_i)).argmin() -# state_pb[i,0:Ni] = numpy.ones(shape=(Ni)) -# -# print 'reset states :', i, tau_i, budget[i], state_pb[i] +# Ni = (numpy.abs(numpy.cumsum(delta) - tau_i)).argmin() +# state_pb[i,0:Ni] = numpy.ones(shape=(Ni)) # -# old_state_as = state_as -# state_as.fill(0) +# print 'reset states :', i, tau_i, budget[i], state_pb[i] # -# # write output file -# output_file = output_file_dir + '/pt-%09d.0.csv' % (count_array[k]) +# old_state_as = state_as +# state_as.fill(0) # -# with open(output_file, 'w') as f : -# writer = csv.writer(f) -# writer.writerows(new_dataset) +# # write output file +# output_file = output_file_dir + '/pt-%09d.0.csv' % (count_array[k]) # -# print 'save :', output_file +# with open(output_file, 'w') as f : +# writer = csv.writer(f) +# writer.writerows(new_dataset) # +# print 'save :', output_file # # - def get_cell_size(self) : +# + def get_cell_size(self) : - # define observational image plane in nm-scale - voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 + # define observational image plane in nm-scale + voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 - ## cell size (nm scale) - Nz = int(self.configs.spatiocyte_lengths[2]*voxel_size) - Ny = int(self.configs.spatiocyte_lengths[1]*voxel_size) - Nx = int(self.configs.spatiocyte_lengths[0]*voxel_size) + ## cell size (nm scale) + Nz = int(self.configs.spatiocyte_lengths[2]*voxel_size) + Ny = int(self.configs.spatiocyte_lengths[1]*voxel_size) + Nx = int(self.configs.spatiocyte_lengths[0]*voxel_size) - return Nx, Ny, Nz + return Nx, Ny, Nz - def get_focal_center(self) : + def get_focal_center(self) : - # get cell size - Nx, Ny, Nz = self.get_cell_size() + # get cell size + Nx, Ny, Nz = self.get_cell_size() - # focal point - f_0 = self.configs.detector_focal_point - p_0 = numpy.array([Nx, Ny, Nz])*f_0 + # focal point + f_0 = self.configs.detector_focal_point + p_0 = numpy.array([Nx, Ny, Nz])*f_0 - return p_0 + return p_0 - def rewrite_InputFile(self, output_file_dir=None) : + def rewrite_InputFile(self, output_file_dir=None) : - if not os.path.exists(output_file_dir): - os.makedirs(output_file_dir) + if not os.path.exists(output_file_dir): + os.makedirs(output_file_dir) - # get focal point - p_0 = self.get_focal_center() + # get focal point + p_0 = self.get_focal_center() - # beam position : Assuming beam position = focal point (for temporary) - p_b = copy.copy(p_0) + # beam position : Assuming beam position = focal point (for temporary) + p_b = copy.copy(p_0) - # set time, delta and count arrays - time_array = numpy.array(self.configs.shutter_time_array) - delta_array = numpy.array(self.configs.shutter_delta_array) - count_array = numpy.array(self.configs.shutter_count_array) + # set time, delta and count arrays + time_array = numpy.array(self.configs.shutter_time_array) + delta_array = numpy.array(self.configs.shutter_delta_array) + count_array = numpy.array(self.configs.shutter_count_array) - # Snell's law - amplitude0, penet_depth = self.snells_law(p_0, p_0) + # Snell's law + amplitude0, penet_depth = self.snells_law(p_0, p_0) - # get the number of emitted photons - N_emit0 = self.get_emit_photons(amplitude0) + # get the number of emitted photons + N_emit0 = self.get_emit_photons(amplitude0) - # copy input-file - csv_input = self.configs.spatiocyte_file_directry + '/pt-input.csv' - shutil.copyfile(csv_input, output_file_dir + '/pt-input.csv') + # copy input-file + csv_input = self.configs.spatiocyte_file_directry + '/pt-input.csv' + shutil.copyfile(csv_input, output_file_dir + '/pt-input.csv') - # copy shape-file - csv_shape = self.configs.spatiocyte_file_directry + '/pt-shape.csv' - shutil.copyfile(csv_shape, output_file_dir + '/pt-shape.csv') + # copy shape-file + csv_shape = self.configs.spatiocyte_file_directry + '/pt-shape.csv' + shutil.copyfile(csv_shape, output_file_dir + '/pt-shape.csv') - # get the total number of particles - N_particles = 4117#len(csv_list) + # get the total number of particles + N_particles = 4117#len(csv_list) - # set molecule-states - self.molecule_states = numpy.zeros(shape=(N_particles)) + # set molecule-states + self.molecule_states = numpy.zeros(shape=(N_particles)) - # set fluorescence - self.effects.set_photophysics_4epifm(time_array, delta_array, N_emit0, N_particles) + # set fluorescence + self.effects.set_photophysics_4epifm(time_array, delta_array, N_emit0, N_particles) - for k in range(len(count_array)) : + for k in range(len(count_array)) : - # read input file - csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (count_array[k]) + # read input file + csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (count_array[k]) - csv_list = list(csv.reader(open(csv_file_path, 'r'))) - dataset = numpy.array(csv_list) + csv_list = list(csv.reader(open(csv_file_path, 'r'))) + dataset = numpy.array(csv_list) - # set molecular-states (unbound-bound) - self.set_molecular_states(k, dataset) + # set molecular-states (unbound-bound) + self.set_molecular_states(k, dataset) - # set photobleaching-dataset arrays - self.set_photobleaching_dataset(k, dataset) + # set photobleaching-dataset arrays + self.set_photobleaching_dataset(k, dataset) - # get new-dataset - new_dataset = self.get_new_dataset(k, N_emit0, dataset) + # get new-dataset + new_dataset = self.get_new_dataset(k, N_emit0, dataset) - # write output file - output_file = output_file_dir + '/pt-%09d.0.csv' % (count_array[k]) + # write output file + output_file = output_file_dir + '/pt-%09d.0.csv' % (count_array[k]) - with open(output_file, 'w') as f : - writer = csv.writer(f) - writer.writerows(new_dataset) + with open(output_file, 'w') as f : + writer = csv.writer(f) + writer.writerows(new_dataset) - def set_molecular_states(self, count, dataset) : + def set_molecular_states(self, count, dataset) : - # reset molecule-states - self.molecule_states.fill(0) + # reset molecule-states + self.molecule_states.fill(0) - # loop for particles - for j, data_j in enumerate(dataset): + # loop for particles + for j, data_j in enumerate(dataset): - # set particle position - p_i = numpy.array(data_j[1:4]).astype('float')/1e-9 + # set particle position + p_i = numpy.array(data_j[1:4]).astype('float')/1e-9 - # Molecule ID and its state - m_id, s_id = literal_eval(data_j[5]) - # Fluorophore ID and compartment ID - f_id, l_id = literal_eval(data_j[6]) + # Molecule ID and its state + m_id, s_id = literal_eval(data_j[5]) + # Fluorophore ID and compartment ID + f_id, l_id = literal_eval(data_j[6]) - # set molecule-states - self.molecule_states[m_id] = int(s_id) + # set molecule-states + self.molecule_states[m_id] = int(s_id) - def set_photobleaching_dataset(self, count, dataset) : + def set_photobleaching_dataset(self, count, dataset) : - if (len(dataset) > 0) : - if self.use_multiprocess(): + if (len(dataset) > 0) : + if self.use_multiprocess(): - # set arrays for photobleaching-state and photon-budget - state_pb = {} - budget = {} + # set arrays for photobleaching-state and photon-budget + state_pb = {} + budget = {} - num_processes = min(multiprocessing.cpu_count(), len(dataset)) - n, m = divmod(len(dataset), num_processes) + num_processes = min(multiprocessing.cpu_count(), len(dataset)) + n, m = divmod(len(dataset), num_processes) - chunks = [n + 1 if i < m else n for i in range(num_processes)] + chunks = [n + 1 if i < m else n for i in range(num_processes)] - processes = [] - start_index = 0 + processes = [] + start_index = 0 - for chunk in chunks: - stop_index = start_index + chunk - p, c = multiprocessing.Pipe() - process = multiprocessing.Process(target=self.get_photobleaching_dataset_process, - args=(count, dataset[start_index:stop_index], c)) - processes.append((p, process)) - start_index = stop_index + for chunk in chunks: + stop_index = start_index + chunk + p, c = multiprocessing.Pipe() + process = multiprocessing.Process(target=self.get_photobleaching_dataset_process, + args=(count, dataset[start_index:stop_index], c)) + processes.append((p, process)) + start_index = stop_index - for _, process in processes: - process.start() + for _, process in processes: + process.start() - for pipe, process in processes: - new_state_pb, new_budget = pipe.recv() - state_pb.update(new_state_pb) - budget.update(new_budget) - process.join() + for pipe, process in processes: + new_state_pb, new_budget = pipe.recv() + state_pb.update(new_state_pb) + budget.update(new_budget) + process.join() - else : - state_pb, budget = self.get_photobleaching_dataset(count, dataset) + else : + state_pb, budget = self.get_photobleaching_dataset(count, dataset) - # reset global-arrays for photobleaching-state and photon-budget - for key, value in state_pb.items() : + # reset global-arrays for photobleaching-state and photon-budget + for key, value in state_pb.items() : - self.effects.fluorescence_state[key,count] = state_pb[key] - self.effects.fluorescence_budget[key] = budget[key] + self.effects.fluorescence_state[key,count] = state_pb[key] + self.effects.fluorescence_budget[key] = budget[key] - def get_new_dataset(self, count, N_emit0, dataset) : + def get_new_dataset(self, count, N_emit0, dataset) : - state_mo = self.molecule_states + state_mo = self.molecule_states - state_pb = self.effects.fluorescence_state[:,count] - budget = self.effects.fluorescence_budget + state_pb = self.effects.fluorescence_state[:,count] + budget = self.effects.fluorescence_budget - # set additional arrays for new-dataset - new_state_pb = state_pb[state_mo > 0] - new_budget = budget[state_mo > 0] + # set additional arrays for new-dataset + new_state_pb = state_pb[state_mo > 0] + new_budget = budget[state_mo > 0] - # set new-dataset - state_stack = numpy.column_stack((new_state_pb, (new_budget/N_emit0).astype('int'))) - new_dataset = numpy.column_stack((dataset, state_stack)) + # set new-dataset + state_stack = numpy.column_stack((new_state_pb, (new_budget/N_emit0).astype('int'))) + new_dataset = numpy.column_stack((dataset, state_stack)) - return new_dataset + return new_dataset - def get_photobleaching_dataset(self, count, dataset) : + def get_photobleaching_dataset(self, count, dataset) : - # get focal point - p_0 = self.get_focal_center() + # get focal point + p_0 = self.get_focal_center() - # set arrays for photobleaching-states and photon-budget - result_state_pb = {} - result_budget = {} + # set arrays for photobleaching-states and photon-budget + result_state_pb = {} + result_budget = {} - # loop for particles - for j, data_j in enumerate(dataset): + # loop for particles + for j, data_j in enumerate(dataset): + + # set particle position + p_i = numpy.array(data_j[1:4]).astype('float')/1e-9 + + # Snell's law + amplitude, penet_depth = self.snells_law(p_i, p_0) + + # particle coordinate in real(nm) scale + p_i, radial, depth = self.get_coordinate(p_i, p_0) + + # Molecule ID and its state + m_id, s_id = literal_eval(data_j[5]) + # Fluorophore ID and compartment ID + f_id, l_id = literal_eval(data_j[6]) + + sid_array = numpy.array(self.configs.spatiocyte_species_id) + s_index = (numpy.abs(sid_array - int(s_id))).argmin() + + if self.configs.spatiocyte_observables[s_index] is True : + state_j = 1 + else : + state_j = 0 + + # get exponential amplitude (only for observation at basal surface) + #amplitide = amplitude*numpy.exp(-depth/pent_depth) + + # get the number of emitted photons + N_emit = self.get_emit_photons(amplitude) + + # get global-arrays for photobleaching-state and photon-budget + state_pb = self.effects.fluorescence_state[m_id,count] + budget = self.effects.fluorescence_budget[m_id] + + # reset photon-budget + photons = budget - N_emit*state_j + + if (photons > 0) : + budget = photons + state_pb = state_j + else : + budget = 0 + state_pb = 0 - # set particle position - p_i = numpy.array(data_j[1:4]).astype('float')/1e-9 + result_state_pb[m_id] = state_pb + result_budget[m_id] = budget - # Snell's law - amplitude, penet_depth = self.snells_law(p_i, p_0) + return result_state_pb, result_budget - # particle coordinate in real(nm) scale - p_i, radial, depth = self.get_coordinate(p_i, p_0) - # Molecule ID and its state - m_id, s_id = literal_eval(data_j[5]) - # Fluorophore ID and compartment ID - f_id, l_id = literal_eval(data_j[6]) - sid_array = numpy.array(self.configs.spatiocyte_species_id) - s_index = (numpy.abs(sid_array - int(s_id))).argmin() + def get_photobleaching_dataset_process(self, count, dataset, pipe) : - if self.configs.spatiocyte_observables[s_index] is True : - state_j = 1 - else : - state_j = 0 + pipe.send(self.get_photobleaching_dataset(count, dataset)) - # get exponential amplitude (only for observation at basal surface) - #amplitide = amplitude*numpy.exp(-depth/pent_depth) - # get the number of emitted photons - N_emit = self.get_emit_photons(amplitude) - # get global-arrays for photobleaching-state and photon-budget - state_pb = self.effects.fluorescence_state[m_id,count] - budget = self.effects.fluorescence_budget[m_id] + def get_molecule_plane(self, cell, j, data_j, p_b, p_0, true_data) : - # reset photon-budget - photons = budget - N_emit*state_j + # particles coordinate, species and lattice-IDs + c_id, m_id, s_id, l_id, p_state, cyc_id = data_j - if (photons > 0) : - budget = photons - state_pb = state_j - else : - budget = 0 - state_pb = 0 + sid_array = numpy.array(self.configs.spatiocyte_species_id) + sid_index = (numpy.abs(sid_array - int(s_id))).argmin() - result_state_pb[m_id] = state_pb - result_budget[m_id] = budget + if self.configs.spatiocyte_observables[sid_index] is True : - return result_state_pb, result_budget + #if (p_state > 0) : + p_i = numpy.array(c_id)/1e-9 + # Snell's law + amplitude, penet_depth = self.snells_law(p_i, p_0) - def get_photobleaching_dataset_process(self, count, dataset, pipe) : + # particles coordinate in real(nm) scale + p_i, radial, depth = self.get_coordinate(p_i, p_0) - pipe.send(self.get_photobleaching_dataset(count, dataset)) + # get exponential amplitude (only for TIRFM-configuration) + amplitude = amplitude*numpy.exp(-depth/penet_depth) + # get signal matrix + signal = self.get_signal(amplitude, radial, depth, p_state) + # add signal matrix to image plane + self.overwrite_signal(cell, signal, p_i) - def get_molecule_plane(self, cell, j, data_j, p_b, p_0, true_data) : + # set true-dataset + true_data[j,1] = m_id # molecule-ID + true_data[j,2] = sid_index # molecular-state + true_data[j,3] = p_state # photon-state + true_data[j,4] = p_i[2] # Y-coordinate in the image-plane + true_data[j,5] = p_i[1] # X-coordinate in the image-plane + true_data[j,6] = depth # Depth from focal-plane - # particles coordinate, species and lattice-IDs - c_id, m_id, s_id, l_id, p_state, cyc_id = data_j - - sid_array = numpy.array(self.configs.spatiocyte_species_id) - sid_index = (numpy.abs(sid_array - int(s_id))).argmin() - if self.configs.spatiocyte_observables[sid_index] is True : - #if (p_state > 0) : + def get_signal(self, amplitude, radial, depth, p_state) : - p_i = numpy.array(c_id)/1e-9 + # fluorophore axial position + fluo_depth = depth if depth < len(self.configs.depth) else -1 - # Snell's law - amplitude, penet_depth = self.snells_law(p_i, p_0) + # get fluorophore PSF + psf_depth = self.fluo_psf[int(fluo_depth)] - # particles coordinate in real(nm) scale - p_i, radial, depth = self.get_coordinate(p_i, p_0) + # get the number of photons emitted + N_emit = self.get_emit_photons(amplitude) - # get exponential amplitude (only for TIRFM-configuration) - amplitude = amplitude*numpy.exp(-depth/penet_depth) + # get signal + signal = p_state*N_emit/(4.0*numpy.pi)*psf_depth - # get signal matrix - signal = self.get_signal(amplitude, radial, depth, p_state) + return signal - # add signal matrix to image plane - self.overwrite_signal(cell, signal, p_i) - # set true-dataset - true_data[j,1] = m_id # molecule-ID - true_data[j,2] = sid_index # molecular-state - true_data[j,3] = p_state # photon-state - true_data[j,4] = p_i[2] # Y-coordinate in the image-plane - true_data[j,5] = p_i[1] # X-coordinate in the image-plane - true_data[j,6] = depth # Depth from focal-plane + def get_emit_photons(self, amplitude) : + # Spatiocyte time interval [sec] + unit_time = self.configs.spatiocyte_interval - def get_signal(self, amplitude, radial, depth, p_state) : + # Absorption coeff [1/(cm M)] + abs_coeff = self.effects.abs_coefficient - # fluorophore axial position - fluo_depth = depth if depth < len(self.configs.depth) else -1 + # Quantum yield + QY = self.effects.quantum_yield - # get fluorophore PSF - psf_depth = self.fluo_psf[int(fluo_depth)] + # Abogadoro's number + Na = self.effects.avogadoros_number - # get the number of photons emitted - N_emit = self.get_emit_photons(amplitude) + # Cross-section [m2] + x_sec = numpy.log(10)*abs_coeff*0.1/Na - # get signal - signal = p_state*N_emit/(4.0*numpy.pi)*psf_depth + # get the number of absorption photons : [#/(m2 sec)]*[m2]*[sec] + n_abs = amplitude*x_sec*unit_time - return signal + # spatiocyte voxel size (~ molecule size) + voxel_radius = self.configs.spatiocyte_VoxelRadius + voxel_volume = (4.0/3.0)*numpy.pi*voxel_radius**3 + voxel_depth = 2.0*voxel_radius + # Beer-Lamberts law : A = log(I0/I) = abs coef. * concentration * path length ([m2]*[#/m3]*[m]) + A = (abs_coeff*0.1/Na)*(1.0/voxel_volume)*(voxel_depth) + # get the number of photons emitted + n_emit = QY*n_abs*(1 - 10**(-A)) - def get_emit_photons(self, amplitude) : + return n_emit - # Spatiocyte time interval [sec] - unit_time = self.configs.spatiocyte_interval - # Absorption coeff [1/(cm M)] - abs_coeff = self.effects.abs_coefficient - # Quantum yield - QY = self.effects.quantum_yield + def snells_law(self, p_i, p_0) : - # Abogadoro's number - Na = self.effects.avogadoros_number + x_0, y_0, z_0 = p_0 + x_i, y_i, z_i = p_i - # Cross-section [m2] - x_sec = numpy.log(10)*abs_coeff*0.1/Na + # (plank const) * (speed of light) [joules meter] + hc = self.configs.hc_const - # get the number of absorption photons : [#/(m2 sec)]*[m2]*[sec] - n_abs = amplitude*x_sec*unit_time + # Illumination : Assume that uniform illumination (No gaussian) + # flux density [W/cm2 (joules/sec/m2)] + P_0 = self.configs.source_flux_density*1e+4 - # spatiocyte voxel size (~ molecule size) - voxel_radius = self.configs.spatiocyte_VoxelRadius - voxel_volume = (4.0/3.0)*numpy.pi*voxel_radius**3 - voxel_depth = 2.0*voxel_radius + # single photon energy + wave_length = self.configs.source_wavelength*1e-9 + E_wl = hc/wave_length - # Beer-Lamberts law : A = log(I0/I) = abs coef. * concentration * path length ([m2]*[#/m3]*[m]) - A = (abs_coeff*0.1/Na)*(1.0/voxel_volume)*(voxel_depth) + # photon flux density [photons/sec/m2] + N_0 = P_0/E_wl - # get the number of photons emitted - n_emit = QY*n_abs*(1 - 10**(-A)) + # Incident beam : Amplitude + A2_Is = N_0 + A2_Ip = N_0 - return n_emit + # incident beam angle + angle = self.configs.source_angle + theta_in = (angle/180.)*numpy.pi + sin_th1 = numpy.sin(theta_in) + cos_th1 = numpy.cos(theta_in) + sin = sin_th1 + cos = cos_th1 + sin2 = sin**2 + cos2 = cos**2 - def snells_law(self, p_i, p_0) : + # index of refraction + n_1 = 1.46 # fused silica + n_2 = 1.384 # cell + n_3 = 1.337 # culture medium - x_0, y_0, z_0 = p_0 - x_i, y_i, z_i = p_i + r = n_2/n_1 + r2 = r**2 - # (plank const) * (speed of light) [joules meter] - hc = self.configs.hc_const - - # Illumination : Assume that uniform illumination (No gaussian) - # flux density [W/cm2 (joules/sec/m2)] - P_0 = self.configs.source_flux_density*1e+4 - - # single photon energy - wave_length = self.configs.source_wavelength*1e-9 - E_wl = hc/wave_length - - # photon flux density [photons/sec/m2] - N_0 = P_0/E_wl - - # Incident beam : Amplitude - A2_Is = N_0 - A2_Ip = N_0 - - # incident beam angle - angle = self.configs.source_angle - theta_in = (angle/180.)*numpy.pi - - sin_th1 = numpy.sin(theta_in) - cos_th1 = numpy.cos(theta_in) - - sin = sin_th1 - cos = cos_th1 - sin2 = sin**2 - cos2 = cos**2 - - # index of refraction - n_1 = 1.46 # fused silica - n_2 = 1.384 # cell - n_3 = 1.337 # culture medium - - r = n_2/n_1 - r2 = r**2 - - # Epi-illumination at apical surface - if (sin2/r2 < 1) : - - # Refracted beam : 2nd beam angle to basal region of the cell - sin_th2 = (n_1/n_2)*sin_th1 - cos_th2 = numpy.sqrt(1 - sin_th2**2) - beam = numpy.array([cos_th2, sin_th2, 0]) - - # Normal vector : Perpendicular to apical surface of the cell - voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius - cell_shape = numpy.array(self.configs.spatiocyte_shape) - - diff = numpy.sqrt(numpy.sum((cell_shape - p_i*1e-9)**2, axis=1)) - k0 = numpy.nonzero(diff < 1.5*voxel_size)[0] - k1 = numpy.nonzero(k0 != diff.argmin())[0] - - r_n = cell_shape[k0[k1]] - - # Optimization is definitely required!! - norm = numpy.array([0, 0, 0]) - count = 0 - for kk in range(len(r_n)) : - for ii in range(len(r_n)) : - for jj in range(len(r_n)) : - if (kk!=ii and kk!=jj and ii!=jj) : - t = r_n[ii] - r_n[kk] - s = r_n[jj] - r_n[kk] - - vec = numpy.cross(s, t) - if (vec[0] < 0) : vec = numpy.cross(t, s) - len_vec = numpy.sqrt(numpy.sum(vec**2)) - if (len_vec > 0) : - norm = norm + vec/len_vec - count += 1 - - norm = norm/count - -# f0_norm = f0_norm/count -# t = r_n[1] - r_n[0] -# s = r_n[-1] - r_n[0] -# -# vec = numpy.cross(s, t) -# vec_len = numpy.sqrt(numpy.sum(vec**2)) -# norm = vec/vec_len - - # Plane to separate between apical and basal cell-surface regions - v_0 = numpy.array([voxel_size/1e-9, y_0, 0]) - v_1 = numpy.array([voxel_size/1e-9, 0, z_0]) - v_2 = numpy.cross(v_0, v_1) - len_v2 = numpy.sqrt(numpy.sum(v_2**2)) - plane_vec = v_2/len_v2 - - # Apical region (>0) and Basal region (<0) - a, b, c = plane_vec - plane_eqn = a*(x_i - voxel_size/1e-9) + b*y_i + c*z_i - - # check the direction of normal vector at each regions - check = numpy.dot(plane_vec, norm) - - if (plane_eqn < 0 and check > 0) : - norm = -norm - elif (plane_eqn > 0 and check < 0) : - norm = -norm - - # Incident beam : 3rd beam angle to apical surface of the cell - #cos_th3 = numpy.dot(beam, norm) - norm_x, norm_y, norm_z = norm - len_norm_xy = numpy.sqrt(norm_x**2 + norm_y**2) - norm_xy = numpy.array([norm_x, norm_y, 0])/len_norm_xy - cos_th3 = numpy.dot(beam, norm_xy) - - if (cos_th3 > 0 and plane_eqn > 0) : - - # Incident beam to apical surface : amplitude - cosT = numpy.sqrt(1 - sin2/r2) - - A2_Ip = A2_Ip*(2*cos/(cosT + r*cos))**2 - A2_Is = A2_Is*(2*cos/(r*cosT + cos))**2 - - # Incident beam to apical surface : 3rd beam angle - cos = cos_th3 - sin = numpy.sqrt(1 - cos**2) - sin2 = sin**2 - cos2 = cos**2 - - r = n_3/n_2 # must be < 1 - r2 = r**2 - - if (sin2/r2 > 1) : - # Evanescent field : Amplitude and Penetration depth - # Assume that the s-polar direction is parallel to y-axis - A2_x = A2_Ip*(4*cos2*(sin2 - r2)/(r2**2*cos2 + sin2 - r2)) - A2_y = A2_Is*(4*cos2/(1 - r2)) - A2_z = A2_Ip*(4*cos2*sin2/(r2**2*cos2 + sin2 - r2)) - - A2_Tp = A2_x + A2_z - A2_Ts = A2_y - - #penetration_depth = wave_length/(4.0*numpy.pi*numpy.sqrt(n_2**2*sin2 - n_3**2)) - - else : - # Epi-fluorescence field : Amplitude and Penetration depth - cosT = numpy.sqrt(1 - sin2/r2) - - A2_Tp = A2_Ip*(2*cos/(cosT + r*cos))**2 - A2_Ts = A2_Is*(2*cos/(r*cosT + cos))**2 - - #penetration_depth = float('inf') - - else : - # Epi-fluorescence field : Amplitude and Penetration depth - A2_Tp = A2_Ip - A2_Ts = A2_Is - - #penetration_depth = float('inf') - - # for temp - penetration_depth = float('inf') - - # TIRF-illumination at basal cell-surface - else : - # Evanescent field : Amplitude and Depth - # Assume that the s-polar direction is parallel to y-axis - A2_x = A2_Ip*(4*cos2*(sin2 - r2)/(r2**2*cos2 + sin2 - r2)) - A2_y = A2_Is*(4*cos2/(1 - r2)) - A2_z = A2_Ip*(4*cos2*sin2/(r2**2*cos2 + sin2 - r2)) - - A2_Tp = A2_x + A2_z - A2_Ts = A2_y - - penetration_depth = wave_length/(4.0*numpy.pi*numpy.sqrt(n_1**2*sin2 - n_2**2)) - penetration_depth = penetration_depth/1e-9 + # Epi-illumination at apical surface + if (sin2/r2 < 1) : - # set illumination amplitude - amplitude = (A2_Tp + A2_Ts)/2 + # Refracted beam : 2nd beam angle to basal region of the cell + sin_th2 = (n_1/n_2)*sin_th1 + cos_th2 = numpy.sqrt(1 - sin_th2**2) + beam = numpy.array([cos_th2, sin_th2, 0]) - return amplitude, penetration_depth + # Normal vector : Perpendicular to apical surface of the cell + voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius + cell_shape = numpy.array(self.configs.spatiocyte_shape) + diff = numpy.sqrt(numpy.sum((cell_shape - p_i*1e-9)**2, axis=1)) + k0 = numpy.nonzero(diff < 1.5*voxel_size)[0] + k1 = numpy.nonzero(k0 != diff.argmin())[0] + r_n = cell_shape[k0[k1]] - def get_coordinate(self, p_i, p_0) : + # Optimization is definitely required!! + norm = numpy.array([0, 0, 0]) + count = 0 + for kk in range(len(r_n)) : + for ii in range(len(r_n)) : + for jj in range(len(r_n)) : + if (kk!=ii and kk!=jj and ii!=jj) : + t = r_n[ii] - r_n[kk] + s = r_n[jj] - r_n[kk] - x_0, y_0, z_0 = p_0 - x_i, y_i, z_i = p_i + vec = numpy.cross(s, t) + if (vec[0] < 0) : vec = numpy.cross(t, s) + len_vec = numpy.sqrt(numpy.sum(vec**2)) + if (len_vec > 0) : + norm = norm + vec/len_vec + count += 1 - # Rotation of focal plane -# norm_p0 = self.configs.detector_focal_norm -# #cos_th0 = numpy.dot(norm_p0, beam) -# cos_th0 = numpy.dot(norm_p0, numpy.array([1, 0, 0])) - cos_th0 = 1 - sin_th0 = numpy.sqrt(1 - cos_th0**2) + norm = norm/count -# th2 = numpy.arccos(cos_th2) -# th0 = numpy.arccos(cos_th0) -# theta = th2 - th0 +# f0_norm = f0_norm/count +# t = r_n[1] - r_n[0] +# s = r_n[-1] - r_n[0] # -# cos_th = numpy.cos(theta) -# sin_th = numpy.sin(theta) - - # Rotational matrix along z-axis - #Rot = numpy.matrix([[cos_th, -sin_th, 0], [sin_th, cos_th, 0], [0, 0, 1]]) - Rot = numpy.matrix([[cos_th0, -sin_th0, 0], [sin_th0, cos_th0, 0], [0, 0, 1]]) - - # Vector of focal point to particle position - vec = p_i - p_0 - len_vec = numpy.sqrt(numpy.sum(vec**2)) - - # Rotated particle position - v_rot = Rot*vec.reshape((3,1)) - p_i = numpy.array(v_rot).ravel() + p_0 +# vec = numpy.cross(s, t) +# vec_len = numpy.sqrt(numpy.sum(vec**2)) +# norm = vec/vec_len - # Normal vector of the focal plane - q_0 = numpy.array([0.0, y_0, 0.0]) - q_1 = numpy.array([0.0, 0.0, z_0]) - R_q0 = numpy.sqrt(numpy.sum(q_0**2)) - R_q1 = numpy.sqrt(numpy.sum(q_1**2)) - - q0_rot = Rot*q_0.reshape((3,1)) - q1_rot = Rot*q_1.reshape((3,1)) - - norm_v = numpy.cross(q0_rot.ravel(), q1_rot.ravel())/(R_q0*R_q1) - - # Radial distance and depth to focal plane - cos_0i = numpy.sum(norm_v*vec)/(1.0*len_vec) - sin_0i = numpy.sqrt(1 - cos_0i**2) - - focal_depth = abs(len_vec*cos_0i) - focal_radial = abs(len_vec*sin_0i) - - return p_i, focal_radial, focal_depth - - - - def polar2cartesian_coordinates(self, r, t, x, y) : - - X, Y = numpy.meshgrid(x, y) - new_r = numpy.sqrt(X*X + Y*Y) - new_t = numpy.arctan2(X, Y) + # Plane to separate between apical and basal cell-surface regions + v_0 = numpy.array([voxel_size/1e-9, y_0, 0]) + v_1 = numpy.array([voxel_size/1e-9, 0, z_0]) + v_2 = numpy.cross(v_0, v_1) + len_v2 = numpy.sqrt(numpy.sum(v_2**2)) + plane_vec = v_2/len_v2 - ir = interp1d(r, numpy.arange(len(r)), bounds_error=False) - it = interp1d(t, numpy.arange(len(t))) + # Apical region (>0) and Basal region (<0) + a, b, c = plane_vec + plane_eqn = a*(x_i - voxel_size/1e-9) + b*y_i + c*z_i - new_ir = ir(new_r.ravel()) - new_it = it(new_t.ravel()) + # check the direction of normal vector at each regions + check = numpy.dot(plane_vec, norm) - new_ir[new_r.ravel() > r.max()] = len(r)-1 - new_ir[new_r.ravel() < r.min()] = 0 + if (plane_eqn < 0 and check > 0) : + norm = -norm + elif (plane_eqn > 0 and check < 0) : + norm = -norm + + # Incident beam : 3rd beam angle to apical surface of the cell + #cos_th3 = numpy.dot(beam, norm) + norm_x, norm_y, norm_z = norm + len_norm_xy = numpy.sqrt(norm_x**2 + norm_y**2) + norm_xy = numpy.array([norm_x, norm_y, 0])/len_norm_xy + cos_th3 = numpy.dot(beam, norm_xy) + + if (cos_th3 > 0 and plane_eqn > 0) : + + # Incident beam to apical surface : amplitude + cosT = numpy.sqrt(1 - sin2/r2) + + A2_Ip = A2_Ip*(2*cos/(cosT + r*cos))**2 + A2_Is = A2_Is*(2*cos/(r*cosT + cos))**2 + + # Incident beam to apical surface : 3rd beam angle + cos = cos_th3 + sin = numpy.sqrt(1 - cos**2) + sin2 = sin**2 + cos2 = cos**2 + + r = n_3/n_2 # must be < 1 + r2 = r**2 + + if (sin2/r2 > 1) : + # Evanescent field : Amplitude and Penetration depth + # Assume that the s-polar direction is parallel to y-axis + A2_x = A2_Ip*(4*cos2*(sin2 - r2)/(r2**2*cos2 + sin2 - r2)) + A2_y = A2_Is*(4*cos2/(1 - r2)) + A2_z = A2_Ip*(4*cos2*sin2/(r2**2*cos2 + sin2 - r2)) + + A2_Tp = A2_x + A2_z + A2_Ts = A2_y + + #penetration_depth = wave_length/(4.0*numpy.pi*numpy.sqrt(n_2**2*sin2 - n_3**2)) - return numpy.array([new_ir, new_it]) + else : + # Epi-fluorescence field : Amplitude and Penetration depth + cosT = numpy.sqrt(1 - sin2/r2) + + A2_Tp = A2_Ip*(2*cos/(cosT + r*cos))**2 + A2_Ts = A2_Is*(2*cos/(r*cosT + cos))**2 + + #penetration_depth = float('inf') + + else : + # Epi-fluorescence field : Amplitude and Penetration depth + A2_Tp = A2_Ip + A2_Ts = A2_Is + + #penetration_depth = float('inf') + + # for temp + penetration_depth = float('inf') + + # TIRF-illumination at basal cell-surface + else : + # Evanescent field : Amplitude and Depth + # Assume that the s-polar direction is parallel to y-axis + A2_x = A2_Ip*(4*cos2*(sin2 - r2)/(r2**2*cos2 + sin2 - r2)) + A2_y = A2_Is*(4*cos2/(1 - r2)) + A2_z = A2_Ip*(4*cos2*sin2/(r2**2*cos2 + sin2 - r2)) + + A2_Tp = A2_x + A2_z + A2_Ts = A2_y + + penetration_depth = wave_length/(4.0*numpy.pi*numpy.sqrt(n_1**2*sin2 - n_2**2)) + penetration_depth = penetration_depth/1e-9 + + # set illumination amplitude + amplitude = (A2_Tp + A2_Ts)/2 + + return amplitude, penetration_depth + + + + def get_coordinate(self, p_i, p_0) : + + x_0, y_0, z_0 = p_0 + x_i, y_i, z_i = p_i + + # Rotation of focal plane +# norm_p0 = self.configs.detector_focal_norm +# #cos_th0 = numpy.dot(norm_p0, beam) +# cos_th0 = numpy.dot(norm_p0, numpy.array([1, 0, 0])) + cos_th0 = 1 + sin_th0 = numpy.sqrt(1 - cos_th0**2) + +# th2 = numpy.arccos(cos_th2) +# th0 = numpy.arccos(cos_th0) +# theta = th2 - th0 +# +# cos_th = numpy.cos(theta) +# sin_th = numpy.sin(theta) + + # Rotational matrix along z-axis + #Rot = numpy.matrix([[cos_th, -sin_th, 0], [sin_th, cos_th, 0], [0, 0, 1]]) + Rot = numpy.matrix([[cos_th0, -sin_th0, 0], [sin_th0, cos_th0, 0], [0, 0, 1]]) + + # Vector of focal point to particle position + vec = p_i - p_0 + len_vec = numpy.sqrt(numpy.sum(vec**2)) + + # Rotated particle position + v_rot = Rot*vec.reshape((3,1)) + p_i = numpy.array(v_rot).ravel() + p_0 + + # Normal vector of the focal plane + q_0 = numpy.array([0.0, y_0, 0.0]) + q_1 = numpy.array([0.0, 0.0, z_0]) + R_q0 = numpy.sqrt(numpy.sum(q_0**2)) + R_q1 = numpy.sqrt(numpy.sum(q_1**2)) + + q0_rot = Rot*q_0.reshape((3,1)) + q1_rot = Rot*q_1.reshape((3,1)) + + norm_v = numpy.cross(q0_rot.ravel(), q1_rot.ravel())/(R_q0*R_q1) + + # Radial distance and depth to focal plane + cos_0i = numpy.sum(norm_v*vec)/(1.0*len_vec) + sin_0i = numpy.sqrt(1 - cos_0i**2) + + focal_depth = abs(len_vec*cos_0i) + focal_radial = abs(len_vec*sin_0i) + + return p_i, focal_radial, focal_depth + + + + def polar2cartesian_coordinates(self, r, t, x, y) : + + X, Y = numpy.meshgrid(x, y) + new_r = numpy.sqrt(X*X + Y*Y) + new_t = numpy.arctan2(X, Y) + + ir = interp1d(r, numpy.arange(len(r)), bounds_error=False) + it = interp1d(t, numpy.arange(len(t))) + + new_ir = ir(new_r.ravel()) + new_it = it(new_t.ravel()) + + new_ir[new_r.ravel() > r.max()] = len(r)-1 + new_ir[new_r.ravel() < r.min()] = 0 + return numpy.array([new_ir, new_it]) - def polar2cartesian(self, grid, coordinates, shape) : - r = shape[0] - 1 - psf_cart = numpy.empty([2*r + 1, 2*r + 1]) - psf_cart[r:, r:] = map_coordinates(grid, coordinates, order=0).reshape(shape) - psf_cart[r:, :r] = psf_cart[r:, :r:-1] - psf_cart[:r, :] = psf_cart[:r:-1, :] + def polar2cartesian(self, grid, coordinates, shape) : - return psf_cart + r = shape[0] - 1 + psf_cart = numpy.empty([2*r + 1, 2*r + 1]) + psf_cart[r:, r:] = map_coordinates(grid, coordinates, order=0).reshape(shape) + psf_cart[r:, :r] = psf_cart[r:, :r:-1] + psf_cart[:r, :] = psf_cart[:r:-1, :] + return psf_cart - def overwrite_signal(self, cell, signal, p_i) : - # particle position - x_i, y_i, z_i = p_i + def overwrite_signal(self, cell, signal, p_i) : - # - flag = True + # particle position + x_i, y_i, z_i = p_i - # z-axis - Nz_cell = len(cell) - Nz_signal = len(signal) - Nr = len(self.configs.radial) + # + flag = True - z_to = z_i + Nr - z_from = z_i - Nr + # z-axis + Nz_cell = len(cell) + Nz_signal = len(signal) + Nr = len(self.configs.radial) - if (z_to > Nz_cell) : - dz_to = z_to - Nz_cell - z0_to = int(Nz_cell) - zi_to = int(Nz_signal - dz_to) - elif (z_to > 0 and z_to < Nz_cell) : - dz_to = Nz_cell - z_to - z0_to = int(Nz_cell - dz_to) - zi_to = int(Nz_signal) - else : - flag = False + z_to = z_i + Nr + z_from = z_i - Nr - if (z_from < 0) : - dz_from = abs(z_from) - z0_from = 0 - zi_from = int(dz_from) - elif (z_from > 0 and z_from < Nz_cell) : - dz_from = z_from - z0_from = int(dz_from) - zi_from = 0 - else : - flag = False + if (z_to > Nz_cell) : + dz_to = z_to - Nz_cell + z0_to = int(Nz_cell) + zi_to = int(Nz_signal - dz_to) + elif (z_to > 0 and z_to < Nz_cell) : + dz_to = Nz_cell - z_to + z0_to = int(Nz_cell - dz_to) + zi_to = int(Nz_signal) + else : + flag = False - # y-axis - Ny_cell = cell.size/Nz_cell - Ny_signal = signal.size/Nz_signal + if (z_from < 0) : + dz_from = abs(z_from) + z0_from = 0 + zi_from = int(dz_from) + elif (z_from > 0 and z_from < Nz_cell) : + dz_from = z_from + z0_from = int(dz_from) + zi_from = 0 + else : + flag = False - y_to = y_i + Nr - y_from = y_i - Nr + # y-axis + Ny_cell = cell.size/Nz_cell + Ny_signal = signal.size/Nz_signal - if (y_to > Ny_cell) : - dy_to = y_to - Ny_cell - y0_to = int(Ny_cell) - yi_to = int(Ny_signal - dy_to) - elif (y_to > 0 and y_to < Ny_cell) : - dy_to = Ny_cell - y_to - y0_to = int(Ny_cell - dy_to) - yi_to = int(Ny_signal) - else : - flag = False + y_to = y_i + Nr + y_from = y_i - Nr - if (y_from < 0) : - dy_from = abs(y_from) - y0_from = 0 - yi_from = int(dy_from) - elif (y_from > 0 and y_from < Ny_cell) : - dy_from = y_from - y0_from = int(dy_from) - yi_from = 0 - else : - flag = False + if (y_to > Ny_cell) : + dy_to = y_to - Ny_cell + y0_to = int(Ny_cell) + yi_to = int(Ny_signal - dy_to) + elif (y_to > 0 and y_to < Ny_cell) : + dy_to = Ny_cell - y_to + y0_to = int(Ny_cell - dy_to) + yi_to = int(Ny_signal) + else : + flag = False - # signal plane in the cellular plane - if (flag == True) : + if (y_from < 0) : + dy_from = abs(y_from) + y0_from = 0 + yi_from = int(dy_from) + elif (y_from > 0 and y_from < Ny_cell) : + dy_from = y_from + y0_from = int(dy_from) + yi_from = 0 + else : + flag = False - # adjust index - ddy = (y0_to - y0_from) - (yi_to - yi_from) - ddz = (z0_to - z0_from) - (zi_to - zi_from) + # signal plane in the cellular plane + if (flag == True) : - if (ddy != 0) : y0_to = y0_to - ddy - if (ddz != 0) : z0_to = z0_to - ddz + # adjust index + ddy = (y0_to - y0_from) - (yi_to - yi_from) + ddz = (z0_to - z0_from) - (zi_to - zi_from) - # add to cellular plane - cell[z0_from:z0_to, y0_from:y0_to] += signal[zi_from:zi_to, yi_from:yi_to] + if (ddy != 0) : y0_to = y0_to - ddy + if (ddz != 0) : z0_to = z0_to - ddz + # add to cellular plane + cell[z0_from:z0_to, y0_from:y0_to] += signal[zi_from:zi_to, yi_from:yi_to] - def output_frames(self, num_div=1): - # set Fluorophores PSF - self.set_fluo_psf() + def output_frames(self, num_div=1): - index_array = numpy.array(self.configs.shutter_index_array) - num_timesteps = len(index_array) - index0 = index_array[0] - - envname = 'ECELL_MICROSCOPE_SINGLE_PROCESS' + # set Fluorophores PSF + self.set_fluo_psf() - if envname in os.environ and os.environ[envname]: + index_array = numpy.array(self.configs.shutter_index_array) + num_timesteps = len(index_array) + index0 = index_array[0] + + envname = 'ECELL_MICROSCOPE_SINGLE_PROCESS' + + if envname in os.environ and os.environ[envname]: # if True: - self.output_frames_each_process(index0, num_timesteps) - else : - num_processes = multiprocessing.cpu_count() - n, m = divmod(num_timesteps, num_processes) - # when 10 tasks is distributed to 4 processes, - # number of tasks of each process must be [3, 3, 2, 2] - chunks = [n + 1 if i < m else n for i in range(num_processes)] + self.output_frames_each_process(index0, num_timesteps) + else : + num_processes = multiprocessing.cpu_count() + n, m = divmod(num_timesteps, num_processes) + # when 10 tasks is distributed to 4 processes, + # number of tasks of each process must be [3, 3, 2, 2] + chunks = [n + 1 if i < m else n for i in range(num_processes)] - processes = [] - start_index = index0 + processes = [] + start_index = index0 - for chunk in chunks: - stop_index = start_index + chunk + for chunk in chunks: + stop_index = start_index + chunk - process = multiprocessing.Process( - target=self.output_frames_each_process, - args=(start_index, stop_index)) - process.start() - processes.append(process) - start_index = stop_index + process = multiprocessing.Process( + target=self.output_frames_each_process, + args=(start_index, stop_index)) + process.start() + processes.append(process) + start_index = stop_index - for process in processes: - process.join() + for process in processes: + process.join() - def output_frames_each_process(self, start_index, stop_index): + def output_frames_each_process(self, start_index, stop_index): - # cell size (nm scale) - Nx, Ny, Nz = self.get_cell_size() + # cell size (nm scale) + Nx, Ny, Nz = self.get_cell_size() - # focal point - p_0 = self.get_focal_center() + # focal point + p_0 = self.get_focal_center() - # beam position : Assuming beam position = focal point (for temporary) - p_b = copy.copy(p_0) + # beam position : Assuming beam position = focal point (for temporary) + p_b = copy.copy(p_0) - # exposure time - exposure_time = self.configs.detector_exposure_time + # exposure time + exposure_time = self.configs.detector_exposure_time - # spatiocyte time interval - data_interval = self.configs.spatiocyte_interval + # spatiocyte time interval + data_interval = self.configs.spatiocyte_interval - # set delta_count - delta_count = int(round(exposure_time/data_interval)) + # set delta_count + delta_count = int(round(exposure_time/data_interval)) - index_array = numpy.array(self.configs.shutter_index_array) - index0 = index_array[0] + index_array = numpy.array(self.configs.shutter_index_array) + index0 = index_array[0] - for index in range(start_index, stop_index, 1) : + for index in range(start_index, stop_index, 1) : - # frame-time in sec - time = exposure_time*index + # frame-time in sec + time = exposure_time*index - print('time : ', time, 'sec (', index, ')') + print('time : ', time, 'sec (', index, ')') - # define cell in nm-scale - cell = numpy.zeros(shape=(Nz, Ny)) + # define cell in nm-scale + cell = numpy.zeros(shape=(Nz, Ny)) - c0 = (index - index0)*delta_count - c1 = (index - index0 + 1)*delta_count + c0 = (index - index0)*delta_count + c1 = (index - index0 + 1)*delta_count - frame_data = self.configs.spatiocyte_data[c0:c1] + frame_data = self.configs.spatiocyte_data[c0:c1] - # loop for frame data - for i, (i_time, data) in enumerate(frame_data): - print('\t', '%02d-th frame : ' % (i), i_time, ' sec') + # loop for frame data + for i, (i_time, data) in enumerate(frame_data): + print('\t', '%02d-th frame : ' % (i), i_time, ' sec') - # define true-dataset in last-frame - # [frame-time, m-ID, m-state, p-state, (depth,y0,z0), sqrt()] - true_data = numpy.zeros(shape=(len(data),7)) - true_data[:,0] = time + # define true-dataset in last-frame + # [frame-time, m-ID, m-state, p-state, (depth,y0,z0), sqrt()] + true_data = numpy.zeros(shape=(len(data),7)) + true_data[:,0] = time - # loop for particles - for j, data_j in enumerate(data): - self.get_molecule_plane(cell, j, data_j, p_b, p_0, true_data) + # loop for particles + for j, data_j in enumerate(data): + self.get_molecule_plane(cell, j, data_j, p_b, p_0, true_data) - # convert image in pixel-scale - camera, true_data = self.detector_output(cell, true_data) - - # save image to numpy-binary file - image_file_name = os.path.join(self.configs.image_file_dir, - self.configs.image_file_name_format % (index)) - numpy.save(image_file_name, camera) + # convert image in pixel-scale + camera, true_data = self.detector_output(cell, true_data) - # save true-dataset to numpy-binary file - true_file_name = os.path.join(self.configs.image_file_dir, - self.configs.true_file_name_format % (index)) - numpy.save(true_file_name, true_data) + # save image to numpy-binary file + image_file_name = os.path.join(self.configs.image_file_dir, + self.configs.image_file_name_format % (index)) + numpy.save(image_file_name, camera) + # save true-dataset to numpy-binary file + true_file_name = os.path.join(self.configs.image_file_dir, + self.configs.true_file_name_format % (index)) + numpy.save(true_file_name, true_data) - def overwrite_smeared(self, cell_pixel, photon_dist, i, j) : - # i-th pixel - Ni_pixel = len(cell_pixel) - Ni_pe = len(photon_dist) + def overwrite_smeared(self, cell_pixel, photon_dist, i, j) : - i_to = i + Ni_pe/2 - i_from = i - Ni_pe/2 + # i-th pixel + Ni_pixel = len(cell_pixel) + Ni_pe = len(photon_dist) - if (i_to > Ni_pixel) : - di_to = i_to - Ni_pixel - i10_to = int(Ni_pixel) - i1_to = int(Ni_pe - di_to) - else : - di_to = Ni_pixel - (i + Ni_pe/2) - i0_to = int(Ni_pixel - di_to) - i1_to = int(Ni_pe) - - if (i_from < 0) : - di_from = abs(i_from) - i0_from = 0 - i1_from = int(di_from) - else : - di_from = i_from - i0_from = int(di_from) - i1_from = 0 + i_to = i + Ni_pe/2 + i_from = i - Ni_pe/2 - ddi = (i0_to - i0_from) - (i1_to - i1_from) + if (i_to > Ni_pixel) : + di_to = i_to - Ni_pixel + i10_to = int(Ni_pixel) + i1_to = int(Ni_pe - di_to) + else : + di_to = Ni_pixel - (i + Ni_pe/2) + i0_to = int(Ni_pixel - di_to) + i1_to = int(Ni_pe) - if (ddi != 0) : i0_to = i0_to - ddi - if (ddi != 0) : i1_to = i1_to - ddi + if (i_from < 0) : + di_from = abs(i_from) + i0_from = 0 + i1_from = int(di_from) + else : + di_from = i_from + i0_from = int(di_from) + i1_from = 0 - # j-th pixel - Nj_pixel = len(cell_pixel[0]) - Nj_pe = len(photon_dist[0]) + ddi = (i0_to - i0_from) - (i1_to - i1_from) - j_to = j + Nj_pe/2 - j_from = j - Nj_pe/2 + if (ddi != 0) : i0_to = i0_to - ddi + if (ddi != 0) : i1_to = i1_to - ddi - if (j_to > Nj_pixel): - dj_to = j_to - Nj_pixel - j0_to = int(Nj_pixel) - j1_to = int(Nj_pe - dj_to) - else : - dj_to = Nj_pixel - (j + Nj_pe/2) - j0_to = int(Nj_pixel - dj_to) - j1_to = int(Nj_pe) - - if (j_from < 0) : - dj_from = abs(j_from) - j0_from = 0 - j1_from = int(dj_from) - else : - dj_from = j_from - j0_from = int(dj_from) - j1_from = 0 + # j-th pixel + Nj_pixel = len(cell_pixel[0]) + Nj_pe = len(photon_dist[0]) - ddj = (j0_to - j0_from) - (j1_to - j1_from) + j_to = j + Nj_pe/2 + j_from = j - Nj_pe/2 - if (ddj != 0) : j0_to = j0_to - ddj - if (ddj != 0) : j1_to = j1_to - ddj + if (j_to > Nj_pixel): + dj_to = j_to - Nj_pixel + j0_to = int(Nj_pixel) + j1_to = int(Nj_pe - dj_to) + else : + dj_to = Nj_pixel - (j + Nj_pe/2) + j0_to = int(Nj_pixel - dj_to) + j1_to = int(Nj_pe) - # add to cellular plane - cell_pixel[i0_from:i0_to, j0_from:j0_to] += photon_dist[i1_from:i1_to, j1_from:j1_to] + if (j_from < 0) : + dj_from = abs(j_from) + j0_from = 0 + j1_from = int(dj_from) + else : + dj_from = j_from + j0_from = int(dj_from) + j1_from = 0 - return cell_pixel + ddj = (j0_to - j0_from) - (j1_to - j1_from) + if (ddj != 0) : j0_to = j0_to - ddj + if (ddj != 0) : j1_to = j1_to - ddj + # add to cellular plane + cell_pixel[i0_from:i0_to, j0_from:j0_to] += photon_dist[i1_from:i1_to, j1_from:j1_to] - def prob_EMCCD(self, S, E) : + return cell_pixel - # get EM gain - M = self.configs.detector_emgain - a = 1.00/M - prob = numpy.zeros(shape=(len(S))) - if (S[0] > 0): - prob = numpy.sqrt(a*E/S)*numpy.exp(-a*S-E+2*numpy.sqrt(a*E*S))*i1e(2*numpy.sqrt(a*E*S)) - else : - prob[0] = numpy.exp(-E) - prob[1:] = numpy.sqrt(a*E/S[1:])*numpy.exp(-a*S[1:]-E+2*numpy.sqrt(a*E*S[1:]))*i1e(2*numpy.sqrt(a*E*S[1:])) + def prob_EMCCD(self, S, E) : - return prob + # get EM gain + M = self.configs.detector_emgain + a = 1.00/M + prob = numpy.zeros(shape=(len(S))) + if (S[0] > 0): + prob = numpy.sqrt(a*E/S)*numpy.exp(-a*S-E+2*numpy.sqrt(a*E*S))*i1e(2*numpy.sqrt(a*E*S)) + else : + prob[0] = numpy.exp(-E) + prob[1:] = numpy.sqrt(a*E/S[1:])*numpy.exp(-a*S[1:]-E+2*numpy.sqrt(a*E*S[1:]))*i1e(2*numpy.sqrt(a*E*S[1:])) - def detector_output(self, cell, true_data) : + return prob - # Detector Output - voxel_radius = self.configs.spatiocyte_VoxelRadius - voxel_size = (2.0*voxel_radius)/1e-9 - Nw_pixel, Nh_pixel = self.configs.detector_image_size - Np = self.configs.image_scaling*voxel_size + def detector_output(self, cell, true_data) : - # cell in nm-scale - Nw_cell = len(cell) - Nh_cell = len(cell[0]) + # Detector Output + voxel_radius = self.configs.spatiocyte_VoxelRadius + voxel_size = (2.0*voxel_radius)/1e-9 - # cell in pixel-scale - Nw_cell_pixel = int(Nw_cell/Np) - Nh_cell_pixel = int(Nh_cell/Np) + Nw_pixel, Nh_pixel = self.configs.detector_image_size - # dummy image in pixel-scale - if (Nw_cell_pixel > Nw_pixel) : - #Nw_dummy = 2*Nw_cell_pixel - Nw_dummy = Nw_cell_pixel - else : - Nw_dummy = 2*Nw_pixel + Np = self.configs.image_scaling*voxel_size - if (Nh_cell_pixel > Nh_pixel) : - #Nh_dummy = 2*Nh_cell_pixel - Nh_dummy = Nh_cell_pixel - else : - Nh_dummy = 2*Nh_pixel + # cell in nm-scale + Nw_cell = len(cell) + Nh_cell = len(cell[0]) - # dummy image in nm-scale - Nw_camera = Nw_dummy*Np - Nh_camera = Nh_dummy*Np + # cell in pixel-scale + Nw_cell_pixel = int(Nw_cell/Np) + Nh_cell_pixel = int(Nh_cell/Np) - # weidth - w_cam_from = Nw_camera/2.0 - Nw_cell/2.0 - w_cam_to = Nw_camera/2.0 + Nw_cell/2.0 + # dummy image in pixel-scale + if (Nw_cell_pixel > Nw_pixel) : + #Nw_dummy = 2*Nw_cell_pixel + Nw_dummy = Nw_cell_pixel + else : + Nw_dummy = 2*Nw_pixel - if (w_cam_from < 0) : - w_cel_from = abs(w_cam_from) - w_cam_from = 0 - else : - w_cel_from = 0 + if (Nh_cell_pixel > Nh_pixel) : + #Nh_dummy = 2*Nh_cell_pixel + Nh_dummy = Nh_cell_pixel + else : + Nh_dummy = 2*Nh_pixel - if (w_cam_to > Nw_camera) : - w_cel_to = Nw_cell - (w_cam_to - Nw_camera) - w_cam_to = Nw_camera - else : - w_cel_to = Nw_cell + # dummy image in nm-scale + Nw_camera = Nw_dummy*Np + Nh_camera = Nh_dummy*Np - # height - h_cam_from = Nh_camera/2.0 - Nh_cell/2.0 - h_cam_to = Nh_camera/2.0 + Nh_cell/2.0 + # weidth + w_cam_from = Nw_camera/2.0 - Nw_cell/2.0 + w_cam_to = Nw_camera/2.0 + Nw_cell/2.0 - if (h_cam_from < 0) : - h_cel_from = abs(h_cam_from) - h_cam_from = 0 - else : - h_cel_from = 0 + if (w_cam_from < 0) : + w_cel_from = abs(w_cam_from) + w_cam_from = 0 + else : + w_cel_from = 0 - if (h_cam_to > Nh_camera) : - h_cel_to = Nh_cell - (h_cam_to - Nh_camera) - h_cam_to = Nh_camera - else : - h_cel_to = Nh_cell + if (w_cam_to > Nw_camera) : + w_cel_to = Nw_cell - (w_cam_to - Nw_camera) + w_cam_to = Nw_camera + else : + w_cel_to = Nw_cell - # image in nm-scale - w_cel0, w_cel1 = int(w_cel_from), int(w_cel_to) - h_cel0, h_cel1 = int(h_cel_from), int(h_cel_to) + # height + h_cam_from = Nh_camera/2.0 - Nh_cell/2.0 + h_cam_to = Nh_camera/2.0 + Nh_cell/2.0 - plane = cell[w_cel0:w_cel1, h_cel0:h_cel1] + if (h_cam_from < 0) : + h_cel_from = abs(h_cam_from) + h_cam_from = 0 + else : + h_cel_from = 0 - # declear cell image in pixel-scale - cell_pixel = numpy.zeros([Nw_cell_pixel, Nh_cell_pixel]) + if (h_cam_to > Nh_camera) : + h_cel_to = Nh_cell - (h_cam_to - Nh_camera) + h_cam_to = Nh_camera + else : + h_cel_to = Nh_cell - # Signal (photon distribution on cell) - for i in range(Nw_cell_pixel) : - for j in range(Nh_cell_pixel) : + # image in nm-scale + w_cel0, w_cel1 = int(w_cel_from), int(w_cel_to) + h_cel0, h_cel1 = int(h_cel_from), int(h_cel_to) - # get photons - i0, i1 = int(i*Np), int((i+1)*Np) - j0, j1 = int(j*Np), int((j+1)*Np) + plane = cell[w_cel0:w_cel1, h_cel0:h_cel1] - photons = numpy.sum(plane[i0:i1,j0:j1]) - #photons = numpy.sum(plane[int(i*Np):int((i+1)*Np),int(j*Np):int((j+1)*Np)]) - #cell_pixel[i][j] = photons + # declear cell image in pixel-scale + cell_pixel = numpy.zeros([Nw_cell_pixel, Nh_cell_pixel]) - if (photons > 0) : + # Signal (photon distribution on cell) + for i in range(Nw_cell_pixel) : + for j in range(Nh_cell_pixel) : - # get crosstalk - if (self.effects.crosstalk_switch == True) : + # get photons + i0, i1 = int(i*Np), int((i+1)*Np) + j0, j1 = int(j*Np), int((j+1)*Np) - width = self.effects.crosstalk_width + photons = numpy.sum(plane[i0:i1,j0:j1]) + #photons = numpy.sum(plane[int(i*Np):int((i+1)*Np),int(j*Np):int((j+1)*Np)]) + #cell_pixel[i][j] = photons - n_i = numpy.random.normal(0, width, int(photons)) - n_j = numpy.random.normal(0, width, int(photons)) + if (photons > 0) : - smeared_photons, edge_i, edge_j = numpy.histogram2d(n_i, n_j, bins=(24, 24), - range=[[-12,12],[-12,12]]) + # get crosstalk + if (self.effects.crosstalk_switch == True) : - # smeared photon distributions - cell_pixel = self.overwrite_smeared(cell_pixel, smeared_photons, i, j) + width = self.effects.crosstalk_width - else : - cell_pixel[i][j] = photons + n_i = numpy.random.normal(0, width, int(photons)) + n_j = numpy.random.normal(0, width, int(photons)) + smeared_photons, edge_i, edge_j = numpy.histogram2d(n_i, n_j, bins=(24, 24), + range=[[-12,12],[-12,12]]) - # declear photon distribution for dummy image - dummy_pixel = numpy.zeros([Nw_dummy, Nh_dummy]) + # smeared photon distributions + cell_pixel = self.overwrite_smeared(cell_pixel, smeared_photons, i, j) - w_cam0 = int(w_cam_from/Np) - w_cam1 = int(w_cam_to/Np) - h_cam0 = int(h_cam_from/Np) - h_cam1 = int(h_cam_to/Np) + else : + cell_pixel[i][j] = photons - w_cel0 = int(w_cel_from/Np) - w_cel1 = int(w_cel_to/Np) - h_cel0 = int(h_cel_from/Np) - h_cel1 = int(h_cel_to/Np) - ddw = (w_cam1 - w_cam0) - (w_cel1 - w_cel0) - ddh = (h_cam1 - h_cam0) - (h_cel1 - h_cel0) + # declear photon distribution for dummy image + dummy_pixel = numpy.zeros([Nw_dummy, Nh_dummy]) - if (ddw > 0) : w_cam1 = w_cam1 - ddw - if (ddw < 0) : w_cel1 = w_cel1 + ddw - if (ddh > 0) : h_cam1 = h_cam1 - ddh - if (ddh < 0) : h_cel1 = h_cel1 + ddh + w_cam0 = int(w_cam_from/Np) + w_cam1 = int(w_cam_to/Np) + h_cam0 = int(h_cam_from/Np) + h_cam1 = int(h_cam_to/Np) - # place cell_pixel data to camera image - dummy_pixel[w_cam0:w_cam1, h_cam0:h_cam1] = cell_pixel[w_cel0:w_cel1, h_cel0:h_cel1] + w_cel0 = int(w_cel_from/Np) + w_cel1 = int(w_cel_to/Np) + h_cel0 = int(h_cel_from/Np) + h_cel1 = int(h_cel_to/Np) - # get focal point - f_x, f_y, f_z = self.configs.detector_focal_point + ddw = (w_cam1 - w_cam0) - (w_cel1 - w_cel0) + ddh = (h_cam1 - h_cam0) - (h_cel1 - h_cel0) - # get dummy-image center and focal position - w0 = Nw_dummy/2 - int(Nw_cell/Np*(0.5-f_z)) - h0 = Nh_dummy/2 - int(Nh_cell/Np*(0.5-f_y)) + if (ddw > 0) : w_cam1 = w_cam1 - ddw + if (ddw < 0) : w_cel1 = w_cel1 + ddw + if (ddh > 0) : h_cam1 = h_cam1 - ddh + if (ddh < 0) : h_cel1 = h_cel1 + ddh - # dummy_pixel image to camera_pixel image - camera_pixel = numpy.zeros([Nw_pixel, Nh_pixel, 2]) - camera_pixel[:,:,0] = dummy_pixel[w0-Nw_pixel/2:w0+Nw_pixel/2, h0-Nh_pixel/2:h0+Nh_pixel/2] + # place cell_pixel data to camera image + dummy_pixel[w_cam0:w_cam1, h_cam0:h_cam1] = cell_pixel[w_cel0:w_cel1, h_cel0:h_cel1] - print('scaling [nm/pixel] :', Np) - print('center (width, height) :', w0, h0) + # get focal point + f_x, f_y, f_z = self.configs.detector_focal_point - # set true-dataset in pixel-scale - true_data[:,4] = true_data[:,4]/Np - w_cel0 + w_cam0 - (w0 - (Nw_pixel-1)/2.0) - true_data[:,5] = true_data[:,5]/Np - h_cel0 + h_cam0 - (h0 - (Nh_pixel-1)/2.0) + # get dummy-image center and focal position + w0 = Nw_dummy/2 - int(Nw_cell/Np*(0.5-f_z)) + h0 = Nh_dummy/2 - int(Nh_cell/Np*(0.5-f_y)) - # set seed for random number - numpy.random.seed() + # dummy_pixel image to camera_pixel image + camera_pixel = numpy.zeros([Nw_pixel, Nh_pixel, 2]) + camera_pixel[:,:,0] = dummy_pixel[w0-Nw_pixel/2:w0+Nw_pixel/2, h0-Nh_pixel/2:h0+Nh_pixel/2] - # CMOS (readout noise probability ditributions) - if (self.configs.detector_type == "CMOS") : - noise_data = numpy.loadtxt(os.path.join(os.path.abspath(os.path.dirname(__file__)), - 'catalog/detector/RNDist_F40.csv'), delimiter=',') - Nr_cmos = noise_data[:,0] - p_noise = noise_data[:,1] - p_nsum = p_noise.sum() + print('scaling [nm/pixel] :', Np) + print('center (width, height) :', w0, h0) - # conversion : photon --> photoelectron --> ADC count - for i in range(Nw_pixel) : - for j in range(Nh_pixel) : + # set true-dataset in pixel-scale + true_data[:,4] = true_data[:,4]/Np - w_cel0 + w_cam0 - (w0 - (Nw_pixel-1)/2.0) + true_data[:,5] = true_data[:,5]/Np - h_cel0 + h_cam0 - (h0 - (Nh_pixel-1)/2.0) - # pixel position - pixel = (i, j) + # set seed for random number + numpy.random.seed() - # Detector : Quantum Efficiency - #index = int(self.configs.psf_wavelength) - int(self.configs.wave_length[0]) - #QE = self.configs.detector_qeff[index] - QE = self.configs.detector_qeff + # CMOS (readout noise probability ditributions) + if (self.configs.detector_type == "CMOS") : + noise_data = numpy.loadtxt(os.path.join(os.path.abspath(os.path.dirname(__file__)), + 'catalog/detector/RNDist_F40.csv'), delimiter=',') + Nr_cmos = noise_data[:,0] + p_noise = noise_data[:,1] + p_nsum = p_noise.sum() - # get signal (photons) - Photons = camera_pixel[i][j][0] + # conversion : photon --> photoelectron --> ADC count + for i in range(Nw_pixel) : + for j in range(Nh_pixel) : - # get constant background (photoelectrons) - Photons_bg = self.effects.background_mean - Photons += Photons_bg + # pixel position + pixel = (i, j) - # get signal (expectation) - Exp = QE*Photons + # Detector : Quantum Efficiency + #index = int(self.configs.psf_wavelength) - int(self.configs.wave_length[0]) + #QE = self.configs.detector_qeff[index] + QE = self.configs.detector_qeff - # select Camera type - if (self.configs.detector_type == "CMOS") : + # get signal (photons) + Photons = camera_pixel[i][j][0] - # get signal (poisson distributions) - signal = numpy.random.poisson(Exp, None) + # get constant background (photoelectrons) + Photons_bg = self.effects.background_mean + Photons += Photons_bg - # get detector noise (photoelectrons) - noise = numpy.random.choice(Nr_cmos, None, p=p_noise/p_nsum) - Nr = 1.3 + # get signal (expectation) + Exp = QE*Photons + # select Camera type + if (self.configs.detector_type == "CMOS") : - elif (self.configs.detector_type == "EMCCD") : + # get signal (poisson distributions) + signal = numpy.random.poisson(Exp, None) - # get signal (photoelectrons) - if (Exp > 0) : + # get detector noise (photoelectrons) + noise = numpy.random.choice(Nr_cmos, None, p=p_noise/p_nsum) + Nr = 1.3 - # get EM gain - M = self.configs.detector_emgain - # set probability distributions - s_min = M*int(Exp - 5.0*numpy.sqrt(Exp) - 10) - s_max = M*int(Exp + 5.0*numpy.sqrt(Exp) + 10) + elif (self.configs.detector_type == "EMCCD") : - if (s_min < 0) : s_min = 0 + # get signal (photoelectrons) + if (Exp > 0) : - s = numpy.array([k for k in range(s_min, s_max)]) - p_signal = self.prob_EMCCD(s, Exp) - p_ssum = p_signal.sum() + # get EM gain + M = self.configs.detector_emgain - # get signal (photoelectrons) - signal = numpy.random.choice(s, None, p=p_signal/p_ssum) + # set probability distributions + s_min = M*int(Exp - 5.0*numpy.sqrt(Exp) - 10) + s_max = M*int(Exp + 5.0*numpy.sqrt(Exp) + 10) - else : - signal = 0 + if (s_min < 0) : s_min = 0 - # get detector noise (photoelectrons) - Nr = self.configs.detector_readout_noise + s = numpy.array([k for k in range(s_min, s_max)]) + p_signal = self.prob_EMCCD(s, Exp) + p_ssum = p_signal.sum() - if (Nr > 0) : - noise = numpy.random.normal(0, Nr, None) - else : noise = 0 + # get signal (photoelectrons) + signal = numpy.random.choice(s, None, p=p_signal/p_ssum) + else : + signal = 0 - elif (self.configs.detector_type == "CCD") : + # get detector noise (photoelectrons) + Nr = self.configs.detector_readout_noise - # get signal (poisson distributions) - signal = numpy.random.poisson(Exp, None) + if (Nr > 0) : + noise = numpy.random.normal(0, Nr, None) + else : noise = 0 - # get detector noise (photoelectrons) - Nr = self.configs.detector_readout_noise - if (Nr > 0) : - noise = numpy.random.normal(0, Nr, None) - else : noise = 0 + elif (self.configs.detector_type == "CCD") : - # A/D converter : Photoelectrons --> ADC counts - PE = signal + noise - ADC = self.get_ADC_value(pixel, PE) + # get signal (poisson distributions) + signal = numpy.random.poisson(Exp, None) - # set data in image array - camera_pixel[i][j] = [Exp, ADC] + # get detector noise (photoelectrons) + Nr = self.configs.detector_readout_noise - - return camera_pixel, true_data + if (Nr > 0) : + noise = numpy.random.normal(0, Nr, None) + else : noise = 0 + # A/D converter : Photoelectrons --> ADC counts + PE = signal + noise + ADC = self.get_ADC_value(pixel, PE) + # set data in image array + camera_pixel[i][j] = [Exp, ADC] - def get_ADC_value(self, pixel, photoelectron) : - # pixel position - i, j = pixel + return camera_pixel, true_data - # check non-linearity - fullwell = self.configs.ADConverter_fullwell - if (photoelectron > fullwell) : - photoelectron = fullwell - # convert photoelectron to ADC counts - k = self.configs.ADConverter_gain[i][j] - ADC0 = self.configs.ADConverter_offset[i][j] - ADC_max = 2**self.configs.ADConverter_bit - 1 + def get_ADC_value(self, pixel, photoelectron) : - ADC = photoelectron/k + ADC0 + # pixel position + i, j = pixel - if (ADC > ADC_max): ADC = ADC_max - if (ADC < 0): ADC = 0 + # check non-linearity + fullwell = self.configs.ADConverter_fullwell - #return int(ADC) - return ADC + if (photoelectron > fullwell) : + photoelectron = fullwell + # convert photoelectron to ADC counts + k = self.configs.ADConverter_gain[i][j] + ADC0 = self.configs.ADConverter_offset[i][j] + ADC_max = 2**self.configs.ADConverter_bit - 1 + ADC = photoelectron/k + ADC0 - def use_multiprocess(self) : + if (ADC > ADC_max): ADC = ADC_max + if (ADC < 0): ADC = 0 - envname = 'ECELL_MICROSCOPE_SINGLE_PROCESS' - return not (envname in os.environ and os.environ[envname]) + #return int(ADC) + return ADC - def set_fluo_psf(self) : + def use_multiprocess(self) : - depths = set() + envname = 'ECELL_MICROSCOPE_SINGLE_PROCESS' + return not (envname in os.environ and os.environ[envname]) - # get cell size - Nx, Ny, Nz = self.get_cell_size() - count_array = numpy.array(self.configs.shutter_count_array) - exposure_time = self.configs.detector_exposure_time - data_interval = self.configs.spatiocyte_interval + def set_fluo_psf(self) : - delta_count = int(round(exposure_time/data_interval)) + depths = set() - for count in range(0, len(count_array), delta_count) : + # get cell size + Nx, Ny, Nz = self.get_cell_size() - # focal point - f_0 = self.configs.detector_focal_point - p_0 = numpy.array([Nx, Ny, Nz])*f_0 - x_0, y_0, z_0 = p_0 + count_array = numpy.array(self.configs.shutter_count_array) - frame_data = self.configs.spatiocyte_data[count:count+delta_count] + exposure_time = self.configs.detector_exposure_time + data_interval = self.configs.spatiocyte_interval - for _, data in frame_data : - for data_j in data : + delta_count = int(round(exposure_time/data_interval)) - p_i = numpy.array(data_j[0])/1e-9 + for count in range(0, len(count_array), delta_count) : - # Snell's law - #amplitude, penet_depth = self.snells_law(p_i, p_0) + # focal point + f_0 = self.configs.detector_focal_point + p_0 = numpy.array([Nx, Ny, Nz])*f_0 + x_0, y_0, z_0 = p_0 - # Particle coordinte in real(nm) scale - p_i, radial, depth = self.get_coordinate(p_i, p_0) + frame_data = self.configs.spatiocyte_data[count:count+delta_count] - # fluorophore axial position - fluo_depth = depth if depth < len(self.configs.depth) else -1 + for _, data in frame_data : + for data_j in data : - depths.add(int(fluo_depth)) + p_i = numpy.array(data_j[0])/1e-9 - depths = list(depths) + # Snell's law + #amplitude, penet_depth = self.snells_law(p_i, p_0) - if (len(depths) > 0) : - if self.use_multiprocess(): - - self.fluo_psf = {} - num_processes = min(multiprocessing.cpu_count(), len(depths)) - - n, m = divmod(len(depths), num_processes) - - chunks = [n + 1 if i < m else n for i in range(num_processes)] - - processes = [] - start_index = 0 - - for chunk in chunks: - stop_index = start_index + chunk - p, c = multiprocessing.Pipe() - process = multiprocessing.Process( - target=self.get_fluo_psf_process, - args=(depths[start_index:stop_index], c)) - processes.append((p, process)) - start_index = stop_index - - for _, process in processes: - process.start() - - for pipe, process in processes: - self.fluo_psf.update(pipe.recv()) - process.join() - - else : - self.fluo_psf = self.get_fluo_psf(depths) + # Particle coordinte in real(nm) scale + p_i, radial, depth = self.get_coordinate(p_i, p_0) + + # fluorophore axial position + fluo_depth = depth if depth < len(self.configs.depth) else -1 + depths.add(int(fluo_depth)) + depths = list(depths) - def get_fluo_psf(self, depths) : + if (len(depths) > 0) : + if self.use_multiprocess(): + + self.fluo_psf = {} + num_processes = min(multiprocessing.cpu_count(), len(depths)) + + n, m = divmod(len(depths), num_processes) + + chunks = [n + 1 if i < m else n for i in range(num_processes)] + + processes = [] + start_index = 0 + + for chunk in chunks: + stop_index = start_index + chunk + p, c = multiprocessing.Pipe() + process = multiprocessing.Process( + target=self.get_fluo_psf_process, + args=(depths[start_index:stop_index], c)) + processes.append((p, process)) + start_index = stop_index + + for _, process in processes: + process.start() + + for pipe, process in processes: + self.fluo_psf.update(pipe.recv()) + process.join() + + else : + self.fluo_psf = self.get_fluo_psf(depths) - r = self.configs.radial - theta = numpy.linspace(0, 90, 91) - z = numpy.linspace(0, +r[-1], len(r)) - y = numpy.linspace(0, +r[-1], len(r)) + def get_fluo_psf(self, depths) : - coordinates = self.polar2cartesian_coordinates(r, theta, z, y) - psf_t = numpy.ones_like(theta) - result = {} + r = self.configs.radial - for depth in depths: - psf_r = self.configs.fluorophore_psf[depth] - psf_polar = numpy.array(map(lambda x: psf_t * x, psf_r)) - result[depth] = self.polar2cartesian(psf_polar, coordinates, (len(r), len(r))) + theta = numpy.linspace(0, 90, 91) - return result + z = numpy.linspace(0, +r[-1], len(r)) + y = numpy.linspace(0, +r[-1], len(r)) + coordinates = self.polar2cartesian_coordinates(r, theta, z, y) + psf_t = numpy.ones_like(theta) + result = {} + for depth in depths: + psf_r = self.configs.fluorophore_psf[depth] + psf_polar = numpy.array(map(lambda x: psf_t * x, psf_r)) + result[depth] = self.polar2cartesian(psf_polar, coordinates, (len(r), len(r))) - def get_fluo_psf_process(self, depths, pipe) : + return result - pipe.send(self.get_fluo_psf(depths)) + def get_fluo_psf_process(self, depths, pipe) : + pipe.send(self.get_fluo_psf(depths)) diff --git a/bioimaging/epifm_rewrite_script.py b/bioimaging/epifm_rewrite_script.py index 7e8f2c8..b30675b 100755 --- a/bioimaging/epifm_rewrite_script.py +++ b/bioimaging/epifm_rewrite_script.py @@ -7,51 +7,49 @@ def test_epifm(t0, t1) : - # create EPIFM imaging - epifm = EPIFMConfigs() - epifm.set_LightSource(source_type='LASER', wave_length=561, flux_density=20, angle=0) - epifm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') - epifm.set_DichroicMirror('FF562-Di03-25x36') - epifm.set_EmissionFilter('FF01-593_40-25') - epifm.set_Magnification(Mag=250) - -# # Detector : CMOS Camera -# epifm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ -# exposure_time=0.5, QE=0.73) -# epifm.set_ADConverter(bit=16, offset=100, fullwell=30000) - - # Detector : EMCCD Camera - epifm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, exposure_time=0.1, \ - focal_point=(0.0,0.65,0.5), QE=0.92, readout_noise=100, emgain=300) - epifm.set_ADConverter(bit=16, offset=2000, fullwell=800000) - -# # Output data -# epifm.set_OutputData(image_file_dir='./numpys_epifm_01000A') - - # Input data - csv_dir = '/home/masaki/wrk/spatiocyte/models/Hiroshima/data_HRG-ErbB/SM/csv/TEST' - epifm.reset_InputData(csv_dir, start=t0, end=t1, observable="r1") - epifm.set_ShapeData(csv_dir) - - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0) - physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) - physics.set_photobleaching(tau0=1.8, alpha=0.73) - #physics.set_photoblinking(t0_on=1.00, a_on=0.58, t0_off=10e-6, a_off=0.48) - - # create image - create = EPIFMVisualizer(configs=epifm, effects=physics) - create.rewrite_InputData(output_file_dir='./data_epifm_TEST_00deg') - #create.output_frames(num_div=16) + # create EPIFM imaging + epifm = EPIFMConfigs() + epifm.set_LightSource(source_type='LASER', wave_length=561, flux_density=20, angle=0) + epifm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') + epifm.set_DichroicMirror('FF562-Di03-25x36') + epifm.set_EmissionFilter('FF01-593_40-25') + epifm.set_Magnification(Mag=250) + +# # Detector : CMOS Camera +# epifm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ +# exposure_time=0.5, QE=0.73) +# epifm.set_ADConverter(bit=16, offset=100, fullwell=30000) + + # Detector : EMCCD Camera + epifm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, exposure_time=0.1, \ + focal_point=(0.0,0.65,0.5), QE=0.92, readout_noise=100, emgain=300) + epifm.set_ADConverter(bit=16, offset=2000, fullwell=800000) + +# # Output data +# epifm.set_OutputData(image_file_dir='./numpys_epifm_01000A') + + # Input data + csv_dir = '/home/masaki/wrk/spatiocyte/models/Hiroshima/data_HRG-ErbB/SM/csv/TEST' + epifm.reset_InputData(csv_dir, start=t0, end=t1, observable="r1") + epifm.set_ShapeData(csv_dir) + + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0) + physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) + physics.set_photobleaching(tau0=1.8, alpha=0.73) + #physics.set_photoblinking(t0_on=1.00, a_on=0.58, t0_off=10e-6, a_off=0.48) + + # create image + create = EPIFMVisualizer(configs=epifm, effects=physics) + create.rewrite_InputData(output_file_dir='./data_epifm_TEST_00deg') + #create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_epifm(t0, t1) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_epifm(t0, t1) diff --git a/bioimaging/epifm_script.py b/bioimaging/epifm_script.py index ea10ff2..5cde283 100755 --- a/bioimaging/epifm_script.py +++ b/bioimaging/epifm_script.py @@ -7,55 +7,53 @@ def test_epifm(t0, t1, angle=0) : - # create EPIFM imaging - epifm = EPIFMConfigs() - epifm.set_LightSource(source_type='LASER', wave_length=561, flux_density=20, angle=angle) - epifm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') - epifm.set_DichroicMirror('FF562-Di03-25x36') - epifm.set_EmissionFilter('FF01-593_40-25') - epifm.set_Magnification(Mag=250) - -# # Detector : CMOS Camera -# epifm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ -# exposure_time=0.5, QE=0.73) -# epifm.set_ADConverter(bit=16, offset=100, fullwell=30000) - - # Detector : EMCCD Camera - epifm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, exposure_time=0.1, \ - focal_point=(0.8355,0.5, 0.5), QE=0.92, readout_noise=100, emgain=300) - epifm.set_ADConverter(bit=16, offset=2000, fullwell=800000) - - # Output data - epifm.set_OutputData(image_file_dir='./numpys_epifm_TEST') - #epifm.set_OutputData(image_file_dir='./numpys_epifm_TEST_%02ddeg' % (angle)) - #epifm.set_OutputData(image_file_dir='./numpys_epifm_TEST_08deg_bleach') - - # Input data - csv_dir = '/home/masaki/wrk/spatiocyte/models/Hiroshima/data_HRG-ErbB/SM/csv/TEST' - #csv_dir = '/home/masaki/bioimaging_4public/data_epifm_TEST_08deg_bleach' - epifm.set_InputData(csv_dir, start=t0, end=t1, observable="r1") - epifm.set_ShapeData(csv_dir) - - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0) - physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) - #physics.set_photobleaching(tau0=1.8, alpha=0.73) - #physics.set_photoblinking(t0_on=1.00, a_on=0.58, t0_off=10e-6, a_off=0.48) - - # create image - create = EPIFMVisualizer(configs=epifm, effects=physics) - #create.rewrite_InputData(output_file_dir='./data_epifm_01000A') - create.output_frames(num_div=16) + # create EPIFM imaging + epifm = EPIFMConfigs() + epifm.set_LightSource(source_type='LASER', wave_length=561, flux_density=20, angle=angle) + epifm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') + epifm.set_DichroicMirror('FF562-Di03-25x36') + epifm.set_EmissionFilter('FF01-593_40-25') + epifm.set_Magnification(Mag=250) + +# # Detector : CMOS Camera +# epifm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ +# exposure_time=0.5, QE=0.73) +# epifm.set_ADConverter(bit=16, offset=100, fullwell=30000) + + # Detector : EMCCD Camera + epifm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, exposure_time=0.1, \ + focal_point=(0.8355,0.5, 0.5), QE=0.92, readout_noise=100, emgain=300) + epifm.set_ADConverter(bit=16, offset=2000, fullwell=800000) + + # Output data + epifm.set_OutputData(image_file_dir='./numpys_epifm_TEST') + #epifm.set_OutputData(image_file_dir='./numpys_epifm_TEST_%02ddeg' % (angle)) + #epifm.set_OutputData(image_file_dir='./numpys_epifm_TEST_08deg_bleach') + + # Input data + csv_dir = '/home/masaki/wrk/spatiocyte/models/Hiroshima/data_HRG-ErbB/SM/csv/TEST' + #csv_dir = '/home/masaki/bioimaging_4public/data_epifm_TEST_08deg_bleach' + epifm.set_InputData(csv_dir, start=t0, end=t1, observable="r1") + epifm.set_ShapeData(csv_dir) + + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0) + physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) + #physics.set_photobleaching(tau0=1.8, alpha=0.73) + #physics.set_photoblinking(t0_on=1.00, a_on=0.58, t0_off=10e-6, a_off=0.48) + + # create image + create = EPIFMVisualizer(configs=epifm, effects=physics) + #create.rewrite_InputData(output_file_dir='./data_epifm_01000A') + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - angle = float(sys.argv[3]) - - test_epifm(t0, t1, angle) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + angle = float(sys.argv[3]) + test_epifm(t0, t1, angle) diff --git a/bioimaging/erk102_lscm_script.py b/bioimaging/erk102_lscm_script.py index f33b457..e22751d 100755 --- a/bioimaging/erk102_lscm_script.py +++ b/bioimaging/erk102_lscm_script.py @@ -7,75 +7,74 @@ def confm_pulses(t0, t1) : - # create Point-scanning Confocal Microscopy - confm = PointScanConfocalConfigs() + # create Point-scanning Confocal Microscopy + confm = PointScanConfocalConfigs() - confm.set_LightSource(source_type='LASER', wave_length=488, flux=100e-6, radius=200e-9) - confm.set_Fluorophore(fluorophore_type='EGFP') - confm.set_Pinhole(radius=28.8e-6) - confm.set_Magnification(Mag=60) + confm.set_LightSource(source_type='LASER', wave_length=488, flux=100e-6, radius=200e-9) + confm.set_Fluorophore(fluorophore_type='EGFP') + confm.set_Pinhole(radius=28.8e-6) + confm.set_Magnification(Mag=60) - # PMT : Analog mode - confm.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.3,0.5,0.5), \ - pixel_length=207.16e-9, scan_time=1.15, QE=0.3, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) - confm.set_ADConverter(bit=12, offset=0, fullwell=4096) - confm.set_OutputData(image_file_dir='./images_erk102') + # PMT : Analog mode + confm.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.3,0.5,0.5), \ + pixel_length=207.16e-9, scan_time=1.15, QE=0.3, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) + confm.set_ADConverter(bit=12, offset=0, fullwell=4096) + confm.set_OutputData(image_file_dir='./images_erk102') - # Input data : EGF model file - confm.set_InputData('./data/erk102', start=t0, end=t1, observable="ERK") + # Input data : EGF model file + confm.set_InputData('./data/erk102', start=t0, end=t1, observable="ERK") - # create physical effects - physics = PhysicalEffects() - physics.set_Conversion(ratio=1e-6) - #physics.set_Background(mean=5) - #physics.set_DetectorCrosstalk(width=1.00) + # create physical effects + physics = PhysicalEffects() + physics.set_Conversion(ratio=1e-6) + #physics.set_Background(mean=5) + #physics.set_DetectorCrosstalk(width=1.00) - # create image and movie - create = PointScanConfocalVisualizer(configs=confm, effects=physics) - create.output_frames(num_div=16) + # create image and movie + create = PointScanConfocalVisualizer(configs=confm, effects=physics) + create.output_frames(num_div=16) def confm_analog(t0, t1) : - # create Point-scanning Confocal Microscopy - confm = PointScanConfocalConfigs() + # create Point-scanning Confocal Microscopy + confm = PointScanConfocalConfigs() - confm.set_LightSource(source_type='LASER', wave_length=488, flux=100e-6, radius=200e-9) - confm.set_Fluorophore(fluorophore_type='EGFP') - confm.set_Pinhole(radius=28.8e-6) - confm.set_Magnification(Mag=60) + confm.set_LightSource(source_type='LASER', wave_length=488, flux=100e-6, radius=200e-9) + confm.set_Fluorophore(fluorophore_type='EGFP') + confm.set_Pinhole(radius=28.8e-6) + confm.set_Magnification(Mag=60) - # PMT : Anlalog mode - confm.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.3,0.5,0.5), - pixel_length=207.16e-9, scan_time=1.15, QE=0.3, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) - confm.set_ADConverter(bit=12, offset=0, fullwell=4096) - confm.set_OutputData(image_file_dir='./images_erk102_mode_analog_100uW') + # PMT : Anlalog mode + confm.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.3,0.5,0.5), + pixel_length=207.16e-9, scan_time=1.15, QE=0.3, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) + confm.set_ADConverter(bit=12, offset=0, fullwell=4096) + confm.set_OutputData(image_file_dir='./images_erk102_mode_analog_100uW') - # Input data : EGF model file - confm.set_InputData('/home/masaki/ecell3/latest/data/csv/erk102', start=t0, end=t1, observable="ERK") + # Input data : EGF model file + confm.set_InputData('/home/masaki/ecell3/latest/data/csv/erk102', start=t0, end=t1, observable="ERK") - # create physical effects - physics = PhysicalEffects() - physics.set_Conversion(ratio=1e-6) - #physics.set_Background(mean=5) - #physics.set_DetectorCrosstalk(width=1.00) + # create physical effects + physics = PhysicalEffects() + physics.set_Conversion(ratio=1e-6) + #physics.set_Background(mean=5) + #physics.set_DetectorCrosstalk(width=1.00) - # create image and movie - create = PointScanConfocalVisualizer(configs=confm, effects=physics) - create.output_frames(num_div=16) + # create image and movie + create = PointScanConfocalVisualizer(configs=confm, effects=physics) + create.output_frames(num_div=16) if __name__ == "__main__": - index = sys.argv[1] - t0 = float(sys.argv[2]) - t1 = float(sys.argv[3]) + index = sys.argv[1] + t0 = float(sys.argv[2]) + t1 = float(sys.argv[3]) - if (index == "Photon-counting") : - confm_pulses(t0, t1) - - if (index == "Analog") : - confm_analog(t0, t1) + if (index == "Photon-counting") : + confm_pulses(t0, t1) + if (index == "Analog") : + confm_analog(t0, t1) diff --git a/bioimaging/fcs_handler.py b/bioimaging/fcs_handler.py index e96ee2d..aefce68 100755 --- a/bioimaging/fcs_handler.py +++ b/bioimaging/fcs_handler.py @@ -27,13 +27,13 @@ class FCSConfigs(EPIFMConfigs) : ''' Point-scanning Confocal configuration - Point-like Gaussian Profile - + - Point-scanning - + - Pinhole - + - Detector : PMT + Point-like Gaussian Profile + + + Point-scanning + + + Pinhole + + + Detector : PMT ''' def __init__(self, user_configs_dict = None): @@ -71,17 +71,17 @@ def set_Pinhole(self, radius = None) : def set_Detector(self, detector = None, - mode = None, + mode = None, pixel_length = None, focal_point = None, base_position = None, exposure_time = None, - QE = None, - readout_noise = None, - dark_count = None, - gain = None, - dyn_stages = None, - pair_pulses = None + QE = None, + readout_noise = None, + dark_count = None, + gain = None, + dyn_stages = None, + pair_pulses = None ): self._set_data('detector_switch', True) @@ -98,13 +98,13 @@ def set_Detector(self, detector = None, self._set_data('detector_dyn_stages', dyn_stages) self._set_data('detector_pair_pulses', pair_pulses) - print('--- Detector : ', self.detector_type, ' (', self.detector_mode, 'mode )') + print('--- Detector : ', self.detector_type, ' (', self.detector_mode, 'mode )') print('\tPixel Size = ', self.detector_pixel_length, 'm/pixel') print('\tFocal Point = ', self.detector_focal_point) print('\tPosition = ', self.detector_base_position) print('\tExposure Time = ', self.detector_exposure_time, 'sec/image') print('\tQuantum Efficiency = ', 100*self.detector_qeff, '%') - print('\tReadout Noise = ', self.detector_readout_noise, 'electron') + print('\tReadout Noise = ', self.detector_readout_noise, 'electron') print('\tDark Count = ', self.detector_dark_count, 'electron/sec') print('\tGain = ', 'x', self.detector_gain) print('\tDynode = ', self.detector_dyn_stages, 'stages') @@ -121,26 +121,26 @@ def set_Illumination_path(self) : # (plank const) * (speed of light) [joules meter] hc = 2.00e-25 - # Illumination + # Illumination w_0 = self.source_radius - # flux [joules/sec=watt] + # flux [joules/sec=watt] P_0 = self.source_flux # single photon energy [joules] wave_length = self.source_wavelength*1e-9 E_wl = hc/wave_length - # photon flux [photons/sec] + # photon flux [photons/sec] N_0 = P_0/E_wl # Beam width [m] w_z = w_0*numpy.sqrt(1 + ((wave_length*d*1e-9)/(numpy.pi*w_0**2))**2) - # photon flux density [photon/(sec m^2)] + # photon flux density [photon/(sec m^2)] self.source_flux_density = numpy.array(map(lambda x : 2*N_0/(numpy.pi*x**2)*numpy.exp(-2*(r*1e-9/x)**2), w_z)) - print('Photon Flux Density (Max) :', numpy.amax(self.source_flux_density)) + print('Photon Flux Density (Max) :', numpy.amax(self.source_flux_density)) @@ -148,23 +148,23 @@ def set_Detection_path(self) : wave_length = self.psf_wavelength*1e-9 - # Magnification - Mag = self.image_magnification + # Magnification + Mag = self.image_magnification - # set voxel radius + # set voxel radius voxel_radius = self.spatiocyte_VoxelRadius - # set pixel length - #pixel_length = (2.0*self.pinhole_radius)/Mag - pixel_length = self.detector_pixel_length + # set pixel length + #pixel_length = (2.0*self.pinhole_radius)/Mag + pixel_length = self.detector_pixel_length - # set image scaling factor - self.image_resolution = pixel_length - self.image_scaling = pixel_length/(2.0*voxel_radius) + # set image scaling factor + self.image_resolution = pixel_length + self.image_scaling = pixel_length/(2.0*voxel_radius) - print('Magnification : x %d' % (Mag)) - print('Resolution :', self.image_resolution, 'm/pixel') - print('Scaling :', self.image_scaling) + print('Magnification : x %d' % (Mag)) + print('Resolution :', self.image_resolution, 'm/pixel') + print('Scaling :', self.image_scaling) # Detector PSF self.set_PSF_detector() @@ -174,302 +174,302 @@ def set_Detection_path(self) : class FCSVisualizer(EPIFMVisualizer) : - ''' - Confocal Visualization class of e-cell simulator - ''' + ''' + Confocal Visualization class of e-cell simulator + ''' - def __init__(self, configs=FCSConfigs(), effects=PhysicalEffects()) : + def __init__(self, configs=FCSConfigs(), effects=PhysicalEffects()) : - assert isinstance(configs, FCSConfigs) - self.configs = configs + assert isinstance(configs, FCSConfigs) + self.configs = configs - assert isinstance(effects, PhysicalEffects) - self.effects = effects + assert isinstance(effects, PhysicalEffects) + self.effects = effects - """ - Check and create the folder for image file. - """ - if not os.path.exists(self.configs.image_file_dir): - os.makedirs(self.configs.image_file_dir) - #else: - # for file in os.listdir(self.configs.movie_image_file_dir): - # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) + """ + Check and create the folder for image file. + """ + if not os.path.exists(self.configs.image_file_dir): + os.makedirs(self.configs.image_file_dir) + #else: + # for file in os.listdir(self.configs.movie_image_file_dir): + # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) - """ - Optical Path - """ - self.configs.set_Optical_path() + """ + Optical Path + """ + self.configs.set_Optical_path() - def get_signal(self, time, pid, s_index, p_i, p_b, p_0, norm) : + def get_signal(self, time, pid, s_index, p_i, p_b, p_0, norm) : - # set focal point - x_0, y_0, z_0 = p_0 + # set focal point + x_0, y_0, z_0 = p_0 - # set source center - x_b, y_b, z_b = p_b + # set source center + x_b, y_b, z_b = p_b - # set particle position - x_i, y_i, z_i = p_i + # set particle position + x_i, y_i, z_i = p_i - # - r = self.configs.radial - d = self.configs.depth + # + r = self.configs.radial + d = self.configs.depth - # beam axial position - d_s = abs(x_i - x_b) + # beam axial position + d_s = abs(x_i - x_b) - if (d_s < 20000) : - source_depth = d_s - else : - source_depth = 19999 + if (d_s < 20000) : + source_depth = d_s + else : + source_depth = 19999 - # beam horizontal position (y-direction) - hh = numpy.sqrt((y_i-y_b)**2) + # beam horizontal position (y-direction) + hh = numpy.sqrt((y_i-y_b)**2) - if (hh < len(r)) : - source_horizon = hh - else : - source_horizon = r[-1] + if (hh < len(r)) : + source_horizon = hh + else : + source_horizon = r[-1] - # get illumination PSF - source_psf = self.configs.source_flux_density[int(source_depth)][int(source_horizon)] - #source_max = norm*self.configs.source_flux_density[0][0] + # get illumination PSF + source_psf = self.configs.source_flux_density[int(source_depth)][int(source_horizon)] + #source_max = norm*self.configs.source_flux_density[0][0] - # signal conversion : - #Intensity = self.get_intensity(time, pid, source_psf, source_max) - Ratio = self.effects.conversion_ratio + # signal conversion : + #Intensity = self.get_intensity(time, pid, source_psf, source_max) + Ratio = self.effects.conversion_ratio - # fluorophore axial position - d_f = abs(x_i - x_b) + # fluorophore axial position + d_f = abs(x_i - x_b) - if (d_f < len(d)) : - fluo_depth = d_f - else : - fluo_depth = d[-1] + if (d_f < len(d)) : + fluo_depth = d_f + else : + fluo_depth = d[-1] - # get fluorophore PSF - fluo_psf = self.fluo_psf[int(fluo_depth)] + # get fluorophore PSF + fluo_psf = self.fluo_psf[int(fluo_depth)] - # signal conversion : Output PSF = PSF(source) * Ratio * PSF(Fluorophore) - signal = norm * source_psf * Ratio * fluo_psf + # signal conversion : Output PSF = PSF(source) * Ratio * PSF(Fluorophore) + signal = norm * source_psf * Ratio * fluo_psf - return signal + return signal - def get_molecule_plane(self, cell, time, data, pid, p_b, p_0) : + def get_molecule_plane(self, cell, time, data, pid, p_b, p_0) : - #voxel_size = (2.0*self.configs.spatiocyte_VoxelRadius)/1e-9 + #voxel_size = (2.0*self.configs.spatiocyte_VoxelRadius)/1e-9 - # get beam position - x_b, y_b, z_b = p_b + # get beam position + x_b, y_b, z_b = p_b - # cutoff randius - Mag = self.configs.image_magnification - pinhole_radius = int(self.configs.pinhole_radius/Mag/1e-9) - cut_off = int(1.5*pinhole_radius) + # cutoff randius + Mag = self.configs.image_magnification + pinhole_radius = int(self.configs.pinhole_radius/Mag/1e-9) + cut_off = int(1.5*pinhole_radius) - # particles coordinate, species and lattice IDs - c_id, s_id, l_id = data + # particles coordinate, species and lattice IDs + c_id, s_id, l_id = data - sid_array = numpy.array(self.configs.spatiocyte_species_id) - s_index = (numpy.abs(sid_array - int(s_id))).argmin() + sid_array = numpy.array(self.configs.spatiocyte_species_id) + s_index = (numpy.abs(sid_array - int(s_id))).argmin() - if self.configs.spatiocyte_observables[s_index] is True : + if self.configs.spatiocyte_observables[s_index] is True : - # Normalization - unit_time = 1.0 - unit_area = (1e-9)**2 - norm = (unit_area*unit_time)/(4.0*numpy.pi) + # Normalization + unit_time = 1.0 + unit_area = (1e-9)**2 + norm = (unit_area*unit_time)/(4.0*numpy.pi) - # particles coordinate in nm-scale - p_i = self.get_coordinate(c_id) - #p_i = p_0 - x_i, y_i, z_i = p_i + # particles coordinate in nm-scale + p_i = self.get_coordinate(c_id) + #p_i = p_0 + x_i, y_i, z_i = p_i - if (numpy.sqrt((y_i - y_b)**2 + (z_i - z_b)**2) < cut_off) : + if (numpy.sqrt((y_i - y_b)**2 + (z_i - z_b)**2) < cut_off) : - #print pid, s_id, p_i - # get signal matrix - signal = self.get_signal(time, pid, s_index, p_i, p_b, p_0, norm) + #print pid, s_id, p_i + # get signal matrix + signal = self.get_signal(time, pid, s_index, p_i, p_b, p_0, norm) - # add signal matrix to image plane - self.overwrite_signal(cell, signal, p_i, p_b) + # add signal matrix to image plane + self.overwrite_signal(cell, signal, p_i, p_b) - def overwrite_signal(self, cell, signal, p_i, p_b) : + def overwrite_signal(self, cell, signal, p_i, p_b) : - # particle position - x_i, y_i, z_i = p_i + # particle position + x_i, y_i, z_i = p_i - # beam position - x_b, y_b, z_b = p_b + # beam position + x_b, y_b, z_b = p_b - # z-axis - Nz_cell = len(cell) - Nz_signal = len(signal) + # z-axis + Nz_cell = len(cell) + Nz_signal = len(signal) - Nh_cell = Nz_cell/2 - Nh_signal = (Nz_signal-1)/2 + Nh_cell = Nz_cell/2 + Nh_signal = (Nz_signal-1)/2 - z_to = (z_i + Nh_signal) - (z_b - Nh_cell) - z_from = (z_i - Nh_signal) - (z_b - Nh_cell) + z_to = (z_i + Nh_signal) - (z_b - Nh_cell) + z_from = (z_i - Nh_signal) - (z_b - Nh_cell) - flag_z = True + flag_z = True - if (z_to > Nz_cell + Nz_signal) : - flag_z = False + if (z_to > Nz_cell + Nz_signal) : + flag_z = False - elif (z_to > Nz_cell and - z_to < Nz_cell + Nz_signal) : + elif (z_to > Nz_cell and + z_to < Nz_cell + Nz_signal) : - dz_to = z_to - Nz_cell + dz_to = z_to - Nz_cell - zi_to = int(Nz_signal - dz_to) - zb_to = int(Nz_cell) + zi_to = int(Nz_signal - dz_to) + zb_to = int(Nz_cell) - elif (z_to > 0 and z_to < Nz_cell) : + elif (z_to > 0 and z_to < Nz_cell) : - zi_to = int(Nz_signal) - zb_to = int(z_to) + zi_to = int(Nz_signal) + zb_to = int(z_to) - else : flag_z = False + else : flag_z = False - if (z_from < 0) : + if (z_from < 0) : - zi_from = int(abs(z_from)) - zb_from = 0 + zi_from = int(abs(z_from)) + zb_from = 0 - else : + else : - zi_from = 0 - zb_from = int(z_from) + zi_from = 0 + zb_from = int(z_from) - if (flag_z == True) : - ddz = (zi_to - zi_from) - (zb_to - zb_from) + if (flag_z == True) : + ddz = (zi_to - zi_from) - (zb_to - zb_from) - if (ddz > 0) : zi_to = zi_to - ddz - if (ddz < 0) : zb_to = zb_to + ddz + if (ddz > 0) : zi_to = zi_to - ddz + if (ddz < 0) : zb_to = zb_to + ddz - # y-axis - Ny_cell = cell.size/Nz_cell - Ny_signal = signal.size/Nz_signal + # y-axis + Ny_cell = cell.size/Nz_cell + Ny_signal = signal.size/Nz_signal - Nh_cell = Ny_cell/2 - Nh_signal = (Ny_signal-1)/2 + Nh_cell = Ny_cell/2 + Nh_signal = (Ny_signal-1)/2 - y_to = (y_i + Nh_signal) - (y_b - Nh_cell) - y_from = (y_i - Nh_signal) - (y_b - Nh_cell) + y_to = (y_i + Nh_signal) - (y_b - Nh_cell) + y_from = (y_i - Nh_signal) - (y_b - Nh_cell) - flag_y = True + flag_y = True - if (y_to > Ny_cell + Ny_signal) : - flag_y = False + if (y_to > Ny_cell + Ny_signal) : + flag_y = False - elif (y_to > Ny_cell and - y_to < Ny_cell + Ny_signal) : + elif (y_to > Ny_cell and + y_to < Ny_cell + Ny_signal) : - dy_to = y_to - Ny_cell + dy_to = y_to - Ny_cell - yi_to = int(Ny_signal - dy_to) - yb_to = int(Ny_cell) + yi_to = int(Ny_signal - dy_to) + yb_to = int(Ny_cell) - elif (y_to > 0 and y_to < Ny_cell) : + elif (y_to > 0 and y_to < Ny_cell) : - yi_to = int(Ny_signal) - yb_to = int(y_to) + yi_to = int(Ny_signal) + yb_to = int(y_to) - else : flag_y = False + else : flag_y = False - if (y_from < 0) : + if (y_from < 0) : - yi_from = int(abs(y_from)) - yb_from = 0 + yi_from = int(abs(y_from)) + yb_from = 0 - else : + else : - yi_from = 0 - yb_from = int(y_from) + yi_from = 0 + yb_from = int(y_from) - if (flag_y == True) : - ddy = (yi_to - yi_from) - (yb_to - yb_from) + if (flag_y == True) : + ddy = (yi_to - yi_from) - (yb_to - yb_from) - if (ddy > 0) : yi_to = yi_to - ddy - if (ddy < 0) : yb_to = yb_to + ddy + if (ddy > 0) : yi_to = yi_to - ddy + if (ddy < 0) : yb_to = yb_to + ddy - if (flag_z == True and flag_y == True) : -# if (abs(ddy) > 2 or abs(ddz) > 2) : -# print zi_from, zi_to, yi_from, yi_to, ddy, ddz -# print zb_from, zb_to, yb_from, yb_to + if (flag_z == True and flag_y == True) : +# if (abs(ddy) > 2 or abs(ddz) > 2) : +# print zi_from, zi_to, yi_from, yi_to, ddy, ddz +# print zb_from, zb_to, yb_from, yb_to - # add to cellular plane - cell[zb_from:zb_to, yb_from:yb_to] += signal[zi_from:zi_to, yi_from:yi_to] + # add to cellular plane + cell[zb_from:zb_to, yb_from:yb_to] += signal[zi_from:zi_to, yi_from:yi_to] - #return cell + #return cell - def output_frames(self, num_div=1): + def output_frames(self, num_div=1): - # set Fluorophores PSF - self.set_fluo_psf() + # set Fluorophores PSF + self.set_fluo_psf() - start = self.configs.spatiocyte_start_time - end = self.configs.spatiocyte_end_time + start = self.configs.spatiocyte_start_time + end = self.configs.spatiocyte_end_time - exposure_time = self.configs.detector_exposure_time - num_timesteps = int(math.ceil((end - start) / exposure_time)) + exposure_time = self.configs.detector_exposure_time + num_timesteps = int(math.ceil((end - start) / exposure_time)) - index0 = int(round(start/exposure_time)) + index0 = int(round(start/exposure_time)) - envname = 'ECELL_MICROSCOPE_SINGLE_PROCESS' + envname = 'ECELL_MICROSCOPE_SINGLE_PROCESS' - if envname in os.environ and os.environ[envname]: - self.output_frames_each_process(index0, num_timesteps) - else: - num_processes = multiprocessing.cpu_count() - n, m = divmod(num_timesteps, num_processes) - # when 10 tasks is distributed to 4 processes, - # number of tasks of each process must be [3, 3, 2, 2] - chunks = [n + 1 if i < m else n for i in range(num_processes)] + if envname in os.environ and os.environ[envname]: + self.output_frames_each_process(index0, num_timesteps) + else: + num_processes = multiprocessing.cpu_count() + n, m = divmod(num_timesteps, num_processes) + # when 10 tasks is distributed to 4 processes, + # number of tasks of each process must be [3, 3, 2, 2] + chunks = [n + 1 if i < m else n for i in range(num_processes)] - processes = [] - start_index = index0 + processes = [] + start_index = index0 - for chunk in chunks: - stop_index = start_index + chunk - process = multiprocessing.Process( - target=self.output_frames_each_process, - args=(start_index, stop_index)) - process.start() - processes.append(process) - start_index = stop_index + for chunk in chunks: + stop_index = start_index + chunk + process = multiprocessing.Process( + target=self.output_frames_each_process, + args=(start_index, stop_index)) + process.start() + processes.append(process) + start_index = stop_index - for process in processes: - process.join() + for process in processes: + process.join() - def output_frames_each_process(self, start_count, stop_count): + def output_frames_each_process(self, start_count, stop_count): - voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 + voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 - # cells dimenssion in nm-scale - Nz = int(self.configs.spatiocyte_lengths[2] * voxel_size) - Ny = int(self.configs.spatiocyte_lengths[1] * voxel_size) - Nx = int(self.configs.spatiocyte_lengths[0] * voxel_size) + # cells dimenssion in nm-scale + Nz = int(self.configs.spatiocyte_lengths[2] * voxel_size) + Ny = int(self.configs.spatiocyte_lengths[1] * voxel_size) + Nx = int(self.configs.spatiocyte_lengths[0] * voxel_size) - # pixel length : nm/pixel - Np = int(self.configs.image_scaling*voxel_size) + # pixel length : nm/pixel + Np = int(self.configs.image_scaling*voxel_size) - # focal point - p_0 = numpy.array([Nx, Ny, Nz])*self.configs.detector_focal_point + # focal point + p_0 = numpy.array([Nx, Ny, Nz])*self.configs.detector_focal_point - # Beam position : - beam_center = numpy.array(self.configs.detector_focal_point) + # Beam position : + beam_center = numpy.array(self.configs.detector_focal_point) # # set boundary condition # if (self.configs.spatiocyte_bc_switch == True) : @@ -477,261 +477,259 @@ def output_frames_each_process(self, start_count, stop_count): # bc = numpy.zeros(shape=(Nz, Ny)) # bc = self.set_boundary_plane(bc, p_b, p_0) - # exposure time - exposure_time = self.configs.detector_exposure_time + # exposure time + exposure_time = self.configs.detector_exposure_time - # spatiocyte start time - start_time = self.configs.spatiocyte_start_time + # spatiocyte start time + start_time = self.configs.spatiocyte_start_time - time = exposure_time * start_count - end = exposure_time * stop_count + time = exposure_time * start_count + end = exposure_time * stop_count - # data-time interval - data_interval = self.configs.spatiocyte_interval + # data-time interval + data_interval = self.configs.spatiocyte_interval - delta_time = int(round(exposure_time / data_interval)) + delta_time = int(round(exposure_time / data_interval)) - # create frame data composed by frame element data - count = start_count - count0 = int(round(start_time / exposure_time)) + # create frame data composed by frame element data + count = start_count + count0 = int(round(start_time / exposure_time)) - # initialize Physical effects - #length0 = len(self.configs.spatiocyte_data[0][1]) - #self.effects.set_states(t0, length0) + # initialize Physical effects + #length0 = len(self.configs.spatiocyte_data[0][1]) + #self.effects.set_states(t0, length0) - while (time < end) : + while (time < end) : - image_file_name = os.path.join(self.configs.image_file_dir, - self.configs.image_file_name_format % (count)) + image_file_name = os.path.join(self.configs.image_file_dir, + self.configs.image_file_name_format % (count)) - print('time : ', time, ' sec (', count, ')') + print('time : ', time, ' sec (', count, ')') - # define cell in nm-scale - #cell = numpy.zeros(shape=(Nz, Ny)) - # define image array in pixel-scale - image_pixel = numpy.zeros([2]) + # define cell in nm-scale + #cell = numpy.zeros(shape=(Nz, Ny)) + # define image array in pixel-scale + image_pixel = numpy.zeros([2]) - count_start = (count - count0)*delta_time - count_end = (count - count0 + 1)*delta_time + count_start = (count - count0)*delta_time + count_end = (count - count0 + 1)*delta_time - frame_data = self.configs.spatiocyte_data[count_start:count_end] + frame_data = self.configs.spatiocyte_data[count_start:count_end] - if (len(frame_data) > 0) : + if (len(frame_data) > 0) : - # Beam position : Fixed - p_b = numpy.array([Nx, Ny, Nz])*beam_center - x_b, y_b, z_b = p_b - - # loop for frame data - i_time, i_data = frame_data[0] + # Beam position : Fixed + p_b = numpy.array([Nx, Ny, Nz])*beam_center + x_b, y_b, z_b = p_b - diff = abs(i_time - time) - data = i_data + # loop for frame data + i_time, i_data = frame_data[0] - for i, (i_time, i_data) in enumerate(frame_data) : - #print '\t', '%02d-th frame : ' % (i), i_time, ' sec' - i_diff = abs(i_time - time) + diff = abs(i_time - time) + data = i_data - if (i_diff < diff) : - diff = i_diff - data = i_data + for i, (i_time, i_data) in enumerate(frame_data) : + #print '\t', '%02d-th frame : ' % (i), i_time, ' sec' + i_diff = abs(i_time - time) - # overwrite the scanned region to cell - #r_p = int(self.configs.image_scaling*voxel_size/2) - Mag = self.configs.image_magnification - r_p = int(self.configs.pinhole_radius/Mag/1e-9) + if (i_diff < diff) : + diff = i_diff + data = i_data - if (y_b-r_p < 0) : y_from = int(y_b) - else : y_from = int(y_b - r_p) + # overwrite the scanned region to cell + #r_p = int(self.configs.image_scaling*voxel_size/2) + Mag = self.configs.image_magnification + r_p = int(self.configs.pinhole_radius/Mag/1e-9) - if (y_b+r_p >= Ny) : y_to = int(y_b) - else : y_to = int(y_b + r_p) + if (y_b-r_p < 0) : y_from = int(y_b) + else : y_from = int(y_b - r_p) - if (z_b-r_p < 0) : z_from = int(z_b) - else : z_from = int(z_b - r_p) + if (y_b+r_p >= Ny) : y_to = int(y_b) + else : y_to = int(y_b + r_p) - if (z_b+r_p >= Nz) : z_to = int(z_b) - else : z_to = int(z_b + r_p) + if (z_b-r_p < 0) : z_from = int(z_b) + else : z_from = int(z_b - r_p) - mask = numpy.zeros(shape=(z_to-z_from, y_to-y_from)) + if (z_b+r_p >= Nz) : z_to = int(z_b) + else : z_to = int(z_b + r_p) - zz, yy = numpy.ogrid[z_from-int(z_b):z_to-int(z_b), y_from-int(y_b):y_to-int(y_b)] - rr_cut = yy**2 + zz**2 < r_p**2 - mask[rr_cut] = 1 + mask = numpy.zeros(shape=(z_to-z_from, y_to-y_from)) - scan_cell = numpy.zeros_like(mask) + zz, yy = numpy.ogrid[z_from-int(z_b):z_to-int(z_b), y_from-int(y_b):y_to-int(y_b)] + rr_cut = yy**2 + zz**2 < r_p**2 + mask[rr_cut] = 1 - # loop for particles - for j, j_data in enumerate(data) : - self.get_molecule_plane(scan_cell, i_time, j_data, j, p_b, p_0) + scan_cell = numpy.zeros_like(mask) - Photon_flux = numpy.sum(mask*scan_cell) + # loop for particles + for j, j_data in enumerate(data) : + self.get_molecule_plane(scan_cell, i_time, j_data, j, p_b, p_0) - # output tuple for particles - image = self.detector_output((time, Photon_flux)) + Photon_flux = numpy.sum(mask*scan_cell) - # save data to numpy-binary file - image_file_name = os.path.join(self.configs.image_file_dir, \ - self.configs.image_file_name_format % (count)) - numpy.save(image_file_name, image) + # output tuple for particles + image = self.detector_output((time, Photon_flux)) + # save data to numpy-binary file + image_file_name = os.path.join(self.configs.image_file_dir, \ + self.configs.image_file_name_format % (count)) + numpy.save(image_file_name, image) - time += exposure_time - count += 1 + time += exposure_time + count += 1 - def prob_analog(self, y, alpha) : - # get average gain - A = self.configs.detector_gain + def prob_analog(self, y, alpha) : - # get dynodes stages - nu = self.configs.detector_dyn_stages + # get average gain + A = self.configs.detector_gain - B = 0.5*(A - 1)/(A**(1.0/nu) - 1) - c = numpy.exp(alpha*(numpy.exp(-A/B) - 1)) - - m_y = alpha*A - m_x = m_y/(1 - c) + # get dynodes stages + nu = self.configs.detector_dyn_stages - s2_y = alpha*(A**2 + 2*A*B) - s2_x = s2_y/(1 - c) - c*m_x**2 + B = 0.5*(A - 1)/(A**(1.0/nu) - 1) + c = numpy.exp(alpha*(numpy.exp(-A/B) - 1)) - #if (y < 10*A) : - # Rayleigh approximation - #s2 = (2.0/numpy.pi)*m_x**2 - #prob = y/s2*numpy.exp(-0.5*y**2/s2) + m_y = alpha*A + m_x = m_y/(1 - c) - # probability distribution - prob = numpy.zeros(shape=(len(y))) + s2_y = alpha*(A**2 + 2*A*B) + s2_x = s2_y/(1 - c) - c*m_x**2 - # get index - k = (numpy.abs(10*A - y)).argmin() + #if (y < 10*A) : + # Rayleigh approximation + #s2 = (2.0/numpy.pi)*m_x**2 + #prob = y/s2*numpy.exp(-0.5*y**2/s2) - # Gamma approximation - k_1 = m_x - k_2 = (m_y**2 + s2_y)/(1 - c) + # probability distribution + prob = numpy.zeros(shape=(len(y))) - a = 1/(k_1*(k_2/k_1**2 - 1)) - b = a*k_1 + # get index + k = (numpy.abs(10*A - y)).argmin() - prob[0:k] = a/gamma(b)*(a*y[0:k])**(b-1)*numpy.exp(-a*y[0:k]) + # Gamma approximation + k_1 = m_x + k_2 = (m_y**2 + s2_y)/(1 - c) - if (k < len(y)) : - # Truncated Gaussian approximation - Q = 0 - beta0 = m_x/numpy.sqrt(s2_x) - beta = beta0 - delta = 0.1*beta0 + a = 1/(k_1*(k_2/k_1**2 - 1)) + b = a*k_1 - while (beta < 11*beta0) : - Q += numpy.exp(-0.5*beta**2)/numpy.sqrt(2*numpy.pi)*delta - beta += delta + prob[0:k] = a/gamma(b)*(a*y[0:k])**(b-1)*numpy.exp(-a*y[0:k]) - prob[k:] = numpy.exp(-0.5*(y[k:] - m_x)**2/s2_x)/(numpy.sqrt(2*numpy.pi*s2_x)*(1 - Q)) + if (k < len(y)) : + # Truncated Gaussian approximation + Q = 0 + beta0 = m_x/numpy.sqrt(s2_x) + beta = beta0 + delta = 0.1*beta0 - return prob + while (beta < 11*beta0) : + Q += numpy.exp(-0.5*beta**2)/numpy.sqrt(2*numpy.pi)*delta + beta += delta + prob[k:] = numpy.exp(-0.5*(y[k:] - m_x)**2/s2_x)/(numpy.sqrt(2*numpy.pi*s2_x)*(1 - Q)) + return prob - def detector_output(self, array) : - # Detector Output - voxel_radius = self.configs.spatiocyte_VoxelRadius - voxel_size = (2.0*voxel_radius)/1e-9 - # set seed for random number - numpy.random.seed() + def detector_output(self, array) : - # observational time - T = self.configs.detector_exposure_time + # Detector Output + voxel_radius = self.configs.spatiocyte_VoxelRadius + voxel_size = (2.0*voxel_radius)/1e-9 - # conversion : photon --> photoelectron --> ADC count - # pixel position - pixel = (0, 0) + # set seed for random number + numpy.random.seed() - # Detector : Quantum Efficiency - #index = int(self.configs.psf_wavelength) - int(self.configs.wave_length[0]) - QE = self.configs.detector_qeff + # observational time + T = self.configs.detector_exposure_time - # get signal (photon flux and photons) - time, Flux = array + # conversion : photon --> photoelectron --> ADC count + # pixel position + pixel = (0, 0) - if (self.configs.detector_mode == "Photon-counting") : - # pair-pulses time resolution (sec) - t_pp = self.configs.detector_pair_pulses - Flux = Flux/(1 + Flux*t_pp) + # Detector : Quantum Efficiency + #index = int(self.configs.psf_wavelength) - int(self.configs.wave_length[0]) + QE = self.configs.detector_qeff - Photons = Flux*T + # get signal (photon flux and photons) + time, Flux = array - # get constant background - if (self.effects.background_switch == True) : + if (self.configs.detector_mode == "Photon-counting") : + # pair-pulses time resolution (sec) + t_pp = self.configs.detector_pair_pulses + Flux = Flux/(1 + Flux*t_pp) - Photons_bg = self.effects.background_mean - Photons += Photons_bg + Photons = Flux*T - # get signal (expectation) - Exp = QE*Photons + # get constant background + if (self.effects.background_switch == True) : - # get dark count - D = self.configs.detector_dark_count - Exp += D*T + Photons_bg = self.effects.background_mean + Photons += Photons_bg - # select Camera type - if (self.configs.detector_mode == "Photon-counting") : + # get signal (expectation) + Exp = QE*Photons - # get signal (poisson distributions) - signal = numpy.random.poisson(Exp, None) + # get dark count + D = self.configs.detector_dark_count + Exp += D*T + # select Camera type + if (self.configs.detector_mode == "Photon-counting") : - if (self.configs.detector_mode == "Analog") : + # get signal (poisson distributions) + signal = numpy.random.poisson(Exp, None) - # get signal (photoelectrons) - if (Exp > 1e-8) : - # get EM gain - G = self.configs.detector_gain + if (self.configs.detector_mode == "Analog") : - # signal array - if (Exp > 1) : sig = numpy.sqrt(Exp) - else : sig = 1 + # get signal (photoelectrons) + if (Exp > 1e-8) : - s_min = int(G*(Exp - 10*sig)) - s_max = int(G*(Exp + 10*sig)) + # get EM gain + G = self.configs.detector_gain - if (s_min < 0) : s_min = 0 + # signal array + if (Exp > 1) : sig = numpy.sqrt(Exp) + else : sig = 1 - delta = (s_max - s_min)/1000. + s_min = int(G*(Exp - 10*sig)) + s_max = int(G*(Exp + 10*sig)) - s = numpy.array([k*delta+s_min for k in range(1000)]) + if (s_min < 0) : s_min = 0 - # probability density fuction - #p_signal = numpy.array(map(lambda y : self.prob_analog(y, Exp), s)) - p_signal = self.prob_analog(s, Exp) - p_ssum = p_signal.sum() + delta = (s_max - s_min)/1000. - # get signal (photoelectrons) - signal = numpy.random.choice(s, None, p=p_signal/p_ssum) - signal = signal/G + s = numpy.array([k*delta+s_min for k in range(1000)]) - else : - signal = 0 + # probability density fuction + #p_signal = numpy.array(map(lambda y : self.prob_analog(y, Exp), s)) + p_signal = self.prob_analog(s, Exp) + p_ssum = p_signal.sum() - # get detector noise (photoelectrons) - Nr = QE*self.configs.detector_readout_noise + # get signal (photoelectrons) + signal = numpy.random.choice(s, None, p=p_signal/p_ssum) + signal = signal/G - if (Nr > 0) : noise = numpy.random.normal(0, Nr, None) - else : noise = 0 + else : + signal = 0 - # A/D converter : Photoelectrons --> ADC counts - PE = signal + noise - ADC = self.get_ADC_value(pixel, PE) + # get detector noise (photoelectrons) + Nr = QE*self.configs.detector_readout_noise - # set data in array - output = [time, Exp, ADC] + if (Nr > 0) : noise = numpy.random.normal(0, Nr, None) + else : noise = 0 - return output + # A/D converter : Photoelectrons --> ADC counts + PE = signal + noise + ADC = self.get_ADC_value(pixel, PE) + # set data in array + output = [time, Exp, ADC] + return output diff --git a/bioimaging/fcs_script.py b/bioimaging/fcs_script.py index 0692623..697c80f 100755 --- a/bioimaging/fcs_script.py +++ b/bioimaging/fcs_script.py @@ -7,54 +7,53 @@ def test_fcs(t0, t1) : - # create FCS - fcs = FCSConfigs() - - fcs.set_LightSource(source_type='LASER', wave_length=488, flux=100e-6, radius=200e-9) - #fcs.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') - fcs.set_Fluorophore(fluorophore_type='EGFP') - fcs.set_Pinhole(radius=28.8e-6) - fcs.set_Magnification(Mag=60) - -# # PMT : Photon-counting mode -# fcs.set_Detector(detector='PMT', mode="Photon-counting", focal_point=(0.3,0.5,0.5), \ -# pixel_length=210e-9, exposure_time=10e-6, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) -# fcs.set_ADConverter(bit=12, offset=0, fullwell=4096) - - # PMT : Anlalog mode - fcs.set_Detector(detector='PMT', mode="Analog", focal_point=(0.3,0.5,0.5), \ - pixel_length=210e-9, exposure_time=10e-6, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) - fcs.set_ADConverter(bit=16, offset=0, fullwell=30000) - - ### Output data - #fcs.set_OutputData(image_file_dir='./images_fcs_D100_A100') - #fcs.set_OutputData(image_file_dir='./images_fcs_D100_A200') - #fcs.set_OutputData(image_file_dir='./images_fcs_D010_A100') - #fcs.set_OutputData(image_file_dir='./images_fcs_D010_A200') - fcs.set_OutputData(image_file_dir='./images_fcs_2StMD_A200') - - ### Input data - #fcs.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_11', start=t0, end=t1, observable="A") - #fcs.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_13', start=t0, end=t1, observable="A") - #fcs.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_12', start=t0, end=t1, observable="A") - #fcs.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_14', start=t0, end=t1, observable="A") - fcs.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_15', start=t0, end=t1, observable="A") - - # create physical effects - physics = PhysicalEffects() - physics.set_Conversion(ratio=1e-6) - #physics.set_Background(mean=3) - - # create image and movie - create = FCSVisualizer(configs=fcs, effects=physics) - create.output_frames(num_div=16) + # create FCS + fcs = FCSConfigs() + + fcs.set_LightSource(source_type='LASER', wave_length=488, flux=100e-6, radius=200e-9) + #fcs.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') + fcs.set_Fluorophore(fluorophore_type='EGFP') + fcs.set_Pinhole(radius=28.8e-6) + fcs.set_Magnification(Mag=60) + +# # PMT : Photon-counting mode +# fcs.set_Detector(detector='PMT', mode="Photon-counting", focal_point=(0.3,0.5,0.5), \ +# pixel_length=210e-9, exposure_time=10e-6, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) +# fcs.set_ADConverter(bit=12, offset=0, fullwell=4096) + + # PMT : Anlalog mode + fcs.set_Detector(detector='PMT', mode="Analog", focal_point=(0.3,0.5,0.5), \ + pixel_length=210e-9, exposure_time=10e-6, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) + fcs.set_ADConverter(bit=16, offset=0, fullwell=30000) + + ### Output data + #fcs.set_OutputData(image_file_dir='./images_fcs_D100_A100') + #fcs.set_OutputData(image_file_dir='./images_fcs_D100_A200') + #fcs.set_OutputData(image_file_dir='./images_fcs_D010_A100') + #fcs.set_OutputData(image_file_dir='./images_fcs_D010_A200') + fcs.set_OutputData(image_file_dir='./images_fcs_2StMD_A200') + + ### Input data + #fcs.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_11', start=t0, end=t1, observable="A") + #fcs.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_13', start=t0, end=t1, observable="A") + #fcs.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_12', start=t0, end=t1, observable="A") + #fcs.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_14', start=t0, end=t1, observable="A") + fcs.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_15', start=t0, end=t1, observable="A") + + # create physical effects + physics = PhysicalEffects() + physics.set_Conversion(ratio=1e-6) + #physics.set_Background(mean=3) + + # create image and movie + create = FCSVisualizer(configs=fcs, effects=physics) + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_fcs(t0, t1) + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_fcs(t0, t1) diff --git a/bioimaging/flip_handler.py b/bioimaging/flip_handler.py index a956ec0..6310223 100755 --- a/bioimaging/flip_handler.py +++ b/bioimaging/flip_handler.py @@ -32,13 +32,13 @@ class FLIPConfigs(LSCMConfigs) : ''' Point-scanning Confocal configuration - Point-like Gaussian Profile - + - Point-scanning - + - Pinhole - + - Detector : PMT + Point-like Gaussian Profile + + + Point-scanning + + + Pinhole + + + Detector : PMT ''' def __init__(self, user_configs_dict = None): @@ -67,7 +67,7 @@ def __init__(self, user_configs_dict = None): def set_LightSource(self, source_type = None, - wave_length = None, + wave_length = None, imaging_flux = None, bleaching_flux = None, bleaching_size = None, @@ -108,327 +108,324 @@ def set_Illumination_path(self) : # (plank const) * (speed of light) [joules meter] hc = 2.00e-25 - # Illumination + # Illumination w_0 = self.source_radius - # flux for imaging [joules/sec=watt] + # flux for imaging [joules/sec=watt] P_0 = self.source_flux - # flux for imaging [joules/sec=watt] + # flux for imaging [joules/sec=watt] P_1 = self.source_bleaching_flux # single photon energy [joules] wave_length = self.source_wavelength*1e-9 E_wl = hc/wave_length - # photon flux for imaging [photons/sec] + # photon flux for imaging [photons/sec] N_0 = P_0/E_wl - # photon flux for bleaching [photons/sec] + # photon flux for bleaching [photons/sec] N_1 = P_1/E_wl # Beam width [m] w_z = w_0*numpy.sqrt(1 + ((wave_length*d*1e-9)/(numpy.pi*w_0**2))**2) - # photon flux density for imaging [photon/(sec m^2)] + # photon flux density for imaging [photon/(sec m^2)] self.source_flux_density = numpy.array(map(lambda x : 2*N_0/(numpy.pi*x**2)*numpy.exp(-2*(r*1e-9/x)**2), w_z)) - # photon flux density for bleaching [photon/(sec m^2)] + # photon flux density for bleaching [photon/(sec m^2)] self.source_bleaching_flux_density = numpy.array(map(lambda x : 2*N_1/(numpy.pi*x**2)*numpy.exp(-2*(r*1e-9/x)**2), w_z)) - print('Photon Flux Density (Imaging) :', numpy.amax(self.source_flux_density)) - print('Photon Flux Density (Bleaching) :', numpy.amax(self.source_bleaching_flux_density)) + print('Photon Flux Density (Imaging) :', numpy.amax(self.source_flux_density)) + print('Photon Flux Density (Bleaching) :', numpy.amax(self.source_bleaching_flux_density)) class FLIPVisualizer(LSCMVisualizer) : - ''' - Confocal Visualization class of e-cell simulator - ''' + ''' + Confocal Visualization class of e-cell simulator + ''' - def __init__(self, configs=FLIPConfigs(), effects=PhysicalEffects()) : + def __init__(self, configs=FLIPConfigs(), effects=PhysicalEffects()) : - assert isinstance(configs, FLIPConfigs) - self.configs = configs + assert isinstance(configs, FLIPConfigs) + self.configs = configs - assert isinstance(effects, PhysicalEffects) - self.effects = effects + assert isinstance(effects, PhysicalEffects) + self.effects = effects - """ - Check and create the folder for image file. - """ - if not os.path.exists(self.configs.image_file_dir): - os.makedirs(self.configs.image_file_dir) - #else: - # for file in os.listdir(self.configs.movie_image_file_dir): - # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) + """ + Check and create the folder for image file. + """ + if not os.path.exists(self.configs.image_file_dir): + os.makedirs(self.configs.image_file_dir) + #else: + # for file in os.listdir(self.configs.movie_image_file_dir): + # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) - """ - Optical Path - """ - self.configs.set_Optical_path() + """ + Optical Path + """ + self.configs.set_Optical_path() - def rewrite_InputData(self, output_file_dir=None) : + def rewrite_InputData(self, output_file_dir=None) : - if not os.path.exists(output_file_dir): - os.makedirs(output_file_dir) + if not os.path.exists(output_file_dir): + os.makedirs(output_file_dir) - # define observational image plane in nm-scale - voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 + # define observational image plane in nm-scale + voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 - # image dimenssion in pixel-scale - Nw_pixel = self.configs.detector_image_size[0] - Nh_pixel = self.configs.detector_image_size[1] + # image dimenssion in pixel-scale + Nw_pixel = self.configs.detector_image_size[0] + Nh_pixel = self.configs.detector_image_size[1] - ## cell size (nm scale) - Nz = int(self.configs.spatiocyte_lengths[2]*voxel_size) - Ny = int(self.configs.spatiocyte_lengths[1]*voxel_size) - Nx = int(self.configs.spatiocyte_lengths[0]*voxel_size) + ## cell size (nm scale) + Nz = int(self.configs.spatiocyte_lengths[2]*voxel_size) + Ny = int(self.configs.spatiocyte_lengths[1]*voxel_size) + Nx = int(self.configs.spatiocyte_lengths[0]*voxel_size) - # pixel length : nm/pixel - Np = int(self.configs.image_scaling*voxel_size) + # pixel length : nm/pixel + Np = int(self.configs.image_scaling*voxel_size) - # image dimenssion in pixel-scale - Nz_pixel = Nz/Np - Ny_pixel = Ny/Np + # image dimenssion in pixel-scale + Nz_pixel = Nz/Np + Ny_pixel = Ny/Np - # Active states per detection time - dt = self.configs.spatiocyte_interval - exposure_time = self.configs.detector_exposure_time + # Active states per detection time + dt = self.configs.spatiocyte_interval + exposure_time = self.configs.detector_exposure_time - start = self.configs.spatiocyte_start_time - end = self.configs.spatiocyte_end_time + start = self.configs.spatiocyte_start_time + end = self.configs.spatiocyte_end_time - # Abogadoro's number - Na = self.effects.avogadoros_number + # Abogadoro's number + Na = self.effects.avogadoros_number - # spatiocyte size - radius = self.configs.spatiocyte_VoxelRadius - volume = 4.0/3.0*numpy.pi*radius**3 - depth = 2.0*radius + # spatiocyte size + radius = self.configs.spatiocyte_VoxelRadius + volume = 4.0/3.0*numpy.pi*radius**3 + depth = 2.0*radius - # Quantum yield - QY = self.effects.quantum_yield + # Quantum yield + QY = self.effects.quantum_yield - # Abs coefficient [1/(cm M)] - abs_coeff = self.effects.abs_coefficient + # Abs coefficient [1/(cm M)] + abs_coeff = self.effects.abs_coefficient - # Imaging : photon flux density [#photons/(sec m2)] - n0 = self.configs.source_flux_density + # Imaging : photon flux density [#photons/(sec m2)] + n0 = self.configs.source_flux_density - # Bleaching : photon flux density [#photons/(sec m2)] - n1 = self.configs.source_bleaching_flux_density + # Bleaching : photon flux density [#photons/(sec m2)] + n1 = self.configs.source_bleaching_flux_density - # scan time per pixel area - dT = exposure_time/(Nh_pixel*Nw_pixel) + # scan time per pixel area + dT = exposure_time/(Nh_pixel*Nw_pixel) - # Cross-Section [m2] - xsec = numpy.log(10)*(0.1*abs_coeff/Na) + # Cross-Section [m2] + xsec = numpy.log(10)*(0.1*abs_coeff/Na) - # Imaging : the number of absorbed photons - N_abs0 = xsec*n0*dT - - # Bleaching : the number of absorbed photons - N_abs1 = xsec*n1*dT - - # Beer-Lambert law : A = log(I0/I) = coef * concentration * path-length - A = (abs_coeff*0.1/Na)*(1/volume)*depth - - # Imaging : the number of emitted photons - N_emit0 = QY*N_abs0*(1 - 10**(-A)) + # Imaging : the number of absorbed photons + N_abs0 = xsec*n0*dT - # Bleaching : the number of emitted photons - N_emit1 = QY*N_abs1*(1 - 10**(-A)) + # Bleaching : the number of absorbed photons + N_abs1 = xsec*n1*dT - # copy input file - csv_input = self.configs.spatiocyte_file_directry + '/pt-input.csv' - shutil.copyfile(csv_input, output_file_dir + '/pt-input.csv') + # Beer-Lambert law : A = log(I0/I) = coef * concentration * path-length + A = (abs_coeff*0.1/Na)*(1/volume)*depth - # read input file - csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (0) - csv_list = list(csv.reader(open(csv_file_path, 'r'))) + # Imaging : the number of emitted photons + N_emit0 = QY*N_abs0*(1 - 10**(-A)) - # fluorescence - if (self.effects.photobleaching_switch == True) : + # Bleaching : the number of emitted photons + N_emit1 = QY*N_abs1*(1 - 10**(-A)) - # get the number of particles - N_particles = len(csv_list) + # copy input file + csv_input = self.configs.spatiocyte_file_directry + '/pt-input.csv' + shutil.copyfile(csv_input, output_file_dir + '/pt-input.csv') - # set fluorescence - self.effects.set_photophysics_4lscm(start, end, dT, N_particles) + # read input file + csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (0) + csv_list = list(csv.reader(open(csv_file_path, 'r'))) - bleach = self.effects.fluorescence_bleach - budget = self.effects.fluorescence_budget*N_emit0[0][0] - state = self.effects.fluorescence_state + # fluorescence + if (self.effects.photobleaching_switch == True) : - scan_state = numpy.copy(state*0) - scan_counter = numpy.zeros(shape=(N_particles)) + # get the number of particles + N_particles = len(csv_list) - # Mesh-grid for pixel-image - w = numpy.linspace(0, Nw_pixel-1, Nw_pixel) - h = numpy.linspace(0, Nh_pixel-1, Nh_pixel) + # set fluorescence + self.effects.set_photophysics_4lscm(start, end, dT, N_particles) - W, H = numpy.meshgrid(w, h) + bleach = self.effects.fluorescence_bleach + budget = self.effects.fluorescence_budget*N_emit0[0][0] + state = self.effects.fluorescence_state - # scan-time (sec) per pixel-image - dt_w = exposure_time/(Nh_pixel*Nw_pixel) - dt_h = dt_w*Nw_pixel + scan_state = numpy.copy(state*0) + scan_counter = numpy.zeros(shape=(N_particles)) - T_b = start + W*dt_w + H*dt_h - time = T_b.reshape((Nw_pixel*Nh_pixel)) + # Mesh-grid for pixel-image + w = numpy.linspace(0, Nw_pixel-1, Nw_pixel) + h = numpy.linspace(0, Nh_pixel-1, Nh_pixel) - # cell-center in pixel-image - W0, H0 = Nw_pixel/2, Nh_pixel/2 + W, H = numpy.meshgrid(w, h) - # cell-origin in pixel-image - w0, h0 = W0-int(Ny_pixel/2), H0-int(Nz_pixel/2) + # scan-time (sec) per pixel-image + dt_w = exposure_time/(Nh_pixel*Nw_pixel) + dt_h = dt_w*Nw_pixel - # cell-located region in pixel image - cell = numpy.zeros((Nw_pixel, Nh_pixel)) - cell[w0:w0+Ny_pixel,h0:h0+Nz_pixel] = 1 - cell_bool = cell.reshape((Nw_pixel*Nh_pixel)) + T_b = start + W*dt_w + H*dt_h + time = T_b.reshape((Nw_pixel*Nh_pixel)) - # Imaging : beam-position (nm) relative to cell-origin - Y_b, Z_b = numpy.meshgrid(Np*(w-w0), Np*(h-h0)) - X_b = Nx*self.configs.detector_focal_point[0] + # cell-center in pixel-image + W0, H0 = Nw_pixel/2, Nh_pixel/2 - # Bleaching : area size in pixel-image - Nw_bleach = self.configs.source_bleaching_size[0] - Nh_bleach = self.configs.source_bleaching_size[1] + # cell-origin in pixel-image + w0, h0 = W0-int(Ny_pixel/2), H0-int(Nz_pixel/2) - # Bleaching : beam position in pixel-image - Wb, Hb = self.configs.source_bleaching_position - w_b0, h_b0 = Wb-int(Nw_bleach/2), Hb-int(Nh_bleach/2) + # cell-located region in pixel image + cell = numpy.zeros((Nw_pixel, Nh_pixel)) + cell[w0:w0+Ny_pixel,h0:h0+Nz_pixel] = 1 + cell_bool = cell.reshape((Nw_pixel*Nh_pixel)) - # Bleaching : region in pixel image - bleach_area = numpy.zeros((Nw_pixel, Nh_pixel)) - bleach_area[w_b0:w_b0+Nw_bleach,h_b0:h_b0+Nh_bleach] = 1 - bleach_bool = bleach_area.reshape((Nw_pixel*Nh_pixel)) + # Imaging : beam-position (nm) relative to cell-origin + Y_b, Z_b = numpy.meshgrid(Np*(w-w0), Np*(h-h0)) + X_b = Nx*self.configs.detector_focal_point[0] - # set flux density as a function of radial and depth - r = numpy.linspace(0, 20000, 20001) - d = numpy.linspace(0, 20000, 20001) + # Bleaching : area size in pixel-image + Nw_bleach = self.configs.source_bleaching_size[0] + Nh_bleach = self.configs.source_bleaching_size[1] - # loop for scannig - index0 = int(start/dt) - delta_index = int(exposure_time/dt) + # Bleaching : beam position in pixel-image + Wb, Hb = self.configs.source_bleaching_position + w_b0, h_b0 = Wb-int(Nw_bleach/2), Hb-int(Nh_bleach/2) - # set bleaching index - time_bleaching = self.configs.source_bleaching_time - count_bleaching = int(time_bleaching/exposure_time) + # Bleaching : region in pixel image + bleach_area = numpy.zeros((Nw_pixel, Nh_pixel)) + bleach_area[w_b0:w_b0+Nw_bleach,h_b0:h_b0+Nh_bleach] = 1 + bleach_bool = bleach_area.reshape((Nw_pixel*Nh_pixel)) - while (time[-1] < end) : + # set flux density as a function of radial and depth + r = numpy.linspace(0, 20000, 20001) + d = numpy.linspace(0, 20000, 20001) - count_imaging = int(round(time[0]/exposure_time)) + # loop for scannig + index0 = int(start/dt) + delta_index = int(exposure_time/dt) - print('time : ', time[0], '-', time[-1], ' sec/image (', count_imaging, ')') + # set bleaching index + time_bleaching = self.configs.source_bleaching_time + count_bleaching = int(time_bleaching/exposure_time) - # set frame datasets - index1 = int(round(time[0]/dt)) - index_start = (index1 - index0) - index_end = (index1 - index0) + delta_index + while (time[-1] < end) : - time_index = (time/dt).astype('int') + count_imaging = int(round(time[0]/exposure_time)) - for index in range(index_start, index_end, 1) : + print('time : ', time[0], '-', time[-1], ' sec/image (', count_imaging, ')') - # read input file - csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (index) + # set frame datasets + index1 = int(round(time[0]/dt)) + index_start = (index1 - index0) + index_end = (index1 - index0) + delta_index - csv_list = list(csv.reader(open(csv_file_path, 'r'))) - dataset = numpy.array(csv_list) + time_index = (time/dt).astype('int') - # set imaging-area - time_bool = (time_index == index).astype('int') - imaging = (time_bool*cell_bool).astype('int') - len_imaging = len(imaging[imaging > 0]) + for index in range(index_start, index_end, 1) : - print(index, index*dt+start, 'length :', len_imaging) + # read input file + csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (index) - # set photo-bleaching-area - non_bleached_area = (time_bool*cell_bool*(1 - bleach_bool)).astype('int') - bleached_area = (time_bool*cell_bool*bleach_bool).astype('int') + csv_list = list(csv.reader(open(csv_file_path, 'r'))) + dataset = numpy.array(csv_list) - if (len_imaging > 0) : + # set imaging-area + time_bool = (time_index == index).astype('int') + imaging = (time_bool*cell_bool).astype('int') + len_imaging = len(imaging[imaging > 0]) - # loop for particles - for j, data_j in enumerate(dataset) : + print(index, index*dt+start, 'length :', len_imaging) - # set particle position - c_id = (float(data_j[1]), float(data_j[2]), float(data_j[3])) - x_i, y_i, z_i = self.get_coordinate(c_id) + # set photo-bleaching-area + non_bleached_area = (time_bool*cell_bool*(1 - bleach_bool)).astype('int') + bleached_area = (time_bool*cell_bool*bleach_bool).astype('int') - # beam axial position - """ Distance between beam depth and fluorophore (depth) """ - dX = int(abs(X_b - x_i)) - depth = dX if dX < d[-1] else d[-1] + if (len_imaging > 0) : - # beam lateral position - """ Distance between beam position and fluorophore (plane) """ - dR = numpy.sqrt((Y_b - y_i)**2 + (Z_b - z_i)**2) - dR_array = dR.reshape((Nw_pixel*Nh_pixel)) + # loop for particles + for j, data_j in enumerate(dataset) : - # Photobleaching - if (count_imaging >= count_bleaching) : - # set flux density as a function of radial and depth (at Non-bleached area) - R0 = (dR_array*non_bleached_area).astype('int') - radius0 = R0[R0 > 0] + # set particle position + c_id = (float(data_j[1]), float(data_j[2]), float(data_j[3])) + x_i, y_i, z_i = self.get_coordinate(c_id) - func_emit0 = interp1d(r, N_emit0[depth], bounds_error=False, fill_value=N_emit0[depth][r[-1]]) - N_emit = func_emit0(radius0) + # beam axial position + """ Distance between beam depth and fluorophore (depth) """ + dX = int(abs(X_b - x_i)) + depth = dX if dX < d[-1] else d[-1] - # set flux density as a function of radial and depth (at Bleached area) - R1 = (dR_array*bleached_area).astype('int') - radius1 = R1[R1 > 0] + # beam lateral position + """ Distance between beam position and fluorophore (plane) """ + dR = numpy.sqrt((Y_b - y_i)**2 + (Z_b - z_i)**2) + dR_array = dR.reshape((Nw_pixel*Nh_pixel)) - func_emit1 = interp1d(r, N_emit1[depth], bounds_error=False, fill_value=N_emit1[depth][r[-1]]) - N_emit_bleaching = func_emit1(radius1) + # Photobleaching + if (count_imaging >= count_bleaching) : + # set flux density as a function of radial and depth (at Non-bleached area) + R0 = (dR_array*non_bleached_area).astype('int') + radius0 = R0[R0 > 0] - # the number of emitted photons - N_emit_sum = N_emit.sum() + N_emit_bleaching.sum() + func_emit0 = interp1d(r, N_emit0[depth], bounds_error=False, fill_value=N_emit0[depth][r[-1]]) + N_emit = func_emit0(radius0) - # Imaging - else : - # set flux density as a function of radial and depth (at Non-bleached area) - R0 = (dR_array*imaging).astype('int') - radius0 = R0[R0 > 0] + # set flux density as a function of radial and depth (at Bleached area) + R1 = (dR_array*bleached_area).astype('int') + radius1 = R1[R1 > 0] - func_emit0 = interp1d(r, N_emit0[depth], bounds_error=False, fill_value=N_emit0[depth][r[-1]]) - N_emit = func_emit0(radius0) + func_emit1 = interp1d(r, N_emit1[depth], bounds_error=False, fill_value=N_emit1[depth][r[-1]]) + N_emit_bleaching = func_emit1(radius1) - # the number of emitted photons - N_emit_sum = N_emit.sum() + # the number of emitted photons + N_emit_sum = N_emit.sum() + N_emit_bleaching.sum() - scan_counter[j] += (N_emit_sum/N_emit0[0][0]) + # Imaging + else : + # set flux density as a function of radial and depth (at Non-bleached area) + R0 = (dR_array*imaging).astype('int') + radius0 = R0[R0 > 0] - if (int(scan_counter[j]) < len(state[j,:])) : - state_j = state[j,int(scan_counter[j])] - else : - state_j = state[j,-1] + func_emit0 = interp1d(r, N_emit0[depth], bounds_error=False, fill_value=N_emit0[depth][r[-1]]) + N_emit = func_emit0(radius0) - if (budget[j] > N_emit_sum) : - budget[j] = budget[j] - state_j*N_emit_sum - else : - budget[j] = 0 + # the number of emitted photons + N_emit_sum = N_emit.sum() - scan_state[j,count_imaging-int(start/dt)] = state_j + scan_counter[j] += (N_emit_sum/N_emit0[0][0]) + if (int(scan_counter[j]) < len(state[j,:])) : + state_j = state[j,int(scan_counter[j])] + else : + state_j = state[j,-1] - state_stack = numpy.column_stack((scan_state[:,count_imaging-int(start/dt)], (budget/N_emit0[0][0]).astype('int'))) - new_dataset = numpy.column_stack((dataset, state_stack)) + if (budget[j] > N_emit_sum) : + budget[j] = budget[j] - state_j*N_emit_sum + else : + budget[j] = 0 - # write output file - output_file = output_file_dir + '/pt-%09d.0.csv' % (index) + scan_state[j,count_imaging-int(start/dt)] = state_j - with open(output_file, 'w') as f : - writer = csv.writer(f) - writer.writerows(new_dataset) - time += exposure_time + state_stack = numpy.column_stack((scan_state[:,count_imaging-int(start/dt)], (budget/N_emit0[0][0]).astype('int'))) + new_dataset = numpy.column_stack((dataset, state_stack)) + # write output file + output_file = output_file_dir + '/pt-%09d.0.csv' % (index) + with open(output_file, 'w') as f : + writer = csv.writer(f) + writer.writerows(new_dataset) + time += exposure_time diff --git a/bioimaging/flip_rewrite_script.py b/bioimaging/flip_rewrite_script.py index b8e601f..d65eeb7 100755 --- a/bioimaging/flip_rewrite_script.py +++ b/bioimaging/flip_rewrite_script.py @@ -7,56 +7,54 @@ def test_flip(t0, t1) : - # create FLIP imaging - flip = FLIPConfigs() - flip.set_LightSource(source_type='LASER', wave_length=532, imaging_flux=100e-6, bleaching_flux=100000e-3, \ - bleaching_size=(30,30), bleaching_position=(512,512), bleaching_time=1.2, radius=400e-9) - flip.set_Fluorophore(fluorophore_type='EGFP') - flip.set_Pinhole(radius=28.8e-6) - flip.set_Magnification(Mag=60) - - # PMT : Photon-counting mode - flip.set_Detector(detector='PMT', mode="Photon-counting", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ - pixel_length=207.16e-9, scan_time=1.10, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) - flip.set_ADConverter(bit=12, offset=0, fullwell=4096) - -# # PMT : Anlalog mode -# flip.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ -# pixel_length=207.16e-9, scan_time=1.15, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) -# flip.set_ADConverter(bit=12, offset=0, fullwell=4096) - -# # Output data -# flip.set_OutputData(image_file_dir='./numpys_flip_001000A') -# flip.set_OutputData(image_file_dir='./numpys_flip_019656A') - - # Input data - #flip.reset_InputData('/home/masaki/wrk/spatiocyte/models/beads_3D_000100A', start=t0, end=t1, observable="A") - #flip.reset_InputData('/home/masaki/wrk/spatiocyte/models/beads_3D_019656A', start=t0, end=t1, observable="A") - flip.reset_InputData('/home/masaki/wrk/spatiocyte/models/beads_3D_080000A', start=t0, end=t1, observable="A") - - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0) - physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) - physics.set_photobleaching(tau0=100e-6, alpha=0.73) - #physics.set_photoactivation(turn_on_ratio=1000, activation_yield=1.0, frac_preactivation=0.00) - #physics.set_photoblinking(t0_on=30e-6, a_on=0.58, t0_off=10e-6, a_off=0.48) - - # create image - create = FLIPVisualizer(configs=flip, effects=physics) - #create.rewrite_InputData(output_file_dir='./data_flip_000100A_bleach') - #create.rewrite_InputData(output_file_dir='./data_flip_019656A_bleach') - #create.rewrite_InputData(output_file_dir='./data_flip_019656A_blink') - create.rewrite_InputData(output_file_dir='./data_flip_010000A_bleach') - #create.output_frames(num_div=16) + # create FLIP imaging + flip = FLIPConfigs() + flip.set_LightSource(source_type='LASER', wave_length=532, imaging_flux=100e-6, bleaching_flux=100000e-3, \ + bleaching_size=(30,30), bleaching_position=(512,512), bleaching_time=1.2, radius=400e-9) + flip.set_Fluorophore(fluorophore_type='EGFP') + flip.set_Pinhole(radius=28.8e-6) + flip.set_Magnification(Mag=60) + + # PMT : Photon-counting mode + flip.set_Detector(detector='PMT', mode="Photon-counting", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ + pixel_length=207.16e-9, scan_time=1.10, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) + flip.set_ADConverter(bit=12, offset=0, fullwell=4096) + +# # PMT : Anlalog mode +# flip.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ +# pixel_length=207.16e-9, scan_time=1.15, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) +# flip.set_ADConverter(bit=12, offset=0, fullwell=4096) + +# # Output data +# flip.set_OutputData(image_file_dir='./numpys_flip_001000A') +# flip.set_OutputData(image_file_dir='./numpys_flip_019656A') + + # Input data + #flip.reset_InputData('/home/masaki/wrk/spatiocyte/models/beads_3D_000100A', start=t0, end=t1, observable="A") + #flip.reset_InputData('/home/masaki/wrk/spatiocyte/models/beads_3D_019656A', start=t0, end=t1, observable="A") + flip.reset_InputData('/home/masaki/wrk/spatiocyte/models/beads_3D_080000A', start=t0, end=t1, observable="A") + + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0) + physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) + physics.set_photobleaching(tau0=100e-6, alpha=0.73) + #physics.set_photoactivation(turn_on_ratio=1000, activation_yield=1.0, frac_preactivation=0.00) + #physics.set_photoblinking(t0_on=30e-6, a_on=0.58, t0_off=10e-6, a_off=0.48) + + # create image + create = FLIPVisualizer(configs=flip, effects=physics) + #create.rewrite_InputData(output_file_dir='./data_flip_000100A_bleach') + #create.rewrite_InputData(output_file_dir='./data_flip_019656A_bleach') + #create.rewrite_InputData(output_file_dir='./data_flip_019656A_blink') + create.rewrite_InputData(output_file_dir='./data_flip_010000A_bleach') + #create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_flip(t0, t1) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_flip(t0, t1) diff --git a/bioimaging/flip_script.py b/bioimaging/flip_script.py index bb64ea2..1834fd9 100755 --- a/bioimaging/flip_script.py +++ b/bioimaging/flip_script.py @@ -7,58 +7,56 @@ def test_flip(t0, t1) : - # create FLIP imaging - flip = FLIPConfigs() - flip.set_LightSource(source_type='LASER', wave_length=532, imaging_flux=100e-6, bleaching_flux=100000e-3, \ - bleaching_size=(30,30), bleaching_position=(512,512), bleaching_time=1.2, radius=400e-9) - flip.set_Fluorophore(fluorophore_type='EGFP') - flip.set_Pinhole(radius=28.8e-6) - flip.set_Magnification(Mag=60) - - # PMT : Photon-counting mode - flip.set_Detector(detector='PMT', mode="Photon-counting", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ - pixel_length=207.16e-9, scan_time=1.10, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) - flip.set_ADConverter(bit=12, offset=0, fullwell=4096) - -# # PMT : Anlalog mode -# flip.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ -# pixel_length=207.16e-9, scan_time=1.15, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) -# flip.set_ADConverter(bit=12, offset=0, fullwell=4096) - -# # Output data - flip.set_OutputData(image_file_dir='./numpys_flip_080000A_bleach') - #flip.set_OutputData(image_file_dir='./numpys_flip_019656A_bleach') - #flip.set_OutputData(image_file_dir='./numpys_flip_019656A_blink') - #flip.set_OutputData(image_file_dir='./numpys_flip_000100A_bleach') - - # Input data - #flip.set_InputData('/home/masaki/wrk/spatiocyte/models/beads_3D_019656A', start=t0, end=t1, observable="A") - flip.set_InputData('/home/masaki/bioimaging_4public/data_flip_010000A_bleach', start=t0, end=t1, observable="A") - #flip.set_InputData('/home/masaki/bioimaging_4public/data_flip_019656A_bleach', start=t0, end=t1, observable="A") - #flip.set_InputData('/home/masaki/bioimaging_4public/data_flip_019656A_blink', start=t0, end=t1, observable="A") - #flip.set_InputData('/home/masaki/bioimaging_4public/data_flip_000100A_bleach', start=t0, end=t1, observable="A") - - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0) - physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) - physics.set_photobleaching(tau0=1e-3, alpha=0.73) - #physics.set_photoactivation(turn_on_ratio=1000, activation_yield=1.0, frac_preactivation=0.00) - #physics.set_photoblinking(t0_on=30e-6, a_on=0.58, t0_off=10e-6, a_off=0.48) - - # create image - create = FLIPVisualizer(configs=flip, effects=physics) - #create.rewrite_InputData(output_file_dir='./data_flip_19656A_bleach') - #create.rewrite_InputData(output_file_dir='./data_flip_19656A_blink') - create.output_frames(num_div=16) + # create FLIP imaging + flip = FLIPConfigs() + flip.set_LightSource(source_type='LASER', wave_length=532, imaging_flux=100e-6, bleaching_flux=100000e-3, \ + bleaching_size=(30,30), bleaching_position=(512,512), bleaching_time=1.2, radius=400e-9) + flip.set_Fluorophore(fluorophore_type='EGFP') + flip.set_Pinhole(radius=28.8e-6) + flip.set_Magnification(Mag=60) + + # PMT : Photon-counting mode + flip.set_Detector(detector='PMT', mode="Photon-counting", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ + pixel_length=207.16e-9, scan_time=1.10, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) + flip.set_ADConverter(bit=12, offset=0, fullwell=4096) + +# # PMT : Anlalog mode +# flip.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ +# pixel_length=207.16e-9, scan_time=1.15, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) +# flip.set_ADConverter(bit=12, offset=0, fullwell=4096) + +# # Output data + flip.set_OutputData(image_file_dir='./numpys_flip_080000A_bleach') + #flip.set_OutputData(image_file_dir='./numpys_flip_019656A_bleach') + #flip.set_OutputData(image_file_dir='./numpys_flip_019656A_blink') + #flip.set_OutputData(image_file_dir='./numpys_flip_000100A_bleach') + + # Input data + #flip.set_InputData('/home/masaki/wrk/spatiocyte/models/beads_3D_019656A', start=t0, end=t1, observable="A") + flip.set_InputData('/home/masaki/bioimaging_4public/data_flip_010000A_bleach', start=t0, end=t1, observable="A") + #flip.set_InputData('/home/masaki/bioimaging_4public/data_flip_019656A_bleach', start=t0, end=t1, observable="A") + #flip.set_InputData('/home/masaki/bioimaging_4public/data_flip_019656A_blink', start=t0, end=t1, observable="A") + #flip.set_InputData('/home/masaki/bioimaging_4public/data_flip_000100A_bleach', start=t0, end=t1, observable="A") + + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0) + physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) + physics.set_photobleaching(tau0=1e-3, alpha=0.73) + #physics.set_photoactivation(turn_on_ratio=1000, activation_yield=1.0, frac_preactivation=0.00) + #physics.set_photoblinking(t0_on=30e-6, a_on=0.58, t0_off=10e-6, a_off=0.48) + + # create image + create = FLIPVisualizer(configs=flip, effects=physics) + #create.rewrite_InputData(output_file_dir='./data_flip_19656A_bleach') + #create.rewrite_InputData(output_file_dir='./data_flip_19656A_blink') + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_flip(t0, t1) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_flip(t0, t1) diff --git a/bioimaging/fluorescence.py b/bioimaging/fluorescence.py index 99d6641..c0dfc8c 100644 --- a/bioimaging/fluorescence.py +++ b/bioimaging/fluorescence.py @@ -26,134 +26,132 @@ def prob_levy(t, t0, a) : - prob = (a/t0)*(t0/t)**(1+a) + prob = (a/t0)*(t0/t)**(1+a) - return prob + return prob def convert(start, end) : - ##### set tau-array for probability density fuction - NNN = int(1e+7) - tau_on = numpy.array([i*dt+t0_on for i in range(NNN)]) - tau_off = numpy.array([i*dt+t0_off for i in range(NNN)]) - tau_bleach = numpy.array([i*dt+t0_bleach for i in range(NNN)]) + ##### set tau-array for probability density fuction + NNN = int(1e+7) + tau_on = numpy.array([i*dt+t0_on for i in range(NNN)]) + tau_off = numpy.array([i*dt+t0_off for i in range(NNN)]) + tau_bleach = numpy.array([i*dt+t0_bleach for i in range(NNN)]) - ##### set photo-blinking effects - NN = int(end/dt/2) + ##### set photo-blinking effects + NN = int(end/dt/2) - # ON-state - prob_on = prob_levy(tau_on, t0_on, a_on) - prob_on_sum = prob_on.sum() - p_on = prob_on/prob_on_sum + # ON-state + prob_on = prob_levy(tau_on, t0_on, a_on) + prob_on_sum = prob_on.sum() + p_on = prob_on/prob_on_sum - t_on = numpy.random.choice(tau_on, NN, p=p_on) + t_on = numpy.random.choice(tau_on, NN, p=p_on) - # OFF-state - prob_off = prob_levy(tau_off, t0_off, a_off) - prob_off_sum = prob_off.sum() - p_off = prob_off/prob_off_sum + # OFF-state + prob_off = prob_levy(tau_off, t0_off, a_off) + prob_off_sum = prob_off.sum() + p_off = prob_off/prob_off_sum - t_off = numpy.random.choice(tau_off, NN, p=p_off) + t_off = numpy.random.choice(tau_off, NN, p=p_off) - # merge t_on/off arrays - a = numpy.array([t_on, t_off]) - t_onoff = numpy.reshape(a, 2*NN, order='F') - t = numpy.cumsum(t_onoff) + # merge t_on/off arrays + a = numpy.array([t_on, t_off]) + t_onoff = numpy.reshape(a, 2*NN, order='F') + t = numpy.cumsum(t_onoff) - # set state array - s_on = numpy.ones(shape=(1, NN)) - s_off = numpy.zeros(shape=(1, NN)) - c = numpy.array([s_on, s_off]) - s = numpy.reshape(c, 2*NN, order='F') + # set state array + s_on = numpy.ones(shape=(1, NN)) + s_off = numpy.zeros(shape=(1, NN)) + c = numpy.array([s_on, s_off]) + s = numpy.reshape(c, 2*NN, order='F') - ##### set photo-bleaching effect - prob_bleach = prob_levy(tau_bleach, t0_bleach, a_bleach) - prob_bleach_sum = prob_bleach.sum() - p_bleach = prob_bleach/prob_bleach_sum + ##### set photo-bleaching effect + prob_bleach = prob_levy(tau_bleach, t0_bleach, a_bleach) + prob_bleach_sum = prob_bleach.sum() + p_bleach = prob_bleach/prob_bleach_sum - t_bleach = numpy.random.choice(tau_bleach, None, p=p_bleach) + t_bleach = numpy.random.choice(tau_bleach, None, p=p_bleach) - ##### - # Active states per detection time - t_life = 30e-9 - N_act0 = int(dt/t_life) - N_act = N_act0 + ##### + # Active states per detection time + t_life = 30e-9 + N_act0 = int(dt/t_life) + N_act = N_act0 - # set active states (All) - N_tot = 0 + # set active states (All) + N_tot = 0 - # initial - Na = 6.022e+23 - hc = 2.00e-25 - wl = 532e-9 - E = hc/wl - abs_coeff = 1e+5 # 1/(cm M) - xsec = numpy.log(10)*0.1*abs_coeff/Na # m2 - QY = 1.0 - P = 20 # W/cm2 + # initial + Na = 6.022e+23 + hc = 2.00e-25 + wl = 532e-9 + E = hc/wl + abs_coeff = 1e+5 # 1/(cm M) + xsec = numpy.log(10)*0.1*abs_coeff/Na # m2 + QY = 1.0 + P = 20 # W/cm2 - r = 20e-9 - volume = 4.0/3.0*numpy.pi*r**3 - depth = 2.0*r + r = 20e-9 + volume = 4.0/3.0*numpy.pi*r**3 + depth = 2.0*r - # the number of absorbed photons - N_abs = (xsec*1e+4)*(P/E)*dt + # the number of absorbed photons + N_abs = (xsec*1e+4)*(P/E)*dt - # Beer-Lambert law : A = log(I0/I) = coef * concentration * path length - A = (abs_coeff*0.1/Na)*(1/volume)*depth + # Beer-Lambert law : A = log(I0/I) = coef * concentration * path length + A = (abs_coeff*0.1/Na)*(1/volume)*depth - # the number of emitted photons - N_emit0 = QY*N_abs*(1 - 10**(-A)) + # the number of emitted photons + N_emit0 = QY*N_abs*(1 - 10**(-A)) - # set permanent dark state (PDS) - N_pds = 0 + # set permanent dark state (PDS) + N_pds = 0 - time = start + time = start - while (time < end) : - N_on = N_off = 0 - N_emit = 0 + while (time < end) : + N_on = N_off = 0 + N_emit = 0 - # get current index of blinking-state array - i = (numpy.abs(t - time)).argmin() + # get current index of blinking-state array + i = (numpy.abs(t - time)).argmin() - # Photo-blinking (ON-state) - if (s[i] == 1) : - N_on = N_act + # Photo-blinking (ON-state) + if (s[i] == 1) : + N_on = N_act - # Fraction of active-molecules - f = N_act/N_act0 + # Fraction of active-molecules + f = N_act/N_act0 - # The number of emitted photons - N_emit = N_emit0 * f + # The number of emitted photons + N_emit = N_emit0 * f - # Photo-blinking (OFF-state) - else : - N_off = N_act + # Photo-blinking (OFF-state) + else : + N_off = N_act - # Photo-bleaching (to permanent dark state) - #dN = N_off*(1/t_pulse + 1/t_bleach)*dt - dN = N_off*(1/t_bleach)*dt - N_pds = N_pds + dN - N_off = N_off - dN + # Photo-bleaching (to permanent dark state) + #dN = N_off*(1/t_pulse + 1/t_bleach)*dt + dN = N_off*(1/t_bleach)*dt + N_pds = N_pds + dN + N_off = N_off - dN - # the number of active states at a given time - N_act = N_on + N_off + # the number of active states at a given time + N_act = N_on + N_off - # the number of active states (Total) - N_tot = N_tot + N_act + # the number of active states (Total) + N_tot = N_tot + N_act - print(time, N_on, N_off, N_pds, N_tot, N_emit) + print(time, N_on, N_off, N_pds, N_tot, N_emit) - time += dt + time += dt if __name__=='__main__': - start = float(sys.argv[1]) - end = float(sys.argv[2]) - - convert(start, end) - + start = float(sys.argv[1]) + end = float(sys.argv[2]) + convert(start, end) diff --git a/bioimaging/frap_handler.py b/bioimaging/frap_handler.py index 3de8565..5a897c6 100755 --- a/bioimaging/frap_handler.py +++ b/bioimaging/frap_handler.py @@ -32,13 +32,13 @@ class FRAPConfigs(LSCMConfigs) : ''' Point-scanning Confocal configuration - Point-like Gaussian Profile - + - Point-scanning - + - Pinhole - + - Detector : PMT + Point-like Gaussian Profile + + + Point-scanning + + + Pinhole + + + Detector : PMT ''' def __init__(self, user_configs_dict = None): @@ -67,7 +67,7 @@ def __init__(self, user_configs_dict = None): def set_LightSource(self, source_type = None, - wave_length = None, + wave_length = None, imaging_flux = None, bleaching_flux = None, bleaching_size = None, @@ -108,327 +108,324 @@ def set_Illumination_path(self) : # (plank const) * (speed of light) [joules meter] hc = 2.00e-25 - # Illumination + # Illumination w_0 = self.source_radius - # flux for imaging [joules/sec=watt] + # flux for imaging [joules/sec=watt] P_0 = self.source_flux - # flux for imaging [joules/sec=watt] + # flux for imaging [joules/sec=watt] P_1 = self.source_bleaching_flux # single photon energy [joules] wave_length = self.source_wavelength*1e-9 E_wl = hc/wave_length - # photon flux for imaging [photons/sec] + # photon flux for imaging [photons/sec] N_0 = P_0/E_wl - # photon flux for bleaching [photons/sec] + # photon flux for bleaching [photons/sec] N_1 = P_1/E_wl # Beam width [m] w_z = w_0*numpy.sqrt(1 + ((wave_length*d*1e-9)/(numpy.pi*w_0**2))**2) - # photon flux density for imaging [photon/(sec m^2)] + # photon flux density for imaging [photon/(sec m^2)] self.source_flux_density = numpy.array(map(lambda x : 2*N_0/(numpy.pi*x**2)*numpy.exp(-2*(r*1e-9/x)**2), w_z)) - # photon flux density for bleaching [photon/(sec m^2)] + # photon flux density for bleaching [photon/(sec m^2)] self.source_bleaching_flux_density = numpy.array(map(lambda x : 2*N_1/(numpy.pi*x**2)*numpy.exp(-2*(r*1e-9/x)**2), w_z)) - print('Photon Flux Density (Imaging) :', numpy.amax(self.source_flux_density)) - print('Photon Flux Density (Bleaching) :', numpy.amax(self.source_bleaching_flux_density)) + print('Photon Flux Density (Imaging) :', numpy.amax(self.source_flux_density)) + print('Photon Flux Density (Bleaching) :', numpy.amax(self.source_bleaching_flux_density)) class FRAPVisualizer(LSCMVisualizer) : - ''' - Confocal Visualization class of e-cell simulator - ''' + ''' + Confocal Visualization class of e-cell simulator + ''' - def __init__(self, configs=FRAPConfigs(), effects=PhysicalEffects()) : + def __init__(self, configs=FRAPConfigs(), effects=PhysicalEffects()) : - assert isinstance(configs, FRAPConfigs) - self.configs = configs + assert isinstance(configs, FRAPConfigs) + self.configs = configs - assert isinstance(effects, PhysicalEffects) - self.effects = effects + assert isinstance(effects, PhysicalEffects) + self.effects = effects - """ - Check and create the folder for image file. - """ - if not os.path.exists(self.configs.image_file_dir): - os.makedirs(self.configs.image_file_dir) - #else: - # for file in os.listdir(self.configs.movie_image_file_dir): - # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) + """ + Check and create the folder for image file. + """ + if not os.path.exists(self.configs.image_file_dir): + os.makedirs(self.configs.image_file_dir) + #else: + # for file in os.listdir(self.configs.movie_image_file_dir): + # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) - """ - Optical Path - """ - self.configs.set_Optical_path() + """ + Optical Path + """ + self.configs.set_Optical_path() - def rewrite_InputData(self, output_file_dir=None) : + def rewrite_InputData(self, output_file_dir=None) : - if not os.path.exists(output_file_dir): - os.makedirs(output_file_dir) + if not os.path.exists(output_file_dir): + os.makedirs(output_file_dir) - # define observational image plane in nm-scale - voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 + # define observational image plane in nm-scale + voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 - # image dimenssion in pixel-scale - Nw_pixel = self.configs.detector_image_size[0] - Nh_pixel = self.configs.detector_image_size[1] + # image dimenssion in pixel-scale + Nw_pixel = self.configs.detector_image_size[0] + Nh_pixel = self.configs.detector_image_size[1] - ## cell size (nm scale) - Nz = int(self.configs.spatiocyte_lengths[2]*voxel_size) - Ny = int(self.configs.spatiocyte_lengths[1]*voxel_size) - Nx = int(self.configs.spatiocyte_lengths[0]*voxel_size) + ## cell size (nm scale) + Nz = int(self.configs.spatiocyte_lengths[2]*voxel_size) + Ny = int(self.configs.spatiocyte_lengths[1]*voxel_size) + Nx = int(self.configs.spatiocyte_lengths[0]*voxel_size) - # pixel length : nm/pixel - Np = int(self.configs.image_scaling*voxel_size) + # pixel length : nm/pixel + Np = int(self.configs.image_scaling*voxel_size) - # image dimenssion in pixel-scale - Nz_pixel = Nz/Np - Ny_pixel = Ny/Np + # image dimenssion in pixel-scale + Nz_pixel = Nz/Np + Ny_pixel = Ny/Np - # Active states per detection time - dt = self.configs.spatiocyte_interval - exposure_time = self.configs.detector_exposure_time + # Active states per detection time + dt = self.configs.spatiocyte_interval + exposure_time = self.configs.detector_exposure_time - start = self.configs.spatiocyte_start_time - end = self.configs.spatiocyte_end_time + start = self.configs.spatiocyte_start_time + end = self.configs.spatiocyte_end_time - # Abogadoro's number - Na = self.effects.avogadoros_number + # Abogadoro's number + Na = self.effects.avogadoros_number - # spatiocyte size - radius = self.configs.spatiocyte_VoxelRadius - volume = 4.0/3.0*numpy.pi*radius**3 - depth = 2.0*radius + # spatiocyte size + radius = self.configs.spatiocyte_VoxelRadius + volume = 4.0/3.0*numpy.pi*radius**3 + depth = 2.0*radius - # Quantum yield - QY = self.effects.quantum_yield + # Quantum yield + QY = self.effects.quantum_yield - # Abs coefficient [1/(cm M)] - abs_coeff = self.effects.abs_coefficient + # Abs coefficient [1/(cm M)] + abs_coeff = self.effects.abs_coefficient - # Imaging : photon flux density [#photons/(sec m2)] - n0 = self.configs.source_flux_density + # Imaging : photon flux density [#photons/(sec m2)] + n0 = self.configs.source_flux_density - # Bleaching : photon flux density [#photons/(sec m2)] - n1 = self.configs.source_bleaching_flux_density + # Bleaching : photon flux density [#photons/(sec m2)] + n1 = self.configs.source_bleaching_flux_density - # scan time per pixel area - dT = exposure_time/(Nh_pixel*Nw_pixel) + # scan time per pixel area + dT = exposure_time/(Nh_pixel*Nw_pixel) - # Cross-Section [m2] - xsec = numpy.log(10)*(0.1*abs_coeff/Na) + # Cross-Section [m2] + xsec = numpy.log(10)*(0.1*abs_coeff/Na) - # Imaging : the number of absorbed photons - N_abs0 = xsec*n0*dT - - # Bleaching : the number of absorbed photons - N_abs1 = xsec*n1*dT - - # Beer-Lambert law : A = log(I0/I) = coef * concentration * path-length - A = (abs_coeff*0.1/Na)*(1/volume)*depth - - # Imaging : the number of emitted photons - N_emit0 = QY*N_abs0*(1 - 10**(-A)) + # Imaging : the number of absorbed photons + N_abs0 = xsec*n0*dT - # Bleaching : the number of emitted photons - N_emit1 = QY*N_abs1*(1 - 10**(-A)) + # Bleaching : the number of absorbed photons + N_abs1 = xsec*n1*dT - # copy input file - csv_input = self.configs.spatiocyte_file_directry + '/pt-input.csv' - shutil.copyfile(csv_input, output_file_dir + '/pt-input.csv') + # Beer-Lambert law : A = log(I0/I) = coef * concentration * path-length + A = (abs_coeff*0.1/Na)*(1/volume)*depth - # read input file - csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (0) - csv_list = list(csv.reader(open(csv_file_path, 'r'))) + # Imaging : the number of emitted photons + N_emit0 = QY*N_abs0*(1 - 10**(-A)) - # fluorescence - if (self.effects.photobleaching_switch == True) : + # Bleaching : the number of emitted photons + N_emit1 = QY*N_abs1*(1 - 10**(-A)) - # get the number of particles - N_particles = len(csv_list) + # copy input file + csv_input = self.configs.spatiocyte_file_directry + '/pt-input.csv' + shutil.copyfile(csv_input, output_file_dir + '/pt-input.csv') - # set fluorescence - self.effects.set_photophysics_4lscm(start, end, dT, N_particles) + # read input file + csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (0) + csv_list = list(csv.reader(open(csv_file_path, 'r'))) - bleach = self.effects.fluorescence_bleach - budget = self.effects.fluorescence_budget*N_emit0[0][0] - state = self.effects.fluorescence_state + # fluorescence + if (self.effects.photobleaching_switch == True) : - scan_state = numpy.copy(state*0) - scan_counter = numpy.zeros(shape=(N_particles)) + # get the number of particles + N_particles = len(csv_list) - # Mesh-grid for pixel-image - w = numpy.linspace(0, Nw_pixel-1, Nw_pixel) - h = numpy.linspace(0, Nh_pixel-1, Nh_pixel) + # set fluorescence + self.effects.set_photophysics_4lscm(start, end, dT, N_particles) - W, H = numpy.meshgrid(w, h) + bleach = self.effects.fluorescence_bleach + budget = self.effects.fluorescence_budget*N_emit0[0][0] + state = self.effects.fluorescence_state - # scan-time (sec) per pixel-image - dt_w = exposure_time/(Nh_pixel*Nw_pixel) - dt_h = dt_w*Nw_pixel + scan_state = numpy.copy(state*0) + scan_counter = numpy.zeros(shape=(N_particles)) - T_b = start + W*dt_w + H*dt_h - time = T_b.reshape((Nw_pixel*Nh_pixel)) + # Mesh-grid for pixel-image + w = numpy.linspace(0, Nw_pixel-1, Nw_pixel) + h = numpy.linspace(0, Nh_pixel-1, Nh_pixel) - # cell-center in pixel-image - W0, H0 = Nw_pixel/2, Nh_pixel/2 + W, H = numpy.meshgrid(w, h) - # cell-origin in pixel-image - w0, h0 = W0-int(Ny_pixel/2), H0-int(Nz_pixel/2) + # scan-time (sec) per pixel-image + dt_w = exposure_time/(Nh_pixel*Nw_pixel) + dt_h = dt_w*Nw_pixel - # cell-located region in pixel image - cell = numpy.zeros((Nw_pixel, Nh_pixel)) - cell[w0:w0+Ny_pixel,h0:h0+Nz_pixel] = 1 - cell_bool = cell.reshape((Nw_pixel*Nh_pixel)) + T_b = start + W*dt_w + H*dt_h + time = T_b.reshape((Nw_pixel*Nh_pixel)) - # Imaging : beam-position (nm) relative to cell-origin - Y_b, Z_b = numpy.meshgrid(Np*(w-w0), Np*(h-h0)) - X_b = Nx*self.configs.detector_focal_point[0] + # cell-center in pixel-image + W0, H0 = Nw_pixel/2, Nh_pixel/2 - # Bleaching : area size in pixel-image - Nw_bleach = self.configs.source_bleaching_size[0] - Nh_bleach = self.configs.source_bleaching_size[1] + # cell-origin in pixel-image + w0, h0 = W0-int(Ny_pixel/2), H0-int(Nz_pixel/2) - # Bleaching : beam position in pixel-image - Wb, Hb = self.configs.source_bleaching_position - w_b0, h_b0 = Wb-int(Nw_bleach/2), Hb-int(Nh_bleach/2) + # cell-located region in pixel image + cell = numpy.zeros((Nw_pixel, Nh_pixel)) + cell[w0:w0+Ny_pixel,h0:h0+Nz_pixel] = 1 + cell_bool = cell.reshape((Nw_pixel*Nh_pixel)) - # Bleaching : region in pixel image - bleach_area = numpy.zeros((Nw_pixel, Nh_pixel)) - bleach_area[w_b0:w_b0+Nw_bleach,h_b0:h_b0+Nh_bleach] = 1 - bleach_bool = bleach_area.reshape((Nw_pixel*Nh_pixel)) + # Imaging : beam-position (nm) relative to cell-origin + Y_b, Z_b = numpy.meshgrid(Np*(w-w0), Np*(h-h0)) + X_b = Nx*self.configs.detector_focal_point[0] - # set flux density as a function of radial and depth - r = numpy.linspace(0, 20000, 20001) - d = numpy.linspace(0, 20000, 20001) + # Bleaching : area size in pixel-image + Nw_bleach = self.configs.source_bleaching_size[0] + Nh_bleach = self.configs.source_bleaching_size[1] - # loop for scannig - index0 = int(round(start/dt)) - delta_index = int(round(exposure_time/dt)) + # Bleaching : beam position in pixel-image + Wb, Hb = self.configs.source_bleaching_position + w_b0, h_b0 = Wb-int(Nw_bleach/2), Hb-int(Nh_bleach/2) - # set bleaching index - time_bleaching = self.configs.source_bleaching_time - count_bleaching = int(round(time_bleaching/exposure_time)) + # Bleaching : region in pixel image + bleach_area = numpy.zeros((Nw_pixel, Nh_pixel)) + bleach_area[w_b0:w_b0+Nw_bleach,h_b0:h_b0+Nh_bleach] = 1 + bleach_bool = bleach_area.reshape((Nw_pixel*Nh_pixel)) - while (time[-1] < end) : + # set flux density as a function of radial and depth + r = numpy.linspace(0, 20000, 20001) + d = numpy.linspace(0, 20000, 20001) - count_imaging = int(round(time[0]/exposure_time)) + # loop for scannig + index0 = int(round(start/dt)) + delta_index = int(round(exposure_time/dt)) - print('time : ', time[0], '-', time[-1], ' sec/image (', count_imaging, ')') + # set bleaching index + time_bleaching = self.configs.source_bleaching_time + count_bleaching = int(round(time_bleaching/exposure_time)) - # set frame datasets - index1 = int(round(time[0]/dt)) - index_start = (index1 - index0) - index_end = (index1 - index0) + delta_index + while (time[-1] < end) : - time_index = (time/dt).astype('int') + count_imaging = int(round(time[0]/exposure_time)) - for index in range(index_start, index_end, 1) : + print('time : ', time[0], '-', time[-1], ' sec/image (', count_imaging, ')') - # read input file - csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (index) + # set frame datasets + index1 = int(round(time[0]/dt)) + index_start = (index1 - index0) + index_end = (index1 - index0) + delta_index - csv_list = list(csv.reader(open(csv_file_path, 'r'))) - dataset = numpy.array(csv_list) + time_index = (time/dt).astype('int') - # set imaging-area - time_bool = (time_index == index).astype('int') - imaging = (time_bool*cell_bool).astype('int') - len_imaging = len(imaging[imaging > 0]) + for index in range(index_start, index_end, 1) : - print(index, index*dt+start, 'length :', len_imaging) + # read input file + csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (index) - # set photo-bleaching-area - non_bleached_area = (time_bool*cell_bool*(1 - bleach_bool)).astype('int') - bleached_area = (time_bool*cell_bool*bleach_bool).astype('int') + csv_list = list(csv.reader(open(csv_file_path, 'r'))) + dataset = numpy.array(csv_list) - if (len_imaging > 0) : + # set imaging-area + time_bool = (time_index == index).astype('int') + imaging = (time_bool*cell_bool).astype('int') + len_imaging = len(imaging[imaging > 0]) - # loop for particles - for j, data_j in enumerate(dataset) : + print(index, index*dt+start, 'length :', len_imaging) - # set particle position - c_id = (float(data_j[1]), float(data_j[2]), float(data_j[3])) - x_i, y_i, z_i = self.get_coordinate(c_id) + # set photo-bleaching-area + non_bleached_area = (time_bool*cell_bool*(1 - bleach_bool)).astype('int') + bleached_area = (time_bool*cell_bool*bleach_bool).astype('int') - # beam axial position - """ Distance between beam depth and fluorophore (depth) """ - dX = int(abs(X_b - x_i)) - depth = dX if dX < d[-1] else d[-1] + if (len_imaging > 0) : - # beam lateral position - """ Distance between beam position and fluorophore (plane) """ - dR = numpy.sqrt((Y_b - y_i)**2 + (Z_b - z_i)**2) - dR_array = dR.reshape((Nw_pixel*Nh_pixel)) + # loop for particles + for j, data_j in enumerate(dataset) : - # Photobleaching - if (count_imaging == count_bleaching) : - # set flux density as a function of radial and depth (at Non-bleached area) - R0 = (dR_array*non_bleached_area).astype('int') - radius0 = R0[R0 > 0] + # set particle position + c_id = (float(data_j[1]), float(data_j[2]), float(data_j[3])) + x_i, y_i, z_i = self.get_coordinate(c_id) - func_emit0 = interp1d(r, N_emit0[depth], bounds_error=False, fill_value=N_emit0[depth][r[-1]]) - N_emit = func_emit0(radius0) + # beam axial position + """ Distance between beam depth and fluorophore (depth) """ + dX = int(abs(X_b - x_i)) + depth = dX if dX < d[-1] else d[-1] - # set flux density as a function of radial and depth (at Bleached area) - R1 = (dR_array*bleached_area).astype('int') - radius1 = R1[R1 > 0] + # beam lateral position + """ Distance between beam position and fluorophore (plane) """ + dR = numpy.sqrt((Y_b - y_i)**2 + (Z_b - z_i)**2) + dR_array = dR.reshape((Nw_pixel*Nh_pixel)) - func_emit1 = interp1d(r, N_emit1[depth], bounds_error=False, fill_value=N_emit1[depth][r[-1]]) - N_emit_bleaching = func_emit1(radius1) + # Photobleaching + if (count_imaging == count_bleaching) : + # set flux density as a function of radial and depth (at Non-bleached area) + R0 = (dR_array*non_bleached_area).astype('int') + radius0 = R0[R0 > 0] - # the number of emitted photons - N_emit_sum = N_emit.sum() + N_emit_bleaching.sum() + func_emit0 = interp1d(r, N_emit0[depth], bounds_error=False, fill_value=N_emit0[depth][r[-1]]) + N_emit = func_emit0(radius0) - # Imaging - else : - # set flux density as a function of radial and depth (at Non-bleached area) - R0 = (dR_array*imaging).astype('int') - radius0 = R0[R0 > 0] + # set flux density as a function of radial and depth (at Bleached area) + R1 = (dR_array*bleached_area).astype('int') + radius1 = R1[R1 > 0] - func_emit0 = interp1d(r, N_emit0[depth], bounds_error=False, fill_value=N_emit0[depth][r[-1]]) - N_emit = func_emit0(radius0) + func_emit1 = interp1d(r, N_emit1[depth], bounds_error=False, fill_value=N_emit1[depth][r[-1]]) + N_emit_bleaching = func_emit1(radius1) - # the number of emitted photons - N_emit_sum = N_emit.sum() + # the number of emitted photons + N_emit_sum = N_emit.sum() + N_emit_bleaching.sum() - scan_counter[j] += (N_emit_sum/N_emit0[0][0]) + # Imaging + else : + # set flux density as a function of radial and depth (at Non-bleached area) + R0 = (dR_array*imaging).astype('int') + radius0 = R0[R0 > 0] - if (int(scan_counter[j]) < len(state[j,:])) : - state_j = state[j,int(scan_counter[j])] - else : - state_j = state[j,-1] + func_emit0 = interp1d(r, N_emit0[depth], bounds_error=False, fill_value=N_emit0[depth][r[-1]]) + N_emit = func_emit0(radius0) - if (budget[j] > N_emit_sum) : - budget[j] = budget[j] - state_j*N_emit_sum - else : - budget[j] = 0 + # the number of emitted photons + N_emit_sum = N_emit.sum() - scan_state[j,count_imaging-int(start/dt)] = state_j + scan_counter[j] += (N_emit_sum/N_emit0[0][0]) + if (int(scan_counter[j]) < len(state[j,:])) : + state_j = state[j,int(scan_counter[j])] + else : + state_j = state[j,-1] - state_stack = numpy.column_stack((scan_state[:,count_imaging-int(start/dt)], (budget/N_emit0[0][0]).astype('int'))) - new_dataset = numpy.column_stack((dataset, state_stack)) + if (budget[j] > N_emit_sum) : + budget[j] = budget[j] - state_j*N_emit_sum + else : + budget[j] = 0 - # write output file - output_file = output_file_dir + '/pt-%09d.0.csv' % (index) + scan_state[j,count_imaging-int(start/dt)] = state_j - with open(output_file, 'w') as f : - writer = csv.writer(f) - writer.writerows(new_dataset) - time += exposure_time + state_stack = numpy.column_stack((scan_state[:,count_imaging-int(start/dt)], (budget/N_emit0[0][0]).astype('int'))) + new_dataset = numpy.column_stack((dataset, state_stack)) + # write output file + output_file = output_file_dir + '/pt-%09d.0.csv' % (index) + with open(output_file, 'w') as f : + writer = csv.writer(f) + writer.writerows(new_dataset) + time += exposure_time diff --git a/bioimaging/frap_rewrite_script.py b/bioimaging/frap_rewrite_script.py index 87a4299..3d60509 100755 --- a/bioimaging/frap_rewrite_script.py +++ b/bioimaging/frap_rewrite_script.py @@ -7,56 +7,54 @@ def test_frap(t0, t1) : - # create FRAP imaging - frap = FRAPConfigs() - frap.set_LightSource(source_type='LASER', wave_length=532, imaging_flux=100e-6, bleaching_flux=100000e-3, \ - bleaching_size=(30,30), bleaching_position=(512,512), bleaching_time=1.2, radius=400e-9) - frap.set_Fluorophore(fluorophore_type='EGFP') - frap.set_Pinhole(radius=28.8e-6) - frap.set_Magnification(Mag=60) - - # PMT : Photon-counting mode - frap.set_Detector(detector='PMT', mode="Photon-counting", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ - pixel_length=207.16e-9, scan_time=1.10, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) - frap.set_ADConverter(bit=12, offset=0, fullwell=4096) - -# # PMT : Anlalog mode -# frap.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ -# pixel_length=207.16e-9, scan_time=1.15, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) -# frap.set_ADConverter(bit=12, offset=0, fullwell=4096) - -# # Output data -# frap.set_OutputData(image_file_dir='./numpys_frap_001000A') -# frap.set_OutputData(image_file_dir='./numpys_frap_019656A') - - # Input data - #frap.reset_InputData('/home/masaki/wrk/spatiocyte/models/beads_3D_000100A', start=t0, end=t1, observable="A") - #frap.reset_InputData('/home/masaki/wrk/spatiocyte/models/beads_3D_019656A', start=t0, end=t1, observable="A") - frap.reset_InputData('/home/masaki/wrk/spatiocyte/models/beads_3D_080000A', start=t0, end=t1, observable="A") - - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0) - physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) - physics.set_photobleaching(tau0=100e-6, alpha=0.73) - #physics.set_photoactivation(turn_on_ratio=1000, activation_yield=1.0, frac_preactivation=0.00) - #physics.set_photoblinking(t0_on=30e-6, a_on=0.58, t0_off=10e-6, a_off=0.48) - - # create image - create = FRAPVisualizer(configs=frap, effects=physics) - #create.rewrite_InputData(output_file_dir='./data_frap_000100A_bleach') - #create.rewrite_InputData(output_file_dir='./data_frap_019656A_bleach') - #create.rewrite_InputData(output_file_dir='./data_frap_019656A_blink') - create.rewrite_InputData(output_file_dir='./data_frap_080000A_bleach') - #create.output_frames(num_div=16) + # create FRAP imaging + frap = FRAPConfigs() + frap.set_LightSource(source_type='LASER', wave_length=532, imaging_flux=100e-6, bleaching_flux=100000e-3, \ + bleaching_size=(30,30), bleaching_position=(512,512), bleaching_time=1.2, radius=400e-9) + frap.set_Fluorophore(fluorophore_type='EGFP') + frap.set_Pinhole(radius=28.8e-6) + frap.set_Magnification(Mag=60) + + # PMT : Photon-counting mode + frap.set_Detector(detector='PMT', mode="Photon-counting", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ + pixel_length=207.16e-9, scan_time=1.10, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) + frap.set_ADConverter(bit=12, offset=0, fullwell=4096) + +# # PMT : Anlalog mode +# frap.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ +# pixel_length=207.16e-9, scan_time=1.15, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) +# frap.set_ADConverter(bit=12, offset=0, fullwell=4096) + +# # Output data +# frap.set_OutputData(image_file_dir='./numpys_frap_001000A') +# frap.set_OutputData(image_file_dir='./numpys_frap_019656A') + + # Input data + #frap.reset_InputData('/home/masaki/wrk/spatiocyte/models/beads_3D_000100A', start=t0, end=t1, observable="A") + #frap.reset_InputData('/home/masaki/wrk/spatiocyte/models/beads_3D_019656A', start=t0, end=t1, observable="A") + frap.reset_InputData('/home/masaki/wrk/spatiocyte/models/beads_3D_080000A', start=t0, end=t1, observable="A") + + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0) + physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) + physics.set_photobleaching(tau0=100e-6, alpha=0.73) + #physics.set_photoactivation(turn_on_ratio=1000, activation_yield=1.0, frac_preactivation=0.00) + #physics.set_photoblinking(t0_on=30e-6, a_on=0.58, t0_off=10e-6, a_off=0.48) + + # create image + create = FRAPVisualizer(configs=frap, effects=physics) + #create.rewrite_InputData(output_file_dir='./data_frap_000100A_bleach') + #create.rewrite_InputData(output_file_dir='./data_frap_019656A_bleach') + #create.rewrite_InputData(output_file_dir='./data_frap_019656A_blink') + create.rewrite_InputData(output_file_dir='./data_frap_080000A_bleach') + #create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_frap(t0, t1) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_frap(t0, t1) diff --git a/bioimaging/frap_script.py b/bioimaging/frap_script.py index 2cb1ffd..5bc19c3 100755 --- a/bioimaging/frap_script.py +++ b/bioimaging/frap_script.py @@ -7,58 +7,56 @@ def test_frap(t0, t1) : - # create FRAP imaging - frap = FRAPConfigs() - frap.set_LightSource(source_type='LASER', wave_length=532, imaging_flux=100e-6, bleaching_flux=100000e-3, \ - bleaching_size=(30,30), bleaching_position=(512,512), bleaching_time=1.2, radius=400e-9) - frap.set_Fluorophore(fluorophore_type='EGFP') - frap.set_Pinhole(radius=28.8e-6) - frap.set_Magnification(Mag=60) - - # PMT : Photon-counting mode - frap.set_Detector(detector='PMT', mode="Photon-counting", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ - pixel_length=207.16e-9, scan_time=1.10, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) - frap.set_ADConverter(bit=12, offset=0, fullwell=4096) - -# # PMT : Anlalog mode -# frap.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ -# pixel_length=207.16e-9, scan_time=1.15, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) -# frap.set_ADConverter(bit=12, offset=0, fullwell=4096) - -# # Output data - frap.set_OutputData(image_file_dir='./numpys_frap_080000A_bleach') - #frap.set_OutputData(image_file_dir='./numpys_frap_019656A_bleach') - #frap.set_OutputData(image_file_dir='./numpys_frap_019656A_blink') - #frap.set_OutputData(image_file_dir='./numpys_frap_000100A_bleach') - - # Input data - #frap.set_InputData('/home/masaki/wrk/spatiocyte/models/beads_3D_019656A', start=t0, end=t1, observable="A") - frap.set_InputData('/home/masaki/bioimaging_4public/data_frap_080000A_bleach', start=t0, end=t1, observable="A") - #frap.set_InputData('/home/masaki/bioimaging_4public/data_frap_019656A_bleach', start=t0, end=t1, observable="A") - #frap.set_InputData('/home/masaki/bioimaging_4public/data_frap_019656A_blink', start=t0, end=t1, observable="A") - #frap.set_InputData('/home/masaki/bioimaging_4public/data_frap_000100A_bleach', start=t0, end=t1, observable="A") - - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0) - physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) - physics.set_photobleaching(tau0=1e-3, alpha=0.73) - #physics.set_photoactivation(turn_on_ratio=1000, activation_yield=1.0, frac_preactivation=0.00) - #physics.set_photoblinking(t0_on=30e-6, a_on=0.58, t0_off=10e-6, a_off=0.48) - - # create image - create = FRAPVisualizer(configs=frap, effects=physics) - #create.rewrite_InputData(output_file_dir='./data_frap_19656A_bleach') - #create.rewrite_InputData(output_file_dir='./data_frap_19656A_blink') - create.output_frames(num_div=16) + # create FRAP imaging + frap = FRAPConfigs() + frap.set_LightSource(source_type='LASER', wave_length=532, imaging_flux=100e-6, bleaching_flux=100000e-3, \ + bleaching_size=(30,30), bleaching_position=(512,512), bleaching_time=1.2, radius=400e-9) + frap.set_Fluorophore(fluorophore_type='EGFP') + frap.set_Pinhole(radius=28.8e-6) + frap.set_Magnification(Mag=60) + + # PMT : Photon-counting mode + frap.set_Detector(detector='PMT', mode="Photon-counting", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ + pixel_length=207.16e-9, scan_time=1.10, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) + frap.set_ADConverter(bit=12, offset=0, fullwell=4096) + +# # PMT : Anlalog mode +# frap.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ +# pixel_length=207.16e-9, scan_time=1.15, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) +# frap.set_ADConverter(bit=12, offset=0, fullwell=4096) + +# # Output data + frap.set_OutputData(image_file_dir='./numpys_frap_080000A_bleach') + #frap.set_OutputData(image_file_dir='./numpys_frap_019656A_bleach') + #frap.set_OutputData(image_file_dir='./numpys_frap_019656A_blink') + #frap.set_OutputData(image_file_dir='./numpys_frap_000100A_bleach') + + # Input data + #frap.set_InputData('/home/masaki/wrk/spatiocyte/models/beads_3D_019656A', start=t0, end=t1, observable="A") + frap.set_InputData('/home/masaki/bioimaging_4public/data_frap_080000A_bleach', start=t0, end=t1, observable="A") + #frap.set_InputData('/home/masaki/bioimaging_4public/data_frap_019656A_bleach', start=t0, end=t1, observable="A") + #frap.set_InputData('/home/masaki/bioimaging_4public/data_frap_019656A_blink', start=t0, end=t1, observable="A") + #frap.set_InputData('/home/masaki/bioimaging_4public/data_frap_000100A_bleach', start=t0, end=t1, observable="A") + + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0) + physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) + physics.set_photobleaching(tau0=1e-3, alpha=0.73) + #physics.set_photoactivation(turn_on_ratio=1000, activation_yield=1.0, frac_preactivation=0.00) + #physics.set_photoblinking(t0_on=30e-6, a_on=0.58, t0_off=10e-6, a_off=0.48) + + # create image + create = FRAPVisualizer(configs=frap, effects=physics) + #create.rewrite_InputData(output_file_dir='./data_frap_19656A_bleach') + #create.rewrite_InputData(output_file_dir='./data_frap_19656A_blink') + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_frap(t0, t1) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_frap(t0, t1) diff --git a/bioimaging/hiroshima_configs.py b/bioimaging/hiroshima_configs.py index daf84e3..540247f 100755 --- a/bioimaging/hiroshima_configs.py +++ b/bioimaging/hiroshima_configs.py @@ -2,7 +2,7 @@ import numpy #----------------------------- -# General +# General #----------------------------- ignore_open_errors = False electron_charge = 1.602e-19 # C @@ -27,19 +27,19 @@ #fluorophore_rgb = numpy.array([(0, 0, 0) for j in range(len(depth))]) #----------------------------- -# Fluorophore PSF +# Fluorophore PSF #----------------------------- psf_wavelength = 600 # nm psf_intensity = 1.00 psf_intensity_noise = 0.50 -psf_width = (200, 200) # Gaussian function (radial width, lateral width) [nm] -psf_cutoff = (400, 100) # cutoff range (radius, depth) -psf_file_name_format = 'psf_%04d.png' # Image file name +psf_width = (200, 200) # Gaussian function (radial width, lateral width) [nm] +psf_cutoff = (400, 100) # cutoff range (radius, depth) +psf_file_name_format = 'psf_%04d.png' # Image file name #----------------------------- # Illumination -#----------------------------- -source_switch = False +#----------------------------- +source_switch = False source_type = 'LASER' source_wavelength = 600. # nm source_flux_density = 1 # W/cm2 diff --git a/bioimaging/hiroshima_handler.py b/bioimaging/hiroshima_handler.py index e4de657..e246aa4 100755 --- a/bioimaging/hiroshima_handler.py +++ b/bioimaging/hiroshima_handler.py @@ -30,9 +30,9 @@ class HiroshimaConfigs(EPIFMConfigs) : Hiroshima configration - Low-angle oblique illumination using two incident beams - + - EMCCD camera + Low-angle oblique illumination using two incident beams + + + EMCCD camera ''' def __init__(self, user_configs_dict = None): @@ -62,7 +62,7 @@ def __init__(self, user_configs_dict = None): def set_LightSource(self, source_type = None, - wave_length = None, + wave_length = None, flux_density = None, radius = None, angle = None ) : @@ -88,509 +88,509 @@ def set_LightSource(self, source_type = None, def set_Spatiocyte_data_arrays(self) : - # get spatiocyte file directry - csv_file_directry = self.spatiocyte_file_directry + # get spatiocyte file directry + csv_file_directry = self.spatiocyte_file_directry - # set data-array - data = [] + # set data-array + data = [] - # get count-array - count_array = numpy.array(self.shutter_count_array) + # get count-array + count_array = numpy.array(self.shutter_count_array) - # read lattice file - for i in range(len(count_array)) : + # read lattice file + for i in range(len(count_array)) : + + csv_file_path = csv_file_directry + '/pt-%09d.0.csv' % (count_array[i]) - csv_file_path = csv_file_directry + '/pt-%09d.0.csv' % (count_array[i]) - try : csv_file = open(csv_file_path, 'r') - dataset = [] + dataset = [] - for row in csv.reader(csv_file) : - dataset.append(row) + for row in csv.reader(csv_file) : + dataset.append(row) # get Time - time = float(dataset[0][0]) - - particles = [] - - for data_id in dataset : - # get Coordinate - c_id = (float(data_id[1]), float(data_id[2]), float(data_id[3])) - # get Molecule ID and its state - m_id, s_id = literal_eval(data_id[5]) - # get Fluorophore ID and compartment ID - f_id, l_id = literal_eval(data_id[6]) - - try : - p_state, cyc_id = float(data_id[7]), float(data_id[8]) - - except Exception : - obs = self.spatiocyte_index[int(s_id)] + time = float(dataset[0][0]) + + particles = [] + + for data_id in dataset : + # get Coordinate + c_id = (float(data_id[1]), float(data_id[2]), float(data_id[3])) + # get Molecule ID and its state + m_id, s_id = literal_eval(data_id[5]) + # get Fluorophore ID and compartment ID + f_id, l_id = literal_eval(data_id[6]) + + try : + p_state, cyc_id = float(data_id[7]), float(data_id[8]) + + except Exception : + obs = self.spatiocyte_index[int(s_id)] + + if (obs == 'R1' or obs == 'Rh1' or + obs == 'R2' or obs == 'Rh2' or obs == 'R3') : + p_state, cyc_id = 1.0, float('inf') + elif (obs == 'rR' or obs == 'pR' or obs == 'rRh') : + p_state, cyc_id = 0.5, float('inf') + elif (obs == 'rrR' or obs == 'rRR') : + p_state, cyc_id = 0.33333, float('inf') + else : + p_state, cyc_id = 0.0, float('inf') - if (obs == 'R1' or obs == 'Rh1' or - obs == 'R2' or obs == 'Rh2' or obs == 'R3') : - p_state, cyc_id = 1.0, float('inf') - elif (obs == 'rR' or obs == 'pR' or obs == 'rRh') : - p_state, cyc_id = 0.5, float('inf') - elif (obs == 'rrR' or obs == 'rRR') : - p_state, cyc_id = 0.33333, float('inf') - else : - p_state, cyc_id = 0.0, float('inf') + particles.append((c_id, m_id, s_id, l_id, p_state, cyc_id)) - particles.append((c_id, m_id, s_id, l_id, p_state, cyc_id)) - - data.append([time, particles]) + data.append([time, particles]) except Exception : print('Error : ', csv_file_path, ' not found') - #exit() + #exit() - data.sort(lambda x, y:cmp(x[0], y[0])) + data.sort(lambda x, y:cmp(x[0], y[0])) - # set data + # set data self._set_data('spatiocyte_data', data) -# if len(self.spatiocyte_data) == 0: -# raise VisualizerError('Cannot find spatiocyte_data in any given csv files: ' \ -# + ', '.join(csv_file_directry)) +# if len(self.spatiocyte_data) == 0: +# raise VisualizerError('Cannot find spatiocyte_data in any given csv files: ' \ +# + ', '.join(csv_file_directry)) class HiroshimaVisualizer(EPIFMVisualizer) : - ''' - Hiroshima visualization class - ''' + ''' + Hiroshima visualization class + ''' + + def __init__(self, configs=HiroshimaConfigs(), effects=PhysicalEffects()) : - def __init__(self, configs=HiroshimaConfigs(), effects=PhysicalEffects()) : + assert isinstance(configs, HiroshimaConfigs) + self.configs = configs - assert isinstance(configs, HiroshimaConfigs) - self.configs = configs + assert isinstance(effects, PhysicalEffects) + self.effects = effects - assert isinstance(effects, PhysicalEffects) - self.effects = effects + """ + Check and create the folder for image file. + """ + if not os.path.exists(self.configs.image_file_dir): + os.makedirs(self.configs.image_file_dir) + #else: + # for file in os.listdir(self.configs.movie_image_file_dir): + # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) - """ - Check and create the folder for image file. - """ - if not os.path.exists(self.configs.image_file_dir): - os.makedirs(self.configs.image_file_dir) - #else: - # for file in os.listdir(self.configs.movie_image_file_dir): - # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) + """ + set optical path from source to detector + """ + self.configs.set_Optical_path() - """ - set optical path from source to detector - """ - self.configs.set_Optical_path() + def rewrite_InputFile(self, output_file_dir=None) : - def rewrite_InputFile(self, output_file_dir=None) : + if not os.path.exists(output_file_dir): + os.makedirs(output_file_dir) - if not os.path.exists(output_file_dir): - os.makedirs(output_file_dir) + # get focal point + p_0 = self.get_focal_center() - # get focal point - p_0 = self.get_focal_center() + # beam position : Assuming beam position = focal point (for temporary) + p_b = copy.copy(p_0) - # beam position : Assuming beam position = focal point (for temporary) - p_b = copy.copy(p_0) + # set time, delta and count arrays + time_array = numpy.array(self.configs.shutter_time_array) + delta_array = numpy.array(self.configs.shutter_delta_array) + count_array = numpy.array(self.configs.shutter_count_array) - # set time, delta and count arrays - time_array = numpy.array(self.configs.shutter_time_array) - delta_array = numpy.array(self.configs.shutter_delta_array) - count_array = numpy.array(self.configs.shutter_count_array) + # Snell's law + amplitude0, penet_depth = self.snells_law(p_0, p_0) - # Snell's law - amplitude0, penet_depth = self.snells_law(p_0, p_0) + # get the number of emitted photons + N_emit0 = self.get_emit_photons(amplitude0) - # get the number of emitted photons - N_emit0 = self.get_emit_photons(amplitude0) + # copy input-file + csv_input = self.configs.spatiocyte_file_directry + '/pt-input.csv' + shutil.copyfile(csv_input, output_file_dir + '/pt-input.csv') - # copy input-file - csv_input = self.configs.spatiocyte_file_directry + '/pt-input.csv' - shutil.copyfile(csv_input, output_file_dir + '/pt-input.csv') + # copy shape-file + csv_shape = self.configs.spatiocyte_file_directry + '/pt-shape.csv' + shutil.copyfile(csv_shape, output_file_dir + '/pt-shape.csv') - # copy shape-file - csv_shape = self.configs.spatiocyte_file_directry + '/pt-shape.csv' - shutil.copyfile(csv_shape, output_file_dir + '/pt-shape.csv') + # get the total number of particles + #N_particles = 12824 + N_particles = 13014 - # get the total number of particles - #N_particles = 12824 - N_particles = 13014 + # set molecule-states + self.molecule_states = numpy.zeros(shape=(N_particles)) - # set molecule-states - self.molecule_states = numpy.zeros(shape=(N_particles)) + # set fluorescence + self.effects.set_photophysics_4epifm(time_array, delta_array, N_emit0, N_particles) - # set fluorescence - self.effects.set_photophysics_4epifm(time_array, delta_array, N_emit0, N_particles) + for k in range(len(count_array)) : - for k in range(len(count_array)) : + # read input file + csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (count_array[k]) - # read input file - csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (count_array[k]) + csv_list = list(csv.reader(open(csv_file_path, 'r'))) + dataset = numpy.array(csv_list) - csv_list = list(csv.reader(open(csv_file_path, 'r'))) - dataset = numpy.array(csv_list) + # get molecular-states (unbound-bound) + self.set_molecular_states(k, dataset) - # get molecular-states (unbound-bound) - self.set_molecular_states(k, dataset) + # set photobleaching-dataset arrays + self.set_photobleaching_dataset(k, dataset) - # set photobleaching-dataset arrays - self.set_photobleaching_dataset(k, dataset) + # get new-dataset arrays + new_dataset = self.get_new_dataset(k, N_emit0, dataset) - # get new-dataset arrays - new_dataset = self.get_new_dataset(k, N_emit0, dataset) + # reset arrays for photobleaching-state and photon-budget + self.reset_photobleaching_state(k, N_emit0) - # reset arrays for photobleaching-state and photon-budget - self.reset_photobleaching_state(k, N_emit0) + # write output file + output_file = output_file_dir + '/pt-%09d.0.csv' % (count_array[k]) - # write output file - output_file = output_file_dir + '/pt-%09d.0.csv' % (count_array[k]) + with open(output_file, 'w') as f : + writer = csv.writer(f) + writer.writerows(new_dataset) - with open(output_file, 'w') as f : - writer = csv.writer(f) - writer.writerows(new_dataset) + def set_molecular_states(self, count, dataset) : - def set_molecular_states(self, count, dataset) : + # get focal point + p_0 = self.get_focal_center() - # get focal point - p_0 = self.get_focal_center() + # reset molecule-states + self.molecule_states.fill(0) - # reset molecule-states - self.molecule_states.fill(0) + # set arrays for photobleaching-states and photon-budget + keys = [] + vals = [] - # set arrays for photobleaching-states and photon-budget - keys = [] - vals = [] + # loop for particles + for j, data_j in enumerate(dataset): - # loop for particles - for j, data_j in enumerate(dataset): + # set particle position + p_i = numpy.array(data_j[1:4]).astype('float')/1e-9 - # set particle position - p_i = numpy.array(data_j[1:4]).astype('float')/1e-9 + # Molecule ID and its state + m_id, s_id = literal_eval(data_j[5]) + # Fluorophore ID and compartment ID + f_id, l_id = literal_eval(data_j[6]) - # Molecule ID and its state - m_id, s_id = literal_eval(data_j[5]) - # Fluorophore ID and compartment ID - f_id, l_id = literal_eval(data_j[6]) + # get fluorophre-ID + obs = self.configs.spatiocyte_index[int(s_id)] + self.molecule_states[m_id] = int(s_id) - # get fluorophre-ID - obs = self.configs.spatiocyte_index[int(s_id)] - self.molecule_states[m_id] = int(s_id) + if (obs == 'rR' or obs == 'pR') : + keys.append(m_id) + vals.append(p_i) - if (obs == 'rR' or obs == 'pR') : - keys.append(m_id) - vals.append(p_i) + m_id = numpy.array(keys) + data = numpy.array(vals) - m_id = numpy.array(keys) - data = numpy.array(vals) + # set dict for internal-states + internal_states = {} - # set dict for internal-states - internal_states = {} + for j in range(len(m_id)) : - for j in range(len(m_id)) : + m_id0 = m_id[j] + p_j = data[j] - m_id0 = m_id[j] - p_j = data[j] + m_id_k = m_id[m_id != m_id0] + data_k = data[m_id != m_id0] + p_k = data_k - m_id_k = m_id[m_id != m_id0] - data_k = data[m_id != m_id0] - p_k = data_k + distance = numpy.array(map(lambda x : numpy.sqrt(numpy.sum((x-p_j)**2)), p_k)) + index = distance.argmin() - distance = numpy.array(map(lambda x : numpy.sqrt(numpy.sum((x-p_j)**2)), p_k)) - index = distance.argmin() + m_id1 = m_id_k[index] - m_id1 = m_id_k[index] + if not m_id0 in internal_states : + if not m_id1 in internal_states : - if not m_id0 in internal_states : - if not m_id1 in internal_states : + # get photon-budget + budget0 = self.effects.fluorescence_budget[m_id0] + budget1 = self.effects.fluorescence_budget[m_id1] - # get photon-budget - budget0 = self.effects.fluorescence_budget[m_id0] - budget1 = self.effects.fluorescence_budget[m_id1] - - if (budget0 > 0 and budget1 > 0) : - if (count == 0) : - prob = numpy.random.uniform(0,1,1) + if (budget0 > 0 and budget1 > 0) : + if (count == 0) : + prob = numpy.random.uniform(0,1,1) - if (prob > 0.5) : - internal_states[m_id0] = 1 - internal_states[m_id1] = 0 - else : - internal_states[m_id0] = 0 - internal_states[m_id1] = 1 - - else : - pasts = self.past_internal_states + if (prob > 0.5) : + internal_states[m_id0] = 1 + internal_states[m_id1] = 0 + else : + internal_states[m_id0] = 0 + internal_states[m_id1] = 1 - if m_id0 in pasts : - internal_states[m_id0] = pasts[m_id0] - - if m_id1 in pasts : - internal_states[m_id1] = pasts[m_id1] - else : - internal_states[m_id1] = int(numpy.abs(pasts[m_id0]-1)) - else : - if m_id1 in pasts : - internal_states[m_id1] = pasts[m_id1] - internal_states[m_id0] = int(numpy.abs(pasts[m_id1]-1)) - else : - prob = numpy.random.uniform(0,1,1) - - if (prob > 0.5) : - internal_states[m_id0] = 1 - internal_states[m_id1] = 0 - else : - internal_states[m_id0] = 0 - internal_states[m_id1] = 1 - - elif (budget0 > 0 and budget1 == 0) : - internal_states[m_id0] = 1 - internal_states[m_id1] = 0 - - elif (budget0 == 0 and budget1 > 0) : - internal_states[m_id0] = 0 - internal_states[m_id1] = 1 - - else : - internal_states[m_id0] = 0 - internal_states[m_id1] = 0 - - self.internal_states = internal_states - - - - def reset_photobleaching_state(self, count, N_emit0) : - - state_mo = self.molecule_states - state_in = self.internal_states - - if (count > 0) : - - # initialize the photobleaching-states and photon-budget - # (1) R -> r (shown as GFP) - # (2) rR -> rr (shown as GFP) - # (3) RR -> pR - molecules = set() - - past_mo = self.past_molecule_states - - case_flag = False - - for i in range(len(state_mo)) : - - obs0 = self.configs.spatiocyte_index[int(state_mo[i])] - obs1 = self.configs.spatiocyte_index[int(past_mo[i])] - - if ((obs0 == 'GFP' and obs1 == 'R1') or - (obs0 == 'GFP' and obs1 == 'rR') or - (obs0 == 'GFP' and obs1 == 'rRh') or - (obs0 == 'GFP' and obs1 == 'Rh1')) : - case_flag = True - - if ((obs0 == 'pR' and obs1 == 'R2') or - (obs0 == 'pR' and obs1 == 'Rh2')) : - if (state_in[i] == 0) : - case_flag = True - - if (case_flag == True) : - molecules.add(i) - #print 'reset states :', count, '(', i, ':', obs1, '->', obs0, ')' - - case_flag = False - - molecules = list(molecules) - - # initialize the photobleaching-states and photon-budget - if (len(molecules) > 0) : - - if self.use_multiprocess(): - # set arrays for photobleaching-state and photon-budget - state_pb = {} - budget = {} - - num_processes = min(multiprocessing.cpu_count(), len(molecules)) - n, m = divmod(len(molecules), num_processes) - - chunks = [n + 1 if i < m else n for i in range(num_processes)] - - processes = [] - start_index = 0 - - for chunk in chunks: - stop_index = start_index + chunk - p, c = multiprocessing.Pipe() - process = multiprocessing.Process(target=self.get_state_initialization_process, - args=(molecules[start_index:stop_index], N_emit0, c)) - processes.append((p, process)) - start_index = stop_index - - for _, process in processes: - process.start() - - for pipe, process in processes: - new_state_pb, new_budget = pipe.recv() - state_pb.update(new_state_pb) - budget.update(new_budget) - process.join() - else : - state_pb, budget = self.get_state_initialization(molecules, N_emit0) - - # reset global-arrays for photobleaching-state and photon-budget - for key, value in state_pb.items() : - - self.effects.fluorescence_state[key] = state_pb[key] - self.effects.fluorescence_budget[key] = budget[key] + pasts = self.past_internal_states + + if m_id0 in pasts : + internal_states[m_id0] = pasts[m_id0] + + if m_id1 in pasts : + internal_states[m_id1] = pasts[m_id1] + else : + internal_states[m_id1] = int(numpy.abs(pasts[m_id0]-1)) + else : + if m_id1 in pasts : + internal_states[m_id1] = pasts[m_id1] + internal_states[m_id0] = int(numpy.abs(pasts[m_id1]-1)) + else : + prob = numpy.random.uniform(0,1,1) + + if (prob > 0.5) : + internal_states[m_id0] = 1 + internal_states[m_id1] = 0 + else : + internal_states[m_id0] = 0 + internal_states[m_id1] = 1 + + elif (budget0 > 0 and budget1 == 0) : + internal_states[m_id0] = 1 + internal_states[m_id1] = 0 + + elif (budget0 == 0 and budget1 > 0) : + internal_states[m_id0] = 0 + internal_states[m_id1] = 1 + + else : + internal_states[m_id0] = 0 + internal_states[m_id1] = 0 + + self.internal_states = internal_states + + + + def reset_photobleaching_state(self, count, N_emit0) : + + state_mo = self.molecule_states + state_in = self.internal_states + + if (count > 0) : + + # initialize the photobleaching-states and photon-budget + # (1) R -> r (shown as GFP) + # (2) rR -> rr (shown as GFP) + # (3) RR -> pR + molecules = set() - # set past-states - self.past_molecule_states = copy.deepcopy(state_mo) - self.past_internal_states = copy.deepcopy(state_in) + past_mo = self.past_molecule_states + case_flag = False + for i in range(len(state_mo)) : - def get_state_initialization(self, molecules, N_emit0) : + obs0 = self.configs.spatiocyte_index[int(state_mo[i])] + obs1 = self.configs.spatiocyte_index[int(past_mo[i])] - state_pb = self.effects.fluorescence_state - budget = self.effects.fluorescence_budget + if ((obs0 == 'GFP' and obs1 == 'R1') or + (obs0 == 'GFP' and obs1 == 'rR') or + (obs0 == 'GFP' and obs1 == 'rRh') or + (obs0 == 'GFP' and obs1 == 'Rh1')) : + case_flag = True - # get delta-time array - delta = numpy.array(self.configs.shutter_delta_array) + if ((obs0 == 'pR' and obs1 == 'R2') or + (obs0 == 'pR' and obs1 == 'Rh2')) : + if (state_in[i] == 0) : + case_flag = True - # set dicts for resetting states and photon-budget - result_state_pb = {} - result_budget = {} + if (case_flag == True) : + molecules.add(i) + #print 'reset states :', count, '(', i, ':', obs1, '->', obs0, ')' - for i in molecules : + case_flag = False - # reset photobleaching-time and photon-budget - tau_i, budget_i = self.effects.get_photobleaching_property(delta[0], N_emit0) + molecules = list(molecules) - # reset the photobleaching-state - state_pb_i = numpy.zeros(shape=(len(state_pb[i]))) + # initialize the photobleaching-states and photon-budget + if (len(molecules) > 0) : - Ni = (numpy.abs(numpy.cumsum(delta) - tau_i)).argmin() - state_pb_i[0:Ni] = numpy.ones(shape=(Ni)) + if self.use_multiprocess(): + # set arrays for photobleaching-state and photon-budget + state_pb = {} + budget = {} - result_state_pb[i] = state_pb_i - result_budget[i] = budget_i + num_processes = min(multiprocessing.cpu_count(), len(molecules)) + n, m = divmod(len(molecules), num_processes) - return result_state_pb, result_budget + chunks = [n + 1 if i < m else n for i in range(num_processes)] + processes = [] + start_index = 0 + for chunk in chunks: + stop_index = start_index + chunk + p, c = multiprocessing.Pipe() + process = multiprocessing.Process(target=self.get_state_initialization_process, + args=(molecules[start_index:stop_index], N_emit0, c)) + processes.append((p, process)) + start_index = stop_index - def get_state_initialization_process(self, molecules, N_emit0, pipe) : + for _, process in processes: + process.start() - pipe.send(self.get_state_initialization(molecules, N_emit0)) + for pipe, process in processes: + new_state_pb, new_budget = pipe.recv() + state_pb.update(new_state_pb) + budget.update(new_budget) + process.join() + else : + state_pb, budget = self.get_state_initialization(molecules, N_emit0) + # reset global-arrays for photobleaching-state and photon-budget + for key, value in state_pb.items() : - def get_photobleaching_dataset(self, count, dataset) : + self.effects.fluorescence_state[key] = state_pb[key] + self.effects.fluorescence_budget[key] = budget[key] - # get focal point - p_0 = self.get_focal_center() + # set past-states + self.past_molecule_states = copy.deepcopy(state_mo) + self.past_internal_states = copy.deepcopy(state_in) - # set arrays for photobleaching-states and photon-budget - result_state_pb = {} - result_budget = {} - # loop for particles - for j, data_j in enumerate(dataset): - # set particle position - p_i = numpy.array(data_j[1:4]).astype('float')/1e-9 + def get_state_initialization(self, molecules, N_emit0) : - # Snell's law - amplitude, penet_depth = self.snells_law(p_i, p_0) + state_pb = self.effects.fluorescence_state + budget = self.effects.fluorescence_budget - # particle coordinate in real(nm) scale - p_i, radial, depth = self.get_coordinate(p_i, p_0) + # get delta-time array + delta = numpy.array(self.configs.shutter_delta_array) - # Molecule ID and its state - m_id, s_id = literal_eval(data_j[5]) - # Fluorophore ID and compartment ID - f_id, l_id = literal_eval(data_j[6]) + # set dicts for resetting states and photon-budget + result_state_pb = {} + result_budget = {} - # get molecule-state - obs = self.configs.spatiocyte_index[int(s_id)] - - if (obs == 'R1' or obs == 'Rh1' or - obs == 'R2' or obs == 'Rh2' or obs == 'R3') : - state_j = 1 + for i in molecules : - elif (obs == 'rR' or obs == 'pR' or obs == 'rRh') : - state_j = self.internal_states[m_id] - else : - state_j = 0 + # reset photobleaching-time and photon-budget + tau_i, budget_i = self.effects.get_photobleaching_property(delta[0], N_emit0) - # get exponential amplitude (only for observation at basal surface) - #amplitide = amplitude*numpy.exp(-depth/pent_depth) + # reset the photobleaching-state + state_pb_i = numpy.zeros(shape=(len(state_pb[i]))) - # get the number of emitted photons - N_emit = self.get_emit_photons(amplitude) + Ni = (numpy.abs(numpy.cumsum(delta) - tau_i)).argmin() + state_pb_i[0:Ni] = numpy.ones(shape=(Ni)) - # get global-arrays for photobleaching-state and photon-budget - state_pb = self.effects.fluorescence_state[m_id,count] - budget = self.effects.fluorescence_budget[m_id] + result_state_pb[i] = state_pb_i + result_budget[i] = budget_i - # reset photon-budget - photons = budget - N_emit*state_j + return result_state_pb, result_budget - if (photons > 0) : - budget = photons - state_pb = state_j - else : - budget = 0 - state_pb = 0 - result_state_pb[m_id] = state_pb - result_budget[m_id] = budget - return result_state_pb, result_budget + def get_state_initialization_process(self, molecules, N_emit0, pipe) : + pipe.send(self.get_state_initialization(molecules, N_emit0)) -# def get_molecule_plane(self, cell, data, p_b, p_0) : + + def get_photobleaching_dataset(self, count, dataset) : + + # get focal point + p_0 = self.get_focal_center() + + # set arrays for photobleaching-states and photon-budget + result_state_pb = {} + result_budget = {} + + # loop for particles + for j, data_j in enumerate(dataset): + + # set particle position + p_i = numpy.array(data_j[1:4]).astype('float')/1e-9 + + # Snell's law + amplitude, penet_depth = self.snells_law(p_i, p_0) + + # particle coordinate in real(nm) scale + p_i, radial, depth = self.get_coordinate(p_i, p_0) + + # Molecule ID and its state + m_id, s_id = literal_eval(data_j[5]) + # Fluorophore ID and compartment ID + f_id, l_id = literal_eval(data_j[6]) + + # get molecule-state + obs = self.configs.spatiocyte_index[int(s_id)] + + if (obs == 'R1' or obs == 'Rh1' or + obs == 'R2' or obs == 'Rh2' or obs == 'R3') : + state_j = 1 + + elif (obs == 'rR' or obs == 'pR' or obs == 'rRh') : + state_j = self.internal_states[m_id] + else : + state_j = 0 + + # get exponential amplitude (only for observation at basal surface) + #amplitide = amplitude*numpy.exp(-depth/pent_depth) + + # get the number of emitted photons + N_emit = self.get_emit_photons(amplitude) + + # get global-arrays for photobleaching-state and photon-budget + state_pb = self.effects.fluorescence_state[m_id,count] + budget = self.effects.fluorescence_budget[m_id] + + # reset photon-budget + photons = budget - N_emit*state_j + + if (photons > 0) : + budget = photons + state_pb = state_j + else : + budget = 0 + state_pb = 0 + + result_state_pb[m_id] = state_pb + result_budget[m_id] = budget + + return result_state_pb, result_budget + + + +# def get_molecule_plane(self, cell, data, p_b, p_0) : # -# # particles coordinate, species and lattice IDs +# # particles coordinate, species and lattice IDs # c_id, s_id, l_id, p_state, cyc_id = data -# -# if self.configs.spatiocyte_observables[s_index] is True : # -# if (p_state > 0) : +# if self.configs.spatiocyte_observables[s_index] is True : +# +# if (p_state > 0) : # -# p_i = numpy.array(c_id)/1e-9 +# p_i = numpy.array(c_id)/1e-9 # -# # Snell's law -# amplitude, penet_depth = self.snells_law(p_i, p_0) +# # Snell's law +# amplitude, penet_depth = self.snells_law(p_i, p_0) # -# #angle_left = self.configs.source_angle_left -# #angle_right = self.configs.source_angle_right -# #A2_left, penet_depth_left = self.snells_law(p_i, p_0, angle_left) -# #A2_right, penet_depth_right = self.snells_law(p_i, p_0, angle_right) -# #amplitude = A2_left + A2_right +# #angle_left = self.configs.source_angle_left +# #angle_right = self.configs.source_angle_right +# #A2_left, penet_depth_left = self.snells_law(p_i, p_0, angle_left) +# #A2_right, penet_depth_right = self.snells_law(p_i, p_0, angle_right) +# #amplitude = A2_left + A2_right # -# # particles coordinate in real(nm) scale +# # particles coordinate in real(nm) scale # p_i, radial, depth = self.get_coordinate(p_i, p_0) # -# # reset p_state -# obs = self.configs.spatiocyte_index[int(s_id)] +# # reset p_state +# obs = self.configs.spatiocyte_index[int(s_id)] # -# if (obs == 'R1' or obs == 'Rh1' or -# obs == 'R2' or obs == 'Rh2') : -# p_state = 1.0 -# elif (obs == 'rR' or obs == 'pR') : -# p_state = 0.5 -# else : -# p_state = 0.0 +# if (obs == 'R1' or obs == 'Rh1' or +# obs == 'R2' or obs == 'Rh2') : +# p_state = 1.0 +# elif (obs == 'rR' or obs == 'pR') : +# p_state = 0.5 +# else : +# p_state = 0.0 # # # get signal matrix # signal = self.get_signal(amplitude, radial, depth, p_state) diff --git a/bioimaging/hiroshima_rewrite_script.py b/bioimaging/hiroshima_rewrite_script.py index 298a816..b266ce0 100755 --- a/bioimaging/hiroshima_rewrite_script.py +++ b/bioimaging/hiroshima_rewrite_script.py @@ -7,51 +7,49 @@ def test_hiroshima(t0, t1, cell) : - # create Hiroshima imaging - hiroshima = HiroshimaConfigs() - hiroshima.set_Shutter(start_time=t0, end_time=t1) - hiroshima.set_LightSource(source_type='LASER', wave_length=532, flux_density=50, angle=60) - hiroshima.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)', normalization=1.0) - hiroshima.set_DichroicMirror('FF562-Di03-25x36') - hiroshima.set_Magnification(Mag=241) - - # Detector : EMCCD Camera - hiroshima.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, exposure_time=0.050, \ - focal_point=(0.92,0.5,0.5), QE=0.92, readout_noise=100, emgain=300) - hiroshima.set_ADConverter(bit=16, offset=2000, fullwell=800000) - - # Output data - #hiroshima.set_OutputData(image_file_dir='./numpys_') - - # Input data - #csv_dir = '/home/masaki/wrk/spatiocyte/models/Hiroshima/data/SM_data/TestCells/Cell_00' - csv_dir = '/home/masaki/wrk/spatiocyte/models/Hiroshima/data/SM_data/ModelCells/Dimer/config_B/Cell_%02d/2000pM' % (cell) - hiroshima.set_InputFile(csv_dir, observable="R") - hiroshima.set_ShapeFile(csv_dir) - - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0.01) - #physics.set_crosstalk(width=0.70) - physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) - physics.set_photobleaching(tau0=2.27, alpha=0.73) - - # create image - create = HiroshimaVisualizer(configs=hiroshima, effects=physics) - #output_file_dir = './data_hiroshima_for_calibration/data_pb/TestCell_00_tirfm' - #output_file_dir = './data_hiroshima_for_calibration/data_pb/TestCell_00_epifm' - output_file_dir = './data_hiroshima_for_calibration/data_pb/ModelCells/Dimer/Cell_%02d/2000pM' % (cell) - create.rewrite_InputFile(output_file_dir=output_file_dir) - #create.output_frames(num_div=16) + # create Hiroshima imaging + hiroshima = HiroshimaConfigs() + hiroshima.set_Shutter(start_time=t0, end_time=t1) + hiroshima.set_LightSource(source_type='LASER', wave_length=532, flux_density=50, angle=60) + hiroshima.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)', normalization=1.0) + hiroshima.set_DichroicMirror('FF562-Di03-25x36') + hiroshima.set_Magnification(Mag=241) + + # Detector : EMCCD Camera + hiroshima.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, exposure_time=0.050, \ + focal_point=(0.92,0.5,0.5), QE=0.92, readout_noise=100, emgain=300) + hiroshima.set_ADConverter(bit=16, offset=2000, fullwell=800000) + + # Output data + #hiroshima.set_OutputData(image_file_dir='./numpys_') + + # Input data + #csv_dir = '/home/masaki/wrk/spatiocyte/models/Hiroshima/data/SM_data/TestCells/Cell_00' + csv_dir = '/home/masaki/wrk/spatiocyte/models/Hiroshima/data/SM_data/ModelCells/Dimer/config_B/Cell_%02d/2000pM' % (cell) + hiroshima.set_InputFile(csv_dir, observable="R") + hiroshima.set_ShapeFile(csv_dir) + + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0.01) + #physics.set_crosstalk(width=0.70) + physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) + physics.set_photobleaching(tau0=2.27, alpha=0.73) + + # create image + create = HiroshimaVisualizer(configs=hiroshima, effects=physics) + #output_file_dir = './data_hiroshima_for_calibration/data_pb/TestCell_00_tirfm' + #output_file_dir = './data_hiroshima_for_calibration/data_pb/TestCell_00_epifm' + output_file_dir = './data_hiroshima_for_calibration/data_pb/ModelCells/Dimer/Cell_%02d/2000pM' % (cell) + create.rewrite_InputFile(output_file_dir=output_file_dir) + #create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - cell = int(sys.argv[3]) - - test_hiroshima(t0, t1, cell) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + cell = int(sys.argv[3]) + test_hiroshima(t0, t1, cell) diff --git a/bioimaging/hiroshima_script.py b/bioimaging/hiroshima_script.py index 9c96e6f..06200f3 100755 --- a/bioimaging/hiroshima_script.py +++ b/bioimaging/hiroshima_script.py @@ -7,60 +7,58 @@ def test_hiroshima(t0, t1, cell, nM) : - # create Hiroshima imaging - hiroshima = HiroshimaConfigs() - hiroshima.set_Shutter(start_time=t0, end_time=t1) - #hiroshima.set_Shutter(start_time=t0, end_time=t1, time_open=0.150, time_lapse=20) - hiroshima.set_LightSource(source_type='LASER', wave_length=532, flux_density=50, angle=60) - hiroshima.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)', normalization=1.0) - hiroshima.set_DichroicMirror('FF562-Di03-25x36') - hiroshima.set_Magnification(Mag=241) + # create Hiroshima imaging + hiroshima = HiroshimaConfigs() + hiroshima.set_Shutter(start_time=t0, end_time=t1) + #hiroshima.set_Shutter(start_time=t0, end_time=t1, time_open=0.150, time_lapse=20) + hiroshima.set_LightSource(source_type='LASER', wave_length=532, flux_density=50, angle=60) + hiroshima.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)', normalization=1.0) + hiroshima.set_DichroicMirror('FF562-Di03-25x36') + hiroshima.set_Magnification(Mag=241) - # Detector : EMCCD Camera - hiroshima.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, exposure_time=0.150, \ - focal_point=(0.92,0.5,0.5), QE=0.92, readout_noise=100, emgain=300) - hiroshima.set_ADConverter(bit=16, offset=2000, fullwell=800000) + # Detector : EMCCD Camera + hiroshima.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, exposure_time=0.150, \ + focal_point=(0.92,0.5,0.5), QE=0.92, readout_noise=100, emgain=300) + hiroshima.set_ADConverter(bit=16, offset=2000, fullwell=800000) - # Output data - #hiroshima.set_OutputData(image_file_dir='./data_hiroshima_for_calibration/numpys/numpys_TestCell_00_epifm') - #hiroshima.set_OutputData(image_file_dir='./data_hiroshima_for_calibration/numpys/numpys_TestCell_00_tirfm') - #hiroshima.set_OutputData(image_file_dir='./data_hiroshima_for_calibration/numpys/ModelCells/Dimer/config_A/Cell_%02d/%04dpM' % (cell, 1000*nM)) - #hiroshima.set_OutputData(image_file_dir='./data_hiroshima_for_calibration/numpys/ModelCells/Dimer/config_B/Cell_%02d/%04dpM_168bg' % (cell, 1000*nM)) - #hiroshima.set_OutputData(image_file_dir='./data_hiroshima_for_calibration/numpys/ModelCells/Dimer/config_C/Cell_%02d/%04dpM' % (cell, 1000*nM)) - hiroshima.set_OutputData(image_file_dir='./data_hiroshima_for_calibration/numpys/ModelCells/Dimer/config_D/Cell_%03d/%04dpM' % (cell, 1000*nM)) - #hiroshima.set_OutputData(image_file_dir='./data_hiroshima_for_calibration/numpys/ModelCells/Monomer/Cell_%03d/%07dpM' % (cell, 1000*nM)) - #hiroshima.set_OutputData(image_file_dir='./data_hiroshima_for_calibration/numpys/ModelCells/Monomer/TestCell_%03d/%07dpM' % (cell, 1000*nM)) + # Output data + #hiroshima.set_OutputData(image_file_dir='./data_hiroshima_for_calibration/numpys/numpys_TestCell_00_epifm') + #hiroshima.set_OutputData(image_file_dir='./data_hiroshima_for_calibration/numpys/numpys_TestCell_00_tirfm') + #hiroshima.set_OutputData(image_file_dir='./data_hiroshima_for_calibration/numpys/ModelCells/Dimer/config_A/Cell_%02d/%04dpM' % (cell, 1000*nM)) + #hiroshima.set_OutputData(image_file_dir='./data_hiroshima_for_calibration/numpys/ModelCells/Dimer/config_B/Cell_%02d/%04dpM_168bg' % (cell, 1000*nM)) + #hiroshima.set_OutputData(image_file_dir='./data_hiroshima_for_calibration/numpys/ModelCells/Dimer/config_C/Cell_%02d/%04dpM' % (cell, 1000*nM)) + hiroshima.set_OutputData(image_file_dir='./data_hiroshima_for_calibration/numpys/ModelCells/Dimer/config_D/Cell_%03d/%04dpM' % (cell, 1000*nM)) + #hiroshima.set_OutputData(image_file_dir='./data_hiroshima_for_calibration/numpys/ModelCells/Monomer/Cell_%03d/%07dpM' % (cell, 1000*nM)) + #hiroshima.set_OutputData(image_file_dir='./data_hiroshima_for_calibration/numpys/ModelCells/Monomer/TestCell_%03d/%07dpM' % (cell, 1000*nM)) - # Input data - #csv_dir = './data_hiroshima_for_calibration/data_pb/TestCell_00_epifm' - #csv_dir = './data_hiroshima_for_calibration/data_pb/TestCell_00_tirfm' - #csv_dir = '/home/masaki/wrk/spatiocyte/models/Hiroshima/data/SM_data/ModelCells/Dimer/Cell_%02d/%04dpM' % (cell, 1000*nM) - csv_dir = '/home/masaki/wrk/spatiocyte/models/Hiroshima/data/SM_data/ModelCells/Dimer/config_D/Cell_%03d/%04dpM' % (cell, 1000*nM) - #csv_dir = '/home/masaki/wrk/spatiocyte/models/Hiroshima/data/SM_data/ModelCells/Monomer/Cell_%03d/%07dpM' % (cell, 1000*nM) - #csv_dir = './data_hiroshima_for_calibration/data_pb/ModelCells/Dimer/Cell_%02d/%04dpM' % (cell, 1000*nM) - hiroshima.set_InputFile(csv_dir, observable="R") - hiroshima.set_ShapeFile(csv_dir) + # Input data + #csv_dir = './data_hiroshima_for_calibration/data_pb/TestCell_00_epifm' + #csv_dir = './data_hiroshima_for_calibration/data_pb/TestCell_00_tirfm' + #csv_dir = '/home/masaki/wrk/spatiocyte/models/Hiroshima/data/SM_data/ModelCells/Dimer/Cell_%02d/%04dpM' % (cell, 1000*nM) + csv_dir = '/home/masaki/wrk/spatiocyte/models/Hiroshima/data/SM_data/ModelCells/Dimer/config_D/Cell_%03d/%04dpM' % (cell, 1000*nM) + #csv_dir = '/home/masaki/wrk/spatiocyte/models/Hiroshima/data/SM_data/ModelCells/Monomer/Cell_%03d/%07dpM' % (cell, 1000*nM) + #csv_dir = './data_hiroshima_for_calibration/data_pb/ModelCells/Dimer/Cell_%02d/%04dpM' % (cell, 1000*nM) + hiroshima.set_InputFile(csv_dir, observable="R") + hiroshima.set_ShapeFile(csv_dir) - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0.01) - #physics.set_background(mean=1.68) - physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) - physics.set_photobleaching(tau0=2.27, alpha=0.73) + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0.01) + #physics.set_background(mean=1.68) + physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) + physics.set_photobleaching(tau0=2.27, alpha=0.73) - # create image - create = HiroshimaVisualizer(configs=hiroshima, effects=physics) - create.output_frames(num_div=16) + # create image + create = HiroshimaVisualizer(configs=hiroshima, effects=physics) + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - cell = int(sys.argv[3]) - nM = float(sys.argv[4]) - - test_hiroshima(t0, t1, cell, nM) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + cell = int(sys.argv[3]) + nM = float(sys.argv[4]) + test_hiroshima(t0, t1, cell, nM) diff --git a/bioimaging/invitro_lscm_script.py b/bioimaging/invitro_lscm_script.py index a695f8f..7383b19 100755 --- a/bioimaging/invitro_lscm_script.py +++ b/bioimaging/invitro_lscm_script.py @@ -7,43 +7,42 @@ def confm_test(t0, t1) : - # create Point-scanning Confocal Microscopy - confm = PointScanConfocalConfigs() + # create Point-scanning Confocal Microscopy + confm = PointScanConfocalConfigs() - confm.set_LightSource(source_type='LASER', wave_length=488, flux=100e-6, radius=200e-9) - confm.set_Fluorophore(fluorophore_type='EGFP') - confm.set_Pinhole(radius=28.8e-6) - confm.set_Magnification(Mag=60) + confm.set_LightSource(source_type='LASER', wave_length=488, flux=100e-6, radius=200e-9) + confm.set_Fluorophore(fluorophore_type='EGFP') + confm.set_Pinhole(radius=28.8e-6) + confm.set_Magnification(Mag=60) - # PMT : Analog-mode - confm.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.3,0.5,0.5), \ - pixel_length=207.16e-9, scan_time=1.15, QE=0.3, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) - confm.set_ADConverter(bit=16, offset=0, fullwell=65536) - #confm.set_OutputData(image_file_dir='./images_erk102_invitro_01') - #confm.set_OutputData(image_file_dir='./images_erk102_invitro_1000nM') - confm.set_OutputData(image_file_dir='./images_EGF_model_3_coord_erk_0_0100') + # PMT : Analog-mode + confm.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.3,0.5,0.5), \ + pixel_length=207.16e-9, scan_time=1.15, QE=0.3, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) + confm.set_ADConverter(bit=16, offset=0, fullwell=65536) + #confm.set_OutputData(image_file_dir='./images_erk102_invitro_01') + #confm.set_OutputData(image_file_dir='./images_erk102_invitro_1000nM') + confm.set_OutputData(image_file_dir='./images_EGF_model_3_coord_erk_0_0100') - # Input data : EGF model file - #confm.set_InputData('/home/masaki/ecell3/latest/data/csv/invitro_01', start=t0, end=t1, observable="A") - #confm.set_InputData('/home/masaki/ecell3/latest/data/csv/erk102_invitro_1000nM', start=t0, end=t1, observable="A") - confm.set_InputData('/home/masaki/ecell3/latest/data/csv/EGF_model_3_coord_erk_0_0100', start=t0, end=t1, observable="ERK") + # Input data : EGF model file + #confm.set_InputData('/home/masaki/ecell3/latest/data/csv/invitro_01', start=t0, end=t1, observable="A") + #confm.set_InputData('/home/masaki/ecell3/latest/data/csv/erk102_invitro_1000nM', start=t0, end=t1, observable="A") + confm.set_InputData('/home/masaki/ecell3/latest/data/csv/EGF_model_3_coord_erk_0_0100', start=t0, end=t1, observable="ERK") - # create physical effects - physics = PhysicalEffects() - physics.set_Conversion(ratio=1e-6) - #physics.set_Background(mean=5) - #physics.set_DetectorCrosstalk(width=1.00) + # create physical effects + physics = PhysicalEffects() + physics.set_Conversion(ratio=1e-6) + #physics.set_Background(mean=5) + #physics.set_DetectorCrosstalk(width=1.00) - # create image and movie - create = PointScanConfocalVisualizer(configs=confm, effects=physics) - create.output_frames(num_div=16) + # create image and movie + create = PointScanConfocalVisualizer(configs=confm, effects=physics) + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - confm_test(t0, t1) + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + confm_test(t0, t1) diff --git a/bioimaging/kaizu_rewrite_script.py b/bioimaging/kaizu_rewrite_script.py index cccd0b9..41f67b7 100755 --- a/bioimaging/kaizu_rewrite_script.py +++ b/bioimaging/kaizu_rewrite_script.py @@ -7,43 +7,41 @@ def test_epifm(t0, t1) : - # create EPIFM imaging - epifm = EPIFMConfigs() - epifm.set_Shutter(start_time=t0, end_time=t1) - epifm.set_LightSource(source_type='LASER', wave_length=532, flux_density=20, angle=72) - epifm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)', normalization=1.0) - epifm.set_DichroicMirror('FF562-Di03-25x36') - epifm.set_Magnification(Mag=364) - - # Detector : EMCCD Camera - epifm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, exposure_time=0.150, \ - focal_point=(0.00,0.5,0.5), QE=0.92, readout_noise=100, emgain=300) - epifm.set_ADConverter(bit=16, offset=2000, fullwell=800000) - - # Input data - #csv_dir = '/home/masaki/wrk/spatiocyte/models/Kaizu/data/ST' - csv_dir = '/home/masaki/wrk/spatiocyte/models/Kaizu/data/S2' - epifm.set_InputFile(csv_dir, observable="S") - epifm.set_ShapeFile(csv_dir) - - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0.01) - physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) - physics.set_photobleaching(tau0=2.27, alpha=0.73) - - # create image - create = EPIFMVisualizer(configs=epifm, effects=physics) - #create.rewrite_InputFile(output_file_dir='./data_kaizu/data_pb/ST_120ms') - create.rewrite_InputFile(output_file_dir='./data_kaizu/data_pb/S2_150ms') + # create EPIFM imaging + epifm = EPIFMConfigs() + epifm.set_Shutter(start_time=t0, end_time=t1) + epifm.set_LightSource(source_type='LASER', wave_length=532, flux_density=20, angle=72) + epifm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)', normalization=1.0) + epifm.set_DichroicMirror('FF562-Di03-25x36') + epifm.set_Magnification(Mag=364) + + # Detector : EMCCD Camera + epifm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, exposure_time=0.150, \ + focal_point=(0.00,0.5,0.5), QE=0.92, readout_noise=100, emgain=300) + epifm.set_ADConverter(bit=16, offset=2000, fullwell=800000) + + # Input data + #csv_dir = '/home/masaki/wrk/spatiocyte/models/Kaizu/data/ST' + csv_dir = '/home/masaki/wrk/spatiocyte/models/Kaizu/data/S2' + epifm.set_InputFile(csv_dir, observable="S") + epifm.set_ShapeFile(csv_dir) + + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0.01) + physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) + physics.set_photobleaching(tau0=2.27, alpha=0.73) + + # create image + create = EPIFMVisualizer(configs=epifm, effects=physics) + #create.rewrite_InputFile(output_file_dir='./data_kaizu/data_pb/ST_120ms') + create.rewrite_InputFile(output_file_dir='./data_kaizu/data_pb/S2_150ms') if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_epifm(t0, t1) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_epifm(t0, t1) diff --git a/bioimaging/kaizu_script.py b/bioimaging/kaizu_script.py index da29b51..cd57a5e 100755 --- a/bioimaging/kaizu_script.py +++ b/bioimaging/kaizu_script.py @@ -7,47 +7,45 @@ def test_epifm(t0, t1) : - # create EPIFM imaging - epifm = EPIFMConfigs() - epifm.set_Shutter(start_time=t0, end_time=t1) - epifm.set_LightSource(source_type='LASER', wave_length=532, flux_density=20, angle=72) - epifm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)', normalization=1.0) - epifm.set_DichroicMirror('FF562-Di03-25x36') - epifm.set_Magnification(Mag=364) - - # Detector : EMCCD Camera - epifm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, exposure_time=0.150, \ - focal_point=(0.0,0.5,0.5), QE=0.92, readout_noise=100, emgain=300) - epifm.set_ADConverter(bit=16, offset=2000, fullwell=800000) - - # Output data - #epifm.set_OutputData(image_file_dir='./data_kaizu/numpys/numpys_ST_030ms') - epifm.set_OutputData(image_file_dir='./data_kaizu/numpys/numpys_S2_150ms') - - # Input data - #csv_dir = './data_kaizu/data_pb/ST_030ms' - csv_dir = './data_kaizu/data_pb/S2_150ms' - #csv_dir = '/home/masaki/wrk/spatiocyte/models/Kaizu/data/ST' - epifm.set_InputFile(csv_dir, observable="S") - epifm.set_ShapeFile(csv_dir) - - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0.01) - physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) - physics.set_photobleaching(tau0=2.27, alpha=0.73) - - # create image - create = EPIFMVisualizer(configs=epifm, effects=physics) - create.output_frames(num_div=16) + # create EPIFM imaging + epifm = EPIFMConfigs() + epifm.set_Shutter(start_time=t0, end_time=t1) + epifm.set_LightSource(source_type='LASER', wave_length=532, flux_density=20, angle=72) + epifm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)', normalization=1.0) + epifm.set_DichroicMirror('FF562-Di03-25x36') + epifm.set_Magnification(Mag=364) + + # Detector : EMCCD Camera + epifm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, exposure_time=0.150, \ + focal_point=(0.0,0.5,0.5), QE=0.92, readout_noise=100, emgain=300) + epifm.set_ADConverter(bit=16, offset=2000, fullwell=800000) + + # Output data + #epifm.set_OutputData(image_file_dir='./data_kaizu/numpys/numpys_ST_030ms') + epifm.set_OutputData(image_file_dir='./data_kaizu/numpys/numpys_S2_150ms') + + # Input data + #csv_dir = './data_kaizu/data_pb/ST_030ms' + csv_dir = './data_kaizu/data_pb/S2_150ms' + #csv_dir = '/home/masaki/wrk/spatiocyte/models/Kaizu/data/ST' + epifm.set_InputFile(csv_dir, observable="S") + epifm.set_ShapeFile(csv_dir) + + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0.01) + physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) + physics.set_photobleaching(tau0=2.27, alpha=0.73) + + # create image + create = EPIFMVisualizer(configs=epifm, effects=physics) + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_epifm(t0, t1) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_epifm(t0, t1) diff --git a/bioimaging/linescan_confm_handler.py b/bioimaging/linescan_confm_handler.py index 2f65769..5922779 100755 --- a/bioimaging/linescan_confm_handler.py +++ b/bioimaging/linescan_confm_handler.py @@ -27,13 +27,13 @@ class LineScanConfocalConfigs(EPIFMConfigs) : ''' Line-scanning Confocal configuration - Line-like Gaussian Profile - + - Line-scanning - + - Slit - + - Detector : EMCCD Camera + Line-like Gaussian Profile + + + Line-scanning + + + Slit + + + Detector : EMCCD Camera ''' def __init__(self, user_configs_dict = None): @@ -79,32 +79,32 @@ def set_Illumination_path(self) : # (plank const) * (speed of light) [joules meter] hc = 2.00e-25 - # Illumination + # Illumination w_0 = self.source_radius - # power [joules/sec] + # power [joules/sec] P_0 = self.source_power - # illumination area [m^2] - A_0 = numpy.pi*w_0**2 + # illumination area [m^2] + A_0 = numpy.pi*w_0**2 # single photon energy wave_length = self.source_wavelength*1e-9 E_wl = hc/wave_length - # photon flux [photons/sec] + # photon flux [photons/sec] N_0 = P_0/E_wl - # Rayleigh range + # Rayleigh range z_R = numpy.pi*w_0**2/wave_length # Beam Flux [photons/(m^2 sec)] w_z = w_0*numpy.sqrt(1 + ((wave_length*d*1e-9)/(numpy.pi*w_0**2))**2) - # photon flux density [photon/(sec m^2)] + # photon flux density [photon/(sec m^2)] self.source_flux = numpy.array(map(lambda x : 2*N_0/(numpy.pi*x**2)*numpy.exp(-2*(r*1e-9/x)**2), w_z)) - print('Photon Flux Density (Max) :', numpy.amax(self.source_flux)) + print('Photon Flux Density (Max) :', numpy.amax(self.source_flux)) @@ -112,23 +112,23 @@ def set_Detection_path(self) : wave_length = self.psf_wavelength*1e-9 - # Magnification - Mag = self.image_magnification + # Magnification + Mag = self.image_magnification - # set image scaling factor + # set image scaling factor voxel_radius = self.spatiocyte_VoxelRadius - # set zoom - zoom = self.detector_zoom + # set zoom + zoom = self.detector_zoom - # set slit pixel length - pixel_length = self.slit_size/(Mag*zoom) + # set slit pixel length + pixel_length = self.slit_size/(Mag*zoom) - self.image_resolution = pixel_length - self.image_scaling = pixel_length/(2.0*voxel_radius) + self.image_resolution = pixel_length + self.image_scaling = pixel_length/(2.0*voxel_radius) - print('Resolution :', self.image_resolution, 'm') - print('Scaling :', self.image_scaling) + print('Resolution :', self.image_resolution, 'm') + print('Scaling :', self.image_scaling) # Detector PSF self.set_PSF_detector() @@ -137,399 +137,399 @@ def set_Detection_path(self) : class LineScanConfocalVisualizer(EPIFMVisualizer) : - ''' - Confocal Visualization class of e-cell simulator - ''' + ''' + Confocal Visualization class of e-cell simulator + ''' - def __init__(self, configs=LineScanConfocalConfigs(), effects=PhysicalEffects()) : + def __init__(self, configs=LineScanConfocalConfigs(), effects=PhysicalEffects()) : - assert isinstance(configs, LineScanConfocalConfigs) - self.configs = configs + assert isinstance(configs, LineScanConfocalConfigs) + self.configs = configs - assert isinstance(effects, PhysicalEffects) - self.effects = effects + assert isinstance(effects, PhysicalEffects) + self.effects = effects - """ - Check and create the folder for image file. - """ - if not os.path.exists(self.configs.image_file_dir): - os.makedirs(self.configs.image_file_dir) - #else: - # for file in os.listdir(self.configs.movie_image_file_dir): - # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) + """ + Check and create the folder for image file. + """ + if not os.path.exists(self.configs.image_file_dir): + os.makedirs(self.configs.image_file_dir) + #else: + # for file in os.listdir(self.configs.movie_image_file_dir): + # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) - """ - Optical Path - """ - self.configs.set_Optical_path() + """ + Optical Path + """ + self.configs.set_Optical_path() - def get_signal(self, time, pid, s_index, p_i, p_b, p_0, norm) : + def get_signal(self, time, pid, s_index, p_i, p_b, p_0, norm) : - # set focal point - x_0, y_0, z_0 = p_0 + # set focal point + x_0, y_0, z_0 = p_0 - # set source center - x_b, y_b, z_b = p_b + # set source center + x_b, y_b, z_b = p_b - # set particle position - x_i, y_i, z_i = p_i + # set particle position + x_i, y_i, z_i = p_i - # - r = self.configs.radial - d = self.configs.depth + # + r = self.configs.radial + d = self.configs.depth - # beam axial position - d_s = abs(x_i - x_b) + # beam axial position + d_s = abs(x_i - x_b) - if (d_s < 20000) : - source_depth = d_s - else : - source_depth = 19999 + if (d_s < 20000) : + source_depth = d_s + else : + source_depth = 19999 - # beam horizontal position (y-direction) - hh = numpy.sqrt((y_i-y_b)**2) + # beam horizontal position (y-direction) + hh = numpy.sqrt((y_i-y_b)**2) - if (hh < len(r)) : - source_horizon = hh - else : - source_horizon = r[-1] + if (hh < len(r)) : + source_horizon = hh + else : + source_horizon = r[-1] - # get illumination PSF - source_psf = self.configs.source_flux[int(source_depth)][int(source_horizon)] - #source_max = norm*self.configs.source_flux[0][0] + # get illumination PSF + source_psf = self.configs.source_flux[int(source_depth)][int(source_horizon)] + #source_max = norm*self.configs.source_flux[0][0] - # signal conversion : - #Intensity = self.get_intensity(time, pid, source_psf, source_max) - Ratio = self.effects.conversion_ratio + # signal conversion : + #Intensity = self.get_intensity(time, pid, source_psf, source_max) + Ratio = self.effects.conversion_ratio - # fluorophore axial position - d_f = abs(x_i - x_b) + # fluorophore axial position + d_f = abs(x_i - x_b) - if (d_f < len(d)) : - fluo_depth = d_f - else : - fluo_depth = d[-1] + if (d_f < len(d)) : + fluo_depth = d_f + else : + fluo_depth = d[-1] - # get fluorophore PSF - fluo_psf = self.fluo_psf[int(fluo_depth)] + # get fluorophore PSF + fluo_psf = self.fluo_psf[int(fluo_depth)] - # signal conversion : Output PSF = PSF(source) * Ratio * PSF(Fluorophore) - signal = norm * source_psf * Ratio * fluo_psf + # signal conversion : Output PSF = PSF(source) * Ratio * PSF(Fluorophore) + signal = norm * source_psf * Ratio * fluo_psf - return signal + return signal - def get_molecule_plane(self, cell, time, data, pid, p_b, p_0, offset) : + def get_molecule_plane(self, cell, time, data, pid, p_b, p_0, offset) : - voxel_size = (2.0*self.configs.spatiocyte_VoxelRadius)/1e-9 + voxel_size = (2.0*self.configs.spatiocyte_VoxelRadius)/1e-9 - # get beam position - x_b, y_b, z_b = p_b + # get beam position + x_b, y_b, z_b = p_b - # cutoff randius - slit_radius = int(self.configs.image_scaling*voxel_size/2) - cut_off = 3*slit_radius + # cutoff randius + slit_radius = int(self.configs.image_scaling*voxel_size/2) + cut_off = 3*slit_radius - # particles coordinate, species and lattice IDs - c_id, s_id, l_id = data + # particles coordinate, species and lattice IDs + c_id, s_id, l_id = data - sid_array = numpy.array(self.configs.spatiocyte_species_id) - s_index = (numpy.abs(sid_array - int(s_id))).argmin() + sid_array = numpy.array(self.configs.spatiocyte_species_id) + s_index = (numpy.abs(sid_array - int(s_id))).argmin() - if self.configs.spatiocyte_observables[s_index] is True : + if self.configs.spatiocyte_observables[s_index] is True : - # Normalization - unit_time = 1.0 - unit_area = (1e-9)**2 - norm = (unit_area*unit_time)/(4.0*numpy.pi) + # Normalization + unit_time = 1.0 + unit_area = (1e-9)**2 + norm = (unit_area*unit_time)/(4.0*numpy.pi) - # particles coordinate in real(nm) scale - #pos = self.get_coordinate(c_id) - #p_i = numpy.array(pos)*voxel_size - p_i = self.get_coordinate(c_id) - x_i, y_i, z_i = p_i + # particles coordinate in real(nm) scale + #pos = self.get_coordinate(c_id) + #p_i = numpy.array(pos)*voxel_size + p_i = self.get_coordinate(c_id) + x_i, y_i, z_i = p_i - if (abs(y_i - y_b) < cut_off) : + if (abs(y_i - y_b) < cut_off) : - #print pid, s_id, p_i - # get signal matrix - signal = self.get_signal(time, pid, s_index, p_i, p_b, p_0, norm) + #print pid, s_id, p_i + # get signal matrix + signal = self.get_signal(time, pid, s_index, p_i, p_b, p_0, norm) - z_from, y_from = offset - z_size, y_size = cell.shape - y_to = y_from + y_size - z_to = z_from + z_size + z_from, y_from = offset + z_size, y_size = cell.shape + y_to = y_from + y_size + z_to = z_from + z_size - r = len(self.configs.radial) + r = len(self.configs.radial) - # add signal matrix to image plane - self.overwrite_signal(cell, signal, p_i, r, z_from, y_from, z_to, y_to) - #self.overwrite_signal(cell, signal, p_i) + # add signal matrix to image plane + self.overwrite_signal(cell, signal, p_i, r, z_from, y_from, z_to, y_to) + #self.overwrite_signal(cell, signal, p_i) - def overwrite_signal(self, cell, signal, p_i, r, z_from, y_from, z_to, y_to) : + def overwrite_signal(self, cell, signal, p_i, r, z_from, y_from, z_to, y_to) : - z_size = z_to - z_from - y_size = y_to - y_from - x_i, y_i, z_i = p_i - zi_size, yi_size = signal.shape + z_size = z_to - z_from + y_size = y_to - y_from + x_i, y_i, z_i = p_i + zi_size, yi_size = signal.shape - #z0_from = bounded(z_i - r - z_from, lower_bound=0, upper_bound=z_size) - y0_from = bounded(y_i - r - y_from, lower_bound=0, upper_bound=y_size) - #z0_to = bounded(z_i + r - z_to, lower_bound=0, upper_bound=z_size) - y0_to = bounded(y_i + r - y_to, lower_bound=0, upper_bound=y_size) + #z0_from = bounded(z_i - r - z_from, lower_bound=0, upper_bound=z_size) + y0_from = bounded(y_i - r - y_from, lower_bound=0, upper_bound=y_size) + #z0_to = bounded(z_i + r - z_to, lower_bound=0, upper_bound=z_size) + y0_to = bounded(y_i + r - y_to, lower_bound=0, upper_bound=y_size) - #zi_from = bounded(z_from - z_i + r, lower_bound=0, upper_bound=zi_size) - yi_from = bounded(y_from - y_i + r, lower_bound=0, upper_bound=yi_size) - #zi_to = bounded(z_from - z_i + r + z_size, lower_bound=0, upper_bound=zi_size) - yi_to = bounded(y_from - y_i + r + y_size, lower_bound=0, upper_bound=yi_size) + #zi_from = bounded(z_from - z_i + r, lower_bound=0, upper_bound=zi_size) + yi_from = bounded(y_from - y_i + r, lower_bound=0, upper_bound=yi_size) + #zi_to = bounded(z_from - z_i + r + z_size, lower_bound=0, upper_bound=zi_size) + yi_to = bounded(y_from - y_i + r + y_size, lower_bound=0, upper_bound=yi_size) - # z-axis - Nz_cell = len(cell) - Nz_signal = len(signal) - Nr = len(self.configs.radial) + # z-axis + Nz_cell = len(cell) + Nz_signal = len(signal) + Nr = len(self.configs.radial) - z_to = z_i + Nr - z_from = z_i - Nr + z_to = z_i + Nr + z_from = z_i - Nr - if (z_to > Nz_cell) : + if (z_to > Nz_cell) : - dz_to = z_to - Nz_cell + dz_to = z_to - Nz_cell - z0_to = int(Nz_cell) - zi_to = int(Nz_signal - dz_to) + z0_to = int(Nz_cell) + zi_to = int(Nz_signal - dz_to) - else : + else : - dz_to = Nz_cell - (z_i + Nr) + dz_to = Nz_cell - (z_i + Nr) - z0_to = int(Nz_cell - dz_to) - zi_to = int(Nz_signal) + z0_to = int(Nz_cell - dz_to) + zi_to = int(Nz_signal) - if (z_from < 0) : + if (z_from < 0) : - dz_from = abs(z_from) + dz_from = abs(z_from) - z0_from = 0 - zi_from = int(dz_from) + z0_from = 0 + zi_from = int(dz_from) - else : + else : - dz_from = z_from + dz_from = z_from - z0_from = int(dz_from) - zi_from = 0 + z0_from = int(dz_from) + zi_from = 0 - ddz = (z0_to - z0_from) - (zi_to - zi_from) + ddz = (z0_to - z0_from) - (zi_to - zi_from) - if (ddz > 0) : z0_to = z0_to - ddz - if (ddz < 0) : zi_to = zi_to - ddz + if (ddz > 0) : z0_to = z0_to - ddz + if (ddz < 0) : zi_to = zi_to - ddz - cell[z0_from:z0_to, y0_from:y0_to] += signal[zi_from:zi_to, yi_from:yi_to] + cell[z0_from:z0_to, y0_from:y0_to] += signal[zi_from:zi_to, yi_from:yi_to] - def output_frames(self, num_div=1): + def output_frames(self, num_div=1): - # set Fluorophores PSF - self.set_fluo_psf() + # set Fluorophores PSF + self.set_fluo_psf() - start = self.configs.spatiocyte_start_time - end = self.configs.spatiocyte_end_time + start = self.configs.spatiocyte_start_time + end = self.configs.spatiocyte_end_time - exposure_time = self.configs.detector_exposure_time - num_timesteps = int(math.ceil((end - start) / exposure_time)) + exposure_time = self.configs.detector_exposure_time + num_timesteps = int(math.ceil((end - start) / exposure_time)) - index0 = int(round(start/exposure_time)) + index0 = int(round(start/exposure_time)) - envname = 'ECELL_MICROSCOPE_SINGLE_PROCESS' + envname = 'ECELL_MICROSCOPE_SINGLE_PROCESS' - if envname in os.environ and os.environ[envname]: - self.output_frames_each_process(index0, num_timesteps) - else: - num_processes = multiprocessing.cpu_count() - n, m = divmod(num_timesteps, num_processes) - # when 10 tasks is distributed to 4 processes, - # number of tasks of each process must be [3, 3, 2, 2] - chunks = [n + 1 if i < m else n for i in range(num_processes)] + if envname in os.environ and os.environ[envname]: + self.output_frames_each_process(index0, num_timesteps) + else: + num_processes = multiprocessing.cpu_count() + n, m = divmod(num_timesteps, num_processes) + # when 10 tasks is distributed to 4 processes, + # number of tasks of each process must be [3, 3, 2, 2] + chunks = [n + 1 if i < m else n for i in range(num_processes)] - processes = [] - start_index = index0 + processes = [] + start_index = index0 - for chunk in chunks: - stop_index = start_index + chunk - process = multiprocessing.Process( - target=self.output_frames_each_process, - args=(start_index, stop_index)) - process.start() - processes.append(process) - start_index = stop_index + for chunk in chunks: + stop_index = start_index + chunk + process = multiprocessing.Process( + target=self.output_frames_each_process, + args=(start_index, stop_index)) + process.start() + processes.append(process) + start_index = stop_index - for process in processes: - process.join() + for process in processes: + process.join() - def output_frames_each_process(self, start_count, stop_count): + def output_frames_each_process(self, start_count, stop_count): - voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 + voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 - # image dimenssion in pixel-scale - Nw_pixel = self.configs.detector_image_size[0] - Nh_pixel = self.configs.detector_image_size[1] + # image dimenssion in pixel-scale + Nw_pixel = self.configs.detector_image_size[0] + Nh_pixel = self.configs.detector_image_size[1] - # cells dimenssion in nm-scale - Nz = int(self.configs.spatiocyte_lengths[2] * voxel_size) - Ny = int(self.configs.spatiocyte_lengths[1] * voxel_size) - Nx = int(self.configs.spatiocyte_lengths[0] * voxel_size) + # cells dimenssion in nm-scale + Nz = int(self.configs.spatiocyte_lengths[2] * voxel_size) + Ny = int(self.configs.spatiocyte_lengths[1] * voxel_size) + Nx = int(self.configs.spatiocyte_lengths[0] * voxel_size) - # pixel length : nm/pixel - Np = int(self.configs.image_scaling*voxel_size) + # pixel length : nm/pixel + Np = int(self.configs.image_scaling*voxel_size) - # cells dimenssion in pixel-scale - Ny_pixel = Ny/Np - Nz_pixel = Nz/Np + # cells dimenssion in pixel-scale + Ny_pixel = Ny/Np + Nz_pixel = Nz/Np - # focal point - p_0 = numpy.array([Nx, Ny, Nz])*self.configs.detector_focal_point + # focal point + p_0 = numpy.array([Nx, Ny, Nz])*self.configs.detector_focal_point - # beam position : - beam_center = numpy.array(self.configs.detector_focal_point) + # beam position : + beam_center = numpy.array(self.configs.detector_focal_point) - # set boundary condition + # set boundary condition # if (self.configs.spatiocyte_bc_switch == True) : # # bc = numpy.zeros(shape=(Nz, Ny)) # bc = self.set_boundary_plane(bc, p_b, p_0) - # exposure time on cell - R = float(Ny_pixel) / float(Nw_pixel) + # exposure time on cell + R = float(Ny_pixel) / float(Nw_pixel) - exposure_time = self.configs.detector_exposure_time - contact_time = R * exposure_time - non_contact_time = (exposure_time - contact_time)/2 + exposure_time = self.configs.detector_exposure_time + contact_time = R * exposure_time + non_contact_time = (exposure_time - contact_time)/2 - spatiocyte_start_time = self.configs.spatiocyte_start_time - time = exposure_time * start_count - end = exposure_time * stop_count + spatiocyte_start_time = self.configs.spatiocyte_start_time + time = exposure_time * start_count + end = exposure_time * stop_count - # data-time interval - data_interval = self.configs.spatiocyte_interval + # data-time interval + data_interval = self.configs.spatiocyte_interval - # time/count - delta_time = int(round(exposure_time / data_interval)) + # time/count + delta_time = int(round(exposure_time / data_interval)) - # create frame data composed by frame element data - count = start_count - count0 = int(round(spatiocyte_start_time / exposure_time)) + # create frame data composed by frame element data + count = start_count + count0 = int(round(spatiocyte_start_time / exposure_time)) - # initialize Physical effects - #length0 = len(self.configs.spatiocyte_data[0][1]) - #self.effects.set_states(t0, length0) + # initialize Physical effects + #length0 = len(self.configs.spatiocyte_data[0][1]) + #self.effects.set_states(t0, length0) - while (time < end) : + while (time < end) : - # set image file name - image_file_name = os.path.join(self.configs.image_file_dir, - self.configs.image_file_name_format % (count)) + # set image file name + image_file_name = os.path.join(self.configs.image_file_dir, + self.configs.image_file_name_format % (count)) - print('time : ', time, ' sec (', count, ')') + print('time : ', time, ' sec (', count, ')') - # define cell - cell = numpy.zeros(shape=(Nz, Ny)) + # define cell + cell = numpy.zeros(shape=(Nz, Ny)) - count_start = (count - count0)*delta_time - count_end = (count - count0 + 1)*delta_time + count_start = (count - count0)*delta_time + count_end = (count - count0 + 1)*delta_time - frame_data = self.configs.spatiocyte_data[count_start:count_end] + frame_data = self.configs.spatiocyte_data[count_start:count_end] - if (len(frame_data) > 0) : + if (len(frame_data) > 0) : - # beam position : initial - p_b = numpy.array([Nx, Ny, Nz])*beam_center + # beam position : initial + p_b = numpy.array([Nx, Ny, Nz])*beam_center - # line-scanning sequences - scan_time = 0 + # line-scanning sequences + scan_time = 0 - while (scan_time < contact_time) : + while (scan_time < contact_time) : - # beam position : i-th frame - if (count%2 == 0) : - # scan from left-to-right - beam_center[1] = scan_time/contact_time - else : - # scan from right-to-left - beam_center[1] = 1 - scan_time/contact_time + # beam position : i-th frame + if (count%2 == 0) : + # scan from left-to-right + beam_center[1] = scan_time/contact_time + else : + # scan from right-to-left + beam_center[1] = 1 - scan_time/contact_time - p_b = numpy.array([Nx, Ny, Nz])*beam_center - x_b, y_b, z_b = p_b + p_b = numpy.array([Nx, Ny, Nz])*beam_center + x_b, y_b, z_b = p_b - # loop for frame data - i_time, i_data = frame_data[0] + # loop for frame data + i_time, i_data = frame_data[0] - diff = abs(i_time - (scan_time + time + non_contact_time)) - data = i_data + diff = abs(i_time - (scan_time + time + non_contact_time)) + data = i_data - for i, (i_time, i_data) in enumerate(frame_data) : - #print '\t', '%02d-th frame : ' % (i), i_time, ' sec' - i_diff = abs(i_time - (scan_time + time)) + for i, (i_time, i_data) in enumerate(frame_data) : + #print '\t', '%02d-th frame : ' % (i), i_time, ' sec' + i_diff = abs(i_time - (scan_time + time)) - if (i_diff < diff) : - diff = i_diff - data = i_data + if (i_diff < diff) : + diff = i_diff + data = i_data - # overwrite the scanned region to cell - r_s = int(self.configs.image_scaling*voxel_size/2) + # overwrite the scanned region to cell + r_s = int(self.configs.image_scaling*voxel_size/2) - if (y_b-r_s < 0) : y_from = int(y_b) - else : y_from = int(y_b - r_s) + if (y_b-r_s < 0) : y_from = int(y_b) + else : y_from = int(y_b - r_s) - if (y_b+r_s >= Ny) : y_to = int(y_b) - else : y_to = int(y_b + r_s) + if (y_b+r_s >= Ny) : y_to = int(y_b) + else : y_to = int(y_b + r_s) - z_from, z_to = 0, Nz + z_from, z_to = 0, Nz - offset = (z_from, y_from) - mask = numpy.zeros(shape=(z_to-z_from, y_to-y_from)) + offset = (z_from, y_from) + mask = numpy.zeros(shape=(z_to-z_from, y_to-y_from)) - zz, yy = numpy.ogrid[z_from:z_to, y_from-int(y_b):y_to-int(y_b)] - rr_cut = yy**2 < r_s**2 - mask[rr_cut] = 1 + zz, yy = numpy.ogrid[z_from:z_to, y_from-int(y_b):y_to-int(y_b)] + rr_cut = yy**2 < r_s**2 + mask[rr_cut] = 1 - scan_cell = numpy.zeros_like(mask) + scan_cell = numpy.zeros_like(mask) - # loop for particles - for j, j_data in enumerate(data) : - self.get_molecule_plane(scan_cell, i_time, j_data, j, p_b, p_0, offset) + # loop for particles + for j, j_data in enumerate(data) : + self.get_molecule_plane(scan_cell, i_time, j_data, j, p_b, p_0, offset) - cell[z_from:z_to, y_from:y_to] += mask*scan_cell*(contact_time/Ny_pixel) + cell[z_from:z_to, y_from:y_to] += mask*scan_cell*(contact_time/Ny_pixel) - scan_time += contact_time/Ny_pixel + scan_time += contact_time/Ny_pixel - if (numpy.amax(cell) > 0) : + if (numpy.amax(cell) > 0) : -# if (self.configs.spatiocyte_bc_switch == True) : -# camera = self.detector_output(cell, bc) -# else : camera = self.detector_output(cell) +# if (self.configs.spatiocyte_bc_switch == True) : +# camera = self.detector_output(cell, bc) +# else : camera = self.detector_output(cell) - camera = self.detector_output(cell) + camera = self.detector_output(cell) - # save data to numpy-binary file - image_file_name = os.path.join(self.configs.image_file_dir, - self.configs.image_file_name_format % (count)) - numpy.save(image_file_name, camera) + # save data to numpy-binary file + image_file_name = os.path.join(self.configs.image_file_dir, + self.configs.image_file_name_format % (count)) + numpy.save(image_file_name, camera) - # save data to png-image file - #camera[:,:,3].astype('uint%d' % (self.configs.ADConverter_bit)) - #toimage(camera[:,:,3], low=numpy.amin(camera[:,:,3]), high=numpy.amax(camera[:,:,3]), mode='I').save(image_file_name) + # save data to png-image file + #camera[:,:,3].astype('uint%d' % (self.configs.ADConverter_bit)) + #toimage(camera[:,:,3], low=numpy.amin(camera[:,:,3]), high=numpy.amax(camera[:,:,3]), mode='I').save(image_file_name) - time += exposure_time - count += 1 + time += exposure_time + count += 1 diff --git a/bioimaging/linescan_confm_script.py b/bioimaging/linescan_confm_script.py index 1931226..c4f7e9e 100755 --- a/bioimaging/linescan_confm_script.py +++ b/bioimaging/linescan_confm_script.py @@ -7,40 +7,39 @@ def test_confm(t0, t1) : - # create Line-scanning Confocal Microscopy - confm = LineScanConfocalConfigs() - - confm.set_LightSource(source_type='LASER', wave_length=532, power=20e-3, radius=200e-9) - confm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') - #confm.set_Fluorophore(fluorophore_type='Gaussian', wave_length=508, width=(70.0, 140.0)) - #confm.set_DichroicMirror('FF562-Di03-25x36') - #confm.set_EmissionFilter('FF01-593_40-25') - confm.set_Slits(size=16e-6) - confm.set_Magnification(Mag=160) - #confm.set_Detector(detector='EMCCD', zoom=1, emgain=1, focal_point=(0.9,0.5,0.5), exposure_time=50e-3) - confm.set_Detector(detector='EMCCD', zoom=1, emgain=100, focal_point=(0.8,0.5,0.5), exposure_time=100) - confm.set_ADConverter(bit=16, offset=2000, fullwell=370000) - #confm.set_OutputData(image_file_dir='./images_dicty_02_line_zaxis09') - confm.set_OutputData(image_file_dir='./images_pten_zaxis09') - #confm.set_InputData('/home/masaki/ecell3/latest/data/csv/simple_dicty_02', start=t0, end=t1) - confm.set_InputData('/home/masaki/ecell3/latest/data/csv/pten', start=t0, end=t1, observable="PTEN") - - # create physical effects - physics = PhysicalEffects() - physics.set_Conversion(ratio=1e-6) - #physics.set_Background(mean=30) - physics.set_DetectorCrosstalk(width=1.00) - - # create image and movie - create = LineScanConfocalVisualizer(configs=confm, effects=physics) - create.output_frames(num_div=16) + # create Line-scanning Confocal Microscopy + confm = LineScanConfocalConfigs() + + confm.set_LightSource(source_type='LASER', wave_length=532, power=20e-3, radius=200e-9) + confm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') + #confm.set_Fluorophore(fluorophore_type='Gaussian', wave_length=508, width=(70.0, 140.0)) + #confm.set_DichroicMirror('FF562-Di03-25x36') + #confm.set_EmissionFilter('FF01-593_40-25') + confm.set_Slits(size=16e-6) + confm.set_Magnification(Mag=160) + #confm.set_Detector(detector='EMCCD', zoom=1, emgain=1, focal_point=(0.9,0.5,0.5), exposure_time=50e-3) + confm.set_Detector(detector='EMCCD', zoom=1, emgain=100, focal_point=(0.8,0.5,0.5), exposure_time=100) + confm.set_ADConverter(bit=16, offset=2000, fullwell=370000) + #confm.set_OutputData(image_file_dir='./images_dicty_02_line_zaxis09') + confm.set_OutputData(image_file_dir='./images_pten_zaxis09') + #confm.set_InputData('/home/masaki/ecell3/latest/data/csv/simple_dicty_02', start=t0, end=t1) + confm.set_InputData('/home/masaki/ecell3/latest/data/csv/pten', start=t0, end=t1, observable="PTEN") + + # create physical effects + physics = PhysicalEffects() + physics.set_Conversion(ratio=1e-6) + #physics.set_Background(mean=30) + physics.set_DetectorCrosstalk(width=1.00) + + # create image and movie + create = LineScanConfocalVisualizer(configs=confm, effects=physics) + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_confm(t0, t1) + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_confm(t0, t1) diff --git a/bioimaging/lscm_handler.py b/bioimaging/lscm_handler.py index a0398ec..1b05ebf 100755 --- a/bioimaging/lscm_handler.py +++ b/bioimaging/lscm_handler.py @@ -32,13 +32,13 @@ class LSCMConfigs(FCSConfigs) : ''' Point-scanning Confocal configuration - Point-like Gaussian Profile - + - Point-scanning - + - Pinhole - + - Detector : PMT + Point-like Gaussian Profile + + + Point-scanning + + + Pinhole + + + Detector : PMT ''' def __init__(self, user_configs_dict = None): @@ -66,7 +66,7 @@ def __init__(self, user_configs_dict = None): def set_LightSource(self, source_type = None, - wave_length = None, + wave_length = None, flux = None, center = None, radius = None, @@ -99,18 +99,18 @@ def set_Pinhole(self, radius = None) : def set_Detector(self, detector = None, - mode = None, - image_size = None, + mode = None, + image_size = None, pixel_length = None, focal_point = None, base_position = None, scan_time = None, - QE = None, - readout_noise = None, - dark_count = None, - gain = None, - dyn_stages = None, - pair_pulses = None + QE = None, + readout_noise = None, + dark_count = None, + gain = None, + dyn_stages = None, + pair_pulses = None ): self._set_data('detector_switch', True) @@ -128,14 +128,14 @@ def set_Detector(self, detector = None, self._set_data('detector_dyn_stages', dyn_stages) self._set_data('detector_pair_pulses', pair_pulses) - print('--- Detector : ', self.detector_type, ' (', self.detector_mode, 'mode )') + print('--- Detector : ', self.detector_type, ' (', self.detector_mode, 'mode )') print('\tImage Size = ', self.detector_image_size[0], 'x', self.detector_image_size[1]) print('\tPixel Size = ', self.detector_pixel_length, 'm/pixel') print('\tFocal Point = ', self.detector_focal_point) print('\tPosition = ', self.detector_base_position) print('\tScan Time = ', self.detector_exposure_time, 'sec/image') print('\tQuantum Efficiency = ', 100*self.detector_qeff, '%') - print('\tReadout Noise = ', self.detector_readout_noise, 'electron') + print('\tReadout Noise = ', self.detector_readout_noise, 'electron') print('\tDark Count = ', self.detector_dark_count, 'electron/sec') print('\tGain = ', 'x', self.detector_gain) print('\tDynode = ', self.detector_dyn_stages, 'stages') @@ -151,26 +151,26 @@ def set_Illumination_path(self) : # (plank const) * (speed of light) [joules meter] hc = 2.00e-25 - # Illumination + # Illumination w_0 = self.source_radius - # flux [joules/sec=watt] + # flux [joules/sec=watt] P_0 = self.source_flux # single photon energy [joules] wave_length = self.source_wavelength*1e-9 E_wl = hc/wave_length - # photon flux [photons/sec] + # photon flux [photons/sec] N_0 = P_0/E_wl # Beam width [m] w_z = w_0*numpy.sqrt(1 + ((wave_length*d*1e-9)/(numpy.pi*w_0**2))**2) - # photon flux density [photon/(sec m^2)] + # photon flux density [photon/(sec m^2)] self.source_flux_density = numpy.array(map(lambda x : 2*N_0/(numpy.pi*x**2)*numpy.exp(-2*(r*1e-9/x)**2), w_z)) - print('Photon Flux Density (Max) :', numpy.amax(self.source_flux_density)) + print('Photon Flux Density (Max) :', numpy.amax(self.source_flux_density)) @@ -178,22 +178,22 @@ def set_Detection_path(self) : wave_length = self.psf_wavelength*1e-9 - # Magnification - Mag = self.image_magnification + # Magnification + Mag = self.image_magnification - # set voxel radius + # set voxel radius voxel_radius = self.spatiocyte_VoxelRadius - # set pixel length - #pixel_length = (2.0*self.pinhole_radius)/Mag - pixel_length = self.detector_pixel_length + # set pixel length + #pixel_length = (2.0*self.pinhole_radius)/Mag + pixel_length = self.detector_pixel_length - # set image scaling factor - self.image_scaling = pixel_length/(2.0*voxel_radius) + # set image scaling factor + self.image_scaling = pixel_length/(2.0*voxel_radius) - print('Magnification : x %d' % (Mag)) - print('Resolution :', pixel_length, 'm/pixel') - print('Scaling :', self.image_scaling) + print('Magnification : x %d' % (Mag)) + print('Resolution :', pixel_length, 'm/pixel') + print('Scaling :', self.image_scaling) # Detector PSF self.set_PSF_detector() @@ -202,74 +202,74 @@ def set_Detection_path(self) : def set_Optical_path(self) : - # (0) Data : Cell Model Sample - self.set_Time_arrays() - self.set_Spatiocyte_data_arrays() + # (0) Data : Cell Model Sample + self.set_Time_arrays() + self.set_Spatiocyte_data_arrays() - # (1) Illumination path : Light source --> Cell Model Sample - self.set_Illumination_path() + # (1) Illumination path : Light source --> Cell Model Sample + self.set_Illumination_path() - # (2) Detection path : Cell Model Sample --> Detector - self.set_Detection_path() + # (2) Detection path : Cell Model Sample --> Detector + self.set_Detection_path() def reset_InputData(self, csv_file_directry, start=0, end=None, observable=None) : - print('--- Input Spatiocyte Data : ', csv_file_directry) + print('--- Input Spatiocyte Data : ', csv_file_directry) - ### header - f = open(csv_file_directry + '/pt-input.csv', 'r') + ### header + f = open(csv_file_directry + '/pt-input.csv', 'r') header = f.readline().rstrip().split(',') header[:5] = [float(_) for _ in header[:5]] f.close() interval, lengths, voxel_r, species_info = header[0], (header[3:0:-1]), header[4], header[5:] - species_id = range(len(species_info)) - species_index = [_.split(':')[1].split(']')[0] for _ in species_info] - species_radius = [float(_.split('=')[1]) for _ in species_info] + species_id = range(len(species_info)) + species_index = [_.split(':')[1].split(']')[0] for _ in species_info] + species_radius = [float(_.split('=')[1]) for _ in species_info] -# ##### -# count_start = int(round(start/interval)) -# count_end = int(round(end/interval)) +# ##### +# count_start = int(round(start/interval)) +# count_end = int(round(end/interval)) # -# data = [] +# data = [] # -# # read lattice file -# for count in range(count_start, count_end, 1) : +# # read lattice file +# for count in range(count_start, count_end, 1) : +# +# csv_file_path = csv_file_directry + '/pt-%09d.0.csv' % (count) # -# csv_file_path = csv_file_directry + '/pt-%09d.0.csv' % (count) -# # try : # # csv_file = open(csv_file_path, 'r') # -# dataset = [] +# dataset = [] # -# for row in csv.reader(csv_file) : -# dataset.append(row) +# for row in csv.reader(csv_file) : +# dataset.append(row) # # ### particle data -# time = float(dataset[0][0]) +# time = float(dataset[0][0]) +# +# particles = [] # -# particles = [] +# from ast import literal_eval # -# from ast import literal_eval +# for data_id in dataset : +# c_id = (float(data_id[1]), float(data_id[2]), float(data_id[3])) +# s_id, l_id = literal_eval(data_id[6]) +# particles.append((c_id, s_id, l_id)) # -# for data_id in dataset : -# c_id = (float(data_id[1]), float(data_id[2]), float(data_id[3])) -# s_id, l_id = literal_eval(data_id[6]) -# particles.append((c_id, s_id, l_id)) -# -# data.append([time, particles]) +# data.append([time, particles]) # # # except Exception : # print 'Error : ', csv_file_path, ' not found' -# exit() +# exit() # -# data.sort(lambda x, y:cmp(x[0], y[0])) +# data.sort(lambda x, y:cmp(x[0], y[0])) # get filename self._set_data('spatiocyte_file_directry', csv_file_directry) @@ -279,14 +279,14 @@ def reset_InputData(self, csv_file_directry, start=0, end=None, observable=None) self._set_data('spatiocyte_end_time', end) self._set_data('spatiocyte_interval', interval) - # get data + # get data #self._set_data('spatiocyte_data', data) # get species properties self._set_data('spatiocyte_species_id', species_id) self._set_data('spatiocyte_index', species_index) - #self._set_data('spatiocyte_diffusion', species_diffusion) - self._set_data('spatiocyte_radius', species_radius) + #self._set_data('spatiocyte_diffusion', species_diffusion) + self._set_data('spatiocyte_radius', species_radius) # get lattice properties #self._set_data('spatiocyte_lattice_id', map(lambda x : x[0], lattice)) @@ -294,650 +294,650 @@ def reset_InputData(self, csv_file_directry, start=0, end=None, observable=None) self._set_data('spatiocyte_VoxelRadius', voxel_r) self._set_data('spatiocyte_theNormalizedVoxelRadius', 0.5) - + # set observable if observable is None : index = [True for i in range(len(self.spatiocyte_index))] - else : + else : index = map(lambda x : True if x.find(observable) > -1 else False, self.spatiocyte_index) - #index = [False, True] + #index = [False, True] self.spatiocyte_observables = copy.copy(index) print('\tStart time =', self.spatiocyte_start_time, 'sec') print('\tEnd time =', self.spatiocyte_end_time, 'sec') print('\tInterval =', self.spatiocyte_interval, 'sec') - print('\tVoxel radius =', self.spatiocyte_VoxelRadius, 'm') + print('\tVoxel radius =', self.spatiocyte_VoxelRadius, 'm') print('\tCompartment lengths :', self.spatiocyte_lengths, 'voxels') print('\tSpecies Index :', self.spatiocyte_index) print('\tObservable :', self.spatiocyte_observables) - # Visualization error - if self.spatiocyte_species_id is None: - raise VisualizerError('Cannot find species_id in any given csv files') + # Visualization error + if self.spatiocyte_species_id is None: + raise VisualizerError('Cannot find species_id in any given csv files') -# if len(self.spatiocyte_data) == 0: -# raise VisualizerError('Cannot find spatiocyte_data in any given csv files: ' \ -# + ', '.join(csv_file_directry)) +# if len(self.spatiocyte_data) == 0: +# raise VisualizerError('Cannot find spatiocyte_data in any given csv files: ' \ +# + ', '.join(csv_file_directry)) - if len(self.spatiocyte_index) == 0 : - raise VisualizerError('Cannot find spatiocyte_index in any given csv files: ' \ - + ', '.join(csv_file_directry)) + if len(self.spatiocyte_index) == 0 : + raise VisualizerError('Cannot find spatiocyte_index in any given csv files: ' \ + + ', '.join(csv_file_directry)) def set_InputData(self, csv_file_directry, start=0, end=None, observable=None) : - print('--- Input Spatiocyte Data : ', csv_file_directry) + print('--- Input Spatiocyte Data : ', csv_file_directry) - ### header - f = open(csv_file_directry + '/pt-input.csv', 'r') + ### header + f = open(csv_file_directry + '/pt-input.csv', 'r') header = f.readline().rstrip().split(',') header[:5] = [float(_) for _ in header[:5]] f.close() interval, lengths, voxel_r, species_info = header[0], (header[3:0:-1]), header[4], header[5:] - species_id = range(len(species_info)) - species_index = [_.split(':')[1].split(']')[0] for _ in species_info] - species_radius = [float(_.split('=')[1]) for _ in species_info] + species_id = range(len(species_info)) + species_index = [_.split(':')[1].split(']')[0] for _ in species_info] + species_radius = [float(_.split('=')[1]) for _ in species_info] - ##### - count_start = int(round(start/interval)) - count_end = int(round(end/interval)) + ##### + count_start = int(round(start/interval)) + count_end = int(round(end/interval)) - data = [] + data = [] - # read lattice file - for count in range(count_start, count_end, 1) : + # read lattice file + for count in range(count_start, count_end, 1) : + + csv_file_path = csv_file_directry + '/pt-%09d.0.csv' % (count) - csv_file_path = csv_file_directry + '/pt-%09d.0.csv' % (count) - try : csv_file = open(csv_file_path, 'r') - dataset = [] + dataset = [] - for row in csv.reader(csv_file) : - dataset.append(row) + for row in csv.reader(csv_file) : + dataset.append(row) ### particle data - time = float(dataset[0][0]) + time = float(dataset[0][0]) - particles = [] + particles = [] - from ast import literal_eval + from ast import literal_eval - for data_id in dataset : - c_id = (float(data_id[1]), float(data_id[2]), float(data_id[3])) - s_id, l_id = literal_eval(data_id[6]) + for data_id in dataset : + c_id = (float(data_id[1]), float(data_id[2]), float(data_id[3])) + s_id, l_id = literal_eval(data_id[6]) - try : - p_state, cyc_id = float(data_id[7]), float(data_id[8]) - except Exception : - p_state, cyc_id = 1.0, float('inf') + try : + p_state, cyc_id = float(data_id[7]), float(data_id[8]) + except Exception : + p_state, cyc_id = 1.0, float('inf') - particles.append((c_id, s_id, l_id, p_state, cyc_id)) + particles.append((c_id, s_id, l_id, p_state, cyc_id)) - data.append([time, particles]) + data.append([time, particles]) except Exception : print('Error : ', csv_file_path, ' not found') - exit() + exit() - data.sort(lambda x, y:cmp(x[0], y[0])) + data.sort(lambda x, y:cmp(x[0], y[0])) # get run time self._set_data('spatiocyte_start_time', start) self._set_data('spatiocyte_end_time', end) self._set_data('spatiocyte_interval', interval) - # get data + # get data self._set_data('spatiocyte_data', data) # get species properties self._set_data('spatiocyte_species_id', species_id) self._set_data('spatiocyte_index', species_index) - #self._set_data('spatiocyte_diffusion', species_diffusion) - self._set_data('spatiocyte_radius', species_radius) + #self._set_data('spatiocyte_diffusion', species_diffusion) + self._set_data('spatiocyte_radius', species_radius) # get lattice properties #self._set_data('spatiocyte_lattice_id', map(lambda x : x[0], lattice)) self._set_data('spatiocyte_lengths', lengths) self._set_data('spatiocyte_VoxelRadius', voxel_r) self._set_data('spatiocyte_theNormalizedVoxelRadius', 0.5) - + # set observable if observable is None : index = [True for i in range(len(self.spatiocyte_index))] - else : + else : index = map(lambda x : True if x.find(observable) > -1 else False, self.spatiocyte_index) - #index = [False, True] + #index = [False, True] self.spatiocyte_observables = copy.copy(index) print('\tStart time =', self.spatiocyte_start_time, 'sec') print('\tEnd time =', self.spatiocyte_end_time, 'sec') print('\tInterval =', self.spatiocyte_interval, 'sec') - print('\tVoxel radius =', self.spatiocyte_VoxelRadius, 'm') + print('\tVoxel radius =', self.spatiocyte_VoxelRadius, 'm') print('\tCompartment lengths :', self.spatiocyte_lengths, 'voxels') print('\tSpecies Index :', self.spatiocyte_index) print('\tObservable :', self.spatiocyte_observables) - # Visualization error - if self.spatiocyte_species_id is None: - raise VisualizerError('Cannot find species_id in any given csv files') + # Visualization error + if self.spatiocyte_species_id is None: + raise VisualizerError('Cannot find species_id in any given csv files') - if len(self.spatiocyte_data) == 0: - raise VisualizerError('Cannot find spatiocyte_data in any given csv files: ' \ - + ', '.join(csv_file_directry)) + if len(self.spatiocyte_data) == 0: + raise VisualizerError('Cannot find spatiocyte_data in any given csv files: ' \ + + ', '.join(csv_file_directry)) - if len(self.spatiocyte_index) == 0 : - raise VisualizerError('Cannot find spatiocyte_index in any given csv files: ' \ - + ', '.join(csv_file_directry)) + if len(self.spatiocyte_index) == 0 : + raise VisualizerError('Cannot find spatiocyte_index in any given csv files: ' \ + + ', '.join(csv_file_directry)) class LSCMVisualizer(FCSVisualizer) : - ''' - Confocal Visualization class of e-cell simulator - ''' + ''' + Confocal Visualization class of e-cell simulator + ''' + + def __init__(self, configs=LSCMConfigs(), effects=PhysicalEffects()) : - def __init__(self, configs=LSCMConfigs(), effects=PhysicalEffects()) : + assert isinstance(configs, LSCMConfigs) + self.configs = configs - assert isinstance(configs, LSCMConfigs) - self.configs = configs + assert isinstance(effects, PhysicalEffects) + self.effects = effects - assert isinstance(effects, PhysicalEffects) - self.effects = effects + """ + Check and create the folder for image file. + """ + if not os.path.exists(self.configs.image_file_dir): + os.makedirs(self.configs.image_file_dir) + #else: + # for file in os.listdir(self.configs.movie_image_file_dir): + # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) - """ - Check and create the folder for image file. - """ - if not os.path.exists(self.configs.image_file_dir): - os.makedirs(self.configs.image_file_dir) - #else: - # for file in os.listdir(self.configs.movie_image_file_dir): - # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) + """ + Optical Path + """ + self.configs.set_Optical_path() - """ - Optical Path - """ - self.configs.set_Optical_path() + def rewrite_InputData(self, output_file_dir=None) : - def rewrite_InputData(self, output_file_dir=None) : + if not os.path.exists(output_file_dir): + os.makedirs(output_file_dir) - if not os.path.exists(output_file_dir): - os.makedirs(output_file_dir) + # define observational image plane in nm-scale + voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 - # define observational image plane in nm-scale - voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 + # image dimenssion in pixel-scale + Nw_pixel = self.configs.detector_image_size[0] + Nh_pixel = self.configs.detector_image_size[1] - # image dimenssion in pixel-scale - Nw_pixel = self.configs.detector_image_size[0] - Nh_pixel = self.configs.detector_image_size[1] + ## cell size (nm scale) + Nz = int(self.configs.spatiocyte_lengths[2]*voxel_size) + Ny = int(self.configs.spatiocyte_lengths[1]*voxel_size) + Nx = int(self.configs.spatiocyte_lengths[0]*voxel_size) - ## cell size (nm scale) - Nz = int(self.configs.spatiocyte_lengths[2]*voxel_size) - Ny = int(self.configs.spatiocyte_lengths[1]*voxel_size) - Nx = int(self.configs.spatiocyte_lengths[0]*voxel_size) + # pixel length : nm/pixel + Np = int(self.configs.image_scaling*voxel_size) - # pixel length : nm/pixel - Np = int(self.configs.image_scaling*voxel_size) + # image dimenssion in pixel-scale + Nz_pixel = Nz/Np + Ny_pixel = Ny/Np - # image dimenssion in pixel-scale - Nz_pixel = Nz/Np - Ny_pixel = Ny/Np + # Active states per detection time + dt = self.configs.spatiocyte_interval + exposure_time = self.configs.detector_exposure_time - # Active states per detection time - dt = self.configs.spatiocyte_interval - exposure_time = self.configs.detector_exposure_time + start = self.configs.spatiocyte_start_time + end = self.configs.spatiocyte_end_time - start = self.configs.spatiocyte_start_time - end = self.configs.spatiocyte_end_time + # Abogadoro's number + Na = self.effects.avogadoros_number - # Abogadoro's number - Na = self.effects.avogadoros_number + # spatiocyte size + radius = self.configs.spatiocyte_VoxelRadius + volume = 4.0/3.0*numpy.pi*radius**3 + depth = 2.0*radius - # spatiocyte size - radius = self.configs.spatiocyte_VoxelRadius - volume = 4.0/3.0*numpy.pi*radius**3 - depth = 2.0*radius + # Quantum yield + QY = self.effects.quantum_yield - # Quantum yield - QY = self.effects.quantum_yield + # Abs coefficient [1/(cm M)] + abs_coeff = self.effects.abs_coefficient - # Abs coefficient [1/(cm M)] - abs_coeff = self.effects.abs_coefficient + # photon flux density [#photons/(sec m2)] + n0 = self.configs.source_flux_density - # photon flux density [#photons/(sec m2)] - n0 = self.configs.source_flux_density + # scan time per pixel area + dT = exposure_time/(Nh_pixel*Nw_pixel) - # scan time per pixel area - dT = exposure_time/(Nh_pixel*Nw_pixel) + # Cross-Section [m2] + xsec = numpy.log(10)*(0.1*abs_coeff/Na) - # Cross-Section [m2] - xsec = numpy.log(10)*(0.1*abs_coeff/Na) + # the number of absorbed photons + N_abs = xsec*n0*dT - # the number of absorbed photons - N_abs = xsec*n0*dT - - # Beer-Lambert law : A = log(I0/I) = coef * concentration * path-length - A = (abs_coeff*0.1/Na)*(1/volume)*depth - - # the number of emitted photons - N_emit0 = QY*N_abs*(1 - 10**(-A)) + # Beer-Lambert law : A = log(I0/I) = coef * concentration * path-length + A = (abs_coeff*0.1/Na)*(1/volume)*depth - # copy input file - csv_input = self.configs.spatiocyte_file_directry + '/pt-input.csv' - shutil.copyfile(csv_input, output_file_dir + '/pt-input.csv') + # the number of emitted photons + N_emit0 = QY*N_abs*(1 - 10**(-A)) - # read input file - csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (0) - csv_list = list(csv.reader(open(csv_file_path, 'r'))) + # copy input file + csv_input = self.configs.spatiocyte_file_directry + '/pt-input.csv' + shutil.copyfile(csv_input, output_file_dir + '/pt-input.csv') - # fluorescence - if (self.effects.photobleaching_switch == True) : + # read input file + csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (0) + csv_list = list(csv.reader(open(csv_file_path, 'r'))) - # get the number of particles - N_particles = len(csv_list) + # fluorescence + if (self.effects.photobleaching_switch == True) : - # set fluorescence - self.effects.set_photophysics_4lscm(start, end, dT, N_particles) + # get the number of particles + N_particles = len(csv_list) - bleach = self.effects.fluorescence_bleach - budget = self.effects.fluorescence_budget*N_emit0[0][0] - state = self.effects.fluorescence_state + # set fluorescence + self.effects.set_photophysics_4lscm(start, end, dT, N_particles) - scan_state = numpy.copy(state*0) - scan_counter = numpy.zeros(shape=(N_particles)) + bleach = self.effects.fluorescence_bleach + budget = self.effects.fluorescence_budget*N_emit0[0][0] + state = self.effects.fluorescence_state - # Mesh-grid for pixel-image - w = numpy.linspace(0, Nw_pixel-1, Nw_pixel) - h = numpy.linspace(0, Nh_pixel-1, Nh_pixel) + scan_state = numpy.copy(state*0) + scan_counter = numpy.zeros(shape=(N_particles)) - W, H = numpy.meshgrid(w, h) + # Mesh-grid for pixel-image + w = numpy.linspace(0, Nw_pixel-1, Nw_pixel) + h = numpy.linspace(0, Nh_pixel-1, Nh_pixel) - # scan-time (sec) per pixel-image - dt_w = exposure_time/(Nh_pixel*Nw_pixel) - dt_h = dt_w*Nw_pixel + W, H = numpy.meshgrid(w, h) - T_b = start + W*dt_w + H*dt_h - time = T_b.reshape((Nw_pixel*Nh_pixel)) + # scan-time (sec) per pixel-image + dt_w = exposure_time/(Nh_pixel*Nw_pixel) + dt_h = dt_w*Nw_pixel - # cell-center in pixel-image - W0, H0 = Nw_pixel/2, Nh_pixel/2 + T_b = start + W*dt_w + H*dt_h + time = T_b.reshape((Nw_pixel*Nh_pixel)) - # cell-origin in pixel-image - w0, h0 = W0-int(Ny_pixel/2), H0-int(Nz_pixel/2) + # cell-center in pixel-image + W0, H0 = Nw_pixel/2, Nh_pixel/2 - # cell-located region in pixel image - cell = numpy.zeros((Nw_pixel, Nh_pixel)) - cell[w0:w0+Ny_pixel,h0:h0+Nz_pixel] = 1 - cell_bool = cell.reshape((Nw_pixel*Nh_pixel)) + # cell-origin in pixel-image + w0, h0 = W0-int(Ny_pixel/2), H0-int(Nz_pixel/2) - # beam-position (nm) relative to cell-origin - Y_b, Z_b = numpy.meshgrid(Np*(w-w0), Np*(h-h0)) - X_b = Nx*self.configs.detector_focal_point[0] + # cell-located region in pixel image + cell = numpy.zeros((Nw_pixel, Nh_pixel)) + cell[w0:w0+Ny_pixel,h0:h0+Nz_pixel] = 1 + cell_bool = cell.reshape((Nw_pixel*Nh_pixel)) - # set flux density as a function of radial and depth - r = numpy.linspace(0, 20000, 20001) - d = numpy.linspace(0, 20000, 20001) + # beam-position (nm) relative to cell-origin + Y_b, Z_b = numpy.meshgrid(Np*(w-w0), Np*(h-h0)) + X_b = Nx*self.configs.detector_focal_point[0] - # loop for scannig - #count0 = int(start/exposure_time) - #count0 = int(start_time/exposure_time) - index0 = int(round(start/dt)) - delta_index = int(round(exposure_time/dt)) + # set flux density as a function of radial and depth + r = numpy.linspace(0, 20000, 20001) + d = numpy.linspace(0, 20000, 20001) - while (time[-1] < end) : + # loop for scannig + #count0 = int(start/exposure_time) + #count0 = int(start_time/exposure_time) + index0 = int(round(start/dt)) + delta_index = int(round(exposure_time/dt)) - count_imaging = int(round(time[0]/exposure_time)) + while (time[-1] < end) : - print('time : ', time[0], '-', time[-1], ' sec/image (', count_imaging, ')') + count_imaging = int(round(time[0]/exposure_time)) - # set frame datasets - index1 = int(round(time[0]/dt)) - index_start = (index1 - index0) - index_end = (index1 - index0) + delta_index + print('time : ', time[0], '-', time[-1], ' sec/image (', count_imaging, ')') - time_index = (time/dt).astype('int') + # set frame datasets + index1 = int(round(time[0]/dt)) + index_start = (index1 - index0) + index_end = (index1 - index0) + delta_index - for index in range(index_start, index_end, 1) : + time_index = (time/dt).astype('int') - # read input file - csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (index) + for index in range(index_start, index_end, 1) : - csv_list = list(csv.reader(open(csv_file_path, 'r'))) - dataset = numpy.array(csv_list) + # read input file + csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (index) - time_bool = (time_index == index).astype('int') - imaging = (time_bool*cell_bool).astype('int') - len_imaging = len(imaging[imaging > 0]) + csv_list = list(csv.reader(open(csv_file_path, 'r'))) + dataset = numpy.array(csv_list) - print(index, index*dt+start, 'length :', len_imaging) + time_bool = (time_index == index).astype('int') + imaging = (time_bool*cell_bool).astype('int') + len_imaging = len(imaging[imaging > 0]) - if (len_imaging > 0) : + print(index, index*dt+start, 'length :', len_imaging) - # loop for particles - for j, data_j in enumerate(dataset) : + if (len_imaging > 0) : - # set particle position - c_id = (float(data_j[1]), float(data_j[2]), float(data_j[3])) - x_i, y_i, z_i = self.get_coordinate(c_id) + # loop for particles + for j, data_j in enumerate(dataset) : - # beam axial position - """ Distance between beam depth and fluorophore (depth) """ - dX = int(abs(X_b - x_i)) - depth = dX if dX < d[-1] else d[-1] + # set particle position + c_id = (float(data_j[1]), float(data_j[2]), float(data_j[3])) + x_i, y_i, z_i = self.get_coordinate(c_id) - # beam lateral position - """ Distance between beam position and fluorophore (plane) """ - dR = numpy.sqrt((Y_b - y_i)**2 + (Z_b - z_i)**2) - R0 = dR.reshape((Nw_pixel*Nh_pixel)) - R1 = (R0*imaging).astype('int') - radius = R1[R1 > 0] + # beam axial position + """ Distance between beam depth and fluorophore (depth) """ + dX = int(abs(X_b - x_i)) + depth = dX if dX < d[-1] else d[-1] - # set flux density as a function of radial and depth - func_emit0 = interp1d(r, N_emit0[depth], bounds_error=False, fill_value=N_emit0[depth][r[-1]]) + # beam lateral position + """ Distance between beam position and fluorophore (plane) """ + dR = numpy.sqrt((Y_b - y_i)**2 + (Z_b - z_i)**2) + R0 = dR.reshape((Nw_pixel*Nh_pixel)) + R1 = (R0*imaging).astype('int') + radius = R1[R1 > 0] - # the number of emitted photons - N_emit = func_emit0(radius) - N_emit_sum = N_emit.sum() + # set flux density as a function of radial and depth + func_emit0 = interp1d(r, N_emit0[depth], bounds_error=False, fill_value=N_emit0[depth][r[-1]]) - scan_counter[j] += (N_emit_sum/N_emit0[0][0]) + # the number of emitted photons + N_emit = func_emit0(radius) + N_emit_sum = N_emit.sum() - if (int(scan_counter[j]) < len(state[j,:])) : - state_j = state[j,int(scan_counter[j])] - else : - state_j = state[j,-1] + scan_counter[j] += (N_emit_sum/N_emit0[0][0]) - if (budget[j] > N_emit_sum) : - budget[j] = budget[j] - state_j*N_emit_sum - else : - budget[j] = 0 + if (int(scan_counter[j]) < len(state[j,:])) : + state_j = state[j,int(scan_counter[j])] + else : + state_j = state[j,-1] - scan_state[j,count_imaging-int(start/dt)] = state_j + if (budget[j] > N_emit_sum) : + budget[j] = budget[j] - state_j*N_emit_sum + else : + budget[j] = 0 + scan_state[j,count_imaging-int(start/dt)] = state_j - state_stack = numpy.column_stack((scan_state[:,count_imaging-int(start/dt)], (budget/N_emit0[0][0]).astype('int'))) - new_dataset = numpy.column_stack((dataset, state_stack)) - # write output file - output_file = output_file_dir + '/pt-%09d.0.csv' % (index) + state_stack = numpy.column_stack((scan_state[:,count_imaging-int(start/dt)], (budget/N_emit0[0][0]).astype('int'))) + new_dataset = numpy.column_stack((dataset, state_stack)) - with open(output_file, 'w') as f : - writer = csv.writer(f) - writer.writerows(new_dataset) + # write output file + output_file = output_file_dir + '/pt-%09d.0.csv' % (index) - time += exposure_time + with open(output_file, 'w') as f : + writer = csv.writer(f) + writer.writerows(new_dataset) + time += exposure_time -# def rewrite_InputData(self, output_file_dir=None) : + +# def rewrite_InputData(self, output_file_dir=None) : # -# if not os.path.exists(output_file_dir): -# os.makedirs(output_file_dir) +# if not os.path.exists(output_file_dir): +# os.makedirs(output_file_dir) # # # define observational image plane in nm-scale # voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 # -# # image dimenssion in pixel-scale -# Nw_pixel = self.configs.detector_image_size[0] -# Nh_pixel = self.configs.detector_image_size[1] +# # image dimenssion in pixel-scale +# Nw_pixel = self.configs.detector_image_size[0] +# Nh_pixel = self.configs.detector_image_size[1] # # ## cell size (nm scale) # Nz = int(self.configs.spatiocyte_lengths[2]*voxel_size) # Ny = int(self.configs.spatiocyte_lengths[1]*voxel_size) # Nx = int(self.configs.spatiocyte_lengths[0]*voxel_size) # -# # pixel length : nm/pixel -# Np = int(self.configs.image_scaling*voxel_size) +# # pixel length : nm/pixel +# Np = int(self.configs.image_scaling*voxel_size) # -# # image dimenssion in pixel-scale -# Nz_pixel = Nz/Np -# Ny_pixel = Ny/Np +# # image dimenssion in pixel-scale +# Nz_pixel = Nz/Np +# Ny_pixel = Ny/Np # # # focal point # p_0 = numpy.array([Nx, Ny, Nz])*self.configs.detector_focal_point # -# # Beam position : +# # Beam position : # beam_center = numpy.array(self.configs.detector_focal_point) # -# # exposure time -# exposure_time = self.configs.detector_exposure_time +# # exposure time +# exposure_time = self.configs.detector_exposure_time # -# # contacting time with cells vertical-axis -# R_z = float(Nz_pixel) / float(Nh_pixel) +# # contacting time with cells vertical-axis +# R_z = float(Nz_pixel) / float(Nh_pixel) # # z_exposure_time = exposure_time # z_contact_time = R_z * z_exposure_time # z_nocontact_time = (z_exposure_time - z_contact_time)/2 # -# # contacting time with cells horizontal-axis -# R_y = float(Ny_pixel) / float(Nw_pixel) +# # contacting time with cells horizontal-axis +# R_y = float(Ny_pixel) / float(Nw_pixel) # # y_exposure_time = z_exposure_time/Nw_pixel # y_contact_time = R_y * y_exposure_time # y_nocontact_time = (y_exposure_time - y_contact_time)/2 # -# # Active states per detection time -# start = self.configs.spatiocyte_start_time -# end = self.configs.spatiocyte_end_time -# dt = self.configs.spatiocyte_interval +# # Active states per detection time +# start = self.configs.spatiocyte_start_time +# end = self.configs.spatiocyte_end_time +# dt = self.configs.spatiocyte_interval +# +# # Abogadoro's number +# Na = self.effects.avogadoros_number +# +# # spatiocyte size +# radius = self.configs.spatiocyte_VoxelRadius +# volume = 4.0/3.0*numpy.pi*radius**3 +# depth = 2.0*radius # -# # Abogadoro's number -# Na = self.effects.avogadoros_number +# # Quantum yield +# QY = self.effects.quantum_yield # -# # spatiocyte size -# radius = self.configs.spatiocyte_VoxelRadius -# volume = 4.0/3.0*numpy.pi*radius**3 -# depth = 2.0*radius +# # Abs coefficient [1/(cm M)] +# abs_coeff = self.effects.abs_coefficient # -# # Quantum yield -# QY = self.effects.quantum_yield +# # photon flux density [#photons/(sec m2)] +# n0 = self.configs.source_flux_density # -# # Abs coefficient [1/(cm M)] -# abs_coeff = self.effects.abs_coefficient +# # scan time per pixel area +# dT = exposure_time/(Nh_pixel*Nw_pixel) # -# # photon flux density [#photons/(sec m2)] -# n0 = self.configs.source_flux_density +# # Cross-Section [m2] +# xsec = numpy.log(10)*(0.1*abs_coeff/Na) # -# # scan time per pixel area -# dT = exposure_time/(Nh_pixel*Nw_pixel) +# # the number of absorbed photons +# N_abs = xsec*n0*dT # -# # Cross-Section [m2] -# xsec = numpy.log(10)*(0.1*abs_coeff/Na) +# # Beer-Lambert law : A = log(I0/I) = coef * concentration * path-length +# A = (abs_coeff*0.1/Na)*(1/volume)*depth # -# # the number of absorbed photons -# N_abs = xsec*n0*dT -# -# # Beer-Lambert law : A = log(I0/I) = coef * concentration * path-length -# A = (abs_coeff*0.1/Na)*(1/volume)*depth -# -# # the number of emitted photons -# N_emit0 = QY*N_abs*(1 - 10**(-A)) +# # the number of emitted photons +# N_emit0 = QY*N_abs*(1 - 10**(-A)) # -# # sequence -# start_count = int(start/dt) -# end_count = int(end/dt) +# # sequence +# start_count = int(start/dt) +# end_count = int(end/dt) # -# # copy input file -# csv_input = self.configs.spatiocyte_file_directry + '/pt-input.csv' -# shutil.copyfile(csv_input, output_file_dir + '/pt-input.csv') +# # copy input file +# csv_input = self.configs.spatiocyte_file_directry + '/pt-input.csv' +# shutil.copyfile(csv_input, output_file_dir + '/pt-input.csv') # -# # read input file -# csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (0) -# csv_list = list(csv.reader(open(csv_file_path, 'r'))) +# # read input file +# csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (0) +# csv_list = list(csv.reader(open(csv_file_path, 'r'))) # -# # fluorescence -# if (self.effects.photobleaching_switch == True) : +# # fluorescence +# if (self.effects.photobleaching_switch == True) : # -# # get the number of particles -# N_particles = len(csv_list) +# # get the number of particles +# N_particles = len(csv_list) # -# # set fluorescence -# self.effects.set_photophysics_4lscm(start, end, dT, N_particles) +# # set fluorescence +# self.effects.set_photophysics_4lscm(start, end, dT, N_particles) # -# bleach = self.effects.fluorescence_bleach -# budget = self.effects.fluorescence_budget*N_emit0[0][0] -# state = self.effects.fluorescence_state +# bleach = self.effects.fluorescence_bleach +# budget = self.effects.fluorescence_budget*N_emit0[0][0] +# state = self.effects.fluorescence_state # -# scan_state = numpy.copy(state*0) -# scan_counter = numpy.zeros(shape=(N_particles)) +# scan_state = numpy.copy(state*0) +# scan_counter = numpy.zeros(shape=(N_particles)) # -# # initialization -# for count in range(start_count, end_count, 1) : +# # initialization +# for count in range(start_count, end_count, 1) : # -# csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (count) +# csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (count) # -# csv_list = list(csv.reader(open(csv_file_path, 'r'))) -# dataset = numpy.array(csv_list) +# csv_list = list(csv.reader(open(csv_file_path, 'r'))) +# dataset = numpy.array(csv_list) # -# state_stack = numpy.column_stack((scan_state[:,count-start_count], (budget/N_emit0[0][0]).astype('int'))) -# new_dataset = numpy.column_stack((dataset, state_stack)) +# state_stack = numpy.column_stack((scan_state[:,count-start_count], (budget/N_emit0[0][0]).astype('int'))) +# new_dataset = numpy.column_stack((dataset, state_stack)) # -# # write output file -# output_file = output_file_dir + '/pt-%09d.0.csv' % (count) +# # write output file +# output_file = output_file_dir + '/pt-%09d.0.csv' % (count) # -# with open(output_file, 'w') as f : -# writer = csv.writer(f) -# writer.writerows(new_dataset) +# with open(output_file, 'w') as f : +# writer = csv.writer(f) +# writer.writerows(new_dataset) # # -# # loop for scannig -# time = start +# # loop for scannig +# time = start # # while (time < end) : # -# print 'time : ', time, ' sec/image' +# print 'time : ', time, ' sec/image' # -# # Beam position : initial -# p_b = numpy.array([Nx, Ny, Nz])*beam_center +# # Beam position : initial +# p_b = numpy.array([Nx, Ny, Nz])*beam_center # -# # contact time : z-direction -# z_scan_time = 0 +# # contact time : z-direction +# z_scan_time = 0 # -# # no contact time : z-direction -# non_contact_time = z_nocontact_time +# # no contact time : z-direction +# non_contact_time = z_nocontact_time # -# # vertical-scanning sequences -# while (z_scan_time < z_contact_time) : +# # vertical-scanning sequences +# while (z_scan_time < z_contact_time) : # -# # Beam position : z-direction -# beam_center[2] = z_scan_time/z_contact_time +# # Beam position : z-direction +# beam_center[2] = z_scan_time/z_contact_time # -# # contact time : y-direction -# y_scan_time = 0 +# # contact time : y-direction +# y_scan_time = 0 # -# # no contact time : y-direction (left-margin) -# non_contact_time += y_nocontact_time +# # no contact time : y-direction (left-margin) +# non_contact_time += y_nocontact_time # -# # horizontal-scanning sequences -# while (y_scan_time < y_contact_time) : +# # horizontal-scanning sequences +# while (y_scan_time < y_contact_time) : # -# # Beam position : y-direction (cell in nm-scale) -# beam_center[1] = y_scan_time/y_contact_time +# # Beam position : y-direction (cell in nm-scale) +# beam_center[1] = y_scan_time/y_contact_time # -# p_b = numpy.array([Nx, Ny, Nz])*beam_center -# x_b, y_b, z_b = p_b +# p_b = numpy.array([Nx, Ny, Nz])*beam_center +# x_b, y_b, z_b = p_b # -# # get scan-time and scan-count number -# scan_time = z_scan_time + y_scan_time + non_contact_time +# # get scan-time and scan-count number +# scan_time = z_scan_time + y_scan_time + non_contact_time # -# # get image count number -# count = int((scan_time + time)/dt) +# # get image count number +# count = int((scan_time + time)/dt) # -# # read input file -# csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (count) +# # read input file +# csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (count) # -# csv_list = list(csv.reader(open(csv_file_path, 'r'))) -# dataset = numpy.array(csv_list) +# csv_list = list(csv.reader(open(csv_file_path, 'r'))) +# dataset = numpy.array(csv_list) # # # loop for particles # for j, data_j in enumerate(dataset) : # -# # set particle position -# c_id = (float(data_j[1]), float(data_j[2]), float(data_j[3])) -# x_i, y_i, z_i = self.get_coordinate(c_id) +# # set particle position +# c_id = (float(data_j[1]), float(data_j[2]), float(data_j[3])) +# x_i, y_i, z_i = self.get_coordinate(c_id) # -# # beam axial position +# # beam axial position # """ Distance between beam and fluorephore (depth) """ -# d_s = abs(x_i - x_b) +# d_s = abs(x_i - x_b) # # if (d_s < 20000) : -# source_depth = d_s +# source_depth = d_s # else : -# source_depth = 19999 +# source_depth = 19999 # -# # beam position +# # beam position # """ Distance between beam position and fluorophore (plane) """ -# rr = numpy.sqrt((y_i-y_b)**2 + (z_i-z_b)**2) +# rr = numpy.sqrt((y_i-y_b)**2 + (z_i-z_b)**2) # -# if (rr < 20000) : -# source_radius = rr -# else : -# source_radius = 19999 +# if (rr < 20000) : +# source_radius = rr +# else : +# source_radius = 19999 # -# N_emit = N_emit0[int(source_depth)][int(source_radius)] +# N_emit = N_emit0[int(source_depth)][int(source_radius)] # -# scan_counter[j] += (N_emit/N_emit0[0][0]) -# state_j = state[j,int(scan_counter[j])] +# scan_counter[j] += (N_emit/N_emit0[0][0]) +# state_j = state[j,int(scan_counter[j])] # -# photons = budget[j] - N_emit*state_j +# photons = budget[j] - N_emit*state_j # -# check = (photons > 0).astype('int') -# budget[j] = numpy.abs(photons*check) +# check = (photons > 0).astype('int') +# budget[j] = numpy.abs(photons*check) # -# scan_state[j,count-start_count] = state_j +# scan_state[j,count-start_count] = state_j # # -# state_stack = numpy.column_stack((scan_state[:,count-start_count], (budget/N_emit0[0][0]).astype('int'))) -# new_dataset = numpy.column_stack((dataset, state_stack)) +# state_stack = numpy.column_stack((scan_state[:,count-start_count], (budget/N_emit0[0][0]).astype('int'))) +# new_dataset = numpy.column_stack((dataset, state_stack)) # -# # write output file -# output_file = output_file_dir + '/pt-%09d.0.csv' % (count) +# # write output file +# output_file = output_file_dir + '/pt-%09d.0.csv' % (count) # -# with open(output_file, 'w') as f : -# writer = csv.writer(f) -# writer.writerows(new_dataset) +# with open(output_file, 'w') as f : +# writer = csv.writer(f) +# writer.writerows(new_dataset) # # -# y_scan_time += y_contact_time/Ny_pixel +# y_scan_time += y_contact_time/Ny_pixel # -# # no contact time : y-direction (right-margin) -# non_contact_time += y_nocontact_time +# # no contact time : y-direction (right-margin) +# non_contact_time += y_nocontact_time # -# z_scan_time += z_contact_time/Nz_pixel +# z_scan_time += z_contact_time/Nz_pixel # -# time += exposure_time +# time += exposure_time # # # # -# def get_molecule_plane(self, cell, data, pid, p_b, p_0) : +# def get_molecule_plane(self, cell, data, pid, p_b, p_0) : # -# # get beam position -# X_b, Y_b, Z_b = p_b +# # get beam position +# X_b, Y_b, Z_b = p_b # -# # particles coordinate, species and lattice IDs +# # particles coordinate, species and lattice IDs # c_id, s_id, l_id, p_state, cyc_id = data -# -# sid_array = numpy.array(self.configs.spatiocyte_species_id) -# s_index = (numpy.abs(sid_array - int(s_id))).argmin() # -# if self.configs.spatiocyte_observables[s_index] is True : +# sid_array = numpy.array(self.configs.spatiocyte_species_id) +# s_index = (numpy.abs(sid_array - int(s_id))).argmin() # -# # particles coordinate in nm-scale +# if self.configs.spatiocyte_observables[s_index] is True : +# +# # particles coordinate in nm-scale # p_i = self.get_coordinate(c_id) # # # get signal matrix @@ -945,131 +945,131 @@ def rewrite_InputData(self, output_file_dir=None) : # signal = self.get_signal(p_i, p_b, p_0, p_state) # # # add signal matrix to image plane -# self.overwrite_signal(cell, signal, p_i, p_b) +# self.overwrite_signal(cell, signal, p_i, p_b) # # # - def get_signal(self, p_i, p_b, p_0, p_state, pinhole) : + def get_signal(self, p_i, p_b, p_0, p_state, pinhole) : - # set focal point - x_0, y_0, z_0 = p_0 + # set focal point + x_0, y_0, z_0 = p_0 - # set source center - x_b, y_b, z_b = p_b + # set source center + x_b, y_b, z_b = p_b - # set particle position - x_i, y_i, z_i = p_i + # set particle position + x_i, y_i, z_i = p_i - # image dimenssion in pixel-scale - Nw_pixel = self.configs.detector_image_size[0] - Nh_pixel = self.configs.detector_image_size[1] + # image dimenssion in pixel-scale + Nw_pixel = self.configs.detector_image_size[0] + Nh_pixel = self.configs.detector_image_size[1] - # scan time - exposure_time = self.configs.detector_exposure_time - dT = exposure_time/(Nw_pixel*Nh_pixel) + # scan time + exposure_time = self.configs.detector_exposure_time + dT = exposure_time/(Nw_pixel*Nh_pixel) - # set arrays of radial and depth - r = numpy.linspace(0, 20000, 20001) - d = numpy.linspace(0, 20000, 20001) + # set arrays of radial and depth + r = numpy.linspace(0, 20000, 20001) + d = numpy.linspace(0, 20000, 20001) - # axial position - """ Distance between beam and fluorefore (depth) """ - dx = abs(x_b - x_i) - depth = dx if dx < d[-1] else d[-1] + # axial position + """ Distance between beam and fluorefore (depth) """ + dx = abs(x_b - x_i) + depth = dx if dx < d[-1] else d[-1] - # lateral position - """ Distance between beam position and fluorophore (plane) """ - radius = numpy.sqrt((y_b - y_i)**2 + (z_b - z_i)**2) + # lateral position + """ Distance between beam position and fluorophore (plane) """ + radius = numpy.sqrt((y_b - y_i)**2 + (z_b - z_i)**2) - # get illumination PSF - n0 = self.configs.source_flux_density + # get illumination PSF + n0 = self.configs.source_flux_density - # Absorption coeff [1/(cm M)] - abs_coeff = self.effects.abs_coefficient + # Absorption coeff [1/(cm M)] + abs_coeff = self.effects.abs_coefficient - # Quantum yield - QY = self.effects.quantum_yield + # Quantum yield + QY = self.effects.quantum_yield - # Abogadoro's number - Na = self.effects.avogadoros_number + # Abogadoro's number + Na = self.effects.avogadoros_number - # Cross-section [m2] - x_sec = numpy.log(10)*abs_coeff*0.1/Na + # Cross-section [m2] + x_sec = numpy.log(10)*abs_coeff*0.1/Na - # get the number of absorption photons : [#/(m2 sec)] [m2] [sec] - N_abs = n0*x_sec*dT + # get the number of absorption photons : [#/(m2 sec)] [m2] [sec] + N_abs = n0*x_sec*dT - # spatiocyte size - voxel_radius = self.configs.spatiocyte_VoxelRadius - voxel_volume = (4.0/3.0)*numpy.pi*voxel_radius**3 - voxel_depth = 2.0*voxel_radius + # spatiocyte size + voxel_radius = self.configs.spatiocyte_VoxelRadius + voxel_volume = (4.0/3.0)*numpy.pi*voxel_radius**3 + voxel_depth = 2.0*voxel_radius - # Beer-Lamberts law : log(I0/I) = A = abs coef. * concentration * path length ([m2] * [#/m3] * [m]) - A = (abs_coeff*0.1/Na)*(1.0/voxel_volume)*(voxel_depth) + # Beer-Lamberts law : log(I0/I) = A = abs coef. * concentration * path length ([m2] * [#/m3] * [m]) + A = (abs_coeff*0.1/Na)*(1.0/voxel_volume)*(voxel_depth) - # get the number of photons emitted - N_emit0 = p_state*QY*N_abs*(1 - 10**(-A)) - #func_emit = interp1d(r, N_emit0[depth], bounds_error=False, fill_value=N_emit0[depth][r[-1]]) - #func_emit = UnivariateSpline(r, N_emit0[depth]) - func_emit = InterpolatedUnivariateSpline(r, N_emit0[depth]) + # get the number of photons emitted + N_emit0 = p_state*QY*N_abs*(1 - 10**(-A)) + #func_emit = interp1d(r, N_emit0[depth], bounds_error=False, fill_value=N_emit0[depth][r[-1]]) + #func_emit = UnivariateSpline(r, N_emit0[depth]) + func_emit = InterpolatedUnivariateSpline(r, N_emit0[depth]) - N_emit = func_emit(radius) + N_emit = func_emit(radius) - # get fluorophore PSF - d_fluo = self.configs.depth - dx = abs(x_i - x_0) - depth_fluo = dx if dx < d_fluo[-1] else d_fluo[-1] + # get fluorophore PSF + d_fluo = self.configs.depth + dx = abs(x_i - x_0) + depth_fluo = dx if dx < d_fluo[-1] else d_fluo[-1] - fluo_psf = self.fluo_psf[int(depth_fluo)] + fluo_psf = self.fluo_psf[int(depth_fluo)] - # set a function of pinhole region cut - Nr = len(pinhole)/2 + # set a function of pinhole region cut + Nr = len(pinhole)/2 - dy0, dz0 = (y_b - y_i) - Nr, (z_b - z_i) - Nr - dy1, dz1 = (y_b - y_i) + Nr, (z_b - z_i) + Nr + dy0, dz0 = (y_b - y_i) - Nr, (z_b - z_i) - Nr + dy1, dz1 = (y_b - y_i) + Nr, (z_b - z_i) + Nr - y_psf = numpy.linspace(fluo_psf[0,0], fluo_psf[-1,0], len(fluo_psf[:,0])) - z_psf = numpy.linspace(fluo_psf[0,0], fluo_psf[0,-1], len(fluo_psf[0,:])) + y_psf = numpy.linspace(fluo_psf[0,0], fluo_psf[-1,0], len(fluo_psf[:,0])) + z_psf = numpy.linspace(fluo_psf[0,0], fluo_psf[0,-1], len(fluo_psf[0,:])) - func_psf = interp2d(y_psf, z_psf, fluo_psf, bounds_error=False, fill_value=fluo_psf[-1,-1]) - #func_psf = RectBivariateSpline(y_psf, z_psf, fluo_psf) + func_psf = interp2d(y_psf, z_psf, fluo_psf, bounds_error=False, fill_value=fluo_psf[-1,-1]) + #func_psf = RectBivariateSpline(y_psf, z_psf, fluo_psf) - # set signal array - dy = [numpy.linspace(dy0[k], dy1[k], 2*Nr) for k in range(len(radius))] - dz = [numpy.linspace(dz0[k], dz1[k], 2*Nr) for k in range(len(radius))] + # set signal array + dy = [numpy.linspace(dy0[k], dy1[k], 2*Nr) for k in range(len(radius))] + dz = [numpy.linspace(dz0[k], dz1[k], 2*Nr) for k in range(len(radius))] - signal = numpy.array(map(lambda Y, Z, N : N*pinhole*func_psf(Y, Z), dy, dz, N_emit)) - photons = signal.sum(-1).sum(-1) + signal = numpy.array(map(lambda Y, Z, N : N*pinhole*func_psf(Y, Z), dy, dz, N_emit)) + photons = signal.sum(-1).sum(-1) - return photons + return photons -# def overwrite_signal(self, cell, signal, p_i, p_b) : +# def overwrite_signal(self, cell, signal, p_i, p_b) : # -# # particle position -# x_i, y_i, z_i = p_i +# # particle position +# x_i, y_i, z_i = p_i # -# # beam position -# x_b, y_b, z_b = p_b +# # beam position +# x_b, y_b, z_b = p_b # -# # z-axis -# Nz_cell = len(cell) -# Nz_signal = len(signal) +# # z-axis +# Nz_cell = len(cell) +# Nz_signal = len(signal) # -# Nh_cell = Nz_cell/2 -# Nh_signal = (Nz_signal-1)/2 +# Nh_cell = Nz_cell/2 +# Nh_signal = (Nz_signal-1)/2 # # z_to = (z_i + Nh_signal) - (z_b - Nh_cell) # z_from = (z_i - Nh_signal) - (z_b - Nh_cell) # -# flag_z = True +# flag_z = True # -# if (z_to > Nz_cell + Nz_signal) : -# flag_z = False +# if (z_to > Nz_cell + Nz_signal) : +# flag_z = False # # elif (z_to > Nz_cell and -# z_to < Nz_cell + Nz_signal) : +# z_to < Nz_cell + Nz_signal) : # # dz_to = z_to - Nz_cell # @@ -1081,7 +1081,7 @@ def get_signal(self, p_i, p_b, p_0, p_state, pinhole) : # zi_to = int(Nz_signal) # zb_to = int(z_to) # -# else : flag_z = False +# else : flag_z = False # # if (z_from < 0) : # @@ -1093,7 +1093,7 @@ def get_signal(self, p_i, p_b, p_0, p_state, pinhole) : # zi_from = 0 # zb_from = int(z_from) # -# if (flag_z == True) : +# if (flag_z == True) : # ddz = (zi_to - zi_from) - (zb_to - zb_from) # # if (ddz > 0) : zi_to = zi_to - ddz @@ -1103,19 +1103,19 @@ def get_signal(self, p_i, p_b, p_0, p_state, pinhole) : # Ny_cell = cell.size/Nz_cell # Ny_signal = signal.size/Nz_signal # -# Nh_cell = Ny_cell/2 -# Nh_signal = (Ny_signal-1)/2 +# Nh_cell = Ny_cell/2 +# Nh_signal = (Ny_signal-1)/2 # # y_to = (y_i + Nh_signal) - (y_b - Nh_cell) # y_from = (y_i - Nh_signal) - (y_b - Nh_cell) # -# flag_y = True +# flag_y = True # -# if (y_to > Ny_cell + Ny_signal) : -# flag_y = False +# if (y_to > Ny_cell + Ny_signal) : +# flag_y = False # # elif (y_to > Ny_cell and -# y_to < Ny_cell + Ny_signal) : +# y_to < Ny_cell + Ny_signal) : # # dy_to = y_to - Ny_cell # @@ -1127,7 +1127,7 @@ def get_signal(self, p_i, p_b, p_0, p_state, pinhole) : # yi_to = int(Ny_signal) # yb_to = int(y_to) # -# else : flag_y = False +# else : flag_y = False # # if (y_from < 0) : # @@ -1139,262 +1139,262 @@ def get_signal(self, p_i, p_b, p_0, p_state, pinhole) : # yi_from = 0 # yb_from = int(y_from) # -# if (flag_y == True) : -# ddy = (yi_to - yi_from) - (yb_to - yb_from) +# if (flag_y == True) : +# ddy = (yi_to - yi_from) - (yb_to - yb_from) # -# if (ddy > 0) : yi_to = yi_to - ddy -# if (ddy < 0) : yb_to = yb_to + ddy +# if (ddy > 0) : yi_to = yi_to - ddy +# if (ddy < 0) : yb_to = yb_to + ddy # -# if (flag_z == True and flag_y == True) : -## if (abs(ddy) > 2 or abs(ddz) > 2) : -## print zi_from, zi_to, yi_from, yi_to, ddy, ddz -## print zb_from, zb_to, yb_from, yb_to +# if (flag_z == True and flag_y == True) : +## if (abs(ddy) > 2 or abs(ddz) > 2) : +## print zi_from, zi_to, yi_from, yi_to, ddy, ddz +## print zb_from, zb_to, yb_from, yb_to # -# # add to cellular plane +# # add to cellular plane # cell[zb_from:zb_to, yb_from:yb_to] += signal[zi_from:zi_to, yi_from:yi_to] # -# #return cell +# #return cell # # # - def output_frames(self, num_div=1): + def output_frames(self, num_div=1): - # set Fluorophores PSF - self.set_fluo_psf() + # set Fluorophores PSF + self.set_fluo_psf() - index_array = numpy.array(self.configs.shutter_index_array) - num_timesteps = len(index_array) - index0 = index_array[0] + index_array = numpy.array(self.configs.shutter_index_array) + num_timesteps = len(index_array) + index0 = index_array[0] - envname = 'ECELL_MICROSCOPE_SINGLE_PROCESS' + envname = 'ECELL_MICROSCOPE_SINGLE_PROCESS' - if envname in os.environ and os.environ[envname]: - self.output_frames_each_process(index0, num_timesteps) - else: - num_processes = multiprocessing.cpu_count() - n, m = divmod(num_timesteps, num_processes) - # when 10 tasks is distributed to 4 processes, - # number of tasks of each process must be [3, 3, 2, 2] - chunks = [n + 1 if i < m else n for i in range(num_processes)] + if envname in os.environ and os.environ[envname]: + self.output_frames_each_process(index0, num_timesteps) + else: + num_processes = multiprocessing.cpu_count() + n, m = divmod(num_timesteps, num_processes) + # when 10 tasks is distributed to 4 processes, + # number of tasks of each process must be [3, 3, 2, 2] + chunks = [n + 1 if i < m else n for i in range(num_processes)] - processes = [] - start_index = index0 + processes = [] + start_index = index0 - for chunk in chunks: - stop_index = start_index + chunk + for chunk in chunks: + stop_index = start_index + chunk - process = multiprocessing.Process( - target=self.output_frames_each_process, - args=(start_index, stop_index)) - process.start() - processes.append(process) - start_index = stop_index + process = multiprocessing.Process( + target=self.output_frames_each_process, + args=(start_index, stop_index)) + process.start() + processes.append(process) + start_index = stop_index - for process in processes: - process.join() + for process in processes: + process.join() - def output_frames_each_process(self, start_count, end_count): + def output_frames_each_process(self, start_count, end_count): - voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 + voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 - # image dimenssion in pixel-scale - Nw_pixel = self.configs.detector_image_size[0] - Nh_pixel = self.configs.detector_image_size[1] + # image dimenssion in pixel-scale + Nw_pixel = self.configs.detector_image_size[0] + Nh_pixel = self.configs.detector_image_size[1] - # cells dimenssion in nm-scale - Nz = int(self.configs.spatiocyte_lengths[2]*voxel_size) - Ny = int(self.configs.spatiocyte_lengths[1]*voxel_size) - Nx = int(self.configs.spatiocyte_lengths[0]*voxel_size) + # cells dimenssion in nm-scale + Nz = int(self.configs.spatiocyte_lengths[2]*voxel_size) + Ny = int(self.configs.spatiocyte_lengths[1]*voxel_size) + Nx = int(self.configs.spatiocyte_lengths[0]*voxel_size) - # pixel length : nm/pixel - Np = int(self.configs.image_scaling*voxel_size) + # pixel length : nm/pixel + Np = int(self.configs.image_scaling*voxel_size) - # cells dimenssion in pixel-scale - Ny_pixel = Ny/Np - Nz_pixel = Nz/Np + # cells dimenssion in pixel-scale + Ny_pixel = Ny/Np + Nz_pixel = Nz/Np - # get focal point - p_0 = self.get_focal_center() + # get focal point + p_0 = self.get_focal_center() - # Mesh-grid for pixel-image - w = numpy.linspace(0, Nw_pixel-1, Nw_pixel) - h = numpy.linspace(0, Nh_pixel-1, Nh_pixel) + # Mesh-grid for pixel-image + w = numpy.linspace(0, Nw_pixel-1, Nw_pixel) + h = numpy.linspace(0, Nh_pixel-1, Nh_pixel) - W, H = numpy.meshgrid(w, h) + W, H = numpy.meshgrid(w, h) - # exposure time - exposure_time = self.configs.detector_exposure_time + # exposure time + exposure_time = self.configs.detector_exposure_time - # spatiocyte time interval - data_interval = self.configs.spatiocyte_interval + # spatiocyte time interval + data_interval = self.configs.spatiocyte_interval - # set delta_index - delta_index = int(round(exposure_time/data_interval)) + # set delta_index + delta_index = int(round(exposure_time/data_interval)) - # scan-time (sec) per pixel-image - dt_w = exposure_time/(Nh_pixel*Nw_pixel) - dt_h = dt_w*Nw_pixel + # scan-time (sec) per pixel-image + dt_w = exposure_time/(Nh_pixel*Nw_pixel) + dt_h = dt_w*Nw_pixel - T_b = W*dt_w + H*dt_h - time = exposure_time*start_count + T_b.reshape((Nw_pixel*Nh_pixel)) + T_b = W*dt_w + H*dt_h + time = exposure_time*start_count + T_b.reshape((Nw_pixel*Nh_pixel)) - # cell-center in pixel-image - W0, H0 = Nw_pixel/2, Nh_pixel/2 + # cell-center in pixel-image + W0, H0 = Nw_pixel/2, Nh_pixel/2 - # cell in pixel-image - w0, h0 = W0 - int(Ny_pixel/2), H0 - int(Nz_pixel/2) - w1, h1 = w0 + Ny_pixel, w0 + Nz_pixel + # cell in pixel-image + w0, h0 = W0 - int(Ny_pixel/2), H0 - int(Nz_pixel/2) + w1, h1 = w0 + Ny_pixel, w0 + Nz_pixel - # cell-located region in pixel image - cell = numpy.zeros((Nw_pixel, Nh_pixel)) - cell[w0:w1,h0:h1] = 1 - cell_bool = cell.reshape((Nw_pixel*Nh_pixel)) + # cell-located region in pixel image + cell = numpy.zeros((Nw_pixel, Nh_pixel)) + cell[w0:w1,h0:h1] = 1 + cell_bool = cell.reshape((Nw_pixel*Nh_pixel)) - # beam-position (nm) relative to cell-origin - Y_b, Z_b = numpy.meshgrid(Np*(w-w0), Np*(h-h0)) - x_b = Nx*self.configs.detector_focal_point[0] + # beam-position (nm) relative to cell-origin + Y_b, Z_b = numpy.meshgrid(Np*(w-w0), Np*(h-h0)) + x_b = Nx*self.configs.detector_focal_point[0] - # set radial and depth arrays - r = self.configs.radial - d = self.configs.depth + # set radial and depth arrays + r = self.configs.radial + d = self.configs.depth - # loop for scannig - index_array = numpy.array(self.configs.shutter_count_array) - index0 = index_array[0] + # loop for scannig + index_array = numpy.array(self.configs.shutter_count_array) + index0 = index_array[0] - if (time[-1] < exposure_time*end_count) : + if (time[-1] < exposure_time*end_count) : - # set count number - count_imaging = int(round(time[0]/exposure_time)) + # set count number + count_imaging = int(round(time[0]/exposure_time)) - print('time : ', time[0], '-', time[-1], ' sec/image (', count_imaging, ')') + print('time : ', time[0], '-', time[-1], ' sec/image (', count_imaging, ')') - image_file_name = os.path.join(self.configs.image_file_dir, - self.configs.image_file_name_format % (count_imaging)) + image_file_name = os.path.join(self.configs.image_file_dir, + self.configs.image_file_name_format % (count_imaging)) - # set frame datasets - index1 = int(round(time[0]/data_interval)) - index_start = (index1 - index0) - index_end = (index1 - index0) + delta_index + # set frame datasets + index1 = int(round(time[0]/data_interval)) + index_start = (index1 - index0) + index_end = (index1 - index0) + delta_index - frame_data = self.configs.spatiocyte_data[index_start:index_end] + frame_data = self.configs.spatiocyte_data[index_start:index_end] - print('data length :', len(frame_data), 'index :', index0, index1, index_start, index_end) + print('data length :', len(frame_data), 'index :', index0, index1, index_start, index_end) - # set time_index - time_index = (time/data_interval).astype('int') + # set time_index + time_index = (time/data_interval).astype('int') - # define image in pixel-scale - image_pixel = numpy.zeros(shape=(Nw_pixel, Nh_pixel, 2)) - image_array0 = image_pixel[:,:,0].reshape((Nw_pixel*Nh_pixel)) + # define image in pixel-scale + image_pixel = numpy.zeros(shape=(Nw_pixel, Nh_pixel, 2)) + image_array0 = image_pixel[:,:,0].reshape((Nw_pixel*Nh_pixel)) - if (len(frame_data) > 0) : + if (len(frame_data) > 0) : - # loop for scanning - for index in range(index_start, index_end, 1) : + # loop for scanning + for index in range(index_start, index_end, 1) : - # set cell-imaging region - time_bool = (time_index == index + index0).astype('int') - imaging = (time_bool*cell_bool).astype('int') + # set cell-imaging region + time_bool = (time_index == index + index0).astype('int') + imaging = (time_bool*cell_bool).astype('int') - len_imaging = len(imaging[imaging > 0]) + len_imaging = len(imaging[imaging > 0]) - print(index+index0, (index+index0)*data_interval, 'length :', len_imaging) + print(index+index0, (index+index0)*data_interval, 'length :', len_imaging) - if (len_imaging > 0) : + if (len_imaging > 0) : - # set frame_data - i_time, i_data = frame_data[index-index_start] + # set frame_data + i_time, i_data = frame_data[index-index_start] - # set beam position array - yy0 = Y_b.reshape((Nw_pixel*Nh_pixel)) - y_b = yy0[imaging > 0] + # set beam position array + yy0 = Y_b.reshape((Nw_pixel*Nh_pixel)) + y_b = yy0[imaging > 0] - zz0 = Z_b.reshape((Nw_pixel*Nh_pixel)) - z_b = zz0[imaging > 0] + zz0 = Z_b.reshape((Nw_pixel*Nh_pixel)) + z_b = zz0[imaging > 0] - # set the number of emitted photons - photons = numpy.zeros(shape=(len_imaging)) + # set the number of emitted photons + photons = numpy.zeros(shape=(len_imaging)) - # set pinhole in nm-scale - Mag = self.configs.image_magnification - Nr = int(self.configs.pinhole_radius/Mag/1e-9) + # set pinhole in nm-scale + Mag = self.configs.image_magnification + Nr = int(self.configs.pinhole_radius/Mag/1e-9) - pinhole = numpy.zeros(shape=(2*Nr,2*Nr)) + pinhole = numpy.zeros(shape=(2*Nr,2*Nr)) - zz, yy = numpy.ogrid[-Nr:Nr,-Nr:Nr] - rr_cut = yy**2 + zz**2 < Nr**2 - pinhole[rr_cut] = 1 + zz, yy = numpy.ogrid[-Nr:Nr,-Nr:Nr] + rr_cut = yy**2 + zz**2 < Nr**2 + pinhole[rr_cut] = 1 - # loop for particles - for j, data_j in enumerate(i_data) : + # loop for particles + for j, data_j in enumerate(i_data) : - # particles coordinate, species and lattice IDs - c_id, m_id, s_id, l_id, p_state, cyc_id = data_j + # particles coordinate, species and lattice IDs + c_id, m_id, s_id, l_id, p_state, cyc_id = data_j - sid_array = numpy.array(self.configs.spatiocyte_species_id) - s_index = (numpy.abs(sid_array - int(s_id))).argmin() + sid_array = numpy.array(self.configs.spatiocyte_species_id) + s_index = (numpy.abs(sid_array - int(s_id))).argmin() - if self.configs.spatiocyte_observables[s_index] is True : + if self.configs.spatiocyte_observables[s_index] is True : - if (p_state > 0) : - p_i = numpy.array(c_id)/1e-9 + if (p_state > 0) : + p_i = numpy.array(c_id)/1e-9 - # particles coordinate in real(nm) scale - p_i, radial, depth = self.get_coordinate(p_i, p_0) - x_i, y_i, z_i = p_i + # particles coordinate in real(nm) scale + p_i, radial, depth = self.get_coordinate(p_i, p_0) + x_i, y_i, z_i = p_i - # set beam positional array in nm-scale - r_cut = numpy.sqrt((y_b - y_i)**2 + (z_b - z_i)**2) < 1.5*Nr - d_cut = abs(x_b - x_i) < 600 #d[-1] + # set beam positional array in nm-scale + r_cut = numpy.sqrt((y_b - y_i)**2 + (z_b - z_i)**2) < 1.5*Nr + d_cut = abs(x_b - x_i) < 600 #d[-1] - p_b = (x_b, y_b[r_cut], z_b[r_cut]) + p_b = (x_b, y_b[r_cut], z_b[r_cut]) - if (len(y_b[r_cut]) > 0 and d_cut) : - # add photon signals per beam positional array - photons[r_cut] += self.get_signal(p_i, p_b, p_0, p_state, pinhole) + if (len(y_b[r_cut]) > 0 and d_cut) : + # add photon signals per beam positional array + photons[r_cut] += self.get_signal(p_i, p_b, p_0, p_state, pinhole) - # set the number of emitted photons to pixel-image - image_array0[imaging > 0] = photons + # set the number of emitted photons to pixel-image + image_array0[imaging > 0] = photons - # get pixel-image - image_pixel[:,:,0] = image_array0.reshape((Nw_pixel, Nh_pixel)) - image = self.detector_output(image_pixel) + # get pixel-image + image_pixel[:,:,0] = image_array0.reshape((Nw_pixel, Nh_pixel)) + image = self.detector_output(image_pixel) - # save data to numpy-binary file - image_file_name = os.path.join(self.configs.image_file_dir, - self.configs.image_file_name_format % (count_imaging)) - numpy.save(image_file_name, image) + # save data to numpy-binary file + image_file_name = os.path.join(self.configs.image_file_dir, + self.configs.image_file_name_format % (count_imaging)) + numpy.save(image_file_name, image) # def output_frames_each_process(self, start_count, stop_count): # -# voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 +# voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 # -# # image dimenssion in pixel-scale -# Nw_pixel = self.configs.detector_image_size[0] -# Nh_pixel = self.configs.detector_image_size[1] +# # image dimenssion in pixel-scale +# Nw_pixel = self.configs.detector_image_size[0] +# Nh_pixel = self.configs.detector_image_size[1] # -# # cells dimenssion in nm-scale +# # cells dimenssion in nm-scale # Nz = int(self.configs.spatiocyte_lengths[2] * voxel_size) # Ny = int(self.configs.spatiocyte_lengths[1] * voxel_size) # Nx = int(self.configs.spatiocyte_lengths[0] * voxel_size) # -# # pixel length : nm/pixel -# Np = int(self.configs.image_scaling*voxel_size) +# # pixel length : nm/pixel +# Np = int(self.configs.image_scaling*voxel_size) # -# # cells dimenssion in pixel-scale -# Ny_pixel = Ny/Np -# Nz_pixel = Nz/Np +# # cells dimenssion in pixel-scale +# Ny_pixel = Ny/Np +# Nz_pixel = Nz/Np # # # focal point # p_0 = numpy.array([Nx, Ny, Nz])*self.configs.detector_focal_point # -# # Beam position : +# # Beam position : # beam_center = numpy.array(self.configs.detector_focal_point) # ## # set boundary condition @@ -1403,24 +1403,24 @@ def output_frames_each_process(self, start_count, end_count): ## bc = numpy.zeros(shape=(Nz, Ny)) ## bc = self.set_boundary_plane(bc, p_b, p_0) # -# # exposure time -# exposure_time = self.configs.detector_exposure_time +# # exposure time +# exposure_time = self.configs.detector_exposure_time # -# # contacting time with cells vertical-axis -# R_z = float(Nz_pixel) / float(Nh_pixel) +# # contacting time with cells vertical-axis +# R_z = float(Nz_pixel) / float(Nh_pixel) # # z_exposure_time = exposure_time # z_contact_time = R_z * z_exposure_time # z_nocontact_time = (z_exposure_time - z_contact_time)/2 # -# # contacting time with cells horizontal-axis -# R_y = float(Ny_pixel) / float(Nw_pixel) +# # contacting time with cells horizontal-axis +# R_y = float(Ny_pixel) / float(Nw_pixel) # # y_exposure_time = z_exposure_time/Nw_pixel # y_contact_time = R_y * y_exposure_time # y_nocontact_time = (y_exposure_time - y_contact_time)/2 # -# ##### +# ##### # start_time = self.configs.spatiocyte_start_time # # time = exposure_time * start_count @@ -1444,225 +1444,225 @@ def output_frames_each_process(self, start_count, end_count): # image_file_name = os.path.join(self.configs.image_file_dir, # self.configs.image_file_name_format % (count)) # -# print 'time : ', time, ' sec (', count, ')' +# print 'time : ', time, ' sec (', count, ')' # # # define cell in nm-scale # #cell = numpy.zeros(shape=(Nz, Ny)) -# # define image array in pixel-scale -# image_pixel = numpy.zeros([Nw_pixel, Nh_pixel, 2]) +# # define image array in pixel-scale +# image_pixel = numpy.zeros([Nw_pixel, Nh_pixel, 2]) # -# count_start = (count - count0)*delta_time -# count_end = (count - count0 + 1)*delta_time +# count_start = (count - count0)*delta_time +# count_end = (count - count0 + 1)*delta_time # # frame_data = self.configs.spatiocyte_data[count_start:count_end] # -# if (len(frame_data) > 0) : +# if (len(frame_data) > 0) : # -# # Beam position : initial -# p_b = numpy.array([Nx, Ny, Nz])*beam_center +# # Beam position : initial +# p_b = numpy.array([Nx, Ny, Nz])*beam_center # -# # Beam position : z-direction (image in pixel-scale) -# z_image_pixel = int(z_nocontact_time/y_exposure_time) +# # Beam position : z-direction (image in pixel-scale) +# z_image_pixel = int(z_nocontact_time/y_exposure_time) # -# # contact time : z-direction -# z_scan_time = 0 +# # contact time : z-direction +# z_scan_time = 0 # -# # no contact time : z-direction -# non_contact_time = z_nocontact_time +# # no contact time : z-direction +# non_contact_time = z_nocontact_time # -# # vertical-scanning sequences -# while (z_scan_time < z_contact_time) : +# # vertical-scanning sequences +# while (z_scan_time < z_contact_time) : # -# # Beam position : z-direction -# beam_center[2] = z_scan_time/z_contact_time +# # Beam position : z-direction +# beam_center[2] = z_scan_time/z_contact_time # -# # Beam position : y-direction (image in pixel-scale) -# y_image_pixel = int(y_nocontact_time/y_exposure_time*Nw_pixel) +# # Beam position : y-direction (image in pixel-scale) +# y_image_pixel = int(y_nocontact_time/y_exposure_time*Nw_pixel) # -# # contact time : y-direction -# y_scan_time = 0 +# # contact time : y-direction +# y_scan_time = 0 # -# # no contact time : y-direction (left-margin) -# non_contact_time += y_nocontact_time +# # no contact time : y-direction (left-margin) +# non_contact_time += y_nocontact_time # -# # horizontal-scanning sequences -# while (y_scan_time < y_contact_time) : +# # horizontal-scanning sequences +# while (y_scan_time < y_contact_time) : # -# # Beam position : y-direction (cell in nm-scale) -# beam_center[1] = y_scan_time/y_contact_time +# # Beam position : y-direction (cell in nm-scale) +# beam_center[1] = y_scan_time/y_contact_time # -# p_b = numpy.array([Nx, Ny, Nz])*beam_center -# x_b, y_b, z_b = p_b +# p_b = numpy.array([Nx, Ny, Nz])*beam_center +# x_b, y_b, z_b = p_b # -# # loop for frame data -# i_time, i_data = frame_data[0] +# # loop for frame data +# i_time, i_data = frame_data[0] # -# scan_time = z_scan_time + y_scan_time + non_contact_time -# diff = abs(i_time - (scan_time + time)) -# data = i_data +# scan_time = z_scan_time + y_scan_time + non_contact_time +# diff = abs(i_time - (scan_time + time)) +# data = i_data # -# for i, (i_time, i_data) in enumerate(frame_data) : -# #print '\t', '%02d-th frame : ' % (i), i_time, ' sec' -# i_diff = abs(i_time - (scan_time + time)) +# for i, (i_time, i_data) in enumerate(frame_data) : +# #print '\t', '%02d-th frame : ' % (i), i_time, ' sec' +# i_diff = abs(i_time - (scan_time + time)) # -# if (i_diff < diff) : -# diff = i_diff -# data = i_data +# if (i_diff < diff) : +# diff = i_diff +# data = i_data # -# # overwrite the scanned region to cell -# #r_p = int(self.configs.image_scaling*voxel_size/2) -# Mag = self.configs.image_magnification -# r_p = int(self.configs.pinhole_radius/Mag/1e-9) +# # overwrite the scanned region to cell +# #r_p = int(self.configs.image_scaling*voxel_size/2) +# Mag = self.configs.image_magnification +# r_p = int(self.configs.pinhole_radius/Mag/1e-9) # -# if (y_b-r_p < 0) : y_from = int(y_b) -# else : y_from = int(y_b - r_p) +# if (y_b-r_p < 0) : y_from = int(y_b) +# else : y_from = int(y_b - r_p) # -# if (y_b+r_p >= Ny) : y_to = int(y_b) -# else : y_to = int(y_b + r_p) +# if (y_b+r_p >= Ny) : y_to = int(y_b) +# else : y_to = int(y_b + r_p) # -# if (z_b-r_p < 0) : z_from = int(z_b) -# else : z_from = int(z_b - r_p) +# if (z_b-r_p < 0) : z_from = int(z_b) +# else : z_from = int(z_b - r_p) # -# if (z_b+r_p >= Nz) : z_to = int(z_b) -# else : z_to = int(z_b + r_p) +# if (z_b+r_p >= Nz) : z_to = int(z_b) +# else : z_to = int(z_b + r_p) # -# mask = numpy.zeros(shape=(z_to-z_from, y_to-y_from)) +# mask = numpy.zeros(shape=(z_to-z_from, y_to-y_from)) # -# zz, yy = numpy.ogrid[z_from-int(z_b):z_to-int(z_b), y_from-int(y_b):y_to-int(y_b)] -# rr_cut = yy**2 + zz**2 < r_p**2 -# mask[rr_cut] = 1 +# zz, yy = numpy.ogrid[z_from-int(z_b):z_to-int(z_b), y_from-int(y_b):y_to-int(y_b)] +# rr_cut = yy**2 + zz**2 < r_p**2 +# mask[rr_cut] = 1 # -# scan_cell = numpy.zeros_like(mask) +# scan_cell = numpy.zeros_like(mask) # -# # loop for particles -# for j, j_data in enumerate(data) : -# self.get_molecule_plane(scan_cell, i_time, j_data, j, p_b, p_0) +# # loop for particles +# for j, j_data in enumerate(data) : +# self.get_molecule_plane(scan_cell, i_time, j_data, j, p_b, p_0) # -# # image pixel position -# ii, jj = z_image_pixel, y_image_pixel +# # image pixel position +# ii, jj = z_image_pixel, y_image_pixel # -# #cell[z_from:z_to, y_from:y_to] += mask*scan_cell -# Photon_flux = numpy.sum(mask*scan_cell) -# image_pixel[ii][jj][0] = Photon_flux +# #cell[z_from:z_to, y_from:y_to] += mask*scan_cell +# Photon_flux = numpy.sum(mask*scan_cell) +# image_pixel[ii][jj][0] = Photon_flux # -# y_scan_time += y_contact_time/Ny_pixel -# y_image_pixel += 1 +# y_scan_time += y_contact_time/Ny_pixel +# y_image_pixel += 1 # -# # no contact time : y-direction (right-margin) -# non_contact_time += y_nocontact_time -# y_image_pixel += int(y_nocontact_time/y_exposure_time*Nw_pixel) +# # no contact time : y-direction (right-margin) +# non_contact_time += y_nocontact_time +# y_image_pixel += int(y_nocontact_time/y_exposure_time*Nw_pixel) # -# z_scan_time += z_contact_time/Nz_pixel -# z_image_pixel += 1 +# z_scan_time += z_contact_time/Nz_pixel +# z_image_pixel += 1 # # -# if (numpy.amax(image_pixel) > 0) : +# if (numpy.amax(image_pixel) > 0) : # -# image = self.detector_output(image_pixel) +# image = self.detector_output(image_pixel) # -# # save data to numpy-binary file -# image_file_name = os.path.join(self.configs.image_file_dir, -# self.configs.image_file_name_format % (count)) -# numpy.save(image_file_name, image) +# # save data to numpy-binary file +# image_file_name = os.path.join(self.configs.image_file_dir, +# self.configs.image_file_name_format % (count)) +# numpy.save(image_file_name, image) # -# # save data to png-image file -# #image[:,:,3].astype('uint%d' % (self.configs.ADConverter_bit)) -# #toimage(image[:,:,3], low=numpy.amin(image[:,:,3]), high=numpy.amax(image[:,:,3]), mode='I').save(image_file_name) +# # save data to png-image file +# #image[:,:,3].astype('uint%d' % (self.configs.ADConverter_bit)) +# #toimage(image[:,:,3], low=numpy.amin(image[:,:,3]), high=numpy.amax(image[:,:,3]), mode='I').save(image_file_name) # -# time += exposure_time -# count += 1 +# time += exposure_time +# count += 1 # # # - def prob_analog(self, y, alpha) : + def prob_analog(self, y, alpha) : + + # get average gain + A = self.configs.detector_gain - # get average gain - A = self.configs.detector_gain + # get dynodes stages + nu = self.configs.detector_dyn_stages - # get dynodes stages - nu = self.configs.detector_dyn_stages + B = 0.5*(A - 1)/(A**(1.0/nu) - 1) + c = numpy.exp(alpha*(numpy.exp(-A/B) - 1)) - B = 0.5*(A - 1)/(A**(1.0/nu) - 1) - c = numpy.exp(alpha*(numpy.exp(-A/B) - 1)) - - m_y = alpha*A - m_x = m_y/(1 - c) + m_y = alpha*A + m_x = m_y/(1 - c) - s2_y = alpha*(A**2 + 2*A*B) - s2_x = s2_y/(1 - c) - c*m_x**2 + s2_y = alpha*(A**2 + 2*A*B) + s2_x = s2_y/(1 - c) - c*m_x**2 - #if (y < 10*A) : - # Rayleigh approximation - #s2 = (2.0/numpy.pi)*m_x**2 - #prob = y/s2*numpy.exp(-0.5*y**2/s2) + #if (y < 10*A) : + # Rayleigh approximation + #s2 = (2.0/numpy.pi)*m_x**2 + #prob = y/s2*numpy.exp(-0.5*y**2/s2) - # probability distribution - prob = numpy.zeros(shape=(len(y))) + # probability distribution + prob = numpy.zeros(shape=(len(y))) - # get index - k = (numpy.abs(10*A - y)).argmin() + # get index + k = (numpy.abs(10*A - y)).argmin() - # Gamma approximation - k_1 = m_x - k_2 = (m_y**2 + s2_y)/(1 - c) + # Gamma approximation + k_1 = m_x + k_2 = (m_y**2 + s2_y)/(1 - c) - a = 1/(k_1*(k_2/k_1**2 - 1)) - b = a*k_1 + a = 1/(k_1*(k_2/k_1**2 - 1)) + b = a*k_1 - prob[0:k] = a/gamma(b)*(a*y[0:k])**(b-1)*numpy.exp(-a*y[0:k]) + prob[0:k] = a/gamma(b)*(a*y[0:k])**(b-1)*numpy.exp(-a*y[0:k]) - if (k < len(y)) : - # Truncated Gaussian approximation - Q = 0 - beta0 = m_x/numpy.sqrt(s2_x) - beta = beta0 - delta = 0.1*beta0 + if (k < len(y)) : + # Truncated Gaussian approximation + Q = 0 + beta0 = m_x/numpy.sqrt(s2_x) + beta = beta0 + delta = 0.1*beta0 - while (beta < 11*beta0) : - Q += numpy.exp(-0.5*beta**2)/numpy.sqrt(2*numpy.pi)*delta - beta += delta + while (beta < 11*beta0) : + Q += numpy.exp(-0.5*beta**2)/numpy.sqrt(2*numpy.pi)*delta + beta += delta - prob[k:] = numpy.exp(-0.5*(y[k:] - m_x)**2/s2_x)/(numpy.sqrt(2*numpy.pi*s2_x)*(1 - Q)) + prob[k:] = numpy.exp(-0.5*(y[k:] - m_x)**2/s2_x)/(numpy.sqrt(2*numpy.pi*s2_x)*(1 - Q)) - return prob + return prob - #def detector_output(self, cell) : - def detector_output(self, image_pixel) : + #def detector_output(self, cell) : + def detector_output(self, image_pixel) : - # Detector Output - voxel_radius = self.configs.spatiocyte_VoxelRadius - voxel_size = (2.0*voxel_radius)/1e-9 + # Detector Output + voxel_radius = self.configs.spatiocyte_VoxelRadius + voxel_size = (2.0*voxel_radius)/1e-9 - Nw_pixel = self.configs.detector_image_size[0] - Nh_pixel = self.configs.detector_image_size[1] + Nw_pixel = self.configs.detector_image_size[0] + Nh_pixel = self.configs.detector_image_size[1] -# Np = int(self.configs.image_scaling*voxel_size) +# Np = int(self.configs.image_scaling*voxel_size) # # # image in nm-scale -# Nw_image = Nw_pixel*Np -# Nh_image = Nh_pixel*Np +# Nw_image = Nw_pixel*Np +# Nh_image = Nh_pixel*Np # -# Nw_cell = len(cell) -# Nh_cell = len(cell[0]) +# Nw_cell = len(cell) +# Nh_cell = len(cell[0]) # -# if (Nw_image > Nw_cell) : +# if (Nw_image > Nw_cell) : # -# w_cam_from = int((Nw_image - Nw_cell)/2.0) -# w_cam_to = w_cam_from + Nw_cell +# w_cam_from = int((Nw_image - Nw_cell)/2.0) +# w_cam_to = w_cam_from + Nw_cell # w_cel_from = 0 # w_cel_to = Nw_cell # -# else : +# else : # # w_cam_from = 0 # w_cam_to = Nw_image # w_cel_from = int((Nw_cell - Nw_image)/2.0) # w_cel_to = w_cel_from + Nw_image # -# if (Nh_image > Nh_cell) : +# if (Nh_image > Nh_cell) : # # h_cam_from = int((Nh_image - Nh_cell)/2.0) # h_cam_to = h_cam_from + Nh_cell @@ -1677,142 +1677,140 @@ def detector_output(self, image_pixel) : # h_cel_to = h_cel_from + Nh_image # # -# # image in nm-scale -# plane = cell[w_cel_from:w_cel_to, h_cel_from:h_cel_to] +# # image in nm-scale +# plane = cell[w_cel_from:w_cel_to, h_cel_from:h_cel_to] # -# # convert image in nm-scale to pixel-scale +# # convert image in nm-scale to pixel-scale # cell_pixel = numpy.zeros(shape=(Nw_cell/Np, Nh_cell/Np)) # -# # Signal (photon distribution on cell) -# for i in range(Nw_cell/Np) : -# for j in range(Nh_cell/Np) : +# # Signal (photon distribution on cell) +# for i in range(Nw_cell/Np) : +# for j in range(Nh_cell/Np) : # -# # get photon flux -# Photon_flux = numpy.sum(plane[i*Np:(i+1)*Np, j*Np:(j+1)*Np]) -# cell_pixel[i][j] = Photon_flux +# # get photon flux +# Photon_flux = numpy.sum(plane[i*Np:(i+1)*Np, j*Np:(j+1)*Np]) +# cell_pixel[i][j] = Photon_flux # # # # Background (photon distribution on image) # image_pixel = numpy.zeros([Nw_pixel, Nh_pixel, 2]) # -# w_cam_from = int(w_cam_from/Np) -# w_cam_to = int(w_cam_to/Np) -# h_cam_from = int(h_cam_from/Np) -# h_cam_to = int(h_cam_to/Np) +# w_cam_from = int(w_cam_from/Np) +# w_cam_to = int(w_cam_to/Np) +# h_cam_from = int(h_cam_from/Np) +# h_cam_to = int(h_cam_to/Np) # -# w_cel_from = int(w_cel_from/Np) -# w_cel_to = int(w_cel_to/Np) -# h_cel_from = int(h_cel_from/Np) -# h_cel_to = int(h_cel_to/Np) +# w_cel_from = int(w_cel_from/Np) +# w_cel_to = int(w_cel_to/Np) +# h_cel_from = int(h_cel_from/Np) +# h_cel_to = int(h_cel_to/Np) # -# ddw = (w_cam_to - w_cam_from) - (w_cel_to - w_cel_from) -# ddh = (h_cam_to - h_cam_from) - (h_cel_to - h_cel_from) +# ddw = (w_cam_to - w_cam_from) - (w_cel_to - w_cel_from) +# ddh = (h_cam_to - h_cam_from) - (h_cel_to - h_cel_from) # -# if (ddw > 0) : w_cam_to = w_cam_to - ddw -# elif (ddw < 0) : w_cel_to = w_cel_to - ddw +# if (ddw > 0) : w_cam_to = w_cam_to - ddw +# elif (ddw < 0) : w_cel_to = w_cel_to - ddw # # if (ddh > 0) : h_cam_to = h_cam_to - ddh # elif (ddh < 0) : h_cel_to = h_cel_to - ddh # -# # place cell_pixel data to image image -# image_pixel[w_cam_from:w_cam_to, h_cam_from:h_cam_to, 0] = cell_pixel[w_cel_from:w_cel_to, h_cel_from:h_cel_to] - - # set seed for random number - numpy.random.seed() - - # observational time - dT = self.configs.detector_exposure_time/(Nw_pixel*Nh_pixel) +# # place cell_pixel data to image image +# image_pixel[w_cam_from:w_cam_to, h_cam_from:h_cam_to, 0] = cell_pixel[w_cel_from:w_cel_to, h_cel_from:h_cel_to] - # conversion : photon --> photoelectron --> ADC count - for i in range(Nw_pixel) : - for j in range(Nh_pixel) : + # set seed for random number + numpy.random.seed() - # pixel position - pixel = (i, j) + # observational time + dT = self.configs.detector_exposure_time/(Nw_pixel*Nh_pixel) - # Detector : Quantum Efficiency - #index = int(self.configs.psf_wavelength) - int(self.configs.wave_length[0]) - QE = self.configs.detector_qeff + # conversion : photon --> photoelectron --> ADC count + for i in range(Nw_pixel) : + for j in range(Nh_pixel) : - # get signal (photon flux and photons) - Photons = image_pixel[i][j][0] - Flux = Photons/dT + # pixel position + pixel = (i, j) - if (self.configs.detector_mode == "Photon-counting") : - # pair-pulses time resolution (sec) - t_pp = self.configs.detector_pair_pulses - Flux = Flux/(1 + Flux*t_pp) + # Detector : Quantum Efficiency + #index = int(self.configs.psf_wavelength) - int(self.configs.wave_length[0]) + QE = self.configs.detector_qeff - # get constant background - if (self.effects.background_switch == True) : + # get signal (photon flux and photons) + Photons = image_pixel[i][j][0] + Flux = Photons/dT - Photons_bg = self.effects.background_mean - Photons += Photons_bg + if (self.configs.detector_mode == "Photon-counting") : + # pair-pulses time resolution (sec) + t_pp = self.configs.detector_pair_pulses + Flux = Flux/(1 + Flux*t_pp) - # get signal (expectation) - Exp = QE*Photons + # get constant background + if (self.effects.background_switch == True) : - # get dark count - D = self.configs.detector_dark_count - Exp += D*dT + Photons_bg = self.effects.background_mean + Photons += Photons_bg - # select Camera type - if (self.configs.detector_mode == "Photon-counting") : + # get signal (expectation) + Exp = QE*Photons - # get signal (poisson distributions) - signal = numpy.random.poisson(Exp, None) + # get dark count + D = self.configs.detector_dark_count + Exp += D*dT + # select Camera type + if (self.configs.detector_mode == "Photon-counting") : - if (self.configs.detector_mode == "Analog") : + # get signal (poisson distributions) + signal = numpy.random.poisson(Exp, None) - # get signal (photoelectrons) - if (Exp > 1e-8) : - # get EM gain - G = self.configs.detector_gain + if (self.configs.detector_mode == "Analog") : - # signal array - if (Exp > 1) : sig = numpy.sqrt(Exp) - else : sig = 1 + # get signal (photoelectrons) + if (Exp > 1e-8) : - s_min = int(G*(Exp - 10*sig)) - s_max = int(G*(Exp + 10*sig)) + # get EM gain + G = self.configs.detector_gain - if (s_min < 0) : s_min = 0 + # signal array + if (Exp > 1) : sig = numpy.sqrt(Exp) + else : sig = 1 - delta = (s_max - s_min)/1000. + s_min = int(G*(Exp - 10*sig)) + s_max = int(G*(Exp + 10*sig)) - s = numpy.array([k*delta+s_min for k in range(1000)]) + if (s_min < 0) : s_min = 0 - # probability density fuction - #p_signal = numpy.array(map(lambda y : self.prob_analog(y, Exp), s)) - p_signal = self.prob_analog(s, Exp) - p_ssum = p_signal.sum() + delta = (s_max - s_min)/1000. - # get signal (photoelectrons) - signal = numpy.random.choice(s, None, p=p_signal/p_ssum) - signal = signal/G + s = numpy.array([k*delta+s_min for k in range(1000)]) - else : - signal = 0 + # probability density fuction + #p_signal = numpy.array(map(lambda y : self.prob_analog(y, Exp), s)) + p_signal = self.prob_analog(s, Exp) + p_ssum = p_signal.sum() - # get detector noise (photoelectrons) - Nr = QE*self.configs.detector_readout_noise + # get signal (photoelectrons) + signal = numpy.random.choice(s, None, p=p_signal/p_ssum) + signal = signal/G - if (Nr > 0) : noise = numpy.random.normal(0, Nr, None) - else : noise = 0 + else : + signal = 0 - # A/D converter : Expectation --> ADC counts - EXP = self.get_ADC_value(pixel, Exp+Nr) + # get detector noise (photoelectrons) + Nr = QE*self.configs.detector_readout_noise - # A/D converter : Photoelectrons --> ADC counts - PE = signal + noise - ADC = self.get_ADC_value(pixel, PE) + if (Nr > 0) : noise = numpy.random.normal(0, Nr, None) + else : noise = 0 - # set data in image array - #image_pixel[i][j] = [Photons, Exp, PE, ADC] - image_pixel[i][j] = [EXP, ADC] + # A/D converter : Expectation --> ADC counts + EXP = self.get_ADC_value(pixel, Exp+Nr) - return image_pixel + # A/D converter : Photoelectrons --> ADC counts + PE = signal + noise + ADC = self.get_ADC_value(pixel, PE) + # set data in image array + #image_pixel[i][j] = [Photons, Exp, PE, ADC] + image_pixel[i][j] = [EXP, ADC] + return image_pixel diff --git a/bioimaging/lscm_rewrite_script.py b/bioimaging/lscm_rewrite_script.py index e6f187f..2626857 100755 --- a/bioimaging/lscm_rewrite_script.py +++ b/bioimaging/lscm_rewrite_script.py @@ -7,51 +7,49 @@ def test_lscm(t0, t1) : - # create LSCM imaging - lscm = LSCMConfigs() - lscm.set_LightSource(source_type='LASER', wave_length=532, flux=100e-6, radius=400e-9) - #lscm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') - lscm.set_Fluorophore(fluorophore_type='EGFP') - lscm.set_Pinhole(radius=28.8e-6) - lscm.set_Magnification(Mag=60) - - # PMT : Photon-counting mode - lscm.set_Detector(detector='PMT', mode="Photon-counting", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ - pixel_length=207.16e-9, scan_time=1.10, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) - lscm.set_ADConverter(bit=12, offset=0, fullwell=4096) - -# # PMT : Anlalog mode -# lscm.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ -# pixel_length=207.16e-9, scan_time=1.15, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) -# lscm.set_ADConverter(bit=12, offset=0, fullwell=4096) - -# # Output data -# lscm.set_OutputData(image_file_dir='./numpys_lscm_001000A') -# lscm.set_OutputData(image_file_dir='./numpys_lscm_019656A') - - # Input data - lscm.reset_InputData('/home/masaki/wrk/spatiocyte/models/.....', start=t0, end=t1, observable="A") - - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0) - physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) - physics.set_photobleaching(tau0=100e-6, alpha=0.73) - #physics.set_photoactivation(turn_on_ratio=1000, activation_yield=1.0, frac_preactivation=0.00) - #physics.set_photoblinking(t0_on=30e-6, a_on=0.58, t0_off=10e-6, a_off=0.48) - - # create image - create = LSCMVisualizer(configs=lscm, effects=physics) - create.rewrite_InputData(output_file_dir='./data_lscm_for_kakenhi/data_pb/.....') - #create.output_frames(num_div=16) + # create LSCM imaging + lscm = LSCMConfigs() + lscm.set_LightSource(source_type='LASER', wave_length=532, flux=100e-6, radius=400e-9) + #lscm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') + lscm.set_Fluorophore(fluorophore_type='EGFP') + lscm.set_Pinhole(radius=28.8e-6) + lscm.set_Magnification(Mag=60) + + # PMT : Photon-counting mode + lscm.set_Detector(detector='PMT', mode="Photon-counting", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ + pixel_length=207.16e-9, scan_time=1.10, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) + lscm.set_ADConverter(bit=12, offset=0, fullwell=4096) + +# # PMT : Anlalog mode +# lscm.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ +# pixel_length=207.16e-9, scan_time=1.15, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) +# lscm.set_ADConverter(bit=12, offset=0, fullwell=4096) + +# # Output data +# lscm.set_OutputData(image_file_dir='./numpys_lscm_001000A') +# lscm.set_OutputData(image_file_dir='./numpys_lscm_019656A') + + # Input data + lscm.reset_InputData('/home/masaki/wrk/spatiocyte/models/.....', start=t0, end=t1, observable="A") + + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0) + physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) + physics.set_photobleaching(tau0=100e-6, alpha=0.73) + #physics.set_photoactivation(turn_on_ratio=1000, activation_yield=1.0, frac_preactivation=0.00) + #physics.set_photoblinking(t0_on=30e-6, a_on=0.58, t0_off=10e-6, a_off=0.48) + + # create image + create = LSCMVisualizer(configs=lscm, effects=physics) + create.rewrite_InputData(output_file_dir='./data_lscm_for_kakenhi/data_pb/.....') + #create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_lscm(t0, t1) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_lscm(t0, t1) diff --git a/bioimaging/lscm_script.py b/bioimaging/lscm_script.py index 21e29c8..903d1cb 100755 --- a/bioimaging/lscm_script.py +++ b/bioimaging/lscm_script.py @@ -7,53 +7,51 @@ def test_lscm(t0, t1) : - # create LSCM imaging - lscm = LSCMConfigs() - lscm.set_Shutter(start_time=t0, end_time=t1) - lscm.set_LightSource(source_type='LASER', wave_length=532, flux=100e-6, radius=400e-9) - #lscm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') - lscm.set_Fluorophore(fluorophore_type='EGFP') - lscm.set_Pinhole(radius=28.8e-6) - lscm.set_Magnification(Mag=60) + # create LSCM imaging + lscm = LSCMConfigs() + lscm.set_Shutter(start_time=t0, end_time=t1) + lscm.set_LightSource(source_type='LASER', wave_length=532, flux=100e-6, radius=400e-9) + #lscm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') + lscm.set_Fluorophore(fluorophore_type='EGFP') + lscm.set_Pinhole(radius=28.8e-6) + lscm.set_Magnification(Mag=60) - # PMT : Photon-counting mode - lscm.set_Detector(detector='PMT', mode="Photon-counting", image_size=(1024,1024), focal_point=(0.0,0.5,0.5), \ - pixel_length=207.16e-9, scan_time=1.00, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) - lscm.set_ADConverter(bit=12, offset=0, fullwell=4096) + # PMT : Photon-counting mode + lscm.set_Detector(detector='PMT', mode="Photon-counting", image_size=(1024,1024), focal_point=(0.0,0.5,0.5), \ + pixel_length=207.16e-9, scan_time=1.00, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) + lscm.set_ADConverter(bit=12, offset=0, fullwell=4096) -# # PMT : Anlalog mode -# lscm.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ -# pixel_length=207.16e-9, scan_time=1.15, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) -# lscm.set_ADConverter(bit=12, offset=0, fullwell=4096) +# # PMT : Anlalog mode +# lscm.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ +# pixel_length=207.16e-9, scan_time=1.15, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) +# lscm.set_ADConverter(bit=12, offset=0, fullwell=4096) - # Output data - lscm.set_OutputData(image_file_dir='./data_for_kakenhi/numpys/TestCell_00') + # Output data + lscm.set_OutputData(image_file_dir='./data_for_kakenhi/numpys/TestCell_00') - # Input data - csv_dir = '/home/masaki/wrk/spatiocyte/models/Kakenhi/data/TestCell_00' - #csv_dir = '/home/masaki/bioimaging_4public/data_for_kakenhi/data_pb/...' - lscm.set_InputFile(csv_dir, observable="R") - lscm.set_ShapeFile(csv_dir) + # Input data + csv_dir = '/home/masaki/wrk/spatiocyte/models/Kakenhi/data/TestCell_00' + #csv_dir = '/home/masaki/bioimaging_4public/data_for_kakenhi/data_pb/...' + lscm.set_InputFile(csv_dir, observable="R") + lscm.set_ShapeFile(csv_dir) - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0.01) - #physics.set_crosstalk(width=0.70) - physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) - physics.set_photobleaching(tau0=2.27, alpha=0.73) + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0.01) + #physics.set_crosstalk(width=0.70) + physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) + physics.set_photobleaching(tau0=2.27, alpha=0.73) - # create image - create = LSCMVisualizer(configs=lscm, effects=physics) - create.output_frames(num_div=16) + # create image + create = LSCMVisualizer(configs=lscm, effects=physics) + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_lscm(t0, t1) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_lscm(t0, t1) diff --git a/bioimaging/lscm_test_convert.py b/bioimaging/lscm_test_convert.py index 387929a..92acb7b 100644 --- a/bioimaging/lscm_test_convert.py +++ b/bioimaging/lscm_test_convert.py @@ -14,156 +14,155 @@ def prob_analog(y, alpha) : - # get average gain - A = 1e+6 + # get average gain + A = 1e+6 - # get dynodes stages - nu = 11 + # get dynodes stages + nu = 11 - B = 0.5*(A - 1)/(A**(1.0/nu) - 1) - c = numpy.exp(alpha*(numpy.exp(-A/B) - 1)) - - m_y = alpha*A - m_x = m_y/(1 - c) + B = 0.5*(A - 1)/(A**(1.0/nu) - 1) + c = numpy.exp(alpha*(numpy.exp(-A/B) - 1)) - s2_y = alpha*(A**2 + 2*A*B) - s2_x = s2_y/(1 - c) - c*m_x**2 + m_y = alpha*A + m_x = m_y/(1 - c) - #if (y < 10*A) : - # Rayleigh approximation - #s2 = (2.0/numpy.pi)*m_x**2 - #prob = y/s2*numpy.exp(-0.5*y**2/s2) + s2_y = alpha*(A**2 + 2*A*B) + s2_x = s2_y/(1 - c) - c*m_x**2 - # probability distribution - prob = numpy.zeros(shape=(len(y))) + #if (y < 10*A) : + # Rayleigh approximation + #s2 = (2.0/numpy.pi)*m_x**2 + #prob = y/s2*numpy.exp(-0.5*y**2/s2) - # get index - k = (numpy.abs(10*A - y)).argmin() + # probability distribution + prob = numpy.zeros(shape=(len(y))) - # Gamma approximation - k_1 = m_x - k_2 = (m_y**2 + s2_y)/(1 - c) + # get index + k = (numpy.abs(10*A - y)).argmin() - a = 1/(k_1*(k_2/k_1**2 - 1)) - b = a*k_1 + # Gamma approximation + k_1 = m_x + k_2 = (m_y**2 + s2_y)/(1 - c) - prob[0:k] = a/gamma(b)*(a*y[0:k])**(b-1)*numpy.exp(-a*y[0:k]) + a = 1/(k_1*(k_2/k_1**2 - 1)) + b = a*k_1 - if (k < len(y)) : - # Truncated Gaussian approximation - Q = 0 - beta0 = m_x/numpy.sqrt(s2_x) - beta = beta0 - delta = 0.1*beta0 + prob[0:k] = a/gamma(b)*(a*y[0:k])**(b-1)*numpy.exp(-a*y[0:k]) - while (beta < 11*beta0) : - Q += numpy.exp(-0.5*beta**2)/numpy.sqrt(2*numpy.pi)*delta - beta += delta + if (k < len(y)) : + # Truncated Gaussian approximation + Q = 0 + beta0 = m_x/numpy.sqrt(s2_x) + beta = beta0 + delta = 0.1*beta0 - prob[k:] = numpy.exp(-0.5*(y[k:] - m_x)**2/s2_x)/(numpy.sqrt(2*numpy.pi*s2_x)*(1 - Q)) + while (beta < 11*beta0) : + Q += numpy.exp(-0.5*beta**2)/numpy.sqrt(2*numpy.pi)*delta + beta += delta - return prob + prob[k:] = numpy.exp(-0.5*(y[k:] - m_x)**2/s2_x)/(numpy.sqrt(2*numpy.pi*s2_x)*(1 - Q)) + + return prob def convert(file_in, file_out, index=None) : - i = 0 + i = 0 - max_count = 0 - image_signal = numpy.zeros([100,100]) + max_count = 0 + image_signal = numpy.zeros([100,100]) - while (True) : - try : - input_image = numpy.load(file_in + '/image_%07d.npy' % (i)) - except Exception : - break + while (True) : + try : + input_image = numpy.load(file_in + '/image_%07d.npy' % (i)) + except Exception : + break - output_image = file_out + '/image_%07d.png' % (i) - #output_image = file_out + '/image_%07d.png' % (i/26) + output_image = file_out + '/image_%07d.png' % (i) + #output_image = file_out + '/image_%07d.png' % (i/26) - # data for tirfm - image_expected = input_image[512-50:512+50,512-50:512+50,0] + # data for tirfm + image_expected = input_image[512-50:512+50,512-50:512+50,0] -# amax = numpy.amax(image_expected) -# amin = numpy.amin(image_expected) +# amax = numpy.amax(image_expected) +# amin = numpy.amin(image_expected) # -# if (max_count < amax) : -# max_count = amax +# if (max_count < amax) : +# max_count = amax # -# print image_expected -# print i, amax, amin +# print image_expected +# print i, amax, amin - #Background = 0 - Background = 0 + #Background = 0 + Background = 0 - for x in range(100) : - for y in range(100) : - # expectation - Exp = image_expected[x,y] + Background + for x in range(100) : + for y in range(100) : + # expectation + Exp = image_expected[x,y] + Background - # get EM gain - G = 1e+6 + # get EM gain + G = 1e+6 - # signal array - if (Exp > 1) : sig = numpy.sqrt(Exp) - else : sig = 1 + # signal array + if (Exp > 1) : sig = numpy.sqrt(Exp) + else : sig = 1 - s_min = int(G*(Exp - 10*sig)) - s_max = int(G*(Exp + 10*sig)) + s_min = int(G*(Exp - 10*sig)) + s_max = int(G*(Exp + 10*sig)) - if (s_min < 0) : s_min = 0 + if (s_min < 0) : s_min = 0 - delta = (s_max - s_min)/1000. + delta = (s_max - s_min)/1000. - s = numpy.array([k*delta+s_min for k in range(1000)]) + s = numpy.array([k*delta+s_min for k in range(1000)]) - # probability density fuction - p_signal = prob_analog(s, Exp) - p_ssum = p_signal.sum() + # probability density fuction + p_signal = prob_analog(s, Exp) + p_ssum = p_signal.sum() - # get signal (photoelectrons) - signal = numpy.random.choice(s, None, p=p_signal/p_ssum) + # get signal (photoelectrons) + signal = numpy.random.choice(s, None, p=p_signal/p_ssum) -# # get detector noise (photoelectrons) -# Nr = 0.001*G -# noise = numpy.random.normal(Nr, numpy.sqrt(Nr), None) +# # get detector noise (photoelectrons) +# Nr = 0.001*G +# noise = numpy.random.normal(Nr, numpy.sqrt(Nr), None) - image_signal[x,y] = signal/1e+4 - #image_signal[x,y] = signal + noise + image_signal[x,y] = signal/1e+4 + #image_signal[x,y] = signal + noise - amax = numpy.amax(image_signal) - amin = numpy.amin(image_signal) + amax = numpy.amax(image_signal) + amin = numpy.amin(image_signal) - if (max_count < amax) : - max_count = amax + if (max_count < amax) : + max_count = amax - print(image_signal) - print(i, amax, amin) + print(image_signal) + print(i, amax, amin) - image_signal2 = (image_signal - amin)*(4336-1424)/(amax-amin) + 1424 + image_signal2 = (image_signal - amin)*(4336-1424)/(amax-amin) + 1424 - # 16-bit data format - image_signal.astype('uint16') - toimage(image_signal2, high=4336, low=1424, mode='I').save(output_image) + # 16-bit data format + image_signal.astype('uint16') + toimage(image_signal2, high=4336, low=1424, mode='I').save(output_image) - # 8-bit data format (for making movie) - #toimage(image_signal, cmin=cmin, cmax=cmax).save(output_image) + # 8-bit data format (for making movie) + #toimage(image_signal, cmin=cmin, cmax=cmax).save(output_image) - i += 1 - break - + i += 1 + break - print('Max count : ', max_count, 'ADC') + print('Max count : ', max_count, 'ADC') -if __name__=='__main__': - file_in = '/home/masaki/bioimaging_4public/images_lscm_test_010uW' - #file_in = '/home/masaki/bioimaging_4public/images_lscm_test_z00_100uW' - file_out = '/home/masaki/bioimaging_4public/images_png' +if __name__=='__main__': - convert(file_in, file_out) + file_in = '/home/masaki/bioimaging_4public/images_lscm_test_010uW' + #file_in = '/home/masaki/bioimaging_4public/images_lscm_test_z00_100uW' + file_out = '/home/masaki/bioimaging_4public/images_png' + convert(file_in, file_out) diff --git a/bioimaging/minDE_handler.py b/bioimaging/minDE_handler.py index cd73101..3a5ce3c 100755 --- a/bioimaging/minDE_handler.py +++ b/bioimaging/minDE_handler.py @@ -30,9 +30,9 @@ class MinDEConfigs(EPIFMConfigs) : MinDE configration - Low-angle oblique illumination using two incident beams - + - EMCCD camera + Low-angle oblique illumination using two incident beams + + + EMCCD camera ''' def __init__(self, user_configs_dict = None): @@ -62,96 +62,93 @@ def __init__(self, user_configs_dict = None): def set_Spatiocyte_data_arrays(self) : - # get spatiocyte file directry - csv_file_directry = self.spatiocyte_file_directry + # get spatiocyte file directry + csv_file_directry = self.spatiocyte_file_directry - # set data-array - data = [] + # set data-array + data = [] - # get count-array - count_array = numpy.array(self.shutter_count_array) + # get count-array + count_array = numpy.array(self.shutter_count_array) - # read lattice file - for i in range(len(count_array)) : + # read lattice file + for i in range(len(count_array)) : + + csv_file_path = csv_file_directry + '/pt-%09d.0.csv' % (count_array[i]) - csv_file_path = csv_file_directry + '/pt-%09d.0.csv' % (count_array[i]) - try : csv_file = open(csv_file_path, 'r') - dataset = [] + dataset = [] - for row in csv.reader(csv_file) : - dataset.append(row) + for row in csv.reader(csv_file) : + dataset.append(row) # get Time - time = float(dataset[0][0]) + time = float(dataset[0][0]) + + particles = [] - particles = [] + for data_id in dataset : + # get Coordinate + #c_id = (float(data_id[1]), float(data_id[2]), float(data_id[3])) + c_id = (float(data_id[3]), float(data_id[2]), float(data_id[1])) + # get Molecule ID and its state + m_id, s_id = literal_eval(data_id[5]) + # get Fluorophore ID and compartment ID + f_id, l_id = literal_eval(data_id[6]) - for data_id in dataset : - # get Coordinate - #c_id = (float(data_id[1]), float(data_id[2]), float(data_id[3])) - c_id = (float(data_id[3]), float(data_id[2]), float(data_id[1])) - # get Molecule ID and its state - m_id, s_id = literal_eval(data_id[5]) - # get Fluorophore ID and compartment ID - f_id, l_id = literal_eval(data_id[6]) + try : + p_state, cyc_id = float(data_id[7]), float(data_id[8]) - try : - p_state, cyc_id = float(data_id[7]), float(data_id[8]) + except Exception : + p_state, cyc_id = 1.0, float('inf') - except Exception : - p_state, cyc_id = 1.0, float('inf') + particles.append((c_id, s_id, l_id, p_state, cyc_id)) - particles.append((c_id, s_id, l_id, p_state, cyc_id)) - - data.append([time, particles]) + data.append([time, particles]) except Exception : print('Warning : ', csv_file_path, ' not found') - #exit() + #exit() - data.sort(lambda x, y:cmp(x[0], y[0])) + data.sort(lambda x, y:cmp(x[0], y[0])) - # set data + # set data self._set_data('spatiocyte_data', data) -# if len(self.spatiocyte_data) == 0: -# raise VisualizerError('Cannot find spatiocyte_data in any given csv files: ' \ -# + ', '.join(csv_file_directry)) +# if len(self.spatiocyte_data) == 0: +# raise VisualizerError('Cannot find spatiocyte_data in any given csv files: ' \ +# + ', '.join(csv_file_directry)) class MinDEVisualizer(EPIFMVisualizer) : - ''' - MinDE visualization class - ''' - - def __init__(self, configs=MinDEConfigs(), effects=PhysicalEffects()) : - - assert isinstance(configs, MinDEConfigs) - self.configs = configs - - assert isinstance(effects, PhysicalEffects) - self.effects = effects + ''' + MinDE visualization class + ''' - """ - Check and create the folder for image file. - """ - if not os.path.exists(self.configs.image_file_dir): - os.makedirs(self.configs.image_file_dir) - #else: - # for file in os.listdir(self.configs.movie_image_file_dir): - # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) + def __init__(self, configs=MinDEConfigs(), effects=PhysicalEffects()) : - """ - set optical path from source to detector - """ - self.configs.set_Optical_path() + assert isinstance(configs, MinDEConfigs) + self.configs = configs + assert isinstance(effects, PhysicalEffects) + self.effects = effects + """ + Check and create the folder for image file. + """ + if not os.path.exists(self.configs.image_file_dir): + os.makedirs(self.configs.image_file_dir) + #else: + # for file in os.listdir(self.configs.movie_image_file_dir): + # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) + """ + set optical path from source to detector + """ + self.configs.set_Optical_path() diff --git a/bioimaging/minDE_script.py b/bioimaging/minDE_script.py index e2e915d..a6c2071 100755 --- a/bioimaging/minDE_script.py +++ b/bioimaging/minDE_script.py @@ -7,46 +7,44 @@ def test_minDE(t0, t1) : - # create EPIFM imaging - minDE = EPIFMConfigs() - minDE.set_Shutter(start_time=t0, end_time=t1) - minDE.set_LightSource(source_type='LASER', wave_length=532, flux_density=20, angle=72) - minDE.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)', normalization=1.0) - minDE.set_DichroicMirror('FF562-Di03-25x36') - minDE.set_Magnification(Mag=100) + # create EPIFM imaging + minDE = EPIFMConfigs() + minDE.set_Shutter(start_time=t0, end_time=t1) + minDE.set_LightSource(source_type='LASER', wave_length=532, flux_density=20, angle=72) + minDE.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)', normalization=1.0) + minDE.set_DichroicMirror('FF562-Di03-25x36') + minDE.set_Magnification(Mag=100) - # Detector : EMCCD Camera - minDE.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, exposure_time=0.200, \ - focal_point=(0.0,0.5,0.5), QE=0.92, readout_noise=100, emgain=300) - minDE.set_ADConverter(bit=16, offset=2000, fullwell=800000) + # Detector : EMCCD Camera + minDE.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, exposure_time=0.200, \ + focal_point=(0.0,0.5,0.5), QE=0.92, readout_noise=100, emgain=300) + minDE.set_ADConverter(bit=16, offset=2000, fullwell=800000) - # Output data - #minDE.set_OutputData(image_file_dir='/home/masaki/bioimaging_4public/data_for_satya/numpys/minE') - minDE.set_OutputData(image_file_dir='/home/masaki/bioimaging_4public/data_for_satya/numpys/minD') + # Output data + #minDE.set_OutputData(image_file_dir='/home/masaki/bioimaging_4public/data_for_satya/numpys/minE') + minDE.set_OutputData(image_file_dir='/home/masaki/bioimaging_4public/data_for_satya/numpys/minD') - # Input data - csv_dir = '/home/masaki/wrk/spatiocyte/models/Satya/data_minDE' - #minDE.set_InputFile(csv_dir, observable="EE") - minDE.set_InputFile(csv_dir, observable="D") - minDE.set_ShapeFile(csv_dir) + # Input data + csv_dir = '/home/masaki/wrk/spatiocyte/models/Satya/data_minDE' + #minDE.set_InputFile(csv_dir, observable="EE") + minDE.set_InputFile(csv_dir, observable="D") + minDE.set_ShapeFile(csv_dir) - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0.01) - physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) - #physics.set_photobleaching(tau0=2.27, alpha=0.73) + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0.01) + physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) + #physics.set_photobleaching(tau0=2.27, alpha=0.73) - # create image - create = EPIFMVisualizer(configs=minDE, effects=physics) - create.output_frames(num_div=16) + # create image + create = EPIFMVisualizer(configs=minDE, effects=physics) + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_minDE(t0, t1) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_minDE(t0, t1) diff --git a/bioimaging/moriga_handler.py b/bioimaging/moriga_handler.py index e011313..6ca8175 100755 --- a/bioimaging/moriga_handler.py +++ b/bioimaging/moriga_handler.py @@ -27,9 +27,9 @@ class MorigaConfigs(TIRFMConfigs) : Moriga-TIRFM configration - Evanescent Field - + - Detector : EMCCD/CMOS + Evanescent Field + + + Detector : EMCCD/CMOS ''' def __init__(self, user_configs_dict = None): @@ -58,8 +58,8 @@ def __init__(self, user_configs_dict = None): def set_Fluorophore(self, fluorophore_type = None, wave_length = None, - intensity = None, - noise = None, + intensity = None, + noise = None, width = None, cutoff = None, file_name_format = None) : @@ -73,25 +73,25 @@ def set_Fluorophore(self, fluorophore_type = None, self._set_data('psf_cutoff', cutoff) self._set_data('psf_file_name_format', file_name_format) - index = (numpy.abs(self.wave_length - self.psf_wavelength)).argmin() + index = (numpy.abs(self.wave_length - self.psf_wavelength)).argmin() - self.fluoex_eff[index] = 100 - self.fluoem_eff[index] = 100 + self.fluoex_eff[index] = 100 + self.fluoem_eff[index] = 100 print('\tWave Length = ', self.psf_wavelength, 'nm') - print('\tIntensity = ', self.psf_intensity) - #print '\tIntensity Noise = ', self.psf_intensity_noise + print('\tIntensity = ', self.psf_intensity) + #print '\tIntensity Noise = ', self.psf_intensity_noise print('\tLateral Width = ', self.psf_width[0], 'nm') print('\tAxial Width = ', self.psf_width[1], 'nm') - print('\tLateral Cutoff = ', self.psf_cutoff[0], 'nm') + print('\tLateral Cutoff = ', self.psf_cutoff[0], 'nm') print('\tAxial Cutoff = ', self.psf_cutoff[1], 'nm') - - # Normalization - norm = sum(self.fluoex_eff) - self.fluoex_norm = numpy.array(self.fluoex_eff)/norm - norm = sum(self.fluoem_eff) - self.fluoem_norm = numpy.array(self.fluoem_eff)/norm + # Normalization + norm = sum(self.fluoex_eff) + self.fluoex_norm = numpy.array(self.fluoex_eff)/norm + + norm = sum(self.fluoem_eff) + self.fluoem_norm = numpy.array(self.fluoem_eff)/norm @@ -112,76 +112,76 @@ def set_PSF_detector(self) : if (self.emission_switch == True) : I = I*0.01*self.emission_eff - # For normalization - norm = map(lambda x : True if x > 1e-4 else False, I) + # For normalization + norm = map(lambda x : True if x > 1e-4 else False, I) - # PSF : Fluorophore + # PSF : Fluorophore I0 = 1.0#self.psf_intensity Ir = sum(map(lambda x : x*numpy.exp(-0.5*(r/self.psf_width[0])**2), norm)) Iz = sum(map(lambda x : x*numpy.exp(-0.5*(z/self.psf_width[1])**2), norm)) - psf_fl = numpy.array(map(lambda x : I0*Ir*x, Iz)) + psf_fl = numpy.array(map(lambda x : I0*Ir*x, Iz)) - self.fluorophore_psf = psf_fl + self.fluorophore_psf = psf_fl def reset_InputData(self, csv_file_directry, start=0, end=None, observable=None) : - print('--- Input Spatiocyte Data : ', csv_file_directry) + print('--- Input Spatiocyte Data : ', csv_file_directry) - ### header - f = open(csv_file_directry + '/pt-input.csv', 'r') + ### header + f = open(csv_file_directry + '/pt-input.csv', 'r') header = f.readline().rstrip().split(',') header[:5] = [float(_) for _ in header[:5]] f.close() interval, lengths, voxel_r, species_info = header[0], (header[3:0:-1]), header[4], header[5:] - species_id = range(len(species_info)) - species_index = [_.split(':')[1].split(']')[0] for _ in species_info] - species_radius = [float(_.split('=')[1]) for _ in species_info] + species_id = range(len(species_info)) + species_index = [_.split(':')[1].split(']')[0] for _ in species_info] + species_radius = [float(_.split('=')[1]) for _ in species_info] -# ##### -# count_start = int(round(start/interval)) -# count_end = int(round(end/interval)) +# ##### +# count_start = int(round(start/interval)) +# count_end = int(round(end/interval)) +# +# data = [] # -# data = [] +# # read lattice file +# for count in range(count_start, count_end, 1) : # -# # read lattice file -# for count in range(count_start, count_end, 1) : +# csv_file_path = csv_file_directry + '/pt-%09d.0.csv' % (count) # -# csv_file_path = csv_file_directry + '/pt-%09d.0.csv' % (count) -# # try : # # csv_file = open(csv_file_path, 'r') # -# dataset = [] +# dataset = [] # -# for row in csv.reader(csv_file) : -# dataset.append(row) +# for row in csv.reader(csv_file) : +# dataset.append(row) # # ### particle data -# time = float(dataset[0][0]) +# time = float(dataset[0][0]) # -# particles = [] +# particles = [] # -# from ast import literal_eval +# from ast import literal_eval # -# for data_id in dataset : -# c_id = (float(data_id[1]), float(data_id[2]), float(data_id[3])) -# s_id, l_id = literal_eval(data_id[6]) -# particles.append((c_id, s_id, l_id)) -# -# data.append([time, particles]) +# for data_id in dataset : +# c_id = (float(data_id[1]), float(data_id[2]), float(data_id[3])) +# s_id, l_id = literal_eval(data_id[6]) +# particles.append((c_id, s_id, l_id)) +# +# data.append([time, particles]) # # # except Exception : # print 'Error : ', csv_file_path, ' not found' -# exit() +# exit() # -# data.sort(lambda x, y:cmp(x[0], y[0])) +# data.sort(lambda x, y:cmp(x[0], y[0])) # get filename self._set_data('spatiocyte_file_directry', csv_file_directry) @@ -191,14 +191,14 @@ def reset_InputData(self, csv_file_directry, start=0, end=None, observable=None) self._set_data('spatiocyte_end_time', end) self._set_data('spatiocyte_interval', interval) - # get data + # get data #self._set_data('spatiocyte_data', data) # get species properties self._set_data('spatiocyte_species_id', species_id) self._set_data('spatiocyte_index', species_index) - #self._set_data('spatiocyte_diffusion', species_diffusion) - self._set_data('spatiocyte_radius', species_radius) + #self._set_data('spatiocyte_diffusion', species_diffusion) + self._set_data('spatiocyte_radius', species_radius) # get lattice properties #self._set_data('spatiocyte_lattice_id', map(lambda x : x[0], lattice)) @@ -206,620 +206,617 @@ def reset_InputData(self, csv_file_directry, start=0, end=None, observable=None) self._set_data('spatiocyte_VoxelRadius', voxel_r) self._set_data('spatiocyte_theNormalizedVoxelRadius', 0.5) - + # set observable if observable is None : index = [True for i in range(len(self.spatiocyte_index))] - else : + else : index = map(lambda x : True if x.find(observable) > -1 else False, self.spatiocyte_index) - #index = [False, True] + #index = [False, True] self.spatiocyte_observables = copy.copy(index) print('\tStart time =', self.spatiocyte_start_time, 'sec') print('\tEnd time =', self.spatiocyte_end_time, 'sec') print('\tInterval =', self.spatiocyte_interval, 'sec') - print('\tVoxel radius =', self.spatiocyte_VoxelRadius, 'm') + print('\tVoxel radius =', self.spatiocyte_VoxelRadius, 'm') print('\tCompartment lengths :', self.spatiocyte_lengths, 'voxels') print('\tSpecies Index :', self.spatiocyte_index) print('\tObservable :', self.spatiocyte_observables) - # Visualization error - if self.spatiocyte_species_id is None: - raise VisualizerError('Cannot find species_id in any given csv files') + # Visualization error + if self.spatiocyte_species_id is None: + raise VisualizerError('Cannot find species_id in any given csv files') -# if len(self.spatiocyte_data) == 0: -# raise VisualizerError('Cannot find spatiocyte_data in any given csv files: ' \ -# + ', '.join(csv_file_directry)) +# if len(self.spatiocyte_data) == 0: +# raise VisualizerError('Cannot find spatiocyte_data in any given csv files: ' \ +# + ', '.join(csv_file_directry)) - if len(self.spatiocyte_index) == 0 : - raise VisualizerError('Cannot find spatiocyte_index in any given csv files: ' \ - + ', '.join(csv_file_directry)) + if len(self.spatiocyte_index) == 0 : + raise VisualizerError('Cannot find spatiocyte_index in any given csv files: ' \ + + ', '.join(csv_file_directry)) def set_InputData(self, csv_file_directry, start=0, end=None, observable=None) : - print('--- Input Spatiocyte Data : ', csv_file_directry) + print('--- Input Spatiocyte Data : ', csv_file_directry) - ### header - f = open(csv_file_directry + '/pt-input.csv', 'r') + ### header + f = open(csv_file_directry + '/pt-input.csv', 'r') header = f.readline().rstrip().split(',') header[:5] = [float(_) for _ in header[:5]] f.close() interval, lengths, voxel_r, species_info = header[0], (header[3:0:-1]), header[4], header[5:] - species_id = range(len(species_info)) - species_index = [_.split(':')[1].split(']')[0] for _ in species_info] - species_radius = [float(_.split('=')[1]) for _ in species_info] + species_id = range(len(species_info)) + species_index = [_.split(':')[1].split(']')[0] for _ in species_info] + species_radius = [float(_.split('=')[1]) for _ in species_info] - ##### - count_start = int(round(start/interval)) - count_end = int(round(end/interval)) + ##### + count_start = int(round(start/interval)) + count_end = int(round(end/interval)) - data = [] + data = [] - # read lattice file - for count in range(count_start, count_end, 1) : + # read lattice file + for count in range(count_start, count_end, 1) : - csv_file_path = csv_file_directry + '/pt-%09d.0.csv' % (count) + csv_file_path = csv_file_directry + '/pt-%09d.0.csv' % (count) try : csv_file = open(csv_file_path, 'r') - dataset = [] + dataset = [] - for row in csv.reader(csv_file) : - dataset.append(row) + for row in csv.reader(csv_file) : + dataset.append(row) ### particle data - time = float(dataset[0][0]) + time = float(dataset[0][0]) - particles = [] + particles = [] - from ast import literal_eval + from ast import literal_eval - for data_id in dataset : - c_id = (float(data_id[1]), float(data_id[2]), float(data_id[3])) - s_id, l_id = literal_eval(data_id[6]) + for data_id in dataset : + c_id = (float(data_id[1]), float(data_id[2]), float(data_id[3])) + s_id, l_id = literal_eval(data_id[6]) - try : - #intensity = float(data_id[7]) - #intensity = (0.458/0.75)*self.psf_intensity + try : + #intensity = float(data_id[7]) + #intensity = (0.458/0.75)*self.psf_intensity - I0 = (0.458/0.75)*self.psf_intensity - I = numpy.array([0.1*i for i in range(2000)]) - A = 0.025; w0 = 14; w1 = 9 - ext_func = A*numpy.exp(-numpy.exp(-(I-I0)/w1) - ((I-I0)/w0) + 1) - ext_sum = ext_func.sum() - intensity = numpy.random.choice(I, None, p=ext_func/ext_sum) + I0 = (0.458/0.75)*self.psf_intensity + I = numpy.array([0.1*i for i in range(2000)]) + A = 0.025; w0 = 14; w1 = 9 + ext_func = A*numpy.exp(-numpy.exp(-(I-I0)/w1) - ((I-I0)/w0) + 1) + ext_sum = ext_func.sum() + intensity = numpy.random.choice(I, None, p=ext_func/ext_sum) - except Exception : - print('Error : Missing intensity') + except Exception : + print('Error : Missing intensity') - particles.append((c_id, s_id, l_id, intensity)) + particles.append((c_id, s_id, l_id, intensity)) - data.append([time, particles]) + data.append([time, particles]) except Exception : print('Error : ', csv_file_path, ' not found') - exit() + exit() - data.sort(lambda x, y:cmp(x[0], y[0])) + data.sort(lambda x, y:cmp(x[0], y[0])) # get run time self._set_data('spatiocyte_start_time', start) self._set_data('spatiocyte_end_time', end) self._set_data('spatiocyte_interval', interval) - # get data + # get data self._set_data('spatiocyte_data', data) # get species properties self._set_data('spatiocyte_species_id', species_id) self._set_data('spatiocyte_index', species_index) - #self._set_data('spatiocyte_diffusion', species_diffusion) - self._set_data('spatiocyte_radius', species_radius) + #self._set_data('spatiocyte_diffusion', species_diffusion) + self._set_data('spatiocyte_radius', species_radius) # get lattice properties #self._set_data('spatiocyte_lattice_id', map(lambda x : x[0], lattice)) self._set_data('spatiocyte_lengths', lengths) self._set_data('spatiocyte_VoxelRadius', voxel_r) self._set_data('spatiocyte_theNormalizedVoxelRadius', 0.5) - + # set observable if observable is None : index = [True for i in range(len(self.spatiocyte_index))] - else : + else : index = map(lambda x : True if x.find(observable) > -1 else False, self.spatiocyte_index) - #index = [False, True] + #index = [False, True] self.spatiocyte_observables = copy.copy(index) print('\tStart time =', self.spatiocyte_start_time, 'sec') print('\tEnd time =', self.spatiocyte_end_time, 'sec') print('\tInterval =', self.spatiocyte_interval, 'sec') - print('\tVoxel radius =', self.spatiocyte_VoxelRadius, 'm') + print('\tVoxel radius =', self.spatiocyte_VoxelRadius, 'm') print('\tCompartment lengths :', self.spatiocyte_lengths, 'voxels') print('\tSpecies Index :', self.spatiocyte_index) print('\tObservable :', self.spatiocyte_observables) - # Visualization error - if self.spatiocyte_species_id is None: - raise VisualizerError('Cannot find species_id in any given csv files') + # Visualization error + if self.spatiocyte_species_id is None: + raise VisualizerError('Cannot find species_id in any given csv files') - if len(self.spatiocyte_data) == 0: - raise VisualizerError('Cannot find spatiocyte_data in any given csv files: ' \ - + ', '.join(csv_file_directry)) + if len(self.spatiocyte_data) == 0: + raise VisualizerError('Cannot find spatiocyte_data in any given csv files: ' \ + + ', '.join(csv_file_directry)) - if len(self.spatiocyte_index) == 0 : - raise VisualizerError('Cannot find spatiocyte_index in any given csv files: ' \ - + ', '.join(csv_file_directry)) + if len(self.spatiocyte_index) == 0 : + raise VisualizerError('Cannot find spatiocyte_index in any given csv files: ' \ + + ', '.join(csv_file_directry)) class MorigaVisualizer(TIRFMVisualizer) : - ''' - Moriga-TIRFM visualization class - ''' + ''' + Moriga-TIRFM visualization class + ''' - def __init__(self, configs=MorigaConfigs(), effects=PhysicalEffects()) : + def __init__(self, configs=MorigaConfigs(), effects=PhysicalEffects()) : - assert isinstance(configs, MorigaConfigs) - self.configs = configs + assert isinstance(configs, MorigaConfigs) + self.configs = configs - assert isinstance(effects, PhysicalEffects) - self.effects = effects + assert isinstance(effects, PhysicalEffects) + self.effects = effects - """ - Check and create the folder for image file. - """ - if not os.path.exists(self.configs.image_file_dir): - os.makedirs(self.configs.image_file_dir) - #else: - # for file in os.listdir(self.configs.movie_image_file_dir): - # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) + """ + Check and create the folder for image file. + """ + if not os.path.exists(self.configs.image_file_dir): + os.makedirs(self.configs.image_file_dir) + #else: + # for file in os.listdir(self.configs.movie_image_file_dir): + # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) - """ - set optical path from source to detector - """ - self.configs.set_Detection_path() + """ + set optical path from source to detector + """ + self.configs.set_Detection_path() - def rewrite_InputData(self, output_file_dir=None) : + def rewrite_InputData(self, output_file_dir=None) : - if not os.path.exists(output_file_dir): - os.makedirs(output_file_dir) + if not os.path.exists(output_file_dir): + os.makedirs(output_file_dir) - # Active states per detection time - start = self.configs.spatiocyte_start_time - end = self.configs.spatiocyte_end_time - dt = self.configs.spatiocyte_interval + # Active states per detection time + start = self.configs.spatiocyte_start_time + end = self.configs.spatiocyte_end_time + dt = self.configs.spatiocyte_interval - # spatiocyte size - radius = self.configs.spatiocyte_VoxelRadius - volume = 4.0/3.0*numpy.pi*radius**3 - depth = 2.0*radius + # spatiocyte size + radius = self.configs.spatiocyte_VoxelRadius + volume = 4.0/3.0*numpy.pi*radius**3 + depth = 2.0*radius - # set signal probability function (PDF) - I0 = 46.7 - A = 0.0274 - w0 = 12.2 - w1 = 9.40 - I = numpy.array([0.01*i for i in range(2000000)]) - p_signal = A*numpy.exp(-numpy.exp(-((I-I0)/w1))-((I-I0)/w0)+1) - p_sum = p_signal.sum() + # set signal probability function (PDF) + I0 = 46.7 + A = 0.0274 + w0 = 12.2 + w1 = 9.40 + I = numpy.array([0.01*i for i in range(2000000)]) + p_signal = A*numpy.exp(-numpy.exp(-((I-I0)/w1))-((I-I0)/w0)+1) + p_sum = p_signal.sum() - # sequence - start_count = int(start/dt) - end_count = int(end/dt) + # sequence + start_count = int(start/dt) + end_count = int(end/dt) - # copy input file - csv_input = self.configs.spatiocyte_file_directry + '/pt-input.csv' - shutil.copyfile(csv_input, output_file_dir + '/pt-input.csv') + # copy input file + csv_input = self.configs.spatiocyte_file_directry + '/pt-input.csv' + shutil.copyfile(csv_input, output_file_dir + '/pt-input.csv') - for count in range(start_count, end_count, 1) : + for count in range(start_count, end_count, 1) : - # read input file - csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (count) + # read input file + csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (count) - csv_list = list(csv.reader(open(csv_file_path, 'r'))) - #csv_temp = list(csv.reader(open(csv_file_path, 'r'))) - #csv_list = [csv_temp[0]] - dataset = numpy.array(csv_list) + csv_list = list(csv.reader(open(csv_file_path, 'r'))) + #csv_temp = list(csv.reader(open(csv_file_path, 'r'))) + #csv_list = [csv_temp[0]] + dataset = numpy.array(csv_list) - time = float(dataset[0][0]) + time = float(dataset[0][0]) - N_particles = len(csv_list) + N_particles = len(csv_list) - I0 = self.configs.psf_intensity - Noise = self.configs.psf_intensity_noise - photons = numpy.random.normal(I0, Noise, N_particles) - signals = numpy.random.choice(I, N_particles, p=p_signal/p_sum) - photons = (0.458/0.75)*signals + I0 = self.configs.psf_intensity + Noise = self.configs.psf_intensity_noise + photons = numpy.random.normal(I0, Noise, N_particles) + signals = numpy.random.choice(I, N_particles, p=p_signal/p_sum) + photons = (0.458/0.75)*signals - check = (photons > 0).astype('int') - Intensity = numpy.abs(photons*check) + check = (photons > 0).astype('int') + Intensity = numpy.abs(photons*check) - new_dataset = numpy.column_stack((dataset, Intensity)) + new_dataset = numpy.column_stack((dataset, Intensity)) - #print float(new_dataset[0][0]), photons + #print float(new_dataset[0][0]), photons - # write output file - output_file = output_file_dir + '/pt-%09d.0.csv' % (count) + # write output file + output_file = output_file_dir + '/pt-%09d.0.csv' % (count) - with open(output_file, 'w') as f : - writer = csv.writer(f) - writer.writerows(new_dataset) + with open(output_file, 'w') as f : + writer = csv.writer(f) + writer.writerows(new_dataset) - def get_molecule_plane(self, cell, time, data, pid, p_b, p_0) : + def get_molecule_plane(self, cell, time, data, pid, p_b, p_0) : - voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 - - # particles coordinate, species and lattice IDs - c_id, s_id, l_id, intensity = data - - sid_array = numpy.array(self.configs.spatiocyte_species_id) - s_index = (numpy.abs(sid_array - int(s_id))).argmin() + voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 - if self.configs.spatiocyte_observables[s_index] is True : + # particles coordinate, species and lattice IDs + c_id, s_id, l_id, intensity = data - # particles coordinate in real(nm) scale - #pos = self.get_coordinate(c_id) - #p_i = numpy.array(pos)*voxel_size - p_i = self.get_coordinate(c_id) + sid_array = numpy.array(self.configs.spatiocyte_species_id) + s_index = (numpy.abs(sid_array - int(s_id))).argmin() - # get signal matrix - signal = self.get_signal(time, pid, s_index, p_i, p_b, p_0, intensity) + if self.configs.spatiocyte_observables[s_index] is True : - # add signal matrix to image plane - self.overwrite_signal(cell, signal, p_i) + # particles coordinate in real(nm) scale + #pos = self.get_coordinate(c_id) + #p_i = numpy.array(pos)*voxel_size + p_i = self.get_coordinate(c_id) + # get signal matrix + signal = self.get_signal(time, pid, s_index, p_i, p_b, p_0, intensity) + # add signal matrix to image plane + self.overwrite_signal(cell, signal, p_i) - def get_signal(self, time, pid, s_index, p_i, p_b, p_0, intensity) : - # set focal point - x_0, y_0, z_0 = p_0 - # set source center - x_b, y_b, z_b = p_b + def get_signal(self, time, pid, s_index, p_i, p_b, p_0, intensity) : - # set particle position - x_i, y_i, z_i = p_i + # set focal point + x_0, y_0, z_0 = p_0 - # - r = self.configs.radial - d = self.configs.depth + # set source center + x_b, y_b, z_b = p_b - # fluorophore axial position - d_f = abs(x_i - x_b) + # set particle position + x_i, y_i, z_i = p_i - if (d_f < len(d)) : - fluo_depth = d_f - else : - fluo_depth = d[-1] + # + r = self.configs.radial + d = self.configs.depth - # get fluorophore PSF - fluo_psf = self.fluo_psf[int(fluo_depth)] + # fluorophore axial position + d_f = abs(x_i - x_b) - # - voxel_radius = self.configs.spatiocyte_VoxelRadius - voxel_size = (2.0*voxel_radius)/1e-9 - Np = int(self.configs.image_scaling*voxel_size) + if (d_f < len(d)) : + fluo_depth = d_f + else : + fluo_depth = d[-1] - Norm = intensity/(Np**2) + # get fluorophore PSF + fluo_psf = self.fluo_psf[int(fluo_depth)] - # get signal - #signal = fluo_psf - signal = Norm*fluo_psf + # + voxel_radius = self.configs.spatiocyte_VoxelRadius + voxel_size = (2.0*voxel_radius)/1e-9 + Np = int(self.configs.image_scaling*voxel_size) - return signal + Norm = intensity/(Np**2) + # get signal + #signal = fluo_psf + signal = Norm*fluo_psf + return signal - def output_frames_each_process(self, start_count, stop_count): - # define observational image plane in nm-scale - voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 - ## cell size (nm scale) - Nz = int(self.configs.spatiocyte_lengths[2]*voxel_size) - Ny = int(self.configs.spatiocyte_lengths[1]*voxel_size) - Nx = int(self.configs.spatiocyte_lengths[0]*voxel_size) + def output_frames_each_process(self, start_count, stop_count): - # focal point - p_0 = numpy.array([Nx, Ny, Nz])*self.configs.detector_focal_point + # define observational image plane in nm-scale + voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 - # print numpy.array([Nx, Ny, Nz])*self.configs.detector_focal_point # [ 1650. 5500. 5500.] - # print numpy.array([Nx, Ny, Nz]) # [ 5500 11000 11000] - # print self.configs.detector_focal_point # (0.3, 0.5, 0.5) - + ## cell size (nm scale) + Nz = int(self.configs.spatiocyte_lengths[2]*voxel_size) + Ny = int(self.configs.spatiocyte_lengths[1]*voxel_size) + Nx = int(self.configs.spatiocyte_lengths[0]*voxel_size) - # beam position : Assuming beam position = focal point for temporary - p_b = copy.copy(p_0) + # focal point + p_0 = numpy.array([Nx, Ny, Nz])*self.configs.detector_focal_point + # print numpy.array([Nx, Ny, Nz])*self.configs.detector_focal_point # [ 1650. 5500. 5500.] + # print numpy.array([Nx, Ny, Nz]) # [ 5500 11000 11000] + # print self.configs.detector_focal_point # (0.3, 0.5, 0.5) - # exposure time - exposure_time = self.configs.detector_exposure_time - start_time = self.configs.spatiocyte_start_time + # beam position : Assuming beam position = focal point for temporary + p_b = copy.copy(p_0) - time = exposure_time * start_count - end = exposure_time * stop_count - # data-time interval - data_interval = self.configs.spatiocyte_interval - delta_time = int(round(exposure_time/data_interval)) + # exposure time + exposure_time = self.configs.detector_exposure_time - # create frame data composed by frame element data - count = start_count - count0 = int(round(start_time / exposure_time)) + start_time = self.configs.spatiocyte_start_time - # initialize Physical effects - #length0 = len(self.configs.spatiocyte_data[0][1]) - #self.effects.set_states(t0, length0) + time = exposure_time * start_count + end = exposure_time * stop_count - while (time < end-1e-9) : - print('time : ', time, ' sec (', count, ')') + # data-time interval + data_interval = self.configs.spatiocyte_interval + delta_time = int(round(exposure_time/data_interval)) - # define cell - cell = numpy.zeros(shape=(Nz, Ny)) + # create frame data composed by frame element data + count = start_count + count0 = int(round(start_time / exposure_time)) - count_start = (count - count0)*delta_time - count_end = (count - count0 + 1)*delta_time + # initialize Physical effects + #length0 = len(self.configs.spatiocyte_data[0][1]) + #self.effects.set_states(t0, length0) - frame_data = self.configs.spatiocyte_data[count_start:count_end] + while (time < end-1e-9) : + print('time : ', time, ' sec (', count, ')') - # loop for frame data - for i, (i_time, data) in enumerate(frame_data): - print('\t', '%02d-th frame : ' % (i), i_time, ' sec') - # loop for particles - for j, data_j in enumerate(data): - self.get_molecule_plane(cell, i_time, data_j, j, p_b, p_0) + # define cell + cell = numpy.zeros(shape=(Nz, Ny)) + count_start = (count - count0)*delta_time + count_end = (count - count0 + 1)*delta_time - #if (numpy.amax(cell) > 0): - camera = self.detector_output(cell) - - # save data to numpy-binary file - image_file_name = os.path.join(self.configs.image_file_dir, - self.configs.image_file_name_format % (count+500)) - #'image_%09.03f' % (self.configs.psf_intensity)) - numpy.save(image_file_name, camera) + frame_data = self.configs.spatiocyte_data[count_start:count_end] - time += exposure_time - count += 1 + # loop for frame data + for i, (i_time, data) in enumerate(frame_data): + print('\t', '%02d-th frame : ' % (i), i_time, ' sec') + # loop for particles + for j, data_j in enumerate(data): + self.get_molecule_plane(cell, i_time, data_j, j, p_b, p_0) + #if (numpy.amax(cell) > 0): + camera = self.detector_output(cell) - def detector_output(self, cell) : + # save data to numpy-binary file + image_file_name = os.path.join(self.configs.image_file_dir, + self.configs.image_file_name_format % (count+500)) + #'image_%09.03f' % (self.configs.psf_intensity)) + numpy.save(image_file_name, camera) - # Detector Output - voxel_radius = self.configs.spatiocyte_VoxelRadius - voxel_size = (2.0*voxel_radius)/1e-9 + time += exposure_time + count += 1 - Nw_pixel = self.configs.detector_image_size[0] - Nh_pixel = self.configs.detector_image_size[1] - Np = int(self.configs.image_scaling*voxel_size) - # image in nm-scale - Nw_camera = Nw_pixel*Np - Nh_camera = Nh_pixel*Np + def detector_output(self, cell) : - Nw_cell = len(cell) - Nh_cell = len(cell[0]) + # Detector Output + voxel_radius = self.configs.spatiocyte_VoxelRadius + voxel_size = (2.0*voxel_radius)/1e-9 - if (Nw_camera > Nw_cell) : - w_cam_from = int((Nw_camera - Nw_cell)/2.0) - w_cam_to = w_cam_from + Nw_cell - w_cel_from = 0 - w_cel_to = Nw_cell - else : - w_cam_from = 0 - w_cam_to = Nw_camera - w_cel_from = int((Nw_cell - Nw_camera)/2.0) - w_cel_to = w_cel_from + Nw_camera + Nw_pixel = self.configs.detector_image_size[0] + Nh_pixel = self.configs.detector_image_size[1] - if (Nh_camera > Nh_cell) : - h_cam_from = int((Nh_camera - Nh_cell)/2.0) - h_cam_to = h_cam_from + Nh_cell - h_cel_from = 0 - h_cel_to = Nh_cell - else : - h_cam_from = 0 - h_cam_to = int(Nh_camera) - h_cel_from = int((Nh_cell - Nh_camera)/2.0) - h_cel_to = h_cel_from + Nh_camera + Np = int(self.configs.image_scaling*voxel_size) + # image in nm-scale + Nw_camera = Nw_pixel*Np + Nh_camera = Nh_pixel*Np - # image in nm-scale - plane = cell[w_cel_from:w_cel_to, h_cel_from:h_cel_to] + Nw_cell = len(cell) + Nh_cell = len(cell[0]) - # declear cell image in pixel-scale - cell_pixel = numpy.zeros(shape=(Nw_cell/Np, Nh_cell/Np)) + if (Nw_camera > Nw_cell) : + w_cam_from = int((Nw_camera - Nw_cell)/2.0) + w_cam_to = w_cam_from + Nw_cell + w_cel_from = 0 + w_cel_to = Nw_cell + else : + w_cam_from = 0 + w_cam_to = Nw_camera + w_cel_from = int((Nw_cell - Nw_camera)/2.0) + w_cel_to = w_cel_from + Nw_camera - # Signal (photon distribution on cell) - for i in range(Nw_cell/Np) : - for j in range(Nh_cell/Np) : + if (Nh_camera > Nh_cell) : + h_cam_from = int((Nh_camera - Nh_cell)/2.0) + h_cam_to = h_cam_from + Nh_cell + h_cel_from = 0 + h_cel_to = Nh_cell + else : + h_cam_from = 0 + h_cam_to = int(Nh_camera) + h_cel_from = int((Nh_cell - Nh_camera)/2.0) + h_cel_to = h_cel_from + Nh_camera - # get photons - photons = numpy.sum(plane[i*Np:(i+1)*Np,j*Np:(j+1)*Np]) - cell_pixel[i][j] = photons -# if (photons > 0) : + # image in nm-scale + plane = cell[w_cel_from:w_cel_to, h_cel_from:h_cel_to] + + # declear cell image in pixel-scale + cell_pixel = numpy.zeros(shape=(Nw_cell/Np, Nh_cell/Np)) + + # Signal (photon distribution on cell) + for i in range(Nw_cell/Np) : + for j in range(Nh_cell/Np) : + + # get photons + photons = numpy.sum(plane[i*Np:(i+1)*Np,j*Np:(j+1)*Np]) + cell_pixel[i][j] = photons + +# if (photons > 0) : # -# # get crosstalk -# if (self.effects.detector_crosstalk_switch == True) : +# # get crosstalk +# if (self.effects.detector_crosstalk_switch == True) : # -# width = self.effects.detector_crosstalk_width +# width = self.effects.detector_crosstalk_width # -# n_i = numpy.random.normal(0, width, photons) -# n_j = numpy.random.normal(0, width, photons) +# n_i = numpy.random.normal(0, width, photons) +# n_j = numpy.random.normal(0, width, photons) # -# #i_bins = int(numpy.amax(n_i) - numpy.amin(n_i)) -# #j_bins = int(numpy.amax(n_j) - numpy.amin(n_j)) +# #i_bins = int(numpy.amax(n_i) - numpy.amin(n_i)) +# #j_bins = int(numpy.amax(n_j) - numpy.amin(n_j)) # -# smeared_photons, edge_i, edge_j = numpy.histogram2d(n_i, n_j, bins=(24, 24), +# smeared_photons, edge_i, edge_j = numpy.histogram2d(n_i, n_j, bins=(24, 24), # range=[[-12,12],[-12,12]]) # -# # smeared photon distributions -# cell_pixel = self.overwrite_smeared(cell_pixel, smeared_photons, i, j) +# # smeared photon distributions +# cell_pixel = self.overwrite_smeared(cell_pixel, smeared_photons, i, j) # -# else : -# cell_pixel[i][j] = photons - - # declear photon distribution for camera image - #camera_pixel = numpy.zeros([Nw_pixel, Nh_pixel, 4]) - camera_pixel = numpy.zeros([Nw_pixel, Nh_pixel, 2]) - - w_cam_from = int(w_cam_from/Np) - w_cam_to = int(w_cam_to/Np) - h_cam_from = int(h_cam_from/Np) - h_cam_to = int(h_cam_to/Np) +# else : +# cell_pixel[i][j] = photons - w_cel_from = int(w_cel_from/Np) - w_cel_to = int(w_cel_to/Np) - h_cel_from = int(h_cel_from/Np) - h_cel_to = int(h_cel_to/Np) + # declear photon distribution for camera image + #camera_pixel = numpy.zeros([Nw_pixel, Nh_pixel, 4]) + camera_pixel = numpy.zeros([Nw_pixel, Nh_pixel, 2]) - ddw = (w_cam_to - w_cam_from) - (w_cel_to - w_cel_from) - ddh = (h_cam_to - h_cam_from) - (h_cel_to - h_cel_from) + w_cam_from = int(w_cam_from/Np) + w_cam_to = int(w_cam_to/Np) + h_cam_from = int(h_cam_from/Np) + h_cam_to = int(h_cam_to/Np) - if (ddw > 0) : w_cam_to = w_cam_to - ddw - elif (ddw < 0) : w_cel_to = w_cel_to - ddw + w_cel_from = int(w_cel_from/Np) + w_cel_to = int(w_cel_to/Np) + h_cel_from = int(h_cel_from/Np) + h_cel_to = int(h_cel_to/Np) - if (ddh > 0) : h_cam_to = h_cam_to - ddh - elif (ddh < 0) : h_cel_to = h_cel_to - ddh + ddw = (w_cam_to - w_cam_from) - (w_cel_to - w_cel_from) + ddh = (h_cam_to - h_cam_from) - (h_cel_to - h_cel_from) - # place cell_pixel data to camera image - camera_pixel[w_cam_from:w_cam_to, h_cam_from:h_cam_to, 0] = cell_pixel[w_cel_from:w_cel_to, h_cel_from:h_cel_to] + if (ddw > 0) : w_cam_to = w_cam_to - ddw + elif (ddw < 0) : w_cel_to = w_cel_to - ddw - print('scaling [nm/pixel] :', Np) - print('width :', w_cam_from, w_cam_to) - print('height :', h_cam_from, h_cam_to) + if (ddh > 0) : h_cam_to = h_cam_to - ddh + elif (ddh < 0) : h_cel_to = h_cel_to - ddh - # set seed for random number - numpy.random.seed() + # place cell_pixel data to camera image + camera_pixel[w_cam_from:w_cam_to, h_cam_from:h_cam_to, 0] = cell_pixel[w_cel_from:w_cel_to, h_cel_from:h_cel_to] - # CMOS (readout noise probability ditributions) - if (self.configs.detector_type == "CMOS") : -# noise_data = numpy.loadtxt("catalog/detector/RNDist_F40.csv", delimiter=',') - noise_data = numpy.loadtxt(os.path.join(os.path.abspath(os.path.dirname(__file__)), - 'catalog/detector/RNDist_F40.csv'), delimiter=',') - Nr_cmos = noise_data[:,0] - p_noise = noise_data[:,1] - p_nsum = p_noise.sum() + print('scaling [nm/pixel] :', Np) + print('width :', w_cam_from, w_cam_to) + print('height :', h_cam_from, h_cam_to) + # set seed for random number + numpy.random.seed() - # conversion : photon --> photoelectron --> ADC count - for i in range(Nw_pixel) : - for j in range(Nh_pixel) : + # CMOS (readout noise probability ditributions) + if (self.configs.detector_type == "CMOS") : +# noise_data = numpy.loadtxt("catalog/detector/RNDist_F40.csv", delimiter=',') + noise_data = numpy.loadtxt(os.path.join(os.path.abspath(os.path.dirname(__file__)), + 'catalog/detector/RNDist_F40.csv'), delimiter=',') + Nr_cmos = noise_data[:,0] + p_noise = noise_data[:,1] + p_nsum = p_noise.sum() - # pixel position - pixel = (i, j) - # Detector : Quantum Efficiency - #index = int(self.configs.psf_wavelength) - int(self.configs.wave_length[0]) - #QE = self.configs.detector_qeff[index] - QE = self.configs.detector_qeff + # conversion : photon --> photoelectron --> ADC count + for i in range(Nw_pixel) : + for j in range(Nh_pixel) : - # get signal (photons) - Photons = camera_pixel[i][j][0] + # pixel position + pixel = (i, j) - # get constant background (photoelectrons) - Photons_bg = self.effects.background_mean - Photons += Photons_bg + # Detector : Quantum Efficiency + #index = int(self.configs.psf_wavelength) - int(self.configs.wave_length[0]) + #QE = self.configs.detector_qeff[index] + QE = self.configs.detector_qeff - # get signal (expectation) - Exp = QE*Photons + # get signal (photons) + Photons = camera_pixel[i][j][0] - # select Camera type - if (self.configs.detector_type == "CMOS") : + # get constant background (photoelectrons) + Photons_bg = self.effects.background_mean + Photons += Photons_bg - # get signal (poisson distributions) - signal = numpy.random.poisson(Exp, None) + # get signal (expectation) + Exp = QE*Photons - #Noise = numpy.sqrt(self.signal_noise**2 + self.background_noise**2) - #signal = numpy.random.normal(Exp, Noise, None) - #if (signal < 0) : signal = 0 + # select Camera type + if (self.configs.detector_type == "CMOS") : - # get detector noise (photoelectrons) - noise = numpy.random.choice(Nr_cmos, None, p=p_noise/p_nsum) - Nr = 1.3 + # get signal (poisson distributions) + signal = numpy.random.poisson(Exp, None) + #Noise = numpy.sqrt(self.signal_noise**2 + self.background_noise**2) + #signal = numpy.random.normal(Exp, Noise, None) + #if (signal < 0) : signal = 0 - elif (self.configs.detector_type == "EMCCD") : + # get detector noise (photoelectrons) + noise = numpy.random.choice(Nr_cmos, None, p=p_noise/p_nsum) + Nr = 1.3 - # get signal (photoelectrons) - if (Exp > 0) : - # get EM gain - M = self.configs.detector_emgain + elif (self.configs.detector_type == "EMCCD") : - # set probability distributions - s_min = M*int(Exp - 5.0*numpy.sqrt(Exp) - 10) - s_max = M*int(Exp + 5.0*numpy.sqrt(Exp) + 10) + # get signal (photoelectrons) + if (Exp > 0) : - if (s_min < 0) : s_min = 0 + # get EM gain + M = self.configs.detector_emgain - s = numpy.array([k for k in range(s_min, s_max)]) - #p_signal = numpy.array(map(lambda x : self.prob_EMCCD(x, Exp), s)) - p_signal = self.prob_EMCCD(s, Exp) - p_ssum = p_signal.sum() + # set probability distributions + s_min = M*int(Exp - 5.0*numpy.sqrt(Exp) - 10) + s_max = M*int(Exp + 5.0*numpy.sqrt(Exp) + 10) - # get signal (photoelectrons) - signal = numpy.random.choice(s, None, p=p_signal/p_ssum) + if (s_min < 0) : s_min = 0 - else : - signal = 0 + s = numpy.array([k for k in range(s_min, s_max)]) + #p_signal = numpy.array(map(lambda x : self.prob_EMCCD(x, Exp), s)) + p_signal = self.prob_EMCCD(s, Exp) + p_ssum = p_signal.sum() - # get detector noise (photoelectrons) - Nr = self.configs.detector_readout_noise + # get signal (photoelectrons) + signal = numpy.random.choice(s, None, p=p_signal/p_ssum) - if (Nr > 0) : - noise = numpy.random.normal(0, Nr, None) - else : noise = 0 + else : + signal = 0 + # get detector noise (photoelectrons) + Nr = self.configs.detector_readout_noise - elif (self.configs.detector_type == "CCD") : + if (Nr > 0) : + noise = numpy.random.normal(0, Nr, None) + else : noise = 0 - # get signal (poisson distributions) - signal = numpy.random.poisson(Exp, None) - # get detector noise (photoelectrons) - Nr = self.configs.detector_readout_noise + elif (self.configs.detector_type == "CCD") : - if (Nr > 0) : - noise = numpy.random.normal(0, Nr, None) - else : noise = 0 + # get signal (poisson distributions) + signal = numpy.random.poisson(Exp, None) - # A/D converter : Expectation --> ADC counts - EXP = self.get_ADC_value(pixel, Exp+Nr) + # get detector noise (photoelectrons) + Nr = self.configs.detector_readout_noise - # A/D converter : Photoelectrons --> ADC counts - PE = signal + noise - ADC = self.get_ADC_value(pixel, PE) + if (Nr > 0) : + noise = numpy.random.normal(0, Nr, None) + else : noise = 0 - # set data in image array - #camera_pixel[i][j] = [Photons, Exp, PE, ADC] - camera_pixel[i][j] = [EXP, ADC] + # A/D converter : Expectation --> ADC counts + EXP = self.get_ADC_value(pixel, Exp+Nr) - - return camera_pixel + # A/D converter : Photoelectrons --> ADC counts + PE = signal + noise + ADC = self.get_ADC_value(pixel, PE) + # set data in image array + #camera_pixel[i][j] = [Photons, Exp, PE, ADC] + camera_pixel[i][j] = [EXP, ADC] + return camera_pixel diff --git a/bioimaging/moriga_rewrite_script.py b/bioimaging/moriga_rewrite_script.py index 54a4f3e..da0b81e 100755 --- a/bioimaging/moriga_rewrite_script.py +++ b/bioimaging/moriga_rewrite_script.py @@ -8,41 +8,39 @@ def test_tirfm(t0, t1, nm, I0, Bg) : - # create TIRF Microscopy (for Moriga-configuration) - tirfm = MorigaConfigs() - tirfm.set_Fluorophore(wave_length=532, intensity=I0, width=(110,400)) - tirfm.set_DichroicMirror('FF562-Di03-25x36') - #tirfm.set_EmissionFilter('FF01-593_40-25') - tirfm.set_Magnification(Mag=89) + # create TIRF Microscopy (for Moriga-configuration) + tirfm = MorigaConfigs() + tirfm.set_Fluorophore(wave_length=532, intensity=I0, width=(110,400)) + tirfm.set_DichroicMirror('FF562-Di03-25x36') + #tirfm.set_EmissionFilter('FF01-593_40-25') + tirfm.set_Magnification(Mag=89) - # Detector : CMOS Camera - tirfm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ - focal_point=(0.0,0.5,0.5), exposure_time=0.1, QE=0.73) - tirfm.set_ADConverter(bit=16, offset=100, fullwell=30000) + # Detector : CMOS Camera + tirfm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ + focal_point=(0.0,0.5,0.5), exposure_time=0.1, QE=0.73) + tirfm.set_ADConverter(bit=16, offset=100, fullwell=30000) - ### Input data - #tirfm.reset_InputData('/home/masaki/wrk/spatiocyte/models/data_moriga/model_%03dA/index_%03d' % (NNN,index), \ - # start=t0, end=t1, observable="A") - tirfm.reset_InputData('/home/masaki/wrk/spatiocyte/models/data_moriga/model_002A_%03dnm' % (nm), \ - start=t0, end=t1, observable="A") + ### Input data + #tirfm.reset_InputData('/home/masaki/wrk/spatiocyte/models/data_moriga/model_%03dA/index_%03d' % (NNN,index), \ + # start=t0, end=t1, observable="A") + tirfm.reset_InputData('/home/masaki/wrk/spatiocyte/models/data_moriga/model_002A_%03dnm' % (nm), \ + start=t0, end=t1, observable="A") - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=Bg) + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=Bg) - # create image and movie - create = MorigaVisualizer(configs=tirfm, effects=physics) - create.rewrite_InputData(output_file_dir='./data_moriga_01/csv/model_002A_%03dnm_%03ddeg' % (nm,deg)) - #create.output_frames(num_div=16) + # create image and movie + create = MorigaVisualizer(configs=tirfm, effects=physics) + create.rewrite_InputData(output_file_dir='./data_moriga_01/csv/model_002A_%03dnm_%03ddeg' % (nm,deg)) + #create.output_frames(num_div=16) if __name__ == "__main__": - #t0 = int(sys.argv[1]) - #t1 = int(sys.argv[2]) - nm = int(sys.argv[1]) - - test_tirfm(0, 50, nm, I0, Bg) - + #t0 = int(sys.argv[1]) + #t1 = int(sys.argv[2]) + nm = int(sys.argv[1]) + test_tirfm(0, 50, nm, I0, Bg) diff --git a/bioimaging/moriga_script.py b/bioimaging/moriga_script.py index c8dd939..10e2e9f 100755 --- a/bioimaging/moriga_script.py +++ b/bioimaging/moriga_script.py @@ -8,44 +8,42 @@ def test_tirfm(t0, t1, nm, I0, Bg) : - # create TIRF Microscopy (for Moriga-configuration) - tirfm = MorigaConfigs() - tirfm.set_Fluorophore(wave_length=532, intensity=I0, width=(110,400)) - tirfm.set_DichroicMirror('FF562-Di03-25x36') - #tirfm.set_EmissionFilter('FF01-593_40-25') - tirfm.set_Magnification(Mag=89) + # create TIRF Microscopy (for Moriga-configuration) + tirfm = MorigaConfigs() + tirfm.set_Fluorophore(wave_length=532, intensity=I0, width=(110,400)) + tirfm.set_DichroicMirror('FF562-Di03-25x36') + #tirfm.set_EmissionFilter('FF01-593_40-25') + tirfm.set_Magnification(Mag=89) - # Detector : CMOS Camera - tirfm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ - focal_point=(0.0,0.5,0.5), exposure_time=0.1, QE=0.73) - tirfm.set_ADConverter(bit=16, offset=100, fullwell=30000) + # Detector : CMOS Camera + tirfm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ + focal_point=(0.0,0.5,0.5), exposure_time=0.1, QE=0.73) + tirfm.set_ADConverter(bit=16, offset=100, fullwell=30000) - ### Output data - tirfm.set_OutputData(image_file_dir='/home/masaki/bioimaging_4public/data_moriga_02/numpys/model_002A_%03dnm_%02dI0_%02dBg_extfunc' % (nm,I0,Bg)) + ### Output data + tirfm.set_OutputData(image_file_dir='/home/masaki/bioimaging_4public/data_moriga_02/numpys/model_002A_%03dnm_%02dI0_%02dBg_extfunc' % (nm,I0,Bg)) - ### Input data - #tirfm.set_InputData('./data_moriga_02/csv/model_002A_%03dnm' % (nm), start=t0, end=t1, observable="A") - tirfm.set_InputData('/home/masaki/wrk/spatiocyte/models/data_moriga/model_002A_%03dnm' % (nm), start=t0, end=t1, observable="A") + ### Input data + #tirfm.set_InputData('./data_moriga_02/csv/model_002A_%03dnm' % (nm), start=t0, end=t1, observable="A") + tirfm.set_InputData('/home/masaki/wrk/spatiocyte/models/data_moriga/model_002A_%03dnm' % (nm), start=t0, end=t1, observable="A") - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=(0.458/0.75)*Bg) + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=(0.458/0.75)*Bg) - # create image and movie - create = MorigaVisualizer(configs=tirfm, effects=physics) - create.output_frames(num_div=16) + # create image and movie + create = MorigaVisualizer(configs=tirfm, effects=physics) + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - nm = int(sys.argv[3]) - I0 = int(sys.argv[4]) - Bg = int(sys.argv[5]) - - test_tirfm(t0, t1, nm, I0, Bg) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + nm = int(sys.argv[3]) + I0 = int(sys.argv[4]) + Bg = int(sys.argv[5]) + test_tirfm(t0, t1, nm, I0, Bg) diff --git a/bioimaging/nikon_oscillator.py b/bioimaging/nikon_oscillator.py index 063d5f2..d772883 100755 --- a/bioimaging/nikon_oscillator.py +++ b/bioimaging/nikon_oscillator.py @@ -7,45 +7,43 @@ def test_oscillator(t0, t1) : - # create TIRFM imaging - oscillator = EPIFMConfigs() - oscillator.set_Shutter(start_time=t0, end_time=t1) - #oscillator.set_Shutter(start_time=t0, end_time=t1, time_open=0.150, time_lapse=20) - oscillator.set_LightSource(source_type='LASER', wave_length=532, flux_density=20, angle=72) - oscillator.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)', normalization=1.0) - oscillator.set_DichroicMirror('FF562-Di03-25x36') - oscillator.set_Magnification(Mag=234) + # create TIRFM imaging + oscillator = EPIFMConfigs() + oscillator.set_Shutter(start_time=t0, end_time=t1) + #oscillator.set_Shutter(start_time=t0, end_time=t1, time_open=0.150, time_lapse=20) + oscillator.set_LightSource(source_type='LASER', wave_length=532, flux_density=20, angle=72) + oscillator.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)', normalization=1.0) + oscillator.set_DichroicMirror('FF562-Di03-25x36') + oscillator.set_Magnification(Mag=234) - # Detector : EMCCD Camera - oscillator.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, exposure_time=0.20, \ - focal_point=(0.0,0.5,0.5), QE=0.92, readout_noise=100, emgain=300) - oscillator.set_ADConverter(bit=16, offset=2000, fullwell=800000) + # Detector : EMCCD Camera + oscillator.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, exposure_time=0.20, \ + focal_point=(0.0,0.5,0.5), QE=0.92, readout_noise=100, emgain=300) + oscillator.set_ADConverter(bit=16, offset=2000, fullwell=800000) - # Output data - oscillator.set_OutputData(image_file_dir='./data_for_nikon/numpys/oscillator00') + # Output data + oscillator.set_OutputData(image_file_dir='./data_for_nikon/numpys/oscillator00') - # Input data - csv_dir = '/home/masaki/wrk/spatiocyte/models/Nikon/data/oscillator00' - oscillator.set_InputFile(csv_dir, observable="Min") - oscillator.set_ShapeFile(csv_dir) + # Input data + csv_dir = '/home/masaki/wrk/spatiocyte/models/Nikon/data/oscillator00' + oscillator.set_InputFile(csv_dir, observable="Min") + oscillator.set_ShapeFile(csv_dir) - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0.01) - physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) - #physics.set_photobleaching(tau0=2.27, alpha=0.73) + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0.01) + physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) + #physics.set_photobleaching(tau0=2.27, alpha=0.73) - # create image - create = EPIFMVisualizer(configs=oscillator, effects=physics) - create.output_frames(num_div=16) + # create image + create = EPIFMVisualizer(configs=oscillator, effects=physics) + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_oscillator(t0, t1) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_oscillator(t0, t1) diff --git a/bioimaging/nikon_repressilator.py b/bioimaging/nikon_repressilator.py index b459840..4cda8be 100755 --- a/bioimaging/nikon_repressilator.py +++ b/bioimaging/nikon_repressilator.py @@ -7,51 +7,49 @@ def test_repressilator(t0, t1) : - # create TIRFM imaging - repressilator = EPIFMConfigs() - repressilator.set_Shutter(start_time=t0, end_time=t1) - #repressilator.set_Shutter(start_time=t0, end_time=t1, time_open=0.150, time_lapse=20) - repressilator.set_LightSource(source_type='LASER', wave_length=532, flux_density=20, angle=72) - repressilator.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)', normalization=1.0) - repressilator.set_DichroicMirror('FF562-Di03-25x36') - #repressilator.set_Magnification(Mag=234) # EMCCD - repressilator.set_Magnification(Mag=95) # CMOS - -# # Detector : EMCCD Camera -# repressilator.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, exposure_time=0.050, \ -# focal_point=(0.0,0.5,0.5), QE=0.92, readout_noise=100, emgain=300) -# repressilator.set_ADConverter(bit=16, offset=2000, fullwell=800000) - # Detector : CMOS Camera - repressilator.set_Detector(detector='CMOS', image_size=(512,512), pixel_length=6.5e-6, \ - exposure_time=0.05, focal_point=(0.0,0.5,0.5), QE=0.73) - repressilator.set_ADConverter(bit=16, offset=100, fullwell=30000, fpn_type='column', fpn_count=10) - - # Output data - #repressilator.set_OutputData(image_file_dir='./data_for_nikon/numpys/repressilator02_emccd') - repressilator.set_OutputData(image_file_dir='./data_for_nikon/numpys/repressilator02_cmos') - - # Input data - csv_dir = '/home/masaki/wrk/spatiocyte/models/Nikon/data/repressilator00' - repressilator.set_InputFile(csv_dir, observable="X") - repressilator.set_ShapeFile(csv_dir) - - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0.01) - physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) - #physics.set_photobleaching(tau0=2.27, alpha=0.73) - - # create image - create = EPIFMVisualizer(configs=repressilator, effects=physics) - create.output_frames(num_div=16) + # create TIRFM imaging + repressilator = EPIFMConfigs() + repressilator.set_Shutter(start_time=t0, end_time=t1) + #repressilator.set_Shutter(start_time=t0, end_time=t1, time_open=0.150, time_lapse=20) + repressilator.set_LightSource(source_type='LASER', wave_length=532, flux_density=20, angle=72) + repressilator.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)', normalization=1.0) + repressilator.set_DichroicMirror('FF562-Di03-25x36') + #repressilator.set_Magnification(Mag=234) # EMCCD + repressilator.set_Magnification(Mag=95) # CMOS + +# # Detector : EMCCD Camera +# repressilator.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, exposure_time=0.050, \ +# focal_point=(0.0,0.5,0.5), QE=0.92, readout_noise=100, emgain=300) +# repressilator.set_ADConverter(bit=16, offset=2000, fullwell=800000) + # Detector : CMOS Camera + repressilator.set_Detector(detector='CMOS', image_size=(512,512), pixel_length=6.5e-6, \ + exposure_time=0.05, focal_point=(0.0,0.5,0.5), QE=0.73) + repressilator.set_ADConverter(bit=16, offset=100, fullwell=30000, fpn_type='column', fpn_count=10) + + # Output data + #repressilator.set_OutputData(image_file_dir='./data_for_nikon/numpys/repressilator02_emccd') + repressilator.set_OutputData(image_file_dir='./data_for_nikon/numpys/repressilator02_cmos') + + # Input data + csv_dir = '/home/masaki/wrk/spatiocyte/models/Nikon/data/repressilator00' + repressilator.set_InputFile(csv_dir, observable="X") + repressilator.set_ShapeFile(csv_dir) + + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0.01) + physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) + #physics.set_photobleaching(tau0=2.27, alpha=0.73) + + # create image + create = EPIFMVisualizer(configs=repressilator, effects=physics) + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_repressilator(t0, t1) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_repressilator(t0, t1) diff --git a/bioimaging/palm_configs.py b/bioimaging/palm_configs.py index 23ac088..a86d3c2 100755 --- a/bioimaging/palm_configs.py +++ b/bioimaging/palm_configs.py @@ -2,7 +2,7 @@ import numpy #----------------------------- -# General +# General #----------------------------- ignore_open_errors = False electron_charge = 1.602e-19 # C @@ -27,18 +27,18 @@ #fluorophore_rgb = numpy.array([(0, 0, 0) for j in range(len(depth))]) #----------------------------- -# Fluorophore PSF +# Fluorophore PSF #----------------------------- psf_wavelength = 600 # nm psf_intensity = 1.00 -psf_width = (200, 200) # Gaussian function (radial width, lateral width) [nm] -psf_cutoff = (400, 100) # cutoff range (radius, depth) -psf_file_name_format = 'psf_%04d.png' # Image file name +psf_width = (200, 200) # Gaussian function (radial width, lateral width) [nm] +psf_cutoff = (400, 100) # cutoff range (radius, depth) +psf_file_name_format = 'psf_%04d.png' # Image file name #----------------------------- # Excitation -#----------------------------- -source_excitation_switch = False +#----------------------------- +source_excitation_switch = False source_excitation_type = 'LASER' source_excitation_wavelength = 600. # nm source_excitation_flux = 20e-3 # W @@ -50,8 +50,8 @@ #----------------------------- # Activation -#----------------------------- -source_activation_switch = False +#----------------------------- +source_activation_switch = False source_activation_type = 'LASER' source_activation_wavelength = 600. # nm source_activation_flux = 20e-3 # W diff --git a/bioimaging/palm_handler.py b/bioimaging/palm_handler.py index a73262f..b27ff57 100755 --- a/bioimaging/palm_handler.py +++ b/bioimaging/palm_handler.py @@ -27,9 +27,9 @@ class PALMConfigs(TIRFMConfigs) : PALM configration - TIRFMiroscopy - + - Photo-bleaching/-blinking + TIRFMiroscopy + + + Photo-bleaching/-blinking ''' def __init__(self, user_configs_dict = None): @@ -58,7 +58,7 @@ def __init__(self, user_configs_dict = None): def set_ExcitationSource(self, source_type = None, - wave_length = None, + wave_length = None, flux_density = None, radius = None, angle = None ) : @@ -80,7 +80,7 @@ def set_ExcitationSource(self, source_type = None, def set_ActivationSource(self, source_type = None, - wave_length = None, + wave_length = None, flux_density = None, radius = None, angle = None, @@ -101,7 +101,7 @@ def set_ActivationSource(self, source_type = None, print('\tFlux Density = ', self.source_activation_flux_density, 'W/cm2') print('\t1/e2 Radius = ', self.source_activation_radius, 'm') print('\tAngle = ', self.source_activation_angle, 'degree') - print('\t') + print('\t') print('\tFrame time = ', self.source_activation_frame_time, 'sec') print('\tBleaching frames = ', self.source_activation_bleaching_frames) @@ -112,79 +112,79 @@ def set_Excitation_path(self) : #r = self.radial #d = self.depth - r = numpy.linspace(0, 20000, 20001) - d = numpy.linspace(0, 20000, 20001) + r = numpy.linspace(0, 20000, 20001) + d = numpy.linspace(0, 20000, 20001) # (plank const) * (speed of light) [joules meter] hc = self.hc_const - # Illumination : Assume that uniform illumination (No gaussian) - # flux density [W/cm2 (joules/sec/cm2)] + # Illumination : Assume that uniform illumination (No gaussian) + # flux density [W/cm2 (joules/sec/cm2)] P_0 = self.source_excitation_flux_density*1e+4 # single photon energy wave_length = self.source_excitation_wavelength*1e-9 E_wl = hc/wave_length - # photon flux density [photons/sec/cm2] + # photon flux density [photons/sec/cm2] N_0 = P_0/E_wl - ################################################################ - # Evanescent field - # - # Assume uniform beam profile (Not Gaussian) - # Linear polarization - # + ################################################################ + # Evanescent field + # + # Assume uniform beam profile (Not Gaussian) + # Linear polarization + # ################################################################ - # Incident beam : Angle - theta_in = (self.source_excitation_angle/180.)*numpy.pi + # Incident beam : Angle + theta_in = (self.source_excitation_angle/180.)*numpy.pi - sin = numpy.sin(theta_in) - cos = numpy.cos(theta_in) - sin2 = sin**2 - cos2 = cos**2 + sin = numpy.sin(theta_in) + cos = numpy.cos(theta_in) + sin2 = sin**2 + cos2 = cos**2 - # index refraction - n_1 = 1.46 # fused silica - n_2 = 1.33 # water (objective : water immersion) - n = n_2/n_1 # must be < 1 - n2 = n**2 + # index refraction + n_1 = 1.46 # fused silica + n_2 = 1.33 # water (objective : water immersion) + n = n_2/n_1 # must be < 1 + n2 = n**2 - # Incident beam : Amplitude - #theta = numpy.pi/2.0 - A2_Is = N_0#*numpy.cos(theta)**2 - A2_Ip = N_0#*numpy.sin(theta)**2 + # Incident beam : Amplitude + #theta = numpy.pi/2.0 + A2_Is = N_0#*numpy.cos(theta)**2 + A2_Ip = N_0#*numpy.sin(theta)**2 - if (sin2/n2 > 1) : - # Evanescent field : Amplitude and Depth - # Assume that the s-polar direction is parallel to y-axis - A2_x = A2_Ip*(4*cos2*(sin2 - n2)/(n2**2*cos2 + sin2 - n2)) - A2_y = A2_Is*(4*cos2/(1 - n2)) - A2_z = A2_Ip*(4*cos2*sin2/(n2**2*cos2 + sin2 - n2)) + if (sin2/n2 > 1) : + # Evanescent field : Amplitude and Depth + # Assume that the s-polar direction is parallel to y-axis + A2_x = A2_Ip*(4*cos2*(sin2 - n2)/(n2**2*cos2 + sin2 - n2)) + A2_y = A2_Is*(4*cos2/(1 - n2)) + A2_z = A2_Ip*(4*cos2*sin2/(n2**2*cos2 + sin2 - n2)) - A2_Tp = A2_x + A2_z - A2_Ts = A2_y + A2_Tp = A2_x + A2_z + A2_Ts = A2_y - depth = wave_length/(4.0*numpy.pi*numpy.sqrt(n_1**2*sin2 - n_2**2)) + depth = wave_length/(4.0*numpy.pi*numpy.sqrt(n_1**2*sin2 - n_2**2)) - else : - # Epi-fluorescence field : Amplitude and Depth - cosT = numpy.sqrt(1 - sin2/n2) + else : + # Epi-fluorescence field : Amplitude and Depth + cosT = numpy.sqrt(1 - sin2/n2) - A2_Tp = A2_Ip*(2*cos/(cosT + n*cos))**2 - A2_Ts = A2_Is*(2*cos/(n*cosT + cos))**2 + A2_Tp = A2_Ip*(2*cos/(cosT + n*cos))**2 + A2_Ts = A2_Is*(2*cos/(n*cosT + cos))**2 - depth = float('inf') + depth = float('inf') - I_d = numpy.exp(-d*1e-9/depth) - I_r = numpy.array(map(lambda x : A2_Tp+A2_Ts, r*1e-9)) + I_d = numpy.exp(-d*1e-9/depth) + I_r = numpy.array(map(lambda x : A2_Tp+A2_Ts, r*1e-9)) - # photon flux density [photon/(sec m^2)] + # photon flux density [photon/(sec m^2)] self.source_excitation_flux_density = numpy.array(map(lambda x : I_r*x, I_d)) - print('Penetration depth :', depth, 'm') - print('Photon Flux Density (Max) :', self.source_excitation_flux_density[0][0], '#photon/(sec m^2)') + print('Penetration depth :', depth, 'm') + print('Photon Flux Density (Max) :', self.source_excitation_flux_density[0][0], '#photon/(sec m^2)') @@ -192,149 +192,149 @@ def set_Activation_path(self) : #r = self.radial #d = self.depth - r = numpy.linspace(0, 20000, 20001) - d = numpy.linspace(0, 20000, 20001) + r = numpy.linspace(0, 20000, 20001) + d = numpy.linspace(0, 20000, 20001) # (plank const) * (speed of light) [joules meter] hc = self.hc_const - # Illumination : Assume that uniform illumination (No gaussian) - # flux density [W/cm2 (joules/sec/cm2)] + # Illumination : Assume that uniform illumination (No gaussian) + # flux density [W/cm2 (joules/sec/cm2)] P_0 = self.source_activation_flux_density*1e+4 # single photon energy wave_length = self.source_activation_wavelength*1e-9 E_wl = hc/wave_length - # photon flux density [photons/sec/cm2] + # photon flux density [photons/sec/cm2] N_0 = P_0/E_wl - ################################################################ - # Evanescent field - # - # Assume uniform beam profile (Not Gaussian) - # Linear polarization - # + ################################################################ + # Evanescent field + # + # Assume uniform beam profile (Not Gaussian) + # Linear polarization + # ################################################################ - # Incident beam : Angle - theta_in = (self.source_activation_angle/180.)*numpy.pi + # Incident beam : Angle + theta_in = (self.source_activation_angle/180.)*numpy.pi - sin = numpy.sin(theta_in) - cos = numpy.cos(theta_in) - sin2 = sin**2 - cos2 = cos**2 + sin = numpy.sin(theta_in) + cos = numpy.cos(theta_in) + sin2 = sin**2 + cos2 = cos**2 - # index refraction - n_1 = 1.46 # fused silica - n_2 = 1.33 # water (objective : water immersion) - n = n_2/n_1 # must be < 1 - n2 = n**2 + # index refraction + n_1 = 1.46 # fused silica + n_2 = 1.33 # water (objective : water immersion) + n = n_2/n_1 # must be < 1 + n2 = n**2 - # Incident beam : Amplitude - #theta = numpy.pi/2.0 - A2_Is = N_0#*numpy.cos(theta)**2 - A2_Ip = N_0#*numpy.sin(theta)**2 + # Incident beam : Amplitude + #theta = numpy.pi/2.0 + A2_Is = N_0#*numpy.cos(theta)**2 + A2_Ip = N_0#*numpy.sin(theta)**2 - if (sin2/n2 > 1) : - # Evanescent field : Amplitude and Depth - # Assume that the s-polar direction is parallel to y-axis - A2_x = A2_Ip*(4*cos2*(sin2 - n2)/(n2**2*cos2 + sin2 - n2)) - A2_y = A2_Is*(4*cos2/(1 - n2)) - A2_z = A2_Ip*(4*cos2*sin2/(n2**2*cos2 + sin2 - n2)) + if (sin2/n2 > 1) : + # Evanescent field : Amplitude and Depth + # Assume that the s-polar direction is parallel to y-axis + A2_x = A2_Ip*(4*cos2*(sin2 - n2)/(n2**2*cos2 + sin2 - n2)) + A2_y = A2_Is*(4*cos2/(1 - n2)) + A2_z = A2_Ip*(4*cos2*sin2/(n2**2*cos2 + sin2 - n2)) - A2_Tp = A2_x + A2_z - A2_Ts = A2_y + A2_Tp = A2_x + A2_z + A2_Ts = A2_y - depth = wave_length/(4.0*numpy.pi*numpy.sqrt(n_1**2*sin2 - n_2**2)) + depth = wave_length/(4.0*numpy.pi*numpy.sqrt(n_1**2*sin2 - n_2**2)) - else : - # Epi-fluorescence field : Amplitude and Depth - cosT = numpy.sqrt(1 - sin2/n2) + else : + # Epi-fluorescence field : Amplitude and Depth + cosT = numpy.sqrt(1 - sin2/n2) - A2_Tp = A2_Ip*(2*cos/(cosT + n*cos))**2 - A2_Ts = A2_Is*(2*cos/(n*cosT + cos))**2 + A2_Tp = A2_Ip*(2*cos/(cosT + n*cos))**2 + A2_Ts = A2_Is*(2*cos/(n*cosT + cos))**2 - depth = float('inf') + depth = float('inf') - I_d = numpy.exp(-d*1e-9/depth) - I_r = numpy.array(map(lambda x : A2_Tp+A2_Ts, r*1e-9)) + I_d = numpy.exp(-d*1e-9/depth) + I_r = numpy.array(map(lambda x : A2_Tp+A2_Ts, r*1e-9)) - # photon flux density [photon/(sec m^2)] + # photon flux density [photon/(sec m^2)] self.source_activation_flux_density = numpy.array(map(lambda x : I_r*x, I_d)) - print('Penetration depth :', depth, 'm') - print('Photon Flux Density (Max) :', self.source_activation_flux_density[0][0], '#photon/(sec m^2)') + print('Penetration depth :', depth, 'm') + print('Photon Flux Density (Max) :', self.source_activation_flux_density[0][0], '#photon/(sec m^2)') def set_Optical_path(self) : - # (1) Illumination path : Light source --> Sample - self.set_Excitation_path() - self.set_Activation_path() + # (1) Illumination path : Light source --> Sample + self.set_Excitation_path() + self.set_Activation_path() - # (2) Detection path : Sample --> Detector - self.set_Detection_path() + # (2) Detection path : Sample --> Detector + self.set_Detection_path() def reset_InputData(self, csv_file_directry, start=0, end=None, observable=None) : - print('--- Input Spatiocyte Data : ', csv_file_directry) + print('--- Input Spatiocyte Data : ', csv_file_directry) - ### header - f = open(csv_file_directry + '/pt-input.csv', 'r') + ### header + f = open(csv_file_directry + '/pt-input.csv', 'r') header = f.readline().rstrip().split(',') header[:5] = [float(_) for _ in header[:5]] f.close() interval, lengths, voxel_r, species_info = header[0], (header[3:0:-1]), header[4], header[5:] - species_id = range(len(species_info)) - species_index = [_.split(':')[1].split(']')[0] for _ in species_info] - species_radius = [float(_.split('=')[1]) for _ in species_info] + species_id = range(len(species_info)) + species_index = [_.split(':')[1].split(']')[0] for _ in species_info] + species_radius = [float(_.split('=')[1]) for _ in species_info] -# ##### -# count_start = int(round(start/interval)) -# count_end = int(round(end/interval)) +# ##### +# count_start = int(round(start/interval)) +# count_end = int(round(end/interval)) +# +# data = [] # -# data = [] +# # read lattice file +# for count in range(count_start, count_end, 1) : # -# # read lattice file -# for count in range(count_start, count_end, 1) : +# csv_file_path = csv_file_directry + '/pt-%09d.0.csv' % (count) # -# csv_file_path = csv_file_directry + '/pt-%09d.0.csv' % (count) -# # try : # # csv_file = open(csv_file_path, 'r') # -# dataset = [] +# dataset = [] # -# for row in csv.reader(csv_file) : -# dataset.append(row) +# for row in csv.reader(csv_file) : +# dataset.append(row) # # ### particle data -# time = float(dataset[0][0]) +# time = float(dataset[0][0]) # -# particles = [] +# particles = [] # -# from ast import literal_eval +# from ast import literal_eval # -# for data_id in dataset : -# c_id = (float(data_id[1]), float(data_id[2]), float(data_id[3])) -# s_id, l_id = literal_eval(data_id[6]) -# particles.append((c_id, s_id, l_id)) -# -# data.append([time, particles]) +# for data_id in dataset : +# c_id = (float(data_id[1]), float(data_id[2]), float(data_id[3])) +# s_id, l_id = literal_eval(data_id[6]) +# particles.append((c_id, s_id, l_id)) +# +# data.append([time, particles]) # # # except Exception : # print 'Error : ', csv_file_path, ' not found' -# exit() +# exit() # -# data.sort(lambda x, y:cmp(x[0], y[0])) +# data.sort(lambda x, y:cmp(x[0], y[0])) # get filename self._set_data('spatiocyte_file_directry', csv_file_directry) @@ -344,14 +344,14 @@ def reset_InputData(self, csv_file_directry, start=0, end=None, observable=None) self._set_data('spatiocyte_end_time', end) self._set_data('spatiocyte_interval', interval) - # get data + # get data #self._set_data('spatiocyte_data', data) # get species properties self._set_data('spatiocyte_species_id', species_id) self._set_data('spatiocyte_index', species_index) - #self._set_data('spatiocyte_diffusion', species_diffusion) - self._set_data('spatiocyte_radius', species_radius) + #self._set_data('spatiocyte_diffusion', species_diffusion) + self._set_data('spatiocyte_radius', species_radius) # get lattice properties #self._set_data('spatiocyte_lattice_id', map(lambda x : x[0], lattice)) @@ -359,474 +359,471 @@ def reset_InputData(self, csv_file_directry, start=0, end=None, observable=None) self._set_data('spatiocyte_VoxelRadius', voxel_r) self._set_data('spatiocyte_theNormalizedVoxelRadius', 0.5) - + # set observable if observable is None : index = [True for i in range(len(self.spatiocyte_index))] - else : + else : index = map(lambda x : True if x.find(observable) > -1 else False, self.spatiocyte_index) - #index = [False, True] + #index = [False, True] self.spatiocyte_observables = copy.copy(index) print('\tStart time =', self.spatiocyte_start_time, 'sec') print('\tEnd time =', self.spatiocyte_end_time, 'sec') print('\tInterval =', self.spatiocyte_interval, 'sec') - print('\tVoxel radius =', self.spatiocyte_VoxelRadius, 'm') + print('\tVoxel radius =', self.spatiocyte_VoxelRadius, 'm') print('\tCompartment lengths :', self.spatiocyte_lengths, 'voxels') print('\tSpecies Index :', self.spatiocyte_index) print('\tObservable :', self.spatiocyte_observables) - # Visualization error - if self.spatiocyte_species_id is None: - raise VisualizerError('Cannot find species_id in any given csv files') + # Visualization error + if self.spatiocyte_species_id is None: + raise VisualizerError('Cannot find species_id in any given csv files') -# if len(self.spatiocyte_data) == 0: -# raise VisualizerError('Cannot find spatiocyte_data in any given csv files: ' \ -# + ', '.join(csv_file_directry)) +# if len(self.spatiocyte_data) == 0: +# raise VisualizerError('Cannot find spatiocyte_data in any given csv files: ' \ +# + ', '.join(csv_file_directry)) - if len(self.spatiocyte_index) == 0 : - raise VisualizerError('Cannot find spatiocyte_index in any given csv files: ' \ - + ', '.join(csv_file_directry)) + if len(self.spatiocyte_index) == 0 : + raise VisualizerError('Cannot find spatiocyte_index in any given csv files: ' \ + + ', '.join(csv_file_directry)) def set_InputData(self, csv_file_directry, start=0, end=None, observable=None) : - print('--- Input Spatiocyte Data : ', csv_file_directry) + print('--- Input Spatiocyte Data : ', csv_file_directry) - ### header - f = open(csv_file_directry + '/pt-input.csv', 'r') + ### header + f = open(csv_file_directry + '/pt-input.csv', 'r') header = f.readline().rstrip().split(',') header[:5] = [float(_) for _ in header[:5]] f.close() interval, lengths, voxel_r, species_info = header[0], (header[3:0:-1]), header[4], header[5:] - species_id = range(len(species_info)) - species_index = [_.split(':')[1].split(']')[0] for _ in species_info] - species_radius = [float(_.split('=')[1]) for _ in species_info] + species_id = range(len(species_info)) + species_index = [_.split(':')[1].split(']')[0] for _ in species_info] + species_radius = [float(_.split('=')[1]) for _ in species_info] - ##### - count_start = int(round(start/interval)) - count_end = int(round(end/interval)) + ##### + count_start = int(round(start/interval)) + count_end = int(round(end/interval)) - data = [] + data = [] - # read lattice file - for count in range(count_start, count_end, 1) : + # read lattice file + for count in range(count_start, count_end, 1) : + + csv_file_path = csv_file_directry + '/pt-%09d.0.csv' % (count) - csv_file_path = csv_file_directry + '/pt-%09d.0.csv' % (count) - try : csv_file = open(csv_file_path, 'r') - dataset = [] + dataset = [] - for row in csv.reader(csv_file) : - dataset.append(row) + for row in csv.reader(csv_file) : + dataset.append(row) ### particle data - time = float(dataset[0][0]) + time = float(dataset[0][0]) - particles = [] + particles = [] - from ast import literal_eval + from ast import literal_eval - for data_id in dataset : - c_id = (float(data_id[1]), float(data_id[2]), float(data_id[3])) - s_id, l_id = literal_eval(data_id[6]) + for data_id in dataset : + c_id = (float(data_id[1]), float(data_id[2]), float(data_id[3])) + s_id, l_id = literal_eval(data_id[6]) - try : - p_state, cyc_id = float(data_id[7]), float(data_id[8]) - except Exception : - p_state, cyc_id = 1.0, float('inf') + try : + p_state, cyc_id = float(data_id[7]), float(data_id[8]) + except Exception : + p_state, cyc_id = 1.0, float('inf') - particles.append((c_id, s_id, l_id, p_state, cyc_id)) + particles.append((c_id, s_id, l_id, p_state, cyc_id)) - data.append([time, particles]) + data.append([time, particles]) except Exception : print('Error : ', csv_file_path, ' not found') - exit() + exit() - data.sort(lambda x, y:cmp(x[0], y[0])) + data.sort(lambda x, y:cmp(x[0], y[0])) # get run time self._set_data('spatiocyte_start_time', start) self._set_data('spatiocyte_end_time', end) self._set_data('spatiocyte_interval', interval) - # get data + # get data self._set_data('spatiocyte_data', data) # get species properties self._set_data('spatiocyte_species_id', species_id) self._set_data('spatiocyte_index', species_index) - #self._set_data('spatiocyte_diffusion', species_diffusion) - self._set_data('spatiocyte_radius', species_radius) + #self._set_data('spatiocyte_diffusion', species_diffusion) + self._set_data('spatiocyte_radius', species_radius) # get lattice properties #self._set_data('spatiocyte_lattice_id', map(lambda x : x[0], lattice)) self._set_data('spatiocyte_lengths', lengths) self._set_data('spatiocyte_VoxelRadius', voxel_r) self._set_data('spatiocyte_theNormalizedVoxelRadius', 0.5) - + # set observable if observable is None : index = [True for i in range(len(self.spatiocyte_index))] - else : + else : index = map(lambda x : True if x.find(observable) > -1 else False, self.spatiocyte_index) - #index = [False, True] + #index = [False, True] self.spatiocyte_observables = copy.copy(index) print('\tStart time =', self.spatiocyte_start_time, 'sec') print('\tEnd time =', self.spatiocyte_end_time, 'sec') print('\tInterval =', self.spatiocyte_interval, 'sec') - print('\tVoxel radius =', self.spatiocyte_VoxelRadius, 'm') + print('\tVoxel radius =', self.spatiocyte_VoxelRadius, 'm') print('\tCompartment lengths :', self.spatiocyte_lengths, 'voxels') print('\tSpecies Index :', self.spatiocyte_index) print('\tObservable :', self.spatiocyte_observables) - # Visualization error - if self.spatiocyte_species_id is None: - raise VisualizerError('Cannot find species_id in any given csv files') + # Visualization error + if self.spatiocyte_species_id is None: + raise VisualizerError('Cannot find species_id in any given csv files') - if len(self.spatiocyte_data) == 0: - raise VisualizerError('Cannot find spatiocyte_data in any given csv files: ' \ - + ', '.join(csv_file_directry)) + if len(self.spatiocyte_data) == 0: + raise VisualizerError('Cannot find spatiocyte_data in any given csv files: ' \ + + ', '.join(csv_file_directry)) - if len(self.spatiocyte_index) == 0 : - raise VisualizerError('Cannot find spatiocyte_index in any given csv files: ' \ - + ', '.join(csv_file_directry)) + if len(self.spatiocyte_index) == 0 : + raise VisualizerError('Cannot find spatiocyte_index in any given csv files: ' \ + + ', '.join(csv_file_directry)) class PALMVisualizer(TIRFMVisualizer) : - ''' - PALM visualization class - ''' + ''' + PALM visualization class + ''' + + def __init__(self, configs=PALMConfigs(), effects=PhysicalEffects()) : - def __init__(self, configs=PALMConfigs(), effects=PhysicalEffects()) : + assert isinstance(configs, PALMConfigs) + self.configs = configs - assert isinstance(configs, PALMConfigs) - self.configs = configs + assert isinstance(effects, PhysicalEffects) + self.effects = effects - assert isinstance(effects, PhysicalEffects) - self.effects = effects + """ + Check and create the folder for image file. + """ + if not os.path.exists(self.configs.image_file_dir): + os.makedirs(self.configs.image_file_dir) + #else: + # for file in os.listdir(self.configs.movie_image_file_dir): + # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) - """ - Check and create the folder for image file. - """ - if not os.path.exists(self.configs.image_file_dir): - os.makedirs(self.configs.image_file_dir) - #else: - # for file in os.listdir(self.configs.movie_image_file_dir): - # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) + """ + set optical path from source to detector + """ + self.configs.set_Optical_path() - """ - set optical path from source to detector - """ - self.configs.set_Optical_path() + def rewrite_InputData(self, output_file_dir=None) : - def rewrite_InputData(self, output_file_dir=None) : + if not os.path.exists(output_file_dir): + os.makedirs(output_file_dir) - if not os.path.exists(output_file_dir): - os.makedirs(output_file_dir) + # define observational image plane in nm-scale + voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 - # define observational image plane in nm-scale - voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 + ## cell size (nm scale) + Nz = int(self.configs.spatiocyte_lengths[2]*voxel_size) + Ny = int(self.configs.spatiocyte_lengths[1]*voxel_size) + Nx = int(self.configs.spatiocyte_lengths[0]*voxel_size) - ## cell size (nm scale) - Nz = int(self.configs.spatiocyte_lengths[2]*voxel_size) - Ny = int(self.configs.spatiocyte_lengths[1]*voxel_size) - Nx = int(self.configs.spatiocyte_lengths[0]*voxel_size) + # focal point + p_0 = numpy.array([Nx, Ny, Nz])*self.configs.detector_focal_point - # focal point - p_0 = numpy.array([Nx, Ny, Nz])*self.configs.detector_focal_point + # beam position : Assuming beam position = focal point for temporary + x_b, y_b, z_b = copy.copy(p_0) - # beam position : Assuming beam position = focal point for temporary - x_b, y_b, z_b = copy.copy(p_0) + # Active states per detection time + start = self.configs.spatiocyte_start_time + end = self.configs.spatiocyte_end_time + dt = self.configs.spatiocyte_interval - # Active states per detection time - start = self.configs.spatiocyte_start_time - end = self.configs.spatiocyte_end_time - dt = self.configs.spatiocyte_interval + tau_frame = self.configs.source_activation_frame_time + F_bleach = self.configs.source_activation_bleaching_frames - tau_frame = self.configs.source_activation_frame_time - F_bleach = self.configs.source_activation_bleaching_frames + # Abogadoro's number + Na = self.effects.avogadoros_number - # Abogadoro's number - Na = self.effects.avogadoros_number + # spatiocyte size + radius = self.configs.spatiocyte_VoxelRadius + volume = 4.0/3.0*numpy.pi*radius**3 + depth = 2.0*radius - # spatiocyte size - radius = self.configs.spatiocyte_VoxelRadius - volume = 4.0/3.0*numpy.pi*radius**3 - depth = 2.0*radius + # Quantum yield + QY = self.effects.quantum_yield - # Quantum yield - QY = self.effects.quantum_yield + # Abs coefficient [1/(cm M)] + abs_coeff = self.effects.abs_coefficient - # Abs coefficient [1/(cm M)] - abs_coeff = self.effects.abs_coefficient + # photon flux density [#photons/(sec m2)] + #n0 = self.configs.source_excitation_flux_density[0][0] + n0 = self.configs.source_excitation_flux_density - # photon flux density [#photons/(sec m2)] - #n0 = self.configs.source_excitation_flux_density[0][0] - n0 = self.configs.source_excitation_flux_density + # Cross-Section [m2] + xsec = numpy.log(10)*(0.1*abs_coeff/Na) - # Cross-Section [m2] - xsec = numpy.log(10)*(0.1*abs_coeff/Na) + # the number of absorbed photons + N_abs = xsec*n0*dt - # the number of absorbed photons - N_abs = xsec*n0*dt - - # Beer-Lambert law : A = log(I0/I) = coef * concentration * path-length - A = (abs_coeff*0.1/Na)*(1/volume)*depth - - # the number of emitted photons - N_emit0 = QY*N_abs*(1 - 10**(-A)) + # Beer-Lambert law : A = log(I0/I) = coef * concentration * path-length + A = (abs_coeff*0.1/Na)*(1/volume)*depth - # sequence - start_count = int(start/dt) - end_count = int(end/dt) + # the number of emitted photons + N_emit0 = QY*N_abs*(1 - 10**(-A)) - # copy input file - csv_input = self.configs.spatiocyte_file_directry + '/pt-input.csv' - shutil.copyfile(csv_input, output_file_dir + '/pt-input.csv') + # sequence + start_count = int(start/dt) + end_count = int(end/dt) - # read input file - csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (0) - csv_list = list(csv.reader(open(csv_file_path, 'r'))) - #csv_temp = list(csv.reader(open(csv_file_path, 'r'))) - #csv_list = [csv_temp[0]] + # copy input file + csv_input = self.configs.spatiocyte_file_directry + '/pt-input.csv' + shutil.copyfile(csv_input, output_file_dir + '/pt-input.csv') - # fluorescence - if (self.effects.photobleaching_switch == True) : + # read input file + csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (0) + csv_list = list(csv.reader(open(csv_file_path, 'r'))) + #csv_temp = list(csv.reader(open(csv_file_path, 'r'))) + #csv_list = [csv_temp[0]] - # activation frames - tau_frame = self.configs.source_activation_frame_time - f = int(tau_frame/dt) + # fluorescence + if (self.effects.photobleaching_switch == True) : - # bleaching frames - F_bleach = self.configs.source_activation_bleaching_frames - exposure_time = self.configs.detector_exposure_time - F = int((F_bleach*exposure_time)/dt) + # activation frames + tau_frame = self.configs.source_activation_frame_time + f = int(tau_frame/dt) - # get the number of particles - N_particles = len(csv_list) + # bleaching frames + F_bleach = self.configs.source_activation_bleaching_frames + exposure_time = self.configs.detector_exposure_time + F = int((F_bleach*exposure_time)/dt) - # set fluorescence - self.effects.set_photophysics_4palm(start, end, dt, f, F, N_particles) + # get the number of particles + N_particles = len(csv_list) - bleach = self.effects.fluorescence_bleach - budget = self.effects.fluorescence_budget*N_emit0[0][0] - state = self.effects.fluorescence_state + # set fluorescence + self.effects.set_photophysics_4palm(start, end, dt, f, F, N_particles) + bleach = self.effects.fluorescence_bleach + budget = self.effects.fluorescence_budget*N_emit0[0][0] + state = self.effects.fluorescence_state - for count in range(start_count, end_count, 1) : - # read input file - csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (count) + for count in range(start_count, end_count, 1) : - csv_list = list(csv.reader(open(csv_file_path, 'r'))) - #csv_temp = list(csv.reader(open(csv_file_path, 'r'))) - #csv_list = [csv_temp[0]] - dataset = numpy.array(csv_list) + # read input file + csv_file_path = self.configs.spatiocyte_file_directry + '/pt-%09d.0.csv' % (count) - time = float(dataset[count][0]) + csv_list = list(csv.reader(open(csv_file_path, 'r'))) + #csv_temp = list(csv.reader(open(csv_file_path, 'r'))) + #csv_list = [csv_temp[0]] + dataset = numpy.array(csv_list) - # loop for particles - for j, data_j in enumerate(dataset): - # set particle position - c_id = (float(data_j[1]), float(data_j[2]), float(data_j[3])) - x_i, y_i, z_i = self.get_coordinate(c_id) + time = float(dataset[count][0]) - r = self.configs.radial - d = self.configs.depth + # loop for particles + for j, data_j in enumerate(dataset): + # set particle position + c_id = (float(data_j[1]), float(data_j[2]), float(data_j[3])) + x_i, y_i, z_i = self.get_coordinate(c_id) - # beam axial position - """ Distance between beam and fluorephore (depth) """ - d_s = abs(x_i - x_b) + r = self.configs.radial + d = self.configs.depth - if (d_s < 20000) : - source_depth = d_s - else : - source_depth = 19999 + # beam axial position + """ Distance between beam and fluorephore (depth) """ + d_s = abs(x_i - x_b) - # beam position - """ Distance between beam position and fluorophore (plane) """ - rr = numpy.sqrt((y_i-y_b)**2 + (z_i-z_b)**2) + if (d_s < 20000) : + source_depth = d_s + else : + source_depth = 19999 - if (rr < 20000) : - source_radius = rr - else : - source_radius = 19999 + # beam position + """ Distance between beam position and fluorophore (plane) """ + rr = numpy.sqrt((y_i-y_b)**2 + (z_i-z_b)**2) + + if (rr < 20000) : + source_radius = rr + else : + source_radius = 19999 - N_emit = N_emit0[int(source_depth)][int(source_radius)]*state[j,count-start_count] - photons = budget[j] - N_emit + N_emit = N_emit0[int(source_depth)][int(source_radius)]*state[j,count-start_count] + photons = budget[j] - N_emit - check = (photons > 0).astype('int') - budget[j] = numpy.abs(photons*check) + check = (photons > 0).astype('int') + budget[j] = numpy.abs(photons*check) - state_stack = numpy.column_stack((state[:,count-start_count], (budget/N_emit0[0][0]).astype('int'))) - new_dataset = numpy.column_stack((dataset, state_stack)) + state_stack = numpy.column_stack((state[:,count-start_count], (budget/N_emit0[0][0]).astype('int'))) + new_dataset = numpy.column_stack((dataset, state_stack)) - #print float(new_dataset[0][0]), N_emit[0], budget[0], state[0,count], int(budget[0]/N_emit0[0][0]) + #print float(new_dataset[0][0]), N_emit[0], budget[0], state[0,count], int(budget[0]/N_emit0[0][0]) - # write output file - output_file = output_file_dir + '/pt-%09d.0.csv' % (count) + # write output file + output_file = output_file_dir + '/pt-%09d.0.csv' % (count) - with open(output_file, 'w') as f : - writer = csv.writer(f) - writer.writerows(new_dataset) + with open(output_file, 'w') as f : + writer = csv.writer(f) + writer.writerows(new_dataset) -# for i, dataset in enumerate(csv.reader(open(csv_file_path, 'r'))) : +# for i, dataset in enumerate(csv.reader(open(csv_file_path, 'r'))) : # -# time = float(dataset[0]) +# time = float(dataset[0]) # -# # get current index of fluorescence-state array -# k = (numpy.abs(time_act[i] - time)).argmin() +# # get current index of fluorescence-state array +# k = (numpy.abs(time_act[i] - time)).argmin() # -# # Photo-activation (ON-state) -# if (state_act[i][k] == 1) : -# N_emit = N_emit0 -# -# # Photo-activation (OFF-state) -# else : N_emit = 0 +# # Photo-activation (ON-state) +# if (state_act[i][k] == 1) : +# N_emit = N_emit0 # -# if (budget[i] > 0) : -# budget[i] = budget[i] - N_emit -# else : -# Budget = 0 -# N_emit = 0 +# # Photo-activation (OFF-state) +# else : N_emit = 0 # -# state = (int(N_emit/N_emit0),int(budget[i]/N_emit0)) +# if (budget[i] > 0) : +# budget[i] = budget[i] - N_emit +# else : +# Budget = 0 +# N_emit = 0 # -# dataset.append(state) +# state = (int(N_emit/N_emit0),int(budget[i]/N_emit0)) # -# with open(output_file, 'a') as f : -# writer = csv.writer(f) -# writer.writerow(dataset) +# dataset.append(state) +# +# with open(output_file, 'a') as f : +# writer = csv.writer(f) +# writer.writerow(dataset) - def get_molecule_plane(self, cell, time, data, pid, p_b, p_0) : + def get_molecule_plane(self, cell, time, data, pid, p_b, p_0) : - voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 - - # particles coordinate, species and lattice IDs - c_id, s_id, l_id, p_state, cyc_id = data - - sid_array = numpy.array(self.configs.spatiocyte_species_id) - s_index = (numpy.abs(sid_array - int(s_id))).argmin() + voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 - if self.configs.spatiocyte_observables[s_index] is True : + # particles coordinate, species and lattice IDs + c_id, s_id, l_id, p_state, cyc_id = data - # particles coordinate in real(nm) scale - #pos = self.get_coordinate(c_id) - #p_i = numpy.array(pos)*voxel_size - p_i = self.get_coordinate(c_id) + sid_array = numpy.array(self.configs.spatiocyte_species_id) + s_index = (numpy.abs(sid_array - int(s_id))).argmin() - # get signal matrix - signal = self.get_signal(time, pid, s_index, p_i, p_b, p_0, p_state) + if self.configs.spatiocyte_observables[s_index] is True : - # add signal matrix to image plane - self.overwrite_signal(cell, signal, p_i) + # particles coordinate in real(nm) scale + #pos = self.get_coordinate(c_id) + #p_i = numpy.array(pos)*voxel_size + p_i = self.get_coordinate(c_id) + # get signal matrix + signal = self.get_signal(time, pid, s_index, p_i, p_b, p_0, p_state) + # add signal matrix to image plane + self.overwrite_signal(cell, signal, p_i) - def get_signal(self, time, pid, s_index, p_i, p_b, p_0, p_state) : - # set focal point - x_0, y_0, z_0 = p_0 - # set source center - x_b, y_b, z_b = p_b + def get_signal(self, time, pid, s_index, p_i, p_b, p_0, p_state) : - # set particle position - x_i, y_i, z_i = p_i + # set focal point + x_0, y_0, z_0 = p_0 - # - r = self.configs.radial - d = self.configs.depth + # set source center + x_b, y_b, z_b = p_b - # beam axial position - """ Distance between beam and fluorefore (depth) """ - d_s = abs(x_i - x_b) + # set particle position + x_i, y_i, z_i = p_i - if (d_s < 20000) : - source_depth = d_s - else : - source_depth = 19999 - - # beam position - """ Distance between beam position and fluorophore (plane) """ - rr = numpy.sqrt((y_i-y_b)**2 + (z_i-z_b)**2) + # + r = self.configs.radial + d = self.configs.depth - if (rr < 20000) : - source_radius = rr - else : - source_radius = 19999 + # beam axial position + """ Distance between beam and fluorefore (depth) """ + d_s = abs(x_i - x_b) - # get illumination PSF - source_psf = self.configs.source_excitation_flux_density[int(source_depth)][int(source_radius)] + if (d_s < 20000) : + source_depth = d_s + else : + source_depth = 19999 - # fluorophore axial position - d_f = abs(x_i - x_b) + # beam position + """ Distance between beam position and fluorophore (plane) """ + rr = numpy.sqrt((y_i-y_b)**2 + (z_i-z_b)**2) - if (d_f < len(d)) : - fluo_depth = d_f - else : - fluo_depth = d[-1] + if (rr < 20000) : + source_radius = rr + else : + source_radius = 19999 - # get fluorophore PSF - fluo_psf = self.fluo_psf[int(fluo_depth)] + # get illumination PSF + source_psf = self.configs.source_excitation_flux_density[int(source_depth)][int(source_radius)] - # Spatiocyte time interval [sec] - unit_time = self.configs.spatiocyte_interval + # fluorophore axial position + d_f = abs(x_i - x_b) - # Absorption coeff [1/(cm M)] - abs_coeff = self.effects.abs_coefficient + if (d_f < len(d)) : + fluo_depth = d_f + else : + fluo_depth = d[-1] - # Quantum yield - QY = self.effects.quantum_yield + # get fluorophore PSF + fluo_psf = self.fluo_psf[int(fluo_depth)] - # Abogadoro's number - Na = self.effects.avogadoros_number + # Spatiocyte time interval [sec] + unit_time = self.configs.spatiocyte_interval - # Cross-section [m2] - x_sec = numpy.log(10)*abs_coeff*0.1/Na + # Absorption coeff [1/(cm M)] + abs_coeff = self.effects.abs_coefficient - # get the number of absorption photons : [#/(m2 sec)] [m2] [sec] - n_abs = source_psf*x_sec*unit_time + # Quantum yield + QY = self.effects.quantum_yield - # spatiocyte size - voxel_radius = self.configs.spatiocyte_VoxelRadius - voxel_volume = (4.0/3.0)*numpy.pi*voxel_radius**3 - voxel_depth = 2.0*voxel_radius + # Abogadoro's number + Na = self.effects.avogadoros_number - # Beer-Lamberts law : log(I0/I) = A = abs coef. * concentration * path length ([m2] * [#/m3] * [m]) - A = (abs_coeff*0.1/Na)*(1.0/voxel_volume)*(voxel_depth) + # Cross-section [m2] + x_sec = numpy.log(10)*abs_coeff*0.1/Na - # get the number of photons emitted - N_emit = p_state*QY*n_abs*(1 - 10**(-A)) + # get the number of absorption photons : [#/(m2 sec)] [m2] [sec] + n_abs = source_psf*x_sec*unit_time - # get signal - signal = N_emit/(4.0*numpy.pi) * fluo_psf + # spatiocyte size + voxel_radius = self.configs.spatiocyte_VoxelRadius + voxel_volume = (4.0/3.0)*numpy.pi*voxel_radius**3 + voxel_depth = 2.0*voxel_radius - return signal + # Beer-Lamberts law : log(I0/I) = A = abs coef. * concentration * path length ([m2] * [#/m3] * [m]) + A = (abs_coeff*0.1/Na)*(1.0/voxel_volume)*(voxel_depth) + # get the number of photons emitted + N_emit = p_state*QY*n_abs*(1 - 10**(-A)) + # get signal + signal = N_emit/(4.0*numpy.pi) * fluo_psf + return signal diff --git a/bioimaging/palm_rewrite_script.py b/bioimaging/palm_rewrite_script.py index 26e39bb..a8264fc 100755 --- a/bioimaging/palm_rewrite_script.py +++ b/bioimaging/palm_rewrite_script.py @@ -7,54 +7,52 @@ def test_palm(t0, t1) : - # create PALM imaging - palm = PALMConfigs() - palm.set_ExcitationSource(source_type='LASER', wave_length=561, flux_density=20, angle=65.7) - palm.set_ActivationSource(source_type='LASER', wave_length=504, flux_density=20, angle=65.7, frame_time=0.5, F_bleach=20) - palm.set_Fluorophore(fluorophore_type='EGFP') -# palm.set_DichroicMirror('FF562-Di03-25x36') -# palm.set_EmissionFilter('FF01-593_40-25') - palm.set_Magnification(Mag=100) - - # Detector : CMOS Camera - palm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ - focal_point=(0.0,0.5,0.5), exposure_time=0.5, QE=0.73) - palm.set_ADConverter(bit=16, offset=100, fullwell=30000) - -# # Detector : EMCCD Camera -# palm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, \ -# focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.92, readout_noise=50, emgain=300) -# palm.set_ADConverter(bit=14, offset=2000, fullwell=800000) + # create PALM imaging + palm = PALMConfigs() + palm.set_ExcitationSource(source_type='LASER', wave_length=561, flux_density=20, angle=65.7) + palm.set_ActivationSource(source_type='LASER', wave_length=504, flux_density=20, angle=65.7, frame_time=0.5, F_bleach=20) + palm.set_Fluorophore(fluorophore_type='EGFP') +# palm.set_DichroicMirror('FF562-Di03-25x36') +# palm.set_EmissionFilter('FF01-593_40-25') + palm.set_Magnification(Mag=100) + + # Detector : CMOS Camera + palm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ + focal_point=(0.0,0.5,0.5), exposure_time=0.5, QE=0.73) + palm.set_ADConverter(bit=16, offset=100, fullwell=30000) + +# # Detector : EMCCD Camera +# palm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, \ +# focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.92, readout_noise=50, emgain=300) +# palm.set_ADConverter(bit=14, offset=2000, fullwell=800000) # -# # Output data -# palm.set_OutputData(image_file_dir='./numpys_palm_01000A') +# # Output data +# palm.set_OutputData(image_file_dir='./numpys_palm_01000A') - # Input data - #palm.reset_InputData('/home/masaki/ecell3/latest/data/csv/beads_palm_10000A', start=t0, end=t1, observable="A") - palm.reset_InputData('/home/masaki/wrk/spatiocyte/examples/2D/data_Ag05', start=t0, end=t1, observable="A") + # Input data + #palm.reset_InputData('/home/masaki/ecell3/latest/data/csv/beads_palm_10000A', start=t0, end=t1, observable="A") + palm.reset_InputData('/home/masaki/wrk/spatiocyte/examples/2D/data_Ag05', start=t0, end=t1, observable="A") - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0) - physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) - physics.set_photobleaching(tau0=1.8, alpha=0.73) - physics.set_photoactivation(turn_on_ratio=1000, activation_yield=0.1, frac_preactivation=0.00) - #physics.set_photoblinking(t0_on=1.00, a_on=0.58, t0_off=10e-6, a_off=0.48) + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0) + physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) + physics.set_photobleaching(tau0=1.8, alpha=0.73) + physics.set_photoactivation(turn_on_ratio=1000, activation_yield=0.1, frac_preactivation=0.00) + #physics.set_photoblinking(t0_on=1.00, a_on=0.58, t0_off=10e-6, a_off=0.48) - # create image - create = PALMVisualizer(configs=palm, effects=physics) - create.rewrite_InputData(output_file_dir='./data_palm_aggr05_temp') - #create.rewrite_InputData(output_file_dir='./data_palm_aggr05_bleach') - #create.rewrite_InputData(output_file_dir='./data_palm_aggr05_blink') - #create.output_frames(num_div=16) + # create image + create = PALMVisualizer(configs=palm, effects=physics) + create.rewrite_InputData(output_file_dir='./data_palm_aggr05_temp') + #create.rewrite_InputData(output_file_dir='./data_palm_aggr05_bleach') + #create.rewrite_InputData(output_file_dir='./data_palm_aggr05_blink') + #create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_palm(t0, t1) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_palm(t0, t1) diff --git a/bioimaging/palm_script.py b/bioimaging/palm_script.py index 7c360ee..5b8cfd1 100755 --- a/bioimaging/palm_script.py +++ b/bioimaging/palm_script.py @@ -7,53 +7,51 @@ def test_palm(t0, t1) : - # create PALM imaging - palm = PALMConfigs() - palm.set_ExcitationSource(source_type='LASER', wave_length=561, flux_density=20, angle=65.7) - palm.set_ActivationSource(source_type='LASER', wave_length=504, flux_density=20, angle=65.7, frame_time=0.5, F_bleach=20) - palm.set_Fluorophore(fluorophore_type='EGFP') -# palm.set_DichroicMirror('FF562-Di03-25x36') -# palm.set_EmissionFilter('FF01-593_40-25') - palm.set_Magnification(Mag=100) - - # Detector : CMOS Camera - palm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ - focal_point=(0.0,0.5,0.5), exposure_time=0.5, QE=0.73) - palm.set_ADConverter(bit=16, offset=100, fullwell=30000) - -# # Detector : EMCCD Camera -# palm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, \ -# focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.92, readout_noise=50, emgain=300) -# palm.set_ADConverter(bit=14, offset=2000, fullwell=800000) - - # Output data - palm.set_OutputData(image_file_dir='./numpys_palm_aggr05_bleach') - #palm.set_OutputData(image_file_dir='./numpys_palm_aggr05_blink') - - # Input data - palm.set_InputData('/home/masaki/bioimaging_4public/data_palm_aggr05_bleach', start=t0, end=t1, observable="A") - #palm.set_InputData('/home/masaki/bioimaging_4public/data_palm_aggr05_blink', start=t0, end=t1, observable="A") - - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0) - physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) - physics.set_photobleaching(tau0=1.8, alpha=0.73) - physics.set_photoactivation(turn_on_ratio=1000, activation_yield=0.1, frac_preactivation=0.00) - #physics.set_photoblinking(t0_on=1.00, a_on=0.58, t0_off=10e-6, a_off=0.48) - - # create image - create = PALMVisualizer(configs=palm, effects=physics) - #create.rewrite_InputData(output_file_dir='./data_palm_01000A') - create.output_frames(num_div=16) + # create PALM imaging + palm = PALMConfigs() + palm.set_ExcitationSource(source_type='LASER', wave_length=561, flux_density=20, angle=65.7) + palm.set_ActivationSource(source_type='LASER', wave_length=504, flux_density=20, angle=65.7, frame_time=0.5, F_bleach=20) + palm.set_Fluorophore(fluorophore_type='EGFP') +# palm.set_DichroicMirror('FF562-Di03-25x36') +# palm.set_EmissionFilter('FF01-593_40-25') + palm.set_Magnification(Mag=100) + + # Detector : CMOS Camera + palm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ + focal_point=(0.0,0.5,0.5), exposure_time=0.5, QE=0.73) + palm.set_ADConverter(bit=16, offset=100, fullwell=30000) + +# # Detector : EMCCD Camera +# palm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, \ +# focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.92, readout_noise=50, emgain=300) +# palm.set_ADConverter(bit=14, offset=2000, fullwell=800000) + + # Output data + palm.set_OutputData(image_file_dir='./numpys_palm_aggr05_bleach') + #palm.set_OutputData(image_file_dir='./numpys_palm_aggr05_blink') + + # Input data + palm.set_InputData('/home/masaki/bioimaging_4public/data_palm_aggr05_bleach', start=t0, end=t1, observable="A") + #palm.set_InputData('/home/masaki/bioimaging_4public/data_palm_aggr05_blink', start=t0, end=t1, observable="A") + + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0) + physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) + physics.set_photobleaching(tau0=1.8, alpha=0.73) + physics.set_photoactivation(turn_on_ratio=1000, activation_yield=0.1, frac_preactivation=0.00) + #physics.set_photoblinking(t0_on=1.00, a_on=0.58, t0_off=10e-6, a_off=0.48) + + # create image + create = PALMVisualizer(configs=palm, effects=physics) + #create.rewrite_InputData(output_file_dir='./data_palm_01000A') + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_palm(t0, t1) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_palm(t0, t1) diff --git a/bioimaging/parameter_configs.py b/bioimaging/parameter_configs.py index 40f2a5b..4a2e661 100755 --- a/bioimaging/parameter_configs.py +++ b/bioimaging/parameter_configs.py @@ -2,7 +2,7 @@ import numpy #----------------------------- -# General +# General #----------------------------- ignore_open_errors = False electron_charge = 1.602e-19 # C @@ -40,19 +40,19 @@ #fluorophore_rgb = numpy.array([(0, 0, 0) for j in range(len(depth))]) #----------------------------- -# Fluorophore PSF +# Fluorophore PSF #----------------------------- psf_wavelength = 600 # nm psf_normalization = 1.00 psf_intensity_noise = 0.50 -psf_width = (200, 200) # Gaussian function (radial width, lateral width) [nm] -psf_cutoff = (400, 100) # cutoff range (radius, depth) -psf_file_name_format = 'psf_%04d.png' # Image file name +psf_width = (200, 200) # Gaussian function (radial width, lateral width) [nm] +psf_cutoff = (400, 100) # cutoff range (radius, depth) +psf_file_name_format = 'psf_%04d.png' # Image file name #----------------------------- # Illumination -#----------------------------- -source_switch = False +#----------------------------- +source_switch = False source_type = 'LASER' source_wavelength = 600. # nm source_flux_density = 1 # W/cm2 diff --git a/bioimaging/parameter_effects.py b/bioimaging/parameter_effects.py index 40d19b9..ba9b1ec 100755 --- a/bioimaging/parameter_effects.py +++ b/bioimaging/parameter_effects.py @@ -50,4 +50,3 @@ photoblinking_a_on = 0.58 photoblinking_t0_off = 0.3 photoblinking_a_off = 0.48 - diff --git a/bioimaging/pmt_handler.py b/bioimaging/pmt_handler.py index 5b6c267..d97191d 100755 --- a/bioimaging/pmt_handler.py +++ b/bioimaging/pmt_handler.py @@ -62,21 +62,21 @@ def __init__(self, user_configs_dict = None): def _set_data(self, key, val) : - + if val != None: setattr(self, key, val) def set_Detector(self, detector = None, - mode = None, + mode = None, bandwidth = None, - QE = None, - readout_noise = None, - dark_count = None, - gain = None, - dyn_stages = None, - pair_pulses = None + QE = None, + readout_noise = None, + dark_count = None, + gain = None, + dyn_stages = None, + pair_pulses = None ): self._set_data('detector_switch', True) @@ -90,10 +90,10 @@ def set_Detector(self, detector = None, self._set_data('detector_dyn_stages', dyn_stages) self._set_data('detector_pair_pulses', pair_pulses) - print('--- Detector : ', self.detector_type, ' (', self.detector_mode, 'mode )') + print('--- Detector : ', self.detector_type, ' (', self.detector_mode, 'mode )') print('\tBandwidth = ', self.detector_exposure_time, 'Hz') print('\tQuantum Efficiency = ', 100*self.detector_qeff, '%') - print('\tReadout Noise = ', self.detector_readout_noise, 'electron/sec') + print('\tReadout Noise = ', self.detector_readout_noise, 'electron/sec') print('\tDark Count = ', self.detector_dark_count, 'electron/sec') print('\tGain = ', 'x', self.detector_gain) print('\tDynode = ', self.detector_dyn_stages, 'stages') @@ -102,222 +102,220 @@ def set_Detector(self, detector = None, def set_OutputData(self, image_file_dir = None, - image_file_cleanup_dir=False) : + image_file_cleanup_dir=False) : + + if image_file_dir is None : - if image_file_dir is None : + image_file_dir = tempfile.mkdtemp(dir=os.getcwd()) + image_file_cleanup_dir = True - image_file_dir = tempfile.mkdtemp(dir=os.getcwd()) - image_file_cleanup_dir = True - - self._set_data('image_file_dir', image_file_dir) - self._set_data('image_file_cleanup_dir', image_file_cleanup_dir) + self._set_data('image_file_dir', image_file_dir) + self._set_data('image_file_cleanup_dir', image_file_cleanup_dir) class PMTVisualizer() : - ''' - PMT Visualization class - ''' + ''' + PMT Visualization class + ''' - def __init__(self, configs=PMTConfigs()) : + def __init__(self, configs=PMTConfigs()) : - assert isinstance(configs, PMTConfigs) - self.configs = configs + assert isinstance(configs, PMTConfigs) + self.configs = configs - """ - Check and create the folder for image file. - """ - if not os.path.exists(self.configs.image_file_dir): - os.makedirs(self.configs.image_file_dir) - #else: - # for file in os.listdir(self.configs.movie_image_file_dir): - # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) + """ + Check and create the folder for image file. + """ + if not os.path.exists(self.configs.image_file_dir): + os.makedirs(self.configs.image_file_dir) + #else: + # for file in os.listdir(self.configs.movie_image_file_dir): + # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) - def output_frames(self, index=0, signal=0, background=0, duration=None): + def output_frames(self, index=0, signal=0, background=0, duration=None): - self.configs.image_file_name_format = "./output_%07d.dat" + self.configs.image_file_name_format = "./output_%07d.dat" - image_file_name = os.path.join(self.configs.image_file_dir, - self.configs.image_file_name_format % (index)) + image_file_name = os.path.join(self.configs.image_file_dir, + self.configs.image_file_name_format % (index)) - with open(image_file_name, 'w') as output : - output.write('#time\tphotons\t\n') - output.write('\n') + with open(image_file_name, 'w') as output : + output.write('#time\tphotons\t\n') + output.write('\n') - # PMT bandwidth - B = self.configs.detector_bandwidth - T = 1/(2*B) + # PMT bandwidth + B = self.configs.detector_bandwidth + T = 1/(2*B) - # initial time - time = 0 + # initial time + time = 0 - while (time < duration) : + while (time < duration) : - # get the number of photons detected in the PMT - photons = self.detector_output(signal + background) + # get the number of photons detected in the PMT + photons = self.detector_output(signal + background) - with open(image_file_name, 'a') as output : - line = str(time) + '\t' - line += str(photons) + '\t\n' - output.write(line) + with open(image_file_name, 'a') as output : + line = str(time) + '\t' + line += str(photons) + '\t\n' + output.write(line) - time += T + time += T # def output_frames(self, index=0, signal=0, background=0, image_size=(100,100)): # -# Nw = image_size[0] -# Nh = image_size[1] +# Nw = image_size[0] +# Nh = image_size[1] # -# flux = numpy.full([Nw, Nh], background) -# flux[int(0.2*Nw):int(0.8*Nw),int(0.2*Nh):int(0.8*Nh)] += numpy.full([int(0.6*Nw),int(0.6*Nh)], signal) +# flux = numpy.full([Nw, Nh], background) +# flux[int(0.2*Nw):int(0.8*Nw),int(0.2*Nh):int(0.8*Nh)] += numpy.full([int(0.6*Nw),int(0.6*Nh)], signal) # # # declear photon distribution for camera image # camera = numpy.zeros([Nw, Nh, 4]) # -# for i in range(Nw) : -# for j in range(Nh) : +# for i in range(Nw) : +# for j in range(Nh) : # -# camera[i][j] = self.detector_output(flux[i][j]) +# camera[i][j] = self.detector_output(flux[i][j]) # -# # save data to numpy-binary file -# image_file_name = os.path.join(self.configs.image_file_dir, -# self.configs.image_file_name_format % (index)) -# numpy.save(image_file_name, camera) - - +# # save data to numpy-binary file +# image_file_name = os.path.join(self.configs.image_file_dir, +# self.configs.image_file_name_format % (index)) +# numpy.save(image_file_name, camera) - def prob_analog(self, y, alpha) : - # get average gain - A = self.configs.detector_gain - # get dynode stages - nu = self.configs.detector_dyn_stages + def prob_analog(self, y, alpha) : - B = 0.5*(A - 1)/(A**(1.0/nu) - 1) - c = numpy.exp(alpha*(numpy.exp(-A/B) - 1)) + # get average gain + A = self.configs.detector_gain - m_y = alpha*A - m_x = m_y/(1 - c) + # get dynode stages + nu = self.configs.detector_dyn_stages - s2_y = alpha*(A**2 + 2*A*B) - s2_x = s2_y/(1 - c) - c*m_x**2 + B = 0.5*(A - 1)/(A**(1.0/nu) - 1) + c = numpy.exp(alpha*(numpy.exp(-A/B) - 1)) - # Rayleigh approximation - #s2 = (2.0/numpy.pi)*m_x**2 - #prob = y/s2*numpy.exp(-0.5*y**2/s2) + m_y = alpha*A + m_x = m_y/(1 - c) - if (y < 100*A) : - # Gamma approximation - k_1 = m_x - k_2 = (m_y**2 + s2_y)/(1 - c) + s2_y = alpha*(A**2 + 2*A*B) + s2_x = s2_y/(1 - c) - c*m_x**2 - a = 1/(k_1*(k_2/k_1**2 - 1)) - b = a*k_1 + # Rayleigh approximation + #s2 = (2.0/numpy.pi)*m_x**2 + #prob = y/s2*numpy.exp(-0.5*y**2/s2) - prob = a/gamma(b)*(a*y)**(b-1)*numpy.exp(-a*y) + if (y < 100*A) : + # Gamma approximation + k_1 = m_x + k_2 = (m_y**2 + s2_y)/(1 - c) - else : - # Truncated Gaussian approximation - Q = 0 - beta0 = m_x/numpy.sqrt(s2_x) - beta = beta0 - delta = 0.1*beta0 + a = 1/(k_1*(k_2/k_1**2 - 1)) + b = a*k_1 - while (beta < 11*beta0) : - Q += numpy.exp(-0.5*beta**2)/numpy.sqrt(2*numpy.pi)*delta - beta += delta + prob = a/gamma(b)*(a*y)**(b-1)*numpy.exp(-a*y) - prob = numpy.exp(-0.5*(y - m_x)**2/s2_x)/(numpy.sqrt(2*numpy.pi*s2_x)*(1 - Q)) - #print alpha, Q, prob + else : + # Truncated Gaussian approximation + Q = 0 + beta0 = m_x/numpy.sqrt(s2_x) + beta = beta0 + delta = 0.1*beta0 - return prob + while (beta < 11*beta0) : + Q += numpy.exp(-0.5*beta**2)/numpy.sqrt(2*numpy.pi)*delta + beta += delta + prob = numpy.exp(-0.5*(y - m_x)**2/s2_x)/(numpy.sqrt(2*numpy.pi*s2_x)*(1 - Q)) + #print alpha, Q, prob + return prob - def detector_output(self, photon_flux) : - # set seed for random number - numpy.random.seed() - # observational time - T = self.configs.detector_exposure_time + def detector_output(self, photon_flux) : - # Detector : Quantum Efficiency - QE = self.configs.detector_qeff + # set seed for random number + numpy.random.seed() - # get signal (photon flux) - Flux = photon_flux + # observational time + T = self.configs.detector_exposure_time - if (self.configs.detector_mode == "Photon-counting") : - # pair-pulses time resolution (sec) - t_pp = self.configs.detector_pair_pulses - Flux = Flux/(1 + Flux*t_pp) + # Detector : Quantum Efficiency + QE = self.configs.detector_qeff - Photons = Flux*T + # get signal (photon flux) + Flux = photon_flux - # get signal (expectation) - Exp = QE*Photons + if (self.configs.detector_mode == "Photon-counting") : + # pair-pulses time resolution (sec) + t_pp = self.configs.detector_pair_pulses + Flux = Flux/(1 + Flux*t_pp) - # get dark count - D = self.configs.detector_dark_count - Exp += D*T + Photons = Flux*T - # select Camera type - if (self.configs.detector_mode == "Photon-counting") : + # get signal (expectation) + Exp = QE*Photons - # get signal (poisson distributions) - signal = numpy.random.poisson(Exp, None) + # get dark count + D = self.configs.detector_dark_count + Exp += D*T + # select Camera type + if (self.configs.detector_mode == "Photon-counting") : - elif (self.configs.detector_mode == "Analog") : + # get signal (poisson distributions) + signal = numpy.random.poisson(Exp, None) - # get signal (photoelectrons) - if (Exp > 0) : - # get EM gain - G = self.configs.detector_gain + elif (self.configs.detector_mode == "Analog") : - # signal array - if (Exp > 1) : sig = numpy.sqrt(Exp) - else : sig = 1 + # get signal (photoelectrons) + if (Exp > 0) : - s_min = int(G*(Exp - 10*sig)) - s_max = int(G*(Exp + 10*sig)) + # get EM gain + G = self.configs.detector_gain - if (s_min < 0) : s_min = 0 + # signal array + if (Exp > 1) : sig = numpy.sqrt(Exp) + else : sig = 1 - delta = (s_max - s_min)/1000. + s_min = int(G*(Exp - 10*sig)) + s_max = int(G*(Exp + 10*sig)) - s = numpy.array([k*delta+s_min for k in range(1000)]) + if (s_min < 0) : s_min = 0 - # probability density function - p_signal = numpy.array(map(lambda y : self.prob_analog(y, Exp), s)) - p_ssum = p_signal.sum() + delta = (s_max - s_min)/1000. - # get signal (photoelectrons) - signal = numpy.random.choice(s, None, p=p_signal/p_ssum) - signal = signal/G + s = numpy.array([k*delta+s_min for k in range(1000)]) - else : - signal = 0 + # probability density function + p_signal = numpy.array(map(lambda y : self.prob_analog(y, Exp), s)) + p_ssum = p_signal.sum() - # get detector noise (photoelectrons) - Nr = self.configs.detector_readout_noise + # get signal (photoelectrons) + signal = numpy.random.choice(s, None, p=p_signal/p_ssum) + signal = signal/G - if (Nr > 0) : noise = numpy.random.normal(0, Nr, None) - else : noise = 0 + else : + signal = 0 - # the number of photons detected in the PMT - PE = signal + noise + # get detector noise (photoelectrons) + Nr = self.configs.detector_readout_noise - intensity = [Photons, Exp, PE, int(PE)] + if (Nr > 0) : noise = numpy.random.normal(0, Nr, None) + else : noise = 0 - return intensity + # the number of photons detected in the PMT + PE = signal + noise + intensity = [Photons, Exp, PE, int(PE)] + return intensity diff --git a/bioimaging/pmt_script.py b/bioimaging/pmt_script.py index 2fb3555..2594613 100755 --- a/bioimaging/pmt_script.py +++ b/bioimaging/pmt_script.py @@ -8,27 +8,25 @@ def test_pmt(index) : - # create PMT configuration - pmt = PMTConfigs() - pmt.set_Detector(detector='PMT', mode="Analog", readout_noise=0, dark_count=0, \ - bandwidth=5e+3, QE=0.30, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) + # create PMT configuration + pmt = PMTConfigs() + pmt.set_Detector(detector='PMT', mode="Analog", readout_noise=0, dark_count=0, \ + bandwidth=5e+3, QE=0.30, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) - ### Output data - #pmt.set_OutputData(image_file_dir='./images_pmt_pulses_1000dc') - #pmt.set_OutputData(image_file_dir='./images_pmt_analog_1000dc') - pmt.set_OutputData(image_file_dir='./images_pmt_test') + ### Output data + #pmt.set_OutputData(image_file_dir='./images_pmt_pulses_1000dc') + #pmt.set_OutputData(image_file_dir='./images_pmt_analog_1000dc') + pmt.set_OutputData(image_file_dir='./images_pmt_test') - # create image and movie - create = PMTVisualizer(configs=pmt) - create.output_frames(index=index, signal=flux[index], background=0, duration=0.1) - #create.output_frames(index=index, signal=flux[index], background=0, image_size=(100,100)) + # create image and movie + create = PMTVisualizer(configs=pmt) + create.output_frames(index=index, signal=flux[index], background=0, duration=0.1) + #create.output_frames(index=index, signal=flux[index], background=0, image_size=(100,100)) if __name__ == "__main__": - index = int(sys.argv[1]) - - test_pmt(index) - + index = int(sys.argv[1]) + test_pmt(index) diff --git a/bioimaging/pointscan_confm_handler.py b/bioimaging/pointscan_confm_handler.py index bd4974b..1b5bb52 100755 --- a/bioimaging/pointscan_confm_handler.py +++ b/bioimaging/pointscan_confm_handler.py @@ -27,13 +27,13 @@ class PointScanConfocalConfigs(FCSConfigs) : ''' Point-scanning Confocal configuration - Point-like Gaussian Profile - + - Point-scanning - + - Pinhole - + - Detector : PMT + Point-like Gaussian Profile + + + Point-scanning + + + Pinhole + + + Detector : PMT ''' def __init__(self, user_configs_dict = None): @@ -71,18 +71,18 @@ def set_Pinhole(self, radius = None) : def set_Detector(self, detector = None, - mode = None, - image_size = None, + mode = None, + image_size = None, pixel_length = None, focal_point = None, base_position = None, scan_time = None, - QE = None, - readout_noise = None, - dark_count = None, - gain = None, - dyn_stages = None, - pair_pulses = None + QE = None, + readout_noise = None, + dark_count = None, + gain = None, + dyn_stages = None, + pair_pulses = None ): self._set_data('detector_switch', True) @@ -100,14 +100,14 @@ def set_Detector(self, detector = None, self._set_data('detector_dyn_stages', dyn_stages) self._set_data('detector_pair_pulses', pair_pulses) - print('--- Detector : ', self.detector_type, ' (', self.detector_mode, 'mode )') + print('--- Detector : ', self.detector_type, ' (', self.detector_mode, 'mode )') print('\tImage Size = ', self.detector_image_size[0], 'x', self.detector_image_size[1]) print('\tPixel Size = ', self.detector_pixel_length, 'm/pixel') print('\tFocal Point = ', self.detector_focal_point) print('\tPosition = ', self.detector_base_position) print('\tScan Time = ', self.detector_exposure_time, 'sec/image') print('\tQuantum Efficiency = ', 100*self.detector_qeff, '%') - print('\tReadout Noise = ', self.detector_readout_noise, 'electron') + print('\tReadout Noise = ', self.detector_readout_noise, 'electron') print('\tDark Count = ', self.detector_dark_count, 'electron/sec') print('\tGain = ', 'x', self.detector_gain) print('\tDynode = ', self.detector_dyn_stages, 'stages') @@ -124,26 +124,26 @@ def set_Illumination_path(self) : # (plank const) * (speed of light) [joules meter] hc = 2.00e-25 - # Illumination + # Illumination w_0 = self.source_radius - # flux [joules/sec=watt] + # flux [joules/sec=watt] P_0 = self.source_flux # single photon energy [joules] wave_length = self.source_wavelength*1e-9 E_wl = hc/wave_length - # photon flux [photons/sec] + # photon flux [photons/sec] N_0 = P_0/E_wl # Beam width [m] w_z = w_0*numpy.sqrt(1 + ((wave_length*d*1e-9)/(numpy.pi*w_0**2))**2) - # photon flux density [photon/(sec m^2)] + # photon flux density [photon/(sec m^2)] self.source_flux_density = numpy.array(map(lambda x : 2*N_0/(numpy.pi*x**2)*numpy.exp(-2*(r*1e-9/x)**2), w_z)) - print('Photon Flux Density (Max) :', numpy.amax(self.source_flux_density)) + print('Photon Flux Density (Max) :', numpy.amax(self.source_flux_density)) @@ -151,23 +151,23 @@ def set_Detection_path(self) : wave_length = self.psf_wavelength*1e-9 - # Magnification - Mag = self.image_magnification + # Magnification + Mag = self.image_magnification - # set voxel radius + # set voxel radius voxel_radius = self.spatiocyte_VoxelRadius - # set pixel length - #pixel_length = (2.0*self.pinhole_radius)/Mag - pixel_length = self.detector_pixel_length + # set pixel length + #pixel_length = (2.0*self.pinhole_radius)/Mag + pixel_length = self.detector_pixel_length - # set image scaling factor - self.image_resolution = pixel_length - self.image_scaling = pixel_length/(2.0*voxel_radius) + # set image scaling factor + self.image_resolution = pixel_length + self.image_scaling = pixel_length/(2.0*voxel_radius) - print('Magnification : x %d' % (Mag)) - print('Resolution :', self.image_resolution, 'm/pixel') - print('Scaling :', self.image_scaling) + print('Magnification : x %d' % (Mag)) + print('Resolution :', self.image_resolution, 'm/pixel') + print('Scaling :', self.image_scaling) # Detector PSF self.set_PSF_detector() @@ -177,310 +177,310 @@ def set_Detection_path(self) : class PointScanConfocalVisualizer(FCSVisualizer) : - ''' - Confocal Visualization class of e-cell simulator - ''' + ''' + Confocal Visualization class of e-cell simulator + ''' - def __init__(self, configs=PointScanConfocalConfigs(), effects=PhysicalEffects()) : + def __init__(self, configs=PointScanConfocalConfigs(), effects=PhysicalEffects()) : - assert isinstance(configs, PointScanConfocalConfigs) - self.configs = configs + assert isinstance(configs, PointScanConfocalConfigs) + self.configs = configs - assert isinstance(effects, PhysicalEffects) - self.effects = effects + assert isinstance(effects, PhysicalEffects) + self.effects = effects - """ - Check and create the folder for image file. - """ - if not os.path.exists(self.configs.image_file_dir): - os.makedirs(self.configs.image_file_dir) - #else: - # for file in os.listdir(self.configs.movie_image_file_dir): - # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) + """ + Check and create the folder for image file. + """ + if not os.path.exists(self.configs.image_file_dir): + os.makedirs(self.configs.image_file_dir) + #else: + # for file in os.listdir(self.configs.movie_image_file_dir): + # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) - """ - Optical Path - """ - self.configs.set_Optical_path() + """ + Optical Path + """ + self.configs.set_Optical_path() - def get_signal(self, time, pid, s_index, p_i, p_b, p_0, norm) : + def get_signal(self, time, pid, s_index, p_i, p_b, p_0, norm) : - # set focal point - x_0, y_0, z_0 = p_0 + # set focal point + x_0, y_0, z_0 = p_0 - # set source center - x_b, y_b, z_b = p_b + # set source center + x_b, y_b, z_b = p_b - # set particle position - x_i, y_i, z_i = p_i + # set particle position + x_i, y_i, z_i = p_i - # - r = self.configs.radial - d = self.configs.depth + # + r = self.configs.radial + d = self.configs.depth - # beam axial position - d_s = abs(x_i - x_b) + # beam axial position + d_s = abs(x_i - x_b) - if (d_s < 20000) : - source_depth = d_s - else : - source_depth = 19999 + if (d_s < 20000) : + source_depth = d_s + else : + source_depth = 19999 - # beam horizontal position (y-direction) - hh = numpy.sqrt((y_i-y_b)**2) + # beam horizontal position (y-direction) + hh = numpy.sqrt((y_i-y_b)**2) - if (hh < len(r)) : - source_horizon = hh - else : - source_horizon = r[-1] + if (hh < len(r)) : + source_horizon = hh + else : + source_horizon = r[-1] - # get illumination PSF - source_psf = self.configs.source_flux_density[int(source_depth)][int(source_horizon)] - #source_max = norm*self.configs.source_flux_density[0][0] + # get illumination PSF + source_psf = self.configs.source_flux_density[int(source_depth)][int(source_horizon)] + #source_max = norm*self.configs.source_flux_density[0][0] - # signal conversion : - #Intensity = self.get_intensity(time, pid, source_psf, source_max) - Ratio = self.effects.conversion_ratio + # signal conversion : + #Intensity = self.get_intensity(time, pid, source_psf, source_max) + Ratio = self.effects.conversion_ratio - # fluorophore axial position - d_f = abs(x_i - x_b) + # fluorophore axial position + d_f = abs(x_i - x_b) - if (d_f < len(d)) : - fluo_depth = d_f - else : - fluo_depth = d[-1] + if (d_f < len(d)) : + fluo_depth = d_f + else : + fluo_depth = d[-1] - # get fluorophore PSF - fluo_psf = self.fluo_psf[int(fluo_depth)] + # get fluorophore PSF + fluo_psf = self.fluo_psf[int(fluo_depth)] - # signal conversion : Output PSF = PSF(source) * Ratio * PSF(Fluorophore) - signal = norm * source_psf * Ratio * fluo_psf + # signal conversion : Output PSF = PSF(source) * Ratio * PSF(Fluorophore) + signal = norm * source_psf * Ratio * fluo_psf - return signal + return signal - def get_molecule_plane(self, cell, time, data, pid, p_b, p_0) : + def get_molecule_plane(self, cell, time, data, pid, p_b, p_0) : - #voxel_size = (2.0*self.configs.spatiocyte_VoxelRadius)/1e-9 + #voxel_size = (2.0*self.configs.spatiocyte_VoxelRadius)/1e-9 - # get beam position - x_b, y_b, z_b = p_b + # get beam position + x_b, y_b, z_b = p_b - # cutoff randius - Mag = self.configs.image_magnification - pinhole_radius = int(self.configs.pinhole_radius/Mag/1e-9) - cut_off = int(1.5*pinhole_radius) + # cutoff randius + Mag = self.configs.image_magnification + pinhole_radius = int(self.configs.pinhole_radius/Mag/1e-9) + cut_off = int(1.5*pinhole_radius) - # particles coordinate, species and lattice IDs - c_id, s_id, l_id = data + # particles coordinate, species and lattice IDs + c_id, s_id, l_id = data - sid_array = numpy.array(self.configs.spatiocyte_species_id) - s_index = (numpy.abs(sid_array - int(s_id))).argmin() + sid_array = numpy.array(self.configs.spatiocyte_species_id) + s_index = (numpy.abs(sid_array - int(s_id))).argmin() - if self.configs.spatiocyte_observables[s_index] is True : + if self.configs.spatiocyte_observables[s_index] is True : - # Normalization - unit_time = 1.0 - unit_area = (1e-9)**2 - norm = (unit_area*unit_time)/(4.0*numpy.pi) + # Normalization + unit_time = 1.0 + unit_area = (1e-9)**2 + norm = (unit_area*unit_time)/(4.0*numpy.pi) - # particles coordinate in nm-scale - p_i = self.get_coordinate(c_id) - #p_i = p_0 - x_i, y_i, z_i = p_i + # particles coordinate in nm-scale + p_i = self.get_coordinate(c_id) + #p_i = p_0 + x_i, y_i, z_i = p_i - if (numpy.sqrt((y_i - y_b)**2 + (z_i - z_b)**2) < cut_off) : + if (numpy.sqrt((y_i - y_b)**2 + (z_i - z_b)**2) < cut_off) : - #print pid, s_id, p_i - # get signal matrix - signal = self.get_signal(time, pid, s_index, p_i, p_b, p_0, norm) + #print pid, s_id, p_i + # get signal matrix + signal = self.get_signal(time, pid, s_index, p_i, p_b, p_0, norm) - # add signal matrix to image plane - self.overwrite_signal(cell, signal, p_i, p_b) + # add signal matrix to image plane + self.overwrite_signal(cell, signal, p_i, p_b) - def overwrite_signal(self, cell, signal, p_i, p_b) : + def overwrite_signal(self, cell, signal, p_i, p_b) : - # particle position - x_i, y_i, z_i = p_i + # particle position + x_i, y_i, z_i = p_i - # beam position - x_b, y_b, z_b = p_b + # beam position + x_b, y_b, z_b = p_b - # z-axis - Nz_cell = len(cell) - Nz_signal = len(signal) + # z-axis + Nz_cell = len(cell) + Nz_signal = len(signal) - Nh_cell = Nz_cell/2 - Nh_signal = (Nz_signal-1)/2 + Nh_cell = Nz_cell/2 + Nh_signal = (Nz_signal-1)/2 - z_to = (z_i + Nh_signal) - (z_b - Nh_cell) - z_from = (z_i - Nh_signal) - (z_b - Nh_cell) + z_to = (z_i + Nh_signal) - (z_b - Nh_cell) + z_from = (z_i - Nh_signal) - (z_b - Nh_cell) - flag_z = True + flag_z = True - if (z_to > Nz_cell + Nz_signal) : - flag_z = False + if (z_to > Nz_cell + Nz_signal) : + flag_z = False - elif (z_to > Nz_cell and - z_to < Nz_cell + Nz_signal) : + elif (z_to > Nz_cell and + z_to < Nz_cell + Nz_signal) : - dz_to = z_to - Nz_cell + dz_to = z_to - Nz_cell - zi_to = int(Nz_signal - dz_to) - zb_to = int(Nz_cell) + zi_to = int(Nz_signal - dz_to) + zb_to = int(Nz_cell) - elif (z_to > 0 and z_to < Nz_cell) : + elif (z_to > 0 and z_to < Nz_cell) : - zi_to = int(Nz_signal) - zb_to = int(z_to) + zi_to = int(Nz_signal) + zb_to = int(z_to) - else : flag_z = False + else : flag_z = False - if (z_from < 0) : + if (z_from < 0) : - zi_from = int(abs(z_from)) - zb_from = 0 + zi_from = int(abs(z_from)) + zb_from = 0 - else : + else : - zi_from = 0 - zb_from = int(z_from) + zi_from = 0 + zb_from = int(z_from) - if (flag_z == True) : - ddz = (zi_to - zi_from) - (zb_to - zb_from) + if (flag_z == True) : + ddz = (zi_to - zi_from) - (zb_to - zb_from) - if (ddz > 0) : zi_to = zi_to - ddz - if (ddz < 0) : zb_to = zb_to + ddz + if (ddz > 0) : zi_to = zi_to - ddz + if (ddz < 0) : zb_to = zb_to + ddz - # y-axis - Ny_cell = cell.size/Nz_cell - Ny_signal = signal.size/Nz_signal + # y-axis + Ny_cell = cell.size/Nz_cell + Ny_signal = signal.size/Nz_signal - Nh_cell = Ny_cell/2 - Nh_signal = (Ny_signal-1)/2 + Nh_cell = Ny_cell/2 + Nh_signal = (Ny_signal-1)/2 - y_to = (y_i + Nh_signal) - (y_b - Nh_cell) - y_from = (y_i - Nh_signal) - (y_b - Nh_cell) + y_to = (y_i + Nh_signal) - (y_b - Nh_cell) + y_from = (y_i - Nh_signal) - (y_b - Nh_cell) - flag_y = True + flag_y = True - if (y_to > Ny_cell + Ny_signal) : - flag_y = False + if (y_to > Ny_cell + Ny_signal) : + flag_y = False - elif (y_to > Ny_cell and - y_to < Ny_cell + Ny_signal) : + elif (y_to > Ny_cell and + y_to < Ny_cell + Ny_signal) : - dy_to = y_to - Ny_cell + dy_to = y_to - Ny_cell - yi_to = int(Ny_signal - dy_to) - yb_to = int(Ny_cell) + yi_to = int(Ny_signal - dy_to) + yb_to = int(Ny_cell) - elif (y_to > 0 and y_to < Ny_cell) : + elif (y_to > 0 and y_to < Ny_cell) : - yi_to = int(Ny_signal) - yb_to = int(y_to) + yi_to = int(Ny_signal) + yb_to = int(y_to) - else : flag_y = False + else : flag_y = False - if (y_from < 0) : + if (y_from < 0) : - yi_from = int(abs(y_from)) - yb_from = 0 + yi_from = int(abs(y_from)) + yb_from = 0 - else : + else : - yi_from = 0 - yb_from = int(y_from) + yi_from = 0 + yb_from = int(y_from) - if (flag_y == True) : - ddy = (yi_to - yi_from) - (yb_to - yb_from) + if (flag_y == True) : + ddy = (yi_to - yi_from) - (yb_to - yb_from) - if (ddy > 0) : yi_to = yi_to - ddy - if (ddy < 0) : yb_to = yb_to + ddy + if (ddy > 0) : yi_to = yi_to - ddy + if (ddy < 0) : yb_to = yb_to + ddy - if (flag_z == True and flag_y == True) : -# if (abs(ddy) > 2 or abs(ddz) > 2) : -# print zi_from, zi_to, yi_from, yi_to, ddy, ddz -# print zb_from, zb_to, yb_from, yb_to + if (flag_z == True and flag_y == True) : +# if (abs(ddy) > 2 or abs(ddz) > 2) : +# print zi_from, zi_to, yi_from, yi_to, ddy, ddz +# print zb_from, zb_to, yb_from, yb_to - # add to cellular plane - cell[zb_from:zb_to, yb_from:yb_to] += signal[zi_from:zi_to, yi_from:yi_to] + # add to cellular plane + cell[zb_from:zb_to, yb_from:yb_to] += signal[zi_from:zi_to, yi_from:yi_to] - #return cell + #return cell - def output_frames(self, num_div=1): + def output_frames(self, num_div=1): - # set Fluorophores PSF - self.set_fluo_psf() + # set Fluorophores PSF + self.set_fluo_psf() - start = self.configs.spatiocyte_start_time - end = self.configs.spatiocyte_end_time + start = self.configs.spatiocyte_start_time + end = self.configs.spatiocyte_end_time - exposure_time = self.configs.detector_exposure_time - num_timesteps = int(math.ceil((end - start) / exposure_time)) + exposure_time = self.configs.detector_exposure_time + num_timesteps = int(math.ceil((end - start) / exposure_time)) - index0 = int(round(start/exposure_time)) + index0 = int(round(start/exposure_time)) - envname = 'ECELL_MICROSCOPE_SINGLE_PROCESS' + envname = 'ECELL_MICROSCOPE_SINGLE_PROCESS' - if envname in os.environ and os.environ[envname]: - self.output_frames_each_process(index0, num_timesteps) - else: - num_processes = multiprocessing.cpu_count() - n, m = divmod(num_timesteps, num_processes) - # when 10 tasks is distributed to 4 processes, - # number of tasks of each process must be [3, 3, 2, 2] - chunks = [n + 1 if i < m else n for i in range(num_processes)] + if envname in os.environ and os.environ[envname]: + self.output_frames_each_process(index0, num_timesteps) + else: + num_processes = multiprocessing.cpu_count() + n, m = divmod(num_timesteps, num_processes) + # when 10 tasks is distributed to 4 processes, + # number of tasks of each process must be [3, 3, 2, 2] + chunks = [n + 1 if i < m else n for i in range(num_processes)] - processes = [] - start_index = index0 + processes = [] + start_index = index0 - for chunk in chunks: - stop_index = start_index + chunk - process = multiprocessing.Process( - target=self.output_frames_each_process, - args=(start_index, stop_index)) - process.start() - processes.append(process) - start_index = stop_index + for chunk in chunks: + stop_index = start_index + chunk + process = multiprocessing.Process( + target=self.output_frames_each_process, + args=(start_index, stop_index)) + process.start() + processes.append(process) + start_index = stop_index - for process in processes: - process.join() + for process in processes: + process.join() - def output_frames_each_process(self, start_count, stop_count): + def output_frames_each_process(self, start_count, stop_count): - voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 + voxel_size = 2.0*self.configs.spatiocyte_VoxelRadius/1e-9 - # image dimenssion in pixel-scale - Nw_pixel = self.configs.detector_image_size[0] - Nh_pixel = self.configs.detector_image_size[1] + # image dimenssion in pixel-scale + Nw_pixel = self.configs.detector_image_size[0] + Nh_pixel = self.configs.detector_image_size[1] - # cells dimenssion in nm-scale - Nz = int(self.configs.spatiocyte_lengths[2] * voxel_size) - Ny = int(self.configs.spatiocyte_lengths[1] * voxel_size) - Nx = int(self.configs.spatiocyte_lengths[0] * voxel_size) + # cells dimenssion in nm-scale + Nz = int(self.configs.spatiocyte_lengths[2] * voxel_size) + Ny = int(self.configs.spatiocyte_lengths[1] * voxel_size) + Nx = int(self.configs.spatiocyte_lengths[0] * voxel_size) - # pixel length : nm/pixel - Np = int(self.configs.image_scaling*voxel_size) + # pixel length : nm/pixel + Np = int(self.configs.image_scaling*voxel_size) - # cells dimenssion in pixel-scale - Ny_pixel = Ny/Np - Nz_pixel = Nz/Np + # cells dimenssion in pixel-scale + Ny_pixel = Ny/Np + Nz_pixel = Nz/Np - # focal point - p_0 = numpy.array([Nx, Ny, Nz])*self.configs.detector_focal_point + # focal point + p_0 = numpy.array([Nx, Ny, Nz])*self.configs.detector_focal_point - # Beam position : - beam_center = numpy.array(self.configs.detector_focal_point) + # Beam position : + beam_center = numpy.array(self.configs.detector_focal_point) # # set boundary condition # if (self.configs.spatiocyte_bc_switch == True) : @@ -488,266 +488,266 @@ def output_frames_each_process(self, start_count, stop_count): # bc = numpy.zeros(shape=(Nz, Ny)) # bc = self.set_boundary_plane(bc, p_b, p_0) - # exposure time - exposure_time = self.configs.detector_exposure_time + # exposure time + exposure_time = self.configs.detector_exposure_time + + # contacting time with cells vertical-axis + R_z = float(Nz_pixel) / float(Nh_pixel) - # contacting time with cells vertical-axis - R_z = float(Nz_pixel) / float(Nh_pixel) + z_exposure_time = exposure_time + z_contact_time = R_z * z_exposure_time + z_nocontact_time = (z_exposure_time - z_contact_time)/2 - z_exposure_time = exposure_time - z_contact_time = R_z * z_exposure_time - z_nocontact_time = (z_exposure_time - z_contact_time)/2 + # contacting time with cells horizontal-axis + R_y = float(Ny_pixel) / float(Nw_pixel) - # contacting time with cells horizontal-axis - R_y = float(Ny_pixel) / float(Nw_pixel) + y_exposure_time = z_exposure_time/Nw_pixel + y_contact_time = R_y * y_exposure_time + y_nocontact_time = (y_exposure_time - y_contact_time)/2 - y_exposure_time = z_exposure_time/Nw_pixel - y_contact_time = R_y * y_exposure_time - y_nocontact_time = (y_exposure_time - y_contact_time)/2 + ##### + start_time = self.configs.spatiocyte_start_time - ##### - start_time = self.configs.spatiocyte_start_time + time = exposure_time * start_count + end = exposure_time * stop_count - time = exposure_time * start_count - end = exposure_time * stop_count + # data-time interval + data_interval = self.configs.spatiocyte_interval - # data-time interval - data_interval = self.configs.spatiocyte_interval + delta_time = int(round(exposure_time / data_interval)) - delta_time = int(round(exposure_time / data_interval)) + # create frame data composed by frame element data + count = start_count + count0 = int(round(start_time / exposure_time)) - # create frame data composed by frame element data - count = start_count - count0 = int(round(start_time / exposure_time)) + # initialize Physical effects + #length0 = len(self.configs.spatiocyte_data[0][1]) + #self.effects.set_states(t0, length0) - # initialize Physical effects - #length0 = len(self.configs.spatiocyte_data[0][1]) - #self.effects.set_states(t0, length0) + while (time < end) : - while (time < end) : + image_file_name = os.path.join(self.configs.image_file_dir, + self.configs.image_file_name_format % (count)) - image_file_name = os.path.join(self.configs.image_file_dir, - self.configs.image_file_name_format % (count)) + print('time : ', time, ' sec (', count, ')') - print('time : ', time, ' sec (', count, ')') + # define cell in nm-scale + #cell = numpy.zeros(shape=(Nz, Ny)) + # define image array in pixel-scale + image_pixel = numpy.zeros([Nw_pixel, Nh_pixel, 2]) - # define cell in nm-scale - #cell = numpy.zeros(shape=(Nz, Ny)) - # define image array in pixel-scale - image_pixel = numpy.zeros([Nw_pixel, Nh_pixel, 2]) + count_start = (count - count0)*delta_time + count_end = (count - count0 + 1)*delta_time - count_start = (count - count0)*delta_time - count_end = (count - count0 + 1)*delta_time + frame_data = self.configs.spatiocyte_data[count_start:count_end] - frame_data = self.configs.spatiocyte_data[count_start:count_end] + if (len(frame_data) > 0) : - if (len(frame_data) > 0) : + # Beam position : initial + p_b = numpy.array([Nx, Ny, Nz])*beam_center - # Beam position : initial - p_b = numpy.array([Nx, Ny, Nz])*beam_center + # Beam position : z-direction (image in pixel-scale) + z_image_pixel = int(z_nocontact_time/y_exposure_time) - # Beam position : z-direction (image in pixel-scale) - z_image_pixel = int(z_nocontact_time/y_exposure_time) + # contact time : z-direction + z_scan_time = 0 - # contact time : z-direction - z_scan_time = 0 + # no contact time : z-direction + non_contact_time = z_nocontact_time - # no contact time : z-direction - non_contact_time = z_nocontact_time + # vertical-scanning sequences + while (z_scan_time < z_contact_time) : - # vertical-scanning sequences - while (z_scan_time < z_contact_time) : + # Beam position : z-direction + beam_center[2] = z_scan_time/z_contact_time - # Beam position : z-direction - beam_center[2] = z_scan_time/z_contact_time + # Beam position : y-direction (image in pixel-scale) + y_image_pixel = int(y_nocontact_time/y_exposure_time*Nw_pixel) - # Beam position : y-direction (image in pixel-scale) - y_image_pixel = int(y_nocontact_time/y_exposure_time*Nw_pixel) + # contact time : y-direction + y_scan_time = 0 - # contact time : y-direction - y_scan_time = 0 + # no contact time : y-direction (left-margin) + non_contact_time += y_nocontact_time - # no contact time : y-direction (left-margin) - non_contact_time += y_nocontact_time + # horizontal-scanning sequences + while (y_scan_time < y_contact_time) : - # horizontal-scanning sequences - while (y_scan_time < y_contact_time) : + # Beam position : y-direction (cell in nm-scale) + beam_center[1] = y_scan_time/y_contact_time - # Beam position : y-direction (cell in nm-scale) - beam_center[1] = y_scan_time/y_contact_time + p_b = numpy.array([Nx, Ny, Nz])*beam_center + x_b, y_b, z_b = p_b - p_b = numpy.array([Nx, Ny, Nz])*beam_center - x_b, y_b, z_b = p_b + # loop for frame data + i_time, i_data = frame_data[0] - # loop for frame data - i_time, i_data = frame_data[0] + scan_time = z_scan_time + y_scan_time + non_contact_time + diff = abs(i_time - (scan_time + time)) + data = i_data - scan_time = z_scan_time + y_scan_time + non_contact_time - diff = abs(i_time - (scan_time + time)) - data = i_data + for i, (i_time, i_data) in enumerate(frame_data) : + #print '\t', '%02d-th frame : ' % (i), i_time, ' sec' + i_diff = abs(i_time - (scan_time + time)) - for i, (i_time, i_data) in enumerate(frame_data) : - #print '\t', '%02d-th frame : ' % (i), i_time, ' sec' - i_diff = abs(i_time - (scan_time + time)) + if (i_diff < diff) : + diff = i_diff + data = i_data - if (i_diff < diff) : - diff = i_diff - data = i_data + # overwrite the scanned region to cell + #r_p = int(self.configs.image_scaling*voxel_size/2) + Mag = self.configs.image_magnification + r_p = int(self.configs.pinhole_radius/Mag/1e-9) - # overwrite the scanned region to cell - #r_p = int(self.configs.image_scaling*voxel_size/2) - Mag = self.configs.image_magnification - r_p = int(self.configs.pinhole_radius/Mag/1e-9) + if (y_b-r_p < 0) : y_from = int(y_b) + else : y_from = int(y_b - r_p) - if (y_b-r_p < 0) : y_from = int(y_b) - else : y_from = int(y_b - r_p) + if (y_b+r_p >= Ny) : y_to = int(y_b) + else : y_to = int(y_b + r_p) - if (y_b+r_p >= Ny) : y_to = int(y_b) - else : y_to = int(y_b + r_p) + if (z_b-r_p < 0) : z_from = int(z_b) + else : z_from = int(z_b - r_p) - if (z_b-r_p < 0) : z_from = int(z_b) - else : z_from = int(z_b - r_p) + if (z_b+r_p >= Nz) : z_to = int(z_b) + else : z_to = int(z_b + r_p) - if (z_b+r_p >= Nz) : z_to = int(z_b) - else : z_to = int(z_b + r_p) + mask = numpy.zeros(shape=(z_to-z_from, y_to-y_from)) - mask = numpy.zeros(shape=(z_to-z_from, y_to-y_from)) + zz, yy = numpy.ogrid[z_from-int(z_b):z_to-int(z_b), y_from-int(y_b):y_to-int(y_b)] + rr_cut = yy**2 + zz**2 < r_p**2 + mask[rr_cut] = 1 - zz, yy = numpy.ogrid[z_from-int(z_b):z_to-int(z_b), y_from-int(y_b):y_to-int(y_b)] - rr_cut = yy**2 + zz**2 < r_p**2 - mask[rr_cut] = 1 + scan_cell = numpy.zeros_like(mask) - scan_cell = numpy.zeros_like(mask) + # loop for particles + for j, j_data in enumerate(data) : + self.get_molecule_plane(scan_cell, i_time, j_data, j, p_b, p_0) - # loop for particles - for j, j_data in enumerate(data) : - self.get_molecule_plane(scan_cell, i_time, j_data, j, p_b, p_0) + # image pixel position + ii, jj = z_image_pixel, y_image_pixel - # image pixel position - ii, jj = z_image_pixel, y_image_pixel + #cell[z_from:z_to, y_from:y_to] += mask*scan_cell + Photon_flux = numpy.sum(mask*scan_cell) + image_pixel[ii][jj][0] = Photon_flux - #cell[z_from:z_to, y_from:y_to] += mask*scan_cell - Photon_flux = numpy.sum(mask*scan_cell) - image_pixel[ii][jj][0] = Photon_flux + y_scan_time += y_contact_time/Ny_pixel + y_image_pixel += 1 - y_scan_time += y_contact_time/Ny_pixel - y_image_pixel += 1 + # no contact time : y-direction (right-margin) + non_contact_time += y_nocontact_time + y_image_pixel += int(y_nocontact_time/y_exposure_time*Nw_pixel) - # no contact time : y-direction (right-margin) - non_contact_time += y_nocontact_time - y_image_pixel += int(y_nocontact_time/y_exposure_time*Nw_pixel) + z_scan_time += z_contact_time/Nz_pixel + z_image_pixel += 1 - z_scan_time += z_contact_time/Nz_pixel - z_image_pixel += 1 + if (numpy.amax(image_pixel) > 0) : - if (numpy.amax(image_pixel) > 0) : + image = self.detector_output(image_pixel) - image = self.detector_output(image_pixel) + # save data to numpy-binary file + image_file_name = os.path.join(self.configs.image_file_dir, + self.configs.image_file_name_format % (count)) + numpy.save(image_file_name, image) - # save data to numpy-binary file - image_file_name = os.path.join(self.configs.image_file_dir, - self.configs.image_file_name_format % (count)) - numpy.save(image_file_name, image) + # save data to png-image file + #image[:,:,3].astype('uint%d' % (self.configs.ADConverter_bit)) + #toimage(image[:,:,3], low=numpy.amin(image[:,:,3]), high=numpy.amax(image[:,:,3]), mode='I').save(image_file_name) - # save data to png-image file - #image[:,:,3].astype('uint%d' % (self.configs.ADConverter_bit)) - #toimage(image[:,:,3], low=numpy.amin(image[:,:,3]), high=numpy.amax(image[:,:,3]), mode='I').save(image_file_name) + time += exposure_time + count += 1 - time += exposure_time - count += 1 + def prob_analog(self, y, alpha) : - def prob_analog(self, y, alpha) : + # get average gain + A = self.configs.detector_gain - # get average gain - A = self.configs.detector_gain + # get dynodes stages + nu = self.configs.detector_dyn_stages - # get dynodes stages - nu = self.configs.detector_dyn_stages + B = 0.5*(A - 1)/(A**(1.0/nu) - 1) + c = numpy.exp(alpha*(numpy.exp(-A/B) - 1)) - B = 0.5*(A - 1)/(A**(1.0/nu) - 1) - c = numpy.exp(alpha*(numpy.exp(-A/B) - 1)) - - m_y = alpha*A - m_x = m_y/(1 - c) + m_y = alpha*A + m_x = m_y/(1 - c) - s2_y = alpha*(A**2 + 2*A*B) - s2_x = s2_y/(1 - c) - c*m_x**2 + s2_y = alpha*(A**2 + 2*A*B) + s2_x = s2_y/(1 - c) - c*m_x**2 - #if (y < 10*A) : - # Rayleigh approximation - #s2 = (2.0/numpy.pi)*m_x**2 - #prob = y/s2*numpy.exp(-0.5*y**2/s2) + #if (y < 10*A) : + # Rayleigh approximation + #s2 = (2.0/numpy.pi)*m_x**2 + #prob = y/s2*numpy.exp(-0.5*y**2/s2) - # probability distribution - prob = numpy.zeros(shape=(len(y))) + # probability distribution + prob = numpy.zeros(shape=(len(y))) - # get index - k = (numpy.abs(10*A - y)).argmin() + # get index + k = (numpy.abs(10*A - y)).argmin() - # Gamma approximation - k_1 = m_x - k_2 = (m_y**2 + s2_y)/(1 - c) + # Gamma approximation + k_1 = m_x + k_2 = (m_y**2 + s2_y)/(1 - c) - a = 1/(k_1*(k_2/k_1**2 - 1)) - b = a*k_1 + a = 1/(k_1*(k_2/k_1**2 - 1)) + b = a*k_1 - prob[0:k] = a/gamma(b)*(a*y[0:k])**(b-1)*numpy.exp(-a*y[0:k]) + prob[0:k] = a/gamma(b)*(a*y[0:k])**(b-1)*numpy.exp(-a*y[0:k]) - if (k < len(y)) : - # Truncated Gaussian approximation - Q = 0 - beta0 = m_x/numpy.sqrt(s2_x) - beta = beta0 - delta = 0.1*beta0 + if (k < len(y)) : + # Truncated Gaussian approximation + Q = 0 + beta0 = m_x/numpy.sqrt(s2_x) + beta = beta0 + delta = 0.1*beta0 - while (beta < 11*beta0) : - Q += numpy.exp(-0.5*beta**2)/numpy.sqrt(2*numpy.pi)*delta - beta += delta + while (beta < 11*beta0) : + Q += numpy.exp(-0.5*beta**2)/numpy.sqrt(2*numpy.pi)*delta + beta += delta - prob[k:] = numpy.exp(-0.5*(y[k:] - m_x)**2/s2_x)/(numpy.sqrt(2*numpy.pi*s2_x)*(1 - Q)) + prob[k:] = numpy.exp(-0.5*(y[k:] - m_x)**2/s2_x)/(numpy.sqrt(2*numpy.pi*s2_x)*(1 - Q)) - return prob + return prob - #def detector_output(self, cell) : - def detector_output(self, image_pixel) : + #def detector_output(self, cell) : + def detector_output(self, image_pixel) : - # Detector Output - voxel_radius = self.configs.spatiocyte_VoxelRadius - voxel_size = (2.0*voxel_radius)/1e-9 + # Detector Output + voxel_radius = self.configs.spatiocyte_VoxelRadius + voxel_size = (2.0*voxel_radius)/1e-9 - Nw_pixel = self.configs.detector_image_size[0] - Nh_pixel = self.configs.detector_image_size[1] + Nw_pixel = self.configs.detector_image_size[0] + Nh_pixel = self.configs.detector_image_size[1] -# Np = int(self.configs.image_scaling*voxel_size) +# Np = int(self.configs.image_scaling*voxel_size) # # # image in nm-scale -# Nw_image = Nw_pixel*Np -# Nh_image = Nh_pixel*Np +# Nw_image = Nw_pixel*Np +# Nh_image = Nh_pixel*Np # -# Nw_cell = len(cell) -# Nh_cell = len(cell[0]) +# Nw_cell = len(cell) +# Nh_cell = len(cell[0]) # -# if (Nw_image > Nw_cell) : +# if (Nw_image > Nw_cell) : # -# w_cam_from = int((Nw_image - Nw_cell)/2.0) -# w_cam_to = w_cam_from + Nw_cell +# w_cam_from = int((Nw_image - Nw_cell)/2.0) +# w_cam_to = w_cam_from + Nw_cell # w_cel_from = 0 # w_cel_to = Nw_cell # -# else : +# else : # # w_cam_from = 0 # w_cam_to = Nw_image # w_cel_from = int((Nw_cell - Nw_image)/2.0) # w_cel_to = w_cel_from + Nw_image # -# if (Nh_image > Nh_cell) : +# if (Nh_image > Nh_cell) : # # h_cam_from = int((Nh_image - Nh_cell)/2.0) # h_cam_to = h_cam_from + Nh_cell @@ -762,143 +762,141 @@ def detector_output(self, image_pixel) : # h_cel_to = h_cel_from + Nh_image # # -# # image in nm-scale -# plane = cell[w_cel_from:w_cel_to, h_cel_from:h_cel_to] +# # image in nm-scale +# plane = cell[w_cel_from:w_cel_to, h_cel_from:h_cel_to] # -# # convert image in nm-scale to pixel-scale +# # convert image in nm-scale to pixel-scale # cell_pixel = numpy.zeros(shape=(Nw_cell/Np, Nh_cell/Np)) # -# # Signal (photon distribution on cell) -# for i in range(Nw_cell/Np) : -# for j in range(Nh_cell/Np) : +# # Signal (photon distribution on cell) +# for i in range(Nw_cell/Np) : +# for j in range(Nh_cell/Np) : # -# # get photon flux -# Photon_flux = numpy.sum(plane[i*Np:(i+1)*Np, j*Np:(j+1)*Np]) -# cell_pixel[i][j] = Photon_flux +# # get photon flux +# Photon_flux = numpy.sum(plane[i*Np:(i+1)*Np, j*Np:(j+1)*Np]) +# cell_pixel[i][j] = Photon_flux # # # # Background (photon distribution on image) # image_pixel = numpy.zeros([Nw_pixel, Nh_pixel, 2]) # -# w_cam_from = int(w_cam_from/Np) -# w_cam_to = int(w_cam_to/Np) -# h_cam_from = int(h_cam_from/Np) -# h_cam_to = int(h_cam_to/Np) +# w_cam_from = int(w_cam_from/Np) +# w_cam_to = int(w_cam_to/Np) +# h_cam_from = int(h_cam_from/Np) +# h_cam_to = int(h_cam_to/Np) # -# w_cel_from = int(w_cel_from/Np) -# w_cel_to = int(w_cel_to/Np) -# h_cel_from = int(h_cel_from/Np) -# h_cel_to = int(h_cel_to/Np) +# w_cel_from = int(w_cel_from/Np) +# w_cel_to = int(w_cel_to/Np) +# h_cel_from = int(h_cel_from/Np) +# h_cel_to = int(h_cel_to/Np) # -# ddw = (w_cam_to - w_cam_from) - (w_cel_to - w_cel_from) -# ddh = (h_cam_to - h_cam_from) - (h_cel_to - h_cel_from) +# ddw = (w_cam_to - w_cam_from) - (w_cel_to - w_cel_from) +# ddh = (h_cam_to - h_cam_from) - (h_cel_to - h_cel_from) # -# if (ddw > 0) : w_cam_to = w_cam_to - ddw -# elif (ddw < 0) : w_cel_to = w_cel_to - ddw +# if (ddw > 0) : w_cam_to = w_cam_to - ddw +# elif (ddw < 0) : w_cel_to = w_cel_to - ddw # # if (ddh > 0) : h_cam_to = h_cam_to - ddh # elif (ddh < 0) : h_cel_to = h_cel_to - ddh # -# # place cell_pixel data to image image -# image_pixel[w_cam_from:w_cam_to, h_cam_from:h_cam_to, 0] = cell_pixel[w_cel_from:w_cel_to, h_cel_from:h_cel_to] - - # set seed for random number - numpy.random.seed() - - # observational time - T = self.configs.detector_exposure_time/(Nw_pixel*Nh_pixel) +# # place cell_pixel data to image image +# image_pixel[w_cam_from:w_cam_to, h_cam_from:h_cam_to, 0] = cell_pixel[w_cel_from:w_cel_to, h_cel_from:h_cel_to] - # conversion : photon --> photoelectron --> ADC count - for i in range(Nw_pixel) : - for j in range(Nh_pixel) : + # set seed for random number + numpy.random.seed() - # pixel position - pixel = (i, j) + # observational time + T = self.configs.detector_exposure_time/(Nw_pixel*Nh_pixel) - # Detector : Quantum Efficiency - #index = int(self.configs.psf_wavelength) - int(self.configs.wave_length[0]) - QE = self.configs.detector_qeff + # conversion : photon --> photoelectron --> ADC count + for i in range(Nw_pixel) : + for j in range(Nh_pixel) : - # get signal (photon flux and photons) - Flux = image_pixel[i][j][0] + # pixel position + pixel = (i, j) - if (self.configs.detector_mode == "Photon-counting") : - # pair-pulses time resolution (sec) - t_pp = self.configs.detector_pair_pulses - Flux = Flux/(1 + Flux*t_pp) + # Detector : Quantum Efficiency + #index = int(self.configs.psf_wavelength) - int(self.configs.wave_length[0]) + QE = self.configs.detector_qeff - Photons = Flux*T + # get signal (photon flux and photons) + Flux = image_pixel[i][j][0] - # get constant background - if (self.effects.background_switch == True) : + if (self.configs.detector_mode == "Photon-counting") : + # pair-pulses time resolution (sec) + t_pp = self.configs.detector_pair_pulses + Flux = Flux/(1 + Flux*t_pp) - Photons_bg = self.effects.background_mean - Photons += Photons_bg + Photons = Flux*T - # get signal (expectation) - Exp = QE*Photons + # get constant background + if (self.effects.background_switch == True) : - # get dark count - D = self.configs.detector_dark_count - Exp += D*T + Photons_bg = self.effects.background_mean + Photons += Photons_bg - # select Camera type - if (self.configs.detector_mode == "Photon-counting") : + # get signal (expectation) + Exp = QE*Photons - # get signal (poisson distributions) - signal = numpy.random.poisson(Exp, None) + # get dark count + D = self.configs.detector_dark_count + Exp += D*T + # select Camera type + if (self.configs.detector_mode == "Photon-counting") : - if (self.configs.detector_mode == "Analog") : + # get signal (poisson distributions) + signal = numpy.random.poisson(Exp, None) - # get signal (photoelectrons) - if (Exp > 1e-8) : - # get EM gain - G = self.configs.detector_gain + if (self.configs.detector_mode == "Analog") : - # signal array - if (Exp > 1) : sig = numpy.sqrt(Exp) - else : sig = 1 + # get signal (photoelectrons) + if (Exp > 1e-8) : - s_min = int(G*(Exp - 10*sig)) - s_max = int(G*(Exp + 10*sig)) + # get EM gain + G = self.configs.detector_gain - if (s_min < 0) : s_min = 0 + # signal array + if (Exp > 1) : sig = numpy.sqrt(Exp) + else : sig = 1 - delta = (s_max - s_min)/1000. + s_min = int(G*(Exp - 10*sig)) + s_max = int(G*(Exp + 10*sig)) - s = numpy.array([k*delta+s_min for k in range(1000)]) + if (s_min < 0) : s_min = 0 - # probability density fuction - #p_signal = numpy.array(map(lambda y : self.prob_analog(y, Exp), s)) - p_signal = self.prob_analog(s, Exp) - p_ssum = p_signal.sum() + delta = (s_max - s_min)/1000. - # get signal (photoelectrons) - signal = numpy.random.choice(s, None, p=p_signal/p_ssum) - signal = signal/G + s = numpy.array([k*delta+s_min for k in range(1000)]) - else : - signal = 0 + # probability density fuction + #p_signal = numpy.array(map(lambda y : self.prob_analog(y, Exp), s)) + p_signal = self.prob_analog(s, Exp) + p_ssum = p_signal.sum() - # get detector noise (photoelectrons) - Nr = QE*self.configs.detector_readout_noise + # get signal (photoelectrons) + signal = numpy.random.choice(s, None, p=p_signal/p_ssum) + signal = signal/G - if (Nr > 0) : noise = numpy.random.normal(0, Nr, None) - else : noise = 0 + else : + signal = 0 - # A/D converter : Expectation --> ADC counts - #EXP = self.get_ADC_value(pixel, Exp) + # get detector noise (photoelectrons) + Nr = QE*self.configs.detector_readout_noise - # A/D converter : Photoelectrons --> ADC counts - PE = signal + noise - ADC = self.get_ADC_value(pixel, PE) + if (Nr > 0) : noise = numpy.random.normal(0, Nr, None) + else : noise = 0 - # set data in image array - #image_pixel[i][j] = [Photons, Exp, PE, ADC] - image_pixel[i][j] = [Exp, ADC] + # A/D converter : Expectation --> ADC counts + #EXP = self.get_ADC_value(pixel, Exp) - return image_pixel + # A/D converter : Photoelectrons --> ADC counts + PE = signal + noise + ADC = self.get_ADC_value(pixel, PE) + # set data in image array + #image_pixel[i][j] = [Photons, Exp, PE, ADC] + image_pixel[i][j] = [Exp, ADC] + return image_pixel diff --git a/bioimaging/pointscan_confm_script.py b/bioimaging/pointscan_confm_script.py index 7454bec..ed99f8a 100755 --- a/bioimaging/pointscan_confm_script.py +++ b/bioimaging/pointscan_confm_script.py @@ -7,46 +7,45 @@ def test_confm(t0, t1, index=None) : - # create Point-scanning Confocal Microscopy - confm = PointScanConfocalConfigs() + # create Point-scanning Confocal Microscopy + confm = PointScanConfocalConfigs() - confm.set_LightSource(source_type='LASER', wave_length=532, flux=10e-6, radius=400e-9) - #confm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') - confm.set_Fluorophore(fluorophore_type='EGFP') - confm.set_Pinhole(radius=90e-6) - confm.set_Magnification(Mag=60) + confm.set_LightSource(source_type='LASER', wave_length=532, flux=10e-6, radius=400e-9) + #confm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') + confm.set_Fluorophore(fluorophore_type='EGFP') + confm.set_Pinhole(radius=90e-6) + confm.set_Magnification(Mag=60) -# # PMT : Photon-counting mode -# confm.set_Detector(detector='PMT', mode="Photon-counting", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ -# pixel_length=207.16e-9, scan_time=1.15, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) -# confm.set_ADConverter(bit=12, offset=0, fullwell=4096) -# confm.set_OutputData(image_file_dir='./images_erk102_pulses') +# # PMT : Photon-counting mode +# confm.set_Detector(detector='PMT', mode="Photon-counting", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ +# pixel_length=207.16e-9, scan_time=1.15, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) +# confm.set_ADConverter(bit=12, offset=0, fullwell=4096) +# confm.set_OutputData(image_file_dir='./images_erk102_pulses') - # PMT : Anlalog mode - confm.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ - pixel_length=207.16e-9, scan_time=1.15, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) - confm.set_ADConverter(bit=12, offset=0, fullwell=4096) - confm.set_OutputData(image_file_dir='./images') + # PMT : Anlalog mode + confm.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.4,0.5,0.5), \ + pixel_length=207.16e-9, scan_time=1.15, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) + confm.set_ADConverter(bit=12, offset=0, fullwell=4096) + confm.set_OutputData(image_file_dir='./images') - # Input data : EGF model file - confm.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_07', start=t0, end=t1, observable="A") + # Input data : EGF model file + confm.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_07', start=t0, end=t1, observable="A") - # create physical effects - physics = PhysicalEffects() - physics.set_Conversion(ratio=1e-6) - #physics.set_Background(mean=10) - #physics.set_DetectorCrosstalk(width=1.00) + # create physical effects + physics = PhysicalEffects() + physics.set_Conversion(ratio=1e-6) + #physics.set_Background(mean=10) + #physics.set_DetectorCrosstalk(width=1.00) - # create image and movie - create = PointScanConfocalVisualizer(configs=confm, effects=physics) - create.output_frames(num_div=16) + # create image and movie + create = PointScanConfocalVisualizer(configs=confm, effects=physics) + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_confm(t0, t1) + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_confm(t0, t1) diff --git a/bioimaging/pten_lscm_script.py b/bioimaging/pten_lscm_script.py index f920ec9..17ca00c 100755 --- a/bioimaging/pten_lscm_script.py +++ b/bioimaging/pten_lscm_script.py @@ -7,74 +7,73 @@ def pten_confm(t0, t1) : - # create Point-scanning Confocal Microscopy - confm = PointScanConfocalConfigs() + # create Point-scanning Confocal Microscopy + confm = PointScanConfocalConfigs() - confm.set_LightSource(source_type='LASER', wave_length=561, flux=100e-6, radius=200e-9) - confm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') - confm.set_Pinhole(radius=20.0e-6) - confm.set_Magnification(Mag=60) + confm.set_LightSource(source_type='LASER', wave_length=561, flux=100e-6, radius=200e-9) + confm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') + confm.set_Pinhole(radius=20.0e-6) + confm.set_Magnification(Mag=60) - # PMT : Anlalog mode - confm.set_Detector(detector='PMT', mode="Analog", image_size=(512,512), focal_point=(0.4,0.5,0.5), - pixel_length=414.3e-9, scan_time=5.00, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) - confm.set_ADConverter(bit=12, offset=0, fullwell=4096) - confm.set_OutputData(image_file_dir='./images_pten') + # PMT : Anlalog mode + confm.set_Detector(detector='PMT', mode="Analog", image_size=(512,512), focal_point=(0.4,0.5,0.5), + pixel_length=414.3e-9, scan_time=5.00, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) + confm.set_ADConverter(bit=12, offset=0, fullwell=4096) + confm.set_OutputData(image_file_dir='./images_pten') - # Input data : EGF model file - confm.set_InputData('./data/csv/pten', start=t0, end=t1, observable="PTEN") + # Input data : EGF model file + confm.set_InputData('./data/csv/pten', start=t0, end=t1, observable="PTEN") - # create physical effects - physics = PhysicalEffects() - physics.set_Conversion(ratio=1e-6) - #physics.set_Background(mean=20) - #physics.set_DetectorCrosstalk(width=1.00) + # create physical effects + physics = PhysicalEffects() + physics.set_Conversion(ratio=1e-6) + #physics.set_Background(mean=20) + #physics.set_DetectorCrosstalk(width=1.00) - # create image and movie - create = PointScanConfocalVisualizer(configs=confm, effects=physics) - create.output_frames(num_div=16) + # create image and movie + create = PointScanConfocalVisualizer(configs=confm, effects=physics) + create.output_frames(num_div=16) def pip3_confm(t0, t1) : - # create Point-scanning Confocal Microscopy - confm = PointScanConfocalConfigs() + # create Point-scanning Confocal Microscopy + confm = PointScanConfocalConfigs() - confm.set_LightSource(source_type='LASER', wave_length=488, flux=100e-6, radius=200e-9) - confm.set_Fluorophore(fluorophore_type='EGFP') - confm.set_Pinhole(radius=20e-6) - confm.set_Magnification(Mag=60) + confm.set_LightSource(source_type='LASER', wave_length=488, flux=100e-6, radius=200e-9) + confm.set_Fluorophore(fluorophore_type='EGFP') + confm.set_Pinhole(radius=20e-6) + confm.set_Magnification(Mag=60) - # PMT : Anlalog mode - confm.set_Detector(detector='PMT', mode="Analog", image_size=(512,512), focal_point=(0.4,0.5,0.5), - pixel_length=414.3e-9, scan_time=5.00, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) - confm.set_ADConverter(bit=12, offset=0, fullwell=4096) - confm.set_OutputData(image_file_dir='./images_pip3') + # PMT : Anlalog mode + confm.set_Detector(detector='PMT', mode="Analog", image_size=(512,512), focal_point=(0.4,0.5,0.5), + pixel_length=414.3e-9, scan_time=5.00, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) + confm.set_ADConverter(bit=12, offset=0, fullwell=4096) + confm.set_OutputData(image_file_dir='./images_pip3') - # Input data : EGF model file - confm.set_InputData('./data/pten', start=t0, end=t1, observable="PIP3") + # Input data : EGF model file + confm.set_InputData('./data/pten', start=t0, end=t1, observable="PIP3") - # create physical effects - physics = PhysicalEffects() - physics.set_Conversion(ratio=1e-6) - #physics.set_Background(mean=20) - #physics.set_DetectorCrosstalk(width=1.00) + # create physical effects + physics = PhysicalEffects() + physics.set_Conversion(ratio=1e-6) + #physics.set_Background(mean=20) + #physics.set_DetectorCrosstalk(width=1.00) - # create image and movie - create = PointScanConfocalVisualizer(configs=confm, effects=physics) - create.output_frames(num_div=16) + # create image and movie + create = PointScanConfocalVisualizer(configs=confm, effects=physics) + create.output_frames(num_div=16) if __name__ == "__main__": - index = sys.argv[1] - t0 = float(sys.argv[2]) - t1 = float(sys.argv[3]) - - if (index == "PTEN") : - pten_confm(t0, t1) - elif (index == "PIP3") : - pip3_confm(t0, t1) + index = sys.argv[1] + t0 = float(sys.argv[2]) + t1 = float(sys.argv[3]) + if (index == "PTEN") : + pten_confm(t0, t1) + elif (index == "PIP3") : + pip3_confm(t0, t1) diff --git a/bioimaging/tirfm_handler.py b/bioimaging/tirfm_handler.py index ad89d4f..456be12 100755 --- a/bioimaging/tirfm_handler.py +++ b/bioimaging/tirfm_handler.py @@ -27,9 +27,9 @@ class TIRFMConfigs(EPIFMConfigs) : TIRFM configration - Evanescent Field - + - Detector : EMCCD/CMOS + Evanescent Field + + + Detector : EMCCD/CMOS ''' def __init__(self, user_configs_dict = None): @@ -60,31 +60,28 @@ def __init__(self, user_configs_dict = None): class TIRFMVisualizer(EPIFMVisualizer) : - ''' - TIRFM visualization class - ''' - - def __init__(self, configs=TIRFMConfigs(), effects=PhysicalEffects()) : - - assert isinstance(configs, TIRFMConfigs) - self.configs = configs - - assert isinstance(effects, PhysicalEffects) - self.effects = effects + ''' + TIRFM visualization class + ''' - """ - Check and create the folder for image file. - """ - if not os.path.exists(self.configs.image_file_dir): - os.makedirs(self.configs.image_file_dir) - #else: - # for file in os.listdir(self.configs.movie_image_file_dir): - # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) + def __init__(self, configs=TIRFMConfigs(), effects=PhysicalEffects()) : - """ - set optical path from source to detector - """ - self.configs.set_Optical_path() + assert isinstance(configs, TIRFMConfigs) + self.configs = configs + assert isinstance(effects, PhysicalEffects) + self.effects = effects + """ + Check and create the folder for image file. + """ + if not os.path.exists(self.configs.image_file_dir): + os.makedirs(self.configs.image_file_dir) + #else: + # for file in os.listdir(self.configs.movie_image_file_dir): + # os.remove(os.path.join(self.configs.movie_image_file_dir, file)) + """ + set optical path from source to detector + """ + self.configs.set_Optical_path() diff --git a/bioimaging/tirfm_rewrite_script.py b/bioimaging/tirfm_rewrite_script.py index 415439a..3cdb278 100755 --- a/bioimaging/tirfm_rewrite_script.py +++ b/bioimaging/tirfm_rewrite_script.py @@ -7,50 +7,48 @@ def test_tirfm(t0, t1) : - # create TIRFM imaging - tirfm = TIRFMConfigs() - tirfm.set_LightSource(source_type='LASER', wave_length=561, flux_density=20, angle=65.7) - tirfm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') - tirfm.set_DichroicMirror('FF562-Di03-25x36') - tirfm.set_EmissionFilter('FF01-593_40-25') - tirfm.set_Magnification(Mag=250) + # create TIRFM imaging + tirfm = TIRFMConfigs() + tirfm.set_LightSource(source_type='LASER', wave_length=561, flux_density=20, angle=65.7) + tirfm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') + tirfm.set_DichroicMirror('FF562-Di03-25x36') + tirfm.set_EmissionFilter('FF01-593_40-25') + tirfm.set_Magnification(Mag=250) -# # Detector : CMOS Camera -# tirfm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ -# focal_point=(0.0,0.5,0.5), exposure_time=0.5, QE=0.73) -# tirfm.set_ADConverter(bit=16, offset=100, fullwell=30000) +# # Detector : CMOS Camera +# tirfm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ +# focal_point=(0.0,0.5,0.5), exposure_time=0.5, QE=0.73) +# tirfm.set_ADConverter(bit=16, offset=100, fullwell=30000) - # Detector : EMCCD Camera - tirfm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, \ - focal_point=(0.0,0.5,0.5), exposure_time=0.1, QE=0.92, readout_noise=100, emgain=300) - tirfm.set_ADConverter(bit=16, offset=2000, fullwell=800000) + # Detector : EMCCD Camera + tirfm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, \ + focal_point=(0.0,0.5,0.5), exposure_time=0.1, QE=0.92, readout_noise=100, emgain=300) + tirfm.set_ADConverter(bit=16, offset=2000, fullwell=800000) -# # Output data -# tirfm.set_OutputData(image_file_dir='./numpys_tirfm_01000A') +# # Output data +# tirfm.set_OutputData(image_file_dir='./numpys_tirfm_01000A') - # Input data - tirfm.reset_InputData('/home/masaki/wrk/spatiocyte/models/Hiroshima/data_HRG-ErbB/SM/csv/TEST', start=t0, end=t1, observable="R") + # Input data + tirfm.reset_InputData('/home/masaki/wrk/spatiocyte/models/Hiroshima/data_HRG-ErbB/SM/csv/TEST', start=t0, end=t1, observable="R") - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0) - physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) - physics.set_photobleaching(tau0=1.8, alpha=0.73) - #physics.set_photoblinking(t0_on=1.00, a_on=0.58, t0_off=10e-6, a_off=0.48) + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0) + physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) + physics.set_photobleaching(tau0=1.8, alpha=0.73) + #physics.set_photoblinking(t0_on=1.00, a_on=0.58, t0_off=10e-6, a_off=0.48) - # create image - create = TIRFMVisualizer(configs=tirfm, effects=physics) - create.rewrite_InputData(output_file_dir='./data_tirfm_TEST_bleach') - #create.rewrite_InputData(output_file_dir='./data_tirfm_TEST_blink') - #create.output_frames(num_div=16) + # create image + create = TIRFMVisualizer(configs=tirfm, effects=physics) + create.rewrite_InputData(output_file_dir='./data_tirfm_TEST_bleach') + #create.rewrite_InputData(output_file_dir='./data_tirfm_TEST_blink') + #create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_tirfm(t0, t1) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_tirfm(t0, t1) diff --git a/bioimaging/tirfm_script.py b/bioimaging/tirfm_script.py index 2adceea..0a9e39e 100755 --- a/bioimaging/tirfm_script.py +++ b/bioimaging/tirfm_script.py @@ -7,51 +7,49 @@ def test_tirfm(t0, t1) : - # create TIRFM imaging - tirfm = TIRFMConfigs() - tirfm.set_LightSource(source_type='LASER', wave_length=561, flux_density=20, angle=65.7) - tirfm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') - tirfm.set_DichroicMirror('FF562-Di03-25x36') - tirfm.set_EmissionFilter('FF01-593_40-25') - tirfm.set_Magnification(Mag=250) - -# # Detector : CMOS Camera -# tirfm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ -# focal_point=(0.0,0.5,0.5), exposure_time=0.5, QE=0.73) -# tirfm.set_ADConverter(bit=16, offset=100, fullwell=30000) - - # Detector : EMCCD Camera - tirfm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, \ - focal_point=(0.0,0.5,0.5), exposure_time=0.10, QE=0.92, readout_noise=100, emgain=300) - tirfm.set_ADConverter(bit=16, offset=2000, fullwell=800000) - - # Output data - #tirfm.set_OutputData(image_file_dir='./numpys_tirfm_TEST') - tirfm.set_OutputData(image_file_dir='./numpys_tirfm_TEST_bleach') - - # Input data - #tirfm.set_InputData('/home/masaki/wrk/spatiocyte/models/Hiroshima/data_HRG-ErbB/SM/csv/TEST', start=t0, end=t1, observable="R") - tirfm.set_InputData('/home/masaki/bioimaging_4public/data_tirfm_TEST_bleach', start=t0, end=t1, observable="R") - - # create physical effects - physics = PhysicalEffects() - physics.set_background(mean=0) - physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) - #physics.set_photobleaching(tau0=1.8, alpha=0.73) - #physics.set_photoblinking(t0_on=1.00, a_on=0.58, t0_off=10e-6, a_off=0.48) - - # create image - create = TIRFMVisualizer(configs=tirfm, effects=physics) - #create.rewrite_InputData(output_file_dir='./data_tirfm_01000A') - create.output_frames(num_div=16) + # create TIRFM imaging + tirfm = TIRFMConfigs() + tirfm.set_LightSource(source_type='LASER', wave_length=561, flux_density=20, angle=65.7) + tirfm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') + tirfm.set_DichroicMirror('FF562-Di03-25x36') + tirfm.set_EmissionFilter('FF01-593_40-25') + tirfm.set_Magnification(Mag=250) + +# # Detector : CMOS Camera +# tirfm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ +# focal_point=(0.0,0.5,0.5), exposure_time=0.5, QE=0.73) +# tirfm.set_ADConverter(bit=16, offset=100, fullwell=30000) + + # Detector : EMCCD Camera + tirfm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, \ + focal_point=(0.0,0.5,0.5), exposure_time=0.10, QE=0.92, readout_noise=100, emgain=300) + tirfm.set_ADConverter(bit=16, offset=2000, fullwell=800000) + + # Output data + #tirfm.set_OutputData(image_file_dir='./numpys_tirfm_TEST') + tirfm.set_OutputData(image_file_dir='./numpys_tirfm_TEST_bleach') + + # Input data + #tirfm.set_InputData('/home/masaki/wrk/spatiocyte/models/Hiroshima/data_HRG-ErbB/SM/csv/TEST', start=t0, end=t1, observable="R") + tirfm.set_InputData('/home/masaki/bioimaging_4public/data_tirfm_TEST_bleach', start=t0, end=t1, observable="R") + + # create physical effects + physics = PhysicalEffects() + physics.set_background(mean=0) + physics.set_fluorescence(quantum_yield=0.61, abs_coefficient=83400) + #physics.set_photobleaching(tau0=1.8, alpha=0.73) + #physics.set_photoblinking(t0_on=1.00, a_on=0.58, t0_off=10e-6, a_off=0.48) + + # create image + create = TIRFMVisualizer(configs=tirfm, effects=physics) + #create.rewrite_InputData(output_file_dir='./data_tirfm_01000A') + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_tirfm(t0, t1) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_tirfm(t0, t1) diff --git a/bioimaging/tmr_lscm_script.py b/bioimaging/tmr_lscm_script.py index 41c06c9..49e8070 100755 --- a/bioimaging/tmr_lscm_script.py +++ b/bioimaging/tmr_lscm_script.py @@ -7,49 +7,48 @@ def test_confm(t0, t1, beam) : - # create Point-scanning Confocal Microscopy - confm = PointScanConfocalConfigs() + # create Point-scanning Confocal Microscopy + confm = PointScanConfocalConfigs() - confm.set_LightSource(source_type='LASER', wave_length=488, flux=beam*1e-6, radius=200e-9) - #confm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') - confm.set_Fluorophore(fluorophore_type='EGFP') - confm.set_Pinhole(radius=28.8e-6) - confm.set_Magnification(Mag=60) + confm.set_LightSource(source_type='LASER', wave_length=488, flux=beam*1e-6, radius=200e-9) + #confm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') + confm.set_Fluorophore(fluorophore_type='EGFP') + confm.set_Pinhole(radius=28.8e-6) + confm.set_Magnification(Mag=60) -# # PMT : Photon-counting mode -# confm.set_Detector(detector='PMT', mode="Photon-counting", image_size=(1024,1024), focal_point=(0.3,0.5,0.5), \ -# pixel_length=210e-9, scan_time=1.00, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) -# confm.set_ADConverter(bit=12, offset=0, fullwell=4096) +# # PMT : Photon-counting mode +# confm.set_Detector(detector='PMT', mode="Photon-counting", image_size=(1024,1024), focal_point=(0.3,0.5,0.5), \ +# pixel_length=210e-9, scan_time=1.00, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) +# confm.set_ADConverter(bit=12, offset=0, fullwell=4096) - # PMT : Anlalog mode - confm.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.3,0.5,0.5), \ - pixel_length=210e-9, scan_time=1.00, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) - confm.set_ADConverter(bit=12, offset=0, fullwell=4096) + # PMT : Anlalog mode + confm.set_Detector(detector='PMT', mode="Analog", image_size=(1024,1024), focal_point=(0.3,0.5,0.5), \ + pixel_length=210e-9, scan_time=1.00, gain=1e+6, dyn_stages=11, pair_pulses=18e-9) + confm.set_ADConverter(bit=12, offset=0, fullwell=4096) - ### Output data - #confm.set_OutputData(image_file_dir='./images_tmr_lscm') - confm.set_OutputData(image_file_dir='./images_lscm_test_%03duW' % (beam)) + ### Output data + #confm.set_OutputData(image_file_dir='./images_tmr_lscm') + confm.set_OutputData(image_file_dir='./images_lscm_test_%03duW' % (beam)) - ### Input data - #confm.set_InputData('./data/tmr_lscm', start=t0, end=t1, observable="A") - confm.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_07', start=t0, end=t1, observable="A") + ### Input data + #confm.set_InputData('./data/tmr_lscm', start=t0, end=t1, observable="A") + confm.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_07', start=t0, end=t1, observable="A") - # create physical effects - physics = PhysicalEffects() - physics.set_Conversion(ratio=1e-6) - #physics.set_Background(mean=3) + # create physical effects + physics = PhysicalEffects() + physics.set_Conversion(ratio=1e-6) + #physics.set_Background(mean=3) - # create image and movie - create = PointScanConfocalVisualizer(configs=confm, effects=physics) - create.output_frames(num_div=16) + # create image and movie + create = PointScanConfocalVisualizer(configs=confm, effects=physics) + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - beam = float(sys.argv[3]) - - test_confm(t0, t1, beam) + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + beam = float(sys.argv[3]) + test_confm(t0, t1, beam) diff --git a/bioimaging/tmr_tirfm_script.py b/bioimaging/tmr_tirfm_script.py index 3fa3bc1..9520ecb 100755 --- a/bioimaging/tmr_tirfm_script.py +++ b/bioimaging/tmr_tirfm_script.py @@ -8,54 +8,52 @@ def test_tirfm(t0, t1, beam) : - # create TIRF Microscopy - tirfm = TIRFMConfigs() - tirfm.set_LightSource(source_type='LASER', wave_length=532, flux_density=beam, angle=65.7) - tirfm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') - #tirfm.set_Fluorophore(fluorophore_type='EGFP') - #tirfm.set_Fluorophore(fluorophore_type='Gaussian', wave_length=532, intensity=0.55, width=(200,400)) - tirfm.set_DichroicMirror('FF562-Di03-25x36') - tirfm.set_EmissionFilter('FF01-593_40-25') - #tirfm.set_Magnification(Mag=250) - #tirfm.set_Magnification(Mag=100) - tirfm.set_Magnification(Mag=198) - #tirfm.set_Magnification(Mag=80) - -# # Detector : CMOS Camera -# tirfm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ -# focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.73) -# tirfm.set_ADConverter(bit=16, offset=100, fullwell=30000) - # Detector : EMCCD Camera - tirfm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, \ - focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.92, readout_noise=100, emgain=300) - tirfm.set_ADConverter(bit=16, offset=100, fullwell=180000) - - ### Output data - tirfm.set_OutputData(image_file_dir='./images_tirfm_test_%02dw' % (beam)) - - ### Input data - #tirfm.set_InputData('./data/tmr_tirfm/', start=t0, end=t1, observable="A") - tirfm.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_00', start=t0, end=t1, observable="A") - - # create physical effects - physics = PhysicalEffects() - physics.set_Conversion(ratio=1e-6) - #physics.set_DetectorCrosstalk(width=0.1) - physics.set_Background(mean=1.0) - - # create image and movie - create = TIRFMVisualizer(configs=tirfm, effects=physics) - create.output_frames(num_div=16) + # create TIRF Microscopy + tirfm = TIRFMConfigs() + tirfm.set_LightSource(source_type='LASER', wave_length=532, flux_density=beam, angle=65.7) + tirfm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') + #tirfm.set_Fluorophore(fluorophore_type='EGFP') + #tirfm.set_Fluorophore(fluorophore_type='Gaussian', wave_length=532, intensity=0.55, width=(200,400)) + tirfm.set_DichroicMirror('FF562-Di03-25x36') + tirfm.set_EmissionFilter('FF01-593_40-25') + #tirfm.set_Magnification(Mag=250) + #tirfm.set_Magnification(Mag=100) + tirfm.set_Magnification(Mag=198) + #tirfm.set_Magnification(Mag=80) + +# # Detector : CMOS Camera +# tirfm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ +# focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.73) +# tirfm.set_ADConverter(bit=16, offset=100, fullwell=30000) + # Detector : EMCCD Camera + tirfm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, \ + focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.92, readout_noise=100, emgain=300) + tirfm.set_ADConverter(bit=16, offset=100, fullwell=180000) + + ### Output data + tirfm.set_OutputData(image_file_dir='./images_tirfm_test_%02dw' % (beam)) + + ### Input data + #tirfm.set_InputData('./data/tmr_tirfm/', start=t0, end=t1, observable="A") + tirfm.set_InputData('/home/masaki/ecell3/latest/data/csv/beads_00', start=t0, end=t1, observable="A") + + # create physical effects + physics = PhysicalEffects() + physics.set_Conversion(ratio=1e-6) + #physics.set_DetectorCrosstalk(width=0.1) + physics.set_Background(mean=1.0) + + # create image and movie + create = TIRFMVisualizer(configs=tirfm, effects=physics) + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - beam = int(sys.argv[3]) - - #test_tirfm(t0, t1) - test_tirfm(t0, t1, beam) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + beam = int(sys.argv[3]) + #test_tirfm(t0, t1) + test_tirfm(t0, t1, beam) diff --git a/bioimaging/twostate_tirfm_script.py b/bioimaging/twostate_tirfm_script.py index e4e2da8..5cd177f 100755 --- a/bioimaging/twostate_tirfm_script.py +++ b/bioimaging/twostate_tirfm_script.py @@ -8,49 +8,47 @@ def test_tirfm(t0, t1) : - # create TIRF Microscopy - tirfm = TIRFMConfigs() - tirfm.set_LightSource(source_type='LASER', wave_length=532, flux_density=10, angle=65.7) - tirfm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') - tirfm.set_DichroicMirror('FF562-Di03-25x36') - tirfm.set_EmissionFilter('FF01-593_40-25') - #tirfm.set_Magnification(Mag=100) - tirfm.set_Magnification(Mag=250) - -# # Detector : CMOS Camera -# tirfm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ -# focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.73) -# tirfm.set_ADConverter(bit=16, offset=100, fullwell=30000) - # Detector : EMCCD Camera - tirfm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, \ - focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.92, readout_noise=100, emgain=300) - tirfm.set_ADConverter(bit=16, offset=2000, fullwell=370000) - - ### Output data - #tifm.set_OutputData(image_file_dir='./images') - #tirfm.set_OutputData(image_file_dir='./images_twostate_model_cmos') - tirfm.set_OutputData(image_file_dir='./images_twostate_model_emccd') - - ### Input data - tirfm.set_InputData('/home/masaki/ecell3/latest/data/csv/twostate_model', start=t0, end=t1) - - # create physical effects - physics = PhysicalEffects() - physics.set_Conversion(ratio=1e-6) - physics.set_Background(mean=0.1) - #physics.set_DetectorCrosstalk(width=1.00) - - # create image and movie - create = TIRFMVisualizer(configs=tirfm, effects=physics) - create.output_frames(num_div=16) + # create TIRF Microscopy + tirfm = TIRFMConfigs() + tirfm.set_LightSource(source_type='LASER', wave_length=532, flux_density=10, angle=65.7) + tirfm.set_Fluorophore(fluorophore_type='Tetramethylrhodamine(TRITC)') + tirfm.set_DichroicMirror('FF562-Di03-25x36') + tirfm.set_EmissionFilter('FF01-593_40-25') + #tirfm.set_Magnification(Mag=100) + tirfm.set_Magnification(Mag=250) + +# # Detector : CMOS Camera +# tirfm.set_Detector(detector='CMOS', image_size=(600,600), pixel_length=6.5e-6, \ +# focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.73) +# tirfm.set_ADConverter(bit=16, offset=100, fullwell=30000) + # Detector : EMCCD Camera + tirfm.set_Detector(detector='EMCCD', image_size=(512,512), pixel_length=16e-6, \ + focal_point=(0.0,0.5,0.5), exposure_time=30e-3, QE=0.92, readout_noise=100, emgain=300) + tirfm.set_ADConverter(bit=16, offset=2000, fullwell=370000) + + ### Output data + #tifm.set_OutputData(image_file_dir='./images') + #tirfm.set_OutputData(image_file_dir='./images_twostate_model_cmos') + tirfm.set_OutputData(image_file_dir='./images_twostate_model_emccd') + + ### Input data + tirfm.set_InputData('/home/masaki/ecell3/latest/data/csv/twostate_model', start=t0, end=t1) + + # create physical effects + physics = PhysicalEffects() + physics.set_Conversion(ratio=1e-6) + physics.set_Background(mean=0.1) + #physics.set_DetectorCrosstalk(width=1.00) + + # create image and movie + create = TIRFMVisualizer(configs=tirfm, effects=physics) + create.output_frames(num_div=16) if __name__ == "__main__": - t0 = float(sys.argv[1]) - t1 = float(sys.argv[2]) - - test_tirfm(t0, t1) - + t0 = float(sys.argv[1]) + t1 = float(sys.argv[2]) + test_tirfm(t0, t1)