How to extract text into dataframe or csv every line using python

Question: I have a text file containing
[(XXX)].XX>[(XXX)].X.XXX
XXX.[(X)].[(XXX)]>>[(XXX)].XX

There are about 10k lines. [(XXX)], XX Theses can be 1 to 10 of them.
I want 2 data frame/CSV containing
Data frame 1
     1       2       3
1 [(XXX)]   XX
2 XXX      [(X)]  [(XXX)]

Data frame 2
     1        2    3
1  [(XXX)]    X   XXX
2  [(XXX)]   XX

I am trying like this but failed
def get_sentences(filename):
    with open(filename) as file_contents:
        d1, d2 ,d3= '>', '>>','.' # just example delimiters
        results = []
        for line in file_contents:
            if d1 in line:
                results = []
            elif d2 in line:
                yield results
            else:
                results.append(line)

Appreciate any suggestion.
Actual dataset
[Na+].[CH3:2][C:3](=[O:5])[O-].[CH3:6][c:7]1[cH:12][cH:11][cH:10][cH:9][cH:8]1>>[c:7]1([CH3:6])[c:12]([C:3]([c:2]2[cH:11][cH:12][cH:7][cH:8][c:9]2[CH3:10])=[O:5])[cH:11][cH:10][cH:9][cH:8]1
[CH:1]1([C:4]([c:6]2[cH:11][cH:10][c:9]([C:12]([CH3:20])(C)[C:13](N(C)OC)=O)[cH:8][cH:7]2)=[O:5])[CH2:3][CH2:2]1.[BrH:21].[C:22](=[O:25])([O-])[OH:23].[Na+]>O>[Br:21][CH2:3][CH2:2][CH2:1][C:4]([c:6]1[cH:11][cH:10][c:9]([C:12]([CH3:20])([CH3:13])[C:22]([OH:23])=[O:25])[cH:8][cH:7]1)=[O:5]

Comment: Changing your question substantially after you received answers is bad because it invalidates tae answers you already received, and it's hard for a new visitor to understand how the answers relate to the now different question. You should probably roll back your latest edit and ac+ept one of the answers, then maybe post a new question with your *actual* requirements if you still can't figure it out. (Feel free to post an answer of your own and accept that, provided of course that it actually solves the question as originally stated.)
Answer: At first, we are opening a file and using the readlines() function to grab all lines. Then we are iterating over the data variable and splitting the line by the .. Then we are creating a new dictionary element - the first element of splitter is the key, the others are the value. At the end, using the pandas DataFrame() function we are creating a DataFrame using created dictionary
import pandas as pd
with open('file_name.txt') as f:
    data = f.readlines()
buffer = {}
for i in data:
    splitter = i.split('.')
    buffer[splitter[0]] = splitter[1:]
df = pd.DataFrame(buffer)

Comment: Can you please tell  that if the text file contain  only two line or more?
Comment: There are about 10k lines. [(XXX)], XX Theses can be 1 to 10 of them.
Comment: Oh, I tried your also but am still facing a problem I have added the actual dataset at the bottom of the post (First two lines). Can you please help.
Answer: try this,
import re
from io import StringIO

text = StringIO("""[(XXX)].XX>[(XXX)].X.XXX
XXX.[(X)].[(XXX)]>>[(XXX)].XX""")

df1, df2 = [], []

for l in text.readlines():
    x, y = re.split(r">+", l)
    df1.append(x.split("."))
    df2.append(y.split("."))

print(pd.DataFrame(df1))
print(pd.DataFrame(df2))

         0      1        2
0  [(XXX)]     XX     None
1      XXX  [(X)]  [(XXX)]

         0   1     2
0  [(XXX)]   X   XXX
1  [(XXX)]  XX  None

Comment: Sorry, my mistake one sec.
Comment: p.splitlines()  ERROR >>> too many values to unpack (expected 2);;; readlines() ERROR 'str' object has no attribute 'readlines'
Comment: f = open("Test(1).txt", "r")
text=f.read()   Got it now using readline()
Comment: try this, ```with open("Test(1).txt", "r") as f: for l in f.readlines()```


How to report cross validation?

Question: When we use cross validation (for example 10 fold) I can obtain 10 sensitivity, specificity and accuracy measures and also 10 ROC curves with the associated AUC.
What should I do for reporting my results? Should I report the result of my best model or the average if all of them?
Comment: Report to whom? What do they want to see? If this is for an academic journal, consider what other articles on similar work report (and if there are no such articles, perhaps that is t the right journal for you).
Answer: Accuracy metrics of cross-validation are difficult because it lives in a statistical gray zone where the samples aren't coming from the same model, but the models aren't independent either since most of the training data are the same.  Ideally, you should avoid the situation all together by setting aside an explicit testing set.  If that is not possible (ex. the dataset is too small to set any aside and still do CV), it is generally better to avoid aggregation and report the statistics for each fold.  This can tell you a lot about the stability of the model because an average accuracy of 80% where half the folds have an accuracy of 100% and half have an accuracy of 60% has a very different interpretation than one where every fold is 80%.
You can additionally report the aggregate measures, but make it clear what you are doing.  Putting the aggregate along side a plot of the individual folds usually does this well.
Answer: I report the mean and standard deviation of fold statistics. Sometimes, I also aggregate by calculating the metrics on predicted vs actual ignoring the folds. For instance, if you are looking at RMSE, then pull all predictions and actuals into one set, and calculate RMSE on it pretending there were no folds.

How to report cross validation?


Creating the submenus with Backbone.js

Question: New in using Backbone.js and underscore.js, trying to create a list of submenus (second level) for my menulist (first level).
So this is my menulist I created in JSON (which I have confirmed exists by printing it to the browser console):
[
  {
    "name": "Sök kund",
    "path":
    [
      {
        "subName": "Fondkonto",
        "subPath": "#fondkonto"
      },

      {
        "subName": "Kassakonto",
        "subPath": "#kassakonto"
      },

      {
        "subName": "Nytt sparande",
        "subPath": "#nyttsparande"
      }
    ]
  },

  {
    "name": "Ny kund",
    "path": "#new_customer"
  },
    {
    "name": "Dokument",
    "path": "#documents"
  },

  {
    "name": "Blanketter",
    "path": "#forms"
  }
]

And this is the code I put out to show in my index-file, which for now prints only the first-level:

And in case you want to know how the view and model/collection are built:
var Menus = require("../collections/menus");

var AllMenus = Backbone.View.extend({

  el: "#menuContent",

  template: _.template(document.getElementById("menus").innerHTML),

  initialize: function() {

    "use strict";

    this.menus = new Menus();
    this.listenTo(this.menus, "reset", this.render);

    this.menus.fetch({
      reset: true,
      url: "./data/menus.json",
      success: function() {
        console.log("Succesfully loaded menus.json file.");
      },
      error: function() {
        console.log("There was some error trying to load and process menus.json file.");
      }
    });
  },

  render: function() {
    console.log( this.menus.toJSON());
    this.$el.html(this.template({ menus: this.menus.toJSON() }));
    return this;
  }

});

var viewMenus = new AllMenus();

Model:
var Menu = Backbone.Model.extend({
  defaults: {
    name: "",
    path: ""
  }
});

module.exports = Menu;

Collection:
var Menu = require("../models/menu");

var Menus = Backbone.Collection.extend({
  model: Menu
});

module.exports = Menus;

Don't mean to paste so much code, but necessary to make this so you understand how I built it up. But I got stuck by trying to show my subMenus with no success to it.
Comment: Sometimes your menu.path is a string and sometimes it's an array. When it's an array  you have to use `menu.path[n].subName` notation. [See here for details](http://stackoverflow.com/a/11922384/3585500). Your template will to have to test what `menu.path` is and either print it or _.each it.
Answer: Your data structure is wrong. You do not want to be switching between Strings and Arrays for your path value. A path should always be a String, and you should create a new property to hold the submenu Array; "submenu" would be a good name. Furthermore, your submenu items should not have subName and subPath as key names. These objects are conceptually the same as their parent objects and they should have the same keys, name and path.
Your revised data structure should look like the following:
var menus = [
    {
        "name": "Sök kund",
        "path": "#",
        "submenu": [
            {
                "name": "Fondkonto",
                "path": "#fondkonto"
            },
            {
                "name": "Kassakonto",
                "path": "#kassakonto"
            },
            {
                "name": "Nytt sparande",
                "path": "#nyttsparande"
            }
        ]
    },
    {
        "name": "Ny kund",
        "path": "#new_customer",
        "submenu": []
    },
    {
        "name": "Dokument",
        "path": "#documents",
        "submenu": []
    },
    {
        "name": "Blanketter",
        "path": "#forms",
        "submenu": []
    }
];

Next, we will update your template to iterate through the top level menu items and, conditionally, their submenus:

Note: I assume you are doing something like the following to tell Underscore to override the default template settings with a Mustache-style syntax. I copied these settings from this answer.
_.templateSettings = {
    evaluate: /\{\{(.+?)\}\}/g,
    interpolate: /\{\{=(.+?)\}\}/g,
    escape: /\{\{-(.+?)\}\}/g
};

Comment: I appreciate the answer, I finished this. And yeah it looked like you mentioned.

Creating the submenus with Backbone.js


Android - How to set width and height for customview in OnDraw() with Canvas?

Question: I am trying to set the Width and Height for a customview on which I use Canvas to draw.
Have noticed that onDraw() always sets to a default width and height, and not take the values as specified in layout xml or set using the onMeasure method.
Could some one help me where I am going wrong.
Thanks
<code>@Override
protected void onMeasure(final int widthMeasureSpec,
                         final int heightMeasureSpec) {
     super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight());
}
</code>
Comment: There is no shame in google "android change size of view dynamically". It will lead you to this [link](https://stackoverflow.com/questions/2963152/how-to-resize-a-custom-view-programmatically).
Comment: @Laufwunder Please understand my question first, i need to change width and height on onDraw using canvas. The link which you have suggested will not work in this case. The answer provided in your link is not helpful. Thanks!!
Comment: Shame on me! sry
Comment: onDraw is not place to change size of the View
Answer: you can use this code
<code>public class CustomView extends View{
private Paint paint;
private int w;
private int h;

public CustomView(Context context, AttributeSet attr) {
    super(context, attr);
    paint = new Paint();
    paint.setTextAlign(Align.CENTER);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    this.w = w;
    this.h = h;
    super.onSizeChanged(w, h, oldw, oldh);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.WHITE);
    canvas.drawText("TEST", w/2, h/2, paint);
}
</code>
}


How to get the hardware ID in .net reactor 6.9.0?

Question: As the title mention above, how can I obtain the Hardware ID for licensing purpose? Because the hardware ID generator tool has been removed in current version(6.9.0).
I have tried using the ID generated from other ID-generator downloaded online(SoftCo for example) but its not working.
Answer: Use the respective license.dll file contained within the SDK:
With the .NET library "License.dll" you are able to determine the current licensing status of your protected software at runtime. You only need to reference this library in your project and use the corresponding methods and properties. All methods and properties should be self explanatory. You don't need to select a license file. If a valid license file is available it will be used automatically to update the licensing status.
Please note, the methods and properties of "License.dll" only return the correct values after you have protected your software. After protection the library "License.dll" is not needed anymore.
Content of "License.dll" in C#:
<code>namespace License
{
    public enum EvaluationType
    {
        Trial_Days,
        Runtime_Minutes
    }

    public class Status
    {

        // Methods
        static Status();
        private Status();
        public static bool CheckConfirmationCode(string harwareID, string confirmationCode);

        public static string GetHardwareID(bool Board, bool CPU, bool HDD, bool MAC);

        public static string InvalidateLicense();
        public static void LoadLicense(string filename);
        public static void LoadLicense(byte[] license);
        public static bool ReactivateLicense(string code);

        // Properties
        public static bool Evaluation_Lock_Enabled { get; set; }
        public static int Evaluation_Time { get; set; }
        public static int Evaluation_Time_Current { get; set; }
        public static EvaluationType Evaluation_Type { get; set; }
        public static DateTime Expiration_Date { get; set; }
        public static bool Expiration_Date_Lock_Enable { get; set; }
        public static bool Hardware_Lock_Enabled { get; set; }
        public static string HardwareID { get; set; }
        public static SortedList KeyValueList { get; set; }
        public static byte[] License { get; set; }
        public static string License_HardwareID { get; set; }
        public static bool Licensed { get; set; }
        public static int Number_Of_Instances { get; set; }
        public static bool Number_Of_Instances_Lock_Enable { get; set; }
        public static int Number_Of_Uses { get; set; }
        public static int Number_Of_Uses_Current { get; set; }
        public static bool Number_Of_Uses_Lock_Enable { get; set; }

    }
}
</code>
Make sure you use the same parameters as you use when protecting the assembly eg The HDD Serial is not used by default in the protection settings so you would use GetHardwareID(true, true, false, true).


Influence of HAC estimates to p-value of t-test

Question: I have a linear regression model and because of heteroskedasticity or autocorrrelation I use HAC (Newey-West) estimates. This influences also p-values of significance t-tests of estimated coefficients as they are then different than the normal p-values would be. My question is: should this influence also the p-value of intercept or should this p-value remain the same?
Comment: Is the mean of your X variables centered on 0?
Comment: You may wish to have a look at http://stats.stackexchange.com/questions/188948/how-to-test-whether-the-average-return-on-susd-aud-of-the-last-30-days-is-sign/189172#189172 to see that the p-values are different even in the case where there are no other regressors at all, just a constant (testing for the mean being implemented via a regression on a constant).
Answer: Keep in mind what the $t$-ratio is: it's $\hat{\alpha}/S(\hat{\alpha})$, where $\hat{\alpha}$ is the estimated intercept and $S(\hat{\alpha})$ its standard error. What you do by choosing to apply HAC is using a different function $S(\cdot)$.
Assume that $\hat{\alpha}$ is the first regressor in the regressor matrix $X$. Then, while for OLS
$S_{OLS}(\hat{\alpha}) =  \sqrt{\text{Var}_{OLS}(\hat{\alpha})_{(1,1)} }= \sqrt{s^2(X'X)^{-1}_{(1,1)}} = \sqrt{(s^2)(1/n)}$,
this is not the case if you use HAC standard errors.
They are constructed diffferently (see e.g. here: https://en.wikipedia.org/wiki/Heteroscedasticity-consistent_standard_errors).
In particular, for the $i$th estimated regression residual $\hat{u_i} = Y_i - X_i\hat{b}$,
$S_{HAC}(\hat{\alpha}) = \sqrt{\left( (X'X)^{-1}(X'\text{diag}(\hat{u_1}, ... \hat{u_n})X)(X'X)^{-1}\right)_{(1,1)} }$.
Notice that if we replaced $\text{diag}(\hat{u_1}, ... \hat{u_n})$ by $I_n \cdot s^2$, the HAC would become identical to the OLS standard error estimate. But unless $\hat{u_1} = ... = \hat{u_n}$, this will not happen.
This applies to the intercept as it does to all other coefficients. Hence, the denominator of your $t$-ratio changes depending on the choice of OLS vs HAC unless $\hat{u_1} = ... = \hat{u_n}$, which happens with probability $0$.
Answer: All p-values will change, including the intercept when you use HAC. They may change to various degrees, of course. Also, sometime, the p-values are calculated to certain precision, so you might not notice the change if it was very small.


How to update select with jquery in rails?

Question: I have the following script in my _form2.html.erb:

This only creates this small exchange-table:

The ID of the green select is "left", the right-ones is "right".
What I want is that it updates the left table, when the AJAX-Method is excuted.
I've allready googled a lot, but I can't find the method, I need.
Can anyone help me out?
EDIT @Anoop Joshi:
Well, I thaught it could rerender the content serverside, and then reload it client side.
This is my code, which needs just to be reloaded:
<%= form_tag({}, {}) do %>
    <% holidays_array = @group.holidays.all.map { |holiday| [holiday.name, holiday.id] } %>
    <%= select(:holiday_id, :name, options_for_select(holidays_array),{}, {:multiple => true, :style => "width: 300px; background-color: #9FE", :width => "300", :size => 20, :id=>"right" }  ) %>
<% end %>

EDIT:
I don't want to add some options to my select. I just need to update the whole select, or if needed the  in the file.
Comment: how is your ajax response look like?
Comment: Sorry, what do you mean?
Comment: What happens is that when I click on << it creates RHolidays (which stand for many to many relations) with the ID of "eine gruppe" and the IDs of the selected holidays.
The >> deletes the RHolidays (which stand for many to many relations) with the ID of "eine gruppe" and the IDs of the selected holidays.
Answer: Assuming that, your success data look likes this,
data = [{
    "id": 123,
    "value": "abc"
}, {
    "id": 1234,
    "value": "abcas"
}, {
    "id": 1232,
    "value": "abcs"
}];

Then your code will be,
$.ajax("/r_holidays/destroy", {
    holiday_id: sel.options[i].value,
    group_id: w_id
}).done(function (data) {

    $(data).each(function () {
        $("#left").append($("<option/>", {
            value: this.id,
            html: this.value
        }));
    });

});

Comment: Thank you,
But isn't there an option, to reload a ?
That would be much more easy.
Comment: I've edited the Original question, to show, which code needs to be rerendered.


Titanium ios notification suddenly stopped working

Question: Push notifications for my ios titanium app has suddenly stopped working. It was working fine before 2-3 weeks ago but it's not working now. Request to notification getting submitted successfully. I had push notifications all nicely set up for our iOS app, but they stopped working a few weeks ago. It looks like our requests are sent to APNS ok, but push notifications are not being received. I am getting follow response to my request.
{
  "meta": {
    "status": "ok",
    "code": 200,
    "method_name": "NotifyTokens"
  }
}
But I am getting no notification to my or any device. Any idea???
Comment: What about your push notification certificate? Have you checked it's expiry date?
Comment: @Anand Thanks buddy it works. It was a certificate problem.
Comment: @Prasanth: You're welcome. If your issue has been solved, please answer to your own question how you resolved the issue. may be it can help some one else :)
Answer: I just figured out my problem. It was a .p12 certificate problem. I generated new certificate and used it for my application and it worked.


How to debug Ubuntu/Cisco VPN issues

Question: I'm trying to connect an Ubuntu laptop (9.10) with some kind of Cisco VPN device; I don't know what's on the other end, and I'm not likely to find out exactly what.  I know my company allows VPN from Linux clients because they provide one that I cannot get to install (it fails to compile).  I've had the most luck with the network-manager-vpnc package, however I can't figure out what's failing.  When I try to connect, I get this message from libnotify:
The VPN connection 'XXX' failed.

which is not very helpful.  I've scoured the system logs and all I can find is this:
Dec 27 12:57:45 jcasadon-lap NetworkManager: <info>  Starting VPN service 'org.freedesktop.NetworkManager.vpnc'...
Dec 27 12:57:45 jcasadon-lap NetworkManager: <info>  VPN service 'org.freedesktop.NetworkManager.vpnc' started (org.freedesktop.NetworkManager.vpnc), PID 2672
Dec 27 12:57:45 jcasadon-lap NetworkManager: <info>  VPN service 'org.freedesktop.NetworkManager.vpnc' just appeared, activating connections
Dec 27 12:58:00 jcasadon-lap NetworkManager: <info>  VPN plugin state changed: 3
Dec 27 12:58:00 jcasadon-lap NetworkManager: <info>  VPN connection 'AmericasEast' (Connect) reply received.
Dec 27 12:58:00 jcasadon-lap NetworkManager:    SCPlugin-Ifupdown: devices added (path: /sys/devices/virtual/net/tun0, iface: tun0)
Dec 27 12:58:00 jcasadon-lap kernel: [ 6144.529002] tun0: Disabled Privacy Extensions
Dec 27 12:58:00 jcasadon-lap NetworkManager:    SCPlugin-Ifupdown: device added (path: /sys/devices/virtual/net/tun0, iface: tun0): no ifupdown configuration found.
Dec 27 12:58:15 jcasadon-lap NetworkManager:    SCPlugin-Ifupdown: devices removed (path: /sys/devices/virtual/net/tun0, iface: tun0)
Dec 27 12:58:15 jcasadon-lap NetworkManager: <info>  VPN plugin failed: 1
Dec 27 12:58:15 jcasadon-lap NetworkManager: <info>  VPN plugin state changed: 6
Dec 27 12:58:15 jcasadon-lap NetworkManager: <info>  VPN plugin state change reason: 0
Dec 27 12:58:15 jcasadon-lap NetworkManager: <WARN>  connection_state_changed(): Could not process the request because no VPN connection was active.
Dec 27 12:58:15 jcasadon-lap NetworkManager: <info>  (wlan0): writing resolv.conf to /sbin/resolvconf
Dec 27 12:58:15 jcasadon-lap NetworkManager: <info>  Policy set 'Northbound Train' (wlan0) as default for routing and DNS.
Dec 27 12:58:27 jcasadon-lap NetworkManager: <debug> [1261936707.002971] ensure_killed(): waiting for vpn service pid 2672 to exit
Dec 27 12:58:27 jcasadon-lap NetworkManager: <debug> [1261936707.003175] ensure_killed(): vpn service pid 2672 cleaned up

I have no idea where to go from here.  Tomorrow I'll ask the IT/IS guys if there's anything they can tell me from their end, but I don't know if they'll be able to tell me anything.  Any ideas?  Thanks!
Comment: It turns out that we have client-side certificates that needed to be installed, but VPNC can't deal with them (yet).
Answer: I connected the Ubuntu 9.10 vpnc to a cisco VPN device by first:
1) Installing the Cisco VPN client for a Window's host (Win XP) in my case, installing the config file provided by the network/security administrator, and having it working on Windows
2) Comparing the needed Network Connections -> VPN options with various screens of the Window's client.
I did not try to compile the Cisco Linux client - I used the built-in vpnc with Ubuntu and Ubuntu 9.10's standard network configuration. For our network configuration, it took about 15 minutes per box (on the windows side, the client installation took the most time. On the Ubuntu side, the cross comparison took the most time.
Answer: Did you try running vpnc from the shell, rather than through network manager?
Murali
Answer: It turns out that we have client-side certificates that needed to be installed, but VPNC can't deal with them (yet)

How to debug Ubuntu/Cisco VPN issues


How can I build a vector whose data is pointed to an array, and is this possible for unified memory?

Question: We know that if we have a vector like
std::vector<int> a_vector;

and this vector has elements, we can have the array of elements from
int *the_array= a_vector.data();

My question is: How can we do the opposite?
int the_array[5]={1,2,3,4,5};

std::vector<int> a_vector;

a_vector.data=the_array; //this does not seem to work

To clarify, I don't wish to copy the values , nor create an array *from * the values but have them in the same memory area.
Also ,  why am I asking this?
I would like to have this original array, in a managed (unified) area of memory
__device__ __managed__ int the_array[5];

and build the vector to point there.
Comment: [`std::span`](https://en.cppreference.com/w/cpp/container/span) (c++20) might be a better "container" for this.
Comment: You could probably do it with a custom allocator, but, why go to the effort?  Plus you wouldn't be able to expand the vector.  See https://stackoverflow.com/questions/8049657/stack-buffer-based-stl-allocator
Comment: https://stackoverflow.com/a/21918950/1863938
Comment: In the latest versions of thrust, a thrust vector can be of a [universal_vector type](https://developer.nvidia.com/blog/support-for-cuda-unified-memory-now-available-in-thrust/), which means that it is using a managed allocator.
Comment: There is also `boost::static_vector`, if boost is your thing.... https://stackoverflow.com/a/21163028/1607937
Comment: Firstly, `int *the_array={1,2,3,4,5}` is invalid.   Second, if you want to control where the data managed by a `std::vector` resides, use a custom allocator.    If you want to create an array of automatic storage duration, and then have a `std::vector` manage that array, my guess is that an allocator for the job will be a hairy job (lots of possibilities of introducing undefined behaviour in response to innocent operations by other code on the vector).
Answer: Being able to construct a vector with already allocated memory would somewhat defeat the RAI design principle which is propably the reason it's isn't part of the std::vector API.
If all you need is a nice modern C++ interface to your allocated memory, span would be the right choice. You don't need to wait for NVIDIA to implement C++20, but could use the gsl-lite implementation of span which provides __host__ __device__ member functions such that you can use their span inside (or as arguments to) kernels as well.
If you want an actual container (not a view), which manages the memory (also allocates it), Thrust would be the obvious choice. While it was possible before to use their device_vector with a custom allocator, nowadays they provide a universal_vector for unified memory.
Answer:
we can have the array of elements from

What we get from std::vector::data is a pointer to the first element.

My question is: How can we do the opposite?
a_vector.data=the_array; //this does not seem to work

No. Only way for a vector to take ownership of an array like that is from another vector of same type.
Comment: @KansaiRobot
Perhaps by writing a custom allocator that calls `cudaMallocManaged`. I don't have experience with that though.
Comment: so I guess the question is if I can define vectors in managed memory...


How do you make it so a file gets the old number and adds to it?

Question: what we want to happen is when a file saves we dont want it to overwrite the old one we want it to add to the old number in the file
This is the code for the writing of the file so far
public void save() throws IOException{
    if (Playtime.hascreatedfolder = false){
        File f = new File("plugins/PlayerTime/");
        f.mkdirs();
        File file = new File(player.getDisplayName() + ".txt");
        //f.createNewFile();
        try {
            BufferedWriter w = new BufferedWriter(new FileWriter(file));
            w.write(getTime());
            w.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

what we want to happen is for it to get the old number add to it and put the new number in Eg. if it had 1:5:21 and a player had played for an hour we will want 2:5:21 but we get 1:0:0 instead.
Comment: Please try to write questions and headings more understandable.
Answer: You never actually read from the file. Here's some possible tips:

Uncomment your create file logic and wrap it in a f.exists() check.
You might also want to do an exists check on f before calling
mkdirs.
If the file exists, read in the value for the current player time and
store it in a variable. Then increment the variable based on the new
player time.
Overwrite the existing value in the file.

I would recommend a RandomAccessFile, it will make your life easier.


How to design multiple page sub report

Question: I am developing asp.net application. In the report section. I need to generate a details report of the site.
First page would explain the detail of the site. The image, information and etc.
The second page and the following would explain about the aircond on the site. If the site got 5 aircond. then 5 page about aircond would be generate.
But i dont know how to do that on asp.net 2010 crystal report.
Answer: The problem is solved by using the sub report. the crystal report would automatically generate one sub report of each row. if we got 5 row of aircond. 5 sub report of aircond would be produce..

How to design multiple page sub report


Accuracy geolocation with google maps geolocation api

Question: I'm using google maps geolocation API to retrieve user position and i need a more accurate position on my web app, on pc or even mobile device.
the docs request cellTowers and wifiAccessPoints info to have this accuracy, that's my problem. I didn't find yet a way to access these informations. Can someone help me with that ?
I'm using React 16.12.0
Comment: Why not use W3C Geolocation API instead?
Answer: You can also use your browser's HTML5 Geolocation feature along with the Maps JavaScript API to show user location or device position in Google Maps. You can check this.
Here's also a sample reactjs code that implements this. Make sure that you change the API Key in the Map.js file in the code for it to work properly. Here's a code snippet:
import React, { Component } from "react";
import Map from "./Map";
import InfoWindow from "./infowindow";
import "./style.css";

class App extends Component {
  constructor() {
    super();
    this.state = {
      name: "React",
    };
    this.handleLocationError = this.handleLocationError.bind(this);
  }

  handleLocationError(browserHasGeolocation, infoWindow, pos, map) {
    infoWindow.setPosition(pos);
    infoWindow.setContent(
      browserHasGeolocation
        ? "Error: The Geolocation service failed."
        : "Error: Your browser doesn't support geolocation."
    );
    infoWindow.open(map);
  }

  render() {
    return (
      <Map
        id="myMap"
        options={{
          center: { lat: 41.0082, lng: 28.9784 },
          zoom: 8,
        }}
        onMapLoad={(map) => {
          let infoWindow = new google.maps.InfoWindow(); // Try HTML5 geolocation.

          navigator.geolocation.getCurrentPosition(
            function (position) {
              let pos = {
                lat: position.coords.latitude,
                lng: position.coords.longitude,
              };
              infoWindow.setPosition(pos);
              infoWindow.setContent("Location found.");
              infoWindow.open(map);
              map.setCenter(pos);
            },
            function (error) {
              infoWindow.setPosition(map.getCenter());
              infoWindow.setContent(
                true
                  ? "Error: The Geolocation service failed."
                  : "Error: Your browser doesn't support geolocation."
              );
              infoWindow.open(map);
            }
          );
        }}
      />
    );
  }
}

export default App;


array_flip() error while creating a menu item with form

Question: My aim is to create a new menu item and display the user profile form on that page.
Here is the code I am using.
/**
 * Implements hook_menu().
 *
 */
function profile_preferences_menu(){
    $items['user/%/edit/profile-preferences'] = array(
        'title' => t('Profile Preferences'),
        'page callback' => '_profile_preference_display_preference',
        'page arguments' => array(1),
        'access arguments' => array('access profile preferences'),
        'type' => MENU_LOCAL_TASK,
        'weight' => '0'
    );
    return $items;
}

/**
 * Implementation of hook_permission().
 */
function profile_preferences_permission() {
    return array(
        'access profile preferences' =>  array(
            'title' => t('Access List of profile preferences'),
            'description' => t('Access permission to view all the profile preferences.')
        ),
    );
}

/**
 * Function to fetch and display preferences form.
 */
function _profile_preference_display_preference() {
    global $user;
    module_load_include('inc', 'user', 'user.pages');
    $form = drupal_get_form('user_profile_form', $user, 'account');
    unset($form['account']);
    return $form;
}

When I access it using the administrator account, it works perfectly; when I access using another user account (who has the required permission) it gives me the following error.

I wonder if there is any issue with the implementation of hook_menu().
Comment: As side note, title and description of a menu item should not be passed to `t()`; that is automatically done by Drupal core code.
Comment: Remove this line: `unset($form['account']);`. That should fix the warning
Comment: What is exactly the purpose of showing the user edit form without some of the form fields? Users can edit their account information / preferences without editing their username (which can be edited only from users with one of the following permissions: _administer users_ or _change own username_), email, or password.
Comment: I don't know if this is the error but your path should be user/%user/... rather than user/%/...
Answer: The code that is causing the error is the following one, executed from DrupalDefaultEntityController::load().
  // Create a new variable which is either a prepared version of the $ids
  // array for later comparison with the entity cache, or FALSE if no $ids
  // were passed. The $ids array is reduced as items are loaded from cache,
  // and we need to know if it's empty for this reason to avoid querying the
  // database when all requested entities are loaded from cache.
  $passed_ids = !empty($ids) ? array_flip($ids) : FALSE;

I could not track down which function is calling user_load() or a similar function to load a user entity. As far as I can see, the form validation and form submission handlers added to the form use $form['#user'] or $form_state['user'] (which is set from the $account parameter passed to user_profile_form()). Keep in mind that $form and $form_state are used when saving the entity, such as in the following function call done from user_profile_form_submit().
entity_form_submit_build_entity('user', $account, $form, $form_state);

It is always a bad idea to unset a form item, as that would be causing problems with the code that expects those form items to be present.
Removing them, you are also avoiding that the corresponding items in $form_state['values'] are present. This is probably causing the error you see.
As side note, your page callback is getting a parameter, but that is not used. Instead, your code is using the global user object, which means the shown form is for the currently logged in user, not for the user whose ID is passed in the URL.
I would also change user/%/edit/profile-preferences with user/%user/edit/profile-preferences. The latter automatically validates the user ID and loads the user object, which is then passed to the page callback.
Comment: @clive I have removed **unset($form['account'])** code, but still it is not working. My point of view if there is problem with unset($form['account']) then it should not work all the account, but in my case code is working fine for admin account, it doesn't work for authenticated users role
Comment: It all depends from the code that is executed. Surely, there is a module that is loading a user entity when I would not expect it to happen (since the user object is already available on `$form_state['user']`). I cannot say if it is a Drupal core module that does it, or a third-party module. Since the same error doesn't happen with the user account form showed by Drupal in user/%user/edit, there must be something that interferes with the form.


Change Menu Link Title Dynamically

Question: Requirements:
Change the Navigation block link "Add Listing" based on role.
This is the built in Navigation block and I've renamed Add Content to Add Listing

I've tried all the solutions here.  Though I admit I am probably doing it wrong.
Change menu link title dynamically?
How do I modify a menu link title dynamically?
How do I dynamically change the title of a menu link?
I've created a module with the following code:
function mymodule_menu_alter(&$items) {
  $item['options']['alter'] = TRUE;
  $items['node/add']['access callback'] = '_accesscheck';
  $items['node/add']['title callback'] = 'mymodule_title_callback';
    var_dump($items);
}

function mymodule_title_callback($arg1) {
    $title = 'New Title';
    return $title;
}

function _accesscheck(){
  global $user;
  return $user->uid;
}

This is a var_dump snippet of what I'm after:
["node/add"]=>
  array(5) {
    ["title"]=>
    string(11) "Add content"
    ["page callback"]=>
    string(13) "node_add_page"
    ["access callback"]=>
    string(16) "_node_add_access"
    ["file"]=>
    string(14) "node.pages.inc"
    ["module"]=>
    string(4) "node"
  }

Before I attempt to include roles I'm just trying to get the link to change.
Comment: Might not matter, but generally your access callback should return a `TRUE` or a `FALSE`, not a uid.
Comment: To see if I can get this work, I removed the function and the access call back.  Still no joy.
Answer: Just tried your code (trimmed down) and it worked without any problems. I simply overwrote the "title callback", like you did. I tried it with admin and anonymous account, see the screenshots for the end result below:

I also added, as an extra precaution, a module_implements_alter implementation. With that hook, you can push your menu_alter implementation to the end of the hook_menu_alter list. That way you can make sure it gets executed after every other menu_alter, in case there was some competing implementation there.
<?php

/**
 * Implements hook_menu_alter().
 */
function mymodule_menu_alter(&$items) {
  $items['node/add']['title callback'] = 'mymodule_title_callback';
}

function mymodule_title_callback($arg1) {
  global $user;
  $title = 'New Title';
  if( $user->uid == 0 ) {
    $title = 'New Title for anonymous';
  }
  return $title;
}

/**
 * Implements hook_module_implements_alter().
 */
function mymodule_module_implements_alter(&$implementations, $hook) {
  if ($hook != 'menu_alter') {
    return;
  }

  $module = 'mymodule';
  $group = array($module => $implementations[$module]);
  unset($implementations[$module]);
  $implementations = $implementations + $group;
}

Hope you can get this to work!
Comment: Perfect and thank you for the code cleanup.  I was easily able to add checks for each role and display the proper link name.  If anyone else runs into this ensure you keep the defaults link names in the Navigation block.  It MUST BE 'Create content' as the link name or it won't work.

Change Menu Link Title Dynamically


Laravel - query builder - left join polymorphic relationship, distinct only

Question: So I'm using Vue 2.0 and Laravel 5.3 to create an application.
I've implemented my own sortable table with Vue, using the built-in pagination provided by Laravel.
Everything's working perfect - except, I'm trying to left join a "media" polymorphic table, so I can show an image in my table.
To enable sorting, I've had to use the query builder, as you can't sort on a relationship using Eloquent (AFAIK).
I define my relationships like:
$inventories = $inventories->leftjoin('users as inventory_user', 'inventories.user_id', '=', 'inventory_user.id');
$inventories = $inventories->leftjoin('categories as category', 'inventories.category_id', '=', 'category.id');
$inventories = $inventories->leftjoin('inventories as parent', 'inventories.parent_id', '=', 'parent.id');

Which work great. However, how exactly do I left join a polymorphic relationship, without repeating (duplicating) any of the rows?
I've got this:

$inventories = $inventories->leftJoin('media', function($q) {
    $q->on('media.model_id', '=', 'inventories.id');
    $q->where('media.model_type', '=', 'App\Models\Inventory');
});

Which does show media (if it has a relation in the polymorphic table). However, if for instance I have 5 images (media) for 1 particular inventory item, my query repeats the inventory for however media there are, which in my case it's repeated 5 times.
This is my select query:

    $inventories = $inventories->select(DB::raw("
            inventories.id as inventory_id,
            inventories.name as inventory_name,
            inventories.order_column as inventory_order_column,
            category.id as category_id,
            category.name as category_name,
            parent.name as parent_name,
            parent.id as parent_id,
            inventories.updated_at as inventory_updated_at,
            media.name
    "));

I think I need to use a groupBy, but I'm not sure where I define it. If I do
$inventories = $inventories->groupBy('inventories.id');

I get
SQLSTATE[42000]: Syntax error or access violation: 1055 'test.inventories.name' isn't in GROUP BY...
Has anyone been in a similar situation or know where to put my groupBy to show 1/distinct inventory items?
EDIT:
I was able to fix by using:

$inventories = $inventories->leftJoin('media', function($q) {
    $q->on('media.model_id', '=', 'inventories.id');
    $q->where('media.model_type', '=', 'App\Models\Inventory');
    $q->orderBy('media.order_column', 'asc');
    $q->groupBy('model.model_id');
});

and then setting strict => false in my database.php.
Comment: FWIW, I would not call it a fix, a workaround at best. Add the column name ('model.mode_id' from your example) you are grouping by to SELECT statement and the error will be gone and you can/should keep the strict setting on.
Comment: If I ever look at that codebase again, I'll have to check that out! I remember feeling queazy about disabling `strict` but workaround fixed it, so I forgot about it.
Comment: The issue is with the grouping. See the answer I added below
Answer: As the error states this issue occurs when name column not being in GROUP BY clause.
To solve this change
'strict' => true,

In mysql configuration under connections in your config/database.php file to
'strict' => false,

Additionally I would highly encourage to use polymorphic relations over left join with eloquent.
Comment: Thanks, Gayan! I originally found that fix here: https://github.com/laravel/framework/issues/15232 but didn't want to disable strict mode, as I didn't know what the repercussions were. I have since disabled it and it's working.

I am indeed using polymorphic relations, which do work fine. However, I'm using the query builder, so I have to manually connect my tables via joins. Thanks for your help!
Comment: Glad you found it helpfull. More on `mysql strict mode` could found here https://mattstauffer.co/blog/strict-mode-and-other-mysql-customizations-in-laravel-5-2
Comment: Sorry, I meant this issue: https://github.com/laravel/framework/issues/14997#issuecomment-242129087
Comment: Ah, that explains so much! Thanks Gayan!
Answer: I think you'll add it here:
$categories = $categories->select(DB::raw("
        inventories.id as inventory_id,
        inventories.name as inventory_name,
        inventories.order_column as inventory_order_column,
        category.id as category_id,
        category.name as category_name,
        parent.name as parent_name,
        parent.id as parent_id,
        inventories.updated_at as inventory_updated_at,
        media.name
"))->groupBy('inventories.id');


How to call Ruby method on user click?

Question: I have a simple .erb file that creates a list of song names.
When the user clicks the name, I want a browser window to open and play that song in YouTube. My YouTube implementation is a quick hack, but to that happen you simply call
YouTubeAPI.new(searchphrase)
and pass it a string.
My question: how can I make the page respond to user input, so that when the user clicks on the song name it passes that song name as the value to my YouTubeAPI class method?
This is my small .erb file:
<!DOCTYPE html>

Songs

There are <%=Song.all.count%> in this playlist.

All Songs:

<% Song.all.each do |song| %>

<a href = "songs/<%=song.name.downcase.gsub(/\s+/, "")%>.html">
  <%=song.name%>

<% end %>

Home

Comment: Where is your YouTubeAPI.new(searchphrase) method? Is it a javascript or a method in Rails server side?
Comment: It's definitely not JS, so I assume it's Rails server side.
Answer: I see few approaches to this.
One is, of course, Ajax. Intercept this click with Javascript and send the song name to your controller. The controller can return the Youtube URL, and your javascript ajax callback can reload this.
Another thing to try is to replace the href to your site - you actually call your site with song name as parameter, and then your YOUtubeAPI will redirect the user to youtube.
One more option, rewrite this helper in Javascript and put it on the client.

How to call Ruby method on user click?


Show that it is at most countable

Question: In a space of finite measure, show that a family of disjoint measurable sets with positive measure is at most countable.
Could you give me some hints what I am supposed to do??
Comment: Can you explain what "at most" countable means and what "at least" countable means?
Answer: Hint: Let $\{A_i\}_{i\in I}$ be the family of sets with $\mu(A_i)>0$ for all $i$. Now consider the sets with measure greater than $1/n$ for each $n\in\Bbb N$. If the family of sets $\{A_i\}$ were uncountable, what conclusion can you draw (keeping in mind that these all are subsets of a space with finite measure)?
Comment: @MaryStar: If $\{A_i\}_{i\in I}$ is uncountable, then there must be infinitely many $A_i$ with the property that $\mu(A_i)\geq\frac{1}{n}$ for some $n\in\Bbb N$. Does this clarify the hint?
Comment: @MaryStar: $\infty=\sum\frac{1}{n}\leq\sum\mu(A_i)\leq\mu(X)=M<\infty$, where $X$ is the space of finite measure.
Comment: @MaryStar: if there are uncountable many $A_i$ of positive measure, then there are at least countably many with $\mu(A_i)\geq 1/n$. Since $1/n\leq\mu(A_i)$, we can add up the countably many $A_i$ we chose. Since the $A_i$ are disjoint, we have $\sum\mu(A_i)=\mu(\bigcup A_i)\leq\mu(X).$
Comment: Do we have the following?? $$\frac{|I|}{n} <\mu(\{A_i\})=\mu(\cup_{i \in I}A_i)\leq \sum_{i \in I}\mu(A_i)$$
Comment: I got stuck right now... Could you explain me how I could get a contradiction??
Comment: I see... So we consider the sets with measure greater than $\frac{1}{n}$ because we know that $\mu(A_i)>0$, right?? For the last inequality we have the following :  $$A \subset X \Rightarrow \mu(A) \leq \mu(X)$$  $$ \mu(A)=\mu(\cup A_i) \leq \sum \mu(A_i)$$ how do we conclude to the inequality:  $$ \infty =\sum \frac{1}{n} \leq \sum(A_i) \leq \mu(A) \leq \mu(X)<\infty$$  ??
Comment: I understand!! Thanks a lot!!! :-)
Comment: Can you explain what "at most" countable means and what "at least" countable means?

Show that it is at most countable


Generate QrCode on ReportBuilder with multiple rows of data

Question: So here's whats happening
Im working with 2dDBbarCode on ReportBuilder (Digital Metaphors) to generate a QrCode that encode all itens (products) in the report, one item per row.
But it only encode the first row of data.
The 2dDBbarcode is positioned on Report's Header band.
How can i have multiple rows of data inside 2dBDbarcode?
I've already take a look at rbuilder guide at http://www.digital-metaphors.com/pdf/rbuilder.pdf but cant get nothing from there
Comment: What datatype do you use for QRcode? http://en.wikipedia.org/wiki/QR-code#Storage
Comment: Only Alphanumeric data
Answer: I actually found a way to get around this problem. instead of create a report pipeline to get data from database directly inside 2dbarcode, I'm passing the data before open up report. The code now is:
<code>        with DataModule2.Sql_IdQtd1 do
          begin
            Close;
            SQL.Clear;
            Sql.Add('select concat("when ",id_produto," then ",quant) from mv_vendas_movimento');
            Sql.Add('where controle=:NumPed');
            ParamByName('NumPed').Value := NumPed;
            ExecSQL;
        Open;
        First;

-- here it goes thru each row of data and store it to 2dbarcode.data, one row by line

         DataModule2.p2dbrcd1.Data := 'UPDATE cad_produtos SET estoque = estoque + CASE id';
            while (not Eof) do begin
              DataModule2.p2dbrcd1.Data := DataModule2.p2dbrcd1.Data +sLineBreak+ DataModule2.Sql_IdQtd1concatwhenid_produtothenquant.Text;
              Next;
            end;
            DataModule2.p2dbrcd1.Data := DataModule2.p2dbrcd1.Data +sLineBreak+ 'ELSE estoque END'

          end;
</code>


Barra header não redirecciona correctamente

Question: Num template que estou a usar, ele é todo responsivo em qualquer tamanho.
Sendo que estou a adequa-lo ao meu site, somente na barra header é que não está a funcionar correctamente, conforme mostro no gif.
Se tiver o modo de programador aberto, de imediato em tamanho pequeno, e abrir para tamanho maior, o template corresponde, caso seja de grande para pequeno (como imagem), o mesmo não dá.
O que poderá ser? Já aconteceu-vos?
Comment: Verifique se você está com a media viewport corretamente ajustada, segue um exemplo:
Comment: Não funcionou.

Uso isto, tal como vem no template, mas não dá.
Answer: Verifique se sua pagina está tratando o redimensionamento através da meta viewport, segue exemplo
<code><meta content="width=device-width, initial-scale=1" name="viewport">
</code>
Comment: Não funcionou.

Uso isto, tal como vem no template, mas não dá.
Comment: Você pode fornecer o codigo fonte ou a url para que possa ser analisada?


is there any contradiction learning different instruments?

Question: I wanted to know if there is any contradiction when someone is learning several different instruments?
For example, I am learning to play piano . Would becoming a good piano player in any way hold me back from playing another instrument professionally?
P.S. Sorry i couldn't explain well
Comment: Just one person's experience, but I remember from my band and orchestra days in high school that the poly-instrumentalists amongst us were the best of the best. There were two in my trombone section that also studied piano, and they were far above everyone else in trombone ability.
Comment: More so than difficulty of learning the physical instrument, I have found that cross-over difficulty for multi-instrumentalists comes from bridging the gap of musical *styles* and *training*. For instance having a classically-trained musician (who typically works in a single key) move towards a more free-form style of music (like jazz) can be difficult. Likewise, I've heard Euro-American percussionists talk about how difficult it is to rewire their brains to allow them to play South American rhythms. That "overcoming your training" is where any difficulty will lie.
Comment: It is certainly good to be *able* to play as many different types of instrument as possible (i.e. keyboards, string, wind, percussion, guitar, etc...) but that is not the same as playing them all at a professional level. For example, if you spend an hour or two playing violin and immediately switch to piano, your left wrist suddenly has to do completely different things and your left arm has to work in a completely different position. That switch-over is hard to do, and can hurt (literally!) - which is not a good thing in the long run.
Comment: Lookup Howard Levy =)
Answer: It's good to keep in mind that it takes a lot of practice to learn an instrument. You have to dedicate a lot of time and it will take years to become a good piano player (same for all the instruments). So, if you want to learn how to play multiple instruments, you will have to practice all of them. If you don't have a lot of time, it will be hard to practice them at the same period of time.
This doesn't mean you cannot. A lot of people practice their skills on multiple instruments.

Would becoming a good piano player in any way hold me back from playing another instrument professionally?

Of course not. Quite the opposite, in fact! I started with double bass for a few years and then I started playing the piano and the fact that I already knew rhythm, melodies, harmony, theory etc, made it easier to learn the piano. If you already know an instrument, it's going to be easier to learn another one. To oversimplify it, you just have to learn the technique in the new instrument.
So my advice would be to start with the one you want to start with (I guess piano in your case) and then if you have the time, take up another one. But, I would also suggest that you take up the second instrument after you have learnt quite decent piano, because otherwise, it'd be quite difficult to learn from scratch two totally new instruments when you don't know anything about music.
Comment: @Hitman2847 That's a different subject. If you build a lot of muscle, it might be more difficult to play an instrument
Comment: I have never met a professional classical performer of any instrument who did not have modest facility playing the piano. It can be very helpful.
Comment: thanks . this question took my mind because i heard practicing some sports makes paying some instruments harder . i think what i heard was swimming or tennis would make playing piano harder due to the changes made to the muscles. anyway , thank you very much.
Comment: Thanks, by the way can i start learning guitar with electric guitar or bass , or i should start with classic guitar? It would help me a lot
Comment: @Hitman2847 you can start with electric guitar, yes. If you are not interested in classical guitar, don't start with that, but id you do, it will definitely help with your technique. But if you want to start with guitar, start with a guitar, not a bass
Comment: thanx so much buddy
Answer: One of the more dangerous combinations is piano and piano accordion.  The similarity of the keyboard leads to a conflation of technique that results in "lowest common denominator" approaches.
The piano is a percussive string instrument featuring impetus-sensitive attack and rather fuzzy decay, the accordion is a continuous-tone wind instrument with an attack basically insensitive to key impulse and a very precise key release imprint that, through bellows inertia, also impacts the strength of the next attack, making leggiero articulation the core of fast play, and continuous  bellows control the core of slow emotional play.  You would not expect to change from hurdygurdy to violin without significant investment in bow technique, either...
In this case it is more the "similarity" of the controllers that is masking core differences.  In a similar vein, few people are renowned for both piano and organ playing skills, even though harpsichord and organ is not all that uncommon as combination.
So when going for multiple instruments, it might be prudent to pick something which significantly differs in its controls.  That way, you are less tempted to skip over basics that may make a difference in why people would rather hear you play a real instrument than a keyboard controller with sampled sounds of it.
Comment: " few people are renowned for both piano and organ playing skills" - because the playing techniques for those instruments are very different! The fact that they both happen to have similar-looking keyboards might make someone think otherwise, but it is almost irrelevant except at an "elementary" level.
Comment: I can also attest to the differences between piano and organ at a more general level. At one time I played both and not only was the feel of the keyboard different but the style of playing is different as well. The addition of bass pedals completely changes what you're doing with your left hand which can be difficult if you're not familiar with playing more melodic phrases with the left hand (rather than chords and bass notes as on the piano) and also makes arranging for or improvising on the instrument quite different and you can't just transfer these skills from the piano to the organ.
Comment: your answer wasn't totally wrong . but since it did not totally fit the question i didn't mark it . hopefully i will vote it up when i have 15 reputation points . thnx
Comment: I thought Michael Johnson was going to say "The addition of [bass] pedals completely changes what you are doing with your [feet.] Which is more accurate than what he did say. For example, "if you're not familiar with playing more melodic phrases with the left hand (rather than chords and bass notes as on the piano)..." That sounds like he has limited exposure to classical (at least) piano repertoire.
Answer: The first thing to emphasise is that "professional" and "highly skilled" do not necessarily go hand in hand.  The Sex Pistols were professional musicians, but you wouldn't use them as examples of how to play their instruments well!
It's also important to emphasise that expecting to become a professional musician when you're just starting is wildly over-optimistic.  From all the people who start playing instruments, maybe 1 in 20 stick with it long enough that they can earn a bit of beer money playing in an pub band.  Maybe 1 in 1000 are good enough to earn a living from it - and most of them will be working as music teachers.  Something like 1 in 100,000 will be good enough to earn a living wage solely from performing.
You certainly will find that different instruments need different techniques.  Synth and piano will need different ways of playing and thinking about the sound, for instance.  That doesn't make it impossible to learn both, but you do need to remember that they are different instruments, and you can't assume that skills on one will always transfer.  Even different examples of the same instrument will need time to learn their specific characteristics - on a piano, for instance, the hammer action can be radically different between different pianos, and it can take a few hours of playing to really get the hang of a new one.
Comment: @Stinkfoot that comment would be better directed at a lot of modern pop musicians, it's not really true about the sex pistols music.  The parts aren't that difficult, but that's not really the point now is it.
Comment: _...The Sex Pistols were professional musicians..._  Not really. "Entertainers" is more apropos than "musicians" - entertaining while holding an instrument and making some quasi-musical noise doesn't make one a "musician".... IDK about the Sex Pistols, but we do know that very often when such groups record in a studio, sessions musicians - people who actually are professional musicians- do the most of the actual playing.
Comment: @Some_Guy - it's been true for **a long time**. Trust me on this.... :) The point is I don't care to hear the term "professional musician" cast about cavalierly. IMO there's a big difference between a "professional musician" and a "professional entertainer", even if they use/play music. (You owe me an answer about the chromatic harp layout...)
Comment: @Stinkfoot I'm under no illusions about the musicianship or lack thereof of post-war commercial "musicians", but as you said yourself, you don't know much about the sex pistols so perhaps you should pause for a second before coming at a subject on which you are admittedly ignorant as if you are a wise old owl talking to children.  In other words, perhaps you should consider listening to "bollocks" before talking it
Comment: @Stinkfoot Stand by what, you're simultaneously claiming ignorance and knowledge. Just listen to the recording, the parts are easily within the realm of players' ability, and there aren't a crazy amount of overdubs, why would you get studio musicians to play an album that it's obvious the existing musicians were able to play? (and did play live).  And as for whether it's quasi-musical noise, well that's like your opinion man but imo it's more interesting than a lot of "professional" music from the same period, and holds up to the test of time pretty well (unlike a lot of prog and fusion)
Comment: @Some_Guy - I know the SP and I said explicitly I don't know how they recorded. i stand by my comments, FWIW. Enjoy.
Comment: @Stinkfoot By definition, a "musician" is someone who makes music, and "professional" means that it is their primary job.  You may have your opinion about the quality of the Sex Pistols (I don't like them much myself), but the literal definition of the words are what they are, and the point I was making to the OP remains valid.
Comment: @Graham : IMO there is more to a term than the literal definition of its  **two individual components**, both of which I know quite well. I'm also not sure what the SP could really considered "their primary job", and that's applicable to a lot of rockers, not just the SP. So I prefer to call them entertainers or "musicians", but not "professional musicians", which to me means someone dedicated to music in a different way. YMMV.
Answer: Don't construct excuses.  With the possible exception of some atheletic pursuits, where over-development of one set of muscles might hinder other requirements - a body-builder is probably not suited to the pole-vault for instance - getting good at one thing is very unlikely to prevent you from getting good at another.  Of course, if you're aiming for virtuoso level with 8 hours of daily practice on piano or violin, you might run into a scheduling problem.


How to find file creation date in Windows using Go

Question: I was hoping to find the created date of a file in Windows. How would I be able to accomplish this simply?
I have been using os.Stat and os.Chtimes to get other file information but it does not seem to offer information on created date.
Comment: @Zyl: The package you link to gets the windows filesystem times using the std library, so you can't really say that the std library doesn't have the times available.
Comment: @Zyl: It's also unfair to say it's "heavily oriented around Linux". More accurate: It's heavily oriented around features broadly available across all OSes. OS-specific features (whether for Linux or Windows or others) are hidden, and require extra work.
Comment: Note that some Unix/Linux file systems *do* have the concept of file-creation time (BSD calls this "birthtime" and ext4 and ZFS both store one). See also [File creation time in unix](https://stackoverflow.com/q/10171069/1256452).
Comment: The Go standard library does not have file creation dates readily available, because it is heavily oriented around how the Linux kernel works, which does not have information about the creation time of files. However, for other platforms, the following library looks like a worthy contender: https://github.com/djherbis/times
Answer: The FileInfo.Sys method returns the system specific data structures. On Windows, this will be a syscall.Win32FileAttributeData, which looks like
type Win32FileAttributeData struct {
    FileAttributes uint32
    CreationTime   Filetime
    LastAccessTime Filetime
    LastWriteTime  Filetime
    FileSizeHigh   uint32
    FileSizeLow    uint32
}

Getting the creation time would look something like:
d := fi.Sys().(*syscall.Win32FileAttributeData)
cTime = time.Unix(0, d.CreationTime.Nanoseconds())

Since this is windows specific, this of course should be protected by build constraints. Either using a _windows.go file or //go:build windows
Comment: Note that `fi` is the `fs.FileInfo` retuned by `os.Stat`.
Comment: @ShaneBishop, yes the type has been moved to the `fs` package, which is why there is a type alias in the `os` package, but they are still functionally the same type. I updated the link directly to the `fs` package type.


Brexit: was "leave" statistically significant?

Question: In this post we ask a question about a natural phenomenon called humans attempt to find decision by counting votes. The specific incident of such natural phenomenon that this question is about is the case of Brexit.
Note: the question is not about politics. The goal is to try to discuss such natural phenomenon from a statistical point of view based on observations.
The specific question is:

Question: What does the $51.9\%$ Brexit vote to leave mean? E.g. does it mean that the public really wants to leave EU? Does it simply mean that the public is unsure and needs more time to think? Or is it something else?

Assumption 1: there is no error in the voting process.
Comment: Democracy is **not** about statistical significance. The 51.9% result means that 51.9% of those who voted, voted "leave". This is not an opinion poll. Those who didn't vote, voted by (not) using their feet. Interpreting 51.9% as "public is unsure and needs more time to think" is simply lying with statistics. Brexit happened with probability 1.
Comment: This thread is destined to be non-statistical, opinionated, and possibly even polemical. It's just no fit for this site, regardless of how popular it might be.  We have a [chat room](http://chat.stackexchange.com/rooms/18/ten-fold) populated by people who would be happy to engage further in such conversations: check it out!
Comment: IMHO, this is a non-statistical question with a thin veneer of statistics added to disguise that fact.  As I read it, the assumption "there is no error in the voting process" eliminates all statistical considerations and necessarily channels the discussion into what "voting ... means" in a democracy. That's a matter of political science and philosophy, not statistics.
Comment: You bring up an important issue: measurement error of public opinion metrics such as polls. I'm afraid that main source of error is not from the sample size.
Comment: I believe the current discussion is statistically focused and is a good example of interpreting voting results as it applies to statistical testing.
Comment: I think statistical significance *can* be important if the sample size is low.  Obviously the results of the poll are all that matter politically, but the thoughts of ALL citizens can be of interest as well.
Comment: @Underminer I could have agreed with that earlier, but the comments and an answer by the OP (as well as your comments) are clearly steering this thread into opinionated discussion.
Comment: You all know how easy it is to mess up the measurements by sample preparation, by filtering out observations etc. That's what happens on the national scale when you have like Brexit. You get Obama trying to scare you into "remain" vote: we won't make trade deals with you if you leave. You get all the media filled with economists, Treasury and BoE promising the end of the world if you leave. All these "experts" are trying to frighten you to stay. There was this massive coercion of British public going on for months, and you tell me there was no sampling bias issues. You guys are funny
Comment: This was a huge experiment: those at the driving seat trying to convince the passengers to go where they didn't want to go anymore. We have a similar situation in USA. Almost entire set of traditional media trashing one of the presidential candidates 24/7 for more than a year in attempt to taint the sample, and get the desired results at the polls: to no avail so far. We'll have the same kind of sampling issue in November in USA. The measurement issues are sampling issues, but not not the sampling size issue: that's what I'm trying to say.
Comment: @Tim: while democracy is not necessarily about statistical significance, it is neither necessarily about "50%+1". While it is true that 17 million voted for Brexit, it is also true that 16 million voted against it. To use "50%+1" as a criterion is not intrinsic to the notion of democracy,
Comment: I agree on what democracy is. However, I am not trying to look at such political aspects. My goal here is to interpret the numbers from a statistical point of view in order to identify whether it was better for UK to conclude that "*well, we are not sure, let's try again!*" by using a principled statistical method
Comment: Personally my direction on this is to interpret votes (and unvotes) in order to estimate the probability that this case was a grey area. I *think* there is hope in reaching an objective conclusion that explicitly states the assumptions. I think it goes under assuming a distribution for votes when the matter at hand is a grey area. Plus I think this could have been a hot topic that would help boost the popularity of Cross Validated ;)
Comment: @whuber , my comment is intended to define the statistical population of interest, not steer towards opinionated discussion.  The question itself seems to seek statistical explanation.
Comment: @Aksakal voting is definitely not an opinion poll and should not be treated as so.
Comment: *Is the concept of sampling error relevant in election results?* is a focused statistical question that would be well on-topic here, in my opinion. *What does the 51.9% Brexit vote to leave mean? E.g. does it mean that the public really wants to leave EU? Does it simply mean that the public is unsure and needs more time to think? Or is it something else?* is inherently unanswerable here because it asks lots of questions about "what voters really want" that lie far beyond the scope of this site - far better suited to Politics Stack Exchange!
Comment: @MartinArgerami you are right abut discussing it in details is off-topic
Comment: For the record: I did not vote to close as opinion-based (which is the closure reason now), I voted to close as off-topic. I have now checked revision history and I am not sure I understand why it was re-opened at all, after @whuber's initial closure (as off-topic).
Comment: @Amoeba The system is a little confusing that way.  It appears that when a mod votes to close, only that reason is posted even when others gave other reasons.
Comment: It's a pity that the question was closed, because the subject is interesting. It would be possible to moderate it to keep the pure politics out and statistics in. The idea of election is to find out what people want. Statisticians could opine on how to do it best. Obviously, nobody's going to listen, but it's still worth a discussion
Answer: I agree with @Underminer that there is no sampling error, but not because sample is large, but because there was no sampling involved. Nobody was sampled to vote. There obviously was some negligible fraction of people who wanted to vote but, weren't able to (e.g. had car accident on this day), or who made invalid votes, but that's the only "sampling" in here.
The result is exact, there is no error involved since the whole population took part in vote (some took part by not taking part in it). Some people decided to vote, some didn't. Some decided to vote on leave, some didn't. Democracy is not about statistical significance, but about what really happened. Voting is not intended to learn about people opinion, but to make a decision. Actually, people sometimes do not vote according to what they think, but to manifest, or achieve something. For example, in election people may vote not to their preferred candidate, but to their second preferred one if they think he has greater chances of winning.
Comment: @caveman no matter if they are sure, or not, what matters is how they voted since voting is about actual votes. For sure, some people did not have clear opinion, with some of them voting and some not, but this also doesn't matter since what counts is the actual votes of those who voted.
Comment: @caveman people change their minds all over the time, psychologists wrote thousands of papers about this... Yes, 51.9% doesn't mean that exactly 51.9% of Brits is 100% sure about leaving EU. People can even be unsure about comparing lengths of lines (https://en.wikipedia.org/wiki/Asch_conformity_experiments)...
Comment: @Aksakal I am not going to comment on who is eligible to vote and who isn't. I am also not going to comment on how difficult it might be to obtain the necessary credentials. That is politics and as such not on-topic here. From a statistical perspective, each eligible voter has a certain probability of not voting. This probability might be influenced by certain factors that may or may not be related to their preferences, but each eligible voter chooses (not) to exercise that right at his/her discretion.
Comment: Consider the case of a grey area where the voting population isn't very sure about what's good for them. For example, the case of having 2 candidates that are almost equally good. In such case, I think those who vote, will probably differ unsystematically as I think their votes might have a distribution close to a uniform one. My goal here is not to re-define democracy (a political topic) but rather to see what can we say about whether Brexit was a grey area?
Comment: If I understand it correctly, your point is about how democracy interprets votes? I agree with you. However, I am not interpreting it in the way politicians do. I am trying to use the population to identify whether a decision is good, bad, or not very clear. This is a different usage of voting.
Comment: @caveman if you considered voting in terms of "error", then is such cases as Brexit it would be **impossible** to make decision since the percentage is close and you could always argue that it was "not enough". 50% + 1 vote is enough, end of story.
Comment: I don't consider it as a voting error. **Assumption 1** states that there are no voting errors. However, there could be some error in the natural neural network in the heads of the voters when modelling the voting process as a solution to find optimal solution to the Brexit problem. I.e., it can be thought as a *classification* error. I  am not discussing the political interpretation. Yes, I know that politicians simply count 50+1 as enough, but this isn't about the political view. This is rather about a statistical view to try to estimate the confidence of the biological classifiers on Brexit
Comment: You're oversimplifying the voting setup. For instance, in some places, such as USA, there are voting violations, where members of certain communities are systematically denied their voting rights by different means. It's done so that you could say "they didn't show up, they must not have wanted to vote." This happens all the time at all levels up to the presidential elections. You can also count on the media that systematically shapes opinion in ways that are beneficial to some forces etc. The world is not as simple and clean as you try to make it look
Comment: @Aksakal I didn't say anything like this... Still, besides voting violations (and I do not seem we are discussing them in here), there is no "random sampling" involved.
Comment: @Tim you wrote "there was no sampling involved." Unless British voting is perfect, there is sampling involved. In USA there's this [voting ID laws](http://www.huffingtonpost.com/2013/08/25/colin-powell-voter-id-laws-backfire_n_3813092.html) issue, which is induces a systematical bias against certain minorities into the sample.
Comment: @Aksakal That may be stratification, perhaps, but it still is not sampling. Sampling involves the *investigator* selecting the individuals to be counted, whereas in a voting process the individuals themselves make the selection. This matters, since a voting process, as Tim stated, does not involve statistics. Approximately 25% of eligible voters decided not to participate. It is impossible, even by holding a second referendum (since opinions change over time), to figure out the opinions of this silent 25%. The only fact available is that 51.9% of the votes were cast in favour of Brexit.
Comment: @user3697176, it's not entirely true. As I wrote before some voters are regularly denied their voting rights. They did not choose to skip voting, but were filtered out.
Comment: @user3697176 the probability of being denied voting rights is higher for minorities, young and otherwise vulnerable population. You could say that missing voting data is not at random. This is a sampling issue.
Answer: 51.9% is the percentage of voters who want to leave.  Since the sample size is so large (>33 million), there is virtually no random sampling error.
Statistical significance testing would try to determine if the difference in remain and leave could be explained by random sampling error alone, and the difference would certainly be significant (see @caveman's answer).
The problem with this approach is that statistical significance makes a very strong assumption that the sample is representative of the entire population (all of Britain), not just those who vote.
The non-response rate (those that do not vote) is enormously important in determining if more than half of all of Britain wants to 'leave', and is difficult to measure.  Non-response bias is created when subgroups who are less likely to vote have systematically different views.  Based on exit-polls, for example, millennials were less likely to vote, but more likely to vote to remain, which biases the results when trying to represent the population of all of Britain.
For this reason, statistical significance testing in its traditional sense is largely inappropriate.

Assumptions:  We need to define some terms for any of this to make sense and avoid political discussion of what voting is trying to accomplish.  Here are my definitions:
Population:  Every person living in Britain
Sampling Frame:  Every voting eligible person capable of voting
Sampling Methodology:  Voluntary response, the act of voting is participating in the survey
Sample:  The individuals who actually vote
In this setup, the sample proportion could be used (for better or worse) to estimate the percentage of all people who lean towards remain (or leave).
Answer: You ask

What does the 51.9% Brexit vote to leave mean?

It means 51.9% of the voters voted to leave.

E.g. does it mean that the public really wants to leave EU? Does it simply mean that the public is unsure and needs more time to think? Or is it something else?

The votes comprised $17\,421\,887$ "leave" votes and $16\,146\,297$ "remain" votes, indicating $12\,931\,353$ eligible voters did not vote and approximately $18$ million inhabitants are not eligible voters.  Since neither the collection of actual voters nor the collection of eligible voters is "the public" and neither is a representative (random, unbiased, pick a relevant adjective) sample of "the public", the 51.9% Brexit vote is noninforming to your second and subsequent questions.
It might have been possible to construct a questionnaire responsive to your questions.  This does not seem to have been what happened in the referendum as implemented.
Comment: Caveman, this comment, more than any other, demonstrates your question is non-statistical.  Because 51.9% (together with the total counts) constitute *all* the data in evidence about the voters, and there is no uncertainty (unless you want to challenge the accuracy of the counting, which is a separate issue), your rejection of this answer implies you are looking for *non-statistical* conclusions.
Comment: Could you please discuss the meaning of the votes in relation to the *voters* (i.e. not the entire population), beyond the surface conclusion that it means "*51.9% voted leave*"? I wonder what is the extent of information we can extract from this.
Comment: What if we model Brexit as a binary classification problem, and consider voters as estimates of classifiers that are a member of an ensemble. In this model, the goal is not to identify what the majority of citizens want, but rather the goal is to identify the optimal classifier from the space of classifiers. We can then use some measures to test the goodness of such human-voter-based classifier ensemble. E.g. we may use Perplexity or something else that is suitable for this binary classification task where ground truth is unknown (e.g. we clearly don't know if leave is better than remain).
Comment: @caveman : Given that the ground truth is (correctly) unknown, what metric would you use to "identify the optimal classifier from the space of classifiers"?  Any such metric encodes the biases of the analyst that picks the metric, except for the metric "reproduces the result of the vote", for which metric you already know the answer: 51.9%/48.1%.
Comment: @EricTowers I've taken this to politics.stackexchange.com where I talked about different methods  -- https://politics.stackexchange.com/questions/11433/voting-methods-that-take-voters-stability-into-account
Answer: TL;DR
I simulated an unsure population below (under details) for $R=1000$ times, and then measured the probability of observing a leave vote of $\ge 51.9\%$ under such unsure simulated population. This gave me the simulated probability that an unsure population can reach a leave vote that is $51.9\%$ or greater.
This simulated probability of leave under the unsure population is $0$.
Maybe redundant, but I also did the same but with remain to measure the probability that such unsure population to get a $\le 48.1\%$ vote remain.
This simulated probability of remain under the unsure population is also $0$.
Therefore I conclude that the Brexit vote is not a noisy side effect of an unsure or confused population. There seems to be a systematic reason that is deriving them to leave EU.
I uploaded the simulator code here: https://github.com/Al-Caveman/Brexit
Details
Given Assumption 1, the possible answers (or hypothesis) are:

$H_0$: The public is unsure.
$H_1$: The public confidently wants to leave.

Note: that it is impossible that the public confidently wants to remain because we have ruled out voting errors.
To answer this question (i.e. whether $H_0$ or $H_1$), I try to measure:

The probability that an unsure population can achieve $\ge 51.9\%$ leave vote.
Or, probability that an unsure population can achieve $\le 1-51.9\%$ remain vote.

If this probability is low enough, we can conclude that the public confidently wants to leave (i.e. $H_1$). However, if this probability is large enough, we can conclude that the public is unsure about deciding Brexit (i.e. $H_0$).
In order to measure this probability, we need to know the distribution of an unsure British population in such a binary voting system as Brexit. Therefore, my first step is to this is to simulate this distribution by following the assumption below:

Assumption 2: a population that is composed of unsure individuals will have a random chance vote. I.e. every possible answer has an equal chance of being chosen.

In my view this assumption is fair/reasonable.
Additionally, we model the leave and remain campaigns as two distinct processes as follows:

Process $P_{\text{leave}}$ with the output $O_{\text{leave}} = [l_1, l_2, \ldots, l_n]$.
Process $P_{\text{remain}}$ with the output $O_{\text{remain}} = [r_1, r_2, \ldots, r_n]$.

where:

$n$ is the total population of UK (includes non-voters).
For any $i \in \{1,2,\ldots,n\}$, $l_i,r_i \in \{0, 1\}$. An output value of $0$ signifies that a voter has voted no for the subject process, and $1$ significances that a voter has voted yes for the same process.

subject to the following constraint:

For any $i \in \{1,2,\ldots,n\}$, $l_i$ and $r_i$ cannot simultaneously be $1$ at the same time. I.e. $l_i=1$ necessarily implies that $r_i = 0$, and $r_i=1$ necessarily implies that $l_i=0$. This is due to the fact that a voter $i$ among the population $\{1,2,\ldots,n\}$ cannot vote to both leave and remain at the same time.

For example, if $O_{\text{leave}} = [1,0,0]$, it means that out of a population of $3$, one has voted yes to leave and two have voted no to leave.
Likewise, if $O_{\text{remain}} = [0,1,0]$, it means that out of a population of $3$, one has voted yes to remain and two have voted no to remain.
Note that in both of the examples above, there is one member of the population that has not voted for any of the processes (or campaigns). Specifically, the third voter (i.e. $O_{\text{leave}}[3] = O_{\text{remain}}[3] = 0$).
What we know from here is that out of $33,568,184$ ballot papers, $51.9\%$ have voted to leave EU (i.e. $100-51.9=48.1\%$ voted to remain). This means:

$n = 33,568,184$.
$33,568,184 \times 0.519 = 17,421,887.496$ have voted yes to the leave campaign. I.e.
$$
\sum_{i=1}^{33,568,184}O_{\text{leave}}[i] = 17,421,887.496 \approx 17,421,887
$$
$33,568,184 \times (1-0.519) = 16,146,296.504$ have voted yes to the remain campaign. I.e.
$$
\sum_{i=1}^{33,568,184}O_{\text{remain}}[i] = 16,146,296.504 \approx 16,146,297
$$

Therefore, we define the output arrays as follows:

For all $i \in \{1,2,\ldots, 17421887\}$, $O_{\text{leave}}[i] = 1$.
For all $i \in \{17421887+1,17421887+2,\ldots, 33568184\}$, $O_{\text{leave}}[i] = 0$.
For all $i \in \{1,2,\ldots, 17421887\}$, $O_{\text{remain}}[i] = 0$.
For all $i \in \{17421887+1,17421887+2,\ldots, 33568184\}$, $O_{\text{remain}}[i] = 1$.
By Assumption 2, for all $i \in \{1,2,\ldots, 33568184\}$, $O_{\text{unsure},m}[i] = C$, where $C$ is a uniformly distributed random variable that takes values in $\{0,1\}$ (e.g. a fair coin toss), and $m$ is a number that identifies a particular random instantiation of $O_{\text{unsure},m}$. In other words, the probability that two distinct random instantiations of $O_{\text{unsure},m}$ equal each other, i.e. $O_{\text{unsure},1} = O_{\text{unsure},2}$, is $0.5^{33,568,184}$.

Finally, we define the $p_{\text{leave}}$ value of the leave process as follows:
$$
p_{\text{leave}} = \frac{1}{R}\sum_{m=1}^R
  \begin{cases}
    1 & \text{if } \Big(\sum_{i=1}^{33,568,184} O_{\text{leave}}[i]\Big) \le \Big(\sum_{i=1}^{33,568,184} O_{\text{unsure},m}[i]\Big)\\
    0 & \text{else}
  \end{cases}
$$
where $R$ is total number of simulation rounds by which at each time a random instance of $O_{\text{unsure},m}$ is defined.
Likewise, we define the $p_{\text{remain}}$ value of the remain process as follows:
$$
p_{\text{remain}} = \frac{1}{R}\sum_{m=1}^R
  \begin{cases}
    1 & \text{if } \Big(\sum_{i=1}^{33,568,184} O_{\text{remain}}[i]\Big) \ge \Big(\sum_{i=1}^{33,568,184} O_{\text{unsure},m}[i]\Big)\\
    0 & \text{else}
  \end{cases}
$$
To answer that, I simulated the above in C using $R=1,000$ and the output is:
<code>total leave votes: 17421887
total remain votes: 16146297
simulating p values............ ok
p value for leave: 0.000000
p value for remain: 0.000000
</code>
In other words:

$p_{\text{leave}} = 0$.
$p_{\text{remain}} = 0$.
Comment: There is no statistically satisfactory way to deal with non-random missing data.
Comment: There is a confusion here about the nature & scope of statistics. You are attempting to create a *process* model of voting, & how that can inform the mechanisms & validity of governance & public decision making. This is a worthwhile task *in Political Science*. It is simply not statistics (although statistics is involved).
Comment: Perhaps more important in this case is the non-response rate (i.e. individuals who do no vote).  The margin of error (or measure of statistical significance) only takes into account random sample error.  Non-response bias is NOT included in this, and it is much more impactful than random sampling error with a poll with such a large sample size.
Comment: Here it says that UK has $46,499,537$ eligible voters. Meaning $46,499,537 - (17421887+16146297) = 12,931,353$ didn't vote. Any idea how to interpret such unvoting population? Source: https://en.wikipedia.org/wiki/European_Union_Referendum_Act_2015#Eligible_voters
Comment: Those who haven't voted, could be composed of individuals  that don't care about politics (e.g. no more trust). Alternatively, it such unvoters could be those who were not sure. Or, it could be a mixture of the two. What would happen if we assume that "*all unvoters are unsure*"? Would this be an upper bound for testing whether the current situation was one where the public was feeling that Brexit was a *grey area*?
Answer: You could ask a slightly different question: Assuming that 50% of a very large population voted "Yes", and you asked a random sample of size S, what is the probability that 51.9% of your sample responded "Yes", depending on the sample size?
The expected value of number of "Yes" votes is 0.5 S. The variance is 0.25 S. The standard definition is 0.5 $S^{1/2}$. A deviation of the actual from the expected number of "Yes" votes more than 6.1 standard deviations has a chance of one in a billion.
We have this when 0.019 S (difference between 50% and 51.9%) is 6.1 * 0.5 * $S^{1/2}$, or S = $(6.1 * 0.5 / 0.019)^2$ or S ≈ 25,800.
Answer: This is another solution using an analytical method instead of a simulation.
Previously, I have simulated an unsure population to be one that its vote is random chance guessing. So out of $n$ many voters, an unsure population would tend to vote leave or remain for $0.5$ of the time.
In order for an unsure population to get exactly $51.9\%$ vote on leave, there needs to be $17,421,887$ 1s in $O_{\text{leave}}$. The probability for this is $0.5^{33,568,184}$. Similarly, the probability of getting $17,421,887 + 1$ votes is also $0.5^{33,568,184}$. This goes on.
This is the probability of getting $\ge 17,421,887$ votes:
$$\begin{split}
\sum_{i=17,421,887}^{33,568,184} 0.5^{33,568,184} &= (33,568,184-17,421,887) \times 0.5^{33,568,184}\\
&= 8.39663381928984×10^-10105024\\
&\approx 0\\
\end{split}
$$
($8.39663381928984×10^-10105024$ calculated by Wolframalpha)
And this is the probability of having $\ge 51.9\%$ of an unsure population vote leave.


How can I move the location of the advanced search form?

Question: This has been giving me troubles for some time now. I am in the process of changing my search results page on my drupal 7 site. I have modified both the search-result and search-results template files to change how my results are displayed on the page.
However I can't figure out how to move the placement of the advanced search form. It is driving me crazy. I have created my own "advanced search" button, and want to give it the dropdown functionality that the default "advanced search" link has.
I know that I can change the "node_form_search_form_alter" function from node.module to alter the guts of this form, but thats not what I want. I just want to place it on the screen somewhere else.
I can't for the life of me figure out how to do this. I have gone through every template file, and the advanced search form just seems to magically appear. Any help would be GREATLY appreciated.
In the image below, you can see the default advanced search link, but I want to move it to my button that I have added, on the right of the page.
Answer: One option is the Advanced search block module which makes the advanced search form available as a configurable block.

Provides the advanced search form (normally displayed above search results) as a block. The block includes default values for the advanced search fields making it easy to refine searches.

From a brief glance at this module's code, it looks like it hides the default form and re-creates the advanced search form in a block.
Comment: awesome thanks! Appreciate it. I should have mentioned I was hoping not to use a module but looks like I might have to.
Comment: You're welcome! I understand not wanting to add a module for this. Seems like there should be a simpler theming solution but I couldn't find it... Good luck! :)


jQuery validation: .each with form

Question: If I have a form of about 40 questions, how do I apply the same rules to questions 1-20, and 21-40?
For example:
$("#form_survey").validate({
    rules: {
        a_ +i: {max:12, maxlength:2},
    },
    messages: {
        a_ +i:{
            max: "That's too much!"
        }

    }

Where the "+i" is the ideal increment of +1...
Should be easy, I'm just stuck on syntax...
Answer: You could have them all use the same class: class="question". Then, use the class to create the validation:

$(".question").each(function (i) {
  this.validate({
      rules: {
          a_ +i: {max:12, maxlength:2},
      },
      messages: {
          a_ +i:{
              max: "That's too much!"
          }
      }
  }
});

Comment: I have to pass rules to the field, right?  It doesn't automatically apply validation rules to these classes...

Maybe you could explain a bit more?
Comment: Hmm... I was hoping that this would apply them to all of the fields using the target class. But if that is not the case then another method would be required. Maybe you could still use the class to iterate over the fields with `each`, and then call into your code for each object?
Comment: That's what I'm going for, I think.  If you could help with the code, I'd be thrilled!
Comment: This looks closer, but this throws an error: "Missing : after property id"
Answer: This solves my problem:
$('.text-input').addClass('hours');
jQuery.validator.addClassRules("hours", {
  required: true,
  minlength: 2
});

jQuery validation: .each with form


como aumentar o limite de ifelse no R?

Question: Preciso calcular os anos de estudo a partir do questionário da pnad.
Fiz uma programação parecida com esta abaixo, mas tenho muitos ifelse dentro do outro (121 para ser exato), pois tenho uma cadeia de perguntas maior, mas o R não calcula além de 50 linhas de ifelse.
pnad2015$agua<-ifelse(v0211==1 & v0212==2, 0,
               ifelse (v0211==1 & v0212==4, 1,
               ifelse (v0211==1 & v0212==6, 1,
               ifelse (v0211==3 & v0213==1, 0,
               ifelse (v0211==3 & v0213==3, 1, NA)))))

Answer: Uma possível forma é usar o case_when do dplyr. Fiz alguns testes e não encontrei a limitação de muitos casos como o ifelse possui. No seu caso, ficaria assim:
case_when(
  v0211==1 & v0212==2 ~ 0,
  v0211==1 & v0212==4 ~ 1,
  v0211==1 & v0212==6 ~ 1,
  v0211==3 & v0213==1 ~ 0,
  v0211==3 & v0213==3 ~ 1,
  TRUE ~ as.numeric(NA)
)

Veja que cada caso é definido da forma condicao ~ resultado se verdadeiro e separado por vírgulas. As condições são avaliadas sequencialmente, assim como o ifelse. A última condição TRUE ~ NA, indica que o valor NA será incluido caso nenhuma das aneteriores seja correspondida. Para ter a função case_when, carregue o pacote com library(dplyr).
Note que: o limite de chamadas não é relacionado ao ifelse e sim ao R. O R limita o número de funções que podem ser chamadas uma dentro da outra (chamado de parse context). Veja este exemplo:
f <- function(x) return(1)

f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
Error: contextstack overflow at line 1

O limite do R é definido a 50 chamadas. E é definido nesta linha do código fonte do R. Ou seja, para alterar este limite você precisa mudar o código fonte do R. Segue uma referência no stackoverflow
Comment: Funcionou
Obrigado
Comment: Obrigado.
agora apareceu este erro
´Error: RHS of case 123 (NA) has type 'logical' not 'double'`
Comment: Editei a resposta. Adicione: as.numeric(NA) na última condição. A `case_when` não deixa tipos diferentes aparecerem no mesmo vetor.

como aumentar o limite de ifelse no R?