Given a regression model with heteroscedasticity, find generalized least squares estimator?

Question: I have $Y_i=\beta_0+U_i, E(U_i)=0, var(U_i)=2log|Z_i|, cov(U_i;U_j)=0$ when $i\neq j$. Suppose there are $n$ observations on $Y_i$ and $Z_i$.
How do I use this information to find the GLS estimator and how to find the variance of this estimator?
Comment: When $-1<Z_i<1$, $var(U_i)<0$?
Comment: What more can you say about $Z_i$?
Answer: With $V$ the variance-covariance matrix of the errors, GLS is
$$(X'V^{-1}X)^{-1}X'V^{-1}y$$
In your case, $X=\textbf{1}$, a vector of ones. Due to lack of covariance of the errors, $V^{-1}$ will be diagonal. I denote the elements of $V^{-1}$ by $\sigma^{-2}_i, i=1,\ldots,n$. Then, $X'V^{-1}X=\sum_i\sigma^{-2}_i$ and $X'V^{-1}y=\sum_iy_i/\sigma^{2}_i$.


Reactive.NET Conditional Throttle

Question: Is there a way to have conditional throttle.
I have a class which has an event with 2 parameters (control sender, string text).I would like to use this event in my Rx code, and use throttling. The only problem is that i would like to throttle text, only for the same sender.
So if the sender = textbox1, then throttle for 300 seconds, however if the sender changes then ignore throttle and send the event up in the chain.
sender=textbox1, text = 'm' within 300 seconds (ignore)
sender textbox1, text = 'mu' within 300 seconds (ignore)
sender textbox1, text = 'muk' more than 300 seconds (process)
sender=textbox1, text = 'm' within 300 seconds(ignore)
sender=textbox2, text = 'y' within 300 seconds(process) //as the sender has changed now.
Comment: Which event do you want to send up the chain?  It seems like you'd want to send "textbox1", "m" up the chain since now you have an event from textbox2.  But you would not want to send textbox2 up the chain unless 300s goes by otherwise you would not be throttling lots of textbox2 events?  Is that what you mean?
Comment: Why not throttle and buffer them separately, then merge the results as they come in?
Answer: The following code will make sure that 300ms have passed before showing a value, unless the sender has changed.
var source = new Subject<Pair>();

// The Publish().RefCount() and Subscribe() are to make the sequence hot
var changedSender = source.DistinctUntilChanged(p => p.Sender).Publish().RefCount();
changedSender.Subscribe();

var throttled = source.Select(p =>
    Observable.Amb(changedSender, source.Skip(TimeSpan.FromMilliseconds(300))).Take(1))
    .Concat();

throttled.Subscribe(WritePair);

source.OnNext(new Pair("A", "i"));
System.Threading.Thread.Sleep(10);
source.OnNext(new Pair("A", "it"));
System.Threading.Thread.Sleep(10);
source.OnNext(new Pair("A", "bit"));
System.Threading.Thread.Sleep(500);
source.OnNext(new Pair("A", "bite"));
source.OnNext(new Pair("B", "a"));
System.Threading.Thread.Sleep(10);
source.OnNext(new Pair("A", "bitey"));
System.Threading.Thread.Sleep(500);
source.OnNext(new Pair("A", "at"));

This produces the following output:
A: bite
B: a
A: bitey
A: at

Comment: Thanks for the answer. Although i realised i want something a little bit different that the output. What i want is that is show A: bite before B:a. I want the previous value before the change. i made the some changes to your code to make this work.
Answer: This is what i have done to make this work
        var source = new Subject<Pair>();

        // The Publish().RefCount() and Subscribe() are to make the sequence hot
        //var changedSender = source.DistinctUntilChanged(p => p.Sender).Publish().RefCount();
        var changedSender = source.Zip(source.Skip(1), (previous, next) => new { Previous = previous, Next = next }).DistinctUntilChanged(arg => arg.Next.Sender).Select(o => o.Previous).Publish().RefCount();
        changedSender.Subscribe();
        var either = changedSender.Merge(source.Throttle(TimeSpan.FromMilliseconds(300))); //.Do(o => Console.WriteLine("Do {0}", o.ToString()));
        either.Subscribe(o => o.Print());

        source.OnNext(new Pair("A", "i"));
        System.Threading.Thread.Sleep(10);
        source.OnNext(new Pair("A", "it"));
        System.Threading.Thread.Sleep(10);
        source.OnNext(new Pair("A", "bit"));
        System.Threading.Thread.Sleep(500);
        source.OnNext(new Pair("A", "bite"));
        source.OnNext(new Pair("A", "bite1"));
        source.OnNext(new Pair("A", "bite2"));
        source.OnNext(new Pair("B", "a"));
        source.OnNext(new Pair("B", "ani"));
        source.OnNext(new Pair("B", "animal"));
        System.Threading.Thread.Sleep(10);
        source.OnNext(new Pair("A", "bitey"));
        System.Threading.Thread.Sleep(500);
        source.OnNext(new Pair("A", "at"));
        source.OnNext(new Pair("B", "empty"));
        source.OnNext(new Pair("A", "empty"));
        source.OnNext(new Pair("C", "new"));

        Console.ReadLine();

Reactive.NET Conditional Throttle


Connect to private/auth failed: No such file or directory

Question: I've setup postfix, dovecot and roundcube. receiving and sending email via roundcube is working as well as receiving emails via imap with another email client.
But I'm not able to send emails with an external email client. As soon as I connect, I see:
postfix/smtpd[1258]: warning: SASL: Connect to private/auth failed: No such file or directory
postfix/smtpd[1258]: fatal: no SASL authentication mechanisms

in mail.log.
here my dovecot config:
dovecot -n
# 2.1.7: /etc/dovecot/dovecot.conf
# OS: Linux 3.2.0-4-amd64 x86_64 Debian 7.6
mail_location = mbox:~/mail:INBOX=/var/mail/%u
namespace inbox {
  inbox = yes
  location =
  mailbox Drafts {
    special_use = \Drafts
  }
  mailbox Junk {
    special_use = \Junk
  }
  mailbox Sent {
    special_use = \Sent
  }
  mailbox "Sent Messages" {
    special_use = \Sent
  }
  mailbox Trash {
    special_use = \Trash
  }
  prefix =
}
passdb {
  args = /etc/dovecot/dovecot-sql.conf
  driver = sql
}
protocols = imap pop3
ssl_cert = </etc/dovecot/dovecot.pem
ssl_key = </etc/dovecot/private/dovecot.pem
userdb {
  args = /etc/dovecot/dovecot-sql.conf
  driver = sql
}

here my postfix config:
postconf -n
alias_database = hash:/etc/aliases
alias_maps = pgsql:/etc/postfix/pgsql-aliases.cf
append_dot_mydomain = no
biff = no
config_directory = /etc/postfix
default_transport = error
dovecot_destination_recipient_limit = 1
html_directory = /usr/share/doc/postfix/html
inet_interfaces = 127.0.0.1, 192.168.1.134
inet_protocols = ipv4
local_recipient_maps = $virtual_mailbox_maps
local_transport = virtual
mailbox_size_limit = 0
mydestination = localhost
myhostname = vocdemosvr01.voxtelo.com
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
myorigin = $mydomain
readme_directory = /usr/share/doc/postfix
recipient_delimiter = +
relay_domains = proxy:pgsql:/etc/postfix/pgsql/relay_domains.cf
relay_transport = error
relayhost =
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
smtpd_sasl_auth_enable = yes
smtpd_sasl_path = private/auth
smtpd_sasl_type = dovecot
smtpd_tls_cert_file = /etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file = /etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtpd_use_tls = yes
virtual_alias_maps = proxy:pgsql:/etc/postfix/pgsql/virtual_alias_maps.cf
virtual_gid_maps = static:12
virtual_mailbox_base = /var/vmail
virtual_mailbox_domains = proxy:pgsql:/etc/postfix/pgsql/virtual_domains_maps.cf
virtual_mailbox_limit = 512000000
virtual_mailbox_maps = proxy:pgsql:/etc/postfix/pgsql/virtual_mailbox_maps.cf
virtual_minimum_uid = 8
virtual_transport = dovecot
virtual_uid_maps = static:8
voxdemosvr01:/var/spool/postfix/private#

Which configuration do I need to check?
Comment: I'm going to take a random guess and say your SASL configuration in the Postfix configuration and your SASL authentication daemon.
Comment: More seriously in the right column in this screen under **related** you might find useful answers to similar questions
Answer: Check
/etc/dovecot/conf.d/10-master.conf

for line
unix_listener private/auth

change it to
unix_listener /var/spool/postfix/private/auth

Restart dovecot and try again. If still doesn't work, post output of dovecot -n and postconf -n.
Comment: so no help? i have same issue!
Comment: For me this was related to a syntax error in conf.d/auth-sql.conf.ext.  Just thought I'd point out that a fatal error in any of these configuration files can cause this.  `dovecot -n` led me to figure this out.  Thanks @Tutul!
Comment: It is already like this. I've added the outputs to the description, thanks.
Comment: this solved my problem. I was unable to my smtps service (465/submissions) using the openssl client (https://serverfault.com/a/644122/253935). Now I can send emails via nodemailer using my server as a secure SMTP server.
Answer: After spending some hours trying to figure out this, I finally fixed my problem, hope this helps to others...
In the postfix/main.cf file you have something like:
smtpd_sasl_path = private/auth

and in the dovecot/conf.d/10-master.conf you have something like:
service auth {
    # auth_socket_path points to this userdb socket by default. It's typically
    # used by dovecot-lda, doveadm, possibly imap process, etc. Its default
    # permissions make it readable only by root, but you may need to relax these
    # permissions. Users that have access to this socket are able to get a list
    # of all usernames and get results of everyone's userdb lookups.
    unix_listener /var/spool/postfix/private/auth {
      mode = 0666
      user = postfix
      group = postfix
    }

  ...

in my case the "private/auth" in the first file was not matching the unix_listener path, as soon as I put this one equal to the first it solved the problem. My guess is that the smtpd_sasl_path has to be the same as the /var/spool/postfix/... otherwise it will fail.
Hope this helps,
Answer: Before doing any of re-jigging of your paths, check the dovecot configuration syntax with:
dovecot -n

That command should dump the dovecot configuration file to screen, if it does not then it will show the first error it encountered. Edit the file to fix errors, if any and you should be good to go.
If you get a  Fatal: Error in configuration file /etc/dovecot/dovecot.conf line 6: Expecting '=' or similar, then simply deleting the spaces before and / or after the equals sign on the denoted line should fix it (I think it is an encoding issue).
Answer: Somehow, in addition to the other settings, I had to add the following line to my main.cf:
queue_directory = /var/spool/postfix

Answer: Another place to check is /etc/postfix/master.cf in the submission configuration. Make sure the smtpd_sasl_path variable there matches the one in dovecot's configuration files.
Answer: I had a similar issue when setting up iRedMail dockerized version on Ubuntu 20.04.
I realized that Dovecot was not running in the Docker container. And when I tried starting the service the command service dovecot start I got the error
root@mail:/var/spool/postfix# service dovecot start
 * Starting IMAP/POP3 mail server dovecot
Error: bind(/var/spool/postfix/private/dovecot-auth) failed: No such file or directory
Fatal: Failed to start listeners

I also ran the command below to confirm that my dovecot configuration was fine:
dovecot -n

And yes the output from the command showed that it was fine.
Here's how I fixed it:
The issue was caused by Postfix not being installed/running on the in my container which I discovered when I checked the docker logs for the container using - docker logs container-id. The file /var/spool/postfix/private/dovecot-auth which Dovecot was referencing was supposed to be created by Postfix, however, since the script to install Postfix failed because the correct path to the script was not picked when building the iRedMail image, this issue then came up as Dovecot could not find the file /var/spool/postfix/private/dovecot-auth:
[iRedMail] [Entrypoint] /docker/entrypoints/postfix.sh
/docker/entrypoints/functions.sh: line 113: /docker/entrypoints/postfix.sh: No such file or directory
[iRedMail] [Entrypoint] /docker/entrypoints/mlmmj.sh
[iRedMail] [Entrypoint] /docker/entrypoints/mlmmjadmin.sh
[iRedMail] [Entrypoint] /docker/entrypoints/iredapd.sh
[iRedMail] [Entrypoint] /docker/entrypoints/antispam.sh
mail: cannot send message: Process exited with a non-zero status

All I had to do was to pull the docker repository for iRedMail using git clone -b stable https://github.com/iredmail/dockerized, rebuilt the image in my local using docker build . --tag iredmail:latest -f dockerized/Dockerfiles/Dockerfile, and then run it. This time the correct location of my Postfix install script was picked, and the Postfix installation and setup ran fine.
And when I checked Dovecot service again using:
service dovecot start

It showed me that it was running fine.
That's all
Answer: For the latest version of postfix using Docker edit dovecot/conf.d/10-master.conf
Change
  unix_listener /var/spool/postfix/private/auth {

with this
 unix_listener /dev/shm/sasl-auth.sock {

Comment: For me uncommenting the first line and its block made it work. It was commented out by default.


C#/WPF - Is It possible to create a WebBrowser in a second AppDomain and display it in the first AppDomain?

Question: I have a WPF application and have been dealing with what seems to be a well documented memory leak in the WebBrowser control.  I have read through many articles to no avail.
I am new to AppDomains.
Is it possible to create a WebBrowser object that lives in a second AppDomain and then add it to a wpf window that exists in my first/original AppDomain?  I am hoping that destroying the second AppDomain when I am done using the WebBrowser control will free up memory that would have otherwise been leaked.
To play around with this I have created a new WPF project (.net 3.5) and have done the following but receive an XamlParseException at the last statement.  I was under the impression the last statement should be giving me a reference to a new WebBrowser control in the second AppDomain.
Any thoughts or guidance is appreciated.
<code>    String pathToDll = @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client\PresentationFramework.dll";

    Type t = typeof(WebBrowser);

    AppDomain newAppDomain = AppDomain.CreateDomain("New domain name");

    WebBrowser webBrowserFromOtherDomain = (WebBrowser)newAppDomain.CreateInstanceAndUnwrap(pathToDll, t.FullName);
</code>
Answer: I think you are out of luck, WPF controls don't inherit from MarshalByRefObject, so they cannot cross domain boundaries.
Also, the CreateInstanceAndUnwrap expects an assembly fully qualified name, you can get it from Assembly.FullName (but still it should not work as the object cannot cross domain boundaries).
Comment: After doing more reading it appears that even if this was an element that inherited from MarshalByRefObject, I would only be able to interact with the object through a proxy.  To me this sounds like I wouldn't be able to grab an instance and add it to a container (was thinking of using System.Windows.Forms.WebBrowser after initially reading your comment).  Does this sound correct?
Comment: Hmmm, not really sure, I have done cross-domain object uses, and yes, it's done through a proxy, but a transparent one, you get what you think it's your class instance but it really is a proxy. WinForm controls do inherit from MarshalByRefObject, so I assume their proxy will allow to do UI manipulation on different domains.
Comment: Thanks! I will try it out and see what happens.


firstorcreate is not working for new entry laravel

Question: I am new to laravel and using ardent for creating models, I have below base class for all model.
class MainModel extends Ardent {
  //with some common methods
}

and now i have sub model class such user
class User extends MainModel
{
  //with user table and other methods related to user
 }

and also I have repository for user ->User Repository and I am calling firstOrcreate method to check(user entry) and create user.
if user is exists in database then above method is working fine ,its returning existing user object but if user is not exists in database it is not creating user also its not inserting entry to table.is I am doing wrong here?
Comment: Does your log file give any insight into what the problem might be?
Answer: You just need to save the user after calling firstOrCreate():
$user->save();

You may want to check if the user doesn't exist before that:
if (!$user->id)  $user->save();


How to pick key words from a list, after appending a .csv file to that list?

Question: I need to be able to pick keywords from an Excel CSV file, I've appended the file to a list. The program is a Phone Troubleshoot, and I need the input ("The screen won't turn on") to have the same output as if I inputted ("The display is blank").
<code>"Troubleshooting Program to give the user a solution to a trouble they've     encountered based on inputted key words."

phoneprob=input("What problem are you having with your phone? ")
prob=open("phone.csv","r")
phone=prob.read()
prob.close()
eachProb=phone.split("\n")

print(eachProb)
problist=[eachProb]

print (problist)
</code>
Comment: Please mention what you are expecting(input and output)
Answer: Are you trying to build a keyword dictionary or retriving a sentence problem ?
In both case, you need to associate a problem to keywords.
A basic approch to get keywords is to split the sentence in words (using s.split()) and update keyword list with the most used of them...
difflib can help here.
Since we don't know the given file schema, i assume it's just a list of sentence and you provided keyword/problems elsewhere (situation Dict).
For example:
<code>csv_ret = ["I can't turn on my phone", "The screen won't turn on", "phone The display is blank"]

situations = {
    "screen": ["turn on", "blank", "display", "screen"],
    "battery": ["turn on", "phone"]
}

def get_situation_from_sentence(sentence):
    occurences = {}
    for word in sentence.split():
        for key, value in situations.items():
            if word in value:
                if occurences.get(key) is None:
                    occurences[key] = [word]
                elif word not in occurences.get(key):
                    occurences[key].append(word)
    averages = {k: ((len(v) * 100) / len(situations[k])) for k, v in occurences.items()}
    return "{}, {}".format(averages, sentence)

for sentence in csv_ret:
    print(get_situation_from_sentence(sentence))
</code>
results:

{'battery': 50.0}, I can't turn on my phone
{'screen': 25.0}, The screen won't turn on
{'screen': 50.0, 'battery': 50.0}, phone The display is blank

This code evaluate a sentence problems and related keyword match in percent.
Once again this is a very basic solution, and you probably need something more robust (lexer/parser, machine learning ...) but sometime simpler is better :)


How to get rid of "You are currently editing a commit"?

Question: Suddenly appeared:
MacBook-Pro: $ git status
On branch dev
Your branch is up-to-date with 'dropbox/dev'.
You are currently editing a commit while rebasing branch 'Releases' on '72ca998'.
  (use "git commit --amend" to amend the current commit)
  (use "git rebase --continue" once you are satisfied with your changes)

nothing to commit, working directory clean

No changed in the branch. Everything is up to date.
Comment: have you tried git rebase --continue?
Comment: @NickCatib yes. error: could not apply fbbaa72... Version 0.2.0

When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
Could not apply fbbaa72581bc420fa6c08f342886488e44fc484c... Version 0.2.0
Comment: please, [edit](https://stackoverflow.com/posts/31252363/edit) your question to add details, try not to put them in the comments.
Answer: git rebase --abort should abort the rebase operation and return to the previous state. But to be sure, what commands did you enter before?
edit: the output of git reflog can show you where the rebase started, it would look something like
7d0b010 HEAD@{0}: rebase finished: returning to refs/heads/bran
7d0b010 HEAD@{1}: rebase: adding file in branch
a7e6693 HEAD@{2}: rebase: checkout master
a7e6693 HEAD@{3}: commit: adding file in master
4c00dd7 HEAD@{4}: checkout: moving from bran to master
3cb192a HEAD@{5}: commit: adding file in branch

Answer: If you end up in this state but you need to keep changes:

Make a backup branch of the current branch: git branch my-backup
On the current branch abort the rebase: git rebase --abort
Merge changes from the backup branch created in step 1: git merge my-backup
Answer: try to use
git rebase --quit
if git rebase --abort failed.
git rebase --quit completely discards the changes.
Answer: I ran into this issue while working in Xcode. I've noticed that Xcode 14, has a habit of stashing changes, which can cause issues when rebasing.
Aborting the rebase, Quitting Xcode, then re-rebasing worked for me.


iphone renew distribution license and provisioning

Question: I have an distribution certificate which is expiring next week... then I renewed it today.
Do I have to build another provisioning profile and install it on all devices which is using the provisioning with the "old distribution certificate"?
Or there is nothing to do, and its automatically, after renew certificate?
Thanks!
Comment: I actually wasn't aware that distribution certificates could expire...at least in this short of a time
Answer: Yes you have to rebuild, the apps under the old provisioning will expire and no longer will be able to be opened...you have to release a new build under the new certificate
Comment: @BestCoder I dont think he meant for the appstore, rather an adhoc installation, and once your profile expires you need to renew it or create a new one and re distribute your app..


How to use angularjs $q.all() with an asynchronously created array of promises

Question: I'm trying to use $q.all() to wait until a bunch of promises is resolved. For this purpose, i created a promises array and I pass it to the all() method, but this doesn't seem to work.
The promises array is filled with the promises returned by the $resource objects, that are created in each iteration, and I think that's the problem because the promises array is filled asynchronously.
I have come to this conclusion because the promise, array of promises, returned by the $q.all() method is always an empty array. It's like the $q.all() method doesn't wait for the array to get filled up. But I'm not sure if that is the problem.
I will really appreciate any help in this matter and, if it is not possible to achieve what I want with $ q.all(), i would like to see other ways to execute code after all the promises are resolved.
Here is the code I am currently using
        var promisesArray = [];
        var images, postObject;
        files.readFile('images.txt')
        .then(function (data) {
            images= angular.fromJson(data);
            images.forEach(function (image, idx) {
                var deferred = $q.defer();
                if (!image.sent) {
                    files.readFile(image.name)
                    .then(function (data) {
                        postObject = angular.fromJson(data);
                        $resource(EndPointsService.baseURL + "images").save(postObject, function(data) {
                            deferred.resolve(data);
                            if (data.status) {
                                image.sent= true;
                            }
                        },function(error){
                          console.log(error);
                        });
                        promisesArray.push(deferred.promise);
                    },function(error){
                        console.log(error);
                    });
                }
            });

              $q.all(promisesArray).then(function (data) {
                    console.log(data);
                    files.writeFile("images.txt", images)
                    .then(function (data) {
                        console.log(data);
                    }, function (error) {
                        console.log(error);
                    });
              });
       },function(error){
          console.log(error)
       });

Comment: You should push the promises to the promises array outside of the async `readFile` call. You've created the deferred above, so you can add it to the array there as well.
Comment: when you say that i should "put it there as well", you mean in the next line after the declaration of deferred?
Comment: Well, technically you are creating the deferred above an if statement. So if that condition is false you've created a deferred for no reason. You should put both deferred creation and addition to the array inside that conditional.
Answer: The .then method of a promise returns a new promise from data returned to it.
Create the promise array inside the .then method and return the $q.all promise to that .then method:
̶v̶a̶r̶ ̶p̶r̶o̶m̶i̶s̶e̶s̶A̶r̶r̶a̶y̶ ̶=̶ ̶[̶]̶;̶
var images, postObject;
var arrayPromise = files.readFile('images.txt')
  .then(function (data) {
    ͟v͟a͟r͟ ͟p͟r͟o͟m͟i͟s͟e͟s͟A͟r͟r͟a͟y͟ ͟=͟ ͟[͟]͟;͟
    images= angular.fromJson(data);
    images.forEach(function (image, idx) {
        ̶v̶a̶r̶ ̶d̶e̶f̶e̶r̶r̶e̶d̶ ̶=̶ ̶$̶q̶.̶d̶e̶f̶e̶r̶(̶)̶;̶
        if (!image.sent) {
            var promise = files.readFile(image.name)
              .then(function (data) {
                postObject = angular.fromJson(data);
                var url = EndPointsService.baseURL + "images"
                ͟r͟e͟t͟u͟r͟n͟ $resource(url).save(postObject).$promise;
            });
            promisesArray.push(promise);
        };
    });
    ͟r͟e͟t͟u͟r͟n͟ $q.all(promisesArray);
});

arrayPromise.then(function (dataArray) {
    console.log(dataArray);
    //Process data here
});

Comment: Glad to know it helped. Please mark the answer as accepted so that others will know the question is answered.


Experimental design for a 96 well plate

Question: I am designing an experiment that will include an assay on a 96 well plate and could use some advice on the optimal layout.
In our experiment we have 12 biological replicates, and each replicate has been given each of two treatments.  We are measuring our primary outcome on a single plate with 4 technical replicates for each measurement (12x2x4=96).
I suspect (and have some evidence) for row and column effects for this assay, and so row and column balance seems important.
My thought was to alias biological replicates with columns as blocks, since the comparison is being made within biological replicates, and then apply either a complete randomised block design (shuffling the eight technical replicates within the block) or to restrict the permutations using something like an extended Latin square so that the treatment was perfectly balanced across both rows and columns.
Option 1: Each column is a biological replicate, four treatment A technical reps and four treatment B technical reps shuffled within each column.  This I think is easy to analyse, but possibly unbalanced across rows.

1
2
3
4
5
6
7
8
9
10
11
12

A
B
B
B
A
B
B
B
B
B
B
A

B
A
A
B
A
A
B
B
A
A
A
A

B
B
B
B
A
B
A
B
A
A
B
B

A
B
B
B
A
B
A
A
B
B
A
A

A
A
B
B
B
A
B
A
B
A
A
B

B
A
A
B
B
A
A
A
A
A
B
B

A
A
A
B
B
A
A
A
B
B
B
B

B
B
A
B
B
B
B
B
A
B
A
A

Option 2 - as before but with the restriction that treatments are balanced across rows (something like this - but built on Latin squares rather than just shuffled columns and rows as I've done it here).  This option is balanced but I think harder to analyse.

1
2
3
4
5
6
7
8
9
10
11
12

A
B
A
B
A
B
B
A
A
B
B
A

B
A
B
A
B
A
A
B
B
A
A
B

B
A
B
A
B
A
A
B
B
A
B
B

B
A
B
A
B
A
A
B
B
A
A
B

A
B
A
B
A
B
B
A
A
B
A
A

B
A
B
A
B
A
A
B
B
A
B
B

A
B
A
B
A
B
B
A
A
B
B
A

A
B
A
B
A
B
B
A
A
B
A
A

So my question are whether these designs make sense at all, whether the first or second approach would be better (and possibly whether I'm overthinking the whole thing).
Comment: First, look carefully at the nature of the row and column effects. In my experience with such plates the problems are typically in the outermost wells, the ones most susceptible to desiccation in an incubator. Some 96-well plates allow for introducing sterile water into the spaces among the wells, essentially solving that problem by maintaining high humidity throughout. It's better to solve the problem mechanically rather than statistically, if that's possible.
Comment: @GeorgeSavva just read your comments after posting an "answer" and wanted to add this - if you only see the effect on columns then it's worth investigating wether the effect is based on the distance from the center (1st and 12th columns are similar) or if it's a linear trend (1st and 12th columns are most different). If the latter, I would suspect that either the plate is not placed horizontally during the procedure and there is a tilt, or maybe it is operated near a heating source or a window or something that creates a temperature trend. In those cases it can be fixed on the wet-lab side.
Comment: Thanks. I agree it would be good to reduce variation and I've discussed this with the team. In this case it concerns pipetting with 12 channel pipette and potentially uneven heating so not sure what can be done. My prior data from one plate suggests an edge effect on one end column only, a second plate showed a right to left gradient.
Comment: Do you have any evidence of differences among rows? What you described seems to be only differences among columns. With too-complicated a design there's a big chance, with these plates, of making errors in reagent or sample distribution.
Comment: No direct evidence of differences among rows, but on talking with the technical people they told me that based on their understanding of the experiment if there was any differences it would be between the rows rather than the columns as I saw in the data.  Hence my trying to hedge for both.
Answer: I have some experience with a problem like this, but not sure if I can provide a correct "answer". Rather I will outline some thoughts, observations, and comments with the hope that they will be helpful for you with figuring out what to do next.
Row and Column effects
I've noticed these two with some of the biological data I've worked with. Primarily I saw row effects while the column effects were weaker. But the general pattern was a gradient which started from the middle of the plate and formed a "wave" pattern towards the edges. Imagine it as a drop falling into water at the middle of the plate creating a round ripple across the plate. So in that regard the first and last rows were more similar to each other compared to, say, the first and middle rows. And the same was true for columns.
At that time, after discussing with "wet-lab" team, our understanding of the cause was a temperature gradient. Even thou each well had a separate controlled heater, we imagined that the temperature was somewhat higher near the middle, because of the proximity to other wells, wheres the edges had fewer neighbours and cooled down. But we were not 100% convinced on this interpretation.
Anyways, effect was there.
The size of the effect
Another thing we noticed is that the magnitude of this effect was not drastic and was quite low. For example - we would detect it when looking specifically for it and we would also see it on first principal components in cases where the samples put on the plate were very very similar to each other. However, if we had things like samples from very different donors (different sexes, different ages, etc) - these things had a lot bigger effect sizes and the gradient effect of the plate was dwarfed by them.
Our way of dealing with the problem
After noticing this effect we concluded that the main problem comes from the outer edges - first and last rows, first and last columns. Thus in the future experiments we did the following:

We used as many different plates as we had technical replicates (in your case - 4). So one plate would only contain biological replicates (in your case - 24)
We left the outers of the plate empty and only used innermost wells.
The position of each biological sample on the plate was randomised and this randomisation was performed separately for each set of technical replicates (each plate).

This way we avoided the biggest effects, and the rest were randomised. And on top of that we had as many sets of randomisations as there were technical replicates.
Another way of dealing with the problem
In your case I assume you will mainly be looking at differences between treatments. And, if I understood correctly, those differences are paired, i.e. biological sample 1 will have treatments A and B, biological sample 2 will also have treatments A and B, etc, same for all the biological samples. If this is correct you can try to place the pairs as close to each other as possible.
This way, when comparing treatments you would do a paired comparison (taking the differences of treatment A minus treatment B, for each sample separately, and testing if the mean of those differences is equal to zero). Thus, if sample 1 treatment A is on well position A1, and sample 1 treatment B is on well position B1, then after taking the difference the column effect is subtracted and should disappear.
You will have 4 replicates, so just repeat the same for other replicates across different places of the plate, but always make sure that the samples that will be subtracted from one another are neighbours.

Just ideas from someone who is a little familiar with the problem, do not take it as an authoritative answer.

Experimental design for a 96 well plate


SQL query conflict between "Thai_CI_AS" and "SQL_Latin1_General_CP1_CI_AS"

Question: Hi may i know what wrong this this query
SELECT [no]
--  into #mytemp
  FROM [pcb_comp]
  where no in (SELECT [no] FROM [Data_Query].[dbo].[query]
  where [no] NOT IN (select no from [IPTS2].[dbo].[abc]))
  and location = 'PCB'
  order by no

Comment: What is the problem exactly?
Comment: the purpose for this query is search from pcb_comp, match with query table, if not found serach from abc table
Comment: You can force collations on certain columns and manually resolve the conflict, q.v. here: http://stackoverflow.com/questions/20544392/how-to-get-rid-of-collation-conflict-in-a-sql-server-query
Comment: if i write same like above will get the error for "Thai_CI_AS" and "SQL_Latin1_General_CP1_CI_AS"
Comment: You should post your schema and tell us what the collations are for each table/column in your query.
Comment: The problem is that columns are being compared but they are in different collations and so they can't be compared. For example there are characters in thai that have no similar latin character and vice versa
Answer: if it is problem in collation conflict with Location column than use below:
SELECT [no]
--  into #mytemp
  FROM [pcb_comp]
  where no in (SELECT [no] FROM [Data_Query].[dbo].[query]
  where [no] NOT IN (select no from [IPTS2].[dbo].[abc]))
  and location COLLATE DATABASE_DEFAULT = 'PCB' COLLATE DATABASE_DEFAULT
  order by no

same applies to your 'in' filter if above does not work.


Flutter: call function in background or just kill the background process

Question: I've been looking everywhere about how to call a function in background (from main isolated process / foreground), but there's no result so far.
This is roughly the process I'm talking about:
import 'package:audioplayers/audioplayers.dart';

Future<void> main() async {
    WidgetsFlutterBinding.ensureInitialized();

    await Firebase.initializeApp();
    FirebaseMessaging.onBackgroundMessage(myBackgroundMessageHandler);

    runApp(MyApp());
}

Future<void> myBackgroundMessageHandler(RemoteMessage message) async {
    flutterLocalNotificationsPlugin.show(
        0,
        "Title",
        "Body",
        NotificationDetails(
          android: AndroidNotificationDetails(
            "channelid",
            "none",
            "none",
            priority: Priority.high,
          ),
        )
    );
    audioPlayer = await audioPlayerCache.loop("ring.mp3", isNotification: true);
}

void stopRing() async {
    await audioPlayer.stop();
}

When the audioPlayer is called in background, calling stopRing() function from foreground will NOT affect anything because - I think - they are in different isolated process.
Here is the flow: app receive firebase notification (in background) -> show notification (using flutter_local_notifications plugin) and play sound -> user tap the notification -> wakeup the app -> call stopRing() (not working, the app is already in foreground).
So, my question is, how to call stopRing() in background from foreground process, or probably just simply terminate the background process, I guess that will stop the ring, too.
Thanks in advance...
(Note: everything else is working well, except the problem mentioned in this post)
Answer: I was also implementing the exact feature you mentioned, and facing the same challenge. I managed to solve it using IsolateNameServer to communicate with isolated process.

In your background process, setup listener to stop the audio player as below:
import 'dart:isolate';
import 'dart:ui';

Future<void> myBackgroundMessageHandler(RemoteMessage message) async {
    AudioPlayer audioPlayer = new AudioPlayer();

    flutterLocalNotificationsPlugin.show(
        0,
        "Title",
        "Body",
        NotificationDetails(
          android: AndroidNotificationDetails(
            "channelid",
            "none",
            "none",
            priority: Priority.high,
          ),
        )
    );

    final SendPort? sendStopBeep = IsolateNameServer.lookupPortByName('stop_beep_send_port');
    if (sendStopBeep == null) {
        ReceivePort portStopBeep = ReceivePort();
        IsolateNameServer.registerPortWithName(portStopBeep.sendPort, 'stop_beep_send_port');

        portStopBeep.listen((dynamic data) {
            // You should stop the audio player here
            await audioPlayer.stop();
            IsolateNameServer.removePortNameMapping('stop_beep_send_port');
        });

        // You should start the audio player here
        audioPlayer = await audioPlayerCache.loop("ring.mp3", isNotification: true);
    }
}

When your app state resumes (or wakes your app), trigger the stop audio player event to the above port listener:
class _HomeState extends State<Home> with WidgetsBindingObserver {
    @override
    void initState() {
        super.initState();
        WidgetsBinding.instance.addObserver(this);
    }

    @override
    void dispose() {
        WidgetsBinding.instance.removeObserver(this);
        super.dispose();
    }

    @override
    Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
        if (state == AppLifecycleState.resumed) {
            final SendPort? send = IsolateNameServer.lookupPortByName('stop_beep_send_port');
            if (send != null) {
                send.send(['test', 1]); // You can send any object over, ['test', 1] is just an example
            }
        }
    }
}

Hope this helps!


rsyslog upload limitation?

Question: I use rsyslog to upload logs to my Graylog server
/etc/rsyslog.conf:
$InputFileName /var/log/apache-error.log
$InputFileFacility apache
$InputFileTag #apacheError:
$InputFileStateFile apache-error
$InputFileSeverity error
$InputRunFileMonitor

if $programname == '#apacheAccess' then @18.104.22.168:1514

Some long messages are truncated in Graylog (the end of the message is unavailable).
When I tried to upload a long message with netcat in UDP:
echo -n -e "$(for i in $(seq 0 64000); do echo -n '0123456789';done)" | nc -u 126.96.36.199 1514
The message is in Graylog (in multiple messages but all the message is available)
Why a part of a message from rsyslog is truncated, and a message from netcat not ?
Is a maximumSize parameter have to be increased or something ?
I tried
$MaxMessageSize 256k
$MainMsgQueueDequeueBatchSize 256

but without success
Thx
Answer: rsyslogd is using the syslog protocol, which is defined in RFC 3164. In section 4.1, it says

The total length of the packet MUST be 1024 bytes or less.

Therefore, rsyslog does the right thing in splitting the message up into smaller packets, in order to comply with the standard for syslog messages.
The reason why ncat sends the message as one huge blob is that ncat doesn't know anything about the protocol for the type of message you're sending, so it's not aware that it ought to be splitting the message up.

rsyslog upload limitation?


Formatar percentagem em python

Question: Como devo formatar a percentagem para este exemplo?
import pandas as pd
flights = pd.read_csv('data/flights_until_june.csv', sep=',')
sum_null = flights.isnull().sum()
sum_null.sort_values(ascending=False, inplace=True)
total = flights.shape[0]
percentage = round(sum_null/total * 100, 2)
print(percentage)
print('{:2%}'.format(percentage))

Comment: Olá Ricardo, bem-vindo à comunidade da Stack Overflow em Português! Acredito que a formatação que deseja é mostrar a porcentagem com duas casas decimais com o sinal de '%' ao final?
Comment: Sim é isso mesmo
Answer: Basta usar o modificador .xf para definir quantas casas decimais você quiser, nesse caso basta substituir a letra x por um número inteiro. Observe que ao fazer isso ele não irá truncar o número, mas sim arrendondar ele. Já o símbolo de porcento tem que ser colocado após o fecha chaves } ao final do "place holder" do número.
Exemplo:
>>> print('{0:.2f}%'.format(34.56634))
34.57%

Note que em '{0:.2f}%' o 0 pode ser omitido ficando somente '{:.2f}%'
Answer: >>> y = print('{0:.2f}'.format(34.56634), '%')
34.57 %

a expressão 2f significa dois valores flutuante (f). Então vai aparecer dois números após a virgula.
O ponto que aparece ao lado do .2 significa que após a virgula haverá apenas dois valores (você pode aumentar esse valor para 3f e vai aparecer 34.576
A expressão que esta dentro das chaves { } e tem que ficar dentro de uma string.
A expressão .format tem que vir após a string e mostrando que você quer formatar o número que esta dentro do parênteses. toda essa informação é separada por uma virgula. Ou seja a virgula separa uma informação da informação seguinte. O resultado de saída vai ser um valor numérico e você pode atribuir a esse valor uma unidade com uma nova virgula ou seja você vai atribuir uma nova informação que é a porcentagem mas observe que ela é uma string longo tem que vir entre suas aspas simples (ou poderia ser dupla. Isso é muito usado em cálculos quando o aluno quer mostrar na saída que o resultado é 2,24 m, ou a temperatura é 75 K.

É um código muito comum nas engenharias.
Comment: Isso está péssimo, e se um engenheiro usar esse valor de `y` numa obra o prédio cai. A função [`print()`](https://docs.python.org/pt-br/3/library/functions.html#print) não retorna valor. O mesmo resultado pode ser obtido com fstring: `print(f'{34.56634:.2f}%')`

Formatar percentagem em python


Can't mount a NFS share on File server rule running in Windows Failover Cluster

Question: On Microsoft Windows Failover Cluster (Windows Server 2019 Datacenter) I've created a File Server role and created a NFS share (similar to how it's described in these instructions). The idea was to mount this share from a virtual machine running Linux (Ubuntu)
The problem is that Linux (Ubuntu) times-out when trying to mount this share:
admin@ubuntu-server:/mnt$ sudo mount -o nolock -v -t nfs 192.168.1.16:/nfs /mnt/nfs/
[sudo] password for admin:
mount.nfs: timeout set for Fri Jul  1 08:22:43 2022
mount.nfs: trying text-based options 'nolock,vers=4.2,addr=192.168.1.16,clientaddr=192.168.1.74'
mount.nfs: mount(2): Connection timed out
mount.nfs: Connection timed out

Further inspection revealed that this File Server does not listen on port 2049/tcp:
admin@ubuntu-server:/mnt$ sudo nmap 192.168.1.16
Starting Nmap 7.80 ( https://nmap.org ) at 2022-07-01 07:12 UTC
Nmap scan report for 192.168.1.16
Host is up (0.00037s latency).
Not shown: 995 filtered ports
PORT     STATE SERVICE
111/tcp  open  rpcbind
135/tcp  open  msrpc
445/tcp  open  microsoft-ds
2179/tcp open  vmrdp
3389/tcp open  ms-wbt-server
MAC Address: A0:36:9F:34:39:26 (Intel Corporate)

Nmap done: 1 IP address (1 host up) scanned in 4.91 seconds

The next step was to try to verify if Linux can in fact mount and NFS-type share created on Windows system. So on the same Windows Server I created folder on local disk and shared it by activating NTF sharing option (in folder properties).
Linux (Ubuntu) was able to mount this share without problems:
admin@ubuntu-server:/mnt$ sudo mount -o nolock -v -t nfs 192.168.1.11:/test2 /mnt/test2/
mount.nfs: timeout set for Fri Jul  1 07:11:58 2022
mount.nfs: trying text-based options 'nolock,vers=4.2,addr=192.168.1.11,clientaddr=192.168.1.74'
mount.nfs: mount(2): Protocol not supported
mount.nfs: trying text-based options 'nolock,vers=4.1,addr=192.168.1.11,clientaddr=192.168.1.74'

admin@ubuntu-server:/mnt$ ll test2/
total 5
drwxrwxrwx 2 nobody 4294967294   64 Jun 15 13:02 ./
drwxr-xr-x 3 root   root       4096 Jul  1 07:08 ../
-rwxrwxrwx 1 nobody 4294967294   11 Jun 15 13:03 README.txt*

So I checked the server's IP for open ports which revealed port 2049/tcp to be open:
admin@ubuntu-server:/mnt$ sudo nmap 192.168.1.11
Starting Nmap 7.80 ( https://nmap.org ) at 2022-07-01 07:12 UTC
Nmap scan report for windows-server.our-domain (192.168.1.11)
Host is up (0.00047s latency).
Not shown: 993 filtered ports
PORT     STATE SERVICE
111/tcp  open  rpcbind
135/tcp  open  msrpc
139/tcp  open  netbios-ssn
445/tcp  open  microsoft-ds
2049/tcp open  nfs             <---------- PORT IS OPEN
2179/tcp open  vmrdp
3389/tcp open  ms-wbt-server
MAC Address: A0:36:9F:34:39:26 (Intel Corporate)

Nmap done: 1 IP address (1 host up) scanned in 4.42 seconds

I've compared how the two shared are configured and the configuration is identical apart the following:

one is running in failover cluster while the other is running directly on windows
one is using local folder while the other is using a disk from the cluster storage
the clustered File server has it's own IP address, while the NFS share done in Windows is accessible via OS's IP address

Now I'm pretty much out of options. Can anyone help?
Here if further info on my system:

Failover cluster is running on Windows Server 2019 Datacenter
cluster uses a Fiber Channel-connected storage
we're using Ubuntu Server 20.04
Answer: We have managed to solve this problem by using CIFS/SMB instead of NFS:

in Windows cluster we configured the File Server role to use Samba (instead of NFS)
in our Ubuntu machine we mounted the used CIFS to mount the volume

P.S.
The goal of this exercise was to migrate away from using Ubuntu inside a virtual machine as NFS server for our Docker Swarm containers and replace it with a Windows Cluster's File Server role. This would eliminate the need of running a whole virtual machine for this role and will make the system administration much easier.


How can I view & access all available variables for a Drupal Commerce cart?

Question: For theming purposes I'm trying to access any and all available Drupal commerce cart variables. For starters, I want to give the user feedback regarding the cart in the global nav.
I thought these vars would be available within the $_SESSION var but not so(?).
an example of what i'm trying to do is as follows, via some pseudo code to be put into the global nav…
<!—- Global nav—->

Cart
    <?php  if($items_in_cart == true) {
        echo '*';
     } ?>

or,....
<?php
if($items_in_cart) {
print $total_items_in_cart;
}?>

Answer: You can use the function commerce_cart_order_load & commerce_line_item_load() to load the current order(cart) object & order product objects respectively:
  global $user;
  $order = commerce_cart_order_load($user->uid);
  $cart_items = array();
  foreach ($order->commerce_line_items[LANGUAGE_NONE] as $line_item) {
    $cart_items[] = commerce_line_item_load($line_item['line_item_id']);
  }

In this example, $order contains the Commerce order(cart) object for the current user & $cart_items contain an array of line item (order products) objects.
Comment: great, thank you. can you recommend, where would i put such code. so that vars are available for theming purposes? would you put in template->html.tpl.php or page.tpl.php ?
Comment: guess i'm wrong in thinking that if i add to html.tpl.php the vars would be available later in block--myNav.tpl.php.
Comment: That's another question but, you can place a `MYTHEME_preprocess_page(&$variables)`,  `MYTHEME_preprocess_html(&$variables)`, or `MYTHEME_preprocess_block(&$variables)` function (depending on your needed scope) in your theme's **template.php** file with MYTHEME replaced with your theme's short name. Any array element you add to `$variables` in these functions will be available to *page.tpl.php*, *html.tpl.php* or *block.tpl.php* (e.g. adding `$variables['example'] = "test';` to the page preprocess function will make it available as `$example` inside *page.tpl.php*).


Issue with conditionally ranking p-values

Question: I am conducting some time series modelling and want to remove terms from my time series based on removing the lowest rank p-values. The criteria I have is to only rank p-values that exclude "ar", "ma", "intercept", "price.diff" and rank only if p>0.2.
Here is an example:
term         pval   rank
ar1          0.001  NA
ar2          0.292  NA
ar3          0.000  NA
ma1          0.000  NA
intercept    0.000  NA
Price.Diff   0.859  NA
School       0.818  2
Easter       0.149  NA
Christmas    0.049  NA
High.Week    0.000  NA
Low.Week     0.000  NA

This is the function I have written:
rank_p<-function(x) {
  x["rank"]<-NA
  x$rank<-ifelse(test = substr(x$term,1,2) != "ar" &
  substr(x$term,1,2) != "ma" &
  substr(x$term,1,stop = nchar(x$term)) != "intercept" &
  substr(x$term,1, stop = nchar(x$term)) != "Price.Diff" &
  x$pval > 0.2,
  yes = rank(-x$pval, na.last = NA),
  no = NA)
 return(x)
  }

My issue is that the rank for this example begins at 2.This would be the second highest p-value however because I am excluding price.diff, this should be ranked 1.
Is the issue in ordering of the conditions?
Comment: Your code may benefit from use of the `%in%` operator. To read about it, look at the `?match` help page.
Comment: Thanks for the reply. I see that the %in% will retur a vector of TRUE/FALSE, will i still not have the ranking issue though, unless I i base the ranking on TRUE values only?
Comment: You can use logical vectors to subset, removing the p-values that you don't want to consider in your ranking. See `?"["` if this sounds interesting.
Comment: If the 2 was instead a 1, would that be a desired output? Or would Easter and Christmas also have a ranking?
Comment: Hi Croote, correct, 2 should be a 1. Eastern and Christmas should not have a ranking as their p-value is <0.2
Comment: Great - I think I understand what it is you desire. let me know if my answer meets the requirement.
Answer: The reason your ranking is not as desired is because your yes term includes the whole column. So you need to subset the column based on your condition.
What is happening in your current function is that you are ranking the column without a filter then changing to NA everything that doesn't fit the condition.
I have just added a slightly changed version of your code to do this.
All I changed was to define your condition then subset the data frame accordingly. EDIT: this will work on the displayed data but not if there are more than one rows which meet the condition.
rank_p <- function(x) {
  x["rank"] <- NA
  cond <- {substr(df$term,1,2) != "ar" &
      substr(df$term,1,2) != "ma" &
      substr(df$term,1,stop = nchar(df$term)) != "intercept" &
      substr(df$term,1, stop = nchar(df$term)) != "Price.Diff" &
      df$pval > 0.2}

  x$rank <- ifelse(test = cond,
                   yes = rank(-x[cond, ]$pval, na.last = NA),
                   no = NA)
  return(x)
}

What this does is only rank the rows that meet your condition( x[cond, ] ).
This can be further simplified because you have already set them to NA just change the ones that meet the condition. It doesn't seem like you need an ifelse at all! This will also assign a vector of ranking to a subset of the same length which should work for larger sets which contain more than 1 row which meets the condition.
rank1_p <- function(x) {
  x["rank"] <- NA
  cond <- {substr(df$term,1,2) != "ar" &
      substr(df$term,1,2) != "ma" &
      substr(df$term,1,stop = nchar(df$term)) != "intercept" &
      substr(df$term,1, stop = nchar(df$term)) != "Price.Diff" &
      df$pval > 0.2}
  x[cond, "rank"] <- rank(-x[cond,]$pval, na.last = NA)

  return(x)
}

Comment: Thankyou Croote. That works perfectly. I have accepted your answer. Could you just clarify that rank(-x[cond,$pval) is indexing the rows of x in column pval and that whether the meet the cond criteria. Many thanks
Comment: I see. Thinking on that question has shown me that there is another issue with the first solution and your `ifelse()` logic. It will not behave as desired because If there are multiple rows which meet the condition it will attempt to place a vector of ranks into a single row and simply take the first.
The second solution is better and will fill the rows accordingly.
Comment: `rank` function in your case is not indexing the rows to check if they meet the condition. It is simply trying to assign the output of `rank` to the yes condition in the `ifelse`


Consultar Dados Com Filtro VBA

Question: Galera, boa noite
Estou com um problema em VBA, podem me ajudar, por favor. Segue:
Tenho um excel com a Sheet1 e a Sheet2. Na Sheet2 tenho um lista de clientes onde tenho os seguintes dados: Nome, Cidade e Idade.

Em VBA, preciso copiar os dados da Sheet2 e colar na Sheet1 com base em alguns filtros, por exemplo onde a Cidade seja igual a "São Paulo".
Exemplo do resultado seria, na Sheet1, copiar os dados onde a cidade seja igual a São Paulo, então teria João e Manuel.
Alguns pontos que podem ser um empecilho:
1 - Eu não sei a quantidade de linhas do cadastro da Sheet2. Por exmplo, conforme imagem acima, o cadastro tem 4 registros, porém poderia tem 200. Nesse caso eu não sei previamente a quantidade de linhas que tem.
2 - No cadastro, pode ser que entre dois registros possua linhas em branco.Por exemplo, entre o registro do Manoel e do Carlos, tem uma linha em branco.
Obrigado.
Comment: Sugiro que coloque uma imagem de exemplo, para facilitar o entendimento de como seus dados estão dispostos na planilha..
Comment: Não entendi muito bem.. você gostaria de criar uma macro onde vc passe um filtro e ela copie os dados da plan2 pra a plan1?
Comment: Editei a pergunta colocando uma imagem de como estaria os dados. No caso eu não sei quantos registros tem na Sheet2 e também pode contem linhas em branco entre os registros.
Answer: Consegui aqui galera.
Descobrindo a ultima linha preenchida, com o comando abaixo, independente das linhas em branco e da quantidade de linha deu certo:
UltimaLinha = PlanilhaOrigem.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

E para o filtro utilizei os comandos abaixo:
PlanilhaOrigem.Sheets("Sheet1").Range(LocalOrigem).Select
Selection.AutoFilter
Selection.AutoFilter Field:=3, Criteria1:="<300"
Range(LocalOrigem).CurrentRegion.Copy

Answer: Para agregar à questão, como outros podem ter o mesmo problema. Eu abordaria de forma diferente. Primeiro iria para a segunda aba e faria um loop independente do número de linhas e da presença de linhas em branco, como já foi respondido pelo autor, mas sem usar o filtro.
Sub copyFromSheet2()

Dim lngUltimaLinha As Long
Dim i As Integer
Dim strNomeCidade As String

lngUltimaLinha = ActiveWorkbook.Sheets("Sheet2").Range("B1048576").End(xlUp).Row

'Aqui vai o nome da cidade que deseja, pode ser por InputBox também
strNomeCidade = "c1"

'Loop para salvar as linhas na outra planilha, no caso, Sheet1
'Se a planilha Sheet1 estiver em branco, a primeira linha vai ficar em branco
For i = 1 To lngUltimaLinha
    If Cells(i, 2).Value = strNomeCidade Then
    Cells(i, 2).EntireRow.Copy ActiveWorkbook.Sheets("Sheet1").Range("A1048576").End(xlUp).Offset(1, 0)
    End If
Next i

End Sub

Consultar Dados Com Filtro VBA


Find natural number $x=523...$ ($6$ digits) such that :$\begin{cases}x\equiv 0\pmod{7}\\x\equiv 0\pmod{8}\\x\equiv 0\pmod{9}\end{cases}$

Question: Problem :
Find natural number  $x=523...$ ($6$ digits) such that :

$$\begin{cases}x\equiv 0\pmod{7}\\x\equiv 0\pmod{8}\\x\equiv 0\pmod{9}\end{cases}$$

This number is $x=523152,,523656$
But I don't know how I find it ??
From congruence $x\equiv 0\pmod{504}$
But how I complete ?
Answer: Your solution $x$ satisfies $x=523000+y$, with $0\le y<1000$.
As $523000\equiv 352\pmod{504}$, you should be able to finish.
Answer: $\begin{align}\text{Purely}\, {\textit mentally }\ \bmod 504\!:\ \ 0&\,\equiv\, x + \overbrace{\color{#c00}{523}(2\cdot \color{#0a0}{500})}^{\textstyle 523000}\\ &\,\equiv\, x\,+\, \color{#c00}{19}(2)(\color{#0a0}{-4})\\ &\,\equiv\, x\,-152\\[.1em]
\iff  &\ \ \ \ \ \bbox[5px,border:1px solid #c00]{x\equiv 152}\end{align}$
Comment: mentally is the only way to do math, doing it otherwise is calculation, or computation.
Comment: @Roddy "Mentally" emphasizes that it can be computed without the assistance of a  calculator.  The approach above uses only $10$ seconds of mental arithmetic, and it illustrates useful techniques for such.
Comment: so can division of 78 digit numbers, by primes which have 10 of order less than 78.
Answer: Let $N=523abc=504000+19000+abc$
$19000=352 mod 504$
$abc+352= 504 k$
$k=1$ ⇒ $abc=504-352=152$
$k=2$ ⇒ $abc=1008-352=656$
$k=3$ ⇒ $abc=1512-352=1106$
So abc=152 and 656 are solutions


Ways to synchronize voice with beat?

Question: For example, on the song Drip or Drown by Gunna, I noticed that every last word on of the intro ("I got woodgrain on my Damier Buckle, Cool quarter mill in my Goyard duffle, ...") aligns with the clap on the beat.
When I try to do a similar effect on my end, I always end up under the impression that I talk too slowly (even thought the voice fits in the pattern), besides that I also have a hard time connecting sentences together.
Is there a formula or general consensus for how to flow  synchronize voice with a beat?
Comment: Other than the obvious "practice, practice, practice,"  it's really not clear what you're after.  Get someone to be a teacher, or at least an observer, to see whether you are in sync or not.
Comment: I understand what you mean about the "practice, practice, practice,"! Thought I find that I lack direction in that regard! I like your idea of getting an observer to check if I am in time, but would there be an alternative where I do not depend on other people?
Answer: It's not a formula, but there are ways to make sure a vocal line fits well with a rhythm or beat.
Here's a map of a typical beat/rhythm, showing where the musical stresses fall:

ONE and Two and THREE and Four and

Usually the first beat of every measure is the strongest, then the third beat is second strongest, and then the second and fourth beats are less strong than the third beat, and the off beats ("ands") are the weakest.
Now let's do the same thing with the same vocal line you posted:

I got wood-grain on my Damier Buckle, Cool quarter mill in my Goyard duffle

I've never heard the song before, so I don't know how it's set to music, but I know right away how I would set it to music:
<code>    1       &       2       &       3      &       4       &
  I got    wood     grain   in my   Dam-   ier     Buck-   le
    1       &       2       &       3      &       4       &
    Cool   quarter  mill    in my   Go-    yard    duff-   le
</code>
There's more than one way to do it, but the important part is that generally, the stressed syllables in the lyrics should line up with the strongest beats in the music. Usually, if your lyrics sound "off" in some way when you set them to music, the first place to look is at the stresses in the lyrics and how they line up, or don't, with the stresses in the beat.
Sometimes it's hard to know what syllables are stressed in the lyrics. The trick for figuring it out is to forget about the music completely and just say the lyrics like you're just talking to someone. Say it at least three times, each time trying to let go of the music and say it as casually as possible. Starting on the third time, listen carefully to how you're saying it. You should be able to hear and feel which syllables you are stressing.
Answer: A perfect take is always best, and you'll approach that with lots of work put into practicing.
That said, a lot of contemporary recordings line up every little noise to the grid. Most DAWs (recording software) have a feature that does this. It involves setting markers in a recording and then quantizing or manually moving them where you want them to go. Whether the artist you mentioned applied this technique, I have no idea, but the track is extremely mechanical and repetitive, so I wouldn't be the least bit surprised.
The technique can be applied to achieve impossible perfection in a recording, but, if you're set up to make recordings, you could also use it for practice. Record several takes of a passage you think needs work and you'll see where your rhythm is off. You can then quantize your performance and practice along with it until you successfully attain the rhythmic accuracy you're looking for.

Ways to synchronize voice with beat?


Pass a complex Object to Angular Directvie

Question: i'm trying to pass a complex object to my directive (and that is a different formFontrol so i can't use [formControl], formControlName etx...)
<input matInput appDateInputEvents [control]="fieldGroup.get('date')">

that is my directive
@Directive({
  selector: '[appDateEventsInput]',
  standalone: true,

})
export class DateInputEventsDirective {

  constructor() { }

  @Input() control!: AbstractControl;

  //MORE STUFF

  }

but angular dosen't reconize [control] as a know property
instead if i use just control witout square brackets angular reconize it as property but just as String
so what i shoud do?
Comment: Your directive's selector is "appDate**Events**Input", but in your `` element you are using "appDateInput**Events**". Both are different. Are you sure your directive works?
Answer: You could maybe try to pass a getter to your directive to ensure the template is correctly interpreting what you're trying to do.
in your ts file
public get dateFormControl(): FormControl {
       return this.fieldGroup.get('date');
}

in the template
<input matInput appDateInputEvents [control]="dateFormControl">

Answer: And remember: if you're using Modules, add in "declarations" the DateInputEventsDirective , if you're using standalone components add in "imports" the DateInputEventsDirective.
BTW, if you use
@Input('appDateEventsInput') control!:AbstractControl

And you can write
  <input matInput [appDateEventsInput]="fieldGroup.get('date')">

NOTE:Another thing you can do, as you aplied your directive in a input that mannage (or not) a FormControl (or a [(ngModel)]) you can get it inject in constructor the NgControl
control!:AbstractControl
constructor(@Optional() ngControl:NgControl)
{
   if (ngControl)
      this.control=ngControl.control;
}


Recursion and List Mutation

Question: I am restrict myself to mutate a list using recursion only, I have a few small troubles when doing so.
def expand_strings(L,s,i):
    if len(L) == 0:
        return
    else:
        if len(L[0]) - 1 <= i:
            L[0] += s
        elif len(L[0]) - 1 > i:
            new_string = L[0][:i] + s + L[0][i:]
            L[0] = new_string
            expand_strings(L[1:], s, i)
            return

L: The input list containing possible 1 or more strings
s: The extra portion of string that I need to "insert" or "append" to the string elements within the list
i: The index of string where I want to insert or append s to.
The main goal of this function is the following:
1. if the index i within the range 0 ~ len(string_element_in_list), then I insert my s starting from index i

2. if the index i is larger than what the current string length, then I do the append s.

The problems I am having right now is that: I notice the recursion will only mutate the first element within the input list, and every element after the first element, they won't be affected by the mutation, I figure it might have something to do with the new input list I pass to the recursion, but I don't know precisely why this doesn't work.
Thanks for helping in advance. :)
Answer: The problem is in the recursive call expand_strings(L[1:], s, i). When you use slicing to get a part of your list, python creates a whole new copy of that sublist. So the recursive call creates a copy of your list, except the first element, and works on that copy.
One way of solving this problem can be returning the modified list from your method:
def expand_strings(L,s,i):
    if len(L) == 0:
        return []
    else:
        if len(L[0]) - 1 <= i:
            L[0] += s
        elif len(L[0]) - 1 > i:
            new_string = L[0][:i] + s + L[0][i:]
            L[0] = new_string
        return [L[0]] + expand_strings(L[1:], s, i)

If you don't want to create a copy of the sublist every time (and return the modified list), you can add one more parameter to your method that would specify the location of the first element to modify. The base case would be where the starting index is equal to the length of the list.
def expand_strings(L,s,i,start):
    if start == len(L):
        return
    if len(L[start]) - 1 <= i:
        L[start] += s
    else:
        L[start] = L[start][:i] + s + L[start][i:]
    expand_strings(L, s, i, start + 1)

Comment: Also, @DigitalSoul, if you want to perform this operation on every string of your list, you'll need to put the recursive call out of the else block.
Comment: Oh I see I see, thanks for helping. Just for the curiosity, what if this function must return None? If this is the case, is there anyway I can still do this using recursion?
Comment: In that case you'll have to operate on the same list. See updated answer.

Recursion and List Mutation


Participation of different teams with the same owner in European cups

Question: Giampaolo Pozzo is owner of Udinese Calcio in Italy and Granada CF in Spain.
What would happen if both teams were to qualify for the same European competition (for example, Europa League) ?
In the past, has there ever been a situation like this?
Answer: Apparently it is not allowed.
I can't find anything at the moment in official UEFA documentation, but in the past week a few mentions were made of it in the Belgian press.
Vincent Tan owns three clubs in Europe: Cardiff City, FK Sarajevo and KV Kortrijk.
There was a chance that FK Sarajevo and KV Kortrijk teams would qualify for the Europa League, but is no longer the case. Anyway, it states explicitly in the article (in Dutch, can't find anything in English) it is not allowed for clubs with the same owner to play in the same UEFA competition. However an investigation could be necessary to prove the link between clubs and owner(s).
The rule on deciding who gets the spot would be the team with the highest ranking in the national division, if tied UEFA Club ranking would be taken into account.
Answer: The issue you've highlighted is addressed in the Article 5. Integrity of the Competition of the UEFA Champions League and UEFA Europa League Regulations, 5.01:

5.01. To ensure the integrity of the UEFA club competitions, the following criteria apply:

no club participating in a UEFA club competition may, either directly or indirectly:

hold or deal in the securities or shares of any other club participating in a UEFA club competition,
be a member of any other club participating in a UEFA club competition,
be involved in any capacity whatsoever in the management, administration and/or sporting performance of any other club
  participating in a UEFA club competition, or
have any power whatsoever in the management, administration and/or sporting performance of any other club participating in a UEFA club
  competition;

[...]

If two or more such clubs were to qualify to CL or EL, only one of them would be admitted according to 5.02:

5.02. If two or more clubs fail to meet the criteria aimed at ensuring the integrity of the competition, only one of them may be admitted to
  a UEFA club competition, in accordance with the following criteria
  (applicable in descending order):

the club which qualifies on sporting merit for the more prestigious UEFA club competition (i.e., in descending order: UEFA Champions
  League and UEFA Europa League);
the club which was best-ranked in the domestic championship giving access to the relevant UEFA club competition;
the club whose association has the highest association coefficient ranking, drawn up in accordance with Annex D.

Regarding your last question:

In the past, has there ever been a situation like this?

At the end of 2016/17 season German Bundesliga runners-up RB Leipzig and Austrian Bundesliga champions RB Salzburg (both de-facto owned by Red Bull conglomerate) qualified to 2017/18 Champions League, but the UEFA Club Financial Control Body (CFCB) has decided to accept the admission of both clubs to the tournament, stating that:

Article 5 (Integrity of the competition) of the competition
  regulations is not breached.
Following a thorough investigation, and further to several important
  governance and structural changes made by the clubs (regarding
  corporate matters, financing, personnel, sponsorship arrangements,
  etc.), the CFCB deemed that no individual or legal entity had anymore
  a decisive influence over more than one club participating in a UEFA
  club competition.
The CFCB will continue to monitor both clubs to ensure that integrity
  rules are respected going forward.

Here you can find the full decision of the aforementioned case.
Answer: AFAIK there is no rule against such situation yet, as explained by a journalistic article I read (I think it's on Guardian but currently I can't find exactly where). Maybe there will be future rules preventing conflict of interest, but not now.
Actually, Pozzo also owns Watford in England in addition to these two teams, and he already operated a lot of player transfers between the clubs to work around financial fair play restrictions. I don't think he was in any way punished or is in the danger of being punished because of those operations.
Comment: Watford is "owned" by his son Gino, so it is more difficult to create rules in this particular case


GAS, Update User infos : AdminSDK Directory Service

Question: I am very new to GAS. I am trying to understand how AdminDirectory Service works updating a user infos.
https://developers.google.com/admin-sdk/directory/v1/reference/users
Here is my code, what's wrong??
function updateUserInfos (){
  var opt = phones{
   type : "work",
   value : "999999",
   primary : true
  };
  var updates = AdminDirectory.Users.update(opt, "email@example.com")
}

Thanks a lot
Comment: A description of the error you're getting would be useful, and confirmation that you've definitely turned on the access to the Admin directory as per: https://developers.google.com/apps-script/guides/services/advanced ?
Comment: No error, simply no changes anymore. Access are turned on... Confirmed
Answer: Try
function updateUserInfos (){
  var opt = {"phones":[{
     "type" : "work",
     "value" : "999999",
     primary : true
    }]
  };
  var updates = AdminDirectory.Users.update(opt, "firstname.lastname@example.com")
}

if multiple phones
var opt = {"phones":[{"type" : "work",
     "value" : "999999",
     primary : true}],
    [{"type" : "mobile",
     "value" : "88888" }]
  };

Stéphane
Comment: You could said what you changed and why.
Comment: Sorry I replied quickly because I had to leave. Just changed the structure of opt var. it was not a valid json format. Just compare the 2 lines.
Comment: Thank you St3ph ... it works! I was just confused about sintax.


Check whether connection to the database is possible or not?

Question: I need to connect to the sql server database from my c# code.
sometimes because of some error( i dont know what it is) i cannot open connection
that is
conn.open() throws exception
so i need to check whether i can connect to the database before i am opening connection.
Thanks for helping me.
Comment: @ZMRBINU You "test" whether the connection can establish or not by *trying to make the connection*.
Comment: I think the best idea is still to find out why you get the error on the conn.open. Any other way of solving this problem is just sticking your head in the sand.
Comment: You need to test the connection by attempting to Open a connection!
Comment: ok. thats fine.. But the problem is that my server will catch if any errors are thrown. Also the team lead says we know that here may happen exception. Then why dont you can test whether the connection can establish or not?
Answer: The best way to test if you can make a connection is to try to make a connection. Catch any exceptions that are thrown, and use them to display an error or try to determine the cause. (most connection errors aren't going to be something your program can fix on the fly).
<code>try{
    conn.open();
    ...//other stuff
}catch(Exception ex){
    //handle exception
}
</code>
Comment: You don't throw an exception, you catch an exception.
Comment: but i dont want to throw any exception. Is there any way other than this?
Comment: @ZMRBINU Any way you would try to "test" a connection that fails will probably end up throwing an exception. If you built up a raw `Socket` and tried to connect with something inaccessible, for instance, the same thing would happen. The error you got back would just be harder for you to deal with.


Use `tf.contrib.predictor` to predict on batches from `tf.estimator.export_savedmodel` for TF 1.13

Question: I found several examples to load my saved estimator, my_estimator.export_savedmodel(export_dir, export_input_fn) model, as a predictor like so, predictor = tf.contrib.predictor.from_saved_model(export_dir). This works great when my tf.train.Example has only one item. How can I make it work for a batch for TF 1.13?
model_input= tf.train.Example(features=tf.train.Features(feature={
      'browser_name': tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"chrome", b"ie"])),
      'version': tf.train.Feature(float_list=tf.train.FloatList(value=[8.0, 11.0]))
  })).SerializeToString()

predictor({"inputs":[model_input]})

call fails when there are multiple inputs per feature.
Answer: Here's a working example with tensorflow 1.13.1 :
import tensorflow as tf
import pandas as pd
import numpy as np

prod_export_dir = 'my_model_dir'
data =  pd.read_csv('my_data.csv')

predictor = tf.contrib.predictor.from_saved_model(prod_export_dir)

model_input = {}
for k, v in predictor.feed_tensors.items():
    model_input[k] = np.array(data[k].tolist(), dtype=v.dtype.as_numpy_dtype)

prediction = predictor(model_input)

Comment: Thank you! Is there an equivalent for 2.x? I only find examples which require serializing each example to a string and pass to a `tf.saved_model.load(MODEL_PATH).signatures["predict"]` instance
Answer: predictor({"inputs":[model_input1, model_input2]})

works but that requires enumerating the data manually into multiple tf.train.Example instances


Webgrid: Maintain Filter after Sorting

Question: I have set up a webgrid and it seems to work fine, allowing me to sort and page. I have added a filter option which also works well however if I filter and then sort the results, the filter is lost and all records are displayed.
Here is my Razor view code:
@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "Get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "myGrid" }))
{

@Html.ValidationSummary(true)
<fieldset>
    <legend>Document Search</legend>

        @Html.Label("Enter a Document code:")

        @Html.Editor("search")

        <input type="submit" value="Search" />

</fieldset>
}

@{
    WebGrid grid = new WebGrid(null, rowsPerPage: 10, canPage: true, canSort: true, ajaxUpdateContainerId: "myGrid");
    grid.Bind(Model, autoSortAndPage: true);
}

    @grid.GetHtml(mode: WebGridPagerModes.All, firstText: "First Page", nextText: "Next", previousText: "Previous", lastText: "Last Page", numericLinksCount: 10,
            columns: grid.Columns(
                grid.Column("DocumentID", "Document Code", canSort: true),
                grid.Column("Title", "Document Title", canSort: true)
        )
    )

And here is my action:
public ActionResult Index(string search)
    {
        List<DocumentIndexViewModel> viewModel = Mapper.Map<List<DocumentIndexViewModel>>(DocumentService.GetDocumentsBySearch(search));
        if (Request.IsAjaxRequest())
            return PartialView("_IndexGrid", viewModel);
        else
            return View(viewModel);
    }

How do I maintain the filter when I sort the displayed records? It seems like I need to append the search string onto the sort links somehow but am not sure how to proceed.
Answer: Since you are using GET for the filter this should preserve it. I am unable to reproduce the problem. Here's my full working test case.
Model:
public class CityViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Controller:
public class HomeController : Controller
{
    public ActionResult Index(string search)
    {
        using (var client = new WebClient())
        {
            var query = HttpUtility.ParseQueryString(string.Empty);
            query["q"] = search;
            var json = client.DownloadString("http://gd.geobytes.com/AutoCompleteCity?" + query.ToString());
            var serializer = new JavaScriptSerializer();
            var viewModel = serializer
                .Deserialize<string[]>(json)
                .Select((x, index) => new CityViewModel
                {
                    Id = index,
                    Name = x
                })
                .Where(x => x.Name.StartsWith(search ?? string.Empty, StringComparison.OrdinalIgnoreCase))
                .ToList();

            if (Request.IsAjaxRequest())
            {
                return PartialView("_IndexGrid", viewModel);
            }
            else
            {
                return View(viewModel);
            }
        }
    }
}

Main view (~/Views/Home/Index.cshtml):
@model IEnumerable<CityViewModel>

@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "GET", InsertionMode = InsertionMode.Replace, UpdateTargetId = "gridPartial" }))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Document Search</legend>

            @Html.Label("Enter a Document code:")

            @Html.Editor("search")

            <input type="submit" value="Search" />

    </fieldset>
}

    @Html.Partial("_IndexGrid")

~/Views/Home/_IndexGrid.cshtml partial:
@model IEnumerable<CityViewModel>

@{
    WebGrid grid = new WebGrid(null, rowsPerPage: 10, canPage: true, canSort: true, ajaxUpdateContainerId: "myGrid");
    grid.Bind(Model, autoSortAndPage: true);
}

    @grid.GetHtml(mode: WebGridPagerModes.All, firstText: "First Page", nextText: "Next", previousText: "Previous", lastText: "Last Page", numericLinksCount: 10,
        columns: grid.Columns(
            grid.Column("Id", "City Id", canSort: true),
            grid.Column("Name", "City Name", canSort: true)
        )
    )

Sorting and pagination preserves the search filter that was entered because it was in the query string.
Comment: Are you saying the sorting header links in the grid have the search string in the href? Mine just seem to have a '#'.
Comment: Don't look at your browser address bar. Look at what's actually sent to the server. Analyze the exact request being sent with AJAX. It contains the search query string parameter. So the filter is not stored in the href of the links at all. Those links have `onclick` javascript events associated to them which trigger an AJAX request sending the search criteria properly to the server.
Comment: In that case then, do I need to just pull the value out of the request in the controller action?
Comment: Strange, it now appears to be working, passing in the search string to the action when I sort the webgrid. However when I follow a link to another page and then use the browser back button, it loses the filter. Infact worse than this, IE displays an older version of the webpage which was modified hours ago! So how do I preserve the filter when navigating back?
Comment: That's the browse back button problem. There's not much you could do about it. You have no control over this button. As a possible workaround provide a link on the other page so that the user could navigate back by passing the filter as query string parameter.

Webgrid: Maintain Filter after Sorting


How to add subquery in views to remove particular type of rows?

Question: I have applied join to users table with node_revision table like below:
function custom_overrides_views_data_alter(&$data) {

  $data['users']['node_revision'] = array(
    'title' => t('Users revisions authored'),
    'help' => t('Relate a user to their revisions'),
    'relationship' => array(
      'base' => 'node_revision',
      'base field' => 'uid',
      'relationship field' => 'uid',
      'handler' => 'views_handler_relationship',
      'label' => t('revisions bubbla'),
    ),
  );
}

And by creating view now I got number of revisions per user like below:

Westerberg (15752)
Eriksson (4305)
Jakob (4182)
Andersson (4153)

But now I want to remove those revisions which are created when user created node first time ie: will only show edits but not the revisions which are created first time when new node was created. To solve this logic is to remove nodes having below true:
excluding the "first time created" nodes (original node revision)
node.created = node_revision.timestamp

I tried using query alter but it uses quotes around field please suggest how to remove that ?
if ($view->name == "my_view") {

    $sub_query = db_select('node', 'n')
      ->fields('n', array('created'))
      ->condition('n.created', 'node_revision_users.timestamp');

    $view->query->where[1]['conditions'][] = array(
      'field' => 'node_revision_users.timestamp',
      'value' => $sub_query,
      'operator' => 'NOT IN'
    );

  }

Output problem is like screenshot:

Please suggest! thanks
Answer: You should use where() to match table columns instead of condition
if ($view->name == "my_view") {
    $sub_query = db_select('node', 'n')
      ->fields('n', array('created'))
      ->where("n.created = node_revision_users.timestamp");

      $view->query->where[1]['conditions'][] = array(
        'field' => 'node_revision_users.timestamp',
        'value' => $sub_query,
        'operator' => 'NOT IN'
      );
}

Update
Why where() not condition()
Syntax for condition:
$query->condition($field, $value = NULL, $operator = '=')

shows condition() is for building standard condition fragement
Syntax for where:
$query->where($snippet, $args = array())

shows where() is for building any arbitrary SQL as conditional fragment.
$snippet may contain any legal SQL fragment.


What is tritone substitution?

Question: What is tritone substitution? How can this device be applied to improvisation and composition?
Comment: Related question: http://music.stackexchange.com/questions/17784/why-is-bii-7b5-considered-a-dominant/17785
Comment: Possible duplicate of [Why is bII 7(b5) considered a dominant?](https://music.stackexchange.com/questions/17784/why-is-bii-7b5-considered-a-dominant)
Answer: Tritone substitution is as it says. The substitution of one chord for another, that is a tritone away from the one being substituted. Thus a V7-I ( G7 - C ) becomes Db7 - C. Because the Db is a tritone, or 3 tones away from the G. Exactly half way, as it happens.
G7 is spelled G,B,D and F. Db7 is Db,F,Ab and Cb. The two common notes of F and B (Cb), being a tritone apart in themselves, swap places, the F (b7) of the G chord becomes the maj. 3 of the Db, while the b7 of Db (B/ Cb) becomes the maj. 3 of G.
Thus the chords swap over. Resolution is often from one note in a chord to a note one semitone away in the resolving chord. Going from the Db to C notes, and the Ab to G notes bring a resolution (as actually,the F and the Cb do as well)- all notes being one semitone from the target.
As far as composition and improv are concerned, tritone sub. can be used in a perfect cadence situation, swapping a V7 - I for a bII7 - I. It introduces non-diatonic notes into the harmony, whilst one can use diatonic notes (or the appropriate non-diatonic) in the melody. It also works with an interrupted cadence. Using ,say, C -E7 -Am as an example, t.s. the E7 for a Bb7 to get to Am.
In a jazz situation, often only two notes are played for a 'chord', so in the first example, B and F could be played to get to Cmaj. That alone isn't a t.s., but the bass for instance, could play a Db. Then it is.
Answer: Tritone substitution is the substitution of one chord (almost always a dominant 7th of some sort) for one with a root a tritone away. For instance, you could substitute Db7 for G7 because Db is a tritone away from G. This works because the important notes in the chord that determine what it leads to (the third and seventh) are the same in both chords - in this case, they would both be B and F. Tritone substitution is used to make changes sound more interesting.
Comment: Generally speaking, a dominant seventh, otherwise it won't work the same.
Answer: As the others have already given good explanations, here's a beautiful fingerpicking tune which gives a good impression of how this sounds in practice: (not really "jazz" though)

This is a true tritone in chord changes. Very rarely seen.
I wish it would work to auto-skip the tune to about 2:50 where the chord change (G#-D) happens. With YT links it works, but with SE's embedding code, it does not work at all.

What is tritone substitution?


Let $R$ be a ring such that for all $a,b$ in $R$, $(a^2-a)b=b(a^2-a)$. Then $R$ is commutative

Question:
Let $R$ be a ring such that for all $a,b$ in $R$, $(a^2-a)b=b(a^2-a)$. Then $R$ is commutative.

I am having difficulties making the connection. Any help would be appreciated.
Comment: Why are we suddenly getting all these questions about conditions under which rings are commutative? That's at least the third one in the last few hours.
Answer: For any $x,y$ in $R$, the relation $(x^2-x)y=y(x^2-x)$ implies that
$$xy-yx=x^2y-yx^2.$$
Now take $a,b$ in $R$ and consider the previous relation for $x=b-a$ and $y=b$ :
$$(b-a)b-b(b-a)=(b-a)^2b-b(b-a)^2~~~~(\ast)$$
Expanding the left side gives $(b-a)b-b(b-a)=ba-ab$.
On the other hand the right side gives
\begin{align}
(b-a)^2b-b(b-a)^2&=(b^2-ab-ba+a^2)b-b(b^2-ab-ba+a^2)\\
&= b^2a-ab^2+a^2b-ba^2\\
&= ba-ab+ab-ba\\
&= 0
\end{align}
Thus $(\ast)$ gives $ba-ab=0$ and $R$ is commutative.
Comment: Very nice. +1${}{}{}{}{}$


Divergence theroem

Question: I have a problem applying the divergence theorem in two problems.
The first asks me to calculate $\int \int F · N dS$ where $F(x,y,z)=(x^2 + \sin z, x y + \cos z, e^y)$ in  in the cylinder $x^2 + y^2=4$  limited by the planes $XY$ and $x+z=6$.
I compute divergence and $\mathop{div}{F} = 3 x$.
To compute $\int \int \int \mathop{div}{F} \ dV $, I use cylindrical coordinates:
$x= r \cos( \alpha)$
$y=r \sin (\alpha)$
$z=t$
for $r \in [0,2]$, $\alpha \in [0, 2 \pi]$ and $t \in [0, 6- r \cos(\alpha)]$.
As the Jacobian is $r$ I have the following:
$$ I=\int \int F · N dS$=  $\int \int \int \mathop{div}{F}  \space dV =  \int_{0}^{2 \pi} \int_{0}^{2} \int_{0}^{6- r \cos(\alpha)} 3 r \cos( \alpha) r \ dt \ dr \ d\alpha $$
I compute it using Mathematica and I have I=0 however I know that the result is not that. Where have I been wrong?
The other problem is similar.
I have to compute I have to calculate the flow F that passes through S where $F(x,y,z)=(x^2 + \sin(yz), y- x e^{-z},z^2)$ and $S$ is the boundery of cylinder $x^2+y^2=4$ limited by planes $z=0$ and $x+z=2$. I have applied the divergence theorem as in the previous case and I have to
$$ I=\int \int F · N dS= \int_{0}^{2 \pi} \int_{0}^ {2} \int _{0}^{2-r \cos(\alpha)} (2 r \cos (\alpha) + 1 + 2z )r\ dz\ dr\ d\alpha=\frac{64 \pi}{3} $$
And as in the previous case, it is not the solution either. Where this error?
Thank you very much to all.
Comment: The divergence is $3x$.  Where does that appear in your integrand?
Answer: Your coordinates should be
\begin{align*}
    x &= r \cos \alpha \\
    y &= \color{red}{r \sin \alpha} \\
    z &= t
\end{align*}
You have a different expression for $y$.  I don't think it matters in the integration, however, as a coincidence.
The volume integral is
$$
    \iiint_E 3x\,dV = \int_0^{2\pi}\int_0^2 \int_0^{6-r\cos\alpha}3(r\cos \alpha) \color{red}{r}\,dt\,dr\,d\alpha
$$
And that should make a big difference.
Comment: The y = $\alpha$ has been a failure to write it in the forum, however the error was that, indeed, I have forgotten the Jacobian. He had been thinking about the problem all afternoon
Answer: I am not following your conversion to polar coordinates.
$\iiint 3x \ dz\ dy\ dx$
$x = r\cos \theta\\
y = r\sin \theta\\
z = z$
Jacobian:
$dx\ dy\ dz = r\ dz\ dr\ d\theta$
$\int_0^{2\pi}\int_0^2\int_0^{6-r\cos\theta} 3r^2\cos\theta \ dz\ dr\ d\theta$
Comment: The $y = \alpha$ has been a failure to write it in the forum.
Comment: We still have the problem that some how you have $2r^2\cos a$ and not $3r^2\cos a$
Comment: It has been another failure to write in the forum. I am sorry. I am going to edit it in case someone consults this thread.

Divergence theroem


Conditions under which diagonalizability of the induced map implies diagonalizability of L

Question: Let $V$ be a finite dimensional vector space over $\mathbb{C}$ and $L: V \rightarrow V$ be a linear operator. Suppose that $U$ is a subspace of $V$ such that $U \subset \ker L$ and $\text{im}(L) \cap U = \{0 \}$. Show that if the induced linear map $\overline{L} : V/U \rightarrow V/U$ is diagonalizable, then $L$ is diagonalizable. Find a counterexample to the statement in case $\text{im}(L) \cap U \neq \{0 \}$.
This is from a qualifying exam I took an hour ago. So I am more interested in the critique of my argument although I will also appreciate a correct alternative solution.
Here is how I argued:
Note that $L$ is diagonalizable if and only if $V$ has a basis of eigenvectors of $L$. Since $\overline{L}$ is diagonalizable, $V/U$ has a basis of eigenvectors of $\overline{L}$, say $\mathfrak{B} = \{v_{1} + U, ..., v_{k} + U \}$. Since $\mathfrak{B}$ is a basis, $\{v_{1},..., v_{k}\}$ is linearly independent. We extend it to a basis of $V$, say $\mathfrak{C} = \{v_{1},..., v_{k}, v_{k+1},..., v_{n}\}$.
Since $\mathfrak{B}$ is a basis, $v_{j} + U = U$ for all $j > k$ so that $v_{j} \in U \implies v_{j} \in \ker L$. So that $Lv_{j} = 0$ for all $j >k$. Also, since $v_{j} \notin U$ for all $j = 1, ..., k$, $v_{j} \notin \ker L$. We have $\lambda_{j}(v_{j} + U) = \overline{L}(v_{j} +U) = L(v_{j}) + U \implies \lambda_{j}v_{j} - L(v_{j}) \in U$ and since $\lambda_{j}v_{j} \in \text{im}(L)$, $\lambda_{j}v_{j}- Lv_{j} \in U \cap \text{im}(L) = \{0 \} \implies Lv_{j} = \lambda_{j}v_{j}$. Hence, $\mathfrak{C}$, is a basis of $V$ consisting of eigenvectors of $V$.
I could not come up with a counterexample after playing with a few two by two matrices and worrying about moving on to other problems I gave up, but my idea was to take $U = \ker L$ and find $L$ such that $\text{rank} L = \text{rank} L^{2}$.
Comment: Presumably, you mean $V/U$ rather than $V\setminus U$
Comment: Yes, of course.
Comment: Also, I don't know what the induced map would mean if $U \not\subset \ker L$.  Do you mean a counterexample in which $im(L) \cap U \neq \{0\}$?
Comment: Thank you so much. I corrected it.
Answer: Your argument is on the right track.   Note, however, that it is not the case that $v \notin U \implies v \notin \ker L$.  You already know that $v_j$ is an eigenvector for $j\leq k$, so it's moot.
As for a counterexample: take
$$
L = \pmatrix{0&1\\0&0}
$$
and confirm that with $U = \ker L$, we have $\bar L = 0$.  Up to similarity, this is the only $2 \times 2$ example.


Using Invisimail within a Contemplate Template

Question: I'm in Drupal 7 and using Contemplate and Invisimail, but I can't figure out how to get my emails to be obfuscated.
I think it is because the display of the node is using the contemplate template, which simply says:
<?php print $node->field_email['und'][0]['email'] ?>

I figure I have to wrap that in a call to invisimail_encode_email, as such:
<?php print invisimail_encode_email($node->field_email['und'][0]['email'], 'html_entities') ?>

but that did not work (error message about undefined array index). So, maybe I'm calling that function with the wrong parameters? or maybe I'm calling the wrong function.
Basically, how do I call invisimal from within php?
Comment: Ok, I used invisimail_encode_html($node...) instead and that encoded the email, but I'm still not sure if _encode_html actually does the full obfuscation thing.
Comment: you could check with a `drupal_set_message(invisimail_encode_email(the_email))` or if you have [devel](http://drupal.org/project/devel) installed just put the function inside a `dsm()`. it will put it as a string.
Answer: I'm doing a similar thing, but in a node template. To get the javascript obscured code to return, use the js_entities encoder similar to this:
<?php
$email = render($content['field_email_address']);

// this takes the email address and turns it into a "mailto:" link, otherwise use 'link' => 0
$email_settings = array('link' => 1);

echo "
" . invisimail_encoder_js_entities_encode($email, $email_settings) . "
";
?>

Also, I use the Quick Javascript Switcher extension in Chrome to quickly toggle js off/on to see if things like this are working:
https://chrome.google.com/webstore/detail/quick-javascript-switcher/geddoclleiomckbhadiaipdggiiccfje


Why doesn't this print to my printer?

Question: I need to print a programmatically generated String to my printer. However, whenever I run this code, the printer doesn't print out anything.
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class PrintMain {
    public static void main(String[] args) {
        PrintService[] ps = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.AUTOSENSE,null);
        String[] psNames = new String[ps.length];
        for(int i = 0;i < ps.length;i++) {
            psNames[i] = ps[i].getName();
        }
        String choiceString = (String)JOptionPane.showInputDialog(new JFrame(),"What printer do you want to use?","Printer",JOptionPane.PLAIN_MESSAGE,null,psNames,null);
        PrintService choice = null;
        for(int i = 0;i < psNames.length;i++) {
            if (choiceString.equals(psNames[i])) {
                choice = ps[i];
            }
        }
        if(choice!=null) {
            DocPrintJob job = choice.createPrintJob();
            String text = "This is a test string. A method call would usually generate text for this, but TL;DR.";
            InputStream stream = new  = ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
            Doc doc = new SimpleDoc(stream,DocFlavor.INPUT_STREAM.AUTOSENSE,null);
            try {
                PrintJobWatcher watch = new PrintJobWatcher();
                job.addPrintJobListener(watch);
                PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
                pras.add(new Copies(1));
                job.print(doc,pras);
                watch.waitForDone();
            } catch(PrintException e1) {
                JOptionPane.showMessageDialog(new JFrame(),"A PrintException has occurred. Message: "+e1.getMessage());
            } finally {
                try {
                    stream.close();
                } catch(IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}

And this is my PrintJobWatcher class:
import javax.print.event.PrintJobEvent;
import javax.print.event.PrintJobListener;

public class PrintJobWatcher implements PrintJobListener {
    public boolean dataTransComplete=false;
    @Override public void printDataTransferCompleted(PrintJobEvent arg0) {
        dataTransComplete=true;
    }
    @Override public void printJobCanceled(PrintJobEvent arg0) {}
    @Override public void printJobCompleted(PrintJobEvent arg0) {}
    @Override public void printJobFailed(PrintJobEvent arg0) {}
    @Override public void printJobNoMoreEvents(PrintJobEvent arg0) {}
    @Override public void printJobRequiresAttention(PrintJobEvent arg0) {}
    public void waitForDone() {
        System.out.println("Waiting...");
        try {
            while(!dataTransComplete) {
                wait();
            }
        } catch(InterruptedException e) {}
        System.out.println("Done!");
    }
}

I can't figure out why the String doesn't print out. What am I doing wrong?
Answer: I've found the solution. Use this to print stuff:
JTextArea toPrint = new JTextArea("Text here.");
try {
    boolean done = toPrint.print();
    JOptionPane.showMessageDialog(new JFrame(),"Printing "+(done?"completed":"canceled"));
} catch(PrinterException ex) {
    JOptionPane.showMessageDialog(new JFrame(),"The printer messed up. " +ex.getMessage());
}

It even opens up a little dialog for printer settings.

Why doesn't this print to my printer?