Why my callback is not invoking in Tensorflow?

Question: Below is my Tensorflow and Python code which will end the training when accuracy in 99% with the call back function. But the callback is not invoking. Where is the problem ?
def train_mnist():

    class myCallback(tf.keras.callbacks.Callback):
        def on_epoc_end(self, epoch,logs={}):
            if (logs.get('accuracy')>0.99):
                print("Reached 99% accuracy so cancelling training!")
                self.model.stop_training=True

    mnist = tf.keras.datasets.mnist

    (x_train, y_train),(x_test, y_test) = mnist.load_data(path=path)

    x_train= x_train/255.0
    x_test= x_test/255.0
    callbacks=myCallback()

    model = tf.keras.models.Sequential([
        # YOUR CODE SHOULD START HERE
        tf.keras.layers.Flatten(input_shape=(28, 28)),
        tf.keras.layers.Dense(256, activation=tf.nn.relu),
        tf.keras.layers.Dense(10, activation=tf.nn.softmax)

    ])

    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])

    # model fitting
    history = model.fit(x_train,y_train, epochs=10,callbacks=[callbacks])
    # model fitting
    return history.epoch, history.history['acc'][-1]

Comment: Because of a typo: it's `epoch` not `epoc` (i.e. look at the name of method in your custom callback).
Answer: You're misspelling epoch and also you should return accuracy not acc.
from tensorflow.keras.layers import Input, Dense, Add, Activation, Flatten
from tensorflow.keras.models import Model, Sequential
import tensorflow as tf
import numpy as np
import random

from tensorflow.python.keras.layers import Input, GaussianNoise, BatchNormalization

def train_mnist():
  class myCallback(tf.keras.callbacks.Callback):
      def on_epoch_end(self, epoch,logs={}):
          print(logs.get('accuracy'))
          if (logs.get('accuracy')>0.9):
              print("Reached 90% accuracy so cancelling training!")
              self.model.stop_training=True

  mnist = tf.keras.datasets.mnist

  (x_train, y_train),(x_test, y_test) = mnist.load_data()

  x_train= x_train/255.0
  x_test= x_test/255.0

  callbacks=myCallback()

  model = tf.keras.models.Sequential([
      # YOUR CODE SHOULD START HERE
      tf.keras.layers.Flatten(input_shape=(28, 28)),
      tf.keras.layers.Dense(256, activation=tf.nn.relu),
      tf.keras.layers.Dense(10, activation=tf.nn.softmax)

  ])

  model.compile(optimizer='adam',
                loss='sparse_categorical_crossentropy',
                metrics=['accuracy'])

  # model fitting
  history = model.fit(x_train,y_train, epochs=10,callbacks=[callbacks])
  # model fitting
  return history.epoch, history.history['accuracy'][-1]

train_mnist()

Epoch 1/10
1859/1875 [============================>.] - ETA: 0s - loss: 0.2273 - accuracy: 0.93580.93586665391922
Reached 90% accuracy so cancelling training!
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2265 - accuracy: 0.9359

([0], 0.93586665391922)

Comment: Please don't provide answers for issues caused by typos. They don't provide any value (and may be considered as rep farming). Instead use "flag" button, then choose "A community specific reason" and then vote for "Not reproducible or was caused by a typo". You may choose to leave a comment on the question so that the OP knows what the typos are. Thanks!
Comment: That's not relevant to Keras. It's how Python works: there is no fixed template for allowed methods in a Python class. And it's for good: you can decide to implement a helper method in your callback which may not be directly used by Keras; rather it may be called by other methods in the class.
Comment: Okay, I didn't know that. Thanks for the advice, I already ran the code and found two bugs so decided to answer anyway. But will definitely keep in mind in the future.
Comment: But it's weird that Keras doesn't throw any warnings when someone tries to implement a random function inside callback without any context.
Answer: Unfortunately don't have enough reputation to provide commentary on one of the above comments, but I wanted to point out that the on_epoch_end function is something called directly through tensorflow when an epoch ends. In this case, we're just implementing it inside a custom python class that will be called automatically by the underlying framework. I'm sourcing from Tensorflow in Practice deeplearning.ai week 2 on coursera. Very similar where the issues with the above callback are coming from it seems.
Here's some proof from my most recent run:
Epoch 1/20
59968/60000 [============================>.] - ETA: 0s - loss: 1.0648 - acc: 0.9491Inside callback
60000/60000 [==============================] - 34s 575us/sample - loss: 1.0645 - acc: 0.9491
Epoch 2/20
59968/60000 [============================>.] - ETA: 0s - loss: 0.0560 - acc: 0.9825Inside callback
60000/60000 [==============================] - 35s 583us/sample - loss: 0.0560 - acc: 0.9825
Epoch 3/20
59840/60000 [============================>.] - ETA: 0s - loss: 0.0457 - acc: 0.9861Inside callback
60000/60000 [==============================] - 31s 512us/sample - loss: 0.0457 - acc: 0.9861
Epoch 4/20
59840/60000 [============================>.] - ETA: 0s - loss: 0.0428 - acc: 0.9873Inside callback
60000/60000 [==============================] - 32s 528us/sample - loss: 0.0428 - acc: 0.9873
Epoch 5/20
59808/60000 [============================>.] - ETA: 0s - loss: 0.0314 - acc: 0.9909Inside callback
60000/60000 [==============================] - 30s 507us/sample - loss: 0.0315 - acc: 0.9909
Epoch 6/20
59840/60000 [============================>.] - ETA: 0s - loss: 0.0271 - acc: 0.9924Inside callback
60000/60000 [==============================] - 32s 532us/sample - loss: 0.0270 - acc: 0.9924
Epoch 7/20
59968/60000 [============================>.] - ETA: 0s - loss: 0.0238 - acc: 0.9938Inside callback
60000/60000 [==============================] - 33s 555us/sample - loss: 0.0238 - acc: 0.9938
Epoch 8/20
59936/60000 [============================>.] - ETA: 0s - loss: 0.0255 - acc: 0.9934Inside callback
60000/60000 [==============================] - 33s 550us/sample - loss: 0.0255 - acc: 0.9934
Epoch 9/20
59872/60000 [============================>.] - ETA: 0s - loss: 0.0195 - acc: 0.9953Inside callback
60000/60000 [==============================] - 33s 557us/sample - loss: 0.0194 - acc: 0.9953
Epoch 10/20
59744/60000 [============================>.] - ETA: 0s - loss: 0.0186 - acc: 0.9959Inside callback
60000/60000 [==============================] - 33s 551us/sample - loss: 0.0185 - acc: 0.9959
Epoch 11/20
59968/60000 [============================>.] - ETA: 0s - loss: 0.0219 - acc: 0.9954Inside callback
60000/60000 [==============================] - 32s 530us/sample - loss: 0.0219 - acc: 0.9954
Epoch 12/20
59936/60000 [============================>.] - ETA: 0s - loss: 0.0208 - acc: 0.9960Inside callback
60000/60000 [==============================] - 33s 558us/sample - loss: 0.0208 - acc: 0.9960
Epoch 13/20
59872/60000 [============================>.] - ETA: 0s - loss: 0.0185 - acc: 0.9968Inside callback
60000/60000 [==============================] - 31s 520us/sample - loss: 0.0184 - acc: 0.9968
Epoch 14/20
59872/60000 [============================>.] - ETA: 0s - loss: 0.0181 - acc: 0.9970Inside callback
60000/60000 [==============================] - 35s 587us/sample - loss: 0.0181 - acc: 0.9970
Epoch 15/20
59936/60000 [============================>.] - ETA: 0s - loss: 0.0193 - acc: 0.9971Inside callback
60000/60000 [==============================] - 33s 555us/sample - loss: 0.0192 - acc: 0.9972
Epoch 16/20
59968/60000 [============================>.] - ETA: 0s - loss: 0.0176 - acc: 0.9972Inside callback
60000/60000 [==============================] - 33s 558us/sample - loss: 0.0176 - acc: 0.9972
Epoch 17/20
59968/60000 [============================>.] - ETA: 0s - loss: 0.0183 - acc: 0.9974Inside callback
60000/60000 [==============================] - 33s 555us/sample - loss: 0.0182 - acc: 0.9974
Epoch 18/20
59872/60000 [============================>.] - ETA: 0s - loss: 0.0225 - acc: 0.9970Inside callback
60000/60000 [==============================] - 34s 570us/sample - loss: 0.0224 - acc: 0.9970
Epoch 19/20
59808/60000 [============================>.] - ETA: 0s - loss: 0.0185 - acc: 0.9975Inside callback
60000/60000 [==============================] - 33s 548us/sample - loss: 0.0185 - acc: 0.9975
Epoch 20/20
59776/60000 [============================>.] - ETA: 0s - loss: 0.0150 - acc: 0.9979Inside callback
60000/60000 [==============================] - 34s 565us/sample - loss: 0.0149 - acc: 0.9979
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-25-1ff3c304aec3> in <module>
----> 1 _, _ = train_mnist_conv()

<ipython-input-24-b469df35dac0> in train_mnist_conv()
     38     )
     39     # model fitting
---> 40     return history.epoch, history.history['accuracy'][-1]
     41

KeyError: 'accuracy'

The key error is because of the history object not having the keyword 'accuracy', so I wanted to address that as a source of  concern before continuing on.


Can I widen the grooves on my guitar's nut myself?

Question: Ok guys, I posted a thread regarding my Low B going out of tune, especially when I would bend my string excessively. What I have come up with after checking the tuner and applying Big Bends Nut Sauce, is that it is probably the nut of the guitar. I do know how to properly string my instrument so that is not an issue. So, without further babbling I am asking you all is widening the nut something I should be doing or is this a job only fit for a luthier?If I am fit to do so could someone tell me how to do it? I see videos on shaving it down and such but nothing on this subject worth watching. Thank you all for your help and constant support!
PS: This is a PRS 7 string with fixed/plate bridge
Comment: Hey @Jordan. You should change the title of your post, so that it refers to your actual question about widening (the grooves?) on your nut.
Comment: Sure. How about "Can I widen the grooves in my guitar's nut myself?"?
Comment: Hey @Bob Broadley, I don't think I can do that myself. If you have the ability to edit my post I will approve. My rep isn't high enough yet.
Comment: That would be perfect my friend
Comment: "Low B"? I think the "B" that you refer to is above mid-C.
Answer: Based on what you have written in your question I would suggest you just take it to a luthier. There are so many things you can get wrong here that it probably is not worth it.
Widening the groove too far will lead to buzzing and tone problems and is not reversible - messing this up requires replacing the nut.
There are good examples of videos on YouTube showing how to do this. Worth watching a few.
Comment: Thank you. Yeah I have been learning how to set this guitar up the way I want it and either I tighten the neck too much and there is a little string buzzing, or I loosen it and lower it at the bridge and I still don't get it quite right. So regarding the nut issue, I think you are right in that a luthier will solve the problem and show me how to set my guitar up properly. Thank you
Answer: I'm guessing with all the bending you appear to do on that low B, that it's already quite a thin gauge string. The nut slot will have been produced for a 'standard' B string, about .054 .058 ish. Have you tried a slightly thinner B string? It might solve the problem of binding in the nut. I'm not sure what the gauge is that Paul would put as standard, but as suggested in my answer to the other question, a chat with him would be a good move initially. As there is no zero fret as a safety net if you over-widen the slot, and without the appropriate nut files, go with Dr. Mayhem's idea. Once the slot's widened, there's no simple return to original.
Comment: Yeah I use the same strings that came on the guitar: D'Addario 10-59. I think this was a mistake made at the factory. I am going to go ahead and go with Mayhem's suggestion. Thank you for responding.
Answer: An answer to "How to widen the groove on the nut" :
1) Read the other answers here warning you about what happens if you get it wrong (Tim & Dr Mayhem). If you still want to proceed ..
2) Remove strings - maybe you can just remove the relevant one but it's probably best to give yourself a bit of room so better to remove them all.
3) put a bit of paper or cardboard up against the nut and on top of the frets, to protect it.
4) ideally add something to protect the neck beyond the nut
5) If possible, put something up against the nut, layed flat against the frets, which butts up agains tht enut at the level that the groov goes down to. This will stop you making the hole any lower, whcih you probably want to avoid.
6) Get either a small (model/jewellery making) file of the right size (ie, just a bit bigger than the existing hole) and GENTLY file the groove wider. Don't file it lower !! you'll make the string buzz as it'll sit too near the frets.
6a) Alternatively, you can use a bit of folded sandpaper - use very smooth, maybe 1200 grit ("wet & dry") so that you don't take off too much at a time.
Pull the file/sandpaper sideways across the nut - don't push down against the neck.
7) Repeatedly try the string in the hole. Try the string at full tension. Do this a bit before you've got the hole large enough because this will give you a clue how much you need to take off. If you're lucky, the string will leave a mark on the groove where it's meeting & tell you where to take the nut back.
8) (I hope you don't get to this stage) cry when you realise you've taken the groove down as well as widened it, and now the string buzzes unless you stick a bit of paper under the string, forever.
9) Take it to the luthier to have a new nut put in.


Overlay module does not work

Question: I updated my Drupal, but now the overlay module does not worki and everything shows on the center of page.
Answer: This is a temporary problem.
do these steps:
1-disable overlay module.
2-click on Save configuration at the end of page.
3-click on modules again
4-enable overlay module Save configuration

<code>its like reseting a computer
</code>

Overlay module does not work


why when I post a form element I get undefined index error?

Question: I am retrieving some data from the database and insert them in to the table using the following code . I am trying to make each row one form and once the user click the button I want to send the information to sub.php to store it in DB.Currently the problem is when I post the info i get undefined value .
Here is my code :
   while($row = $result->fetch_assoc()) {
                                echo '<form  method="Post" action="sub.php" >

                                            '
                                                .$row["name"] .
                                            '
                                            '
                                                .$row["course_name"] .
                                            '
                                            '
                                                .$row["course_lec"] .
                                            '
                                            '
                                                .$row["course_sem"] .
                                            '

                                                <select  style="background-color: #222222; border: none; name="q1">
                                                 <option value="0">0</option>
                                                  <option value="1">1</option>
                                                  <option value="2">2</option>
                                                  <option value="3">3</option>
                                                  <option value="4">4</option>
                                                  <option value="5">5</option>
                                                  <option value="6">6</option>
                                                  <option value="7">7</option>
                                                  <option value="8">8</option>
                                                  <option value="9">9</option>
                                                  <option value="10">10</option>

                                            </select>

                                            <select  style="background-color: #222222; border: none; name="q2">
                                                 <option value="0">0</option>
                                                  <option value="1">1</option>
                                                  <option value="2">2</option>
                                                  <option value="3">3</option>
                                                  <option value="4">4</option>
                                                  <option value="5">5</option>
                                                  <option value="6">6</option>
                                                  <option value="7">7</option>
                                                  <option value="8">8</option>
                                                  <option value="9">9</option>
                                                  <option value="10">10</option>

                                            </select>

                                                   <select  style="background-color: #222222; border: none; name="q3">
                                                 <option value="0">0</option>
                                                  <option value="1">1</option>
                                                  <option value="2">2</option>
                                                  <option value="3">3</option>
                                                  <option value="4">4</option>
                                                  <option value="5">5</option>
                                                  <option value="6">6</option>
                                                  <option value="7">7</option>
                                                  <option value="8">8</option>
                                                  <option value="9">9</option>
                                                  <option value="10">10</option>

                                            </select>

                                                   <select  style="background-color: #222222; border: none; name="q4">
                                                 <option value="0">0</option>
                                                  <option value="1">1</option>
                                                  <option value="2">2</option>
                                                  <option value="3">3</option>
                                                  <option value="4">4</option>
                                                  <option value="5">5</option>
                                                  <option value="6">6</option>
                                                  <option value="7">7</option>
                                                  <option value="8">8</option>
                                                  <option value="9">9</option>
                                                  <option value="10">10</option>

                                            </select>

                                                   <select  style="background-color: #222222; border: none; name="q5" >
                                                 <option value="0">0</option>
                                                  <option value="1">1</option>
                                                  <option value="2">2</option>
                                                  <option value="3">3</option>
                                                  <option value="4">4</option>
                                                  <option value="5">5</option>
                                                  <option value="6">6</option>
                                                  <option value="7">7</option>
                                                  <option value="8">8</option>
                                                  <option value="9">9</option>
                                                  <option value="10">10</option>

                                            </select>

                                                <button type="submit"  class="btn btn-primary btn-xs" >submit</button>

                                           </form> '

                                            ;
                             }

                    }

and in sub.php I have :
<?php

include ("conn.php") ;
session_start();

$selectOption = $_POST["q1"];

?>

and for better understanding I've put the image there too
Comment: Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php)
Comment: Most probably the select box with the name q1 haven't selected before submitting.
Answer: You don't have a closing quote on your style="" attribute in your select tag:
<select  style="background-color: #222222; border: none; name="q1">

Hence the field isn't getting named q1, fix it like this:
<select  style="background-color: #222222; border: none;" name="q1">

Same goes for your other questions.
Answer: you may have to try :
if(!empty $_POST["q1"])
{
    $selectOption = $_POST["q1"];
}

Test existing variables to be sure you won't get an index error.
Hope it was helping!
Comment: This is invalid syntax.


How to merge two or more list uniquely in c#

Question: I have three list in which some of items are common. I want to merge all them to form one new list as per following conditions:

All items in new list must be unique.
If any item is available two or more (three) lists, the order of that item should be as such that all items before that item of every list must be added before that item in new list.
Uncommon items may be added in any order but subject to point no. 2.

For demonstration:
    List1:
    D
    B
    C
    K
    R

    List2:
    D
    A
    B
    G
    C
    K
    T
    R

    List3:
    D
    B
    C
    K
    M
    R

Desired Output
New List:
    D
    A
    B
    G
    C
    K
    T
    M
    R

I've tried union but it does not give desired (order) output.
Comment: is this school work or what? and what have you tried?
Comment: (2) isn't adequately described.  What if my three lists are `A B C` `C B A` and `C A  B`? All letters should be before all other letters based on requirement (2)
Comment: @MoHo This will not be happen. Order of occurrence will same in all list. If "B" comes before "A" in any list, it will comes before "A" in all list. Please look at my demonstration.
Answer: It sounds like you want to make a call to .Distinct() which will return only the unique elements of a list.
Basic usage:
var list1 = new List<char> {
    'D',
    'A',
    'B',
    'G',
    'C',
    'K',
    'T',
    'R'
};

var list2 = new List<char> {
    'D',
    'B',
    'C',
    'K',
    'M',
    'R'
};

var combined = list1.Concat(list2);

Dictionary<char,int> occurances = new Dictionary<char,int>();

foreach(var letter in combined){
    if(occurances.ContainsKey(letter)){
        // The letter is already in, so we give it +1 occurance
        occurances[letter] += 1;
    }else{
        // New letter, add it with 1 occurance
        occurances.Add(letter, 1);
    }
}

var sortedItems = occurances
    .OrderByDescending(o=>o.Value) // Using descending order so we get 3...2...1... etc.
    .Select(s=>s.Key) // Select the key (the character)
    .Distinct(); // Use distinct to eliminate the duplicates

Here is a .NET Fiddle example
Comment: Thanks, but you missed condition on point no 2 which is most important.
Comment: Sorry I missed that at first. Your wording on the 2nd point is a little confusing. I've updated the fiddle and my answer which shows one way to get the items sorted from most to least occurrences.
Comment: Sorry, I can't elaborate properly. I want to insert "B" in new list only after "D" and "A" because in List2, "B" comes after "D" & "A" whereas it comes after "D" in List1 & List3. Likewise "R" must be inserted at last in new list.


Input Sampler in Hadoop

Question: My understanding about InputSampler is that it gets data from record reader and samples keys and then creates a partition file in HDFS.
I have few queries about this sampler:
1) Is this sampling task a map task ?
2) My data is on HDFS (distributed across nodes of my cluster). Will this sampler run on nodes which has the data to be sampled?
3) Will this consume my map slots?
4) Will the sample run simultaneously with the map tasks of my MR job ? I want to know whether it will affect time consumed by mappers by reducing the number of slots?
Answer: I found that the InputSampler makes a seriously flawed assumption and is therefore not very helpful.
The idea is that it samples key values from the mapper input and then uses the resulting statistics to evenly partition the mapper output.  The assumption then is that the key type and value distribution are the same for the mapper input and output.  In my experience the mapper almost never sends the same key value types to the reducer as it reads in. So the InputSampler is useless.
In the few times where I had to sample in order to partition effectively, I ended up doing the sampling as part of the mapper (since only then did I know what keys were being produced) and writing the results out in the mapper's close() method to a directory (one set of stats per mapper).  My partitioner then had to perform lazy initialization on its first call to read the mapper-written files, assimilate the stats into some useful structure and then to partition subsequent keys accordingly.
Your only other real option is to guess at development time how the key values are distributed and hard-code that assumption into your partitioner.
Not very clean but it was the best I could figure.
Answer: This question was asked a long time ago, and many questions were left unanswered.
The only and most voted answer by @Chris does not really answer the questions, but gives an interesting point of view, though a bit too pessimistic and misleading in my opinion, so I'll discuss it here as well.
Answers to the original questions
The sampling task is done in the call to InputSampler.writePartitionFile(job, sampler). The call to this method is blocking, during which the sampling is done, in the same thread.
That's why you don't need to call job.waitForCompletion(). It's not a MapReduce Job, it simply runs in your client's process. Besides, a MapReduce job needs at least 20 seconds just to start, but sampling a small file only takes a couple of second.
Thus, the answer to all of your questions is simply "No".
More details from reading the code
If you look at the code of the writePartitionFile(), you will find that it calls sampler.getSample(), who will call inputformat.getSplits() to get a list of all input splits to be samples.
These input formats will then be read sequentially to extract the samples. Each input split is read by a new record reader created within the same method. This means that your client is doing the reading and sampling.
Your other nodes are not running any "map" or other processes, they are simply serving HDFS the block data needed by your client for its input splits needed for sampling.
Using Different key types between Map input and output
Now, to discuss the answer given by Chris. I agree that the InputSampler and TotalOrderPartitioner are probably flawed in some ways, since they are really not easy to understand and use ... But they do not impose key types to be the same between map input and output.
The InputSampler uses the job's InputFormat (and its RecordReader) keys to create the partition file containing all sampled keys. This file is then used by the TotalOrderPartitioner during the partitioning phase at the end of the Mapper's process to create partitions.
The easiest solution is to create a custom RecordReader, for the InputSampler only, which performs the same key transformation as your Mapper.
To illustrate this, let's say your dataset contains pairs of (char, int), and that your mapper transforms them into (int, int), by taking the character's ascii value. For example 'a' becomes 97.
If you want to perform total order partitioning of this job, your InputSampler would sample letters 'a', 'b', 'c'. Then during the partitioning phase, your mapper output keys will be integer values like 102 or 107, which wouldn't be comparable to 'a', 'g' or 't' from the partition-file for partition distribution. This is not consistent, and this is why it looks like the input and output key types are assumed to be the same, when using the same InputFormat for sampling and your mapreduce job.
So the solution is to write a custom InputFormat and its RecordReader, used only for the sampling client-side job, which reads your input file and does the same transformation from char to int before returning each record. This way the InputSampler will directly write the integer ascii values from the custom record reader to the partition-file, which maintain the same distribution, and will be usable with your mapper's output.
It's not so easy to grasp in a few lines of text explanation texts, but anybody interested in fully understanding how the InputSampler and TotalOrderPartitioner work should check out this page : http://blog.ditullio.fr/2016/01/04/hadoop-basics-total-order-sorting-mapreduce/
It explains in details how to use them in different cases.

Input Sampler in Hadoop


Close Jquery UI tab, on dialog button click

Question: I'm using Jquery UI Tabs. This stays on my index.jsp where on click of the search button the following function gets called and a tab is added content is loaded.
function addTab(url, title) {
  var id = "tabs-" + tabCounter,
    li = $( tabTemplate.replace( /~\{href\}/g, "#" + id ).replace( /~\{label\}/g, title ) ); tabs.find( ".ui-tabs-nav" ).append( li );
  tabs.append( "

" );
  $("#" + id).load(url);
  $("#accordion").accordion();
  tabs.tabs( "refresh" );
  tabCounter++;}

On successful search, it renders success.jsp into the tab. But in case of invalid input I'm rendering a error.jsp file which contains the following sample code.

The entered ID was not found.

On click of the dialog box I want it to close the dialog box, AND the tab it's in. I've tried a few possible solutions I've found online but didn't get it to work. Any help?
EDIT 1: Still no luck in finding an apt solution. Is there any more information that's required for someone to answer? Please let me know so that I can do so. Basically want to be able to close the tab, in which a dialog box is present, by clicking a button on the dialog box.
Comment: what your Jquery UI Tabs html?
Comment: Jquery UI Tabs is a widget from Jquery UI which I've used.
Answer: 2 ways to do it :
1) After the page have been loaded in the new tab, add some static code inside the new tab. This code will be adapted with the tabs-'id' defined for the tab.
$("#" + id).load(url)
// something like :
$("#" + id).append('close this tab')
$("#accordion").accordion();

2) add to the url the tabCounter and modify the jsp page in order to use this tabCounter for killing it self.
url += "?"+tabCounter;

new JPS page returned by the server :

The entered ID was not found.

;)
Comment: I didn't quite get your first solution. The second one sort of works but I prefer not to pass the values as parameters each time. Is there another way? Also by this method I can remove the tab content, how do I remove the tab entry itself?
Answer:    var activeTab = tabs.find( ".ui-tabs-active" ).remove().attr( "aria-controls" );
    $( "#" + activeTab).remove();
    tabs.tabs( "refresh" );

In case anyone else faces this issue,This worked for me. I'm getting the active tab the dialog is open in, removing the aria controls attribute which removes the tab, followed by removing the tab content itself.


If $\phi\psi=id$, then $\phi$ is surjective and $\psi$ is injective

Question: The complete context of the question is the following. Suppose I have $End(V)$, which is the $K$-algebra of all endomorphisms of a vector space $V$ over a field $K$ with function composition as the bilinear map. Let $\phi \in End(V)$ be a unit of $End(V)$, thus there is some $\psi \in End(V)$ such that $\phi\psi=\psi\phi=id$. What I want to prove is that $\phi$ is surjective and $\psi$ is injective.
Here's what I have so far, I just want to make sure it is a proper proof since I feel like I'm missing something. To prove that $\phi$ is surjective, since $\phi\psi=id$, then $\phi(\psi(\mathbf{v}))=\psi(\mathbf{v})$, for some $\mathbf{v} \in V$. The identity map is surjective, and so $\phi$ is surjective. I'm not quite sure this is correct since it feels like I am saying that $\phi$ is the identity map, which it obviously doesn't have to be. But my understanding of $\phi\psi=id$ is that $\phi$ is just mapping its argument to itself, so it acts as the identity mapping. Someone correct me if this isn't the proper way of interpreting this.
To prove that $\psi$ is injective, suppose $\psi(\mathbf{v}_{1})=\psi(\mathbf{v}_{2})$. Then $\phi(\psi(\mathbf{v}_{1}))=\phi(\psi(\mathbf{v}_{2}))$. Based on other answers here, I should show that $\phi\psi$ is injective so then $\mathbf{v}_{1}=\mathbf{v}_{2}$ and thus prove that $\psi$ is injective. However, I am not sure how to prove that $\phi\psi$ is injective. Would I use the fact that $\phi\psi$ is the identity map and the fact that the identity map is injective? But again, that would rely on whether I am correct in my interpretation of $\phi\psi=id$.
Is this proof correct or is there something I am doing wrong? Any help would be appreciated.
Answer: Let $f:X\to Y$ and $g:Y\to Z$ be functions. Suppose $g\circ f$ is surjective.
We shall prove that $g$ is also surjective.
Indeed, it always holds that $g(Y)\subseteq Z$. We need to prove that $Z\subseteq g(Y)$:
\begin{align*}
Z = (g\circ f)(X) = g(f(X)) \subseteq g(Y)
\end{align*}
because $f(X)\subseteq Y$, and we are done!
Answer: Meditate on the following. If we compose a non-injective function with any function at all, the result is not injective. If we precompose a non-surjective function with any function at all, the result is not surjective.
Answer: Over here, when they write $\phi \psi$, what they really mean is $\phi \circ \psi$. So:
$$\forall v \in V: (\phi \circ \psi)(v) = \phi(\psi(v))$$
Your proof for surjectivity doesn't work. So, you know that $\phi \psi = id$. But this doesn't imply that $\phi(\psi(v)) = \psi(v)$ for every $v \in V$. To prove this, let $v \in V$. We want to show that there is a $w \in V$ such that $\phi(w) = v$. But now, you know that $\phi(\psi(v)) = v$. So, if we choose $\psi(v) = w$, then we're done. That proves surjectivity. Your proof for injectivity is correct and you know that $\phi \psi$ is injective precisely because it is the identity function and the identity function is injective.


How would you retrieve message and optional attributes from PubSubIO?

Question: I have a topic on PubSub, and I want to create a pipeline on dataflow that reads from the topic classifies them according to message data and further read the attribute key value pairs.
Is there a way to differentiate between the message and attributes after they have transformed to PCollection using the PubSubIO's read transform?
Answer: Unfortunately, Dataflow's PubsubIO API doesn't currently handle metadata and attributes of the Pubsub messages. I cannot think of an easy workaround.
This is something we are looking to improve rather soon. This work is tracked as BEAM-53. Please check there for the most recent status.
Comment: In the latest Apache Beam PubSub IO you can get the attribute list of the message using getAttributeMap and getAttribute.  See https://beam.apache.org/documentation/sdks/javadoc/0.6.0/org/apache/beam/sdk/io/PubsubIO.PubsubMessage.html#getAttribute-java.lang.String-
Comment: Thank you. That was pretty helpful.


Why does the PHP add extra space in the email body for mail()?

Question: Recently I use the mail($to, $subject, $content, $headers) to send email with a very long content including some images link like http://foo.com/image/1.jpg. The problem is that the email body sometimes break at some point and show the link like http://foo.com/image/1.jp+g, which will break the link.
Or sometimes it just break the html tags like break
 in to
, and finally the tag is shown in somewhere that is not supposed to be. Making the email looks so wired.
This problem has bugged me for a whole morning.
Answer: Actually, I tracked down this problem and find that the body is too long that in the email, it will add newline automatically in the $content when applied in the mail() message body. As a result the image link will be likehttp://foo.com/image/1.jp
g
 and the tags will be like

This problem has troubled me for a whole morning.
Now I found the solution would be adding \n at some point in your message so that your mail body is not only on one line, and the mail() will not add its own new line at the point that you do not want it to.
Hope this can help someone if they have the similar problem.
Comment: You can also tick your own answer.


A 2-Part Test (or The Squeeze Thm.) Question

Question:
Find the limit, if it exists.

I am sure that one of the paths which uses the line y=mx does not work.
I am not sure for y=x^m because it lead me to
*mtan(x^2+m^2*x^2)/(x^2+m^2)*
What should I do?
]1
Comment: In your limit, $y$ is kept fix. Consider two cases: first if $y=0$ and second if $y\neq 0$. In the first case, your function is simply 0 so the limit evaluates to 0. In the second case, your function is continuous at zero and the limit once again evaluates to 0
Answer: Let consider the more general
$$\lim_{x,y\to0} \frac{xy \tan(x^2+y^2)}{x^4+y^2}$$
we have that
$$\frac{xy \tan(x^2+y^2)}{x^4+y^2}=\frac{xy(x^2+y^2)}{x^4+y^2}\frac{ \tan(x^2+y^2)}{x^2+y^2}\to 0$$
indeed
$$\frac{ \tan(x^2+y^2)}{x^2+y^2}\to 1$$
and
$$\frac{xy(x^2+y^2)}{x^4+y^2}\to0$$
indeed consider
$$\frac{|xy|(x^2+y^2)}{x^4+y^2}$$
and let

$x^2=r \cos \theta$
$y=r \sin \theta$

thus
$$0\le\frac{|xy|(x^2+y^2)}{x^4+y^2}=\frac{r^3\,\sqrt r\sqrt{|\cos \theta|}\sin \theta}{r^2}=r\,\sqrt r\sqrt{|\cos \theta|}\sin \theta \to0$$
and for squeeze theorem
$$\frac{|xy|(x^2+y^2)}{x^4+y^2}\to 0 \implies \frac{xy(x^2+y^2)}{x^4+y^2}\to0$$


Sanitize and Highlight Text with HTML at the same time

Question: My web site offers a possibility to store user data and to search for it again. When searching, all data is being printed that contains the query. I want the matching substrings to be highlighted.
In simplified terms I did it like this:
function highlight_and_sanitize_search_result($data, $query){
   $highlighted = str_replace(
                       $query,
                       '' . $query . '',
                       $data
                  );
   return sanitize($highlighted);
}

When I print the return value of the function, my HTML code is not sanitized. But when I sanitize it, my  will falsely be sanitized, too:
 Query:  match
  Data:  Here is the match
Output:  Here is the <span class="match">match<span>

So I thought I might put it the other way around:
function sanitize_and_highlight_search_result($data, $query){
   $sanitized = sanitize($highlighted);
   return str_replace(
               $query,
               '' . $query . '',
               $data
          );
}

But this also leads to an error if you search for a query which is part of the sanitized HTML code:
 Query:  &am
  Data:  You & me
Output:  You & me

So what is the right way to do both - sanitation and highlighting?
Answer: Sanitize both your data AND the query. Then the sanitized query will still be found in the sanitized HTML data.
function sanitize_and_highlight_search_result($data, $query){
   $sanitized = sanitize($data);
   $query = sanitize($query);
   return str_replace(
               $query,
               '' . $query . '',
               $sanitized
          );
}

Example: http://3v4l.org/R9n9U


add subview to subclassed UITextField not showing

Question: I have subclassed a UITextField and trying to add a subview by calling [self.superView addSubview:newView] but this is not working and self.superview.class returns null.
How can one add a subview to the superView
The view is showing on the screen and done using Storyboard
@interface NewTextField : UITextField <UITextFieldDelegate>

@end

and then
@implementation MBTextField

- (id)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self addWhiteLine]; // here im adding a view
    }
    return self;
}

- (void)addWhiteLine{

    UIView *div = [UIView new];
    div.frame = CGRectMake(self.frame.origin.x,
                           self.frame.origin.y + self.frame.size.height+borderWidth,
                           self.frame.size.width,
                           borderWidth);

    div.backgroundColor = [borderColor colorWithAlphaComponent:0.3f];
    [self.superview addSubview:div];

}

Solution: accepted answer and comments by @mackworth. Frame of the view had to be changed to following.
    div.frame = CGRectMake(0,
                           self.frame.size.height - borderWidth,
                           self.frame.size.width,
                           borderWidth);

Comment: At the time you call `addWhiteLine`, the text field hasn't been added to a view yet.
Comment: And it's bad to have this custom view add a view to something other than itself. Now you have to be sure to remove the other view as well as move the other view as `self` is moved around. Just add the subview to `self` to keep it simple.
Comment: Why are you adding newly created view to textfield's superview rather than adding it as a subview of textfield?
Answer: If self.superview is nil, then at the time addWhiteLine is being called, self hasn't been added to the view hierarchy yet. This actually makes sense given that you're calling it from the initWithCoder. Whoever's loading this view has to alloc/init before adding it to a superview.  As a fix, why add to self.superview, shouldn't you add to self instead?
Edit: As discussed in comments, another problem is that the line is being drawn outside the frame, so it should be self.frame.origin.y + self.frame.size.height-borderWidth instead.
Comment: adding to self is also not showing anything on the screen. I made it significantly higher height and still nothing.
Comment: origin.y+height+borderWidth will take you off the bottom of the frame, no?  http://www.idev101.com/code/User_Interface/view_frames_bounds.html
Comment: I think your comment is key to solution. It jumps outside the text field frame which is why I was trying to add to superview. If I keep in frame it overlaps the text in the textField. Any suggestions?
Comment: What's your goal for the line? if you want it at the bottom of the field, then it should be self.frame.origin.y + self.frame.size.height-borderWidth, but is that what overlaps the text?


Song structures and tonality

Question: It has to be prety basic but I never understood how it is conventionally explained.
Usually a pop song has a fixed key like in:
Cmajor:
Verse: C-Am-F-G
Bridge: Am-G, Am-G
Chorus: F-G-Am
How is this analysed?
1) Cmajor song, C root always
2) Cmajor song, C root, then A root in eolian, then F root in lydian mode?
I mean, when Bridge comes, is that a VIm-V or a new Im-V after a key change?
I guess my mistake is that C root and C key does not mean the same
Answer: I would say that the song is in the key of C the whole time.  I would only analyze the bridge as a modulation if you were not playing in Aeolian, which would have to be Dorian given the major chord a whole step below.  However, if I were analyzing for a school assignment I would probably put both or justify why you chose one or the other in the written portion.  If I were to analyze it as being in different modes I would probably say that the bridge and chorus are actually in Aeolian.  With the chorus I would say this because I'm guessing it feels resolved when you arrive on the Am.  Analysis is open to interpretation though so if you feel the F is root it may be.  Also, the roman numeral you suggested for the G in the bridge (V) would actually be called VII or bVII in Aeolian: Im-VII, which could also be written: i-VII or i-bVII, where lower case roman numerals represent minor.
Root is usually used when talking about a chord, which represents the 1, even if 1 is not in the bass.  The term tonic is usually used to refer to the 1st degree of the key you are in.  If you choose to analyze each section as having its own mode, then I would say the tonic changes with each section.
Answer: C key means tonality, C root is a C chord in the root position (with lowest note C). This can though have different interpretations in different countries, C root might also mean a chord (like F or Am) that has a C as lowest note.
There are always many ways to analyse, different ways to see it that are correct. I would say depending on the size/length of the bridge I would analyse as a Key change or not. Also if inside the section you have different chords that build a relation around the new I, then analyzing/concluding a key change would be more correct.
Answer: The analysis is:
Verse: I VIm IV V
Bridge: VIm V VIm V
Chorus: IV V VIm
When you focus on the root and fifth of the VIm, you are creating a minor tonality.
when you focus on the root and fifth of the I, you create a major tonality.
The other chords are cadences that reinforce the tonality. A cadence may reinforce either the I or the VIm tonality.
The bridge is in the same key, not a different key, because V can resolve to VIm just as it resolves to Imaj.
When the song reinforces Am, then you say that part of the song is in Am (Aeolian). when the song reinforces Cmaj, then you say the song is in C major (or more specifically, the "key of the moment" is C major).
The roots of the chords don't tell you the tonality or key of the song, they just tell you where the chord starts.
Comment: I would just like to point out a couple things here: You need to do more than just "focus" on a root and fifth to create tonality. Also, *not all other chords* are cadences.  Cadences are punctuation in musical phrasing.  Saying every chord is a cadence is, like, typing, a. sentence. like. this. Cadences do not always reinforce tonality - sometimes doing exactly the opposite!  Also, traditionally when using roman-numeral analysis, the numerals are lowercase for minor chords such that the Verse progression would look like: **I-vi-IV-V**.  Just an FYI :)
Comment: @jjmusicnotes: I've seen all manner of Roman numeral notation. I decided on a particular style for myself because it makes other notation unambiguous (I use lower roman numeral for key tones in the major scale.) Regarding your other comments, of course not all chords are cadences, but the point here is not terminology, but the concept of whether you are using chord progressions to establish tonality or not.
Comment: agreed that the point here is not terminology, however, terminology should be used correctly in addressing the intended point.  Regarding roman numerals, there are many types of numeral notation - not all of them correct.  What I put forth is the scholarly accepted convention of roman numeral notation. Made-up or esoteric systems may be helpful on an individual basis, but given the nature of this site, it is important to keep at least national interests in mind.

Song structures and tonality


Allow character variants in username

Question: I am trying to import some users from a phpBB forum and found an odd thing: in phpBB I have some users with weird characters in the username. Eg: Henriëtte .
The problem is that in Drupal I can't create 2 users with the username "Henriëtte" and "henriette". Apparently "Henriëtte" = "henriette" and this will give an warning saying that the username already exists.
Is there any way to import both usernames in Drupal? Thanks.
[LATER EDIT]
The database encoding is utf8_general_ci. Also, after further investigation the user_load_by_name() function is responsible for returning a valid user even if one letter is different (ë > e). Also, user_load_by_name will in the end use entity_load() to load the user object.
Comment: What sort of encoding is your database using?
Comment: utf8_general_ci
Comment: That was the problem. I changed it to `utf8_bin` and now it is working as expected
Comment: That's good news
Answer: Found the answer.
The encoding should be utf8_bin not utf8_general_ci

Allow character variants in username


How to iterate over a JsonArray in Java (not java 8)

Question: I would like to iterate over an array in this Json object:
{
  "id": "2234",
  "Messages": [
    {
      "MessageId": 321231239,
      "Text_message": "my text message",
      "date": 1444666348
    },
    {
      "MessageId": 3217437239,
      "Text_message": "my text message 2",
      "date": 1444666348
    }
  ]
}

in my code I have:
JsonArray messagesJson = jsonObject.getJsonArray("Messages");

my imports related to this are:
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;

Now I want to iterate over this array and extract fields for my bean.
But I don't want to use the for loop with index. I want to use a foreach type of loop. I am working with Java 7.
What I want is something that is more like the Scala way of doing it:
messagesJson.map { json =>
    MyBean(
        (json \ "MessageId").getOrElse(""),
        (json \ "text_message").getOrElse(""),
        (json \ "date").getOrElse("")
    )
}

Comment: use Gson o jackson, they simplify the iteration and are developed to make easier the life
Comment: What's wrong with `for ( JsonValue val : messageJson ) ...`?
Answer: As the documentation says, JsonArray extends List<JsonValue> so you can simply iterate over JsonValue.
for(JsonValue value : yourJsonArray){
....
}

Comment: How would you turn value into JsonObject?
Comment: you can use getValueType() and check if the result is equal to one of the values of JsonType.ValueType enum. Take a look here: http://docs.oracle.com/javaee/7/api/javax/json/JsonValue.ValueType.html


Como criar expressão para permitir apenas alguns caracteres?

Question: Estou a criar um input onde um onKeyUp verifica os caracteres inseridos, ele deve validar o seguinte:

Não há quantidade de caracteres máximos
O primeiro dígito deve ser apenas números [0-9]
O código é composto de dígitos alfanuméricos e apenas os caracteres / (barra) e - (hífen), e nem sempre terão todos eles.

válidos:
260509
8605/05
5ABC605/05
756574-7
88BS-AS0

Inválidos:
A8605/05
B756574-7

Utilizando a expressão:
/^\d([\w\/-]*)/gm

Eu consigo dar match nos valores de forma correta, porém preciso inseri-lo na função:
this.value=this.value.replace(/^\d([\w\/-]*)/gm,'')

Mas a função deve ter o funcionamento invertido, quando não der match, substituir por " ".
Comment: Será que é uma boa ideia fazer isso no keyUp? Veja [essa pergunta](http://stackoverflow.com/q/9205164/520779) que postei no SOen há bastante tempo, e [essa resposta](http://stackoverflow.com/a/9205475/520779) em particular. Dito isso, é possível sim fazer uma expressão como a que você quer, vou postar em breve como resposta.
Comment: Eu não acho que seja @mgibsonbr, porém é exigência do cliente.
Comment: Eu estou um pouco confuso com esse `m` - a entrada é um conjunto de códigos separados por quebras de linha, ou a substituição ocorrerá linha por linha?
Comment: Acredito que o `m` pode ser retirado, pois o valor será um único código digitado no input.
Comment: Efetuando a substituição do método `replace` por `match`, ocorreu o resultado esperado porém não tenho certeza se é a solução ideal: `this.value=this.value.match(/^\d([\w\/-]*)/g);`
Comment: É uma possibilidade, mas se houver um caractere inválido no meio do caminho ele vai dividir o campo em dois. Ex.: `123$567`
Comment: Ele não permitiu inserir um caractere fora do match, não entendi como mas funcionou.
Comment: Experimente copiar o exemplo acima e colar no campo com o seu código. Ele vai ser substituído por `123`, e não por `123567`.
Answer: O problema de validar enquanto o usuário digita é que é necessário não que o campo inteiro seja válido mas que um prefixo do mesmo o seja (pois senão o usuário nunca ia conseguir acabar de digitar, a menos que colasse o valor inteiro com Ctrl+V), e somente no final uma validação do campo completo pode ser feita. No seu caso entretanto é fácil, já que somente um único dígito é obrigatório e os demais são opcionais:

document.querySelector("input").onkeyup = function(e) {
    this.value = this.value.replace(/^\D.*|[^\w\/-]/gm,'');
}
<input>

Esse código elimina o campo inteiro se o primeiro dígito não for um número, ou elimina cada caractere específico que não esteja no conjunto dado (i.e. seu conjunto de caracteres permitidos, negado). Se o usuário colar o campo de algum outro lugar, somente os caracteres válidos permanecerão.
A principal desvantagem desse método é que ele não preserva o caret. Seria altamente desejável que o usuário pudesse voltar com as setas e corrigir um trecho incorreto, mas isso complicaria um pouco as coisas.
Nota: essa resposta assume que somente o trecho incorreto deve ser eliminado na substituição, não o código inteiro - caso contrário, se o usuário estivesse digitando e acidentalmente entrasse com um caractere inválido, e o campo todo sumisse, acho que ele não ia ficar muito satisfeito...
Comment: A solução funciona corretamente, exceto no bendito IE. Infelizmente preciso que funcione nele também. Quando o valor inserido no input não dá match, ele insere NULL no campo e não permite nem mesmo apagar. Saberia o motivo disto ocorrer?
Comment: @MarcelodeAndrade Infelizmente eu só tenho o IE11 pra testar, e que eu saiba a partir do IE9 as expressões regulares funcionam sem problemas. Não sei como fazer pra funcionar no IE8 ou anterior, o jeito pode ser testar o valor de retorno por NULL e, se for, não fazer a substituição (sendo um browser em versão antiga, creio que seu cliente vai aceitar a limitação).


Prevalence conversion to effect size in meta-analysis

Question: I'm used to thinking about meta-analyses in terms of evaluating the impact of an IV on an outcome of interest (ex: relationship between a teacher's level of self-confidence and student outcomes), and I'm used to calculating r (or using t-test, F or p-values, etc to calculate g or d and then converting that to r).
However, for my current study I'm interested in first describing the prevalence of a particular attitude; specifically, to what extent do professionals (physicians, police, clergy, etc) hold stigmatizing views of the eldery? However, I'm not initially trying to compare across groups, and many studies only have one type of participant (e.g., all physicians).  I've found a bunch of studies that essentially gave a measure of stigma to a set of participants and reported either:
 a) The number or percent who responded with stigmatizing views   or
 b) The average and standard deviation stigma score (on a continuous scale) for the sample.
Is it possible to convert this data into an effect size? I've found many effect size calculators, but none that seem to deal with converting the prevalence of a trait from a single group into an effect size. I've seen several references to "proportion, prevalence, or central tendency effect sizes" but no formulas.
Any advice on extracting information from these papers and converting it to d, g, or r (or even an odds ratio) would be greatly appreciated!
Answer: Following Sutton et al. (2000: 18f), I would suggest converting proportions $p$ to logits:
$logit = log(odds) = log(\frac{p}{1-p}).$
Using the number of cases with an event ($N_{event}$) and without an event ($N_{\neg event}$), the variance of $logit$ is given by
$Var(logit) = \frac{1}{N_{event}} + \frac{1}{N_{\neg event}}$.
Reference
Sutton, A. J., K. R. Abrams, M. Jonas, T. A. Sheldon und F. Song, 2000a: Methods for
meta-analysis in medical research. Wiley series in probability and mathematical
statistics, Chichester; New York: Wiley.
Comment: Thank you! Now I'm struggling with how to work with the different scales in this format. Studies use different scales which aremeasuring the same construct. I could:
Comment: Thank you @Bernd Weiss ! Follow up question: These formulas are for dichotomized or categorical data. The literature I have measures stigma on various continuous scales.  I could: 1) say that anyone who scores 1 st. deviation above the mean qualified as stigmatizing; or 2) create cut points for each scale (though they would be arbitary). Neither seem like great options. If you are aware of other ways to use continuous scales to determine prevalence/severity, I would appreciate the advice.
Comment: @user30295 No, I am sorry. In that case you probably have to deal with multiple dependent variables and run one meta-analysis per dependent variable.


Stuck With The Differentiation Of A Inverse Hyperbolic Function

Question: I'am suppose to show that $$\frac{\mathrm{d} }{\mathrm{d} x}[x \operatorname{cosh}^{-1}(\cosh x)] = 2x$$
And this is what i've tried.Upon differentiating the above function wrt $x$ using  the product rule and applying the formula $\cosh^{-1}f(x) = \frac{f\prime(x)}{\sqrt{[f(x)]^2 - 1}}$, I end up getting $$x\frac{\sinh x}{\sqrt{(\cosh )^2 - 1}} + \cosh^{-1}(\cosh x)$$
How to proceed from here?
Thank You
Comment: What is the capital of the country whose capital is Ottawa?
Comment: http://en.wikipedia.org/wiki/Inverse_function#Inverses_and_composition
Comment: I got it!!! $\cosh^{-1}(\cosh x) = x$ sorry to bug you..
Answer: You're working way too hard. Simplify! What is $\cosh^{-1}\big(\cosh x\big)$?
Comment: @alok: Stop thinking about differentiation. That will come **after** you've simplified $\cosh^{-1}(\cosh x)$. Yes, $\ln e^x=x$; that's because if $f(x)=e^x$, $\ln x=f^{-1}(x)$. The definition of $f^{-1}$ says that $f^{-1}(f(x))$ is always just $x$.
Comment: Exactly.I've used $\cosh^{-1}f(x) = \frac{f\prime(x)}{\sqrt{[f(x)]^2 - 1}}$ and now i'am unable to simplify.
Comment: @alok What is $f^{-1}(f(x))$ if $f(x)$ is invertable?
Comment: @alok: What is $\ln e^x$? More generally, if $f$ is any invertible function, what is $f^{-1}(f(x))$?
Comment: Composition of a function.Chain Rule isn't it? Not sure
Comment: @BrianM.Scott $ln e^x = x$.
Comment: @alok Also, if we let $f(x)=\ln x$ and $f^{-1}(x)= e^x$ we also find that $f^{-1}(f(x))=\ln e^x =x$ as well.
Answer: If we are being fussy, the answer is not $2x$.
The function $\cosh x$ is an even function, like $\cos x$. Note that $\cosh^{-1} w$ is the non-negative number whose $\cosh$ is $w$. Now let $x$ be negative. Then $\cosh^{-1}(\cosh x)=-x$. Alternately, $\cosh^{-1}(\cosh x)=|x|$.
So the function we are trying to differentiate is in fact $x^2$ when $x\ge 0$ and $-x^2$ when $x\lt 0$. Its derivative is $2x$ when $x \ge 0$ and $-2x$ when $x\lt 0$, or alternately $2|x|$.


Count number of selected rows in ag-grid

Question: I want to count the number of selected rows but it seems onRowClicked or onRowSelected happens before the row i click actually is selected.
How can I count the number of rows when I select a row? I thought this was the way to go.
this.gridOptions.onRowSelected = function (params) {
    console.log(this.gridOptions.api.getSelectedRows().length)
    // Prints one less then the number of rows that is selected.
}

Answer: In a React (with ES6) project I use this:
onSelectionChanged = debounce((e) => {
    this.setState({
        selectedRows: this.api.getSelectedRows(),
    });
}, 1)

(this.api is https://www.ag-grid.com/javascript-grid-api/?framework=all#gsc.tab=0)
I use Lodash's debounce to solve a performance problem if many rows are selected at the same time. (Although after some quick testing, looks like this has been fixed in ag-grid at some point.)
The simplest version could look like (untested):
this.gridOptions.onSelectionChanged = function () {
    console.log(this.gridOptions.api.getSelectedRows().length);
}


What happens after AIR certificate expires?

Question: I have desktop AIR app which has been signed with a valid certificate from Chosensecurity. The certificate will expire later this month.
When it expires, will it affect the apps that has been already installed? And will I be able to keep letting users download/install this same version? Of course, if a new version of the app is released, I'd have to renew the certificate but for few months there will not be a new version.
Answer: As long as you set a timestamp when you apply the codesigning, that particular module is good forever. (or at least until/if the certificate is revoked, not expired).
Expiring is a normal process that does not effect older signed modules.
Applying the timestamp looks something like this in Windows:
signtool.exe sign /v /f $(ProjectDir)..\DennisSign.pfx /t http://timestamp.verisign.com/scripts/timestamp.dll $(TargetPath)


Javascript invalid regular expresion error in NW.js

Question: I have a regular expression which I need to test with a string, it works when I test it online
^(?![.])^[.a-zA-Z0-9]*(?<!\.)$

You can test it : https://regex101.com/r/bomm5g/1/
But when I write a code for it, I get this error
Invalid regular expression: /^(?![.])^[.a-zA-Z0-9]*(?<!.)$/: Invalid group

The code is as below
var regex = new RegExp("^(?![.])^[.a-zA-Z0-9]*(?<!\.)$");
regex.test(inputvalue);

How can I get this to work ?
Comment: Did you read the error that is mentioned by regex101.com (_"`?` The preceding token is not quantifiable"_)?
Comment: I ran your code in Chrome's console and I got no error. However Regexpal.com says : `Lookbehind is not supported in JS`.
Comment: There is no test subject in your regex101.
Comment: The code you have posted doesn't throw that error. Which browser are you using? Also, you need to escape backslashes if you are using `RegExp` constructor: [Regex created via new RegExp(myString) not working (backslashes)](https://stackoverflow.com/questions/4914484)
Comment: @adiga I am using NW.js(nodewebkit previously) which is chromium based.  I don't have any backslashes in the expression as such.
Comment: There is a backslash here: `(?<!\.)`
Comment: Many browsers, including Firefox and Edge don't support lookbehind.
Answer: @Abbas just try this
var regex = new RegExp(/^(?![.])^[.a-zA-Z0-9]*(?<!\.)$/);

instead of var regex = new RegExp("^(?![.])^[.a-zA-Z0-9]*(?
Comment: Got the same error
Comment: `new RegExp` expects a string as parameter!
Comment: @Toto it also takes the pattern. It's working fine for me.


Augmented 6th chord in Haydn String Quartet op.74 no.3, II

Question: In measure 34, we modulate from C major to E minor. In m. 36, the notes played are C in the bass; E, F♯, and G in the viola; G, F♯, E in the second violin; and a whole note A♯ in the first violin. I labeled the chord as Fr+6/(third degree) but my TA says it is a Ger+6/(third degree). Can someone explain this? I can't build this chord. When I try, I get A♭, C, E, F♯ but the passage has an A♯ and not a A♭.
Comment: Welcome to the site! We don't do homework for people here because it's unlikely to help other people.  Unfortunately, Patrx2 has already given a great answer, so it would be great if you could edit your question to be a bit more broad - such as making it clear that you're asking about Aug6 chords, citing the Haydn as an example (and removing the bit about the TA). Free Advice: When you analyze theory, ask yourself how it *sounds*, not every not is equally important; remember the larger picture.
Answer: OK, here's the bar in question (4th bar in the system):

So, the notes in the chord are (SATB order): A♯, G, E & C. We can ignore the F♯s - they are pretty much passing tones between the chord tones.
Now, a French sixth on a C bass would be (again SATB order) A♯, F♯, E & C - French sixths are whole tone chords that tend to act like a gapped dominant seventh chord to the following chord (in this case, F♯7 moving to B7) over the descending leading tone to the following harmony (in this case, C-B in the bass).
The German sixth, however, uses the same tones as a dominant 7th built on the bass as a root, but spelt differently as the "seventh" ascends to the tonic of the following chord (rather than descending to the third of a chord a fifth below), thus acting as an augmented sixth. Note that the German sixth is not a whole tone chord.
Check the Wikipedia entry. The German sixth has parallel fifth problems not inherent in the French sixth, and Haydn went out of his way to avoid them here.
Edit: what your TA meant by Ger+6/3 was a first inversion chord - the C in the bass is the third of the chord, with A♯ as the "root" of the chord (forming a diminished third with C; when placed in first inversion, A♯ forms an augmented sixth with C).
The distinguishing trait of all augmented sixth chords is that they have two leading tones to the root of the following chord, a descending "Phrygian" leading tone in the bass, and an ascending leading tone in an upper voice (usually the descant). The harmonic function of augmented sixth chords is determined by melodic movement, not root movement.
Indeed, for French sixths, determining a harmonic root for the chord isn't really possible: if you invert the chord so that (in our case) G♭ (F♯) is in the bass, you have another French sixth that moves to the root a tritone away from the original destination (in our case, that would be F rather than B). This kind of indeterminacy is common with whole tone chords. The French sixth, however, functions just like the other forms of augmented sixth because of the two leading tones.
Comment: @alephzero, not quite. Haydn's time used well-tempered tunings (Young, Vallotti, etc.) - not equal, but re-entrant. B♭ and A♯ would be effectively the same note, but the intervals formed with other members of the chromatic scale would not be in neat multiples of 100 cents - the comma was distributed so as to give preferential purity to C major while avoiding wolf fifths in more distant keys and closing the cycle of fifths. That being said, string players would tend to shrink semitones in the direction of movement by adjusting the leading tones, _and they still do_.
Comment: The basic "flavour" of the German 6th has been somewhat obscured by Equal Temperament tuning. In ET, the chords C E G A# and C E G Bb are identical. In the unequal temperaments of Haydn's time, A# was *not* the same note as Bb. The C E G A# chord sounded best resolving onto B major, each note moving by a semitone (that ignores the issue of consecutive 5ths, of course) whereas C E G Bb sounded best resolving as the dominant 7th of F major. In equal temperament, the tritone interval between the resolutions on F and B is analogous to tritone chord substitutions in jazz harmony.
Answer: It's a German 6th resolving on (and temporarily tonicizing) B major which is the dominant of E major - the key of the new section.
Answer: The main issue you're running into is deciding your local key; you are expecting A♭, C, E, and F♯ because that's the Ger+6 in the key of C. (Actually, it would be E♭, not E♮; use the minor third scale degree, just like you use the minor sixth scale degree in the bass.)
Since you're moving to E (as you said), we'll build it on the same scale degrees: (♭)6, 1, (♭)3, and ♯4: C, E, G, A♯. [Note that by (♭) before these scale degrees I mean they're the lowered version. In major we have to actively lower them a half step, but in minor the key signature handles that for us.)
Comment: Nope. The German sixth only differs from the root position dominant seventh on the bass note by interpreting the seventh as an augmented sixth. Augmented sixth chords derive from the Phrygian cadence, which is iv6 - V (- i or I). In E major, iv6 would be A minor with C in the bass. The Italian sixth sharps the root (A in this case) to create a leading tone to the dominant; the other two add another tone to the It+6 to smooth the voiceleading. _All_ of them work around a base (in the key of E) of A♯, E & C.
Comment: We're saying the same thing in two different ways; I was mainly trying to address OP's remark of "When I try, I get A flat, C, E, F# when the passage has an A# and not a A flat." It was clear s/he was thinking in C, not in E.
Comment: Ah, OK. I never even considered a key of C because it is, _at very best_, a passing tonality here, so this caught me a bit off-guard - the previous two bars strongly suggest a move through A minor to 2nd inversion E minor just prior to the augmented sixth, so I assumed that the cadence in E was quite clear, and we had a misunderstanding of how German sixths were spelt. My bad.


Forecast Vs Actual accuracy calculation

Question: I have two time series, first is forecasted values (results of some forecasting algorithm) and second series is, actual values observed for same time frame.
We are trying to compare both these series and find out how much forecasted values are inline with actual values. We have used MAPE & MAE formulas which are fine when the difference between both are small values.
When difference between actual and forecast values are far off, MAE and MAPE are bigger values (I understand that MAPE has no upper limit, which is why bigger values).
These values make sense for statistician, but when common users saw these numbers,  feedback we got was our accuracy calculation is doomed. Now the task we are trying to answer is, how can we calculate the difference between forecast vs actual (using MAE (or) MAPE (or) some other algorithm) and show it with in range of 0 - 100%?
Any suggestions would be appreciated.
EDIT 1: These numbers are handmade to convey the problem we are trying to solve, please don't consider about what forecasting algorithm may improve our forecast values etc., irrespective what best algorithm we use, there are few data sets we have could force us into this particular situation.
Here is example time series:
Comment: If we just look at the 9 observations you listed, your forecast is indeed poor, and a naive model would work better.  But there may be a lot more to the series we're not seeing -- for example, these may be off-season forecasts, where the mean forecast and actual may both be, say, 2000.  How do you subjectively feel about the quality of these forecasts?
Comment: Most forecasts are procedures that begin with fitting a model in a way that minimizes some measure of accuracy. You should prefer to use the same measure in assessing the quality of the forecast!
Comment: The best way is to compute the cost of forecast errors in units that business users understand. For instance, if it's sales forecast, then there could be a cost associated with error in dollars. The cost is usually asymmetric, btw.
Comment: @Nambari, let's say actual is \$100, and your forecast was \$500. What is the accuracy in your understanding?
Comment: @Nambari, it's not common to think in terms of accuracy where precise forecast is 100% and bad forecast is 0%. I think this is your issue with applying MAPE, you're not using it conventionally.
Comment: @Nambari, in my example the conventional understanding would be that my \$500 forecast was $\frac{500-100}{100}*100\%=400\%$ error, not 0% accuracy as you suggested. Also, be careful with using [*accuracy*](http://en.wikipedia.org/wiki/Accuracy_and_precision) term, it's usually reserved for a bias, and *precision* is often used for error variance. When we compute MAPE we sort of combine them in one error measure.
Comment: @zbicyclist: 1) Those numbers are hand made numbers, our original timeseries has lot more points 2) We understand that our forecast is indeed poor in this case and would like to show something like 1% (or) 2% (or) 5% accurate comparing with actual values, instead of showing 499% error, which for common users look strange. This way our users knows they can ignore this forecast because it has very low accuracy and feed data set to us to improve our implementation.
Comment: @zbicyclist: I understand your concern and I am aware that there are seasonality, trend factors which influence forecast. Assume those are all factored in and our algorithm (as you know one algorithm doesn't fit all data sets and unfortunately we are in that space), but produced bad forecast for this data set, how can we show some number between 0 - 100 instead of showing some number like 499, that is the point we are struggling at this moment.
Comment: @Aksakal: That is nice suggestion, but we want to show accuracy rather than error. In general when we show accuracy, it falls between 0-100% right? Am I missing anything?
Comment: @Aksakal: For your example our forecast is no way reliable. Accuracy of the forecast value is ZERO (or) near ZERO percent. That is what we are trying to achieve.
Comment: @Aksakal: If you don't mind, could you please elaborate little bit more on your previous comment?
Comment: @Aksakal: Thanks! now I got what you mean by conventional understanding. In our case we are biased to show that our forecasts are good (for most of the cases), which is the reason why picked term accuracy. I think you almost got where we are struggling, in business most of the times app want to show we are x% of good rather than 400% bad, which is why we are leaning towards converting this error percentage to success percentage (in this case success % will be lower number). Any thoughts? Your comments helped lot, thanks!
Comment: @Nambari, stick to what's conventional.
Answer: One approach I've used for this problem is to define the MAPE as
(A-F)/(average of A and F)
instead of
(A-F)/A.
This measure (which I think I borrowed from Mosteller and Tukey's book, but I don't have it at hand right now) is symmetric and bounded by -200% and +200%.  I know you wanted it to be 0 through 100, but I got you partway there with a measure I may be able to find a reference for.
I have used this where (a) I wanted a symmetric measure, and (b) where I wanted to cap the errors ['whether they were horrible (200%) or atrocious (5000%) didn't matter].  The image below compares a standard MAPE with this calculation (AdjMAPE). Later edit: because the errors are signed, they should be a form of MPE, not MAPE. See also comments below by me and whuber.
Comment: One would use absolute errors as an objective function for optimization, but signed errors to assess the associated residuals.  Compare this with $\chi^2$ residuals in analyzing a two-way table, for instance: the $\chi^2$ statistic is a sum of non-negative terms $(O-E)^2/E$, but the residuals $(O-E)\sqrt{E}$ are given the signs of the differences $O-E$ so you can see which data are higher than expected and which are lower.
Comment: Many analytical laboratories call this figure the "Relative Percent Difference": see (Google books) at https://books.google.com/books?id=XMW6JsJrLK4C&pg=PA168&dq=relative+percent+difference+laboratory+quality&hl=en&sa=X&ei=uveZVOrsMMX_gwSktIDIDA&ved=0CCYQ6AEwAA#v=onepage&q=relative%20percent%20difference%20laboratory%20quality&f=false or https://books.google.com/books?id=-4n-2q627ZgC&pg=PA24&dq=relative+percent+difference+laboratory+quality&hl=en&sa=X&ei=uveZVOrsMMX_gwSktIDIDA&ved=0CDkQ6AEwBA#v=onepage&q=relative%20percent%20difference%20laboratory%20quality&f=false, for instance.
Comment: It's precisely the formula given in this answer: the difference divided by the average.  It's a measure that is appropriate when *both* quantities are subject to random error (of approximately the same magnitude). It tends to be near $0$ and in size much less than $100\%$, making it readily interpretable (that's one reason labs use it).  Remember, it's measuring an *error*, so zero is best. RPDs of size greater than $100\%$ are usually considered pretty bad.
Comment: Thank you for your time and answer, I think the approach you followed here is similar to sMAPE referred here http://en.wikipedia.org/wiki/Symmetric_mean_absolute_percentage_error (but this seems not a real absolute percentage accuracy calculation based Rob J because results are in negative accuracy) https://www.otexts.org/fpp/2/5
Comment: @whuber when you say this figure, you mean -200 to 200 (or) 0 to 100%? Will go through the links you posted, thanks for your time!
Comment: Yes, in looking for a reference I see Hyndman and Athanasopoulos define sMAPE similarly (page 46) except that in my formula the errors are signed, and in their formula they are absolute. https://www.otexts.org/fpp/2/5
Comment: Thank you both! let me read through these links tonight. If anyone has any other thoughts, please do post.

Forecast Vs Actual accuracy calculation


PHP: Replace apenas na primeira palavra repetida

Question: Olá, por exemplo, tenho a frase:

Meu nome é "Vitor", o nome "dele" "é" "Vitor", "seu" nome "é" "Vitor"

Eu utilizo um preg_replace para trocar todas as palavras que estão entre aspas pela palavra "abacaxi".
Ficaria:

Meu nome é "abacaxi", o nome "abacaxi" "abacaxi" "abacaxi", "abacaxi" nome "abacaxi" "abacaxi"

Porém, como eu posso realizar esse replace apenas nas primeiras palavras repetidas que estão entre aspas? Exemplo:

Meu nome é "abacaxi", o nome "abacaxi" "abacaxi" "Vitor", "abacaxi" nome "é" "Vitor"

Obrigado.
Comment: Poderia esclarecer melhor sua duvida, veja: na sentença: **Meu nome é Vitor, o nome dele é Vitor, seu nome é Vitor**, se fossemos ocultar somente a primeira ocorrência de cada repetição, ficaria: **Meu abacaxi abacaxi abacaxi, o nome dele é Vitor, seu nome é Vitor** e não **Meu nome é abacaxi, o nome abacaxi abacaxi Vitor, abacaxi nome é Vitor** como exemplificado por você.
Answer: Pode ser que tenha uma função especifica para isso, porém é fácil de fazer na mão.
Supondo o texto:

Meu nome é "Vitor", o nome "dele" "é" "Vitor", "seu" nome "é" "Vitor"

Podemos fazer simplesmente:
Encontrar o ", encontrando obtenha a palavra até o próximo ". Esta palavra verifique se já foi substituída anteriormente, se não substitui e adiciona esta palavra na lista de "já substituído".

Isto é mais para fins didáticos, mas claramente pode fazer isso de maneira mais organizada.
$substituto = "abacaxi";

$posicaoInicio = -1;
$posicaoFim = -1;
$dicionario = [];

while($posicaoInicio !== false && $posicaoFim !== false){

    $posicaoInicio = strpos($texto, '"', $posicaoFim + 1);
    $posicaoFim = strpos($texto, '"', $posicaoInicio + 1);

    $palavraTamanho = ($posicaoFim - $posicaoInicio) - 1;
    $palavra = substr($texto, $posicaoInicio + 1, $palavraTamanho);

    if($posicaoInicio !== false
       && $posicaoFim !== false
       && in_array($palavra, $dicionario, true) === false){

        $dicionario[] = $palavra;

        $texto = substr_replace($texto, $substituto, $posicaoInicio + 1, $palavraTamanho);

        $diferencaTamanho = strlen($substituto) - $palavraTamanho;
        $posicaoFim = ($posicaoFim + $diferencaTamanho) + 1;

    }

}

Teste isto.
A ideia é bem simplória, primeiro fazemos encontramos o inicio e o fim da palavra alvo, ou seja a palavra que está entre as aspas, para isso usamos:
 $posicaoInicio = strpos($texto, '"', $posicaoFim + 1);
 $posicaoFim = strpos($texto, '"', $posicaoInicio + 1);

O strpos encontrará a primeira ocorrência a partir do offset, portanto, supondo:

Meu nome é "Vitor"

A primeira aspa (inicio) é 12, então precisamos procura a outra a partir do 13, que será encontrado na posição 18.
Nós então sabemos que a primeira palavra está entre o 13º até o 17º byte, neste caso. Logico, precisamos excluir as aspas, para isso usamos:
$palavraTamanho = ($posicaoFim - $posicaoInicio) - 1;

Isso será utilizado para que 18 - 12 (= 6), então subtraímos 1, tornando 5. Esse é o tamanho da palavra.
Então obtemos a palavra:
$palavra = substr($texto, $posicaoInicio + 1, $palavraTamanho);

O inicio é 12, porém 12 inclui as aspas, portanto temos que adicionar 1, assim pegando os 5 bytes, correspondente a Vitor, sem aspas.
Agora precisamos saber se a palavra existe e se ela já não foi substituída, então:
$posicaoInicio !== false
&& $posicaoFim !== false
&& in_array($palavra, $dicionario, true) === false

Isso irá fazer com que ele tenha que ter achado as duas aspas e a palavra não pode está inserida em $dicionario, que é uma array que iremos preencher com as palavras já substituídas. Ou seja, todas as palavras que já conhecemos (que está no dicionário) já foi trocada. ;)
Se estiver tudo certo e não tiver sido substituído fazermos isto. Adicionamos a palavra no nosso dicionário:
$dicionario[] = $palavra;

Agora trocamos a palavra no texto usando:
$texto = substr_replace($texto, $substituto, $posicaoInicio + 1, $palavraTamanho);

Isso irá fazer com que coloque insira o abacaxi a partir do 13º byte com um tamanho de 5 dígitos. Ou seja, trocará o Vitor por abacaxi, ele irá remover o Vitor e adicionará o abacaxi em seu lugar.
Se você perceber, existe um problema, o abacaxi é maior, em quantidade de caracteres, do que o Vitor, por isso precisamos calcular o $posicaoFim, afinal nós precisamos saber qual foi a última aspa identificada.
Por isso usamos:
$diferencaTamanho = strlen($substituto) - $palavraTamanho;
$posicaoFim = ($posicaoFim + $diferencaTamanho) + 1;

Isso irá calcular a diferença do tamanho de ambos os textos (Vitor e abacaxi) e irá ajustar a última ". Matematicamente a diferença entre os dois é de 7 - 5 (=2). Então precisamos contar com as aspas, acrescentando 1, no total ficará 18 + 2 + 1 (= 21). A última aspa está na 21º posição.
Agora, fazemos isso de novo, porém com o dele, assim por diante.


Book on Complex Dynamic Systems Economics

Question: I'm searching for a good text book on economic Dynamics. I already read the book from Richard Day but I'm still struggling a little bit and I need some further material. I saw this post Recommendation for a book on dynamic systems but I'm searching for something in the field of economics and I don't need any Proofs or sophisticated theorems.
Any suggestions?
Answer: I would strongly recommend G. Gandolfo, \emph{Economic Dynamics}, 4th edition, 2009. See http://www.springer.com/gp/book/9783642038624


How to call the formatMessage() to load the messages from the .json files

Question: I'm trying to call the formatMessage() API directly outside the comp w/o using the standard <FormattedMessage../> component.
const locale = 'en';
const messages= defineMessages({
    greeting: {
        id: 'app.greeting',
        message:"some message",
        defaultMessage: 'Hello,all',
        description: 'Greeting to welcome the user to the app',
    }
});
const { intl } = new IntlProvider({locale, messages}).getChildContext();
export function someFunc(key, values) {
   return intl.formatMessage({id:'greeting'});
}

well the above is static messages defined locally, however I have a translation files, en.json and ja.json globally stored that I would like to load the messages from.
How can I do that using the defineMessages, so that when I call the formatMessage() I should be able to see the messages from the resp files depending on the locale selected...
ideas appreciated!!
Thanks!
Answer: I don't know the format of your .json files, but you can reformat them (if needed) so each key in them has at least id and defaultMessage (details) and pass to defineMessages() function.
import enMessages from 'path/to/en.json'
import jaMessages from 'path/to/ja.json'
const localeMessages = locale === 'en' ? enMessages : jaMessages

const messages= defineMessages(localeMessages);

...
const { intl } = new IntlProvider({locale, messages}).getChildContext();
intl.formatMessage({id:'greeting'});

Answer: If using the react-router npm package, you can set the IntlProvider component (from the react-intl npm package) as the main route within your route definitions. This means that all the translations are passed down as props to your components, i.e. you could access a particular message with this.props.messages.myMessage:
import { IntlProvider, addLocaleData } from 'react-intl';
import en from 'react-intl/locale-data/en';

addLocaleData(en);
let locale = 'en-GB';
const messages = require("./i18n/" + locale + ".i18n.json");
const renderRoutes = ({ locale, messages }) => (
    <IntlProvider locale={locale} messages={messages}>
    ...
    </IntlProvider>
);
ReactDom.render((renderRoutes({ locale, messages })), this.container);


Building a form wizard and need to validate parts of the greater form… possible?

Question: I have a <FormWizard /> component that looks roughly like this:
const FormWizard = ({
    children,
    defaultValues,
    onSubmit,
}) => {
    const formMethods = useForm({ defaultValues });
    const {
        formState: {
            errors,
            isSubmitting,
            isValid,
        },
        handleSubmit,
    } = formMethods;

    return (
        <FormProvider { ...formMethods }>
            <form
                noValidate
                defaultValues={ defaultValues }
                onSubmit={ handleSubmit(onSubmit) }
            >
                {
                    Children.map(children, child => {
                        if (isValidElement(child)) {
                            return (
                                <FormWizardPanel>
                                    {
                                        cloneElement(child, {
                                            errors,
                                            inWizard: true,
                                            isSubmitting,
                                            isValid,
                                        })
                                    }
                                </FormWizardPanel>
                            );
                        }
                        return child;
                    })
                }

            </form>
        </FormProvider>
    );
};

The children passed in are just form components that look roughly like this:
export const FormSignup = ({
    errors,
    inWizard,
    isSubmitting,
    isValid,
}) => {
    return (
        <fieldset>
            <Input
                required
                disabled={ isSubmitting }
                error={ errors?.email?.message }
                label="Email Address"
                name="email"
                type="email"
            />
            <Input
                required
                disabled={ isSubmitting }
                error={ errors?.password?.message }
                label="Password"
                name="password"
                type="password"
            />
            {
                !inWizard ? (


What am I missing in this argument for $\lim\limits_{x\rightarrow \infty} \ln x = \infty$?

Question: In an appendix of Stewart's Calculus, the logarithmic and exponential functions are built up starting from the defnition $\ln x = \int_1^x \frac{1}{t}\,dt$.
Having shown that $\ln(x^n) = n \ln(x)$ for integer $n$, the following argument is then made for $\lim\limits_{x\rightarrow \infty} \ln x = \infty$:

With $x=2$ and n any positive integer, we have $\ln(2^n) = n\ln 2$.
  Now $\ln 2 > 0$, so this shows that $\ln(2^n) \rightarrow \infty$ as
  $n\rightarrow \infty$.
But $\ln x$ is an increasing function since its derivative
  $\frac{1}{x} > 0$. Therefore $\ln x \rightarrow \infty$ as $x \rightarrow \infty$.

The first sentence makes perfect sense.
The second sentence, taken on its own, is a false argument (e.g. $1-e^{-x}$ has a positive derivative, yet it goes to $1$ not $\infty$).
I can show by other means that $\lim\limits_{x\rightarrow \infty} \ln x = \infty$. My question is what am I missing in this specific argument, which at the moment seems to me like a non sequitur.
Comment: Since it is an increasing function, the limit at infinity is simply the supremum. Since your function is not bounded above, the supremum is $+ \infty$. Is this clear to you?
Comment: Since you have an increasing function, it has a limit (finite or infinite) at infinity. So $\lim_{\infty} \ln(x) = L$ Therefore $\lim_{\infty} \ln(2^n) = L$
$$ $$
Which gives us $L = \infty$
Comment: You are quite right, and provide an example, that just because a function is always increasing does not mean that it is unbounded, it is a non sequitor.
Answer: It is meant to connect $\lim_{n \to \infty}\ln(2^n)$, which is just saying something about what $\ln$ is doing on very specific, discrete points, with $\lim_{x \to \infty} \ln (x)$ which says what happens to the function at all points.
For contrast, take
$$
f(x) = x\cos(2\pi x)
$$
where we have
$$
\lim_{n \to \infty} f(n) = \lim_{n \to \infty}n = \infty
$$
but the function is far from monotonous, so we cannot conclude that $\lim_{x \to \infty} f(x) = \infty$.
Answer: You know that
$$\lim_{x\to+\infty} f(x)=+\infty\stackrel{\operatorname{def}}{\iff} \forall M>0,\, \exists \alpha_M>0\ \forall x\ge\alpha_M,\, f(x)\ge M$$
You have already shown that $$\forall M>0,\,\exists n_M\ \forall m\ge n_M,\ \ln(2^m)\ge M$$
Expecially, $\ln(2^{n_M})\ge M$.
Now, since $\ln$ is a growing function (since its derivative is strictly positive), if you pick $\alpha_M=2^{n_M}$ you know that $$x\ge \alpha_M\implies \ln(x)\ge \ln(\alpha_M)=\ln(2^{n_M})\ge M$$
Whence the thesis for $\alpha_M$ defined as above.
Answer: He uses the monotonicity to make a comparison. For $x>2$ by monotonicity
$$\ln(x^n)>\ln(2^n) \longrightarrow +\infty$$ as $n \to \infty$ (by his first argument). So, the LHS goes also to $\infty$. In your example with $1-e^{-x}$ this comparison in not possible because the RHS goes to $0$.


Monitor communication to outside the network

Question: I work at a place where they have access to very sensitive data and think a lot about security. I dont want to ask bc they may lie, is there software to monitor data such as email (pop/imap AND webmail such as hotmail or gmail), forum post etc. Basically any POST data. I am worried that if i read or wrote an embarrassing email to (girl)friend or say something that can be considered suspicious; that it would come back to bite me in the behind (either laughed at or have me put under surveillance).
Answer: Yes, there's a number of techniques that could conceivably be used by an employer to monitor computer use. Are you talking about working someplace where there's security clearances?

They could have a firewall that logs where all your packets go.  Probably not hard for them to see what non-SSL URLs you visit and what SSL IPs you visit.
They could have a transparent proxy that logs a lot more than the firewall would.  Limited data on HTTPS or other encrypted traffic.
They could have a general network tap (IDS-style system) that could be logging all sorts of things.
They could have a non-transparent proxy that your system is configured to use, which would probably give them the ability to see encrypted traffic, too.
They could have a keylogger set up that monitors everything you type.  This could be software or hardware.  There's
They could have even more sophisticated monitoring of computer use going on. Most likely in the form of something like a keylogger that also tracks other data moving around.

Does your work prohibit personal use of work computers?  If so, don't do it; not even a little.
Comment: +1 for a comprehensive list and for mentioning the employer's policy and that encrypted content is not immune from access, particularly if a third party controls the DNS service, the proxy server and the client's list of root certificates.
Answer: Without listing the myriad ways of doing this, yes there are applications and techniques for sniffing network traffic including email, instant messaging, web browsing, etc., etc. There isn't any way you're going to discover on your own whether or not your communication and computer activity has been monitored.
My advice would be to keep your communication and computer activity at work related to work. Save the private/personal stuff for your own computer on your own time.
Comment: Its not that it isnt allowed, its if i am going to embarrass myself or say something stupid to get myself fired (i am new. I dont have any complaints ATM but if i said one in an email i could get fired?)
Comment: It's better to be safe than sorry. I never mix my work and personal life. I never use my work email or phone for personal stuff. You never know who's watching or listening.
Answer: Absolutely
Your employer can monitor you in a number of given ways ranging from monitoring the PC itself via software, screenshots, keystroke logger, etc to monitoring the traffic itself as it passes through a firewall appliance such as an IPCop box, pfSense box, Smoothwall box, etc.
In short... the computer isn't yours (therefore not to be trusted) and the internet connection isn't yours (therefore not to be trusted).
The best advice I could give you to be safe would be to tell you to ASSUME that everything you do on your computer is monitored.
Always keep this in the back of your mind and you'll be just fine.

As a side note... I've personally seen screenshots between girlfriends and boyfriends... seen screenshots of personal online banking balances... seen screenshots of people surfing porn at work... seen employees bitching about other employees or their bosses... seen employees planning on quitting, etc, etc.
The sad part is... a lot of stuff is done while the person is in a bad mood and just venting.  The REALLY sad part is... they're usually over it an hour or two later... but the captures are (or can be) FOREVER.
Just things to keep in mind.


Can you calculate the Area under the ROC curve (or C-statistic) using logistic regression from case control data?

Question: I know you can estimate odds ratios based on logistic regression of case control data because the baseline risks cancel out. Does this mean that only the intercept is contaminated by the analysis of case control data in logistic regression?
And furthermore, does this mean that the predictions are all off by the same fixed constant (additive on the logistic scale; multiplicative on the probability scale) so that you can still maintain the proper ordering?
If the answer is "yes", doesn't that mean you can still calculate the AUC (C-statistic) because all it relies on is the proper ordering of the predicted values?
Thank you
Answer: You're correct. Only the intercept is skewed by case-control data, but all AUC cares about is the ordering of the prediction scores. Even if the scores are $0.1$ for negatives and $0.2$ for positives, that's still a ROC AUC of $1$ because all positives are ranked more highly than all negatives.
In case you need a citation, Agresti's Categorical Data Analysis discusses the properties of case-control logistic regression, the salient details of which you've outlined.
Comment: @casecontrol_logistic I don't know about that test but you could ask a new question about it. If you've found my answer helpful, please consider upvoting it.
Comment: thank you! Does this also mean I can do things like the Hosmer-Lemeshow lack of fit test, knowing that both the predictions and the outcomes are off by the same fixed multiplicative constant?
Comment: Actually, I found it on my own! https://www.ncbi.nlm.nih.gov/pubmed/20527013 thanks again SY


TestNG Gradle how to run multiple suites in parallel

Question: I was wondering if there is a way to run multiple test suites in parallel with TestNG + Gradle.
I have a master suite file which contains other individual suites to run, however it will only run the suites sequentially.
I currently run tests inside individual suites in parallel but I also need to set multiple suites to run at the same time as-well.
Example master suite file:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="debug suite">
  <suite-files>
    <suite-file path="debug-test.xml"></suite-file>
    <suite-file path="debug-test.xml"></suite-file>
    <suite-file path="debug-test.xml"></suite-file>
    <suite-file path="debug-test.xml"></suite-file>
    <suite-file path="debug-test.xml"></suite-file>
  </suite-files>
</suite>

Example suite file containing a test:
<suite name="Debug suite" verbose="1" parallel="tests" thread-count="2" data-provider-thread-count="1">
    <test name="example test 1">
        <classes>
            <class name="ExampleTest"/>
        </classes>
    </test>
    <test name="example test 1">
        <classes>
            <class name="ExampleTest"/>
        </classes>
    </test>
</suite>

I run my tests using this gradle task in my build.gradle:
ask runSuite(type: Test, dependsOn: ['clean', 'classes']) {
    def suite = System.properties["suite"]

    useTestNG() {
        suites "src/test/suite/" + suite
    }
    testLogging.showStandardStreams = true
}

I have seen that testNG has a parameter '-suitethreadpoolsize' that would achieve what I want but its not supported when using Gradle. Does Testng + Gradle support this kind of behaviour?
Comment: Did you checked this one? https://stackoverflow.com/a/32868928/1358344
Answer: Resolved to programmatically extract tests from all child suites in the master suite and place them into one suite, then run those in parallel.
Not ideal but fits my use case.