Heteroskedasticity underlying data generation process in logistic regression

Question: We can develop the logistic regression model using the latent variable approach:
\begin{equation}
y =
  \begin{cases}
  1, & \mathrm{if}\ X\beta + \epsilon > 0\\
  0, & \mathrm{otherwise}
  \end{cases}
\end{equation}
where $\epsilon \sim \mathcal{L}(0,1)$. This development is equivalent to the generalized linear model formulation which makes no mention of logistic errors:
\begin{align}
\begin{split}
P(y = 1 \mid X) &{}= P(X\beta + \epsilon > 0 \mid X) = P(\epsilon > -X\beta \mid X) \\
&{}= P(\epsilon < X\beta \mid X) \quad \epsilon \text{ is symmetric about about zero}\\
&{}= \frac{1}{1+e^{-X\beta}} \quad \text{equivalent to GLM formulation}
\end{split}
\end{align}
My question is how can one obtain unbiased estimates of $\beta$ when the error is heteroskedastic, $\epsilon_i \sim \mathcal{L}(0,\sigma_i)$? I report a simple simulation to demonstrate the problem. The data generation process for the logistic regression model under heteroskedasticity was:
\begin{equation}
y =
  \begin{cases}
  1, & \mathrm{if}\ x + \epsilon > 0\\
  0, & \mathrm{otherwise}
  \end{cases} \quad \text{where }x\sim\mathcal{N}(0, 1)\text{ and }\epsilon\sim\mathcal{L}(0,\lvert x\rvert)
\end{equation}
R syntax:
set.seed(12345)
res <- t(replicate(10000, { # 10,000 replications
  dat <- data.frame(x = rnorm(500))
  # Use Bernoulli RNG to determine homoskedasticity or heteroskedasticity
  flip <- rbinom(1, 1, .5)
  # Assign logistic error to data frame
  ifelse(flip == 0, dat$error <- rlogis(nrow(dat)),
         dat$error <- rlogis(nrow(dat), scale = abs(dat$x)))
  # Create binary outcome
  dat$y <- (rowSums(dat) > 0) + 0
  # Return coin flip and logistic regression coefficient
  c(flip, unname(coef(glm(y ~ x, binomial, dat))["x"]))
}))
# Summarize the results
par(mfrow = c(1, 2))
hist(res[res[, 1] == 0, 2], main = paste(
  "homosked, mean:", round(mean(res[res[, 1] == 0, 2]), 3)))
abline(v = mean(res[res[, 1] == 0, 2]))
hist(res[res[, 1] == 1, 2], main = paste(
  "heterosked, mean:", round(mean(res[res[, 1] == 1, 2]), 3)))
abline(v = mean(res[res[, 1] == 1, 2]))
par(mfrow = c(1, 1))

As is evident from the plot above, when the logistic error is heteroskedastic, the coefficient estimated using ML is biased downwards. Does anyone know an approach for unbiased estimation of the model coefficients under this form of heteroskedasticity? Not the specific process here, but under heteroskedastic logistic errors.
Comment: Some good intuition about this from David Giles' [blog](https://davegiles.blogspot.com/2015/06/logit-probit-heteroskedasticity.html).
Comment: @DimitriyV.Masterov thanks for the link. I came across the blog post at some point. I went into a little detail in my question because there are people who are only familiar with the GLM formulation for the logistic regression model.
Comment: If you really only care about $\beta$, note that it is possible to estimate
this up to scale without assuming that $e$ and $x$ are independent, and without assuming a distribution on $e$ using the maximum score estimator - in fact even the variance $Var(e|x)$ is unrestricted. What you need to assume is that  the median of $e$ and $x$ is 0 and that at least of on the estimated parameters is not zero and continuous
Comment: @Repmat Although I have accepted an answer, can you develop this further in an answer below? I or anyone who comes across this question could benefit from it.
Answer: The model you are describing is known in econometrics as the heteroscedastic logit model and if you specify the regression relationship for the latent scale parameter, you can estimate it by maximum likelihood. Of course, this does not have to be unbiased but it will be consistent and asymptotically normal provided that you have specified everything correctly.
If you look at the literature, I suggest that you look for heteroscedastic probit rather than logit because there seems to be more material on the Gaussian case. However, clearly the same principles apply to logit. A simple package in R that allows to estimate these models is glmx with function hetglm().
Having said that: A common misunderstanding about these models is, though, that they are similar to linear regression with heteroscedastic errors. However, the heteroscedasticity is only on the latent scale. On the manifest scale, the heteroscedasticity will look like nonlinearity in the mean! Specifically, in the case you designed you simply get a step function.
Let's look at this with some more notation. I'll discuss the Gaussian case here as I'm re-using materials from presentation slides (ERCIM-2013.pdf) on this topic.
We assume a latent variable $y^*$ and corresponding manifest response $y$:
$$
\begin{eqnarray*}
y_i^* & = & x_i^\top \beta + \varepsilon_i \\
y_i & = & I(y_i^* > 0)
\end{eqnarray*}
$$
For homoscedastic Gaussian errors $\varepsilon_i \sim \mathcal{N}(0, \sigma^2)$:
$$
\begin{eqnarray*}
  \text{Prob}(y_i = 1 ~|~ x_i) & = & \text{Prob}(y_i^* > 0 ~|~ x_i) \\
%%                             & = & \text{Prob}(-\varepsilon_i > x_i^\top \beta ~|~ x_i) \\
                               & = & \Phi \left( \frac{x_i^\top \beta - 0}{\sigma} \right)
\end{eqnarray*}
$$
In this case the scale parameter $\sigma$ is not identified and hence taken to be $\sigma = 1$. However, while we cannot identify both $\beta$ and $\sigma$ in absolute terms, we can identify scale differences via:
$$
\begin{equation*}
  \log(\sigma_i) ~=~ z_i^\top \gamma
\end{equation*}
$$
where $z_i$ must not include a constant term for identifiability. Instead of using a log-link as above to assure positivity of $\sigma_i$ you can also assume an identity link. In that case, one should really add a constant though to avoid $\sigma_i$ becoming zero:
$$
\begin{equation*}
  \sigma_i ~=~ 1 + z_i^\top \gamma
\end{equation*}
$$
so that when $z_i = 0$ or $\gamma = 0$ you drop back to the standard probit model. The resulting mean function of the model is then:
$$
\begin{equation*}
  E(y_i | x_i) ~=~ \text{Prob}(y_i = 1 ~|~ x_i) ~=~ \Phi \left( \frac{x_i^\top \beta}{\sigma_i} \right)
\end{equation*}
$$
Thus, on top of the nonlinearity due to the probit link function you get a rational specification instead of the usual linear predictor. So even on the link scale, this is nonlinear.
With your specification of $\eta_i = 0 + 1 \cdot x_i$ and $\sigma_i = |x_i|$ you get:
$$
  \Phi^{-1}(E(y_i | x_i)) ~=~ \frac{0 + x_i}{|x_i|} ~=~ \text{sign}(x_i)
$$
Exactly the same applies in the logit case. Thus, your conditional mean aka the conditional "success" probability simply switches from low to high when $x_i$ changes sign. (Note that, strictly speaking, for $x_i = 0$ the above is not defined, though.) Thus, if you want to estimate that model you could also simply use glm(y ~ factor(x > 0), family = binomial).
The set of slides linked above point out a few other potential problems and pitfalls. All in all, I would recommend to only use these models if there are convincing assumptions that heteroscedasticity on a latent scale is really the issue. Otherwise I would simply use a logit GAM instead of a logit GLM with smooth nonlinear effects. Such convincing cases do exist, see e.g., our precipitation forecasting example in doi:10.1175/MWR-D-13-00271.1. But in economics and the social sciences this is more difficult, e.g., as pointed out by Alvarez & Brehm (1995, American Journal of Political Science) or Keele & Park (2006, Unpublished manuscript).
Comment: Thank you very much for your reply. In the past day, I've come across these models and found the `glmx` package. I've started testing its behaviour under simulation. I was also able to replicate it using standard MLE in R - I originally kept the intercept in the equation for the scale, that didn't go well. Thanks again for the explanation and the links you provided.
Comment: Models with identity link in the scale equation are always a bit tricky. In the cases we considered the log link either worked better or was on par with identity or square root link. But the main problem is really to check what kind of mean function you get on the manifest scale from the heteroscedastic function on the latent scale. It's easy to specify something that collapses or is very close to collapsing to something simpler...
Comment: I found the manuscript by Keele & Park very informative! Thanks again.
Answer: I'm not sure if this is what you're asking, but from the practical side, you can check for heteroscedasticity of the residuals in logistic regression models with DHARMa, and you can correct it, e.g., with glmmTMB.
The fact that you will never be 100% sure if you have the right model is inherent to any applied statistical problem. It is a problem for sure, but I don't see that the issue of heteroscedasticity is more problematic than any other misspecification problem (though it is probably less frequently checked).
EDIT - due to the comments, here an example how to diagnose the model misspecification in DHARMa
set.seed(12345)

dat <- data.frame(x = rnorm(1000))
# switch here to use one or the other option
#dat$error <- rlogis(nrow(dat))
dat$error <- rlogis(nrow(dat), scale = abs(dat$x))
dat$y <- (rowSums(dat) > 0) + 0

fit <- glm(y ~ x, binomial, dat)

library(DHARMa)

# standard plots for 0/1  data don't show the problem

res <- simulateResiduals(fit)
plot(res)
testDispersion(res)

# as explained at the end of the DHARMa vignette, one has to
# group the data to see dispersion problems

qnt <- quantile(dat$x,seq(0,1,0.02))
dat$sizegroup = cut(dat$x,unique(qnt),include.lowest=TRUE)

res2 = recalculateResiduals(res , group = dat$sizegroup)
testDispersion(res2)
plot(res2)
plotResiduals(1:nlevels(dat$sizegroup), res2$scaledResiduals)

Comment: The issue with 0/1 data is that one always has to group them to see dispersion problems. I have added an example in my answer. I guess Achim's comments are more to the point regarding this specific example, but in general I think the workflow described above works well for logistic regression.
Comment: I'm a DHARMa user and had not considered this. I'll see if it can pick this up in the data and report back, thanks. And my interest is not related to any application, more theoretical. One thing I'm almost sure of is that glmmTMB will not let you model dispersion if your variable is Bernoulli because of the standard relationship between mean and variance for a Bernoulli variable.
Comment: Jim, you can adjust the dispersion in glmmTMB with the dispersion parameter, as long as you take a distribution with variable dispersion. For a binomial, the betabinomial should work ... for anything more complicated, one would probably have to move to a general framework, e.g. Jags or Stan.
Comment: I tried out DHARMa on data with a heteroskedastic data generating process for the logistic regression model; the resulting plots appeared adequate. I do not think it's a type of model misspecification that it can pick up.
Comment: Thanks! Tested it for false positive and false negative and it worked about it.
Answer: The MLE was derived based on the assumption $ϵ∼L(0,1)$. Then it was used on the totally different situation, $ϵ∼L(0,|x|)$. Of course, the original MLE does not work anymore. The basic principle for MLE is: MLE is based on the distribution of the random variables; if the assumptions are changed, new MLE based on new distribution should be derived separately, and the old MLE cannot be used.
So for your situation, get $P(\epsilon < X\beta \mid X)$ under the assumption that $ϵ∼L(0,|x|)$, then derive MLE. You will get unbiased estimate.
Comment: @HeteroskedasticJim thats why god invented robust standard errors, under the right assumptions this will give you the (nominally) right inference
Comment: In empirical data analysis, one has no idea of the data generation process. So it is no use deriving an MLE for this specific process. I'm more interested in general approaches. It is acceptable if there are none. I see how my last sentence could have been misleading in that regard.
Comment: @Repmat lol, but my question is about bias not inference.
Comment: @Repmat So-called robust standard errors only help you if you have the estimating function correctly specified and the rest of the likelihood misspecified. Here, this does not help: The mean function of the manifest variable is misspecified! Hence coefficient estimates are inconsistent and no standard errors can save you save you from that...
Comment: @AchimZeileis
There are certain situations why it is still usefull. Hence my "under the rightassumptions" - of course I should have qualified this (its not a general case in any sense).
Regardless I think it largely misses the point, there is not particular reason to care about the estimate of $\beta$. I have yet to see a single application where $E(y^* \mid x)$, i.e. the effect on the latent response, was of any interest. The interest is almost always on $P(Y=1 \mid x)$, and probit/logit might still do a pretty good job here even with heteroskedasticity
Comment: I do not know the purpose of  this kind of studies. When a statistical method is derived/established under the assumptions, then someone finds that under other situations which violate the original assumptions, the statistical method is not good. Yes, you are right, because before you tell me, I already know it because the assumptions are not met.


Joint probability vs marginal probability - understanding the integration limits

Question: Can i pick your brains about this?

My goal was to calculate $E[X]$, $E[Y]$ and E[XY] (with the end goal of computing covariance but that's not important for this query).
It was when I tried to calculate $f_Y(y)$ that i realized I didn't understand the integration limits when we calculate $E[XY]$. Please observe the two limits for the $x$ variation.
For $f_Y(y)$:

For the $E[XY]$:

I suspect because both variables are changing together we can do this but I don't have intuition for it or a rigorous explanation for why this difference is there. $0\to{1}$ vs $0\to{y}\cup{y-1\to{1}$.
Thank you in advance!
Comment: Can you clearly write out what the joint density is?
Comment: Edit: I realized I made a blunder calculating $E[Y]$ and possibly $f_Y(y)$. This exercise is typically solved using iterated expectations, jumping from $f_{Y|X}(x|y)$ which comes easily from the joint and the marginal of $x$, to that expectation and from there using the law of iterated expectations to the intended expectation, $E[XY]$. However I would like to calculate $f_Y(y)$ for practice. Please help
Answer: It looks like the support is the paralellogram $\langle 0,0\rangle\langle 1,1\rangle\langle 1,2\rangle\langle 0,1\rangle$
As such, the marginal density for $Y$ shall be a piecewise function, not a sum.  Look at the graph and see how the bounds of the horizontal cross-section depend upon the height.
$\qquad f_{\small Y}(y)=\begin{cases}\int\limits_0^{y} f_{\small X,Y}(x,y)\,\mathrm d x&:& 0\leq y\lt 1\\[1ex]\int\limits_{y-1}^1 f_{\small X,Y}(x,y)\,\mathrm d x&:& 1\leq y\leq 2\\[1ex]0&:&\textrm{otherwise}\end{cases}$
However, for the expectation you will be summing over all the pieces of the marginal's domain, so you shall have:
$\qquad\begin{align}\mathsf E(Y) &= \int_0^1 y~f_{\small Y}(y)\,\mathrm d y\\&=\int_0^1 y\int_0^y f_{\small X,Y}(x,y)\,\mathrm d x\,\mathrm d y+\int_1^2 y\int_{y-1}^1 f_{\small X,Y}(x,y)\,\mathrm d x\,\mathrm d y\end{align}$
You should now be able to find expressions for $\mathsf E(X)$ and $\mathsf E(XY)$.

BTW: The marginal density for $X$ is indeed :
$\qquad f_{\small X}(x)=\begin{cases}\int\limits_x^{x+1} f_{\small X,Y}(x,y)\,\mathrm d y&:& 0\leq x\leq 1\\[1ex]0&:&\textrm{otherwise}\end{cases}$
However, this should not be used to calculate $\mathsf E(\color{red}Y)$.   Please, use it for $\mathsf E(X)$ ...


How does expectation work for a complex indicator variable?

Question: There's a problem on expectation for a discrete random variable that involves people being selected as happy/unhappy. I am stumped, I thought I might reach out for help on the theory.
The problem is
"Consider a group of $n >= 4$ people, numbered from 1 to $n$. For each pair $(i, j)$ with $i =/= j$ person $i$ and person $j$ are friends, with probability $p$. Friendships are independent for different pairs. These $n$ people are seated around a round table. For convenience, assume that the chairs are numbered from to $1$ to $n$, clockwise, with $n$ located next to 1, and that person $i$ seated in chair $i$. In particular, person 1 and person $n$ are seated next to each other.
If a person is friends with both people sitting next to him/her, we say this person is happy. Let $H$ be the total number of happy people. Express your answers in terms of $p$ and/or $n$"
It says Lets $I_i$, be a random variable indicating whether the person seated in chair i is happy or not ($I_i$ = 1 if person $i$ is happy and 0 otherwise). Find $E[I_i]$ for $i$ = 1,2,...,n
I modeled it out in a circumstance where $n$ = 5. I'm stumped by the question of how to handle p. Is p different for each pair? I know the $E[I_a]$ for an indicator variable is $p$, but that doesn't seem to apply because $p$ seems specific to each pair.
It has a couple other questions, like $E[H]$ and $E[I^2 sub k]$ for any $k$ in set ${1,2,..n}$ but I'm so stuck on the first part that these seem very far away.
Comment: FYI, I rolled back to the previous version. Please don't vandalize posts, even your own posts.
Comment: By linearity of expectation, $E(H)= np^2$.
Comment: @MathAvengers Look at grand_chat's answer
Answer: You can assume that $p$ is the same for every pair $(i,j)$; if the probability depended on which people were involved, you'd see a subscript on $p$, as in "person $i$ and person $j$ are friends with probability $p_{i,j}$".
To calculate $E(I_i)$: This is the probability that person $i$ is happy, which by definition is the probability that person $i$ is friends with the person to the left, and person $i$ is friends with the person to the right. These two events are independent (since friendships are independent for different pairs), and each event has probability $p$, so the probability of their intersection ("and") is $p^2$, the product of the individual probabilities. Therefore $E(I_i)=p^2$ for every $i$.
Answer: c) For any k∈{1,2,…,n} find $E[I^2_k]$
$E[I^2_k]$ = $(p^2)^2 = p^4$
d) For any i∈{1,2,…,n} and under the convention $I_n+_1=I_1, find E[I_iI_i+_1]$
$E[I_iI_i+_1] = 2*p^2$
I am not 100% sure about the answers though. Can anyone please confirm here?


Making tableView Cells Fall into place one after the other (tableViewCellAnimation)

Question: I'm new to obj-C and I have this code which makes the cells slide in from the right of the page (when I call it after reloading the table data).
Instead of going through each one of them, waiting for the delay and then doing the next, they all slide in at the same time after the delay count.
Why are they sliding in all at once instead of staggering the animation?
-(void) animate {
  for (UITableViewCell * aCell in [self.tableView visibleCells]) {
    [aCell setFrame: CGRectMake(320, aCell.frame.origin.y, aCell.frame.size.width, aCell.frame.size.height)];
    [UIView animateWithDuration: 0.5 delay: 0.5 options: UIViewAnimationOptionBeginFromCurrentState animations: ^ {
      [aCell setFrame: CGRectMake(0, aCell.frame.origin.y, aCell.frame.size.width, aCell.frame.size.height)];
    }
    completion: nil
    ];

  }

Answer: I think you just don't understand how delay and blocks works. Let me try to clear this code:

You go throw all [self.tableView visibleCells] array
You call [UIView animateWithDuration:...] method number of times, how much elements in visibleCells array (delay doesn't freeze your code)
In every method above starts 0.5 timer in another tread.
After 0.5 seconds animation block calls number of times, how much elements visibleCells has

For you easiest solution will be to save visible cells in any array, then call animation method with parameter - index of cell you will animate in this method. In completion block call next animation. Here is code example:
- (void)animate:(NSInteger)index {
    if (visibaleCells.count <= index) {
        return;
    }

    [UIView animateWithDuration:0.5 delay:0.5 options:0 animations:^{
        [visibaleCells[index] setFrame:CGRectMake(0, aCell.frame.origin.y, aCell.frame.size.width, aCell.frame.size.height)];
    } completion:^(BOOL finished) {
        [self animate:index + 1];
    }];
}


Floor plan in flutter

Question: i'm trying to come up with best way to draw a floor plan in flutter, something like these images, but it would be for regals in one concrete shop, instead of plan of shopping centre with multiple shops.
floor plan 1
floor plan 2
i decided rectangles would be sufficient enough for now and i have multiple ideas on how to execute, but no idea which one is the best. or maybe there is even better one i have not thought of
1. using custom painter
regals have attributes: ax, ay, bx, by, so they go from point a (left bottom) to b (right upper)
code like this
final rect = Rect.fromPoints(
    Offset(regal.ax.toDouble(), size.height - regal.ay.toDouble()),
    Offset(regal.bx.toDouble(), size.height - regal.by.toDouble()),
);

this is good because it is flexible, there is pretty much unlimited range of options, but using CustomPainter is a bit buggy in my case, alongside with Transform and GestureDetector it bugs out sometimes and instead of clicking on "buttons" you need to track where user clicked, ehm, tapped.
2. using gridView?
i dont have thought this thru as much as first option, but big plus would be using styled buttons as regals, instead of rectangles.
possible problems would be button sizing, if one regal would be times bigger than others.
regal attributes would be order on x axis, order on y axis, x flex (for example 3 as 3 times of base size), y flex
i think i have not thought of the best solution yet.
what would it be?
Answer: Here is a quick playground using a Stack of Regals who are just Containers in this quick implementation under 250 lines of code.

Click the FloatActionButton to create random Regal. Then, you can define the position of each Regal and its Size, within the limit of the Floor Plan and Max/min Regal Size.
In this quick implementation, the position of a Regal can be defined both with Gestures or Sliders; while its size can only be defined using the sliders.
Package Dependencies

Riverpod (Flutter Hooks flavor) for State Management
Freezed for Domain classes immutability

Full Source Code (222 lines)
import 'dart:math' show Random;

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

part '66478145.floor_plan.freezed.dart';

void main() {
  runApp(
    ProviderScope(
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Flutter Demo',
        home: HomePage(),
      ),
    ),
  );
}

class HomePage extends HookWidget {
  @override
  Widget build(BuildContext context) {
    final regals = useProvider(regalsProvider.state);
    return Scaffold(
      body: Center(
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: [
            Stack(
              children: [
                Container(
                    width: kFloorSize.width,
                    height: kFloorSize.height,
                    color: Colors.amber.shade100),
                ...regals
                    .map(
                      (regal) => Positioned(
                        top: regal.offset.dy,
                        left: regal.offset.dx,
                        child: GestureDetector(
                          child: RegalWidget(regal: regal),
                        ),
                      ),
                    )
                    .toList(),
              ],
            ),
            const SizedBox(width: 16.0),
            RegalProperties(),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => context.read(regalsProvider).createRegal(),
        child: Icon(Icons.add),
      ),
    );
  }
}

class RegalWidget extends HookWidget {
  final Regal regal;

  const RegalWidget({Key key, this.regal}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final _previousOffset = useState<Offset>(null);
    final _refOffset = useState<Offset>(null);
    return GestureDetector(
      onTap: () => context.read(selectedRegalIdProvider).state = regal.id,
      onPanStart: (details) {
        _previousOffset.value = regal.offset;
        _refOffset.value = details.localPosition;
      },
      onPanUpdate: (details) => context.read(regalsProvider).updateRegal(
            regal.copyWith(
                offset: _previousOffset.value +
                    details.localPosition -
                    _refOffset.value),
          ),
      child: Container(
        width: regal.size.width,
        height: regal.size.height,
        color: regal.color,
      ),
    );
  }
}

class RegalProperties extends HookWidget {
  @override
  Widget build(BuildContext context) {
    final regal = useProvider(selectedRegalProvider);
    return Padding(
      padding: EdgeInsets.all(16.0),
      child: regal == null
          ? Text('Click a Regal to start')
          : Form(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text('WIDTH'),
                  Slider(
                    min: kRegalMinSize.width,
                    max: kRegalMaxSize.width,
                    value: regal.size.width,
                    onChanged: (value) => context
                        .read(regalsProvider)
                        .updateRegal(
                          regal.copyWith(size: Size(value, regal.size.height)),
                        ),
                  ),
                  const SizedBox(height: 16.0),
                  Text('HEIGHT'),
                  Slider(
                    min: kRegalMinSize.height,
                    max: kRegalMaxSize.height,
                    value: regal.size.height,
                    onChanged: (value) => context
                        .read(regalsProvider)
                        .updateRegal(
                          regal.copyWith(size: Size(regal.size.width, value)),
                        ),
                  ),
                  const SizedBox(height: 16.0),
                  Text('LEFT'),
                  Slider(
                    min: 0,
                    max: kFloorSize.width - regal.size.width,
                    value: regal.offset.dx,
                    onChanged: (value) =>
                        context.read(regalsProvider).updateRegal(
                              regal.copyWith(
                                  offset: Offset(value, regal.offset.dy)),
                            ),
                  ),
                  const SizedBox(height: 16.0),
                  Text('TOP'),
                  Slider(
                    min: 0,
                    max: kFloorSize.height - regal.size.height,
                    value: regal.offset.dy,
                    onChanged: (value) =>
                        context.read(regalsProvider).updateRegal(
                              regal.copyWith(
                                  offset: Offset(regal.offset.dx, value)),
                            ),
                  ),
                ],
              ),
            ),
    );
  }
}

final selectedRegalIdProvider = StateProvider<String>((ref) => null);

final selectedRegalProvider = Provider<Regal>((ref) {
  final selectedId = ref.watch(selectedRegalIdProvider).state;
  final regals = ref.watch(regalsProvider.state);
  return regals.firstWhereOrNull((regal) => regal.id == selectedId);
});

final regalsProvider =
    StateNotifierProvider<RegalsNotifier>((ref) => RegalsNotifier());

class RegalsNotifier extends StateNotifier<List<Regal>> {
  final Size floorSize;
  final Size maxSize;

  RegalsNotifier({
    this.floorSize = const Size(600, 400),
    this.maxSize = const Size(100, 100),
    List<Regal> state,
  }) : super(state ?? []);

  void createRegal() {
    state = [...state, Regal.random];
    print(state.last);
  }

  void updateRegal(Regal updated) {
    state = state.map((r) => r.id == updated.id ? updated : r).toList();
  }
}

@freezed
abstract class Regal implements _$Regal {
  const factory Regal({
    String id,
    Color color,
    Offset offset,
    Size size,
  }) = _Regal;

  static Regal get random {
    final rnd = Random();
    return Regal(
      id: DateTime.now().millisecondsSinceEpoch.toString(),
      color: Color(0xff555555 + rnd.nextInt(0x777777)),
      offset: Offset(
        rnd.nextDouble() * (kFloorSize.width - kRegalMaxSize.width),
        rnd.nextDouble() * (kFloorSize.height - kRegalMaxSize.height),
      ),
      size: Size(
        kRegalMinSize.width +
            rnd.nextDouble() * (kRegalMaxSize.width - kRegalMinSize.width),
        kRegalMinSize.height +
            rnd.nextDouble() * (kRegalMaxSize.height - kRegalMinSize.height),
      ),
    );
  }
}

// CONFIG
const kFloorSize = Size(600, 400);
const kRegalMinSize = Size(10, 10);
const kRegalMaxSize = Size(200, 200);

Floor plan in flutter


Multilevel model analysis of dyad data

Question: I'm trying to understand why when conducting a multilevel model analysis the Hessian matrix is not positive definite (using SPSS). The data I'm analyzing are responses from individuals who are grouped into dyads. Dyad is a level 2 grouping variable where 2 individuals comprise a dyad. I have a score on x for each individual.
Looking at the distribution of x it looks ok (x was positively skewed but looks pretty normal after a log transformation). Scores on x are between 0 and 1.1 with a sd of 0.26.
When I examine the null model where I only include a random intercept for dyad predicting x the Hessian matrix is not positive definite. If I take the x values and randomize them across dyads there are no problems with the Hessian matrix. This suggests to me some dependence of x between dyad members but I don't know why this should be a problem.
Any thoughts?
Comment: http://www.theanalysisfactor.com/wacky-hessian-matrix/
Comment: Thanks Alex R. So the conclusion here should be that the model can't uniquely estimate any variance between dyads so the appropriate thing to is to exclude dyad from the analysis?
Answer: This will happen when there is no variation between dyads.

Multilevel model analysis of dyad data


Can't configure quota on CentOS 5.5. Strange behaviour of quotacheck and other quota commands

Question: I have Centos 5.5 linux with quota enabled in fstab

[root@drone3 ~]# mount  | grep usrquota
/dev/sda4 on /storage type ext4 (rw,noatime,nodiratime,usrquota)

When I tray to activate quota on the /storage I hit the following messages :

[root@drone3 ~]# quotacheck -cu /storage
quotacheck: Mountpoint (or device) /storage not found.
quotacheck: Can't find filesystem to check or filesystem not mounted with quota option.
[root@drone3 ~]# uname -a
Linux drone3.xxxxxxxxxxx.com 2.6.18-194.26.1.el5 #1 SMP Tue Nov 9 12:54:20 EST 2010 x86_64 x86_64 x86_64 GNU/Linux

As a result no aquota.user file is created on /storage and quota is not enabled/started.
What do you think - where is the problem? Is it because of ext4 file system and/or this is a bug ? I did not found anything related to these messages and ext4 in google.
All Quota related commands give similar messages - mount point is not found.
Answer: https://bugzilla.redhat.com/show_bug.cgi?id=500231


Update numa tabela A de números distintos, usando tabela B de números repetidos

Question: Tenho uma tabela (A) de números distintos e preciso atualizar vários campos dessa tabela em função do numero distinto, só que a outra tabela (B) que vai servir para atualizar tem os números distintos repetidos, quando faço o update ele só pega um registro da tabela B
TABELA A
NUMERO          H00 H01 H02 H03 H04 H05
552135792058    0   0   0   0   0   0

TABELA B        H00 H01 H02 H03 H04 H05
552135792058    0   1   0   0   0   0
552135792058    0   0   1   0   0   0
552135792058    0   0   0   1   0   0

Quando faço o update ele só pega um dos registros da tabela B, como faço para ele pegar todos.
Segue a minha query
update NUMD set NUMD.H00 = N.H00 + I.H00, NUMD.H01 = N.H01 + I.H01, NUMD.H02 = N.H02 + I.H02,
       NUMD.H03 = N.H03 + I.H03, NUMD.H04 = N.H04 + I.H04, NUMD.H05 = N.H05 + I.H05
        FROM NUMD N INNER JOIN #TBLtmp I
        ON N.NUMERO COLLATE Latin1_General_CS_AS = I.NUMERO COLLATE Latin1_General_CS_AS

Answer: Creio que o comando abaixo atenda a suas necessidades:
UPDATE NUMD set H00 = H00 + I.H00, H01 = H01 + I.H01, H02 = H02 + I.H02, H03 = H03 + I.H03, H04 = H04 + I.H04, H05 = H05 + I.H05
FROM  #TBLtmp I
WHERE NUMD.NUMERO COLLATE Latin1_General_CS_AS = I.NUMERO COLLATE Latin1_General_CS_AS;

Comment: Rogerio, esse update não funciona, retorna a mesma coisa do que fiz. Um cursor demora dias para processar, sao milhoes de registros. Preciso dum update que resolva. Obg.
Comment: Por acaso seus campos NUMERO estão indexados?


Find the probability that $2$ black balls extracted are from urn $B$.

Question:
The urn $A$ contains $3$ black balls and $5$ red balls while urn $B$ contains
  $4$ black balls and $7$ red balls , If the $2$ balls are extracted simultaneosuly
   from any of the two urns are found to be black, find the probability that they are extracted from urn $B$.

$\dfrac{\dbinom{3}{2}}{\dbinom{8}{2}}+\dfrac{\dbinom{4}{2}}{\dbinom{11}{2}}=\dfrac{333}{1540}$
I am not sure if my logic is correct, also please tell that which concept of probability this
question uses whether is is binomial distribution or condintional probability etc.
I look for a short and simple way .
I have studied maths up to $12$th grade.
Comment: Both balls are extracted only from a box?
Comment: $\ \ \ \ $ Yess.
Comment: Can you use total probability formula and conditional probability?
Comment: U mean am I allowed to use "total probability formula and conditional probability"?
Comment: Yes, i mean it.
Comment: Yes I can use any formula , method , no restriction on the process, this questions are generally objective type with options  ,I need to check an option and proceed.
Comment: Does "two balls are extracted simultaneously from any of the two urns" mean they may be extracted from different urns, or must be extracted from the same urn?  Is the urn selected first, or are the balls selected without bias?
Comment: @graham :They must be extracted from the same urn.
Comment: I think you are misunderstanding the question, and that K. Jiang's answer is correct. To rephrase the question: _In the event that $2$ balls extracted from either urn, at random and without replacement, are both black, what is the probability that they were both extracted from urn $B$?_
Answer: Be careful when you add probabilities. Usually, adding is fine if the probabilities are distinct (they do not affect each other), but here, they most certainly do. The method I will walk through in this solution requires knowledge of some basic conditional probability.
We begin by finding the total number of ways to end up with $2$ black balls. This is easy to count: we can select $2$ of the $3$ from $A,$ which can be done in $\dbinom{3}{2} = 3$ ways; $2$ of the $4$ from $B,$ which can be done in $\dbinom{4}{2} = 6$ ways; or $1$ ball from each urn, which can be done in $3 \cdot 4 = 12$ ways. Our "sample space" is $3 + 6 + 12 = 21$ ways (to get $2$ black balls).
Now we find the number of ways to get $6$ black balls from just $B.$ We already found this (above) to be $6$ ways.
Our probability, then, is $P = \frac{6}{21} = \boxed{\frac{2}{7}}.$
Answer: $\begin{eqnarray}
P(B|bb)=P(bb|B)\frac{P(B)}{P(bb)}...(*)
\end{eqnarray}$
Now, $P(bb)=P(bb|B)P(B)+P(bb|A)P(A)=\frac{1}{2}(P(bb|B)+P(bb|A))=\frac{1}{2}\left[\dfrac{\binom{4}{2}}{\binom{11}{2}}+\dfrac{\binom{3}{2}}{\binom{8}{2}}\right]$.
Sustituying in $(*)$
$\begin{eqnarray}
P(B|bb)&=&P(bb|B)\frac{P(B)}{P(bb)}\\
&=&\dfrac{\frac{\binom{4}{2}}{\binom{11}{2}}}{\dfrac{\binom{4}{2}}{\binom{11}{2}}+\dfrac{\binom{3}{2}}{\binom{8}{2}}}\\
&=&\frac{56}{111}\\
&\sim&0.504
\end{eqnarray}$
Answer: I will use some notation with which you might be unfamiliar. Feel free to ask for clarification.
Let $T = \{\text{Drew 2 black balls}\}$ and $U = \{\text{Chose urn $i$}\}$ for $i = A,B$. Then, we are interested in
$$P(U_B|T) = \frac{P(TU_B)}{P(T)}.\tag{$\star$}$$
This is true using Bayes' rule.
Notice that it would make calculations easier if I conditioned the numerator on $U$
$$P(TU_B) = P(T|U_B)P(U_B)$$
Since the question doesn't specify, I think we can assume that each urn is chosen with equal probability, so $P(U_B) = \frac{1}{2}$.
Now, if we chose urn $U$, what is the probability that we draw two black balls?
You seem to understand that this is
$$P(T|U_B) = \frac{\binom{4}{2}}{\binom{11}{2}} = \frac{6}{55}.$$
To get the denominator in $(\star)$, we use the law of total probability and then condition again on $U_i$,
\begin{align*}
P(T) &= P(TU_A)+P(TU_B) \\
&= P(T|U_A)P(U_A)+P(T|U_B)P(U_B) \\
&= \frac{\binom{3}{2}}{\binom{8}{2}}\cdot\frac{1}{2}+\frac{\binom{4}{2}}{\binom{11}{2}}\cdot\frac{1}{2} \\
&= \frac{3}{28}\cdot\frac{1}{2}+\frac{6}{55}\cdot\frac{1}{2}.
\end{align*}
Putting it all together we see that
$$P(U_B|T) = \frac{P(TU_B)}{P(T)} = \frac{\frac{6}{55}\cdot\frac{1}{2}}{\frac{3}{28}\cdot\frac{1}{2}+\frac{6}{55}\cdot\frac{1}{2}} = \frac{56}{111}=0.5045045
$$
Answer: Since the balls must be extracted from the same urn, we simply wish the conditional probability that they are event $B$: "from urn-$B$", given that they are event $b$: "both black".
We use Bayes' Rule and the Law of Total Probability.
$$\begin{align}\mathsf P(B\mid b) & = \dfrac{\mathsf P(b\mid B)\;\mathsf P(B)}{\mathsf P(b)} & \textsf{Bayes' Rule}
\\ & = \dfrac{\mathsf P(b\mid B)\,\mathsf P(B)}{\mathsf P(b\mid B)\;\mathsf P(B)+\mathsf P(b\mid A)\;\mathsf P(A)} & \text{Law of Total Probability}
\\ & = \frac{\mathsf P(b\mid B)}{\mathsf P(b\mid B)+\mathsf P(b\mid A)} & {\textsf{assuming no bias in urn selection}\\\mathsf P(A)=\mathsf P(B) } \\ & = \dfrac{\binom 3 2 / \binom 8 2}{\binom 3 2 / \binom 8 2 + \binom 4 2 / \binom {11} 2} & \textsf{as you were attempting to reason} \\ & = \frac {55}{111}  \end{align}$$


Why are linear and spline term estimates so different?

Question: I am working on modelling in an epidemiological setting. I want to estimate the association between an exposure (air pollution) and two event outcomes. To achieve that, I am using a Cox PH model with time-varying exposure (I work with the survival package in R).
I defined two main models:

Linear: coxph(Surv(time1, time2, event) ~ covariates + exposure)
Spline:  coxph(Surv(time1, time2, event) ~ covariates + pspline(exposure, df=4))

The estimated associations for the linear model are displayed by 5-unit increase in the exposure:

Outcome 1: HR= 1.21 (95% confidence intervals = 1.03-1.43)
Outcome 2: HR= 1.22 (95% confidence intervals = 1.00-1.50)

The estimated association for the spline model are displayed below, using as reference the lowest value of the exposure (x in the graph).

I don't understand why despite having very similar HRs in the linear model, their curves are so different in the spline model. Especially, for outcome 1, I don't understand how the linear HR can be 1.22 when in the curve is mostly below 1.00 until the exposure is approximately 12 (x $\approx$ 12)
Code for HR spline plots (simplified and ispired by Terry Therneau tutorial):
#DATA EXTRACTION
mod <- coxph(Surv(time1, time2, event) ~ covariates + pspline(exposure, df=4), data=df)
pmterm <- termplot(mod,terms = which(mod$pterms == 1),
                 se=TRUE, plot=FALSE)
dtplot<- list(pmterm = pmterm[["exposure"]], exposure = df$exposure)

#DATA FORMATTING
 centre <- with(dtplot$spline, y[x==min(x)])
 ytemp <- dtplot$spline$y + outer(dtplot$spline$se, c(0, -1.96, 1.96), '*')
 ytemp <- exp(ytemp - centre) #THIS LINE IS COMMENTED FOR LOG-HAZARD PLOT
 datspline <- data.frame(cbind(dtplot$spline$x,ytemp))
 datexp<- data.frame(dtplot$exp)
 names(dat)<-c("exp","HR","low","up")

#PLOTTING
 ggplot(datspline, aes(x = exp)) +
 geom_line(aes(y=HR)) + geom_ribbon(aes(ymin=low, ymax=up),alpha = .1) +
 geom_histogram(aes(x=exp, y=10*(after_stat(density))), binwidth=1,
        linewidth=0,colour="firebrick3",fill="firebrick3", alpha = 0.25,
        data=datexp) + #GEOM_HISTOGRAM IS EXCLUDED FOR LOG-HAZARD PLOT
labs(title=paste0("outcome ",i), x = "exposure", y = "HR (95% CI)") + #y="log-hazard" FOR LOG-HAZARD PLOT
theme_classic() +
geom_hline(yintercept = 1, lty = 2) +  coord_cartesian(ylim = c(0,2.5)) #ylim=c(-0.5,0.5) FOR LOG-HAZARD PLOT

Comment: It would help if you could please edit the question to show the tabular reports for the two models. Something like `print(linearModel)` and `print(splineModel)`; see Section 5.5 of [Therneau and Grambsch](https://www.springer.com/us/book/9780387987842). Showing the code that produced the plots from the models would help, too. For comparison against a model linear in exposure, changing the y-axis of your plots to the log-hazard instead of the hazard ratio would be preferable.
Comment: @jmarkov if you did the above plots for the linear model, on the log-hazard scale they would be straight lines. On the HR scale they would be exponentials. Maybe not a big difference with your fairly restricted range of HR values, but over time I've learned that it's usually much easier to think about things and do calculations in the log-hazard scales of the model coefficients and only change to HRs at the very end if ever.
Comment: What's the distribution of exposure time? And how many events happen at high exposures?
Comment: What do you mean the distribution of exposure time? Regarding your second question, most of the events (approx. 70-80%) occur below 12 unit of exposure.
Comment: Distribution of exposure time would be how many people had how much exposure.
Comment: Thank you for your comments. I'll update the question with some code. @EdM why do you think it's better to plot the log-hazards instead of the HR?
Comment: I added code for plots and improved the plots with log-hazards and exposure distribution. Sorry, @EdM I haven't posted the `print()` yet as I would have to re-run the model which takes some time. Let me know if this gives enough info
Comment: @PeterFlom regarding the number of events by exposure units I have: for outcome 1 4600 cases for exposure <=10,  7450 for exposure <= 12 and 2000 above 12. For outcome 2 I have 3250 for exposure <=10, 5100 for exposure <=12 and 1200 above 12.
Answer: Following @EdM answer, I show that splines and linear estimated associations apparently looking very different, in fact, when displayed using the right transformation are similar.
Below you find the graph of the spline log-hazards (black line) and 95% confidence intervals (gray shades) for outcome 1 and 2. It is showed overimposed the blue line which consists of the beta estimated using the linear term.  I t is showed in the background the histogram of the exposure distribution (in red).

Code (in R) to compute model estimates and plot graphs for a single outcome (linear associations are computed for a 1 unit increment in exposure):
  #MODEL ESTIMATION

  #Spline model
  modspline <- coxph(Surv(time1, time2, event) ~ covariates + pspline(exposure, df=4),data=df)
  pmterm <- termplot(mod,terms = which(mod$pterms == 1),se=TRUE, plot=FALSE)
  splinest<- list(pmterm = pmterm[["exposure"]], exposure = df$exposure)

  #Linear model
  modlin <- coxph(Surv(time1, time2, event) ~ covariates + exposure,data=df)
  ind <- grep("cbpm25", names(coef(mod)))
  coef <- coef(mod)[ind]
  vcov <- vcov(mod)[ind,ind,drop=F]
  hrfit  <- exp(coef)
  hrlow  <- exp(coef - 1.96*sqrt(vcov))
  hrup <- exp(coef + 1.96*sqrt(vcov))
  cat(round(c(hrfit,hrlow,hrhigh))#display linear HR with 95%CI

  #FUNCTION TO COMPUTE INTERCEPT
  intercept<-function(x,y,slope){mean(y) - slope * mean(x)}

  #EXTRACT DATA FOR PLOT OF LOG-HAZARD

  #for spline (black curve and shaded grey area)
  centre <- with(splinest$spline, y[x==min(x)])
  ytemp <- splinest$spline$y + outer(splinest$spline$se, c(0, -1.96, 1.96), '*')
  dat <- data.frame(cbind(splinest$spline$x,ytemp))
  datexp<- data.frame(splinest$exposure)
  mexp<-mean(datexp$pm.exp)
  names(dat)<-c("exposure","loghazard","low","up")

  #for linear effect (blue line)
  inter<-intercept(x=mexp, y=0, slope=coef)

  #PLOT GRAPH
  ggplot(dat, aes(x = exposure)) +
    geom_line(aes(y=loghazard)) + geom_ribbon(aes(ymin=low, ymax=up), alpha=0.2) +
    geom_histogram(aes(x=pm.exp, y=2*(after_stat(density))), binwidth=1, size=0,
                   colour="firebrick3", fill="firebrick3", alpha = 0.25, data=datexp) +
    geom_abline(intercept = inter, slope = coef, col="blue") +
    labs(title=paste0("outcome ",i), x = "exposure", y = "log-hazard") + theme_classic() +  coord_cartesian(ylim = c(-0.5,0.7),

Comment: Looks good. Thanks. (+1)
Answer: The HR and log-hazard plots are dominated visually by the estimates at the extremes of exposure, where we now see that there are relatively fewer observations. Note how wide the confidence intervals are at those extremes.
In the linear model, you report HRs of 1.21 and 1.22 for a 5-unit increase in exposure for the two outcomes. In the log-hazard scale, those correspond to slopes of log(1.21)/5 and log(1.22)/5 per 1-unit increase of exposure, approximately 0.038 and 0.040, respectively. Try superimposing lines with those slopes on the spline fits in the log-hazard scales, passing through the corresponding origins on the exposure axis for 0 log hazard. Show the results on this page. I think you'll see that the spline fits aren't that far off from those linear fits overall, except at the extremes.
Applying the print() function to the models using pspline() will provide an output that evaluates whether the nonlinear terms add anything statistically significant beyond the linear terms. Whether any nonlinearity is substantively important depends on your understanding of the subject matter.
Comment: I posted it, do you think it's clear enough?
Comment: Got it, thanks!
Comment: @jmarkov for others who might visit this page, consider posting the spline-fit log-hazard plots with the superimposed linear approximations (maybe also with the code) as another answer here. It's OK to post an answer to your own question on this site.


nginx rewrite of url with query string

Question: I have been googling for a good while and can't figure this one out.  It seems simple, and I am sure it is, but I am not a server expert.
My url is: http://www.example.com/blog/?tag=Word1+Word2
Some of the  tags are a single word (ex: bicycle), some are longer (ex: Two wheel bicycle).
I need this url to be output as: example.com/blog/tag/word1-word2/
How can I get a rewrite to achieve this?
Comment: Possible duplicate of [Nginx Rewrite Convert Querystring to Path](http://serverfault.com/questions/488444/nginx-rewrite-convert-querystring-to-path)
Comment: I had looked at that one.  The question I come back to is that /blog/ is the root of my blog.  Those responses in that other post all have /location/file.asmx.  I need the blog to work, and post urls like /blog/post-title/.
Comment: `Some of the tags are a single word, some are longer.` — what does it mean?
Answer: You should send - in your query instead of + to get same format and make an easier request :
location ~ /blog/ {
    if ($args ~* "tag=(.*)") {
        set $w1 $1;
        rewrite .* /blog/tag/$w1/? permanent;
    }
}

Based on the link i posted
? at the end will remove the query string parameters (from rewrite doc  )

If you specify a ? at the end of a rewrite then Nginx will drop the original $args (arguments)

another to achieve it is to set args to nothing:
set $args '';

Comment: I tried variations on that and got 404's. what happens if the tag is only a single word like "bicycle"?
Comment: in this case it will not works, so try first with 2 words, if it work i will modify to works for your case. Are there max 2 words, or can it be (n) words ?
Comment: it can be (n) words.  It did not work when I tested on a two word tag.   The rewrite you have there resulted in this url: http://www.urlt.com/blog/tag/-/?tag=Reliance+Nitro
Comment: ok i did some change can you confirm it works now with 2 parameters ?
Comment: that resulted in: http://www.url.com/blog/tag/-/
Comment: Answer edited to work

nginx rewrite of url with query string


Bs4 retornando apenas versão

Question: Opa, estou criando uma ferramenta que procura a versão do wordpress e imprime na tela (requests + bs4).
Trecho do código:
def versao():
    r = requests.get('https://'+f'{site}')
    html = r.text

    sopinha = BeautifulSoup(html, 'html.parser')

    versao = sopinha.find_all('meta', attrs={'name':'generator'})
    print(versao)

versao()

Eu queria que ele retorna-se apenas o número da versão do wordpress, como por exemplo: 4.9.2
Porém ele retorna tudo isso: (Deixei em vermelho o que quero que retorne)
Comment: Você buscou por todos os elementos `` da página. Não seria o caso de buscar apenas o que possui `name="generator"` e tratar seu valor?
Comment: Tentei tirar o meta porém dá o mesmo resultado
Answer: Tente utilizar sopinha.find("meta", {"name":"generator"}), isso deveria te retornar a linha completa caso queira reduzir ainda mais o retorno, tente: sopinha.find("meta", {"name":"generator"})['content'].

Bs4 retornando apenas versão


Search the record based on struct values in golang

Question: I am writing a service for search functionality. when i am passing values in the body to get the specific record i am able to get it based on the struct value only for PHONE.
I am very new to golang.
i need to search the values using all the fields  in the struct for  ex.phone or firstname or lastname of the patientstruct
my struct is as below
type PatientEntity struct {
    ID        int64
    FirstName string
    LastName  string
    Phone     string
}

my code is
func SearchPatientsHandler(res http.ResponseWriter, req *http.Request) {
    patient := &PatientEntity{}
    if err := json.NewDecoder(req.Body).Decode(patient); err != nil {
        panic(err)
    }

    patients := []*PatientEntity{}
    q := datastore.NewQuery(KINDPATIENT).Filter("FirstName =",patient.FirstName)

    keys, err := q.GetAll(ctx, &patients)

    if err != nil {
        // Handle error
        return
    }

    json.NewEncoder(res).Encode(patients)
}

i need to search with all the values of struct .how can i resolve.
Comment: This article contains everything you need for this task with example code: [Index Selection and Advanced Search](https://cloud.google.com/appengine/articles/indexselection)
Comment: Please show the values of `patient` before `NewQuery`, `patients` after `GetAll`, the value of `KINDPATIENT`. You may also want to query the datastore to make sure your data is as expected.
Comment: I want to filter the values either with PHONE or FIRSTNAME or LASTNAME. Is it possible with DATASTORE by using FILTER.
Answer: If I understand correctly, you're trying to combine filters with OR. According to the docs, this is not possible:

Cloud Datastore currently only natively supports combining filters
  with the AND operator.

There's a tracking issue for this, but it's been there for ages with no action.
You can instead issue a separate query for each filter, then merge the results (query for firstname, then query for lastname, then query for phone. Finally: combine all returns results while removing duplicates).
Something along the lines of (pseudocode, but that's the logic for an OR search):
var results []Patients
if firstname != "" {
  results = append(results, query("firstname = ", firstname)
}
if lastname != "" {
  results = append(results, query("lastname = ", lastname)
}
etc...
removeDuplicates(results)

Comment: `if (patient.FirstName != "" ){
  q = datastore.NewQuery(KINDPATIENT).Filter("FirstName=",patient.FirstName)

 }else if(patient.LastName != ""){

  q = datastore.NewQuery(KINDPATIENT).Filter("LastName =", patient.LastName)

 }else if(patient.Phone != ""){
  q = datastore.NewQuery(KINDPATIENT).Filter("Phone =", patient.Phone)

 }`
In these way i am doing .is it makes sense
Comment: You're not doing an OR search. If I specify both `firstname` and `lastname`, you will not search for `lastname` at all. I've updated my answer with pseucode for what you want to do.
Comment: The above code is not working for me i am getting an error showing that CANNOT USE QUERY , removeDuplicates(undefined)
i am using datastore.NewQuery
Comment: I did say it was pseudocode, not actual code. you'll have to write `removeDuplicates` yourself, and `query(...)` stands for building/sending a query and parsing the result.
Comment: i stuck with that .can you help me


Moving a Drupal between linux servers, best practice to avoid file-ownership problems

Question: I want to port over a Drupal commons 6.24 site from a local LAMP stack to a production server. Both systems runs on OpenSuse Linux.
How can I achieve this? What are the most important steps? How should I handle file-ownership?
It's important for me to have to have full control on the file ownership. If I use the wwwrun account, I frequently run into problems, due to a very strict server admin.
See for example the long history of looking for fixes and solutions
in this thread or in this very long and impressive thread here.
All troubles I run into have to do with file ownership and permissions.
This is my current setup.
Note: This was just a quick hacked installation, quick and dirty. Well my interest is after the general options I have in migrating from Linux to Linux.
<code>linux-vi17:/srv/www/htdocs/com624 # ls -l
insgesamt 224
-rwxrwxrwx  1 root www 45285 19. Jan 00:54 CHANGELOG.txt
-rwxrwxrwx  1 root www   925 19. Jan 00:54 COPYRIGHT.txt
-rwxrwxrwx  1 root www   206 19. Jan 00:54 cron.php
drwxrwxrwx  2 root www  4096 19. Jan 00:54 includes
-rwxrwxrwx  1 root www   923 19. Jan 00:54 index.php
-rwxrwxrwx  1 root www  1244 19. Jan 00:54 INSTALL.mysql.txt
-rwxrwxrwx  1 root www  1011 19. Jan 00:54 INSTALL.pgsql.txt
-rwxrwxrwx  1 root www 47073 19. Jan 00:54 install.php
-rwxrwxrwx  1 root www 15572 19. Jan 00:54 INSTALL.txt
-rwxrwxrwx  1 root www 14940 19. Jan 00:54 LICENSE.txt
-rwxrwxrwx  1 root www  1858 19. Jan 00:54 MAINTAINERS.txt
drwxrwxrwx  3 root www  4096 19. Jan 00:54 misc
drwxrwxrwx 35 root www  4096 19. Jan 00:54 modules
drwxrwxrwx  4 root www  4096 19. Jan 00:54 profiles
-rwxrwxrwx  1 root www  1470 19. Jan 00:54 robots.txt
drwxrwxrwx  2 root www  4096 19. Jan 00:54 scripts
drwxrwxrwx  4 root www  4096 19. Jan 00:54 sites
drwxrwxrwx  7 root www  4096 19. Jan 00:54 themes
-rwxrwxrwx  1 root www 26250 19. Jan 00:54 update.php
-rwxrwxrwx  1 root www  4864 19. Jan 00:54 UPGRADE.txt
-rwxrwxrwx  1 root www   294 19. Jan 00:54 xmlrpc.php
linux-vi17:/srv/www/htdocs/com624 #
</code>
Comment: Your files permissions seems incorrect, have a look at [Securing file permissions and ownership](http://drupal.org/node/244924)
Answer: We are successfully using rsync for this. It gives you fine-grained control over permission settings, and how they are transferred from one machine to the other one.
A very nice side effect of rsync is that, once properly set up, it gives you a very nice deployment tool. There's even Drush integration available.
Comment: by the way - how easy can i install drush!?


Meteor/Mongo: Get object of nested array to a template spacebar

Question: I want to set the content of an textarea by getting the data of a nested array. In the helper I define the document id and the element id. Now I want to get the content of textfor displaying in the template.
But my problem is to get the exact field out of the findOne-result, so the spacebar helper {{infotext}} is obviously wrong:
Document JSON
{
    "_id" : "8rmzAN3yw79TAKNeD",
    "title" : "title",
    "tooltips" : [
        {
            "element" : "16091688",
            "text" : "Lorem"
        },
        {
            "element" : "944a0d46",
            "text" : "ipsum"
        }
    ]
}

Helper
Template.infotext.helpers({
    infotext: function() {
        var id = '8rmzAN3yw79TAKNeD',
            elementId = Session.get('elementID');
        return Articles.findOne({_id: id, 'tooltips.element': elementId});
    }
});

Template
<template name="infotext">
    <form>
        <textarea>{{infotext}}</textarea>
        <input type="submit" class="btn btn-default" value="save">
    </form>
</template>

Answer: Your helper is returning the entire document when you are only after information in the document.  You definitely need to modify the helper function. (note this code is untested and may not actually work but will give you a bit of info on what to do)
Template.infotext.helpers({
    infotext: function() {
    var id = '8rmzAN3yw79TAKNeD',
    elementId = Session.get('elementID');
    var doc = Articles.findOne({_id: id, 'tooltips.element': elementId});
    var text = '';
    if (doc) {
        var filtered = doc.tooltips.filter(function(tooltip) {
            if (tooltip.element == Session.get('elementID')) {
                return tooltip;
            }
            return false;
         });
         if (filtered[0]) {
             text = filtered[0].text; //returns the first match - assuming there is no duplicates
         }
    }
    return text;
}
});

Answer: have the same issue,thanks, This helper works for me :
 userAmount(){
    let item =Items.findOne({_id:this._id,'cart.user':Meteor.userId()});
    let amount ='';
      if(item){
         item.cart.filter(function (cart) {
              if(cart.user ==Meteor.userId()){
              amount =Number(cart.amount);}else{amount=0;}
             }
         }else{amount=0;}

return amount;
}


How can I programmatically load and display all available blocks?

Question: <code>$block = Drupal\block\Entity\Block::load($block_id);
ksm($block);
</code>
This code loads only one specific block. But how can I load and display all available blocks?
Answer: Using the service plugin.manager.block we will get all blocks available definition.
<code>$blockManager = \Drupal::service('plugin.manager.block');

$contextRepository = \Drupal::service('context.repository');

// Get blocks definition

$definitions = $blockManager->getDefinitionsForContexts($contextRepository->getAvailableContexts());
</code>
To get details about one specific block you can use below code.
<code>print_r($definitions['help_block']);
</code>
Comment: how can we limit the list of block here.
Comment: I wish there was a drush command for that
Answer: Try this:
<code>$blocks = Drupal\block\Entity\Block::loadMultiple();
ksm($blocks);
</code>
Comment: This will help you find all the pluginIds you can then use: function _helper_block_build_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) {
  if($block->getPluginId() === "webform_block"){
    dd($block);
  }
}
Answer: The core block module using "getFilteredDefinitions" method to list the blocks
<code>$blockManager = \Drupal::service('plugin.manager.block');
$contextRepository = \Drupal::service('context.repository');

// Get blocks definition
$definitions = $blockManager->getFilteredDefinitions('block_ui', $contextRepository->getAvailableContexts());

print_r($definitions);
</code>
Comment: Can you please suggest how can we use `getFilteredDefinitions` to return just the content blocks (the ones created from the Blocks UI) and are visible on a given URL or a content type?
Answer: <code>$query = \Drupal::entityQuery('block_content');
$ids  = $query->execute();
</code>
You can also use 'block' instead of 'block_content' to get blocks from code


Quantum Hermiticity Bra-Ket notation please

Question:
If $A$ and $B$ are Hermitian operators, show that $$C~:=~i[A,B]$$ is Hermitian too.

My work:
$$\begin{gather}
C=i(AB-BA) \\
\langle\psi\rvert C\lvert\phi\rangle = i\langle\psi\rvert AB\lvert\phi\rangle-i\langle\psi\rvert BA\lvert\phi\rangle
\end{gather}$$
$A$ and $B$ are Hermitian such that:
$$\begin{align}
\langle\psi\rvert A\lvert\phi\rangle &= \langle\phi\rvert A\lvert\psi\rangle^* \\
\langle\psi\rvert B\lvert\phi\rangle &= \langle\phi\rvert B\lvert\psi\rangle^*
\end{align}$$
I know a little about the identy operator, which I've seen used to do a similar trick, but I'm not that clear on its exact meaning hmm... $$1=\sum_n\lvert n\rangle\langle n\rvert$$
The definition of Hermiticity I learnt from lectures is the one I stated above for A, can you prove it in this way for C? ie can you use bra-ket notation.
Answer: The bra-ket notation is very formal, but well, let's go through it.
We must prove that $\langle \psi | C | \phi \rangle = \langle \phi | C | \psi \rangle ^*$, where the $^*$ is complex conjugation.
First we prove that the adjoint of the composite operator $AB$ is $BA$, that is, that $\langle \psi | AB | \phi \rangle = \langle \phi | BA | \psi \rangle^*$.
For that, notice that
$\langle \psi | AB | \phi \rangle =
 \langle \psi | A \, 1 \, B | \phi \rangle =
 \sum_n \langle \psi | A | n \rangle\langle n | B | \phi \rangle =
 \sum_n \langle n | A | \psi \rangle^* \langle \phi | B | n \rangle^* =
 (\sum_n \langle \phi | B | n \rangle \langle n | A | \psi \rangle ) ^* =
 (\langle \phi | B \, 1 \, A | \psi \rangle ) ^* =
 \langle \phi | BA | \psi \rangle^*$.
By the linearity of the inner product, one has then
$\langle \psi | (iAB) | \phi \rangle =
 i(\langle \psi | AB | \phi \rangle) =
 (-i)^* (\langle \phi | BA | \psi \rangle)^* =
 (\langle \phi | (-iBA) | \psi \rangle)^*$.
Switching $AB$ to $BA$ then gives you:

$\langle \psi | (iAB) | \phi \rangle = (\langle \phi | (-iBA) | \psi \rangle)^*$.
$\langle \psi | (iBA) | \phi \rangle = (\langle \phi | (-iAB) | \psi \rangle)^*$.

Now one has
$\langle \psi | C | \phi \rangle
 = \langle \psi | (iAB) | \phi \rangle - \langle \psi | (iBA) | \phi \rangle
 = (\langle \phi | (-iAB) | \psi \rangle)^* - (\langle \phi | (-iBA) | \psi \rangle)^*
 = (\langle \phi | C | \psi \rangle)^*$.
That concludes the proof.
--
If you want to know what is going on, I recommend you find the definition of the adjoint of an operator. Mathematicians use the star $A^*$, and physicists use the dagger $A^\dagger$. If you know that $A\mapsto A^\dagger$ is antilinear and satisfies the rule $(AB)^\dagger = B^\dagger A^\dagger$, then your exercise can be solved by checking $C^\dagger = (iAB - iBA)^\dagger = -i BA + i AB = C$.


Python: how to translate complex SQL aggregate statements into pandas?

Question: With pandas, what is the best way to create the equivalent of a SQL group by statement, using:

a different aggregate function on each field (e.g. I need the sum of
field1, the avg of field2 and the max of field3)
slightly more
complex calculations like sum(field1) / sum(field2), e.g. for weighted
averages

Say I have a table with data at the city level and I want to aggregate it by country and region. In SQL I'd write:
select Country, Region
, count(*) as '# of cities'
,sum(GDP) as GDP
,avg(Population) as 'avg # inhabitants per city'
,sum(male_population) / sum(Population) as '% of male population'
from CityTable
group by Country, Region

How can I do the same in pandas? Thank you!
Answer: >>> df
   Country  Region  GDP  Population  male_population
0      USA      TX   10         100               50
1      USA      TX   11         120               60
2      USA      KY   11         200              120
3  Austria  Wienna    5          50               34
>>>
>>> df2 = df.groupby(['Country','Region']).agg({'GDP': [np.size, np.sum], 'Population': [np.average, np.sum], 'male_population': np.sum})
>>> df2
                GDP     male_population Population
               size sum             sum    average  sum
Country Region
Austria Wienna    1   5              34         50   50
USA     KY        1  11             120        200  200
        TX        2  21             110        110  220
>>>
>>> df2['% of male population'] = df2['male_population','sum'].divide(df2['Population','sum'])
>>> df2
                GDP     male_population Population      % of male population
               size sum             sum    average  sum
Country Region
Austria Wienna    1   5              34         50   50                 0.68
USA     KY        1  11             120        200  200                 0.60
        TX        2  21             110        110  220                 0.50
>>>
>>> del df2['male_population', 'sum']
>>> del df2['Population', 'sum']
>>> df2.columns = ['# of cities', 'GDP', 'avg # inhabitants per city', '% of male population']

Result
>>> df2
                # of cities  GDP  avg # inhabitants per city  % of male population
Country Region
Austria Wienna            1    5                          50                  0.68
USA     KY                1   11                         200                  0.60
        TX                2   21                         110                  0.50

Comment: Thanks. Useful, but I'm afraid it's still a bit convoluted when having to calculate, say, 20 aggregate fields. So far I'm finding it easier to write the dataframe to a sqlite3 database in memory, doing my group bys in sqlite, and outputting the result to a pandas dataframe.
I'm wondering if a more direct way exists.
Answer: Here is an alternative:
In [1]: import pandas as pd

In [2]: df = pd.read_csv('countries.csv')

In [3]: df
Out[3]:
  Country Region  GDP  Population  male_population
0      US     TX   10         100               50
1      US     TX   11         120               60
2      US     KY   11         200              120
3      AU     WN    5          50               34
4      AU     SY    8         100               60

In [4]: df.groupby(['Country', 'Region']).apply(lambda gb: pd.Series({
   ...:     '# of cities': len(gb),
   ...:     'GDP': gb['GDP'].sum(),
   ...:     'avg': gb['Population'].mean(),
   ...:     '% of male population': float(gb['male_population'].sum()) / gb['Population'].sum(),
   ...: }))
Out[4]:
                # of cities  % of male population  GDP  avg
Country Region
AU      SY                1                  0.60    8  100
        WN                1                  0.68    5   50
US      KY                1                  0.60   11  200
        TX                2                  0.50   21  110

One pitfall of this approach is that you can't reference a calculation in the query, i.e.: reuse the population's sum for the avg and % male results.


Why does the dominant lead to the tonic?

Question: Going from a dominant to the chord to which it is a dominant always sounds good to the ear. It feels like the chord leads to the next one. And if the composition or movement is written in a specific key, then when the harmony reaches a dominant chord, you feel a sense of unease and instability. Moreover, if the 7th key is added to the chord, the effect is increased.
I've heard that the reason for this has to do with half steps. For instance, if we consider the key to be C-major, then in order to form a C-major chord from a G-major chord, you only need to change the B to a C (minor second difference) and the D to an E (major second up). However, this also applies to the subdominant F-major, but the effect is completely different. And of course, you can go the other way - from the dominant to the tonic - and the steps are the same but reversed. So, it seems to me that just saying that the steps cause this effect isn't the full truth.
Basically, is there any known explanation as to why the dominant sounds like it leads to the tonic? When we listen to music, why does it sound so natural when chords follow in sequence through different dominants?
Comment: The dominant can also convincingly sound like it goes to the secondary dominant (anything from V/V to vii°7/V) or a deceptive chord (usually vi or (b)VI, although I'm seeing increasingly many German Augmented 6ths). Should answers also take those resolution tendencies into account?
Comment: That's very interesting. Could you perhaps give me an example of such a progression? Though I'm trying my best to translate it into specific chords, the results I get sound pretty different from what I expected (I got from G(/B) to B maj 7(/F#), but I don't usually use the number-convention-thing, so I might be misinterpreting something)
Comment: Using C major as the home key, an example of a dominant to secondary dominant progression (the dominant's equivalent to the tonic-to-dominant progression) is G to D7 (or V to V7/V). An example of a dominant to deceptive progression is G7 to Am (or V7 to vi).
Comment: Oh, got it! Thanks for clarifying! (And yes, for what I had in mind, those tendencies would be taken into account as well. Actually, that's what I'm interested in knowing - the fundamental nature of dominant chords and why such progressions sound good, rather than just the special case where the chord is the dominant to the home key.)
Comment: It can also lead to the VI chord.
Answer: You've done a nice job of describing the V-I progression, and you've identified the resolution through half steps and whole steps. All of this is important to understanding why the V-I sounds so good, but it's not the whole story. There is something else that a V-I progression possesses which is not present in a I-V progression or a IV-I progression. In a V-I progression, the V chord contains a chord tone that resolves to the root of the I chord. This is a crucial distinction which allows the V chord to resolve strongly to the I chord.
For example, here's a V7-I resolution in C maj, taken from Beethoven's Fifth Symphony:

The V7 chord contains a B which resolves to the root  of the I chord, C.
By contrast, playing | F maj | C maj | (a IV-I progression) would not be the same. The notes of the F maj chord are F A C, and the notes of the C maj chord are C E G. We can set up our chords so that:

the F resolves down a half step to the E
the A resolves down a whole step to the G
the C remains a C

As this shows, there are no notes from the IV chord (F maj) which resolve up or down a step to the root of the I chord (C maj). Rather, in this progression (with this particular voice leading), the tones of the IV chord only lead in to the 3rd and 5th tones of the I chord. To our ear, this IV-I resolution is weaker as the IV's chord tones provide no movement toward the root of the I chord. That movement toward the root of the I is present, though, in a V-I progression; in fact, our ear anticipates that movement/resolution of the chord tones, making the V-I progression even more natural sounding.
The instability of the V7 chord, as you've described, is largely due to the presence of a tritone (diminished fifth) interval in the V7 chord. In particular, the V7 chord contains both an F (the seventh of the V7 chord) and a B (the third of the V7 chord), and these two notes are 6 half notes apart. That particular interval has a quality of instability and dissonance that further drives the ear to desire resolution. This is part of why a I-V progression would not sound like a resolution.
When analyzing a progression, a key thing to look for is the particular chord tones involved in the resolution. For example, resolving to the root of the tonic creates stronger movement to the tonic than resolving to the fifth of the tonic.
Comment: The feel of V>I is different from I>V. One good reason is the suspense and resolution side of music, where the listener knows where 'home' is, sonically, so getting there is usually a blessed relief, especially using V7>I. Going the other way is the leaving home feeling, either to V *or* IV,  which in real life has a very different feel from arriving. +1.
Comment: +1, great answer! I'm curious if you might reword one section: "There is something else that a V-I progression possesses which is not present in a... IV-I progression. In a V-I progression, the V chord resolves to the root of the I chord." IV-I resolved to the root of the I chord as well, no?
Comment: @Richard, thanks for the helpful edit! You're right of course; what I wanted to say was that one of the chord tones in the V resolves to the root tone of the I chord. In a IV-I progression like F-C, there is no B or D in the F chord which could then resolve up/down a step to a C when the I chord is played. The C is already part of the F chord, and so the F chord doesn't have any tones that can resolve to the root of the I. I'll reword to communicate this more clearly.
Comment: @Tim, thanks for making this point! Just as you've said, a song provides important context and establishes the I chord as a home base--as the most natural-sounding place for a chord progression to resolve to.
Answer: Yes, it's about the minor second ("half step") movement.
The progression can be generalized in terms of root progression and voice leading: root progression by descending perfect fifth, and voice leading of one voice held and two voices moving by step.
In the diatonic gamut that only happens in two places: Vb I and Ib IV. (b means first inversion.) There is a sort of equivalence in those two progressions in terms of root progression and voice leading. In a purely diatonic context the two can be distinguished by making V a dominant seventh chord, like V7b I. You cannot do the same diatonically moving to IV and the added seventh in V7b provides two minor-second motions resolving to I in TI DO and FA MI.
It's interesting to compare these progressions with some other progressions that also have the voice leading of one voice held and two moving by step:

ivc I the borrowed minor iv
IIIc I a chromatic mediant relationship
Fr+6 V an imperfect cadence progression (Fr means the French sixth)

While those chords are all chromatic it's important to note the chord roots are diatonic. So while they are chromatic, they are also quite "at home" in a key.
Technically Fr+6 V is root progression by descending P5 and it has two minor-second resolutions, with one of those moving to the chord root of V. That is quite strong.
ivc I is root progression by descending P4 a bit "less strong" than descending P5, and the non-movement to the root of I as a held note provides some theoretical explanation of why it's "less strong."
IIIc I is interesting, because while the root progression would be considered a "weaker" roots by descending third it does have a minor-second motion to the root of I. This is a chromatic mediant relationship. And while the root progression may be "weak" the chromatic mediant is unique and very identifiable. They are "strong" in chromatic color and expressive potential.
To some degree I'm simply saying an important, unique distinction for V7 I, in addition to the minor-second movements, is that it's a diatonic progression. But some other chromatic progressions share similar characteristics. It's good to be aware of those progressions. And they also reinforce the notion that minor-second motions do indeed create "strong" progressions.
Finally, I think it needs to be stated that the "strength" of the dominant leading to the tonic, the strength of the leading-note moving up to the tonic, is a matter of convention. It's perceived as strong, because that is how the major/minor system treats it. However, in modal style, in the Phrygian mode, this convention is literally turned upside down. In Phrygian the "leading note" is a minor-second above the tonic. Final cadences might look like vii I or viib I, a minor subtonic chord moving to a tonic with a tierce de Picardie (two voices moving by a minor second, but root progression by a major second). (In the Phrygian mode, the seventh degree --- the root of the chord vii --- is a major second below the tonic.) That progression totally reinforces the theory that minor-second movements make strong progressions, but allows for tonalities other than only major and minor keys.


settings.php after migration

Question: I transferred my files via FTP and database to a hosting service. I edited two files, settings.php and sites/default/settings.php whose content was originally the following.
$databases = array (
  'default' =>
  array (
    'default' =>
    array (
      'database' => 'db',
      'username' => 'loginname',
      'password' => 'passwd',
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
);

I changed the database name, username, password and host (to yes.domainname.com) in both files.
There is probably something wrong because I get this warning.

Warning: Cannot modify header information - headers already sent by (output started at /web/www/yes.domainname.com/sites/default/settings.php:1) in drupal_send_headers() (line 1044 of /web/www/yes.domainname.com/includes/bootstrap.inc).

Do you know what is wrong?
Answer: Verify that there are not extra characters at the beginning of the file, right before the <?php part; this would cause the error you have seen.
Sometimes the editors add the BOM (Byte Order Mark) when saving an Unicode file; verify Unicode is not the text encoding used to save the file, or that the editor is not set to add the BOM to the files it saves as Unicode.
The BOM characters are not normally visible in a text editor; if the editor has the option to show characters that have a specific code, or "invisible characters," that can help to notice them.
Alternatively, the file got corrupted when transferred.
Comment: I would rather use the domain name used by the website.
Comment: Thank you. I uploaded the two files again and edited it with gvim instead of oowriter (from open office) and it helped. I let host as localhost and it works. Is it better to let it as localhost or use rather yes.domainname.com?

settings.php after migration


PHP email is not working

Question: I have been at this for hours and I cant determine why my PHP email form is not working.
I have set up if-then check for each form field since all fields are optional at this point in time. I have checked everything I can think of.
<?php
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "email@example.com";
    $email_subject = "IND Form";

    if(!isset($_POST['submit_name'])){$submit_name = " ";}
    else{$submit_name = $_POST['submit_name'];}

    if(!isset($_POST['companyName'])){$companyName = " ";}
    else{$companyName = $_POST['companyName'];}

    if(!isset($_POST['companyLocation'])){$companyLocation = " ";}
    else{$companyLocation = $_POST['companyLocation'];}

    if(!isset($_POST['companyCity'])){$companyCity = " ";}
    else{$companyCity = $_POST['companyCity'];}

    if(!isset($_POST['companyState'])){$companyState = " ";}
    else{$companyState = $_POST['companyState'];}

    if(!isset($_POST['timeframe'])){$timeframe = " ";}
    else{$timeframe = $_POST['timeframe'];}

    if(!isset($_POST['BenchCountingScales'])){$benchCountingScales = " ";}
    else{$benchCountingScales = $_POST['BenchCountingScales'];}

    if(!isset($_POST['FloorScales'])){$floorScales = " ";}
    else{$floorScales = $_POST['FloorScales'];}

    if(!isset($_POST['WeighModulesLoadCells'])){$weighModulesLoadCells = " ";}
    else{$weighModulesLoadCells = $_POST['WeighModulesLoadCells'];}

    if(!isset($_POST['TruckRailScales'])){$truckRailScales = " ";}
    else{$truckRailScales = $_POST['TruckRailScales'];}

    if(!isset($_POST['SQCFormulationSoftwareSolutions'])){$SQCFormulationSoftwareSolutions = " ";}
    else{$SQCFormulationSoftwareSolutions = $_POST['SQCFormulationSoftwareSolutions'];}

    if(!isset($_POST['Other'])){$other = " ";}
    else{$other = $_POST['Other'];}

    if(!isset($_POST['PreventativeMaintenanceAgreement'])){$preventativeMaintenanceAgreement = " ";}
    else{$preventativeMaintenanceAgreement = $_POST['PreventativeMaintenanceAgreement'];}

    if(!isset($_POST['GWPVerification'])){$GWPVerification = " ";}
    else{$GWPVerification = $_POST['GWPVerification'];}

    if(!isset($_POST['Other2'])){$other2 = " ";}
    else{$other2 = $_POST['Other2'];}

    if(!isset($_POST['CustomerApplication'])){$customerApplication = " ";}
    else{$customerApplication = $_POST['CustomerApplication'];}

    if(!isset($_POST['Salutation'])){$salutation = " ";}
    else{$salutation = $_POST['Salutation'];}

    if(!isset($_POST['CustomerFirstName'])){$customerFirstName = " ";}
    else{$customerFirstName = $_POST['CustomerFirstName'];}

    if(!isset($_POST['CustomerLastName'])){$customerLastName = " ";}
    else{$customerLastName = $_POST['CustomerLastName'];}

    if(!isset($_POST['CustomerAcademicTitle'])){$customerAcademicTitle = " ";}
    else{$customerAcademicTitle = $_POST['CustomerAcademicTitle'];}

    if(!isset($_POST['CustomerEmail'])){$customerEmail = " ";}
    else{$customerEmail = $_POST['CustomerEmail'];}

    if(!isset($_POST['CustomerPhone'])){$customerPhone = " ";}
    else{$customerPhone = $_POST['CustomerPhone'];}

    if(!isset($_POST['CustomerFax'])){$customerFax = " ";}
    else{$customerFax = $_POST['CustomerFax'];}

    if(!isset($_POST['CustomerCompanyName'])){$customerCompanyName = " ";}
    else{$customerCompanyName = $_POST['CustomerCompanyName'];}

    if(!isset($_POST['CustomerDepartment'])){$customerDepartment = " ";}
    else{$customerDepartment = $_POST['CustomerDepartment'];}

    if(!isset($_POST['CustomerJobTitle'])){$customerJobTitle = " ";}
    else{$customerJobTitle = $_POST['CustomerJobTitle'];}

    if(!isset($_POST['CustomerStreet'])){$customerStreet = " ";}
    else{$customerStreet = $_POST['CustomerStreet'];}

    if(!isset($_POST['CustomerCountry'])){$customerCountry = " ";}
    else{$customerCountry = $_POST['CustomerCountry'];}

    if(!isset($_POST['CustomerCity'])){$customerCity = " ";}
    else{$customerCity = $_POST['CustomerCity'];}

    if(!isset($_POST['CustomerState'])){$customerState = " ";}
    else{$customerState = $_POST['CustomerState'];}

    if(!isset($_POST['CustomerPostalCode'])){$customerPostalCode = " ";}
    else{$customerPostalCode = $_POST['CustomerPostalCode'];}

    if(!isset($_POST['BusinessType'])){$businessType = " ";}
    else{$businessType = $_POST['BusinessType'];}

    if(!isset($_POST['industryType'])){$industryType = " ";}
    else{$industryType = $_POST['industryType'];}

    if(!isset($_POST['Employees'])){$semployees = " ";}
    else{$employees = $_POST['Employees'];}

    $email_message = "Form details below.\n\n";

    $email_message .= "Lead Submitted By: $submit_name\n";
    $email_message .= "Company Name: $companyName\n";
    $email_message .= "Location: $companyLocation\n";
    $email_message .= "City: $companyCity\n";
    $email_message .= "State: $companyState\n\n";

    $email_message .= "Purchase Timeframe: $timeframe\n\n";

    $email_message .= "Product Interest:\nBench Counting Scales: $benchCountingScales\n";
    $email_message .= "Floor Scales: $floorScales\n";
    $email_message .= "Weigh Modules Load Cells: $weighModulesLoadCells\n";
    $email_message .= "Truck Rail Scales: $truckRailScales\n";
    $email_message .= "SQC Formulation Software Solutions: $SQCFormulationSoftwareSolutions\n";
    $email_message .= "Other: $other\n";
    $email_message .= "Service Interest:\nPreventative Maintenance Agreement: $preventativeMaintenanceAgreement\n";
    $email_message .= "GWP Verification: $GWPVerification\n";
    $email_message .= "Other: $other2\n\n";

    $email_message .= "Customer Information:\nCustomer Application: $customerApplication\n";
    $email_message .= "Salutation: $salutation\n";
    $email_message .= "First Name: $customerFirstName\n";
    $email_message .= "Last Name: $customerLastName\n";
    $email_message .= "Academic Title: $customerAcademicTitle\n";
    $email_message .= "Email: $customerEmail\n";
    $email_message .= "Phone: $customerPhone\n";
    $email_message .= "Fax: $customerFax\n";
    $email_message .= "Company: $customerCompanyName\n";
    $email_message .= "Department: $customerDepartment\n";
    $email_message .= "Job Title: $customerJobTitle\n";
    $email_message .= "Address:\nStreet: $customerStreet\n";
    $email_message .= "Country: $customerCountry\n";
    $email_message .= "City: $customerCity\n";
    $email_message .= "State: $customerState\n";
    $email_message .= "Postal Code: $customerPostalCode\n\n";

    $email_message .= "Type of Business: $businessType\n";
    $email_message .= "Industry: $industryType\n";
    $email_message .= "Number of Employees: $employees\n\n";

// create email headers
$headers = 'From: '.$email_to."\r\n".
'Reply-To: '.$email_to."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>

Thank you for submitting.

<?php
}
?>

Comment: In general it's not recommended to use `mail()` directly - once you get into multipart and attachments, it gets messy quickly. Consider using a library like SwiftMailer.
Answer: @mail($email_to, $email_subject, $email_message, $headers);
Should be:
mail($email_to, $email_subject, $email_message, $headers);
Remove the @ and see if there are any errors.
Also keep in mind that the mail() function does not have support for SMTP. You may have to check your settings. Could be another issue.
Comment: Additional to the answer: The `@` symbol in front of a function silences it. Meaning, you won't get any types of error messages when executing it, even if it fails. So I also suggest: **don't use it**.
Comment: Maybe [this](http://stackoverflow.com/questions/6093976/setting-smtp-details-for-php-mail-function) post can help you out there.
Answer: I figured out the issue...
Line 2:
if(isset($_POST['email'])) {

contains a reference to "email" which is a field that no longer exits in the form.
Thank you all for your help and at least I learned something (what the @ symbol does in PHP).
Comment: That would do it. Glad you figured it out.

PHP email is not working


Ember-CLI Installation not recognized

Question: I have followed the following tutorial and have successfully installed everything but ember-cli.
http://www.ember-cli.com/#getting-started

node --help (shows output help messages)
npm --help  (shows output help messages )
npm install -g bower
npm install -g phantomjs

All the above work with no problems but when I try to ember new my-new-app
I got the following
$ ember new my-new-app
-bash: ember: command not found

When I do $ npm install -g ember-cli
I get the following
$ npm install -g ember-cli
/Users/MGK/.node/bin/ember -> /Users/MGK/.node/lib/node_modules/ember-cli/bin/ember
firstname.lastname@example.com /Users/MGK/.node/lib/node_modules/ember-cli
├── email@example.com
├── email@example.com
├── email@example.com (email@example.com)
├── firstname.lastname@example.com (email@example.com)
├── email@example.com (email@example.com)
├── email@example.com (email@example.com, firstname.lastname@example.com)
├── email@example.com (email@example.com, firstname.lastname@example.com)
├── firstname.lastname@example.com (email@example.com, email@example.com, firstname.lastname@example.com)
└── email@example.com (firstname.lastname@example.com, firstname.lastname@example.com, quick-         email@example.com, firstname.lastname@example.com, email@example.com, firstname.lastname@example.com)

Any ideas?
Update, here is my echo $PATH
$ echo $PATH
/Users/MGK/.rvm/gems/ruby-2.1.2/bin:/Users/MGK/.rvm/gems/ruby-2.1.2@global/bin:/Users/MGK/.rvm/rubies/ruby-2.1.2/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/MGK/.rvm/bin

Comment: Just to clarify, did you try `ember new my-new-app` again after running `npm install -g ember-cli`?
Comment: Yup, still getting `-bash: $: command not found`
Comment: Your paths are probably screwed up. Is `bower` found? Check to make sure the directory containing globally-installed node modules executables is on your path.
Comment: @torazaburo That's what I was thinking. @Mills If you have trouble evaluating your path on your own, you could always update your question with the contents of `echo $PATH`
Comment: @torazaburo yes bower is found, @wisew let me update my question with my `echo $PATH` output, thanks!
Answer: The problem is that npm's path isn't in your $PATH variable, so your shell has no idea where to look for any of your npm modules. There are a few ways to remedy this:

Run npm config get prefix. Open your .bash_profile or .bashrc config file and add the following line:
export PATH="~/.node/bin:$PATH"

This will add your npm executables to your path. (~/.node/bin was taken from the console output when you installed ember-cli)
Then run source ~/.bashrc or source ~/.bash_profile depending on which file you edited. This will load the changes you've made to your $PATH. Or:
Run npm config set prefix /usr/local (/usr/local since you're on a Mac and it's already in your $PATH).

(See this question for a more general instance of the same npm installation issue.)
Comment: I have /usr/local as the prefix and added that to my path.. still no luck
Answer: For windows users;
Add your nmp path to the PATH environment variable.
Start-> Advanced System Settings->Environment Variables
Under the System Variables section, click Path and then edit.
Add C:\Users\username\AppData\Roaming\npm to the list.
After a restart it worked for me.
Hope it helps.
Comment: this worked for me. thanks
Answer: is there literally that giant gap (tab or spaces) in the in your path when you run $PATH?
if so, your path may be corrupt because of that whitespace.  based on the PATH your provided, try doing this to reset your PATH. paste this line into your terminal window:
PATH=/Users/MGK/.rvm/gems/ruby-2.1.2/bin:/Users/MGK/.rvm/gems/ruby-2.1.2@global/bin:/Users/MGK/.rvm/rubies/ruby-2.1.2/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/MGK/.rvm/bin

Comment: I updated my $PATH above, the whitespace is not there anymore. Thanks for pointing that out!
Answer: As said by @wisew, the issue is that npm path not being set in the path variable!
I faced the same issue in my win7 machine, I fixed the issue by adding "C:\Users\username\AppData\Roaming\npm" to the path variable as below.

open command prompt (cmd) and run
setx "%path%;C:\Users\username\AppData\Roaming\npm"
Reopen the cmd to confirm the changes
run command ember

You should be seeing that ember command is detected now and is working :)
Thank you!

Ember-CLI Installation not recognized


NHibernate: restore inner objects' events

Question: Suppose I have
public class Parent
{
   private IList<Child> _children;
   public void AddChild(Child child)
   {
      _children.Add(child);
      child.OnSmthChanged += ParentSmthChangedHandler;
   }
   public void RemoveChild(Child child)
   {
      _children.Remove(child);
      child.OnSmthChanged -= ParentSmthChangedHandler;
   }
}
public class Child
{
   public event EventHandler OnSmthChanged;
}

How do I make NHibernate to restore events when _children are loaded from database? Notice that:

I don't want Parent bi-directional reference in Child
_children is a field so I cannot intercept its "set"
I don't like to use global events like OrderEvents.OrderChanged(order)

So, I think about intercepting NHibernate doing load of _children and there recursively add events. Two questions:

How do I do this interception for _children loading? Intercepting all collections loading would be too ineffective I guess. Small note: I use Fluent NHibernate so specific advice would be nice.

Something like OnAfterObjectCreated(Parent) would not work because I will not have access to the private field. Well, I can use reflection to setup if this is the only way.

Is there a better way?
Answer: You can tell nhibernate to use observable collection for _children field:
<bag collection-type="uNhAddIns.WPF.Collections.Types.ObservableBagType`1[Your.Namespace.Child
, Your.Assembly.Name], uNhAddIns.WPF" ... />

then:
public Parent()
{
   (_children as INotifyCollectionChanged).CollectionChanged += DoSomething;
}

public void DoSomething(object sender, NotifyCollectionChangedEventArgs e)
{
    if(e.Action == NotifyCollectionChangedAction.Add)
        foreach(var child in e.NewItems.Cast<Child>())
           child.OnSmthChanged += ParentSmthChangedHandler;

    if(e.Action == NotifyCollectionChangedAction.Remove)
        foreach(var child in e.OldItems.Cast<Child>())
           child.OnSmthChanged -= ParentSmthChangedHandler;
}

uNhAddins you can find here.


If $\{F_i:i\in\Bbb{N}\}$ is a collection of not empty closed sets mutually disjoint in a continuum $X$ then $X\neq\bigcup_{i\in\Bbb{N}}F_i$.

Question: What shown below is a reference from "Elementos de Topología General" by Fidel Cassarubias Segura and Ángel Tamariz Mascarúa

Lemma
If $X$ is a continuum that is covered by a sequence of numerable mutually disjoint closed sets $F_1,...,F_n$  of which at least two are not empty then for any $i\in\Bbb{N}$ there exist a continuum $C\subseteq X$ such that $C\cap F_i=\varnothing$ and at least two of the sets $C\cap F_1, C\cap F_2,...,C\cap F_n,...$ are not empty.
Theorem
If $\{F_i:i\in\Bbb{N}\}$ is a collection of not empty closed sets mutually disjoint in a continuum $X$ then $X\neq\bigcup_{i\in\Bbb{N}}F_i$.
Proof. So we suppose that the theorem if false. So $X=\bigcup_{i\in\Bbb{N}}F_i$, where $\mathcal{F}=\{F_i:i\in\Bbb{N}\}$is a partition of $X$ that is composed of closed disjoint set of which at least two are not emtpy. Since $X$ is connected, the collection of the elements of $\mathcal{F}$ that are not empty is infinite. So without loss of generality we can suppose that any $F_i$ is not empty. So for the previous lemma there exist a continuum $C_1\subseteq X$ such that $C_1\cap F_1=\varnothing$ and at least two of the sets of the sequence $C_1\cap F_\, C_1\cap F_2,...$ are not empty. So the continuum $C_1$ is equal to $\bigcup_{n\in\Bbb{N}}(C_1\cap F_i)$ and $C_1$ and $\{C_1\cap F_i:i\in\Bbb{N}\}$ respect the condictions of the previous lemma and so there exist a conitnuum $C_2\subseteq C_1$ such that $C_2\cap(C_1\cap F_2)=C_2\cap C_2\cap F_2=\varnothing$ and at least two elements of the sequence $C_2\cap F_1, C_2\cap F_2$ are not empty. So in this way we cand make a sequence $C_1\supseteq C_2\supseteq...\supseteq C_n\supseteq....$ of continuum of $X$ such that $C_i\cap F_i=\varnothing$ for any $i\in\Bbb{N}$. So this means that
  $$
\Big(\bigcap_{i\in\Bbb{N}}C_i\Big)\cap\Big(\bigcup_{i\in\Bbb{N}}F_i\Big)=\varnothing
$$
  and so $\bigcap_{i\in\Bbb{N}}C_i=\varnothing$. However since $X$ is compact, it must be $\bigcap_{i\in\Bbb{N}}C_i\neq\varnothing$, that is impossible.

Here for sake of completeness the text of the original proof: I hope that my was a good traslation.

So I don't understand why $\Big(\bigcap_{i\in\Bbb{N}}C_i\Big)\cap\Big(\bigcup_{i\in\Bbb{N}}F_i\Big)=\varnothing$. Indeed using induction it is possible to proof that $\Big(\bigcap_{i=1}^n C_i\Big)\cap\Big(\bigcup_{i=1}^n F_i\Big)=\varnothing$ for any $n\in\Bbb{N}$ but I don't know if $\Big(\bigcap_{i\in\Bbb{N}}C_i\Big)\cap\Big(\bigcup_{i\in\Bbb{N}}F_i\Big)=\bigcup_{n\in\Bbb{N}}\Big[\Big(\bigcap_{i=1}^n C_i\Big)\cap\Big(\bigcup_{i=1}^n F_i\Big)\Big]=\varnothing$.  Moreover is it must br $\bigcap_{i\in\Bbb{N}}C_i\neq\varnothing$ because the collection of $C_i$ is a collection of closed sets of $X$ that has the finite intersection property?
Finally I don't understad why in the first part we prove that any $F_i$ is not empty if $X$ is connected: indeed for hypothesis $\{F_i:i\in\Bbb{N}\}$ is a collection of not empty closed sets.
So could someone help me please?
Answer: Note that for each $n\in\Bbb N$ you have
$$\left(\bigcap_{i\in\Bbb N}C_i\right)\cap\left(\bigcup_{i=1}^nF_i\right)\subseteq\left(\bigcap_{i=1}^nC_i\right)\cap\left(\bigcup_{i=1}^nF_i\right)=\varnothing\;,$$
so
$$\left(\bigcap_{i\in\Bbb N}C_i\right)\cap\left(\bigcup_{i\in\Bbb N}F_i\right)=\bigcup_{n\in\Bbb N}\left(\left(\bigcap_{i\in\Bbb N}C_i\right)\cap\left(\bigcup_{i=1}^nF_i\right)\right)=\bigcup_{n\in\Bbb N}\varnothing=\varnothing\;.$$
Comment: @AntonioMariaDiMauro: We only assume that at least two of the sets $F_i$ are non-empty. The first part of the proof points out that $X$ cannot be covered by a finite family of two or more pairwise disjoint connected sets, so if at least two of the $F_i$ are non-empty, infinitely many of them must be non-empty in order to cover $X$. But then we can simply throw away any empty ones and assume that all of the $F_i$ are non-empty: we'll still have an infinite collection of them.
Comment: @AntonioMariaDiMauro: Good question; I didn't notice that the statement of the theorem required all of the sets to be non-empty. My best guess is that the author wanted to give a reference for the result and therefore stated it more or less as Sierpiński had but was thinking in terms of the lemma when he wrote up the proof and didn't notice that he was doing unnecessary work. Whatever the reason, it does look like an oversight on his part.
Comment: Unfortunately in Italy this is necessary: italian teachers are damn listless and moreover they don't teach these things. They teach Calculus like they were born in XIX century, because they claim Topology is not necessary. Nevertheless, in my opinion mathematicians created it to make Calculus simpler and more intuitive, so I think everybody should study Set Theory and Topology. Indeed, I'm studing engineering and to study Fluid Dynamics I have to learn Differential Geometry and so even Topology and Set Theory. That's all.
Comment: Okay, so finally what can you say about the observation I did about the first part of the proof?
Comment: Okay, it is clear: however in the enunciation of the theorem it seems to me that we immediately suppose that any $F_i$ is not empty so what it is written in first part is pleonastic.
Comment: Okay, all clear. So thanks to much for your assistance!!! indeed I'm a self-taught so your explanation are very useful to me!!! so thanks again.
Comment: @AntonioMariaDiMauro: You're very welcome; it can be tough when you're working on your own.
Answer: If $x\in \bigcap C_i\cap\bigcup F_i$, then $x\in F_i$ for some $i$. For this (as for any) $i$, also $x\in C_i$, but $C_i\cap F_i=\emptyset$.


Ordered Tests in Visual Studio 2008

Question: I created an Ordered Test in Visual Studio 2008, which includes a subset of unit tests in the project.
How do I now run this Ordered Test (without running every other test).
Answer: The ordered test appears in the test view as the filename you gave it. You can highlight it and run it by itself.

Ordered Tests in Visual Studio 2008