Copy import numpy
import mfs
from matplotlib import pyplot
def mfs_example(
cmap='hot',
savebin=False,
savetif=False,
savevol=False,
plot=True,
**kwargs,
):
args = {
'shape': (512, 512),
'dims': (5.0, 5.0),
'ex_wavelen': 488.0,
'em_wavelen': 520.0,
'num_aperture': 1.2,
'refr_index': 1.333,
'magnification': 1.0,
'pinhole_radius': 0.05,
'pinhole_shape': 'square',
}
args.update(kwargs)
obsvol = mfs.mfs(mfs.ISOTROPIC | mfs.CONFOCAL, **args)
exmfs = obsvol.exmfs
emmfs = obsvol.emmfs
gauss = gauss2 = mfs.mfs(
mfs.GAUSSIAN | mfs.EXCITATION, **args
)
assert exmfs is not None
assert emmfs is not None
print(exmfs)
print(emmfs)
print(obsvol)
print(gauss)
print(gauss2)
if savebin:
emmfs.data.tofile('emmfs.bin')
exmfs.data.tofile('exmfs.bin')
gauss.data.tofile('gauss.bin')
obsvol.data.tofile('obsvol.bin')
if savetif:
from tifffile import imwrite
imwrite('emmfs.tif', emmfs.data)
imwrite('exmfs.tif', exmfs.data)
imwrite('gauss.tif', gauss.data)
imwrite('obsvol.tif', obsvol.data)
if savevol:
from tifffile import imwrite
imwrite('emmfs_vol.tif', emmfs.volume())
imwrite('exmfs_vol.tif', exmfs.volume())
imwrite('gauss_vol.tif', gauss.volume())
imwrite('obsvol_vol.tif', obsvol.volume())
if not plot:
return
pyplot.rc('font', family='sans-serif', weight='normal')
pyplot.figure(
dpi=96, figsize=(9.5, 5.0), frameon=True, facecolor='w', edgecolor='w'
)
pyplot.subplots_adjust(
bottom=0.02, top=0.92, left=0.02, right=0.98, hspace=0.01, wspace=0.01
)
ax = exmfs.imshow(241, cmap=cmap)[0]
emmfs.imshow(242, sharex=ax, sharey=ax, cmap=cmap)
obsvol.imshow(243, sharex=ax, sharey=ax, cmap=cmap)
gauss.imshow(244, sharex=ax, sharey=ax, cmap=cmap)
i = 0
mfs.imshow(245, data=exmfs.slice(i), sharex=ax, cmap=cmap)
mfs.imshow(246, data=emmfs.slice(i), sharex=ax, cmap=cmap)
mfs.imshow(247, data=obsvol.slice(i), sharex=ax, cmap=cmap)
mfs.imshow(248, data=gauss.slice(i), sharex=ax, cmap=cmap)
z = numpy.arange(0, gauss.dims.ou[0], gauss.dims.ou[0] / gauss.dims.px[0])
r = numpy.arange(0, gauss.dims.ou[1], gauss.dims.ou[1] / gauss.dims.px[1])
zr_max = 20.0
pyplot.figure()
pyplot.subplot(211)
pyplot.title('mfs cross sections')
pyplot.plot(r, exmfs[0], 'r-', label=exmfs.name + ' (r)')
pyplot.plot(r, gauss2[0], 'r:', label='')
pyplot.plot(r, obsvol[0], 'b-', label=obsvol.name + ' (r)')
pyplot.plot(r, gauss[0], 'b:', label="")
pyplot.plot(z, exmfs[:, 0], 'm-', label=exmfs.name + ' (z)')
pyplot.plot(z, gauss2[:, 0], 'm:', label='')
pyplot.plot(z, obsvol[:, 0], 'c-', label=obsvol.name + ' (z)')
pyplot.plot(z, gauss[:, 0], 'c:', label='')
pyplot.legend()
pyplot.axis([0, zr_max, 0, 1])
pyplot.subplot(212)
pyplot.title('Residuals of gaussian approximation')
pyplot.plot(r, exmfs[0] - gauss2[0], 'r-', label=exmfs.name + ' (r)')
pyplot.plot(r, obsvol[0] - gauss[0], 'b-', label=obsvol.name + ' (r)')
pyplot.plot(z, exmfs[:, 0] - gauss2[:, 0], 'm-', label=exmfs.name + ' (z)')
pyplot.plot(
z, obsvol[:, 0] - gauss[:, 0], 'c-', label=obsvol.name + ' (z)'
)
pyplot.axis([0, zr_max, -0.25, 0.25])
pyplot.tight_layout()
pyplot.show()
if __name__ == '__main__':
mfs_example()