Beispiel #1
0
def test_scott_bin_width(N=10000, rseed=0):
    rng = np.random.RandomState(rseed)
    X = rng.randn(N)

    delta = scott_bin_width(X)
    assert_allclose(delta, 3.5 * np.std(X) / N ** (1 / 3))

    delta, bins = scott_bin_width(X, return_bins=True)
    assert_allclose(delta, 3.5 * np.std(X) / N ** (1 / 3))

    with pytest.raises(ValueError):
        scott_bin_width(rng.rand(2, 10))
Beispiel #2
0
def test_scott_bin_width(N=10000, rseed=0):
    rng = np.random.default_rng(rseed)
    X = rng.standard_normal(N)

    delta = scott_bin_width(X)
    assert_allclose(delta, 3.5 * np.std(X) / N**(1 / 3))

    delta, bins = scott_bin_width(X, return_bins=True)
    assert_allclose(delta, 3.5 * np.std(X) / N**(1 / 3))

    with pytest.raises(ValueError):
        scott_bin_width(rng.random((2, 10)))
Beispiel #3
0
def scotts_bin_width(data, return_bins=False):
    r"""Return the optimal histogram bin width using Scott's rule:

    Parameters
    ----------
    data : array-like, ndim=1
        observed (one-dimensional) data
    return_bins : bool (optional)
        if True, then return the bin edges

    Returns
    -------
    width : float
        optimal bin width using Scott's rule
    bins : ndarray
        bin edges: returned if `return_bins` is True

    Notes
    -----
    The optimal bin width is

    .. math::
        \Delta_b = \frac{3.5\sigma}{n^{1/3}}

    where :math:`\sigma` is the standard deviation of the data, and
    :math:`n` is the number of data points.

    See Also
    --------
    knuth_bin_width
    freedman_bin_width
    astroML.plotting.hist
    """
    return astropy_stats.scott_bin_width(data, return_bins)
Beispiel #4
0
def knuth_bw_selector(dat_list):
    """Selects the kde bandwidth using Knuth's rule implemented in Astropy
    If Knuth's rule raises error, Scott's rule is used
    
    Parameters
    ----------
    dat_list : list
        List of data arrays that will be used to generate a kde

    Returns
    -------
    bw_min : float
        Minimum of bandwidths for all of the data arrays in dat_list
    """

    bw_list = []
    for dat in dat_list:
        try:
            bw = astrostats.knuth_bin_width(dat)
        except:
            print('Using Scott Rule!!')
            bw = astrostats.scott_bin_width(dat)
        bw_list.append(bw)
    return np.mean(bw_list)
        
Beispiel #5
0
def histogram(a, bins=10, range=None, **kwargs):
    """Enhanced histogram

    This is a histogram function that enables the use of more sophisticated
    algorithms for determining bins.  Aside from the `bins` argument allowing
    a string specified how bins are computed, the parameters are the same
    as numpy.histogram().

    Parameters
    ----------
    a : array_like
        array of data to be histogrammed

    bins : int or list or str (optional)
        If bins is a string, then it must be one of:
        'blocks' : use bayesian blocks for dynamic bin widths
        'knuth' : use Knuth's rule to determine bins
        'scotts' : use Scott's rule to determine bins
        'freedman' : use the Freedman-diaconis rule to determine bins

    range : tuple or None (optional)
        the minimum and maximum range for the histogram.  If not specified,
        it will be (x.min(), x.max())

    other keyword arguments are described in numpy.hist().

    Returns
    -------
    hist : array
        The values of the histogram. See `normed` and `weights` for a
        description of the possible semantics.
    bin_edges : array of dtype float
        Return the bin edges ``(length(hist)+1)``.

    See Also
    --------
    numpy.histogram
    astroML.plotting.hist
    """
    a = np.asarray(a)

    # if range is specified, we need to truncate the data for
    # the bin-finding routines
    if (range is not None and (bins in ['blocks', 'knuth',
                                        'scotts', 'freedman'])):
        a = a[(a >= range[0]) & (a <= range[1])]

    if isinstance(bins, str):
        if bins == 'blocks':
            bins = astropy_stats.bayesian_blocks(a)
        elif bins == 'knuth':
            da, bins = astropy_stats.knuth_bin_width(a, True)
        elif bins == 'scotts':
            da, bins = astropy_stats.scott_bin_width(a, True)
        elif bins == 'freedman':
            da, bins = astropy_stats.freedman_bin_width(a, True)
        else:
            raise ValueError("unrecognized bin code: '{}'".format(bins))

    return np.histogram(a, bins, range, **kwargs)
Beispiel #6
0
def scotts_bin_width(data, return_bins=False):
    r"""Return the optimal histogram bin width using Scott's rule:

    Parameters
    ----------
    data : array-like, ndim=1
        observed (one-dimensional) data
    return_bins : bool (optional)
        if True, then return the bin edges

    Returns
    -------
    width : float
        optimal bin width using Scott's rule
    bins : ndarray
        bin edges: returned if `return_bins` is True

    Notes
    -----
    The optimal bin width is

    .. math::
        \Delta_b = \frac{3.5\sigma}{n^{1/3}}

    where :math:`\sigma` is the standard deviation of the data, and
    :math:`n` is the number of data points.

    See Also
    --------
    knuth_bin_width
    freedman_bin_width
    astroML.plotting.hist
    """
    return astropy_stats.scott_bin_width(data, return_bins)
Beispiel #7
0
def get_bin_sizes_xy(x, y, algo='scott'):
    """ Smartly get bin size to have a loer bias due to binning"""
    from astropy.stats import freedman_bin_width, scott_bin_width, knuth_bin_width, bayesian_blocks
    logger.info(" > Get smart bin sizes in 2D")

    if algo == 'scott':
        logger.info("use scott rule of thumb")
        width_x, bins_x = scott_bin_width(x, return_bins=True)
        width_y, bins_y = scott_bin_width(y, return_bins=True)
    elif algo == 'knuth':
        logger.info("use knuth rule of thumb")
        width_x, bins_x = knuth_bin_width(x, return_bins=True)
        width_y, bins_y = knuth_bin_width(y, return_bins=True)
    elif algo == 'freedman':
        logger.info("use freedman rule of thumb")
        width_x, bins_x = freedman_bin_width(x, return_bins=True)
        width_y, bins_y = freedman_bin_width(y, return_bins=True)
    else:
        raise NotImplementedError("use scott or knuth")
    n_bins_x, n_bins_y = len(bins_x), len(bins_y)

    return bins_x, bins_y, width_x, width_y
Beispiel #8
0
def calc_bin(data, mode):
    n = len(data)
    if mode == "sqrt":
        bins = np.sqrt(n)
    elif mode == "sturges":
        bins = np.log2(n) + 1
    elif mode == "scott":
        width, bins = scott_bin_width(data, return_bins=True)
        bins = len(bins)
    elif mode == "freedman":
        width, bins = freedman_bin_width(data, return_bins=True)
        bins = len(bins)
    elif mode == "knuth":
        width, bins = knuth_bin_width(data, return_bins=True)
        bins = len(bins)
    return bins
Beispiel #9
0
def plot_all_col_plotly(df,
                        hue=None,
                        numeric_only=False,
                        string_only=False,
                        bin_width=None):
    '''plot all columns conditioned on one, using plotly.express

    `bin_width`: for numeric data only.
    If None, pass nbins=None to plotly,
    if 'freedman', use Freedman–Diaconis rule,
    else use Scott's normal reference rule.
    '''
    for name, col in df.items():
        if name == hue:
            pass
        elif is_numeric_dtype(col):
            if not string_only:
                if bin_width is None:
                    nbins = None
                else:
                    from astropy.stats import freedman_bin_width, scott_bin_width

                    values = col.values
                    width = freedman_bin_width(
                        values
                    ) if bin_width == 'freedman' else scott_bin_width(values)
                    nbins = int(np.round((values.max() - values.min()) /
                                         width)) if width else None
                fig = px.histogram(df,
                                   y=name,
                                   color=hue,
                                   orientation='h',
                                   barmode='overlay',
                                   histnorm='probability density',
                                   nbins=nbins)
                fig.show()
        elif is_string_dtype(col):
            if not numeric_only:
                fig = px.histogram(df,
                                   y=name,
                                   color=hue,
                                   orientation='h',
                                   barmode='group',
                                   histnorm='probability density')
                fig.show()
        else:
            raise ValueError
Beispiel #10
0
def get_nofz(z, fsky, cosmo=None):
    ''' calculate nbar(z) given redshift values and f_sky (sky coverage
    fraction)
    '''
    # calculate nbar(z) for each galaxy
    _, edges = scott_bin_width(z, return_bins=True)

    dig = np.searchsorted(edges, z, "right")
    N = np.bincount(dig, minlength=len(edges) + 1)[1:-1]

    R_hi = cosmo.comoving_distance(edges[1:])  # Mpc/h
    R_lo = cosmo.comoving_distance(edges[:-1])  # Mpc/h

    dV = (4. / 3.) * np.pi * (R_hi**3 - R_lo**3) * fsky

    nofz = InterpolatedUnivariateSpline(0.5 * (edges[1:] + edges[:-1]),
                                        N / dV,
                                        ext='const')

    return nofz(z)
Beispiel #11
0
def get_bin_sizes_x(x, algo='scott'):
    """ Smartly get bin size to have a loer bias due to binning"""
    from astropy.stats import freedman_bin_width, scott_bin_width, knuth_bin_width, bayesian_blocks
    logger.info(" > Get smart bin sizes in 1D")

    if algo == 'scott':
        logger.info("use scott rule of thumb")
        width_x, bins_x = scott_bin_width(x, return_bins=True)
    elif algo == 'knuth':
        logger.info("use knuth rule of thumb")
        width_x, bins_x = knuth_bin_width(x, return_bins=True)
    elif algo == 'freedman':
        logger.info("use freedman rule of thumb")
        width_x, bins_x = freedman_bin_width(x, return_bins=True)
    elif algo == 'blocks':
        logger.info("use bayesian blocks rule of thumb")
        width_x, bins_x = bayesian_blocks(x, return_bins=True)
    else:
        raise NotImplementedError("use scott, knuth, freedman or blocks")

    return bins_x, width_x
Beispiel #12
0
def hist(x, bins=10, range=None, *args, **kwargs):
    """Enhanced histogram

    This is a histogram function that enables the use of more sophisticated
    algorithms for determining bins.  Aside from the `bins` argument allowing
    a string specified how bins are computed, the parameters are the same
    as pylab.hist().

    Parameters
    ----------
    x : array_like
        array of data to be histogrammed

    bins : int or list or str (optional)
        If bins is a string, then it must be one of:
        'blocks' : use bayesian blocks for dynamic bin widths
        'knuth' : use Knuth's rule to determine bins
        'scott' : use Scott's rule to determine bins
        'freedman' : use the Freedman-diaconis rule to determine bins

    range : tuple or None (optional)
        the minimum and maximum range for the histogram.  If not specified,
        it will be (x.min(), x.max())

    ax : Axes instance (optional)
        specify the Axes on which to draw the histogram.  If not specified,
        then the current active axes will be used.

    **kwargs :
        other keyword arguments are described in pylab.hist().
    """
    if isinstance(bins, str) and "weights" in kwargs:
        warnings.warn("weights argument is not supported: it will be ignored.")
        kwargs.pop('weights')

    x = np.asarray(x)

    if 'ax' in kwargs:
        ax = kwargs['ax']
        del kwargs['ax']
    else:
        # import here so that testing with Agg will work
        from matplotlib import pyplot as plt
        ax = plt.gca()

    # if range is specified, we need to truncate the data for
    # the bin-finding routines
    if (range is not None and (bins in ['blocks',
                                        'knuth', 'knuths',
                                        'scott', 'scotts',
                                        'freedman', 'freedmans'])):
        x = x[(x >= range[0]) & (x <= range[1])]

    if bins in ['blocks']:
        bins = bayesian_blocks(x)
    elif bins in ['knuth', 'knuths']:
        dx, bins = knuth_bin_width(x, True)
    elif bins in ['scott', 'scotts']:
        dx, bins = scott_bin_width(x, True)
    elif bins in ['freedman', 'freedmans']:
        dx, bins = freedman_bin_width(x, True)
    elif isinstance(bins, str):
        raise ValueError("unrecognized bin code: '{}'".format(bins))

    return ax.hist(x, bins, range, **kwargs)
Beispiel #13
0
def match(dataCm):
    """Performs the Match calculation in Eq. 1 of Breivik & Larson (2018)

    Parameters
    ----------
    dataCm : list
        List of two cumulative data sets for a single paramter

    Returns
    -------
    match : list
        List of matches for each cumulative data set

    binwidth : float
        Binwidth of histograms used for match computation
    """

    # DEFINE A LIST TO HOLD THE BINNED DATA:
    histo = [[], []]
    histoBinEdges = [[], []]
    # COMPUTE THE BINWIDTH FOR THE MOST COMPLETE DATA SET:
    # NOTE: THIS WILL BE THE BINWIDTH FOR ALL THE HISTOGRAMS IN THE HISTO LIST
    with warnings.catch_warnings():
        warnings.filterwarnings(
            "ignore", message="divide by zero encountered in double_scalars")
        try:
            bw, binEdges = astroStats.knuth_bin_width(np.array(dataCm[0]),
                                                      return_bins=True)
        except Exception:
            bw, binEdges = astroStats.scott_bin_width(np.array(dataCm[0]),
                                                      return_bins=True)
    if bw < 1e-4:
        bw = 1e-4
        binEdges = np.arange(binEdges[0], binEdges[-1], bw)

    # BIN THE DATA:
    for i in range(2):
        histo[i], histoBinEdges[i] = astroStats.histogram(dataCm[i],
                                                          bins=binEdges,
                                                          density=True)
    # COMPUTE THE MATCH:
    nominator = []
    denominator1 = []
    denominator2 = []
    nominatorSum = []
    denominator1Sum = []
    denominator2Sum = []

    histo2 = histo[1]
    histo1 = histo[0]

    for j in range(len(histo1)):
        nominator.append(histo1[j] * histo2[j])
        denominator1.append((histo1[j] * histo1[j]))
        denominator2.append((histo2[j] * histo2[j]))
    nominatorSum.append(np.sum(nominator))
    denominator1Sum.append(np.sum(denominator1))
    denominator2Sum.append(np.sum(denominator2))

    nominatorSum = np.array(nominatorSum)
    denominator1Sum = np.array(denominator1Sum)
    denominator2Sum = np.array(denominator2Sum)

    binwidth = binEdges[1] - binEdges[0]
    if binwidth < 1e-7:
        match = 1e-9
    else:
        match = np.log10(1 - nominatorSum /
                         np.sqrt(denominator1Sum * denominator2Sum))

    return match[0], binwidth
Beispiel #14
0
def hist(x, bins=10, range=None, *args, **kwargs):
    """Enhanced histogram

    This is a histogram function that enables the use of more sophisticated
    algorithms for determining bins.  Aside from the `bins` argument allowing
    a string specified how bins are computed, the parameters are the same
    as pylab.hist().

    Parameters
    ----------
    x : array_like
        array of data to be histogrammed

    bins : int or list or str (optional)
        If bins is a string, then it must be one of:
        'blocks' : use bayesian blocks for dynamic bin widths
        'knuth' : use Knuth's rule to determine bins
        'scott' : use Scott's rule to determine bins
        'freedman' : use the Freedman-diaconis rule to determine bins

    range : tuple or None (optional)
        the minimum and maximum range for the histogram.  If not specified,
        it will be (x.min(), x.max())

    ax : Axes instance (optional)
        specify the Axes on which to draw the histogram.  If not specified,
        then the current active axes will be used.

    **kwargs :
        other keyword arguments are described in pylab.hist().
    """
    if isinstance(bins, str) and "weights" in kwargs:
        warnings.warn("weights argument is not supported: it will be ignored.")
        kwargs.pop('weights')

    x = np.asarray(x)

    if 'ax' in kwargs:
        ax = kwargs['ax']
        del kwargs['ax']
    else:
        # import here so that testing with Agg will work
        from matplotlib import pyplot as plt
        ax = plt.gca()

    # if range is specified, we need to truncate the data for
    # the bin-finding routines
    if (range is not None and (bins in [
            'blocks', 'knuth', 'knuths', 'scott', 'scotts', 'freedman',
            'freedmans'
    ])):
        x = x[(x >= range[0]) & (x <= range[1])]

    if bins in ['blocks']:
        bins = bayesian_blocks(x)
    elif bins in ['knuth', 'knuths']:
        dx, bins = knuth_bin_width(x, True, disp=False)
    elif bins in ['scott', 'scotts']:
        dx, bins = scott_bin_width(x, True)
    elif bins in ['freedman', 'freedmans']:
        dx, bins = freedman_bin_width(x, True)
    elif isinstance(bins, str):
        raise ValueError("unrecognized bin code: '%s'" % bins)

    return ax.hist(x, bins, range, **kwargs)
Beispiel #15
0
def LSSTsim(Ncores, Nbin):
    def paramTransform(dat):
        datMin = min(dat) - 0.0001
        datMax = max(dat) + 0.0001
        datZeroed = dat - datMin

        datTransformed = datZeroed / (datMax - datMin)
        return datTransformed

    # SET TIME TO TRACK COMPUTATION TIME
    ##############################################################################
    start_time = time.time()
    #np.random.seed()

    # CONSTANTS
    ##############################################################################
    G = 6.67384 * 10.**-11.0
    c = 2.99792458 * 10.**8.0
    parsec = 3.08567758 * 10.**16.
    Rsun = 6.955 * 10.**8.
    Msun = 1.9891 * 10.**30.
    day = 86400.0
    rsun_in_au = 215.0954
    day_in_year = 365.242
    sec_in_day = 86400.0
    sec_in_hour = 3600.0
    hrs_in_day = 24.0
    sec_in_year = 3.15569 * 10**7.0
    Tobs = 3.15569 * 10**7.0
    geo_mass = G / c**2
    m_in_AU = 1.496 * 10**11.0

    mTotDisk = 2.15 * 10**10
    binID = '0012'

    ##############################################################################
    # STELLAR TYPES - KW
    #
    #   0 - deeply or fully convective low mass MS star
    #   1 - Main Sequence star
    #   2 - Hertzsprung Gap
    #   3 - First Giant Branch
    #   4 - Core Helium Burning
    #   5 - First Asymptotic Giant Branch
    #   6 - Second Asymptotic Giant Branch
    #   7 - Main Sequence Naked Helium star
    #   8 - Hertzsprung Gap Naked Helium star
    #   9 - Giant Branch Naked Helium star
    #  10 - Helium White Dwarf
    #  11 - Carbon/Oxygen White Dwarf
    #  12 - Oxygen/Neon White Dwarf
    #  13 - Neutron Star
    #  14 - Black Hole
    #  15 - Massless Supernova
    ##############################################################################

    # LOAD THE FIXED POPULATION DATA
    ##############################################################################
    ############## UNITS ################
    #                                   #
    # mass: Msun, porb: year, sep: rsun #
    #                                   #
    #####################################

    dts = {
        'names':
        ('binNum', 'tBornBin', 'Time', 'tBorn1', 'tBorn2', 'commonEnv', 'id1',
         'id2', 'm1', 'm2', 'm1Init', 'm2Init', 'Lum1', 'Lum2', 'rad1', 'rad2',
         'T1', 'T2', 'massc1', 'massc2', 'radc1', 'radc2', 'menv1', 'menv2',
         'renv1', 'renv2', 'spin1', 'spin2', 'rrol1', 'rrol2', 'porb', 'sep',
         'ecc'),
        'formats': ('i', 'f', 'f', 'f', 'f', 'i', 'i', 'i', 'f', 'f', 'f', 'f',
                    'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f',
                    'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f')
    }

    #FixedPop = np.loadtxt('fixedData_'+binID+'.dat', delimiter = ',', dtype=dts)
    FixedPop = pd.read_hdf('../input/dat_ThinDisk_12_0_12_0.h5', key='bcm')
    FixedPopLog = np.loadtxt('../input/fixedPopLogCm_' + binID + '.dat',
                             delimiter=',')

    # COMPUTE THE NUMBER AT PRESENT DAY NORMALIZED BY TOTAL MASS OF THE GX COMPONENT
    ##############################################################################
    mTotFixed = sum(FixedPopLog[:, 2])
    nPop = int(len(FixedPop) * mTotDisk / mTotFixed)
    print('The number of binaries in the Gx for: ' + str(binID) + ' is: ' +
          str(nPop))

    # TRANSFORM THE FIXED POP DATA TO HAVE LIMITS [0,1] &
    # COMPUTE THE BINWIDTH TO USE FOR THE KDE SAMPLE; SEE KNUTH_BIN_WIDTH IN ASTROPY
    ##############################################################################

    # UNITS:
    # MASS [MSUN], ORBITAL PERIOD [LOG10(YEARS)], LUMINOSITIES [LSUN], RADII [RSUN]
    #FixedPop['m1'] = FixedPop['mass_1'] #or maybe some more efficient way
    ###
    #print (FixedPop['m1'])

    FixedPop['m1'] = FixedPop['mass_1']
    #print (FixedPop['m1'])
    FixedPop['m2'] = FixedPop['mass_2']
    FixedPop['Lum1'] = FixedPop['lumin_1']
    FixedPop['Lum2'] = FixedPop['lumin_2']
    FixedPop['rad1'] = FixedPop['rad_1']
    FixedPop['rad2'] = FixedPop['rad_2']

    m1Trans = ss.logit(paramTransform(FixedPop['m1']))
    bwM1 = astroStats.scott_bin_width(m1Trans)

    m2Trans = ss.logit(paramTransform(FixedPop['m2']))
    bwM2 = astroStats.scott_bin_width(m2Trans)

    porbTrans = ss.logit(paramTransform(np.log10(FixedPop['porb'])))
    bwPorb = astroStats.scott_bin_width(porbTrans)

    Lum1Trans = ss.logit(paramTransform(FixedPop['Lum1']))
    bwLum1 = astroStats.scott_bin_width(FixedPop['Lum1'])

    Lum2Trans = ss.logit(paramTransform(FixedPop['Lum2']))
    bwLum2 = astroStats.scott_bin_width(FixedPop['Lum2'])

    # The eccentricity is already transformed, but only fit KDE to ecc if ecc!=0.0
    eIndex, = np.where(FixedPop['ecc'] > 1e-2)
    if len(eIndex) > 50:

        eccTrans = FixedPop['ecc']
        for jj in eccTrans.keys():
            if eccTrans[jj] > 0.999:
                eccTrans[jj] = 0.999
            elif eccTrans[jj] < 1e-4:
                eccTrans[jj] = 1e-4
        eccTrans = ss.logit(eccTrans)
        bwEcc = astroStats.scott_bin_width(eccTrans)
    else:
        bwEcc = 100.0

    rad1Trans = ss.logit(paramTransform(FixedPop['rad1']))
    bwRad1 = astroStats.scott_bin_width(rad1Trans)

    rad2Trans = ss.logit(paramTransform(FixedPop['rad2']))
    bwRad2 = astroStats.scott_bin_width(rad2Trans)
    #print(bwEcc,bwPorb,bwM1,bwM2,bwLum1,bwLum2,bwRad1,bwRad2)
    popBw = min(bwEcc, bwPorb, bwM1, bwM2, bwLum1, bwLum2, bwRad1, bwRad2)

    # GENERATE THE DATA LIST DEPENDING ON THE TYPE OF COMPACT BINARY TYPE
    ##############################################################################
    type1Save = 1
    if type1Save < 14 and len(eIndex) > 50:
        print('both bright stars and eccentric')
        datList = np.array((m1Trans, m2Trans, porbTrans, eccTrans, rad1Trans,
                            rad2Trans, Lum1Trans, Lum2Trans))
    elif type1Save < 14 and len(eIndex) < 50:
        print('both bright stars and circular')
        datList = np.array((m1Trans, m2Trans, porbTrans, rad1Trans, rad2Trans,
                            Lum1Trans, Lum2Trans))

    # GENERATE THE KDE FOR THE DATA LIST
    ##############################################################################\
    print(popBw)
    print(datList)
    # for i,x in enumerate(datList):
    # 	print(len(x))
    # 	f = plt.figure()
    # 	plt.plot(x)
    # 	f.savefig(str(i)+".png")

    sampleKernel = scipy.stats.gaussian_kde(datList)  #, bw_method=popBw)

    # CALL THE MONTE CARLO GALAXY SAMPLE CODE
    ##############################################################################
    print('nSample: ' + str(Nbin))
    output = multiprocessing.Queue()
    processes = [multiprocessing.Process(target = GxSample.GxSample, \
        args   = (x, Ncores, FixedPop, sampleKernel,\
        binID, Nbin, popBw, len(eIndex), Tobs, output)) \
        for x in range(Ncores)]
    for p in processes:
        p.start()

    for p in processes:
        p.join()

    nSRC = [output.get() for p in processes]

    print('The number of sources in pop ' + binID + ' is ', nSRC)

    gxDatTot = []
    for kk in range(Ncores):
        print(kk)
        if os.path.getsize('gxRealization_' + str(kk) + '_' + str(binID) +
                           '.dat') > 0:
            gxReal = np.loadtxt('gxRealization_' + str(kk) + '_' + str(binID) +
                                '.dat',
                                delimiter=',')
        else:
            gxReal = []
        if len(gxReal) > 0:
            gxDatTot.append(gxReal)
        os.remove('gxRealization_' + str(kk) + '_' + str(binID) + '.dat')
    gxDatSave = np.vstack(gxDatTot)
    print(np.shape(gxDatSave))

    return gxDatSave
Beispiel #16
0
    def setKernel(self):
        print("getting Breivik kernel")

        def paramTransform(dat):
            datMin = min(dat) - 0.0001
            datMax = max(dat) + 0.0001
            datZeroed = dat - datMin

            datTransformed = datZeroed / (datMax - datMin)
            return datTransformed

        # SET TIME TO TRACK COMPUTATION TIME
        ##############################################################################
        start_time = time.time()
        #np.random.seed()

        # CONSTANTS
        ##############################################################################
        G = 6.67384 * 10.**-11.0
        c = 2.99792458 * 10.**8.0
        parsec = 3.08567758 * 10.**16.
        Rsun = 6.955 * 10.**8.
        Msun = 1.9891 * 10.**30.
        day = 86400.0
        rsun_in_au = 215.0954
        day_in_year = 365.242
        sec_in_day = 86400.0
        sec_in_hour = 3600.0
        hrs_in_day = 24.0
        sec_in_year = 3.15569 * 10**7.0
        Tobs = 3.15569 * 10**7.0
        geo_mass = G / c**2
        m_in_AU = 1.496 * 10**11.0

        mTotDisk = 2.15 * 10**10

        ##############################################################################
        # STELLAR TYPES - KW
        #
        #   0 - deeply or fully convective low mass MS star
        #   1 - Main Sequence star
        #   2 - Hertzsprung Gap
        #   3 - First Giant Branch
        #   4 - Core Helium Burning
        #   5 - First Asymptotic Giant Branch
        #   6 - Second Asymptotic Giant Branch
        #   7 - Main Sequence Naked Helium star
        #   8 - Hertzsprung Gap Naked Helium star
        #   9 - Giant Branch Naked Helium star
        #  10 - Helium White Dwarf
        #  11 - Carbon/Oxygen White Dwarf
        #  12 - Oxygen/Neon White Dwarf
        #  13 - Neutron Star
        #  14 - Black Hole
        #  15 - Massless Supernova
        ##############################################################################

        # LOAD THE FIXED POPULATION DATA
        ##############################################################################
        ############## UNITS ################
        #                                   #
        # mass: Msun, porb: year, sep: rsun #
        #                                   #
        #####################################

        dts = {
            'names':
            ('binNum', 'tBornBin', 'Time', 'tBorn1', 'tBorn2', 'commonEnv',
             'id1', 'id2', 'm1', 'm2', 'm1Init', 'm2Init', 'Lum1', 'Lum2',
             'rad1', 'rad2', 'T1', 'T2', 'massc1', 'massc2', 'radc1', 'radc2',
             'menv1', 'menv2', 'renv1', 'renv2', 'spin1', 'spin2', 'rrol1',
             'rrol2', 'porb', 'sep', 'ecc'),
            'formats': ('i', 'f', 'f', 'f', 'f', 'i', 'i', 'i', 'f', 'f', 'f',
                        'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f',
                        'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f')
        }

        self.fixedPop = pd.read_hdf(self.GalaxyFile, key='bcm')
        FixedPopLog = np.loadtxt(self.GalaxyFileLogPrefix + self.popID +
                                 '.dat',
                                 delimiter=',')

        # COMPUTE THE NUMBER AT PRESENT DAY NORMALIZED BY TOTAL MASS OF THE GX COMPONENT
        ##############################################################################
        mTotFixed = sum(FixedPopLog[:, 2])
        nPop = int(len(self.fixedPop) * mTotDisk / mTotFixed)
        print('The number of binaries in the Gx for: ' + str(self.popID) +
              ' is: ' + str(nPop))

        # TRANSFORM THE FIXED POP DATA TO HAVE LIMITS [0,1] &
        # COMPUTE THE BINWIDTH TO USE FOR THE KDE SAMPLE; SEE KNUTH_BIN_WIDTH IN ASTROPY
        ##############################################################################

        # UNITS:
        # MASS [MSUN], ORBITAL PERIOD [LOG10(YEARS)], LUMINOSITIES [LSUN], RADII [RSUN]
        #self.fixedPop['m1'] = self.fixedPop['mass_1'] #or maybe some more efficient way
        ###
        #print (self.fixedPop['m1'])

        #some changes to the names here because some were in the log but not names as such by Katie!
        self.fixedPop['m1'] = self.fixedPop['mass_1']
        #print (self.fixedPop['m1'])
        self.fixedPop['m2'] = self.fixedPop['mass_2']
        self.fixedPop['logL1'] = self.fixedPop['lumin_1']
        self.fixedPop['logL2'] = self.fixedPop['lumin_2']
        self.fixedPop['logr1'] = self.fixedPop['rad_1']
        self.fixedPop['logr2'] = self.fixedPop['rad_2']
        self.fixedPop['logp'] = np.log10(self.fixedPop['porb'])
        self.setMinMax()

        m1Trans = ss.logit(paramTransform(self.fixedPop['m1']))
        bwM1 = astroStats.scott_bin_width(m1Trans)

        m2Trans = ss.logit(paramTransform(self.fixedPop['m2']))
        bwM2 = astroStats.scott_bin_width(m2Trans)

        porbTrans = ss.logit(paramTransform(self.fixedPop['logp']))
        bwPorb = astroStats.scott_bin_width(porbTrans)

        Lum1Trans = ss.logit(paramTransform(self.fixedPop['logL1']))
        bwLum1 = astroStats.scott_bin_width(self.fixedPop['logL1'])

        Lum2Trans = ss.logit(paramTransform(self.fixedPop['logL2']))
        bwLum2 = astroStats.scott_bin_width(self.fixedPop['logL2'])

        # The eccentricity is already transformed, but only fit KDE to ecc if ecc!=0.0
        eIndex, = np.where(self.fixedPop['ecc'] > 1e-2)
        if len(eIndex) > 50:

            eccTrans = self.fixedPop['ecc']
            for jj in eccTrans.keys():
                if eccTrans[jj] > 0.999:
                    eccTrans[jj] = 0.999
                elif eccTrans[jj] < 1e-4:
                    eccTrans[jj] = 1e-4
            eccTrans = ss.logit(eccTrans)
            bwEcc = astroStats.scott_bin_width(eccTrans)
        else:
            bwEcc = 100.0

        rad1Trans = ss.logit(paramTransform(self.fixedPop['logr1']))
        bwRad1 = astroStats.scott_bin_width(rad1Trans)

        rad2Trans = ss.logit(paramTransform(self.fixedPop['logr2']))
        bwRad2 = astroStats.scott_bin_width(rad2Trans)
        #print(bwEcc,bwPorb,bwM1,bwM2,bwLum1,bwLum2,bwRad1,bwRad2)
        #popBw = min(bwEcc,bwPorb,bwM1,bwM2,bwLum1,bwLum2,bwRad1,bwRad2)

        # GENERATE THE DATA LIST DEPENDING ON THE TYPE OF COMPACT BINARY TYPE
        ##############################################################################
        type1Save = 1
        if type1Save < 14 and len(eIndex) > 50:
            print('both bright stars and eccentric')
            datList = np.array((m1Trans, m2Trans, porbTrans, eccTrans,
                                rad1Trans, rad2Trans, Lum1Trans, Lum2Trans))
        elif type1Save < 14 and len(eIndex) < 50:
            print('both bright stars and circular')
            datList = np.array((m1Trans, m2Trans, porbTrans, rad1Trans,
                                rad2Trans, Lum1Trans, Lum2Trans))

        # GENERATE THE KDE FOR THE DATA LIST
        ##############################################################################
        if (self.verbose):
            print(datList)

        self.sampleKernel = scipy.stats.gaussian_kde(
            datList)  #, bw_method=popBw)