Incorrect File Permissions When Images Are Uploaded in the CKEditor WYSIWYG using ElFinder on Windows

Question: When I upload and image in the WYSIWYG, the image is added to the /sites/default/files/ folder, but cannot be seen on the web becasue IIS_IUSRS does not have the correct file permissions. Also, the permissions are only viewable by an administrator.
I tried the answer given on IMCE image upload and permissions issue, but both the temp and destination folders give IIS_IUSRS and IUSRS read/write/modify. I have even tried temporarily giveing 'Everyone' full control to get this working, but that does not help.

First, I have to click advanced to view the uploaded files permissions. I don't have to do that on the folders or other files in the folder.
Then I have to give Administrator permission. Again, this is not required on the folders or other files in the folder.

Then I can see the file permissions and IIS_IUSRS has List folder/read data, not modify. Also, this is inherited from 'Parent Object' not the parent folder.

If I click Disable inheritence and remove all permissions, then click enable inheritrance and apply, the correct permissions re inherited and the image can be seen on the web.

Has anyone else experienced this? Why doesn't the uploaded file inherit the folder permissiosn correctly in the first place? Why do I have to disable then enable inheritence to get it to work?
I need to fix this because the web editors will be adding a lot of images and it is not practical for me to manually change the permissiosn every time.
I don't know if this is an error with Windows Server 2012, IIS 8.5, CKEditor or ElFinder.
I am using ElFinder, but was only able to tag this with CKfinder. I am using Plupload as well, but can't remember if it integrates with ElFidner or not.
P.S. This site is stil in development. I will remove 'Everyone' before it goes live.
Answer: I was able to correct this by enabling inheritance on the c:\windows\temp folder, after verifying that the IIS_IUSRS (machinename) account has read & execute/list folder contents/read permissions.  I was prompted about a potential security risk, but ultimately this fixed it.
Comment: Sorry for the delay responding - I was away. That worked. Thanks!


User with multiple accounts

Question: What's the community's take on users with multiple accounts?
The case I have in mind concerns a user (ID1/ID2/ID3) who frequently asks homework-type questions (some with shown effort, others without). The user also frequently cross-posts on other SE platforms and deletes his/her own questions that either got excessive downvotes or received no answers. Also, one of his/her accounts on MSE is under temporary suspension "for voting irregularities".
As far as I can tell, the multiple accounts are created not to game the SE reputation system, but to ask questions. The user appears to be a genuine learner, has been very polite and caused no noticeable disruption to the community. However, the type and number of questions that this user asks do raise suspicion that he/she may have been trying to outsource a lot homework question here.

Update on May 9.
Two new IDs are detected (ID4/ID5). These IDs also posted quite a few low-effort / low-quality questions in the past 24 hours:

Homothetic preference and the price elasticity of demand
Show that First order conditions are necessary and sufficient for utility maximization.
Question about budget constraint and utility maximization
Inferior and normal good and the change in price of those goods
optimization problem with Kuhn-Tucker conditions
Comment: It's ridiculous the way he's [editing his own questions with a different account](https://economics.stackexchange.com/posts/21945/revisions). It's almost as if he didn't bother to log out of his other account, but still wanted to make an edit.
Comment: @denesp: I flagged it for moderator attention. The user also posted several such "please see my other question" comments under answers to his previous questions.
Comment: @denesp: From the pattern of his questions (a bunch of micro followed by a bunch of macro, and now micro again), it looks like he's preping/taking some sort of qualifying/comprehensive exams. It doesn't help that most of his questions receive answers, which in turn encourages more of such questions.
Comment: This user's "can you please see my related question" [answer](https://economics.stackexchange.com/a/22015/1601) to an old question just got an upvote from 'someone'.
Comment: Now he has at least one [new account](https://economics.stackexchange.com/users/18566/b11b) with a "." at the end.
Answer: The network consensus seems to be that multiple accounts are okay, provided they are not being used to game the system (e.g.,  by voting for your own posts). See, e.g., How should sockpuppets be handled on Stack Exchange?
Thanks for flagging the particular case: the mods have tools to check for sock-puppet voting and indeed it seems to be going on here.
Comment: Thank you for the feedback. Also see my update in the question.

User with multiple accounts


--skip-grant-tables option cannot execute statement

Question: I'm creating a mysql database via the CLI when I run this
command line
Create user meriemPFE;

he made me get this error message, which I could not resolve.

the server is running with the --skip-grant-tables option can not execute this statement.

EDIT:
yes I put it in the file C: \ xampp \ mysql \ bin \ my.ini
this is a portion of code
[mysqld]

skip-grant-tables
port= 3306
socket = "C:/xampp/mysql/mysql.sock"
tmpdir = "C:/xampp/tmp"
datadir = "C:/xampp/mysql/data"
pid_file = "mysql.pid"

Comment: Did you intentionally set `--skip-grant-tables` on?
Comment: WHY are you using `skip-grant-tables` It shoudl only be used for specific situations like recovering a lost root password. It is INCREDIBLY insecure to run with this set for normal processing as anybody is allowed to login without any password or credential checking
Comment: It is simpler just to use `phpMyAdmin` or the `mysql.exe` to reset a password. Only use this IF its the `root` user account that you have lost the password for.
Comment: Try `CREATE USER meriemPFE IDENTIFIED BY SomePassword;`
Comment: yes I put it in the file C: \ xampp \ mysql \ bin \ my.ini

this is a portion of code ' [mysqld]
skip-grant-tables
port= 3306
socket = "C:/xampp/mysql/mysql.sock"'
Comment: and i tried CREATE USER meriemPFE IDENTIFIED BY SomePassword; , same error message
Comment: I added that to your question. Using the `edit` link under your question. That is also what you should use in the furture. specially if a comment contains any code. As it is almost unnreadable in a comment :)
Comment: I use it to fix access denied for user
Answer: In mysql, verify executing the flush privileges command.

mysql> flush privileges

This action solved the error in my case.

the server is running with the --skip-grant-tables option can not
execute this statement.

I hope this helps you too. Health!
Answer: As of recent versions of MySQL/MariaDB starting with --skip-grant-tables doesn't work to reset the mysql root user password.
As per MySQL docs you now need a different process to reset the mysql root user.
Assuming a Linux machine running MariaDB as root (if not get root with sudo su root).
Then create a file somewhere that the mysql user can read. I use /var/lib/mysql/mysql-init (but it can be anywhere really). In that file put the following:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

(Obviously change MyNewPass to be your desired password).
As noted the mysql user must have read access to this file. To ensure no other users can read it I recommend chowning it and set permissions to read only for all other users. I.e.:
chown mysql:mysql /var/lib/mysql/mysql-init
chmod 0400 /var/lib/mysql/mysql-init

Then stop MariaDB, assuming systemd:
systemctl stop mariadb

Then start it again from CLI:
mysqld --init-file=/var/lib/mysql/mysql-init &

(note other options may be required, but just like that works for me)
Once it has successfully started, then kill it again:
kill $(pgrep mysqld)

And restart the service:
systemctl start mariadb

And you should be good to go with your new password!
I also suggest cleaning up the mysql-init file:
rm /var/lib/mysql/mysql-init

If like me you want to change to use the unix_socket then:
mysql -e "ALTER USER root@localhost IDENTIFIED VIA 'unix_socket';"


É possivel lançar um erro que desencadeia outros?

Question: Segue um exemplo:
public class Teste
{
    public static void main(String[] args)
    {
        func1();
    }

    public static void func1()
    {
        try
        {
            func2();
        }
        catch (Exception e)
        {
            System.err.println("Error 1\n");
        }
    }

    public static void func2()
    {
        try
        {
            int x = 1 / 0;
        }
        catch (Exception e)
        {
            System.err.println("Error 2\n");
        }
    }
}

Saída:

Error 2

É possível fazer com que assim que ocorrer o erro na func2 ele printe Error 2 e desencadeie esse erro para a func1 printando Error 1 como no exemplo de saída abaixo?

Error 2Error 1
Comment: Uma dúvida, isso é só curiosidade ou você pretende implementar isso em algum lugar?
Comment: Leitura relacionada: [Como melhor tratar exceções em Java?](http://pt.stackoverflow.com/a/21939/28595)
Comment: Pergunto porque eu vejo isso como uma péssima pratica, a exceção é pra ser usada quando algo inesperado acontece. O melhor é sempre procurar tratar todos os erros, ao inves de pilhar exceção em cima de outra.
Comment: Mesmo assim, esta forma vai te complicar futuramente, dependendo da complexidade. Da uma olhada na pergunta que linkei, o post é bem esclarecedor, vale a pena :)
Comment: Estou estudando DB, minha classe tem várias funções e todas elas tem tratamento de erro, porem em alguns casos uma função chama a outra e se der erro na ultima função haverá problemas em alguns objetos no final
Answer: Sim, é possível, basta relançar a exceção:
class Teste {
    public static void main(String[] args) {
        func1();
    }

    public static void func1() {
        try {
            func2();
        } catch (Exception e) {
            System.out.println("Error 1");
        }
    }

    public static void func2() {
        try {
            int x = 1 / 0;
        } catch (Exception e) {
            System.out.println("Error 2");
            throw e; // <==================== relançou aqui
        }
    }
}

Veja funcionando no ideone. E no repl.it. Também coloquei no GitHub para referência futura.
Só espero que esteja capturando Exception como um teste rápido. Eu gosto de responder essas questões sobre exceções, mas sempre temo pelo uso. A maioria dos programadores abusam delas.
Veja se realmente tem que capturar a exceção e lançá-la novamente. Raramente isso é necessário. O que mais vejo é fazerem isso por erro. Tem caso que precisa tratar a exceção em mais de uma passo, mas não é tão comum assim. Na maioria dos casos é, ou trata tudo, ou deixa para outro local tratar.
Comment: Sim eu li [Como melhor tratar exceções em Java?](http://pt.stackoverflow.com/questions/21933/como-melhor-tratar-exce%C3%A7%C3%B5es-em-java/21939#21939), foi apenas um teste rápido.


Erro na conexão com banco de dados

Question: Bom dia!
Estou com um painel em php e o mesmo não está fazendo conexão com a DB.
Qualquer senha que eu digite, da senha ou usuário incorreto.
Segue o trecho do código que faz a conexão:
<?php
session_start();
$usuariot = $_POST['usuario'];
$senhat = $_POST['senha'];
include "conexao.php";

$result = mysqli_query("SELECT * FROM usuario WHERE login='$usuariot' AND senha='$senhat' LIMIT 1");
$resultado = mysqli_fetch_assoc($result);
if(empty($resultado)){
    $_SESSION['loginErro'] = "Usuário ou senha inválido";
    header ("Location: login.php");
}

?>

E em baixo o trecho do código que faz a conexão ao banco de dados:
<?php
$conectar = mysqli_connect("localhost","root","") or die ("Erro na conexão");
mysqli_select_db($conectar, "painel_admin");
?>

Desde já agradeço!
Comment: Lembra que comentei ontem na outra pergunta? no `mysqli_query()` você precisa passar a conexão como primeiro argumento sempre.
Comment: Você quer dizer assim:  $result = mysqli_query("localhost","SELECT * FROM usuario WHERE login='$usuariot' AND senha='$senhat' LIMIT 1");
Comment: Assim: `mysqli_query($conectar, "SELECT * FROM usu....").`
Comment: Obrigado rray! Deu certo
Answer: Segue um exemplo
Conexão
$mysqli = new mysqli($servidor, $usuario, $senha, $banco);

if (mysqli_connect_errno()) trigger_error(mysqli_connect_error());

SEU SELECT
$sql = "SELECT * FROM usuario WHERE login='$usuariot' AND senha='$senhat' LIMIT 1";
$query = $mysqli->query($sql);

$resultado = mysqli_fetch_assoc($query);
if(empty($resultado)){
    $_SESSION['loginErro'] = "Usuário ou senha inválido";
    header ("Location: login.php");
}

Comment: Agora deu certo! muito obrigado a todos!
Comment: @PedroRibeiro Caso a resposta tenha te ajudado, poderia marcar como resolvido e dar +1
Comment: já coloquei Rafael, porém sou novo mebro, ai não aparece.

Erro na conexão com banco de dados


Sum over (squares of) Laguerre Polynomials

Question: I'm looking for a closed form of the sum \begin{equation}
\sum_{n=0}^\infty \frac{n!}{(n+k)!} (L_n^k(x))^2 t^n,
\end{equation}
where $L_n^k(x)$ are the Laguerre Polynomials. I have been looking for some time and only found equations that are pretty close, but not exactly what I need (like here).
Comment: Welcome to MSE! Please review the [Meta Read](https://math.meta.stackexchange.com/questions/9959/how-to-ask-a-good-question/9960#9960) and enhance your question to provide your motivation/attempts.
Answer: Go the wikipedia article for Laguerre polynomials and search for 'Hardy-Hille' formula near the end of it.  Put $\alpha=k$ and $y=x.$


Php foreach sorting with three variables

Question: I am trying to use a foreach loop to print out links which are sorted after one of the three variables in an array, $term->description. The value of $term->name is the text inside the link element. How could I add a href attribute to a link by using the third variable $term->slug in the same foreach loop? I have tried to make two separate arrays and use array_combine, but to no avail.
Here is the code working for two variables. To make things clear I have simply added $term->slug added at its desired location.
$terms = wp_get_object_terms( $post->ID, $taxonomy );
$a = array();
foreach ($terms as $term ) :
    $a[] = array( $term->name, $term->description, $term->slug);
endforeach;

// print_r($a);
// Array ( [0] => Array ( [0] => Los Angeles [1] => City [2] => los-angeles ) [1] => Array ( [0] => San Francisco [1] => City [2] => san-francisco ) [2] => Array ( [0] => San Diego [1] => City [2] => san-diego ) [3] => Array ( [0] => United States [1] => Country [2] => united-states ) )

foreach ($a as $key => $value) {
    $return[$value[1]][] = $value[0];
}

foreach ($return as $key => $value) {
    echo $key.": slug'>".implode(',slug">', $value)."";
}

// Outputs:
// City: Los Angeles  San Fransisco  San Diego
// Country: United States

Comment: Can you give me the desired html output. $term->slug will not work as $term is in first foreach loop.
Answer: Code :
<?php
  error_reporting('E_ALL');

  $a = Array(Array("Los Angeles","City","los-angeles"),Array("San Francisco","City","san-francisco"),Array("San Diego","City","san-diego"),Array("United States","Country","united-states"));

  foreach ($a as $key => $value) {
      $return[$value[1]][] = array("name"=>$value[0],"slug"=>$value[2]);
  }

  foreach ($return as $key => $value) {
    echo $key.":";
      foreach($value as $newvalue){
       echo ''.$newvalue["name"].' ';
    }
    echo "
";
  }

Output:
City:Los Angeles San Francisco San Diego
Country:United States

Comment: Great answer. It didn't occur to me how straightforward it was to nest another foreach loop inside the first one. Thanks a lot!
Comment: Happy to help always.


What is used as tie-breaker in mountain classification?

Question: If Mikel Landa finished 2nd in the yesterday's (30 May 2015) stage of Giro d'Italia, both him and Giovanni Visconti would have 125 points in mountains classification. The last stage is flat, no points in mountain classification are given. So in this scenario they would finish both with the same number of points.

What is used as a secondary criterion if two riders have the same number of points in mountain classification?
Are the rules the same for all Grand Tours (all races in UCI World Tour)?

Bonus question: Has it ever happened in some important stage race that the winner of mountain classification has to be decided by some additional criterion, as two (or more) riders were tied on points?
Answer: For most multi-day tours the rules are similar. The winner is determined in the following order:

Most points for the mountain classification
Most first places at Hors Categorie climbs
Most first places at first categorie climbs
And so on...
If both riders have finished first on the exact same distribution of mountains. That is, there is still no decisive winner. The rider that has best general classification wins the mountain jersey.

See for example Wikipedia or the 2012 Tour de France rule book.
Answer: The following reference is from "Tour de France for Dummies".  However, it is applicable for all the Grand Tours and the large majority of other major tours.

If there's a points tie in the final climbing classification, the cyclist with the most first-place finishes in Beyond Category climbs wins.  If there is still a tie, victory is awarded to the rider with the most first-place finishes in Category 1 climbs.  The same process is used, if necessary, in Categories 2, 3 and 4.
Answer: I will add quote from UCI cycling regulation - Part II: Road Races (version on 05.02.2015), which seems to confirm what was written in the other answers:

2.6.017
  In the event of a tie in the general individual classification by points, the following criteria shall be applied in order until the riders are separated:

number of stage wins;
number of wins in intermediate sprints counting for the general classification on points;
general individual classification by time.

In the event of a tie in the general individual mountains classification, the following criteria shall be applied in order until the riders are separated:

number of first places in the highest category climbs;
number of first places on climbs in the next inferior category and so on;
general individual classification by time

(text modified on 1.01.02; 1.01.03; 1.01.05).


Can I use windows inside a docker container?

Question: I'm wondering, is it possible to configure a docker container to run Windows and then connect to it via RDP or parsec and use it as my main machine?
Answer:
is it possible to configure a docker container to run Windows

Yes, you can Run native Windows Server Containers

connect to it via RDP

Nope.
According to an answer, Windows Containers does not support RDP. To quote an article from 2019:

But notice you still cannot RDP into such Windows containers.

See What's new for Docker on Windows Server 2019?
Answer: Well, yes, you can run containers with Windows. Microsoft even provides official container images, but don't expect them to be as versatile as Linux images. I don't have any experience with them because I didn't have any use for it yet.
From what I gathered you need a Windows host to run the containers, they don't run on Linux hosts (please correct me if I'm wrong here). You won't have a desktop either. If you manage to rdp into it (which I doubt is possible) you will get a command shell at most.
The containers are meant to run powershell scripts or applications without gui, nothing more.


Foco em um campo com maskedinput

Question: Bom dia, pessoal...
Tenho em meu form um campo text com entradas recursivas. Como faço p/depois de enviar um valor digitado, limpa-lo e deixar pronto para a próxima entrada ? Assim que teclo o primeiro caracter da proxima entrada ele apresenta o valor já digitado anteriormente...meu código está assim :
if(valor == 0){
  alert("Digitou zero.");
  document.frmsaidadoscor.txtnumdo.focus();
}else{
  dos.push(valor);
  txtnumdo.value='';
  cont = cont + 1;
  document.frmsaidadoscor.txtnumdo.focus("");
}

Answer: Para limpar o valor do campo, basta você anular o seu valor antes de realizar o foco. Já para o autocomplete do browser você pode tentar o autocomplete="off", mas alguns ignoram essa instrução.

let cont = 0;
let dos = [];
let input = document.getElementById('txtnumdo');

let salvar = function() {

  let valor = input.value;
  if (valor == 0) {
    alert("Digitou zero.");

  } else {
    dos.push(valor);
    txtnumdo.value = '';
    cont = cont + 1;

  }

  input.value = null;
  input.focus();

  console.clear();
  console.log(dos);
}
<form autocomplete="off">
<input id="txtnumdo" type="text" autocomplete="false" />
<input type="button" onclick="salvar()" value="Salvar" />
</form>
Comment: Grato, colega....funcionou...
Comment: @Ninja2112, se a solução apresentada resolveu o seu problema você deve aceitá-la como resposta. E é importante ao fazer uma pergunta sempre se preocupar em apresentar um [MCVE]

Foco em um campo com maskedinput


Cocos2d-android detecting bounding box of sprite

Question: Is it possible to detect collisions with certain parts of a sprite's bounding box?
For example if a sprite jumps and landed into a platform, the bottom part of the sprite bounding box's and the top part of the platform bounding box's collision will be detected.
Answer: In my projects I have a helper library that has lots of little functions including something like:
CCRect CHelperMethods::InsetRect(const CCRect rectToInset, const float insetValueX, const float insetValueY) {
return CCRectMake(rectToInset.origin.x + insetValueY, rectToInset.origin.y + insetValueY, rectToInset.size.width - insetValueX * 2, rectToInset.size.height - insetValueY * 2);
}

Then, for example with a sprite that has landed, it would have a State variable, and I would say (psuedo code)
if(sprite.getState() == constStateLanded) {
    CheckCollisionForBox( CHelperMethods::InsetRect(sprite->boundingBox(), yourXOffset, yourYOffset));
}

HTH, as you tagged Android and ipHone I didn't know whether you wanted Obj-C or C++ :)
Comment: I tagged Android only and java for the language but thanks for your help! I'll try to use your suggestion. :)
Comment: Ah, I guess the other edit added the tags :). SAme thing applies in Java, though


If a baseball team takes a large early lead, would it make sense to substitute an inferior pitcher to save the better one?

Question: The Pittsburgh Pirates took an early lead of 5-0 against the Chicago White Sox in the first inning. Their pitcher was their ace, Francisco Liriano.
With such a large lead, can the Pirates afford to substitute an inferior pitcher, say Vance Worley and save Liriano for a later game in the (four game) series? (This would be on the theory that even Worley wouldn't blow such a big lead.) Or would such a move throw the whole rotation out of whack?
As it were, the Pirates are at home, meaning that Liriano had to pitch the top of the first. Would the proposed substitution make better sense if the Pirates were the visitors and run up the big lead, before Liriano threw a ball?
Answer: It would not be a common strategy for several reasons.

While it might keep a few innings off your pitcher at the end of the season, you're not really "saving" him in any useful way.  Given that he's working on his normal throwing day, has gone through a full warmup and entered the game, he won't be available to throw much earlier than he would otherwise.  Even if he were, barring a double-header or something, you'd probably have to modify your existing rotation to re-use him.  Some other pitchers might not react well to that.  I'm sure there are "ironmen" that would be fine now and again with odd rotations, but it's not something that a club is going to plan for.  You use a pitcher normally and they get hurt, too bad.  You use a pitcher in a non-standard way and they get hurt, everyone is going to blame you.
The person you can replace him with is not one of your everyday starters, and may not be suited to going long into a game.  Your starter is valuable by consuming innings.  If the replacement starts giving up runs and you have to go deeper into the bullpen, you leave yourself in a poorer position for subsequent nights with tired relievers.  Except in an emergency/playoffs, you wouldn't put in another starter because they're not at the right point in the rotation and wouldn't be comfortable pitching that day.  (There are cases where you could use someone on their throwing day, but that's not common).
Five runs (especially in the first inning) is not such a large lead.   Consider that the other pitchers might allow the other team to tie the game and it goes extra innings.  In this case, you want to have every pitcher possible available.  Throwing away a possible six or seven innings of available pitching for that day isn't a good idea if you plan for that possibility.  Don't think of it as an easy win, think of it as a chance for your ace to pitch effectively with less pressure than he would normally have.  Maybe you'll get eight innings and give the bullpen a day off.
Players want to play.  A starter that's healthy wants to pitch on his day.  Taking him out with a big lead in the first means that a big chance for a win on his stats goes away.  Unless there's an injury, he's going to be very confused or offended to come out in that situation.  In the middle of the season, sometimes managing players is as important as managing the game.

It doesn't help whether you're at home or on the road.  The starting pitcher is already announced and already has gone through game-day warmups by the time this happens.  You still lose his services if you execute it before he faces a batter.
Comment: +1 Many starters throw 50+ warmup pitches before a game. With this alone, they would be out for at least 2 days and then the rotation would be messed up.
Comment: Indeed 5 runs is not that much, especially early in the game. There's a somewhat famous story about how Jim Rooker had to walk from Philadelphia to Pittsburgh after the Pirates blew a 10 run 1st inning lead: https://archive.triblive.com/sports/pirates/25-years-ago-ex-pirates-pitcher-jim-rooker-made-good-by-trekking-from-philly-to-pittsburgh/


Is Joel Spolsky's "Hunting of the Snark" post valid statistical content analysis?

Question: If you've been reading the community bulletins lately, you've likely seen The Hunting of the Snark, a post on the official StackExchange blog by Joel Spolsky, the CEO of the StackExchange network. He discusses a statistical analysis conducted on a sample of SE comments to evaluate their "friendliness" from an outside user's perspective. The comments were randomly sampled from StackOverflow and the content analysts were members of Amazon's Mechanical Turk community, a market for work that connects companies to workers who do small, short tasks for affordable fees.
Not so long ago, I was a graduate student in political science and one of the classes I took was Statistical Content Analysis. The class's final project, in fact its entire purpose, was to conduct a detailed analysis of the New York Times' war reporting, to test whether or not many assumptions Americans make about news coverage during wars were accurate (spoiler: evidence suggests they're not). The project was huge and quite fun, but by far its most painful section was the 'training and reliability testing phase', which occurred before we could conduct full analysis. It had two purposes (see page 9 of the linked paper for a detailed description, as well as references to intercoder reliability standards in the content analysis statistical literature):

Confirm all coders, i.e., readers of the content, were trained on the same qualitative definitions. In Joel's analysis, this meant everyone would know exactly how the project defined "friendly" and "unfriendly."
Confirm all coders interpreted these rules reliably, i.e. we sampled our sample, analyzed the subset, and then statistically demonstrated our pairwise correlations on qualitative evaluations were quite similar.

Reliability testing hurt because we had to do it three or four times. Until -1- was locked down and -2- showed high enough pairwise correlations, our results for the full analysis were suspect. They couldn't be demonstrated valid or invalid. Most importantly, we had to do pilot tests of reliability before the final sample set.
My question is this: Joel's statistical analysis lacked a pilot reliability test and didn't establish any operational definitions of "friendliness". Was the final data reliable enough to say anything about the statistical validity of his results?
For one perspective, consider this primer on the value of intercoder reliability and consistent operational definitions. From deeper in the same source, you can read about pilot reliability tests (item 5 in the list).
Per Andy W.'s suggestion in his answer, I'm attempting to calculate a variety of reliability statistics on the dataset, which is available here, using this command series in R (updated as I calculate new statistics).
Descriptive statistics are here
Percentage agreement  (with tolerance = 0): 0.0143
Percentage agreement (with tolerance = 1): 11.8
Krippendorff's alpha: 0.1529467
I also attempted an item-response model for this data in another question.
Comment: Re: #1 - It should be noted that this wasn't so much an exercise on if the comments *were* friendly or not, but more of an exercise on if the comments were *perceived* as friendly or not to an outside user.
Comment: @Rachel I don't think that's right. If they were measuring how outsiders perceive comments on SO, they'd have needed quite a larger sample set than 20 people.
Comment: It's the difference between concluding something about how outsiders perceive the comments, and concluding something about the comments themselves. In the first case, you'd need a much larger sample of people, and the conclusion would be "Outsiders think 2.3% of SO comments are unfriendly." In the second, it's "2.3% of SO comments are unfriendly." They're different conclusions, and I think the second one might not be possible to make, because we can't demonstrate the coders evaluate the comments similarly without a reliability test.
Comment: @Christopher Friendliness is very subjective though. Depending on who you ask, the same comment can be viewed as both friendly and unfriendly. That is why I think its more important to get the point of view from a large number of random users instead of someone someone who has the exact same viewpoint as yourself.
Comment: The [did publicly release the coding data](http://blog.stackoverflow.com/?attachment_id=12040) so one could go and assess the reliability of the coders themselves if one wanted to.
Comment: @Rachel: Random button presses are just the most extreme form of unreliability. The tester needs to ensure -- to some reasonable extent -- that the measurements are actually measuring the same thing. If you ask a child, an average adult, and a basketball star "Is this fence tall or short?", and then compile the answers as a statistical exercise, I don't believe that you can have much confidence in the analysis. As I said, though, I'm no statistician, so I am open to being wrong about this, and will be interested to read any answers.
Comment: @JoshCaswell I'm quite interested in the answers as well :) I think the test done by Joel was more like asking someone "Do you think this question is tall or short", as opposed to asking "Is this fence tall or short". What matters is the user's opinion and point of view, not if the fence is actually tall or short.
Comment: @Christopher I don't think it was the same 20 people rating all 7000 comments. They simply asked for 20 ratings on each of the comments, and those ratings could come from any high-rated user.
Comment: I can get the final data set, yes, but my understanding of content analysis methodology was that reliability testing must occur before the full content analysis, and that no statistical manipulation could extract reliability from the final dataset. If that's not the case, then I suppose Mechanical Turk is a content analyst's dream.
Comment: @Rachel: Clarified the purpose of the study in the first paragraph
Comment: It seems that [Seth Rogers](http://meta.stackoverflow.com/users/168422/seth-rogers) was the statistician on this exercise -- it might be worth pinging him to get his feedback here.
Comment: @Rachel: I lack the stats knowledge to express this in the correct terminology, but no matter what was being tested, it still needs to be demonstrated to some degree of certainty that the respondants were not just mashing the "friendly" and "unfriendly" buttons at random.
Comment: @JoshCaswell I pinged Seth on twitter...
Comment: @JoshCaswell I think that was part of the experiment though. People are unique, and everyone views things differently. I think you will actually get a more accurate result on if users view comments as friendly/unfriendly by asking 20 random people, instead of only asking 20 people that share the same friendly/unfriendly view as you. It was noted in one of the blog posts that the users that performed the evaluation were rated highly on MTurk, so I can assume they are not users who would simply hit a random button.
Comment: @Rachel: You still have to find out what they think "friendly" means. One way or the other, there needs to be a standard set for the measurement. We can say "We define this comment, _c_, as 'friendly'. Now we will find out if this agrees with the opinion of the person in the street." (this tests respondents' own definitions) or, "We wish to find out if this comment is 'friendly'. We will ask testers to rate it according to these criteria." (this tests the comment itself). (cont'd)
Comment: To have both sides of the question undefined creates meaningless answers -- does the object under measured have some quality which may be measured differently by different measurers? (Also, if the former was the intent of the exercise, then there need to be _way_ more data points.)
Comment: @Rachel: "Friendliness is subjective" -- this is exactly the problem; everyone has his or her own definition. Imagine that you asked 20 people to rate 7000 comments as "glurkly", "frabnous", or "unglurkly". What do the results mean?
Answer: Reliability of scores is frequently interpreted in terms of Classical Test Theory. Here one has a true score, X, but what you observe at any particular outcome is not only the true score, but the true score with some error (i.e. Observed = X + error). In theory, by taking multiple observed measures of the same underlying test (making some assumptions about the distribution of the errors of those tests) one can then measure the unobserved true score.
Note here in this framework that you have to assume that your multiple observed measures are measuring the same underlying test. Poor reliability of test items is then frequently taken as evidence that the observed measures are not measuring the same underlying test. This is just a convention of the field though, poor reliability, in and of itself, does not prove (in any statistical sense) the items are not measuring the same construct. So it could be argued that by taking many observed measures, even with very unreliable tests, one could come about a reliable measure of the true score.
It also stands to be mentioned that classical test theory isn't necessarily the only way to interpret such tests, and many scholors would argue that the concept of latent variables and item-response theory is always more appropriate than classical test theory.

Also a similar implicit assumption in classical test theory is when people say reliabilities are too high. It doesn't say anything about the validity of whether particular item(s) measure some underlying test, but that when reliabilities are too high researchers take it as evidence that errors between the tests are not independent.
I'm not quite sure why you are so vehement about not going in and calculating the reliabilities yourself. Why could one not do this and subsequently interpret the analysis in light of this extra information?
Comment: [Other question](http://stats.stackexchange.com/questions/33639/what-am-i-measuring-when-i-apply-a-graded-response-model-to-the-hunting-of-the) is up.
Comment: So first let me point out that I'm not a grad student doing stats anymore for a good reason: it wasn't quite my forte. I might be misremembering the methodology. All the same, I think you and I might be talking about different measures of reliability, or at the least there is research to suggest measuring intercoder reliability before the final analysis is conducted matters for validity. I've edited the question to include one source I found on the web, which cites considerably more research on the subject.
Comment: It is a different context (reliability of dichotomous test items instead of some continuous outcome), but the logic is functionally the same. Hence why I did not mention any specific measure of reliability (there are many). Your quote does not insinuate anything about `before the final analysis`, so I'm not quite sure where that notion comes from.
Comment: Ah ha. You are correct, it is not quite a requirement. Reading further into that link I posted, it looks like this pilot tests are considered a methodological best practice (search for pilot test in it).
Comment: I've changed my question to accommodate the new information. Thank you for the help correcting my error.
Comment: I dug my knowledge of R out of the deep recesses of my memory and am attempting to calculate [Krippendorff's alpha](http://en.wikipedia.org/wiki/Krippendorff%27s_alpha) on the dataset right now. If my machine is actually able to return a result, I'll pass it along.
Comment: Added a few (classical test theory I believe) reliability measures. Attempted an item-response model, but I'm not quite sure how to approach the data. All the ordinal data examples I can find map hundreds of individuals to a few questions with ordinal responses. This data is thousands of comments mapped to 20 individuals who gave ordinal responses. As such, I can run the analysis without data manipulation, but I'm not quite sure what I'm testing.
Comment: @Christopher, that is probably best as a second question for our site!
Answer:
Percentage agreement (with tolerance = 0): 0.0143
Percentage agreement (with tolerance = 1): 11.8
Krippendorff's alpha: 0.1529467

These agreement measures state that there is virtually no categorial agreement - each coder has his or her own internal cutoff point for judging comments as "friendly" or "unfriendly".
If we assume that the three categories are ordered, i.e.: Unfriendly < Neutral < Friendly, we can also calculate the intraclass correlation as another measure of agreement. On a random sample of 1000 comments, there is an ICC (2,1) of .28, and an ICC (2, k) of .88. That means, if you would only take one of the 20 raters, results would be very unreliable (.28), if you take the average of 20 raters, results are reliable (.88). Taking different combinations of three random raters, the averaged reliability is between .50 and .60, which still would be judged to be too low.
The average bivariate correlation between two coders is .34, which also is rather low.
If these agreement measures are seen as a quality measure of the coders (who actually should show good agreement), the answer is: they are not good coders and should be better trained.
If this is seen as a measure of "how good is spontaneous agreement amongst random persons", the answer also is: not very high. As a benchmark, the average correlation for physical attractiveness ratings is around .47 - .71 [1]
[1] Langlois, J. H., Kalakanis, L., Rubenstein, A. J., Larson, A., Hallam, M., & Smoot, M. (2000). Maxims or myths of beauty? A meta-analytic and theoretical review. Psychological Bulletin, 126, 390–423. doi:10.1037/0033-2909.126.3.390


Confused in selecting the number of hidden layers and neurons in an MLP for a binary classification problem

Question: I'm working on a disease classification dataset which has 25 features including the class attribute. It is a binary classification problem. The dataset has total 300 training instances.
I trained a feed-forward neural network with the following architecture:
model = Sequential()

model.add(Dense(100, input_dim=train_x.shape[1], activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(25, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

model.fit(train_x.values, train_y.values, epochs=50, batch_size=40, validation_split=.2, verbose=0)

The architecture has 3 hidden layers with 100, 50 and 25 hidden neurons in the layers.
After training, the model shows the following scores on the test set which includes 100 test instances.
100/100 [==============================] - 0s 94us/step
Test loss score: 0.02940008039586246
Test accuracy: 100.0

So, I've got 100% accuracy with this architecture.
The number of hidden layers and hidden neurons per layer was taken arbitrarily.
However, I have found something in an article of Jeff Heaton that:

and also found some rules about choosing the number of hidden neurons per layer:
1. The number of hidden neurons should be between the size of the input layer and the size of the output layer.
2. The number of hidden neurons should be 2/3 the size of the input layer, plus the size of the output layer.
3. The number of hidden neurons should be less than twice the size of the input layer.
These three rules provide a starting point for you to consider.
Now my question is:

Is my model architecture acceptable?
According to Heaton, we can have more than 2 hidden layers for sort of automatic feature engineering. So is using 3 layers okay or not?
Here, I've used 100, 50 and 25 neurons in the hidden layers arbitrarily. The output layer contains only 1 neuron as it is a binary classification.  But according to the thumb rule, the number of hidden neurons should be between the size of the input layer and the size of the output layer. So, according to this rule, the number of hidden neurons for this dataset should be between 1-24 as there are 24 input features and the 1 output feature. So, using 100, 50, 25 hidden neurons is wrong?
For this dataset of 300 instances with 24 features and a class attribute of a binary class, what should be the number of hidden neurons? Is there any other empirical rule that I can follow?
Using 100, 50 and 25 hidden neurons yields a good result. So should I decrease the number of neurons? Why?
Comment: Don't pay attention to Heaton's blog and focus on your problem. The fact that you get such an high accuracy without any regularization at all is indeed remarkable. 1) what's the prevalence of the minority class in the training and in the test set? 2) Try applying a linear SVM to your data with & without regularization and tell us what you get. I have other suggestions which I may post as an answer, but for now please check these.
Comment: You're welcome, but please note I didn't say anything about NNs being bad. Any method which gets you 100% test accuracy is not bad by definition, _provided you have convincingly ruled out any overfitting to the test set_. I was just saying that with such a simple problem, SVMs provide a few advantages over NNs, such as: faster training/inference time, convex optimization (so no need to bother with early stopping, validation sets, etc.), easily interpretable (you said it's a linear SVM, right?) and online training becomes very easy if you get new data (a bit unlikely in your case, I know).
Comment: In the training set, there are 300 instances in which 183 are YES and 117 are NO class. In the training set, there are 100 instances in which 67 are YES and 33 are NO class. I've just used MaxMinScaler to the attributes. I've trained a linear SVC with 'l2' and without any regularization. Both of them yields 100% accuracy. I've also trained a logistic regression which yields 98% accuracy.
Comment: @DeltaIV Thanks for your comment. I am actually trying various algorithms on this dataset. Finally, I'll compare the performance of the classifiers and choose the best one. NN could be a bad method for this problem. But, I just want to make sure that I've tried a logically acceptable NN architecture.
Comment: You meant "in the test set there are 100 instances", but ok. The NO group is a bit underrepresented in the test set: nothing too serious, but just for pedantry consider splitting again train set & test set in a balanced way: it's trivial with `scikit-learn`, can't remember the exact method now but google and you'll find it easily. Also, you've got a small data set: it won't take time (and if it does, just use a GPU on Google Colab). The fact that the SVM is killing this data set confirms my intuition: it's just that your problem is simple. If you had had issues with fitting the logistic 1/
Comment: 2/ regression (regularized or not? That's important), I would have even concluded that it was a linearly separable problem. Later on I'll write an answer listing a few sanity checks you can do, just to convince yourself that you don't have any "overfitting to the test set" issues, but if I were in you, I'd just throw away the NN and use the SVM, which is also much more interpretable.
Comment: @DeltaIV I agree with you. But, can you help me in building an NN architecture for this dataset which is logically acceptable and minimize the risk of overfitting? Any empirical rule for choosing the number of hidden layers and hidden neurons in each layer (in term of this dataset)?
Comment: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/78729/discussion-between-abdullah-al-imran-and-deltaiv).
Answer: In Deep Learning there are no hard & fast rules to set the number of layers, the number of hidden units per layer and not even the kind of connections between layers: who claims the contrary often doesn't have experience of struggling with modern architectures, and relies on insight 20 years (if not more) old. "Proofs" of my claim:

you have all kinds of different architectures performing increasingly better on ImageNet: VGG, Inception, ResNet, ResNext, Xception, DenseNet, etc. Even the simplest empirical rules such as "double the number of channels before pooling" or even just "use pooling after a convolutional layer" aren't valid in general, though replacing pooling with increasing strides in successive layers has its drawbacks
if the "back-of-the-envelope" rules to choose the number of hidden layers & neurons from the days of old were still valid today, we wouldn't have so much investment on automated architecture learning - I think after  Deep RL and GANs, this is currently the single biggest R&D investment in AI companies.

But that's not necessarily bad news: your architecture is getting excellent test set accuracy as it is, so maybe you just don't have to worry about optimal architecture. Accuracy is not the correct metric to look at for classification, but as you have nearly balanced classes, and anyway astonishing classification accuracy, it's not like using another metric will make a big difference.
Since you haven't provided some data, or some training curves, it's hard to say if overfitting to the training set is actually happening. However, there are a few checks you could make, to clear any doubts. Since your architecture is relatively small ($25\times100+100\times50+50\times25+25=8775$ parameters, if I'm not mistaken), these checks won't take you much time. Every time I mention retraining in the following tips, I mean "retraining from scratch", i.e., after weight initialisation, not from the current weights.

create new training/test sets with a different random split, possibly having the same class ratio for train & test as in the whole dataset ($\frac{250}{150}=\frac{5}{3}$) and retrain
use a different estimate of generalization error, e.g., $k-$fold cross-validation instead than train/validation/test split. This time you will need to train a few (exactly $k$) times, so this check is a bit more time consuming than the others.
verify that all data preprocessing steps (apparently, MaxMinScaler only, in your case), have been fit on the training set and applied to the test set, without refitting them on the test set
you already checked that your problem is actually super-easy: both linear SVM and logistic regression could solve it, so that's an excellent reason to suspect that no overfitting to the test is occuring. It's just an easy problem. Lucky you :-)
I would have a look at decision regions for a few pairs or triplets of variables, maybe the most influential ones according to linear SVM or logreg. Maybe you'll find out that your problem is (close to) linearly separable, so basically most classifiers will do a great job here.
if you still are worried, shuffle class labels and retrain. Now the only way for your neural network to get high training set accuracy is to memorize the training set, which will manifest in much longer training time. At the same time, the test set will go down dramatically. If this doesn't happen, there's something seriously wrong somewhere in your code.
Perform the opposite test: initialise weights and train on just two or three data points. This time, train accuracy will immediately go to 100%, but test set accuracy will stay extremely low, no matter how long you train. If this doesn't happen, again you have a serious bug somewhere.

If at this point you're still worried (you shouldn't), get new test points (which you've never seen until now) and test on them.
Another possibility may be to just scrap the NN and use the linear SVM: you gain

much faster training & inference
better interpretability
convex optimization, i.e., no doubts about the number of training epochs
cross-validation becomes very simple and feasible
online training become fast and simple. Well, actually with such a small neural network you could easily perform online training with the NN too.
Answer: Most likely you are overfitting your data.
The accuracy you see at the end of your training procedure doesn't tell you much about whether your model is any good. You can make sufficiently complex models fit completely arbitrary training data without actually learning anything whatsoever. To evaluate whether your model is learning something interesting, you want to test it on the testing data (which I assume you have, from your variable names).
In general, choosing the size, number and form of hidden layers is a difficult problem which depends on the type and amount of data you have, as well as the computational resources available to you. A lot of trial and error usually goes into finding a good architecture.
Now specifically to your questions

Probably not, as I pointed out above.
In principle yes, but in your case it doesn't seem to help very much.
Going by those principles, you're doing it wrong, yes. While there can be cases where using larger hidden layers can be useful, you usually come across those after having tried smaller layers.
Since you don't have much data but a lot of dimensions, I would expect that you would perform quite well already with only one or two layers. Start trying one layer, play around with different sizes, then try two and see if you actually improve.
As you can probably judge from the answer so far, yes, you probably should reduce the number of neurons.
Comment: The accuracy and loss I showed above was the testing accuracy & loss.
Answer: I personally use the following heuristic  to choose the number of hidden neurons. Say the input layer has n variables and the output layer has m.
Find the prime number factors of n:

Then find the prime number decomposition of m:

The right amount of hidden neurons that don't overfit usually is among the combinations of these prime factors that divide both the number of input variables and output ones.
For example say you have 520 input features in a multi class classification task with 120 classes. Then

then you should try the following combinations:
 2
 4
 5
 8
10
20
40

In the specific case I was working on the right combination was 40 hidden neurons but of course that depends on the case you are dealing with.
Comment: have you a source for that heuristic? it seems to me that it would completely fail if you have nothing much in common between factors of input and output. for example if you have 97 inputs and 17 outputs, the heuristic suggests you should not have any hidden neurons at all


propensity score matching in MatchIt package

Question: I use the MatchIt package do the propenstiy score in matching
I want to know the "case" match to which "control", how do i do
for example, the first case match to the first control, second case match to the second control, and so on.
Comment: Mmm.. not a greatly documented package IMHO (no vignettes, no examples). If you don't find your answer here, try at this mailing list https://lists.gking.harvard.edu/mailman/listinfo/matchit
Answer: I just had the same problem and found some syntax that can help.
First, after you open the data set but before you run MatchIt, use the following code (data is your data frame and ID is your identifer):
####set rownames to id.
rownames(data) <- data$ID
head(data)

Second, Run the appropriate matchit syntax into an object, like so:
M.OUT1 <- matchit(Treat ~ X1 + X2, data = data)
Third run the following code:
mm<-M.OUT1$match.matrix
tx<-as.numeric(row.names(mm))
m<-cbind(tx,as.numeric(mm))
m[1:5,]
View(m)
write_sav(data.frame(m),"MatchedPairs.sav")

The write_sav line can be modified to fit whatever you are exporting your data into (in my case, SPSS. The write_sav function requires the haven package)


How to convert symbols "&'<> to entities " & ' < > in Perl

Question: What is the simplest method in Perl to convert special symbols "&'<> to entities " & ' < > in Perl? It is easy to write functions like this, but I think this problem has been solved a lot of times and there is no need to write your own functions.
sub add_entities {
    my ($text) = @_;

    $text =~ s/&/&/g;
    $text =~ s/"/"/g;
    $text =~ s/'/'/g;
    $text =~ s/</</g;
    $text =~ s/>/>/g;

    return $text;
}

sub remove_entities {
    my ($text) = @_;

    $text =~ s/"/"/g;
    $text =~ s/&/&/g;
    $text =~ s/'/'/g;
    $text =~ s/</</g;
    $text =~ s/>/>/g;

    return $text;
}

Comment: [HTML::Entities](http://search.cpan.org/~gaas/HTML-Parser-3.71/lib/HTML/Entities.pm)
Comment: @raina77ow, HTML doesn't have `'`, so it seems the OP is dealing with XML. HTML::Entities is not appropriate for XML (except perhaps XHTML). (Not that the OP's verison aren't much better. They are horribly broken too.)
Comment: I agree that using XML writing library (and its own escaping tools) is way better when you deal with XML. I disagree that 'use XML::LibXML' is a proper answer to the question _as given_. )
Comment: @raina77ow, Who are you disagreeing with? Noone said that.
Comment: Be careful with the order of the replacement in the two routines. Consider `$s="abc" , $t;`. The `remove_entities` routine should reverse the order of changes made in `add_entities`.
Answer: You should never ever need remove_entities. Your parser shouldn't return any entities. Seems you have a horribly broken parser. I recommend XML::LibXML.
Same goes for add_entities XML. The XML writing library will handle all of that for you. You could use XML::LibXML for this too, but XML::Writer is much simpler to use for this task.
Note that both of your routines are horribly broken. add_entities doesn't consider character set. remove_entities doesn't handle numerical and entities outside of the base XML spec.


Harmonic analysis of J.S. Bach's Prelude in Cm (BWV 847)

Question: I sometimes try to analyze the harmony of a Bach prelude to understand how chords change, and how he manages to shift from one key to another. Here is the prelude in Cm BWV 847 (source image, MP3 here):

Here is what I understand:
<code>BAR    CHORD  DEGREE / KEY
              [Cm key]     [Gm key]      [Fm key]
1      Cm     I
2      Fm     IV
3      G      V
4      Cm     I
5      Ab     VI    =      IIb ?
6      D                   V
7      Gm                  I
8      C7                  IV      =     V
9      Fm                                I
</code>
Would you have a similar analysis or are the key changes on bars 5 and 8 incorrect?

Notes: I'm using capital letters for every degree, I don't write I for major and i for minor, I hope it's understandable. Also I haven't written the chords "with bass", for example bar 5 it should Ab/C for Ab major chord with bass C.
Comment: @ToddWilcox There are dozens of similar questions and answers: https://music.stackexchange.com/questions/116942/how-could-these-chords-in-debussys-reverie-be-analyzed, https://music.stackexchange.com/questions/113421/harmonic-analysis-of-c-m-von-weber-clarinet-concerto-no-1, https://music.stackexchange.com/questions/107340/how-to-think-about-this-moment-from-bwv-816, https://music.stackexchange.com/questions/33653/chords-in-bwv-846. I can find lots of others.
Comment: Not sure why you defended it **and** edited it, but the edits do make it much better
Answer: First of all, I suggest you study chords in BWV 846 which contains much to indicate why your strict harmony approach is limited.
Let me just remark on a few things. In Bach's music, as in much of that time, melodies can force the music to go in a different direction from where the harmony wants to go. So you need to look at melodic lines here.
To me the sequence of measures starting at 5 is a two interlinked chains of tension-release. In the melody there are the pairs Eb-D, D-C, C-Bb &c, with in the bass C-Bb Bb-Ab Ab-G. (In the left hand there is a middle voice going A-Bb G-Ab along with the bass.)
Yes, that is a sequence of II-V but that alone does not explain the overall structure. For instance, the transition measure 4->5->6 to me is driven by the G->Ab->A melodic motion. Chromatic up means increasing tension, so that gives you the starting point for the sequence of II-V resolutions.
Answer: As a starting point, I disagree with the analysis of m. 3, which I see as B diminished seventh over a C pedal tone. I hear the left-hand Ab as the structural pitch and G as the decorative one.
I agree with m. 5. It's a pivot chord, serving as VI in the original key of C minor and bII in the upcoming (tonicized) G minor.
Bar 8 partially matches my analysis. I consider it a direct modulation (tonicization). That is, it's V the following F chord, but does not function in the key of G minor. To be a G minor chord, it would need to include Eb rather than E-natural. (Although the E natural could, perhaps, be attributed to melodic minor, the expectation would be that it move to F#, which it does not.)


NullReferenceException on TreeView node deselect

Question: I have two TreeViews in my program that deal with SelectedItemChanged in the same way. The problem is that one works fine and the other one throws a NullReferenceException. The exception gets thrown in one of the trees when a node is deselected...
How SelectedItemChanged is handled for both trees in the code-behind:
//How *ViewModel* is declared...
public DatabaseViewModel ViewModel { get { return DataContext as DatabaseViewModel; } }

//Gets selected item in TreeView
private void Tree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
     var data = e.NewValue as TreeModel;
     ViewModel.Tree.SelectedItem = data;
}

This is basically what happens:
-A node in the problem tree is selected
-Next, a node in the well-behaved tree is selected
-A NullReferenceException is then thrown on the line: ViewModel.Tree.SelectedItem = data. in the problem tree's code-behind. The exception says: "Object reference not set to an instance of an object".
What is going wrong here and how can I fix it? Thank you.
*Note: I'd just like to point out that this does not happen for the other tree.
Comment: Have you verified that the DataContext in the bad case is actually an instance of DatabaseViewModel?  Set a breakpoint on the line where the exception is thrown, then you can check the DataContext.
Answer: Try this :
//Gets selected item in TreeView
private void Tree_SelectedItemChanged(object sender,RoutedPropertyChangedEventArgs<object> e)
{
     var data = e.NewValue as TreeModel;
     if(data!=null)
     ViewModel.Tree.SelectedItem = data;
}

Comment: This is almost so simple that I probably should have thought of it. Regardless, thanks it worked fine! =)


Creating simple user groups with a term in Drupal 7

Question: I need to tag my users as belonging to a 'workplace'. And then display a page with workplace details and a list of associated users.
I have setup a vocabulary in Taxonomy called 'workplace' and I can easily tag a user. I need to know how to create a block that will show on a taxonomy / term / id page and show tagged users.
I imagine I will have to make part of the user details public as well as by default they won't be?
Any help much appreciated - even a hint as to where to start ...
Answer:
Since users are field able you can now attach a taxonomy term reference field to users.
Create a view that list all users where the tid of the term reference is a select value. You could also get the value from a url arg.
The view itself should show the user data you want to display, maybe as a list or whatever fits you better.
In the views header you could display the info about taxonomy term. Right now this will require some views knowledge and some custom code, as you need to create what views calls an area handler, to display the info. You could also do this in a preprocess function instead.
Comment: Thanks that is great. If I want to store fields associated with the 'Workplace' i.e. location, details etc is it good practise to add fields to the 'term' or should I create a content type and use the 'term' to create the one to many relationship between the 'workplace' and it's many 'users'.


Follow all links in JSON-LD API

Question: Say I want to consume an API that returns JSON-LD and follow all the links. (I'm experimenting with the Hydra API-Demo, but it should work with all JSON-LD APIs, not only Hydra-based ones. Any good APIs out there that I should try?)
So I want to follow all the links and my environment doesn't have native RDF support. Probably, I should first parse it with one of the libs and get it into extended form with jsonld.expand(). Then I just grab all the values with key @id. Is that the recommended way to do it or am I missing some edge-cases?
Answer: The purpose of the expansion API is to produce regular, context-free output (expanded form) for algorithmic processing -- which is exactly what it sounds like you want to do. So, yes, you've got the right approach; you shouldn't be missing any edge cases as I've understood you. After you have JSON-LD in expanded form, you can easily follow the @ids (and, if you also need to do some kind of analysis of the vocabulary/ontology, you can follow the properties which will then be fully-expanded URLs).
Comment: Thanks! Can you point me to one or two good JSON-LD APIs that I can use for testing?
Comment: There's some data here you can crawl (see the JSON-LD link at the bottom): http://dbpedia.org/page/Nissan_Motor_Indonesia, also here: https://dev.payswarm.com/i/manu (request application/ld+json).

Follow all links in JSON-LD API


How to redirect multiple domain to same ip but different port for different services

Question: In my VM, i'm running both apache2 and nginx, but apache2 is pointed to Port 8080 and nginx is Port 80. Already i pointed my domain to ip in nginx and its running properly. The same thing i need to do for apache2. But how will i give Port number while mapping the ip to Domain. In the cName record, i'm not able to specify the port. Is there any alternative way to resolve this issues.
Comment: You can't. DNS doesn't handle ports.
Answer: DNS CNAME record resolves only to IP address, so you cannot specify port, you have to explicitly add port to your URL in a request like this: http://example.com:8080.
Another approach is to use load balancer like for example Haproxy, Traefic, or even Nginx with its reverse proxy capabilities (check this: http://nginx.org/en/docs/http/load_balancing.html). Then you will be able to use only one port (well, you can use multiple ports as well, but typically port 80 and 443) on its frontend traffic and "routing" it to your backends by some criteria (domain name, path, HTTP header info, cookies, source or destination IP address, etc.).


Anonymous permissions with Nodeaccess

Question:
Content type: Webform
Allowed grants: View, Edit
Allowed roles: Anon, authorized, admin

Go to the webform node eg. node/20
Click Grants tab
Tick authenticated and admin users can View, anonymous is unticked
Page is still accessed by anonymous?

node/20:

Global Nodeaccess settings:

At the bottom Webform is ticked to View for anon/authenticated/admin because we want anonymous to see all webforms by default, except one that needs to be logged in. Doesn't really matter because unticking View for anonymous, anonymous can still view the page?!
Answer: With every Nodeaccess permission change, Permissions need to be rebuilt every time, for some reason I am not getting the warning message on save, run it at <code>/admin/reports/status/rebuild</code>

Anonymous permissions with Nodeaccess


Can a voice-based sentiment analysis model differ depending on the language?

Question: I'm trying to figure out if building a voice-based sentiment analysis model for Brazilian portuguese would be possible. Since I couldn't find any free Brazilian datasets for voice analysis, would it be possible to develop the model using a dataset in english considering I won't need to transcribe the audio to text?
Answer: You could make it, but it probably wouldn't work well. Portuguese and English have different phonology, vocabulary, prosody, and intonation. Those are the types of clues that your model would need to latch onto.
As with many ML problems on this site, it could be solved by collecting more data.


Find a substring in a string with for loops

Question:

function match(str1, str2) {
  var matched = false
  for (i in str1) {
    if (str1[i] == str2[0]) {
      for (j in str2) {
        if (str2[j] != str1[i]) {
          return false
        } else {
          i++
          matched = true
        }
      }

    }
    return matched
  }

}

console.log(match("umbrella", "rella"))

Can someone fix this code. Is this piece of code good enough. or any improvements. I wanted to find a string in another string with just using the for loops. I saw some answers but they were too complex.
Im a beginner
Answer: You are returning after only one iteration, change
    }
    return matched
  }
}

to
    }
  }
  return matched
}

That being said, if I'm understanding your code correctly, right now it will fail if it starts to find a match, but then it doesn't match, and there is a match later.
E.g.
console.log(match("umbrerella", "rella"))

function match(str1, str2) {
  var matched = false
top:
  for (i in str1) {
    if (str1[i] == str2[0]) {
      for (j in str2) {
        if (str2[j] != str1[i]) {
          matched = false
          continue top
        } else {
          i++
          matched = true
        }
      }
      if (matched) return true;

    }
  }
  return matched
}

console.log(match("umbrella", "rella"))
console.log(match("umbrella", "della"))
console.log(match("umbrerellas", "rella"))
Answer: The for ... in statement iterates over the own enumerable keys of the object. This could be handy for objects, but it does not work always for arrays (Why is using "for...in" for array iteration a bad idea?).
Instead use a classic for statement with an index variable from zero to less than the length.

You could take some short circuits and exit early on found and omit iterating over the lengths.

function match(str1, str2) {
    for (let i = 0, l = str1.length - str2.length + 1; i < l; i++) {
        let match = true;
        for (let j = 0; match && j < str2.length; j++) {
            if (str1[i + j] !== str2[j]) match = false;
        }
        if (match) return true;
    }
    return false;
}

console.log(match("umbrella", "rella"));
console.log(match("umbrella", "rello"));
Answer: Why don't you just use:

console.log("umbrella".includes("rella"))
Comment: I dont want to use any helper methods
Comment: The OP only wants to use for loops.


Determining the significance of rare events

Question: We have been having a discussion about the significance of 2 accidents.
If I have 2 groups:

Group 1 is a baseline where an accident has occurred 70 times in
3,500,000 and
Group 2 where an accident has occurred 2 times in 14,000

How confident can I be that an accident is more likely in group 2?
Can I do a valid calculation with only 2 events in group 2?
Is the difference between the groups statistically significant?
Thanks for any suggestions.
Answer: The standard approach for a question like this is to calculate a confidence-interval for the mean accident rate in your two groups and check whether they overlap. Typically, one would use 95% CIs.
Now, things that can happen or not in $n$ independent samples, like accidents, are binomially distributed. So we need to calculate CIs for the binomial parameter $p$.
You have a rather small "success rate", i.e., accident rate. In such cases, the standard normal distribution approximation for $p$ breaks down, and you need somewhat more elaborate ways of calculating CIs. Brown et al. (Statistical Science, 2001) recommend Agresti and Coull for large $n$ (which you certainly have, with your numbers of trials).
We can use binom.confint() in the binom package for R to calculate the intervals and plot them for good measure:
> library(binom)
>
> (CIs <- binom.confint(x=c(70,2),n=c(3500000,14000),methods="agresti-coull"))
         method  x       n         mean        lower        upper
1 agresti-coull 70 3500000 0.0000200000 1.579976e-05 2.529776e-05
2 agresti-coull  2   14000 0.0001428571 2.883566e-06 5.570670e-04
>
> plot(range(CIs[,c("upper","lower")]),c(0.7,2.3),type="n",xlab="",ylab="",yaxt="n")
> axis(2,2:1,paste("Group",1:2),las=2)
> lines(x=CIs[1,c("upper","lower")],y=rep(2,2),lwd=2)
> lines(x=CIs[2,c("upper","lower")],y=rep(1,2),lwd=2)
> points(CIs[,"mean"],2:1,pch="|",col="red")

Note how the CI for group 2 is much wider than that for group 1, because the sample size is much smaller. (But look at the horizontal axis: both CIs are still tiny. We can be reasonably sure that the accident rate in group 2 is still less than $6\times 10^{-4}=.0004$.) Importantly, the CIs overlap, so based on this analysis, we cannot reject the null hypothesis that the accident rates are identical in both groups.
See the following two earlier threads on binomial CIs:

Confidence interval for Bernoulli sampling
Confidence interval around binomial estimate of 0 or 1 - I took the suggestion here from Karl Ove Hufthammer's answer, so please go and upvote it
Comment: Thank you for the detailed answer. The practical side of the problem is a debate about whether group 2 is failing to meet the expected standard, and whether action is justified to try to improve it. "Cannot reject the null hypothesis" would be used to argue that no action should be taken - it's basically the same argument as 2 is too small a number to draw any conclusions. But I think the CI shows that it is more likely that there is a difference, and it could in fact be much larger than already observed? Is that a fair statement?
Comment: Both are fair statements: on the one hand, the observed mean in group 2 is quite a bit larger than in group 1. On the other hand, we simply can't tell whether this is systematic, or simply due to chance. Because we have a rather small sample and a tiny accident rate, it's simply hard to estimate the underlying rate. As a sensitivity check, you might re-run the analysis with 1 (or 3) accidents in group 2. If the number of accidents might easily have been 1 or 3, you could think about whether you are comfortable basing your decisions on a single accident happening or not, out of 14,000.
Answer: One other simple way to answer this would be determine the probability of seeing 2 accidents in a sample of 14000 when the probability of accident from baseline is (=70/35000000). If this probability is below 0.05 (assuming 95% confidence level) then one could conclude that sample 2 is riskier.
The probability would be: nCr * p^r * (1-p)^(n-r) [using binomial distribution]
where n = 14000; r = 2; p = 0.002%
This comes out at 2.96%. This means the probability of seeing 2 accidents in a sample of 14000 is only 2.96%. So we can assume that is not due to noise i.e. reject null hypothesis and accept alternate hypothesis


cocoa wait for finish executions

Question: 'm Having a problem running more then one method which contain NSTask to run  a script in the app bundle (using GDC to update the UI).
The problem is how to use one after the other, i.e. only when the previous one has been finished.
Each of the methods as I said use NSTask to run the sh scripts, which have a duration of several seconds, and maybe even more than a minute (depending on which calculations are done).
Some things I tried unfortunately blocked the UI and this is not correct, because even the "Stop" button is locked.
I'm calling the methods as below:
[self performSelectorOnMainThread:@selector(runScript1:) withObject:blabla waitUntilDone:YES];
//some if statements here
[self performSelectorOnMainThread:@selector(runScript2:) withObject:blabla waitUntilDone:YES]; how to wait for runScript1 completition??
//more script to call after

EDIT (adding example of runScript1)
    - (void)runScrip1:(NSArray*)args
{
    if (_operationPermitted == 1) {
        NSString *acq, *path;

        acq = [args objectAtIndex:0];
        path = [args objectAtIndex:1];

        dispatch_queue_t runQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
        dispatch_async(runQueue, ^{

            @try {
                runScrip1Task = [[NSTask alloc] init];

                Scrip1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Scrip1.sh"];
                [_runScrip1sTask setLaunchPath:Scrip1];
                [_runScrip1Task setArguments:[NSArray arrayWithObjects:@"skip", @"main", acq, path, nil]];

                [_runScrip1Task setCurrentDirectoryPath:@"/private/tmp"];
                [tasks addObject:_runScrip1Task];

                self.outputPipe               = [[NSPipe alloc] init];
                _runScrip1Task.standardOutput = self.outputPipe;
                _runScrip1Task.standardError  = self.outputPipe;

                [[self.outputPipe fileHandleForReading] waitForDataInBackgroundAndNotify];

                [[NSNotificationCenter defaultCenter] addObserverForName:NSFileHandleDataAvailableNotification object:[self.outputPipe fileHandleForReading] queue:nil usingBlock:^(NSNotification *notification){

                    self.display2.string = @"\nAcquiring all the data...";
                    NSData *output = [[self.outputPipe fileHandleForReading] availableData];
                    NSString *outPutString = [[NSString alloc] initWithData:output encoding:NSUTF8StringEncoding];

                    // ----------------------------------------------- //

                    // here some code to update my UI

                    // ----------------------------------------------- //

                    dispatch_sync(dispatch_get_main_queue(), ^{
                        // manage text sobstitutions
                        [_outputView appendAttributedString:[[NSAttributedString alloc] initWithString:outPutString attributes:_whiteTextwithArialFont]];
                        [_displayCenter scrollToEndOfDocument:self];
                    });

                    [[self.outputPipe fileHandleForReading] waitForDataInBackgroundAndNotify];
                }];

                [_runScrip1TaskTask launch];
                [_runScrip1TaskTask waitUntilExit];

            }
            // handle errors w/o app crashes
            @catch (NSException *exception) {
                NSLog(@"Problem Running Task: %@", [exception description]);
                [self stopAllRunningsTasks];
            }
            @finally {
                _operationPermitted = YES;
                _finished = true;

            }
            if (_runScrip1TaskTask.terminationStatus > 0) {
                [self stopAllRunningsTasks];
            }
        });
    }
}

Can anyone direct me to the right way, without blocking the user interface?
thanks
Comment: Tag your question with the language you are using, please.
Comment: If I understand correctly, I added objective-c, and sorry for that.
Answer: The solution is to leave your scripts asynchronous as they actually are inherently, and use "continuation".
Here is a sample code which should provide some help:
typedef void (^completion_t)(int result);

- (void) runScript1WithParams:(NSArray*)params completion:(completion_t)completion {
   // Create and setup the task:
   NSTask* task = [[NSTask alloc] init];
   ...
   [[NSNotificationCenter defaultCenter] addObserverForName:NSFileHandleDataAvailableNotification object:[self.outputPipe fileHandleForReading] queue:nil usingBlock:^(NSNotification *notification){
       ...
   }];

   // set termination handler:
   task.terminationHandler = ^(NSTask* task){
        if (completion) {
            completion(task.terminationStatus);
        }
   };

   // handle errors if required
   ...

   // start the task asynchronously:
   [task launch];
}

Your other runScript method is implemented the same way.
Usage:
[self runScript1WithParams:params completion:^(int result){
    if (result == 0) {
        [self runScript2WithParams:params completion:^(int result){
            if (result == ...) {
                ...
            }
            else {
                ...
            }
        }
    }
}];

Comment: amazing, I've just tried entering your code with just two tasks (to test), and this seems to work!
I need to implement all the code and see what comes out, but in the meantime thank you very much!
Answer: Does this do what you want?
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [self runScript1:blabla];
    [self runScript2:blabla];
});

Alternately, you could create your own serial dispatch queue or NSOperationQueue with a max concurrent operation count of 1 and use that.
Comment: First of all thank you for responding.
I edited the first post, adding an example of runScript1 method.
I have to work with many of these blocks of code , which are very similar to each other , and some even use the command line instead of scripts.

To try your suggestion I need to make some changes , but in the meantime you have a better idea of what I want to do .thanks
Comment: Oops forget.., "_operationPermitted" and "_finished = true;" are a bool that I'm tryng to force-wait for completition: "while !_fished...etc"

cocoa wait for finish executions


How to substitute bash variables into a yq command within a bash function

Question: Say I have an example YAML file, example.yaml:
services:
test:
  item0:
  item1:
  item2:
  item3:

And I have the follow bash function to recurse through the YAML levels:
function readYAML {
    local yaml="$1"
    declare -a arr
    readarray -t arr <<< "$(echo "$yaml" | yq -r '. |select(.) |  keys | .[]' )"
    echo "array -> ${arr[@]}"
    for item in "${arr[@]}"; do
        if [$item != ""]; then
            echo $("'.$item'")
            yq -r "$("'.$item'")" | readYAML;
        fi
    done
}

After running
readYAML "$(cat example.yaml)"

I get a yq error like '.test': command not found
How can I adjust the  yq -r $("'.$item'") | readYAML; line so that I end up with an array arr that contains item0 ... item3?
I think that yq is interpreting $("'.$item'")  as a command not as a filtering instruction. So I am doing the variable substitution incorrectly somehow.

N.B.
i. I know arr is overwritten (just stripped out array naming to simplify the question).
ii. I am using Go yq (Mike Farrah v4.31.2)
iii. There are similar questions. But none that I found dealing with substitute bash variables into a yq command within a bash function like this question:
Pass bash variable in yq
Answer: Which implementation of yq are you using?
If you are using kislyuk/yq, you can make use of the @sh builtin from stedolan/jq (which is used under the hood) to properly escape the exported strings for shell, and declare your array at once:
$ unset arr; declare -a arr="($(yq -r '.test | keys_unsorted | @sh' example.yaml))"
$ echo "${arr[1]}"
item1

mikefarah/yq also implemented an @sh encoder in the quite recent version v4.31.1, so now you can do the same:
$ unset arr; declare -a arr="($(yq -r '.test | keys | .[] | @sh' example.yaml))"
$ echo "${arr[1]}"
item1

Comment: @Lee My approach handles the whole array at once, so you wouldn't need a (recursive) function. If you want to do it iteratively, I would still opt for a loop passing in incremented indices for the `keys` array (`bash` is heavily imperative, after all). when using a bash function (even more so in the recursive case) you also have to somehow handle the "returning" of the array wrt scoping and subshells.
Comment: @Lee As a sidenote: If you wanted to "live" in a more declarative-styled shell, have a look at https://www.nushell.sh/. It implements arrays and can natively read YAML. My approach from above would translate to `let arr = (open example.yaml | get test | columns)`, then printing the array would be simply `echo $arr`, and single items just `echo $arr.1`. Also, recursive functions work just as you'd expect them to.
Comment: Thanks this is an improvement to the `readarray` line I think. However, where I'm struggling is in recursively calling the function to get the next level down. In this case in passing `item0: item1:...` etc as an argument to `readYAML`.
Comment: Using mikefarah/yq at the moment btw (question updated)


How to start a runbook using web service call to SC Orchestrator in Powershell?

Question: I have System Center Orchestrator 2012 R2 and I want to start a Runbook on the runbook server by calling the web service. The orchestrator console works fine if I browse to it like so: http://orchestratorserver.my.domain:82
When I first browse to it I get prompted to enter my domain credentials, only then does the Orchestrator Console page load.
If I call this PowerShell script, I get the following error and I'm wondering if it is related to the credential prompt? There are no firewalls on the orchestrator runbook server, or the host I'm calling from.
<code>Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (405) Method Not Allowed."
</code>
Answer: I got it working by using the module from here.
http://orchestrator.codeplex.com/releases/view/82959


Cookies across subdomains and hosts

Question: In the application I'm writing using a combination of development environments and languages, I have need of accessing a cookie from two different subdomains, each on a separate host.
The cookie is being set on www.mydomain.com using the PHP code that follows, and I'm attempting to access it from distant.mydomain.com on a separate host.
setcookie('token', base64_encode(serialize($token)), time()+10800, '/', '.mydomain.com');

I'm trying to access the cookie from distant.mydomain.com using the following code:
if (isset($_COOKIE['token'])) { /* do stuff */ }

The problem: distant.mydomain.com is not finding the cookie.  The if statement just mentioned returns false, even though the cookie exists.
I have verified that the cookie that is set is for mydomain.com (by checking my Firefox cookies).  I can't think of any reason this wouldn't be working.
Using the same setcookie code, I have an old application running exclusively on the www.mydomain.com host, and that application is able to access the cookie across domains.  This makes me suspect that the problem has to do with separate hosts.
Just in case any of the following information is pertinent:
- www.mydomain.com is IIS 6.0
- distant.mydomain.com is Apache 2.2.9
- Both servers use PHP 5.2.x
- Both servers are operating on Windows Server 2003
If there is any further information I can provide in order to better describe the problem, please let me know!
Comment: Sorry about that; I still haven't figured out why the second half of my post didn't make it through when I hit submit.
Comment: there's no question though still :)
Comment: Curses!  You know how the second time you have to write a post/e-mail/whatever, it's never as good as the first time?  *goes off to edit*
Answer: For the benefit of anyone reading this question the code and information contained in the original post are exactly correct and work fine.
The problem is when you introduce other technology.  For instance, I have since learned that sending PHP code through a Python module, one that allows Django to serve PHP files/content, changes a great deal about what is accessible to the script and what is not.
This was eventually discovered following the advice of Marc Novakowski, who suggested sending $_COOKIE to the log in order to find out what was there.
I also checked out $_SERVER and $_GET.  It was the emptiness of $_GET that tipped me off that the setup I am attempting to use is not as straightforward as I had thought.  It was that mistaken understanding that led to not including the information about Django in the original post.
Apologies and thanks to all who responded to this question!
Answer: Cookies set in domain
'.aaa.sub.domain.com'
will collide with identically named cookies set in domain
'.sub.domain.com'
and '.some.stupidly.obscure.multi.sub.domain.com'
That means (and this took some time to wade thru) if you're going to use the same-named cookie across multiple domains, you must set it once (and once only) in the main/base domain, in this case '.domain.com'; otherwise, the resulting cookie will be indeterminantly and randomly returned arrived at, sometimes the cookie 'jasper' set in .a.sub.domain.com, sometimes the cookie 'jasper' set in .sub.domain.com, sometimes the cookie 'jasper' set in .b.c.d.domain.com, sometimes the cookie 'jasper' set in '.sub.domain.com' and sometimes the cookie 'jasper' set in '.domain.com'
Answer: Does one of the subdomains use an underscore ? IE has problems accepting cookies from subdomain's that dont follow the URI RFC.
This is asumming 'distant' is a placeholder and not the actual subdomain name and of course that you use IE. Although more browsers could very well be effected by as, Fireworks doesn't though.
Comment: The actual domain has a hyphen but all other characters are alpha.  The subdomains in question are exclusively alpha.
Answer: I'd try installing Charles Proxy and see what headers are a) being sent to Firefox to begin with (to set the cookie) and b) which headers are being sent from Firefox to the second server.  At least that way you can narrow down where the problem is (browser or server).
Comment: If the browser's sending the cookie then it must be a server problem.  Maybe try logging or printing all the headers that the server sees coming in from the client, and try dumping everything in $_COOKIE to the log
Comment: Useful tool!  I took a look, though, and the headers for requesting distant.mydomain.com and local.mydomain.com (the application that *can* read the cookie) were identical, save for the actual hostname.
Answer: From php.net about the setCookie-function:

The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain . If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain . The default value is the current directory that the cookie is being set in.

The domain that the cookie is available. To make the cookie available on all subdomains of example.com then you'd set it to '.example.com'. The . is not required but makes it compatible with more browsers. Setting it to www.example.com will make the cookie only available in the www subdomain. Refer to tail matching in the » spec for details.

Basically: Your 4. and 5. parameter needs to be checked: Well, your path seems to be fine, but the domain needs to be changed:
Today you block the cookie to all others than domain A, but you want it to be awailable to both domain A and B. This is a bit tricky, but can be solved. Get inspiration on 15seconds ;-)
Comment: It seems i migth have misunderstand. When you told me you use two different servers, I automatically thought about two different hosts. But if the cookie only have to be visible at _one_ host, then your code seems correct. :-/
Comment: I may be interpreting your response incorrectly, but based on the http://php.net/setcookie documentation, my path and domain should be correct as given in my original question.  If they should be corrected, please provide an example of what the setcookie() function call should look like.

Cookies across subdomains and hosts


CreateAsync doesn't save IdentityUser to database with derived IdentityUser

Question: When Posting the following code works fine until it reaches the _userManager.CreateAsync method. No data is saved to the database.
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    private readonly ApplicationDbContext _appDbContext;

    private readonly UserManager<IdentityUser> _userManager;

    public ValuesController(UserManager<IdentityUser> userManager, ApplicationDbContext appDbContext)
    {
        _userManager = userManager;
        _appDbContext = appDbContext;
    }

    [HttpPost]
    public async Task<IActionResult> Post()
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        AppUser user = new AppUser();
        user.Email = "email@example.com";
        user.FirstName = "mark";
        user.LastName = "Macneill";
        user.UserName = "Saints22";
        await _userManager.CreateAsync(user, "P4$$word");

        return new OkObjectResult("Account created");

    }

    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

Comment: Having never used UserManager myself I did some minimal reading and it looks as though UserManager is only responsible for adding info to the UserManager object but doesn't actually save the data to the database. I read something about passing through an IUserStore to the UserManager constructor which is what is actually responsible for saving the data to a data store.
Comment: if i am not mistaken your `UserManager` must be `UserManager`
Comment: here is a code where i manually used the user manager for some refrence. but your code seems fine. https://github.com/neville-nazerane/dotnet-csp/blob/master/DotnetCsp.Website/Controllers/AccountController.cs
Answer: You don't seem to be using the IdentityUser directly but instead a AppUser class. This might be a class extending IdentityUser<> or IdentityUser. I am not sure if you have the rest of your setup right, so here is the process:
If you have created a custom AppUser class, let's say you created it as follows:
class AppUser : IdentityUser<int>

This would mean you have assigned the primary key as in int. However, if you extended IdentityUser instead, note that IdentityUser extends IdentityUser<string>, meaning your key is a string. Going ahead I am going to assume your key is int. If not, you can make the changes accordingly (change all ints to your key type.
In your startup class, you need to be adding the following to register this as your user class used for Identity
services.AddIdentity<AppUser, IdentityRole<int>>()
        .AddEntityFrameworkStores<ApplicationDbContext>();

Your ApplicationDbContext needs to be defined as follows:
public class ApplicationDbContext : IdentityDbContext<AppUser, IdentityRole<int>, int>

If you don't want to use IdentityRole<int> in the above two pieces of code, you can define a custom role class as follows:
public AppUserRole : IdentityRole<int>

Once you have these setup, you need to inject UserManager<AppUser> not UserManager<IdentityUser>.
Answer: Thank you neville-nazerane
I had IdentityUser in dependency injection and all I had to do was change IdentityUser to AppUser.
Startup.cs
services.AddIdentity<AppUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

Comment: I would recommend adding your original startup code to your question
Answer: I came across a similar issue and found out that the issue was because of the password restrictions:
var result = await userManager.CreateAsync(user, password);

result.Succeeded turned out to be false.
To manage password restrictions you can update the policies as follows in Program.cs:
builder.Services.AddIdentity<AppUser, AppRole>(options => {
    // ---- customize your settings here...
    options.Password.RequireDigit = false;
    options.Password.RequiredLength = 6;
    options.Password.RequireNonAlphanumeric = false;
    options.Password.RequireUppercase = false;
    options.Password.RequireLowercase = false;
    options.SignIn.RequireConfirmedEmail = true;
    options.SignIn.RequireConfirmedAccount = true;
    // many different options to setup here!!!
});


Map ip+port to internal website name

Question: I have a LAN web application running in our organization on a Ubuntu 20.04 at this address:
http://192.168.0.104:8084/
Rest of devices are Windows and our servers are Windows Server too. So our clients are accessing the application typing the whole route http://192.168.0.104:8084/ on web browser.
I would like clients can access the web application by using an internal domain name i.e. http://web.organization.com/ and it resolves internally to http://192.168.0.104:8084/. But I do not know if it is even possible to configure something like this in our Windows DNS server
Answer: To little rep for a simple comment:
DNS can only point the more easily remembered host/domain name such as web.example.com to an IP-address. (And that should be possible with your Windows DNS server.)
When you don't run a web service on the default port, DNS won't allow you (yet) to instruct a we browser: "when connecting to http://web.eaxmple.com ; do not connect to the default port for the HTTP protocol, port 80, but instead connect to custom port 8084."
You solve that by: either run your web application on port 80, or you configure a reverse proxy on port 80, that will forward HTTP requests (for the specific web.example.com host) from port 80 to custom port 8084. When you already run another website on port 80, many web servers come with reverse proxy functionality and can be configured with virtual hosts so that only requests for web.example.com will be proxied to port 8084.
Comment: Thank you! I understand, i will change the web app to publish on por 80 instead. However, how can I configure my windows DNS server to map that 192.168.0.104:80 to web.example.com?
Comment: That depends on the Windows release, if you already have the correct roles enabled etc. https://docs.microsoft.com/en-us/windows-server/networking/technologies/ipam/add-a-dns-resource-record
Comment: perfect, it definetly answers my question

Map ip+port to internal website name