Ejemplo n.º 1
0
def parallel_fitter(spec):

    # Make into a OneDSpectrum
    spec_obj = OneDSpectrum(spec, wcs=my_wcs)
    spec_obj = spec_obj.with_spectral_unit(u.m / u.s)

    agd_kwargs = {
        "plot": False,
        "verbose": False,
        "SNR_thresh1": 5.,
        "SNR_thresh2": 8.,
        "SNR2_thresh1": 4.,
        "SNR2_thresh2": 3.5,
        "mode": "conv",
        "deblend": True,
        "intermediate_fit": False,
        "perform_final_fit": False,
        "component_sigma": 5.
    }

    alphas = [5., 10., 15., 20., 30., 50.]

    out = fit_func_gausspy(spec_obj,
                           m31_noise,
                           alphas=alphas,
                           vcent=None,
                           min_finite_chan=30,
                           downsamp_factor=1,
                           max_comp=ncomp_max_fitter,
                           agd_kwargs=agd_kwargs)
    return out
Ejemplo n.º 2
0
def find_and_plot_cont(basename, background='mean'):

    spec = OneDSpectrum.from_hdu(fits.open(basename+'.maxspec.fits'))
    clipped = stats.sigma_clip(spec.value, sigma=1.8, stdfunc=stats.mad_std)
    maxsel = clipped.mask

    spec = OneDSpectrum.from_hdu(fits.open(basename+'.medianspec.fits'))
    clipped = stats.sigma_clip(spec.value, sigma=1.8, stdfunc=stats.mad_std)
    medsel = clipped.mask

    spec = OneDSpectrum.from_hdu(fits.open(basename+'.meanspec.fits'))
    clipped = stats.sigma_clip(spec.value, sigma=1.8, stdfunc=stats.mad_std)
    meansel = clipped.mask

    spec = OneDSpectrum.from_hdu(fits.open(basename+'.{0}spec.fits'.format(background)))

    spec.quicklook()
    pl.plot(spec.spectral_axis, clipped, linewidth=4, alpha=0.75, zorder=10, color='r')
    clipped.mask = medsel
    pl.plot(spec.spectral_axis, clipped, linewidth=3.5, alpha=0.75, zorder=-1)
    clipped.mask = maxsel
    pl.plot(spec.spectral_axis, clipped, linewidth=3, alpha=0.75, zorder=11)

    labels, _ = label(meansel)
    return find_objects(labels)
    data=total_spectrum_hi_radial_peakvel_s.T.reshape(
        (spec_shape, bin_centers.size, 1)),
    wcs=hi_cube_peakvel.wcs)
peakvel_stack_s = peakvel_stack_s.with_mask(
    np.ones_like(peakvel_stack_s, dtype='bool'))
peakvel_stack_s.write(hi_stackpath(
    "peakvel_stacked_radial_south_{0}_noCO.fits".format(wstring)),
                      overwrite=True)

total_spectrum_hi_peakvel = total_spectrum_hi_radial_peakvel.sum(0)

# Save each of these
oned_wcs = hi_cube_peakvel[:, 0, 0].wcs
OneDSpectrum(total_spectrum_hi_peakvel.value,
             unit=total_spectrum_hi_peakvel.unit,
             wcs=oned_wcs).write(hi_stackpath(
                 "peakvel_stacked_{0}_noCO.fits".format(maxrad_string)),
                                 overwrite=True)

total_spectrum_hi_peakvel_n = total_spectrum_hi_radial_peakvel_n.sum(0)

# Save each of these
oned_wcs = hi_cube_peakvel[:, 0, 0].wcs
OneDSpectrum(total_spectrum_hi_peakvel_n.value,
             unit=total_spectrum_hi_peakvel.unit,
             wcs=oned_wcs).write(hi_stackpath(
                 "peakvel_stacked_north_{0}_noCO.fits".format(maxrad_string)),
                                 overwrite=True)

total_spectrum_hi_peakvel_s = total_spectrum_hi_radial_peakvel_s.sum(0)
Ejemplo n.º 4
0
            vels,
            spec,
            0.0 * u.km / u.s,
            delta_vcent=5 * u.km / u.s,
            err=m31_noise,
            verbose=False,
            # plot_fit=True,
            plot_fit=False,
            return_model=False,
            use_emcee=False,
            emcee_kwargs={})

        thick_BICs[cur_iter] = out0.bic

        # Make into a OneDSpectrum
        spec_obj = OneDSpectrum(spec, wcs=my_wcs)
        spec_obj = spec_obj.with_spectral_unit(u.m / u.s)

        agd_kwargs = {
            "plot": False,
            "verbose": False,
            "SNR_thresh1": 5.,
            "SNR_thresh2": 8.,
            "SNR2_thresh1": 4.,
            "SNR2_thresh2": 3.5,
            "mode": "conv",
            "deblend": True,
            "intermediate_fit": False,
            "perform_final_fit": False,
            "component_sigma": 5.
        }
Ejemplo n.º 5
0
def fit_func_simple(spec,
                    noise_val,
                    vcent=None,
                    min_finite_chan=30,
                    downsamp_factor=1,
                    max_comp=10,
                    delta_vcent=5 * u.km / u.s):
    '''
    Wrapper function to work in parallelized map.
    Fits with fit_isoturbHI_model_simple.
    '''

    params_array = np.zeros((4, )) * np.NaN
    uncerts_array = np.zeros((4, )) * np.NaN

    if np.isfinite(spec.filled_data[:]).sum() < min_finite_chan:

        return params_array, uncerts_array, np.NaN

    # Downsample if needed
    if downsamp_factor > 1:
        new_width = np.diff(spec.spectral_axis)[0] * 2
        new_axis = np.arange(spec.spectral_axis.value[0],
                             spec.spectral_axis.value[-1],
                             new_width.value) * new_width.unit
        spec_conv = OneDSpectrum(spec.filled_data[:], wcs=spec.wcs)
        spec_conv = spec_conv.spectral_interpolate(new_axis)

        noise_val /= np.sqrt(downsamp_factor)

    elif downsamp_factor < 1:
        raise ValueError("Cannot upsample data.")
    else:
        spec_conv = spec

    # The function converts to km/s. Don't need to do it twice.
    vels = spec.spectral_axis

    # Still trying to catch this weird edge case with <4 finite
    # points to fit to.
    try:
        out = fit_isoturbHI_model_simple(
            vels,
            spec,
            vcent,
            delta_vcent=delta_vcent,
            err=noise_val,
            verbose=False,
            plot_fit=False,
            return_model=False,
            use_emcee=False,
        )
    except TypeError:
        out = None

    # Too few points to fit
    if out is None:
        return params_array, uncerts_array, np.NaN

    params_array = np.array([par.value for par in out.params.values()])
    uncerts_array = np.array([
        par.stderr if par.stderr is not None else np.NaN
        for par in out.params.values()
    ])

    # Want to return a range of fit statistics.
    fit_stats = np.array([out.bic, out.aic, out.redchi])

    return params_array, uncerts_array, fit_stats
Ejemplo n.º 6
0
figure_folder = allfigs_path("stacked_profiles")
if not os.path.exists(figure_folder):
    os.mkdir(figure_folder)


dr = 500 * u.pc
max_radius = (7.0 * u.kpc).to(u.pc)
wstring = "{0}{1}".format(int(dr.value), dr.unit)
maxrad_string = "{0}{1}".format(int(max_radius.value), max_radius.unit)

# Load the CO stacks

co_stackpath = lambda x: osjoin(iram_co21_14B088_data_path("smooth_2beam/stacked_spectra"), x)

total_spectrum_co_cent = OneDSpectrum.from_hdu(fits.open(co_stackpath("centroid_stacked_{}.fits".format(maxrad_string))))
total_spectrum_co_peakvel = OneDSpectrum.from_hdu(fits.open(co_stackpath("peakvel_stacked_{}.fits".format(maxrad_string))))

# Load the total HI profiles in
hi_stackpath = lambda x: osjoin(fourteenB_HI_data_wGBT_path("smooth_2beam/stacked_spectra"), x)

total_spectrum_hi_cent = OneDSpectrum.from_hdu(fits.open(hi_stackpath("centroid_stacked_{}.fits".format(maxrad_string))))
total_spectrum_hi_peakvel = OneDSpectrum.from_hdu(fits.open(hi_stackpath("peakvel_stacked_{}.fits".format(maxrad_string))))

spectra = [total_spectrum_co_cent,
           total_spectrum_co_peakvel]
hi_spectra = [total_spectrum_hi_cent,
              total_spectrum_hi_peakvel]
co_fit_vals = {}
hi_fit_vals = {}
Ejemplo n.º 7
0
                    continue

                for operation in ('mean', 'max', 'median'):
                    out_fn = f'spectra/{field}_{array}_{band}_spw{spw}_robust{robust}{suffix}.{operation}spec.fits'
                    if overwrite or not os.path.exists(out_fn):
                        spec = getattr(cube, operation)(axis=(1, 2))
                        #spec = cube.apply_numpy_function(getattr(np, 'nan'+operation),
                        #                                 axis=(1,2),
                        #                                 progressbar=True,
                        #                                 projection=True,
                        #                                 how='slice',
                        #                                 unit=cube.unit,
                        #                                )
                        spec.write(out_fn, overwrite=overwrite)

                    spec = OneDSpectrum.from_hdu(fits.open(out_fn))

                    fig_fn = f'spectra/pngs/{field}_{array}_{band}_spw{spw}_robust{robust}{suffix}.{operation}spec.png'
                    pl.clf()
                    spec.quicklook(filename=fig_fn)
                    sel = np.zeros(spec.size, dtype='int')

                    muid = metadata[band][field]['muid_configs']['12Mshort']
                    cdatfile = metadata[band][field]['cont.dat'][muid]
                    contfreqs = parse_contdotdat(cdatfile)

                    for freqrange in contfreqs.split(";"):
                        low, high = freqrange.split("~")
                        high = u.Quantity(high)
                        low = u.Quantity(low, unit=high.unit)
                        sel += (spec.spectral_axis > low) & (spec.spectral_axis
from astropy import units as u

pl.close(1)
pl.close(2)
fig = pl.figure(1, figsize=(25, 10))

spectra = pyspeckit.Spectra(
    pyspeckit.Spectrum(x) for x in glob.glob("spectra/*max.fits"))
spectra.plotter(figure=fig)

spectra.plotter.figure.savefig('full_spectrum_max.png',
                               dpi=200,
                               bbox_inches='tight')

fig2 = pl.figure(2, figsize=(25, 10))

kspectra = [
    OneDSpectrum.from_hdu(fits.open(x)).to(u.K)
    for x in glob.glob("spectra/*max.fits")
]
kspectra_ps = [pyspeckit.Spectrum.from_hdu(kspec.hdu) for kspec in kspectra]
spectra_K = pyspeckit.Spectra(kspectra_ps)
spectra_K.plotter(figure=fig2)

spectra_K.plotter.figure.savefig('full_spectrum_max_K.png',
                                 dpi=200,
                                 bbox_inches='tight')
spectra_K.plotter.figure.savefig('full_spectrum_max_K.pdf',
                                 dpi=200,
                                 bbox_inches='tight')
Ejemplo n.º 9
0
                            use_dask=True).with_spectral_unit(u.GHz)
                    else:
                        log.exception(
                            "File {0} does not exist".format(filename))
                        if os.path.exists(filename[:-5]):
                            log.exception("But {0} does!!!!".format(
                                filename[:-5]))
                        continue

                    for operation in ('mean', 'max', 'median'):
                        out_fn = f'spectra/{field}_{array}_{band}_spw{spw}_robust{robust}{suffix}.{operation}spec.fits'
                        if overwrite or not os.path.exists(out_fn):
                            spec = getattr(cube, operation)(axis=(1, 2))
                            spec.write(out_fn, overwrite=overwrite)

                        spec_jy = OneDSpectrum.from_hdu(
                            fits.open(out_fn)).with_spectral_unit(u.GHz)
                        if cube.shape[0] != spec_jy.size:
                            spec_jy = getattr(cube, operation)(axis=(1, 2))
                            spec_jy.write(out_fn, overwrite=True)

                        jtok = cube.jtok_factors()
                        spec_K = spec_jy * jtok * u.K / (u.Jy / u.beam)

                        for spec, unit in zip((spec_jy, spec_K), ("", "K")):
                            fig_fn = f'spectra/pngs/{field}_{array}_{band}_spw{spw}_robust{robust}{suffix}{unit}.{operation}spec.png'
                            pl.clf()
                            spec.quicklook(filename=fig_fn,
                                           color='k',
                                           linewidth=0.9,
                                           drawstyle='steps-mid')
                            sel = np.zeros(spec.size, dtype='int')
Ejemplo n.º 10
0
    "centroid_stacked_radial_{}.fits".format(wstring)),
                 overwrite=True)

# Separately save the number of pixels in each bin
np.save(
    co_stackpath("radial_stacking_pixelsinbin_{}.npy").format(wstring),
    num_pixels)

# Save the total profiles over the inner 7 kpc

total_spectrum_co = total_spectrum_co_radial.sum(0)

oned_wcs = co_cube[:, 0, 0].wcs
OneDSpectrum(total_spectrum_co.value,
             unit=total_spectrum_co.unit,
             wcs=oned_wcs).write(co_stackpath(
                 "centroid_stacked_{}.fits".format(maxrad_string)),
                                 overwrite=True)

del co_cube

co_cube_peakvel = \
    SpectralCube.read(smooth_2beam_co_path("m33.co21_iram.14B-088_HI.38arcsec.peakvels_corrected.fits"),
                      memmap=False)
co_cube_peakvel.allow_huge_operations = True

log.info("Stacking CO with HI peak vel. 2 * beam")
total_spectrum_co_radial_peakvel = \
    radial_stacking(gal, co_cube_peakvel, dr=dr,
                    max_radius=max_radius,
                    pa_bounds=None,
    peakvel_stack_s = SpectralCube(
        data=total_spectrum_co_radial_peakvel_s.T.reshape(
            (spec_shape, bin_centers.size, 1)),
        wcs=co_cube_peakvel.wcs)
    peakvel_stack_s.write(co_stackpath(
        "peakvel_stacked_radial_south_{0}_sigmacut_{1}.fits".format(
            wstring, level)),
                          overwrite=True)

    total_spectrum_co_peakvel = total_spectrum_co_radial_peakvel.sum(0)

    # Save each of these
    oned_wcs = co_cube_peakvel[:, 0, 0].wcs
    OneDSpectrum(total_spectrum_co_peakvel.value,
                 unit=total_spectrum_co_peakvel.unit,
                 wcs=oned_wcs).write(co_stackpath(
                     "peakvel_stacked_{0}_sigmacut_{1}.fits".format(
                         maxrad_string, level)),
                                     overwrite=True)

    total_spectrum_co_peakvel_n = total_spectrum_co_radial_peakvel_n.sum(0)

    # Save each of these
    oned_wcs = co_cube_peakvel[:, 0, 0].wcs
    OneDSpectrum(total_spectrum_co_peakvel_n.value,
                 unit=total_spectrum_co_peakvel.unit,
                 wcs=oned_wcs).write(co_stackpath(
                     "peakvel_stacked_north_{0}_sigmacut_{1}.fits".format(
                         maxrad_string, level)),
                                     overwrite=True)

    total_spectrum_co_peakvel_s = total_spectrum_co_radial_peakvel_s.sum(0)