Example #1
0
def plot_ASF_ATF(savepath, myATF, myASF):
    #% Write ASF and ATF to disk
    plt.figure()
    plt.subplot(331), plt.title('myATF'), plt.imshow(
        np.abs(myATF)[:, myATF.shape[1] // 2, :],
        norm=LogNorm()), plt.colorbar()  #, plt.show()
    plt.subplot(332), plt.title('myATF'), plt.imshow(
        np.abs(myATF)[myATF.shape[0] // 2, :, :],
        norm=LogNorm()), plt.colorbar()  #, plt.show()
    plt.subplot(333), plt.title('myATF'), plt.imshow(
        np.abs(myATF)[:, :, myATF.shape[2] // 2],
        norm=LogNorm()), plt.colorbar()  #, plt.show()
    plt.subplot(334), plt.title('real myASF'), plt.imshow(
        np.real(myASF)[:,
                       myASF.shape[1] // 2, :]), plt.colorbar()  #, plt.show()
    plt.subplot(335), plt.title('real myASF'), plt.imshow(
        np.real(myASF)[myASF.shape[0] //
                       2, :, :]), plt.colorbar()  #, plt.show()
    plt.subplot(336), plt.title('real myASF'), plt.imshow(
        np.real(myASF)[:, :,
                       myASF.shape[2] // 2]), plt.colorbar()  #, plt.show()
    plt.subplot(337), plt.title('imag myASF'), plt.imshow(
        np.imag(myASF)[:,
                       myASF.shape[1] // 2, :]), plt.colorbar()  #, plt.show()
    plt.subplot(338), plt.title('imag myASF'), plt.imshow(
        np.imag(myASF)[myASF.shape[0] //
                       2, :, :]), plt.colorbar()  #, plt.show()
    plt.subplot(339), plt.title('imag myASF'), plt.imshow(
        np.imag(myASF)[:, :,
                       myASF.shape[2] // 2]), plt.colorbar()  #, plt.show()
    plt.savefig(savepath + '/ASFATF.png'), plt.show()
    data.export_realdatastack_h5(
        savepath + '/myasf.h5', 'real, imag',
        np.stack((np.real(myASF), np.imag(myASF)), axis=0))
Example #2
0
def plot_obj_fft(savepath, myObj):
    #% Write obj and spectrum to disk
    plt.figure()
    myobjft = np.fft.fftshift(np.fft.fftn(myObj))
    plt.subplot(331), plt.title('abs FT obj'), plt.imshow(
        np.abs(myobjft)[:, myobjft.shape[1] // 2, :],
        norm=LogNorm()), plt.colorbar()  #, plt.show()
    plt.subplot(332), plt.title('abs FT obj'), plt.imshow(
        np.abs(myobjft)[myobjft.shape[0] // 2, :, :],
        norm=LogNorm()), plt.colorbar()  #, plt.show()
    plt.subplot(333), plt.title('abs FT obj'), plt.imshow(
        np.abs(myobjft)[:, :, myobjft.shape[2] // 2],
        norm=LogNorm()), plt.colorbar()  #, plt.show()
    plt.subplot(334), plt.title('real obj'), plt.imshow(
        np.real(((myObj)))[:, myobjft.shape[1] //
                           2, :]), plt.colorbar()  #, plt.show()
    plt.subplot(335), plt.title('real obj'), plt.imshow(
        np.real(((myObj)))[myobjft.shape[0] //
                           2, :, :]), plt.colorbar()  #, plt.show()
    plt.subplot(336), plt.title('real obj'), plt.imshow(
        np.real(
            ((myObj)))[:, :,
                       myobjft.shape[2] // 2]), plt.colorbar()  #, plt.show()
    plt.subplot(337), plt.title('imag obj'), plt.imshow(
        np.imag(((myObj)))[:, myobjft.shape[1] //
                           2, :]), plt.colorbar()  #, plt.show()
    plt.subplot(338), plt.title('imag obj'), plt.imshow(
        np.imag(((myObj)))[myobjft.shape[0] //
                           2, :, :]), plt.colorbar()  #, plt.show()
    plt.subplot(339), plt.title('imag obj'), plt.imshow(
        np.imag(
            ((myObj)))[:, :,
                       myobjft.shape[2] // 2]), plt.colorbar()  #, plt.show()
    plt.savefig(savepath + '/ATF_Support.png'), plt.show()
    data.export_realdatastack_h5(
        savepath + '/myobjspectrum.h5', 'real, imag',
        np.stack((np.abs(myobjft) / np.max(np.abs(myobjft)),
                  np.abs(myObj) / np.max(np.abs(myObj))),
                 axis=0))
                                myfwdlist,
                                mylosslist,
                                myfidelitylist,
                                myneglosslist,
                                mytvlosslist,
                                result_phaselist,
                                result_absorptionlist,
                                globareallist,
                                globalimaglist,
                                np_meas,
                                figsuffix='FINAL')

        data.export_realdatastack_h5(
            savepath + '/myrefractiveindex.h5', 'phase, abs',
            np.stack(((nip.extract(result_phaselist[-1], muscat.mysize, None,
                                   None)),
                      (nip.extract(result_absorptionlist[-1], muscat.mysize,
                                   None, None))),
                     axis=0))
        data.export_realdatastack_h5(
            savepath + '/mymeas.h5', 'real, imag',
            np.stack((np.real(np_meas), np.imag(np_meas)), axis=0))

        print('Zernikes: ' + str(np.real(sess.run(muscat.TF_zernikefactors))))
        print('ShiftX/Y: ' + str(sess.run(muscat.TF_shiftIcX)) + ' / ' +
              str(sess.run(muscat.TF_shiftIcY)))

        #nip.v5(nip.cat(np.stack((np.flip(nip.extract(result_phaselist[-1], muscat.mysize,None,None),0),np.real(np_meas), np.imag(np_meas)), axis=0)))
        nip.v5(
            nip.cat(
                np.stack(
    iter_last = iterx
#%%
''' Save Figures and Parameters '''
muscat.saveFigures_list(savepath,
                        myfwdlist,
                        mylosslist,
                        myfidelitylist,
                        myneglosslist,
                        mytvlosslist,
                        result_phaselist,
                        result_absorptionlist,
                        globalphaselist,
                        globalabslist,
                        np_meas,
                        figsuffix='FINAL')

data.export_realdatastack_h5(savepath + '/myrefractiveindex.h5', 'temp',
                             np.array(result_phaselist))
data.export_realdatastack_h5(savepath + '/myrefractiveindex_absorption.h5',
                             'temp', np.array(result_absorptionlist))

print('Zernikes: ' + str(np.real(sess.run(muscat.TF_zernikefactors))))

# backup current script
from shutil import copyfile
import os
src = (os.path.basename(__file__))
copyfile(src, savepath + '/script_bak.py')

#%%
plt.imshow(np.fft.ifftshift(np.angle(sess.run(muscat.TF_Po_aberr))))
#%% Display the results
myfwd, mymeas, my_res = sess.run([tf_fwd, tf_meas, muscat.TF_obj], feed_dict={tf_meas:np_meas})
        
if(is_display): plt.subplot(231),plt.title('FFT, Meas, YZ'), plt.imshow(np.abs(np.fft.fftshift(np.fft.fftn(mymeas))**.2)[:,muscat.mysize[1]//2,:]), plt.colorbar()#, plt.show()    
if(is_display): plt.subplot(232),plt.title('FFT, Meas, XZ'), plt.imshow(np.abs(np.fft.fftshift(np.fft.fftn(mymeas))**.2)[muscat.mysize[0]//2,:,:]), plt.colorbar()#, plt.show()   
if(is_display): plt.subplot(233),plt.title('FFT, Meas, XY'), plt.imshow(np.abs(np.fft.fftshift(np.fft.fftn(mymeas))**.2)[:,:,muscat.mysize[2]//2]), plt.colorbar()#, plt.show()     
if(is_display): plt.subplot(234),plt.title('FFT, Meas, YZ'), plt.imshow(np.abs(np.fft.fftshift(np.fft.fftn(myfwd))**.2)[:,muscat.mysize[1]//2,:]), plt.colorbar()#, plt.show()
if(is_display): plt.subplot(235),plt.title('FFT, Meas, XZ'), plt.imshow(np.abs(np.fft.fftshift(np.fft.fftn(myfwd))**.2)[muscat.mysize[0]//2,:,:]), plt.colorbar()#, plt.show()    
if(is_display): plt.subplot(236),plt.title('FFT, Meas, XY'), plt.imshow(np.abs(np.fft.fftshift(np.fft.fftn(myfwd))**.2)[:,:,muscat.mysize[2]//2]), plt.colorbar(), plt.show()    
if(is_display): plt.subplot(236),plt.title('FFT, Meas, XY'), plt.imshow(np.abs(np.fft.fftshift(np.fft.fftn(myfwd))**.2)[:,:,muscat.mysize[2]//2]), plt.colorbar(), plt.show()    


#%% save the results
#np.save(savepath+'/rec.npy', my_res)
import src.data as data
data.export_realdatastack_h5('temp.h5', 'temp', np.array(resultlist))


if(is_display): plt.subplot(231), plt.title('ABS XZ'),plt.imshow(np.abs(myfwd)[:,muscat.mysize[1]//2,:]), plt.colorbar()#, plt.show()
if(is_display): plt.subplot(232), plt.title('ABS XZ'),plt.imshow(np.abs(myfwd)[:,:,muscat.mysize[2]//2]), plt.colorbar()#, plt.show()
if(is_display): plt.subplot(233), plt.title('ABS XY'),plt.imshow(np.abs(myfwd)[muscat.mysize[0]//2,:,:]), plt.colorbar()#, plt.show()

if(is_display): plt.subplot(234), plt.title('Angle XZ'),plt.imshow(np.angle(myfwd)[:,muscat.mysize[1]//2,:]), plt.colorbar()#, plt.show()
if(is_display): plt.subplot(235), plt.title('Angle XZ'),plt.imshow(np.angle(myfwd)[:,:,muscat.mysize[2]//2]), plt.colorbar()#, plt.show()
if(is_display): plt.subplot(236), plt.title('Angle XY'),plt.imshow(np.angle(myfwd)[muscat.mysize[0]//2,:,:]), plt.colorbar(), plt.show()

# diplay the error over time
plt.title('Error/Cost-function'), plt.plot(np.array(mylosslist)), plt.show()
plt.title('Fidelity-function'), plt.plot(np.array(myfidelitylist)), plt.show()
plt.title('Neg-loss-function'), plt.plot(np.array(myposlosslist)), plt.show()
plt.title('Pos-loss-function'), plt.plot(np.array(myneglosslist)), plt.show()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 21 10:08:51 2018

@author: bene
"""
import numpy as np
import src.data as data
mydata = np.load(
    '/Users/bene/Dropbox/Dokumente/Promotion/PROJECTS/BOSTON/MUSCAT/PYTHON/muScat/tmp.npy'
)
data.export_realdatastack_h5(
    '/Users/bene/Dropbox/Dokumente/Promotion/PROJECTS/BOSTON/MUSCAT/PYTHON/muScat/temp.h5',
    'temp', mydata)
    def saveFigures(self,
                    sess,
                    savepath,
                    tf_fwd,
                    np_meas,
                    mylosslist,
                    myfidelitylist,
                    myneglosslist,
                    mytvlosslist,
                    globalphaselist,
                    globalabslist,
                    result_phaselist=None,
                    result_absorptionlist=None,
                    init_guess=None,
                    figsuffix=''):
        # This is the reconstruction
        if (init_guess is not None):
            myfwd, mymeas, my_res, my_res_absorption = sess.run(
                [tf_fwd, self.tf_meas, self.TF_obj, self.TF_obj_absorption],
                feed_dict={
                    self.tf_meas: np_meas,
                    self.TF_obj: np.real(init_guess),
                    self.TF_obj_absorption: np.imag(init_guess)
                })
        else:
            myfwd, mymeas, my_res, my_res_absorption = sess.run(
                [tf_fwd, self.tf_meas, self.TF_obj, self.TF_obj_absorption],
                feed_dict={self.tf_meas: np_meas})

        plt.figure()
        plt.subplot(231), plt.title('ABS XZ'), plt.imshow(
            np.abs(myfwd)[:, myfwd.shape[1] //
                          2, :]), plt.colorbar()  #, plt.show()
        plt.subplot(232), plt.title('ABS YZ'), plt.imshow(
            np.abs(myfwd)[:, :,
                          myfwd.shape[2] // 2]), plt.colorbar()  #, plt.show()
        plt.subplot(233), plt.title('ABS XY'), plt.imshow(
            np.abs(myfwd)[myfwd.shape[0] //
                          2, :, :]), plt.colorbar()  #, plt.show()
        plt.subplot(234), plt.title('Angle XZ'), plt.imshow(
            np.angle(myfwd)[:, myfwd.shape[1] //
                            2, :]), plt.colorbar()  #, plt.show()
        plt.subplot(235), plt.title('Angle XZ'), plt.imshow(
            np.angle(myfwd)[:, :, myfwd.shape[2] //
                            2]), plt.colorbar()  #, plt.show()
        plt.subplot(236), plt.title('Angle XY'), plt.imshow(
            np.angle(myfwd)[myfwd.shape[0] //
                            2, :, :]), plt.colorbar()  #, plt.show()
        plt.savefig(savepath + '/res_myfwd' + figsuffix + '.png'), plt.show()

        # This is the measurment
        plt.figure()
        plt.subplot(231), plt.title('ABS XZ'), plt.imshow(
            np.abs(mymeas)[:, mymeas.shape[1] //
                           2, :]), plt.colorbar()  #, plt.show()
        plt.subplot(232), plt.title('ABS YZ'), plt.imshow(
            np.abs(mymeas)[:, :, mymeas.shape[2] //
                           2]), plt.colorbar()  #, plt.show()
        plt.subplot(233), plt.title('ABS XY'), plt.imshow(
            np.abs(mymeas)[mymeas.shape[0] //
                           2, :, :]), plt.colorbar()  #, plt.show()
        plt.subplot(234), plt.title('Angle XZ'), plt.imshow(
            np.angle(mymeas)[:, mymeas.shape[1] //
                             2, :]), plt.colorbar()  #, plt.show()
        plt.subplot(235), plt.title('Angle XZ'), plt.imshow(
            np.angle(mymeas)[:, :, mymeas.shape[2] //
                             2]), plt.colorbar()  #, plt.show()
        plt.subplot(236), plt.title('Angle XY'), plt.imshow(
            np.angle(mymeas)[mymeas.shape[0] //
                             2, :, :]), plt.colorbar()  #, plt.show()
        plt.savefig(savepath + '/res_mymeas' + figsuffix + '.png'), plt.show()

        # This is the residual
        plt.figure()
        plt.subplot(231), plt.title('Residual ABS XZ'), plt.imshow(
            (np.abs(myfwd) -
             np.abs(mymeas))[:, myfwd.shape[1] //
                             2, :]), plt.colorbar()  #, plt.show()
        plt.subplot(232), plt.title('Residual ABS YZ'), plt.imshow(
            (np.abs(myfwd) - np.abs(mymeas))[:, :, myfwd.shape[2] //
                                             2]), plt.colorbar()  #, plt.show()
        plt.subplot(233), plt.title('Residual ABS XY'), plt.imshow(
            (np.abs(myfwd) -
             np.abs(mymeas))[myfwd.shape[0] //
                             2, :, :]), plt.colorbar()  #, plt.show()
        plt.subplot(234), plt.title('Residual Angle XZ'), plt.imshow(
            (np.angle(myfwd) -
             np.angle(mymeas))[:, myfwd.shape[1] //
                               2, :]), plt.colorbar()  #, plt.show()
        plt.subplot(235), plt.title('Residual Angle XZ'), plt.imshow(
            (np.angle(myfwd) -
             np.angle(mymeas))[:, :, myfwd.shape[2] //
                               2]), plt.colorbar()  #, plt.show()
        plt.subplot(236), plt.title('Residual Angle XY'), plt.imshow(
            (np.angle(myfwd) -
             np.angle(mymeas))[myfwd.shape[0] //
                               2, :, :]), plt.colorbar()  #, plt.show()
        plt.savefig(savepath + '/res_myresidual' + figsuffix +
                    '.png'), plt.show()

        # diplay the error over time
        plt.figure()
        plt.subplot(231), plt.title('Error/Cost-function'), plt.semilogy(
            (np.array(mylosslist)))  #, plt.show()
        plt.subplot(232), plt.title('Fidelity-function'), plt.semilogy(
            (np.array(myfidelitylist)))  #, plt.show()
        plt.subplot(233), plt.title('Neg-loss-function'), plt.plot(
            np.array(myneglosslist))  #, plt.show()
        plt.subplot(234), plt.title('TV-loss-function'), plt.semilogy(
            np.array(mytvlosslist))  #, plt.show()
        plt.subplot(235), plt.title('Global Phase'), plt.plot(
            np.array(globalphaselist))  #, plt.show()
        plt.subplot(236), plt.title('Global ABS'), plt.plot(
            np.array(globalabslist))  #, plt.show()
        plt.savefig(savepath + '/myplots' + figsuffix + '.png'), plt.show()

        plt.figure()
        plt.subplot(231), plt.title('Result Phase: XZ'), plt.imshow(
            my_res[:, my_res.shape[1] // 2, :]), plt.colorbar()  #, plt.show()
        plt.subplot(232), plt.title('Result Phase: XZ'), plt.imshow(
            my_res[:, :, my_res.shape[2] // 2]), plt.colorbar()  #, plt.show()
        plt.subplot(233), plt.title('Result Phase: XY'), plt.imshow(
            my_res[my_res.shape[0] // 2, :, :]), plt.colorbar()
        plt.subplot(234), plt.title('Result Abs: XZ'), plt.imshow(
            my_res_absorption[:, my_res.shape[1] //
                              2, :]), plt.colorbar()  #, plt.show()
        plt.subplot(235), plt.title('Result abs: XZ'), plt.imshow(
            my_res_absorption[:, :, my_res.shape[2] //
                              2]), plt.colorbar()  #, plt.show()
        plt.subplot(236), plt.title('Result abs: XY'), plt.imshow(
            my_res_absorption[my_res.shape[0] // 2, :, :]), plt.colorbar()
        plt.savefig(savepath + '/RI_abs_result' + figsuffix +
                    '.png'), plt.show()

        print(np.real(sess.run(self.TF_zernikefactors)))
        plt.figure()
        plt.subplot(121), plt.title('Po Phase'), plt.imshow(
            np.fft.fftshift(np.angle(sess.run(
                self.TF_Po_aberr)))), plt.colorbar()
        plt.subplot(122), plt.title('Po abs'), plt.imshow(
            np.fft.fftshift(np.abs(sess.run(
                self.TF_Po_aberr)))), plt.colorbar()
        plt.savefig(savepath + '/recovered_pupil' + figsuffix +
                    '.png'), plt.show()

        if (result_phaselist is not None):
            data.export_realdatastack_h5(
                savepath + '/myrefractiveindex' + figsuffix + '.h5', 'temp',
                np.array(result_phaselist))
        if (result_absorptionlist is not None):
            data.export_realdatastack_h5(
                savepath + '/myrefractiveindex_absorption' + figsuffix + '.h5',
                'temp', np.array(result_absorptionlist))
Example #8
0
#%%
if (0):
    #%% Rainers Test with Carrington approach
    print('Start Deconvolution')
    import InverseModelling as im
    TF_myres, _ = im.convolveCutPSFcpx(TF_meas, myASF, maxdim=3)
    matlab_val = sess.run(TF_myres)

#%%
myres = sess.run(TF_myres, feed_dict={muscat.TF_alpha: .005})
tosave = []
tosave.append(np.real(myres))
tosave.append(np.imag(myres))
tosave = np.array(tosave)
np.save('thikonovinvse.npy', myres)
data.export_realdatastack_h5('./thikonov_deconv.h5', 'temp', tosave)

#%
print('This is the ATF')
plt.figure()
plt.subplot(231), plt.imshow(np.real(myres[:, myATF.shape[1] //
                                           2, :])), plt.colorbar()
plt.subplot(232), plt.imshow(np.real(myres[myATF.shape[0] //
                                           2, :, :])), plt.colorbar()
plt.subplot(233), plt.imshow(np.real(myres[:, :, myATF.shape[2] //
                                           2])), plt.colorbar()

print('This is the deconvolved result:')
plt.subplot(234), plt.imshow(np.imag(myres[:, myATF.shape[1] //
                                           2, :])), plt.colorbar()
plt.subplot(235), plt.imshow(np.imag(myres[myATF.shape[0] //
    np.abs(myfwd)[centerslice, :, :]), plt.colorbar()  # plt.show()
plt.subplot(234), plt.title('angle XZ'), plt.imshow(
    np.angle(myfwd)[:, myfwd.shape[1] // 2, :]), plt.colorbar()  #, plt.show()
plt.subplot(235), plt.title('angle YZ'), plt.imshow(
    np.angle(myfwd)[:, :, myfwd.shape[2] // 2]), plt.colorbar()  #, plt.show()
plt.subplot(236), plt.title('angle XY'), plt.imshow(
    np.angle(myfwd)[centerslice, :, :]), plt.colorbar(), plt.show()

# plot the object RI distribution
plt.figure()
plt.subplot(231), plt.title('obj - real XZ'), plt.imshow(
    np.real(obj)[:, obj.shape[1] // 2, :]), plt.colorbar()  #, plt.show()
plt.subplot(232), plt.title('obj - real YZ'), plt.imshow(
    np.real(obj)[:, :, obj.shape[2] // 2]), plt.colorbar()  #, plt.show()
plt.subplot(233), plt.title('obj - real XY'), plt.imshow(
    np.real(obj)[centerslice, :, :]), plt.colorbar()  # plt.show()
plt.subplot(234), plt.title('obj - imag XZ'), plt.imshow(
    np.imag(obj)[:, obj.shape[1] // 2, :]), plt.colorbar()  #, plt.show()
plt.subplot(235), plt.title('obj - imag YZ'), plt.imshow(
    np.imag(obj)[:, :, obj.shape[2] // 2]), plt.colorbar()  #, plt.show()
plt.subplot(236), plt.title('obj - imag XY'), plt.imshow(
    np.imag(obj)[centerslice, :, :]), plt.colorbar(), plt.show()

#%% save the resultsl
np.save(savepath + 'allAmp_simu.npy', myfwd)
data.export_realdatastack_h5(savepath + '/obj.h5', 'phase, abs',
                             np.stack((np.real(obj), np.imag(obj)), axis=0))
data.export_realdatastack_h5(
    savepath + '/myfwd.h5', 'real, imag',
    np.stack((np.real(myfwd), np.imag(myfwd)), axis=0))