Renew facebook access token from Node

Question: In my Node application, I store in the DB a long-lived user facebook access token and run a cron periodically.
There's a way to renew that access token after the 60 days without asking the user to renew it in my mobile app? I use fbgraph node module to communicate with the graph API, so maybe there's a way to renew that token from the backend itself without going to the mobile app, ask for status and login again with the JavaScript SDK.
Answer: Tokens always need to be refreshed with user interaction, there is no way to auto-refresh them.
But you don´t need to login again, you just need to use this function to refresh the User Token: https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/
Comment: can you give a code example to renew it given the old access token?
Comment: just open the link in my answer, there is a code example. it´s really easy. you don´t need the old token, just call the function.

Renew facebook access token from Node


How can I hide nodes from view, but open access to JSON:API at the same time?

Question: I would like to use Drupal as a decoupled system, sourcing its content by using Gatsby.
Having set it all up, I would like my Drupal nodes not to be accessed by random unauthenticated users so I removed the permission from 'View Content'.
Having done that now I can't access the content by my Front end service which uses JSON API (unauthenticated of course).
What's the method to remove the node pages from the site and leave only the admin backend, while allowing to access the content using JSON API ?
Answer: If you're working with unauthenticated consumers, the Rabbit Hole module has the means to disable node/* routes without using the user role/permission system.
The Rabbit Hole module has a sub-module Rabbit Hole nodes that, when enabled, will let you disable node/* routes. This should leave open your jsonapi/* endpoints to anonymous consumers.
Answer: Another solution I found was to remove the 'view published content' permission for anonymous users, and activate the Basic Authorisation module to use for JSON API requests.


HTML/CSS auto width for a table inside smaller div with overflow visible

Question: I have a div with a small width and an overflow visible. I have inside a larger table with only one cell and a text inside:

         A small text with spaces...

I would like the width of the table to be set automotically as the text width but without breaking line. i.e. I would like to have the same result I would have if I writed only:

       A small text with spaces...

How can I set the table to not be "of minimum width" without specifying a precise witdh?
Comment: Please add your CSS, it's not so clear, what you want to achieve. Maybe `white-space: nowrap;` on the table cell might help. But what are you actually trying to solve? This doesn't look like tabular data at all, so using a table isn't the best choice. If you explain your real goal a bit, maybe we can find a simple solution.
Comment: A little unclear on what you want to achieve... Are you looking for `min-width`, i.e. `min-width: 100px;`?
Comment: From the code snippet I am unsure why you need that table at all, why not just use the div tag although I suspect you have a reason that is unclear here but more information would be useful
Answer: I think your answer is here: Object larger than outside div
It uses the position absolute to remove the stipulations of the containers surrounding it. The one issue then is positioning, but You can fix that with setting the left and top CSS rules.
jsfiddle example

         A small text with spaces...

         A small text with spaces...

EDIT
As said in a comment above by "insertusernamehere": "white-space:nowrap;" works as well.
NOTE:
Tables are not the best thing to use. Use DIV's and set the `"float:left/right;" CSS style so that they mimic tables. It is easier to code (for me at least, it takes some getting used to at first.) but it is much more browser friendly and you have more room to play in.


Redirect in Centos 6

Question: I have a site running on an apache server, and I'm trying to redirect the user to the main page by just typing the ip, such as this:
188.8.131.52 brings you to 22.214.171.124/my/website/main/page
How do you do that, and which is the best way?
Apparently it's pretty easy, but the guides and documentation I've found are kinda confusing, and since it's my time doing this, I'm pretty lost.
Comment: You can use mod_rewrite in your apache configuration to write rewrite rule, see here - https://httpd.apache.org/docs/2.4/rewrite/remapping.html
Answer: The simplest way is to append this to your configuration file:
<code>RedirectMatch ^/$ /my/website/main/page
</code>
mod_rewrite not required.
Comment: Yup this worked, thanks man. While it can be found in the documentation, it's incredibly confusing for someone who hasn't dealt with apache...

Redirect in Centos 6


Fazer animate com scrollTop

Question: Quero que a div #parte3 fiquei display:block quando chegar no scrollTop = 1110.
Minha sintaxe
console.log($(window).scrollTop(), $(window).scrollTop() > 1110);
    if ($(window).scrollTop() > 1110) {
        $("#parte3").animate("display", "block");
    };

Comment: se possivel poste o resto do código html/css  ou algum exemplo na internet.
Comment: Qual é a diferença para [esta outra pergunta](http://pt.stackoverflow.com/questions/44476/scrolltop-n%C3%A3o-funciona) que você fez em dezembro? O problema parece o mesmo, falta um listener para o evento de scroll.
Comment: Não consigo imaginar qual seria o sentido de animar a propriedade `display`, visto que não tem situações intermediárias. Mais fácil você explicar o resultado desejado do que o jeito que está tentando fazer. Não seria simplesmente `.css( "display", "block" )` ? E fora isso, teria que colocar esse teste ao fazer o scroll, e devolver a propriedade para a original se for menor que 1110.
Comment: Acho que deves usar `.css()` em vez de animate: `$("#parte3").css("display", "block");`. Se quiseres fazer uma transição tens de usar `visibility` ou `$("#parte3").fadeIn()`
Comment: @Sergio eu gostaria de deixar um dalay até a div aparecer, não quero que seja um movimento rápido.
Comment: Já experimentaste com `$("#parte3").fadeIn()` ou `.faeOut()`?
Answer: Provavelmente é algo nesta linha que você precisa:
$(/* elemento desejado, ou simplesmente window */).bind('scroll', function() {
   if ($(window).scrollTop() >= 1110) {
      $("#parte3").css("display", "block");
   } else {                                  // use o else se quiser reverter ao subir.
      $("#parte3").css("display", "none");   // aqui ponha o display original.
   }
});

Se está falando de display:none, troque por opacity com animate (ou fadeTo, que é o jeito simples do jQuery).
EDIT: Como bem lembrado pelo @Sergio, se for pra mostrar 100%, basta usar .fadeIn().
$(window).bind('scroll', function() {
   if ($(window).scrollTop() >= 1110) {
      $( "#parte3" ).fadeTo( 500, .5 ); // velocidade, transparência
   } ...

Neste caso, não use display:none inicialmente, e sim opacity: 0.
E ainda, se desejar um temporizador antes do efeito, tem o .delay():
$(window).bind('scroll', function() {
   if ($(window).scrollTop() >= 1110) {
      $( "#parte3" ).delay( 500 ).fadeIn( 500 );
   } ...

Comment: excelente resposta, estou procurando algo desse gênero, tem como? http://tympanus.net/Development/GridLoadingEffects/index.html
Comment: @FelipeStoker eu acho que até ja vi alguem pedindo este mesmo efeito aqui, vou ver se acho o que era.
Comment: Não era bem isso que eu gostaria, e sim algo parecido com o plugin que postei em minha pergunta, gostaria que as `` fossem aparecendo com o scroll do mouse.

Fazer animate com scrollTop


How to add html checkbox on Datable without adding additional columns

Question: I have my data as json key value pairs in an array.
$(document).ready(function () {
        var table = $("#main").DataTable({
            columns: [{
                    "data": "ProductNumber",
                    "title": "ProductNumber"
                },
                {
                    "data": "ReceivingDate",
                    "title": "ReceivingDate"
                }]

I want to add checkboxes so that users can select some rows.
columnDefs: [{
                targets: 0,
                searchable: false,
                orderable: false,
                className: 'dt-body-center',
                render: function (data, type, full, meta) {
                    return '<input type="checkbox" name="id[]" value="' +
                        $('
').text(data).html() + '">';
                }
            }]

I found the columnDef example from the DataTable website. The issue is, it is told to render this checkbox on column 0 which replaces my productnumber column. And I can't add an empty column as my json data has the exact number of data as the columns have. Are there any other ways I can achieve this?
Answer: You can insert the checkbox into an existing cell like Id Columns.


Let p>=3 be a prime number. Show that whatever the distict numbers x_1,x_2,...,x_(p-1) beloning to the set {1,2,...,p-1}, there exists a pair

Question: Let $p\geq3$ be a prime number. Show that whatever the distinct numbers $x_1, x_2, \ldots , x_{p−1}$ belonging to the set $\{1, 2, \ldots , p−1\}$, there exists a pair $(i, j)$ with $1 \leq i <\ j \leq p−1$ such that $p\vert ix_i − jx_j$.
I've observed that from Bezout's Theorem it is clear that for any number $h\in\{1, 2, \ldots, p−1\}$ there exists a $h'\in\{1, 2, \ldots , p−1\}$ such that $h'h\equiv1 \pmod{p}$.
I have also observed that if $h\in\{1, 2, \ldots , p−1\}$ then $h,2h,\ldots(p-1)h$ have all different ressidues when divided with p (all non-zero). This can be easily deduced if we assume the contrary.
I've just found this this question that uses Wilson's theorem that would also solve my problem. But I do not seem to be able to understand the way in which it is used.
Comment: Do I understand correctly that $(x_1,x_2,\dots,x_{p-1})$ is a permutation of $(1,2,\dots,p-1)$?
Comment: @MatthewLeingang Yes!
Answer: By Wilson's theorem:
$$1\cdot 2 \dotsb (p-1) \equiv \underbrace{x_1 \cdot x_2 \dotsb x_{p-1}}_{\text{any permutation of } \{1,2, \ldots ,p-1\}} \equiv -1 \pmod{p}.$$
If $\{ix_i\}_{i=1}^{p-1}$ was a permutation of $1,2, \ldots ,p-1$, then By Wilson's theorem, it should have also been equivalent to $-1 \pmod{p}$. However,
$$(1x_1)(2x_2) \dotsb ((p-1)x_{p-1}) \equiv (1\cdot 2 \dotsb (p-1))\,\,(x_1 \cdot x_2 \dotsb x_{p-1}) \equiv (-1)^2 \pmod{p}.$$
Thus, $\{ix_i\}_{i=1}^{p-1}$ is not a permutation of $1,2, \ldots, p-1$. It is clear that for all $1 \leq i \leq p-1$, $ix_i \not\equiv 0 \pmod{p}$. Thus, the number of residues mod $p$ in $\{ix_i\}_{i=1}^{p-1}$ is no more than $p-2$.


Cross-validation for survival analysis

Question: In my problem I am trying to predict the time from a vehicle leaves a charging station until the next one arrives using various techniques, of which Cox Proportional Hazard and DeepSurv are some of them. When training a model I do the following:

Decide upon a set of hyperparameters for tuning.
Split data in training and test data.
Loop through every combination of hyperparameters.

Perform K-fold cross-validation on the training set.
Choose the hyperparameter combination with the best results.

Train a model with the best-found parameters on all training data.
Evaluate the test set with the model from the previous point.

In 3.2. I am however uncertain about how to evaluate the model. A classic approach would be to choose to use the C-index. From my problem perspective, this might not be so useful though, as the C-index is the model's ability to correctly provide a reliable ranking of the survival times (taken from pysurvival). What is more interesting is the actual predictions which for example can be shown by the RMSE which by taking the AUC of the survival function can be estimated.
Is this the right way to think about it or am I on the wrong path?
Comment: However, survival analysis seems to be completely the wrong approach to predict time between arrival times. Could you clarify what you think drives arrival times... eg is there no queuing
Comment: So am not too familiar with cox, but would suggest against it (as its aim is to infer effects, ignoring the baseline survival).  Can you discretise the time? in that case you could just use a logistic regression.
Comment: I'm not so pessimistic as @seanv507 about using survival analysis, as one might model this as a set of recurring events.See the main and multi-state vignettes of the R [survival package](https://cran.r-project.org/web/packages/survival/index.html). Don't know if that's how you're modeling, how you are incorporating covariates into the model, etc. Not clear what "discrimination" as measured by C-index would represent in that case. For your application a measure of calibration is probably more important. A parametric model, maybe Poisson based, would probably be more useful than Cox.
Comment: @seanv507 so my question is more of a research question to also test how and if survival analysis can model the problem. I do not see how a logistic regression would be beneficial as I am trying to predict the time (so for example minutes) between a vehicle leaving and another arriving. It is a very hard problem with few features due to no knowledge about users and as the stations obviously are stationary. At the moment my features are time and knowledge about previous behaviour (not exactly time series alike, but similar).
Comment: @EdM what do you excatly mean with *a measure of calibration*? And why do you think a Poisson based model would be more beneficial?
Comment: so I realised that survival analysis sounds fine given the basic idea that interarrival time is exponentially distributed.
Comment: You can use logistic regression (or xgboost, RF etc) to build a discrete time survival model. (eg predicting each 5 minute interval | no arrival in all previous 5 minute intervals).You chain the survival  predictions together to get probabilities for eg the next 30 minutes.  your crossvalidation can just use log loss (for each interval). https://bookdown.org/content/4253/fitting-basic-discrete-time-hazard-models.html#fitting-a-discrete-time-hazard-model-to-data
Comment: My criticism of the cox model is that given you are finding the best predictor, you are unlikely to want the baseline hazard function assumed by cox model.
Comment: @andKaae there is a form of survival analysis that is more general than Cox proportional hazards model termed "discrete time survival analysis" and it may use logistic (or probit, or complementary log-log) regression. See Allison, P. D. (1982). Discrete-time methods for the analysis of event histories. *Sociological Methodology*, 13(1982), 61–98 or Singer, J. D., & Willett, J. B. (1993). It's about time: Using discrete-time survival analysis to study duration and the timing of events. *Journal of Educational and Behavioral Statistics*, 18(2), 155–195.
Answer: To answer the title of the question, as survival models are fit by maximizing likelihood (partial likelihood for a Cox model), that's the best cross-validation measure for survival analysis: the (partial) likelihood of the held-out data given the parameter estimates. Even if the C-index is appropriate in your application, Frank Harrell (who originated the C-index for survival models) doesn't find it adequately sensitive to distinguish between competing models. See this thread, for example. I don't use Python so I can't help with implementation directly; packages for survival analysis in Python should show the way.
For evaluating the final model, what can be most useful is whether its event-time probability predictions are close to those observed. That's what I meant by "calibration" in a comment. For example, order the events in your test set in terms of the model-predicted probability of having an "event" before a specified time, group the events into bins accordingly, and plot the observed proportion of events before that time within each bin against the predicted probability. Repeat for a set of times of interest. There are ways to do this more smoothly, without binning, for standard single-event data but I haven't thought through whether they would apply here. If the model is well calibrated, any other measure of interest (e.g., RMSE) should be as good as you are going to get, so evaluate the probability calibration first.
Is survival modeling appropriate here?
Discussion in comments had more to do with the last sentence of the question:

Is this the right way to think about it or am I on the wrong path?

That led to multiple comments about whether and how survival analysis is appropriate for this application. The terminology can hide a lot of commonalities. For example: there are close relations between survival models of the type appropriate here and other models of point processes (e.g., Poisson models) or multi-state processes; discrete-time survival analysis is essentially a set of logistic regressions; Cox models and fully parametric models have many similar strengths and weaknesses. So the issue is which form of multi-state/repeated event analysis works best here.
There are two general issues to consider. First, how are you defining time = 0 for survival analysis? Should that be the initial installation time of the charging station? The time you started data collection? Should time reset to 0 for a particular station each time it becomes available? Second, you can't assume that all "events" are independent; within individual stations, events and their associations with covariates are likely to be correlated. Survival software referenced below handles that; if you are writing your own, make sure that you do, too.
Beyond those general considerations, the question is what type of survival model to use. This might be structured simply as a repeated-event survival model, but you need to apply your knowledge of the subject matter to decide. A recurrent-event survival model assumes "a single event type, with the possibility of multiple events per subject" (Section 2.2 of the R survival vignette). An implicit assumption in a simple repeating-event model is that the "event" is effectively instantaneous. Does that make sense here? Is the duration of occupancy of a charging station that much shorter than the time between vehicle arrivals?
You thus might need to move at least to a two-state model, modeling transitions both from occupied to unoccupied and vice-versa (perhaps modeling things like "out-of-order" states, too). The R multi-state vignette provides a useful introduction.
Another question is whether to use a Cox-type semi-parametric model or a fully parametric model. Cox models have the advantage/disadvantage of not specifying a parametric form for the baseline hazard. They can be used for predictions, provided that the baseline hazard estimated from the model* is representative of the population of interest. You do need to document that the proportional hazards assumption holds adequately.
Fully parametric models have advantages in terms of providing equations for describing the model and making predictions. They aren't necessarily better, however.
With a parametric model, you must document that the chosen parametric family models the data adequately, like you have to document proportionality of hazards in a Cox model. For generalization, you must assume that the chosen form will also apply to new samples of the population of interest, like you have to assume that the empirical baseline hazard of a Cox model will adequately represent new samples. Assumptions relating to proper model specification (e.g., linearity in predictors) ultimately are the same with standard regression models of either type, or, for that matter, with discrete-time models.
The R flexsurv package can handle multi-state parametric models. Its multi-state modeling vignette is a good resource for helping to think through how to formulate a multi-state model even if you aren't going to use the package itself and will use boosted trees or neural nets instead.

*A Cox model does not assume a particular baseline hazard; it just assumes that covariate-associated differences in hazard are proportional, whatever the baseline hazard function might be. If that assumption holds, then the Cox regression-coefficient estimates plus the original data on covariate values and survival directly provide an empirical baseline hazard.
Comment: My point was that if you are trying to find the best time estimate you want to explicitly fit/smooth the baseline hazard

Cross-validation for survival analysis


After enabling the "experimentalInteractiveRunEvents" flag to run the after:spec event in Cypress IDE : after:spec event is not calling

Question: I have enabled the "experimentalInteractiveRunEvents": true in cypress.json to run the after:spec event of cypress-testRail-simple plugin through cypress IDE.
link for plugin : https://github.com/bahmutov/cypress-testrail-simple.git
I am using this event in cypress-testRail-simple's plugin.js file.

I have defined plugin in plugins -> index.js

enabled the flag in cypress.json file:

I have multiple spec files.
When running through command line :
npx cypress run
after:spec - getting called after all the spec run finish.
When running through cypress IDE :
npx cypress open
after:spec - called zero time
reference : how to run the cypress in interactive mode with flag enabled
Comment: Please add the code you tried to your question.
Comment: Added Please check!
Answer: There's a  fundamental difference between npx cypress open (interactive mode) and npx cypress run (command line mode).
Interactive mode
All the specs are run as in a single continuous run, as if they had been written as one spec. Because of this, before:spec is called only one.
Command line mode
All specs are run as separate runs, hence two calls to before:spec.

From After Spec API

When running cypress via cypress open, the event will fire when the browser closes.
Comment: I am using cypress-testrail-simple plugin which is using after:spec event that is the mandatory!
Comment: Yeah, that's also the case with Mochawesome reporter plugin.


A tag HTML tem um atributo ou valor de atributo não permitido

Question: A tag HTML tem um atributo ou valor de atributo não permitido.
O problema persiste nesse trecho do código:
<form class="wpcf7-form" action="/wp-admin/post.php?post=4648&
action=edit&message=1#wpcf7-f2766-o1" method="post" novalidate="novalidate">

Answer: Tem uma "quebra de linha" na sua url da action=""!

Veja o que fala no validador W3C

Se vc corrigir esse url vc vai ver que não vai apresentar Erro na validação, pelo menos no validador da W3C não da erro! https://validator.w3.org/nu/#textarea

Tenha cuidado para colocar tudo concatenado, sem deixar quebrar a linha
<form class="wpcf7-form" action="/wp-admin/post.php?post=4648&action=edit&message=1#wpcf7-f2766-o1" method="post" novalidate="novalidate">
</form>


Hardware list for wardriving(pen testing) with an rpi2 and laptop

Question: so basically I've been online trying to research this the whole day and I seem to only be able to come across specific setups that people have for their own specific needs rather than a generic list of hardware needed.
What I want to do firstly using my raspberry pi 2 running raspbian, and secondly a laptop running kali, is to be able to do penetration testing along with some extras.
What I Am looking for is a list of hardware that I need (other than the rpi2 for the first case and laptop for the second) that will enable me to sniff out WiFi signals,  and attempt to get onto the network. I believe the general name for this is wardriving.
I know that I need a portable power supply for the rpi2, and a screen or some sort (I want a small screen that I can see the rpi gui desktop from. Not just terminal), so any suggestions of examples of those would be appreciated.
Where I get confused is about the WiFi antenna that I need.  From what I understand is that it needs to be one that can monitor as well as connect to a WiFi,  but I don't really know of any examples or of what the actual difference is between it and a normal usb WiFi stick.
I'm also not sure what else I need to have beyond that to successfully accomplish my stated goal.
Any further help would be greatly appreciated,  and I think beneficial to anyone else who's looking to get started doing the same thing.
Any extra information would be good too   what I mean is when I was doing my research I saw some people mentioning radio attachments,  gps attachments, etc. But I'm not really sure if they're necessary to start or things that can be added further down the road with experience.
Thanks.
Answer: Ok so I seem to have found a good article that answers at least the general part of my question.  It can be found here.
http://lewiscomputerhowto.blogspot.ca/2014/06/how-to-hack-wpawpa2-wi-fi-with-kali.html?m=1
It also gives tips on the process of pen testing.
Comment: I mean,  it answers my question about the wireless card,  and antenna kind of.  So that being relative to pen testing with a laptop.  Still doesn't answer any of my questions regarding doing it with my rpi2.


New Azure Resource group powershell

Question: I have installed Azure powershell 1.0.0 released 3 days ago. and trying to create a new azure resource group using this command
New-AzureRMResourceGroup
Before this when I run Login-AzureRMAccount and supply it with my subscription ID(as I am linked with more than one subscription).
Login-AzureRMAccount -SubscriptionID ""

This strangely tells me , my email id does not have rights to this subscription. It's weird because I can login to this subscription alright and can do things through portal.
When I try to do this using Set-AzureSubscription Select-AzureSubscription and then create resource group. New Resource group is always created in my first subscription... Not sure what's going on.
Answer: I had the same issue and following this thread led me to provide the -tenantID parameter in addition to the -subscriptionID of the desired subscription to the Login-AzureRMAccount cmdlet, and got around the issue.
New items began to appear under the account I was expecting.

New Azure Resource group powershell


I still forget concepts even after answering numerous math problems

Question: Note: this is particularly aimed at high-school/entry level college problems
When I'm learning a new topic:
1) I read the theory given in the textbook at the start of each topic
2) proceed to read the solved example problems which the textbook provides (usually 3-5 with full solutions)
3) I then proceed onto answering every question within each exercise.
My problem is that I still forget concepts, for instance, let's say a week later (or even a few days later sometimes).
What am I doing wrong?
Comment: Nothing. When you are learing a new language, do you immediately know **and remember** how to use its grammar right after few examples and some practice? No, you (usually) get it after making fourty mistakes, then you forget it again, then you recall it again, and afterwards hopefully you recall it for as long as you practice. Same with Math.
Comment: @Studentmath your comment should be an answer, really.
Answer: Usually the things that we remember are those things that we both understand completely and have found useful.
Understanding requires an intuitive grasp of why in the first place you want to consider certain mathematical objects, why a theorem should be true, and how intermediate objects were conceived. If you don't understand why someone constructed some strange object in the middle of a proof, it's impossible to have a full grasp of the underlying structure. You can know whether you fully understand a topic if you can re-derive all the theorems on your own without knowing how it was done in the textbook. Typically that means that you'll have to try to prove each theorem yourself before reading the given proof. If you get stuck, you can glance quickly through the given proof to find roughly where you have reached, and then read one or two more lines as a kind of hint so that you can try to continue by yourself. At the same time you should try to figure out what aspect of the problem you had missed in being unable to proceed. This may not be possible with some textbook proofs that pull intermediate objects out from thin air without explanation of how they got it, in which case you should ask someone familiar with the topic.
Secondly you need to find out what it is that what you're learning is useful for. If you cannot apply it to the real world, you can still try to extract the core structure and perhaps that might appear in other more applicable areas. If you can't use what you learn, it's quite natural that you'll forget it sooner or later, even if you practise numerous times using exercises in the book.
Answer: You don't say anything about writing notes or summaries. For each type of problem, while it is still fresh in your mind, write a detailed reminder to yourself on how to do it -- maybe a particularly good example. Include key definitions, theorems with examples and non-examples. Keep these summary notes separate from your lecture notes so you can quickly review them.


Distinction between json decoding and objects in php

Question: If I send the message
curl -i -H "Content-Type: application/json" -X POST \
     -d '{"name": "Bob", "age": 32}' \
     http://localhost:8000/user.php

while running php -S localhost:8000 on the directory containing user.php:
<?php
    class User {
        public $name, $age;
        function __construct($name, $age) {
            $this->name = $name;
            $this->age = $age;
        }
    }

    $user1 = json_decode(file_get_contents('php://input'), true);
    $user2 = new User("Bob", 32);
?>

Is there any distinction between $user1 and $user2? If yes, how can I decode the message directly into an instance of class User? If no, does this mean that class User is altogether redundant in the sample above?
Docs:

json_decode
file_get_contents
Answer: First off, to get an object out of json_decode, you need to pass false as the second argument, otherwise the result (what you're currently getting) is an associative array. Note false is the default for the second argument, so you can simply omit it here.
Assuming you don't pass true for the second argument to json_decode, $user1 in your example is an instance of stdClass, whereas $user2 is an instance of User, so the instances are of different classes.
If you want to make the input into a User instance you could do
$rawUser = json_decode(file_get_contents('php://input'));
$user1   = new User($rawUser->name, $rawUser->age);

Another way might be a static factory method
class User {
    public $name, $age;
    function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }

   static public function fromRaw(stdClass $rawUser) {
       $newUser = new self();
       $newUser->name = $rawUser->name;
       $newUser->age  = $rawUser->age;

       return $newUser;
   }
}

then you could revise the first example to
$rawUser = json_decode(file_get_contents('php://input'));
$user1   = User::fromRaw($rawUser);

Comment: @Calif the problem is JSON is not a native format to php. If you use [serialization](http://php.net/manual/en/function.serialize.php) instead, you can pass a instance of a class as a string, then when you call [`unserialize`](http://php.net/manual/en/function.unserialize.php) you'll have the instance immediately available without the intermediate steps from the JSON approach.
Comment: Instead of `new User();`, you shouls use `new self();`. Try it here: http://sandbox.onlinephpfunctions.com/code/9ee4edf53a46357dc5ba446febd220f0199dd2f8
Comment: @quickshiftin  Sounds good, with almost perfect clarity! But one point remains: It seems wasteful to get `json_decode` to first construct an instance of `stdClass`, only to then keep it for just an instant before constructing the object we really want. Yet short of parsing the string returned by file_get_contents myself, that's just good enough. Could you opine?


Element of, subset of and empty sets

Question: I am trying to make sense of these. To me a is false because the set isn't empty. Is that correct?
b is true because the empty set is an element of that set.
c is false because the set the empty set isn't an element, it is a subset or proper subset.
d is true because the set the empty set is a member of that set.
e the set the empty set is false because it is not a proper subset I think
f false for the same reason
g false for the same

a) ∅ ∈ {∅}
b) ∅ ∈ {∅, {∅}}
c) {∅} ∈ {∅}
d) {∅} ∈ {{∅}}
e) {∅} ⊂ {∅, {∅}}
f ) {{∅}} ⊂ {∅, {∅}}
g) {{∅}} ⊂ {{∅}, {∅}}

I know not all these are correct, why is my logic wrong?
Comment: (f) is true: the only element of $\{\{\varnothing\}\}$ is $\{\varnothing\}$, which is also an element of the righthand side. Whether (g) is true or false depends on whether you're using $\subset$ to mean *subset* or *proper subset*; in the former case it's true, and in the latter it's false, since the righthand side is equal to the lefthand side.
Comment: No, (a) is true: no matter what $a$ is, $a\in\{a\}$, because by definition $\{a\}$ is the set whose only element is $a$.
Comment: e) is also true, since $A\subset B$ means that every element of $A$ is also an element of $B$.
Comment: I don't even know how to **parse** "the set the empty set is false". What do you mean by "the set the empty set"? And how can a "set" be true or false?
Comment: @bof  {∅}  that is the set the empty set, like {1} is the set containing 1, or {{1}} is the set the set containing 1. Anyways I meant to say the statement is false.
Comment: I knew what you meant by it, but *the set the empty set* just isn't English; what you're trying to say, I think, is that $\{\varnothing\}$ is the set whose only member is the empty set. Similarly, $\{\{1\}\}$ is the set whose only member is the set whose only member is $1$.
Comment: I would say that it *is* English, but it doesn't mean what the OP intends. To me it seems analogous to "the band the Beatles". Rather clumsy English, certainly.
Answer: Don't argue too much, do it mechanically instead. If you wipe out the enclosing $\{$ and $\}$ you will see a list of the elements of the set, e.g., if $A=\{a,b,\{a\}\}$ then after removing the braces, this
$$a,b,\{a\}$$
is the list of elements of $A$, so
$$a\in A,\quad b\in A\quad \{a\}\in A.$$
You'll get any subset of $A$ by first removing the braces to get the element-list, then choose some elements and put braces around your chosen elements, for example
$$\{a,b\}\subset A,\quad \{a,\{a\}\}\subset A,\quad \{\{a\}\}\subset A.$$ So only (c) is wrong.  For example, (f) is true: get the element list of $M=\{\emptyset,\{\emptyset\}\}$ by removing braces:
$$\emptyset, \{\emptyset\},$$
chose the element $\emptyset$, (thus $\emptyset\in M$), put braces around your chosen element to get $\{\emptyset\}$, to yield a subset of $M$, that is $\{\emptyset\}\subset M$.
Comment: There is not an iota mention of the phrase 'proper-subset' in your answer.
Comment: Not only c, g is wrong too. A set can't be proper set of itself.

Element of, subset of and empty sets


How to remove the shipping flat rate for particular products in Drupal commerce kickstart?

Question: I am using Commerce kickstart. I have to add the products for category wise. Categories are DVD,Audio CD and edownload.I have to use term reference field for field_category in the products content types.
Now the user purchase the edownload products means shipping charge should be 0rs. How to set the edownload term reference field products for shipping charge is 0. Anyone guide to me?.
Thanks in advance.
Answer: Try using these modules:

Commerce Discount Product Category
Commerce Discount


Efeito de escrita utilizando javascript, setInterval não funciona

Question: Estou tentando utilizar a função setInterval para escrever 1 letra a cada segundo, mas não funcionou:
partText = "";

function escrever(text) {
    for (i = 0; i < text.length; i++) {
        partText += (text.charAt(i));
        document.getElementById("texto").innerHTML = partText;
    }
}

setInterval(escrever("teste"), 1000);

Answer: Podes fazer assim:

function escrever(text) {
    var el = document.getElementById("texto");
    for (i = 0; i < text.length; i++) {
        setTimeout(function(letra) {
            el.innerHTML += letra;
        }, 1000 * i, text[i]);
    }
}
escrever('teste');

jsFiddle: https://jsfiddle.net/y18afv4t/
A ter em conta:

usa += para acrescentar HTML. Somente = vai substituir tudo
evita document.getElementByID dentro do for, isso é pesado para o browser. Coloca em cache como fiz no exemplo.
Answer: O Erro está em duas parte:
1 setInterval.

setInterval(escrever("teste"), 1000);

o correto é

setInterval(function(){escrever("teste")}, 1000);

2 Laço de repetição.

for (i = 0; i < text.length; i++)

Aqui ele sempre vai escrever tudo o que tem no text
Abaixo as alterações:
partText = "";
var index=-1;
function escrever(text) {

    index++;

    if(index<text.length){
      partText += (text.charAt(index));
      document.getElementById("texto").innerHTML = partText;
    }
    else
    {
      clearInterval(timer);
    }

  }

var timer= setInterval(function(){escrever("teste")}, 1000);

Exemplo|jsfidlle
Comment: Acho desnecessário ter uma variável global só para guardar o index do `.charAt` Nesse caso era bom ter uma variável para `document.getElementById("texto")`, assim evitando estar sempre a ir ao DOM buscá-la. Junta também `var` a `partText` para não exportar para o espaço global.
Answer: O setInterval pode não ser uma boa, porque ele repete indefinidamente (até que seja determinado que ele pare com clearInterval).
O que recomendo é que utilize o setTimeout que executa apenas uma vez.
Com isso, seu código ficaria mais ou menos assim:
var partText = "";
function escreverNoObjeto (objeto, texto) {
    objeto.innerHTML = texto;
}

function escrever(text, intervalo) {
    var partText = '';
    var fn = function(texto) {
        return function() {
            /* pode ser document.getElementById("texto")*/
            escreverNoObjeto(document.body, texto);
        };
    };

    for (i = 0; i < text.length; i++) {
        partText += text.charAt(i);

        setTimeout(fn(partText), intervalo * (i + 1));
    }
}

escrever("teste", 1000);

Você ainda poder ver ele funcionando no JSFiddle
Espero ter ajudado \o/
Comment: Estás a declarar `var partText = "";` duas vezes, acho que foi esquecimento não? Seria uma boa colcoar em cache o objeto do DOM fora de `fn` que é chamado n vezes.
Answer: Os parametros não são passados assim como está a fazer:
Tente:
setInterval(escrever, 1000, 'test');

Exemplo no jsfiddle.
Não sei se é isso que quer mas assim a impressão de cada letra é instantanea, apenas na str toda é que se nota.
Para fazer letra a letra:
...

...

var testStr = 'Vamos lá escrever isto';
function escrever(i) {
    partText = testStr.charAt(i);
    document.getElementById("texto").innerHTML += partText;
    i += 1;
    if(i < testStr.length) {
        setTimeout(escrever, 400, i); // timing entre cada impressão aqui
    }
}

index_da_letra = 0;
escrever(index_da_letra);

Exemplo no jsfiddle:
Comment: Essa função `escrever` é chamada eternamente... quando é que ela pára?


Show that $B \setminus C$ is connected.

Question: Let A and B be connected sets and $ A\subset B$. If C is a component of $B \setminus A$, prove that $B \setminus C$ is connected.
I have tried to solve this problem but I am really having a hard time proving it.
I hope someone can help me. Thanks a lot.
Answer: If $A = B$ then $C = \emptyset$ and $B \setminus C = B$ is connected. If $A = \emptyset$, then $C = B$ and $B \setminus C = \emptyset$ is connected. Assume that $A$ is a proper subset of $B$.
Let $C_i$ denote the connected components of $B \setminus A$ with $i \in I$ in some index set $I$. By definition, each connected component $C_i$ is a connected subset of $B \setminus A$ which is maximal in the inclusion order. Therefore, each $C_i$ is closed in $B \setminus A$.
Fix $k$ and assume $B \setminus C_k$ is not connected, i.e.
$$
B \setminus C_k = D_1 \cup D_2
$$
for $D_1$ and $D_2$ disjoint non-empty sets that are open in $B \setminus C_k$. We will derive a contradiction by showing that $B$ is not connected.
First, note that every $X \subset B \setminus C_k$ can be written as
$$
X = (X \cap D_1) \cup (X \cap D_2)
$$
where $X \cap D_i$ is open in $X$ for $i = 1, 2$. Therefore, every connected subset $X \subset B \setminus C_k$ is either contained in $D_1$ or in $D_2$. In particular, $A$ is contained in $D_1$ or in $D_2$ and for each $i \in I, i \ne k$, $C_i$ is contained in $D_1$ or in $D_2$.
Assume, without loss of generality, that $A \subset D_1$. Define
$$
E_1 = D_1 \cup C_k \\
E_2 = D_2
$$
and note that $E_1$ and $E_2$ are disjoint non-empty sets and $E_1 \cup E_2 = B$. We will show that both are open in $B$ by demonstrating that $E_2$ is clopen in $B$.
Recall that $E_2$ is open in $B \setminus C_k$, so there is an open set $G$ in $B$ such that $E_2 = G \cap (B \setminus C_k)$. Let $X'$ denote the closure of $X$ in $B$. Noting that $C_k' \subset C_k \cup A$ and $G \cap A = \emptyset$, we can write $E_2$ as the intersection of two sets open in $B$
$$
E_2 = G \cap (B \setminus C_k')
$$
so $E_2$ is open in $B$.
Similarly, recall that $E_2$ is closed in $B \setminus C_k$ so there is a closed set $F$ in $B$ such that $E_2 = F \cap (B \setminus C_k)$. Noting that $E_2' \subset E_2 \cup A$ and $F \cap A = \emptyset$, we can write $E_2$ as the intersection of two sets closed in $B$
$$
E_2 = F \cap E_2'
$$
so $E_2$ is closed in $B$.
Thus, $E_2$ is both open and closed in $B$ and consequently $B = E_1 \cup E_2$ is the union of two disjoint non-empty open sets contradicting the fact that $B$ is connected. Therefore, the assumption  that $B \setminus C_k$ can be written as the union of two disjoint non-empty open sets is false and thus $B \setminus C_k$ is connected.
Comment: $B \setminus C_i $ is connected. It contains $A$.
Comment: @PaulFrost Thank you for pointing out the error! I've rewritten the answer.
Comment: Nice proof! Why did you introduce $E_1,E_2$? Wouldn't it be sufficient to show that $D_2$ (which is $\ne B, \emptyset$) is open and closed in $B$?
Comment: Why is $E'_2 \subset E_2 \cup A$? It is true if and only if $E_2$ is closed in $B \setminus A$. But we only know that it is closed in $B \setminus C_k$.

Show that $B \setminus C$ is connected.


wss fails over https on apache server

Question: I have a server in digital ocean which is using StartCOM class I primary intermediate CA for ssl. I have set u a websocket server and I want to make a connection to  it from a page which is served by https.
When I try to connect to the websocket using just http it works fine. But when I try to use it over https by changing the websocket uri from ws to wss it does not connect.
What am I doing wrong. Connection is made using fancywebsockets.js
fancywebsockets.js
var FancyWebSocket = function(url)
{
    var callbacks = {};
    var ws_url = url;
    var conn;

    this.bind = function(event_name, callback){
        callbacks[event_name] = callbacks[event_name] || [];
        callbacks[event_name].push(callback);
        return this;// chainable
    };

    this.send = function(event_name, event_data){
        this.conn.send( event_data );
        return this;
    };

    this.connect = function() {
        if ( typeof(MozWebSocket) == 'function' )
            this.conn = new MozWebSocket(url);
        else
            this.conn = new WebSocket(url);

        // dispatch to the right handlers
        this.conn.onmessage = function(evt){
            dispatch('message', evt.data);
        };

        this.conn.onclose = function(){dispatch('close',null)}
        this.conn.onopen = function(){dispatch('open',null)}
    };

    this.disconnect = function() {
        this.conn.close();
    };

    var dispatch = function(event_name, message){
        var chain = callbacks[event_name];
        if(typeof chain == 'undefined') return; // no callbacks for this event
        for(var i = 0; i < chain.length; i++){
            chain[i]( message )
        }
    }
};

Client Side JS
var Server;

        function log( text ) {
        console.log(text)
    }

        function send( text ) {
            Server.send( 'message', text );
        }

        $(document).ready(function() {
            log('Connecting...');
            Server = new FancyWebSocket('ws://www.myserver.com:9400');

            $('#message').keypress(function(e) {
                if ( e.keyCode == 13 && this.value ) {
                    log( 'You: ' + this.value );
                    send( this.value );

                }
            });

            //Let the user know we're connected
            Server.bind('open', function() {
                log( "Connected." );
            });

            //OH NOES! Disconnection occurred.
            Server.bind('close', function( data ) {
                log( "Disconnected." );
            });

            //Log any messages sent from server
            Server.bind('message', function( payload ) {
                log( payload );
            });

            Server.connect();
        });

Server.php
<?php
// prevent the server from timing out
set_time_limit(0);

// include the web sockets server script (the server is started at the far bottom of this file)
require 'class.PHPWebSocket.php';

// when a client sends data to the server
function wsOnMessage($clientID, $message, $messageLength, $binary) {
    global $Server;
    $ip = long2ip( $Server->wsClients[$clientID][6] );

    // check if message length is 0
    if ($messageLength == 0) {
        $Server->wsClose($clientID);
        return;
    }

    //The speaker is the only person in the room. Don't let them feel lonely.
    if ( sizeof($Server->wsClients) == 1 )
        $Server->wsSend($clientID, "There isn't anyone else in the room, but I'll still listen to you. --Your Trusty Server");
    else
        //Send the message to everyone but the person who said it
        foreach ( $Server->wsClients as $id => $client )
            if ( $id != $clientID )
                $Server->wsSend($id, "Visitor $clientID ($ip) said \"$message\"");
}

// when a client connects
function wsOnOpen($clientID)
{
    global $Server;
    $ip = long2ip( $Server->wsClients[$clientID][6] );

    $Server->log( "$ip ($clientID) has connected." );

    //Send a join notice to everyone but the person who joined
    foreach ( $Server->wsClients as $id => $client )
        if ( $id != $clientID )
            $Server->wsSend($id, "Visitor $clientID ($ip) has joined the room.");
}

// when a client closes or lost connection
function wsOnClose($clientID, $status) {
    global $Server;
    $ip = long2ip( $Server->wsClients[$clientID][6] );

    $Server->log( "$ip ($clientID) has disconnected." );

    //Send a user left notice to everyone in the room
    foreach ( $Server->wsClients as $id => $client )
        $Server->wsSend($id, "Visitor $clientID ($ip) has left the room.");
}

// start the server
$Server = new PHPWebSocket();
$Server->bind('message', 'wsOnMessage');
$Server->bind('open', 'wsOnOpen');
$Server->bind('close', 'wsOnClose');
// for other computers to connect, you will probably need to change this to your LAN IP or external IP,
// alternatively use: gethostbyaddr(gethostbyname($_SERVER['SERVER_NAME']))
$Server->wsStartServer('my.ip.add.ress', 9400);

?>

Server Screen Shot

It keeps on getting disconnected
Comment: Can you please provide your code?
Comment: Have you ever been able to resolve this? I am using the exact same code and changing my site to HTTPS as well.
Comment: @GillesLesire I moved to firebase which is serving my need for a realtime chat application. But I think this problem comes when you use a free SSL or when your SSL is invalid or expired.
Comment: I have a legit SSL so that is not the issue. Maybe I have to allow SSL on that specific port through my SSL config or something, I'll have to keep looking. Thanks for your response though.
Comment: Forwarding a secured port to an unsecured port using stunnels fixed the job. The only problem is that you can no longer retrieve the host IP address. It's all 127.0.0.1
Answer: I ran into the same issue (I even use the same PHP script) and manage to resolve this using stunnel. With a lot of trial-and-error from all kind of different, contradicting sources I managed to get it running. However you won't get client ip addresses anymore, you'll have to send it to the server manually if you need it. I read somewhere that you could forward the client IP using proxy but haven't yet been able to set that up myself.
Install stunnel on server
Login to your server through ssh make sure your system is fully up to date.
apt-get update
apt-get upgrade

Install the stunnel package
apt-get install stunnel4 -y

Configure stunnel
Create a config file for stunnel by running the following command
nano /etc/stunnel/stunnel.conf

Write the following into the file
foreground = yes
key = /path/to/your/ssl/cert.key
cert =  /path/to/your/ssl/cert.pem
CAfile = /path/to/your/ssl/cert.pem
debug = 7
output = /var/log/stunnel_websocket.log

[websocket]
accept = myserver.com:9401
connect = 9400

Personally I just used the same certificate for the stunnel as I did for the HTTPS setup.
Run the stunnel
You have to keep the stunnel running like you have to keep the websocket running.
Do this with the command:
/etc/init.d/stunnel4 restart

Alter your PHP script
Change the PHP file server.php
$Server->wsStartServer('my.ip.add.ress', 9400);

to
$Server->wsStartServer('localhost', 9400);

Alter the client side JS script
Server = new FancyWebSocket('ws://www.myserver.com:9400');

should be changed to
Server = new FancyWebSocket('wss://www.myserver.com:9401');

So if you are running the stunnel and the websocket you should now be able to send and receive data over the socket again with the only difference that all clients now appear to be coming from 127.0.0.1 according to the websocket script.

wss fails over https on apache server


Why is the variance of my random effect negative?

Question: I have two variables, response and group, and I fitted the model in which group is a random effect.
proc mixed data=myData nobound;
   class group;
   model response =;
   random group;
run;

However, SAS returns a negative variance for the random effect...
I have noticed that, unexpectedly, the variability of response within group may be larger than the overall variability of response. I guess this is the reason why SAS returns a negative variance. Is it your opinion too? If so, how could this be seen from a mathematical point of view?
DATA
group   response
1   70.8
1   64.2
1   67.8
1   62
1   60
1   66.6
1   67.8
1   60
1   70
1   63.3
1   37
1   53.8
2   82.7
2   65.5
2   67.7
2   51.6
2   53.8
2   64.3
2   72.4
2   48.3
2   54.8
2   64.8
3   88.5
3   69
3   60.9
3   71.4
3   52
3   58.3
3   63.6
3   51.9
3   52.4
3   52.2
3   65.4

Answer: When you use ANOVA or MINQUE method of variance components estimation the estimate for the variance of a random factor may indeed occasionally be negative. There may be several causes, and it is not easy to detect the actual one. See Hocking R. R. (1985) The analysis of linear models. For example outliers and small sample size may be guilty. Try to delete a pair of extreme values and/or enlarge sample and see what you get.
Addition. Some of possible reasons:

Variation of obsevations may be too large for the sample size. Larger
sample is needed.
Outliers
The true variance of the random factor is 0
Groups in the random or fixed factor is too unbalanced (unequal sized)
Variance components is not a right model for covariance structure for the data
Comment: Ok, thx !
Now, can one see that these reasons do lead to negative variance from a mathematical point of view?
Comment: Thx for the answer. Unfortunately, I do not have that book. Would it be possible to summarise the reasons mentioned there? Or would you have another reference?
Comment: I don't have the book at hand either. I've made some extractions though, see added to the response.
Answer: When you delete nobound from the code of SAS the variance of group=0.
Also when you use the following code you can get the same results;
PROC VARCOMP METHOD=REML;
CLASSES  group;
MODEL response= group/FIXED=0;
run;
I think these results confirm that the variance of group =0 despite some methods gave negative value such as Mivque.
Answer: When you doubled your data and perform the analysis you will get also negative value but less. For each increasing in data size the negative variance decrease. The second factor is the differences among three means of groups was close. These two factors are responsible about the negative value.


What is a good algorithm for estimating the median of a huge read-once data set?

Question: I'm looking for a good algorithm (meaning minimal computation, minimal storage requirements) to estimate the median of a data set that is too large to store, such that each value can only be read once (unless you explicitly store that value). There are no bounds on the data that can be assumed.
Approximations are fine, as long as the accuracy is known.
Any pointers?
Comment: Perhaps, asking on Stackoverflow may get better answers.
Comment: @Srikant:> it's a pretty active area of research in statistics :) The solution closest to the lower theoretical bounds in terms of storage involve some pretty clever probability constructs as well. All in all i was surprised when i first looked unto it a couple of month ago; there is more stats here than meets the eye.
Comment: You can try to find a median based on grouped frequency distribution, [here is some details](http://www.statcan.gc.ca/edu/power-pouvoir/ch11/median-mediane/5214872-eng.htm)
Answer: How about something like a binning procedure?  Assume (for illustration purposes) that you know that the values are between 1 and 1 million.  Set up N bins, of size S. So if S=10000, you'd have 100 bins, corresponding to values [1:10000, 10001:20000, ... , 990001:1000000]
Then, step through the values. Instead of storing each value, just increment the counter in the appropriate bin. Using the midpoint of each bin as an estimate, you can make a reasonable approximation of the median. You can scale this to as fine or coarse of a resolution as you want by changing the size of the bins. You're limited only by how much memory you have.
Since you don't know how big your values may get, just pick a bin size large enough that you aren't likely to run out of memory, using some quick back-of-the-envelope calculations. You might also store the bins sparsely, such that you only add a bin if it contains a value.
Edit:
The link ryfm provides gives an example of doing this, with the additional step of using the cumulative percentages to more accurately estimate the point within the median bin, instead of just using midpoints. This is a nice improvement.
Comment: The problem with the binning approach is that we do not have a good upper bound for the data, and so the midpoint for the largest bin would have to be huge. So, we'd need a huge number of bins (not enough memory for that), or have pretty wide bins (which would then lead to a fairly inaccurate answer.)

And the data is not very sparse.
Comment: Since you are only interested in the median why couldn't you make the bins wider at higher values of your variable?
Comment: drknexus - because we don't know what the largest bin should be.
Comment: Do you have **any** intuition as to what the range will be?  If you're fairly sure that over half of the answers will be below number N, then you can make your last bin as large as you want.  Maybe your last bin is all numbers greater than 1 trillion - would that be high enough?  With the amount of memory in modern systems you can store a LOT of bins and achieve fairly high resolution. In terms of data structures, we're not talking anything fancy and memory intensive here.
Comment: Any intuition? yes. And your approach could work in general. However, in this case we can not have a lot of memory/computation. It is in a networking application where the device could see tens of thousands of items per second, and have VERY little processing left over for this purpose. Not the ideal/typical scenario, I know, but that is what makes it interesting!
Answer: I re-direct you to my answer to a similar question. In a nutshell, it's a read once, 'on the fly' algorithm with $O(n)$ worst case complexity to compute the (exact) median.
Answer: I implemented the P-Square Algorithm for Dynamic Calculation of Quantiles and Histograms without Storing Observations in a neat Python module I wrote called LiveStats. It should solve your problem quite effectively.
Comment: (+1) Thanks for stopping by and providing that link, Sean!
Answer: The Rivest-Tarjan-Selection algorithm (sometimes also called the median-of-medians algorithm) will let you compute the median element in linear-time without any sorting. For large data sets this is can be quite a bit faster than log-linear sorting. However, it won't solve your memory storage problem.
Answer: Could you group the data set into much smaller data sets (say 100 or 1000 or 10,000 data points) If you then calculated the median of each of the groups. If you did this with enough data sets you could plot something like the average of the results of each of the smaller sets and this woul, by running enough smaller data sets converge to an 'average' solution.
Comment: So, according to a long lost colleague, the best apropoach seems to be Chiranjeeb Buragohain and Subhash Suri. Quantiles on Streams.
http://www.cs.ucsb.edu/~suri/psdir/ency.pdf

I also like Ian's approach, as these medians of smaller data sets will converge to a normal distribution, and so I can form conf intervals for the medians.
Comment: This is interesting, and where some statistical advice could come in! Assume in total I've got (say) 500,000 i.i.d. points and I look at groups of (say) 1,000 of them, and calculate the median of each group. Now I've got 500 medians. Is there theory that could allow me to calculate a confidence interval for the overall median based on these 500 medians?
Answer: I've never had to do this, so this is just a suggestion.
I see two (other) possibilities.
Half data

Load in half the data and sort
Next read in the remaining values and compare against the your sorted list.

If the new value is larger, discard it.
else put the value in the sorted list and removing the largest value from that list.

Sampling distribution
The other option, is to use an approximation involving the sampling distribution. If your data is Normal, then the standard error for moderate n is:
1.253 * sd / sqrt(n)
To determine the size of n that you would be happy with, I ran a quick Monte-Carlo simulation in R
n = 10000
outside.ci.uni = 0
outside.ci.nor = 0
N=1000
for(i in 1:N){
  #Theoretical median is 0
  uni = runif(n, -10, 10)
  nor  = rnorm(n, 0, 10)

  if(abs(median(uni)) > 1.96*1.253*sd(uni)/sqrt(n))
    outside.ci.uni = outside.ci.uni + 1

  if(abs(median(nor)) > 1.96*1.253*sd(nor)/sqrt(n))
    outside.ci.nor = outside.ci.nor + 1
}

outside.ci.uni/N
outside.ci.nor/N

For n=10000, 15% of the uniform median estimates were outside the CI.
Comment: The data set is potentially too big to read in half of it...it is in a networking context where the device doing the processing can see tens of thousands of items per second, and probably has enough memory to store only a few hundred.

Also the data is definitely not Gaussian. In fact it does not fit well to any of the common distributions.
Answer: The Remedian Algorithm (PDF) gives a one-pass median estimate with low storage requirements and well defined accuracy.

The remedian with base b proceeds by computing medians of groups of b observations, and then medians of these medians, until only a single estimate remains. This method merely needs k  arrays of size b (where n =  b^k)...
Comment: Link no longer good. Here is another: https://www.researchgate.net/publication/247974442_The_Remedian_A_Robust_Averaging_Method_for_Large_Data_Sets
Answer: If the values you are using are within a certain range, say 1 to 100000, you can efficiently compute the median on an extremely large number of values (say, trillions of entries), with an integer bucket (this code taken from BSD licensed ea-utils/sam-stats.cpp)
class ibucket {
public:
    int tot;
    vector<int> dat;
    ibucket(int max) {dat.resize(max+1);tot=0;}
    int size() const {return tot;};

    int operator[] (int n) const {
        assert(n < size());
        int i;
        for (i=0;i<dat.size();++i) {
            if (n < dat[i]) {
                return i;
            }
            n-=dat[i];
        }
    }

    void push(int v) {
        assert(v<dat.size());
        ++dat[v];
        ++tot;
    }
};

template <class vtype>
double quantile(const vtype &vec, double p) {
        int l = vec.size();
        if (!l) return 0;
        double t = ((double)l-1)*p;
        int it = (int) t;
        int v=vec[it];
        if (t > (double)it) {
                return (v + (t-it) * (vec[it+1] - v));
        } else {
                return v;
        }
}

Comment: Also, this can be extended to using a finite number of bins for real-time medians, etc.
Answer: Another thought is in the line of random sampling.  I had similar problem. My problem is that I have > 100 million data (each has 10 million). The computation took too long.  You can just random sample N data points from the 10 million, then find the median on those.
Comment: Yes, I've heard of random sampling before. You might be surprised to learn that in statistics, the word "efficiency" characterizes the precision of a result in terms of the sample size. In other words, the efficiency of an estimator provides an exact answer to the question "how close will I be to the median obtained from the original?"
Comment: How does the efficiency of this procedure compare to the other proposed solutions in the other answers?
Comment: Can you characterize that trade-off more precisely? How much loss in precision are you trading for the increase in speed?
Comment: It is not about efficiency, it is about lazyness. In my case, I know the median is between 1 and 151.  I got an huge array of > 2 million each.  I know by sampling just a few thousands, the median will be very close the the median obtained from the original.  This is a technique used frequently in statistics.
Comment: Some data structures are based on probability.
Comment: Agree. The bad thing about random sampling is that, if you do it 10 times, each time you got a different answer.  If you can allow for some error and trying to gain speed, then you can take this trade-off.
Comment: I am not up to date on my statistics; there are closed formula for estimation of mean from samples, but to my knowledge median does not have the nice mathematical features.  Maybe you have to it through sampling to get numeric approximations.
Answer: Here's an answer to the question asked on stackoverflow: https://stackoverflow.com/questions/1058813/on-line-iterator-algorithms-for-estimating-statistical-median-mode-skewness/2144754#2144754
The iterative update median += eta * sgn(sample - median) sounds like it could be a way to go.
Comment: but then how to choose eta, and what doe sthis then mean statistically? i.e. how to form confidence intervals for the median from this result?
Comment: @PeterR, hey, what is final solution you used?


Alice Bundle : dynamic entity loop parameter

Question: I use Hautelook AliceBundle which use Faker to generate fixtures with real world data.
In Alice, we can use parameters in our YAML file like below:
parameters:
  pwd_parameter:   anything

My\UserEntity:
  #generate 10 users with password equals to my parameter
  user_{1..10}:
    password:      '<{pwd_parameter}>'

Is it possible to use parameters to generate dynamic numbers of fixtures?
The solution I am looking for is to do this:
parameters:
  pwd_parameter:   anything
  nb_users:        10

My\UserEntity:
  #generate nb_users users with password equals to my parameter
  user_{1..nb_users}:   #<---THIS IS THE PROBLEM
      password:    '<{pwd_parameter}>

I tried:
user_{1..  nb_users   }
user_{1.. <nb_users>  }
user_{1..<{nb_users}> }
user_{1..<{nb_users}> }

which throws:

Warning: array_merge(): Argument #1 is not an array

How can I generate my number of entities dynamically?
Comment: It's configuration, you can't do that. Unless, you create bundle extension to read dynamic bundle parse by your code and put to paramaters
Answer: It is not possible to do something like that since fixtures yaml configuration files do not get merged with symfony parameters.
Nelmio\Alice\Fixtures\Fixture\RangeName is the class used to validate and parse configuration from your example.
Take a look at RangeName::canBuild() and you'll see regular expression that validates yaml key.
You could create your own Builder Method that would randomize number of fixture rows inserted.
Comment: Thanks, it put me toward the right direction. Do you know how can I add my custom builder to the existing one? I've seen the `Fixtures\Loader` class but i can't find how to add mine without writing in my vendor class
Comment: The parameters are not symfony one, it is used by alice as you can see in `password:      '<{pwd_parameter}>' `
Comment: Take a look at: https://github.com/nelmio/alice/pull/136/commits someone already created a pull request for it. It's for 1.x but might help you.


SQLServer não verifica informação corretamente em aplicação Delphi

Question: Possuo uma tela de login em Delphi + SQLServer onde o objetivo é comparar os dados enviados com os que possuem no banco!
1º Cenário:
Se eu adicionar a senha incorreta e login correto. apresenta erro!
2º Cenário:
Se eu adicionar um texto aleatório no campo de usuário e a senha correta ele funciona.
<code>procedure TFMLogin.Img_confClick(Sender: TObject);
var verif: boolean;
begin
  FMHome.ADOLogin.close;
  FMHome.ADOLogin.SQL.Clear;
  FMHome.ADOLogin.SQL.add('Select * from "login" where "usuario" = :usuario');
  FMHome.ADOLogin.Parameters.ParamByName('usuario').Value := edt_usuario.Text;
  FMHome.ADOLogin.Open;
  try
    if (Not FMHome.ADOLogin.isEmpty) and (edt_senha.Text = FMHome.ADOLogin.FieldByName('senha').AsString) then
      begin
        Modalresult := mrok;
        verif := true;
      end
    else
      begin
         application.MessageBox('Senha ou usuário incorretos!','Atenção',MB_OK+MB_ICONINFORMATION);
         edt_usuario.Clear;
         edt_senha.Clear;
         edt_usuario.SetFocus;
         verif := false;
      end;
  finally
    FMHome.ADOLogin.Close;

  end;
    if (verif = true) then
  begin
      FreeAndNil(FmLogin); //Libera o form de Login da memória
      Application.CreateForm(TFmHome, FmHome); //Cria a janela main
      Application.Run; //Roda a aplicação
  end;
end;
</code>
Acredito que esteja faltando alguma informação mas não consegui localizar.
Answer: Eu faria da seguinte forma, inclusive uso +/- dessa forma:
<code>  FMHome.ADOLogin.SQL.add('Select * from "login" where "usuario" = :usuario AND "senha" = :senha');
  FMHome.ADOLogin.Parameters.ParamByName('usuario').Value := edt_usuario.Text;
  FMHome.ADOLogin.Parameters.ParamByName('senha').Value := edt_senha.Text;
  FMHome.ADOLogin.Open;
  try
    if Not (FMHome.ADOLogin.isEmpty) then
    begin
      Modalresult := mrok;
      verif := true;
    end
    else
    begin
      Application.MessageBox('Senha ou usuário incorretos!','Atenção',MB_OK+MB_ICONINFORMATION);
      edt_usuario.Clear;
      edt_senha.Clear;
      edt_usuario.SetFocus;
      verif := False;
    end;
</code>


Which mandolin chords should I teach a young girl?

Question: I was asked, today, to teach a little girl mandolin for a couple of weeks during the summer. When I was being taught (last summer), my teacher taught me simple two-finger chords such as these:

However, it is a bluegrass program and these chords are  not conducive to the main mando playing style, "chopping."
Those chords would look more like this:

My question is which chord-type should I teach her? I want her to be better off than I was. Also, I haven't touched my mandolin for nearly a year and the atmospherically high actions renders it nigh unplayable.
Comment: How old, how big, musical, already plays? All relevant facts missing from the question. Assuming a beginner, instant success is required, thus simple chords are fine. Particularly those which will fit together in certain keys - e.g. G, C, D. Try those more advanced chords and it'll kill any enthusiasm pretty soon.
Comment: quick answer is teach the circle of fifths as it applies to the instrument
Answer: Well, I would suggest telling her both ways, and if she doesn't understand, explain it as "there's more than one way to add up to 5: 2+3=5 , 4+1=5 , etc.".
I learned the A that you learned plays A5, A, Am, and A7. So at first, I would tell her the simple ones, then eventually switch to harder ones. My father copies off another, much more experienced guitar player at my church, but I find it difficult to "copy" off the our pastor on mandolin, but he can tell when I play wrong chords, so it might not be too difficult to see if she actually uses the new chords. I would say yes to the simpler chords first though.


Dissonance: why doesn't the roughness curve have a dip for complex intervals like 7/6?

Question: Roughness is explained well in Is there a way to measure the consonance or dissonance of a chord?
In particular the Plomp-Levelt curve is derived, which has various dips showing how simple intervals (3/2, 4/3 etc) are less rough than the average.
However the curve appears to have no dips for the interval 7/6 and above.
Why is this?
Is it because the curve is empirically derived, and human perception cannot (on average) distinguish these intervals from arbitrary ones?
Or is it a limitation of the model represented by that curve?
Comment: Good point. According to that curve *any* interval between major second and octave is considered less rough than the major second, and *almost any* interval is considered more consonant than minor 3rd. That really doesn't seem right to me - is this the impact of cultural training?
Comment: I suspect part of the reason why the Plomp-Levelt curve has no dips there is because we generally consider minor thirds to be more consonant than major seconds, which are themselves more consonant than minor seconds, and so on. What's also interesting about that curve is that apparently the tritone(/augmented fourth/diminished fifth) is still considered as somewhat consonant (can someone confirm whether it's more consonant than the minor second according to that curve?).
Comment: @Dekkadeci the Plomp-Levelt model calculates less beat interference for a tritone than a minor second. I would hesitate to apply consonance directly to that result, as consonance could be made up of other factors besides beat interference. David Cope in "Computer Models of Musical Creativity" (p.229-230) also ranks the tritone as having lower tension (0.65) than the minor second (1.0, maximum) or major second (0.8).
Answer: If you mean this curve:

probably because it was only calculated using the first 6 harmonics.
Plomp & Levelt 1965:

In this way, the curves ... were computed for complex tones consisting of 6 harmonics. ... shows how the consonance of some intervals, given by simple frequency ratios, depends on frequency.

And this one:

was also only calculated with 6 harmonics.
7 harmonics would produce a notch at 7:6.
I also did a curve with 14 harmonics, and it has a notch at 7:6 and lower:

(and here's one with all audible harmonics)
Timbre and odd vs even changes the curves a lot.  Including only odd harmonics produces notches at some of the intervals on the Bohlen-Pierce scale, etc:
Comment: @RosieF It's uncommon for an instrument to have 6 harmonics and then abruptly stop, so I'd say it's just an artifact of the way these particular graphs were made.  One could generate such tones and listen to a sweep in the vicinity of 8:5 and see if one hears an especially consonant point or not.   (And by "one", I mean "Me, but I don't have time right now")
Comment: Thank you for those graphs. The first two show no notch for 8:5 (minor 6th) even though that shouldn't be markedly less consonant than minor 6th or minor 3rd. Is it really objectively less consonant than conventional musicology claims, or does this show a fault in the model?
Comment: @RosieF At that interval the frequencies would be 1 2 3 4 5 6 and 8/5, 16/5, 24/5, 32/5, 8, 48/5, so there would be no harmonics in common.
Comment: @RosieF Nevermind I made the sound file: https://soundcloud.com/endolith/6-harmonics-sethares-plot
Comment: Hi Endolith - any chance you can share the details of how you produced your 14 harmonic curve?
Comment: @topomorto The details are in the Flickr link https://gist.github.com/endolith/3066664 " Base frequency is 500 Hz, including the effects of 14 harmonics" `model='product'` and most likely the harmonics are amplitude 1, 1/2, 1/3, 1/4, etc.
Comment: Thanks - clicking through, I hadn't realised there was so much on the sethares site too, despite looking at it many times recently!


Using NX with no PasswordAuthentication SSH setup

Question: I'm trying to setup passwordless SSH access.
My username is bmccann, so in /etc/ssh/sshd_config I added:
PermitRootLogin no
PasswordAuthentication no
AllowUsers bmccann nx

I ran ssh-keygen on the client and put ~/.ssh/id_rsa.pub from the client into ~/.ssh/authorized_keys on the server.
I can now login with no password using the ssh command.  However, I can no longer access the machine via NX as long as /etc/ssh/sshd_config has "PasswordAuthentication no".
Server error logs:
$ grep NX /var/log/messages
Feb 11 01:25:51 bmccann-htpc NXSERVER-3.4.0-12[23552]: ERROR: Failed authentication. NXSsh exit status is:255 'NXNssUserManager::auth'
Feb 11 01:25:51 bmccann-htpc NXSERVER-3.4.0-12[23552]: Failed SSHd authentication for user 'bmccann', to '127.0.0.1', port '22': 'NX> 204 Authentication failed.\n ' 'NXNssUserManager::auth'
Feb 11 01:25:51 bmccann-htpc NXSERVER-3.4.0-12[23552]: ERROR: Error while trying to authenticate user:bmccann. NXNssUserManager::auth returned 255 'NXShell::handler_login'
Feb 11 01:25:51 bmccann-htpc NXSERVER-3.4.0-12[23552]: ERROR: failed 'sshd authentication' for user 'bmccann' from '22.214.171.124'. NXShell::handler_login NXShell 373

What do I need to do to restore my NX access?  Is there something I need to setup in the NX client so that it no longer asks me for a password?
Answer:
What do I need to do to restore my NX access?

Bring back the change in /etc/ssh/sshd_config and restart OpenSSH server.
Use password or authorized keys. Empty passwords not allowed with PAM authentication.
Comment: I ran ssh-keygen on the client.

Then I put ~/.ssh/id_rsa.pub from the client into ~/.ssh/authorized_keys on server.

Is that what you mean by authorized keys or do you mean that I need to enter a passphrase when I run ssh-keygen?
Comment: No, passphrase not needed. Test ssh from client to server: ssh -vvv server
Comment: Hmm, I am using authorized keys without a passphrase, so I'm not sure what's wrong.  I've updated the question to make that clearer and provide some more details.  Thanks so much for the help.
Comment: AllowUsers myusername nx bmccann
Comment: I only put myusername as an example because I wasn't sure it would be obvious my username was bmccann.  I actually have "AllowUsers bmccann nx" in the file.  Sorry for the confusion.  I've clarified above.
Answer: I couldn't find a way to get NX to stop prompting me for a password, but the following worked to allow a password to be used with NX:
EnableUserDB = "1"
EnablePasswordDB = "1"
sudo /usr/NX/bin/nxserver --useradd bmccann

I added more details on my blog.


Citation Network - What is the most effective way to present?

Question: I have been working on a project that illustrates relationships between authors' articles and their respective citations (from other authors). Then, I created a matrix that shows edges between them.
Ultimately, we want to measure originality among all the articles, and we are open to additional suggestions on measuring originality.
Below is the code that I have already created (in RStudio using the bibtex package and igraph package):
<code>data <- readFiles("network_science_450.bib") #read in data
convert<- convert2df(data, dbsource = "isi", format = "bibtex") #converted in a data frame

matrix <- cocMatrix(convert, Field = "CR", sep = ";")
sort(Matrix::colSums(matrix), decreasing = TRUE)[1:5]

NetMatrix <- biblioNetwork(convert, analysis = "coupling", network = "references", sep = ".  ")
NetMatrixTable <- as.matrix(NetMatrix, mode="directed", weighted=TRUE)

binary <- ifelse(NetMatrixTable>0,1,0) #converted into a binary matrix
as.matrix(binary)
</code>
We have created a binary matrix to represent all these relationships, but I was wondering if there is a better way to present our data. We have explored Hasse diagram as a possibility.
Our main problem is we cannot find a way to create an adjacency matrix to perform further analysis. We want to perform transitive reduction on the matrix.
Comment: Without having data to use, it would seem that `cocMatrix` would generate an adjacency matrix (i.e. binary matrix representing which articles cite other articles). What downstream analyses. It would help if you could be more specific about the problem you are having and provide a sample of your data `dput(head(convert))`
Answer: I don't really understand your problem but it looks like you want to build a sociomatrix. If so, try:
<code># citaton data
df <- data.frame(article = sample(LETTERS, 50, replace = TRUE),
                 cited_article = sample(LETTERS, 50, replace = TRUE))
## network creation
# 2-mode sociomatrix
df.2mode <- table(df)
df.2mode
# diag(df.2mode) <- 0
</code>
(A reproducible example is required for SO' questions)


How do I solve this problem on matrices?

Question: A club has m members with serial numbers $1,2,...,m$. If members with serial numbers $i$ and $j$ are friends, then $A(i,j)=A(j,i)=1$ otherwise $A(i,j)=A(j,i)=0$. By convention, $A(i,i)=0$, that is, a person cannot be friends with oneself. Let $A^k(i,j)$ refer to the $(i,j)th$ entry in the $kth$ power of the matrix $A$, that is $A^k$.
It is given that $A^9(i,j)>0$ for all pairs of $i,j$, where $i$ and $j$ are less than or equal to $m$, $A^2(1,2)>0$ and $A^4(1,3)=0$.
Then which of the following are true?
a) Member 1 and 2 have at least one friend in common.
I did this by taking $A$ as a square matrix of order 3, with diagonal entries equal to 0 as the problem mentions. After calculating $A^2$, with the given data in the problem, I found that members 3 and 2 are friends, and members 1 and 3 are friends. So 3 is the common friend in the least case scenario and this statement is true.
b) $A^2(i,i)>0$ for all i, i less than or equal to m.
The matrix $A^2$ turns out to have all non-zero entries so this is true.
c) m is less than or equal to 9.
d) m is greater than or equal to 6.
I cannot figure out how to do parts c) and d).
Comment: *"A person cannot be friend with oneself."* This is sad.
Comment: *"The matrix $A^2$ turns out to have all non-zero entries so this is true."*  How did you get this?  If true it implies $A^k$ is a positive matrix for $k\geq 2$.  That $A^2(i,i)\gt 0$ is weaker and implied by the fact that $A^9$ is a positive matrix
Comment: I just calculated $A^2$ for a square matrix $A$ of order 3 after putting the diagonal elements of $A$ equal to 0, and $A(1,3)=A(3,1)=1$ and $A(2,3)=A(3,2)=1$.
Answer: (a) is true. Since $A^2(1,2)=\sum_{k=1}^mA(1,k)A(k,2)>0$, we have $A(1,k),A(k,2)>0$ for some $k$. However, by definition, $A(1,1)=A(2,2)=0$. Therefore $k\ne1,2$, meaning that member $k$ is a common friend to members $1$ and $2$.
(b) is true. Since $A^9>0$, $A$ is primitive and hence irreducible. It follows that for any given $i$, we must have $A(i,j)=1$ for some $j\ne i$. But then $A^2(i,i)\ge A(i,j)A(j,i)=1>0$.
(c) and (d) are false. For any $m\ge5$, consider the undirected graph where every node $k\ge5$ is only connected to node $4$ and the subgraph containing nodes $1$ to $5$ are connected in the following way: $3\leftrightarrow1\leftrightarrow4\leftrightarrow2\leftrightarrow5\leftrightarrow4$. Then $A^2(1,2)>0$ because the path $1\to4\to2$ has length $2$. Also, $A^4(1,3)=0$ because every path of length $\le4$ joining nodes $1$ and $3$ must have odd length. Finally, $A^9>0$: since every node (including node $4$) is connected to node $4$ by a path of length at most $2$, by appending an appropriate number of copies of the two-cycle $4\to1\to4$ and the three-cycle $4\to2\to5\to4$, every pair of nodes $i$ and $j$ are connected by a path of length $9$:
$$
i\to\cdots\to\underbrace{4\to\cdots\to4}_{\text{concatenation of two or three cycles}}\to\cdots\to j.
$$
In terms of matrix, for any $m\ge5$ (therefore $m$ can be smaller than $6$ or greater than $9$), if we put
$$
A=\pmatrix{B&e_4e^T\\ ee_4^T&0},
\ B=\pmatrix{
0&0&1&1&0\\
0&0&0&1&1\\
1&0&0&0&0\\
1&1&0&0&1\\
0&1&0&1&0}
$$
where $e=(1,1,1,1,1,\ldots,1)^T$ and $e_4=(0,0,0,1,0,\ldots,0)^T$, then $A^2(1,2)>0,\,A^4(1,3)=0$ and $A^9>0$.


Node Js / Rotas / Controllers

Question: Estou começando a estudar o nodejs e surgiu uma dúvida:
Bom eu queria fazer uma api para alimentar o front - end web e mobile com essa api
no caso eu tenho essas rotas (apenas para estudo):
const express = require('express');
const routes = express.Router();

const ProductController = require('./controllers/ProductController');
//primeira rota
routes.get("/products", ProductController.index);
routes.get("/products/:id", ProductController.show);
routes.post("/products", ProductController.store);
routes.put("/products/:id", ProductController.update);
routes.delete("/products/:id", ProductController.delete);

module.exports = routes;

Bom eu fiquei  com dúvida como eu conseguiria fazer com que determinada rota só fosse acessada pelo meu front-end de mobile ou web
um exemplo: Para cadastrar um produto, como eu conseguiria limitar isso apenas para o meu front-end( ou seja um dashboard de administrador acessar essa rota), e caso um usuario comum consiga achar essa rota não conseguisse adicionar um produto .. ( creio que isso seja relacionado com segurança? )
Se alguém poder me dar dicas de o que estudar para limitar determinada rota apenas para o meu front - end.
Answer: Para a segurança do acesso a sua API, uma das alternativas mais comuns é utilizar token. O token seria um código gerado pela sua aplicação no processo de login. Este código deveria ser devolvido no cabeçalho de todas as requisições (Bearer Token). Antes de executar uma rota, ele seria verificado utilizando um "middleware", que é um trecho de código executado antes de atender a requisição. Se ele existir, ou se ele existir mas o seu conteúdo não for válido, então a rota não é executada.
Através do token, você pode validar se aquele usuário tem permissão para acessar a rota
Aqui tem um exemplo de validação de rotas e token com JWT
Arquivo de rotas:

const express = require("express");
const validate = require("express-validation");
const handle = require("express-async-handler");
const routes = express.Router();
const authMiddleware = require("./app/middlewares/auth");
const UsuarioController = require("./app/controllers/UsuarioController");
const FerramentasController = require("./app/controllers/FerramentasController");

routes.post("/login", validate(validatorLogin), handle(UsuarioController.login));
routes.post(
  "/usuario",
  handle(UsuarioController.cadastrar)
);
routes.get("/ferramentas", handle(FerramentasController.listar));
routes.get("/ferramentas/:id", handle(FerramentasController.detalhe));
//As rotas a partir daqui exigem autenticação
routes.use(authMiddleware);
routes.post(
  "/ferramentas",
  handle(FerramentasController.cadastrar)
);
routes.delete("/ferramentas/:id", handle(FerramentasController.remover));

module.exports = routes;

Aqui tem um exemplo de como seria o "authMiddleware" responsável por validar o token antes de executar a rota:

const jwt = require("jsonwebtoken");
const authConfig = require("../../config/auth");
const { promisify } = require("util");
module.exports = async (req, res, next) => {
  const authHeader = req.headers.authorization;
  if (!authHeader) {
    res.status("401").json({ error: "Token não informado" });
  }
  const [, token] = authHeader.split(" "); // Quebra o header num array e pega o segundo elemento (token sem o Bearer)
  try {
    //Transforma uma funcao que usa callbacks numa promisse
    const decoded = await promisify(jwt.verify)(token, authConfig.secret);
    req.userID = decoded.id; // Adiciona em todas as requisicoes que utilizaram este middleware o ID do usuário
    return next(); // Middleware tem sempre req, res e next
  } catch (error) {
    res.status("401").json({ error: "Token inválido" });
  }
};

Dentro da rota, você vai poder validar o req.userID recuperado pelo middleware, e validar se o usuário tem permissão para utilizar aquela rota.
Em relação a bloquear o acesso por Web ou Mobile, você poderia pensar em criar um header (cabeçalho) personalizado. Então além do token ser válido, o cabeçalho também deveria ter uma determinada informação para que a rota seja executada.
Aqui tem um tutorial de como implementar um JWT token
https://www.luiztools.com.br/post/autenticacao-json-web-token-jwt-em-nodejs/
Comment: Muito obrigado vou ler com bastante atenção e tentar aplicar!

Node Js / Rotas / Controllers


Quais são os formatos utilizados para se representar valores (cor, contraste, brilho) em imagens digitais?

Question: O que quero é: reconhecer padrões de pixels em um arquivo, ou saber como o assembler trata isso (para justamente facilitar reconhecer estes padrões).
Sempre quis criar alguma aplicação para manipular imagens, fazendo reconhecimento de objetos, tentativas de novas técnicas de chroma key, etc. É possível por exemplo, saber as o que uma cadeia de bytes escritas em ASCII significa, mas não sei como são escrito os pixels.
E nunca encontrei nenhum material falando do funcionamento dos bytes ou pixels de uma imagem em baixo nível. Saber como é tratado em assembly pode ajudar.
Minhas tentativas foram: abrir o bitmap em um programa de leitura binária, aonde não encontrei um padrão, e tentei o básico também tentando abrir em um interpretador de ASCII, aonde foi pior.
Se eu conseguir descobrir como encontrar estes padrões de pixels, eu poderia usar qualquer linguagem que manipula arquivos (mesmo em alto nível) para reescrever pixels. Ou criar algum algoritmo de reconhecimento facial, etc.
Se alguém tiver alguma ideia do que eu devo pesquisar já agradeço!
Para ser justo, achei dois ótimos materiais na internet:
Uma questão próxima a isso no Yahoo respostas
https://br.answers.yahoo.com/question/index?qid=20111021192959AAUuYZ5
Uma biblioteca gráfica que já usei para programação de games
http://www.unidev.com.br/index.php?/topic/54321-gdi-setpixel-em-baixo-n%C3%ADvel/
Porém, preciso mais do que isso!
Espero que minha dúvida enriqueça a curiosidades de muitos!
Comment: Olá Paulo, bem vindo ao SOPT. Antes de mais nada, saiba que este site não é um fórum. Sua pergunta precisaria ter uma dúvida mais objetiva. Se não fez ainda, faça o [tour] e leia [ask]. Quando vc diz " [...] material falando do funcionamento dos bytes ou pixels de uma imagem em baixo nível." O que vc quer dizer com "baixo nível"? Apesar de mencionar assembly, o restante da pergunta parece indicar que você quer saber quais são os **formatos** comuns de armazenamento de imagens. Se for esse o caso, sugiro dar uma olhada aqui: https://en.wikipedia.org/wiki/Image_file_formats
Comment: O formato bitmap é razoavelmente bem especificado. Veja [aqui](https://en.wikipedia.org/wiki/BMP_file_format) e [aqui](http://www.dragonwins.com/domains/getteched/bmp/bmpfileformat.htm), por exemplo. De todas as formas, **já existe muita coisa pronta** para o que você quer fazer. Vc já tentou estudar um pouco de [OpenCV](http://opencv.org/), por exemplo? Uma sugestão de link que pode servir como boa introdução: https://www.youtube.com/watch?v=-Hb7ZFSYqnk
Comment: Paulo, eu realmente não entendi a sua dúvida. Vc agora menciona "drives para comunicação". Pergunta se alguém conhece "maneiras de fazer o que quer" em mais alto nível... Mas o que você quer?! Não está claro. Tente editar a pergunta e deixá-la mais focada. Seria o seu desejo simplesmente saber como ler/gravar um arquivo bitmap em assembly?
Comment: Tem que ser em assembly? A tag não condiz muito com o que você pede no texto. Além disso, uma coisa é manipular bitmaps em memória, outra é salvar e ler de arquivos - frequentemente são formatos diferentes.
Comment: A manipulação de bitmaps em memória de trabalho é irrelevante no momento, uma vez que o texto indaga sobre manipulação de arquivos. Assembly é uma linguagem de montagem, considerada de baixo nível, pois ainda que seja legível a humanos, faz comunicação direta com os dispositivos, uma vez que a maioria das arquiteturas tem sua própria versão de assembly, e seus respectivos drives para comunicação. Portanto, o tópico não só trata de assembly, como também drives escritos sobres. O ponto é: para que quero isso.
Se conhece maneiras de fazer o quero em nível mais alto, gostaria que compartilhasse.
Comment: Se você quer manipular arquivos de imagens e imagens em memória são irrelevantes a você, então porque o assembly? O assembly na prática só vai manipular imagens em memória e quase ninguém vai se preocupar em usar assembly diretamente para ler ou escrever em arquivos, vez que isso é algo que linguagens de alto nível conseguem fazer com excelência.
Comment: Isso foi em resposta ao usuário Victor Stafusa sobre contestar sobre o uso da tag assembly. Não é uma explicação da minha dúvida.
Mas se não está clara minha dúvida, eu vou tentar especular o que possivelmente não ficou claro. Se você poder adiantar que parte, vai ajudar a eu editar e melhorar minha pergunta.
Comment: Paulo, a questão que eu estou colocando é que saber em assembly **não vai te ajudar**. Os algoritmos para ler e escrever imagens em PNG, GIF, JPG, TIF, PCM, PCX, TGA, BMP ou o formato que você preferir não tem relação com o assembly e podem ser utilizadas quaisquer linguagens de programação para manipulá-los. Em especial, assembly seria uma das piores escolhas possíveis para isso porque não é lá muito portável e compiladores são capazes de escrever assembly muito melhor que a maioria dos humanos conseguiria. [continua]
Comment: [continuação] Normalmente esses algoritmos são implementados em C, mas já vi serem feitos em Delphi, em Java e em Python também. O assembly seria útil talvez na parte de drivers, mas os drivers não manipulam arquivos, eles manipulam apenas bitmaps em memória que você deixou bem claro que não são o seu interesse.
Comment: Ah, e se você quer fazer coisas como reconhecimento facial, o melhor seria ler o arquivo, colocá-lo na memória e fazer o reconhecimento direto na memória. O motivo disso, é que em vários formatos de arquivo, os pixels não estão codificados lá diretamente. Por exemplo, o JPG usa uma técnica matemática bem complicada chamada de [transformada rápida de Fourier](https://pt.wikipedia.org/wiki/Transformada_r%C3%A1pida_de_Fourier) para fazer compactação e reduzir o tamanho do arquivo. Por outro lado, descompactado na memória, ele estará em um formato de fácil manipulação para o que você quer.
Comment: Não. Reconhecimento facial (no sentido do que quero) é apenas umas das possíveis vantagens extras (que você explicou que não seria vantajoso dessa forma).
Comment: O que quero é: reconhecer padrões de pixels em um arquivo, ou saber como o assembler trata isso (para justamente facilitar reconhecer estes padrões).
O que posso conseguir com isso..........coloco aqui as possibilidades....!
Eu não consigo ser mais claro. Estou me esforçando!
Comment: Vamos [continuar esta discussão no chat](http://chat.stackexchange.com/rooms/51437/discussion-between-paulo-sergio-duff-and-victor-stafusa).
Answer: Para imagens em memória, existem vários formatos possíveis, basicamente de acordo com o número de bits por pixel, que em geral pode variar de 1 (preto ou branco) até 32 (8 para o vermelho, 8 para o verde, 8 para o azul e 8 para o alpha). Os mais comuns são:

1 bit por pixel - Preto ou branco.
2 bits por pixel - Preto, branco e 2 tons de cinza.
4 bits por pixel - 16 tons de cinza.
8 bits por pixel - 256 tons de cinza.
2 bits por pixel - Preto, branco, ciano e magenta.
2 bits por pixel - Preto, branco, verde e vermelho.
4 bits por pixel - 16 cores básicas escolhidas de acordo com uma palheta de cores. Normalmente essas cores são preto, branco e dois tons de cada uma dessas cores: cinza; azul; vermelho; verde; amarelo; ciano e magenta.
8 bits por pixel - 256 cores escolhidas arbitrariamente de uma palheta/tabela.
16 bits por pixel - Capaz de representar 65536 cores distintas. Desses bits, temos 5 para o componente vermelho, 6 para o componente verde e 5 para o componente azul. A ordem dos bits pode ser RGB (vermelho-verde-azul) ou BGR (azul-verde-vermelho).
24 bits por pixel - capaz de representar 16777216 cores distintas. São usados 8 bits para cada componente de cor. A ordem dos bits pode ser RGB ou BGR.
32 bits por pixel - Igual ao caso dos 24 bits por pixel, mas acrescentando um byte para representar a transparência, ou mais precisamente o alpha, que é a opacidade, o oposto da transparência. Um alpha de valor zero indica total transparência, enquanto que um alpha de valor máximo (255 neste caso) indica uma cor totalmente opaca. O conceito de transparência/opacidade é útil quando você quiser desenhar imagens umas sobre as outras. A ordem dos bits pode ser ARGB, RGBA, ABGR ou BGRA.

Os formatos em arquivo tais como PNG, GIF, JPG, PCX, BMP, TGA, TIF, PGM, PPM, PBM, etc são muitas vezes compactados e nem todos representam os pixels diretamente. Eles tem largas diferenças entre si e cada um tem um formato único e bastante diferente dos demais. Por exemplo:

Alguns formatos, como o BMP, o PGM e o PPM, representam os pixels diretamente e de forma sequencial.
O PNG usa uma compactação com algumas similaridades com o ZIP.
Há formatos (por exemplo, GIF) onde há um cabeçalho com uma palheta/tabela de cores e cada cor é um número de 0 a 255 de acordo com a tabela. Neste caso, podemos ter 0=azul, 1=laranja fosforescente, 2=verde musgo, 3=dourado... Dependendo de como está a tabela.
Transmissões de TV analógicas utilizam dois componentes para cores (crominância) e um para o brilho (luminância), para ser compatível com formatos de TV preto-e-branco que só tinham o brilho. Esse mesmo conceito é usado também no JPG.
Há formatos que usam algumas técnicas matemáticas bem complicadas que surgem da necessidade de se representar informações que representam ondas (com frequência, amplitude e fase) na forma de pontos discretos e compactos. Disso advém a transformada rápida de Fourier e a transformada discreta do cosseno. Elas são frequentes na conversão de formatos analógicos para digitais e também para espremer o máximo possível de informações sobre a imagem na menor área possível. O JPG, por exemplo, utiliza a transformada discreta do cosseno.

Assim sendo, tratar os pixels diretamente em muitos desses formatos é difícil, e convém muito mais você ler os arquivos, convertendo-os em bitmaps diretamente na memória e então manipular esses pixels diretamente na memória.
Na prática, não vale a pena se preocupar muito com os formatos dos arquivos, vale mais a pena utilizar uma biblioteca pronta para ler e escrever imagens nesses formatos e se preocupar então apenas com os bitmaps em memória.
Como o colega Luiz Vieira mencionou em comentários, o OpenCV é uma biblioteca bem rica e amplamente difundida que já tem muitas dessas coisas já prontas para se usar.
Comment: Eram essas informações que você e o Luiz me passaram que estava procurando. Isso já me dá muito para começar. Muito obrigado Victor!
Comment: Apesar da pergunta não ter sido tão clara, a resposta foi realmente muito boa e útil. :)
Comment: O BMP é o formato mais fácil de interpretar (já consegui uma vez) : http://dragonwins.com/domains/getteched/bmp/bmpfileformat.htm Se puder linkar na resposta, agradeço