Exemple #1
0
def get_ideal_photons(fields, cam, comps=True, plot=False):
    ntime, nwave = fields.shape[0], fields.shape[1]

    cam.rebinned_cube = np.zeros(
        (ntime, nwave, cam.array_size[1], cam.array_size[0]))
    for step in range(len(fields)):
        print(step)
        if comps:
            spectralcube = np.sum(fields[step], axis=1)
        else:
            spectralcube = fields[step, :, 0]

        cube = cam.get_ideal_cube(spectralcube)
        cam.rebinned_cube[step] = cube

    cam.rebinned_cube /= np.sum(cam.rebinned_cube)  # /sp.numframes
    cam.rebinned_cube = np.transpose(cam.rebinned_cube, (1, 0, 2, 3))

    if plot:
        # plt.figure()
        # plt.hist(cam.rebinned_cube[cam.rebinned_cube != 0].flatten(), bins=np.linspace(0, 1e4, 50))
        # plt.yscale('log')
        grid(cam.rebinned_cube, show=True, title='get ideal photons', nstd=6)

    if cam.usesave:
        cam.save()

    return cam
Exemple #2
0
    def get_photons(self, datacube, chunk_step=0, plot=False):
        """
        Given an intensity spectralcube and timestep create a quantized photon list

        :param datacube:
        :param step:
        :param plot:
        :return:
        """

        if mp.QE_var:
            datacube *= self.QE_map.T

        num_events = self.num_from_cube(datacube)

        if sp.verbose:
            print(
                f"star flux: {ap.star_flux}, cube sum: {np.sum(datacube)}, numframes: {self.numframes},"
                f"num events: {num_events:e}")

        photons = self.sample_cube(datacube, num_events)
        photons[0] += chunk_step
        photons[0] = self.assign_time(photons[0])
        photons[1] = self.assign_phase(photons[1])

        if plot:
            grid(self.rebin_list(photons), title='get_photons')

        if sp.degrade_photons:
            photons = self.degrade_photons(photons)

        return photons
Exemple #3
0
    def make_fields_master(self, plot=False):
        """ The master fields file of which all the photons are seeded from according to their device

        :return:
        """

        backup_fields = os.path.join(self.masterdir, 'fields_planet_slices.h5')

        ap.companion = False
        self.contrast = copy.deepcopy(ap.contrast)

        telescope = Telescope(usesave=False)
        fields = telescope()['fields']

        unoccultname = os.path.join(iop.testdir, f'telescope_unoccult')
        self.psf_template = self.get_unoccult_psf(unoccultname)
        # grid(self.psf_template)

        if plot:
            grid(fields[0], logZ=True, title='make_fields_master')

        if os.path.exists(backup_fields):
            fields_master = fields
        else:
            assert len(fields.shape) == 6

            collapse_comps = np.zeros(
                (sp.numframes, ap.n_wvl_final, sp.grid_size, sp.grid_size))
            # grid(self.psf_template)
            for (x, y), scaling in zip(
                    np.array(ap.companion_xy) * 20, self.contrast):
                cube = copy.deepcopy(self.psf_template)
                print(x, y, scaling)
                cube = np.roll(cube, -int(x), 2)
                cube = np.roll(cube, -int(y), 1)
                cube *= scaling
                # grid(cube)
                collapse_comps += cube

            obs_seq = np.abs(fields[:, -1])**2

            fields_master = np.zeros(
                (sp.numframes, ap.n_wvl_final, 2, sp.grid_size, sp.grid_size))
            # collapse_comps = np.sum(obs_seq[:, :, 1:], axis=2)
            fields_master[:, :, 0] = obs_seq[:, :, 0]
            fields_master[:, :, 1] = collapse_comps
            if plot:
                grid(fields_master[0], logZ=True, title='star and comps cube')

            dprint(
                f"Reduced shape of obs_seq = {np.shape(fields_master)} (numframes x nwsamp x 2 x grid x grid)"
            )

            os.rename(iop.fields, backup_fields)
            telescope.save_fields(fields_master)

        return fields_master
Exemple #4
0
def investigate_stats():
    sp.grid_size = 512
    sim = RunMedis(name=f'{TESTDIR}/fields', product='fields')
    observation = sim()

    fields = np.abs(observation['fields'][:,-1])**2
    timecube = np.sum(fields[:,0], axis=1)
    grid([np.sum(timecube, axis=0)], show=False)

    locs = [[210,210], [256,256], [256,206], [256,512-206]]
    names = ['satelite', 'star', 'planet', 'speckle']

    plot_stats(timecube, locs, names)
def view_reduced():
    trios = []
    for i in range(6, 21, 1):
        reduced_images = get_reduced_images(ind=-i, plot=False)
        PCA = reduced_images[1, 0]
        PCD = reduced_images[0, 2]
        PCDPCA = reduced_images[1, 2]
        trio = np.array([PCA, PCD, PCDPCA])
        trio[trio <= 0] = 0.1
        trios.append(trio)

        if i % 5 == 0:
            print(i)
            grid(trios, logZ=True, vlim=(1, 60), show=False)
            trios = []

    plt.show(block=True)
Exemple #6
0
def investigate_quantized():

    sp.quick_detect = True
    mp.array_size = np.array([100, 100])
    sp.verbose = True
    sp.save_to_disk = True
    center = mp.array_size[0] // 2
    contrasts = range(-1, -8, -3)
    print(contrasts)
    locs = [[center + 33, center], [center, center], [center, center - 33],
            [center - 24, center + 24]]
    objects = ['satelite', 'star', 'planet', 'speckle']
    # fig, axes = plt.subplots(2, len(contrasts))
    name = f'{TESTDIR}/subaru/'

    axes_list = []
    atmosdir = iop.atmosdir.split('/')[-1]
    iop.atmosdir = os.path.join(iop.datadir, name, iop.atmosroot, atmosdir)
    for c, contrast in enumerate(contrasts):
        axes_list.append(plt.subplots(2, len(locs))[1])
        ap.contrast = 10**np.array([float(contrast)])
        sim = RunMedis(name=name + f'1e{contrast}', product='photons')
        iop.atmosdir = os.path.join(iop.datadir, name, iop.atmosroot, atmosdir)
        iop.aberdir = os.path.join(iop.datadir, name, iop.aberroot, atmosdir)
        observation = sim()
        print(observation.keys(), observation['photons'].shape,
              observation['rebinned_photons'].shape)
        grid(observation['rebinned_photons'][:10],
             logZ=True,
             nstd=10,
             show=False,
             vlim=(0, 2))
        timecube = np.sum(observation['rebinned_photons'], axis=1)
        # plot_stats(timecube, locs, names)
        for l, (loc, obj) in enumerate(zip(locs, objects)):
            print(l, loc, obj)
            bins_list, I_list, timesamps, lc_list = pixel_stats(timecube, loc)
            axes_list[c][0, l].plot(timesamps, lc_list[0])
            axes_list[c][1, l].plot(bins_list[0][:-1], I_list[0])
            axes_list[c][0, l].set_xlabel('time samples')
            axes_list[c][0, l].set_ylabel('intensity')
            axes_list[c][1, l].set_xlabel('intensity')
            axes_list[c][1, l].set_ylabel('amount')
            axes_list[c][0, l].set_title(f'{obj} contrast 10^{contrast}')
    plt.show()
Exemple #7
0
    def degrade_photons(self, photons, plot=False):
        if plot:
            grid(self.rebin_list(photons), title='before degrade')

        if mp.dark_counts:
            dark_photons = self.get_bad_packets(type='dark')
            dprint(photons.shape, dark_photons.shape, 'dark')
            photons = np.hstack((photons, dark_photons))

        if mp.hot_pix:
            hot_photons = self.get_bad_packets(type='hot')
            photons = np.hstack((photons, hot_photons))
            # stem = add_hot(stem)

        if plot:
            grid(self.rebin_list(photons), title='after bad')

        if mp.phase_uncertainty:
            photons[1] *= self.responsivity_error_map[np.int_(photons[2]),
                                                      np.int_(photons[3])]
            photons, idx = self.apply_phase_offset_array(photons, self.sigs)

        # thresh =  photons[1] < self.basesDeg[np.int_(photons[3]),np.int_(photons[2])]
        if mp.phase_background:
            thresh = -photons[1] > 3 * self.sigs[-1,
                                                 np.int_(photons[3]),
                                                 np.int_(photons[2])]
            photons = photons[:, thresh]

        if mp.remove_close:
            stem = self.arange_into_stem(
                photons.T, (self.array_size[0], self.array_size[1]))
            stem = self.remove_close(stem)
            photons = self.ungroup(stem)

        if plot:
            grid(self.rebin_list(photons), title='after remove close')

        # This step was taking a long time
        # stem = arange_into_stem(photons.T, (self.array_size[0], self.array_size[1]))
        # cube = make_datacube(stem, (self.array_size[0], self.array_size[1], ap.n_wvl_final))
        # # ax7.imshow(cube[0], origin='lower', norm=LogNorm(), cmap='inferno', vmin=1)
        # cube /= self.QE_map
        # photons = ungroup(stem)

        return photons
Exemple #8
0
    def __call__(self, debug=True):

        dprint(self.performance_data)
        # self.performance_data = '/Users/dodkins/MKIDSim/mkid_param_invest/figure3_develop_mkid_lowerflux/1/max_count/performance_data.pkl'
        if not os.path.exists(self.performance_data):
            # self.metric.create_adapted_cams()

            comps_ = [True, False]
            pca_products = []
            for comps in comps_:
                if hasattr(self.metric, 'get_rebinned_cubes'):
                    self.metric.get_rebinned_cubes(self.master_fields,
                                                   comps=comps)
                else:
                    self.get_rebinned_cubes(self.master_fields, comps=comps)

            #     pca_products.append(self.pca_rebinned_cubes(comps))
            #
            # maps = pca_products[0]
            # rad_samps = pca_products[1][1]
            # thruputs = pca_products[1][2]
            # noises = pca_products[1][3]
            # conts = pca_products[1][4]

            maps, rad_samps, thruputs, noises, conts = self.pca_rebinned_cubes(
            )

            with open(self.performance_data, 'wb') as handle:
                pickle.dump((maps, rad_samps, thruputs, noises, conts,
                             self.metric.vals),
                            handle,
                            protocol=pickle.HIGHEST_PROTOCOL)
        else:
            with open(self.performance_data, 'rb') as handle:
                performance_data = pickle.load(handle)
                if len(performance_data) == 6:
                    maps, rad_samps, thruputs, noises, conts, self.metric.vals = performance_data
                else:
                    maps, rad_samps, conts, self.metric.vals = performance_data

        if debug:
            try:
                contrcurve_plot(self.metric.vals, rad_samps, thruputs, noises,
                                conts)
                if self.metric.name != 'array_size':
                    grid(maps, logZ=False,
                         title=self.metric.name)  #, vlim=(-2,2)) for no-normed
                else:
                    pass
                plt.show(block=True)
            except UnboundLocalError:
                dprint('thruputs and noises not saved in old versions :(')
                # raise UnboundLocalError
                pass

            combo_performance(maps,
                              rad_samps,
                              conts,
                              self.metric.vals,
                              self.metric.name, [0, -1],
                              savedir=self.testdir)

        return {'maps': maps, 'rad_samps': rad_samps, 'conts': conts}
Exemple #9
0
tp.ao_act = 60
tp.rotate_atmos = False
tp.rotate_sky = False
tp.f_lens = 200.0 * tp.entrance_d
tp.occult_loc = [0, 0]

# Saving
sp.save_to_disk = False  # save obs_sequence (timestep, wavelength, x, y)
sp.save_list = ['detector']  # list of locations in optics train to save
# sp.skip_planes = ['coronagraph']  # ['wfs', 'deformable mirror']  # list of locations in optics train to save
sp.quick_detect = True
sp.debug = False
sp.verbose = True

if __name__ == '__main__':
    # =======================================================================
    # Run it!!!!!!!!!!!!!!!!!
    # =======================================================================

    sim = mm.RunMedis(name='general_example', product='photons')
    observation = sim()
    fp_sampling = sim.cam.platescale
    rebinned_photons = sim.cam.rebinned_cube

    dprint(f"Sampling in focal plane is {fp_sampling}")
    for o in range(len(ap.contrast) + 1):
        print(rebinned_photons.shape)
        datacube = rebinned_photons[o]
        print(o, datacube.shape)
        grid(datacube, logZ=True, title='Spectral Channels')
Exemple #10
0
def get_form_photons(fields,
                     cam,
                     comps=True,
                     plot=False,
                     collapse_time_first=False,
                     norm=False):
    """
    Alternative to cam.__call__ that allows the user to specify whether the spectracube contains the planets

    :param fields: ndarray
    :param cam: mkids.Camera()
    :param comps: bool

    :return:
    mkids.Camera()
    """
    dprint(cam.name)
    if os.path.exists(cam.name):
        print(f'loading cam rebined_cube save at {cam.name}')
        with open(cam.name, 'rb') as handle:
            cam = pickle.load(handle)
    else:
        if comps:
            fourcube = np.sum(fields, axis=2)
        else:
            fourcube = fields[:, :, 0]

        fourcube = np.abs(fourcube)**2
        dprint(np.sum(fourcube))
        fourcube = cam.rescale_cube(fourcube)

        max_steps = cam.max_chunk(fourcube)
        num_chunks = int(np.ceil(len(fourcube) / max_steps))
        dprint(fourcube.shape, max_steps,
               len(fourcube) / max_steps, num_chunks)
        # cam.photons = np.empty((4,0))
        cam.rebinned_cube = np.zeros_like(fourcube)
        for chunk in range(num_chunks):
            photons = cam.get_photons(fourcube[chunk * max_steps:(chunk + 1) *
                                               max_steps],
                                      chunk_step=chunk * max_steps)
            photons = cam.degrade_photons(photons)
            # cam.photons = np.hstack((cam.photons, photons))
            # dprint(photons.shape, cam.photons.shape)
            cam.rebinned_cube[chunk * max_steps:(chunk + 1) *
                              max_steps] = cam.rebin_list(
                                  photons,
                                  time_inds=[
                                      chunk * max_steps,
                                      (chunk + 1) * max_steps
                                  ])
        # cam.rebinned_cube = cam.rebin_list(cam.photons)

        cam.photons = None

        for step in range(len(fields)):
            print(step, cam.max_count)
            if cam.max_count:
                cam.rebinned_cube[step] = cam.cut_max_count(
                    cam.rebinned_cube[step])

        if norm:
            cam.rebinned_cube /= np.sum(cam.rebinned_cube)  # /sp.numframes

        if collapse_time_first:
            grid(cam.rebinned_cube, title='comp sum', show=False, logZ=True)
            cam.rebinned_cube = np.median(cam.rebinned_cube,
                                          axis=0)[np.newaxis]
            grid(cam.rebinned_cube, title='comp sum', show=False, logZ=True)

        cam.rebinned_cube = np.transpose(cam.rebinned_cube, (1, 0, 2, 3))

        if plot:
            grid(cam.rebinned_cube, show=True, title='get form photons')

        if cam.usesave:
            cam.save_instance()

    return cam
Exemple #11
0
        h5file.close()

    def load_fields(self, span=(0, -1)):
        """ load fields h5

         warning sampling is not currently stored in h5. It is stored in telescope.pkl however
         """
        print(f"Loading fields from {iop.fields}")
        h5file = tables.open_file(iop.fields,
                                  mode="r",
                                  title="MEDIS Electric Fields File")
        if span[1] == -1:
            self.cpx_sequence = h5file.root.data[span[0]:]
        else:
            self.cpx_sequence = h5file.root.data[span[0]:span[1]]
        h5file.close()
        self.pretty_sequence_shape()
        self.sampling = h5file.root.sampling[0]

        return {'fields': self.cpx_sequence, 'sampling': self.sampling}


if __name__ == '__main__':
    iop.update_testname('telescope_module_test')

    telescope_sim = Telescope()
    observation = telescope_sim()
    print(observation.keys())
    grid(observation['fields'], logZ=True, nstd=5)
Exemple #12
0
                         self.platescale)
        ynew = np.arange(-self.array_size[1] * self.platescale / 2,
                         self.array_size[1] * self.platescale / 2,
                         self.platescale)
        mkid_cube = np.zeros((rebinned_cube.shape[0], rebinned_cube.shape[1],
                              self.array_size[0], self.array_size[1]))
        for d, datacube in enumerate(rebinned_cube):
            for s, slice in enumerate(datacube):
                f = interpolate.interp2d(x, x, slice, kind='cubic')
                mkid_cube[d, s] = f(ynew, xnew)
        mkid_cube = mkid_cube * np.sum(datacube) / np.sum(mkid_cube)
        # grid(mkid_cube, logZ=True, show=True, extract_center=False, title='post')
        rebinned_cube = mkid_cube

        rebinned_cube[rebinned_cube < 0] *= -1

        if conserve:
            rebinned_cube *= total / np.sum(rebinned_cube)

        return rebinned_cube


if __name__ == '__main__':
    iop.update_testname('MKIDS_module_test')
    sp.quick_detect = True

    cam = Camera()
    observation = cam()
    print(observation.keys())
    grid(observation['rebinned_photons'], nstd=5)
product = 'photons'
# product = 'rebinned_cube'
# product = 'fields'

sp.verbose = True
# sp.debug = True
# sp.checkpointing = 10

if __name__ == '__main__':
    name = f'{TESTDIR}/{atmp.model}'
    sim = RunMedis(name=name, product=product)
    observation = sim()
    print(observation.keys(), )

    if product == 'rebinned_cube':
        grid(observation['rebinned_cube'], vlim=(0, 3))
    elif product == 'photons':
        grid(sim.cam.rebin_list(observation['photons']), vlim=(0, 3))
        # plt.hist(observation['photons'][0])
        # plt.show()
    else:
        dprint(
            np.sum(
                np.abs(np.sum(observation['fields'][:, -1, :, :], axis=2))**2))
        grid(observation['fields'])

    if product == 'photons':
        from mkidpipeline.hdf.photontable import ObsFile

        # to look at the photontable
        obs = ObsFile(iop.photonlist)
def contrast_curve():
    thrus = np.zeros((4, 5, 3))  # 4 conts, 5 rad, 3 types
    r = range(73)
    stds = np.zeros((1, len(r), 3))
    fwhm = config['data']['fwhm']
    planet_locs = np.array([[39, 36], [46, 44], [53, 51], [60, 59], [66, 65]])
    loc_rads = np.sqrt(np.sum((mp.array_size[0] // 2 - planet_locs)**2,
                              axis=1))

    imlistsfile = 'imlists.npy'
    if os.path.exists(imlistsfile):
        with open(imlistsfile, 'rb') as f:
            imlists = np.load(f)
    else:
        imlists = []
        alldata = load_meta(kind='pt_outputs')
        for ix, rev_ind in enumerate(range(1, 21, 1)):  #1,21

            cur_seg, pred_seg_res, cur_data, _, trainbool, _ = alldata[
                -rev_ind]
            metrics = get_bin_measures(cur_seg, pred_seg_res, sum=False)

            true_star_photons = np.concatenate(
                (cur_data[metrics[1]], cur_data[metrics[3]]), axis=0)

            bins = [
                np.linspace(true_star_photons[:, 0].min(),
                            true_star_photons[:, 0].max(), sp.numframes + 1),
                np.linspace(true_star_photons[:, 1].min(),
                            true_star_photons[:, 1].max(), ap.n_wvl_final + 1),
                np.linspace(-200, 200, 151),
                np.linspace(-200, 200, 151)
            ]

            true_star_tess, edges = np.histogramdd(true_star_photons,
                                                   bins=bins)

            true_star_tess = np.transpose(true_star_tess, axes=(1, 0, 2, 3))
            angle_list = -np.linspace(
                0, sp.numframes * sp.sample_time * config['data']['rot_rate'] /
                60, true_star_tess.shape[1])
            star_derot = np.sum(cube_derotate(np.sum(true_star_tess, axis=0),
                                              angle_list,
                                              imlib='opencv',
                                              interpolation='lanczos4'),
                                axis=0)

            reduced_images = get_reduced_images(ind=-rev_ind, plot=False)

            PCA = reduced_images[1, 0]
            PCD = reduced_images[0, 2]
            PCDPCA = reduced_images[1, 2]

            imlist = np.array([star_derot, PCA, PCD, PCDPCA])
            # imlist[imlist <= 0] = 0.01
            imlists.append(imlist)

        with open(imlistsfile, 'wb') as f:
            np.save(f, np.array(imlists))

    trios = []
    for ix in range(20):
        imlist = imlists[ix]
        star_derot, PCA, PCD, PCDPCA = imlist
        cont_ind = ix // 5
        r_ind = ix % 5

        trio = np.array([PCA, PCD, PCDPCA])
        trio[trio <= 0] = 0.1
        trios.append(trio)

        if ix % 5 == 4:
            print(ix)
            grid(trios, logZ=True, vlim=(1, 60), show=False)
            trios = []

        plot = False
        # if cont_ind >= 0:
        #     plot = True

        # true_flux = aperture_flux(planet_derot, [planet_locs[r_ind,0]],[planet_locs[r_ind,1]], fwhm, plot=plot)[0]
        # measured = np.array([aperture_flux(PCA, [planet_locs[r_ind,0]],[planet_locs[r_ind,1]], fwhm, plot=plot)[0],
        #                      aperture_flux(PCD, [planet_locs[r_ind,0]], [planet_locs[r_ind,1]], fwhm, plot=plot)[0],
        #                      aperture_flux(PCDPCA, [planet_locs[r_ind,0]], [planet_locs[r_ind,1]], fwhm, plot=plot)[0]
        #                      ])

        # print(ix, cont_ind, r_ind, measured/true_flux, true_flux)
        # thrus[cont_ind, r_ind] = measured/true_flux

        if cont_ind == 0:  #4-1:
            std = np.array([
                noise_per_annulus(PCA, 1, fwhm)[0],
                noise_per_annulus(PCD, 1, fwhm)[0],
                noise_per_annulus(PCDPCA, 1, fwhm)[0]
            ])
            stds[0] = std.T

    plt.show()

    # mean_thrus = np.mean(thrus[2:], axis=0)
    full_thru = noise_per_annulus(star_derot, 1, fwhm, mean_per_ann=True)[0]
    # full_thrus = []
    fig = plt.figure()
    ax1 = fig.add_subplot(1, 3, 1)
    ax2 = fig.add_subplot(1, 3, 2)
    ax3 = fig.add_subplot(1, 3, 3)
    labels = ['PCA', 'PCD (sum)', 'PCD + PCA']
    for i in range(3):
        # ax1.plot(loc_rads[::-1], mean_thrus[:,i][::-1], 'o')
        # f = InterpolatedUnivariateSpline(loc_rads[::-1], mean_thrus[:,i][::-1], k=1, ext=3)  # switch order since furthest out is first
        # full_thru = f(r)
        # full_thru[full_thru<=0] = 0.01

        sensitivity = stds[0, :, i] / max(full_thru)
        ax1.plot(full_thru)
        ax2.plot(stds[0, :, i])
        ax3.plot(sensitivity)
    ax1.set_yscale("log")
    ax2.set_yscale("log")
    ax3.set_yscale("log")

    # plt.plot(sensitivity/1e5, label=labels[i])
    # plt.yscale("log")
    # plt.legend()
    plt.show(block=True)
Exemple #15
0
    def pca_rebinned_cubes(self):
        maps, rad_samps, thruputs, noises, conts = [], [], [], [], []
        cams = self.metric.cams
        colors = [f'C{i}' for i in range(len(self.metric.cams['star']))]
        # fig, axes = plt.subplots(1,6)
        fig, axes = plt.subplots(1, 3)

        dprint(np.shape(axes))
        for i in range(len(cams['comp'])):
            comp_cube = cams['comp'][i].rebinned_cube
            dprint(comp_cube.shape)
            frame_comp = pca.pca(comp_cube,
                                 angle_list=np.zeros((comp_cube.shape[1])),
                                 scale_list=self.scale_list,
                                 mask_center_px=None,
                                 adimsdi='double',
                                 ncomp=self.ncomp,
                                 ncomp2=None,
                                 collapse=self.collapse)
            maps.append(frame_comp)

            nocomp_cube = cams['star'][i].rebinned_cube
            frame_nocomp = pca.pca(nocomp_cube,
                                   angle_list=np.zeros((nocomp_cube.shape[1])),
                                   scale_list=self.scale_list,
                                   mask_center_px=None,
                                   adimsdi='double',
                                   ncomp=self.ncomp,
                                   ncomp2=None,
                                   collapse=self.collapse)

            median_fwhm = cams['star'][i].lod if hasattr(
                cams['star'][i], 'lod') else mp.lod
            median_wave = (self.wsamples[-1] + self.wsamples[0]) / 2
            fwhm = median_fwhm * self.wsamples / median_wave

            mask = cams['star'][i].QE_map == 0

            print('check whether to be using mask here?!')
            noise_samp, rad_samp = contrcurve.noise_per_annulus(
                frame_nocomp, separation=1, fwhm=median_fwhm)
            # mask=mask)

            xy = np.array(
                ap.companion_xy
            ) * 20 * cams['star'][i].sampling / cams['star'][i].platescale
            px, py = (cams['star'][i].array_size[::-1] / 2 - xy).T
            cx, cy = cams['star'][i].array_size[::-1] / 2
            dists = np.sqrt((py - cy)**2 + (px - cx)**2)
            # grid(np.sum(comp_cube, axis=1), title='in comp time collapse', show=False)
            # injected_flux=[contrcurve.aperture_flux(np.sum(comp_cube-nocomp_cube, axis=1)[i], py, px, fwhm[i]/1.5, ap_factor=1) for i in range(comp_cube.shape[0])]
            injected_flux = [
                contrcurve.aperture_flux(np.sum(comp_cube, axis=1)[i],
                                         py,
                                         px,
                                         fwhm[i] / 1.5,
                                         ap_factor=1,
                                         plot=False)
                for i in range(comp_cube.shape[0])
            ]
            # [axes[i+3].plot(dists, influx_wave) for influx_wave in injected_flux]
            injected_flux = np.mean(injected_flux, axis=0)

            # if i ==0 or i == len(self.metric.cams['star'])-1:
            # grid(comp_cube, title='comp', show=False)
            # grid(nocomp_cube, title='no', show=False)
            # grid(comp_cube-nocomp_cube, title='diff', show=False)
            # grid([np.sum(comp_cube, axis=(0,1))], title='sum comp', logZ=True, show=False)
            # grid([np.sum(nocomp_cube, axis=(0,1))], title='sum nocomp', logZ=True, show=False)
            # grid([np.sum(comp_cube, axis=(0,1))-np.sum(nocomp_cube, axis=(0,1))], title='sum then diff', logZ=True, show=False)
            # grid([], title='comp', logZ=True, show=False)
            grid([frame_comp, frame_nocomp, frame_comp - frame_nocomp],
                 title='comp, nocomp, diff',
                 logZ=False,
                 show=False,
                 vlim=(-2e-7, 2e-7))  # vlim=(-2,2))#, vlim=(-2e-7,2e-7))
            grid([
                np.sum(comp_cube, axis=1)[::4],
                np.sum(nocomp_cube, axis=1)[::4]
            ],
                 title='input: comp, no comp',
                 logZ=False,
                 show=False,
                 vlim=(-1e-5, 1e-5))  # vlim=(-2,2))#, vlim=(-1e-5,1e-5))
            grid(np.sum(comp_cube - nocomp_cube, axis=1)[::4],
                 title='diiff input cube',
                 logZ=False,
                 show=False)  #, vlim=(-2,2))
            # grid([(frame_comp-frame_nocomp)/(np.sum(comp_cube-nocomp_cube, axis=(0,1)))], title='throughput', logZ=True, show=False)

            # recovered_flux = contrcurve.aperture_flux((frame_comp - frame_nocomp), py, px, median_fwhm/1.5, ap_factor=1, )
            recovered_flux = contrcurve.aperture_flux((frame_comp),
                                                      py,
                                                      px,
                                                      median_fwhm / 1.5,
                                                      ap_factor=1,
                                                      plot=False)
            # thruput = recovered_flux*1e6 #/ injected_flux
            thruput = recovered_flux / injected_flux

            thruput[np.where(thruput < 0)] = 0

            # plt.figure()
            axes[0].plot(dists, thruput, c=colors[i])
            axes[1].plot(dists, injected_flux, c=colors[i])
            axes[2].plot(dists,
                         recovered_flux,
                         c=colors[i],
                         label=f'{self.metric.vals[i]}')
            axes[2].legend()
            # plt.plot(rad_samp, noise_samp)
            # plt.show()

            win = min(noise_samp.shape[0] - 2, int(2 * median_fwhm))
            if win % 2 == 0: win += 1
            noise_samp_sm = savgol_filter(noise_samp,
                                          polyorder=2,
                                          mode='nearest',
                                          window_length=win)

            # thruput_mean_log = np.log10(thruput + 1e-5)
            # f = InterpolatedUnivariateSpline(dists, thruput_mean_log, k=2)
            # thruput_interp_log = f(rad_samp)
            # thruput_interp = 10 ** thruput_interp_log
            # thruput_interp[thruput_interp <= 0] = np.nan  # 1e-5

            # thruput_interp = np.interp(rad_samp, dists, thruput)

            from scipy.interpolate import interp1d
            # f = interp1d(dists, thruput, fill_value='extrapolate')
            # thruput_interp = f(rad_samp)
            thruput_com = thruput.reshape(4, -1, order='F').mean(axis=0)
            dists_com = dists.reshape(4, -1, order='F').mean(axis=0)

            thruput_com_log = np.log10(thruput_com + 1e-5)
            f = interp1d(dists_com, thruput_com_log, fill_value='extrapolate')
            thruput_interp_log = f(rad_samp)
            thruput_interp = 10**thruput_interp_log

            axes[0].plot(dists_com, thruput_com, marker='o', c=colors[i])

            print(thruput, thruput_interp)
            axes[0].plot(rad_samp, thruput_interp, c=colors[i])

            starphot = 1.1 / 2
            sigma = 5
            cont_curve_samp = (
                (sigma * noise_samp_sm) / thruput_interp) / starphot
            cont_curve_samp[cont_curve_samp < 0] = 1
            cont_curve_samp[cont_curve_samp > 1] = 1

            thruputs.append(thruput_interp)
            noises.append(noise_samp_sm)
            conts.append(cont_curve_samp)
            rad_samp = cams['star'][i].platescale * rad_samp
            rad_samps.append(rad_samp)
            # maps.append(frame_nocomp)
        plt.show(block=True)
        return maps, rad_samps, thruputs, noises, conts
Exemple #16
0
class vel():
    def __init__(self, num_samp=3):
        self.median_val = 10
        # self.multiplier = np.logspace(np.log10(0.1), np.log10(10), num_samp)
        self.multiplier = np.array([0])
        self.vals = self.multiplier * self.median_val

if __name__ == '__main__':
    metric_config = vel()
    for i, val in enumerate(metric_config.vals):
        atmp.vel = val
        name = f'{TESTDIR}/{val}'
        sim = RunMedis(name=name, product=product)
        observation = sim()

        if product == 'fields':
            grid(observation['fields'], show=False)#, vlim=(-2e-7,2e-7))

        elif product == 'photons':
            grid(sim.cam.rebin_list(observation['photons']), show=False)

            # to look at the photontable
            obs = ObsFile(iop.photonlist)
            print(obs.photonTable)
            # to plot an image of all the photons on the array
            image = obs.getPixelCountImage(integrationTime=None)['image']
            quick2D(image, show=False, title=f'{val}')

    plt.show(block=True)

        planet_photons = np.array(planet_photons).T.astype(np.float32)
        planet_photons = np.insert(planet_photons,
                                   obj=1,
                                   values=cam.phase_cal(ap.wvl_range[0]),
                                   axis=0)
        planet_photons[0] = (planet_photons[0] + abs_step) * sp.sample_time
        photons = np.concatenate((star_photons, planet_photons), axis=1)

        # cam.save_photontable(photonlist=photons, index=None, populate_subsidiaries=False)
        stem = cam.arange_into_stem(photons.T,
                                    (cam.array_size[1], cam.array_size[0]))
        stem = list(map(list, zip(*stem)))
        stem = cam.remove_close(stem)
        photons = cam.ungroup(stem)
        photons = photons[[0, 1, 3, 2]]
        # grid(cam.rebin_list(photons), show=False)
        cam.populate_photontable(photons=photons, finalise=False)
    cam.populate_photontable(photons=[], finalise=True)
    grid(cam.rebin_list(photons), show=False)

    # to look at the photontable
    obs = Photontable(iop.photonlist)
    print(obs.photonTable)

    # to plot an image of all the photons on the array
    image = obs.getPixelCountImage(integrationTime=None)['image']
    quick2D(image, show=False, title='Const planet photons')

    plt.show(block=True)
def get_reduced_images(ind=1,
                       use_spec=True,
                       plot=False,
                       use_pca=True,
                       verbose=False):
    """ Looks for reduced images file in the home folder and returns if it exists. If you're on the local machine
    and the file has not been transferred it will throw a FileNotFoundError """

    all_photons, star_photons, planet_photons = get_photons(amount=-ind)
    all_tess, star_tess, planet_tess = (
        get_tess(photons)
        for photons in [all_photons, star_photons, planet_photons])

    all_tess = np.transpose(all_tess, axes=(1, 0, 2, 3))
    star_tess = np.transpose(star_tess, axes=(1, 0, 2, 3))
    planet_tess = np.transpose(planet_tess, axes=(1, 0, 2, 3))

    all_raw = np.sum(all_tess, axis=(0, 1))
    star_raw = np.sum(star_tess, axis=(0, 1))
    planet_raw = np.sum(planet_tess, axis=(0, 1))

    sp.numframes = 80
    sp.sample_time = 15

    angle_list = -np.linspace(
        0, sp.numframes * sp.sample_time * config['data']['rot_rate'] / 60,
        all_tess.shape[1])
    print(angle_list, 'angle')

    if use_spec:
        wsamples = np.linspace(ap.wvl_range[0], ap.wvl_range[1],
                               all_tess.shape[0])
        scale_list = wsamples / (ap.wvl_range[1] - ap.wvl_range[0])

        all_pca = pca.pca(all_tess,
                          angle_list=angle_list,
                          scale_list=scale_list,
                          mask_center_px=None,
                          adimsdi='double',
                          ncomp=(None, 1),
                          collapse='sum',
                          verbose=verbose)

        star_pca = pca.pca(star_tess,
                           angle_list=angle_list,
                           scale_list=scale_list,
                           mask_center_px=None,
                           adimsdi='double',
                           ncomp=(None, 1),
                           collapse='sum',
                           verbose=verbose)

        planet_pca = pca.pca(planet_tess,
                             angle_list=angle_list,
                             scale_list=scale_list,
                             mask_center_px=None,
                             adimsdi='double',
                             ncomp=(None, 1),
                             collapse='sum',
                             verbose=verbose)
        # reduced_images = np.array([[all_raw, star_raw, planet_raw], [all_pca, star_pca, planet_pca]])

        all_med = medsub_source.median_sub(all_tess,
                                           angle_list=angle_list,
                                           scale_list=scale_list,
                                           collapse='sum')
        star_med = medsub_source.median_sub(star_tess,
                                            angle_list=angle_list,
                                            scale_list=scale_list,
                                            collapse='sum')
        planet_med = medsub_source.median_sub(planet_tess,
                                              angle_list=angle_list,
                                              scale_list=scale_list,
                                              collapse='sum')

    else:
        pass
        # all_med = medsub_source.median_sub(np.sum(all_tess, axis=0), angle_list=angle_list, collapse='sum')
        # star_med = medsub_source.median_sub(np.sum(star_tess, axis=0), angle_list=angle_list, collapse='sum')
        # planet_med = medsub_source.median_sub(np.sum(planet_tess, axis=0), angle_list=angle_list,
        #                                       collapse='sum')

    all_derot = np.sum(cube_derotate(np.sum(all_tess, axis=0),
                                     angle_list,
                                     imlib='opencv',
                                     interpolation='lanczos4'),
                       axis=0)
    star_derot = np.sum(cube_derotate(np.sum(star_tess, axis=0),
                                      angle_list,
                                      imlib='opencv',
                                      interpolation='lanczos4'),
                        axis=0)
    planet_derot = np.sum(cube_derotate(np.sum(planet_tess, axis=0),
                                        angle_list,
                                        imlib='opencv',
                                        interpolation='lanczos4'),
                          axis=0)

    # all_snr = snrmap(all_pca, fwhm=5, plot=True)
    # star_snr = snrmap(star_pca, fwhm=5, plot=True)
    # planet_snr = snrmap(planet_pca, fwhm=5, plot=True)

    reduced_images = np.array([[all_raw, star_raw, planet_raw],
                               [all_derot, star_derot, planet_derot],
                               [all_pca, star_pca, planet_pca],
                               [all_med, star_med, planet_med]
                               ])  #, )  # [all_snr, star_snr, planet_snr]

    planet_loc = (92, 60)
    fwhm = config['data']['fwhm']
    nproc = 8

    # reduced_images = np.array([[all_med, snrmap(all_med, fwhm, known_sources=planet_loc, nproc=nproc)],
    #                            [all_pca, snrmap(all_pca, fwhm, known_sources=planet_loc,  nproc=nproc)],
    #                            [planet_derot, snrmap(planet_derot, fwhm, known_sources=planet_loc, nproc=nproc)],
    #                            [planet_pca, snrmap(planet_pca, fwhm, known_sources=planet_loc,  nproc=nproc)]])
    # plt.imshow(planet_derot)
    # plt.imshow(snrmap(planet_derot, fwhm, nproc=nproc))
    # plt.show()
    # reduced_images = np.array([[all_raw, star_raw, planet_raw]])

    # grid(reduced_images, logZ=True, vlim=(1,50))  #, vlim=(1,70)
    if plot:
        grid(reduced_images, vlim=(-10, 106))

    return reduced_images