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
python numpy if-statement
add a comment |
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
python numpy if-statement
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 singleif
/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
add a comment |
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
python numpy if-statement
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
python numpy if-statement
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 singleif
/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
add a comment |
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 singleif
/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
add a comment |
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]])
add a comment |
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]])
add a comment |
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]])
add a comment |
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]])
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]])
answered Nov 11 at 8:17
Paul Panzer
28.6k21139
28.6k21139
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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