# Path: pyqstem/detection.py
# def detect(img,sampling,dose=None,MTF_param=None,MTF_func=None,blur=None,resample=None):
# 
#     if resample is not None:
#         if not isinstance(resample, (list, tuple)):
#             resample=(resample,)*2
#         zoom=(sampling[0]/resample[0],sampling[1]/resample[1])
#         sampling=resample
#         warnings.filterwarnings('ignore')
#         img = scipy.ndimage.interpolation.zoom(img, zoom)
#         warnings.filterwarnings('always')
#         
#     
#     if blur is not None:
#         img=gaussian(img,blur)
#     
#     if dose is not None:
#         img = img/np.sum(img)*dose*np.product(sampling)*np.product(img.shape)
#         
#         img[img<0]=0
#         #vals = len(np.unique(img))
#         #vals = 2**np.ceil(np.log2(vals))
#         #img = np.random.poisson(img * vals) / float(vals)
# 
#         img = np.random.poisson(img).astype(np.int64)
#     
#     if MTF_param is not None:
#         if MTF_func is None:
#             MTF_func=MTF_a
#     
#         kx,ky,k2=spatial_frequencies(img.shape,sampling)
#         k=np.sqrt(k2)
#         mtf=MTF_func(k,*MTF_param)
#         img=np.fft.ifft2(np.fft.fft2(img)*np.sqrt(mtf))
#         img=(img.real+img.imag)/2
#     
#     return img


import matplotlib.pyplot as plt
import scipy
import scipy.ndimage
import numpy as np
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.widgets import Slider
from .detection import detect

        if len(self.array.shape)==2:
            raise RuntimeError('z_slice() only works for 3d wavefunctions')

        return Wavefunction(self.array[:,:,ind],self.energy,self.sampling[:2],self.offset)

    def apply_ctf(self,ctf):
        return ctf.apply(self)

    def resample(self,sampling):

        if len(self.array.shape)==3:
            raise RuntimeError('resample() only works for 2d wavefunctions')

        if not isinstance(sampling, (list, tuple)):
            sampling=(sampling,)*2

        zoom=(self.sampling[0]/sampling[0],self.sampling[1]/sampling[1])

        real = scipy.ndimage.interpolation.zoom(np.real(self.array), zoom)
        imag = scipy.ndimage.interpolation.zoom(np.imag(self.array), zoom)

        sampling=(self.array.shape[0]*self.sampling[0]/real.shape[0],
                    self.array.shape[1]*self.sampling[1]/real.shape[1])

        return Wavefunction(real+1.j*imag,self.energy,sampling,self.offset)

    def detect(self,dose=None,MTF_param=None,MTF_func=None,blur=None,resample=None):
        sampling=self.sampling
        img=np.abs(self.array)**2

# Path: pyqstem/detection.py
# def detect(img,sampling,dose=None,MTF_param=None,MTF_func=None,blur=None,resample=None):
# 
#     if resample is not None:
#         if not isinstance(resample, (list, tuple)):
#             resample=(resample,)*2
#         zoom=(sampling[0]/resample[0],sampling[1]/resample[1])
#         sampling=resample
#         warnings.filterwarnings('ignore')
#         img = scipy.ndimage.interpolation.zoom(img, zoom)
#         warnings.filterwarnings('always')
#         
#     
#     if blur is not None:
#         img=gaussian(img,blur)
#     
#     if dose is not None:
#         img = img/np.sum(img)*dose*np.product(sampling)*np.product(img.shape)
#         
#         img[img<0]=0
#         #vals = len(np.unique(img))
#         #vals = 2**np.ceil(np.log2(vals))
#         #img = np.random.poisson(img * vals) / float(vals)
# 
#         img = np.random.poisson(img).astype(np.int64)
#     
#     if MTF_param is not None:
#         if MTF_func is None:
#             MTF_func=MTF_a
#     
#         kx,ky,k2=spatial_frequencies(img.shape,sampling)
#         k=np.sqrt(k2)
#         mtf=MTF_func(k,*MTF_param)
#         img=np.fft.ifft2(np.fft.fft2(img)*np.sqrt(mtf))
#         img=(img.real+img.imag)/2
#     
#     return img


# Path: pyqstem/wave.py
import matplotlib.pyplot as plt
import scipy
import scipy.ndimage
import numpy as np
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.widgets import Slider
from .detection import detect


        if len(self.array.shape)==2:
            raise RuntimeError('z_slice() only works for 3d wavefunctions')

        return Wavefunction(self.array[:,:,ind],self.energy,self.sampling[:2],self.offset)

    def apply_ctf(self,ctf):
        return ctf.apply(self)

    def resample(self,sampling):

        if len(self.array.shape)==3:
            raise RuntimeError('resample() only works for 2d wavefunctions')

        if not isinstance(sampling, (list, tuple)):
            sampling=(sampling,)*2

        zoom=(self.sampling[0]/sampling[0],self.sampling[1]/sampling[1])

        real = scipy.ndimage.interpolation.zoom(np.real(self.array), zoom)
        imag = scipy.ndimage.interpolation.zoom(np.imag(self.array), zoom)

        sampling=(self.array.shape[0]*self.sampling[0]/real.shape[0],
                    self.array.shape[1]*self.sampling[1]/real.shape[1])

        return Wavefunction(real+1.j*imag,self.energy,sampling,self.offset)

    def detect(self,dose=None,MTF_param=None,MTF_func=None,blur=None,resample=None):
        sampling=self.sampling
        img=np.abs(self.array)**2

        return detect(img,sampling,dose=dose,MTF_param=MTF_param,MTF_func=MTF_func,blur=blur,resample=resample)