pulsar_spectra modules

catalogue

Loads all the data required by vcstools from the data directory.

pulsar_spectra.catalogue.all_flux_from_atnf(query=None)[source]

Queries the ATNF database for flux info for all pulsar at all frequencies.

Parameters
querypsrqpy object, optional

A previous psrqpy.QueryATNF query. Can be supplied to prevent performing a new query.

Returns
jname_cat_dictdict

Catalgoues dictionary with the keys in the format jname_cat_dict[jname][ref][‘Frequency MHz’, ‘Flux Density mJy’, ‘Flux Density error mJy’]

'jname'str

The pulsar’s Jname.

'ref'str

The reference label.

'Frequency MHz'

The observing frequency in MHz.

'Flux Density mJy'

The flux density in mJy.

'Flux Density error mJy'

The error of the flux density in mJy.

pulsar_spectra.catalogue.collect_catalogue_fluxes(only_use=None, exclude=None, query=None)[source]

Collect the fluxes from all of the catalogues recorded in this repo.

Parameters
only_uselist, optional

A list of reference labels (in the format ‘Author_year’) of all the papers you want to use.

excludelist, optional

A list of reference labels (in the format ‘Author_year’) of all the papers you want to exclude.

querypsrqpy object, optional

A previous psrqpy.QueryATNF query. Can be supplied to prevent performing a new query.

Returns
jname_cat_list[jname]dict

Catalgoues dictionary with the keys:

'jname'str

The pulsar’s Jname.

Each dictionary contains a list of lists with the following:

Frequency MHzlist

The observing frequency in MHz.

Flux Density mJylist

The flux density in mJy.

Flux Density error mJylist

The error of the flux density in mJy.

reflist

The reference label (in the format ‘Author_year’).

pulsar_spectra.catalogue.convert_antf_ref(ref_code, ref_dict=None)[source]

Converts an ATNF psrcat reference code to a reference in the format “Author Year”

Parameters
ref_codestr

An ATNF psrcat reference code as found from psrqpy.get_references(updaterefcache=True) and https://www.atnf.csiro.au/research/pulsar/psrcat/psrcat_ref.html.

ref_dictdict, optional

A previous psrqpy.get_references query. Can be supplied to prevent performing a new query.

Returns
refstr

Reference in the format “Author Year”.

pulsar_spectra.catalogue.convert_cat_list_to_dict(jname_cat_list)[source]
Returns
jname_cat_dictdict

Catalgoues dictionary with the keys in the format jname_cat_dict[jname][ref][‘Frequency MHz’, ‘Flux Density mJy’, ‘Flux Density error mJy’]

'jname'str

The pulsar’s Jname.

'ref'str

The reference label.

'Frequency MHz'

The observing frequency in MHz.

'Flux Density mJy'

The flux density in mJy.

'Flux Density error mJy'

The error of the flux density in mJy.

pulsar_spectra.catalogue.flux_from_atnf(pulsar, query=None, ref_dict=None, assumed_error=0.5)[source]

Queries the ATNF database for flux info on a particular pulsar at all frequencies.

Parameters
pulsarstr

The Jname of the pulsar.

querypsrqpy object, optional

A previous psrqpy.QueryATNF query. Can be supplied to prevent performing a new query.

ref_dictdict, optional

A previous psrqpy.get_references query. Can be supplied to prevent performing a new query.

assumed_errorfloat, optional

If no error found, apply this factor to flux to make an assumed error.
Default: 0.5.

Returns
freq_alllist

All frequencies in Hz with flux values on ATNF.

flux_alllist

The flux values corresponding to the freq_all list in mJy.

flux_err_alllist

The uncertainty in the flux_all values.

referenceslist

The reference keys from: https://www.atnf.csiro.au/research/pulsar/psrcat/psrcat_ref.html

pulsar_spectra.catalogue.get_antf_references()[source]

Wrapper for psrqpy.get_references() that ensures the cache is only Updated once.

models

Spectral models from Jankowski et al. 2018 and references within

pulsar_spectra.models.broken_power_law(v, vb, a1, a2, b, v0)[source]

Broken power law:

\[\begin{split}S_v = b \left\{\begin{matrix} x^{a_1} & \mathrm{if} x ≤ x_b \\ x^{a_2}x_b^{a_1-a_2} & \mathrm{otherwise'} \end{matrix}\right.\end{split}\]

where \(x=\frac{v}{1.3e9},x_b=\frac{v_b}{1.3e9}\)

Parameters
vlist

Frequency in Hz.

v_bfloat

The frequency of the break in Hz.

a_1float

The spectral index before the break.

a_2float

The spectral index after the break.

bfloat

Constant.

v0float

Reference frequency.

Returns
S_vlist

The flux density predicted by the model.

pulsar_spectra.models.high_frequency_cut_off_power_law(v, vc, b, v0)[source]

Power law with high-frequency cut-off off:

\[S_v = bx^{-2} \left ( 1 - \frac{x}{x_c} \right ), x < x_c\]

where \(x=\frac{v}{1.3e9},x_c=\frac{v_c}{1.3e9}\)

Parameters
vlist

Frequency in Hz.

v_clist

Cut off frequency in Hz.

bfloat

Constant.

v0float

Reference frequency.

Returns
S_vlist

The flux density predicted by the model.

pulsar_spectra.models.log_parabolic_spectrum(v, a, b, c, v0)[source]

Log-parabolic spectrum:

\[\mathrm{log}_{10} S_v = ax^2 + bx +c\]

where \(x=\mathrm{log}_{10} \left ( \frac{v}{1.3e9} \right )\)

Parameters
vlist

Frequency in Hz.

afloat

Curvature parameter.

bfloat

The spectral index for \(a = 0\).

cfloat

Constant.

v0float

Reference frequency.

Returns
S_vlist

The flux density predicted by the model.

pulsar_spectra.models.low_frequency_turn_over_power_law(v, vc, a, b, beta, v0)[source]

power law with low-frequency turn-over:

\[S_v = bx^{a} exp\left ( \frac{a}{\beta} x_c^{-\beta} \right )\]

where \(x=\frac{v}{1.3e9},x_c=\frac{v_c}{1.3e9}\)

Parameters
vlist

Frequency in Hz.

v_clist

Trun-over frequency in Hz.

afloat

The spectral index.

bfloat

Constant.

betafloat

The smoothness of the turn-over.

v0float

Reference frequency.

Returns
S_vlist

The flux density predicted by the model.

pulsar_spectra.models.model_settings(print_models=False)[source]

Holds metadata about spectral models such as common names and default fit parameters.

Parameters
print_modelsboolean, optional

If true, will print the models dictionary which is useful for debuging new models. Default False.

Returns
model_dictdict

Returns a dictionary in the format

{model_name: [model_function, short_name, start_params, mod_limits]}

pulsar_spectra.models.simple_power_law(v, a, b, v0)[source]

Simple power law:

\[S_v = b x^a\]

where \(x=\frac{v}{1.3e9}\)

Parameters
vlist

Frequency in Hz.

afloat

Spectral Index.

bfloat

Constant.

v0float

Reference frequency.

Returns
S_vlist

The flux density predicted by the model.

spectral_fit

Function used to fit different spectral models to the fluxs_mJy densities of pulsars

pulsar_spectra.spectral_fit.estimate_flux_density(est_freq, model, iminuit_result)[source]

Estimate a pulsar’s flux density using a previous spectra fit.

Parameters
est_freqfloat or list

A single or list of frequencies to estimate flux at (in MHz).

modelfunction

The pulsar spectra model function from pulsar_spectra.models().

miminuit.Minuit

The Minuit class after being fit in pulsar_spectra.spectral_fit.iminuit_fit_spectral_model().

Returns
fitted_fluxfloat or list

The estimated flux density of the pulsar at the input frequencies.

fitted_flux_errfloat or list

The estimated flux density errors of the pulsar at the input frequencies.

pulsar_spectra.spectral_fit.find_best_spectral_fit(pulsar, freqs_MHz, fluxs_mJy, flux_errs_mJy, ref_all, plot_all=False, plot_best=False, plot_compare=False, plot_error=True, alternate_style=False, axis=None, secondary_fit=False, fit_range=None)[source]

Fit pulsar spectra with iminuit.

Parameters
pulsarstr

The Jname of the pulsar to be fit.

freqs_MHzlist

A list of the frequencies in MHz.

fluxs_mJylist

A list of the flux density in mJy.

flux_errs_mJylist

A list of the uncertainty of the flux density in mJy.

ref_alllist

A list of the reference label (in the format ‘Author_year’).

plot_allboolean, optional

If you want to plot the result of all fits.
Default: False.

plot_bestboolean, optional

If you want to only plot the best fit.
Default: False.

plot_compareboolean, optional

If you want to make a single plot with the result of all fits.
Default: False.

plot_errorboolean, optional

If you want to include the fit error in the plot.
Default: True.

alternate_styleboolean, optional

Plot with the alternate plot style based on Jankowski 2018.
Default: False.

axisAxes, optional

The axes with which the spectrum will be plotted.
Default: None.

secondary_fitboolean, optional

Plot model with an alternate style and without markers. Does not work for comparison plots.
Default: False.

fit_rangetuple, optional

Frequency range to plot the second model over.
Default: None.

Returns
model_namestr

The best fit model name from pulsar_spectra.models().

miminuit.Minuit

The Minuit class after being fit in pulsar_spectra.spectral_fit.iminuit_fit_spectral_model().

fit_infostr

The string to label the fit with from pulsar_spectra.spectral_fit.iminuit_fit_spectral_model().

p_bestfloat

The probability that the best-fit model is actually the best-fit model.

p_categorystr

Category based on the quality of spectral fit, as defined in Jankowski et al. (2018).

pulsar_spectra.spectral_fit.huber_loss_function(sq_resi, k=1.345)[source]

Robust loss function which penalises outliers, as detailed in Jankowski et al (2018).

Parameters
sq_resifloat or list

A single or list of the squared residuals.

kfloat, optional

A constant that defines at which distance the loss function starts to penalize outliers.
Default: 1.345.

Returns
rhofloat or list

The modified squared residuals.

pulsar_spectra.spectral_fit.iminuit_fit_spectral_model(freqs_MHz, fluxs_mJy, flux_errs_mJy, ref, model_name='simple_power_law', start_params=None, mod_limits=None, plot=False, plot_error=True, save_name='fit.png', alternate_style=False, axis=None, secondary_fit=False, fit_range=None)[source]

Fit pulsar spectra with iminuit.

Parameters
freqs_MHzlist

A list of the frequencies in MHz.

fluxs_mJylist

A list of the flux density in mJy.

flux_errs_mJylist

A list of the uncertainty of the flux density in mJy.

reflist

A list of the reference label (in the format ‘Author_year’).

model_namefunction, optional

One of the model names from pulsar_spectra.models.model_settings(). Default: pulsar_spectra.models.simple_power_law().

start_paramstuple, optional

A tuple of the starting paramaters for each input to the model that iminuit will use as an initial estimate. If none provided, will use the defaults from pulsar_spectra.models.model_settings().

mod_limitslist of `tuple`s, optional

A list of tuples where each tuples is the minimum and maximum limits that will be applied to the model by iminuit. If none provided, will use the defaults from pulsar_spectra.models.model_settings().

plotboolean, optional

If you want to plot the result of the fit.
Default: False.

plot_errorboolean, optional

If you want to include the fit error in the plot.
Default: True.

save_namestr, optional

The name of the saved plot.
Default: “fit.png”.

alternate_styleboolean, optional

If you want to use the alternate plot style.
Default: False.

axisAxes, optional

The axes with which the spectrum will be plotted.
None.

secondary_fitboolean, optional

Plot model with an alternate style and without markers.
Default: False.

fit_rangetuple, optional

Frequency range to plot the second model over.
Default: None.

Returns
aicfloat

The Akaike information criterion of the fit.

miminuit.Minuit

The Minuit class after being fit in pulsar_spectra.spectral_fit.iminuit_fit_spectral_model().

fit_infostr

The string to label the fit with from pulsar_spectra.spectral_fit.iminuit_fit_spectral_model().

pulsar_spectra.spectral_fit.plot_fit(freqs_MHz, fluxs_mJy, flux_errs_mJy, ref, model, iminuit_result, fit_info, plot_error=True, save_name='fit.png', alternate_style=False, axis=None, secondary_fit=False, fit_range=None)[source]

Create a plot of the pulsar spectral fit.

Parameters
freqs_MHzlist

A list of the frequencies in MHz.

fluxs_mJylist

A list of the flux density in mJy.

flux_errs_mJylist

A list of the uncertainty of the flux density in mJy.

reflist

A list of the reference label (in the format ‘Author_year’).

modelfunction

One of the model functions from pulsar_spectra.models().

iminuit_resultiminuit.Minuit

The Minuit class after being fit in pulsar_spectra.spectral_fit.iminuit_fit_spectral_model().

fit_infostr

The string to label the fit with from pulsar_spectra.spectral_fit.iminuit_fit_spectral_model().

plot_errorboolean, optional

If you want to include the fit error in the plot.
Default: True.

save_namestr, optional

The name of the saved plot.
Default: “fit.png”.

alternate_styleboolean, optional

Plot with the alternate plot style based on Jankowski 2018.
Default: False.

axisAxes, optional

The axes with which the spectrum will be plotted.
None.

secondary_fitboolean, optional

Plot model with an alternate style and without markers.
Default: False.

fit_rangetuple, (float, float) optional

Frequency range to plot the second model over in MHz, eg. (100, 3000).
Default: None, will use input frequency range.

pulsar_spectra.spectral_fit.robust_cost_function(f_y, y, sigma_y, k=1.345)[source]

Robust cost function. The negative log-likelihood of a Gaussian likelihood with Huber loss.

Parameters
f_ylist

A list of predicted values according to the model.

ylist

A list of measured values at the same frequency as the model values.

sigma_ylist

A list of uncertainties corresponding to the measured values y.

kfloat, optional

A constant that defines at which distance the loss function starts to penalize outliers.
Default: 1.345.

Returns
betafloat

The cost of the model fit.