Xfinium.Pdf.NET not working in Blazor WASM .NET5/6 project

Question: I'm developing a Blazor PWA project where the WASM client needs to generate PDFs. My employer had already purchased an Xfinium license, thus I have been using it in our project. I have just recently ported the project from .NET Core 3.1 to .NET5 and have begun encountering issues with the Xfinium library. First of all, I am using the Xfinium.Pdf.NET library, as it supposedly supports .NET5. Upon attempting to initialize any object from this library (i.e. PdfFlowDocument test = new();) I am immediately greeted with the following error: Assertion at /__w/1/s/src/mono/mono/mini/interp/transform.c:3592, condition <disabled>  not met, which causes my application to crash. To further pinpoint this issue, I created two separate Blazor PWA WASM projects, one with .NET5 and one with .NET6, installed the Xfinium.Pdf.NET library, and attempted to use it. Both resulted in the exact same error as above. I then recreated this on a separate machine as well.
I have reached out to Xfinium support in regards to this issue, and have yet to hear back. Wondering if anyone else has experience a problem similar to this one. For the time being I will simply keep the project on .NET Core 3.1 (the upgrade was to fix another issue where we couldn't read more than 2mb at a time from indexed db database).
Any help is much appreciated!
Answer: Upon receiving a helpful workaround from the Microsoft Forums and a response from the Xfinium support team, I can answer my own question.
The Xfinium support team was able to recreate my issue with the obfuscated NET version of their Xfinium.Pdf library. Upon testing the non-obfuscated assemblies of the same library, the problem was non-existant. Therefore, they believe the obfuscation tool is the source of the problem and have notified me that they have reached out to their obfuscation tool vendor. I will be alerted by their support team when the issue is fixed.
As for a workaround, a user named Bruce on the Microsoft Q&A Forums suggested I try the NetStandard version of the library in my NET5 project. This solved my problem. I then alerted the Xfinium support of this solution and they told me that the NetStandard assemblies for this library are in fact obfuscated using a separate tool.
Thus, for the time being, I will be going forward using the NetStandard version of the library. I will update this post when Xfinium support alerts me that a fix has been shipped.


Is there a relation eigenvectors and unitary operator.

Question: I am trying the understand the spectral theorem as given in wikipedia link:
https://en.wikipedia.org/wiki/Spectral_theorem
I understand that eigenvectors are vectors and unitary operator is a matrix. My question is: for a positive definite matrix $A=UDU^H$ is the set of eigenvectors $U$ is unitary? In the above equation $D$ is a diagonal matrix of eigenvalues. Can we form a general relation between set of eigenvectors of an operator to unitary operator?
Answer: For a normal matrix $A$ (in particular, for a positive definite one), there exist a unitary matrix $U$ and diagonal matrix $D$ such that
$A = U D U^H$.  The diagonal elements of $D$ are the eigenvalues, and the
columns of $U$ are eigenvectors of $A$ corresponding to those eigenvalues.
Answer: Look at the section on that page regarding "Normal Operators" (Normal operators include Self-Adjoint operators.  There it is stated that
"$A$ is normal if and only if there exists a unitary matrix $U$ such that
$$A=U D U^*$$
So the spectral theorem directly addresses your question in that it is saying: You can diagonalize with a Unitary matrix precisely when the matrix is Normal.
Regarding positive definite matrices.  If a matrix is positive-definite, then it is Self-Adjoint.  You can find a proof of that in this MSE thread.  So then, for a positive definite matrix, you can find a unitary matrix $U$ that diagonalizes it.
Proof Complex positive definite => self-adjoint


Can I subsample a large dataset at every MCMC iteration?

Question: Problem: I want to perform a Gibbs sampling to infer some posterior over a large dataset. Unfortunatelly, my model is not very simple and thus sampling is too slow. I would consider variational or parallel approaches, but before going that far...
Question: I would like to know whether I could randomly sample (with replacement) from my dataset at every Gibbs iteration, so that I have less instances to learn from at every step.
My intuition is that even if I change the samples, I would not be changing the probability density and therefore the Gibbs sample should not notice the trick. Am I right? Are there some references of people having done this?
Comment: This is an area of active research that I don't know well enough to accurately summarize for you.  See for example http://jmlr.org/proceedings/papers/v32/bardenet14.pdf and http://arxiv.org/pdf/1304.5299v4.pdf
Comment: I cannot answer your exact question with any authority (although my suspicion is that you would be just increasing the approximation error that comes with Monte Carlo), the sad truth is that this is just an unfortunate aspect of Bayesian MCMC analyses: they're computationally expensive. @conjectures comment is a great idea, but doesn't really get at the heart of the issue: it's too expensive to draw all of those samples for each individual. My recommendation is to write your own C code for the heavy work (Rcpp in R, Cython in Python, etc.) and also parallelize (when no branch dependencies).
Comment: As an aside: another idea would be to do multiple analyses on random subsamples of the big dataset. That way you can also cross-validate.
Comment: @conjectures This sounds like Michael Jordan's bag of little bootstraps.
Comment: I would suggest changing your sampler to avoid the latent variable augmentation altogether. You will no longer have a Gibbs sampler, but a Metropolis-Hastings algorithm with a proposal based on a normal approximation to the likelihood should work just fine. See Section 16.4 of the 2nd edition of Bayesian Data Analysis.
Comment: As this is still rumbling: no you can't (justify this with current theory). An MCMC iteration requires sampling all variables and using the full-data likelihood. AFAIK there is no theory permitting the use of unbiased minibatch estimators of the likelihood in MCMC (as is done in VB). This does idea make some sense to me (sauce for the goose), but may be an open research topic. I would be interested in a reference if such theory has been published.
Comment: Thanks guys!. Parallelizing seems an overkilling here, and I think the scipy.stats module in python is quite optimized already, so Cython might not improve things. I will explore the other two options (in case of Metropolis I guess I can try a Hamiltonian MC). Still, I'm curious about the resampling at every iteration.
Comment: It seems to me that different resamples of the data might yield different characteristic sets for the parameter values.  Thus, in the proposed method the majority of MCMC samples might look like 'burn in' between the resampled data, rather than unbiased samples from the posterior distribution given the resampled data.  I have no idea whether this is guaranteed to ultimately sample from the desired overall distribution (across all resamples) or not. If this problem is real (and I'm not sure that it is), @conjectures suggestion avoids it.
Answer: About the subsampling strategies: just for example consider  to have two observations $X_1 \sim N(\mu_1, \sigma_1^2)$ and $X_2 \sim N(\mu_2,\sigma_2^2)$ and consider to put some priors on the mean and variance. Let $\theta = (\mu_1, \mu_2, \sigma_1^2, \sigma_2^2)$, the posterior we want to evaluate is
$$
f(\theta|X_1, X_2) \propto f(X_1|\theta)f(X_2 | \theta)f(\theta)
$$
COnsider now a binomial variable $\delta \sim B(0.5)$. If $\delta=0$  we chose $X_1$, if $\delta =1$ we chose $X_2$, the new posterior is$$
f(\theta, \delta|X_1, X_2) \propto f(X_1, X_2|\delta,\theta)f(\theta)f(\delta)
$$
where $f(X_1, X_2|\delta,\theta) = f(X_1|\theta)^{\delta} f(X_2|\theta)^{1-\delta}$ and $f(\delta) = 0.5$. Now if you want to sample $\delta$ with a Gibbs step you have to compute  $f(X_1|\theta)$ and $f(X_2|\theta)$ because $P(\delta=1)= \frac{f(X_1|\theta) }{f(X_1|\theta) +f(X_2|\theta) }$. If you otherwise use the Metropolis Hastings then you propose a new state $\delta^*$ and you have to compute only one between $f(X_1|\theta)$ and $f(X_2|\theta)$, the one associated with the proposed states but you have to compute one between $f(X_1|\theta)$ and $f(X_2|\theta)$ even for the last accepted state of $\delta$. Then I am not sure that the metropolis will give you some advantage. Moreover here we are considering a bivariate process, but with a multivariate process the sampling of the $\delta$s can be very complicated with the metropolis.


LilyPond: how do I place a hairpin under a slur?

Question: Here is the desired engraving (look at the last 4 notes of the left hand):

Here is what I tried:
sib4 la8 sol16 \< (la \! \clef bass
\once \override Hairpin.outside-staff-priority = #0
sib \> la fa \! sol)

outside-staff-priority had no effect.

What should I do?
Answer: Slurs are not placed as outside staff elements, but directly drawn into the score. But this means that outside staff elements like Hairpins will be placed ouside of the slur (which is part of the staff). The solution is rather simple: Tell Lilypond that your Hairpin (or rather its parent, the DynamicLineSpanner) is not an outside staff object. This is done by setting outside-staff-priority to ##f.
\language "italiano"

{
  sib4 la8 sol16 \< _(la \! \clef bass
  \once\override DynamicLineSpanner.outside-staff-priority = ##f
  sib \> la fa \! sol)
}

Alternatively you could make the Slur an outside staff element
\language "italiano"

{
  sib4 la8 sol16 \<
  \tweak outside-staff-priority #10
  _(la \! \clef bass
  \once\override DynamicLineSpanner.outside-staff-priority = #0
  sib \> la fa \! sol)
}

But this is usually not a good idea, as this will render the Slur shape optimization meaningless (after all the Slur is optimized for a very specific position).
Comment: For your alternative solution, I think you need to move the `\once\override Slur.outside-staff-priority = #10`, to before the `sol16 \< _(`.
Comment: @ElementsinSpace Yes. Replaced it with a tweak. The former does still work because it creates an unattached slur even, which is then applied to the previous note.
Comment: Thank you! Gosh, a solution, more often than not, is quite simple. I hope I will learn how to solve LilyPond puzzles.


Trying to access Cloud composer APIs

Question: I am using below code to modify Airflow dag status.
statusObject = {"state": "failed"}
AUTH_SCOPE = "https://www.googleapis.com/auth/cloud-platform"
CREDENTIALS, _ = google.auth.default(scopes=[AUTH_SCOPE])
authed_session = AuthorizedSession(CREDENTIALS)
headers = {'content-type': 'application/json', 'accept': 'application/json'}

url = "https://{composer-name}.composer.googleusercontent.com/api/v1/dags/" + dag_run.conf["dag_id"] + "/dagRuns/" + dag_run.conf["cancel_dag_id"]

response= authed_session.request("PATCH", url, json=statusObject,headers=headers)

I am getting 401 unauthorised because the service account I am using has access_type = 'online'.
How can I make this API request or set access_type='offline' programmatically?
Comment: You can set the `access_type` parameter by specifying `access_type` as a keyword argument when calling the `flow.authorization_url` method as: `authorization_url, state = flow.authorization_url( access_type='offline', include_granted_scopes='true')`. For more information you can refer to this [document](https://developers.google.com/identity/protocols/oauth2/web-server#python_7). Let me know if it's helpful or not?
Comment: Hi @Abhishek Singh, did the above comment help?
Comment: I figured out the issue was because service account was more than 64 character not  access_type = offline.
Answer: @Abhishek Singh, As you have mentioned in the comment, issue can be resolved by limiting the character size of service account ID.
The ID must be between 6 and 30 characters, and can contain lowercase alphanumeric characters and dashes. After you create a service account, you cannot change its name. The service account's name appears in the email address that is provisioned during creation, in the format SA_NAME @ PROJECT_ID.
For more information this document can be referred.
Posting the answer as community wiki for the benefit of the community that might encounter this use case in the future.
Feel free to edit this answer for additional information.

Trying to access Cloud composer APIs


One second delay on EasyPHP - IPV4 vs IPV6

Question: Some months ago I got this computer and while running EasyPHP I perceived a 1 second delay to page load that didn't happen on my previous PC. I searched for some help and resolved it. The problem is that I've formated the PC now and I don't remember how I did that.
I kinda remember it was about changing something about ServerName in httpd.conf file, but I don't know what. I also found this answer: https://stackoverflow.com/a/15436435/4501021. Ok, I think that is the problem, but I have no idea on how to solve. The answer talk about some issues but WHERE do I need to change it? I'm newbie, I know nothing about it, so, if a good man come and tell me exactly what files I need to change that would be lovely. Thanks!
Answer: I've earned the tumbleweed badge from this question o/
I HAVE THE ANSWEAR GUYS
Open the following folder
C:\Windows\System32\drivers\etc
Now, open the notepad as admin and load the file "hosts" and remove the comment from this line:
<code>#   127.0.0.1       localhost
</code>
That's all folks, at least I think it is. Be happy.


Problemas para fazer o QuickSort em um vetor de structs

Question: Preciso pegar as palavras de um arquivo .txt e armazenar a palavra e a quantidade de vezes que ela aparece no arquivo em um vetor de structs. Cada palavra terá a sua posição do vetor.
O struct:
typedef struct
{
    char palavra[50];
    int ocorrencia;
} dicionario;

Depois de armazenar tudo no vetor, uma das funções que o programa deve executar é ordenar essas palavras em sentido crescente de ocorrência, por isso quero fazer o quickSort.
Minha tentativa de quickSort:
void quickSort(dicionario tabela[], int inicio, int tamanho)
{

    int i = inicio, j = tamanho;
    int temp;
    int pivot = tabela[(i + j) / 2].ocorrencia;

    while(i<=j)
    {
        while(tabela[i].ocorrencia < pivot) i++;
        while(tabela[j].ocorrencia > pivot) j--;
        if(i<=j)
        {
            temp = tabela[i].ocorrencia;
            tabela[i].ocorrencia = tabela[j].ocorrencia;
            tabela[j].ocorrencia = temp;
            i++;
            j++;
        }
    }

    if(inicio < j) quickSort(tabela,inicio,j);
    if(i < tamanho) quickSort(tabela,i,tamanho);

}

Quando executo essa parte, o programa simplesmente finaliza sem retornar nada (mesmo colocando printf embaixo nada acontece).
Tentei pesquisar na internet mas tudo que acho com relação a isso está em C++.
Comment: **Observação do autor da pergunta**: *...não estou acostumado a mandar perguntas aqui, se precisarem de mais informações, só pedir...*
Comment: Acho que seria melhor utilzar uma árvore binária balanceada.
Comment: poste um programa inteiro, compilável
Answer: Você vai ter uma lista de palavras assim
int 23
main 1
teste 12
outro 22

E espera classificar pela segunda coluna e acabar com algo assim
main 1
teste 12
outro 22
int 23

quickSort() não entende vetores de struct ou de qualquer outra coisa: apenas considera um vetor de ponteiros e uma função de comparação. Veja o quick sort disponível em stdlib:
void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))

E isso é muito prático porque classifica qualquer coisa sem saber o que é. Basta escrever uma função de comparação.
Sua estrutura
typedef struct
{
    char palavra[50];
    int ocorrencia;
} dicionario;

para usar
void quickSort(dicionario tabela[], int inicio, int tamanho)

Não está muito boa nem flexível. Compare com algo assim
typedef struct
{
    char* pal; // a palavra
    int   N; // quantas vezes aparece

}   Entrada;

Essa é uma entrada do seu tipo de dados.
E o dicionário?
typedef struct
{
    unsigned N; //quantos itens
    Entrada* item; // vetor de itens

}   Dict;

Exemplo em C usando qsort() e esses dados
Um programa que usa esses dados do exemplo e classifica
criando o dicionário
Dict* cria()
{
    Entrada const coisas[4] = {
        [0].pal = "teste", [0].N = 12,
        [1].pal = "int",   [1].N = 23,
        [2].pal = "outro", [2].N = 22,
        [3].pal = "main",  [3].N = 1};

    Dict* teste = (Dict*)malloc(sizeof(Dict));
    teste->N    = 4;
    teste->item =
        (Entrada*)malloc(teste->N * sizeof(Entrada));
    for (unsigned i = 0; i < teste->N; i += 1)
        teste->item[i] = coisas[i];
    return teste;
}

Essa função cria uma estrutura de testes como a que seu programa deve criar a partir do arquivo de entrada e retorna o endereço
A função de comparação
É uma função que compara duas entradas e retorna 1 se elas estão fora de ordem. Isso é o que falta em seu QuickSort
int compara(const void* v1, const void* v2)
{  // compara duas entradas do dicionario
    Entrada const* pal1 = v1;
    Entrada const* pal2 = v2;
    if (pal1->N < pal2->N) return -1;
    if (pal1->N > pal2->N) return +1;
    return 0;
}

main() para o teste
Este programa cria a tabela, mostra, classifica, mostra o resultado e apaga. Compare...
int main(void)
{
    Dict* teste = cria();
    mostra(teste, "tabela original\n\n");
    qsort(
        &(teste->item[0]), teste->N, sizeof(Entrada),
        compara);
    mostra(teste, "tabela classificada\n\n");
    // teste = apaga(teste);
    return 0;
}

saída do exemplo
tabela original

#N      Palavra
----    --------
12      teste
23      int
22      outro
1       main

tabela classificada

#N      Palavra
----    --------
1       main
12      teste
22      outro
23      int

O código completo
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    char*    pal;  // a palavra
    unsigned N;    // quantas vezes aparece

} Entrada;

typedef struct
{
    unsigned N;     // quantos itens
    Entrada* item;  // vetor de itens

} Dict;

int   compara(const void* v1, const void* v2);
Dict* apaga(Dict*);
Dict* cria();
void  mostra(Dict*, const char*);

int main(void)
{
    Dict* teste = cria();
    mostra(teste, "tabela original\n\n");
    qsort(
        &(teste->item[0]), teste->N, sizeof(Entrada),
        compara);
    mostra(teste, "tabela classificada\n\n");
    // teste = apaga(teste);
    return 0;
}

int compara(const void* v1, const void* v2)
{  // compara duas entradas do dicionario
    Entrada const* pal1 = v1;
    Entrada const* pal2 = v2;
    if (pal1->N < pal2->N) return -1;
    if (pal1->N > pal2->N) return +1;
    return 0;
}

Dict* apaga(Dict* D)
{
    free(D->item);
    free(D);
    return NULL;
}

Dict* cria()
{
    Entrada const coisas[4] = {
        [0].pal = "teste", [0].N = 12,
        [1].pal = "int",   [1].N = 23,
        [2].pal = "outro", [2].N = 22,
        [3].pal = "main",  [3].N = 1};

    Dict* teste = (Dict*)malloc(sizeof(Dict));
    teste->N    = 4;
    teste->item =
        (Entrada*)malloc(teste->N * sizeof(Entrada));
    for (unsigned i = 0; i < teste->N; i += 1)
        teste->item[i] = coisas[i];
    return teste;
}

void mostra(Dict* dic, const char* tit)
{
    if (tit != NULL) printf("%s", tit);
    if (dic->N > 0)
        printf("\n#N\tPalavra\n----    --------\n");
    else
        printf("\nSem entradas\n");
    for (unsigned i = 0; i < dic->N; i += 1)
        printf(
            "%d\t%s\n", dic->item[i].N, dic->item[i].pal);
    printf("\n");
}

Comment: É porque eu tinha um prazo para entregar esse projeto. Só recebi a sua resposta depois que o prazo tinha se esgotado. Por isso não utilizei exatamente o que o senhor publicou. Mas mesmo assim eu agradeço a ajuda.
Comment: Obrigado! Acabei utilizando a função qsort mesmo. Pesquisando em alguns sites consegui criar uma função de comparação, não sei se é eficiente, mas obtive o resultado esperado. Mais uma vez obrigado pela ajuda
Comment: porque não usou simplesmente a que te mostrei? podia pesquisar nesse site nesse post apenas :) e teria uma função


How do you tell if a user is logged in as root on Mac OS X?

Question: If I sudo /bin/bash, the who and w command does not show a root login on Mac OS X.  How do you enable this?
Answer: There is a log in the /var/log directory called auth.log where you can see if somebody su or sudo to root.
<code>Dec 23 00:32:43 hostname su: user to root on /dev/pts/3
</code>


Module does not exist error in Brackets.io extension

Question: I'm developing an extension for Brackets.io editor. I want to load trie-search, an npm module.
I cded to the local directory of the extension then run:
npm install trie-search

The extension code has to look like this:
define(function (require, exports, module) {
    "use strict";

    var AppInit           = brackets.getModule("utils/AppInit"),
        CodeHintManager     = brackets.getModule("editor/CodeHintManager"),
        LanguageManager     = brackets.getModule("language/LanguageManager" ),
        TrieSearch          = require('trie-search'), // throws an error
        MGfuncHint          = require('text!mg-func.txt');

This throws an error saying module does not exist. How could I load the module?
Answer: U may want to try the below.
var TrieSearch = new NodeDomain("simple", ExtensionUtils.getModulePath(module, "trie-search"));

Reference link:
https://github.com/adobe/brackets/wiki/Brackets-Node-Process:-Overview-for-Developers#usage-example
Hope it helps
Cheers


pyside QGraphicsScene: Why it is not working?

Question: I am new to Qt and PySyde. I trying to create a small app to visualize sime line drawings.
In order to do that I try to use QGraphicsView an QGraphicsScene. I made a test to learn how it is working but it isn't. I googled a lot around, I do not understan why it isn't working.
Can somebody explain me the reason and bring me the light?
My code (just want to put a line and a sample text on the scene):
#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PySide import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):

        hbox=QtGui.QHBoxLayout()
        leftpanel=QtGui.QFrame()
        leftpanel.setGeometry(0,0,300,400)
        scene=QtGui.QGraphicsScene()
        scene.addText("Hello, world!")
        view=QtGui.QGraphicsView(scene,leftpanel)
        view.setSceneRect(0,0,300,400)
        pen=QtGui.QPen(QtCore.Qt.black,2)
        scene.addLine(0,0,200,200,pen)
        hbox.addWidget(leftpanel)
        rightpanel=QtGui.QFrame()
        hbox.addWidget(rightpanel)
        szoveg=QtGui.QLabel(rightpanel)
        szoveg.setText(u"Hello World!")
        self.setLayout(hbox)
        self.resize(500,500)
        self.setWindowTitle('blabla')
        self.show()

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Answer: You need to save reference to scene somewhere, e.g. in Example instance:
def initUI(self):
    # ...
    scene = QtGui.QGraphicsScene()
    self.scene = scene  # save reference to scene, or it will be destroyed
    scene.addText("Hello, world!")
    # ...

In another function, you'll be able to add more items to the scene:
def anotherFunction(self):
    self.scene.addText("Another Hello, world!")

Comment: thanks for your answer, but the problem is that the scene does not displayed either. I think the graphicview does not connected to the scene, but it should be, because in this line view=QtGui.QGraphicsView(scene,leftpanel) I provide the constuctor the scene and the parent. Here the documentation says it is possible: http://www.pyside.org/docs/pyside/PySide/QtGui/QGraphicsScene.html#PySide.QtGui.QGraphicsScene
Comment: @sipiatti, Have you tried? It works for me when reference to scene is saved. full code with fix: http://pastebin.com/cBy2y8rq . Scene does not displayed because Python garbage collector deletes it when `initUI` returns.
Comment: @reclosedev. This looks like a bug (or unexpected behaviour) in PySide. Strictly speaking, it should not be necessary to save a reference in this particular case (although I'm not actually recommending that). The way the code is written, all the objects _should_ become connected together in a hierarchy via parenting (which is why it works with PyQt4). Alternative fix: `scene=QtGui.QGraphicsScene(leftpanel)`.
Comment: @reclosedev thanks for your effort, it works. On the other hand it should work with parenting so it could be a bug in PySide as ekhumoro told. If I create the GraphicsView first, and after I set its scene by the setScene method it works in PySide also. Anyway, I accept your answer as it solves the situation.
Comment: @sipiatti, yes it's very strange behaviour.


How do I use CkEditor layout on textfield input?

Question: I have a content type with a "Text (formated)" field. When I try to write something in it, all my HTML is ok. But I want to use the buttons of ckEditor to make it more "user friendly". I don't want to always have to write all my tags manually.
On "Text, long (formated)" field, there is no problem.
You can see what I mean in this screenshot.

I already tried to switch text format under my field. Nothing changed, except I got more tags allowed.
Comment: Are you sure that is a text formatted _long_ and not just text formatted?
Comment: That's not a long text and that's the problem. I don't want to use a long text field for just a 2 word's phrase. But I will use it if it's not possible to use ckeditor on in formatted textfield :) Thanks for your comment.
Answer: CKEditor only works on a textarea, you can't attach an instance to a textfield.
You could implement hook_form_alter and change it from a textfield, but it's hard to see what the point would be.
If you need long text, just use a long text field. It was designed for your use case, so will always work as expected.
Comment: I mark your reponse as the answer. Thanks for the information. I don't want to use a long text for just 2 words (it's a page title, like : "About this work" where work is bold for exemple.) But it looks that's not possible to do what I want simply. I will use a long text field so. Thanks :)
Comment: The "Text (formated)" field might be removed in the future:
https://www.drupal.org/project/drupal/issues/2737499


Python implied path works on XP but not windows 7

Question: I am learning to use Python and tkinter as a hobby.
I have two systems at different locations: one is XP (SP3) and the other is Win7 (SP1).
The code snippet for this question is as follows:
import tkinter
import sys
print (sys.path)
wnd = tkinter.Tk()
wnd.title("Sinewave's window")
wnd.geometry("250x100+10+30")

#this one works in both XP and Windows7
wnd.wm_iconbitmap('C:\Python34\icons\colours.ico')

#this one works in XP but not in windows 7
#wnd.wm_iconbitmap('./icons/colours.ico') #implied path

wnd.mainloop()

The shell window shows the path as:
['C:/Python34/py scripts', 'C:\\Python34\\py scripts', 'C:\\Python34\\Lib\\idlelib', 'C:\\Windows\\system32\\python34.zip', 'C:\\Python34\\DLLs', 'C:\\Python34\\lib', 'C:\\Python34', 'C:\\Python34\\lib\\site-packages']

The error when I try the "implied" path in Win7 is:
_tkinter.TclError: bitmap "./icons/colours.ico" not defined

I have tried many variations but cannot get the "implied" path to work in Win7.
Am I missing something, or does it just not work like in XP ?
(Of course, to try out this snippet you must create the folder "icons" and define an  appropriate icon).
Answer: For loading files, the relative path is not relative to sys.path , it should be relative to your current working directory.
sys.path is used when python imports modules , its not used for loading files or for tkinter.
For file openning and all only the current working directory is considered (unless you have given complete aboslute path).
So the issue must be occuring in Windows 7 , because there may not be a icons/colours.ico file relative to the directory from which you run the script. It may be present in case of Windows XP.
Comment: Thank you for your explanation. Actually I got caught up in my own (misplaced) enthusiasm, but you did point me in the right direction. Between the "working" XP version and the "not working" win7 version I had re-arranged a few folders. I moved the python scripts into their own folder below the "python34" folder. Now the "icons" folder and the "scripts" folder are on the same level below the "python34" folder. So the "fix" in my code snippet is: wnd.wm_iconbitmap('../icons/colours.ico') - note the extra dot before ./icons - it now works properly.
Comment: Also, `..` means to go to parent directory , for anyone who did not know.


how to get the stationary distribution of MCMC (Markov Chain Monte Carlo) model

Question: Based on my reading some course notes as well as the answers and comments to this SO post I started thinking about the general steps for creating a stationary distribution.
Assuming my problem were to model the chance of snow tomorrow based on weather conditions today I would need to be able to predict probabilites from each distribution:

use Monte Carlo methods to generate a larger dataset based on the parameters for each variable I had observed in my data. know that 50% chance of sun tomorrow if sunny today. know that 10% chance of snow tomorrow if raining today. know that 60% chance of snow if temperature is below freezing.
Feed that larger dataset into Use a convergence algorithm (like Metropolis-Hastings) to get a distribution for each variable. This distribution tells the likelihood of a particular change in that variable. (eg 10% chance of snow tomorrow if it is raining today for the snow distribution). or 50% chance of sun tomorrow if it is sunny today for the sun distribution.
??? Do magic???Create a single distribution that takes into account the likelihood of each variable (eg must be raining and cold to be snowing), so if raining today you have X percent chnace of it snowing tomorrow. This is the Probability Mass Function (PMF)
Get a Probability Mass Function (PMF) aka stationary distribution that tells you the probability of a specific outcome given your starting state.

My question is, how do I do Step 3, taking the probability of one distribution (eg sunny or rainy from the SO example) and add its effect to another distribution (say temperature) to predict days snowing. How do I know how much effect rain has on snowing? Do I assume by default that it must be raining to be snowing (if I didnt know this based on logic)?
Comment: I do not understand your step 1. It mixes dataset, data, parameters, and variable!
Comment: I do not understand your step 2. Again, there is a mix between simulated dataset, parameters as simulated by a Metropolis-Hastings algorithm, and variable. Since the Metropolis-Hastings algorithm aims at simulating a posterior, I do not understand how it relates to "the likelihood of a particular change in the variable".
Comment: @Xi'an, Hopefully I have made this clearer with a concrete example. How do I know how much effect rain has on snowing? Do I assume by default that it must be raining to be snowing?
Comment: I find this question very unclear; I'm quite familiar with the basics of MCMC but I really don't understand what you're after here.
Answer: Diagonalize the stochastic transition matrix and then pick the Eigen vector with the Eigen value 1. That is what your data would converge to.


Connection of FTP and read xml file using php

Question: I have connect FTP through PHP and try to read .xml file , but I am getting warning that file can't found.
$date =  date('d');

$path = dirname(__FILE__)."/data/".$date;

$ftp_server = "xyz.com";

$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");

$login = ftp_login($ftp_conn, 'uname', 'pwd');

$file_list = ftp_nlist($ftp_conn, ".");

$file_lists = ftp_nlist($ftp_conn,$file_list[2].'/'.date("Y/m/d"));

ftp_pasv($login, true);
ob_start();
ftp_get($ftp_conn,$path,$file_list[2].'/'.date("Y/m/d").'/'.$file_lists[0], FTP_ASCII);
$data = ob_get_contents();
ob_end_clean();
ftp_close($ftp_conn);
var_dump($data);

How can I read .xml file?
Comment: try to `var_dump($file_lists)`
Comment: If i am use it than getting list of file , but i want to read each file and get all data
Comment: 1. you don't need output buffering here 2. do you see your file in `$file_lists` array?
Comment: Yes , I can see list of file , so i used that `$file_lists[0]`
Answer: In fact ftp_get() is just downloading it but not reading contents. Change your path to read remote file to output:
ob_start();
$result = ftp_get($ftp_conn, "php://output", $file_list[2].'/'.date("Y/m/d").'/'.$file_lists[0], FTP_ASCII);
$data = ob_get_contents();
ob_end_clean();

or read local file:
ftp_get($ftp_conn, $path, $file_list[2].'/'.date("Y/m/d").'/'.$file_lists[0], FTP_ASCII);
$data = file_get_contents($path);


Generating multiple files of shuffled integers for a given range c++

Question: I am trying to generate multiple files (say 100) of shuffled integers for a particular range (for eg: 1000, 10000 etc). This is the code I have tried so far
<code>// random_shuffle example
#include <iostream>     // std::cout
#include <algorithm>    // std::random_shuffle
#include <vector>       // std::vector
#include <ctime>        // std::time
#include <cstdlib>      // std::rand, std::srand
#include <fstream>

// random generator function:
int myrandom (int i) { return std::rand()%i;}

int main(){
   std::srand ( unsigned ( std::time(0) ) );
   std::vector<int> myvector2;
// For creating 10 different shuffled files
   for(int j=0;j<10;j++){
// set some values:
      for (int i=1; i<=10000; ++i) myvector2.push_back(i);

// using built-in random generator:
      std::random_shuffle ( myvector2.begin(), myvector2.end() );

// using myrandom:
      std::random_shuffle ( myvector2.begin(), myvector2.end(), myrandom);

// print out content:
       std::cout << "Data Shuffled:";
// put content in out file
      std::ofstream f2("out.txt");
       for (std::vector<int>::iterator it=myvector2.begin(); it!=myvector2.end(); ++it)
       f2<< *it <<'\n';
     }
return 0;
}
</code>
The file 'out.txt' gets overwritten and I end up with just one file. How can I create multiple files which have different set of shuffled integers in each file?
Thank You
Comment: Does it matter that you are writing random numbers to the files? If not, you will need to reduce your code by that to get closer to the core of the problem. Also, please make the code complete, so that others can take and compile it. And format it, too, so others can read and understand it. As it stands, your question is off-topic.
Comment: @UlrichEckhardt . Can you help me understand why my question sounds off-topic?
Comment: @NivethaMahalakshmi You should read the help section on "[How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve)".
Answer: you are using the same file name each time std::ofstream f2("out.txt");
try to use different name in each time for example out1.text, out2.txt, ...
you can get this form j in the for loop
<code>std::ostringstream stringStream;
stringStream << "out"<<j<<".txt";
std::string fileName= stringStream.str();
</code>
Answer: The the sake of providing an educational example, this is probably how i'd approach it.
Comments are inline. This code uses some c++14 features.
Note that The entire operation is expressed in terms of standard algorithms. This is the best way to make the code portable, maintainable, correct and gives the compiler the best possible opportunities for optimisation.
<code>#include<random>
#include<fstream>
#include<vector>
#include<utility>
#include<iterator>
#include<algorithm>

// generate a vector of all integers in the set Is...
template<int...Is>
auto initial_vector(std::integer_sequence<int, Is...>)
{
    return std::vector<int>({ Is... });
}

// defering to template expansion function allows the compiler to populate the vector from a compile-time
// computed data table. Totally unnecessary, but I find it pleasing.
auto initial_vector()
{
    return initial_vector(std::make_integer_sequence<int, 10000>());
}

int main()
{
    auto eng = std::default_random_engine(std::random_device{}());

    // make our initial vector
    auto data = initial_vector();

    // for each data set...
    for (int i = 0 ; i < 100 ; ++i)
    {
        // shuffe the data...
        std::shuffle(std::begin(data), std::end(data), eng);

        // create a filename from th index of the data set
        auto strm = std::ofstream("data" + std::to_string(i) + ".txt");

        // copy the data-set to the file, encoded as newline-delimited strings representing decimal integers
        std::copy(std::begin(data), std::end(data), std::ostream_iterator<int>(strm, "\n"));
    }
}
</code>
Comment: This is really helpful. Thank You.


Project Euler – Exercise 1

Question: I am trying to find the sum of all numbers less than 1000 divisible by 3 and 5. I have this so far:
for i in range(0, 1000):
    if i % 3 == 0:
        print i
    elif i % 5 == 0:
        print i
b = sum(i)
print b

I get a TypeError: 'int' object is not iterable (referring to b = sum(i))
Comment: Good old Project Euler problem #1. It's much more beneficial if you work through them yourself. Also I wouldn't settle for the current answers if I were you - there's a neater way using arithmetic progression.
Comment: Is it `3 and 5`, or `3 or 5`?  You wrote 3 and 5 which is simply `divisible by 15` but your code implies `3 or 5`.
Comment: Try summing all numbers less than 1000, first. Get that right, then work on the additional constraints.
Comment: Printing i does not sum anything up.
Comment: Do you want all the numbers divisible by **either** 3 or 5, or just the numbers divisible by **both** 3 and 5?
Comment: The sum of all the multiples of 3 or 5 below 1000.
Answer: You could try something like this:
# initially we declare a variable that will hold the current sum
# and at the end of the for it will hold
# the sum of the numbers you have mentioned.
b = 0
for i in range(1000):
    # Then whether i is evenly divided by 3 or 5
    # we add this number to the current sum
    if i % 3 == 0 or i % 5 == 0:
        b += i

# last we print the sum
print b

Answer: use the following line
sum takes an iterable object as input. List, sets, etc. are iterable.
So try the following
b = sum(i for i in range(1000) if i%3 == 0 or i%5 == 0)

Comment: You do not need the brackets and it's faster without
Comment: @MaxNoe it faster with because internal anyway it converted to list: `In [17]: %timeit sum(i for i in range(0,1000) if i%3==0 or i%5==0)
1000 loops, best of 3: 230 us per loop`     `In [18]: %timeit sum([i for i in range(0,1000) if i%3==0 or i%5==0])
1000 loops, best of 3: 228 us per loop`
Answer: From your code I suppose you need the sum of the numbers which are divisible by 3 or 5. Among all the solutions, another possible one-liner solution is:
print(sum(filter(lambda x: x%3==0 or x%5==0, range(1000))))

Here you get this TypeError because sum function takes a sequence of numbers and returns sum of the sequence. In your code you are passing i as the parameter of sum which is, in you case, an int type object. So you are getting this error.

Project Euler – Exercise 1


How to display a block in twig (D8)

Question: I read the other similar questions, but it didn't work.
I created a block in a region and I want to display that same block on my custom page (inside the twig).
I'm using this code to load the block:
$block = \Drupal::entityTypeManager()->getStorage('block')->load('block_id');
$block_content =  \Drupal::entityTypeManager()->getViewBuilder('block')->view($block);

and I'm trying to build it like this:
$build['page'] = array(
      '#type' => 'inline_template',
      '#template' => $template,
      '#form' => $block_content,
);
return $build;

My twig file is like this:

{{ form }}

But nothing is displayed...
What am I doing wrong?
Thanks in advance.
Answer: To make the form variable available in the Twig template you need to supply a context like follows.
$build['page'] = [
  '#type' => 'inline_template',
  '#template' => $template,
  '#context' => ['form' => $block_content],
];

Generally, I would recommend using custom #theme hook instead of an inline template.
Another way to render block within a template is using Twig Tweak module.
{{ drupal_entity('block', 'block_id') }}

How to display a block in twig (D8)


How to evaluate results of linear regression

Question: I have a linear regression problem. In short, I have a dataset, I divided it into two subsets. One subset is used to find the linear regression (training subset), another is used to evaluate it (evaluation subset). My question is how to evaluate the result of this linear regression after applying it to the evaluation subset of data?
Here are the details:
In the training subset, I do linear regression: $y = ax + b$, where $y$ is groundtruth (also known as target), $x$ is an independent variable. Then I found $a$ and $b$. ($x$ and $y$ are given in the training subset).
Now, using $a$ and $b$ found above from the training subset, apply them to the evaluation subset, I found $y' = ax' + b$. In other words, these $y'$ are found from linear regression with $x'$. Now, in addition to $y'$, I also have $y$ from the evaluation set. How do I evaluate my result (how much $y'$ differ from $y$)? Any general mathematical model to do that? It needs to be some sort of math model/formula. I can think of different ways to do it, but they are all kinda ad-hoc or simple, but this is for a scientific work, so things that sound ad-hoc cannot be used here, unfortunately.
Any idea?
Comment: I don't think this kind of assessment is generally used with simple regression models. What would it tell you that you wouldn't find out from using the entire dataset to generate your regression parameters? Normally the reason to use an evaluation dataset is to prevent overfitting, but that's not an issue when you already know that your model is going to contain just one independent variable.
Comment: To be clear, @octern was talking about linear regression. Training-test split is perfectly good practice to do this with logistic regression. It could even make sense in linear regression if the test-set has different/non-stationary distribution than the training, or comes from a different time-period (e.g. advertising campaign, shopping behavior).
Answer: I'd agree with @Octern that one rarely sees people using train/test splits (or even things like cross-validation) for linear models. Overfitting is (almost) certainly not an issue with a very simple model like this one.
If you wanted to get a sense for your model's "quality", you may want to report confidence intervals (or their Bayesian equivalents) around your regression coefficients. There are several ways to do this. If you know/can assume that your errors are normally distributed, there's a simple formula (and most popular data analysis packages will give you these values).
Another popular alternative is to compute them through resampling (e.g., bootstrapping or jackknifing), which makes fewer assumptions about the distribution of errors. In either case, I'd use the complete data set for the computation.
Comment: You should also do residual analysis by plotting.
Answer: if you really are fine with your linear trainig model and want to know how well it would predict your test data, then all you would have to do is to use the linear model formula you already have and include the estimated coefficients a (= intercept) and b(regression coefficient, also called slope) resulting from first model.
should look like
y= a + b*X    here some imaginary numbers...   y= 2 + 0.5*X
Which software are you using? Are you using R ?  if so, you can use the predict.lm() function and apply it on your 2nd dataset.
Answer: While this largely depends on exactly what your goals are, a simple and standard way to do this would be measuring the mean squared error (MSE). So if you have your test dataset $\mathcal{D}$ which consist of input/output pairs, $\mathcal{D} = \{(x_1, y_1), (x_2, y_2), \ldots, (x_n, y_n)\}$ and your parameters $a$ and $b$, then the MSE can be calculated as
$$
\text{MSE}_{a,b} = \frac{1}{n}\sum_{i=1}^n (y_i - (ax_i + b))^2.
$$
This is probably a sensible way to measure your error also since this is likely the criteria you used for finding the parameters $a$ and $b$. If you want to get a better idea of how well your estimated parameters generalize, you should look into something like cross validation.


Storing images using canvas2image function

Question: I am using Canvas2Image() function to convert an canvas element to Image of jpg (or) png format. I want to know where will this image be stored and how can I access the converted image?
Answer: As I read the documentation the user will be prompted to save the image as they wish. Additionally you cache make the function return a html img element if that's what you want.
But you can't save the image to the HDD and then modify it.
Comment: Add that detail next time please :) I'll probably just take the dataurl and save to the database. And send it to the server either by an ajax call or posting it with a form :) and then let the server handle the insert. If you want to save the image as a blob you could use the server to decode the dataUrl to the actual bytes and save that :)
Comment: I want to store the Image into my Server side Database. Once the user draw using canvas and hit a Submit button , Canvas Element should be converted to image format and then I want to store that Image into my database for further use.


How to replace the "button" with "image" in the Javascript code

Question: How can I replace the "button" element with "image", so that, instead of clicking on the "button", I can execute the code by clicking on the "image".
<input id="hideObj" accesskey="o" type="button" style="background-color:midnightblue;color:white;font-size:18px;" class="btnHideCSSClass" value="Hide" />
<input id="showObj" accesskey="o" type="button" style="background-color:midnightblue;color:white;font-size:18px;" class="btnShowCSSClass" value="Show" />

Comment: Put an `` element inside an `` element - whatever click handler you're using with the buttons (which you should [edit] your question to show) should work fine with anchors too. (Don't use just an `` on its own, because keyboard-only users couldn't "click" that they way they could an `` element.)
Comment: Is the script written using id? Then you can simply replace this btn with  and use same id for image.
Answer: It would be something like this:

Comment: I changed the code to the following:
 but doesn't work for me
Comment: Try removing the semi colon in the src. In addition, press F12 in the browser, to go Network tab (assuming you are using chrome). Then F5 to reload the page. Check if there is any request 404.


Fourier transform of $f(x+h)$?

Question:
Show that $f(x+h)\to \hat{f}(w)e^{2\pi i h w}$

Let $g(y) = f(x+h)$, then $\hat{g}(w) = \int_{-\infty}^\infty g(y) e^{-2\pi i y w} dy = \int_{-\infty}^\infty f(x+h) e^{-2\pi i (x+h) w} dx$, then I simply have no idea how to continue?
Comment: Have you tried change of variables in the integral? also its not $x+h$ in the exponential
Answer: You're confusing yourself with $x$ and $y$. You want to find the transform of $g(x) = f(x+h)$, not $g(y) = f(x+h)$.
Putting $g(x) = f(x+h)$, We see \begin{align*} \hat g(x) = \int_\mathbb R g(x) e^{-2\pi i x w} dx = \int_{\mathbb R} f(x+h) e^{-2\pi i xw} dx &= \int_\mathbb R f(x) e^{-2\pi i(x-h)w} dx \\ &= e^{2\pi i h w}\int_{\mathbb R}f(x) e^{-2\pi i x w} dx \\ &= e^{2\pi i h w}  \hat f(w)\end{align*} where the last step of the first line, I made the transformation $x \mapsto x-h$.
Comment: Yeah you are right. I was confusing myself. I just realized that I don't even define new $g$. Something like $f(y) = f(x + h)$ would be good enough.

Fourier transform of $f(x+h)$?


load node_type_form form

Question: I need to load and print drupal 'internal' form on the page, which id is "node_type_form"
I have:
$form = drupal_get_form("node_type_form");
$html = drupal_render($form);
echo $html;
it only prints "Menu settings" and "Save and add fields" button.
I get:
Notice: Undefined index: node_type_form in drupal_retrieve_form()
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'node_type_form' not found or invalid function name in drupal_retrieve_form()
Notice: Trying to get property of non-object in menu_form_node_type_form_alter()
Notice: Undefined index: #node_type in menu_form_node_type_form_alter()
Answer: node_type_form() lives in modules/node/content_types.inc, which the system only includes when it needs to (to fulfil menu paths etc).
If you include it manually before your call to drupal_get_form() it should work...
module_load_include('inc', 'node', 'content_types');
$form = drupal_get_form("node_type_form");
// ...

Comment: I should have noticed that when I was looking at the API. worked great! thanks

load node_type_form form


Balalaika Fingering Standards

Question: I have noticed that many players construct chords on the balalaika using the thumb, however the neck is quite narrow and even a small hand can manage to extend fingers across the fret board.
As a new player, should I aim to construct chords with the thumb to 'future-proof' my playing for some really radical stuff I cannot yet imagine, or can I get away with a fingers only approach as I have learned for uke/guitar? ... or is it simply impractical and rude to violate the traditional stylings of any instrument?
Answer: You were absolutely correct in noticing that the thumb is used in fingering for playing the balalaika. Once you get over the initial hesitation in using your thumb, you will realize that it is a huge advantage for playing chords and solo passages. The thumb is typically used for the 2nd and 3rd (E) strings and the other fingers are used to play on the 1st (A) string. In printed music, you will often see a "T" or Russian "б" (big finger) above the note indicating that you should press down on the fret with your thumb. If you see it below the note, it means you should pluck the string with the thumb of your other hand.
I don't think anyone will be offended if you don't use your thumb but they will certainly think you don't know what you are doing. Your attitude should not be that you are playing with your thumb in order to fit into some dogmatic tradition but that you are using your thumb to a great advantage over other instruments. On a virtuoso level, players absolutely shred the instrument in a very impressive manner while using thumb pizzicato. You will never develop very far as a balalaika player unless you can get used to playing with your thumb.

Balalaika Fingering Standards


Android gridview item change

Question: I would like to change the GridView item background in onCreateView(), after setting the adapter. I tried this way:
final int numVisibleChildren = gv_categories.getAdapter().getCount();

    for ( int i = 0; i < numVisibleChildren; i++ ) {
        int positionOfView = i + 1;

        try {
            if (positionOfView == Integer.valueOf(m_transactionItem.getIcon())) {
                View view = gv_categories.getChildAt(i);
                int nColor = Color.parseColor(m_transactionItem.getcolor());
                if(view != null) {
                    view.setBackgroundColor(nColor);
                }
            }
        } catch(Exception e) {
            Log.e("GridviewItemErr", e.toString());
        }
    }

But view is always null. This is my custom adapter that contains an ImageView and the TextView:
public class CategoryAdapter extends BaseAdapter {
private Context mContext;
int id = 0;
private LayoutInflater inflater;
ViewHolder holder;

public static class ViewHolder {
    private TextView text;
    private ImageView icon;
    private int mId;

    public int getId() {
        return mId;
    }
    public void setId(int id) {
        mId = id;
    }

}

public CategoryAdapter(Context c) {
    mContext = c;
    inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    return mAllIcons.length;
}

public Object getItem(int position) {
    return 0;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        id++;
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.categories_grid_item, null);
        holder.icon = (ImageView) convertView.findViewById(R.id.ivGrid_Item_Category);
        //holder.icon.setId(id);
        holder.icon.setScaleType(ImageView.ScaleType.FIT_CENTER);
        holder.text = (TextView) convertView.findViewById(R.id.tvGrid_Item_Category);
        //holder.text.setId(id);
        holder.setId(id);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.icon.setImageResource(mAllIcons[position]);
    holder.text.setText(mCategNames[position]);

    convertView.setTag(holder);
    return convertView;
}

private Integer[] mAllIcons = {
        R.drawable.icon_1_prijevoz_neselktirane,
        R.drawable.icon_2_putovanje_neselktirane,
        R.drawable.icon_3_hoteli_neselektirane,
        R.drawable.icon_4_odrzavanje_neselktirane,
        R.drawable.icon_5_zabava_neselktirane,
        R.drawable.icon_6_kucne_potrepstine_neselektirane,
        R.drawable.icon_7_odjeca_neselektirane,
        R.drawable.icon_8_trgovina_neselektirane,
        R.drawable.icon_9_rezije_neselktirane,
        R.drawable.icon_10_restorani_neselktirane,
        R.drawable.icon_11_slobodno_vrijeme_neselktirane,
        R.drawable.icon_12_luksuz_neselktirane,
        R.drawable.icon_13_odmor_i_rekreacija_neselktirane,
        R.drawable.icon_14_ostalo_neselektirane,
        R.drawable.icon_15_prihod_neselektirane,
        R.drawable.icon_16_stalni_prihodi_neselektirane,
        R.drawable.icon_17_default_neselektirane
};

private String[] mCategNames = {
        "Prijevoz", "Putovanje",
        "Hoteli", "Održavanje",
        "Zabava", "Kućne potrepštine",
        "Odjeća", "Trgovina",
        "Režije", "Restorani",
        "Slobodno vrijeme", "Luksuz",
        "Odmor i rekreacija",
        "Ostalo", "Prihod",
        "Stalni prihod", "Default",
};
}

I tried many other ways to do this but I cant figure out how to get "visible" GridView items.
NOTE:
The background needs to be changed when fragment is created, not in onItemClickedListener or onScrollListener or like...
Answer: I think that your problem arises from the fact that you are trying to reference the grid's views before your ImageAdapter had created them, if you wish really to set the background color at the time of onCreateView() and not at the time of calling getView(), I would suggest that you use getViewTreeObserver().addOnGlobalLayoutListener , to make sure that the gridview had been properly inflated before calling getChildAt(i), your code would look something like this:
gv_categories.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        //At this point the layout is complete and the
        //dimensions of myView and any child views are known.
        final int numVisibleChildren = gv_categories.getAdapter().getCount();
        for ( int i = 0; i < numVisibleChildren; i++ ) {
            int positionOfView = i + 1;
            try {
                if (positionOfView == Integer.valueOf(m_transactionItem.getIcon())) {
                View view = gv_categories.getChildAt(i);
                int nColor = Color.parseColor(m_transactionItem.getcolor());
                    if(view != null) {
                        view.setBackgroundColor(nColor);
                    }
                }
            } catch(Exception e) {
            Log.e("GridviewItemErr", e.toString());
        }
    }
});

Answer: Base on my understanding, to work with 'Adapter', I don't think you need touch the view directly at creating time. Just change the dataset of the Adapter, and when system call
public View getView(int position, View convertView, ViewGroup parent)

You could just either create a new view or reuse a converted view.
Another scenario is that data changes at runtime, please make sure the updating happens in worker thread. And call :
public void notifyDataSetChanged()

to notify UI thread to change the UI correspondingly.
Comment: Sorry I wasnt clear enough, i need to change item background when fragment is first created...
Comment: ic. I think it should be doable. For me, I guess just need change the data which ties to the UI item in background thread, and call 'notifyDataSetChanged()', then system will call getView() again and you could update the color/text/... and any other attributes there. Hopefully, this is the answer you are seeking for.
Answer: You should change ListAdapter items from inside the getView() method, based on the position. Since the Views are recycled, don't forget to revert the changes (e.g. reset your background to default) if the position doesn't match the one you want to change.
AdapterViews such as GridView lazy-load their items dynamically from their Adapters based on the layout and scroll position, so they are not populated during the initial View population.

Android gridview item change


OAuth2 refresh token - how to update?

Question: I'm writing a program that automates some actions with my QuickBooks account (using Node js).But expires refresh token also, what can i did?I have this error message.
<code>Error: The provided authorization grant (e.g., authorization code, resource owner credentials) or refresh token is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.
</code>
Answer: You can refer the official node-oauth2.0 lib.
https://www.npmjs.com/package/intuit-oauth#refresh-access_token
You need to generate new accessToken using the latest refreshToken.
Ref docs - https://developer.intuit.com/app/developer/qbo/docs/develop/authentication-and-authorization/oauth-2.0

OAuth2 refresh token - how to update?


Media queries não está aplicando os css conforme width do dispositivo

Question: Estou tentando criar um layout responsivo. Alguns elementos ele está aplicando o css conforme citado no media queries, alguns outros está pegando css do web. Como posso concertar pra ele pegar o css correspondente do que eu quero? Porque isso ocorre?
Segue exemplo:
.scss:
<code>@media (min-device-width: 320px)
and (max-device-width: 564px){
.button-collapse{
    background-color: red;
    border: none;
    color: red;
    outline: none;
  }
}

.button-collapse{
  background-color: transparent;
  border: none;
  color: white;
  outline: none;
}
</code>
Neste caso, usando um dispositivo 320px width ele está pegando as propriedades fora do media querie, porque ocorre isso?
Segue imagem com mais detalhes.
Comment: Sua media query especifica um mínimo **e** um máximo. Significa que só será aplicável para dispositivos de largura que esteja entre os dois valores, não? Telas maiores que o mínimo de 320px e menores que 564px aplicam o CSS dentro da media query. Qualquer outra coisa (menores que 320 ou maiores que 564) fica de fora...
Comment: Sim, mas eu estou testando em um dispositivo de 320width... Ele deveria pegar a cor vermelha, mas está pegando a cor branca
Comment: ops, verdade, percebi logo depois que mandei o comentario mas nao deu tempo de deletar antes de você esclarecer, agora fica registrada a minha falta de leitura completa da pergunta, desculpa ae, hahah... a resposta do dvd resolve: media queries devem ser mutuamente excludentes e sempre depois das regras globais, pra não dar dor de cabeça ^^'
Answer: O @media rule deve vir após o estilo normal. Basta alterar a ordem:
<code>.button-collapse{
  background-color: transparent;
  border: none;
  color: white;
  outline: none;
}

@media (min-device-width: 320px) and (max-device-width: 564px){
.button-collapse{
    background-color: red;
    border: none;
    color: red;
    outline: none;
  }
}
</code>


Azure App Service Switch Database from Primary to Secondary and Vice Versa

Question: We have Azure App Service and Azure Sql Database, this database is updated on from on premise and we are thinking to have two databases to have transactional integrity and switch App Service to primary to secondary and vice versa whenever load is complete. How can we do it smoothly while using Azure App Service with minimal or no downtime?
Comment: https://learn.microsoft.com/en-us/azure/app-service/deploy-staging-slots
Comment: @xslguy no, there isn't a way can automate it.
Comment: Thanks , it looks like it will work , is there way to automate it using ADF pipeline once data load is complete ?  When I say automate it means swapping of the slot using ADF pipelines assuming once data load is complete using ADF pipeline and we want to swap the space .
Comment: Hi Leon , if I understand correctly we can not swap in automatic fashion and it has be done by someone manually once you get notification once your data load is complete .
Comment: @xslguy yes, you're right~
Answer: The Azure document Set up staging environments in Azure App Service provided by @CSharpRocks should solve your problem. For you another question, your understand is correct.
Just for now, we can't a way can automate these settings.
Comment: Thanks for confirmation , another question how do I configure my pipeline with two different sink , do you think having two pipeline will be better approach or store flag in the database and check , any idea about best practices in such scenario where ADF is moving data to two different sink alternatively . e.g first pipeline run goes in db1 and second pipeline goes in db2 if first pipeline run is successful , any thoughts ?
Comment: Hi @xslguy,  I see the new question. I will check it. By the way, If my answer is helpful for you, hope can accept it as answer. This can be beneficial to other community members. Thank you.


Por que não estoura a pilha?

Question: Com esse código, o livro me diz que isso não estoura a pilha, mas por que exatamente não estoura?
#include <stdio.h>

int main(){
   int *p;
   while(1){
      p = new int;
   }
}

Comment: Porque estouraria? Tem só uma variável na pilha.
Comment: mas o laço while fz o progrma fica em um loop eterno ?
Comment: Quando você usa `new`, você está usando alocação dinâmica propriamente dita. Normalmente a pilha fica com as coisas estáticas, não dinâmicas. Você teria um estouro de pilha se fizesse `int main() { main(); }`, pois aí estaria empilhando chamada da função `main` sobre `main`
Answer: A variável p foi declarada nessa função. Ela terá o tamanho de 4 ou 8 bytes dependendo da arquitetura. Essa variável é única em toda a função e ao que parece toda aplicação é este código. Por que estouraria a pilha com apenas uma variável de tão pouco tamanho?
A pilha tem tamanho fixo, não tem porque estourar. O estouro só ocorre quando ela vai sendo ocupada cada vez mais, o que não ocorre. Obviamente que se ninguém parar essa aplicação ela nunca terminará.
Em algum momento a aplicação ficará bem lenta ou terá problema com o heap porque a memória virtual tem limite e há limite de RAM.
Se a arquitetura for 32 bits quando rodar pouco mais de 1 bilhão de vezes, ocupará os 4GBB da memória total. Como nem tudo deve estar em RAM deverá ter alguma lentidão para jogar em armazenamento secundário. Se tiver um SSD de última geração nem sentirá tanto assim.
Se a arquitetura for 64 bits precisará mais de 4 quintilhões de interações para encher todo espaço, claro que precisará de um armazenamento secundário preparado para suportar tamanha memória virtual, precisaria centenas de HDDs com a capacidade atual. Acho que levaria alguns séculos para rodar isso com hardware atual.
Se tirar o new não há mais alocação em heap, então é tudo feito na pilha, sempre no mesmo lugar, já que sempre existe uma só variável, então aí nem o heap pode estourar. A pilha, em essência, deixando de lado pequenos detalhes, ocupará 4 bytes (pode ser diferente, mas na prática será 4 bytes).
O operador new é o responsável pela alocação no heap e ele está sendo executado diversas vezes alocando um inteiro em cada vez, o que nem é apenas 4 bytes, mas isso é outro assunto. Ele retorna sempre um ponteiro que é armazenado em p sobrepondo o valor anterior, ou seja, o objeto criado anteriormente fica sem nenhum ponteiro apontando para ele, fica inacessível. Como não é dado um delete no objeto, ele fica lá ocupando espaço sem necessidade.
Em uma linguagem de memória gerenciada isto não ocorreria e o coletor de lixo iria liberando memória.
Veja mais sobre a stack e o heap.
Coloquei no GitHub para referência futura.
Comment: Deixa eu ver se entendi, a pilha não está enchendo, mas o heap está? Conforme o heap enche, começa-se a usar o armazenamento do HD/SSD ao invés da RAM? Se sim, haveria "estouro de heap" em algum momento (pelo HD/SSD ficar cheio ou por algum "limite pré-definido" estourar)?
Comment: pq a heap esta enchendo se é apenas um ponteiro ?
Comment: Basta encher o que foi reservado pelo sistema operacional para memória virtual e haverá estouro de memória. O ponteiro só existe um de cada vez e fica na pilha, conforme está na resposta. O *heap* está enchendo porque a cada iteração do laço está mandando alocar um novo inteiro no *heap*, com o operador `new`. E nunca está mandando desalocar, como deveria em algum momento que ele não seja mais necessário. Cada alocação gera um ponteiro, só que ele está sendo jogado em cima do ponteiro anterior já que só há uma variável.
Comment: Inclusive todas essas alocações não podem ser acessadas pelo código já que o ponteiro para cada uma delas é perdido logo depois de outro ser criado.
Comment: entao a cada loop o p esta recebendo mais 4 bits de memoria
Comment: mas o operador new  é chamado varias vezes pelo q entendo ele aloca mais memoria .
Comment: Não, ele está alocando 4 bytes (a grosso modo) no *heap*. Isto porque o `new` está sendo chamado a cada vez. O `p` recebe 4 ou 8 bytes do ponteiro onde houve a alocação, mas está sobrepondo porque só existe uma variável. Agora vou encerrar aqui porque estou só repetindo o que já foi dito antes.
Comment: ok obg acho q eu estou confundindo  stack e  heap,
Comment: https://pt.stackoverflow.com/q/3797/101
Comment: OBRIGADAO  agr sim ficou claro ,eu estava confundindo o heap com stack
Comment: Blz, é uma pena que você preferiu a resposta que tem vários erros. Por ela está negativada enquanto a minha tem vários votos, mas se deseja aprender errado, só você perderá.
Answer: De forma simplificada podemos considerar que existem duas grandes áreas para alocação e manipulação de memória:
Stack(pilha)
Heap(monte).

A stack ou pilha é uma área bem menor e a heap ou monte é uma área de memória bem maior. A stack tem no windows um tamanho padrão de apenas 1MB(um megabyte)!
No entanto, essa região de memória de pilha é bem mais eficaz para localizar e manipular recursos se comparado a heap.
Ao declararmos variáveis locais  elas ficarão alocadas na Stack
Ponteiros também ficam na stack e chamadas de funções são empilhadas na stack
Assim se você rodar o código abaixo ocorrerá o erro stack overflow, pois haverá tentativa de alocar 3 milhoes e 300 mil inteiros da variável vetor que é local da função main().
#include <stdio.h>

int main()
{
    int vetor[3300000];
 //TENTANDO ALOCAR 3 MILHOES E 300 MIL INTEIROS
    printf("Esta linha não será exibida pois ocorrerá antes STACKOVERFLOW");

    return 0;
}

Bem, se considerarmos no windows que um inteiro ocupa 4B(quatro Bytes) temos então a tentativa de alocar
3.300.000 x 4B = 13.200.000 Bytes
1MB(um megabyte) equivale a 1.048.576 Bytes
13.200.000B /1.048.576B EQUIVALE A 12,58MB(doze virgula cinquenta e oito megabytes)
Muito maior que 1MB de espaço na pilha(stack)
Logo, VAI ESTOURAR!
Veja o que ocorre ao executar este código no Visual Studio Community 2015:

No seu código o que ocorre?
#include <stdio.h>

int main(){
   int *p; // aloca uma variável ponteiro na stack(pilha)
   while(1){ //loop infinito
      p = new int; // a variável p contida na pilha recebe o endereço de memória alocado para um inteiro através de new
   }
}

Observe que new aloca memória na Heap que é muito maior
portanto durante todo o programa o loop infinito irá:
Criar uma nova região de memória para alocar um inteiro(possivelmente de 4KB)
e o endereço desta região de memória será atribuída na variável ponteiro p que está na stack
Então observe que a variável ponteiro p não está sendo criada novamente!
O que está ocorrendo é apenas a mudança do endereço contido dentro dela.
Claro, 4 novos KB estão sendo alocados na Heap a cada loop, mas isso não será suficiente para ocupar toda a heap em pouco tempo. Se isso ocorrer ocorreria erro de tentativa de invasão de memória além da alocada inicialmente para o programa*(Veja este assunto de registrador base e limite na disciplina Sistemas Operacionais)*
Obs:
Os programas em linguagem C típicos usam  as funções malloc e free que aloca e desaloca na memória heap.
Em C++ existe new e delete que também  aloca e desaloca objetos na memória heap, mas com aprimoramentos de debug de gerenciamento de memória úteis para detectar possíveis vazamentos de memória.
(veja também ponteiros inteligentes std::unique_ptr e std::shared_ptr em c++?)
Comment: obg cara essa foi a melhor explicação, eu estou apreendendo  c++ com um livro ,eu venho do java  nunca  tive q lidar com gerenciamento de memoria ate agr ,mas essa explicação me tirou muitas duvidas  (P.S NOSSA O WINDOWS TEM SO 1 MEGA DE ESPAÇO ,POR ISSO Q OS PONTEIROS SAO  TAO UTEIS ) OBRIGADAO
Comment: Obrigado Dougllas, Veja que editei a coloquei agora o tamanho correto que um inteiro ocupa no windows que é de 4 ou 8 Bytes! Veja as novas contas! Abraço

Por que não estoura a pilha?


Qt 5.1.0 on Windows using minGW 4.8 taking a really long time to debug

Question: I have downloaded and installed the Qt 5.1.0 for Windows 32-bit (MinGW 4.8) from the qt-project downloads page. I have run the installer, and am able to compile and run applications using these libraries and the minGW 4.8 32-bit toolhchain.
However, I have a large application, and when I try to debug it (using the gdb bundled with the minGW toolchain), it takes an insane amount of time to start running, and any interaction with the application takes a long time to complete. Not an annoying amount of time, but an unusable amount of time. Has anyone else had this problem and are there any solutions?
In case this helps, I get lots of output when debugging like this:
Temporarily disabling breakpoints for unloaded shared library "C:\Qt\Qt5.1.0\5.1.0\mingw48_32\plugins\somefolder\somelib.dll"

Answer: There is a gdb bug that was introduced at some point between 7.4 and 7.5, which makes it much slower. When debugging QObject classes, the slower becomes awfully slow.
By disabling debugging helper, you improve it, but then you miss a lot of precious information in the Local Variables and Expressions. For instance, you cannot display nicely the contents of QLists, etc...
It seems that either:

buidling gdb from the CVS or
using an older gdb (7.4.1)

solves the issue.
Answer: Qt creator has "attempt quick start" in its gdb options. It helps a LOT.
Or you can switch to using MSVC compiler on Windows. That also switches your debugging to CDB instead of GDB and bypasses the problem entirely. You can just install MSVC compiler and plug it into QtCreator instead of mingw if you don't like MS IDE.
P.S. This also gives you readable core dumps which is a godsend.
Comment: Brought debug launch time from 90sec to 7sec for me. Wonder if the lunch comes for free.
Comment: The lunch does not come for free. Turns out all this option does is postpone library loading until app crash. So when the app crashes and you want to find out why, you still need to wait the extra 83sec. So the benefit from this option is marginal.
Comment: I definitely remember this helping a lot. Certainly nowhere near 80 seconds after this option enabled. Though I have since started using MSVC compiler and this doesnt have that problem. And Unix never had any GDB startup problems to begin with. BTW, msvc compiler comes with an ability to dissect core dumps on windows so you might want to switch too.
Comment: So when you build with a different compiler (MSVC) but keep using the same debugger (gdb), the debugger starts up quickly? That sounds great.
Comment: No, building with MSVC you simply use a different debugger (CDB)
Comment: Ah, and I guess this requires using the VS IDE as well, dropping Qt Creator?
Comment: No it doesn't. You can install free version of MS ide and plug its compiler into qt creator
Comment: Great, that worked. Thanks! I posted [a community wiki answer](http://stackoverflow.com/a/39254045/122687) that repeats what you said, so such useful knowledge doesn't stay buried in the comments.
Answer: See the comments on Zeks' answer. There he explains that switching from the MinGW toolchain to the Microsoft toolchain (compiler, debugger) solves the problem completely. Fortunately, Qt Creator supports the Microsoft toolchain so you don't need to switch IDEs.
After I did that, debugger launch time is now 4sec, and on app crash it there is zero delay. It also sped up builds a lot.
For reference, I've described how I've set up my system here.
Answer: I have managed to improve the debugging speed significantly after changing several settings:

Made sure the compiler is gcc.exe and not g++.exe in the Qt5.1.0\Tools\mingw48_32\bin folder
Unchecked Use Debugging Helper in the Tools->Options->Debugger->Locals & Expressions menu
Unchecked Stop when qWarning() is called and Stop when qFatal() is called
Comment: There's no difference between gcc and g++. It's still the same compiler. The first item on your list is a no-op.
Comment: For sure debugging helper improves speed, but that's a major feature and watching at Qt classes while debugging is nearly impossible without it.


Does linear regression assume all variables (predictors and response) to be multivariate normal?

Question: I stumbled on this really nice blog.
http://www.statisticssolutions.com/assumptions-of-linear-regression/
It has mentioned- "the linear regression analysis requires all variables to be multivariate normal".
I think the assumption of normal distribution is for the residuals. I understand that skewed data can distort significance tests and it is desirable to have normally distributed data. But can we say "normal distribution of variables" is one of the assumptions of linear regression?
I am confused with this. Could somebody please shed light on this?
Comment: If ordinary multiple linear regression is being discussed, the assertion is simply not true. The IVs are treated as fixed, not random. You'd have to ask the person saying it why they'd make such a claim. One need only do the derivations (which are not difficult) to understand what's going on. The error term is only assumed normal when doing the usual normal theory hypothesis tests and intervals.
Comment: You are much more nearly right than the source you cite. At most, it's helpful if error terms are normally distributed. The rest is unsound.  Here's one simple refutation: it would not be sound to use binary predictors if that were so, but every non-trivial account of regression explains how that's a useful technique.
Comment: Your second quotation "the normal distribution or any distribution" seems mangled: please check it and edit.
Comment: `the assumption of normal distribution is for the residuals` More strictly speaking, "is for errors" (there is a [subtle difference](http://en.wikipedia.org/wiki/Errors_and_residuals_in_statistics)) or "fully conditioned Y is normal".
Comment: @ttnphns, you don't need normality of residuals either. It's good to have it for finite samples, of course.
Comment: See also [here](http://stats.stackexchange.com/questions/94337), [here](http://stats.stackexchange.com/questions/86835), & [here](http://stats.stackexchange.com/questions/50206).
Answer: As a general assertation this is just plain wrong, I agree complexly with @Glen_b. For a review of the classical linear model assumptions see this
But essentially normality of the error term ensures, that the distribution of the $\hat{\beta}_k$ is exactly normal. Instead of just being approximated by the t distribution. Note the comment below, about the distribution when the standard error is estimated.
I have no idea why the webpage, would say, what is says. To say why normality is not needed, you could just derive the estimator your self (or look it up online). OLS is (must typically) derived by methods of moments - normality enters no where in those equations.
Perhaps it is a misunderstanding of the maximum likehood method, at least the OLS MLE assumes normality. However, it is not needed since the estimates are entirely robust to this assumption (quasi-MLE).
Comment: (+1) Note that if the error variance is estimated from the data (the typical case), the distribution of the standardized coefficient estimates follows a t-distribution (with degrees of freedom equal to the sample size minus the no. predictor terms) **exactly** when the error term is normal. See [here](http://stats.stackexchange.com/questions/117406).


Display random images from folder to JLabel in Netbeans

Question: My project contains a folder named images containing images.  I want to display the images randomly to the JLabel in the frame when a button pressed.  I tried the code below:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
 {
    Image im=new ImageIcon(this.getClass().getResource("/images/a1.jpg")).getImage();
    ImageIcon iconLogo = new ImageIcon(im);
    jLabel2.setIcon(iconLogo);
 }

This code displays only the image a1.  But I need the images randomly (one image at a time).
Comment: Check [`ImageViewer`](http://stackoverflow.com/a/13463684/418556) for tips on displaying multiple images.  1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556) (another way is shown in the `ImageViewer` code).
Answer: Use something like
..getResource("/images/a" + randomNumber + ".jpg")

Genderate a random number for randomNumber variable. As long as all your images have the same prefix and just different numerical suffix, you should be fine.

If they're all different, then store each string path into a String array and the random number will be the index
getResource("/images/" + pathArray[randomNumber])

Example
String[] imageNames {"hello.jpg", "world.png", "!.gif"};
Random rand = rand = new Random();
....
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
    int index = rand.nextInt(3);

    Image im=new ImageIcon(this.getClass()
                .getResource("/images/" + imageNames[index])).getImage();
    ImageIcon iconLogo = new ImageIcon(im);
    jLabel2.setIcon(iconLogo);
}

UPDATE to OP comment

"Oh!if the folder contains 100 pictures it seems very difficult.My project needs more images"

then load the filed names into a data structure via the File API.. file.list() <-- return a String[]
File file = new File("src/images");
String[] imageNames = file.list();
...
int index = rand.nextInt(imagNames.length);

As long as all the files are files and not directories, this should work fine.

UPDATE
As discussed below in the comments, its been noted that the above answer will probably not work at time of deployment. Here is @AndrewThompson's suggestion as a fix to the file problem

The best way I can think of is:

Create a small helper class that creates a list of the images.
Write that list to a File, one name per line.
Include the file as a resource (the easiest place is where the images are).
Use getResource(String) to gain an URL to it.
Read it back in at run-time.
Comment: if the folder contains different images like png,jpg,jpeg?
Comment: You put all the file names into a String array
Comment: Oh!if the folder contains 100 pictures it seems very difficult.My project needs more images.
Comment: @user3164246, then load the filed names into a data structure via the `File API`.. `file.list()`
Comment: @AndrewThompson how would _you_ load the file names, for the sake of deployment? I'm not too sure


Python: Selenium - Message: element not interactable

Question: Novice trying to figure things out. I've looked around for an answer and not found one.
While trying to interact with a webpage, I get this message from Python:

element not interactable

Here is my code:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://www.annemcgilvray.com")

search = driver.find_element_by_name("q")

search.send_keys("test")

I've tried waiting and implicitly waiting. I don't think it is in an iframe, though there are iframes on the page.
Any help would be appreciated!
Answer: When I tried your code, the problem was the the site did not adjust to the size of my browser window and the search field was out of view, and hence could not be interacted with, although selenium could locate it.
I tried using execute_script to scroll it into view, and after that, send_keys worked.
from selenium import webdriver

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://www.annemcgilvray.com")

search = wait.until(EC.visibility_of_element_located((By.NAME, "q")))

driver.execute_script(f"window.scrollBy({search.location['x']},0)")

search.send_keys("test")

Answer: You need to add wait / delay to let the element fully loaded before accessing it.
The best approach is to use explicit wait implemented by expected conditions, as following:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
wait = WebDriverWait(driver, 20)

driver.get("https://www.annemcgilvray.com")

search = wait.until(EC.visibility_of_element_located((By.NAME, "q")))

search.send_keys("test")

Comment: Thank you! This worked!
Comment: If so please accept my answer


I Want to see Bi-Modal Posterior in Bayesian Linear Regression!

Question: I'm playing around with a Metropolis-Hastings MCMC algorithm as described in this post.
I made an example data set with points taken from two lines shown below.

Both lines have a y-intercept of 0 and the normally distributed noise has standard deviation of 0.1 for both. One line has a slope of 1 and one line has a slope of 10.
I tried computing the posterior distribution for all three parameters; slope, intercept, and standard deviation of noise. I wanted to see that the slope parameter posterior had a bi-modal distribution, with one mode at 1 and the other at 10. However, it just looked like a normal distribution with a mean around 5.
What is going on? I expected the Bayesian analysis to show that there were two values for the slope that are equally likely. I don't really see how it is more useful than least squares regression otherwise.
Comment: We wrote a [paper](https://www.ceremade.dauphine.fr/~xian/jcgsrobert.pdf) on estimating mixtures of regression, published in [JCGS, 2003](http://www.jstor.org/stable/1391069), this could suggest you ways of simulating from the posterior.
Comment: You did not manage because your example concerns with mixture distribution (example here http://stats.stackexchange.com/questions/178321/my-mcmc-do-not-overlap-mixturemodel-with-jags-and-r/178328) and it needs more complicated tools. Also please tell us more about your approach to this problem since it is not clear from your question.
Answer: If you literally used the code from that post you linked to, then it's fairly clear what happened. You shouldn't expect that your posterior will be bimodal. It will be unimodal because of the likelihood which you chose. Doing the Bayesian analysis will essentially give you the same answer as running a linear regression using lm, and you can see that fitting a line through this set of points with least squares will yield the sort of result that you got!
The only way you could get a bimodel posterior is if you used a strongly bimodal prior with peaks around $1$ and $10$. Or alternatively, you should use a different likelihood (which I think is what you want to do.)


difference between np.linalg.lstsq and linear regression in scikit learn

Question: comb 1 is a  pandas data frame with following values.
yearID    teamID      salary          W
408             ANA          51464167      82
409            ARI         81027833        85
When I use np.linalg.lstsq I am able to print dfg data frame.
dfg = pd.DataFrame()

comb1 = combined[combined['yearID'] == 2000]
x1 = comb1['salary'].values /1000000
y1 =comb1['W'].values
A1 = np.array([x1, np.ones(len(x1))])
w1 = np.linalg.lstsq(A1.T,y1)[0]
yq = (w1[0]*x1+w1[1])
dfg['New val'] = y1 - yq

When I use scikit learn libary for the linear regression and do the same operation I am getting a value error
from sklearn.linear_model import LinearRegression
fg = pd.DataFrame()

x2 = comb1['salary'].values /1000000
y2 =comb1['W'].values

x2_reshape = x2.reshape(-1,1)
y2_reshape = y2.reshape(-1,1)

clf1 = LinearRegression()
clf1.fit(x2_reshape, y2_reshape)
predicted_train = clf1.predict(x2_reshape)

 x_pre = y2 - predicted_train
fg['New val'] = x_pre

What is the difference between these two ?? kindly help me!!
Comment: Please post the results of your code and the error.
Answer: They should be the same:
Notes

From the implementation point of view, this is just plain Ordinary Least Squares (scipy.linalg.lstsq) wrapped as a predictor object.

If you getting an error, it's probably because of they way you set up your data.


xcrun usdz_converter issues after Xcode 10.1 update

Question: Before updating Xcode today I was able to convert .obj files to usdz without any issue. I have tried searching the internet and even turning it off and back on again. Has anyone else ran across this issue before?
<code>$ xcrun usdz_converter ~/Desktop/obj_file/demo.obj ~/Desktop/obj_file/demo.usdz -v -a -l
objc[969]: Class MDLTSUZipReadChannel is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff87025898) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x1080001d0). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSUZipFileWriter is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff870255c8) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x108000220). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSULogCatThreadSafeMutableSet is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff87025758) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x108000270). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSUContainedZipArchive is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff87025a28) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x1080002c0). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSULogHelper is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff87025398) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x108000310). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSUIOUtils is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff87025640) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x108000388). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSUZipWriter is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff87025c08) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x1080003b0). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSUZipWriterEntry is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff87025c30) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x1080003d8). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSUBundleLookupClass is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff870257d0) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x1080004c8). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSUTemporaryDirectory is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff87025988) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x1080004f0). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSUFileIOChannel is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff87025438) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x108000540). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSUEncodedBlockInfoInternal is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff870257f8) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x108000590). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSUEncodedBlockInfoWithDecodedLengthInternal is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff87025848) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x1080005e0). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSUReadChannelInputStreamAdapter is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff870253e8) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x108000630). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSUZipFileArchive is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff870256b8) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x108000680). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSUZipFileDescriptorWrapper is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff870256e0) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x1080006a8). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSUZipArchive is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff870258e8) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x108000720). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSUZipEntry is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff87025a00) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x108000798). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSUAssertionHandler is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff87025550) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x108000838). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSUBufferedReadChannel is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff87025ca8) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x108000860). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSUBufferedReadChannelHelper is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff87025cd0) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x108000888). One of the two will be used. Which one is undefined.
objc[969]: Class MDLTSUZipInflateReadChannel is implemented in both /System/Library/Frameworks/ModelIO.framework/Versions/A/ModelIO (0x7fff87025668) and /Applications/Xcode.app/Contents/Developer/usr/bin/usdz_converter.dylib (0x108000900). One of the two will be used. Which one is undefined.

usdz_converter
Version: 1.008

-v: Verbose output
-a: Generate .usda intermediate file
-l: Leave generated .usda file
</code>
Comment: Is it failing? If it's working properly I would just submit a bug report to Apple.
Answer: I got the following response from Apple on their developer forums.

Yes, we're seeing this too.
  It appears(?) to be harmless, but if you encounter any issues beyond the warnings please let us know (bugreporter.apple.com).


Re-render in vegalite?

Question: I'm trying to use vega lite to create an in-page graph renderer.
Oddly, I can't find any documentation on how to re-render a graph after updating the spec.
Am I missing a reference somewhere?
Answer: When you change the spec, you need to compile again. You can just pass the new spec to Vega-Embed's embed method.

Re-render in vegalite?


One sample t-test equivalent for repeated measures design

Question: I might be looking for something fairly obvious but here is my setup:
I have one group of participants who will perform multiple (different) tasks in an experiment. For each one of the tasks, I want to compare the performance of the participants against a given, fixed value. I might be comparing the tasks against each other as well (then using a linear mixed model) but this does not help me for the comparison to the constant.
When looking at the design, I am tempted to perform multiple one-sample t-tests and correcting for alpha error inflation.
However, I have a feeling that I might be missing a better fitting approach.
Therefore I would like to find an answer to the following question:
Is there a validated approach to compare data from repeated measure designs against a (non-changing) constant value?
Answer: A couple of options:
You can still do this using a mixed effects model, it sounds like you would be fitting only the intercept as fixed (then include your random effects to account for the grouping).  The intercept is then the estimate of the overall mean.
To use this information to test the mean against a fixed value you can compute the confidence interval for the intercept/grand mean then see if the fixed null value is in the confidence interval.
Another approach is to subtract the fixed null value from all of the $y$ values (response values) then fit the mixed effects model on the adjusted response.  The test that the intercept in this model is different from 0 is the test that the intercept on the original scale is different from your null value.
If using R, you can include the fixed null value in an offset so that the tests and CI now give what you want.  There is probably similar functionality in other tools, I am just not familiar enough with them to point you the correct direction.
You could  fit a hierarchical Bayesian model (similar to the mixed effects model)  then not worry about formal testing but compare your fixed values of interest to the posterior distribution.
Comment: Thank you for your response. However, just for clarification: I understood your first two approaches in a way that they test the grand mean over all trials against the constant. However, for my design I would actually be interested in the performance in the separate trials/tasks (averaged over participants) against the constant. Did I misunderstand your approaches or did I phrase my question poorly?
Comment: @Dom42, did each subject to a given task multiple times?  if yes, then I would analyze each task by itself with the mixed effects model.  If each subject did each task only once, then you can analyze each task without worrying about random effects.  If you want to analyze all tasks at once (assume the error variance is the same) then you can do above, just compare the fixed null value for each task to the mean for that group, or subtract the fixed null from the response corresponding to that value.
Comment: Thanks once again. Each participant will only do a task once, therefore I'm actually not that worried about the random effects but rather about the alpha error inflation (as I would perform as many one-sample t-tests as tasks exist). I was just wondering, if there isn't a better approach for this design case.
Comment: @Dom42, if each subject only does each task once and your main concern is inflating type I error, then search for "Adjust for multiple comparisons" or "False Discovery Rate".
Comment: thanks for the advice! As mentioned in my inital post, I was aware, that this would be a possibility (that's what I meant with the remark on correcting for alpha error inflation). However, I was under the impression that a more appropriate method should/could be available, but apparently, this is not the case. Thank you!