Python for loop for Array - arrays

I would like to take mean of a window (5x5).
And this window beginning will move +1 at x axis. After first x axis finished, window will move +1 at y axis and will start a new loop. And I have to say that bant4.tif is an image 512x512.
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
nir = mpimg.imread('bant4.tif')
A = np.array(nir)
for k in range(0,508):
for j in range(0,508):
ilk = A[k][j] + A[k][j+1] + A[k][j+2] + A[k][j+3] + A[k][j+4]
ikinci = A[k+1][j] + A[k+1][j+1] + A[k+1][j+2] + A[k+1][j+3] + A[k+1][j+4]
ucuncu = A[k+2][j] + A[k+2][j+1] + A[k+2][j+2] + A[k+2][j+3] + A[k+2][j+4]
dorduncu = A[k+3][j] + A[k+3][j+1] + A[k+3][j+2] + A[k+3][j+3] + A[k+3][j+4]
besinci = A[k+4][j] + A[k+4][j+1] + A[k+4][j+2] + A[k+4][j+3] + A[k+4][j+4]
ort = (ilk + ikinci + ucuncu + dorduncu + besinci) / 5
ort.tofile('ort.txt')
Here is the error.
Warning (from warnings module):
File "C:\Users\Celik\Desktop\ödev5.py", line 19
ort = (ilk + ikinci + ucuncu + dorduncu + besinci) / 5
RuntimeWarning: overflow encountered in ubyte_scalars
Warning (from warnings module):
File "C:\Users\Celik\Desktop\ödev5.py", line 17
besinci = A[k+4][j] + A[k+4][j+1] + A[k+4][j+2] + A[k+4][j+3] + A[k+4][j+4]
RuntimeWarning: overflow encountered in ubyte_scalars
Warning (from warnings module):
File "C:\Users\Celik\Desktop\ödev5.py", line 14
ikinci = A[k+1][j] + A[k+1][j+1] + A[k+1][j+2] + A[k+1][j+3] + A[k+1][j+4]
RuntimeWarning: overflow encountered in ubyte_scalars
Warning (from warnings module):
File "C:\Users\Celik\Desktop\ödev5.py", line 15
ucuncu = A[k+2][j] + A[k+2][j+1] + A[k+2][j+2] + A[k+2][j+3] + A[k+2][j+4]
RuntimeWarning: overflow encountered in ubyte_scalars
Warning (from warnings module):
File "C:\Users\Celik\Desktop\ödev5.py", line 13
ilk = A[k][j] + A[k][j+1] + A[k][j+2] + A[k][j+3] + A[k][j+4]
RuntimeWarning: overflow encountered in ubyte_scalars
Warning (from warnings module):
File "C:\Users\Celik\Desktop\ödev5.py", line 16
dorduncu = A[k+3][j] + A[k+3][j+1] + A[k+3][j+2] + A[k+3][j+3] + A[k+3][j+4]
RuntimeWarning: overflow encountered in ubyte_scalars

Related

Why my R seems take forever to run a Mixed effects logistic regression

I indeed got a relatively big dataset and my mixed effects logistic regression is like below. Is that normal to take that long to run? or I made some mistakes?
library(lme4)
glmer_EBRD_undersample_1 <- glmer(leave_happened ~
performance_rating_2016 + performance_rating_2017 + performance_rating_2018 + performance_rating_2019 + performance_rating_2020
+ gender
+ target_group
+ target_pmf_band
+ target_hq_or_ro
+ target_office_location_country_distilled
+ target_org_unit_cost_centre_code_distilled
+ target_ebrd_region_distilled
+ target_contract_group_distilled
+ target_position_tenure_group
+ target_length_of_service_group_distilled
+ leaves_to_date
+ moves_to_date
+ joins_to_date
+ applied_count_to_date
+ line_reviewed_to_date
+ interviewed_to_date
+ offered_to_date
+ hired_to_date
+ (1 | person_id)
,
data = train_undersample_1,
family = binomial,
control = glmerControl(optimizer = "bobyqa"),
nAGQ = 10
)
summary(glmer_EBRD_undersample_1)
Also gave a warning like this: Warning in commonArgs(par, fn, control, environment()) :
maxfun < 10 * length(par)^2 is not recommended.

typescript date formatting not working

I am trying to get a date for mat like "2018-05-17T08:09:02", but when I tried below code I get "2018-05-17T8:9:2"
can some one help to get "2018-05-17T08:09:02" , this format
let d = new Date();
console.log("date>> "+d.getFullYear() + "-" + ((d.getMonth() + 1) < 10 ? '0' : '') +
(d.getMonth() + 1) + "-" + d.getDate() + "T" +( d.getHours() )+ ":"+ d.getMinutes() + ":"+ d.getSeconds());
According to How to format numbers by prepending 0 to single-digit numbers?
Your desirable answer is
let d = new Date();
console.log("date>> "+d.getFullYear() + "-" + ((d.getMonth() + 1) < 10 ? '0' : '') +
(d.getMonth() + 1) + "-" + d.getDate() + "T" +("0" + d.getHours()).slice(-2)+ ":"+ ("0" + d.getMinutes()).slice(-2) + ":"+ ("0" + d.getSeconds()).slice(-2));
But as #Aleksey Solovey already mentioned in the above, I also recommand to use d.toISOString().slice(0,-5).

Partial differentiation/gradient of anonymous function with array input

I have following anonymous function (with x as an array):
f = #(x) 312*x(2) - 240*x(1) + 30*x(3) - 24*x(4) + 282*x(1)*x(2) + 30*x(1)*x(3) + 18*x(1)*x(4) + 54*x(2)*x(3) + 6*x(2)*x(4) + 6*x(3)*x(4) + 638*x(1)^2 + 207*x(2)^2 + 6*x(3)^2 + 3*x(4)^2 + 4063
I want to make gradient of this function and save it for future use. Also with array input.
X = [ 0;...
0;...
0;...
0];
F = f(X)
G = g(X)
Is it possible to archive this with this type of function? Or maybe it is possible to somehow make it via diff command? Something like this:
g = [diff(f, x(1));...
diff(f, x(2));...
diff(f, x(3));...
diff(f, x(4))]
I guess the following is what you want. I'm afraid, you need the Symbolic Math Toolbox for a simple solution, otherwise I'd rather calculate the derivatives by hand.
x = [1 2 3 4];
%// define function
syms a b c d
f = 312*b - 240*a + 30*c - 24*d + 282*a*b + 30*a*c + 18*a*d + 54*b*c + ...
6*b*d + 6*c*d + 638*a^2 + 207*b^2 + 6*c^2 + 3*d^2 + 4063
%// symbolic gradient
g = gradient(f,[a,b,c,d])
%// eval symbolic function
F = subs(f,[a,b,c,d],x)
G = subs(g,[a,b,c,d],x)
%// convert symbolic value to double
Fd = double(F)
Gd = double(G)
or alternatively:
%// convert symbolic function to anonymous function
fd = matlabFunction(f)
gd = matlabFunction(g)
%// eval anonymous function
x = num2cell(x)
Fd = fd(x{:})
Gd = gd(x{:})
f =
638*a^2 + 282*a*b + 30*a*c + 18*a*d - 240*a + 207*b^2 + 54*b*c +
6*b*d + 312*b + 6*c^2 + 6*c*d + 30*c + 3*d^2 - 24*d + 4063
g =
1276*a + 282*b + 30*c + 18*d - 240
282*a + 414*b + 54*c + 6*d + 312
30*a + 54*b + 12*c + 6*d + 30
18*a + 6*b + 6*c + 6*d - 24
F =
7179
G =
1762
1608
228
48
fd =
#(a,b,c,d)a.*-2.4e2+b.*3.12e2+c.*3.0e1-d.*2.4e1+a.*b.*2.82e2+a.*c.*3.0e1+a.*d.*1.8e1+b.*c.*5.4e1+b.*d.*6.0+c.*d.*6.0+a.^2.*6.38e2+b.^2.*2.07e2+c.^2.*6.0+d.^2.*3.0+4.063e3
gd =
#(a,b,c,d)[a.*1.276e3+b.*2.82e2+c.*3.0e1+d.*1.8e1-2.4e2;a.*2.82e2+b.*4.14e2+c.*5.4e1+d.*6.0+3.12e2;a.*3.0e1+b.*5.4e1+c.*1.2e1+d.*6.0+3.0e1;a.*1.8e1+b.*6.0+c.*6.0+d.*6.0-2.4e1]
x =
[1] [2] [3] [4]
Fd =
7179
Gd =
1762
1608
228
48

how to create pymol rename loop

I would like to create a loop for changing interactions name in PyMol. But after one selection loop it crashes and doesn't work.
def get_dists(interactions): # interactions=([1,2], [3,4])
for i in interactions:
a = "////" + str(i[0]) + "/C2'"
b = "////" + str(i[1]) + "/C2'"
cmd.distance("(" + a + ")", "(" + b + ")")
for j in range(1, 599):
x = "dist" + "0" + str(j)
y = str(i[0]) + " " + str(i[1])
cmd.set_name(str(x), str(y))
In Pymol the default name of interactions is dist01, 02 , 03.
I want to change these to 1_3, 5_59, 4_8, (interaction between residue).
Your code is totally fine except for one thing: If PyMol doesn't succeed with set_name the whole script is aborted. When you change it to, it should work:
try:
cmd.set_name(str(x), str(y))
except:
print('failed to rename')
Some additional comments:
y = str(i[0]) + " " + str(i[1]) should be y = str(i[0]) + "_" + str(i[1])
this line is probably for padding zeros x = "dist" + "0" + str(j). This is only needed when j is a single digit, otherwise the name of the distance objects is dist20 or dist123
cmd.set_name(str(x), str(y)) can be simplified to cmd.set_name(x, y) since x and y are already strings.

XMPP implementation in silverlight authentication returns failure

I am trying to implement Xmpp protocol in silverlight and trying to connect to Facebook, here I am getting everything correct until <challenge .. > obtained from server.
I am using X Facebook platform authentication.
I have done this with following code:
byte[] ch = Convert.FromBase64String(message.challenge);
string challenge = System.Text.Encoding.UTF8.GetString(ch, 0, ch.Length);
string response = "";
long callId = DateTime.UtcNow.Ticks;
MD5 md = new MD5();
String signature1 = "api_key=203366556348506"
+ "call_id=" + callId
+ "method=auth.xmpp_login"
+ param[2]
+ "session_key=" + messageClient.SessionKey
+ "v=1.0"
+ messageClient.AppSecret;
md.Value = signature1;
response = "method=auth.xmpp_login&api_key=203366556348506&session_key=bc6d6e00462cc2bb73a824bd.4-100001565408667&call_id=" + callId + "&sig=c47d741cb8f18c4e78b990f48e2f63aa&v=1.0&" + param[2];
message.Request = "<response xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">" + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(response)) + "</response>";
this.messageClient.SendMessageAsync(message);
But I am getting following message from server:
<failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/></failure>
Please let me know where I am going wrong.
Try following code:
String signature1 = "api_key=" + messageClient.ApiKey
+ "call_id=" + callId
+ "method=auth.xmpp_login"
+ param[2]
+ "session_key=" + messageClient.SessionKey
+ "v=1.0"
+ messageClient.AppSecret;
md.Value = signature1;
response = "method=auth.xmpp_login&api_key=" + messageClient.ApiKey + "&session_key=" + messageClient.SessionKey + "&call_id=" + callId + "&sig=" + md.FingerPrint.ToLower() + "&v=1.0&" + param[2];
I have changed response string to the one above.
This has returned success for me. Hope this will help you.

Resources