Specifying random slope in a nested random effects

Question: I collected crop yield and rainfall data from multiple counties and year ( > 30 years).
Each county can only belong to one province and each province can only belong to one region. I am interested in knowing the relationship between
crop yield and rainfall.
Which of the two is the right way to specify the nested structure ( I guess it's the first one):
mod1 <- lmer(yield ~ rainfall + (1|region/province), data = dat)
mod2 <- lmer(yield ~ rainfall + (1|region) + (1|province), data = dat)

Building on this, if I am also interested in including the time trend of each county, is the following specification correct:
 mod3 <- lmer(yield ~ rainfall + year + (1|region/province) + (1 + year|county), data = dat)

Lastly if I am interested in fitting a model just based on year and location (i.e. no rainfall), is it the right way to specify yield as a function
of year and location and year trend is allowed to vary by county?:
mod4 <- lmer(yield ~ year + (1|region/province) + (1 + year|county), data = dat)

Answer: You are correct for the first model but you would use this syntax.
mod1 <- lmer(yield ~ rainfall + (1|region:province), data = dat)

The syntax below are for if you have crossed random effects where provinces are included in multiple regions.
mod2 <- lmer(yield ~ rainfall + (1|region) + (1|province), data = dat)

How many counties do you have? To model the trend of counties over time you might want to include your time and county variables as fixed effects, presumably multiple dummy coded county variables, and an interaction between time and the county variables.
mod4 <- lmer(yield ~ year + county_1 + year*county_1 +(1|region:province), data = dat)

Comment: Each of your county has multiple measurements right? So if you include county as a random effect it accounts for repeated measures.
Comment: Thank you.  I have around 5000 counties and for each county I have collected yield data for 35 years. If I want the time trend to a random effect, how do I insert it?
Comment: Oh I see so you want to model repeated measures, I was thinking you want to look at differences between counties over time.
Comment: I would change the random effects so it would be (1|region:province:county) and if you want to let the slopes for time to vary it would be (year | region:province:county).
Comment: Okay. I did not understand why I need county as a random effect. I am using province to group my counties as in ` (1 | region:province)` but I also need to add the time trend of each county as well. Does this make sense? ` (1 | region:province)  + (year|county)`?
Comment: Oh its because your counties are nested within provinces that are nested within regions so you want to account for dependence due to clustering.
Comment: https://stats.stackexchange.com/questions/228800/crossed-vs-nested-random-effects-how-do-they-differ-and-how-are-they-specified?rq=1
Comment: That is a great discussion of crossed vs nested random effects.


What is the upside of treating a factor as random in a mixed model?

Question: I have a problem embracing the benefits of labeling a model factor as random for a few reasons. To me it appears like in almost all cases the optimal solution is to treat all of the factors as fixed.
First, the distinction of fixed vs random is quite arbitrary. The standard explanation is that, if one is interested in the particular experimental units per se, then one should use fixed effects, and, if one is interested in the population represented by the experimental units, one should use random effects. This is not of much help because it implies one can alternate between fixed and random views even if the data and experimental design remain the same. Also, this definition promotes an illusion that, if a factor is labeled as random, the inference drawn from the model is somehow more applicable to the population than in case when the factor is labeled as fixed. Finally, Gelman shows that the fixed-random distinction is confusing even at the definition level because there are four more definitions of what fixed and random effects are.
Second, the estimation of mixed models is quite complicated. As opposed to a "purely fixed" model, there are more than a few ways to obtain the p-values. Prof. Bates who implemented REML estimation in the lme4 package in R went so far as to refuse to report the p-values altogether.
Third,  there is a murky issue of how many implicit parameters are introduced by a random factor. The following example is my adaptation of that in Burnham & Anderson, Model Selection and Multi-Model Inference: A Practical Information-Theoretic Approach. From the bias-variance tradeoff perspective, the role of random effects can be illustrated as follows. Consider a one-way ANOVA with $K$ treatments and $K$ main factor effects, of which $K - 1$ are estimable. The error term has $\mathcal N(0, \sigma^2)$ distribution. If the number of observations is fixed, the bias-variance tradeoff will deteriorate as $K$ goes up. Suppose we say that the $K$ main effects are drawn from $\mathcal N(0, \sigma_K)$ distribution. The corresponding model will have a complexity that is somewhere in between the fixed (overfitted) version and the underfitted model that contains the intercept only. The number of effective parameters in the fixed model is
$$1 \:\:\mathrm{intercept} + (K - 1) \:\:\mathrm{main\: effects} + 1 \:\:\sigma = K + 1.$$
The number of effective parameters in the random model is at least three: $ \mathrm{intercept}, \sigma, \sigma_K$. In addition,  the random model has  a number of "hidden" parameters implied by the distributional (normal in this case) restriction imposed on the main effects.
In particular, if there is a factor with two levels, it doesn't make sense to call it random, even if we know for sure that its levels have been sampled at random from some population. That is because the fixed effect version has three parameters, and the random effect version has over three parameters. In this case, the random model turns out to have more complexity than the fixed version. Apparently, a switch from the fixed to random version is more grounded for larger $K$. However, the number of "hidden" parameters in the random model is unknown, so it is impossible to compare the fixed and random versions based on the information criteria such as AIC. Therefore, while this example illuminates the contribution of random effects (the possibility of a better bias-variance tradeoff), it also shows that it is hard to say when it is justifiable to relabel the factor from fixed to random.
None of the above problems are present in a "purely fixed" model. Therefore, I am willing to ask:

Can anyone provide an example when something very bad happened when a random factor was used as if it were fixed? I believe there should be some simulation studies that address the issue explicitly.
Is there a proven quantitative method to decide when it makes sense to switch from  fixed to random label?
Comment: Well-written question, and I learned a few things from reading it. I do wonder if random effects analysis is doomed in the long run, perhaps to be beat out by more straightforward smoothing techniques. If I do try to answer #1, I'll go for a situation with high variability, many subjects, and small sample sizes within subject. Then the within-subject fixed estimates will be all over the place.
Answer: 1. A famous example in psychology and linguistics is described by Herb Clark (1973; following Coleman, 1964): "The language-as-fixed-effect fallacy: A critique of language statistics in psychological research."
Clark is a psycholinguist discussing psychological experiments in which a sample of research subjects make responses to a set of stimulus materials, commonly various words drawn from some corpus. He points out that the standard statistical procedure used in these cases, based on repeated-measures ANOVA, and referred to by Clark as $F_1$, treats participants as a random factor but (perhaps implicitly) treats the stimulus materials (or "language") as fixed. This leads to problems in interpreting the results of hypothesis tests on the experimental condition factor: naturally we want to assume that a positive result tells us something about both the population from which we drew our participant sample as well as the theoretical population from which we drew the language materials. But $F_1$, by treating participants as random and stimuli as fixed, only tells us the about the effect of the condition factor across other similar participants responding to the exact same stimuli. Conducting the $F_1$ analysis when both participants and stimuli are more appropriately viewed as random can lead to Type 1 error rates that substantially exceed the nominal $\alpha$ level--usually .05--with the extent depending on factors such as the number and variability of stimuli and the design of the experiment. In these cases, the more appropriate analysis, at least under the classical ANOVA framework, is to use what are called quasi-$F$ statistics based on ratios of linear combinations of mean squares.
Clark's paper made a splash in psycholinguistics at the time, but failed to make a big dent in the wider psychological literature. (And even within psycholinguistics the advice of Clark became somewhat distorted over the years, as documented by Raaijmakers, Schrijnemakers, & Gremmen, 1999.) But in more recent years the issue has seen something of a revival, due in large part to statistical advances in mixed-effects models, of which the classical mixed model ANOVA can be seen as a special case. Some of these recent papers include Baayen, Davidson, & Bates (2008), Murayama, Sakaki, Yan, & Smith (2014), and (ahem) Judd, Westfall, & Kenny (2012). I'm sure there are some I'm forgetting.
2. Not exactly. There are methods of getting at whether a factor is better included as a random effect or not in the model at all (see e.g., Pinheiro & Bates, 2000, pp. 83-87; however see Barr, Levy, Scheepers, & Tily, 2013). And of course there are classical model comparison techniques for determining if a factor is better included as a fixed effect or not at all (i.e., $F$-tests). But I think that determining whether a factor is better considered as fixed or random is generally best left as a conceptual question, to be answered by considering the design of the study and the nature of the conclusions to be drawn from it.
One of my graduate statistics instructors, Gary McClelland, liked to say that perhaps the fundamental question of statistical inference is: "Compared to what?" Following Gary, I think we can frame the conceptual question that I mentioned above as: What is the reference class of hypothetical experimental results that I want to compare my actual observed results to? Staying in the psycholinguistics context, and considering an experimental design in which we have a sample of Subjects responding to a sample of Words that are classified in one of two Conditions (the particular design discussed at length by Clark, 1973), I will focus on two possibilities:

The set of experiments in which, for each experiment, we draw a new sample of Subjects, a new sample of Words, and a new sample of errors from the generative model. Under this model, Subjects and Words are both random effects.
The set of experiments in which, for each experiment, we draw a new sample of Subjects, and a new sample of errors, but we always use the same set of Words. Under this model, Subjects are random effects but Words are fixed effects.

To make this totally concrete, below are some plots from (above) 4 sets of hypothetical results from 4 simulated experiments under Model 1; (below) 4 sets of hypothetical results from 4 simulated experiments under Model 2. Each experiment views the results in two ways: (left panels) grouped by Subjects, with the Subject-by-Condition means plotted and tied together for each Subject; (right panels) grouped by Words, with box plots summarizing the distribution of responses for each Word. All experiments involve 10 Subjects responding to 10 Words, and in all experiments the "null hypothesis" of no Condition difference is true in the relevant population.
Subjects and Words both random: 4 simulated experiments

Notice here that in each experiment, the response profiles for the Subjects and Words are totally different. For the Subjects, we sometimes get low overall responders, sometimes high responders, sometimes Subjects that tend to show large Condition differences, and sometimes Subjects that tend to show small Condition difference. Likewise, for the Words, we sometimes get Words that tend to elicit low responses, and sometimes get Words that tend to elicit high responses.
Subjects random, Words fixed: 4 simulated experiments

Notice here that across the 4 simulated experiments, the Subjects look different every time, but the responses profiles for the Words look basically the same, consistent with the assumption that we are reusing the same set of Words for every experiment under this model.
Our choice of whether we think Model 1 (Subjects and Words both random) or Model 2 (Subjects random, Words fixed) provides the appropriate reference class for the experimental results we actually observed can make a big difference to our assessment of whether the Condition manipulation "worked." We expect more chance variation in the data under Model 1 than under Model 2, because there are more "moving parts." So if the conclusions that we wish to draw are more consistent with the assumptions of Model 1, where chance variability is relatively higher, but we analyze our data under the assumptions of Model 2, where chance variability is relatively lower, then our Type 1 error rate for testing the Condition difference is going to be inflated to some (possibly quite large) extent. For more information, see the References below.
References
Baayen, R. H., Davidson, D. J., & Bates, D. M. (2008). Mixed-effects modeling with crossed random effects for subjects and items. Journal of memory and language, 59(4), 390-412. PDF
Barr, D. J., Levy, R., Scheepers, C., & Tily, H. J. (2013). Random effects structure for confirmatory hypothesis testing: Keep it maximal. Journal of Memory and Language, 68(3), 255-278. PDF
Clark, H. H. (1973). The language-as-fixed-effect fallacy: A critique of language statistics in psychological research. Journal of verbal learning and verbal behavior, 12(4), 335-359. PDF
Coleman, E. B. (1964). Generalizing to a language population. Psychological Reports, 14(1), 219-226.
Judd, C. M., Westfall, J., & Kenny, D. A. (2012). Treating stimuli as a random factor in social psychology: a new and comprehensive solution to a pervasive but largely ignored problem. Journal of personality and social psychology, 103(1), 54. PDF
Murayama, K., Sakaki, M., Yan, V. X., & Smith, G. M. (2014). Type I Error Inflation in the Traditional By-Participant Analysis to Metamemory Accuracy: A Generalized Mixed-Effects Model Perspective. Journal of Experimental Psychology: Learning, Memory, and Cognition. PDF
Pinheiro, J. C., & Bates, D. M. (2000). Mixed-effects models in S and S-PLUS. Springer.
Raaijmakers, J. G., Schrijnemakers, J., & Gremmen, F. (1999). How to deal with "the language-as-fixed-effect fallacy": Common misconceptions and alternative solutions. Journal of Memory and Language, 41(3), 416-426. PDF
Comment: +1 This a great answer, and your 2012 paper is a very nice read.
Comment: The Gelman link
Comment: Thanks for the pictures. According to your last paragraph, the more effects in the model are labeled as random, the higher the p-value for the remaining fixed effect(s). However, for an additive model it looks like when the default "containment" method is used in PROC MIXED, then the p-value for a fixed effect will be the same. One particular example is in this question: http://stats.stackexchange.com/q/112640/54099 How can you explain that?
Comment: Link to SAS manual: http://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_mixed_sect015.htm
Comment: @James Well, no, it's not as simple "when more factors are random, p-values go up." It depends on, among other things, the design/structure of the data, as you have discovered in your other thread. My last paragraph should only be interpreted as applying to the class of designs that I discuss in my answer: those with *crossed* random effects. On the other hand when groups or clusters are *nested* rather than crossed, the choice of fixed vs. random only affects estimates to the extent that the data are unbalanced and/or missing data. In the case of balanced, complete data, it doesn't matter.
Comment: @James And I don't really know where you're getting that about the containment DF method. That is just a method of determining the degrees of freedom for the t- and F-tests of the fixed effects--nothing more. That choice doesn't affect the parameter estimates or standard errors at all, which are the more substantial and interesting consequences that I'm talking about.
Comment: Oh and I should clarify what I said above about it not mattering. I am there referring to cases where we have categorical predictors only, like in your other thread. If there are continuous predictors in the model, it's a little more complicated and my statement above might not be true (indeed it likely isn't, but will be in certain special cases). The answer given by @Affine gives a helpful discussion of the relevant issues for the case of continuous predictors.
Comment: It's not enough to know the point estimates and standard errors because one has to know the null distribution as well. Containment method is one way to specify the null distribution which allows one to obtain the p-value.
Comment: I am aware of that @James. Do you have any other questions or...?
Comment: since the original question has already linked to an excellent discussion that gets into this - what exactly are you referring to?
Comment: @James I went ahead and added some more conceptual stuff, including some pictures. Let me know what you think.
Comment: Dear Prof. Westfall: Thank you for the references again. Could you suggest more recent references on data-driven model selection, including generalized mixed models. Thanks in advance
Answer: Suppose I have a manufacturing process that involves making material on several different machines.  They're the only machines I have so "machine" is a fixed effect.  But I make many lots of material on each machine and I am interested in predicting things about future lots.  I'll make "Lot number" a random factor because I'm interested in the results that I'll get for future lots.
Comment: Dear Emil: I am afraid you failed to understand the questions I asked. Your example illustrates the most common definition of "fixed vs random" that I myself provided in my question. Anyway, using your example, can you tell me why it is a bad idea to draw inference about future response from a model where the lot number is a fixed factor?
Comment: If you treat "lot number" as fixed, your inferences will only apply to the lots that you have already tested.  In other situations the same sort of thing occurs.  If you want to make inferences about random effects then treating them as fixed effects will generally give wrong answers.  In many mixed model situations, treating the random effects as fixed effects will even give incorrect answers regarding the effects that actually are fixed.
Comment: It's not a matter of upside versus downside.  If one does the an inappropriate analysis the results will usually be incorrect.
Comment: Could you provide a reference for a simulation study that demonstrated how the result become incorrect depending on the fixed/random label?
Comment: Note also that MLE does not come for free, especially when variance components are present. Try to set up a crossed factorial design with 3 factors and a couple of continues covariates. Then try estimating a pure fixed model and a few mixed specifications. As long as there is a random component that causes a switch from OLS to MLE/REML, the problems with convergence, getting zero or negative variance components, or other meaningless results are more likely.
Comment: That depends on the software.  JMP will default to REML if there are random effects but you choose Expected Mean Squares in the dialog box.
Comment: And with EMS, the solution doesn't have to exist at all.
Comment: With EMS you may get a negative variance component but that just tells you that that variance is small.  (An unbiased estimator of zero will come out negative some of the time.)  REML is often preferred but one can then get into convergence issues.

I'm not saying that I like random effects.  They are often a big pain in the neck, but if the real world situation one is modeling calls for random effects its hard to justify calling them fixed just for convenience.
Answer: So you treat them as random so that there is an averaging effect between the overall average and the average for that particular factor based on the sample size of the factor and the overall number of observations. This allows you to say that your results apply to the population at large, since you have a type of weighted average and an estimate of the variation due to that factor, if not, you really can only say that your results apply to the factor levels you used since the regression will treat them as discrete factors and not random ones that get the weighted averaging.
They are also useful when you have repeated measures on the same subject, since you can use them to account for the correlation between measures on the same subject.
Comment: As for RM, that's one of the reasons why I asked the question in the first place. As I mentioned here: http://stats.stackexchange.com/q/112640/54099 treating Subject as fixed or random doesn't change the Treatment p-value, so why bother.
Comment: If you have a simple crossed design with one factor fixed and one random and use Expected Mean Squares, the p-value for the fixed factor will differ from what you would get if you treated both as fixed.
Answer: When we have a grouping structure to our data $ Y_{ij} = \beta_1 X_{ij} + \beta_2 Z_{i} + e_{i} + \mu_{ij}$ where $X_{ij}$ are the observables of our individual observations and $Z_{i}$ are observations invariant among them and only observed at the grouping level, we cannot use fixed effects if we want to estimate $\beta_2$, since if we use the within estimator, $Z_{i}$ drops out, and if we use dummy variables for each $i$, $Z_{i}$ is collinear with them. So if we use a fixed effects estimator in this situation, we are ignoring potentially important information.
Even in the case $ Y_{ij} = \beta_1 X_{ij} + e_{i} + \mu_{ij}$ where we do not have any $Z_{i}$, we may still want to use random effects for a few reasons, despite the problems that may come with them, some of which are listed in your question.
In these kind of settings, random variation has two (or more if there are multiple grouping levels) sources of variation - variation "within" a group and variation "between" groups. The fixed effects (or "within") estimator completely removes variation between groups in estimating $\beta_1$. The random/mixed effects estimator allows "between" variation to contribute to the estimate of $\beta_1$, theoretically resulting in smaller standard errors.

(Original Answer)
One place where you are essentially required to use random effects is when you want to include parameters that are invariant at the grouping level of the fixed effect.
For example, say you want to investigate the impact of doctor characteristics (eg/ education) on patient outcomes. The dataset is patient-level with observed patient outcomes and patient /doctor characteristics. Since patients treated under a single doctor are likely correlated, you want to control for this. You could insert a doctor fixed effect here but in doing so, you preclude including any of the doctor characteristics in the model. Which is problematic if the interest is in doctor-level characteristics.
Comment: Could you provide some model statements?
Answer: I think it's related to consistency of the estimates.
Let's say $x_{ij} = a_i+b_j+e$ where $a_i$ stands for fixed effect(some experimental condition)
and $b_j$ stands for random effect(may person).
Neyman and Scott(1948) points out the problem of consistency of
Maximum Likelihood Estimates of $a_i$ and $b_j$.
If we take $a_i$ and $b_j$ as both fixed effect, the estimates are no longer
consistent. At least, that's how I understood...


Ajuda com esquema do banco de dados

Question: Estou desenvolvendo um sistema de controle de pedidos, e estou finalizando, porém agora me deparei com um problema no esquema da parte de pagamentos:
Estou ligando o id_pedido com os pagamentos, porém, quando o pedido custa R$48,90 e a pessoa paga R$50,00, eu tenho que dar R$1,10 de troco, estou salvando o R$50,00 no banco de dados para depois subtrair com o valor total, para na nota sair o valor do troco, até ai tudo certo, mas quando for gerar os relatórios isso vai me dar muita dor de cabeça, e não consigo pensar noutra maneira de fazer essa parte do trocado, pois precisa ficar salvo na nota trocado.
A pessoa pode pagar mais de uma maneira, vamos supor dinheiro e cartão.
Comment: Nota fiscal? PHP? Ai!. Não vejo problema algum aí, exveto usar a ferramenta errada para o problema. A não ser que o problema não seja esse e não esteja nada claro.
Comment: Problema está mais em lógica do que em programação. Se existe mais de uma forma de pagamento então você tem um relacionamento 1 para N ( 1 pedido para N formas de pagamento), só inserir na tabela de relação de Pedido e Tipo de pagamento o valor pago em cada forma. Somar tudo e subtrair do valor total do pedido.
Answer: Imagino que na sua tabela de pagamento vá ter o id do pedido e o valor pago, dessa forma você já consegue saber a diferença a ser devolvida.
Porém, você pode incluir se for o caso uma coluna na sua tabela de pagamento referente a esse tipo de diferença.
Ou ná própria query pra gerar o relatório pode ser feito a conta exemplo:
select pedido.valortotal, pagamento.valorpago, (pedido.valortotal - pagamento.valorpago) as troco
from pedido
inner join pagamento on pedido.id = pagamento.idpedido

Comment: então ,na verdade o valor eu coloco pela tabela de peças do pedido, pois cada peça pode ter um valor separado,e para pegar o valor total do pedido eu itero em todas as peças do mesmo pedido e faço a soma.terei que criar uma coluna e colocar o valor total do pedido então?
Comment: e sobre pegar o valor total de renda nos mês vamos supor, eu vou ter que pegar todos os pedidos e verificar na tabela de pagamentos, e fizer um if else?
Comment: O ideal é que na tabela de pedidos tenha o valor total do pedido já considerando o preço dos produtos.

Ajuda com esquema do banco de dados


como alterar o display da div pelo js?

Question: Por algum motivo que desconheço dessa vez o browser não quer renderizar o código... basicamente eu preciso mudar o display: nome da div para display: blockatravés do javascript.

botaoSticky = function(e) {
    e.target.style.display = "none"
    aparecerSticky()
}

const aparecerSticky = function(){
    const mySticky = document.getElementsByClassName('sticky')
    mySticky.style.display = "block";
}
 .sticky{
    display: none;
    background: rgb(42, 197, 211);
    height: 30vh;
    width: 90%;
    margin-top: 20px;

}

       <input type="button" value="botão sticky" class="botao" onclick=botaoSticky(event)>

meu sticky

Comment: Falta o índice da classe do elemento. Se for o primeiro, seria: `document.getElementsByClassName('sticky')[0]`
Answer: O método .getElementsByClassName retorna uma lista de elementos, tipo array, e não um elemento só. Para isso funcionar deves fazer document.getElementsByClassName('sticky')[0] ou document.querySelector('.sticky')

const botaoSticky = function(e) {
  e.target.style.display = "none";
  aparecerSticky();
}

const aparecerSticky = function() {
  const mySticky = document.querySelector('.sticky');
  mySticky.style.display = "block";
}
.sticky {
  display: none;
  background: rgb(42, 197, 211);
  height: 30vh;
  width: 90%;
  margin-top: 20px;
}

  <input type="button" value="botão sticky" class="botao" onclick=botaoSticky(event)>

meu sticky

Comment: caramba é mesmo tinha esquecido desse detalhe ksksks vlw maninho sz

como alterar o display da div pelo js?


IntentService doesn't get called in combination with PendingIntent

Question: I am trying to make an Android app about Geofencing. I have followed the tutorial on developers page and the sample code, however it doesn't work as expected. I could have maybe made some failures regarding the life cycle of an Android app since I combined Location Addresses and Geofencing.
My question is specifically why I don't get onHandleIntent called in the IntentService in the GeofenceTransitionIntentService.java class. Is there any error in the getGeofencePending function in MainActivity.java.
Here is the code:
package com.example.geofence;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.Builder;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.internal.mr;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingRequest;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.model.LatLng;

import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;

public class MainActivity extends Activity implements OnClickListener,
ConnectionCallbacks, OnConnectionFailedListener, LocationListener, ResultCallback <Status>{

    protected static final String TAG = "creating-and-monitoring-geofences";
    GoogleApiClient mGoogleApiClient;
    LocationRequest mLocationRequest;
    Boolean mRequestingLocationUpdates = true;
    Boolean mAddressRequested = true;
    String mAddressOutput = null;
    Location mCurrentLocation;
    protected Location mLastLocation;
    private AddressResultReceiver mResultReceiver;
    String mLastUpdateTime;
    TextView latitudeText, longitudeText, lastUpdateTimeText, locationAddress;
    private SharedPreferences mSharedPreferences;

    private Button mAddGeofencesButton, mRemoveGeofencesButton;
    protected ArrayList<Geofence> mGeofenceList = null;
    private PendingIntent mGeofencePendingIntent;
    private boolean mGeofencesAdded;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Toast.makeText(this, "OnCreate", Toast.LENGTH_SHORT).show();
        setContentView(R.layout.activity_main);
        mResultReceiver = new AddressResultReceiver(new Handler());
        latitudeText = (TextView) findViewById(R.id.latitudeText);
        longitudeText = (TextView) findViewById(R.id.longitudeText);
        lastUpdateTimeText = (TextView) findViewById(R.id.lastUpdateText);
        locationAddress = (TextView) findViewById(R.id.locationAddressText);

        mAddGeofencesButton = (Button) findViewById(R.id.add_geofence);
        mRemoveGeofencesButton = (Button) findViewById(R.id.remove_geofence);
        mGeofenceList = new ArrayList<Geofence>();
        mGeofencePendingIntent = null;
        mSharedPreferences = getSharedPreferences(Constants.SHARED_PREFERENCES_NAME,
                MODE_PRIVATE);
        mGeofencesAdded = mSharedPreferences.getBoolean(Constants.GEOFENCES_ADDED_KEY, false);

        mAddGeofencesButton.setOnClickListener(this);
        mRemoveGeofencesButton.setOnClickListener(this);

        setButtonsEnabledState();
        buildGoogleApiClient();
    }

    protected void startIntentService() {
        Intent intent = new Intent(this, FetchAddressIntentService.class);
        intent.putExtra(Constants.RECEIVER, mResultReceiver);
        intent.putExtra(Constants.LOCATION_DATA_EXTRA, mLastLocation);
        startService(intent);
    }
    /*
    @Override
    protected void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putBoolean(REQUESTING_LOCATION_UPDATES_KEY,
                mRequestingLocationUpdates);
        savedInstanceState.putParcelable(LOCATION_KEY, mCurrentLocation);
        savedInstanceState.putString(LAST_UPDATED_TIME_STRING_KEY, mLastUpdateTime);
        super.onSaveInstanceState(savedInstanceState);
    }
    */

    @Override
    public void onClick(View v) {
        if(v==mAddGeofencesButton){
            addGeofencesButtonHandler(v);
        }
        if(v==mRemoveGeofencesButton){
            removeGeofencesButtonHandler(v);
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        stopLocationUpdates();
    }

    @Override
    public void onResume() {
        super.onResume();
        Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
        if (mGoogleApiClient.isConnected() && !mRequestingLocationUpdates) {
            startLocationUpdates();
        }
    }

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(LocationServices.API)
        .build();
        mGoogleApiClient.connect();

    }
    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "onConnectionFailed", Toast.LENGTH_SHORT).show();

    }
    @SuppressLint("NewApi")
    @Override
    public void onConnected(Bundle connectionHint) {
        Toast.makeText(this, "onConnected", Toast.LENGTH_SHORT).show();
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            String latitude = String.valueOf(mLastLocation.getLatitude());
            String longitude = String.valueOf(mLastLocation.getLongitude());
            latitudeText.setText("latitude: " + latitude);
            longitudeText.setText("longitude: " + longitude);
        }
        if(mRequestingLocationUpdates){
            createLocationRequest();
            startLocationUpdates();
        }
        if (mLastLocation != null) {
            // Determine whether a Geocoder is available.
            if (!Geocoder.isPresent()) {
                Toast.makeText(this, "No geocoder available",
                        Toast.LENGTH_SHORT).show();
                return;
            }

            if (mAddressRequested) {
                startIntentService();
            }
        }

    }
    @Override
    public void onConnectionSuspended(int cause) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "onConnectionSuspended", Toast.LENGTH_SHORT).show();
    }

    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10000);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }

    protected void startLocationUpdates() {
        LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient, mLocationRequest, this); //this refers to the interface LocationListener
    }

    protected void stopLocationUpdates() {
        LocationServices.FusedLocationApi.removeLocationUpdates(
                mGoogleApiClient, this);
    }

    @Override
    public void onLocationChanged(Location location) {
        mCurrentLocation = location;
        mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
        updateUI();
    }

    public void updateUI() {
        latitudeText.setText(String.valueOf(mCurrentLocation.getLatitude()));
        longitudeText.setText(String.valueOf(mCurrentLocation.getLongitude()));
        lastUpdateTimeText.setText(mLastUpdateTime);
        locationAddress.setText(mAddressOutput);
    }

    public void showToast(String text){
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }

    class AddressResultReceiver extends ResultReceiver {
        public AddressResultReceiver(Handler handler) {
            super(handler);
        }

        @Override
        protected void onReceiveResult(int resultCode, Bundle resultData) {
            showToast("onReceiveResult");
            // Display the address string
            // or an error message sent from the intent service.
            mAddressOutput = resultData.getString(Constants.RESULT_DATA_KEY);
            //displayAddressOutput();
            System.out.println(mAddressOutput);
            Log.e("RESULT!!!", mAddressOutput);
            locationAddress.setText(mAddressOutput);

            // Show a toast message if an address was found.
            if (resultCode == Constants.SUCCESS_RESULT) {
                ;
            }
        }
    }

    /**
    * Adds geofences, which sets alerts to be notified when the device enters or exits one of the
    * specified geofences. Handles the success or failure results returned by addGeofences().
    */
   public void addGeofencesButtonHandler(View view) {
       if (!mGoogleApiClient.isConnected()) {
           Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show();
           return;
       }

       try {
           LocationServices.GeofencingApi.addGeofences(
                   mGoogleApiClient,
                   // The GeofenceRequest object.
                   getGeofencingRequest(),
                   // A pending intent that that is reused when calling removeGeofences(). This
                   // pending intent is used to generate an intent when a matched geofence
                   // transition is observed.
                   getGeofencePendingIntent()
           ).setResultCallback(this); // Result processed in onResult().
       } catch (SecurityException securityException) {
           // Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission.
           logSecurityException(securityException);
       }
   }

   /**
    * Removes geofences, which stops further notifications when the device enters or exits
    * previously registered geofences.
    */
   public void removeGeofencesButtonHandler(View view) {
       if (!mGoogleApiClient.isConnected()) {
           Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show();
           return;
       }
       try {
           // Remove geofences.
           LocationServices.GeofencingApi.removeGeofences(
                   mGoogleApiClient,
                   // This is the same pending intent that was used in addGeofences().
                   getGeofencePendingIntent()
           ).setResultCallback(this); // Result processed in onResult().
       } catch (SecurityException securityException) {
           // Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission.
           logSecurityException(securityException);
       }
   }

   private void logSecurityException(SecurityException securityException) {
       Log.e(TAG, "Invalid location permission. " +
               "You need to use ACCESS_FINE_LOCATION with geofences", securityException);
   }

    /**
     * Gets a PendingIntent to send with the request to add or remove Geofences. Location Services
     * issues the Intent inside this PendingIntent whenever a geofence transition occurs for the
     * current list of geofences.
     *
     * @return A PendingIntent for the IntentService that handles geofence transitions.
     */
    private PendingIntent getGeofencePendingIntent() {
        // Reuse the PendingIntent if we already have it.
        if (mGeofencePendingIntent != null) {
            Toast.makeText(this, "mGeofencePendingIntent != null", Toast.LENGTH_SHORT).show();
            return mGeofencePendingIntent;
        }
        Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
        // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
        // addGeofences() and removeGeofences().
        Toast.makeText(this, "mGeofencePendingIntent == null", Toast.LENGTH_SHORT).show();
        mGeofencePendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return mGeofencePendingIntent;
    }

    /**
     * Builds and returns a GeofencingRequest. Specifies the list of geofences to be monitored.
     * Also specifies how the geofence notifications are initially triggered.
     */
    private GeofencingRequest getGeofencingRequest() {

        GeofencingRequest.Builder builder = new GeofencingRequest.Builder();

        // The INITIAL_TRIGGER_ENTER flag indicates that geofencing service should trigger a
        // GEOFENCE_TRANSITION_ENTER notification when the geofence is added and if the device
        // is already inside that geofence.
            builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);

        // Add the geofences to be monitored by geofencing service.
        builder.addGeofences(mGeofenceList);
        builder.addGeofence(new Geofence.Builder()
        .setRequestId("CurrentLocation")
        .setCircularRegion(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude(), 50)
        .setExpirationDuration(Geofence.NEVER_EXPIRE)
        .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
        .build());

        Toast.makeText(this, "Geofence request added",  Toast.LENGTH_SHORT).show();

        // Return a GeofencingRequest.
        return builder.build();
    }

    @Override
    public void onResult(Status status) {
        if (status.isSuccess()) {
            // Update state and save in shared preferences.
            mGeofencesAdded = !mGeofencesAdded;
            SharedPreferences.Editor editor = mSharedPreferences.edit();
            editor.putBoolean(Constants.GEOFENCES_ADDED_KEY, mGeofencesAdded);
            editor.commit();

            // Update the UI. Adding geofences enables the Remove Geofences button, and removing
            // geofences enables the Add Geofences button.
            setButtonsEnabledState();

            Toast.makeText(
                    this,
                    getString(mGeofencesAdded ? R.string.geofences_added :
                            R.string.geofences_removed),
                    Toast.LENGTH_SHORT
            ).show();
        } else {
            // Get the status code for the error and log it using a user-friendly message.
            String errorMessage = GeoFenceErrorMessages.getErrorString(this,
                    status.getStatusCode());
            Log.e(TAG, errorMessage);
        }
    }

     /**
     * Ensures that only one button is enabled at any time. The Add Geofences button is enabled
     * if the user hasn't yet added geofences. The Remove Geofences button is enabled if the
     * user has added geofences.
     */
    private void setButtonsEnabledState() {
        if (mGeofencesAdded) {
            mAddGeofencesButton.setEnabled(false);
            mRemoveGeofencesButton.setEnabled(true);
        } else {
            mAddGeofencesButton.setEnabled(true);
            mRemoveGeofencesButton.setEnabled(false);
        }
    }
}

and the GeofenceTransitionIntentService class, that should deal with the triggering event
package com.example.geofence;

import java.util.ArrayList;
import java.util.List;

import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingEvent;

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

public class GeofenceTransitionsIntentService extends IntentService{

    protected static final String TAG = "geofence-transitions-service";

    public GeofenceTransitionsIntentService(String name) {
        super("name");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Toast.makeText(this, "OnHandleIntent", Toast.LENGTH_SHORT).show();
        Log.e(TAG, "OnHandleIntent");
        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
        if (geofencingEvent.hasError()) {
            String errorMessage = GeoFenceErrorMessages.getErrorString(this,
                    geofencingEvent.getErrorCode());
            Log.e(TAG, errorMessage);
            return;
        }
        Log.e("DEBUG", "No geofencingEvent error");

         // Get the transition type.
        int geofenceTransition = geofencingEvent.getGeofenceTransition();
        Log.e("DEBUG", "getGeofenceTransition has been called");
        // Test that the reported transition was of interest.
        if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
                geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
            Log.e("DEBUG", "Geofence Transition enter or exit");
            // Get the geofences that were triggered. A single event can trigger multiple geofences.
            List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();

            // Get the transition details as a String.
            String geofenceTransitionDetails = getGeofenceTransitionDetails(
                    this,
                    geofenceTransition,
                    triggeringGeofences
            );

            // Send notification and log the transition details.
            sendNotification(geofenceTransitionDetails);
            Log.i(TAG, geofenceTransitionDetails);
        } else {
            // Log the error.
            Log.e(TAG, getString(R.string.geofence_transition_invalid_type, geofenceTransition));
        }
    }

    /**
     * Gets transition details and returns them as a formatted string.
     *
     * @param context               The app context.
     * @param geofenceTransition    The ID of the geofence transition.
     * @param triggeringGeofences   The geofence(s) triggered.
     * @return                      The transition details formatted as String.
     */
    private String getGeofenceTransitionDetails(
            Context context,
            int geofenceTransition,
            List<Geofence> triggeringGeofences) {
        Log.e("DEBUG", "getGeofenceTransitionDetails");
        String geofenceTransitionString = getTransitionString(geofenceTransition);

        // Get the Ids of each geofence that was triggered.
        ArrayList triggeringGeofencesIdsList = new ArrayList();
        for (Geofence geofence : triggeringGeofences) {
            triggeringGeofencesIdsList.add(geofence.getRequestId());
        }
        String triggeringGeofencesIdsString = TextUtils.join(", ",  triggeringGeofencesIdsList);

        return geofenceTransitionString + ": " + triggeringGeofencesIdsString;
    }

    /**
     * Posts a notification in the notification bar when a transition is detected.
     * If the user clicks the notification, control goes to the MainActivity.
     */
    private void sendNotification(String notificationDetails) {
        // Create an explicit content Intent that starts the main Activity.
        Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
        Log.e("DEBUG", "sendNotification");
        // Construct a task stack.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

        // Add the main Activity to the task stack as the parent.
        stackBuilder.addParentStack(MainActivity.class);

        // Push the content Intent onto the stack.
        stackBuilder.addNextIntent(notificationIntent);

        // Get a PendingIntent containing the entire back stack.
        PendingIntent notificationPendingIntent =
                stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        // Get a notification builder that's compatible with platform versions >= 4
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        // Define the notification settings.
        builder.setSmallIcon(R.drawable.ic_launcher)
                // In a real app, you may want to use a library like Volley
                // to decode the Bitmap.
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                        R.drawable.ic_launcher))
                .setColor(Color.RED)
                .setContentTitle(notificationDetails)
                .setContentText(getString(R.string.geofence_transition_notification_text))
                .setContentIntent(notificationPendingIntent);

        // Dismiss notification once the user touches it.
        builder.setAutoCancel(true);

        // Get an instance of the Notification manager
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Issue the notification
        mNotificationManager.notify(0, builder.build());
    }

    /**
     * Maps geofence transition types to their human-readable equivalents.
     *
     * @param transitionType    A transition type constant defined in Geofence
     * @return                  A String indicating the type of transition
     */
    private String getTransitionString(int transitionType) {
        Log.e("DEBUG", "getTransitionString");
        switch (transitionType) {
            case Geofence.GEOFENCE_TRANSITION_ENTER:
                return getString(R.string.geofence_transition_entered);
            case Geofence.GEOFENCE_TRANSITION_EXIT:
                return getString(R.string.geofence_transition_exited);
            default:
                return getString(R.string.unknown_geofence_transition);
        }
    }

}

The manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.geofence"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET" />

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name=".FetchAddressIntentService"
            android:exported="false"/>
        <service android:name=".GeofenceTransitionsIntentService"/>
    </application>

</manifest>

Comment: are you adding your service in the Manifest?
Comment: I have added the service in Manifest
Comment: I just solved the problem by removing the parameter of the constructor of GeofenceTransitionIntentService.java
Answer: I just solved the problem by removing the parameter of the constructor of GeofenceTransitionIntentService.java


Style a Bootstrap 4 Checkbox without label

Question: I'm trying to style a Bootstrap 4 checkbox without a label:

    <input class="form-check-input position-static" type="checkbox" id="blankCheckbox" value="option1">

My trial and error styles:
  .form-check {
      .form-check-input {
        color: #2c2b2c;
        font-size: 12px;
        line-height: 2.7;
        padding-left:15px;
        text-transform: uppercase;
        background-color: red;
      }
      .form-check-input::after,
      .form-check-input::before {
        height: 25px;
        width: 25px;
      }
      .form-check-input::before {
        background-color: #fff;
        border: 1px solid #2c2b2c;
      }
    }
  }

I'm able to style the label version, but unsuccessful with this version.
UPDATE: As @mebin-joe mentioned, this is a known issue: https://github.com/twbs/bootstrap/issues/26221
I ended up using the custom Bootstrap checkbox element and styled it:
.custom-checkbox .custom-control-input:checked~.custom-control-label::before
{
    background-color: #caca4c;
}
.custom-control-input:checked~.custom-control-label::before {
    color: #fff;
    background-color: #caca4c;
}

Comment: :before and :after are pseudo elements which wont work for Self-Closing tags like input.
Comment: This is a known issue. Some workarounds are discussed here https://github.com/twbs/bootstrap/issues/26221
Comment: basically input fields don't support `:before` and `:after`
Comment: @melissaSkywalkz i saw that question, but mine is regarding Bootstrap.
Comment: Your question has already been asked before,
[you can find the answer here](https://stackoverflow.com/questions/4148499/how-to-style-a-checkbox-using-css).
Answer: It seems that with bootstrap 4.5.0 following works

  XXX
  <input class="custom-control-input" aria-labelledby="N1" type="radio" value="XX">

or depending of the need just

  XXX
  <input id="N1" class="custom-control-input" type="radio" value="XX">

Answer: Please use the following Codes it will work:

<input
    type="radio"
    name="quest-27"
    id="quest-27-10"
    class="custom-control-input"
    value="1">

    Never


Using behat to create nodes and testing user owns them

Question: I would like to create a Behat test that confirms a user can only see content they own or are referenced in.  The struggle is that I can't figure out how to assign the content to created user.
Starting with something like:
Given users:
| name | status |
| test | 1      |
Given "Page" content:
| title | uid |
| A title | ?? |

I need to create several nodes to test, and it seems...odd... that I would need to do the entire steps (e.g. Then I go to register, then I go to node/add/page, ....).
How do I know the uid of the user created in the Given?
Comment: Are you trying to add content as a fixture/mock to test something else? What driver are you using?
Comment: @mradcliffe I'm testing that a user sees a list of content that a) they own or b) they have been referenced in.  I just found http://nuvole.org/blog/2015/mar/24/pluggable-field-handling-system-behat-drupal-extension which seems to be what I need (user "author")
Answer: In the example in features/api.feature you'll find how to do this:
  Scenario: Create nodes with specific authorship
    Given users:
    | name     | mail            | status |
    | Joe User | firstname.lastname@example.com | 1      |
    And "article" nodes:
    | title          | author   | body             | promote |
    | Article by Joe | Joe User | PLACEHOLDER BODY | 1       |
    When I am logged in as a user with the "administrator" role
    And I am on the homepage
    And I follow "Article by Joe"
    Then I should see the link "Joe User"


Can I play Musical Saw with a Double Bass bow?

Question: I am going to buy a musical saw soon, and I was wondering if I could use my double bass bow to play it.
Q: Can I use my double bass bow to play the musical saw or do I have to get a new bow?
Answer: Yes, you can use a bass bow.
Not sure why Tim above said that a cello bow is longer than a violin one - it's the other way round, which is why a cello bow is better than a violin bow for saw playing.
Not sure why the person in the 1st answer thinks long notes would be difficult with a shorter bow - most sawists don't bow continuously like violinists - you bow then release immediately, letting the blade resonate for however long is needed.
For more info and for recommendations about best saw to buy - e-mail me through www.SawLady.com
I know all the above because I am
Answer: Standard appears to be a violin bow, but a longer cello or string bass bow would do. Personally, I'd keep my bass bow for that instrument, and have a dedicated saw bow. Not sure how much wear and tear on the hair it produces, but having a second bow to your string has always been thought of as something good...
Answer: Short answer is yes. I've heard the saw played with bows of all sizes, and in one concert, with a wooden dowel (just add rosin).
In my experience, a saw takes more physical pressure than a violin if you want a good tone. The shorter length of the bass and cello bows is an advantage for this, because the further you get from where you are gripping the bow, the harder it is to sustain even pressure. The downside, of course, is that longer notes become more difficult because your bow doesn't have as much length. I'd start out with whatever bow you have, or with a selection of wooden dowels in whatever lengths you want to try.


"Rotating" a ArrayList

Question: I have an ArrayList in the board game-like game I am developing in Java, I format this ArrayList as a square, as so (numbers being indexs);
<code>/*
 *  |0 |1 |2 |3 |
 *  |4 |5 |6 |7 |
 *  |8 |9 |10|11|
 *  |12|13|14|15|
 */
</code>
What I want to do is take this ArrayList and rotate it;
e.g turn() would output (based on the original index)
<code>/*
 *  |0 |4 |8 |12|
 *  |1 |5 |9 |13|
 *  |2 |6 |10|14|
 *  |3 |7 |11|15|
 */
</code>
My current approach is breaking the ArrayList in to rows with a getRow() function i wrote and manually writing the rows back
Comment: This is called transposition

See: http://stackoverflow.com/questions/2941997/how-to-transpose-listlist
Comment: Do you have ArrayList of ArrayList?
Comment: Just a note, the values can be "null"
Comment: so you have 16 values in an ArrayList?
Comment: I would a) use a 2D array and b) flip the index values
Answer: <code>for(int i = 0; i < 4; ++i){
        for(int j = i; j < 4; ++j){
            int t1 = al.get(i).get(j);
            al.get(i).set(j, al.get(j).get(i));
            al.get(j).set(i, t1);
        }
    }
</code>
it's called matrix transpose.

"Rotating" a ArrayList


find relationships in a many-to-many structure with sql

Question: my question is near a parent-child problem, and may need some recursive query, but i didn't find any answers by browsing forums.
here is my problem: I've got 3 tables:

T1 (people)      T2 (places)  T3 (relationship betwenn A and B)
-------         ------      --------
id1 (pk)        id2 (pk)    id3 (pk)
name            city        id_A
                            id_B

I would like to identify groups of places and people that are related.
For example, if
John visits London and Paris,
Mary visits Paris and New York,
Peter visits Bangalore and Tokyo,
I would like to affect the same group code to Mary, John, Paris, London and New York, and another group code to Peter, Tokyo and Bangalore.
I really don't know how to do this with sql. Any Idea?
Thanks
Answer: This problem is "finding isolated subgraphs".
It's quite a simple problem, though it cannot be efficiently solved with a single SQL query.
It's quite easy to write a simple stored procedure:

Create a staging table:
group_id   city_id

For each city, find group_id's of all its neighbors (all other cities the city's visitors have also visited)
If the neighbors belong to different group_id's, update the staging table, setting all the group_id's to the least one of the set.
Insert the city with the new group_id into the staging table.
Comment: Hello,
Thanks for your quick answer, though it's not so easy for me to write the procedure.
I perfectly understand your method, but i'm struggling to write the stuff.
I'll be very pleased if you have more advices on the specific functions to use in the procedure.
Thanks again


Como ordenar por vários campos com Ionic e/ou AngularJS de um JSON?

Question: Estou tentando ordenar campos dinamicamente pelo Ionic e o AngularJs. Comecei usando o plugin Modal Select, com o botão assim:
<button class="button button-stable button-block icon-left ion-android-restaurant" modal-select="" ng-model="someModel" options="selectableNames" option-property="role" modal-title="Ordenar por..." header-footer-class="bar-assertive">Ordenar

{{option.name}}

</button>

E o controller assim:
// ORDENA POR...
$scope.selectableNames = [
    {
        name: "Por preço: Do Menor para o Maior",
        role: "+cadastra_oferta_valor_com_desconto"
    },
    {
        name: "Por preço: Do Maior para o Menor",
        role: "-cadastra_oferta_valor_com_desconto"
    },
    {
        name: "Por Maior Desconto (%)",
        role: "-cadastra_oferta_desconto"
    },
    {
        name: "Menor Prazo de Entrega",
        role: "+fornecedor_configura_frete_prazo_entrega_min"
    },
    {
        name: "Oferta em Ordem Alfabética",
        role: "+cadastra_oferta_titulo_promocao"
    },
    //...
];

Colocando meu filter na view desta forma:

Ordena até bem, mas os campos de preços que são:
{
    name: "Por preço: Do Menor para o Maior",
    role: "+cadastra_oferta_valor_com_desconto"
},
{
    name: "Por preço: Do Maior para o Menor",
    role: "-cadastra_oferta_valor_com_desconto"
},
// ...

Quanto tem centavo, ele não ordena direito. O que é curioso, pois o campo "desconto" que tem o mesmo formato que os campos valores ele ordena bem.
{
    name: "Por Maior Desconto (%)",
    role: "-cadastra_oferta_desconto"
},

Já tentei colocar | currency no "role" mas não aceita. Então quero fazer esta ordenação de forma diferente, mas não sei como e não encontrei nada na Internet. Teria que ser uma modal para filtrar estes campos. O meu JSON pode ser acessado aqui.
Como posso criar uma ordenação com diversos campos e tipos utilizando "Modal" e este meu JSON. Não precisando, necessariamente, utilizar este plugin mencionado no inicio desta questão.
Desde já agradeço.
Comment: Você pode disponibilizar um Codepen com o seu código? Aqui tem um modelo do Modal Select para você usar: http://codepen.io/bianchimro/pen/epYYQO?editors=101
Answer: Com Angular, primeiramente, recomendo que NÃO utilize a ordenação (ou qualquer outro tipo de filtro) através do filter, especialmente dentro de um ngRepeat, o impacto em performance é realmente grande quando se trabalha com listas extensas.
Quanto ao problema propriamente dito, não sei como você está utilizando mais de um campo, mas o correto é efetuar a declaração através de uma array, por exemplo:
orderBy: ['nome', '-idade', 'ativo']

// No seu exemplo
ng-repeat="item in ofertass | orderBy:['cadastra_oferta_desconto', 'currency']

Nota: Para declaração em ordem crescente, não há necessidade da utilização de +, apenas o nome do campo.

Então, unificando as duas soluções que acabei de passar, o ideal seria ordenar a lista utilizando o filtro $filter, dentro do seu controller (ou service), recomendo que você utilize um código nesse estilo:
// HTML
<select ng-model="campoOrdena" ng-change="ordenarLista()">
    <option value="nome">Nome</option>
    <option value="-valor">Valor</option>
</select>

// AngularJs
$scope.ofertas = [...]; //Sua array com os objetos a serem ordenados

$scope.ordenarLista = function() {
    // Explicando: $filter('orderBy')( "lista a ser ordenada", "campos para ordenar");
    // Ex.: $filter('orderBy')( $scope.ofertas, ['nome', 'valor', '-idade']);

    $scope.ofertas = $filter('orderBy')($scope.ofertas, $campoOrdena); // Lista com a nova ordem
}

Outro ponto que notei agora, para que você possa ordenar corretamente com valores (sejam eles monetários ou apenas numéricos), é importante que o campo seja do tipo int, caso contrário ele não terá consistência nos resultados, o que pode ser o seu caso, visto que o seu JSON está retornando o campo desconto como string.
Essa é a ideia geral, com isso acho que você terá o conhecimento para adaptar o seu código para utilizar a solução proposta. Se ainda tiver dúvidas, deixe um comentário que tentarei ajudar!
Comment: Obrigado @CelsomTrindade. Funcionou perfeito. Valeu!


Searching in a binary search tree - Explanation

Question: Suppose that we have numbers between 1 and 100 in a binary search tree and want to search for the number 55. Which of the following sequences CANNOT be the sequence of nodes examined?
<code>A) 10, 75, 64, 43, 60, 57, 55

B) 90, 12, 68, 34, 62, 45, 55

C) 9, 85, 47, 68, 43, 57, 55

D) 79, 14, 72, 56, 16, 53, 55
</code>
Comment: I don't get your post, the explanation is on the link you posted. Explanation: In BST on right side of parent number should be greater than it, but in C after 47, 43 appears that is wrong.
Answer: Answer: C
Compare the key (55) with each element of the sequence.
a. is 55 greater than or less than 9 ?   -> it is bigger so go to the right and write 85
b. is 55 greater than or less than 85 ? -> it is smaller so go to the left and write 47
c. is 55 greater than or less than 47 ? -> it is bigger so go to the right 68
d. is 55 greater than or less than 68 ? -> it is smaller so go to the left and write 43
43 violate the binary search tree property
 (left child is smaller than the parent and right child is bigger than the parent)
43 is on the right subtree of 9        CORRECT
43 is on the left subtree of 85        CORRECT
43 is on the RIGHT subtree of 47      ****ERROR*****


Derivative of $ h(t)= \sin (\cos^{-1}t$)?

Question: Can someone please explain the steps/rules I need to preform to find the derivative of
$h(t)= \sin (\cos^{-1}t)$?
I tried to used the product rule, and realized it was obviously a failure.
Using the chain rule I get:
$\frac{dy}{dx}= \cos(\cos^{-1}t)\frac{-t}{\sqrt{1-t^2}}$
I know I am not finished, what am I supposed to do with this?
Comment: First, that should be dh/dt, not dy/dx. Secondly, why are you not finished? That's the derivative.
Comment: Well, the **only** thing you can apply here is the chain rule, of course: you have composition of two functions, **not** a product. You're indeed almost finished, just change that $\;-t\;$ to $\;-1\;$ and you're done.
Comment: Oops, I missed the mistake that @DonAntonio pointed out. It does need to be changed.
Comment: Okay, I see my mistake with the t needing to be 1, but I don't understand the rest of it because before coming here, I checked solutions to my book which showed: h(t)= sin (arccos t)= sqrt(1-t^2) therefore, h'(t)= [(1/2)(1-t^2)^(-1/2)][-2t]= -t/sqrt(1-t^2)
Comment: Yes, the $\;-t\;$ now appears **after** $\;\cos\arccos t=t\;$ , not *before*...
Comment: Thank you everyone for your help!
Answer: You can also make the observation that $h(t)=\sin(\cos^{-1}(t)) = \sqrt{1-t^2}$ and differentiate from there (do some trig to see where this observation comes from).
\begin{align}
(\sqrt{1-t^2})' = \frac{1}{2\sqrt{1-t^2}}\times(1-t^2)'=\frac{-2t}{2\sqrt{1-t^2}}=\frac{-t}{\sqrt{1-t^2}}
\end{align}
Answer: \begin{align}
h'(t) &= \frac{d}{dt}\sin(\arccos t) \\
&=\cos(\arccos t) \frac{d}{dt}(\arccos t) & \text{chain rule}\\
&=\cos(\arccos t) \cdot -\frac{1}{\sqrt{1-t^2}} & \text{$\frac{d}{dt}(\arccos t)=-\frac{1}{\sqrt{1-t^2}}$}\\
&=t \cdot -\frac{1}{\sqrt{1-t^2}} & \text{$\cos(\arccos t)=t$}\\
&= \frac{-t}{\sqrt{1-t^2}}
\end{align}
Answer: Use the chain rule:
$$\begin{align}h'(t) &= \cos(\cos^{-1}t)\cdot\frac{d}{dt}(\cos^{-1}t)\\
&=t\cdot\frac{-1}{\sqrt{1-t^2}}\\
&=\frac{-t}{\sqrt{1-t^2}}\end{align}$$
Answer: HINT:
If $\displaystyle\cos^{-1}t=u,\cos u =t$
As the Principal values of inverse cosine ratio lies in $\displaystyle\left[0,\pi\right]$
$\displaystyle\sin(\cos^{-1}t)=\sin u\ge0\implies \sin u=+\sqrt{1-\cos^2u}=\sqrt{1-t^2}$
Comment: This is exactly what I wasn't understanding from my book as it doesn't explain what changes are being made. I didn't make this connection, Thanks :)
Comment: For what it's worth, compositions of the form $\operatorname{trig}_1 \left( \operatorname{trig}_2^{-1} (t) \right)$, where $\operatorname{trig}_1$ and $\operatorname{trig}_2$ are any of the six trigonometric functions, are always equivalent to rational expressions, whose numerators and denominators are expressions of the form $1$, $t$, $\sqrt{1 + t^2}$, or $\sqrt{1 - t^2}$.  I call these *Pythagorean* expressions, as they form the labels of the sides of a right triangle.
Comment: The previous comment is for the OP.
Answer: If you don't like the function composition, we can also re-arrange this as
$ \ \arcsin  (h) \ = \ \arccos (t) \ \ $ and use implicit differentiation (along with the known derivatives of these inverse trigonometric functions) to write
$$ \frac{d}{dt} \ [\arcsin (h)] \ = \ \frac{d}{dt} \ [\arccos (t)]   $$
$$ \Rightarrow \ \ \frac{1}{\sqrt{1 \ - \ h^2}} \ \cdot \ \frac{dh}{dt} \ = \ -\frac{1}{\sqrt{1 \ - \ t^2}}  \ \  \Rightarrow \ \ \frac{dh}{dt} \ = \ -\frac{\sqrt{1 \ - \ h^2}}{\sqrt{1 \ - \ t^2}} \ \ . $$
But since $ \ h(t) \ = \ \sin (\arccos (t)) \ , $ we can apply the Pythagorean Identity to write (with appropriate domain restrictions)
$$ \frac{dh}{dt} \ = \ -\frac{\cos (\arccos (t))}{\sqrt{1 \ - \ t^2}} \ = \ -\frac{t}{\sqrt{1 \ - \ t^2}} \ \ . $$


How to remove the blue box shadow border in button if clicked

Question: I want to do is remove the button blue box shadow effect in my class btnd if the button is click.
current output:
i tried this but it doesnt work.
.btnd:active,
.btnd.active {
  background-image: none;
  outline: 0;
  -webkit-box-shadow: none;
          box-shadow: none;
}

Comment: Just add `outline: 0;` for `.btnd:focus` as explained here. http://stackoverflow.com/questions/20340138/remove-blue-border-from-css-custom-styled-button-in-chrome#answer-21758143
Answer: Blue shadow is browser default :focus state
.btnd:active,
.btnd:focus,
.btnd:focus:active {
  background-image: none;
  outline: 0;
  box-shadow: none;
}

Comment: Although this answer is technically correct, I am surprised that no one has mentioned that removing the outline is not recommended for accessibility reasons. See http://www.outlinenone.com/ or https://a11yproject.com/posts/never-remove-css-outlines/.
Answer: I deal with this problem just yesterday. You need:
.btnd:focus, .btnd:active, .btnd.active, .btnd:focus:active {
  background-image: none;
  outline: none;
  -webkit-box-shadow: none;
  box-shadow: none;
}

The key is in last selector .btnd:focus:active.
Comment: The .btnd:focus:active prevents the outline that appears for a split second when clicked. +1, well done
Answer: just use this line to remove the focus
$( "#OK" ).button().blur();


Listing Tweets from a protected account?

Question: I have a protected twitter account but I want to be able to show a list of my most recent tweets on my website.
I'm prepared to dive in to the twitter docs but I just wanted a heads up to know if its possible?
Would my script have to autenticate with my account or is there a way to allow access to my twitter account from a particular IP?
Thanks
Answer: Yes, definitely possible. I don't know if there is a simpler solution, but one way would be to create a new app on Twitter which your account authorizes. In the authorization process, Twitter will give you access tokens for your specific account. Store your access tokens in a database. Read your tweets from a PHP widget say, which uses the Twitter PHP API, and uses your access tokens to display it.
When you are retrieving your tweets, it doesn't matter if it is protected or not as long as you have authorized that specific application.
Simply put, the widget that displays your tweets is a Twitter application that you have authorized. (one-time authentication)
Answer: Well I don't think so you can read anything from Protected account.
I was reading few articles about the same issue and here is the alternative solution if you would like to give a try. (I personally didn't tried it so leaving up to you to give shot)
http://www.ehow.com/how_6474863_protected-twitter-updates.html


Find High CPU Processes in Azure Log Analytics

Question: I have created the following alert in Log Analytics. The alert is designed to get all computers where the _Total CPU usage was over 90% in the last ten minutes. However, when I run the alert I get the error:
'join' operator: Failed to resolve table or column expression named 'FindCPU'
Can someone let me know what might be required to fix this issue?
<code>//Find Top processes utilizing CPU
// by finding the machine(s) using over 90% of CPU
// then finding the processes using the CPU
// also finding CPU count of the machines to find the actual percentage of CPU being used

//defining our CPU threshold
let CPUThreshold = 90;

//define time sample rate
let Time = 10m;

//define Count of processes to return
let Count = 5;

//Find instances of total cpu being used above 90% over the last 10 minutes
let TopCPU = Perf
| where TimeGenerated > now(-Time)
              and ObjectName == "Processor"
              and CounterName == "% Processor Time"
              and InstanceName == "_Total"
              and CounterValue > CPUThreshold
| project Computer, ObjectName
              , CounterName, CounterValue
              , TimeGenerated;
//end query

// find top Processes, excluding _Total and Idle instances, there may be other instances you want to exclude as well
let TopProcess = Perf
| where TimeGenerated > now(-Time)
               and CounterName == "% Processor Time"
               and InstanceName != "_Total"
               and InstanceName != "Idle"
| project Computer, ObjectName
              , CounterName, InstanceName
              , CounterValue, TimeGenerated;
// end query

// find CPU count for servers(s)
let FindCPU = Perf
| where TimeGenerated >= ago(1h)
| where ObjectName == "Processor"
              and CounterName == "% Processor Time"
              and InstanceName!="_Total"
| sort by InstanceName asc nulls first
| summarize CPUCount = dcount(InstanceName) by Computer;
// end query

//Join all 3 datasets together
FindCPU | join(TopCPU) on Computer
| join(TopProcess)on Computer
| extend PercentProcessorUsed = CounterValue1 / CPUCount
| summarize avg(PercentProcessorUsed) by Computer, ObjectName
                  , CounterName, CPUCount
                  , TotalCPU=CounterValue //rename CounterValue to TotalCPU
                  , Process=ObjectName1 //rename ObjectName1 to Process
                  , ProcessTime=CounterName1 //rename CounterName1 to ProcessTime
                  , ProcessName=InstanceName //rename InstanceName to ProcessName
                  , TimeGenerated
| where Process == "Process"
and avg_PercentProcessorUsed > 25 // only return processes that are using more than 25%
| top Count by avg_PercentProcessorUsed desc
| project Computer, CPUCount
                , ProcessName , avg_PercentProcessorUsed
                , TotalCPU, Process
                , ProcessTime, TimeGenerated
</code>
Thanks
Answer: The query runs fine (although seems a bit over complicated for what you are trying to do), however, you need to make sure that you select all of the queries when you run it.
By default, if your cursor is at the end of all the queries, Log Analytics will only run the last query, which obviously errors because it cannot find "FindCPU". Highlight everything, then run it and it works fine.


Pitchers winning consecutive Cy Young awards

Question: How many pitchers have won back-to-back Cy Young awards?
Answer: According to Wikipedia there have been 18 multiple Cy Young award winners.
9 of them won in consecutive years.

Roger Clemens (1986-1987, 1997-1998)
Randy Johnson (1999-2002)
Greg Maddux (1992-1995)
Sandy Koufax (1965-1966)
Pedro Martinez (1999-2000)
Jim Palmer (1975-1976)
Clayton Kershaw (2013-2014)
Tim Lincecum (2008-2009)
Denny McLain (1968-1969)


Check all bytes of a __m128i for a match of a single byte using SSE/AVX/AVX2

Question: I'm looking for efficient ways to compute the following function:
Input: __m128i data, uint8_t in;
Output: boolean indicating if any byte in data is in.
I'm essentially using them to implement a space/time efficient stack for bytes with capacity 8. My most efficient solution is to first compute a __m128i tmp with all bytes as in. Then check if any bytes in tmp\xor data is a zero byte.
Answer: Yes, AVX2 has efficient byte-broadcast.  SSSE3 pshufb with an all-zero mask is just as cheap, but you have to create the shuffle-control vector.  AVX512BW/F even has single-instruction vpbroadcastb/w/d/q  x/y/zmm, r32.  (With optional masking, so you can zero some or merge with an existing vector if you want, e.g. insert at a position using a single-bit mask.)
Fortunately, compilers know how to do this when implementing _mm_set1_epi8 so we can leave it to the compiler.
Then it just boils down to the usual pcmpeqb/pmovmskb to get an integer which will have a 1 bit for matching elements, which you can branch on.
// 0 for not found, non-zero for found.  (Bit position tells you where).
unsigned contains(__m128i data, uint8_t needle) {
    __m128i k = _mm_set1_epi8(needle);
    __m128i cmp = _mm_cmpeq_epi8(data, k);  // vector mask
    return _mm_movemask_epi8(cmp);          // integer bitmask
}

As you'd expect, all compilers make this use this asm (Godbolt)
contains(long long __vector(2), unsigned char):
    vmovd   xmm1, edi
    vpbroadcastb    xmm1, xmm1
    vpcmpeqb        xmm0, xmm0, xmm1
    vpmovmskb       eax, xmm0
    ret

Except MSVC, which wastes an instruction on movsx  eax, dl first.  (Windows x64 passes the 2nd arg in RDX, vs. x86-64 System V passing the first integer arg in RDI.)

Without AVX2, you'll get something like this with SSSE3 or above
# gcc8.3 -O3 -march=nehalem
contains(long long __vector(2), unsigned char):
    movd    xmm1, edi

    pxor    xmm2, xmm2
    pshufb  xmm1, xmm2         # _mm_shuffle_epi8(needle, _mm_setzero_si128())

    pcmpeqb xmm0, xmm1
    pmovmskb        eax, xmm0
    ret

Or with just SSE2 (baseline for x86-64):
contains(long long __vector(2), unsigned char):
    mov     DWORD PTR [rsp-12], edi
    movd    xmm1, DWORD PTR [rsp-12]    # gcc's tune=generic strategy is still store/reload  /facepalm
    punpcklbw       xmm1, xmm1          # duplicate to low 2 bytes
    punpcklwd       xmm1, xmm1          # duplciate to low 4 bytes
    pshufd  xmm1, xmm1, 0               # broadcast

    pcmpeqb xmm1, xmm0
    pmovmskb        eax, xmm1
    ret

Related:

How to compare two vectors using SIMD and get a single boolean result? and many duplicates
How can I count the occurrence of a byte in array using SIMD?
SIMD/SSE: How to check that all vector elements are non-zero (pxor+ptest+jcc = 4 uops vs. pcmpeqb+pmovmskb + macro-fused test/jcc = 3 uops.)
The indices of non-zero bytes of an SSE/AVX register (finding the match positions)
How to count character occurrences using SIMD (like memchr but counting matches instead of finding the first, using AVX2.  With efficient accumulation of the counts, and efficient horizontal sums.)
Comment: Nice! Didn't know there is a byte-broadcast instruction!


What is the autocovariance function of the Wold representation?

Question: Given the Wold representation:
$$X_t = \mu + \sum_{j=0}^{\inf} b_j \epsilon_{t-j}$$
Which is its autocovariance function?
I made some algebra and I ended up with: $$COV(X_s,X_t)=\sigma^2 \cdot \sum_{i=0}^{min(s,t)}b_i$$
Is it correct? Thanks.
Answer: Assume $s<t$. Then:
$$\text{Cov}(X_s, X_t) = \text{Cov}\left(\sum_{j=0}^\infty b_j \epsilon_{s-j},\sum_{j=0}^\infty b_j \epsilon_{t-j}\right) $$
$$ = \text{Cov}\left(b_0\epsilon_s+b_1\epsilon_{s-1}+\cdots,b_0\epsilon_t+b_1\epsilon_{t-1}+\cdots+b_{t-s}\epsilon_{t-(t-s)}+b_{t-s+1}\epsilon_{t-(t-s+1)}+\cdots\right)$$
$$= \text{Cov}\left(b_0\epsilon_s+b_1\epsilon_{s-1}+\cdots,b_{t-s}\epsilon_{s}+b_{t-s+1}\epsilon_{s-1}+\cdots\right)$$
$$=\sigma^2 \sum_{j=0}^\infty b_jb_{t-s+j}$$
If $s>t$, then we can follow an identical procedure and arrive at
$$\text{Cov}(X_s, X_t)=\sigma^2 \sum_{j=0}^\infty b_jb_{s-t+j}$$
Pulling these together, in general we have
$$\text{Cov}(X_s, X_t)=\sigma^2 \sum_{j=0}^\infty b_jb_{|t-s|+j}$$
Note that this also gives the expected answer if $t=s$, i.e.
$$\text{Cov}(X_t, X_t)= \sigma^2 \sum_{j=0}^\infty b_j^2= \text{Var}(X_t)$$


Getting "Failed to build synonyms" message when trying to build synonyms filter

Question: I'm using Elasticsearch 6.8 with python 3.7
I'm trying to create my own synonyms which refer to emoticons as text.
for example: ":-)" will refer as "happy-smiley".
I'm trying to build and create the synonyms and index with the following code:
def create_analyzer(es_api, index_name, doc_type):
    body = {
        "settings": {
                "index": {
                    "analysis": {
                        "filter": {
                            "synonym_filter": {
                                "type": "synonym",
                                "synonyms": [
                                    ":-), happy-smiley",
                                    ":-(, sad-smiley"
                                ]
                            }
                        },
                        "analyzer": {
                            "synonym_analyzer": {
                                "tokenizer": "standard",
                                "filter": ["lowercase", "synonym_filter"]
                            }
                        }
                    }
                }
            },
        "mappings": {
            doc_type: {
                "properties": {
                    "tweet": {"type": "text", "fielddata": "true"},
                    "existence": {"type": "text"},
                    "confidence": {"type": "float"}
                }
            }}
    }
    res = es_api.indices.create(index=index_name, body=body)

But I'm getting errors:
lasticsearch.exceptions.RequestError: RequestError(400, 'illegal_argument_exception', 'failed to build synonyms')

What is wrong and how can I fix it ?
Answer: I can say you whats wrong, (updated) how to fix this.
So if you will run this query in dev tools or bu cURL you will see the reason of error - think that Python cutting error details, so you cannot see reason.
PUT st_t3
{
  "settings": {
    "index": {
      "analysis": {
        "filter": {
          "synonym_filter": {
            "type": "synonym",
            "synonyms": [
              ":-), happy-smiley",
              ":-(, sad-smiley"
            ]
          }
        },
        "analyzer": {
          "synonym_analyzer": {
            "tokenizer": "standard",
            "filter": [
              "lowercase",
              "synonym_filter"
            ]
          }
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "tweet": {
        "type": "text",
        "fielddata": "true"
      },
      "existence": {
        "type": "text"
      },
      "confidence": {
        "type": "float"
      }
    }
  }
}

Response:
{
  "error": {
    "root_cause": [
      {
        "type": "remote_transport_exception",
        "reason": "[127.0.0.1:9301][indices:admin/create]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "failed to build synonyms",
    "caused_by": {
      "type": "parse_exception",
      "reason": "parse_exception: Invalid synonym rule at line 1",
      "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "term: :-) was completely eliminated by analyzer"
      }
    }
  },
  "status": 400
}

So the reason "reason": "term: :-) was completely eliminated by analyzer" - means that Elastic not supporting this characters in synonym filter.
UPDATE
It can be done by char_filter filter.
Example:
PUT st_t3
{
  "settings": {
    "index": {
      "analysis": {
        "char_filter": {
          "happy_filter": {
            "type": "mapping",
            "mappings": [
              ":-) => happy-smiley",
              ":-( => sad-smiley"
            ]
          }
        },
        "analyzer": {
          "smile_analyzer": {
            "type": "custom",
            "char_filter": [
              "happy_filter"
            ],
            "tokenizer": "standard",
            "filter": [
              "lowercase"
            ]
          }
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "tweet": {
        "type": "text",
        "fielddata": "true"
      },
      "existence": {
        "type": "text"
      },
      "confidence": {
        "type": "float"
      }
    }
  }
}

Test
POST st_t3/_analyze
{
  "text": ":-) test",
  "analyzer": "smile_analyzer"
}

Answer
{
  "tokens" : [
    {
      "token" : "happy",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "smiley",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "test",
      "start_offset" : 4,
      "end_offset" : 8,
      "type" : "<ALPHANUM>",
      "position" : 2
    }
  ]
}


Is there a way to force sshd to call fsync() at the end of an scp upload?

Question: I've got a Debian Linux-based server that accepts file uploads via scp, and this server tends to get powered off suddenly from time to time.  If the power loss occurs shortly after an scp upload has completed, this can result in the uploaded file(s) showing up truncated (or missing) the next time power is restored.
Is there a way to force sshd (or whatever process is writing the uploaded data to disk... I think it is sshd) to call fsync() when the upload is complete, in order to minimize the chance of data loss due to a power outage?
As an example of the kind of thing I'm looking for, this server also accepts file uploads via Samba/CIFS, and I found that adding the following lines to the [globals] section of my smb.conf helps guard against data loss there:
strict sync = yes
sync always = yes

So I'm looking for something analogous, perhaps something that I could put into /etc/sshd_config ?
Thanks!
Answer: One of the GNU core-utils is the sync command.  So you can just have your upload script ssh in and run that command at the end.  There might be something cleaner than that though.
Comment: well done on the new job dude, don't hesitate to call on me for help or whatever ok - I'm strongest on server hardware, ESX/i and FC/FCoE SAN ok.
Answer: No way I know of to make sshd do this, though Kyle's solution is certainly a valid one (assuming you're using keys and an agent, and can stand a few seconds between the upload finishing & the sync request).
If you're paranoid about data loss I suggest investing in a UPS and a RAID card with a battery backup.
If cost is an issue you can mount the target filesystem with the sync option (forces all writes to occur synchronously).
Mounting with sync set will be a a performance hit, but when your transfer is done you know the data is all on disk (subject to the whims of disk controller caches, which if you're using a decent RAID controller may be a noticeable delay - see above :).


Make Fail when installing uuid pecl extension on OSX

Question: So not to familiar with installing extensions by here is my process and the moment of fail.
$ sudo pecl install uuid

then all is good until...
creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
running: make
/bin/sh /private/var/tmp/pear-build-root/uuid-1.0.2/libtool --mode=compile cc  -I. -I/private/var/tmp/apache_mod_php/apache_mod_php-53~1/Build/tmp/pear/temp/uuid -DPHP_ATOM_INC -I/private/var/tmp/pear-build-root/uuid-1.0.2/include -I/private/var/tmp/pear-build-root/uuid-1.0.2/main -I/private/var/tmp/apache_mod_php/apache_mod_php-53~1/Build/tmp/pear/temp/uuid -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/include  -DHAVE_CONFIG_H  -g -O2   -c /private/var/tmp/apache_mod_php/apache_mod_php-53~1/Build/tmp/pear/temp/uuid/uuid.c -o uuid.lo
mkdir .libs
 cc -I. "-I/private/var/tmp/apache_mod_php/apache_mod_php-53~1/Build/tmp/pear/temp/uuid" -DPHP_ATOM_INC -I/private/var/tmp/pear-build-root/uuid-1.0.2/include -I/private/var/tmp/pear-build-root/uuid-1.0.2/main "-I/private/var/tmp/apache_mod_php/apache_mod_php-53~1/Build/tmp/pear/temp/uuid" -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/include -DHAVE_CONFIG_H -g -O2 -c "/private/var/tmp/apache_mod_php/apache_mod_php-53~1/Build/tmp/pear/temp/uuid/uuid.c"  -fno-common -DPIC -o .libs/uuid.o
/private/var/tmp/apache_mod_php/apache_mod_php-53~1/Build/tmp/pear/temp/uuid/uuid.c: In function 'zm_startup_uuid':
/private/var/tmp/apache_mod_php/apache_mod_php-53~1/Build/tmp/pear/temp/uuid/uuid.c:89: error: 'UUID_TYPE_DCE_TIME' undeclared (first use in this function)
/private/var/tmp/apache_mod_php/apache_mod_php-53~1/Build/tmp/pear/temp/uuid/uuid.c:89: error: (Each undeclared identifier is reported only once
/private/var/tmp/apache_mod_php/apache_mod_php-53~1/Build/tmp/pear/temp/uuid/uuid.c:89: error: for each function it appears in.)
/private/var/tmp/apache_mod_php/apache_mod_php-53~1/Build/tmp/pear/temp/uuid/uuid.c:90: error: 'UUID_TYPE_DCE_RANDOM' undeclared (first use in this function)
/private/var/tmp/apache_mod_php/apache_mod_php-53~1/Build/tmp/pear/temp/uuid/uuid.c: In function 'zif_uuid_create':
/private/var/tmp/apache_mod_php/apache_mod_php-53~1/Build/tmp/pear/temp/uuid/uuid.c:168: error: 'UUID_TYPE_DCE_TIME' undeclared (first use in this function)
/private/var/tmp/apache_mod_php/apache_mod_php-53~1/Build/tmp/pear/temp/uuid/uuid.c:171: error: 'UUID_TYPE_DCE_RANDOM' undeclared (first use in this function)
/private/var/tmp/apache_mod_php/apache_mod_php-53~1/Build/tmp/pear/temp/uuid/uuid.c:181: warning: format '%d' expects type 'int', but argument 4 has type 'long int'
make: *** [uuid.lo] Error 1
ERROR: `make' failed

Answer: Follow this tutorial --
http://unrealexpectations.com/blog/2010/04/mamp-pecluuid-module-working-on-snow-leopard/
When you go to make the patch make sure the file to patch are entered correctly, you want to have it be tests/uuid_mac.phpt
If that doesn't work make sure you have you have your php.ini configured with the extension_dir and have the extension=uuid.so there as well.
This also works for OS X 10.7
Answer: You will need an updated version of uuid from util-linux. It is available on Homebrew.
brew install util-linux

But util-linux is a keg-only package and will not be symlinked to /usr/local. Hence you will have to specify the following path when prompted for uuid installation directory
/usr/local/opt/util-linux

For unattended installations, you can use
printf "/usr/local/opt/util-linux" | pecl install uuid


erro cors no angular 6

Question: Pessoal estou com esse erro em um projeto:
Failed to load http://127.0.0.1:8000/hqi/pages: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:4200' is therefore not allowed access.

E não consigo resolve, segui algns exemplos que ensinavam a criar um arquivo de proxy:
const proxy = [
{
   context: '/hqi',
   target: 'http://localhost:8000'
}];
module.exports = proxy;

porem, não deu resultado o erro persiste, alguem poderia me ajudar?
Comment: vc tem que configurar  o cors no seu backend
Comment: O que você está usando no back-end?
Answer: Adicione o seguinte header na resposta do seu back-end.
httpResponse.setHeader("Access-Control-Allow-Origin", "*");
Comment: no laravel insiro aonde sabe me dizer?
Comment: Procure o objeto que é utilizado como resposta para as requisições
Comment: de acordo com a documentação oque você me passo seria isso

    Route::get('home', function () {
       return response('Hello World', 200)
                  ->header('Content-Type', 'text/plain');
   });

mas trabalho com rotas assim

    Route::resource('users', 'UserController');

então não sei onde se encaixaria isso
Comment: isso, adicione o cabeçalho que eu citei na resposta no ->header('...')
Comment: Agora sim entendi oque faltou entender era onde eu colocaria isso, e seria no controller, agradeço a dica
Answer: Provavelmente o problema é no seu backend, qual linguagem está utilizando ? Você precisa definir o cabeçalho: 'Access-Control-Allow-Origin' com o valor '*' no seu backend, isso varia de linguagem pra linguagem, posta aqui o código do teu backend pra gente poder ter uma ideia.
Comment: back end é php com laravel e o front é angular

Mas ja busquei solucão no back end tbm e nao resolveu
Comment: pode postar o código do backend onde você pôs o cors aqui ?

erro cors no angular 6


How can i use template specialization within a non-templated class? c++

Question: I have a class that is not a templated class, I need to add a function to this class that is a template function. The problem is that I call a function in the class that requires a string as the parameter, so I need to make a specialized version of this template so I can call this function only if the parameter is a const char* or make a conditional check inside the function to only make a call to the function if the parameter is a const char* but that does not seem to work either. Any help would be appreciated!
template<class myType> __declspec(nothrow)
std::string GetStrVal(int row, int col, myType default) {

    try {
        CheckColumnType(col, String);
    }
    catch(DatatableException e){
        return default;
    }
    return this->m_rows[row][col];
}

template<class myType>
std::string GetStrVal(int row, const char* col, myType default) {

    unsigned int columnIndex = this->GetColumnIndex(col);
    return GetStrVal(row,columnIndex, default);
}

GetColumnIndex() only takes a const char*.
Comment: The signature are different so you don't have a need for template specialization.
Comment: Does the compiler really accept the keyword `default` as an identifier (please don't tell me that is your actual error, since otherwise your code looks reasonable, at least regarding validity)? Or can it be that the compiler cannot decide between converting the `unsigned int columnIndex` into an `int` or a `const char*` (`int` should have priority, but I'm not sure about that). Also keep in mind that currently `GetStrVal(row, NULL, default)` will call the `int`-version (or maybe it cannot decide, too?), which might not be expected.
Answer: You don't need to specialize anything; you can just provide an overload or two:
    template<class myType> __declspec(nothrow)
    std::string GetStrVal(int row, int col, myType default);  // template

    __declspec(nothrow)
    std::string GetStrVal(int row, int col, std::string default); // overload

    __declspec(nothrow)
    std::string GetStrVal(int row, int col, const char *default); // overload

Comment: He wants to specialize for `col`, not for the templated parameter. But still overloading is the correct approach.


Given two equal sized chords, find angle $ABC$

Question: Given two chords of equal length that also intersect at a point on the circle, find angle $ABC$ given that angle $AOB = 70$ degrees and the radius is $r$. My drawn image
Note that the origin is denote by $O$ and the two chords are denoted as $[AB]$ and $[AC]$
How can we find angle OAB without having an actual value for the radius? Once we have that angle, I think we can continue to find angle BOC and then apply side-angle-side to find angle BOC. Once we have that, we can take the sum BOC + ABO and have our answer.
Answer: Triangle $AOB$ as two sides with the same length, $[AO]$ and $[OB]$. Thus the angles $OAB$ and $OBA$, opposite, are equals.  Since angle $AOB = 70$ degrees, the other two are half the remaining, 55 degrees.
Comment: Oh, of course. Then angle $BAC = 110$. Then we get angle $ABC=35$ as our answer. No need for the complicated process I previously thought we needed


What's the event space of a single coin toss?

Question: Take a probability triple, $(\Omega,\mathscr{F},\mathbb{P})$, representing a single coin toss.
Then
\begin{align}
& \Omega = \{H,T\}. \tag{Prop. 1} \\
\end{align}
Now,
\begin{align}
\text{A } \sigma \text{-algebra must be closed under countable union}, \tag{Prop. 2}
\end{align}
so
\begin{align}
\text{if we have } \{H\},\{T\} \in \mathscr{F} \text{ then we must also have } \{H,T\} \in \mathscr{F}, \tag{Prop. 3}
\end{align}
but
\begin{align}
\{H,T\} \text{ is not a possible event resulting from a single coin toss.} \tag{Prop. 4}
\end{align}
What's gone wrong?
Comment: You are mixing up _events_ (_collections_ of outcomes} and outcomes. $\{H,T\}=\Omega$ _is_ an event since it is the set containing two outcomes, but it is not, by itself, an outcome. $H$ and $T$ are outcomes, $\{H\}$ and $\{T\}$ are the corresponding _events_ consisting of single outcomes.
Answer: The event $\{H,T\}$ is that the result of the flip is either $H$ or $T$; this has probability $1$
The event $\emptyset = \{\,\}$ is that  the result of the flip is neither $H$ nor $T$; this has probability $0$
So there is no problem; $\mathscr{F}= \{\emptyset,\{H\},\{T\},\{H,T\}\}$ as you might expect
Comment: So proposition 4 is wrong. I guess that $\{H,T\}$ got interpreted as 'tails *after* heads' instead of 'tails *or* heads'. In that case it resembles an [equivocation](https://en.wikipedia.org/wiki/Equivocation) fallacy.