Skip to content

Commit

Permalink
Removed RMS noise estimate for being badly defined. Removed _chan 1D …
Browse files Browse the repository at this point in the history
…outputs for being low-value and confusing.
  • Loading branch information
Cameron-Van-Eck committed Nov 22, 2023
1 parent efac24d commit 0755c39
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 116 deletions.
1 change: 0 additions & 1 deletion RMtools_1D/do_RMclean_1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ def run_rmclean(mDict, aDict, cutoff,
log('QU Noise = %.4g %s' % (mDict["dQU"],mDict["units"]))
log('FDF Noise (theory) = %.4g %s' % (mDict["dFDFth"],mDict["units"]))
log('FDF Noise (Corrected MAD) = %.4g %s' % (mDict_cl["dFDFcorMAD"],mDict["units"]))
log('FDF Noise (rms) = %.4g %s' % (mDict_cl["dFDFrms"],mDict["units"]))

log('FDF SNR = %.4g ' % (mDict_cl["snrPIfit"]))
log()
Expand Down
7 changes: 3 additions & 4 deletions RMtools_1D/do_RMsynth_1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@ def run_rmsynth(data, polyOrd=2, phiMax_radm2=None, dPhi_radm2=None,
rmsFig = plt.figure(facecolor='w',figsize=(12.0, 8))
ax = rmsFig.add_subplot(111)
ax.plot(freqArr_Hz/1e9, dQUArr, marker='o', color='k', lw=0.5,
label='rms <QU>')
label='noise <QU>')
ax.plot(freqArr_Hz/1e9, dQArr, marker='o', color='b', lw=0.5,
label='rms Q')
label='noise Q')
ax.plot(freqArr_Hz/1e9, dUArr, marker='o', color='r', lw=0.5,
label='rms U')
label='noise U')
xRange = (np.nanmax(freqArr_Hz)-np.nanmin(freqArr_Hz))/1e9
ax.set_xlim( np.min(freqArr_Hz)/1e9 - xRange*0.05,
np.max(freqArr_Hz)/1e9 + xRange*0.05)
Expand Down Expand Up @@ -431,7 +431,6 @@ def run_rmsynth(data, polyOrd=2, phiMax_radm2=None, dPhi_radm2=None,
log('QU Noise = %.4g %s' % (mDict["dQU"],units))
log('FDF Noise (theory) = %.4g %s' % (mDict["dFDFth"],units))
log('FDF Noise (Corrected MAD) = %.4g %s' % (mDict["dFDFcorMAD"],units))
log('FDF Noise (rms) = %.4g %s' % (mDict["dFDFrms"],units))
log('FDF SNR = %.4g ' % (mDict["snrPIfit"]))
log('sigma_add(q) = %.4g (+%.4g, -%.4g)' % (mDict["sigmaAddQ"],
mDict["dSigmaAddPlusQ"],
Expand Down
47 changes: 3 additions & 44 deletions RMtools_1D/rmtools_bwdepol.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,6 @@ def bwdepol_measure_FDF_parms(FDF, phiArr, fwhmRMSF, adjoint_sens, adjoint_noise
rm_fdf = absFDF / adjoint_noise # RM spectrum in S:N units (normalized by RM-dependent noise)
amp_fdf = absFDF/ adjoint_sens # RM spectrum normalized by (RM-dependent) sensitivity
indxPeakPIchan = np.nanargmax(rm_fdf[1:-1])+1 # Masks out the edge channels
ampPeakPIchan = amp_fdf[indxPeakPIchan] # since they can't be fit to.

# new theoretical dFDF correction for adjoint method
#This is noise in the adjoint-spectrum.
Expand All @@ -638,42 +637,17 @@ def bwdepol_measure_FDF_parms(FDF, phiArr, fwhmRMSF, adjoint_sens, adjoint_noise
absFDFmsked = absFDFmsked[np.where(absFDFmsked==absFDFmsked)]
if float(len(absFDFmsked))/len(absFDF)<0.3:
dFDFcorMAD = MAD(rm_fdf)
dFDFrms = np.sqrt( np.mean(rm_fdf**2) )
else:
dFDFcorMAD = MAD(absFDFmsked)
dFDFrms = np.sqrt( np.mean(absFDFmsked**2) )

#The noise is re-normalized by the predicted noise at the peak RM.
dFDFcorMAD = dFDFcorMAD * adjoint_noise[indxPeakPIchan]
dFDFrms = dFDFrms * adjoint_noise[indxPeakPIchan]

# Measure the RM of the peak channel
phiPeakPIchan = phiArr[indxPeakPIchan]
snrPIchan = ampPeakPIchan * adjoint_sens[indxPeakPIchan] / dFDF
dPhiPeakPIchan = fwhmRMSF / (2.0 * snrPIchan)

# Correct the peak for polarisation bias (POSSUM report 11)
ampPeakPIchanEff = ampPeakPIchan
if snrPIchan >= snrDoBiasCorrect:
ampPeakPIchanEff = np.sqrt(ampPeakPIchan**2.0 - 2.3 * dampPeakPI**2.0)

# Calculate the polarisation angle from the channel; normalize by sensitivity
peakFDFimagChan = FDF.imag[indxPeakPIchan] / adjoint_sens[indxPeakPIchan]
peakFDFrealChan = FDF.real[indxPeakPIchan] / adjoint_sens[indxPeakPIchan]
polAngleChan_deg = 0.5 * np.degrees(np.arctan2(peakFDFimagChan,
peakFDFrealChan)) % 180
dPolAngleChan_deg = np.degrees(0.5 / snrPIchan)

# Calculate the derotated polarisation angle and uncertainty
polAngle0Chan_deg = np.degrees(np.radians(polAngleChan_deg) -
phiPeakPIchan * lam0Sq) % 180



nChansGood = np.sum(np.where(lamSqArr_m2==lamSqArr_m2, 1.0, 0.0))
varLamSqArr_m2 = (np.sum(lamSqArr_m2**2.0) -
np.sum(lamSqArr_m2)**2.0/nChansGood) / (nChansGood-1)
dPolAngle0Chan_rad = \
np.sqrt( dampPeakPI**2.0*nChansGood / (4.0*(nChansGood-2.0)*ampPeakPIchan**2.0) *
((nChansGood-1)/nChansGood + lam0Sq**2.0/varLamSqArr_m2) )
dPolAngle0Chan_deg = np.degrees(dPolAngle0Chan_rad)

# Determine the peak in the FDF, its amplitude and Phi using a
# 3-point parabolic interpolation
Expand Down Expand Up @@ -733,20 +707,6 @@ def bwdepol_measure_FDF_parms(FDF, phiArr, fwhmRMSF, adjoint_sens, adjoint_noise

# Store the measurements in a dictionary and return
mDict = {'dFDFcorMAD': toscalar(dFDFcorMAD),
'dFDFrms': toscalar(dFDFrms),
'phiPeakPIchan_rm2': toscalar(phiPeakPIchan),
'dPhiPeakPIchan_rm2': toscalar(dPhiPeakPIchan),
'ampPeakPIchan': toscalar(ampPeakPIchan),
'ampPeakPIchanEff': toscalar(ampPeakPIchanEff),
'dAmpPeakPIchan': toscalar(dampPeakPI),
'snrPIchan': toscalar(snrPIchan),
'indxPeakPIchan': toscalar(indxPeakPIchan),
'peakFDFimagChan': toscalar(peakFDFimagChan),
'peakFDFrealChan': toscalar(peakFDFrealChan),
'polAngleChan_deg': toscalar(polAngleChan_deg),
'dPolAngleChan_deg': toscalar(dPolAngleChan_deg),
'polAngle0Chan_deg': toscalar(polAngle0Chan_deg),
'dPolAngle0Chan_deg': toscalar(dPolAngle0Chan_deg),
'phiPeakPIfit_rm2': toscalar(phiPeakPIfit),
'dPhiPeakPIfit_rm2': toscalar(dPhiPeakPIfit),
'ampPeakPIfit': toscalar(ampPeakPIfit),
Expand Down Expand Up @@ -1224,7 +1184,6 @@ def run_adjoint_rmsynth(data, polyOrd=3, phiMax_radm2=None, dPhi_radm2=None,
log('QU Noise = %.4g %s' % (mDict["dQU"],units))
log('FDF Noise (theory) = %.4g %s' % (mDict["dFDFth"],units))
log('FDF Noise (Corrected MAD) = %.4g %s' % (mDict["dFDFcorMAD"],units))
log('FDF Noise (rms) = %.4g %s' % (mDict["dFDFrms"],units))
log('FDF SNR = %.4g ' % (mDict["snrPIfit"]))
log('sigma_add(q) = %.4g (+%.4g, -%.4g)' % (mDict["sigmaAddQ"],
mDict["dSigmaAddPlusQ"],
Expand Down
17 changes: 2 additions & 15 deletions RMtools_3D/RMpeakfit_3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,7 @@ def save_maps(map_dict, prefix_path,FDFheader):


#Dictionary of units for peak fitting output parameters (for FITS headers)
unit_dict={"dFDFcorMAD": flux_unit, "dFDFrms": flux_unit,
"phiPeakPIchan_rm2": 'rad/m^2', "dPhiPeakPIchan_rm2": 'rad/m^2',
"ampPeakPIchan": flux_unit, "ampPeakPIchanEff": flux_unit,
"dAmpPeakPIchan": flux_unit,
"snrPIchan": '',
"indxPeakPIchan": '',
"peakFDFimagChan": flux_unit, "peakFDFrealChan": flux_unit,
"polAngleChan_deg": 'deg', "dPolAngleChan_deg": 'deg',
"polAngle0Chan_deg": 'deg', "dPolAngle0Chan_deg": 'deg',
unit_dict={"dFDFcorMAD": flux_unit,
"phiPeakPIfit_rm2": 'rad/m^2', "dPhiPeakPIfit_rm2": 'rad/m^2',
"ampPeakPIfit": flux_unit, "ampPeakPIfitEff": flux_unit,
"dAmpPeakPIfit": flux_unit,
Expand Down Expand Up @@ -344,12 +336,7 @@ def main():
"ampPeakPIfitEff","dAmpPeakPIfit","snrPIfit",
"polAngle0Fit_deg","dPolAngle0Fit_deg"]
elif args.all_products:
product_list=['dFDFcorMAD', 'dFDFrms', 'phiPeakPIchan_rm2',
'dPhiPeakPIchan_rm2', 'ampPeakPIchan', 'ampPeakPIchanEff',
'dAmpPeakPIchan', 'snrPIchan', 'indxPeakPIchan',
'peakFDFimagChan', 'peakFDFrealChan', 'polAngleChan_deg',
'dPolAngleChan_deg', 'polAngle0Chan_deg',
'dPolAngle0Chan_deg', 'phiPeakPIfit_rm2',
product_list=['dFDFcorMAD', 'phiPeakPIfit_rm2',
'dPhiPeakPIfit_rm2', 'ampPeakPIfit', 'ampPeakPIfitEff',
'dAmpPeakPIfit', 'snrPIfit', 'indxPeakPIfit',
'peakFDFimagFit', 'peakFDFrealFit', 'polAngleFit_deg',
Expand Down
4 changes: 2 additions & 2 deletions RMtools_3D/do_RMsynth_3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ def run_rmsynth(dataQ, dataU, freqArr_Hz, dataI=None, rmsArr=None,
Kwargs:
dataI (array_like): Model cube of Stokes I spectra (see do_fitIcube).
rmsArr (array_like): Cube of RMS spectra.
rmsArr (array_like): Cube of uncertainty spectra.
phiMax_radm2 (float): Maximum absolute Faraday depth (rad/m^2).
dPhi_radm2 (float): Faraday depth channel size (rad/m^2).
nSamples (float): Number of samples across the RMSF.
weightType (str): Can be "variance" or "uniform"
"variance" -- Weight by RMS.
"variance" -- Weight by inverse variance.
"uniform" -- Weight uniformly (i.e. with 1s)
fitRMSF (bool): Fit a Gaussian to the RMSF?
nBits (int): Precision of floating point numbers.
Expand Down
42 changes: 1 addition & 41 deletions RMutils/util_RM.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,6 @@ def measure_FDF_parms(FDF, phiArr, fwhmRMSF, dFDF=None, lamSqArr_m2=None,
# Determine the peak channel in the FDF, its amplitude and index
absFDF = np.abs(FDF)
indxPeakPIchan = np.nanargmax(absFDF[1:-1])+1 #Masks out the edge channels, since they can't be fit to.
ampPeakPIchan = absFDF[indxPeakPIchan]

# Measure the RMS noise in the spectrum after masking the peak
dPhi = np.nanmin(np.diff(phiArr))
Expand All @@ -844,42 +843,17 @@ def measure_FDF_parms(FDF, phiArr, fwhmRMSF, dFDF=None, lamSqArr_m2=None,
absFDFmsked = absFDFmsked[np.where(absFDFmsked==absFDFmsked)]
if float(len(absFDFmsked))/len(absFDF)<0.3:
dFDFcorMAD = MAD(absFDF)
dFDFrms = np.sqrt( np.mean(absFDF**2) )
else:
dFDFcorMAD = MAD(absFDFmsked)
dFDFrms = np.sqrt( np.mean(absFDFmsked**2) )

# Default to using the measured FDF if a noise value has not been provided
if dFDF is None:
dFDF = dFDFcorMAD

# Measure the RM of the peak channel
phiPeakPIchan = phiArr[indxPeakPIchan]
dPhiPeakPIchan = fwhmRMSF * dFDF / (2.0 * ampPeakPIchan)
snrPIchan = ampPeakPIchan / dFDF

# Correct the peak for polarisation bias (POSSUM report 11)
ampPeakPIchanEff = ampPeakPIchan
if snrPIchan >= snrDoBiasCorrect:
ampPeakPIchanEff = np.sqrt(ampPeakPIchan**2.0 - 2.3 * dFDF**2.0)

# Calculate the polarisation angle from the channel
peakFDFimagChan = FDF.imag[indxPeakPIchan]
peakFDFrealChan = FDF.real[indxPeakPIchan]
polAngleChan_deg = 0.5 * np.degrees(np.arctan2(peakFDFimagChan,
peakFDFrealChan)) % 180
dPolAngleChan_deg = np.degrees(dFDF / (2.0 * ampPeakPIchan))

# Calculate the derotated polarisation angle and uncertainty
polAngle0Chan_deg = np.degrees(np.radians(polAngleChan_deg) -
phiPeakPIchan * lam0Sq) % 180

nChansGood = np.sum(np.where(lamSqArr_m2==lamSqArr_m2, 1.0, 0.0))
varLamSqArr_m2 = (np.sum(lamSqArr_m2**2.0) -
np.sum(lamSqArr_m2)**2.0/nChansGood) / (nChansGood-1)
dPolAngle0Chan_rad = \
np.sqrt( dFDF**2.0*nChansGood / (4.0*(nChansGood-2.0)*ampPeakPIchan**2.0) *
((nChansGood-1)/nChansGood + lam0Sq**2.0/varLamSqArr_m2) )
dPolAngle0Chan_deg = np.degrees(dPolAngle0Chan_rad)

# Determine the peak in the FDF, its amplitude and Phi using a
# 3-point parabolic interpolation
Expand Down Expand Up @@ -946,20 +920,6 @@ def measure_FDF_parms(FDF, phiArr, fwhmRMSF, dFDF=None, lamSqArr_m2=None,

# Store the measurements in a dictionary and return
mDict = {'dFDFcorMAD': toscalar(dFDFcorMAD),
'dFDFrms': toscalar(dFDFrms),
'phiPeakPIchan_rm2': toscalar(phiPeakPIchan),
'dPhiPeakPIchan_rm2': toscalar(dPhiPeakPIchan),
'ampPeakPIchan': toscalar(ampPeakPIchan),
'ampPeakPIchanEff': toscalar(ampPeakPIchanEff),
'dAmpPeakPIchan': toscalar(dFDF),
'snrPIchan': toscalar(snrPIchan),
'indxPeakPIchan': toscalar(indxPeakPIchan),
'peakFDFimagChan': toscalar(peakFDFimagChan),
'peakFDFrealChan': toscalar(peakFDFrealChan),
'polAngleChan_deg': toscalar(polAngleChan_deg),
'dPolAngleChan_deg': toscalar(dPolAngleChan_deg),
'polAngle0Chan_deg': toscalar(polAngle0Chan_deg),
'dPolAngle0Chan_deg': toscalar(dPolAngle0Chan_deg),
'phiPeakPIfit_rm2': toscalar(phiPeakPIfit),
'dPhiPeakPIfit_rm2': toscalar(dPhiPeakPIfit),
'ampPeakPIfit': toscalar(ampPeakPIfit),
Expand Down
41 changes: 34 additions & 7 deletions RMutils/util_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,32 @@
# #
#=============================================================================#

import os
import sys
import copy
import re
import time
import traceback
import math as m
import numpy as np
import numpy.ma as ma
import scipy.ndimage as ndi
from scipy.stats import norm
#import ConfigParser
import sqlite3
import csv
import json

from RMutils.mpfit import mpfit

from . import __version__
from deprecation import deprecated


C = 2.99792458e8


#-----------------------------------------------------------------------------#
@deprecated(
deprecated_in="1.3.1",
removed_in="1.4",
current_version=__version__,
details="This function is not used anywhere in current RM-Tools.",
)
def config_read(filename, delim='=', doValueSplit=True):
"""
Read a configuration file and output a 'KEY=VALUE' dictionary.
Expand Down Expand Up @@ -179,6 +183,12 @@ def csv_read_to_list(fileName, delim=",", doFloat=False):


#-----------------------------------------------------------------------------#
@deprecated(
deprecated_in="1.3.1",
removed_in="1.4",
current_version=__version__,
details="This function is not used anywhere in RM-Tools.",
)
def cleanup_str_input(textBlock):

# Compile a few useful regular expressions
Expand Down Expand Up @@ -209,6 +219,12 @@ def split_repeat_lst(inLst, nPre, nRepeat):


#-----------------------------------------------------------------------------#
@deprecated(
deprecated_in="1.3.1",
removed_in="1.4",
current_version=__version__,
details="This function is not used anywhere in RM-Tools.",
)
def deg2dms(deg, delim=':', doSign=False, nPlaces=2):
"""
Convert a float in degrees to 'dd mm ss' format.
Expand Down Expand Up @@ -408,7 +424,12 @@ def calculate_StokesI_model(fitDict,freqArr_Hz):




@deprecated(
deprecated_in="1.3",
removed_in="2.0",
current_version=__version__,
details="This function cannot current propagate errors. It may be reactivated if someone works through how to do that.",
)
def renormalize_StokesI_model(fitDict,new_reference_frequency):
"""This functions adjusts the reference frequency for the Stokes I model
and fixes the fit parameters such that the the model is the same. This is
Expand Down Expand Up @@ -785,6 +806,12 @@ def calc_stats(a, maskzero=False):


#-----------------------------------------------------------------------------#
@deprecated(
deprecated_in="1.3.1",
removed_in="1.4",
current_version=__version__,
details="This function is not used anywhere in current RM-Tools.",
)
def sort_nicely(l):
"""
Sort a list in the order a human would.
Expand Down
2 changes: 1 addition & 1 deletion tests/RMclean1D_referencevalues.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"dFDFcorMAD": 0.004604571498930454, "dFDFrms": 0.010632162913680077, "phiPeakPIchan_rm2": 201.0, "dPhiPeakPIchan_rm2": 0.248169183058158, "ampPeakPIchan": 0.6993770003318787, "ampPeakPIchanEff": 0.6993199035324889, "dAmpPeakPIchan": 0.005892556709695188, "snrPIchan": 118.68820866520879, "indxPeakPIchan": 267, "peakFDFimagChan": -0.5605486035346985, "peakFDFrealChan": 0.41822659969329834, "polAngleChan_deg": 153.36335372924805, "dPolAngleChan_deg": 0.2413709843523719, "polAngle0Chan_deg": 44.00355316060791, "dPolAngle0Chan_deg": 1.3733003278171074, "phiPeakPIfit_rm2": 200.32874264404734, "dPhiPeakPIfit_rm2": 0.24808332381938963, "ampPeakPIfit": 0.6996190479469143, "ampPeakPIfitEff": 0.6995619709029499, "dAmpPeakPIfit": 0.005892556709695188, "snrPIfit": 118.72928550620676, "indxPeakPIfit": 266.77624754801576, "peakFDFimagFit": -0.5592812449927833, "peakFDFrealFit": 0.4189165597767205, "polAngleFit_deg": 153.41709453229126, "dPolAngleFit_deg": 0.24128747709042306, "polAngle0Fit_deg": 48.02926667512406, "dPolAngle0Fit_deg": 1.3728252062919697, "cleanCutoff": 0.017677670129085565, "nIter": 11, "mom2CCFDF": 0.9521095156669617, "dPhiObserved_rm2": 0.19385768495688968, "dAmpObserved": 0.004604571498930454, "dPolAngleFitObserved_deg": 0.1885472630634841, "dPolAngleFit0Observed_deg": 1.0727553639839833}
{"dFDFcorMAD": 0.004604571498930454, "phiPeakPIfit_rm2": 200.32874264404734, "dPhiPeakPIfit_rm2": 0.24808332381938963, "ampPeakPIfit": 0.6996190479469143, "ampPeakPIfitEff": 0.6995619709029499, "dAmpPeakPIfit": 0.005892556709695188, "snrPIfit": 118.72928550620676, "indxPeakPIfit": 266.77624754801576, "peakFDFimagFit": -0.5592812449927833, "peakFDFrealFit": 0.4189165597767205, "polAngleFit_deg": 153.41709453229126, "dPolAngleFit_deg": 0.24128747709042306, "polAngle0Fit_deg": 48.02926667512406, "dPolAngle0Fit_deg": 1.3728252062919697, "cleanCutoff": 0.017677670129085565, "nIter": 11, "mom2CCFDF": 0.9521095156669617, "dPhiObserved_rm2": 0.19385768495688968, "dAmpObserved": 0.004604571498930454, "dPolAngleFitObserved_deg": 0.1885472630634841, "dPolAngleFit0Observed_deg": 1.0727553639839833}
Loading

0 comments on commit 0755c39

Please sign in to comment.