pythonnumpyrange

unwrapping a contour


I have 3 arrays as below

(Pdb) Istag
[[67, 68], [227, 228]]

(Pdb) spl
array([151, 302])

(Pdb) wspl
array([26, 52])

what these data represents is as per the diagram below;

enter image description here

Numbers in black corresponds to the numbers shown in the above arrays while, numbers in Red shows the transformed numbers if I were to take them into a single list of numbers. The curvy sections are held in one array "spl" while the straight sections are held in another array "wspl". Curvy section is to be split into two ranges based on the "Istag" while adding straight sections into indices.

basically, what I want is to convert these array indices into 6 range objects S1,S2,S3... etc and the ranges should be as follows

S1: range( 67,   0, -1)
S2: range( 68, 151,  1)
S3: range(151, 177,  1)
S4: range(253, 178, -1)
S5: range(254, 328,  1)
S6: range(328, 380,  1)

could someone please shed some light indicating how this can be done ?


Solution

  • The exact logic is not fully clear (what happens if you have more than 2 items?), but you should be able to use a small loop and zip:

    Istag = [[67, 68], [227, 228]]
    spl = np.array([151, 302])
    wspl = np.array([26, 52])
    
    out = []
    
    prev_c = -1
    prev_d = 0
    for (a, b), c, d in zip(Istag, spl, wspl):
        out.extend([range(a+prev_d, prev_c+prev_d+1, -1),
                    range(b+prev_d, c+prev_d),
                    range(c+prev_d, c+d+prev_d)])
        prev_c = c
        prev_d = d
    

    Output:

    [range(67, 0, -1),
     range(68, 151),
     range(151, 177),
     range(253, 178, -1),
     range(254, 328),
     range(328, 380)]
    

    NB. if you want actual lists, replace range(...) by list(range(...)) in the above code. If you want a cumulative sum, use prev_c += c/prev_d += d in place of prev_c = c/prev_d = d.