Matteo Brini
Matteo Brini

Reputation: 183

From for loop and if conditions to Numpy

I've to read a file containing some START and STOP times. The file is shaped as 2 columns: the first can have values 1 (meaning START) or 2 (meaning STOP), the second column contains the time values. I've to make an array where I have all the differences between consecutive START and STOP times. I've achieved this using a for loop and if statements. I am wondering if there is a way to do this also with some Numpy functions.

import numpy as np

if __name__ == '__main__':
    diff = []
    data = np.fromfile('file.dat', sep=" ").reshape((-1, 2))
    for obj in data:
        if obj[0] == 1:
            tmp = obj[1]
        if obj[0] == 2:
            diff.append(obj[1] - tmp)

If more consecutive 2 appear, I want the script to create as many diff-elements as the consecutive 2, considering for all of them the same START (the last 1 found). If more consecutive 1 appear, I want the script to skip until the last 1 of them, ignoring the others.

Upvotes: 0

Views: 84

Answers (1)

Tls Chris
Tls Chris

Reputation: 3824

Often the complications of using numpy arrays for this type of calculation isn't really worth it. The code in the question is probably much easier to understand. But...

import numpy as np

# *********** Generate some data ***************
            
np.random.seed( 2345 )

code = np.random.randint( 1, 3, size = 10 )

code
# array([1, 2, 1, 1, 1, 2, 2, 1, 1, 1])

time = np.random.rand( 10 ) * 3.0
time = time.cumsum()
time
# array([ 1.91045539,  2.42703695,  3.20819455,  5.95415453,  7.34260105,
#         7.70317392, 10.34583043, 11.02448729, 13.45880826, 14.79644502])

# **********************************************

# Calculation
diff = np.zeros( 10 )
diff[ code == 1 ] = np.diff( time[ code == 1 ], prepend = 0 )
# Take differences between consecutive items where the code is 1

diff
# array([1.91045539, 0.        , 1.29773916, 2.74595997, 1.38844653,
#        0.        , 0.        , 3.68188623, 2.43432098, 1.33763676])

base = diff.cumsum()  # Cumulate that result
base
# array([ 1.91045539,  1.91045539,  3.20819455,  5.95415453,  7.34260105,
#         7.34260105,  7.34260105, 11.02448729, 13.45880826, 14.79644502]

(time - base)[ code == 2 ]
# array([0.51658156, 0.36057286, 3.00322938])
# Subtract the base from time and report where code == 2

Upvotes: 2

Related Questions