How to use the tractor.patch.Patch function in tractor

To help you get started, we’ve selected a few tractor examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dstndstn / tractor / tractor / mixture_profiles.py View on Github external
#     print('  ', 'yd', (yderiv.shape, yderiv.dtype),)
        # print('  ', 'mask', mask.shape, mask.dtype)

        rtn = c_gauss_2d_masked(int(x0), int(y0), int(w), int(h),
                                float(fx), float(fy),
                                self.amp.astype(np.float32),
                                self.mean.astype(np.float32),
                                self.var.astype(np.float32),
                                result, xderiv, yderiv, mask)

        # print('gauss_2d_masked returned.')

        assert(rtn == 0)
        if derivs:
            return (Patch(x0, y0, result), Patch(x0, y0, xderiv),
                    Patch(x0, y0, yderiv))
        return Patch(x0, y0, result)
github dstndstn / tractor / tractor / patch.py View on Github external
def performArithmetic(self, other, opname, otype=float):
        assert(isinstance(other, Patch))
        if (self.x0 == other.getX0() and self.y0 == other.getY0() and
                self.shape == other.shape):
            assert(self.x0 == other.getX0())
            assert(self.y0 == other.getY0())
            assert(self.shape == other.shape)
            if self.patch is None or other.patch is None:
                return Patch(self.x0, self.y0, None)
            pcopy = self.patch.copy()
            op = getattr(pcopy, opname)
            return Patch(self.x0, self.y0, op(other.patch))

        (ph, pw) = self.patch.shape
        (ox0, oy0) = other.getX0(), other.getY0()
        (oh, ow) = other.shape

        # Find the union of the regions.
github dstndstn / tractor / tractor / patch.py View on Github external
def copy(self):
        if self.patch is None:
            return Patch(self.x0, self.y0, None)
        return Patch(self.x0, self.y0, self.patch.copy())
github dstndstn / tractor / tractor / mixture_profiles.py View on Github external
def evaluate_grid_dstn(self, x0, x1, y0, y1, cx, cy):
        '''
        [x0,x1): (int) X values to evaluate
        [y0,y1): (int) Y values to evaluate
        (cx,cy): (float) pixel center of the MoG
        '''
        from tractor.mix import c_gauss_2d_grid
        assert(self.D == 2)
        result = np.zeros((y1 - y0, x1 - x0))
        rtn = c_gauss_2d_grid(int(x0), int(x1), int(y0), int(y1), cx, cy,
                              self.amp, self.mean, self.var, result)
        if rtn == -1:
            raise RuntimeError('c_gauss_2d_grid failed')
        return Patch(x0, y0, result)
github dstndstn / tractor / tractor / patch.py View on Github external
def __mul__(self, f):
        if self.patch is None:
            return Patch(self.x0, self.y0, None)
        return Patch(self.x0, self.y0, self.patch * f)
github dstndstn / tractor / tractor / psf.py View on Github external
return None
        # Otherwise, we'll just produce the Lanczos-shifted PSF
        # image as usual, and then copy it into the modelMask
        # space.
        L = 3
        padding = L
        # Create a modelMask + padding sized stamp and insert PSF image into it
        mm = np.zeros((mh+2*padding, mw+2*padding), np.float32)
        yi,yo = get_overlapping_region(my0-y0-padding, my0-y0+mh-1+padding, 0, H-1)
        xi,xo = get_overlapping_region(mx0-x0-padding, mx0-x0+mw-1+padding, 0, W-1)
        mm[yo,xo] = img[yi,xi]
        mm = lanczos_shift_image(mm, dx, dy)
        mm = mm[padding:-padding, padding:-padding]
        assert(np.all(np.isfinite(mm)))

        return Patch(mx0, my0, mm)
github dstndstn / tractor / tractor / mixture_profiles.py View on Github external
print('-->', int(x0), int(x1), int(y0), int(y1))
            print('-->', float(fx), float(fy), float(minval))
            print('-->', cx, cy, int(minradius))
            raise
        assert(rtn == 0)
        if doslice:
            slc = slice(sy0, sy1), slice(sx0, sx1)
            result = result[slc].copy()
            if derivs:
                xderiv = xderiv[slc].copy()
                yderiv = yderiv[slc].copy()
            x0 += sx0
            y0 += sy0

        if derivs:
            return (Patch(x0, y0, result), Patch(x0, y0, xderiv),
                    Patch(x0, y0, yderiv))

        return Patch(x0, y0, result)
github dstndstn / tractor / tractor / sky.py View on Github external
def getParamDerivatives(self, tractor, img, srcs):
        import numpy as np
        p = Patch(0, 0, np.ones_like(img.getImage()))
        p.setName('dsky')
        return [p]
    def addTo(self, img, scale=1.):
github dstndstn / tractor / tractor / galaxy.py View on Github external
boffy = y0 - bigy0
                assert(bigw >= mw)
                assert(bigh >= mh)
                assert(boffx >= 0)
                assert(boffy >= 0)
                bigMask = np.zeros((bigh, bigw), bool)
                if modelMask.mask is not None:
                    bigMask[boffy:boffy + mh,
                            boffx:boffx + mw] = modelMask.mask
                else:
                    bigMask[boffy:boffy + mh, boffx:boffx + mw] = True
                bigMask = ModelMask(bigx0, bigy0, bigMask)
                # print('Recursing:', self, ':', (mh,mw), 'to', (bigh,bigw))
                bigmodel = self._realGetUnitFluxModelPatch(
                    img, px, py, minval, modelMask=bigMask)
                return Patch(x0, y0,
                             bigmodel.patch[boffy:boffy + mh, boffx:boffx + mw])

        # print('Getting Fourier transform of PSF at', px,py)
        # print('Tim shape:', img.shape)
        P, (cx, cy), (pH, pW), (v, w) = psf.getFourierTransform(px, py, halfsize)

        dx = px - cx
        dy = py - cy
        if modelMask is not None:
            # the Patch we return *must* have this origin.
            ix0 = x0
            iy0 = y0
            # the difference that we have to handle by shifting the model image
            mux = dx - ix0
            muy = dy - iy0
            # we will handle the integer portion by computing a shifted image
github dstndstn / tractor / tractor / galaxy.py View on Github external
gh, gw = G.shape
            if gw + ix0 > x1:
                G = G[:, :x1 - ix0]
            if gh + iy0 > y1:
                G = G[:y1 - iy0, :]

        if mogmix is not None:
            if modelMask is not None:
                mogpatch = run_mog(amix=mogmix, mm=modelMask)
            else:
                gh, gw = G.shape
                mogpatch = run_mog(amix=mogmix, mm=ModelMask(ix0, iy0, gw, gh))
            assert(mogpatch.patch.shape == G.shape)
            G += mogpatch.patch

        return Patch(ix0, iy0, G)