Exemple #1
0
def load_training_images(type, **kwargs):
    if type is 'mnist':
        # MNIST dataset (use train and testing images to get more samples)
        (images, _), (test_images, _) = mnist.load_data()
        images = np.concatenate((images, test_images), axis=0)

    if type is 'cifar10':
        # CIFAR10 dataset
        class_to_train = int(kwargs['class_to_train'])
        (images, y_images), (test_images, y_test_images) = cifar10.load_data()
        images = images[np.array([item[0]
                                  for item in y_images]) == class_to_train]
        test_images = test_images[np.array([item[0]
                                            for item in y_test_images]) == 4]
        images = np.concatenate((images, test_images), axis=0)

    if type is 'local':
        # Local dataset
        images = load_data('./pictures')

    # Convert RGB values to float values
    images = (images.astype(np.float32) - 127.5) / 127.5
    # Resize images
    images = images.reshape((len(images), 28**2))
    return images
Exemple #2
0
def get_tf(rs, xHII, xHeII, dlnz, coarsen_factor=1):
    """
    Returns the interpolated transfer functions. 

    Parameters
    ----------
    rs : float
        The current redshift (1+z) to obtain the functions. 
    xHII : float
        The ionization fraction nHII/nH.
    xHeII : float
        The ionization fraction nHeII/nH.
    dlnz : float
        The dlnz of the output transfer functions.
    coarsen_factor : int
        The coarsening factor for the output transfer functions.

    Returns
    -------
    tuple
        Contains the high-energy photon, low-energy photon, low-energy
        electron, upscattered CMB photon energy and high-energy deposition
        transfer functions. 
    """

    # Load data.

    dep_tf_data = load_data('dep_tf')

    highengphot_tf_interp = dep_tf_data['highengphot']
    lowengphot_tf_interp = dep_tf_data['lowengphot']
    lowengelec_tf_interp = dep_tf_data['lowengelec']
    highengdep_interp = dep_tf_data['highengdep']

    if coarsen_factor > 1:
        # rs_to_interpolate = rs
        rs_to_interpolate = np.exp(np.log(rs) - dlnz * coarsen_factor / 2)
    else:
        rs_to_interpolate = rs

    highengphot_tf = highengphot_tf_interp.get_tf(xHII, xHeII,
                                                  rs_to_interpolate)
    lowengphot_tf = lowengphot_tf_interp.get_tf(xHII, xHeII, rs_to_interpolate)
    lowengelec_tf = lowengelec_tf_interp.get_tf(xHII, xHeII, rs_to_interpolate)
    highengdep_arr = highengdep_interp.get_val(xHII, xHeII, rs_to_interpolate)

    if coarsen_factor > 1:
        prop_tf = np.zeros_like(highengphot_tf._grid_vals)
        for i in np.arange(coarsen_factor):
            prop_tf += matrix_power(highengphot_tf._grid_vals, i)
        lowengphot_tf._grid_vals = np.matmul(prop_tf, lowengphot_tf._grid_vals)
        lowengelec_tf._grid_vals = np.matmul(prop_tf, lowengelec_tf._grid_vals)
        highengphot_tf._grid_vals = matrix_power(highengphot_tf._grid_vals,
                                                 coarsen_factor)
        # cmbloss_arr = np.matmul(prop_tf, cmbloss_arr)/coarsen_factor
        highengdep_arr = (np.matmul(prop_tf, highengdep_arr) / coarsen_factor)

    return (highengphot_tf, lowengphot_tf, lowengelec_tf, highengdep_arr)
Exemple #3
0
def struct_boost_func(model='einasto_subs', model_params=None):
    """Structure formation boost factor 1+B(z).

    Parameters
    ----------
    model : {'einasto_subs', 'einasto_no_subs', 'NFW_subs', 'NFW_no_subs', 'erfc'}
        Model to use. See 1604.02457. 
    model_params : tuple of floats
        Model parameters (b_h, delta, z_h) for 'erfc' option. 

    Returns
    -------
    float or ndarray
        Boost factor. 

    Notes
    -----
    Refer to 1408.1109 for erfc model, 1604.02457 for all other model
    descriptions and parameters.

    """

    if model == 'erfc':

        from scipy.special import erfc

        if model_params is None:
            # Smallest boost in 1408.1109.
            b_h = 1.6e5
            delta = 1.54
            z_h = 19.5
        else:
            b_h = model_params[0]
            delta = model_params[1]
            z_h = model_params[2]

        def func(rs):

            return 1. + b_h / rs**delta * erfc(rs / (1 + z_h))

    else:

        struct_data = load_data('struct')[model]
        log_struct_interp = interp1d(np.log(struct_data[:, 0]),
                                     np.log(struct_data[:, 1]),
                                     bounds_error=False,
                                     fill_value=(np.nan, 0.))

        def func(rs):

            return np.exp(log_struct_interp(np.log(rs)))

    return func
Exemple #4
0
def xHeII_std(rs):
    """Baseline nHeII/nH value.

    Parameters
    ----------
    rs : float
        The redshift (1+z). 

    Returns
    -------
    float
        nHeII/nH. 
    """

    global _xHeII_std

    if _xHeII_std is None:

        rs_vec = load_data('hist')['rs']
        xHeII_vec = load_data('hist')['xHeII']

        _xHeII_std = interp1d(rs_vec, xHeII_vec)

    return _xHeII_std(rs)
Exemple #5
0
def Tm_std(rs):
    """Baseline Tm value.

    Parameters
    ----------
    rs : float
        The redshift (1+z). 

    Returns
    -------
    float
        Tm in eV. 
    """

    global _Tm_std

    if _Tm_std is None:

        rs_vec = load_data('hist')['rs']
        Tm_vec = load_data('hist')['Tm']

        _Tm_std = interp1d(rs_vec, Tm_vec)

    return _Tm_std(rs)
Exemple #6
0
def dial_old(root):
    if root is None:
        dialw = Tk()
    else:
        dialw = Toplevel(root)

    shop_frames = []

    srch_entry = Entry(
        dialw,
        validate="key",
        validatecommand=(dialw.register(
            lambda x, y, z, t=shop_frames: srch_upd(x, y, z, t)), '%P', '%i',
                         '%d'))
    srch_entry.pack()

    # add list of ...
    #   my shops and their orders
    #   list of scheduled calls

    shops = load_data("shops")
    shops_frame = Frame(dialw)
    shops_frame.pack()

    row = 1
    for s in shops:
        #print(s[0], s[4])
        if s[4] != "Да":
            continue
        fr = Frame(shops_frame)
        shop_label = Label(fr, text=s[0], width=36)
        shop_label.pack(side=LEFT, fill=X, expand=True)
        ph = normalize_phone(s[1]) or 'Общий'
        shop_phone = Button(
            fr,
            text=ph,
            width=15,
            command=lambda s=s[0], ph=ph, eid=s[5]: order_window(s, ph, eid))
        shop_phone.pack(side=LEFT)
        fr.grid(column=0, row=row, sticky=W)
        shop_frames.append((fr, s[0], row, ph))
        row += 1

    srch_entry.focus()

    if not root:
        dialw.mainloop()
Exemple #7
0
def update_leads_window():
    global leads_window
    leads_window.leads.delete(0, END)
    x = config.load_data("leads")
    fields = x.get('keymap')
    if not fields:
        print("no leads data")
        return
    data = x.get('list')
    print(fields)

    leads_window.leadsmap = {}
    leads_window.leadskeys = fields
    for l1 in data:
        print(l1[fields['l_shop_phone']])
        print(l1[fields['l_client_phone']])
        lead_key = "%s -> %s" % (l1[fields['l_shop_phone']],
                                 l1[fields['l_client_phone']])
        leads_window.leadsmap[lead_key] = l1
        leads_window.leads.insert(END, lead_key)
    return  # is there leads to call immediately for this operator
Exemple #8
0
def f_std(mDM,
          rs,
          inj_particle=None,
          inj_type=None,
          struct=False,
          channel=None):
    """energy deposition fraction into channel c, f_c(z), as a function of dark matter mass and redshift.

    Parameters
    ----------
    mDM : float
        Dark matter mass
    inj_particle : string
        Injected particle, either set to 'phot' for photons, or 'elec' for electrons.
    inj_type : string
        Type of energy injection, either 'swave' or 'decay
    struct : bool
        If *True*, include structure formation, if *False* assume no structure formation. Default is *False*. This option makes no difference for decays.
    """

    if (inj_particle != 'phot') and (inj_particle != 'elec'):
        raise ValueError("inj_particle must either be 'phot' or 'elec'")

    if (inj_type != 'decay') and (inj_type != 'swave'):
        raise ValueError("inj_type must either be 'swave' or 'decay'")

    if channel not in ['H ion', 'cont', 'exc', 'heat', 'He ion']:
        raise ValueError(
            "channel must be in ['H ion', 'He ion', 'exc', 'heat', 'cont']")

    if isinstance(mDM, (int, float)):
        mDM = np.array([mDM]) * 1.
    else:
        mDM = np.array(mDM) * 1.

    if isinstance(rs, (int, float)):
        rs = np.array([rs]) * 1.
    else:
        rs = np.array(rs) * 1.

    struct_str = ''
    if inj_type == 'swave':
        if inj_particle == 'phot':
            Einj = mDM
        else:
            Einj = mDM - me

        if struct:
            struct_str = '_struct'
    else:
        if inj_particle == 'phot':
            Einj = mDM / 2
        else:
            Einj = mDM / 2 - me

    ind_dict = {'H ion': 0, 'He ion': 1, 'exc': 2, 'heat': 3, 'cont': 4}
    ind = ind_dict[channel]
    f_data_baseline = load_data('f')[inj_particle + '_' + inj_type +
                                     struct_str]

    Einj[Einj < 5.001e3] = 5.001e3
    Einj[Einj > 10**12.6015] = 10**12.6015

    if inj_particle != 'phot' or inj_type != 'swave':
        rs[rs < 4.017] = 4.017
    else:
        rs[rs < 5.2] = 5.2
    rs[rs > 3000] = 3000

    return np.exp(f_data_baseline((np.log10(Einj), np.log(rs)))[:, ind])
                            FileName = filename,#.replace("#",""),
                            FilePath = webpath,
                            page = zipcount,
                            FileSize = -1)


        if force_save:
            ind_data.save()

        if fext[1:] in configdata["filetypes"]["image_safe_files"] and skippable:
            break
        #count += 1
#    if index_data.objects.values("id").filter(fqpndirectory=webpath.lower(),
#                                              ignore=False).count() != count:
#        print("Running Validate")
#        validate_database(dir_to_scan)
    return webpath.replace(os.sep, r"/")


if __name__ == '__main__':
    from config import configdata
    import config
    cfg_path = os.path.abspath(r"../cfg")
    config.load_data(os.path.join(cfg_path, "paths.ini"))
    config.load_data(os.path.join(cfg_path, "settings.ini"))
    config.load_data(os.path.join(cfg_path, "filetypes.ini"))
    import doctest
    doctest.testmod()
else:
    from frontend.config import configdata
Exemple #10
0
def get_elec_cooling_tf(
    eleceng, photeng, rs, xHII, xHeII=0, 
    raw_thomson_tf=None, raw_rel_tf=None, raw_engloss_tf=None,
    coll_ion_sec_elec_specs=None, coll_exc_sec_elec_specs=None,
    ics_engloss_data=None, 
    check_conservation_eng = False, verbose=False
):

    """Transfer functions for complete electron cooling through inverse Compton scattering (ICS) and atomic processes.
  

    Parameters
    ----------
    eleceng : ndarray, shape (m, )
        The electron kinetic energy abscissa.
    photeng : ndarray
        The photon energy abscissa.
    rs : float
        The redshift (1+z). 
    xHII : float
        Ionized hydrogen fraction, nHII/nH. 
    xHeII : float, optional
        Singly-ionized helium fraction, nHe+/nH. Default is 0. 
    raw_thomson_tf : TransFuncAtRedshift, optional
        Thomson ICS scattered photon spectrum transfer function. If None, uses the default transfer function. Default is None.
    raw_rel_tf : TransFuncAtRedshift, optional
        Relativistic ICS scattered photon spectrum transfer function. If None, uses the default transfer function. Default is None.
    raw_engloss_tf : TransFuncAtRedshift, optional
        Thomson ICS scattered electron net energy loss transfer function. If None, uses the default transfer function. Default is None.
    coll_ion_sec_elec_specs : tuple of 3 ndarrays, shapes (m, m), optional 
        Normalized collisional ionization secondary electron spectra, order HI, HeI, HeII, indexed by injected electron energy by outgoing electron energy. If None, the function calculates this. Default is None.
    coll_exc_sec_elec_specs : tuple of 3 ndarray, shapes (m, m), optional 
        Normalized collisional excitation secondary electron spectra, order HI, HeI, HeII, indexed by injected electron energy by outgoing electron energy. If None, the function calculates this. Default is None.
    ics_engloss_data : EnglossRebinData
        An `EnglossRebinData` object which stores rebinning information (based on ``eleceng`` and ``photeng``) for speed. Default is None.
    check_conservation_eng : bool
        If True, lower=True, checks for energy conservation. Default is False.
    verbose : bool
        If True, prints energy conservation checks. Default is False.
    
    Returns
    -------

    tuple
        Transfer functions for electron cooling deposition and spectra.

    See Also
    ---------
    :class:`.TransFuncAtRedshift`
    :class:`.EnglossRebinData`
    :mod:`.ics`

    Notes
    -----
    
    The entries of the output tuple are (see Sec IIIC of the paper):

    0. The secondary propagating photon transfer function :math:`\\overline{\\mathsf{T}}_\\gamma`; 
    1. The low-energy electron transfer function :math:`\\overline{\\mathsf{T}}_e`; 
    2. Energy deposited into ionization :math:`\\overline{\\mathbf{R}}_\\text{ion}`; 
    3. Energy deposited into excitation :math:`\\overline{\\mathbf{R}}_\\text{exc}`; 
    4. Energy deposited into heating :math:`\\overline{\\mathbf{R}}_\\text{heat}`;
    5. Upscattered CMB photon total energy :math:`\\overline{\\mathbf{R}}_\\text{CMB}`, and
    6. Numerical error away from energy conservation.

    Items 2--5 are vectors that when dotted into an electron spectrum with abscissa ``eleceng``, return the energy deposited/CMB energy upscattered for that spectrum. 

    Items 0--1 are :class:`.TransFuncAtRedshift` objects. For each of these objects ``tf`` and a given electron spectrum ``elec_spec``, ``tf.sum_specs(elec_spec)`` returns the propagating photon/low-energy electron spectrum after cooling.

    The default version of the three ICS transfer functions that are required by this function is provided in :mod:`.tf_data`.

    """

    # Use default ICS transfer functions if not specified.

    ics_tf = load_data('ics_tf')

    raw_thomson_tf = ics_tf['thomson']
    raw_rel_tf     = ics_tf['rel']
    raw_engloss_tf = ics_tf['engloss']

    if coll_ion_sec_elec_specs is None:

        # Compute the (normalized) collisional ionization spectra.
        coll_ion_sec_elec_specs = (
            phys.coll_ion_sec_elec_spec(eleceng, eleceng, species='HI'),
            phys.coll_ion_sec_elec_spec(eleceng, eleceng, species='HeI'),
            phys.coll_ion_sec_elec_spec(eleceng, eleceng, species='HeII')
        )

    if coll_exc_sec_elec_specs is None:

        # Compute the (normalized) collisional excitation spectra.
        id_mat = np.identity(eleceng.size)

        # Electron with energy eleceng produces a spectrum with one particle
        # of energy eleceng - phys.lya.eng. Similar for helium. 
        coll_exc_sec_elec_tf_HI = tf.TransFuncAtRedshift(
            np.squeeze(id_mat[:, np.where(eleceng > phys.lya_eng)]),
            in_eng = eleceng, rs = -1*np.ones_like(eleceng),
            eng = eleceng[eleceng > phys.lya_eng] - phys.lya_eng,
            dlnz = -1, spec_type = 'N'
        )

        coll_exc_sec_elec_tf_HeI = tf.TransFuncAtRedshift(
            np.squeeze(
                id_mat[:, np.where(eleceng > phys.He_exc_eng['23s'])]
            ),
            in_eng = eleceng, rs = -1*np.ones_like(eleceng),
            eng = (
                eleceng[eleceng > phys.He_exc_eng['23s']] 
                - phys.He_exc_eng['23s']
            ), 
            dlnz = -1, spec_type = 'N'
        )

        coll_exc_sec_elec_tf_HeII = tf.TransFuncAtRedshift(
            np.squeeze(id_mat[:, np.where(eleceng > 4*phys.lya_eng)]),
            in_eng = eleceng, rs = -1*np.ones_like(eleceng),
            eng = eleceng[eleceng > 4*phys.lya_eng] - 4*phys.lya_eng,
            dlnz = -1, spec_type = 'N'
        )

        # Rebin the data so that the spectra stored above now have an abscissa
        # of eleceng again (instead of eleceng - phys.lya_eng for HI etc.)
        coll_exc_sec_elec_tf_HI.rebin(eleceng)
        coll_exc_sec_elec_tf_HeI.rebin(eleceng)
        coll_exc_sec_elec_tf_HeII.rebin(eleceng)

        # Put them in a tuple.
        coll_exc_sec_elec_specs = (
            coll_exc_sec_elec_tf_HI.grid_vals,
            coll_exc_sec_elec_tf_HeI.grid_vals,
            coll_exc_sec_elec_tf_HeII.grid_vals
        )

    # Set the electron fraction. 
    xe = xHII + xHeII
    
    # v/c of electrons
    beta_ele = np.sqrt(1 - 1/(1 + eleceng/phys.me)**2)

    #####################################
    # Inverse Compton
    #####################################

    T = phys.TCMB(rs)

    # Photon transfer function for single primary electron single scattering.
    # This is dN/(dE dt), dt = 1 s.
    phot_ICS_tf = ics_spec(
        eleceng, photeng, T, thomson_tf = raw_thomson_tf, rel_tf = raw_rel_tf
    )

    # Downcasting speeds up np.dot
    phot_ICS_tf._grid_vals = phot_ICS_tf.grid_vals.astype('float64')

    # Energy loss transfer function for single primary electron
    # single scattering. This is dN/(dE dt), dt = 1 s.
    engloss_ICS_tf = engloss_spec(
        eleceng, photeng, T, thomson_tf = raw_engloss_tf, rel_tf = raw_rel_tf
    )

    # Downcasting speeds up np.dot
    engloss_ICS_tf._grid_vals = engloss_ICS_tf.grid_vals.astype('float64')

    # Switch the spectra type here to type 'N'.
    if phot_ICS_tf.spec_type == 'dNdE':
        phot_ICS_tf.switch_spec_type()
    if engloss_ICS_tf.spec_type == 'dNdE':
        engloss_ICS_tf.switch_spec_type()


    # Define some useful lengths.
    N_eleceng = eleceng.size
    N_photeng = photeng.size

    # Create the secondary electron transfer functions.

    # ICS transfer function.
    elec_ICS_tf = tf.TransFuncAtRedshift(
        np.zeros((N_eleceng, N_eleceng)), in_eng = eleceng,
        rs = rs*np.ones_like(eleceng), eng = eleceng,
        dlnz = -1, spec_type = 'N'
    )

    if ics_engloss_data is not None:
        elec_ICS_tf._grid_vals = ics_engloss_data.rebin(
            engloss_ICS_tf.grid_vals
        )
    else:
        elec_ICS_tf._grid_vals = spectools.engloss_rebin_fast(
            eleceng, photeng, engloss_ICS_tf.grid_vals, eleceng
        )
    
    # Total upscattered photon energy.
    cont_loss_ICS_vec = np.zeros_like(eleceng)
    # Deposited energy, enforces energy conservation.
    deposited_ICS_vec = np.zeros_like(eleceng)
    
    #########################
    # Collisional Excitation  
    #########################


    # Collisional excitation rates.
    rate_vec_exc_HI = (
        (1 - xHII)*phys.nH*rs**3 * phys.coll_exc_xsec(eleceng, species='HI') * beta_ele * phys.c
    )
    
    rate_vec_exc_HeI = (
        (phys.nHe/phys.nH - xHeII)*phys.nH*rs**3 * phys.coll_exc_xsec(eleceng, species='HeI') * beta_ele * phys.c
    )
    
    rate_vec_exc_HeII = (
        xHeII*phys.nH*rs**3 * phys.coll_exc_xsec(eleceng, species='HeII') * beta_ele * phys.c
    )

    # Normalized electron spectrum after excitation.
    elec_exc_HI_tf = tf.TransFuncAtRedshift(
        rate_vec_exc_HI[:, np.newaxis]*coll_exc_sec_elec_specs[0],
        in_eng = eleceng, rs = rs*np.ones_like(eleceng),
        eng = eleceng, dlnz = -1, spec_type  = 'N'
    )

    elec_exc_HeI_tf = tf.TransFuncAtRedshift(
        rate_vec_exc_HeI[:, np.newaxis]*coll_exc_sec_elec_specs[1],
        in_eng = eleceng, rs = rs*np.ones_like(eleceng),
        eng = eleceng, dlnz = -1, spec_type  = 'N'
    )

    elec_exc_HeII_tf = tf.TransFuncAtRedshift(
        rate_vec_exc_HeII[:, np.newaxis]*coll_exc_sec_elec_specs[2],
        in_eng = eleceng, rs = rs*np.ones_like(eleceng),
        eng = eleceng, dlnz = -1, spec_type  = 'N'
    )
   
    # Deposited energy for excitation.
    deposited_exc_vec = np.zeros_like(eleceng)


    #########################
    # Collisional Ionization  
    #########################

    # Collisional ionization rates.
    rate_vec_ion_HI = (
        (1 - xHII)*phys.nH*rs**3 
        * phys.coll_ion_xsec(eleceng, species='HI') * beta_ele * phys.c
    )
    
    rate_vec_ion_HeI = (
        (phys.nHe/phys.nH - xHeII)*phys.nH*rs**3 
        * phys.coll_ion_xsec(eleceng, species='HeI') * beta_ele * phys.c
    )
    
    rate_vec_ion_HeII = (
        xHeII*phys.nH*rs**3
        * phys.coll_ion_xsec(eleceng, species='HeII') * beta_ele * phys.c
    )

    # Normalized secondary electron spectra after ionization.
    elec_spec_ion_HI   = (
        rate_vec_ion_HI[:,np.newaxis]   * coll_ion_sec_elec_specs[0]
    )
    elec_spec_ion_HeI  = (
        rate_vec_ion_HeI[:,np.newaxis]  * coll_ion_sec_elec_specs[1]
    )
    elec_spec_ion_HeII = (
        rate_vec_ion_HeII[:,np.newaxis] * coll_ion_sec_elec_specs[2]
    )

    # Construct TransFuncAtRedshift objects.
    elec_ion_HI_tf = tf.TransFuncAtRedshift(
        elec_spec_ion_HI, in_eng = eleceng, rs = rs*np.ones_like(eleceng), 
        eng = eleceng, dlnz = -1, spec_type = 'N'
    )
    elec_ion_HeI_tf = tf.TransFuncAtRedshift(
        elec_spec_ion_HeI, in_eng = eleceng, rs = rs*np.ones_like(eleceng), 
        eng = eleceng, dlnz = -1, spec_type = 'N'
    )
    elec_ion_HeII_tf = tf.TransFuncAtRedshift(
        elec_spec_ion_HeII, in_eng = eleceng, rs = rs*np.ones_like(eleceng), 
        eng = eleceng, dlnz = -1, spec_type = 'N'
    )

    # Deposited energy for ionization.
    deposited_ion_vec = np.zeros_like(eleceng)

     #############################################
    # Heating
    #############################################
    
    dE_heat_dt = phys.elec_heating_engloss_rate(eleceng, xe, rs)
    
    deposited_heat_vec = np.zeros_like(eleceng)

    # new_eleceng = eleceng - dE_heat_dt

    # if not np.all(new_eleceng[1:] > eleceng[:-1]):
    #     utils.compare_arr([new_eleceng, eleceng])
    #     raise ValueError('heating loss is too large: smaller time step required.')

    # # After the check above, we can define the spectra by
    # # manually assigning slightly less than 1 particle along
    # # diagonal, and a small amount in the bin below. 

    # # N_n-1 E_n-1 + N_n E_n = E_n - dE_dt
    # # N_n-1 + N_n = 1
    # # therefore, (1 - N_n) E_n-1 - (1 - N_n) E_n = - dE_dt
    # # i.e. N_n = 1 + dE_dt/(E_n-1 - E_n)

    elec_heat_spec_grid = np.identity(eleceng.size)
    elec_heat_spec_grid[0,0] -= dE_heat_dt[0]/eleceng[0]
    elec_heat_spec_grid[1:, 1:] += np.diag(
        dE_heat_dt[1:]/(eleceng[:-1] - eleceng[1:])
    )
    elec_heat_spec_grid[1:, :-1] -= np.diag(
        dE_heat_dt[1:]/(eleceng[:-1] - eleceng[1:])
    )


    #############################################
    # Initialization of secondary spectra 
    #############################################

    # Low and high energy boundaries
    loweng = 3000
    eleceng_high = eleceng[eleceng > loweng]
    eleceng_high_ind = np.arange(eleceng.size)[eleceng > loweng]
    eleceng_low = eleceng[eleceng <= loweng]
    eleceng_low_ind  = np.arange(eleceng.size)[eleceng <= loweng]


    if eleceng_low.size == 0:
        raise TypeError('Energy abscissa must contain a low energy bin below 3 keV.')

    # Empty containers for quantities.
    # Final secondary photon spectrum.
    sec_phot_tf = tf.TransFuncAtRedshift(
        np.zeros((N_eleceng, N_photeng)), in_eng = eleceng,
        rs = rs*np.ones_like(eleceng), eng = photeng,
        dlnz = -1, spec_type = 'N'
    )
    # Final secondary low energy electron spectrum.
    sec_lowengelec_tf = tf.TransFuncAtRedshift(
        np.zeros((N_eleceng, N_eleceng)), in_eng = eleceng,
        rs = rs*np.ones_like(eleceng), eng = eleceng,
        dlnz = -1, spec_type = 'N'
    )

    # Continuum energy loss rate per electron, dU_CMB/dt.
    CMB_upscatter_eng_rate = phys.thomson_xsec*phys.c*phys.CMB_eng_density(T)
    
    # Secondary scattered electron spectrum.
    sec_elec_spec_N_arr = (
        elec_ICS_tf.grid_vals
        + elec_exc_HI_tf.grid_vals 
        + elec_exc_HeI_tf.grid_vals 
        + elec_exc_HeII_tf.grid_vals
        + elec_ion_HI_tf.grid_vals 
        + elec_ion_HeI_tf.grid_vals 
        + elec_ion_HeII_tf.grid_vals
        + elec_heat_spec_grid
    )
    
    # Secondary photon spectrum (from ICS). 
    sec_phot_spec_N_arr = phot_ICS_tf.grid_vals
    
    # Deposited ICS array.
    deposited_ICS_eng_arr = (
        np.sum(elec_ICS_tf.grid_vals, axis=1)*eleceng
        - np.dot(elec_ICS_tf.grid_vals, eleceng)
        - (np.dot(sec_phot_spec_N_arr, photeng) - CMB_upscatter_eng_rate)
    )

    # Energy loss is not taken into account for eleceng > 20*phys.me
    deposited_ICS_eng_arr[eleceng > 20*phys.me - phys.me] -= ( 
        CMB_upscatter_eng_rate
    )

    # Continuum energy loss array.
    continuum_engloss_arr = CMB_upscatter_eng_rate*np.ones_like(eleceng)
    # Energy loss is not taken into account for eleceng > 20*phys.me
    continuum_engloss_arr[eleceng > 20*phys.me - phys.me] = 0
    
    # Deposited excitation array.
    deposited_exc_eng_arr = (
        phys.lya_eng*np.sum(elec_exc_HI_tf.grid_vals, axis=1)
        + phys.He_exc_eng['23s']*np.sum(elec_exc_HeI_tf.grid_vals, axis=1)
        + 4*phys.lya_eng*np.sum(elec_exc_HeII_tf.grid_vals, axis=1)
    )
    
    # Deposited ionization array.
    deposited_ion_eng_arr = (
        phys.rydberg*np.sum(elec_ion_HI_tf.grid_vals, axis=1)/2
        + phys.He_ion_eng*np.sum(elec_ion_HeI_tf.grid_vals, axis=1)/2
        + 4*phys.rydberg*np.sum(elec_ion_HeII_tf.grid_vals, axis=1)/2
    )

    # Deposited heating array.
    deposited_heat_eng_arr = dE_heat_dt
    
    # Remove self-scattering, re-normalize. 
    np.fill_diagonal(sec_elec_spec_N_arr, 0)
    
    toteng_no_self_scatter_arr = (
        np.dot(sec_elec_spec_N_arr, eleceng)
        + np.dot(sec_phot_spec_N_arr, photeng)
        - continuum_engloss_arr
        + deposited_ICS_eng_arr
        + deposited_exc_eng_arr
        + deposited_ion_eng_arr
        + deposited_heat_eng_arr
    )

    fac_arr = eleceng/toteng_no_self_scatter_arr
    
    sec_elec_spec_N_arr *= fac_arr[:, np.newaxis]
    sec_phot_spec_N_arr *= fac_arr[:, np.newaxis]
    continuum_engloss_arr  *= fac_arr
    deposited_ICS_eng_arr  *= fac_arr
    deposited_exc_eng_arr  *= fac_arr
    deposited_ion_eng_arr  *= fac_arr
    deposited_heat_eng_arr *= fac_arr
    
    # Zero out deposition/ICS processes below loweng. 
    deposited_ICS_eng_arr[eleceng < loweng]  = 0
    deposited_exc_eng_arr[eleceng < loweng]  = 0
    deposited_ion_eng_arr[eleceng < loweng]  = 0
    deposited_heat_eng_arr[eleceng < loweng] = 0
    
    continuum_engloss_arr[eleceng < loweng]  = 0
    
    sec_phot_spec_N_arr[eleceng < loweng] = 0
    
    # Scattered low energy and high energy electrons. 
    # Needed for final low energy electron spectra.
    sec_lowengelec_N_arr = np.identity(eleceng.size)
    sec_lowengelec_N_arr[eleceng >= loweng] = 0
    sec_lowengelec_N_arr[eleceng_high_ind[0]:, :eleceng_high_ind[0]] += sec_elec_spec_N_arr[eleceng_high_ind[0]:, :eleceng_high_ind[0]]

    sec_highengelec_N_arr = np.zeros_like(sec_elec_spec_N_arr)
    sec_highengelec_N_arr[:, eleceng_high_ind[0]:] = (
        sec_elec_spec_N_arr[:, eleceng_high_ind[0]:]
    )
    
    # T = E.T + Prompt
    deposited_ICS_vec  = solve_triangular(
        np.identity(eleceng.size) - sec_elec_spec_N_arr,
        deposited_ICS_eng_arr, lower=True, check_finite=False
    )
    deposited_exc_vec  = solve_triangular(
        np.identity(eleceng.size) - sec_elec_spec_N_arr, 
        deposited_exc_eng_arr, lower=True, check_finite=False
    )
    deposited_ion_vec  = solve_triangular(
        np.identity(eleceng.size) - sec_elec_spec_N_arr, 
        deposited_ion_eng_arr, lower=True, check_finite=False
    )
    deposited_heat_vec = solve_triangular(
        np.identity(eleceng.size) - sec_elec_spec_N_arr, 
        deposited_heat_eng_arr, lower=True, check_finite=False
    )
    
    cont_loss_ICS_vec = solve_triangular(
        np.identity(eleceng.size) - sec_elec_spec_N_arr, 
        continuum_engloss_arr, lower=True, check_finite=False
    )
    
    sec_phot_specs = solve_triangular(
        np.identity(eleceng.size) - sec_elec_spec_N_arr, 
        sec_phot_spec_N_arr, lower=True, check_finite=False
    )
    
    # Prompt: low energy e produced in secondary spectrum upon scattering (sec_lowengelec_N_arr).
    # T : high energy e produced (sec_highengelec_N_arr). 
    sec_lowengelec_specs = solve_triangular(
        np.identity(eleceng.size) - sec_highengelec_N_arr,
        sec_lowengelec_N_arr, lower=True, check_finite=False
    )

    # Subtract continuum from sec_phot_specs. After this point, 
    # sec_phot_specs will contain the *distortions* to the CMB. 

    # Normalized CMB spectrum. 
    norm_CMB_spec = Spectrum(
        photeng, phys.CMB_spec(photeng, phys.TCMB(rs)), spec_type='dNdE'
    )
    norm_CMB_spec /= norm_CMB_spec.toteng()

    # Get the CMB spectrum upscattered from cont_loss_ICS_vec. 
    upscattered_CMB_grid = np.outer(cont_loss_ICS_vec, norm_CMB_spec.N)

    # Subtract this spectrum from sec_phot_specs to get the final
    # transfer function.

    sec_phot_tf._grid_vals = sec_phot_specs - upscattered_CMB_grid
    sec_lowengelec_tf._grid_vals = sec_lowengelec_specs

    # Conservation checks.
    failed_conservation_check = False

    if check_conservation_eng:

        conservation_check = (
            eleceng
            - np.dot(sec_lowengelec_tf.grid_vals, eleceng)
            # + cont_loss_ICS_vec
            - np.dot(sec_phot_tf.grid_vals, photeng)
            - deposited_exc_vec
            - deposited_ion_vec
            - deposited_heat_vec
        )

        if np.any(np.abs(conservation_check/eleceng) > 0.1):
            failed_conservation_check = True

        if verbose or failed_conservation_check:

            for i,eng in enumerate(eleceng):

                print('***************************************************')
                print('rs: ', rs)
                print('injected energy: ', eng)

                print(
                    'Fraction of Energy in low energy electrons: ',
                    np.dot(sec_lowengelec_tf.grid_vals[i], eleceng)/eng
                )

                # print('Energy in photons: ', 
                #     np.dot(sec_phot_tf.grid_vals[i], photeng)
                # )
                
                # print('Continuum_engloss: ', cont_loss_ICS_vec[i])
                
                print(
                    'Fraction of Energy in photons - Continuum: ', (
                        np.dot(sec_phot_tf.grid_vals[i], photeng)/eng
                        # - cont_loss_ICS_vec[i]
                    )
                )

                print(
                    'Fraction Deposited in ionization: ', 
                    deposited_ion_vec[i]/eng
                )

                print(
                    'Fraction Deposited in excitation: ', 
                    deposited_exc_vec[i]/eng
                )

                print(
                    'Fraction Deposited in heating: ', 
                    deposited_heat_vec[i]/eng
                )

                print(
                    'Energy is conserved up to (%): ',
                    conservation_check[i]/eng*100
                )
                print('Fraction Deposited in ICS (Numerical Error): ', 
                    deposited_ICS_vec[i]/eng
                )
                
                print(
                    'Energy conservation with deposited (%): ',
                    (conservation_check[i] - deposited_ICS_vec[i])/eng*100
                )
                print('***************************************************')
                
            if failed_conservation_check:
                raise RuntimeError('Conservation of energy failed.')

    return (
        sec_phot_tf, sec_lowengelec_tf,
        deposited_ion_vec, deposited_exc_vec, deposited_heat_vec,
        cont_loss_ICS_vec, deposited_ICS_vec
    )
Exemple #11
0
		_, test_loss = test(NN, X_test, Y_test)
		plot_error(epoch, error, test_loss, plt)
	print("Training\nAccuracy: %f Loss: %f " % test(NN, X_train, Y_train))
	print_images(NN, X_train, Y_train, X_test, Y_test)

def test(NN, X_test, Y_test):
	loss = 0
	correct = 0
	for X, Y in zip(X_test, Y_test):
		y = NN.forward(X)
		loss += mse(Y, y)
		if cf.configuration == 1:
			idx = np.argmax(y.flatten())
			y = np.zeros((10, 1))
			y[idx, 0] = 1
		else:
			y = (y >= 0.5)
		if np.array_equal(Y, y):
			correct += 1
		else:
			pass
	return correct / len(X_test) * 100, loss / len(X_test)

if __name__ == '__main__':
	NN = NN.model(cf.number_of_layers, cf.lr, cf.layers, cf.activations, bias=bias)
	X_train, Y_train, X_test, Y_test = cf.load_data()
	train(NN, X_train, Y_train, X_test, Y_test)
	accuracy, loss = test(NN, X_test, Y_test)
	print("Testing")
	print("\tAccuracy: %f%% Loss %f" % (accuracy, loss))
	plt.pause(10000)
Exemple #12
0
def load_shops():
    print("Dialw: load shops")
    shops = load_data("shops")
    shops.sort(key=lambda x: x[0].lower())
    return shops
Exemple #13
0
from dataset_tricks import f_score
from dataset_tricks import xgboost_feature_selection
from dataset_tricks import cat_to_num
from dataset_tricks import feature_variances
from dataset_tricks import data_binning
from dataset_tricks import reduce_variances
from data_cleaning import countNaNs
from Model import train, validate, predict
from datetime import datetime

beginning = datetime.now()

print("Loading Data...")

#Load the data
train_df = config.load_data(config.TRAIN_DATASET_PATH)
target_df = config.load_data(config.TARGET_DATASET_PATH)
test_df = config.load_data(config.TEST_DATASET_PATH)
# xgboost_features = config.load_data(config.XGBOOST_FEATURE_PATH)

print("Reducing Memory Allocation...")

#Save some memory
train_df = memoryReduce(train_df)
target_df = memoryReduce(target_df)
test_df = memoryReduce(test_df)

# uncomment to check the data types and memory allocations
# train_df.info()
# test_df.info()
# target_df.info()
Exemple #14
0
 def load(self):
     for s in load_data("shops"):
         #print(s)
         self.by_phone[s[1]] = self.by_dest[s[3]] = self.by_name[s[0]] = s
Exemple #15
0
        self.by_dest = {}
        self.by_name = {}

    def load(self):
        for s in load_data("shops"):
            #print(s)
            self.by_phone[s[1]] = self.by_dest[s[3]] = self.by_name[s[0]] = s
            # name, script, phone


shops = ShopsData()
shops.load()
#print(shops.by_dest); exit()

call_tags = []
for tag_id, tag_name in load_data("tags"):
    call_tags.append(tag_name)

#print(call_tags)
#print(dir(root))
#print(root.winfo_screenwidth(), root.winfo_screenheight())

bgthread = threading.Thread(target=bg_task)
bg_run = True
bg_run_showpage = None
bgthread.start()


def init_help_window():
    global bg_run, bg_run_showpage
    try:
Exemple #16
0
import statsmodels.formula.api as sm
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn import tree
from sklearn.cross_validation import train_test_split

from config import categorical_features, numerical_features
from config import FPlength_dict, Pill_dict, CycleVar_dict, NCbefore_dict, ExitStatus_dict
from config import load_data, encode_all, get_algo_performance

describe_and_plot = 0

#****************************************************************

# Read in and pre-process the input data
data = load_data()

# ***************************************************************

# Explore and visualize the data

if describe_and_plot:

    data.describe()

    # Print out the statistics for categorical features
    for c in categorical_features:
        data[c].value_counts()
        fig = plt.figure()
        data[c].value_counts().plot(kind='bar')
        plt.savefig('visualization/general/' + c + '.pdf')
Exemple #17
0
def evolve(in_spec_elec=None,
           in_spec_phot=None,
           rate_func_N=None,
           rate_func_eng=None,
           DM_process=None,
           mDM=None,
           sigmav=None,
           lifetime=None,
           primary=None,
           struct_boost=None,
           start_rs=None,
           end_rs=4,
           helium_TLA=False,
           reion_switch=False,
           reion_rs=None,
           photoion_rate_func=None,
           photoheat_rate_func=None,
           xe_reion_func=None,
           init_cond=None,
           coarsen_factor=1,
           backreaction=True,
           compute_fs_method='no_He',
           mxstep=1000,
           rtol=1e-4,
           use_tqdm=True,
           cross_check=False):
    """
    Main function computing histories and spectra. 

    Parameters
    -----------
    in_spec_elec : :class:`.Spectrum`, optional
        Spectrum per injection event into electrons. *in_spec_elec.rs*
        of the :class:`.Spectrum` must be the initial redshift. 
    in_spec_phot : :class:`.Spectrum`, optional
        Spectrum per injection event into photons. *in_spec_phot.rs* 
        of the :class:`.Spectrum` must be the initial redshift. 
    rate_func_N : function, optional
        Function returning number of injection events per volume per time, with redshift :math:`(1+z)` as an input.  
    rate_func_eng : function, optional
        Function returning energy injected per volume per time, with redshift :math:`(1+z)` as an input. 
    DM_process : {'swave', 'decay'}, optional
        Dark matter process to use. 
    sigmav : float, optional
        Thermally averaged cross section for dark matter annihilation. 
    lifetime : float, optional
        Decay lifetime for dark matter decay.
    primary : string, optional
        Primary channel of annihilation/decay. See :func:`.get_pppc_spec` for complete list. Use *'elec_delta'* or *'phot_delta'* for delta function injections of a pair of photons/an electron-positron pair. 
    struct_boost : function, optional
        Energy injection boost factor due to structure formation.
    start_rs : float, optional
        Starting redshift :math:`(1+z)` to evolve from. Default is :math:`(1+z)` = 3000. Specify only for use with *DM_process*. Otherwise, initialize *in_spec_elec.rs* and/or *in_spec_phot.rs* directly. 
    end_rs : float, optional
        Final redshift :math:`(1+z)` to evolve to. Default is 1+z = 4. 
    reion_switch : bool
        Reionization model included if *True*, default is *False*. 
    helium_TLA : bool
        If *True*, the TLA is solved with helium. Default is *False*.
    reion_rs : float, optional
        Redshift :math:`(1+z)` at which reionization effects turn on. 
    photoion_rate_func : tuple of functions, optional
        Functions take redshift :math:`1+z` as input, return the photoionization rate in s\ :sup:`-1` of HI, HeI and HeII respectively. If not specified, defaults to :func:`.photoion_rate`.
    photoheat_rate_func : tuple of functions, optional
        Functions take redshift :math:`1+z` as input, return the photoheating rate in s\ :sup:`-1` of HI, HeI and HeII respectively. If not specified, defaults to :func:`.photoheat_rate`.
    xe_reion_func : function, optional
        Specifies a fixed ionization history after reion_rs.
    init_cond : tuple of floats
        Specifies the initial (xH, xHe, Tm). Defaults to :func:`.Tm_std`, :func:`.xHII_std` and :func:`.xHeII_std` at the *start_rs*. 
    coarsen_factor : int
        Coarsening to apply to the transfer function matrix. Default is 1. 
    backreaction : bool
        If *False*, uses the baseline TLA solution to calculate :math:`f_c(z)`. Default is True.
    compute_fs_method : {'no_He', 'He_recomb', 'He'}

    mxstep : int, optional
        The maximum number of steps allowed for each integration point. See *scipy.integrate.odeint()* for more information. Default is *1000*. 
    rtol : float, optional
        The relative error of the solution. See *scipy.integrate.odeint()* for more information. Default is *1e-4*.
    use_tqdm : bool, optional
        Uses tqdm if *True*. Default is *True*. 
    cross_check : bool, optional
        If *True*, compare against 1604.02457 by using original MEDEA files, turning off partial binning, etc. Default is *False*.

    Examples
    --------

    1. *Dark matter annihilation* -- dark matter mass of 50 GeV, annihilation cross section :math:`2 \\times 10^{-26}` cm\ :sup:`3` s\ :sup:`-1`, annihilating to :math:`b \\bar{b}`, solved without backreaction, a coarsening factor of 32 and the default structure formation boost: ::

        import darkhistory.physics as phys

        out = evolve(
            DM_process='swave', mDM=50e9, sigmav=2e-26, 
            primary='b', start_rs=3000., 
            backreaction=False,
            struct_boost=phys.struct_boost_func()
        )

    2. *Dark matter decay* -- dark matter mass of 100 GeV, decay lifetime :math:`3 \\times 10^{25}` s, decaying to a pair of :math:`e^+e^-`, solved with backreaction, a coarsening factor of 16: ::

        out = evolve(
            DM_process='decay', mDM=1e8, lifetime=3e25,
            primary='elec_delta', start_rs=3000.,
            backreaction=True
        ) 

    See Also
    ---------
    :func:`.get_pppc_spec`

    :func:`.struct_boost_func`

    :func:`.photoion_rate`, :func:`.photoheat_rate`

    :func:`.Tm_std`, :func:`.xHII_std` and :func:`.xHeII_std`


    """

    #########################################################################
    #########################################################################
    # Input                                                                 #
    #########################################################################
    #########################################################################

    #####################################
    # Initialization for DM_process     #
    #####################################

    # Load data.
    binning = load_data('binning')
    photeng = binning['phot']
    eleceng = binning['elec']

    dep_tf_data = load_data('dep_tf')

    highengphot_tf_interp = dep_tf_data['highengphot']
    lowengphot_tf_interp = dep_tf_data['lowengphot']
    lowengelec_tf_interp = dep_tf_data['lowengelec']
    highengdep_interp = dep_tf_data['highengdep']

    ics_tf_data = load_data('ics_tf')

    ics_thomson_ref_tf = ics_tf_data['thomson']
    ics_rel_ref_tf = ics_tf_data['rel']
    engloss_ref_tf = ics_tf_data['engloss']

    # Handle the case where a DM process is specified.
    if DM_process == 'swave':
        if sigmav is None or start_rs is None:
            raise ValueError('sigmav and start_rs must be specified.')

        # Get input spectra from PPPC.
        in_spec_elec = pppc.get_pppc_spec(mDM, eleceng, primary, 'elec')
        in_spec_phot = pppc.get_pppc_spec(mDM, photeng, primary, 'phot')
        # Initialize the input spectrum redshift.
        in_spec_elec.rs = start_rs
        in_spec_phot.rs = start_rs
        # Convert to type 'N'.
        in_spec_elec.switch_spec_type('N')
        in_spec_phot.switch_spec_type('N')

        # If struct_boost is none, just set to 1.
        if struct_boost is None:

            def struct_boost(rs):
                return 1.

        # Define the rate functions.
        def rate_func_N(rs):
            return (phys.inj_rate('swave', rs, mDM=mDM, sigmav=sigmav) *
                    struct_boost(rs) / (2 * mDM))

        def rate_func_eng(rs):
            return (phys.inj_rate('swave', rs, mDM=mDM, sigmav=sigmav) *
                    struct_boost(rs))

    if DM_process == 'decay':
        if lifetime is None or start_rs is None:
            raise ValueError('lifetime and start_rs must be specified.')

        # The decay rate is insensitive to structure formation
        def struct_boost(rs):
            return 1

        # Get spectra from PPPC.
        in_spec_elec = pppc.get_pppc_spec(mDM,
                                          eleceng,
                                          primary,
                                          'elec',
                                          decay=True)
        in_spec_phot = pppc.get_pppc_spec(mDM,
                                          photeng,
                                          primary,
                                          'phot',
                                          decay=True)

        # Initialize the input spectrum redshift.
        in_spec_elec.rs = start_rs
        in_spec_phot.rs = start_rs
        # Convert to type 'N'.
        in_spec_elec.switch_spec_type('N')
        in_spec_phot.switch_spec_type('N')

        # Define the rate functions.
        def rate_func_N(rs):
            return (phys.inj_rate('decay', rs, mDM=mDM, lifetime=lifetime) /
                    mDM)

        def rate_func_eng(rs):
            return phys.inj_rate('decay', rs, mDM=mDM, lifetime=lifetime)

    #####################################
    # Input Checks                      #
    #####################################

    if (not np.array_equal(in_spec_elec.eng, eleceng)
            or not np.array_equal(in_spec_phot.eng, photeng)):
        raise ValueError(
            'in_spec_elec and in_spec_phot must use config.photeng and config.eleceng respectively as abscissa.'
        )

    if (highengphot_tf_interp.dlnz != lowengphot_tf_interp.dlnz
            or highengphot_tf_interp.dlnz != lowengelec_tf_interp.dlnz
            or lowengphot_tf_interp.dlnz != lowengelec_tf_interp.dlnz):
        raise ValueError(
            'TransferFuncInterp objects must all have the same dlnz.')

    if in_spec_elec.rs != in_spec_phot.rs:
        raise ValueError('Input spectra must have the same rs.')

    if cross_check:
        print(
            'cross_check has been set to True -- No longer using all MEDEA files and no longer using partial-binning.'
        )

    #####################################
    # Initialization                    #
    #####################################

    # Initialize start_rs for arbitrary injection.
    start_rs = in_spec_elec.rs

    # Initialize the initial x and Tm.
    if init_cond is None:
        # Default to baseline
        xH_init = phys.xHII_std(start_rs)
        xHe_init = phys.xHeII_std(start_rs)
        Tm_init = phys.Tm_std(start_rs)
    else:
        # User-specified.
        xH_init = init_cond[0]
        xHe_init = init_cond[1]
        Tm_init = init_cond[2]

    # Initialize redshift/timestep related quantities.

    # Default step in the transfer function. Note highengphot_tf_interp.dlnz
    # contains 3 different regimes, and we start with the first.
    dlnz = highengphot_tf_interp.dlnz[-1]

    # The current redshift.
    rs = start_rs

    # The timestep between evaluations of transfer functions, including
    # coarsening.
    dt = dlnz * coarsen_factor / phys.hubble(rs)

    # tqdm set-up.
    if use_tqdm:
        from tqdm import tqdm_notebook as tqdm
        pbar = tqdm(total=np.ceil((np.log(rs) - np.log(end_rs)) / dlnz /
                                  coarsen_factor))

    def norm_fac(rs):
        # Normalization to convert from per injection event to
        # per baryon per dlnz step.
        return rate_func_N(rs) * (dlnz * coarsen_factor / phys.hubble(rs) /
                                  (phys.nB * rs**3))

    def rate_func_eng_unclustered(rs):
        # The rate excluding structure formation for s-wave annihilation.
        # This is the correct normalization for f_c(z).
        if struct_boost is not None:
            return rate_func_eng(rs) / struct_boost(rs)
        else:
            return rate_func_eng(rs)

    # If there are no electrons, we get a speed up by ignoring them.
    elec_processes = False
    if in_spec_elec.totN() > 0:
        elec_processes = True

    if elec_processes:

        #####################################
        # High-Energy Electrons             #
        #####################################

        # Get the data necessary to compute the electron cooling results.
        # coll_ion_sec_elec_specs is \bar{N} for collisional ionization,
        # and coll_exc_sec_elec_specs \bar{N} for collisional excitation.
        # Heating and others are evaluated in get_elec_cooling_tf
        # itself.

        # Contains information that makes converting an energy loss spectrum
        # to a scattered electron spectrum fast.
        (coll_ion_sec_elec_specs, coll_exc_sec_elec_specs,
         ics_engloss_data) = get_elec_cooling_data(eleceng, photeng)

    #########################################################################
    #########################################################################
    # Pre-Loop Preliminaries                                                #
    #########################################################################
    #########################################################################

    # Initialize the arrays that will contain x and Tm results.
    x_arr = np.array([[xH_init, xHe_init]])
    Tm_arr = np.array([Tm_init])

    # Initialize Spectra objects to contain all of the output spectra.

    out_highengphot_specs = Spectra([], spec_type='N')
    out_lowengphot_specs = Spectra([], spec_type='N')
    out_lowengelec_specs = Spectra([], spec_type='N')

    # Define these methods for speed.
    append_highengphot_spec = out_highengphot_specs.append
    append_lowengphot_spec = out_lowengphot_specs.append
    append_lowengelec_spec = out_lowengelec_specs.append

    # Initialize arrays to store f values.
    f_low = np.empty((0, 5))
    f_high = np.empty((0, 5))

    # Initialize array to store high-energy energy deposition rate.
    highengdep_grid = np.empty((0, 4))

    # Object to help us interpolate over MEDEA results.
    MEDEA_interp = make_interpolator(interp_type='2D', cross_check=cross_check)

    #########################################################################
    #########################################################################
    # LOOP! LOOP! LOOP! LOOP!                                               #
    #########################################################################
    #########################################################################

    while rs > end_rs:

        # Update tqdm.
        if use_tqdm:
            pbar.update(1)

        #############################
        # First Step Special Cases  #
        #############################
        if rs == start_rs:
            # Initialize the electron and photon arrays.
            # These will carry the spectra produced by applying the
            # transfer function at rs to high-energy photons.
            highengphot_spec_at_rs = in_spec_phot * 0
            lowengphot_spec_at_rs = in_spec_phot * 0
            lowengelec_spec_at_rs = in_spec_elec * 0
            highengdep_at_rs = np.zeros(4)

        #####################################################################
        #####################################################################
        # Electron Cooling                                                  #
        #####################################################################
        #####################################################################

        # Get the transfer functions corresponding to electron cooling.
        # These are \bar{T}_\gamma, \bar{T}_e and \bar{R}_c.
        if elec_processes:

            if backreaction:
                xHII_elec_cooling = x_arr[-1, 0]
                xHeII_elec_cooling = x_arr[-1, 1]
            else:
                xHII_elec_cooling = phys.xHII_std(rs)
                xHeII_elec_cooling = phys.xHeII_std(rs)

            (ics_sec_phot_tf, elec_processes_lowengelec_tf, deposited_ion_arr,
             deposited_exc_arr, deposited_heat_arr, continuum_loss,
             deposited_ICS_arr) = get_elec_cooling_tf(
                 eleceng,
                 photeng,
                 rs,
                 xHII_elec_cooling,
                 xHeII=xHeII_elec_cooling,
                 raw_thomson_tf=ics_thomson_ref_tf,
                 raw_rel_tf=ics_rel_ref_tf,
                 raw_engloss_tf=engloss_ref_tf,
                 coll_ion_sec_elec_specs=coll_ion_sec_elec_specs,
                 coll_exc_sec_elec_specs=coll_exc_sec_elec_specs,
                 ics_engloss_data=ics_engloss_data)

            # Apply the transfer function to the input electron spectrum.

            # Low energy electrons from electron cooling, per injection event.
            elec_processes_lowengelec_spec = (
                elec_processes_lowengelec_tf.sum_specs(in_spec_elec))

            # Add this to lowengelec_at_rs.
            lowengelec_spec_at_rs += (elec_processes_lowengelec_spec *
                                      norm_fac(rs))

            # High-energy deposition into ionization,
            # *per baryon in this step*.
            deposited_ion = np.dot(deposited_ion_arr,
                                   in_spec_elec.N * norm_fac(rs))
            # High-energy deposition into excitation,
            # *per baryon in this step*.
            deposited_exc = np.dot(deposited_exc_arr,
                                   in_spec_elec.N * norm_fac(rs))
            # High-energy deposition into heating,
            # *per baryon in this step*.
            deposited_heat = np.dot(deposited_heat_arr,
                                    in_spec_elec.N * norm_fac(rs))
            # High-energy deposition numerical error,
            # *per baryon in this step*.
            deposited_ICS = np.dot(deposited_ICS_arr,
                                   in_spec_elec.N * norm_fac(rs))

            #######################################
            # Photons from Injected Electrons     #
            #######################################

            # ICS secondary photon spectrum after electron cooling,
            # per injection event.
            ics_phot_spec = ics_sec_phot_tf.sum_specs(in_spec_elec)

            # Get the spectrum from positron annihilation, per injection event.
            # Only half of in_spec_elec is positrons!
            positronium_phot_spec = pos.weighted_photon_spec(photeng) * (
                in_spec_elec.totN() / 2)
            positronium_phot_spec.switch_spec_type('N')

        # Add injected photons + photons from injected electrons
        # to the photon spectrum that got propagated forward.
        if elec_processes:
            highengphot_spec_at_rs += (in_spec_phot + ics_phot_spec +
                                       positronium_phot_spec) * norm_fac(rs)
        else:
            highengphot_spec_at_rs += in_spec_phot * norm_fac(rs)

        # Set the redshift correctly.
        highengphot_spec_at_rs.rs = rs

        #####################################################################
        #####################################################################
        # Save the Spectra!                                                 #
        #####################################################################
        #####################################################################

        # At this point, highengphot_at_rs, lowengphot_at_rs and
        # lowengelec_at_rs have been computed for this redshift.
        append_highengphot_spec(highengphot_spec_at_rs)
        append_lowengphot_spec(lowengphot_spec_at_rs)
        append_lowengelec_spec(lowengelec_spec_at_rs)

        #####################################################################
        #####################################################################
        # Compute f_c(z)                                                    #
        #####################################################################
        #####################################################################
        if elec_processes:
            # High-energy deposition from input electrons.
            highengdep_at_rs += np.array([
                deposited_ion / dt, deposited_exc / dt, deposited_heat / dt,
                deposited_ICS / dt
            ])

        # Values of (xHI, xHeI, xHeII) to use for computing f.
        if backreaction:
            # Use the previous values with backreaction.
            x_vec_for_f = np.array(
                [1. - x_arr[-1, 0], phys.chi - x_arr[-1, 1], x_arr[-1, 1]])
        else:
            # Use baseline values if no backreaction.
            x_vec_for_f = np.array([
                1. - phys.xHII_std(rs), phys.chi - phys.xHeII_std(rs),
                phys.xHeII_std(rs)
            ])

        f_raw = compute_fs(MEDEA_interp,
                           lowengelec_spec_at_rs,
                           lowengphot_spec_at_rs,
                           x_vec_for_f,
                           rate_func_eng_unclustered(rs),
                           dt,
                           highengdep_at_rs,
                           method=compute_fs_method,
                           cross_check=cross_check)

        # Save the f_c(z) values.
        f_low = np.concatenate((f_low, [f_raw[0]]))
        f_high = np.concatenate((f_high, [f_raw[1]]))

        # print(f_low, f_high)

        # Save CMB upscattered rate and high-energy deposition rate.
        highengdep_grid = np.concatenate((highengdep_grid, [highengdep_at_rs]))

        # Compute f for TLA: sum of low and high.
        f_H_ion = f_raw[0][0] + f_raw[1][0]
        f_exc = f_raw[0][2] + f_raw[1][2]
        f_heat = f_raw[0][3] + f_raw[1][3]

        if compute_fs_method == 'old':
            # The old method neglects helium.
            f_He_ion = 0.
        else:
            f_He_ion = f_raw[0][1] + f_raw[1][1]

        #####################################################################
        #####################################################################
        # ********* AFTER THIS, COMPUTE QUANTITIES FOR NEXT STEP *********  #
        #####################################################################
        #####################################################################

        # Define the next redshift step.
        next_rs = np.exp(np.log(rs) - dlnz * coarsen_factor)

        #####################################################################
        #####################################################################
        # TLA Integration                                                   #
        #####################################################################
        #####################################################################

        # Initial conditions for the TLA, (Tm, xHII, xHeII, xHeIII).
        # This is simply the last set of these variables.
        init_cond_TLA = np.array([Tm_arr[-1], x_arr[-1, 0], x_arr[-1, 1], 0])

        # Solve the TLA for x, Tm for the *next* step.
        new_vals = tla.get_history(np.array([rs, next_rs]),
                                   init_cond=init_cond_TLA,
                                   f_H_ion=f_H_ion,
                                   f_H_exc=f_exc,
                                   f_heating=f_heat,
                                   injection_rate=rate_func_eng_unclustered,
                                   reion_switch=reion_switch,
                                   reion_rs=reion_rs,
                                   photoion_rate_func=photoion_rate_func,
                                   photoheat_rate_func=photoheat_rate_func,
                                   xe_reion_func=xe_reion_func,
                                   helium_TLA=helium_TLA,
                                   f_He_ion=f_He_ion,
                                   mxstep=mxstep,
                                   rtol=rtol)

        #####################################################################
        #####################################################################
        # Photon Cooling Transfer Functions                                 #
        #####################################################################
        #####################################################################

        # Get the transfer functions for this step.
        if not backreaction:
            # Interpolate using the baseline solution.
            xHII_to_interp = phys.xHII_std(rs)
            xHeII_to_interp = phys.xHeII_std(rs)
        else:
            # Interpolate using the current xHII, xHeII values.
            xHII_to_interp = x_arr[-1, 0]
            xHeII_to_interp = x_arr[-1, 1]

        highengphot_tf, lowengphot_tf, lowengelec_tf, highengdep_arr = (get_tf(
            rs,
            xHII_to_interp,
            xHeII_to_interp,
            dlnz,
            coarsen_factor=coarsen_factor))

        # Get the spectra for the next step by applying the
        # transfer functions.
        highengdep_at_rs = np.dot(np.swapaxes(highengdep_arr, 0, 1),
                                  out_highengphot_specs[-1].N)

        highengphot_spec_at_rs = highengphot_tf.sum_specs(
            out_highengphot_specs[-1])

        lowengphot_spec_at_rs = lowengphot_tf.sum_specs(
            out_highengphot_specs[-1])

        lowengelec_spec_at_rs = lowengelec_tf.sum_specs(
            out_highengphot_specs[-1])

        highengphot_spec_at_rs.rs = next_rs
        lowengphot_spec_at_rs.rs = next_rs
        lowengelec_spec_at_rs.rs = next_rs

        if next_rs > end_rs:
            # Only save if next_rs < end_rs, since these are the x, Tm
            # values for the next redshift.

            # Save the x, Tm data for the next step in x_arr and Tm_arr.
            Tm_arr = np.append(Tm_arr, new_vals[-1, 0])

            if helium_TLA:
                # Append the calculated xHe to x_arr.
                x_arr = np.append(x_arr, [[new_vals[-1, 1], new_vals[-1, 2]]],
                                  axis=0)
            else:
                # Append the baseline solution value.
                x_arr = np.append(x_arr,
                                  [[new_vals[-1, 1],
                                    phys.xHeII_std(next_rs)]],
                                  axis=0)

        # Re-define existing variables.
        rs = next_rs
        dt = dlnz * coarsen_factor / phys.hubble(rs)

    #########################################################################
    #########################################################################
    # END OF LOOP! END OF LOOP!                                             #
    #########################################################################
    #########################################################################

    if use_tqdm:
        pbar.close()

    f_to_return = (f_low, f_high)

    # Some processing to get the data into presentable shape.
    f_low_dict = {
        'H ion': f_low[:, 0],
        'He ion': f_low[:, 1],
        'exc': f_low[:, 2],
        'heat': f_low[:, 3],
        'cont': f_low[:, 4]
    }
    f_high_dict = {
        'H ion': f_high[:, 0],
        'He ion': f_high[:, 1],
        'exc': f_high[:, 2],
        'heat': f_high[:, 3],
        'cont': f_high[:, 4]
    }

    f = {'low': f_low_dict, 'high': f_high_dict}

    data = {
        'rs': out_highengphot_specs.rs,
        'x': x_arr,
        'Tm': Tm_arr,
        'highengphot': out_highengphot_specs,
        'lowengphot': out_lowengphot_specs,
        'lowengelec': out_lowengelec_specs,
        'f': f
    }

    return data
Exemple #18
0
def get_pppc_spec(mDM, eng, pri, sec, decay=False):
    """ Returns the PPPC4DMID spectrum. 

    This is the secondary spectrum to e+e-/photons normalized to one annihilation or decay event to the species specified in ``pri``. These results include electroweak corrections. The full list of allowed channels is: 

    - :math:`\\delta`\ -function injections: ``elec_delta, phot_delta``
    - Leptons: ``e_L, e_R, e, mu_L, mu_R, mu, tau_L, tau_R, tau``
    - Quarks:  ``q, c, b, t``
    - Gauge bosons: ``gamma, g, W_L, W_T, W, Z_L, Z_T, Z``
    - Higgs: ``h``

    ``elec_delta`` and ``phot_delta`` assumes annihilation or decay to two electrons and photons respectively with no EW corrections or ISR/FSR. 

    Variables with subscripts, e.g. ``e_L``, correspond to particles with different polarizations. These polarizations are suitably averaged to obtain the spectra returned in their corresponding variables without subscripts, e.g. ``e``. 

    Parameters
    ----------
    mDM : float
        The mass of the annihilating/decaying dark matter particle (in eV). 
    eng : ndarray
        The energy abscissa for the output spectrum (in eV). 
    pri : string
        One of the available channels (see above). 
    sec : {'elec', 'phot'}
        The secondary spectrum to obtain. 
    decay : bool, optional
        If ``True``, returns the result for decays.

    Returns
    -------
    Spectrum
        Output :class:`.Spectrum` object, ``spec_type == 'dNdE'``.
    
    """

    if decay:
        # Primary energies is for 1 GeV decay = 0.5 GeV annihilation.
        _mDM = mDM / 2.
    else:
        _mDM = mDM

    if _mDM < mass_threshold[pri]:
        # This avoids the absurd situation where mDM is less than the
        # threshold but we get a nonzero spectrum due to interpolation.
        raise ValueError(
            'mDM is below the threshold to produce pri particles.')

    if pri == 'elec_delta':
        # Exact kinetic energy of each electron.
        if not decay:
            eng_elec = mDM - phys.me
        else:
            eng_elec = (mDM - 2 * phys.me) / 2
        # Find the correct bin in eleceng.
        eng_to_inj = eng[eng < eng_elec][-1]
        # Place 2*eng_elec worth of electrons into that bin. Use
        # rebinning to accomplish this.

        if sec == 'elec':
            return rebin_N_arr(np.array([2 * eng_elec / eng_to_inj]),
                               np.array([eng_to_inj]), eng)

        elif sec == 'phot':
            return Spectrum(eng, np.zeros_like(eng), spec_type='dNdE')

        else:
            raise ValueError('invalid sec.')

    if pri == 'phot_delta':
        # Exact kinetic energy of each photon.
        if not decay:
            eng_phot = mDM
        else:
            eng_phot = mDM / 2
        # Find the correct bin in photeng.
        eng_to_inj = eng[eng < eng_phot][-1]
        # Place 2*eng_phot worth of photons into that bin. Use
        # rebinning to accomplish this.
        if sec == 'elec':
            return Spectrum(eng, np.zeros_like(eng), spec_type='dNdE')

        elif sec == 'phot':
            return rebin_N_arr(np.array([2 * eng_phot / eng_to_inj]),
                               np.array([eng_to_inj]), eng)

        else:
            raise ValueError('invalid sec.')

    log10x = np.log10(eng / _mDM)

    # Refine the binning so that the spectrum is accurate.
    # Do this by checking that in the relevant range, there are at
    # least 50,000 bins. If not, double (unless an absurd number
    # of bins already).

    if (log10x[(log10x < 1) & (log10x > 1e-9)].size > 0
            and log10x.size < 500000):
        while log10x[(log10x < 1) & (log10x > 1e-9)].size < 50000:
            log10x = np.interp(np.arange(0, log10x.size - 0.5, 0.5),
                               np.arange(log10x.size), log10x)
    # Get the interpolator.
    dlNdlxIEW_interp = load_data('pppc')

    # Get the spectrum from the interpolator.
    dN_dlog10x = 10**dlNdlxIEW_interp[sec][pri].get_val(_mDM / 1e9, log10x)

    # Recall that dN/dE = dN/dlog10x * dlog10x/dE
    x = 10**log10x
    spec = Spectrum(x * _mDM,
                    dN_dlog10x / (x * _mDM * np.log(10)),
                    spec_type='dNdE')

    # Rebin down to the original binning.

    # The highest bin of spec.eng should be the same as eng[-1], based on
    # the interpolation strategy above. However, sometimes a floating point
    # error is picked up. We'll get rid of this so that rebin doesn't
    # complain.
    spec.eng[-1] = eng[-1]
    spec.rebin(eng)

    return spec