Skip to content

Commit

Permalink
Implement new comments on notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
srosagomez committed Sep 11, 2024
1 parent 51fc2cd commit 2897021
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 193 deletions.
74 changes: 31 additions & 43 deletions notebooks/COS/ExceptionReport/ExceptionReport.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -263,54 +263,42 @@
" flux = data[\"FLUX\"].ravel()\n",
" wl = data[\"WAVELENGTH\"].ravel()\n",
"\n",
"# Plotting flux vs wavelength on top row\n",
"ax[0][0].plot(wl, flux,\n",
" lw=2,\n",
" color=\"black\")\n",
"\n",
"ax[0][1].plot(wl, flux,\n",
"for i, axis in enumerate(ax[0]):\n",
" # Plotting flux vs wavelength on top row\n",
" axis.plot(wl, flux,\n",
" lw=2,\n",
" color=\"black\")\n",
"\n",
"# Plotting the counts on the detector for bottom row\n",
"with fits.open(successful_raw_a) as hdul:\n",
" data = hdul[1].data\n",
" ax[1][0].scatter(\n",
" data[\"RAWX\"],\n",
" data[\"RAWY\"],\n",
" s=1,\n",
" color=\"red\",\n",
" alpha=0.05\n",
" )\n",
"\n",
"with fits.open(successful_raw_b) as hdul:\n",
" data = hdul[1].data\n",
" ax[1][1].scatter(\n",
" data[\"RAWX\"],\n",
" data[\"RAWY\"],\n",
" s=1,\n",
" color=\"blue\",\n",
" alpha=0.05\n",
" )\n",
"\n",
"# Adding formatting and titles to flux vs wl row\n",
"ax[0][0].set_title(\"Flux vs Wavelength\")\n",
"ax[0][0].set_xlabel(r'Wavelength [$\\AA$]')\n",
"ax[0][0].set_ylabel(r'Flux [$erg\\ s^{-1}\\ cm^{-2}\\ \\AA^{-1}$]')\n",
"\n",
"ax[0][1].set_title(\"Flux vs Wavelength, ZOOMED\")\n",
"ax[0][1].set_xlabel(r'Wavelength [$\\AA$]')\n",
"ax[0][1].set_ylabel(r'Flux [$erg\\ s^{-1}\\ cm^{-2}\\ \\AA^{-1}$]')\n",
"\n",
"ax[0][1].set_ylim(0, 0.6e-13)\n",
" axis.set_xlabel(r'Wavelength [$\\AA$]')\n",
" axis.set_ylabel(r'Flux [$erg\\ s^{-1}\\ cm^{-2}\\ \\AA^{-1}$]')\n",
"\n",
" if i == 0:\n",
" axis.set_title(\"Flux vs Wavelength\")\n",
"\n",
" else:\n",
" axis.set_title(\"Flux vs Wavelength, ZOOMED\")\n",
" axis.set_ylim(0, 0.6e-13)\n",
"\n",
"for i, axis in enumerate(ax[1]):\n",
" # Plotting the counts on the detector for bottom row\n",
" with fits.open([successful_raw_a, successful_raw_b][i]) as hdul:\n",
" data = hdul[1].data\n",
" axis.scatter(\n",
" data[\"RAWX\"],\n",
" data[\"RAWY\"],\n",
" s=1,\n",
" color=[\"red\", \"blue\"][i],\n",
" alpha=0.05\n",
" )\n",
"\n",
"ax[1][0].set_title(\"RAWY vs RAWX, FUVA\")\n",
"ax[1][0].set_xlabel(\"RAWX\")\n",
"ax[1][0].set_ylabel(\"RAWY\")\n",
" axis.set_xlabel(\"RAWX\")\n",
" axis.set_ylabel(\"RAWY\")\n",
" \n",
" if i == 0:\n",
" axis.set_title(\"RAWY vs RAWX, FUVA\")\n",
"\n",
"ax[1][1].set_title(\"RAWY vs RAWX, FUVB\")\n",
"ax[1][1].set_xlabel(\"RAWX\")\n",
"ax[1][1].set_ylabel(\"RAWY\")\n",
" else:\n",
" axis.set_title(\"RAWY vs RAWX, FUVB\")\n",
"\n",
"plt.suptitle(f'Successful Exposure, {fits.getval(successful_x1d, \"ROOTNAME\")}',\n",
" fontweight=\"bold\",\n",
Expand Down
146 changes: 0 additions & 146 deletions notebooks/COS/ExceptionReport/cos_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,149 +286,3 @@ def estimate_snr(
"\nThis range was found on multiple segments, (grating = G230L?) ,which at present is not fully supported. The returned array should be accurate, but the mean may be incorrect."
)
return weight_avg_snr, snr_array


# %%
def withinPercent(val1, val2, percent=1.0):
"""
Primarily created for testing, this function evaluates whether two values are 'close-enough' to one another, i.e. within a percent value, that they could only differ by slight pipeline changes; This one is defined such that at close values, the percent difference is accurate.
Parameters:
val1, val2 (numerical) : Values to compare.
percent (float) : Returns value of true if the values are within this percent.
Returns:
bool : Whether or not values are within the specified percent.
float : Percent they are off by.
"""
if (val1 == np.nan) | (val2 == np.nan):
print("One of your values is NOT A NUMBER")
lowval = np.min(np.array([val1, val2]))
absDif = np.abs(np.subtract(val1, val2))
percentDif = np.abs(100 * (absDif / lowval))
within_percent_bool = percentDif <= percent
return within_percent_bool, percentDif


# %%
"""
This section is for testing and validating our S/N Calculations.
"""
if __name__ == "__main__":
print("Running test on S/N estimate function.")
test_SNR_calculations = True
if test_SNR_calculations:
# You will need to set this filepath to a fitsfile.
# I used the publicly available ldxt08010_x1dsum.fits from program 15646 (https://archive.stsci.edu/cgi-bin/mastpreview?mission=hst&dataid=LDXT08010). It was chosen at random, and is not particularly high quality data.
filepath = "./ldxt08010_x1dsum.fits.gz"

unbin_tab = Table.read(filepath)
X = [1]
snr_counts_approach = [
estimate_snr(
unbin_tab,
snr_range=[1565, 1575],
bin_data_first=False,
)[0]
]

snr_flux_approach = [
np.nanmean(
unbin_tab["FLUX"][
(unbin_tab["WAVELENGTH"] > 1565) & (unbin_tab["WAVELENGTH"] < 1575)
]
/ unbin_tab["ERROR"][
(unbin_tab["WAVELENGTH"] > 1565) & (unbin_tab["WAVELENGTH"] < 1575)
]
)
]
snr_flux_approach_low = [
np.nanmean(
unbin_tab["FLUX"][
(unbin_tab["WAVELENGTH"] > 1565) & (unbin_tab["WAVELENGTH"] < 1575)
]
/ unbin_tab["ERROR_LOWER"][
(unbin_tab["WAVELENGTH"] > 1565) & (unbin_tab["WAVELENGTH"] < 1575)
]
)
]
for bs_ in range(1, 100):
try:
a, b = estimate_snr(
unbin_tab,
snr_range=[1565, 1575],
bin_data_first=True,
binsize_=bs_,
weighted=True,
verbose=False,
)
X.append(bs_)
snr_counts_approach.append(a)
# flux/error approach:
bin_tab = bin_by_resel(unbin_tab, binsize=bs_)
x1d_error_range = (
bin_tab["FLUX"][
(bin_tab["WAVELENGTH"] > 1565) & (bin_tab["WAVELENGTH"] < 1575)
]
/ bin_tab["ERROR"][
(bin_tab["WAVELENGTH"] > 1565) & (bin_tab["WAVELENGTH"] < 1575)
]
)
x1d_error_range_low = (
bin_tab["FLUX"][
(bin_tab["WAVELENGTH"] > 1565) & (bin_tab["WAVELENGTH"] < 1575)
]
/ bin_tab["ERROR_LOWER"][
(bin_tab["WAVELENGTH"] > 1565) & (bin_tab["WAVELENGTH"] < 1575)
]
)
snr_flux_approach_val = np.nanmean(x1d_error_range)
snr_flux_approach_val_low = np.nanmean(x1d_error_range_low)
snr_flux_approach.append(snr_flux_approach_val)
snr_flux_approach_low.append(snr_flux_approach_val_low)

except Exception as ex:
print("failed for", bs_)
print(ex)
plt.figure(figsize=(8, 6), dpi=200)
plt.scatter(X, snr_counts_approach, label="From my binning algorithm")
plt.scatter(
x=X,
y=snr_flux_approach,
label=r"From the X1DSUM's $\dfrac{FLUX}{ERROR}$",
marker="x",
c="r",
)
plt.scatter(
x=X,
y=snr_flux_approach_low,
label=r"From the X1DSUM's $\dfrac{FLUX}{ERROR\_LOWER}$",
marker="x",
c="g",
)
plt.xlabel("Binsize [pixels]")
plt.ylabel(r"$\dfrac{Signal}{Noise}$")
# plt.xlim(0,30)
plt.legend()
plt.savefig("./test_estimate_snr_function.png")

plt.figure(figsize=(8, 6), dpi=200)
plt.title("Comparing our S/N calculation to CalCOS'")
plt.scatter(
x=X,
y=100 * (np.divide(snr_counts_approach, snr_flux_approach) - 1),
label="Comparing against S/N from ERROR",
marker="x",
c="k",
)
plt.scatter(
x=X,
y=100 * (np.divide(snr_counts_approach, snr_flux_approach_low) - 1),
label="Comparing against S/N from ERROR_LOWER",
marker=".",
c="b",
)
plt.xlabel("Binsize [pixels]")
plt.ylabel("Exceeds the X1DSum file's binned S/N by this %")
plt.legend()
plt.savefig("./test_estimate_snr_function_percent_overestimation.png")
8 changes: 4 additions & 4 deletions notebooks/COS/ExceptionReport/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
astropy==6.1.1
astroquery==0.4.7
matplotlib==3.9.0
numpy==1.26.2
astropy>=6.1.1
astroquery>=0.4.7
matplotlib>=3.9.0
numpy>=1.26.2
scipy>=1.14.0

0 comments on commit 2897021

Please sign in to comment.