Skip to content

Commit d024c7d

Browse files
committed
Merge branch 'feature/82_test_data' into develop #94
2 parents ea41dd3 + d0049d4 commit d024c7d

11 files changed

Lines changed: 241 additions & 10 deletions

File tree

Metallicity_Stack_Commons/analysis/error_prop.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,14 @@ def fluxes_derived_prop(path, raw=False, binned_data=True, apply_dust=False,
144144
#
145145

146146
if apply_dust:
147-
EBV = compute_EBV(flux_ratios_dict[dust[0]], source=dust[0],
148-
verbose=verbose, log=log)
149-
EBV_HdHb = compute_EBV(flux_ratios_dict[dust[1]], source=dust[1],
150-
verbose=verbose, log=log)
147+
EBV = compute_EBV(flux_ratios_dict[dust[0]],
148+
source=dust[0].replace('_composite', ''),
149+
verbose=verbose,
150+
log=log)
151+
EBV_HdHb = compute_EBV(flux_ratios_dict[dust[1]],
152+
source=dust[1].replace('_composite', ''),
153+
verbose=verbose,
154+
log=log)
151155
else:
152156
EBV = None
153157
EBV_HdHb = None
@@ -256,9 +260,11 @@ def fluxes_derived_prop(path, raw=False, binned_data=True, apply_dust=False,
256260
dust[2] + '_low_err': np.repeat(np.nan, len(prop_tab0)),
257261
dust[2] + '_high_err': np.repeat(np.nan, len(prop_tab0))}
258262

259-
EBV, EBV_peak = compute_EBV(flux_ratios_dict[dust[0]],
260-
source=dust[0], verbose=verbose,
261-
log=log)
263+
EBV, \
264+
EBV_peak = compute_EBV(flux_ratios_dict[dust[0]],
265+
source=dust[0].replace('_composite', ''),
266+
verbose=verbose,
267+
log=log)
262268

263269
err_prop, peak_prop = compute_onesig_pdf(EBV, EBV_peak,
264270
usepeak=True)
@@ -273,9 +279,10 @@ def fluxes_derived_prop(path, raw=False, binned_data=True, apply_dust=False,
273279
dust[3] + '_low_err': np.repeat(np.nan, len(prop_tab0)),
274280
dust[3] + '_high_err': np.repeat(np.nan, len(prop_tab0))}
275281

276-
EBV_HdHb, EBV_HdHb_peak = compute_EBV(flux_ratios_dict[dust[1]],
277-
source=dust[1],
278-
verbose=verbose, log=log)
282+
EBV_HdHb, \
283+
EBV_HdHb_peak = compute_EBV(flux_ratios_dict[dust[1]],
284+
source=dust[1].replace('_composite', ''),
285+
verbose=verbose, log=log)
279286
log.info(EBV_HdHb_peak)
280287

281288
err_prop, peak_prop = compute_onesig_pdf(EBV_HdHb, EBV_HdHb_peak,

Metallicity_Stack_Commons/others/__init__.py

Whitespace-only changes.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
from os.path import exists, join
2+
from os import symlink
3+
import numpy as np
4+
5+
from astropy.io import fits
6+
from astropy.table import Table
7+
8+
from ..logging import log_stdout
9+
from .. import line_name
10+
from ..column_names import filename_dict
11+
12+
13+
def main(infile: str, log=None):
14+
"""
15+
Import previous DEEP2 Ly et al. (2015) dataset and export
16+
an astropy Table called bin_fit.tbl
17+
18+
:param infile: Input filename
19+
:param log: LogClass or logging object
20+
"""
21+
if log is None:
22+
log = log_stdout()
23+
24+
log.info(f"Reading : {infile}")
25+
orig_tab = fits.getdata(infile)
26+
27+
gauss_cols = [str0 + '_Flux_Gaussian' for str0 in line_name]
28+
obs_cols = [str0 + '_Flux_Observed' for str0 in line_name]
29+
flux_rms_cols = [str0 + '_RMS' for str0 in line_name]
30+
sigma_cols = [str0 + '_Sigma' for str0 in line_name]
31+
snr_cols = [str0 + '_S/N' for str0 in line_name]
32+
33+
# Column name in orig_table corresponding to gauss_cols
34+
prefixes = ['OII', 'HD', 'HG', 'OIIIA', 'HB', 'OIIIB', 'OIIIR']
35+
orig_gauss_cols = [str0 + '_FLUX_MOD' for str0 in prefixes]
36+
orig_obs_cols = [str0 + '_FLUX_DATA' for str0 in prefixes]
37+
orig_sigma_cols = [str0 + '_SIGMA' for str0 in prefixes]
38+
orig_snr_cols = [str0 + '_SNR' for str0 in prefixes]
39+
40+
reformatted_tab = Table()
41+
reformatted_tab['bin_ID'] = 1 + np.arange(len(orig_tab))
42+
43+
for ii in range(len(gauss_cols)):
44+
flux = orig_tab[orig_gauss_cols[ii]]
45+
rms = flux / orig_tab[orig_snr_cols[ii]]
46+
# Widths are in observed frame, shift to rest-frame
47+
gauss_sig = orig_tab[orig_sigma_cols[ii]] / (1+orig_tab['ZSPEC'])
48+
reformatted_tab[gauss_cols[ii]] = flux
49+
reformatted_tab[obs_cols[ii]] = orig_tab[orig_obs_cols[ii]]
50+
reformatted_tab[flux_rms_cols[ii]] = rms
51+
if 'OIII_4363' in sigma_cols[ii]:
52+
reformatted_tab[sigma_cols[ii]] = gauss_sig
53+
reformatted_tab[snr_cols[ii]] = orig_tab[orig_snr_cols[ii]]
54+
55+
out_dir = "tests_data/DEEP2_Ly2015"
56+
output_table = "DEEP2_Ly2015_fluxes.tbl"
57+
out_file = join(out_dir, output_table)
58+
log.info(f"Writing: {out_file}")
59+
reformatted_tab.write(out_file, format='ascii.fixed_width_two_line',
60+
overwrite=True)
61+
62+
symlink_file = join(out_dir, filename_dict['bin_fit'])
63+
if not exists(symlink_file):
64+
symlink(output_table, symlink_file)
65+
log.info(f"Symlink created to : {output_table}")
66+
else:
67+
log.info(f"Symlink exists: {symlink_file}")
68+
69+
# Write bin_info file to use with valid_table module
70+
info_tab = Table()
71+
info_tab['bin_ID'] = 1 + np.arange(len(orig_tab))
72+
info_tab['N_stack'] = np.repeat(1, len(orig_tab))
73+
info_file = join(out_dir, filename_dict['bin_info'])
74+
log.info(f"Writing: {info_file}")
75+
info_tab.write(info_file, format='ascii.fixed_width_two_line',
76+
overwrite=True)

tests/test_attenuation.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@
55
from Metallicity_Stack_Commons import line_name_short
66
from chun_codes import random_pdf
77

8+
import pytest
9+
810

911
def test_compute_EBV():
1012

13+
# Test Error handling when list is provided
14+
with pytest.raises(TypeError):
15+
attenuation.compute_EBV([0.45, 0.38], verbose=True)
16+
17+
# Test Error handling when source is incorrect
18+
with pytest.raises(KeyError):
19+
attenuation.compute_EBV(0.45, source='test', verbose=True)
20+
1121
dx = 0.05 # This is for randomization
1222

1323
offsets = [0, -0.01, 0.01]

tests/test_error_prop.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from Metallicity_Stack_Commons.analysis import error_prop
2+
3+
deep2_dir = 'tests_data/DEEP2_Ly2015/'
4+
5+
6+
def test_fluxes_derived_prop():
7+
8+
# Even though these are individual galaxies, we can trick it to think
9+
# it's binned data.
10+
params_list = [
11+
{'raw': True, # No MC, no dust, use default valid_table
12+
'apply_dust': False,
13+
'revised': False},
14+
{'raw': True, # No MC, no dust, use revised valid_table
15+
'apply_dust': False,
16+
'revised': True},
17+
{'raw': True, # No MC, apply dust, use default valid_table
18+
'apply_dust': True,
19+
'revised': False},
20+
{'raw': True, # No MC, apply dust, use revised valid_table
21+
'apply_dust': True,
22+
'revised': True},
23+
{'raw': False, # MC, no dust, use default valid_table
24+
'apply_dust': False,
25+
'revised': False},
26+
{'raw': False, # MC, no dust, use revised valid_table
27+
'apply_dust': False,
28+
'revised': True},
29+
{'raw': False, # MC, apply dust, use default valid_table
30+
'apply_dust': True,
31+
'revised': False},
32+
{'raw': False, # MC, apply dust, use revised valid_table
33+
'apply_dust': True,
34+
'revised': True},
35+
]
36+
for params in params_list:
37+
error_prop.fluxes_derived_prop(deep2_dir, **params, binned_data=True,
38+
verbose=True)

tests/test_valid_table.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from Metallicity_Stack_Commons import valid_table
2+
3+
deep2_dir = 'tests_data/DEEP2_Ly2015/'
4+
5+
6+
def test_valid_table():
7+
8+
valid_table.make_validation_table(deep2_dir)

0 commit comments

Comments
 (0)