Different bar colors for each poll choice?

Question: I know how to change ALL of the bar colors - but how do I code it so each choice bar is a different color?  I basically just need code to append the number to the "bar" class, so it would read "bar-1" , "bar-2" etc.
Answer: jQuery can help you in this regard if you not going to edit templates.
(function(){
   var i=0;
   jQuery('div.poll .bar .foreground').each(function(){
        i++;
        switch(i){
            case 1:
                $(this).css('background-color', '#color')
            break;
             ...
         }

    });
})();

Comment: I'm fine with editing the templates if that's a better way to do it.  I'm not good with jQuery at all - what would the rest (...) of that function actually look like?
Answer: This is not simple without patchs in poll.module and poll-bar.tpl.php.
PATCH: poll.module Adding an additional parameter (i.e. choice id)
/**
 * Implementation of hook_theme()
 */
function poll_theme() {
  return array(
    ....
    'poll_bar' => array(
      'template' => 'poll-bar',
      'arguments' => array(
          'title' => NULL,
          'votes' => NULL,
          'total_votes' => NULL,
          'vote' => NULL,
          'block' => NULL,
          'choice_id' => null), // An additional param
    ),
  );
}

Changes where theme('poll_bar') is invoked. NOTE: $i is passed as last param
  foreach ($node->choice as $i => $choice) {
    if (!empty($choice['chtext'])) {
      $chvotes = isset($choice['chvotes']) ? $choice['chvotes'] : NULL;
      $poll_results .= theme('poll_bar',
                $choice['chtext'],
                $chvotes,
                $total_votes,
                isset($node->vote) && $node->vote == $i,
                $block,
                $i, // The additional param
                );
    }
  }

PATCH: poll-bar.tpl.php Adding classes to div.bar as choice-x

<?php print $title; ?>

<div class="bar choice-<?php print $choice_id;?>">
  <div style="width: <?php print $percentage; ?>%;" class="foreground">

  <?php print $percentage; ?>% (<?php print format_plural($votes, '1 vote', '@count votes'); ?>)

Answer: Consider trying CSS Pseudo-classes. Specifically have a look at :nth-of-type() where you can specify specific colors for each instance of a class or tag in the theme's CSS.


Show that $B \setminus A \sim B$ where $A \subseteq B$ countable and $B \setminus A$ is infinite

Question: Given $A\subseteq B$ such that $A$ is countable and $B\setminus A$ is infinite. I want to show that the cardinality of $B\setminus A$ is the same as the cardinality of $B$.
I know that since $B \setminus A$ is infinite, there is a function $g: \mathbb{N} \rightarrow B \setminus A$ injective. But how does this help me find a bijective function $f: B \setminus A \rightarrow B$ ?
Comment: Can you assume that any set can be well ordered?
Comment: $A$ is countable, which means it is bijective to a subset of $\Bbb N$. $g$ is also a bijection between $\Bbb N$ and $g(\Bbb N)$. So if you can think of a way to map the *disjoint* union of a subset of $\Bbb N$ and $\Bbb N$ to $\Bbb N$ bijectively, you have a ready-made bijection between $A\cup g(\Bbb N)$ and $g(\Bbb N)$.
Comment: Not really @Robert Shore
Comment: @Paul Sinclair If I understand what you say the idea is to find a mapping $h: C \cup \mathbb{N} \rightarrow \mathbb{N}$ bijective where $C \subset \mathbb{N}$?
Comment: Almost. Note that I emphasized the **disjoint** union. You don't get to ignore that part. This turns the problem from being about sets of which you know almost nothing to a set which you've been studying since you were a child yourself, and for which a vast body of knowledge exists.
Comment: But if $C \in \mathbb{N}$ how come $C \cup \mathbb{N}$ can be disjoint? @Paul Sinclair
Comment: See [Disjoint union](https://en.wikipedia.org/wiki/Disjoint_union). What is important here is that both sets can be treated as natural numbers, so that you can use whatever operations are handy to meet the challenge of fitting these together.
Answer: Let $h: \Bbb N \to A$ be a bijection and denote $a_n=h(n)$.
Let $g: \Bbb N \to B \setminus A$ be an injection and denote $b_n=g(n)$.
$$\text{Define } f: B \to B \setminus A \text{ via }f(x)=
\begin{cases}
b_{2n}, & \text{if }x=a_n \\
b_{2n+1}, & \text{if } x=b_n \\
x & \text{otherwise}
\end{cases}$$
Then $f$ is a bijection.


What is a simple definition of the pullback of a section?

Question: I am simply asking for a definition for something everyone uses but nobody defines. Really, this is used in class and in Hartshorne, and I have tried to look for a definition in Hartshorne, Qing Liu, Wikipedia, nothing comes up, so I am wondering whether somebody on this planet knows a definition of this.
Let $X,Y$ be topological spaces and $F, G$ be sheaves of modules over $X,Y$ respectively.
The pull-back of a sheaf is very well-documented and defined everywhere with high precision:
If $f:X\rightarrow Y$ is a continuous map, then
$f^*G=f^{-1}G\otimes_{f^{-1}O_Y} O_X$
So I know what $f^*G$ and what $(f^*G)(U)$ are (with $U \subset X$).
But what is $f^*s$ if $s\in G(Y)$, or more generally $s \in G(U)$ where $U \subset Y$ is some open subset of $Y$?
I know there is already a discussion in this thread and apparently the definition is given in a comment for affine schemes (it is just the image by the induced ring map), but I don't find it particularly enlightening. Could somebody please provide a straightforward definition for the pull-back of a section of a sheaf of modules on a general scheme? Can it be defined in a simple way (with e.g. a formula) without using high-powered, unintelligible stuff? In particular I don't know what adjunction correspondance is...
Comment: I think from the étale space point of view of sheaves, where sections are literal sections of certain continuous maps, this may correspond to the pullback section explained in Mac Lane-Moerdijk _Sheaves in Geometry and Logic_, §II.9, equation (3).
Answer: There is a pretty geometric answer, if $X, Y$ are schemes and $G = \mathcal{O}_Y$ is the structure sheaf. In that case, a global section $s \in \Gamma(Y, \mathcal{O}_Y)$ is the same as a morphism $Y \to \mathbb{A}^1_\mathbb{Z}$. The pull-back $f^*s$ then corresponds to the composed morphism
$$X \xrightarrow{f} Y \to \mathbb{A}^1_\mathbb{Z},$$
and defines in that way a global section $f^*s \in \Gamma(X, \mathcal{O}_X)$.
For arbitrary ringed spaces $X,Y$, and sheaf $G$, verify that a global section $s \in \Gamma(Y, G)$ is the same as a $\mathcal{O}_Y$-module homomorphism $s: \mathcal{O}_Y \to G$. Pulling-back that morphism yields
$$f^*s: \mathcal{O}_X = f^*\mathcal{O}_Y \to f^*G,$$
which defines the global section $f^*s \in \Gamma(X, f^*G)$. Here it is important to verify, that $f^*$ is a functor $\operatorname{Mod}(Y) \to \operatorname{Mod}(X)$.
Also verify that this yields the first construction in the case $G = \mathcal{O}_Y$.
Comment: In the easy affine case, is the morphism just the one corresponding to $\mathbb{Z}[T] \rightarrow A$ which evaluates the polynomial at $s \in A$ if $A = \Gamma(Y,O_Y)$? It is the only obvious one that comes to mind that could be associated to a global section
Comment: So the pull-back of a section is only defined for global sections on the structure sheaf? Why is $s \in \Gamma(Y,O_Y)$ equivalent to a morphism  $Y \rightarrow \operatorname{Spec}A[T]$? And what is $A$ in this case?
Comment: I added the general case. Here $\mathbb{A}^1 = \operatorname{Spec} \mathbb{Z}[T]$. The equivalence $Y \to \mathbb{A}^1$ and $s \in \Gamma(Y, \mathcal{O}_Y)$ is easy, if $Y$ is affine. I guess the non-affine case can be done by gluing the map (or the section) obtained on an affine cover.
Comment: @Evariste Exactly.
Comment: Ok, thanks a lot.
Answer: red_trumpet's answer is quite categorical, choosing to talk about morphisms and apply functoriality in place of talking about elements as much as possible. This is a good perspective, but it can also be useful to understand what's going on directly at the level of elements, so I thought I'd add another answer. In fact, there's a fairly satisfying "only thing it could be" definition of $f^*s$.
Suppose we have: $f\colon X\to Y$ a morphism of schemes, $G$ a sheaf of $\mathcal{O}_Y$-modules, $U\subseteq Y$ open, and $s\in G(U)$.
First of all, what should $f^*s$ be? The most natural answer is that we should have $f^*s \in f^*G(f^{-1}U)$. Ok, now let's unpack the definitions. Set $V = f^{-1}(U)$.
First, $f^*G = f^{-1}G\otimes_{f^{-1}\mathcal{O}_Y}\mathcal{O}_X$. This tensor product is the sheafification of the tensor product presheaf $f^{-1}G\otimes^{\text{p}}_{f^{-1}\mathcal{O}_Y}\mathcal{O}_X$ (that p is for "presheaf"). And there is a map of presheaves from any presheaf to its sheafification. So to give an element of $f^*G(V)$, it suffices to give an element of $$(f^{-1}G\otimes_{f^{-1}\mathcal{O}_Y} ^{\text{p}}\mathcal{O}_X)(V) = f^{-1}G(V)\otimes_{f^{-1}\mathcal{O}_Y(V)}\mathcal{O}_X(V).$$
Now given an $R$-module $M$ and an extension of rings $R\to S$, there is a natural map $m\mapsto m\otimes_R 1_S$ from $M$ to the "base changed" $S$-module $M\otimes_R S$ So to give an element of the module above, it suffices to give an element of the module $f^{-1}G(V)$.
The sheaf $f^{-1}G$ is the sheafification of the presheaf $f^{-1}_\text{p}G$ defined by $$f^{-1}_\text{p}G(V) = \text{colim}_{W\supseteq f(V)} G(W).$$
Again, there is a map from any presheaf to its sheafification, so to give an element of $f^{-1}G(V)$, it suffices to give an element of  $f^{-1}_\text{p}G(V)$.
We have $U\supseteq f(f^{-1}U) = f(V)$, so $G(U)$ is one of the modules included in the colimit diagram. That is, we have a map $$G(U) \to \text{colim}_{W\supseteq f(V)} G(W) = f^{-1}_\text{p}G(V).$$
The image of $s\in G(U)$ under this long chain of maps $$G(U)\to f^{-1}_\text{p}G(V) \to f^{-1}G(V) \to f^{-1}G(V)\otimes_{f^{-1}\mathcal{O}_Y(V)} \mathcal{O}_X(V)\to f^*G(V)$$ is the desired element $f^*s\in f^*G(V)$.
Comment: @PerelMan But the map from a presheaf to its sheafification is totally explicit. One way to construct the sheafification is this: Let $F$ be a presheaf on $X$. For open $U\subseteq X$, set $F^{\text{sh}}(U)$ be the set of all compatible families of germs of $F$ at all points of $U$ (meaning for all $p\in U$, we get a germ $g_p\in F_p$, and there exists a neighborhood $p\subseteq V\subseteq U$ such that there is a section $f\in F(V)$ and $g_q = f_q$ for all $q\in V$). Then the map $F(U)\to F^{\text{sh}}(U)$ takes a section $f\in F(U)$ to its family of germs $(f_p)_{q\in U}$.
Comment: Yep, thats more explicit. One note: In general, a map $M \to M \otimes_R N$ involves choices, e.g. for any $n \in N$ one has the map $m \mapsto m \otimes n$. In our situation though, $N = S$ is a ring, so the map $m \mapsto m \otimes 1_S$ seems to be canonical, right?
Comment: @red_trumpet Yes, good point. I'll edit my answer.
Comment: @AlexKruckman Indeed, this is more explict and well detailed. However I am not sure it is a constructive description of $f^*s$ especially for the sheafification step? Maybe we can't go more explicit than that and that we can only describe locally on an affine open  $f^*s$  to be equal to $f^{\#}(s)$, and the sheafification step will remain forever unclear? Thank you for your lights.
Answer: We treat pullbacks of global sections, nothing changes if we restrict to some other open set. We will be using the fact that tensor product of sheaves commutes with restriction to open sets.
Call $f: X \rightarrow Y$ our morphism of schemes. Let $\mathscr{G}$ be an $\mathcal{O}_Y$-module. There is a canonical morphism $\mathscr{G} \rightarrow f_* f^* \mathscr{G}$ of $\mathcal{O}_Y$-modules. This induces a morphism of global sections $\psi:\Gamma(Y,\mathscr{G}) \rightarrow \Gamma(X,f^*\mathscr{G})$. The question is how to describe concretely the image of $s \in \Gamma(Y,\mathscr{G})$ under this map.
It is instructive to begin with the special case $\mathscr{G}=\mathcal{O}_Y$. Consider the following rings $A=\Gamma(Y,\mathcal{O}_Y), \, B = \Gamma(X,f^{-1} \mathcal{O}_Y), \, C = \Gamma(X,\mathcal{O}_X)$. The data of $f$ include a morphism $\mathcal{O}_Y \rightarrow f_* \mathcal{O}_X$. This induces a ring homomorphism $\phi_1: A \rightarrow C$ on global sections. Now the canonical morphism $\mathcal{O}_Y \rightarrow f_* f^{*} \mathcal{O}_Y$ induces a ring homomorphism $\phi_1':A \rightarrow \Gamma(X,f^* \mathscr{G})$. The key is that $\phi_1$ and $\phi_1'$ have the same image. To see this note that we have a ring homomorphism $\phi_2: A \rightarrow B$ induced by $\mathcal{O}_Y \rightarrow f_* f^{-1} \mathcal{O}_Y$. We also have a $\phi_3 : B \rightarrow C$ induced by $f^{-1} \mathcal{O}_Y \rightarrow f^{-1} f_* \mathcal{O}_X \rightarrow \mathcal{O}_X$. Moreover $\phi_1 = \phi_3 \circ \phi_2$. Now $\Gamma(X,f^* \mathcal{O}_Y) = B \otimes_B C$. Finally by checking the definition of $\phi_1'$ we can see that $\phi_1'$ sends $s$ to $\phi_2(s) \otimes 1 \in B \otimes_B C$ which we can identify with $\phi_3(\phi_2(s))=\phi_1(s)$.
For general $\mathscr{G}$ the picture is analogous with the note that $\Gamma(X,f^*\mathscr{G}) = \Gamma(X,f^{-1}\mathscr{G}) \otimes_B C$. That is $\psi$ sends global sections of $\mathscr{G}$ to their canonical image in the $B$-module $\Gamma(X,f^{-1} \mathscr{G})$ and then to their image in the $C$-module $\Gamma(X,f^{-1} \mathscr{G}) \otimes_B C$, the latter being an extended module by scalars according to $\phi_3: B \rightarrow C$.
Answer: Let's give geometric motivation to the definition of the pullback of a section. Suppose $f:X\to Y$ is a morphism of topological manifolds (resp., of smooth manifolds, complex manifolds, or classical algebraic $k$-varieties), where $X$ and $Y$ are regarded as locally ringed spaces with their natural sheaves of real-valued continuous (resp., real-valued smooth, complex-valued holomorphic, $k$-valued regular) functions. Let $U\subset Y$ be open. The pullback of a section $s\in\mathcal{O}_Y(U)$ is defined to be $f^*s=s\circ f|_{f^{-1}(U)}\in\mathcal{O}_X(f^{-1}(U))$. That is, $f^*s$ is the image of $s$ along the map $f^\sharp:\mathcal{O}_Y\to f_*\mathcal{O}_X$. In general:

Definition 1. Let $f:X\to Y$ be a morphism of ringed spaces, let $G$ be a sheaf of $\mathcal{O}_Y$-modules and let $U\subset Y$ be open. The pullback of a section $s\in G(U)$ is the image of $s$ along the unit $G\to f_*f^*G$ of $f^*\dashv f_*$.

In the particular case $G=\mathcal{O}_Y$, one can check that the unit equals $f^\sharp$, so that our general definition is compatible with the particular case above. On the other hand, naturality of the unit implies equivalence with red_trumpet's definition, i.e., the following diagram commutes
$$
\require{AMScd}
\begin{CD}
\mathcal{O}_Y@>>> G\\
@VVV@VVV\\
f_*\mathcal{O}_X=f_*f^*\mathcal{O}_Y@>>> f_*f^*G
\end{CD}
$$
where the left map is $f^\sharp:\mathcal{O}_Y\to f_*\mathcal{O}_X$.
Naturality of the unit also allows to prove the following: if $\varphi:G\to G'$ is a morphism of sheaves of $\mathcal{O}_Y$-modules and $s$ is a local section of $G$, then $f^*(\varphi (s))=(f^*\varphi)(f^*s)$. (Write the naturality square of the unit of $f^*\dashv f_*$ for the morphism $\varphi$ and apply Definition 1.)
Note that from red_trumpet's definition it is easy to show that pullingback sections is a contravariantly functorial operation: if $X\xrightarrow{f}Y\xrightarrow{g}Z$ are morphisms of ringed spaces, $H$ is a sheaf of $\mathcal{O}_Z$-modules and $t\in\mathcal{O}_Z(W)$, by using the isomorphism of functors $(g\circ f)^*\cong f^*\circ g^*$ one gets $(g\circ f)^*(t)=f^*(g^*(t))$.
For a very detailed description of the action of the unit $G\to f_*f^*G$ on sections, see Alex Kruckman's answer (it's the composite at the end of his answer). On the level of stalks, the morphism $G(U)\to f^*G(f^{-1}(U))$ is the unique map such that for all $x\in f^{-1}(U)$ the following diagram commutes:
$$
\require{AMScd}
\begin{CD}
G(U)@>>> f^*G(f^{-1}(U))\\
@VVV@VVV\\
G_{f(x)}@>>>(f^*G)_x\cong G_{f(x)}\otimes_{\mathcal{O}_{Y,f(x)}}\mathcal{O}_{X,x}
\end{CD}
$$
where the bottom map sends $s_{f(x)}$ to $s_{f(x)}\otimes 1\in G_{f(x)}\otimes_{\mathcal{O}_{Y,f(x)}}\mathcal{O}_{X,x}$. That is, $\operatorname{germ}_x^{f^*G}(f^*s)=s_{f(x)}\otimes 1$. To prove this, consider the map $\tilde{s}:\mathcal{O}_Y\to G$ coming from $s\in\Gamma(G,Y)$ and study the map induced by $f^*\tilde{s}$ on stalks at $x\in X$ (and use 0098).

Warning for the differential geometers (there is some terminological overload): given a smooth manifold $M$, denote $C^\infty_M$ to the sheaf of $\mathbb{R}$-algebras of smooth real-valued functions on $M$. Given a vector bundle $\pi:E\to M$, denote $\Sigma_E$ to the sheaf of $C^\infty_M$-modules of smooth sections of $\pi$. A smooth map of smooth manifolds $f:M\to N$ induces the pullback of covector fields $\varphi:\Sigma_{T^*N}\to f_*\Sigma_{T^*M}$ (that in turns induces the pullback of differential $k$-forms $\Omega_N^k\to f_*\Omega_M^k$), which is a morphism of $C_N^\infty$-modules. The map $\varphi$ should not be confused with the unit $\Sigma_{T^*N}\to f_*f^*\Sigma_{T^*N}$. Actually, $\varphi$ equals
$$
\Sigma_{T^*N}
\to f_*f^*\Sigma_{T^*N}
\to f_*\Sigma_{T^*M},
$$
where $f^*\Sigma_{T^*N}\to \Sigma_{T^*M}$ is the adjoint map to $\Sigma_{T^*N}\to f_*\Sigma_{T^*M}$.


Image auto width in CSS for all browsers?

Question: I have my img tag defined with some CSS as:
img
{
  width: auto !important;
  height: auto !important;
  max-width: 100%;
}

It works just fine in some of the Mobile Browsers I have tried, as well as Google Chrome on a desktop. However, it does not seem to work in IE or FireFox. By that, I mean, as you resize the window. Take a look at a sandbox site I am working on:  http://terraninstitute.com. I have the image on the home page intentionally set to be a huge picture. Also navigate to the Information (main menu) then Newcomers (side menu) and notice the map image at the bottom.  On my Droid (and a few other devices I can get my hands on) as well as in Google Chrome this looks pretty good. In IE and FireFox, not so much.
Am I missing something? Conflicting style definitions? I am not finding them as of yet if it is.
TIA
Comment: Why not just set width to 100%?
Answer: You're declaring the width of your images multiple times in your document unnecessarily and declaring a max-width the wrong way. Max-width is supposed to define a max size/boundary for your images (600px, for example), if you declare max-width:100% in conjunction with width:100%, you're not really doing anything with that declaration as the images will expand 100% as it is.
Remove the width declarations from your CSS in the following lines:
line 116: delete the img declaration all together, it is not needed.
line 365: remove the max-width:100% and height:auto; attribute as they are not needed (and if you can delete the declaration all together as you can declare that elsewhere)
line 121: Now just stick with this one and just add the following:
img {
   height: auto;
   width:100%;
}

Comment: thanks.  I cleaned up all of the img tags in the various CSS files leaving the one in the inuit.css file.  It was interesting playing with width vs max-width and you were right.  width was all I needed.  So many wrong turns when I googled for it.  And inuit.css was setup for CSS3 and auto scaling so I just *assumed* it should always work.  When I first started it did work (I thought).  Further in now and adding too many CSS definitions and finally things started going south.  Thanks!
Answer: Bootstrap solution for responsive images:
img {
  /* Responsive images (ensure images don't scale beyond their parents) */

  /* Part 1: Set a maxium relative to the parent */
  max-width: 100%;

  /* IE7-8 need help adjusting responsive images */
  width: auto\9;

  /* Part 2: Scale the height according to the width, otherwise you get stretching */
  height: auto;

  vertical-align: middle;
  border: 0;
  -ms-interpolation-mode: bicubic;
}

Answer: Don't use !important
Make your CSS more speficic:
body img {
  width: auto;
  height: auto;
  max-width: 100%;
}

Comment: To whoever downvoted Diodeus: he wasn't suggesting this as a solution.  He was pointed out a good CSS practice.
Answer: in style.css line line 116, 212 and in inuit.css line 365. Remove all those.
img{
    height: auto;
    max-width: 100%;
}

Thats all you need.


Individual CCK Imagefield promote-to-front

Question: At the moment, my view creates a gallery of images by fetching all images across multiple node types. What I need is a way to indicate which images should be used in the gallery. So, instead of my view simply displaying all images, I want the client to indicate which image they want to "promote" to the gallery.
Can anyone help please?
Answer: Here is another option, that will work for sure.  May need to do a bit of reconfiguring with your current imagefields.

Download latest version of CCK 3.x.
Install multigroups module (currently this module is stable)

On the selected content type:

Add on/off checkbox featured field (set limit to 1)
Add Image field (set limit to 1)
Encapsulate theses 2 fields in a multigroup (set limit to unlimited)
Create view that will filter on featured field
Answer: So, in my example scenario, I needed to indicate which individual image(s) are to be part of my gallery.
My node/1 contains a cck imagefield with unlimited uploads and currently holds 5 images. Of those 5, I want 2 of the images to be part of a gallery on a seperate page.
So, in the global settings of cck imagefield configuration, there is the 'list field' area with 2 radio button options. By default it is set to 'disabled'. I enabled it.
Now, in the node there will be a check box next to each one of the image uploads. I check the images I want in my gallery.
In the view that generates my gallery, in the filter area, each imagefield exposes a 'list' option for your cck imagefields. It's options are 'true' of 'false'. I make sure it is set to true.
This way I now how granular control over my image displays.
I trust that will help someone out there!
Answer: Probably you have to add checkbox field to all content types for indicate image to promote.
Comment: As my nodes have multiple images, the checkbox will not target individual images but the entire node itself ???
Comment: I am trying to indicate individual imagefields as promoted-to-front or display this image in the gallery. Not promote the entire node and get all it's images
Answer: You have 2 options
Option 1:
In your view, you add a filter for
<code>Node: Promoted to front page
Whether or not the node is promoted to the front page.
</code>
Set value is 1.  Now all nodes promoted to the front page will be in the gallery
Option 2:
You create a cck field that is a single on/off checkbox.  Featured Image
Go back to your view and add a filter for this new cck field.
Comment: But isn't the on/off checkbox referring to the node rather than the imagefield ?? My nodes have multiple images
Comment: yes, it would feature all images attached to that node
Comment: Did some research, and found 2 threads and 1 module (that doesn't do it but should).  http://drupal.org/node/213222, Imagefield Extended - http://drupal.org/project/imagefield_extended. Its issue to include a views filter - http://drupal.org/node/107577
Comment: I did some more research, and Imagefield Extended will need to be rewritten to do this.   The data is store in a serialized column in the database.  Serialized data cannot be sorted on.  See this related post for more information http://drupal.org/node/1128270.  Once there is a Drupal 7 release, this should be available for Drupal 7 as the data is no longer serialized.
Comment: Thanks again iStryker. I tried the Imagefield Extended and you are right - it can not be sorted on which is why I guess there is no views filter support. So I had to abandon it. However, I found a solution already in Drupal which I will post as an answer for anyone interested ...


SQL Server Functions Passed into SQL

Question: Help on this would be appreciated.
Scenario:
I want to create a scenario to handle nulls with specific default values. The table below will have the default values:
CREATE TABLE dbo.NullhandleMetadata
(
ColumnName varchar(50)
, Defaultvalue varchar(50)
, DataType (varchar(50)
)

The table definition:
ColumnName  Defaultvalue    Dataype
 columnA    Unknown         varchar(50)
 ColumnB    999             int

I want to use a function to be called from within the SELECT statements to inject the defalut value if the column is null.
The function is as below:
CREATE FUNCTION dbo.Default_handle (@AttValue varchar(200)) RETURNS varchar(200)

BEGIN
DECLARE @value varchar(200)
    SELECT
        @value= Defaultvalue
    FROM
        dbo.NullhandleMetadata
    WHERE ColumnName = @AttValue

  RETURN @value
END

Below is an example of the SELECT statement with the the function:
SELECT
    ISNULL(columnA, dbo.Default_handle(@value)) AS ColumnAData

FROM source_table

I am not able to get this function to work - any help would be appreciated
Is there a way for the same function to deal with different data types and the return value to also return a different data type?
So if the column is integer then it returns and integer or if it is a string/varchar field then to return a varchar?
Comment: So you have a table to hold the default value for every table and column if the value is null? This sounds horrific. And keep in mind that scalar functions are horribly inefficient. Are you going to be wrapping every single column in your select statements with ISNULL(MyColumn.Value, Default_handle('MyColumn')). Something sounds very wrong with this concept.
Comment: Columns don't have defaults unless you create default constraints on them. So, seems like a far easier and reliable thing to do would be to create default constraints, using the value you want to use. Update any existing null values and then alter the column definition to NOT NULL.
Comment: Do you meant to use a function instead of a join?
Comment: "not able to get this function to work" -- what's that supposed to mean?
Comment: Apologise @Jason - I did not mean SQL defaults.
Comment: @SeanLange - The table will keep a list of all the columns and custom default values can be assigned to each column. This is not my preferred approach at all but I have been asked to develop this.
Comment: @clementakis - Yes there is no join - the proposed solution will inject the 'custom' values for the null fields into a SELECT statement.
Comment: I don't understand what is @value in your select prototype... I guess it should be a source_table column. ?
Comment: @clementakis - the [@]value is the variable used to store the value of the column [DefaultValue] returned. It is a column for the table dbo.NullhandleMetadata
Answer: See what you think of this...
USE tempdb;
GO

CREATE TABLE dbo.TestingTable (
    id INT NOT NULL IDENTITY(1,1),
    Col1 DATETIME,
    Col2 VARCHAR(10),
    Col3 INT,
    Col4 DATE,
    Col5 TIME(0)
);
INSERT dbo.TestingTable (Col1, Col2, Col3, Col4, Col5) VALUES
    (GETDATE(), NULL,    NULL, '2017-11-01', NULL),
    (NULL,      NULL,    NULL, '2017-11-01', '12:54:32'),
    (NULL,      NULL,    NULL, '2017-11-01', '12:54:32'),
    (NULL,      'Hello', NULL, NULL,         '12:54:32'),
    (NULL,      'Hello', NULL, NULL,         '12:54:32'),
    (GETDATE(), 'Hello', NULL, NULL,         '12:54:32'),
    (GETDATE(), 'Hello', 2587, NULL,         NULL),
    (GETDATE(), 'Hello', 2588, NULL,         NULL),
    (GETDATE(), 'Hello', 2589, '2017-11-01', NULL),
    (GETDATE(), NULL,    2590, '2017-11-01', NULL),
    (GETDATE(), NULL,    NULL, '2017-11-01', NULL);

-- create the dbo.NullhandleMetadata and use sys.columns to capture the object_id & column_ids.
USE tempdb;
GO

CREATE TABLE dbo.NullhandleMetadata (
    ObjectID INT,
    ColumnName sysname,
    ColumnID INT,
    Defaultvalue VARCHAR(50)
    );

INSERT dbo.NullhandleMetadata (ObjectID, ColumnName, ColumnID, Defaultvalue)
SELECT
    c.object_id,
    c.name,
    c.column_id,
    Defaultvalue = CASE  c.name
                        WHEN 'id  ' THEN '0'
                        WHEN 'Col1' THEN 'GETDATE()'
                        WHEN 'Col2' THEN ''''''
                        WHEN 'Col3' THEN '0'
                        WHEN 'Col4' THEN 'GETDATE()'
                        WHEN 'Col5' THEN  '00:00:00'
                    END
FROM
    sys.columns c
WHERE
    c.object_id = OBJECT_ID('tempdb.dbo.TestingTable', 'U');

SELECT * FROM dbo.NullhandleMetadata;

-- create the function dbo.tfn_NullhandleMetadata (see function comments)
CREATE FUNCTION dbo.tfn_NullhandleMetadata
/* ====================================================
Pulls default values from dbo.NullhandleMetadata
parameters will accept either the object_id & column_id as INT values
or as the actual names for easier use.
==================================================== */
(
    @ObjectID sysname,
    @Column sysname
)
RETURNS TABLE WITH SCHEMABINDING AS
RETURN
    SELECT
        nm.Defaultvalue
    FROM
        dbo.NullhandleMetadata nm
    WHERE
        nm.ObjectID IN (OBJECT_ID(@ObjectID, 'U'), TRY_CAST(@ObjectID AS INT))
        AND (
                nm.ColumnName = @Column
                OR
                nm.ColumnID = TRY_CAST(@Column AS INT)
            );
GO

Here are two different use cases for the function. Check the execution plans of both before choosing which syntax to use...
-- Use case 1...
-- cleaner syntax but slightly higher plan cost.
UPDATE tt SET
    tt.Col3 = nm.Defaultvalue
FROM
    dbo.TestingTable tt
    CROSS APPLY dbo.tfn_NullhandleMetadata('tempdb.dbo.TestingTable', 'col3') nm
WHERE
    tt.Col3 IS NULL;

-- Use case 2...
-- a bit more cumbersome but lower plan cost.
DECLARE @Defailt VARCHAR(50);
SELECT
    @Defailt = nm.Defaultvalue
FROM
    dbo.tfn_NullhandleMetadata('tempdb.dbo.TestingTable', 'col3') nm;

UPDATE tt SET
    tt.Col3 = @Defailt
FROM
    dbo.TestingTable tt
WHERE
    tt.Col3 IS NULL;

Results of running against Col3...
id          Col1                    Col2       Col3        Col4       Col5
----------- ----------------------- ---------- ----------- ---------- ----------------
1           2017-09-28 14:52:39.280 NULL       0           2017-11-01 NULL
2           NULL                    NULL       0           2017-11-01 12:54:32
3           NULL                    NULL       0           2017-11-01 12:54:32
4           NULL                    Hello      0           NULL       12:54:32
5           NULL                    Hello      0           NULL       12:54:32
6           2017-09-28 14:52:39.280 Hello      0           NULL       12:54:32
7           2017-09-28 14:52:39.280 Hello      2587        NULL       NULL
8           2017-09-28 14:52:39.280 Hello      2588        NULL       NULL
9           2017-09-28 14:52:39.280 Hello      2589        2017-11-01 NULL
10          2017-09-28 14:52:39.280 NULL       2590        2017-11-01 NULL
11          2017-09-28 14:52:39.280 NULL       0           2017-11-01 NULL

Comment: A Long. - your solution was so much better but they want it to be driven from the metadata table and not from system tables :(
Comment: @Aarion - That solution isn't actually dependent on the system tables. You'll notice that the function itself does't look at sys.columns at all. I simply "borrowed" the object_id & column_id as a matter of convenience and to help guarantee uniqueness and accuracy... It also provides the convenience of using the OBJECT_ID() function allowing search by name or id.
Answer: Maybe this will work :
   SELECT ISNULL(columnA, A.Defaultvalue) AS ColumnAData
       , ISNULL(columnB, B.Defaultvalue) AS ColumnBData
       , ISNULL(columnC, C.Defaultvalue) AS ColumnCData
    FROM source_table AS S
    LEFT JOIN dbo.NullhandleMetadata AS A ON (A.ColumnName = 'columnA')
    LEFT JOIN dbo.NullhandleMetadata AS B ON (B.ColumnName = 'columnB')
    LEFT JOIN dbo.NullhandleMetadata AS C ON (C.ColumnName = 'columnC')

It could still be an issue with implict conversions...
Comment: Thank you for this, but I need the select statement IS NULL condition when true populated via the SQL Function 'dbo.Default_handle'

SQL Server Functions Passed into SQL


Selecting one date at a time from a resampled dataframe

Question: I have a dataframe of tick data, which i have resampled into minute data.  doing a vanilla
df.resample('1Min').ohlc().fillna(method='ffill')

super easy.
I now need to iterate over that resampled dataframe each day at a time, but i cant figure out the best way to do it.
ive tried taking my 1min resampled dataframe and then resampling that for "1D" and then converting that to a list to iterate over and filter, but that gives me a list of:
Timestamp('2011-09-13 00:00:00', freq='D')

objects, and it wont let me slice a dataframe based on that.
this seems like it would be something easy, but i just cant find the answer. thanks-
#sample data_1m dataframe
data_1m.head()

                     open  high  low  close
timestamp
2011-09-13 13:53:00   5.8   6.0  5.8    6.0
2011-09-13 13:54:00   5.8   6.0  5.8    6.0
2011-09-13 13:55:00   5.8   6.0  5.8    6.0
2011-09-13 13:56:00   5.8   6.0  5.8    6.0
2011-09-13 13:57:00   5.8   6.0  5.8    6.0
...

#i want to get everything for date 2011-09-13 im trying

days_in_df = data_1m.resample('1D').ohlc().fillna(method='ffill').index.to_list()

data_1m.loc[days_in_df[0]]

KeyError: Timestamp('2011-09-13 00:00:00', freq='D')

Comment: You may want to post an expected result. I have three things that I think you could be asking, other Stack Overflow user could have more as well.
Comment: what happens if you use the same dates? `df.loc['2011-09-13':'2011-09-13']` Does that fit your needs?
Comment: can you add a sample of your data frame? also, can you add a column of the minute of the timestamp, and the day of the timestamp and then loop on the `day` column?
Comment: ok, i have done it. thanks.
Comment: so, you want to pass a date to `loc` and return all of the values that happen during that day, yes?
Comment: Possible duplicate of [Filtering Pandas DataFrames on dates](https://stackoverflow.com/questions/22898824/filtering-pandas-dataframes-on-dates). I would look at this post. Just pass whatever dates you want through `loc`
Comment: i dont really care how it gets done i just need to iterate over the dates one at a time, this was my best guess, it can be some other way, i just need to be able to basically break this df up into individual date sub dataframes
Comment: i saw that other post, i want a single date at a time, not a range of dates.  that post (as i indicated, doesnt work)
Answer: Here's my two cents. I don't resample the data so much as adding another index level to the frame:
data_1m = data_1m.reset_index()
data_1m['date'] = data_1m['timestamp'].astype('datetime64[D]')
data_1m = data_1m.set_index(['date', 'timestamp'])

And to select an entire day:
data_1m.loc['2011-09-13']


How is holding time $H_i$ defined in continuous-time Markov chain?

Question: I'm reading lecture note Continuous-Time Markov Chains:

A stochastic process $\{X(t): t \geq 0\}$ on $(\Omega, \mathcal F, \mathbb P)$ with discrete state space $\mathcal{S}$ is called a continuous-time Markvov chain if for all $(t,s,i,j) \in \mathbb R_+^2 \times \mathcal{S}^2$, we have
$$
\mathbb P[X(s+t)=j | X(s)=i,\{X(u): 0 \leq u<s\}]=\mathbb P[X(s+t)=j | X(s)=i]=P_{i j}(t)
$$
Assume $\mathcal{S}=\mathbb{Z}=\{\cdots,-2,-1,0,1,2, \cdots\}$. Suppose now that whenever a chain enters state $i \in \mathcal{S}$, independent of the past, the length of time spent in state $i$ is a continuous, strictly positive (and proper) random variable $H_{i}$ called the holding time in state $i$.

Could you please explain how random variable $H_i:\Omega \to \mathbb R$ is defined, i.e., what is $H_i (\omega)$?
Answer: I've found the answer and posted here to close this question.

The sequence of $\textit{jump times}$ $(T_n)_{n \in \mathbb N}$ is defined recursively by $T_0 = 0$ and $T_{n+1}=\inf \left\{t \ge T_n \mid X_t \neq X_{T_n}\right\}$ with $\inf \emptyset = \infty$. Notice that $T_n = \infty$ implies $T_{n + k} = \infty$ for all $k \in \mathbb N$. The times between jump times $T_{n+1} - T_n$ are called $\textit{holding times}$. The $\textit{embedded chain}$ $(Y_n)_{n \in \mathbb N}$ is given by
$$Y_{n}=\left\{\begin{array}{ll}
X_{T_n} & \text{if} \quad T_{n}<\infty \\
\Delta & \text{if} \quad T_{n}=\infty
\end{array} \quad \text{for all} \quad n \in \mathbb{N}\right.$$
where $\Delta$ is an arbitrary element not in $V$.


partial class template specialization with parameter pack not in last position

Question: The following code comes from an answer to another question, and it works. I tested it with recent versions of GCC, Clang, ICC and MSVC. But I don't understand which part of the standard allows this kind of construction, could you please help?
template<class F, class Alloc> class C; //undefined
template<class T, class... Args, class Alloc>
class C<T(Args...), Alloc> {
    // implementation
};

The C++ standard provides constraints on how parameter packs can be used in primary class templates, but I can't figure out how these apply to partial specializations.
So my question is: where are the rules of placement for parameter packs in the context of partial template specialization defined in the C++ standard?
Comment: @Drealmer `T(Args...)` is not a pack expansion argument. `Args...` would have been a pack expansion argument
Comment: You mean like [here](http://eel.is/c++draft/temp.class.spec#8)?
Comment: What's your actual question? How does partial specialization work?
Comment: Thanks, that looks indeed like right on topic. But then it says "If an argument is a pack expansion ([temp.variadic]), it shall be the last argument in the template argument list." which seems to contradict the example, or am I missing something?
Comment: alright, this is starting to make sense, thanks guys! but then does this say something about the placement of parameter packs inside the parameter list of a specialization? (since the part about the argument list is indeed not a problem)
Answer: While not a formal and technically correct way of understanding how partial specialization works (check out the cppreference page for a more technical explanation), you can think of the code snippet you posted in terms of "pattern matching" (just as an intuition):
// `C` is a template class that takes two types as template parameters.
template<class F, class Alloc> class C;

// I'm going to specialize `C` so that:
// * The first type will be a function signature type, where the return
//   type is going to be matched by `T` and the argument types will be
//   matched by `Args...`.
// * The second type will be an user-provided `Alloc` typename.
template<class T, class... Args, class Alloc>
class C<T(Args...), Alloc> { /* ... */ };

Let's say I instantiate C like this:
using My_C = C<int(float, char), std::allocator<int>>;
My_C c;

Very roughly speaking, My_C will "match" the C<T(Args...), Alloc> partial template specialization as follows:
int ( float, char )     std::allocator<int>
^^^   ^^^^^^^^^^^       ^^^^^^^^^^^^^^^^^^^
 T  (    Args...  )           Alloc
^^^^^^^^^^^^^^^^^^^     ^^^^^^^^^^^^^^^^^^^
         F                    Alloc

int is "matched" by T, and float, char are "matched" by Args.... There's no reason why Args... should be restricted to be the last template parameter of C.
As you can see, it is not required for the Args... parameter pack to be the last template argument, as we're just using it to "provide a name" for the argument type list in the passed function signature.
Comment: Mind explaining the downvote? I interpreted the question as *"I thought that parameter packs need to be at the end of parameter lists and I do not understand why this code works"*, and tried to give a non-formal interpretation of the role of `Args...` in the posted code snippet.
Comment: I don't understand the downvote either, it isn't mine.
Comment: We don't really have "pattern matching" in C++. Picking a partial specialization involves template deduction and partial ordering.
Comment: @Barry: I am aware of that, I used the "pattern matching" term to provide an intuition to easily understand the role of `Args...` in the snippet. I'll make it more clear in the answer that "pattern matching" is not happening on a technical level.


Linear Regression vs. SARIMAX with Exogenous Variable: Coefficient Interpretation

Question: I have data concerning, say, ice cream sales and wish to predict future sales and also, to quantify the relationship between temperature and sales. The data has daily seasonality.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pmdarima as pm
import statsmodels.api as sm

np.random.seed(1)

def generate_synthetic_data(n_days=10, sd=5):
    day_time, sales, temperature = [], [], []
    for i in range(n_days):
        day_time.extend([str(i)+'_0000', str(i)+'_0400', str(i)+'_0800', str(i)+'_1200', str(i)+'_1600', str(i)+'_2000'])
        #time.extend(['0000', '0400', '0800', '1200', '1600', '2000'])
        sales.extend([260, 322, 530, 547, 379, 234] + np.random.normal(0,sd*10,6))
        temperature.extend([15, 18, 25, 22, 21, 12] + np.random.normal(0,sd,6))
    df = pd.DataFrame({'day_time': day_time, 'sales': sales, 'temperature': temperature})
    return df

n_days=50
df = generate_synthetic_data(n_days)

A quick plot of the first 5 days shows what the data looks like:

Clearly, sales and temperature are related. I fit a simple linear regression model.
slope, intercept, r_value, p_value, std_err = stats.linregress(df['temperature'], df['sales'])
# Slope is 11.8.

So I conclude that a unit increase in temperature results in an increase of 11.8 in ice cream sales. So far so good.
Now, I proceed to forecasting future ice cream sales. I split the data into train-test sets and fit a SARIMAX model, with temperature as an exogenous variable.
train_df = df.iloc[:294]
test_df = df.iloc[294:]
model = pm.auto_arima(y=train_df['sales'], X=train_df['temperature'].to_frame(), seasonal=True, m=6)

The output of this model is as follows:

The predictions on the test set look good, but this is not what I'm concerned about. The coefficient for temperature has now drastically changed (from 11.8 to 0.85). It wouldn't make much sense to change my interpretation now to a unit increase in temperature resulting in an additional 0.85 ice cream sales. Is this huge difference down to:

Collinearity between sales and temperature?
Misinterpreting ARIMAX coefficients, and I'd be better off with a linear regression with AR(I)MA errors? If this is the case, is there a package that implements this in Python? Or should I turn to R?
Both of the above?
Answer: Reason 1 makes sense to me. Your first model has the following format:
$y_t = \beta_0 + \theta * x_t$
And if I am interpreting correctly your SARIMAX model format is:
$y_t = \beta_0 + sazon_t + a_1*y_{t-1} + \theta * x_t$
So having same $\theta$ values on both models your require the term $sazon_t + a_1*y_{t-1}$ to be estimated as 0 (zero) and that doesn't look like the case, as your series clearly has sazonality.
You could validate the sarimax coeficients understanding estimating a model without sazonality and check if coeficient to temperature approaches the previous model one.
Other things to be aware of:

pvalues indicate if the coefficient impact is being confidently estimated as different than zero by the model. The SARIMAX model seems to be estimating intercept, temperature and AR[1] as zero.
Using the temperature model as exogenous variable on the model requires that you have this variable value on prediction time. Just be sure this is the case to use this.


Git branching strategy for our Project

Question: I'm new to git and currently exploring the same, I would like to take some suggestions from the git experts here !
In our organization we are in the process of migrating to git from RTC. We have ~20 releases shared as a tarball in a ftp site and that has to be imported into git.
In addition to that, a latest delta changes is also shared on top of the 20 release tarballs. Kindly let me know how to go forward with a best strategy.(like how to create branches or tags that suits the best to our case)
Also we have decided to go with gitlab latest version
Thanks in advance for the help :)
Answer: Look at this doc for options on git branching strategies. It is related to Atlassian Stash, but can be applied to other tools.
There are several options, and you can try them all and see which works best for you.
I hope this helps.
Answer: The simple way would be to create an empty repo, unpack the first tarball there and commit. Then remove all working files, unpack the next tarball and commit. Et cetera until you've processed all tarballs.
Have a look at git-weave. It can synthesize a repository from a sequence of tarballs.

Git branching strategy for our Project


$\sigma$- compact clopen subgroup.

Question: I am given $G$ locally compact group, and I want to show that there exists a clopen subgroup $H$ of $G$ that is $\sigma$-compact.
So here's what I did so far:
for $e \in U$, where $U$ is a nbhd of the identity element we know that $x \in x\bar{U}$, and $x\bar{U}$ is compact in $G$, now take some finite open cover of this set,$\cup_{i=1}^{n} V_i$, again this set is open nbhd of $x$ so again its closure is compact, and its closure equals the union of $\bar{V_i}$, now I am kind of stuck, I wish I could take this set as the clopen subgroup, but I don't think that I know that it's closed under multiplication, right?
Answer: Hint: Take a compact symmetric neighborhood $K$ of the identity and consider the $\sigma$-compact subgroup $H = \bigcup_{n =1}^{\infty} K^n$.

Prove that $H$ is an open subgroup.
Prove that an open sugroup of a topological group is closed — its complement is a union of (open) cosets.

As additional exercises I suggest:

Prove that the connected component of the identity is a clopen subgroup. (Connected components are open and open subgroups are closed)
Prove that a connected locally compact group $G$ is $\sigma$-compact.
Comment: Ah, thanks. it does look closed under multiplication.
Comment: Yes, and since $K^{-1} = K$ it is closed under inversion.
Comment: I have another question, which I think uses the same trick, and I don't want to open another post just for it. I am given $G$ a topological group, and I want to show that every compact clopen nbhd of $e$ contains a compact clopen subgroup, $H<G$. Now, here I think I just need to take $K$ which is compact and clopen nbhd of $e$ (which I can always take as being symmetric), and then take $H=\cap_{n=1}^{\infty}K^n$, obviously it's contained in $K$, and it's a subgroup, and it's compact as a countable intersection of compact sets, is this right for this question?
Comment: You're right that the idea is very similar. However, your idea doesn't seem to give an *open* subgroup in general. See e.g. [Hewitt-Ross, Theorem (7.5), p.61](http://books.google.com/books?id=uf11K1wXEYUC&pg=PA61) for the standard argument.

$\sigma$- compact clopen subgroup.


How do I make the link clickable in the rendered template in Django?

Question: I am doing a Django project, named PhoneRadar. I have a template page (details.html), which looks like this:

As you can see, the link is not clickable. How can I make it clickable?
Here are my codes of models.py:
from django.db import models
from django.template.defaultfilters import slugify

# Create your models here.
class Brand(models.Model):
    brand_name = models.CharField(max_length=100)
    origin = models.CharField(max_length=100)
    manufacturing_since = models.CharField(max_length=100, null=True, blank=True)
    slug = models.SlugField(max_length=150, null=True, blank=True)

    def __str__(self):
        return self.brand_name

    def save(self, *args, **kwargs):
        self.slug = slugify(self.brand_name)
        super().save(*args, **kwargs)

class PhoneModel(models.Model):
    brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
    model_name = models.CharField(max_length=100)
    launch_date = models.CharField(max_length=100)
    platform = models.CharField(max_length=100)
    slug = models.SlugField(max_length=150, null=True, blank=True)

    def __str__(self):
        return self.model_name

    def save(self, *args, **kwargs):
        self.slug = slugify(self.model_name)
        super().save(*args, **kwargs)

class Review(models.Model):
    phone_model = models.ManyToManyField(PhoneModel, related_name='reviews')
    review_article = models.TextField()
    date_published = models.DateField(auto_now=True)
    # slug = models.SlugField(max_length=150, null=True, blank=True)
    link = models.TextField(max_length=150, null=True, blank=True)

    def __str__(self):
        return self.review_article

Here are my codes of details.html located inside templates folder:
{% extends 'PhoneReview/base.html' %}
{% load static %}

<link rel="stylesheet" type="text/css" href="{% static "css/style.css" %}">

{% block title%}Details{% endblock %}

{% block content %}

This is the Details Page

Review:

{{ review.review_article }}

News Link:

{{ review.link }}

{% endblock %}

How can I fix this issue?
Answer: Because you use p but need a for clickable link. More info link.

News Link:

{{ review.link }}

Answer: In HTML, paragraph tag doesn't directly support link, instead you should use the a tag as shown below
{{ review.link }}


About the Bonferroni correction

Question: I have 50 variables in my dataset. I have correlated each variable against each variable, thus I have $49·50/2 = 1225$ unique correlations when no variable is tested against itself. Now, suppose I want to correct the statistics for multiple comparison, and that for some reason (well, just for learning now) I want to use the Bonferroni correction. Let the threshold for significance be $\alpha = 0.05$. Is the corrected threshold $0.05/1200$ (where 1225 is number of tests) or $0.05/49$ (because each variable was correlated with 49 other)?
Comment: @Wrzlprmft: There are multiplicity corrections that are designed for known dependency (in particular due to the design, like here) or estimate the dependency in order to be less conservative.
Comment: Note that all corrections for multiple testing are overly conservative in your case as your tests are not fully independent: If $X$ is strongly correlated to $Y$ and $Y$ is strongly correlated to $Z$ then it is very likely that $X$ is also strongly correlated to $Z$.
Answer: With Bonferroni correction the divisor is equal to the number of tests you carry out, dependent or independent
It helps to understand the purpose of the Bonferroni correction. You are testing correlations between variables. Let's assume your null hypothesis for any two variables is that the correlation is 0. (Any null hypothesis will suffice.) Your significance threshold is $\alpha = 0.05$. In other words, there is a 5% chance that you will reject the null hypothesis erroneously. This is known as a type 1 error or a  false positive.
Now, let's say you did 100 tests, all at the $\alpha = 0.05$ level. You would expect $5%$ of these to give a false positive ( ie to fail by chance alone). If you do 1225 tests then you expect 5% = ~61 false positives. This is quite a lot!  Bonferroni offers a level of protection against this scenario. You can think of it as familywise protection as it offers a family of tests a single level of protection against even one false positive. Instead of testing with an $\alpha = 0.05$ threshold, you perform each test at the $\alpha =  0.5/1225 = ~.0000408$ threshold. In this case Bonferroni reduces the probability of even one false positive amongst all tests to an $\alpha = 0.05$ threshold.
Bonferonni is very conservative and there exists several improvements. My favourite is False Discovery Rate
Comment: Why the downvote? If there's an issue with the answer it'd be helpful to point it out - I can't see one
Answer: If you are correcting for Multiple testing, you need to take into account the number of tests performed.
As you stated you don't need to correct for all $50*50 = 2500$ tests since $corr(X,X)=1$ and $corr(X,Y)=corr(Y,X)$. You need to correct for  $(n-1)*n/2 = (49*50)/2 = 1225$ (note not $1200$ as you specified in question) tests.
Note if you are in a regression setting, checking for correlatin between your explanatory variables is basically testing for multicollinearity. There are more standard methods of looking into this problem that you should read up on (e.g. Variance inflation factors, Farrar–Glauber test).
Answer: Firstly, use the modified Holm-Bonferroni method instead of the plain Bonferroni method.
Regarding the 49 vs 1200 question. In my opinion, if you want to make statistical assumptions on all the variables together, then you have to use the 1200 number. The number 49 would come in order if you wanted to test whether a single variable is somehow different from the others.
But still, both numbers are very high and I think you will have problems with the corrected α values as they will be ridiculously small.
Maybe a change of method is in order, as pairwise comparison is definitely not the best option here. Maybe some other method, which could test on all the variables altogether - like ANOVA or Friedman test (but I doubt that those two will be relevant for your case).
Comment: @HorstGrünbusch I am pretty sure it's not the case. Incidentally, if it is, all the implementations I know (e.g. R's `p.adjust`) are wrong because you certainly don't need to specify any order.
Comment: You're right, it's ordering on its own by the $p$-values. So you don't have to prespecify them.
Answer: if you are trying to find out whether a p-value corresponded to a correlation coefficient is significant or not using Bonferroni correction, it will be right to use the following correction:
0.05/((2500-50)/2) = 0.05/1225
Comment: Although that is the right formula for correcting $1225$ nearly-independent tests, the huge degree of interdependence among the correlations of $50$ variables would suggest including a strong cautionary note against relying on the Bonferroni method in this situation (except perhaps in interpreting its result as a lower bound on the correct p-value).

About the Bonferroni correction


Registration and Webforms

Question: I'm using Drupal 7 - is there any way to view and output the users who've registered with site in a similar way to how Webforms displays submissions and allows you to download and import into Excel?
I can export the users table from the database as a CSV file but I was hoping to find something that would be integrated into the Drupal admin ui (similar to the way Webforms is).
Thank you for any guidance you can provide.
Answer: There are two modules that you could use, the 'Database Administration' and 'Views Excel Export' These are still in alpha and dev for drupal 7 but might worth a try.

Registration and Webforms


Integrating Spring MVC and JSF 2.1

Question: I have a huge read-only RESTFUL application built in Spring MVC and Hibernate-Search that marshals some data to json and xml to be consumed for a mobile app and a monitoring application.
Our urls are pretty simple, we have this:

server:port/entity/id/?marshalling=json

or

server:port/entity/id/?marshalling=xml

and sometimes

server:port/entity/id/?marshalling=something&filterProp=entity.prop&ordering=desc

Now I have a requirement to make a presentation layer for this using JSF 2.1 and adding some user admin capabilities and I was thinking in something like this:

server:port/entity/id/?marshall=html

Or omitting entirely the marshall request parameter.
Now. As far as I know, you can only couple JSF 2.1 and Spring Web Flow, not Spring MVC directly. Anyone know how can I accomplish this requirement?
Answer: Both Spring MVC and JSF work on seperate servlets that can only be mapped to a context that doesn't conflict with another servlet, so this cannot be done easily and if you succeed then it is basically an enormous hack.
If I absolutely had to do it this way (which I wouldn't), then I would probably use the marshall servlet to clone my JSF requests, send that request to my FacesServlet using a WebClient, and then copying the returning WebClient response into my marshall servlet response and return that.  This would be transparent to the user but highly messy and potentially insecure.
The best way to implement a seperate web based presentation layer would be for requests to your presentation layer to be mapped to the FacesServlet so that your RESTFUL web services do not get called directly.
Actor - > /admin/page.jsf -> FacesServlet -> JSF View - >
JSF Controller -> Business Logic Layer -> RESTFUL Web Services
In this way your RESTFUL web services can return XML or JSON data and act as your DAO, your Business Logic layer can perform additional logic on that.
Comment: I am a bit preoccupied about performance in this topic... any useful tip about it?
Comment: @mael If you are concerned about the performance of this approach then the other option is to run the RESTFUL web services as well as the JSF front end on the same instance, which will allow you to bypass the service layer and go directly to the Business Logic layer of the RESTFUL web services.  This would reduce the overhead of making those additional web requests on the JSF server side.

Integrating Spring MVC and JSF 2.1


with SSH only: reverse tunnel web access via ssh SOCKS proxy

Question: Once in a while i have to connect to a server where access is highly restricted. Only inbound SSH via VPN is allowed by the DMZ firewall.Outbound HTTP connections are blocked.
I'm looking for an easy way to tunnel web access through my SSH session, so i can install updates and software via yum / apt-get.
Ideally, i would like to avoid installing additional software/services in the protected area.
What do you do in such a situation?
SSH has the -D <port> SOCKS proxy option. But unfortunately it's only one-way from client to server and there is no reverse option.
Answer: I finally managed to accomplish this with ssh only:

start a local SOCKS proxy on your client machine (using ssh -D) EDIT: not necessary with SSH>7.6
connect to remote server and setup a reverse port forwarding (ssh -R) to your local SOCKS proxy
configure the server software to use the forwarded proxy

1. Start local socks proxy in the background
EDIT SSH>7.6 allow a simpler syntax to start the proxy. Skip this and continue with step 2!
Connect to localhost via SSH and open SOCKS proxy on port 54321.
$ ssh -f -N -D 54321 localhost

-f runs SSH in the background.
Note: If you close the terminal where you started the command, the proxy process will be killed. Also remember to clean up after yourself by either closing the terminal window when you are done or by killing the process yourself!
2. connect to remote server and setup reverse port forwarding
Bind remote port 6666 to local port 54321. This makes your local socks proxy available to the remote site on port 6666.
$ ssh root@target -R6666:localhost:54321

EDIT SSH>7.6 allows a simpler syntax to start the proxy! Step 1 is not needed then:
$ ssh root@target -R6666:localhost

3. configure the server software to use the forwarded proxy
Just configure yum, apt, curl, wget or any other tool that supports SOCKS to use the proxy 127.0.0.1:6666.
Voilá! Happy tunneling!

4. optional: install proxychains to make things easy
proxychains installed on the target server enables any software to use the forwarded SOCKS proxy (even telnet). It uses a LD_PRELOAD trick to redirect TCP and DNS requests from arbitrary commands into a proxy and is really handy.
Setup /etc/proxychains.conf to use the forwarded socks proxy:
[ProxyList]
# SSH reverse proxy
socks5  127.0.0.1 6666

Tunnel arbitrary tools (that use TCP) with proxychains:
$ proxychains telnet google.com 80
$ proxychains yum update
$ proxychains apt-get update

Answer: Newer versions of SSH allow to use the very simple option of ssh -R <[bind_address:]port>. Using only the port on the host and maybe the bind address, but not specifying the client side port will create a reverse SOCKS proxy.
This is also stated in the man pages of newer SSH versions:

[...] if no explicit destination was specified, ssh will
act as a SOCKS 4/5 proxy and forward connections to the destinations
requested by the remote SOCKS client.

You can test this with curl connecting to a simple "give me my IP"-API like http://ifconfig.io.
$ curl ifconfig.io
vs
$ curl --socks5 localhost:<PORT> ifconfig.io
Comment: It would be useful to mention in which version number that feature was introduced.
Comment: @kasperd: [OpenSSH 7.6](https://www.openssh.com/txt/release-7.6), released on 2017-10-03. Check the 3rd bullet point under _New Features_
Comment: much simpler, thanx!
Comment: More details https://medium.com/faun/simple-socks-proxy-for-closed-network-via-ssh-reverse-tunnel-c2d91779cc6e


Imacro SAVE_ELEMENT_SCREENSHOT error

Question: Can't save screenshot of the Element...
Here is code:
ONDOWNLOAD FOLDER=C:\img\ FILE=screen.png WAIT=YES
WAIT SECONDS=1
TAG POS=1 TYPE=DIV ATTR=ID:adcopy-puzzle-image  CONTENT=EVENT:SAVE_ELEMENT_SCREENSHOT

Error:
TypeError: Argument 1 of CanvasRenderingContext2D.drawWindow does not implement interface Window., line 21 (Error code: -1001)

Couldn't find anything in web. This error was reported only on ScreenGrab add-on.
Do you know how to fix this?
Thanks in advance.
Answer: Well.
I had firefox developer edition and that was causing problems... after reinstalling normal firefox it worked perfectly.

Imacro SAVE_ELEMENT_SCREENSHOT error


How to give a powerful kick in Foosball?

Question: I have played for one year and get quite frustrated this month. How to kick POWERFULLY?
Comment: It's all in the wrist.
Comment: How are you with a Frisbee?
Answer: I had a tough time picking up the wrist flick initially. However, before I get to that, it's more important to learn control because that's how you'll be able to get the ball to where you want it and block it from your opponent(s). Once you have it where you want it, you'll be able to score far more often rather than if you simply shot powerfully with no control. Sure, speed looks great and all, but the better you get, the easier you will be able to score with the speed.
With regards to the wrist flick, you don't want to spin the players, but be light with your hand and, with a quick motion downwards while clasping the handle (think of rocketing your palm to the floor), tighten at the last moment. When you tense at the last moment after going very quickly, you will generate more power.
I hope I explained things clearly. It's a little hard to explain with no pictures or videos. Just keep practicing and you will get better.
Good luck!
Answer: One technique I use to create power is to have the foosball player start by pinning the ball between him and the table in a forward position (in front of the player).  The idea here is to achieve an almost full rotation of the player and then hit the ball. It is almost 360 degree spin and creates amazing power.  This is achieved by placing your wrist on the handle then, in a quick motion, pulling up and rolling the handle through the 360 degrees. At the end of the pull up your fingers cup and steady the handle (to avoid the out of control spin).  It can be seen here in slow motion. Especially at the 2:10 mark.


Damped Driven Oscillator Dimensional Analysis

Question: So, I am studying the damped driven oscillator with the following drive force:
\begin{equation}
M\ddot{x}+\gamma \dot{x}+kx=F_0 \cos({\omega t})
\end{equation}
where $M$ is the mass, $\gamma$ is the damping force, $k$ is the spring constant and $\omega$ is constant.
What I am trying to do is find a dimensionless expression of the equation above which can be done by applying the transformation:
\begin{align}
& x \to \xi=\frac{x}{x_c} \\
& t \to \tau=\frac{t}{t_c}
\end{align}
where $x_c, t_c$ are some "x, t characteristic". Now, I found that the expression of the oscillator in those variables would be:
\begin{equation}
\ddot{\xi}+\left( \frac{\gamma t_c}{M} \right) \dot{\xi}+\left( \frac{k t_c^2}{M} \right) \xi=\left( \frac{F_0}{M} \right)\frac{t_c^2}{x_c}\cos({\omega t})
\end{equation}
and by solving the initial equation I can acquire the analytical solution from which I could deduct that (for the case of $\Delta <0$):
\begin{equation}
t_c=\sqrt{\frac{M}{k-\frac{\gamma^2}{4M}}}
\end{equation}
which proves that everything is in the right place since it does match the period of the damped oscillator.
My question is the following
What about $x_c$? I am not able to find an expression for it, and I can see that it is directly involved in the ODE.
Thank you all!
Comment: Is $\Delta$ the discriminant which indicates that the damping is small, i.e. $\gamma^2/4-k/M$?
Comment: Also, shouldn't there in the formula for $t_c$ be $\gamma^2$ instead of $\gamma$?
Comment: Set $x_c=\frac{M}{F_0t_c^2}$ then see what you can do with $t_c$.
Comment: @Sobanoodles yeap its $\gamma ^2$ :) And $\Delta$ is the discriminant of the characteristic polynomial for the initial equation. (I also studied the overdamped and the critical case but only the underdamped case has appears to have oscillations obviously)
Comment: @David Are you sure I should do that? Why would you do that?
Comment: You want to reduce the number of parameters, by setting certain coefficients to one. You can control $x_c$ and $t_c$, so choose them to set as many coefficients as possible to be zero. You will end up with a single  coefficient being not equal to 1.
Answer: So, I think I figured it out.
For the dimensionless expression, the constant coefficient $\frac{F_0 t_c^2}{Mx_c}$ of the drive force $\cos({\omega t})$, stands for the oscillation length. Taking that into account, along with the fact that we do demand $x_c$ to be dimensionless, one could think:
\begin{equation}
\frac{F_0 t_c^2}{Mx_c}=\lambda \Leftrightarrow x_c=\frac{F_0 t_c^2}{M \lambda}
\end{equation}
where $\lambda$ of course is a dimensionless constant.
Next, something that did not occure to me while posting this question, is that $t_c$ could be expressed either on terms of the system's own period like above:
\begin{equation}
t_c=\sqrt{\frac{M}{k-\frac{\gamma^2}{4M}}}
\end{equation}
or on terms of the frequency applied to the system by the drive force, therefore:
\begin{equation}
t_c=\frac{1}{\omega}
\end{equation}
One can use both, and I gave to myself a treat by using the second (and simpler) to reach the final result:
\begin{equation}
x_c=\frac{F_0}{\lambda M \omega^2}, \quad t_c=\frac{1}{\omega}
\end{equation}


How to pass scope value to the directive controller

Question: Hai i am trying to pass array of scope value from controller to the directive how it possiblbe.
i need to pass this value to my directive controller                 $scope.recipientsOrgIdArr.push(data.relatedEntityInstanceId);
HTML file
<search  searchobj="tei_org"  selecteditemslist="recipientsOrgIdArr"   searchid="organisation" />

Search Directive
.directive('search', function ($timeout) {
    return {
        restrict: 'AEC',
        scope: {
            selecteditemslist: "="
}
link: function (scope, elem, attrs) {
console.log(scope.selecteditemslist);  // getting as undefined

//how can i get scope value here
}
}
})

please help me to solve this problem thanks in advance
Comment: Most probably when the directive link function is called the array recipientsOrgIdArr is not filled. Use a watch in link function to know when the array gets data.
Answer: The problem you're experiencing is that selecteditemslist has not yet been set by the time your directives link function is called.
Try this
.directive('search', function ($timeout) {
    return {
        restrict: 'AEC',
        scope: {
            selecteditemslist: "="
        }
        link: function (scope, elem, attrs) {
            scope.$watch('selecteditemslist', function(value) {
                if(value){
                    console.log(scope.selecteditemslist);
                }
        }...

Answer: try using this code:
    .directive('search', function ($timeout) {
        return {
            restrict: 'AEC',
            scope: {
                selecteditemslist: "="
    }
    link: function (scope, elem, attrs) {
    attrs.$observe('selecteditemslist', function(value) {
    console.log(value);
  });
    }
    }
    })


retrieving an object from boost::optional

Question: Suppose a method returns something like this
boost::optional<SomeClass> SomeMethod()
{...}

Now suppose I have something like this
boost::optional<SomeClass> val = SomeMethod();

Now my question is how can I extract SomeClass out of val ?
So that I could do something like this:
SomeClass sc = val ?

Answer: You could use the de-reference operator:
SomeClass sc = *val;

Alternatively, you can use the get() method:
SomeClass sc = val.get();

Both of these return an lvalue reference to the underlying SomeClass object.
Answer: To check if the optional contains a value, and the optionally retrieve it:
boost::optional<SomeClass> x = SomeMethod();
if (x)
     x.get();

To get the optional value, or a default value if it does not exist:
SomeMethod().get_value_or(/*default value*/)

Answer: As mentioned in the previous answers, the de-reference operator and the function get() have the same functionality. Both require the optional to contain valid data.
if (val)
{
    // the optional must be valid before it can be accessed
    SomeClass sc1 = *val;
    SomeClass sc2 = val.get();
}

An alternative is the function value(), which throws an exception if the optional does not carry a value.
// throws if val is invalid
SomeClass sc3 = val.value();

Alternatively, the functions value_or and value_or_eval can be used to specify defaults that are returned in case the value is not set.


SQLite Insert with the newly generated id

Question: I have a table with an auto increment field and another field VARCHAR filed. I would like to fill the string field with the newly generated id directly in the insert query. For example with table:
Person
(
    id auto_increment,
    name varchar
)

I would like to perform a query like this:
INSERT INTO Person(name) VALUE ('Person #' + ROWID())

Anyway to do this?
Answer: This will use the last inserted row id with the increment of 1 to generate the next person id. Keep in mind that for last_insert_rowid() you should not be doing concurrent inserts to the database.
INSERT INTO Person (name) SELECT 'Person #' || (last_insert_rowid()+1);

Alternatively this will read the biggest ID you currently have and increment +1 to it and then concatenate it with the text. If it returns null it uses 1.
INSERT INTO Person (name) SELECT 'Person #' || (IFNULL((MAX(id)+1),1)) FROM Person LIMIT 1;

This one will get the index of autoincrement:
INSERT INTO Person (name) SELECT 'Person #' || (seq+1) FROM sqlite_sequence WHERE name="person"

You can also do something like this:
INSERT INTO Person (name) VALUES (NULL);
INSERT INTO Person (name) VALUES (NULL);
INSERT INTO Person (name) VALUES (NULL);
INSERT INTO Person (name) VALUES (NULL);
INSERT INTO Person (name) VALUES (NULL);
INSERT INTO Person (name) VALUES (NULL);
INSERT INTO Person (name) VALUES (NULL);
INSERT INTO Person (name) VALUES (NULL);
UPDATE Person SET name = 'Person #' || id WHERE name IS NULL;

Or:
INSERT INTO Person (name) VALUES ('Person');
UPDATE Person SET name = name || ' #' || id WHERE id = last_insert_rowid();

This one will update the names that does not have a # with its row id:
INSERT INTO Person (name) VALUES ('Person');
INSERT INTO Person (name) VALUES ('Person');
INSERT INTO Person (name) VALUES ('Person');
INSERT INTO Person (name) VALUES ('Person');
UPDATE Person SET name = name || ' #' || id WHERE (LENGTH(name)-LENGTH(REPLACE(name, '#', ''))) = 0;

Live DEMO
Comment: Thank you! Actually, i was looking for a method without update or select, but since it's seems to be not possible, i will use the update way, which is i think the safest.
Answer: You shouldn't store the same info twice. Just generate the string you want dynamically when you query:
SELECT name || '#' || id FROM Person

Answer: You have to do two sentences: INSERT + UPDATE, you cannot access the last insert id in the current insert sentence
Answer: The system will not reliably know that ID until the row is inserted.  I would think the best solution would be a view (since SQLite does not support computed columns which would be the easiest route).  Just insert the VARCHAR and use a view to build the concatenated value at request time using the inserted VARCHAR and the allocated ID.
Good Luck!
Answer: This will not be guranteed but this might work ...
INSERT INTO tab1(val) VALUES ('tab1 #' + cast((SELECT top 1 IDENT_CURRENT('tab1') + IDENT_INCR('tab1') FROM tab1) as varchar(20)))


SVM regression with LIBSVM and python - Expected runtime

Question: Im new to regression, but quit experienced in classification and machine learning. In Classification, the state of the art technique for classification is SVM, so the first solution I would like to use for my regression problem is an SVM one.
The regression problem im trying to solve can be described as follows:
I have one dependent variable which i would like to estimate with 100,1000,4000 independent variables respectively, and estimate the accuracy with each size. I have around 100,000 observations which Im planning to use in a 90%-10% cross validation scheme.
The generating process is probably not linear, this is why I want to use SVM regression (I will test linear kernel, though)
Question is, before I even attempt such process, what is the expected run-time for one regression iteration with linear or RBF kernel? Can anybody give a raw estimation? Is this problem feasible in the regression world?
Comment: SVMs are not state of the art.
Answer: The complexity of SVM regression is similar to the complexity of SVM classification. If problems of that size are feasible for you in a classification context, they are also feasible in regression.
When using a nonlinear kernel, training complexity is quadratic in terms of the number of training instances. 100k training instances is quite a lot, so I most definitely recommend trying a linear kernel first. For the linear kernel, you should consider using LIBLINEAR instead of LIBSVM (same authors, the former is made specifically for large-scale problems).
The impact of the number of dimensions on training time is not very high, this is one of the advantages of kernel methods. Knowing that, you may well go for 4000 dimensions straight away. If you have 4000 dimensions, linear models are likely to perform quite well.
It is very hard to give a good estimate of the actual run time as it depends on a lot of things, related to the data and your hardware. That said, you can expect training time to be in the order of hours tens of minutes per model for LIBSVM. If you use LIBLINEAR, it will be a couple of seconds.
Comment: Wow, that's a huge differnce. Thanks for the info, I'll try liblinear first. Im going to use 100K observations becuase of the large amount of parameters i'll need to estimate. the nature of the problem is such that the more feature i'll bring, the accurate i'll get. Thanks :)
Comment: @idoda looks like I made a mistake. Nonlinear training time goes in the order of hours for more than 500k instances on some data I have. For 100k instances you can expect 10-15 minutes per model (which is still **much** more than linear).


How can I customize the way my taxonomy terms are displaying?

Question: I have been searching all over the place for some help on this.
I currently have my tags displaying for each node like such: tag,tag,tag
Now I would like them to display a little cleaner and more appealing to my visitors. So what I would like to do is have them display like such: tag \ tag \ tag
Now from what I have read, I need to target the field API? and use a preprocess function to alter what the array is spiting out? Please correct me if I am wrong.
This seems like an easy task to complete but I just can't seem to find a solution.
Thanks in advance
Answer: Indeed a preprocess function is what you're looking for, I suspect in D7 this will help:
<code>function MYTHEME_preprocess_html(&$variables) {
if(arg(0)=='node' && is_numeric(arg(1))) {
    $node = node_load(arg(1));
    $results = taxonomy_node_get_terms($node);
    if(is_array($results)) {
        foreach ($results as $item) {
</code>
In terms of D6, node-taxonomy.tpl.php and page-taxonomy-term.tpl.php should put you on the right path.
Comment: The sample I provided is incomplete PHP code. The addition of the '/' character would be in the foreach loop as you see fit. $item is would each of the terms to something like:

$categories .= $item .' / ';

Perhaps you might want to investigate basic PHP programming concepts first? It will be essential to customizing Drupal.
Comment: Thanks for helping me out... but in your example I don't see here the \ symbol comes into play... i'm still a little lost and yes I am in D7. Again thanks for the response!
Comment: @DeeZone... great thanks for the added explanation.


Executable directory where application is running from?

Question: I need to get the path (not the executable) where my application is running from:
System.AppDomain.CurrentDomain.BaseDirectory()

When I run the above statement with & "/images/image.jpg" on my local machine it works fine but when I install the application on another machine it says it cannot find the file and there is a lot of extra path information some.
I just need the directory of where the app is running.  I am coding in VB.NET with Visual Studio 2008.
Thanks!
Comment: dont know much about vs 2008 but when you do a dirinfo dont you get the dirinfo from where the app. is running?
Comment: Possible duplicate of [Get programs path?](http://stackoverflow.com/questions/2216141/get-programs-path)
Answer: This is the first post on google so I thought I'd post different ways that are available and how they compare.  Unfortunately I can't figure out how to create a table here, so it's an image.  The code for each is below the image using fully qualified names.

My.Application.Info.DirectoryPath

Environment.CurrentDirectory

System.Windows.Forms.Application.StartupPath

AppDomain.CurrentDomain.BaseDirectory

System.Reflection.Assembly.GetExecutingAssembly.Location

System.Reflection.Assembly.GetExecutingAssembly.CodeBase

New System.UriBuilder(System.Reflection.Assembly.GetExecutingAssembly.CodeBase)

Path.GetDirectoryName(Uri.UnescapeDataString((New System.UriBuilder(System.Reflection.Assembly.GetExecutingAssembly.CodeBase).Path)))

Uri.UnescapeDataString((New System.UriBuilder(System.Reflection.Assembly.GetExecutingAssembly.CodeBase).Path))

---
Edit October 18, 2021:
Sigh... None of the above work if using net5.0 or net6.0 and publishing app as single-file bundle. Best I got now is:
// This will give you the directory but not the assembly
string basedir = AppContext.BaseDirectory;
// Before you package the app as a single file bundle, you will get the dll.
// But after you publish it, you'll get the exe.
string pathToExecutable = Environment.GetCommandLineArgs()[0].Replace(".dll", ".exe");

Comment: System.AppDomain.CurrentDomain.BaseDirectory()
Works good also in console/service application!
Answer: Dim strPath As String = System.IO.Path.GetDirectoryName( _
    System.Reflection.Assembly.GetExecutingAssembly().CodeBase)

Taken from HOW TO: Determine the Executing Application's Path (MSDN)
Comment: strPath contains **file**:\\c:\\xxxxxx
Comment: This is not safe, for example when it is being used to open config file it causes `System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: URI formats are not supported`
Comment: This link is out of date :(.
Answer: I needed to know this and came here, before I remembered the Environment class.
In case anyone else had this issue, just use this: Environment.CurrentDirectory.
Example:
Dim dataDirectory As String = String.Format("{0}\Data\", Environment.CurrentDirectory)

When run from Visual Studio in debug mode yeilds:
C:\Development\solution folder\application folder\bin\debug

This is the exact behaviour I needed, and its simple and straightforward enough.
Comment: Smallest, easiest solution, with formatting! +1, thanks.
Comment: Yes, this help to make the application truly portable, by keeping the settings file in the root path of the application. Thanks for the simple solution!
Comment: @vr_driver This is bad. For portable the executable file's parent directory should be used, not the current directory, which can be different from the former. For example if a script running in `%windir%` calls the program using an absolute path it will look for ` %windir%\data\ ` and that is bad.
Comment: @jason-alls: Your example output is not 100% correct. With your code you get `C:\Development\solution folder\application folder\bin\debug\DATA` because you add it to your value from `Environment.CurrentDirectory`
Comment: This is the answer that worked for me.  All the Application properties were returning .Net framework -based results, this returned the actual directory my app is running in.
Comment: Digging old posts and found this! Thanks a lot, saved my day!
Answer: Dim P As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
P = New Uri(P).LocalPath

Answer: You could use the static StartupPath property of the Application class.
Answer: You can write the following:
Path.Combine(Path.GetParentDirectory(GetType(MyClass).Assembly.Location), "Images\image.jpg")

Comment: There are pitfalls to this approach as the assembly does not necessarily reside in the same directory as the executing assembly.


Radical Applications of Algebraic Topology

Question: Are there any radical applications of algebraic topology? For example, in probability theory we look at sample spaces. Suppose the sample space is a torus (for example). Would computing homology groups and homotopy groups of the torus illuminate anything about the probability of certain events? If we have a random variable $X$ with some pdf $f_{X}(x)$ then we can transform it (e.g. $Y = X^2$) and find the pdf $f_{Y}(y)$ using the Jacobian. Is there any usefulness of computing various topological invariants of spaces in terms of probability theory?
Comment: What do you mean by "radical"?
Comment: Cohomology theories apparently have [some use in engineering](http://jointmathematicsmeetings.org/meetings/national/jmm2012/2138_program_ss37.html).
Comment: I've heard of an application of homotopy theory to functional analysis (or maybe PDEs, not sure), but I don't know any more about it than that.
Comment: To add to what was mentioned by Jesse Madnick, they also have applications in computational complexity theory.
Comment: In most cases, the important thing with sample spaces is the sigma-algebra on them, which doesn't notice the kind of topological structure you mention. Using a torus for the sample space is no different from using a square or a 7-dimensional sphere. Your change of variable example is to do with structure on the space in which your random variable takes values, not with structure on the sample space. However, you can consider objects with interesting structure (locally compact groups, for instance) and study spaces of measures on them, but that seems slightly different from what you describe.
Comment: Also, radical is, like, totally egregious. Bill S. Preston said so.
Answer: If you're using "radical" in the 1980s sense, then there are lots of such things (in my mind). Something that might be cool is how things in algebraic geometry (or homological algebra) can be cast in the light of algebraic topology. A good read is Peter May's paper Derived Categories in Algebra and Topology.


Does PagingData Run on a Background Thread by Default in Paging 3 Library?

Question: Is background threading managed automatically with PagingData as it is with PagedList, and then returned on the main thread?
From the logs below, it appears PagingData is not run on the backgroud thread in the Paging 3 library compared to PagedList in Paging 2's library.
Expect (Based on the Paging Codelab sample)

GithubPagingSource override suspend fun load(...) to run on an IO thread.
SearchRepositoriesActivity viewModel.searchRepo(query).collectLatest { ... } to run on the main thread.

Observe

Both GithubPagingSource override suspend fun load(...) and SearchRepositoriesActivity viewModel.searchRepo(query).collectLatest { ... } run on the main thread.

Paging 2
Threading is handled on the background by PagedList with toLiveData according to the documentation.

If you use LivePagedListBuilder to get a LiveData, it will initialize PagedLists on a background thread for you.

Paging 3
The Paging 3 documentation does not mention how threading is managed. However, from the logs, PagingSource appears to be running the network request on a main thread and returning the PagingData on a main thread.
My Sample Code
I've recreated the Codelab pattern in the CryptoTweets sample app app-simple module.
FeedPagingSource.kt
class FeedPagingSource : PagingSource<Int, Tweet>() {

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Tweet> {
        try {
            val page = params.key ?: 1
            val nextPage = page + 1
            val tweets = Injection.feedService.getTweets(...)
            println("Thread: FeedPagingSource ${Thread.currentThread().name}")
            Log.v(LOG_TAG, "load success: ${tweets}")
            return LoadResult.Page(...)
        } catch (error: Exception) {
            ...
        }
    }
}

FeedRepository.kt
class FeedRepository {
    fun initFeed() = Pager(
        config = PagingConfig(pageSize = FEED_PAGEDLIST_SIZE),
        pagingSourceFactory = { FeedPagingSource() }
    ).flow
}

FeedViewModel.kt
repository.initFeed().onEach {
    println("Thread: FeedViewModel ${Thread.currentThread().name}")
    _feed.value = it
}.launchIn(viewModelScope)

Attempted Solution
In order to run the PagingSource on a background thread, the flow is initiated on Dispatchers.IO. However, the log still shows PagingSource runs on the main thread in FeedPagingSource.kt.
FeedViewModel.kt
repository.initFeed().onEach {
            println("Thread: FeedViewModel ${Thread.currentThread().name}")
            _feed.value = it
}.flowOn(Dispatchers.IO).launchIn(viewModelScope)

Comment: Try `.launchIn(viewModelScope  + Dispatchers.IO)`
Comment: Thanks for the recommendation @IR42. However, this results in the flow data emitting on the IO thread, `DefaultDispatcher-worker-1`, and the `PagingSource` still running on `main` in the code above.
Answer: It is not running on background thread in Paging 3, even I use lifecycleScope.launch(Dispatchers.IO) in the view model, as the PagingSource is accessed from Main thread when adapter loads. So for Room, I get it working by wrapping the PagingSource database code inside withContext(Dispatchers.IO) {
private const val STARTING_PAGE_INDEX = 0
private const val COUNT_PER_PAGE = 20

class LocalImagesPagingSource() : PagingSource<Int, GalleryImage>() {

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, GalleryImage> {
        val page = params.key ?: STARTING_PAGE_INDEX
        return try {
            withContext(Dispatchers.IO) {
                val dao = GalleryImageDatabase.getDatabase(context).galleryImageDao()
                val images = dao.getGalleryImagesByPosition(page * COUNT_PER_PAGE)

                LoadResult.Page(
                    data = images,
                    prevKey = if (page == STARTING_PAGE_INDEX) null else page - 1,
                    nextKey = if (images.size == 0) null else page + 1
                )
            }
        } catch (exception: IOException) {
            return LoadResult.Error(exception)
        }
    }
}

Comment: My original solution is for loading local images from CursorLoader, but also saving and loading additional info from room. I simplify the sample code with just room related code.
Comment: I'm having the same problem. Could you please post the sample code which uses "withContext(Dispatchers.IO) {"?
Comment: Otherwise just make the Room DAO call a `suspend fun` then you won't have to use `withContext(Dispatchers.IO)` in your `PagingSource`.
Answer: Kotlin Coroutines
When implementing PagingData and PagingSource with Kotlin Coroutines and making a Retrofit network request, as in the sample code above, Retrofit handles the background threading by default and returns on the main thread.
See StackOverflow: Does Retrofit make network calls on main thread?
RxJava
When using RxJava with Retrofit as the network source, the threading needs to be explicitly specified as shown in the Android documentation sample.
See Android documentation: Page from network and database > Implement a RemoteMediator
Comment: what about room?!
Comment: @A.sobhdel according to the Coroutines codelabs [here](https://developer.android.com/codelabs/kotlin-coroutines#8) Room uses a custom dispatcher


Django Queryset Filter (Maybe Needs a Subquery)

Question: I want to generate a queryset to find mismatches.  As an example
class Vehicle(models.Model):
    car = models.CharField(max_length=100)
    model= models.CharField(max_length=100)
    passengers = models.IntegerField()

i want to generate a query where i can find cars erroneously listed with two different models.
something along the lines of a query to find that if a car = Wrangler, model = Jeep to find instances of car = Wrangler, model is not Jeep.
Is this possible to do within the ORM, or do i need to use raw SQL?  Someone in #django suggested a subquery, but I am not familiar with how to do this
Sample Output
would just be a queryset of mis-matched vehicles (for example car = Wrangler, model = Ford exists for one object but car = Wrangler, model = Jeep for another object).  I was thinking of not having an input just being able to find mismatches.  Does this make sense yet?
Comment: Don't obfuscate your model like that. What are `some car` and `some model`? Foreign keys?
Comment: You should give an example of the expected input and output. It's a bit hard to understand at the moment what exactly do you mean.
Answer: If I understand you need to model+car be unique together, so you need to find repetitive records first:
from django.db.models.aggregates import Count

repetitve_cars_list = Vehicle.objects.values('car').annotate(count_car=Count('car')).filter(co
unt_car__gt=1)

This will give you the cars that was defined with more than one model


Iteration/loop through imported data in R

Question: I have created a working process below that allows for the graph's baseline correction for a given data set outlined below.

setwd("C:/Users/o/OneDrive/Desktop")

importData = (read.delim("OSJH103h.txt", header=F))
matrixData = as.matrix(importData)
swappedColRow = t(matrixData)
row.names(swappedColRow) = c(1,2)
removedColumn = swappedColRow[-c(1),]
matrixRemovedCol = as.matrix(removedColumn)
swappedMatrix = t(matrixRemovedCol)

bc.irls = baseline(swappedMatrix, lambda=2, hwi=100, it=10, int=2000, method = 'fillPeaks')
mf = getCorrected(bc.irls)
mf2d=data.frame(ys=mf[1,], xs=importData$V1)
par(mfrow=c(1,1))
plot(x=mf2d$xs, y=smooth(mf2d$ys), col=2, type="lines")

How would I import multiple data files that could be iterated/looped through and remove the baseline for each given dataset?
I have outlined a method for importing all the .txt files in a given directory.
temp = list.files(pattern="*.txt")
myfiles = lapply(temp, read.delim, header=FALSE)

The files are imported as [[1]], [[2]], [[3]]...
Thus replacing 'importData' for myfiles[[2]] yields the same result
Looking for a way to import ~10/15 data sets at a time and remove the baseline for each. Then ideally, export corrected data to a separate txt file.
I hope this makes sense. Any help would be appreciated.
Comment: what is your `getCorrected(` function that is above?
Comment: it's a function in a baseline package which extracts the baseline corrected raw data
Comment: I think you simplify by separating 'processing' from 'plotting', and using a series of `lapply` in a function with your return being a list of `mf`. I'll sketch below what I think might work, but I don't have `baseline` nor your data, so you can do the debugging...`temp` in, `mf` out. Forgive if I'm mainly copy/pasting your above.
Answer: Perhaps this:
library(baseline)
temp = list.files(pattern="*.txt")
reproc_base <- function(temp) {
    importData = lapply(temp, read.delim, header=FALSE)
    matrixData = lapply(importData,as.matrix)
    swappedColRow = lapply(matrixData, t)
    swappedColRow = lapply(swappedColRow, row.names,c(1,2)) # uncertain
# lapply(myList, function(x) { x["ID"] <- NULL; x }) SOF?12664430
    removedColumn = lapply(swappedColRow, function(x) {x[1, ] <- NULL; x}) # uncertain
    matrixRemovedCol = lapply(removedColumn, as.matrix)
    swappedMatrix = lapply(matrixRemovedCol, t)
    bc.irls = lapply(swappedMatrix, baseline, lambda=2, hwi=100, it=10, int=2000, method = 'fillPeaks')
    mf = lapply(bc.irls, getCorrected)
    return(mf)
}

#while debugonce(reproc_base), you'll probably just want 1 file
debugonce(reproc_base)
test_mf <- reproc_base(temp[1])

Well, as you see, there are a couple of notations that I'm uncertain about. But play with it in debugonce(reproc_base) or debug(reproc_base) and let's see where it breaks. And anonymous function SOF 12664430.
Answer: My solution in case anyone is interested
temp = list.files(pattern="*.txt", full.names = T)
myfiles = lapply(temp, read.delim, header=FALSE)

for (i in 1:length(temp)){
  matrixData = as.matrix(myfiles[[i]])
  swappedColRow = t(matrixData)
  row.names(swappedColRow) = c(1,2)
  removedColumn = swappedColRow[-c(1),]
  matrixRemovedCol = as.matrix(removedColumn)
  swappedMatrix = t(matrixRemovedCol)

  bc.irls = baseline(swappedMatrix, lambda=2, hwi=100, it=10, int=2000, method = 'fillPeaks')
  # plot(bc.irls)
  mf = getCorrected(bc.irls)
  mf2d=data.frame(xs=myfiles[[i]]$V1, ys=mf[1,])
  par(mfrow=c(1,1))
  plot(x=mf2d$xs, y=smooth(mf2d$ys), col=2, type="l")

  teststr<-temp[i]
  str_sub(teststr,1,2)<-""
  str_sub(teststr,-4,str_length(teststr))<-""

  teststr
  write.csv(mf2d,paste0(teststr," BLC.csv"), row.names = FALSE)
}


How to optimize if else statement to Map using java code

Question: I am new to java I want to optimize my code using Map in java. When I
write my Code I use lot of if else statement so I want reduce my code
using  map of collection framework When I implement Map i am not get
accurate result. My Function totally not working. Here Is my code.
private List<Item> getBussinessSearchItem(IteamSearch itemSearch, byte searchType,int size, int offSet){
        StringBuilder  queryBuilder - new StringBuilder();

        if(itemSearch.getOrigin()!=null && (checkOriginDomain(itemSearch.getOrigin))) {
            if(itemSearch.getSort().equalsIgnoreCase("name")) {
                if(itemSearch.getSortOrder() == 1) {
                    queryBuilder.append("ORDDER BY Name ASc");
                }else {
                    queryBuilder.append("ORDDER BY Name ASc");
                }
            }else if(itemSearch.getSort().equalsIgnoreCase("upc1")) {
                if(itemSearch.getSortOrder() == 1) {
                    queryBuilder.append("ORDDER BY upc1 ASc");
                }else {
                    queryBuilder.append("ORDDER BY upc1 ASc");
                }
            }
            else if(itemSearch.getSort().equalsIgnoreCase("minQuantiy")) {
                if(itemSearch.getSortOrder() == 1) {
                    queryBuilder.append("ORDDER BY minQuantiy ASc");
                }else {
                    queryBuilder.append("ORDDER BY minQuantiy ASc");
                }
            }
        }

when I use this above statement it works fine and give me result
properly sorting with ASCENDING AND DESCINDING order. But I do not
want to implement above statement I want to implement map and that map
works same as the above statement.
Here is my code but this below code not working
private Map<String , String> getBussinessSearchItem(ItemSearch itemSearch){
        Map<Srring,String> newMap = new HashMap<>();
        itemSearch.getSort().equalsIgnoreCase("name");
        itemSearch.getSort().equalsIgnoreCase("upc1");
        itemSearch.getSort().equalsIgnoreCase("minQuantiy");
    }

I do not know how to implement map instead of if else statement. I
want  solve  this issue using map  or enum.
My ItemSerach table contain name,upc1,minquantity
Comment: Right now your code always orders by ascending. Typo?
Comment: Your code does nothing with the map, so of course it doesn't work. Your map is, in fact, empty. I'd start with filling it with what you want it to hold, and then worry about how to get what you want from it. Or rather, I'd start learning JPA or something like that and stop building my queries with a StringBuilder.
Comment: besides, it now spells ordder
Answer: If you move sort options to a separate class ItemSearchOrder class which implements equals and hashCode properly, you could use it directly as key in your map:
class ItemSearchOrder {
  private final String sort;
  private final int order;
  public ItemSearchOrder(final String sort, final int order) {
    this.sort = Objects.requireNonNull(sort);
    this.order = order;
  }

  public static ItemSearchOrder asc(final String sort) {
    return new ItemSearchOrder(sort, 1);
  }

  public static ItemSearchOrder desc(final String sort) {
    return new ItemSearchOrder(sort, 0);
  }

  @Override
  public boolean equals(final Object other) {
    if (other == null) return false;
    if (other == this) return true;
    if (other.getClass() != this.getClass()) return false;

    return this.order == other.order
      && this.sort.equalsIgnoreCase(other.sort);
  }

  @Override
  public int hashCode() {
    return Objects.hash(sort, order);
  }
}

Then build your map:
final Map<ItemSearchOrder, String> sortQueries = Map.ofEntries(
  Map.entry(ItemSearchOrder.asc("name")), "ORDER BY name ASC"),
  Map.entry(ItemSearchOrder.desc("name")), "ORDER BY name DESC"),
  Map.entry(ItemSearchOrder.asc("upc1")), "ORDER BY upc1 ASC"),
  ...
);

And then use it to look up the correct ORDER BY clause:
public List<Item> getBussinessSearchItem(
    final IteamSearch itemSearch, ...) {
  final String sort = sortQueries.get(itemSearch.getOrder());
  if (sort == null) {
    // unknown sort
  }

  // ...
}