Esempio n. 1
0
def do_vca(vcacube, array_save_loc, fig_save_loc):
    """This function greets to
    the person passed in as
    parameter"""

    vca_array = np.zeros(3)
    #arlen=int(len(vcacube.data[:,0,0]))/10

    #do full thickness mom0 SPS and add to array first
    #import data and compute moment 0
    moment0 = vcacube.moment(order=0)

    #compute SPS, add in distance at some point as parameter
    pspec = PowerSpectrum(moment0)
    pspec.run(verbose=False, xunit=u.pix**-1)
    vca_array = np.vstack(
        (vca_array, [pspec.slope,
                     len(vcacube[:, 0, 0]), pspec.slope_err]))

    #iterate VCA over fractions of the total width of the PPV vcacube
    for i in [128, 64, 32, 16, 8, 4, 2, 1]:
        vcacube.allow_huge_operations = True
        downsamp_vcacube = vcacube.downsample_axis(i, axis=0)
        downsamp_vcacube.allow_huge_operations = True
        vca = VCA(downsamp_vcacube)
        vca.run(verbose=False,
                beam_correct=correctbeam,
                save_name=fig_save_loc + '_thickness' + str(i) + '.png')
        vca_array = np.vstack((vca_array, [vca.slope, i, vca.slope_err]))
    vca_array = vca_array[1:, :]

    #save the array for future plotting without recomputing
    np.save(array_save_loc, vca_array)
Esempio n. 2
0
def run_pspec(cube,
              distance=414 * u.pc,
              xunit=u.pix**-1,
              pickle_file=None,
              keep_data=False,
              **kwargs):
    cube = SpectralCube.read(cube)
    mom0_hdu = cube.moment0().hdu
    pspec = PowerSpectrum(mom0_hdu, distance=distance)
    pspec.run(xunit=xunit, **kwargs)
    if pickle_file:
        pspec.save_results(pickle_file, keep_data=keep_data)
    return pspec
Esempio n. 3
0
def do_vca(input):
    #split inputs into the components
    vcacube, chansamps, array_save_loc, fig_save_loc = input
    print('starting' + str(vcacube))

    #load in the vcacube
    vcacube = SpectralCube.read(vcacube)

    #check for only nans in first slice of cube, even though it reads the unmasked
    # data the data in the fits may be masked in some way before it reaches this point which will cause the vca to fail if there are only nonfinite values in the subcube

    finites = 0
    nonfinites = 0
    for checkx in np.arange(0, len(vcacube.unmasked_data[0, :, 0])):
        for checky in np.arange(0, len(vcacube.unmasked_data[0, 0, :])):
            if np.isfinite(vcacube.unmasked_data[0, checkx, checky]) == True:
                finites += 1
            else:
                nonfinites += 1

    #do vca or skip depending on whether its only NaNs
    if finites < 1:
        return 'data is only NaNs/inf'
    else:
        #do full thickness mom0 SPS and add to array first
        #import data and compute moment 0
        moment0 = vcacube.moment(order=0)

        #compute SPS, add in distance at some point as parameter
        pspec = PowerSpectrum(moment0)
        pspec.run(verbose=False, xunit=u.pix**-1)
        vca_array = [pspec.slope, len(vcacube[:, 0, 0]), pspec.slope_err]

        #iterate VCA over fractions of the total width of the PPV vcacube
        #for i in [128,64,32,16,8,4,2,1]:
        for i in chansamps:
            vcacube.allow_huge_operations = True
            downsamp_vcacube = vcacube.downsample_axis(i, axis=0)
            downsamp_vcacube.allow_huge_operations = True
            vca = VCA(downsamp_vcacube)
            vca.run(verbose=True, save_name=f'{fig_save_loc}_thickness{i}.png')
            vca_array = np.vstack((vca_array, [vca.slope, i, vca.slope_err]))

        #save the array for future plotting without recomputing
        np.save(array_save_loc, vca_array)
        return vca_array
    print('finished' + str(vcacube))
Esempio n. 4
0
def pspec_noise(cube="../subcubes/c18o_jrf_l1641n.fits",
                vel_low=12 * u.km / u.s,
                vel_hi=16 * u.km / u.s,
                run_kwargs={},
                pspec_file="pspec_noise_c18o_jrf_l1641n"):
    try:
        cube = SpectralCube.read(cube)
    except:
        pass

    noise_hdu = cube.spectral_slab(vel_low, vel_hi).moment0().hdu
    noise_pspec = PowerSpectrum(noise_hdu)
    noise_pspec.run(**run_kwargs)

    noise_pspec.save_results(pspec_file)

    return noise_pspec
Esempio n. 5
0
def test_pspec(plotname="pspec_rnoise_beamsmooth_apodizetukey.pdf",
               size=256,
               powerlaw=3.,
               run_kwargs={
                   'verbose': False,
                   'apodize_kernel': 'tukey'
               },
               plot_kwargs={'fit_color': 'black'},
               beam_smooth=True,
               pixel_scale=2 * u.arcsec,
               bmin=8.09 * u.arcsec,
               bmaj=10.01 * u.arcsec,
               bpa=-12.9 * u.deg,
               restfreq=1.4 * u.GHz,
               bunit=u.K):
    from spectral_cube import Projection
    from radio_beam import Beam

    rnoise_img = make_extended(size, powerlaw)
    # Create a FITS HDU
    rnoise_hdu = create_fits_hdu(rnoise_img, 2 * u.arcsec, 2 * u.arcsec,
                                 rnoise_img.shape, 1.4 * u.GHz, u.K)

    pspec = PowerSpectrum(rnoise_hdu)

    if beam_smooth:
        pencil_beam = Beam(0 * u.deg)
        rnoise_proj = Projection.from_hdu(rnoise_hdu).with_beam(pencil_beam)
        new_beam = Beam(bmaj, bmin, bpa)
        rnoise_conv = rnoise_proj.convolve_to(new_beam)

        # hdr = fits.Header(header)
        # rnoise_hdu = fits.PrimaryHDU(rnoise_img, header=hdr)
        pspec = PowerSpectrum(rnoise_conv)

    pspec.run(**run_kwargs)
    pspec.plot_fit(save_name=plotname, **plot_kwargs)

    return pspec
Esempio n. 6
0
    beamfwhm = 3 * u.arcsec
    imshape = rnoise_img.shape
    restfreq = 1.4 * u.GHz
    bunit = u.K

    plaw_hdu = create_fits_hdu(rnoise_img, pixel_scale, beamfwhm, imshape,
                               restfreq, bunit)

    plt.imshow(plaw_hdu.data, cmap='viridis')
    plt.savefig(osjoin(fig_path, "rednoise_slope3_img.png"))
    plt.close()

    pspec = PowerSpectrum(plaw_hdu)
    pspec.run(verbose=True,
              radial_pspec_kwargs={'binsize': 1.0},
              fit_kwargs={'weighted_fit': False},
              fit_2D=False,
              low_cut=1. / (60 * u.pix),
              save_name=osjoin(fig_path, "rednoise_pspec_slope3.png"))

    pspec_partial = PowerSpectrum(rnoise_img[:128, :128],
                                  header=plaw_hdu.header)
    pspec_partial.run(verbose=False, fit_2D=False, low_cut=1 / (60. * u.pix))
    plt.imshow(np.log10(pspec_partial.ps2D))
    plt.savefig(osjoin(fig_path, "rednoise_pspec_slope3_2D_slicecross.png"))
    plt.close()

    pspec2 = PowerSpectrum(plaw_hdu)
    pspec2.run(
        verbose=False,
        radial_pspec_kwargs={'binsize': 1.0},
        fit_kwargs={'weighted_fit': False},
Esempio n. 7
0
    pixel_scale = 3 * u.arcsec
    beamfwhm = 3 * u.arcsec
    imshape = rnoise_img.shape
    restfreq = 1.4 * u.GHz
    bunit = u.K

    plaw_hdu = create_fits_hdu(rnoise_img, pixel_scale, beamfwhm, imshape,
                               restfreq, bunit)

    plt.imshow(plaw_hdu.data, cmap='viridis')
    plt.savefig(osjoin(fig_path, "rednoise_slope3_img.png"))
    plt.close()

    pspec = PowerSpectrum(plaw_hdu)
    pspec.run(verbose=True, radial_pspec_kwargs={'binsize': 1.0},
              fit_kwargs={'weighted_fit': False}, fit_2D=False,
              low_cut=1. / (60 * u.pix),
              save_name=osjoin(fig_path, "rednoise_pspec_slope3.png"))

    pspec_partial = PowerSpectrum(rnoise_img[:128, :128], header=plaw_hdu.header)
    pspec_partial.run(verbose=False, fit_2D=False, low_cut=1 / (60. * u.pix))
    plt.imshow(np.log10(pspec_partial.ps2D))
    plt.savefig(osjoin(fig_path, "rednoise_pspec_slope3_2D_slicecross.png"))
    plt.close()

    pspec2 = PowerSpectrum(plaw_hdu)
    pspec2.run(verbose=False, radial_pspec_kwargs={'binsize': 1.0},
               fit_kwargs={'weighted_fit': False}, fit_2D=False,
               low_cut=1. / (60 * u.pix),
               apodize_kernel='hanning',)

    pspec3 = PowerSpectrum(plaw_hdu)
Esempio n. 8
0
size = 512
slope = 3

test_img = fits.PrimaryHDU(
    make_extended(size,
                  powerlaw=slope,
                  ellip=0.4,
                  theta=(60 * u.deg).to(u.rad),
                  randomseed=345987))

# The power-law behaviour continues up to ~1/4 of the size
pspec = PowerSpectrum(test_img)
pspec.run(fit_2D=True,
          radial_pspec_kwargs={'binsize': 2.0},
          fit_kwargs={'weighted_fit': False},
          low_cut=1 / (15 * u.pix),
          verbose=False)

print("{0}+/-{1}".format(pspec.slope, pspec.slope_err))
print("{0}+/-{1}".format(pspec.slope2D, pspec.slope2D_err))
print("{0}+/-{1}".format(pspec.ellip2D, pspec.ellip2D_err))
print("{0}+/-{1}".format(pspec.theta2D, pspec.theta2D_err))

# pspec.plot_fit(show_2D=True)

width = 8.75
# fig_ratio = (4.4 / 6.4) / 2
height = 5.07
figsize = (width, height)
Esempio n. 9
0
        # Now open the kernel file
        kernfits_name = names[name][0]
        kernfits_ext = names[name][1]

        kernel_filename = osjoin(kern_path, kernfits_name)

        kern_proj = Projection.from_hdu(fits.open(osjoin(kern_path, kernel_filename))[kernfits_ext])

        img_scale = np.abs(proj_plane_pixel_scales(proj.wcs))[0]
        kern_scale = np.abs(proj_plane_pixel_scales(kern_proj.wcs))[0]

        kernel = resize_psf(kern_proj.value, kern_scale, img_scale)

        # Normalize to make a kernel
        kernel /= kernel.sum()

        kern_pspec = PowerSpectrum((kernel, kern_proj.header))
        kern_pspec.run(verbose=False, fit_2D=False)

        save_name = "{0}_kernel_{1}.pspec.pkl".format(name, gal.lower())

        kern_pspec.save_results(osjoin(data_path, gal, save_name),
                                keep_data=True)

        # plt.draw()

        # input("?")

        # # plt.clf()
        # plt.close()
Esempio n. 10
0
mvc_val = mvc.ps1D
mvc_slope = mvc.slope
mvc_slope2D = mvc.slope2D

# Spatial Power Spectrum/ Bispectrum

from turbustat.statistics import (PSpec_Distance, BiSpectrum_Distance,
                                  BiSpectrum, PowerSpectrum)

pspec_distance = \
    PSpec_Distance(dataset1["moment0"],
                   dataset2["moment0"]).distance_metric()

pspec = PowerSpectrum(dataset1['moment0'])
pspec.run()

pspec_val = pspec.ps1D
pspec_slope = pspec.slope
pspec_slope2D = pspec.slope2D

bispec_distance = \
    BiSpectrum_Distance(dataset1["moment0"],
                        dataset2["moment0"]).distance_metric()

bispec_val = bispec_distance.bispec1.bicoherence

bispec_meansub = BiSpectrum(dataset1['moment0'])
bispec_meansub.run(mean_subtract=True)

bispec_val_meansub = bispec_meansub.bicoherence
do_makepspec_co = False
do_fitpspec_co = False

do_makepspec_dust_smooth = False
do_fitpspec_dust_smooth = True

if do_makepspec:

    pspec_name = osjoin(data_path, 'M31_CO', 'm33_hi_co_dustSD.pspec.pkl')

    gas_sd = hi_proj * hi_mass_conversion + co10_mass_conversion * co_proj
    gas_sd[np.isnan(hi_proj)] = np.NaN

    pspec = PowerSpectrum(gas_sd, distance=720 * u.kpc)

    pspec.run(verbose=False, fit_2D=False, high_cut=10**-1.3 / u.pix)
    # pspec.plot_fit()

    pspec.save_results(pspec_name)

# Fit the pspec.
if do_fitpspec:

    pspec = PowerSpectrum.load_results(pspec_name)
    pspec.load_beam()

    beam_size = pspec._beam.major.to(u.deg) / pspec._ang_size.to(u.deg)
    beam_size = beam_size.value
    beam_gauss_width = beam_size / np.sqrt(8 * np.log(2))

    high_cut = (1 / (beam_gauss_width * 3.))
    hi_satpt = 10.  # Msol / pc^-2
    # hi_satpt = 15. # Msol / pc^-2

    # dense gas to dust conversion of 340 for LMC from Roman-Duval+2014

    gdr = fitinfo_dict[gal]['GDR']

    dust_satpt = (hi_satpt / gdr) / fitinfo_dict[gal]['cosinc'].value
    sat_mask = proj_coldens >= dust_satpt

    dust_sat = proj_coldens.copy()
    dust_sat[sat_mask] = dust_satpt

    pspec = PowerSpectrum(fits.PrimaryHDU(proj_coldens, hdr),
                          distance=fitinfo_dict[gal]['distance'])
    pspec.run(verbose=False, fit_2D=False)

    pspec_sat = PowerSpectrum(fits.PrimaryHDU(dust_sat, hdr),
                              distance=fitinfo_dict[gal]['distance'])
    pspec_sat.run(verbose=False, fit_2D=False)

    beam_size = pspec_sat._beam.major.to(u.deg) / pspec_sat._ang_size.to(u.deg)
    beam_size = beam_size.value
    beam_gauss_width = beam_size / np.sqrt(8 * np.log(2))

    high_cut = (1 / (beam_gauss_width * 3.))

    fit_mask = pspec_sat.freqs.value < high_cut

    # And cut out the largest scales due to expected deviations with
    # small stddev
            else:
                save_name = "{0}_{1}_{2}_{3}_mjysr.pspec.pkl".format(gal.lower(),
                                                                     name,
                                                                     res_type,
                                                                     slice_name)

            # For now skip already saved power-spectra
            if os.path.exists(osjoin(data_path, gal, save_name)) and skip_check:
                print("Already saved pspec for {}. Skipping".format(filename))
                continue
            else:
                os.system("rm -f {}".format(osjoin(data_path, gal, save_name)))

            pspec = PowerSpectrum(proj[slicer], distance=dist)
            pspec.run(verbose=False, beam_correct=False, fit_2D=False,
                      high_cut=0.1 / u.pix,
                      use_pyfftw=True, threads=ncores,
                      apodize_kernel='tukey', alpha=0.3)

            pspec.save_results(osjoin(data_path, gal, save_name),
                               keep_data=False)


if do_fit_pspec:

    # Load model functions
    repo_path = os.path.expanduser("~/ownCloud/project_code/DustyPowerSpectra/")
    code_name = os.path.join(repo_path, "models.py")
    exec(compile(open(code_name, "rb").read(), code_name, 'exec'))

    # Make a plot output folder
    plot_folder = osjoin(data_path, "{}_plots".format(gal))
import astropy.units as u

plt.rcParams['axes.unicode_minus'] = False

size = 512
slope = 3

test_img = fits.PrimaryHDU(make_extended(size, powerlaw=slope,
                                         ellip=0.4,
                                         theta=(60 * u.deg).to(u.rad),
                                         randomseed=345987))

# The power-law behaviour continues up to ~1/4 of the size
pspec = PowerSpectrum(test_img)
pspec.run(fit_2D=True, radial_pspec_kwargs={'binsize': 2.0},
          fit_kwargs={'weighted_fit': False},
          low_cut=1 / (15 * u.pix),
          verbose=False)

print("{0}+/-{1}".format(pspec.slope, pspec.slope_err))
print("{0}+/-{1}".format(pspec.slope2D, pspec.slope2D_err))
print("{0}+/-{1}".format(pspec.ellip2D, pspec.ellip2D_err))
print("{0}+/-{1}".format(pspec.theta2D, pspec.theta2D_err))

# pspec.plot_fit(show_2D=True)

width = 8.75
# fig_ratio = (4.4 / 6.4) / 2
height = 5.07
figsize = (width, height)

fig = plt.figure(figsize=figsize)
Esempio n. 15
0
                 save_name=osjoin(fig_path, "pdf_design4_mom0_plaw.png"))

    cube = SpectralCube.read(osjoin(data_path, "Design4_flatrho_0021_00_radmc.fits"))
    pdf_cube = PDF(cube)
    pdf_cube.run(verbose=True, do_fit=False,
                 save_name=osjoin(fig_path, "pdf_design4.png"))

# PSpec
if run_pspec:

    from turbustat.statistics import PowerSpectrum

    moment0 = fits.open(osjoin(data_path, "Design4_flatrho_0021_00_radmc_moment0.fits"))[0]

    pspec = PowerSpectrum(moment0, distance=250 * u.pc)
    pspec.run(verbose=True, xunit=u.pix**-1,
              save_name=osjoin(fig_path, "design4_pspec.png"))

    pspec.run(verbose=True, xunit=u.pix**-1,
              low_cut=0.025 / u.pix, high_cut=0.1 / u.pix,
              save_name=osjoin(fig_path, "design4_pspec_limitedfreq.png"))

    print(pspec.slope2D, pspec.slope2D_err)
    print(pspec.ellip2D, pspec.ellip2D_err)
    print(pspec.theta2D, pspec.theta2D_err)

    # How about fitting a break?
    pspec = PowerSpectrum(moment0, distance=250 * u.pc)
    pspec.run(verbose=True, xunit=u.pc**-1,
              low_cut=0.025 / u.pix, high_cut=0.4 / u.pix, fit_2D=False,
              fit_kwargs={'brk': 0.1 / u.pix, 'log_break': False},
              save_name=osjoin(fig_path, "design4_pspec_breakfit.png"))
Esempio n. 16
0
def generate_unitvals():

    import numpy as np
    import astropy.units as u

    # The machine producing these values should have emcee installed!
    try:
        import emcee
    except ImportError:
        raise ImportError("Install emcee to generate unit test data.")

    from turbustat.tests._testing_data import dataset1, dataset2

    # Wavelet Transform

    from turbustat.statistics import Wavelet_Distance, Wavelet

    wavelet_distance = \
        Wavelet_Distance(dataset1["moment0"],
                         dataset2["moment0"]).distance_metric()

    wavelet_val = wavelet_distance.wt1.values
    wavelet_slope = wavelet_distance.wt1.slope

    # Wavelet with a break
    wave_break = Wavelet(dataset1['moment0']).run(xhigh=7 * u.pix,
                                                  brk=5.5 * u.pix)

    wavelet_slope_wbrk = wave_break.slope
    wavelet_brk_wbrk = wave_break.brk.value

    # MVC

    from turbustat.statistics import MVC_Distance, MVC

    mvc_distance = MVC_Distance(dataset1, dataset2).distance_metric()

    mvc = MVC(dataset1["centroid"], dataset1["moment0"], dataset1["linewidth"])
    mvc.run()

    mvc_val = mvc.ps1D
    mvc_slope = mvc.slope
    mvc_slope2D = mvc.slope2D

    # Spatial Power Spectrum/ Bispectrum

    from turbustat.statistics import (PSpec_Distance, Bispectrum_Distance,
                                      Bispectrum, PowerSpectrum)

    pspec_distance = \
        PSpec_Distance(dataset1["moment0"],
                       dataset2["moment0"]).distance_metric()

    pspec = PowerSpectrum(dataset1['moment0'])
    pspec.run()

    pspec_val = pspec.ps1D
    pspec_slope = pspec.slope
    pspec_slope2D = pspec.slope2D

    bispec_distance = \
        Bispectrum_Distance(dataset1["moment0"],
                            dataset2["moment0"]).distance_metric()

    bispec_val = bispec_distance.bispec1.bicoherence

    azimuthal_slice = bispec_distance.bispec1.azimuthal_slice(
        16, 10, value='bispectrum_logamp', bin_width=5 * u.deg)
    bispec_azim_bins = azimuthal_slice[16][0]
    bispec_azim_vals = azimuthal_slice[16][1]
    bispec_azim_stds = azimuthal_slice[16][2]

    bispec_meansub = Bispectrum(dataset1['moment0'])
    bispec_meansub.run(mean_subtract=True)

    bispec_val_meansub = bispec_meansub.bicoherence

    # Genus

    from turbustat.statistics import GenusDistance, Genus

    smooth_scales = np.linspace(1.0, 0.1 * min(dataset1["moment0"][0].shape),
                                5)

    genus_distance = \
        GenusDistance(dataset1["moment0"],
                      dataset2["moment0"],
                      lowdens_percent=20,
                      genus_kwargs=dict(match_kernel=True)).distance_metric()

    # The distance method requires standardizing the data. Make a
    # separate version that isn't
    genus = Genus(dataset1['moment0'], smoothing_radii=smooth_scales)
    genus.run(match_kernel=True)

    genus_val = genus.genus_stats

    # Delta-Variance

    from turbustat.statistics import DeltaVariance_Distance, DeltaVariance

    delvar_distance = \
        DeltaVariance_Distance(dataset1["moment0"],
                               dataset2["moment0"],
                               weights1=dataset1["moment0_error"][0],
                               weights2=dataset2["moment0_error"][0],
                               delvar_kwargs=dict(xhigh=11 * u.pix))

    delvar_distance.distance_metric()

    delvar = DeltaVariance(dataset1["moment0"],
                           weights=dataset1['moment0_error'][0]).run(xhigh=11 *
                                                                     u.pix)

    delvar_val = delvar.delta_var
    delvar_slope = delvar.slope

    # Test with a break point
    delvar_wbrk = \
      DeltaVariance(dataset1["moment0"],
                    weights=dataset1['moment0_error'][0]).run(xhigh=11 * u.pix,
                                                              brk=6 * u.pix)

    delvar_slope_wbrk = delvar_wbrk.slope
    delvar_brk = delvar_wbrk.brk.value

    # Change boundary conditions

    delvar_fill = \
        DeltaVariance(dataset1["moment0"],
                      weights=dataset1['moment0_error'][0]).run(xhigh=11 * u.pix,
                                                                boundary='fill',
                                                                nan_treatment='interpolate')

    delvar_fill_val = delvar_fill.delta_var
    delvar_fill_slope = delvar_fill.slope

    # VCA/VCS

    from turbustat.statistics import VCA_Distance, VCS_Distance, VCA

    vcs_distance = VCS_Distance(dataset1["cube"],
                                dataset2["cube"],
                                fit_kwargs=dict(high_cut=0.3 / u.pix,
                                                low_cut=3e-2 / u.pix))
    vcs_distance.distance_metric()

    vcs_val = vcs_distance.vcs1.ps1D
    vcs_slopes = vcs_distance.vcs1.slope

    vca_distance = VCA_Distance(dataset1["cube"],
                                dataset2["cube"]).distance_metric()

    vca = VCA(dataset1['cube'])
    vca.run()

    vca_val = vca.ps1D
    vca_slope = vca.slope
    vca_slope2D = vca.slope2D

    # Tsallis

    from turbustat.statistics import Tsallis

    tsallis_kwargs = {"sigma_clip": 5, "num_bins": 100}

    tsallis = Tsallis(dataset1['moment0'], lags=[1, 2, 4, 8, 16] * u.pix)
    tsallis.run(periodic=True, **tsallis_kwargs)

    tsallis_val = tsallis.tsallis_params
    tsallis_stderrs = tsallis.tsallis_stderrs

    tsallis_noper = Tsallis(dataset1['moment0'], lags=[1, 2, 4, 8, 16] * u.pix)
    tsallis_noper.run(periodic=False, num_bins=100)

    tsallis_val_noper = tsallis_noper.tsallis_params

    # High-order stats

    from turbustat.statistics import StatMoments_Distance, StatMoments

    moment_distance = \
        StatMoments_Distance(dataset1["moment0"],
                             dataset2["moment0"]).distance_metric()

    kurtosis_val = moment_distance.moments1.kurtosis_hist[1]
    skewness_val = moment_distance.moments1.skewness_hist[1]

    # Save a few from the non-distance version
    tester = StatMoments(dataset1["moment0"])
    tester.run()

    kurtosis_nondist_val = tester.kurtosis_hist[1]
    skewness_nondist_val = tester.skewness_hist[1]

    # Non-periodic
    tester = StatMoments(dataset1["moment0"])
    tester.run(periodic=False)

    kurtosis_nonper_val = tester.kurtosis_hist[1]
    skewness_nonper_val = tester.skewness_hist[1]

    # PCA

    from turbustat.statistics import PCA_Distance, PCA
    pca_distance = PCA_Distance(dataset1["cube"],
                                dataset2["cube"]).distance_metric()

    pca = PCA(dataset1["cube"], distance=250 * u.pc)
    pca.run(mean_sub=True,
            eigen_cut_method='proportion',
            min_eigval=0.75,
            spatial_method='contour',
            spectral_method='walk-down',
            fit_method='odr',
            brunt_beamcorrect=False,
            spectral_output_unit=u.m / u.s)

    pca_val = pca.eigvals
    pca_spectral_widths = pca.spectral_width().value
    pca_spatial_widths = pca.spatial_width().value

    pca_fit_vals = {
        "index": pca.index,
        "gamma": pca.gamma,
        "intercept": pca.intercept().value,
        "sonic_length": pca.sonic_length()[0].value
    }

    # Now get those values using mcmc
    pca.run(mean_sub=True,
            eigen_cut_method='proportion',
            min_eigval=0.75,
            spatial_method='contour',
            spectral_method='walk-down',
            fit_method='bayes',
            brunt_beamcorrect=False,
            spectral_output_unit=u.m / u.s)

    pca_fit_vals["index_bayes"] = pca.index
    pca_fit_vals["gamma_bayes"] = pca.gamma
    pca_fit_vals["intercept_bayes"] = pca.intercept().value
    pca_fit_vals["sonic_length_bayes"] = pca.sonic_length()[0].value

    # Record the number of eigenvalues kept by the auto method
    pca.run(mean_sub=True,
            n_eigs='auto',
            min_eigval=0.001,
            eigen_cut_method='value',
            decomp_only=True)

    pca_fit_vals["n_eigs_value"] = pca.n_eigs

    # Now w/ the proportion of variance cut
    pca.run(mean_sub=True,
            n_eigs='auto',
            min_eigval=0.99,
            eigen_cut_method='proportion',
            decomp_only=True)

    pca_fit_vals["n_eigs_proportion"] = pca.n_eigs

    # SCF

    from turbustat.statistics import SCF_Distance, SCF

    scf_distance = SCF_Distance(dataset1["cube"], dataset2["cube"],
                                size=11).distance_metric()

    scf = SCF(dataset1['cube'], size=11).run()

    scf_val = scf.scf_surface
    scf_spectrum = scf.scf_spectrum
    scf_slope = scf.slope
    scf_slope2D = scf.slope2D

    # Now run the SCF when the boundaries aren't continuous
    scf_distance_cut_bound = SCF_Distance(dataset1["cube"],
                                          dataset2["cube"],
                                          size=11,
                                          boundary='cut').distance_metric()
    scf_val_noncon_bound = scf_distance_cut_bound.scf1.scf_surface

    scf_fitlims = SCF(dataset1['cube'], size=11)
    scf_fitlims.run(boundary='continuous', xlow=1.5 * u.pix, xhigh=4.5 * u.pix)

    scf_slope_wlimits = scf_fitlims.slope
    scf_slope_wlimits_2D = scf_fitlims.slope2D

    # Cramer Statistic

    from turbustat.statistics import Cramer_Distance

    cramer_distance = Cramer_Distance(
        dataset1["cube"], dataset2["cube"], noise_value1=0.1,
        noise_value2=0.1).distance_metric(normalize=False)

    cramer_val = cramer_distance.data_matrix1

    # Dendrograms

    from turbustat.statistics import Dendrogram_Distance, Dendrogram_Stats

    min_deltas = np.logspace(-1.5, 0.5, 40)

    dendro_distance = Dendrogram_Distance(
        dataset1["cube"], dataset2["cube"],
        min_deltas=min_deltas).distance_metric()

    dendrogram_val = dendro_distance.dendro1.numfeatures

    # With periodic boundaries
    dendro = Dendrogram_Stats(dataset1['cube'], min_deltas=min_deltas)
    dendro.run(periodic_bounds=True)

    dendrogram_periodic_val = dendro.numfeatures

    # PDF

    from turbustat.statistics import PDF_Distance

    pdf_distance = \
        PDF_Distance(dataset1["moment0"],
                     dataset2["moment0"],
                     min_val1=0.05,
                     min_val2=0.05,
                     weights1=dataset1["moment0_error"][0]**-2.,
                     weights2=dataset2["moment0_error"][0]**-2.,
                     do_fit=False,
                     normalization_type='standardize')

    pdf_distance.distance_metric()

    pdf_val = pdf_distance.PDF1.pdf
    pdf_ecdf = pdf_distance.PDF1.ecdf
    pdf_bins = pdf_distance.bins

    # Do a fitted version of the PDF pca
    pdf_fit_distance = \
        PDF_Distance(dataset1["moment0"],
                     dataset2["moment0"],
                     min_val1=0.05,
                     min_val2=0.05,
                     do_fit=True,
                     normalization_type=None)

    pdf_fit_distance.distance_metric()

    np.savez_compressed('checkVals',
                        wavelet_val=wavelet_val,
                        wavelet_slope=wavelet_slope,
                        wavelet_slope_wbrk=wavelet_slope_wbrk,
                        wavelet_brk_wbrk=wavelet_brk_wbrk,
                        mvc_val=mvc_val,
                        mvc_slope=mvc_slope,
                        mvc_slope2D=mvc_slope2D,
                        pspec_val=pspec_val,
                        pspec_slope=pspec_slope,
                        pspec_slope2D=pspec_slope2D,
                        bispec_val=bispec_val,
                        bispec_azim_bins=bispec_azim_bins,
                        bispec_azim_vals=bispec_azim_vals,
                        bispec_azim_stds=bispec_azim_stds,
                        bispec_val_meansub=bispec_val_meansub,
                        genus_val=genus_val,
                        delvar_val=delvar_val,
                        delvar_slope=delvar_slope,
                        delvar_slope_wbrk=delvar_slope_wbrk,
                        delvar_brk=delvar_brk,
                        delvar_fill_val=delvar_fill_val,
                        delvar_fill_slope=delvar_fill_slope,
                        vcs_val=vcs_val,
                        vcs_slopes=vcs_slopes,
                        vca_val=vca_val,
                        vca_slope=vca_slope,
                        vca_slope2D=vca_slope2D,
                        tsallis_val=tsallis_val,
                        tsallis_stderrs=tsallis_stderrs,
                        tsallis_val_noper=tsallis_val_noper,
                        kurtosis_val=kurtosis_val,
                        skewness_val=skewness_val,
                        kurtosis_nondist_val=kurtosis_nondist_val,
                        skewness_nondist_val=skewness_nondist_val,
                        kurtosis_nonper_val=kurtosis_nonper_val,
                        skewness_nonper_val=skewness_nonper_val,
                        pca_val=pca_val,
                        pca_fit_vals=pca_fit_vals,
                        pca_spectral_widths=pca_spectral_widths,
                        pca_spatial_widths=pca_spatial_widths,
                        scf_val=scf_val,
                        scf_slope_wlimits=scf_slope_wlimits,
                        scf_slope_wlimits_2D=scf_slope_wlimits_2D,
                        scf_val_noncon_bound=scf_val_noncon_bound,
                        scf_spectrum=scf_spectrum,
                        scf_slope=scf_slope,
                        scf_slope2D=scf_slope2D,
                        cramer_val=cramer_val,
                        dendrogram_val=dendrogram_val,
                        dendrogram_periodic_val=dendrogram_periodic_val,
                        pdf_val=pdf_val,
                        pdf_bins=pdf_bins,
                        pdf_ecdf=pdf_ecdf)

    np.savez_compressed(
        'computed_distances',
        mvc_distance=mvc_distance.distance,
        pca_distance=pca_distance.distance,
        vca_distance=vca_distance.distance,
        pspec_distance=pspec_distance.distance,
        scf_distance=scf_distance.distance,
        wavelet_distance=wavelet_distance.distance,
        delvar_curve_distance=delvar_distance.curve_distance,
        delvar_slope_distance=delvar_distance.slope_distance,
        # tsallis_distance=tsallis_distance.distance,
        kurtosis_distance=moment_distance.kurtosis_distance,
        skewness_distance=moment_distance.skewness_distance,
        cramer_distance=cramer_distance.distance,
        genus_distance=genus_distance.distance,
        vcs_distance=vcs_distance.distance,
        bispec_mean_distance=bispec_distance.mean_distance,
        bispec_surface_distance=bispec_distance.surface_distance,
        dendrohist_distance=dendro_distance.histogram_distance,
        dendronum_distance=dendro_distance.num_distance,
        pdf_hellinger_distance=pdf_distance.hellinger_distance,
        pdf_ks_distance=pdf_distance.ks_distance,
        pdf_lognorm_distance=pdf_fit_distance.lognormal_distance)
Esempio n. 17
0
    pdf_cube = PDF(cube)
    pdf_cube.run(verbose=True,
                 do_fit=False,
                 save_name=osjoin(fig_path, "pdf_design4.png"))

# PSpec
if run_pspec:

    from turbustat.statistics import PowerSpectrum

    moment0 = fits.open(
        osjoin(data_path, "Design4_flatrho_0021_00_radmc_moment0.fits"))[0]

    pspec = PowerSpectrum(moment0, distance=250 * u.pc)
    pspec.run(verbose=True,
              xunit=u.pix**-1,
              save_name=osjoin(fig_path, "design4_pspec.png"))

    pspec.run(verbose=True,
              xunit=u.pix**-1,
              low_cut=0.02 / u.pix,
              high_cut=0.1 / u.pix,
              save_name=osjoin(fig_path, "design4_pspec_limitedfreq.png"))

    print(pspec.slope2D, pspec.slope2D_err)
    print(pspec.ellip2D, pspec.ellip2D_err)
    print(pspec.theta2D, pspec.theta2D_err)

    # How about fitting a break?
    pspec = PowerSpectrum(moment0, distance=250 * u.pc)
    pspec.run(verbose=True,
Esempio n. 18
0
    pdf_cube = PDF(cube)
    pdf_cube.run(verbose=True,
                 do_fit=False,
                 save_name=osjoin(fig_path, "pdf_design4.png"))

# PSpec
if run_pspec:

    from turbustat.statistics import PowerSpectrum

    moment0 = fits.open(
        osjoin(data_path, "Design4_flatrho_0021_00_radmc_moment0.fits"))[0]

    pspec = PowerSpectrum(moment0, distance=250 * u.pc)
    pspec.run(verbose=True,
              xunit=u.pix**-1,
              save_name=osjoin(fig_path, "design4_pspec.png"))

    pspec.run(verbose=True,
              xunit=u.pix**-1,
              low_cut=0.025 / u.pix,
              high_cut=0.1 / u.pix,
              save_name=osjoin(fig_path, "design4_pspec_limitedfreq.png"))

    print(pspec.slope2D, pspec.slope2D_err)
    print(pspec.ellip2D, pspec.ellip2D_err)
    print(pspec.theta2D, pspec.theta2D_err)

    # How about fitting a break?
    pspec = PowerSpectrum(moment0, distance=250 * u.pc)
    pspec.run(verbose=True,
Esempio n. 19
0
        fits.PrimaryHDU(hdu_coldens[0].data[0].squeeze(),
                        hdu_coldens[0].header))

    # Get minimal size
    proj_coldens = proj_coldens[nd.find_objects(np.isfinite(proj_coldens))[0]]

    proj_coldens = proj_coldens.with_beam(fitinfo_dict[gal]['beam'])

    # Run on the original image

    pspec = PowerSpectrum(proj_coldens, distance=fitinfo_dict[gal]['distance'])
    pspec.run(
        verbose=False,
        beam_correct=False,
        fit_2D=False,
        high_cut=0.1 / u.pix,
        use_pyfftw=True,
        threads=ncores,
        # radial_pspec_kwargs={"theta_0": gal_obj.position_angle + 90*u.deg, "delta_theta": 50 * u.deg},
        # radial_pspec_kwargs={"logspacing": True, 'binsize': 10.},
        apodize_kernel=fitinfo_dict[gal]['apod_kern'])

    # Deproject the image AND the beam

    deproj_img = deproject(proj_coldens,
                           proj_coldens.header,
                           gal_obj,
                           inc_correction=True)

    # Cut this image down to the minimal size
    deproj_img = deproj_img[nd.find_objects(np.isfinite(deproj_img))[0]]
Esempio n. 20
0
                    gal.lower(), name)

                # For now skip already saved power-spectra
                if os.path.exists(osjoin(data_path, gal,
                                         save_name)) and skip_check:
                    print("Already saved pspec for {}. Skipping".format(
                        filename))
                    continue
                else:
                    os.system("rm -f {}".format(
                        osjoin(data_path, gal, save_name)))

                pspec = PowerSpectrum(proj, distance=dist)
                pspec.run(verbose=False,
                          beam_correct=False,
                          fit_2D=False,
                          high_cut=0.1 / u.pix,
                          use_pyfftw=True,
                          threads=ncores)

                pspec.save_results(osjoin(data_path, gal, save_name),
                                   keep_data=False)

                del pspec, proj, hdu

if do_fitpspec:

    fit_results = {
        'logA': [],
        'ind': [],
        'logB': [],
        'logA_std': [],
Esempio n. 21
0
            save_name = f"{out_filename.rstrip('.fits')}.pspec.pkl"

            # For now skip already saved power-spectra
            if os.path.exists(osjoin(data_path, gal,
                                     save_name)) and skip_check:
                print("Already saved pspec for {}. Skipping".format(filename))
                continue
            else:
                os.system("rm -f {}".format(osjoin(data_path, gal, save_name)))

            pspec = PowerSpectrum(proj, distance=dist)
            pspec.run(verbose=False,
                      beam_correct=False,
                      fit_2D=False,
                      high_cut=0.1 / u.pix,
                      use_pyfftw=True,
                      threads=ncores,
                      apodize_kernel=fitinfo_dict[gal][name]['apod_kern'])

            pspec.save_results(osjoin(data_path, gal, save_name),
                               keep_data=False)

            del pspec, proj, hdu

if run_fits:

    row_names = []
    fit_results = {
        'logA': [],
        'ind': [],
Esempio n. 22
0
mvc_val = mvc.ps1D
mvc_slope = mvc.slope
mvc_slope2D = mvc.slope2D

# Spatial Power Spectrum/ Bispectrum

from turbustat.statistics import (PSpec_Distance, Bispectrum_Distance,
                                  Bispectrum, PowerSpectrum)

pspec_distance = \
    PSpec_Distance(dataset1["moment0"],
                   dataset2["moment0"]).distance_metric()

pspec = PowerSpectrum(dataset1['moment0'])
pspec.run()

pspec_val = pspec.ps1D
pspec_slope = pspec.slope
pspec_slope2D = pspec.slope2D

bispec_distance = \
    Bispectrum_Distance(dataset1["moment0"],
                        dataset2["moment0"]).distance_metric()

bispec_val = bispec_distance.bispec1.bicoherence

azimuthal_slice = bispec_distance.bispec1.azimuthal_slice(16, 10,
                                                          value='bispectrum_logamp',
                                                          bin_width=5 * u.deg)
bispec_azim_bins = azimuthal_slice[16][0]