Skip to content

Commit

Permalink
corrected argument for CIV in absorberutils, typos corrected
Browse files Browse the repository at this point in the history
  • Loading branch information
abhi0395 committed Aug 16, 2024
1 parent d9cba60 commit 776c335
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 29 deletions.
5 changes: 2 additions & 3 deletions qsoabsfind/absfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def convolution_method_absorber_finder_in_QSO_spectra(spec_index, absorber='MgII
for sig_ker in width_kernel:
line_centre = (line1 + line2) / 2

conv_arr = convolution_fun(absorber, mult_resi * unmsk_residual, sig_ker, amp_ratio=0.5, log=logwave, wave_res=wave_res, index=spec_index)
conv_arr = convolution_fun(absorber, mult_resi * unmsk_residual, sig_ker, log=logwave, wave_res=wave_res, index=spec_index)
sigma_cr = estimate_local_sigma_conv_array(conv_arr, pm_pixel=pm_pixel)
thr = np.nanmedian(conv_arr) - coeff_sigma * sigma_cr

Expand All @@ -197,8 +197,7 @@ def convolution_method_absorber_finder_in_QSO_spectra(spec_index, absorber='MgII

combined_final_our_z = reduce(add, combined_final_our_z)
combined_final_our_z = list(set(combined_final_our_z))
combined_final_our_z = median_selection_after_combining(combined_final_our_z, lam_obs, residual)

combined_final_our_z = median_selection_after_combining(combined_final_our_z, lam_obs, residual, d_pix=d_pix, use_kernel=absorber)
combined_final_our_z = np.array(combined_final_our_z)
combined_final_our_z = combined_final_our_z[~np.isnan(combined_final_our_z)]
combined_final_our_z = combined_final_our_z.tolist()
Expand Down
16 changes: 8 additions & 8 deletions qsoabsfind/absorberutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def group_and_weighted_mean_selection_function(master_list_of_pot_absorber, resi

return z_ind

def median_selection_after_combining(combined_final_our_z, lam_search, residual, d_pix=0.6, gamma=4, use_kernel='MgII'):
def median_selection_after_combining(combined_final_our_z, lam_search, residual, d_pix, use_kernel, gamma=4):
"""
Perform grouping and weighted mean from the list of all potentially
identified absorbers after combining from all the runs with different
Expand All @@ -273,15 +273,15 @@ def median_selection_after_combining(combined_final_our_z, lam_search, residual,
lam_search (numpy.ndarray): Wavelength search array.
residual (numpy.ndarray): Residual values corresponding to the absorbers.
d_pix (float): pixel separation for toloerance in wavelength (default 0.6 A)
use_kernel (str, optional): Kernel type (MgII).
gamma (int): power for lambda to use in 1/lam**gamma weighting scheme (default 4)
use_kernel (str, optional): Kernel type (MgII). Default is 'MgII'.
Returns:
list: List after grouping contiguous pixels for each spectrum.
"""

if use_kernel=='MgII':thresh=lines['MgII_2796']
if use_kernel=='CIV':thresh=['CIV_1548']
if use_kernel=='CIV':thresh=lines['CIV_1548']

z_ind = [] # Final list of median redshifts for each spectrum

Expand Down Expand Up @@ -380,7 +380,7 @@ def remove_Mg_falsely_come_from_Fe_absorber(index, z_after_grouping, lam_obs, re
return match_abs

#@jit(nopython=False)
def z_abs_from_same_metal_absorber(first_list_z, lam_obs, residual, error, d_pix=0.6, use_kernel='MgII', logwave=None):
def z_abs_from_same_metal_absorber(first_list_z, lam_obs, residual, error, d_pix, use_kernel, logwave):
"""
Remove any absorber that arises due to the MgII2803 or CIV1550 line but has already
been detected for the MgII2796 or CIV1548 line, exploiting the doublet property of MgII/CIV to
Expand All @@ -391,8 +391,8 @@ def z_abs_from_same_metal_absorber(first_list_z, lam_obs, residual, error, d_pix
lam_obs (numpy.ndarray): Observed wavelengths.
residual (numpy.ndarray): Residual values.
error (numpy.ndarray): Error values corresponding to the residuals.
d_pix (float): Pixel distance for line separation during Gaussian fitting. Default is 0.6.
use_kernel (str, optional): Kernel type (MgII, CIV). Default is 'MgII'.
d_pix (float): Pixel distance for line separation during Gaussian fitting.
use_kernel (str, optional): Kernel type (MgII, CIV).
logwave (bool): if wavelength bins are on log scale
Returns:
Expand Down Expand Up @@ -433,7 +433,7 @@ def z_abs_from_same_metal_absorber(first_list_z, lam_obs, residual, error, d_pix
return match_abs

#@jit(nopython=False)
def contiguous_pixel_remover(abs_z, sn1_all, sn2_all, use_kernel='MgII'):
def contiguous_pixel_remover(abs_z, sn1_all, sn2_all, use_kernel):
"""
Remove contiguous pixels by evaluating the signal-to-noise ratio (SNR)
for absorbers.
Expand All @@ -442,7 +442,7 @@ def contiguous_pixel_remover(abs_z, sn1_all, sn2_all, use_kernel='MgII'):
abs_z (list or numpy.ndarray): List of absorber redshifts.
sn1_all (list or numpy.ndarray): List of SNR values for the first line.
sn2_all (list or numpy.ndarray): List of SNR values for the second line.
use_kernel (str, optional): Kernel type (MgII, CIV). Default is 'MgII'.
use_kernel (str, optional): Kernel type (MgII, CIV).
Returns:
list: Updated list of absorbers with false positives removed.
Expand Down
37 changes: 20 additions & 17 deletions qsoabsfind/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,18 @@ def gauss_two_lines_kernel(x, a):

return norm_constant * (-a1 * np.exp(-((x - a[1]) / a[2]) ** 2 / 2) - a2 * np.exp(-((x - a[4]) / a[5]) ** 2 / 2)) * 0.5

def convolution_fun(absorber, residual_arr_after_mask, width, amp_ratio=0.5, log=True, wave_res=0.0001, index=None):
def convolution_fun(absorber, residual_arr_after_mask, width, log, wave_res, index, amp_ratio=0.5):
"""
Convolves the spectrum with a Gaussian kernel.
Args:
absorber (str): Type of absorber (e.g., 'MgII', 'CIV').
residual_arr_after_mask (numpy.ndarray): Final residual array after masking.
width (float): The width of the Gaussian kernel (decide base dupon width of real absorption feature).
amp_ratio (float): Amplitude ratio for the Gaussian lines (default 0.5).
log (bool): if log bins should be used for wavelength (dlam = 0.0001, default True)
log (bool): if log bins should be used for wavelength
wave_res (float): wavelength pixel size (SDSS: 0.0001 on log scale, DESI: 0.8 on linear scale)
index (int): QSO index
amp_ratio (float): Amplitude ratio for the Gaussian lines (default 0.5).
Returns:
numpy.ndarray: The convolved residual array.
Expand Down Expand Up @@ -240,22 +240,22 @@ def modify_units(col_name, col):
def combine_fits_files(directory, output_file):
"""
Combines data from several FITS files in a directory into a single FITS file.
Note:
This function assumes that all FITS files have the same HDU structure and that all HDUs are Tables. Any column with 'EW' in its name will have its unit set to Angstrom. The `INDEX_SPEC` column is not concatenated. The primary HDU from the first FITS file is copied to the final combined file. So make sure that directory contains only qsoabsfind output files.
Args:
directory (str): Path to the directory containing the FITS files.
output_file (str): Path to the output FITS file.
Returns:
None: The combined FITS file is saved to the specified output path.
"""
from astropy.table import vstack
from astropy.table import vstack
# Initialize a dictionary to store tables for each HDU
combined_tables = {}
primary_hdu = None
formats = {}
formats = {}
# Loop through each file in the directory
for i, file_name in enumerate(os.listdir(directory)):
if file_name.endswith('.fits'):
Expand All @@ -275,7 +275,7 @@ def combine_fits_files(directory, output_file):
if isinstance(hdu, fits.BinTableHDU):
hdu_name = hdu.name
table = Table(hdu.data)

# Remove the INDEX_SPEC column if it exists
if 'INDEX_SPEC' in table.colnames:
table.remove_column('INDEX_SPEC')
Expand All @@ -287,26 +287,26 @@ def combine_fits_files(directory, output_file):
else:
combined_tables[hdu_name] = table
print(f"Initialized HDU '{hdu_name}' with data from file {i + 1}.")

# Create the HDUs to write to the output file
hdul_out = fits.HDUList([primary_hdu])

for hdu_name, table in combined_tables.items():
# Explicitly handle units by creating columns with modified unit information
columns = []
for col_name in table.colnames:
col = table[col_name]

# Modify the unit based on the column name
unit = modify_units(col_name, col)

# Create the FITS column, let the format be inferred
fits_format = formats[hdu_name][col_name]
columns.append(fits.Column(name=col_name, array=col.data, format=fits_format, unit=unit))
hdu_out = fits.BinTableHDU.from_columns(columns, name=hdu_name)
hdul_out.append(hdu_out)
print(f"Added HDU '{hdu_name}' to the output file.")
print(f"Added HDU '{hdu_name}' to the output file.")

# Write the combined data to the output FITS file
hdul_out.writeto(output_file, overwrite=True)
print(f"Combined FITS file saved to {output_file}")
Expand Down Expand Up @@ -396,6 +396,8 @@ def plot_absorber(spectra, absorber, zabs, show_error=False, xlabel='obs wave (a

num_absorbers = len(redshifts)

sep = 25 # Set separation for zoomed plot ranges

# Create a grid with 2 rows: 1 for the main plot and 1 for zoomed plots
fig = plt.figure(figsize=(13.5, 8))
fig.subplots_adjust(hspace=0.15, wspace=0.15) # Adjust space between plots
Expand All @@ -408,7 +410,9 @@ def plot_absorber(spectra, absorber, zabs, show_error=False, xlabel='obs wave (a
ax_main.plot(lam, residual, ls='-', lw=1.5, label='residual')
if show_error:
ax_main.plot(lam, error, ls='-', lw=1.5, label='error')
ax_main.set_xlim(3800, 9200)
ymask = ~np.isnan(residual)
xmin, xmax = lam[ymask].min(), lam[ymask].max()
ax_main.set_xlim(xmin, xmax)
ax_main.legend()

# Determine the absorber line labels
Expand All @@ -431,7 +435,6 @@ def plot_absorber(spectra, absorber, zabs, show_error=False, xlabel='obs wave (a
ax_main.set_ylim(-1, 2)

# Add subplots for zoomed-in regions in the second row
sep = 25 # Set separation for zoomed plot ranges

for idx, z in enumerate(redshifts):
shift_z = 1 + z
Expand All @@ -447,7 +450,7 @@ def plot_absorber(spectra, absorber, zabs, show_error=False, xlabel='obs wave (a
ax_zoom.set_xlim([x1 - sep, x2 + sep])

# Determine appropriate y-limits for the subplot based on data
y_min, y_max = residual[mask].min(), residual[mask].max()
y_min, y_max = max(0,np.nanmin(residual[mask])), np.nanmax(residual[mask])
y_margin = 0.2 * (y_max - y_min) # Add a margin for better visibility
ax_zoom.set_ylim(y_min - y_margin, y_max + y_margin)

Expand Down
Binary file modified tests/__pycache__/test_utils.cpython-39.pyc
Binary file not shown.
5 changes: 4 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ def setUp(self):
self.residual = np.random.random(4500)
self.width = 3.0
self.amp_ratio = 0.5
self.wave_res=0.0001
self.index=None
self.log=True

def test_convolution_fun(self):
result = convolution_fun(self.absorber, self.residual, self.width, self.amp_ratio)
result = convolution_fun(self.absorber, self.residual, self.width, self.log, self.wave_res, self.index, self.amp_ratio)
self.assertEqual(len(result), len(self.residual))

if __name__ == '__main__':
Expand Down

0 comments on commit 776c335

Please sign in to comment.