What is a good machine learning academic research workflow/tools?

Question: I'm interested in ML research and want to get some insights into how its done in practice.
In particular:

What kind of data are you dealing with?
How are you dealing with hyper-param optimization?
How are you making sure the work is reproducible?
Are you using a cloud provider to run jobs on the cloud? If so, how are analyzing the results?
Do you have a single number evaluation metric?
Does your work involve building on top of existing models? If so, how do you obtain them
Any other extra tips & tricks, best practices etc.

Thanks for all the helpers!
Comment: This isn't really on-topic here, and the haters are going to rain fire for it.  They will say go ask software (stack overflow) about software packages.  There is a computational science group you could also ask.
Comment: @EngStudent: "Haters"!? Anyway, the problem with this question is that it's a poll with as many "correct" answers as we have users - it's not going to fly on any Stack Exchange site. It'd be fine in chat of course, & probably somewhere on Quora or Reddit.
Comment: @Matantsuberi: Welcome to Cross Validated! Sorry to have to close your first question - please have a look at our help pages to get an idea what sort of questions the site is for.
Answer: In the interest of transparency I'll answer based on how my team currently does it, without commenting on whether I think this is the "best" way so that it more closely describes real world practices rather than normative ideals.

10-fold cross validation grid search, selecting on AUC
Final models are trained with final parameters and validated on yet another, "true," holdout test set (not used in cross validation)
No ML cloud providers, but we do use compute-optimized EC2 instances from AWS
Yes, AUC (mean of AUC from 10 cross-validation or bootstrap passes) for binary classification; otherwise we decide at the start of a project, usually MSE but it depends
Yes, proprietary imputation for known missing data, 3rd party demographics (income, employment, etc), various feature engineering
Use caret for R, sklearn for Python. Always start with naive logistic regression to get a baseline to compare to more advanced models. RandomForest can't overfit, so run one over night to get feature importance before proceeding to hyper parameter optimization and model selection: if RF can't use a feature, it probably has no mutual information with the DV. After selecting a reasonably strong model, 90% of improvements come from data cleaning and thoughtful feature engineering. Always calibrate the output of a black box model using a data set with a real world class prevalence.
Comment: RFs can overfit.
Comment: Can you maybe give some idea as to what kind of data are you dealing with?
Comment: Also, why grid search instead of random search?
Comment: @Inathan. Of course. But random forest does not continue to overfit more and more simply because you increase the number of trees in the forest. Therefore a very lazy way to build a pretty good model is to crank the number of trees way up and run over night. The resulting RF model can be used for certain things, such as estimating feature importance and getting a reasonable estimate for how good a final model. Such a bloated RF model will not be production quality but is an excellent low-effort first step.
Comment: @matantsuberi CRM data. We use machine learning mostly to predict propensity to convert, purchase, or utilize various services for a populations of current and prospective customers, estimate lifetime value, predict churn and retention, etc.
Comment: @matantsuberi we generally use gridsearch instead of random search because (after building literally thousands of similar models on different cuts of the similar data) we have a pretty good idea what range each parameter will fall in, and we know (or believe) that only half a dozen or so hyper-parameters matter at all. For novel problems, without that same experience where you have to test dozens of hyper-parameters across wide ranges, random search will get very close to optimal with much less computation time.
Comment: Are you working in industry or producing academic papers?


Why does this abort right on the UIlabel?

Question: I connected the UILabel and UIButton but is still aborts. Please help me fix this.
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        counterLabel.text = "0"
        // Do any additional setup after loading the view, typically from a nib.
    }
    var counterNum: Int = 0

    @IBOutlet weak var counterLabel: UILabel!

    @IBAction func Tap(_ sender: UIButton) {

        self.counterNum += 1
        self.counterLabel.text = String(self.counterNum)
    }
}

Comment: What's your issue exactly ? Do you have an crash log?
Comment: Why did you include `func didReceiveMemoryWarning`? Please only add _relevant_ code to your question.
Comment: When it aborts, what does Xcode show?
Comment: 2017-03-03 18:43:48.521 #57[30240:689945] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<_57.ViewController 0x7fd2cef029c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Tap.'     This is what it says
Comment: Please edit your question, remove unnecessary code and add this information. If you improve your question, you have a better chance of getting a good answer.
Comment: We need more info. What "abort" means? Which is the error message displayed? Thank you.
Comment: Ashley Mills,  Got it thanks for the advice. I'm a noob.
Comment: Do a search on "this class is not key value coding-compliant for the key". This has been asked and answered a million times before.
Comment: it says Tread1:signal SIGABRT
Comment: It looks like in this case you made an _outlet_ to Tap whereas what you actually want is an _action_ from the button. Or maybe you made the outlet to Tap when you intended an outlet to counterLabel.
Comment: Thanks! I fixed it)
Answer: Please check your outlet connect in your storyboard for counterLabel. It seems you forget to do the outlet connection.
Comment: it says Tread1:signal  SIGABRT


Exercise in measure theory/probability

Question: This is an exercise in chapter 2 of Probability with Martingales by David Williams.
Question: Let $\mathcal{A}$ be the set of all maps $\alpha : \mathbb{N} \rightarrow \mathbb{N}$ such that $\alpha(1) < \alpha(2) < \dots  $,. For $\alpha \in \mathcal{A}$, let
$$F_\alpha = \{ \omega : \frac{\# (k \le n : \omega_{\alpha_{(k)}} = H)}{n} \rightarrow 0.5  \} .$$
Prove that
$$\bigcap_{\alpha \in \mathcal{A}} F_\alpha = \emptyset.$$
It is not too clear to me what can be done. Would appreciate some hints.
Answer: The text gives you a hint for this: "For any given $\omega$ find an $\alpha \ldots$". To extend this hint a little:
Let $\omega\in\Omega$, with $\omega=(\omega_1,\omega_2,\ldots)$ and with infinitely many Heads. Then consider a map $\alpha_\omega:\mathbb{N}\to\mathbb{N}$ defined by
$$\alpha_\omega(n) = j,\qquad\text{where $\omega_j$ is the $n^{th}$ Head in sequence $\omega$.}$$
Show that $\alpha_\omega\in \mathcal{A}$ and that $\omega\notin F_{\alpha_{\omega}}$. Also show that any $\omega\in\Omega$ with finitely many Heads will not belong to any $F_{\alpha}$ set. The result follows.

Exercise in measure theory/probability


Expect Value of Red Cards Drawn from a Deck

Question: I'm having some trouble figuring out the right way of approaching the following problem.

You draw 2 cards from a standard deck of 52. This contains 13 of
  hearts, diamond, clubs and spades: clubs and spades are black, hearts
  and diamonds are red. What is the expected number of red cards chosen?

My current attempt is using the summation formula given in the lectures. I'm not sure how to write it in here properly though. Basically the summation of x*p(x) from x=0 to x=2. Where x is the number of red cards drawn.
This gives me 0*(1/2*25/51)+1*(1/2*26/51)+2*(1/2*25/51) = 0.745, however I feel that this is wrong as I would've thought the answer would be much closer to 1.
I feel like I'm supposed to be doing the probabilities differently, i.e. p(x=1) should be both red/black and black/red, and p(x=2) should be doubled. At this point I've messed around with the options so much I'm not really sure which is right or wrong.
If someone could check over this and give some insight as to what I'm missing it would be appreciated.
Thanks.
Answer: Let's define the pdf, saying that $x$ is the number of red cards:
$$p(x) = \begin{cases}
\frac{26}{52}\cdot\frac{25}{51} = \frac{25}{102}& \text{if}~x=0\\
\left(\frac{26}{52}\cdot\frac{26}{51} + \frac{26}{52}\cdot\frac{26}{51}\right) = \frac{52}{102}& \text{if}~x=1\\
\frac{26}{52}\cdot\frac{25}{51} = \frac{25}{102}& \text{if}~x=2
\end{cases}$$
Then:
$$\mathbb{E}[x] = \sum_{x=0}^2 xp(x) = \\
= 0\cdot\frac{25}{102}+1\cdot\frac{52}{102} + 2\cdot\frac{25}{102} = \\
= \frac{52+50}{102} = 1,$$
which, as you expected, is exactly $1$.
Indeed, you were wrongly evaluating $p(x=1)$, because:

the first card is red and the second black (probability $ = \frac{26}{52}\cdot\frac{26}{51}$);

OR

the first card is black and the second red (probability $ = \frac{26}{52}\cdot\frac{26}{51}$);

At the end, $p(x=1) = \frac{26}{52}\cdot\frac{26}{51}+\frac{26}{52}\cdot\frac{26}{51} = \frac{26}{51}$.
Comment: exactly, 1st and 2nd are distinct. You could have find out your error by finding that, after your calculations you got $\sum_{x=0}^2 p(x) \neq 1$
Comment: Excellent. I figured there was something wrong with the probabilities. So I'm guessing you don't need to treat the 2 red draw as separate events due to the red cards not being distinct?
Comment: Fair enough. Everyone keeps saying probability is counter intuitive so I second guessed it. Thanks for the help.
Answer: I hope that your lecturer expects you to solve problems in the most appropriate way, which is sometimes the shortest and most elegant. Immediate considerations of symmetry tell you that the expected result for any number of cards drawn is half red and half black, likewise a quarter for each suit and a thirteenth for each denomination.
Comment: To be completely honest the lecturers don't really seem to care as long as you get the right answer.
Answer: Expectation problems are generally much easier to deal with,
using linearity of expectation
P[get red in one draw] = E[# of reds in one draw] =  $\frac12$
So by linearity of expectation, E[# of reds in 2 draws] = $2\cdot\frac12 = 1$
Comment: Thanks. While I wouldn't be able to use this as an answer (due to not being taught this so far), it is definitely good to know for the exam.
Comment: You're welcome !


Python3 reading mixed text/binary data line-by-line

Question: I need to parse a file which has a UTF-16 text header and followed directly by binary data. To be able to read the binary data, I open the file in "rb" mode, then, for reading the header, wrap it into a io.TextIOWrapper().
The problem is that when I execute the .readline() method of the TextIOWrapper object, the wrapper reads ahead too far (even though I only requested a single line) and then runs into a UTF-16 decoding error when encountering the binary portion: A UnicodeDecodeError is raised.
However, I need proper parsing of the text data and cannot simply do a binary read first, then do a data.find(b"\n\0") because it's not guaranteed that this actually matches at an even offset (could be midway in-between characters). I would like to avoid doing UTF-16 parsing myself.
Is there an easy way to tell TextIOWrapper to not read ahead?
Comment: what kind of file is this ?
Answer: No, you can't use a TextIOWrapper() object because it'll read from the underlying buffer in larger blocks, not just in lines, so yes, it'll try to decode binary data past than first line. You can't prevent this.
For a single line of text using \n line delimiters, you really don't need to use TextIOWrapper(). Binary files still support line-by-line reading, where file.readline() will give you the binary data up to the next \n byte. Just open the file as binary, and read one line.
Valid UTF-16 data will always have an even length. But because UTF-16 comes in two flavours, big endian and little endian byte orders, you'll need to check how much data was read to see what byte order was used, in order to conditionally read a single byte that should be part of that first line of data. If UTF-16 little-endian was used, you are guaranteed to have read an odd number of bytes, as newlines are encoded to 0a 00 rather than 00 0a and the .readline() call will have left the single 00 byte in the file stream. In that case, just read one more byte and add it to the first line data before decoding:
with open(filename, 'rb') as binfile:
    firstline = binfile.readline()
    if len(firstline) % 2:
        # little-endian UTF-16, add one more byte
        firstline += binfile.read(1)
    text = firstline.decode('utf-16')

    # read binary data from the file

A demo with io.BytesIO() where we first write UTF-16 little-endian data (with the BOM to indicate the byte order for the decoder), with the text followed by two low-surrogate sequences that would cause a UTF-16 decoding error to stand for 'binary data', after which we read the text and data again:
>>> import io, codecs
>>> from pprint import pprint
>>> binfile = io.BytesIO()
>>> utf16le_wrapper = io.TextIOWrapper(binfile, encoding='utf-16-le', write_through=True)
>>> utf16le_wrapper.write('\ufeff')  # write the UTF-16 BOM manually, as the -le and -be variants won't include this
1
>>> utf16le_wrapper.write('The quick brown  jumps over the lazy \n')
40
>>> binfile.write(b'\xDF\xFF\xDF\xFF')  # binary data, guaranteed to not decode as UTF-16
4
>>> binfile.flush()  # flush and seek back to start to move to reading
>>> binfile.seek(0)
0
>>> firstline = binfile.readline()  # read that first line
>>> len(firstline) % 2              # confirm we read an odd number of bytes
1
>>> firstline += binfile.read(1)    # add the expected null byte
>>> pprint(firstline)               # peek at the UTF-16 data we read
(b'\xff\xfeT\x00h\x00e\x00 \x00q\x00u\x00i\x00c\x00k\x00 \x00b\x00r\x00o\x00'
 b'w\x00n\x00 \x00>\xd8\x8a\xdd \x00j\x00u\x00m\x00p\x00s\x00 \x00o\x00v\x00'
 b'e\x00r\x00 \x00t\x00h\x00e\x00 \x00l\x00a\x00z\x00y\x00 \x00=\xd8\x15\xdc'
 b'\n\x00')
>>> print(firstline.decode('utf-16'))  # bom included, so the decoder detects LE vs BE
The quick brown  jumps over the lazy

>>> binfile.read()
b'\xdf\xff\xdf\xff'

Any alternative implementations that still can use TextIOWrapper() would require an intermediate wrapper to sit between the binary file and the TextIOWrapper() instance to prevent TextIOWrapper() to read too far, and this would get complex fast and would require the wrapper have knowledge of the codec used anyway. For a single line of text, that's just not worth the effort required.


Database schema for image field

Question: I have a module which behaves much like question and answers. Each question needs to have a help image. So far, I have written the schema as this:

function mymodule_schema() {
  $schema(mymodule_questions) = array(
      'description' => 'Stores questions for my module',
      'fields' => array(
          'qid' => array(
            'type' => 'serial',
              'not null' => TRUE,
              'description' => 'Primary Key: Question ID.'
          ),
          'prompt' => array(
             'type' => 'text',
              'not null' => TRUE,
              'size' => 'normal',
              'description' => 'Question prompt',
              'length' => '1000'
          ),
          'option_type' => array(
              'type' => 'varchar',
              'not null' => TRUE,
              'description' => 'Option Type',
              'length' => '255'
          ),
          /* Image field schema goes here */
      ),
  );
}
?>

Now, I want to add an image field, i.e. a field where admins can upload images to be shown with each question. What should be the schema for this field? Should I use the Field API in the schema (is that possible?)? If yes, how?
Thanks.
Kaustav Das Modak
Answer: To be able to integrate with field API, you would need to declare your custom data thing as a fieldable entity. This is possibly, but it is quite a bit of work and then you'll have to add the field in a install hook.
What you can do as well is simply add a single 'fid' integer column to your table, add a form element of #type 'managed_file' to your form, which will automatically save the file for you and create an entry in the file_managed table. You can then simply reference the fid (file id) of that object. Also note that you need to call file_usage_add() so that the managed file knows that it is in use. When your object is deleted, use file_usage_delete() then the file will automatically be removed.
This is exactly what is done in Drupal 7 for profile pictures, have a look at user_save() for an example.
Comment: Got it. I was trying to use Field API but still wanted to retain some referencing control. Adding the 'fid' column helped. Thanks @Berdir

Database schema for image field


Why does js click function run multiple times?

Question: I'm creating some accordion functionality for fields and paragraphs using a combination of twig templates and a custom JavaScript file.
Everything prints as expected and the basic accordion functionality works; however, the click function script is running multiple times, causing an "expand-collapse-expand-collapse..." behavior. See image:

Here is the script file:
(function($, Drupal) {

/* Add span to wysiwyg button classes for alignment
------------------------------------ */
Drupal.behaviors.accordionFaqFunction = {
  attach: function (context, settings) {
        $('.toggle').click(function(e) {

                e.preventDefault();
                var $this = $(this);

                if ($this.next().hasClass('show')) {
                        $this.next().removeClass('show');
                        $this.next().slideUp(350);
                        console.log("hiding");
                } else {
                        $this.parent().parent().find('li .inner').removeClass('show');
                        $this.parent().parent().find('li .inner').slideUp(350);
                        console.log("showing");
                        $this.next().toggleClass('show');
                        $this.next().slideToggle(350);
                }
        });

  }
};

})(jQuery, Drupal);

Why is the script running multiple times?
Comment: High level guess - because `Drupal.behaviors.attach` is executing multiple times, and you're not making use of `once` to ensure the event handlers are only bound once
Comment: @Clive that may well be the case - would you please explain in an answer how to best do this for Drupal.Behaviors?
Answer: Most importantly you have to use the JQuery selector context. Otherwise you run the entire DOM through your js code anytime when Drupal behaviors passes you only context to process.
If your function still runs multiple times, add once() with a unique identifier:
$('input.myCustom', context).once('mySecondBehavior').each(function () {

Don't forget to add the library core/jquery.once to mytheme.libraries.yml, see https://www.drupal.org/docs/8/api/javascript-api/javascript-api-overview
Answer: I just came across this post while working something out in our project. We have multiple events bound to $('body', context) since flexibility in the platform allows for widgets and such to be placed almost anywhere within the body.
Using $('body', context).on('click', '.modal-trigger', function (e) {}); was causing the click event to be bound on body 2x.
Simply using $('body', context).once().on('click', '.modal-trigger', function (e) {}); was binding only the last attached/loaded script to the body, resulting in no other script events being attached.
Adding .once() with a unique identifier for each script solved this issue beautifully.
Ex.
1st modal script
  Drupal.behaviors.SigmaModal = {

    attach: function (context, settings) {

      // Trigger sigma dialog on click.
      $('body', context).once('SigmaModal').on('click', '.sigma-modal-trigger', function (e) {

2nd modal script
  Drupal.behaviors.DisplayModal = {

    attach: function (context, settings) {

      // Trigger generic dialogs on click.
      $('body', context).once('DisplayModal').on('click', '.modal-trigger', function (e) {

Answer: You can try this one on Drupal 8:
Drupal.behaviors.custom = {
    attach: function(context, settings) {
        if(!Drupal.behaviors.custom.click_set){
            //Your code.
            Drupal.behaviors.custom.click_set = true;
        }
    }
};

Answer: You should add context to the dom object, which will limit execution to a particular context.
Refer to the example below which will fetch data from a remote url.
Drupal.behaviors.fetchTermsAndConditions = {
    attach: function (context, settings) {
        $('#clickableElementID', context).on('click', function(){
                var remoteURL = 'https://remotejson.url';
                $.ajax({
                    type: 'GET',
                    url: remoteURL,
                    success: function (data) {
                      {
                        $('#targetElementID').append(data.value);
                      }
                    }
                  });
                });

            }
      }
    };

In addition if the function to be executed on global context uses the html or body context it is Drupals equivalent to $(document).ready( myInit() );
Read more on the Drupal documentation here
https://www.drupal.org/docs/drupal-apis/javascript-api/javascript-api-overview


How to get one document instead having multiple with the same name nodejs mongodb

Question: I am working on nodejs mongodb RESTful APIs.I have 10 object documents(works that electrician can do) with the same category name(electrician).I need to get that category name in just one object one time but when i query then all 10 documents shows up with just category name.how can i get just one object instead having 10 with same category name.
There are 10 objects like given below with same category name.
{ "category": [ { "_id": "5b80393f7fca491488980fe1", "category_name": "electrical", "sub_category_name": "switches meters and fuses", "price": 300 }] }

But when i query i get
{"category": [ { "_id": "5b80393f7fca491488980fe1", "category_name": "electrical" },{ "_id": "5b80393f7fca491488980fe1", "category_name": "electrical" }] }

Comment: {
    "category": [
        {
            "_id": "5b80393f7fca491488980fe1",
            "category_name": "electrical",
            "sub_category_name": "switches meters and fuses",
            "price": 300
        }]
}
Comment: But when i query i get
Comment: {"category": [
        {
            "_id": "5b80393f7fca491488980fe1",
            "category_name": "electrical"
        },{
            "_id": "5b80393f7fca491488980fe1",
            "category_name": "electrical"
        }]
}
Comment: Can you paste the query you're using?
Comment: router.get('/name', async(req,res)=>{
    const c = await Category.find()
        // .select('category_name');
        res.send({
            category: c
        });
});
Comment: i am using just find query
Answer: If I understand your expected output correctly, you can use aggregate in place of your find to do what you want.
$match takes the query [1]:
{ $match: { "category.category_name": 'electrical' }},

$unwind will flatten the category array [2]:
{ $unwind: '$category' }

$group allows us to group by the category_name and make an array out of the individual documents in each group [3]:
{
    $group: {
        _id: { category_name: '$category.category_name' },
       category: {
            $push: {
                _id: '$category._id',
                sub_category_name: '$category.sub_category_name',
                price: '$category.price'
            }
       }
    }
}

$project to get the category_name out of _id [4]:
{
    $project: {
        _id: 0,
        category_name: '$_id.category_name',
        category: '$category'
    }
}


Where are all the controller classes instantiated?

Question: In Drupal 8, if I want to create a form, I have to define a class inside the module folder like this.
class MyForm extends FormBase{

   public function getFormId()
   {
     return 'my_form';
   }
   public function buildForm(array $form, FormStateInterface $form_state)
   {

   }
   /* AND ALL OTHER OVERRIDDEN METHODS  */
}

Then I need to register a path for it in module.routing.yml.
module_name.myform:
  path: '/admin/structure/myform'
  defaults:
    _form: '\Drupal\my_formr\Form\MyForm'
    _title: 'My form'
  requirements:
    _permission: 'administer banner configuration'

Where is the MyForm class instantiated? What calls all the methods we are overriding?
We just define the class and register a path for it; neither we create an object of that class, nor we invoke any of the methods.
So, how does Drupal 8 manage these classes?
Answer: So, first, this is not a controller, it's a form class. Controllers are instantiated by the routing component.
Every route must have a controller. _form and some other special cases are processed after routes were collected and the actual controller is added. For forms, that is \Drupal\Core\Controller\HtmlFormController.
The actual processing happens in \Drupal\Core\Controller\FormController::getContentResult(), which calls \Drupal\Core\Controller\HtmlFormController::getFormObject(), which in turn uses \Drupal\Core\DependencyInjection\ClassResolverInterface::getInstanceFromDefinition() to instantiate the form class.
FormController then hands of the processing of the form to \Drupal\Core\Form\FormBuilder, which uses various other classes in the Form namespace.
Comment: @Jasodeep Chatterjee, controllers are a concept of the symfony framework: http://symfony.com/doc/current/book/controller.html. A map for drupal: https://www.drupal.org/files/d8_render_pipeline.pdf
Comment: ok so, this how _form & other special classes are instantiated.              now, which routing components/core classes are responsible for instantiating the controller classes (src/controllers).


Drupal doesn't output list of children on custom module page

Question: I am on Drupal 7. I want to create a page that lists its children, the same way you see the list of links in 'admin/structure' (Blocks, Content types, Entity types ...).
I have modified child 1 and child 2. They aren't really important for this example.
on mymodule.module I have inside function mymodule_menu():
  $items['admin/mymodule'] = array(
    'title' => 'My module parent page',
    'page callback' => 'mymodule_admin_page',
    'access callback' => 'mymodule_admin_list',
    'access arguments' => array(1),
    'file' => 'mymodule.admin.inc',
  );

  // Child 1
  $items['admin/mymodule/child1'] = array(
    'title' => 'Child 1',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('mymodule_something_01', 1),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'mymodule.admin.inc',
  );

  // Child 2
  $items['admin/mymodule/child2'] = array(
    'title' => 'Child 2',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('mymodule_something_02', 1),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'mymodule.admin.inc',
  );

Then, on mymodule.admin.inc, I have this function:
function mymodule_admin_page() {
  $item = menu_get_item();
  if ($content = system_admin_menu_block($item)) {
    $output = theme('mymodule_admin_list', array('content' => $content));
    // This gets printed on the page
    drupal_set_message('This gets printed on the page');
  }
  else {
    $output = t('You do not have any administrative items.');
    // This doesn't get printed on the page because my base page has children.
    drupal_set_message('This doesnt get printed on the page because my base page has children.');
  }
  return $output;
}

Finally, according to documentation I have read, I should add a function like the one below:
function theme_mymodule_admin_list($variables) {

  $content = $variables['content'];
  $output = '';
  // This doesn't get printed on the page...
  drupal_set_message('this doesnt get printed... Why???');

  if ($content) {
    $output = '
';
    foreach ($content as $item) {
      $output .= '
' . l($item['title'], $item['href'], $item['localized_options']) . '
';
      $output .= '
' . filter_xss_admin($item['description']) . '
';
    }
    $output .= '
';
  }

  return $output;
}

Check my drupal_set_message above. Messages on function mymodule_admin_page get printed, but messages on theme_mymodule_admin_list don't get printed... Actually, admin/mymodule responds as a page, but instead of displaying links to its children, it will display a blank page...
I must be missing something, but I really don't know what. Thank you!
========
SOLUTION
I am posting the solution below. Thank you, Geoff, for your comment. It did the trick! I was missing one of the pieces in this puzzle, which was the mymodule_theme function.
function mymodule_theme($existing, $type, $theme, $path) {
  return array(
    'mymodule_admin_list' => array(
      'variables' => array('content' => NULL),
      'file' => 'mymodule.admin.inc',
    ),
  );
}

Comment: might seem like stupid questions, but is theme_mymodule_admin_list() in your active themes template.php and have you flushed the cache?
Comment: Hey! Not stupid at all, your hint made all the difference! I was missing one of the pieces in the puzzle. Please, add your comment as an answer and I will check it as approved. Thank you! I am adding a more complete solution into my own question.
Comment: what I wrote wasn't really a solution even if it got you thinking in the right direction, so you can add your own if you like.
Comment: Well, ok, but pointing the right direction was really an eye-opener. Thank you. If you would like to post it as an answer, I will accept it as solved. Best regards.
Answer: Another Solution here would be to do what the admin pages do for those pages
$items['admin/somemodule'] = array(
 'title' => 'My Module Section',
 'description' => 'Manage My Module',
 'page callback' => 'system_admin_menu_block_page',
 'access arguments' => array('access administration pages'),
 'file path' => drupal_get_path('module', 'system'),
 'file' => 'system.admin.inc',
);


Can this be achieve by attributes?

Question: I am thinking of setting a product that is only valid from a period of time eg:
product a from 15january2016 til 15january 2018, upon this setup it will be available from that period of time.
also when creating the product a, I can set the time of the product available more of like a timeslot like :
9:00am - 10:00am - 3booth
10:00am -11:00am - 3booth
etc
user will be able to choose more than one.
example :
A buy 9am-10am - 3booth
A also buy 10am-11am - 1booth
so A can buy 3stock from a timeslot and also buy another timeslot with only 1 stock. and how do we proceed with this option? can product attributes help?
this product is going to be available daily.
more of like selling practice session.
my question are :
can this be achieve by attributes? for timeslot? and since there is only 3 classes, and this each class can only be taken by one person 1hour, is there any solution for validation of stock?
I am brainstorming of doing custom attributes but can not get away of thinking how to handle the stock of the class available or not?
some ideas would be great
thank you!
Answer: The following four fields would be easily possible for making "timeslot" products:

Start - date field, single date (not a range)You want a single date field because views will be more straightforward when not dealing with ranges on one field. It's much easier to do two fields and have two filters than it is to have one field with a range filter (date supports both use cases).
End - date field, single date (not a range)
Timeslot - Product title would be easiest OR taxonomy reference The taxonomy route would allow you to make the timeslots fieldable and help you connect products of the same timeslot (if it is helpful to have all 9am - 10am products in a single list).
Stock - You could use commerce_stock to create a single stock availability OR you could just disable the product on checkout.

So, the simplest route would only include a "start" and "end" date field with the product title being the "timeslot" and using the "status" of products as a simple "single availability" flag.
Hope that helps!
Comment: Hi joshmiller, thank you for giving out some clues, but current situation, the products(course) is available everyday and every timeslots for example like 9:00am - 10am it will have 3booth, and one user can book it all and that means each timeslot will have each own different stocks. and is allowed to be purchased like : A buy 9am-10am 3slot/booths, then A also buy 10am-11am but only 1slot/booth, so the stock will automatically deducted from the available date. and I dont think timeslot can be created via taxonomy, can we use maybe product attributes for it?
Comment: Not sure I follow ... feel free to post a visual or some more detailed information.

Can this be achieve by attributes?


ImageViews in circle

Question: I want to put 20 ImageView objects in LinearLayout rotated so that they form a circle. Is this possible and how, and is this the right approach for creating circular layout of ImageView objects?
Thanks
Answer: LinearLayout can only present child elements either vertical or horizontal. You can position the ImageViews to form a circle if you use a RelativeLayout, but rotating them will include lots of calculations
Answer: consider using the canvas and draw paint functions
Answer: I'm not conversant with the android API, but it doesn't look like you can bend a LinearLayout into a circle.  Rotating it should only change it's angle (horizontal versus vertical versus diagonal).
Probably, you're going to want to do something based off of an AbsoluteLayout or RelativeLayout, then perform geometry-related math to find out the positions the various elements need to be at.
Ideally, for future re-use, you should actually create a new CircularLayout that could be applied to containers.  You want the API to require a List of some sort of the elements (plus optionally padding), then have the layout perform the necessary math.
Answer: Try this:
Your XML
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/myImageontoolbar"
android:padding="5dp"
android:layout_width="10mm"
android:layout_height="10mm"
android:layout_gravity="left"
app:srcCompat="@android:drawable/sym_def_app_icon" />

Your Gradle
compile 'de.hdodenhof:circleimageview:2.1.0'

ImageViews in circle


Cannot get session ID for "remember me" users inside spring security login event listener

Question: I'm writing a web app using grails and spring-security 3.0.3 which requires me to keep a databse record of all successful logins, including the username, time, ip, and sessionId.
I have created a login listener as so(groovy code):
class DpLoginListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {
    void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {
        def source = event.getSource()
        def principal = source.principal
        def details = source.details
        TrafficLogin.withTransaction {
            new TrafficLogin(userId: principal.id, ipAddress: details.remoteAddress, sessionId: details.sessionId ?: 'remember me').save(failOnError: true)
        }
    }
}

This fires as expected when a user logs in, but when someone comes back to a site as a "remember me" user, the sessionId is null.  Is this the expected behavior?  Shouldn't a session be created by the time login has succeeded?
I tried to workaround this by adding a separate SessionCreationEvent listener, which would find the latest databse login record for the user and update that row with the correct sessionId as soon as the session exists, but it seems that this session creation event never fires, and I can't figure out why.
The session creation listener looks like this:
class DpSessionCreatedListener implements ApplicationListener<SessionCreationEvent> {
  void onApplicationEvent(SessionCreationEvent event) {
    def source = event.getSource()
    def principal = source.principal
    def details = source.details
    TrafficLogin.withTransaction {
      def rememberMe = TrafficLogin.find("from TrafficLogin as t where t.userId=? and t.sessionId='remember me' order by t.dateCreated desc", principal.id)
      if (rememberMe) {
        rememberMe.sessionId = details.sessionId
        rememberMe.save(failOnError:true)
      }
    }
  }
}

And a bean is defined for it in my resources.groovy file, in the same manner as the login listener, which fires fine.
Any ideas how to correctly set this sessionId?
Answer: Not related to grails, but this SO question may offer a workaround if grails supports it.


Make div occupy remaining width of parent

Question: On the webpage I am making, I want to add a search bar. I have a div container for the entire searchbar and I have a text input and a button inside of it. What i am trying to achieve is, that the text input fills out the entire parent div except for the search button.
Currently the HTML is this:

    <input type="text" name="search" placeholder="Search..." style="padding: 14px; font-size: 16px;">
    <button type="submit" style="float: right; padding: 16px;">

    </button>

I tried entering "100%" in the width but that didn't work. I could use JS but im sure there is an easy, plain-html approach to this.
Answer: First of all, try avoiding float, it's quite messy.
In order to achieve the 'two items on one line' trick, you need to know to set the width of your button, and tell the input to take the remaining space with the calc() CSS attribute.
input {
display: inline-block;
width: calc(100% - 100px);
}

button {
width: 100px;
margin: 0;
display: inline-block
}

div {
width: 100%;
}

Check out this flexbox guide as well, it's very informative on how to place objects!
Good luck!


How to define a higher-degree spline using python?

Question: I am using the Scipy CubicSpline interpolation based on a certain number of points as shown in the diagram below:
My problem is, the second derivative of the Cubic Splive function looks a little bit edgy:

In order to smooth the second curve I need a higher degree of spline interpolation. Is there a Scipy build in function (similar to CubicSpline) or an easy way to do that? (A b-spline function want work)
Comment: have a look at [UnivariateSpline](https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.interpolate.UnivariateSpline.html#scipy.interpolate.UnivariateSpline), the degree could be set up to 5
Comment: Expecting derivatives of a spline to look smooth ignores the fact that splines are defined in a piecewise manner.
Comment: What do you mean?  Is there a better way to achieve a smooth derivative?
Answer: make_interp_spline should be able to construct BSpline objects of higher degrees (FITPACK only goes up to k=5, which is hardcoded fairly deep down).


Pegar ID do registro criado Laravel 5

Question: Tenho o código abaixo para criação de um relatório:
<code>$this->repository->create($request->all());
return response()->json(['sucesso' => 'Relatorio cadastrado com sucesso.']);
</code>
Como faço para pegar o ID do registro criado e mandar junto na resposta?
Answer: Guarde o objeto numa variável, neste caso o <code>$new</code>, tente o seguinte:
<code>$new = $this->repository->create($request->all());
return response()->json(['sucesso' => 'Relatorio cadastrado com sucesso. ID: ' .$new->id]);
</code>
Comment: Perfeito @Miguel obrigado! (Aguardarei o tempo mínimo para marcar como correta)
Comment: De nada @RaylanSoares, ainda bem que resolveu

Pegar ID do registro criado Laravel 5


Entire functions bounded along $\vert z\vert = R$

Question: Suppose that $f$ is an entire function such that $\forall \vert z \vert =R$, $f(z)\leq M$, then for each coefficient $C_k$ in its power series expansion $f(z)=\sum C_kz^k$, $$|C_k|\leq \frac{M}{R^k}$$
My approach was plugging in $f(Re^{i\theta})$ which is $\sum C_kR^ke^{ik\theta}$. So $$\sum C_kR^ke^{ik\theta} \leq M$$
and then suppose that $C_l = 0$ for $l\neq k$. Which leads to $$C_kR^ke^{ik\theta} \leq M$$ $$C_k \leq \frac{M}{R^Ke^{ik\theta}}$$
I am not sure what to do at this point or if my steps leading up to this are correct. Any guidance would be appreciated. Thanks
Comment: Why can you assume that $C_l=0$ for $l\neq k$?
Comment: I suggest using the Cauchy integral formula and then bounding the integral.
Answer: For the function $f(z)=\sum_{n=0}^{\infty}c_nz^n,$ we have$$c_n=\dfrac{f^{(n)}(0)}{n!}.$$
 Now use Cauchy's integral formula $$f^{(n)}(0)=\dfrac{n!}{2\pi i}\int_{|z|=R}\dfrac{f(z)}{z^{n+1}}dz.$$ Also keep in mind that $$\left|\int_{|z|=R}\dfrac{f(z)}{z^{n+1}}dz\right|\le\int_{|z|=R}\left|\dfrac{f(z)}{z^{n+1}}\right||dz|.$$ A simple version of the last inequality is given by the ML inequality.


Erro de conversão de numérico para numérico

Question: Tenho uma stored procedure no banco de dados que altera um registro de um cliente, inserindo a data na qual ele será visitado e o entrevistador que realizará a visita.
CREATE PROCEDURE dbo.Captacao_InsereDadosAgendamento
@DATA_ENTREVISTA DATETIME,
@ENTREVISTADOR NVARCHAR(30),
@CGC NVARCHAR(14)
AS
BEGIN
    UPDATE
        CLIENTES
    SET
        DATA_AGENDAMENTO = @DATA_ENTREVISTA,
        ENTREVISTADOR = UPPER(@ENTREVISTADOR)
    WHERE
        CGC = @CGC
END

Mas quando eu executo a procedure, mesmo que passe os três parâmetros corretamente, recebo o seguinte erro:

Arithmetic overflow error converting numeric to data type numeric.
  The statement has been terminated.

Nenhum dos parâmetros da procedure ou da tabela é do tipo numeric.
O que eu faço para corrigir esse erro?
Comment: Qual é o tipo das colunas data agendamento, entrevistador e cgc?
Comment: data agendamento é datetime, entrevistador é nvarchar(30) e cgc é nvarchar(14)
Comment: Quais são os dados que estão sendo passado nas variaveis? Acredito que o problema possa estar no datetime. Tente excluir ele da sua procedure para fazer um teste
Comment: tentei passar só o nome do entrevistador e dá o mesmo erro
Comment: Pode mostrar como está chamando a proc e quais os valores?
Comment: Eu executo a procedure no SQL Manager Lite e passo os parâmetros direto no pop-up que ele abre: https://drive.google.com/file/d/1M5foCBETcb2vxzy5EmbaFybsfdJx4AwO/view?usp=sharing
Answer: Estou colocando a resposta para poder encerrar a pergunta.
Conversando com um dos analistas, encontramos uma trigger que era acionada no insert ou update nessa tabela de clientes, para realizar os tratamentos dos dados inseridos na tabela. Como nenhum desses dados é usado nessa procedure, decidimos usar os comandos DISABLE TRIGGER e ENABLE TRIGGER. E funcionou. O novo código ficou assim:
CREATE PROCEDURE [dbo].[Captacao_InsereDadosAgendamento]
@DATA_ENTREVISTA DATETIME,
@ENTREVISTADOR NVARCHAR(30),
@CGC NVARCHAR(14)
AS
BEGIN
    DISABLE TRIGGER CLIENTES.CLIENTES_triu ON CLIENTES;
    UPDATE
        CLIENTES
    SET
        DATA_AGENDAMENTO = @DATA_ENTREVISTA,
        ENTREVISTADOR = UPPER(@ENTREVISTADOR)
    WHERE
        CGC = @CGC;
    ENABLE TRIGGER CLIENTES.CLIENTES_triu ON CLIENTES;
END

Obrigada aos que tentaram ajudar!


Diretório recursivo PHP

Question: Preciso incluir um arquivo de um diretório recursivo, estou utilizando a seguinte forma:
<?php
  require_once '../../cabecalho.php';
?>

Ok, o que está acontecendo é o seguinte. O cabeçalho, que seria o header da página + menu está carregando, porém, somente o HTML, tudo que está referenciado dentro do header, como por exemplo:
<link href="css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection"/>

não está carregando, ajuda?
Comment: olhando assim, parece que o caminho do seu css que não esta sendo localizado.
Comment: O que é exatamente um "diretório recursivo"? Pesquisei agora em dois _search engines_, e só apareceu a sua pergunta com o termo exato.
Answer: Para funcionar dessa forma, o caminho do css, javascript ou imagem devem ser relativos a página que está incluindo o cabeçalho e não do cabeçalho em si.
Ou como alternativa, você pode usar o caminho absoluto para incluir os arquivos, por exemplo:
<link href="http://seuservidor.com/css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection"/>

Comment: Com base na raíz é até mais prático: `/css/materialize.css`
Comment: Exatamente. Coloquei daquela forma porque existe alguns servidores locais tipo localhost/webdav/projeto, etc.. que pode dar erro. Mas para usar online é a melhor opção

Diretório recursivo PHP


Email server - Disk quota sizes - suggestions?

Question: Working out a new server for an agency of 200 Employees - with approx 240 email accounts.
Internally I'm arguing with myself over the amount of drive space to allocate to each user for the disk quota, I'm just looking for suggestions.
Once i have a quota size decided, it will define the solution for storage.
I've had everything from 4 GB per account ( which i feel is being generous ) down to 500 Mb ( with is rather restrictive in today's day and age. )
Thing is 4 GB per acocunt is just under  1 TB of allocated storage for email alone.
Does anyone follow a "rule of thumb" or have thoughts on this?
thanks in advance
Comment: I find that a lot of companies only consider the "hard cost" of backup and storage of email, and fail to account for the "soft cost" when employees use ad hoc email rentention systems like PST files and hardcopy to "get around" the imposed email quota policies. I have personal experience with a Fortune 500 company who set absurdly low email retention limits on their Exchange Server computers, but allows users to store email in PST files in their home directories on servers, creating a backup nightmare (goodby differential backups) and a legal compliance nightmare (eDiscovery request?).
Comment: I agree - We look at the cost of storage as just another cost of doing business. The quota's i want to set in place are not to lower my storage expense. just help define the initial storage provisioning. All of our users are configured with IMAP to the client or through web mail. This is specifically for portability as well as backup of email.
Much easy to back up the source than rely on the users to back up their own email.
Answer: This is a "length of a piece of string" situation - it depends a lot on the amount of mail and the content of it (all small simple text messages, or do your users regularly send and receive large attachments?).
Your best bet is to have look at recent mail patterns for a sample of users, set your base quota by that multiplied by a small factor.
Two rules of thumb for mail quotas:

One size does not fit all, so don't give every user in all departments 4Gb just because the users in the art/marketing department need that much for all the large images they transfer by mail
Increasing a user's quota if/when they need it is a lot easier than decreasing it if they don't (they'll moan about losing something, even if they don't need it!) so set the quotas to a reasonable but not over-the-top value and make sure the server(s) send alerts (to the user, your mail admins, or both) when an account hits, say, 90% of quota.

4Gb is certainly too much as a general quota - I have over 18 months worth of mail in less than 256Mb on our current system (we kept the old mail server for archive purposes instead of migrating old data over) and I'm sure many people need less than I unless you are archiving mail for a long time. 1Gb is more realistic, but may still be too much as your base quota for everyone.
As well as considering the volume of allocated space for the live mail stores, remember that you need to allocate enough resource to backups of this data.
And if your users are sending large objects by mail internally (I've seen people send 10s of Mb to everyone in a company address book even though only a few people actually needed the documents), which could result in big mailboxes over time, you should at least consider encouraging them to use a more special purpose document sharing system - this might be easier to manage size wise, could allow useful things like revision/signoff control and other workflow helpers, and might be easier to backup in a way that makes restoring individual documents less work.
Comment: Good point - a fluid quota or a flexible quota i guess is the option we will go for.
We do have different requirements depending on the department.
Leaving overhead to increase on the fly - thanks.
Comment: I think it depends (comment on distributing large attachments internally) on how the space was used. I was told that Exchange will point to a common attachment on the storage side to give it to other users...if you have a user send a 40 meg attachment to 10 users internally, there is one copy of the attachment that they see (as long as it's not resent or altered) so it takes up 40 meg, not 400, on the server itself. Was that true?
Comment: That will depend on the mail server software and sometimes on how the software is configured. Like you suggest some mail servers will be bright enough to store only one copy of attachments sent that way, but certainly not all.
Answer: I've found that it entirely depends on your agency. If you have an environment where you can dictate policy and training, having a 2 or 3 gig mailbox is more than generous; you would need to keep at the users to get them to back up old mail and archive it offline to a storage area though for regular backups.
Make it larger and in some cases you'll have users that start using your mail server as a way to store all their documents, like an uber-convenient disk storage cloud.
If you don't have the ability to say "this is how it is", you're going to have administrator and people who outrank you dictate they want XYZ to work the way they want it to work regardless and probably will abuse it.
Do you have a mail system in place right now? What are the users currently using? What are the current statistics? That should form the basis of your estimates.
Usually if you set a big limit...5 gig, perhaps...you're going to have a small percent who abuse it and will climb as high as possible, while most will use only a fraction of that space.
I'd look at your current usage patterns and see if you can get a rough estimate from that. Otherwise, get a terabyte of storage (if email is an important function, the cost of a good backup system and storage of a terabyte isn't much, even adding in RAID).
Last look at your data retention requirements. You don't say what agency this is for, but depending on the field there may be laws dictating what has to be retained and for how long. You'll need to factor that into your backup and storage strategy as well.
Comment: Thank you all for your input. I think with the cost of storage being as low as it is setting my original thought of about 2GB er user will work.
Data retention policies for our agency currently don't extend to email, but as we progress forward I know that it will.

And yes, for better or worse I DO have the ability to dictate the way it's going to work.

Thanks again
Comment: We have retention issues that are being imposed legally since we're a school district, so it's not an internal policy thing as much as a state auditing thing. I didn't know if you were referring to a government agency where there may be laws dictating how far back email can be subpeona'd...either way if you have power to dictate, all the better. You can then periodically educate users on how to store data offline and have it backed up properly and not abuse the mail system. Good luck!
Answer: Well, several large companies (my current and former employers included) cap their employees to 100 megabytes with the theory that if the quota is low, users will more aggressively archive or delete what they don't need.
Of course, if you need to deal with many or large attachments, that's a bad solution.
I personally clean my mailboxes quite frequently, and rarely, if ever, get close to the limit imposed. However, for large-volume mailing lists, you could easily hit it in under a week.
You really need to look at current usage, and see if there's a valid reason to go with a large quota, in my opinion.
Make sure you know what (if any) data rentention regulations surround your line of work, too.
Another option, employed by the first college I went to, was to quota our network space. We could use those 500 megs for storage, email, assignments, or our website. But it was all one quota.
Answer: Depending on just how email is being used at that agency you may find there are too many variables to just lock in one size for everyone. In my current position we have users who manage to keep their mailboxes to less than 100MB. There are others who cannot realistically keep theirs trimmed to less than 2GB+. I have it fairly easy due to the small number of users, currently about 25 with email accounts. What I've done is to monitor their mailbox sizes over the last year and use that as a guideline for setting individual limits. Not an ideal solution but one that works for us as it prevents users getting slack about their housekeeping.
Answer: 4gb upward. Email is not somethin to prune these days - my email folder goes back years and I use full text search quite often to find things. At the end of the day that is totally ridiculous low in cost - we do not talk of anything significant.


Where does the symbol $\mathcal O$ for sheaves come from?

Question: Sheafs are often denoted by the letter $\mathcal O$. What does this O stand for? To me it seems that more natural choices of symbols for sheaves would be $\mathcal S$ or $\mathcal F$ (for the french faisceau).
Comment: My guess. From the symbol for holomorphic functions.
Comment: And where does *that* come from?
Comment: See http://mathoverflow.net/questions/92135/etymology-of-the-o-notation-for-algebras-of-holomorphic-functions
Comment: @Brad I think this answers my question. Could you turn that into an answer?
Answer: Quoting the historical footnote in Grauert/Remmert, Coherent Analytic Sheaves, concerning the origin of the notation $\mathcal{O}$ for the rings of holomorphic functions (and their associated sheaves):

Some people think the symbol $\mathcal{O}$ was chosen in honor of Oka, sometimes it is even said that $\mathcal{O}$ reflects the French pronunciation of holomorphe. The truth is that the symbol was chosen accidentally. In a letter to the authors from March 22, 1982, H. Cartan writes: "Je m'étais simplement inspiré d'une notation utilisée par van der Waerden dans son classique traité 'Moderne Algebra' (cf. par exemple §16 de la 2e édition allemande, p.52)"
Comment: I just learned from Keith Conrad at HSM that your speculation about order is correct: http://hsm.stackexchange.com/questions/2922/who-first-introduced-the-notation-mathcalo-in-algebraic-geometry-or-algebra/2924?noredirect=1
Comment: Presumably because Dedekind used $\mathfrak{o}$ for an [order](http://en.wikipedia.org/wiki/Order_%28ring_theory%29) ("Ordnung" in German). But that may also be idle speculation, I don't know why.
Comment: And why did van der Waerden use it?
Comment: For those who are curious, van der Waerden _Moderne Algebra_, 2nd German edition, §16, begins with "let $\mathfrak{o}$ be a ring".


Show that chi-squared random variables are sub-exponential with parameter $4$

Question: Let $X \sim N(0, 1)$ so that $Y = X^2$ is distributed as chi squared with mean $1$. So the moment generating function of $Y$ is:
$$\frac{e^{-\lambda}}{\sqrt{1-2\lambda}}$$
$Y$ is subexponential if the moment generating function is bounded above by $e^{\lambda^2\sigma^2/2}$ for some $\sigma^2$ in an interval around $0$. We want to show that $\sigma^2=4$ is a valid choice when the interval being considered is $\lambda \in [-1/4, 1/4]$.
My attempt
It is equivalent to show that:
$$-\frac{1}{2}\log(1-2\lambda) \leq \lambda^2\sigma^2/2+\lambda$$
Taking a taylor expansion of the LHS around $0$:
$$-\frac{1}{2}\left ( -2\lambda -2\lambda^2 -\frac{8}{6}\lambda^3\frac{2}{(1-2\hat\lambda )^3}\right )$$
for some $\hat \lambda \in [0, \lambda]$.
Taking the difference between the RHS and the LHS:
$$RHS - LHS = \lambda^2\sigma^2/2+\lambda - \lambda -\lambda^2- \frac{4}{3}\frac{\lambda^3}{(1-2\hat\lambda)^3}$$
Since $\hat \lambda \leq \lambda < 1/4$, this is at least:
$$\lambda^2(\sigma^2/2-1)- \frac{4}{3}\frac{\lambda^3}{(1+1/2)^3}$$
which is nonnegative when $\sigma^2 \geq 2+64/81\lambda$. So it is sufficient for $\sigma^2$ to be at least $3$.
Since $3 \leq 4$, we have shown the desired result.
I commonly see $\sigma^2 = 4$ reported, so I am wondering if I made a mistake in my derivation.
Answer: There is a mistake at the end of the proof. The choice of $\hat \lambda$ that minimizes $RHS-LHS$ over the interval $[0, \lambda]$ is $1/4$, not $-1/4$ since $-1/(1-2\hat\lambda)^3$ is decreasing on this interval.
This correction gives a $\sigma^2$ of $8$, although apparently any value larger than $1$ is sufficient: How to verify chi-square random variables are sub-exponential variables?


KVM bridge only assiging ipv6 adresses on VM

Question: I use KVM in bridged network mode so I can have a VM that has an IP from the host LAN network.
My host (Ubuntu 18.04 server) networking is configured as below :
network:
  ethernets:
    enp0s31f6:
      dhcp4: true
    enp5s0:
      dhcp4: true
  bridges:
    br0:
      interfaces: [enp0s31f6]
      macaddress: EX:DX:5X:X0:X0:XO
      dhcp4: yes
  version: 2

I have created a bridged connection using virtsh for my VM.
<network>
  <name>host-bridge</name>
  <uuid>0fe3b67b-5155-4581-92f2-2510d864ed65</uuid>
  <forward mode='bridge'/>
  <bridge name='br0'/>
</network>

However, when i do an ifconfig on the VM, I only see an ipv6 address assigned.
enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 2a01:cb00:c3c:e500:9949:de93:1df3:c13c  prefixlen 64  scopeid 0x0<global>
        inet6 2a01:cb00:c3c:e500:ae16:cee:c3a6:c493  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::ff22:79d5:c7fe:884f  prefixlen 64  scopeid 0x20<link>
        ether XX:XX:XX:XX:XX:XX  txqueuelen 1000  (Ethernet)
        RX packets 5990  bytes 560495 (560.4 KB)
        RX errors 0  dropped 2778  overruns 0  frame 0
        TX packets 702  bytes 89008 (89.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

How can i force KVM or change the host/guest network to only assign IPV4?
I thought about turning ipV6 on the guest grub, but this seems to cause issues on VMs. My server is a remote one,and I don't want to lose ssh connectivity.
Answer: I had to explicitly disable dhcp4 and dhcp4 in the enp0s31f6 interface and restart both the host and the VM.
This is the final netplan config i used:
network:
  ethernets:
    enp0s31f6:
      dhcp4: false
      dhcp6: false
    enp5s0:
      dhcp4: false
      dhcp6: false
  bridges:
    br0:
      interfaces: [enp0s31f6]
      macaddress: XX:XX:XX:XX:XX:X
      dhcp4: yes
      dhcp6: true
  version: 2

Everything worked fine after that. It is also important to note that I had to explicitly set the mac address of the bridge on the config to avoid the router not updating the bridge as a seprate device.


Homology groups of spherical $3$-manifolds

Question: Let $G$ be a finite subgroup of $SO(4)$, acting freely on $\mathbb{S}^3$. How can we compute $H_2(\mathbb{S}^3/G; \mathbb{Z})$? Is it zero?
Comment: Poincare duality?
Comment: Perfect! This helps!
Answer: As Steve D suggests in the comments, this follows from Poincaré duality. The quotient $S^3/G$ is orientable because $G$ is a subgroup of $SO(4)$, and therefore
$$H_2(S^3/G; \mathbb{Z}) \cong H^1(S^3/G; \mathbb{Z}) \cong \operatorname{Hom}(\pi_1(M/G), \mathbb{Z}) = \operatorname{Hom}(G, \mathbb{Z}) = 0.$$
More generally, for any finite group $G$ which acts freely on a rational homology sphere $X$ with $\dim X = 2n + 1 \geq 3$, we have $H_{2n}(X/G; \mathbb{Z}) = 0$. To see this, first note that $X/G$ must be orientable. There are two ways to do this:

As $ \pi : X \to X/G$ is a finite covering, $\pi^* : H^*(X/G; \mathbb{Q}) \to H^*(X; \mathbb{Q})$ is injective, so $b_i(X/G) \leq b_i(X)$ for all $i$. As $X$ is a $(2n+1)$-dimensional rational homology sphere, its only non-zero Betti numbers are $b_0(X) = b_{2n+1}(X) = 1$. Note that $b_0(X/G) = 1$, while $b_{2n+1}(X/G) = 1$ if and only if $X/G$ is orientable, and all other Betti numbers are zero. As $\chi(X/G) = 0$, we must have $b_{2n+1}(X/G) = 1$ and hence $X/G$ is orientable.

Let $f : X \to X$ be a fixed-point free homeomorphism. By the Lefschetz fixed point theorem, the alternating sum of traces of $f_*$ on rational homology is zero. As $X$ is a rational homology sphere, it only has non-zero rational homology in degrees $0$ and $2n+1$ in which case it is one dimensional. On degree zero homology, $f_*$ is always the identity, while on degree $2n + 1$ homology, it acts by the identity if and only if $f$ is orientation-preserving. As the alternating sum of traces is zero, we see that $f_*$ must be the identity on $H_{2n+1}(X; \mathbb{Q})$ and hence $f$ is orientation-preserving. In particular, $G$ must be an orientation-preserving action, and hence $X/G$ is orientable.

Now that we know $X/G$ is orientable, we see that $H_{2n}(X/G; \mathbb{Z}) \cong H^1(X/G; \mathbb{Z})$ by Poincaré duality. As mentioned in point 1 above, we have $b_1(X/G) \leq b_1(X) = 0$ so $H^1(X/G; \mathbb{Z})$ has rank zero. As the first cohomology group is always torsion-free, we conclude that $H^1(X/G; \mathbb{Z}) = 0$ and hence $H_{2n}(X/G; \mathbb{Z}) = 0$.


Potential resource leak: 'httpclient' may not be closed at this location

Question: Getting the error mentioned in the title in the line

return responseXML.toString();

HttpClient httpclient = HttpClientBuilder.create().build();
HttpPost httppost = new HttpPost("test");
httppost.setHeader("Authorization", "test");
httppost.setHeader("content-type", "text/html; charset=UTF-8");
try {
        httppost.setEntity(new UrlEncodedFormEntity(NameValuePairs));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    HttpResponse response = null;
    try {
        response = httpclient.execute(httppost);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    InputStream inputStream = null;
    try {
        inputStream = response.getEntity().getContent();
    } catch (IllegalStateException | IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    BufferedReader bufferReader = new BufferedReader(
            new InputStreamReader(inputStream));

    StringBuffer responseXML = new StringBuffer();
    String line = "";
    try {
        while ((line = bufferReader.readLine()) != null) {
            responseXML.append(line);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        inputStream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println( responseXML.toString());
    return responseXML.toString();

Comment: Where is httpClient initialised, and is it autocloseable? It may be possible to do a try-with-resources here: try (HttpClient httpClient = ....) { }
Comment: added httpclient initialisation
Answer: As noted, try-with-resources ought to help.
To exploit try-with-resources your HttpClient needs to be of a autocloseable type: one that implements java.lang.AutoCloseable. Assuming you're using Apache httpcomponents...
try(CloseableHttpClient httpclient = HttpClientBuilder.create().build()) {
   // note the CloseableHttpClient which allows this to happen.
}

There are other opportunities in your code for this pattern, for example where you have the buffered reader.
See this tutorial on try-with-resources.
Answer: You could use a try-with-ressources : try(HttpClient httpClient = new HttpClient)
(or whereever you get the client from) or you could use a finally where you close the htppClient:
finally {
httpClient.close();
}

Comment: there is no httpClient.close ()


When an anonymous user hits '/user/login' route, how can I trigger an event?

Question: I want to trigger an event when an anonymous user comes to /user/login. I made an event subscriber, but it only fires one time:
namespace Drupal\mymodule\EventSubscriber;

use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Routing\TrustedRedirectResponse;

/**
 * Class LoginSubscriber.
 *
 * @package Drupal\mymodule\EventSubscriber
 */
class LoginSubscriber implements EventSubscriberInterface {

  /**
   * Redirect the user before we let them access a login form.
   */
  public function onLoginRequest(GetResponseEvent $event) {
    $account= \Drupal::currentUser();

    $config = \Drupal::config('mymodule.settings');
    $base_url = \Drupal::request()->getSchemeAndHttpHost();
    $site_key = $config->get('eweb_site_key');

    switch ($config->get('mode')) {
      case 'test':
        $url = $config->get('eweb_base_test');
        break;

      case 'live':
        $url = $config->get('eweb_base_live');
        break;

      default:
        $url = $config->get('eweb_base_test');
        break;
    }

    // User is accessing login page, check for active session
    if ($event->getRequest()->getRequestUri() == '/user/login' && $account->isAnonymous() && $event->getRequest()->query->get('loginFailed') == FALSE) {
      $response = new TrustedRedirectResponse($url . '/StartPage.aspx?Site=' . $site_key . '&URL_success=' . $base_url . '/user/netforum/{token}&URL_failed=' . $base_url . '/user/login?loginFailed=true');
      return $response->send();
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = ['onLoginRequest'];
    return $events;
  }
}

The purpose is that the external site will look to see if this user has an active session, if so, it will redirect back to my site with a token and that controller will handle logging in that user with some logic and user_login_finalize.
However, it is currently only firing once.
Comment: Adding `\Drupal::service('page_cache_kill_switch')->trigger();` to user login form via form alter seems to do it, but that feels dirty, but implementing my own middleware as alluded to here seems like overkill: https://drupal.stackexchange.com/a/237560/57
Answer: I agree with @Berdir, it shouldn't be a problem if the internal page cache stores a response for each query string.
But the issue here is probably that TrustedRedirectResponse is a cacheable response. When you use such a cacheable response you  have to add the correct cache metadata for the content of the response, here a context for the query arg:
  if ($event->getRequest()->getRequestUri() == '/user/login' && $account->isAnonymous() && $event->getRequest()->query->get('loginFailed') == FALSE) {

    $response = new TrustedRedirectResponse($url . '/StartPage.aspx?Site=' . $site_key . '&URL_success=' . $base_url . '/user/netforum/{token}&URL_failed=' . $base_url . '/user/login?loginFailed=true');

    $cache_metadata = new CacheableMetadata();
    $cache_metadata->setCacheContexts(['url.query_args:loginFailed']);
    // or set max-age = 0 instead
    // $cache_metadata->setCacheMaxAge(0);
    $response->addCacheableDependency($cache_metadata);

    $event->setResponse($response);

    }
  }

Then the dynamic page cache will vary by this context. Or add max-age = 0 to disable the dynamic page cache, because caching won't help here much, this is already cached for anonymous users.
Comment: You could optimize it a bit and only cache by that specific query argument with url.query_args:loginFailed
Answer: Page cache is a middleware and happens outside of event subscribers.
You could implement that logic in JS for example.
But it's not exactly clear to me why it is even a problem that it is only called once. The internal page cache will cache that for the exact URL and will always return a redirect and not a redirect with the login failed query argument. is that a problem?
Not 100% sure about the dynamic page cache, if that caches the request as well (you can easily see that based on the headers and/or the cache_dynamic_page_cache table), then you need to add a cache context for that query argument.
Comment: Well, I went to the page, and my breakpoint triggered. I went back again, and it did not. To me it seemed like it only fired that code once, unless I missed something. After adding the killswitch trigger, the breakpoint hit every time.
Comment: Yes, it is cached. My point is that this is not actually a problem because it should redirect or not as expected? So if that's all your doing, then it should just work and it doesn't matter that it is actually cached. And if it results in an endless loop or so in case of a fail then that might be due to dynamic page cache
Comment: Should always redirect if anonymous and the query string parameter is false or does not exist


Marginal Likelihood in the Bayesian Posterior Formulation

Question:
What do we mean by "Integrating out the parameters" in Marginal likelihood? Particularly in the posterior formula.

The marginal likelihood in a posterior formulation, i.e P(theta|data) , as per my understanding is the probability of all data without taking the 'theta' into account. So does this mean that we are integrating out theta? If that is the case, do we apply limits over the integral in that case? What are those limits?

Some people say that we can integrate the product of prior and likelihood to obtain the marginal likelihood. So how does this links in posterior formulation, which already has this product in numerator.?

Calculation of Marginal likelihood is ignored at times. Its a constant, that is not used usually. However, we don't call such approaches "Fully Bayesian". What kind of advantages does Full Bayesian provide if you are adding a constant.
Comment: Would this answer your question? [Marginal Likelihood as probability distribution](https://stats.stackexchange.com/questions/318547/marginal-likelihood-as-probability-distribution)
Answer: Considering a statistical model represented by a family of probability densities (wrt the same measure $\text dx$)
$$\mathfrak F=\{f(\cdot;\theta)\,;\ \theta\in\Theta\}$$
each density within that family is associated with a parameter $\theta$.
Given in addition a probability density $\pi(\cdot)$ over $\Theta$ (endowed with a measure $\text d\theta$) the marginal likelihood is defined as
$$m(x) = \int_{\Theta} f(x;\theta)\,\pi(\theta)\text d\theta\tag{1}$$
and is therefore

the marginal density of $X$ associated with the joint density $f(x;\theta)\,\pi(\theta)$ of the pair $(X,\theta)$
free of the parameter $\theta$, which is effectively integrated out in (1)
a misnomer in that it is a function of $x$ and only of $x$, while the likelihood is primarily a function of $\theta$, which is why the term evidence or integrated likelihood is often preferred
an average of all possible density values at $x$, when weighting each value of $\theta$ with $\pi(\theta)$
the prior predictive density resulting from (i) generating $\theta$ from the prior and (ii) $X$ from the model associated with the generated value of $\theta$
the normalising constant in the posterior distribution, so that it integrates to one in $\theta$
a measure of how the model $\mathfrak F$ fits the realisation $x$ of $X$ and hence a tool for model comparison (via Bayes factors)
not necessarily needed when approximating Bayesian procedures by simulation (although this approach remains "fully Bayesian") since simulation techniques often bypass the normalising constant.


how to play video more than one time in customized web view in android?

Question: I am developing an android app in which i have to load a web page and then play a movie clip from it in a customized web view. I have done almost every thing there and even movie is played in web view. But problem is that when we play it once it does not play again. Even when same link is accessed via android standard browser it works fine. But in customized web view video only played once. Any body please tell me that why is that happening and also how to over come this issue as my app is just in finish line.
Thanks:
Comment: try  using frame layout and adding the webview to it. http://stackoverflow.com/a/8011027/1220586
Answer: try
<code>@Override
    public void onPrepared(MediaPlayer mp) {
        // TODO Auto-generated method stub
    mCustomViewCallback.onCustomViewHidden();

}
</code>
In your WebChromeClient.


Can anyone please help on dendogram labels in r?

Question: I have used this code to make horizontal dendogram but somehow the variables name are not showing properly.
structure(list(Location = c("Plastic", "Foamed Plastics", "Paper & Cardboard", "Rubber", "Cloth", "wood", "Glass & Ceramic", "Metal"), Cox.s.Bazar = c(3072L, 463L, 208L, 177L, 59L, 63L, 62L, 28L), Chittagong = c(1372L, 397L, 402L, 60L, 87L, 27L, 28L, 8L), St..Martin.s.Island = c(1121L, 48L, 216L, 54L, 107L, 74L, 18L, 27L), Kuakata = c(544, 37, 65, 12, 34, 0.1, 17, 0.1), Kotka = c(126, 0.1, 12, 0.1, 3, 0.1, 0.1, 0.1)), class = "data.frame", row.names = c("Plastic", "Foamed Plastics", "Paper & Cardboard", "Rubber", "Cloth", "wood", "Glass & Ceramic", "Metal"))

Code I have used
rownames(df) = c(df$Location)
head(df)
data = df[,-1]
head(data)
mydata = scale(data)
head(mydata)
require(stats)
res.dist = dist(x=mydata, method = "euclidean")
hcl = hclust(d=res.dist, method = "complete")
require(graphics)
plot(as.dendrogram(hcl), xlab ="Height", horiz = T)

Answer: You need to set the margin with par(mar = c(...))
# order of margin is bottom, left, top, right - for your case increase the right margin
par(mar = c(5, 5, 5, 15))
plot(as.dendrogram(hcl), xlab ="Height", horiz = T)

ggplot solution
To use ggplot it required to do some manual for the text if want the text to be on the right side of plot. I found this solution from another answers for your reference I also put the link at bottom.
library("ggplot2")
library("ggdendro")

dendr    <- dendro_data(hcl, type="rectangle") # convert for ggplot
clust    <- cutree(hcl, k=2)                    # find 2 clusters
clust.df <- data.frame(label=names(clust), cluster=factor(clust))
# dendr[["labels"]] has the labels, merge with clust.df based on label column
dendr[["labels"]] <- merge(dendr[["labels"]],clust.df, by="label")
# plot the dendrogram; note use of color=cluster in geom_text(...)
ggplot() +
  geom_segment(data=segment(dendr), aes(x=x, y=y, xend=xend, yend=yend)) +
  geom_text(data=label(dendr), aes(x, y, label=label, hjust=0, color=cluster),
    size=3) +
  coord_flip() + scale_y_reverse(expand=c(0.2, 0)) +
  theme(axis.line.y=element_blank(),
    axis.ticks.y=element_blank(),
    axis.text.y=element_blank(),
    axis.title.y=element_blank(),
    panel.background=element_rect(fill="white"),
    panel.grid=element_blank())

Created on 2021-05-19 by the reprex package (v2.0.0)
Link to another answer: Colorize Clusters in Dendogram with ggplot2
Comment: Thanks Nguyen, but if I want turn it on right side then how will I do?
Comment: The base plot is on the right side & I updated the ggplot solution which now also have text display on the right side instead.


Get list element with JQuery according to its value

Question: Assume I have the following list:

RU

RO

With JQuery, I want to select the list element which has the value RU. How can I achieve this?
Answer: Try something like:
$(".pagination li a").filter(function() {
    return $(this).text() === "RU";
});

Answer: Alternative solution in one line ;)

var ru = $("ul li a:contains('RU')").text();

// Demonstration purposes only

$('.tron').text(ru);

RU

RO

Answer: select all list items and use the jQuery .filter() method to filter elements with the needed text:

$(function(){
    $(".pagination li").filter(function(){
        return $(this).text()=="RU";
    }).css({
        "border":"2px solid red"
    });
});

RU

RO

Answer: You have to specify value attribute in each li tag
As in your example

RU

RO
 after that to access value of li tag use this
$(li).val();
Answer: this is how you can achieve this $('li').eq(0).text() == 'RU'; simple as that but you need to navigate more often to jquery site for info on this kind of stuff.


Content type - field type - Select list: Add options: Existing content on the website

Question: I want to do something in my website, but I can't figure how. Let me explain:
I have a content type called bananas, and I have created a lot of bananas pages.
I want to create a content type called peaches and add a field with a select list called "Existing bananas on this peach" - and I want to be able to select from all the bananas pages my website has.
Then, if it is not asking too much, I want this field to be a link to the specific bananas page.
Am I making myself clear? For me it's simple, but I don't think there's a way on Drupal to do that. Or maybe there is, but I'm just not seeing it.
Hard or easy, do you have any solution?
Thank you so much in advance, hope you guys can help me.
Answer: Assuming you are using Drupal 7, I think you need the Entity Reference module.
This module provides a field that you can add to your content type peaches and provides the requested widget that allows you to select from all the bananas nodes your website has. When outputting the peaches node, the standard formatter (that the Entity Reference module offers) creates a link using the selected bananas node's title.
Comment: Thank you! Entity Reference module solved my problem. Everything works perfectly!
Answer: You can't do this with just Drupal core, but this is easy to do with either https://www.drupal.org/project/references and https://www.drupal.org/project/entityreference.  Simply download and enable one of these modules (Node Reference is in the References project), add a field of that type to the new Peaches content type called "Existing Bananas", and configure the field's options and display.  You'll have a number of options in both the form field configuration (auto complete, list, etc) and the display (render, link, plain text of title, etc).  It's hard to say which is better for your site without knowing what else you are doing.  I personally use Entity Reference more often, but that's largely because it is a requirement for Organic Groups and Commerce which I often end up using.  Entity Reference requires both Entity API and CTools which will add a lot of code if you aren't already using these.
I would test these modules on a development version of your site and see which one works best for you, but either one should work.
Comment: Thank you for your anwser. Entity Reference module solved my problem.


Is the polyphony limit on a Digital piano tied to the sound engine?

Question: I don't want to end up paying more for a digital piano for a higher polyphonic capability if I don't need it.
I was wondering if the polyphony limit on digital pianos is tied to the sound engines they possess, or to the actual physical key presses like on some old computer keyboards.
Essentially, if I connect a Yamaha P45, which has a polyphonic limit of 64, via midi to a piano VST, would I be able to go above that limit?
Answer: A VSTi will have its own polyphony limit, completely separate to that of the generating instrument.
This usually comes into play when you're using a lot of pedal. The notes on/off are still generated, but you run out of voices to play back the oldest sustained notes, so they're 'stolen' from the playback engine. Many modern keyboards are smart enough steal the 'least noticeable' note if they have to steal.
This will not affect any note on/off data, as it's the pedal data keeping the sustain going. The key down/up events have all been accounted for.
tbh, I have never tested trying to press more than 64 keys simultaneously to see if it can hold them all in memory waiting for a key-off. It feels like it would be a futile pursuit ;)
Comment: Thank your for your quick answer! That's very good to hear I'm pretty sure that answered my question! Just to be 100% sure though, that means that they key presses themselves don't have a limit right?
Comment: There *might* be a simultaneous keypress limit, as intimated, but holding down more than 64 keys at once is really not all that common. I used to work for Yamaha R&D, & honestly, this was never even mentioned.
Comment: True true... Thank you again so much!! You really cleared this up a lot for me! Have a fantastic day :D
Answer: Computer keyboards are typically wired as a scanned matrix.  The keyboard hardware drives each row, and then sees which columns are connected.  If three keys that are pressed simultaneously would form three corners of a rectangle in the matrix, then both rows and both columns will be connected to each other, and the hardware will have no way of determining which three corners of the rectangle are pressed, or whether all four are pressed.
While some cheap toy musical keyboards that are limited to two-note polyphony use such a design, any non-toy keyboard will either include a diode with each key to ensure that all combinations of keys can be read by the matrix, or else use dedicated input circuitry for each key avoiding the "rectangles" problem altogether.
While some keyboards might possibly limit polyphony when outputting MIDI data, supporting 127-note polyphony would not be significantly harder than supporting anything less.
Comment: For additional info, the term for this is called [rollover](https://en.wikipedia.org/wiki/Rollover_(keyboard)).
Comment: @AndrewT.: The term "rollover" is sometimes used with a bit more limited meaning than suggested by that article, to describe situations where a transition from having one combination of keys pressed to having some other combination of keys pressed can be reliably handled even if some of the latter keys are pressed before the former keys are released, without reporting any states that have not actually occurred.  If a two-key polyphonic keyboard handles rollover correctly and one presses two keys, holds them while pressing two more, and then releases the first two, then...
Comment: ...it will register the latter keys as having been pressed sometime between the time they were and the time the first two keys were released. Overlap may slightly skew the timing with which the latter keys are detected, but won't cause erroneous events.  On a keyboard that doesn't handle this correctly, key overlap, no matter how brief, may trigger notes that are just plain wrong.
Comment: Also note that any serious musical keyboard won't just have an on/off switch for its keys, but rather a sophisticated measurement device that allows it to measure how fast and how far each key was pressed. There's definitely no scanned matrix for that.
Comment: @Vilx-: Some velocity-sensitive keyboards have two contacts on each key, and measure the time between them.  Doing this usefully would require scanning keys much faster than would be necessary for note on/note off, but fast scanning by a dedicated controller is hardly impractical.
Comment: @supercat Hmm ok, I didn't know that. Still, if you're already going through all this effort (and velocity sensitive keyboards aren't cheap) then adding matrix-style wiring which has limitations on how many keys can be pressed at the same time seems... kinda counterproductive. It's possible in theory, sure, but does anyone _actually_ do that?
Comment: @Vilx-: I've encountered one keyboard in the cheap-toy section of a retailer which looked reasonable (I think it was a 3.5 octave keyboard for US$5) which had goofy matrix behavior, some non-velocity-sensitive keyboards used a diode per key when that was cheaper than the per-pin cost of input chips, and some inexpensive velcoity-sensitive keyboards use a 2xN matrix, but since the two contacts for each key can't close simultaneously that doesn't pose a problem.  Don't know if any velcoity-sensitive keyboards ever used diode matrices, but I wouldn't be surprised either way.
Comment: @supercat I haven't  seen velocity sensitive keyboards on anything cheaper than 500$. I think that if it's meant to be used as a serious musical instrument, then this won't be a problem.
Comment: @Vilx-: I bought my Yamaha PSR-33 (five octave, velocity sensitive) in 1992 for $300, and it served me well for about 20 years and my dad for another 5.  Not sure how it was wired internally.
Comment: @supercat - OK, I had meant recent prices. :) $300 in 1992, Google says, is about $620 today.
Comment: @Vilx-: The keyboard was about 1/7 the cost of a 68030-baed Macintosh system that I bought a couple years later, and 1/10 the cost of the 80386-based PC I'd bought a couple years before.  I would expect that for $500 today or even $300 today someone could buy something a lot nicer.
Answer: VST (and other synth/sampler) 'voices' do not correspond exactly to musical notes.
Sampled piano instruments typically use a number of underlying voices per keyed note - as well as the basic sound of the string vibrating, you might get separate samples for hammer movement, damping, and sympathetic resonance of multiple other strings.
The point of those 64 voices is not to allow you to play 64 different notes at once, it's to more convincingly reproduce the sound of the ten or so you might reasonably play at once, where each note requires multiple voices.
So, it's possible your MIDI device does have a limit on how many simultaneous held down keys it can detect, but that doesn't mean it won't effectively use the available voices on the VST instrument


Linux + exit status from Perl one-liner code

Question: I use Perl one-liners in my ksh scripts.
Sometimes it's necessary to get an exit status from the Perl one-liner in order to verify if the Perl one-liner succeeded or not.
For example, I need to verify if the "print" in the Perl one-liner code succeeded or not.
But Perl will exit with status 0 in both cases even if Perl does not match the words "AAA and BBB".
Maybe by changing my code I can get exit status 0 when Perl matches successfully. And get exit status 1 when Perl does not match the words "AAA and BBB".
Is this possible?
more file
AAA BBB

perl -ne '/AAA/ && /BBB/ && print' file
AAA BBB

echo $?
0

more file1
ZZZ

perl -ne '/AAA/ && /BBB/ && print' file1
echo $?
 0

Answer: Count matching lines and set exit code based on it in an END{...} block:
perl -ne '/AAA/ && /BBB/ && print && $MATCH++; END{exit 1 unless $MATCH>0}' file

Comment: another question - where to put the "&& $MATCH++; END{exit 1 unless $MATCH>0}'" on ---> perl -ne'BEGIN { $ip = shift(@ARGV); } print if /(^|\s)\Q$ip\E(\s|$)/; ' "$STR" file
Comment: perl -ne 'BEGIN{ ... }; if( .... ) { print; $MATCH++ }; END{ exit 1 unless $MATCH>0 };'
Comment: I got error: perl -ne'BEGIN { $ip = shift(@ARGV); }; if (^|\s)\Q$ip\E(\s|$) { print; $MATCH++ }; END{ exit 1 unless $MATCH>0 };' "$STR" file
syntax error at -e line 1, near "(^"
Substitution replacement not terminated at -e line 1.^C
Comment: fix `if` statement between BEGIN and END blocks:  `if( /(^|\s)\Q$ip\E(\s|$)/ ) { print; $MATCH++};`


How do I use module aliases in Serverless Stack?

Question: I am trying to follow the guide for Material UI on replacing the existing style engine with styled-components https://mui.com/guides/styled-engine/
I am also using it as part of the Serverless Stack framework which shows that it is using ESBuild https://docs.serverless-stack.com/packages/cli#build
Is it possible to do this module aliasing? I looked around but wasn't able to figure out how.
Answer: After checking on their slack, I figured out that ESBuild is only used in building the backend SST.Functions. But in the material-ui examples, there is an example on how to do the aliasing for create-react-app https://github.com/mui-org/material-ui/tree/next/examples/create-react-app-with-styled-components-typescript.


css clip is not working

Question: I tried several different permutations on css position for parent and child.  I have a spritesheet with 3 cells.  I have some old code that uses the background image trick and I'm trying to update it to use clip :rect( )  inside css.    Ideally I'll have a span that controls the display size. and combination of css properties on the image will get the right cell. What am I missing here?

Answer: Well, you may want to change the clip value to rect(top, right, bottom, left), then change the position of your image to absolute so you'll be able to freely adjust its position (left/top). You'll then need to calculate the top/left accordingly based on the size of the span block and the size of your sprite.
Here is an example.
Some basic information on clip property can be found here http://www.w3schools.com/cssref/pr_pos_clip.asp

.marketItemImage {
  display: inline-block;
  width: 100px;
  height: 104px;
  border: 3px solid black;
  overflow: hidden;
}
img {
  position: absolute;
  left: -189px;
  clip: rect(0px, 300px, 104px, 200px);
}

Comment: I'm surprised that a left: -189px is needed.  I was expecting  clip to take care of all that.
Comment: Ok I get it now.  clip rect parameters don't move the underlying image at all. You still have to use css left and top for positioning.

css clip is not working


why curve should be planar?

Question: Please explain to me why it is necessary to assume that the curve was planar?
If for a regular surface parametrized by $f$ we define the curvature in the direction of $X$ to be the curvature of a curve $c$ formed by intersecting the plane through $p$ and parallel with $\nu$ and $X$ with the surface, and $c'(0)=X$, where $X \in T_pf $ and each $X$ is a unit vector. That is $c$ is a planar curve spanned by $\nu$ and $X$.
Note: This question has been edited to follow the site regulations.
My attempt at solving this problem: Let $c$ be a parametrized curve on the surface, parametrized by arc length, that is $\langle c',c' \rangle =1$. and $ c(0)=p$. The total curvature $\kappa$ of $c$ at $p$ is equal to the length of $c''$. We decompose $c''$ into its tangential and normal components. Now, $c''(0) =c''(0)^{Tang.}+\langle c''(0), \nu \rangle \nu$ when $c$ is a planar curve in a plane parallel to $\nu (p)$. Since $c''(0)$ and $c'(0)$ are orthogonal, the tangential component of $c''(0)$ vanishes. this is not necessarily true if $c$ is non-planar. When  $c''(0)^{Tang.} \neq 0$, we have $\kappa=\Vert c''(0) \Vert > | \langle c''(0), \nu \rangle |= | II (c'(0),c'(0) | $.
Answer: It's more accurate to say the planar hypothesis is without loss of generality: The normal curvature of a surface depends only on a point $p$ of the surface and direction $v$ at $p$.
Comment: But only for the normal plane slice is the normal curvature (up to sign) the actual curvature of the curve.
Comment: @TedShifrin Right, I should explicitly clarify that "depends only on..." means we're looking only at curves in the surface whose osculating plane is the normal plane to the surface, not arbitrary curves with the given position and velocity.

why curve should be planar?


How to prove the formula for the argument of a complex number?

Question: $\arg(x + iy) = 2 \cdot \arctan(\dfrac{y}{x + r})$
there is always the mark, this is derived from the 'Half-angle formula'
How can I come from $\tan(\phi) = \tan(\phi + k \pi) = \dfrac{y}{x}$  to $\phi = \arg(x + iy)$?
Answer: Look at the next figure
$\hspace4cm$
Clearly ${\rm Arg}(z)=2\theta=2\arctan\dfrac{y}{x+r}$. Note that this gives the principal determination of the argument of $z\in\mathbb{C} \setminus((-\infty,0]\times\mathbb{R})~$.


Favicon Icon Not Working

Question: I am Trying to add a favicon into my website. I created the icon on https://www.favicon-generator.org/ and pasted in the given code. I'm sure the file directory is right as I've checked multiple times. Could someone help me out, what styling to I need to add or remove or am I missing something?
  <DOCTYPE html>

<title>Drafted</title>

<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="style.css">

<video id="video-bg" preload="auto" autoplay="true" loop="loop">
    <source src="video.mp4" type="video/mp4">
        Video Not Supported :(
    </video>

SUBMIT A BACKGROUND VIDEO!

drafted

STEAM

ESEA

TWITTER

Answer: Favicons have a tendency to cache super-aggressively.
Your code doesn't appear to be off, but I would suggest the following troubleshooting steps:

Try accessing the same page in a different browser. Make sure that going to /favicon.ico directly in the browser actually works, and gives you the icon you expect.
Load the page in a different browser (or a different computer) - one that has preferably never seen this page before.

I'm not sure it's related to this, but there a couple of issues with other parts of the code that could possibly be causing this side-effect;

You've missed a ! in your doctype - it should be <!DOCTYPE html>
Your title should be inside your head section.

I suggest also running your html through a linter, as badly formed html can have really strange side effects sometimes.
Comment: I'm not sure, but Chrome actually tries to get `favicon` even if it's not there in markup, each time.
Comment: Thanks, turns out it was my file directory It was hidden In a folder I didn't see. Thanks for telling me about linter too never heard of it before!
Answer: Your favicon is referenced correctly in your HTML, and should display. As such, there are only a few possible causes of your problem. Check whether you can navigate to www.yourwebsite.com/favicon.ico manually.
If so, there's a caching issue:

Try clearing your HTML cache. This can be done by pressing CTRL + F5.
Try clearing your CSS cache. This can be done by holding SHIFT while clicking on the refresh icon of your browser.
Try a hard refresh. This is done by holding CTRL and SHIFT and then pressing R.

If not, the problem is that the file isn't found:

You may not have got your favicon stored at the root of your project. Ensure that the file is actually at root level.
You may not have uploaded your favicon to the server. Check whether the file exists on the server itself.
If the file exists on the server, ensure that it is a valid .ico image.

You also have invalid markup, which may be the cause of your issue:

Your <DOCTYPE> tag needs an exclamation mark before html.
Your <title> tag should be inside .

I would recommend running your HTML through a markup validation service, such as The W3C Markup Validation Service, to ensure that you have valid HTML.
Confirming all of that and fixing up those two errors should almost certainly fix your favicon not coming through.
Hope this helps!

Favicon Icon Not Working


how to force build when ever a page shows up in flutter

Question: Hello hope you guys are ok.
the problem I'm facing is we have a main_page which leads to a page doing some changes on data which are show on the main page.
after some process if the user touches back button and goes to main_page app loads it from stack and the data are not shown because it does not get rebuilt.
I don't want to control back button because there are other pages which lead to data changing page and I also tried using valuelistenablebuilder but I don't know why it goes wild and gets into a screen refresh loop without even changing the valueListenable I used redux to manage the value.
actual reason I want main page to rebuild is to call a method. why do I not call that method in second page is complicated and because i don't want to.
in conclusion I want main page to rebuild whenever it shows up even when it's read from the stack or even is there a way to tamper with stack of the pages without visual changes to the user.
Answer: you need to use Shared preferences plugin
https://pub.dev/packages/shared_preferences
Comment: my question is not about state management its about stack changes and the lower stack does not get rebuilt forcefully when the upper stack pops.
i know i can use a call back function but its not efficient for my situation im also using redux but its not rebuilding on value change.
Comment: Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer).
Answer: If I understood the question correctly
The flow should be:
Screen A
<code>@override
void initState() {
 loadSomeDataFromDB();
 super.initState();
}
</code>
Screen B
<code>@override
void initState() { *//INIT AS EXAMPLE*
 changeSomeDataFromDB();
 super.initState();
}
</code>
You can try this in Screen A
<code> onPressed: () async {
            bool? shouldLoadDataAgain = await
            Navigator.of(context).push(PageRouteBuilder(
              pageBuilder: (context, animation, secondaryAnimation) =>
                  const ScreenB(),
            ));
            if(shouldLoadDataAgain!=null&&shouldLoadDataAgain ==true){
             loadSomeDataFromDB();
            }
          },
</code>
and this in Screen B when user press back button
<code>  onPressed: () async {
            Navigator.of(context).pop(true)
          },
</code>
Comment: well you have understood the flow right but i don't want to control back button because there are man routes to the page B and the data could have been changed on page B so page A needs to update. I don't know why storeconnector of redux does not rebuild the page A on change.
Answer: solution for my problem was that used redux store had an object and updating object property or a list does not count as variable change to redux so the widget wouldn't rebuild.