bulum.stats.negflo module
Bulum implementation of NEGFLO.
NEGFLO redistributes the negative flows in a residual flow series (e.g. downstream gauged flow minus routed upstream flow) by reducing positive flows, so that the negatives can be set to zero without inflating the total volume.
Concepts
- Flow event
A maximal run of consecutive days with flow above the flow limit. Days with missing data (NaN) break events.
- Negative volume
The total of the negative flows between two consecutive events (or before the first event, or after the last).
- Absorbing
An event absorbs a negative volume by reducing each flow’s excess above the flow limit in proportion, i.e. by a factor of
1 - negative volume / total excess. Flows are never reduced below the flow limit. If the event’s total excess is too small, all its flows are set to the flow limit and the remainder is left over.- Unallocated negatives
Negative volume that was not absorbed by any event. It is reported as a negative number per column in
Negflo.unallocated_negatives, so that for every method:sum(result) == sum(residual) - unallocated negatives
i.e. the result totals more than the residual by the unallocated volume.
- Missing data
Missing days (NaN) contribute nothing to any total, break flow events, and remain NaN in every result. Negative volumes are carried across gaps exactly as they are across days of zero flow.
After smoothing, all remaining negative flows are set to zero. Flows between zero and the flow limit that are not part of an event are left unchanged.
Differences from NEGFLO6
This implementation follows the NEGFLO6 documentation and C source, except:
Missing data is excluded rather than treated as a measured flow of zero (which in NEGFLO6 created spurious negative residuals equal to the modelled flow).
The flow limit is applied (SM2-SM5, SM7). NEGFLO6 silently ignored positive flow limits.
Results are not rounded to whole numbers.
SM2 and SM3 smooth an event that is still open at the end of the series. NEGFLO6 skipped smoothing when that was the only event.
SM4’s carry-over is calculated correctly. NEGFLO6 had a typo that reduced each event by the wrong negative total, and reused the previous event’s total for an event still open at the end of the series.
SM6 only clips days outside every segment. NEGFLO6 could instead change them using totals left over from SM2 when the segments did not cover the whole period.
SM7 is a method in its own right using a non-negative flow limit, rather than being triggered by a negative flow limit. An event may absorb negative volumes from both sides, and an event still open at the end of the series is compared like any other. NEGFLO6 dropped one of the two volumes when an event was chosen by both, and never chose an open final event.
References
Specification: https://qldhyd.atlassian.net/wiki/spaces/MET/pages/524386/Negflo
- class Negflo(df_residual: DataFrame | TimeseriesDataframe, flow_limit: float = 0.0, *, segments: Sequence[tuple[Timestamp, Timestamp]] | None = None)
Bases:
objectBulum implementation of NEGFLO.
Each analysis method (
rw1(),cl1(),sm1()…sm7()) works from the residual supplied on construction, independently of any previous call, and returns the result as a new dataframe. Columns are processed independently. See the module documentation for the concepts and the treatment of missing data.- Parameters:
df_residual (pd.DataFrame or TimeseriesDataframe) – Residual flows, one series per column, in time order. Missing data should be NaN. Values are converted to float.
flow_limit (float, default 0.0) – Non-negative flow limit (see
flow_limit).segments (sequence of (start, end), optional) – Inclusive date ranges for
sm6().
- Raises:
ValueError – If flow_limit is negative.
- flow_limit
Flows at or below this limit never absorb negative flow (SM2-SM5, SM7). SM1 and SM6 do not use it.
- Type:
- unallocated_negatives
Negative volume (as a negative number) that the most recent analysis could not redistribute, per column.
- Type:
- df_name
Name of the input TimeseriesDataframe, used to name output files. None for other inputs, including those constructed with
from_flows(), whose files are namedresult.*.- Type:
str or None
- cl1() DataFrame
Set all negative flows to zero.
- Returns:
The clipped residual dataframe.
- Return type:
pd.DataFrame
- classmethod from_flows(df_measured: DataFrame, df_modelled: DataFrame, flow_limit: float = 0.0, *, segments: Sequence[tuple[Timestamp, Timestamp]] | None = None) Negflo
Construct from measured (e.g. downstream gauged) and modelled (e.g. routed upstream) flows, computing the residual as measured - modelled.
The dataframes are aligned on their index; a day is missing in the residual if it is missing (or absent) in either input.
- run_all(folder: str = '.') None
Runs all analyses on the residual, saving each to
folder.Requires an index of dates, for
sm6().
- rw1() DataFrame
The raw residual, unchanged.
- Returns:
The raw residual dataframe.
- Return type:
pd.DataFrame
- sm1() DataFrame
Redistribute negative flows across the whole period.
All negative flows are set to zero and all positive flows are factored by:
1 - abs(Total of the negative flows)/(Total of the positive flows)
This preserves the variability of the flows and the total volume, and is most useful for preserving storage behaviour if the reach flows into a dam. The flow limit is not used (i.e. it is zero).
- Returns:
The smoothed residual dataframe.
- Return type:
pd.DataFrame
- sm2() DataFrame
Redistribute negative flows into the following flow event, with carry-over.
Each flow event (a run of flows above the flow limit) absorbs the negative flows preceding it, reducing the flows’ excess above the flow limit by the factor:
1 - abs(Total of the preceding negative flows)/(Total excess of the event)
If an event is not large enough, its flows are set to the flow limit and the remaining negative flow is carried over to the next event.
This is most useful if the negative flows mainly occur on the rising limb of the hydrograph. With a flow limit of zero, the mean flow is close to the measured mean, but small flow peaks can be eliminated. A higher flow limit preserves these peaks but can severely reduce high flows, giving a ranked flow plot with a notch at the flow limit.
- Returns:
The smoothed residual dataframe.
- Return type:
pd.DataFrame
- sm3() DataFrame
As
sm2(), but negative flow an event cannot absorb is not carried over to the next event.- Returns:
The smoothed residual dataframe.
- Return type:
pd.DataFrame
- sm4() DataFrame
Redistribute negative flows into the preceding flow event, with carry-over.
As
sm2(), except each event absorbs the negative flows following it. Negative flow an event cannot absorb is carried over to the next event. Negative flow before the first event has no preceding event, so it is not redistributed (as in NEGFLO6). This is most useful if the negative flows mainly occur on the falling limb of the hydrograph.- Returns:
The smoothed residual dataframe.
- Return type:
pd.DataFrame
- sm5() DataFrame
As
sm4(), but negative flow an event cannot absorb (or that precedes the first event) is not carried over.- Returns:
The smoothed residual dataframe.
- Return type:
pd.DataFrame
- sm6(*, use_predefined_segments: bool = True, sampling_frequency: DateOffset | None = None, sampling_start_date: Timestamp | None = None) DataFrame
Redistribute negative flows within each segment, as per
sm1().Negative flows outside every segment are set to zero. As with
sm1(), the flow limit is not used.- Parameters:
use_predefined_segments (bool, default True) – Use
segmentsif they exist. Otherwise segments are generated from sampling_frequency and sampling_start_date.sampling_frequency (pd.DateOffset, optional) – Length of generated segments. Defaults to one year.
sampling_start_date (pd.Timestamp, optional) – Start of the first generated segment. Defaults to the start of the data.
- Returns:
The smoothed residual dataframe.
- Return type:
pd.DataFrame
- Raises:
ValueError – If the residual is empty, its index is not sorted, unique dates, or a segment ends before it starts or overlaps another.
See also
- sm7() DataFrame
Redistribute negative flows into the larger adjacent flow event.
The negative flows between two flow events are absorbed (as per
sm2()) by whichever of the two events has the larger total excess above the flow limit; the preceding event wins ties. Negative flows before the first event or after the last are absorbed by that event. Negative flow an event cannot absorb is not carried over.- Returns:
The smoothed residual dataframe.
- Return type:
pd.DataFrame
- to_file(*, out_filename: str | None = None, folder: str | None = None) None
Saves the result of the most recent analysis to a csv file. Missing data is written as an empty value.
- Parameters:
out_filename (str, optional) – Explicit output path. If it has no extension, the extension of the current analysis type is added. If omitted, the filename is derived from
df_nameand the current analysis type extension.folder (str, optional) – Directory in which to place the auto-named file. Ignored when
out_filenameis supplied.