Can you legitimately claim to have used intention-to-treat analysis in a cluster randomised trial if recruitment occurs after randomisation?

Question: In cluster-randomised clinical trials it is often the case that 'units', be they hospitals, practitioners or primary care centres etc., are randomised to the intervention or control arms prior to recruitment of the patients.
In this case, it can be expected that not 100% of patients in each cluster will consent to being involved in the trial. If patients are excluded after randomisation because they do not consent, can the authors legitimately claim to have used intention-to-treat analysis?
I feel that they cannot. However, this is based on a basic understanding that ITT is not possible if you exclude a patient after randomisation. Is there a more nuanced explanation for why they can or cannot use ITT?
Comment: Do the patients or investigators know to which cluster they are assigned prior to the consent process?
Answer:
If patients are excluded after randomisation because they do not consent, can the authors legitimately claim to have used intention-to-treat analysis? I feel that they cannot.

I agree with you. I don't think you can. The definition of Intention to Treat is well understood in the epidemiology and clinical trials literature: that patients are analysed according to their original allocation, regardless of the treatment they actually received. Accordingly, withdrawals, losses to follow-up, and crossovers are ignored in a strict intention to treat analysis.
There are now a plethora of studies that report that they used a "modified intention to treat" (mITT) analysis. One problem with this term is it is not well defined. In the Revised Cochrane Risk of Bias Tool for Randomized Trials (RoB 2.0) (Eldridge et al, 2016) the authors define mITT as:

excluding participants if they did not receive a specified minimum amount of the intended intervention

An example of a recent cluster-randomised trial, that used mITT, is The IDEM Cluster Randomized Clinical Trial (Rolland et al, 2020):

The modified intention-to-treat population, defined as all residents included who were followed up for at least 30 days, was used as the main analysis population for all efficacy end points (primary and secondary).

In 2004 an updated CONSORT statement warned that using the term "modified intention to treat" could lead to confusion and to inaccurate results (Ioannidis et al, 2004).
There have been several systematic reviews on this topic. Abraha & Montedori (2010) looked at 475 RCTs and concluded that:

...the increasing appearance of randomised controlled trials that report use of the modified intention to treat approach can be considered a shortcoming in the quality of reporting. The misuse of the intention to treat definition or the introduction of the modified intention to treat term cannot overcome problems related to missing data or protocol deviations. Inconsistent and multiple descriptions of the modified intention to treat approach may cause failure to achieve the standard intention to treat principle, which is the best method that preserves randomisation. We recommend the intention to treat approach as the method of choice for analysis in trials investigating the superiority of an intervention. Authors should provide complete information on post-randomisation exclusions if there are appropriate reasons to exclude participants.

In another review of 6 journals , Montedori et al (2011) looked at 367 RCTs and of these 197 were classified as ITT, 56 as mITT, and 114 as "no ITT" trials, and they found that:

Trials classified as mITT were significantly more likely to be published in general medical journals, to report post-randomisation exclusions and to use placebo as a comparator. A total of 258 (69%) trials received complete or partial financial support from a for-profit agency and 216 of the trials (60%) reported results that favoured the treatment under investigation.

References:
Abraha, I., & Montedori, A. (2010). Modified intention to treat reporting in randomised controlled trials: systematic review. Bmj, 340.
Eldridge, S., Campbell, M., Campbell, M., Dahota, A., Giraudeau, B., Higgins, J., ... & Siegfried, N. (2016). Revised Cochrane risk of bias tool for randomized trials (RoB 2.0): additional considerations for cluster-randomized trials. Cochrane Methods. Cochrane Database Syst Rev, 10.
Ioannidis JP, Evans SJ, Gotzsche PC, O'Neill RT, Altman DG, Schulz K, et al. Better reporting of harms in randomized trials: an extension of the CONSORT statement. Ann Intern Med2004;141:781-8
Montedori, A., Bonacini, M. I., Casazza, G., Luchetta, M. L., Duca, P., Cozzolino, F., & Abraha, I. (2011). Modified versus standard intention-to-treat reporting: are there differences in methodological quality, sponsorship, and findings in randomized trials? A cross-sectional study. Trials, 12(1), 1-9.
Rolland, Y., Tavassoli, N., de Souto Barreto, P., Perrin, A., de Mazières, C. L., Rapp, T., ... & Andrieu, S. (2020). Systematic dementia screening by multidisciplinary team meetings in nursing homes for reducing emergency department transfers: the IDEM cluster randomized clinical trial. JAMA Network Open, 3(2), e200049-e200049.
Comment: Hmm here's an analogous problem. You introduce some feature change on an ads platform and randomize advertisers. But some advertisers never log on during the experiment; so you intended to treat them but they never received the treatment. Same problem, different context. And I'd argue different conclusion.
Comment: Or you can just use daily activity as a variable to stratify on, so there's not over/under representation of those that don't log on during the experiment. Since logging on is correlated with treatment, could confound your results if you don't stratify.
Comment: I was under the impression that losses-to-followup should be included in an ITT-analysis. According to the definition, once the subjects have been measured at baseline and randomized, they should be included in the analysis. Multiple imputation seems to be recommended frequently in these cases. When I searched the literature recently, there was no concensus on this issue, however.
Answer: Cluster randomized trials are used when the treatment can be delivered "in total" such as when you educate all the staff in a hospital ward about a certain medical procedure, and you want to test whether there is benefit of such education.  The education of the staff informs how all the patients are treated by that staff.  We do a lot of cluster randomized trials and I have not seen many, if any, that excluded any patients from a cluster.
So one can argue that removing patients from clusters when the treatment is designed to be applied to a whole cluster is invalid.
Answer: Isn't this what placebos are for? Use interest in participation as a screening factor for all randomization units, however, administer a placebo in the control group.
Robert's answer initially agrees with you (ITT not reliable) but then captures a lot of scholarly skepticism on the alternative, mITT.
Interest/consent in the treatment might be correlated with the treatment effect, itself, so tread cautiously.
By removing observations where units do not consent to the treatment, you could overestimate the effectiveness of treatment.
For example, suppose radiation is used for cancer patients, but some patients with terminal illness opt out of radiation in favor of hospice, instead. They do not consent but note that consent is corrrlated with treatment efficacy. When consent is not present, the efficacy of radiation could be much lower. But by excluding lack of consent, you overestimate effect of radiation.
In this context, I'd prefer a multilevel model where the global coefficient for treatment effect is a function of two subgroup group coefficients, namely with-consent and without-consent (censored, imputed as zero.)
In this case, you could both differentiate the consent from non-consent contexts but also be aware of correlation between consent and treatment effect.


Is it possible to find a correlation coefficient in this situation?

Question: I am new to statistics. I have a list of coyote sightings and am trying to figure out if the moon illumination percentage on that particular night is correlated to the sighting. Meaning, if the moon has a higher percentage of illumination, does that relate to an increased chance of a sighting happening or vice versa. Is it possible to find a correlation coefficient for this potential relationship?
Comment: Yes. Are you asking how? You'll need to provide an example of the type of data that you have (a reproducible example is ideal) https://stats.stackexchange.com/help/how-to-ask
Comment: Do you have measurements of illuminance? Otherwise maybe there is historical data on cloud cover and stage of moon cycle.
Comment: can you provide eg a graph of the data. a correlation coefficient relates to a linear relationship - 100 times more light 100 times more sightings.
Comment: Also, 100% agree that correlation coefficients can come later (if at all).
Comment: "*100 times more light 100 times more sightings.*" Notwithstanding a change of variables (e.g. log transform), yes. I've read that some human senses (e.g. [Fechner's law](https://en.wikipedia.org/wiki/Weber%E2%80%93Fechner_law#:~:text=Fechner%27s%20law%20states%20that%20the,with%20an%20accurate%20nonhuman%20instrument.)) are logarithmic. Linear models are only guaranteed to be linear in their parameters, not their predictors.
Comment: @Galen That applies to things like reaction times, motor control (see the literature on how people use mice, for instance) and, to some extent, to light sensitivity.  But, at the very least, these sitings will be related to *coyote* behavior, too.  Thus it would be wise not to approach the analysis with a simple theory in mind but rather to display and view the data in useful, meaningful ways to see whether and what relationships might hold.  Correlation coefficients can come later (if at all).
Comment: @whuber We agree it is unwise to rigidly restrict our models according to a simple (if not simplistic) theory. I tend to follow [*Bayesian Workflow*](https://arxiv.org/abs/2011.01808) which involves starting with simple models and working up to more complex ones often by composing the simpler ideas together. I also tend to alternate between theorizing and plotting, starting with theory first. I find it valuable to iteratively brainstorm/read and then visually test my/other's thinking against plots. A bit subjective, but I feel I learn more and produce better models this way.
Answer: Some Answers
As direct answers to your questions...

Is it possible to find a correlation coefficient for this potential relationship?

This is certainly so given these are both numeric variables, though it depends on what the data looks like. If your data is fairly linear, you should consider something like a Pearson $r$ coefficient, whereas if it behaves in a monotonic fashion, you should instead think of using something like Spearman's $\rho$ coefficient. The choice of correlation will depend on how the data is shaped.

If the moon has a higher percentage of illumination, does that relate to an increased chance of a sighting happening or vice versa?

It could, but that's only known after you get your data. However, we can't really assign causality to either direction of this relationship given it is bivariate and can be tainted by any number of factors related to confounds, cross-sectional behavior, and other influences. You can see some of these issues are present in the comments, where the other users mention complexity of models. I highlight how this relates to regression in a recent answer here, as some variables have unexpected relationships that shape their associations with each other.
Potential Avenue of Exploration
If you are familiar with R at all, there is an excellent episode from Tidy Tuesdays that analyzes Sasquatch sightings that may give you some relevant insight on what kind of data you could collect and what it may look like (such as timelines of sightings, seasonal distributions, text analysis of eyewitness testimony, etc.). As an example, I recall one part was where they looked at location by season differences in sightings, where there were some interesting communalities and uniqueness to each location.

Obviously the assumptions with that kind of data are as interesting as they are loaded with confounding influences. Are the sightings due to chance? Does location play a role? What alternative explanations can there be to each relationship? This may build some insight into your own project, as they both deal with encounters in the wild and provide some angles you haven't considered yet, particularly what extraneous variables may be shaping the relationships in these sightings.


Why does the jacobian of the metric tensor give zero?

Question: I am trying to compute the derivatives of the metric tensor given as follows:

As part of this, I am using PyTorch to compute the jacobian of the metric. Here is my code so far:
# initial coordinates
r0, theta0, phi0 = (3., torch.pi/2, 0.1)
coord = torch.tensor([r0, theta0, phi0], requires_grad=True)
print("r, theta, phi coordinates: ",coord.data)

def metric_tensor(coords):
    r = coords[0]
    theta = coords[1]
    phi = coords[2]
    return torch.tensor([
        [1., 0., 0.],
        [0., r ** 2, 0.],
        [0., 0., r ** 2 * torch.sin(theta) ** 2]
    ])

jacobian = torch.autograd.functional.jacobian(metric_tensor, coord, create_graph=True)

For reasons I don't understand, the jacobian always returns zero, even though the derivatives of the metric shouldn't all be zero. Could anyone point me to what the issue may be?
Answer: torch.pi is not exist, so you should implement it by yourself.
Moreover, I think the problem is that the calculation is made explicitly on the created tensor.
This works for me:
import torch

# initial coordinates
torch.pi = torch.acos(torch.zeros(1)).item() * 2
r0, theta0, phi0 = (3., torch.pi / 2, 0.1)
coord = torch.tensor([r0, theta0, phi0], requires_grad=True)
print("r, theta, phi coordinates: ", coord.data)

def metric_tensor(coords):
    r = coords[0]
    theta = coords[1]
    phi = coords[2]
    res = torch.zeros(3, 3)
    res[0, 0] = 1.
    res[1, 1] = r ** 2
    res[2, 2] = (r ** 2) * (torch.sin(theta) ** 2)
    return res

jacobian = torch.autograd.functional.jacobian(metric_tensor, coord, create_graph=True)
print(jacobian)

The result is this:
r, theta, phi coordinates:  tensor([3.0000, 1.5708, 0.1000])
tensor([[[ 0.0000e+00,  0.0000e+00,  0.0000e+00],
         [ 0.0000e+00,  0.0000e+00,  0.0000e+00],
         [ 0.0000e+00,  0.0000e+00,  0.0000e+00]],

        [[ 0.0000e+00,  0.0000e+00,  0.0000e+00],
         [ 6.0000e+00,  0.0000e+00,  0.0000e+00],
         [ 0.0000e+00,  0.0000e+00,  0.0000e+00]],

        [[ 0.0000e+00,  0.0000e+00,  0.0000e+00],
         [ 0.0000e+00,  0.0000e+00,  0.0000e+00],
         [ 6.0000e+00, -7.8681e-07,  0.0000e+00]]], grad_fn=<ViewBackward>)


Pairwise deletion and non-positive definite sample covariance matrix

Question: So I have read that using pairwise deletion can lead to a non-positive definite matrix. However, I tried to find a source which showed this mathematically. Can someone point me to that?
My intuition thus far is that the factor $ N - 1 $ by which I divide the covariance between two variables differs. Also that using different sample sizes leads to different levels of accuracy in $ \hat{\sigma}_{ij} $. But I would appreciate a more detailed explanation.
Answer: The simple explanation is that the mathematical proof that covariance matrices are positive semi-definite breaks down when using pairwise deletion.  But to see with more details what is happening, let us look at a simple example. The simplest example possible needs 3 variables, since we need different pairs of observations being left out for different pairs of variables. Let us make a simple data example which has (approximately) a singular (that is semi-definite) covariance matrix, so that a small perturbation could make it non-definite, for instance, with a small, negative, determinant. Let the covariance matrix be
$$
\Sigma = \begin{pmatrix} 1 & -\frac12 & -\frac12 \\
                        -\frac12 & 1 & -\frac12 \\
                        -\frac12 & -\frac12 & 1\end{pmatrix}
$$ which you can check has determinant 0.
An example data set with approximately this covariance matrix is
 XX
    [,1]  [,2]  [,3]
1   9.29 10.86  9.85
2   9.10 11.03  9.87
3  12.23  9.17  8.60
4  10.91  9.63  9.46
5   9.66  9.90 10.43
6  10.03  9.73 10.23
7   9.61 10.62  9.77
8   9.54  7.97 12.48
9  10.63  9.76  9.61
10  9.00 11.32  9.68

with the following pairs plot:

Let us decide for a few variables we can declare as missing, NA.  We need the covariances to get a little bit more negative, but without changing the variances too much. So look in the plot for some concordant pairs, that is, pairs contributing positively to the empirical covariance. That way the covariance will be more negative when this pairs is left out. But at the same time, choose pairs with values close to their variables means, so that the variance will not change too much. Looking at the plot shows a possible solution:
XXX <- XX
XXX[9, 1] <- NA
XXX[4, 3] <- NA
XXX[7, 2] <- NA

and then check that this works:
cov(XXX, use="pairwise")
           [,1]       [,2]       [,3]
[1,]  1.0689000 -0.5853143 -0.5133893
[2,] -0.5853143  1.0732500 -0.6492607
[3,] -0.5133893 -0.6492607  1.0831194
> det(cov(XXX, use="pairwise"))
[1] -0.2521741


How to determine if one predictive model is statistically significantly better than another one?

Question: I have a data set, two competing predictive models (regressions) and I need to decide which predictive model is better. Let us also assume that I have a measure of accuracy (for example mean squared deviation).
As the first step, I can randomly split my data set into two equal parts. I use the first part to train each of the two models and then I evaluate the two trained models using the second data set. In that way I get two out-of-sample errors. Then I could say that the model corresponding to the smaller error is better. However, how can I be sure that the results are statistically significant? For example, it can be the case that the first model wins for one split of the data and looses for another split.
I thought that, as a workaround, I could take all the deviations between the targets and the corresponding predictions and then resample from these deviations. Then I check how frequently one model is better / worse than another one. However, this way I do not count for the randomness determined by the limited size of the training set. In other words, it might be the case that model 1 is statistically significantly better than the model 2 if both models are trained on the first 100 randomly selected data points but if we use other 100 data points, model 2 can be better than model 1.
I guess that the main cause of my problem is that the measure of accuracy is random because of two reasons: (1) the limited sizes of the evaluation set and (2) limited size of the training set. So, I need to have a way to treat this randomness properly.
Comment: Read up on (leave-one-out) cross-validation and model selection criteria. Note: Leave-one-out cross-validation is asymptotically equivalent to the AIC model selection criterion.
Comment: @bbrot, I know about leave-one-out cross-validation. Unfortunately, I do not see how it solves my problem. For each data point I do get an out-of-sample prediction and each model gets an average error this way. But still, I only have "sample error", which is a random number and one of them can be smaller than another one just by chance (and not because the corresponding population error is smaller). Moreover, I do not know what results would I get if I have another data set of the same size (which I do not have).
Comment: @bbrot: Roman is right - LOOCV is one of the few cross validation procedures that *never* allows to distinguish limited-number-of-test-cases-variance from limited-number-of-training-cases-variance (instability).
Answer: You are right that your validation/verification results are subject to (at least) these two sources of variance:

the measure of accuracy is random because of two reasons: (1) the limited sizes of the evaluation set and (2) limited size of the training set.

Variance (2) is commonly called model instability.
Practically speaking, there are 3 situations:

Var (1) >> Var (2): you can use Var (1) as approximation for the total variance.
Var (1) << Var (2): you can use Var (2) as approximation for the total variance.
However, you may want to reconsider the meaning of this finding: the predictions of the model in question is unstable wrt. exchanging some training cases for other training cases from the same population. Maybe you should improve your training first in order to arrive at a stable models.
variance (1) and (2) are in the same order of magnitude. Both of them need to be taken into account for the decision.
Again, think whether the observed level of instability is acceptable for your application.

You can measure Var (2) rather directly with iterated/repeated cross validation (see e.g. our paper: Beleites, C. & Salzer, R. Assessing and improving the stability of chemometric models in small sample size situations, Anal Bioanal Chem, 2008, 390, 1261-1271), but also out-of-bootstrap allows you to separate the two variance sources.

When formulating the test, keep in mind that Var (2) is likely or even expected to vary between models.


How to positioning a field before another specific field with hook_form_alter()

Question: I'm modifying a form by adding another text input, and I need to add this input right before a field. How do i do it? this is my code and it's working correctly:
function provintegra_form_alter(&$form, &$form_state, $form_id) {

    $form['input_latitude'] = array(
      '#type' => 'textfield',
      '#title' => t('Search address'),
      '#default_value' => '',
      '#prefix' => '
Mappa qui
',
    );

}

I just need to position this before $form['field_latitude'].
Answer: Set the weight property of your required lower than other.
$form['field_latitude']['#weight'] = 0;
$form['input_latitude']['#weight'] = -10;
or
function provintegra_form_alter(&$form, &$form_state, $form_id) {
$form['input_latitude'] = array(
  '#type' => 'textfield',
  '#title' => t('Search address'),
  '#default_value' => '',
  '#prefix' => '
Mappa qui
',
  '#weight' => -10,
);

}
Answer: Each form element can have a weight.

Description: Used to sort the list of form elements before being
  output; lower numbers appear before higher numbers.

See the example on the api.drupal.org page
Answer: Setting the "#weight" property for the form field works, but it doesn't give you the certainty the form fields are consecutive. If you want to be sure they are consecutive, you should use code similar to the following one (I suppose the form is not already using "latitude" as array index):
function provintegra_form_alter(&$form, &$form_state, $form_id) {
    $form['latitude']['input_latitude'] = array(
      '#type' => 'textfield',
      '#title' => t('Search address'),
      '#default_value' => '',
      '#prefix' => '
Mappa qui
',
    );

    $form['latitude']['field_latitude'] = $form['field_latitude'];

    unset($form['field_latitude']);
}

This is similarly to what done in node_content_form(), where the form field for the node body, and the fields to select the input format are placed in the same array index ($form['body_field']). The structure of the node edit form array is the following:
  $form['body_field']['teaser_js'] = array(
    '#type' => 'textarea',
    '#rows' => 10,
    '#teaser' => 'edit-body',
    '#teaser_checkbox' => 'edit-teaser-include',
    '#disabled' => TRUE,
  );

  $form['body_field']['teaser_include'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show summary in full view'),
    '#default_value' => $include,
    '#prefix' => '
',
    '#suffix' => '
',
  );

  $form['body_field']['body'] = array(
    '#type' => 'textarea',
    '#title' => check_plain($label),
    '#default_value' => $include ? $node->body : ($node->teaser . $node->body),
    '#rows' => 20,
    '#required' => ($word_count > 0),
  );

  $form['body_field']['format'] = filter_form($node->format);


Can't add second interface to the pod with multus - minikube

Question: I am trying to deploy a pod with second interface using multus-cni. However, when I deploy my pod I only see just one interface the main one. The secondary interface is not created.
I followed the steps in the quick start guide to install multus.
Environment:
minikube v1.12.1 on Microsoft Windows 10 Enterprise
Kubernetes v1.18.3 on Docker 19.03.12
Multus version
--cni-version=0.3.1
$00-multus.conf
{ "cniVersion": "0.3.1", "name": "multus-cni-network", "type": "multus", "kubeconfig": "/etc/cni/net.d/multus.d/multus.kubeconfig", "delegates": [ { "cniVersion": "0.3.1", "name":
"bridge", "type": "bridge", "bridge": "bridge", "addIf": "true", "isDefaultGateway": true, "forceAddress": false, "ipMasq": true, "hairpinMode": true, "ipam": { "type": "host-local
", "subnet": "10.244.0.0/16" } } ] }

$1-k8s.conf
{
"cniVersion": "0.3.1",
"name": "bridge",
"type": "bridge",
"bridge": "bridge",
"addIf": "true",
"isDefaultGateway": true,
"forceAddress": false,
"ipMasq": true,
"hairpinMode": true,
"ipam": {
"type": "host-local",
"subnet": "10.244.0.0/16"
}
}

$87-podman-bridge.conflist
{
"cniVersion": "0.4.0",
"name": "podman",
"plugins": [
{
"type": "bridge",
"bridge": "cni-podman0",
"isGateway": true,
"ipMasq": true,
"hairpinMode": true,
"ipam": {
"type": "host-local",
"routes": [{ "dst": "0.0.0.0/0" }],
"ranges": [
[
{
"subnet": "10.88.0.0/16",
"gateway": "10.88.0.1"
}
]
]
}
},
{
"type": "portmap",
"capabilities": {
"portMappings": true
}
},
{
"type": "firewall"
},
{
"type": "tuning"
}
]
}

$multus.kubeconfig
apiVersion: v1
kind: Config
clusters:
- name: local
  cluster:
    server: https://[10.96.0.1]:443
    certificate-authority-data: .....
users:
- name: multus
  user:
    token: .....
contexts:
- name: multus-context
  context:
    cluster: local
    user: multus
current-context: multus-context

File of '/etc/cni/multus/net.d'

**NetworkAttachment info:**

    cat <<EOF | kubectl create -f -
    apiVersion: "k8s.cni.cncf.io/v1"
    kind: NetworkAttachmentDefinition
    metadata:
    name: macvlan-conf
    spec:
    config: '{
    "cniVersion": "0.3.1",
    "type": "macvlan",
    "master": "eth1",
    "mode": "bridge",
    "ipam": {
    "type": "host-local",
    "subnet": "192.168.1.0/24",
    "rangeStart": "192.168.1.200",
    "rangeEnd": "192.168.1.216",
    "routes": [
    { "dst": "0.0.0.0/0" }
    ],
    "gateway": "192.168.1.1"
    }
    }'
    EOF

Pod yaml info:
cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
name: samplepod
annotations:
k8s.v1.cni.cncf.io/networks: macvlan-conf
spec:
containers:
    name: samplepod
    command: ["/bin/ash", "-c", "trap : TERM INT; sleep infinity & wait"]
    image: alpine
    EOF

Answer: I installed new minikube version, now adding secondary interface seems to be fine.


Close popup window opened by a different domain

Question: I have a link in abc domain which opens a popup window and opens a page in a different domain. I need to add a button on the popup which calls window.close() and closes it via javascript. Is is even doable? can I close a popup using windo.close which has been opened by a different domain?
Answer: Do you mean something like this:
function openWin(){
   myWindow=window.open("http://www.google.co.uk","","width=200,height=100");
}

function closeWin(){
  if(myWindow){
    myWindow.close();
   }
}

hooked up to these buttons:
<input type="button" value="Open 'myWindow'" onclick="openWin()" />
<input type="button" value="Close 'myWindow'" onclick="closeWin()" />

it's a tad crude but should work... I'd also make it unobtrusive if I were you, I've used obtrusive code just to get an answer to you quickly
Comment: He means in the window opened to the other domain, have it contain a "close" button. Not buttons on his page that close the opened window.


Violin practice tips, correction and advice

Question: About 8 years ago, I started learning violin  with a tutor for about 5 months but I left it abruptly because of academics. Now, three months ago, I decided to get back to it since I have more free time because of lockdown. Unfortunately I can't get a violin tutor right now both because of the lockdown and also because of financial issues. However,  I do remember the little teachings that my tutor gave me and also with the help of YouTube, I am trying to still relearn violin. I know the importance of proper bowing even before I started relearning my violin. But because of lack of a tutor who can correct me on my mistakes which I don't know I'm making, I'm trying to put a small clip of me playing so I can get tips and corrections to my bowing. Also, I know I am way out of tune while playing some of the notes, mostly because I'm still a beginner and partly because I was a bit nervous to play in front of the camera.

Please tell me any improvements that can be done. (Sorry if this question seems personal)
Answer: Some right hand (bowing) comments:
You seem to have used a low resolution which means that a lot of the time your bow hand is blurred. However I managed to find a moment when it was relatively still and pausing the video gave me a good view of your bow-hold.

There is a fair amount of tension in your right hand, which is understandable for a relative beginner, but a lot of it comes from a bad bow-hold. You appear to be pinching the bow between thumb and fingers. Your bow-hold should be relaxed. If someone knocked your bow or bumped your arm there should be a real risk of dropping the bow. There is no chance of that here.
Your thumb is arched in a tense position away from your hand. It should be the exact opposite. Your thumb should be curled towards your fingers. The tip of your thumb should be supporting (not gripping) the bow somewhere round the gap between the end of the leather and the frog.
Your pinky is also reverse arched with the tip on the bow. I also have this problem, caused by having a double joint in one of the finger joints. If you also have this double joint then it is very difficult to fix. Can you bend your little finger the wrong way? If so you are double jointed. If not then you should try and fix this. Ideally your pinky should be curved naturally allowing you to move the bow up and down simply by straightening and bending the little finger. The principle function of the pinky is to support some of the weight of the bow when you are playing near the frog and all the weight of the bow is trying to press the bow down harder into the string.
Your third finger is curved round, particularly the top joint/section, gripping the bow tightly. The finger should be much straighter, exerting much less pressure on the bow.
For the second finger you can pretty much copy & paste the third finger comments. Your fingers should be supporting the bow not gripping.
The function of the first (index) finger is to apply pressure on the top of the bow to press it down onto the strings when you are playing near the tip of the bow and you want more volume. To do this the first bone/knuckle of the finger (the one nearest the hand) needs to be resting on the silver winding ready to press down when needed. You have a gap there because you are gripping the bow tightly.
There is some tension in your right shoulder which is slightly raised. Before you start playing, before you pick up your instrument and bow try and relax shrugging your shoulders up and down and shaking your arms and hands to loosen them up. Pick your bow up with your left hand and lower your relaxed right hand down to meet the bow. Take the bow in the lightest of grips (bad word) with your right hand. Keep hold of the stick lightly with your left hand (so you don't drop it) and with your right hand, while holding the bow, do a gentle, relaxed waving motion. Now you are ready to play.
You overall right arm bowing action is quite good although, because you are playing a completely non-challenging piece (more of that later), your action is short. We don't see you bowing at or near the tip or the frog.

Some left hand comments.
For some strange reason you have music on a stand obscuring your left hand somewhat. I say "strange reason" because surely you don't need to read the music for such a simple piece which is obviously well below your level? As Robert Browning said, "Ah, but a man's reach should exceed his grasp, Or what's a heaven for?"

The first thing that strikes me is that we can tell from your right arm that you play on different strings at different times but there is no hint hint of that in your left arm. Normally we should expect to see your left elbow move towards your right when you move towards the G string and, conversely, to move towards your left when you move towards the E string. This movement makes it easier to maintain the correct "cupped" form of the left hand required for good playing.
Overall the angles between hand, wrist, forearm, elbow, upper arm look good.
Your grip of the neck and your finger position looks bad. Your fingers appear to be too flat, too close to the strings. It looks as if too much weight of the violin is resting on the top joint of the first finger and not enough on the first joint of the thumb. The problem that arises is that you are pressing the strings down with the flat part of the top bone of the finger when you should be using the actual tips of the fingers. When you are playing something slow and simple like this then you can get away with this bad technique. You cannot play fast passages like this.

Hope that helps.
Comment: Thank you so much for writing out such a helpful response and bringing such details into notice!! I had recorded this video not with the intent of using it in such a way, but in hindsight I do realise I should have used a video focusing on both my hand movements without the stand... Thank you again for this answer


Kong, 502 Bad Gateway with localhost Service

Question: I am facing to an issue with Kong, i created my service and my route exactly like the 5min quickstart of the Kong's documentation but i only change some parameters to setup the service with my own api working on localhost:3000.
So there is my setup:

Kong running on docker (Docker is running on the same machine than my service)
http://localhost:3000/users which is my service always returning JSON
{"status": "OK"}

there is what i did to create my service.
I created my service with parameters :
{
    "name": "users",
    "url": "http://localhost:3000",
    "host": "127.0.0.1"
}

then i created the Route with body:
{
    "hosts": [ "127.0.0.1"],
    "methods": ["GET"],
    "paths": ["/"],
    "name": "users"
}

then i tried if everything is working with http://localhost:8080/users with header:
"Host:127.0.0.1"
and i got the reply:

502 Bad Gateway : An invalid response was received from the upstream
  server

The docker's Logs (this one hosting kong) :

2019/03/04 09:28:36 [error] 36#0: *63824 connect() failed (111:
  Connection refused) while connecting to upstream, client: 172.18.0.1,
  server: kong, request: "GET /users HTTP/1.1", upstream:
  "http://127.0.0.1:3000/users", host: "127.0.0.1"

So we can see Kong trying to access the good endpoint '127.0.0.1:3000/' but can't access to it.
Maybe Kong trying to access is own container localhost but i don't know, and i don't know how to fix it, maybe you can help me!
Answer: You have indeed configured Kong to access its own container. In order to access services running on your actual machine (I assume Windows or macOS here), you can point the service to http://host.docker.internal:3000 instead. Additionally, I would leave out the host properties until you have connected correctly, and then add the restrictions back in again.
See also this question: Dockererized Kong on windows giving "An invalid response was received from the upstream server"
Kong running in a container is like it's having its own little VM (don't hit me, I know this is not quite true, but from a networking perspective, this works), and localhost for Kong is just that, the container. The host of the container, to Kong, is a separate host.
Answer: As your service is running outside the container and the kong network, you enter your local IP instead of localhost.
Example:
http://192.168.1.10:3000


How to delete a node with same specific field content and by same user when a more recent one is created in drupal 7?

Question: I have created a review system allowing users to make a review of a place. One of the fields for each review is the actual location which needs to be filled. If a user decides to create a review of 'the Red Lion' and it happens that she has already written one on that place, I want the old review to be deleted, and only the latest one kept.
I have tried to use Rules with the 'fetch entity by property' and a double loop, but it does not seem to do the trick. Here is the export of a test rule where I just flag the 'former review'. The current result is that it flags all reviews of the 'Red Lion' made prior to the current one being saved. Any suggestions?
<code>{ "rules_delete_old_reviews" : {
"LABEL" : "Delete old reviews",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "flag" ],
"ON" : { "node_presave--review" : { "bundle" : "review" } },
"DO" : [
  { "entity_query" : {
      "USING" : {
        "type" : "node",
        "property" : "author",
        "value" : [ "site:current-user" ]
      },
      "PROVIDE" : { "entity_fetched" : { "entity_fetched_author" : "Fetched node by author" } }
    }
  },
  { "LOOP" : {
      "USING" : { "list" : [ "entity-fetched-author" ] },
      "ITEM" : { "list_item_nodeauthor" : "Current list item" },
      "DO" : [
        { "entity_query" : {
            "USING" : {
              "type" : "node",
              "property" : "field_revlocation",
              "value" : [ "node:field-revlocation" ]
            },
            "PROVIDE" : { "entity_fetched" : { "old_review_item" : "old_review" } }
          }
        },
        { "LOOP" : {
            "USING" : { "list" : [ "old-review-item" ] },
            "ITEM" : { "list_item" : "Current list item" },
            "DO" : [
              { "drupal_message" : { "message" : "This node \u0022[list-item:title]\u0022 is now flagged." } },
              { "flag_flagnode" : {
                  "flag" : "tagged_review",
                  "node" : [ "old-review-item:0" ],
                  "flagging_user" : [ "site:current-user" ],
                  "permission_check" : "1"
                }
              }
            ]
          }
        }
      ]
    }
  }
]
  }
}
</code>
Answer: Here is how I achieved it in the end:
First, create a component rule 'Listreviews'. This component rule will filter nodes created by the current user and then

Component plug-in is set to 'rule'.
The variable is of type 'node' of type 'parameter'. Give it a machine name 'nodefromlist' and save.
Add a condition: data comparison. This comparison will be have the data selector nodefromlist:author:uid, and it needs to equal site:current-user:uid.
Add an action: 'delete entity', which will have the data selector value of 'nodefromlist'.
Save!

Then, create a rule 'Delete duplicate review'.

the event chosen is 'before saving content type review'
no conditions
Actions: 'fetch entity by property'

entity type 'node'
property: the field 'location' (in my example)
data selector: go down the arborescence up to: node:field-location
I put a limit of 10, but may need to be adjusted based on previous content available.
Add a label to the provided variables section
Add a variable name like: item_same_location

Then

Create a loop below the action above with the parameter item_same_location

call the current list item: list_item.

In that loop, create an action 'component' with the name of the rule component created above.

the data selector will be: list_item.

Save your rule!

This should now work. As a bonus, I added a 'display message' action in the component rule in order to let the user know that their previous review was deleted. I hope this helps someone in the future!


limit validation button on visible state textfield?

Question: Suppose I had a selectbox with 2 options. One option enabling the visible state of one field, while the other disables that state and makes a different text field visible.
If I had a button that has limit_validation_errors, how would one go about validating only the field whose state is visible?
Currently my setup looks like the following:
<code>$form['unit'] = array(
  '#type' => 'select',
  '#options' => array(
    0 => '€',
    1 => '%',
  ),
  '#default_value' => 0,
);

$form['price-percentage-field'] = array(
  '#type' => 'textfield',
  '#title' => t('Price'),
  '#description' => t('Set the percentage discount price for this volume set'),
  '#size' => 10,
  '#element_validate' => array('price_percentage_validate'),
  '#states' => array(
    'visible' => array(
      ':input[name="unit"]' => array('value' => 1),
    ),
  ),
);

$form['price-field'] = array(
  '#type' => 'textfield',
  '#title' => t('Price'),
  '#description' => t('Set the price for this volume set'),
  '#size' => 10,
  '#element_validate' => array('price_validate'),
  '#states' => array(
    'visible' => array(
      ':input[name="unit"]' => array('value' => 0),
    ),
  ),
);

$form['add'] = array(
  '#type' => 'submit',
  '#value' => t('Add'),
  '#name' => 'add_row',
  '#limit_validation_errors' => array(
    array('price-field'),
    array('price-percentage-field'),
  ),
  '#submit' => array('add_volume_standard'),
  '#ajax' => array(
    'callback' => 'add_volume_standard_ajax_callback',
    'wrapper' => 'error-placeholder', //<- this here is for my custom error-placeholder that will contain the form_errors nicely in a div using a few ajax_commands and form_states
    'method' => 'append',
    'effect' => 'fade',
  ),
  '#title' => t('Click this button to add a standard volume price'),
);
</code>
When using this code, both fields will always be validated. While I want to only validate the visible field. How can I do this?
Also, a validation function is not my preferable approach. I have my reasons.
Answer: I would personally prefer a form-validation-function, which seems for me the ideal solution...
But since you're using custom validators, you can check the value of your unit-field in there:
The second argument in your custom element-validation-function is the form_state-object, with that you have access all form-values. (eg. $form_state['values']['unit'])
Check the value of the unit-field in both validation-functions, depending of it's state do or skip the validation.
Example:
<code>   function price_percentage_validate($element, &$form_state, $form) {
        if ($form_state['values']['unit']>0) {
            /* your validation code */
        }
    }
   function price_validate($element, &$form_state, $form) {
        if ($form_state['values']['unit']<1) {
            /* your validation code */
        }
    }
</code>
Comment: How did I not see this, such a simple answer. I feel really stupid. I've been using it already.. Thank you for helping me out!
Comment: I'm glad that i could help you :) now, it would be nice if you accept my answer, so the question will be closed, otherwise it will be still listed as a unanswered question.
Comment: My bad I didn't know I was able to with a new account I assumed I was not since I couldn't upvote the answer. My assumptions are trivial, I should try before assuming. My apoligies :/


How to write visible character count validation in Ruby on Rails

Question: (as a follow up to this question: In Jquery AND Rails, how to get visible character count of a string?)
Let's say you have a string with embedded links and tags like so:
 This string has some links inside it.

Then the visible string to a user is:
 This string has some links inside it.

The full string has 73 characters and the visible just 37 characters.
Let's say I now want to write a rails validation (custom validation) that checks the visible length of the string. ie:
 validate :string_visible_length

 def string_visible_length
      #some code that verifies the character length of a field named :field_name
 end

Any ideas how to accomplish this? I would need to strip the html tags and then check the character length..
Answer: I think you could use the strip links method in your validation.
Make helpers available in your model file:
  include ActionView::Helpers

Here's the validation:
  validate :string_visible_length

private

  def string_visible_length
    visible_string = strip_links(self.column_name) # where column_name is the field you need validated
    errors.add(:base, 'Visible string is too long') if visible_string.length > 20 # 20 is whatever length you need
  end

Comment: with your code the error "undefined method `strip_links' for "asdfasdfasdf":String" comes up. Looked at the api, and you need to put the string in strip_links("this string"). But then switching to strip_links(self.column_name).length... leads to the error "undefined method `strip_links' for #" thoughts?
Comment: Oh yeah, duh, it's a helper method, not an AR call. Let me rewrite that.
Comment: hmm.. i think a line has to be added to the model along the lines of "      include ActionController::Base::Helpers" to the model (although I don't think this line works). Ideas?
Comment: Try it now by including the action_view helpers.


How can I filter the entire contents of a site by region?

Question: I have a classifieds site that I created at http://www.gastonia.com.  I've introduced the location module, and I am using views to search by proximity after someone types in a zipcode and distance. Everything is working fine.
I now want to experiment with filtering the entire site's displayed contents - all pages, all blocks, all views, all menus, etc. When someone types in their zip code and hits submit, every item of content is filtered for them within 25 miles of their region.
I can easily do this with Views and filters, even for blocks where arguments don't work.  I also want to do this for all 770 towns in my state, which means creating views programatically.  Each would have its own page / url title like Gastonia-NC and Shelby-NC and show items accordingly.
I  have created the script to do that - loop through all 770, and everything actually works, except it's slow, to say the least. Maybe there is a better way to do this?  Note the production site does not have the location / filter implemented, just my dev.
Answer: You can still use views then just use the views global filter module. This way they only enter their location only once then every other view on the site inherits this filter.


Manter os campos preenchidos no modo edição

Question: Colegas.
Tenho um código, que não é de minha autoria, do qual na hora de cadastrar o usuário clica em um botão Adicionar mais tamanhos e mais campos aparecem. Vejam:

Até aqui tudo certo, os tamanhos e estoques vão para a base de dados. O problema está na hora de editar. Gostaria que esses campos já aparecessem "abertos" para que eu possa incluir as informações que virão do banco de dados. Vejam o código:

    <input type="text" name="Tamanho[]" class="form-control" placeholder="Tamanho">
    <input type="text" name="Estoque[]" class="form-control pull-left" placeholder="Estoque">
    <button type="button" class="removerCampo btn btn-danger" title="Remover linha"> Remover</button>

<button type="button" class="adicionarCampo btn btn-primary" title="Adicionar item"> Adicionar mais tamanhos</button>

JQuery

Comment: Dessa forma não tem como responder a sua pergunta, dê mais detalhes. Qual linguagem você está utilizando para fazer a conexão com o banco? Você já tem as informações na view? Qual o formato que está vindo?
Comment: Olá Junior. O problema está no jquery e não no PHP ou Mysql. Preciso que na hora de editar, apareça a quantidade de campos referentes a quantidade de informações cadastradas baseadas no código do post.
Comment: Sim, mas como você vai obter quantos itens estão cadastrados e quais as devidas informações? Isso terá que vir do MySQL e do PHP não? Você já tá trazendo?
Comment: Isso... estou sim. Consigo trazer as informações do banco de dados, todos os campos do formulário já estão preenchidos com as informações, só faltam esses...
Comment: Mas a gente precisa saber como tá vindo essa informação, qual o nome da variável? qual a estrutura dos dados? tem como você "printar" o valor dessa variável?
Comment: Olá Junior. Consegui trazer. Fiz dentro do laço do PHP mesmo.
Answer: Consegui resolver trazendo do PHP mesmo.
$sql = mysqli_query($this->conexao,"SELECT * FROM produtos WHERE IDProdutos = '".$idProdutos."';");
     $visualizar = "";

    while($jmTamanhos = mysqli_fetch_object($sql)){
        if($jmTamanhos->Tamanho != ""){
           $visualizar .=  "

                            <input type=\"text\" name=\"Tamanho[]\" class=\"form-control\" placeholder=\"Tamanho\" value='".$jmTamanhos->Tamanho."'>
                            <input type=\"text\" name=\"Estoque[]\" class=\"form-control pull-left\" placeholder=\"Estoque\" value='".$jmTamanhos->Estoque."'>
                            <button type=\"button\" class=\"removerCampo btn btn-danger\" title=\"Remover linha\"> Remover</button>

";
        }
     }
     return $visualizar;
 }


Tensorflow : TypeError: Fetch argument None has invalid type

Question: I am trying to run this simple program to compute gradient, But  i am getting None error :
import tensorflow as tf
import numpy as np

batch_size = 5
dim = 3
hidden_units = 8

sess = tf.Session()

with sess.as_default():
    x = tf.placeholder(dtype=tf.float32, shape=[None, dim], name="x")
    y = tf.placeholder(dtype=tf.int32, shape=[None], name="y")
    w = tf.Variable(initial_value=tf.random_normal(shape=[dim, hidden_units]), name="w")
    b = tf.Variable(initial_value=tf.zeros(shape=[hidden_units]), name="b")
    logits = tf.nn.tanh(tf.matmul(x, w) + b)

    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, y,name="xentropy")
    # define model end

    # begin training
    optimizer = tf.train.GradientDescentOptimizer(1e-5)
    grads_and_vars = optimizer.compute_gradients(cross_entropy, tf.trainable_variables())

    # generate data
    data = np.random.randn(batch_size, dim)
    labels = np.random.randint(0, 10, size=batch_size)

    sess.run(tf.initialize_all_variables())
    gradients_and_vars = sess.run(grads_and_vars, feed_dict={x:data, y:labels})
    for g, v in gradients_and_vars:
        if g is not None:
            print "****************this is variable*************"
            print "variable's shape:", v.shape
            print v
            print "****************this is gradient*************"
            print "gradient's shape:", g.shape
            print g

sess.close()

Error :
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-8096b2e21e06> in <module>()
     29
     30     sess.run(tf.initialize_all_variables())
---> 31     outnet = sess.run(grads_and_vars, feed_dict={x:data, y:labels})
     32 #     print(gradients_and_vars)
     33 #         if g is not None:

//anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    893     try:
    894       result = self._run(None, fetches, feed_dict, options_ptr,
--> 895                          run_metadata_ptr)
    896       if run_metadata:
    897         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

//anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1107     # Create a fetch handler to take care of the structure of fetches.
   1108     fetch_handler = _FetchHandler(
-> 1109         self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles)
   1110
   1111     # Run request and get response.

//anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in __init__(self, graph, fetches, feeds, feed_handles)
    411     """
    412     with graph.as_default():
--> 413       self._fetch_mapper = _FetchMapper.for_fetch(fetches)
    414     self._fetches = []
    415     self._targets = []

//anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in for_fetch(fetch)
    231     elif isinstance(fetch, (list, tuple)):
    232       # NOTE(touts): This is also the code path for namedtuples.
--> 233       return _ListFetchMapper(fetch)
    234     elif isinstance(fetch, dict):
    235       return _DictFetchMapper(fetch)

//anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in __init__(self, fetches)
    338     """
    339     self._fetch_type = type(fetches)
--> 340     self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
    341     self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers)
    342

//anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in <listcomp>(.0)
    338     """
    339     self._fetch_type = type(fetches)
--> 340     self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
    341     self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers)
    342

//anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in for_fetch(fetch)
    231     elif isinstance(fetch, (list, tuple)):
    232       # NOTE(touts): This is also the code path for namedtuples.
--> 233       return _ListFetchMapper(fetch)
    234     elif isinstance(fetch, dict):
    235       return _DictFetchMapper(fetch)

//anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in __init__(self, fetches)
    338     """
    339     self._fetch_type = type(fetches)
--> 340     self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
    341     self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers)
    342

//anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in <listcomp>(.0)
    338     """
    339     self._fetch_type = type(fetches)
--> 340     self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
    341     self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers)
    342

//anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in for_fetch(fetch)
    228     if fetch is None:
    229       raise TypeError('Fetch argument %r has invalid type %r' %
--> 230                       (fetch, type(fetch)))
    231     elif isinstance(fetch, (list, tuple)):
    232       # NOTE(touts): This is also the code path for namedtuples.

TypeError: Fetch argument None has invalid type <class 'NoneType'>

Why it's giving this error ? Version issue ?
Answer: Gradients are returned None if there are no explicit connections between them in the graph. In your code, it seems all the variables declared have connections, so may be a case of loading variables from other graph. You can use:
 print([v.name for v in tf.all_variables()])

and check only intended variables are part of this graph.
Try something like this:
sess.run(tf.initialize_all_variables())
gradients_and_vars = sess.run([variable for grad,variable in grads_and_vars], feed_dict={x:data, y:labels})
print(gradients_and_vars)

Comment: So , I am building autoencoder , I have one question , data values are float and int values , should i normalize the data before feeding to autoencoder or feed raw data ?


Drupal 7 js popup

Question: Trying to put a popup window on my drupal 7 site that hides/shows on click.
Here's the popup.js file:
Drupal.behaviors.my_custom_behavior = {
attach: function(context, settings) {

function donpopup(cat) {
  $('div.popup-dialog').hide();
  $('div#popup-dialog-' + cat).show();
  return false;
}

function closepop() {
 $('div.popup-dialog').hide();
}

}
};

})(jQuery, Drupal, this, this.document);

Here's the css:
 div.popup-dialog {
 display:none;
 background-color: #FFFFFF;
 border: 1px solid #000000;
 border-radius: 4px 4px 4px 4px;
 box-shadow: 3px 3px 3px #AAAAAA;
 left: 25px;
 padding: 10px 15px;
 position: absolute;
 top: 20px;
 width: 300px;
 z-index: 100;
 }

 div.popcls{
 float:right;
 display:block;
 overflow:hidden;
 }

And here's the html

x

I made sure that I added my js file in the theme .info..
But it doesn't work. Can anyone tell me what I'm doing wrong? Thanks!
Comment: I don't see anywhere that donpopup(cat) is called. there's an onclick to close it, but I don't see one to open it.
Comment: Oh duh.. you're right. I'm a js newb, I snagged this from an online tutorial and then spent an hour figuring out how to get js files to load in drupal. Any idea how to fix it? Thanks!
Comment: actually.. i don't think this does what I want it to at all. If I understand this right, this starts with the window already popped up. And I need to show/hide on a click. Back to the drawing board.
Comment: In short, a function that is locale to `Drupal.behaviors.my_custom_behavior.attach()` is not global; therefore, it is not visible outside `Drupal.behaviors.my_custom_behavior.attach()`. This is a plain JavaScript question, not a Drupal question.
Answer: I just did this for a small website and I used some drupal modules, rather than trying to hardcode stuff like this.
I used ctools_automodal. You supply a hook_menu function with a special key modal => TRUE. You then supply a page callback that uses drupal_get_form to show your modal content. The content need not be a full on form persay, you can use $form['#markup'] to show non form stuff like paragraphs and images. From the project page it looks like this:
function mymodule_menu() {
  $items['mymodule/form'] = array(
    'page callback' => 'drupal_get_form',
    'page arguments' => array('mymodule_form'),
    'access arguments' => array('administer mymodule'),
    'modal' => TRUE, // This line is where the magic happens.
  );
}

// your custom form content could look like this:
function mymodule_form($form, &$form_state) {
  $form['info'] = array(
  '#markup' => "
Some Info here ...
",
  );
}

Then the last part you need to do is put a link to this custom modal window using the l() or the url() function, or like views_linkarea. The link must be generated by Drupal and not plain old HTML as you've done.
By using the modules above and the drupal API your popup window is more incorporated into Drupal, and if you want to add Panels or alter forms to include this popup link it's very easy. Also, note you haven't written an Javascript -- the ctools_automodal module is hiding all the complexity from you :).
Simply add CSS rules that target this modal form to style it as you need.
Comment: this would work, but i'm curious what the hardcoded version would look like.

i'm digging into changing css onclick to hide/show now
Comment: hmm, then i recommend debugging your webpage using Firebug what pieces of the code is broken. It's simply an academic task to find the errors. 1 benefit of drupal is to "stand on the shoulders of others" and not need to debug boilerplate hand-written code such as your example.

Drupal 7 js popup


Is my Cox regression variable linear?

Question:
I am evaluating linearity on Serum Creatinine for Cox regression.
It seems to be influenced heavily by 2 points in the middle. Is this okay or should I look into transforming?
X <- HF$serum_creatinine
Y <- resid(reducedMod, type = "martingale")
plot(X, Y, pch = 20, col = "darkgray",
     xlab = "Serum Creatine", ylab = "Martingale Residual")+
abline(h = 0)+
lines(smooth.spline(X, Y, df = 7), lty = 2, lwd = 2)

Comment: ***No*** variable is "linear"**:** ***relationships*** between variables are linear (or not).
Comment: How did you fit the smoothed curve? It might be possible to get some type of error estimate for the fit, to see if the apparent dip due to those 2 points is really anything to worry about. Please add that information by editing the question. (Also, is this really "creatine" or "creatinine"? The latter is the more usual clinical measure.)
Comment: I added the code I used for plotting!
Answer: There is often not a "yes/no" answer to this type of question. Linearity is almost never perfect in real-world data. You have to apply your understanding of the subject matter to decide if any lack of linearity between the outcome and the predictor is big enough to matter.
In this particular situation, you might consider whether the two data points with very negative martingale residuals (and thus deviance residuals) represent outliers due to errors in data collection, with the usual precautions about (a) removing cases just because they seem to be outliers and (b) only evaluating cases with large residuals for potential errors in data collection.
In general, it's best for this type of evaluation to use a smoothed fit together with an estimate of the error in the smooth. That doesn't seem to be very straightforward with the smooth.spline() you used. I suspect that there's a lot of error in the estimate of the smooth in the region that concerns you. A loess() fit can return both the curve and error estimates, with its associated predict() function. That might be even more simply done with the ggplot2 package and its built-in functions like geom_smooth().
You also allowed for an extremely flexible fit with 7 degrees of freedom. That's perhaps appropriate if you have reason to expect rapid local changes in the Y-X association, but for this type of model any underlying non-linearity is more likely to be gradual. Finally, with most of your values at the lower end of the range, a display with serum_creatinine on a log scale might be more representative of the data set as a whole.

Is my Cox regression variable linear?


Is it possible to solve a system of first-order linear PDE's through a matricial approach?

Question: Consider the following ODE:
$$P_n\frac{d f_n(x)}{d x} = \sum_{n' = 1}^NQ_{n,n'}f_{n'}(x),$$
for $n$ and $n' = 1, 2, ..., N$, and in which $P_n$ and $Q_{n,n'}$ are real constant parameters. If we expand this equation for every possible value of $n$ and $n'$, we would get a $N \times N$ system of ODE's that could be rewritten like:
$$\frac{d}{dx}\begin{bmatrix}f_1(x)\\\vdots\\f_N(x)\end{bmatrix}=\begin{bmatrix}Q_{1,1}/P_1&...&Q_{1,N}/P_1\\\vdots&\ddots&\vdots\\Q_{N,1}/P_N&...&Q_{N,N}/P_N\end{bmatrix} \begin{bmatrix}f_1(x)\\\vdots\\f_N(x)\end{bmatrix}.$$
So, if we call $\textbf{f}\equiv[f_1(x),...,f_N(x)]^T$ and the $N\times N$ matrix above (which we will consider henceforth to be non-singular) as $\textbf{M}$, then we could rewrite it again as:
$$\frac{d\textbf{f}}{dx}=\textbf{M}\textbf{f}.$$
Thus, we can suppose a solution to $\textbf{f}$ of the type:
$$\textbf{f} = \textbf{ξ}e^{\lambda x},$$
in which $\textbf{ξ}$ is the constant eigenvector associated with a particular eigenvalue $\lambda$ of matrix $\textbf{M}$. So, if matrix $\textbf{M}$ has $L$ eigenvalues $\lambda_l$ (so that $l = 1,2,...L$ and $L \le N)$ then the general solution for $f_n(x)$ would be:
$$f_n(x)=\sum_{l=1}^Lc_l ξ_n^{l}e^{\lambda_lx},$$
in which $ξ_n^{l}$ is the $n$-th coordinate of the eigenvector associated with the $l$-th eigenvalue of matrix $\textbf{M}$, and $c_l$ are constants determined by the boundary conditions of the problem, which are of no interest now.
My actual doubt:
Consider now the following PDE:
$$A_n\frac{\partial f_n(x,y,z)}{\partial x} + B_n\frac{\partial f_n(x,y,z)}{\partial y} + C_n\frac{\partial f_n(x,y,z)}{\partial z} = \sum_{n' = 1}^ND_{n,n'}f_{n'}(x,y,z),$$
for $n$ and $n' = 1, 2, ..., N$, and in which $A_n, B_n, C_n$ and $D_{n,n'}$ are real constant parameters. If we expand this equation for every possible value of $n$ and $n'$, we would get a $N \times N$ system of PDE's.
Is it possible to solve this system of PDE's through that same matricial approach that I presented for the ODE's, or through an equivalent one?
Answer: Consider solutions of the form $f_n(x,y,z) = u_n \exp(a x + b y + c z)$.
You get a system of linear equations $(A_n a + B_n b + C_n c) u_n - \sum_{n'} D_{n,n'} u_{n'} = 0$.  These have nontrivial solutions if and only if $(a,b,c)$ makes the
determinant $P(a,b,c)$ of the coefficient matrix zero.  A difference between this and the ODE case is that instead of having a finite number of eigenvalues, $P(a,b,c) = 0$ is in general a two-dimensional variety in $\mathbb C^3$.
Comment: Thank you very much!


How to create custom error classes in Ember?

Question: What is the right way to create custom error classes in Ember and where to put the error class definition files in Ember CLI?
All code samples that I have found are messing around with JavaScript object prototypes. Why can't I just call Ember.Error.extend like we do for normal Ember objects?
Proper place for custom error classes should be under app/errors/ directory, but it seems that Ember CLI is not resolving those files.
Answer: Create custom file for example in app/errors/ directory, and call it custom-error.js.
Use following code as base to declare your custom error class:
import Ember from 'ember';

let CustomError = function (errors, message = 'This error is result of my custom logic.') {
  Ember.Error.call(this, message);

  this.errors = errors || [
    {
      title: 'This is custom error.',
      detail: message
    }
  ];
}

CustomError.prototype = Object.create(Ember.Error.prototype);

export default CustomError;

Then if you want to use this error somewhere:
import Ember from 'ember';
import CustomError from '../errors/custom-error';

export default Ember.Controller.extend({
  appName: 'Ember Twiddle',
  testCustomError: Ember.on('init', () => {
    let customErrorInstance = new CustomError();
    console.log(customErrorInstance);
  })
});

Result of console.log(customErrorInstance) is:

CustomError {description: undefined, fileName: undefined, lineNumber:
  undefined, message: "This error is result of my custom logic.", name:
  "Error"…}

Working demo.
Full code behind demo.
Comment: Thanks for quick and detailed answer. Got my code working nicely.


VS2010 hanging a lot lately. Should I try SP1 beta?

Question: VS2010 start hanging in some dummy operations like paste, scrolling, opening a file (video). Very frustrating.
I've disabled all extensions, but still no luck.
Is SP1 beta stable enough to take a chance on it?
Answer: I don't have those issues myself.  Taking a glance over the issues list I don't see anything particularly related to that.  In fact, the performance feedback is being requested with some details.  Check the link.

We continue to see a lot of feedback
  on performance in particular which we
  would like to correlate with machine
  configs and project types.  If you can
  send your performance related issues
  to firstname.lastname@example.com we want to do
  more investigation of the issues.
Answer: I'd always prefer reinstalling the RTM over using a beta for production code.
Comment: I don't have big hopes that a reinstall will solve the issues, and don't have time for a complete repav.
Answer: Finally installed SP1 beta. So far no hangings, It seams that VS2010 SP1 handle the errors better before completely crashing (even when is extension related)


Exception while reading/writing file

Question: I have two android apps, one app accesses the content provider of the other apps to access some file after receiving data from other app. I try to write it to a file but get an exception.
Below is my piece of code
private String getLog(String logFile)
{
    try {
        Uri fileUri = Uri.parse("content://" + AUTHORITY + "/" + logFile);
        ParcelFileDescriptor logFilePath = ControlApplication.getControlApplicationContext()
            .getContentResolver().openFileDescriptor(fileUri, "r");
        InputStream fileIs = new FileInputStream(logFilePath.getFileDescriptor());
        InputStreamReader tmp = new InputStreamReader(fileIs);
        BufferedReader reader = new BufferedReader(tmp);
        String str;
        StringBuffer buf = new StringBuffer();
        while ((str = reader.readLine()) != null) {
            buf.append(str + "\n");
        }
        fileIs.close();
        FileOutputStream outstream = ControlApplication.getControlApplicationContext()
            .openFileOutput(logFile, Context.MODE_PRIVATE);
        outstream.write(buf.toString().getBytes());
        outstream.close();

    }
    catch (Exception e) {
        Maas360Logger.e(loggerName, e, "Exception occured");
    }

    return logFile;
}

Here, logFile is the log file that would already exist from the other app. This file I am trying to get through my content provider.
The exception below is thrown while reading from reader even though I am not creating a duplicate directory:
01-03-2016 19:03:48.265 | E | AsyncTask #9 | DiagnosticServices | Exception occured
java.io.IOException: read failed: EISDIR (Is a directory)

Caused by: read failed: EISDIR (Is a directory)
at libcore.io.IoBridge.read(IoBridge.java:482)
at java.io.FileInputStream.read(FileInputStream.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:231)
at java.io.BufferedReader.fillBuf(BufferedReader.java:145)
at java.io.BufferedReader.readLine(BufferedReader.java:397)

Answer: The problem here is that you're trying to readLine on a directory.
Print out the value of "content://" + AUTHORITY + "/" + logFile and ensure this is what you intend to read.
You can also verify this is a directory like so:
File folder = new File("content://" + AUTHORITY + "/" + logFile");
boolean isDir = folder.isDirectory();

If you want to see what files you should be opening inside this directory, try this answer.

Exception while reading/writing file


Mudar cor de fundo de forma dinâmica

Question: Eu tenho esse código que pega as informações de um Pokémon e eu queria mudar a cor do background de Type dependendo do tipo do Pokémon. Caso ele seja elétrico ficar amarelo, de água ficar azul etc. Como eu faria isso?
<Info>
               {data.name}
               Type: {pokemonType}
              Height: {data.height}
           Weight: {data.weight} kg
     </Info>
  ```

Answer: Faça uma função pra retornar a cor de acordo com o tipo, e determine o background-color de maneira dinâmica, pode fazer usando o useRef ou assim :

function getTypeColor(type){
  switch(type){
    case 'earth': return 'brown';break;
    case 'water': return 'blue';break;
    case 'electric': return 'yellow';break;
    // repetir todos os tipos retornando suas cores, usar rbg ou hex pra cor exata
    default: return 'black'
  }
}

<Info>
    {data.name}
    Type: {pokemonType}
    Height: {data.height}
    Weight: {data.weight} kg
</Info>

Se quiser apenas o tipo com cor diferente, basta envolver apenas o tipo numa div com a lógica acima, assim:

  Type: {pokemonType}

Answer: Você poderia criar uma função com object literals. Exemplo:
function getBackgroundColorType(type) {
const colorsType = {
    earth: 'brown',
    water: 'blue',
    fire: 'red',
    electric: 'yellow',
    // E assim por diante...
};

const lowerCaseType = type.toLowerCase();

if (colorsType.hasOwnProperty(lowerCaseType)) {
    return colorsType[lowerCaseType];
}

// Caso o tipo não seja encontrado, retorne uma cor padrão
return 'gray'; // ou qualquer outra cor que você queira usar como padrão

}

{name}

Type: {type}

Mudar cor de fundo de forma dinâmica


create symlink to another machine

Question: I have 2 machines. Both running CentOS. Box1 is webserver with apache, php. Box2 is mysql, and file storage. The files will only be accessible from Box1 within the webapp.
I'd like to somehow create a symlink or somesuch on box1 to a folder on box2 where uploaded files can be stored and retrieved. Security in mind, what would be the best way to go about linking these 2 boxes up in a transparent (to apache) way?
NB: the boxes are connected directly to each other via a crossover cable; no lan access to box2.
Much thanks!
Answer: You would need to run NFS on box2, mount it on box1, and then create an NFS link to wherever you mounted it.

create symlink to another machine


How to integrate $I = \int_{-a/2}^{a/2}\frac{1}{\sqrt{x^2 + b}}dx$

Question: $$I = \int_{-a/2}^{a/2}\frac{1}{\sqrt{x^2 + b}}dx$$
I tried to integrate by parts but failed. I think I'm supposed to change variables using a hyperbolic sine, but I don't know this method.
Answer: Assuming $a,b>0$ we have:
$$ \int_{-a/2}^{a/2}\frac{dx}{\sqrt{x^2+b}}=2\int_{0}^{\frac{a}{2\sqrt{b}}}\frac{dx}{\sqrt{x^2+1}}=2\operatorname{arcsinh}\frac{a}{2\sqrt{b}}=\log\frac{a+\sqrt{a^2+4b}}{-a+\sqrt{a^2+4b}}.$$
Answer: Since
$$\sinh x=\frac{e^x-e^{-x}}2$$
and
$$\cosh x=\frac{e^{x}+e^{-x}}2$$
we have that
$$\cosh^2 x-\sinh^2 x=1$$
Assuming that $b>0$, write
$$\sqrt{x^2+b}=\sqrt b\cdot \sqrt{\left(\frac x{\sqrt b}\right)^2+1}$$
and now,
$$\frac x{\sqrt b}=\sinh t$$
Answer: If $b=0$, then the problem is trivial.
If $b>0$, then you should take a look at the function $\ln(x+\sqrt{1+x^2})$ (hyperbolic arcsin, aka areasinus). Its derivative is equal to $1/\sqrt{1+x^2}$ (found in any reference book on derivatives/primitives).
If $b<0$, then you should take a look at $\ln(\sqrt{x^2-1}+x)$ (see comment above).


2019: a year in moderation

Question: It's New Year's Day in Stack Exchange land...
A distinguishing characteristic of these sites is how they are moderated:

We designed the Stack Exchange network engine to be mostly self-regulating, in that we amortize the overall moderation cost of the system across thousands of teeny-tiny slices of effort contributed by regular, everyday users.
  -- A Theory of Moderation

While there certainly are Moderators here, a significant amount of the moderation is done by ordinary people, using the privileges
they've earned by virtue of their contributions to the site. Each of you contributes a little bit of time and effort, and together you accomplish much.
As we enter a new year, let's pause and reflect, taking a moment to appreciate the work that we do here together.
And what could be more festive than a big pile of numbers?
So here is a breakdown of moderation actions performed on Expatriates over the past 12 months:
<code>                  Action                  Moderators Community¹
----------------------------------------- ---------- ----------
Users suspended²                                   0          4
Users destroyed³                                   1          0
Users deleted                                      4          0
Tasks reviewed⁴: Suggested Edit queue             25        349
Tasks reviewed⁴: Reopen Vote queue                 0         18
Tasks reviewed⁴: Low Quality Posts queue           6        175
Tasks reviewed⁴: Late Answer queue                 5        173
Tasks reviewed⁴: First Post queue                  8        918
Tasks reviewed⁴: Close Votes queue                13        492
Tags merged                                        1          0
Tag synonyms proposed                              1          0
Tag synonyms created                               1          0
Revisions redacted                                 2          0
Questions reopened                                 1          0
Questions protected                                1         34
Questions migrated                                 2          0
Questions flagged⁵                                 3         98
Questions closed                                  19         80
Question flags handled⁵                           50         51
Posts undeleted                                    1          4
Posts locked                                       0         34
Posts deleted⁶                                   176        422
Posts bumped                                       0        294
Escalations to the Community Manager team          1          0
Comments undeleted                                 1          0
Comments flagged                                   0        129
Comments deleted⁷                                126        339
Comment flags handled                             46         83
Answers flagged                                   10        743
Answer flags handled                             648        106
All comments on a post moved to chat               2          0
</code>
Footnotes
¹ "Community" here refers both to the membership of Expatriates without diamonds next to their names, and to the automated systems otherwise known as user #-1.
² The system will suspend users under three circumstances: when a user is recreated after being previously suspended, when a user is recreated after being destroyed for spam or abuse, and when a network-wide suspension is in effect on an account.
³ A "destroyed" user is deleted along with all that they had posted: questions, answers, comments. Generally used as an expedient way of getting rid of spam.
⁴ This counts every review that was submitted (not skipped) - so the 2 suggested edits reviews needed to approve an edit would count as 2, the goal being to indicate the frequency of moderation actions. This also applies to flags, etc.
⁵ Includes close flags (but not close or reopen votes).
⁶ This ignores numerous deletions that happen automatically in response to some other action.
⁷ This includes comments deleted by their own authors (which also account for some number of handled comment flags).
Further reading:

Wanna see how these numbers have changed over time? I posted a similar report here last year: 2018: a year in moderation...
You can also check out this report on other sites
Or peruse detailed information on the number of questions closed and reopened across all sites

Wishing you all a happy new year...
Answer: As shown by the numbers above the community here is definitely doing a good job in handling whatever it can. I'd like this opportunity to say thank you to everyone participating in this site and making it as good as possible.

2019: a year in moderation


Path Breadcrumbs adds unwanted space in link path, why and how can I avoid it?

Question: I added in the link path field for a breadcrumb the following:
<code>%site:url%node:tax-title-for-url-construction
</code>
"%node:tax-title-for-url-construction" is a custom token which returns a taxonomy title like "Xbox 360" in a cleaner way "xbox-360".
So the links should look like http://domain.com/xbox-360 but it return the following http://domain.com/ xbox-360 Note the space between site url and tax title, but there is no space in the link path field within the breadcrumbs configuration. So where does this space come from?
Here is the code from my custom token:
<code><?php
$node = node_load(arg(1));
$rtaxidarray = field_get_items('node', $node, 'field_plattform');
$rtaxid = $rtaxidarray[0]["tid"];    // taxid sauber
$term = taxonomy_term_load($rtaxid);
$name = $term->name;
$small = strtolower($name);
$clean = str_replace(" ", "-", $small);
return $clean;
?>
</code>
Comment: Does this space shows in `dpr($clean)`? If not, does it appears when you use your tokens in other places than Path Breadcrumbs? If not, it might be a module's bug. Or maybe it's %site:url that returns that space, have you tested that?
Comment: Post it as a self-answer then, and let's forget about this case ;)
Answer: HM There was a space before the opening php tag in the custom token value field, I really hate spaces. I think spaces should be exchanged with a X ;)


Removing entities from Collection in hibernate

Question: I'm trying to manually delete every entity that's in a collection on an entity. The problem is, the entities don't get deleted from the database, even though they get removed from the collection on the task.
Below is the code im using to achieve this:
public int removeExistingCosts(final DataStoreTask task) {

    int removedAccumulator = 0;
    Query query = entityManager.createNamedQuery(DataStoreCost.GET_COSTS_FOR_TASK);
    query.setParameter(DataStoreCost.TASK_VARIABLE_NAME, task);

    try {

        List costsForTask = query.getResultList();
        for(Object cost : costsForTask) {
            task.getCosts().remove(cost);
            removedAccumulator++;
        }

    } catch (NoResultException e) {
        logger.debug("Couldn't costs for task: {}", task.getId());
    }

    entityManager.flush();
    entityManager.persist(task);

    return removedAccumulator;
}

Any ideas?
P.S the collection is represented as:
@OneToMany(targetEntity = DataStoreCost.class, mappedBy = "task", cascade = CascadeType.ALL)
private Collection<DataStoreCost> costs;

Cheers.
Answer: It's not deleting the entity, because it doesn't know if something else is referring to it.
You need to enable delete orphan.  In jpa2, use the orphanRemoval attribute.  If you're using hibernate annotations, use CascadeStyle delete orphan.
Answer: I think you need to explicitly remove the Cost entity via the entityManager. When you remove the Cost from the Tasks cost list you actually only remove the reference to that instance. It does not know that that particular Cost will not be used anywhere else.
Comment: Yes, call `entityManager.remove(cost);` in the for loop.


how to show all the siamese twins with diference 1 in discrete mathematics

Question: two prime numbers p and q are siamese twins if |p-q|= 1. List all the
siamese twins that exist, and prove your list is complete.
i have found a  siamese twin, 2 and 3 but how do i prove it is the list of all the
siamese twins with difference one?
pliz explain the answer as i am still a undergraduate student year 1.
Comment: All prime numbers greater or equal to $3$ are odd numbers. Therefore their distance is at least $2$. Hence the only siamese twins are $2$ and $3$.
Answer: First, take a look at the first few prime numbers; 2, 3, 5, 7, 11, 13, 17, 19, ...
Now, notice that 2 is the only even prime number, because every even integer is written in the form 2a, where a is some integer. That is, all prime numbers other than 2 are odd. Then you can see that no two primes can be consecutive integers other than 2 and 3, since the rest of the primes have to be two consecutive odd integers.


Summation Identity: Index Manipulation By Real Number Constant

Question: Is the following identity correct?
For any $a, b \in \mathbb{Z}$ such that $a \leq b$ and for any $c \in \mathbb{R}$, the following holds:
$$\sum_{i = a}^{b} f(i) = \sum_{i = a + c}^{b + c} f(i - c)$$
Comment: Looks good to me.
Answer: Your equation is true if you follow a certain convention. If $a,b\in\Bbb Z$ and $c\in\Bbb R$, in general $a+c,b+c\notin\Bbb Z$, so the summation with non-integral bounds is not obvious.
If we agree that summation with non-integral bounds means that you increase the dummy variable by $1$ for each member of the series, then your equation is correct. For example, we can say that
$$\sum_{k=1/2}^{5/2}g(x)=g(1/2)+g(3/2)+g(5/2)$$
but I am not sure that is standard.
If $c$ is an integer, your equation is well-understood and is correct.


expectation of the weighted sum of positive random variables

Question: Let $X_{i}(\omega)$, for $i=1,\dots,k$, and $Y(\omega)$ be the random variables defined on the same probability space $(\Omega, F,P)$, such that $E[X_{i}] \leq E[Y]$ for every $i$ and both of them are positive with probability $1$. Next, let $A_{i}$ be disjoint elements of the sigma-algebra $F$ such that $\cup_{i}A_{i} = \Omega$.
Is the following correct:
$$
\sum_{i}^{k}E[X_{i}I\{A_{i}\}] \leq E[Y]?
$$
Answer: No. Consider the following: $k=2$, $\Omega = \{1,2\}$, $P(1)=P(2) = 1/2$, and $A_1 = \{1\}$, $A_2=\{2\}$ with
$$
X_i = \frac{1}{50}+\mathbb{1}_{\{i\}},\qquad i\in\{1,2\}
$$
and $Y=1$ (constant r.v.). Then $\mathbb{E} X_i = \frac{1}{50}+\frac{1}{2} \leq \mathbb{E} Y$ for $i\in\{1,2\}$, yet
$$
\sum_{i=1}^2 \mathbb{E}[ X_i \mathbb{1}_{A_i} ]
= 2\cdot\frac{51}{100}  > 1 = \mathbb{E} Y
$$
(The $1/50$ is simply an annoying technicality to make the $X_i$'s positive with probability $1$. Remove it if you only want non-negative -- same idea, simpler answer.)
Answer: Let $k\geq2$, let $P(A_i)>0$ for $i\in\{1,\dots,k\}$ and let $c>0$.
Let $X_i=c+1_{A_i}$ and let $Y=c+1_A$ such that $\mathsf P(A_i)\leq\mathsf P(A)<1$ for every $i\in\{1,\dots,k\}$.
For instance you can take $A=A_{i_0}$ where $\mathsf P(A_{i_0})$ is maximal among the $\mathsf P(A_i)$.
Then $Y$ and the $X_i$ are  positive and:  $$\mathsf EX_i=c+\mathsf P(A_i)\leq c+\mathsf P(A)=\mathsf EY$$ for every $i\in\{1,\dots,k\}$.
But also we have: $$\sum_{i=1}^k\mathsf E[X_i1_{A_i}]=\sum_{i=1}^k(c+1)\mathsf P(A_i)=c+1>c+\mathsf P(A)=\mathsf EY$$
Comment: That's basically the same as my answer, slightly generalized for $k\geq 2$, isn't it?
Comment: @ClementC. Yes, there is no essential difference. I am staying a bit closer to the formulation of the question. I upvoted your answer.
Comment: acknowledged. (Same.)


Django: how to import a function

Question: Python 3.6.7
Django 2.1.5
In PyCharm Community edition I created a new project called simple_pr.
Then I organized a Django project:
django-admin startproject simple_project

Then an application:
python manage.py startapp home

My Pythonpath:
/home/michael/PycharmProjects/simple_pr/simple_project
/opt/pycharm-community-2018.3.2/helpers/pydev
/home/michael/PycharmProjects/simple_pr
/opt/pycharm-community-2018.3.2/helpers/third_party/thriftpy
/opt/pycharm-community-2018.3.2/helpers/pydev
/home/michael/.PyCharmCE2018.3/system/cythonExtensions
/home/michael/PycharmProjects/simple_pr/simple_project
/usr/lib/python36.zip
/usr/lib/python3.6
/usr/lib/python3.6/lib-dynload
/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages
/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/setuptools-39.1.0-py3.6.egg
/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/pip-10.0.1-py3.6.egg

Project structure:
(venv) michael@michael:~/PycharmProjects/simple_pr$ tree
.
├── simple_project
│   ├── home
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   │   └── __init__.py
│   │   ├── models.py
│   │   ├── tests.py
│   │   └── views.py
│   ├── manage.py
│   └── simple_project
│       ├── __init__.py
│       ├── __pycache__
│       │   ├── __init__.cpython-36.pyc
│       │   └── settings.cpython-36.pyc
│       ├── settings.py
│       ├── urls.py
│       └── wsgi.py
└── venv

urls.py
from django.contrib import admin
from django.urls import path
from simple_project.home.views import current_datetime

urlpatterns = [
    path('admin/', admin.site.urls),
]

home.views.py
from django.http import HttpResponse
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    html = "It is now %s." % now
    return HttpResponse(html)

settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'home',
]

Traceback:
/home/michael/PycharmProjects/simple_pr/venv/bin/python /opt/pycharm-community-2018.3.2/helpers/pydev/pydevd.py --multiproc --qt-support=auto --client 127.0.0.1 --port 37559 --file /home/michael/PycharmProjects/simple_pr/simple_project/manage.py runserver
pydev debugger: process 5982 is connecting

Connected to pydev debugger (build 183.4886.43)
pydev debugger: process 5988 is connecting

Performing system checks...

Unhandled exception in thread started by <_pydev_bundle.pydev_monkey._NewThreadStartupWithTrace object at 0x7f67b10a5940>
Traceback (most recent call last):
  File "/opt/pycharm-community-2018.3.2/helpers/pydev/_pydev_bundle/pydev_monkey.py", line 632, in __call__
    return self.original_func(*self.args, **self.kwargs)
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/core/management/base.py", line 379, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/core/management/base.py", line 366, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/core/checks/registry.py", line 71, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/urls/resolvers.py", line 533, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/django/urls/resolvers.py", line 526, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/michael/PycharmProjects/simple_pr/simple_project/simple_project/urls.py", line 18, in <module>
    from simple_project.home.views import current_datetime
ModuleNotFoundError: No module named 'simple_project.home'
/home/michael/PycharmProjects/simple_pr/simple_project
/opt/pycharm-community-2018.3.2/helpers/pydev
/home/michael/PycharmProjects/simple_pr
/opt/pycharm-community-2018.3.2/helpers/third_party/thriftpy
/opt/pycharm-community-2018.3.2/helpers/pydev
/home/michael/.PyCharmCE2018.3/system/cythonExtensions
/home/michael/PycharmProjects/simple_pr/simple_project
/usr/lib/python36.zip
/usr/lib/python3.6
/usr/lib/python3.6/lib-dynload
/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages
/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/setuptools-39.1.0-py3.6.egg
/home/michael/PycharmProjects/simple_pr/venv/lib/python3.6/site-packages/pip-10.0.1-py3.6.egg

Well, in other words, the problem is located here:
from simple_project.home.views import current_datetime
    ModuleNotFoundError: No module named 'simple_project.home'

By the way, PyCharm itself suggested me how to import it with its code completion function.
Could you help me understand what is wrong with my importation?
Comment: add `settings.py` INSTALLED_APPS section
Comment: As you suggested, I have added it. Please, have a look.
Answer: You shouldn't include the project name in the module path.
from home.views import current_datetime

Comment: Seems to be reasonable. I have tried. No exception has been risen. But now PyCharm's code completion not working and PyCharm underlines the line as a erroneous. Could you comment?

Maybe I should mark the directory as source route?

Django: how to import a function


Query foreign key and constraint's name of table in sqlserver

Question: I know that I can see relationship by create a diagram, however, I want to get all relationship's table in sqlserver database by using a query. Please share if you can! Thanks.
Answer: I've found a solution to get the relationship's table:
<code>SELECT
       OBJECT_NAME(f.parent_object_id) TableName,
       COL_NAME(fc.parent_object_id,fc.parent_column_id) ColName
    FROM
       sys.foreign_keys AS f
    INNER JOIN
       sys.foreign_key_columns AS fc
          ON f.OBJECT_ID = fc.constraint_object_id
    INNER JOIN
       sys.tables t
          ON t.OBJECT_ID = fc.referenced_object_id
    WHERE
       OBJECT_NAME (f.referenced_object_id) = 'YourTableName'
</code>


Count number of bytes of all gzip files in a directory

Question: I have a bash script that needs to know the count of bytes for all gzip files in a directory.  For now, I'm assuming it's just a single directory without sub-directories.  It's very tempting to do something like this:
du -scb /my/dir/*.gz|tail -n 1
However, I have a TON of files.  Won't the *.gz expand to some sort of overflow condition?  Is there a faster, safer way to check this number?
Comment: @Chris I think he's worried about exceeding the command line length limit. On most Linux systems it's pretty huge (128kB, if I remember right) but on some *nixes it's pretty small (I think on Solaris it's something like 255 bytes).
Comment: What you have seems fine. Any decent implementation would support 64-bit files (larger than 2GB, I mean), and thus 64-bit totals too.
Comment: The command line length limit is my concern.  Is there a way to set and get that value from bash?
Answer: This works and is "safe":
(find . -type f -print0 |
  xargs -0 stat -c '%s' |
  tr '\n' '+'; echo 0) |
  bc

How it works:

First, use find to find the '.gz' files. Print them with nul-separators, so we can deal with weird filenames.
xargs will break up the groups of filenames into manageable chunks. Give those filenames to stat -c '%s' to get the sizes in bytes (thanks @Fritschy).
tr converts the newlines to +. The echo adds one extra 0 at the end plus a newline. This is so we don't end up with a dangling plus sign, and bc needs a newline at the end of its input.
Feed the huge sum into bc.
Comment: Nice one, check `stat(1)`, which can stat a file and print info according to a given format.
Comment: @Fritschy: thanks, I didn't know about `stat(1)`. Incorporated into the answer.
Answer: echo $(( $(find . -type f -name '*.gz' -printf '%s+') 0 ))

If you need huge values, change '%s+' to '%s+0' and change the $(( ... 0 )) to ... | bc
Edit: For even larger values, you might want to use %k instead of %s, giving you kilobytes. Since the integer might overflow.
Since we are only using built-ins in bash, I'm not sure if the argument-length of executables are problem.


$T$ has a finite rank $\iff$ $\exists N \in \mathbb{N}$ such that $\lambda_n=0$, $\forall n \geq N$

Question: The question goes as follows:
$T$ has a finite rank $\iff$ $\exists N \in \mathbb{N}$ such that $\lambda_n=0$, $\forall n \geq N$.
Given is the data:  $X$ is a Hilbert space with an orthonormal basis $(e_n)$.
The linear operator $T: X \rightarrow X$ is defined as;
$Tx=\sum\limits_{n=1}^{\infty} \lambda_n(x,e_n)e_n$.
In a previous question I solved $T \in K(X)$ $\iff$ $\lambda_n \rightarrow 0$, which I`m supposed to use.
My attempt:
If $T$ is a compact operator with infinite rank, then $\lambda_n \rightarrow 0$ as $n \rightarrow \infty$. So if $T$ has rank $K$, with $K$ finite, then $\lambda_n \rightarrow 0$ still has to hold. But because no values of $\lambda_n$ are plugged in for $n > K$, thus for $N=K-1$ we have, $\lambda_n=0$ for all $N \in \mathbb{N}$ whenever $n \geq N$.
The $\leftarrow$ way I understand.
Comment: What does finite rank mean?
Answer: By definition, rank of $T$ = dimension of the range $R(T)$. We have $Te_n=\lambda_n e_n$, and if $\lambda_n\ne0$, then $e_n=T(\lambda_n^{-1}e_n)$; i.e., $e_n\in R(T)$. Since $\operatorname{dim} R(T)<\infty$, it follows that $\lambda_n\ne0$ only for finitely many $n$. Thus, $\exists N\colon \lambda_n=0$ for $n>N$. QED.


Hide/remove "Last" label/icon in views pager

Question: I don't want my user to click the "Last" label on the views pager, but I can't find a way to remove this "Last" label even I leave the "Last page link text" field empty, I don't want to use the mini pager, so in the full pager, how can we remove the "Last" label/icon ? Thanks
Answer: You can override pager.html.twig in your theme, removing the section that adds the 'last' link:
{% if items.last %}

      {{ 'Last page'|t }}
      {{ items.last.text|default('Last »'|t) }}

{% endif %}


How to Interpret coefficients of a beta regression model with logit link

Question: I'm running a beta regression in R where the outcome variable is continuous but bounded between 0-1. I need the results to be understandable to an audience with a very basic stats background. I need some help interpreting the coefficients. My model is very basic and look like:
betareg(Y ~ ANE + factor(Year) +factor(Month) +factor(Sector))
I'm not sure how to interpret the coefficient on ANE. Below are the results I got for the ANE variable:

Coefficients (mean model with logit link):
              Estimate  Std. Error  z value  Pr(>|z|)
(Intercept) 1.036e+00 : 7.735e-02 : 13.395 : < 2e-16 ***
ANE        -1.693e-05 : 6.948e-06 : -2.437 : 0.014803 *

Answer: I know this question is kind of old, but it has hundereds of views (including myself, as I went hunting for an answer to this very question today), so maybe someone else might find this information useful. I came across this post: interpretation of betareg coef , which helped me a lot, but I still had to make the leap to understand how to translate the coefficients into the original proportional space.
Essentially, you can express your coefficients in terms of
$$x/(1-x)=exp(\text{coefficient or sum of coefficients})$$ and solve for x, so that $$x=exp(\text{coefficient})/(1+exp(\text{coefficient}))$$ so for the above example, the intercept would equal $exp(1.036)/(1+exp(1.036))$, or $2.817923/3.817923$, which equals $0.7380775$. Then ANE would be $exp(1.036-0.00001693)/(1+exp(1.036-0.00001693))$, or $0.7380742$, which is an absolute difference of $-0.0000033$.
It then gets trickier to figure out absolute values for slopes for year or month, which you haven't shown, but would be of the form $$exp(1.036-0.00001693+\text{year_coefficient}*Year)/(1+exp(1.036-0.00001693+\text{year_coefficient}*Year))$$
and then you have to figure out how to deal with Year. I decided to calculate a change in 1 from the intercept for the local rate of change, and then additionally calculate the max and min for the overall scope of change.