IIS 7 ARR v3.0 URL Rewrite Different Domain

Question: I need to take requests to firstdomain.com/example/foo/bar and serve up content from seconddomain.com/example/foo/bar but I want the end user to still see firstdomain.com/example/foo/bar in their browser. That rules out a 301/302 redirect.  I thought URL-rewrite would do the trick, but that has to be same domain.  I think ARR is what I need.  So, I've installed ARR v3.0 and rebooted my server.  Now that ARR v3.0 is installed, how do I go about configuring it to perform the desired request routing?
I should note seconddomain.com is a 3rd party hosted server, whereas firstdomain.com is hosted on a server on our network.
Answer: <code><?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <urlCompression doStaticCompression="false" doDynamicCompression="false" dynamicCompressionBeforeCache="false" />
        <rewrite>
            <rules>
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://www.seconddomain.com/{R:1}" />
                </rule>
            </rules>
            <outboundRules>
                <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
                    <match filterByTags="A, Form, Img" pattern="^http(s)?://www.seconddomain.com/(.*)" />
                    <action type="Rewrite" value="http{R:1}://www.firstdomain.com/{R:2}" />
                </rule>
                <preConditions>
                    <preCondition name="ResponseIsHtml1">
                        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                    </preCondition>
                </preConditions>
            </outboundRules>
        </rewrite>
    </system.webServer>
</configuration>
</code>


How to automatically copy up the value of selected option of dropdown list into a text. Using jquery

Question:
Possible Duplicate:
Form input value based on option value selected

I have a dropdown list. I want that whenever I select a value from the dropdown, the selected value is automatically copied into a text box in the same form. I would like to make use of jQuery and not simply other, plain Javascript methods.
The code for the form is:
<s:form action="okadddqs" method="post" cssClass="text">
    <s:select label="Correct Option :"
        id="opc"
        name="opc"
        list="#@java.util.LinkedHashMap@{'One':'One', 'Two':'Two','Three':'Three','Four':'Four','Five':'Five'}"/>
    <s:textfield label="Choosed Option" id="cname" name="cname"/>
    <s:submit value="Submit now"/>

I wnat cname to be filled automatically with the selected value of opc. How do I implement this?
Comment: I have edited the question with my code. Can someone help me out now. I am waiting..
Comment: possible duplicate of [Form input value based on option value selected](http://stackoverflow.com/q/7802235/). Note that jQuery *is* Javascript. "Plz give teh codez" type questions are generally frowned upon, as is impatience. Both denote an attitude of entitlement.
Comment: Can some one help me on this problem as well..? thanks.. http://stackoverflow.com/questions/11641662/doccookie-is-not-getting-saved-instantly
Answer: I am not for sure on the names or other selectors of your elements, so here is a generic means of achieving what you are looking for:
$('select').change(function () {
  var el = $(this);

  el.closest('form').find('input[type=text]').val( el.val() );

});

Comment: I have edited the question with the code of form in jsp page.
I am very very new to jquery. So can u please post the exact full code inside the  tag. Actually i am not getting what what this code is doing so m not able to modify according to my needs. SO please post the whole script code. Thanks.
Comment: Can u please post the modified code according to the ids and selectors as per the form asks. I have edited the question with the code of he form. Please do this. coz i am new to jquery. and i am not able to modify the code. waiting for the help...
Answer: $(document).ready(function(){
    $("#opc").change(function(){
        $("#cname").val($(this).find("option:selected").text());
    });

});

DEMO


Usuário digita 15 números aleatórios, programa retorna a média, os números maiores que a média e os menores que a média!

Question: num = []
soma = 0
media = 0
for i in range(1, 16):
    num = int(input('Digite um número: '))
    soma += num
    media = soma / 15
    if num[i] > media:
        print('Maires que a media {}'.format(num[i]))
    if num[i] < media:
        print('Menores que a media {}'.format(num[i]))
print('Soma {} e Media {}'.format(soma, media))

Answer: Problema
Olá Uberlan, pelo que entendi você gostaria de um script simples para calcular alguns valores referentes aos números digitados aleatoriamente.
Sendo mostrado: a soma, média, valores abaixo da média, valores acima da média.
Da maneira que está fazendo, não é funcional pois a verificação dos números acima/abaixo da média está sendo verificado na inserção dos valores, sendo assim sem saber qual é necessariamente a média, você primeiro tem que incluir os valores para poder fazer as verificações desejadas.
Solução
Como resolução, fiz o seguinte código que atende o que está querendo fazer.
# criando o array para a inclusão dos 15 números aleatórios
numbers = []

# array para separaçnao dos números maiores e menores que a média
above_average_numbers = []
below_average_numbers = []

# laço de repetição para executar o código identado 15 vezes
for i in range(15):
    numbers.append(int(input('Digite o {}º número: '.format(i+1))))

# realizando a soma dos números digitados
sum_numbers = sum(numbers)

# calculando a média
average = sum_numbers / len(numbers)

# laco de repetição para facilitar a manipulação dos números abaixo da média, e acima da média
for number in numbers:
    if number >= average:
        above_average_numbers.append(number)
    else:

        below_average_numbers.append(number)

# retornos
print('\n* A soma dos valores do vetor {} totaliza o valor de {}!\n* A média dos valores é: {}\n* Temos {} números abaixo da média: {}\n* E temos {} números acima da média: {}'.format(numbers, sum_numbers, average, len(below_average_numbers), below_average_numbers, len(above_average_numbers), above_average_numbers))

Algumas informações do código: fiz a criação de 3 vetores para armazenar os valores:

numbers para receber todos os valores que forem digitados, incluindo utilizando o método append() - que inclui o elemento no final da estrutura.
above_average_numbers para receber os valores acima ou iguais a média
below_average_numbers para os valores abaixo da média

No laço de repetição de inclusão:
for i in range(15):
    numbers.append(int(input('Digite o {}º número: '.format(i+1))))

Fui adicionando os valores solicitados no array de maneira padronizada para integer inteiro, porém, poderá alterar caso tenha necessidade, e também mostrei a ordem dos números pelo i+1 pois na primeira iteração o i possui o valor de 0.

sum_numbers = sum(numbers) está realizando a soma de todos os valores do array. Pois sum() é uma função nativa do Python, então, porque não utiliza-la?
average = sum_numbers / len(numbers) calcula a média com os valores obtidos pela somaque realizamos anteriormente, junto com o tamanho do vetor que incluímos todos os valores. len() é uma função que retorna para nós o tamanho do vetor.

Realizamos posteriormente outro laço de repetição para separar os valores:
for number in numbers:
    if number >= average:
        above_average_numbers.append(number)
    else:

        below_average_numbers.append(number)

Acredito que esta parte está facil de compreender, utilizamos a variável local numberpara obter o valor respectivo de numbers - isso é um padrão de programação apresentado para boas práticas do python (PEP8) e o number é verificado se é maior ou menor que a média, incluindo assim no respectivo vetor.
Após isso, mostramos todos os nossos resultados de uma maneira clara, apesar do print() ter ficado um pouco grande com várias informações.
print('\n* A soma dos valores do vetor {} totaliza o valor de {}!\n* A média dos valores é: {}\n* Temos {} números abaixo da média: {}\n* E temos {} números acima da média: {}'.format(numbers, sum_numbers, average, len(below_average_numbers), below_average_numbers, len(above_average_numbers), above_average_numbers))

Espero ter ajudado.
Comment: Cara ajudou muito, tenho que fazer uma lista de 16 exercícios, e tenho que aprender python praticamente sozinho! Vlw mesmo!
Comment: Que bom @UberlanTorres, qualquer dúvida que tenha sobre este mesmo fluxo, basta realizar um comentário nesta resposta que no momento que ver a notificação venho te auxiliar. Caso esta resposta resolveu o problema ou foi útil para encontrar a solução, poderá aceita-la clicando no v(zinho) que está ao lado da pergunta. Para que outras pessoas com o mesmo problema sejam ajudadas por uma resposta validada.


Best approach to design this MySQL big database

Question: I'm planing to design a database that contains only 2 tables (stats1 and stats2), but, this tables will contain rows going to 40 000 000 records!
All the records will be "positive/unsigned" integers.
MySQL client version: 5.0.91
Here's some info concerning both tables:
stats1
total current records = 10 000 000
contains 3 columns that stores fixed numerals as follow:

colA = 12345678901
colB = 1234
colC = 12345678

stats2
total current records = 5 000 000
contains 3 columns that stores fixed numerals as follow:

colA = 1234567890
colB = 12345678901234
colC = 12345678

I know a little bit about MySQL, PHPmyadmin but need your advice concerning making this efficient.
Comment: Efficiency will depend on having the right indexes, which depends on what queries you want to do.
Comment: 40 million records? I Do not do mySql - but on commercial SQL server that is not really something large. On Sql Server I would consider it non-trivial, but not something BIG. THat would start around 1 billion rows.
Comment: @TomTom: SQL Server is much more robust. But I agree with you, even in MySQL 120 million integers is not enormous. "That would start around 1 billion rows.": IMHO, it is less about the number of rows, and more about the size of the whole data. A thousand of records containing each one 100 MB blobs *is* huge.
Comment: thanx for the answers guys.
Answer: MySQL can handle that size without serious problems (we store well over 100 million).
What is the growth rate of the data? 1000 inserts a minute/hour/day? Will the growth be constant or will records be added in bulk (i.e. 100000 records in a batch)?
Your usage is the key. If you are doing arbitrary queries with aggregate functions (sum, etc.) performance will suffer.
If the data is relatively static and your performing analytic type functions on it, I would look at a column oriented database engine. Something like InfoBright or InfiniDB. Both of these work with MySQL clients. If you now how to use MySQL, you can use these. But, they may not work for you depending on how you want to access and query your data.
Comment: +1 for InfoBright and InfiniDB.
Comment: Thank you guys for your support

At the moment I will just read from the database; will not perform "complex" queries.

For the moment i'll stick with row oriented databse engine (I'm not advanced on database world, so i'll keep it simple riht now!)

thanx ;-)
Answer: First, 4×10⁷ columns with three integers in each is not toooo big. I suppose even Sqlite or less robust databases can handle this number of records.
Now, if you ask about efficiency, everything depends on the usage of the database. All you said is how is stored the data, but more important is how is it used. Consider the following situations:

The numbers are sent from a satellite and stored in database for further analysis,
The numbers are collected from an experiment and you must check they are unique before storing them,
The numbers are used for statistics and retrieved one by one, from a single machine,
The numbers are accessed randomly by hundreds of machines in parallel.

Every case requires a different approach.


NFS directory not working

Question: On the client server the NFS mount on /oracle/r12/inst/apps/database1 works as expected, but /oracle/r12/apps does not. For example if I touch test in /oracle/r12/inst/apps/database1 the file test appears on both the client and the server, this is not the case with /oracle/r12/apps.
NFS Server /etc/exports
[root@nfs-server apps]# cat /etc/exports
/oracle/ x.x.x.x(rw,fsid=0,sync,nohide,no_subtree_check,no_wdelay,no_root_squash)
/oracle/r12/ x.x.x.x(rw,sync,no_wdelay,nohide,no_subtree_check,no_root_squash)
/oracle/r12/apps/ x.x.x.x(rw,sync,no_wdelay,nohide,no_subtree_check,no_root_squash)
/oracle/r12/inst/apps/ x.x.x.x(rw,sync,no_wdelay,nohide,no_subtree_check,no_root_squash)

NFS Client /etc/fstab
[root@nfs-client ~]# cat /etc/fstab
/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/VolGroup00/LogVol01 swap                    swap    defaults        0 0
x.x.x.x:/oracle/r12/inst/apps/database1 /oracle/r12/inst/apps/database1 nfs defaults 1 2
x.x.x.x:/oracle/r12/apps /oracle/r12/apps nfs defaults 1 2

NFS Client output of df -h
[root@erl-ora-apps1 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                      274G   16G  244G   7% /
/dev/sda1              99M   61M   34M  65% /boot
tmpfs                 7.9G     0  7.9G   0% /dev/shm
x.x.x.x:/oracle/r12/apps
                      3.1T  2.5T  441G  86% /oracle/r12/apps
x.x.x.x:/oracle/r12/inst/apps/database1
                      3.1T  2.5T  441G  86% /oracle/r12/inst/apps/database1

Comment: Why are you exporting /oracle and /oracle/r12 if the first level you're mounting remotely is /oracle/r12/apps ?
Comment: Don't think they need to be removed, it's just unusual to see that in my experience. What error are you getting when you try to touch a file in /oracle/r12/apps on the client?
Comment: Are the times on the server and client synced via NTP? Also: indirectly related at most, but change the "1 2" and the end of the NFS mounts in fstab to be "0 0".
Comment: Start up NTP on the server and client, and your problems just might disappear.
Comment: Does mounting as (vfstype) nfs4 make a difference?
Comment: @에이바 have you issued  `showmount -a x.x.x.x` on your nfs server where you are exporting the shares? Check the directory permissions of `apps` see if there is any difference. Also add only these options `x.x.x.x/24(rw)`  See for the netmask. I assume it has a `255.255.255.0` mask.
Comment: I don't know, /etc/exports was already like that. Think those need to be removed? They're all pointing to the same IP address anyway.
Comment: There is no error but it does not update to the server and vice versa.
Comment: Nice catch on the 0 0 and no they're not using NTP. Since you mentioned it though, I checked the date and noticed a slight difference in time.
Comment: I turned on NTP and syncronized the time as well as changed nfs to nfs4 in fstab. No change.
Comment: I commented out all of the lines but `/oracle/r12/ x.x.x.x(rw,sync,no_wdelay,nohide,no_subtree_check,no_root_squash)` in exports and /oracle/r12/inst/apps/database1 still works but /oracle/r12/apps does not.
Comment: I changed the IP to have /24 and removed all options but rw. Still the same behavior except now I cannot create any files on the NFS drive from the client. showmount -a only shows /oracle/r12/inst/apps/database1
Answer: Thank you for all of the comments everyone.
Here is what I did to fix the issue.
Started ntp service on both servers and synchronized the time.
Changed /etc/fstab on the NFS client to

x.x.x.x:/oracle/r12/inst/apps/database1
  /oracle/r12/inst/apps/database1 nfs defaults 0 0
  x.x.x.x:/oracle/r12/apps /oracle/r12/apps nfs defaults 0 0

Changed /etc/exports on NFS server to
/oracle/r12/ x.x.x.x/24(rw,sync,no_wdelay,nohide,no_subtree_check,no_root_squash)
/oracle/r12/apps/ x.x.x.x/24(rw,sync,no_wdelay,nohide,no_subtree_check,no_root_squash)

Ran exportfs -av on NFS Server
Manually unmounted drives on the NFS client and issued
mount -a to re-read /etc/fstab and mount file systems.
Now it's working properly.

NFS directory not working


Create S/MIME certificates

Question: I am trying to generate S/MIME certificate using the solution provided here:
https://serverfault.com/questions/103263/can-i-create-my-own-s-mime-certificate-for-email-encryption
Solution:
openssl genrsa -des3 -out smime.key 4096
openssl req -new -key smime.key -out smime.csr
openssl x509 -req -days 365 -in smime.csr -CA ca.crt -CAkey ca.key -set_serial 1 -out smime.crt -setalias "Self Signed SMIME" -addtrust emailProtection -addreject clientAuth -addreject serverAuth -trustout
openssl pkcs12 -export -in smime.crt -inkey smime.key -out smime.p12

I want to provide credentials for signing mime data too. How can I possibly do that?
Any help would be really appreciated.
Answer: You have already created the credentials for signing MIME in you first three commands. You can sign some text_to_sign.txt file with your smime.key and smime.crt:
openssl smime -sign -in text_to_sign.txt -inkey smime.key -signer smime.crt

Create S/MIME certificates


Network drive wont appear or allow anyone to use it

Question: I have a Windows 2008 R2 server that has a drive in it called X: that I would like to use as a drive that everyone can access. However, I can't seem to get it to appear on anyone's computer.
I can access it when I am signed in as the domain administrator at \servername\x$ but when I am signed in as a non-administrator I cannot access it. I have tried to set it to map in group policy but no luck, nor does drive-sharing do anything.
How do I get this drive to appear on all user's computers, so everyone can use it?
Answer: I think the answer is actually in
Windows Server 2008 R2 mapped shares do not show up in command line
which was asking about why a mapped drive was not available in an elevated command prompt.  The answer pointed at  http://msdn.microsoft.com/en-us/library/windows/desktop/ms685143(v=vs.85).aspx  and quotes it as follows:

Drive letters are not global to the system. Each logon session receives its own set of drive letters from A to Z. Therefore, redirected drives cannot be shared between processes running under different user accounts. Moreover, a service (or any process running within its own logon session) cannot access the drive letters that were established within a different logon session.


VBA: pull a value from an html span

Question: I am sure this has been answered, but I think I might not know how to ask the question correctly, so any help that either points me in the right direction and/or answers the question would be greatly appreciated.
I am attempting to pull a value out of a google search for weather.  The page has the code:
 60

What I want to get is the value "60".  The code I am working with is:
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate ("https://www.google.com/#q=weather+little+rock")
While ie.busy
    DoEvents
Wend
Set iedoc = ie.document
temp = iedoc.getElementById("wob_tm").Value

Please and thanks!
Comment: Thanks for helping with the editing.  How do you do the code block?
Comment: iedoc.getelementbyid("wob_tm").innertext
Comment: Perfect!  thank you very much!
Answer: iedoc.getelementbyid("wob_tm").innertext

VBA: pull a value from an html span


Prime elements of $\mathbb{Z}[i\sqrt5]$.

Question: I was studying the Gaussian integers and I proved that every composite number in $\mathbb{N}$ is not a prime in $\mathbb{Z}[i]$. This is true because this ring is an Euclidean domain, and if $n=ab$ is composite then
$$\lambda(n)=\lambda(a)\lambda(b),$$
and $\lambda(a),\lambda(b)>1$ where $\lambda(\alpha)=\alpha\overline{\alpha}$. Basically I used the property that every irreducible is prime.
This same property is not valid in $\mathbb{Z}[i\sqrt5]$ because for example $2$ is irreducible but is not prime, as $2$ divides $6=(1+i\sqrt5)(1-i\sqrt5)$ but $2$ does not divide either of the factors.
How could I prove that there is some composite number of $\mathbb{N}$ which is prime in $\mathbb{Z}[i\sqrt5]$? Is it possible to list the primes of this ring?
Comment: recall that prime elements in *any* integral domain are always irreducible, so if you can show that an element is reducible then necessarily you will have that it is not prime
Comment: @AtticusStonestrom and so it is reducible in any ring containing $\mathbb{Z}$ and so it can not be prime. Thanks!! :)
Comment: then exploit the fact that prime and irreducible elements are the same in $\mathbb{Z}$
Comment: (hence, if an element is not prime in $\mathbb{Z}$, it will be reducible in $\mathbb{Z}$, and so... what can you conclude about it in any ring containing $\mathbb{Z}$?)
Answer: Because $\Bbb{N}\subset\Bbb{Z}[i\sqrt{5}]$, any factorization of $n$ in $\Bbb{N}$ is also a factorization of $n$ in $\Bbb{Z}[i\sqrt{5}]$.
This relies on the fact that the ring extension $\Bbb{Z}\subset\Bbb{Z}[i\sqrt{5}]$ does not introduce any new units, as noted in the comments.
Comment: You have to also note that none of the factors in $\mathbb N$ will become units in $\mathbb Z[i\sqrt{5}]$.
Comment: Thanks!! I have edited my question. Can I list the primes of this ring, or at least describe them?
Comment: @Wojuwu You are right, I have made this explicit.


Create a list in PHP from a DB call

Question: I need to create this list (in javascript):
var videoList = ['GACS1.mp4','GACS4.mp4'];

From a call to the DB. So within the DB I have a field that I am calling to to get those mp4's back.
Here is the function.php, it is really just an Ajax page that processes the request.
function assignedvideos()
{
    global $db_name, $path;
    $uid = $_GET['uid'];
    $selectedvideos = ORM::for_table('movies1')->raw_query('SELECT B.ID AS bid, B.*, LX.* FROM movies1 LX JOIN movies B ON LX.movieid = B.id JOIN locations L ON LX.locationid = L.id JOIN tablets T ON LX.locationid = T.tabletlocation WHERE T.uniqueid =".uid." ORDER BY LX.order ASC')->find_many();
    if($selectedvideos)
    {
        foreach ($selectedvideos as $row)
        {
            $row['movielocation'] = stripslashes($row['movielocation']);
            $row['moviename'] = stripslashes($row['moviename']);
        }
    }
    die();
}

And ofcourse here is the call to the ajax page to retrieve the function.php page to retrieve the values.
$something = $_GET['uid'];
jQuery.ajax({
    url: 'function.php',
    type: "POST",
    data: 'action=assignedvideos&uid='+$something,
    success: function (response) {
    },
    error: function (response) {
    }
});

The question is does this get built in the AJAX and sent back as a string and if so how?
Or does the AJAX return the list of variables and I build the list within the success response of the AJAX call? If so, then how?
Answer: To make it easy on yourself, put whatever values you want to send back to the client into a variable, and then call
echo json_encode($somevar);

in your case, there is an additional problem,
foreach ($selectedvideos as $row)
    {
        $row['movielocation'] = stripslashes($row['movielocation']);
        $row['moviename'] = stripslashes($row['moviename']);
    }

$row is replaced on each iteration of the loop with new data from the next row in $slectedvideos, so your operation basically does nothing, after the foreach $row will contain the last row of data from $selectedvideos. (also, $row will not modify $selectedvideos). if you wanted to modify $selectedvideos, there's a number of ways to do that, but without changing your code too much, you could do
foreach ($selectedvideos as $key => $row)
    {
        $selectedvideos[$key]['movielocation'] = stripslashes($row['movielocation']);
        $selectedvideos[$key]['moviename'] = stripslashes($row['moviename']);
    }

and then
echo json_encode($selectedvideos);

which you can then convert to a javascript object in the success call using JSON.parse()
Note that in this case, the javascript object won't look exactly how you want your data to look, instead it will be an array of objects such that array[0].movielocation will be (if I'm not mistaken) the file location of the first movie etc.
If you want it to be exactly an array of file paths, the foreach loop could look like this:
$output = array();
foreach ($selectedvideos as $row)
    {
         $output[] = stripslashes($row['movielocation']);
    }

echo json_encode($output);

Note also that using stripslashes on the file location will force you to either put all the videos in the same folder on the server or use mod_rewrite in some shape or form.

Create a list in PHP from a DB call


Python imshow: Completely filled and completely blank image, no differences at all

Question: I have two 28x28 matrices, one filled with 0.5s and other filled with -0.5s
show_difference(matrix_filled, matrix_empty)

Results in:

Also, to make sure I'm not messing with the values shader can accept, I also tried,
show_difference(matrix_filled*0, matrix_filled*2)
# numpy arrays

However I still get the same results! Why?

Here's the function I use to show them side by side:
def show_difference(image_a, image_b):
    plt.subplot(1,2,1)
    plt.imshow(image_a, interpolation='none')

    plt.subplot(1,2,2)
    plt.imshow(image_b, interpolation='none')

    plt.show()

Answer: Each imshow plot is normalized individually on its minimum and maximum value. Since minimum and maximum are the same for a constant array, all pixels will have the lowest color from the colormap. And thus both plots look the same.
To define a normalization you may use the vmin vmax  arguments of imshow, e.g.
plt.imshow(array, vmin=0, vmax=2)

or use a normalization instance, e.g.
norm=plt.Normalize(vmin=0, vmax=2)
plt.imshow(array, norm=norm)

So in your case you may first find out the minimum and maximum of both arrays and use those values for the image plot
def show_difference(image_a, image_b):
    norm = plt.Normalize(vmin=np.min([image_a.min(),image_b.min()]),
                         vmax=np.max([image_a.max(),image_b.max()])  )

    plt.subplot(1,2,1)
    plt.imshow(image_a, interpolation='none', norm=norm)

    plt.subplot(1,2,2)
    plt.imshow(image_b, interpolation='none', norm=norm)

    plt.show()


Find which columns of a matrix are linear combination of the others

Question: Consider the example where I have a matrix $\mathbf{D}$ in $-1/1$ coding with $5$ columns,
$$D = \begin{bmatrix}-1&-1&-1&1&1\\1&-1&-1&-1&1\\-1&1&-1&-1&-1\\1&1&-1&1&-1\\
-1&-1&1&1&-1\\1&-1&1&-1&-1\\-1&1&1&-1&1\\1&1&1&1&1\end{bmatrix}$$
We see that the fourth and fifth columns are combinations of the first three columns so that if we label the columns $a,b,c,d,e$ we can say that $d=a*b$ and $e=b*c$. We can separate the columns in two groups: $a,b,c$ are basic columns and $e,d$ are added columns. Furthermore we can call the relations that define $e$ and $d$ as a combination of $a,b,c$ defining contrasts.
My question is this:
Is there a way to determine more generally (for a larger matrix or a matrix with different defining contrasts) which columns are combinations of the others and
Comment: Have you tried Gaussian elimination?
Comment: No, I don't know what it is. Can you explain it quickly ?
Answer: For an $m \times n$ matrix $A$ let $A'$ be the unique row-reduced echelon matrixx that is row-equivalent to $A.$ The relations of linear dependence among the columns of $A$ are the same as those among the columns of $A'.$ The columns of $A'$ in which a leading 1 appears are a basis for the column-space of $A'$ and the columns of $A$ in the same positions are a basis for the column-space of $A.$Let the $t-$th column of $A$ be $\zeta_t$ and let the $t-$th column of $A'$ be $\xi_t$ for $1 \le t \le n.$For a column $\zeta $ of $A$ let $\xi$ be the column of of $A'$ in the same position, let the element in row $i$ of $\xi$ be $c_i$ and, if $c_i \ne 0$ let the leading 1 in row $i$ of  $A'$ occur in column $\nu_i$ of $A'$ for $1 \le i \le m.$ If $c_i=0,$ let
$\zeta_{\nu_i}=\zeta_{\nu_i}=\mathbf 0.$ Then $$\xi=\sum_{i=1}^mc_i\xi_{\nu_i}$$ and $$\zeta=\sum_{i=1}^mc_i\zeta_{\nu_i}$$.
Answer: There is a mutuality in linear dependence.
If a vector $\bf a$ is linearly dependent of $\bf b$ and $\bf c$, then $\bf b$ is linearly dependent on $\bf a$ and $\bf c$ and vice versa.
This is no stranger than we can re-write $${\bf v_0} = \sum_{\forall i \neq 0} c_i {\bf v_i}$$
into
$${\bf v_k} = \frac{1}{c_k}\sum_{\forall i \neq k} c_i {\bf v_i}$$
The linear weights just get scaled by $c_k$.
So what we can do now is to build a linear equation system removing one of the columns as the data $\bf d$, (we modify $\bf D$ by removing this column), and the rest as regression functions and solve with a classical linear least squares:
$${\bf x_o = }\min_{\bf x}\|{\bf D x - d}\|_2^2$$
If there is a perfect fit, then $\bf d$ is linearly dependent on at least some of the vectors in $\bf D$. If not, then still some of the vectors in $\bf D$ may be linearly dependent of each other.
Answer: At the comments @Rodrigo de Azevedo said you can apply Gaussian elimination which is quite intuitive method to apply but I personally suggest that if you want to find the linear dependence relation between rows or columns of a square matrix, taking the determinant of the matrix  then if it is 0 then you may want to apply gaussian elimination. It will reduce the work.


Continued fraction of $π$ using sums of cubes

Question: Recently I came across this identity:
$$\pi=3+\cfrac1{6+\cfrac{1^3+2^3}{6+\cfrac{1^3+2^3+3^3+4^3}{6+\cfrac{1^3+2^3+3^3+4^3+5^3+6^3}{6+\ddots}}}},\tag1$$
thus
$$\pi=3+\cfrac{1}{6+\cfrac{(1\cdot3)^2}{6+\cfrac{(2\cdot5)^2}{6+\cfrac{(3\cdot7)^2}{6+\ddots}}}}.\tag2$$
We also know that
$$\pi=3+\sum_{n=1}^{\infty}\frac{(-1)^{n+1}}{n(n+1)(2n+1)},\tag3$$
Converting the sum to a continued fraction we get:
$$\pi=3+\cfrac{1}{6\cdot1^2+\cfrac{(1\cdot2\cdot3)^2}{6\cdot2^2+\cfrac{(2\cdot3\cdot5)^2}{6\cdot3^2+\cfrac{(3\cdot4\cdot7)^2}{6\cdot4^2+\ddots}}}}.\tag4$$
Are the two continued fractions $(2)$ and $(4)$ really equal? And how is the above continued fraction using sums of cubes derived?
Comment: I should add that I've verified numerically it diverges, as does a couple continued fraction forms earlier on in Foster's derivation. He tried to make a simplified (not "simple")  continued fraction after converting the Nilakantha infinite series into a nonsimple continued fraction.
Comment: The sum-of-cubes formula was written up by  Tony Foster, available  at https://pdfcoffee.com/a-continued-fraction-for-pi-containing-cubes-author-tony-foster-pdf-free.html  .  He did algebraic magic starting with numerators that were squares of triangular numbers.
Answer: The first two identities $(1),(2)$ are wrong; the continued fraction with "sums of cubes" doesn't converge. This can be shown using the known criterion: for a sequence $\{a_n\}$ of positive real numbers, the continued fraction $$a_0+\cfrac{1}{a_1+\cfrac{1}{a_2+\cfrac{1}{\ldots}}}$$ converges if and only if the series $\sum_{n=0}^{\infty}a_n$ diverges. In our case, for $n>0$, we have $a_n=6c_n$ where $c_1=1$ and $c_{n+1}=1/\big(n^2(2n+1)^2 c_n\big)$, and one obtains $\color{blue}{a_n=\mathcal{O}(1/n^2)}$ using, say, the $\Gamma$-function: $$c_n=d_n\left(\frac{\Gamma\left(\frac{n}{2}\right)\Gamma\left(\frac{n}{2}+\frac14\right)}{\Gamma\left(\frac{n+1}{2}\right)\Gamma\left(\frac{n}{2}+\frac34\right)}\right)^2\quad\implies\quad d_{n+1}=\frac{1}{64d_n},$$ giving $d_n=\mathcal{O}(1)$ and $c_n=\mathcal{O}(1/n^2)$ since $\Gamma(x+a)/\big(x^a\Gamma(x)\big)\underset{x\to\infty}{\longrightarrow}1$.
For curiosity, here are computed approximate values of the lower/upper limits, respectively:
$$\mathtt{3.14221404702232210406367353362166370131484883936217-}\\\mathtt{3.15126273205858662275081482878228893534757749143403-}$$
Comment: @mathlover   The sum-of-cubes formula was written up by  Tony Foster, available  at https://pdfcoffee.com/a-continued-fraction-for-pi-containing-cubes-author-tony-foster-pdf-free.html  .  He did algebraic magic starting with numerators that were squares of triangular numbers.
Comment: @metamorphy I've put tags on the OP's equation to clarify that both forms (1) and (2) are wrong. P.S. I haven't checked the last form (4).


Como tratar valores que vem no this.props em React

Question: Tenho um componente Semaforo que chama o componente Luz 3 vezes cada um com seu parâmetro

    <Luz color="red" />
    <Luz color="yellow" />
    <Luz color="green" />

como posso na raiz do componente Luz identificar qual é o valor de classe e adicionar uma classe especifica para cada uma das chamadas?
Ressalto que sempre os 3 componentes <Luz /> serão renderizados cada um com uma classe especifica que alterará seu background e já tentei usar map e não deu certo
Comment: Não ficou muito claro o que você está tentando fazer. O que é essa classe que você está mencionando? A propriedade `color` seria essa classe? E por que você está usando o método `map` para gerar um JSX estático?
Comment: Tem como você exemplificar com o seu código, e porque precisa identificar? tem algum processo que deseja fazer? Talvez o componente Luz só precise mudar a cor (eu to quase entendendo)?
Comment: estou fazendo um sefamofo onde tem a class Semaforo que é essa que coloquei acima e essa class chama o component Luz que é apenas um componente chamado 3 vezes para representar cada cor do semaforo estou passando color="red" para está identificando qual será a sua cor com isso estou tentando receber esse valor no componete Luz e assim efetuar o tratamento para adicionar a cor certa como background do elemento
Comment: Mostre o seu código e você quer ficar trocando de cor?
Comment: Por favor, adicione o codigo do seu componente Luz, para ficar mais claro qual é o problema!
Answer: Dentro do seu componente Luz, voce pode usar o atributo className para adicionar a classe desejada, como abaixo:
const Luz = (props) => {
    return (

        ...

    );
}

E entao definir as cores pelo CSS:
.compLuz.red { background-color: red; }
.compLuz.yellow { background-color: yellow; }
.compLuz.green { background-color: green; }


creating new vector of different strings

Question: New to R so not really sure what I'm looking, but I guess purrr could do this for me.
How could I create this vector:
c <- c("cookie", "cookie", "cookie", "cookie", "dog", "dog", "dog")

from this:
a <- 4
b <- 3

Comment: Do you need `rep(c("cookie", "dog"), c(4, 3))` ?
Comment: Yes, that was quick, thank you!
Answer: Use rep
rep(c("cookie","dog"),c(a,b))


Do you have to remove perfectly collinear independent variables prior to Cox regression?

Question: Suppose you have independent variables that change only with each new time step (and possibly others that change freely):
id   time   X_1   X_2   X_3     …   event
01      1   a_1   b_1   c_1     …       0
02      1   a_1   b_1   c_1     …       0
 ⁝       ⁝     ⁝     ⁝      ⁝      …       ⁝
01      2   a_2   b_2   c_2     …       ⁝
02      2   a_2   b_2   c_2     …       ⁝
 ⁝       ⁝     ⁝     ⁝      ⁝      …       ⁝
01      n   a_n   b_n   c_n     …       ⁝
02      n   a_n   b_n   c_n     …       ⁝

At time=1, all values of X_1 are equal to a_1 (which may or may not equal a_2, a_3, ...) for all ids. Similarly, at time=i, all values of X_1 are equal to a_i for all ids, and so on. Independent variables X_2 and X_3 also change only with each new time step. Other variables might change freely, but they are not important to the question. Are independent variables X_1, X_2, X_3 perfectly collinear? And should they be removed prior to Cox regression due to collinearity or otherwise (zero variance per time step)?
To be clear, all individuals have exactly the same value for any one of X_1, X_2, X_3 at any given time point.
Comment: No these variables are not perfectly collinear. Supposing, say, you had height, weight, and age. Obviously they all change together, but they don't *perfectly predict* one another. Of course, the Cox model is in many ways like a categorical analysis, such as logistic regression, so that certain expressions of the model matrix that might converge with a linear regression will fail in a Cox or logistic model due to separation.
Comment: Is this any different from what would happen with `age` if you updated it as a predictor at each time point? Multicollinearity has to do with correlations _among_ predictor variables, not within a single predictor. Are you also saying that the values of `X_2` or `X_3` are changing in parallel with the values of `X_1` in a way that introduces linear dependence among them? Please provide that information by editing the question, as comments are easy to overlook and can get deleted.
Comment: "Are you also saying that the values of X_2 or X_3 are changing in parallel with the values of X_1 in a way that introduces linear dependence among them?" The values of `X_2` and `X_3` also change only with each new time step. I am not sure if it introduces linear dependence or not. For example, `age` and `year` have a linear dependence, but what about `year` and `annual_inflation_rate`, or a less obvious example, `annual_inflation_rate` and `annual_interest_rate`.
Comment: @AdamO So they are not perfectly collinear, but should they be removed prior to Cox regression due to zero variance per time step? How would the regression model know which variable is affecting the hazard?
Comment: @user572780 you say 0 variance but you mean 0 variance within a cluster id if your data example is setup correctly. Consider the whole sample.  If the X_i is completely constant within each value of id, that's fine as long as there's variability between each id. It amounts to adjusting for a between-cluster effect in a panel regression.
Comment: @AdamO The data shows zero variance per time step for all ids, right? For example, if each id represents a customer, then at `time=1`, all customers live in the same `year`, with the same `annual_inflation_rate` and the same `annual_interest_rate`. I don't see how the regression model will "know" the effect of `year` vs the effect of `annual_inflation_rate` vs the effect of `annual_interest_rate`. To me, it seems that the reasonable thing to do is to remove the additional variables and keep one dummy variable to absorb all effects that happen at each time step.
Comment: @user572780 It depends if all subjects in the sample are included and followed over the same time periods. If your data is one row per subject per year, then adjusting for year is not necessary because you already have a baseline hazard function to account for calendar time.
Answer: Perfect multicollinearity among predictors should be dealt with in any regression, Cox or otherwise. Some software might just refuse to fit such data. Some functions are smart enough to find and remove enough predictors to ensure linear independence (perhaps silently), but you don't want to count on that and you presumably would prefer to make such a choice consciously yourself.
Multicollinearity doesn't arise from associations of values within single predictors like your X_1. It has to do with linear associations among predictors. There's no way to say in general whether the type of data structure you describe will lead to perfect multicollinearity among X_1, X_2 and X_3.
The problem in your scenario is that you might not gain any information from including covariates that have "zero variance" among members of the risk set at all event times in a Cox model. If all individuals at risk at each event time have identical values of a particular covariate $X_j(s)$, and that is true for all of the event times $s$, then you can't estimate a hazard for that covariate unless it has interactions with other covariates.
The vector of coefficient estimates $\hat \beta$ in a Cox model comes from solving the partial-likelihood vector score function $U$ as a function of the covariate-coefficient vector $\beta$ for $U(\hat \beta)=0$. Following Therneau and Grambsch on page 40, with p covariates and n individuals, the $p \times 1$ score vector is:
$$ U(\beta) = \sum_{i=1}^n \int_0^{\infty} [X_i(s) - \bar x(\beta,s)]dN_i(s),$$
where $\bar x(\beta,s)$ is a risk-weighted mean of the $X$ values over the risk set at time $s$ and $dN_i(s)$ is 1 at the event time for individual $i$ and 0 otherwise.
If the entire risk set at an event time $s$ has identical values of $X_j$ and that holds true for all values of $s$, then the $j^{th}$ component of the score vector is necessarily 0 regardless of the coefficient values $\beta$. There is thus no unique solution for $\hat \beta_j$. If $X_j$ is involved in interaction terms with other covariates, however, then estimates of those interaction coefficients could be possible.
This difficulty with modeling such covariates in a Cox model might be overcome in a parametric survival model. With a parametric model, covariates can contribute to the entire likelihood function over time, as summarized on this page, and thus allow for estimation of their associated coefficients if they vary over time. Unless they follow defined functions of time, however, time-varying covariates for an individual are modeled over a set of time spans, each of which is left truncated and is either right-censored or ends with an event. If you choose an accelerated failure time parametric model, handling left truncation requires some care as explained in this vignette of the R eha package.

For reference of others reading this thread, know that time-varying predictors in a Cox model are typically formatted in a different way than shown, with one data row for each combination of individual and time period during which all covariate values are constant. That's sometimes called the counting-process format. Each row specifies the start time (left truncated) and end time (censoring/event) of each such time period, along with the covariate values and an event/censoring indicator. See the R time-dependence vignette for details.
Comment: I have changed the format of the data. The variables are not perfectly collinear. What about variables with zero variance per time step (the case here)? Should they be removed?
Comment: @user572780 it's hard to say without more details of the data. For Cox models it's generally best to incorporate as many predictors as you can without overfitting, to minimize omitted-variable bias. Even if all of the individuals have the same value of a covariate at any particular time, the changes in estimated hazard as that covariate value changes over time will provide some information. You might, however, have problems trying to make predictions from new sets of time-varying covariate values. See [this thread](https://stats.stackexchange.com/q/488992/28500) for example.
Comment: I have difficulty understanding why I should include variables with zero variance per time step. How would the regression model know which variable is affecting the hazard? I was thinking of combining all of the variables with zero variance into a new term. Is that a reasonable thing to do?
Comment: @user572780 I maybe hadn't completely internalized that for any one of those predictors at any given time point,  _all_ individuals have _exactly_ the same value. So you are correct--if each is modeled independently of other predictors. If some of those predictors are involved in interactions with other predictors that don't have zero variance, however, you would need to include them to model those interactions. Otherwise, I don't see that combining all of the zero-variance predictors gains you anything at all--you still have a combined predictor with zero variance at any time point.
Comment: Okay, that clears it up. I was thinking that there is still information to gain by including a *single* predictor with zero variance at any time step. For example, if you keep the `year` predictor, which has zero variance at any given time step. Then, you might notice that when `year=2021`, there was an increase in loan defaults. And you might interpret that as an increase in the hazard due to `year=2021` (or `annual_inflation_rate` or `annual_interest_rate` or any other predictor with zero variance at any time step).
Comment: @user572780 I've expanded my answer to try to show when such predictors might be useless, when they might provide information, and to discuss the possibility of parametric modeling.


Waterline: Sum/Avg/Mix/Max of populated values in waterline

Question: Please see the example:
User Object:
{
    name: 'Mike',
    age: 16,
    createdAt: Wed Feb 12 2014 18:06:50 GMT-0600 (CST),
    updatedAt: Wed Feb 12 2014 19:30:54 GMT-0600 (CST),
    id: 7
}

Pet Objects associated with the user Mike
{
    name: 'Scooby',
    color: 'pink',
    petAge: 2
    id: 1,
    createdAt: Wed Feb 12 2014 18:06:50 GMT-0600 (CST),
    updatedAt: Wed Feb 12 2014 18:06:50 GMT-0600 (CST)
}

{
    name: 'Alpha',
    color: 'red',
    petAge: 5
    id: 2,
    createdAt: Wed Feb 12 2014 18:16:50 GMT-0600 (CST),
    updatedAt: Wed Feb 12 2014 18:16:50 GMT-0600 (CST)
}

I want to get sum of 'petAge' for pets of Mike. I was trying the below query, but it did not work.
User.find({name:'Mike'})
.populate('pets')
.sum('pets.petAge')
.exec(function(e,r){

});

I want to understand if getting sum of petAge is possible or not. (sum('pets.petAge')).
Comment: Try `sum('petAge')` . It should work, because after populating `pets`, you would have array of pets having `petAge` as property.
Comment: @MukeshSharma, just tried. That is not working.
Answer: What you can do is use the sum function on the Pets model like this.
Pets.sum('petAge').where({user: "UserID"}).exec(function (e,r) {
//do what you want here
});

here UserID is the id of the user.


c++ program has triggered a breakpoint on operator[]

Question: I'm creating class Student, and I'm getting an error:
program has triggered a breakpoint
on operator[]. Please help me and show what's wrong, and what is missing.
Fixed:
#include "stdafx.h"
#include <iostream>

class Student {
public:
    Student();
    ~Student();
    void Show();
private:
    char* Name;
    char* Surname;
    char* DateOfBirth;
    unsigned long PhoneNumber;
};

Student::Student() {
    Name = nullptr;
    Surname = nullptr;
    DateOfBirth = nullptr;
}

Student::~Student() {
    if (Name != nullptr) delete[] Name;
    if (Surname != nullptr) delete[] Surname;
    if (DateOfBirth != nullptr) delete[] DateOfBirth;
}

void Student::Show() {
    std::cout << Name << " " << Surname << "\t" << DateOfBirth << "\t" << PhoneNumber << std::endl;
}

class StudentGroup {
public:
    StudentGroup();
    ~StudentGroup();
    void Add(char* Name, char* Surname, char* DateOfBirth, unsigned long PhoneNumber);
    Student& operator[](int index);
private:
    Student* stud;
    int length;
};

StudentGroup::StudentGroup() {
    length = 0;
    stud=nullptr;
}

StudentGroup::~StudentGroup() {
    delete[] stud;

}

void StudentGroup::Add(char* Name, char* Surname, char* DateOfBirth, unsigned long PhoneNumber)
{
    Student* stud1;
    stud1 = new Student[length+1];
    for (int i = 0; i < length; i++)
        stud1[i] = stud[i];
    delete [] stud;
    stud = stud1;
    stud[length].SetName(Name);
    stud[length].SetSurname(Surname);
    stud[length].SetDateOfBirth(DateOfBirth);
    stud[length].SetPhoneNumber(PhoneNumber);
    length++;
}

Student& StudentGroup::operator[](int index) {
    return stud[index];
}

int _tmain(int argc, _TCHAR* argv[])
{
    setlocale(LC_CTYPE,"Russian");
    StudentGroup students;
    students.Add("Ivan","Ivanov","2010-01-01",234234422);
    students.Add("Petr","Petrov","2012-01-01",365753663);
    students[0].Show();
    return 0;
}

Comment: Your constructor is faulty in that it does not initialize `stud` to a nullptr.
Comment: Relieve yourself of memory allocation issue and use `std::string`.
Comment: ***Please help me and show what's wrong*** You probably need to spend some time and debug so that you can tell us what you think is wrong.
Comment: See https://stackoverflow.com/questions/4193982/delete-a-pointer-to-pointer-as-array-of-arrays

, It makes good approach to deal with pointer object that has pointers, deletion mechanism!
Comment: Short answer: undefined behavior. I see how it invokes it in (at least) 2 different ways, in `StudentGroup::AddStudent`.
Comment: Error is in `operator[]`
Comment: What debugging details it is needed? It doesn't show any breakpoint.
Answer: drescherjm is correct in the comments.
Your constructor doesn't initialize StudentGroup::stud, so when you attempt to increase the size of it you call delete [] <uninitialized_pointer>.
To fix this, you should have:
StudentGroup::StudentGroup() {
    stud = nullptr;
    length = 0;
    nextIndex = 0;
}

Comment: Did you test it before posting? if true, which platform or compiler you used?
Comment: @AliRazmkhah: Whatever repl.it uses and gdb.
Comment: Hi, this does not help, error is in `operator[]`
Answer: You struct student has pointers, exactly 3 pointers!
It is obvious calling delete[] stud; without handling deletion of char* Name, char* Surname and char* DateOfBirth members of each student object, leads to HEAP CORRUPTION ERROR
Comment: This would lead to a memory leak, but I don't see how this would lead to a corruption of the heap.
Comment: Very Simple! Run the code! heap corruption error appeared! In vs 2017 c++11!
Comment: And please tell me, deletion of pointer object that has some pointer members (array) that are initiated by "new" operator leads only memory leak??? nor Heap corruption??
Comment: Thank you, but error is in `operator[]`. What's wrong here?
Comment: Please see above comments, in vs 2017 c++11 as adding second student and "delete[] stud" called I encountered "HEAP corruption error"
Comment: Sorry, I changed many things. I have edited the code.
Comment: Added fixed code to the question.


How To Set Sources For Jupyter Notebooks

Question: I've just bought a new computer, and can't remember/don't know how I had Jupyter notebooks set up on my old machine. Specifically, how do you tell Jupyter about sources.
If I have a conda environment called django, and I do source activate django followed by jupyter notebook, I end up with a notebook, but then when I do from django.http import HttpResponse I get the error message
ModuleNotFoundError: No module named 'django'

Which is bizarre (I think), because Django is definitely installed in the Django environment.
How do I set up my Jupyter notebooks so that they know about the various sources that I want them to know about?
Answer: It is very likely your conda environment django doesn't have iPython Kernel installed which jupyter notebook needs. You can run jupyter notebook without activating a conda environment as notebook allows you to select any conda environment with ipykernel installed.
Try:

run in terminal : source activate django
run in terminal : conda install ipykernel
run in terminal : source deactivate
run in terminal : jupyter notebook
select django for your kernel
Comment: I'm not familiar with conda. Does creating the `django` virtual environment in conda automatically create the `django` kernel in Jupyter?
Comment: @AChampion, Good question. Creating conda environment **does not** install `ipykernel` automatically.
Answer: I create a kernel for each of my virtual environments. On MacOS Jupyter kernels are located here $HOME/Library/Jupyter/kernels. Make sure ipykernel is installed in your virtual env.
So $HOME/Library/Jupyter/kernels/myenv/kernel.json looks like:
{
 "env": { "VIRTUAL_ENV": "/Users/<User>/<myenv>" },
 "argv": [ "/Users/<User>/<myenv>/bin/python3", "-m", "ipykernel", "-f", "{connection_file}" ],
 "display_name": "My Virtual Env",
 "language": "python"
}

Answer: Found the answer here.
source activate django
conda install ipykernal
python -m ipykernel install --user --name django --display-name "Python (Django)"


Multivariate analysis?

Question: I realize this might be a fairly broad question but could anyone suggest an appropriate statistical approach for analyzing this kind of data? For example does % of dead cells differ significantly between doses and does the % of cell types differ?

Do the total % of dead cells and the types which make it up represent separate dependent variables and therefore constitute multivariate analysis?
Comment: Do you know the size of the sample from which this parblot was obtained?
Comment: Two replicates of 8000 cells for each dose
Answer: I'm not sure I would consider this a multivariate question.
To me it looks like logistic regression:
% dead cells ~ Dose * Type
The interaction term would tell you if the type of dead cells differs by dose.
Answer: Maybe what you want to test here is probably this: "are Dose and Type independent?"
$\chi^2$ Test of Independence
Since we know the sample size $N$ we can conduct a $\chi^2$ test of independence.
This is our observed fractions (per dose)
<code>         dose
cell.type     0 0.1    1   10
        A 0.080 0.1 0.06 0.10
        B 0.135 0.2 0.22 0.15
        C 0.165 0.2 0.32 0.50
</code>

Since there there are 8000 observations per dose, we get the following observed values
<code>         dose
cell.type    0  0.1    1   10
        A  640  800  480  800
        B 1080 1600 1760 1200
        C 1320 1600 2560 4000
</code>
Assuming independence, we'd expect to get these values (it's row total times column total divided by overall total for each cell)
<code>         dose
cell.type    0  0.1    1   10
        A  463  610  732  915
        B  961 1265 1517 1897
        C 1615 2126 2551 3188
</code>
So we compare them, calculate the $X^2$ statistics and calculate the $p$ value, which, in this case, is very low ($\approx 10^{-16}$).
Thus, we reject the independence hypothesis and conclude that they cannot be independent.
Caveat:

the sample size is very big (8000) - which results in a too small $p$ value (see more here)

R code
<code>data_raw = c(0.080, 0.1, 0.06, 0.10,
             0.135, 0.2, 0.22, 0.15,
             0.165, 0.2 ,0.32, 0.50)

tbl = matrix(data=data_raw, nrow=3, ncol=4, byrow=T)
dimnames(tbl) = list(cell.type=c('A', 'B', 'C'),
                     dose=c('0', '0.1', '1', '10'))
barplot(apply(tbl, 2, rev), col=c('red', 'green', 'blue'),
        ylim=c(0, 1), xlab='dose',
        legend.text=rev(c('A', 'B', 'C')))

N = 8000
observed = tbl * N
chisq.test(observed)
</code>
References

http://mlwiki.org/index.php/Chi-Squared_Test_of_Independence

Multivariate analysis?


Renderizar PartialViews diferentes de forma aleatória

Question: Eu tenho o seguinte cenário: tenho que passar para uma view dados de tabelas diferentes (dados parecidos, porem sem relação alguma), até ai consegui de boa. Criei uma classe(viewmodel) que recebe os dados, no Controller alimento ela quando vou dar o retorno para a view, e na view dou um foreach em cada propriedade e dentro desse foreach renderizo as PartialView respectivas. Até ai tudo certo.
Porém, tenho a intenção de "misturar" essas partialView, e não mostrar primeiro os dados de uma propriedade e depois que acabar todos, ir para outra.
Como exibiria isso de uma forma "aleatória"? Ou melhor, qual a lógica para exibir algo "aleatório"? Seria algum laço for?
Para maior entendimento, segue os ViewModel, Controller e View:
ViewModel
public class VideosManuaisIndex
{
    public IEnumerable<Video> Videos { get; set; }
    public IEnumerable<Manual> Manuais { get; set; }
}

Controller
public ActionResult Index()
{
    return View(new VideosManuaisIndex
    {
         Videos = db.Video.ToList().OrderBy(v => v.sistema),
         Manuais = db.Manual.ToList().OrderBy(m => m.sistema)
    });
}

View

@foreach (var manual in Model.Manuais)
{
    @Html.Partial("_Manual", manual);
}

@foreach (var video in Model.Videos)
{
    @Html.Partial("_Video", video)
}

Answer: Bem, você poderia criar um For Loop, e ir verificando se os valores são nulos, não sei se é ortodoxo mas ficaria algo como:
@{

    var length = Model.Manuais.Count > Model.Videos.Count ? Model.Manuais.Count : Model.Videos.Count;

    for (int i = 0; i < length; i++)
    {
        if(Model.Manuais[i] != null)
        {
            Html.Partial("_Manual", Model.Manuais[i]);
        }

        if (Model.Videos[i] != null)
        {
            Html.Partial("_Video", Model.Videos[i]);
        }
    }
}


Regression form - which way round

Question: The phrasing of regressions confuses me, but I'm aware of how to do it in general. Could you clarify as to whether the log of imports or GDP is the Y variable in $Y=a+bX+u$.
The task is as follows:

Perform  a regression  of  the  logarithm  of  imports on a constant
  and the  logarithm  of  GDP.

A rather basic question I'm sure for here, but thanks for the help.
Answer: The "on a constant" part gives it away.  It must be:
$$ \log(\text{imports}) \approx a + b \log(\text{GDP}) $$
The general terminology is "regress $y$ on $x_1, x_2, \ldots, x_n$".

Regression form - which way round


Can this chord be played or is it transcribed incorrectly?

Question: I'm just starting to learn to play the piano and read music and have no intention of trying to learn this piece at least for a long time, but out of curiosity I had to take a look.
Here's a snippet of the very first part of the first part of "Vingt Regards sur l'Enfant Jésus" by Olivier Messiaen, from a transcription on musescore.com:

I'm looking at the first five-note chord for the left hand on the lowest of the three staves, circled in red. Can somebody

Confirm if I'm reading the notes right?  I think this piece is in F#, and I read it as (starting on the lowest note, and ignoring the grace note preceeding the chord): A# B# E A A#

Explain how on earth anybody can play that with one hand?

Watching both Ashley Hribar and Steven Osborne play these first chords in performances on youtube I can't see any obvious sign of either crazy contortions or rolling of the hand to play the notes non-simultaneously!
Is this transcription wrong?  Am I reading it wrong?  Are these pianists space aliens with extra fingers?
I suspect the transcription because it's so crazy, and I'm not sure it sounds right either (hard to tell because I can only hit all the notes one chord at a time!).
Comment: _Ignoring the grace note_ is where your analysis falls flat, if you will.
Answer: The transcription is correct. Below is a screenshot from a YouTube video that includes the score.
To play the chord, play the grace note (A#) with your pinky, and hold it with the pedal. The remainder of the chord B#-E-A-A# can be played 5-3-2-1 (or 5-4-2-1). Someone with big enough hands could play the entire thing, including the grace note, 5-4-3-2-1.
Screen capture of Vingt Regards sur l'Enfant-Jésus (1944), 1. "Regard du Père", measure 1.
Comment: By the way. As so often in Messiaen, the notes are all from an octatonic scale: A# B# C# D# E F# G A.  It's a lovely piece isn't it? Good idea not to try and learn it if you're just starting! Well done for reading the notes correctly.
Comment: Re the "big enough hands" point, I learnt to play as a kid, but since then I've only dabbled for 20+ years and never really got any better.  One of "my" pieces is the *Moonlight Sonata*, and that features an octave-plus-one stretch.  I don't have spider fingers, but I can do that.  I was very amused to find that my mum, who taught piano for 30 years and in retirement "tours" her area to play the organ for church services, has to fake that stretch because her hands were never big enough, and she can only just do a full octave.  Don't let small hands hold you back, people. :)
Comment: I just tried this, and I can still only see how one could possibly do it with 5-4-1-2 (i.e 2 hitting the A# and thumb on the A) and painful contortions! So I think I still don't understand
Comment: @CroadLangshan My suggestion is 5-on-B#, 3-on-E, 2-on-A, 1-on-A#. Does that work for you? If no, where in your fingers/hand/arm do you feel the most strain?
Comment: If my left hand 5/3/2 are on those keys, then my thumb is nowhere near A# :-)
Comment: There are two A#s. One is the low grace note; play that with your pinky, then hold it with the pedal and let go with your pinky so you can re-use that finger for the B#. The other A# is the top of the chord (which contains both A and A#); play that one with your thumb. The other issue might be that you need to move your hand away from your body and toward the back of the keys. That will move your thumb closer to the A# so you can avoid any twisting. It's okay if your other fingers are between the black keys.
Comment: Thanks, I do have the right A# I believe (the second one below middle C) -- by "nowhere near" I mean a few centimetres away above my thumb.  I've tried the back of the keys too -- then with my thumb on the A# I can reach G# with 2, but definitely not A.  On the other hand if I'm at the front of the keys and -- just barely -- have 5/3/2 on B#/E/A, my thumb is a mere... 11 cm from the A# (way in front of the keyboard).  I think my hands are just too small for this chord unless I cheat playing both the A and A# with my thumb
Comment: If you leave out the A natural, can you play the B#-E-A# chord?
Comment: Yes, I can play those three easily!  But if you're going to suggest then playing the A natural with my right hand with @Alexander Woo: I can do that, but then I can't get anywhere near the A and A# that my right hand is notated as playing.  Seems like the only way I can reach is with the cheat that Olly suggested still.
Comment: @CroadLangshan I was looking to see where you're running into trouble. It sounds like the issue is the stretch involving your second finger. Given that you're a beginner, you'll have to discover whether you gain flexibility as you gain experience.
Answer: You could also play both the A and A# at the top of the chord with your thumb if you get the position just right (thumb at the front left corner of the A#)
Comment: Thanks, I discovered this too experimenting after reading @Aaron's answer, it's the only way I can do it as far as I can tell
Answer: You could also play the A# (or maybe actually it's more comfortable playing the A - or maybe not but it's easier to get the intended voicing right that way) with your right hand.
In general, the distribution between the staves indicates how you are supposed to think about the chords, not how you are supposed to actually play them.
Comment: Interesting -- I can do that, but then I can't get anywhere near the upper A and A# that my right hand is notated as playing :-|


Are there popular dimensionality reduction tools for classification type supervised learning?

Question: I am familiar with PCA as a linear transformation in order to align the axis of the IV space with the directions of maximal variance in order to possibly be able to reduce the dimensionality of the problem. Are there similar techniques (or can one use PCA itself) when facing a binary classification problem? Like a logistic regression? I.E. Suppose we have a dependent variable we want to predict which only takes values 0 and 1 and we have a lot of features from which to predict, but maybe there are "principal components" in the high dimensional feature space which carry the most explanatory power.
Comment: It is unclear in your question how "dimensionality reduction" task relates to "classification" task, and whether "binary" applies to the nature of variables (features) or the number of classes (i.e. there's 2 classes). Please clarify.
Comment: Because you have only 2 classes, only 1 predictive "principal component" can exist, and that being a linear combination of the features, the regressional model. You could prefer logistic regression or linear regression. You could then drop features with small coefficients from the model if you want to have a concise model. There is no need for preliminary PCA or PLS at all, for me (unless there are collinearity/singularity problems).
Comment: You mean in a multi-class problem PCA/PLS could be useful but not so in the case of two classes? If i visualize a 3D axis with y-values of 0  or 1, cant there still be two orthogonal u_1, u_2 vectors spanning a "PCA" like frame? (i.e. say two principal components with one capturing more variance?)
Comment: Oh also, what about PLS-DA? Wikipedia says: "Partial least squares Discriminant Analysis (PLS-DA) is a variant used when the Y is binary."
Comment: `You mean in a multi-class problem PCA/PLS could be useful but not so in the case of two classes?` No I don't mean this. I mean preliminary PCA is blind to the existance of classes, so if you retain just few first principal components after it, they are not necessarily the best or even good discriminators between the classes.
Comment: @Palace, you can also use linear discriminant analysis (LDA), see e.g. this recent thread: [Supervised dimensionality reduction](http://stats.stackexchange.com/questions/161362).
Answer: Yes, you can use PCA retaining only certain components as pre-processing step for classification. It is actually pretty common in chemometric classification.
However, I prefer PLS as it also uses your class labels and tries to find good separation (which PCA doesn't).
In both cases, keep in mind that this is a data-driven pre-processing and validation of the final model needs data independent also of these pre-processing steps.
Comment: That's right, but one can use regularized LDA (rLDA), which is pretty similar in spirit to PCA+LDA or PLS+LDA...
Comment: +1. I know this is a very old thread but it was bumped to the front page again. It seems that linear discriminant analysis (LDA) is another natural option to consider in this case.
Comment: Yes, but keep in mind that LDA (at least with the usual algorithms) needs to have reasonably low dimensionality before it can be applied in order to obtain a stable inversion of the covariance matrix. At least in my field, dimensionality reduction is often done in order to become able to do LDA...
Answer: I like Information Gain, but there are others.
I found this paper (Fabrizio Sebastiani, Machine Learning in Automated Text Categorization, ACM Computing Surveys, Vol. 34, No.1, pp.1-47, 2002) to be a good theoretical treatment of text classification, including feature reduction by a variety of methods from the simple (Term Frequency) to the complex (Information-Theoretic).

These functions try to capture the intuition that the best terms for ci are the
  ones distributed most differently in the sets of positive and negative examples of
  ci. However, interpretations of this principle vary across different functions. For instance, in the experimental sciences χ2 is used to measure how the results of an observation differ (i.e., are independent) from the results expected according to an initial hypothesis (lower values indicate lower dependence). In DR we measure how independent tk and ci are. The terms tk with the lowest value for χ2(tk, ci) are thus the most independent from ci; since we are interested in the terms which are not, we select the terms for which χ2(tk, ci) is highest.

These techniques help you choose terms that are most useful in separating the training documents into the given classes; the terms with the highest predictive value for your problem.
I've been successful using Information Gain for feature reduction and found this paper (Entropy based feature selection for text categorization Largeron, Christine and Moulin, Christophe and Géry, Mathias - SAC - Pages 924-928 2011) to be a very good practical guide.
Here the authors present a simple formulation of entropy-based feature selection that's useful for implementation in code:

Given a term tj and a category ck, ECCD(tj , ck) can be
  computed from a contingency table. Let A be the number
  of documents in the category containing tj ; B, the number
  of documents in the other categories containing tj ; C, the
  number of documents of ck which do not contain tj and D,
  the number of documents in the other categories which do
  not contain tj (with N = A + B + C + D):

Using this contingency table, Information Gain can be estimated by:

This approach is easy to implement and provides very good Information-Theoretic feature reduction.
You needn't use a single technique either; you can combine them.  Ter-Frequency is simple, but can also be effective.  I've combined the Information Gain approach with Term Frequency to do feature selection successfully.  You should experiment with your data to see which technique or techniques work most effectively.
Comment: Welcome to the site, @BobDillon. We are trying to build a permanent repository of high-quality statistical information in the form of questions & answers. Thus, we're wary of link-only answers, eg due to linkrot. Can you post a full citation & a summary of the information at the link, in case it goes dead?
Comment: I would have liked to added links to the papers but I do not have enough points yet to have those links as well as images of the equations/tables.


Instance initializer in interface java

Question: Hey I was wondering if it would be possible to give some initialization to an interface when an implementer is made. Like a blank constructor in an abstract class.
I tried something like this:
public interface State {

{
//Do something.
}

public void render();
public void tick();
}

But it does not let you have an instance initializer. Is there any way to do this? Possibly with an inner class?
So the idea is that a piece of code is automatically called when a new instance of an implementing object is created.
Answer: You cannot have static or instance blocks in an interface. But as of java 8 you can have static and default methods.
public interface MyData {

default void print(String str) {
    if (!isNull(str))
        System.out.println("MyData Print::" + str);
  }

static boolean isNull(String str) {
    System.out.println("Interface Null Check");

    return str == null ? true : "".equals(str) ? true : false;
  }
}

Comment: Yes but the idea is that a piece of code is automatically called when a new instance of an implementing object is created.
Answer: You cannot do this, an interface cannot define an initializer.
An interface is basically a list of method signatures.
Comment: Second statement is not true anymore as java8 added default implementation to methods in an interface
Comment: Also you can declare fields (they become `static final` automatically)
Answer: An Interface doesn't work that way, it is a list of method signatures, methods that have to be implemented in a class that implements the interface. To do that you will need a class not an Interface.
There is a possible solution for something like this, from Java 8 you can create static and default methods in your interface, that allows you to create methods with a body in interfaces.

Instance initializer in interface java


How can I protect a disabled form from being submitted?

Question: I need to protect URL alias from being submitted.

I've tried setting #disabled, #access, unset(), and what not. It hides the form, but the user is still able to modify the form on the client-side and submit it.
How do I prevent this form from being submitted on the client-side?
My current module code
<?php
function mymodule_form_alter(&$form, &$form_state){
  if (empty($form['type']['#value'])) return;
  $form['#after_build'][] = 'mymodule_after_built';
}

function mymodule_after_built($form, &$form_state) {
  $form['path']['#access'] = FALSE;
  $form['path']['alias']['#access'] = FALSE;
  $form['path']['alias']['#value'] = $form['path']['alias']['#default_value'];

  return ($form);
}

UPDATE:
To clear the question, I am not asking how to hide the form, but how to prevent the end-user from manually altering the code and submitting the form I just hid.
End-user can just edit source code and add <input type="text" id="edit-path-alias" name="path[alias]" value="" size="60" maxlength="255" class="form-text"> and still enter the alias.
Comment: Why don't you just unset this field in `hook_form_alter` directly? I mean, a whole vertical tab. Try. And if you try and fail, post the code you tried. Make sure you inspect form's structure first, with `kpr`, `var_dump` or something.
Comment: I've tried `unset($form['path'])` in `hook_form_alter`. As far as I know, it's not possible to unset the form in `hook_form_alter`, unless attaching it to `#after_built`.
Comment: I never experienced any serious problems with unsetting form elements directly. So 1) how do you know it's really `$form['path']`? 2) What exactly happened when you tried?
Comment: Also, see https://www.drupal.org/node/1131786 - was this what you are using? If solution stopped working, consider reopening that issue...
Comment: Good article. Yeah, because path is added in `path_form_alter`, it's not accessible in `hook_form_alter`, which is why `#after_built` seems like it's the only way to override it.
Comment: ...so the solution works. My question is that although I am able to hide the element, the end-user is still able to manually edit html code to send the form, even-though it's not present.
Comment: I've updated the question for clarity
Answer: Found the issue! It seems to be a Drupal bug.
#access set in #after_build does not completely override the #access settings, set in hook_form_alter.
In theory, a form is altered by hook_form_alter and hook_form_alter is altered by #after-build. When I try setting #access in hook_form_alter, to alter the form, everything works. However, when I try setting #access permissions in #after_build, to alter hook_form_alter, it only hides the form, still allowing anyone to submit it, by altering the code on the client side.
As a temporary solution, I disabled URL alias permissions, by unchecking "Administer URL aliases" and "Create and edit URL aliases".
Answer: If you set #access to FALSE in #after_build callback, the value will not be submitted even if it's sent to server.
Comment: That's what I thought also. But it is actually submitted to the server. My code example already sets the element `#access` to `FALSE`, but I'm still able to manually send the form to the server.
Comment: Send is okay. But form API should not pass it in $form_state['values'] in submit callback. Does it?


How to upload files with inertia js and laravel 8.0

Question: I have an update ( patch form ) that can have some basic fields with one featured image and an array of gallery images.
updatePhotoPreview(event) {
   this.featuredImagePreview = URL.createObjectURL(event.target.files[0])
   this.updateProductForm.featured_image = event.target.files[0]
},

Before sending the request and even in the request header, the featured image is File and binary.
Then I'm simply posting the form with this.form.patch(url).
I'm getting an error the featured_image must be an image.
I've dumped the request and somehow my featured_image value has been set to an empty array.
The same is for the gallery images, it has been turned into an array of empty arrays.
I've tried changing the route to post, put, the result is the same, I've added a custom header
 { headers: {'Content-Type': 'multipart/form-data'} }

The result is the same.
but the same form is working for the create endpoint of this resource with POST method.
I've removed all the request classes and dumping with request()->all()
How can I fix this?
Answer: It's possible to upload a photo via InertiaJS and Laravel 8 following the steps below:

In our VueJS component's HTML:

<input type="file" class="hidden"
             ref="photo"
             @change="updatePreview">

In our methods object:

updatePhotoPreview() {
    const reader = new FileReader();

    reader.onload = (e) => {
        this.preview = e.target.result;
    };

    reader.readAsDataURL(this.$refs.photo.files[0]);
},
storePhoto() {
    if (this.$refs.photo) {
        this.form.photo = this.$refs.photo.files[0]
    }

    this.form.post(route('photo.store'), {
        preserveScroll: true
    });
},

In our data function:

data() {
   return {
       form: this.$inertia.form({
           '_method': 'PUT',
           photo: null,
       }, {
           resetOnSuccess: false,
       }),
       preview: null,
   }
},

In our Laravel Controller:

class PhotoController
{
    public function store(array $input)
    {
        Validator::make($input, [
            'photo' => ['nullable', 'image', 'max:1024'],
        ]);

        if (isset($input['photo'])) {
           $photo->storePublicly();
        }
    }
}

Comment: Works perfect with `form.post` and add `'_method': 'PUT'` to params.. Thank you!
Comment: Always happy to help @MrEduar ! :)
Comment: When I add this I am getting, `The PUT method is not supported for route pateints/create. Supported methods: GET, HEAD, POST`
Answer: I fix this by adding '_method' on form and using 'post' request.
data() {
    return {
        form: this.$inertia.form({
            _method: "PUT",
        })
    };
},
methods: {
    submit(form) {
        this.form
            .post("route/id", form, {
                preserveState: true
        })
    }
}

Comment: When I add this I am getting, `The PUT method is not supported for route pateints/create. Supported methods: GET, HEAD, POST`
Answer: var data = new FormData()
data.append('first_name', first_name || '')
data.append('last_name', last_name || '')
data.append('email', email || '')
data.append('password', password || '')
data.append('photo', photo || '')

Inertia.post('/users', data)

Try this. Just use FormData() instantiation
Please refer the documentation https://inertiajs.com/forms#file-uploads
Answer: use formData

const fd = new FormData()

fd.append('file', this.form.file)

this.$inertia.post(route('file.register'), fd)


Google/Bing Maps load on dynamically generated div

Question: How can I load Bing/Google Maps on a dynamically generated div?
Example

...

$('#cont').append('

');//dynamically add div

var map = new Microsoft.Maps.Map($("mapDiv"), myOptions);//Bing/Google maps

Any suggestion?
Answer: There is no problem in adding Google Maps or Bing Maps on a dynamic node.
But you should change your code a little bit, in order to properly use the HTML node with [0]. And don't forget the #
var map = new Microsoft.Maps.Map($("#mapDiv")[0], myOptions);//Bing/Google maps


Can I change Google Apps Script sandbox settings to allow an XMLHttpRequest to a non-SSL API?

Question: In a Google Apps Script script, I have a function displaying an HTTP sidebar.
I would like to populate this sidebar with data from an Intranet API, which is not SSL-enabled.
GAS blocks the query, requiring that the content be served over HTTPS.
As there is no real safety concern here since the API is only reachable from the company's intranet, considered safe, is there a way to bypass the built-in security, and allow an AJAX request to a non-safe server?
Comment: `bypass the built-in security` - I hope not!
Comment: obviously not. otherwise, what would be the point of the sandbox?
Answer: You certainly can't.
The IFRAME sandbox mode in Apps Script is based on the HTML5's iframe sandbox wherein the only allowed values are:

allow-forms
allow-modals
allow-orientation-lock
allow-pointer-lock
allow-popups
allow-popups-to-escape-sandbox
allow-presentation
allow-same-origin
allow-scripts
allow-top-navigation
allow-top-navigation-by-user-activation


trigonometric presentation inside a diagonal matrix.

Question: How can I write an example of $2\times2$ matrix where $d_2$ belongs to the first quadrant and $d_1$ belongs to fourth quadrant, that has this form:
$D = \pmatrix{d_1&0\\0&d_2},$
I wrote something like this, and for some reason it is wrong:
$D =\pmatrix{rcis(\frac74)\pi&0\\0&rcis(\frac13)\pi}$
the university's site says that my choice of d1 is wrong. but why ?
Answer: The first quadrant is the set of angles between $0$ and $\pi/2$.  The fourth quadrant is the set of angles between $3 \pi/2$ and $2 \pi$.  If you switch the values of $d_1$ and $d_2$, you should have a valid solution.
Comment: why? but I chose  $\pi/3$ for first quadrant and $7\pi/4$ for forth quadrant and they are inside the ranges that u wrote.
Comment: Oh, you're right. It's strange that you mentioned $d_2$ first.
Comment: Did you set a value for $r$ (such as $r = 1$), or did you write it in as "r"?


What hook can I use to modify Views Datasource JSON output?

Question: I'm using Views and Views Datasource to generate a JSON feed for my site. I've got a Video field which has both a video and a thumbnail image.
I want to include the url of the thumbnail image in my json feed. I add the Video field to my list of fields in the View and I choose the "Video thumbnail" format instead of the "Video Player" format so that it will show the thumbnail instead of the video. When I do this in a normal View it works perfectly. However, when I do it with a JSON document view, it doesn't work properly. It renders the url to the video instead of rendering the url to the thumbnail image.
I'm going to create another question which talks about how to get this to work properly, but THIS question is my backup solution.
I can rewrite the field so that it includes the fid of the thumbnail image, so my current idea is implementing a hook that will allow me to post process the rendered json or manipulate the data structure before it gets converted to json so that I can just lookup the image by its fid and then replace that piece of data in the array with the proper image url.
I have tried MANY hooks so far, but none of them provide data which represents the json data.

hook_views_pre_view
hook_views_pre_render
hook_views_post_render
hook_views_post_execute

My guess is that Views Datasource is processing the data after I've seen it in my hook implementations.
What hook can I use so manipulate the data structure before it gets converted to json or alternatively, what hook can I use to manipulate the json string after it has been converted to json?
Answer: I find my 1st answer informative regarding auxillery information regarding views and views_datasource. So here's 1 way I think you get the url you want.
You said:

My guess is that Views Datasource is processing the data after I've
  seen it in my hook implementations.

It is, look at what views_json_view_pre_render() is doing:
function views_json_views_pre_render(&$view) {
  if(isset($view->plugin_name) && $view->plugin_name == 'views_json') {
    // Support for Video field.
    if(!empty($view->result)) {
      // Process each View result.
      foreach($view->result as $row => $result) {
        // Only process the entity fields defined by the View.
        foreach($view->field as $field_name => $field) {
          if($field instanceof views_handler_field_field) {
            if($field->field_info['type'] == 'video') {

              // Get the Video URL.
              $video = $field->get_value($view->result[$row]);
              // -- See this line !!
              $url = file_create_url($video[0]['uri']);
              $render_array = array(
                '#type' => 'markup',
                '#markup' => filter_xss($url),
              );
              // Substitute embed code with URL. @todo Add support for escaped embed codes.
              // See this line !!
              $view->result[$row]->{'field_'. $field_name}[0]['rendered'] = $render_array;
            }

If its a video field views_json is taking each of the results and replacing the embed code with the URL of the video. This is what  you're seeing.
So how do you fix this. You write your own hook_views_pre_render() that runs before views_json's pre render. In your pre render function you change $video[0]['uri'] to whatever url you want -- ultimately thats the value views_json will use. If you can get the FID or the full url to the image here you should be able to easily set that value.
So in your own module you'd write 2 functions:
// File: mymodule.install
function MYMODULE_install() {
// Set the weight of the mymodule.module to a value lower than views_datasource's views_json module.
  db_update('system')
   ->fields(array('weight' => -1))
   ->condition('name', 'mymodule')
   ->execute();
}

// File: mymodule.module
/**
 * Implements hook_views_pre_render().
 */
function mymodule_views_pre_render(&$view) {
  if(isset($view->plugin_name) && $view->plugin_name == 'views_json' && $view->name == 'YOUR_SPECIFIC_VIEW') {
    // Support for Video field.
    if(!empty($view->result)) {
      // Process each View result.
      foreach($view->result as $row => $result) {
        // Only process the entity fields defined by the View.
        foreach($view->field as $field_name => $field) {
          if($field instanceof views_handler_field_field) {
            if($field->field_info['type'] == 'video') {
              // Change the video uri to the image you want ...
              $view->result[$row][0]['uri'] = /* some logic here */;
            }
          }
        }
      }
    }
  }
}

Your pre render hook will run before views_datasource's. It simply alters the url to whatever you want. Then views_datasource's views_json hook runs and just uses it as its supposed to -- you just slipped in the correct value. This should work. Code is untested.
Comment: I'll test this out tonight.
Answer: See this issue queue. Currently (some people) think field formatting in Views is brittle. It will change in Drupal8 it looks like.
I haven't done this but this very complete issue post discusses how to alter a view field currently:

Currently Views Datasource is hardcoded to use the the default Field
  API formatters that come with each field. I've spent enough time
  researching this issue and talking to the likes of @merlinofchaos (he
  must hate me at this point) to conclude that the problem is not in
  Core or Views itself. Both provide the appropriate infrastructure to
  completely customize the Field API rendering experience from within
  Views. The way that Views allows us to customize the field rendering
  experience is via the use of Views Plugins.
The problem with Views Datasource is that it implements Views plugins
  strictly via OOP inheritance. While that is perfectly O.K. if no one
  ever wished to override this module's behavior, it doesn't work once
  you need to say something like: "render me all the fields, regardless
  if they are empty or not", or things like "allow HTML in the title
  field, because that's what my manager requested". To further
  complicate matters, if you implement your own Views plugin, there's no
  clear way of telling Views Datasource: "hey, use my Views plugin!". In
  other words, without extensive patching or forking, there seems to be
  no elegant way to tame Views Datasource.

TLTR: make your own field formatter that outputs the explicit URL you want. The player stuff you describes probably looks at the view mode destination (the HTML destination, page vs feed vs json from Media) and is altering the output someway as you describe.
Comment: If Views Datasource is always going to use the default formatter, then how will creating my own formatter help?
Comment: Also, I appreciate the info about the bug in Views Datasource. If you post that answer on my other question, I'll accept it as the answer to that one. This question, however, is about what hook I can use to fix the problem given that Views Datasource isn't going to work properly.


What is the intersection of all Sobolev spaces of square integrable functions?

Question: Let $U \subset \mathbb{R}^n$. If
$$H^k=\{f: U \rightarrow \mathbb{R}: D^\alpha f \in L^2(U)\ \forall \alpha \in \mathbb{N}^n \ \text{with} \ \vert \alpha \vert \leq k \}$$
then how do I show that
$$\cap _{k \geq 2} H^k \subset C^\infty (U)$$
Is there a dircet approach that does not go through Morrey's and the Gagliardo-Nirenberg-Sobolev inequality?
Comment: Is the set $U$ a bounded Lipschitz open set or can it be anything in $\mathbb{R}^{n}$ ?
Comment: Does it work for any open set? I actually only need open balls.
Answer: Yes, but you have to define the Hilbert-Sobolev spaces $H^s(\mathbb{R}^n)$ with $s \in \mathbb{R}$, and this is a corollary to the continuous inclusion theorem Sobolev, which you can view here:
If $u \in H^s(\mathbb{R}^n)$ for $s > n/2$, then $u \in L^\infty(\mathbb{R}^n)$?
and $\bigcap_{s \in \mathbb{R}} H^s(\mathbb{R}^n) \subset C^k(\mathbb{R}^n)$.
In particular, if $s=m \in \mathbb{N}$, then $H^s(\mathbb{R}^n)=H^m(\mathbb{R}^n)$ with equivalent norms,
and it can be shown that, if $m-k > n/2$, then $H^m(\Omega) \hookrightarrow C^k(\Omega)$, and also $\bigcap_{m \in \mathbb{N}} H^m(\Omega) \subset C^k(\Omega)$, with $\Omega \subset \mathbb{R}^n$ open.
Comment: Sincerely other roads does not know them. But the regularity of PDE believe that studying these spaces is almost essential
Comment: Where can I see the proof of your last assertion, please?
Comment: @DaniloGregorin Y. Egorov e B.W. Schulze, Pseudo-differential Operators, Singularities, Applications. Birkhauser, 1996.


Filtrar arquivo específico para não excluir

Question: Como fazer o código não apagar um arquivo específico que eu quero entre os  .exe que eu pego. Exemplo: quero que ele não apague "teste.exe", mas que ainda continue apagando todo o resto.
Segue o código:
string[] arquivos = Directory.GetFiles(@"C:\temp\ae", "*.exe", SearchOption.TopDirectoryOnly);
foreach (string arquivo in arquivos)
{
    //nome = Path.GetFileName(arquivo);
    File.Delete(arquivo);
}

Answer: foreach (var arquivo in Directory.GetFiles(@"C:\temp\ae", "*.exe", SearchOption.TopDirectoryOnly) {
    if (Path.GetFileName(arquivo) != "teste.exe") File.Delete(arquivo);
}

Se preferir pode fazer com LINQ, eu faria, porque é mais performático neste caso. Fazendo certo apesar de ter um pouco de overhead pela infraestrutura do LINQ ele faz apenas um loop. Na resposta do Barbetta ele faz 2 loops, um pra pegar os arquivos e outro para filtrar. O Enumerate não executa loop algum.
foreach (var arquivo in Directory.EnumerateFiles(@"C:\temp\ae", "*.exe", SearchOption.TopDirectoryOnly).Where(f => Path.GetFileName(f) != "teste.exe") File.Delete(arquivo);

Coloquei no GitHub para referência futura.
Comment: i) As vezes queria ter a opção de +5 para suas respostas; ii) As vezes ficamos preocupados em fazer um código mais legível(digo isso no sentido de passo a passo) que acabamos dando voltas e voltas e "gastando" mais do que deveria.
Comment: Só não te dei +1 pq a primeira é consideravelmente menos eficiente que o segundo, e pode induzir alguém ao erro
Answer: Você pode usar um condicional dentro do próprio foreach, assim:
    string[] arquivos = Directory.GetFiles(@"C:\temp\ae", "*.exe", SearchOption.TopDirectoryOnly);
    foreach (string arquivo in arquivos)
    {
        var nome = Path.GetFileName(arquivo);
        if (nome != "teste.exe")
            File.Delete(arquivo);
    }

Answer: Segue mais uma opção utilizando linq
string[] arquivos = Directory.GetFiles(@"C:\temp\ae", "*.exe", SearchOption.TopDirectoryOnly).Where(p => p != "teste.exe").ToArray();
foreach (string arquivo in arquivos)
    File.Delete(arquivo);


Python3 user input [Recommended Way]

Question: When using input() for user input in python3, pylint3 throws the warning of using builtin function - [bad-builtin].
When I searched online for this, I found that functions like map and filter
are in this category because there is a better, recommended way (For comprehensions) for achieving the same results.
So what is the recommended way for this???
PS:
Why doesn't this appear bad to pylint??
mes = '\n'.join(iter(input, "###"))

Answer: I don't get any error about input() (and it's indeed the right way) with an up-to-date pylint (1.5.5).
As for your second example, what would you expect it to complain about?

Python3 user input [Recommended Way]


COM type library and Interface exposure

Question: When you have a COM client that exposes an Interface for an out of proc COM server to access, do you have to register the type library or .idl file with the system for the server to be able to access the interface?
I'm not sure if I can generate a proxy/stub DLL from this client process to register with the system.
Does it matter in what type of Apartment Threading the object implementing the interface is created or is this marshalling taken care of by the COM runtime?
I am not sure if my client is MTA or STA.
Comment: Why are you not sure about proxy/stub generation?  It is the obvious approach.
Comment: Yes, how do I generate the proxy/stub for the client.exe?
Comment: You generate proxy/stub for an out-proc server exactly the same way as you do it for an in-proc server. Ask MIDL to generate proxy/stub files, then compile and link them into a library. What's the problem here?
Comment: Can you tell me how I generate the proxy/Stub using MIDL?  I have never needed to do it before.
Answer: Your client and server are in separate processes, so you will need marshalling anyway - threading models won't affect this. You can use any kind of marshalling suitable - typelib (automation) marshalling, proxy/stub marshalling, whatever else - this is up to you.

COM type library and Interface exposure


Artifactory - partial repository sync

Question: I have two instances of artifactory on two separate networks. The two are not connected at all, and only one has an internet connection.
I would like to sync one repository with the other. However, this occurs manually and if I have to transfer entire repositories every time it will be extremely slow. Is there a way to partially export a repository based on date or something like that? The only clue I have so far is that I could access the new artifacts via artifactory's rest api.
Thanks.
Answer: The "Artifacts Created in Date Range" REST API query was actually introduced to solve the issue you're facing.
Creating a scheduled task that executes this query and downloads the artifacts provided in the result set is probably your best bet.
Comment: I'm looking in to this. It seems like almost everything will have to be done externally, such as:
- Querying for the new artifacts
- Downloading the artifacts
- Pushing the artifacts to the destination repositories

Artifactory - partial repository sync


How can I delete lines containing only spaces in python?

Question: I'm developing a simple tool that allows to extract relevant data from HTML files and write them in TXT files. So far, I've achieved most of what I had in mind, but the final result is still unusable because there are (lots of) lines consisting of only white spaces that keep getting transcribed into the final TXT files. I'll attach a picture of how one of the TXTs is looking like as of right now:
Ideally, I'd want all lines containing text to be consecutive. How do I ignore all the lines containing ONLY spaces (.i.e. containing no alphanumeric character) when reading the HTML file once I got rid of the etiquettes? (the spaces are the remainder after deleting everything in between "<" and ">" for the TXTs)
Comment: hint: `str.strip()` will strip whitespace from the string. You can then check if a string is an empty string.
Comment: Loop over the lines and add `if not line.strip(): continue` or something similar.
Answer: You should post some code, for instance of how you write your TXT file.
Anyway, if you use lines, you can simply have a condition:
if len(line.strip()) > 0:
     f.write(line)

Comment: Works even without `len(` and `) > 0`.
Answer: Use str.strip to get rid of the spaces, then you can use filter to remove the (then empty) lines:
example = """

AAA

f
ffifljlsehfshogfse

hello
"""

def remove_blank_lines(s):
    lines = s.split("\n")
    lines = filter(None, lines)
    return "\n".join(lines)

# Or as a one-liner:
# remove_blank_lines = lambda s: "\n".join(filter(None, s.split("\n")))

print(remove_blank_lines(example))

Answer: with open("<file_name>.txt") as f:
    data = list(filter(lambda x: x.strip(), f.readlines()))


Probability of Sum being $4$ with unfair dice

Question: You roll two six-sided dice.  Die $1$ is fair.  Die $2$ is unfair such that the probability of rolling an odd number is $2/3$ and the probability of rolling an even number is $1/3$, though the probability rolling of each odd number is the same, and the probability of rolling each even number is the same.  What is the probability of the sum of both dice adding up to $4$?
My answer:
I wrote out the combinations to sum $4$ between both dies:
(F1, U3), (F2, U2), (F3, U1) [F = Fair die, U = Unfair die].
My thinking is we need to find the OR probability of these pairs:
P((F1 and U3) or (F2 and U2) or (F3 and U1)):
$$(\frac16 \times \frac23) + (\frac16 \times \frac12) + (\frac16 \times \frac23) = 0.30555555555.$$
Is this correct?
Comment: The $\frac23$ probability for U3 is an obvious conceptual error, but $\frac12$ for U2? It looks like you were not careful with your calculations. That will get you wrong answers even when you have the right idea.
Answer: There is a mistake. For the unfair die, the probability of an odd number is $\frac{2}{3}$ and of an even number is $\frac{1}{3}$. It also says that the probability of each odd number is same and of each even number is same.
So for the unfair die, probability of each odd number is $\frac{2}{9}$ and of each even number is $\frac{1}{9}$.
So the desired probability should be $ = \displaystyle \small 2\cdot \frac{1}{6} \cdot \frac{2}{9} + \frac{1}{6} \cdot \frac{1}{9} = \frac{5}{54}$
Answer: No.
With the fair die, the chance of $1,2,3$ is $(1/6), (1/6), (1/6)$, respectively.
With the unfair die, the chance of $1,2,3$ is $(2/9), (1/9), (2/9)$, respectively.
Roll of 4 by 1-3 or 2-2 or 3-1.  Chances are
$(2/54) + (1/54) + (2/54) = (5/54).$
Answer: The computations of @user2661923 and @mathlover are confirmed (+1 each) by a simulation in R. With a million rolls of the two dice, one can expect two place accuracy.
<code>set.seed(313)
d1 = sample(1:6, 10^6, rep=T)
d2 = sample(1:6, 10^6, rep=T, p = c(2,1,2,1,2,1))
s = d1+d2
mean(s == 4)
[1] 0.092494     # aprx P(S = 4)
5/54
[1] 0.09259259   # exact

cutp = (1:12)+.5
hist(s, prob=T, br=cutp, col="skyblue2",
     main="Two Dice: One Fair")
</code>