Apache(XAMPP) not respond when high load

Question: My computer has 8 core and 16 GB RAM, in case of high traffic, my web server (apache - xampp) can't be accessed when the RAM hit 3GB. My CPU only used on 20%. And I need to restart the web server.
I set this on php.ini
max_execution_time=60
memory_limit=10128M

is there any suggestion to optimize my server, so that my server can serve till RAM/CPU used to it's limit?

edit:
my httpd-mpm.conf
#
# Server-Pool Management (MPM specific)
#

#
# PidFile: The file in which the server should record its process
# identification number when it starts.
#
# Note that this is the default PidFile for most MPMs.
#
<IfModule !mpm_netware_module>
    PidFile "logs/httpd.pid"
</IfModule>

#
# Only one of the below sections will be relevant on your
# installed httpd.  Use "apachectl -l" to find out the
# active mpm.
#

# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# MaxRequestWorkers: maximum number of server processes allowed to start
# MaxConnectionsPerChild: maximum number of connections a server process serves
#                         before terminating
<IfModule mpm_prefork_module>
    StartServers             5
    MinSpareServers          5
    MaxSpareServers         10
    MaxRequestWorkers      150
    MaxConnectionsPerChild   0
</IfModule>

# worker MPM
# StartServers: initial number of server processes to start
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestWorkers: maximum number of worker threads
# MaxConnectionsPerChild: maximum number of connections a server process serves
#                         before terminating
<IfModule mpm_worker_module>
    StartServers             2
    ServerLimit        500
    MinSpareThreads         25
    MaxSpareThreads         75
    ThreadsPerChild         25
    MaxRequestWorkers      150
    MaxConnectionsPerChild   0
</IfModule>

# event MPM
# StartServers: initial number of server processes to start
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestWorkers: maximum number of worker threads
# MaxConnectionsPerChild: maximum number of connections a server process serves
#                         before terminating
<IfModule mpm_event_module>
    StartServers             2
    MinSpareThreads         25
    MaxSpareThreads         75
    ThreadsPerChild         25
    MaxRequestWorkers      150
    MaxConnectionsPerChild   0
</IfModule>

# NetWare MPM
# ThreadStackSize: Stack size allocated for each worker thread
# StartThreads: Number of worker threads launched at server startup
# MinSpareThreads: Minimum number of idle threads, to handle request spikes
# MaxSpareThreads: Maximum number of idle threads
# MaxThreads: Maximum number of worker threads alive at the same time
# MaxConnectionsPerChild: Maximum  number of connections a thread serves. It
#                         is recommended that the default value of 0 be set
#                         for this directive on NetWare.  This will allow the
#                         thread to continue to service requests indefinitely.
<IfModule mpm_netware_module>
    ThreadStackSize      65536
    StartThreads           250
    MinSpareThreads         25
    MaxSpareThreads        250
    MaxThreads            1000
    MaxConnectionsPerChild   0
</IfModule>

# OS/2 MPM
# StartServers: Number of server processes to maintain
# MinSpareThreads: Minimum number of idle threads per process,
#                  to handle request spikes
# MaxSpareThreads: Maximum number of idle threads per process
# MaxConnectionsPerChild: Maximum number of connections per server process
<IfModule mpm_mpmt_os2_module>
    StartServers             2
    MinSpareThreads          5
    MaxSpareThreads         10
    MaxConnectionsPerChild   0
</IfModule>

# WinNT MPM
# ThreadsPerChild: constant number of worker threads in the server process
# MaxConnectionsPerChild: maximum number of connections a server process serves
<IfModule mpm_winnt_module>
    ThreadsPerChild        1150
    MaxConnectionsPerChild   0
</IfModule>

# The maximum number of free Kbytes that every allocator is allowed
# to hold without calling free(). In threaded MPMs, every thread has its own
# allocator. When not set, or when set to zero, the threshold will be set to
# unlimited.
<IfModule !mpm_netware_module>
    MaxMemFree            2048
</IfModule>
<IfModule mpm_netware_module>
    MaxMemFree             100
</IfModule>

Comment: Check how many Apache workers you are set to. Consider asking this on serverfault instead :)
Comment: i add my httpd-mpm.conf. would you help me optimize it?
Comment: How about not running a high traffic site on XAMPP?
Comment: this was leftover project, I just continue to maintenance it. I'll consider this option.
Comment: XAMPP is for development, not really geared for production systems.  However, Apache, PHP, and MySQL can obviously be geared to run production systems fine, just off topic for Stackoverflow.  There are far too many things to check to cover in one question even if it weren't off topic.
Comment: What is the OS you are running XAMPP on?
Are you using 32-bit php.exe?
Comment: oracle linux 6, 64 bit
Comment: Do you see any errors or warning on launch of apache in apache logs?
Comment: there's no error message even in log file.
Comment: Are you getting server not responding from client side? What is the issue?
Answer: Some of the tweaks which you can add to your apache configuration file (httpd.conf) are
KeepAlive On

MaxKeepAliveRequests 75

KeepAliveTimeout 100

AcceptFilter http none
AcceptFilter https none

Comment: i set MaxKeepAliveRequests 100 but KeepAliveTimeout 10, i'll try change it to 100


Generating gamma random field with given covariance matrix

Question: I have to generate multivariate gamma distributions with given positive-definite covariance matrix. Anyone can suggest me a method?
Thanks,
Answer: <code>library(clusterGeneration)
genPositiveDefMat(dim,...) #Where  dim is the dimension of the matrix, explore the   #function
</code>
You can now use any package that will generate a multivariate gamma in R using the positive definite matrix above.
Answer: This article, "Gamma random field simulation by a covariance matrix transformation method
" (https://link.springer.com/article/10.1007%252Fs00477-010-0434-8), should help you.
Comment: Please add the reference of the paper in case the link dies in the future. Thanks!
Comment: And if possible, provide a brief summary of how the paper is helpful here


Some questions about $\operatorname{spec}( \mathbb{Z} )$

Question: Let consider the scheme $\operatorname{spec}(\mathbb{Z})$ I have some questions about this scheme :

For a variety $X$ defined over a field $K$, one can define the dualizing sheaf. Is there an analogue for $\operatorname{spec}(\mathbb{Z})$ ? Does $\operatorname{spec}(\mathbb{Z})$ have a "dualizing sheaf" ?

I know that on affine schemes the cohomology groups $H^i(\operatorname{Spec}(\mathbb{Z}), \mathcal{F})$ are 0 for $i>0$ and for every quasicoherent sheaf $\mathcal{F}$. And I know that invertible sheaf are quasi coherent. So let $D$ be a divisor on $\operatorname{spec}(\mathbb{Z})$, if I denote $L_D$ the invertible sheaf associated with $D$, we have $H^i( \operatorname{spec}(\mathbb{Z}), L_D)=0$ for all $i>0$. But what is the forme of the $H^0$ ? How can I describe it ?

Thanks for help
Comment: For 1), note that the dualizing sheaf is a notion for a *relative* scheme, ie a *morphism* of schemes. Even for a variety $X$, the dualizing sheaf is that of $X$ *over $K$*. Wikipedia says that, at least, every proper finitely presented morphism has a dualizing sheaf. For 2), I'd need to check again the notation, but since $L_D$ is invertible over the spectrum of $\mathbb{Z}$, it's $\mathcal{O}_{\mathrm{Spec}\,\mathbb{Z}}t$ for some rational number $t$. Depending on signs, $t$ will be equal to the product of $p^{n_D(p)}$ or its inverse, where $n_D(p)$ is the "coefficient" of $p$ in $D$.
Comment: For a Noetherian ring $R$ a finitely generated module $K$ is a dualizing module if $\operatorname{Ext}^i(R/M, K)=0$, $M$ any maximal ideal,  for $i$ not equal to the height of $M$ and is one dimensional over $R/M$ when $i$ is the height of $M$. With this definition, the dualizing module of integers is just $\mathbb{Z}$.
Comment: @Mindlack that looks like an answer to me - would you care to record it as such below?
Answer: I'm turning my comment into an answer at KReiser's request, although it would probably need an improvement – more precision, maybe.
For 1), note that the dualizing sheaf is a notion for a relative scheme, ie a morphism of schemes. Even for a variety $X$, the dualizing sheaf is that of $X$ over $K$. Wikipedia says that, at least, every proper finitely presented morphism has a dualizing sheaf.
For 2), I'd need to check again the notation, but since $L_D$ is invertible over the spectrum of $\mathbb{Z}$, it's $\mathcal{O}_{\mathrm{Spec}\,\mathbb{Z}}t$, for some rational number $t$ (ie a rational function on $\mathrm{Spec}\,\mathbb{Z}$). Depending on signs and conventions, $t$ will be equal to (plus or minus) the product of the $p^{n_D(p)}$ or its inverse, where $p$ runs over the prime numbers and $n_D(p)$ is the "coefficient" of the closed point corresponding to $p$ in $D$ (seen as a Weil divisor).


Explaining gam model output with a reference

Question: I'm seeking a good reference to explain the gam model output to colleague. Here is my model.
mod1 <- gam(severity ~  s(mean_rh, k = 8) + s(mean_temp, k = 10) + s(mean_ws, k =7) + s(avg_daily_rain, k = 7), family = betar(),  data = dat_seasonal)

summary(mod1)

Here is the output:
Formula:
disease_severity ~ s(mean_rh, k = 8) + s(mean_temp, k = 10) +
    s(mean_ws, k = 7) + s(avg_daily_rain, k = 7)

Parametric coefficients:
            Estimate Std. Error z value Pr(>|z|)
(Intercept)  -0.1687     0.1374  -1.228    0.219

Approximate significance of smooth terms:
                    edf Ref.df Chi.sq  p-value
s(mean_rh)        1.000  1.000   2.76 0.096633 .
s(mean_temp)      4.231  4.598  74.22  < 2e-16 ***
s(mean_ws)        2.461  2.673  17.53 0.000669 ***
s(avg_daily_rain) 1.000  1.000  49.89  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

R-sq.(adj) =  0.847   Deviance explained = 91.8%
-REML = -29.205  Scale est. = 1         n = 37

Here is the raw data plot (left) and the model output plot (right) for the predictor mean_temp.

My collaborator is not aware of GAMS and expects linear/continuous/higher disease severity with increasing temperatures OR a bell shaped curve (he doesn't expect less disease at 14 & 16 degree Celsius and higher disease at 12 degree Celcius). Here is his specific comment. "Does the figure show greater disease severity at 11 and 17 degree Celsius than the temperature in between? That is what I saw in this subfigure and that is against current knowledge and indefensible."
I need to explain to him with a reference that the model has captured the raw data pattern very well, and the whole point behind a GAM is that it models complex behavior between predictors and outcomes. Terms with EDFs higher than one are not supposed to have a singular explanation. The response variable is not linear, DHARMa residuals are fine and the gam.check() output is also good. Thanks
Answer: You could cite the famous book Generalized Additive Models, An introduction with R. It include a lot of explanations and several examples.
Comment: Thanks. The book is not available for free. I'm looking for link to somewhat similar example, which is readily available.
Comment: I am sorry. Maybe you can find some way to get it through you institution if you belong to one. It's a pretty standard textbook (the same guy wrote the mgcv package) so it really should be easy to find in any university library.
Answer: An Introduction to Statistical Learning covers the principles of GAMs in Section 7.7, with Section 7.7.1 specifically on regression models like yours. I don't think that the particular smooths described there are identical to the s() smooths in the gam() function of mgcv that you used, but the general principle of smooth nonlinear additive modeling is the same.
Note that comparison to the raw data plot can be misleading, as it doesn't take into account the other predictor variables. I'll admit that I didn't at first see how the curve on the right could reasonably have represented the raw data on the left, until I remembered what I was looking at.
As mean_rh and avg_daily_rain ended up with what seem to be linear fits, you might display the residuals around what you would have predicted based on those linear fits, instead of showing raw data values. That might make the curvilinear association with mean_temp more obvious.
Comment: I have already added residuals using `plot(mod1,  rug = TRUE, residuals = TRUE, pch = 1, cex = 1, shade = TRUE, seWithMean = TRUE, shift = coef(mod1)[1])`. Unless you're referring to some other residuals?
Comment: @Ahsk I'm looking for a way to generate something like a "raw data" plot that looks more like your smooth. The raw data plot you show doesn't take into account any of the other variables in the model. If you could take into account the contributions of `mean_rh` and `avg_daily_rain`, that might help. Try plotting the residuals from a simple linear model, with just those two linear predictors, against the `mean_temp` values as an alternative to your simple raw data plot on the left. I suspect that will look more like the smooth on the right.
Comment: Okay, thank you.
Comment: In addition to providing the reference, maybe I should reword my interpretation too to avoid potential conflict. Can I say that, `Overall, increasing temperatures had a positive effect on the response variable, although the effect was not linear. Lower values for response variable were observed between 14-16 degree Celsius`. Thanks very much.
Comment: @Ahsk that doesn't seem to describe the modeled values on the plot on the right, although it _might_ describe the raw values on the left. Modeled values at 12 degrees are about the same as they are at 17. If you are talking about the modeled values, I also wouldn't describe them as "observed." For the modeled values you might say something like: "With other variables taken into account, the model suggests a dip in the response as a function of temperature between about 14 and 16 degrees."
Comment: Thank you very much.


Implements interface with generics, both methods have same erasure

Question: Here's my code. I've a base interface to define common function.
    interface BaseInterface<T> {
        void baseFunc(T param)
    }

    interface MyInterface1 extends BaseInterface<MyEntity1> {
        void func1()
    }

    interface MyInterface2 extends BaseInterface<MyEntity2> {
        void func2()
    }

And my class is:
    abstract class BaseClass implements BaseInterface<T> {
        // ...some common things
    }

    class ClassA extends BaseClass implements MyInterface1 {
        @Override
        public void func1() {...}

        @Override
        public void baseFunc(MyEntity1 param) {...}
    }

    class ClassB extends BaseClass implements MyInterface2 {
        @Override
        public void func2() {...}

        @Override
        public void baseFunc(MyEntity2 param) {...}
    }

Different class do the to do same things with different bean MyEntity1 and MyEntity2. So I defined baseFunc() with generic T.
But there's a problem here. "both methods have same erasure yet neither overrides the other baseFunc(T) and baseFunc(MyEntity1)..."
How to slove this problem?
By the way, It works fine when I removed the generic and move the baseFunc to MyInterface1 and MyInterface2. But I don't think it's the best way.
Comment: Something's wrong with `abstract class BaseClass implements BaseInterface`. What's `T`?
Comment: Right, I reviewed my code and found this. abstract class BaseClass implements BaseInterface                   change to  abstract class BaseClass implements BaseInterface    class ClassA extends BaseClass class ClassB extends BaseClass
Comment: @OscarZhang Sorry, I typed my answer before I noticed your comment.  Did that work for  you?  If so, you could convert your comment to an answer, in which case I'll delete mine; since your comment and my answer say basically the same thing.
Comment: @DawoodibnKareem haha, it's ok. Your answer is is the solution. Keep it and it's more obvious to others who met the same problem.
Answer: The declaration
abstract class BaseClass implements BaseInterface<T>

makes no sense, unless T is an actual class.  If it's implementing a generic interface, it needs to either

be a generic class, or
supply a type argument in place of the generic type parameter.

I believe what you actually wanted to write was
abstract class BaseClass<T> implements BaseInterface<T>

to keep BaseClass as a generic class.  You could then declare the other classes as
class ClassA extends BaseClass<MyEntity1> implements MyInterface1

and
class ClassB extends BaseClass<MyEntity2> implements MyInterface2

Then there would be no clash of the types passed to the baseFunc method.


Como pegar o valor do campo data?

Question: Tenho um método que retorna valores do banco nos meus componentes da tela. Ele recebe como parâmetro 3 string e 2 DateTime
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.Titulo1.SetaTitulo(Resources.Resource.titulo_cadVideo);
        if (!string.IsNullOrEmpty(Request.QueryString["nIDMedia"]))
        {
            string nIDMedia = Request.QueryString["nIDMedia"];
            Video.PreencheCampos(nIDMedia, txt_descricao.Text,
                txt_endereco_link.Text, cl_inicio, cl_fim);
            }
        }
    }

O problema é que não consigo pegar o valor dos campos de data que são o cl_inicio e cl_fim que são do tipo Calendar.
Comment: Só com esse trecho de código fica difícil saber o que está acontecendo. Existe algum motivo específico para estas variáveis serem do tipo `Calendar`?
Comment: Sua pergunta ainda não está clara. O problema é: Pegar o valor e mostrar em um campo texto? Pegar o valor e mostrar marcado no calendário? Pegar o valor que o usuário selecionou na tela?
Answer: Use da seguinte forma:
Video.PreencheCampos(nIDMedia, txt_descricao.Text,
    txt_endereco_link.Text, cl_inicio.SelectedDate, cl_fim.SelectedDate);
}

Veja mais sobre o SelectedDate aqui.

Como pegar o valor do campo data?


Polynomial with complex coefficients proof problem

Question: Let $P(z)$ be a polynomial with complex coefficients such that $P(n) = 0$ for all integer $n$. Prove that $P(z) = 0$ for all $z \in C$.
Comment: Any nonconstant polynomial must have at most finitely many zeroes.  Since there are countably infinite integers and P vanishes at all of them, P must be a constant polynomial; clearly, it must be the constant-zero polynomial.
Comment: @SinisterCutlass hi, so P=0? Is that what u mean, but I don't get it why in the question it says it's with complex coefficient
Comment: It doesn't matter if a polynomial has complex or real coefficients. The idea is that it can only have a finite number of roots. Thus, this is a useful fact that you can learn about polynomials: if a polynomial has infinitely many roots then it is the zero polynomial.
Comment: @BeniBogosel wait, if a polynomial has "infinitely" or "finitely" many roots?
Comment: The only case where a polynomial has infinitely many roots is where the polynomial is equal to zero everywhere. Then every number is a root. If a polynomial is not constant zero then it has finitely many roots.
Answer: A polynomial of degree $d$ has at most $d$ differents roots : indeed, by euclidian division you get :
$$\alpha\text{ is a root of }P\iff(X-\alpha)\mid P$$
 Here you can see that the degree $d$ of your polynomial can't be in $\mathbb{N}$ because $1,2,3,...,d-1,d,d+1$ will be $d+1>d$ different roots : so the only choice for your polynomial is to be the zero polynomial.
Comment: To show that polynomial has at most $d$ roots doesn't require FTA. If $f$ has degree $d$ and root $a$, then $f(x)/(x-a)$ has degree $d-1$. Use induction.
Comment: Sorry I still can't see why the only choice for my polynomial is the zero polynomial
Comment: @user281392 : Yes it is indeed too strong, I will edit that. Thank you !
Comment: Han Lopez : I edited it with the advise of user281392.
Comment: @Baloown Thank you, but I am still confused with the answer, so p is the zero polynomial means p=0? But in the question it says p is polynomial with complex coefficient
Comment: Yes : note that $0=0\times 1+0\times X+0\times X^2+...$ can be seen as a polynomial with complex coefficients.
Comment: @Baloown but 0 isn't a complex number right? How can it be a complex coefficient then
Comment: $0$ is : it is $0\times1+0\times i$.
Comment: @Baloown oh yeah 0 is complex number, but I still don't see why the number of roots here matters, how is it related to the P(n)=0 for all integer n
Comment: "$P(n)=0$" says exactly that $n$ is a root of $P$. By the property I have given you in the answer (which is the more important to understand here), you can conclude.


How to migrate users from one website to another website?

Question: I created a Drupal Kickstart site and when updating I created another test site. During the updating process my older site has new members and orders how do I transfer the new database tables to reflect the new changes (members and orders) from the older site?
Answer: Users Export to export the user from your Kickstart and User Import to your new website.
For the future you should use consider using something similar to Deploy or Configuration Management so you can keep track from your changes.


How to verify a password without sending it unencrypted to a server

Question: I want to build a login application, that has a client for username and password input, as well as a server, which then recieves the username and the password.
The server should then check, wheather the password it recieved matches the already stored hash, or not.
I thought it would be smart not to send the password unencrypted (just in a String) from the client to the server, so I already encrypted it on the client side with BCrypt.
But now i have two hashes on the server-side, but BCrypt only offers the methode to compare a plaintext with a hash, not a hash with a hash.
Should I now send the password in plaintext to the server or is there a way to compare two hashes?
Thanks for your help
Answer: The simplest way would be to use TLS https://en.wikipedia.org/wiki/Transport_Layer_Security
Comment: And then just send the password in plain text?
Comment: It's not plaintext if it's sent via TLS
Answer: "Challenge-Response" is the term you should google for.
The Principle:
On server side you have stored a hashed (salted) password and the salt.
The client sends first the login name.
The server looks up the salt of the login in the database and sends this to the client, together with a random string (That's the Challenge).
The Client has now to calculate an encrypted password in the following way: Concatenate login password with salt and hash it. Concatenate the result with the random string from the challenge and hash it. Send the result to the server.
The server now concatenates the hashed password (stored in user database) with the random string sent together with the Challenge and calculates the hash. The result must be exactly the same as the result recieved from the client.
Comment: Had to sketch it first, but thanks for the good explanation. A simpler way than using the tls-protocol, which i have to take a look at too.
Answer: You can fetch hashed value from database and compare it with sent value
Comment: What exactly do you mean?


Types of WCF Service (ajax, data service, standard service etc)

Question: Can anyone point me in the direction of a comparison of the "standard" types of WCF Service as seen in VS2010 "Add New Item"?
e.g.
WCF Service
AJAX-enabled WCF Service
WCF Data Service
What each one is targetted for/when you would use it?
Advantages/disadvantages etc?
Can you add multiple endpoints and re-use the same service for combinations/all three?
Answer: Hope this helps: http://msdn.microsoft.com/en-us/library/bb907578.aspx
Rob
Comment: I was unable to tell what the difference between the plain vanilla [WCF Service] was compared to [WCF Data Service] or [AJAX-enabled WCF Service].


"Unexpected data found" no retorno da função

Question: Tenho uma função que deveria retornar uma json a partir de uma collection gerada por uma query builder.
    $today = Carbon::today()->format('Y-m-d');
    $tasks = Task::where('date', $today)->whereHas('collaborators', function($q) {

        $q->where('collaborator_id', '=', Auth::user()->id)
        ->where('allocation', '=', 1)
        ->where('accepted', '=', 1)
        ->where('confirm_allocation', '=', 1);

    })->with('event')->get();

No entanto sempre que tento retornar o resultado com:
    return response()->json($tasks, 200);

Aparece-me um erro "Unexpected data found"

Ao fazer um  Dump and Die na variável $tasks retorna-me os resultados previstos.

Ou seja, o retorno do json é faz surgir o erro "Unexpected data found".
Caso eu retire a relação da query, ou seja o with('event'), os resultados já são retornados correctamente e no formato json, mas preciso mesmo que a relação seja retornada também.
Answer: Retirei do Model Event
protected $dateFormat = 'Y-m-d H:i:s';
protected $dates = ['date_time_init', 'date_time_end'];

Estas linhas de código faziam conflito com
public function getDateTimeInitAttribute($value)
{
    return $this->attributes['date_time_init'] = Carbon::parse($value)->format('d-m-Y H:i');
}

public function getDateTimeEndAttribute($value)
{
    return $this->attributes['date_time_end'] = Carbon::parse($value)->format('d-m-Y H:i');
}

public function setDateTimeInitAttribute($value)
{
    return $this->attributes['date_time_init'] = Carbon::parse($value)->format('Y-m-d H:i:s');
}

public function setDateTimeEndAttribute($value)
{
    return $this->attributes['date_time_end'] = Carbon::parse($value)->format('Y-m-d H:i:s');
}

public function setCreatedAtAttribute($value)
{
    return $this->attributes['created_at'] = Carbon::parse($value);
}

public function setUpdatedAtAttribute($value)
{
    return $this->attributes['updated_at'] = Carbon::parse($value);
}


How to shift a column in a pandas dataframe

Question: I have a dataframe, and I need to shift all the elements in it by 1, and I've tried using .shift() but I keep getting a SettingWithCopyWarning exception. I first tried both
df['x'] = df.x.shift(1)

and
df['x'] = df['x'].shift(1)

some quick research told me that using .loc() should fix my issue, but I tried to do
df['x'] = df.loc[:, "x"].shift(1)

but it still raised the same error SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
Why is it still raising an error?
Printing df.loc[:, "x"] tells me it is the correct series, and I used the recommended function to retrieve it.
Edit: I tried doing this:
df.loc[:, 'x'] = df.loc[:, "x"].shift(shift)

and it still raised the same error
Another Edit:
x= df.x.shift(1)

works fine, but when I try to assign x back to the df['x'] using either loc() or normal indexing, it still raises the same error. I read the pandas documentation on this warning and don't understand why this is still happening.
Comment: Does this answer your question? [How to deal with SettingWithCopyWarning in Pandas](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas)
Comment: @KristianCanler I tried using .loc as recommended in that thread and received the same error
Comment: Sorry, that's an automatic comment when the question is reported as duplicate. But you should read the links in that answer (especially dataquest and realpython) because this is an important warning to understand. Adding `.copy()` can be another get around, but the real goal to to avoid repeated slicing assignment.
Comment: In this case I don't think we can really say what's happening specifically without a [example]
Comment: @Monte - Need `copy` for filtering before your code, not `loc`. If pass code to question before `df['x'] = df.x.shift(1)` I can explain more
Answer: You have another df when you filter to get your df adding copy
df = somedf[condition].copy()


Why do complex roots come in pairs?

Question: First time using this website so excuse me for using the body like this.
Comment: See https://en.wikipedia.org/wiki/Complex_conjugate_root_theorem
Comment: Welcome to math.SE. Please see [how to ask a question here](http://math.stackexchange.com/help/how-to-ask)
Comment: Since people will make some efforts answering it is considered to be polite if a question makes some efforts too in providing more details and showing more context that help others to identify the source of the problem. For example, are you talking about polynomials or some other functions? Are coefficients supposed to be real or complex? etc
Comment: The question is unclear. The complex roots of $x-i=0$ do not come in pairs.
Comment: why the downvotes? because granted he can improve the question but it is his first time. His question is not a clear homework question, or even a throw away question. It is a concept he requires.
Comment: *Why do complex roots come in pairs ?* - Because an evil seldom comes alone ?
Comment: Sorry for the vagueness of my question guys. I hadn't heard of complex coefficients. I'll research my questions more in the future.
Answer: Complex roots of polynomials with real coefficients come in conjugate pairs because otherwise they wouldn't have real coefficients! Suppose a polynomial $p(z)=a_{0}+a_{1}z+ \ldots + a_{n}z^{n}$ has a complex root $\alpha$. Then $$\overline{p(\alpha)}=\overline{a_{0}+a_{1}\alpha+ \ldots + a_{n}\alpha^{n}}=\bar{a_{0}}+\bar{a_{1}}\bar{\alpha}+ \ldots + \bar{a_{n}}\bar{\alpha}^{n}\\=a_{0}+a_{1}\bar{\alpha}+ \ldots + a_{n}\bar{\alpha}^{n}=\bar{0}=0$$
So $\bar{\alpha}$ is also a root.
Comment: Does it? $P(X)=X-(1+i)$ has only one root, $1+i$, whose conjugate is not a root...
Comment: This also works if you have complex coefficients, you know.
Comment: You have lines over some expressions and certain variables. What do those mean?
Comment: That's complex conjugation
Comment: @Singulis: $\bar{z}$ is the conjugate of $z$ (that's the standard notation): if $z=a+ib$ with $a,b\in\mathbb{R}$, then $\bar{z}=a-ib$.
Answer: The key is that complex conjugation (i.e the operation $\ z=a+bi\ \mapsto\  a-bi=\bar z\ $ where $a,b\in\Bbb R$) respects addition and multiplication.
So that, whenever $z$ satisfies $a_0+a_1z+a_2z^2+\dots=0$, taking the complex conjugate of both sides, if all coefficients are real $(a_k\in\Bbb R) \implies a_k=\bar{a_k}$, we will have
$$0=\bar 0=\overline{a_0+a_1z+a_2z^2+\dots}=\\
=\bar{a_0}+\bar{a_1}\bar z+\bar{a_2}\bar z^2+\dots=\\
=a_0+a_1\bar z+a_2\bar z^2+\dots$$
i.e. $\bar z$ is another root.
Answer: Because, in the complex plane, a root is also a rotation. In general, think of a complex number in polar representation:
$$z=r\cdot e^{i\phi}$$ Then taking the square root gives you:
$$\sqrt{z} = z^{-1/2} = \sqrt r\cdot e^{i\phi/2}$$
But - the angle $(\phi/2+\pi)$ also satisfies the demands for being half the angle $\phi$, because:
$$2\cdot(\phi/2+\pi) = \phi+2\pi = \phi$$
It all stems from the fact that rotations live in on a circle, which is a cyclic space (obviously). When you do arithmetic on angles you must remember that a $full\ rotation = no\ ratation$, and from that it follows that when solving simple equations like $2x=y$ you end up with more than a single solution.

Why do complex roots come in pairs?


Local Encrypted Incremental Space efficient Backup Solution

Question: I am a student and for my hobby projects i managed to buy a vps. Problem is how can i backup that? I have got a webdav account somewhere (and they allow to store any file types for any purposes as long as copyrights are not being voilated) and i have mounted webdav as /media/webdav.
My usual tbz2 backups site is around 300-400M and incremental backups vary from 20M to 120M. This is when i do them manually and download them on my laptop.
I am using Debian Lenny on the server and i am looking for a backup solution that makes full and incremental backups with encryption as i would store the backups on the webdav mount.
I tried duplicity, but its a bit complexed, but i have disabled root's ssh, and only root has access to most of the folders that i want to backup. So if possible please suggest a solution.
I have looked over rdiff-backup, rsnapshot, ftplicity, box backup and some others.
Finally decided Duplicity as the tool to go with.
Answer: I don't know if it will work with WebDAV, but you can try Box Backup. It does encrypted, incremental, space efficient backups quite well in our testing.
Comment: That looks like a very interesting project. Might have to try it myself.
Comment: seems complex setup to me, even though under debian i install server and client using apt-get...
Answer: Brackup might fit your requirements.
http://code.google.com/p/brackup/
Comment: From http://www.openfusion.net/tags/brackup : "as soon as you start trying to brackup trees on any size you find that brackup aborts if it finds a file has changed between the time it initially walks the tree and when it comes to back it up."

And he solved it using LVM's snapshot, too bad, i dont have LVM.


k-uniform, k-regular set family. Prove there is a coloring that leave no member monochromatic.

Question: Let F be a set family.
F is k-uniform: each member of F has k points.
F is also k-regular: every point occurs in exactly k members of F.
Show that for k $\geq$ 10 there is a way to color all the points with 2 colors such that no member of F is monochromatic.
Answer: Let $X=\bigcup F$. Color the points of $X$ randomly black and white, independently and with each color having probability $\frac12$ for each point. For $f\in F$ let $A_f$ be the event that $f$ is monochromatic; clearly $$\Pr(A_f)=\frac1{2^{k-1}}$$ for each $f\in F$. Construct a graph $G$ with vertex set $V(G)=F$ by making $\{f_0,f_1\}$ an edge of $G$ iff $f_0\cap f_1\ne\varnothing$: $E(G)=\big\{\{f_0,f_1\}\in[F]^2:f_0\cap f_1\ne\varnothing\big\}$. Fix $f\in F$; each of the $k$ elements of $f$ belongs to $k-1$ other members of $F$, so $\deg_G(f)\le k(k-1)$. Thus, if $d=\max\{\deg_G(f):f\in F\}$, we have
$$\frac1{4d}\ge\frac1{4k(k-1)}\ge\frac1{2^{k-1}}=\Pr(A_f)$$
for each $f\in F$, provided that $k\ge 10$. Now use the (unnumbered) lemma on page $616$ of Erdős & Lovász, Problems and results on 3-chromatic hypergraphs and some related questions, available as a PDF here. (It's on the eighth page of the PDF.)
Comment: @Luffy: For $k=1,\ldots,n-1$ let $P_k=\{k,k+1\}$, and let $P_n=\{1,n\}$; then $\{P_k:k\in[n]\}$ is a $2$-regular family of $2$-subsets of $[n]$ (for $n\ge 3$). Another example is $\{[n]\setminus\{k\}:k\in[n]\}$; this is an $(n-1)$-regular family of $(n-1)$-subsets of $[n]$.
Comment: Brian, can you give me an example of what a regular set is? Also, a regular intersecting family?
Comment: @Luffy: Not without more context than that, I'm afraid.
Comment: A family $F$ of subsets of $X$ (where $X$ is an n-set) is regular if every point lies in a constant number $r$ of elements of $F$.


Redirect removing sufixes after 3 URL segmentes in Nginx

Question: This is the URL that I need to remove the last segment (?order=asc)
https://www.example.com/segment_1/segment_2/page/the_number_goes_here/?order?asc

This is the code I'm trying:
location ~ ^/([^/]+)/([^/]+)/page/(\d+)/ {
    try_files $uri $uri/ /index.php?$args;
    # Capture the 2 segments and page number
    set $segment_1 $1;
    set $segment_2 $2;
    set $page_number $4;
    # Redirect without the sufix
    rewrite ^ /$segment_1/$segment_2/page/$page_number/ permanent;
}

Answer: The location and rewrite directives process the normalised URI which does not include the query string (which you have called "the last segment").
The query string is available in the $args variable.
The rewrite directive can optionally remove the original query string by appending a ? to the end of the replacement value.
For example:
rewrite ^ /$segment_1/$segment_2/page/$page_number/? permanent;

Also, in your example code:

The page number is captured in $3 and not $4
The try_files statement is probably redundant as Nginx will always evaluate the set and rewrite directives first.

is evaluated first


Can multiple instruments make a chord?

Question: This seems in line with the definition of chord
"three or more musical tones sounded simultaneously"
But do people commonly refer to a chord coming from multiple instruments? Then any set of instruments can make chords, whether the same or different types even.
Comment: It's almost certain that chords were produced in this way (probably with more than one voice) before instruments which could produce chords on their own.
Comment: Well, not strictly *any* set of intruments - you won't get a chord out of a bunch of unpitched percussions - but most you can hear in practice.
Answer: Yes, any collection of instruments can make a chord. "Chord" just means several simultaneous, different pitches, and the various terms like "major" and "minor" designate particular characteristic chord constructions. The concepts are instrument independent.
Comment: Including, I would note, the human voice.
Comment: @phoog ... and the [muppophone](https://www.youtube.com/watch?v=JaL6s4eNNBg)
Comment: @phoog and hand bell choirs.
Comment: Simultaneous pitches - unless its a broken chord.
Answer: It could be added, if it's not obvious, that voices can also make chords.
Comment: @ConnieMnemonic [One randomly found example.](https://www.youtube.com/watch?v=gpLsLuuVmtk) It's very common for choirs and vocal groups to do this.
Comment: Oh, and [one notorious example](https://www.youtube.com/watch?v=b-VAxGJdJeQ&t=86s)... sorry about the sopranos being out of key.
Comment: @ConnieMnemonic Yeah, the technique is called overtone singing and let's you project several tones simultanously, eg. [this](https://www.youtube.com/watch?v=vC9Qh709gas)
Comment: Could you give an example?
Comment: OH, thank you! Sorry, I think I misinterpreted your answer, you mean voices like "a collection of human voices", not "a single voice making a chord". I was thinking that'd be some impressive singing. I think I've seen some people who can do similar/weird stuff but still curious. Thank you!
Answer: Yes, absolutely! Stuff like, say, a whole symphonic orchestra playing the final chord of a piece is something you can definitely hear about using just these words. First violins contribute one tone, flutes another, and so on - all together, it's a chord.
Answer: One concrete example is with complex chords on a guitar, e.g. 11th or 13th.
They might be hard or impossible to play (there are 7 notes in a 13th chord, and only 6 strings). One solution is to leave out the root. It will most probably be played by the bass or piano, and the whole will sound like a chord.
Answer: Certain instruments, like horns and woodwinds, can only sound one note at a time. To produce a chord, several of them would need to sound the component notes simultaneously.
Comment: It's probably reasonable to say that *most* instruments can only sound one note at a time. Keyboard and string instruments are the exceptional ones.

Can multiple instruments make a chord?


Curve Fit with logarithmic Regression in Python

Question: I need to find a model which best fits my data. It looks like this:

So I thought about logarithmic regression.
But when I try to make a simple fit in python I get the following result:

My code for now looks like this:
import csv
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
from scipy.optimize import curve_fit
from pylab import rcParams
rcParams['figure.figsize'] = 20,10

plt.close('all')

# read the data
with open('car-2015-10-16-12-19-23.log.csv','r') as f:
    reader=csv.reader(f,delimiter=',')
    next(reader, None)
    data=np.array([tuple(row[0:]+row[:1]) for row in reader],dtype=None)

# print(mc.report_memory())

# to test some time-windows
#data = data[500:1500]

# delete Fuel Status because sometimes there is NODATA or garbage
data = np.delete(data,np.s_[::5],1)

# convert last index to microseconds
for dt in data:
    ms = datetime.strptime(dt[-1], '%H:%M:%S.%f')
    dt[-1] = ms.microsecond + ms.second * 1000000 + ms.minute * 60 * 1000000 + ms.hour *3600 * 1000000
    dt[1] = float(dt[1]) * 1.60934

# font style
labelfont = {
        'family' : 'sans-serif',  # (cursive, fantasy, monospace, serif)
        'color'  : 'black',       # html hex or colour name
        'weight' : 'normal',      # (normal, bold, bolder, lighter)
        'size'   : 36,            # default value:12
        }

titlefont = {
        'family' : 'serif',
        'color'  : 'black',
        'weight' : 'bold',
        'size'   : 40,
        }

# delete garbage
data = np.delete(data, 0, 0)
data = np.delete(data, 0, 0)

# title and labels
plt.title('Throttle - Load Relation', fontdict=titlefont)
plt.xlabel('Throttle in %', fontdict=labelfont)
plt.ylabel('Load in %', fontdict=labelfont)

# adjust fontsize of ticks
plt.tick_params(axis='both', which='major', labelsize=30)
plt.tick_params(axis='both', which='minor', labelsize=30)

# return data as float
data = data.astype(float)

# just for regression
xdata = data[:,2]
ydata = data[:,3]

# logarithmic function
def func(x, p1,p2):
  return p1*np.log(x)+p2

popt, pcov = curve_fit(func, xdata, ydata,p0=(1.0,10.2))

# curve params
p1 = popt[0]
p2 = popt[1]

# plot curve
curvex=np.linspace(15,85,1000)
curvey=func(curvex,p1,p2)
plt.plot(curvex,curvey,'r', linewidth=5)

# plot data
plt.plot(data[:,2],data[:,3],'x',label = 'Xsaved')

plt.show()

The point is that both x and y can only be max 100% therefore I decided to try with logarithmic regression.
If you want you can get the data from
https://drive.google.com/file/d/0B7s23N5eDcceR00yUDZWUC1zWE0/view?usp=sharing
EDIT:
To better explain what I'm looking for:

Where btw. how to start here then?
Comment: What is your question? Looking at your code you may want to use `pandas.read_csv` instead.  It's a lot easier to use than the `csv` module.
Comment: You could try fitting a spline or loess curve. https://en.wikipedia.org/wiki/Scatterplot_smoothing
Comment: It looks to me like you might consider splines; natural regression splines might work well  (with knots about every 5% at the left end where there's lots of data and maybe every 10% above x=40-ish); alternatively cubic smoothing splines might work well enough.
Comment: Wow, isn't it amazing that nobody suggested the user to first use logarithmic and double logarithmic coordinates to actually check whether the function is a power or a logarithm? o_O
Comment: my question is if I could fit the curve more...
I think it's not the best fit or my model is not enough...
Comment: just tried with polynomial of 4th degree and got better fit. but I'm sceptical if I'm right
Comment: should I filter the data (f.e. after 30% of throttle there are some exceptions) and so get logarithmic function of better fit?
Comment: Your drawn fit (the green curve) makes little sense. Consider where you have the green curve when throttle is below 20 and compare it with the mean at given values there. E.g. look at the data in this [thin strip](http://i.imgur.com/ljdb32j.png) (marked in red), somewhere around throttle=17%. Your green line has the mean way down at 10, possibly lower (i.e. below *all* the data in that region except for the low outliers). The curve there should be somewhere up around the interval I marked in brown -- up around load=15%. Your curve is far below E(Y|X=x) there & also below the conditional median
Comment: @Glen_b: you're right without any doubt. the drawn curve should only be the visualisation of what I'm thinking about. At the moment I got quite good result with 3th or 4th degree polynomial. as the ranges are limited to 100 I want to go on with testing another data-samples if it fits in what I have.
Comment: @Glen_b: just read about splines and I agree with you that it will make sense. do you know about good examples with python? I get  enough with R but it will be difficult for me to switch at the moment
Comment: @x4k3p Sorry, I know only a little python
Comment: ok, I think I will start with patsy
Comment: what you are looking (the green lines you drew in the picture) for is a piecewise linear function, why not fit with piecewise linear function? something like [this](http://www.r-bloggers.com/r-for-ecologists-putting-together-a-piecewise-regression/)
Comment: Original question was off-topic in focus on Python, but as the central point seems to be how to model such data, this has survived.
Answer: (Post edited based on comments - thank you for the corrections)
Your data looks very much like the data I see every day as a biochemist.  I see no reason that your data should look like enzyme reaction curves, but it seems that your data may be modeled well by a fit that works well for enzyme reaction curves, generalized logistic function:
https://en.wikipedia.org/wiki/Generalised_logistic_function
(Since you appear to be using python, here is a modified form of the generalized logistic, expressed in a form usable for python scripting)
def fivepl(x, a, b, c, d, g):

return ( ( (a-d) / ( (1+( (x/c)** b )) **g) ) + d )

In this variant of the generalized logistic, here are what the variables represent:
$a$ the lower asymptote
$b$ the Hill coefficient, i.e. the steepness of the slope in the linear portion of the sigmoid
$c$ is related to the value $Y(0)$, and is the inflection point of the curve, i.e. the $x$ value of the middle of the the linear portion of the curve
$d$ the upper asymptote
$g$ asymmetry factor - set to 0.5 initially
Comment: Looks helpful, but it's customary to state the software you are using and not to assume that people use whatever you use. I guess the last  right parenthesis closing `return()`. comes too early. The implication presumably is to reach for some nonlinear least squares function; yours being `curve_fit`.
Comment: I guess you're using Python like the OP. Convention here is to focus on statistics, not code in any particular software.
Answer: Maybe a bit late, but this helped me:
instead of p1*np.log(x)+p2, try:
def func(x, a, b,c):
    return a*np.log2(b+x)+c

without the parameter b, I get the same problem than you but with it, it fits well.
This may then be used with scipy's curve fit:
popt, pcov = curve_fit(func, x, y)

And plotted
plt.figure()
plt.scatter(x, y, label="Original Noised Data")
plt.plot(x, func(x, *popt), 'r-', label="Fitted Curve")
plt.legend()
plt.show()

You can print popt to get the values of a,b,c.
Comment: Could you expand your answer a little bit more? *Why* do you suggest using such function?
Comment: @Sam What's your source/ref for the model design?
Answer: Not knowing which physical process is working in the background.
I would suggest to use two functions which cover different areas of the data.

My python skills are not sufficient to solve this task nicely, but maybe this is a beginning.
I fittet your proposed function to two sections of the  data. But (1) I do not know how to blend the functions nicely and (2) if it would be possible to plug this approach in the optimiser as well.

However, maybe another problem is the distribution of data points. Since you have a lot more data points for the low throttle area the fitting algorithm might weigh this area more (how does python fitting work?).
To prevent this I sliced the data up into 15 slices average those and than fit through 15 data points.
Averaging the slices and tweaking the model (by scaling and moving the data, be aware that most of the functions look very nice in the range from 0 to 1, but real data usually lives somewhere else.)
p1 * np.log( (x-p3)/p4 ) + p2

I end up with:

The green dots are the slice-averages. The parameters are:
[18.26324409 -34.75603362  15.80303842   0.10152119]

Comment: could anyone help me with my figure. I don't really know why they look so crappy/blurry
Answer: To me that looks like a square root function, If you use a regression with the order of 1/2 that might give you a greater correlation.


Difference between '?' and 'help()'

Question: What is the difference between '?' and 'help()' in Python Jupyter Notebooks. E.g.
import scipy
help(scipy)
?scipy

Comment: I see a difference. In the specific case I am currently looking at, help() returns something while ? does not.
Comment: Are you using Jupyter notebook?
Comment: Yes I am using Jupyter notebooks.
Comment: And do you see any difference? If not, what exactly is your question about?
Answer: Documentations are always a good place to start. :)
So, ? which is a magic, is equivalent to %pinfo as specified in the IPython docs.
Whereas help() is a built-in Python function that returns the documentation associated with a Python Object.
#p.s. Jupyter uses an IPython (or Interactive Python) kernel, which supports the magics.
Answer: In Python, the ? symbol is used to display the documentation string of an object, while the help() function is used to display the documentation for a module, function, or object.
Here is an example of how to use these two methods in Python:
>>> x = [1, 2, 3]

>>> x?
Type:        list
String form: [1, 2, 3]
Length:      3
Docstring:
Built-in mutable sequence.

>>> help(list)
Help on class list in module builtins:

class list(object)
    list() -> new empty list
    list(iterable) -> new list initialized from iterable's items

    Methods defined here:

    __add__(self, value, /)
        Return self+value.

    __contains__(self, key, /)
        Return key in self.

    __delitem__(self, key, /)
        Delete self[key].

Comment: You say "In Python, the ? symbol is used [...]". That's not correct. The `?` is not valid in Python. It might be used from some REPLs like IPython or Jupyter.

Difference between '?' and 'help()'


How to inject remote CSS file onto current tab? [Chrome Extension Manifest V3]

Question: Does anyone know how to inject a remote CSS file onto current tab without downloading it and adding to my extension package?
Here's the code I'm running:
<code>chrome.tabs.query({active: true, currentWindow: true}, function(tabs)
{
    var activeTab = tabs[0];
    chrome.scripting.insertCSS
    ({
        target: { tabId: activeTab.id },
        files: ['https://fonts.googleapis.com/css2?family=Acme']
    });
    chrome.scripting.insertCSS
    ({
        target: { tabId: activeTab.id },
        css: 'body{font-family:Acme !important;}'
    });
});
</code>
I get the following error:

Uncaught (in promise) Error: Could not load file: 'https://fonts.googleapis.com/css2?family=Acme'.

Here's sections of the manifest I've added without luck:
<code>"host_permissions":
[
    "https://fonts.googleapis.com",
    "https://fonts.gstatic.com"
],
"web_accessible_resources":
[
    {
      "resources": [ "css2?family=Acme" ],
      "matches": [ "https://fonts.googleapis.com/*" ]
    },
    {
      "resources": [ "MwQ5bhbm2POE2V9BPQ.woff2" ],
      "matches": [ "https://fonts.gstatic.com/*" ]
    }
]
</code>
Answer: Here's one workaround for my problem that actually gives me the result I'm looking for by storing the content of the remote CSS file into a string first. It's a bit hacky and relies on jQuery. Would still like to find a better way.
<code>$.ajax
({
    type:'GET',
    url:'https://fonts.googleapis.com/css2?family=Acme',
    success: function(fontCss)
    {
        chrome.tabs.query({active: true, currentWindow: true}, function(tabs)
        {
            var activeTab = tabs[0];
            var customCss = 'body{font-family:Acme !important;}';
            var fullCss = fontCss + customCss;
            chrome.scripting.insertCSS
            ({
                target: { tabId: activeTab.id },
                css: fullCss
            });
        });
    }
});
</code>
Comment: @wOxxOm Is this for manifest v3 only? Does this mean AJAX requests to remote resources are not allowed as well? In developer mode, it's working fine. Also, I've seen some extensions that definitely inject remote resources onto a page one way or another.
Comment: @wOxxOm Also, from what I read, remote scripts are forbidden from being injected on the page for security reasons, but I haven't read anything about HTML or CSS being forbidden.
Comment: You're right, they don't enforce it yet for MV2.
Comment: @wOxxOm Thanks for the replies. I might have wasted days of coding for nothing assuming you're right. The documentation is really ambiguous in a lot of areas. My extension is working great in developer mode without errors so assumed it would pass inspection.
Comment: @wOxxOm Just to follow up with this. I submitted the extension for publishing to the web store and it was approved already. I haven't actually published it because it's not 100% done.
Comment: The quick approvals on CWS may be semi-automated so the problem may still arise when the extension finally reaches a human review.
Comment: @wOxxOm Is there such a step? once the webstore approves it I think that's the end of the journey. You are ready to publish it basically. Am I correct?
Comment: There must be, since they can't physically perform human review on all extensions quickly.


Forward a port via UPnP in Python

Question: I am making a Python application that requires the user to have a port forwarded to his computer in order to communicate with a server or another user. The current implementation works quite great, yet the only thing is that the person who's running the file must forward the port to the local IP manually. I want to automate this. He picks a port, script checks if it can be forwarded, then it forwards it. If it can't, it handles the error respectively.
I've looked into some libraries that claim they can do this in pure Python (since I will need to compile to .exe's [...] after finishing) but didn't manage to find something useful. If you could provide me with a code sample on how to attempt to forward a port and handle success/fail respectively, that would be great.
Thanks in advance for your time.
P.S.:It's Python 2.7.X that I am targeting
Comment: P.S.: The app will be developed on a Mac and later tested on a Windows machine. If it works, it will be compiled and deployed to all platforms.
Comment: I was looking into the same thing, unfortunately I found only [Brisa](http://stackoverflow.com/q/4742001/897968) and [MiniUPNP](http://stackoverflow.com/a/10440556/897968) so far... there surely must be a more current/compact/pure Python implementation? Anyone?
Comment: Looking for cross-platform solution here.
Comment: @AbhishekBhatia I will be trying the one answer provided and in general work on this for a few minutes. Since this was almost 3 years ago I do not remember exactly what I did. I probably abandoned the port forwarding idea. Let me know if you come up with something too.
Comment: @DaKnOb Hi, thanks. I tried this https://stackoverflow.com/questions/36123075/port-forwarding-in-python-to-allow-socket-connections . But it doesn't seem to work though. Let me know if possible.
Answer: Looks like there are a few options:

Miniupnp has python bindings
Python bindings for GNUPnP
miranda-upnp is pure python

There is a nice example of the python bindings for GNUPnP being used to open ports on a router here.
In that example the lease time is set to 0, which is unlimited. See here for the definition of add_port.
A simple example might be:
<code>#! /usr/bin/python
import gupnp.igd
import glib
from sys import stderr

my_ip = YOUR_IP

igd = gupnp.igd.Simple()
igd.external_ip = None

main = glib.MainLoop()

def mep(igd, proto, eip, erip, port, localip, lport, msg):
    if port == 80:
        igd.external_ip = eip
        main.quit()

def emp(igd, err, proto, ep, lip, lp, msg):
    print >> stderr, "ERR"
    print >> stderr, err, proto, ep, lip, lp, msg
    main.quit()

igd.connect("mapped-external-port", mep)
igd.connect("error-mapping-port", emp)

#igd.add_port("PROTO", EXTERNAL_PORT, INTERNAL_IP, INTERNAL_PORT, LEASE_DURATION_IN_SECONDS, "NAME")
igd.add_port("TCP", 80, my_ip, 8080, 86400, "web")

main.run()
</code>

Forward a port via UPnP in Python


How to create list in sql based on condition

Question: I am working on a SQL query, where considering the following dataset:

clientid
visited
channel
purchase
visit_order

123
abc133
google
0
1

123
efg446
facebook
0
2

123
gij729
instagram
1
3

456
klm183
google
0
1

456
nop496
linkedin
0
2

456
qrs729
pinterest
1
3

456
tuv894
google
0
1

456
wyz634
instagram
0
2

I want to get the following output:

clientid
user_journey
conversion

123
google, facebook, instagram
1

456
google, linkedin, interest
1

456
google, instagram
0

where the user_jorney column is composed of the channels that participated in a conversion journey. Note that the journey of users who, until then, have not made a purchase is also built.
Looking for commands that can help with this task, I found concat_ws, where I wrote the code below:
select
  clientid,
  concat_ws(',', collect_list(channel)) as user_journey,
  sum(purchase) as conversion
from table_name group by  clientid;

I get this result:

clientid
user_journey
conversion

123
google, facebook, instagram
1

456
google, linkedin, pinterest, google, instagram
1

Now I'm trying to consider a condition to get the desired result but so far I haven't been able to find.
Could you help me how can i solve this task?
Answer: Note: you are missing very important data point that most likely available in your data - which is timestamp or data or something that allows determine global order of visits
Having this in mind consider below (ts is reference to the missing in your question column)
select clientid,
  string_agg(channel, ', ' order by visit_order) user_journey,
  sum(purchase) as conversion
from (
  select *, countif(visit_order = 1) over(partition by clientid order by ts) grp
  from your_table
)
group by clientid, grp

if applied to sample data in your question - output is


How to setup "drush @mysite.env ssh" command?

Question: I am trying to create a custom drush command in which I have to access another remote environment and run some drush commands there. The problem is I am unable to ssh to another environment e.g, "dev" etc.
I am able to run
drush @mysite.env status

but when I try
drush @mysite.env ssh

then I get this error
PTY allocation request failed on channel 0
Command not supported as typed.
An error 255 occurred while running the command `ssh -p 2222 -o AddressFamily inet"

"pantheon.aliases.drushrc.php" is placed in "~/.drush" folder.
Please let me know what's missing.
Thanks
Answer: Pantheon does not support ssh access to your server. Write your own Drush command, place it in the drush folder at your repository root, and call it with drush @mysite.env mycommand.


Como pegar o primeiro elemento diferente de zero?

Question: estou com o seguinte problema, tenho uma matriz e quero percorrê-la e pegar a primeira linha que tenha todos os elementos diferentes de zero. Por exemplo:
a = matrix(c(rep(0,4), 1:16), ncol = 4, byrow = T)
b = matrix(c(1:4, rep(0,4), 5:16), ncol = 4, byrow = T)
d = matrix(c(1:8,rep(0,4) , 9:16), ncol = 4, byrow = T)

A resposta de "a" seria a linha 2, a resposta de "b" seria a linha 1 e a resposta de "d" seria linha 1.
Desde já agradeço.
Answer: Tente a seguinte função:
primeira_linha_nao_nula <- function(m){
  vetor <- apply(m, 1, function(x) return(all(x != 0)))
  indice <- NULL
  if(max(vetor) == 1){
    indice <- order(vetor, decreasing = T)[1]
  }
  return(m[indice,])
}

A primeira linha percorre a matriz e cria um vetor, chamado vetor, que é TRUE quando a linha possui todos elementos diferentes de zero.
Em seguida ela verifica se existe pelo menos um TRUE nesse vetor, para depois retornar o menor índice que é TRUE.
> primeira_linha_nao_nula(a)
[1] 1 2 3 4
> primeira_linha_nao_nula(b)
[1] 1 2 3 4
> primeira_linha_nao_nula(d)
[1] 1 2 3 4

Comment: Daniel, me expressei errado, quero que retorne os elementos da matriz, ou seja, todos os elementos da linha 2, 1 e 1.
Comment: Vou editar, mas é apenas uma linha a mais!


Integral homology of $S^{n-1}/\pi$, $H_*(S^{n-1}/\pi; \mathbb{Z}_p)$

Question: Let $p$ be an odd prime number. Regard the cyclic group $\pi$ of order $p$ as the group of $p$th roots of unity contained in $S^1$. Regard $S^{2n-1}$ as the unit sphere in $\mathbb{C}^n$, $n \ge 2$. Then $\pi \subset S^1$ acts freely on $S^{2n-1}$ via$$\zeta(z_1, \dots, z_n) = (\zeta z_1, \dots, \zeta z_n).$$Let $L^n = S^{2n-1}/\pi$ be the orbit space; it is called a lens space is an odd primary analogue of $\mathbb{R}P^n$. The obvious quotient map $S^{2n-1} \to L^n$ is a universal covering.
Now, I have two questions.

What is the integral homology of $L^n$, $n \ge2$?
What is $H_*(L^n; \mathbb{Z}_p)$, where $\mathbb{Z}_p = \mathbb{Z}/p\mathbb{Z}$?

Sorrry yet again for two rather remedial questions...
Answer: That is exactly exercise 5, page 103, of ``A concise course in algebraic topology'',  which gives a hint: mimic the analogous (cellular) computation for $RP^n$.  I won't spoil the fun by giving the answer.


Redirecionamento: Location vs Refresh

Question: Quando usar Location e Refresh par fazer redirecionamento.
    header( "Location: www.dominio.com" , TRUE , 302 )
    header( "Refresh:5; url=www.dominio.com" , TRUE , 302 )

Ambas opções produzem o mesmo resultado: redirecionamento. A diferença é que usando Location o redirecionamento é instantâneo, enquanto Refresh pode-se definir um delay para o redirecionamento.
Estou querendo compreender as diferenças entre os dois casos, quando optar por um ou outro. Já que Refresh possui a vantagem de escolher o tempo, não vejo sentido no Location.
Pode-se definir o status 3xx nos dois casos. O status-code é quem informa os casos de redirecionamento, então Location e Refresh são apenas um meio para um objetivo - são indiferentes?
Comment: Acho que essa questão não é relacionada somente com [tag:php], talvez alterar a tag para [tag:http] seja mais coerente.
Comment: Fiquei curioso agora. O refresh altera o histórico da mesma forma que o redirecionamento?
Comment: Algo relacionado está [nessa questão](http://stackoverflow.com/q/1571973), mas para falar a verdade, não compreendi bem.
Comment: Talvez [essa questão](http://stackoverflow.com/questions/283752/refresh-http-header) responda sua dúvida
Comment: Valeu @gmsantos, alterei a tag.
Answer: As duas funções servem para redirecionar/atualizar uma página, contudo eles possuem formas distintas de funcionar.
Com o Location o navegador não tem que baixar todo o conteúdo da página antes de fazer o redirecionamento. Desta forma o não haverá problemas em usar o botão de voltar do seu navegador pois a função é feita server-side.
Com a função de Refresh você estara enviando uma solicitação para o navegador ( client-side) para que atualize a página. Ou seja, o navegador ira primeiro fazer o download da página e, em seguida o tempo setado na meta tag, ira fazer o redirecionamento da página.
Alem disso, se o utilizador clicar em "Voltar" no navegador não ira funcionar como deveria, pois voltara para a pagina em que ele acabou  de sair e sera redirecionado novamente.

Location

Server-side
Não faz download do site antes de redirecionar

Refresh

Client-side
Pode definir tempo para enviar para próxima página
Pode atrapalhar o bom funcionamento do botão voltar do navegador
Faz o download do conteudo do sites antes de redirecionar

Para mais informações: http://www.w3.org/QA/Tips/reback
Comment: Perfeito! Explica muito bem cada funcionalidade. Agora está mais facil decidir quando usar cada caso.
Comment: Conferindo melhor a resposta dele vi que eu interpretei um pouco errado, agora percebi que ele se refere ao Location como o header, enquanto ao Refresh como sendo a tag `meta` enquanto eu considerava que os dois fossem o header, já que sua pergunta foi sobre os headers e não a tag `meta`. Se tratando dos headers acho errado dizer que um é server-side e o outro cliente-side, redirecionamento com o status code 3XX é considerado server-side porém depende da implementação client-side, quem realmente abre o endereço novo é o cliente
Comment: Um detalhe que acho importante também e não tem em nenhuma das respostas, o header Refresh na verdade não faz parte da especificação do HTTP/1.1, se não me engano foi criado na época do Netscape e continua sendo suportado pelos navegadores. Se você conferir no [RFC7231](http://tools.ietf.org/html/rfc7231#section-6.4.3) do HTTP/1.1 ele diz que um redirecionamento 302 por exemplo DEVE conter o campo Location, e o Refresh não está em lugar algum do RFC
Comment: @Leandro Godoy Rosa, poderia dissertar mais sobre isso?
Comment: Mas de qualquer forma acho que esta resposta não é a correta para a pergunta já que a pergunta em si foi sobre os headers Location e Refresh, e a resposta foi sobre o header Location e o uso da tag meta para fazer o refresh
Comment: @LeandroGodoyRosa eu sabia disso, mais não tinha fundamento para provar, elabore uma resposta com isso, é bem legal
Answer: Para descobrir a diferença teremos que analisar essa questão de dois pontos de vista, server-side e client-side:
Server-side
Para o servidor não tem diferença, será simplesmente mais uma resposta HTTP com  o status 302 que na especificação é status de redirecionamento. "Como assim não tem diferença ?" Isso mesmo para o servidor que importa é a requisição a resposta é de interesse do cliente(desconsiderando gateways http) então aqui percebemos que teremos que analisar esse problema do lado do cliente!
Client-side
Agora que sabemos que o problema está aqui temos que entender uma coisa sobre o cliente HTTP: Para o cliente HTTP não importa qual status e cabeçalho o servidor mande, a resposta será sempre baixada para o cliente, a responsabilidade de mandar um body ou não está no servidor, por mais que a resposta seja de interesse do cliente ele não consegue fazer isso. Sabendo disso percebemos que a diferença não está no baixar ou não o conteúdo da resposta! Por essas horas você vai perguntar: "Tá, então qual a diferença?". E eu teria que responder: "Qual o seu cliente HTTP? Um browser? Um cliente JavaScript? Um cliente PHP? Como ele está implementado?". O que é feito com as cabeçalhos da resposta é de decisão do cliente HTTP! Então sem conhecer o seu cliente não tem como saber como será o comportamento para cada resposta.
Conclusão
Percebemos que a diferença irá se manifestar na implementação do cliente HTTP, então não posso responder com precisão qual é a diferença! Mas em geral a diferença é que a header Refresh foi criada para apresentar o conteúdo da resposta antes do redirecionamento, já a header Location não apresenta nenhum conteúdo por mais que o tenha.
Comment: Mais uma ótima resposta. Só não entendi uma coisa. Refresh e Location possuem diferença entre browser e javascript, ou o caso do JS foi o um exemplo de cabeçalho de status para uma requisição assíncrona?
Comment: É que quando você realiza um AJAX por exemplo o javascript não realiza um follow redirect automático, a não ser que você implemente isso, em alguns navegadores atuais isso não é mais verdade... Então mesmo que você responda com status 302 e com a header _location_ se o seu cliente javascript não implementou o follow redirect não irá acontecer nada! Já nos browsers vai depender de cada um, cada navegador implementa do seu jeito, ainda bem que os navegadores populares seguem relativamente bem as especificações HTTP =)
Comment: Meu caso é via navegador. Obrigado pela explicação.

Redirecionamento: Location vs Refresh


Programmatically get the results of a view

Question: Using the group module in D8, I have the below view with machine name: group_members_per_group

Machine Name of:

(Member account) User: Full Name is: field_user_full_name
(Member account) User: Phone Number is: field_user_phone_number

The above view has the below results:

On cron run, and while Looping through all rows of the view results, I want to get for each row the field_user_phone_number value so I can use it with other code.
So I wrote:
function my_module_cron() {

// Get and loop through the View `group_members_per_group`
//$args = [$gid];
$view = \Drupal\views\Views::getView('group_members_per_group');
//$view->setArguments($args);
$view->setDisplay('default');
$view->execute();

// Get the results of the view.
$view_result = $view->result;

// Check if the view is not empty and return results.
if (!empty($view_result)) {

// If the view returns results...
foreach ($view->result as $row) {

// Get the full name value.
$name = $row->field_user_full_name;

// check the result output for testing only.
\Drupal::messenger()->addMessage(t($name));
  }
 }
}

However, on cron run, I am receiving the below error:

Notice: Undefined property: Drupal\views\ResultRow::$field_user_full_name in my_module_cron() (line 103 of modules\custom\my_module\my_module.module).
      my_module_cron(Object)
      call_user_func('my_module_cron', Object) (Line: 316)
      Drupal\ultimate_cron\Entity\CronJob->invokeCallback() (Line: 459)
      Drupal\ultimate_cron\Entity\CronJob->run(Object) (Line: 24)
      Drupal\ultimate_cron\Controller\JobController->runCronJob(Object)
      call_user_func_array(Array, Array) (Line: 123)
      Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber{closure}() (Line: 582)
      …
      …

Line 103 in the error is:
$name = $row->field_user_full_name;

Comment: You need to combine $view->result with $view->field, see https://drupal.stackexchange.com/questions/219475/get-result-view-with-formatter-programmattically
Comment: @4k4 I ran through this link while searching online for answers, but did not understand how to use as for my use case! Can you please give a real example on how to use this combination ?
Answer: You can not get the value of field directly from row result.
You can get it by accessing to the entity like the following:
Option1
  foreach ($view->result as $id => $result) {
    $node = $result->_entity;
    // Get the full name value.
    $name = $node->get('field_user_full_name')->value;
    // check the result output for testing only.
    \Drupal::messenger()->addMessage(t($name));
  }

Or by loop through the fields like the following:
Option2:
foreach ($view->result as $id => $row) {
      foreach ($view->field as $fid => $field) {
        if ($fid == 'field_user_full_name') {
          //Get the full name value.
          $name = $field->getValue($row);
          // check the result output for testing only.
          \Drupal::messenger()->addMessage(t($name));
        }
      }
    }

Comment: Also, wrong, as are all the answers here. These are simply describing how to get a field from the base entity returned by the View. These are not the fields of the view.
Comment: if you find those answers wrong, step up and post one correct people here just trying to help so they don't need those kind of comments
Comment: that's an odd comment. I certainly appreciate it when people post that something is wrong; that way i don't waste time trying it (as i did with all these answers). And knowing something is wrong doesn't mean you know the right answer - i am still looking.
Comment: posted correct answer below.
Answer: In my case, the '$result->_entity' was empty, so I had to get the value from the '$result->_object' :
function YOUR_THEME_preprocess_views_view__YOUR_VIEW_ID(array &$variables): void {
  foreach ($variables['view']->result as $result) {
    $node = $result->_object->getEntity();
    $title = $node->get('title')->getValue()[0]['value'];
    $body = $node->get('body')->getValue()[0]['value'];
  }
}

Comment: In fact, `$result->_entity` is empty when the preprocessed view is rendering data from a Search API index and not rendering standard content.
Comment: Thanks, that helps a lot
Answer: I had to re-ask this question to be more clear here: How to get a rendered views field value programmatically?
@4k4 pointed me towards the right solution for getting a value from a field in a view:
foreach ($view->result as $rid => $row) {
  $type[$row->nid] = $view->field['my_views_field']->advancedRender($row)->__toString();
}

Comment: Error: Call to a member function __toString() on string
Comment: Hmm, odd this is down-voted when no reason added. Not to mention it is the correct answer.


How to move live QEMU-KVM VM without libvirt?

Question: Let me start by saying that I read How to (properly) back up a live QEMU/KVM VM? But it does not seem to meet my need.
I am looking for a solution to backup AND move a few QEMU-KVM VMs without stopping them. The state do not need to be perfecting preserved (i.e. I don't care about their state after backup) but the backup must not be corrupted. I have no libvirt in the setup.
I have done so far: backup a powered off VM and move it to another host. This can be done easily by copying the .qcow2 disk images. But how can I do it without powering off VMs? Can I take a snapshot and then tell QEMU to only copy up to the snapshot? The snapshot method used is internal (using the same disk image), I would like to keep it that way.
Answer: If you want to make your life really easy and you can afford to run a testing distribution, than upgrade to Debian bookworm (it will migrate into stable at some point). That has made my QEMU/KVM life a lot easier, since bookworm comes with QEMU 6.1.
You can backup a life-VM with the simple command:
virsh backup-begin YourVM

If you have a fast disk (e.g. NVMe) that whole process will finish in less than a minute while the VM is running and not even slowing down much. You will find your Backup-VM in the default directory: /var/lib/libvirt/images/ with a Unix-timestamp added to it. Make sure you also save the YourVM.xml file in /etc/libvirt/qemu/ together with your backup for later restoration. You can simply start that image later - works like a charm for me.
You can also check the documentation here. According to the docs, backup should work with QEMU 5.2 in Debian bullseye too, but that did not work for me.


Completing a proof for a pigeonhole principle question

Question: Seventeen people correspond by mail with one another - each one with all the rest. In their letters only three different topics are discussed. Each pair of correspondents deals with only one of these topics. Prove that there are at least three different people who write to each other about the same topic.
Work I have done so far
Each person can talk to maximum $16$ people on $3$ topics $\implies$ at least one topic will be spoken about $6$ times by Pigeonhole principle.
This part is clear to me. I do not understand how to complete the proof by showing that there are at least three different people who write to each other about the same topic.
Answer: Consider the person that talks about a topic with other $6$. Take that group of people, and consider that if some pair talks about that same topic then we're done. So consider they don't talk about that topic. They're now a group of $6$ people talking about $2$ topics.
If we apply the same process you did at the start, we have that anyone talks about one of the two remaining topics at least $3$ times. If any pair of those three remaining people talk about that topic, say topic number two, we're done. If there is no pair of those three remaining people talking about  topic number two, then they're all talking about topic number three. But then we're done too, because they're a triangle of people talking about the same topic.
It's basically applying what you did three times, until you have a trivial case of a triangle.
Comment: Finally, I have grasped your solution
Comment: @SanjayAdhith Glad it helped :)
Answer: So there is someone who corresponds with $6$ other people on topic $1$. If any pair of these $6$ people correspond with one another on topic $1$, then we done. So suppose that each of these $6$ people only correspond on topics $2$ or $3$ ... Well that's a $2$ coloring of $K_6$, which must have a $K_3$, so we are done.
For more details see https://en.wikipedia.org/wiki/Ramsey%27s_theorem#Examples
Comment: What do you mean by 2 colouring of K6, which must have a K3? I am confused by that.
Comment: $K_n$ denote the graph with $n$ vertices and with every pair of vertices joined (the complete graph on $n$ vertices). We then think in terms of colouring each edge with $1$ of $2$ colours & see if there are any "monochromatic triangles".


Infimum of a function over a closed bounded interval

Question: Let $f:[a,b]\to \mathbb{R}$ be a function such that for every $x\in[a,b]$ there exists a $y \in [a,b]$ such that $|f(y)|<\frac{1}{2}|f(x)|$. What is the infimum of $|f(x)|$ on $[a,b]$?
My idea:
Suppose $\inf\limits_{x \in [a,b]}|f(x)|$ exists, then there exists $x_0 \in [a,b]$ such that $$|f(x_0)|\le |f(x)|, \hskip{0.2cm} \forall x\in [a,b]$$ Then by hypothesis, there exists a $y \in [a,b]$ such that $$|f(y)|\le \frac{1}{2}|f(x_0)|\le \frac{1}{2}|f(x)|$$
Thus, $|f(y)|\le 2|f(y)|<|f(x_0)|$, a contradiction to the infimum attained.
Are my arguments valid?
Comment: Also, $\{\lvert f(x) \rvert : x \in [a, b]\}$ is bounded below, so surely the infimum does exist. This gives you some idea as to how you should proceed.
Comment: If a function is not continuous on a closed and bounded interval, then it needn't attain its infimum. Consider $f: [0, 1] \to \Bbb R$ defined by $f(0) = 1$, and $f(x) = x$ otherwise.
Comment: @WADon $f(x)\neq 0$ for all $x$ else there would exists $y\mid |f(y)|<0$ which is not possible.
Comment: right. you cannot assume such an $x_0$ is available.
Answer: Let $\theta$ be the infimum of $|f|$ on the given interval $I$. Obviously $\theta \in [0,|f(a)|].$
Our $\theta$ must be zero, for if we suppose $\theta>0$, there must be $t \in I$ satisfying $|f(t)|<2\theta$.
Hence there exists $s \in I$ such that
$$|f(s)|\,\,<\,\,\frac{1}{2}\,|f(t)|\,\,<\,\,\theta.$$
Thus $\theta$ is not the infimum after all.


Mean square convergence not higher-order mean

Question: There are numerous examples of sequences random variables $\{X_n\}_n$ converging in mean to a random variable $X$, i.e., $\mathbb{E}[|X_n-X|]\to 0$, but not in mean square, i.e., $\mathbb{E}[(X_n-X)^2] \not\to 0$ (the converse is always true). I'm using the definitions reported in this Wikipedia article.
I would like to know whether we can come up with an example where $\{X_n\}_n$ converges to $X$ in the mean square sense, but not in the $p$-th mean, for some $p>2$.
Can we draw some additional assumptions on $\{X_n\}_n$ so that convergence in the mean square sense entails convergence in the $p$-order mean for $p>2$?
Answer: Consider a discrete random variable $\xi_{p,v}$ with parameters $p$ and $v$, which takes value $v > 0$ with probability $p > 0$, and takes zero value otherwise. Obviously, $E|\xi_{p,v}|^s = p v^s$.
Take a random variable $X$ being constant zero, and a sequence $X_n$ of random variables with the above-mentioned distribution:
$$
\begin{cases}
  X_n \equiv \xi_{p_n, v_n} \\
  p_n = \frac{1}{n^s \ln n} \\
  v_n = n
\end{cases}
$$
It is easy to calculate its mean:
$$E|X_n|^t = \frac{n^{t-s}}{\ln n}$$
So $\mathbb{E}[|X_n-X|^s] \to 0$ but $\mathbb{E}[|X_n-X|^t] \to \infty$ for any $t > s$. Now put $s = 2$, $t = 3$ here, and you are done.


Returned content not displaying

Question: I have created a custom page which has a db query to return content, which is using the pagerdefault extender on a Drupal 7 install.
However when I return the content the pager shows but not the content.  This is the code after the query:
$content = array();
foreach ($result AS $events)
{
    $content[] = '
' . $events->type . ' - ' . $events->nid . ' - ' . $events->tutorial_start . $events->event_start . '
';
}
$build += $content;
$build['pager'] = array(
    '#theme' => 'pager',
    '#weight' => 1,
);
return $build;

I can see the $content rows if I dsm $build, but not display them on the page.  How do I render them on the page?
Answer: There are two things you should know to make it works:

Your query should extend PagerDefault.
Element inside content should be Rendered Array

here is a working example adjust the code to your needs:
//- Build array
  $build = array();
  /**
   * @TODO: adjust the query to your needs.
   */
  //- Prepare the Query to Extend PagerDefault
  $query = db_select('node', 'n')->extend('PagerDefault');
  $nids = $query
    ->fields('n', array('nid'))
    ->condition('type', 'page')
    ->condition('status', 1)
    ->execute()
    ->fetchCol();
  if (!empty($nids)) {
    $nodes = node_load_multiple($nids);
    $content = array();
    foreach ($nodes as $event) {
      //- Prepare the content
      $content[] = array(
        '#type'   => 'markup',
        '#markup' => '
' . $event->type . ' - ' . $event->nid . '
',
      );
    }
    //- Add Content to the build
    $build += $content;
    $build['pager'] = array(
      '#theme'  => 'pager',
      '#weight' => 5,
    );
  }
  return $build;

Comment: Ah thank you, that makes sense.  I was looking at the blog display code where it uses $build+= node_view_multiple() which must be already formatted in a render array.

Returned content not displaying


How do I cluster/group people together given their durations for an given event?

Question: I am new to machine learning and do have a very large dataset for a set of 100 people over a period of 1 year. and the goal is to find out who are buddys based on their lunch times.
I have the following dataset:
<code>Person  StartTime EndTime Duration(dif for start and end times)
Person1 Time11    Time12   diff1
Person2 Time21    Time22   diff2
Person3 Time31    Time22   diff3
Person4 Time41    Time32   diff4
</code>
Now I would like to cluster/group people together based on their times ( with +/- 5 minutes time difference, meaning if start time and end time of person 1 is 12:00 - 1:00 PM and person 2 is 11:55 - 1:05 they fall under the same group relative to Person 1)
Answer: Measure the relative overlap of the lunch times of any two persons. I.e. if their times at a day overlap by 90%, then add +0.9 to their similarity. You could also use the square of these values.
Then cluster with hierarchical clustering using this similarity matrix.
Comment: I think this is a bit brief in its current form, though it does seem to constitute something of an answer. Is there any chance you can expand on it, either to explain in more detail how to implement it or to justify why it would be a preferable approach?
Answer: umm ...the whole point of clustering is to let the algo figure out which points in your data set "overlap" , so in your case, it would mean people who were at lunch from 1-2 pm and also folks who were at lunch from 1245 to 1:50 pm (but thats just generalization ..i can't make any definite statements without looking at your dataset). So lets take K means algo and your label is going to be "duration" (diff1,2 etc) and you specify, say 3 clusters. What thats going to do is find all durations that are in a band and get them together. So in your case say there are 3 clusters where the bands of duration are 1-1hr 15 mins ; 45 mins - 50 mins ; and 1 hr 30 to 2 hours , then you have to do nothing and just play around with K (number of clusters) , also i must mention clustering algorithms are non parametric , meaning you can't specify rules to the algo. Hope it helpds


Openstack multiple nodes: separate Glance and Keystone machines

Question: I followed the guide on hastexo to create an OpenStack Cloud. The Keystone service and the are supposed to run on separate machines. However, when I wanted to run
glance-api in the console for debugging I had the following error:

ERROR: Unable to load glance-api-keystone from configuration file /etc/glance/glance-api-paste.ini.
  Got: ImportError('No module named keystone.middleware.auth_token',)

So the Glance API isn't running. I'm running Ubuntu 12.04 LTS and Openstack Essex.
Answer: If you want to use keystone for auth, you will need to install some extra python modules. These are needed for both the keystone server and its clients.
Try running
<code> apt-get install python-keystone python-keystoneclient
</code>


Seperation of variables differnetial equation $t^2 \frac{dy}{dt} - t = 1 + y + ty$

Question: $$t^2 \frac{dy}{dt} - t = 1 + y + ty$$
Obviously this problem is meant to trick you, I am sure there is some simple solution but after 20 minutes I don't know.
How do you approach something like this>
Answer: Hint: We start with $$t^{2}\frac{dy}{dt} - t = 1 + y + ty$$
Move $t$ over to the right side and factor:
$$t^{2}\frac{dy}{dt} = (y+1)(t+1)$$
Now separate to obtain:
$$\frac{dy}{y+1} = \frac{t+1}{t^{2}}dt$$
Can you solve from here?
Comment: @PaulthePirate practice, practice, practice. Then practice some more. The more problems you do, the more problems you will become familiar with and learn how to solve, the more tricks you will learn, and the more your understanding will expand. I'm not any smarter than you, I've just done this before - so keep at it!! :)
Comment: How could you possibly know to do that? I would have spent hours on this on a test.
Comment: No more practice time :(


Ajuda lógica com PHP usando Twitter API

Question: Bom, a ideia é a seguinte, estou utilizando API do Twitter para fazer autenticação de usuários, estou utilizando o seguinte recurso: https://twitteroauth.com.
Usei este vídeo para me ajudar na autenticação, está exatamente igual: https://www.youtube.com/watch?v=D8pO7tbYlBk&t=32s
A lógica:
Todos os usuários que autorizarem o aplicativo, está sujeito a seguir e receber outros seguidores... o Twitter não fornece este recurso, mas é possível fazer utilizando PHP, Javascript, JSON etc...
Meu problema
Autentiquei alguns usuários no localhost, contas fakes, ao autenticar e autorizar o aplicativo, é salvo o screen_name em uma tabela no banco de dados, entenderam? Quero fazer tipo uma roleta, um segue o outro automaticamente.
OBS: Não encontrei nada nos termos de uso do Twitter dizendo que é proibido. (Se eu estiver errada me corrijam, por favor).
Me ajudem com a lógica?
Resumindo a pergunta: Como retornar dados do JSON com PHP e depois utilizar o ajax para roletar isto?
Comment: para manipular JSON em PHP você consegue converter para array e vice versa. Em resumo você usará [json_encode](http://php.net/manual/pt_BR/function.json-encode.php) e [json_decode](http://php.net/manual/pt_BR/function.json-decode.php)
Comment: Acho que minha pergunta pode te ajudar https://pt.stackoverflow.com/questions/208247/d%C3%BAvida-sobre-api-do-twitter?rq=1
Comment: Você pode utilizar esse método da API do twitter https://dev.twitter.com/rest/reference/post/friendships/create
Comment: Grata estudarei, se eu conseguir postarei a resposta.
Comment: Eu já vi, o que quero saber, é como os usuários se seguirem, claro quem autorizar o aplicativo.
Comment: @gmsantos, o problema é que não sei usar `JSON` com `PHP`, por isso pedi ajuda com a lógica. Se você tiver uma resposta simples mas que foque no assunto serei grata. Eu tentei fazer um método que retornasse o `JSON` em `array` foi sem sucesso.
Answer: ToS do Twitter:

Ações de seguir/deixar de seguir automatizadas: Não é permitido seguir ou deixar de seguir contas do Twitter de maneira indiscriminada ou em massa. Ações de seguir de maneira agressiva é uma violação das Regras do Twitter. Examine nossas Regras e práticas recomendadas do seguidor para garantir sua conformidade. Lembre-se de que os aplicativos que alegam conseguir mais seguidores para os usuários também são proibidos, de acordo com as Regras do Twitter.

Fonte (fim da página)

Nunca surpreenda os usuários. Se o seu aplicativo tweeta para um usuário ou realiza ações como deixar de seguir, um usuário deve iniciar isso - e não ser surpreendido.

Fonte
O Twitter considera como spam:

[...]

se você seguiu e/ou deixou de seguir um grande número de contas em um curto período, especialmente de forma automatizada (comportamento de seguir agressivamente ou rotatividade de seguidor);
se você seguir e deixar de seguir pessoas repetidamente, seja para criar seguidores ou para atrair mais atenção para seu perfil;

[...]

se você estiver seguindo de forma aleatória ou agressiva, marcando como favorito ou retweetando Tweets;

[...]

se você criar contas ou interações de conta enganosas;
se você vender ou comprar interações com contas (por exemplo, vender ou comprar seguidores, retweets, favoritos, etc.); e
se você promover ou usar serviços ou aplicativos de terceiros que dizem conseguir mais seguidores para você (como trens de seguidores,
  sites que prometem "mais seguidores de forma rápida" ou qualquer outro
  site que se ofereça para adicionar seguidores automaticamente à sua
  conta).

Fonte (fim da página)
Observe que os termos podem ser alterados frequentemente, as informações contidas aqui podem não está atualizadas de acordo, verifique os links para acessar a informação mais recente. ;)

API Pública:
O OAuth possui um endpoint para seguir outros usuários, ele é exatamente:
https://api.twitter.com/1.1/friendships/create.json

Segundo a documentação:

Allows the authenticating users to follow the user specified in the ID parameter.

Isso será utilizado para seguir outras pessoas, que você especifica o ID, ou seja informando o token da pessoa X, essa pessoa X irá seguir o ID especificado.

A questão mencionada nos comentários se refere a como fazer o login usando login/senha, ao menos essa era a dúvida desta pergunta, isto de fato não é possível usando a API Pública, por razões obvias, isto é explicado lá.
Comment: Oi, estou utilizando `friendships/create` porém obtenho o seguinte erro `public 'message' => string 'Cannot find specified user.' (length=27)`, obrigada pela sua resposta, eu entendi os termos de outra forma. mas acho que troca de seguidores não é considerado spam desde que o usuário saiba que está autorizando e concordando com os termos escritos no meu site. Você conhece outro tipo de autenticação sem ser do abrahm? serei grata se puder me ajudar. vou marcar como respondido. Não terás respostas melhores que esta.  Obrigada.

Ajuda lógica com PHP usando Twitter API


Stream video on discord programmatically?

Question: Is it possible to use the "go live" feature to stream video programmatically?
Answer: No, Discord.js (or any library, actually) doesn't support going live at this time.


Speeding up Matlab DFTs with NAG functions

Question: I want to translate some Matlab Fourier transform commands FFTs and IFFTs into NAG functions. the DFTs are:

fft(A,n)
fft(A,n,dim)
ifft(A,[],dim)

where A is a complex vector. For example fft(A,n) isc06fc(real(A),imag(A),'n',n).
Problem: I'm having trouble finding appropriate NAG functions that do the same job as fft(A,n,dim) and ifft(A,[],dim).
Context: I am using Bruno Luong's convolution function convnfft to calculate convolutions of vectors with 1 million complex elements, convnfft(A,B,'same'). Luong's function uses FFTs to quickly calculate convolutions, and I want to speed this up even more by using NAG functions.
Comment: What is your question?
Answer: Few remarks:

MATLAB uses FFTW for DFT (See MATLAB fftw()).
FFTW is considered te fastest FFT Library out there. I really doubt NAG's implementation is faster. You won't get faster FFT than what you have in MATLAB.
I see you're using Multiplication in the Frequency Domain to apply convolution in time domain. I'd just note that it is not always faster thing to do. It really depends on the length of your filter compared to the length of the signal. If the filter is short you better do it in the time domain.

Complexity in Time Domain vs. Fourier Domain of 1D Convolution
Assuming the filter is of length m and the signal is of length n (Assume n > m).
Then, Asymptotically, the complexity of Tome Domain Convolution is O(mn).
Yet, to apply convolution in Fourier Domain one must make both the filter and signal to the same length and then the complexity is O(2nlog(n)).
Hence, for cases 2log(n) > m one better use Time Domain Convolution.
Since this is asymptotic analysis in practice one need 2log(n) >> m to have a real gain over time domain convolution.
Comment: Thanks @Royi. 1. I have already investigated `fftw` but there was little or no improvement in speed. 2. Sounds interesting. Can you provide some good references for this?
Comment: Hi, Have you compared FFTW to what? If you compared to MATLAB, as I wrote above, you won't see gains as MATLAB uses FFTW when you use `fft` or `ifft`. Anyhow, you can't get faster FFT than what inside MATLAB (Maybe, just maybe, Intel MKL FFT is better on some cases).


Are there any functions involving these operators that have 'messy' undefined limits?

Question: Given ONLY the functions of addition, subtraction, multiplication, division, exponentiation, floor, modulo, summation, and the large product operator:
Can any function whatsoever be constructed that has an undefined limit purely due to extreme infinite "messiness" around the point of which the limit is being taken.
Comment: @TheGreatDuck Perhaps you could link to the definition then as a quick search doesn't find anything relevant.
Comment: Messy like $\lim_{x\to 0} \sin(1/x)$ or what do you mean?
Comment: @AlexisOlson messy limits is a well defined term but yes that is what I mean.
Comment: https://en.wikipedia.org/wiki/Oscillation_(mathematics)
Answer: Sure, there are many such functions.  A simple example is $$f(x)=\frac{1}{x}-\lfloor\frac{1}{x}\rfloor.$$  As $x$ approaches $0$, $f(x)$ oscillates between $0$ and $1$ infinitely many times.