Пример #1
0
def osc_nitr(c, h, o, n):
    """
    This function calculates a carbon oxidation state (OSC from Kroll et al., 2011). This is calculated by the formula:
        OSC = 2*(O/C) - (H/C) - 5*(N/C).
    :param c: (int/float list) Numerical list with the number of carbons in the molecules for which OSC will be
        calculated.
    :param h: (int/float list) Numerical list with the number of hydrogens in the molecules for which OSC will be
        calculated.
    :param o: (int/float list) Numerical list with the number of oxygens in the molecules for which OSC will be
        calculated.
    :param n: (int/float list) Numerical list with the number of nitrogens in the molecules for which OSC will be
        calculated.
    :return: ox_states_nitr: (float list) Oxidation states, accounting for nitrogen groups (assumed nitrates)
    """
    # Verify that parameters are lists of numerical values (numpy ndarray or python list) to prevent error in
    # subsequent loops
    if (_check.check_ls(ls=c, nt_flag=True) and _check.check_np_array(arr=c, nt_flag=True)) or \
            (_check.check_ls(ls=h, nt_flag=True) and _check.check_np_array(arr=h)) or \
            (_check.check_ls(ls=o, nt_flag=True) and _check.check_np_array(arr=o)) or \
            (_check.check_ls(ls=n, nt_flag=True) and _check.check_np_array(arr=n)):
        main_module, main_fn, main_lineno = _check.parent_fn_mod_3step()
        calling_module, calling_fn, calling_lineno = _check.parent_fn_mod_2step(
        )
        print('On line %i in function %s of module %s' %
              (main_lineno, main_fn, main_module))
        print('     Error on line %i in module %s' %
              (calling_lineno, calling_module))
        print('         Invalid input for function %s' % calling_fn)
        sys.exit('ERROR: Either a list or np.array is required')
    _check.check_eq_ls_len(list_ls=[c, h, o, n])
    _check.check_numeric(values=c)
    _check.check_numeric(values=h)
    _check.check_numeric(values=o)
    _check.check_numeric(values=n)

    ox_states_nitr = []
    for nc, nh, no, nn in zip(c, h, o, n):
        if nc == 0:
            ox_states_nitr.append(np.nan)
        else:
            ox_states_nitr.append(
                float((2 * (no / nc) - (nh / nc) - 5 * (nn / nc))))

    return ox_states_nitr
Пример #2
0
def h_to_c(h, c):
    """
    This function calculates a simple O/C ratio for a list of oxygen and carbon numbers. len(h) and len(c) must
        be equal.
    :param h: (int/float list) A list of numeric values representing the number of hydrogens for a list of molecules.
        Can be float or int, but float should only be used if the calculation is for a bulk average. Otherwise, a float
        value doesn't make sense for a single molecule.
    :param c: (int/float list) A list of numeric values representing the number of hydrogens for a list of molecules.
        Can be float or int, but float should only be used if the calculation is for a bulk average. Otherwise, a float
        value doesn't make sense for a single molecule.
    :return: hc_ratios: (float list) A list of values that are the index-to-index ratios of the values in h and c
    """
    # Check to make sure that input lists are numeric and the same length to prevent errors during processing.
    if (_check.check_ls(ls=h, nt_flag=True) and _check.check_np_array(arr=h, nt_flag=True)) or \
            (_check.check_ls(ls=c, nt_flag=True) and _check.check_np_array(arr=c)):
        main_module, main_fn, main_lineno = _check.parent_fn_mod_3step()
        calling_module, calling_fn, calling_lineno = _check.parent_fn_mod_2step(
        )
        print('On line %i in function %s of module %s' %
              (main_lineno, main_fn, main_module))
        print('     Error on line %i in module %s' %
              (calling_lineno, calling_module))
        print('         Invalid input for function %s' % calling_fn)
        sys.exit('ERROR: Either a list or np.array is required')
    _check.check_eq_ls_len(list_ls=[h, c])
    _check.check_numeric(values=h)
    _check.check_numeric(values=c)

    hc_ratios = []
    for no, nc in zip(h, c):
        if nc == 0:
            hc_ratios.append(np.nan)
        else:
            hc_ratios.append(float(no / nc))

    return hc_ratios
Пример #3
0
def concat_df_lists(df_ls1, df_ls2, axis=0, join='inner'):
    """
    This function takes two lists of pandas DataFrames and concatenates them. Options allow user to specify the axis
        along which to concatenate as well as the pandas join method (e.g., 'inner', 'outer'). Where available,
        inherent pandas DataFrame functionality is employed (e.g., transpose, axis selection, join method, etc).
        Parameter choice should be in line with the requirements of the pandas library and associated functions,
        and therefore the same convention is used for parameters axis and join. DataFrames are concatenated pairwise;
        that is, df_ls1[i] is concatenated with df_ls2[i].

        Specific to the pygaero package, this function is included for the joining of Tmax and elemental analysis
        DataFrames in preparation for using clustering or classification algorithms from scikit-learn

    Parameters:
    :param df_ls1: A list of DataFrames on which to concatenate df_ls2
    :param df_ls2: A list of DataFrames to concatenate onto the corresponding DataFrames
    :param axis: The axis along which the DataFrames will be concatenated. axis=1 for column-wise, 0 for row-wise
        (standard pandas DataFrame syntax). Example: if axis=0, DataFrames will be concatenated in the row dimension
        (i.e., stacked vertically; will require same # of columns).  If axis=1, will be concatenated in the
        column dimension (i.e., side-by-side)
    :param join: Allows user to specify the join parameter for pandas.concat(). Must be compatible with choices
        available within the pandas package.
    :return: df_list: A list of DataFrames where elements are DataFrames from list 1 concatenated onto the corresponding
        DataFrame from list 2
    """
    # Check data types to prevent errors during processing.
    _check.check_ls(ls=df_ls1)
    _check.check_ls(ls=df_ls2)
    _check.check_dfs(values=df_ls1)
    _check.check_dfs(values=df_ls2)
    _check.check_eq_ls_len(list_ls=[df_ls1, df_ls2])
    _check.check_numeric(values=[axis])
    _check.param_exists_in_set(value=axis, val_set=[0, 1])
    _check.check_string(values=[join])
    _check.param_exists_in_set(value=join, val_set=['inner', 'outer'])

    # Initialize return list for concatenated DataFrames
    df_ls_concat = []
    # Check row or column lengths of lists to make sure they're the same.  If not, tell user, but try to proceed.
    if axis == 0:
        for df1, df2 in zip(df_ls1, df_ls2):
            if df1.shape[1] != df2.shape[1]:
                print(
                    'WARNING: You chose concatenation in row dimension (i.e., vertical stacking) with '
                    'parameter axis=0,\n but some DataFrame pairs have different numbers of columns.  Proceeding...'
                )
            else:
                pass
    elif axis == 1:
        for df1, df2 in zip(df_ls1, df_ls2):
            if df1.shape[0] != df2.shape[0]:
                print(
                    'WARNING: You chose to concatenate in column dimension (side-by-side) with axis=1, but'
                    'some DataFrame pairs have different number of rows.  Proceeding...'
                )
    else:
        print('ERROR: Parameter axis must be set to 0 or 1')
        sys.exit()

    for df1, df2 in zip(df_ls1, df_ls2):
        df_ls_concat.append(pd.concat([df1, df2], axis=axis, join=join))

    return df_ls_concat