How to use tractor - 10 common examples

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 / sdss.py View on Github external
np.all(klpsf[:, -1] == 0.)):
                klpsf = klpsf[1:-1, 1:-1]
            else:
                break
        mypsf = PixelizedPSF(klpsf)

    elif psf == 'kl-gm':
        from tractor.emfit import em_fit_2d
        from tractor.fitpsf import em_init_params

        # Create Gaussian mixture model PSF approximation.
        klpsf = psfield.getPsfAtPoints(bandnum, x0 + W / 2, y0 + H / 2)
        S = klpsf.shape[0]
        # number of Gaussian components
        K = 3
        w, mu, sig = em_init_params(K, None, None, None)
        II = klpsf.copy()
        II /= II.sum()
        # HIDEOUS HACK
        II = np.maximum(II, 0)
        #print('Multi-Gaussian PSF fit...')
        xm, ym = -(S // 2), -(S // 2)
        if savepsfimg is not None:
            plt.clf()
            plt.imshow(II, interpolation='nearest', origin='lower')
            plt.title('PSF image to fit with EM')
            plt.savefig(savepsfimg)
        res = em_fit_2d(II, xm, ym, w, mu, sig)
        #print('em_fit_2d result:', res)
        if res == 0:
            # print('w,mu,sig', w,mu,sig)
            mypsf = GaussianMixturePSF(w, mu, sig)
github dstndstn / tractor / tractor / psf.py View on Github external
v3=False):
        '''
        optional P0 = (w,mu,var): initial parameter guess.

        w has shape (N,)
        mu has shape (N,2)
        var (variance) has shape (N,2,2)

        optional xy0 = int x0,y0 origin of stamp.
        '''
        from tractor.emfit import em_fit_2d_reg
        from tractor.fitpsf import em_init_params
        if P0 is not None:
            w, mu, var = P0
        else:
            w, mu, var = em_init_params(N, None, None, None)
        stamp = stamp.copy()

        if xy0 is None:
            xm, ym = -(stamp.shape[1] // 2), -(stamp.shape[0] // 2)
        else:
            xm, ym = xy0

        if v3:
            tpsf = GaussianMixturePSF(w, mu, var)
            tim = Image(data=stamp, invvar=1e6 * np.ones_like(stamp),
                        psf=tpsf)
            h, w = tim.shape
            src = PointSource(PixPos(w // 2, h // 2), Flux(1.))
            tr = Tractor([tim], [src])
            tr.freezeParam('catalog')
            tim.freezeAllBut('psf')
github dstndstn / tractor / tractor / sdss.py View on Github external
S = klpsf.shape[0]
        # number of Gaussian components
        K = 3
        w, mu, sig = em_init_params(K, None, None, None)
        II = klpsf.copy()
        II /= II.sum()
        # HIDEOUS HACK
        II = np.maximum(II, 0)
        #print('Multi-Gaussian PSF fit...')
        xm, ym = -(S // 2), -(S // 2)
        if savepsfimg is not None:
            plt.clf()
            plt.imshow(II, interpolation='nearest', origin='lower')
            plt.title('PSF image to fit with EM')
            plt.savefig(savepsfimg)
        res = em_fit_2d(II, xm, ym, w, mu, sig)
        #print('em_fit_2d result:', res)
        if res == 0:
            # print('w,mu,sig', w,mu,sig)
            mypsf = GaussianMixturePSF(w, mu, sig)
            mypsf.computeRadius()
        else:
            # Failed!  Return 'dg' model instead?
            print('PSF model fit', psf, 'failed!  Returning DG model instead')
            psf = 'dg'
    if psf == 'dg':
        print('Creating double-Gaussian PSF approximation')
        (a, s1, b, s2) = dgpsf
        mypsf = NCircularGaussianPSF([s1, s2], [a, b])

    if brightpsf:
        print('Wrapping PSF in SdssBrightPSF')
github dstndstn / tractor / tractor / splinesky.py View on Github external
from __future__ import print_function
import numpy as np
import scipy.interpolate as interp
from tractor.utils import ParamList
from tractor import ducks


class SplineSky(ParamList, ducks.ImageCalibration):

    @staticmethod
    def BlantonMethod(image, mask, gridsize, estimator=np.median):
        '''
        mask: True to use pixel.  None for no masking.
        '''
        H, W = image.shape
        halfbox = gridsize // 2

        nx = int(np.round(W // float(halfbox))) + 2
        x0 = int((W - (nx - 2) * halfbox) // 2)
        xgrid = x0 + (halfbox * (np.arange(nx) - 0.5)).astype(int)

        ny = int(np.round(H // float(halfbox))) + 2
        y0 = int((H - (ny - 2) * halfbox) // 2)
        ygrid = y0 + (halfbox * (np.arange(ny) - 0.5)).astype(int)
github dstndstn / tractor / test / test_mix.py View on Github external
x = mg.evaluate(np.array([-3.,2.]))
x = mg.evaluate(np.array([[-3.,2.], [-17,4], [4,-2]]))

mg = mp.MixtureOfGaussians([1.], [0.,1.], np.array([2.]))
x = mg.evaluate(np.array([0.,1.]))
x = mg.evaluate(np.array([0,0]))
x = mg.evaluate(np.array([-3.,2.]))
x = mg.evaluate(np.array([[-3.,2.], [-17,4], [4,-2]]))

mg = mp.MixtureOfGaussians([1.], [0.,1.], np.array([ [[1.3, 0.1],[0.1,3.1]], ]))
x = mg.evaluate(np.array([0.,1.]))
x = mg.evaluate(np.array([0,0]))
x = mg.evaluate(np.array([-3.,2.]))
x = mg.evaluate(np.array([[-3.,2.], [-17,4], [4,-2]]))

mg = mp.MixtureOfGaussians([1., 0.5], np.array([ [0.,1.], [-0.3,2] ]),
						   np.array([ [[1.3, 0.1],[0.1,3.1]], [[1.2, -0.8],[-0.8, 2.4]], ]))
x = mg.evaluate(np.array([0.,1.]))
x = mg.evaluate(np.array([0,0]))
x = mg.evaluate(np.array([-3.,2.]))
x = mg.evaluate(np.array([[-3.,2.], [-17,4], [4,-2]]))

# via test_hogg_galaxy.py

mg = mp.MixtureOfGaussians(
	np.array([  4.31155865e-05,   1.34300460e-03,   1.62488556e-02,
				1.13537806e-01,   4.19327122e-01,   4.49500096e-01]),
	np.array([[ 50., 66.],
			  [ 50., 66.],
			  [ 50., 66.],
			  [ 50., 66.],
			  [ 50., 66.],
github dstndstn / tractor / test / test_mix.py View on Github external
mg = mp.MixtureOfGaussians([1.], [0.,0.], np.array([1.]))
x = mg.evaluate(np.array([0,0]))
x = mg.evaluate(np.array([0.,1.]))
x = mg.evaluate(np.array([-3.,2.]))
x = mg.evaluate(np.array([[-3.,2.], [-17,4], [4,-2]]))

mg = mp.MixtureOfGaussians([1.], [0.,1.], np.array([2.]))
x = mg.evaluate(np.array([0.,1.]))
x = mg.evaluate(np.array([0,0]))
x = mg.evaluate(np.array([-3.,2.]))
x = mg.evaluate(np.array([[-3.,2.], [-17,4], [4,-2]]))

mg = mp.MixtureOfGaussians([1.], [0.,1.], np.array([ [[1.3, 0.1],[0.1,3.1]], ]))
x = mg.evaluate(np.array([0.,1.]))
x = mg.evaluate(np.array([0,0]))
x = mg.evaluate(np.array([-3.,2.]))
x = mg.evaluate(np.array([[-3.,2.], [-17,4], [4,-2]]))

mg = mp.MixtureOfGaussians([1., 0.5], np.array([ [0.,1.], [-0.3,2] ]),
						   np.array([ [[1.3, 0.1],[0.1,3.1]], [[1.2, -0.8],[-0.8, 2.4]], ]))
x = mg.evaluate(np.array([0.,1.]))
x = mg.evaluate(np.array([0,0]))
x = mg.evaluate(np.array([-3.,2.]))
x = mg.evaluate(np.array([[-3.,2.], [-17,4], [4,-2]]))

# via test_hogg_galaxy.py

mg = mp.MixtureOfGaussians(
	np.array([  4.31155865e-05,   1.34300460e-03,   1.62488556e-02,
github goodboy / tractor / tests / test_cancellation.py View on Github external
await nursery.run_in_actor('errorer1', assert_err)
            portal2 = await nursery.run_in_actor('errorer2', assert_err)

            # get result(s) from main task
            try:
                await portal2.result()
            except tractor.RemoteActorError as err:
                assert err.type == AssertionError
                print("Look Maa that first actor failed hard, hehh")
                raise

        # here we should get a `trio.MultiError` containing exceptions
        # from both subactors

    with pytest.raises(trio.MultiError):
        tractor.run(main, arbiter_addr=arb_addr)
github goodboy / tractor / tests / test_pubsub.py View on Github external
if pub_actor is 'arbiter':
                assert 'even' not in get_topics()

            await odd_portal.cancel_actor()
            await trio.sleep(1)

            if pub_actor is 'arbiter':
                while get_topics():
                    await trio.sleep(0.1)
                    if time.time() - start > 1:
                        pytest.fail("odds subscription never dropped?")
            else:
                await master_portal.cancel_actor()

    tractor.run(
        main,
        arbiter_addr=arb_addr,
        rpc_module_paths=[__name__],
    )
github goodboy / tractor / tests / test_streaming.py View on Github external
def time_quad_ex(arb_addr, travis, spawn_backend):
    if travis and spawn_backend == 'mp' and (platform.system() != 'Windows'):
        # no idea, but the travis, mp, linux runs are flaking out here often
        pytest.skip("Test is too flaky on mp in CI")

    timeout = 7 if platform.system() == 'Windows' else 4
    start = time.time()
    results = tractor.run(cancel_after, timeout, arbiter_addr=arb_addr)
    diff = time.time() - start
    assert results
    return results, diff
github goodboy / tractor / tests / test_rpc.py View on Github external
def run():
        tractor.run(
            main,
            arbiter_addr=arb_addr,
            rpc_module_paths=exposed_mods,
        )