To create an OIII/SII ratio map with adaptive binning based on signal-to-noise ratio (S/N) using Python and the vorbin package, you'll need to carefully handle the noise estimation to avoid division by zero, especially in low S/N regions. Below is a step-by-step guide, including code snippets, to help you accomplish this task.
Overview:
- Load OIII and SII images.
- Estimate signal and noise for each pixel.
- Compute S/N maps.
- Perform adaptive Voronoi binning using
vorbin.
- Compute the OIII/SII ratio in each bin.
- Create the ratio map while handling low S/N regions.
Let's go through each step in detail.
1. Load OIII and SII Images
Assuming your images are FITS files, you can use astropy.io.fits to load them.
from astropy.io import fits
import numpy as np
# Load the OIII and SII images
hdu_oiii = fits.open('OIII_image.fits')
hdu_sii = fits.open('SII_image.fits')
oiii_data = hdu_oiii[0].data.astype(float)
sii_data = hdu_sii[0].data.astype(float)
# Close the files
hdu_oiii.close()
hdu_sii.close()
2. Estimate Signal and Noise
Signal: The signal is simply the pixel values in your images.
Noise: Estimating noise can be tricky, especially in low S/N regions. Here's an approach:
- Assume Poisson noise: For photon counting, the noise can be approximated as the square root of the signal.
- Include read noise: If available, include a constant read noise term.
# Parameters
read_noise = 5.0 # Example value, in the same units as your data
# Estimate the noise
oiii_noise = np.sqrt(np.abs(oiii_data) + read_noise**2)
sii_noise = np.sqrt(np.abs(sii_data) + read_noise**2)
Note: Using np.abs() ensures we don't take the square root of negative numbers. Adjust read_noise based on your instrument's characteristics.
3. Compute S/N Maps
Compute the S/N for each pixel.
oiii_sn = np.divide(oiii_data, oiii_noise, out=np.zeros_like(oiii_data), where=oiii_noise!=0)
sii_sn = np.divide(sii_data, sii_noise, out=np.zeros_like(sii_data), where=sii_noise!=0)
Combined S/N Map:
You might want to consider a combined S/N map for binning, such as the average or sum of the OIII and SII S/N maps.
combined_sn = np.sqrt(oiii_sn**2 + sii_sn**2)
4. Perform Adaptive Voronoi Binning
We'll use the vorbin package, which implements Cappellari & Copin's (2003) adaptive binning algorithm.
First, install vorbin if you haven't:
pip install vorbin
Import and Prepare Data for Binning:
from vorbin.voronoi_2d_binning import voronoi_2d_binning
# Coordinates of each pixel
yy, xx = np.indices(oiii_data.shape)
# Flatten the arrays for input to voronoi_2d_binning
x = xx.flatten()
y = yy.flatten()
signal = combined_sn.flatten()
noise = np.ones_like(signal) # Since we're using S/N, signal/noise is just signal/1
Set a S/N Target:
Decide on a target S/N ratio per bin.
target_sn = 10 # Adjust based on your requirements
Perform Binning:
binNum, xNode, yNode, sn, nPixels, scale = voronoi_2d_binning(
x, y, signal, noise, target_sn, plot=True, quiet=False)
Reshape the Bin Numbers into the Image Shape:
binNumMap = binNum.reshape(oiii_data.shape)
5. Compute the OIII/SII Ratio in Each Bin
We need to compute the sum of OIII and SII signals in each bin and then compute their ratio.
Create Arrays to Hold Binned Data:
from scipy.stats import binned_statistic
num_bins = binNum.max() + 1 # Since bin numbers start from 0
# Arrays to store binned sums and counts
oiii_sum = np.zeros(num_bins)
sii_sum = np.zeros(num_bins)
Compute Binned Sums:
# Flatten data arrays
oiii_flat = oiii_data.flatten()
sii_flat = sii_data.flatten()
# For each bin, sum the OIII and SII signals
for i in range(num_bins):
idx = binNum == i
oiii_sum[i] = oiii_flat[idx].sum()
sii_sum[i] = sii_flat[idx].sum()
Compute the Ratios Safely:
To avoid division by zero, we can use np.divide with where and specify a fill value.
ratio = np.divide(oiii_sum, sii_sum, out=np.zeros_like(oiii_sum), where=sii_sum != 0)
6. Create the Ratio Map
Now, assign the ratio back to each pixel in the bin.
# Create a map to hold the ratios
ratio_map = np.zeros_like(oiii_data)
# Assign ratio to each pixel based on bin number
for i in range(num_bins):
ratio_map[binNumMap == i] = ratio[i]
Handling Low S/N Regions
In regions where both OIII and SII signals are very low, the ratio might not be meaningful. You can mask these regions.
# Create a mask for low S/N
sn_threshold = 2 # Adjust based on your requirements
sn_mask = combined_sn.reshape(oiii_data.shape) < sn_threshold
# Apply mask to ratio map
ratio_map_masked = np.ma.array(ratio_map, mask=sn_mask)
7. Plot the Ratio Map
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 8))
plt.imshow(ratio_map_masked, origin='lower', cmap='viridis')
plt.colorbar(label='OIII/SII Ratio')
plt.title('OIII/SII Ratio Map with Adaptive Binning')
plt.xlabel('X Pixel')
plt.ylabel('Y Pixel')
plt.show()
Notes and Recommendations
- Zero Signals: To prevent 0/0 cases, ensure that you handle divisions safely using
np.divide with appropriate where conditions.
- S/N Thresholds: Adjust the
target_sn and sn_threshold based on the quality of your data and the level of detail required.
- Read Noise: If you have specific knowledge about the read noise or other noise sources in your data, include them in the noise estimation.
- Visual Inspection: Always visually inspect your ratio map to identify any artifacts or unexpected regions.
- Data Quality: Consider masking or excluding areas with extremely low counts to avoid misleading ratios.
Example Putting It All Together
Here's a consolidated code snippet incorporating the steps above:
import numpy as np
from astropy.io import fits
from vorbin.voronoi_2d_binning import voronoi_2d_binning
import matplotlib.pyplot as plt
# Load images
oiii_data = fits.getdata('OIII_image.fits').astype(float)
sii_data = fits.getdata('SII_image.fits').astype(float)
# Estimate noise
read_noise = 5.0
oiii_noise = np.sqrt(np.abs(oiii_data) + read_noise**2)
sii_noise = np.sqrt(np.abs(sii_data) + read_noise**2)
# Compute S/N maps
oiii_sn = np.divide(oiii_data, oiii_noise, out=np.zeros_like(oiii_data), where=oiii_noise!=0)
sii_sn = np.divide(sii_data, sii_noise, out=np.zeros_like(sii_data), where=sii_noise!=0)
# Combined S/N
combined_sn = np.sqrt(oiii_sn**2 + sii_sn**2)
# Prepare for binning
yy, xx = np.indices(oiii_data.shape)
x = xx.flatten()
y = yy.flatten()
signal = combined_sn.flatten()
noise = np.ones_like(signal)
# Perform binning
target_sn = 10
binNum, xNode, yNode, sn, nPixels, scale = voronoi_2d_binning(
x, y, signal, noise, target_sn, plot=False, quiet=True)
binNumMap = binNum.reshape(oiii_data.shape)
# Compute binned sums
num_bins = binNum.max() + 1
oiii_flat = oiii_data.flatten()
sii_flat = sii_data.flatten()
oiii_sum = np.zeros(num_bins)
sii_sum = np.zeros(num_bins)
for i in range(num_bins):
idx = binNum == i
oiii_sum[i] = oiii_flat[idx].sum()
sii_sum[i] = sii_flat[idx].sum()
# Compute ratios
ratio = np.divide(oiii_sum, sii_sum, out=np.zeros_like(oiii_sum), where=sii_sum != 0)
# Create ratio map
ratio_map = np.zeros_like(oiii_data)
for i in range(num_bins):
ratio_map[binNumMap == i] = ratio[i]
# Mask low S/N regions
sn_threshold = 2
sn_mask = combined_sn < sn_threshold
ratio_map_masked = np.ma.array(ratio_map, mask=sn_mask)
# Plot
plt.figure(figsize=(10, 8))
plt.imshow(ratio_map_masked, origin='lower', cmap='viridis')
plt.colorbar(label='OIII/SII Ratio')
plt.title('OIII/SII Ratio Map with Adaptive Binning')
plt.xlabel('X Pixel')
plt.ylabel('Y Pixel')
plt.show()
Final Tips
- Error Propagation: If you need to compute uncertainties in the ratio, perform error propagation considering the variances in OIII and SII.
- Further Binning: If you have other emission lines or want to include more data, you can extend the method accordingly.
- Documentation: Refer to the
vorbin documentation for advanced options and customization.
References:
- Cappellari, M., & Copin, Y. (2003). Adaptive spatial binning of integral-field spectroscopic data using Voronoi tessellations. Monthly Notices of the Royal Astronomical Society, 342(2), 345–354.
vorbin package: GitHub Repository
I hope this helps you create your OIII/SII ratio map with adaptive binning while correctly handling noise in low S/N regions!