Views 3 Relationship Handler

Question: I am trying to write a relationship handler for Views 3 in Drupal 7, but am failing even to begin.
Basically, I have a bunch of nodes of various types all with the same field attached to them. This field is a reference field linking off to a Civicrm contact - but that's not important, as essentially these fields just contain integers.
Now, let's say I have a node of content type 'story', and its reference field contains the integer, oh, let's say 55. In addition, there are nodes of content type 'news' which also contain the reference ID 55.
I want to build a block view, that sits on the story page, that reads in the node's nid as its argument, and then through a relationship it discovers all other nodes that contain the same reference integer as itself.
(In my head, the sql looks something like: SELECT n.nid FROM node n INNER JOIN node n2 ON n.reference = n2.reference [Assuming, for the sake of simplicity, the field data is kept in the node table]).
How do I create a Views handler that caters for this relationship? The only documentation I can find is the views api, which is for version 2 and not 3, and besides is utterly unhelpful. Reading various module's views handlers is not helping either, and views tutorials on the web are almost non-existent.
Comment: If you need to add a filter for field that is an entity reference you need the [Views Reference Filter](https://drupal.org/project/entityreference_filter)
Answer: Are you really sure you need a relationship handler for this?
In general the default relationship handler is already quite flexible because it uses the default join class which is really powerful.
There are some use cases for relationship handlers, for example the relation module, but this module does some vodoo stuff.
So back to the problem. It might be enought if you define 'relationship' with the right field and base field.
PS: There is a big reason why there is no documentation about api=3 because the difference to api=2 isn't big at all.
Here is an example how to define a relationship.
  $data['node_revision']['vid'] = array(
    'relationship' => array(
      'handler' => 'views_handler_relationship',
      'base' => 'node',
      'base field' => 'nid',
      'title' => t('Content'),
      'label' => t('Get the actual content from a content revision.'),
    ),
  );

Comment: Thanks for this comment! This helped me write a views contextual filter for another project, and hopefully will allow me to return to writing this relationship. I will post the code for that when it is completed.

Views 3 Relationship Handler


OpenGL ES 2.0 Failing to correctly assign the color attribute

Question: I'm struggling a bit to apply the color for my geometry. When I specify it directly in the vertex shader ("varColor = vec4(1.0, 0.5, 0.4, 1.0);") - everything is ok. But if I use color values from the "vColor" attribue - everything gets messed up.
(Added some screenshots to show what I mean)
Can someone help me to figure out what am I doing wrong, or point me in the right direction? Thanks.
Using "varColor = vec4(1.0, 0.5, 0.4, 1.0);"

Using "varColor = vColor"

Vertex shader:
precision mediump float;

uniform mat4 modelViewProjectionMatrix;

attribute vec4 vPosition;
attribute vec2 vTexCoord;
attribute vec4 vColor;

varying lowp vec4 varColor;
varying mediump vec2 varTexCoord;

void main()
{
    gl_Position = modelViewProjectionMatrix * vPosition;
    varTexCoord = vTexCoord;
//  varColor    = vColor;
    varColor    = vec4(1.0, 0.5, 0.4, 1.0);
}

Fragment shader:
precision mediump float;

uniform sampler2D Texture0;

varying vec4 varColor;
varying vec2 varTexCoord;

void main()
{
    gl_FragColor = texture2D(Texture0, varTexCoord) * varColor;
}

After shader is linked, I'm binding my attributes like this:
mMatrixMVP = glGetUniformLocation(mProgramId, "modelViewProjectionMatrix");

glBindAttribLocation(mProgramId, 0, "vPosition");
glBindAttribLocation(mProgramId, 1, "vTexCoord");
glBindAttribLocation(mProgramId, 2, "vColor");

mTexture = glGetUniformLocation(mProgramId, "Texture0");
glUniform1i(mTexture, 0);

Structure that holds my vertex information:
struct Vertex
{
   float         xyz[3];
   float         st[2];
   unsigned char color[4]; // All assigned to value of 255
};

When rendering, after vertex buffer is bound, I'm setting vertex attributes like this:
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) offsetof(Vertex, xyz));

glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) offsetof(Vertex, st));

glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (GLvoid*) offsetof(Vertex, color));

glActiveTexture(GL_TEXTURE0);
pTexture->Bind(); // Just a "glBindTexture(GL_TEXTURE_2D, mTextureId);"
glUniform1i(mpCurrentShader->GetTexture(), 0);

After this I'm binding the index buffer and calling "glDrawElements".
Then, calling glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); and disabling all attributes with "glDisableVertexAttribArray" and finally - calling glBindBuffer(GL_ARRAY_BUFFER, 0);
Comment: If I understand your screenshot correctly, you have problems with your texture coordinates rather than (or in addition to) vertex color. Actually *using* a vertex attribute in a shader will make it *active*, and will change the automatically assigned locations.
Answer: You need to make your glBindAttribLocation() calls before linking the shader program.
If you link a program without specifying the location of attributes with either glBindAttribLocation(), or with layout qualifiers in the shader code, the attribute locations will be assigned automatically when the shader program is linked. If you call glBindAttribLocation() after linking the program, the new locations will only take effect if you link the shader program again.


Functional analysis question about topological closure of a subset

Question: Let $E$ be a normed space and $F$ be a subset of $E$ and $x\in E$. Define $\text{dist}(x,F)=\inf_{y\in F}\Vert x-y\Vert.$ Show that
$$\overline{F}=\{x\in E:\text{dist}(x,F)=0\}.$$
My proof:
The topology generated on $E$ by the norm has the basis consisting of open balls
$\small B(x,\epsilon)=\{y\in E: \Vert x-y\Vert<\epsilon\}$ for all $x\in E$ and for all $\epsilon\in\Bbb R^+.$
If $x\in\overline F$, then for any $\epsilon>0$, $B(x,\epsilon)\cap F\neq\emptyset$ and so there exists $y\in F$ such that $\Vert x-y\Vert<\epsilon$. Hence, $x\in
\{x\in E:\text{dist}(x,F)=0\}.$
Conversely, if $x\in\{x\in E:\text{dist}(x,F)=0\}$, then for any $\epsilon>0$, there exists $y\in F$ such that $\Vert x-y \Vert<\epsilon.$ But that means for any $\epsilon>0$, $B(x,\epsilon)\cap F\neq\emptyset$. Hence $x\in\overline F.$ Q.E.D.
Is my proof okay? What can I add more?
Also, is the definition of infimum important here? I didn't use it.
Thanks for your reading.
Comment: The relevance of the infimum instead of minimum is that the points in the closure might not be in $F$. Just like the closure of $(0,1)$ is $[0,1]$; you don't have $0,1 \in (0,1)$ but $\text{dist}(0,(0,1)) = 0 = \text{dist}(1,(0,1))$ just as you proved.
Answer: The use of the infimum is essential to the definition $\text{dist}(x,F) := \inf_{y\in F}\|x-y\|$, because for a given $x\in E$, there is, in general, no $y_{0}\in F$ such that $\|x - y_{0}\| = \inf_{y\in F}\|x-y\|$. The existence of such a $y_{0}$ can be guaranteed when $E$ is finite-dimensional and $F$ is closed, but not in general.
You did make use of the infimum while you were using that $z\in \{x\in E:\text{dist}(x,F) = 0\}$ is equivalent to the property that for all $\varepsilon >0$, there is some $y\in F$ such that $\|z-y\| < \varepsilon$.
In addition, you should be careful writing $x\in \{ x\in E : \text{dist}(x,F) = 0 \}$, because you are using the variable $x$ both as a fixed element of $E$ outside of the set, and as a variable which ranges over all the elements of $E$ in the set.
It is also worth mentioning that this result holds in metric spaces with an essentially identical proof.


PayPal integration with ionic hybrid mobile app

Question: I am new in ionic,I try a PayPal integration in my app but i have no any success in it.
If any one knows can please describe a steps wise integration of PayPal in ionic mobile app.
Comment: Please check "How to Ask questions" on Stack Overflow. Also, Google: https://forum.ionicframework.com/t/can-we-integrate-paypal-payment-gateway-with-ionic/32956
Answer: I integrate it .. but it's not simple ...
I've used this :
<code>https://github.com/paypal/PayPal-Cordova-Plugin
</code>


Job portal using Joomla 1.5.15

Question: I want to build a Job portal using joomla 1.5. So anyone have some contents or guides from where i can start this site. And anyone can tell how to start it as i m a begineer in joomla.
Answer: There are three ways you can accomplish that.

Code the job portal component yourself. Some good MVC coding will do the trick. If youre a beginner this can be difficult.
Use a free or commercial component form extensions.joomla.org which suits your needs best.
Joomlart has done a Job board component you should check:
http://www.joomlart.com/addons/components_and_modules/ja_job_board.html
Use one of the CCK components out there to build your job board (ZOO, Flexicontent, K2)
Answer: http://www.jobberbase.com/

Job portal using Joomla 1.5.15


Netty multithreading broken in version 4.1 ? Unable to process short queries after long ones?

Question: I just want to setup a very common server : it must accept connections and make some business calculations to return the answer. Calculations can be short or long -> I need some kind of ThreadPoolExecutor to execute these calculations.
In netty 3, that we were using since a long time, this was achieved very easily, by just using an ExecutionHandler in the pipeline, before my BusinessHandler.
But now trying to setup the same thing in netty 4, i read in the documentation that ExecutionHandler was not existing anymore, and that i add to specify a EventExecutor when adding my BusinessHandler to the channel pipeline.
DefaultEventExecutorGroup applicativeExecutorGroup = new DefaultEventExecutorGroup(10);
...
ch.pipeline().addLast(applicativeExecutorGroup, businessHandler);

It works for very basic scenarios (only short queries), but not in the following one. The reason is that DefaultEventExecutorGroup will not select a free worker, but any one based on a round-robin.

A first request (R1) comes, is assigned T1 (Thread 1 of the DefaultEventExecutorGroup), and will take a long time (say 1 minute).
Then a few other queries Ri (i=2 to 10) are received. They are assigned Ti, and are also processed successfully.
But when R11 comes, it is assigned again T1, due to the round-robin algorithm implemented in DefaultEventExecutorGroup, and the query is queued after the long R1. As a result, it will not start its processing before one minute, and that is clearly an unacceptable delay. In concrete scenarios, clients never get the answer, because they time out waiting for the answer before we start the processing.
And this continue like this. One query every 10 queries will just fail, because queued after the long one in the only busy thread, while all the other threads of the Group were just idle.

Is there another configuration of my pipeline that would work ? For example, does a implemntation of EventExecutor exist that would just work like a standard Executor (select a FREE worker).
Or is it just a bug in netty 4.1 ? It would looks very strange, as this looks as a very common scenario for any server.
Thanks for your help.
Answer: From what you explained above I think you want to use UnorderedThreadPoolEventExecutor as a replacement for DefaultEventExecutorGroup. Or if ordering is important NonStickyEventExecutorGroup.


$X^2$, when $X\sim N(0,1)$ : $2\frac{\operatorname{d}F_X(\sqrt{y})}{\operatorname{d} y}$

Question: I was reading this:
Characteristic function of Normal random variable squared
And there is a step, where I do not manage to understand the equality marked as "$\stackrel{???}{=}$"
We have
$$ F_X\left(\sqrt{y}\right)= \int_{-\infty}^\sqrt{y} \frac{1}{\sqrt{2\pi}} \operatorname{e}^{-\frac{t^2}{2}} \operatorname{d}t$$
and the pdf is
$$f_Y(y) = \frac{\operatorname{d}F_Y(y}{\operatorname{d} y}=2\frac{\operatorname{d}F_X(\sqrt{y})}{\operatorname{d} y} \stackrel{???}{=} 2 \frac{1}{\sqrt{2}\sqrt{\pi}} e^{-\frac{y}{2}} \left( \frac{1}{2} y^{-\frac{1}{2}} \right) = \frac{1}{2^{\frac{1}{2}} \Gamma(\frac{1}{2})}y^{\frac{1}{2}-1}e^{-\frac{y}{2}}  \Bbb{I}_{[0,+\infty)}(y)
$$
Thank you for your insight!
Comment: $$\frac{d}{dy}F_X(\sqrt y)=f_X(\sqrt y)\frac{d}{dy}(\sqrt y)$$ , where $f_X$ is the pdf of $X$.
Comment: I would suggest that you take a look at the Leibniz integral rule https://en.m.wikipedia.org/wiki/Leibniz_integral_rule and the fundamental theorem of calculus https://en.m.wikipedia.org/wiki/Fundamental_theorem_of_calculus
Answer: First, let's differentiate $F_X$ with respect to its argument $\sqrt{y}$, giving $\frac{1}{\sqrt{2\pi}}\exp -\frac{(\sqrt{y})^2}{2}=\frac{1}{\sqrt{2\pi}}\exp -\frac{y}{2}$. Now we use the chain rule to get the derivative with respect to $y$ instead, by multiplying by $\frac{d}{dy}\sqrt{y}=\frac{1}{2}y^{-1/2}$.


Simple algorithm for online outlier detection of a generic time series

Question: I am working with a large amount of time series. These time series are basically network measurements coming every 10 minutes, and some of them are periodic (i.e. the bandwidth), while some other aren't (i.e. the amount of routing traffic).
I would like a simple algorithm for doing an online "outlier detection". Basically, I want to keep in memory (or on disk) the whole historical data for each time series, and I want to detect any outlier in a live scenario (each time a new sample is captured). What is the best way to achieve these results?
I'm currently using a moving average in order to remove some noise, but then what next? Simple things like standard deviation, mad, ... against the whole data set doesn't work well (I can't assume the time series are stationary), and I would like something more "accurate", ideally a black box like:
double outlier_detection(double* vector, double value);
where vector is the array of double containing the historical data, and the return value is the anomaly score for the new sample "value" .
Comment: I think we should encourage posters to post links as part of the question if they have posted the same question at another SE site.
Comment: Just for clarity, here's the original question on SO: http://stackoverflow.com/questions/3390458/simple-algorithm-for-online-outlier-detection-of-a-generic-time-series
Comment: I also suggest you check out the other Related links on the right hand side of the page. This is a popular question and it has come up in a variety of questions previously. If they aren't satisfactory it is best to update your question about the specifics of your situation.
Comment: Good catch, @Andy!  Let's merge this question with the other one.
Comment: 2cts input. Working on the same kind of models I came to the conclusion that internet traffic "can just happen", changes in volume are common and can happen without requiring action. Derived questions like is the percentage of locked accounts increasing, does the geographical origin of the traffic change can be more interesting and or stable.
Answer: Here is a simple R function that will find time series outliers (and optionally show them in a plot). It will handle seasonal and non-seasonal time series. The basic idea is to find robust estimates of the trend and seasonal components and subtract them. Then find outliers in the residuals. The test for residual outliers is the same as for the standard boxplot -- points greater than 1.5IQR above or below the upper and lower quartiles are assumed outliers. The number of IQRs above/below these thresholds is returned as an outlier "score". So the score can be any positive number, and will be zero for non-outliers.
I realise you are not implementing this in R, but I often find an R function a good place to start. Then the task is to translate this into whatever language is required.
tsoutliers <- function(x,plot=FALSE)
{
    x <- as.ts(x)
    if(frequency(x)>1)
        resid <- stl(x,s.window="periodic",robust=TRUE)$time.series[,3]
    else
    {
        tt <- 1:length(x)
        resid <- residuals(loess(x ~ tt))
    }
    resid.q <- quantile(resid,prob=c(0.25,0.75))
    iqr <- diff(resid.q)
    limits <- resid.q + 1.5*iqr*c(-1,1)
    score <- abs(pmin((resid-limits[1])/iqr,0) + pmax((resid - limits[2])/iqr,0))
    if(plot)
    {
        plot(x)
        x2 <- ts(rep(NA,length(x)))
        x2[score>0] <- x[score>0]
        tsp(x2) <- tsp(x)
        points(x2,pch=19,col="red")
        return(invisible(score))
    }
    else
        return(score)
}

Comment: @Marcin, I recommend taking a stab at it yourself.  Maybe paste your solution at gist.github.com and post a SO question when you're done, to have others check your work?
Comment: Really thank you for your help, I really appreciate. I'm quite busy at work now, but I'm going to test an approach like yours as soon as possible, and I will come back with my final considerations about this issue. One only thought: in your function, from what I see, I have to manually specify the frequency of the time series (when constructing it), and the seasonality component is considered only when the frequency is greater than 1. Is there a robust way to deal with this automatically?
Comment: Yes, I have assumed the frequency is known and specified. There are methods to estimate the frequency automatically, but that would complicate the function considerably. If you need to estimate the frequency, try asking a separate question about it -- and I'll probably provide an answer! But it needs more space than I have available in a comment.
Comment: +1 from me, excellent. So > 1.5 X inter-quartile range is the consensus definition of an outlier for time-dependent series? That would be nice to have a scale-independent reference.
Comment: The outlier test is on the residuals, so hopefully the time-dependence is small. I don't know about a consensus, but boxplots are often used for outlier detection and seem to work reasonably well. There are better methods if someone wanted to make the function a little fancier.
Comment: I finally had the time to play a bit with this method, and this seems  a good compromise (even if I still have to evaluate how complex is the stl() routine from a memory/cpu point of view), which works pretty well if we known the frequency of the time series. BTW, I noticed that the results of the detection heavily depend on the kind of signal I'm analyzing, and in order to obtain the optimum results I have to manually tune the probabilities for the quantiles or the multiplier for the IQR.
Since you're an expert of this topic, is there a "smart" and "de facto" solution to deal with this issue?
Comment: Is there a chance someone could rewrite this in Matlab?  I don't 'speak' R, but this seems very useful.
Comment: I just posted a follow-up question to @RobHyndman's answer here: [Simple algorithm for online outlier detection of a generic time series II: Daily cycle within annual ](http://stats.stackexchange.com/questions/151840/simple-algorithm-for-online-outlier-detection-of-a-generic-time-series-ii-daily)
Comment: @RobHyndman thanks for a great solution. I'm trying to step through the code and I'm curious about what the tt variable stands for. I understand that it's a vector of 1 to n and it's used as a predictor in the loess model. My brain is wired a bit odd and I have an need to understand each element of a puzzel : ) Thanks again
Comment: tt = time trend
Comment: Any good practice on what value should I replace the outlier? Value of last season or last value (or maybe a combination of both)?
Answer: A good solution will have several ingredients, including:

Use a resistant, moving window smooth to remove nonstationarity.
Re-express the original data so that the residuals with respect to the smooth are approximately symmetrically distributed.  Given the nature of your data, it's likely that their square roots or logarithms would give symmetric residuals.
Apply control chart methods, or at least control chart thinking, to the residuals.

As far as that last one goes, control chart thinking shows that "conventional" thresholds like 2 SD or 1.5 times the IQR beyond the quartiles work poorly because they trigger too many false out-of-control signals.  People usually use 3 SD in control chart work, whence 2.5 (or even 3) times the IQR beyond the quartiles would be a good starting point.
I have more or less outlined the nature of Rob Hyndman's solution while adding to it two major points: the potential need to re-express the data and the wisdom of being more conservative in signaling an outlier.  I'm not sure that Loess is good for an online detector, though, because it doesn't work well at the endpoints.  You might instead use something as simple as a moving median filter (as in Tukey's resistant smoothing).  If outliers don't come in bursts, you can use a narrow window (5 data points, perhaps, which will break down only with a burst of 3 or more outliers within a group of 5).
Once you have performed the analysis to determine a good re-expression of the data, it's unlikely you'll need to change the re-expression.  Therefore, your online detector really only needs to reference the most recent values (the latest window) because it won't use the earlier data at all.  If you have really long time series you could go further to analyze autocorrelation and seasonality (such as recurring daily or weekly fluctuations) to improve the procedure.
Comment: @John, 1.5 IQR is Tukey's original recommendation for the longest whiskers on a boxplot and 3 IQR is his recommendation for marking points as "far outliers" (a riff on a popular 60's phrase). This is built into many boxplot algorithms. The recommendation is analyzed theoretically in Hoaglin, Mosteller, & Tukey, *Understanding Robust and Exploratory Data Analysis.*
Comment: This is an extraordinary answer for practical analysis. Never would have thought needed to try 3 IQR beyond the quartiles.
Comment: @Neo Your best bet may be to experiment with a subset of your data and confirm your conclusions with tests on the remainder.  You could conduct a more formal cross-validation, too (but special care is needed with time series data due to the interdependence of all the values).
Comment: This confirms time series data I have been trying to analyse. Window average and also a window standard deviations. ((x - avg) / sd) > 3 seem to be the points I want to flag as outliers. Well at least warn as outliers, I flag anything higher than 10 sd as extreme error outliers. The problem I run into is what is an ideal window length? I'm playing with anything between 4-8 data points.
Answer: (This answer responded to a duplicate (now closed) question at Detecting outstanding events, which presented some data in graphical form.)

Outlier detection depends on the nature of the data and on what you are willing to assume about them.  General-purpose methods rely on robust statistics.  The spirit of this approach is to characterize the bulk of the data in a way that is not influenced by any outliers and then point to any individual values that do not fit within that characterization.
Because this is a time series, it adds the complication of needing to (re)detect outliers on an ongoing basis.  If this is to be done as the series unfolds, then we are allowed only to use older data for the detection, not future data!  Moreover, as protection against the many repeated tests, we would want to use a method that has a very low false positive rate.
These considerations suggest running a simple, robust moving window outlier test over the data.  There are many possibilities, but one simple, easily understood and easily implemented one is based on a running MAD: median absolute deviation from the median. This is a strongly robust measure of variation within the data, akin to a standard deviation. An outlying peak would be several MADs or more greater than the median.
There is still some tuning to be done: how much of a deviation from the bulk of the data should be considered outlying and how far back in time should one look? Let's leave these as parameters for experimentation.  Here's an R implementation applied to data $x = (1,2,\ldots,n)$ (with $n=1150$ to emulate the data) with corresponding values $y$:
# Parameters to tune to the circumstances:
window <- 30
threshold <- 5

# An upper threshold ("ut") calculation based on the MAD:
library(zoo) # rollapply()
ut <- function(x) {m = median(x); median(x) + threshold * median(abs(x - m))}
z <- rollapply(zoo(y), window, ut, align="right")
z <- c(rep(z[1], window-1), z) # Use z[1] throughout the initial period
outliers <- y > z

# Graph the data, show the ut() cutoffs, and mark the outliers:
plot(x, y, type="l", lwd=2, col="#E00000", ylim=c(0, 20000))
lines(x, z, col="Gray")
points(x[outliers], y[outliers], pch=19)

Applied to a dataset like the red curve illustrated in the question, it produces this result:

The data are shown in red, the 30-day window of median+5*MAD thresholds in gray, and the outliers--which are simply those data values above the gray curve--in black.
(The threshold can only be computed beginning at the end of the initial window. For all data within this initial window, the first threshold is used: that's why the gray curve is flat between x=0 and x=30.)
The effects of changing the parameters are (a) increasing the value of window will tend to smooth out the gray curve and (b) increasing threshold will raise the gray curve.  Knowing this, one can take an initial segment of the data and quickly identify values of the parameters that best segregate the outlying peaks from the rest of the data.  Apply these parameter values to checking the rest of the data.  If a plot shows the method is worsening over time, that means the nature of the data are changing and the parameters might need re-tuning.
Notice how little this method assumes about the data: they do not have to be normally distributed; they do not need to exhibit any periodicity; they don't even have to be non-negative. All it assumes is that the data behave in reasonably similar ways over time and that the outlying peaks are visibly higher than the rest of the data.

If anyone would like to experiment (or compare some other solution to the one offered here), here is the code I used to produce data like those shown in the question.
n.length <- 1150
cycle.a <- 11
cycle.b <- 365/12
amp.a <- 800
amp.b <- 8000

set.seed(17)
x <- 1:n.length
baseline <- (1/2) * amp.a * (1 + sin(x * 2*pi / cycle.a)) * rgamma(n.length, 40, scale=1/40)
peaks <- rbinom(n.length, 1,  exp(2*(-1 + sin(((1 + x/2)^(1/5) / (1 + n.length/2)^(1/5))*x * 2*pi / cycle.b))*cycle.b))
y <- peaks * rgamma(n.length, 20, scale=amp.b/20) + baseline

Comment: @TheGoat Yes: it's an *ad hoc* way of getting the analysis started at the beginning of the time series.
Comment: This is a really interesting solution and I appreciate that I can implement it without using R (just using plain JavaScript in a web application). Thanks!
Comment: I think a (slightly modified) approach using the MAD is also known under the term "Hampel identifier".
Comment: @whuber thanks for the great post. Is the purpose of "z <- c(rep(z[1], window-1), z) # Use z[1] throughout the initial period" to impute for values 1:29 due to the rolling window being right aligned and producing a result at position 30? Have I understood that correctly? Thanks again
Answer: If you're worried about assumptions with any particular approach, one approach is to train a number of learners on different signals, then use ensemble methods and aggregate over the "votes" from your learners to make the outlier classification.
BTW, this might be worth reading or skimming since it references a few approaches to the problem.

Online outlier detection over data streams
Answer: I am guessing sophisticated time series model will not work for you because of the time it takes to detect outliers using this methodology. Therefore, here is a workaround:

First establish a baseline 'normal' traffic patterns for a year based on manual analysis of historical data which accounts for time of the day, weekday vs weekend, month of the year etc.
Use this baseline along with some simple mechanism (e.g., moving average suggested by Carlos) to detect outliers.

You may also want to review the statistical process control literature for some ideas.
Comment: Yeah, this is exactly what I am doing: until now I manually split the signal into periods, so that for each of them I can define a confidence interval within which the signal is supposed to be stationary, and therefore I can use standard methods such as standard deviation, ...
The real problem is that I can not decide the expected pattern for all the signals I have to analyze, and that's why I'm looking for something more intelligent.
Comment: Here is a one idea: Step 1: Implement and estimate a generic time series model on a one time basis based on historical data. This can be done offline. Step 2: Use the resulting model to detect outliers. Step 3: At some frequency (perhaps every month?), re-calibrate the time series model (this can be done offline) so that your step 2 detection of outliers does not go too much out of step with current traffic patterns. Would that work for your context?
Comment: Please keep in mind I'm not too lazy for investigating these parameters, the point is that these values need to be set according to the expected pattern of the signal, and in my scenario I can't make any assumption.
Comment: ARIMA models are classic time series models that can be used to fit time series data. I would encourage you to explore the application of ARIMA models. You could wait for Rob to be online and perhaps he will chime in with some ideas.
Comment: Yes, this might work. I was thinking about a similar approach (recomputing the baseline every week, which can be CPU intensive if you have hundreds of univariate time series to analyze).
BTW the real difficult question is "what is the best blackbox-style algorithm for modeling a completely generic signal, considering noise, trend estimation and seasonality?".
AFAIK, every approach in literature requires a really hard "parameter tuning" phase, and the only one automatic method I found is an ARIMA model by Hyndman (http://robjhyndman.com/software/forecast/). Am I missing something?
Answer: Seasonally adjust the data such that a normal day looks closer to flat. You could take today's 5:00pm sample and subtract or divide out the average of the previous 30 days at 5:00pm. Then look past N standard deviations (measured using pre-adjusted data) for outliers.
This could be done separately for weekly and daily "seasons."
Comment: Unless you are willing to implement a general time series model (which brings in its cons in terms of latency etc) I am pessimistic that you will find a general implementation which at the same time is simple enough to work for all sorts of time series.
Comment: Again, this works pretty well if the signal is supposed to have a seasonality like that, but if I use a completely different time series (i.e. the average TCP round trip time over time), this method will not work (since it would be better to handle that one with a simple global mean and standard deviation using a sliding window containing historical data).
Comment: Another comment: I know a good answer might be "so you might estimate the periodicity of the signal, and decide the algorithm to use according to it", but I didn't find a real good solution to this other problem (I played a bit with spectral analysis using DFT and time analysis using the autocorrelation function, but my time series contain a lot of noise and such methods give some crazy results mosts of the time)
Comment: A comment to your last comment: that's why I'm looking for a more generic approach, but I need a kind of "black box" because I can't make any assumption about the analyzed signal, and therefore I can't create the "best parameter set for the learning algorithm".
Comment: @gianluca As you have intimated the underlying ARIMA structure can mask the anomaly. Incorrect formulation pf possible cause variables such as hour of the day, day-of-the-week, holiday effects etc can also mask the anomaly. The answer is fairly clear you need to have a good eqaution to effectively detect anomalies. To quote Bacon , "For whoever knows the ways of Nature will more easily notice her deviations and, on the other hand, whoever knows her deviations will more accurately describe her ways."
Answer: An alternative to the approach outlined by Rob Hyndman would be to use Holt-Winters Forecasting . The confidence bands derived from Holt-Winters can be used to detect outliers. Here is a paper that describes how to use Holt-Winters for "Aberrant Behavior Detection in Time Series for Network Monitoring". An implementation for RRDTool can be found here.
Answer: Spectral analysis detects periodicity in stationary time series.  The frequency domain approach based on spectral density estimation is an approach I would recommend as your first step.
If for certain periods irregularity means a much higher peak than is typical for that period then the series with such irregularities would not be stationary and spectral anlsysis would not be appropriate. But assuming you have identified the period that has the irregularities you should be able to determine approximately what the normal peak height would be and then can set a threshold at some level above that average to designate the irregular cases.
Comment: Could you explain how this solution would detect "local irregularities"? Presenting a worked example would be extremely helpful. (To be honest, I'm suggesting you do this because in carrying out such an exercise I believe you will discover that your suggestion is not effective for outlier detection. But I could be wrong...)
Comment: @whuber The spectral analysis will only identify where all the peaks are. The next step would be to fit a yime series model using sine and cosine terms with the frequencies determined from the spectral analysis and the amplitudes estimated from the data. If irregularities mean peaks with very high amplitudes then I think a threshold on the amplitude would be appropriate. If local irregularities means that for a period the amplitude sometimes is significantly larger than other then the series is not stationary and spectral analysis would not be approriate.
Comment: I don't follow the conclusion about lack of stationarity. For instance, the sum of a regular sinusoidal waveform and a marked Poisson point process would be stationary, but it would not exhibit any of the periodicity you seek. You would nevertheless find some strong peaks in the periodogram, but they would tell you nothing relevant to the irregular data peaks introduced by the Poisson process component.
Comment: A stationary time series has a constant mean.  If the peak for a periodic component can change over time it can cuase the mean to change over time and hence the seires would be nonstationary.
Answer: Since  it is a time series data, a simple exponential filter http://en.wikipedia.org/wiki/Exponential_smoothing will smoothen the data. It is a very good filter since you don't need to accumulate old data points. Compare every newly smoothed data value with its unsmoothed value. Once the deviation exceeds a certain predefined threshold (depending on what you believe an outlier in your data is), then your outlier can be easily detected.
In C I will do the following for a real-time 16 bit sample (I believe this is found somewhere here < Explanation - https://dsp.stackexchange.com/questions/378/what-is-the-best-first-order-iir-approximation-to-a-moving-average-filter>)
#define BITS2 2     //< This is roughly = log2( 1 / alpha ), depending on how smooth you want your data to be

short Simple_Exp_Filter(int new_sample)
{static int filtered_sample = 0;
long local_sample = sample << 16; /*We assume it is a 16 bit sample */
filtered_sample += (local_sample - filtered_sample) >> BITS2;
return (short) ((filtered_sample+0x8000) >> 16); //< Round by adding .5 and truncating.
}

int main()
{
newly_arrived = function_receive_new_sample();
filtered_sample = Simple_Exp_Filter(newly_arrived);
if (abs(newly_arrived - filtered_sample)/newly_arrived > THRESHOLD)
    {
    //AN OUTLIER HAS BEEN FOUND
    }
 return 0;
}

Answer: You could use the standard deviation of the last N measurements (you have to pick a suitable N). A good anomaly score would be how many standard deviations a measurement is from the moving average.
Comment: Thank you for your response, but what if the signal exhibits a high seasonality (i.e. a lot of network measurements are characterized by a daily and weekly pattern at the same time, for example night vs day or weekend vs working days)? An approach based on standard deviation will not work in that case.
Comment: For example, if I get a new sample every 10 minutes, and I'm doing an outlier detection of the network bandwidth usage of a company, basically at 6pm this measure will fall down (this is an expected an totaly normal pattern), and a standard deviation computed over a sliding window will fail (because it will trigger an alert for sure). At the same time, if the measure falls down at 4pm (deviating from the usual baseline), this is a real outlier.
Answer: what I do is group the measurements by hour and day of week and compare standard deviations of that. Still doesn't correct for things like holidays and summer/winter seasonality but its correct most of the time.
The downside is that you really need to collect a year or so of data to have enough so that stddev starts making sense.
Comment: Thank you, that's exactly what I was trying to avoid (having a lot of samples as baseline), because I would like a really reactive approach (e.g. online detection, maybe "dirty", after 1-2 weeks of baseline)
Answer: anomaly detection requires the construction of an equation which describes the expectation. Intervention Detection is available in both a non-causal and causal setting . If one has a predictor series like price then things can get a little complicated. Other responses here don't seem to take into account assignable cause attributable to user specified predictor series like price and thus might be flawed. Quantity sold may well depend on price , perhaps previous prices and perhaps quantity sold in the past. The basis for the anomaly detection ( pulses,seasonal pulses, level shifts and local time trends ) is found in  https://pdfs.semanticscholar.org/09c4/ba8dd3cc88289caf18d71e8985bdd11ad21c.pdf
Comment: The link is not working, could you please fix it. Thanks
Comment: done ..................
Answer: For the case where one has to compute the outliers quickly, one could use the idea of Rob Hyndman and Mahito Sugiyama ( https://github.com/BorgwardtLab/sampling-outlier-detection , library(spoutlier), function qsp ) to compute the outliers as follows:
library(spoutlier)
rapidtsoutliers <- function(x,plot=FALSE,seed=123)
{
    set.seed(seed)
    x <- as.numeric(x)
    tt <- 1:length(x)
    qspscore <- qsp(x)
    limit <- quantile(qspscore,prob=c(0.95))
    score <- pmax((qspscore - limit),0)
    if(plot)
    {
        plot(x,type="l")
        x2 <- ts(rep(NA,length(x)))
        x2[score>0] <- x[score>0]
        tsp(x2) <- tsp(x)
        points(x2,pch=19,col="red")
        return(invisible(score))
    }
    else
        return(score)
}


How to access Mysql installed on my google cloud instance via Mysql workbench

Question: I have Mysql installed on google cloud instance and its running fine.
Earlier i had a separate google cloud sql instance ,but due to performance issues i installed mysql on my google cloud instance.Iam currently running the database from my google cloud instance.
The issues is that when it was a seperate sql instance i could access the database from Mysql Workbench.
But now that i have it installed on my google cloud instance,i can not access it from workbench.
Is there a way i can access it from my workbench.
Please advise and help
Comment: What issues are you encountering exactly? What have you tried? Did you assign an IP to your cloud sql instance and tried to connect?
Comment: earlier we had a separate sql instance which had a seperate ip.
But now we have installed sql onto the google cloud instance ,so my vm and sql are both running on the same machine instead of sql instance running seperately and vm running seperately.But since i have installed sql on the vm now i can not access it from mysql workbench software.Earlier when sql instance was separate i could access it via mysql workbench.Is there something i need to do now to access sql using work bench?why am i not able to access it now using workbench
Comment: So you are running MySQL on a Compute Engine VM, right? you should make sure that the MySQL port (normally `3306`) is accesible from outside.
Answer: I assume that you have created user in the cloud MySQL instance by giving current public IP. Once you done with it go to the MySQL workbench and click on little plus icon. Then you get a window like below. You can give any name to the database. For the host name you must provide host address relevant to your MySQL instance. Once you done with give a username. To enter the password you must click on the Store in Vault enter it. Once you complete click on TestConnection. If it gives successful message then your connection is done. If not you must recheck inputs most input your public IP, because sometimes this change even after one or two hours. No need of filling Default Schema field. This might be helpful for your work.
Comment: There is one caveat to that, a user created in the instance does not have GRANTS to create things like stored procedures.  I have been pulling my hair out on trying to get any user (even tried AIM) with that role so I can load sProcs.


Por que a Condição ELSE valida mesmo a IF sendo True?

Question:  let valor=prompt("Digite um Numero:")

if(valor<20){
  alert("Valor menor que 20")}
if(valor==20){
  alert("Valor Igual a 20") }
    else {
  alert("Valor maior que 20")}

Answer: O uso do seu condicional está errado.
Pelo que entendi, uma possível solução para o seu problema seria:
let valor=prompt("Digite um número:")

if(valor<20){
    alert("Valor menor que 20")
} else if(valor==20){
    alert("Valor Igual a 20")
} else {
    alert("Valor maior que 20")
}

Repare que temos um encadeamento do comando condicional if
Comment: Era isso mesmo , encadeamento ! valeu !


x raised by the power of y equals infinity.

Question: How many zeros are there in this equation?
<code>10000^999
</code>
In my calculator it says infinity but that doesn't seem right.
Comment: We know $(a^m)^n=a^{mn}$  Here $10000=10^4$
Comment: what have u tried?
Comment: Further, calculators aren't perfect.
Comment: should it be 1*10^4^999 = 10^3996 ?
Comment: Calculators make a lot of approximations, so that they can do arithmetic with finite decimal numbers.

You're probably familiar with approximations like writing $1/3 \sim 0.33333333$. If your calculator only uses 8 decimal places, this error is too small for the calculator to represent.

Approximating sufficiently large numbers with $+\infty$ is the same idea.
Answer: The answer is: $3996$.
$10000^1 - 4$ zeroes $(4 \cdot 1)$
$10000^2 - 8$ zeroes $(4 \cdot 2)$
...
$10000^{999} - 3996$ zeroes $(4 \cdot 999)$
Answer: Everytime we mulltiply a number with $10$ we get:
$$n \cdot 10 = \overline{n0}$$
This would hold for any power of $10$. So for $1000=10^4$, this would be:
$$n \cdot 10000 = \overline{n0000}$$
If we multiply again we have:
$$\overline{n0000} \cdot 10000 = \overline{n00000000}$$
To sum up everytime we multiply we add $k$ zeroes at the end of the number, where $k$ is the exponent in the prime power of $10$.
So in our specific case, take $n=1$ and multiply by $10^4$ that would add $4$ zeroes at the end of the number, repeat that $999$ times and you'll get that $10000^{999}$ will have $4\cdot 999 = 3996$ zeroes.
Note that this property is a consequence of our decimal numbercal system. If we were working in a numberical system with base $k$ then the same property would hold for every power of $k$.


How to know whether my app is installed on a mobile?

Question: I am creating an app.In this app when user signs up all of that user's contacts will be invited.My problem is x has two numbers of y say y1 and y2.When x sent invitation to y and y installed my app on his/her mobile with the number of y1.After that y shouldn't get any invitation not only from x but also from any of his contacts.How to achieve this?
Answer: Try thid code to know your app is installed or not
<code>     boolean isAppInstalled = appInstalledOrNot("com.check.application");

    if(isAppInstalled) {
        //This intent will help you to launch if the package is already installed
        Intent LaunchIntent = getPackageManager()
            .getLaunchIntentForPackage("com.check.application");
        startActivity(LaunchIntent);

        Log.i("Application is already installed.");
    } else {
        // Do whatever we want to do if application not installed
        // For example, Redirect to play store

        Log.i("Application is not currently installed.");
    }
}

private boolean appInstalledOrNot(String uri) {
    PackageManager pm = getPackageManager();
    try {
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
    }

    return false;
}
</code>
Comment: Nice answer, please add some description about the answer , so that other also know what actually it is and how it works. Thanks
Comment: where to use this code . . . i mean in which class ??
Comment: Use can use this code in any class ..may be click on any button or anywhere
Comment: ok . . but the class would run this code only after the installation. . . then how this would check whether the app is installed or not. . . bcz it is already installed on the device.


Finding an isomorphism between the additive group $\mathbb{Z}/4$ and the multiplicative group $\mathbb{Z}/5 - \{0\}$

Question: I am trying to show that $\mathbb{Z}/4$ with addition is isomorphic to the non-zero elements of $\mathbb{Z}/5$ with multiplication.
After writing out the multiplication tables, I've found that the elements of $\mathbb{Z}/4\mathbb{Z}$ have the following orders. (I'm not sure if it's standard to use vertical bars or brackets, but I'm going to use brackets and subscripts to distinguish the two groups.)
\begin{align*}
|[0]_4| &= 1 \\
|[1]_4| &= 4 \\
|[2]_4| &= 2 \\
|[3]_4| &= 4.
\end{align*}
As for $\mathbb{Z}/5 - \{0\}$:
\begin{align*}
|[1]_5| &= 1 \\
|[2]_5| &= 4 \\
|[3]_5| &= 4 \\
|[4]_5| &= 2.
\end{align*}
An isomorphism has to preserve orders, so I need to map:
\begin{align*}
[0]_4 &\mapsto [1]_5 \\
[2]_4 &\mapsto [4]_5.
\end{align*}
At this point, I believe I have a choice for the elements of order $4$. I know I need inverses to preserve inverses and to preserve the multiplication table, so it's possible that only one works. (Or is it sufficient to preserve orders? I'd appreciate some help with the intuition.) From the multiple tables, it was clearer to me to send:
\begin{align*}
[1]_4 &\mapsto [3]_5 \\
[3]_4 &\mapsto [3]_5.
\end{align*}
The map that accomplishes this is $f: \mathbb{Z}/4\mathbb{Z} \to (\mathbb{Z}/5\mathbb{Z})^{\times}$ defined by
$$
f([x]_4) = [2]_5^{[x]_5}.
$$
I need to show four things:

This map is well-defined: if $[x]_4 = [y]_4$, then $[2]_5^{[x]_5} = [2]_5^{[x]_5}$.

This map is a homomorphism: for every $[x]_4, [y]_4 \in \mathbb{Z}/4$,
$$
f([x]_4 [y]_4) = f([x]_4)f([y]_4).
$$

This map is injective: either (directly) $f([x]_4) = f([y]_4)$ implies $[x]_4 = [y]_4$ or $\mathrm{ker}(f)$ is trivial.

This map is surjective (though this is implied from it being an injective homomorphism between groups of equal order): for every $[y]_5 \in (\mathbb{Z}/5)^{\times})$, there exists $[x]_4 \in \mathbb{Z}/4$ such that $f([x]_4) = [2]_5^{[x]_5} = [y]_5$.

I'm ok with proving the homomorphism property. For $[x]_4, [y]_4 \in \mathbb{Z}/4$, the exponent properties and commutativity of $(\mathbb{Z}/5\mathbb{Z})^{\times}$ imply
\begin{align*}
f([x]_4 [y]_4) &= f([xy]_4) \\
&= [2]_5^{[xy]_5} \\
&= [2]_5^{[x]_5 [y]_5} \\
&= [2]_5^{[x]_5} [2]_5^{[y]_5} \\
&= f([x]_4) f([y]_4).
\end{align*}
I'm struggling to prove well-definedness. For injectivity, I'd like to invoke injectivity of the exponential function $2^x$, which requires that I invoke logarithms mod $2$, but I'm not sure if I'm allowed to take a logarithm in mod $5$. I'm also not sure how to prove surjectivity, but injectivity, I believe, suffices because the groups have the same order.
I'd appreciate some direction or hints on how to prove well-definedness and bijectivity.
Comment: Remark.  See "primitive root".  Since $2$ is one of the primitive roots mod $5$, such a map can be written $(n \mod 4)\mapsto (2^n \mod 5)$.  Here it is:

$$0 \mapsto 2^0 = 1\\
1 \mapsto 2^1 = 2\\
2 \mapsto 2^2 = 4\\
3 \mapsto 2^3 = 8 \equiv 3\mod 5\\
4 \mapsto 2^4 = 16 \equiv 1\mod 5$$
and it repeats.
Answer: You're overthinking.
Let $G=\Bbb Z_4, H=\Bbb Z_5^*$.
There are four elements in $H$ by inspection and
$$\begin{align}
[2]_5^0&=[1]_5,\\
[2]_5^1&=[2]_5,\\
[2]_5^2&=[4]_5,\\
[2]_5^3&=[2\times 4]_5=[3]_5,\\
[2]_5^4&=[2\times 3]_5=[1]_5.
\end{align}$$
Thus $H=\langle [2]_5\rangle$ is cyclic.
There are only two groups of order four up to isomorphism: $\Bbb Z_2^2$ and $G$.
Hence $G\cong H$.

Isomorphisms of cyclic groups are determined by where the generator is sent; simply:
$$\begin{align}
\varphi: H&\to G,\\
[2]_5&\mapsto[1]_4
\end{align}$$
would suffice.


Universal Application for IPhone/Ipod Touch 4th gen only + IPad

Question: I'm developing a Universal Application that's only meant to be used on Retina display IPhone/IPod touch devices + IPad. How can I specify this on the plist or anywhere else in my app/binary?
UIRequiredDeviceCapabilities doesn't do the trick for both devices, since I could specify that I require front-facing-camera for the IPhone/IPod but that would exclude the IPad 1.
Comment: Why do you absolutely need the device to be retina display equipped?
Comment: It just doesn't look good enough on non retina displays. It's a game and we just don't want to deliver a sub optimal experience even though we will lose some buyers because of that.
Comment: I'm not sure this is possible; as you are aware 4.0.x + is available for the 3GS/4/Pad etc, and whilst there is code that can detect whether a retina display is available or not, the option does not exist when uploading to iTunes. It's a no for now, I reckon.
Comment: yep, unfortunately it looks like it can't be done
Answer: This can't be done. The UIRequiredDeviceCapabilities should be used to restrict capabilities that your app/game really needs or excludes, it's not meant to be used to restrict a certain set of devices just because the developer doesn't want to support them. So basically you should always design keeping in mind all devices that are currently being updated to the last OS.
Answer: I don't recommend doing this really, but you could just find out what device it is and then base loading your app on if the method isCorrectDevice returns true:
- (BOOL) isCorrectDevice
{
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = malloc(size);
    sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platform = [NSString stringWithCString:machine];
    free(machine);

    if ([platform isEqualToString:@"iPhone3,1"] ||
        [platform isEqualToString:@"iPhone3,2"] ||
        [platform isEqualToString:@"iPod4,1"] ||
        [platform isEqualToString:@"iPad1,1"] ||
        [platform isEqualToString:@"iPad2,1"] ||
        [platform isEqualToString:@"iPad2,2"] ||
        [platform isEqualToString:@"iPad2,3"])
        return true;
    else
        return false;
}

There are a few problems doing this. One is, as soon as Apple release a new device this will be out of date and will not include the newer devices; but also, this won't stop users of other devices from downloading it. I guess you could just load a screen that says "Only available on..."? Just an idea.
Comment: Thanks for this, but what we really wanted to avoid was users from downloading the app.
Comment: Yes I figured. Annoyingly this might be your only option. You could always produce a splash screen at the start saying "Optimized for the retina display" ;)
Comment: :) we are fixing it for 3GS, it was not as hard as expected, but it certainly doesn't look the way we want to.
Comment: I've found that devices the pre-date the 3GS don't run 4.0 very well and most users with that will be on 3.x! 3GS is definitely one to test with though!
Comment: we don't even have one! We borrowed one today and it's looking good so far, we are only supporting iOS 4.1, so older devices won't be a problem


Is my time complexity correct for my bubble sort code?

Question: new to computer science here, so i've wrote this bubble sort code and trying to calculate it's time complexity and performance here, the code for the sort is here:
for (int i = 0; i < size; i++)
{
  Node current = head.next;
  while (current.next != tail)
  {
    if (current.element.depth < current.next.element.depth)
    {
      swap(current, current.next);
    }
    else
    {
      current = current.next;
    }
  }
}

The swap method code is here:
void swap(Node nodeA, Node nodeB)
{
    Node before = nodeA.prev;
    Node after = nodeB.next;
    before.next = nodeB;
    after.prev = nodeA;
    nodeB.next = nodeA;
    nodeB.prev = before;
    nodeA.prev = nodeB;
    nodeA.next = after;
}

Now I know the bubble sort's time complexity is O(n^2) in worst-case performance, but I'm trying to calculate the function of each executions in my for loop here. I have a basic understanding of time complexity, i know a standard for loop is f(n) = 2n + 2 and we consider the worse case scenario for time complexity. So far, this is my thought progress to find the f(n) for my code:
int i = 0;            This will be executed only once.
i < size;             This will be executed N+1 times.
i ++;                 This will be executed N times.
current = head.next;  This will be executed N times.
current.next != tail; This will be executed N times.

And since a while loop is within the for loop,
    it's n*n within the while loop, there
    are 4 operations, so it's 4n^2.

In the worst scenario, I'll have to use swap method every time, and since my time complexity for swap method is simply 8 (i think, it's only 8 executions right?) So the worst scenario for swap(current,current.next) is 8n?
If we add them up:
f(n) = 1 + n + 1 + n + n + n+  4n^2 + 8n

f(n) = 4n^2 + 12n + 2

f(n) ~ O(n^2)

Is my time complexity f(n) correct?
If not, can you point me to the right answer please, also do you have some suggestions to improve on my code performance?
Answer: As you have a while loop inside a for loop - yes, you have a O(n^2) complexity, which is considered bad.
The rule of thumb here is as follows (for N elements input, from better to worse):

No loops, just some executions regardless of the input size = O(1)
Loop(N/M) (you divide the input on each iteration) = O(log N)
Just one loop over N elements = O(N)
Loop2(N) inside Loop1(N) = O(N^2)

See this answer for better explanation: What does O(log n) mean exactly?


Hook for URL admin/people/create

Question: What is the hook to alter the form that appears for the URL admin/people/create?
I tried hook_form_alter(), but that's not the right one.
Comment: I think you made a mistake somewhere, I can't think of a reason why hook_form_alter wouldn't work for that particular form.
Answer:
/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  switch($form_id) {
    case "user_register_form" :
      //Your Logic
    break;
  }
}

But for performance issue use hook_form_FORM_ID_alter()
Comment: For future readers: The implementation of `hook_form_FORM_ID_alter()` for user_register_form would be `hook_form_user_register_form_alter()`.
Answer: function modulename_form_user_register_form_alter(&$form) {
  // modify $form var
}

hook_form_FORM_ID_alter() API Docs
Comment: The complete list of parameters would be `&$form, &$form_state, $form_id`; if the function is just using the first parameter (as it normally happen), it can short the parameter list to only that parameter.

Hook for URL admin/people/create


My level of significance is 1 for all my variables

Question: Basically, I'm trying to run a logistic regression, to predict, if the state and year is given will the birth level in the particular state would be high (1 = high, 0 = otherwise).
How the Data looks like. (2015 - 2018)

train = gData_dummy$Year<2018  # equal to the year less than 2005 (year 2001 to 2004)
glm.fit <- glm(Level_d ~ Year + State,
               data = gData_dummy,
               family = binomial("logit"), maxit = 100,
               subset = train)
summary(glm.fit)

Result
Comment: It looks like `glm()` didn't converge.  Why did you limit it using `maxit = 100`?
Comment: Actually am not sure as well, I saw someone else's code that fixed their the model that did not converge by using maxit, so I tot I'd try @user2554330
Answer: This is more of a question for cross-validated, but...
The regression here failed to fit, most likely because you have columns which are linear combinations of others (i.e. they are redundant). What you can do is to fit a regularized model such as the ones from the package glmnet.


Existe alguma maneira de se descobrir um erro ocorrido em json_decode?

Question: Bem, o PHP tem uma coisa que as vezes considero ruim: Algumas funções lançam Warning quando algum erro ocorre, e já outras, retornam False ou Null.
Um caso que exemplifica isso é o fopen.
fopen('non-exists.txt', 'r');

Gera um Warning:

PHP warning:  fopen(non-exists.txt): failed to open stream: No such file    or directory

Porém existem outros casos que não há nenhum aviso de erro, como no caso das funções json_decode e json_encode.
Um pequeno exemplo ao tentar decodificar um json mal-formado:
 json_decode('{'); // null

Eu preciso nesse caso saber que a string JSON está mal formada, pois as vezes esse JSON vem de fontes externas, e creio que seria interessante enteder porque alguma operação deu erro, e não somente retornar Null.
Existe alguma maneira de descobrir qual é o erro ocorrido nessas funções de Json do PHP?
Quer dizer, não estou falando de fazer um if e saber se tem um erro, mas estou falando de detalhar qual foi o problema ocorrido ao tentar usar a função.
Comment: A resposta que for dada aqui vai ajudar quem teve problema aqui: http://pt.stackoverflow.com/questions/21334/json-retorna-null-com-caracteres-especiais?rq=1
Answer: Para detectar um json inválido ou mal formatado use a função json_last_error_msg() para obter uma descrição da 'categoria' do problema. Essa função está disponível no php5.5 ou superior.
$arr = json_decode('{') or die(json_last_error_msg()); //Syntax error

O código acima é apenas um exemplo, o die() pode ser trocado por um if.
Em versões anteriores pode-se utilizar exemplo:
$arr = json_decode('{') ?: json_last_error();//4 representa o erro de sintaxe.

Comment: Ótimo, você deu a resposta certinha... O problema é que, se tiver `json_decode('false')` vai fazer com que o `or` caia no erro..
Comment: +1 cara, valeu..
Comment: Parece que essa função só funciona para versões `5.5>=` do PHP :(
Comment: @WallaceMaxters sim, mas tem outra forma de pegar a descrição, através do código de retorno.


Como buscar o segundo maior número

Question: Preciso buscar a foto com o segundo maior salario
<?php

    $host = "localhost";
    $username = "root";
    $password = "";
    $db = "imagem";

    mysql_connect($host,$username,$password) or die("Impossível conectar ao banco.");

    @mysql_select_db($db) or die("Impossível conectar ao banco");

    $img=mysql_query("SELECT * FROM arquivo WHERE sal < (SELECT MAX(sal)FROM arquivo)") or die("Impossível executar a query");

    while($row=mysql_fetch_object($img)) {
        echo "codigo' \">";
     }
?>

Comment: a do rray deu certo!! obrigado pessoal
Answer: Pode criar duas consulta a mais interna pega os dois maiores registros e ordena do maior para o menor (DESC), com isso a consulta mais externa pega o resultado e intervete ele ou seja ordena do menor para o maior (ASC) e retorna apenas uma das duas linhas.
SELECT sal FROM
      (SELECT t.sal FROM cad as t ORDER BY t.sal DESC LIMIT 2)
ORDER BY sal ASC LIMIT 1

Adaptando no php (comentário) fica:
<?php
    $img = mysql_query("SELECT sal FROM (SELECT t.sal FROM cad as t ORDER BY t.sal DESC LIMIT 2) ORDER BY sal ASC LIMIT 1") or die(mysql_error());
    $row = mysql_fetch_object($img);

    echo !empty($row) ? "codigo'>" : '';

Comment: ae deu certo, obrigado
Comment: porem, só posso exibir na tela o segundo maior salario
Comment: @GuilhermeFelipeSimão vc fez um while? pode fazer duas chamadas de `mysql_fetch_asso()` o primeiro vc descarta e o segundo usa.
Comment: nao consegui fzr, te explicar melhor, preciso buscar a foto, que contenha o segundo maior salario.
Comment: codigo' \">";
     }
 ?> nossa ficou muito ruim pra ver, tentei mas n deu tbm
Comment: @GuilhermeFelipeSimão editei minha resposta, se precisar retornar mais um campo faça nos dois selects.
Comment: SELECT sal FROM
          (SELECT t.sal FROM cad as t ORDER BY t.sal DESC LIMIT 2)
    ORDER BY sal ASC LIMIT 1

Esse ?
Comment: @GuilhermeFelipeSimão isso.
Comment: tentei, mas n chegou exibir nada dessa vez. Coloquei assim:

$img=mysql_query("SELECT * FROM (SELECT t.sal FROM arquivo as t ORDER BY t.sal DESC LIMIT 2)
ORDER BY sal ASC LIMIT 1") or die("Impossível executar a query");
Comment: @GuilhermeFelipeSimão editei novamente a resposta.
Answer: Poderia ser assim:
SELECT MAX( SAL ) FROM CAD WHERE SAL < ( SELECT MAX( SAL ) FROM CAD )

Comment: Demonstrando a ideia do Reginaldo: http://sqlfiddle.com/#!9/596334/2. Veja que o resultado é 60, o segundo maior, apenas.
Comment: possuo muitos registros, assim ele exibiria todos os menores do maior salario.
Comment: Não. Exibe apenas um.
Comment: ele exibiu tods =/
Comment: Hmmmm.. O select puro como está traz apenas um registro, mesmo que haja valores repetidos. Se esta trazendo vários, penso que o problema esteja em outro lugar.
Comment: Se ´SELECT MAX(sal) FROM cad´ traz apenas um, por que ´SELECT MAX( SAL ) FROM CAD WHERE SAL < ( SELECT MAX( SAL ) FROM CAD )´ traria vários?
Comment: vou fazer outro post la embaixo, com o código como está, de uma conferida .

Como buscar o segundo maior número


É possível vincular uma string ao nome de uma nova instancia em C#?

Question: Usando C#, gostaria que o valor de uma string fosse utilizado (ou vinculado) ao nome de uma nova instancia de um objeto. No caso do exemplo abaixo onde há XXX gostaria que recebesse o valor da string NumeroDaConta.

namespace ConsoleApp9

{

    public class Conta
    {
        public string Nome;
        public int Teste;
    }
}

using ConsoleApp9;

Console.WriteLine("Digite um numero");

public int Count = int.Parse(Console.ReadLine());

public string NumeroDaConta = "Conta " + Count.ToString();

public Conta XXX = new Conta();

Comment: as variáveis são declaradas antes de se instanciar, antes do código ser compilado, então não é possível fazer algo assim, e ainda assim, imagina que precisa usar a variável XXX abaixo, o programa precisa saber exatamente o nome da variável antes de usar. Por curiosidade, para que precisa fazer isso? explique melhor a necessidade, talvez um dicionário te ajude mas é preciso entender melhor o que pretende
Comment: Muito obrigado cara. Na verdade, queria criar um laço de "while" e ir adicionando instancias com o nome incrementado a partir dessa string. Por exemplo, enquanto o usuário fosse digitando o número um novo objeto do tipo Contaé criado (Conta 1, Conta 2, Conta 3...)
Comment: mas pra que vai fazer isso não após instanciar quando o programa estiver sendo executado não vai poder ver os nomes? Pooderia por exemplo fazer assim : `var contas = new List()` e ir adicionando: `contas.Add(new Conta())`, mas isso é só para exemplificar.. já um dicionário poderia identicar com um nome, assim por exemplo: `var contas = new Dictionary()` e a seguir: `contas.Add("NumeroDaConta", new Conta());`, talvez isso ajude a entender, mas ainda assim não me parece uma boa ideia
Answer: É possível, mas não é adequado fazer isso. E em geral você não precisa disto. Cada conta criada virará uma variável no código?
Bem, vamos conceituar melhor as coisas. Tem diversos erros nesse código, mas não vou falar sobre eles porque não é foco da pergunta, só recomendo procurar por melhores fontes ou outro método para aprender, se treinar o erro é ele que fará para sempre.
Uma instância não tem nome no código. Nome para indicar um local na memória onde tem um objeto (uma instância), de forma direta ou por indireção, é chamado de variável (mais). E é isso que parece querer usar no lugar do XXX.
Se o problema pedir por isso mesmo então é possível até ter várias variáveis existindo na execução do programa, mas não expressadas individualmente no código, e sim durante a execução. Essas variáveis são consideradas indexáveis e o array é a forma mais simples e famosa de fazer isto (embora seja mais provável que uma List seja melhor opção).
Outra forma muito utilizada, mas menos falada é o dicionário, que pode ser usado de diversas formas, sendo a mais comum no .NET o Dictionary. A diferença é justamente que o índice é uma string (pode ser outros tipos, mas string é o mais comum). É exatamente o que deseja. Parece.
Isso pode não ser uma boa solução, para funcionar corretamente terá que ter muitos cuidados, qualquer mínimo erro no uso da aplicação e não obterá o resultado esperado. Índices assim são ruins, especialmente quando é controlado pelo usuário e não pelo programador. O programador pode ajudar dar menos problemas ou pelo menos detetá-los, mas dá trabalho e é fácil esquecer de algo possível, usuário precisa ser estudado pela NASA.
Códigos e números existem no mundo, não é nem só na programação para evitar esse erro. A lista seria bem mais útil com eles, e só porque é um experimento, não entenda que isso é um código real que poderá ser usado em aplicação em produção. Se os códigos podem ser esparsos o dicionário ainda pode ser útil, já que um array ou lista precisa de uma sequência contínua, mas já é melhor que um texto.
Número da conta deveria ser uma string ou um número? O usuário escolhe o número que bem entender, do jeito que quiser? Experimenta digitar uma letra quando pede um número. Percebe como tem muitos erros conceituais? Números de conta costumam ser sequenciais, ainda que a pessoa possa escolher um deles.
As outras formas (reflexão, injeção de código usando o Roslyn) são bem mais complicadas, ineficientes e "erradas", mesmo que funcione.
Escolha fugir da pseudo qualificação.


Why do I keep getting an infinite loop whenever a non-integer type is inputted?

Question: #include <stdio.h>
#include <stdlib.h>
/*
 * GETOP -- get an integer operand; we do operation-specific checks later
 *
 * parameters: opno     the number of the operand (as in 1st, 2nd, etc.)
 * returns: int         the integer value of the operand just read
 * exceptions: none */

int getop(int opno)

 {
    int val;
    int rv; /* value returned from scanf */
            /* a
             * loop until you get an integer or EOF
             *
             * prompt and read a value */
    do{
        /* prompt and read a value */
        printf("\toperand %d: ", opno);
        rv = scanf("%d", &val);
        /* oops */
        if (rv == 0)
        {
            printf("\toperand must be an integer\n");
            /* loop until a valid value */
        }
        while (rv == 0);
            /*
             * if it's EOF, say so and quit
             */
        if (rv == EOF)
        {
            exit(EXIT_SUCCESS);
        }

     /*
             * otherwise, say what you read
             */
        return(val);

    }

/* When I wrote rv == 0, it keeps giving me an infinite loop. Am I writing something wrong or is there another way to check for non-integers without the program going into an infinite loop? */
Answer: Because when scanf sees input that doesn't match its format, it simply stops reading, and leaves the invalid input in the buffer. So next time you try to read, you will attempt to read the exact same invalid input again, and again, and...
A simple solution is to read a line using fgets and then use sscanf to get the data.
Comment: Ok I understand. Thanks!
Comment: Or check the return from `scanf`, which will tell you how many format codes were successfully parsed.


Why only accuracy is used in meta and few-shot learning as evaluation parameters?

Question: I was going through many state-of-the-art papers in Meta-learning and few-shot learning, and I found that almost all use "accuracy" as evaluation criterion. Unlike other domains like object detection, NLP and other machine learning domain which use precision, recall, MAP, ROC etc. apart from accuracy. Why meta-learning uses only accuracy and no other parameter?
Comment: [Why is accuracy not the best measure for assessing classification models?](https://stats.stackexchange.com/q/312780/1352) The same problems also apply to precision, recall etc. AUC is slightly better: https://stats.stackexchange.com/q/339919/1352
Answer: One reason is coming from the dataset structure: Consider the few-shot learning, suppose we are trying to identify a person from one photo in testing/production stage, and in training stage, we only have few photos for each person.
The natural question can be, if the model says the photo is person A, how accurate it can be (such metric can be extended into top N accuracy instead of top 1 accuracy).
Note that, the concept of the "precision" and "recall" cannot be easily applied there.
Because in testing stage, we only have one photo.

For precision, we are checking the "false positive rate", we need to have more test instances tested for a given person and check the how much false positives are there.

For recall, it means how many "true positive" we can find in a large dataset (assume we have a lot of positives). For few-shot learning use case, this is also not applicable: where we only have very few true positives (very few instances for each class).


Running 32 bit userland on Redhat

Question: I have a third party python API thats only available in 32 bit (ELF 32-bit LSB shared object) that I need to run on a x64 RedHat server. Of course this doesn't work smothly since the python installation is 64 bit. I figure that the way to solve this is to run python as 32 bit userland on the 64 bit kernel.
How do I do this? Do I have to compile a 32 bit python on RedHat x64? Is there an easier way?
# cat /etc/redhat-release
Red Hat Enterprise Linux Client release 5.3 (Tikanga)
Answer: You should be able to easily install a 32-bit redhat chroot and run your python from that. You can use Mach to generate the chroot, then just chroot into the directory that you built with Mach and run your script from there.
More information can be found:

http://www.psg.com/~brian/howto/mach.html
http://fedoraproject.org/wiki/UsingMach

Running 32 bit userland on Redhat


Internet Explorer sempre em primeiro plano

Question: Estou usando IE no modo Kiosk e apareceu um erro de outro app na frente. O que gostaria de fazer é que caso apareça alguma outra janela no Windows, não apareça por cima da janela do IE, deixando-o sempre em primeiro plano.
Answer: Você pode deixar a janela do IE em modo sempre na frente através da biblioteca do windows.
Se você quer um programa que faça isso para você, eu sugiro utilizar o TurboTop. Ele é bem simples de usar, só instalar e selecionar na bandeja de ícones qual a janela você quer que fique sempre na frente.
Se quiser fazer isso programaticamente, utilize a função SetWindowPos da biblioteca do windows:
SetWindowPos Function - MSDN
Você pode usar a biblioteca do windows em qualquer linguagem, vou dar um exemplo da utilização em python, com o modulo Pywin32, colocando a janela do Notepad sempre no topo:
<code>import win32gui
import win32con

hwnd = win32gui.FindWindow('Notepad', None)
win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 100, 100, 300, 200, 0)
</code>
Fonte do código: StackOverFlow - Set another program to always be on top?


MySQL efficient data to junction table query

Question: The problem:
I want to move the links of the categories from the table companies_1 into the company_categories table. The company_id in the company_categories table need to be equal to the id of the companies_2 table. The records of the companies_1 and the companies_2 table are linked by the "name"-column.

The current code below took me over a night, still unfinished! I want to learn to be more efficient and speed this progress up. I feel like there is very much to optimize because there are A LOT of company records.
Another issue was that i found no way how to check where my query was while looping (resulting in no way to check the progress). Because the progress took so long i killed the query and I'm searching for a better way to solve this issue.

The information:
There is a table with companies like:
----------------------------------------
| companies_1                          |
----------------------------------------
| id   |  category_id   | name         |
----------------------------------------
| 1    |  1             | example-1    |
| 2    |  2             | example-1    |
| 3    |  1             | example-2    |
| 4    |  2             | example-2    |
| 5    |  3             | example-2    |
| 6    |  1             | example-3    |
----------------------------------------

A table with the DISTINCT company names:
-------------------------
| companies_2           |
-------------------------
| id   |   name         |
-------------------------
| 1    |   example-1    |
| 2    |   example-2    |
| 3    |   example-3    |
-------------------------

A categories table, like:
-------------------------
| categories            |
-------------------------
| id   |  name          |
-------------------------

And a junction table, like:
---------------------------------
| company_categories            |
---------------------------------
| company_id   |  category_id   |
---------------------------------

The current code:
This code works, but is far from efficient.
DELIMITER $$
 DROP PROCEDURE IF EXISTS fill_junc_table$$
 CREATE PROCEDURE fill_junc_table()
 BEGIN
 DECLARE r  INT;
 DECLARE i  INT;
 DECLARE i2  INT;
 DECLARE loop_length  INT;
 DECLARE company_old_len  INT;
 DECLARE _href  VARCHAR(255);
 DECLARE cat_id  INT;
 DECLARE comp_id  INT;

 SET r = 0;
 SET i = 0;
 SET company_old_len = 0;
 SELECT COUNT(*) INTO loop_length FROM companies;

 WHILE i  < loop_length DO
  SELECT href INTO _href FROM company_old LIMIT i,1;
  SELECT id INTO comp_id FROM companies WHERE site_href=_href;
  SELECT COUNT(*) INTO company_old_len FROM company_old WHERE href=_href;
  SET i2 = 0;
  WHILE i2  < company_old_len DO
   SELECT category_id INTO cat_id FROM company_old WHERE href=_href LIMIT i2,1;
   INSERT INTO company_categories (company_id, category_id) VALUES (comp_id, cat_id);
   SET  r = r + 1;
   SET  i2 = i2 + 1;
  END WHILE;
  SET  i = i + 1;
 END WHILE;

 SELECT r;
 END$$
DELIMITER ;

CALL fill_junc_table();

Edit (new idea):
I am going to test another way to solve this problem by fully copying the companies_1 table with the following columns (company_id empty on copy):
---------------------------------------------
| company_id   | category_id  |  name       |
---------------------------------------------

Then, I will loop through the companies_2 table to fill the correct company_id related to the name-column.
I hope you can give your thoughts about this. When I finish my test I will leave the result over here for others.
Comment: Wow a procedure seems not very effective ! 1 question, where is the column href in your examples tables ?
Comment: Yes the procedure way was a bit nonsense... The href is the name of the company, sorry for the mistake.
Answer: To clarify, I don't see any PIVOT transformation in the company_categories. What I see is you want a JUNCTION TABLE because it seems that companies and categories tables have many-to-many relationship.
In your case, you have company which has multiple categories. And you also have categories assigned to multiple companies.
Now base from your requirement:

I want to move the links of the categories from the table companies_1
  into the company_categories table. The company_id in the
  company_categories table need to be equal to the id of the companies_2
  table. The records of the companies_1 and the companies_2 table are
  linked by the "name"-column.

I arrived with this query:
INSERT INTO company_categories (company_id, category_id)
SELECT C2.id
    , C1.category_id
    FROM companies_1 C1
    INNER JOIN companies_2 C2 ON C2.name = C1.name

Let me know if this works. The nested loops that you created will really take a while.
As @DanielE pointed out, this query will work in the assumption that company_categories is empty. We will need to use UPDATE otherwise.
Comment: As you are doing an insert, if there is already some lines in company_categories you may duplicate lines. I will rather go on an update to avoid that
Comment: I see. Let's truncate the `company_categories` then HAHA!
Comment: Your approach is really clean! Glad to learn, My thoughts were way too difficult with the loops and the huge amounts of queries.
Comment: Actually, I chucked down the data and placed it in many temporary tables to optimize performance since I'm dealing with millions of records. I used partial code from @Nick since his code was easier to chunk (the select statement felt/is easier to limit). I tried your answer on an not-indexed database and it took over 24 hours to complete LOL. Nonetheless you answer was amazing, and I will accept it since it is the cleanest way to solve the problem I stated.
Comment: There is a risk of duplicates here, no ?
Comment: Which part can have duplicates? I am assuming that `companies_1` is the main table. :)
Comment: company_categories ;)
Comment: @DanielE. I mean which column will be duplicated?
Comment: @JustDevelop thanks! Just keep on exploring and you'll learn a lot! If my answer solved your problem, you can accept it ;)
Comment: I see, there are many factors affecting the execution plan of each queries. Your schema design, normalization, indexes are just a few :) Thanks for accepting!
Answer: Why not just update companies_1?
ALTER TABLE companies_1 ADD (company_id INT)
UPDATE companies_1 SET company_id = (SELECT id FROM companies_2 WHERE name=companies_1.name)
ALTER TABLE companies_1 DROP name, RENAME TO company_categories
SELECT * FROM `company_categories`

Output
id  category_id company_id
1   1           1
2   2           1
3   1           2
4   2           2
5   3           2
6   1           3

Comment: Great solution, but the downside is that the old data is gone (not too bad, but since I was experimenting with loads of data I prefer a backup). Nonetheless I used parts of your code to accomplish my goal. Thank you for your great help!


Client side validation gem for simple form not working

Question: <%= simple_form_for (@category), :validate => true do |f| %>
  <%= f.error_messages %>

        <%= f.hidden_field :user_id, :value => current_user.id %>

      <%= f.input :category, :size =>20, :title => "Enter Category Name"  %>
    <%= f.submit %>
<%= f.submit "preview", :style => 'width:100px;', data: {:disable_with=>"loading preview..."} %>

<% end %>

the validation working only when i click on the submit button not when tab out of the field, I have also generated the rails.validation file under the initializer directory, and added the assets for simple form in application.js also
Answer: You have to add rails.validations.simple_form after rails.validations. Also you could either add as follows:
(<%= javascript_include_tag "application", "rails.validations", "rails.validations.simple_form" %>)

Or,
In app/assets/javascripts/application.js:
//= require rails.validations
//= require rails.validations.simple_form

Then change javascript_include_tag to following in app/views/layouts/application.html.erb:
<%= javascript_include_tag "application" %>

Comment: yes dipak jadam i have followed the steps in rail cast completely the link is here http://railscasts.com/episodes/263-client-side-validations?view=comments
Answer: There is a lot at work in the process you describe, so adding more detail would be good. For instance: clarify the difference between what happened, and what you expected to happen here. Understanding how this gem functions is also important--perhaps it is working as it should. This kind of action is based on events your browser generates, and clicking a button is quite different than the blur event, for example. It's possible the gem only operates on certain events, in other words. Expand on the situation!
Answer: I believe simple_form's error messages are just passing on Rails validation errors. It sounds like you want some client-side validation. Maybe you want to look into using some front-end js to match your rails validation requirements.
EDIT:
I noticed in a comment you referred to a railscast episode you followed Take note! That's a three-year old episode. I saw in the comments of the railscast someone recommending this gem as a current alternative:
https://github.com/kalkov/rails4_client_side_validations
It's not well supported either but you might have better luck if you're using rails 4.
Comment: It works for form_for but not for simple_form_for plugin


How to remove all commerce coupon from database?

Question: Hi I have used commerce coupon module. I have lot of entries of used coupon. So, right now I want to delete all commerce coupon from database. I have 860000 coupon entries in database. How can I delete all entries using custom module functionality?
Answer: Since coupons are entities, you'll want to delete them using the entity functions. As an example, if I want to delete all disabled coupons, something like this should work:
<code>$disabled_coupons = commerce_coupon_load_multiple(array(), array('status' => 0));
$disabled_coupon_ids = array_keys($disabled_coupons);
commerce_coupon_delete_multiple($disabled_coupon_ids);
</code>
If you need to batch them, you may want to try doing them in batches of 250-1000... not sure how many can be deleted successfully per batch. Then just let it run.


Como comparar um CEP dentro de um array com vários intervalos em PHP?

Question: Eae pessoal!
Estou precisando de uma sugestão de como filtrar um CEP dentro de uma lista que vai possuir vários intervalos, se (IF) o CEP estiver dentro dessa lista, executar um HTML que possui algumas strings em PHP.
Basicamente o CEP de teste seria 33100-100
E na lista de CEP seria por exemplo:
<code>01000-000...01599-999
02000-000...02099-999
08000-000...08099-999
06300-000...06399-999
00640-000...06499-999
</code>
Como o CEP 33100-100 não participa da lista, não apareceria nada.
Se tentássemos com o CEP 01099-999 ele aceitaria e executaria o HTML.
O problema é que a lista de CEP é relativamente bagunçada e possui vários intervalos diferentes. No momento já são 85 intervalos, e talvez aumente no futuro, então queria algo prático pra facilitar na inclusão de novos intervalos no futuro. Alguém tem alguma ideia?
Answer: Penso que pode ser assim:
<code>//cep a ser encontrado
$cep="03100-100";
//array com os intervalos dos ceps
$array=array(
    array("01000-000","01599-999"),
    array("02000-000","02099-999"),
    array("08000-000","08099-999"),
    array("06300-000","06399-999"),
    array("00640-000","06499-999")
);
//foreach para buscar dentro do array de intervalos
foreach($array as $key=>$value){
    //$value assume o papel dos arrays contidos dentro do array principal, sendo assim o offset [0] para o inicio do intervalo e [1] para o fim
    if($cep >= $value[0] && $cep <= $value[1]){
        echo "bingo ".$key;
        break;
    }else{
        echo "desculpe cep não encontrado";
    }
}
</code>
Desculpe pela bagunça, foi apenas para você ter um ideia do que fazer. Espero ter ajudado.
Comment: Obrigado! Deu certinho :D


Redshift Python encrypt/decrypt UDF Error - String contains invalid or unsupported UTF8 codepoints

Question: I want to create the below encrypt and decrypt UDF's in Redshift.
Library:
create library pyaes
language plpythonu
from 's3://aws_python/library/pyaes/pyaes.zip'
credentials 'aws-role'
region as 'aws-region';

Encrypt:
CREATE OR REPLACE FUNCTION test.aes_encrypt(input varchar(max))
RETURNS varchar(max) AS
'    if input is None:
        return None
    import pyaes
    key = ''abcdefghijklopoo''
    aes = pyaes.AESModeOfOperationCTR(key)
    encrypted_msg = aes.encrypt(input)
    return encrypted_msg
'
LANGUAGE plpythonu STABLE;

Tried with below options as well:
encrypted_msg = aes.encrypt(input.encode("utf8"))
 key = key.encode('utf-8')

Decrypt:
CREATE OR REPLACE FUNCTION test.aes_decrypt(encrypted_msg varchar(max))
RETURNS varchar(max) AS
'
    if encrypted_msg is None or len(str(encrypted_msg)) == 0:
       return None
    import pyaes
    key = ''abcdefghijklopoo''
    aes = pyaes.AESModeOfOperationCTR (key)
    decrypted_msg = aes.decrypt(encrypted_msg).decode("utf8")
    return decrypted_msg
'
LANGUAGE plpythonu STABLE;

select aes_encrypt('Testing');
select aes_decrypt('');
But it's throwing below error:
Error: Invalid operation: String contains invalid or unsupported UTF8 codepoints. Bad UTF8 hex sequence: d5 fc (error 4);
Please advise. Thanks in advance.
Comment: Why do you believe that the input was UTF-8 in the first place?
Comment: Hi, When I created the function and use it in select statement its throwing error: Invalid operation: String contains invalid or unsupported UTF8 codepoints. Bad UTF8 hex sequence: d5 fc (error 4); Hence I have tried to use utf-8 encoding but still its throwing the same error.
Comment: Please suggest on this. Not sure where I'm doing wrong in this code.
Comment: @IgnacioVazquez-Abrams Could you please advise? https://stackoverflow.com/questions/51735127/redshift-udf-aes-encrypt-no-module-named-crypto-cipher-mode-ecb
Answer: I worked this through in a notebook where I could view the outputs.  Basically an error occurs while the AES function is happening that you can't see the output of, and it passes a bad type back to Redshift because aes returns as a byte.
PLEASE NOTE: THIS IS MEANT FOR ENCRYPTED OBFUSCATION OF COLUMNS OF DATA and is not server side encryption.  If security is critical please go encrypt your whole database.  If you're going to use this to protect your customers data, please don't, please encrypt salt and hash everything etc etc.  THIS IS your DISCLAIMER.
This needs to be converted to something redshift can deal with like hex, so use
'binascii.hexlify(cipher_txt)' to get the value back in a printable manner.
CREATE OR REPLACE FUNCTION aes_encrypt(input VARCHAR(20000))
RETURNS VARCHAR STABLE AS $$
  import pyaes
  import binascii
  if input is None:
    return None
  key = 'abcdefghijklnosp'
  aes=pyaes.AESModeOfOperationCTR(key)
  cipher_txt=aes.encrypt(input)
  cipher_txt2=binascii.hexlify(cipher_txt)

  return str(cipher_txt2.decode('utf-8'))

$$ LANGUAGE plpythonu ;

Note the decode on return is redundant, and the cipher texts are split into lines to illustrate.  I'm sure you can put them back into one line.  like
    cipher_txt=binascii.hexlify(aes.encrypt(input))
To unencrypt:
CREATE OR REPLACE FUNCTION aes_decrypt(encrypted_msg varchar(max))
RETURNS VARCHAR STABLE AS $$
  import pyaes
  import binascii
  if encrypted_msg is None or len(str(encrypted_msg)) == 0:
       return None
  key = 'abcdefghijklnosp'
  aes = pyaes.AESModeOfOperationCTR(key)
  encrypted_msg2=binascii.unhexlify(encrypted_msg)
  decrypted_msg2 = aes.decrypt(encrypted_msg2)
  return str(decrypted_msg2.decode('utf-8'))
$$ LANGUAGE plpythonu ;

Also - side note - you don't need to specify length of the return varchar unless you need to for some other reason (unions or some-such) - because making everything max is a waste of space.


Meaning of the phrase: To drive the ball "on the up"?

Question: I have heard many times cricket commentators referring to players driving the ball 'on the up'. By the context, this is taken to be a difficult and risky thing to do and makes a good shot even better as the batsmen is displaying some boldness by playing the shot to that delivery. As in "that was a fine cover drive, and he was playing the ball on the up".
Why is this confusing to me? Anytime that you play a drive, the ball is 'coming up'. The only exception would be a full toss, but that is an unusual delivery so it would be hardly remarkable to play any shot to a non full toss.
There are many examples of nonsensical phrases in sports commentary and the viewer just needs to learn what a particular nonsensical phrase means. In this case I've never understood what is being meant by this. My best guess is that the ball is fuller or shorter than a delivery that you would normally expect to see a drive played against (one or the other, it couldn't mean both). Any ideas?
Answer: According to the weighty cricket coaching tome Bob Woolmer's Art and Science of Cricket, driving 'on the up' means "hitting the ball as it rises off a good length" (p.134). On page 138-139 there is a discussion of the technicalities of doing so.
The key point appears to be that it is played to a good-length ball, rather than a half-volley, and thus requires excellent timing: it is "risky, but to the well-set batsman on a good pitch it can pay excellent dividends" (p.138).
Answer: This is really a good question. As per wikipedia:

On the up:
  a batsman playing a shot, usually a drive, to a ball that is
  quite short and has already risen to knee height or more as the shot
  is played.

Usually the commentators refers it as the shot played when the ball is pitched on a good length where it is difficult to drive the ball as the ball would have already risen to knee level. Batsmen will be usually confused on what shot to play for these kind of balls as the length will be in between full pitched ball and short pitched ball. So it is not an easy shot to play it on the up.
Here is a commentatory from cricinfo which indicates this:

33.3
Chigumbura to Mominul Haque, OUT, guess what's happened again, won't
  be too hard, another one has given it away, Mominul tries to punch a
  good length ball through extra cover, instead, he's scooped it for the
  man at extra cover to take a dolly, Mominul had got across and was
  close to the line, tried to slap it away on the up in the end but it
  bounced Mominul Haque c SW Masakadza b Chigumbura 23 (66m 41b 2x4 0x6)
  SR: 56.09
Answer: I have found only this from link-
here

This term refers to hitting the ball when the bat is on the upswing in the follow through of the shot. It is heard often, but rarely explained.

and this from link here

Making contact with the ball before it reaches the top of the bounce - hitting it on the rise. Viv Richards was a prominent exponent

and lastly this from here

When a ball is rising from the ground after bouncing on it, if the batsman hits the ball before it has completely reached the top of the bounce, it is said that the batsman has hit it "On the up." One famous example of such a type of batsman is Vivian Richards of West Indies.


Can you use es6 import alias syntax for React Components?

Question: I'm trying to do something like the following, however it returns null:
import { Button as styledButton } from 'component-library'

then attempting to render it as:
import React, { PropTypes } from "react";
import cx from 'classNames';

import { Button as styledButton } from 'component-library';

export default class Button extends React.Component {
    constructor(props){
        super(props)
    }
    render() {
        return (

        )
    }
}

The reason is, I need to import the Button component from a library, and also export a wrapper component with the same name but maintaining the functionality from the imported component.  If I leave it at import { Button } from component library then of course, I get a multiple declaration error.
Any ideas?
Comment: React component should start with a capital letter : dont use `styledButton` but `StyledButton`
Comment: Why you can't change class Button name?
Comment: @Ved I'm using react-styleguidist to display every component, and need to wrap all the components in the component library.  If I change the class Button name, the `show code` would have different names for every component in the playground.
Comment: ya as @topheman said, Alias name should be in pascal case  -->AliasName
Answer: Your syntax is valid.  JSX is syntax sugar for React.createElement(type) so as long as type is a valid React type, it can be used in JSX "tags".  If Button is null, your import is not correct.  Maybe Button is a default export from component-library.  Try:
import {default as StyledButton} from "component-library";

The other possibility is your library is using commonjs exports i.e. module.exports = foo.  In this case you can import like this:
import * as componentLibrary from "component-library";

Update
Since this is a popular answer, here a few more tidbits:
export default Button              -> import Button from './button'
                                      const Button = require('./button').default

export const Button                -> import { Button } from './button'
                                      const { Button } = require('./button')

export { Button }                  -> import { Button } from './button'
                                      const { Button } = require('./button')

module.exports.Button              -> import { Button } from './button'
                                      const { Button } = require('./button')

module.exports.Button = Button     -> import { Button } from './button'
                                      const { Button } = require('./button')

module.exports = Button            -> import * as Button from './button'
                                      const Button = require('./button')

Comment: Hmm, doesn't seem to work;
Button is defined int he library as:
```import { ButtonCore } from 'react-atlas-core';
import { ButtonStyle } from 'react-atlas-default-theme';
export const Button = CSSModules(ButtonCore, ButtonStyle, {allowMultiple: true}); ```
Comment: no use of default export; so i have NO IDEA why it wouldn't return anything.
Comment: i'd probably re-evaluate what you think is going wrong, since that syntax and your intention is valid.  What exactly is the error you are getting?
Comment: There was no error, and everything renders without an error.  Except that there is no `Button` functionality that is getting imported from the library.
Answer: Try to import this way
import {default as StyledLibrary} from 'component-library';

I suppose you export
export default StyledLibrary

Answer: Careful with capitalisation. Best to always CamelCase.
One:
import Thing from "component";

One with alias:
import {Thing as OtherThing} from "component";

One with alias plus other defaults:
import {Thing as OtherThing}, Stuff, Fluff from "component";

More detailed example
import
{Thing as StyledButton},
{Stuff as Stuffing},
{Fluff as Fluffy},
Wool,
Cotton
from "component";

Answer: User-Defined Components Must Be Capitalized
https://reactjs.org/docs/jsx-in-depth.html#user-defined-components-must-be-capitalized
change your code to
import { Button as StyledButton } from 'component-library';
....bah...bah....bah

Answer: No idea why I am not able to alias the import;
As a work around, I ended up doing this:
import React, { PropTypes } from "react";
import * as StyledLibrary from 'component-library';

export default class Button extends React.Component {
    constructor(props){
        super(props)
    }
    render() {
        return (

        )
    }
}

Thanks all
Answer: note that when you capitalized StyledLibrary and it worked
whereas, in the original question, you did not capitalize styledButton and it did not work
both of these are the expected results with React
so you didn't discover a workaround, you simply discovered the (documented) React way of doing things
Comment: Thats interesting... Maybe I didn't catch that before.  Could you point to where the documentation is for requiring Components be PascalCase?  I was always under the assumption that React wanted component names match their filenames (since they are classes).