Beispiel #1
0
    def evaluate_earth(self,
                       measurement_std=0.001,
                       theta_tik=0.01,
                       cloud_type='earth',
                       year=2004,
                       pov='faceon'):

        # Set orbital properties
        p_rotation = 23.934
        p_orbit = 365.256363 * 24.0
        phi_orb = np.pi

        self.pov = pov

        if (self.pov == 'faceon'):
            inclination = 0 * np.pi / 180.0  #0.001#np.pi/2
            obliquity = 90. * np.pi / 180.0

        if (self.pov == 'edgeon'):
            inclination = 90 * np.pi / 180.0  #0.001#np.pi/2
            obliquity = 23. * np.pi / 180.0

        if (self.pov == 'edgeon_zero'):
            inclination = 90 * np.pi / 180.0  #0.001#np.pi/2
            obliquity = 0. * np.pi / 180.0

        phi_rot = np.pi / 2.0

        nside = 16
        npix = hp.nside2npix(nside)
        polar_angle, azimuthal_angle = hp.pixelfunc.pix2ang(
            nside, np.arange(npix))

        x = np.sin(polar_angle) * np.cos(azimuthal_angle)
        y = np.sin(polar_angle) * np.sin(azimuthal_angle)
        z = np.cos(polar_angle)

        # From NASA Earth Observations
        tmp = pd.read_csv('earth_720.csv')
        earth = tmp.values[:, :]

        # earth[earth < 99999.0] = 1.0
        earth[earth == 99999.0] = 0.0

        lat = 90. - 0.5 * np.arange(359)
        lon = -180 + 0.5 * np.arange(720)

        LON, LAT = np.meshgrid(lon, lat)

        ind = hp.ang2pix(nside, LON, LAT, lonlat=True)

        simulated_map = np.zeros(npix)
        simulated_map[ind] = earth

        simulated_map = hp.sphtfunc.smoothing(simulated_map, fwhm=0.06)
        simulated_map[simulated_map < 0] = 0.0

        # Observation schedule. Each epoch is one week.
        epoch_duration_days = 7
        cadence = 5.0  # hours
        nobs_per_epoch = int(24 // cadence * epoch_duration_days)
        epoch_duration = nobs_per_epoch * cadence
        n_epochs = int(p_orbit / 24.0 / epoch_duration_days)
        epoch_starts = [epoch_duration_days * 24 * j for j in range(n_epochs)]

        if (cloud_type == 'generate'):
            simulated_clouds = np.zeros((n_epochs, npix))
            n_octaves = 5

            print("Generating clouds...")
            for k in tqdm(range(n_epochs)):
                seed = 123 + k
                noises = [None] * n_octaves
                for i in range(n_octaves):
                    noises[i] = OpenSimplex(seed=seed + i)
                for i in range(npix):
                    freq = 1.0
                    persistence = 1.0
                    for j in range(n_octaves):
                        simulated_clouds[k, i] += billow_noise(
                            noises[j], x[i], y[i], z[i], freq, persistence)
                        freq *= 1.65
                        persistence *= 0.5

            thr = 0.3
            mx = np.max(simulated_clouds)
            mn = np.min(simulated_clouds)
            simulated_clouds = (simulated_clouds - mn) / (mx - mn)
            simulated_clouds[simulated_clouds < thr] = 0.0

            mx = np.max(simulated_clouds[simulated_clouds > thr])
            mn = np.min(simulated_clouds[simulated_clouds > thr])
            simulated_clouds[simulated_clouds > thr] = (
                simulated_clouds[simulated_clouds > thr] - mn) / (mx - mn)

        if (cloud_type == 'earth'):
            simulated_clouds = np.zeros((n_epochs, npix))
            lat_clouds = 90. - 180.0 / 64.0 * np.arange(64)
            lon_clouds = -180 + 360.0 / 128.0 * np.arange(128)

            LON_clouds, LAT_clouds = np.meshgrid(lon_clouds, lat_clouds)

            ind_clouds = hp.ang2pix(nside, LON_clouds, LAT_clouds, lonlat=True)

            print("Generating clouds from Earth observations...")
            d = datetime.date(year, 1, 1)
            for k in tqdm(range(n_epochs)):
                clouds = np.loadtxt(
                    f'clouds_earth/T42_{d.year}.{d.month:02d}.{d.day:02d}_davg.dat'
                )
                simulated_clouds[k, ind_clouds] = 0.7 * (clouds / 100.0)**1
                d += datetime.timedelta(days=epoch_duration_days)

        times = np.array([])
        for epoch_start in epoch_starts:
            epoch_times = np.linspace(epoch_start,
                                      epoch_start + epoch_duration,
                                      nobs_per_epoch)
            times = np.concatenate([times, epoch_times])

        measurement_std = 0.001

        truth = exocartographer.IlluminationMapPosterior(times,
                                                         np.zeros_like(times),
                                                         measurement_std,
                                                         nside=nside,
                                                         nside_illum=nside)

        true_params = {
            'log_orbital_period':
            np.log(p_orbit),
            'log_rotation_period':
            np.log(p_rotation),
            'logit_cos_inc':
            exocartographer.util.logit(np.cos(inclination)),
            'logit_cos_obl':
            exocartographer.util.logit(np.cos(obliquity)),
            'logit_phi_orb':
            exocartographer.util.logit(phi_orb, low=0, high=2 * np.pi),
            'logit_obl_orientation':
            exocartographer.util.logit(phi_rot, low=0, high=2 * np.pi)
        }
        truth.fix_params(true_params)
        p = np.concatenate([np.zeros(truth.nparams), simulated_map])

        Phi = truth.visibility_illumination_matrix(p)

        largest_eval = scipy.sparse.linalg.eigsh(Phi.T @ Phi,
                                                 k=1,
                                                 which='LM',
                                                 return_eigenvectors=False)
        rho = 0.4 / largest_eval[0]

        Phi_split = Phi.reshape((n_epochs, nobs_per_epoch, npix))

        d_split = np.zeros((n_epochs, nobs_per_epoch))
        for i in range(n_epochs):
            d_split[i, :] = Phi_split[i, :, :] @ (
                simulated_clouds[i, :] +
                (1.0 - simulated_clouds[i, :])**2 * simulated_map
            ) + measurement_std * np.random.randn(nobs_per_epoch)
            # d_split[i, :] = Phi_split[i, : ,:] @ (simulated_map + (0.7 - simulated_map) * simulated_clouds[i, :] / 0.7) + measurement_std * np.random.randn(nobs_per_epoch)

        self.surf0 = torch.zeros((1, 3072)).to(self.device)
        self.clouds0 = torch.zeros((1, n_epochs, 3072)).to(self.device)
        Phi_split = torch.tensor(
            Phi_split[None, :, :, :].astype('float32')).to(self.device)
        rho = torch.tensor(rho[None].astype('float32')).to(self.device)
        d_split = torch.tensor(d_split[None, :, :].astype('float32')).to(
            self.device)

        self.model_1d.eval()
        self.model_2d.eval()

        with torch.no_grad():

            start = time.time()
            surf_1d, clouds_1d, out_surface_1d, out_clouds_1d = self.model_1d(
                d_split,
                self.surf0,
                self.clouds0,
                Phi_split,
                rho,
                n_epochs=n_epochs)
            print(f'Elapsed time 1D : {time.time()-start}')

            start = time.time()
            surf_2d, clouds_2d, out_surface_2d, out_clouds_2d = self.model_2d(
                d_split,
                self.surf0,
                self.clouds0,
                Phi_split,
                rho,
                n_epochs=n_epochs)
            print(f'Elapsed time 2D : {time.time()-start}')

        out_surface_1d = out_surface_1d[-1].squeeze().cpu().numpy()
        out_clouds_1d = out_clouds_1d[-1].squeeze().cpu().numpy()
        out_surface_2d = out_surface_2d[-1].squeeze().cpu().numpy()
        out_clouds_2d = out_clouds_2d[-1].squeeze().cpu().numpy()
        Phi_split = Phi_split.cpu().numpy()

        # import ipdb
        # ipdb.set_trace()

        return simulated_map, simulated_clouds, out_surface_1d, out_surface_2d, out_clouds_1d, out_clouds_2d, Phi_split
    def evaluate_earth(self,
                       measurement_std=0.001,
                       theta_tik=0.01,
                       do_tikhonov=True):

        # Set orbital properties
        p_rotation = 23.934
        p_orbit = 365.256363 * 24.0
        phi_orb = np.pi
        inclination = 0 * np.pi / 180.0  #0.001#np.pi/2
        obliquity = 90. * np.pi / 180.0
        phi_rot = np.pi / 2.0

        nside = 16
        npix = hp.nside2npix(nside)

        # From NASA Earth Observations
        tmp = pd.read_csv('earth_720.csv')
        earth = tmp.values[:, :]

        # earth[earth < 99999.0] = 1.0
        earth[earth == 99999.0] = 0.0

        lat = 90. - 0.5 * np.arange(359)
        lon = -180 + 0.5 * np.arange(720)

        LON, LAT = np.meshgrid(lon, lat)

        ind = hp.ang2pix(nside, LON, LAT, lonlat=True)

        simulated_map = np.zeros(npix)
        simulated_map[ind] = earth

        simulated_map = hp.sphtfunc.smoothing(simulated_map, fwhm=0.06)
        simulated_map[simulated_map < 0] = 0.0

        # Observation schedule
        cadence = 5.0  # hours
        n = p_orbit // cadence
        times = cadence * np.arange(n)
        self.times = times

        truth = exocartographer.IlluminationMapPosterior(times,
                                                         np.zeros_like(times),
                                                         measurement_std,
                                                         nside=nside,
                                                         nside_illum=nside)

        true_params = {
            'log_orbital_period':
            np.log(p_orbit),
            'log_rotation_period':
            np.log(p_rotation),
            'logit_cos_inc':
            exocartographer.util.logit(np.cos(inclination)),
            'logit_cos_obl':
            exocartographer.util.logit(np.cos(obliquity)),
            'logit_phi_orb':
            exocartographer.util.logit(phi_orb, low=0, high=2 * np.pi),
            'logit_obl_orientation':
            exocartographer.util.logit(phi_rot, low=0, high=2 * np.pi)
        }
        truth.fix_params(true_params)
        p = np.concatenate([np.zeros(truth.nparams), simulated_map])

        Phi_np = truth.visibility_illumination_matrix(p)[None, :, :]

        PhiT_Phi = Phi_np[0, :, :].T @ Phi_np[0, :, :]
        largest_eval = scipy.sparse.linalg.eigsh(PhiT_Phi,
                                                 k=1,
                                                 which='LM',
                                                 return_eigenvectors=False)
        rho = 0.4 / largest_eval
        rho = torch.tensor(rho.astype('float32')).to(self.device)

        Phi = torch.tensor(Phi_np.astype('float32')).to(self.device)
        PhiT = torch.transpose(Phi, 1, 2)

        n = len(times)
        light = truth.lightcurve(p)
        light += measurement_std * np.random.randn(n)

        light = torch.tensor(light[None, :,
                                   None].astype('float32')).to(self.device)

        np.random.seed(123)
        self.x0 = torch.tensor(np.random.rand(1, 3072).astype('float32')).to(
            self.device)
        self.x0 = torch.zeros((1, 3072)).to(self.device)

        self.model_1d.eval()
        self.model_2d.eval()

        with torch.no_grad():

            start = time.time()
            out_1d, _ = self.model_1d(light, self.x0, Phi, PhiT, rho)
            print(f'Elapsed time 1D : {time.time()-start}')

            start = time.time()
            out_2d, _ = self.model_2d(light, self.x0, Phi, PhiT, rho)
            print(f'Elapsed time 2D : {time.time()-start}')

        Phi_np = Phi[0, :, :].cpu().numpy()
        y_np = light[0, :, :].cpu().numpy()

        simulated_light_1d = (Phi @ out_1d[:, :, None]).squeeze().cpu().numpy()
        simulated_light_2d = (Phi @ out_2d[:, :, None]).squeeze().cpu().numpy()
        light = light.squeeze().cpu().numpy()
        out_1d = out_1d.squeeze().cpu().numpy()
        out_2d = out_2d.squeeze().cpu().numpy()

        if (do_tikhonov):
            tik = tikhonov.tikhonov(Phi_np, y_np, theta=theta_tik, iter=2000)
            tik_light = Phi_np @ tik.flatten()
            return simulated_map, out_1d, out_2d, simulated_light_1d, simulated_light_2d, light, tik.flatten(
            ), tik_light
        else:
            return simulated_map, out_1d, out_2d, simulated_light_1d, simulated_light_2d, light, Phi_np

        # import ipdb
        # ipdb.set_trace()

        return simulated_map, out_1d, out_2d, simulated_light_1d, simulated_light_2d, light, tik.flatten(
        ), tik_light
    def evaluate_libnoise(self,
                          seed=137,
                          measurement_std=0.001,
                          theta_tik=0.01):

        noise = OpenSimplex(seed=seed)

        # Set orbital properties
        p_rotation = 23.934
        p_orbit = 365.256363 * 24.0
        phi_orb = np.pi
        inclination = 0 * np.pi / 180.0  #0.001#np.pi/2
        obliquity = 90. * np.pi / 180.0
        phi_rot = np.pi / 2.0

        nside = 16
        npix = hp.nside2npix(nside)

        delta_lat = 180.0 / 512.0
        lat = 90.0 - delta_lat * np.arange(512)

        delta_lon = 360.0 / 1024.0
        lon = delta_lon * np.arange(1024)

        LAT, LON = np.meshgrid(lat, lon)

        ind = hp.ang2pix(nside, LON, LAT, lonlat=True)

        process = subprocess.run(["./noise", "simple", f"{seed}"])

        simulated_map = np.zeros(npix)
        planet = np.array(Image.open(f'planet-{seed}-specular.bmp'))[:, :, 0]

        simulated_map[ind.T] = 1.0 - planet / 255.0

        # simulated_map *= np.random.uniform(low=0.2, high=0.8, size=1)

        process = subprocess.run(["rm", "-f", f"planet-{seed}-specular.bmp"])
        process = subprocess.run(["rm", "-f", f"planet-{seed}-surface.bmp"])
        process = subprocess.run(["rm", "-f", f"planet-{seed}-normal.bmp"])

        # Observation schedule
        cadence = p_rotation / 24.
        nobs_per_epoch = 24
        epoch_duration = nobs_per_epoch * cadence

        # epoch_starts = [15*i*p_rotation for i in range(15)]
        # epoch_starts.extend([19*i*p_rotation for i in range(15)])
        epoch_starts = [
            30 * p_rotation, 60 * p_rotation, 150 * p_rotation,
            210 * p_rotation, 250 * p_rotation
        ]

        times = np.array([])
        for epoch_start in epoch_starts:
            epoch_times = np.linspace(epoch_start,
                                      epoch_start + epoch_duration,
                                      nobs_per_epoch)
            times = np.concatenate([times, epoch_times])

        truth = exocartographer.IlluminationMapPosterior(times,
                                                         np.zeros_like(times),
                                                         measurement_std,
                                                         nside=nside,
                                                         nside_illum=nside)

        true_params = {
            'log_orbital_period':
            np.log(p_orbit),
            'log_rotation_period':
            np.log(p_rotation),
            'logit_cos_inc':
            exocartographer.util.logit(np.cos(inclination)),
            'logit_cos_obl':
            exocartographer.util.logit(np.cos(obliquity)),
            'logit_phi_orb':
            exocartographer.util.logit(phi_orb, low=0, high=2 * np.pi),
            'logit_obl_orientation':
            exocartographer.util.logit(phi_rot, low=0, high=2 * np.pi)
        }
        truth.fix_params(true_params)
        p = np.concatenate([np.zeros(truth.nparams), simulated_map])

        Phi_np = truth.visibility_illumination_matrix(p)[None, :, :]

        PhiT_Phi = Phi_np[0, :, :].T @ Phi_np[0, :, :]
        largest_eval = scipy.sparse.linalg.eigsh(PhiT_Phi,
                                                 k=1,
                                                 which='LM',
                                                 return_eigenvectors=False)
        rho = 0.4 / largest_eval
        rho = torch.tensor(rho.astype('float32')).to(self.device)

        Phi = torch.tensor(Phi_np.astype('float32')).to(self.device)
        PhiT = torch.transpose(Phi, 1, 2)

        n = len(times)
        light = truth.lightcurve(p)
        light += measurement_std * np.random.randn(n)

        light = torch.tensor(light[None, :,
                                   None].astype('float32')).to(self.device)

        np.random.seed(123)
        self.x0 = torch.tensor(np.random.rand(1, 3072).astype('float32')).to(
            self.device)
        self.x0 = torch.zeros((1, 3072)).to(self.device)

        self.model_1d.eval()
        self.model_2d.eval()

        with torch.no_grad():

            start = time.time()
            out_1d, _ = self.model_1d(light, self.x0, Phi, PhiT, rho)
            print(f'Elapsed time 1D : {time.time()-start}')

            start = time.time()
            out_2d, _ = self.model_2d(light, self.x0, Phi, PhiT, rho)
            print(f'Elapsed time 2D : {time.time()-start}')

        Phi_np = Phi[0, :, :].cpu().numpy()
        y_np = light[0, :, :].cpu().numpy()
        tik = tikhonov.tikhonov(Phi_np, y_np, theta=theta_tik, iter=1500)

        simulated_light_1d = (Phi @ out_1d[:, :, None]).squeeze().cpu().numpy()
        simulated_light_2d = (Phi @ out_2d[:, :, None]).squeeze().cpu().numpy()
        light = light.squeeze().cpu().numpy()
        out_1d = out_1d.squeeze().cpu().numpy()
        out_2d = out_2d.squeeze().cpu().numpy()

        return simulated_map, out_1d, out_2d, simulated_light_1d, simulated_light_2d, light, tik.flatten(
        )
    def evaluate_fine(self, seed=137, measurement_std=0.001):

        noise = OpenSimplex(seed=seed)

        # Set orbital properties
        p_rotation = 23.934
        p_orbit = 365.256363 * 24.0
        phi_orb = np.pi
        inclination = 0 * np.pi / 180.0  #0.001#np.pi/2
        obliquity = 90. * np.pi / 180.0
        phi_rot = np.pi / 2.0

        nside = 16
        npix = hp.nside2npix(nside)
        polar_angle, azimuthal_angle = hp.pixelfunc.pix2ang(
            nside, np.arange(npix))

        x = np.sin(polar_angle) * np.cos(azimuthal_angle)
        y = np.sin(polar_angle) * np.sin(azimuthal_angle)
        z = np.cos(polar_angle)

        simulated_map = np.zeros(npix)
        for i in range(npix):
            simulated_map[i] = simplex_noise(noise, x[i], y[i], z[i], 1.0, 1.0) + \
                simplex_noise(noise, x[i], y[i], z[i], 2.0, 1/4.0) + \
                simplex_noise(noise, x[i], y[i], z[i], 4.0, 1/8.0)

        simulated_map = simulated_map**2.5
        thr = 0.3
        simulated_map[simulated_map < thr] = 0.0
        max_tmp = np.max(simulated_map)
        tmp = simulated_map >= thr
        simulated_map[tmp] -= thr
        simulated_map[tmp] /= max_tmp - thr

        # Observation schedule
        cadence = p_rotation / 24.
        nobs_per_epoch = 24
        epoch_duration = nobs_per_epoch * cadence

        epoch_starts = [15 * i * p_rotation for i in range(25)]
        epoch_starts.extend([19 * i * p_rotation for i in range(25)])
        epoch_starts.extend([23 * i * p_rotation for i in range(25)])
        # epoch_starts = [30*p_rotation, 60*p_rotation, 150*p_rotation,
        # 210*p_rotation, 250*p_rotation]

        times = np.array([])
        for epoch_start in epoch_starts:
            epoch_times = np.linspace(epoch_start,
                                      epoch_start + epoch_duration,
                                      nobs_per_epoch)
            times = np.concatenate([times, epoch_times])

        truth = exocartographer.IlluminationMapPosterior(times,
                                                         np.zeros_like(times),
                                                         measurement_std,
                                                         nside=nside,
                                                         nside_illum=nside)

        true_params = {
            'log_orbital_period':
            np.log(p_orbit),
            'log_rotation_period':
            np.log(p_rotation),
            'logit_cos_inc':
            exocartographer.util.logit(np.cos(inclination)),
            'logit_cos_obl':
            exocartographer.util.logit(np.cos(obliquity)),
            'logit_phi_orb':
            exocartographer.util.logit(phi_orb, low=0, high=2 * np.pi),
            'logit_obl_orientation':
            exocartographer.util.logit(phi_rot, low=0, high=2 * np.pi)
        }
        truth.fix_params(true_params)
        p = np.concatenate([np.zeros(truth.nparams), simulated_map])

        Phi = torch.tensor(
            truth.visibility_illumination_matrix(p)[None, :, :].astype(
                'float32')).to(self.device)
        PhiT = torch.transpose(Phi, 1, 2)

        n = len(times)
        light = truth.lightcurve(p)
        light += measurement_std * np.random.randn(n)

        light = torch.tensor(light[None, :,
                                   None].astype('float32')).to(self.device)

        np.random.seed(123)
        self.x0 = torch.tensor(np.random.rand(1, 3072).astype('float32')).to(
            self.device)
        self.x0 = torch.zeros((1, 3072)).to(self.device)

        self.model.eval()

        with torch.no_grad():

            out, _ = self.model(light, self.x0, Phi, PhiT)

        Phi_np = Phi[0, :, :].cpu().numpy()
        y_np = light[0, :, :].cpu().numpy()
        tik = tikhonov.tikhonov(Phi_np, y_np, rho=10.0, theta=0.05, iter=1000)

        simulated_light = (Phi @ out[:, :, None]).squeeze().cpu().numpy()
        light = light.squeeze().cpu().numpy()
        out = out.squeeze().cpu().numpy()

        return simulated_map, out, simulated_light, light, tik.flatten()
Beispiel #5
0
    epoch_starts = [
        30 * p_rotation, 60 * p_rotation, 150 * p_rotation, 210 * p_rotation,
        250 * p_rotation
    ]

    times = np.array([])
    for epoch_start in epoch_starts:
        epoch_times = np.linspace(epoch_start, epoch_start + epoch_duration,
                                  nobs_per_epoch)
        times = np.concatenate([times, epoch_times])

    measurement_std = 0.001

    truth = exocartographer.IlluminationMapPosterior(times,
                                                     np.zeros_like(times),
                                                     measurement_std,
                                                     nside=nside,
                                                     nside_illum=nside)

    true_params = {
        'log_orbital_period':
        np.log(p_orbit),
        'log_rotation_period':
        np.log(p_rotation),
        'logit_cos_inc':
        exocartographer.util.logit(np.cos(inclination)),
        'logit_cos_obl':
        exocartographer.util.logit(np.cos(obliquity)),
        'logit_phi_orb':
        exocartographer.util.logit(phi_orb, low=0, high=2 * np.pi),
        'logit_obl_orientation':