0

I found this useful starting point for counting raising edges by positive signs: Efficiently detect sign-changes in python

def crossings_nonzero_all(data):
    pos = data > 0
    npos = ~pos
    return ((pos[:-1] & npos[1:]) | (npos[:-1] & pos[1:])).nonzero()[0]

But, I rather find only those indices that have positive followed by negative and discount those indices that have positive, followed by zeros, and then positive signs again.

[0, 1, 2, -1, 0, 1, 2, 0] should pick up only one case because there is a change from positive 2 to negative -1. [2, 0] does not count because there are no negative sign before zero.

Could you help me to achieve this?

Added better data example:

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2102.4937, 2359.4937, 2359.4937, 2615.4937, 2872.4937, 3129.4937, 3385.4937, 3642.4937, 4155.4937, 4412.4937, 4669.4937, 4925.4937, 5439.4937, 5695.4937, 6209.4937, 6465.4937, 6979.4937, 7492.4937, 8006.4937, 8519.4937, 9032.4937, 9546.4937, 10059.4937, 10829.4937, 11342.4937, 11856.4937, 12626.4937, 13396.4937, 13909.4937, 14679.4937, 15449.4937, 15963.4937, 16733.4937, 17503.4937, 18273.4937, 19043.4937, 19813.4937, 20583.4937, 21353.4937, 22123.4937, 22893.4937, 23920.4937, 24690.4937, 25460.4937, 26230.4937, 27000.4937, 27770.4937, 28540.4937, 29310.4937, 30080.4937, 30593.4937, 31363.4937, 31877.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32561.4937, 32133.4937, 31363.4937, 30593.4937, 30080.4937, 29567.4937, 28797.4937, 28283.4937, 27770.4937, 27257.4937, 26743.4937, 26230.4937, 25716.4937, 25203.4937, 24690.4937, 24176.4937, 23663.4937, 23150.4937, 22636.4937, 22123.4937, 21610.4937, 21096.4937, 20583.4937, 20070.4937, 19556.4937, 19043.4937, 18786.4937, 18273.4937, 17759.4937, 17246.4937, 16733.4937, 16219.4937, 15963.4937, 15449.4937, 14936.4937, 14423.4937, 13909.4937, 13653.4937, 12882.4937, 12626.4937, 12112.4937, 11599.4937, 11086.4937, 10829.4937, 10316.4937, 9802.4937, 9289.4937, 9032.4937, 8519.4937, 8262.4937, 7749.4937, 7236.4937, 6979.4937, 6465.4937, 5952.4937, 5695.4937, 5182.4937, 4925.4937, 4412.4937, 4155.4937, 3642.4937, 3385.4937, 3129.4937, 2615.4937, 2359.4937, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2260.5063, 2516.5063, 2773.5063, 3030.5063, 3286.5063, 3543.5063, 3800.5063, 4056.5063, 4313.5063, 4570.5063, 4826.5063, 5083.5063, 5340.5063, 5596.5063, 5853.5063, 5853.5063, 6110.5063, 6366.5063, 6623.5063, 6880.5063, 6880.5063, 7137.5063, 7393.5063, 7393.5063, 7650.5063, 7907.5063, 7907.5063, 8163.5063, 8420.5063, 8420.5063, 8677.5063, 8677.5063, 8933.5063, 9190.5063, 9190.5063, 9447.5063, 9447.5063, 9703.5063, 9703.5063, 9703.5063, 9960.5063, 10217.5063, 10217.5063, 10217.5063, 10217.5063, 10473.5063, 10473.5063, 10473.5063, 10473.5063, 10730.5063, 10730.5063, 10730.5063, 10987.5063, 10987.5063, 10987.5063, 10987.5063, 11243.5063, 11243.5063, 11243.5063, 11243.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11757.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11500.5063, 11243.5063, 11243.5063, 11243.5063, 11243.5063, 11243.5063, 11243.5063, 11243.5063, 10987.5063, 10987.5063, 10987.5063, 10987.5063, 10987.5063, 10987.5063, 10730.5063, 10730.5063, 10730.5063, 10730.5063, 10473.5063, 10730.5063, 10473.5063, 10473.5063, 10473.5063, 10217.5063, 10217.5063, 10217.5063, 10217.5063, 10217.5063, 9960.5063, 9960.5063, 9960.5063, 9960.5063, 9703.5063, 9703.5063, 9703.5063, 9703.5063, 9447.5063, 9447.5063, 9447.5063, 9447.5063, 9190.5063, 9190.5063, 9190.5063, 9190.5063, 9190.5063, 8933.5063, 8933.5063, 8677.5063, 8677.5063, 8677.5063, 8420.5063, 8420.5063, 8420.5063, 8420.5063, 8163.5063, 8163.5063, 8163.5063, 7907.5063, 8163.5063, 7907.5063, 7907.5063, 7907.5063, 7650.5063, 7650.5063, 7650.5063, 7393.5063, 7650.5063, 7393.5063, 7393.5063, 7393.5063, 7137.5063, 7137.5063, 7137.5063, 7137.5063, 6880.5063, 6880.5063, 6880.5063, 6623.5063, 6623.5063, 6623.5063, 6623.5063, 6366.5063, 6366.5063, 6366.5063, 6366.5063, 6366.5063, 6110.5063, 6110.5063, 6110.5063, 5853.5063, 5853.5063, 5853.5063, 5853.5063, 5853.5063, 5596.5063, 5596.5063, 5596.5063, 5340.5063, 5340.5063, 5340.5063, 5340.5063, 5340.5063, 5083.5063, 5083.5063, 5083.5063, 4826.5063, 4826.5063, 4826.5063, 4826.5063, 4826.5063, 4826.5063, 4570.5063, 4570.5063, 4570.5063, 4570.5063, 4570.5063, 4313.5063, 4313.5063, 4313.5063, 4313.5063, 4056.5063, 4056.5063, 4056.5063, 4056.5063, 4056.5063, 4056.5063, 3800.5063, 3800.5063, 3800.5063, 3800.5063, 3800.5063, 3800.5063, 3543.5063, 3543.5063, 3543.5063, 3543.5063, 3543.5063, 3286.5063, 3286.5063, 3286.5063, 3286.5063, 3286.5063, 3286.5063, 3286.5063, 3030.5063, 3030.5063, 3030.5063, 3030.5063, 3030.5063, 2773.5063, 2773.5063, 2773.5063, 2773.5063, 2773.5063, 2773.5063, 2773.5063, 2773.5063, 2773.5063, 2516.5063, 2516.5063, 2516.5063, 2516.5063, 2516.5063, 2516.5063, 2516.5063, 2260.5063, 2260.5063, 2260.5063, 2260.5063, 2260.5063, 2260.5063, 2260.5063, 0.0, 0.0, 2260.5063, 2260.5063, 0.0, 0.0]

This should give indices from [0.0, 2102.4937] and [0.0, 2260.5063] but not from [0.0, 2296.8233] because there are no declining values, just raising in the last sample.

MarkokraM
  • 980
  • 1
  • 12
  • 26

1 Answers1

0

You could call np.sign followed by np.diff, for example:

a = np.array([0, 1, 2, -1, 0, 1, 2, 0])
idx, = np.where(np.diff(np.sign(a)) == -2)
# idx == array([2]), corresponding to sign difference of -2 = sign(-1) - sign(2). 
hilberts_drinking_problem
  • 11,322
  • 3
  • 22
  • 51
  • Thanks. I rather have a more general form independent of values, just finding if they are rising or declining. I did better job with a double diff and comparing consequencing values on indices (n, n+1): d = np.diff(np.sign(data), n=2); np.where((d[1:] == -1) & (d[:-1] == 1))[0] but it is still not totally correct. Algo should not count cases where there are only positive signs before zero. Please, see my OP better sample data, if you wish to think more and edit your answer. – MarkokraM Jan 02 '22 at 19:46