Which (dis)similarity index to choose for cluster analysis?

Question: I have data that refer to the number of occurrences of specific variable in samples:
       V1  V2  V3 ...
sample1 0   2   1
sample2 7   1   0
sample3 1   4   1
....

The data refers to the occurrence of genes(V1...) in different genomes (sample1..).
I want to perform a cluster analysis combined with an heat map.
I used the function heatmap.2 in the gplot package in R.
I used Euclidian distances for calculating the distance among the samples.
The clustering algorithm is the default one for the function hclust in R (hclust(d, method = "complete", members = NULL)).
However, I am not completely sure it is the right method.
Any suggestion on how to choose the right method to calculate the distances among my samples?
EDIT
The aim is to describe the distribution of the variables (genes) among the samples (genome), and cluster the samples(genomes) according with the values that each variables assume (meaning, how many specific genes are present)
Comment: Great, I don't have time to add an answer right now but the question is now much more answerable than before, not to mention more helpful to future readers
Comment: You need to describe your variables _and_ state which clustering method you're using. Choice of distance or dissimilarity index tends to be application-specific.
Comment: @ssdecontrol Thanks for your comment. Please, see my edit.
Comment: Also, some info that might help you [here](http://stats.stackexchange.com/q/81539/36229), [here](http://www.stat-athens.aueb.gr/~karlis/18th%20IWSM-Leuven%20Karlis.pdf), and maybe [here](http://www.theanalysisfactor.com/count-data-considered-continuous/)
Comment: Might you be looking for something like this: http://www.immunityageing.com/content/5/1/5/figure/F2?highres=y
Answer: How to choose? the right method?
There may be no single right method. If there is, it's whatever best represents similarity /dissimilarity in the domain. Does the euclidian distance actually reflect a biologically meaningful measure of how similar or different these genomes are? I'm no biologist, but perhaps you should compare how many genes are in common... maybe how many are in common out of the total? a jaccard coefficient perhaps?
If none stands out, try two or three and see if they give very different results.
If they give the same results, then it's not important which one you use. If they give radically different results then you should worry about it, because it's really important, and figure it out with respect to genetics, not stats/machine learning. If you get pretty much the same results with a few differences, then you should draw conclusions primarily from the results that are consistent across different similarity/distance metrics, and be more cautious about the others.
Comment: Thanks for your answer. Using Jaccard and Euclidian I get different clustering. Do you know any reference I could read to understand when is better to use one and when to use the other?
Comment: Sorry, I don't. I could google it, but you can just as well. Also, I think it's an issue that a biologist should answer. Take a sample, and run it through a few experts (you included, perhaps) and let them decide following their intuition, which data points they think are similar. Then take their expert decisions and see which distance metric they match best. The purpose of clustering is to do automatically and on a large scale what a human could only do on a small scale. But on a sample, both should match.


Replace NA with previous value for certain column in R

Question: I want to convert NAs to a certain value, based on the given variable value for that specific ID. Sample query: df1 ---> df2
df1 = data.frame(ID=c(1,1, 1, 1, 2,2,2,2,3,3,3,3),WHR=c(0.8,NA, NA, NA,1.0, NA, NA,NA,1.1, NA, NA, NA))

df2=data.frame(ID=c(1,1, 1, 1, 2,2,2,2,3,3,3,3),WHR=c(0.8,0.8, 0.8, 0.8,1.0, 1.0,1.0,1.0, 1.1, 1.1,1.1,1.1))`

What I tried
R fill in NA with previous row value with condition:
library(xts)
df1[,WHR:=na.locf("WHR", fromlast=TRUE, by = ID)`

Got error:
could not find function ":=";

I used this code because I have hundreds of ID values and I would like an automatic code that changes NAs in a particular column based on ID.
How can I convert df1 to df2? (pls explain your code as well, so it may help other beginner users). Thank you!
Comment: Possible duplicate of [Replacing NAs with latest non-NA value](http://stackoverflow.com/questions/7735647/replacing-nas-with-latest-non-na-value)
Comment: You need to install and load the `data.table` package and convert your data to a `data.table` object (for example using `setDT(df1)`) for the code to work
Comment: sorry, did not work @mtoto
Comment: @docendodiscimus Loaded `data.table` and ran `df1[,WHR:=na.locf(WHR, fromlast=TRUE), by = ID]`, now getting another error: unused argument (by = ID)
Comment: Did you run `setDT(df1)`?
Comment: Thanks, @docendo! Query has been solved by this code: `df2$WHR.Comp = ave(df1$WHR, cumsum(!is.na(df1$WHR)), FUN=function(x) x[1])`
Comment: This code: `df2$WHR.Comp = ave(df1$WHR, cumsum(!is.na(df1$WHR)), FUN=function(x) x[1])` simply returns the values for ID 1 -3, again and again, for IDs 4 to 100.. not sure what is happening?
Answer: Using base R you can broadcast the first non-NA using the cumsum of their locations and the ave function:
df2$WHR.Comp = ave(df1$WHR, cumsum(!is.na(df1$WHR)), FUN=function(x) x[1])

> df2
   ID WHR WHR.Comp
1   1 0.8      0.8
2   1 0.8      0.8
3   1 0.8      0.8
4   1 0.8      0.8
5   2 1.0      1.0
6   2 1.0      1.0
7   2 1.0      1.0
8   2 1.0      1.0
9   3 1.1      1.1
10  3 1.1      1.1
11  3 1.1      1.1
12  3 1.1      1.1

Comment: @Zelazyn7 - I noticed this code works for first three IDs only, it replicates 0.8, 1.0, 1.1 for further IDs, which it should not. Why is it so for the next IDs?
Comment: are you referencing the correct data.frames? If it is repeating those values, my guess is that you are still referencing the test data.frame and the results are being recycled.
Comment: there was an issue with column name, e.g., `df2$WHR.Comp` vs `df2$`WHR.Comp``, if the column name had single inverted commas, then code did not work
Answer: You could construct a mapping for the missing values:
idx <- !is.na(df1[,"WHR"])
map <- setNames(df1[idx,"WHR"], df1[idx,"ID"])

and then apply this map to the NA values
df2[!idx, "WHR2"] <- map[df2[!idx, "ID"]]

Comment: Thanks, Karsten! @Zelazyn7 solved the query. Your code changes the values of WHR  (to 0.8 1.0 1.0 1.0 1.0 2.0 2.0 2.0 1.1 3.0 3.0 3.0). Never mind, but wonder why?
Comment: Sorry, wrong argument order to `setNames`. Fixed it. Thanks for trying it out!
Comment: No issues. This code: `df2$WHR.Comp = ave(df1$WHR, cumsum(!is.na(df1$WHR)), FUN=function(x) x[1])` worked but it simply returns the values of ID 1 -3, again and again, for IDs 4 to 100.. not sure what is happening?
Answer: mean by ID:
for (i in unique(df1$ID)) df1[df1$ID==i & is.na(df1[,'WHR']),'WHR'] <- mean(df2[df2$ID==i,'WHR'])


Problema ao enviar e-mail (com html) usando PHP

Question: Olá,
Eu tenho um código em html para enviar no e-mail que no modo normal do html ele apreseta normalmente mas quando envio o email, ao vizualizar no gmail, chega todo errado o html.
Já no Thunderbird o email chega com o html corretamente.
Alguem tem uma solucao para mim?
Meu código html é:
function html() {
    $r = '

        '.template("empresa").'

        '.template("descricao_item_01").'

        DESCRICAO ---> 01
        PREÇO ----> R$ '.template("preco_item_01").'

        '.template("descricao_item_02").'

        DESCRICAO ---> 02
        PREÇO ----> R$ '.template("preco_item_02").'

        '.template("descricao_item_03").'

        DESCRICAO ---> 03
        PREÇO ----> R$ '.template("preco_item_03").'

        '.template("descricao_item_04").'

        DESCRICAO ---> 04
        PREÇO ----> R$ '.template("preco_item_04").'

        '.template("descricao_item_05").'

        DESCRICAO ---> 05
        PREÇO ----> R$ '.template("preco_item_05").'

        '.template("descricao_item_06").'

        DESCRICAO ---> 06
        PREÇO ----> R$ '.template("preco_item_06").'

'.template("cnpj").' - '.template("telefone").'

    ';
    return $r;
}

O código de enviar o html é:
function sendmail() {
    if(!isset($_POST[Submit])) die("Não foi recebido nenhum parâmetro");
    /* Medida preventiva para evitar que outros domínios sejam remetente da sua mensagem. */
    if (eregi('tempsite.ws$|locaweb.com.br$|hospedagemdesites.ws$|websiteseguro.com$', $_SERVER[HTTP_HOST])) {
            $emailsender=trim($_POST['emailremetente']);
    } else {
            $emailsender = "noreply@" . $_SERVER[HTTP_HOST];
    }

    /* Verifica qual é o sistema operacional do servidor para ajustar o cabeçalho de forma correta. Não alterar */
    if(PHP_OS == "Linux") $quebra_linha = "\n";
    elseif(PHP_OS == "WINNT") $quebra_linha = "\r\n";
    else die("Incompatibilidade com Sistema Operacional");

    // Passando os dados obtidos pelo formulário para as variáveis abaixo
    $nomeremetente     = $_POST['nomeremetente'];
    $emailremetente    = trim($_POST['emailremetente']);
    $assunto           = $_POST['assunto'];

    $array_emaildestinatario = array();
    $array_emaildestinatario = explode("\n",trim($_POST['emaildestinatario'])); // separa os emails pelas vírgulas em uma array
    foreach($array_emaildestinatario as $elemento) {
        //pra cada email envia
        /* Montando a mensagem a ser enviada no corpo do e-mail. */
        $mensagemHTML = html();

        /* Montando o cabeçalho da mensagem */
        $headers = "MIME-Version: 1.1".$quebra_linha;
        $headers .= "Content-type: text/html; charset=iso-8859-1".$quebra_linha;
        // Perceba que a linha acima contém "text/html", sem essa linha, a mensagem não chegará formatada.
        $headers .= "From: ".$emailsender.$quebra_linha;
        $headers .= "Return-Path: " . $emailsender . $quebra_linha;

        $headers .= "Reply-To: ".$emailremetente.$quebra_linha;
        // Note que o e-mail do remetente será usado no campo Reply-To (Responder Para)

        /* Enviando a mensagem */
        mail($elemento, $assunto, $mensagemHTML, $headers, "-r". $emailsender);
    }
    /* Mostrando na tela as informações enviadas por e-mail */
    ?>

            Enviando <?php echo count($array_emaildestinatario); ?> e-mails, aguarde um instante...

            De:<?php echo $emailsender; ?>

        <?php
        foreach($array_emaildestinatario as $elemento) {
            echo "
$elemento
";
        }
        ?>

<?php
}

ELE CHEGA ERRADO NO GMAIL ASSIM: (MEU PROBLEMA ESTA AQUI NO GMAIL)

E NO THUNDERBIRD CHEGA CORRETO ASSIM:

Alguem tem uma solucao?
Comment: A solução é usar HTML 4, , e praticamente nada de CSS para email.
Comment: Verdade, nada de CSS e se tiver que usar, coloca inline, dentro do código html e não num arquivo a parte. Sempre use para as imagens url's absolutas.
Comment: Eu tive que passar para CSS INLINE, retirando todo meu HEAD para funcionar. Nesse caso a solução que consegui neste momento é melhor que usar HTML 4 Table.

Mas agora eu queria é carregar um css externo em vez de inline, pelo jeito externo ou no proprio head como estava nao dá entao :/ ?

Valeu pela ajuda pessoal, Marcos Vinicius..., era isso mesmo, INLINE resolveu a primeira viagem. Uma pena ter que usar esse monte de codigo css no meio do meu html.
Comment: Vou fazer o template e depois dele pronto passar pra inline, se eu faço tudo inline fico mais doido que sou apagando e reescrevendo.
Valeu ai
Answer: No ThunderBird pode ser conflito de codificação com seu PHP (verifique a codificação do seu php se também é 8859-1).
No Gmail pode ser questão de estar usando css inline na head, segundo esse artigo
(aqui) ele lista nos itens que não devem ser feitos.
Answer: Você não precisa usar esses testes de quebra linha. A constante PHP_EOL já faz isso.
Você não pode usar estilos externos nem dentro da tag style.
Use uma ferramenta de CSS inliner para fazer isso para você, fica mais fácil.
http://templates.mailchimp.com/resources/inline-css/
Você não deve usar codificação ISO-8859-1 como o colega acima falou, e sim Unicode UTF-8 SEMPRE, caso não queira ter problemas com acentuação e caracteres estranhos.


How to reset UID number back to default order?

Question: I have a Drupal 7 website with only UID1 user which is root admin. I deleted previous users. Now if I create a new user, it gets UID of e.g. 74.
It should be UID 2 and so on.
Anyway, I had same problem with resetting nodes back to 1. And I did it by following this answer, but it worked only if you have no nodes. Now, I have at least one user and I don't know if that query would work for 'users'.
Comment: It works for any database table. BTW, Drupal considers user with UID 1 is always the root admin. No matter if you have recreated it.
Comment: So I ran the ALTER TABLE `users` AUTO_INCREMENT = 1; but didn't work.
Comment: Ah...you are right. Sorry for giving wrong information. I've found the possible reason [here](https://api.drupal.org/comment/58438#comment-58438).
Answer: I've found the discussion thread about the issue about why users table is not auto-increment and creating a new sequences table for that purpose.

This is NOT a full sequences API not at all but it's enough for some uses. actions_aid and simpletest_test_id , that's duplicate code not to mention it belongs to the DB layer. This patch solves that. Further work: we can reuse this for users.uid as that should not be serial because of the known problems. The postgresql folks can create a version which uses proper sequences. The mysql folks can support more than the single serial mysql allows -- like a timestamp and a serial. Sorry for the hasty writeup.

In your case, you just update the value in sequences table to 1.
AND
You need to run ALTER TABLE sequences AUTO_INCREMENT = 1 as well.
Comment: I did that. But it resets back to 83 when creating new user.
Comment: You need to alter `sequences` table to start from 1. Please check the updated answer.


Using Offset and TextBox.Value to input between 2 columns

Question: I am trying to use an ActiveCell.Offset function. I am slightly confused to code using Column A as my base.
I wouldn't want to input as new row. I would want to start from number 1 instead of skipping to an empty row.
My code is as below.
Private Sub CommandButton1_Click()

Dim rng As Range
Set rng = Range("A2:A2")

If TextBox1.Value = "" Then
    If MsgBox("Form is not complete. Do you want to continue?", vbQuestion+vbYesNo) <> vbYes Then
    Exit Sub
End If
End If

ActiveCell.Offset(0, 1) = TextBox1.Value
ActiveCell.Offset(0, 2) = TextBox2.Value
ActiveCell.Offset(0, 5) = TextBox3.Value

ActiveCell.Offset(1, 0).Select

End Sub

Comment: It's not clear what your goal is. Can you please [edit] your question and describe detailed step-by-step in words what you expect your procedure should do?
Answer: You seem to be setting a range
Set rng = Range("A2:A2")

and not using rng anywhere in the sub. In addition you seem to be basing your offset on the ActiveCell but not having a clear ActiveCell selected in the sub (you could be selecting it outside the routines of course) which leads me to suspect that this 'rng' is your desired Active cell. If this is the case then you will need to make rng the ActiveCell by activating the cell.
Comment: It should be mentioned that `Range("A2:A2")` dosen't make much sense because you can write just `Range("A2")`. Also you might want to specify in which sheet the range is `Worksheets("SheetName").Range("A2")` if that's always the same sheet.
Comment: Appreciate your help. Have replaced ActiveCell with rng, it did work! Thank you so much!
Comment: If it was useful then you may want to accept the answer or upvote the answer as useful?


HTML "onsubmit" cancelling out the "action"

Question: When running my onsubmit for the JavaScript validation, it cancels the action where my form details are added to a database. Once the onsubmit is taken away, the action function works, but with both of them there the action doesn't get called or work. Any fix to this?
<form onsubmit="return validation()" action="includes\submitContact.inc.php" method="post" id="form" >

Comment: post `validation()` please
Comment: If the validation method does not return true, the form submit will not happen.
Comment: Just solved it! Had a return false. Thanks @Berto99 for pointing me there! Its all working as it should now.
Comment: @DevB Given that you found the solution after debugging it from the comments, you should self post an answer showing what the issue was and how you fixed it.
Answer: if(check == 1)
{
    errorMsg.style.backgroundColor="#28B463";
    errorMsg.style.color="white";
    text = "Form Successfully Submited"
    errorMsg.innerHTML = text;
    return false;
}

I was playing around with the JavaScript to change CSS styles, and due to the above code, a false return was happening, resulting in the "action" not running as it should.
Answer: The return value of an event handler determines whether or not the action should be processed
I think validation returns false or undefined so nothing happens
Try
<form onsubmit="validation()" action="includes\submitContact.inc.php" method="post" id="form" >

Edit:
This means the issue comes from there if above works, then you can debug safely why validation returns a falsy boolean
Comment: Without the return, the validation cannot cancel the submit on failure.
Comment: @Taplar Thanks for the input, I thought this was implicitly clear. I edited the answer to extend on this
Comment: Thanks! Helped, did indeed have a return false at the end of the validation.
Comment: "Do this and if it works, then you can debug" isn't a complete answer.  It does not solve the issue.
Comment: @Taplar Yes I agree, and your input made the answer more exhaustive. The issue on itself wasn't the clearest at first neither, maybe this is where we could have digged more before answering. Glad at least we guided our new community member towards a resolution!
Answer: Probably validation function doesn't return true, it should return true to continue the submission
Comment: Thanks! Helped, did indeed have a return false at the end of the validation.


How to create failure log file owned by syslog while running systemd service?

Question: I am running Go binary as a service on ubuntu 16.04,and not able to create log file which is owned by syslog. I have created a services for myapp.service and it looks like as follows:
[Unit]
Description=myapp

[Service]
Type=simple
Restart=always
RestartSec=30
WorkingDirectory= /home/go/src/myapp
ExecStart=/home/go/src/myapp/myapp

# myapp.log owned by syslog
PermissionsStartOnly=true
ExecStartPre=/bin/mkdir -p /var/log/myapp
ExecStartPre=/bin/chown syslog:adm /var/log/myapp
ExecStartPre=/bin/chmod 755 /var/log/myapp
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=myapp

[Install]
WantedBy=multi-user.target

Now It just creates a folder inside /var/log/myapp . But didn't creates any myapp.log file inside myapp folder. I can check these logs using command ::
sudo journalctl -f -u myapp

It will show all logs inside terminal. I have following questions ::

how can I create a log file, which will maintain all logs(inside
/var/log/myapp/myapp.log).
And I also want to categorize logs, like I just want to maintain
failure logs only, so after created log file how to write a failure logs   inside myapp.log. For now it shown all logs(both success failure)
inside terminal.
I am hitting golang api on port 8080, but whenever I checked port it
doesn't seems busy. I didn't understand why port 8080 is not busy when I am running it as a service.Although it gives response in both cases but after stopping service and if running app in foreground it shows port 8080 is busy. Is there any reason for this?
Comment: are you running your app as user `root`? or do you want it run as `syslog`? What does `systemctl status app` show? With 3), what do you mean by hitting? What does it mean 'doesn't seem to be busy' (on the couch?)? If the app process is running,`lsof -p {pid}` would be useful to see if log directory descriptors exist or listing ports.
Comment: hitting is used to hit the url on browser, and I am using port `8080` for this. `systemctl status app`  shows it's active ,loaded  and it's processes status.
Comment: I am not able to create myapp.log file inside `/var/log/myapp/myapp.log` , please suggest me how can I create failure log file for myapp.service
Comment: Thanks for answering some questions. Other answers may have helped give you and answer to your repeated question. Replace `ExecStart=/usr/bin/strace -s 99 -o /tmp/myapp.strace /home/go/src/myapp/myapp` and look at the `tmp/myapp.strace` and see what syscalls there are around `/var/log/myapp` and *show them*. Maybe apparmor is preventing access. Maybe you should use [journal](https://github.com/coreos/go-systemd/blob/master/journal/journal.go) and not worry about creating log files.
Comment: I caanot replace ExecStart with this, because I am using go app binary to run as syatemd service. And I need a log file to maintain failure logs
Answer: If you are using rsyslog to manage syslogs, created a file inside /etc/rsyslog.d/myapp.conf with the following content:
if $programname == 'myapp' then /var/log/myapp/myapp.log
& stop

Now restart the rsyslog service and you should see the syslog listener on port 514, restart the myapp, and now you should see log events being sent to the log file.
sudo systemctl restart rsyslog
sudo systemctl restart myapp

But It will creates a log file having both success failure logs.


Between-person level interaction

Question: I have a database called dat1. Each participant (id) had multiple measurements. One of the variables measures type of the stress called stress_type (acute=0, chronic =1) and another variable is the stressful level of the stressor (continuous variable 0-3).
I wanted to examine the effect of the stressor type, stressful level and interaction of type*level of stress on depression.
I disentangled stress_type and stress level variables to get the within and between person mean, and calculated the within and between interaction as well.
mean_stress_type_rec = between person mean of stress type
mean_stress_level = between person mean of stress level
cent_stress_type_rec = within person mean of stress type
cent_stress_level = within person mean of stress level
I ran the following model:
res_intvsnonint_levels_int_nopleas <- lme(dep ~  mean_stress_type_rec * mean_stress_level +
 cent_stress_type_rec * cent_stress_level,
 random = ~ cent_stress_type_rec + cent_stress_level _rec + obs_cent1 | id, data=dat1,
 na.action=na.omit,
 control=lmeControl(maxIter=1000, msMaxIter=1000, niterEM=1000, sing.tol=1e-20))

I found a very marginal interaction at the between level:
                    Coef.   SE         95% CI      t-value    p
Fixed effects
Intercept           29.28   4.42    20.61 – 37.96   6.62    <0.001
Time               -0.07    0.03    -0.13 – -0.01   -2.43   0.015
Type - between      4.46    6.47    -8.36 – 17.27   0.69    0.492
Type - within       0.41    0.59    -0.74 – 1.56    0.7     0.484
Level - between     2.14    2.6     -3.01 – 7.29    0.82    0.413
Level - within      0.41    0.35    -0.27 – 1.10    1.18    0.237
Type*Level - between-8.21   4.13    -16.40 – -0.03  -1.99   0.048
Type*Level - within  0.6    0.79    -0.96 – 2.16    0.76    0.448

I don't know what then I should do to further examine the between-level interaction. I'd appreciate your help.
Answer: In principle*, you can use emmeans emtrends for this. You need to choose a couple of "testing points" for your other continuous predictor. This is easiest if you standardize your predictors and choose for instance points -1, 0, 1 for predictor 1. This way you will be comparing predictor 2 slopes at predictor 1's levels of mean-1 SD, mean, and mean +1 SD, which I find is often reasonable way to test continuous-continuous interaction. So, first standardize your predictors, then run the model again with standardized predictors, then:
library(emmeans)
emtrends(modelname, pairwise ~ levelBetween, var = "levelWithin", at=list(levelBetween=c(-1,0,1))

You don't have to standardize though if you can find reasonable testing points from the predictor's raw values.
*However, I wonder about your stress type variable - can it really be understood as continuous? I understand you need to somehow get the mean value from it but I wonder what the resulting variable actually represents because it was originally categorical (?). But in principle you can use the above with a cont x cont interaction.
Comment: Thank you for your answer. If I run the model using the standardized predictors I might lose disentangling the within and between person, am I correct? The model showed only the interaction of the between-person level of both stress type and stress level was significant (mean_stress_type_rec * mean_stress_level), which is what I'd like to understand. The within-person interaction was not significant. For clarification, the between-person variable of stress type is the proportion of the chronic (vs acute) events over the study.
Comment: You don't have to standardize anything, you can choose suitable points from your predictor's distribution in its current form. You can use the emtrends code I provided without standardization. Just change the -1, 0, 1 into values from the range of your predictor in its current form.
Comment: (however it seems that both variables in your significant interaction are between-subjects (level 2) variables i.e., have 1 value per participant - in which case standardizing them should not do anything to your model/interpretation. It's only level 1 variables you need to keep subject-mean centered in a multilevel model. But it may be I'm missing something here)
Comment: Yes, the significant interaction is in the between-subjects level. I disentangled the within and between variance. The interaction was n.s. in the within level. That's why I'm lost  regarding what I need to do in this case in which the interaction is in the between subjects level.

Between-person level interaction


Is GEE the simplest choice for a 2x2 factorial design with repeated measures and binary dependent variable?

Question: I have a 2x2 factorial design. Both independent variables are binary (age: young/old and condition: experimental/control). Age is between subject and condition is within subject. The dependent variable is binary (pass/fail). The hypothesis is that there will be interaction between the two factors.
Is Generalized Estimated Equations (GEE) the simplest correct way to test the hypothesis? Or are there simpler methods that will give fully equivalent results of the statistical test?
Edit:
Each subject receives only one experimental and one control task. All included subjects contribute score from both task.
Thanks in advance,
jan
Comment: Is the each patient randomized to receive experimental treatment and control treatment only one time? Has each patient completed an equal number of trials?
Comment: Thanks @AdamO Yes and yes. I added this info to the main question above.
Answer: This is a case for conditional logistic regression, not GEE.
GEE is a widely touted general method for handling repeated measures dependent data. But one struggles to justify its low power and handling of the response under control condition in an experimental design. The power to detect an effect of treatment will suffer because GEE doesn't fully condition on the outcome during the "control" portion of the study. Rather, the outcome while receiving control is considered a random response that has residual variation - some of which attributed to the patient - but some of which is random binomial variation. In all, there's a loss of power.
With pre-post analyses, like with paired t-test,  ANCOVA, or McNemar's test, the powerful analysis results from conditioning on the "pre" response. Had you no strata/blocking, the McNemar test would readily present as the obvious choice. But recently, methods have been established for a stratified version.
The conditional logistic regression is a powerful and computationally complex algorithm. The internal working of conditional logistic regression is irrelevant. But essentially the conditional likelihood of the binomial response is identical to the partial likelihood of a Cox proportional hazards model for time-to-event outcomes where each matched set comprises a stratum in an analysis of a single event time with patients achieving response marked as "events" and those not marked as "censored". Since the number of strata are relatively high compared to standard Cox models.
This is why in R, the conditional logistic regression lives inside the survival package as clogit with detailed documentation.
Comment: Thank you @AdamO. I am looking into the conditional logistic regression. I did not know of it, so I am very happy to learn. I see two problems:
1. The study will have a moderate-size sample to start with: 50 participants total in two equal groups of 25.
2. I expect high performance (mostly pass score) in the control task overall and high performance in one group but not other.
So after dropping participants with same performance in control and experimental task (mostly "pass" on both task), the sample left will be small and likely imbalanced across conditions. Will this affect inference?
Comment: Are you saying 25 are randomized to receive control then treatment, and 25 are randomized to receive treatment then control?
Comment: No. I will have two groups of 25. I called them above "young" and "old". Maybe these labels are confusing, because this is a cross-sectional design, not longitudinal. We can call them group 1 and group 2.Both groups are tested at the same time. The within subject factor is type of task: experimental vs. control. And in fact they are also administered at the same time. And their order cannot be counterbalanced. So time and order play no role in this design.
Comment: @jan you are not giving a clear enough example to make this design obvious to someone who is not actually involved in the research. You can't administer two conditions at the "same time" unless they're, for example, in two parts of the body, like the right and left eye.
Comment: Ok sorry. I realize I should have given less abstract description. This is an imitation paradigm for toddlers. They see a two element sequence: Action-outcome. When they imitate they can produce one or the other or both. By theory producing the outcome is the control task. Producing the action is the experimental task.The sequence has to be presented to participants in a fixed order. But children can produce any or both elements in either order.


Create new line/carriage return on paragraph after more than 1 spaces

Question: I would like to know how to create carriage return/start in new line after more than 3 spaces in a single line of paragraph in Javascript. Thank You
Comment: Give a key down event to the paragraph. Track the spaces and if its count is 3 then trigger enter event. If the content is already there, then iterate and append
Comment: Please elaborate your question. Are you dealing with a content editable element or maybe with textarea, or something else?
Comment: I'm using indexOf to count the number of spaces on the given data from host(mainframe)then will start a new line after I hit more than 1 spaces then display the host message on the web browser using Javascript, is there a way to increment a spaces??how? Thank you
Comment: Thanks for the help guys, got it.. I used the regular expression  below to check the spaces then carriage return & new line for more than 2 spaces. thanks                                                      var sChecktemp = sChecktemp .replace(/\s{2,}/g, "\r\n");
Answer: It would help  if you posted your code, but if you want to parse a block of text and reformat it, how about this:
var string = //paragraph text;
var newStr = string.replace(/ {3,}/g, '
');

Then replace string with newsStr.


Excel 365 formula to retrieve client's newest score

Question: I am using this formula to pull the client's highest score:
<code>=MAX(IF(FILTER(Appointments!G:G, Appointments!C:C=C2)>0,FILTER(Appointments!G:G,
Appointments!C:C=C2)))
</code>
What I want is the most recent score. Appointments!G is the score, Appointments!C is the client ID, Appointments!D is the date of the score.
Comment: Dates are numbers. Use `Max` on the date maybe?
Comment: `=MAXIFS(Appointments!G:G, Appointments!C:C,C2,Appointments!D:D,MAXIFS(Appointments!D:D,Appointments!C:C,C2))`
Comment: Also your can be simplified original: `=MAXIFS(Appointments!G:G, Appointments!C:C,C2)`
Comment: Yes, just add that to the criteria: `=MAXIFS(Appointments!G:G, Appointments!C:C,C2,Appointments!D:D,MAXIFS(Appointments!D:D,Appointments!C:C,C2,Appointments!G:G,">0"))` and `=MAXIFS(Appointments!G:G, Appointments!C:C,C2,Appointments!G:G,">0")`
Comment: @ScottCraner I like this simplification, I'm also looking for the first lowest score that is above 0. Is there a way to write this so it excludes 0s? =MINFS(Appointments!G:G, Appointments!C:C>0, C2) didn't work
Comment: @ScottCraner Thank you! This was very helpful, I had looked at MIN/MAXIFS earlier but hadn't quite gotten it.
Comment: @ScottCraner Sir, if you share the solution in answers, with a small sample data, it will be very helpful and will be grateful as well, for future references.
Answer: you could use the below trick of match formula.
=MATCH(1,(C2=Appointments!C:C)*
((MAX(FILTER(Appointments!D:D,Appointments!C:C=C2)))=Appointments!D:D),0)
this formula will return the row index for the most recent score.
then you could combine it with the index function to get the value.
=index(Appointments!G:G,MATCH(1,(C2=Appointments!C:C)*
((MAX(FILTER(Appointments!D:D,Appointments!C:C=C2)))=Appointments!D:D),0))
the bold part is to get the most recent date:
(MAX(FILTER(Appointments!D:D,Appointments!C:C=C2)))


How to integrate WooComerceAPI into React?

Question: I wanted to receive data to my site on React through the API.I did everything as stated in the documentation, performed the installation of npm install --save woocommerce-api, created the object with parameters as in the documentation http://woocommerce.github.io/woocommerce-rest-api-docs/?javascript#pagination
import React, { Component } from 'react';
import '../App.css';
import WooCommerceAPI from 'woocommerce-api';

class Goods extends Component {
    WooCommerce = new WooCommerceAPI({
    url: 'http://portland.com/wp/', // Your store URL
    consumerKey: '**KEY**', // Your consumer key
    consumerSecret: '**KEY**', // Your consumer secret
    wpAPI: true, // Enable the WP REST API integration
    version: 'wc/v2' // WooCommerce WP REST API version
  });
    render() {
        return(

            {WooCommerce.get('products/1').name}

            black

            $49.99

        );
    }
}

export default Goods;

But I get Line 20: 'WooCommerce' is not defined no-undef
Can u help me integrate correctly API in my site?
Comment: From what i understood while going through woocommerce docs, woocommerce-api is for nodejs(server side) and not for reactjs
Comment: In your render function, you need `this.WooCommerce`. This isn't about React or WooCommerce, or integrating an API, this is just about how classes work.
Comment: About the general issue: React is front-end, the WooCommerce API is backend. What you need to do is use the API in node.js or PHP or Ruby. Then in your React app, you make requests to your own backend (which grabs wordpress data using the WooCommerce API and forwards it to your React client).
Comment: Sorry! I didn't understand what I should do.I use WordPress as a backend for my site and want receive data from API.I must create node.js file with 'var WooCommerceAPI = require('woocommerce-api');

var WooCommerce = new WooCommerceAPI({
  url: 'http://example.com', // Your store URL
  consumerKey: 'consumer_key', // Your consumer key
  consumerSecret: 'consumer_secret', // Your consumer secret
  wpAPI: true, // Enable the WP REST API integration
  version: 'wc/v2' // WooCommerce WP REST API version' and this file take data from woocomerce?
Answer: I Think you have to use the oauth authentication for getting the data from the woo-commerce rest api because your wordpress is on the server where there is no ssl certificate.
Answer: Try this way:
1). Use axios library (https://alligator.io/react/axios-react/) to call the WooCommerce Rest API.
2). To simplify the authentication process, make sure you enable SSL on your Wordpress hosting (I tried authenticate on the non SSL, it appears the process was not that easy, compared the SSL one).
3). Follow the steps from this link to generate the WooCommerce Rest API key
https://woocommerce.github.io/woocommerce-rest-api-docs/?javascript#authentication
4). Once everything setup, test retrieve list of products using Postman (https://www.getpostman.com/).
[GET]
https://{{host}}/{{wordpressFolder}}/wp-json/wc/v2/products/

[HEADER]
// Inside your React code, do this:
const basicToken = btoa(PRODUCTS_CONSUMER_KEY+':'+PRODUCTS_CONSUMER_SECRET)

Authorization: Basic ${basicToken}


Consulta MYSQL trazer resultados por ordem de distância usando coordenadas

Question: Tenho uma tabela que tem as coordenadas (latitude e longitude) dos usuarios.
Como faço fazer uma consulta para trazer quem está mais próximo da localização.
Ex:
ID |     Lat    | Long
1  | -22.874428 | -43.372334
2  | -20.292799 | -40.301991

Trazer esses dois onde o id 2 estaria mais próximo (coordenadas de busca: -20.292799 | -40.301991)
Gostaria que a consulta trouxesse algo do tipo
ID | Distancia
1  | 2km
2  | 20km

o 2km e 20km seriam calculados dentro da consulta sql
Comment: Você tem os dados das coordenadas de referência? E você sabe calcular a distância utilizando coordenadas?
Comment: tenho os dados sim, esse por exemplo: -20.292799 | -40.301991. Não sei calcular a distância.. :/
Comment: Esse aí só traz a distância específica, preciso que traga a consulta uma coluna com a distância, para que possa fazer a ordenação, vou editar a pergunta para ficar melhor, obrigado.
Comment: Qual o valor da origem? Pois, como vai calcular a distancia se não sabe onde começa? Essa distancia seria em linha reta certo? E não utilizando um sistema de ruas para verificar a distancia, certo?
Answer: Você consegue calcular a distancia usando uma expressão aritmética, exemplo:
SELECT
    l.id,
    l.lat,
    l.lng,
    ( 3959 * acos( cos( radians(c.lat) )
              * cos( radians( l.lat ) )
              * cos( radians( l.lng ) - radians(c.lng) )
              + sin( radians(c.lat) )
              * sin( radians( l.lat ) ) ) ) AS distancia
FROM
    locais AS l
    JOIN (
      SELECT
        -20.282957 AS lat,
        -40.401991 AS lng
    ) AS c
ORDER BY
    distancia

Fiddle rodando o exemplo acima: https://www.db-fiddle.com/f/pFEVDYaXQyj81g2kh6VLBw/2


is there any way we can introduce the wildcard character in creating release in azure

Question:
Instead of default I want to select the particular branch from release folder like release/*.
is that possible with Azure?
Comment: Please check for the answer if helpful to you. Then you can sign it as a correct one to benefit others who meet the same issue.
Answer: Yes. You can specify wildcards for triggers in the branch specification either through yaml or classic pipelines.
e.g.


Entity Reference- How do I display more than one view mode?

Question: I am using entity reference to display some nodes. In manage display, I am rendering my entity reference content with teaser view mode. However, I need to display the first node different. I need the first one with a picture and the other ones I just need the date, text and a link. Something like  in the picture.
Answer: I solved by using the module entity reference multiple display  which let you to configure different view modes for your field reference. You can split in groups to achieve that.
You install the module and after it is enabled, you go to your content type and in manage display choose rendered entity with different view modes. I have attached a picture

From then you can choose how many groups you would like to display in my case I just need two, the first one I needed to be displayed as view mode teaser and the second one I need no picture in the teaser and just need to display at most two elements in that group.
If anyone has another way to handle this please post your answer here.


How to use V4L or V4L2 (Video for Linux) in Android?

Question: Does Android Linux Kernel support V4L or V4L2 (Video for Linux). If not, is it possible to add it?
Comment: It depends from tablet to tablet. If you take Acer Iconia tablet, it is present. And most of the tabs have the support. This is included to add UVC support for cameras(one of the purposes). Even if not included, you can compile the linux kernel by enabling the V4L2 inside drivers. But it would be a problem to test it if you dont have a development platform in hand.
Answer: Yes,Android support V4L2,Android camera subsystem internally uses V4L2.
For more reference pls check http://comments.gmane.org/gmane.comp.handhelds.android.porting/12285
Answer: On newer devices it's actually used ( Samsung Note 3 or S4 GT-I9506 ) to play videos, handle the camera and so on.
Comment: Can you post a reference?


MSE loss and information

Question: I'm thinking if there's a way of interpreting mean square error loss frequently applied in statistics and machine learning under the information framework?
Suppose we have two random variables (or vectors), $X$ and $Y$, and let's denote their observations by $\tilde{X}_i$ and $\tilde{Y}_i$, $i=1,\ldots,n$. Our job is to predict $Y$ as best as we can. In regression tasks, one is often encouraged to find a function $f$ such that $L(f) = \frac{1}{m}\sum_i (f(\tilde{X_i})-\tilde{Y_i})^2$, the squared error, is minimized. We often feel comfortable to apply $f$ on $X$ to predict $Y$.
From an intuitive point of view, if we denote all possible regressors are in the set $\Omega$, and if $\min_{f\in\Omega}L(f)$ is sufficiently, we can say that $X$ contains enough "information" about $Y$. Now the function $f$ minimizing the loss globally may not be found, but once you find a $f$ such that $L(f)$ is obtained, I suppose, in principle, one can obtain a lower bound of how much information $X$ contains about $Y$.
In information theory, the idea of information is formalized by the self-information w.r.t a random variable, $H(X), H(Y)$, mutual information between two variables, $I(X,Y) = H(X) - H(X|Y)$ and the K-L divergence $D(Y||X)$. My question is, can we find a lower bound for $I(Y|X)$ or $D(Y||X)$ once we are given a regressor $f$ and its MSE $L(f)$, without specifying the distribution of $Y$ or $X$? Or is it in principle possible?
Another often used loss function is the cross entropy loss. Would the problem become easier if we replace MSE with cross entropy loss, since it is in form consistence with the definition of information?
Comment: @FabianWerner In that case, given the observations of X and Y, can I estimate H(X), H(Y), and I(X, Y)?
Comment: There is the 'no-free-lunch' theorem stating that the error a concrete predictor $f$ makes must be 'big in average', i.e. for a given learning strategy we can find a distribution $p(X,Y)$ such that the learning strategy fails miserably (is arbitrarily close to guessing). In general Im unsure about the nature of the question: I(X,Y) is something that does not involve neither $f$ nor tha strategy how to build $f$... so, what do you mean? How big/small I(X,Y) can be if we do not know anything about X and Y? Well... it can be 0 if they are independent...
Comment: @FabianWerner Thanks for the comment. I mean if we find a f such that the mse loss is L(f), we know that the L(f) is an upper bound of the minimal error we can get from a predictor, since ff achieves it. I guess less loss means more information about Y contained in X, so is it possible to derive a lower bound for $I(X,Y)$, in this case?
Comment: @FabianWerner I realize that my question can be ill-formed or primitive. Maybe without constraint on $f$ or how it is built, the question is meaningless.
Comment: Well... yes, there is a lower bound: It is always $\geq 0$ and it can be exactly zero if $X$ is completely unrelated to $Y$... so, (just trying to figure out the 'right question') isn't it a lower bound on the performance/error of a predictor that you are actually after? Or is it a relation like 'if I(X,Y) is that small then we can find no better predictor than ...'? For example: If I(X,Y)=0 then we can only guess and we will never do better than guessing...
Answer: If we can predict X from Y with some accuracy, this in general gives us a lower bound on mutual information. Our predictor certifies that X has a certain amount of information about Y, but we only have a lower bound because it could be that a different predictor would work better. In the discrete case, this is summarized with Fano's inequality (keep in mind $I(X,Y) = H(X)-H(X|Y)$ when looking at this page), which shows how mutual information is bounded by prediction error.
For the continuous case, it is possible to derive similar bounds. One example would be the following:
\begin{align}
I(X,Y) &= H(Y)-H(Y|X) = H(Y) + \mathbb E_p \log p(y|x) \\
&\geq  H(Y) + \mathbb E_p \log q(y|x) \\
&= H(Y) - 1/2 \log \mathbb E_p[(Y-f(X))^2] -1/2\log (2 \pi e)
\end{align}
In the second line, we use the non-negativity of KL divergence, introducing some variational distribution, $q$. In the third line, we make a specific choice for $q(y|x) \sim \mathcal N(f(x), E_p[(Y-f(X))^2])$. As you can see, the smaller the error in your regression, the larger the lower bound on mutual information will become. Through different choices of the variational distribution, you can get different bounds. (In case it is not clear, $\mathbb E_p$ is the expectation and the sample expectation would just be $\mathbb E_p[ (Y-f(X))^2] = 1/m \sum_{i=1}^m (Y_i-f(X_i))^2$.)
Comment: is this trick to obtain a "continuous Fano inequality" from somewhere? Could you suggest a reference?

MSE loss and information


Como converter uma matriz JavaScript para matriz em PHP?

Question: O sistema me dá uma matriz em JavaScript para trabalhar na página. Eu posso obter esta matriz com PHP, mas não sei como converter de JavaScript para PHP e trabalhar usando o PHP.
Este é um exemplo da matriz em JavaScript:

Quero deixar assim:
$categ = array(
    '0' => array('1','0','0','categoria A','Descrição da categoria A','/board/categoria_a/1','1'),
    '1' => array('2','0','0','Categoria B','Descrição da categoria B','/board/categoria_b/2','0'),
);

Comment: Creio que se você serializar em JSON usando JavaScript e de-serializar usando PHP você vai obter exatamente o que quer.
Answer: Do lado do JavaScript tens de serializar esse array. Podes usar o JSON.stringify assim:
JSON.stringify(uCatsOut);
// dá uma string -> '[["1","0","0","categoria A","Descrição da categoria A","/board/categoria_a/1","1"],["2","0","0","Categoria B","Descrição da categoria B","/board/categoria_b/2","0"]]'

Do lado do PHP tens de descodificar o JSON, podes usar o json_decode.
$json = '[["1","0","0","categoria A","Descrição da categoria A","/board/categoria_a/1","1"],["2","0","0","Categoria B","Descrição da categoria B","/board/categoria_b/2","0"]]';
$phpArray = json_decode($json, true);
var_dump($phpArray);

Que vai dar:
array(2) {
  [0]=>
  array(7) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "0"
    [2]=>
    string(1) "0"
    [3]=>
    string(11) "categoria A"
    [4]=>
    string(26) "Descrição da categoria A"
    [5]=>
    string(20) "/board/categoria_a/1"
    [6]=>
    string(1) "1"
  }
  [1]=>
  array(7) {
    [0]=>
    string(1) "2"
    [1]=>
    string(1) "0"
    [2]=>
    string(1) "0"
    [3]=>
    string(11) "Categoria B"
    [4]=>
    string(30) "Descrição da categoria B"
    [5]=>
    string(20) "/board/categoria_b/2"
    [6]=>
    string(1) "0"
  }
}

Comment: E, caso não esteja claro para o autor da pergunta, a passagem dos dados do JS para o PHP precisa ocorrer via Ajax ou envio de formulário.


How many digits are 9 at the end?

Question: How many digits at the rightmost of this sum are 9?
$$1! + 2\times2! + 3\times3! +\dots +48\times48!$$
I tried to calculate the first few terms but I couldn't solve it. The answer is 10.
Answer: HINT: $n\cdot n!=(n+1)n!-n!=(n+1)!-n!$, so
$$\sum_{k=1}^{48}k\cdot k!=\sum_{k=1}^{48}\big((k+1)!-k!\big)\;.$$
Write this out, and you'll see that most of the terms cancel. In fact, the sum is $49!-1$. Now you need only determine how many zeroes are on the end of $49!$.
Comment: Thank you! I definitely underestimated this question.Now  its clear that last 10 terms of 49!-1 are "9".
Comment: @guest: You're welcome!

How many digits are 9 at the end?


Support vector machine margin term, why norm squared?

Question: For a SVM with soft margin, we want to minimize the following:
$$ \lambda||\hat w||^2 +(1/n)\sum   max(0,1-y_i(\hat w \hat x_i -b))   $$
we know that $2/||\hat w||$ is the width of the margin.
The second term penalizes a misclassified point for how far away it is from the margin relative to the width of the margin. E.g., suppose there is a misclassified point $x_0$:
$$ 1-y_0(\hat w \hat x_0 -b)=3 $$
That means $x_0$ is $3/||\hat w||$ away from $1-y_i(\hat w \hat x_i -b)=0$ and is penalized for $3$.
The first term penalizes for the inverse of the width of the margin squared. I find it hard to reconcile with the second term, they seem to be of different scales. Is there any reason (intuitively) why $||\hat w||^2$ is used instead of just $||\hat w||$?
PS: Perhaps one reason is that  $||\hat w||^2 $ is easier computation-wise (quadratic programming)? Or perhaps norm squared assumes sample noise to be Gaussian? I am not sure. Has anyone seen the use of $||\hat w||$ instead of $||\hat w||^2$?
Comment: In case anyone is curious, [here](https://math.stackexchange.com/questions/436901/svm-min-square-norm) is another answer on mathematics.SE.
Answer: As far as I know, the square is introduced in the formulation for convenience. The norm will reach the optimum at the same point, and we get rid of an ugly square root.
With respect to the hinge loss term, the square just makes no difference either, because of the presence of $\lambda$. Both $f(x)=\|x\|$ and $g(x)=\|x\|^2$  are surjective functions of the form $\mathbb R^d \rightarrow \mathbb R_+$. This implies that for any value of $w$ there exists $\lambda \in \mathbb R$ such that $\lambda \|w\|=\|w\|^2$.
That is, for any solution that you find for the squared objective, you can find exactly the same one for the non-squared objective by tweaking $\lambda$.
Since the square is introduced for convenience and it makes no effective difference, I doubt you'll be able to find an intuitive reason for its being there.
Comment: Thanks for the reply. When the errors are normally distributed, the least squares estimates are maximum likelihood (http://www.dsplog.com/2012/01/15/least-squares-gaussian-noise-maximum-likelihood/). My additional question is, if the noise in the samples are far from Gaussian, wouldn't $||w||^2$ be a bad choice? Or does that hardly matter in practice?
Comment: Here, the norm being minimized does not represent the same thing as in least squares estimation, so that perspective does not translate to this context.
Comment: Right my mistake. I think you've already answered my original question very well, so I am accepting it.
Comment: Suppose the minimizer of the loss $\lambda_1 \|w\|^2+f(w)$ is $w_0$. Does that mean if we choose $\lambda_2 = \lambda_1\|w_0\|$, then the minimizer of the loss $\lambda_2\|w\|+f(w)$ is also $w_0$?


The manufacturing date of a violin

Question: I'm buying this violin from a violin player. I inspected the label inside it and I found Suzuki Anno 1981.
Here is a picture of it:

Which means that it is 34+ year old.
I'm a guitarist but I don't know if this is really the manufacturing date or something else.
Comment: In my opinion *Anno* leaves no room for interpretation. It could theoretically be the year of a major maintenance, but I doubt it. Anyway 34 years is a completely unremarkable age for a violin, I would recommend to focus on more interesting properties like sound, opinion of a violin player you trust.
Comment: He probably means that the age is neither a good thing or bad.  For quality instruments, even guitars, 34 years for an acoustic instrument is just getting started.  Especially if it was well-cared for.  I have a rather unremarkable (read: nothing fancy or collectible about it) violin from my Great Grandfather that's over 100 years now.  I think coming from the world of guitar (when a mint 1950's Strat is worth the average cost of a home) is the confusing part here.
Comment: "average cost of a home"   yeah yeah  try buying a cello or violin from a Strad-class luthier of the 18th century  :-( .   OK, seriously, cost of 50's electrics is due more to "collectibility" than playability or sound quality. Solid-body instruments really don't change with age, unlike any wood-based hollowbody instrument.
Comment: Thanks for your comment, "Anyway 34 years is a completely unremarkable age for a violin"  i'm not sure if i understand your statement, do you mean that  34 years is not enough to consider a violin an old one? could you please give more details?
Comment: So very true!  There are plenty of components to solid body instruments that just aren't recreated effectively or possible in today's world.  And there are plenty of people out there willing to disprove the fact that solid wood doesn't change with age.
Comment: @SmartyTwiti: Yes Shawn interpreted me correctly. It is no antiquity, its nowhere near its expected lifetime, and hey, it obviously didn't fall apart for quite some time (which new very cheap ones could do).
Comment: 34 is practically newborn.  People are still playing violins that were made 334 years ago.
Answer: My guitar is older than that and nothing special.  My violin is from 1820 and not all that special, either, coming from an area known for mass manufacture of violins a few generations later.  My principal accordion was built in 1960.  Naturally, all of them had needed some servicing while in my possession.
In modern times, it is customary to run through a number of possessions in the course of your life.  Really good instruments, in contrast, tend to run through a number of players in the course of their existence.  Quite a few instruments by old masters were given names of their own and have a known history of owners and players.
Violins don't "play through".  Strings need changing, fingerboards need honing and eventual change, tuning pegs need to get replaced eventually, sometimes the bridge fails, sometime the soundpost needs replacement and, depending on whether soundpost replacement is done timely, the cover of the instrument may need to be worked on.
Depending on the instrument, all of those works may be worth doing or not.  But principally violins and their cousins of the string family are built to last indefinitely when cared for properly.  All of the parts that might wear are replaceable at moderate cost and effort.
That's different to some woodwinds with mechanics that wear out.  While they can be replaced as well, their contribution to the value/cost of the instrument is larger and the material of the instrument is less contributive to its sound than with string instruments.  So something like "an old clarinet" or "an old flute" is not coveted for its sound qualities but rather for its antiquity.  Violins are different in that regard as the wood is an essential part of its sound.  And these days, slowly grown and well-aged wood is harder to come by than centuries ago.  Even if you don't subscribe to worship of the old masters.
Comment: You are right. It is all about the wood and how old it is.
Comment: The point about wood is quite valid.  "Old growth" always seems coveted, and the over-harvesting we've done in the 20th century has really hampered old growth in current instruments.  Let alone wood species wiped out or nearly wiped out by over harvesting.Brazilian Rosewood, for example in guitars is very very rare and pretty much impossible to get new now.  It's sound is unique to itself and vintage instruments were all made from it.  It's already a property of what makes older guitars sound the way they do.
Answer: The label means that the instrument was probably made in 1981. The thing to keep in mind about violin labels is that they are frequently dishonest. In this case, the label claims that the instrument is a mid-quality brand, and not too old. That means there is a high chance it is the original label, and that it is honest because there would be relatively little profit in counterfeiting it. A string specialty shop can usually tell you on the spot if your instrument is worth much.
If your label said Stradivarius, or Guarneri, or had a date over 150 years ago, it would almost certainly be fake. Those labels starting appearing in large numbers in the late 1800s, and have been in production ever since.
Answer: My violin was from 1928, and although it has above average sound quality and volume, and its about 9,000 dollars, that's the lower end of violins. They can be, as someone said, more expensive than a house, and my former teacher played a loaner violin once that was valued at 4 million dollars. Violins can get expensive, but 1981 is nothing too desirable, so yes, 1981 is most likely the manufacturing date, and 34 years is very young for a violin.

The manufacturing date of a violin


Firebase Database permission denied with custom tokens

Question: My application is set up with a backend REST api written with Java and Spring Boot.  I am creating custom firebase tokens in the backend in which the client side (iOS platform) calls an API to retrieve.  With this custom token, I use FIRAuth.auth()?.signIn(withCustomToken: customToken) to sign in, where customToken is the value from the api response.
I am creating these custom tokens as follows:
public static String createCustomFirebaseToken(){
        UUID uid = UUID.randomUUID();
        Task<String> authTask = FirebaseAuth.getInstance().createCustomToken(uid.toString());
        try {
            Tasks.await(authTask);
        } catch (ExecutionException | InterruptedException e ) {
            //TODO: handle exception
        }
        String customToken = authTask.getResult();
        return customToken;
}

I am able to view the UUIDs that were generated to create these custom tokens in the Firebase console, under the Authentication tab.
In my Firebase Database, I want to only allow read/write permissions to these authenticated users - this is where I am stuck as I am not too familiar with Firebase.
For example, say I have a news model in which I will allow all authenticated users to read & write.
How would I go about implementing a rule such as:
{
    "news":{
        ".read": // authenticated user
        ".write": // authenticated user
    }
}

Thank you in advance!
Answer: {
    "news": {
        ".read": "auth != null",
        ".write": "auth != null"
    }
}

Is this what you're looking for?
Comment: Perfect, that was a lot easier than I thought - thanks friend!


Configure Apache with a htaccess file to strip out unneeded respond-headers

Question: For ultimate speed, I want my Apache server strip unneeded headers from the response.
Currently, the headers looks like this (excluding the status header):
Connection:Keep-Alive
Content-Length:200
Content-Type:text/html
Date:Sat, 15 May 2010 16:28:37 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.8l DAV/2 PHP/5.3.1 Phusion_Passenger/2.2.7
X-Powered-By:PHP/5.3.1

Which I want to be like:
Connection:Keep-Alive
Content-Type:text/html
Keep-Alive:timeout=5, max=100

I tried this:
Header unset Date
Header unset Server
Header unset X-Powered-By
Header unset Content-Length
Header unset X-Pad

But the server and date and content-length headers are still being sent.
How can I configure this in a .htaccess file? Thanks
Comment: Yes, and in relation to the actual content you are serving?
Comment: In what way would that improve what kind of performance?
Comment: @andol It's not much, but it uses less bandwith.
Answer: you could set ServerSignature Off and potentially avoid /that/ particular line.
and expose_php = Off in your php.ini to remove that line.
Answer: Date is required in HTTP/1.1.
You also can't remove Content-Length without switching to chunked transfer encoding or downgrading to HTTP/1.0 without keep-alive (but that's opposite of "ultimate speed").


How do I migrate a Kickstart site from one server to another?

Question: I created a site using Commerce Kickstart.
Then I added all the products and stuff. Now I want to move this site on a new server as it is.
I have downloaded all the files from the original site. Also exported the database.
Is it possible to use these files and database to migrate the site on new server with different domain name as well as different database name?
If no, what should I do?
Answer:
drush ard
(https://drushcommands.com/drush-7x/core/archive-dump/)
copy backup file to the new server
drush arr (https://drushcommands.com/drush-7x/core/archive-restore/)
done
Comment: Hi @Lóránt, welcome to Drupal Aswers. Thank you for your answer; could you elaborate a bit more instead of just posting the commands to use?
Answer: This should definitely be possible. Upload all of your files to your new server and create a new blank database.
Make sure to delete your existing settings.php file and create a new one from default.settings.php. You can find this file in sites/default/
Run the install script - yourdomain.com/install.php
Now upload your database to the new database.
Alternatively, you can upload your database to the new database and change the settings in settings.php manually.
Be careful to make the settings.php file secure by either running this: chmod 644 sites/default/settings.php on the command line or changing it via filezilla/your FTP client.
Answer:
First install the Backup and Migrate module into your existing site, then after enabling it, go to

/admin/config/system/backup_migrate

Click Backup now under the backup tab. Store this file
Now take backup of your files from existing site
Either rename settings.php to some other name or just remove it
Now upload the backed up files to desired destination
Just enter your new URL, it will take you to

yoursite.xxx/install.php

Now follow the usual method of installing your site (enter new database credentials)
You'll be redirected to a fresh and basic Drupal site
Now go to modules, enable Backup and Migrate module
Go to configuration page of the same, click the Restore tab, now upload previously downloaded file (using same module)
Click on Restore Now
That's it. Your site is restored!


CocoaHTTPServer2.3 Implicit declaration of function 'LOG_OBJC_MAYBE' is invalid in C99

Question: I use the last version of  pod 'CocoaHTTPServer', '~> 2.3' and xcode Version 8.1 beta (8T29o) and swift 3.0
When i install this pod i get this error on HTTPLogError  (and other method of HTTPLoging.h).
/Volumes/app/Project_ios/Pods/CocoaHTTPServer/Extensions/WebDAV/DAVResponse.m:102:9: Implicit declaration of function 'LOG_OBJC_MAYBE' is invalid in C99

Comment: I also have this same issue , and is not quiet solved. What I did was to removed all the cocoa lumber jack functions in cocoahttpserver pod (Not a good thing).

The issue I suspects is that cocoalumberjack have only support for xcode 8 and swift 3 . The backward compatibility is for xcode 7.3 and swift 2.3 .
Answer: I did the following steps to solve the problem:
Step 1: change #import "DDLog.h" to #import <CocoaLumberjack/CocoaLumberjack.h> in file HTTPLogging.h
Step 2: replace all LOG_OBJC_MAYBE with HTTP_LOG_OBJC_MAYBE and all LOG_C_MAYBE with HTTP_LOG_C_MAYBE in file HTTPLogging.h
Step 3: add the following macro in file HTTPLogging.h
#define HTTP_LOG_OBJC_MAYBE(async, lvl, flg, ctx, frmt, ...) \
do{ if(HTTP_LOG_ASYNC_ENABLED) LOG_MAYBE(async, lvl, flg, ctx, nil, sel_getName(_cmd), frmt, ##__VA_ARGS__); } while(0)

#define HTTP_LOG_C_MAYBE(async, lvl, flg, ctx, frmt, ...) \
do{ if(HTTP_LOG_ASYNC_ENABLED) LOG_MAYBE(async, lvl, flg, ctx, nil, __FUNCTION__, frmt, ##__VA_ARGS__); } while(0)

Hope my experience can give help.


What is the difference between EC and ECDSA in the OpenSSL EVP API?

Question: I'm working on a program in which I would like to generate an ECDSA key with OpenSSL's libcrypto EVP API. I found this piece of documentation that deals with this topic.
When specifying the key type, there is no EVP_PKEY_ECDSA, only EVP_PKEY_EC. The documentation says this is for ECDSA and ECDH keys.
However the parameter generation function EVP_PKEY_CTX_set_ec_paramgen_curve_nid only takes a curve NID (name of the used elliptic curve in OpenSSL). There is no way to tell this function that I want an ECDSA key. According to this documentation, there is no other EVP_PKEY_CTX_set_ec_ function, either.
I'm not an expert in cryptography, so I may not understand correctly.
Is an EC key the same as an ECDSA or ECDH key? The OpenSSL docs and terminology definitely suggest that, but do not say it explicitly. If they are not the same, how can I make sure to generate an ECDSA key?
Answer: Edit: didn't notice before answering, but near dupe Is there a difference between ECDH and ECDSA keys?
For Weierstrass curves, yes the same EC keypair can be used for either ECDSA or ECDH. (Or both, but that's not best practice, because in general you should not use one key for different purposes and signing and keyagreement are different purposes.) That same keypair can also be used for other elliptic-curve algorithms like ECMQV in things that implement those algorithms -- which OpenSSL doesn't.
If you use the key in conjunction with a certificate, which SSL/TLS protocols and CMS and S/MIME messages among other things do, then the certificate can impose restrictions on which operations (thus algorithms) use the key. But not everything uses certificates, and for those that do, nothing technically prevents you from having multiple certificates with different keyusage for the same key.
Bernstein's 'curve25519' uses different keys and algorithms, and is handled as a special case.
Comment: @Venemo For OpenSSL purposes, all but 25519 are Weierstrass. When X9/NIST/Certicom first standardized ECC ca 1999 they picked Weierstrass, and the first main competitor (Brainpool) didn't try to change it, so all of those curves are Weierstrass. Others have proposed alternatives, but none besides Bernstein has gotten any real acceptance. For his (rather dense) take on the situation see https://safecurves.cr.yp.to/equation.html
Comment: Thanks Dave, this clears things up for me! One more thing: which are the Weierstrass curves?


About totally umbilical hypersurfaces

Question: Suppose $\tilde{M} \subset M$ is a hypersurface sitting inside a Riemannian manifold $(M,g)$. The second fundamental form of $M$ evaluated on $u,v \in T_pM$ is denoted $II(u,v)$ and defined as the normal component of $\tilde{\nabla}_UV$ for some extensions $U$ and $V$ of $u$ and $v$. Define $l(u,v)$ by the relation $II(u,v) = l(u,v)\nu$ for $\nu$ a fixed unit normal vector field of $M$ around $p$.
I recently came across the following exercice : The hypersurface $M$ is said to be totally umbilical if $l = fg$ for $f \in C^{\infty}(M)$ and $g$ the restriction of the metric to $M$. It is asked to show that if $\dim M \geq 2$, then $f$ must be a constant.
The book says it is a consequence of the Gauss-Codazzi equation
$$\tilde{R}(x,y,u,\nu) = \nabla_xl(y,u) - \nabla_yl(x,u).$$
I tried playing around with this equation, taking traces hoping to find a Bianchi identity, but didn't have any success. Can anyone help me out?
Comment: The result could also be obtained using moving frames, if you're familiar with them.
Comment: @AaronS: can you please give a reference to the proof via moving frames method?
Answer: (1) If $\widetilde{M}={\bf R}^{n+1}$, then from Codazzi, we have $$
x(f) g(y,z) - y(f) g(x,z) =0$$
If $y=z\perp x$, then $x(f)=0$ That is $f$ is constant
(2) If $\widetilde{M}$ is general, then we have an counter example : $\widetilde{M}=({\bf R}^2\times {\bf R},
G:=F(x_3)^2g_1(x_1,x_2)+ g_2(x_3)),\ M={\bf R}^2$ Here $g_i$ are
canonical metric on each factor
If $E_i$ is coordinate vector field, then $\nu :=E_3 $ are
orthonormal And $$ \Gamma_{13}^1 = \frac{1}{2} G^{11} \{ G_{11,3} + G_{13,1} -
G_{13,1} \} =  \frac{F_3 }{F} = \Gamma_{23}^2   $$
$$ \Gamma_{13}^2 = \frac{1}{2} G^{22} \{ G_{12,3} + G_{23,1} -
G_{13,2}\} =0 $$
So $$ l(E_1,E_1)=(\nabla_1 E_3, E_1)=F^2\Gamma_{13}^1 =FF_3= l
(E_2,E_2) $$
And $$ l (E_1,E_2)=(\nabla_1 E_3,E_2)= F^2\Gamma_{13}^2 = 0 $$
Hence $$ f:=\frac{F_3}{F} $$
Answer: What about if the hypersurface is only $C^2$, so you cannot use Codazzi-Mainardi equations?

About totally umbilical hypersurfaces


EntityFramework StoredProcedure recuperar parametro output

Question: Como recuperar o parametro Output de uma storedProcedure?
SP Exemplo:
create PROCEDURE StoreProcedure_Name
    @ParametterWithNummvalue    varchar(50) = null,
    @In_Parameter varchar(50),
    @Out_Parameter varchar(10) out
AS

    set @Out_Parameter = 'teste'

RETURN 0

Código C#
SqlParameter[] Params = {
                new SqlParameter("@ParametterWithNummvalue", DBNull.Value),
                new SqlParameter("@In_Parameter", "teste novo"),
                new SqlParameter("@Out_Parameter", SqlDbType.VarChar, 10) { Direction = ParameterDirection.Output }};

            var r = Db.Database.ExecuteSqlCommand("exec StoreProcedure_Name @ParametterWithNummvalue, @In_Parameter, @Out_Parameter", Params);

            var ReturnValue = Params[2].Value;

ReturnValue deveria conter o valor "teste" mas está retornando vazio.
Comment: O que realmente você que retornar?  uma String o int?
Comment: @MarconcilioSouza desculpe, escrevi errado não é int é varchar
Answer: Olá, tente fazer da seguinte maneira:
//Separe os parâmetros de entrada e saída
var parametterWithNummvalue = new SqlParameter();
parametterWithNummvalue.ParameterName = "@ParametterWithNummvalue";
parametterWithNummvalue.Direction = ParameterDirection.Input;
parametterWithNummvalue.SqlDbType = SqlDbType.VarChar;
parametterWithNummvalue.Size = 50;
parametterWithNummvalue.Value = null; //Substitua pelo valor desejado

var inParameter = new SqlParameter();
inParameter.ParameterName = "@InParameter";
inParameter.Direction = ParameterDirection.Input;
inParameter.SqlDbType = SqlDbType.VarChar;
inParameter.Size = 50;
inParameter.Value = null; //Substitua pelo valor desejado

var outParameter = new SqlParameter();
outParameter.ParameterName = "@OutParameter";
outParameter.Direction = ParameterDirection.Output;
outParameter.SqlDbType = SqlDbType.VarChar;
outParameter.Size = 10;
outParameter.Value = null; //Substitua pelo valor desejado

var valorRetornado = new SqlParameter();
valorRetornado.ParameterName = "@ValorRetornado";
valorRetornado.SqlDbType = SqlDbType.Int;
valorRetornado.Direction = ParameterDirection.Output;

//Chame a procedure com base nos parâmetros criados
var dados = Db.Database.SqlQuery("exec @ValorRetornado = StoreProcedure_Name @ParametterWithNummvalue, @InParameter, @OutParameter OUT", valorRetornado, parametterWithNummvalue, inParameter, outParameter);

Obs: Perceba que da forma que sua procedure foi criada no exemplo, retornará dois valores, um no OUT (variável: outParameter) e outra no RETURN (variável: valorRetornado).
Espero ter ajudado.
Comment: ambas as variáveis (outParameter e valorRetornado) estão retornando null
Answer: Consegui resolver apenas adicionado a expressão Out após o parametro outParameter (resposta do TomCunha)
No final ficou assim:
SqlParameter[] Params = {
    new SqlParameter("@ParametterWithNummvalue", DBNull.Value),
    new SqlParameter("@In_Parameter", "teste novo"),
    new SqlParameter("@Out_Parameter", SqlDbType.VarChar, 10) { Direction = ParameterDirection.Output }};

var r = Db.Database.ExecuteSqlCommand("exec StoreProcedure_Name @ParametterWithNummvalue, @In_Parameter, @Out_Parameter Out", Params);

var ReturnValue = Params[2].Value;


Rails Convert Time String to Spoken Text String

Question: In Ruby/Rails, is there a built-in method or gem to convert a date string
<code>"Friday February 7, 3:30AM 2014"
</code>
to
<code>"Friday February seventh, three thirty AM two thousand fourteen"`
</code>
I wanted to see if anyone had a solution before I tried to roll my own.
Comment: https://rubygems.org/gems/humanize converts numbers to plain english, but doesn't handle ordinals.  I ended up writing my own methods, but hopefully this helps someone.
Answer: There is no built-in method or gem. You need to create your own.


handling non-node based items with ubercart

Question: I'm working on a book repository website very similar to Islandora which is based on  Fedora Commons and Drupal.
Books's information stored in repository outside Drupal. Apache Solr is configured on server and Drupal to search repository and display result in Drupal.
everything works fine, now I want make this books purchasable in Drupal.
As these search results are not nodes and there is not a node per book, I'm looking for a way to adding this non-node based items to cart!
any suggestion may be helpful!
http://www.ubercart.org/forum/support/14856/non_node_based_items
Answer: Having dealt with this exact circumstance before, we had a content type named 'bundle' which was purely a placeholder for the product details in Ubercart.
The user views the product and clicks 'add to cart' and in the cart is an instance of this 'bundle' with the product details in it.
Its far from a good solution and I would definitely be looking at creating a node for each of the products you have for sale - You could even create the nodes when 'added to cart' so you didnt need to add any in advance.
Ubercart has a great API for manipulating cart contents and line items, have you looked at that at all?
Comment: There are lots of functions that will assist you. Have a look at http://drupalcontrib.org/api/search/6/uc_cart. Note 'uc_cart_add_item' and 'uc_cart_get_contents/item' and 'uc_cart_update_item' and 'uc_cart_update_item_object'
Comment: can you provide links to useful parts of API?


Regex to find characters between find underscore and period

Question: I'm a total newcomer to Regex and wondered how I extract 0090203 from this String:
/src/test/resources/shopping_invoice/invoice_0090203.txt
The following gets me everything after the last underscore:
([^_]+)?\$")
I'm sure it must be simple :(
Comment: You can use: `\d+(?=\.)`
Comment: there are several ways and it depends on what you consider the granted pattern in such string.. anyway maybe one safe option could be `_(\d+)\.txt$` and grab the capturing group
Comment: Another way: `(?<=/invoice_)[0-9]+`
Answer: You can use
(?<=/invoice_)[0-9]+
[0-9]+(?=[.][^./]+$)
[0-9]+(?=[.]txt$)

See the regex demo #1, regex demo #2, regex demo #3
The (?<=/invoice_)[0-9]+ regex matches one or more digits that are immediately preceded with /invoice_.
The [0-9]+(?=[.][^./]+$) regex matches one or more digits that are immediately followed with . and then one or more chars other than . and / till end of string.
The [0-9]+(?=[.]txt$) regex matches one or more digits that are immediately followed with . and then txt at the end of string.


cannot connect to ftp server from lan?

Question: i am trying to make my ftp server accessible (for download only) by other computers on my LAN using anonymous user and without password, when i connect to it from the same pc using filezilla it works well but when trying from other PCs on the LAN it says
Status: Connection established, waiting for welcome message...
Error: Could not connect to server

iam using Ubuntu 11.04 and this is my configuration:
vsftpd.conf :
anon_root=/home/ftp
no_anon_password=YES
listen=YES
anonymous_enable=YES
local_enable=NO
write_enable=NO
anon_upload_enable=NO
anon_mkdir_write_enable=NO
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
chown_uploads=YES
ftpd_banner=Welcome to blah FTP service.
secure_chroot_dir=/var/run/vsftpd
pam_service_name=vsftpd
rsa_cert_file=/etc/ssl/certs/vsftpd.pem

the directory /home/ftp permission:
drwxr-xr-x  2 root     root     4096 2012-02-04 19:39 ftp

i think since it says "connection established" there is no problem with the iptables right?
and the selinux is not installed either! i have lost many hours on this problem :'( !! any help please..
Answer: Just because it says connection established does not mean there is no iptables issue. Try stopping your firewall first, since you are not saying if you are using active or passive ftp.
The other thing you can try is stopping apparmor (if installed).
/etc/init.d/apparmor stop

Comment: my firewall is off i forgot to mention that.. i used "ufw disable "
Comment: and since i didn't put pasv_enable=NO it is by default YES as i read in the man page, but yes i think it is ports issue because as i read the ftp uses 2 ports one for control and the other for data transfer and i think the problem is with the data transfer ports!

cannot connect to ftp server from lan?


Python Numpy get 2 Indexes which are not in a Blacklist

Question: Given a numpy-vector and a list of forbidden indexes. I need to return two indexes which are not in the list of forbidden indexes and both the values at the indexes are non zero and if this is not possible i need to return false.
a = np.matrix([[7885593,9648586,9182,14763,0]])
forbidden = [1,2]

So i need to return [0,3]
I thought about numpy.where() but this works only with values not with indexes.
Comment: have you tried working with an array of indices: `np.arange(a.shape(1))`?  Or a boolean like `np.ones((a.shape(1),),dtype=bool)`.
Answer: Your question is vague in places, so I'm making some guesses as to what you want.
Boolean masks are a nice way of 'removing' items from an array.
i=np.ones(5,bool)    # array of True
i[[1,2]]=False       # falsify the forbidden ones
np.where(i)[0][:2]   # get the first 2 true indices

producing:
array([0, 3], dtype=int32)


Why did Hapoel Be'er Sheva go through over Southampton in UEFA EL 2016-'17?

Question: I scrolled through some soccer leagues and I accidentally came across the following situation. It's about Hapoel Be'er Sheva and Southampton from Group K in the 2016-'17 UEFA Europa League. To start, they both got 8 point in the group stage. So now we're going to apply the tiebreaker rules:

higher number of points obtained in the group matches played among the teams in question;

The two matches between Hapoel Be'er Sheva and Southampton resulted in a 0-0 and 1-1, so in this case they each scored 2 points (because of the two draws).

superior goal difference from the group matches played among the teams in question;

The goal difference among Hapoel Be'er Sheva and Southampton is in both matches zero.

higher number of goals scored in the group matches played among the teams in question;

In these two matches, Hapoel Be'er Sheva has scored 1 goal and Southampton also has scored 1 goal.

higher number of goals scored away from home in the group matches played among the teams in question;

Again, Hapoel Be'er Sheva has scored 1 goal and Southampton also has scored 1 goal.

if, after having applied criteria 1 to 4, teams still have an equal ranking, criteria 1 to 4 are reapplied exclusively to the matches between the teams in question to determine their final rankings. If this procedure does not lead to a decision, criteria 6 to 12 apply;
superior goal difference in all group matches;

The superior goal difference of Southampton is +2 and that from Hapoel Be'er Sheva is 0.
Then why did Hapoel Be'er Sheva got through over Southampton if Southampton got a higher superior goal difference at rule 6? Did I miss something?
Answer: This is the criterion which decides it (emphasis mine).

higher number of goals scored away from home in the group matches played among the teams in question;

It is basically the same as the well-known away goals rule which is commonly used for two-leg fixtures.
If you look at the results in the matches between these two teams, Hapoel scored one goal in the away match (since the 1-1 draw was in Southampton). Southampton scored no goals away. (In the 0-0 draw was the match played in Beersheba, so Hapeol was the home team and Southampton was the away team.)


Android: adding CheckBox to a ListAdapter

Question: I'm wondering how to add (and control) a CheckBox field for my ListActivity's ListAdapter. Here is my code thus far which I use to set it up:
@Override
    public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.music);
<code>      String[] proj = { MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Video.Media.SIZE };

      musicCursor = this.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
      startManagingCursor(musicCursor);

        // THE DESIRED COLUMNS TO BE BOUND
      String[] columns = new String[] { MediaStore.Audio.Media.DISPLAY_NAME }; //, MediaStore.Audio.Media.SIZE
        // THE XML DEFINED VIEWS WHICH THE DATA WILL BE BOUND TO
      int[] to = new int[] { R.id.text1 }; //, R.id.text2 , R.id.musicChecked

      mAdapter = new SimpleCursorAdapter(this, R.layout.song_item, musicCursor, columns, to);
      // SET THIS ADAPTER AS YOUR LISTACTIVITY'S ADAPTER
      // i.e. the UI element
      setListAdapter(mAdapter);

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
        Toast.makeText(this, "You selected: " + Integer.toString(position), Toast.LENGTH_LONG).show();

}
</code>
//music.xml

<code> <ListView android:id="@android:id/list"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:layout_weight="1"
           android:drawSelectorOnTop="false"
           android:fastScrollEnabled="true"
           />

 <TextView android:id="@android:id/empty"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:text="No data"/>
</code>

//song_item.xml

<code> <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:paddingTop="5px"
     android:paddingBottom="5px">

     <CheckBox android:id="@+id/musicChecked"
        android:text=""
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        />

     <TextView android:id="@+id/text1"
         android:textSize="16sp"
         android:textStyle="bold"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:paddingTop="15px"
         android:paddingBottom="15px"/>

     </LinearLayout>
</code>

Right now what happens is that the list is properly generated, with the checkboxes and the song names. However, my onItemClick doesn't work with the CheckBox "musicChecked" added in (however if I remove it from XML then I see the Toast). I want to be able to click on a field and have the CheckBox toggle true/false.
Thanks for any help guys.
Answer: Really, it's  just a matter of catching the event.  Set your checkbox to
<code>android:clickable="false"
android:focusable="false"
</code>
in your xml layout.  Then just handle the selecting and unselecting of the checkbox in your existing onListItemClick method with the inherited setChecked() method.
Comment: Alright so I set the checkBox clickable to false, but I'm still not catching the event for some reason. What do you think?
Comment: Sorry!  I forgot to mention that you also need to set android:focusable="false" in your xml.  Otherwise, the checkbox grabs focus from the list item.  I updated my answer to reflect this.


Como funciona o polimorfismo de sobrecarga em Python?

Question: Recentemente aprendi POO em Java e agora estou aprendendo em Python. Há polimorfismo de sobrecarga (implementar métodos com nomes iguais na mesma classe, que dependendo dos parâmetros que você entrega para o método, ele chama um ou outro)?
Caso não haja, qual seria a alternativa a isso?
Answer: Python não tem. Não faz sentido em uma linguagem de tipagem dinâmica. A sobrecarga depende da quantidade de parâmetros e seus tipos para formar uma assinatura. O Python só tem o nome na assinatura, então não é possível ter este mecanismo. Mesmo com as anotações de tipos das versões mais novas, é uma camada muito superficial só para auxiliar a robustez, a tipagem continua sendo dinâmica.
A alternativa é usar um nome diferente dando uma semântica específica nele para diferenciar de outro método.
Coloquei no GitHub para referência futura.
Answer: Por padrão não há isso em Python  - Sendo uma linguagem dinâmica a abordagem é completamente diferente:
Tudo em Python é um objeto derivado da classe object - e a linguagem não checa o tipo de nenhum argumento em nenhuma chamada de função (mas a quantidade, e nome dos argumentos, sim).
Então num exemplo simples:
def soma(a, b):
   return a + b

Essa função vai ser chamada para quaisquer valores de a e b. Se forem números, eles serão somados. Se forem texto (string), a string será concatenada (já que para strings, o operador "+" é sobrecarregado para efetuar concatenação), e o método funciona do mesmo jeito.
Já se você passar objetos que não implementam a operação de soma com o operador "+" (para suportar o operador, a classe do objeto tem que ter um método com o nome especial __add__), você obterá um TypeError em tempo de execução.
Isso é, em Python a linguagem não faz verificação de tipagem, e te dá flexibilidade para executar as mesmas operações e chamar funções com vários tipos de objeto diferentes, desde que eles "funcionem" da forma que a função chamada espera (nesse caso da soma um dos dois tem que ter um método __add__ ou __radd__ que reconheça o outro objeto passado).
Nada impede que você faça a checagem manual dentro do método ou função com if usando isnstance ou issubclass para determinar se os objetos passados tem os comportamentos necessários implementados - mas isso raramente é feito. De qualquer forma, a recomendação é testar o comportamento, e não a classe - por exemplo, vamos supor uma função que receba um objeto que pode ou não conter texto, mas se contiver, deve tentar converte-lo para maiúsculas com seu próprio método .upper() -  a forma poderia ser:
def generate_slug(obj):
    if hasattr(obj, "upper"):
        obj = obj.upper()
...

Em detrimento de:
def generate_slug(obj):
    if isinstance(obj, str):
        obj = obj.upper()
    ...

Com isso você resolve tudo o que conseguiria com polimorfismo variando os tipos dos argumentos passados. Agora há uma outra característica realmente muito útil em Python que são os parâmetros opcionais:
def soma3(a, b, c=0):
    return a + b + c

Veja nessa declaração - com o = é especifícado um valor padrão para  c: ou seja soma3 pode ser chamado com 2 ou 3 argumentos. Se só dois forem usados, c retem o valor 0. Caso seja passados 3 valores, o terceiro é usado para c.
Há ainda sintaxes que permitem um número indeterminado de parâmetros em sequência (para os quais não importa o nome, apenas a ordem), e uma quantidade indeterminada de parâmetros com nome -
def soma_n(*args):
   acc = 0
   for arg in args:
      acc += arg
   return acc

soma_n(5,6,7,8,9,10)

Sugiro buscar uma leitura suplementar sobre parâmetros com nome, e com mais exemplos de uso de *args e **kwargs  - são tópicos bem importantes mas não diretamente relacionados com a questão do polimorfismo.
Então até aqui temos que: uma única função pode receber argumentos de tipos variados, e um número variado de parâmetros - em contraste com linguagens estaticamente tipadas implementando polimorfismo, onde é necessário uma declaração de função para cada variante possível de tipagem/número de parâmetros.  (Tente imaginar o soma3 acima podendo funcionar com combinações distintas de inteiros, números float, números decimais, fração, e cada uma dessas com 2 ou 3 parâmetros - o número de implementações necessárias cresce de forma geométrica. Ok, números podem descender de uma classe abstrata "Number" - mas dá pra pegar a ideia).
Agora sim, há ocasiões em que é mais elegante definir as funções separadamente para tipos distintos de parâmetros, do que separar internamente por if. Para isso,a biblioteca padrão tem um recurso de "singledispatch" que seleciona automaticamente um corpo de função dependendo do tipo do primeiro parâmetro.  No modo interativo pode se escrever:
In [15]: import functools

In [16]: @functools.singledispatch
    ...: def soma(a, b):
    ...:     pass
    ...:

In [17]: @soma.register(int)
    ...: def s(a, b):
    ...:     return f"{a + b}  integer"
    ...:

In [18]: @soma.register(str)
    ...: def s(a, b):
    ...:     return f"{a + b} string"
    ...:

In [20]: soma(3,4)
Out[20]: '7  integer'

...

Note que isso está na biblioteca padrão, mas a natureza dinâmica de Python aliada a possibilidade de escrever funções de ordem mais alta - que nesse caso são usadas como decoradores, prefixando funções com a sintaxe de @ - permite que um projeto crie seus próprios métodos para fazer um dispatch para outros métodos/funções automaticamente - e isso não é complexo quando se entende bem a linguagem.
Por fim, mas não menos importante, vale a pena mencionar que embora essas características dinâmicas de Python sejam muito legais para código de baixo acoplamento, e escrita rápida de funcionalidades, em projetos maiores, com equipes grandes, podem atrapalhar - por isso já há alguns anos Python vem evoluindo uma especificação de anotação de tipos, que não força argumentos de um determinado tipo em tempo de execução, mas permite que ferramentas auxiliares verifiquem a tipagem usada nas chamadas em uma etapa de checagem (commit hooks, rodar testes, etc...).  Com isso se padroniza uma sintaxe introduzida no Python 3.0 que permite a especificação do tipo de um parâmetro usando-se ":" na declaração de uma função:
In [28]: from numbers import Number

In [29]: def soma3(a: Number, b: Number, c: Number=0):
    ...:     return a + b + c
    ...:

Deixa explícito para as ferramentas de checagem estática (algumas podem ser usadas de forma transparente por uma IDE, por exemplo), que a idéia é que essa função só receba tipos numéricos. Em tempo de execução no entanto, a linguagem não deixaria de chamar essa função se for invocada com strings, por exemplo. (Mas sim, seria possível criar uma ferramenta que desse erro automaticamente nesse caso - mas com isso: (1) tudo ficaria mais lento e (2) você perde as vantagens de estar usando uma linguagem dinâmica - você poderia usar Cython, por exemplo que realmente pode tirar vantagens otimizando a execução ao se declarar alguns dos tipos de parâmetros)
Answer: Complementando a resposta escolhida:
Realmente esta questão de sobrecarga não faz sentido considerando a tipagem dinâmica do Python. Contudo a PEP 484 introduz o decorador @overload no módulo de tipagem do Python. Este decorador funciona da seguinte maneira:
Você pode definir diversos tipos de chamadas para sua função decoradas com @overload e essas definições devem ser seguidas por uma definição padrão da sua função sem o decorador.
from typing import overload

@overload
def process(response: None) -> None:
    ...
@overload
def process(response: int) -> tuple[int, str]:
    ...
@overload
def process(response: bytes) -> str:
    ...
def process(response):
    ...  # implementação de fato é feita aqui

Este tipo de implementação é útil apenas para os checadores de tipagem e torna a declaração mais explícita, mas ao ser executado, qualquer implementação realizada nas declarações com o decorador serão sobrescritas pela declaração final sem o decorador.
Para mais informações verifique a PEP citada e/ou a documentação sobre o @typing.overload


undefined_quantity for paypal subscription button

Question: I want to define a Paypal button with circular payment (once a month), but with custom quantity. For example, if I choose 1 items I will pay 1$ per month, if I choose 2 items I will pay 2$ for month etc..
Is there any way to do it?
Answer: This is taken from a subscription button that allows the customer to choose between 3 options.

<input name="on0" value="Monthly Autoship" type="hidden">

<select name="os0">
<option name="item_name" value="1 Pro">1 Pro: $88.84</option>
<option name="item_name" value="2 Pro">2 Pro: $161.29</option>
<option name="item_name" value="3 Pro">3 Pro: $235.87</option>

It's just a normal dropdown selection.


Help with a Minecraft Piece Analysis?

Question:
So I did an anlysis for a Minecraft Piece for a video I'm working on. I was wondering if there were any glaring errors?
I called the chords that had arpeggiated the 9th Maj9 chords, but for some that just had them as passing tones I decided it best to analyze those as non chord tones.
The brief modulation at the end, should that be a modulation or just a secondary subdominant moving to a secondary dominant? Also did I notate that modulation correctly? I was pretty sure it was a direct modulation so I went ahead with that and just said it modulated on the scale degree.
Thanks in advance.
Comment: I'm sure some other readers will chip in with a full appraisal of your analysis and/or other points, but I just thought I'd make a comment about sus2 chords: a sus2 chord doesn't have a third, as the suspended second is a suspension OF the third. Chords with seconds like these are either add9 chords (if there is no 7th) or 9th chords (if there is a 7th). You may benefit from searching this site, or elsewhere online, for further info about suspended chords and how tertian harmony influences chord naming.
Comment: I just went ahead and made some of the changes, fixing the "sus2" chords and calling them 9 chords properly. How does it look now?
Comment: I was wondering if my notation for the ii(9) was correct, since it'sr a minor 7th but a major 9th? Also, is how I did the direct modulation correct, or is that even a direct modulation? Is it just IV/IV going to V/IV?
Answer: Maybe I'm a little bit late, but I don't think there is a modulation at all. After he plays G major, he never resolves to D. Instead, he always goes back to A. There is no moment where D feels like the tonal center.
Also, you got a little confused in 15. Bm9 is ii9 in relation to A, not D. Considering the modulation, vi9 would be right.
Having said that, I would:

Rip out the modulation;
Change 9, 10, 11, 12, 17 and 21 to VIIb (maj9 or add9);
Change 13, 14, 18 and 22 to I.

Help with a Minecraft Piece Analysis?


Diagonal Perturbations of Linear Equations

Question: Consider the system of linear equations (A+D)x=b, where D is a positive semidefinite diagonal matrix. In my particular case of interest, D has the form D = blkdiag(0,M) for some positive diagonal matrix M. So, a subset of the diagonal entries of A are being perturbed.
Are there any off-the-shelf theorems that characterize how the components of the solution x change as the elements of D change? Assuming you don't lose full rank, intuitively as M increases in size, the bottom elements of x should shrink. Coupling through A should then also shrink the top elements a little bit.
Thanks
-John
Comment: Do we know anything about the matrix A?
Comment: It's full rank and indefinite, so for arbitrary D as I've written it, the matrix A+D is potentially singular. But let's say we restrict our attention to sufficiently small or large D such that this isn't a problem.
Answer: Depends on the bookshelf of whom ;-D
On my bookshelf, there is a copy of Stewart and Sun, Matrix Perturbation Theory, Academic Press Inc., San Diego, 1990. By Theorem 2.11 (p.124), if your $D$ is small enough and $Ax=(A+D)\hat{x}=b$, then the relative difference between $\hat{x}$ and $x$ is bounded as follows:
$$
\frac{\|\hat{x}-x\|}{\|x\|}\le\frac{\kappa(A)\frac{\|D\|}{\|A\|}}{1-\kappa(A)\frac{\|D\|}{\|A\|}},
$$
where $\kappa(A)$ is the condition number of $A$.
The above theorem deals with the overall relative or absolute change in $x$ rather than the change in any particular entry of $x$, so I am not sure if it suits your needs or not. At any rate, your intuition that the bottom elements of $x$ might shrink is wrong. For instance, consider the simple case where $A=\begin{pmatrix}1\\&-1\end{pmatrix},\ b=\begin{pmatrix}1\\-1\end{pmatrix},\ x=\begin{pmatrix}1\\1\end{pmatrix}$ and $D=\begin{pmatrix}0\\&\epsilon\end{pmatrix}$ with $\epsilon>0$. The solution to $(A+D)\hat{x}=b$, or equivalently, $\begin{pmatrix}1\\&-1+\epsilon\end{pmatrix}\hat{x} = \begin{pmatrix}1\\-1\end{pmatrix}$, is given by $\hat{x}=\begin{pmatrix}1\\ \frac{1}{1-\epsilon}\end{pmatrix}$. When $\epsilon$ increases, the bottom element of $x$ becomes larger, while the top element remains unchanged.