How to add two fieldConditons in Entity field Query?

Question: Is this the correct way to add field Conditions
<code>$query->entityCondition('entity_type', 'profile2')
   ->entityCondition('bundle', 'user_profile')
   ->fieldCondition('field_town_city', 'value', "%".$search_title."%", 'LIKE')
   ->fieldCondition('field_land_state', 'value', "%".$search_title."%", 'LIKE');
</code>
Answer: You can chain multiple field conditions, however I'd probably try something like this (haven't tested it):
<code>->fieldCondition('field_town_city', 'value', $search_title, 'CONTAINS')
</code>
The EntityFieldQuery::fieldCondition() invokes the addFieldCondition() method, see the possible $op values on api.drupal.org:
https://api.drupal.org/api/drupal/includes!entity.inc/function/EntityFieldQuery%3A%3AaddFieldCondition/7
Comment: @Mukil What do you need example code for exactly? Your code is correct
Comment: can you give a example code?
Comment: when i execute this code `$query->entityCondition('entity_type', 'profile2')
       ->entityCondition('bundle', array('user_profile'), IN)
      ->fieldCondition('field_fl_name', 'value', $search_title, 'CONTAINS')
      ->fieldCondition('field_town_city', 'value', $search_title, 'CONTAINS');` it returns empty array.
Comment: how to add OR condition between fieldCondition...?
Comment: @Mukil As far as I know there's no `CONTAINS` in MySQL. OP might be thinking of T-SQL. Just use `LIKE` if your db engine doesn't support it. ORs are tricky with EFQ, see http://drupal.stackexchange.com/questions/14499/using-or-with-entityfieldquery


Sums of roots of unity with integer coefficients

Question: Let $w$ be a primitive $p^{th}$ root of unity, where $p$ is a prime and $n \geq 3$ is a positive integer. I am trying to determine if there exists integers $\alpha_{1}, \alpha_{2}, \ldots , \alpha_{n}$ such that $$ \big( \alpha_{1} \sum_{i \in J_{1}} w^{i} + \alpha_{2} \sum_{i \in J_{2}} w^{i}  + \ldots + \alpha_{n} \sum_{i \in J_{n}} w^{i} \big) \not\in\mathbb{Q} $$
for every $J_{1}, J_{2}, \ldots, J_{n} \subset \{1, 2, \ldots, p-1\}$ such that at least one of the $J_{i}$ is not equal to $\{1, 2, \ldots, p-1\}$.
My attempt
Assume for the contradiction  that there is a relation of the form $$ \alpha_{1} \sum_{i \in J_{1}} w^{i} + \alpha_{2} \sum_{i \in J_{2}} w^{i}  + \ldots + \alpha_{n} \sum_{i \in J_{n}} w^{i} = q \hspace{3mm} (*)$$ for some rational number $q$ and some non-zero integers $\alpha_{1}, \alpha_{2}, \ldots, \alpha_{n} \in\mathbb{Z}\setminus\{0\}$. We can rewrite this relation by collecting coefficients associated with powers $w, w^{2}, \ldots, w^{p-1}$ as
$$ w^{1} \sum_{i \in I_{1}} \alpha_{i} + w^{2} \sum_{i \in I_{2}} \alpha_{i} + \ldots + w^{p-1} \sum_{i \in I_{p-1}} \alpha_{i} = q  \hspace{3mm} (*) $$ for subsets $I_{1}, I_{2}, \ldots, I_{p-1} \subset \{1, 2, \ldots, n\}$ such that at least one of the $I_{\nu}$ is not equal to $\{1, 2, \ldots, n\}$. Since the minimal polynomial of $w$ is $1 + w + \ldots + w^{p-1}$, the relation $(*)$ implies that
$$ \sum_{i\in I_{1}} \alpha_{i} = \sum_{i\in I_{2}} \alpha_{i} = \ldots = \sum_{ i \in I_{p-1}} \alpha_{i}.  \hspace{3mm} (**) $$ We want to show that $(**)$ is not satisfied for some $(\alpha_{1}, \alpha_{2}, \ldots, \alpha_{n})\in\mathbb{Z}^{n}$. One way $(**)$ can hold is when $I_{1} = I_{2} = \ldots = I_{p-1} = I (say) \subset\{1, 2, \ldots, n\}$. Since $\alpha_{i}$ were non-zero, i.e., each $\alpha_{i}$ appears in some $I_{j}$, this implies $I = \{1, 2, \ldots, n\}$ and hence $J_{1} = J_{2} = \ldots = J_{n} = \{1,2,\ldots, p-1\}$, contradicting our initial assumption.
On the other hand, if $I_{r} \neq I_{s}$ for some $r, s\in\{1, 2, \ldots, p-1\}$, then consider the equality $$\sum_{i \in I_{r}} \alpha_{i} = \sum_{i \in I_{s}} \alpha_{i}$$ which gives rise to an equation of the form $$\sum_{m=1}^{n} \epsilon_{m} \alpha_{m}=0,$$ where $\epsilon_{m}$ are in $\{-1,0,1\}$; the solution set of which has dimension at most $(n-1)$ in $\mathbb{Z}^{n}$. Therefore, there are $(\alpha_{1}, \alpha_{2}, \ldots, \alpha_{n})$ not satisfying $(**)$ and hence not satisfying $(*)$.
Answer: For every such $(J_1,\ldots,J_n)$, let $V_{J}$ be the set of $\alpha \in \mathbb{Q}^n$ such that $\sum_{i=1}^n{\alpha_i\sum_{t \in J_i}{w^t}}\in \mathbb{Q}$. Then $V_{J}$ is a proper sev of $\mathbb{Q}^n$.
A large multiple of some $\alpha$ not in the reunion of the $V_J$ (there are finitely many of them so their reunion is not all of $\mathbb{Q}^n$) then works.
Comment: how do I know that $V_{J}$ is a proper subset of $\mathbb{Q}^{n}$? If $\sum_{t\in J_{i}} w^{t} \in\mathbb{Q}$ for every $i = 1, 2, \ldots, n$, then may be we can say that $V_{J}$ is a union of hyperplane in $\mathbb{Q}^{n}$. However, otherwise, I do not see.
Comment: @HumbleStudent: this is because $(1,w,\ldots,w^{p-2})$ is $\mathbb{Q}$-free.
Comment: In your notation, is $V_{J}$ then a zariski closed subset of $\mathbb{R}^{n}$?


How can I programmatically create a node?

Question: How can I programmatically create nodes with date and image fields?
I know I can do it in Drupal 7 with the following code.
global $user;

$node = new stdClass();
$node->title = "YOUR TITLE";
$node->type = "YOUR_NODE_TYPE";
node_object_prepare($node);
$node->language = LANGUAGE_NONE;
$node->uid = $user->uid;
$node->status = 1; //(1 or 0): published or not
$node->promote = 0; //(1 or 0): promoted to front page

$existing_filepath = "/home/nzcodarnoc/sites/default/files/imported/picture.jpg";
$new_filepath = "public://picture.jpg";
$drupal_file = file_save_data(file_get_contents($existing_filepath), $new_filepath);
$drupal_file->alt = $node->title;
$drupal_file->title = $node->title;
$node->field_my_file[$node->language][0] = get_object_vars($drupal_file);
$node->birth_date[LANGUAGE_NONE][0]['value'] = time();
$node = node_submit($node);
node_save($node);

What is the equivalent code for Drupal 8?
Answer: The following code will help you to save an image in a new node.
use \Drupal\node\Entity\Node;
use \Drupal\file\Entity\File;

// Create file object from remote URL.
$data = file_get_contents('https://www.drupal.org/files/druplicon.small_.png');
$file = file_save_data($data, 'public://druplicon.png', FILE_EXISTS_REPLACE);

// Create node object with attached file.
$node = Node::create([
  'type'        => 'article',
  'title'       => 'Druplicon test',
  'field_image' => [
    'target_id' => $file->id(),
    'alt' => 'Hello world',
    'title' => 'Goodbye world'
  ],
]);
$node->save();

Comment: The reality loop link no longer leads to the Drupal 8 info page.
Answer: I think that object oriented way is more convenient, isn't it?
use Drupal\node\Entity\Node;

$my_article = Node::create(['type' => 'article']);
$my_article->set('title', 'My article');
$my_article->set('field_text', 'My text');
$my_article->set('field_image', FID);
$my_article->set('field_user', UID);
$my_article->enforceIsNew();
$my_article->save();

Answer: In drupal 8 entities are objects and as such, to create an entity is to create an instance of the entity's type class.
If you know the class of the entity then you can either use the new keyword or the create function.
I.E. $foo = new Foo(); or $foo = Foo::create();
If you don't know the class of entity (only the machine name) then you can use the request the Storage class like so: \Drupal::entityTypeManager()->getStorage($entity_type_id)->create();
To populate the fields of an entity you can either use the $entity->set($key, $value) method on the entity object or pass a key=>value array to the entity constructor. As such:
$foo = new Foo([
  'name'=>'bar',
  'baz'=> TRUE,
  'multi_value' => [
    'first',
    'second',
    'third',
  ]
]);

To save an entity you only have to call the $entity->save() method on the entity object.
Since files in drupal 8 are also entities you need to either pass the id of the file entity, or the actual file entity, as the value.
$file_1 = File::load(1);
$foo->set('bar_files', [
  $file_1,
  2
]);

Here is a code for your specific scenario:
$node_entity_type = \Drupal::entityTypeManager()->getDefinition('node');
// The [file_save_upload][1] function returns an array of all the files that were saved.
$poster_images = file_save_upload($upload_name, $validators, $destination);
$node = new Node([
  $node_entity_type->getKey('bundle') => 'movie',
  $node_entity_type->getKey('label') => 'Foo',
  'field_release_date' => '1/1/2015',
  'field_poster_image' => $poster_images,
]);
$node->save();

Comment: Updated my answer
Comment: I want to add one image field and one date ffield,provide your answer with this field types.
Comment: But how to populate text fields programmatically in Drupal 8, at the time of node creation?
Comment: The data in $node_data is mapped directly to the node fields. If you want to add text to a field called field_body just add another entry with the key field_body.
Comment: I've updated my answer with more details. You are welcome.
Comment: "I.E. $foo = new Foo(); or $foo = new Foo();" How was this supposed to read?
Answer: If you want to do it in the most clean way (testable), use entity_type.manager service:
$storage = $this->entityTypeManager->getStorage($entity_type_id);
$my_entity = $storage->create([
   ....
]);

The problem with Node::create function, that it is static call, and that's why you cannot really unit test your class anymore. Avoid doing static calls whenever it's possible. It will make your code more readable (because dependencies will be clear).
Answer: Following code is working for me
use \Drupal\node\Entity\Node;
use \Drupal\file\Entity\File;

$node = entity_create('node', array(
'type' => 'article',
'title' => $form_state->getValue('title'),
'body' => array(
'value' => $form_state->getValue('body'),
'format' => 'basic_html',
),
'uid' => $uid,
));
$node->save();

Comment: entity_create is deprecated
Comment: Also, `$form_state` is available only in specific contexts; otherwise, you cannot access it.
Answer: Another way to create node with image is:
use \Drupal\file\Entity\File;

// Create file object from remote URL.
$data = file_get_contents('https://www.drupal.org/files/druplicon.small_.png');
$file = file_save_data($data, 'public://druplicon.png', FILE_EXISTS_REPLACE);

$node = \Drupal::entityTypeManager()->getStorage('node')->create(array(
  'type'        => 'article',
  'title'       => 'Druplicon test',
  'field_image' => [
    'target_id' => $file->id(),
    'alt' => 'Hello world',
    'title' => 'Goodbye world'
  ],
));
$node->save();

Answer: use Drupal\Core\Language\Language;

$definition = \Drupal::entityTypeManager()->getDefinition('node');
$values = [
    $definition->getKey('bundle') => 'basic_page',
    'langcode'                    => Language::LANGCODE_NOT_SPECIFIED,
    'title'                       => '...',
];
$entity = \Drupal::entityTypeManager()->getStorage('node')->create($values);


Diferença entre sintaxe de propriedades no C#

Question: Pelo que pesquisei, se eu fizer:
public int numero { get; set; }

é a mesma coisa que:
private int numero;

public int Numero {
    get{
        return numero;
    }

    set{
        numero = value;
    }
}

Mas no primeiro trecho de código tudo fica com acesso public, já no segundo o atributo é declarado como private e o get e set são public, isso não tem realmente diferença?
Comment: Nesse sentido não porque o que pode ser acedido de forma pública é exactamente o mesmo nos dois casos - a propriedade `Numero`
Comment: Pelo que explica no link abaixo não é atributo e sim características de um objeto.(Ou variável de classe ou de instância)
Recomendo a leitura: [Propriedade x Atributo](https://pt.stackoverflow.com/questions/75265/propriedade-x-atributo)
Comment: Tambem não entendo bem a diferença então tenho a mesma duvida
Comment: Não é a mesma coisa como em java ?

private String name;
public void setName(String name) {
this.name=name;
}
Answer: De acordo com a documentação, os trechos são (praticamente) equivalentes. No segundo, você define um campo da classe como privado e cria uma propriedade para gerenciá-lo.
public class Person
{

    // Define o campo `name`:
    private string name;

    // Define a propriedade `Name`:
    public string Name
    {
        get
        {
            return name;
        }

        set
        {
            name = value;
        }
    }

}

Ou seja, desta forma você está criando um campo name privado e uma propriedade Name pública que acessa o campo name. Mas se nenhuma lógica for necessária na implementação dos acessadores do campo, gete set, não há a necessidade de escrever todo esse código acima. A partir da versão 3.0 do C# é possível fazer apenas:
public class Person
{
    public string Name { get; set; };
}

Com esta sintaxe, o C# criará a propriedade Name definindo os métodos get e set assim como no exemplo acima, porém, também criará um campo privado e anônimo para suporte que será acessado por tais métodos. Vide documentação:

Ao declarar uma propriedade o compilador cria um campo de suporte privado e anônimo que pode ser acessado somente por meio dos acessadores get e set da propriedade.

Esta sintaxe é denominada propriedade autoimplementada. Uma leitura interessante sobre o assunto é:
Propriedade Vs Variáveis
Na verdade, esta é até duplicata desta pergunta.
Documentação
Usando propriedades (Guia de Programação em C#)
Propriedades autoimplementadas (Guia de Programação em C#)
Answer: O primeiro código é um açúcar sintático para o segundo, grosso modo. Então não há diferença entre eles, isto você sabe. Se é a mesma coisa, por que a dúvida? O primeiro tem tudo o que tem no segundo, você só não está vendo escrito ali.
Se escrever o primeiro é o mesmo que tivesse escrito o segundo, é óbvio que não tem diferença. Você talvez esteja querendo ver outra coisa que não existe aí.
Existe um campo privado no primeiro também, afinal onde ele vai guardar o dado? Precisa ser em uma variável da classe (estática ou de instância, no caso é de instância). Só porque você não digitou ou não vê não quer dizer que não esteja lá.
O código do get e do set são métodos que executam um código, por mais simples que eles sejam, mas não armazenam valores, só o campo pode armazenar um valor. Ambos são públicos.
Aliás, muita gente acha que uma linha de código pode ser mais rápida que várias linhas. Isto obviamente não é verdade, o que está por trás daquela linha é que define o que ela faz. O que você vê é sempre uma abstração de algo maior. Na verdade, nem mesmo uma linha de Assembly define oi quanto custa um processamento, cada instrução seu custo diferente.
Veja como fica o código gerado:
[DebuggerBrowsable(DebuggerBrowsableState.Never), CompilerGenerated]
private int <numero>k__BackingField; //o campo privado criado pelo copilador

private int _numero; //o cam criado manualamente

public int numero
{
    [CompilerGenerated]
    get
    {
        return this.<numero>k__BackingField; //o acesso criado pelo compilador
    }
    [CompilerGenerated]
    set
    {
        this.<numero>k__BackingField = value; //a mutação criada pelo compilador
    }
}

public int Numero
{
    get
    {
        return this._numero;
    }
    set
    {
        this._numero = value;
    }
}

Veja no SharpLab.
Comment: Pode, mas isso não é C# idiomático, tem desvantagens.
Comment: Não se pode fazer assim tambem? private int _numero;
public void setName(int _numero) {
this._numero=_numero;
}
Comment: ok então, entendi
Comment: Ia perguntar sobre a diferença de definir uma propriedade assim e manter o campo como público, mas pesquisei e acabei achando esta [pergunta](https://pt.stackoverflow.com/questions/133924/propriedade-vs-vari%c3%a1veis). No fim, acho que é até duplicata.
Answer: Concordo que é um açúcar sintático. Na prática (pra mim, ok?) eu uso o get;set; sem nada quando só preciso atribuir a propriedade "as is". Quando é necessário fazer algum tipo de tratamento (como formatar a saída, por exemplo) eu declaro os fields privados e os trato nos métodos get e set. Eu faço isso só para poder padronizar as interações com a propriedade sem ter de alterar um zilhão de classes prováveis.
Fora isso, não tem diferença de performance, etc. Vc só escreve um pouco mais.
Espero ter ajudado!


Erro na execução do Naïve Bayes utilizando Spark

Question: Quando altero o arquivo de entrada (por default: sample_svm_data.txt), por outro também contendo atributos numéricos separados por espaços, ocorre o seguinte erro:
File "/usr/local/spark/python/lib/pyspark.zip/pyspark/mllib/util.py", line 53, in _parse_libsvm_line
    index, value = items[1 + i].split(":")
ValueError: need more than 1 value to unpack
O que pode ser?
O algoritmo em python está sendo executado utilizando o Apache Spark, tal qual este exemplo, sendo a única alteração o arquivo de entrada.
Answer: A mensagem de erro encontrada indica que dentro da biblioteca, era esperado que um valor contivesse ":" quando não contém.
(A mensagem não é explicita, por que realmente o código não esperava isso, mas veja a linha de código: ele faz um <code>.split(":")</code> e tenta atribuir o resultado disso a dois parâmetros do lado esquerdo. Sem o  ":", o split retorna apenas um valor)
Você pode verificar os dois arquivos e ver se em alguma das colunas os dados tem ":" no arquivo original?
Pode até ser que os dados sejam separados por ":" em vez de espaços.


Sample size needed to show difference of means is smaller than $y\%$

Question: The unpaired $t$-test is commonly used to reject the null hypothesis that two sample means are equal.
However, suppose one wants to prove that the sample means are no different than some given maximal difference, i.e. we want to reject the hypothesis $|\mu_1-\mu_2|>y\%$. Moreover, suppose we have an a priori estimate of the pooled sample variance $\sigma^2$.
How can one calculate the minimal sample size needed $n$ so that we can reject this hypothesis with a given power?
Comment: Look for TOST : *two one-sided t-tests*
Comment: Null hypotheses typically do not have strict inequality, and are usually specified as based on "(almost) no difference" rather than "at least some specified absolute difference" which is strangely two-tailed.  You might also say what $y\%$ means here: is that a percentage of $\mu_1$ or $\mu_2$?
Comment: @Henry - The percentage shouldn't matter that much, it can be quantified as a specific number or a percentage of the mean of means, either the way the math should be the same.
Comment: @Henry - Do see my answer below.
Answer: A possible approach:
Using the CDF of the student-$t$ distribution:
$$F(t)=1-\frac{1}{2}I_x(\nu/2,1/2),\quad x \equiv \frac{\nu}{\nu+t^2}$$
The $p$-value for the hypothesis $H_0:|\mu_1-\mu_2|>y$ assuming equal sample sizes and average standard deviation $\sigma$ is:
$$t=\frac{\mu_1 - \mu_2}{\sigma\sqrt{2/N}} $$
$$P(H_0)=2-2F\left(\sqrt{\frac{N}{2}}\frac{y}{\sigma}\right)$$
If one is given a priori values of $y$ and $\sigma$ in terms of percentages of the expected value, one can then calculate $N$ for a given $p$-value. This answer is incomplete as it does not give $N$ explicitly nor does it give the dependence of $N$ on the power.


how to select a non-drupal (souce db) database in a migrate module

Question: can i still use a db_select(); if my source database is a non-drupal site? how can the migrate module know what database im referring? i cant see any configuration for migrate module on how to change database selection.
<code>$query = db_select('sourcedb.brands', 'b');
$query->addField('i1', 'foreign_key', 'foreign_key');
$query->addField('i1', 'locale', 'locale');
$query->addField('i1', 'content', 'brand_description');
$query->addField('b', 'name', 'brand_name');
$query->addField('b', 'owg_id', 'owg_id');
$query->join('sourcedb.i18n', 'i1', 'i1.foreign_key = b.id');
$query->condition('i1.locale', 'spa', '=');
$query->condition('i1.model', 'Brand', '=');
$query->condition('i1.field', 'description', '=');
$query->orderBy('i1.foreign_key', 'ASC');
</code>
is the "sourcedb" in 'sourcedb.brands' a keyword in drupal? or can i replace it directly with the table name?
Answer: It would probably be better if you looked at the Feeds module using the Feeds Database fetcher.
Comment: can feeds handle big data transfer?
Comment: Yes it can. And it can do all the drupal-type mapping for you.
Comment: HEy i have another problem, i cant see the list of databases in the Feeds Database module tab. check out my other question
Comment: hey @triskelion check out my other question sir http://drupal.stackexchange.com/questions/66955/cant-see-the-list-of-databases-in-feeds-database-module-ui-tab


Especializar apenas um metodo de classe template

Question: Eu possuo 2 tipos básicos de classes, ClasseA e ClasseB. A ClasseA tem um método que gera um inteiro, e a ClasseB tem um método que gera uma ClasseA.
Eu gostaria que no método de uma ClasseC, caso o template seja uma ClasseA(ou filhas) retornasse o inteiro pelo método diretamente, e caso o template seja uma ClasseB(ou filhas) gere uma ClasseA e depois retornasse o valor pelo  método, se fosse qualquer outra ele retornasse 0.
Resumi os códigos pra facilitar :
<code>//ClasseA.h
class ClasseA{
public:
    int valor;
    ClasseA(int valor) : valor(valor){
    }
    int retValor() {
        int valor_final = valor;
        //alguns calculos
        return valor_final;
    }
}
//ClasseB.h
class ClasseB{
public:
    int valorA, valorB;
    ClasseB(int valorA, int valorB) : valorA(valorA) , valorB(valorB){
    }
    ClasseA toA() {
        int valor_finalA = valorA;
        int valor_finalB = valorB;
        //alguns calculos
        return ClasseA(valor_finalA / valor_finalB );
    }
}
//ClasseC.h
template<typename T>
class ClasseC{
public:
    T classeT;
    ClasseC(T classeT) : classeT(classeT) {
    }
    //vários métodos
    int retValorFinal();
}
//ClasseC.cpp
template<typename T>
int ClasseC<T>::retValorFinal(){
    //????
}
template class ClasseC<ClasseA>;
template class ClasseC<ClasseB>;
//outros tipos
</code>
Answer: Uma possível solução é fazer diferentes versões do método dependendo da classe do template.
<code>template<>
int ClasseC<ClasseA>::retValorFinal() {
    std::cout << "Classe A\n";
    return classeT.retValor();
}

template<>
int ClasseC<ClasseB>::retValorFinal() {
    std::cout << "Classe B\n";
    return classeT.toA().retValor();
}

template<typename T>
int ClasseC<T>::retValorFinal() {
    std::cout << "Classe Desconhecida\n";
    return 0;
}
</code>
Comment: Obrigado, agora funcionou perfeitamente


Owl carousel autoplayTimeout does not work

Question: I have the following settings in my owl carousel
$(".slider").owlCarousel({
        items: 1,
        loop: true,
        nav: true,
        margin: 0,
        dots: true,
        // animateIn: 'fadeIn',
        // animateOut: 'fadeOut',
        autoplay: true,
        lazyLoad: true,
        smartSpeed: 1000,
        autoplayTimeout: 7000
        //autoplaySpeed: true,
    });

I think, it is correct, hovewer the autoplayTimeout does not work. Why? Owl carousel 2, ver. 2.3.4.
Comment: It works fine in their demo on version 2.3.4. https://owlcarousel2.github.io/OwlCarousel2/demos/autoplay.html
Comment: Ok, it works but with the number less or equal 5000. Above not. How to resolve this issue?
Answer: Works just fine as I told you and here you have an example with your settings:

var owl = $('.owl-carousel');
owl.owlCarousel({
    items: 1,
        loop: true,
        nav: true,
        margin: 0,
        dots: true,
        // animateIn: 'fadeIn',
        // animateOut: 'fadeOut',
        autoplay: true,
        lazyLoad: true,
        smartSpeed: 1000,
        autoplayTimeout: 7000
        //autoplaySpeed: true,
});
.item1
{
height:200px;
background-color:red;
}
.item2
{
height:200px;
background-color:blue;
}
.item3
{
height:200px;
background-color:magenta;
}
.item4
{
height:200px;
background-color:green;
}

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.theme.default.css">

1

2

3

4

Comment: works for me, so I voted up


How to calculate KNN Variable Importance in R

Question: I implemented an Authorship attribution project where I was able to train my KNN model with articles from two authors using KNN. Then, I classify the author of a new article to be either author A or author B.
I use knn() function to generate the model.
The output of the model is the table below.
   Word1 Word2 Word3  Author
11    1     48    8      A
2     2     0     0      B
29    1     45    9      A
1     2     0     0      B
4     0     0     0      B
28    3     1     1      B

As seen from the model, it is obvious to see that Word2 and Word3 are the most significant variables that cause the classification between Author A and Author B.
My question is how can I identify this using R.
Comment: Why kNN, and not e.g. decision trees? Decision trees (and random forests) are much easier to use if you want to know about variable importance.
Answer: Basically, your question boils down to having some variables (Word1, Word2, and Word3 in your example) and a binary outcome (Author in your example) and wanting to know the importance of different variables in determining that outcome. A natural approach would be training a regression model to predict the outcome using the variables and to check the variable importance in that model. I'll include two approaches (logistic regression and random forest) here, but many others could be used.
Let's start with a slightly larger example, in which the outcome only depends on Word2 and Word3, and Word2 has a much larger effect than Word3:
set.seed(144)
dat <- data.frame(Word1=rnorm(10000), Word2=rnorm(10000), Word3=rnorm(10000))
dat$Author <- ifelse(runif(10000) < 1/(1+exp(-10*dat$Word2+dat$Word3)), "A", "B")

We can use the summary of the logistic regression model predicting Author to determine the most important variables:
summary(glm(I(Author=="A")~., data=dat, family="binomial"))
# [snip]
# Coefficients:
#             Estimate Std. Error z value Pr(>|z|)
# (Intercept)  0.05117    0.04935   1.037    0.300
# Word1       -0.02123    0.04926  -0.431    0.666
# Word2        9.52679    0.26895  35.422   <2e-16 ***
# Word3       -0.97022    0.05629 -17.236   <2e-16 ***

From the p-values, we can see that Word2 has a large positive effect and Word3 has a large negative effect. From the coefficients we can see that Word2 has a higher magnitude of effect on the outcome (since by construction we know all the variables are on the same scale).
We can use the variable importance from a random forest predicting the Author outcome similarly:
library(randomForest)
rf <- randomForest(as.factor(Author)~., data=dat)
rf$importance
#       MeanDecreaseGini
# Word1         294.9039
# Word2        4353.2107
# Word3         351.3268

We can identify Word2 as by far the most important variable. This tells us something else that's interesting -- given that we know Word2, Word3 actually isn't too much more useful than Word1 in predicting the outcome (and Word1 shouldn't be too useful because it wasn't used to compute the outcome).
Comment: Note that randomForest ist pretty much unrelated to `knn` classification.
Comment: @alandalusi a small p-value indicates that the coefficient differs from 0 with high probability. Yes, Pr(>|z|) is the p-value.
Comment: @Anony-Mousse as I understand it, the labels A and B came from knn, and the author now wants to better understand the relationship between independent variables and label. Therefore there's no need to further use knn to assess the variable importance.
Comment: @Anony-Mousse Thanks for the explanation. Can you further elaborate on the p-value? you said Word2 has a large positive effect and Word3 has a large negative effect. How did you see that from Pr(>|z|)?
Comment: The positive and negative effects can be seen from the sign of the z value


What are U-type statistics?

Question: In an article, I recently came across the mention of first and second order U-type statistics  without further detail.
Does anyone know what U-type statistics are?
References will be highly appreciated.
Comment: Do you mean in [this sense](http://en.wikipedia.org/wiki/U-statistic)?
Comment: Probably averages over functions taking one argument vs. averages over all pairs for functions taking two arguments. A. van der Vaart's *Asymptotic Statistics* has a chapter that provides a lovely introduction to this topic.
Comment: Huber has found the Wikipedia description of U-statistics that come up in nonparametric statistics and were original found by Hoeffding.  That is what I assumed you meant also when I saw the question.  I don't think the term U-type is common though.
Comment: It looks like it. Any hint at what first and second order could be?
Comment: @cardinal yes that would make sense in the context. I will look it up. Thanks!
Answer: From the comments and the answer I got that "U-type statistics" is jargon for "U-statistics".
Here are a couple of elements taken from the reference provided by @cardinal, and in the previous answer. A U-statistics of degree or order $r$ is based on a permutation symmetric kernel function $h$ of arity $r$
$$ h(x_1, ..., x_r): \mathbb{X}^r \rightarrow \mathbb{R}, $$
and is the average of that function taken over all possible subsets of observations from the sample. More formally
$$ U = \frac{1}{\left( \array{n\\r} \right)} \sum_{\Pi_r(n)}h(x_{\pi_1}, ..., x_{\pi_r}), $$
where the sum is taken over $\Pi_r$, the set of all unordered subsets chosen from $\{1, ..., n\}$. The interest of U-statistics is that they are asymptotically Gaussian provided $E \{ h^2(X_1, ..., X_r) \} < \infty$.
Example 1: The sample mean is a first order U-statistics with $h(x) = x$.
Example 2: The signed rank statistic is a second order U-statistics with $h(x_1, x_2) = 1_{\mathbb{R}^+}(x_1+x_2)$ (the function that is equal to $1$ if $x_1 + x_2 > 0$, and $0$ otherwise).
$$ U = \frac{1}{\left( \array{n\\2} \right)} \sum_{i=1}^{n-1} \sum_{j=i+1}^n 1_{\mathbb{R}^+}(x_i+x_i) $$
is the sum of pairs $(x_i, x_j)$ from the sample with positive sum $x_i+x_j > 0$ and can be used as test statistic for investigating whether the distribution of the observations is located at 0.
Example 3: The unit definition space $\mathbb{X}$ of $h$ need not be real. Kendall's $\tau$ statistics is a second order U-statistics with $\frac{1}{2} h((x_1, y_1), (x_2, y_2)) = 1_{\mathbb{R}^+}((y_2-y_1)(x_2-x_1)) - 1$.
$$ \tau = \frac{2}{\left( \array{n\\2} \right)} \sum_{i=1}^{n-1} \sum_{j=i+1}^n 1_{\mathbb{R}^+}((y_2-y_1)(x_2-x_1)) - 1 $$
is a measure of dependence between $X$ and $Y$ and counts the number of concordant pairs $(x_i, y_i)$ and $(x_j, y_j)$ in the observations.
Answer: We have established that U-statistics are what the OP is looking for.  I will address his second question about orer of U-statistics.  The theory of U-statistics can be found in many books on nonparametrics and I am sure also in the various statistical encyclopedias. Here is a nice article by Tom Ferguson that summarizes the theory.  I think it is actually a class tutorial on it.  Here is what he says about order.  The rest you can find in the paper

5. Degeneracy. When using U-statistics for testing hypotheses, it occasionally
  happens that at the null hypothesis, the asymptotic distribution has variance zero. This
  is a degenerate case, and we cannot use Theorem 2 to find approximate cutoff points. The
  general definition of degeneracy for a U-statistic of order $m$ and variances, $\sigma_1^2 \leq \sigma_2^2 \leq ... \leq \sigma_m^2$ given by (19) is as follows.
  Definition 3. We say that a U-statistic has a degeneracy of order $k$ if $\sigma_1^2 = · · · = \sigma_k^2 = 0$
  and $\sigma^2_{k+1} > 0$.

http://www.math.ucla.edu/~tom/Stat200C/Ustat.pdf
Comment: @gu11aume.  Do you know Tom Ferguson?  He was a UCLA professor way back in the late 1970s when I was a graduate student.  At Stanford would used his book in our graduate math stat course.  It was a really good text and I think he writes very well.
Comment: @gui11aume Thanks for the nice editing job.  I just fixed one thing (an extra 1 at the end after k+1).

What are U-type statistics?


Adding NSNumber Object to NSMutableArray

Question: This piece of code causes an error but it should be correct.
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSNumber *rowNumber = [[NSNumber alloc] initWithInt:indexPath.row];
    [genreArray addObject:rowNumber];
    [self.tableView reloadData];
}

genreArray is a NSMutableArray
But on touch on a cell I get this error
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
*** First throw call stack:
(0x1c9c012 0x10d9e7e 0x1c9bdeb 0x1d1c9a5 0x1d1c8b0 0x7b4c 0xcd285 0xcd4ed 0xad75b3 0x1c5b376 0x1c5ae06 0x1c42a82 0x1c41f44 0x1c41e1b 0x1bf67e3 0x1bf6668 0x1dffc 0x271d 0x2645)
libc++abi.dylib: terminate called throwing an exception

Has anyone got an idea why this isn't working?
Comment: Have you alloc `genreArray = [[NSMutableArray alloc] init];`
Comment: In addition to the answer for simplicity you can change the `NSNumber *rowNumber = [[NSNumber alloc] initWithInt:indexPath.row];` with `NSNumber *rowNumber = @(indexPath.row);`
Comment: Problem was i allocated it as NSMutableArray but then i wrote: genreArray = someOtherArray; someOtherArray was an array, not an mutable array. Now i allocate it with [NSMutableArray arrayWithArray:someOtherArray]; That works!
Answer: Because your array is immutable...
Check the code where you instantiated genreArray : both type declaration, AND instantiation must be done with NSMutableArray
//valid declaration + instantiation
NSMutableArray *genreArray = [NSArray array];

//valid at compile-time : genreArray is supposed to be mutable
//however will result in runtime error : genreArray is actually immutable
[genreArray addObject:anObject];

Comment: I've found the problem. There was a worng initialization, thanks =)


How to delete a full row in a ListView if no items are there?

Question: I'm doing something that loads items into two columns in a ListView.
Is there a loop I can run so that if a row does not have an item under column1 AND column2, the row is automatically deleted?
Answer:         var lst = new  ListView();
            for (int i = lst.Items.Count - 1; i >= 0; i--)
        {
            var itemtoremove = //Put your condition here and get that item
            lst.Items.Remove(lst.Items.Find("key"));
        }

Comment: What do you mean by condition? I dont want to delete a row if it says 'soccer' for example, I want to delete a row if any of the two columns don't contain anything.
Comment: Also, I tried the code, and there was this error: Argument 1: cannot convert from 'System.Windows.Forms.ListViewItem[]' to 'System.Windows.Forms.ListViewItem
Comment: ok.. I don't have ide with me..but logically u should remove the items in reverse direction.


place image button over the image background on android

Question: in my app i have a background image and need to place an image button at the specified location of the background image.i did by using frame layout with  android:layout_gravity,layout_alignBottom and marginLeft and right properties. please refer image no :1
i set it for one emulator and it works but i get  wrong when launch other emulator of different size  so how to set image view over a background image so that imageview not changes its position for any size of screen.
Is there any possibilities to  trigger action find by touching image button on the screen(both background image and image button combined as single image - please refer Image no 2)
Answer: For this kind of problem, I would think that the best solution in order not to have problem with the screen sizes/densities would be to have different images for the play phase, stop etc.
Thus having:
microphone with play button: 1 image
microphone with stop button: 1 image
microphone with play selected button: 1 image
Etc. and handling the user events.
Hope this helps!


Inequality involving a convex function

Question: I am stuck, showing the following inequality in an easy way (using only inequalities or something):
Let $x\in [-a,a]$ for some $a>0$ and $p\in (1,2)$. I want to show that there then exists a constant $M>0$ such that for all $x$:
$$||x-1|^p-1|\leq M |x|^{c}.$$
(where $0<c \leq p$; i.e. $c=0.5$).
I am having a hard time showing this innocent looking inequality. The reason is that for $p\in (1,2)$, the function $f(x)=|x|^p$ is convex and homogeneous of degree $p>1$. As a result, $f(x)$ is not subadditive and we can't use the reverse triangle inequality anymore (from which the result would otherwise directly follow).
On the other hand, applying the standard triangle inequality on the absolute value seems not to be sharp enough, since
$$f(x):=||x-1|^p-1| \leq |x-1|^p +1 \leq 2^{p-1}|x|^p + 2^{p-1}\cdot 1 +1 \leq M|x|^c + 3$$
which obviously does not imply the desired upper bound.
=> Is there some kind of (reverse triangle) inequality for convex functions of this kind I am not aware of, or any other easy way to proof the assertion? $$$$
(To see that the inequality holds I can provide an heuristic proof here:
For some subset I can show that the inequality holds: For $x \in [0,2]$ the function $||x-1|^p-1|=1-|1-x|^p = f_1(x)$ is strictly concave and attains a unique maximum at $x=1$ with $f_1(1)=1$. Hence on the interval $[0,2]$ it is sufficient to show that there exists $M_1\geq 1$, such that
$$f_1(x) \leq M_1 |x|.$$
Note that $M_1|x|\geq 1=\max_{x\in [0,2]} f(x)$ for $1\leq x\leq 2$ given $M_1\geq 1$. So, let $0\leq x \leq 1$ we then have by binomial inequality:
$$f_1(x)=1-|1-x|^p = 1-(1-x)^p \leq 1 - 1 + p x \leq 2\cdot x.$$
=> with $M_1\geq 2$ the inequality holds on the subinterval [0,2].
Moreover for $x\in [-c,0]$ the function $f(x)$ is strictly decreasing (and convex) with $f(0)=0$. While the function is strictly increasing on $x\in[0,c]$ so there will indead exist a $M\geq 0$ such that $f(x) \leq M|x|^{0.5}$)
Answer: The function
$$f(x) = \left|1-\left|1-x\right|^p\right|,\quad p\in(1,2) $$
is non-negative and concave in a right neighbourhood of the origin, non-negative and convex in a left neighbourhood of the origin, hence there are no positive constants $M$ and $c\in(0,p]$ such that
$$ f(x)\leq M |x|^c$$
holds over a whole neighbourhood of zero.
Comment: thanks for your answer but I am sorry - I can not follow your argument.

you are right: the function is nonnegativ, convex and strictly decreasing on $[-c,0]$ with $f(0)=0$. the function is nonnegativ and concave on $[0,2]$ where it is strictly increasing on $[0,1]$ and strictly decreasing on $[1,2]$. while on $[2,c]$ it is convex and strictly increasing.

I can't see any argument in this observation which shows me that there shouldn't exists such constants.

I illustrated some special cases which shows that the inequality will indeed hold. take a look at:
http://imgur.com/a/j0nxA
Answer: Let $f(x)= ||x-1|^p-1|$ and $g(x) = M|x|^c$. Assume $p\in (1,2)$, $M>0$, $c \in (0,p]$, $a>0$.
1) If $c>1$ then it will not work:
We have $f(0)=g(0)=0$.  But the right-derivative of $f$ at $x=0$ is $p>0$, while the right-derivative of $g$ at $x=0$ is $0$.  It follows that there is a $\delta>0$ such that $f(x)\geq g(x)$ for all $x \in [0, \delta]$.
2) If $c \in (0,1]$ then it will work for suitably large $M>0$.
The magnitudes of the left and right derivatives of $f$ are bounded over $[-a,a]$, so $f$ is $L$-Lipschitz continuous over $[-a,a]$ for some constant $L>0$, so that $|f(x)-f(y)|\leq L|x-y|$ for all $x,y \in [-a,a]$. In particular, since $f(0)=0$, we get:
$$ |f(x)|\leq L|x|  \quad \forall x \in [-a,a] $$
It suffices to find an $M>0$ such that $L|x|\leq M|x|^c$ for all $x \in [-a,a]$.
Case 1:  Suppose $c=1$.  Then choose $M=L$ and we are done.
Case 2: Suppose $c \in (0,1)$. The functions $M|x|^c$ and $L|x|$ are symmetric about the origin. It suffices to find $M>0$ such that $Lx\leq Mx^c$ for all $x \in [0,a]$.  For $x>0$ we have $\frac{d}{dx} Mx^c = \frac{Mc}{x^{1-c}} \geq \frac{Mc}{a^{1-c}}$.  So $Mx^c \geq \left(\frac{Mc}{a^{1-c}}\right)x$ for all $x \in [0,a]$. Just choose $M$ so that $\frac{Mc}{a^{1-c}}=L$. $\Box$
Comment: thank you very much. using lipschitz-continuuity seems really the key here for a simple proof.
nice and clear. thank you.

Inequality involving a convex function


RegEx npp - having trouble with lookaround matching

Question: I have a complicated bit of text I wish to search/delete from frequent output documents. (See bottom of question for source text)
Using the regex, I wish to:
Leave lines like this: 1234TH-ST-TA5000-1#sh in sh 1/7/12 re
Delete this: several lines of whitespace
Stop before this line: -------------------------------
My current regex uses lookaround to begin matching AFTER the first line, but for some reason it includes the first line in the match and only finds the first instance (there are six or seven that should be matched/removed).
Current RegEx: (?:(\r|\n|\r\n).+-TA5000-\d\#sh in sh \d+\/\d+\/\d+ re\b)\s+
RegEx101 Fiddle to play with
Source text:

1234TH-ST-TA5000-1#sh in sh 1/7/12 re

--------------------------------------------------------------------------------
SHDSL 1/7/12
  Description                      690686
  Admin Status                     In Service
  Oper Status                      Up
  Channel Rate (kbps)              2624
  ES                               0
  SES                              0
  UAS                              0
  CVC                              0
  LOSWS                            0
  Retrains                         0
  Handshake Failures               0
  SNR Margin Absolute (dB)         18,18,18 (Cur, Min, Max)
  SNR Margin Current Train (dB)    18,6,18 (Cur, Min, Max)
  SNR Margin Previous Train (dB)   na,17,17 (Cur, Min, Max)
  Loop Attenuation (dB)            15,15,15 (Cur, Min, Max)
  Upstream PBO (dB)                0
  Downstream PBO (dB)              0
  Annex                            A/B/EFM
  Line Probe                       Disabled
(OUTPUT TRUNCATED)--------------------------------------------------------------
 Exit - 'Ctrl-C', Clear - 'c', Freeze - 'f', Resume - 'r'

SHDSL 1/7/12
  Description                      690686
  Admin Status                     In Service
  Oper Status                      Up
  Channel Rate (kbps)              2624
  ES                               0
  SES                              0
  UAS                              0
  CVC                              0
  LOSWS                            0
  Retrains                         0
  Handshake Failures               0
  SNR Margin Absolute (dB)         18,18,18 (Cur, Min, Max)
  SNR Margin Current Train (dB)    18,6,18 (Cur, Min, Max)
  SNR Margin Previous Train (dB)   na,17,17 (Cur, Min, Max)
  Loop Attenuation (dB)            15,15,15 (Cur, Min, Max)
  Upstream PBO (dB)                0
  Downstream PBO (dB)              0
  Annex                            A/B/EFM
  Line Probe                       Disabled
  Line Rate (N x DS0s)             41,41,41 (Cur, Min, Max)
  Constellation Crossover (DS0s)   60
  Constellation (bits/baud)        3
1234TH-ST-TA5000-1#sh in sh 1/2/24 re

--------------------------------------------------------------------------------
SHDSL 1/2/24
  Description                      689932
  Admin Status                     In Service
  Oper Status                      Up
  Channel Rate (kbps)              832
  ES                               0
  SES                              0
  UAS                              0
  CVC                              0
  LOSWS                            0
  Retrains                         0
  Handshake Failures               0
  SNR Margin Absolute (dB)         29,29,29 (Cur, Min, Max)
  SNR Margin Current Train (dB)    29,4,70 (Cur, Min, Max)
  SNR Margin Previous Train (dB)   na,2,4 (Cur, Min, Max)
  Loop Attenuation (dB)            6,6,6 (Cur, Min, Max)
  Upstream PBO (dB)                3
  Downstream PBO (dB)              4
  Annex                            A/B/EFM
  Line Probe                       Disabled
(OUTPUT TRUNCATED)--------------------------------------------------------------
 Exit - 'Ctrl-C', Clear - 'c', Freeze - 'f', Resume - 'r'

SHDSL 1/2/24
  Description                      689932
  Admin Status                     In Service
  Oper Status                      Up
  Channel Rate (kbps)              832
  ES                               0
  SES                              0
  UAS                              0
  CVC                              0
  LOSWS                            0
  Retrains                         0
  Handshake Failures               0
  SNR Margin Absolute (dB)         29,29,29 (Cur, Min, Max)
  SNR Margin Current Train (dB)    29,4,70 (Cur, Min, Max)
  SNR Margin Previous Train (dB)   na,2,4 (Cur, Min, Max)
  Loop Attenuation (dB)            6,6,6 (Cur, Min, Max)
  Upstream PBO (dB)                3
  Downstream PBO (dB)              4
  Annex                            A/B/EFM
  Line Probe                       Disabled
  Line Rate (N x DS0s)             13,13,13 (Cur, Min, Max)
  Constellation Crossover (DS0s)   60
  Constellation (bits/baud)        3
1234TH-ST-TA5000-1#sh in sh 1/7/1 re

--------------------------------------------------------------------------------
SHDSL 1/7/1
  Description
  Admin Status                     Out of Service - Unassigned
  Oper Status                      Down
  Channel Rate (kbps)              0
  ES                               0
  SES                              0
  UAS                              0
  CVC                              0
  LOSWS                            0
  Retrains                         0
  Handshake Failures               0
  SNR Margin Absolute (dB)         -2,-2,-2 (Cur, Min, Max)
  SNR Margin Current Train (dB)    0,0,0 (Cur, Min, Max)
  SNR Margin Previous Train (dB)   na,8,12 (Cur, Min, Max)
  Loop Attenuation (dB)            27,27,27 (Cur, Min, Max)
  Upstream PBO (dB)                0
  Downstream PBO (dB)              0
  Annex                            A/B/EFM
  Line Probe                       Worstcase
(OUTPUT TRUNCATED)--------------------------------------------------------------
 Exit - 'Ctrl-C', Clear - 'c', Freeze - 'f', Resume - 'r'

SHDSL 1/7/1
  Description
  Admin Status                     Out of Service - Unassigned
  Oper Status                      Down
  Channel Rate (kbps)              0
  ES                               0
  SES                              0
  UAS                              0
  CVC                              0
  LOSWS                            0
  Retrains                         0
  Handshake Failures               0
  SNR Margin Absolute (dB)         -2,-2,-2 (Cur, Min, Max)
  SNR Margin Current Train (dB)    0,0,0 (Cur, Min, Max)
  SNR Margin Previous Train (dB)   na,8,12 (Cur, Min, Max)
  Loop Attenuation (dB)            27,27,27 (Cur, Min, Max)
  Upstream PBO (dB)                0
  Downstream PBO (dB)              0
  Annex                            A/B/EFM
  Line Probe                       Worstcase
  Worst Case Margin                3
  Line Rate (N x DS0s)             0,3,89 (Cur, Min, Max)
  Constellation Crossover (DS0s)   60
1234TH-ST-TA5000-1#sh in sh 1/7/31 re

--------------------------------------------------------------------------------
SHDSL 1/7/31
  Description                      690689
  Admin Status                     In Service
  Oper Status                      Up
  Channel Rate (kbps)              1472
  ES                               0
  SES                              0
  UAS                              0
  CVC                              0
  LOSWS                            0
  Retrains                         0
  Handshake Failures               0
  SNR Margin Absolute (dB)         26,26,26 (Cur, Min, Max)
  SNR Margin Current Train (dB)    26,23,27 (Cur, Min, Max)
  SNR Margin Previous Train (dB)   na,27,27 (Cur, Min, Max)
  Loop Attenuation (dB)            7,7,7 (Cur, Min, Max)
  Upstream PBO (dB)                3
  Downstream PBO (dB)              4
  Annex                            A/B/EFM
  Line Probe                       Disabled
(OUTPUT TRUNCATED)--------------------------------------------------------------
 Exit - 'Ctrl-C', Clear - 'c', Freeze - 'f', Resume - 'r'

SHDSL 1/7/31
  Description                      690689
  Admin Status                     In Service
  Oper Status                      Up
  Channel Rate (kbps)              1472
  ES                               0
  SES                              0
  UAS                              0
  CVC                              0
  LOSWS                            0
  Retrains                         0
  Handshake Failures               0
  SNR Margin Absolute (dB)         26,26,26 (Cur, Min, Max)
  SNR Margin Current Train (dB)    26,23,27 (Cur, Min, Max)
  SNR Margin Previous Train (dB)   na,27,27 (Cur, Min, Max)
  Loop Attenuation (dB)            7,7,7 (Cur, Min, Max)
  Upstream PBO (dB)                3
  Downstream PBO (dB)              4
  Annex                            A/B/EFM
  Line Probe                       Disabled
  Line Rate (N x DS0s)             23,23,23 (Cur, Min, Max)
  Constellation Crossover (DS0s)   60
  Constellation (bits/baud)        3
1234TH-ST-TA5000-1#sh in sh 1/7/32 re

--------------------------------------------------------------------------------
SHDSL 1/7/32
  Description                      690690
  Admin Status                     In Service
  Oper Status                      Down
  Channel Rate (kbps)              0
  ES                               0
  SES                              0
  UAS                              1
  CVC                              0
  LOSWS                            1
  Retrains                         0
  Handshake Failures               0
  SNR Margin Absolute (dB)         -3,-3,-3 (Cur, Min, Max)
  SNR Margin Current Train (dB)    0,0,0 (Cur, Min, Max)
  SNR Margin Previous Train (dB)   na,0,8 (Cur, Min, Max)
  Loop Attenuation (dB)            46,46,46 (Cur, Min, Max)
  Upstream PBO (dB)                0
  Downstream PBO (dB)              0
  Annex                            A/B/EFM
  Line Probe                       Disabled
(OUTPUT TRUNCATED)--------------------------------------------------------------
 Exit - 'Ctrl-C', Clear - 'c', Freeze - 'f', Resume - 'r'

SHDSL 1/7/32
  Description                      690690
  Admin Status                     In Service
  Oper Status                      Down
  Channel Rate (kbps)              0
  ES                               0
  SES                              0
  UAS                              2
  CVC                              0
  LOSWS                            2
  Retrains                         0
  Handshake Failures               0
  SNR Margin Absolute (dB)         -3,-3,-3 (Cur, Min, Max)
  SNR Margin Current Train (dB)    0,0,0 (Cur, Min, Max)
  SNR Margin Previous Train (dB)   na,0,8 (Cur, Min, Max)
  Loop Attenuation (dB)            46,46,46 (Cur, Min, Max)
  Upstream PBO (dB)                0
  Downstream PBO (dB)              0
  Annex                            A/B/EFM
  Line Probe                       Disabled
  Line Rate (N x DS0s)             0,20,20 (Cur, Min, Max)
  Constellation Crossover (DS0s)   60
  Constellation (bits/baud)        N/A
1234TH-ST-TA5000-1#sh in sh 1/7/10 re

--------------------------------------------------------------------------------
SHDSL 1/7/10
  Description                      690685
  Admin Status                     In Service
  Oper Status                      Down
  Channel Rate (kbps)              0
  ES                               57020
  SES                              92
  UAS                              132288
  CVC                              138706
  LOSWS                            131657
  Retrains                         728
  Handshake Failures               0
  SNR Margin Absolute (dB)         -2,-8,20 (Cur, Min, Max)
  SNR Margin Current Train (dB)    0,0,0 (Cur, Min, Max)
  SNR Margin Previous Train (dB)   na,6,10 (Cur, Min, Max)
  Loop Attenuation (dB)            20,8,37 (Cur, Min, Max)
  Upstream PBO (dB)                0
  Downstream PBO (dB)              1
  Annex                            A/B/EFM
  Line Probe                       Disabled
(OUTPUT TRUNCATED)--------------------------------------------------------------
 Exit - 'Ctrl-C', Clear - 'c', Freeze - 'f', Resume - 'r'

SHDSL 1/7/10
  Description                      690685
  Admin Status                     In Service
  Oper Status                      Down
  Channel Rate (kbps)              0
  ES                               0
  SES                              0
  UAS                              0
  CVC                              0
  LOSWS                            0
  Retrains                         0
  Handshake Failures               0
  SNR Margin Absolute (dB)         -2,-2,-2 (Cur, Min, Max)
  SNR Margin Current Train (dB)    0,0,0 (Cur, Min, Max)
  SNR Margin Previous Train (dB)   na,6,10 (Cur, Min, Max)
  Loop Attenuation (dB)            20,20,20 (Cur, Min, Max)
  Upstream PBO (dB)                0
  Downstream PBO (dB)              1
  Annex                            A/B/EFM
  Line Probe                       Disabled
  Line Rate (N x DS0s)             0,19,19 (Cur, Min, Max)
  Constellation Crossover (DS0s)   60
  Constellation (bits/baud)        N/A
1234TH-ST-TA5000-1#
Comment: Not sure what you need - just remove the empty lines after that specific line? Like [here](https://regex101.com/r/F9vf9w/1)?
Comment: Yes, please add that as the answer - I see you got it. I was hoping to leave the conditional `(\r|\n|\r\n)` but I can live without it Thanks!
Answer: You may use
(^.+-TA5000-\d#sh in sh \d+/\d+/\d+ re$)\s+

and replace with $1\n.
See the regex demo.
Note that Notepad++ regexps do not use regex delimiters, there is no need escaping a forward slash.
Also, NPP regex uses the multiline option by default, ^ matches the start of a line, thus, I enabled m option at regex101.com when testing. I also enabled g option there to look for multiple matches (to emulate Replace All).
Pattern details:

(^.+-TA5000-\d#sh in sh \d+/\d+/\d+ re$) - Group 1 (referred to with $1 backreference from the replacement pattern):

^ - start of a line
.+ - any 1+ chars other than line break symbols
-TA5000- - a literal substring
\d - a digit
#sh in sh  - a literal substring
\d+/\d+/\d+ - 1+ digits, /, 1+ digits, /, 1+ digits
 re - a literal " re" substring
$ - the end of line

\s+ - 1 or more any whitespace symbols (these will get removed).
Comment: Your guesses/assumptions re NPP are spot on. Thanks
Comment: @crashwap: If you want to avoid matching this specific line at the start of a file, then yes, a linebreak before it should be captured as well. Replace `^` with `\R` in my above regex.


Changing Bool Value from checkbox

Question: I have a bool called sendDataAuth. This bool is set to true on load. I have a checkbox which is in a checked state as default, on load.
I am trying to get this bool value to change to false if unchecked and back to true if checked again.
I have tried to create a if statement. If the checkbox state is changed, change the value of the bool.
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    if (checkBox1.Checked)
    {
        sendDataAuth = true;
    }
    else
    {
        sendDataAuth = false;
    }
}

The bool remains true and never changes.
Comment: Do you have set the variable to true in the Page_Load event after checking if the Page_Load is called as a postback event? Or do you set the Checked state of the Checkbox in the Page_Load event?
Comment: Have you tried debugging step by step your code?
Answer: In the case of you are using C# Winform
The simple way to change the value of sendDataAuth to the value of the checkbox is :
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    sendDataAuth = checkBox1.Checked;
}

In the case of you are using C# WPF
There may be a misunderstanding between Checked and IsChecked.
Checked is an action that is called when there is a click on the CheckBox.
IsChecked is its value (a boolean)
So if you want to modify the value of sendDataAuth you can bind this value to IsChecked of your Checkbox or if you continue with your actual method do this :
private void CheckBox1_OnChecked(object sender, RoutedEventArgs e)
{
    sendDataAuth = CheckBox1.IsChecked ?? false;
}

Comment: I am confused here. How is this even possible to solve the problem, by changing the code to something that does exactly the same thing, but now it is apparently working?=!  Also since we know now, that it is winforms. Wouldn't it be appropriate to adopt the answer? "In the case of you are using C# WPF" it is technically not the appropriate answer.
Comment: "Must admit I did find something that was changing the bool back again." I shouldn't have removed my first comment ;) sorry mates but in my oppinion this post is not coherent in itself anymore. 1) the problem is not reproducible by teh given information in the post, and the accpected answer does not provide any solution. I think this post should be deleted
Comment: Assuming a WPF application of course. But in that case can he compile its example?
Comment: Mong Zhu Thanks for you feedback.

@TimSchmelter. The checkbox was just for testing purpose and will be deleted shortly and recreated properly
Comment: @Steve Yes it's a WPF answer, the code in the question can't compile in WPF (i don't know if it work in winform ?)
Comment: Legends. Thanks. Sorry was a winform. Checked the code to

 private void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            sendDataAuth = checkBox1.Checked;
}
Comment: @ChrisBirch does it work now after you changed the code to `sendDataAuth = checkBox1.Checked;` ?
Comment: wouldn't WPF have `RoutedEventArgs`  in the event parameters? as opposed to `EventArgs` ?
Comment: sendDataAuth = checkBox1.Checked seems to be a neat why of doing what I was. Must admit I did find something that was changing the bool back again.

Im owning up to that. - Thanks everyone of their help though
Comment: @TimSchmelter I know :) This was just a dirty test on a new winform till its implemented in my code :)

Changing Bool Value from checkbox


No SMTP server defined. Use real server address instead of 127.0.0.1 in your account

Question: We have an in house MS Exchange 2016 server.  It has been running well for a few years now. This is happening more and more often.  A user gets a kickback stating the email was undeliverable.  There have been no changes in the system for sometime so it is very interesting this error pops up like some sort of configuration error.  I've noticed this happens mostly with gmail and  comcast, but a few come from other domains.
I'd like know where to find where the SMTP server is defined as 127.0.0.1.
Thanks,
Here is the actual (revised) email the user gets:
<code> From: Microsoft Outlook
 Sent: Tuesday, August 20, 2019 9:12 AM
 To: FUser LUser
 Subject: Undeliverable: RE: Payment Needed - VMC Design Order

 EXCHANGE2016 rejected your message to the following email addresses:
 FRecp LRecp (email@example.com)
 A problem occurred while delivering your message to this email address. Try sending your message again. If the problem continues, please contact your email admin.

 EXCHANGE2016 gave this error:
 No SMTP server defined. Use real server address instead of 127.0.0.1 in your account.

 Diagnostic information for administrators:

 Generating server: EXCHANGE2016.DAMIN.GOT

 firstname.lastname@example.com
 EXCHANGE2016
 Remote Server returned '550 No SMTP server defined. Use real server address instead of 127.0.0.1 in your account.'

 Original message headers:

 Received: from EXCHANGE2016.DAMIN.GOT (10.1.7.8) by EXCHANGE2016.DAMIN.GOT
  (10.1.7.8) with Microsoft SMTP Server (TLS) id 184.108.40.206; Tue, 20 Aug 2019
  09:11:48 -0500
 Received: from EXCHANGE2016.DAMIN.GOT ([fe80::4d0e:7a20:4aa0:90e1]) by
  EXCHANGE2016.DAMIN.GOT ([fe80::4d0e:7a20:4aa0:90e1%13]) with mapi id
  15.01.0225.041; Tue, 20 Aug 2019 09:11:42 -0500
 From: FUser LUser <email@example.com>
 To: FFRecp LRecp LFRecp LRecp <email@example.com>
 Subject: RE: Payment Needed - VMC Design Order
 Thread-Topic: Payment Needed - VMC Design Order
 Thread-Index: AdVO5uopoQqQJ3urQyi6W+fJb7TlCQAL3KAAALdwjfAADmk+gAEYTI/gAD6dUAAAChE60A==
 Date: Tue, 20 Aug 2019 14:11:42 +0000
 Message-ID: <email@example.com>
 References: <email@example.com>
  <firstname.lastname@example.com>
  <firstname.lastname@example.com>
  <firstname.lastname@example.com>
  <firstname.lastname@example.com>
  <firstname.lastname@example.com>
 In-Reply-To: <email@example.com>
 Accept-Language: en-US
 Content-Language: en-US
 X-MS-Has-Attach: yes
 X-MS-TNEF-Correlator:
 x-originating-ip: [10.1.2.52]
 x-avast-antispam: clean, score=10
 x-original-content-type: application/ms-tnef
 Content-Type: multipart/mixed;
 boundary="_004_b9b88a1a5994480fa06dc14e3f9aeefdincnet_"
 MIME-Version: 1.0
</code>
Comment: @HarryJohnston I'm still trying to figure out exactly which anti-virus software it is, but that might be enough for the OP to figure it out on their own.
Comment: To avoid any possible confusion, are the emails that are being rejected coming *from* your domain and being rejected *by* gmail, comcast, etc., or are they coming *from* gmail, comcast, etc., and being rejected *by* your domain?
Comment: @HarryJohnston The question states that the mail is coming from local users and being rejected by the local Exchange server.
Comment: @MichaelHampton, I'm prepared to defer to your expertise, but I don't understand how you drew that conclusion.
Comment: @HarryJohnston I just read the question. It wasn't difficult.
Comment: @MichaelHampton, but the email message explicitly says that the "Remote Server" generated the 550 error.  Surely that means gmail.com?
Comment: @HarryJohnston No, that is plainly not a Gmail message. In fact, a quick Google suggests that it comes from anti-virus software. Someone's really messed up on this Exchange server.
Comment: @MichaelHampton, ah, so the Exchange server *thinks* the message came from gmail.com but actually the connection had been intercepted by some sort of anti-virus solution?  Sounds reasonable.  Should that be an answer, do you think, or is it too speculative?
Comment: We use AVG for antivirus protection.  Part of the kickback email states  "x-avast-antispam: clean, score=10"  still trying to run down why it shows avast.    I did disable the email scanning on the server but it did not change anything.
Answer: If you use avast on your server, you need to disable the Spam Shield and Mail Shield components on your server. Apparently they are for workstation use only. You can leave the "Exchange" Shield on to keep protecting your email server.
Source: https://forum.avast.com/index.php?topic=228734.0

Re: Avast Business Antivirus - block e-mail from my server to gmail

Quote from: kel2 on September 03, 2019, 04:12:23 PM
Found this ... Why has it been working for so long and now stopped ...
    https://kb.support.business.avast.com/GetPublicArticle?title=No-SMTP-Server-Defined-Error-BAV#

I'm unsure how to answer your question, but like the Avast Firewall, both the Mail Shield and the Anti-spam components are designed only for workstations and not mail servers. I've heard our development/production team are working on a permanent solution that will automatically remove these components if the Avast installation detects Exchange on the server.
Answer: John's solution works, but you don't need to disable the whole Mail Shield. instead disable only Scan outbound emails (SMTP) in Avast Settings.
Go to Avast Interface > Settings > Protection > Select Core Shields > Under shield settings, go to Mail Shield tab section > Disable Scan outbound emails (SMTP).


Building An RTP Server in iOS That Allows WiFi Speakers To Stream Music

Question: A Little Background On Why I Have To Do This
I am currently optimising an app in order to improve the transferring of media files to the WiFi speakers that our team developed. Our solution before was using iPhone as an HTTP server and then allow the speakers to connect and download music from it. But unfortunately a lot of problems occurred such as frequent slow transfer speed, file read failure, and when user uses the "seek" command, the speakers would have to download the whole file in order for it to seek into that particular time before it starts to play. This is a very bad experience for our users.
What I Need
In order to solve the problem I mentioned above. We thought of changing the HTTP server to an RTP server that will be ran on an iPhone and then allows the WiFi speakers to stream music from it. However, from what I read on other Q&A platforms they mentioned that iPhone does that support transferring of data using RTP. I also tried searching here in stack but were not able to find an answer that solves my problem.
My Question
Is it possible to run an RTP server on iPhone and is there any demo about this that I can refer to?
Any suggestions would be high appreciated.
Answer: Please read link http://dss.macosforge.org/
Darwin Streaming Server from Apple official.
However, I'm not sure it can work on iOS.
Best regards,
Comment: Have a look this link hopefully it can help you .http://research.edm.uhasselt.be/~jori/page/index.php?n=CS.Jrtplib


calling jni method from other jni library

Question: Is it possible to call jni method of 1 library from another method of different jni library ?
for eg: I have 2 libraries lib_1.so and lib_2.so.
I want to call a method get_interface() of lib_1.so from lib_2.so.
Is this possible?
If yes, please share the example of how this can be done.
Answer: When calling from one shared library to another, its really no longer jni, its just native code(c->c or c++ -> c++).  Include the .h and invoke the function as you would normally, passing whatever parameters the function requires.
Answer: I think you can do dlopen("lib2.so") from lib1.so so you have a handle to your lib2.so library and then using that handle you can call the methods.


How to get contract wage from worked days in odoo8

Question: contract.wage is returning the wage of contract mentioned in payslip. But what if i have multiple worked days input and other inputs with different contracts of same person. We have a contract field for each input.
I want to get wage of each contract mentioned/selected in worked days inputs and other inputs.
Note: I want to access wage of contract in salary rules.
Any idea??
Answer: You can can access the wage on Worked Days inputs from salary rules using the relation:
worked_days.WORK100.contract_id.wage

where WORK100 is the code of the Worked Days input. In Odoo 10, this code can be named anything. For example, in my own case, I have input codes NORMAL and SPECIAL for normal and special work respectively.


How to convert Binding array value to Binding<[type]> instead of [Binding] in Swift/SwiftUI

Question: I have trouble with getting Binding values in correct format out of dataModel.
The XCode error I am getting is
"Cannot convert value of type '[Binding<String?>]' to expected argument type
'Binding<[String]>'"
public final class RoomSelectionDataModel: ObservableObject {
    @Published var roomsList: [Room]
    @Published var selectedRoom: String?

}
public struct RoomSelectionView: View {
    @StateObject var dataModel: RoomSelectionDataModel
    public var body: some View {
        let bindingArray = $dataModel.roomsList.compactMap { $0.name } // here the format is '[Binding<String?>]' instead of 'Binding<[String]>'
        SelectionList(listElements: bindingArray,
                      backgroundColor: Color(red: 0.94, green: 0.94, blue: 0.94), // color to be removed
                      selectedElement: $dataModel.selectedRoom)
    }

    public init(dataModel: RoomSelectionDataModel) {
        self._dataModel = StateObject(wrappedValue: dataModel)

    }
}

public struct SelectionList: View {

    let backgroundColor: Color
    @Binding var listElements: [String]
    @Binding var selectedElement: String?

    public init(listElements: Binding<[String]>, backgroundColor: Color, selectedElement: Binding<String?>) {
        self._listElements = listElements
        self.backgroundColor = backgroundColor
        self._selectedElement = selectedElement
    }
}

 public class Room: NSObject, Codable {
    public var name: String?
}

The models are simplified here, to only have the relevant information.
Comment: `Binding` is by definition a two-way connection. You break the connection with the `compactMap` line, its purpose is to transform. Swift does not have the ability to maintain the connection between the array of names and the array of rooms. Pass the entire room array if you need the connection or you can keep the array of strings without the binding, solely for display. If you extract the `Room` object with a `ForEach` you can use `room.name`.
Comment: What's the definition of `SelectionList`? But, the error is saying that you are giving an array of bindings optional string, but it expects an binding of array of string.
Comment: Also, show the declaration of `Room`
Comment: Updated post with requested information.
Comment: You seem to have mismatched types. Why are you trying to connect an array of bindings to a binding of an array?
Comment: @burnsi Yes I modify it.
Comment: @Cristik This is the essence of my question.
I could add another property like
```@Published var roomsListAsString: [String] = [] ```
And change the implementation of ```@Published var roomsList: [Room]``` to
```@Published var roomsList: [Room] {
didSet {
self. roomsListAsString = roomsList.compactMap { $0.name }
}
}
```
But this doesn't feel clean, and I think this is like second unecessary source of truth(which is redundant).
So the question is basically how to do it in a clean way, having one source of truth.
Comment: @Asperi Could you please specify which local variable do you mean?
Comment: Still, it's clear that you are mixing two incompatible types. Why would you expect for that to work? Maybe rephrase the question to let us know what you need to achieve?
Comment: @Cristik "Why would you expect for that to work? ".

I am not convinced these are incompatible types.
What I am using(without the compactMap function) is basically

Binding type while what I need is Binding

But every single room has property of String type. So it feels like it should be possible to achieve something like Binding which would be Binding.

Or other approach could be to flatten it like with Combine Publishers, where you can flatMap them into one, but I am not sure if that's possibile with SwiftUI Binding types.
Comment: But they are obviously incompatible, the error message clearly states this: cannot convert from `[Binding']` to`Binding<[String]>`. Leaving aside the optionality, you're still trying to pass an array of bindings instead of a binding to an array.
Answer: Lets try explicitly creating the binding array as it seems you wish to:
        let bindingArray = Binding<[String]>(
            get: { dataModel.roomsList.compactMap { $0.name } },
            set: { newValue in
                let rooms = dataModel.roomsList.filter { $0.name != nil }
                rooms.enumerated().forEach { numberedPair in
                    numberedPair.element.name = newValue[numberedPair.offset]
                }
            })

The get side of the binding is pretty straightforward.  When you do the put side, things get a little more complicated.  What my code does is assumes that the rooms are in the same order, that all the rooms which had names last time still have names, and that you're going to assign the names into the rooms that have them in their ordinal positions based on the new array.
All in all it's not clear why you want a Binding on the array because the put operation of [String] doesn't really match the model here.


What is the probability that the distance between any 2 consecutive points on the line is less than the given minimum distance, d?

Question: Question:
Given a line segment $L$ and $n$ random points on it. Assuming the points are uniformly distributed over $[0, L]$. What is the probability that the distance between any 2 consecutive points on the line is less than the given minimum distance, d?
Attempt:
This question came from my curiosity, but my attempt is as follows:
To calculate the probability that the distance between any two consecutive points on the line is less than the given minimum distance $d$, we need to consider the total number of possible arrangements of the $n$ points on the line, as well as the number of arrangements that satisfy the condition.
The total number of possible arrangements of the n points on the line is given by the number of ways to choose $n-1$ points out of $L$, which is given by the binomial coefficient: $$C = {L-dn+1 \choose n-1}$$
Now, let's consider the number of arrangements that satisfy the condition. To do this, we can imagine dividing the line segment into small intervals of length $d$. For the distance between any two consecutive points to be less than $d$, each interval must contain at most one point. Therefore, the number of arrangements that satisfy the condition is given by the number of ways to choose $n$ or fewer intervals out of $(L/d)$ intervals, which is again given by the binomial coefficient:
$$S = {L/d + 1 \choose n}$$
Finally, the probability that the distance between any two consecutive points on the line is less than d is given by the ratio of the number of arrangements that satisfy the condition to the total number of possible arrangements:
$$P = \dfrac{S}{C}$$
I've tested the theoretical calculation with a Monte Carlo simulation. The results do not match. Hopefully, someone can provide an answer to the query. Thanks.
EDIT: For reference, using Monte Carlo simulation with the parameters $n = 10, L = 20, d = 2$, and $10^6$ trials the estimated probability is 0.002.
Comment: Are you asking for the probability that at least one of the $n-1$ distances is greater than $d$, or the probability that a randomly selected one of the $n-1$ distances is greater than $d$?
Comment: @BrianTung Perhaps the Monte Carlo algo steps will clarify your question. 1. generate n random points
2. sort the points by order of smaller first
3. calculate the distances between consecutive points
4. count how many distances are smaller or equal to the minimumDistance.
Comment: You approach of dividing the line into intervals and requiring each interval to contain one point is incorrect. Imagine two consecutive intervals and one point is at the beginning of the first interval and the second is at the end of a second. The distance between points will be greater than $d$
Comment: Steps $1, 2, 3$ were never in doubt. I was asking whether in $4$ you want the probability that at least one of the $n-1$ distances is greater than $d$, or the probability that a randomly selected one of the $n-1$ distances is greater than $d$ (equivalently, the expected proportion of distances greater than $d$). It sounds like the latter? However, with $10$ points on an interval of length $L = 20$, how could only $0.002$ of the distances be greater than $2$? That doesn't sound right.
Comment: @BrianTung The question is asking for the probability of all $n-1$ distances being less than $d$.
Comment: Which is the same as asking for the probability that the *maximum* of the $n-1$ distances is less than $d$.
Comment: Ahh! OK, that was not so clear to me, thanks!
Answer: What is described is the probability that the maximum, $x_{max}$, of the $n-1$ distances is less than $d$. This question was almost answered in a CV post. The difference here is that the distance from 0 to the first point and the distance from the last point to $L$ are not considered. To exclude those two intervals, change the $k+1$ term in the summation and binomial coefficient to $k-1$ (or change $n+1$ to $n-1$ using our notation):
$$P(x_{max}\leq d)=\sum_{j=0}^{n-1}{n-1\choose j}(-1)^j(1-jd/L)_+^n$$
As a function in R:
f <- function(n, d) {
  j <- 0:(n - 1L)
  jd1 <- 1 - j*d
  jd1[jd1 < 0] <- 0
  sum(choose(n - 1L, j)*(-1)^j*jd1^n)
}

L <- 20
d <- 2
n <- 10L

f(n, d/L)
#> [1] 0.00199584

Check against a simulation with $10^8$ replications:
s <- 1e8L
m <- matrix(rexp((n + 1L)*s), s, n + 1L)
mean(rowSums((m/rowSums(m))[,2:n] > d/L) == 0)
#> [1] 0.00200159


Dynamic filtering values in select elements in Datatables

Question: Using the following code of multi-filtering select inputs in Datatables is it possible to show only available values in the other select inputs upon a selection in one filter? To be more precise, in this example if I select 'Tokyo' as an Office, I would like to populate only the values 'Accountant', 'Integration Specialist', 'Support Engineer' and 'Regional Marketing' in the dropdown menu of Position.
$(document).ready(function() {
$('#example').DataTable( {
    initComplete: function () {
        this.api().columns([1,2]).every( function () {
            var column = this;
            var select = $('<select><option value=""></option></select>')
                .appendTo( $(column.footer()).empty() )
                .on( 'change', function () {
                    var val = $.fn.dataTable.util.escapeRegex(
                        $(this).val()
                    );

                    column
                        .search( val ? '^'+val+'$' : '', true, false )
                        .draw();
                } );

            column.data().unique().sort().each( function ( d, j ) {
                select.append( '<option value="'+d+'">'+d+'</option>' )
            } );
        } );
    }
} );
} );

////// here I get the unique values of each filtered `select` option
$('select').on('change', function () {
            var dtable = $('#datatable').DataTable();

            var filteredArray = [];
            var filteredArray2 = [];

            dtable.column(1, { search: 'applied' }).data()
            .unique()
            .sort()
            .each(function (value, index) {
                filteredArray.push(value);
            });

            dtable.column(2, { search: 'applied' })
            .data()
            .unique()
            .sort()
            .each(function (value, index) {
                filteredArray2.push(value);
            });

            console.log(filteredArray);
            console.log(filteredArray2);

});

In my case I have filters in two columns only as it is shown in the above snippet, so upon selection in one of the two filters I would ideally like to show only available values in the other filter.
Although I have managed to get the unique values of each filter upon a selection I am struggling to hide all the input values that do not exist in the filteredArrays
Answer: Here is one approach for doing this.
The end result is as follows:

Building a drop-down which only contains the unfiltered (visible) values of a column is relatively straightforward. At the heart of doing this we use the following:
columns( { search: 'applied' } ).data()[index]

Most of the complexity relates to managing the inter-related states of the two drop-downs. After loading the page, whichever drop-down gets used first is designated as the "primary" drop-down and the other is the "secondary". Whenever the user selects a new value from the primary drop-down, we have to clear the secondary drop-down; and then after the primary drop-down filter has been applied, we have to re-build the secondary drop-down's list of values.
The end result is this:

Comment: @andQlimax - Do you mean 40 drop-downs which are all inter-dependent? (The selected value in any one drop-down will affect the available values in all the others?)
Comment: If you don't mind me asking - what is the scenario where you have this requirement? It's very unusual, I think, because it would be potentially complex for a user to actually use. Or, to ask the question a different way: What do you lose if you have 40 independent drop-downs?
Comment: OK - understood. In that case, this is more like a drill-down than a traditional filter (or perhaps a hybrid of the two). Take a look at the new [SearchBuilder](https://datatables.net/blog/2020-09-01) extension for DataTables. Click on the "Add condition" button to see it in action. If that is not the way forward for you, then it's going to be a custom JavaScript solution, with a UI similar to how Excel does it. (Or, you can ask the community in a new question - they may have a better approach than I can think of).
Comment: what about if there are several dropdowns? I have a scrollable table with around 40 dropdowns, is there an other way to do it?
Comment: yes exactly..but not sure if there is an easy way to manage (I'm using jquery datatables, but anyway still need to update the dropdown options)
Comment: Well the customers want to be able to apply filters like in excel (every filter applied, update the reamining filters), obviously they will not use all 40 filters together, but maybe only 2 or 3 together (but still I need to manage on all columns of the table)
Comment: WOW that's powerful..It can help my scenario. Actually I already develoved the dropdown filters on each column of the table, but they are all independed, probably I better remove the dropdown on each column and add the "Search Builder", I didn't kknow it exists
Comment: It does not provide your requirement to have the "next" drop-down list filtered based on previous ones - but otherwise, it may be useful. In fact, I wonder if you can customize that aspect of its behavior.


Criar uma animação em canvas sobre vídeo

Question: Estou fazendo uma pequena aplicação web onde preciso, a partir de algumas coordenadas fazer uma caixa (retângulo) se mover frame a frame.
Em meu código já consegui extrair o frame atual do vídeo porém não consigo fazer a caixa se movimentar frame a frame, segue o código abaixo(deixei apenas a parte relevente):
var i;
var canvas2 = document.getElementById("canvas2");
var ctx = canvas2.getContext("2d");

function boundingBoxes(){
  for(i=0; i < 480; i++){
    ctx.strokeRect(i,20,100,100);
    setTimeout(function(){ctx.clearRect(i-2,18,105,105)},3);
  }
}

O vídeo aparece acima, logo abaixo dele existem duas janelas de canvas sobrepostas, uma que recebe o vídeo, a segunda onde estou desenhando as boxes, alguém tem alguma ideia ou referência ?
Answer: O problema da canvas é que ela é como um tela de pintura, o que você pintou vai fica la, a menos que você mesmo "apague" (pinte por cima).
O método que você esta usando clearRect serve para limpar uma área, eu pessoalmente uso o próprio fillRect ja que do mesmo eu terei de "limpar" a tela.

var canvas2 = document.getElementById("canvas2");
var ctx = canvas2.getContext("2d");

var x = 10;
var ani = setInterval(function(){
  ctx.fillStyle="#FFF";
  ctx.fillRect(0, 0, 300, 80);
  ctx.fillStyle="green";
  ctx.fillRect(x, 20, 50, 50);
  x+=5;
  if(x >= 290 - 50){
    clearInterval(ani);
  }
},100);
<canvas id="canvas2" width="300" height="80" style="border:solid 1px #CCC;">
  Seu browser não tem suporte a canvas
<canvas>

Animação com canvas é pura ilusão é o mesmo que tirar um monte de print da tela e apos ir passando uma por uma bem rápido, criando um efeito de movimento.
Comment: Olá novamente, consegui realizar o que estava procurando graças a seu código, vou posta-lo como resposta abaixo para futuras pesquisas envolvendo o assunto, mais um vez, obrigado!
Comment: Olá @Guilherme Lautert, obrigado pela resposta, abaixo coloquei o código que estou utilizando para podermos melhor discutir uma solução para o problema, quando eu desenho mesmo que em branco sobre o canvas irei sobrepor o vídeo, por isto estava utilizando a função clearRect.
Answer: Olá estou postando a resolução de meu problema inicial que alcancei graças a contribuição de @Guilherme Lautert, fiz algumas modificações para que o canvas não sobreponha o vídeo.
<!DOCTYPE html>

0

<video height="180" width="100%" id="video" controls = "true">
  <source src="http://www.w3schools.com/html/mov_bbb.mp4"></source>
</video>

  <button id="play-pause" onclick="drawCanvas()">Play</button>

<canvas id="canvas" width="480" height="270"> </canvas>
<canvas id="canvas2" width="480" height="270"> </canvas>


Selecting a range of rows that is the same number as a predefined range

Question: So I'm trying to produce a scatter graph and histogram.
I have saved the range of my active selection in a variable like so,
Dim MyRange As Range
Set MyRange = Selection

What I'm now trying to do is select another range that is 4 columns to the left of this range - but has the same number of cells. Again, this could be saved into a variable. - If I were to do this with a single cell I would use Active.Offset - is there a similar way to do this with a range?
What I then want to do is produce the scatter graph using these two ranges as the x and y values.
For example,
Range(MyRange, NewRange(this will be the rows, 4 columns adjacent to MyRange)).Select
ActiveSheet.Shapes.Addchart.Select
ActiveChart.ChartType = x1XYScatter

Thanks for any guidance.
Answer: Use
Dim r as Range, x as Range, y as Range
Set r = Selection
Set x = r.Offset(0,4)  `same size as r, but 4 columns to the right
Set y = r.Resize(r.Rows.Count,4)  `same rows as r, but with 4 columns

Comment: Thanks, that worked well. One final question, this keeps confusing me - for example I'm using a variable (already defined as a range) in something that usually selects a range, for example the below requires X and Y values, do I do it as such,
ActiveChart.SetSourceData Source:= Range.(MyRange, NewRange) or do I now scrap the Range at the beginning since it's already defined as a range?
Hope this makes sense.
Comment: Yes keep it simple. You can do something like `Source := MyRange.Resize(MyRange.Rows.Count,4)` directly so you don't have to define separately `NewRange`. Also the statement `Range(x,y)` is not needed.


What's the story behind the log-normal distribution?

Question: I have been playing around with datasets for the past while for practice. I've noticed that a distribution that looks something like the following appears:

This shape appears frequently! I can guess it is the log-normal (because I am familiar with the tip-skew of the distribution). I do not understand why this distribution appears in nature so often.
Is there some intuition to why this comes up frequently?. My guess is that there is a natural skew in the data. The distribution above was for the prices of lunch bills. The positive skew here would be due to people wanting to pay less for their meals.
Comment: The lognormal can arise through multiplicative processes acting on Gaussian (normal) inputs. Skew here is the name of the problem, not the name of the solution, but many processes have lower bounds at or very near zero but no well defined upper limit
Comment: @Nick For the same reasons Gaussian distributions may appear "in nature" as sums of many small near-independent perturbations, *none of which need be Gaussian*, Lognormal distributions appear as products of many small near-independent perturbations: and again they needn't be Gaussian.
Comment: A lognormal is always right-skewed (except in some parameterisations the normal is a limiting case). What you describe is left-skewed and thus  some other distribution and not lognormal. Indeed I understand SAT to be bounded, so that's inevitable for that reason alone. The existence of other distributions shapes arises for other reasons. I have never heard grading described as a "natural" cause before.
Comment: From far being concrete, the best rationale is abstract, some kind of multiplicative spin on the central limit theorem. I don't know a general theory otherwise. You need to separate out reasons for skewness and reasons for lognormality, which your comments seem to conflate. Why not ask, why are many distributions gamma-like or Weibull-like, which their enthusiasts would want to underline? The selection process is that theorists propose distributions as more or less tractable functions with total probability 1 and practical people push those easy to fit and which seem to work some of the time.
Comment: @whuber Naturally I agree. Indeed, how does the Gaussian arise?
Comment: @whubber If I understand correctly, you're saying that log-normal is due to noise in a Gaussian? If so, I do not agree. SAT scores at Harvard follows a log-normal skewed negatively (people get good grades). This is due to a natural cause of the data itself.
Comment: The natural tendency/cause would be that Harvard students try to get good grades and that causes the skew. My intution is that there is always a story behind skews. But I'd like a concrete answer to the reasons for seeing the log-normal frequently.
Comment: You are right. I just have seen the above shaped distribution very frequently. I was hoping that someone could shed light into why I see it so often.
Comment: The shape you described is not specific of log-normal: a number of other distributions can have the same shape (e.g. gamma).
Comment: Thanks Tim. So for all skewed distributions, do you immediately think of fitting a gamma or log-normal?
Answer: As observed in  comments, the lognormal distribution can arise as a kind of multiplicative central limit process. Formalizing, assume $Z_1, Z_2, \dotsc, Z_n$ are iid positive random variables such that the expectation and variance of $\log Z_i$ exists.  We are interested in the (limiting) distribution of $Z=\prod_1^n Z_i$. Write $Y = \log Z = \sum_1^n \log Z_i$. Now we can apply the usual central limit theorem for the sum of $\log Z_i$, obtaining an (limiting) normal distribution for $Y$. It follows that $Z=e^Y$ will have a (limiting) lognormal distribution.
A paper with many good examples is https://stat.ethz.ch/~stahel/lognormal/bioscience.pdf


How to redirect a node to an external URL?

Question: Is there a way to "alias" a Drupal site node to an external URL?
I'd like that a click on a link in my Drupal site like 'http://mysite/node/64' redirects to an address like 'http://anothersite/whateverurl'.
I use Fullcalendar View module.
I added a Date range field to a custom content of mine (my modified Basic page).
If I set a date in a node of that type, I can see an item linked to it in the Calendar:

If I click on the item, I am redirected to the corresponding node and I see its content.
For some nodes, I'd like the link to point to an external url instead...
[Edit]
I tried something else: I created a new View with no fields at all except the Daterange.
When I use Format: Unformatted list, I get this result:

which is OK as I have only one node of the type filtered.
When I use Format: Full Calendar Display, I get this result:

which is strange as I have no fields in the View description pointing to the node itself...
Does it mean that Fullcalendar View] module generates its magic elsewhere?
I'm a bit confused :-(
Any help?
Comment: If you're redirecting why would you also have a node? Can't you just redirect the URL at vhost/.htaccess level?
Comment: Or with the same result in a request event subscriber, example before routing [How do I redirect users visiting a path matching a regular expression?](https://drupal.stackexchange.com/a/223896/47547) , which you can also do with a lower priority after routing to catch aliases of the node.
Comment: Updated my question to clarify why...
Comment: You'd be better off keeping that external URL in a field attached to the node, then overwriting the Views row template to link it based on that value if available
Comment: Any clue how to do that?
Comment: I gave this Fullcalendar module a shot and it seems its not Modular enough to do what I describe below ... if this issue is resolved https://www.drupal.org/project/fullcalendar_view/issues/2972642 or with custom programming it can probably be done.
Comment: In this case, what should be the proper hook_views where I should put my custom code?
Answer: Since you want this done in a view here is a possible solution

Add a link field to your content type external url which would need to be followed when available otherwise (if empty) it would take to node view (normal behaviour).

Modify the Fullcalendar View and add follow 2 fields

Link to content hidden

external url and adjust the No results behaviour to include {{ view_node }}

Use external url as the link

NOTE: it appears that Fullcallendar view module takes the link to node in a way that it cannot be modified within the view as described above.  There is a feature request that asks for such an option.  In the meantime perhaps the above can be achieved with another calendar module () or modify the URL from within a custom module.
Comment: Sorry. I couldn't make your proposal work. I don't understand your 4th point: Use `external url` as the link. How do I do that?
Comment: @gbmapo your calendar view outputs a field that is the actual link. Change that field to use the external URL output instead, you can post a screenshot of your calendar View configuration
Comment: I updated my question...
Comment: I just read your NOTE.
Any clue where I can put the code in my custom module?
what should be the proper hook_views to be used?
Comment: @gbmapo I believe this functionality is hardcoded in `template_preprocess_views_view_fullcalendar` function in `fullcalendar_view.theme.inc` file.  Its a template function so you can probably copy paste it in your `THEMENAME.module` and rename it to `THEMENAME_preprocess_views_view_fullcalendar`.  I think what needs changing  is on line  154 `'url' => $current_entity->toUrl()->toString(),`
Comment: I added the following [code](https://imgur.com/a/3sxI8VC) in `mytheme_preprocess_views_view_fullcalendar` and it works!
Thanks for putting me in the right direction.
Answer: Lets say you have a node on your website
example.com/node/123 with an alias to it example.com/my-node
If what you need is:

when a user try to access the node by going to example.com/node/123, so he will be redirected to an external url.
but when the user access the node by going to example.com/my-node, no redirection and he will normally have access to the node page.

If this what you are asking about, reply to my answer here and i will gibe you a full tutorial on how to do it !
Otherwise, as @clive mentioned, your question does not make sense ! And for what you need the node if you are not going to use it.
Comment: As very straight solution, you can use rules module or business rules module and create a condition to fire a page redirect action when for example certain arguments are available in the url or when a specific node field = to a specific value. **Meaning:** if the node you want to be redirected to an external url has the **title field** contains for example the world *external* so you can add a **condition** to check the title of every **loaded node**, if the **title contains** tbe word **external* so fire a **page redirect** action to whatever url you want. Hope it helps !!!
Comment: Updated my question to clarify why...
Comment: **For some nodes, I'd like the link to point to an external url instead...** does those nodes have in the url anything to distinguish them from the other nodes ? What is the difference between those nodes you want to redirect and the others with no redirection ? (Field, url, title..!?!)
Comment: I thought to create a specific content type for that...
Answer: It's not easy to do this, as the other answers show.
I wrote a module called "Alien Alias" which does this, it can link to internal or external locations. It also lets you keep stats of the clicks (if you want it to).
(Optionally) it intercepts very early in the page build process to redirect away, although you can let it build a whole page and then redirect.
Here: https://drupal.org/project/alien_alias


Run php function on selected time

Question: I want to run a php function on my server in a certain time without make the script sleep.
I need this in order to update my database in specific time.
For example a chat room that lasts for 5 minutes.
The chat room have a flag in the database for open status.
I want to change the flag in the database to close after 5 minutes.
<code>public function callOnDelay($time, $data)
    {
        //SOME CODE EXECUTED AFTER SOME TIME IS OVER//
    }
</code>
Comment: Ok thanks for sharing. While we are listing things we want: I could use a new car.
Comment: Make `cron` do the job?
Comment: @PeeHaa Well in java this function is very easy
Answer: There are different ways of doing this depending on your OS.
On Linux you can look for Cron Jobs (http://www.thesitewizard.com/general/set-cron-job.shtml).
On Windows you can look for Task Scheduler (http://technet.microsoft.com/en-us/library/cc766428.aspx).
Also another, not so reliable, method of doing this is adding a conditional/if in your main script (index.php ?) or a "before function" in your controller (if you are using some kind of framework that supports it) and checking for the last status and doing something with it.

Run php function on selected time