Example #1
0
File: warp.py Project: wholden/xrt
 def __init__(self, *args, **kwargs):
     roe.ToroidMirror.__init__(self, *args, **kwargs)
     ### here you specify the bump and its mesh ###
     if get_distorted_surface is None:
         self.limPhysX = [-5, 5]
         self.limPhysY = [-125, 125]
         self.get_surface_limits()
         return
     self.warpX, self.warpY, self.warpZ, self.distortedSurfaceName =\
         get_distorted_surface()
     #        print('xyz sizes:')
     #        print(self.warpX.min(), self.warpX.max())
     #        print(self.warpY.min(), self.warpY.max())
     #        print(self.warpZ.min(), self.warpZ.max())
     self.warpNX, self.warpNY = len(self.warpX), len(self.warpY)
     self.limPhysX = np.min(self.warpX), np.max(self.warpX)
     self.limPhysY = np.min(self.warpY), np.max(self.warpY)
     self.get_surface_limits()
     self.warpA, self.warpB = np.gradient(self.warpZ)
     dx = self.warpX[1] - self.warpX[0]
     dy = self.warpY[1] - self.warpY[0]
     self.warpA = np.arctan(self.warpA / dx)
     self.warpB = np.arctan(self.warpB / dy)
     #        print(self.warpZ.shape)
     #end# here you specify the bump and its mesh ###
     self.warpSplineZ = ndimage.spline_filter(self.warpZ)
     self.warpSplineA = ndimage.spline_filter(self.warpA)
     self.warpSplineB = ndimage.spline_filter(self.warpB)
Example #2
0
def interpolate(h, hnext, filter):
  """ interpolate h into hnext, this needs a lot of memory(full mesh of the
  lower level) """
  # round down
  exbot = (hnext['Offset'] * h['Nmesh']) // hnext['Nmesh']
  # round up
  extop = ((hnext['Offset'] + hnext['Size']) * h['Nmesh'] + h['Nmesh'] - 1)// hnext['Nmesh']

  print h['Nmesh'], 'box', exbot, extop

  overlay = joinchunks(h, exbot, extop)
  src = overlay.reshape(*(extop-exbot))
  if filter >= 2:
    src['delta'] = spline_filter(src['delta'], order=filter)
    for d in range(3):
      src['disp'][:, d] = spline_filter(src['disp'][:, d], order=filter)

  for fn in list(yieldfilename(hnext, lookfor='delta1')):
    # read in the current level displacement
    chunk = readchunk(hnext, fn, None, None, extra='1')
    ipos = chunk['ipos']
    ipos = 1.0 * ipos * h['Nmesh'] / hnext['Nmesh'] - exbot
    chunk['delta'] += map_coordinates(src['delta'], ipos.T, order=filter,
            prefilter=False)
    for d in range(3):
      chunk['disp'][:, d] += map_coordinates(src['disp'][..., d], ipos.T,
              order=filter, prefilter=False)
    # write to the full displacement
    writechunk(hnext, fn, chunk)
Example #3
0
def interpolate(h, hnext, filter):
    """ interpolate h into hnext, this needs a lot of memory(full mesh of the
  lower level) """
    # round down
    exbot = (hnext['Offset'] * h['Nmesh']) // hnext['Nmesh']
    # round up
    extop = ((hnext['Offset'] + hnext['Size']) * h['Nmesh'] + h['Nmesh'] -
             1) // hnext['Nmesh']

    print h['Nmesh'], 'box', exbot, extop

    overlay = joinchunks(h, exbot, extop)
    src = overlay.reshape(*(extop - exbot))
    if filter >= 2:
        src['delta'] = spline_filter(src['delta'], order=filter)
        for d in range(3):
            src['disp'][:, d] = spline_filter(src['disp'][:, d], order=filter)

    for fn in list(yieldfilename(hnext, lookfor='delta1')):
        # read in the current level displacement
        chunk = readchunk(hnext, fn, None, None, extra='1')
        ipos = chunk['ipos']
        ipos = 1.0 * ipos * h['Nmesh'] / hnext['Nmesh'] - exbot
        chunk['delta'] += map_coordinates(src['delta'],
                                          ipos.T,
                                          order=filter,
                                          prefilter=False)
        for d in range(3):
            chunk['disp'][:, d] += map_coordinates(src['disp'][..., d],
                                                   ipos.T,
                                                   order=filter,
                                                   prefilter=False)
        # write to the full displacement
        writechunk(hnext, fn, chunk)
Example #4
0
File: warp.py Project: kklmn/xrt
    def __init__(self, *args, **kwargs):
        roe.ToroidMirror.__init__(self, *args, **kwargs)
### here you specify the bump and its mesh ###
        if get_distorted_surface is None:
            self.limPhysX = [-5, 5]
            self.limPhysY = [-125, 125]
            self.get_surface_limits()
            return
        self.warpX, self.warpY, self.warpZ, self.distortedSurfaceName =\
            get_distorted_surface()
#        print('xyz sizes:')
#        print(self.warpX.min(), self.warpX.max())
#        print(self.warpY.min(), self.warpY.max())
#        print(self.warpZ.min(), self.warpZ.max())
        self.warpNX, self.warpNY = len(self.warpX), len(self.warpY)
        self.limPhysX = np.min(self.warpX), np.max(self.warpX)
        self.limPhysY = np.min(self.warpY), np.max(self.warpY)
        self.get_surface_limits()
        self.warpA, self.warpB = np.gradient(self.warpZ)
        dx = self.warpX[1] - self.warpX[0]
        dy = self.warpY[1] - self.warpY[0]
        self.warpA = np.arctan(self.warpA/dx)
        self.warpB = np.arctan(self.warpB/dy)
#        print(self.warpZ.shape)
#end# here you specify the bump and its mesh ###
        self.warpSplineZ = ndimage.spline_filter(self.warpZ)
        self.warpSplineA = ndimage.spline_filter(self.warpA)
        self.warpSplineB = ndimage.spline_filter(self.warpB)
 def _precompute(self):
     """function which is called after model loading and can be
     overridden to allow for interpolation specific precomputations"""
      #compute the gradient of the PSF for interpolated jacobians
     self.gradX, self.gradY, self.gradZ = np.gradient(self.interpModel)
     self.gradX /= self.dx
     self.gradY /= self.dy
     self.gradZ /= self.dz
     
     self.interpModel = ndimage.spline_filter(self.interpModel).astype('f')
     self.gradX = ndimage.spline_filter(self.gradX).astype('f')
     self.gradY = ndimage.spline_filter(self.gradY).astype('f')
     self.gradZ = ndimage.spline_filter(self.gradZ).astype('f')
Example #6
0
 def _precompute(self):
     '''function which is called after model loading and can be
     overridden to allow for interpolation specific precomputations'''
      #compute the gradient of the PSF for interpolated jacobians
     self.gradX, self.gradY, self.gradZ = gradient(self.interpModel)
     self.gradX /= self.dx
     self.gradY /= self.dy
     self.gradZ /= self.dz
     
     self.interpModel = ndimage.spline_filter(self.interpModel).astype('f')
     self.gradX = ndimage.spline_filter(self.gradX).astype('f')
     self.gradY = ndimage.spline_filter(self.gradY).astype('f')
     self.gradZ = ndimage.spline_filter(self.gradZ).astype('f')
Example #7
0
 def test_spline05(self, dtype, order):
     data = numpy.ones([4, 4], dtype)
     out = ndimage.spline_filter(data, order=order)
     assert_array_almost_equal(out, [[1, 1, 1, 1],
                                     [1, 1, 1, 1],
                                     [1, 1, 1, 1],
                                     [1, 1, 1, 1]])
Example #8
0
 def test_affine_transform26(self, order):
     # test homogeneous coordinates
     data = numpy.array([[4, 1, 3, 2], [7, 6, 8, 5], [3, 5, 3, 6]])
     if (order > 1):
         filtered = ndimage.spline_filter(data, order=order)
     else:
         filtered = data
     tform_original = numpy.eye(2)
     offset_original = -numpy.ones((2, 1))
     tform_h1 = numpy.hstack((tform_original, offset_original))
     tform_h2 = numpy.vstack((tform_h1, [[0, 0, 1]]))
     out1 = ndimage.affine_transform(filtered,
                                     tform_original,
                                     offset_original.ravel(),
                                     order=order,
                                     prefilter=False)
     out2 = ndimage.affine_transform(filtered,
                                     tform_h1,
                                     order=order,
                                     prefilter=False)
     out3 = ndimage.affine_transform(filtered,
                                     tform_h2,
                                     order=order,
                                     prefilter=False)
     for out in [out1, out2, out3]:
         assert_array_almost_equal(
             out, [[0, 0, 0, 0], [0, 4, 1, 3], [0, 7, 6, 8]])
Example #9
0
def setModel(modName, md):
    global IntXVals, IntYVals, IntZVals, interpModel, interpModelName, dx, dy, dz

    if not modName == interpModelName:
        mf = open(getFullExistingFilename(modName), 'rb')
        mod, voxelsize = cPickle.load(mf)
        mf.close()

        interpModelName = modName

        #if not voxelsize.x == md.voxelsize.x:
        #    raise RuntimeError("PSF and Image voxel sizes don't match")

        IntXVals = 1e3 * voxelsize.x * mgrid[-(mod.shape[0] / 2.):
                                             (mod.shape[0] / 2.)]
        IntYVals = 1e3 * voxelsize.y * mgrid[-(mod.shape[1] / 2.):
                                             (mod.shape[1] / 2.)]
        IntZVals = 1e3 * voxelsize.z * mgrid[-(mod.shape[2] / 2.):
                                             (mod.shape[2] / 2.)]

        dx = voxelsize.x * 1e3
        dy = voxelsize.y * 1e3
        dz = voxelsize.z * 1e3

        interpModel = mod

        interpModel = interpModel / interpModel.max()  #normalise to 1

        #do the spline filtering here rather than in interpolation
        interpModel = ndimage.spline_filter(interpModel)

        twist.twistCal(interpModel, IntXVals, IntYVals, IntZVals)
Example #10
0
def perform_voting(patches,
                   output_shape,
                   expected_shape,
                   extraction_step,
                   window_sep=(8, 8, 8)):
    vote_img = np.zeros(expected_shape)
    vote_count = np.zeros(expected_shape)

    coordinates = generate_indexes(output_shape, extraction_step,
                                   expected_shape)

    W = np.ones(output_shape)
    W[window_sep[0]:-window_sep[0], window_sep[1]:-window_sep[1],
      window_sep[2]:-window_sep[2]] = 0

    W_dist = distance_transform_edt(W)
    W_dist = 1 - W_dist / W_dist.max()

    for count, coord in enumerate(coordinates):
        selection = [
            slice(coord[i] - output_shape[i], coord[i])
            for i in range(len(coord))
        ]
        vote_img[selection] += np.multiply(patches[count], W_dist)
        vote_count[selection] += np.multiply(
            np.ones(vote_img[selection].shape), W_dist)

    vote_count[vote_count == 0] = 1

    return spline_filter(np.divide(vote_img, vote_count))
Example #11
0
def convert_and_resample_from_tif(file_name, output_file='image_cropped_ana.h5', z_factor=2.35, path_to_image='data'):
    import javabridge
    import bioformats
    
    javabridge.start_vm(class_path=bioformats.JARS)
    r = bioformats.ImageReader(file_name)
    
    shape = (r.rdr.getSizeT(), r.rdr.getSizeC(), r.rdr.getSizeZ(), r.rdr.getSizeY(), r.rdr.getSizeX())
    shape_r = (r.rdr.getSizeT(), r.rdr.getSizeC(), int(z_factor * r.rdr.getSizeZ()), r.rdr.getSizeY(), r.rdr.getSizeX())
    
    img = numpy.zeros(shape, dtype=numpy.float32)
    img_r = numpy.zeros(shape_r, dtype=numpy.float32)
    img_r_prefilter = numpy.zeros(shape_r, dtype=numpy.float32)
    for t in range(shape[0]):
        print "T:", t,
        for c in range(shape[1]):
            for z in range(shape[2]):
                img[t, c, z, :, :,] = r.read(c=c, t=t, z=z)
            img_r[t,c,:,:,:] = vigra.sampling.resizeVolumeSplineInterpolation(img[t,c,:,:,:], shape_r[2:])
            img_r_prefilter[t, c, :, :, :] = ndimage.spline_filter(img_r[t,c,:,:,:])
        
    f = h5py.File(output_file, 'w')
    f["/"].create_dataset(path_to_image, data=img)
    f["/"].create_dataset(path_to_image + "_resampled", data=img_r)
    f["/"].create_dataset(path_to_image + "_resampled_prefiltered", data=img_r_prefilter)
    f.close()
    javabridge.kill_vm()  
Example #12
0
    def _buildknots(self, use_mmap):
        if self.order > 1:
##             data = ndimage.spline_filter(
##                 np.nan_to_num(np.asarray(self.image).astype('d')),
##                 self.order)
            in_data = np.asarray(self.image)
            data = ndimage.spline_filter(in_data,
                                         order=self.order,
                                         output=in_data.dtype)
        else:
##             data = np.nan_to_num(np.asarray(self.image).astype('d'))
            data = np.asarray(self.image)

        if use_mmap:
            if self._datafile is None:
                _, fname = tempfile.mkstemp()
                self._datafile = file(fname, mode='wb')
            else:
                self._datafile = file(self._datafile.name, 'wb')

            data.tofile(self._datafile)
            datashape = data.shape
            dtype = data.dtype
            del(data)
            self._datafile.close()
            self._datafile = file(self._datafile.name)
            self.data = np.memmap(self._datafile.name, dtype=dtype,
                                  mode='r+', shape=datashape)
        else:
            self.data = data
Example #13
0
def correlate_frames(node_pos, mesh, img, ref, settings):
    """
    Parameters
    ----------
    node_pos : ndarray
       The position of the nodes
    mesh : Mesh
       The mesh object
    img : ndarray
       2d array containing the image frame
    ref : Reference
       The reference object
    settings : DICInput
       The settings which will be used during the analysis
   Returns
   -------
   updated node positions, current pixel values
    """

    logger = logging.getLogger(__name__)

    node_pos = np.copy(node_pos).astype(settings.precision)

    # Declare empty arrays
    pixel_pos = np.zeros((2, ref.n_pixels), dtype=settings.precision)
    dnod_x = np.zeros(mesh.n_nodes * 2)

    image_filtered = nd.spline_filter(
        img, order=settings.interpolation_order).transpose()

    for it in range(settings.maxit):

        # Find nodal positions within ROI
        np.dot(node_pos, ref.Nref_stack, out=pixel_pos)

        # Find pixel values for current coordinates
        Ic = nd.map_coordinates(image_filtered,
                                pixel_pos,
                                order=settings.interpolation_order,
                                prefilter=False)

        # Calculate position increment as (B^T B)^-1 * (B^T*dIk) "Least squares solution"
        dnod = np.dot(ref.K, ref.I0_stack - Ic)

        # Add increment to nodal positions
        node_pos[0, :] += dnod[:mesh.n_nodes]
        node_pos[1, :] += dnod[mesh.n_nodes:]

        dnod_x += dnod

        # Check for convergence
        if np.max(np.abs(dnod)) < settings.tol:
            logger.info('Frame converged in %s iterations', it)
            return np.array(
                (dnod_x[:mesh.n_nodes], dnod_x[mesh.n_nodes:])), Ic, True

        # Reset array values
    logger.info("Frame did not converge. Largest increment was %f pixels" %
                np.max(dnod))
    return np.array((dnod_x[:mesh.n_nodes], dnod_x[mesh.n_nodes:])), Ic, False
Example #14
0
 def createModel(self, order=1):
     if order == 1:
         self.model = self.image.copy()
     else:
         self.model = ndimage.spline_filter(self.image,
                                            output=numpy.float64,
                                            order=order)
     self.order = order
Example #15
0
 def advect(field, filter_epsilon=10e-2, mode='constant'):
     filtered = spline_filter(field, order=self.advect_order, mode=mode)
     field = filtered * (1 - filter_epsilon) + field * filter_epsilon
     return map_coordinates(field,
                            advection_map,
                            prefilter=False,
                            order=self.advect_order,
                            mode=mode)
Example #16
0
 def createModel(self, order=1):
     self.model = {}
     for band in self.psfs:
         if order==1:
             self.model[band] = self.psfs[band].copy()
         else:
             self.model[band] = ndimage.spline_filter(self.psfs[band], output=np.float64, order=order)
     self.order = order
Example #17
0
    def _interpolationFunctionFactory(self, spline_order=None, cval=None):
        """Returns a function F(x,y,z) that interpolates any values on the grid.

        _interpolationFunctionFactory(self,spline_order=3,cval=None) --> F

        *cval* is set to :meth:`Grid.grid.min`. *cval* cannot be chosen too
        large or too small or NaN because otherwise the spline interpolation
        breaks down near that region and produces wild oscillations.

        .. Note:: Only correct for equally spaced values (i.e. regular edges with
                  constant delta).
        .. SeeAlso:: http://www.scipy.org/Cookbook/Interpolation
        """
        # for scipy >=0.9: should use scipy.interpolate.griddata
        # http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.griddata.html#scipy.interpolate.griddata
        # (does it work for nD?)

        from scipy import ndimage

        if spline_order is None:
            # must be compatible with whatever :func:`scipy.ndimage.spline_filter` takes.
            spline_order = self.interpolation_spline_order
        if cval is None:
            cval = self.interpolation_cval

        data = self.grid
        if cval is None:
            cval = data.min()
        try:
            # masked arrays
            _data = data.filled(cval)   # fill with min; hopefully keeps spline happy
        except AttributeError:
            _data = data

        coeffs = ndimage.spline_filter(_data, order=spline_order)
        x0 = self.origin
        dx = self.delta.diagonal()    # fixed dx required!!

        def _transform(cnew, c0, dc):
            return (numpy.atleast_1d(cnew) - c0) / dc

        def interpolatedF(*coordinates):
            """B-spline function over the data grid(x,y,z).

            interpolatedF([x1,x2,...],[y1,y2,...],[z1,z2,...]) -> F[x1,y1,z1],F[x2,y2,z2],...

            Example usage for resampling::
              >>> XX,YY,ZZ = numpy.mgrid[40:75:0.5, 96:150:0.5, 20:50:0.5]
              >>> FF = _interpolationFunction(XX,YY,ZZ)            
            """
            _coordinates = numpy.array(
                [_transform(coordinates[i], x0[i], dx[i]) for i in range(len(coordinates))])
            return ndimage.map_coordinates(coeffs, _coordinates, prefilter=False,
                                           mode='nearest', cval=cval)

            # mode='wrap' would be ideal but is broken: http://projects.scipy.org/scipy/ticket/796

        return interpolatedF
Example #18
0
 def __init__(self,axes,z,order=3):
     from scipy import ndimage
     import scipy
     self.axes = {}
     for key in axes.keys():
         self.axes[key] = axes[key]
     self.z = z.copy()
     self.spline = ndimage.spline_filter(z,output=scipy.float64,order=order)
     self.order = order
Example #19
0
 def test_shift09(self, order):
     data = numpy.array([[4, 1, 3, 2], [7, 6, 8, 5], [3, 5, 3, 6]])
     if (order > 1):
         filtered = ndimage.spline_filter(data, order=order)
     else:
         filtered = data
     out = ndimage.shift(filtered, [1, 1], order=order, prefilter=False)
     assert_array_almost_equal(out,
                               [[0, 0, 0, 0], [0, 4, 1, 3], [0, 7, 6, 8]])
 def set_order(self,order):
     from scipy import ndimage
     import scipy
     self.order = order
     if order==1:
         self.spline = self.z.copy()
         return
     self.spline = ndimage.spline_filter(self.z,output=scipy.float64,
                                             order=order)
Example #21
0
 def set_order(self, order):
     from scipy import ndimage
     import scipy
     self.order = order
     if order == 1:
         self.spline = self.z.copy()
         return
     self.spline = ndimage.spline_filter(self.z,
                                         output=scipy.float64,
                                         order=order)
Example #22
0
    def __init__( self, griddata, lo, hi, maps=None, copy=True, verbose=1,
            order=1, prefilter=False ):
        griddata = np.asanyarray( griddata )
        dim = griddata.ndim  # - (griddata.shape[-1] == 1)  # ??
        assert dim >= 2, griddata.shape
        self.dim = dim
        if np.isscalar(lo):
            lo *= np.ones(dim)
        if np.isscalar(hi):
            hi *= np.ones(dim)
        self.loclip = lo = np.asarray_chkfinite( lo ).copy()
        self.hiclip = hi = np.asarray_chkfinite( hi ).copy()
        assert lo.shape == (dim,), lo.shape
        assert hi.shape == (dim,), hi.shape
        self.copy = copy
        self.verbose = verbose
        self.order = order
        if order > 1  and 0 < prefilter < 1:  # 1/3: Mitchell-Netravali = 1/3 B + 2/3 fit
            exactfit = spline_filter( griddata )  # see Unser
            griddata += prefilter * (exactfit - griddata)
            prefilter = False
        self.griddata = griddata
        self.prefilter = (prefilter == True)

        if maps is None:
            maps = [None,] * len(lo)

        self.maps = maps
        self.nmap = 0
        if len(maps) > 0:
            assert len(maps) == dim, "maps must have len %d, not %d" % (
                    dim, len(maps))
            # linear maps (map None): Xcol -= lo *= scale -> [0, n-1]
            # nonlinear: np.interp e.g. [50 52 62 63] -> [0 1 2 3]
            self._lo = np.zeros(dim)
            self._scale = np.ones(dim)

            for j, (map, n, l, h) in enumerate( zip( maps, griddata.shape, lo, hi )):
                ## print "test: j map n l h:", j, map, n, l, h
                if map is None  or isinstance(map, collections.Callable):
                    self._lo[j] = l
                    if h > l:
                        self._scale[j] = (n - 1) / (h - l)  # _map lo -> 0, hi -> n - 1
                    else:
                        self._scale[j] = 0  # h <= l: X[:,j] -> 0
                    continue
                self.maps[j] = map = np.asanyarray(map)
                self.nmap += 1
                assert len(map) == n, "maps[%d] must have len %d, not %d" % (
                    j, n, len(map) )
                mlo, mhi = map.min(), map.max()
                if not (l <= mlo <= mhi <= h):
                    print("Warning: Intergrid maps[%d] min %.3g max %.3g " \
                        "are outside lo %.3g hi %.3g" % (
                        j, mlo, mhi, l, h ))
Example #23
0
    def __init__( self, griddata, lo, hi, maps=None, copy=True, verbose=1,
            order=1, prefilter=False ):
        griddata = np.asanyarray( griddata )
        dim = griddata.ndim # - (griddata.shape[-1] == 1) # ??
        assert dim >= 2, griddata.shape
        self.dim = dim
        if np.isscalar(lo):
            lo *= np.ones(dim)
        if np.isscalar(hi):
            hi *= np.ones(dim)
        self.loclip = lo = np.asarray_chkfinite( lo ).copy()
        self.hiclip = hi = np.asarray_chkfinite( hi ).copy()
        assert lo.shape == (dim,), lo.shape
        assert hi.shape == (dim,), hi.shape
        self.copy = copy
        self.verbose = verbose
        self.order = order
        if order > 1 and 0 < prefilter < 1: # 1/3: Mitchell-Netravali = 1/3 B + 2/3 fit
            exactfit = spline_filter( griddata ) # see Unser
            griddata += prefilter * (exactfit - griddata)
            prefilter = False
        self.griddata = griddata
        self.prefilter = (prefilter == True)

        if maps is None:
            maps = [None,] * len(lo)

        self.maps = maps
        self.nmap = 0
        if len(maps) > 0:
            assert len(maps) == dim, "maps must have len %d, not %d" % (
                    dim, len(maps))
            # linear maps (map None): Xcol -= lo *= scale -> [0, n-1]
            # nonlinear: np.interp e.g. [50 52 62 63] -> [0 1 2 3]
            self._lo = np.zeros(dim)
            self._scale = np.ones(dim)

            for j, (map, n, l, h) in enumerate( zip( maps, griddata.shape, lo, hi )):
                ## print "test: j map n l h:", j, map, n, l, h
                if map is None or callable(map):
                    self._lo[j] = l
                    if h > l:
                        self._scale[j] = (n - 1) / (h - l) # _map lo -> 0, hi -> n - 1
                    else:
                        self._scale[j] = 0 # h <= l: X[:,j] -> 0
                    continue
                self.maps[j] = map = np.asanyarray(map)
                self.nmap += 1
                assert len(map) == n, "maps[%d] must have len %d, not %d" % (
                    j, n, len(map) )
                mlo, mhi = map.min(), map.max()
                if not (l <= mlo <= mhi <= h):
                    print "Warning: Intergrid maps[%d] min %.3g max %.3g " \
                        "are outside lo %.3g hi %.3g" % (
                        j, mlo, mhi, l, h )
Example #24
0
File: test.py Project: jit9/enlib
def spline_filter_mat(n, order=3, border="mirror", trans=False, scipy=False):
	out = np.zeros((n,n))
	for i in range(n):
		data = np.zeros(n)
		data[i] = 1
		if not scipy:
			out[i] = spline_filter(data, order=order, border=border, trans=trans)
		else:
			if order > 1:
				out[i] = ndimage.spline_filter(data, order=order)
	return out
Example #25
0
def interpolate(arrayin,shape=(256, 256)):
    """Interpolate to the grid."""
    if arrayin.dtype == 'complex' :
        Ln = interpolate(np.real(arrayin),shape) + 1.0J * interpolate(np.imag(arrayin),shape)
        #Ln = interpolate(np.abs(arrayin),new_res) * np.exp(1.0J * interpolate(np.angle(arrayin),new_res))
    else :
        coeffs    = ndimage.spline_filter(arrayin)
        rows,cols = arrayin.shape
        coords    = np.mgrid[0:rows-1:1j*shape[0],0:cols-1:1j*shape[1]]
        Ln        = sp.ndimage.map_coordinates(coeffs, coords, prefilter=False)
    return Ln
Example #26
0
def plot_NOM_3D(fname):
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib import cm
    from matplotlib.ticker import LinearLocator, FormatStrFormatter

    xL, yL, zL = np.loadtxt(fname + '.dat', unpack=True)
    nX = (yL == yL[0]).sum()
    nY = (xL == xL[0]).sum()
    x = xL.reshape((nY, nX))
    y = yL.reshape((nY, nX))
    z = zL.reshape((nY, nX))
    x1D = xL[:nX]
    y1D = yL[::nX]
    #    z += z[::-1, :]
    zmax = abs(z).max()

    fig = plt.figure()
    ax = fig.gca(projection='3d')
    surf = ax.plot_surface(x,
                           y,
                           z,
                           rstride=1,
                           cstride=1,
                           cmap=cm.coolwarm,
                           linewidth=0,
                           antialiased=False,
                           alpha=0.5)
    ax.set_zlim(-zmax, zmax)
    ax.zaxis.set_major_locator(LinearLocator(10))
    ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

    fig.colorbar(surf, shrink=0.5, aspect=5)

    splineZ = ndimage.spline_filter(z.T)
    nrays = 1e3
    xnew = np.random.uniform(x1D[0], x1D[-1], nrays)
    ynew = np.random.uniform(y1D[0], y1D[-1], nrays)
    coords = np.array([(xnew - x1D[0]) / (x1D[-1] - x1D[0]) * (nX - 1),
                       (ynew - y1D[0]) / (y1D[-1] - y1D[0]) * (nY - 1)])
    znew = ndimage.map_coordinates(splineZ, coords, prefilter=True)
    ax.scatter(xnew,
               ynew,
               znew,
               c=znew,
               marker='o',
               color='gray',
               s=50,
               cmap=cm.coolwarm)

    fig.savefig(fname + '_3d.png')
    plt.show()
Example #27
0
 def save_spline_arrays(self, pickleName, what):
     if _DEBUG:
         print('. Pickling splines to\n{0}'.format(pickleName))
     splines = []
     for ia, a in enumerate(what):
         a = np.concatenate((a[:, self.extraRows:0:-1, :], a), axis=1)
         a = np.concatenate((a[:, :, self.extraRows:0:-1], a), axis=2)
         if self.order == 3:
             spline = ndimage.spline_filter(a)
         else:
             spline = a
         splines.append(spline)
     Imax = np.max(what[0])
     with open(pickleName, 'wb') as f:
         pickle.dump((Imax, splines), f, protocol=2)
     return splines, Imax
Example #28
0
 def save_spline_arrays(self, pickleName, what):
     if _DEBUG:
         print('. Pickling splines to\n{0}'.format(pickleName))
     splines = []
     for ia, a in enumerate(what):
         a = np.concatenate((a[:, self.extraRows:0:-1, :], a), axis=1)
         a = np.concatenate((a[:, :, self.extraRows:0:-1], a), axis=2)
         if self.order == 3:
             spline = ndimage.spline_filter(a)
         else:
             spline = a
         splines.append(spline)
     Imax = np.max(what[0])
     with open(pickleName, 'wb') as f:
         pickle.dump((Imax, splines), f, protocol=2)
     return splines, Imax
Example #29
0
    def test_geometric_transform10(self, order):
        data = numpy.array([[4, 1, 3, 2],
                            [7, 6, 8, 5],
                            [3, 5, 3, 6]])

        def mapping(x):
            return (x[0] - 1, x[1] - 1)

        if (order > 1):
            filtered = ndimage.spline_filter(data, order=order)
        else:
            filtered = data
        out = ndimage.geometric_transform(filtered, mapping, data.shape,
                                          order=order, prefilter=False)
        assert_array_almost_equal(out, [[0, 0, 0, 0],
                                        [0, 4, 1, 3],
                                        [0, 7, 6, 8]])
def tweakImages(batchImages):
	for index, image in enumerate(batchImages):
		# Change shape for tweaking
		image = np.reshape(image, (image.shape[0], image.shape[1]))
		original2DShape = image.shape
		# Rotate image
		image = ndimage.rotate(image, randint(-10, 10), reshape=False)
		# Shift image
		image = ndimage.shift(image, (randint(-1, 1), randint(-1, 1)))
		# Invert image
		if choice([True, False]):	image = -image
		# Spline (smoothing of neighbouring pixels)
		if choice([True, False]):	image = ndimage.spline_filter(image)
		# Put back into shape
		image = np.reshape(image, (image.shape[0], image.shape[1], 1))
		batchImages[index] = image
	return batchImages
Example #31
0
    def __init__( self, griddata, lo, hi, maps=[], copy=True, verbose=1,
            order=1, prefilter=False ):
        griddata = np.asanyarray( griddata )
        dim = griddata.ndim  # - (griddata.shape[-1] == 1)  # ??
        assert dim >= 2, griddata.shape
        self.dim = dim
        if np.isscalar(lo):
            lo *= np.ones(dim)
        if np.isscalar(hi):
            hi *= np.ones(dim)
        assert lo.shape == (dim,), lo.shape
        assert hi.shape == (dim,), hi.shape
        self.loclip = lo = np.asarray_chkfinite( lo ).copy()
        self.hiclip = hi = np.asarray_chkfinite( hi ).copy()
        self.copy = copy
        self.verbose = verbose
        self.order = order
        if order > 1  and 0 < prefilter < 1:  # 1/3: Mitchell-Netravali = 1/3 B + 2/3 fit
            exactfit = spline_filter( griddata )  # see Unser
            griddata += prefilter * (exactfit - griddata)
            prefilter = False
        self.griddata = griddata
        self.prefilter = (prefilter == True)

        self.nmap = 0
        if maps != []:  # sanity check maps --
            assert len(maps) == dim, "maps must have len %d, not %d" % (
                dim, len(maps))
            for j, (map, n, l, h) in enumerate( zip( maps, griddata.shape, lo, hi )):
                if map is None  or  callable(map):
                    lo[j], hi[j] = 0, 1
                    continue
                maps[j] = map = np.asanyarray(map)
                self.nmap += 1
                assert len(map) == n, "maps[%d] must have len %d, not %d" % (
                    j, n, len(map) )
                mlo, mhi = map.min(), map.max()
                if not (l <= mlo <= mhi <= h):
                    print "Warning: Intergrid maps[%d] min %.3g max %.3g " \
                        "are outside lo %.3g hi %.3g" % (
                        j, mlo, mhi, l, h )
        self.maps = maps
        self._lo = lo  # caller's lo for linear, 0 nonlinear maps
        shape_1 = np.array( self.griddata.shape, float ) - 1
        self._linearmap = shape_1 / np.where( hi > lo, hi - lo, np.inf )  # 25jun
Example #32
0
 def __init__(self,
              img=None,
              order=1,
              exp_numPix=601,
              numPix=241):  #Matt said the order is 1
     self.exp_numPix = exp_numPix
     self.numPix = numPix
     self.img = img
     self.dy, self.dx = img.shape
     self.x0 = self.dx / 2
     self.y0 = self.dy / 2
     self.order = order
     self.nx, self.ny = None, None  #For coordinate the pixel position
     if self.order == 1:
         self.model = self.img
     else:
         self.model = ndimage.spline_filter(self.img,
                                            output=np.float64,
                                            order=order)
Example #33
0
def work_func(args):
    frame_num, dat, setup_list, args = args
    
    setup = setupfile.eval_str_commands(setup_list)
    
    nz, ny, nx = dat.shape

#    z, y, x  = mgrid[:nz, :ny, :nx]
    y, x  = mgrid[:ny, :nx]
    
    ys = ny / float(nx)
    zs = nz / float(nx)
    #print ys, zs
    
    x0 = (x / float(nx)) * 2 - 1
    y0 = ((y / float(ny)) * 2 - 1) * ys
    #z0 = ((z / float(nz)) * 2 - 1) * zs
    
    #x = setup['x_func'](x0, y0, z0) * 0.5 + 0.5
    #y = setup['y_func'](x0, y0, z0)/ys * 0.5 + 0.5
    #z = setup['z_func'](x0, y0, z0)/zs * 0.5 + 0.5
        
    if args.order > 1:
        dat = ndimage.spline_filter(dat, args.order)

    dat2 = zeros_like(dat)
    #This avoids having to have the whole damn thing in memory        
    for zz in arange(nz):
        z0 = ((zz / float(nz)) * 2 - 1) * zs
        
        x = setup['x_func'](x0, y0, z0) * 0.5 + 0.5
        y = setup['y_func'](x0, y0, z0)/ys * 0.5 + 0.5
        z = setup['z_func'](x0, y0, z0)/zs * 0.5 + 0.5

        dat2[zz] = ndimage.map_coordinates(dat, [z*nz, y*ny, x*nx], order=args.order, prefilter=False)
    
    block = cine.make_block(dat2)
    
    #Attempt to prevent data leaks -- shouldn't really be necessary, but...
    del x, y, z, dat, dat2
    
    return (frame_num, block)
Example #34
0
 def _buildknots(self):
     if self.order > 1:
         data = ndimage.spline_filter(
             np.nan_to_num(self.image.get_data()),
             self.order)
     else:
         data = np.nan_to_num(self.image.get_data())
     if self._datafile is None:
         _, fname = tempfile.mkstemp()
         self._datafile = file(fname, mode='wb')
     else:
         self._datafile = file(self._datafile.name, 'wb')
     data = np.nan_to_num(data.astype(np.float64))
     data.tofile(self._datafile)
     datashape = data.shape
     dtype = data.dtype
     del(data)
     self._datafile.close()
     self._datafile = file(self._datafile.name)
     self.data = np.memmap(self._datafile.name, dtype=dtype,
                           mode='r+', shape=datashape)
Example #35
0
def genTheoreticalModel(md):
    global IntXVals, IntYVals, IntZVals, interpModel, dx, dy, dz

    vs = md.voxelsize_nm
    if not dx == vs.x and not dy == vs.y and not vs.z:

        IntXVals = vs.x * scipy.mgrid[-20:20]
        IntYVals = vs.y * scipy.mgrid[-20:20]
        IntZVals = vs.z * scipy.mgrid[-20:20]

        dx, dy, dz = vs

        P = scipy.arange(0, 1.01, .01)

        interpModel = genWidefieldPSF(IntXVals, IntYVals, IntZVals, P, 1e3, 0,
                                      0, 0, 2 * scipy.pi / 525, 1.47, 10e3)

        interpModel = interpModel / interpModel.max()  #normalise to 1

        #do the spline filtering here rather than in interpolation
        interpModel = ndimage.spline_filter(interpModel)
Example #36
0
 def _buildknots(self):
     if self.order > 1:
         data = ndimage.spline_filter(np.nan_to_num(self.image.get_data()),
                                      self.order)
     else:
         data = np.nan_to_num(self.image.get_data())
     if self._datafile is None:
         _, fname = tempfile.mkstemp()
         self._datafile = open(fname, mode='wb')
     else:
         self._datafile = open(self._datafile.name, 'wb')
     data = np.nan_to_num(data.astype(np.float64))
     data.tofile(self._datafile)
     datashape = data.shape
     dtype = data.dtype
     del (data)
     self._datafile.close()
     self._datafile = open(self._datafile.name)
     self.data = np.memmap(self._datafile.name,
                           dtype=dtype,
                           mode='r+',
                           shape=datashape)
Example #37
0
def plot_NOM_3D(fname):
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib import cm
    from matplotlib.ticker import LinearLocator, FormatStrFormatter

    xL, yL, zL = np.loadtxt(fname+'.dat', unpack=True)
    nX = (yL == yL[0]).sum()
    nY = (xL == xL[0]).sum()
    x = xL.reshape((nY, nX))
    y = yL.reshape((nY, nX))
    z = zL.reshape((nY, nX))
    x1D = xL[:nX]
    y1D = yL[::nX]
#    z += z[::-1, :]
    zmax = abs(z).max()

    fig = plt.figure()
    ax = fig.gca(projection='3d')
    surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap=cm.coolwarm,
                           linewidth=0, antialiased=False, alpha=0.5)
    ax.set_zlim(-zmax, zmax)
    ax.zaxis.set_major_locator(LinearLocator(10))
    ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

    fig.colorbar(surf, shrink=0.5, aspect=5)

    splineZ = ndimage.spline_filter(z.T)
    nrays = 1e3
    xnew = np.random.uniform(x1D[0], x1D[-1], nrays)
    ynew = np.random.uniform(y1D[0], y1D[-1], nrays)
    coords = np.array([(xnew-x1D[0]) / (x1D[-1]-x1D[0]) * (nX-1),
                       (ynew-y1D[0]) / (y1D[-1]-y1D[0]) * (nY-1)])
    znew = ndimage.map_coordinates(splineZ, coords, prefilter=True)
    ax.scatter(xnew, ynew, znew, c=znew, marker='o', color='gray', s=50,
               cmap=cm.coolwarm)

    fig.savefig(fname+'_3d.png')
    plt.show()
Example #38
0
 def __init__(self, coords, fld, dim, order=1):
     x0 = [None] * dim
     dx = [None] * dim
     for i in range(dim):
         if dim > 1:
             coord = coords[i]
             coord2 = coords[i]
         else:
             coord = coords
             coord2 = coords
         for j in range(dim):
             coord = coord[0]
             coord2 = coord2[np.int(i == j)]
         x0[i] = coord
         dx[i] = coord2 - coord
     self.x0 = x0
     self.dx = dx
     if order > 1:
         self.coeffs = ndimage.spline_filter(fld, order=order)
     else:
         self.coeffs = fld
     self.dim = dim
     self.order = order
Example #39
0
 def _precompute(self):
     """function which is called after model loading and can be
     overridden to allow for interpolation specific precomputations"""
      #do the spline filtering here rather than in interpolation
     self.interpModel = ndimage.spline_filter(self.interpModel)
Example #40
0
    def OnPlotProfile(self, event=None):
        lx, ly, hx, hy = self.do.GetSliceSelection()

        w = int(np.floor(0.5*self.do.selectionWidth))

        try:
            names = self.image.mdh.getEntry('ChannelNames')
        except:
            names = ['Channel %d' % d for d in range(self.image.data.shape[3])]

        try:
            voxx = self.image.mdh.getEntry('voxelsize.x')
        except:
            voxx=1

        Dx = hx - lx
        Dy = hy - ly

        l = np.sqrt((Dx**2 + Dy**2))

        dx = Dx/l
        dy = Dy/l
        
        if Dx == 0 and Dy == 0: #special case - profile is orthogonal to current plane
            d_x = w
            d_y = w
        else:
            d_x = w*abs(dy)
            d_y = w*abs(dx)

        #print lx, hx, ly, hy

        #pylab.figure()
        plots = []
        t = np.arange(np.ceil(l))

        for chanNum in range(self.image.data.shape[3]):

            x_0 = min(lx, hx)
            y_0 = min(ly, hy)

            d__x = abs(d_x) + 1
            d__y = abs(d_y) + 1

            print((dx, dy, d__x, d__y, w))

            if(self.do.slice == self.do.SLICE_XY):
                ims = self.image.data[(min(lx, hx) - d__x):(max(lx,hx)+d__x+1), (min(ly, hy)-d__y):(max(ly,hy)+d__y+1), self.do.zp, chanNum].squeeze()

            splf = ndimage.spline_filter(ims) 

            p = np.zeros(len(t))


            x_c = t*dx + lx - x_0
            y_c = t*dy + ly - y_0

            print((splf.shape))


            for i in range(-w, w+1):
                #print np.vstack([x_c + d__x +i*dy, y_c + d__y + i*dx])
                p += ndimage.map_coordinates(splf, np.vstack([x_c + d__x +i*dy, y_c + d__y - i*dx]), prefilter=False)

            p = p/(2*w + 1)



            plots.append(p.reshape(-1, 1,1))

        #pylab.legend(names)

        im = ImageStack(plots, titleStub='New Profile')
        im.xvals = t*voxx

        if not voxx == 1:
            im.xlabel = 'Distance [um]'
        else:
            im.xlabel = 'Distance [pixels]'

        im.ylabel = 'Intensity'
        im.defaultExt = '.txt'

        im.mdh['voxelsize.x'] = voxx
        im.mdh['ChannelNames'] = names
        im.mdh['Profile.XValues'] = im.xvals
        im.mdh['Profile.XLabel'] = im.xlabel
        im.mdh['Profile.YLabel'] = im.ylabel
        im.mdh['Profile.StartX'] = lx
        im.mdh['Profile.StartY'] = ly
        im.mdh['Profile.EndX'] = hx
        im.mdh['Profile.EndY'] = hy
        im.mdh['Profile.Width'] = 2*w + 1

        im.mdh['OriginalImage'] = self.image.filename

        ViewIm3D(im, mode='graph', parent=wx.GetTopLevelParent(self.dsviewer))
Example #41
0
dx = x[1,0] - x0
dy = y[0,1] - y0
ivals = (newx - x0)/dx
jvals = (newy - y0)/dy
coords = array([ivals, jvals])
newf = ndimage.map_coordinates(fvals, coords)

# <markdowncell>

# To pre-compute the weights (for multiple interpolation results), you
# would use
# 
# <codecell>


coeffs = ndimage.spline_filter(fvals)
newf = ndimage.map_coordinates(coeffs, coords, prefilter=False)

# <markdowncell>

# ![](files/Interpolation_attachments/interpolate_figure2.png
# 
# Interpolation of an N-D curve
# =============================
# 
# The scipy.interpolate packages wraps the netlib FITPACK routines
# (Dierckx) for calculating smoothing splines for various kinds of data
# and geometries. Although the data is evenly spaced in this example, it
# need not be so to use this routine.
# 
# <codecell>
    def __init__(self,data,theta,order=1,mode='log'):

        """Initialize an interpolator object.

        Parameters
        ----------
        data : n-dim array or 1-d array
            Datacube to be interpolated. Sampled on a rectilinear grid
            (it need not be regular!). 'data' is either an
            n-dimensional array (hypercube), or a 1-dimensional
            array. If hypercube, each axis corresponds to one of the
            model parameters, and the index location along each axis
            grows with the parameter value (the parameter values are
            given in `theta`). If 'data' is a 1-d array of values, it
            will be converted into the hypercube format. This means
            that the order of entries in the 1-d array must be as if
            constructed via looping over all axes, i.e.

            .. code:: python

                counter = 0
                for j0 in theta[0]:
                    for j1 in theta[1]:
                        for j2 in theta[2]:
                            ...
                            hypercube[j0,j1,j2,...] = onedarray[counter]
                            counter += 1

        theta : list
            List of lists, each holding in ascending order the unique
            values for one of the axes in `data` hypercube. Example:
            for the CLUMPY models of AGN tori (Nenkova et al. 2008)

              theta = [{i}, {tv}, {q}, {N0}, {sig}, {Y}, {wave}]

            where the {.} are 1-d arrays of unique model parameter
            values, e.g.

              {i} = array([0,10,20,30,40,50,60,70,80,90]) (degrees).

        order : int
            Order of interpolation spline to be used. ``order=1``
            (default) is multi-linear interpolation, ``order=3`` is
            cubic-spline (quite a bit slower, and not necessarily
            better, especially for complicated n-dim
            functions. ``order=1`` is recommended.

        mode : str
            ``log`` is default, and will take log10(data) first, which
            severely improves the interpolation accuracy if the data
            span many orders of magnitude. This is of course only
            applicable if all entries in `data` are greater than
            0. Any string other that ``log`` will keep `data` as-is.

        Returns
        -------
        NdimInterpolation instance.

        Example
        -------
        General way to use ndiminterpolation

        .. code:: python

            # to be written

        """

        self.theta = copy(theta) # list of lists of parameter values, unique, in correct order

        if not isinstance(self.theta,(list,tuple)):
            self.theta = [self.theta]
        
        shape_ = tuple([len(t) for t in self.theta])

        # determine if data is hypercube or list of 1d arrays
        if shape_ == data.shape:
            self.data_hypercube = data
        else:
            raise Exception("'theta' not compatible with the shape of 'data'.")

        # interpolation orders
        if order in (1,3):
            self.order = order
        else:
            raise Exception("Interpolation spline order not supported! Must be 1 (linear) or 3 (cubic).")

        # interpolate in log10 space?
        self.mode = mode

        # take log10 of 'data' ('y' values)
        if self.mode in ('log','loglog'):
            try:
                self.data_hypercube = np.log10(self.data_hypercube)
            except RuntimeWarning:
                raise Exception("For mode='log' all entries in 'data' must be > 0.")

        # take log10 of 'theta' ('x' values)
        if self.mode == 'loglog':
            for jt,t in enumerate(self.theta):
                try:
                    self.theta[jt] = np.log10(t)
                except:
                    raise # Exception

        # set up n 1-d linear interpolators for all n parameters in theta
        self.ips = [] # list of 1-d interpolator objects
        for t in self.theta:
            self.ips.append(interpolate.interp1d(t,np.linspace(0.,float(t.size-1.),t.size)))

        if self.order == 3:
            print("Evaluating cubic spline coefficients for subsequent use, please wait...")
            self.coeffs = ndimage.spline_filter(self.data_hypercube,order=3)
            print("Done.")
Example #43
0
 def createModel(self,order=1):
     if order==1:
         self.model = self.image.copy()
     else:
         self.model = ndimage.spline_filter(self.image,output=numpy.float64,order=order)
     self.order = order
Example #44
0
def map_coordinates_parallel(input, coordinates, output=None, order=3, mode='constant', cval=0.0,
                             prefilter=True, chunklen=None, threads=None):
    """

    Parallalized version of `scipy.ndimage.map_coordinates`.

    `scipy.ndimage.map_coordinates` is slow for large datasets. Speed improvement can be
    achieved by

     * Splitting the data into chunks
     * Performing the transformation of chunks in parallel

    New parameters:

    chunklen: Size of the chunks in pixels per axis. Default: None
        Special values:
            None: Automatic (Chooses a default based on number of dimensions)
            0: Do not split data into chunks. (implicitly sets threads==1)

    threads: Number of threads. Default: None
        None: Automatic (One thread per available processing unit)

    """

    # this part is taken without change from scipy's serial implementation
    if order < 0 or order > 5:
        raise RuntimeError('spline order not supported')
    input = np.asarray(input)
    if np.iscomplexobj(input):
        raise TypeError('Complex type not supported')
    coordinates = np.asarray(coordinates)
    if np.iscomplexobj(coordinates):
        raise TypeError('Complex type not supported')
    output_shape = coordinates.shape[1:]
    if input.ndim < 1 or len(output_shape) < 1:
        raise RuntimeError('input and output rank must be > 0')
    if coordinates.shape[0] != input.ndim:
        raise RuntimeError('invalid shape for coordinate array')
    mode = _ni_support._extend_mode_to_code(mode)
    if prefilter and order > 1:
        filtered = spline_filter(input, order, output=np.float64)
    else:
        filtered = input

    # return value of `_ni_support._get_output` changed between scipy versions, code here is
    # adapted to work with both
    output = _ni_support._get_output(output, input, shape=output_shape)
    retval = output
    if isinstance(output, tuple):
        output, retval = output

    # below here there is the new code for splitting into chunks and parallel execution
    if chunklen is None:
        # set defaults
        chunklen = 128
        if output.ndim < 3:
            chunklen = 1024

    def chunk_arguments(filtered, coordinates, output):
        chunks = []
        for axis in range(output.ndim):
            chunkstarts = np.arange(0, output.shape[axis], chunklen)
            chunkends = chunkstarts + chunklen
            chunkends[-1] = output.shape[axis]
            chunks.append([slice(start, stop) for start, stop in zip(chunkstarts, chunkends)])

        for chunk in itertools.product(*chunks):
            sub_coordinates = coordinates[(slice(None),) + chunk].copy()
            filtered_region = []
            for in_axis in range(filtered.ndim):
                c = sub_coordinates[in_axis, ...]
                cmin = max(0, int(np.floor(np.min(c)))-5)
                cmax = min(filtered.shape[in_axis], int(np.ceil(np.max(c)))+5)
                sub_coordinates[in_axis, ...] -= cmin
                filtered_region.append(slice(cmin, cmax))
            sub_filtered = filtered[tuple(filtered_region)]
            sub_output = output[chunk]

            yield (sub_filtered, sub_coordinates, sub_output)

    def map_coordinates_chunk(arg):
        sub_filtered, sub_coordinates, sub_output = arg
        _nd_image.geometric_transform(sub_filtered, None, sub_coordinates, None, None,
                                      sub_output, order, mode, cval, None, None)

    if chunklen > 0:
        list_of_chunk_args = list(chunk_arguments(filtered, coordinates, output))
    else:
        list_of_chunk_args = [(filtered, coordinates, output)]

    if len(list_of_chunk_args) == 1:
        threads = 1

    if threads != 1:
        threadpool = ThreadPoolExecutor(threads)
        my_map = threadpool.map
    else:
        my_map = map

    # execution happens here
    list(my_map(map_coordinates_chunk, list_of_chunk_args))

    if threads != 1:
        if have_concurrent_futures:
            threadpool.shutdown()
        else:
            threadpool.close()
            threadpool.join()

    return retval
Example #45
0
 def _calc_spline(self):
     padded_values = np.pad(self[:, :, :, 0], ((self.spl_order, )),
                            mode='wrap')
     self.spl_coeffs = ndimage.spline_filter(padded_values,
                                             order=self.spl_order)
     return
Example #46
0
    def __init__(self,pot=None,delta=None,Rmax=5.,
                 nE=25,npsi=25,nLz=30,numcores=1,
                 interpecc=False,
                 **kwargs):
        """
        NAME:
           __init__
        PURPOSE:
           initialize an actionAngleStaeckelGrid object
        INPUT:
           pot= potential or list of potentials

           delta= focus of prolate confocal coordinate system (can be Quantity)

           Rmax = Rmax for building grids (natural units)

           nE=, npsi=, nLz= grid size

           interpecc= (False) if True, also interpolate the approximate eccentricity, zmax, rperi, and rapo

           numcores= number of cpus to use to parallellize

           ro= distance from vantage point to GC (kpc; can be Quantity)

           vo= circular velocity at ro (km/s; can be Quantity)

        OUTPUT:
         
           instance

        HISTORY:

            2012-11-29 - Written - Bovy (IAS)

            2017-12-15 - Written - Bovy (UofT)

        """
        actionAngle.__init__(self,
                             ro=kwargs.get('ro',None),vo=kwargs.get('vo',None))
        if pot is None:
            raise IOError("Must specify pot= for actionAngleStaeckelGrid")
        self._pot= flatten_potential(pot)
        if delta is None:
            raise IOError("Must specify delta= for actionAngleStaeckelGrid")
        if ext_loaded and 'c' in kwargs and kwargs['c']:
            self._c= True
        else:
            self._c= False
        self._delta= conversion.parse_length(delta,ro=self._ro)
        self._Rmax= Rmax
        self._Rmin= 0.01
        #Set up the actionAngleStaeckel object that we will use to interpolate
        self._aA= actionAngleStaeckel.actionAngleStaeckel(pot=self._pot,delta=self._delta,c=self._c)
        #Build grid
        self._Lzmin= 0.01
        self._Lzs= numpy.linspace(self._Lzmin,
                                  self._Rmax\
                                      *potential.vcirc(self._pot,self._Rmax),
                                  nLz)
        self._Lzmax= self._Lzs[-1]
        self._nLz= nLz
        #Calculate E_c(R=RL), energy of circular orbit
        self._RL= numpy.array([potential.rl(self._pot,l) for l in self._Lzs])
        self._RLInterp= interpolate.InterpolatedUnivariateSpline(self._Lzs,
                                                                 self._RL,k=3)
        self._ERL= _evaluatePotentials(self._pot,self._RL,
                                       numpy.zeros(self._nLz))\
                                       +self._Lzs**2./2./self._RL**2.
        self._ERLmax= numpy.amax(self._ERL)+1.
        self._ERLInterp= interpolate.InterpolatedUnivariateSpline(self._Lzs,
                                                                  numpy.log(-(self._ERL-self._ERLmax)),k=3)
        self._Ramax= 200./8.
        self._ERa= _evaluatePotentials(self._pot,self._Ramax,0.) +self._Lzs**2./2./self._Ramax**2.
        #self._EEsc= numpy.array([self._ERL[ii]+potential.vesc(self._pot,self._RL[ii])**2./4. for ii in range(nLz)])
        self._ERamax= numpy.amax(self._ERa)+1.
        self._ERaInterp= interpolate.InterpolatedUnivariateSpline(self._Lzs,
                                                                  numpy.log(-(self._ERa-self._ERamax)),k=3)
        y= numpy.linspace(0.,1.,nE)
        self._nE= nE
        psis= numpy.linspace(0.,1.,npsi)*numpy.pi/2.
        self._npsi= npsi
        jr= numpy.zeros((nLz,nE,npsi))
        jz= numpy.zeros((nLz,nE,npsi))
        u0= numpy.zeros((nLz,nE))
        jrLzE= numpy.zeros((nLz))
        jzLzE= numpy.zeros((nLz))
        #First calculate u0
        thisLzs= (numpy.tile(self._Lzs,(nE,1)).T).flatten()
        thisERL= (numpy.tile(self._ERL,(nE,1)).T).flatten()
        thisERa= (numpy.tile(self._ERa,(nE,1)).T).flatten()
        thisy= (numpy.tile(y,(nLz,1))).flatten()
        thisE= _invEfunc(_Efunc(thisERa,thisERL)+thisy*(_Efunc(thisERL,thisERL)-_Efunc(thisERa,thisERL)),thisERL)
        if isinstance(self._pot,potential.interpRZPotential) and hasattr(self._pot,'_origPot'):
            u0pot= self._pot._origPot
        else:
            u0pot= self._pot
        if self._c:
            mu0= actionAngleStaeckel_c.actionAngleStaeckel_calcu0(thisE,thisLzs,
                                                                  u0pot,
                                                                  self._delta)[0]
        else:
            if numcores > 1:
                mu0= multi.parallel_map((lambda x: self.calcu0(thisE[x],
                                                               thisLzs[x])),
                                        range(nE*nLz),
                                        numcores=numcores)
            else:
                mu0= list(map((lambda x: self.calcu0(thisE[x],
                                                     thisLzs[x])),
                              range(nE*nLz)))
        u0= numpy.reshape(mu0,(nLz,nE))
        thisR= self._delta*numpy.sinh(u0)
        thisv= numpy.reshape(self.vatu0(thisE.flatten(),thisLzs.flatten(),
                                        u0.flatten(),
                                        thisR.flatten()),(nLz,nE))
        self.thisv= thisv
        #reshape
        thisLzs= numpy.reshape(thisLzs,(nLz,nE))
        thispsi= numpy.tile(psis,(nLz,nE,1)).flatten()
        thisLzs= numpy.tile(thisLzs.T,(npsi,1,1)).T.flatten()
        thisR= numpy.tile(thisR.T,(npsi,1,1)).T.flatten()
        thisv= numpy.tile(thisv.T,(npsi,1,1)).T.flatten()
        mjr, mlz, mjz= self._aA(thisR, #R
                                thisv*numpy.cos(thispsi), #vR
                                thisLzs/thisR, #vT
                                numpy.zeros(len(thisR)), #z
                                thisv*numpy.sin(thispsi), #vz
                                fixed_quad=True) 
        if interpecc:
            mecc, mzmax, mrperi, mrap=\
                self._aA.EccZmaxRperiRap(thisR, #R
                                         thisv*numpy.cos(thispsi), #vR
                                         thisLzs/thisR, #vT
                                         numpy.zeros(len(thisR)), #z
                                         thisv*numpy.sin(thispsi)) #vz
        if isinstance(self._pot,potential.interpRZPotential) and hasattr(self._pot,'_origPot'):
            #Interpolated potentials have problems with extreme orbits
            indx= (mjr == 9999.99)
            indx+= (mjz == 9999.99)
            #Re-calculate these using the original potential, hopefully not too slow
            tmpaA= actionAngleStaeckel.actionAngleStaeckel(pot=self._pot._origPot,delta=self._delta,c=self._c)
            mjr[indx], dum, mjz[indx]= tmpaA(thisR[indx], #R
                                             thisv[indx]*numpy.cos(thispsi[indx]), #vR
                                             thisLzs[indx]/thisR[indx], #vT
                                             numpy.zeros(numpy.sum(indx)), #z
                                             thisv[indx]*numpy.sin(thispsi[indx]), #vz
                                             fixed_quad=True)
            if interpecc:
                mecc[indx], mzmax[indx], mrperi[indx], mrap[indx]=\
                    self._aA.EccZmaxRperiRap(thisR[indx], #R
                                             thisv[indx]*numpy.cos(thispsi[indx]), #vR
                                             thisLzs[indx]/thisR[indx], #vT
                                             numpy.zeros(numpy.sum(indx)), #z
                                             thisv[indx]*numpy.sin(thispsi[indx])) #vz
        jr= numpy.reshape(mjr,(nLz,nE,npsi))
        jz= numpy.reshape(mjz,(nLz,nE,npsi))
        if interpecc:
            ecc= numpy.reshape(mecc,(nLz,nE,npsi))
            zmax= numpy.reshape(mzmax,(nLz,nE,npsi))
            rperi= numpy.reshape(mrperi,(nLz,nE,npsi))
            rap= numpy.reshape(mrap,(nLz,nE,npsi))
            zmaxLzE= numpy.zeros((nLz))
            rperiLzE= numpy.zeros((nLz))
            rapLzE= numpy.zeros((nLz))
        for ii in range(nLz):
            jrLzE[ii]= numpy.nanmax(jr[ii,(jr[ii,:,:] != 9999.99)])#:,:])
            jzLzE[ii]= numpy.nanmax(jz[ii,(jz[ii,:,:] != 9999.99)])#:,:])
            if interpecc:
                zmaxLzE[ii]= numpy.amax(zmax[ii,numpy.isfinite(zmax[ii])])
                rperiLzE[ii]= numpy.amax(rperi[ii,numpy.isfinite(rperi[ii])])
                rapLzE[ii]= numpy.amax(rap[ii,numpy.isfinite(rap[ii])])
        jrLzE[(jrLzE == 0.)]= numpy.nanmin(jrLzE[(jrLzE > 0.)])
        jzLzE[(jzLzE == 0.)]= numpy.nanmin(jzLzE[(jzLzE > 0.)])
        if interpecc:
            zmaxLzE[(zmaxLzE == 0.)]= numpy.nanmin(zmaxLzE[(zmaxLzE > 0.)])
            rperiLzE[(rperiLzE == 0.)]= numpy.nanmin(rperiLzE[(rperiLzE > 0.)])
            rapLzE[(rapLzE == 0.)]= numpy.nanmin(rapLzE[(rapLzE > 0.)])
        for ii in range(nLz):
            jr[ii,:,:]/= jrLzE[ii]
            jz[ii,:,:]/= jzLzE[ii]
            if interpecc:
                zmax[ii,:,:]/= zmaxLzE[ii]
                rperi[ii,:,:]/= rperiLzE[ii]
                rap[ii,:,:]/= rapLzE[ii]
        #Deal w/ 9999.99
        jr[(jr > 1.)]= 1.
        jz[(jz > 1.)]= 1.
        #Deal w/ NaN
        jr[numpy.isnan(jr)]= 0.
        jz[numpy.isnan(jz)]= 0.
        if interpecc:
            ecc[(ecc < 0.)]= 0.
            ecc[(ecc > 1.)]= 1.
            ecc[numpy.isnan(ecc)]= 0.
            ecc[numpy.isinf(ecc)]= 1.
            zmax[(zmax > 1.)]= 1.
            zmax[numpy.isnan(zmax)]= 0.
            zmax[numpy.isinf(zmax)]= 1.
            rperi[(rperi > 1.)]= 1.
            rperi[numpy.isnan(rperi)]= 0.
            rperi[numpy.isinf(rperi)]= 0. # typically orbits that can reach 0
            rap[(rap > 1.)]= 1.
            rap[numpy.isnan(rap)]= 0.
            rap[numpy.isinf(rap)]= 1.
        #First interpolate the maxima
        self._jr= jr
        self._jz= jz
        self._u0= u0
        self._jrLzE= jrLzE
        self._jzLzE= jzLzE
        self._jrLzInterp= interpolate.InterpolatedUnivariateSpline(self._Lzs,
                                                                   numpy.log(jrLzE+10.**-5.),k=3)
        self._jzLzInterp= interpolate.InterpolatedUnivariateSpline(self._Lzs,
                                                                   numpy.log(jzLzE+10.**-5.),k=3)
        if interpecc:
            self._ecc= ecc
            self._zmax= zmax
            self._rperi= rperi
            self._rap= rap
            self._zmaxLzE= zmaxLzE
            self._rperiLzE= rperiLzE
            self._rapLzE= rapLzE
            self._zmaxLzInterp=\
                interpolate.InterpolatedUnivariateSpline(self._Lzs,
                                                         numpy.log(zmaxLzE+10.**-5.),k=3)
            self._rperiLzInterp=\
                interpolate.InterpolatedUnivariateSpline(self._Lzs,
                                                         numpy.log(rperiLzE+10.**-5.),k=3)
            self._rapLzInterp=\
                interpolate.InterpolatedUnivariateSpline(self._Lzs,
                                                         numpy.log(rapLzE+10.**-5.),k=3)
        #Interpolate u0
        self._logu0Interp= interpolate.RectBivariateSpline(self._Lzs,
                                                           y,
                                                           numpy.log(u0),
                                                           kx=3,ky=3,s=0.)
        #spline filter jr and jz, such that they can be used with ndimage.map_coordinates
        self._jrFiltered= ndimage.spline_filter(numpy.log(self._jr+10.**-10.),order=3)
        self._jzFiltered= ndimage.spline_filter(numpy.log(self._jz+10.**-10.),order=3)
        if interpecc:
            self._eccFiltered= ndimage.spline_filter(numpy.log(self._ecc+10.**-10.),order=3)
            self._zmaxFiltered= ndimage.spline_filter(numpy.log(self._zmax+10.**-10.),order=3)
            self._rperiFiltered= ndimage.spline_filter(numpy.log(self._rperi+10.**-10.),order=3)
            self._rapFiltered= ndimage.spline_filter(numpy.log(self._rap+10.**-10.),order=3)
        # Check the units
        self._check_consistent_units()
        return None
    def prepare_batches(self,
                        sample_rate,
                        frame_len,
                        fps,
                        mel_bands,
                        mel_min,
                        mel_max,
                        blocklen,
                        batchsize,
                        batch_data=True):

        bin_nyquist = frame_len // 2 + 1
        bin_mel_max = bin_nyquist * 2 * mel_max // sample_rate
        spects = []
        for fn in progress(self.filelist, 'File'):
            cache_fn = (self.featuredir
                        and os.path.join(self.featuredir, fn + '.npy'))
            spects.append(
                cached(cache_fn, audio.extract_spect,
                       os.path.join(self.datadir, 'audio', fn), sample_rate,
                       frame_len, fps))
        # - load and convert corresponding labels
        print("Loading labels...")
        labels = []
        for fn, spect in zip(self.filelist, spects):
            fn = os.path.join(self.datadir, 'labels',
                              fn.rsplit('.', 1)[0] + '.lab')
            with io.open(fn) as f:
                segments = [l.rstrip().split() for l in f if l.rstrip()]
            segments = [(float(start), float(end), label == 'sing')
                        for start, end, label in segments]
            timestamps = np.arange(len(spect)) / float(fps)
            labels.append(create_aligned_targets(segments, timestamps,
                                                 np.bool))

        if (self.input_type == 'stft'):
            print('Create dataset with stft output')
            if (batch_data):
                batches = augment.grab_random_excerpts(spects, labels,
                                                       batchsize, blocklen)
                return batches
            else:
                return spects, labels

        if (self.input_type == 'mel_spects'
                or self.input_type == 'mel_spects_norm'):
            # - prepare mel filterbank
            filterbank = audio.create_mel_filterbank(sample_rate, frame_len,
                                                     mel_bands, mel_min,
                                                     mel_max)
            filterbank = filterbank[:bin_mel_max].astype(floatX)

            # - precompute mel spectra, if needed, otherwise just define a generator
            mel_spects = (np.log(
                np.maximum(np.dot(spect[:, :bin_mel_max], filterbank), 1e-7))
                          for spect in spects)

            if not self.augment:
                mel_spects = list(mel_spects)
                del spects

            # - load mean/std or compute it, if not computed yet
            meanstd_file = os.path.join(os.path.dirname(__file__),
                                        '%s_meanstd.npz' % self.dataset)
            try:
                with np.load(meanstd_file) as f:
                    mean = f['mean']
                    std = f['std']
            except (IOError, KeyError):
                print("Computing mean and standard deviation...")
                mean, std = znorm.compute_mean_std(mel_spects)
                np.savez(meanstd_file, mean=mean, std=std)
            mean = mean.astype(floatX)
            istd = np.reciprocal(std).astype(floatX)
            #print(meanstd_file,mean, istd)
            #input('wait')
            #print(znorm.compute_mean_std(mel_spects))
            #input('wait')
        # - prepare training data generator
        print("Preparing training data feed...")
        if not self.augment:
            # Without augmentation, we just precompute the normalized mel spectra
            # and create a generator that returns mini-batches of random excerpts
            if (self.input_type == 'mel_spects'):
                print('Creating batches of mel spects without znorm')
                if (batch_data):
                    batches = augment.grab_random_excerpts(
                        mel_spects, labels, batchsize, blocklen)
                else:
                    pad = np.tile((np.log(1e-7).astype(floatX)),
                                  (blocklen // 2, 80))
                    mel_spects = (np.concatenate((pad, spect, pad), axis=0)
                                  for spect in mel_spects)
                    mel_spects = list(mel_spects)
                    return mel_spects, labels
            elif (self.input_type == 'mel_spects_norm'):
                print('Creating batches of mel spects with znorm')
                #mel_spects = [(spect - mean) * istd for spect in mel_spects]
                if (batch_data):
                    mel_spects = [(spect - mean) * istd
                                  for spect in mel_spects]
                    batches = augment.grab_random_excerpts(
                        mel_spects, labels, batchsize, blocklen)
                else:
                    #pad = np.tile((np.log(1e-7)-mean)*istd, (blocklen//2, 1))
                    #input(pad)
                    pad = np.tile((np.log(1e-7).astype(floatX)),
                                  (blocklen // 2, 80))
                    mel_spects = (np.concatenate((pad, spect, pad), axis=0)
                                  for spect in mel_spects)
                    mel_spects = [(spect - mean) * istd
                                  for spect in mel_spects]
                    #input(mean.shape)
                    #mel_spects = [(spect - np.zeros(80)) * np.ones(80) for spect in mel_spects]

                    mel_spects = list(mel_spects)

                    return mel_spects, labels
            else:
                print('Creating batches of stfts')
                batches = augment.grab_random_excerpts(spects, labels,
                                                       batchsize, blocklen)
        else:
            # For time stretching and pitch shifting, it pays off to preapply the
            # spline filter to each input spectrogram, so it does not need to be
            # applied to each mini-batch later.
            spline_order = cfg['spline_order']
            if spline_order > 1:
                from scipy.ndimage import spline_filter
                spects = [
                    spline_filter(spect, spline_order).astype(floatX)
                    for spect in spects
                ]

            # We define a function to create the mini-batch generator. This allows
            # us to easily create multiple generators for multithreading if needed.
            def create_datafeed(spects, labels):
                # With augmentation, as we want to apply random time-stretching,
                # we request longer excerpts than we finally need to return.
                max_stretch = cfg['max_stretch']
                batches = augment.grab_random_excerpts(
                    spects,
                    labels,
                    batchsize=batchsize,
                    frames=int(blocklen / (1 - max_stretch)))

                # We wrap the generator in another one that applies random time
                # stretching and pitch shifting, keeping a given number of frames
                # and bins only.
                max_shift = cfg['max_shift']
                batches = augment.apply_random_stretch_shift(
                    batches,
                    max_stretch,
                    max_shift,
                    keep_frames=blocklen,
                    keep_bins=bin_mel_max,
                    order=spline_order,
                    prefiltered=True)

                # We transform the excerpts to mel frequency and log magnitude.
                batches = augment.apply_filterbank(batches, filterbank)
                batches = augment.apply_logarithm(batches)

                # We apply random frequency filters
                max_db = cfg['max_db']
                batches = augment.apply_random_filters(batches,
                                                       filterbank,
                                                       mel_max,
                                                       max_db=max_db)

                # We apply normalization
                batches = augment.apply_znorm(batches, mean, istd)

                return batches

            # We start the mini-batch generator and augmenter in one or more
            # background threads or processes (unless disabled).
            bg_threads = cfg['bg_threads']
            bg_processes = cfg['bg_processes']
            if not bg_threads and not bg_processes:
                # no background processing: just create a single generator
                batches = create_datafeed(spects, labels)
            elif bg_threads:
                # multithreading: create a separate generator per thread
                batches = augment.generate_in_background(
                    [
                        create_datafeed(spects, labels)
                        for _ in range(bg_threads)
                    ],
                    num_cached=bg_threads * 5)
            elif bg_processes:
                # multiprocessing: single generator is forked along with processes
                batches = augment.generate_in_background(
                    [create_datafeed(spects, labels)] * bg_processes,
                    num_cached=bg_processes * 25,
                    in_processes=True)

        return batches
Example #48
0
def make_polychrome(lam1,
                    lam2,
                    hires_arrs,
                    lam_arr,
                    psftool,
                    allcoef,
                    xindx,
                    yindx,
                    upsample=5,
                    nlam=10,
                    trans=None):
    """
    """

    padding = 10
    image = np.zeros((2048 + 2 * padding, 2048 + 2 * padding))
    x = np.arange(image.shape[0])
    x, y = np.meshgrid(x, x)
    npix = hires_arrs[0].shape[2] // upsample

    dloglam = (np.log(lam2) - np.log(lam1)) / nlam
    loglam = np.log(lam1) + dloglam / 2. + np.arange(nlam) * dloglam

    for lam in np.exp(loglam):

        if trans is not None:
            indx = np.where(np.abs(np.log(trans[:, 0] / lam)) < dloglam / 2.)
            meantrans = np.mean(trans[:, 1][indx])

        ################################################################
        # Build the appropriate average hires image by averaging over
        # the nearest wavelengths.  Then apply a spline filter to the
        # interpolated high resolution PSFlet images to avoid having
        # to do this later, saving a factor of a few in time.
        ################################################################

        hires = np.zeros((hires_arrs[0].shape))
        if lam <= np.amin(lam_arr):
            hires[:] = hires_arrs[0]
        elif lam >= np.amax(lam_arr):
            hires[:] = hires_arrs[-1]
        else:
            i1 = np.amax(np.arange(len(lam_arr))[np.where(lam > lam_arr)])
            i2 = i1 + 1
            hires = hires_arrs[i1] * (lam - lam_arr[i1]) / (lam_arr[i2] -
                                                            lam_arr[i1])
            hires += hires_arrs[i2] * (lam_arr[i2] - lam) / (lam_arr[i2] -
                                                             lam_arr[i1])

        for i in range(hires.shape[0]):
            for j in range(hires.shape[1]):
                hires[i, j] = ndimage.spline_filter(hires[i, j])

        ################################################################
        # Run through lenslet centroids at this wavelength using the
        # fitted coefficients in psftool to get the centroids.  For
        # each centroid, compute the weights for the four nearest
        # regions on which the high-resolution PSFlets have been made.
        # Interpolate the high-resolution PSFlets and take their
        # weighted average, adding this to the image in the
        # appropriate place.
        ################################################################

        xcen, ycen = psftool.return_locations(lam, allcoef, xindx, yindx)
        xcen += padding
        ycen += padding
        xcen = np.reshape(xcen, -1)
        ycen = np.reshape(ycen, -1)
        for i in range(xcen.shape[0]):
            if not (xcen[i] > npix // 2
                    and xcen[i] < image.shape[0] - npix // 2
                    and ycen[i] > npix // 2
                    and ycen[i] < image.shape[0] - npix // 2):
                continue

            # central pixel -> npix*upsample//2
            iy1 = int(ycen[i]) - npix // 2
            iy2 = iy1 + npix
            ix1 = int(xcen[i]) - npix // 2
            ix2 = ix1 + npix
            yinterp = (y[iy1:iy2, ix1:ix2] -
                       ycen[i]) * upsample + upsample * npix / 2
            xinterp = (x[iy1:iy2, ix1:ix2] -
                       xcen[i]) * upsample + upsample * npix / 2
            # Now find the closest high-resolution PSFs

            x_hires = xcen[i] * 1. / image.shape[1]
            y_hires = ycen[i] * 1. / image.shape[0]

            x_hires = x_hires * hires_arrs[0].shape[1] - 0.5
            y_hires = y_hires * hires_arrs[0].shape[0] - 0.5

            totweight = 0

            if x_hires <= 0:
                i1 = i2 = 0
            elif x_hires >= hires_arrs[0].shape[1] - 1:
                i1 = i2 = hires_arrs[0].shape[1] - 1
            else:
                i1 = int(x_hires)
                i2 = i1 + 1

            if y_hires < 0:
                j1 = j2 = 0
            elif y_hires >= hires_arrs[0].shape[0] - 1:
                j1 = j2 = hires_arrs[0].shape[0] - 1
            else:
                j1 = int(y_hires)
                j2 = j1 + 1

            ##############################################################
            # Bilinear interpolation by hand.  Do not extrapolate, but
            # instead use the nearest PSFlet near the edge of the
            # image.  The outer regions will therefore have slightly
            # less reliable PSFlet reconstructions.  Then take the
            # weighted average of the interpolated PSFlets.
            ##############################################################

            weight22 = max(0, (x_hires - i1) * (y_hires - j1))
            weight12 = max(0, (x_hires - i1) * (j2 - y_hires))
            weight21 = max(0, (i2 - x_hires) * (y_hires - j1))
            weight11 = max(0, (i2 - x_hires) * (j2 - y_hires))
            totweight = weight11 + weight21 + weight12 + weight22
            weight11 /= totweight * nlam
            weight12 /= totweight * nlam
            weight21 /= totweight * nlam
            weight22 /= totweight * nlam

            if trans is not None:
                weight11 *= meantrans
                weight12 *= meantrans
                weight21 *= meantrans
                weight22 *= meantrans

            image[iy1:iy2, ix1:ix2] += weight11 * ndimage.map_coordinates(
                hires[j1, i1], [yinterp, xinterp], prefilter=False)
            image[iy1:iy2, ix1:ix2] += weight12 * ndimage.map_coordinates(
                hires[j1, i2], [yinterp, xinterp], prefilter=False)
            image[iy1:iy2, ix1:ix2] += weight21 * ndimage.map_coordinates(
                hires[j2, i1], [yinterp, xinterp], prefilter=False)
            image[iy1:iy2, ix1:ix2] += weight22 * ndimage.map_coordinates(
                hires[j2, i2], [yinterp, xinterp], prefilter=False)

    image = image[padding:-padding, padding:-padding]
    return image
Example #49
0
def plot_NOM_2D(fname):

    xL, yL, zL = np.loadtxt(fname+'.dat', unpack=True)
    nX = (yL == yL[0]).sum()
    nY = (xL == xL[0]).sum()
    x = xL[:nX]
    y = yL[::nX]
    print(nX, nY)
    z = zL.reshape((nY, nX))

    zmax = abs(z).max()
    print(z.shape)
#    print(zmax)

    fig = plt.figure(figsize=(16, 8))
    rect_2D = [0.1, 0.1, 0.72, 0.6]
    rect_1Dx = [0.1, 0.72, 0.72, 0.26]
    rect_1Dy = [0.83, 0.1, 0.13, 0.6]
    extent = [x[0], x[-1], y[0], y[-1]]
    ax2D = plt.axes(rect_2D)
    ax2D.set_xlabel('x (mm)')
    ax2D.set_ylabel('y (mm)')
    ax2D.imshow(
        z, aspect='auto', cmap='jet', extent=extent,
#        interpolation='nearest',
        interpolation='none',
        origin='lower', figure=fig)

    ax1Dx = plt.axes(rect_1Dx, sharex=ax2D)
    ax1Dy = plt.axes(rect_1Dy, sharey=ax2D)
    ax1Dx.set_ylabel('h (nm)')
    ax1Dy.set_xlabel('h (nm)')
    plt.setp(ax1Dx.get_xticklabels() + ax1Dy.get_yticklabels(),
             visible=False)
#    ax1Dx.plot(x, x*0, 'gray')
    kl, = ax1Dx.plot(x, z.sum(axis=0)/nY, 'k')
    ax1Dx.plot(x, z[0, :], 'r')
    ax1Dx.plot(x, z[nY//2, :], 'g')
    ax1Dx.plot(x, z[nY-1, :], 'b')
    ax1Dx.legend([kl], ['average over y'], loc='upper left', frameon=False)

#    ax1Dy.plot(y*0, y, 'gray')
    ax1Dy.plot(z.sum(axis=1)/nX, y, 'k')
    ax1Dy.plot(z[:, 0], y, 'y')
    ax1Dy.plot(z[:, nX//2], y, 'c')
    ax1Dy.plot(z[:, nX-1], y, 'm')

    ax2D.set_xlim(extent[0], extent[1])
    ax2D.set_ylim(extent[2], extent[3])
    ax1Dx.set_ylim(-zmax, zmax)
    ax1Dy.set_xlim(-zmax, zmax)

    ax2D.annotate('', (0, 0), (-0.03, 0), size=10,
                  xycoords="axes fraction",
                  arrowprops=dict(alpha=1, fc='r', ec='r', headwidth=10,
                                  frac=0.4))
    ax2D.annotate('', (0, 0.5), (-0.03, 0.5), size=10,
                  xycoords="axes fraction",
                  arrowprops=dict(alpha=1, fc='g', ec='g', headwidth=10,
                                  frac=0.4))
    ax2D.annotate('', (0, 1), (-0.03, 1), size=10,
                  xycoords="axes fraction",
                  arrowprops=dict(alpha=1, fc='b', ec='b', headwidth=10,
                                  frac=0.4))

    ax2D.annotate('', (0, 0), (0, -0.06), size=10,
                  xycoords="axes fraction",
                  arrowprops=dict(alpha=1, fc='y', ec='y', headwidth=10,
                                  frac=0.4))
    ax2D.annotate('', (0.5, 0), (0.5, -0.06), size=10,
                  xycoords="axes fraction",
                  arrowprops=dict(alpha=1, fc='c', ec='c', headwidth=10,
                                  frac=0.4))
    ax2D.annotate('', (1, 0), (1, -0.06), size=10,
                  xycoords="axes fraction",
                  arrowprops=dict(alpha=1, fc='m', ec='m', headwidth=10,
                                  frac=0.4))

    b, a = np.gradient(z)
    dx = x[1] - x[0]
    dy = y[1] - y[0]
    a /= dx
    b /= dy
    rmsA = ((a**2).sum() / (nX * nY))**0.5
    rmsB = ((b**2).sum() / (nX * nY))**0.5
    aveZ = z.sum() / (nX * nY)
    fig.text(0.91, 0.95,
             u'rms slope errors:\ndz/dx = {0:.2f} µrad\n'
             u'dz/dy = {1:.2f} µrad\n\n'
             u'mean figure error:\n<z> = {2:.2f} pm\n'
             .format(rmsA, rmsB, aveZ*1e3),
             transform=fig.transFigure, size=12, color='r', ha='center',
             va='top')

# this is the way how the surface is used in ray-tracing/wave-propagation:
# the hight and the directions are spline-interpolated (the spline coefficients
# are pre-calculated) and then the spline piece-wise polinomials are used to
# reconstruct the height and the two directions and arbitrary (x, y) points.
    splineZ = ndimage.spline_filter(z.T)
    splineA = ndimage.spline_filter(a.T)
    splineB = ndimage.spline_filter(b.T)

    nrays = 1e3
    xnew = np.random.uniform(x[0], x[-1], nrays)
    ynew = np.random.uniform(y[0], y[-1], nrays)
    coords = np.array([(xnew-x[0]) / (x[-1]-x[0]) * (nX-1),
                       (ynew-y[0]) / (y[-1]-y[0]) * (nY-1)])

    znew = ndimage.map_coordinates(splineZ, coords, prefilter=True)
    anew = ndimage.map_coordinates(splineA, coords, prefilter=True)
    bnew = ndimage.map_coordinates(splineB, coords, prefilter=True)

    ax2D.scatter(xnew, ynew, c=znew, marker='o', color='gray', s=50,
                 cmap='jet')
    ax2D.quiver(xnew, ynew, -anew, -bnew, edgecolor='gray', color='gray',
#                headaxislength=5,
                scale=200, lw=0.2)

    fig.savefig(fname+'.png')
    plt.show()
    def __init__(self,pot=None,delta=None,Rmax=5.,
                 nE=25,npsi=25,nLz=30,numcores=1,
                 **kwargs):
        """
        NAME:
           __init__
        PURPOSE:
           initialize an actionAngleStaeckelGrid object
        INPUT:
           pot= potential or list of potentials

           delta= focus of prolate confocal coordinate system (can be Quantity)

           Rmax = Rmax for building grids (natural units)

           nE=, npsi=, nLz= grid size

           numcores= number of cpus to use to parallellize

           ro= distance from vantage point to GC (kpc; can be Quantity)

           vo= circular velocity at ro (km/s; can be Quantity)

        OUTPUT:
         
           instance

        HISTORY:

            2012-11-29 - Written - Bovy (IAS)

        """
        actionAngle.__init__(self,
                             ro=kwargs.get('ro',None),vo=kwargs.get('vo',None))
        if pot is None:
            raise IOError("Must specify pot= for actionAngleStaeckelGrid")
        self._pot= pot
        if delta is None:
            raise IOError("Must specify delta= for actionAngleStaeckelGrid")
        if ext_loaded and 'c' in kwargs and kwargs['c']:
            self._c= True
        else:
            self._c= False
        self._delta= delta
        if _APY_LOADED and isinstance(self._delta,units.Quantity):
            self._delta= self._delta.to(units.kpc).value/self._ro
        self._Rmax= Rmax
        self._Rmin= 0.01
        #Set up the actionAngleStaeckel object that we will use to interpolate
        self._aA= actionAngleStaeckel.actionAngleStaeckel(pot=self._pot,delta=self._delta,c=self._c)
        #Build grid
        self._Lzmin= 0.01
        self._Lzs= numpy.linspace(self._Lzmin,
                                  self._Rmax\
                                      *galpy.potential.vcirc(self._pot,
                                                             self._Rmax),
                                  nLz)
        self._Lzmax= self._Lzs[-1]
        self._nLz= nLz
        #Calculate E_c(R=RL), energy of circular orbit
        self._RL= numpy.array([galpy.potential.rl(self._pot,l) for l in self._Lzs])
        self._RLInterp= interpolate.InterpolatedUnivariateSpline(self._Lzs,
                                                                 self._RL,k=3)
        self._ERL= _evaluatePotentials(self._pot,self._RL,
                                       numpy.zeros(self._nLz))\
                                       +self._Lzs**2./2./self._RL**2.
        self._ERLmax= numpy.amax(self._ERL)+1.
        self._ERLInterp= interpolate.InterpolatedUnivariateSpline(self._Lzs,
                                                                  numpy.log(-(self._ERL-self._ERLmax)),k=3)
        self._Ramax= 200./8.
        self._ERa= _evaluatePotentials(self._pot,self._Ramax,0.) +self._Lzs**2./2./self._Ramax**2.
        #self._EEsc= numpy.array([self._ERL[ii]+galpy.potential.vesc(self._pot,self._RL[ii])**2./4. for ii in range(nLz)])
        self._ERamax= numpy.amax(self._ERa)+1.
        self._ERaInterp= interpolate.InterpolatedUnivariateSpline(self._Lzs,
                                                                  numpy.log(-(self._ERa-self._ERamax)),k=3)
        y= numpy.linspace(0.,1.,nE)
        self._nE= nE
        psis= numpy.linspace(0.,1.,npsi)*numpy.pi/2.
        self._npsi= npsi
        jr= numpy.zeros((nLz,nE,npsi))
        jz= numpy.zeros((nLz,nE,npsi))
        u0= numpy.zeros((nLz,nE))
        jrLzE= numpy.zeros((nLz))
        jzLzE= numpy.zeros((nLz))
        #First calculate u0
        thisLzs= (numpy.tile(self._Lzs,(nE,1)).T).flatten()
        thisERL= (numpy.tile(self._ERL,(nE,1)).T).flatten()
        thisERa= (numpy.tile(self._ERa,(nE,1)).T).flatten()
        thisy= (numpy.tile(y,(nLz,1))).flatten()
        thisE= _invEfunc(_Efunc(thisERa,thisERL)+thisy*(_Efunc(thisERL,thisERL)-_Efunc(thisERa,thisERL)),thisERL)
        if isinstance(self._pot,galpy.potential.interpRZPotential) and hasattr(self._pot,'_origPot'):
            u0pot= self._pot._origPot
        else:
            u0pot= self._pot
        if self._c:
            mu0= actionAngleStaeckel_c.actionAngleStaeckel_calcu0(thisE,thisLzs,
                                                                  u0pot,
                                                                  self._delta)[0]
        else:
            if numcores > 1:
                mu0= multi.parallel_map((lambda x: self.calcu0(thisE[x],
                                                               thisLzs[x])),
                                        range(nE*nLz),
                                        numcores=numcores)
            else:
                mu0= list(map((lambda x: self.calcu0(thisE[x],
                                                     thisLzs[x])),
                              range(nE*nLz)))
        u0= numpy.reshape(mu0,(nLz,nE))
        thisR= self._delta*numpy.sinh(u0)
        thisv= numpy.reshape(self.vatu0(thisE.flatten(),thisLzs.flatten(),
                                        u0.flatten(),
                                        thisR.flatten()),(nLz,nE))
        self.thisv= thisv
        #reshape
        thisLzs= numpy.reshape(thisLzs,(nLz,nE))
        thispsi= numpy.tile(psis,(nLz,nE,1)).flatten()
        thisLzs= numpy.tile(thisLzs.T,(npsi,1,1)).T.flatten()
        thisR= numpy.tile(thisR.T,(npsi,1,1)).T.flatten()
        thisv= numpy.tile(thisv.T,(npsi,1,1)).T.flatten()
        mjr, mlz, mjz= self._aA(thisR, #R
                                thisv*numpy.cos(thispsi), #vR
                                thisLzs/thisR, #vT
                                numpy.zeros(len(thisR)), #z
                                thisv*numpy.sin(thispsi), #vz
                                fixed_quad=True) 
        if isinstance(self._pot,galpy.potential.interpRZPotential) and hasattr(self._pot,'_origPot'):
            #Interpolated potentials have problems with extreme orbits
            indx= (mjr == 9999.99)
            indx+= (mjz == 9999.99)
            #Re-calculate these using the original potential, hopefully not too slow
            tmpaA= actionAngleStaeckel.actionAngleStaeckel(pot=self._pot._origPot,delta=self._delta,c=self._c)
            mjr[indx], dum, mjz[indx]= tmpaA(thisR[indx], #R
                                             thisv[indx]*numpy.cos(thispsi[indx]), #vR
                                             thisLzs[indx]/thisR[indx], #vT
                                             numpy.zeros(numpy.sum(indx)), #z
                                             thisv[indx]*numpy.sin(thispsi[indx]), #vz
                                             fixed_quad=True)
        jr= numpy.reshape(mjr,(nLz,nE,npsi))
        jz= numpy.reshape(mjz,(nLz,nE,npsi))
        for ii in range(nLz):
            jrLzE[ii]= numpy.nanmax(jr[ii,(jr[ii,:,:] != 9999.99)])#:,:])
            jzLzE[ii]= numpy.nanmax(jz[ii,(jz[ii,:,:] != 9999.99)])#:,:])
        jrLzE[(jrLzE == 0.)]= numpy.nanmin(jrLzE[(jrLzE > 0.)])
        jzLzE[(jzLzE == 0.)]= numpy.nanmin(jzLzE[(jzLzE > 0.)])
        for ii in range(nLz):
            jr[ii,:,:]/= jrLzE[ii]
            jz[ii,:,:]/= jzLzE[ii]
        #Deal w/ 9999.99
        jr[(jr > 1.)]= 1.
        jz[(jz > 1.)]= 1.
        #Deal w/ NaN
        jr[numpy.isnan(jr)]= 0.
        jz[numpy.isnan(jz)]= 0.
        #First interpolate the maxima
        self._jr= jr
        self._jz= jz
        self._u0= u0
        self._jrLzE= jrLzE
        self._jzLzE= jzLzE
        self._jrLzInterp= interpolate.InterpolatedUnivariateSpline(self._Lzs,
                                                                   numpy.log(jrLzE+10.**-5.),k=3)
        self._jzLzInterp= interpolate.InterpolatedUnivariateSpline(self._Lzs,
                                                                   numpy.log(jzLzE+10.**-5.),k=3)
        #Interpolate u0
        self._logu0Interp= interpolate.RectBivariateSpline(self._Lzs,
                                                           y,
                                                           numpy.log(u0),
                                                           kx=3,ky=3,s=0.)
        #spline filter jr and jz, such that they can be used with ndimage.map_coordinates
        self._jrFiltered= ndimage.spline_filter(numpy.log(self._jr+10.**-10.),order=3)
        self._jzFiltered= ndimage.spline_filter(numpy.log(self._jz+10.**-10.),order=3)
        # Check the units
        self._check_consistent_units()
        return None
Example #51
0
 def _coeffs(self, order):
     if order not in self.coeffs:
         coeff = ndimage.spline_filter(self.values, order=order)
         self.coeffs[order] = coeff
     return self.coeffs[order]
Example #52
0


if __name__ == '__main__':
    f1 = sys.argv[1]
    f2 = sys.argv[2]
    if not (f1.endswith('pfd')):
        print 'file name %s not end with pfd ' % (f1)
        sys.exit(1)

    pfdfile = pfd(f1)
    pfdfile.dedisperse()
    profs = pfdfile.profs
    pshape = profs.shape
    x, y, z = profs.shape
    data = profs.reshape((-1,1))
    del pfdfile

    mean = np.mean(data)
    var = np.std(data)
    data = (data-mean)/var
    profs = data.reshape(pshape)
    X, Y, Z = ogrid[0:1:x,0:1:y,0:1:z]
    coords = array([X, Y, Z])
    coeffs = ndimage.spline_filter(profs )
    X, Y, Z = mgrid[0:1:8j,0:1:8j,0:1:8j]
    coords = array([X, Y, Z])
    newf = ndimage.map_coordinates(coeffs, coords, prefilter=False)

    np.save(f2, newf)
Example #53
0
 def set_order(self,order):
     from scipy import ndimage
     import scipy
     self.order = order
     self.spline = ndimage.spline_filter(self.z,output=scipy.float64,
                                             order=order)
 def _precompute(self):
     '''function which is called after model loading and can be
     overridden to allow for interpolation specific precomputations'''
      #do the spline filtering here rather than in interpolation
     self.interpModel = ndimage.spline_filter(self.interpModel)