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










-1














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















-1














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













-1












-1








-1







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


















0














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',
    autoActivateHeartbeat: false,
    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









    0














    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

























      0














      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























        0












        0








        0






        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

        29.9k21241




        29.9k21241



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid …


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid …


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            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

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党