How to determine which element has called (triggered) the AJAX callback function?

Question: For example:
/** FIRST FIELDSET **/

$form['fieldset_1']['enabled'] = array(
  '#type'        => 'radios',
  '#options'     => array(1 => t('Enable'),
                          0 => t('Desable'),
                          ),
  '#ajax'    => array('callback' => 'my_ajax_callback',
                      'wrapper'  => 'my_wrapper_1',
                     ),
);

$form['fieldset_1']['myChangebleField'] = array(
  '#type'   => 'whatever',
  '#prefix' => '
',
  '#suffix' => '
',
);

/** SECOND FIELDSET **/

$form['fieldset_2']['enabled'] = array(
  '#type'        => 'radios',
  '#options'     => array(1 => t('Enable'),
                          0 => t('Desable'),
                          ),
  // Now I am calling the same callback function but assigning a different wrapper
  '#ajax'    => array('callback' => 'my_ajax_callback',
                      'wrapper'  => 'my_wrapper_2',
                     ),
);

$form['fieldset_2']['myChangebleField'] = array(
  '#type'   => 'whatever',
  '#prefix' => '
',
  '#suffix' => '
',
);

function my_ajax_callback($form, $form_state) {

/**
 *  How do I figure out which of the element "enabled" was the caller?
 *
 *  In that way I could do something like:
 **/
   switch($caller){
     case 'fieldset_1':
       // do stuff...
       break;
     case 'fieldset_2':
       // do stuff...
       break;
     // ...
   }

   return $form[$fieldset]['myChangebleField']
}

As seen in the example above, how do I figure out which of the element "enabled" was the caller?
Having two callback functions is not an option as the form elements will be constructed in a dynamic way and I won't actually use a switch statement in the callback function as shown in the example.
Cheers,
Answer: Got it!!! This guy here $form_state['triggering_element'] does the trick.


Convergence of a sequence of subspaces

Question: Let $E_n\subset \mathbb R^n$ be a sequence of subspaces. What does it mean $E_n$ convergence to a subspace $E\subset \mathbb R^n$? I saw this when reading about hyperbolic sets.
Where can I  read about this subject?
Thanks.
Answer: If all the subspaces have a common dimension $r$, then a natural object to look at is the Grassmannian $Gr(r,\mathbb{R}^n)$.
In particular one may define a metric $d(E,F) = \|P_E - P_F\|$, where $\|\cdot\|$ is the operator norm and $P_E \colon \mathbb{R}^n \to E$ is the orthogonal projection on $E$. The convergence of $E_n \to E$ can now be taken to mean that $\lim_{n \to \infty} d(E_n, E) = 0$. One more concrete way of computing this metric is via the formula:
$$d(E,F) = \sup \{|\langle u,v \rangle| : u \in E, v \in F^\bot, \|u\| = \|v\| = 1\}$$
Comment: Thanks you. Could you please tell me where did you read about this? I want to read on references?
Comment: @mac: Unfortunately I don't remember where I learned this from but Grassmannians are quite standard stuff, so I'd expect a little googling should suffice. The formula in the end is something I proved in my master's thesis a few years ago, having found it used somewhere else without a proof. You could try to prove it too - it's not that hard.

Convergence of a sequence of subspaces


EntityMetadataWrapperException when accessing empty entity body. How can I test if body is empty first?

Question: The following call
$body = $entity->body->value->value();

throw the following error
EntityMetadataWrapperException : Unable to get the data property value as the parent data structure is not set. dans EntityStructureWrapper->getPropertyValue()

when the body is empty.
How can I prevent it?
Answer: Just simply see the value is empty
if($node_wrapper->body->value() != ""){
 //Evaluates true
}

or use a try catch
try{
 echo $entity->body->value->value();
}catch(Exception $e){
  echo "Value does not exist continue";
}

Comment: Catching an `EntityMetadataWrapperException` would be better, but yeah `try...catch` is the way to go


Adding a block to the page but not declaring it in hook_block_info()

Question: I have inherited some code from a contractor. This code adds a block to page_content but it is not defined in hook_block_info().
We then have the following code:
/**
 * Implements hook_block_view().
 */
function erepair_blocks_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    ...
    case 'page_content':
      $block['content'] = page_content();
      break;
    ...
  }
  return $block;
}

This block is then displaying on some certain pages but I can't work out how.
So I have looked into the database and have found an entry for the block in the table.
It is safe to presume that they have either:

executed some sql to add the block (but did not do it in an install file)
had an entry in hook_block_info() added the block to the page then deleted the deceleration in hook_block_info()

I imagine what ever they have done is not best practice. Is there anything else they could have done (we are not using features).
Comment: What's in the `page_content()` function?
Answer: The block may be positioned using the Context module, or via custom code in about a million places. What you can do is temporarily modify the template_preprocess_block() function in block.module, identify the block you're looking for by looking at the data in $variables['block'], and then looking at the output of debug_backtrace() to see the callstack and find the place the block is coming from.


read file from s3 bucket

Question: I'm trying to get a file from s3 bucket with golang. What's special in my request is that I need to get a file from the root of the s3. i.e, in my situation, i have a buckets folder which is the root for the s3, inside that i have folders and files. I need to get the files from the buckets folder. it means that i don't have a bucket folder because i access only to the root.
the code im trying is:
<code>numBytes, err := downloader.Download(file, &s3.GetObjectInput{
  Bucket: aws.String("/"),
  Key:   aws.String("some_image.jpeg"),
})
</code>
The problem is I got an error that says the object does not exist.
Is it possible to read files from the root of s3? What do I need to write in the bucket? the key is written okay?
Many thanks for helping!
Comment: You have to specify the bucket name to read the object. The key is part which allows folder like addressing.
Comment: I tried to put on the Bucket the value "buckets", I thought maybe if I will give the root folder it will work but I still got the same error… what you mean is I only can read files from a bucket folder and not from the root?
Comment: Example: Bucket: "mybucket", Key: "dogs/snoopy.png".
Answer: All files in S3 are stored inside buckets. You're not able to store a file in the root of s3.
Each bucket is its own distinct namespace. You can have multiple buckets in your Amazon account, and each file must belong to one of those buckets.
You can either create a bucket using the AWS web interface, command line tools or API. (Or 3rd party software like Cyberduck).
You can read more about buckets in S3 here: https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingBucket.html
Comment: You need to add the actual name of your bucket. It is not possible to have a bucket without a name. You can either log in to the AWS web console, or use a program like Cyberduck, to view your buckets. That should tell you the name of the bucket you created. (Or, if you haven't created one yet, you can create one there too)
Comment: I'm using on seaweedFS which allow me to upload files into the root folder. The question it's can I read the files I upload to the root folder?
Comment: Yes definitely - you can read a file that was uploaded into the root of the bucket. In that case, you will need to adjust your code to include the correct bucket name.

(As an aside, S3 doesn't actually have folders in the traditional sense - each file has its own distinct name. It's mainly just a convention that the slash '/' is used as a delimiter to simulate a directory structure)
Comment: i tried to put on the bucket name the value "buckets" which mean the root of the storage but it didnt worked. do you know which convention i need to make it work in the meaning of bucket value and key value in the code?

read file from s3 bucket


Cannot open include file: 'gmp.h': No such file or directory (Python/Cython)

Question: I was learning how to integrate gmpy2 with Cython. From the docs, I was provided with an example code. Since I am not really sure about what was going on, I felt that I can learn how to use gmpy in Cython by playing with the example code provided.
The example code:
setup.py
   "A minimal setup.py for compiling test_gmpy2.pyx"

    from distutils.core import setup
    from distutils.extension import Extension
    from Cython.Build import cythonize
    import sys

    ext = Extension("test_gmpy2", ["test_gmpy2.pyx"], include_dirs=sys.path, libraries=['gmp', 'mpfr', 'mpc'])

    setup(
        name="cython_gmpy_test",
        ext_modules=cythonize([ext], include_path=sys.path)
    )

test_gmpy2.pyx
    "A minimal cython file test_gmpy2.pyx"

    from gmpy2 cimport *

    cdef extern from "gmp.h":
        void mpz_set_si(mpz_t, long)

    import_gmpy2()   # needed to initialize the C-API

    cdef mpz z = GMPy_MPZ_New(NULL)
    mpz_set_si(MPZ(z), -7)

    print(z + 3)

Running setup.py gives me this error:
fatal error C1083: Cannot open include file: 'gmp.h': No such file or directory

What is wrong? How do I fix it? I checked my install for gmpy. I believe there isn't anything wrong with it.
Comment: I maintain gmpy2. If you are on Linux, you'll need to install the development headers for GMP, MPFR, and MPC. `sudo apt install libmpc-dev` or similar will do that. If you are using Windows, let me know.
Comment: Is `gmp.h` in the same directory as the file?
Comment: Hello! gmp.h is not in the same directory as the file, and I am using windows.
Comment: Good news: I'm able to successfully build the Cython test suite on Windows. Bad news: I haven't properly documented it. I've created https://github.com/aleaxit/gmpy/issues/320 to document the procedure. If you can help test / provide feedback, please leave a comment there. Once it is working, I leave a complete answer.
Answer: I can provide a working example. The package fractalshades includes a Cython extension which imports (cimport) gmpy2 and is compiled both under Linux and Windows.
The code for the Cython extension is hosted here
https://github.com/GBillotey/Fractalshades/blob/master/src/fractalshades/mpmath_utils/FP_loop.pyx
The key parts to compile for windows (with MS Visual Studio) have been :

includes the headers in gmpy2 installation directory
An import library (.lib file) is necessary when calling functions in a dll - it provides the stubs that hook up to the DLL at runtime. To generate these .lib one can use dumpbin & lib executables provided by visual studio.

For implementation details, the Windows build is run on a github runner with the full script available as a Github workflow.


Laravel delete entry with destroy method

Question: I have got a form with a delete button which I see on the console that it's sending a delete request.
jquery.js:4 DELETE http://laravel.com/painel/player/53 500 (Internal Server Error)

and my route is:
Route::resource('painel/player','PlayerController');

| DELETE    | painel/player/{player}      | painel.player.destroy | App\Http\Controllers\PlayerController@destroy    |

and my method destroy is as below:
    public function destroy($id)
    {
            $player = Player::where('id_player', '=', $id)->first();
            $player->delete();
            $player = array(
                    'users'         => Player::all(),
                    'refresh'       => true
            );
            return View::make('painel.player.show', $player);
    }

EDIT:
I forgot to mention the ajax:
    $( document ).on('click', '.solsoConfirm', function(){
        $("#solsoDeletForm").prop('action', $(this).attr('data-href'));
    });

    $( document ).on('click', '.solsoDelete', function(e){
        e.preventDefault();

        var solsoSelector   = $(this);
        var solsoFormAction = $('#solsoDeletForm').attr('action');

        $.ajax({
            url:    solsoFormAction,
            type:   'delete',
            cache:  false,
            dataType: 'html',
            success:function(data) {
                $('#solsoDeleteModal').modal('hide');
                $('#ajaxTable').html(data);
                $('#countClients').text( $('.solsoTable').attr('data-all') );
                $.growl.notice({ title: solsoSelector.attr('data-message-title'), message: solsoSelector.attr('data-message-success') });
                $('.solsoTable').dataTable();
            }
        });

        return false;
    });

Comment: If you had it as an implicit controller, it would. Anyway, can you post your controller and `routes.php` full code to the question?
Comment: How have you defined the routes for the controller, is it a resource controller or an implicit controller? Can you include your `routes.php` code as well.
Comment: It is a resource controller.. Does that make any difference?
Comment: Sorry, It is an AJAX request. I just edited the question...
Comment: I understand that its an AJAX request, but you must have defined the _route_ for the delete request that you're making (most probably in the file `routes.php`), so can you post its content as well as full controller code for the destroy method?
Comment: I just edited with the route.
Answer: While your $.ajax({ ... type: 'delete' ... }) should work, you need to set up your response to accept the 'delete' method.
The alternative and standard way to do PUT, PATCH, and DELETE in Laravel is via method spoofing:
<form action="painel/player/{{ $id }}" method="POST">
    {{ method_field('DELETE') }}
    {{ csrf_field() }}
</form>

Your JS might look something like:
var csrf = $('input[name="_token"]').val();

$.ajax({
    url: solsoFormAction,
    type: 'post',
    data: {_method: 'delete', _token: csrf},
    ...
});

Comment: BTW the actual content of the 500 error will probably tell you where the problem is - you should be able to see it in your browser's developer tools.
Comment: It's not working... the dev tool only says "500 (Internal Server Error)".. it says the error is on the ajax method.. if I send the request without being an ajax request.. it works.
Comment: In dev tools, switch to network tab, and click the POST.  You should see "Headers", and "Response", and some other things.  Click Response - you should see the full response that Laravel generates, which will describe the problem.  If you can't find it, check the Laravel logfile.  Have you updated your AJAX code to send both _method and _token?


Using Paperclip to Form Image Database

Question: I have a database called stories.  I can create a story using the form and upload it to the database. I then have a show page where I display the name of the store and a link to view it.
I want to do the same things with pictures.  I want there to be two separate databases.  All the tutorials I come across tend to show you how to add avatars to a user model.  I don't want to do that.  I want to create a whole new second database.  Does anyone know of a tutorial out there like this?  I have been looking for days.  Is this possible?
Comment: don't you mean a new database table, rather than a whole new database? I'd recommend using Paperclip (https://github.com/thoughtbot/paperclip) and integrating it into a model.
Comment: Yes sorry, new table.
Answer: I think you want store pictures in individual database table not other database right?
There's the same thing with add avatar to user model. Create a model named picture, and set a column to store image file name. The rest is following Paperclip doc to fill your picture model.

Using Paperclip to Form Image Database


MATLAB: modify arima model coefficients

Question: I want to modify ARIMA model coefficients (MATLAB). I estimated my model using this code:
mdl_1 = arima(2, 1, 2);
estMdl_1 = estimate(mdl_1, data1');

MATLAB output:
 ARIMA(2,1,2) Model:
    --------------------
    Conditional Probability Distribution: Gaussian

                                  Standard          t
     Parameter       Value          Error       Statistic
    -----------   -----------   ------------   -----------
     Constant     -0.0251424     0.0207631       -1.21092
        AR{1}     -0.0111471     0.0720374      -0.154741
        AR{2}       0.662378     0.0498501        13.2874
        MA{1}       0.259267      0.153002        1.69454
        MA{2}      -0.600908      0.111204       -5.40366
     Variance      0.0833621     0.0122521        6.80391

And now I want to modify this values in order to see what happens with the residuals. Unfortunately, MATLAB does not support modifying the estimated model directly. So I use this code:
my_mdl1 = arima('Constant',-0.0251424,'AR',{-0.0111471,0.662378},...
'MA',{0.259267,-0.600908},'Variance',0.0833621);

As you can see, I created an arima model which equals to estimated model mentioned above.
After that I trying to infer my residuals for model my_mdl1 but they are not equals to residuls of the estimated model. Why?
Also I tried to look at the values of variables. The values of the estimated model (by MATLAB):
  estMdl_1 =

ARIMA(2,1,2) Model:
--------------------
Distribution: Name = 'Gaussian'
           P: 3
           D: 1
           Q: 2
    Constant: -0.0251424
          AR: {-0.0111471 0.662378} at Lags [1 2]
         SAR: {}
          MA: {0.259267 -0.600908} at Lags [1 2]
         SMA: {}
    Variance: 0.0833621

The values of the model created by me:
my_mdl1 =

ARIMA(2,0,2) Model:
--------------------
Distribution: Name = 'Gaussian'
           P: 2
           D: 0
           Q: 2
    Constant: -0.0251424
          AR: {-0.0111471 0.662378} at Lags [1 2]
         SAR: {}
          MA: {0.259267 -0.600908} at Lags [1 2]
         SMA: {}
    Variance: 0.0833621

As you can see, there is some difference. P, D, Q are not equals. Maybe it's a problem, or not? Unfortunately I don't know, how to edit P, D, Q values :(
Answer: Fortunately, I found the answer! Here it is.
And the decision for my case:
my_mdl1 = arima(2, 1, 2);
my_mdl1.Constant = -0.0251424;
my_mdl1.AR = {-0.0111471,0.662378};
my_mdl1.MA = {0.259267,-0.600908};
my_mdl1.Variance = 0.0833621;

It works! I can change the coefficients and receive proper residuals.

MATLAB: modify arima model coefficients


What are the advantages of stepwise regression?

Question: I am experimenting with stepwise regression for the sake of diversity in my approach to the problem. So, I have 2 questions:

What are the advantages of stepwise regression? What are its specific strengths?
What do you think about hybrid approach, where you use stepwise regression to select features, and then apply regular regression taking all the selected features together?
Answer: The primary advantage of stepwise regression is that it's computationally efficient. However, its performance is generally worse than alternative methods. The problem is that it's too greedy. By making a hard selection on the the next regressor and 'freezing' the weight, it makes choices that are locally optimal at each step, but suboptimal in general. And, it can't go back to revise its past choices.
As far as I'm aware, stepwise regression has generally fallen out of favor compared to $l_1$ regularized regression (LASSO), which tends to produce better solutions.

Tibshirani (1996). Regression Shrinkage and Selection via the Lasso

LASSO penalizes the $l_1$ norm of the weights, which induces sparsity in the solution (many weights are forced to zero). This performs variable selection (the 'relevant' variables are allowed to have nonzero weights). The degree of sparsity is controlled by the penality term, and some procedure must be used to select it (cross-validation is a common choice). LASSO is more computationally intensive than stepwise regression, but a number of efficient algorithms exist. Some examples are least angle regression (LARS), and an approach based on coordinate descent.
A similar approach to what you suggested in (2) is called orthogonal matching pursuit. It's a generalization of matching pursuit, which is the name for stepwise regression in the signal processing literature.

Pati et al. (1993). Orthogonal matching pursuit: recursive function approximation with applications to wavelet decomposition

On each iteration, the next best regressor is added to the active set. Then, the weights for all regressors in the active set are recomputed. Because of the reweighting step, this approach is less greedy (and has better performance) than the regular matching pursuit/stepwise regression. But, it still employs a greedy search heuristic.
All of these approaches (stepwise regression, LASSO, and orthogonal matching pursuit) can be thought of as approximations of the following problem:
$$\underset{w}{\min} \| y - X w \|_2^2 \quad \text{s.t. } \|w\|_0 \le c$$
In a regression context, columns of $X$ correspond to the independent variables and $y$ to the dependent variable. In signal processing, columns of $X$ correspond to basis functions and $y$ is a signal to approximate. The goal is to find a sparse set of weights $w$ that give the best (least squares) approximation of $y$. The $l_0$ norm simply counts the number of non-zero entries in $w$. Unfortunately, this problem is NP-hard, so approximation algorithms must be used in practice. Stepwise regression and orthogonal matching pursuit attempt to solve the problem using a greedy search strategy. LASSO reformulates the problem using a relaxation of the $l_0$ norm to the $l_1$ norm. Here, the optimization problem becomes convex (and thus tractable). And, although the problem is no longer identical, the solution is similar. If I recall correctly, both LASSO and orthogonal matching pursuit have been proved to recover the exact solution under certain conditions.
Comment: The objective function you write should include an intercept. Why you don't include it?
Answer: Stepwise selection is not generally a good idea.  To understand why, it may help you to read my answer here: Algorithms for automatic model selection.
As far as advantages go, in the days when searching through all possible combinations of features was too computationally intensive for computers to handle, stepwise selection saved time and was tractable.  However, note that the problems discussed in my linked answer above apply just as much to 'best subset' regression, so stepwise does not produce a good solution, just a bad solution faster.
Your idea of a hybrid approach would be fine, so long as the second model (with the selected features) was fitted on a new dataset.
Comment: Regarding what the OP called a "hybrid approach" (not quite sure why it's hybrid), you mean it's fine in the sense that the estimates of the coefficients of the model on the second new datasets should be fine (while biased and problematic on the original data), as long as the new dataset is large enough? It would of course potentially be a poor model, because it was selected in a bad way on the first dataset, simply it's coefficients would be estimated in a less problematic dataset.
Comment: Plus it's still often impossible to look through all possible combinations, because the number of different variables we have data on grows even faster than computing power, and people have more and more ideas about what to include in their models.
Comment: Reading that thread continues not to be helpful.
Answer: I have just taked a google search for what is Stepwise regression. I'm not sure if I fully understand it, but here is my first thought

It's greedy so it can not produce the good solution as Lasso does. I prefer Lasso
It's simple, easy to use, easy to code
After you use Stepwise regression, you already end up with a trained model that use selected features, so you do not need to use an other regression step as you mentioned as hybrid approach


ng-click not working when I use onsen ui and $().append

Question: When I use the JS to append a p tag into a div,the ng-click not working.My code as follows:
$("#newsArList").append("
Go To Article
");`

Actually,I
console.log("
1234
");`the result is `
1234

I just copy this one into the

,and it working.
I don't know the reason for this situation. Any suggestions?
Comment: The reason is that angular needs to know that the DOM has changed, and to compile it, in order to do its magic. Just don't do that. Use angular as it's meant to be used. Put that snippet of HTML in your page, add an `ng-if="showStuff"` to the element, and set $scope.showStuff to true when you want the element to appear in the page.
Comment: Thanks you help me,but it cannot solve my question.My code as follows :`` and

     `$("#newsArList").append("1234");`
  the result is the p tag will appear,but when I click it, it will not go to the article.html
Answer: You use angular directive, so you must compile the code into the DOM:
$("#newsArList").append('
Go To Article
');
ons.compile($("#newsArList")[0]);
As described here:

ons.compile function converts your custom elements based HTML to a
  normal DOM structure. Most browsers does not (yet) support custom
  elements by default, you need to call the function every time to make
  the magic happen.


Are LASSO coefficients raw or standardized?

Question: I'm doing binary classification and ran LASSO to try and do feature selection to reduce the parameters in the model. I have the coefficients from glmnet at the minimum lambda but am unsure how to interpret them. Most of the inputs to the model are test statistics/measurements of a sample. Is further normalization across all predictors before-hand needed to be able to say the coefficients are standardized? I saw similar questions but never saw a clear answer to this particular question, sorry if considered duplicate.
Answer: From the glmnet documentation for the function glmnet:

standardize
Logical flag for x variable standardization, prior to fitting the model sequence. The coefficients are always returned on the original scale. Default is standardize=TRUE. If variables are in the same units already, you might not wish to standardize. See details below for y standardization with family="gaussian".
Comment: No. The second sentence states "**The coefficients are always returned on the original scale.**"


laravel map user table to existing table

Question: i want to implement an api with laravel for an existing app. Here we have already a user tabel with other name und fieldnames.
I tried to map the fieldnames in the User Model with the following snippet:
...
use HasApiTokens, Notifiable;

public $timestamps = false;
protected $table = 'benutzer';

protected $maps =[
    "id" => "id",
    "name" => "app_user_id",
    "email" => "email",
    "password" => "kennwort",
    "email_verified_at" => "agb_zentrale_akzeptiert_datum"
];...

unfortunately when I send a post request to register a user I get a sql error back, while in my benutzer table the name is stored in the field app_user_id
so what do i miss or do i wrong here
Georg
Comment: We need to see your error before we can help you with said error.
Answer: The protected $maps = [] is not an official Laravel feature.
I assume you have forgotten to install the following package https://github.com/jarektkaczyk/eloquence given the $maps =[ seems to be copy-pasted from their documentation with the same no spacing between =[
A thorough installation guide can be found at the package documentation here: https://github.com/jarektkaczyk/eloquence/wiki/Installation


Python: Find smallest values in nested dict

Question: Python 2.7
This is a variation on Python: get key with the least value from a dictionary BUT multiple minimum values
I have a dictionary that looks like this:
dates = {
    'first record': {
        'first date': '1985',
        'last date': '2000',
        },
    'second record': {
        'first date': '1985',
        'last date': '2012',
        },
    'third record': {
        'first date': '1985',
        'last date': '2000',
        },
    'fourth record': {
        'first date': '2000',
        'last date': '2014',
        }
}

I am trying to retrieve the key(s) of the oldest record(s), where oldest means the earliest first date and earliest last date. In the above example, 'first record' and 'third record' would both be returned.
I'm having a hard time figuring out how to implement a solution using the itervalues/iteritems approach described in answers to similar (but less complex) questions. Could anyone offer some guidance?
Comment: It might be more beneficial to implement a class and override comparative magic methods, then sort them.
Answer: As you just want the keys:
mn_year = min((int(d['first date']), int(d['last date'])) for d in dates.values())

print(mn_year)

 print([k for k in dates
       if (int(dates[k]['first date']), int(dates[k]['last date'])) == mn_year])
(1985, 2000)
['third record', 'first record']

If the values are not related you need to calculate the min separately i.e:
dates = {
    'first record': {
        'first date': '1984',
        'last date': '2001',
    },
    'second record': {
        'first date': '1985',
        'last date': '2012',
    },
    'third record': {
        'first date': '1985',
        'last date': '2000',
    },
    'fourth record': {
        'first date': '2000',
        'last date': '2014',
    }
}

mn_first = min((int(d['first date'])) for d in dates.values())
mn_last = min((int(d['last date'])) for d in dates.values())

print([k for k,v in dates.iteritems()
       if int(v['first date']) == mn_first or int(v['last date']) == mn_last])

returns ['third record', 'first record'] as opposed to ['first record'] using the first code.
Getting a min of tuples will not get the min of each key but the min of the paired up tuple so any unique first date will not necessarily get paired with the minimum last date unless the min last date happens to be in the same dict.
Instead of looping twice to get the min we can get both at the same using a loop:
mn_first, mn_last = float("inf"),float("inf")

for d in dates.values():
    f, l = int(d['first date']),int(d['last date'])
    if f < mn_first:
        mn_first = f
    if l < mn_last:
        mn_last = l

print([k for k,v in dates.iteritems()
       if int(v['first date']) == mn_first or int(v['last date']) == mn_last])

Comment: That last bit with the comprehension in the list should be and, not or, but this works perfectly. Thanks a lot.
Comment: @AutomaticStatic, ah ok so you want the keys that satisfy both conditions?
Comment: @AutomaticStatic yep, I thought you wanted the key if it had either of the values, in that case the first code should  work as is if the values are always in pairs.


What classifier could predict spam/ham labels for SMS messages better than Naive Bayes?

Question: I have 7000 SMS messages, 6000 ham, 1000 spam. Typical messages are:
Ham: Yo, any way we could pick something up tonight?
Spam: Great News! Call FREEFONE 08006344447 to claim your guaranteed £1000 CASH or £2000 gift.

I want to implement a supervised classifier that would predict the ham/spam label given a new SMS.
The two classifiers have I tried are as follows:

Simple-predictor, where I count how many elements in the following keywords
 [
    "!", "click", "visit", "reply", "subscribe", "free", "price", "offer",
    "claim code", "charge", "stop", "unlimited", "expires", "£",
    "new voicemail", "cash prize", "special-call"
 ]

are substrings of the (decapitalized) SMS message and predict spam if the count is greater than 1, ham otherwise. The method achieves
accuracy (correct guesses ratio): 0.9742822966507177
sensitivity (correct spam guesses ratio): 0.8452380952380952

Bayes (monograms) predictor, where I split the SMS into a tokens list $L = [t_1, t_2, ..., t_n]$ (e.g. for the ham message above $L$ would be ['yo', 'any', 'way', ..., 'tonight']), compare the quantities :

$s = P(spam) \cdot P(t_1 | spam) \cdot \ldots \cdot P(t_n | spam)$,

$h = P(ham) \cdot P(t_1 | ham) \cdot \ldots \cdot P(t_n | ham)$,

and predict spam if $s > h$, ham otherwise.
$P(spam), P(ham), P(token | spam), P(token | ham)$ are estimated from the training data.
This method achieves
  accuracy: 0.9881889763779528
  sensitivity: 0.9312977099236641

when trained on 4000 messages and tested on the other 3000 messages

What new idea could I try to obtain a classifier with better prediction scores?
Note that I have already tried 'tuning' both Simple-predictor (e.g. trying different keywords list, changing the count threshold, etc.) and Bayes predictor (e.g. performance of bigrams predictor is worse due to a limited training set size) to achieve these scores. Now I am looking for a new idea.
Answer: Basically any method text classification can be applied here. If you want to stick with classical MT methods, you can try:

Different model (logistic regression, SVM),

Feature engineering (e.g., replacing all phone numbers with a special token, removing stop words, in case of discriminative models, you can weight the input with TF-IDF scores, including n-gram features),

Word embeddings (such as GloVe or FastText) as an input of a discriminative model.

If you do not care about the inference time, you can try some neural models. 7k messages should be enough to train a small LSTM classifier and definitely enough to fine-tune BERT or RoBERTa.


Inicializando Array de uma nova classe

Question: Considerando duas classes em C#:
public class aClass
{
    public string afield;
}

public class bClass
{
    public aClass[] bfield;
}

Eu pretendo iniciar a variável como
bClass cVar = new bClass();
cVar.bfield[0].afield = "text";

mas está gerando o seguinte erro durante o debug.

System.NullReferenceException: 'Object reference not set to an instance of an object.'
cVar.bfield was null.

Como eu inicio a variável para evitar este erro?
Comment: Nosso idioma é o **Português**, traduza sua pergunta.
Comment: Traduza sua pergunta, assim eu vou poder sinalizá-la como duplicata de `O que é NullReferenceException`. Respondendo: Você precisa inicializar o campo `bfield` ou na própria declaração do campo ou no construtor. Assim: `bfield = new [] { new aClass() { afield = "conteúdo" } }`
Answer: Aparentemente o seu problema é que você não inicializou o "Array", para resolver você poderia inicializa-lo antes de atribuir algum valor:
bClass cVar = new bClass();

cVar.bfield = new bfield[] {
    new aClass {
        afield = "text";
    };
};

Ou
bClass cVar = new bClass();

cVar.bfield = new aClass[1];
cVar.bfield[0].afield = "text";

Porém com toda certeza esta não seria a melhor maneira de fazer o que você quer, um array não fornece toda a flexibilidade que você provavelmente busca, talvez seja melhor você optar por uma lista, poderia fazer assim:
public class aClass
{
    public string afield;
}

public class bClass
{
    public List<aClass> bfield = new List<aClass>();
}

bClass cVar = new bClass();

cVar.bfield.Add(new aClass {
    afield = "text"
});

Answer: Acho que a implementação mais fácil é criar uma propriedade iniciada inline
public class bClass
{
    public aClass[] bfield => new aClass[10];
}

Inicializando Array de uma nova classe


SQL : Coalesce within where clause giving error Oracle (Not in DB2)

Question: I am trying to use COALESCE in WHERE clause, and I am getting the following error:

ORA-00907: missing right parenthesis Failed SQL stmt:

If I remove the COALESCE, I don't get the error anymore. I am not sure why would it give me this error as the parenthesis seem correct. Here's my SQL statement:
SELECT S.OPRID, A.OPRNAME, S.EMAIL_ADDR
FROM TABLE1 S, TABLE2 A
WHERE COALESCE(S.REHIRE_DT,S.ORIG_HIRE_DT)
        BETWEEN (TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM- DD'),'YYYY-MM-DD') - 3 DAY)
            AND (TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD'),'YYYY-MM-DD') - 1 DAY)
AND S.EMPLSTATUS = 'A'
AND A.EMPLID     = S.EMPLID
ORDER BY S.OPRID

Comment: What's with the "DAY" in the to_date calls?
Comment: Yes LitteBobbyTables, I have put both the expressions in Coalesce function.
Comment: OldProgrammer, we are subtracting 3 day from current date and checking if the dates on the table fall in the last two days.
Comment: Why would the equivalent of this query will work in DB2?
SELECT

S.OPRID ,
A.OPRNAME ,
S.EMAIL_ADDR

FROM
 TABLE1 S ,
 TABLE2 A

WHERE
COALESCE(S.REHIRE_DT,S.ORIG_HIRE_DT) BETWEEN CURRENT DATE - 3 DAY AND CURRENT DATE - 1 DAY

AND S.EMPLSTATUS = 'A'
AND A.EMPLID     = S.EMPLID
ORDER BY S.OPRID
Comment: I've seen this error pop up as a way of telling the user you have too many parentheses, not that you're actually missing one.
Comment: This error is sometimes misleading. Oracle produces this error for different kinds of syntax errors that sometimes have nothing to with parenthesis.
Comment: Can you post your *COALESCE-less* query that works in Oracle?
Comment: The issue was not with the coalesce, it was with the DAY keyword. We were trying to use it in a Oracle specific query( we were migrating from DB2 to Oracle, hence we missed removing the keyword "DAY"). The actual running query has been posted below in the answer by OldProgrammer.
Answer: Take the "DAY" word out.  That is not used in oracle in that manner:
SELECT S.OPRID, A.OPRNAME, S.EMAIL_ADDR
FROM TABLE1 S, TABLE2 A
WHERE COALESCE(S.REHIRE_DT,S.ORIG_HIRE_DT)
        BETWEEN (TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM- DD'),'YYYY-MM-DD') - 3 )
            AND (TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD'),'YYYY-MM-DD') - 1 )
AND S.EMPLSTATUS = 'A'
AND A.EMPLID     = S.EMPLID
ORDER BY S.OPRID

"DAY" is a keyword to be used as part of the EXTRACT function - see Here
The default unit of subtraction from a date field is already in units of days.  I am not familiar with DB2, but I am assuming that your usage of DAY is a DB2-specific attribute.  That is probably not portable SQL.  Yes, oracle's error messages can be confusing at times.
Now that I think about it, you can replace all that "TO_DATE" stuff with just:
BETWEEN TRUNC(SYSDATE)-3 AND TRUNC(SYSDATE)-1


Going Up Theorem and Affine Sets.

Question: So for an affine scheme, we know that this is true: Suppose that $k$ was algebraically closed.
Let $X$ and $Y$ be affine schemes and $\phi: X \rightarrow Y$ be a polynomial map with the corresponding coordinate ring homomorphism $\Phi: Spec(Y) \rightarrow Spec(X)$. I can show that $\Phi$ is going up, then $\phi$ is a closed map. (Follows from the going up theorem and surjectivity is given so that forces $\phi$ to be closed and vice versa)
But if what if $X$ and $Y$ were affine sets(So $X$ and $Y$ could be reducible) instead? If we have a polynomial map $\phi: X \rightarrow Y$ and the corresponding coordinate ring $\Phi: k[Y] \rightarrow k[X]$. Does $\Phi$ going up imply that $\phi$ is closed and vice versa?
This required a bit of thought for me. I thought it would be exactly the same for an affine scheme, but the inclusion map is going up does not imply we have surjectivity. So I'm not exactly sure if this is true or not. Any ideas?
Answer: Let $f:X \to Y$ be a morphism of finite type of noetherian schemes. Then $f(X)$ is a constructible subset of $Y$. If $f$ is going-up, that is, if $f(X)$ is closed under specialization, then $f(X)$ is closed in $Y$. This is stated in Hartshorne, Algebraic Geometry, II, ex. 3.18c, ex 3.19.
Now if $X,Y$ are affine algebraic schemes over a field $k$ then $X$ and $Y$ are obviously noetherian. Furthermore $f$ is of finite type, as the morphisms $X \to k$ and $Y \to k$ are of finite type and $Y \to k$ is separated. (Hartshorne ex. II, 4.8d).
Comment: I'm not sure if you read the question. My question was if $X$ and $Y$ were just affine algebraic sets (so we can have that $X$ and $Y$ are reducible in the Zariski topology). Does the question I have still hold?
Comment: Let $X$ be an "affine algebraic set" in your terminology. Then I understood it as meaning $X={\mathrm spec}(A)$ where $A=B/I$ and $B=k[X_1,\ldots,X_n]$ is a polynomial ring over $k$ and $I \subseteq B$ an arbitrary ideal. This is exactly what I called "affine algebraic scheme" in my answer.

Going Up Theorem and Affine Sets.


Suggest me any option for using  or  tag for android to make justify alignment for text within strings file

Question: I have string content with numbering(list items) i wanted to make justify alignment for each and every list..Please suggest me with an option for doing it..I'm populating the content from strings file into textview.
<string name="sample">\n Title

  \n\t1.Sub heading 1
   ggsdhsfdjshf
  \n\12.Sub heading 2
   hjdjkfhdishfidhgd
   fdgvbfhgfnn

 </string>

<TextView
        android:id="@+id/textView121"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:gravity="left|center_horizontal"
        android:text="@string/sample"
        android:textSize="@dimen/content_text_size"
        android:textColor="@android:color/black"
        android:textStyle="normal" />

For this i want the each points below sub heading to be justified..Can suggest now?
Comment: fro justify Text see http://stackoverflow.com/questions/19873733/justifying-text-in-android
Answer: You can use Html string as the text to display in TextView
Try this.
String htmlString = "This
is
test!";
textView.setText(Html.fromHtml(htmlString));

Answer: Use ArrayList in Android.
You can also get the the strings from string file.
You can add item using
ArrayList<String> arraylist = new ArrayList<String>;
arrylist.add("1.Item1");
arrylist.add("2.Item2");

Comment: how this can justify text?
Comment: Can you please share some xml code which you have implement?
Comment: I don't ask this question, i just serious because OP want justify text and you answer with initialize List.


How do I tie a note with other notes in between without creating a slur?

Question: For example, let's say I have four quarter notes, only I want to first note to span the length of all four, and I want the other three notes to be played individually with no slurring involved. Is there a way to write this in music notation?
Answer: Yes--put the first note in a separate voice from the other 3, and make that first note a whole note instead.


Why is POSIXct converted to numeric when converting list to vector

Question: I have a list redDressTweets of statuses. status is a class with a field created. I am trying to form a list of times in which tweets were posted. Here is how I'm trying to do it
times <- unlist(lapply(redDressTweets, function(tweet) {tweet$created }))

The output is a vector of numbers:
[1] 1478044029 1477954062 1477909847 1477887746 1477832560 1477640940 1477640939
[8] 1477628031 1477540826

But the class of redDressTweets[[1]]$created is "POSIXct" "POSIXt".
Why is this happening and how do I stop it from converting POSIXct to numeric?
Answer: You can reproduce more easily like this:
x <- list(as.POSIXct("2016-11-02"), as.POSIXct("2016-11-03"))
unlist(x)
#[1] 1478041200 1478127600

unlist combines the values inside the list. The internal representation of POSIXct variables are numeric values. They only are a POSIXct variable due to attributes, most importantly the class attribute. A list can hold all kinds of objects. The documentation says:

Where possible the list elements are coerced to a common mode during
  the unlisting, and so the result often ends up as a character vector.
  Vectors will be coerced to the highest type of the components in the
  hierarchy NULL < raw < logical < integer < double < complex <
  character < list < expression: pairlists are treated as lists.

Note that it says "common mode", not common class. Since a class could be anything and could enforce any kind of underlying structure (e.g., it might not even be possible to combine two objects of the same class in a meaningful way), unlist just strips all attributes (except for a list where all elements are factors). It would be possible to handle POSIXct values like factor values, but that's not the case (and might have performance implications).
You can't avoid this, but fix it easily:
y <- unlist(x)
attributes(y) <- attributes(x[[1]])
y
#[1] "2016-11-02 CET" "2016-11-03 CET"

Obviously this assumes that all list elements have the same timezone attribute.
Comment: unless it's a named list. In this case you have to add poix-attribute, instead of just setting it:
  attributes(y) <- c(attributes(y), attributes(x[[1]]))


Making a block's content's all show in one row

Question: I have a block that is made up of 3 subblocks: an image block, a menu block and a views block.  I combine the three blocks programmatically like this:
<code>function inflatable_icons_menu_combo_block_content() {
    $block = block_load('views', 'icon_views-block_5');
    $block2 = block_load('menu', 'menu-facebook-menu');
    $block3 = block_load('imageblock', '1');
    $output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block3, $block, $block2))));
    return ($output);
}
</code>
Now this block shows up with all three blocks and looks like this:

But I need the image to show up on the same row as the other two blocks.  What would be a good way to make it all show up on one line?
Comment: Do you have a public URL we can look at, or even the markup?
Answer: This appears to be a stylesheet issue. Everything should 'float:left'. If this has already been done, then your first block is too wide and is forcing the other blocks to displace down.
Experiment with maxwidh on your first block to leave room for the other two.


ViewBag e ViewData Dúvida

Question: Estou iniciando a carreira em desenvolvimento, e no projeto aqui da empresa, encontrei uma ViewData sendo utilizada, e pelas minhas pesquisas eu não consigo atualizá-la em um ActionResult no controller. Acabei testando com uma ViewBag também, onde também fui infeliz. Lembrando que o valor destas duas serão atribuídos na renderização da página, correto? (Me corrijam, por favor)
Como eu poderia fazer para recuperar este valores, via ajax, até outra questão, pois minhas funções .js estão em um outro arquivo, não localizados dentro do Index.cshtml
Edit 1: No meu cenário atual, está acontecendo o seguinte:
public IActionResult Index()
{
   // Crio minha ViewData
   ViewData["NomeDaViewData"] = null;
   UpdateViewData(); // aqui onde vou atualizá-la

   return View();
}

private void UpdateViewData()
{
   var dado = _myReposiory.BuscaValor();
   ViewData["NomeDaViewData"] = dado;
}

Logo após no meu Index jogo o valor da ViewData em um input hidden, onde consigo trabalhar no meu arquivo .js tranquilamente. Mas vejo que posso melhor esta maneira, só não sei como.
Comment: quando você diz "via ajax", quer dizer que no `javascript` precisa de um valor que foi passado por `ViewData` ou `ViewBag` é isso?
Comment: Voce quer passar o valor do ajax que esta na View para o Controller?
Comment: Isso mesmo @RicardoPontual.
Comment: Gostaria de passar um valor que está no meu Controller para a minha View @RobsonOliveira
Answer: Os valores da ViewData, ViewBagou ainda TempData não podem ser acessados no arquivo javascript, o que dificulta a utilização posterior numa chamada Ajax.
Uma solução é ler no arquivo .cshtml, e colocar numa variável javascript, que possa ser acessada depois por seu arquivo, por exemplo:
arquivo.cshtml:

ViewBag e ViewData Dúvida


How to convert sourceSets from a project to kotlin kts

Question: I have a multi module gradle project. I'm converting all build.gradle configuration files to Kotlin kts.
But I can't find how to convert this code below to kotlin kts.
I tried to place groovy code inside kts and checked the documentation, but I could't find anything.
    testCompile project(":entities").sourceSets.test.output

Answer: After some investigation, I found that the syntax in this area has changed recently.

This gives some history:
https://github.com/gradle/kotlin-dsl/issues/577
The Kotlin DSL 1.0-RC3 introduced some breaking changes:
https://github.com/gradle/kotlin-dsl/releases/tag/v1.0-RC3
the sourceSets collection does still exist on the project:   https://docs.gradle.org/5.0/dsl/org.gradle.api.Project.html#org.gradle.api.Project:sourceSets)
this collection can be cross-referenced via a peer project

I tried a few variants, and discovered that:
dependencies {
  testImplementation(project(":entities").sourceSets["test"].output)
}

results in
testImplementation(project(":entities").sourceSets["test"].output)
                                     ^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
                                                        public val Project.sourceSets: SourceSetContainer defined in org.gradle.kotlin.dsl

But the following works just fine, as long as the Java plugin is enabled:
val entityTests: SourceSetOutput = project(":entities").sourceSets["test"].output

dependencies {
  testImplementation(entityTests)
}

Comment: For me it doesn't work, I still get `Unresolved reference: sourceSets`. But when I add `java` before `sourceSets`, it works, however, I get build error which didn't happen before.
Comment: The sourceSets property of a project is only activated when the Java plugin is enabled. See https://docs.gradle.org/5.4/dsl/org.gradle.api.Project.html#N159F7. I'll update my answer to include this. I might be able to help with the build error if you provide some further information.
Comment: Only works if the val is not declared in dependencies block (just FYI)
Answer: This also works for me.
dependencies {
    testImplementation(project(":entities").dependencyProject.sourceSets["test"].output)
}


Visualize a continuous variable against a binary variable

Question: I want to visualize a continuous variable, BMI index, against a binary variable, heart disease.
I want to check by visualization in my data, if heart disease is more common among higher BMI index cases than lower BMI. I used catplot boxplot:
Plot = sns.catplot(x="heart_disease", y="bmi", kind="box",
                   data=data);
Plot.fig.suptitle('Correlation between Heart Disease and
                   BMI index', size=15, y=1.12);

From the result plot, the question to the answer is no. Do you think using boxplot is a good idea? is there another plot type that could do a better job for the question at hand ?
Answer: Boxplots lose an enormous amount of information, since they condense all data into just five summary statistics (the median, the box and the whiskers) plus what is unhappily called "outliers".
I would always go with beanplots, also known as violin plots. If you want, you can always overlay boxplots, or the original data. If you do add the original data, jitter them horizontally to avoid overplotting. (And if you plot both boxplots and the original data, suppress the "outliers" plotted by the boxplots, because then you would have the same data plotted twice.) This answer of mine gives an example (the second plot) and R code to create it. In Python, it seems like you could use statsmodels.graphics.boxplots.beanplot or seaborn.violinplot.
Visually checking relationships is a very good idea. However, it is of course subjective. If you want an objective result, consider running a logistic regression of heart_disease on BMI. Consider using splines to capture any nonlinearities.
Answer: I usually do overlayed histograms.
E.g. https://stackoverflow.com/questions/49533978/multiple-histograms-in-python
Comment: +1. Alternatively, since the OP has only two groups, you could do a [back-to-back histogram](https://www.google.com/search?q=back-to-back+histogram).
Answer: Depending on the sample size, you might do a strip plot (where every observation is a dot in the graph), maybe with jittering or transparent points.
I second Stephen's recommendation of using splines -- the box plot you've shown seems to show that the median and quartiles are roughly similar, but that the outliers are quite different. Oddly, the high outliers all show no heart disease.  Was N much larger for that group?
Answer: Variants on this question arise frequently: see for example Histogram or box plot, to compare two distributions of means?.
I want to add to the fine answers to date, first by emphasising some small tensions here:
Show the data, but show summaries and expose detail too You should want the data to be seen clearly, but also allow summaries and important detail to jump out at readers. Important detail could be outliers, gaps and spikes as well as skewness and long tails and additive or multiplicative shift between groups. Substantively (meaning, scientifically or practically), people often want to focus on differences in distribution level (means, medians, whatever) between two groups, but differences in distribution spread and shape could complicate or even undermine any comparison based on levels alone.
Familiar method or novel method? There is some advantage to simple methods that are familiar to people in your field, yet there are many small new ideas even on this basic problem that may deserve experiment. Histograms are traditional but often work fine. Box plots for some fields have become traditional too, but their limitations are shown by many small variants aimed to enhance them. Just reference and explain any novel method you use.
Show the data directly? Showing the data directly sounds an obvious goal, but some methods don't do exactly that, in pursuit of clarity or simplicity. Jittering to shake identical points apart and smoothing the density function are contradictory moves, but united by a motive of making overall patterns easier to grasp.
Use a transformed scale? People in different fields can range from those using a transformed scale immediately as a matter of standard measurement convention (pH, Richter scale, decibels) to those suspicious of or unfamiliar with any kind of transformation. Most statistically-minded people are aware of the possibility of showing data on a transformed scale, yet also variable in their enthusiasm for using one in practice.
Here I use a hybrid display with elements of quantile plot (i.e. a plot of all values against a cumulative probability scale), box plot (starting with the idea of median and quartiles in a box), and extra annotation. The goal -- easy to explain, but harder to achieve well -- is to allow readers to see summaries and detail at the same time. Back in 1979, Emanuel Parzen suggested quantile-box plots (reference below). The display below is similar in spirit. Parzen's own examples weren't especially impressive and his readers were perhaps distracted by an attempt in that paper to represent exploratory data analysis in fine mathematical clothes, a project resisted robustly (!) by John W. Tukey in discussion. The name quantile-box plots has also been used since for other related but not identical plots.
The reaction time means (units not stated) from the CV thread cited at the beginning of this answer will serve fine to show some technique. Here is one of many plots that could be shown.

These are small samples, 20 values in each, and so it's certainly possible to show each value distinctly without distortion. The design copes reasonably with many more points (200, 2000, 20000, ...) in so far as major details (e.g. marked outliers if they exist) will still jump out.

Offsetting point markers according to the associated cumulative probability (rank, equivalently) avoids or at least reduces any call for jittering. If the underlying distribution is fairly smooth, so too will be the quantile trace.

The cumulative probability scale here is linear, but other scales could make as much or more sense (e.g. transformed to unit normal deviates).

Parzen superimposed quantile and box plots (that was much of his point, that quantile and box plots share links to cumulative probabilities) and I do that too sometimes. Here they are juxtaposed.

Box plots are presented but here have only a summary role (and so can be made quite thin without loss). As the quantile plot shows all the data, we needn't concern ourselves with any rule or convention about which data points are shown individually. The most common convention used seems to remain the convention that Tukey settled on after some experiment, namely to show all points individually that lie more than 1.5 IQR from the nearer quartile. I find in my reading and discussion that (a) teachers and researchers are often poor in explaining the convention that they used, and readers are often less familiar with such conventions than is assumed; (b) there is even a tendency to take that rule of thumb for which data points are to be shown individually as a hard-and-fast criterion for outliers, which is unfortunate. It's partly a matter of taste, but I often like to revert to a practice of just taking the whiskers out to paired percentiles in the tails, which is quite often done in various literatures. Which percentiles is not, and need not be, standardized and that should not matter much so long as a choice is explained.

I add longer lines showing the means. Naturally, that could be done too with distinct point symbols. Show geometric means, midmeans or anything else instead if that fits the problem or the data better. I note a bizarre but common habit of accompanying discussions of t-tests or analysis of variance with box plots that don't show means too; that is much better than no graph at all, but is like Romeo and Juliet without Romeo and Juliet.

As above, this display is compatible with transformations, the major detail to be clear whether log(mean) or mean(log) is being shown. On a logarithmic scale showing geometric means to compare with medians is a really good idea.

As a matter of record, I show Stata code I used. If your favourite software doesn't make something like this fairly easy, you need a new favourite.
clear
input float(A B)
.8792397 .9964306
.5845183 .7269523
.8092829 .9343457
.6869933 .9014275
.7223416 .7856004
.6551149 .8224649
.6549308 .8670868
.6560364 .7797318
 .602458 .8236209
.6110293 .8315545
.6373121  .774942
.6295298 .8020451
.6144096 .7622244
.6776401 .8087511
.6165493 .7977815
.6055175 .7647625
.6318304 .7652289
.6315798 .7064912
.6329535 .7116355
.5817685 .7861449
end

stripplot A B, cumul vertical box(barw(0.1))  refline pctile(5) boffset(-0.1) ///
height(0.4) yla(, ang(h)) ytitle("") ///
note("box and whiskers show median, quartiles, 5% and 95% percentiles" "longer lines show means") ///
xla(, tlc(bg)) ytitle("more explanation goes here")

Parzen, E. 1979. Nonparametric statistical data modeling. Journal of the American Statistical Association 74(365): 105–121. https://doi.org/10.2307/2286734


Data snooping in selecting between ML models

Question: If you train a lot of machine learning algorithms on a problem, svm, nn, rf,...via i.e. caret, and I mean a lot of it,like hundreds or thousands (not that hard,considering all parameter tuning in validation), eventually you will find one that will work.
But that is data snooping. As you have trained so much models, I think that you have to test the hypothesis that you had been lucky, maybe with White Reallity Check test or Hansen Superior Predictive Ability test.
Around 99.9% of the papers, articles, posts,.... didn't use White's or Hansen's tests (0.1% are papers regarding stock trading via ML). I suppose this is because, on a normal basis, we use to train a few models(really?).
The question is:
Do you have any idea about the number of models required to consider data snooping terrible effects?
This is, if i'm choosing between 3 models, I think that the possiblity of getting good results by chance is low. But in choosing between 30? 300? 3000?
Answer: A lot of effort is underway to address this issue of data dredging with the techniques from differential privacy. The algorithm allows repeated reuse of the test set in evaluating different models, and in the process keeps enough details about the test set hidden that the final model does not overfit.
See the tutorial "Rigorous Data Dredging: Theory and Tools for Adaptive Data Analysis" in this yr's ICML
http://icml.cc/2016/?page_id=97
Dwork, C., Feldman, V., Hardt, M., Pitassi, T., Reingold, O., & Roth, A. L. (2015, June). Preserving statistical validity in adaptive data analysis. In Proceedings of the Forty-Seventh Annual ACM on Symposium on Theory of Computing (pp. 117-126). ACM.
Comment: To be clear on my side, I'm not saying there's nothing to it, but that I don't get it yet. There's a lot of things that work great that I don't get! I have it in my mind to thoroughly investigate this some day, but given that I haven't had the time, if someone else has I'm very interested.
Comment: @Cliff AB I hear you and I'll add an example to show how I think it should work. But your skepticism is justified, given what little else is out there right now.
Comment: Yes, and if you can explain why their method works, please answer the [following question](http://stats.stackexchange.com/questions/223188/has-the-journal-science-endorsed-the-garden-of-forking-pathes-analyses). I'm not saying there's nothing to this paper, but I am saying it doesn't make sense to me. I am very interested if someone can explain it a clear manner.
Comment: @Cliff AB I think we crossed path before. Let me make it clear, I'm not saying differential privacy is the (only) holy grail in machine learning that completely solves the data snooping problem. But, this is a pretty neat trick on an otherwise intractable problem, isn't it. Before embarking on a 1000 model searches, this algorithm provides at least a sort of protective shield against overfitting.
Comment: If I understood it, the idea is: add noise to the test set performance and use this as the real test set performance for that model. You've just made it re-usable. Depending on the similarity of train and test performances, you add noise in  one way or another. I don't know  if it works, but sounds good. Is this right?
Comment: @PeterTschuschke That's the general idea, but they pick Laplacian noise because of certain sample complexity guar.entee. See Theorem 14 in the paper cite above.
Comment: I must be missing something, why in  slide 71 they pick normal noise? if (abs (sample_mean-holdout_mean)) < randon.normal.... @horaceT
Comment: The type of noise and the dispersion of the noise variables determine the guarantee of differential privacy. See their Thresholdout algorithm, and Thm 9 following it for proof of the guarantee.
Comment: I believe most of the differential-privacy theory uses Laplace noise for proofs. But I think the "ThresholdOut" demos do use Gaussian noise in practice (their Python demo for the Science paper, at least).
Comment: @GeoMatt22 I've seen gaussian noise being used, too. I think the deal is about satisfying the $\delta, \epsilon$ bound.
Comment: I'm having troubles trying to implement that. Let's say i have some classification problem, and i want a model, lets say a SVM with linear kernel.It only has a paremeter C.  In the non-private way,roughly, i did cross-validation, select a value for C, try in the test set, and if test set accuracy is similar to cross validation estimation of the test error, that's the end.
Comment: But now, in this differencial privacy new frame,¿how should i proceed? I try a lot of C values on training set, get my training errors, and test them one  by one against the test set returning the test score according to the thresholdout algorithm?
I think that this has no sense, because how do i choose between models with different C's? According to the test set score (which has noise) ?
And how do i choose sigma and threshold? ¿sigma = 1.0/sqrt(len(sample))? threshold=k*sigma, k=2 or 3?


iCloud and Core Data Error (Ubiquity: Didn't get baseline metadata back from metadata url)

Question: I'm having a bit of trouble trying to get iCloud working with my app. I tried following Tim Roadley's example here, but still get the log below appearing whenever the app is launched via Xcode (syncing did work briefly, but has ceased to work now).
[PFUbiquityBaseline metadataFromCurrentBaselineForStoreWithName:modelVersionHash:andUbiquityRootLocation:withError:](1091):
CoreData: Ubiquity:  Didn't get baseline metadata back from metadata url:
file://localhost/private/var/mobile/Library/Mobile%20Documents/<TEAM
ID>~samburnstone~Staff-Manager/Logs/.baseline/current.nosync/<TEAM
ID>.samburnstone.StaffManager/g9TNo_uNFuNyltbjcAmDaFE7wl~6a2eGmW6uKIZCC1s=
/baseline.meta Error: (null)

(TEAM ID is my alphanumeric sequence of characters that can be found in Apple's Member Center)
If anyone has any idea what could be causing this I'd be very grateful.
Thanks!
Comment: I'm seeing similar problems. still no luck in solving it.
Comment: Yeah. iCloud seems to occasionally work correctly and then other times gives me this error. I've given up for now and might give it another go in the future ;)
Comment: @Darknight that is pretty much the consensus with iCloud+CoreData :-)
Answer: Check if you the following applies to your issue - it helped me:
http://mentalfaculty.tumblr.com/post/25241910449/under-the-sheets-with-icloud-and-core-data
Without seeing your code it's hard to comment on the actual issue.
Answer: I think the problem is that it needs to be unjammed via a force eviction.
My answer references the User Dictionary, related to Text Input, as a concrete example.  This answer has many specific path names which I have kept so it is easy to see context and structure of files/directories, but your paths will be different.
Blogger AtomicBird has looked into iCloud mechanics and shows that the metadata file comes from the baseline.zip file, which can be found and expanded:
find ~/Library/Mobile\ Documents -name baseline.zip
cd $HOME/"Library/Mobile Documents/com~apple~TextInput/Dictionaries/.baseline/UserDictionary"
cd SAlQVUhF7208e6_gvZx_zdKx1U1AzKGem3HO2pLKjgY=

I have file baseline.zip under subdirectory SAlQVUhF7208e6_gvZx_zdKx1U1AzKGem3HO2pLKjgY= in my case.
Unzipping the baseline.zip file shows the metadata in question.  If you have the plistutil tool (via brew package manager) you can see its contents:
plistutil -i metadata
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>$version</key>
    <integer>100000</integer>
    <key>$objects</key>
    <array>
.
.

The contents are not documented officially.
When the problem occurred, it must have been related to the contents of this file.  The iCloud sync logic has much improved over the last few years but it is still not perfect.
When things are jammed up, we need to force the system to get a fresh file from iCloud.
It is possible to force this via a command line tool, brctl with the evict operation.  For example:
brctl evict "/Users/faisalm/Library/Mobile Documents/com~apple~TextInput/Dictionaries/faisalm~79CD0053-95EA-5F84-9773-5D1269B74D13/UserDictionary"

It is also possible to then use the brctl dump command to see how it managed to sync up.  I have in my output the snippet referencing UserDictionary
:3 queries: metadata:idle etag:hg apps:{} InCloudDocsZone bundles:{com.apple.inputmethod.TCIM, com.apple.Preferences, com.apple.InputMethodKit.UserDictionary, com.apple.InputMethodKit.TextReplacementService, com.apple.kbd, com.apple.inputmethod.SCIM}>

Once unjammed, it should work ok.
Answer: You can also try this below code and call it every time you launch the app:

PFUbiquityBaseline metadataFromCurrentBaselineForStoreWithName:modelVersionHash:andUbiquityRootLocation:withError:

if (![[NSFileManager defaultManager] fileExistsAtPath:[[NSURL fileURLWithPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"Logs"]] URLByAppendingPathComponent:@"current.nosync"].path])
    {
        [PFUbiquityBaseline metadataFromCurrentBaselineForStoreWithName:@"AppStore" modelVersionHash:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] andUbiquityRootLocation:[[NSURL fileURLWithPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"Logs"]] URLByAppendingPathComponent:@"current.nosync"] withError:nil];
    }


Melhor prática para registrar log do sistema

Question: Qual a melhor prática para se registrar um log de sistema?
    public void inserirLog(string Acao)
        {

        StringBuilder csql = new StringBuilder();
        csql.Append("insert into Log (Acao,data) values(");
        csql.Append(@"""" + Acao + @""",");
        csql.Append("'" + DateTime.Now + "')");
        Conexao conn= new Conexao();
        conn.ExecutarComando(csql.ToString());
        }

Hoje faço desse maneira, ou seja, a cada ação que quero que apareça um log, gero um insert no banco de dados. Essa maneira além de custosa gera muitas requisições ao banco de dados então acredito não ser a mais viável, estava pensando em registrar cada log em um arquivo texto, então depois transmitir esse arquivo, será que essa seria uma boa forma?
Obs:. Os logs são para registro de ação , por exemplo: "Abriu tela X", "Logou no Sistema", "Saiu do Sistema", etc.
Meu sistema possui muitas funcionalidades, então esse log seria para mapear o que vem sendo utilizado pelos clientes.
Comment: Não tem como definir o objeto `Conexao con` para uma váriavel `private` e se ela já estiver setada, você só reaproveitara a mesma "conexão" se houver mais de um LOG na mesma requisição?
Comment: Obrigado pela Resposta, vou tentar seguir o conselho do amigo abaixo, mas mesmo assim obrigado pela opinião. Bem visto, não preciso instanciar a conexão a cada log poderia ter utilizado a mesma.
Answer: A melhor prática é não reinventar a roda. É usar uma ferramenta de log que foi bem pensada e testada por profissionais que se dedicam a isto. E usar estas ferramentas prontas de acordo com as recomendações específicas.
Claro que que alguns casos é interessante criar um sistema próprio, mas você precisa ter um motivo para isto. Eu não acho que soluções prontas resolvem todos os problemas. Mas não parece ser o seu caso, acho que você poderia muito bem usar algo pronto que resolve essas questões que você está preocupado.
Estas ferramentas oferecem soluções para realizar buffering, fazer balanceamento de carga e resolução de falhas, escrevem assincronamente e permite escolher vários alvos para persistir o log.
Antes de passar algumas soluções quero dizer que log em banco de dados tem que ser bem pensado mesmo, imagine que o banco de dados pode ser uma grande fonte de problemas em certas circunstâncias (a não ser que seja um SQLite, por exemplo), aí você precisaria logar os problemas ocorridos no seu log. Complica, né?.
Alguns frameworks de log conhecidos para .NET:

Log4Net - um dos mais conhecidos e usados baseado no Log4J, parece abandonado.
NLog - Um dos mais modernos e conhecidos. O único que eu usei de fato e parece ser o mais popular até porque também costuma ser o mais eficiente.
Enterprise Library - Solução oficial da Microsoft mas não resolve bem algumas coisas.
Elmah - Bastante recomendado para uso com ASP.NET. Muito simples de usar porque ele injeta o que é necessário, acho um pouco pesado.
.NET Logging Framework - Outro bem recomendado e tem algumas características diferentes - não sei se existe ainda.
SeriLog - Não conheço mas é mais um para investigar e ver se te atende melhor, muita gente usa e considera bom.
Audit.NET - É uma opção mais nova e moderna mas quase não usado até onde eu percebi.
ASP.NET - Se precisa de algo simples e para web não precisa de nada externo, o próprio ASP.NET tem um sistema de log simples.
.NET - Para aplicações em geral tem uma pequena ajuda para cenários bem simples.

Existem outros mais simples mas que são pouco suportados e vários abandonados. Existem também algumas soluções mais completas mas pagas, raramente valem à pena.
Em geral essas soluções só cuidam do log em si, exigindo que você invoque sua execução não de interceptação que provavelmente requerem ferramentas de reescrita do assembly usando técnicas de orientação a aspectos. Veja resposta complementar do Cigano Morrison Mendez.
Existem ainda algumas ferramentas para analisar estes logs, o que ajuda justificar a escolha por uma solução padrão.

Sentinel - Open Source
Chainsaw - Open Source
Gibraltar - Gratuito
LogEntries- Pago
Reflect Insight - Pago

Coloquei no GitHub para referência futura.
Comment: Como a pergunta possivelmente envolve interceptação de ações específicas, seria bom aprimorar a resposta pra incluir o Castle Windsor e o Castle Dynamic Proxy.
Comment: @TobyMosque a sugestão é boa mas para ter uma solução de *log* robusta e mais poderosa precisaria de mais que este recurso. Posso estar enganado mas parece que isto não foi feito para funcionar como um *log* do dia-a-dia.
Comment: @AndrewAlex neste caso você usará a FileSystem como alvo. O envio deverá ser feito de forma independente. Mas estude todas as opções e teste para ver o que melhor te atende.
Comment: @Marconi sim, são, em geral você chama um método dele passando a informação que você quer *logar*. Mas você pode automatizar isto. Tem vantagens e desvantagens em fazer isto. Veja resposta do Cigano para ver uma das formas mais conhecidas de como fazer a interceptação da execução e "logar sozinho". A documentação de alguns deles também dão dicas de como fazer isto.
Comment: Eu adicionaria a esta lista o TraceSource... não sei pq utilizar uma ferramenta de terceiros quando o proprio dotNEt disponibiliza uma solução bem completa.
Comment: Obrigado irei dar uma olhada nesses Framework.

O que necessito é apenas, salvar cada log de ação para saber o que os clientes estão utilizando no sistema.

Por exemplo: "Abriu tela X", "Logou no Sistema", "Saiu do Sistema", etc.

Obrigado pela resposta por enquanto.
Comment: @CiganoMorrisonMendez responda com esta parte. Embora tenha lido alguma coisa eu não os conheço, provavelmente falaria besteira.
Comment: @bigown Vou complementar a sua.
Comment: @bigown, eu particulamente não vejo muita diferença entre o TraceSource o Log4Net, pelo menos com a minha pouca experiencia com ambos. a unica grande vantagem do TraceSource, é a configuração inicial que é muito simples.
Comment: Acho que ele quis log para eventos de DB...
Comment: Os logs são para registro de ação mesmo, or exemplo: "Abriu tela X", "Logou no Sistema", "Saiu do Sistema", etc

Meu sistema possui muitas funcionalidades, então esse log seria para mapear o que vem sendo utilizado pelos clientes.

Hoje salvo cada ação no BD, e depois gero um csv e transmito para o servidor.
Comment: @Rod não foi o que eu entendi, em geral *log* dos eventos do DB são gerados pelo próprio DB, ninguém precisa fazer nada a não ser consultá-lo e mantê-lo.
Comment: Entendi, pensei que fosse para auditoria, mas as ações, você poderia usar google analyctics, saberá quais as urls mais acessadas...se fosse só para verificar o que mais ele está usando...
Comment: @bigown sim...+1 para sua resposta, gostei do NLog, principalmente por enviar e-mail quando ocorre erros, isso é muito útil ao meu ver =)
Comment: @bigown Os links passados seriam frameworks de logs ?
Como ele registraria uma ação minha?
Comment: @bigown Muito obrigado eu não sabia nem dá existência disso, com certeza estarei estudando alguns.
Answer: Como o que você quer registrar envolve interceptação de ações, o correto é combinar alguma solução de log (resposta do @Maniero) com proxy dinâmico, sendo esta interceptação feita pelo Framework Castle Dynamic Proxy. Entretanto, por ser uma aplicação Web, você terá que registrar os gatilhos usando um contenedor (container) de inversão de controle, neste caso, o Castle Windsor.
No tutorial do próprio Castle Windsor, na parte 5 dele ensina a fazer exatamente o que você quer.
Se você quer registrar apenas persistências e seleções em banco e sua ideia é usar o Entity Framework, use o EntityFramework.Extended, que tem o recurso de log de auditoria.
Comment: @AndrewAlex esta solução é para você pode automatizar a execução do *log* assim você nem precisa ficar chamando nada, você não mexe no seu código. ele *log* por conta própria. Pode ser que você não precise disto mas pode ser bastante útil. Realmente entenda suas vantagens antes de descartar, pode lhe poupar bastante trabalho.
Comment: No meu caso a aplicação é WinForms, sendo que vou salvar esse arquivo de log diariamente e depois transmitir para o meu servidor ftp("Caso o cliente esteja conectado"). Então sua sugestão não entraria no contexto correto?
Comment: @AndrewAlex E por que não entraria?
Comment: Vou estudar um pouco esse então também, entendi errado achei que quando você comentou "por ser uma aplicação Web" estava se referindo que precisaria utilizar uma tecnologia Web.
Comment: Não, não. Apenas diagnostiquei o que você estaria usando e dei alguns atalhos ;)
Comment: @bigown Certo, não irei descartar, Agora com essas respostas Consegui bastante material de estudo. E vi que preciso entender melhor do que se trata o "LOG" em si.
Comment: @CiganoMorrisonMendez Você teria mais algum exemplo para utilizar o Castle Dynamic? Não consegui entender como "capturo" automaticamente a execução do log.
Utilizando o log4net, já consegui um avanço considerável, porém agora resta automatizar o processo de *logar*.
Comment: @AndrewAlex Seria por interceptação de método, [conforme descrito aqui](http://docs.castleproject.org/Windsor.Windsor-Tutorial-Part-Five-Adding-logging-support.ashx). Repare que a solução combina o Castle Windsor com o Castle Dynamic Proxy.


React typescript leaflet draw: unable to toggle button

Question: In a React + Typescript web application, I'd like to be able to show/hide the leaflet-draw "Draw polyline" button programmatically.
To perform this task, I acted on the EditControl's draw properties, as in this example (code sandbox at: https://codesandbox.io/s/leaflet-toggle-button-0hbr0?file=/src/MyMap.tsx:0-1257 )
import React, { useState } from "react";
import { MapContainer, TileLayer, FeatureGroup } from "react-leaflet";
import "leaflet/dist/leaflet.css";
import "leaflet-draw/dist/leaflet.draw.css";
import { EditControl } from "react-leaflet-draw";
import "leaflet-draw";

const MyMap = () => {
  const [showButton, setShowButton] = useState<boolean>(true);

  return (
    <>
      <button
        onClick={() => {
          setShowButton((oldValue) => !oldValue);
        }}
      >
        Toggle
      </button>

{"Show button: " + showButton}

      <MapContainer
        center={[37.8189, -122.4786]}
        zoom={13}
        style={{ height: "300px" }}
      >
        <TileLayer
          attribution='© OpenStreetMap contributors'
          url="http://{s}.tile.osm.org/{z}/{x}/{y}.png"
        />
        <FeatureGroup>
          <EditControl
            position="topright"
            draw={{
              rectangle: false,
              circle: false,
              circlemarker: false,
              polygon: false,
              marker: false,
              polyline: showButton
            }}
          />
        </FeatureGroup>
      </MapContainer>
    </>
  );
};

export default MyMap;

The button is originally displayed, and, after clicking the "Toggle" button, it correctly disappears.
Unfortunately, pressing the "Toggle" button again, it doesn't appear any more.
What is the proper way of achieving the correct behavior?
EDIT: according to https://github.com/Leaflet/Leaflet.draw#user-content-example-leafletdraw-config ,

You can change a draw handlers options after initialisation by using the setDrawingOptions method on the Leaflet.draw control.

Unfortunately, I don't manage to call that method, either.
Answer: I had a similar problem.
As a workaround, I'm setting draw options with an added key which is always different to bypass the prevProps VS props check. See below:
setDrawOptions({
  ...drawOptions,
  polyline: showButton
  bugWorkaround: Math.random(), // TODO remove when lib bug is fixed
});

That's really hacky but should get you going until it's fixed.


Undesired output (Levels) while selecting from R dataframe

Question: Part of my code is similar to following:
id.row <- c("x1","x2", "x10", "x20")
id.num <- c(1, 2, 10, 20)
id.name <- c("one","two","ten","twenty")
info.data <- data.frame(id.row, id.num, id.name)

req.mat <- matrix(1:4, nrow = 2, ncol = 2)
row.names(req.mat) <- c("x1","x10")

p1 <- info.data$id.row %in% row.names(req.mat)

op1 <- info.data$id.num[p1]
op2 <- info.data$id.name[p1]

I think the code is pretty much self explanatory and am getting the results that i want. There is no problem in printing op1 but when I am trying to get op2 its giving me additional output (Levels). As,there are 1000s of rows in original info.data so this "level" is not looking nice. Is there a way to turn it off?
Comment: `data.frame(..., stringsAsFactors = FALSE)`
Comment: Possible duplicate: every R question ever
Comment: The linked "duplicate" question is quite different from this one. This is asking how to not PRINT factors, the other is asking how to remove extra factor levels that are no longer used.
Answer: Maybe you can use print(op2, max.levels=0)
Comment: This suggestion worked for me when printing a factor vector.
Answer: If you didn't want any of your variables to be factors, then
info.data <- data.frame(id.row, id.num, id.name, stringsAsFactors=F)

is a good choice. If you wanted id.row but not id.name to be a factor then
info.data <- data.frame(id.row=factor(id.row), id.num, id.name,
    stringsAsFactors=F)

is better to set that up. If you created your data in some other way so they already exist as factors in the data.frame, you can convert them back to character with
info.data$id.name <- as.character(info.data$id.name)

If you do want id.name to be a factor but just want to drop the extra levels after subsetting, then
op2 <- info.data$id.name[p1, drop=T]

will do the trick.


Displaying parse errors in module development on the browser

Question: I am currently trying to learn Drupal Module development.
As one might guess, many errors get created in the process. But, all i can see is WSOD on the browser.
For the same errors, most of the Module development videos on youtube show display the errors on the browser itself in an easy to understand format with specific details on the parse error(attached image).

I have tried some solutions on configuring on index.php, but in vain. Also, I can see the errors on Drush, but that doesn't satiate
Could anyone please tell me on how i can have this in my environment?
I'm running Drupal 7 on Windows 8 using Bitnami WAMP Stack.
Comment: That's XDebug - install the extension into your version of PHP, enable and configure, turn Drupal's error reporting up to maximum through the UI, et voila
Answer: You have to enable error displaying in PHP. There are several ways to do it, but one of the most straightforward is to enable them in the settings.php of your project.
Just add those lines to the file:
error_reporting(E_ALL);
$conf['error_level'] = 2;
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

Meaning:

Instruct PHP to notify all errors.
Instructs Drupal to show all errors (using a drupal_set_message).
Instruct PHP to display errors instead of show a blank page.
Instruct PHP to display errors even those in the startup phase.

Also, as Clive comments, if you enable XDebug error reporting is more verbose. The screenshot you show includes XDdebug error backtrace.
Comment: Thanks for the help. Much appreciated.

I am getting the errors displayed in this format:
Parse error: syntax error, unexpected '{', expecting ')' in C:\Users\ajay.un\Downloads\wampstack-5.6.18-0\apache2\htdocs\test\sites\all\modules\custom\mymod\mymod.module on line 90

Can i not have it in a better way, like the one enclosed in screenshot?
Also, like Clive mentioned, can i have Xdebug installed on windows?

Apologies if my questions sond silly - I am new to these dev envts.
Comment: You have to enable Xdebug, and yes, you can have it installed on windows, but those are different questions. Serach onDrupal Answers or create new ones.
Answer: Just add below lines in your site's settings.php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

More details you can find from Show all errors while developing


Reading Outlook Emails in Shared Folder - Python

Question: I have been using the below code to access a public folder in Outlook:
import win32com.client
import datetime

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

inbox = outlook.GetDefaultFolder(18).Folders.Item("xxxxx")
messages = inbox.Items
date = datetime.date.today()

subject = "xxxxxxx"

for message in messages:
    if subject in message.subject and date in message.senton.date():
     print(message.senton.time())

However, our Outlook has been moved from an Exchange server to the cloud and public folders have been changed to shared folders (I think?). The above code no longer works and I get the below error:
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The attempted operation failed.  An object could not be found.', None, 0, -2147221233), None)

Is there any way to search emails from a shared mailbox?
Thanks
Answer: Try this:
inbox = outlook.Folders("Name of Shared Folder").Folders.Item("xxxxx")
Comment: Awesome! That seems to have worked, I have one more question though. I can't seem to download attachments from emails with the code I was previously using on the old folder. I have updated my question with the code to download attachments. Would you have any idea how?
Comment: okay never mind, I think I will post a separate question. Thanks again for the help!
Comment: You can use

 `attachments = message.Attachments`

Then to test it works, print the name of the attachements:

`for i in range(attachments.Count):
 attachment = attachments.Item(i+1)
 print(attachment)`
Comment: Okay awesome, do you have any idea how to specify to download only excel attachments for example? And ignore pdfs etc?
Comment: I would recommend using the `find()` function on the string and look for ".xlsx" or ".pdf". Good luck!


Push the checked item at the top of the list

Question: I have a checkbox with dynamic list item.I want to push the checked item at the top of the list in onload the list.
I have tried as in below snippet.I tried to change the color of the checked item.color get changed but not pushed the item to top of the list.

$(document).ready(function(){
$("input[name='geo[]']").each(function(){
if ($(this).is(":checked")){

      $(this).prop("checked",true);
    $(this).parent('li').css('background-color','#3875D7');
     $(this).parent('li').prepend($(this));

    }
  });
});

<input type="checkbox" value="US_East" name="geo[]" id="geo_US_East" >US East

<input type="checkbox" value="US_West" name="geo[]" id="geo_US_West" >US West

<input type="checkbox" value="NSU" name="geo[]" id="geo_NSU" checked="checked">NSU

Comment: Is the php tag relevant?
Comment: Did you want it to change dynamically as well (i.e. when a different checkbox is checked)?
Answer:

$(document).ready(function(){
$("input[name='geo[]']").each(function(){
    if ($(this).is(":checked")){
        $(this).prop("checked",true);
        $(this).parent('li').css('background-color','#3875D7');
        $(this).parents('ul').prepend($(this).parent('li'));
    }
  });
});

<input type="checkbox" value="US_East" name="geo[]" id="geo_US_East" >US East

<input type="checkbox" value="US_West" name="geo[]" id="geo_US_West" >US West

<input type="checkbox" value="NSU" name="geo[]" id="geo_NSU" checked="checked">NSU

This is the only code that has changed:
$(this).parents('ul').prepend($(this).parent('li'))

The parents() selector gets all parents, not just the adjacent parent. Then prepend the parent of the current input item which is the whole li, that will be added at the beginning of the ul list
Answer: You could reach the ul tag as the parent of your li first, then prepend the list item li to the ul list.

$(document).ready(function() {
  $("input[name='geo[]']").each(function() {
    if ($(this).is(":checked")) {
      $(this).parent('li').css('background-color', '#3875D7');
      let item = $(this).parent('li');
      let list = $(item).parent('ul');
      $(list).prepend(item);
    }
  });
});

<input type="checkbox" value="US_East" name="geo[]" id="geo_US_East">US East

<input type="checkbox" value="US_West" name="geo[]" id="geo_US_West">US West

<input type="checkbox" value="NSU" name="geo[]" id="geo_NSU" checked="checked">NSU

Answer: Hello I hope this helps you further, just implemented the list, not the color you want.
Edit: Add Color

var list = $("ul");
var unorderedList= list.children();

list.on("click", ":checkbox", function() {
    var i = document.createDocumentFragment();
    var checked = document.createDocumentFragment();
    var unchecked = document.createDocumentFragment();

    for (i = 0; i < unorderedList.length; i++) {
        if (unorderedList[i].getElementsByTagName("input")[0].checked) {
            checked.appendChild(unorderedList[i]);
            unorderedList[i].style.backgroundColor ="red";
        } else {
            unchecked.appendChild(unorderedList[i]);
            unorderedList[i].style.backgroundColor ="white";
        }
    }
    list.append(checked).append(unchecked);
});

<input type="checkbox" value="US_East" name="geo[]" id="geo_US_East" >US East

<input type="checkbox" value="US_West" name="geo[]" id="geo_US_West" >US West

<input type="checkbox" value="NSU" name="geo[]" id="geo_NSU">NSU


Load External JSON file in Java Android

Question: i just started learning Java. I'm currently working on a small app that retrieves data from a .JSON file, and stores it into a SQLite database.
I'm trying to open a .JSON file and store it as a JSONObject. Here is my code for reading a JSON file.
private JSONObject readFile(String path) {
    StringBuilder sb = new StringBuilder();
    try {
        FileReader in = new FileReader(new File(path));
        int b;
        while((b = in.read()) != -1) {
            sb.appendCodePoint(b);
        }
        in.close();

        return new JSONObject(sb.toString());
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return null;
}

My JSON files are stored in : app\src\main\java\proj\bb\database\jsonFiles
Im currently calling the readFile method like this:, but it throws a NullPointer. (DatabaseOperations is the classname).
JSONObject jsonFile = readFile(DatabaseOperations.class.getResource("filename.json").toString());
JSONArray arr = jsonFile.getJSONArray("someArray");

I also tried this without succes:
JSONObject jsonFile = readFile("jsonFiles/filename.json").toString());
JSONArray arr = jsonFile.getJSONArray("someArray");

So my question is, what needs to be the path i need to pass as an argument? Or am I doing something else wrong?
Thanks in advance !
Comment: `JSONObject profile = new JSONObject(yourString);`
Comment: Yes, but what has to be `yourString`...?
Answer: Put your JSON Files in the assets folder and access it from there.
You can use the following method to read the data as-
public static String getJSONData(Context context, String textFileName) {
    String strJSON;
    StringBuilder buf = new StringBuilder();
    InputStream json;
    try {
        json = context.getAssets().open(textFileName);

        BufferedReader in = new BufferedReader(new InputStreamReader(json, "UTF-8"));

        while ((strJSON = in.readLine()) != null) {
            buf.append(strJSON);
        }
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return buf.toString();
}

Method will return the JSON string and you can create instance of JSONObject as
External json file as : sample.json
String jsonString = getJSONData(MainActivity.this, "sample.json");

JSONObject jsonObject = new JSONObject(jsonString);

Comment: You're a hero. I've tried so many things and this worked perfectly, thank you!

Load External JSON file in Java Android


How to upload a file into a repo?

Question: I have a free version github account where I created a repo and I need to add a 670KB text file (it has test data) into this repo. I use IE browser and Windows XP OS, and haven't downloaded or installed any gihub software or app into my local system.
How do you directly upload the file into the repo without creating a file and then copying the content?
Comment: go through the git tutorial: http://learn.github.com/p/intro.html
Comment: This is not explained in help. I am not asking hoe to create a new file, it can be done by clicking '+' against repo name. What am I asking is how to directly upload an existing file from local system into git if you don't have git installed. I think the below answer by alestanis seems right, one needs to have git installed. :(
Comment: you can always do what alestanis suggested and then copy/paste the text into github
Answer: Github is supposed to be used wit git, a version control system. You need to use git in order to download/upload/update files inside a github repo.
You can also create a file directly inside Github by clicking on the little button on the right of your repo's name:

And then you can modify it inside Github by clicking on it.

How to upload a file into a repo?


c program doesn't takes "space" as input to show the acsii code

Question: The program shows output for all other characters instead of space. When I enter "space" it doesn't do anything and just waits.
#include<stdio.h>
#include<stdbool.h>

int main(){
    char c;
    while(true){
        printf("Enter character:");
        scanf("\n%c", &c);
        if (c == 27 )break;
        printf("ascii value:%d\n", c);

    }
    return 0;
}

The output for all other character comes fine.
Enter character:r
ascii value:114
Enter character:e
ascii value:101
Enter character:c
ascii value:99
Enter character:p
ascii value:112
Enter character:^[

I don't understand what's going on!
Comment: Did you try the format string `"%c"` for `scanf()`? However, if you just want to input single characters, other functions like `fgetc()` and friends are better choices.
Comment: to read control characters you need to put your terminal in raw mode.
Comment: @thebusybee "%c" was actually causing the problem. "%c" was trying to read the character from the buffer and was showing the ascii code of "enter" i.e 10 as well.
Answer:
When I enter "space" it doesn't do anything and just waits.

scanf("\n%c", &c); does not mean read a line-feed, then a character.
Instead "\n%c" means to read any number of white-spaces like '\n', space, tab, .... and then read a character.
OP's code is stuck reading white-spaces.
Instead, read a line.  Below has minimal testing and only pays attention to the first char in a line of input.
// scanf("\n%c", &c);
char buf[80];
if (fgets(buf, sizeof buf, stdin) == NULL) {
  fprintf(stderr, "Input is closed\n");
  return -1;
}
c = buf[0];

Remember the Enter is a char too.

To read the escape character, other input functions may be needed.
Comment: @Jabberwocky I am confident `fgets()` will work as well as `scanf("\n%c", &c);` to read an escape character.  Yet I suspect your are correct in that neither work and OP needs an implementation specific input function.
Comment: I'm not sure if you can read `Esc` via `fgets` (refering to `if (c == 27 ) ...`) . At least on Windows it doesn't work.
Comment: @chux-ReinstateMonica yes, I had to write a function that clears the buffer after reading.
Answer: #include<stdio.h>
#include<stdlib.h>

void clear_buffer(){
    char ch;
    while(1){
        ch = getchar();
        if (ch == '\n' || ch == EOF ) break;
    }
}

int main(){
    char c;

    while(1){
        printf("Enter character:");
        scanf("%c", &c);
        if (c == 27 )break;
        printf("ascii value:%d\n", c);
        clear_buffer();
        }
    return 0;
}

The function clear_buffer clears the buffer(enter character gets cleared) so that only the first character is accepted and none of the other characters after that.


Digital Signatures Using Entreprise PKI

Question: We are a global entreprise with thousands of employees worldwide. We have our own PKI infrastructure which is trusted internally by our systems but unknown externally.
We sign contrats with our clients. There is an ongoing "paperless" project which aims to abandon physical contrats and use electronic ones instead.
My management is asking me to implement digital signatures on such contracts, using our own PKI infrastructure.
My question is, do the digital signatures produced using our PKI have any added value ? Or do we absolutely need to use a PKI/CA that is trusetd worldwide (like Digicert or Verisign) ?
Answer:
My question is, do the digital signatures produced using our PKI have any added value ?

Value to whom?
The point of a public key infrastructure is to create a chain of trust. Someone who trusts your CA will trust all certificates issued by that CA, and therefore also trust the digital signature issued with those certificates. So if your counterparts do trust your CA, then a signature issued by it has value. If not, not.
So the question you should be asking is: What reason do people outside of your organisation have to trust that your CA follows any sort of standard in issuing, managing and revoking certificates?
The point of using a third party signing service is that the third party then should have been audited by a trusted entity so that both you and the other relevant parties have a good reason to trust them. If you're in the EU, there are specific rules set up by eIDAS for what's called "(Qualified) Trust Services Providers", and companies who want to issue signatures trusted according to that ruleset need to be audited specifically against those rules.
You should also note that the certificates used for web servers usually aren't usable for digital signature of documents.
If this answer seems unclear, it's probably because this is a huge question, and any answer will basically need a thorough understanding of your requirements and current PKI systems as well as the guidelines, rules and laws applicable to you and your counterparts. If you don't have anyone within your company with that knowledge, I strongly suggest that you use outside expertise. It's going to be expensive, but a lot less expensive than finding out in court several years from now that some signature isn't legally valid when you thought it was.
Answer: Electronically, Trust is established by using Digital Signature Certificates. Therefore, you may procure DSC's from any of the globally trusted Certifying Authorities.

Digital Signatures Using Entreprise PKI