예제 #1
0
    def test_empty_init(self):
        "Test the default Observations creation"

        # 1. Create default Device object
        myobj = observations.Observations()

        # 2. Make sure it has the default values
        self.assertEqual(myobj.text, None)
        self.assertEqual(len(myobj.data), 0)
예제 #2
0
    def __init__(self, text=None, part2=False):

        # 1. Set the initial values
        self.part2 = part2
        self.text = text
        self.obs = None
        self.program = None
        self.opcodes = None

        # 2. Process text (if any)
        if text is not None:
            self.obs = observations.Observations(text=text)
            self.program = self.read_program(text=text)
예제 #3
0
    def test_text_init(self):
        "Test the Observation object creation from text"

        # 1. Create Device object from text
        myobj = observations.Observations(
            text=aoc_16.from_text(EXAMPLE_TEXT_MULTIPLE))

        # 2. Make sure it has the expected values
        self.assertEqual(len(myobj.text), 55)
        self.assertEqual(len(myobj.data), 13)
        self.assertEqual(myobj.data[0].before, [3, 2, 1, 1])
        self.assertEqual(myobj.data[0].instruction, [9, 2, 1, 2])
        self.assertEqual(myobj.data[0].after, [3, 2, 2, 1])
예제 #4
0
    def test_opcodes(self):
        "Test determining opcodes"
        # 1. Create Device object from text
        myobj = observations.Observations(
            text=aoc_16.from_text(EXAMPLE_TEXT_MULTIPLE))
        # 2. Make sure it has the expected values
        self.assertEqual(len(myobj.text), 55)
        self.assertEqual(len(myobj.data), 13)

        # 3. Process the observations
        myobj.process_observations(device.INSTS)
        self.assertEqual(myobj.data[0].names, set(['seti', 'addi', 'mulr']))
        self.assertEqual(
            myobj.data[1].names,
            set([
                'bani', 'eqri', 'gtrr', 'muli', 'borr', 'bori', 'addr', 'setr',
                'gtir'
            ]))
        self.assertEqual(myobj.data[2].names,
                         set(['seti', 'banr', 'borr', 'setr', 'bani', 'bori']))

        # 4. Evaluate the opcodes
        self.assertEqual(myobj.determine_opcodes(device.INSTS)[15], None)
예제 #5
0
def obs_single_star_job(tn, obsfile='Tests/cl_0.fits', outdir='Tests/cl_0'):
    filter_names = 'hst_wfc3_f275w hst_wfc3_f336w hst_acs_wfc_f475w hst_acs_wfc_f814w hst_wfc3_f110w hst_wfc3_f160w'.upper(
    ).split()

    #Load the initial model grid
    g0 = grid.FileSpectralGrid(
        'libs/stellib_kurucz2004_padovaiso.spectralgrid.fits')

    #load the filters
    filts = phot.load_filters(filter_names, interp=True, lamb=g0.lamb)

    # define the extinction priors
    oAv = extinction.Cardelli()

    #Observations
    obs = observations.Observations(obsfile)
    obs.setFilters(filter_names)

    ##define Av with a step size
    #Av = numpy.arange(0.,3., 0.1)

    ##define Av with a number of points
    Av = numpy.linspace(0, 1, 3)
    #Av = numpy.array([0.0], dtype=float)
    Rv = numpy.array([3.1], dtype=float)
    fb = numpy.array(
        [0.5],
        dtype=float)  # only one value still needs to be array/list/tuple

    #get the grid iterator
    iter_grid = iter_Av_grid(g0, oAv, Av=Av, Rv=Rv, f_bump=fb)

    # Parameters from which we generate fake data
    idx = tn
    fakesed, fakeerr, mask = obs.getObs(idx)

    with timeit('Likelihood Object %d' % tn):
        #define output filename
        outname = outdir + '/star_%d.fits' % (tn)
        for gk, thetak in iter_grid:
            with timeit('\t * Computing SEDS'):
                seds = gk.getSEDs(filts, absFlux=True)
            with timeit('\t * Computing Lnp'):
                lnp = computeLogLikelihood(fakesed,
                                           fakeerr,
                                           seds.seds,
                                           normed=False,
                                           mask=mask)
            with timeit('\t * Writing outputs'):
                t = Table(name='SEDOUT')
                #t = gk.grid
                t.addCol(numpy.arange(seds.grid.nrows, dtype=int), name='idx')
                t.addCol(lnp, name='lnp')
                for ek, nk in enumerate(filter_names):
                    t.addCol(seds.seds[:, ek], name=nk)
                t.header['Av'] = thetak[0]
                t.header['Rv'] = thetak[1]
                t.header['f_bump'] = thetak[2]
                t.write(outname)

    with timeit('\t * Writing Original grid'):
        g0.grid.header['NAME'] = 'MODELS'
        g0.grid.header['EXTNAME'] = 'MODELS'
        g0.grid.write(outname)
예제 #6
0
def getFakeCluster(g,
                   age,
                   Z,
                   filts,
                   err=0.1,
                   suffix='0',
                   Nstars=None,
                   oAv=None,
                   **kwargs):
    """ Generate a fake sed from a model grid
    INPUTS:
        idx     int                     index number on the grid
        age     float                   age of the desired population
        Z       float                   metallicity of the desired population
        filters list[filter]            list of filter names
    OUTPUTS:
        fakein  int                     the index of the model on the grid
        lamb    ndarray[float, ndim=1]  wavelength taken from the grid
        fakesed ndarray[float, ndim=1]  resulting SED

    KEYWORDS:
        suffix  str                 suffix used to store the cluster table
        err     float               proportional error to consider on the fluxes
        Nstars  int                 number of stars in the population (None = all possible stars)
        oAv     ExtinctionLaw       if provided, apply extinction function using **kwargs

        **kwargs if provided, extra keywords are used to apply an extinction
    """
    # find all the stars matching age and Z criteria
    glogA = numpy.unique(g.logA)
    gZ = numpy.unique(g.Z)
    _logA = glogA[numpy.argmin(abs(glogA - numpy.log10(age)))]
    _Z = gZ[numpy.argmin(abs(gZ - Z))]
    idx = numpy.where((g.logA == _logA) & (g.Z == _Z))[0]
    # Restrict to Nstars is provided
    if Nstars is not None:
        idx = numpy.random.randint(0, len(idx), min([len(idx), Nstars]))

    fakeseds = g.seds[idx]
    # Compute extinction is requested
    if (oAv is not None) & (len(kwargs) > 0):
        tau = oAv(g.lamb * 1e-4, Alambda=True, **kwargs)
        fakeseds *= exp(-tau)[None, :]
    ## extract photometry
    gg = grid.SpectralGrid()
    gg.lamb = g.lamb
    gg.seds = fakeseds
    gg.grid = g.grid[idx]
    seds = gg.getSEDs(filts, absFlux=True)

    seds.grid.addCol(idx, name='idx')

    for k, v in kwargs.iteritems():
        seds.grid.header[k] = v

    filter_names = []
    for ek, nk in enumerate(filts):
        fname = nk.name
        seds.grid.addCol(seds.seds[:, ek], name=fname)
        seds.grid.addCol(seds.seds[:, ek] * err, name=fname + 'err')
        filter_names.append(nk.name)
    seds.grid.header['logA'] = _logA
    seds.grid.header['Z'] = _Z

    seds.grid.write('Tests/cl_%s.fits' % suffix)
    del seds

    obs = observations.Observations('Tests/cl_%s.fits' % suffix)
    obs.setFilters(filter_names)
    return obs
예제 #7
0
ACTIONS_COUNT = 3
MINI_BATCH_SIZE = 128
SAVE_CHECK_EVERY_X = 5000  # Save checkpoint every 5000 steps
PRINT_EVERY_X = 2000

parser = argparse.ArgumentParser()
parser.add_argument("name", help="The name for the observations and checkpoint files");
parser.add_argument("-n", "--nsteps", type=int, 
   dest="nsteps", help="The number of steps of training to be performed", default=20000)
args = parser.parse_args()


obsfilename = args.name + ".obs"
chkfilename = "./" + args.name 
print("Loading observations from {}".format(obsfilename))
obs = observations.Observations()
obs.loadFromFile(args.name + ".obs")


print("There are {} observations".format(len(obs)))


session = tf.Session()
input_layer, output_layer, action, target, train_operation = nnetwork.create_network()



    


session.run(tf.global_variables_initializer())