python - replace multiple if elif statements and conditions with numpy function such as numpy.where











up vote
-1
down vote

favorite












Radio_index, n_x and n_y are integers
I wrote a if/elif code that can be run.
The purpose is to find x,y position for a radio_index
Am I able to do with np.where



def radio_index2xy(radio_index,n_x,n_y): 

con1 = radio_index <= n_x
con2 = (radio_index > n_x) & (radio_index <= n_x+n_y-1)
con3 = (radio_index > n_x+n_y-1) & (radio_index <= 2*n_x+n_y-2)
con4 = (radio_index > 2*n_x+n_y-2) & (radio_index <= 2*n_x+2*n_y-4)
condlist = [[con1],[con2],[con3],[con4]]
choicelist = [[x_pos = radio_index -1 ,y_pos = 0],
[(x_pos = n_x -1),(y_pos = radio_index - n_x)],
[(x_pos = (n_x-1)-(radio_index-n_x-n_y+1)),(y_pos = n_y -1)],
[(x_pos = 0),(y_pos = 2*n_x+2*n_y-4-radio_index+1)]]
np.select(condlist,choicelist)

return x_pos,y_pos





if radio_index <= n_x:
x_pos = radio_index -1
y_pos = 0
elif radio_index > n_x and radio_index <= n_x+n_y-1:
x_pos = n_x -1
y_pos = radio_index - n_x
elif radio_index > n_x+n_y-1 and radio_index <= 2*n_x+n_y-2:
x_pos = (n_x-1)-(radio_index-n_x-n_y+1)
y_pos = n_y -1
elif radio_index > 2*n_x+n_y-2 and radio_index <= 2*n_x+2*n_y-4:
x_pos = 0
y_pos = 2*n_x+2*n_y-4-radio_index+1









share|improve this question




















  • 3




    Please tell us what the code is supposed to do, I don't think anybody is going to reverse engineer it.
    – timgeb
    Nov 10 at 21:40






  • 1




    Also there's not a single if/elif in this code.
    – timgeb
    Nov 10 at 21:41










  • This is used to find the x(vertical axis) and y(horizontal axis) from the given radio_index
    – Yci Meow
    Nov 11 at 4:37










  • The if/elif method can be run.
    – Yci Meow
    Nov 11 at 4:37















up vote
-1
down vote

favorite












Radio_index, n_x and n_y are integers
I wrote a if/elif code that can be run.
The purpose is to find x,y position for a radio_index
Am I able to do with np.where



def radio_index2xy(radio_index,n_x,n_y): 

con1 = radio_index <= n_x
con2 = (radio_index > n_x) & (radio_index <= n_x+n_y-1)
con3 = (radio_index > n_x+n_y-1) & (radio_index <= 2*n_x+n_y-2)
con4 = (radio_index > 2*n_x+n_y-2) & (radio_index <= 2*n_x+2*n_y-4)
condlist = [[con1],[con2],[con3],[con4]]
choicelist = [[x_pos = radio_index -1 ,y_pos = 0],
[(x_pos = n_x -1),(y_pos = radio_index - n_x)],
[(x_pos = (n_x-1)-(radio_index-n_x-n_y+1)),(y_pos = n_y -1)],
[(x_pos = 0),(y_pos = 2*n_x+2*n_y-4-radio_index+1)]]
np.select(condlist,choicelist)

return x_pos,y_pos





if radio_index <= n_x:
x_pos = radio_index -1
y_pos = 0
elif radio_index > n_x and radio_index <= n_x+n_y-1:
x_pos = n_x -1
y_pos = radio_index - n_x
elif radio_index > n_x+n_y-1 and radio_index <= 2*n_x+n_y-2:
x_pos = (n_x-1)-(radio_index-n_x-n_y+1)
y_pos = n_y -1
elif radio_index > 2*n_x+n_y-2 and radio_index <= 2*n_x+2*n_y-4:
x_pos = 0
y_pos = 2*n_x+2*n_y-4-radio_index+1









share|improve this question




















  • 3




    Please tell us what the code is supposed to do, I don't think anybody is going to reverse engineer it.
    – timgeb
    Nov 10 at 21:40






  • 1




    Also there's not a single if/elif in this code.
    – timgeb
    Nov 10 at 21:41










  • This is used to find the x(vertical axis) and y(horizontal axis) from the given radio_index
    – Yci Meow
    Nov 11 at 4:37










  • The if/elif method can be run.
    – Yci Meow
    Nov 11 at 4:37













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











Radio_index, n_x and n_y are integers
I wrote a if/elif code that can be run.
The purpose is to find x,y position for a radio_index
Am I able to do with np.where



def radio_index2xy(radio_index,n_x,n_y): 

con1 = radio_index <= n_x
con2 = (radio_index > n_x) & (radio_index <= n_x+n_y-1)
con3 = (radio_index > n_x+n_y-1) & (radio_index <= 2*n_x+n_y-2)
con4 = (radio_index > 2*n_x+n_y-2) & (radio_index <= 2*n_x+2*n_y-4)
condlist = [[con1],[con2],[con3],[con4]]
choicelist = [[x_pos = radio_index -1 ,y_pos = 0],
[(x_pos = n_x -1),(y_pos = radio_index - n_x)],
[(x_pos = (n_x-1)-(radio_index-n_x-n_y+1)),(y_pos = n_y -1)],
[(x_pos = 0),(y_pos = 2*n_x+2*n_y-4-radio_index+1)]]
np.select(condlist,choicelist)

return x_pos,y_pos





if radio_index <= n_x:
x_pos = radio_index -1
y_pos = 0
elif radio_index > n_x and radio_index <= n_x+n_y-1:
x_pos = n_x -1
y_pos = radio_index - n_x
elif radio_index > n_x+n_y-1 and radio_index <= 2*n_x+n_y-2:
x_pos = (n_x-1)-(radio_index-n_x-n_y+1)
y_pos = n_y -1
elif radio_index > 2*n_x+n_y-2 and radio_index <= 2*n_x+2*n_y-4:
x_pos = 0
y_pos = 2*n_x+2*n_y-4-radio_index+1









share|improve this question















Radio_index, n_x and n_y are integers
I wrote a if/elif code that can be run.
The purpose is to find x,y position for a radio_index
Am I able to do with np.where



def radio_index2xy(radio_index,n_x,n_y): 

con1 = radio_index <= n_x
con2 = (radio_index > n_x) & (radio_index <= n_x+n_y-1)
con3 = (radio_index > n_x+n_y-1) & (radio_index <= 2*n_x+n_y-2)
con4 = (radio_index > 2*n_x+n_y-2) & (radio_index <= 2*n_x+2*n_y-4)
condlist = [[con1],[con2],[con3],[con4]]
choicelist = [[x_pos = radio_index -1 ,y_pos = 0],
[(x_pos = n_x -1),(y_pos = radio_index - n_x)],
[(x_pos = (n_x-1)-(radio_index-n_x-n_y+1)),(y_pos = n_y -1)],
[(x_pos = 0),(y_pos = 2*n_x+2*n_y-4-radio_index+1)]]
np.select(condlist,choicelist)

return x_pos,y_pos





if radio_index <= n_x:
x_pos = radio_index -1
y_pos = 0
elif radio_index > n_x and radio_index <= n_x+n_y-1:
x_pos = n_x -1
y_pos = radio_index - n_x
elif radio_index > n_x+n_y-1 and radio_index <= 2*n_x+n_y-2:
x_pos = (n_x-1)-(radio_index-n_x-n_y+1)
y_pos = n_y -1
elif radio_index > 2*n_x+n_y-2 and radio_index <= 2*n_x+2*n_y-4:
x_pos = 0
y_pos = 2*n_x+2*n_y-4-radio_index+1






python numpy if-statement






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 4:40

























asked Nov 10 at 21:37









Yci Meow

83




83








  • 3




    Please tell us what the code is supposed to do, I don't think anybody is going to reverse engineer it.
    – timgeb
    Nov 10 at 21:40






  • 1




    Also there's not a single if/elif in this code.
    – timgeb
    Nov 10 at 21:41










  • This is used to find the x(vertical axis) and y(horizontal axis) from the given radio_index
    – Yci Meow
    Nov 11 at 4:37










  • The if/elif method can be run.
    – Yci Meow
    Nov 11 at 4:37














  • 3




    Please tell us what the code is supposed to do, I don't think anybody is going to reverse engineer it.
    – timgeb
    Nov 10 at 21:40






  • 1




    Also there's not a single if/elif in this code.
    – timgeb
    Nov 10 at 21:41










  • This is used to find the x(vertical axis) and y(horizontal axis) from the given radio_index
    – Yci Meow
    Nov 11 at 4:37










  • The if/elif method can be run.
    – Yci Meow
    Nov 11 at 4:37








3




3




Please tell us what the code is supposed to do, I don't think anybody is going to reverse engineer it.
– timgeb
Nov 10 at 21:40




Please tell us what the code is supposed to do, I don't think anybody is going to reverse engineer it.
– timgeb
Nov 10 at 21:40




1




1




Also there's not a single if/elif in this code.
– timgeb
Nov 10 at 21:41




Also there's not a single if/elif in this code.
– timgeb
Nov 10 at 21:41












This is used to find the x(vertical axis) and y(horizontal axis) from the given radio_index
– Yci Meow
Nov 11 at 4:37




This is used to find the x(vertical axis) and y(horizontal axis) from the given radio_index
– Yci Meow
Nov 11 at 4:37












The if/elif method can be run.
– Yci Meow
Nov 11 at 4:37




The if/elif method can be run.
– Yci Meow
Nov 11 at 4:37












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










np.searchsorted is useful for this kind of logic:



def radio_index2xy_v(radio_index, n_x, n_y):
sgn = np.array([0, 1, 1, -1, -1, 0])
col = np.array([-1, 1, 0, 1, 0, -1])
coeffs = np.array([[-1, -1],
[0, -1],
[-n_x, n_x - 1],
[n_y - 1, 2*n_x + n_y - 2],
[2*n_x + 2*n_y - 3, 0],
[-1, -1]])
cusps = np.cumsum([0, n_x, n_y-1, n_x-1, n_y-2])
idx = cusps.searchsorted(radio_index)
out = coeffs[idx]
out[np.arange(idx.size), col[idx]] += sgn[idx] * radio_index
return out


Demo:



>>> radio_index2xy_v(np.arange(20), 5, 4)
array([[-1, -1],
[ 0, 0],
[ 0, 1],
[ 0, 2],
[ 0, 3],
[ 0, 4],
[ 1, 4],
[ 2, 4],
[ 3, 4],
[ 3, 3],
[ 3, 2],
[ 3, 1],
[ 3, 0],
[ 2, 0],
[ 1, 0],
[-1, -1],
[-1, -1],
[-1, -1],
[-1, -1],
[-1, -1]])





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243650%2fpython-replace-multiple-if-elif-statements-and-conditions-with-numpy-function%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    np.searchsorted is useful for this kind of logic:



    def radio_index2xy_v(radio_index, n_x, n_y):
    sgn = np.array([0, 1, 1, -1, -1, 0])
    col = np.array([-1, 1, 0, 1, 0, -1])
    coeffs = np.array([[-1, -1],
    [0, -1],
    [-n_x, n_x - 1],
    [n_y - 1, 2*n_x + n_y - 2],
    [2*n_x + 2*n_y - 3, 0],
    [-1, -1]])
    cusps = np.cumsum([0, n_x, n_y-1, n_x-1, n_y-2])
    idx = cusps.searchsorted(radio_index)
    out = coeffs[idx]
    out[np.arange(idx.size), col[idx]] += sgn[idx] * radio_index
    return out


    Demo:



    >>> radio_index2xy_v(np.arange(20), 5, 4)
    array([[-1, -1],
    [ 0, 0],
    [ 0, 1],
    [ 0, 2],
    [ 0, 3],
    [ 0, 4],
    [ 1, 4],
    [ 2, 4],
    [ 3, 4],
    [ 3, 3],
    [ 3, 2],
    [ 3, 1],
    [ 3, 0],
    [ 2, 0],
    [ 1, 0],
    [-1, -1],
    [-1, -1],
    [-1, -1],
    [-1, -1],
    [-1, -1]])





    share|improve this answer

























      up vote
      0
      down vote



      accepted










      np.searchsorted is useful for this kind of logic:



      def radio_index2xy_v(radio_index, n_x, n_y):
      sgn = np.array([0, 1, 1, -1, -1, 0])
      col = np.array([-1, 1, 0, 1, 0, -1])
      coeffs = np.array([[-1, -1],
      [0, -1],
      [-n_x, n_x - 1],
      [n_y - 1, 2*n_x + n_y - 2],
      [2*n_x + 2*n_y - 3, 0],
      [-1, -1]])
      cusps = np.cumsum([0, n_x, n_y-1, n_x-1, n_y-2])
      idx = cusps.searchsorted(radio_index)
      out = coeffs[idx]
      out[np.arange(idx.size), col[idx]] += sgn[idx] * radio_index
      return out


      Demo:



      >>> radio_index2xy_v(np.arange(20), 5, 4)
      array([[-1, -1],
      [ 0, 0],
      [ 0, 1],
      [ 0, 2],
      [ 0, 3],
      [ 0, 4],
      [ 1, 4],
      [ 2, 4],
      [ 3, 4],
      [ 3, 3],
      [ 3, 2],
      [ 3, 1],
      [ 3, 0],
      [ 2, 0],
      [ 1, 0],
      [-1, -1],
      [-1, -1],
      [-1, -1],
      [-1, -1],
      [-1, -1]])





      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        np.searchsorted is useful for this kind of logic:



        def radio_index2xy_v(radio_index, n_x, n_y):
        sgn = np.array([0, 1, 1, -1, -1, 0])
        col = np.array([-1, 1, 0, 1, 0, -1])
        coeffs = np.array([[-1, -1],
        [0, -1],
        [-n_x, n_x - 1],
        [n_y - 1, 2*n_x + n_y - 2],
        [2*n_x + 2*n_y - 3, 0],
        [-1, -1]])
        cusps = np.cumsum([0, n_x, n_y-1, n_x-1, n_y-2])
        idx = cusps.searchsorted(radio_index)
        out = coeffs[idx]
        out[np.arange(idx.size), col[idx]] += sgn[idx] * radio_index
        return out


        Demo:



        >>> radio_index2xy_v(np.arange(20), 5, 4)
        array([[-1, -1],
        [ 0, 0],
        [ 0, 1],
        [ 0, 2],
        [ 0, 3],
        [ 0, 4],
        [ 1, 4],
        [ 2, 4],
        [ 3, 4],
        [ 3, 3],
        [ 3, 2],
        [ 3, 1],
        [ 3, 0],
        [ 2, 0],
        [ 1, 0],
        [-1, -1],
        [-1, -1],
        [-1, -1],
        [-1, -1],
        [-1, -1]])





        share|improve this answer












        np.searchsorted is useful for this kind of logic:



        def radio_index2xy_v(radio_index, n_x, n_y):
        sgn = np.array([0, 1, 1, -1, -1, 0])
        col = np.array([-1, 1, 0, 1, 0, -1])
        coeffs = np.array([[-1, -1],
        [0, -1],
        [-n_x, n_x - 1],
        [n_y - 1, 2*n_x + n_y - 2],
        [2*n_x + 2*n_y - 3, 0],
        [-1, -1]])
        cusps = np.cumsum([0, n_x, n_y-1, n_x-1, n_y-2])
        idx = cusps.searchsorted(radio_index)
        out = coeffs[idx]
        out[np.arange(idx.size), col[idx]] += sgn[idx] * radio_index
        return out


        Demo:



        >>> radio_index2xy_v(np.arange(20), 5, 4)
        array([[-1, -1],
        [ 0, 0],
        [ 0, 1],
        [ 0, 2],
        [ 0, 3],
        [ 0, 4],
        [ 1, 4],
        [ 2, 4],
        [ 3, 4],
        [ 3, 3],
        [ 3, 2],
        [ 3, 1],
        [ 3, 0],
        [ 2, 0],
        [ 1, 0],
        [-1, -1],
        [-1, -1],
        [-1, -1],
        [-1, -1],
        [-1, -1]])






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 8:17









        Paul Panzer

        28.6k21139




        28.6k21139






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243650%2fpython-replace-multiple-if-elif-statements-and-conditions-with-numpy-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine