Standard error of estimated sum or product mean

Question: Updated question: Given two sample means ($\bar X, \bar Y$) and sample standard deviations ($S_X, S_Y$) with different sample sizes ($n_X, n_Y$), I want to calculate the standard errors ($SE_\theta, SE_\rho$) of estimated sum ($\theta$) and product ($\rho$) means.
The given sample means and sample standard deviations are calculated as
$\bar X = \frac{\sum\limits_{i=1}^{n_X} X_{i}}{n_X}$, $S_X = \sqrt{\frac{\sum\limits_{i=1}^{n_X} (X_{i} - \bar X)^2}{n_X - 1}}$, $\bar Y = \frac{\sum\limits_{i=1}^{n_Y} Y_{i}}{n_Y}$ and $S_Y = \sqrt{\frac{\sum\limits_{i=1}^{n_Y} (Y_{i} - \bar Y)^2}{n_Y - 1}}$
where $n_X \neq n_Y$.
To clarify, I provide two made-up examples where the variables $X$ and $Y$ are assumed to be independent:
Sum: Imagine a population of sharks spending 5 ± 0.2 h d$^{–1}$ ($\bar X \pm S_X$) feeding on seagrass and 3 ± 0.1 h d$^{–1}$ ($\bar Y \pm S_Y$) feeding on fish. The first mean is based on 20 sampled sharks ($n_X$= 20), the second on 10 ($n_Y$= 10). If I want to estimate the sum mean, I use $\theta = \bar X + \bar Y$. In this example $\theta$ = 8 h d$^{–1}$.
Product: Imagine a forest with a mean density of 10 ± 3 plants m$^{-2}$ ($\bar X \pm S_X$) and a mean photosynthetic rate of 20 ± 0.5 $\mu$mol O$_2$ plant$^{-1}$ min$^{-1}$ ($\bar Y \pm S_Y$). The first mean is based on 15 replicate measurements ($n_X$= 15), the second on 40 ($n_Y$= 40). If I want to estimate the product mean, I use $\rho = \bar X\bar Y$. In this example $\rho$ = 200 $\mu$mol O$_2$ m$^{-2}$ min$^{-1}$.
I want to know how to calculate $SE_\theta$ and $SE_\rho$ using the provided information in each example.
The accepted answer to this question provides this equation for $SE_\theta$:
$SE_\theta = \sqrt{\frac{S_Y^2}{n_Y} + \frac{S_X^2}{n_X}}$
This very helpful paper provides this equation for $SE_\rho$:
$SE_\rho = \sqrt{\frac{\bar X^2S_Y^2}{n_Y} + \frac{\bar Y^2S_X^2}{n_X} + \frac{S_X^2S_Y^2}{n_Xn_Y}}$
Are these equations correct?
Answer: I have found my own answer in the meantime and thought it might be useful to share it here. The equations for the standard error of the sum mean ($SE_\theta$) and the standard error of the product mean ($SE_\rho$) are correct. They are derived from $Var(\bar X + \bar Y) = Var(\bar X) + Var(\bar Y)$ and $Var(\bar X\bar Y) = E(X)^2Var(\bar Y) + E(Y)^2Var(\bar X) + Var(\bar X)Var(\bar Y)$.
When the standard errors of $\bar X$ and $\bar Y$ are given, $SE_\theta$ and $SE_\rho$ can be calculated as
$SE_\theta = \sqrt{SE_\bar X^2 + SE_\bar Y^2}$ and $SE_\rho = \sqrt{\bar X^2SE_\bar Y^2 + \bar Y^2SE_\bar X^2 + SE_\bar X^2SE_\bar Y^2}$
where $SE_\bar X = \frac{S_X}{\sqrt{n_X}}$ and $SE_\bar Y = \frac{S_Y}{\sqrt{n_Y}}$.
Comment: I think your notation and question needs some refinement. You refer to two means $\bar x_1$ and $\bar x_2$ of two random variables, but presumably your intention is to refer to two SAMPLE MEANS (not population means). The product of two population means is not a random variable (and hence the variance is zero); the product of two sample means is a random variable. Then you need to define whether your standard deviations are population standard deviations, or the square root of sample variance, and if the latter, what measure of sample variance.
Comment: FINALLY, you need to define $\bar x_3$ mathematically: on the one hand, you describe $\bar x_3 = \bar x_1 \bar x_2$;  on the other hand, you describe $\bar x_3$ as a sample mean. It is not clear what you intend.
Comment: I have updated my question accordingly. Do you have an answer to my question?
Comment: Your definition of the sample mean is incorrect, and should not have the square root.
Answer: The 2nd equation can't be exactly right as it implies that when both sample means are 0, $\sigma_3$ must be 0. Looks like your sources for the 2nd equation are ignoring the $Var(X)Var(Y)$ term under the assumption that its size will be negligible compared to the other terms.
The 1st equation is a better approximation, as per the derivation given on the Cross Validated post you linked, assuming that $\sigma_1, \sigma_2$ are the standard deviations of $\bar{x}_1, \bar{x}_2$, not the sample standard deviations. (Intuition: as you increase your sample sizes, your sample standard deviations will converge to the population standard deviation, but the standard deviation in your estimates of $\bar{x}_1, \bar{x}_2$ should converge to 0, so the standard deviation in your estimate of $\bar{x}_3$ should converge to 0). To be exact, you would need to use the true population means rather than the sample means.
Edit: rereading your question it's unclear whether you are looking for the standard error in the estimate of the product mean (this is what "I want to calculate the standard deviation of the product of the first two sample means" means) or just for the standard deviation in the distribution of the product of the two independent variables (which I think is what your forest calculation is doing).
Answer: For your revised problem:
Given: $X$ and $Y$ are independent random variables with different sample sizes, say $n$ and $m$ respectively.
The variance of two independent random variables, in this case, being the sample means $\bar X$ and $\bar Y$, is:
$$ {\rm Var}(\bar X \space \bar Y) ={\rm Var}(\bar X){\rm Var}(\bar Y)+{\rm Var}(\bar X)(E(\bar Y))^2+{\rm Var}(\bar Y)(E(\bar X))^2$$
where:

$E(\bar X) = \mu_X$
$E(\bar Y) = \mu_Y$
${\rm Var}(\bar X) = \frac1n {\rm Var}(X)$
${\rm Var}(\bar Y) = \frac1m {\rm Var}(Y)$

That is all there is to it.
Comment: The solution is expressed in terms of central and raw moments of the population, the definitions are provided for those terms, and is completely general.
Comment: Ok thanks, I see that you have defined the output, but of what variable is the sample size $n$. I want to be able to calculate the standard deviation for the product of two sample means with different sample sizes, so I need to differentiate between $n_X$ and $n_Y$.
Comment: So you agree that the first equation I provided yields the best estimate?
Comment: No - your first equation is incorrect: (i) it uses sample estimators, whereas the correct solution requires the population moments; (ii) the solution is not in any event an estimate, but the calculation of the exact variance of the product of two independent sample means


virtualhost not working properly, site only displays with www prefix

Question: I'm configuring my first Apache server with mod_wsgi and it isn't working properly. The script runs fine if the browser navigates to : www.sitename.com, but navigating to sitename.com only displays the document root.
<VirtualHost *:80>
        DocumentRoot /var/www
        ServerName sitename.com
        ServerAlias www.sitename.com
        WSGIScriptAlias / /var/www/sitename.com/site/site.wsgi
</VirtualHost>

Any help much appreciated!
Comment: Your problem is most likely that you have another VirtualHost definition in the Apache configuration with ServerName set to sitename.com and for that hostname it is using that instead. It is only use the above for www.sitename.com as likely that is the only VirtualHost it is listed in.
Answer: This is not an exact solution to your problem, but a recommendation.
You should only use one domain, with or without www to serve content to your visitors, and then use HTTP 301 redirect to direct users to the real site. This way Google cannot have any issues with possible duplicate content on different domains.


Run VB Script from Windows Service

Question: I have written a small Windows Service in C# which executes .BAT and .VBS scripts. The following code triggers the .vbs script:
string path = "C:\\Path\\To\\MyScript.vbs";
Process p = new Process();
p.StartInfo = new ProcessStartInfo();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WorkingDirectory = Environment.SystemDirectory;
p.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe");
p.StartInfo.Arguments = "/C C:\\Windows\\System32\\cscript.exe //Nologo \"" + path + "\"";
p.StartInfo.ErrorDialog = false;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.OutputDataReceived += OutputDataReceived;
p.ErrorDataReceived += OutputDataReceived;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

try
{
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.WaitForExit();
}
catch { // }

The code compiles and runs without any exceptions. However, there miust be something wrong since the VBS script does not execute until the end. Inside the script, I open a telnet session and enter some commands. When I run the same code and the same script in a WinForms Application, everything works perfectly fine.
I assume the problem is caused by my service, which runs as LOCAL SYSTEM.
Any ideas?
EDIT
This is my VBScript:
set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run "cmd"
WScript.Sleep 1000

WshShell.SendKeys "telnet 127.0.0.1 3000"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 2000

WshShell.SendKeys "reboot application"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 2000

For some reason, the telnet command is never executed, but the Process still ends with exitcode '0'...
Comment: Did you debug the service and see what happens ?
Comment: I did, but I get neither an exception nor any error message...
Comment: You might not get exception but Process.Start() might throw error code. Check process.ExitCode and you should get the integer describing the cause.
Comment: Thanks for the hint. The process exits with '0', so it looks like everything is okay. I will add the VB script above to provide additional information.
Answer: Got it - SendKeys does not work when no user is logged in. The code itself works perfectly fine.

Run VB Script from Windows Service


How can I get *ngIf to show the div when a certain string is present

Question: Still trying to grasp Angular. How can I use the *ngIf directive to only display to following div when a certain string is present. To be more specific, I want the div to show when the phrase 'Que' is present in the 'reason' database column which is part of the database table TutorRequests.
I have tried to use the indexOf() method but I am assuming it is not correctly implemented.

Tutor Request

{{tutorRequest.tutor.firstName + " " +
             tutorRequest.tutor.lastName}}

{{tutorRequest.request.reason}}

           <button style="margin-top: 1px;" class="btn btn-warning btn-sm"
             (click)="rejectTutorRequest(tutorRequest.tutor.userID)">Unflag
           </button>

Tutor
                    Requests to show

I am sure this is a simple fix, but again I am new to angular.
Comment: indexOf return -1 if not exists, but 0 if "Que" is at the start of reason
Answer: You can use the includes method to check if a string contains a substring.

Answer:
ES2016 Specifications included the includes() method for Array data structure.
The includes() method check if an array includes a certain element, returning true or false as appropriate.
The includes method finds NaN and undefined whereas the indexOf method doesn't.

So thats why you can use following code
*ngIf="tutorRequests.reason.includes('Que')"

Answer: Since > is a special HTML character, you need to use an HTML entity.

Of course, if you only need to know if the string starts with "Que", then this will do:

Comment: Many thanks for your quick suggestion. Yes I do only have to know if it starts with 'Que'. I will give it a go!
Comment: could just do `!tutorRequests.reason.indexOf('Que')` in this case


Trigger compactions when TWCS TTL changes in Cassandra 3.0

Question: I have a table in cassandra where I saved data using clients TTL = 1 month (tables TTL is 0), the table is configured with time window compaction strategy.
Everyday Cassandra cleaned one single sstable containing expired data from one month ago.
Recently I changed the clients TTL to 15 days, I was expecting cassandra to clean two sstables a day at some point, and release the space. But it keeps cleaning one sstable a day and keeping 15 days of dead data.
How do I know?
for f in /data/cassandra/data/keyspace/table-*/*Data.db; do meta=$(sudo sstablemetadata $f); echo -e "Max:" $(date --date=@$(echo "$meta" | grep Maximum\ time | cut -d" " -f3| cut -c 1-10) '+%m/%d/%Y') "Min:" $(date --date=@$(echo "$meta" | grep Minimum\ time | cut -d" " -f3| cut -c 1-10) '+%m/%d/%Y') $(echo "$meta" | grep droppable) ' \t ' $(ls -lh $f | awk '{print $5" "$6" "$7" "$8" "$9}'); done | sort

This command list all the sstables
Max: 05/19/2018 Min: 05/18/2018 Estimated droppable tombstones: 0.9876591095477787        84G May 21 02:59 /data/cassandra/data/pcc/data_history-c46a3220980211e7991e7d12377f9342/mc-218473-big-Data.db
Max: 05/20/2018 Min: 05/19/2018 Estimated droppable tombstones: 0.9875830312750179        84G May 22 15:25 /data/cassandra/data/pcc/data_history-c46a3220980211e7991e7d12377f9342/mc-221915-big-Data.db
Max: 05/21/2018 Min: 05/20/2018 Estimated droppable tombstones: 0.9876636061230402        85G May 23 13:56 /data/cassandra/data/pcc/data_history-c46a3220980211e7991e7d12377f9342/mc-224302-big-Data.db
...

For now I have been triggering the compactions manually using JMX, but I want all erased as it would normally do.
run -b org.apache.cassandra.db:type=CompactionManager forceUserDefinedCompaction /data/cassandra/data/keyspace/sstable_path

Answer: I think I figured it out. Had to run manual compaction on the oldest and newest sstable with all it's content expired, both sstables at the same time.
After a couple of days it cleanead everything.
How do I know it was running? Because when I tried to run forceUserDefinedCompaction on any other sstable in between it always returned null.
EDIT: It didn't work, the expired Sstable count keeps increasing once again


How to find all solutions to system of congruences with Chinese Remainder Theorem?

Question: This is from Discrete Mathematics and its Applications

Here is my work so far
gcd(3, 4) = 1 $\,$gcd(3, 5) = 1$\,$gcd(4,5) = 1
$\quad$ mod 3 $\quad$ mod 4 $\quad$mod 5
x $\equiv$ 4 * 5$\qquad$3 * 5 * 3 $\quad$ 3 * 4 * 4
Applying mod 3 to everything, you get  x $\equiv$ 20$\equiv$2 (mod 3), which matches up
Applying mod 4 to everything, you get x $\equiv$ 15$\equiv$3 (mod 4), which doesn't match up with x $\equiv$2 (mod 3) so i multiplied 15 by 3 to get 45, meaning x $\equiv$ 45$\equiv$1 (mod 4)
And finally after applying mod 5 to everything, you get x $\equiv$ 12$\equiv$2 (mod 5), which doesn't match up with x $\equiv$3 (mod 5) so I multiplied 12 by 4 to get 48, meaning x $\equiv$ 48$\equiv$3 (mod 5).
To find one solution, I added up (4)(5) + (3)(5)(3) + (3)(4)(4) to get 113. I tested this solution and it worked for the system of congruences.
How would you find all the other solutions to this? Would you use set builder notation?
Answer: Set-builder notation is just a compact way of describing a set. You can use set-builder notation to describe the set of solutions once you find out what that set is, but it won't help you actually to find the solutions.
Proceed as in the video: $3\cdot4\cdot5=60$ is congruent to $0$ modulo $3,4$ and $5$, so adding or subtracting any integral multiple of $60$ won't affect the value modulo $3,4$, or $5$. That is $60\equiv 0\pmod 3$, so $113+60n\equiv 2+0\cdot n\equiv 2\pmod 3$, so any number of the form $113+60n$ is still a solution to the first congruence. Similar calculations show that $113+60n\equiv 1\pmod 4$ and $113+60n\equiv 3\pmod 5$ for each integer $n$, so every number of the form $113+60n$ is a solution to the system of three congruences. It turns out that these are the only solutions, so the set of solutions is
$$\{113+60n:n\in\Bbb Z\}\;,$$
where as usual $\Bbb Z$ is the set of integers. Equivalently, it's
$$\{n\in\Bbb Z:n\equiv 113\!\!\pmod{60}\}\;.$$
Note that in most contexts it's customary to take $53$, obtained when $n=-1$, as a more or less standard representative of this set rather than $113$, so that one would more likely write
$$\{n\in\Bbb Z:n\equiv 53\!\!\pmod{60}\}\;.$$
Answer: Generally it is much easier to successively solve the system in pairs. For example:
${\rm mod}\ 5\!:\  3\equiv x\iff x = \color{#0a0}{3+5i}$
$\smash[t]{{\rm mod}\ 4\!:\  1\equiv x\equiv \color{#0a0}{3\!+\!5i}\equiv 3\!+\!i\iff i = -2\!+\!4j\iff x=\color{#0a0}{3\!+\!5}(\overbrace{-2\!+\!4j}^{\large\color{#0a0} i})=\,\color{#c00}{-7\!+\!20j}}$
$\smash[b]{{\rm mod}\ 3\!:\ 2\equiv x\equiv \color{#c00}{-7+20j}\equiv 2-j\iff j\equiv0\iff j=3k\iff x= \,\color{#c00}{-7+20}\underbrace{(3k)}_{\large\color{#c00}j}}$
Thus $\,x\,$ satisfies the system $\iff x\equiv \color{#c00}{-7}\equiv 53\pmod{60}$
Answer: The system of equations is
$$\begin{cases}x=\color{green}2\pmod 3\\
x=\color{green}1\pmod4\\
x=\color{green}3\pmod5\end{cases}$$
Now do as the proof here :
$$\begin{align}&N_1=20\implies x_1=N_1^{-1}\pmod3=\color{red}2\\
&N_2=15\implies x_2=N_2^{-1}\pmod4=\color{red}3\\
&N_3=12\implies x_3=N_3^{-1}\pmod5=\color{red}3\end{align}$$
and now we define
$$x:=20\cdot\color{green}2\cdot\color{red}2+15\cdot\color{green}1\cdot\color{red}3+12\cdot\color{green}3\cdot\color{red}3=233=53\pmod{3\cdot4\cdot5}=53\pmod{60}$$


When a drummer wears headphones, what is he listening to?

Question: I recently watched a recording of Jean Michel Jarre's Twelve Dreams of the Sun concert and noticed that his drummer seems to rhythmically move to a beat not audible to the audience, e.g. as 11:10. You can clearly see he's having fun doing what he does, but what exactly might he be listening to?
Comment: probably a click track, or click + cues - though I'm not going to watch 2 hours of youtube just to find an example.
Comment: You should incorporate that into an answer Tetsujin.
Comment: So tempting to suggest he's listening to "Toad"  (for you youngsters, that's  http://en.wikipedia.org/wiki/Toad_%28instrumental%29 )
Comment: @user 2808054 - you beat me to it! I used to play with a guy who had the Beano on his music stand, for the more tedious parts of a gig.
Comment: @Tim He sure did, but only by little less than 1.5 years.
Comment: "Breathe in, breathe out. Breathe in, breathe out..."
Comment: I thought it was a tad terse to really be an answer ;-) Having at least a time-point to look at would possibly help, even though the answer will still likely be the same
Comment: @Wormbo - better late than never! I'm not using this site *all* the time!
Comment: @Tim LOL re the Beano! Oh I hope some over-the-shoulder camera footage captured that !
Answer: He is probably listening to a click track, which is a kind of sophisticated metronome. In concerts like this, most of the music is being played by live musicians, but some parts of the music you are hearing have been pre-recorded in a recording studio. With each piece of music, the drummer is listening to a rhythmic clicking sound which is synchronized to the beat of the pre-recorded parts. This enables the entire band to perform their live music alongside the pre-recorded tracks and to keep everything tightly synchronized.
Comment: I should have mentioned a time index in the question. For example 11:10 supports the click track explanation. I'll accept this answer unless anyone can provide a better theory.
Comment: I would add there may or may not be monitors in the headphones also. Sometimes the headphones are for click only and traditional monitors are used and set to be audible through the headphones, and or some monitor signal may be mixed in with the click and either no traditional monitors used or just a sub is used or they are set at a lower level to fill out the sound. Side note: The drummer for Mutemath is famous for taping the headphones to his head to keep them from falling off from his gyrations.
Answer: I skimmed through your youtube video, and found a spot at about 1:07:08 or 1:07:09 that I think shows what you're talking about.  We hear strings and piano, the drummer has some rests, nods his head with the beat and then comes in.  In this case my guess is he isn't listening to a click track, because there is a conductor -- I saw the conductor at, for example, 1:03:19.  Now, if the conductor had headphones too, then I could see that they both might be listening to a recording of the piece.
I think that at the 1:07:08 spot, the drummer is probably moving his head to the beat, feeling his rests before he comes in.
If you're wondering why the drummer is wearing headphones at all, I believe it's to protect his hearing.  He needs to hear (monitor) what the rest of the ensemble is doing; and he needs to hear himself but with reduced volume.
Comment: (cont.) when not to, etc.  Some people said they are more likely to use one in the recording studio than live.  Personally, having played in symphony orchestras for years, I just don't see how a drummer playing with a full orchestra can use one, unless the conductor is using it too.  After all, this drum part has some pretty substantial rests -- and that's different from a simple pop or rock tune, where the drummer probably plays consistently all the way through.  When the drummer isn't playing the conductor is liable to wander off a click track if he can't hear it.
Comment: I'm sorry, I should have mentioned a time index. Starting with 11:10, for example, you can clearly see the behavior I mentioned.
Comment: I've watched a lot of drummers play live in small to medium venues -- I live in a university town.  (I've never been to a big rock concert, though.)  I don't think a drummer moving to a beat in his head (or given by a conductor) means that he's necessarily listening to a click track.  However, I can't swear this drummer isn't listening to a click track.  My problem is I just don't know what the layout was for this concert.  Could the drummer see the conductor?  -  Out of curiosity I did some reading on the internet, and found lots of discussions of when to use a click track, (cont.)
Answer: Personally, I listen to an audiobook of the latest Stephen King whilst playing a gig on the drums.
More seriously, there's three likely answers:

A click track, basically a metronome pulse, possibly with the same signal being heard by some of the other musicians, keeping them in time.
A Monitor track, they're hearing a mix of the music which is taylored to the drummer, it'll probably have a lot of the bass sounds and vocals.
Far more rarely, there may be instructions from a musical director, including some real-time notes on the performance. This doesn't happen often unless it's a musical theater-type performance, and would often be heard as well as one of the above signals.
Answer: I was invited to go out on the road and play with a country band, im a drummer, and they told me they use click tracks in their ears to keep time! Im new to this gear! But sounds like fun!!!
Comment: If they *need* to use click tracks to keep in time, they can't think much of the drummer! That's way more than half of his job, and apart, unless they're playing for line dancing, etc., they most likely aren't aware that a lot of music needs to get stretched and squashed, ever so slightly, otherwise it ends up more like disco stuff. Unless, of course, they're using backing tracks as well.
Comment: Rather sadly, a lot of bands think they have to reproduce a studio recording on their live gigs.  That means pre-recording all the stuff that is impossible to play live.  So they're locked to a click track.

It's sad, because the logical conclusion is to record it ALL and just become a mime act.
Comment: Maybe thats what it is then! But this band is doing original recordings. I don t think i ll need the click track! I play loud anyway!!!
Answer: It is probably that the headphones is linked to the sound system, and works as monitors so he can hear a balanced sound image of the total live performance and hear the others over the drums.
Answer: He may be hearing a click or other pre-recorded material - a simple sequenced drum track can be more useful to play with than a simple click.    He may be monitoring the rest of the band - wedge monitors are not always appropriate among the forest of mics often used on a drum kit.  Or, if he's sensible and wants to have some hearing left when he's 40, they might be ear protectors.
My drummer for a school production once was very attached to his ear protectors.  Very sensible too, when he was playing in his rock band.  It took some time to persuade him that in THIS situation his job was to play quieter, so he didn't NEED the protectors.
Answer: An additional consideration which I don't think has been mentioned yet.
In huge stadium production gigs such as those put on by Jean-Michel Jarre there are other elements than just the music, such as video projections, pyrotechnics, etc.
The drummer will be listening to a click track and/or other cues in their headphones so that they, and as a consequence the rest of the band, are precisely in time with the computer system that is driving the non-musical effects. Any slight timing errors between the band and the video wall behind them would be glaringly obvious, so the drummer follows the click track to make sure that doesn't happen.


How do I test Go's "testing" package functions?

Question: I'm writing a library of testing utility functions and I would like them to be themselves tested.
An example of one such function is:
func IsShwifty(t *testing.T, foo string) bool {
    result, err := bar.MyFunction(string)
    if err != nil {
      t.Error("Failed to get shwifty: " + foo, err)
    }
    return result == "shwifty"
}

I would like to write a TestIsShwifty that feeds in something to make MyFunction return an error and then make t.Error. Then, I want to have the TestIsShwifty pass with something like:
func TestIsShwifty(t *testing.T) {
  if doesError(t, IsShwifty(t)) == false {
    t.Error("Oh know! We didn't error!")
  }
}

Is this possible in Go?
Comment: What are the obstacles you came across? What is the error?
Comment: The error will break my test, but I would like my test to pass when it errors.
Comment: It seems you want something that is not supported by the test tool. I don't think you are going to achieve it.
Answer: I figured it out!
I just needed to create a separate instance of testing.T.
func TestIsShwifty(t *testing.T) {
  newT := testing.T{}
  IsShwifty(newT)
  if newT.Failed() == false {
    t.Error("Test should have failed")
  }
}


Trying to build a C# grammar for bison/wisent

Question: I've never done Bison or Wisent before.
how can I get started?
My real goal is to produce a working Wisent/Semantic grammar for C#, to allow C# to be edited in emacs with code-completion, and all the other CEDET goodies. (For those who don't know, Wisent is a emacs-lisp port of GNU Bison, which is included into CEDET.  The Wisent apparently is a European Bison.  And Bison, I take it, is a play-on-words deriving from YACC. And CEDET is a Collection of Emacs Development Tools. All caught up?  I'm not going to try to define emacs. )
Microsoft provides the BNF grammar for C#, including all the LINQ extensions, in the language reference document.  I was able to translate that into a .wy file that compiles successfully with semantic-grammar-create-package.
But the compiled grammar doesn't "work".  In some cases the grammar "finds" enum declarations, but not class declarations. Why?  I don't know.  I haven't been able to get it to recognize attributes.
I'm not finding the "debugging" of the grammar to be very easy.
I thought I'd take a step back and try to produce a wisent grammar for a vastly simpler language, a toy language with only a few keywords. Just to sort of gain some experience. Even that is proving a challenge.
I've seen the .info documents on the grammar fw, and wisent, but... still those things are not really clarifying for me, how the stuff really works.
So
Q1: any tips on debugging a wisent grammar in emacs?  Is there a way to run a "lint-like" thing on the grammar to find out if there are unused rules, dead-ends stuff like that?  What about being able to watch the parser in action?  Anything like that?
Q2: Any tips on coming up to speed on bison/wisent in general?  What I'm thinking is a tool that will allow me to gain some insight into how the rules work. Something that provides some transparency, instead of  the "it didn't work" experience i'm getting now with Wisent.
Q3: Rather than continue to fight this, should I give up and become an organic farmer?

ps:  I know about the existing C# grammar in the contrib directory of CEDET/semantic.  That thing works, but ... It doesn't support the latest C# spec, including LINQ, partial classes and methods, yield, anonymous methods, object initializers, and so on.  Also it mostly punts on parsing a bunch of the C# code. It sniffs out the classes and methods, and then bails out.  Even foreach loops aren't done quite right.  It's good as far as it goes, but I'd like to see it be better. What I'm trying to do is make it current, and also extend it to parse more of the C# code.
Answer: You may want to look at the calc example in the semantic/wisent directory.  It is quite simple, and also shows how to use the %left and %right features.  It will "execute" the code instead of convert it into tags.  Some other simple grammars include the 'dot' parser in cogre, and the srecode parser in srecode.
For wisent debugging, there is a verbosity flag in the menu, though to be honest I hadn't tried it.  There is also wisent-debug-on-entry which lets you select a action that will cause the Emacs debugger to stop in that action so you can see what the values are.
The older "bovine" parser has a debug mode that allows you to step through the rules, but it was never ported to wisent.  That is a feature I have sorely missed as I write wisent parsers.
Answer: Regarding Q1:
1st make sure that the wisent parser is actually used:
(fetch-overload 'semantic-parse-stream)

should return wisent-parse-stream.
Run the following elisp-snippet:
(easy-menu-add-item semantic-mode-map '(menu-bar cedet-menu) ["Wisent-Debug" wisent-debug-toggle :style toggle :selected (wisent-debug-active)])
(defun wisent-debug-active ()
  "Return non-nil if wisent debugging is active."
  (assoc 'wisent-parse-action-debug (ad-get-advice-info-field 'wisent-parse-action 'after)))
(defun wisent-debug-toggle ()
  "Install debugging of wisent-parser"
  (interactive)
  (if (wisent-debug-active)
      (ad-unadvise 'wisent-parse-action)
    (defadvice wisent-parse-action (after wisent-parse-action-debug activate)
      (princ (format "\ntoken:%S;\nactionList:%S;\nreturn:%S\n"
             (eval i)
             (eval al)
             (eval ad-return-value)) (get-buffer-create "*wisent-debug*"))))
  (let ((fileName (locate-file "semantic/wisent/wisent" load-path '(".el" ".el.gz")))
    fct found)
    (if fileName
    (with-current-buffer (find-file-noselect fileName)
      (goto-char (point-max))
      (while (progn
           (backward-list)
           (setq fct (sexp-at-point))
           (null
            (or
             (bobp)
             (and
              (listp fct)
              (eq 'defun (car fct))
              (setq found (eq 'wisent-parse (cadr fct))))))))
      (if found
          (eval fct)
        (error "Did not find wisent-parse.")))
      (error "Source file for semantic/wisent/wisent not found.")
      )))

It creates a new entry Wisent-Debug in the Development-menu. Clicking this entry toggles debugging of the wisent parser. Next time you reparse a buffer with the wisent-parser it outputs debug information to the buffer *wisent debug*. The buffer *wisent debug* is not shown automatically but you find it via the buffer menu.
To avoid a flooding of *wisent debug* you should disable "Reparse when idle".
From time to time you shold clear the buffer *wisent debug* with erase-buffer.


Binding gets an old value out of nowhere

Question: I have a control which derives from a TextBox and has 2 additional string properties: Formula and FormulaResult (readonly):
public class SpreadsheetCellControl : TextBox, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };

    public string Formula
    {
        get;
        set;
    }
    public string FormulaResult
    {
        get
        {
            if (Formula != null && Formula != "")
            {
                string formula = Formula;
                formula = formula.StartsWith("=") ? formula.Substring(1) : formula;
                var calculation = Calculator.Calculate(formula);
                return calculation.Result.ToString();
            }
            else return "";
        }
    }

    protected override void OnPreviewLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
    {
        Formula = Text;
        base.OnPreviewLostKeyboardFocus(e);
    }
}

As defined in XAML, Text is bound to Formula when the control is focused and FormulaResult when it is not focused:

    </Style>

Everything works as it should. For instance, I click the control, type for example "1/2", click anywhere else and it shows "0.5".
Then I set the Formula property from code-behind to "3" (any value actually). Before I click on the control, it shows all three properties correct: 3, 3 and 3. But when I click on the control, the Text property suddenly becomes the old "1/2", which is not stored anywhere. I checked that by placing a TextBlock and binding its Text to all three values of the cell control. Debugging also showed only new values.
P.S. Also "Formula" does not seem to take values by itself from "Text" here: Setter Property="Text" Value="{Binding Formula, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Comment: As a note, the Binding in the Text Setter inside the Trigger seems to be missing `RelativeSource Self`.
Comment: Can you try with making the Formula as a Dependency property or ReadOnly DependencyProperty?
Comment: @Clemens Thank you! That little mistake caused the whole problem. I wasn't even sure on whether to mention that. Could you post it as an answer?
Answer: The Binding in the Text property Setter inside the Trigger is missing RelativeSource Self:
<Setter Property="Text"
        Value="{Binding Path=Formula,
                        RelativeSource={RelativeSource Self},
                        Mode=TwoWay,
                        UpdateSourceTrigger=PropertyChanged}"/>


What is an interpretation of the $\,f'\!\left(\sum_i w_{ij}y_i\right)$ factor in the in the $\delta$-rule in back propagation?

Question: In the $\delta$-rule which is used for error back propagation in neural networks, there is a factor $f'\!\left(\sum_i w_{ij}y_i\right)$, often written as $f'(\text{net}_i)$, which is just the derivative of the activation or transfer function evaluated at the weighted sum of the input. The derivative of $\tanh$ is bell-shaped so that values $\left|x\right|\geqslant3$ it is almost zero:

This means that units with a strong activation in either way have a more limited scope of action, where as values close to zero have more leeway. If I'm understanding it correctly, it keeps neurons that have already strongly decided one way or the other in their configuration. What is the purpose of this for weight updating? How would the networks behave differently if one would remove or change this factor?

As a reminder, the full $\delta$-rule looks like this for hidden neurons and output neurons:
$$\Delta w_{ij} = \eta\;\delta_j\;y_i$$
$$\delta_j = (\hat y_j - y_j)\;f'(\text{net}_j) \quad \text{or} \quad \delta_j = \sum\limits_k^K\left(w_{jk}\delta_k\right)\;f'(\text{net}_j)$$
where $\Delta w_{ij}$ is the change of the weight that connects the neuron $i^{\text{th}}$ neuron in the preceding layer with the $j^{\text{th}}$ neuron in the layer we are currently considering; $\eta$ is the learning rate, $y_j$ is the output of the current neuron, $\hat y_j$ is the teacher value for the $j^{\text{th}}$ output, $y_i$ is the output of the $i^{\text{th}}$, and $k$ is an index in the subsequent layer.
Answer: I think that you are overanalysing this a bit too much. I am sure you are aware that the back-propagation algorithm is mathematically derived by search for the derivates of the weights with respect to the loss.
Therefore the derivative of the activation function evaluated at the net input of the layer is a necessaty in the backward-pass and can not be removed or changed. I mean you can obviously remove/alter this aspect, but the mathematical foundation of back-prop will become invalid which would result in ineffective weight updates (i.e. you are no longer moving in the direction of the gradient) and thus is likely to yield worse convergence and/or a worse optima when training. What exactly would happen can not be determined, unless you start fixing other aspects of the network and do an empirical comparison.
I do like your analysis on the bell-shape of the derivative of the activation function (which btw a sigmoid activation function also has), however don't get tricked into over analysing this, as for example a relu activation function (max(0,x)), which is often used, has a constant derivative. So in fact you could argue that you can remove this property by switching to a relu unit although you would have to adjust the forward pass as well of course
At last if you are looking for a particular problem with the tanh, sigmoid activation functions then you should focus on the fact that they are saturating non-linearities. This makes it such that large layer inputs result in similar activations even if they are quite far apart. This indifference to change when the net input is large (or very small) may trouble training, as in the forward and backward pass the changes are unnoticeable.
Comment: Oh, I see what you are trying to say. Indeed when you would remove, or alter the derivative term then this would not lead to a different gradient direction, however the magnitude will change (and at each consecutive layer this effect will be larger). It therefore may occur that you overshoot/undershoot the proposed step, from which there may be another optimum which is more attractive. I am thus saying that you will likely end up in a different optimum, be it worse or better. This makes it very unpredictable. Especially when combining this with stochastic grad desc as opposed to grad desc
Comment: Isn't the direction of the gradient determined by the error differentials which are passed from the subsequent layer or the teacher-output difference? The co-domain of the derivative of $\tanh$ is positive, so it can't have an effect of the direction of the gradient unless I'm overlooking something.
Comment: That is correct. The initial direction is determined by the direction of the deltas in the last layer. This direction is then back-propagated through each layer, where the weights alter the direction according to the contribution of the layer in the forward pass


Is there a simpler way to decode this json in Go?

Question: I am trying to parse some JSON from Jira to variables. This is using the go-jira package (https://godoc.org/github.com/andygrunwald/go-jira)
Currently I have some code to get the developer:
dev := jiraIssue.Fields.Unknowns["customfield_11343"].(map[string]interface{})["name"]
and team := jiraIssue.Fields.Unknowns["customfield_12046"].([]interface{})[0].(map[string]interface{})["value"]
to get the team they are a part of from.
Getting the team they are on is a bit gross, is there a cleaner way to get the team besides having to type assert, set the index, then type assert again?
Here is the complete json (modified but structure is same, its way too long):
{
 "expand":"renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations",
   "id":"136944",
   "self":"https://jira.redacted.com/rest/api/2/issue/136944",
   "key":"RM-2506",
   "fields":{
      "customfield_11343":{
         "self":"https://redacted.com/rest/api/2/user?username=flast",
         "name":"flast",
         "key":"flast",
         "emailAddress":"firstname.lastname@example.com",
         "displayName":"first last",
         "active":true,
         "timeZone":"Europe/London"
      },
      "customfield_12046":[
         {
            "self":"https://jira.redacted.com/rest/api/2/customFieldOption/12045",
            "value":"diy",
            "id":"12045"
         }
      ],

   }

Thanks
Comment: The usual approach is to define Go types that match the structure of the JSON and decode to the type.  Show the complete JSON.
Comment: Are the value types arbitrary, or is there a documented mapping between the field names and the value types?
Comment: added more of the json
Comment: The custom fields have different value types.  Is the type determined by the name of the field ("customfield_11343" is an example) or something else?
Comment: that is determined by Jira themselves
Comment: from what I can tell its pretty much arbitrary. jira offers a very high level way of choosing customfields and it populates the types and the entries
Comment: Dave Johnson in his answer is on the right track in my opinion, however you do not need reflection to check whether the value is an array or an object, all you need is to take a look at the first byte. https://play.golang.org/p/GiVx60gY-ng
Answer: This is a tough one since the second one is in an array form. It makes it hard to use a map.
For the first one, it's simple enough to use:
type JiraCustomField struct {
    Self         string `json:"self"`
    Name         string `json:"name"`
    Key          string `json:"key"`
    EmailAddress string `json:"emailAddress"`
    DisplayName  string `json:"displayName"`
    Active       bool   `json:"active"`
    TimeZone     string `json:"timeZone"`
}
type JiraPayload struct {
    Expand string                     `json:"expand"`
    ID     string                     `json:"id"`
    Key    string                     `json:"key"`
    Fields map[string]JiraCustomField `json:"fields"`
}

https://play.golang.org/p/y8-g6r0kInV
Specifically this part Fields map[string]JiraCustomField for the second case it looks like you need it in an array form like Fields map[string][]JiraCustomField.
In a case like this, I think you'll need to make your own Unmarshaler. This is a good tutorial: https://blog.gopheracademy.com/advent-2016/advanced-encoding-decoding/
What you could do with your custom Unmarshal/marshaler, is use the Reflection package and check if it's an array or a struct. If it's a struct then put it into an array, and store it in Fields map[string][]JiraCustomField.
Answer: The way I go about problems like this is:

Copy some JSON with things I am interested in and paste it into https://mholt.github.io/json-to-go/
Remove fields that aren´t of interest.
Just read the data and unmarshal.

You might end up with something like this given the two custom fields of interest, but you can cut the structure down further if you just need the name.
type AutoGenerated struct {
    Fields struct {
        Customfield11343 struct {
            Self         string `json:"self"`
            Name         string `json:"name"`
            Key          string `json:"key"`
            EmailAddress string `json:"emailAddress"`
            DisplayName  string `json:"displayName"`
            Active       bool   `json:"active"`
            TimeZone     string `json:"timeZone"`
        } `json:"customfield_11343"`
        Customfield12046 []struct {
            Self  string `json:"self"`
            Value string `json:"value"`
            ID    string `json:"id"`
        } `json:"customfield_12046"`
    } `json:"fields"`
}

The effect you get is that all extra information in the feed is discarded, but you get the data you want very cleanly.
Comment: wow okay, so all along there was a .Values() method of the Unknowns type.. and you can access fields like this `dev, exists := jiraIssue.Fields.Unknowns.Value("customfield_11343/name")` . I appreciate the learning from the answers though
Comment: hmm.. so what go-jira does is return an IssueFields type, with those customfields in the "Unknowns" data. I assume i would need to do something similar to this but get the data from the MarshalMap of the Unknowns into my created struct?
Comment: Cool :-) I think you can also do a NewRawRequest t if you want the library to mostly go away but still use it for authentication.


Creating plots with ggplot in R

Question: I am relatively new to R and now I am trying to use ggplot to create some graphs.
However, I proably did not understand the logic fully, because I have some problems now:
For example, I have the following dataframe which I alrady reshaped:
     id variable value
1     1 Var1     1
2     1 Var2     1
3     2 Var1     NA
4     2 Var2     NA
5     3 Var1     1
6     3 Var2     NA
7     4 Var1     NA
8     4 Var2     1
...

Now, I want to produce a simple plot (like a barplot) that adds up the values for each of the two varaibles.
Howver, I do not get any sensible results...
In a second data frame, my data looks different
     id variable value
1     1 Var1     4
2     2 Var1     5
3     3 Var1     2
4     4 Var1     5
5     5 Var1     3
6     6 Var1     1
7     7 Var1     5
8     8 Var1     4
...

Here I would also like to get a barplot, but here I would like to get the percentage shares of the values as bars.
E.g. 20% said 5, 30% said 4, ... I tried this by calculatiing the percentage shares in advance, but then I wanted to add the number of observations under each bar, which did not work out, because I only had the percentages left in my dataframe. Is there a way to get the sahres as bars, but the number of observations under each bar?
I guess that should not be so diffucult, but I have problems with the logic behind ggplot...
Any help would be highly appreciated! I also checked other posts, but could not find a way that was working.
Thank you very much in adavance!
Comment: Please share a `dput(your_data)` so others can get it easily into R. "I have problems with ggplot" What have you tried? Additionally, it seems that you are asking for more than one plot, what are your expected outputs ?
Comment: Thank you for your totally correct feedback! I understand that it is necessary to improve the understanding of the issues! I will keep this in mind for the future cases.
Answer: As u/Matias Andina mentioned, you should provide a bit more information about your particular question and post your code attempts so that we can help you.  However, I can demonstrate using an example similar to the example dataset you posted and how to create a bar graph.
geom_bar() vs. geom_col()
First of all, note that ggplot has two geoms that you can use to create "bar graphs".  In this case, "geoms" refer to the geometries that are applied to the chart area to "draw out" your actual plot: geom_line for the lines on a line chart, geom_point to display the points on a scatter plot, etc.
For bar charts, see this documentation, which describes the differences between geom_bar and geom_col: both of which create bar graphs.  In a nutshell:

geom_bar makes the height of the bar proportional to the number of cases in a group
geom_col makes the height of the bar proportional to the value of the cases in a group

Examples of both
Here's my own dataset, modeled after the one you posted:
df <- data.frame(
    id=c(1:7),
    variable = c('var1', 'var1', 'var1', 'var1', 'var2', 'var2', 'var2'),
    value =c(1,1,3,2,4,5,2))

geom_bar plots the number of cases in a group--in this case, the number of times "var1" and "var2" appear in the dataset in the "variable" column.  For a geom_bar plot, therefore, we only specify what's on the x-axis, and the y-axis values are calculated by the stat method built into the geom_bar method.  In this case, to create the plot, it's pretty simple:
ggplot(df, aes(x=variable)) + geom_bar()

Note that we only specified "variable" on the x-axis, which is done by applying it to aes(), conveniently within the ggplot() call, but you can also do that within geom_bar() if you prefer--it's just best practice to put it in ggplot() if it applies to the whole plot.  Notice that on the y-axis, the "count" of each are represented--not the value total.  In fact, if you specify a value for x and y within aes() and attempt to use a geom_bar, you will see an error related to the stat method (stat_count()) used in geom_bar because it can't apply the method properly.
geom_col plots the value of each variable, so the only difference is that you need to specify within aes() the x- and y-axis columns, but otherwise it's still very simple:
ggplot(df, aes(x=variable, y=value)) + geom_col()

If you have a more specific question, please indicate with the output of your data (post the output of dput(your.data.frame)), and please also post your own code that you've tried with the results and a description of what your expected results would be.
Comment: Thank you very much for your very helpful answer! It was not only solving my issues, but also was helping me in understanding ggplot better! I also managed to get shares on the y-axis, which was the only thing I could not get straight away out of your answer by adding `geom_bar(aes(y = (..count..)/sum(..count..)))` to my plots.
Comment: Yes - with that you are overwriting the default `stat_count` method that is used.  To my mind I believe you can also do that within `geom_bar` via: `geom_bar(stat=...)` to specify the stat method to use... but honestly have not had too much experience trying that approach myself yet.

Creating plots with ggplot in R


Wireless Lan: 2.4 and 5.0GHz bands into single SSID?

Question: At the moment I do not need to set up, vlans to split traffic from different APs or bands, so I was wondering if is it a good idea to just have an SSID for all the APs, both 2.4 and 5.0 bands.
What may be the disadvantage of such a setup?
Answer: One disadvantage is that might make it harder to know which band you're connecting to (not that you couldn't know such as after the fact, it just wouldn't be as obvious depending on what connection manager you're using to connect to WiFi networks).  As long as you don't need the extra range that 2.4ghz offers, it's nice to ensure that you're connecting to the faster 5ghz band which also has less potential interference from things like microwaves or certain non-WiFi wireless devices.
Comment: And the other way around is also a disadvantage, as [some devices](http://readwrite.com/2013/08/07/chromecast-n-wireless-wifi-5ghz-2-4ghz) don't yet support 5hgz.


How to insert struct, map type in Apache Hudi

Question: I see the official document, there are no samples about inserting complex types like struct and map.
So, what's the grammar?
My table definition:
spark-sql> desc struct_map;
_hoodie_commit_time     string  NULL
_hoodie_commit_seqno    string  NULL
_hoodie_record_key      string  NULL
_hoodie_partition_path  string  NULL
_hoodie_file_name       string  NULL
uuid    int     NULL
col1    struct<col11:int,col12:struct<col121:int>>      NULL
col2    map<string,int> NULL

Answer: Hudi uses the spark SQL syntax, so you can use its documentation (examples from databricks doc: ex1, ex2)
For map, you can use the func map('<field 1>', val1, '<field 2>', val2, ...), and for struct, you can use the func struct(val1, val2, ...) or named_struct('<field 1>', val1, '<field 2>', val2)
INSERT INTO struct_map VALUES
(0, struct(0, struct(0)), map('key1', 1, 'key2', 2));
INSERT INTO struct_map VALUES
(0, named_struct('col11', 0, 'col12', struct('col121', 0)), map('key1', 1, 'key2', 2));


Adding process function in address field

Question: I am customising address field so need to add process function in address field element.
If I am using following code, then its adding process function but then I am not able to call protected function which are defined in address class in address field module.
<code>$info['address']['#process'][] = 'custom_feature_address_element_process';
</code>
If I am using following code, then its not working.
<code>$info['address']['#process'][]  = array(0 => 'Drupal\customaddress\Element\CustomAddress', 1 => 'customprocessAddress');
</code>
Comment: Which class are you overriding?
Comment: I am overriding address class from address field module
Answer: I have fixed the issue using following code.
<code>$info['address']['#process'][]  = 'Drupal\custom_feature\Element\CustomFeature::customprocessAddress';
</code>
Naming convention was also issue.


What kind or Result analysis can be done to analyse the characteristics of Restricted boltzmann machine?

Question: I have used Mean square error value is one of the parameter to analyse.I also noticed that every time i run a design i get different Mean square error value.Not able to identify the reason .
What would be other parameters used to analyse?
Comment: If your data is different each time, why would you expect a number calculated from that data to be the same each time?
Comment: Thanks.you meant that mean square error values depends on the input sample taken.Since, each time it takes randomly different inputs error measure also change.
Answer: There is a very nice paper by Geoffry Hinton in which he gives practical advise on how to train RBMs: http://www.cs.toronto.edu/~hinton/absps/guideTR.pdf
If you are training your RBM on images you can plot the weights from the hidden to the visible units. To give an example: As an input you have a 16 x 16 pixel image (in total 256 pixels) thus you got 256 visible units. Suppose you have 50 hidden units than from each hidden unit, there are 256 weights, one to each visible unit. You plot each weight from a given hidden unit to a visible unit according to the position the visible unit has in your image (you will end up with 50 figures, one for each hidden unit). If we are looking at hidden unit 1, weight 1,1 (connecting hidden unit 1 with visible unit 1), that weight has to fill the pixel at position 1 x 1 in image 1). If your training works, you will see patterns in your images.
What I forgot to mention is, that these images are usually greyscaled.
Furthermore you can also approximate the likelihood value and/or the free energy. (More stuff can be found in the paper)
And of course you will most unlikely end up with the same value (think of the sampling steps you are taking during each weight update).
Comment: Thanks. As per the paper the hinton's guide, i plotted histogram of learned weights after 50 epochs. i really couldnot understand what the figure represents.
Comment: This is the matlab command, i used to plot histogram of weights. Could you please provide me a guidance in understanding that graph. As per my understanding, it is representing the weight value range and how many elements fall in each value. What it represent interms of learning?                         [nelements,xcentres]=hist(vishid);
figure;
bar(xcentres,nelements);

title('Weights');


mixed models: extract slopes

Question: I have a mixed model (using lme in R) with a random intercept.
   model <- lme(HCA ~ time, random=~1|subject, data=mydata)

my supervisor asked me to extract the slopes of HCA for each individual, so that I can use this in another model.
I am in doubt here: as I have no random slope, wouldn't the slope be the same for each subject? Or should I add the intercept and the slope of time?
Thanks!
Here is the spaghetti plot
Comment: Why don't you ask your supervisor for clarification?
Answer: We can only speculate why your supervisor thinks this is a good idea, but I agree with your first interpretation of his or her instructions. Every subject has the same slope for HCA, which is the fixed effect of HCA. The random intercept and the effect of time play no role here.
Comment: The way I understood it is that the individual variability over time may correlate with another variable. I do agree that the slope is the same, but then, when I look at a spaghetti plot, I do see individual slopes... not sure how to get these..
Comment: @HIL Those are individual slopes with respect to time (plus error, of course), not with respect to HCA, and they're not being modeled, since you have no random slope for time.
Answer: Your current model only specifies a random intercept, so you're correct that every subject has the same slope in this model (fixed effect of HCA). Looking at your spaghettiplot it does seem like slopes might be different for each individual.
You could try to model a random slope to see if this provides a better fit to your data.
model2 <- lme(HCA ~ time, random=~ 1+ time|subject, data=mydata)

After fitting this model you can compare model fit with anova(model, model2). To get the random slope for each individual you can look at the second column of ranef(model2) (the first being the random intercept). The summary function will give you some more information on the random slope distribution. Under random effects you can find the slope-intercept correlation and the standard deviation of your random effects. Hope this helps.

mixed models: extract slopes


blogs/books for xcode 4 with just cocoa programming.. no touch

Question: Been looking for a book or a nice blog to jump into coding a gui app for mac os x. Everything thing I find for using xcode 4 is just for iphone dev. Anyone have any books or blogs or video tutorials I can take a look at?
thanks!
Comment: possible duplicate of [XCode 4 Tutorials for Cocoa OS X Applications](http://stackoverflow.com/questions/6215942/xcode-4-tutorials-for-cocoa-os-x-applications)
Answer: This is a good book that focuses on cocoa programming. The updatet version will be published in November and should also include examples in Xcode 4

Cocoa Programming for Mac OS X 3rd edition (2008)
Cocoa
Programming for Mac OS X 4th edition (Nov 2011)
Comment: I took a look at the 3rd addition but as soon as it gets into cocoa seems to focus on ios dev but thanks I'll keep an eye out for the 4th edition


One object is not moving

Question: I am very new to pygame and python, and recently started creating the following for the school project but the problem is that, the object apple is not moving or dropping down, but everything else works fine! Please help me out! My complete code is as follows:
import sys, pygame, random
from menu_lib import *
from credit import credit

class Player(pygame.sprite.Sprite):
    def __init__(self, *groups):
        super(Player, self).__init__(*groups)
        self.image = pygame.image.load('catcher_left.png')
        self.rect = pygame.rect.Rect((320,240), self.image.get_size())
        self.rect.bottom = 452
        self.rect.left = 320

    def update(self):
        key = pygame.key.get_pressed()
        if key[pygame.K_LEFT]:
            self.rect.x -= 10
            self.image = pygame.image.load('catcher_left.png')
            pygame.display.flip()
        if key[pygame.K_RIGHT]:
            self.rect.x += 10
            self.image = pygame.image.load('catcher_right.png')
            pygame.display.flip()
        if self.rect.left < 0:
            self.rect.left = 0
        elif self.rect.right > 640:
            self.rect.right = 640
        #if self.rect.colliderect(Apple.rect):
            #print "Collided!"

class Apple(object):
    def __init__(self):
        self.image = pygame.image.load('apple.png')
        self.rect=pygame.rect.Rect((32,37), self.image.get_size())
        self.rect.top=0
        self.rect.left=0

    def draw(self, surface):
        surface.blit(self.image, (self.rect.top, self.rect.left))

    def update(self):
        if self.rect.top==0:
            self.rect.top+=10
            self.rect.left=random.randint(10, 630)
        elif self.rect.top!=0 and self.rect.top>480:
            self.rect.top+=10
        elif self.rect.top>=480:
            self.rect.top=0

class Game(object):
    def main(self, screen):
        clock = pygame.time.Clock()
        background = pygame.image.load('background.jpg')
        background = pygame.transform.scale(background, (640, 480))
        apple_count=0
        sprites = pygame.sprite.Group()
        self.player = Player(sprites)
        apple = Apple()

        while 1:
            clock.tick(30)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    screen.fill((0,0,0))
                    pygame.display.flip()
                    Mainmenu()
                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    screen.fill((0,0,0))
                    pygame.display.flip()
                    Mainmenu()

            sprites.update()
            apple.update()
            screen.fill((200, 200, 200))
            screen.blit(background, (0, 0))
            sprites.draw(screen)
            apple.draw(screen)
            score_txt='Score: '+str(apple_count)
            label = myfont.render(score_txt, 1, (255,0,0))
            screen.blit(label, (500, 10))
            pygame.display.flip()

def Mainmenu():
    screen = pygame.display.set_mode((640, 480))
    menu = cMenu(50, 50, 20, 5, 'vertical', 300, screen,
           [('Start Game', 1, None),
            ('Credits',    2, None),
            ('Exit',       3, None)])

    menu.set_center(True, True)

    menu.set_alignment('center', 'center')

    state = 0
    prev_state = 1

    rect_list = []

    pygame.event.set_blocked(pygame.MOUSEMOTION)

    while 1:
        key = pygame.key.get_pressed()
        if key[pygame.K_ESCAPE]:
            pygame.quit()
            sys.exit()

        if prev_state != state:
         pygame.event.post(pygame.event.Event(EVENT_CHANGE_STATE, key = 0))
         prev_state = state

        e = pygame.event.wait()

        if e.type == pygame.KEYDOWN or e.type == EVENT_CHANGE_STATE:
         if state == 0:
            rect_list, state = menu.update(e, state)
         elif state == 1:
             screen = pygame.display.set_mode((640, 480))
             Game().main(screen)
         elif state == 2:
             credits()
         else:
            print 'Exit!'
            pygame.quit()
            sys.exit()

        if e.type == pygame.QUIT:
         pygame.quit()
         sys.exit()

        pygame.display.update(rect_list)

def credits():
    clock = pygame.time.Clock()
    screen=pygame.display.set_mode((640,480))
    screen.fill((0, 0, 0))
    text = "Credits \n _ _ _ _ _ _ _ _ _ _ _ _ \n\n\n Designer \n Creator \n\n Manikiran P"
    color = 0xa0a0a000
    credit(text,myfont,color)
    Mainmenu()
    while 1:
        clock.tick(30)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                screen.fill((0,0,0))
                pygame.display.flip()
                Mainmenu()
            if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                screen.fill((0,0,0))
                pygame.display.flip()
                Mainmenu()

        pygame.display.flip()

if __name__ == '__main__':
    pygame.init()
    screen=pygame.display.set_mode((640,480))
    pygame.display.set_caption("Evenure - (c) Manikiran")
    myfont = pygame.font.Font("captureit.ttf", 20)
    Mainmenu()

Comment: `Apple.update` only does anything if `self.rect.top` is exactly 0 or at least 480. Is that what you want? Perhaps you got a comparison flipped.
Comment: The same code worked when i took apple_x and apple_y without having apple as sprite, so why now?
Comment: I did not understand what you are trying to say! And i am very new to Pygame!
Comment: @user3699054 this is nothing to do with `pygame`; look at `Apple.update` and think about what path you take through the function if `self.rect.top == 10`.
Comment: Reread the `update` method in the `Apple` class, paying close attention to which way the `>` signs are pointing.
Comment: My guess would be you screwed up the change, perhaps by mistyping something.
Comment: Thank you very much everyone! I screwed up exactly in the if statement. Error: self.rect.top>480; Correction: self.rect.top<480;
Answer: You have miss-typed in the elif statement:
elif self.rect.top!=0 and self.rect.top>480:

Correction:
elif self.rect.top!=0 and self.rect.top<480:

Comment: Thank you very much, and i cannot vote up because i don't have enough reputation!

One object is not moving


Intuitive/geometric way of thinking about effective divisors?

Question:
What is the motivation/intuition/geometric way of thinking about an effective divisor?

I know that a divisor is effective if all its coefficients are non-negative. We write $D \ge 0$ for effective divisors. We can extend this in the obvious manner to get a partial ordering of divisors.
I would consider any explanation that introduces the concepts of Weil and/or Cartier divisors to be obfuscating the underlying intuition of the definition in more formalism...
Comment: For example, consider a Riemann surface (which is a smooth complex curve). Then a Weil divisor is just a finite sum of points, with sign. The effective divisors are those that you can actually *see* on your curve.
Comment: Actually, if you think about a divisor as a Weil divisor (which you cannot always do, though), you should see that an effective divisor is just a union of irreducible varieties of codimension $1$ (counted with the respective multiplicities). This is actually why they are called *effective*.
Comment: It might help get answers useful to you specifically if you elaborate how you think of divisors.
Comment: Restricting ourselves to the factorial case, where there is no Weil/Cartier distinction: effective divisors are cut out by regular sections of line bundles, while general divisors are cut out by rational sections. Also, I should maybe mention that something clicked in my head when I noticed that the Spanish term for cash is "dinero efectivo."
Comment: I'm almost certain that "Matt E" has some answers along these lines on this site. A little too lazy to dig them up right now myself though...
Answer: An effective divisor is just a formal linear combination of codimension one subvarieties of some scheme. The best way to understand that statement is with an example. Consider a quartic surface
$$
X = \textbf{Proj}\left( \frac{\mathbb{C}[x,y,z,w]}{(x^4 + y^4 + z^4 + w^4)}\right)
$$
Notice if we take any homogeneous polynomial in the quotient ring defining $X$, we get a codimension one subscheme. Notice I stated subscheme and not subvariety here, this is intentional. If we consider the divisor $D$ of $X$ as a subscheme given by
$$
\textbf{Proj}\left( \frac{\mathbb{C}[x,y,z,w]}{(x^4 + y^4 + z^4 + w^4, x^4(2x^2 + 3x)(x^2 + y^2)^2)}\right)
$$
then we can write it as
$$
D = 4[x] + [2x^2 + 3x] + 2[x^2 + y^2]
$$
So we've kept the topological information inside the brackets and moved the scheme theoretic information outside of the brackets and turned it into the coefficients of our divisor. If you really wanted, you could write down $X_i$'s as proj's of these graded rings, but I leave this as an exercise for the reader.


Functions not showing up in Azure Function App

Question: I am migrating my functions to V2 from V1 which were running previously. I removed the V1 init.py and function.json files in favor of the V2 function_app.py however when I pushed to github the functions will not longer show up in my functions app despite GH deploying successfully. The functions are on timer triggers and I suspect that it has something to do with the configuration of the host file. I have tried many configurations without any progress. Below are the details of my app and host file. The file folder structure is as follows:
(python_file(n) are files referenced in the function_app.py file)
- monitoring_weekly (folder)
   - function_app.py
   - python_file1.py
   - python_file2.py
   - python_file3.py
   - python_file4.py
   - python_file5.py
- host.json
- local.settings.json

host file
{
    "version": "2.0",
    "extensionBundle": {
      "id": "Microsoft.Azure.Functions.ExtensionBundle",
      "version": "[4.0.0, 5.0.0)"
    },
    "extensions": {
      "timer": {
        "maxConcurrentExecutions": 1,
        "scheduleStatusFilePath": "app_offline.htm"
      }
    }
  }

function app file
import azure.functions as func
import os
import datetime
import logging
import sys
import importlib.util

# Azure function timer. To alter timer cadence see function.json file
app = func.FunctionApp()

@app.function_name(name="monitoring_weekly")
@app.schedule(schedule="0 5 * * MON",
              arg_name="mytimer",
              run_on_startup=False,
              use_monitor=True)

def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    if mytimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function ran at %s', utc_timestamp)

def run_script(script_name):
    script_path = os.path.join(os.path.dirname(__file__), f'{script_name}')

    s = importlib.util.spec_from_file_location(script_name, script_path)
    m = importlib.util.module_from_spec(s)
    sys.modules[s.name] = m
    s.loader.exec_module(m)

def batch_execution(file_list):

    exceptions = [] #store errors

    for file in file_list:
        try:
            run_script(file)
        except Exception as e:
            exceptions.append(e)
        else:
            print('Success')

    return exceptions

#list of scripts running in function, if adding new script add to list.
file_list = ['python_file1.py',
        'python_file2.py',
        'python_file3.py',
        'python_file4.py',
        'python_file5.py']

batch_execution(file_list)

Runtime stack = Python 3.10
Runtime version = 22.214.171.124
Answer: I tried your requirement in my requirement in my environment with your function_app.py code and I could see the function trigger in Azure function app after successful deployment.
My Project Structure:
- _pycache_
- .python_packages
- .vscode
- env
- .funcignore
- .gitignore
- function_app.py
- host.json
- local.settings.json
- python_file1.py
- python_file2.py
- python_file3.py
- python_file4.py
- python_file5.py
- requirements.txt

host.json
{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[4.*, 5.0.0)"
  }
}

Local Response:

Created Python Function app in Azure Portal
Application Settings of my Function App:
Make sure you have the below settings in your function app.

Deployed the Python function project to Function app through GitHub:

Portal:

Deployed function application files:

Response:


How do I call an object in a prepressor only once

Question: In drupal8 is there a way to call a function only once in the theme layer. Can I call this function only once and not on other preprocessor hooks or
am I using the wrong hook?
<code>function template_preprocess(&$vars){
    //-- can I call this function only once and not on other preprocessor hooks
    _updateFieldOnNode('field_metatag_title');
}
</code>
Answer: If you want to call the preprocess hook on nodes only you can use template_preprocess_node().


Combinatoric question about difficult and easy problems

Question: Three children attempted one hundred problems. Each child solved sixty problems, and every problem was
solved by at least one child. A problem is called difficult if it was only solved by one child, and called easy if it
was solved by all three children.
How many more easy problems were there than difficult problems?
I have tried to make progress on this question but I always end up with more difficult questions than easy questions. I got this question from a homework sheet from my teacher but I don't know the textbook it is from. I have been working on this question for over an hour and I have made no progress.
I used the example that:
Child 1 solved questions $1-60$, but not $61-100.$
Child 2 solved $41-100$ but not $1-40$ (so each question is at least solved by one child)
Child 3 solved questions $1-60$ but not $61-100.$
So the questions where all three children solved them was $41-60,$ but questions $61-100$ was where only one person solved it. This means that there is $40$ hard questions and $20$ easy questions, meaning there was $-20$ more easy problems than hard questions.
It is quite an interesting problem because it seems easy to explain, but not easy to solve. I did get an answer of $-20 $ when I tried an example, but it didn't seem right to me because I did not expect the answer to be negative, so I thought my method was wrong. I want to know how to solve these types of questions in the future.
Comment: Please make the subject line relevant as to the *mathematical content* of the post, and not about you.
Comment: Thank you, this is sufficient context in my opinion.
Comment: @lonestudent I had said there was sufficient context even before it got closed. With this added context there is no doubt, so thanks for confirming.
Comment: Thank you all for the help!
Comment: Pigeonhole give us that any pair of student share 20 questions
Answer: Let $d$ be the number of difficult problems, $e$ the number of easy problems and $m$ the medium problems.
There are $100$ problems, so $d+m+e = 100$.
Each child solves $60$ questions, so from the definition of easy, medium, difficult we have $d+2m+3e=3\cdot60=180$.
There are 3 variables and only two equations, so this is not enough to determine each variable precisely. The question however asks only about $d$ and $e$, so let's see what happens when we eliminate the variable $m$. Multiplying the first equation by 2 and subtracting the second equation gives:
$$2(d+m+e)-(d+2m+3e) = 2\cdot100-180$$
which simplifies to
$$d-e = 20$$
There are therefore $20$ fewer easy questions than difficult questions. The original problem statement seems to assume that there are more easy questions than difficult ones, so it is unclear if there is a mistake in the question or whether they expect the answer $-20$.
Comment: It would be -20 because it asks for how many more easy questions than difficult problems. Thank you though anyway!
Answer: Let $A$ be the set of questions solved by the first child, $B$ the set solved by the second child and $C$ for the third.
You are told the following information:  "Three children attempted 100 problems... and every problem was solved by at least one child"  $|A\cup B\cup C|=100$
"Each child solved 60 problems" $|A|=|B|=|C|=60$
We know from inclusion-exclusion that $|A\cup B\cup C|=|A|+|B|+|C|-|A\cap B|-|A\cap C|-|B\cap C|+|A\cap B\cap C|$
Plugging in numbers into inclusion-exclusion we have $100 = 180 - |A\cap B|-|A\cap C|-|B\cap C|+|A\cap B\cap C|$ and so $|A\cap B|+|A\cap C|+|B\cap C| - |A\cap B\cap C| = 80$
What we want to find is the difference of the amount of problems solved by all students versus solved by only a single student.  The amount solved by all is $|A\cap B\cap C|$.  The amount solved only by $A$ would be $|A|-|A\cap B|-|A\cap C|+|A\cap B\cap C|$, seen similar to inclusion-exclusion.  Similarly for $B$ and $C$.
So, the expression you are after in the end is:
$$|A\cap B\cap C|-(A-|A\cap B|-|A\cap C|+|A\cap B\cap C|) - (B-|A\cap B|-|B\cap C|+|A\cap B\cap C|)-(C-|A\cap C|-|B\cap C|+|A\cap B\cap C|)$$
$$=-2|A\cap B\cap C| - |A|-|B|-|C|+2|A\cap B|+2|A\cap C|+2|B\cap C|$$
$$=2(|A\cap B|+|A\cap C|+|B\cap C|-|A\cap B\cap C|) - |A|-|B|-|C|$$
Plugging in numbers, we have then
$$=2(80)-60-60-60 = 160-180=-20$$
That is, there were $20$ more hard questions (solved by only a single student) than there were easy questions (solved by all students)
Note... we don't care (nor can we determine exactly) how many hard questions there were or how many easy questions there were... but despite this we were able to find the difference in size between them.
For instance, perhaps the students all solved the first $40$ problems, but then each solved a different twenty of the remaining, giving a total of $40$ easy questions at $60$ hard questions.  Alternatively, perhaps student $A$ solved problems $1$-$60$, student $B$ solved problems $1$-$20$ and $61$-$100$, and student $C$ solved problems $41$-$100$ for a total of zero easy questions and $20$ hard questions.
Comment: Thank you very much, I understand now, I guess not all answers must be positive.
Answer: Letting $A$, $B$, and $C$ be the sets of questions answered correctly by the each of the three students, the Generalized Inclusion-Exclusion Principle says the following:
The number of questions answered by exactly $0$ students is
$$
\begin{align}
\overset{\substack{\text{answered by none}\\\downarrow}}{\color{#C00}{0}}&=\binom{0}{0}\overbrace{|A\cup B\cup C|}^{N(0)=100}-\binom{1}{0}\overbrace{(|A|+|B|+|C|)}^{N(1)=180}\\
&+\binom{2}{0}\overbrace{(|A\cap B|+|B\cap C|+|C\cap A|)}^{N(2)}-\binom{3}{0}\overbrace{|A\cap B\cap C|}^{N(3)=\text{easy}}
\end{align}
$$
The number of questions answered by exactly $1$ student is
$$
\begin{align}
\color{#090}{\text{difficult}}
&=\binom{1}{1}\overbrace{(|A|+|B|+|C|)}^{N(1)=180}-\binom{2}{1}\overbrace{(|A\cap B|+|B\cap C|+|C\cap A|)}^{N(2)}\\
&+\binom{3}{1}\overbrace{|A\cap B\cap C|}^{N(3)=\text{easy}}
\end{align}
$$
Therefore, cancelling $N(2)$ in both,
$$
\begin{align}
\color{#090}{\text{difficult}}+2\cdot\color{#C00}{0}
&=\color{#090}{(180-2N(2)+3\,\text{easy})}+2\,\color{#C00}{(100-180+N(2)-\text{easy})}\\
&=\text{easy}+20
\end{align}
$$
Thus, there are $20$ more difficult questions than easy questions.


Column Spaces and Subsets

Question:
Let $X = [\vec{v_1} ... \vec{v_k}]$ and $Y = [\vec{u_1} ... \vec{u_k}]$ be $n \times  k$ matrices. Prove that if Span {$\vec{v_1}, ..., \vec{v_k}$} $\subseteq$ Span {$\vec{u_1}, ..., \vec{u_k}$}, then there exists a matrix $A$ such that $X=YA$.

I have a hunch this has something to do with columns spaces but I can't wrap my head around it. Any hints?
Comment: Hint: $v_i\in\mbox{Span}\left\{u_1,...,u_k\right\}$ so there exist $a_{i,1},..., a_{i, k}\in\mathbb{R}$ such that $$v_i=a_{i,1}u_1+...+a_{i, k}u_{k}$$
Answer: Since
$\DeclareMathOperator{Span}{Span}\Span\{v_1,\dotsc,v_k\}\subset\Span\{u_1,\dotsc,u_k\}$,
each $v_i$ is a linear combination of the vectors $\{u_1,\dotsc,u_k\}$. This
means that there are scalars $a_{ij}$ satisfying
\begin{array}{rcrcrcrcrc}
v_1    &=& a_{11}\cdot u_1 &+& a_{21}\cdot u_2 &+& \dotsb &+& a_{k1}\cdot u_k \\
v_2    &=& a_{12}\cdot u_1 &+& a_{22}\cdot u_2 &+& \dotsb &+& a_{k2}\cdot u_k \\
\vdots & & \vdots          & & \vdots          & & \ddots & & \vdots \\
v_k    &=& a_{1k}\cdot u_1 &+& a_{2k}\cdot u_2 &+& \dotsb &+& a_{kk}\cdot u_k \\
\end{array}
Now, let $A=[a_{ij}]$. What is $YA$?

Column Spaces and Subsets


how to set event if cursor is out of an input tag in jquery

Question: im new to jquery and i want to know how to get value of an input tag when a cursor is out/left to that input element/tag. Thank you in advance.
Answer: Use blur event
 $(function() {
    $('input[type="text"]').on('blur' , function() {
        // Your code here
    });
  });

CHECK DEMO
Comment: I know this is old. but is there a way for the JavaScript to know the id of the field that was 'blurred'?
Comment: @theB3RV yes - use ```var the_id = $(this).attr('id');```
Answer: I think the method you're searching is focusout
$(function() {
    $('#input-element').focusout(function() {
        // put your code here!
    });
});

I hope it helps.
Answer: <form>
  <input id="target" type="text" value="Field 1" />
  <input type="text" value="Field 2" />
</form>

  Trigger the handler


Error executing programs/compiling inside IntelliJ, no errors in command line

Question: I recently bought a Macbook Air and it's now running Mountain Lion, but i have some problems running the company's project, the only other person using a mac on work runs Lion on his Macbook Pro and he had no such problems.
As the title says theres no problem compiling the project on the command line, but when i try to compile it inside IntelliJ i get this error
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (requireJS-Optimizer) on project MarfeelTouch: Command execution failed. Cannot run program "node" (in directory "/Users/pedrompg/Documents/Marfeel/MarfeelTouch"): error=2, No such file or directory -> [Help 1]

The problem also happens when i compile it from the command line and try to run the program
Caused by: java.util.concurrent.ExecutionException: java.io.IOException: Cannot run program "phantomjs" (in directory "/Users/pedrompg/Documents/Tenants/vhosts/discoverint"): error=2, No such file or directory
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:232) ~[na:1.6.0_35]
    at java.util.concurrent.FutureTask.get(FutureTask.java:91) ~[na:1.6.0_35]
    at com.marfeel.pressSystem.impl.SectionPressImpl.getAllItemsFromSectionFeeds(SectionPressImpl.java:137) ~[MarfeelPressSystem-1.0.jar:na]
    ... 29 common frames omitted

It seems that i can't run any command line programs from inside the project
This is how we the phantomJS call:
private Process buildProcess() throws IOException {
        Process process;
        String[] invocationCmd = getInvocationCmd();

        if (executionDirectory != null) {
            if (LOG.isDebugEnabled()) {
                LOG.info("Invoking PhantomJS with {} in {}.", Arrays.toString(invocationCmd), executionDirectory);
            }

            process = Runtime.getRuntime().exec(invocationCmd, null,
                    new File(executionDirectory));
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.info("Invoking PhantomJS with {} in {}.", Arrays.toString(invocationCmd));
            }

            process = Runtime.getRuntime().exec(invocationCmd, null);
        }

        return process;
    }

the getInvocationCmd() returns the following array
[phantomjs,--load-images=no,--disk-cache=yes,--max-disk-cache-size=1048576,/Users/pedrompg/Documents/Marfeel/MarfeelHub/target/webapp/WEB-INF/classes/whiteCollar.js,marca/marca.js,http://www.marca.com/]

Don't know if i leave any relevant information
We use Maven, tomcat 7, nodeJS, phantomJS 1.5, nginx 1.2.4, java 1.6.0_35 on the project
Hope someone can help, i'm getting really worried about this, already wasted 2 days trying to solve this problem.
Thanks in advance
Answer: As you are using Mac, it's most likely environment related issue. Please note that on Mac GUI applications do not inherit Terminal environment variables, therefore if you have adjusted PATH variable and command works from the Terminal, it will not work when you try to run from other applications.
See the related questions about this Mac feature. Pay attention to the second link, Mountain Lion has different behavior for environment variables.
The easiest way to verify that it's the case and workaround the problem is to run IntelliJ IDEA from Terminal:
open -a /Applications/IntelliJ\ IDEA\ 11.app/

This way Terminal environment will be passed to IDEA and commands you can run from Terminal will also run from IntelliJ IDEA.
Comment: My god you saved my life!!! i would upvote you if i could, still need +5 rep to do it.


Help changing the order of integration

Question: So I need to change the order of integration. I am giving the following limits, $1 \leq x \leq 9$ and $\sqrt{x} \leq y \leq 4$. I am having no luck solving this one. Any help would be greatly appreciated!
Comment: Have you sketched the region?
Comment: Yah, That was the first thing I did.
Comment: So how big/small does $y$ get on the given region (this gives at least the bounds on $y$)?
Comment: The bounds I found for y would be between 1 and 4.
Comment: Right: And what is the smallest $x$ gets (should be a number)? Finally, note that $\sqrt x = y \iff y^2 = x$, which gives you the right-hand bound.
Comment: so the smallest x gets is also 1, and I originally got the limits for x to be between 1 and y^2. However I still appear to get the wrong answer.
Comment: That's almost the right region, except that the right-hand bound changes at the very top part of the region; try decomposing into the region $\{1 \le x \le y^2, 1 \le y \le 3\}$ together with a rectangle at the top.
Comment: so basically two different integrals one from (1<=x<=y^2, 1<=y<=3} then another from {1<=x<=9, 3<=y<=4}?
Comment: Yes, exactly right.
Comment: Thanks so much. I'll give it a go -- but that sounds right!
Answer:
From the picture it is clear that if you want to change the order of integration you should integrate over the region D. Region D can be divided in two parts:

For D1 we have: integrate from $x=1$ to $x=y^2$, and $y\in(1,3)$, and for D2 we integrate from $x=1$ to $x=9$ and $y\in(3,4)$.
Calculus for D1:
$$ D_1=\int^3_1 dy \int^1_{y^2} dx $$
and for D2:
$$ D_2=\int^4_3dy\int^1_9 dx $$
Answer: Or you could simply do the following:
$$
\text {Area} = 9\times 4 -\int_1^9 \sqrt x dx
$$

Help changing the order of integration


retornar para uma Nav tab especifica

Question: Tenho um controller, que depois que salva, ele precisa ser derecionado para uma tab especifica, então eu uso como reterno os seguintes parametros.
return Redirect(Url.Action("Editar/" + resultado.Id, "ClienteEmpresa") + "#nav-profile");

Com isso eu consigo, pegar na url, exatamente, a o que eu quero, porém não sei como faço para que a Nav-Tab, #nav-profile,  seja a que abra, pois ainda estás a abrir, a primeira Tab.
Minha Url fica dessa forma ao abrir
http://localhost:51029/ClienteEmpresa/Editar/14#nav-profile

Porém a tab que vem aberta é a #nav-home-tab
Esse aqui é meu HTML.

            Empresa
            Contador
            Bloqueios
            Controle de Vencimentos
            Controle De Cobranças

            @Html.Action("_tblBloqueios", new { Id = Model.Id })

            @Html.Action("_tableVencimentos", new { Id = Model.Id })

            @Html.Action("_tableControleDeCobranca", new { Id = Model.Id })

Como eu posso fazer isso? não manjo muito de Js e nem Jquery. então toda e qualquer dica será bem vinda.
Answer: Para isso será necessário verificar a presença de um # na url.
function afterLoad(){
    var url = location.href;
    var splittedUrl = url.split("#");
    if (splittedUrl.length >= 2) {
        var goToTabAriaControls = splittedUrl[splittedUrl.length-1];
        $('a[aria-controls="' + goToTabAriaControls + '"]').click();
    } else {
        //implemente aqui o código caso não seja encontrada a presença de um #
    }
}

Você pode executar o código acima sempre que houver o carrega da página, por exemplo, no final da View você poderia colocar esse código e executá-lo afterLoad() logo em seguida, ou colocar esse código num arquivo javascript previamente inserido e apenas executar o método, sendo que a execução ainda estaria na View.
Nessa resposta é verificada a presença do # na url e é utilizada última parte encontrada para realizar a ação do click.
goToTabAriaControls sempre será a string que se encontra após o último # encontrado.
Comment: subarashi!, cara, de todos que eu fucei na net, (e olha que eu to com mais de 5 hora de pesquisa, o seu script/explicação, foi a unica que funcionou. muito obrigado!
Comment: É necessário prestar atenção que NÃO existe espaçamento entre o `a` e o `[`
Comment: Por nada! Fico feliz em ter ajudado
Comment: Não querendo abusar, mas aproveitando o gancho, teria como além de eu direcionar para a tab, ainda abrir uma modal? pq a verdade é que o objetivo principal é essa, é uma regra. (depois que eu clicar em salvar para adicionar o cliente, obrigatoriamente, eu tenho que mandar o usuário para a aba #nav-profile, nessa aba eu tenho um botão que abre um modal para adicionar um contador, mas a verdade, é que era para abrir na aba, e ja abrir a modal.
Comment: Após executar o `.click()` verifique qual o valor de `goToTabAriaControls` se for o `nav-profile` abra a modal
Comment: `if (goToTabAriaControls == 'nav-profile') {
                    $("#addContador").load("_addContador?id=" + id, function () {
                        $("#addContador").modal();
                    });

                }` assim?
Comment: Creio que seria isso mesmo
Comment: Não deu muito certo, mas vou continuar pesquisando aqui hehe, já é meio caminho andado!!

retornar para uma Nav tab especifica


How to come up with SELECT queries?

Question: I have a query like this:
SELECT DISTINCT field_name FROM table_name

I tried:
\Drupal::database()->select('table_name', 't');
$query->fields('t', ['field_name']);
$result = $query->execute();

But since Connection::select expects base table this did not work.
How am I supposed to execute it? I used entityQuery previously. I could not find a way to filter out the unique values of only one field using entityQuery.
Note: I went through this example which is basically using Connection::select. But Connection::select requires to provide the base table. In entityQuery (https://api.drupal.org/api/drupal/core!lib!Drupal.php/function/Drupal%3A%3AentityQuery/8.2.x) I did not have to provide the base table data.
Comment: Hey hey, please add what you tried to give us a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). Did you already have a look at [Connection::select](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Database%21Connection.php/function/Connection%3A%3Aselect/8.2.x)?
Comment: "But since Connection::select expects base table this did not work." - what?
Comment: https://www.codeexpertz.com/blog/drupal/drupal-8-how-use-select-query-syntax-and-example
I went through this example which is basically using Connection::select. But Connection::select requires to provide the base table. In entityQuery (https://api.drupal.org/api/drupal/core!lib!Drupal.php/function/Drupal%3A%3AentityQuery/8.2.x) I did not have to provide the base table data.
Comment: Forget that random blog post. What code are you actually trying?
Answer: The Problem is, that you need to fetch the results from your execution.
Your code should look like this:
$query = \Drupal::database()->select('table_name', 't');
$query->fields('t', ['field_name']);
$query->distinct();
$result = $query->execute()->fetchAll();

That does the same as the query you stated above.
Alternatively you can fetch your results with a complete custom query:
$result = Drupal::database()->query('SELECT DISTINCT field_name FROM table_name')->fetchAll();

Notice here that you dont have to execute() your query in this case. Instead you can directly fetch your results.
You will still need the execute() for insert/update or similiar queries.

How to come up with SELECT queries?


Views alter changes next row instead of current one

Question: I have a view that is showing event content types. Users can apply for these events. I am using a hook to alter the 'view applicants' so I can show how many applications already excist.
The following code works and shows the values but is somehow skipping the first row and printing all the values one row later.
function frontview_general_preprocess_views_view_field(&$vars) {

  $view = $vars['view'];

  if(isset($vars['view']) && ($vars['view']->id() == 'evenementen') && ($vars['view']->current_display == 'accreditaties_overzicht')) {

    if($vars['field']->options['id'] ==='nothing_1') {

      // Getting the values
      $accreditaties = $vars['row']->_entity->get('field_accreditaties');
      $accreditaties = $accreditaties->getValue('list');
      $amount = count($accreditaties);

      $vars['field']->options['alter']['text'] = 'View Applicants ('. $amount .')';
    }

  }
}

This is my current output, the first row still shows the normal values without the hook but the first row values are printed on the second row, second row on third row, ...

This is the output that I need

I'm not sure why it's doing that, when I dd() my code it looks fine.
Thanks in advance!
*Drupal 8
Answer: Got it to work with the following code. I needed to output the changed value in the $vars['output']. This would override the url in the nothing_1 field so I then had to create a new link and use that to alter the original value.
/**
 * Implements hook_preprocess_views_view_field().
 */
function frontview_general_preprocess_views_view_field(&$vars) {

  $view = $vars['view'];

  if(isset($vars['view']) && ($vars['view']->id() == 'evenementen') && ($vars['view']->current_display == 'accreditaties_overzicht')) {

    if($vars['field']->options['id'] ==='nothing_1') {
      $accreditaties = $vars['row']->_entity->get('field_accreditaties');
      $accreditaties = $accreditaties->getValue('list');
      $amount = count($accreditaties);

      // $vars['field']->options['alter']['text'] = 'View Applicants ('. $amount .')';

      $nid_value = $vars['row']->nid;
      $url = Link::fromTextAndUrl(t('View Applicants ('. $amount .')'), Url::fromRoute('view.evenementen.page_2', ['arg_0' => $nid_value]))->toString();

      $vars['output'] = $url;
    }
  }
}


Debugging JavaScript tests

Question: Recently started working on some JavaScript tests by extending JavascriptTestBase in drupal 8.2 and the biggest problem currently is debugging.
Phantomjs is installed and i have no problem running JavaScript tests. Xdebug is installed too.
<code>$this->getSession()->getScreenshot();
</code>
is not returning any output, maybe some additional configuration is needed?
<code>file_put_contents('/Users/me/debug.html', $this->getSession()->getDriver()->getHtml('/html/body'))
</code>
is giving me a half broken page.
I would really appreciate if someone can share how they debug these tests and how they set it up, some useful links. The biggest help would be to be able to take screenshots but any info on this topic is welcome.
Comment: `getScreenshot` is a Mink framework method and the API documentation is available either in Mink or on api.drupal.org. The return value is a `string screenshot of MIME type image/* depending on driver (e.g., image/png, image/jpeg)`.
Comment: thanks. thought that this should work, looks like it might be a permission problem, on another machine both commands are working fine.
Answer: I think that you are looking for JavascriptTestBase::createScreenshot() https://api.drupal.org/api/drupal/core!tests!Drupal!FunctionalJavascriptTests!JavascriptTestBase.php/function/JavascriptTestBase%3A%3AcreateScreenshot/8.2.x
See the following snippet from a core test:
<code>  /**
   * Tests creating screenshots.
   */
  public function testCreateScreenshot() {
    $this->drupalGet('<front>');
    $this->createScreenshot('public://screenshot.jpg');
    $this->assertFileExists('public://screenshot.jpg');
  }
</code>

Debugging JavaScript tests


Most Trophies won by a Player in T20 leagues

Question: There are so many T20 leagues played all over the World. Many T20 specialist Players like Chris Gayle, Darren Sammy, Russel, Thisara Perera, etc play almost many of them.
Which player has a record of winning most Trophies in T20 Leagues? (The Leagues I am interested about are IPL, BPL, BBL, MSS, PSL,Caribbean League)
Comment: I must say, it's *andre russell*.
Answer: The player who has a record of winning most trophies in T20 Leagues is Kieron Pollard – 11 trophies, you can get a list of other players in this website (https://www.sportskeeda.com/cricket/5-most-successful-t20-cricketers-ever/5)
 Website shows Kieron Pollard – 10 trophies because they haven't added IPL 2017.


Filtro em um projeto que consiste em vários projetos e que são executados em diferentes portas (Spring Boot)

Question: Gostaria de saber se há como (e como posso) desenvolver uma aplicação que é constituída de várias outras aplicações e, para acessar essas outras partes da aplicação eu preciso ter feito o login.
Acredito que esse conceito está ligado aos microserviços ou estou enganado? É possível desenvolver esse interceptador que irá fazer a verificação dos logins nos projetos que rodam nessas portas?
Comment: Sim tem como fazer, a forma mais primitiva é com um HttpClint, mas existem implementações que abstraem-se um pouco isso.
Comment: Pode comentar um pouco mais? ou mandar algum link de referência?
Comment: Bem-vindo ao [pt.so]! Só para conceituar as coisas, o que você está descrevendo chama-se microservices, vários serviços pequenos cooperando, com single-sign-on (ou SSO) - um login vale para todas as aplicações. Infelizmente não tenho tempo para elaborar uma resposta completa sobre o assunto, mas um começo seria você ter uma aplicação responsável pelo login que gera um token para o cliente que é então validado quando a requisição vai para outros serviços.
Answer: Dividir uma aplicação em outras menores faz parte do conceito de microserviços, mas não é tudo. Existem várias definições, mas uma importante é que cada microserviço deve refletir um aspecto relevante do sistema.
A comunicação entre os serviços geralmente é feita usando HTTP e algum protocolo de mais alto nível. Pode ser um ad hoc com REST e JSON mesmo ou algo mais elaborado como o Google Protobuf.
Quando à autenticação, você pode deve estudar oAuth e procurar alguma implementação de Single Sign-On (SSO). Há várias formas de implementar isto e existem alguns frameworks prontos (me lembro do Josso, mas faz tempo, deve ter coisa melhor hoje).
O funcionamento básico é mais ou menos assim:

Os usuários se autenticam no servidor de SSO e retorna um token para o cliente/usuário/browser.
A cada requisição para outro serviço, o cliente inclui o token.
Os outros serviços validam com o servidor de SSO se o token é realmente válido.

Mas, lembrando, tudo isso pode ser abstraído. Existem inclusive alguns serviços disponíveis na internet que podem ser usados caso você queira mais produtividade para se focar no negócio e não perder tempo criando sua própria solução, tal como o Auth0.
Uma técnica comum em microserviços é criar um serviço "façade" contendo toda a API externa da sua aplicação. Ela pode ser responsável por autenticar e autorizar cada requisição. Assim você não precisa expor cada serviço na internet em portas diferentes e a implementação dos microserviços fica transparente para os clientes.