Impossibly long flute note

Question: I sometimes compose for my own amusement but I have no delusions of creating anything that anyone else would like to hear.
In one of my ideas, I want a long sustained flute note to bridge two movements.  The other instruments will stop on a chord but the flute will continue.  The next movement will begin on another chord in which the flute's note serves a different role.
So, the note might be too long to play comfortably or at all. I was wondering whether I could give the illusion of this impossibly long note by requesting two players to collaborate.  One will begin and fade out when necessary and the other would come in.  A couple of such handovers should be sufficient.
Is the idea feasible?
Comment: "I have no delusions of creating anything that anyone else would like to hear." Don't sell yourself short. If you write music, you have a rare gift that many other people would love to hear for themselves. You certainly don't have to share your music if you don't want to, but if you do want to, you should go for it! :)
Comment: IIRC, this very trick is used to play Debussy's Prelude to the Afternoon of a Faun. It's not notated, but he simply wrote a note too long to actually be played, and that's how folks accomplish the phrase IRL. (Someone can jump in and correct me if I'm pointing to the wrong piece. I'm 90% sure it was this piece, but if it wasn't, then it could have been Debussy or Faure)
Comment: Strings can do this effortlessly...
Comment: Wonder if you're aiming for something like this: In the Mendelssohn violin concerto, the bassoon holds one of the notes over from the last chord of the first movement to bridge to the second movement.   I am not a wind player so I can't say whether it's more reasonable for bassoon to sustain than flute; but in this case, the one note is not held by itself that long before other instruments start coming back in bit by bit.
Comment: @wrschneider Yes, I am aware that the idea is not completely original.
Comment: It's certainly easier for a reed instrument than for a flute, but it's been done. When alternating players, in the scores I've read, the first payer is usually notated as playing a whole note plus an eighth while the second player starts, and so on.
Comment: The transition between movements 8 and 9 (Nimrod) of Elgar's Enigma variations uses the same effect (although it is one player and shouldn't be unplayably long)
Comment: @CJBrew Oh dear, that suggests subconscious plagiarism.
Comment: @badjohn possibly -- but good ideas are good ideas wherever they come from :D
Comment: @Kevin +1 for that comment!  There is always plenty of music out there that is worse!
Comment: @dalearn I'll aspire to write music that is not the worst out there.
Answer: Find a flutist who can do circular breathing.  They are rarer than oboists with that skill, but it is getting increasingly demanded these days.
As long as you are composing only for your amusement, the capabilities of MIDI expanders will be more important than that of actual flutists anyway.
Comment: For reference: circular breathing is used by Australian Aborigines when playing the Didgeridoo.
Answer: The handover is feasible.  The circular breathing thing would be classed as a #special skill' I think!
But how long do you need?  A good flautist might give you 30 seconds without special technique.
Comment: It may be infectious.  I don't mean literally though that could be so.  I mean after one person coughs, others who have been holding back their coughs may feel that they may now let go.
Comment: @badjohn Colorado Symphony Orchestra. After intermission and before starting the *Rite*, the conductor took the opportunity to tell the audience that the two pieces were composed barely more than a decade apart and explain some of the rhythmic and harmonic elements that made the *Rite* so groundbreaking and different.
Comment: ^ Can confirm -- just saw Rach 2 and *Rite of Spring* this weekend, and there was an *incredible* amount of coughing, especially given how small the audience was.
Comment: @KyleStrand Rach 2 and Rite of Spring in the same concert?  Where?  I go to see the CBSO (City of Birmingham Symphony Orchestra) often.  I love them but there is one aspect that I don't like: their programmes seem to be constructed by pulling names of pieces from a hat.
Comment: @LaurencePayne With the CBSO, we don't even get that much connection.  We went last Saturday and got: an overture by Bax, Grieg's Piano Concerto, and Orff's Carmina Burana.
Comment: The "30 seconds" figure will vary quite a lot depending on the range and dynamic of the note in question.  In general, higher & louder notes need significantly more air on the flute.
Comment: @MichaelSeifert The note would be the Bb just above the treble clef so not near either extreme.  The dynamic will not be extreme either, maybe mp.  Easily audible but no more than that.
Answer: It seems simple enough to write two parts for two different flute chairs.  Simply add breath marks in alternating places.  Then you can carry the note potentially for the entire piece.
I don't think a flute is loud enough that you need to worry about one part fading in while another is fading out (if, as you describe, there are other instruments accompanying).
Answer: It might be an idea to keep in mind that the composer (especially one just starting out in the world) should write as practically for the chosen instruments as possible.  The flute would not be my first choice of woodwind for holding a long and exposed note.  The reed instruments are better at this for obvious reasons.  Your long flute note might have to be so soft that it might not achieve your desired effect of maintaining audience attention.  The dovetailing of two flutes would work but an oboe would be more noticeable and would achieve it effortlessly.  If it's flute that you really want, then go for the two flutes.
Answer: I agree with the other user that if this is just for fun and it's all going to stay in software, there's no reason to worry about this kind of practicality.
If you are really interested in playability, then what you suggested is completely reasonable. Flutes in particular can sneak in and out very subtly. It might help to have at least two people playing at all times, since the difference between one and two players is noticeable.
In the event that you do manage to get a reading of your piece, it will likely be through a college or community band/orchestra, and I don't think it's reasonable to expect players at that level to circular breathe or be able to sustain a note for that long.
Answer: My preferred notation method would simply be a small text note, perhaps as a symbol and footnote, indicating that the held note may be staggered if necessary. Specifically spelling out how to stagger it seems unnecessary, especially since simply holding out the note is also acceptable if possible.
An example note: "May be staggered among two or more flautists."

Impossibly long flute note


How to make role field on approval in the Edit Profile page by Admin

Question:
This website has over 1000 users with different profiles. Each user choose their profile on registration. On their Edit Profile page, the authenticated users can change or add more profiles to themselves. This was achieved by using the Role Delegation module and by changing some permissions.
Now, I would like to add another feature, namely, the role changing has to be approved by the administrator. This question is actually proposing what I'm looking for, but I cannot select the Role field, only the standard fields, such as First Name, or Last Name, or Email.
How can I achieve this additional feature?
Thanks!
UPDATE 1
I can try to narrow the question.  What I'm looking for is a rule, which notifies the Admin when a normal user changes its role, by sending an email. If possible, the rule should keep this edit unsaved until the Admin approves the change. Is it possible to achieve this kind of action?
Comment: Voting to close this as "Too Broad". Adding such feature would require custom coding.
Answer: You can't allow the users to change the role/profile directly, if this is what you want. What you'll need instead is another field to act as a proxy for the roles, so you would have a checkbox for each one.
Then you can use a rule to detect a change to the user and if there is a difference in the old and new values of this field, send an email to the admin.
I think this can probably all be done with contrib modules though it will not be a trivial matter, and would be easier to build and maintain with a little custom code acting as glue.


Are there "rules" for improvising with the minor pentatonic scale over 12-bar shuffle?

Question: I'm learning Justin Guitar's beginner's course and just reached the blues improvisation lesson. I know the notes in the A minor pentatonic scale. To improvise with someone playing A minor 12-bar blues over and over (A7 A7 A7 A7 D7 D7 A7 A7 E7 D7 A7 E7), can i just play any notes of that scale at any time? Should I try to confine certain notes to certain chords or beats?
Of course whatever sounds good is good, but I'm curious if there are some other "rules" or rather guidelines? The only guideline that I know of is to stick to notes from the A minor pentatonic scale.
Comment: I'm a little surprised that nobody has mentioned major pentatonic scales here. A major pentatonic scale has the same notes as the minor pentatonic scale a minor third (three half steps) below it. So you can play an _A major pentatonic_ scale by playing the notes from _F♯ minor pentatonic_. This can give you a nice new sound; for a blues in **A** you might try playing _A major pentatonic_ on the **A7**, _A minor pentatonic_ on the **D7**, and _E major pentatonic_ on the **E7**. You still need to pay attention to how the scales sound against the chords, as the very good answers below suggest.
Answer: You can, and there are many who do! However, you'll find if you play each note separately over each chord, that some will fit better than others. You'll also find that some individual notes will fit to two, and at a pinch, all three chords.
The root note, A, fits well over A7 and also D7, but not so well over E7. The next note, C, fits very well over D7, and in a bluesy manner over A7, although a lot of better players give it a tweak - a bend up, hinting , or even arriving, at C#, the maj. 3rd of A, where it fits perfectly.
I won't take each note in turn - that's your job! You may feel some fit better, or not at all...  Obviously, the better fitting ones will be reflected in the prevalent chord, so will work best - on beats 1 and 3, with passing notes which maybe don't belong at all in between.
The other big issue is that, as said, you can widdle away with Am pent. notes all through, but how about Dm pent. notes on D7, etc? You'll actually find some notes from one are also from the other. That's why it works by itself - to a degree.
When you get a bit fed up with just those 5 notes (from A, or from a, D and E), there's a bitter-sweet note that sounds horrible and gorgeous together, it lives between the third and fourth note you play in min. pent. The blue note!!
And, well done for recognising 'guidelines' instead of 'rules'!
Comment: I appreciate your point that each person should go through the exercise of testing each note him/herself, but your advice on the A and C is *super* helpful. +1 I would love to see a discussion of the other notes too. (e.g., playing D over the A7 is unstable and wants to resolve; playing A over E7 isn't as strong because an altered sound is preferred to a sus sound; or whatever you think!)
Comment: Fair enough. I agree that it's personal--I just found the couple examples you provided so helpful and wondered what other wisdom/advice you had about the other notes. But doing the work oneself is certainly the most important thing for improving on one's instrument.
Comment: @jdjazz - I'd have thought that D note over A7 is the same as A note over E7 - both sort of 'sus 4', but it'll depend where you are in the sequence and what else you play just before or after, and obviously where it comes in the particular bar. I wasn't going to provide a 'to do' list. It's too personal, and I prefer people who ask questions like this to go away and do a bit of homework. For their own good, you understand...
Answer: One of the most common improv tools is a technique called 'target notes'. These are the main notes of your improvised melody, and they will typically be one of the chord tones of the chord for that measure. For example, in the first bar of A7, the main notes of chord are A, C#, and E. Pick one of these notes as the 'target note' and then highlight it by playing it on the quarter note beats (the beats that the kick and snare hit). The other notes can be any note from the scale (or outside of it!), and add color to the melody. By focusing on playing chord tones on the quarter note beats, your improvised melody will sound more organized, and is the beginning to understanding phrasing.
You might have noticed that the notes of the chord A are A, C#, and E, but the note in the A minor pentatonic scale is C, not C#. This is a common technique in blues where you use both the major 3rd and minor 3rd, sometimes right on top of each other. When you are soloing over this progression, you will probably want to use the note C as a target note, which will clash with the C# in the chord in a very 'bluesy' way. You can also bend the C up to C#, and then release it back down. You will probably recognize that sound from many blues songs when you play it.
Once you are comfortable using the 3 notes of the chord as target notes, try using the 7th. For A7, this will be the note G. The 7th is a very colorful tone of each chord, and emphasizing it as a target note can liven up your solos. By writing a simple quarter or half note melody of target notes, and then improvising around them, you will sound like you are playing a deliberate song, and will come to be able to create melodies from scratch, even if you have never heard the tune before.
Comment: Whichever they get called, generally we determine the time sig. and beginning of each bar by listening to a few bars. The first beat rarely has a snare hit on it - that's the kick drum's role. Same with beat 3 in 4/4, and *that's* where the chord changes, if there are any, will be, on 1 and 3. I don't often find a change of chord on beats 2 or 4.
Comment: Aren't beats 2 and 4 in 4/4 time the upbeats? 1 and 3 are the downbeats - usually the points where the kick drum is played.
Comment: That is true, I probably should have called them the up beats to be more accurate, but I was thinking in terms of the sound of the beat, the snare is the strong beat of the measure, and would be a typical place to place chord tones, especially at the beginning of learning to improvise.
Comment: The more I think, I agree with your comments and edited my answer to clarify
Answer: You can play any note from the scale at any time and it will never sound completely wrong, but each note will have a different sound based on the chord being played.
I suggest to best way to learn the combinations and how they sound is to both improvise your own solos and learn other people's solos, or at least parts of them. That way you are training your fingers and your mind at the same time.
A few immediate guidelines are that the keynote (A in this case) is a very important note that sounds like it fits better than the others, and less so with the fourth and fifth (D and E in this case). Overusing any of those three can quickly get boring. The third and the seventh (C and G) add more flavor but can clash more often.
One of us could write out a list of notes and combinations but it's much better to learn to feel your way through it.
Oh one more thing about your last sentence. Once you have a good feel for the five notes of the pentatonic minor scale, you might start experimenting with notes outside of it. The guideline to stick to the scale is good for beginners. If you start learning other people's solos you'll find very early on that they will go beyond the scale to add even more color. Notes to experiment with include (for the key of A) C#, D# and G#.
Answer: It's hard to play the same scale over every chord and sound great. The 4th scale degree (D over A7) should be used as a passing tone, not a tone to dwell on. Either slide/bend it up to the 5 quickly, or play something like 5 - 4 - b3 - 3 - 1.
Any note that's in the chord will sound good with that chord. Here's one approach:
Over A7, use the A Mixolydian scale using the flat 3 sliding up to the 3, and SKIPPING the 4 (the D) when ASCENDING:
A B C-C# E F# G A
In terms of scale degrees, that's 1 2 b3-3 5 6 b7 1
When DESCENDING, SKIP the 2, and use the 4 ONLY in the sequence 4 - b3 - 3:
A G F# E D C-C# A
In terms of scale degrees, that's 1 b7 6 5 4 b3-3 1
Do the same thing over the other chords:
Over D7, play D Mixolydian as described above:
D E F-F# A B C D (ascending)
D C B A G F-F# D (descending)
Over E7, play E Mixolydian as described above:
E F# G-G# B C# D E (ascending)
E D C# B A G-G# E (descending)
Comment: Of course, there are endless ways to improvise over a 12 bar blues, but this answer seems to mostly ignore the OP question of how to improvise _using a pentatonic minor scale_ in favor of improvising instead using mixolydian scales.


Dataframe output from a for-loop

Question: I am trying to populate the output of a for loop into a data frame. The loop is repeating across the columns of a dataset called "data". The output is to be put into a new dataset called "data2". I specified an empty data frame with 4 columns (i.e. ncol=4). However, the output generates only the first two columns. I also get a warning message: "In matrix(value, n, p) : data length [2403] is not a sub-multiple or multiple of the number of columns [2]"
Why does the dataframe called "data2" have 2 columns, when I have specified 4 columns? This is my code:
a <- 0
b <- 0
GM <- 0
GSD <- 0
data2 <- data.frame(ncol=4, nrow=33)
for (i in 1:ncol(data))
{
    if (i==34) {break}
    a[i] <- colnames(data[i])
    b <- data$cycle
    GM[i] <- geoMean(data[,i], na.rm=TRUE)
    GSD[i] <- geoSD(data[,i], na.rm=TRUE)
    data2[i,] <- c(a[i], b, GM[i], GSD[i])
}
data2

Answer: If you look at the ?data.frame() help page, you'll see that it does not take arguments nrow and ncol--those are arguments for the matrix() function.
This is how you initialize data2, and you can see it starts with 2 columns, one column is named ncol, the second column is named nrow.
data2 <- data.frame(ncol=4, nrow=33)
data2
#   ncol nrow
# 1    4   33

Instead you could try data2 <- as.data.frame(matrix(NA, ncol = 4, nrow = 33)), though if you share a small sample of data and your expected result there may be more efficient ways than explicit loops to get this job done.
Generally, if you do loop, you want to do as much outside of the loop as possible. This is just guesswork without having sample data, these changes seem like a start at improving your code.
a <- colnames(data)
b <- data$cycle ## this never changes, no need to redefine every iteration
GM <- numeric(ncol(data)) ## better to initialize vectors to the correct length
GSD <- numeric(ncol(data))
data2 <- as.data.frame(matrix(NA, ncol = 4, nrow = 33))
for (i in 1:ncol(data))
{
    if (i==34) {break}
    GM[i] <- geoMean(data[,i], na.rm=TRUE)
    GSD[i] <- geoSD(data[,i], na.rm=TRUE)
    ## it's weird to assign a row of data.frame at once...
    ## maybe you should keep it as a matrix?
    data2[i,] <- c(a[i], b, GM[i], GSD[i])
}
data2

Dataframe output from a for-loop


Status bar , how to make my lines,ovals and rects visible?

Question: I have my lines, rectangles and ovals set and I have one question.
How can I make my lines, ovals and rectangles visible in the main method? For some reason they are not being outputted when I compile and run the file... thanks
 import java.awt.BorderLayout;
 import java.awt.Color;

 import java.awt.Dimension;
 import java.awt.Graphics;

 import javax.swing.JPanel;

 public class LinesRectsOvalsJPanel extends JPanel {

public LinesRectsOvalsJPanel() {

setLayout(new BorderLayout());
setPreferredSize(new Dimension(10, 23));

JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.setOpaque(true);

add(rightPanel, BorderLayout.EAST);
   }

  public void paintComponent( Graphics g ) {

   super.paintComponent( g );

     this.setBackground( Color.WHITE );
     //   x y width height
     g.setColor(Color.BLACK);
     g.drawLine(5,10,5,30);
     g.setColor(Color.BLUE);
     g.drawLine(18,70,127,24);
     g.setColor(Color.RED);
     g.drawLine(25,45,100,38);

     g.setColor(Color.YELLOW);
     g.drawOval(23,25,23,55);
     g.setColor(Color.BLACK);
     g.drawOval(15,14,40,78);
     g.setColor(Color.CYAN);
     g.drawOval(180,102,5,90);
     g.setColor(Color.RED);
     g.drawOval(21,20,89,11);
     g.setColor(Color.BLUE);
     g.drawOval(35,87,39,27);
     g.setColor(Color.YELLOW);
     g.fillRect(87,5,5,60);
     g.setColor(Color.GREEN);
     g.fillRect(105,15,15,85);
     g.setColor(Color.CYAN);
     g.fillRect(14,45,76,86);
     g.setColor(Color.RED);
     g.fillRect(70,79,65,86);
     g.setColor(Color.BLUE);
     g.fillRect(90,108,5,8);
 }
}

Code 2
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
 import java.awt.Dimension;
 import java.awt.Graphics;

 import javax.swing.BoxLayout;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.SwingConstants;
 import javax.swing.border.BevelBorder;

  public class Main {
    public static void main(String[] args) {
  JFrame frame = new JFrame("");
 frame.setLayout(new BorderLayout());
frame.setSize(300, 300);

JPanel statusPanel = new JPanel();
statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
frame.add(statusPanel, BorderLayout.SOUTH);
statusPanel.setPreferredSize(new Dimension(frame.getWidth(), 16));
statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
JLabel statusLabel = new JLabel("status");
statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
statusPanel.add(statusLabel);

frame.setVisible(true);
}
}

Comment: 1) *"JSwing"*  No such thing. 2) Please use a consistent and logical indent for code blocks.
Answer: You have not added your LinesRectsOvalsJPanel component:
frame.add(new LinesRectsOvalsJPanel());

Answer: Try something like this:
This is the main class (frame):
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Frame extends JFrame{

    Panel p = new Panel();

    public Frame(){
        p.setPreferredSize(new Dimension(640,480));
        add(p);
    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                Frame f = new Frame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.pack();
                f.setVisible(true);
            }
        });
    }
}

Your panel class:
import javax.swing.*;

public class Panel extends JPanel{
    public void paintComponent(Graphics g){
        g.setColor(Color.BLUE);
        g.fillRect(20, 20, 50, 50);
    }
}


Link id with name

Question: I know that it is quite a simple question but I'm still quite new to R.
If I have two tables like this
id = c(1,2,3,4,5,6)
cost = c(11,22,33,44,55,66)
name =c("aa","bb","cc","dd","ee","ff")

tableone = cbind (id, cost)
tabletwo = cbind(name,id)

how can I add the correct names (as in tabletwo) to tableone with the costs? In reality table1 is much more complex with multiple duplications etc.
Comment: Give a fuller example of the actual data because it sounds like the solution here is `cbind(name,id,cost)` but you imply that it's more complex than that.
Answer: Here's a way using the data.table package:
Load the package:
library(data.table)

Load your vectors as you had them defined:
id <- c(1,2,3,4,5,6)
cost <- c(11,22,33,44,55,66)
name <- c("aa","bb","cc","dd","ee","ff")

Turn the vectors into single column data tables:
id.dt<-data.table(id)
setnames(id.dt,"id")

cost.dt<-data.table(cost)
setnames(cost.dt,"cost")

name.dt<-data.table(name)
setnames(name.dt,"name")

Combine columns, set a key (this might be unneeded for this example, but I included it just to show:
tableone.dt<-cbind(id.dt,cost.dt)
setkey(tableone.dt,id)
tabletwo.dt<-cbind(name.dt,id.dt)
setkey(tabletwo.dt,id)

Merge the tables:
merge(tableone.dt,tabletwo.dt)

Of course this is a round-about way of doing it. If it suits your needs you can easily simplify the process. For example:
simple.table <- data.table("id"=c(1,2,3,4,5,6),"cost"=c(11,22,33,44,55,66),"name"=name <- c("aa","bb","cc","dd","ee","ff"))

or Thomas's comment will work. Also, do see the link commented by flodel.

Link id with name


Log likelihood with exponential function

Question: I'm trying to find the maximum likelihood of this function.
I have samples in this question as follows: (0.77, 0.82, 0.94, 0.92, 0.98)
$$f_Y(y;\theta)=\theta y^{\theta-1} ;, \quad 0 \le y \le 1 ;, 0 \lt \theta$$
$$L(\theta) = \prod\limits_{i=1}^{n} \theta y_i^{\theta-1} \\
= \theta^n\prod\limits_{i=1}^{n}y_i^{\theta-1}$$
From here i'm stuck. I'm not sure if I should take the log now or there is one more move before I take the log. The answer is 8.00. if I put the values it would probably make it easier for me, but it appears to me that I do not know a technique or identity from here to move on.
Any help?
Continuing on:
$$\text{Let }\; T = \frac{\partial}{\partial \theta}\bigg(n\ln \theta + (\theta -1)\sum_{i=1}^n \ln y_i\bigg) = \frac{n}{\theta} + \sum_\limits{i=1}^{n}ln(y_i) \\
\text{Let } \; T = 0 = \frac{n}{\theta} -0.261 - 0.198 - 0.083 - 0.061 - 0.02 \\
\text{implies } \hat{\theta} = \frac{5}{0.623} = 8$$
Comment: The MLE of the $\text{Beta}(\theta,1)$ distribution was derived [here](https://math.stackexchange.com/questions/2649775/mle-maximum-likelihood-estimator-of-beta-distribution?rq=1).
Comment: Thankyou @StubbornAtom that was a helpful article on R and bootstrap too.
Answer: Continuing from where you left it, it simply is :
$$ L(\theta) = \theta^n\prod\limits_{i=1}^{n}y_i^{\theta-1} \Rightarrow \ln (L(\theta)) =
\ln\bigg(\theta^n\prod\limits_{i=1}^{n}y_i^{\theta-1}
\bigg) \Leftrightarrow \ln(L(\theta)) = n\ln\theta + \ln\bigg(\prod_{i=1}^ny_i^{\theta-1}\bigg) $$
$$\Leftrightarrow$$
$$\ln(L(\theta)) = n \ln \theta + \sum_{i=1}^n \ln(y_i^{\theta-1}) \Leftrightarrow \ln(L(\theta)) = n\ln \theta + (\theta -1)\sum_{i=1}^n \ln y_i$$
To find the MLE for the given function, differentiate by $\theta$ and solve with respect to $\theta$ so you get the estimator $\hat{\theta}$:
$$\frac{\partial \ln(L(\theta))}{\partial \theta} = \frac{\partial}{\partial \theta}\bigg(n\ln \theta + (\theta -1)\sum_{i=1}^n \ln y_i\bigg) \Leftrightarrow \dots$$
Finally, calculate the MLE values for the given points you have but remember, the likelihood is yielded by using the $\ln$ function, which means that your final values should be exponanted to $e$.
Comment: $ \ln\bigg(\prod_{i=1}^ny_i^{\theta-1}\bigg) = \sum_{i=1}^n \ln(y_i^{\theta-1})$, good one. Thankyou @Rebellos
Comment: I got that to the answer in the book, but what you're saying is, although that might satisfy the book in interrogating knowledge of the maximum likelihood process, the figure is not really useful in that form until it is brought back into the same "units".


How to make the name of an organic group unique

Question: can someone explain how to make groupnames unique? If i create 2 groups with the same name, i want to become a validation error. Actually i tested og in the commons distribution an there i can create groups with same names, but in the list of all groups are only one of the same groups listed.
Thanks for your help.
Greats Daniel
Answer: Assuming your og groups are based on node entities.

You can have unique group titles with help of unique_field module.
With help of this module you can set the title field as unique fields the content type generating groups on your site.
Comment: I will have a look at this module. But i am not sure if a unique check of a string field is case insensitive. For example 2 groups named "group_a" and "group_A" they are unique but just because the chaeck is case sensitive. Is there a way to say that the unique check should be case insensitive?
Comment: Could it be an option to call a validation function before saving the organic group entity? Where i can programmatically perform a unique check?
Comment: It pretty much depends on type of database you are using, if its mysql then go ahead and give a try to this module.
Comment: The reason I am saying is
node title field in database is varchar(that means , nonbinary string comparison i.e. case insensitive) and utf_general_ci (ci itself says case insensitive).
Comment: ok, thank you. I have check the database setting and you are right. So i will try it with the unique_field module.


How do I set the default values for creating a new content type?

Question: This is a follow up to my question in Can I change the Display Settings by modifying the database? which was answered. Basically, I could not get to the 'display authoring and date information' checkbox because of a module installed. Running the command 'drush cedit node.type.article' as given in @4k4's answer allows me to change this setting.
However, when a new content type is created, the type has display_submitted: true set meaning I have to go in and change it whenever a new content type is created, which I sometimes cannot do.
Is there a way to change the default settings so that any new content type has display_submitted: false by default?
Answer: For content or config entities you can set default values in a hook_ENTITY_TYPE_create():
use Drupal\node\Entity\NodeType;

/**
 * Implements hook_ENTITY_TYPE_create() for 'node_type'.
 */
function mymodule_node_type_create(NodeType $node_type) {
  $node_type->set('display_submitted', FALSE);
}

Comment: So do I still have to do this for every new content type? If not, where exactly do I use this code?
Comment: No, once this hook is implemented it is called automatically when new content types are created. Put it in the file `mymodule.module` of a custom module, replace mymodule with your module name.
Comment: That does the trick! I didn't realize I had two write a module to do this (it might be a good idea to mention that in your answer, although people more familiar with drupal than me would already know that).


Is any countable subset of an uncountable set closed?

Question: Consider uncountable nonempty $X\subset\mathbb{R}^k$. I wonder if I choose $E\subset X$ such that $E={p_1,p_2,...}$ countable and infinite, then $E$ must be closed in X.
Also, if I choose such set in spaces other than Euclidean space, can I say any countable subset of an uncountable set must be closed?
Comment: $\Bbb Q{{{}}}$?
Answer: Not in general, no. For example, consider
$$
\left\{\frac{1}{n} : n \in \mathbb{N}\right\} \subset \mathbb{R}.
$$
Answer: When you consider your favourite sequence $a_1,a_2,\cdots$, be it in $\Bbb R^k$ or in another topological space, you typically don't expect its limit (or any of its limit points) to be one of the $a_i$-s.


What can I conclude from the following Granger causality test?

Question: I have two monthly time series:

one for house prices expressed in annual change growth rates: $\left( \text{ln}(X_t) - \text{ln}(X_{t-12})\right) - \left( \text{ln}(X_{t-1}) - \text{ln}(X_{t-1-12})\right)$;
the other simply in growth rates: $\text{ln}(X_t) - \text{ln}(X_{t-1})$.

Here is the data:
House Prices = [1]  0.009189829  0.022612618  0.003952796 -0.015179184  0.001903336 -0.028779902  0.025668239 -0.011237850
  [9]  0.014782630 -0.018844480 -0.023547458  0.020613233  0.029281069 -0.010539781  0.006707366  0.023693144
 [17] -0.002632498  0.148738752 -0.154539337  0.013908319 -0.002294980  0.013274177  0.010043605 -0.007862785
 [25] -0.018297295 -0.003167249  0.022984841  0.001666694 -0.001310199 -0.131548705  0.114723242 -0.003431495
 [33]  0.000953231 -0.010096108 -0.009434595 -0.037774255  0.030877947 -0.011245971 -0.018800312 -0.012805013
 [41]  0.001326392 -0.012034079 -0.045279346 -0.017308170  0.002490863 -0.007340975  0.005052948 -0.024053201
 [49] -0.004190424 -0.028607790  0.004678486  0.026626293 -0.015166864  0.006988983  0.038257855  0.020798177
 [57]  0.008175391  0.021294030 -0.013331432  0.030969145  0.017065249 -0.002672683  0.019435476 -0.037047871
 [65]  0.001844432  0.007663458  0.034406137 -0.049379845 -0.012527106 -0.012859680  0.012954488 -0.015463951
 [73] -0.025509006  0.006318645  0.012977464  0.019940525 -0.025592828  0.020774198 -0.033613414  0.018338077
 [81]  0.001765807  0.009236604 -0.041413104  0.030227358  0.017180849  0.012593360 -0.039001526 -0.004994992
 [89]  0.037766071 -0.043167230 -0.016613786  0.023199890 -0.016214873 -0.012282560  0.065978520 -0.031465767
 [97]  0.006355108 -0.000449523 -0.005810647  0.016823517 -0.021988463  0.026178014  0.007654339 -0.008356379
[105]  0.013273736  0.031645473 -0.046408064  0.022334664  0.008517194 -0.014892335  0.019147342  0.007955040
[113]  0.014122506 -0.035722162  0.018174284  0.021410306 -0.038943797 -0.014517888  0.032750195  0.022506553
[121] -0.003870785  0.130924075 -0.057934974 -0.174228244  0.016937619  0.010647759  0.015691962 -0.033174094
[129]  0.038263205  0.003456250 -0.013422897

B = [1] -0.0223848461  0.0102749646  0.0913403867 -0.0758207770 -0.0053898407 -0.0204047336  0.0050358986
  [8]  0.0195335195 -0.0200303353 -0.0045390828  0.0056380761 -0.0004492945  0.0040043649  0.0012918928
 [15] -0.0104850394  0.0047110190  0.0049805985 -0.0046957178  0.0095002549  0.0202597343 -0.0183526932
 [22]  0.0237185217 -0.0137022065  0.0133787918 -0.0212629487  0.0070512978  0.0959447868 -0.0801519036
 [29] -0.0362526334 -0.0000278572  0.0269014993  0.0009862920 -0.0329868357  0.0283667004 -0.0135186142
 [36] -0.0004975495  0.0053822189  0.0108219907 -0.0078419784  0.0418340658 -0.0316367599 -0.0092324801
 [43] -0.0192830637  0.0336003682  0.0021479539 -0.0146426306  0.0003717930  0.0216259502 -0.0323127786
 [50]  0.0033077606 -0.0123735085 -0.0014757035  0.0266339779 -0.0228959378  0.0002848944  0.0133572802
 [57] -0.0093035312 -0.0034350607  0.0052349772  0.0115210916 -0.0122443122  0.0435497970 -0.0100099291
 [64]  0.0267252321 -0.0654005679  0.0088385287 -0.0089122237  0.0155299273 -0.0027394997 -0.0126183268
 [71]  0.0090999709  0.0017039487 -0.0144843611  0.0269128625  0.0042663583  0.0220574344 -0.0523831016
 [78] -0.0059331639  0.0171559908  0.0125030653  0.0151902738  0.0471484001 -0.0477394702  0.0888317354
 [85] -0.1044700154  0.0234134906 -0.0215966718  0.0157974035  0.0970094980 -0.1049559862 -0.0290578406
 [92]  0.0617653831 -0.0132202439  0.0022117274  0.0091225692  0.0424813190 -0.0614889434  0.0163745828
 [99] -0.0112793057  0.0666179349 -0.0352838073 -0.0259179501  0.0269557599  0.0127882202 -0.0430512536
[106]  0.0862308560 -0.0633012329  0.0596481270  0.0900367605 -0.0303162498 -0.0153738373 -0.0442218848
[113] -0.0116158350 -0.0531058308  0.2036373944  0.1598602057 -0.3837940703 -0.0069112146 -0.0192015196
[120]  0.0110269191 -0.0351135484  0.0439917033  0.0522746614  0.0036354828 -0.0414276671 -0.0361649669
[127]  0.0080753079  0.0352684982 -0.0282391428 -0.0141622744  0.0045799464

I am studying if B has an effect on House prices. For this reason first a take a simple liner regression between the two and I get a negative and significant estimate at the 95% confidence interval: (-0.0004189 *).
Wanting to reach a step forward I undertake a Granger causality test as following:
I) Determine the optimal number of lags using the AIC/BIC test using:
select.lags<-function(x,y,max.lag=20) {
  y<-as.numeric(y)
  y.lag<-embed(y,max.lag+1)[,-1,drop=FALSE]
  x.lag<-embed(x,max.lag+1)[,-1,drop=FALSE]

  t<-tail(seq_along(y),nrow(y.lag))

  ms=lapply(1:max.lag,function(i) lm(y[t]~y.lag[,1:i]+x.lag[,1:i]))

  pvals<-mapply(function(i) anova(ms[[i]],ms[[i-1]])[2,"Pr(>F)"],max.lag:2)
  ind<-which(pvals<0.05)[1]
  ftest<-ifelse(is.na(ind),1,max.lag-ind+1)

  aic<-as.numeric(lapply(ms,AIC))
  bic<-as.numeric(lapply(ms,BIC))
  structure(list(ic=cbind(aic=aic,bic=bic),pvals=pvals,
                 selection=list(aic=which.min(aic),bic=which.min(bic),ftest=ftest)))
}

s<-select.lags(Topic.15,House.Prices,20)
t(s$selection)
plot.ts(s$ic)

As a result I get:
aic bic ftest
14  12  13

Here is when it comes the first doubt: why are they giving me different results? Nevertheless, when I do the Granger causality test for both directions using these numbers as possible lags I get in all high significant results (***) only in the direction that B is causing House prices movements:
lmtest::grangertest(Topic.15,House.Prices,12)
lmtest::grangertest(House.Prices,Topic.15,12)

I do not seem to see the direction of the cause, is it possitive or negative (an increase in B produces an increase or a drop in House prices at time $t+1$?).
Another question, is the conclusion valid that changes in B produce changes in House prices? What are the weakness in this line of argument?
Answer:
why are they giving me different results?

Why should they not give you different results? AIC and BIC are not the same. They serve different goals and are optimal in different ways (e.g. AIC is efficient while BIC is consistent). If you want to use the model for prediction, you could prefer AIC due to its efficiency. For explanatory modelling, BIC could be chosen for its consistency.
In any case, the models are quite large for a sample size of mere 131 observation. Since you could expect monthly seasonality here, consider using just the first few lags plus the lag number 12, or seasonally adjust the data in advance and rerun the lag order selection. Edit Responding to a relevant comment: Since you used seasonal differencing when constructing the Housing price time series, there is probably no need to look for seasonality there.

I do not seem to see the direction of the cause, is it possitive or negative (and increase in B produces an increase or a drop in House prices at t+1?)

Take a look at impulse response functions (IRFs). Consider a positive impulse to B and see how House prices react over time. You could do that using "vars" package in R. Select model order (may do it manually or use function VARselect), estimate the model (function VAR) and extract impulse responses (function irf).

is the conclusion valid that changes in B produce changes in House prices? what are the weakness in this line of argument?

If you conclude that B Granger-causes House prices, the model must have shown (with, say, 95% confidence) that indeed changes in B produce changes in House prices. If you believe the model is alright and there are no hidden problems (e.g. there is no hidden factor that causes both B and House prices), the argument is fine.
Note: I did not proof-read your code but I assume it does what you say it does.
Comment: What exactly do you mean? Are you having a difficulty using the `irf` function, or do you want to do the analysis by hand? If the latter, forecast from the model a few steps ahead using $\varepsilon_{t+1,B}=\sigma_B$ (one standard deviation) but all the other shocks to `B` and to `House price` equal to zero.
Comment: Great answer @Richard, thanks a lot! since House prices are computed as the the first difference in the annual change of each observations which after computing the acf() showed no seasonality this should not be a problem right. Also, variable B has no seasonality nor trend.
Comment: Oh, I did not take the time to think about that. Indeed, seasonal differencing (as you appear to be doing) should remove seasonality. But why then does AIC and BIC suggest such a high lag order for such a small sample?.. You could double check by using `VARselect`, perhaps there is a mistake in your code.
Comment: By the way, any idea on how to code the positive impulse to B and its reaction to House prices through impulse response functions in R?


Ignite select queries (including nested select) on replicated caches - What if any processing happens on the query initiator?

Question: Few questions,

For select queries on replicated caches is it true that NO processing happens on the query initiator  (say in an ignite client node when querying from application in the case of application embedded ignite client). including when selecting only a subset of the 'fields/columns' and 'entries/rows' from a cache?
In the case of nested select queries all involving replicated caches (either a select nested in a select or a select nested in an insert where some aggregation is done on the nested selected data before insert) is it still true that NO processing happens on the query initiator?
If 1 & 2 is true is it still true even when this is performed using ignite spring data abstraction (with @query annotation)?

Thanks
lmk
CLARIFICATION UPDATE (June 17th, 2020):
Specifically, what processing can we not avoid on the client?. Reason i ask is for example in the second case i mentioned above i want to see what is the exact network & client (when query initiator is a client in this case) overhead when executing nested select queries all involving replicated caches particularly if they act over large rows/entities or perform computationally intensive operations repeatedly.
Comment: Thanks @Alexandr Shapkin, can you please elaborate on this point you note on replicated caches "but can't avoid any processing on a client since it can't store the data"? What processing can we not avoid on the client?. Reason i ask is for example in the second case i mentioned above i want to see what is the exact network & client (when query initiator is a client in this case) overhead when executing nested select queries all involving replicated caches particularly if they act over large rows/entities or perform computationally intensive operations repeatedly.
Comment: @Alexandr Shapkin hope you will get a chance to look at my answer to your question in the previous comment above. I am copy pasting here again, "Reason i ask is for example in the second case i mentioned above i want to see what is the exact network & client (when query initiator is a client in this case) overhead when executing nested select queries all involving replicated caches particularly if they act over large rows/entities or perform computationally intensive operations repeatedly."
Answer: An SQL query is executed in a map-reduce fashion, in general, the reduce phase does happen on a query initiator that might be a client or a server node. Replicated caches help you to perform the whole execution on a single node, but can't avoid any processing on a client since it can't store the data.
Could you please explain, why is it important for you/are there any issues with it?
Comment: Thanks @Alexandr Shapkin, can you please elaborate on this point you note on replicated caches "but can't avoid any processing on a client since it can't store the data"? What processing can we not avoid on the client?. Reason i ask is for example in the second case i mentioned above i want to see what is the exact network & client (when query initiator is a client in this case) overhead when executing nested select queries all involving replicated caches particularly if they act over large rows/entities or perform computationally intensive operations repeatedly.


Linux router: User friendly, web-based traffic monitor app

Question: This is not a tech question but I hope someone can help me out.
I've been searching all over the web for an app like this, but I can't seem to find one:

Friendly user interface where a non-technical person can manage LAN computers.
Set individual download quotas. After that quota is reached, disable the internet connection for that specific host.

This app must not necessarily be free, but if it is, I wouldn't mind. :)
Comment: Product recommendations are off-topic on here. The functionality you seem to be looking for is something I associate with network management tools for hotels and WiFi hot-spots for customers. One potential search term is [Captive_portal](http://en.wikipedia.org/wiki/Captive_portal) technology.
Answer: Check out http://www.ntop.org/ . It has comprehensive traffic stats and web interface easy to use by non-technical person.
The part with quotas is not that trivial, but ntop can certainly provide statistics based on which block can be applied.
Comment: Thanks for the answer. I am familiar with ntop, but unfortunatelly, it doesn't really do what I need...


Apply a Report filter in code

Question: My particular, specific issue is probably too localized, but the general question I'm about to ask is something I'm sure others will ask and have wondered:
General question:  In Sql Server Reporting Services, is it possible to apply a filter to a report in code?  If not, is it possible to use branching in the report filter based on the value of a variable, and can you point me to documentation or explain how to do it.

My specific example follows, to expand on what I mean by the above, in case I worded it badly:
I'm learning SSRS and the docs and Google are coming up short.
The desired effect is that we have a report based on an incident tracking system. In this system, we have various teams that can track incidents:  IT Ops, Development, Security, etc.  Each of these teams have team members assigned.

We have a base report that displays ALL incidents.
We have added a boolean parameter named "LimitByTeam", which produces a CheckBox on the report as you'd expect.
We have added a String parameter that accepts multiple values.  The allowed values come from a data set that lists the teams.  This has added the expected drop-down list to the report, allowing users to select one or more teams.
We added a dataset that contains team and team member.

If the CheckBox is NOT selected, we want to display all incidents. (The default)
If it IS selected, we want to have the  report to filter based on the login ID of the person who created the incident ticket.
Were I to do this in SQL, I'd do it as
Select
   (ticket fields)
From
   Table
WHERE TicketCreator IN (
             Select LoginId FROM TeamMembersTable
             WHERE TeamName in ('IT Ops', 'Developers'))

In SQL, or in VB, etc, this would be simple.
In SSRS I'm not figuring out quite how to do this.  I've gotten as far as figuring out that I can use Custom Code to do more complex logic using VB (and it appears to be VB.NET.  HOORAY! Familiar territory)
So I've added custom code and verified that I can read the value of the report parameter, but I can't figure out for the life of me how to apply a filter if the parameter value is True.  Here's what I've got.
Public Sub ApplyTeamFilter()

    ' Report.Parameters("LimitByTeam") is a
    ' boolean report parameter that I'm able to access
    ' so I've got the IF statement worked out

    If Report.Parameters("LimitByTeam").Value = True Then
                 ' Pseudo-code - I'm looking for something like Report.Filters.Add(filterstatement)
                 ' Alternatively a way to change this to a function to return a list of items from
                 ' the Team MembersTable table and use it in a custom expression.
     End If
End Sub

The problem is that I can't seem to find a Filters property on the Report object, or any method that lets me find it.  Since I couldn't find it there, I expanded my search to everything in this section of the MSDN library and can't find it, or anything even remotely resembling a way to do what I'm attempting.
I'm also trying to do something like this because I think I see a way to use this function:
Public Function IsLoginIdInTeam(ByVal LoginId as String, byVal Team As String) As Boolean

    '  Report.Parameters("LimitByTeam") is a
    ' boolean report parameter that I'm able to access
    ' so I've got the IF statement worked out

    If Report.Parameters("LimitByTeam").Value = False Then
        Return True ' Default
    Else
        ' Access the TeamMembers table and look for a match
                ' Something like
                ' Convert.ToBoolean(Report.DataSets!TeamMembers.Compute(Count, "TeamName = '" & Team & "' AND LoginId = '" & LoginId & "'")

     End If
End Function

But I can't figure out how to access the Data Sets in the report, either, much less the syntax for interacting with them.  The pseudocode works with a System.Data.DataTable, but I suspect SSRS DataSets are a different beast.
Am I missing something blindingly obvious?  Should I be giving up on filtering this way in the report, and try another track, like changing the original query in the DataSet?
Comment: Really, if I'm being dumb and should be doing something COMPLETELY different, let me know.  I'm so used to standard .NET development that sometimes I expect everything to be similar, and I miss the obvious.  I wonder if I'm asking something dumb because of it - like someone used to riding a bike, trying to learn to ride a skateboard and saying "Help!  I can't find the handlebars!"  I'm usually good at researching how to do things, if I have a general idea of where to look and what to look for.  I'm just completely out of my element with SSRS.
Comment: No need for custom code. You can use these parameters in SQL, passed through SSRS. I'll write up a quick sample and add an answer in the next few minutes.
Answer: I'm not a huge fan of boolean parameters in SSRS (or BIT in SQL for that matter.) You can't write things like
WHERE @MyBool OR @MyOtherBool

It needs to be
WHERE @MyBool = 1 or @MyOtherBool = 1

So if I have an SSRS report with a boolean called MyBoolParam and a multivalue text parameter called MyMultiSelectTextParam , a SQL query like this will work:
SELECT
   MyField,
   MyOtherField
FROM
   MyTable t
WHERE
   @MyBoolParam = 1
   OR
   t.MyField IN ( @MyMultiSelectTextParam)

(My preferred alternative to boolean parameters is actually a string with possible values, but it would be handled in the query in the same way.)
If you can't easily change your SQL query (such as using a third party SP) then you can also accomplish the same thing by adding a filter to the SSRS DataSet. Right click on the DataSet in the Report Data pane and look at the Dataset Properties. The Filters pane will let you add criteria that are evaluated for each row. I haven't used these as much with multivalue parameters, but the IN Operator should work for that.
Comment: Darn if that's not easier.  I AM that kid on a skateboard looking for handlebars.  Thank you.  I'll go try it now.
Comment: A+ for effort? SSRS is a pretty decent product. Mostly the things that should be easy, really are. Most of the exceptions I can think of off the top of my head are related to page layout. The data query and filtering generally work well. (I'm going to add some alternative ways to accomplish this to my answer.)

Apply a Report filter in code


How to increment date object hours with omitting weekend days

Question: I'm making a deadline calculator function in JavaScript using Date object.
I have a date and a turnaround time for inputs, and the algorithm gives back the deadline in a date. It works now fine, however when I increment the date object with a turnaround time big enough to start a new week, it includes the weekend days as well. This crushes my whole algorithm, because I don't want to count the weekends as valid working hours.
Can I somehow make a setting on the Date object, to omit weekend days? For example:
0 = "Sunday";
1 = "Monday";
2 = "Tuesday";
3 = "Wednesday";
4 = "Thursday";
5 = "Friday";
6 = "Saturday";

Thursday date + 24 hours => Friday date. But Friday date + 72 hours => Monday date
instead of Wednesday date.

How could I calculate the deadline without using the weekend days, so when I give 72 hour to a Friday date for example, I would not get Monday, but Wednesday instead? Like if the Date object would jump weekend days over.
Comment: It's not hard at all without a library, see [*Add Working Days Using Javascript*](http://stackoverflow.com/questions/40739059/add-working-days-using-javascript) ([*and many others*](http://stackoverflow.com/search?q=%5Bjavascript%5D+working+days)). You really should post what you've tried.
Comment: Possible duplicate of [How to exclude weekends between two dates using Moment.js](http://stackoverflow.com/questions/20788411/how-to-exclude-weekends-between-two-dates-using-moment-js)
Comment: @Julian Yeah, I'm not using Moment.js
Comment: Good point, although it is recommend to use Moment.js
Comment: I think it would be hard to do this without Moment.js, and why reinvent the wheel?
Comment: Okay, I'm going to dig into it. However my algorithm is already implemented, using Date object. I figured, maybe converting the turnaround hours into (days + hours), and first incrementing the days may give a chance to skip the weekends.
Comment: @Julian recommended by who? Yes, I recommend it but you didn't ask me.
Comment: Btw I absolutely do not get the downvote's reason. This is a valid question, and far from trivial.
Answer: Okay, trying to follow Clean Code conception, I've put the whole algorithm into separete functions. I'm ommiting a couple of other functions, the main part is covered by the following:
function incrementWithTime(submit, days, hours_to_increment) {
    let date = new Date(submit);
    repeatThis(days) (() => date = pushToNextWeekIfReachedWeekend(new Date(date.getTime() + (24*1000*3600))));
    date = new Date(date.getTime() + (hours_to_increment*1000*3600));
    return date;
}

This increments the Date object day-by-day. Why do we need this? Well, I had to know where the time interval reaches a weekend day, so I can 'step' on it, and skip, using the pushToNextWeekIfReachedWeekend() method:
function pushToNextWeekIfReachedWeekend(submit) {
    let date = new Date(submit);
    let day = date.getDay();
    const saturday = 6, sunday = 0;

if(day == saturday)
    date.setDate(date.getDate() + 2);
if(day == sunday)
    date.setDate(date.getDate() + 1);
return date;
}

Now, if you wonder how the reapeatThis() works:
const repeatThis = x => f => {
    if (x > 0) {
        f();
        repeatThis(x - 1)(f);
    }
}


How to add link list to JQuery Mobile Panel?

Question: I am trying to add some buttons to j Query Mobile panel that would take me to the video div that is the sibling of home div. I did the following codes but it won't work.

    Videos
    Close

Comment: it wont open the video div ..
Comment: but if i add "http://m.facebook.com" instead of "#video" , it sends me to facebook ...
Comment: Add data-ajax=false to anchor.
Comment: I have another issue now:
I clicked on Videos, it took me to the videos page, and when I click "back" button and come back to the "home" page again. the panel button wont work code of panel button -->  Menu
Comment: This is another issue, please post another question with more details on the problem.
Answer: Add data-ajax="false" attribute to anchor, if you want to scroll to a div within the active page.

  Videos


Calling method with IEnumerable sequence as argument, if that sequence is not empty

Question: I have method Foo, which do some CPU intensive computations and returns IEnumerable<T> sequence. I need to check, if that sequence is empty. And if not, call method Bar with that sequence as argument.
I thought about three approaches...

Check, if sequence is empty with Any(). This is ok, if sequence is really empty, which will be case most of the times. But it will have horrible performance, if sequence will contains some elements and Foo will need them compute again...
Convert sequence to list, check if that list it empty... and pass it to Bar. This have also limitation. Bar will need only first x items, so Foo will be doing unnecessary work...
Check, if sequence is empty without actually reset the sequence. This sounds like win-win, but I can't find any easy build-in way, how to do it. So I create this obscure workaround and wondering, whether this is really a best approach.

Condition
var source = Foo();

if (!IsEmpty(ref source))
    Bar(source);

with IsEmpty implemented as
bool IsEmpty<T>(ref IEnumerable<T> source)
{
    var enumerator = source.GetEnumerator();

    if (enumerator.MoveNext())
    {
        source = CreateIEnumerable(enumerator);
        return false;
    }

    return true;

    IEnumerable<T> CreateIEnumerable(IEnumerator<T> usedEnumerator)
    {
        yield return usedEnumerator.Current;

        while (usedEnumerator.MoveNext())
        {
            yield return usedEnumerator.Current;
        }
    }
}

Also note, that calling Bar with empty sequence is not option...
EDIT:
After some consideration, best answer for my case is from Olivier Jacot-Descombes - avoid that scenario completely. Accepted solution answers this question - if it is really no other way.
Comment: It heavily depends on what `Foo` does exactly. `Any()` should only consume one element. If `Foo` uses `yield return` to compute the elements it will have to recompute only one element.
Comment: You mean: `Foo` will need them compute again?
Comment: @OlivierJacot-Descombes Ah, thanks, fixed :-)
Comment: `Foo` need to initialize some resources, parse XML file and fill some HashSets, which will be used to filter (yield) returned data. This is not option unfortunately..
Comment: What exactly is your question?  You would like to "peek" the first value of the enumerable, but that's not part of the interface, so you start to enumerate it, and if you get at least one value, you create a combined enumerable with the first value concatenated with the yet-to-be-evaluated remainder of the enumeration.  The only problem I see is that you are not disposing the enumerator, see https://blogs.msdn.microsoft.com/dancre/2008/03/15/yield-and-usings-your-dispose-may-not-be-called/
Comment: @dbc Yep, that is my question and thanks for letting me know about that disposable problem in my code...
Comment: This question belongs in code review. The code works.
Comment: If you can change your `Foo()` method to return [`IReadOnlyCollection`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.ireadonlycollection-1?view=netframework-4.7.2) instead of `IEnumerable` then that would be quite efficient. An `IReadOnlyCollection` comprises an `IEnumerable` and a `Count` (and nothing else).
Comment: @MatthewWatson I can't change `Foo` to return `IReadOnlyCollection`, because in most cases, not all items in that collection will be uses, so their computation will be useless...
Comment: @OlivierJacot-Descombes You're assuming 1) that computing each element takes the same amount of time.  Often the first element is much more expensive than the rest 2) That the sequence computes the same values each time it's iterated 3) That iterating the sequence, even in part, doesn't cause side effects.  Those assumptions are violated by all sorts of sequences.
Comment: Why can you not implement `Bar` such that it simply behaves properly when passed an empty sequence?  This is almost always the preferable design.
Comment: @Servy This is addressed in the second part of my answer.
Answer: I don't know whether your algorithm in Foo allows to determine if the enumeration will be empty without doing the calculations. But if this is the case, return null if the sequence would be empty:
public IEnumerable<T> Foo()
{
    if (<check if sequence will be empty>) {
        return null;
    }
    return GetSequence();
}

private IEnumerable<T> GetSequence()
{
    ...
    yield return item;
    ...
}

Note that if a method uses yield return, it cannot use a simple return to return null. Therefore a second method is needed.
var sequence = Foo();
if (sequence != null) {
    Bar(sequence);
}

After reading one of your comments

Foo need to initialize some resources, parse XML file and fill some HashSets, which will be used to filter (yield) returned data.

I suggest another approach. The time consuming part seems to be the initialization. To be able to separate it from the iteration, create a foo calculator class. Something like:
public class FooCalculator<T>
{
     private bool _isInitialized;
     private string _file;

     public FooCalculator(string file)
     {
         _file = file;
     }

     private EnsureInitialized()
     {
         if (_isInitialized) return;

         // Parse XML.
         // Fill some HashSets.

         _isInitialized = true;
     }

     public IEnumerable<T> Result
     {
         get {
             EnsureInitialized();
             ...
             yield return ...;
             ...
         }
     }
}

This ensures that the costly initialization stuff is executed only once. Now you can safely use Any().
Other optimizations are conceivable. The Result property could remember the position of the first returned element, so that if it is called again, it could skip to it immediately.
Comment: Your "question" actually does not contain any question (ending with a question mark).
Comment: That `EnsureInitialized` logic seems like best approach for my case, and I will probably go that way. Thanks. But I will check other answer as accepted, since this is not answering the original question...
Answer: You would like to call some function Bar<T>(IEnumerable<T> source) if and only if the enumerable source contains at least one element, but you're running into two problems:

There is no method T Peek() in IEnumerable<T> so you would need to actually begin to evaluate the enumerable to see if it's nonempty, but...
You don't want to even partially double-evaluate the enumerable since setting up the enumerable might be expensive.

In that case your approach looks reasonable.  You do, however, have some issues with your imlementation:

You need to dispose enumerator after using it.
As pointed out by Ivan Stoev in comments, if the Bar() method attempts to evaluate the IEnumerable<T> more than once (e.g. by calling Any() then foreach (...)) then the results will be undefined because usedEnumerator will have been exhausted by the first enumeration.

To resolve these issues, I'd suggest modifying your API a little and create an extension method IfNonEmpty<T>(this IEnumerable<T> source, Action<IEnumerable<T>> func) that calls a specified method only if the sequence is nonempty, as shown below:
public static partial class EnumerableExtensions
{
    public static bool IfNonEmpty<T>(this IEnumerable<T> source, Action<IEnumerable<T>> func)
    {
        if (source == null|| func == null)
            throw new ArgumentNullException();
        using (var enumerator = source.GetEnumerator())
        {
            if (!enumerator.MoveNext())
                return false;
            func(new UsedEnumerator<T>(enumerator));
            return true;
        }
    }

    class UsedEnumerator<T> : IEnumerable<T>
    {
        IEnumerator<T> usedEnumerator;

        public UsedEnumerator(IEnumerator<T> usedEnumerator)
        {
            if (usedEnumerator == null)
                throw new ArgumentNullException();
            this.usedEnumerator = usedEnumerator;
        }

        public IEnumerator<T> GetEnumerator()
        {
            var localEnumerator = System.Threading.Interlocked.Exchange(ref usedEnumerator, null);
            if (localEnumerator == null)
                // An attempt has been made to enumerate usedEnumerator more than once;
                // throw an exception since this is not allowed.
                throw new InvalidOperationException();
            yield return localEnumerator.Current;
            while (localEnumerator.MoveNext())
            {
                yield return localEnumerator.Current;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}

Demo fiddle with unit tests here.
Comment: Unfortunately with or without dispose, the enumerable created from enumerator won't work if enumerated more than once by the consumer.
Comment: @IvanStoev - Well, that's true.  Maybe that should be protected against, let me think a bit...
Comment: @IvanStoev So.. if I call (in `Bar`) for example `.Any()` two times, the second call will fail?
Comment: @MatějPokorný - actually the behavior may be undefined since `usedEnumerator` will have been exhausted by the first enumeration.
Comment: @MatějPokorný Indeed. Because the enumerator will already be moved / disposed by the previous call.
Comment: @MatějPokorný - answer updated.  But overall the *After reading one of your comments...* approach by Olivier Jacot-Descombes [here](https://stackoverflow.com/a/53583118/3744182) seems less fragile.
Comment: @dbc thanks. I will keep that as accepted answer with some notes about that alternative in my question.
Answer: The accepted answer is probably the best approach but, based on, and I quote:

Convert sequence to list, check if that list it empty... and pass it to Bar. This have also limitation. Bar will need only first x items, so Foo will be doing unnecessary work...

Another take would be creating an IEnumerable<T> that partially caches the underlying enumeration. Something along the following lines:
interface IDisposableEnumerable<T>
    :IEnumerable<T>, IDisposable
{
}

static class PartiallyCachedEnumerable
{
    public static IDisposableEnumerable<T> Create<T>(
        IEnumerable<T> source,
        int cachedCount)
    {
        if (source == null)
            throw new NullReferenceException(
                nameof(source));

        if (cachedCount < 1)
            throw new ArgumentOutOfRangeException(
                nameof(cachedCount));

        return new partiallyCachedEnumerable<T>(
            source, cachedCount);
    }

    private class partiallyCachedEnumerable<T>
        : IDisposableEnumerable<T>
    {
        private readonly IEnumerator<T> enumerator;
        private bool disposed;
        private readonly List<T> cache;
        private readonly bool hasMoreItems;

        public partiallyCachedEnumerable(
            IEnumerable<T> source,
            int cachedCount)
        {
            Debug.Assert(source != null);
            Debug.Assert(cachedCount > 0);
            enumerator = source.GetEnumerator();
            cache = new List<T>(cachedCount);
            var count = 0;

            while (enumerator.MoveNext() &&
                   count < cachedCount)
            {
                cache.Add(enumerator.Current);
                count += 1;
            }

            hasMoreItems = !(count < cachedCount);
        }

        public void Dispose()
        {
            if (disposed)
                return;

            enumerator.Dispose();
            disposed = true;
        }

        public IEnumerator<T> GetEnumerator()
        {
            foreach (var t in cache)
                yield return t;

            if (disposed)
                yield break;

            while (enumerator.MoveNext())
            {
                yield return enumerator.Current;
                cache.Add(enumerator.Current)
            }

            Dispose();
        }

        IEnumerator IEnumerable.GetEnumerator()
            => GetEnumerator();
    }
}

Answer: If you can change Bar then how about change it to TryBar that returns false when IEnumerable<T> was empty?
bool TryBar(IEnumerable<Foo> source)
{
  var count = 0;
  foreach (var x in source)
  {
    count++;
  }
  return count > 0;
}

If that doesn't work for you could always create your own IEnumerable<T> wrapper that caches values after they have been iterated once.
Comment: `Bar` can't be changed. It is external code and calling it means, that some error occured.
Comment: Creating a `IEnumerable` wrapper that caches values when they are iterated is probably your best choice then.
Comment: Example this cached `IEnumerable` implementation looks pretty solid https://github.com/ayende/rhino-etl/blob/master/Rhino.Etl.Core/Enumerables/CachingEnumerable.cs (BSD-3 license)
Comment: It looks... I'm glad, that I'm not alone, who need something like this. Thanks for letting me know about that...
Comment: It seems that implementation expect `IEnumerable` to be iterared completely when it's first iterated. So it won't work in your case because `.Any()` only iterates first element. Maybe you can get some pointers from that code how to write your own version that keeps wrapped iterator open until it's completely iterated/cached.
Answer: One improvement for your IsEmpty would be to check if source is ICollection<T>, and if it is, check .Count (also, dispose the enumerator):
bool IsEmpty<T>(ref IEnumerable<T> source)
{
    if (source is ICollection<T> collection)
    {
        return collection.Count == 0;
    }
    var enumerator = source.GetEnumerator();
    if (enumerator.MoveNext())
    {
        source = CreateIEnumerable(enumerator);
        return false;
    }
    enumerator.Dispose();
    return true;
    IEnumerable<T> CreateIEnumerable(IEnumerator<T> usedEnumerator)
    {
        yield return usedEnumerator.Current;
        while (usedEnumerator.MoveNext())
        {
            yield return usedEnumerator.Current;
        }
        usedEnumerator.Dispose();
    }
}

This will work for arrays and lists.
I would, however, rework IsEmpty to return:
IEnumerable<T> NotEmpty<T>(IEnumerable<T> source)
{
    if (source is ICollection<T> collection)
    {
        if (collection.Count == 0)
        {
           return null;
        }
        return source;
    }
    var enumerator = source.GetEnumerator();
    if (enumerator.MoveNext())
    {
        return CreateIEnumerable(enumerator);
    }
    enumerator.Dispose();
    return null;
    IEnumerable<T> CreateIEnumerable(IEnumerator<T> usedEnumerator)
    {
        yield return usedEnumerator.Current;
        while (usedEnumerator.MoveNext())
        {
            yield return usedEnumerator.Current;
        }
        usedEnumerator.Dispose();
    }
}

Now, you would check if it returned null.
Comment: The dispose in the outer method is referencing a variable name from the local function. I dont think that works


How to determine an appropriate "closeness" threshold when matching for causal inference?

Question: Say I have a [yes/no] treatment variable (e.g. the customer complained about their order) and I want to estimate the causal impact of this "treatment" on the average customer's future spend. To do so, I match tens of thousands of observations in such a way as to minimize their Mahalanobis distance as calculated across a dozen covariates. To estimate the average treatment effect, I prepare a difference-of-means t-test, but before I implement this test across the "treated" and "control" groups, I need to prune my observations of pairs that are insufficiently similar to serve as an effective control -i.e. I need to make a judgement call on the maximum distance a pair of observations can have before being dropped. It goes without saying, the results of the t-test vary drastically as a function of this threshold.
How do I rigorously determine an appropriate "closeness" threshold in the context of causal inference matching?
Comment: Bravo (+1). You just realised that arbitrary thresholds make for poor inferencial procedures. Use a covariate adjustment method and do not prune your data. (I will writing more on this tomorrow)
Comment: Thanks! I am not familiar with covariate adjustment methods and would love to learn more.
Answer: There are two qualities on which matched samples should be assessed: covariate balance and remaining (effective) sample size. Covariate balance is the degree to which the covariate distributions are the same between the treatment groups in the matched sample. Remaining sample size is the number of units remaining after discarding unmatched units. Covariate balance is required to eliminate bias due to confounding, and remaining sample size is required to achieve a precise estimate. In many cases, there is a trade-off: discarding units can improve balance but reduces remaining sample size. This is an instance of the fundamental bias-variance trade-off that is ubiquitous in statistics.
Another potentially important feature of the matched dataset is the degree to which it represents the population to which you want your effect to generalize. If you discard units in such a way that the remaining matched sample does not resemble your target population, the estimated effect will not be valid for that population. In general, discarding units moves your sample further from the target population. In some cases, this is not so important because the target population itself may be poorly defined or arbitrary, in which case you can say a treatment effect may exist for some population, but not a specific one. I discuss this a bit in my answer here.
So, the answer to your question is to find the cutoff that ensures balance, retains many units, and ensures the sample resembles the target population. There is no magic number, and the optimal value will vary from dataset to dataset and is in principle unknown to the analyst. A commonly used criterion is to disallow pairs of units that are more than .2 standard deviations of the logit of the propensity score apart from each other. Typically, rather than perform a match and then discard distant pairs, you incorporate this criterion, which is known as a "caliper", into the matching itself; that way you don't discard a unit that might have been a good match for a different unit. Calipers are optional in matching; if your matched sample is well-balanced, there is no need to impose a restriction on the distance between paired units.
Comment: I have written about King & Nielsen (2019) [here](https://stats.stackexchange.com/questions/481110/propensity-score-matching-what-is-the-problem) and [here](https://stats.stackexchange.com/questions/415571/exact-matching-multiple-regression-on-high-dimensional-treatment-control-study/415598#415598). Just because one researcher doesn't like a method doesn't mean it is falling out of favor. That said, I'm not talking about PS matching. You do Mahalanobis distance matching with PS-based calipers. This method is recommended by the inventors of the PS.
Comment: Thank you for your response. I was under the impression that propensity score matching was falling out of favor based on [work by Gary King](https://gking.harvard.edu/publications/why-propensity-scores-should-not-be-used-formatching). Do you know if this .2 standard deviations criterion is equally applicable in the context of Mahalanobis distance matching?
Comment: Understood. Thank you for clarifying.


Sending email after POST request with Django and Tastypie

Question: I am using Tastypie to create an API for a Django project. What I'd like to do is to send an email after a POST request to a vouchers resource (creating a new voucher). Does anybody know what's the best solution to this problem?
I am not sure how can I intercept the request and process it. A solution that comes to mind is to override the model's constructor and send the email when the instance is created! I don't know whether this is a good idea or not.
The answer to this question seems a possible solution: Adding to the "constructor" of a django model
Comment: You forgot to ask a question.
Answer: Use post_save in Django Signals. https://docs.djangoproject.com/en/dev/topics/signals/
You can check the created parameter to know if you need to send an email.


graphite: how to get per-second metrics from batch metrics?

Question: I'm trying to measure a online mini-batch processing system with a per-second metrics (total query per second). For every batch, a metric (e.g. "stats.gauges.<host>.query.count") will be send to graphite. batches are processed in several different hosts in parallel and a batch of data take about 5 seconds to process.
I've tried:

simply sum series: sumSeries(stats.gauges.*.query.count),
the result metrics is many times greater than the actual value;
scale    to 1 second:
scaleToSeconds(sumSeries(stats.gauges.*.query.count),
1), the result metrics is much less than the actual value;
integral then derivative: nonNegativeDerivative(sumSeries(integral(stats.gauges.*.query.count))), same as the first case ...
send gauges with
delta=True param, then derivative. the result is about 20% greater
than the actual value

so, how to get per-second metrics from batch metrics? what is the best practice?
Answer: You should use carbon-aggregator service to add several metrics together as they come in. There is an example which fits your case at http://graphite.readthedocs.io/en/latest/config-carbon.html#aggregation-rules-conf
As your batch takes 5 secs to process, frequency should be 5 to buffer all the metrics. After five seconds, aggregator will sum them up and write to graphite.


Annotating graphs in R

Question: This is more of a "how to use R" question than an actual hardcore statistics question, but I think the concentration of R masters here makes this a good forum for it. I'm refreshing a time series graphing package that currently uses gnuplot. The first step is getting somewhere close to the current graphs, and then I hope to be able to add more statistical analysis from R later.
Currently, the gnuplot-generated graphs look like this:

The corresponding graph from R looks like this:

I'm reasonably happy with this. What's missing is the legend stuff from below the gnuplot graph. I can add a legend saying "In" and "Out", but how to best present the average, maximal and minimal values? I could add horizontal lines to the graph and print the values inside the graph area, but the placement might interfere with the actual graph.
Comment: `paste` is your friend. Just construct a string like `legend.string <- paste("In traffic [Max", max(values),"]")` and then pass that as the `legend` parameter of `legend`
Comment: So... why don't you write it as in the top graph?
Comment: @nico Lack of expertise on how to do it in R, basically.
Comment: Sorry this is an answer -- I guess I haven't earned the "comment" button yet... All I was going to suggest is that "how to" questions like this one might be a better fit for StackOverflow. There are R pros there who answer these sorts of questions before you can hit 'send'! I don't know if there's an official FAQ or guidance on SO vs. CV, but since Hadley himself answers a lot of the ggplot2 questions on SO, that's where I go first.
Answer: I'm sure that there's a more elegant way to do this but you can try this :
Data <- data.frame(serie1 = cumsum(rnorm(100)), serie2 = rnorm(100), temps = as.Date(1:100, origin = "2000-01-01"))

 label <- c("In traffic [max 2.0G bps] [Avg 1.1G bps] [95% 1.8G bps] [Min 569.4M bps]", "Out traffic [max 2.0G bps] [Avg 672M bps] [95% 2.1G bps] [Min 154.3M bps]")

p <- ggplot(melt(Data, id = "temps"), aes(temps, value, group = variable, colour = variable))
p <- p + geom_line() +xlab("") + opts(legend.position = "bottom", legend.title = NULL)
p <- p + scale_colour_manual(values = c("red", "blue"), breaks = c("serie1", "serie2"), labels = label)

print(p)

Comment: (+1) Your solution is at least more elegant than mine because you don't duplicate the grouping variable!
Comment: Related question [here](http://stackoverflow.com/questions/8736647/adding-legend-annotate-to-rs-plot-similarly-to-rs-ggplot), I am trying to remix this. Beautiful +1!
Answer: A quick and dirty way to paste some text and numerical results along the labels of your legend is to simply rename the factor levels. For instance,
df <- data.frame(x=rnorm(100), y=rnorm(100), f=gl(2,50))
df$f2 <- df$f
levels(df$f2) <- paste(levels(df$f), tapply(df$y, df$f, mean), sep=": ")
p <- ggplot(data=df) + geom_point(aes(x=x, y=y, color=f2))
p + opts(legend.position = 'bottom', legend.title=NULL)

You can add whatever you want into the new labels, such as mean, min, max, etc. (e.g., create a custom function, inspired from summary() that returns the values you want, and append them to c("In","Out")).
Answer: Try the directlabels package for some more control.  As far as the caption goes, if you're automatically generating the graphs, you might simply use it as caption text (in LaTeX or HTML or what have you).  Hope this helps.

Annotating graphs in R


making a column right to left direction

Question: I have a table and in this table I have a column which is used for arabic texts. However in the column the text are in left to right direction. Is it possible to make them rtl direction?
Comment: You didn't specify *where* you're viewing this data and *what* program/application you wish to have it displaying RTL. Is it a browser, some SQL GUI, MySQL CLI etc?
Comment: Can you dig up the table/database encodings, etc, and update the post with that info?
Answer: There's the REVERSE() function. Here's the manual entry.
This function is multi-byte safe, which means it's safe for every character set / encoding.
SELECT REVERSE('abc');

results in
cba

To make it permanent you can update your column like this:
UPDATE yourTable SET yourColumn = REVERSE(yourColumn)

Comment: Your answer is both valid and highly unusual.  I admit I was being sarcastic at first, but after thinking some more, it's totally valid.  It's probably not what the OP was hoping for, but hey, who am I to judge that.  The root problem is probably a character set/encoding issue, but reverse() probably would work at least as a short term thing.
Comment: This is epic.  Well done.
Comment: @DaveS. Feel free to upvote, which is by the way the usual way to say thanks here ;) Or were you sarcastic? Did I get the question wrong?
Comment: @DaveS. Will update the question. But I don't get what's so "unusual" about my answer.
Comment: Nothing intrinsically, just I had interpreted the question to be an issue with collation character sets, and so from that perspective your answer almost seemed flip, but in a beautifully subtle way.  Sort of like Laurence Olivier saying to Dustin Hoffman "Try acting dear boy".

making a column right to left direction


Change varchar type (of MySQL ) in SWI-Prolog

Question: Can anyone help me with converting varchar type(of MySQL column) to utf8_general_ci in SWI-Prolog?
I've tried this
<code>:-module(utf8_general_ci).
</code>
But still doesn't recognize the language of the value.
Answer: the problem was in the encoding type of Prolog.. So i use
<code> :- encoding(utf8).
</code>
and know everything is ok.


How can I extract a portion of a camera?

Question: I'm using Camera in Android like this tutorial in my "Insert frame for photo" app
http://developer.android.com/guide/topics/media/camera.html
I have a ImageView in FrameLayout to insert a image frame, with inside part is transparent.
<code><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <FrameLayout
            android:id="@+id/camera_container"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:gravity="top"
            android:orientation="vertical" >
        </FrameLayout>

        <ImageView
            android:id="@+id/grid"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/demo" />
    </RelativeLayout>

</LinearLayout>
</code>
How can I extract the frame image where the inside part is from the camera?
Answer: <code>        private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
        private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
                if (resultCode == RESULT_OK) {
                    // Image captured and saved to fileUri specified in the Intent
                    Toast.makeText(this, "Image saved to:\n" +
                             data.getData(), Toast.LENGTH_LONG).show();
                } else if (resultCode == RESULT_CANCELED) {
                    // User cancelled the image capture
                } else {
                    // Image capture failed, advise user
                }
            }

            if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
                if (resultCode == RESULT_OK) {
                    // Video captured and saved to fileUri specified in the Intent
                    Toast.makeText(this, "Video saved to:\n" +
                             data.getData(), Toast.LENGTH_LONG).show();
                } else if (resultCode == RESULT_CANCELED) {
                    // User cancelled the video capture
                } else {
                    // Video capture failed, advise user
                }
            }
        }
</code>


How can I check to see if a file exists before proceeding using Python

Question: I have some code that will find the newest file in a directory and append a time stamp to the file name.  It works great as long as there is a file in the directory to rename.  If there isn't I am getting:
"ValueError: max() arg is an empty sequence"
Here's my code:
import os
import glob
import datetime
now = datetime.datetime.now()
append = now.strftime("%H%M%S")
newest = max(glob.iglob('1234_fileName*.LOG'), key=os.path.getmtime)
newfile = (append+"_"+newest)
os.rename(newest, newfile)

Any suggestions for simplifying the code would be appreciated as well as explaining how to only run if a "1234_fileName*.LOG" (note the wildcard) file is detected.
What I need this program to do is run periodically (I can use task scheduler for that) and check for a new file.  If there is a new file append the hours, minutes and seconds to it's name.
Thanks!
Comment: could you edit your title to reflect the question text? If you want to preserve the title then it is a duplicate of: [Check if a file exists using Python](http://stackoverflow.com/q/82831/4279)
Comment: related: [Check the permissions of a file in python](http://stackoverflow.com/q/27434643/4279).
Answer: You could use glob.glob()  that returns a list instead of glob.iglob() that returns an iterator:
files = glob.glob('1234_fileName*.LOG')
if files:
   newest = max(files, key=os.path.getmtime)
   newfile = append + "_" + newest
   os.rename(newest, newfile)

Both glob() and iglob() use os.listdir() under the hood so there is no difference for a single directory.
Comment: Thank you.  Works exactly like I want now.
Answer: max() is complaining that you're asking for the largest of 0 items, and throwing a ValueError. You'll have to catch it. This will continue to throw any IOErrors that might occur:
import os, glob, datetime

try:
    app = datetime.datetime.now().strftime("%H%M%S")
    newest = max(glob.iglob("1234_filename*.LOG"), key=os.path.getmtime)
    newfile = (app + "_" + newest)
    os.rename(newest, newfile)
except ValueError:
    pass

Comment: `files` is always true in your example because `iglob()` returns an iterator
Comment: You're right, iterators can't be tested that way. OK, then, back to try/except.
Comment: Thank you.  This helped me clean up my code.  However, I still was getting the same error.  Changing files = glob.iglob to files = glob.glob worked.
Answer: os.access allows you to check access rights before operations. An example is right under the link.
Also, it's fine to just do things inside a try .. except IOError.
Comment: Plus one for `try`-`catch`, that's allegedly more Pythonic and it seems as if it would be [more resistant to race conditions](http://blog.rodneyrehm.de/archives/12-Improving-Disk-IO-in-PHP-Apps.html).
Comment: how `os.access()` may help to avoid `"ValueError: max() arg is an empty sequence"` in this case?
Comment: @J.F.Sebastian: Sorry, I was answering the title question, "How can I check to see if a file exists before proceeding using Python", not the implied question "why this code fails".


Warning: Invalid argument supplied for foreach() ViewsData.php

Question: On my drupal 9 website I get a warning when running the Cache Clear Command. I get these two messages. I have no clue where to start with finding the problem. Hope someone understands these messages and can point me in te right direction.
Warning: Invalid argument supplied for foreach() in Drupal\views\ViewsData->getData() (line 239 of core/modules/views/src/ViewsData.php).
Drupal\views\ViewsData->getData() (Line: 154)
Drupal\views\ViewsData->get('block_content') (Line: 91)
Drupal\views\Plugin\Derivative\ViewsEntityRow->getDerivativeDefinitions(Array) (Line: 101)
Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator->getDerivatives(Array) (Line: 87)
Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator->getDefinitions() (Line: 285)
Drupal\Core\Plugin\DefaultPluginManager->findDefinitions() (Line: 175)
Drupal\Core\Plugin\DefaultPluginManager->getDefinitions() (Line: 22)
Drupal\Core\Plugin\DefaultPluginManager->getDefinition('data_field') (Line: 16)
Drupal\Core\Plugin\Factory\ContainerFactory->createInstance('data_field', Array) (Line: 83)
Drupal\Component\Plugin\PluginManagerBase->createInstance('data_field') (Line: 817)
Drupal\views\Plugin\views\display\DisplayPluginBase->getPlugin('row') (Line: 122)
Drupal\views\Plugin\views\style\StylePluginBase->init(Object, Object, Array) (Line: 820)
Drupal\views\Plugin\views\display\DisplayPluginBase->getPlugin('style') (Line: 346)
Drupal\rest\Plugin\views\display\RestExport->collectRoutes(Object) (Line: 120)
Drupal\views\EventSubscriber\RouteSubscriber->routes()
call_user_func(Array) (Line: 146)
Drupal\Core\Routing\RouteBuilder->rebuild() (Line: 83)
Drupal\Core\ProxyClass\Routing\RouteBuilder->rebuild() (Line: 382)
Drupal\Core\Extension\ModuleInstaller->install(Array, 1) (Line: 83)
Drupal\Core\ProxyClass\Extension\ModuleInstaller->install(Array) (Line: 476)
Drupal\system\Form\ModulesListForm->submitForm(Array, Object)
call_user_func_array(Array, Array) (Line: 114)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 52)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 592)
Drupal\Core\Form\FormBuilder->processForm('system_modules', Array, Object) (Line: 320)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 564)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 158)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Warning: Invalid argument supplied for foreach() in Drupal\Component\Utility\NestedArray::mergeDeepArray() (line 327 of core/lib/Drupal/Component/Utility/NestedArray.php).
Drupal\Component\Utility\NestedArray::mergeDeepArray(Array) (Line: 295)
Drupal\Component\Utility\NestedArray::mergeDeep(Array, NULL) (Line: 244)
Drupal\views\ViewsData->getData() (Line: 154)
Drupal\views\ViewsData->get('block_content') (Line: 91)
Drupal\views\Plugin\Derivative\ViewsEntityRow->getDerivativeDefinitions(Array) (Line: 101)
Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator->getDerivatives(Array) (Line: 87)
Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator->getDefinitions() (Line: 285)
Drupal\Core\Plugin\DefaultPluginManager->findDefinitions() (Line: 175)
Drupal\Core\Plugin\DefaultPluginManager->getDefinitions() (Line: 22)
Drupal\Core\Plugin\DefaultPluginManager->getDefinition('data_field') (Line: 16)
Drupal\Core\Plugin\Factory\ContainerFactory->createInstance('data_field', Array) (Line: 83)
Drupal\Component\Plugin\PluginManagerBase->createInstance('data_field') (Line: 817)
Drupal\views\Plugin\views\display\DisplayPluginBase->getPlugin('row') (Line: 122)
Drupal\views\Plugin\views\style\StylePluginBase->init(Object, Object, Array) (Line: 820)
Drupal\views\Plugin\views\display\DisplayPluginBase->getPlugin('style') (Line: 346)
Drupal\rest\Plugin\views\display\RestExport->collectRoutes(Object) (Line: 120)
Drupal\views\EventSubscriber\RouteSubscriber->routes()
call_user_func(Array) (Line: 146)
Drupal\Core\Routing\RouteBuilder->rebuild() (Line: 83)
Drupal\Core\ProxyClass\Routing\RouteBuilder->rebuild() (Line: 382)
Drupal\Core\Extension\ModuleInstaller->install(Array, 1) (Line: 83)
Drupal\Core\ProxyClass\Extension\ModuleInstaller->install(Array) (Line: 476)
Drupal\system\Form\ModulesListForm->submitForm(Array, Object)
call_user_func_array(Array, Array) (Line: 114)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 52)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 592)
Drupal\Core\Form\FormBuilder->processForm('system_modules', Array, Object) (Line: 320)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 564)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 158)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Comment: The cache clear invalidates all views data so that every hook_views_data is invoked. One of them, probably in a custom module, is not returning a proper views data array.
Answer: Thanks to 4k4s comment:

"The cache clear invalidates all views data so that every
hook_views_data is invoked. One of them, probably in a custom module,
is not returning a proper views data array."

I searched my project for _views_data and found out I still had the Draggableviews module installed. I remember having problems with this module before and therefore don't use it anymore, it was quite easy to uninstall and remove from my project. Doing this was the solution for my problem.


How to correct identify js code in CakePHP project with PhpStorm IDE?

Question: I am using Phpstorm as IDE in a CakePHP 3.0 project and the plugin CakeStorm. I would like to treat code inside script blocks as javascript code but the IDE doesnt identify with the correct syntax.
<code><?php $this->start('script') ?>
  //javascript code here ...
<?php $this->end() ?>
</code>
I know there is a feature called "language injection" in PhpStorm that could solve the problem but couldnt figure out a way to achieve this.
Comment: (by default) that is not possible in html context, check https://www.jetbrains.com/help/phpstorm/2016.2/language-injection-settings-generic-php.html
Comment: Doubt, really doubt that it is possible at all in PhpStorm with default functionality (unless done special processing by PHP parser/custom plugin). The only thing I can think of it to use `echo` with  HEREDOC (using `JS` as a label) in actual PHP code...
Comment: any tips for the downvote?
Answer: However, you can do that in php context:
<code>$this->start('script');
echo <<<JS
    //javascript code here ...
JS;
$this->end();
</code>


Passar variavel com varios input/select

Question: Tenho um formulario com varios input que crio com um ciclo for, que passo por POST para outra pagina e altero o seu nome como no exemplo:
<form action="page2.php" method="post">
<?php
    for($i=1; $i<=10; $i++){
?>
<select name=fichier<?php echo $i?> > (...)

O problema é caso não quero preencher a input ou select, como faço para receber o seu valor? Para não tar a repetir codigo para os varios inpuut tenho isto na outra pagina que recebe o POST.
for($i=1; $i<=10; $i++){
    if (isset($_POST['fichier'.$i]))
    {
        $fichier = htmlentities($_POST['fichier'.$i]);
    }

    if(isset($_POST['fichier'.$i])== 0){
        break;
    }
    //echo $fichier;
}

Answer: Você pode nomear os inputs com colchetes no final, Ex:
<?php

if (isset($_POST)) {
    var_dump($_POST);
}

?>

<form method="post">
    <input id="itens[]" name="itens[]" value="text1" type="text" />

    <input id="itens[]" name="itens[]" value="text2" type="text" />

    <input id="itens[]" name="itens[]" value="text3" type="text" />

    <input id="itens[]" name="itens[]" value="text4" type="text" />

    <input type="submit" />
</form>

Saída:
array (size=1)
  'itens' =>
    array (size=4)
      0 => string 'text1' (length=5)
      1 => string 'text2' (length=5)
      2 => string 'text3' (length=5)
      3 => string 'text4' (length=5)

Answer: Como você tem um formulário com vários input, e nao quer repetir os valores a solução seria inserir um nome com [] no final para que os valores sejam salvos em um array.
Exemplo:
page.html
<form action="page2.php" method="post">
   Input1 <input id="fichier[]" name="fichier[]" type="text" />

   Input2 <input id="fichier[]" name="fichier[]" type="text" />

   Input3 <input id="fichier[]" name="fichier[]" type="text" />

    <input type="submit" value="Enviar" />
</form>

page2.php
<?php
    if (isset($_POST['fichier'])) {
        var_dump($_POST['fichier']);
    }
 ?>

Saida apos POST:
array(3) { [0]=> string(6) "texto1" [1]=> string(6) "texto2" [2]=> string(6) "texto3" }

Passar variavel com varios input/select