Skip to content

Include first and last recession flows in util_RecessionSegments.m #3

@RY4GIT

Description

@RY4GIT

% find timesteps with decreasing flow
decreasing_flow = Q(2:end)<(Q(1:end-1)+eps);
% start on a non-decreasing point
start_point = find(decreasing_flow==0,1);
decreasing_flow = decreasing_flow(start_point:end);
% find start and end of decreasing sections
flow_change = find(decreasing_flow(1:end-1) ~= decreasing_flow(2:end));
% reshape into x by 2 array (columns = start, end of decrease)
flow_change = flow_change(1:(2*floor(size(flow_change,1)./2)));
flow_change = reshape(flow_change,2,[]).';

Issue Description:

The current code excludes the first and last recession segments of the flow record, when the recession segments are partial (if the timeseries starts during recession, or the timeseries ends during recession).

This is generally fine for long time-series data; I even think discarding first and last recession segments may be more appropriate, because they might have lead some uncertainty. However, it can lead to an issue for short time frames, where you don't wanna miss any recession segments.

I don't think it needs an immediate fix, so I will leave here as an issue documentation to provide information for others who may encounter the same issue.

Proposed Solution:

The following is a Python code snippet that accounts for the first and last recession flows (I've been trying to translate this Matlab code to Python). I apologize that it is not in Matlab.

# Identify timesteps with decreasing flow (same as original)
_decreasing_flow = Q[1:].to_numpy() < (
    Q[:-1].to_numpy() + eps
) 

# The index for the first decreasing flow
# [ADDED: If the flow record starts from decreasing values, set the first starting point as the first "non-decreasing" index, so that it gets detected by np.diff]
if _decreasing_flow[0]: 
    start_point = 0
else:
    start_point = np.where(_decreasing_flow == 0)[0][0]

# Get the decreasing flow section  (same as original)
decreasing_flow = _decreasing_flow[start_point:]

# [ADDED: If the flow record starts from decreasing values, change the first value of decreasing_flow to False, so that it gets detected by np.diff]
if _decreasing_flow[0]:
    decreasing_flow[0] = False

# Find start and end of decreasing sections (same as original)
_flow_change = np.where(np.diff(decreasing_flow) != 0)[0] + start_point

# [ADDED: If the flow is still decreasing at the end of the period, append that to flow change]
if decreasing_flow[-1]:
    _flow_change = np.append(_flow_change, len(Q))

# Detect the flow change  (same as original)
flow_change = _flow_change[: 2 * (len(_flow_change) // 2)].reshape(-1, 2)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions