def __init__(self, z, base_dir): self.z = z self.base_dir = base_dir A10 = 2.85e-15 # s^-1 kb = 1.381e-23 # m^2 kg s^-2 K^-1 # 21cmGEM code h = 0.6704 H0 = 100 * h * 1000 / 3.089e22 omega_c = 0.12038 / h**2 omega_b = 0.022032 / h**2 omega_m = omega_b + omega_c T_cmb0 = 2.725 # K planck_h = 6.626e-34 # m^2 kg s^-1 c = 3e8 # m/s self.T_r = T_cmb0 * (1 + self.z) z_ref = 40 T_K_ref = 33.7340 # K self.T_K = T_K_ref * ((1 + self.z) / (1 + z_ref))**2 Y = 0.274 # Helium abundance by mass rhoc = 1.36e11 * (h / 0.7)**2 # M_sol/cMpc^3 mp = 8.40969762e-58 # m_p in M_sol nH = (rhoc / mp) * (1 - Y) * omega_b * (1 + self.z)**3 * 3.40368e-68 Tstar = 0.068 # K try: t, kappa10_HH_data = np.loadtxt('kappa_HH.txt', unpack=True) except OSError: download().kappa() t, kappa10_HH_data = np.loadtxt('kappa_HH.txt', unpack=True) kappa10_HH = np.interp(self.T_K, t, kappa10_HH_data) xc = (nH * kappa10_HH * 1e-6 * Tstar) / (A10 * self.T_r) invT_s = (1 / self.T_r + xc * (1 / self.T_K)) / (1 + xc) self.T_s = 1 / invT_s xHI = 1 nu0 = 1420.4e6 Hz = H0 * np.sqrt(omega_m * (1 + self.z)**3) tau = (3*planck_h*c**3*A10*xHI*nH) / \ (32*np.pi*kb*self.T_s*nu0**2*(1+self.z)*Hz/(1+self.z)) deltaT = (self.T_s - self.T_r) / (1 + self.z) * (1 - np.exp(-tau)) norm_factor = np.load(self.base_dir + 'AFB_norm_factor.npy') self.deltaT = deltaT / np.abs(deltaT).max() * np.abs(norm_factor) * 1e3 np.savetxt(self.base_dir + 'AFB.txt', self.deltaT)
def test_existing_dir(): if os.path.exists('kappa_HH.txt'): os.remove('kappa_HH.txt') download().kappa() with pytest.raises(TypeError): download(xHI=2).kappa() # for use in later tests... download_21cmGEM_data()
def test_existing_dir(): if os.path.exists('T_release/'): shutil.rmtree('T_release/') if os.path.exists('xHI_release/'): shutil.rmtree('xHI_release/') download().model() download(xHI=True).model() predictor = evaluate(z=originalz, base_dir='T_release/', gc=True) signal, z = predictor(params) assert (len(signal.shape) == 1) params_array = [] for i in range(10): params_array.append(np.random.uniform(0, 1, 7)) params_array = np.array(params_array) data_max = np.loadtxt('T_release/data_maxs.txt') data_min = np.loadtxt('T_release/data_mins.txt') for i in range(params_array.shape[1]): params_array[:, i] = params_array[:, i] * \ (data_max[i] - data_min[i]) + data_min[i] signal, z = predictor(params_array) assert (len(signal.shape) == 2) predictor = evaluate(z=10, base_dir='T_release/') signal, z = predictor(params) preprocess = predictor.preprocess_settings assert (preprocess['AFB'] is True) assert (preprocess['resampling'] is True) assert (preprocess['std_division'] is True) predictor = evaluate(z=originalz, base_dir='xHI_release/') signal, z = predictor(params) with pytest.raises(KeyError): evaluate(z=originalz, basedir='T_release/') with pytest.raises(TypeError): predictor = evaluate(z=originalz, base_dir='T_release/') predictor(10) with pytest.raises(TypeError): predictor = evaluate(z=originalz, base_dir=100) with pytest.raises(TypeError): predictor = evaluate(z=originalz, base_dir='T_release/', logs=10) with pytest.raises(TypeError): predictor = evaluate(z='foo', base_dir='T_release/') with pytest.raises(TypeError): predictor = evaluate(z=originalz, base_dir='T_release/', gc='false')
def test_config(): if os.path.exists('T_release/'): shutil.rmtree('T_release/') if os.path.exists('xHI_release/'): shutil.rmtree('xHI_release/') download().model() download(xHI=True).model() paramnames = [ r'$\log(f_*)$', r'$\log(V_c)$', r'$\log(f_X)$', r'$\nu_\mathrm{min}$', r'$\tau$', r'$\alpha$', r'$R_\mathrm{mfp}$' ] # Providing this with global signal data as neutral fraction data is # not publicly available. Will not effect efficacy of the test. config('xHI_release/', paramnames, '21cmGEM_data/', logs=[0, 1, 2]) assert (os.path.exists('xHI_release/gui_configuration.csv') is True) res = pd.read_csv('xHI_release/gui_configuration.csv') logs = res['logs'].tolist() logs = [int(x) for x in logs if x != '--'] assert (logs == [0, 1, 2]) paramnames = [ r'$\log(f_*)$', r'$\log(V_c)$', r'$\log(f_X)$', r'$\tau$', r'$\alpha$', r'$\nu_\mathrm{min}$', r'$R_\mathrm{mfp}$' ] config('T_release/', paramnames, '21cmGEM_data/') assert (os.path.exists('T_release/gui_configuration.csv') is True) with pytest.raises(KeyError): config('T_release', paramnames, '21cmGEM_data/') with pytest.raises(TypeError): config(10, paramnames, '21cmGEM_data/') with pytest.raises(KeyError): config('T_release/', paramnames, '21cmGEM_data') with pytest.raises(TypeError): config('T_release/', paramnames, '21cmGEM_data/', logs='banana') with pytest.raises(TypeError): config('T_release/', 5, '21cmGEM_data/') with pytest.raises(KeyError): config('T_release/', paramnames, '21cmGEM_data/', color='C0')
def test_existing_dir(): if os.path.exists('T_release/'): shutil.rmtree('T_release/') if os.path.exists('xHI_release/'): shutil.rmtree('xHI_release/') download().model() download(xHI=True).model() predictor = evaluate(base_dir='T_release/') parameters = np.loadtxt('21cmGEM_data/test_data.txt') labels = np.loadtxt('21cmGEM_data/test_labels.txt') signal_plot(parameters, labels, 'rmse', predictor, 'T_release/', figsizex=3.15, figsizey=6, loss_label='RMSE = {:.4f} [mK]') assert (os.path.exists('T_release/eval_plot.pdf') is True) with pytest.raises(KeyError): signal_plot(parameters, labels, 'rmse', predictor, 'T_release/', figsize_x=3.15) with pytest.raises(TypeError): signal_plot(parameters, labels, 'rmse', predictor, 'T_release') with pytest.raises(TypeError): signal_plot(1, labels, 'rmse', predictor, 'T_release/') with pytest.raises(TypeError): signal_plot(parameters, labels, 'banana', predictor, 'T_release/') with pytest.raises(TypeError): signal_plot(parameters, 6, 'rmse', predictor, 'T_release/') with pytest.raises(TypeError): signal_plot(parameters, labels, 'rmse', 'foobar', 'T_release/') with pytest.raises(TypeError): signal_plot(parameters, labels, 'rmse', predictor, 'T_release/', rtol='banana') with pytest.raises(TypeError): signal_plot(parameters, labels, 'rmse', predictor, 'T_release/', atol='banana') with pytest.raises(TypeError): signal_plot(parameters, labels, 'rmse', predictor, 'T_release/', figsizex='banana') with pytest.raises(TypeError): signal_plot(parameters, labels, 'rmse', predictor, 'T_release/', figsizey='banana') with pytest.raises(TypeError): signal_plot(parameters, labels, 'rmse', predictor, 'T_release/', loss_label=60) with pytest.raises(TypeError): signal_plot(parameters, labels, 'rmse', predictor, 'T_release/', xHI=60) def loss_func(labels, signals): return np.mean(np.square(labels - signals)) * 1000 signal_plot(parameters, labels, loss_func, predictor, 'T_release/') assert (os.path.exists('T_release/eval_plot.pdf') is True) signal_plot(parameters, labels, 'mse', predictor, 'T_release/') signal_plot(parameters, labels, 'GEMLoss', predictor, 'T_release/')