pyscal.utils.monotonicity

Monotonocity support functions for pyscal

class pyscal.utils.monotonicity.MonotonicitySpec[source]

Specification of monotonicity for a vector of values

allowzero: bool

If True, consecutive zeros will be allowes in an otherwise strictly monotonic column. Optional parameter.

lower: float

Values will be clipped at lower limit, and non-strict monotonicity is allowed at limit. Optional parameter.

sign: int

Value of +1 dictates strictly increasing, value of -1 dictates scrictly decreasing. Required parameter.

upper: float

Values will be clipped at upper limit, and non-strict monotonicity is allowed at limit. Optional parameter.

pyscal.utils.monotonicity.check_almost_monotone(series, digits, sign)[source]

Raise a ValueError if a series is not sufficiently close to constant or monotone in a certain direction.

Parameters:
  • series (Series) – Vector of numbers

  • digits (int) –

  • sign (int) – direction. >0 means positive

Return type:

None

pyscal.utils.monotonicity.check_limits(series, monotonicity, colname='')[source]

Check a series whether it obeys numerical limits. Equivalence to limits is allowed.

Exceptions will be raised in case of error. Nothing is returned when everything is ok.

Parameters:
  • series (Union[List[float], Series, ndarray]) – Vector of numbers to check

  • monotonicity (MonotonicitySpec) –

  • colname (str) – Optional string for a column name that will be included in any error message.

Return type:

None

pyscal.utils.monotonicity.clip_accumulate(series, monotonicity)[source]

Modify a series (vector of numbers) for non-strict monotonicity, and optionally clip at lower and upper limits.

Parameters:
  • series (Union[List[float], Series, ndarray]) – Vector of numbers to modify

  • monotonicity (MonotonicitySpec) –

Return type:

ndarray

Returns:

np.array, copy of original.

pyscal.utils.monotonicity.modify_dframe_monotonicity(dframe, monotonicity, digits)[source]

Modify a dataframe for monotonicity.

Columns in the dataframe are modified in-place.

Number intervals to consider when enforcing monotonicity:

<value>                          <orig>    <fixed>
<lower limit>                     0.00      0.00
<values smaller than accuracy>    0.0002    0.00
<accuracy limit>                  0.01      0.01
<potential constants>             0.010001  0.02
<allow ups/downs below accuracy>  0.0100001 0.03
                                  0.01      0.04
<upper limit minus accuracy>      0.99      0.99
<values too close to upper limit> 0.999     1.00
<overshooting values>             1.0001    1.00
<upper limit>                     1.00      1.00

Values close to upper or lower limits (if limits are supplied), but which deviate from the limit by less than the requested accuracy are allowed, and will be shifted to the limits.

Only strict monotonicity is supported. Non-strict monotonicity is only allowed at upper and lower limit, or for all-zero vectors if that option is activated.

For non-strict monotocity, see the function clip_accumulate()

Parameters:
  • dframe (DataFrame) – Data to modify.

  • monotonicity (Dict[str, MonotonicitySpec]) – Keys are column names

  • digits (int) – Number of digits to ensure monotonicity for.

Return type:

DataFrame

pyscal.utils.monotonicity.rows_to_be_fixed(series, monotonicity, digits)[source]

Compute boolean array of rows that must be modified

Parameters:
  • series (Series) –

  • monotonicity (MonotonicitySpec) –

  • digits (int) – Accuracy required, how many digits that are to be printed, and to which we should relate constancy to.

Return type:

Series

Returns:

boolean series.

pyscal.utils.monotonicity.validate_monotonicity_arg(monotonicity, dframe_colnames)[source]

Validate a dictionary with monotonicity arguments that can be given to df2str().

Will raise ValueError exceptions if anything is wrong.

Parameters:
  • monotonicity (Dict[str, MonotonicitySpec]) – Keys are column names.

  • dframe_colnames (List[str]) – Names of column names in dframes. Used in error messages.

Return type:

None