user20759431
user20759431

Reputation:

how to count the cycle count using python

I am new to the python, I have huge cycle data I need to count the increasing cycle peaks where cycle lower end is less than 5 and cycle upper end in between range in between 21 and 22 it has to detect as a cycle I used this code as reference Counting loops/cycles of numbers in Python

code I used is

cycle= [0, 14,  9,  0,  0,  7,  0,  0, 12, 16, 15, 11,  7, 20, 24, 13, 13,
       14, 19, 13, 12, 10,  7,  3,  3,  3, 25, 14, 14, 14,  7, 24, 20, 20,
       21, 20, 20, 20, 20, 20, 21, 16, 11, 11, 18, 22, 22, 20, 19, 19, 18,
       15, 20, 23, 21, 23, 24, 15, 16, 19, 25, 24,  0, 20, 23, 24, 23, 22,
       21, 23, 25, 28, 24, 23, 23, 17,  7, 11, 21, 25, 25, 25, 25, 25, 25,
       15, 13,  9,  0, 21, 10, 18, 25, 25, 26, 23, 25, 23, 25, 27, 25, 12,
        0,  0,  0, 19, 22, 24, 25, 25, 24, 24, 23, 23, 16, 19, 23, 24, 24,
       17,  8,  0,  9,  7, 11, 18, 20, 23, 23, 24, 25, 25, 25, 17, 24, 24,
       25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 16,  0,  7, 14, 21, 26, 26,
       27, 28, 27, 15, 25, 26, 25, 25, 25, 24, 25, 25, 24, 26, 26, 26, 23]
sub_lists = np.split(cycle, np.where(np.diff(cycle) < 0)[0] + 1)
id_count = 0
id_list = []
for unit in sub_lists:
    if min(unit) >0 and max(unit) < 25 and len(set(unit)) > 1:
        id_count += 1
        id_list.append(unit)

but getting out as 12 cycles since there is only one increasing cycle in the data this cycle is not counting

Upvotes: 2

Views: 879

Answers (1)

liansheng
liansheng

Reputation: 308

you code count a value in (5, 25) of list cycle.
if you want get count of increasing cycle and the min of cycle > 5 and max of cycle < 25, you must be split the list of cycle by increasing.

  1. you need split the list of cycle
In [56]: cycle = [0, 14,  9,  0,  0,  7,  0,  0, 12, 16, 15, 11,  7, 20, 24, 13, 13]

In [57]: sub_lists = np.split(cycle, np.where(np.diff(cycle) < 0)[0] + 1)

In [58]: sub_lists
Out[58]:
[array([ 0, 14]),
 array([9]),
 array([0, 0, 7]),
 array([ 0,  0, 12, 16]),
 array([15]),
 array([11]),
 array([ 7, 20, 24]),
 array([13, 13])]
  1. find you want range in sub_lists
In [71]: id_count = 0
    ...: id_list = []
    ...: for unit in sub_lists:
    ...:     if min(unit) > 5 and max(unit) < 25 and len(set(unit)) > 1:
    ...:         id_count += 1
    ...:         id_list.append(unit)
    ...:
    ...:
    ...:

In [72]: id_count
Out[72]: 1

In [73]: id_list
Out[73]: [array([ 7, 20, 24])]

code in py file is :
you need run pip install numpy first.

import numpy as np
cycle = [0, 14,  9,  0,  0,  7,  0,  0, 12, 16, 15, 11,  7, 20, 24, 13, 13]
sub_lists = np.split(cycle, np.where(np.diff(cycle) < 0)[0] + 1)
id_count = 0
id_list = []
for unit in sub_lists:
    if min(unit) > 5 and max(unit) < 25 and len(set(unit)) > 1:
        id_count += 1
        id_list.append(unit)

Upvotes: 2

Related Questions