Subdistribution/cause-specific survival functions

Question: How is the subdistribution survival function, associated with the subdistribution hazard function, interpreted? Is it just the probability of failing from the specific cause after the current time? I know it was developed to allow to estimate covariate effects on the incidence, but I'm not sure if that also means the survival function associated with it is interpretable as a normal survival function.
And then, is the survival function associated with the cause-specific hazard not interpretable as the probability of failing from the specific cause after the current time?
Comment: I suggest you check out the Fine-Gray section in this vignette: https://cloud.r-project.org/web/packages/survival/vignettes/compete.pdf I'm getting the impression that you can't treat the cumulative sub distribution hazard curves as survival curves since they might add up to more than 1.
Answer: Fine and Gray used the word "subdistribution" to represent the cumulative incidence function of one of two or more competing events. Their method allows separate modeling of each event type in a way that readily allows for evaluating covariate associations with the risk of an event type. That's unlike the combined treatment in cause-specific modeling, in which the hazard of one event type implicitly includes the hazards of all the others.
To do that, however, one needs to continue including individuals who have already experienced another type of event in the risk set for the event in question. They noted the difficulty in interpreting the hazard function $\lambda_1(t)$ for event type $\varepsilon=1$ on page 497:

Clearly, the risk set associated with the hazard $\lambda_1$ is unnatural, as in reality those individuals who have already failed from causes other than $\varepsilon = 1$ prior to time $t$ are not "at risk" at $t$.

That makes is difficult to interpret a "survival curve" derived from a cumulative subdistributional hazard. As Iyar Lin points out in a comment, the R competing risks vignette illustrates that this separate modeling of event types can allow the sum of the probabilities of the individual events to exceed 1, which doesn't happen with combined cause-specific modeling. Austin et al recently explored that limitation in detail. Despite this, they conclude:

the [Fine-Gray] model can still be useful for at least two reasons. First, as noted above, it allows one to determine which variables are associated with increased or decreased risk. Knowledge of this cannot be extracted from cause‐specific hazard models in the presence of competing risks. Second, when the focus is on a single event type, the subdistribution hazard model allows for estimation of subject‐specific estimates of absolute risk. While there may be bias in some of these estimates, these estimates are still preferable to those obtained using a single Cox proportional hazards model that ignores the presence of competing risks.

That's pretty much what Fine and Gray said originally (page 498), that their method

is intended to be a convenient empirical representation for the cumulative risk of a competing risk and should be evaluated on the extent to which it permits the analyst to assess the effect of covariates on the cumulative incidence function...

Jason P. Fine and Robert J. Gray. A Proportional Hazards Model for the Subdistribution of a Competing Risk Journal of the American Statistical Association 94: 496-509, 1999.
Peter C Austin, Ewout W Steyerberg, and Hein Putter. Fine-Gray subdistribution hazard models to simultaneously estimate the absolute risk of different event types: Cumulative total failure probability may exceed 1. Stat Med 40: 4200-4212, 2021.
Comment: Thanks for the answer; besides the subdistribution model, I seem to remember reading something about the cause-specific hazard not even having a survival function that is estimable but maybe it actually said it wasn't interpretable. Do you know about this? So for the subdistribution hazard you can get the (biased) survival function by $exp{-\int_{0}^{x} h_k(t) dt}$ -- does trying this with the cause-specific hazard not get you anything that is useful?
Comment: @user352188 a survival curve for one event type might be interpretable in a combined cause-specific model, but the associations of covariates with specific event types are then difficult to interpret. In such a model, a change in a covariate might increase the apparent risk of one event type primarily because it decreases the risk of a competing event, while having no direct influence on the first event type of interest. The Fine-Gray model more directly provides specific associations between event types and covariates, but with the drawbacks noted in the answer.


How to write a generator that returns ALL-BUT-LAST items in the iterable in Python?

Question: I asked some similar questions [1, 2] yesterday and got great answers, but I am not yet technically skilled enough to write a generator of such sophistication myself.
How could I write a generator that would raise StopIteration if it's the last item, instead of yielding it?
I am thinking I should somehow ask two values at a time, and see if the 2nd value is StopIteration. If it is, then instead of yielding the first value, I should raise this StopIteration. But somehow I should also remember the 2nd value that I asked if it wasn't StopIteration.
I don't know how to write it myself. Please help.
For example, if the iterable is [1, 2, 3], then the generator should return 1 and 2.
Thanks, Boda Cydo.
[1] How do I modify a generator in Python?
[2] How to determine if the value is ONE-BUT-LAST in a Python generator?
Answer: This should do the trick:
def allbutlast(iterable):
    it = iter(iterable)
    current = it.next()
    for i in it:
        yield current
        current = i

>>> list(allbutlast([1,2,3]))
[1, 2]

This will iterate through the entire list, and return the previous item so the last item is never returned.
Note that calling the above on both [] and [1] will return an empty list.
Answer: First off, is a generator really needed? This sounds like the perfect job for Python's slices syntax:
result = my_range[ : -1]

I.e.: take a range form the first item to the one before the last.
Comment: Thanks for the friendly response, but a generator is absolutely needed, not a slice.
Answer: The more_itertools project has a tool that emulates itertools.islice with support for negative indices:
import more_itertools as mit

list(mit.islice_extended([1, 2, 3], None, -1))
# [1, 2]

Answer: the itertools module shows a pairwise() method in its recipes. adapting from this recipe, you can get your generator:
from itertools import *

def n_apart(iterable, n):
    a,b = tee(iterable)
    for count in range(n):
        next(b)
    return zip(a,b)

def all_but_n_last(iterable, n):
    return (value for value,dummy in n_apart(iterable, n))

the n_apart() function return pairs of values which are n elements apart in the input iterable, ignoring all pairs . all_but_b_last() returns the first value of all pairs, which incidentally ignores the n last elements of the list.
>>> data = range(10)
>>> list(data)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(n_apart(data,3))
[(0, 3), (1, 4), (2, 5), (3, 6), (4, 7), (5, 8), (6, 9)]
>>> list(all_but_n_last(data,3))
[0, 1, 2, 3, 4, 5, 6]
>>>
>>> list(all_but_n_last(data,1))
[0, 1, 2, 3, 4, 5, 6, 7, 8]

Answer: gen = (x for x in iterable[:-1])

Comment: Two problems: a) not all iterables can be sliced, b) if `iterable` is a large list, `iterable[:-1]` will create a large copy of most of that list, which will then be thrown away in a generator. There's got to be a better way to make this one-liner generator work. (But not `itertools.islice`, which doesn't support negative arguments. My bad.)
Comment: The argument with the large list may be applicable in general, but as the OP wants a generator that will stop at the last-but-one element, the list will presumably be traversed entirely, so that's not too much wasted space.
Comment: Hm, on second thought, it is wasted space. :) The iterable contains all we need, no need to copy it. Thx for the hint, Chris.


O que são métodos Async?

Question: Reparei que a maioria dos métodos em C# tem um método igual mas com um nome async.
O que são esses métodos e como eles funcionam?
Answer: São métodos que podem executar assincronamente, ou seja, quem chamou não precisa esperar por sua execução e ela pode continuar normalmente sem bloquear a aplicação, assim quando o método assíncrono chamado termina ele pode voltar para o ponto em que foi chamado e dar continuidade ao que estava fazendo. Isso é feito com a palavra-chave await que já foi explica em Em C#, para que serve a palavra chave await?. O seu funcionamento é explicado nessa pergunta, ele é basicamente uma máquina de estados que regula a execução entre a linha de execução normal e a assíncrona.
É uma convenção que esses métodos terminem com o sufixo Async.
Também já foi falado sobre assincronicidade em O que são processamentos assíncronos e processamentos síncronos?.
Um exemplo prático da diferença: Qual diferença entre ToListAsync() e ToList()?
Provavelmente vai querer saber sobre Diferença entre Task e Thread e um exemplo prático do seu uso: Quais os prós e contras da implementação Task<List<Object>> sobre List<Object>.
Em operações que a resposta será muito rápida o seu uso não vale a pena, isto é útil quando há alguma espera, algo como 50ms, mas sempre depende do caso, pode ser menos ou mais. Por isso dizer que a maioria dos métodos possuem Async no nome é força de expressão :) Afinal a maioria executa na casa dos micro ou nanossegundos.
Coloquei no GitHub para referência futura.
Comment: Então métodos async seria como se tivesse uma thread só para eles?
Comment: @viniciusafx não exatamente. Nesses *links* eu até falo disto, ele nunca cria *threads*. `Task` pode criar *thread* se necessário. Mas a grosso modo tem um efeito semelhante.
Answer: São métodos assíncronos como o @Maniero mencionou, somente para complementar a explicação e você compreender melhor, muitas vezes utilizamos async para consumir um serviço Httpclient em uma aplicação Winforms ou WPF. Por exemplo:
static async Task<Product> GetProductAsync(string path)
{
    Product product = null;
    HttpResponseMessage response = await client.GetAsync(path);
    if (response.IsSuccessStatusCode)
    {
        product = await response.Content.ReadAsAsync<Product>();
    }
    return product;
}

Este método irá fazer uma requisição do tipo "GET" a um serviço web, para receber esse objeto Product sua função ficaria assim:
private async void GetProduct(){
 string url = "http://localhost:50500/MyController/MyAction/ProductId";
 var product = await GetProductAsync(url);
}

Sempre que você for chamar um método assíncrono o escopo da função deverá ter o async antes do tipo de retorno e a chamada da função deverá ter await antes do nome do método. Basicamente é assim que eu utilizo esses tipos de funções.

O que são métodos Async?


Altering paragraph field within revision using preprocess hook

Question: I'm using a hook_preprocess function (hook_preprocess_entity_print) to alter the content within a paragraph field prior to rendering (replacing some strings based on a regex). This works great when I'm working on the node, but not when I'm trying to do the same on a revision of the node.
This works for the node:
/**
 * Implements hook_preprocess_entity_print().
 */
function mymodule_preprocess_entity_print(&$variables) {
  $route = \Drupal::routeMatch();
  $entity_id = $route->getParameter('entity_id');
  $node = \Drupal::entityTypeManager()->getStorage('node')->load($entity_id);
  $type = $node->getType();
  if ($type === 'product_child') {
    foreach ($node->get('field_submittal_wysiwyg') as $paragraph) {
      $type = $paragraph->entity->getType();
      switch ($type) {
        case 'submittal_2_col':
          $field_submittal_text = $paragraph->entity->field_submittal_text_1->value;
          $field_submittal_text = mymodule_token_replace($field_submittal_text, $variables);
          $paragraph->entity->field_submittal_text_1->value = $field_submittal_text;
          break;
      }
    }
  }
}

When I decided to make these same replacements work when rendering a revision of the node, I tried this first:
/**
 * Implements hook_preprocess_entity_print().
 */
function mymodule_preprocess_entity_print(&$variables) {
  $route = \Drupal::routeMatch();
  $entity_id = $route->getParameter('entity_id');
  $revision_id = $route->getParameter('revision_id');
  $node_revision = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($revision_id);
  $type = $node_revision->getType();
  if ($type === 'product_child') {
    foreach ($node_revision->get('field_submittal_wysiwyg') as $paragraph) {
      $type = $paragraph->entity->getType();
      switch ($type) {
        case 'submittal_2_col':
          $field_submittal_text = $paragraph->entity->field_submittal_text_1->value;
          $field_submittal_text = mymodule_token_replace($field_submittal_text, $variables);
          $paragraph->entity->field_submittal_text_1->value = $field_submittal_text;
          break;
      }
    }

  }
}

I can confirm that I'm getting the correct values for the draft revision in $field_submittal_text. But when I try to set the value of the paragraph, it works only for the published node.
I figured I must not be loading the paragraph revisions referenced within the node revisions. So I then tried to address that:
/**
 * Implements hook_preprocess_entity_print().
 */
function mymodule_preprocess_entity_print(&$variables) {
  $route = \Drupal::routeMatch();
  $entity_id = $route->getParameter('entity_id');
  $revision_id = $route->getParameter('revision_id');
  $node_revision = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($revision_id);
  $type = $node_revision->getType();
  if ($type === 'product_child') {
    $field_submittal_wysiwyg = $node_revision->field_submittal_wysiwyg->referencedEntities();
    foreach ($field_submittal_wysiwyg as $paragraph) {
      $paragraph_type = $paragraph->getType();
      switch ($paragraph_type) {
        case 'submittal_2_col':
          $field_submittal_text = $paragraph->field_submittal_text_1->value;
          $field_submittal_text = mymodule_token_replace($field_submittal_text, $variables);
          $paragraph->field_submittal_text_1->value = $field_submittal_text;
          break;
      }
    }
  }
}

Again, I can confirm that I'm getting the correct values for $field_submittal_text based on the revision that I'm trying to load. But the last line before the break isn't changing what is rendered.
What do I need to do to set the value of the paragraph field for a revision before rendering?
ETA: I'm attempting to replace some product attribute values in several paragraph WYSIWYG fields with values from another field, based on a product_id sent in through the route parameters. So I need access to multiple fields in order to do these replacements.
Comment: Doesn't the `$variables` already contain the entity and by that also the parent entity? That means there's no need to get the node from the route. Getting the parent entity from the `$variables` also means your code would work even when you are not viewing a node on its canonical URL (in views for example).
Comment: It's not surprising that I made it more complicated than it needs to be. For some reason, accessing entities programmatically has confused me. Thanks for the advice; I'll look into stripping it down to changing $variables directly.
Comment: So far, in `$variables`, I'm able to find only the published versions of the paragraph fields I'm seeking to alter. I can't locate the draft version. This is the case regardless of whether I'm viewing the published revision or a draft. This is why I went to the extra step of loading the node_revision.
Comment: (I do wonder if I might be missing the values I'm seeking due to a max_depth for my `dump()` statement.)
Answer: The purpose of a preprocess hook is to prepare the variables printed in the template. Check the {{...}} statements in the Twig template.
Some other options:

Look for a different template that prints the variable you want to change.
Create a custom text filter and add it to the text format.
Add a #post_render callback, see How to alter page content?
Comment: Instead of the futile attempt to change objects out of scope in a preprocess hook, you might consider storing the computed data in a new field. The simplest Drupal way for a computed field is an extra field, if you don't want to define a custom field plugin. See https://www.webomelette.com/creating-pseudo-fields-drupal-8. You can also consider putting the extra field inside the paragraph type, it's easier to access the different fields (read-only!) from there.
Comment: I added a little more context to my question. I'm trying to change some text strings (essentially token strings) in a few paragraph wysiwyg fields with values from another field (product data text field) based on a product_id sent in the route parameters. So I need access to multiple fields at once. All this needs to occur prior to handing over to entity_print to render the PDF. I handled this for the published revision of the node using this preprocess hook; now just trying to make the same thing work for a draft revision.
Comment: I was able to accomplish what I needed with `hook_ENTITY_TYPE_view()`. Thanks!


Unable to scan WiFi network with 2 SSIDs

Question: I am trying to scan WiFi networks and trying to connect to them using my Debian based NanoPi device. It is able to scan all the networks except for one.
This network has two SSIDs and both of them are on 2.4GHz band. They are on the same WiFi Channel.
So my laptop and mobile phone are able to scan these two networks.
But the NanoPi device is not scanning this network.
Is both the network on the same band and wifi channel causing interference with one another? Do I need to change the bands or the channel? Or something needs to be done at the device end? Why is my phone able to scan the network but the nano pi device is unable to scan?
Comment: Do both SSIDs send out beacon frames? If not you need to have active scanning (sending out probe request frames) on your NanoPi.    Which channel are those SSIDs on? Are you able to scan other SSIDs on that channel?
Comment: Yes both the SSIDs send out the beacon frames. But this is strange, today it is scanning the network. But some days it doesn't ??
Comment: One possible explanation could be that the SSIDs frequently change their channel number and your NanoPi is limited in the channels it scans due to the country settings. What channel are the SSIDs on when scanning works and when it doesn't work?
Comment: Yesterday it was able to scan the network and the channels were on Channel 1. Few days ago the channels were on 10, when the device was unable to scan.
Comment: Channel 10 is not subject to country restrictions, so that's not it.
Answer: Your two WiFi networks are definitely interfering with each other, changing the channel will solve this problem.
Since you are using 2.4 GHz band, I recommend you to use channels 1, 6, 11 (wide bandwidth) to avoid interference, though they aren't the very best for all cases, As for the optimal results use this WiFi Analyzer app, it will tell you the best channel to use for both networks.
Comment: This is definitely not the problem here. We are not talking about reduced throughput. OP is not seeing any beacon frames from the APs. Otherwise you would never be able to see more than one device on a single channel anywhere.


Add custom field to taxonomy terms

Question: I am writing a module that creates a vocabulary through code. Here's what I use:
<code>$new_vocab_name = 'my_categories';
$vocab_obj = (object) array(
   'name' => t('My Categories'),
   'machine_name' => $new_vocab_name,
);
taxonomy_vocabulary_save($vocab_obj);
</code>
How do I add custom fields for terms that will be in this vocabulary? E.g., I want to add an image field for each term.
Answer: I think this article will be usefull for You because taxonomy is one of entity types:
Working programmatically with fields in Drupal 7
So You should set "entity_types" as taxonomy.
Comment: Thanks; to be more precise, the **entity_type** must be set to **taxonomy_term**, and in order to get a list of available entities, you can use the function **entity_get_info()**.
Comment: I'm also linking to [this post](http://drupal.stackexchange.com/questions/36966/how-to-programmatically-set-a-custom-field) because I was having some trouble using the custom field and it helped me sort it out.

Add custom field to taxonomy terms


Salvar arquivo IE gerado por JavaScript usando VBA

Question: Caros, estou usando a rotina para acessar uma pagina e baixar um arquivo, o qual usa um javascript para ser gerado, porem preciso que este arquivo seja salvo automaticamente, sem intervenção do usuario.
Sub ConnectWeb()
Dim ie As InternetExplorer
Dim C
Dim ULogin As Boolean, ieForm
Dim MyPass As String, MyLogin As String
Dim Linha As Integer
Dim PN As String
MyLogin = Application.InputBox("Por Favor entre com o Login", "Empresa", Default:="User", Type:=2)
MyPass = Application.InputBox("Por favor entre com a senha", "Empresa", Default:="Password", Type:=2)

Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "http://url"
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop
ie.Document.all("cuser").innerText = MyLogin
ie.Document.all("cpass").innerText = MyPass
ie.Document.getElementById("cent").Value = "BR"

ie.Document.forms(0).submit
Do While ie.Busy
   DoEvents
Loop
PN = "D515005-5304"
'JavaScript to create file
ie.Document.parentWindow.execScript ("printPL('" & PN &         "','N%2FC','no')")

End Sub

Apos o JavaScript aparece aquela mensagem do SaveAs e precisava que esta opcao fosse "Clicada" automaticamente, ou melhor ainda, que fosse salva em um local especifico.
Answer: Caros, optei por utilizar o chrome e descobri que o javascript envia informacoes via url, sendo assim usei a seguinte logica:
Sub teste()
Dim chromePath, PN As String
Dim Linha As Integer
chromePath = """C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"""
Linha = 2
Cells(Linha, "A").Select
Do While ActiveCell <> ""
    PN = Cells(Linha, "A").Value
    strURL = "http://cdroyal4.cdzodiac.com/cgi-bin/wspd_cgi.sh/WService=wslive/wpt/function.r?funct=print.pl&drawing-no=" & PN & "&prelim=no&rev=N%2FC&filename=" & PN
    Shell (chromePath & strURL)
    Linha = Linha + 1
    Cells(Linha, "A").Select
Loop

MsgBox "Complete", vbInformation, "Empresa"
End Sub

Com o Chrome o download é feito automaticamente, resultando no resultado esperado.
Obrigado a todos que analisaram.


Fetch Link field Label and Link separately in views-view-fields template

Question: How can I fetch label and link field in views-view-fields--VIEW-NAME-HERE.html.twig file
So far I have tried
<code>{{ fields.field_action_link.content }} give me the anchor tag prebuilt (html)
{{ fields.field_action_link.0.url }}
{{ content.field_action_link[0]['#url'] }}
</code>
but none of them seems to work for me. I am able to retrieve all other fields including image field.
Comment: Are you tried twig debug option.
Answer:
Just edit the Link field and select these two options (highlighted in screenshot)


Homotopy between two closed curves in the punctured plane.

Question: I'm looking at the following lemma.

Suppose $\gamma_{0}, \gamma_{1}: \mathbb{S}^{1} \rightarrow \mathbb{R}^{2} \backslash\{0\}$ are two
closed curves in the punctured plane. If
$$
\left\|\gamma_{0}(z)-\gamma_{1}(z)\right\| \leq\left\|\gamma_{0}(z)\right\|
$$
for all $z \in \mathbb{S}^{1}$, then $\gamma_{0} \simeq \gamma_{1}$ in $\mathbb{R}^{2} \backslash\{0\} .$

The lecturer provides the following analogy to illustrate the idea behind the lemma.

Suppose you are walking your dog and you pass near a tree. To ensure that you don't get the leash tangled around the tree, it suffices to adjust its length so that, at any instant, it is shorter than your distance to the tree. Mathematically, we prove this "dog-on-a-leash" lemma simply by considering a linear homotopy.

I can see how the two curves $\gamma_0$ and $\gamma_1$ satisfying the condition in the statement of the lemma would forbid $\gamma_1$ to "wrap around" the missing point. But beyond that I don't see how the analogy translates to a linear homotopy. Some more elaboration on the idea and the proof would be appreciated.
Comment: Consider the line through $\gamma_0(t)$ and $\gamma_1(t)$, and shrink one down to the other along that line.  Do that for each $t$.
Answer: When you hear or read "linear homotopy" your mind should automatically and immediately go to $H:S^1\times[0,1]\to \Bbb R^2$ given by
$$
H(z,t)=(1-t)\gamma_1(z)+t\gamma_0(z)
$$
because that's what "linear homotopy" means. It is often the simplest homotopy between two maps, and therefore also often the first thing to try when you want a homotopy between two maps. At least as long as the codomain of the two maps is a vector space or a subset of a vector space.
And indeed, for any fixed $z$, the given requirement on $|\gamma_1(z)-\gamma_0(z)|$ means that $H(z,t)\neq0$ for all $t\in[0,1]$, and we do have a valid homotopy.
PS. This exact argument lies at the heart of my personal favourite proof of the fundamental theorem of algebra: for any degree-$n$ (monic) polynomial $p$, if $S^1$ is a large enough circle around the origin, and $\gamma_1(z)=p(z)$ and $\gamma_0(z)=z^n$, the two therefore go around the origin the same number of times. And unless $0$ is a root of $p$, a small enough circle around the origin is mapped by $p$ to a circle that doesn't go around the origin. So some circle in-between needs to cross the origin, and $p$ has a root.
Comment: @ensbana Yes, the point of the condition is to make sure that the straight line segment between $\gamma_0(z)$ and $\gamma_1(z)$ avoids the origin, and then use the homotopy $H$ to transform one to the other along that line segment.
Comment: I see. So the point of the condition is to make sure the image of the homotopy is never $0$? And as soon as we have that, all curves can be transformed into another?


Fail2ban compatibility debian debian jessie systemd

Question: Fail2ban debian jessie package not working:
On fresh debian jessie with openssh-server (tested in docker container):
root@2b29327677c8:/# cat /etc/debian_version
8.3
root@2b29327677c8:/# apt-get install fail2ban
root@2b29327677c8:/# apt-cache policy fail2ban
fail2ban:
  Installed: 0.8.13-1
  Candidate: 0.8.13-1
  Version table:
 *** 0.8.13-1 0
        500 http://httpredir.debian.org/debian/ jessie/main amd64 Packages
        100 /var/lib/dpkg/status

root@2b29327677c8:/# service fail2ban start
Starting authentication failure monitor: fail2banERROR  No file(s) found for glob /var/log/auth.log
ERROR  Failed during configuration: Have not found any log file for ssh jail
 failed!

Debian jessie do not have /var/log/auth.log
How to use fail2ban on debian jessie to watch ssh connections ?
Comment: @ansi_lumen You're diggin up a big debate ^^
Comment: http://bugs.debian.org/
Comment: @MichaelHampton https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=770171
Answer: It appears that fail2ban version 0.9.0 introduced a systemd journal backend:

systemd journal backend has been created. This has a dependency on
  python-systemd. To use journal match in filters there is a
  "journalmatch".

However, Debian 8 (jessie) has fail2ban version 0.8.13 and the changelog as of package version 0.8.13-1 has no indication that this functionality would have been been backported either.
This seems like a bit of an oversight considering the use of systemd, but would explain your problems. (You can obviously still monitor anything that is written to plain log files.)
Comment: You're correct! I can install >=0.9.0 fail2ban package with
http://neuro.debian.net/ repo. But my question include how to configure
fail2ban in this case. (i tried to configure it with backend=systemd and
some other config without success)


Looking for an elegant proof of $\det(A) = \det(A^t)$ without Schur decomposition

Question: Looking for an elegant proof of $\det(\textbf{A}) = \det(\textbf{A}^{t})$ without Schur decomposition.
Proof 1 with Schur decomposition
$$\textbf{A} = \textbf{P}^{t}\Delta\textbf{P} \implies\textbf{A}^{t} = (\textbf{P}^{t}\Delta\textbf{P})^{t} = \textbf{P}^{t}\Delta^{t}\textbf{P}$$
So, $\textbf{P}$  is unitary matrix, $\textbf{P}^{t}=\textbf{P}^{-1}$.
$$\det(\textbf{P})=\det(\textbf{P}^{t})= \det(\textbf{P}^{-1})$$
 $\Delta$ is upper triangular matrix.
$$\det(\Delta)=\det(\Delta^{t}) \implies   \det(\textbf{A})=\det(\textbf{A}^{t})$$
Comment: It's not really a proof, but you can say "Leibniz definition of determinant arbitrarily chose between rows and columns".
Comment: @DanielV That can be very easily transformed into a proof. Another proof is that this is trivial for diagonalizable matrices, and these are dense in the set of matrices. Another proof is that every matrix is similar to its transpose. etc. etc.
Answer: Here's one proof, not sure if it's elegant or not.
Clearly $A$ is invertible if and only if $A^\mathrm{T}$ is, so we limit our proof to invertible matrices. Invertible matrices are products of elementary matrices and the determinants of elementary matrices are easily verified to be invariant under transpose. Hence the result follows.
Answer: $\lambda$ is an eigenvalue of $A$ with $v$ as its non-zero eigenvector then
$$
(A-\lambda I)v=0\implies v^T(A-\lambda I)^T=0\implies \det(A^T-\lambda I)=0
$$
Therefore the eigenvalues of $A$ and $A^T$ are the same and they have the same geometric multiplicity. For those class of matrices with the same algebraic and geometric multiplicities, we can say that  the eigenvalues of $A$ and $A^T$ are the same in multiplicity too.
Again independently we can show that the determinant of $A$ is the product of its eigenvalues and hence:
$$
\det A=\prod_{i=1}^n\lambda_i=\det A^T.
$$
So we use the fact that $A$ and $A^T$ have the same eigenvalues to prove the result. Note that one should not use $\det A=\det A^T$ to prove that $A$ and $A^T$ have the same eigenvalues to avoid circularity.
Comment: Your first argument shows that the set of eigenvalues of the transpose is the same as the original matrix, but I'm not sure that it shows that the eigenvalues are equal up to multiplicity.
Comment: Thanks for the comment. You are right, it does not show that they have the same algebraic multiplicity but the same geometric multiplicity. So the proof works only for those matrices with the same geometric and algebraic multiplicities.
Answer: I'm not sure this is quite what you're looking for, but:
For any map $f:V \to V$ with $n = \dim V$ finite, the induced map $\bar{f}:\bigwedge V^n \to \bigwedge V^n$ is a map on a one-dimensional space, and thus must be given by $\overline{f}(x) = \alpha x$ for some constant $\alpha$ (independent of a choice of basis); this constant $\alpha$ is by definition $\det f$. With the notation above, we have $\overline{A^t} = (\overline{A})^t$. It's then easy to verify directly that $\det(A^t) = \det(A)$.
Answer: Every square matrix $A$ has an LDU-factorization. That is if $A$ is a square matrix there is a lower-triangular matrix $L$, a diagonal matrix $D$, and an upper-triangular matrix $U$ such that $A=LDU$ and both $L$ and $U$ have 1s on their diagonals.
From this factorization, it is clear that $$\det(A)=\det(LDU)=\det L\cdot\det D\cdot\det U=\det U^T\cdot\det D^T\cdot\det L^T=\det(U^TD^TL^T)=\det(A^T)$$
The existence of this factorization can be derived in a way similar to how one finds the inverse of a matrix. Given $A$ construct the matrix $[A|I]$ and perform row reduction to make $A$ into an upper diagonal matrix. The resulting matrix on the right is the inverse of the matrix you want in the factorization. To get the upper diagonal matrix do the same thing with the row reduced matrix $A'$ on top of the identity matrix and perform column reduction. The resulting matrix on the bottom will be the inverse of matrix you want in the factorization.


After deployment the permissions are not the one i wanted

Question: I am using capistrano with git to deploy a RoR project. After deployment, the permissions in the current folder are 770 instead of 755, and it cause apache to display a forbidden page. My original folder where i work is using 755 as permissions. In my deploy.rb there is nothing about files permissions.
Why does capistrano change the permissions and how can i have correct permissions when deploying on my apache?
Answer: I don't know about Capistrano, but git itself does not store any permissions, apart from the "executable" bit. You are supposed to achieve the expected permissions by setting an appropiate umask in your shell.
Comment: The only umask i founded does not use 770. I think git does not keep files ownership, but the permissions are stored, if i change a file permission, the change will appear in the next commit.
Comment: You where right, git only use 755 or 644. Anyway all my users have 0007 as umask but if i do a touch it create a 330 file (go figure). As git only store 644/755 capistrano must change the permission to 770 at some point.
Comment: @Sylario: I don't think you understand how a umask works - it's a mask. When you attempt to create a new file with given permissions, the result is the attempted permissions which are *not* in the mask. The umask that you want is `0022`, which will get you `755` for directories and executable files, and `644` for non-executable files.
Comment: i set umask 0022 in /etc/profile and ~/.bashrc but it still create 770 folders...
Comment: I changed umask and it works fine now. Umask may be in profile or in bashrc depending the User.
Comment: I think changing umask is a bad idea. Setting the correct permissions at the end of deployment is better IMO.
Answer: See this blog post with the solution: http://konradpodgorski.com/blog/2012/07/26/capifony-fix-for-deployment-on-servers-with-strict-permission-settings/
Permission changes like that is usually caused by the default umask setting which is usually fine for normal usage. However some hosts require specific permissions (usually 755 for directories and 644 for files). This is mainly because of security reasons. The best thing you can do is to make this part of the deployment and set correct permissions.


Rails JSON API: limit has_many relationship to current_user

Question: I have a rails json API with 2 models (Bags and Items). I am using active record serializers with MySQL to organize the json output. I want to show all Bags for all users, but only Items for the current user.
Looking for something like this:
class Bag < ActiveRecord::Base
  has_many :items,-> {where current_user: "#{current_user.id}"},class_name: 'Items'
end

I don't know of a way to pass the current_user.id property to the model. Hoping someone knows a way.
Answer: Two things:

You can't access controller and helper methods directly in your models. You need to pass them to your model. An easy way of doing it would be to add a method or a scope.
Association query customization does not support argument passing to your block (-> {where current_user: ...). So, you can't do it in association declaration.

So, I guess you would have to add a scope:
class Bag < ActiveRecord::Base
  has_many :items
  scope :user_items, ->(user) { where(items: {current_user: user}) }
end

PS. You don't need to convert "#{current_user.id}" to a string.
Comment: Yea, left the "#{curent_user.id}" in for example purposes. Just didn't change it for this example. But where is the "->(user)" argument getting passed from? The controller?
Comment: Doesn't matter. You can call it anywhere and pass an instance of your user: `Bag.user_items(current_user)`.


ReactJs set background image

Question: Having trouble figuring out how to set a bg image correctly.
This is what i've tried.
backgroundImage: 'url(./static/images/auth_bg.jpg)'

import Background from './static/images/auth_bg.jpg';

backgroundImage: 'url(${Background})'

Styles
const useStyles = makeStyles((theme) => ({
 bg: {
        backgroundImage: 'url(${Background})'
    }
}));

const classes = useStyles();

What im getting back

Module not found: Can't resolve './static/images/auth_bg.jpg' in
  '/home/h1dd3n/Desktop/Folder/social_blog/blog_react/src/authentication/components'

Beside this it's saying that Background is unused import. Next line complains that it can't reslove directory static
Comment: well Background is unused since you need to use backticks (`) to render a variable inside a string and you are using single quote
Comment: @buzatto changed it to backsticks, still have the same error. And instead of giving the div a `className, i've tried giving it a styles property. Still same `unresolved` error
Answer: Anyway, i totally missed out the point that my current component was out or scope.
This fixed it ../../static/images/auth_bg.jpg'

ReactJs set background image


Typescript: How to set value to empty string

Question: I'm doing sorting and grouping the data based on first word that split with _ (underscore). So result should return 3 groups (eg: orders, items and ""). How I can set some value (eg: Others) to that empty string. Here is stackblitz
let rawData = [
  { name : 'orders_list', id : 10},
  { name : 'orders_price', id : 7},
  { name : 'items_list', id : 12 },
  { name : 'items_price', id : 7},
  { name : 'others-list', id : 7},
  { name : 'secondOthers-list', id : 7},
]

let data = rawData.reduce((r, e) => {

  let group = e.name.substr(0, e.name.indexOf('_'));

  if(!r[group]) {
    r[group] = {group, children: [e]}
  } else {
    r[group].children.push(e);
  }

  return r;
}, {})

let result = Object.values(data)

console.log(result)

Comment: your stackblitz is working fine
Comment: Yes correct but third item in array returning empty string. How I can replace empty string with "Others" value ?
Comment: write a unit test with your expected output.
Answer: here is a working stackblitz I made :)
// Import stylesheets
import './style.css';

// Write TypeScript code!
const appDiv: HTMLElement = document.getElementById('app');
appDiv.innerHTML = `<h1>TypeScript Starter</h1>`;

let rawData = [
  { name : 'orders_list', id : 10},
  { name : 'orders_price', id : 7},
  { name : 'items_list', id : 12 },
  { name : 'items_price', id : 7},
  { name : 'others-list', id : 7},
  { name : 'secondOthers-list', id : 7},
]

let data = rawData.reduce((r, e) => {

  let group = e.name.substr(0, e.name.indexOf('_'));

  if(!r[group]) {
    r[group] = {group, children: [e]}
  } else {
    r[group].children.push(e);
  }

  return r;
}, {})

// since data at this point is an object, to get array of values
// we use Object.values method
let result = Object.values(data)
var resultMap = result.map(_val => {
  return _val.group
})
var index = resultMap.indexOf('')
result[index].group = 'Others'

console.log(result)

https://stackblitz.com/edit/typescript-6xfk3j
Answer: Check this.
// Import stylesheets
import './style.css';

// Write TypeScript code!
const appDiv: HTMLElement = document.getElementById('app');
appDiv.innerHTML = `<h1>TypeScript Starter</h1>`;

let rawData = [
  { name : 'orders_list', id : 10},
  { name : 'orders_price', id : 7},
  { name : 'items_list', id : 12 },
  { name : 'items_price', id : 7},
  { name : 'others-list', id : 7},
  { name : 'secondOthers-list', id : 7},
]

let data = rawData.reduce((r, e) => {

  let group = e.name.substr(0, e.name.indexOf('_'));

  if(!r[group]) {
    r[group] = {group, children: [e]}
  } else {
    r[group].children.push(e);
  }

  return r;
}, {})

// since data at this point is an object, to get array of values
// we use Object.values method
let result = Object.values(data)
result.filter(f=>{
  if(f.group==''){
    f.group='Others'
  }
});

console.log(result)

StackBlitz : https://stackblitz.com/edit/typescript-pe8k9p


Fetch data with SSR

Question: I have a page which has query parameters and my data is fetching using SSR. Whenever my query parameters change (and it changes quite fast), data is fetched again with SSR (no client side data fetching).
Is it okay to fetch data like that OR should I combine it with client side data fetching ( I mean first time I pre-render and fetch data with SSR and after that I should user client side data fetching). If second option, please, can you provide some resources or answers how I can do that.
Comment: Hey @Andrii, what do you mean by your query parameters changing fast? Do I understand it correctly that your page stays the same but just the query params change?
Comment: @Skocdopole yep you're right, my query parameters are changing every time user changes input and then they are used to search for data. The page stays the same. But this page is needed to be indexed.
Answer: When it comes to rendering a page and then updating its content often, let's say upon user interaction, SSR is best used only on the initial navigation, and the subsequent content fetches are done client-side.
On the first glance it's logical. SSR's primary goal is to make the first interaction with a new page seamless, be it for bots or users - it presents complete markup to crawlers and makes the page load faster (or makes it look faster) to users. But if you would use SSR for the subsequent updates as well, it would be a waste of bytes by sending the full HTML over the wire and letting the server generate the markup all the time and would result in a worse UX.
So to combine both approaches, you can do something like:
export default class Page extends React.Component<Props> {
  static async getInitialProps({ query }: NextPageContext) {
    const data = await fetchYourData(query.yourParam);
    return { data };
  }

  render() {
    const { data } = this.props;
    return Your data: {data};
  }
}

The key is using getInitialProps. That method will first run server-side and on subsequent navigations to the same route, but with different search params, will run client-side.

Of course there are few caveats. First of all, do you even need SSR? Often, pages heavily customized by search params are not indexed (imagine product search page), which eliminates one of the big arguments for SSR.
PS: This is not relevant to your example in next.js, but I'll put it out there anyway, you can theoretically perform SSR only for the part of the page that needs to be updated. Remix.js is capable of doing just that by sending the partial markup together with some metadata over HTTP which then gets applied just to the relevant section of the page.
Comment: Thank you, I found your answer useful, so as I got fetch data with only SSR will take more time and resources than client side data fetching, yep?
Comment: There isn't a simple answer. If we take the remix.js example with only partial SSR update, I can easily imagine a scenario where it is faster than client-side data fetch with UI update (eg. for slower mobile phones). You'd have to compare and experiment a bit.

But if we are talking SSR that next.js offers - always sending the full HTML over the wire when page is requested - it probably returns much larger response than a simple JSON data fetch, and client-side fetch is probably easier to implement here as well.

Fetch data with SSR


How do I let end users create and manage arbitrary connections between their user account and an entity

Question: I have an event content type and I want to allow users to 'add' themselves to it the same as Facebook users would join an event. Any ideas?
Comment: IMHO, using entityreference would not be the right way to achieve this. It can be achieved by using the flag module and add a flag to the content type so that user's could subscribe / add themselves to the project.
Comment: Having written an answer doesn't preclude me from having completely misunderstood the question ;) Though my understanding is just that the question is 'How do I let end users create and manage arbitrary connections between their user account and an entity". I would love for the question to be re-phrased like that, but it seems like to big an edit for me to make on behalf of the original poster.
Comment: I'm not sure what do you mean by "add user to a content type". Could you explain a bit more? Apparently Letharion understood you all right, so I accept it might be problem at my side, but if you could be so nice anyway?
Comment: Add a user in the same way that you can add a user to a facebook event for example. A collection of users are referenced in some way on the node. And these users when viewing the node, can instigate this reference.
Comment: Please do make the edit :)
Answer: With Entity reference, this isn't very easy, because references are fields, and the permission management you need here is hard to do on a field level. You could still do it, but it would require a significant amount of custom work.
Consider using Relation or Flag instead.
You'll most likely get a better "out of the box experience" with the Flag module, but you will get better flexibility with the Relation module.
I've tried to convince one of Flag maintainers that the Flag is a special case of the Relation module and so should be based on it, but he didn't want to add a dependency.
Comment: Yes, using flag is the best approach in this case of associating a user with a node on the fly, otherwise if we try to have the entity reference while viewing the node we need to achieve it through code.
Comment: @djs Nothing prevents you from invoking the flag functions in your own code if you need both scenarios.
Comment: I think you underestimate the power of Flaggings. Flagging doesn't only mean to create attention for it. Flagging are entities and they are also fieldable, that means you can also add a form to a flag. And you can get the list of users who has flagged a node with views. You can also use rules to trigger actions on events, for example send email to all users who has flagged the node before one week of the event date etc. You can do so many powerful things with Flags. Just don't underestimate :)
Comment: You can also use rules so when a user flags a node they are appended to the list in the entity reference field
Comment: Entirely true, but I don't quite like the solution because it's not very flexible. It breaks as soon as one wants to create a reference between and two entities that doesn't include the user itself because then there's no longer any owner tracking, and so the connection cannot be managed once created. Also I find rules to in theory be a cool module, but in practice have a hard to work with UI.
Comment: 'Flagging' a node implies flagging it to create attention for it, but I am looking for a solution to associate a list of users with a node, as you might associate a list of users with a Facebook event.
Comment: I think that relation is too complex and more of an admin creatino of the relationship rather than something and end user can create easily.
Answer: I believe you are looking for the Organic Groups module (https://drupal.org/project/OG).
This module allows you set your current content types to be 'subscribed' to. Thus users can join a group, or event in your case.
The group can have administrators where they can manage and invite users, such as setting to them active, pending or blocked.
We recently created a Drupal module that would allow a group members to invite other users, even if they were registered or new.
Organic Groups will also allow you your members to 'post' content INTO the group. E.g. you could post pictures relating to that event.
It's quite easy to setup and many tutorials around.
Hope this helps,
Amarjit
Comment: I was talking about this yesterday. I will look into it.Have heard good and bad htings about the OG modules.- e.g. difficulty of configuration, but with some investigation should be able to figure it out.
Comment: My fear is that OG is too complex for what I want to do at the moment, and as far as I can see it uses entity reference for its associations. However, it may also solve one of the other problems I have which is how a user can assign other users to edit their content. Can you define what 'group' means here. Is it a group of users or a group of content or a group as in a 'real world group' like a classic car collectors club
Comment: A group is a collection of entities. User can belong to a group, along with content belonging to a group. At the center of this group, could be say a particular Page. This page may list information about this particular group.

Think of it as the Event being a group and users being able to join that group. As well as this, users can post content to these groups.
Thus every Event can have its own bunch of users.
Comment: Hi, is it easily possible to rename group to event across all instances of the 'group' naming?
Comment: I believe this is not possible, unless you are creating custom displays and calling the OG functions. However, there is some work being done to get this functionality.

If you are familiar with Drupal modules and such, you can follow this thread on Drupal, [Allow the ability to rename "groups" / assign labels to groups](https://drupal.org/node/252836).
Answer: The Entity Registration module should be a solution.

Entity Registration is a simple, flexible module for allowing and
  tracking user registrations for events, or just about anything you
  want people to sign up for. Entity Registration can be integrated with
  Drupal Commerce to allow fee-based registrations: sell tickets to your
  stuff! This module is also handy if you want to collect information
  along with registrations: like shoe-size for a bowling event.


GIT: unapply a stash - too many revisions specified

Question: I'm sorry if the solution is trivial, but I'm not a GIT expert and I didn't find anything on the internet.
I stashed some changes, and I'm having hard times at reverting them. Maybe I sent the git stash command twice, I hope this didn't make me lose everything.
According to this question and official documentation, I've tried the following commands:
git stash pop

And I received:

src/schema/schema-generator2.js: needs merge
unable to refresh index

So I tried:
git merge

error: Merging is not possible because you have unmerged files

Then I tried:
git stash apply stash@{0}

unknown option: -encodedCommand

git stash apply

src/schema/schema-generator2.js: needs merge

git stash apply --index

src/schema/schema-generator2.js: needs merge

git stash show -p stash@{0} | git apply -R

git : Too many revisions specified: 'stash@' 'MAA=' 'xml' 'xml'
In riga:1 car:1
+ git stash show -p stash@{0} | git apply -R
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Too many revisi...A=' 'xml' 'xml':String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
error: unrecognized input

In the above text I wrote as code the message, and as quote the answer. I hope it's clear.
I don't understand what's happening, why nothing works? What can I do to un-stash my changes?
Comment: You forgot to mention that you using PowerShell as shell. And `{` is special character for PowerShell in this example.
Comment: can you post the output of `git status` command
Comment: @PetSerAl you are right, I'm using VS Code and many errors stop appearing when I use terminal.
Comment: By the way, I solved the problem, even if I can't explain how. Probably it was just a problem with VS Code and restarting it was enough
Answer: Looks like the problem is the stashed changes and latest HEAD of your branch are not automatically mergable, specifically in src/schema/schema-generator2.js file
This means you will have to manually resolve the merge conflicts
The best way to do this would be to convert the stashed changed into a patch file and try applying the patch file. You will still see the merge conflicts but will have better control of resolving the changes
$ git stash show -p --color=never > my-stashed-changes.patch
# This should show the same merge error
$ git apply my-stashed-changes.patch
# modify the patch file to resolve conflicts and apply again


Restart an application in CloudHub using rest API and ClientID/Client secret

Question: I am trying to stop/restart a CloudHub Mule application using the rest API endpoint.
I am following this link: https://help.mulesoft.com/s/article/How-to-start-stop-or-restart-a-CloudHub-Application-using-CloudHub-REST-APIs
I am trying to get a Bearer token as per this link (OAuth using a Client ID and secret): https://anypoint.mulesoft.com/exchange/portals/anypoint-platform/f1e97bc6-315a-4490-82a7-23abe036327a.anypoint-platform/access-management-api/minor/1.0/pages/Authentication/
But we have Organizational level client id and secret,
Environment level client id and secret,
and Application level client id and secret.
Which client id/secret is needed to restart the application? I tried all 3 but had no luck.
NOTE: We are using Single sign-on using Microsoft 360
The link says to pass EnvironmentID in the header, but is the key name "EnvironmentID" or different?
Answer: Short answer: the recommended method is to use a Connected App. See below to find details.
Long answer:
The KB article that you share has a link on how to get the bearer token. It explicitly says that you can not use client id/client secret for CloudHub.
To get a bearer token from your user the detailed steps are detailed in the link. In a simple case you can get the token by requesting a POST to the https://anypoint.mulesoft.com/accounts/login endpoint.
Example:
<code>curl -H "Content-Type: application/json" -X POST -d '{"username":"<USERNAME>","password":"<PASSWORD>' https://anypoint.mulesoft.com/accounts/login
</code>
This is not going to work if your organization has configured single sign on with a corporate Identity Provider. Find details in the article on how to get the token.
What you probably really need is to create a Connected App in Access Management for what you need to do, assign the permissions needed for CloudHub to it, and then use the client id and client secret generated for the Connected App.
The environment id is a unique identifier for the environment where the application is deployed, similar to how the org id identifies an organization. Again the KB article list the link to another article on how to find it.
Comment: Edited the answer to clarify that Connected App is the recommended method.
Comment: While you can work around that, Connected Apps is the recommended method for automations, like CI/CD pipelines, since they are not tied up to an user.
Comment: In general you should NEVER use the organisational or environment secrets in your code. Security vulnerabilities can arise any time which can turn just a simple "restarting of application" to a fatal mistake for the organisation
Comment: Thanks for the response, since we have "single sign-on" setup, I guess the "Connected App" is the solution.

Thanks again


I want google maps API autocomplete to search towns in a specific city only

Question: I am trying to get the only Town Names in an input field using google maps API. The following is the image of what I am working on.
Screenshot of my input fields
So, I have entered all my cities in the first select field and I have used google maps autocomplete API on the second input field. I have used country restriction on it and country restriction is working fine as I want.
But now the point is that I want it to autocomplete only town names in input field from the selected city only, so please provide some help in this respect.
Following is the code that I am using in this respect.


Xamarin Forms Nested flex layout with other controls not rendering properly

Question: I am trying to render a button inside nested FlexLayout, it's not getting resized automatically.
<ContentPage.Content>
    <StackLayout>
        <FlexLayout
            AlignItems="Center"
            Direction="Row">
            <StackLayout>
                <FlexLayout BackgroundColor="LightBlue" Direction="Column">

                </FlexLayout>
            </StackLayout>
            <Button BackgroundColor="Green" Text="Submit" />
        </FlexLayout>
    </StackLayout>
</ContentPage.Content>

Which property to modify to render the green button as shown properly. I don't want to give any specific Width and Height it should automatically adjust according to text of button.
Comment: Why are you wrapping the FlexLayouts in StackLayout?
Answer: Try to change FlexLayout.Grow="1" to your FlexLayout and remove the StackLayout like below:
<ContentPage.Content>
    <StackLayout>
        <FlexLayout
        AlignItems="Center"
        Direction="Row">
            <FlexLayout  FlexLayout.Grow="1" BackgroundColor="LightBlue" Direction="Column" >

                </FlexLayout>

            <Button  BackgroundColor="Green" Text="Submit" />
        </FlexLayout>
    </StackLayout>
</ContentPage.Content>

Comment: Is there any way to auto adjust height, if I don't keep parent flex-layout inside stack layout it takes complete screen height.


Verificar palavras em Array php

Question: Estou montando um verificador de texto, onde eu armazeno em array1 diversos textos (não consegui filtrar eles) e no array2 eu coloco determinadas palavras para serem comparadas. Assim, deve-se verificar se as palavras que estão no array2, se encontram no array1, para poder se determinar do que se trata o texto.
<?PHP
$array1 = explode("", file_get_contents('cotidiano.txt'));
$array2 = explode("", file_get_contents('palavras.txt'));
?>

Texto exemplo:
O texto vem da seguinte maneira:
TEXTO1
 <edic><Diário Gaúcho 12/05/2013</edic>
<autor>Denise Waskow, email@example.com</autor>

<subtítulo>Para celebrar</subtítulo>
<título>Mães e filhos se divertem em atividades culturais no Parque da Redenção</título>
Jogos de mesa, futebol, brinquedos infláveis, teatro, dança e uma série de outras brincadeiras movimentaram o domingo no parque
Criançada aproveitou o domingo nos brinquedos
Denise Waskow
email@example.com
Se existe algo que deixa toda mãe realizada é ver que o filho está feliz. E quem levou a prole neste domingo à Redenção ganhou esse presente para celebrar o Dia das Mães.
Jogos de mesa, futebol, brinquedos infláveis, teatro, dança e uma série de outras brincadeiras movimentaram o domingo, em uma ação promovida pela Secretaria Municipal de Esporte, Recreação e Lazer da Capital. A gurizada aproveitou muito, e as mamães se emocionaram com a alegria deles.
<subtítulo>Diversão nos brinquedos</subtítulo>
Enquanto a pequena Amanda, três anos, criava coragem para descer no tobogã inflável, a mãe, a babá Roselaine Pereira, 30 anos, ficava na parte de baixo do brinquedo, incentivando a menina a descer, fotografando e se divertindo junto.
- É maravilhoso, é ótimo. O melhor presente é isso, a felicidade deles - derrete-se.
O filho mais velho, Jhonattan, 13 anos, já não está mais na fase de brincar, mas nem por isso deixou de acompanhar a mãe. Enquanto a irmã caçula passava de um brinquedo para o outro, ele contou como homenageou Roselaine no início do domingo:
- Eu disse que eu amo ela.
<subtítulo>Uma torcida especial</subtítulo>
"Vai Joana, vai Joana!". Ao lado do espaço demarcado para o futebol, a massoterapeuta Tânia Regina Horta, 46 anos, incentivava a filha caçula, de seis anos, no esporte. E apesar de a mãe ser gremista e a filha colorada, não existe rivalidade. O que conta nessa relação, claro, é o amor.
- A mãe que está sendo homenageada e são os filhos que se divertem. Está ótimo, o melhor é curtir a companhia deles - elogia.
Depois de uma manhã inteira de brincadeiras, o planejamento de Tânia era encontrar os dois filhos mais velhos, além dos dois enteados, para o tradicional almoço de Dia das Mães.

Minha dúvida seria : Como realizar a verificação de dados em diferentes arrays (ou o que for necessário implementar) para se determinar do que se trata o texto?! Não conheço muito da linguagem e por isso fiquei em dúvida.
Answer: Você pode verificar se a palavra existe no Array:
$texto1 = preg_replace("<(.|\n)*?>","",file_get_contents('cotidiano.txt'));
$array1 = explode("",$texto1);
$array2 = explode("", file_get_contents('palavras.txt'));

foreach($array2 as $valor){
   if(in_array($valor, $array1){
     echo "$valor Existe !";
   }
}

Edit:
Adicionado trecho para remover o html.
Só cuidado, pois a função vai remover tudo que estiver entre <>
Comment: Ótimo! Dessa maneira eu iria conseguir percorrer todas as palavras do texto? Pois o arquivo que acaba sendo lido, contém mais ou menos 150 textos na mesma formatação que esse colocado como exemplo.
Comment: Ele vai percorrer todas as palavras que estão no "array 2" e verificar se ela existe no "array 1", a quantidade de texto depende de como você populou o array
Comment: Sim, no caso o "array2" contém as palavras "chaves" e no "array1" contém o texto com esse padrão. Tem alguma maneira de filtrar e separar os textos do array1? Tipo, o texto principal começa após a tag ****** e termina antes do ******.
Comment: @user56742 editado

Verificar palavras em Array php


Spark MLlib collaborative filtering

Question: I am building a recommender system for implicit data in Spark using MLlib. I was trying to find a already implemented function to make recommendations to users not seeing during training and I could find anything. Does anybody knows if such a function exist ? otherwise anybody has any advise on how too implemented it efficiently?
Answer: There is no function for this. In fact it's not really feasible to make on-the-fly recommendations given the simple RDD-based model you get from MLlib. You can see an implementation in this blog post, at http://blog.cloudera.com/blog/2014/03/why-apache-spark-is-a-crossover-hit-for-data-scientists/ :
def recommend(questionID: Int, howMany: Int = 5): Array[(String, Double)] = {
  // Build list of one question and all items and predict value for all of them
  val predictions = model.predict(tagHashes.map(t => (questionID,t._1)))
  // Get top howMany recommendations ordered by prediction value
  val topN = predictions.top(howMany)(Ordering.by[Rating,Double](_.rating))
  // Translate back to tags from IDs
  topN.map(r => (tagHashes.lookup(r.product)(0), r.rating))
}

The lookup makes it very slow though. To implement the recommendation rapidly (like <10ms) you would have to coalesce the RDD into an in-core representation.
However if you just need batch recommendations, then an approach like the above can be made efficient with joins.

Spark MLlib collaborative filtering


How to shift the weighted mean of a monotically increasing series of values

Question: I have a monotonically increasing series of values $X, x\in[0,1] \ \forall i\in1,2,...,60$ the weighted mean of which is defined by
$$\bar{x}=\frac{x_1+\sum_{i=2}^{60} i(x_i-x_{i-1})}{x_{60}}.$$
Given an input $\alpha$, I would like to figure out how to shift the values of $X$ to $X^{'}$ such that the weighted mean of $X^{'}$ is equal to $\alpha$.
For a "real world" example of this, imagine I am a member of a support team who is given a number of new accounts to support each month.  We have a curve based on past experience that defines the percentage of accounts starting in any given month that will have left after $y$ months over a 5 year period (a persistence curve, if you will).  The weighted mean I described above gives the average time any one account will continue receiving support from my team.  If we expect the average time in which accounts will stay with us to increase from, say, 12 months to 18 months, how should I adjust this curve to shift the weighted mean to 18 from 12 while keeping the general shape of the curve the same?
Thanks in advance!
Answer: Generalizing a little,
$\overline{x}
=\dfrac{x_1+\sum_{i=2}^{n} i(x_i-x_{i-1})}{x_{n}}\\
$
So,
replacing $x_i$ by $x_i+c$,
we get
$\begin{array}\\
\overline{x+c}
&=\dfrac{x_1+c+\sum_{i=2}^{n} i((x_i+c)-(x_{i-1}+c))}{x_{n}+c}\\
&=\dfrac{x_1+c+\sum_{i=2}^{n} i((x_i+c)-(x_{i-1}+c))}{x_n}\dfrac{x_n}{x_{n}+c}\\
&=\left(\dfrac{x_1+\sum_{i=2}^{n} i(x_i-x_{i-1})}{x_{n}}+\dfrac{c}{x_{n}}\right)\dfrac{x_n}{x_{n}+c}\\
&=\left(\overline{x}+\dfrac{c}{x_{n}}\right)\dfrac{x_n}{x_{n}+c}\\
&=\left(\overline{x}x_n+c\right)\dfrac{1}{x_{n}+c}\\
\end{array}
$
and if this $=a$,
then
$a
=\left(\overline{x}x_n+c\right)\dfrac{1}{x_{n}+c}
$
or
$a(x_n+c)
=\overline{x}x_n+c
$
or
$c(a-1)
=\overline{x}x_n-ax_n
=x_n(\overline{x}-a)
$
or
$c
=\dfrac{x_n(\overline{x}-a)}{a-1}
$.
Note that if you want
$a=1$,
this requires
$\overline{x} = 1$,
so no change is needed.
Comment: Thanks for the answer.  However, I am having a little trouble with this and was hoping you could help me clarify.

Consider the sample sequence $x^T=[0,0.4,.7,.9,1]$.  The weighted mean of this sequence is $3$.  If I want to change it to $4$, your answer directs me to find $c=(1)*(4-3)=1$.  Then I add this to each $x$ to get $y^T=[1,1.4,1.7,1.9,2]$.  However, the weighted mean of this sequence is $2$.  Am I doing something incorrectly here?
Comment: I see my error. I am trying to fix it. Thanks.


problem when saving images in wpf

Question: how to save the image  with tooltip in wpf.
so that when i open the image again,
the tooltip where i added must be displayed
Comment: i load a image and add a tooltip when mousedouble clicks on it. when i save the image, the tooltip must also be saved but hidden, so tat when i load the image again, the tooltip where i created tooltip initially must be shown.
Answer: To do this you must save location of the tooltip seperaterly. and load it back again when you load image. Essentialy you can save picture in your custom format where in first line you would put X and Y location of the tooltip and any other info you need, rest of the file is image. When you load file read firs line and parse it manually, then load the rest of the file as image.

problem when saving images in wpf


How to perform a search action by android custom keyboard action button?

Question: I'm developing an android custom keyboard. I need to perform search action, move cursor to next text field action according to the text fields that user selected. How to perform those actions?
Answer: I got the answer.
<code>getCurrentInputConnection().performEditorAction(EditorInfo.IME_ACTION_GO);
getCurrentInputConnection().performEditorAction(EditorInfo.IME_ACTION_SEARCH);
getCurrentInputConnection().performEditorAction(EditorInfo.IME_ACTION_NEXT);
</code>


PLS procedure in SAS software

Question: experts, I have a question about PLS procedure in SAS. The manual said that the prediction on new data is by combining training data and new data (new data dont have response values).
I did experiments, say, 300 training data and 2000 new data, by using that approach, the predicted results of 2000 new data are quit similar (variability is less than 0.3) which is not right. Can you have some suggestions on how to do prediction using PLS procedure in a proper way?
Many thanks for your kind reply.
Answer:
The similarity of the output in itself does not allow any conclusions. After all, that may be correct.
PLS regularization "pushes" the output towards the mean output in the training data. A PLS model that too heavily regularized (undercomplex) will typically predict with a bias towards the mean, i.e. too low variability. Undercomplex here is wrt. the underlying truth. The undercomplex model can still be the objectively best model that can be obtained with the given training data set.

PLS procedure in SAS software


Compressing vectors by using log-linear modeling?

Question: I have $k$ vectors, each consisting of $2^n$ positive reals adding up to 1, and I'd like to compress them by only saving $n$ reals per vector.
One approach is a no-interactions log-linear model to model $2^n$ outcomes by using $n$ parameters for $k$ different distributions, except here I'm free to pick $n$ features of my log-linear model, which are then fixed for all $k$ vectors.

Has this been done?
What algorithm to use to pick $n$ features?
Answer: One common approach would be to use an autoencoder (neural network) with a $n$ dimensional bottleneck. As you say, the output would then be $2^n$ dimensional again and then one would apply the softmax function to it. I'd assume you could do this relatively easily with gradient descent and e.g. a cross-entropy loss. Autoencoders are of course a pretty common idea. It would sort of feature engineer its own $n$ features out of the original inputs via the layers of the neural network.
Of course, it's probably not clear what the best architecture is (e.g. how many layers until the bottle-neck, e.g. 2, with how many neurons in each layer, e.g. decreasing in some sensible way, and then the same questions when going back to the $2^n$ dimensional output), what activation functions (e.g. ReLU) to use in which layers, how to regularize (I'd assume drop-out might not be so great for a regression type task, but maybe weight decay of some form) and whether to perhaps do something like a denoising autoencoder. All those are probably best experimented with via a suitable cross-validation set-up.
Comment: Bottleneck autoencoders are actually pretty hard to train, GD just plateaus at high loss, I did some experiments here -- https://yaroslavvb.medium.com/optimizing-deeper-networks-with-kfac-in-pytorch-4004adcba1b0
Comment: Motivation for this question was Example 7 in https://arxiv.org/abs/0707.3794 -- you can recover $m$ original numbers from a set of $n$ overlapping sums. If you assume independence, $n<<m$. So it comes down to -- which overlapping sums to use?


when does Poisson distribution approximate binomial distribution - what is n in an e-commerce setting

Question: I am quoting from here:
"As with many ideas in statistics, "large" and "small" are up to interpretation. A rule of thumb is the Poisson distribution is a decent approximation of the Binomial if n > 20 and np < 10. Therefore, a coin flip, even for 100 trials, should be modeled as a Binomial because np = 50. A call center which gets 1 call every 30 minutes over 120 minutes could be modeled as a Poisson distribution as np = 4."
Here n is trials with a probability p. I try to translate this into an e-commerce setting where we have websites which users can visit. I would think that np is the visiting rate. So in the above example there are 4 visitors within 120 minutes on average. I am sorry I do not get my head around what n is in my setting? I would think it has to do with conversions? Thanks.
Comment: The quotation is confusing, because (1) there are not any separate values of $n$ and $p$--there is only $np;$ and (2) the relevant parameter is a *rate*, which here would be $4$ calls *per* 120 minutes: that is, $2$ calls per hour or $1$ call per 30 minutes.  This latter comment demonstrates forcibly that $n$ has no specific value in this setting, because it depends on the duration of your process.
Comment: Yes: a Poisson-like process with a variable rate is known as an *inhomogeneous Poisson process.*  They are used to model time series and spatial events.  You can find current papers on these techniques by searching on the key words.
Comment: @whuber that's what I thought as well. I know you as a great expert. I really want to learn more about Poisson processes etc. in this context but I feel they are too restrictive as the rate is constant. can the rate also be modelled? basically I want to model conversions depending on price and other factors. I feel like a (somewhat confused) kid in the sweetshop with all the different possible ways: choice modelling, poisson glm (?) even thompson sampling. If you could please be so kind and recommend any good teaching resource on that subject, that would be very much appreciated please.Thanks.
Comment: thanks - so the keyword is inhomogeneous (-:
Answer: Binomial and Poisson distribution families are both distributions for counts, rather than continuous measures. The difference is in the cardinality: Poisson distributions are useful for unbound counts. Binomial distributions have an upper limit, which is the number of trials. Poisson distribution can be seen as a limiting case of infinite trials. The more trials a Binomial process has (at a very low success rate), the more it starts to look Poisson.
However, since Poisson and Binomial aka Logistic regression are equally well established and can both easily be estimated, it makes no sense to approximate one with the other. In essence, if you can precisely tell what the number of trials is, it is Binomial. If you can't tell it, it usually is Poisson.
Regarding your domain of e-commerce, examples of Poisson distributed variables would be number of visitors in a time frame or number of products in the cart. Binomial variables would be proportion of visits where sth is actually bought or number of test users that prefer design A over B.


Proof of $ \text{Var}\,\left(\sum_{i=1}^{n}g(X_i)\right)=n\left(\text{Var}\,g(X_1)\right).$

Question: I have a question about part of a proof of a Lemma in a book (Casella's Statistical Inference) I'm reading. This it how it goes.

Let $X_1, \cdots ,X_n$ are a random sample from a population and let $g(x)$ be a function such that $\mathbb{E}g(X_1)$  and $\text{Var}\,g(X_1)$ exist. Then $$ \text{Var}\,\left(\sum_{i=1}^{n}g(X_i)\right)=n\left(\text{Var}\,g(X_1)\right).$$

So this is how I proceeded to to prove it.
Since the $X_i's$ are independent, we have that
$$
\begin {align*}
  \text{Var}\,\left(\sum_{i=1}^{n}g(X_i)\right)&= \text{Var}\,g(X_1)+\cdots +\text{Var}\,g(X_n)\\
&= n\text{Var}\, g(X_1). \end {align*}$$
where the last equality holds because the $X_i's$ are identically distributed.
Can I do this? I'm asking this because the proof in the book started by using the definition of the variance and somewhere along the lines involved the covariance matrix.
Thanks.
Comment: Yes, you can do this. Let $Y_i = g(X_i)$. Then $Y_1,\ldots,Y_n$ are i.i.d. random variables with ${\rm Var}(Y_1) < \infty$ (hence also the mean exists). It is an elementary (and very useful) result that, in this case, ${\rm Var}(Y_1 + \cdots + Y_n) = {\rm Var}(Y_1) + \cdots + {\rm Var}(Y_n)$.
Comment: Thanks. That's refreshing to know...)
Answer: Shai has answered Nana's question, but in the interest of this question being "officially" answered let's prove the elementary result Shai cites; namely, that if $Y_1, \ldots, Y_n$ are independent random variables with finite mean and variance then $\newcommand{\Var}{\mathrm{Var}}\Var(Y_1 + \cdots + Y_n) = \Var(Y_1) + \cdots + \Var(Y_n)$.
First, let's prove it in the $n=2$ case.  If $Y_1$ and $Y_2$ are independent then we know that $E[Y_1 Y_2] = E[Y_1] E[Y_2]$.  By a basic property of the variance,
$$\Var(Y_1 + Y_2) = E[(Y_1 + Y_2)^2] - (E[Y_1 + Y_2])^2 = E[Y_1 + 2Y_1Y_2 + Y_2^2] - (E[Y_1] + E[Y_2])^2$$
$$= E[Y_1^2] + 2E[Y_1Y_2] + E[Y_2^2] - E[Y_1]^2 -2E[Y_1]E[Y_2] - E[Y_2]^2$$
$$= E[Y_1^2] - E[Y_1]^2 + E[Y_2^2] - E[Y_2]^2 = \Var(Y_1) + \Var(Y_2).$$
Then, applying the result for the $n=2$ case successively to the general case we have
$$\Var(Y_1 + \cdots + Y_n) = \Var(Y_1) + \Var(Y_2 + \cdots + Y_n) = \Var(Y_1) + \Var(Y_2) + \Var(Y_3 + \cdots + Y_n)$$
$$= \cdots = \Var(Y_1) + \cdots + \Var(Y_n).$$


Change my location button direction and icon in the Jetpack Compose

Question: I have default MyLocation button on the Jetpack Compose's map but I want to move the Botton in the bottom and right and change the button icon but I don't know how can I do this in the Jetpack Compose.
My map view screen:
AndroidView(
    factory = { mapView }
) {
    mapView.getMapAsync { map ->
        map.apply {
            if (permissionsState){
                isMyLocationEnabled = true
            }
            uiSettings.isMyLocationButtonEnabled = true
            navigatorViewModel.apply {
                scope.launch {
                    lastSelectedLocation.collect {
                        val position = LatLng(
                            it.lat,
                            it.lng
                        )
                        moveCamera(
                            CameraUpdateFactory.newLatLngZoom(
                                position,
                                Constants.ZOOM_CAMERA
                            )
                        )
                    }
                }
                scope.launch {
                    setOnCameraIdleListener {
                        val cameraPosition = map.cameraPosition
                        setLocation(
                            LocationModel(
                                lat = cameraPosition.target.latitude,
                                lng = cameraPosition.target.longitude
                            )
                        )
                    }
                }
            }
        }
    }
}

Comment: @Johann It's in our design
Comment: Why would you want to do that? It's a standard part of the map where users expect to find it. What is your real objective?
Comment: Sorry to bother but did you manage to find a solution to it? A way to move the button to the button to the bottom right corner/ modify the button layout ?
Answer: You can draw an overlay and place your custom button there.
Comment: Answer needs supporting information
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](https://stackoverflow.com/help/how-to-answer).


How does the function $f(z)=\dfrac{\sin z}{z}$ have isolated singularity at $z=0$?

Question: I want to ask how $f(z)=\dfrac{\sin z}{z}$ has a isolated  singularity at $z=0$? please someone tell.
Comment: Do you know the *definition* of an isolated singularity? This one is removable, BTW.
Comment: You divide by $z$. Division by $0$ is problematic. The singularity is however removable.
Comment: $\frac{\sin z}{z^2}$ has an isolated singularity.
Comment: Your function is holomorphic on $\Bbb{C}/\{0\}$.
Comment: Notice that $(\sin0)/0$ does not exist, but $\lim_{z\to0}(\sin z)/z$ exists and equals $1$.  So it's not just an isolated point where the function is undefined; it's also a point where there is a removable singularity.  (There is the problem of showing that when the singularity is removed, the resulting function is holomorphic.  If I were to write the details of that right now, I might have made it an answer rather than a comment.)
Answer: A function $f$ has an isolated singularity at $z=a$ if there is an $R>0$ such that $f$ is defined and analytic in $B(a;r)\setminus \{a\}$ but not in $B(a;R)$. [Conway, pg. 103]
What values of $z \in \mathbb{C}$ make your function non-analytic?


Commutivity of unique existence quantifiers

Question: Find an expression P(x,y) to disprove the following equivalence,
$(\exists!x)(\exists!y)P(x,y)\Leftrightarrow(\exists!y)(\exists!x)P(x,y)$
I could only think of a few statements of $P(x,y)$ that made some sense, such as in a commutative ring, $(\exists!x)(\exists!y)(\forall a)(ax=x\wedge ay=a)$ where $x$ is our zero and $y$ is our 1. This doesn't seem to work since it doesn't matter if we change the order of the quantifiers of $x$ and $y$.
Or maybe a version of L'Hopital's rule,
$(\exists!x)(\exists!y)\left(\lim_{z\rightarrow a}\frac{f'(z)}{g'(z)}\Rightarrow\lim_{z\rightarrow a}\frac{f(z)}{g(z)}\right)$, which to me seems more plausable since the unique existence of $y$ has no bearing on the unique existence of $x$, as L'Hopital's rule states.
However, determining whether this helps come up with a counter-example is difficult since I'm having trouble interpreting the truth of this statement (maybe I need to specify $a,g$, or $f$).
Feel free to fix either of my examples, or make some counter-examples of your own.
Answer: It may be easier to make up a completely abstract example which is designed to do what you want, rather than looking for a "real life" example.  On $S=\{1,2,3\}$ define
$$P(x,y)\quad\hbox{if and only if}\quad
  (x,y)\in\{(1,1),(2,1),(2,2),(3,1),(3,2)\}\ .$$
Then for $x=1$ there is a unique $y$ (namely, $y=1$) such that $P(x,y)$; and for any $x\ne1$ there are two values of $y$ for which $P(x,y)$ is true.  So
$$\exists!\,x\ \exists!\,y\ P(x,y)$$
is true.  On the other hand, for any $y\in S$ there are either three, two or no values of $x$ for which $P(x,y)$ is true.  Therefore
$$\exists\,y\ \exists!\,x\ P(x,y)$$
is false, and consequently
$$\exists!\,y\ \exists!\,x\ P(x,y)$$
is false.
Answer: Your first example isn't good because $P(x,y)$ can't have any more variables and you're looking at $a$ as a variable. You should look at it as a constant and do not quantify it. Even better would be to replace by a constant, but you need to find an actual example that works.
Your l'Hôpital example doesn't make sense because $\left(\lim_{z\rightarrow a}\frac{f'(z)}{g'(z)}\Rightarrow\lim_{z\rightarrow a}\frac{f(z)}{g(z)}\right)$ is not a well formed formula, it is not a statement.
For an example that works, consider your universe to be $\mathbb R$ and the predicate $P(x,y)\colon x^2=y$.


Weights for estimating ATE (rather than ATT) in SAS %CEM macro for coarsened exact matching?

Question: I'm running Gary King's %CEM SAS macro (available here) for coarsened exact matching (CEM) for a project at work.
The macro works fine for estimating the Average Treatment Effect on the Treated (ATT) and returns sample weights (labelled Wsc1_stur) for that purpose.
We're also interested in estimating the overall Average Treatment Effect (ATE) using CEM.
Is there an option in the %CEM macro for calculating the ATE?
Answer: First, be aware that if any units are discarded (i.e., not matched), you will not be estimating the ATE but rather the ATE in the matched sample.
This option is not available in the %CEM macro. If you have access to stratum membership, you can produce stratum weights manually. For each stratum $S$, the treated units get a weight of $\frac{N_S}{N_{S,1}}$ and the control units get a weight of $\frac{N_S}{N_{S,0}}$, where $N_S$ is the number of units in stratum $S$, $N_{S,1}$ is the number of treated units in stratum $S$, and $N_{S,0}$ is the number of control units in stratum $S$.
It may also be possible to call the R package MatchIt from within SAS (e.g., using the %PROC R macro), which has support for ATE weights form coarsened exact matching.
Comment: The formula they provide is the same except for a scaling factor applied to the weights of $1/\left(1+\frac{N_0}{N_1}\right)$ for treated and $1/\left(1+\frac{N_1}{N_0}\right)$ for control. Including a constant scaling factor doesn't affect balance, the difference in outcome means, or the results of a fully interacted outcome model. In `MatchIt`, I use a scaling factor (not sure if it's actually this one) to ensure the average of the nonzero is equal to 1 in each group.
Comment: Thanks Noah! Yes, looking in the %CEM macro code I bet I can manually code the ATE weights using your formula. BTW the following blog post on CEM uses different formulas for the ATE weights: https://towardsdatascience.com/coarsened-exact-matching-in-r-a36ae7ef6849 . Are they equivalent to the weight formula you provided?


How to PHPUnit test a method with no return value?

Question: I am trying to test methods from the following class I have written (there are more functions than what is shown, basically, one function for each is_*() method):
class Validate {
  private static $initialized = false;

  /**
  * Construct won't be called inside this class and is uncallable from the outside. This prevents
  * instantiating this class. This is by purpose, because we want a static class.
  */
  private function __construct() {}

  /**
  * If needed, allows the class to initialize itself
  */
  private static function initialize()
  {
    if(self::$initialized) {
      return;
    } else {
      self::$initialized = true;
      //Set any other class static variables here
    }
  }

  ...

  public static function isString($string) {
    self::initialize();
    if(!is_string($string)) throw new InvalidArgumentException('Expected a string but found ' . gettype($string));
  }

  ...

}

When I test if the methods throw an exception on invalid input, it works great! However, when I test if the method works as expected, PHPUnit complains because I have no assert in the test. The specific error is:
# RISKY This test did not perform any assertions

However, I don't have any value to assert against so I'm not sure how to overcome this.
I've read some about testing static methods, but that mostly seems to cover dependencies between static methods. Further, even non-static methods could have no return value, so, how to fix this?
For reference, my test code:
class ValidateTest extends PHPUnit_Framework_TestCase {
  /**
  * @covers ../data/objects/Validate::isString
  * @expectedException InvalidArgumentException
  */
  public function testIsStringThrowsExceptionArgumentInvalid() {
    Validate::isString(NULL);
  }

  /**
  * @covers ../data/objects/Validate::isString
  */
  public function testIsStringNoExceptionArgumentValid() {
    Validate::isString("I am a string.");
  }
}

Comment: Yes, but when you have a validation method like this, why should the validation class dictate behavior to the caller? Maybe the code calling this class is trying to make a decision on something to do based on whether a value is a string, fully expecting that sometimes that other data types may be present.  Why should this validation class throw an exception, which must than be handled, rather than just giving the caller what it is looking for - an understanding of whether the value in question is indeed of string type? This also seems like a trivial function in that PHP has this built-in.
Comment: Why would that method not return true/false? WHy would it be an exception when a non-string value is passed as parameter, as obviously a method to test if something is a string, should expect non-string values or there is not utility to the method.  This seems like an ill-conceived method.
Comment: @MikeBrant It doesn't return because it has no need to. I use the method in my code as a break/assert - if the method doesn't throw an exception my code continues on as normal. If it does, the exception must be dealt with. There is no reason at all to return anything from these methods, and even less reason to write if/else statements and spend cycles checking those return values. I want exception instead of TRUE/FALSE because exceptions can stop code execution. If you put an int where an array should go in Java (or most typed languages) you get an exception. I want similar functionality.
Comment: @MikeBrant Further, proper testing means ensuring my method works for both invalid and valid input.
Comment: @MikeBrant PHP does have it built it, `is_string()`, I use it in the function. However, since I do use exceptions, I don't want to be writing code for new exceptions all over the place. Having a validation class is not uncommon. I understand what you are saying about how the class works, and you have valid points. However, the way you point out is not the way I designed it, and while my design might not be perfect in general, it's the way I think it should be for what I am doing.
Comment: @MikeBrant I did think about having all the Validate methods return true at the end, but it seems bad to write code just to make testing easier.
Comment: @MikeBrant agree with Mike, why design it like this in the first place? It checks for string, dangit, just return true / false or -1 0 1 if you like numbers more
Answer: Test void function with assertNull:
    /**
     * @covers ../data/objects/Validate::isString
     */
    public function testIsStringNoExceptionArgumentValid() {
         $this->assertNull( Validate::isString("I am a string.") );
    }

Comment: Trying to use the return value of a void function will (rightly) get flagged in many linters.
Answer: To prevent the warning about the assertions you can use the @doesNotPerformAssertions annotation as explained in the documentation: https://phpunit.de/manual/current/en/appendixes.annotations.html#idp1585440
Or if you prefer code over annotation:
$this->doesNotPerformAssertions();
Comment: Nice answer, but instead `doesNotPerformAssertions` there should be `expectNotToPerformAssertions`, because first just return current value. [github](https://github.com/sebastianbergmann/phpunit/blob/64d377cc488cd3707f34f46266c8d22e21f1da06/src/Framework/TestCase.php#L617)
Answer: One solution I have come upon is the following, based on example 2.12 from chapter 2 of PHPUnit. It feels a little hacky to me, but it's the best I've found so far. Also, based on this PHPUnit Gitub issue discussion, it seems several other people want this feature but that there are no plans to implement it.
Change testIsStringNoExceptionArgumentValid() to the following:
  /**
  * @covers ../data/objects/Validate::isString
  */
  public function testIsStringNoExceptionArgumentValid() {
    try {
      Validate::isString("I am a string.");
    } catch (InvalidArgumentException $notExpected) {
      $this->fail();
    }

    $this->assertTrue(TRUE);
  }

Comment: You don't need the try/catch bit. No need to fail(), the exception will do that.
Comment: How to test a method that is not returning anything and does not throw any exception
Comment: @Souad either let the function return something ( you don't need to use it in the main program logic! ) or create a local var, let's say "errorCode", and with everyhing that does not work as intended increment errorCode by 1, thus you can assertEquals(0,errorCode);
Answer: If you want to test a void function you only need to run it without any assertion.
If it there is any issue it will throw an exception and test will fails. No need to put     $this->assertTrue(TRUE); as you are not running an assertion and having assertions is not required to test your code.
You will get a message like

Time: 7.39 seconds, Memory: 16.00 MB

OK (1 test, 0 assertions)
Process finished with exit code 0


Where can you perform variable substitution in an equation?

Question: I've ended up in a debate on a proof for .999˜ being equal to 1 (in the real number system.)  I think the arguments have boiled down to whether or not we can preform variable substitution on one side of the equation, while keeping that variable rather than substituting it on the other side of the equation.
What we are looking at:
Given: x = 0.999˜ (Using the ˜ symbol to represent repeating)
    .. A
    10x - x     = 9.999˜ - x
    9x          = 9.999˜ - x
    9(.999˜)    = 9.999˜ - (.999˜)
    8.999˜      = 9
    ..
    OR
    .. B
    10x - x     = 9.999˜ - x
    9x          = 9.999˜ - (0.999˜) // Preform a substitution on ONLY the RHS
    9x          = 9
    x           = 1
    ..
What's so confusing is that both should be right...since 8.999˜ IS equal to 9.  But, since this is a proof you should expect the same representation of the number at the most simplified step (I believe?)
So for simplicity sake my questions are...:

Is the algebra valid in both examples A and B?
Does example A act as proof regardless of giving different representations of the same number at the most simplified step?
Comment: A does not find the value of $x$; so A does not give you a "representation" for $x$. In particular, you did not find two different representations of the same number.
Comment: @ArturoMagidin: That's a good point.  However, it should show that it is a balanced equation nonetheless...but I'm not sure if it does that given that it doesn't simplify to the same representation of the number?
Comment: $5$ can be represented as $2+3$ and as $4+1$. Does that mean that "$2+3=4+1$" is not "balanced" because it doesn't give you the same representation of "the number 5"? A doesn't compute anything, it just gives tells you (modulo unjustified arithmetical claims) that there are at least two ways to write $9$: as $9$, and as $8.9999\overline{9}$. B is trying to find an alternative expression for $x$. There is nothing "unbalanced" going on, though there is a lot of unjustified arithmetic.
Comment: @ArturoMagidin: That makes sense.
Answer: (Yes: decimal representations of some real numbers are not unique; any number with terminating decimal representation has two representations: one that terminates, and one that does not.)
In both procedures, the real difficulty lies in justifying the step from
$$9.999\overline{9} - 0.999\overline{9}$$
to $9$. The problem is not the substitution, the problem is the justification for that difference. In $A$, there is the additional problem of justifying the step that goes from $9(9.999\overline{9})$ to $8.999\overline{9}$.
Note also that in A you are not finding the value of $x$, you are finding something else. So you are not getting "two different representations of the same number". You didn't get a value for $x$ in A.
The reason why the steps mentioned above are troubling:
If you are justifying the step that goes from $9.999\overline{9} - 0.999\overline{9}$ to $9$  in terms of the algorithm for computing differences of numbers written in decimal notation, you are going to run into a serious problem: the algorithm says, as its first step, to start at the rightmost digit of the two numbers. But neither of these numbers has a "rightmost digit", so the algorithm for computing differences does not apply.
If you are not justifying the step in terms of that algorithm, then how are you justifying it?
Likewise, the algorithm for computing $9\times 9.999\overline{9}$ requires you to perform infinitely many products; in all steps you have a "carry", so the procedure never ends. On what grounds can you call the computation "finished"?
The steps can be justified, but not by basic arithmetic. You end up talking about infinite series and limits.
Comment: So, it's legal to do variable substitution on one and only one side of an equation?
Comment: @Purebe: You are using transitivity of equality. If $9x=7x+2$ and $x=a$, then $7x+2 = 7a+2$; so $9x=7x+2$, and $7x+2=7a+2$, hence $9x=7a+2$ (to pick a random example).
Comment: Transitivity of equality...that is what I am looking for.  Thanks!


rsocket net readerIndex exceeds writerIndex

Question: On websocket connection between .net and spring boot rsocket I am trying to encode the routing header to the quotes endpoint like this:
int routeSize = 6;
string hexValue = routeSize.ToString("X");
metaData = hexValue + "quotes";

I don't think this is correct. The entire net client code is
var client = new RSocketClient(new WebSocketTransport("ws://127.0.0.1:7000/"));
await client.ConnectAsync(new RSocketOptions()
{
    InitialRequestSize = 3,
    DataMimeType = "application/json",
    MetadataMimeType = "message/x.rsocket.routing.v0"
});

String json = {\"myQuote\":\"1234\"}
int routeSize = 6;
string hexValue = routeSize.ToString("X");
metaData = hexValue + "quotes";

var stringclient = new RSocketClient.ForStrings(client);
await stringclient.RequestStream(json, metaData)
    .ForEachAsync((result) =>
    {
        Console.WriteLine($"Result ===> {result}");
    });

and this produces the error
0001 Error {000}: [00000201] readerIndex(1) + length(54) exceeds writerIndex(7): UnpooledSlicedByteBuf(ridx: 1, widx: 7, cap: 7/7, unwrapped: PooledUnsafeDirectByteBuf(ridx: 0, widx: 281, cap: 281))
As related to RSocket Net client request stream routing metadata to spring boot @MessageMapping routes what's required is the C# equivalent of JavaScript String.fromCharCode(route.length) + route;
Answer: The answer was to use a default encoding to get a byte[] of the route name size as integer 6 and then add the length of the route name in bytes followed by the route, passing the string as metaData according to https://github.com/rsocket/rsocket/blob/master/Extensions/Routing.md
byte[] intBytes = BitConverter.GetBytes(6);
string stringBytes = Encoding.Default.GetString(intBytes, 0, 1);
metaData = stringBytes + "quotes";