cmlm.ctable_tools

Tools for tabulated function data of arbitrary dimension (via pandas DataFrames).

Functions

check_tabfunc_integrity(ctable[, ...])

Verify DataFrame can be a valid TabulatedFunction for interpolation.

convert_chemtable_units(ctable[, conversion])

Find selected variables in a table and convert units between CGS and MKS.

interpolate_axis(ctable, axis_name, new_grid)

Interpolates one axis of a DataFrame/TabulatedFunction onto a new grid.

is_strictly_increasing(array)

Return true if array is strictly monotonically increasing.

main()

print_chemtable(df[, model_name])

read_chemtable_binary(filename[, tformat, ...])

slice_table(ctable[, slice_vars, ...])

write_chemtable_binary(filename, ctable, ...)

Classes

TabulatedFunction(table[, model_name, ...])

class cmlm.ctable_tools.TabulatedFunction(table, model_name=None, verbose=0, tformat='Pele', Ndim=None, Dimnames=None, index=None, columns=None, dtype=None, copy=None)

Bases: DataFrame

getDimNames()
getDimSizes()
getMatrixData(var)
getNdim()
interpolate(var, vals=None, method='linear', **kwargs)

Fill NaN values using an interpolation method.

Please note that only method='linear' is supported for DataFrame/Series with a MultiIndex.

Parameters:
  • method (str, default 'linear') –

    Interpolation technique to use. One of:

    • ’linear’: Ignore the index and treat the values as equally spaced. This is the only method supported on MultiIndexes.

    • ’time’: Works on daily and higher resolution data to interpolate given length of interval.

    • ’index’, ‘values’: use the actual numerical values of the index.

    • ’pad’: Fill in NaNs using existing values.

    • ’nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘barycentric’, ‘polynomial’: Passed to scipy.interpolate.interp1d, whereas ‘spline’ is passed to scipy.interpolate.UnivariateSpline. These methods use the numerical values of the index. Both ‘polynomial’ and ‘spline’ require that you also specify an order (int), e.g. df.interpolate(method='polynomial', order=5). Note that, slinear method in Pandas refers to the Scipy first order spline instead of Pandas first order spline.

    • ’krogh’, ‘piecewise_polynomial’, ‘spline’, ‘pchip’, ‘akima’, ‘cubicspline’: Wrappers around the SciPy interpolation methods of similar names. See Notes.

    • ’from_derivatives’: Refers to scipy.interpolate.BPoly.from_derivatives.

  • axis ({{0 or 'index', 1 or 'columns', None}}, default None) – Axis to interpolate along. For Series this parameter is unused and defaults to 0.

  • limit (int, optional) – Maximum number of consecutive NaNs to fill. Must be greater than 0.

  • inplace (bool, default False) – Update the data in place if possible.

  • limit_direction ({{'forward', 'backward', 'both'}}, Optional) –

    Consecutive NaNs will be filled in this direction.

    If limit is specified:
    • If ‘method’ is ‘pad’ or ‘ffill’, ‘limit_direction’ must be ‘forward’.

    • If ‘method’ is ‘backfill’ or ‘bfill’, ‘limit_direction’ must be ‘backwards’.

    If ‘limit’ is not specified:
    • If ‘method’ is ‘backfill’ or ‘bfill’, the default is ‘backward’

    • else the default is ‘forward’

    raises ValueError if limit_direction is ‘forward’ or ‘both’ and

    method is ‘backfill’ or ‘bfill’.

    raises ValueError if limit_direction is ‘backward’ or ‘both’ and

    method is ‘pad’ or ‘ffill’.

  • limit_area ({{None, ‘inside’, ‘outside’}}, default None) –

    If limit is specified, consecutive NaNs will be filled with this restriction.

    • None: No fill restriction.

    • ’inside’: Only fill NaNs surrounded by valid values (interpolate).

    • ’outside’: Only fill NaNs outside valid values (extrapolate).

  • downcast (optional, 'infer' or None, defaults to None) –

    Downcast dtypes if possible.

    Deprecated since version 2.1.0.

  • **kwargs (optional) – Keyword arguments to pass on to the interpolating function.

Returns:

Returns the same object type as the caller, interpolated at some or all NaN values or None if inplace=True.

Return type:

Series or DataFrame or None

See also

fillna

Fill missing values using different methods.

scipy.interpolate.Akima1DInterpolator

Piecewise cubic polynomials (Akima interpolator).

scipy.interpolate.BPoly.from_derivatives

Piecewise polynomial in the Bernstein basis.

scipy.interpolate.interp1d

Interpolate a 1-D function.

scipy.interpolate.KroghInterpolator

Interpolate polynomial (Krogh interpolator).

scipy.interpolate.PchipInterpolator

PCHIP 1-d monotonic cubic interpolation.

scipy.interpolate.CubicSpline

Cubic spline data interpolator.

Notes

The ‘krogh’, ‘piecewise_polynomial’, ‘spline’, ‘pchip’ and ‘akima’ methods are wrappers around the respective SciPy implementations of similar names. These use the actual numerical values of the index. For more information on their behavior, see the SciPy documentation.

Examples

Filling in NaN in a Series via linear interpolation.

>>> s = pd.Series([0, 1, np.nan, 3])
>>> s
0    0.0
1    1.0
2    NaN
3    3.0
dtype: float64
>>> s.interpolate()
0    0.0
1    1.0
2    2.0
3    3.0
dtype: float64

Filling in NaN in a Series via polynomial interpolation or splines: Both ‘polynomial’ and ‘spline’ methods require that you also specify an order (int).

>>> s = pd.Series([0, 2, np.nan, 8])
>>> s.interpolate(method='polynomial', order=2)
0    0.000000
1    2.000000
2    4.666667
3    8.000000
dtype: float64

Fill the DataFrame forward (that is, going down) along each column using linear interpolation.

Note how the last entry in column ‘a’ is interpolated differently, because there is no entry after it to use for interpolation. Note how the first entry in column ‘b’ remains NaN, because there is no entry before it to use for interpolation.

>>> df = pd.DataFrame([(0.0, np.nan, -1.0, 1.0),
...                    (np.nan, 2.0, np.nan, np.nan),
...                    (2.0, 3.0, np.nan, 9.0),
...                    (np.nan, 4.0, -4.0, 16.0)],
...                   columns=list('abcd'))
>>> df
     a    b    c     d
0  0.0  NaN -1.0   1.0
1  NaN  2.0  NaN   NaN
2  2.0  3.0  NaN   9.0
3  NaN  4.0 -4.0  16.0
>>> df.interpolate(method='linear', limit_direction='forward', axis=0)
     a    b    c     d
0  0.0  NaN -1.0   1.0
1  1.0  2.0 -2.0   5.0
2  2.0  3.0 -3.0   9.0
3  2.0  4.0 -4.0  16.0

Using polynomial interpolation.

>>> df['d'].interpolate(method='polynomial', order=2)
0     1.0
1     4.0
2     9.0
3    16.0
Name: d, dtype: float64
is_valid()
cmlm.ctable_tools.check_tabfunc_integrity(ctable, allow_monoindex=False)

Verify DataFrame can be a valid TabulatedFunction for interpolation.

Parameters:
  • ctable (DataFrame) – Table to check integrity of

  • allow_monoindex (Bool) – If true, don’t require DataFrame to MultiIndex

Returns:

is_valid – True indicates table is ready for interpolation

Return type:

bool

cmlm.ctable_tools.convert_chemtable_units(ctable, conversion='mks2cgs')

Find selected variables in a table and convert units between CGS and MKS.

Converts variables with the following names (mks2cgs conversion shown):

  • RHO: kg m-3 -> g cm-3

  • DIFF (actually rhoD): kg s-1 m-1 -> g s-1 cm-1

  • VISC (dynamic): kg s-1 m-1 -> g s-1 cm-1

  • WBAR (molecular mass): kg kmol-1 -> g mol-1

  • CP* (heat capacity): J kg-1 K-1 -> erg g-1 K-1

  • SRC_* (species source terms): kg m-3 s-1 -> g cm-3 s-1

  • T: K -> K

  • X (length): m -> cm

  • VEL (velocity) m s-1 -> cm s-1

Parameters:
  • ctable (TabulatedFunction, pd.DataFrame) – tabular data to convert (conversion happens in place)

  • conversion (str ('mks2cgs' or 'cgs2mks', default 'mks2cgs')) – unit conversion to perform

Returns:

ierr – conversion happens in place, returns 0 if success

Return type:

int

cmlm.ctable_tools.interpolate_axis(ctable, axis_name, new_grid, grid_var_name=None, remove_nonmonotonic=False, verbose=0)

Interpolates one axis of a DataFrame/TabulatedFunction onto a new grid.

We require that all axes to the right of the specified axis already be on regular cartesian grids (they will be interpolated simultaneously). The interpolation axis itself and all axes to the left may be on irregular grids. After interpolation, the interpolation axis will be regular, but the axes to the left won’t be changed.

Parameters:
  • ctable (pandas DataFrame, TabulatedFunction) – Table that we will interpolate onto new_grid

  • axis_name (str) – Name of table axis that will be interpolated

  • new_grid (1d array of floats (or similar object) sorted in increasing order) – Modified grid values on which to interpolate (must be strictly ascending)

  • verbose (int) – Verbosity level of logging to terminal (0: none, 1: one statement, 2: statement for each group)

  • grid_var_name (str, default None) – If set, use the specified column of the table to interpolate/redefine the specified axis

  • remove_nonmonotonic (Bool, (default False)) – If True, ignore nonmonotonic entries in the current grid; otherwise an error will be raised for non-monotonic values in the current grid

Returns:

newtable – Table with specified axis_name interpolated onto the new_grid

Return type:

pandas DataFrame

cmlm.ctable_tools.is_strictly_increasing(array)

Return true if array is strictly monotonically increasing.

cmlm.ctable_tools.main()
cmlm.ctable_tools.print_chemtable(df, model_name=None)
cmlm.ctable_tools.read_chemtable_binary(filename, tformat='Pele', Ndim=None, Dimnames=None, verbose=0)
cmlm.ctable_tools.slice_table(ctable, slice_vars=None, slice_vals=None, slice_pairs=None)
cmlm.ctable_tools.write_chemtable_binary(filename, ctable, tablename, tformat='Pele')