Why I'm unable to access the outer class variable from inner class?

Question: I'm doing something like below but it's not working. Why is that? And how to do it?
public class A {
    private int a;

    private class BInner {
        a = 10;
    }
}

Comment: Because you try to make a statement outside a method. Try this in a method of the inner class and it should work.
Comment: So what's the solution?
Comment: Adding a method? See the answer.
Comment: "not working" is not a problem description.
Comment: To access `a`, try `A.this.a = 10`.
Answer: Usually the initialization stuff is done inside a constructor, so you can change or init the outer class variable a inside the constructor of inner class; like this:
public class A {
    private int a;

    private class BInner {
        public BInner(){
           a = 10;
        }
    }
}

Answer: You forgot to do the statement in a method:
public class A {
    private int a;

    private class BInner {
        void changeA(){
           a = 10;
        }
    }
}


Capybara tests using headless chromium not reading React code

Question: Problem
When running e2e tests with Capybara in my Rails/React app, whenever the javascript uses React, it has trouble executing the code.

 remains empty while the code renders properly locally and in docker. I've duplicated this running the capybara tests locally as well. What is odd is that if I add a document.getElementById("root").innerText = "Foo bar" it runs the javascript and either doesn't know how to execute the ReactDOM.render bit or just doesn't. When running tests against Stimulus code, it renders properly. For funsies, I downgraded to react 16 but had the same issue.
Background:
We use Vite js to bundle the javascript which I don't think is related but definitely could be. The app runs in an alpine docker environment but I can reproduce it locally so I don't think its specific environment related. Rails routes are empty endpoints that serves an empty html page with a #root div for React to hydrate and route accordingly. We're not using the react-rails gem. In the output of the html page, the assets are all pointing at the correct js files and the code does exist in those files.
Code
The main runtime code for capybara tests. I've included the code for the small react snippet I tested with the capybara test and the output of the print page.html.
app/javascript/entrypoints/test.jsx
import React from "react"
import ReactDOM from "react-dom"

// If this is uncommented, this line runs correctly but is not
// replaced by the "Hello World" in the `render` method
// document.getElementById("root").innerText = "Foo bar"

// This never gets run or is run incorrectly
ReactDOM.render(

Hello World
,
  document.getElementById("root")
)

test.html.haml (yes, I know haml is awful)
!!!
%html{lang: :en}
  %head
    = vite_client_tag
    = vite_react_refresh_tag
    = vite_javascript_tag "test.jsx"

  %body
    #root

react_test_spec.rb
require "rails_helper"

RSpec.describe "Testing react", type: :feature, js: true do
  describe "just checking", :with_csrf do
    before { visit test_home_path }
    subject { page }

    it "renders react" do
      print page.html
      expect(page).to have_content "Hello World"
    end
  end
end

print page.html output

<link rel="modulepreload" href="/vite-test/assets/jsx-dev-runtime.ddafb254.js" as="script" crossorigin="anonymous">

Config/setup code. Package versions, capybara/vite configuration, and etc.
package.json
    // react related packages
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router-dom": "6",

    // vite related packages
    "stimulus-vite-helpers": "^3.0.0",
    "vite": "^2.9.1",
    "vite-plugin-ruby": "^3.0.9",
    "vite-plugin-stimulus-hmr": "^3.0.0",
    "@vitejs/plugin-react": "^1.3.2",

    // babel related packages
    "@babel/core": "^7.0.0-0",
    "@babel/preset-react": "^7.16.7",
    "@babel/preset-typescript": "^7.17.12",
    "@babel/eslint-parser": "^7.17.0",
    "@babel/plugin-transform-runtime": "^7.18.2",
    "@babel/preset-env": "^7.17.10",
    "babel-jest": "^27.5.1",
    "babel-plugin-macros": "^3.1.0",

capybara.rb
Capybara.register_driver :chrome_headless do |app|
  options = ::Selenium::WebDriver::Chrome::Options.new

  options.add_argument("--headless")
  options.add_argument("--no-sandbox")
  options.add_argument("--disable-dev-shm-usage")
  options.add_argument("--window-size=1400,1400")

  Capybara::Selenium::Driver.new(app, browser: :chrome, capabilities: [options])
end

Capybara.javascript_driver = :chrome_headless

vite.config.ts
export default defineConfig({
  build: {
    sourcemap: true,
  },
  plugins: [RubyPlugin(), react(), StimulusHMR()],
})

vite.json
{
  "all": {
    "sourceCodeDir": "app/javascript",
    "watchAdditionalPaths": []
  },
  "development": {
    "autoBuild": true,
    "publicOutputDir": "vite-dev",
    "port": 3036
  },
  "test": {
    "autoBuild": true,
    "publicOutputDir": "vite-test",
    "port": 3037
  }
}

some packages in Dockerfile.development. Also duplicated this issue locally using chromedriver
RUN apk add \
  build-base \
  chromium \
  chromium-chromedriver \

Answer: Your JS assets are likely built differently in dev and test modes - and this sounds like you have a JS bug which is preventing the hydration. Add a pause to  your test, run it in non-headless mode and look at the developer console for JS/network errors
Comment: Ended up finding `jsxDevRuntime.exports.jsxDEV is not a function` as the primary culprit and followed the solution laid out here to resolve. https://github.com/vitejs/vite/discussions/5803#discussioncomment-1772907 hopefully helps someone out in the future


How to fix "Sign in disabled" appearing under any circumstances?

Question: Since some time (a pair of weeks) i can't run no third party Google Sheet or Google Sheets add-on.
Trying to do so i'm forced to run through the usual authentification andon the end of it i get the alert "Sign in with Google is temporarily disabled for this app". The alert comes without a link "advanced", so it isn't possible to go around of the alert.
This happens with all Google accounts i can login into (privat account, firm's account). Noone of these accounts is a GSuite - all are just normal account created with GMail. Nobody other, who uses the same firm's account (we are a couple of users having access to it), doesn't experiencing this issue.
How can i debug and fix this issue?
PS: the limitation to 100 users is definitely not the problem. After i experienced this problem firtsly i thought, this could be the cause.
But after i failed to use multiple Google Sheets published publicly and having multiple users and Google Sheets addons, published in Google's official addon marketplace, and having multiple users too, i don't think, the limitation is the cause of the issue.
This is the alert coming while authentification:
Comment: I'm voting to close this question because it is not programming related.  OP should contact the developer or company who has created the app they are having an issue with.  SO cant help you fix issues with an application you have not created.
Comment: For me it looks like an issue with Google account setup. Recommendation to ask developer of *every* app is not realistic.
Answer: Google has been increased the security on Google Accounts by denying access to many apps and services that previously had access to google accounts. Google announced a forthcoming change to how apps sign in and gain access to your Google account more then a year ago, to give app developers plenty of time to conform to googles requirements regarding account access.
If an app/service is showing the 'temporarily disabled' then the app developers need to work on getting their app verified by google in order to use Google Sign In.
More info on the security implications of allowing third-party apps and services to sign into your account and why these changes were considered necessary are provided here:
Better protection against Man in the Middle phishing attacks
app script addon
Client verification app script

Verification is not required for Apps Script projects whose owner and users belong to the same G Suite domain or customer.

you stated your users are not Gsuite there for the app needs to be verified before it can be used by people not on the domain of the developer who created the app.
Unverified apps
The following image looks exactly like the screen you are seeingand taken directly from Unverified apps documentation

The app needs to be verified there is no debug and fix for this issue
Comment: This is definitely not the problem. Apps, scripts and addons i'm trying to use are all in public access and have multiple users through the whole internet. Some of addons ican't use are published in the Google Sheets Addon repository and are used by everybody who installs it. After i experienced this firstly i thought like you, but after multiple cases with different publicly accessible Google Sheets and addons - no, this is not the cause.
Comment: App app scripts and addons stil have to be verified.   If the author hasnt bothered to do it yet then it is definitely the problem
Comment: I can't believe it. Mainly because of two things: i fail on using every single third-party sheet, addon or script, not SOME of them. And: no one from my colleagues experiences this issue.
Comment: I suggest you contact the developer of the App and see what they say.
Comment: All of them i contacted say different things: to re-grant access to their app (while their app is not in the list of apps with granted access), another one said, i should enable signin via Google (wtf?), even other said, i should pay for GSuite, and the next one said, he doesn't know, what happens to me, noone has reported similar issue...
Comment: See the problem is this is starting to sound like it is not your application we are discussing.  Which means this question is not programming related making it off topic for Stack Overflow.


Existe forma de esconder um url em uma página web?

Question: Vamos por partes, estou a fazer um site onde vou ler um URL da base de dados e a seguir vou usá-lo em uma imagem, ao clicar nela(imagem) vai abrir esse URL (dentro de um iframe ou em um novo separador).
No código, tenho uma página usando HTML e PHP onde mostro a imagem com o URL da hiperligação visível:
<!DOCTYPE html>

    <?php
        include ("connection.php");

        $sql = "SELECT * FROM table";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            // output data of each row
            while($row = $result->fetch_assoc()) {
                echo "
";
                echo "  ";
                echo "    ";
                echo "  ";
                echo "
".$row["name"]."
";
                echo "
";
            }
        } else {
            echo "0 results";
        }

        $conn->close();
    ?>

O resultado do código anterior é algo como isso:

Até aqui tudo bem, agora o que eu pretendia era não ser visível o URL, na imagem seguinte assinalei o que falo:

Existe forma de ocultar o URL usando por exemplo PHP ou alguma outra forma?
Answer: Antigamente, muito antigamente, existia um método para isso: window.status. Bastava você colocar no link algo como onmouseover="window.status='Olá'" que ao passar o mouse sobre o link aparecia "Olá" na barra de status do navegador em vez do link no href.
Porém, esse método foi abolido e não tem mais suporte nos navegadores mais modernos.
Uma solução é usar o método window.open com onclick abrindo a URL numa nova aba (alguns navegadores irão abrir em nova janela):
link

Desta forma irá aparecer "javascript:void(0)' no navegador em vez da URL, e a URL "destino.html" será aberta em uma nova aba/janela ("_blank").
Usando uma função e excluindo o script nas "opções de programador" (developers tools):
link

ou
link

Script com um id:

Comment: fiz uma edição na pergunta pois não se notava muito bem a outra seta, além disso preciso também de esconder no "código" o URL
Comment: @Tmc Atualizei a resposta mudando o href de "#" para "javascript:void(0)"
Comment: resultou o que da forma que fala para mudar não aparecer o URL ao fundo da página no entanto falta nas "opções de programador" veja na imagem da pergunta o que falo se não me perceber. Tenho duas setas na imagem.
Comment: @Tmc Entendi, vc quer ocultar nos dois lugares?
Comment: exatamente, precisava disso, existe forma?
Comment: @Tmc Existe criando uma função e atribuindo uma variável para a URL. Porém se o usuário visualizar o código fonte poderá ver a URL na função, mas é possível excluir a função nas "opções de programador".
Comment: @Tmc Acrescentei na resposta.
Comment: Vamos [continuar esta discussão no chat](http://chat.stackexchange.com/rooms/67363/discussion-between-tmc-and-dvd).
Answer: Você pode usar a função file_get_contents, e nela passar a URL da sua imagem. A função irá retornar um base64 da sua imagem, então é só você colocar esse base64 dentro da tag IMG, ficaria mais ou menos assim:
$base64 = file_get_contents('caminho_da_sua_imagem');
echo "";

Lembre-se que para cada extensão, é necessário informar no SRC:

PNG =
JPG =
GIF =
Comment: eu não queria escoder o URL da imagem mas sim o URL da hiperligação da imagem como, está identificado na imagem que postei
Comment: isso também pode ser usado para um URL?


Adding nested dict key to plist

Question: I am trying to add these keys to the root of a plist:
<dict>
  <key>11</key>
    <dict>
      <key>FeatureLockdown</key>
        <dict>
          <key>bUpdater</key>
          <false/>
        </dict>
    </dict>
</dict>

I have tried using PlistBuddy and defaults. I can't seem to get it to work.
Does anyone know a way to properly format this via terminal?
Answer: I was able to do this by looking at the man page for PlistBuddy and playing around in the Terminal:
/usr/libexec/PlistBuddy -c "Add :11 dict" Info.plist
/usr/libexec/PlistBuddy -c "Add :11:FeatureLockdown dict" Info.plist
/usr/libexec/PlistBuddy -c "Add :11:FeatureLockdown:bUpdater bool false" Info.plist

which gives a result of
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>11</key>
    <dict>
        <key>FeatureLockdown</key>
        <dict>
           <key>bUpdater</key>
           <false/>
        </dict>
    </dict>
...
...
...

Comment: Thank you. I realized my mistake was trying to do it all in one batch. Thank you for the help!

Adding nested dict key to plist


Proving existence of a lcm in a Unique Factorization Domain $D$ using the ascending chain condition?

Question: I'm aware there are many posts asking this question, but I was wondering if it was possible to prove this fact using ascending chains?
The problem (as in the title) is the following:  Let $D$ be a UFD.  Prove that for all $a,b \in D \setminus \{0\}$, the least common multiple $\text{lcm}(a,b)$ exists.
My idea was to go by contradiction and try to avoid the messy expansions I've seen in various proofs of this fact (which leads me to believe that my idea is obviously incorrect at some point).
Suppose that $a,b \in D$ are nonzero and that $\text{lcm}(a,b)$ does not exist.  Note since $a \mid ab$ and $b \mid ab$, there exist elements of $D$ that $a,b$ both divide.
Thus, given any $\beta_1 \in D$ such that $a \mid \beta_1$ and $b \mid \beta_1$, there must exist some $\beta_2 \in D$ such that $\beta_1 \neq \beta_2$ and $\beta_2 \mid \beta_1$.  Repeating inductively gives a chain of elements $\beta_k \in D$ such that $\beta_{k+1} \mid \beta_k$ and $\beta_{k+1} \neq \beta_k$.  Thus, we have that $\langle \beta_{k} \rangle \subsetneq \langle \beta_{k+1} \rangle$ for all $k$ which is a chain of principle ideals in $D$ that never stabilizes, a contradiction to the fact that all UFD's satisfy the ascending chain condition on principle ideals (ACCPI).  Thus, $a,b$ must have a lcm.
Is this correct, or did I make some obvious mistake somewhere?  I imagine someone more experienced than myself can point out any blunder I may have in this argument.
Thank you.
Answer: The argument is incorrect because it uses only ACCP (not UFD), and in a domain satisfying ACCP a (divisibly) minimal common multiple need not be a minimum (lcm), up to associates.
Indeed, if the argument were correct it implies that in every ACCP domain all lcms exist thus all gcds exist, so it is a UFD, but there are easy examples of non-UFD ACCP domains, e.g. many (quadratic) number rings.


C# - Manutenção de Classes geradas a partir de arquivo xsd

Question: Estou desenvolvendo uma aplicação que utiliza classes geradas a partir de arquivos xsd disponibilizados pela receita federal com o xsd.exe. Utilizo-as para serializar arquivos xml a partir dos objetos destas classes após realizar validação do que foi preenchido com o próprio xsd.
Até aí tudo bem, o problema é que estes arquivos xsd mudam de versão quando a receita federal bem entende, o que vai me causar muita dor de cabeça pra atualizar o xsd e a classe gerada a partir dele para todos os clientes.
Me forçaria a gerar novamente a classe C# a partir do xsd de nova versão e recompilar a aplicação. O que é muito ruim. Sem falar que se eu comer bola e algum cliente ficar sem a build a atualizada, nenhum xml gerado pela aplicação será válido.
Ok chega de choro, como sou muito amador, gostaria de perguntar:
Existe uma forma de manter os arquivos xsd e suas respectivas classes atualizadas sem tanto trabalho manual?
Comment: Pode adicionar duas versoes diferentes do ficheiro xsd á sua pergunta para ter um contexto melhor do problema?
Comment: @BrunoCosta acredito que o colega não está mais acompanhando a comunidade, mas, me parece uma pergunta ainda pertinente. Os XSD a que se refere são da documentação da nota fiscal eletronica, que podem ser encontrados aqui: http://www.nfe.fazenda.gov.br/portal/listaConteudo.aspx?tipoConteudo=/fwLvLUSmU8= (várias versões inclusive)
Comment: O problema é que se o XSD muda radicalmente, é porque as coisas foram mudadas por quem fez o esquema, o governo, infelizmente. Quando a mudança é incremental, menos o código a ser mudado.
Answer: A classe C# gerada é a representação do Esquema usado, no caso descrito a partir do arquivo XSD (Definição de Esquema XML), portanto é lógico que se o esquema original muda, a classe deve mudar de acordo. O ideal é não editar esta classe gerada pelo XSD.exe. Quando houver alterações, deve-se gerar novas classes usando a ferramente XSD.exe, adequar o código do programa, compilar e distribuir a nova versão.
De fato:
1) O problema é que se o XSD muda radicalmente, é porque as coisas foram mudadas por quem fez o esquema, o governo, infelizmente o código também tem que ser alterado.
Quando a mudança é incremental, menos o código a ser mudado.
Acho que no momento não é possível ter uma solução completamente automática para atualizar o codigo/lógica do aplicativo + dados do banco de dados conforme os esquemas do XSD do governo mudam.
Então por enquanto o que se pode fazer é quando houver alterações nos layouts / XSD, rodar novamente o XSD.exe criando as novas classes atualizadas, e adequar o código e lógica que utiliza estas classes.
2) Para manter o aplicativo atualizado no cliente você pode usar o ClickOnce com WPF, o .appinstaller ou MSIX (preview) com UWP.
Estas tecnologias podem ser configuradas para verificar se há nova versão do Aplicativo e então fazer o download e atualizar no cliente.
Comment: Ver também https://pt.stackoverflow.com/q/217909/5704


Do scales have a relation to certain genres or themes?

Question: If someone was creating music to fit a genre or to match a certain theme/mood would this influence their choice of which scale to use? Are some scales used more than others within a genre of music?
Answer:
Are some scales used more than others within a genre of music?

Short answer: yes!
I think an exploration of the relationship between genre and tonality could probably fill several very large books - partly because the term 'genre' itself can cover many different axes. For example, we can look at genre from a geographic point of view, and see that many regions of the planet are associated with particular scales. We could also look at genre chronologically, and see how the development of western music ties in with the emergence of the equal-tempered 12-tone scale and the extended possibilities it allows.
We can also zoom in to particular genres and see that many of them are associated with particular scales. The archetypal blues sound comes from the use of a particular scale played by bending and inflecting notes played on certain scale degrees. Particular modes of the diatonic scale tend to be associated with European folk music.
We can't necessarily restrict our concept of tonality just to scale, though. Different genres create their own harmonic language through use of - or avoidance of - particular chords and cadences. Minor seventh chords feature heavily in funk and disco, for example. Rock musicians might avoid playing minor chords (even if playing in a minor key) because minor thirds can sound very harsh through distortion. The grunge music of the 80s and early 90s tended to avoid V-I cadences, and often featured particular chord voicings that were created by drop tuning. These are examples of features of harmony that can't be explained just by relating a genre to use of a particular scale.

If someone was creating music to fit a genre or to match a certain theme/mood would this influence their choice of which scale to use?

As discussed above, to write to a genre, you need to do more than just use a particular scale - you need to be aware of the chord vocabulary, chord voicings, and cadences (or avoidance thereof) that are typical for that genre, as well as how it might modulate or otherwise vary in tonality.
To some extent, you can point to scales that are commonly-understood to have certain "meanings" - such as the "minor=sad, major=happy" correlation, or the association of the whole-tone scale with dream sequences/flashbacks. But as well as thinking whether those assumptions are still valid within the genre you're working in, you also have to consider how your audience is going to perceive the statement you're making. Is your use of a particular scale really going to communicate the feeling you want, or is it just going to come across as a tedious cliché?
One of the problems with trying to associate any particular sonic feature with a particular function or emotion is that as soon as the association becomes strong enough for it to be commonly understood in a certain culture, there is artistic capital to be gained in working against that association in unexpected ways. This is one of the forces that has led to the creation of new genres of music.


I want to merge 2 tabs into 1 page with using Selenium Python.Concretely,each page has just 1 tab and the result page has 2 tabs

Question: I wrote below code.
from selenium import webdriver
driver1 = webdriver.Chrome()
driver1.get('https://www.google.com/')
driver2 = webdriver.Chrome()
driver2.get('https://www.yahoo.com/')

Of course this code finally create 2 pages but I want to merge it into 1 page.
How do I implement it?.
Answer: You mean you want multiple tabs in one window?
I believe there's no such function in Selenium. However, you can run JavaScript to open a URL in a new tab using the execute_script() function.
driver.get("https://google.com")

script = "window.open('https://www.yahoo.com/','_blank');"
driver.execute_script(script)

Hope this helps.


How do I find all CR+LF's in a string?

Question: I'm trying to replace all carrage returns with a
 tag in C#.  I thought this would take care of everything:
StringBuilder sb = new StringBuilder(s);
sb.Replace(System.Environment.NewLine, @"
");

But apparently not.  It doesn't seem to catch CR+LF.
Comment: Have you verified that `s` contains `\r\n` and not simply `\n`?
Comment: `System.Environment.Newline` is going to depend on the executing environment. As ChaosPandion said, it is likely that you're actually just working with `\n`
Answer: That will work if Environment.NewLine is CR+LF, which it's likely to be on Windows. Of course it won't catch the situation where the string actually only contains line feeds, or only contains carriage returns. Perhaps you want:
StringBuilder sb = new StringBuilder(s).Replace("\r\n", "
")
                                       .Replace("\n", "
")
                                       .Replace("\r", "
");

(Note that there's no point in using a verbatim string literal for "
" as there's no backslash in the string, and it's a single line.)
Comment: @JimSchubert - Must be mind-recognition software or something :)
Comment: @Adel, It's most likely the rule "There are two types of programmers: good programmers, and those that are not Jon Skeet." [source](http://meta.stackexchange.com/a/9138/152872)
Comment: Why are you always 30 seconds faster than me :(
Answer: If you know for sure you will replace \r\n why not just use string.replace?
s.replace("\r\n", "
")

Answer: By this code
input.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n")

Answer: string ReplaceNewlines(string blockOfText, string replaceWith)
{
    return blockOfText.Replace("\r\n", replaceWith).Replace("\n", replaceWith).Replace("\r", replaceWith);
}

How do I find all CR+LF's in a string?


What are the pros and cons of doubling on two brass instruments?

Question: I am a trombonist and have played in a band for almost 3 years now. I am hoping to continue on by also doubling with the trumpet, and at the same time also continue to play the trombone.
The thing is: many have told me that there will be an embouchure problem if I play two or more brass instruments, but I still want to continue to be a trombone/trumpet player at the same time.
Can someone enlighten me about the pros and cons of doing so?
Answer: Speaking as a brass doubler (trombone is my primary; I play all other brass instruments with varying proficiencies), the main difference between the embouchures for trumpet and trombone has to do with the tone concept.
The trombone itself (and current pedagogy and instrument manufacturing) allows for a very open and dark tone concept, and the fact that tuning can be adjusted easily with the slide helps to allow for this.
If I play trumpet like I play trombone, it will have a very wide open sound, but certain partials will be woefully out of tune. When I play trumpet, I need to treat the oral cavity differently to enable the correction of certain partials with the embouchure. On good instruments less correction will be required, but you still need to be generating the correct amount of tension with your embouchure.
If there are any cons, it really depends on what your level of technique is, what you're aiming for, and how much professional instruction you're getting. Any doubler just needs to remember what instrument they're playing at the time and have a different mindset for each one. If you try to play the trumpet like the trombone, you're going to run into trouble, just as if you started learning trumpet and then played trombone like you were playing trumpet. Private instruction will let you know if you're developing any bad habits on any one instrument.
On the other hand, if you're self taught and just want to play ska music with your friends, chances are your technique isn't on a level where it'll make a difference.
The pros here are pretty obvious: you learn another instrument, you start opening your ears up to that instrument, you get a different perspective on music you already know, and it's just good general music practice to read notes in a different clef and transfer generic musical knowledge from one instrument to another.
Comment: And you can get more work if you can play more than one instrument competently.
Comment: Nice one, very inspiring overall for me :)

Anyway, I am just hoping to do continue playing the trombone (and not affect my tone) but also double with the trumpet, but this answer is quite clear, thanks!
Answer: The brass players I know and play with all seem to double on some other brass instrument. While it may take some time to adapt, I think it benefits to be able to know the different instruments.
Think of it this way if you drive a car: In your own car you get to know the clutch and know when to shift gears, then when you drive another car you suddenly get surprised by how high the clutch is and you may be jumping all the way home. But if you drive both cars regularly, your feet will know what to do.
Comment: @Hydra Even though piano and organ both use fingers to play, the techniques are very, *very* different.  I think it's definitely a good comparison on Thomas's part.
Comment: btw I am a pianist that also plays organ and whatnot, and people told me that playing organ would ruin my piano technique. I learned that the organ taught my fingers something very valuable when I transferred it to the piano.
Comment: Hmm, piano/organ is using fingers to play, but brass instruments use the mouth, and the embouchure for the different instrument differs.
Comment: I see. While it is indeed a good comparison, but I am hoping that someone does explain the pros and cons to me about doubling with trombone and trumpet, especially someone that tried it before. I do want to double both instruments, but I don't want it to affect my sound quality of either trombone or trumpet.
Answer: I am currently trying to double high and low brass. My trumpet playing isn't great yet, but T-bone is unaffected. No problem. I've been playing T-bone for years and marched the previous season. My band no longer marches T-bone, sad, but I though why not learn high brass? So I'm learning mellophone and trumpet. It is exactly like piano and organ or piano and synth or piano and harpsichord. I play both, more piano than organ, but the non-weighted keys give my fingers a more precise attack and release. My piano playing benefits greatly. I see the same in brass but with different aspects of course. I can hit high notes easier on T-bone now. And I've leaned to lip up where it was otherwise unnecessary on T-bone. (I already read both clefs but it otherwise would have been good to do so. I can read T-bone in treble now.)
Answer: I think it's good to learn more than one instrument. It helps you understand more about creating music that works well, both rhythmically and melodically. Also to understand more the roles musicians play in making music. I started playing sousaphone but then I bought a clarinet and a guitar and a tuba, I love them all equally and aspire to make good music with all of them. As for embouchure problems I haven't really noticed any, when I started playing tuba I could already easily play in the high register but it's a bit more difficult in the low register. The choice to play multiple instruments is quite personal I think, if you have a good ear for pitch and intonation discrepancies and always try to brush up your technique I think you can maintain 2 or more instruments. It depends on what your goals are as a musician.
Answer: My primary instrument is tuba but I know most other brass instruments. I actually play trombone and euphonium just as well as I play tuba. The only problem I've noticed is embouchure, but it isn't too much of a problem. This past year I had to play trumpet for jazz band, and it didn't necessarily hurt my low brass playing, but I busted my chops a lot easier than a normal trumpet player would because I'm used to playing low brass instruments. However, learning trumpet has helped a lot with my high range on low brass instruments, and knowing low brass instruments made it very easy to play low notes on trumpet.
Answer: I am a trumpet player who is learning the trombone, the benefits are that I'm learning bass clef and having to think hard about what I'm doing, my brain gets a good workout as I'm working through the Arban method for Trombone. On the down side I have to make sure that that I lay off the trombone and concentrate on trumpet before a concert or rehearsal as I need to re-educate my chops to play trumpet! I don't know if the day will come when I could play a solo on one and move to the other in the same gig, I treat the trombone as an intellectual challenge and a bit of fun at the moment although I'm sure once I became reasonably proficient and given enough notice I could play reasonably well and not embarrass myself too much!


Backbone.js: model save request has current url path prefixed

Question: In my backbone.js, when I call model.save() the url gets the current URL path prefixed in it.
Suppose my model is:
RegisterModel = Backbone.Model.extend({
    defaults: {
        id: ''
    },
    url: 'register/confirm'
});

My router is defined like:
AppRouter = Backbone.Router.extend({
    routes: {
        'signup/confirm/:key': 'confirm'
    },

    confirm: function(key) {
         var mod1 = new RegisterModel({key:key});
         mod1.save();
    }
});

Now if I understand the ajax request should be sent to
register/confirm
Instead it is sent to...
signup/confirm/register/confirm
What could be leading to this? Could this be because of pushstate:true that I am using to start Backbone.History???
Backbone.history.start({pushState:true});

I need this because I don't want to use hashed URLs.
Answer: Can you use URLs that are relative to the site's root? In that case you could try:
RegisterModel = Backbone.Model.extend({
    defaults: {
        id: ''
    },
    url: '/register/confirm'
});


Route 53 doesn't allow adding DKIM keys because length is too long

Question: Here is how I enter the value for DKIM key:
"v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwztXzIUqic95qSESmnqX U5v4W4ENbciFWyBkymsmmSNOhLlEtzp/mnyhf50ApwCTGLK9U7goo/ijX/wr5roy XhReVrvcqtIo3+63a1Et58C1J2o4xCvp0K2/lM6hla4B9jSph7QzjYdtWlOJqLRs o0nzcut7DSq/xYcVqvrFDNbutCfG//0wcRVUtGEyLX/a/7mAAkW6H8UEYMPglQ9c eEDfTT6pzIlqaK9cHGOsSCg4r0N8YxnHFMRzKaZwmudaXTorSbCs7e681g125/vJ e82VV7DE0uvKW/jquZYtgMn7+0rm+2FDYcDx/7lzoByl91rx37MAJaUx/2JHi1EA nwIDAQAB"

There are no new lines in this value (I specifically copy pasted and tested it in a text editor). But for some reason I keep getting TXT is too long error:

TXTRDATATooLong encountered at "v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwztXzIUqic95qSESmnqX U5v4W4ENbciFWyBkymsmmSNOhLlEtzp/mnyhf50ApwCTGLK9U7goo/ijX/wr5roy XhReVrvcqtIo3+63a1Et58C1J2o4xCvp0K2/lM6hla4B9jSph7QzjYdtWlOJqLRs o0nzcut7DSq/xYcVqvrFDNbutCfG//0wcRVUtGEyLX/a/7mAAkW6H8UEYMPglQ9c eEDfTT6pzIlqaK9cHGOsSCg4r0N8YxnHFMRzKaZwmudaXTorSbCs7e681g125/vJ e82VV7DE0uvKW/jquZYtgMn7+0rm+2FDYcDx/7lzoByl91rx37MAJaUx/2JHi1EA nwIDAQAB"

I really don't know what I should do to fix this issue.
Comment: I also have encountered this problem with AWS route 53 DKIM ... did you resolve this ?    I find that Amazon AWS is poorly maintained ... just try to use their Q/A Forum ... what a joke of ancient obsolete 15 year old web UI
Comment: Try removing the space after each ;
Comment: @ScottStensland As the accepted answer notes, this isn't AWS's fault, and there's a straightforward solution.
Answer: See a similar issue in Route 53 forum:

Unfortunately the 255 character limit per string on TXT records is not a Route53 limit but rather one imposed by the DNS protocol itself. However, each TXT record can have multiple strings, each 255 characters long. You will need to split your DKIM into multiple strings for your TXT record. You can do this via the console by entering each string encapsulated in quotes, one string per line.

Important note: Do not use "one string per line" as the instructions say -- separate strings with a single space, eg. "foo" "bar" not "foo"\n"bar". Use DKIMValidator to validate the signature is being read correctly.
Comment: Also make sure 1 space is between each end/start quotation - not a newline/cr as AWS shows when you add TXT records - these are not separate records.. Example: `"p=sdfgkjhsdfg...." "sdklfjsdf....."`  < space between.
Comment: Newline v/s space was the issue for me. Thanks @bshea
Comment: The space is what worked for me as well! I hope AWS changes this limit of 255 characters as everything is using larger hashes. This issue alone was submitted nearly 8 years ago from my comment today.
Answer: You can chop it up into chunks of quoted text with a max length of 255 per chunk. You don't have to make each chunk exact.
For example, if your value looks something like:
"v=DKIM1; k=rsa; p=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyza bcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabc"

You can chop it up at arbitrary places:
"v=DKIM1; k=rsa; p=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzab" "cdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabc"

Note those are single spaces, not newlines, between each chunk!
Google's setup instructions, unsurprisingly, are terrible. I just wrote up a complete guide for how to do this on Route53.
Comment: "Note those are single spaces, not newlines, between each chunk!"
This was important for me, reason it wasnt working. Worked immediately after switching, thanks!
Comment: This should be the accepted answer.
Comment: Oddly enough, spaces worked for DKIM, however when adding my SPF value to an existing TXT record, I had to separate the quoted strings with line breaks, not spaces, before it would work in AWS Route 53.
Comment: @andy-ray - Your website was a HUGE help, but just a heads up, a lot of the images don't load because they're pointed at localhost : )
Comment: I get the error ARRDATANotSingleField (Value contains spaces)
Answer: DKIM on AWS Route53
You must split the DMARC record into 255 character parts.
If you happen to host your DNS using AWS Route53, insert each part quoted with "..." into the record.
Do not use newlines to split the parts, because that would be separate TXT entries.
v=DKIM1; k=rsa; p=ABC123longkeypart1ABC123longkeypart2

becomes
"v=DKIM1; k=rsa; p=ABC123longkeypart1" "ABC123longkeypart2"

Using Terraform and Route53
When using Terraform to provision your Route53 records, you must split the DKIM-key into 255 max character parts as well.
Since Terraform takes care of quoting the TXT-entry, you must only provide quotes between each part, like so \"\".
v=DKIM1; k=rsa; p=ABC123longkeypart1ABC123longkeypart2

becomes
resource "aws_route53_record" "some_domain_dkim" {
  zone_id = "${aws_route53_zone.some_domain.zone_id}"
  name    = "google._domainkey.some_domain.com."
  type    = "TXT"
  ttl     = "3600"

  records = [
    "v=DKIM1; k=rsa; p=ABC123longkeypart1\"\"ABC123longkeypart2"
  ]
}

See also: https://www.terraform.io/docs/providers/aws/r/route53_record.html#records
Comment: If useful, there is a terraform module that handles long DKIM records automatically here: https://github.com/brightbock/dns-dkim-tf
Answer: You can simply select 1024 instead of 2048 and add this to route 53 at the time of generating the TXT in Google Apps
Comment: There is no reason to do this - if you want 2048 follow the selected answer. Connect the dkim record with quotes+single space between. It will work.
Comment: Downvoted: 1024-bit RSA keys are significantly less secure than 2048-bit RSA keys, and most guides today (and many even in 2016) recommend against their use
Comment: Same issue with 1024-bit keys, also :-)
Answer: On AWS Route 53, create or edit the TXT record for DKIM and go for the "use wizard" option rather than the quick entry method. The record name should be default._domainkey.yourdomain.com (or apropos) and the record type TXT. You know all that already but the sticky bit is 256 character records.
In the record value box cut n paste "everything inside the parenthesis" output from
sudo cat /etc/opendkim/keys/yourdomain.com/default.txt
Delete the quotes and place each quoted string in its own line unquoted and hit the submit button.
Go back and edit it and you will see the quotes have been put in. These quoted strings in quotes are on three separate lines causing multiple records rather than just one so edit the record value and replace the newline character with a space instead (the word wrap will make it appear the same but it is different in character when held in the zone record and for reasons beyond me, it appears as a single record splite into <256 char chunks rather than three separate records.
Thanks to Overbyrd above for pointing that out.
This is for quick n easy AWS entry putting a little more context incase I myself or others need to dig for info on  AWS Route 53 DNS TXT DKIM EC2 256 character opendkim issues long record issues.
Comment: using the Wizard WITH QUOTED chunks worked for me. Thank you. (what a joke Amazon!)
Answer: On Amazon lightsail, I had to enclose the separated texts in an additional quotes, such that:
"v=DKIM1;k=rsa; p=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"

Becomes:
""v=DKIM1;k=rsa; p=abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz""

Comment: This doesn't seem to be accepted by the Lightsail validation anymore. The only way I could make it work was to add a line break (no quotes were needed whatsoever). But upon checking using https://dkimvalidator.com, it still says my DKIM is invalid
Answer: The is how I got 2048 bit dkim to work on AWS route 53.
Initially it was this and it failed
"v=DKIM1; k=rsa;"
"p=MII...EndofKeyOne" "4tOt...EndofKeyTwo"
This is what worked below:
"v=DKIM1; k=rsa; p="
"MII...EndofKeyOne" "4tOt...EndofKeyTwo"
Note: (1) It just single space that separated the double quotes of key one from key two, not line separation.
(2) The first key started on the new line with the double quote sign ( I assumed it could have still start on the same with v & k specifications and would still have work).
(3) That p tag (public key tag) was inserted in the open & close double quotes that start the value i.e "v=DKIM1; k=rsa; p="
(4) The point 3 above is actually the missing puzzle that get the configuration done.
Answer: Use this to split the dns txt record in 255 limit
https://www.mailhardener.com/tools/dns-record-splitter
Just grab the text inside v=
"default._domainkey.blabla.app. 1   IN  TXT "v=DKIM1; k=rsa; p=asdfjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq9BqhK5AKY7d8aE3hEqUaqdth+0Vj9iCYUhl51iNonhJP84wVXvcZe5IRcIAGd7cLSs3nYfL/Nlp8NFIPiOmvjEp6DCVmUw5rQLwz876+umizg/Zg0LYgEUDo8od2YbBBAkNkrpWetaHf2AV+f5NeWio" "ocDMzS80pVuCdQVi/ONW5kJ1Oej/GqSfxg2aFXSN3F06GoyAJ38tCGgGGFJHJWJjP1mIQJGXJ46RsQxM2AKm42KDkROKU3YNjQMXmADwXLHKfr8bRzEUPCcf9zPj8FIpJDspFAiXpTxgOG5w5IlP04v5XjPfoyr+c1A71e1fYaYBpJ4+P6KnZdzcWbvGIQIDAQAB;""

eg.
"v=DKIM1; k=rsa; p=asdfjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq9BqhK5AKY7d8aE3hEqUaqdth+0Vj9iCYUhl51iNonhJP84wVXvcZe5IRcIAGd7cLSs3nYfL/Nlp8NFIPiOmvjEp6DCVmUw5rQLwz876+umizg/Zg0LYgEUDo8od2YbBBAkNkrpWetaHf2AV+f5NeWio" "ocDMzS80pVuCdQVi/ONW5kJ1Oej/GqSfxg2aFXSN3F06GoyAJ38tCGgGGFJHJWJjP1mIQJGXJ46RsQxM2AKm42KDkROKU3YNjQMXmADwXLHKfr8bRzEUPCcf9zPj8FIpJDspFAiXpTxgOG5w5IlP04v5XjPfoyr+c1A71e1fYaYBpJ4+P6KnZdzcWbvGIQIDAQAB;"


Typescript project hitting wrong debug point (visual studio 2015 cordova project)

Question: I am using visual studio 2015 RTM and making a hybrid app ,but it seems the typescript debugger is always hitting the wrong point (sometimes not even a break point), it arbitrary hit anywhere in any file in the project, below is the screen shot
Please help me with the issue
Comment: Alternatively you can set breakpoints in the browser and just forget about VS. I'm not diving into TypeScript (and any other source to js compiler) for these kind of issues :(
Comment: thanx for reply , at present i am doing that only but there are some cordova related things which are only visible when i debug in vs
Comment: Can you use developer tools in cordova to check if the map file maps the line correctly there?
Comment: HI, I encountered the same problem. The execution point in the "call stack" window is CORRECT but the code window shows WRONG line.
Comment: @smartkid did you find any solution ??
Comment: @PranayDutta  not for now ......   I think the issue is about "Call Stack" rather than "Breakpoints". When I double click in the "Call Stack" window, VS highlights incorrect source file/line. I am still fighting ....
Comment: @PranayDutta Just a workaround:  Disable the source map when generating "appBundle.js" and set breakpoint in `appBundle.js` rather than in `*.ts`
Comment: @PranayDutta I put a tsconfig.json with line `"sourceMap": false`
 in the `scripts` folder which all `ts` files live in.
Answer: Finally I get the debugger work !
I removed all /// <references path="" lines at the top of all .ts files.
The downside is, modules can NOT use import for other internal modules because the imported module variable may (or may not) be initialized after the importing module in appBundle.js.
Comment: but i am keeping a compilation sequence in index.ts and for that i need to give the references in which it needs to compile
Comment: when i am running in windows 10 emulator,works like a charm,seems that android and ios are breaking it apart
Answer: this issue has been resolved in tools for apache cordova update 2


Integral $\int\limits_0^y \text{d}x \sin(a x+b/x)/x$

Question: I am interested in this integral (for $a,b\in\mathbb{R}$):
$$ F(a,b;y) := \int\limits_0^y \text{d}x \, \frac{\sin(ax+b/x)}{x} $$
Usually for integrals of this type it is useful to transform $\tilde{x} = 1/x$, but in my case the integral boundaries will get changed as well:
$$ F(a,b;y) := \int\limits^\infty_{1/y} \text{d}\tilde{x} \, \frac{\sin(b\tilde{x}+a/\tilde{x})}{\tilde{x}} $$
So I conclude that this transformation is only useful for the integral over the entire real line, and not in my case.
In the case of $b=0$ one has of course $F(a,0;y) = \text{Si}(a\,y)$. Is there a special function for the case $b\not=0$? Any ideas?
Comment: What is your question exactly? Even for $b=0$ your integral is not an elementary function of $y$. You will need some special functions here.
Comment: Of course, it will just be the sine integral in the case $b=0$, that is, F(a,0;y) = Si(a\,y). My question is if there exists a special function that is the solution of this integral. I will add a line to clarify this.
Comment: Ok. Your question is clear now..
Answer: I would enjoy to have a special function for that.
The only thing I have in mind is to simplify the problem using $x=\frac y a$ and $c=ab$ to make
$$\int\frac{\sin \left(a x+\frac{b}{x}\right)}{x}\,dx=\int\frac{\sin \left(y+\frac{c }{y}\right)}{y}\,dy$$ and then expand the integrand as a series built at $c=0$ to get
$$\frac{\sin \left(y+\frac{c }{y}\right)}{y}=\sum_{n=0}^\infty \frac{ \sin \left(y+n\frac{\pi  }{2}\right)}{y^{n+1}\,n!}c^n$$ and then use
$$\int \frac{\sin(y)}{y^k}\,dy=-\frac{1}{2} y^{-k} \left((-i y)^k \Gamma (1-k,-i y)+(i y)^k \Gamma (1-k,i y)\right)$$
$$\int \frac{\cos(y)}{y^k}\,dy=-\frac{1}{2} i y^{-k} \left((-i y)^k \Gamma (1-k,-i y)-(i y)^k \Gamma (1-k,i y)\right)$$


R: difference between Generalized Least Square and the Standard Least Squares with Cholesky

Question: According to Wikipedia (source of all truth and knowledge...),
http://en.wikipedia.org/wiki/Generalized_least_squares#Properties
a weighted least square regression is equivalent to a standard least square regression, if the variables have been previously "decorrelated" using a Cholesky decomposition.
I made up then a very simple example with the function pgls from the package CAPER to test it, where the correlation arises from a phylogeny tree:
tree.mod:
((A:0.2,(B:0.1,C:0.1):0.1):0.1,((E:0.1,F:0.1):0.1,D:0.2):0.1);

The two approaches are compared here:
library(caper)

## Data
species = c("A","B","C","D","E","F")
gene = c(0.1,0.2,0.3,0.5,0.6,0.7)
pheno = c( 0,0,0,1,1,1)
data=data.frame(species,gene,pheno)

## Phylogeny
tree = read.tree( "small/tree_small.mod" )

## GLS regression
cat("\n     ===> GLS\n")
cdata   = comparative.data( phy = tree, data = data, names.col = "species" )
res = pgls( pheno~gene, cdata )
print(summary(res))

## Cholesky
cat("\n     ===> Cholesky\n")
corr = vcv( tree )
cholesky = chol( corr )
invCho = solve( cholesky )
data.gene =  invCho %*% as.vector( data$gene )
data.pheno =  invCho %*% as.vector( data$pheno )
res=lm( data.pheno ~ data.gene )
print(summary(res))

and yield the outputs:
====> GLS
Coefficients:
        Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.13000    0.27261 -0.4769  0.65834
gene         1.63333    0.59489  2.7456  0.05161 .

=====>Cholesky
Coefficients:
        Estimate Std. Error t value Pr(>|t|)
(Intercept)  0.02214    0.28551   0.078    0.942
data.gene    1.29188    0.35006   3.690    0.021 *

as you can see the results are different...
Does anyone have a clue why?
Answer: I think one of the issues is that you're only correcting one of the explanatory variables - there is an intercept in the model too and the transformation has to be applied to the entire model design matrix.
Explaining it to myself a while ago, I made the example below showing how the two models are calculated by the functions and through solving the linear equation Ax=b using the original and transformed data to generate the lm and pgls coefficients.
I can't work out how to tweak your example to replicate this. The vcv matrix is a covariance, not a correlation matrix and solve(cholesky) and chol2inv(cholesky) give different answers so it may be some combination of tweaking those bits of the code.
library(caper)

# a phylogeny
tree <- read.tree(text="((t2:1.46,(t6:0.73,t1:0.73):0.733):1.18,(t3:0.119,(t5:0.0554,t4:0.0554):0.0635):2.53);")
group <- gl(2,3, labels=c('Clade1','Clade2'))

# a data frame of variables and a comparative dataset
data <- data.frame(species = tree$tip.label, A = 1:6,
                  b = c(3.068, 2.213, 2.529, 5.681, 6.23, 6.05))

cdata <- comparative.data(tree, data=data, names='species')

# create a design and response matrix for the model
Amat <- model.matrix(b ~ A, data=data)
b <- as.matrix(dat$b)

# OLS
# solving Ax=b for x:
# x = (A^TA)^{-1} A^Tb
x <- solve(t(Amat) %*% Amat) %*% t(Amat)%*%b

# PGLS

# same basic equation Ax=b, but correcting for phylogenetic signal
# get the inverse of the VCV matrix
V <- VCV.array(tree)
iV <- solve(V)

# definition of inverse: VV'=I
zapsmall(V %*% iV)

# apply phylogenetic correction to A and b
Amat.prime <- iV %*% Amat
b.prime <- iV %*% b

# solve Ax=b for x
# x = (A^TA')^{-1} A^Tb'
x.phy <- solve(t(Amat) %*% Amat.prime) %*% t(Amat)%*%b.prime

# CHECK USING LM AND PGLS
mod <- lm(b ~ A, data=dat)
pmod <- pgls(b ~ A, data=cdata)

# Visualise
par(mfrow=c(1,2), mar=c(3,3,1,1), mgp=c(2,1,0))
palette(c('khaki','salmon' ))
plot(tree)
tiplabels(pch=21, col='black', bg=group, adj=0.8)
plot(b ~ A, data=dat, pch=21, col='black', bg=group)
abline(x, col='grey', lwd=3)
abline(mod, col='white', lty=2, lwd=1.5)
abline(x.phy, col='cornflowerblue', lwd=3)
abline(pmod, col='white', lty=2, lwd=1.5)

Comment: Thanks, concerning the covariance matrix,I replaced it by a correlation matrix using vcv(tree,cor=TRUE), but the result of the Cholesky result is the same. What do you mean by "you're only correcting one of the explanatory variables" ? Both input variables (data.gene and data.pheno) have been multiplied by the inverse of the Cholesky matrix. Besides that, the discrepancy between the GLS and the Cholesky approaches diminishes when using chol2inv rather than solve().


Should I partition a RAID or just create a file system on it?

Question: Given a RAID 6 created from 6 drives /dev/md0 which should be used fully for data (no booting from it, root and swap are on a different drive), should I create one partition on that device and then create a file system in that partition, or should I just create a file system on the device?
parted /dev/md0
  <create a partion>
mkfs -t ext3 /dev/md0p1

or just
mkfs -t ext3 /dev/md0

And why?
(This is Ubuntu Server, just in case it matters.)
Comment: Why would you create a partition on a device that was already being partitioned? I don't know if there are any performance issues for this but unless you want to use a LVM creating an additional partition makes absolutely no sense. General rule of IT: If there are two approaches for one task and one is more simple than the other, the simple one is the best one.
Comment: @kasperd you are right, it's a duplicate by content but I did not find that question because the wording is totally different from mine.
Comment: @Broco I have just been looking at examples on creating RAIDs with mdadm and some of them partition the RAID, others don't but I found no explanation why I would want to do one or the other. So I asked.
Comment: @Broco: Right. But even for LVM, you don't need partitions, you can use `/dev/mdX` as PV directly.
Comment: @SvW yes, you are right of course. Partitioning would only make sense if you would put multiple partitions on the one RAID. But still it is suggested to just create a raid-partition for each file-system-partition.
Comment: @dummzeuch It's ok to ask and it's nowadays possible to create multiple "native" (meaning no LVM) partitions on one RAID-volume but since you said you just need one partition on this raid this would make no sense.
Comment: @dummzeuch That happens to most of us occasionally. Either one doesn't for just the right terms to find it, or as it happened to me once, the previous question had a screen shot containing the exact phrase I had searched for.
Answer: Arguments against the partitioning:
Partitions are strict, obsolete things in the today. A repartitioning mostly a problematic thing. You won't be able to change things in the future.
Raid devices are directly partitionable only in newer kernels.

Thus I suggest you to simply use the whole disk directly, if you won't use some advanced solution. On Linux, LVM is a such advanced "partitioning" system. It enables for you to dynamically create/move/resize/remove partitions, even below a running system, transparently.
Answer: There is no added value of creating a partition table there, unless you want to subdivide the amount of space further.
If you need to subdivide the space further you would possibly benefit most from using the logical volume manager instead of creating partitions and then you can still skip the creation of a partition table and address the whole RAID 6 volume as a LVM physical volume.
The advantage of not creating a partition table and not using partitions comes mainly in play when you extend the RAID array with additional disks.


Is it possible to disable dropbox notifications for app folder

Question: I'm playing around with dropbox.js and have created a web-app to take notes and save them to my dropbox account.
Is there a possibility to turn off notifications for only this one App folder without turning off all notifications?
Comment: It's not possible to switch off notifications for just one folder (or more). The only way to do it is to stop syncing this folder (Preferences > Advanced > Selective sync) which is not your goal.
Answer: These days, with API v2, you can mute notifications on individual upload operations via the mute parameter on /files/upload.
See https://www.dropbox.com/developers/documentation/http/documentation#files-upload for the detailed API documentation.


Expected value in Markov chains

Question: Let $\left\{X_{n}\right\}_{n\geq0}$ be a homogeneous markov chain with state space E and transition matrix P. Let $\tau$ be the first time n for which $X_{n}$ $\neq$ $X_{0}$, where $\tau=+\infty$ if $X_{n}=X_{0}$ for all $n\geq0$. We need to compute $E[\tau|X_{0}=i]$ in terms of $p_{ii}$
My solution
For any $l>0$ let $l$ be the first for which $X_{n}$ $\neq$ $X_{0}$ which means that $\forall$ $m$ such that $1\leq m <l$ the value of $X_{m}=X_{0}$ but $X_{i} \neq X_{0}$. The probability of this happening is the product of probability of $P_{ii}^{m}$ for all $m$ multiplied by $(1-P_{ii}^{l})$. The resulting product is then again multiplied by the value of $l$ and then summed over for all possible values of $l$ i.e from 1 to $\infty$ to find expectation
The expected value therefore is $(1-P_{ii}^{1})+2(1-P^{2}_{ii})P_{ii}^{1}+3(1-P_{ii}^{3})P_{ii}^{2}P_{ii}^{1}+.....=\sum_{j=1}^{\infty}(j(1-P^{j}_{ii})\prod_{k=0}^{j-1}P^{k}_{ii})$ where $P^{0}_{ii}=1$
Is this correct ?
Comment: Your notation seems inconsistent.  You begin by saying the chain is "homogeneous".  However, examining your expression it seems you are using $P^k_{ii}$ to denote the transition probability *at time step $k$* rather than the probability to transition $i\to i$ in $k$ steps.  (I have assumed a time homogeneous chain for my answer below.)
Answer: You are thinking along the right lines but it seems your notation is obscuring things (see bottom of this answer).
The event $\tau = k$ means we remain in state $X_0$ for $k-1$ steps (which has probability $p_{ii}^{k-1}$) and then jump to another state on step $k$ (which has probability $1-p_{ii}$).  Consequently,
$$
\mathbb{P}(\tau = k) = p_{ii}^{k-1}(1-p_{ii}).
$$
This "waiting for an event to happen (in a time homogenous setting)" time distribution is called the geometric distribution.
The expectation can be evaluated with a "differentiate under the integral" trick:
$$
\mathbb{E}[\tau] = \sum_k k p_{ii}^{k-1}(1-p_{ii}) \\
= (1-p_{ii})\sum_k \frac{d}{dp} \left. p^k \right|_{p=p_{ii}} \\
= (1-p_{ii})\frac{d}{dp}\left. \sum_k  p^k \right|_{p=p_{ii}} \\
= \frac{1}{1-p_{ii}},
$$
the differentiation in the infinite series being ok because all terms are non-negative.  Also we used the formula for a geometric series.

Usually "homogeneous" means time homogeneous for Markov chains.
With the usual notation for transition matrices $P = (p_{ij})_{i,j \in \mathcal{S}}$, $P^k$ is the $k$-step transition probability (just the matrix power).  You appear to use $P^k$ for the transition probability at time step $k$ (which is different to the usual convention).

With this understanding, taking $P^j_{ii}=p_{ii}$ (in your notation) makes your attempt look right.

Expected value in Markov chains


SEM: factor analysis and categorical data

Question: Lets say I want to run the following model in R/lavaan:
F1 =~ V1 + V2 + V3
F2 =~ V4 + V5 + V6
F1 ~~ F2

V1-3 are continuous indicators, V4-6 are categorical (binary, nominal/not ordinal). Sample size ~1000
An example of the F2 factor might be trying to capture something about a person's level of fitness, so indicators might be:
Did you ride your bicycle or drive to school today? (Response: Bicycle/car)
Did you go to the gym today? (Response: yes/no)
From what Ive learnt so far, categorical indicators will work if you can safely assume that theres an underlying continuous normal distribution driving the categories.
If you cant meet that assumption, say if V4-6 are nominal/categorical like the examples above, what would be the recommended approach?

Can you introduce some form of logistic regression?
And if so, how would that be done using lavaan and how would you interpret the output?
Which estimator should be used in a case like this?
Comment: Can you indicate which kinds of true nominal variables could be under influence of a latent factor? I could not think of a latent factor that would "influence" the gender of a person (while vice-versa it could work of course).
Comment: Gender was a bad example. I added more realistic examples to my question
Comment: I guess I'm mostly wondering how logistic regression might work into this. I think thats what the IRT world does with this type of data?
Answer: When you ask "Did you go to the gym today?" going to the gym is an outcome with multiple causes, if these causes manage to pass some threshold you went, otherwise you didn't. So I think it's fine to consider this to be a latent variable.
In addition, your model looks like this:
F1 =~ V1 + V2 + V3
F2 =~ V4 + V5 + V6
F1 ~~ F2

But say that V6 was your nominal variable, and you are not comfortable treating it as an indicator of a latent variable, so instead you regress it on the latent, essentially doing a logistic regression.
F1 =~ V1 + V2 + V3
F2 =~ V4 + V5
F1 ~~ F2
V6 ~ F1

It turns out that these models are equivalent - they are the same model.
In short, I don't think you need to worry.


How can we sing in tune, given a perfect ear for music, if we always hear ourselves lower than we actually sound?

Question: According to this BBC article, we hear ourselves bassy compared to how we sound to others.

What makes a recording of our voice sound so different... and awful? It's because when you speak you hear your own voice in two different ways. Greg Foot explains all.
The first is through vibrating sound waves hitting your ear drum, the
  way other people hear your voice. The second way is through vibrations
  inside your skull set off by your vocal chords. Those vibrations
  travel up through your bony skull and again set the ear drum
  vibrating. However as they travel through the bone they spread out and
  lower in pitch, giving you a false sense of bass. Then when you hear a
  recording of your voice, it sounds distinctly higher.

How do we then sing in tune to a song? We must be doing a constant pitch correction while trying to reproduce a previously heard sound. How does this pitch correction actually work?

EDIT 2016.02.09:
Added link to, and a quote from the BBC article which I've read before asking this question, and which reconfirmed a wrong assumption on which my question was based.
Comment: I think you're getting pitch and tone or timbre mixed up. The timbre can have a low character even at high pitches. What we hear in our heads has a different timbre from what others hear but it has the same pitch.
Comment: "we hear ourselves bassy compared to how we sound to others" Do we? Just for my curiosity, can you back that up with one or more references?
Comment: Before asking, I've read at [BBC](http://www.bbc.com/future/story/20130913-why-we-hate-hearing-our-own-voice) we hear us in 2 ways "The first is through vibrating sound waves hitting your ear drum, the way other people hear your voice. The second way is through vibrations inside your skull set off by your vocal chords. Those vibrations travel up through your bony skull and again set the ear drum vibrating. **However as they travel through the bone they spread out and lower in pitch, giving you a false sense of bass. Then when you hear a recording of your voice, it sounds distinctly higher.**"
Answer: The BBC article is confusing pitch with frequency response.
When one says your voice sounds lower to you than it does to other people, this is referring NOT to the pitch of your voice (the dominant vibrations-per-second frequency), but to the overall timbre and tone.
Any sound made by acoustic means consists of an entire spectrum of pitches, where certain frequencies on this spectrum stand out more than others. The relative strength of these frequencies causes the phenomena of timbre, for example, how Bob and Jack both singing a C4 can still sound like Bob and Jack to the listener. This is also why if you have a multi-band equalizer, a change in any one of the EQ bands will have an effect on your voice, even if you are singing the same pitch each time.
So to specifically address your question, when you are listening to your own voice, what you are hearing includes physical vibrations being transmitted inside your own body from your vocal cords and oral cavity to your cochlea. Lower frequencies are conducted much more efficiently this way than they are through the air to the eardrums of all of your listeners.
So, you are getting more response from the bass frequencies of your own voice than an outside listener would, but the pitch isn't changing. It's like being inside your own head activates the "bass boost" button just like on an old tape deck.
Comment: @ToddWilcox I don't remember a lot of high school physics, but I remember this being pretty important--materials change wavelength because they ALSO change the speed at which the wave is moving. The frequency stays constant because all three factors are related. Think of the border between two different materials (two different slinkies tied together is a good visualization): the waves are reaching and crossing that border at a regular time interval, but then the waves _propagate_ through the material with a different speed and wavelength.
Comment: Yes. The phenomenon that is referred to in the question is like an EQ with bass frequencies boosted, not Auto-Tune modifying the pitch.
Comment: @ToddWilcox I just watched the video referenced, and I would argue that it is incorrect. Materials can't shift pitch, they can only filter it. If you inhale helium, your voice still sounds higher to a listener even though the entire room is not filled with helium.
Comment: Yeah, and there's this: http://physics.stackexchange.com/questions/193609/why-does-medium-not-affect-the-frequency-of-sound So ok that makes sense. So the phrase "lower in pitch" in the BBC quote is the erroneous one. (And belated +1)
Comment: I haven't voted up or down on this question (yet) since on the one hand it addresses what seems to be a misconception about the difference between timbre and pitch, and on the other hand doesn't address the material added to the question from the BBC site which correctly points out that bone conduction **does** lower the frequencies of sounds that are transmitted inside our heads.
Comment: So the BBC quote is actually incorrect? After doing some web searches it does seem to be true that sounds do not change in frequency in different media, only wavelength. And our pitch perception seems to be based more on frequency. And yes I meant I haven't voted on the answer yet.
Comment: @Todd, I think you meant that you haven't voted up or down on this **answer**, not question.


How to get view mode of a node?

Question: So my question is how I can get programmatically in preprocess node hook, the display mode of a node, for example, to check if view mode is a teaser or full?
Answer: The view mode is in the vars:
function MYMODULE_preprocess_node(&$vars) {
    $view_mode = $vars['view_mode'];
}

See template_preprocess_node() or node.html.twig for more info.
Comment: Exactly right :) (well, you might want a "t" in the "easer" but I'm guessing you know tha)
Comment: ...wouldn't have access to `$vars['node']`, and so on
Comment: @leymannx If memory serves this goes back to a convention that grew up from D6 - `template_preprocess_*` is basically Drupal's way of saying _this theme has an implied, required preprocess that sets up variables, and here it is_. If I had to guess, I'd say it was to ensure that those required variables would be available to modules/themes subsequently implementing the preprocess hook. Otherwise, if it was `node_preprocess_node`, for example, the node module would have to fight with the other modules implementing the same hook in terms of module weight. `foo_preprocess_node`...
Comment: Thanks for respond Clive. Exactly what I've needed so proper condition would be                                                                                `$view_mode = $vars['view_mode'];
  if ($view_mode == 'easer') {                                                                   Do something                                                                                }`
Comment: @leymannx Just FYI I reverted that last edit, I didn't see the improvement in it - preprocess hooks are equally valid in modules or themes
Comment: I just applied standard indentation, wrote `$variables` out, and prefixed the hook with `MYTHEME` as you linked to the `template_*` hook and not to `hook_preprocess_HOOK`.
Comment: `template_preprocess_node` is in a module @leymannx...
Comment: I always thought they are prefixed `template_` as they are supposed to be used in themes (`template.php`, now `*.theme`). Always confuses me why there seems to be so many duplicate documentation as both hooks are doing just the same...
Comment: Ah I see. That makes sense! So one could say `template_preprocess_node` is just a consolidated/fixed version of `hook_preprocess_HOOK`?!
Comment: Yeah pretty much @leymannx :)

How to get view mode of a node?


Probability Density function of Poisson distribution

Question: This is an assignment I got for my course on Stochastic Processes:

Let us consider a random variable X distributed as a Poisson P (λ)
where λ ∼ [0.5, 1].
(a) Which are the unconditional mean and variance for variable X? (DONE)
(b) Which is the probability density function of X? (Not need to solve
the integral)

I managed to do the first part (a) but the second part (b) doesn't make sense to me.
How can X have a probability density function if X is a Poisson and the poisson is discrete?
Am I missing something?
Also, the professor says that there is no need to solve the integral, but how can there be an integral if the Poisson is discrete?
I guess the answer lies in this part:

λ ∼ [0.5, 1]

But I can't find it.
This is what I did to solve (a)
Comment: I suspect the vague notation "$\lambda\sim[0.5,1]$" might be intended to stipulate that $\lambda$ is a random variable with a uniform distribution on the interval $[0.5,1].$
Comment: @Xi'an I don't know, she didn't add anything to that.
Comment: @Xi'an Could you please explain me how to do that?
For question (a) I simply demonstrated that the mean and variance of a poisson is given by λ.
Answer: Question (a): One need call the Law of Total Expectation and Law of Total Variance.

 $$\mathbb{E}(X)=\mathbb{E}[\mathbb{E}(X\mid \lambda)])$$
 where $\mathbb{E}(X\mid \lambda)=\lambda$ and
 $$\operatorname{Var}(X)=\mathbb{E}[\operatorname{Var}(X\mid \lambda)] + \operatorname{Var}(\mathbb{E}[X\mid \lambda])$$
 where $\operatorname{Var}(X\mid \lambda)=\lambda$

Question (b): If the integral need not be solved, the marginal density of $X$ can be written as the integral (when $k\in\mathbb{N}$)

$$p(k)=2\int_{0.5}^1 \frac{\lambda^k}{k!}e^{-\lambda}\,\text{d}\lambda$$
Comment: I'm having trouble understanding how to calculate Var(X)=[Var(X∣λ)]+Var([X∣λ]).
I think it is:  Var(X)=[Var(X∣λ)] + Var(λ).
Where the Var(λ) is the variance of an uniform distribution, but I don't understand how to calculate [Var(X∣λ)].
Comment: $\text{var}(X|\lambda)$ is the variance of the Poisson $P(\lambda)$ distribution.


A luigi task fails if the next file is already completed

Question: I have a pipeline built with Luigi in which some taks require other tasks and each task creates a file.
Something like:
TaskA-------->TaskB---------> TaskC-------->TaskD
(fileA)      (fileB)         (fileC)       (fileD)

The first time I run the pipeline, everything runs well and gets created.
If I run the pipeline again, nothing gets run since TaskD was completed already.
If I manually delete fileB (made by TaskB), I expected it to be recreated and everything else running but the pipeline fails.
yes, fileB gets recreated but TaskC fails, with the error message saying that fileC already exist.
Is there a way for the subsequent files to be recreated again? Or fileC be overwritten?
Answer: What you are describing is exactly how Luigi is set up to run. When you specify which tasks to run (in luigi.build or the cli), you are specifying which tasks need to be completed for Luigi to consider its job done. Luigi will not care about your pipeline unless you tell it to. One way to do this would be to inform Luigi of all tasks you do care about, which in this case seem to be TaskB and Task D. So, it would look something like:
luigi.build([TaskD(...), TaskB(...)])


The best way to synchronize old host and new host when migrating a Drupal site?

Question: I'm planing to migrate my live site from the current shared host to another shared host. I'm thinking to use Backup and Migrate module or importing/exporting mysql by phpmyadmin.
However I'm concerning about the new users who register on my site and their content profile node. Because on both old and new host users will be registered for some while (maybe 2 weeks?). And in this period there probably will be duplicated User ID and Node ID with different data on both hosts.
Is there any way to prevent this or synchronize both old and new site for some time?
Or is it safe to import eventually the data of discrepancy from the old one to the new one just changing uid and nid after name server propagation is completed?
Comment: I just came up with an idea. What about if I change the setting.php in the old server like this $db_url='old mysql server' to $db_url='new mysql server'?  Then whatever change will be done in the new server I suppose. Does this work??
Answer: I think you're on the right track. It is nearly impossible to implement a migration project and not plan for incremental updates. The migrate module supports that with its "update" function (as shown in the screen shot).
That said, there shouldn't be a problem of duplicate UIDs after the initial migration of data, unless you are letting people create accounts on both the legacy system and the new instance.
Comment: Is that screenshot from [Backup and Migrate](http://drupal.org/project/backup_migrate) or [Migrate](http://drupal.org/project/migrate)?  @chinita7 mentioned Backup and Migrate, but it sounds like you are recommending Migrate an an alternative solution.
Answer: I might be way off here, but since we know that DNS update takes only up to 24 hours at most, why not just put an upgrade notice on the old site and disable new content and user creation for a couple of hours while the migration is progressing?
Answer: if i get it right, you are afraid it would get 2 weeks for people (or their computers) to notice the change of DNS ? (is that the only reason for running two sites in paralel ?).
That shouldnt be necessary. You should be able to set the TTL (time to live) on your current DNS records and then (after some time - 24hours minimum) change it to the new IP adress and it shouldnt take more than a day for everyone to realise.
(see http://www.dnswatch.info/articles/dns-update  - other records).
Anyway, if you still think you want people to work on 2 sites :). You could setup the next value for the ID on the important tables (node, user, comment, file ?),
<code> ALTER TABLE tbl AUTO_INCREMENT = 100;
</code>
probably on the new database so that it would be possible to merge the two databases afterwards. But it still is quite complicated - you cant just use backup migrate - you cant replace the databse, you have to MERGE it.
Probably something like deploy could be of help, but rather harder to set up if its just for the few days.
Comment: The reason I would run the site on two server is that I thought it would take up to 2 weeks until DNS change is done completely because of the DNS cach. But if 24 hours is enough ideally I will just set TTL on my current DNS recored as you mentioned. As soon as possible I need to close the old one and just use the new one. Changing important ID by sql before merge it might work though I have no experience with that. So then I suppose I need to put AUTO_INCREMENT somewhere in node module and user module?
Comment: i think the best to do is to avoid the need to merging the databases :). Should you want to set the auto increment , its just done once by running that command - basicly mysql stores the next id value and you would tell it to use a higher one, "move it forward".
Answer: Create and import the database in the new server, Use this db from the shared hosting changing the data in setting.php so all the new changes will be in one database.
Then you change the dns for your domain name and should be ok.
Or do the opposite:
leave the db in the shared hosting and use it from the new server, then put the website offline for 5 minutes and import the new database.
Be careful because you will work with one db, and the changes will affect both website.


How to Convert TIFF to JPG Inside Silverlight, client side, using a control or a class/function?

Question: We are creating a client in Silverlight that will show a lot of TIFF images. Silverlight natively do not support TIFF. I need a control/class to convert tiff to jpg in runtime inside the Silverlight client. Any idea?
Answer: In Silverlight forum last friday I received this post:

Re: How to Convert TIFF to JPG Inside Silverlight, client side, using a control or a class/function?
12-18-2009 5:38 PM |
If you're willing to use a third-party library, check out ImageGear for Silverlight provided by Accusoft Pegasus.  It's a 100% managed Silverlight toolkit (http://www.accusoft.com/ig-silverlight.htm), and runs entirely on the client.  TIFF support is included (among lots of other formats).
Hope this helps,
Casey
Answer: I was successful displaying TIFFs in Silverlight. It's easy to port the free LibTiff.NET library to Silverlight, just 3-4 minor tweaks required.
The library itself is quite legacy-like and raw to use and one still needs to have some knowledge about the inner workings of the TIFF format in order to be able to extract the image data the way one needs it.
But it's doable and the bits and pieces can then be chiseled into a WriteableBitmap.
Comment: helping out @Dataji who probably couldn't comment
http://stackoverflow.com/questions/1923140/how-to-convert-tiff-to-jpg-inside-silverlight-client-side-using-a-control-or-a/2712743#2712743
Comment: Can you make your changes public?
Comment: @kenny they're gonna release a new version soon with the small changes that enable Silverlight support. I also contributed a demo application. Write them an email and you can have a beta version.
Answer: Not sure you are interested in solution by now, but we've just released LibTiff.Net 2.0 with Silverlight support, improved documentation and samples.
There is Silverlight Test Application in source package that shows how to create WriteableBitmap from TIFF images in Silverlight. Maybe it will help you or others.


contact from on aws with jquery and lambda

Question: I have a static website, hosted via an aws S3 bucket and using cloudfront.
I have been trying to set up a contact form, using jquery to turn the form into python and submit to my aws API gateway (which in turn calls a lambda function to send the form as an email via SES).
I have followed some tutorials, and though I can run a working test on the AWS test function (in both the API gateway and lambda), it never works on the site.
When I click the submit button on the website, the page just refreshes - there is no error or success message, and no email sent.
As it works in the aws console (it sends an email as expected) but not in the site, and particularly that i get a page refresh and not an error, leads me to believe the issue is in the js or html
The html for the form is:

Your message has been sent. Thank you!

          <form method="post" role="form" class="contactForm">

              <input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />

              <input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" />

              <input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />

              <textarea class="form-control" name="message" rows="5" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>

<button name="submit" id="submit" class="ready-btn right-btn" onclick="">Contact Us</button>

          </form>

The javascript at contactform/contactform.js is
$(document).ready(function() {

  $("#submit").click(function(e) {
  e.preventDefault();

  var name = $("#name").val(),
      email = $("#email").val(),
      subject = $("#subject").val(),
      message = $("#message").val();

  $.ajax({
      type: "POST",
      url: 'APIGATEWAY_URL',
      contentType: 'application/json',
      data: JSON.stringify({
          'name': name,
          'email': email,
          'subject': subject,
          'message': message
      }),
      success: function(res){
          $('#form-response').text('Thank you for your message, we will respond soon..');
      },
      error: function(){
          $('#form-response').text('Error sending message.');
      }
  });
   })
    });

Is anyone able to shed light on why this is just resulting in a page refresh and not doing anything?
Comment: You have a e.preventDefault. Is this expected?
Answer: In your <form>'s onclick attribute do this:
onlclick="return false;"
You should also add a console.log() or alert() call in your button's event handler just to make sure the function is actually called. If it isn't, check if you have included the javascript file correctly.
Comment: Have you checked the network tab in your browser? Is there any traffic? Is it hitting the server? Is there a response?
Comment: Thanks. So I get the alert, which suggests the javascript is being done correctly, but nothing else. the email still doesn't send
Comment: i didn't think of that. it is hitting the url but giving a 403 error
Comment: it seems you need to send authentication params
Comment: the API says it doens't need a key?


How to compare -50% and +100%

Question: This might seem (and be) a really trivial question!
I have this table:
id until_2000 after_2000
1  1          1          1
2  2          1          2
3  3          2          1

Now I want to compare the relative change between the columns after_2000 and until_2000. Just dividing the after_2000 values by the until_2000 (and substract 1?!) values yields the colum rel_change
  id until_2000 after_2000 rel_change
1  1          1          1        0.0
2  2          1          2        1.0
3  3          2          1       -0.5

While the first row stays as is (has a change of 0%), the second row doubles (has a relative change of 100 %). Yet the third row is half the value and thus has a relative value of -50 %.
Now my trivial question is how to correctly compute and average these relative changes. In my line of thinking the values of 0, doubling, and "halfing" should result in an average change of 0.
Yet adding 0 + 1 - 0.5 and dividing it by three yields a value of 0.16666.
What am I conceptually understanding wrong here?
Answer: The problem is twofold:
You shouldn't subtract one. Just divide one by the other, and you'll get relative numbers 1, 2, 0.5 -- equivalent to 100 % (no change), 200 % (double) and 50 % (half.)
Then to get the average relative change, you take the geometric mean: multiply the relative changes and take the root. $\sqrt[3]{1 × 2 × 0.5} = 1$, leaving the value unchanged after all three steps.
Comment: https://en.wikipedia.org/wiki/Geometric_mean
Comment: @Lenn obviously be careful with dividing if it's by 0. I'm presume the "after_2000" could be 0.
Comment: oh great thanks so much! Why is it that I need to multiply these values to get the average ?
Comment: There is a big question on how would you want a change from 100 to 200 to modify the result, If it's also a +100% then just count as 2 or if it should have more weight because the untill_2000 is bigger. I added a answer on this point of view
Comment: But 3.5 divided by 3 still does not give 1, how do we resolve that point? What is the way to average these numbers?
Answer: As good as is kqr answer, I would go for another method, which would work even if you have a 0 value somewhere and would keep his way of calculating the relative change
When you ask the question "how to correctly compute and average these relative changes" we could also first do the average then compute the relative change. You would then have
id    until_2000 after_2000 rel_change
total       4          4        0

Then you can calculate the difference as you were calculating before
The average and the sum of the different rows lead to the same result here as your denominator is the same for both (3 rows)
$\frac{sumAfter/nRows - sumUntil/nRows}{sumUntil/nRows}  = \frac{sumAfter - sumUntil}{sumUntil}$
Answer: Adding the numbers and then dividing by the count is the arithmetic mean. When dealing with relative change, it often makes more sense to use the geometric mean, which is where you take the product of the numbers and then taking the nth root. So if you take 0.512 and then take the third root, you get 1, which makes more sense as the "average" change. If you had 0.5, 1, 2, and 2, then you'd take the fourth root of 2, which is 1.189
You can also take the logs of the ratio. You can then take the arithmetic mean of the logs. If you want to convert back to relative change, you can then take the exponential of the result. Taking the log, then the arithmetic mean, then taking the exponential of the result gives you the same answer as the geometric mean.
Answer: Alternatively you can use log returns (there are issues with division by 0, same as multiplication by 0 with a geometric mean).
> df=structure(list(id = 1:3, until_2000 = c(1L, 1L, 2L), after_2000 = c(1L,
2L, 1L)), class = "data.frame", row.names = c("1", "2", "3"))

> log(df$after_2000/df$until_2000)
[1]  0.0000000  0.6931472 -0.6931472

and you can use a regular average now (which is 0 in this case).
Adding another row
> df=rbind(df,c(4,3,5))
> log(df$after_2000/df$until_2000)
[1]  0.0000000  0.6931472 -0.6931472  0.5108256

the regular average of these 4 log values is $0.1277064$, an overall $\approx 12.7 \%$ increase.
Compare this to kqr's method:
Edit after the correct formula for the geometric mean was written down
> prod(df$after_2000/df$until_2000)^(1/nrow(df))
[1] 1.136219

so an average $13.6 \%$ increase.
Comment: I'm pretty sure you're misunderstanding the meaning of this calculation. I don't quite follow it, but averaging the logs of _anything_ won't give you a percentage. Perhaps you could explain what you're doing in your answer?
Comment: @wizzwizz4 Log returns can be interpreted as percentage differences (assuming small enough differences), there is a lot going on here, see [What does the average log-return value of a stock mean?](https://money.stackexchange.com/questions/24382/what-does-the-average-log-return-value-of-a-stock-mean) and [Why stock prices are lognormal but stock returns are normal](https://stats.stackexchange.com/questions/125761/why-stock-prices-are-lognormal-but-stock-returns-are-normal).
Comment: For _small_ values, yes… but you have access to a computer! This kind of approximation is silly. 12.7% and 13.6% are quite far apart.
Comment: @wizzwizz4 It's not an approximation, it approximates percentage changes under certain conditions, however it still measures something different, so it's understandable that it is different. Whether this makes sense is context dependent.
Comment: I see! It might be worth explaining that in your answer, for stats noobs like me.

How to compare -50% and +100%


How do I add a template suggestion for a node based on the current view?

Question: I have a view that lists a bunch of nodes of a certain content type. I want to style this list of nodes in a way that I can't do easily with the standard node.tpl.php that they're being rendered with. I'm trying to override my themename_preprocess_node function to add a template suggestion depending on what view the nodes were called from. I tried this:
<?php
  function themename_preprocess_node(&$variables)
  {
    // Add theming based on view name. If the view is named
    // 'featurefront' one of the template suggestions will be
    // 'node-view--featurefront.tpl.php'.
    if (isset($variables['view']))
    {
       $variables['theme_hook_suggestions'][] = 'node_view__'.
            str_replace('_', '--', $variables['view']->name);
    }
  }
?>

but it doesn't work because $variables['view'] isn't ever set anywhere. Basically my question comes down to this:
What variable can theme_preprocess_node use that will tell it the name of the current view or whether it was called from a view at all?
Answer: You can leverage the views_get_current_view() function to get the current view being rendered.  I haven't tested the code below, but it should get you close to what you are trying to do.  Note that it could have unintended consequences if nodes are being rendered somewhere else on the page.
function themename_preprocess_node(&$variables)  {
  if (($view = views_get_current_view()) && 'featurefront' == $view->name) {
    $variables['theme_hook_suggestions'][] = 'node_view__' . $view->name;
  }
}

Views doesn't set template variables for nodes, which is why the technique above has to be leveraged.


fit GLM for weibull family

Question: I am trying to fit generalized linear model for weibull family, but when I try it in R, it gives an error. I know that weibull does not fit in exponential family, but I have read some research articles about fitting GLM for weibull family. If anyone can help me with this, I really appreciate.
It gives the following error.
> data(lung)
> glm(time ~ age+sex+ph.ecog+ wt.loss, family = weibull(link='log'), data = lung)
Error in glm(time ~ age + sex + ph.ecog + wt.loss, family = weibull(link = "log"),  :
  could not find function "weibull"

Answer: Sorry i'm quite late with this.... but might help someone i believe :
gamlss package is what you should be looking for. It supports almost all the distributions( not just exponential family ones). It gives amazing flexibility on almost all the parameters of a distribution.
Comment: +1. `gamlss` supports the Weibull distribution via `WEI`, `WEI2` and `WEI3`, all 2-parameter though obviously different. Not sure if it supports censoring, though, which would be a key element of an AFT survival model.
Comment: @Wayne gamlss definitely supports right censoring and left truncation also ....both also if wanted
Answer: The glm() function does not support the Weibull distribution in R unfortunately. You can try ?family to see which distributions are available. I would try using survreg() from the survival package instead.
Comment: yeah glm() do not support weibull. I am trying to use glm approach and AFT model using survreg, then compare the results of two methods. That is why I tried to figure it out to fit glm for weibull. Thank you for your comments. I appreciate it.
Answer: I have used the brms package, which is Bayesian. It supports the Weibull, exponential, lognormal, Frechet, and other families and (left/right/interval) censoring so implements AFT models. It also includes random effects which are known in survival models as "frailty", and a host of other regression options like gam-style smoothers.
Since Bayesian approaches use MCMC sampling, it's slower than glm, gamlss, or survreg, but it's also a comprehensive regression solution, and being Bayesian has other advantages. (I love it's stanplot, which provides a host of illuminating diagnostic plots.)

fit GLM for weibull family


Fetching node field values programmatically

Question: I have figured out how to query the node table programmatically without using the deprecated db_query() function. However, I soon discovered that the actual content fields are not held in the node table, but in a separate revisions table.
I can't figure out how to select fields from the content nodes.
So for example, I can get a count of the number of nodes of type "entity", by using the following code.
use Drupal\Core\Database\Database;
$connection = Database::getConnection();

$sth = $connection->select('node', 'x')
->fields('x', array('nid', 'type'));

$executed = $sth->execute();
$results = $executed->fetchAll(\PDO::FETCH_OBJ);

$type_entities = array();

foreach ($results as $row) {

    // COUNT ONLY ENTITIES
    if ( $row->type == "entity" ) {
        $type_entities[] = $row->nid;
    }
}
echo "Total: ".count( $type_entities );

This code works fine, because I am not referencing any data from the node, just counting the node id's.
But say I also have a field entitled "color", and I want to return that field too, so I can count the number or red, green, blue, etc? The following code won't work, because the "color" field is not in the node table, so can someone please help me to create a working version of the following:
use Drupal\Core\Database\Database;
$connection = Database::getConnection();

$sth = $connection->select('node', 'x')
->fields('x', array('nid', ['type', 'color'] ));  // <-- ADDED color FIELD

$executed = $sth->execute();
$results = $executed->fetchAll(\PDO::FETCH_OBJ);

$type_entities = array();
$is_red = array();
$is_greed = array();
$is_blue = array();

foreach ($results as $row) {

    // COUNT ONLY ENTITIES
    if ( $row->type == "entity" ) {
        $type_entities[] = $row->nid;

        // COUNT COLOURS
        if ( $row->color == "red" ) {
            $is_red[] = $row->nid;
        } else if ( $row->color == "green" ) {
            $is_green[] = $row->nid;
        } else if ( $row->color == "blue" ) {
            $is_blue[] = $row->nid;
        }
    }
}
echo "Total: ".count( $type_entities );
echo "Red: ".count( $is_red );
echo "Green: ".count( $is_green );
echo "Blue: ".count( $is_blue );

So how do I do that, only fetching the "color" field properly using ->fetchField() or ->fetchCol() maybe?
Thanks for looking!
Answer: I think you should leave the database structure and focus on the entities concept.
This brings you to a simpler approach using entityTypeManager  to get nodes and filter them by whatever you want.
$query = $this->entityTypeManager()->getStorage('node')->getQuery();
$nids = $query->loadByProperties(
    [
        'type' => 'content_type',
        'status' => 1,
        'field_name' => 'value'
    ])
    ->execute();

Comment: Brilliant, thanks @nessunluogo this was just what I needed!


How to get the URL for the re sized image

Question: I have built a custom form in Drupal 7. The form has a file type field which allows the user to upload an image. I'm using the below code in the submit function to save and re size the image:
function adddeals_submit($form, &$form_state){
    global $user;

    $file = $form_state['storage']['file'];
    // We are done with the file, remove it from storage.
    unset($form_state['storage']['file']);
    // Make the storage of the file permanent
    $file->status = FILE_STATUS_PERMANENT;
    // Save file status.
    file_save($file);
    //get the url for the saved file
    $url = file_create_url($file->uri);
    echo $url;
    echo '
';

    $destination = 'public://ImageThumbnail';
    $img = image_load($file->uri);
    image_resize($img, 200, 200);
    $result = image_save($img,$destination);
    echo $result;

    exit;
  }

The image is re-sizing and saving correctly as the $result is printing 1. I need to know how to get the URL for the re sized image. Am I missing something?
Thanks for you help.
Comment: I think in this case, you can get the URL by reloading the $img object as this object is updated when saving the resized image
Answer: I wouldn't use your approach because as you can see by yourself now you have no reference to the resized image, what I would do is use image_style_path :
function adddeals_submit($form, &$form_state){
    global $user;

    $file = $form_state['storage']['file'];
    unset($form_state['storage']['file']);
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);
    $url = file_create_url($file->uri);
    // this
    $derivative_uri = image_style_path($your_style, $file->uri);
    $url = image_style_create_derivative(image_style_load('your_style'), $file->uri, $derivative_uri);
}

This creates a folder in your files folder with your style name and stores the image there, then wherever in your code you want to get that image you just reutilize this code:
$derivative_uri = image_style_path($your_style, $file->uri);
$url = image_style_create_derivative(image_style_load('your_style'), $file->uri, $derivative_uri);

Drupal checks if an image is there and gives you back the $url, if it's not there it creates the image applying your style.
P.S. you need to register your style in configuration and once the style folder is created check it's permission settings.


How to send a simple email using PHP code

Question: I would like to send an email to my gmail account using some simple PHP code. The code below works in terms of execution, however the problem is even thought is says "Message Sent" I am not receiving my email in my gmail account. Please advice
ini_set('SMTP',"smtp.gmail.com");
    $to ="firstname.lastname@example.com"; // this will be replaced with my actual email
    $from ="firstname.lastname@example.com"; // this will be replaced with senders email

    $message = $_GET['Message'];
    $subject = "This is a test";

    if(mail($to,$subject,$message,$from))
    {
        echo "Message Sent";
    }
    else
    {
         echo "Message Not Sent";
    }

Comment: To successfully connect to email accounts like GMAIL you have to sign in with a userid/password before they will play with you. Thats not possible using vanilla PHP. Have a look at [phpMailer](http://phpmailer.worxware.com/) as a great library that gets you over this issue
Answer: Steps to send a simple email

Go to google
Search for "PHP Mail"
Click the first result
Read, read, keep reading, wait, read it over, read on
Enjoy!

But seriously:
(Examples are taken from PHP.net)
Example 1
Sending a simple email
Using mail() to send a simple email:
<?php
// The message
$message = "Line 1\r\nLine 2\r\nLine 3";

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");

// Send
mail('email@example.com', 'My Subject', $message);
?>

Example 2
Sending mail with extra headers.
The addition of basic headers, telling the MUA the From and Reply-To addresses:
<?php
$to      = 'email@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: email@example.com' . "\r\n" .
    'Reply-To: firstname.lastname@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

Answer: Use his line of code:
$to ="firstname.lastname@example.com"; // this will be replaced with my actual email
$from ="email@example.com"; // this will be replaced with senders email

$headers = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
//$headers .= "CC: email@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message = $_GET['Message'];
$subject = "This is a test";

mail($to, $subject, $message, $headers);