Thinking outside of the box

Question: You want to draw a circle with a 4 inch radius.  A trivial task for you and your trusty compass.  When you go to grab your compass which has not had much love for a while you find it is rusted shut; stuck at 5 inches.  Is it still possible to complete the task and draw a perfect circle with a 4-inch radius using the compass that can only draw circles with a 5-inch radius?
You may use other things as well to solve the problem such as a straightedge.
Comment: No, you already answered the question for yourself. If it could draw a circle with a 4-inch radius, then it would not be a compass "that can only draw circles with a 5-inch radius", would it?
Comment: you can if you have infinite time you can construct a segment of  4 inch distance, but not draw a circle of 4 inch so draw a circle of 5 inch and then in every direction draw a segment of four
Answer: My first approach
Yes, just place the center 3 inches above the paper. If that is a possibility?
Different rendering
To put it differently, if you like, you could draw a 5-inch-circle, use scissors to cut a radius, then form a cone by overlapping at the cutting line until the proportion of the height to the radius of the base is 3 to 4...
That will happen when the overlap covers $\frac{1}{4}$ of the surface of the cone...
Illustrations
To fully earn your votes, here is an illustration:

To see the situation from arbitrary angles, consult the following dynamic 3D-graph:
GeoGebra-illustration
In particular, see what it looks like from above - the $1:4$ proportion of the overlap becomes evident!
Comment: Very nice.  Yes I agree the first answer wins in practicality, but the second is more imaginative.  I asked this question to open a talk I gave and the only answer I got was WD-40.
Comment: @DavidCaliri: I added an illustration of the cone-solution. Be sure to visit the link adress to see the dynamic version of it!
Comment: Yes that is one solution.  You can use a 3 inch block to raise the center.
Comment: I think the initial one-line solution was perfect because it was also practical. IMHO, no need to add anything...
Comment: @Vadim: You are quite right, although my last edit suggests a way to measure the exact result by eye, if I am not mistaken :)
Comment: And just at a suggestion to anyone who takes interest in the dynamic 3D-version given in the link: feel free to download GeoGebra (it is free) for yourselves and download the document - then the rendering will run smoothly without delays and lacking in performance.
Answer: Lay a ruler along one side of the compass with zero mark adjacent to the axis of the compass. set a pencil at the 4 inch mark of the ruler and maintain it there as you rotate the compass and ruler as a unit, completing a full rotation.

Thinking outside of the box


Condense a time series into a given number of points

Question: What is the term for condensing a time series consisting of an arbitrary number of points (X), into one consisting of K points? Google searches for "condensing" or "reducing array" weren't fruitful, so I'm probably using the wrong term.
<code>[1, 3, 7, 9] -> [2, 8]  // X=4, K=2
</code>
Also, how should this be done for non-trivial cases, e.g. when X is not a multiple of K (condensing 150 points into 100 points) or when there are gaps in the timeline?
Comment: @Tim: that's pretty much the problem :) I thought the example would help... in the meantime I found out that "downsampling" might describe what I'm looking for, though none of the [three common methods I've seen](https://www.npmjs.com/package/downsample) produces the intended result from my example (simply averaging by X/K buckets).
Comment: What do you mean by "condensing"?
Comment: Based on Gascoyne's clarifications of his original question, I have deleted my answer as not relevant.
Answer: I am not aware of a canonical term, but there are hits for "time series compression".
I give a simple approach to compressing (or uncompressing/"smearing out") in nontrivial cases here, though it won't work if there are gaps in the data.


How can I use jython setup.py install?

Question: I am using a Jython virtualenv where I can install whatever software via pip or via easy_install, but there is a software that is not registered yet and the installation mode via:
[sudo] python setup.py install

and I am trying to do the same with jython:
[sudo] jython setup.py install

So, I am getting these follow errors:
Traceback (most recent call last):
File "setup.py", line 3, in <module>
from setuptools import setup, find_packages
ImportError: No module named setuptools

I checked and installed jython ez_setup.py again.
I downloaded the yolk and didn't solved too.
My folder:
╭─hudson@hudson-pc ~/jython2.7a1/Lib/site-packages ‹› ‹master*›
╰─$ ls
easy-install.pth         setuptools.pth yolk 0.4.3-py2.7.egg README should_dsl-2.0a5-py2.7.egg setuptools-0.6c11-py2.7.egg virtualenv-1.7.2-py2.7.egg

And at the normal(real) environment, without virtualenvs, I got the same erros.
At Python I already installed this software and worked well.
If I enter at Jython Shell and try import setuptools, I got the same erros too:
>>> import setuptools
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named setuptools
>>> import sys
>>> sys.path
['', '/home/hudson/jython2.7a1/Lib', '/home/hudson/__classpath__',   '/home/hudson/__pyclasspath__']

Then, I add the site-packages to the sys (It can be a stupid attempt):
>>> sys.path.append('/home/hudson/jython2.7a1/Lib/site-packages')
>>> sys.path
['', '/home/hudson/jython2.7a1/Lib', '/home/hudson/__classpath__', '/home/hudson/__pyclasspath__', '/home/hudson/jython2.7a1/Lib/site-packages']
>>> import setuptools
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named setuptools

Why is not recognizing?
Answer: If pip "works" then you could use it to install your software. To try it, run from a directory with setup.py:
$ pip install -e .

If you have a tarball of the package:
$ pip install your_package-0.0.1.tar.gz

pip can install from a git repository, use custom urls from where to get packages, etc.
I've tested it: jython works with virtualenv, distribute (a fork of setuptools), pip. So jython can install a package if it uses setuptools in setup.py.
Comment: I runned pip install -e . and worked well. I didn't know this param. Thank you very much!
Comment: `pip install -e` runs `python setup.py develop` not `python setup.py install` so it's not equivalent of the latter.
Comment: How how does jython work with distribute or setuptools?
Comment: @Thufir I don't understand your question. What do you mean by "how"? Note: many-many versions have been released since 2012 — I don't know how whether the latest pip/setuptools/virtualenv versions are compatible with jython.
Answer: Basically you need to install the installtool first. To do so see the doc linked below.
install this: http://peak.telecommunity.com/dist/ez_setup.py
(see here:http://www.jython.org/jythonbook/en/1.0/appendixA.html#setuptools)
Comment: As you can see at my first line explanation I already did it and via easy_install or via pip works, but I need to install a software that is not registered yet.
Comment: Make sure you did `jython ez_setup.py`. What is in your `/Lib/SitePackages` directory? i don't have jython here but if i get a chance iw ill tell you were easy install should be, since the error message clearly states that `setuptools` is missing
Comment: Maybe you can obtain a listing of packages with the `yolk` package (see package index)
Comment: You can try import setuptools on your console and check that setuptools is in `sys.path`. In my basic installation setuptools lives as `setuptoolsXXX.egg` under `/Lib/SitePackages` I am not quite sure if virtualenv should copy this on creation or simply runs it from the base. A further thing you could try is to install the package from your real enviornment with the `--user` flag to see if that works
Comment: First of all, thanks for your attention. I checked and installed **jython ez_setup.py** again. I downloaded the yolk and didnt solved too. At my folder **jython/lib/SitePackages**:

**╭─hudson@hudson-pc  ~/jython2.7a1/Lib/site-packages ‹› ‹master*›
╰─$ ls
easy-install.pth             setuptools.pth              yolk 0.4.3-py2.7.egg
README                       should_dsl-2.0a5-py2.7.egg
setuptools-0.6c11-py2.7.egg  virtualenv-1.7.2-py2.7.egg**
And at the normal(real) environment I got the same erros. At Python I already installed this software and works.
Comment: what about trying `import setuptools` on a jython shell? Can you give information about the package, atleast the first three lines (error at line three)? And if you don't mind, just add your information to your initial question, that way it is much easier to read, just give me a first note in the comments so I get a notice that you updated. I hope that this can be solved before tomorrow, after that I am gone for a week. And if the import on the shell fails can you print the `sys.path`?

How can I use jython setup.py install?


Can anyone help me with this contradicting graphs?

Question: While studying SAT MATH 2 , I tried to solve the following problem but faced some difficulty. The problem goes ........
In the graph of the parametric equations  $x= t^2+t$   , $y=t^2-t$
A) $x\ge 0$
B) $x\ge -\frac{1}{4}$
The answer given in the book is as follows
https://drive.google.com/file/d/0B8VKUxBBIaO2LTJlb1lTalcxQ1k/view?usp=sharing
Here starts the problem. When I graph the given parametric equations, my graph does not extend to the left of zero. According to the graph I obtained, the answer should be $x\ge0$. Here is a pic of the graph I obtained
https://drive.google.com/file/d/0B8VKUxBBIaO2YzY0VVhrVFp6U2c/view?usp=sharing
So, I hereby kindly request someone to help me with this issue
Comment: What happens to your graph when $t=-0.1$?
Comment: https://drive.google.com/file/d/0B8VKUxBBIaO2YzY0VVhrVFp6U2c/view ...There is no graph for x<0
Comment: What do you mean? $x$ is given by the formula, $x=t^2+t$. What happens when $t=-0.1$?
Comment: -0.09 .      This is what I get when I substitute in the eq for x
Comment: So, doesn't that tell you that there **is** a graph for $x<0$? Doesn't it tell you that the point $(-0.09,0.11)$ is on the graph?
Comment: Then why doesnt it show in my graph ?
Comment: Presumably because you made some mistake when you drew your graph. Since I don't know how you drew your graph, I can't tell you why it doesn't show in your graph. But you do agree that it's there, right?
Comment: This is what I entered to get the graph ... https://drive.google.com/open?id=0B8VKUxBBIaO2aUFvMWFyMUV6eHc ... Whats the fault now?
Comment: I'm afraid you'll have to ask someone who knows something about that kind of calculator.
Answer: the command "ParametricPlot[{t^2+t,t^2-t},{t,-1,1}]" shows the parabola like in your exercise book
Comment: My graphic calculator is fx 9860 ... so I selected graph mode , changed the type of parametric and type x1 and y1 repect ..... So what should I do now ?
Comment: you must select parametric plot


asana getUserInfo error using PHP

Question: this is my code
<?php
require_once('asana.php');

$asana = new Asana(array('apiKey' => 'XXXXXXXX')); // API Key

$userinfo=$asana->getUserInfo();

if ($asana->responseCode != '200' || is_null($userinfo)) {
    echo 'Error while trying to connect to Asana, response code: ' . $asana->responseCode;
    return;
}
 $resultJson = json_decode($userinfo);

foreach ($resultJson->data as $user) {
    echo  $user->name . ' (id ' . $user->id . ')' .  PHP_EOL;
}

?>

In asana.php
public function getUserInfo($userId = null, array $opts = array()) {
        $options = http_build_query($opts);

        if (is_null($userId)) {
            $userId = 'me';
        }

        return $this->askAsana($this->userUrl . '/' . $userId . '?' . $options);
    }

And I got error like this: Notice: Trying to get property of non-object in C:\wamp\www\pngtest\index.php on line 15
What is the problem? Thanks in advance
Comment: Seems like your `$user` variable is not an object, yet you reference it like it is: `$user->name`. What does `var_dump($user)` show?
Answer: $resultJson = json_decode($userinfo);

foreach ($resultJson->data as $user) {
}

You're trying to access  $resultJson->data before you know if $resultJson is a valid object or not.
Note that json_decode returns:

NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

It is possible (likely) that $resultJson is NULL, because it was given invalid JSON data. You should echo $userinfo and determine if it is what you expect.

asana getUserInfo error using PHP


How to optimize hyper-parameters in LDA?

Question: After reading Hanna Wallach's paper Rethinking LDA: Why Priors Matter, I want to add hyper-parameter optimization to my own implementation of LDA. However, the paper doesn't given any details about how this optimization is to be done. I suppose I could dive into MALLET source code, but I want to understand this to the point that I can implement it rather than just copy code. Does anyone have any pointers to papers or tutorials that can help me?
Edit: Apparently there is a comparison of 5 different methods for optimizing the hyper-parameters in a Dirichlet-multinomial context in Wallach's PhD disertation. I found this on the topic models mailing list at Princeton, but I also missed the reference to the dissertation in the priors paper. I'm going to read through the disertation and then see if I can distill the ideas specifically for LDA in an answer to my own question, but I would be glad to accept someone else's answer if they beat me to it :)
Comment: A number of the methods Wallach describes are from Minka's paper [here](http://research.microsoft.com/en-us/um/people/minka/papers/dirichlet/minka-dirichlet.pdf). The simplicity of Eq. 65 makes it a good starting point, and Wallach's analysis suggests that it's roughly in the middle of the pack, performance- and accuracy-wise.
Answer: "Distributed algorithms for topic models" by Newman, D. and Asuncion, A. and Smyth, P. and Welling, M. gives an auxiliary variable sampling method for hyperparameters. These methods are related to sampling schemes for Hierarchical Dirichlet Process parameters. It doesn't appear that Hannah Wallach includes this method in her dissertation.
Also, "On Smoothing and Inference for Topic Models" by Teh et. al. has an interesting discussion on the role of hyperparameters in LDA.


Azure APIM Premium - Number of requests can handle

Question: We are building an Ecom app which will relay on Azure APIM, we expect 66.5 million requests per day. In a pick hours, it would be 10 million requests. Can Azure APIM Premium handle such a load? At present we have 8 scale units, is that sufficient?
Answer: According to the docs the estimated maximum throughput is 4000 rps. If you have 8 scale units the estimated maximum throughput is
32.000 rps
1.9m rpm
115.2 rph
You can have up to 12 scale units so you should be safe to handle such a load. (You can call to have > 12 scale units if needed)


Adding Zend View Helper to all views in application (in bootstrap)

Question: I am using Zend Framework 1.6 hence I'm not utilizing Zend_Application.
I have a simple, normal View Helper (extending Zend_View_Helper_Abstract). It works perfectly fine as long as I add it to the view in my action controller. But I want to be able to use it in every view in every module. I figured it should be easy to just get the global view in my bootstrap file and do this:
$view->addHelperPath(PATH_VIEW_HELPERS, 'RT_View_Helper_');

But I can't seem to be able to get the $view object in my bootstrap file. I also tried
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
if (null === $viewRenderer->view) {
    $viewRenderer->initView();
}
$view = $viewRenderer->view;
$view = new Zend_View(array('encoding'=>'UTF-8'));
$view->addHelperPath(PATH_VIEW_HELPERS, 'RT_View_Helper_');

But that doesn't do the trick either. I've tried putting it in the preDispatch() and postDispatch() of my boostrap (which is a front controller plugin).
Does anyone have any thoughts on how to do this? It seems it should be so simple, yet I haven't been able to find a solution for it in two days.
Thanks all :)
Ali
Comment: Out of curiosity why are you using ZF1.6?
Answer: If you are using Zend_Layout then something along these lines should get you the Zend_View:
$layout = Zend_Layout::getMvcInstance();
$view = $layout->getView();
// set helpers, doctype, etc...

Comment: Yes, I _am_ using Zend_Layout. I'll try this method and see if it works, thank you :)
Comment: NICE!! It worked. Thank you!

For anybody out there who needs to do this, I have a method called initView() in my Initializer class (bootstrap) that gets called in the routeStartup() phase with the following code:

    $layout = Zend_Layout::getMvcInstance();
    $view = $layout->getView();
    $view->addHelperPath(PATH_VIEW_HELPERS, 'My_View_Helper_');

Thanks again @robertbasic :)
Answer: Im not sure  understand your Problem but, in my opinion you could register the view Object in Zend_Registry
Zend_Registry::set('view', new Your_view);

Comment: Thanks man, my problem is that I can't access the view object in my bootstrap file. I'm not trying to create a new view of my own, I just want access to the view that my controller is going to be using, so that I can add the view helper to it.
Answer: You can do the following in your "setup.php / bootstrap.php or whatever file"
$view = new Zend_View();
// add Helper Path
$view->setHelperPath('/path/to/more/helpers', 'My_View_Helper');

Comment: Thanks ArneRie, but unfortunately that only sets the helper path of that particular instance of Zend_View that you just created. Unfortunately that doesn't work.


how can i increase the rotation speed

Question: i want to increase the rotation speed.How can I increase the rotation speed of the ImageView in this code ??anyone please help me.I need help with this code. Thanks in advance.
public class MainActivity extends Activity {
ImageView img_one;
Button btn_one;
Random r;
int angle;
boolean restart=false;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    img_one = (ImageView) findViewById(R.id.imageView_ring);
    r=new Random();
    btn_one = (Button) findViewById(R.id.btn_1);
    btn_one.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (restart){
                 angle=angle & 360;
                angle=r.nextInt()+360;
                RotateAnimation rotate=new RotateAnimation(angle,360,RotateAnimation.RELATIVE_TO_SELF,0.5f,
                        RotateAnimation.RELATIVE_TO_SELF,0.5f);
                rotate.setFillAfter(true);
                rotate.setDuration(50);
                rotate.setInterpolator(new AccelerateDecelerateInterpolator());
                img_one.startAnimation(rotate);
                restart=false;
            }else {
                angle=r.nextInt()+360;
                RotateAnimation rotate=new RotateAnimation(0,angle,RotateAnimation.RELATIVE_TO_SELF,0.5f,
                        RotateAnimation.RELATIVE_TO_SELF,0.5f);
                rotate.setFillAfter(true);
                rotate.setDuration(50);
                rotate.setInterpolator(new AccelerateDecelerateInterpolator());
                img_one.startAnimation(rotate);
                restart=true;
            }

        }
    });
}

}
Answer: If you want to rotate faster, you need to lower the duration of the animation. Currently you have it set to last for 50 milliseconds:
rotate.setDuration(50);

but you can set the duration to whatever you need to achieve your desired effect.
Comment: thanks for your reply but i do not get the desire speed
Comment: is there any other way to increase the speed
Comment: Have you tried using a different interpolator or not using one at all?
Comment: no i did not use at all . how can i use decelerate interpolator

how can i increase the rotation speed


Norm Euclidean-problem by proof

Question: This is my first post. I am sorry but my English is not very good.
So, I have problem:

Let $\,a,\,b \in \mathbb{R}^n$.
  Find $c \in \mathbb{R}^n$ and $r>0$, such that:
  $ \forall x \in \mathbb{R}^n : \left\lVert x-a\right\rVert = 2 \left\lVert  x-b  \right\rVert \iff \left\lVert x-c\right\rVert =r$.
Answer: Put $c:=\dfrac{4b-a}{3}$ and let $r:=\dfrac{2}{3}\|a-b\|.$
Let me explain why: you want $c$ to be such that $\|x-a\|=2\|x-b\|$ if and only if $\|x-c\|=r,$ this is equivalent to $\|x-a\|^2=4\|x-b\|^2$ if and only if $\|x-c\|^2=r^2$ or equivalently $\|a\|^2-2x\cdot a=3\|x\|^2+4\|b\|^2-8x\cdot b$ if and only if $\|x\|^2+\|c\|^2-2x\cdot c=r^2$
or equivalently $\|x\|^2-2x\cdot\left[\dfrac{1}{3}(4b-a)\right]+\dfrac{4\|b\|^2-\|a\|^2}{3}=0$ if and only if $\|x\|^2-2x\cdot c+\|c\|^2-r^2=0$ so if we compare the two expressions we conjecture that $c=\dfrac{1}{3}(4b-a)$ and $r=\sqrt{\|c\|^2+\dfrac{\|a\|^2-4\|b\|^2}{3}}=\dfrac{2}{3}\|a-b\|$ do the job (if we assume, of course, that $a\neq b;$ if they are equal then there is no such pair $(c,r)$)

Norm Euclidean-problem by proof


Combining heuristics when ranking news feed items

Question: We have a news feed, and we want to surface items to the user based on a number of criteria. Certain items will be surfaced because of factor A, another because of factor B, and yet another because of factor C. We can create individual heuristics for each factor, but we then need to combine these heuristics in such a way that it promotes the best content considering each factor while still giving a mix of content from each factor.
Our naive approach is to load the top n from each factor, take the first of each, and make those the first 3 of the feed. Then take the 2nd from each feed and make that the second 3, and so on and so forth. Ideally, we would have some algorithm for more intelligently ranking these feed items - our first thought was to simply sum the three heuristics and pull the top items using the resulting combined score, but there are no guarantees that the heuristics are evenly-scaled (or are evenly-scaled for that particular user), which could result in one factor dominating over the others in the feed. Is there some more intelligent way of ranking these news feed items (akin to what Facebook does in its pseudo-chronological news feed)?
Answer: Averaging over differently-scaled variables can be accomplished with the geometric mean. As you point out, the arithmetic mean of several variables (which is proportional to the sum) is dominated by the "large" variables. If you have one metric that scores in the range 1 to 10 and another that score from 1 to 1,000,000, the first one will be totally insignificant in calculating the total.
The geometric mean fixes this, such that a 50% increase in the first variable will result in the same change in mean as a 50% increase in the second variable, regardless of the scale of those variables. It is effectively performing averaging over the percentage change, rather than the absolute magnitude of the change. This is a simple but effective way to combine variables that are on different scales.


locking down windows 7 lab computers through group policy

Question: I'm kinda new to Windows administration, so please forgive me if I sound like I don't know what I'm talking about. We have a domain environment that has a mixture of Windows server 2003 and server 2008. I have been administering a Windows server 2003 lab server that controls our labs that have Windows 7 clients.
The problem that I have been having is that students have been installing browser toolbars (conduit seems to be the main culprit) that allow them them to install apps such as bittorrent clients within the toolbar. This also changes the search engine to conduit search. They have been using this to bypass the security policies that were put in place by the last administrator.
They also seem to be able to change the wallpaper, this was not a problem when all the clients were Windows XP.
Anyone know how to solve this problem through group policy or some other method?
Comment: What is a `"Lab Server"`? Is it a Domain Controller? Do you have a separate forest for your labs? What browser are the toolbars being installed on? Please re-read your question and pretend like you're one of us; someone with no idea what your setup is. You need to be a lot clearer with what you are asking.
Comment: First step: Everyone is a limited user.  Second step: [Software Restriction Policy](http://technet.microsoft.com/en-us/library/bb457006.aspx)
Answer: This thread might help you.
https://superuser.com/questions/9411/prevent-installation-of-ie-toolbars
In group policy there is also a bunch of things regarding how a person can interact with aspects of the desktops. You can turn off the ability to change background by forcing the one you want. If you have access to group policy that would be ideal, if not you can use the local security policy by running gpedit.msc. Local security policy is more difficult to maintain but if you get it the way you want you shouldn't have to adjust it very often.


In Apache does the .htaccess file override the httpd.conf for mod_rewrites?

Question: In my Apache httpd.conf file I have this declaration inside a VirtualHost tag.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.shanestillwell.com$ [NC]
RewriteRule ^(.*)$ http://www.shanestillwell.com/$1 [L,R=301]

And this inside my .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

The rule in my httpd.conf seems to be ignored. Are the rules in .htaccess wiping out the httpd.conf rewrite rule?
Answer: Generally your settings in .htaccess file inside a directory overrides httpd.conf file settings. You can disable this effect by editing your httpd.conf file and change the following line
  <Directory ...>
  AllowOverride all

to
    <Directory ...>
    AllowOverride none

This will prevent .htaccess file from overriding httpd.conf settings. So the settings in .htaccess file will have no effect.
Comment: "Generally your settings in .htaccess file inside a directory overrides httpd.conf file settings." - this statement is false (or at best misleading) without further information/context. The question is specifically discussing mod_rewrite. The _mod_rewrite_ directives in the `.htaccess` file will only override the _mod_rewrite_ directives in `httpd.conf` if the directives in `httpd.conf` are also in a `` container. Unfortunately, this has not actually been stated in the question. The _context_ in which these directives are used is everything.
Comment: I have `AllowOverride All`, but I think in this case it's because the .htaccess catches the request and redirects it before it hits the rules in the http.conf file. See http://stackoverflow.com/questions/3557040/in-apache-does-the-htaccess-file-override-the-httpd-conf-for-mod-rewrites-clos
Comment: "This will prevent .htaccess file from overriding httpd.conf settings. So the settings in .htaccess file will have no effect." But what if I want the rules in .htaccess to be applied too? i.e. I want to merge the two rulesets.
Answer:
In Apache does the .htaccess file override the httpd.conf for mod_rewrites?

Taken literally, the answer is "No". However, ...

In my Apache httpd.conf file I have this declaration inside a VirtualHost tag

If it really is directly inside the <VirtualHost> container, ie. in a virtualhost context, then these mod_rewrite directives should override the mod_rewrite directives in your .htaccess file - not the other way round. This behaviour cannot be changed (eg. by changing mod_rewrite inheritance with the RewriteOptions directive).
However, if these directives are inside a <Directory> container (which is inside your <VirtualHost> container), ie. in a directory context (as opposed to a virtualhost context, as suggested above), then the mod_rewrite directives in your .htaccess file will indeed completely override the mod_rewrite directives in your <Directory> container. However, this behaviour can be changed by changing the way mod_rewrite directives are inherited. (By either setting RewriteOptions InheritBefore in the .htaccess file, or RewriteOptions InheritDownBefore in the parent <Directory> container. InheritDownBefore requires Apache 2.4.8. However, there are other caveats with mod_rewrite inheritance to be aware of.)
Reference:
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriteoptions
The behaviour you are seeing (and the specific syntax of the directive used (see below) - although this might simply be a mistake if the code has never been executed?) suggests these directives are in a directory context, not a virtualhost context. So yes, the mod_rewrite directives in .htaccess will completely override the parent directives - the mod_rewrite directives in the <Directory> container are effectively ignored.

Aside:

RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

If this was used in a virtualhost context then you'd get an erroneous double slash at the start of the URL-path, because in a virtualhost context the RewriteRule pattern matches against the full URL-path (which includes a slash prefix). So the $1 backreference itself would already contain a slash prefix before including an additional slash in the substitution.
However, in a directory context, the RewriteRule pattern matches against a partial path - a filesystem path less the directory-prefix (which ends with a slash) - so the path that is matched by the RewriteRule pattern never starts with a slash in a directory context (so you need to include it in the substitution - like you have done).
Answer: if you have a <Directory ...> with AllowOverride all then whatever you have on your .htaccess file will be replacing any previous rule.
It could be in your httpd.conf or on your virtualhost as long as it point to / or to your domain path.


How to get tomorrow date in mql5?

Question: I want to draw a horizontal trendline that extends from today to tomorrow, And I have trouble getting time tomorrow.
for example today is 2021.12.17 00:00:00
Comment( iTime(_Symbol, PERIOD_D1, 0) + (PERIOD_D1*60) );

// Today: 2021.12.17 00:00:00
// MQL4:  2021.12.18 00:00:00 ... it's ok.
// MQL5:  2021.12.28 09:28:00 ... it's not ok! why?

MQL4: As you can see in the code above, in mql4, through the iTime function and adding a period, the date of tomorrow can be obtained, it's return 2021.12.18 00:00:00 .
MQL5: But this code shows date 2021.12.28 09:28:00 in mql5.
In other words, PERIOD_D1 is equal to 1440 in mql4 but in mql5 is equal to 16408, Why?
Comment(PERIOD_D1);
// MQL4: 1440
// MQL5: 16408 why?!!!

Answer: Unlike MQL4, PERIOD_D1 does not relate to the amount of minutes in the period in MQL5. The simple workaround is to add (1440*60) to iTime().
Alternatively, use PeriodSeconds() as below
iTime(_Symbol, PERIOD_D1, 0) + PeriodSeconds(PERIOD_D1)

How to get tomorrow date in mql5?


SQL query that can find Typos in Arabic language

Question: I want to make a dictionary and I need to have a query that can find words with misspelling (I mean Typos / spelling error or typo), if the query could not find exact word then try to get with other spellings...
So what is Arabic: In Arabic there are some letter with  (almost) same pronunciation but different letter, people sometimes don't know which one should use. For example there are 4 kind of Z in Arabic "ز / ظ / ذ / ض" pronunciation are  different (a little) but people will forgot which one is the correct spelling. an example of one word with different using of "z" letter:

مریز / مریض / مریظ / مریذ
The correct is مریض

Here are other worlds that has more than one latter:
z: ض / ز / ذ / ظ

T: ت / ط /

S: ث / س / ص /

Gh: ق / غ

So what is your idea?
How should be the query?
I want if a user searched for "مریز" instead of showing him an error of 404 (not found)  search the database with other letter (all Z) then return result if I found anything.
Comment: I know arabic, but I don't know if you tried anything. You see, someone will give you negative, because you didn't even post any queries where one can see if you even tried.
Comment: @Amel I don't want the exact query, I want idea (I don't know Arabic well) thanks for your comment
Answer: In German, we have the same issue regarding t and tt or dt - especially in names.
One way to approach this would be to store additional normalized column containing the name / word with fixed transformation.
 tt -> t
 dt -> t
 ß  -> s
 ss -> s

So table would contain
 WORD    | NORMALIZED
 schmitt | schmit
 schmidt | schmit

At query time, apply these same transformations to the query and then compare against normalized column.
Comment: Thanks, I did not know there is same problem in German (my native language is not Arabic!), you know it is a great dictionary, I am looking for a good algorithm (and query) because there are about half million words for search!
Comment: The police ought to use a similar algorithm on automobile license plates -- is it O or 0; I or 1; Z or 2.  Even E vs F can be a problem if there is a "plate surround".
Answer: There is an algorithm called Levenshtein distance (there are others as well), which tells the edit distance between two strings.
You could derive from this, try to find the most resembling words in your dictionary compared to your input.
Later you can assign weight for substitutions based on the letter tuples you mentioned to refine your search.
In fact there is an implementation for MySQL you definitely should check out: https://www.artfulsoftware.com/infotree/qrytip.php?id=552
Most of the levenshtein+mysql questions here in SO point to this page.
Answer: A simpler solution would be using regular expressions within a like statement.
For letters that are likely to be misspelled you can keep varieties in a regular expression wildcard.
For the letters corresponding to z wildcard is "[زذظض]"
You can replace all ز، ذ، ظ، ض letters with the wildcard and then query with a like statement:
select * from searched_table where word like "%[مرى[زذظض%"

After you find all versions of the searched word, you may either show the user all of them, or you may calculate the levenshtein distance (koshinae's answer) and show the closest words.
Edit: only for the letter Z, query would be like below
set @word = 'مرىض'; -- take this text from user
set @word = replace(@word, 'ذ', 'Z');
set @word = replace(@word, 'ظ', 'Z');
set @word = replace(@word, 'ض', 'Z');
set @word = replace(@word, 'ز', 'Z');
set @word = replace(@word, 'Z', '[زظضذ]');
set @word = Concat('%',  @word,  '%');
select @word;

select * from mydb.searchTable where word like @word;

Comment: Thanks dear friend. but I want to have dynamic query.
Comment: You are welcome, and you can build a dynamic query with above query using prepare-execute statements


Mouse handling on PixelToaster lib

Question: I'm using PixelToaster (a C++ library to draw to a framebuffer) for a simple 3D (ehm.. 2.5D) raycasting engine. I use the old school WASD key configuration to move the camera around (W=forward, S=backward, A=turn left, D=turn right) however I want to use the mouse for the modern freelock approach (WASD for moving and strafing, the mouse to turn head around).
I noticed that PixelToaster gives a mouselistener in which only the ABSOLUTE mouse coordinate x,y are given (relative to the window width,height). Using such coordinate system is not what I want because the turning stops as soon as the mouse x coordinate reaches the margin of the screen. (in all the commercial games you can turn around endlessly by swiping continuously the mouse in one direction).
How can I get the same behaviour using the PixelToaster mouse listener?
Answer: What you need is fairly simple to implement.
Simply reset the mouse pointer to the centre of the screen when you hit the margins.
Note that for this to work you should not "map" your 2D screen space coordinates to the angular rotation of your character. Instead have an accumulator that keeps adding up incremental changes in angle (calculated from 2D screen space movements of the mouse pointer).
Alternate Approach: You could detect when you are at the edges of your screen and keep rotating the character until you leave that edge. This way you don't need to reset the mouse pointer. For some reason I assumed that you wouldn't have a cross-hair in your game so I suggested my former approach first. :)
Comment: I tried both approaches: the first works very well however the turning movement "hiccups" as the pointer is reset and is not as smooth as seen on real games. The second one works also but it's not what a user expect (i.e. to keep turning even with a still mouse).
Comment: That is because most games do not linearly rotate the camera as the mouse moves. The angular acceleration of the camera smoothly increases as the deltas in mouse movements get bigger. Once the mouse deltas stop, the angular acceleration smoothly drops to negative and brings the camera to a halt. The angular speed of the camera is capped to avoid large values. If you get around to implementing this, play around with the values a little to get what feels "right".
Comment: Regarding the "hiccup" that you refer to. Make sure that when you reset the mouse pointer you do not accumulate that delta ( because it is bound to be big - from the end of the screen till the middle!!  :) )

Mouse handling on PixelToaster lib


How to limit the products available to choose from, in the inline entity form of a product display

Question: Drupal Commerce, when adding/editing a product display, allows for the creation of new products and the selection of existing products, using the inline entity form.
I have created a marketplace website in which multiple sellers can create their products and product displays.
Question: how to limit the available products to those created by the logged in user? When a seller creates or edits a products, he should be able to only select his own products in he 'add existing product' field.
I would have expected that the field would allow for the selection of an entityreference view, but that seems not implemented in the module.
Alternatives to accomplish the same are welcome! Thanks
Answer: When you are adding an existing product to a product display there is an autocomplete field that you can use to search existing products.
This field is called entity_id. If you use hook_form_alter() and inspect the form you'll see this field has a property called #autocomplete_path.
e.g.
$form['product_display_variations'][LANGUAGE_NONE]['form']['entity_id']['#autocomplete_path'] = 'inline_entity_form/autocomplete/node/product_display_variations/product_type';

You can visit that URL and see it just returns a set of json formatted results.
You can create your own path with hook_menu() and then use your path instead of the default one (make this change in hook_form_alter).
In your custom menu callback you can apply any additional logic (e.g. filtering by uid) to just return a sub-set of products. Then use drupal_json_output() to return your dataset.


What are good techniques and resources for teaching Bayes theorem?

Question: My friend and I want to do a hands on tutorial on Bayes theorem for the Seattle LessWrong group. Neither of us have done this before, so we're searching for prior art; techniques that other people have tried before and descriptions of how they turned out.
What are good techniques and resources for teaching bayes theorem? Reports of both successes and failures are useful, I'd like to know what not to do in addition to what to do.
The audience is a group of ~8 programmers and natural science students. They'll be smart and capable but not necessarily used to doing much math.
Comment: I think it's important to establish what Bayes theorem *means* both intuitively and mathematically. For the latter, a good presentation using Venn diagrams makes clear the relationship between the conditional probabilities and the probability of the intersection. From there the conclusion of the theorem using basic algebra will seem "obvious".
Comment: When I was student, I read in a book that Bayes' formula is a formula to "go back in time". This was really enlightening.
Comment: Here is a blog I wrote up on explaining Bayesian inference. This comes up often.
http://bayesianthink.blogspot.com
The link summarizes the various approaches to understanding Bayesian inference through puzzles ranging from the simple to fairly complex ones.
Answer: I have to recommend the book "Doing Bayesian Data Analysis" by John Kruschke (Indiana). Having sampled a few "introductory" texts over the last while, this one really shines.
There are many really well explained points but I suppose the best lever he uses to introduce the notion of combining prior and evidence is to introduce Bayes in the context of a multi-way table, where the data cause you to restrict your attention to one row, and sum over marginals to get a posterior for the cell. It is then easily expansible to continuous variables and thence to multi-way distributions.
Might be worth your while looking at it.
Answer: The LessWrong website actually has a great visual explanation of Bayes' Theorem: Bayes' Theorem Illustrated (My Way).
Answer: For the basic Bayes Formula one common example to use is disease screening.  Assume that you have a test for a disease that if used on someone who has the disease will show positive with 95% probability and if used with someone without the disease will show negative with 90% probability; further we know that 1 in 1,000 in the population have the disease.  We randomly choose a person from the population (don't know ahead of time if they have the disease) and do the test which turns out positive: what is the probability that they have the disease?  This example is often eye-opening to a lot of people.  One way to demonstrate this (and quickly show the effect of changes) is using the SensSpec.demo function in the TeachingDemos function for R (also see tkexamp in the same package for a GUI interface to this in the examples).
If you want to expand to Bayesian statistics then one fun approach is to start by showing the students a simple success/fail game like throwing a dart at a target, tossing a wadded up piece of paper into a basket, etc., and choosing a student that will play the game.  Ask the students how many times out of 4 they predict the student will succeed, and use their prediction as parameters for a Beta distribution as the prior distribution (plot this to show where they think the true probability could be).  Now have the student do the game 10 times and count the successes, use this as the data for a binomial likelihood, and combine with the prior to get a posterior distribution for the student's proportion of successes.  Show how you moved from a prior to a posterior using data and fairly simple calculations.  If you have time you can let the student play the game more times and use the first posterior as a new prior, then get an updated posterior, and show how the distribution changes with additional information.
Comment: Another version of Greg's answer: if 99% of all email on the Internet
is spam, and we block 99% of all spam, what percentage of your email
will be spam (answer: 50%). Used this in a RL presentation once, and
the managers were shocked, and stopped complaining for almost 2 whole
days!
Comment: (+1) The "canonical" example you describe in the first paragraph was the one I was going to suggest as well. It is both informative and, sometimes, at least mildly surprising to an uninitiated audience.
Answer: Use a deck of cards.
What are the chances this card is a spade?  What are the chances this card is a spade if I know the card is black?
What are the chances this card is a king?  What are the chances this card is a king if I know it is a diamond?  What are the chances it is a king if I know it is a face card?
Show them how it's used in everyday life.  What are the chances it will take me less than 30 minutes to get to work.. What if I leave at 8am?  What are the chances I will wait in line at the check out counter?  What if it is 6pm?  What are the chances this person committed the murder?  what if we know he has the same blood type as the murderer.


C++: using namespace directive for non-existing namespace

Question: I am tryting to compile following code with g++ (version 7.5.0)
using namespace nspace;
int main()
{
        return 0;
}

It gives error as follow
$ g++ above_code.cpp
namespaces_mystery1.cpp:1:17: error: 'nspace' is not a namespace-name
 using namespace nspace;
                 ^~~~~~
namespaces_mystery1.cpp:1:23: error: expected namespace-name before ';' token
 using namespace nspace;
                       ^

Above behaviour is what I have expected.
But when I try to compile following code, it compiles fine without error like above.
using namespace std;
int main()
{
        return 0;
}

Why this different behaviour for namespace named std compared to namespace named nspace
Comment: Say hello to a GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=29556 (14 year old bug, no less).
Comment: @StoryTeller-UnslanderMonica:  Do you have an answer for this up your sleeve? I don't like the "duplicate"; am happy to reopen. I *think* a compiler is allowed to effectively hardcode the statement "namespace std{};"
Comment: @Bathsheba - The duplicate (chain) is sound.
Answer: The namespace nspace doesn't exist at the point using namespace nspace; is encountered, whereas the std namespace does. The latter could be true due to implicit or explicit inclusion of facets of the C++ standard library, or the compiler itself might even hardcode it.
If you had written
namespace nspace{}

before the using statement, then compilation would succeed.
Comment: I'm pretty certain there is no inclusion going on
Comment: Thanks for the prompt response. But can you please share more light on " implicit or explicit inclusion of facets of the C++ standard library". Because I expected error in first case. But what puzzles me is the second case.
Comment: The compiler probably has some implicit `std::` namespace identifiers "baked" into the compiler itself.  Probably for intrinsics, such as `std::abs` mapping to an intrinsic assembly instruction rather than being a function call or even an inline function call.


Creating webform submissions programatically in order to use with SimpleTest

Question: I'm trying to build out a SimpleTest to test out functionality on submitted webforms. I need to simulate loading a drupal webform node, applying fake data, and submitting the form (triggering a database save).
Currently, I'm forcing data in (on a vagrant box) a drupal instance, but I have a feeling there is a better way to do this.
Question:
In Simpletest, how do I properly load a webform, inject fake data into the form fields, and submit the form?
First attemtp:
Original fixture code. It works, but it's sloppy!
Class myTest extends DrupalWebTestCase {
.
.
.
  /*
  Drops ALL DATA in the tables being tested
  */
  function _dropData() {
     db_query('delete from {webform_submitted_data}');
     db_query('delete from {webform_submissions}');
  }

  /*
  Fixtures, test data. Clears both webform_submitted_data and webform_submissions
  and then adds a new set of mock data generated initially by a browser plugin
  that autofills forms. This allows for a standardized set of data used on the
  simple test form (import script included in the package).
  */
  function _populateTablesWithTestData() {
    $q1 = "SET SQL_MODE = \"NO_AUTO_VALUE_ON_ZERO\"; INSERT INTO `webform_submitted_data` (`nid`, `sid`, `cid`, `no`, `data`) VALUES (1, 59, 2, '0', 'Dignissimos enim dolorem quia et minim velit eiusmod eiusmod voluptas nemo nostrum cum aliquam unde mollit obcaecati'), (1, 59, 3, '0', 'firstname.lastname@example.com'), (1, 59, 4, '0', '2013-03-11'), (1, 60, 2, '0', 'Sapiente velit voluptatem et quod et proident voluptatem Sunt minima reprehenderit reprehenderit'),(1, 60, 3, '0', 'firstname.lastname@example.com'),(1, 60, 4, '0', '2014-05-16'),(1, 61, 2, '0', 'Id nisi pariatur Quidem in nihil culpa qui rerum minim nihil ea minim ipsa fugiat'),(1, 61, 3, '0', 'firstname.lastname@example.com'),(1, 61, 4, '0', '2015-05-02'),(1, 62, 2, '0', 'Omnis quisquam nisi non et est est eiusmod qui qui odio ut dignissimos'),(1, 62, 3, '0', 'email@example.com'),(1, 62, 4, '0', '2015-03-02');";
    $q2 = "SET SQL_MODE = \"NO_AUTO_VALUE_ON_ZERO\"; INSERT INTO `webform_submissions` (`sid`, `nid`, `serial`, `uid`, `is_draft`, `submitted`, `remote_addr`) VALUES (59, 1, 59, 0, 0, 1424275998, '192.168.33.1'), (60, 1, 60, 0, 0, 1424276006, '192.168.33.1'), (61, 1, 61, 0, 0, 1424276013, '192.168.33.1'), (62, 1, 62, 0, 0, 1424276031, '192.168.33.1');";

    $this->_dropData();
    db_query($q1);
    db_query($q2);
  }

Second Attempt:
In a test script to be run from drush...
$form_state = array();
$form_state['values']['test2']='Something';
$form_state['values']['test_email']='firstname.lastname@example.com';
$form_state['values']['test_date']="";

drupal_form_submit('webform_client_form_1', $form_state);

However, this gives me:
$ drush scr test.php

Missing argument 3 for webform_client_form() webform.module:2268                                                               [warning]
Invalid argument supplied for foreach() webform.module:2843

Third Attempt
$form_state = array();
$form_state['values']['test2']='Something';
$form_state['values']['test_email']='firstname.lastname@example.com';
$form_state['values']['test_date']="";
$form_state['op'] = t('Submit');

drupal_form_submit('webform_client_form_1', $form_state, node_load(1), array());
print_r(form_get_errors());

Which gets:
$drush scr test.php

end() expects parameter 1 to be array, null given webform.module:3120                                                          [warning]
end() expects parameter 1 to be array, null given webform.module:3123

What the form looks like
Export Code:
$webform = array (
  'nid' => '1',
  'next_serial' => '63',
  'confirmation' => '',
  'confirmation_format' => 'filtered_html',
  'redirect_url' => '<confirmation>',
  'status' => '1',
  'block' => '0',
  'allow_draft' => '0',
  'auto_save' => '0',
  'submit_notice' => '1',
  'submit_text' => '',
  'submit_limit' => '-1',
  'submit_interval' => '-1',
  'total_submit_limit' => '-1',
  'total_submit_interval' => '-1',
  'progressbar_bar' => '1',
  'progressbar_page_number' => '0',
  'progressbar_percent' => '0',
  'progressbar_pagebreak_labels' => '1',
  'progressbar_include_confirmation' => '1',
  'progressbar_label_first' => 'Start',
  'progressbar_label_confirmation' => 'Complete',
  'preview' => '0',
  'preview_next_button_label' => '',
  'preview_prev_button_label' => '',
  'preview_title' => '',
  'preview_message' => '',
  'preview_message_format' => 'filtered_html',
  'preview_excluded_components' =>
  array (
  ),
  'record_exists' => true,
  'roles' =>
  array (
    0 => '1',
    1 => '2',
  ),
  'emails' =>
  array (
  ),
  'components' =>
  array (
    2 =>
    array (
      'nid' => 1,
      'cid' => '2',
      'pid' => '0',
      'form_key' => 'test2',
      'name' => 'TEST Text ',
      'type' => 'textfield',
      'value' => '',
      'extra' =>
      array (
        'title_display' => 'before',
        'private' => 0,
        'wrapper_classes' => '',
        'css_classes' => '',
        'width' => '',
        'maxlength' => '',
        'field_prefix' => '',
        'field_suffix' => '',
        'disabled' => 0,
        'unique' => 0,
        'description' => '',
        'placeholder' => '',
        'attributes' =>
        array (
        ),
        'analysis' => false,
      ),
      'required' => '0',
      'weight' => '1',
      'page_num' => 1,
    ),
    3 =>
    array (
      'nid' => 1,
      'cid' => '3',
      'pid' => '0',
      'form_key' => 'test_email',
      'name' => 'TEst Email',
      'type' => 'email',
      'value' => '',
      'extra' =>
      array (
        'title_display' => 'before',
        'private' => 0,
        'wrapper_classes' => '',
        'css_classes' => '',
        'width' => '',
        'unique' => 0,
        'disabled' => 0,
        'description' => '',
        'placeholder' => '',
        'attributes' =>
        array (
        ),
        'analysis' => false,
      ),
      'required' => '0',
      'weight' => '2',
      'page_num' => 1,
    ),
    4 =>
    array (
      'nid' => 1,
      'cid' => '4',
      'pid' => '0',
      'form_key' => 'date',
      'name' => 'Date',
      'type' => 'date',
      'value' => '',
      'extra' =>
      array (
        'title_display' => 'before',
        'private' => 0,
        'wrapper_classes' => '',
        'timezone' => 'user',
        'start_date' => '-2 years',
        'end_date' => '+2 years',
        'year_textfield' => 0,
        'datepicker' => 1,
        'description' => '',
        'analysis' => false,
      ),
      'required' => '0',
      'weight' => '4',
      'page_num' => 1,
    ),
  ),
  'conditionals' =>
  array (
  ),
);

Screenshot:
Comment: You can find a good installation guide here : http://behat-drupal-extension.readthedocs.org/en/latest/globalinstall.html Some basic explanation here: http://kevinquillen.com/bdd/2014/06/08/your-first-behat-test/ And a nice project to check out here: https://github.com/jhedstrom/drupalextension
Comment: I know that you're question is related to SimpleTest. You could also give behat testing a shot. You can fill in forms and submitted them following the steps(clicking etc) and actual user would do.
Comment: https://api.drupal.org/api/drupal/modules%21simpletest%21drupal_web_test_case.php/function/DrupalWebTestCase%3A%3AdrupalPost/7
Comment: Notice that webform module itself contains tests for form submission. You can use them as an example.
Comment: @PotneySwitters, I'm unfamiliar with that option? Can you throw a good link up?
Comment: Thanks @PotneySwitters, while @web-spider answer below got it working in the context of `drush scr`, porting into SimpleTest has been a nightmare. I will definitely check this out. In fact a local camp has a session on this I believe soon.
Comment: For building automated tests you should used [SeleniumHQ](http://www.seleniumhq.org/).
Comment: For front end, yes. However, I need to test specific actions *inside the database*. This is also an exercise in learning how to properly set up a SimpleTest in Drupal for module development.
Comment: Have you hear about the Coder module ?
Comment: I've used Coder, which is great at syntax checking and best practices in coding convention. My real hope is to learn how to create webform content in order to use in server side automated testing.
Answer: You can programatically submit a Drupal form with drupal_form_submit() by passing it a $form_state array with its values sub-array pre-populated with the data you want to submit. You'll need the form id of the form you're submitting, too, which in the case of a Webform is webform_client_form_<NODEID> (of course, replace <NODEID> with the node ID of the webform you want to submit).
Comment: I see you got past that, but have another error. But think you're getting close (disclaimer: I've never done this, I just threw my answer out here since I've used drupal_form_submit for non webform-related purposes and figured it would work here). Try nesting your submitted values in another sub-array, `$form_state['values']['submitted']`. That's the way Webform handles its fields, so maybe your submission isn't passing validation. FWIW, somebody else has gotten this to work: http://drupal.stackexchange.com/questions/76865/how-do-i-programatically-submit-a-webform-via-drupal-form-submit.
Comment: I've added the results of doing this in the question above. Basically, I get "`Missing argument 3 for webform_client_form() webform.module: 2268                                                           [warning] Invalid argument supplied for foreach() webform.module:2843`"
Comment: Thanks. Although another answer provided a good course,  I upvoted your answer for not only pointing me in a good direction but staying in the conversation. Many, many thanks. Upvote.
Comment: You're welcome, and good to know you got a working solution. Cheers!
Answer: To programmatically submit a webform, you can use the following code.
global $user;

$node = node_load($nid);
$data = array(
  1 => array( $component1_value),
  2 => array($component2_value),
);

$submission = (object) array(
  'nid' => $node->nid,
  'uid' => $user->uid,
  'submitted' => REQUEST_TIME,
  'remote_addr' => ip_address(),
  'is_draft' => FALSE,
  'data' => $data,
  'serial' => _webform_service_submission_serial_next_value($node->nid)
);

module_load_include('inc', 'webform', 'includes/webform.submissions');
webform_submission_insert($node, $submission);
webform_submission_send_mail($node, $submission);

function _webform_service_submission_serial_next_value($nid, $increment = TRUE) {
  if ($increment) {
    db_transaction();
  }
  $next_serial = db_select('webform', 'w')
    ->forUpdate()
    ->fields('w', array('next_serial'))
    ->condition('nid', $nid)
    ->execute()
    ->fetchField();

  if ($next_serial && $increment) {
    $increment_amount = 1;
    db_update('webform')->fields(array('next_serial' => $next_serial + $increment_amount))
      ->condition('nid', $nid)
      ->execute();
  }
  return $next_serial;
}

Comment: WOW. This was basically a copy and paste operation. I cannot thank you enough for this. I threw this into a test file, ran by `drush scr test.php`, and bam, the webform results tab has another entry. Bounty rewarded.
Comment: I've gotten this to work inside of stright PHP via `drush scr test.php` but the SimpleTest itself is spitting back errors. http://drupal.stackexchange.com/questions/150360


tls mutual authentication between zuul and microservice with certificates

Question: I have a zuul proxy acting as an edge server and my microservices are non jvm applications and I dont use ribbon or eureka. These microservices are accessible over https only and require client certs to communicate with them. Does zuul support mutual auth with certs for downstream? If so how do i set it up on my zuul server.
Answer: If you're using Spring Cloud Zuul, you can provide your own Http Client by define CloseableHttpClient bean like below.  (supported from Edgware release)
@Bean
public CloseableHttpClient httpClient() throws Throwable {
    return HttpClients.custom()
           .......ssl context or sslsocketfactory settging.
           .build();
}

If you provides this type of bean, Zuul will use this bean when making http request. Therefore you can define your custom SSL context to support client certificate. You can find many examples to support client certificate in apache http client like this or this.
Comment: @Ubunfu you can use the flag `-Djavax.net.debug=all` when you run the jar or there is an equivalent system property that you can set in your code
Comment: Thanks @youngsung. I got this partially working. The new httpclient works if the microservice is over https but the client cert is still failing. From the debug logs I see an error "Warning: no suitable certificate found - continuing without client authentication" and hence i think client certificate is not send I tried this [link](https://stackoverflow.com/questions/9299133/why-doesnt-java-send-the-client-certificate-during-ssl-handshake) and i can confirm that my pkcs12 keystore has the complete chain. Any suggestions?
Comment: so i ran some more tests and i see that a stand alone httpclient works but the same code within zuul fails.
`*** ServerHelloDone
[read] MD5 and SHA1 hashes:  len = 4
0000: 0E 00 00 00                                        ....
Warning: no suitable certificate found - continuing without client authentication
*** Certificate chain

***
*** ECDHClientKeyExchange`
Comment: It turns i had the older version of spring boot, upgrading fixed the issue.
Comment: @yongsung.yoon you saved my life!
Comment: @Abhishek how did you enable the debug logging that you mention above, where it says "Warning: no suitable certificate found - continuing without client authentication"? I'm experiencing what I believe is the same problem (through packet captures and server-side logs), but I'd like to try to reproduce that same error message.


Assign text from select list to a Hiddenfor field using Jquery

Question: I'm trying to assign the text (not the value) from a MVC Html.DropDownListFor field to a HiddenFor field.
Right now I have the following code:
<code> @Html.Label("Departure Route:", new { @class = "label-travel" })
 @Html.DropDownListFor(m => m.DepartureRoute, routesSelectList, new { @class = "dropdown", @id = "Outbound-route" })
 @Html.HiddenFor(m => m.DepartureRouteName, new {@id = "Outbound-route-name"})
</code>
I have tried to use Javascript / Jquery to take the selected field text in the dropdownlist and assign the text value to the model field (Hiddenfor)
What I have tried so far:
<code> var InitializeRouteNames = function () {
    $("DepartureRouteName").html = $("#Outbound-route option:selected").
};
</code>
Hope someone can see what I have done wrong.
Comment: Your hidden input has `id="Outbound-route-name"` so its `$(`#Outbound-route-name`).val($("#Outbound-route option:selected").text());`
Comment: Also, if you want to select by id, you need to prefix it with `#` or if you're trying to select by name, then you need the attribute selector syntax: `[name=...]`. When you just select a name without any additional syntax, it's an *element* selector, and there's obviously no `DepartureRouteName` element in HTML.
Comment: Thanks Stephen.. it worked.
Answer: Stephan showed me how to do this in the right way. I forgot to use the val() method in order to assign the HiddenFor value.
<code>var InitializeRouteNames = function () {
    $("#Outbound-route-name").val($("#Outbound-route option:selected").text());
    $("#Return-route-name").val($("#Return-route option:selected").text());
};
</code>


Weblogic Deployment Error

Question: When i deploy my working application on Weblogic, i get a Stack Over Flow error:
<code>###<Sep 5, 2011 3:56:43 AM EDT> <Error> <Console> <qdcls0757> <CorpDEVAdmin> <[ACTIVE] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)'> <wlsadm9> <> <> <1315209403700> <BEA-240003> <Console encountered the following error java.lang.Exception: java.lang.StackOverflowError
    at weblogic.deploy.service.internal.adminserver.AdminRequestStatus.includeDeploymentFailure(AdminRequestStatus.java:1100)   at weblogic.deploy.service.internal.adminserver.AdminRequestStatus.signalCancelSucceeded(AdminRequestStatus.java:1042)
    at weblogic.deploy.service.internal.statemachines.adminserver.AwaitingCancelResponses.signalCancelCompletion(AwaitingCancelResponses.java:130)
    at weblogic.deploy.service.internal.statemachines.adminserver.AwaitingCancelResponses.doCancelCompletionCheck(AwaitingCancelResponses.java:112)
    at weblogic.deploy.service.internal.statemachines.adminserver.AwaitingCancelResponses.receivedCancelSucceeded(AwaitingCancelResponses.java:84)
    at weblogic.deploy.service.internal.adminserver.AdminRequestImpl.receivedCancelSucceeded(AdminRequestImpl.java:452)
    at weblogic.deploy.service.internal.transport.CommonMessageReceiver.receiveCancelSucceededMsg(CommonMessageReceiver.java:263)
    at weblogic.deploy.service.internal.transport.CommonMessageReceiver$3.run(CommonMessageReceiver.java:753)
    at weblogic.work.ServerWorkManagerImpl$WorkAdapterImpl.run(ServerWorkManagerImpl.java:518)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:181)
Caused by: java.lang.StackOverflowError:
    at sun.nio.cs.UTF_8$Decoder.decodeLoop(UTF_8.java:416)
    at java.nio.charset.CharsetDecoder.decode(CharsetDecoder.java:544)
    at java.lang.StringCoding$CharsetSD.decode(StringCoding.java:190)
    at java.lang.StringCoding.decode(StringCoding.java:228)
    at java.lang.String.<init>(String.java:405)
    at java.lang.String.<init>(String.java:433)
    at java.io.FileOutputStream.writeBytes(Native Method)
    at java.io.FileOutputStream.write(FileOutputStream.java:260)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
    at weblogic.logging.RotatingFileOutputStream.flush(RotatingFileOutputStream.java:88)`
</code>
Can anybody help me on this
Comment: the stacktrace also shows a Cancel Deployment - did you fire that or did it happen as part of the deployment?
Answer: try changing weblogic logging log level to FATAL from log4j.xml file and then start the server again.
Comment: Y to do this so.. can you please explain the source of this issue... @Ankit

Weblogic Deployment Error


How do I make the output of field X look like Y?

Question: I have a field of type X, and it's output is not quite the way I want to it to be. I would like it to look like Y instead. How can I do that?
Answer: I usually solve this type of problem by implementing a custom field formatter, which is what controls how a field is rendered.
Below is sample code to implement a basic single delta field formatter. It is used on normal "text" fields, and will simple output todays date, with a single setting for in what format to output the date.
Since this particular example does not actually use the field data, it's important to know that you need the field to have some value, or Drupal will ignore it.
<code>// Declare a new field formatter.
function hook_field_formatter_info() {
  return array(
    'custom_current_date' => array(
      'label' => t('Current date'),
      'field types' => array('text'),
      'settings' => array('date_configuration' => 'm'),
    ),
  );
}

// Form API callback for the formatters settings form
function hook_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];

  $element = array(
    'date_configuration' => array(
      '#type' => 'textfield',
      '#title' => t('Date configuration'),
      '#default_value' => $settings['date_configuration']
    ),
  );

  return $element;
}

// Output a message showing what the current field settings are.
function hook_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];

  return 'Date config is ' . $settings['date_configuration'];
}

// Actual field output goes under the #markup key. Add a loop instead of the
// leading 0 to support multi-value fields.
function hook_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $settings = $display['settings'];

  return array(0 => array('#markup' => date($settings['date_configuration'])));
}
</code>


Log-transforming a mean - what to do with SD?

Question: I am doing a meta-analysis of means. I have gathered means, SD and N from a set of studies. I do not have access to the raw data of the means. After doing an Anderson-Darling test, I found that the means are not normally distributed. Thus, I log-transformed the means. But what do I do with the SD? I am using the escalc and rma functions from the package metafor in R. If I use log(mean) and log(SD) in escalc, this provides negative values of vi, which does not work in rma. Is there another way to do this? Or can I leave the SD untransformed, and just use log(mean)? The plot looks a bit strange though, as the variation of each study becomes very large.
Comment: I would suggest using the raw values you have and then looking at the diagnostics from the model using fit.rma() from the package you are using. This would give you a handle on how much the residuals deviate from normality and whether that is likely to matter.
Comment: A more informative plot can be obtained with qqnorm()
Comment: You should add the `qqnorm` plot, but that histogram doesn't look terribly non-normal, it has some skew, but do not have heavy tails.
Comment: Unfortunately the log of a mean is not much use even if logarithmic scales make sense, and the log of an SD even less use. But negative logarithms just mean original values < 1 and it's hard to see that as invalid in general.
Comment: I agree with others, I think, and see no reason not to use those results as they come. Sure, the A-D test says that they are not exactly normal, but that wouldn't worry me.
Comment: If you are still not happy and want to ask if there is an R solution relaxing the assumption of normality then ask on the mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-meta-analysis// You need to register first. Software recommendations are on-topic there, but not here.
Comment: I doubt there is a good idea to log-transform the means, if not for other reason, tere is no good way to transform the corresponding sd. So better, search for a test that does not depend on normality. Can you show us a plot of the means?  If you told us what variable this is mean of, that might help.
Comment: I am doing a meta-analysis of the basal metabolic rate (BMR) of bats. I have mean BMR + SD + N from each study.

Histogram of means added in original post. I did an Anderson-Darling test and found that the means were not normally distributed. In the handbook I'm using, it says that log-transforming the means is one option if the means are not normally distributed, but they do not mention what to do with the weighting of the means (i.e. the variation). The AD test for the log-transformed data is not significant (p=0.558), so they are normally distributed on log scale.
Comment: Thank you for helping! The qqnorm plot is added as well
Comment: The Anderson-Darling test of the raw values gives a p-value of 0.017.
Answer: The standard meta-analytic models do NOT assume that the marginal distribution of the observed outcomes is normal.
Take, the random-effects model, which is given by $$y_i = \mu + u_i + e_i,$$ where $y_i$ is the observed outcome in the $i$th study (in your case, a mean), $u_i \sim N(0, \tau^2)$ is a random effect for heterogeneity, and $e_i \sim N(0, v_i)$ is the sampling error in the $i$th study with (approximately) known sampling variance $v_i$ (for means, the sampling variance is $v_i = \mbox{SD}^2_i / n_i$, where $\mbox{SD}_i$ is the observed standard deviation and $n_i$ the sample size).
The marginal distribution of the observed $y_i$ values (which is what you are examining) is a mixture distribution of normals with different variances and hence is not normal. Therefore, examining the distribution of the observed outcomes is not a useful diagnostic tool.
Instead, one should consider/examine the two normality assumptions underlying this model separately.
The first pertains to the sampling distributions. Based on the central limit theorem, we know that the sampling distribution of a mean will tend towards normality with increasing sample size. Hence, assuming $n_i$ is not too small in any of the studies, I wouldn't worry too much about this assumption. You cannot really say much about this assumption anyway without access to the raw data (e.g., if the raw data come from a normal distribution, then the sampling distribution of the mean will be normal no matter what the sample size is, but convergence to a normal sampling distribution could be slow (or even not happen at all) for other distributions underlying the raw data).
The second normality assumptions pertains to the $u_i$ values. One cannot observe these values directly, but we can obtain BLUPs (best linear unbiased predictions) of these values. Since you mentioned using the metafor package, you can use the ranef() function on the fitted model object to obtain those BLUPs. But even examining the distribution of the BLUPs is problematic, because they have heteroscedastic standard errors as well. One could divide the BLUPs by their standard errors and examine the distribution of those (approximate) z-values, but in my experience that only works so-so.
Finally, one can also examine the distribution of the standardized residuals (rstandard()) or studentized residuals (rstudent()). This doesn't differentiate between the two issues above, but can work as a rough check of the assumptions underlying this model in general.


Erro ao formatar o Json

Question: Preciso gerar e expor um Json via web api e a dúvida é o seguinte:
1o. - Tem como não expor o atributo "services":null, conforme o JSon gerado por mim descrito abaixo ?
2o. - Como faço para gerar no inicio do Json: "services": [ conforme o Json Solicitado ?
Modelos:
public class ProductType
{
    public ProductType()
    {
        services = new Services();
    }
    public string productType { get; set; }
    public Services services { get; set; }
}

public class Services
{
    public string subtype { get; set; }
    public string services { get; set; }

}

Como deve ser o JSON:
{
    "services": [
        {
            "productType": "Produto A",
                "services": [
                    {
                        "subtype": "Sub Produto A1"
                    }
                ]
        },
        {
            "productType": "Produto B",
                "services": [
                    {
                        "subtype": "Sub Produto A2"
                    }
                ]
        },
        {
            "productType": "Produto C",
                "services": [
                    {
                        "subtype": "Sub Produto A3"
                    }
                ]
        }
    ]
}

Como estou gerando o JSON:
[
    {
        "productType":"Produto A",
        "services":
                {
                    "subtype":"Sub Produto A",
                    "services":null
                }
    },
    {
        "productType":"Produto B",
        "services":
                {
                    "subtype":"Sub Produto B",
                    "services":null
                }
    },
    {
        "productType":"Produto C",
        "services":
                {
                    "subtype":"Sub Produto C",
                    "services":null
                }
    }
]

Answer: Opa, vamos lá.
você consegue sim ocultar, procure na API que você está utilizando qual o método ou anotação que você ignora os nulos.
exemplo, no jackson json você consegue fazer isso no mapper
mapper.setSerializationInclusion(Include.NON_NULL);
quando a aparecer o rotulo de services, você pode criar um objeto Staff de JSON que contem um objeto do tipo services. Com isso, ele passará a ser criado.

Erro ao formatar o Json


Como empacotar um projeto Node.js em um executável?

Question: Possuo um projeto Node.js porém preciso criar um executável para Windows e um para Linux, ambos contendo ainda o Node.js embutido.
Eu consegui fazer algo parecido numa aplicação node-webkit utilizando um plugin para o Grunt, porém gostaria de saber como fazer isso em uma aplicação Node.js normal.
Comment: Da uma olhada nesse link: http://stackoverflow.com/questions/8173232/make-exe-files-from-node-js-app. Aparentemente existem algumas ferramentas para esse trabalho.
Comment: Acho que a melhor opção seria criar um programa usando elétron
Answer: A solução é usar algo como AppJS. Ele cria um pacote que contem o seu aplicativo NodeJS assim como também o runtime (node.exe), as dependências, e ainda provê a possibilidade de usar uma biblioteca que permite criar GUIs usando HTML.
Comment: Acredito que o ruim de usar o AppJS é que vem com toda a GUI, caso não seja necessária uma. Aí acaba pesando bastante!
Comment: Exato, o que eu quero é criar um app de linha de comando.
Comment: Se não me engano basta não fazer uso de `app.createWindow(..)` que ele funciona igual que qualquer outro aplicativo NodeJS. Talvez seja até possível nem mesmo dar `require('appjs')` possibilitando usar apenar a parte de empacotamento do AppJS. O unico problema disso é que a biblioteca do Chromium vai junto com o aplicativo empacotado por mais que você não faça uso, o que deve aumentar o tamanho do mesmo, relativo a se não tivesse GUI.
Comment: Irei testar este modo que tu sugeriu e se der tudo certo confirmo a tua resposta. Muito Obrigado.
Answer: Atualmente parece não haver muitas opções para distribuir um binário standalone de uma aplicação Node.js.
Uma delas, e praticamente única, opção é o nexe. Faz justamente o que você quer, mas existem alguns (grandes) problemas:

Pode ser um pouco instável (seu código é bem simples).
Não suporta módulos nativos.
Não suporta Windows.

Apesar de todos os contras... vamos a um hello world:
1. Instalando o nexe:
 $ sudo npm install -g nexe

2. Criando um arquivo de teste:
$ echo "console.log('Hello World');" > hello.js

3. Compilando a aplicação:
$ nexe -i hello.js -o hello
making application bundle with /home/talles/hello.js
writing application bundle:
[a partir daqui vêm uma saída 'pesada' do compilador]

-i, de input, são os arquivos de entrada; -o, de output, o arquivo de saída. Existe também -t de diretório temporário e -r de runtime (versão do node  a ser utilizada).
4. Pronto, o arquivo final está gerado:
$ ./hello
Hello World

No exemplo o arquivo final gerado teve um tamanho de 11Mb.
Comment: Não tão novo assim, olhei o histórico de commits. Tem poucos commits espalhados em mais de 2 anos de existência do projeto.
Comment: @gustavohenke verdade...


Como atribuir dados de um objeto a editText no Android?

Question: Preciso atribuir em campos de EditText, do meu formulário, os dados de um objeto. Exemplo, estou usando uma API que busca endereço do usuário pelo viaCep. Já estou conseguindo retornar os dados, agora preciso jogar esse dados nos seus devidos campos.
public void getCEP(String cep){
  RequestParams rp = new RequestParams();

  AsyncHttpClient client = new AsyncHttpClient();
  client.get("http://viacep.com.br/ws/" + cep + "/json/", rp, new TextHttpResponseHandler() {
    @Override
    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
      Toast.makeText(getBaseContext(), "Problema na conexao!"+statusCode, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onSuccess(int statusCode, Header[] headers, String responseString) {
      try {
        JSONObject obj = new JSONObject(responseString);

        String retorno = "";

        if (!obj.has("erro")) {
          retorno += "\n" + obj.getString("cep");
          retorno += "\n" + obj.getString("logradouro");
          retorno += "\n" + obj.getString("complemento");
          retorno += "\n" + obj.getString("bairro");
          retorno += "\n" + obj.getString("localidade");
          retorno += "\n" + obj.getString("uf");
          retorno += "\n" + obj.getString("ibge");
          retorno += "\n" + obj.getString("gia");

          Toast.makeText(getBaseContext(),"Dados retornados: "+retorno, Toast.LENGTH_LONG).show();

          EditText edtCep = ((EditText) findViewById(R.id.edtCep));
          //edtCep.setText("cep", retorno.);
        }

      } catch (JSONException e){

      }
    }
  });
}

public void btnCadastrar(View view) {

  final EditText edtNome = ((EditText) findViewById(R.id.edtNome));
  final EditText edtSobrenome = ((EditText) findViewById(R.id.edtSobrenome));
  EditText edtCelular = ((EditText) findViewById(R.id.edtCelular));
  EditText edtCep = ((EditText) findViewById(R.id.edtCep));

  final String nome = edtNome.getText().toString();
  String sobrenome = edtSobrenome.getText().toString();
  String celular = edtCelular.getText().toString();
  String cep = edtCep.getText().toString();

  String url = "http://reservacomdomanda.com/areaAdmin/api/area_admin/usuario.php";

  StringRequest srUrl = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {

    }
  }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }
  })
  {
    protected Map<String, String > getParams(){
      Map<String, String> params = new HashMap<String, String>();

      params.put("nome", edtNome.getText().toString());
      params.put("sobrenome", edtSobrenome.getText().toString());

      return params;
    }
  };

}

Answer: No trecho que você deixou comentado, após declarar o EditText edtCep, basta trocar por esse comando:
edtCep.setText(obj.optString("cep"), TextView.BufferType.EDITABLE);

Comment: Certo, mas onde está o cep de dentro do objeto retorno, nesse seu comando? Tipo retorno.cep, algo assim
Comment: Usa "obj.getString("cep")" em vez de "retorno"
Comment: OK, vou tentar...
Comment: Se você não tem certeza que os dados de "obj" podem conter dados, usa optString em vez de getString, com isso o EditText vai simplesmente ficar vazio, em vez de gerar uma exceção.
Comment: Pois é, o que vc disse antes, não deu muito certo, pois dei um Lod.d... e retornou isso no console:
"D/CEP: android.support.v7.widget.AppCompatEditText{1b611dab VFED..CL ..."
Comment: Fiz isso:

edtCep.setText(obj.optString("cep"), TextView.BufferType.EDITABLE);
Log.d("CEP", String.valueOf(edtCep));

E não apareceu o cep no console
Comment: O cep pode não estar vindo no seu json. Na sua string "retorno" ele aparece?
Comment: Sim, tenho uma Toast que mostro os dados todos que vem no objeto retorno.
Aqui:
Toast.makeText(getBaseContext(),"Dados retornados: "+retorno, Toast.LENGTH_LONG).show();
Comment: Sim, eu reparei esse toast. Mas é estranho o cep aparecer nele e não no editText, visto que ambos usam o mesmo obj.getString("cep").
Comment: Ta, esquece o que eu te disse antes kkkkk É que como eu já preencho o CEP pra buscar os dados, é óbvio que ele não vai reaparecer... Fiz o mesmo com logradouro e ele apareceu.... VALEU.
Answer: Crie uma classe. Algo como:
public class Endereco {
    private String logradouro;

    public void setLogradouro(String logradouro) {
         this.logradouro = logradouro;
    }

    public String getLogradouro() {
        return logradouro;
    }
}

No retorno popule esse objeto
Endereco endereco = new Endereco();
endereco.setLogradouro(obj.getString("logradouro"));

edtCep.setText(endereco.getLogradouro());

Comment: Sim, até aí eu sei, mas e como pegar essa "alguma coisa" do objeto?
Comment: O setText para EditText precisa de um segundo parâmetro TextView.BufferType.
Comment: Criar uma classe só pra isso? É necessário?
Comment: Também acho que não.


Can the Wilcoxon rank sum test give a different result to the Kolmogorov-Smirnov test result?

Question: Let's say I have two data sets (in R, say); $x_1, x_2,..., x_n$ and $y_1, y_2,..., y_n$.
The Wilcoxon rank sum test rejects, indicating that the "X" population distribution differs from that for "Y".
Is it possible that the two sample Kolmogorov-Smirnov test would not indicate that they're different?
Or can we predict that the Wilcoxon would not cause us to reject the null if the Kolmogorov Smirnov test did not?
Comment: What do you mean by "show x population dist. same as y"? Do you mean not reject the null hypothesis that they are the same (very different from showing that this null hypothesis is true)? If so,  yes two different tests will give different p-values, so this can happen.
Comment: What do you want to do? x and y can't be the same.
Comment: I mean same distribution of population. I edited quesiton.
Comment: @Björn,"show x population dist. same as y" means `p value>0.05` then accept null hypothesis, which means same distribution.
Comment: can we predict  `wilcox.test(x,y)>0.05` if `ks.test(x,y)>0.05 `?
Comment: @WhiteGirl It's of course possible, although I can't come up with a data set immediately.
Comment: As I mentioned, p>0.05 does not mean the distributions are the same (or that we should conclude that).
Comment: I have edited the question to make it clearer. Please check it still asks what you intend.
Answer: Statistical tests measure differences between samples in different ways and are sensitive to different aspects of the distributional differences.
It's certainly possible for one test to reject and another test (perhaps designed for testing a somewhat different hypothesis) to fail to reject when applied to the same sample at the same significance level. (However, you seem to be misinterpreting what failure to reject implies -- it does not mean that the test shows the population distributions are the same. It only means that the differences in the sample weren't large enough or perhaps were not of the right kind for the test to detect whatever difference there might have been.)
In fact that's what we should hope -- that a test that's designed to pick up more specific kinds of differences should be slightly more sensitive to those kinds of differences that we're most interested in.
In the case of the Kolmogorov-Smirnov test, it's intended to pick up any kind of difference in distributions, but tends to have less power to detect differences in the tails than say Tony Pettitt's two-sample version of the Anderson-Darling (while doing slightly better at differences in the middle). The Wilcoxon-Mann-Whitney by contrast is focused on picking up differences that tend to make one variable typically "larger" than another (alternatives of the form $P(X>Y)>\frac12$ which encompasses location shift alternatives among a host of other possibilities). At the same time, it can't detect a shift in scale that's not accompanied by a difference in location (e.g. a difference in variance of two normals with the same mean). Because its statistic concentrates on a more particular kind of difference in distribution, it does better at seeing when that's the kind of difference that's present.
Here's a pair of samples where the Wilcoxon-Mann-Whitney rejects and the Komogorov-Smirnov does not:
x
 10.110 10.372 10.038 10.159  9.716 10.741 10.637  9.417  9.751 10.014
y
 11.273 11.199 10.575 11.051 10.214 10.448 12.439 10.308 10.225  9.387

Looked at pictorially, the Kolmogorov-Smirnov is looking at the biggest vertical distance between the two ecdfs (which is anywhere just to the left of the second-smallest blue point at 10.214 and to the right of the red point at 10.159); it picks up that there's been a lot of red points with only the one blue one. Meanwhile the Wilcoxon-Mann-Whitney is focused on the tendency to have a lot more red points to the left of blue points than blue points to the left of red points.
As we see here, where the Wilcoxon-Mann-Whitney attains a p-value of 4.3% while the Kolmogorov-Smirnov has a p-value of 5.2%:
> wilcox.test(x,y)

        Wilcoxon rank sum test

data:  x and y
W = 23, p-value = 0.04326
alternative hypothesis: true location shift is not equal to 0

> ks.test(x,y)

        Two-sample Kolmogorov-Smirnov test

data:  x and y
D = 0.6, p-value = 0.05245
alternative hypothesis: two-sided

In this case I made the data by generating two random (normally distributed, as it happens but this is not important) samples and then shifted one slightly up (just enough to lead the rank sum test to reject). Since it's somewhat more sensitive to that kind of difference than the Kolmogorov-Smirnov two sample test is, there was a good chance that the Kolmogorov-Smirnov test might not pick up the difference at that significance level (and that's what happened).
Comment: +1, but a discussion of the "more specific kinds of differences" each of these tests are "designed to pick up" seems appropriate here.
Comment: Side note: The proportion odds (PO) semiparametric ordinal logistic regression model [generalizes the Wilcoxon test](https://fharrell.com/post/rpo).  The partial PO model generalizes the PO model.  A partial PO model that has 2-3 parameters capturing an interaction between X and Y will give you a 3-4 degree of freedom near-equivalent of the K-S test.  This generalizes the idea of allowing variances to be unequal in a two-sample t-test but effectively allowing for differences also in skewness/kurtosis.
Answer: The crux of @Glen_b's answer (+1) is that these are two different tests that are "designed to pick up... [different and] specific kinds of differences" between the two distributions.  So to understand how the results (in terms of whether they are significant or not) can differ between the Wilcoxon rank sum test and the Kolmogorov-Smirnov tests, we need to understand what the tests are designed to detect.

The Wilcoxon rank sum test tests if:

the probability of an observation from the population X exceeding an observation from the second population Y equals the probability of an observation from Y exceeding an observation from X: P(X > Y) = P(Y > X) or P(X > Y) + 0.5 · P(X = Y) = 0.5

That is, it is testing if values of X tend to be larger or smaller than values of Y.
The Kolmogorov-Smirnov test assesses the largest1 difference between the two empirical cumulative distribution functions (ECDFs) and compares it to its sampling distribution assuming the distributions are the same.

From here, it is easy to see how there can be datasets where the tests will yield different results.

The Wilcoxon will be significant while the KS will not when one sample is consistently greater than the other, but not by a large absolute value, and where the distribution shapes are largely the same.
set.seed(9825)
g1 = rnorm(10)
g2 = g1+1.27

wilcox.test(g1, g2)
#   Wilcoxon rank sum test
#
# data:  g1 and g2
# W = 22, p-value = 0.03546
# alternative hypothesis: true location shift is not equal to 0
ks.test(g1, g2)
#   Two-sample Kolmogorov-Smirnov test
#
# data:  g1 and g2
# D = 0.5, p-value = 0.1678
# alternative hypothesis: two-sided

The KS will be significant while the rank sum test will not when the means and medians are the same but the shapes differ markedly.
set.seed(3806)
g1 = scale(rnorm(15),       center=TRUE, scale=FALSE)
g2 = scale(rnorm(15, sd=5), center=TRUE, scale=FALSE)

wilcox.test(g1, g2)
#   Wilcoxon rank sum test
#
# data:  g1 and g2
# W = 131, p-value = 0.461
# alternative hypothesis: true location shift is not equal to 0
ks.test(g1, g2)
#   Two-sample Kolmogorov-Smirnov test
#
# data:  g1 and g2
# D = 0.53333, p-value = 0.02625
# alternative hypothesis: two-sided

1. More technically the supremum.


Javascript Regex (x,y) Match multiple

Question: I am newbie to Javascript.
What regex will be used to match the following expression type. Please note that there could be spaces between digits.
(3,2), ( 2,3),(5, 4)

I am trying as (\d+,\d) but this is not working for multiple pairs of (x,y).
Answer: See the RE below: \s* means: "any whitespace". The parentheses have a special meaning, and have to be escaped.
The first RE just matches all pairs of numbers, while the second RE also group the numbers, so that they can be referred when using the RegExp.exec function.
/\(\s*\d+\s*,\s*\d+\s*\)/g

/\(\s*(\d+)\s*,\s*(\d+)\s*\)/g

Example, get all (x,y) pairs within a string, and store the pair in an array:
var source = "(1,2), (3,4) (5,6) (7,8)"; //Comma between pairs is optional
var RE = /\(\s*(\d+)\s*,\s*(\d+)\s*\)/g, pair;
var pairList = [];
while((pair = RE.exec(source)) !== null){
    pairList.push([pair[1], pair[2]]);
}
//pairList is an array which consists all x,y pairs

Comment: I am trying your answer but it is not working for atleast this site http://www.regular-expressions.info/javascriptexample.html
Comment: See the example at the bottom of my answer. @PujanSrivastava, your website doesn't accept the slashes and the flags of a RE.
Answer: this should match (x,y),(v,w) like patterns. 0 to n (x,y), expresions plus 1 (x,y) expresion (including whitespaces)
/(\(\s*\d+\s*,\s*\d+\s*\),\s*)*\(\s*\d+\s*,\s*\d+\s*\)/

Answer: Try this code:
patt = RegExp('\(\s*\d*\s*,\s*\d*\s*\),{0,1}', 'g')
patt.test("(3,2), ( 2,3),(5, 4)") => true

Answer: You can try this /(\(\d+\s*,\s*\d+\)*)/g.exec('(2,3), (3,4)') or using the string at the start  '(2,3), (3,4)'.match(/(\(\d+\s*,\s*\d+\)*)/g)

Javascript Regex (x,y) Match multiple


How is ARMA/ARIMA related to mixed effects modeling?

Question: In panel data analysis, I have used multi-level models with random/mixed effects to deal with auto-correlation issues (i.e., observations are clustered within individuals over time) with other parameters added to adjust for some specification of time and shocks of interest. ARMA/ARIMA seem designed to address similar issues.
The resources I've found online discuss either times series (ARMA/ARIMA) or mixed effect models but beyond being build on regression, I don't understand the relationship between the two.  Might one want to use ARMA/ARIMA from within a multilevel model? Is there a sense in which the two are equivalent or redundant?
Answers or pointers to resources that discuss this would be great.
Answer: I think the simplest way to look at it is to note that ARMA and similar models are designed to do different things than multi-level models, and use different data.
Time series analysis usually has long time series (possibly of hundreds or even thousands of time points) and the primary goal is to look at how a single variable changes over time. There are sophisticated methods to deal with many problems - not just autocorrelation, but seasonality and other periodic changes and so on.
Multilevel models are extensions from regression. They usually have relatively few time points (although they can have many) and the primary goal is to examine the relationship between a dependent variable and several independent variables. These models are not as good at dealing with complex relationships between a variable and time, partly because they usually have fewer time points (it's hard to look at seasonality if you don't have multiple data for each season).
Comment: Your explanation is quite good, in practice, though I would add a slight caveat. ARIMA models can be implemented as State Space models (R's `arima` does this, under the hood), also known as Dynamic Linear models (DLMs). DLMs are also extensions from regression (in a different way than Mixed Effects), so I'd guess that there is a deep-down relationship between ARIMA and Mixed-effect models. That doesn't change the differences *in practice*, which you summarize well.
Comment: :Peter Very nice summary. I would only add that time series data is not usually "long" when dealing with weekly/monthly/annual data BUT can get long when dealing with daily/hourly/second data.
Comment: This is very useful. I will point that out that adding a moving average to a multi-level model is certainly possible (and, in the simplest form, is done all the time by adding lagged variables (e.g., the dependent variable at $t-1$).
Comment: Benjamin: The whole idea of statistics is to IDENTIFY STRUCTURE not assume it.
Comment: I think a complete answer might also mention the difference between time series and panel data. If I understand correctly, ARIMA and similar are primarily used for data where each observation is of the same variable over time. In the multilevel model for change, we are usually focused on panel data and we are modeling a variable measured across a range of individuals, groups, countries, etc, over time. Right?
Answer: ARMA/ARIMA are univariate models that optimize how to use the past of a single series to predict that single series. One can augment these models with empirically identified Intervention Variables such as Pulses, Level Shifts , Seasonal Pulses and Local Time Trends BUT they are still fundamentally non-causal as no user-suggested input series are in place. The multivariate extension of these models is call XARMAX or more generally Transfer Function Models which use PDL/ADL structures on the inputs and employ any needed ARMA/ARIMA structure on the remainder. These models can also be robustified by incorporating empirically identifiable deterministic inputs. Thus both of these models can be considered Applications to longitudinal (repeated measures) data. Now the Wikipedia article on multi-level models refers to their application to time series/longitudinal data by assuming certain primitive/trivial i.e. non-analytical structures like "The simplest models assume that the effect of time is linear. Polynomial models can be specified to allow for quadratic or cubic effects of time".
One can extend the Transfer Function model to cover multiple groups thus evolving to Pooled Cross-section time series analysis where the appropriate structure (lags/leads) can be used in conjunction with ARIMA structure to form both local models and an overall model.
Comment: :Benjamin The problem with rhat is you are assuming that the seasonality is deterministic and to top it off that the seasonal coefficients are invariant over time as compared to a seasonal pulse one of the ISI-1 dummies which had no effect for the first k time periods but did so after. Another equally possible seasonal structure is the seasonal ARIMA component which uses an adaptive response to prior seasons as compared to your suggested FIXED response.
Comment: Multi-level models can also use a general specification for time which add dummies for each time that will capture the average effect for that time period.


How to extract data from a database and populate a sheet in Excel

Question: I am storing data in a backend database (PostgreSQL) which is running on a Linux machine. I want to be able to fetch data from the database, and populate a sheet in an excel workbook, so that I can carry out analysis in Excel.
It has been quite a while since I wrote anything in VBA, so I would appreciate some help (or links) in getting started. I would like to know the best way to approach this:

Pure VBA solution OR
Mixture of C# or other .Net language for data extraction logic and VBA for manipulating Excel objects (sheet data population etc)

Any ideas, tips, snippets and/or links that can help me get started on the twin objectives:

fetching data from a backend database (PostgreSQL) into Excel
populating a specified sheet in Excel with the columnar data retreived from the database

will be much appreciated.
Comment: An alternative solution is to use PowerPivot. http://www.microsoft.com/en-us/bi/powerPivot.aspx. Here is how to connect to a databas other than SQL Server: http://stackoverflow.com/questions/4170658/accessing-mysql-db-into-excel-2010-powerpivot
Answer: If you are just trying to import data, Excel can do that without additional code. Just set up your windows client to connect to your postgreSQL server thru ODBC. (Here's one way guide to setting that part up: enter link description here
Then in Excel (Use a modern version, like 2007 or greater) from the "Data" tab, click on "Existing Connections" to connect to the ODBC connection you set up and pick the tables/data to import into excel. Once the spreadsheet has loaded the data, you can just click the "Refresh All" button to update the data.