Load Symfony Project in a directory with Apache

Question: I have a Symfony PHP Application I want to run in a Windows server with Apache 2.4
Its files are stored in D:\Apache24\htdocs\project1\public
Its entry point is D:\Apache24\htdocs\project1\public\index.php
I followed instructions available at https://symfony.com/doc/current/setup/web_server_configuration.html and added this to my \Apache24\conf\extra\httpd-vhosts.conf:
<VirtualHost *:80>
    ServerName localhost
    ServerAlias localhost

    DocumentRoot /Apache24/htdocs/project1/public
    <Directory /Apache24/htdocs/project1/public>
        AllowOverride All
        Require all granted
    </Directory>

    # uncomment the following lines if you install assets as symlinks
    # or run into problems when compiling LESS/Sass/CoffeeScript assets
    # <Directory /var/www/project>
    #     Options FollowSymlinks
    # </Directory>

    ErrorLog /Apache24/logs/project1_error.log
    CustomLog /Apache24/logs/project1_access.log combined
</VirtualHost>

But then my project takes over the server and loads at the root, and I don't want this.
I want this project's app to be served as http://localhost/project1
How can I achieve that and comply with Symfony's routing needs?
This is my http -S output:
Included configuration files:
  (*) D:/Apache24/conf/httpd.conf
    (514) D:/Apache24/conf/extra/httpd-vhosts.conf
VirtualHost configuration:
*:80                   localhost (D:/Apache24/conf/extra/httpd-vhosts.conf:41)
ServerRoot: "D:/Apache24"
Main DocumentRoot: "D:/Apache24/htdocs"
Main ErrorLog: "D:/Apache24/logs/error.log"
Mutex default: dir="D:/Apache24/logs/" mechanism=default
PidFile: "D:/Apache24/logs/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
Define: DUMP_MODULES
Define: DUMP_INCLUDES
Define: SRVROOT=D:\Apache24
Loaded Modules:
 core_module (static)
 win32_module (static)
 mpm_winnt_module (static)
 http_module (static)
 so_module (static)
 actions_module (shared)
 alias_module (shared)
 allowmethods_module (shared)
 asis_module (shared)
 auth_basic_module (shared)
 authn_core_module (shared)
 authn_file_module (shared)
 authz_core_module (shared)
 authz_groupfile_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 cgi_module (shared)
 dir_module (shared)
 env_module (shared)
 include_module (shared)
 isapi_module (shared)
 log_config_module (shared)
 mime_module (shared)
 negotiation_module (shared)
 setenvif_module (shared)
 php7_module (shared)

Answer: You need to use the following in your vhost config:
Alias /project1 /Apache24/htdocs/project1/public
More info is available at:
https://httpd.apache.org/docs/2.4/mod/mod_alias.html
An alternative would be to use a custom domain for your site that's hosted on your localhost by adding a domain like project1.local, to your local hosts file.
(You will need admin to modify this file)
c:\windows\system32\drivers\etc\hosts
or on *nix (Mac) it's /etc/hosts
The addition would look something like the following, but don't remove anything you didn't already add:
127.0.0.1       project1.local www.project1.local
I prefer the second approach for development, as I can have my local website hostname similar to my production one. It also makes transitioning from dev to prod environments smoother as the application is only dependent on a different hostname, versus a hostname and a path.


Accessing State's value outside of being installed on a View. This will result in a constant Binding of the initial value and will not update

Question: I tried to encapsulate the gesture in a class and then call it in another view.
My project can run well, and it can be built well. but Xcode12 give me a pink error. This is my code
<code>class GestureClass: ObservableObject {
    private var minZoom:CGFloat=1
    private var maxZoom:CGFloat=4.00

    @GestureState private var magnificationLevel:CGFloat=1
    @Published public var zoomLevel:CGFloat=1
    @Published public var current:CGFloat=1

    var magnify: some Gesture {
        MagnificationGesture()
        .updating($magnificationLevel, body:{(value,state,_) in
           return state=value
        })
        .onChanged({ value in
            self.current = value
        })
        .onEnded({ value in
            self.zoomLevel = self.setZoom(value)
            self.current = 1
        })
    }

    func setZoom(_ gesturevalue:CGFloat) -> CGFloat {
        return max(min(self.zoomLevel*gesturevalue, self.maxZoom), self.minZoom)
    }

    func gestureresult() -> CGFloat {
        return self.setZoom(self.current)
    }
}

struct GestureModelView: View {
    @EnvironmentObject var gesturemodel:GestureClass
    var body: some View {
        let myges = gesturemodel.magnify

        HStack {
            Rectangle()
                .foregroundColor(.blue)
                .frame(width:200*gesturemodel.gestureresult(), height:200)
                .gesture(myges)
        }
    }
}
</code>
Answer: I deleted this code in the computed property, and the error disappeared. But i really don't know why。
<code>.updating($magnificationLevel, body:{(value,state,_) in
           return state=value
        })
</code>


Instalando Qt no Visual Studio

Question: Gostaria de saber como instalar o QT no visual studio 2015, fiz de tudo, porém não consigo utilizar o mesmo. Sempre que dou um #include  por exemplo, fala que não existe o arquivo ou diretório. Já tentei de várias formas.
Qual a melhor forma para se utilizar o QT?
Comment: Espero que na versão seguinte do Qt5.x, já tenham resolvido esse problema de reconhecimento do VS2015!
Answer: Apenas o QT5.6 suporta o VS2015, mas ele ainda esta em versão RC (Release Candidate), pode experimentar, mas versões RC estão sujeitas a BUGs, mesmo estando próximo ao lançamento, segue os downloads:

http://download.qt.io/development_releases/qt/5.6/5.6.0-rc/

Já as versões estáveis do QT5 suportam apenas suporta o VS 2010, VS 2012 e VS 2013 (este ultimo suporta 32bit e 64bit), ou seja provavelmente tentou usar uma destas versões, mas o VS2015 não as reconhece.
Pra instalar o QT com VisualStudio siga estes passos:
Primeiro selecione a versão desejada:

QT para VS2013 64bit: http://download.qt.io/official_releases/qt/5.5/5.5.1/qt-opensource-windows-x86-msvc2013_64-5.5.1.exe
QT para VS2013 32bit: http://download.qt.io/official_releases/qt/5.5/5.5.1/qt-opensource-windows-x86-msvc2013-5.5.1.exe

Depois instale o VisualStudio2013, no caso existe a versão comunitária que é "free":

https://www.visualstudio.com/post-download-vs?sku=community&clcid=0x409

Agora o mais importante (que geralmente esquecemos), instale o VS Add-in pra QT5 (compatível pra qualquer versão do QT5.x.x incluindo o QT5.6-rc):

http://download.qt.io/official_releases/vsaddin/qt-vs-addin-1.2.5.exe

Depois disso você poderá usar o QT pelo VisualStudio diretamente e até importar projetos do .pro e usa em .sln:

Eu pessoalmente prefiro o "QT Creator" pra desenvolver, mas tanto o VS quanto o Qtcreator irão atender as suas necessidades, então não tem uma melhor forma de usar, você escolhe conforme você se sentir mais acostumado.

Instalando Qt no Visual Studio


adding string object to gArray

Question: I want to add some string objects in GArray. But when i read the API manual of
g_array_new(), it seeks the size of the element to be added while creating the array, but
different string objects have different length, so how can i cope with this issue.
Regards,
iSight
Answer: What you need is GPtrArray, not GArray.
Answer: I believe, the size of the element is:
<code>sizeof(gchar*)
</code>
You put into array not the actual string, but the pointer to a string.

adding string object to gArray


Where to put PHP encoding type header?

Question: I am learning about handling UTF8 character sets and the recommendation is to explicitly set the encoding type in the output headers within your PHP script like so:
header('Content-Type: text/html; charset=utf-8');

My question is about where I should place this header. I have a config file that is included into every script and is run first. Should I place it at the top of that file so that this header is included first in every php file?
Will that affect my ability to set other headers, including a header location redirect down the line? Or should I place this just before any html output, such as the rendering of my template file? Ie- can this header be in place before ALL other php processing, and what are the consequences of doing that? Does it affect anything server side, or just the encoding of the output?
Thanks.!
Answer: Although early setting of this header will not affect anything server-side nor other types of headers, your question being asked out of assumption that the only content your code going to output is of html type.
However, general PHP application sometimes outputs content of some other types, like application/pdf, application/excel or whatever.
Thus, sending it just before html output seems to be exactly the right place.
Comment: What if I remove the content-type part and only specify the charset?
Comment: And wouldn't re-setting this header before outputing a zip or CSV file for example, solve this dilemma? I just want to make sure all output is UTF8, by default. Then I can change the encoding when necessary.
Comment: @Sherri re-setting it would be just dirty hack. Why don't you want to set it at the right place - before starting template rendering? If you want to make sure you always send it, set it up in web-server config
Comment: @Col. Shrapnel - yes that's a good point. I was just hoping to set the default encoding in one place, then reset as needed. I don't have a nice place to put it where it would happen before ALL output rendering (some output does not use the template - ex error log page, 3rd party plugins, etc).
Comment: @Col. Shrapnel: Nicely explained, +1.
Comment: @Sherri: You could use output buffering?
Answer: The only constraint you have is not to output anything (print, echo, ...) before you call the header function. Apart from this, you can call that function anywhere you want to and any number of times (you can set other headers afterwards).
Answer: I'm only recommending this as an alternative. But the setting can be fixated in the php.ini using:
default_mimetype = "text/html; charset=UTF-8"

It's less common on shared hosters, where some of the users might depend on L1. But if you use your own server and want to ensure that it's correctly set regardless of code and whichever PHP script is invoked, then this is your option.
Comment: Not sure about specifying charset in `default_mimetype`, but at least by default value of [`default_chartset`](http://php.net/manual/en/ini.core.php#ini.default-charset) is used for the "charset=..." part.
Answer: You can put the header directive anywhere prior to when you start sending page text.  The order relative to other headers is not supposed to matter; however, if you have sent even one character of page text, PHP will give you an error.
Note that "sending page text" can sometimes happen inadvertently due to another error message or due to a space before or after <?php ?> tags, etc.
Answer: As long as it is inside the -tags and BEFORE you create any output (echo, print, print_r and so on - or any HTML BEFORE the -tags).
OKAY METHOD:
<?php
$var = "hello world";
$var2 = "Goodbye world!";

header();

echo $var2;
?>

OKAY METHOD:
<?php
header();
$var = "hello world";
$var2 = "Goodbye world!";

echo $var2;
?>

NOT OKAY METHOD:
<?php
$var = "hello world";
$var2 = "Goodbye world!";

echo $var2;
header();
?>

Hope this helps!

Where to put PHP encoding type header?


Convert incoming mail to plain text

Question: Is it possible to convert incoming emails sent in HTML format to plain-text in Exchange Server 2003?
Alternatively, is it possible to retrieve emails from Exchange Server as plain-text only?
Answer: If your application is getting mail from a specific mailbox using POP3 or IMAP, you can configure the mailbox's POP3/IMAP settings so that Exchange serves plain text only, regardless of the format of messages in the mailbox.
I haven't worked on Exchange 2003 in quite a while, nor do I have access to a 2003 server, so I can't give you the exact place to look, but if you view the mailbox properties using AD Users & Computers on the Exchange server, it shouldn't be too hard to spot.
Update: after a bit of searching, I managed to find the location of the setting I was thinking of.
On your Exchange Server, open AD Users & Computers, it should be under Start/All Programs/Microsoft Exchange.

Find the relevant mailbox used by your application and open it's properties page.
Select the "Exchange Features" tab.
On the tab you should see a "Protocols" section (example).
If you click on IMAP4 and then Properties, you should be able to force plain text there.

I have done this in the the past with an app that polled a mailbox using POP3 so I know for a fact it works. I'm 99.9% sure the exact same option is available for IMAP4.
Comment: Hi, I am connecting to IMAP and I have tried all the settings possible on the exchange server i.e. only allow plain text. However, It doesn't seem to work I am still getting HTML in the body.
Comment: Ah ok I was doing this settings from the Exchange System Manager. I will give it a try, thanks!
Comment: Hi I tried this and the body is still in HTML. Do I need to reboot the server before the changes take effect?
Comment: I just did some research about that settings and I think that only restricts that account to sending emails as plain text, not converting them as they come in :(
Comment: POP3 and IMAP are not related to sending mail, those settings are definitely for controlling the format/s for receiving mail.
Answer: http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/4b8cb921-cef9-4662-b472-f6fbb141d408/
Comment: Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference.
Answer: I decided to use the HtmlAgilityPack and manually parse the email body. I couldn't seem to find an exhange server solution.

Convert incoming mail to plain text


AWS SNS endpoint to topic subscription or filtering or multiple subscription

Question: This question is related, but I am unable to comment or upvote:
Amazon SNS multiple subscriptions of the same endpoint
Out of the example of the AWS doc (https://docs.aws.amazon.com/sns/latest/dg/message-filtering.html#message-filtering-example-policy-string-logic):
<code>{
    "customer_interests": ["rugby"],
    "price_usd": [{"numeric":[">", 100]}]
}
</code>
I now derrive the following question: How would I set up the filter if an endpoint would like to subscribe to "rugby" with a price over 100 and also to "baseball" with a price below 200?
This overall OR logic is somehow missing in the documentation.
Therefore I thought of subscribing the same endpoint to the topic twice with different filter plicies. One with the filter for "rugby" and price over 100, and one subscription for "baseball" with a price below 200. This was kind of asked in the question of SO (link above).
Comment: Hi, Did you get the answer for this? I also have the same requirement where I've to apply the OR logic between message attributes and subscribe to same endpoint.
Comment: Unfortunately no. I had to implement the logic into my own app. There I stored the ARN. And then sending messages to multiple ARNs.
Answer: In Nov 2023, SNS message filtering introduced support for the 'OR' operator. This new operator should be able to address your use case.
More information: https://aws.amazon.com/about-aws/whats-new/2023/11/amazon-sns-message-additional-filtering-operators/
Comment: Thank you for your upkeep of this issue. Unfortunately I have switched to a new company and do not have access to AWS anymore. So I cannot test this. But it is exactly what I was looking for back in the day. So I will, untested, mark this as the answer.


Combining exposed filter with Facet

Question: This is my big problem, I want to combine a field of free search (text) generated with an exposed filter and facet filter.
The main problem is the follow: if I do a search across exposed filter then facets are update regularly. (as it should be).
If, however, at the first filter the view with facets and then I do a search with the field exposed, the facets are reset.
Is it possible combine facet and exposed filter?
Need some idea pls.
Answer: Better is late than never :) you can use views with AJAX mode and my module ajax_facets
Answer: For Drupal >=9 see https://www.drupal.org/project/facets/issues/3013702#comment-14685036.
Facets 3.x (still dev) will have this functionality built in. It can then also be combined with better_exposed_filters for further customization.

Combining exposed filter with Facet


Inverse Probability Weighting for Binary Outcomes

Question: When using IPW with a binary outcome, the results after IPW are not bound between 0 and 1.
I am using IPW to get doubly robust scores.
$$
\mu + \frac{D_i(y_i - \mu)}{ps_i}
$$
where $\mu$ is the predicted outcome, $D$ is the treatment indicator, $y$ the observed outcome, and $ps$ the propensity score.
What are approaches to bound the outcomes?
Comment: Could you please explain *how* you are using IPW?  What are you estimating and what is your estimator?  Can you give a simple example of how it produces erroneous, strange, or invalid results?
Comment: Looks like you're going for Augmented IPW (AIPW).
First, just verifying that $ps_i=\Pr[A=a_i|X_i]$ (the inverse probability weight) and not $\Pr[A=1|X_i]$ (as these are sometimes get confused). Second, as Noah wrote below, AIPW, just like IPW, is not well-defined at the individual level, but rather only after averaging over the dataset. There do exist other doubly-robust methods that support individual prediction, like TMLE.
Comment: @whuber: the doubly robust score with the ipw gives values outside of 0 and 1 for the individuals that actually received the treatment.
Answer: If you use the Hajek estimator, the most commonly used estimator for IPW, the expected potential outcomes are bounded between 0 and 1 as long as the weights are non-negative, which they will be in most applications. The Hajek estimator of a counterfactual mean is computed as
$$
E[Y^a]=\frac{\sum_{i=1}^n{I(A_i=a)w_i Y_i}}{\sum_{i=1}^n{I(A_i=a)w_i} }
$$
or just the weighted mean of the outcome $Y$ in the treatment group $A=a$. If all the $Y_i$s are 0 or 1 and all the $w_i$s are nonnegative, there is no way for this estimator to yield a weighted mean outside 0 to 1. This is not a property of IPW but of all weighted means. When you use an unadjusted weighted linear regression of the outcome on the treatment, the resulting coefficient on the treatment is equal to the Hajek estimator of the treatment effect.
Things change when your weights are negative, which can be the case when the outcome regression includes other covariates, especially those that are not balanced. See Chattopadhyay and Zubizarreta (2022) for more details on negative weights implied by a linear regression.
Comment: I see that the mean will yield a value between 0 and 1, but the individual expected potential outcomes are not bound as I understand it.
Comment: IPW doesn't estimate the individual potential outcomes. It uses the raw outcomes themselves, which are just 0s and 1s. If you are using a DR method, you can just use a method of estimating the potential outcomes that bounds them between 0 and 1, like logistic regression or extracting the predicted probability from a classification algorithm. That has nothing to do with IPW, though.


Move file to Teams SharePoint directory using Powershell

Question: I'm trying to move a file to a Teams Channel\SharePoint directory using powershell. It appears to me that I only need three lines of code for proof of concept.  I'm using the -UseWebLogin to get my login information
$Output = Get-ChildItem C:\Temp\ToSharepoint\Test.txt
Connect-PnPOnline -Url "https://My365.sharepoint.com/" –UseWebLogin
Add-pnpfile -path $Output -Folder https://My365.sharepoint.com/:f:/s/MyServicesInc/%20ITCommunications/Forms/AllItems.aspx
I've tried numerous strings for the destination but none of them seem to work. I'm currently receiving this error message,
Add-pnpfile : Access denied.
At line:1 char:1

Add-pnpfile -path $Output -Folder  https://My365.sharepoint.com/:f:/ ...

<code>  + CategoryInfo          : WriteError: (:) [Add-PnPFile], ServerUnauthorizedAccessException
  + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.AddFile

</code>

I am able to upload a file manually so I believe my credentials are not the issue but I don't know that for sure.
Any help would be greatly appreciated! - Thanks
Answer: Please use the Url of your teams channel site url under following code:

Connect-PnPOnline -Url
"**https://My365.sharepoint.com/sites/Team1-PrivateChannel38**"
–UseWebLogin

In addition, use a relative folder path like this:

Shared Documents/Private Channel 38/Folder1

Here is my test which works well:
<code>$Output = Get-ChildItem C:\Temp\lulu4.jpg
Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/Team1-PrivateChannel38" –UseWebLogin
Add-pnpfile -path $Output -Folder "Shared Documents/Private Channel 38/Folder1"
</code>
Comment: Hi Julie, thanks so much for taking the time to comment!  The picture is also a great bonus!  I see exactly what you did and I can replicate it but still can't upload the file.  The one difference I noticed is that to the right of Team1 - Private Channel 38 I have the words  "Private Group."  Is there something special I need to do when that's the case? Thanks again!
Comment: If possible, please provide some screenshots and which parameters are different.
Answer: Here's the interface I'm working with and what I'm using.  The only way I could get the picture in was to submit this as an answer, sorry about that. I'm still not able to get it to work.  I am a member of the group rather than the owner and I'm wondering if that is the issue although I can manually upload files and delete file from sharepoint.
$Output = Get-ChildItem C:\Temp\ToSharepoint\Test.txt
Connect-PnPOnline -Url "https://My365.sharepoint.com/sites/UnitedWeStand" –UseWebLogin
Add-pnpfile -path $Output -Folder "Documents/IT Communications"
Thanks again!
My Interface


Difference between .info files hook_menu() file key?

Question: What is the difference between declaring the files[] in a module's .info file and declaring the file inside a hook_menu() item?
According to the Drupal Documentation on .info files...

When a module is enabled, Drupal will rescan all declared files and index all the >classes and interfaces that it finds. Classes will be loaded automatically by PHP >when they are first accessed.

And the documentation on the files key for hook_menu() is...

A file that will be included before the page callback is called; this allows page >callback functions to be in separate files. The file should be relative to the >implementing module's directory unless otherwise specified by the "file path" >option.

This sounds a little redundant to me. Aren't the files being "included" when you declare them in the .info file? So why would i need to list the file location for a function inside hook_menu when it should already be included?
Comment: Do you know difference between interfaces and classes, and functions? If yes, edit your question because it does not make much sense. If no, it is PHP question, not Drupal one.
Answer: I'm sorry you do not understand my question, however, I believe the question was pretty straightforward. After discovering some interesting articles I think worked out my confusion.
The files[] entry in the module .info file is not used for "including" php files. It is meant only to auto load your classes and interfaces for your application (if you're writing your module using object oriented methodologies). It could be thought of as the "import" statement in Java.
The following articles go more into detail about PSR-0 standard and auto loading in php.
previousnext.com.au/blog/drupal-8-now-psr-0-code-drupal-7/
riceball.com/d/book/export/html/1200
That being said, the purpose of the file key inside the hook_menu() $item[] is to specify where the file containing the page_callback function exists. If your page_callback function is defined inside a class (statically), then you do not have to provide the file and filepath keys. The callback would look something like this,
    'page_callback' => array('MyClass::myfunction');
For more information on OO Page callbacks in Drupal 7, read the following article.
previousnext.com.au/blog/object-oriented-page-callbacks-drupal-7
If your page_callback is not inside a class, but just inside a file then you'll need to provide the file and filepath keys so that Drupal can include this file when your page is created.
Comment: I'm not sure if you understood it 100%, because from your comment I understand that if you use class in menu hook it will be autoloaded, but it is not. if you use classes as menu callback you still need to take care how classes are loaded. 3 ways to do that: 1. you give the path where class is in menu callback. 2. you put file path of the class inside .info file. 3. you use xautoload, that modules automatically loads classes which are defined in PSR-0 standard way
Answer: in files from .info it scans for classes and interfaces. When you use class somewhere in code, file is included automatically. You don't need to include it
In hook_menu you define in which file your callback function is.
not sure how is it redundant. how would you be able to switch one with another?
Comment: Two different situations. 1) What if my callback function is inside a class that specified in the .info file? The class has already been added so why would i need to include that file again? 2) What if i don't have any classes and my files are just a bunch of defined functions? Do i not add those to the .info file? If i do add them, is specifying them in hook_menu() now redundant?


Ranking randomly distributed variables based on decreasingness

Question: For some research I am conducting, I have run into the following problem, which I must solve repeatedly:
My data consists of sequences of observations $\{O_1,O_2,\ldots,O_n\}$, each with a different normally distributed error.  The sequences are variable in length, ranging from 5 to 44 observations.
I need a ranking system that expresses how much a given sequence of observations is decreasing.

Here's what I've come up with so far:
Since everything is normally distributed, it's easy to compute $P(O_1 > \cdots > O_n )$ or $\prod_{i=1}^{n-1}P(O_i > O_{i+1})$ via Monte Carlo.  So my first naïve way of tackling this problem is to try these two scores.
There are problems with this approach. In my data, the observations may correspond to an exponentially decreasing quantity, or follow a power law. In these asymptotically convergent cases, for large $n$ these scores could be very low.  What's more, a short sequence with wide error bars could easily get a higher score.
So a good ranking system should punish short, ambiguous sequences and forgive long, asymptotically convergent sequences.
Thinking of the ambiguous case, one can show that if all of the observation are i.i.d, then regardless of the distribution:

$\displaystyle P(O_1 > O_2 > \cdots > O_n) = \frac{1}{n!}$ and
$\displaystyle\prod_{i=1}^{n-1}P(O_i > O_{i+1}) = \frac{1}{2^{n-1}}$

From these observations, I was inspired to come up with the following rankings:

$P(O_1 > \cdots > O_n ) \times {n!}$
$\displaystyle\prod_{i=1}^{n-1}P(O_i > O_{i+1}) \times 2^{n-1}$

I can't say anything about the statistical properties of these rankings, as they are my own invention and I can't find any papers on this problem.  Also, I have yet to implement anything, since I only encountered this problem very recently.  I will update this question with my developments.
Input and criticisms are welcome!  Thanks everyone in advance!
Comment: The results you give for the iid case hold for any continuous distribution. Furthermore, the sequence of events $A_i := \cap_{j < i} \{O_i < O_j\}$ corresponding to whether the $i$th observation is a *record* are, in fact, independent. This is a famous result due to Renyi. There is also some recent work on statistical properties of rankings. One example, which isn't immediately related to your problem, but has some vague similarities is P. Hall and H. Miller (2010), [Modeling the variability of rankings](http://projecteuclid.org/euclid.aos/1278861456), *Ann. Stat.*, vol. 38, no. 5, 2652-2677.
Answer: Instead of approximating a really small probability, then rescaling it by something huge (like $44! \approx 2.7 \times 10^54$) you would do better to compute integer-valued statistics such as the count of values $i$ so that $O_i \gt O_{i+1}$. This will be a more efficient use of any MonteCarlo runs you perform.
One idea which has been studied a lot by mathematicians is the number of inversions, pairs $i \lt j$ where $O_i \gt O_j$. For a uniformly random permutation, the number of inversions is a sum of independent discrete uniform distributions
$$ \sum_{i=1}^n U[\lbrace 0, 1, ..., i-1 \rbrace] $$
and as $n \to \infty$ this is asymptotically normal with mean ${n \choose 2}/2$ and standard deviation $\sqrt{(2n^3 + 3n^2-5n)/72} = n^{3/2}/6 + O(n).$
I don't know of a closed form expression for the number of permutations of $n$ with exactly/at least $i$ inversions but there are recursions which are fast enough for much larger $n$ than you are using. Let $I(n,i)$ be the number of permutations of $n$ with exactly $i$ inversions.  Then
$$ I(n,i) = \sum_{j=0}^{i-1} I(n-1,i-j)$$
by conditioning on the last uniform random variable above.
Comment: Thanks Douglas!  Thanks for your remark regarding the use integer-valued statistics.  I'll throw out the first ranking, but the second ranking algorithm I came up with, $\displaystyle\prod_{i=1}^{n-1}P(O_i > O_{i+1}) \times 2^{n-1}$, can be refactored as $\displaystyle\prod_{i=1}^{n-1} 2 \times P(O_i > O_{i+1})$.  This can be reduced to counting $i$ such that $O_i > O_{i+1}$ as per your suggestion.  I'm still thinking of how to make use of your inversion insight to produce a ranking.


Device ID & Platform Not Retrieving Amazon Fire OS

Question: Is there any issue when trying to retrieve the device ID or platform from a Amazon fire OS device? it's working fine for iOS and Android but does not get anything when testing on a Amazon device.
I am using http://ngcordova.com/docs/plugins/device/
Answer: The Cordova Device plugin doesn't support getting UUID nor platform from an Amazon Fire OS device. According to this page these are supported platforms:

Android
BlackBerry 10
iOS
Tizen
Windows Phone 7 and 8
Windows 8


SpringBoot 404 on ModelAndView

Question: I have a controller like this:
@RestController
public class SecureController {

    private static final Logger logger = LoggerFactory.getLogger(SecureController.class);

    @RequestMapping(value={"/secure"},
                    method=RequestMethod.GET)
    public ResponseEntity<?> getSecure() {
        String str = "Security Area? -- GET";

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            String currentUserName = authentication.getName();
            str = str + " -- " + currentUserName + "\n\n";
        }

        return new ResponseEntity<>(str, HttpStatus.OK);
    }

    @RequestMapping(value={"/secure"},
                    method=RequestMethod.POST)
    public ResponseEntity<?> postSecure() {
        String str = "Security Area? -- POST";

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            String currentUserName = authentication.getName();
            str = str + " -- " + currentUserName + "\n\n";
        }

        return new ResponseEntity<>(str, HttpStatus.OK);
    }

    @RequestMapping(value={"/secure/sub"},
                    method=RequestMethod.GET)
    public ResponseEntity<?> getSecureSub() {
        String str = "Security area subpage -- GET";

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            String currentUserName = authentication.getName();
            str = str + " -- " + currentUserName + "\n\n";
        }

        return new ResponseEntity<>(str, HttpStatus.OK);
    }

    @RequestMapping(value={"/secure/list"}, method=RequestMethod.GET)
    public ModelAndView getSecureList() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("listpage.html");
        return modelAndView;
    }

    @RequestMapping(value={"/login"}, method=RequestMethod.GET)
    public ModelAndView getLogin(@RequestParam(value="logout", required=false) String logout,
                                 @RequestParam(value="error", required=false) String error) {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("loginpage.html");
        return modelAndView;
    }
}

There's a WebSecurity thing like:
@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf().disable()
        .authorizeRequests()
        .antMatchers("/secure/**").fullyAuthenticated()
        .antMatchers("/login").fullyAuthenticated()
        .antMatchers(HttpMethod.GET, "/login").permitAll()
        .and()
        .formLogin().loginPage("/login").failureUrl("/login?error").defaultSuccessUrl("/secure/list", true).permitAll()
        .and()
        .logout();
}

Which seems to work.
I was testing some security thing... Anyway, when going to the login page, the loginpage.html is rendered just fine. It POSTs to /login and everything works.
Until you try to go to /secure/list. Then it 404s. The route exists, and the listpage.html exists in the same dir as loginpage.html and I know it finds it because if I change the name to list.html it gives me an exception about circular routes.
Anyway, the /secure/sub route works fine. So based on this it appears that there's no problem with the ModelAndView and there's no problem with the subroutes but for some reason when used together, there's a problem.
Why is it giving me a 404?
Some Logs:
2017-11-09 17:52:49.865 DEBUG 9020 --- [nio-8088-exec-4] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, application/xhtml+xml, image/webp, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*])
2017-11-09 17:52:49.865 DEBUG 9020 --- [nio-8088-exec-4] o.s.w.servlet.view.BeanNameViewResolver  : No matching bean found for view name 'listpage.html'
2017-11-09 17:52:49.866 DEBUG 9020 --- [nio-8088-exec-4] o.s.w.servlet.view.BeanNameViewResolver  : No matching bean found for view name 'listpage.html.html'
2017-11-09 17:52:49.867 DEBUG 9020 --- [nio-8088-exec-4] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.web.servlet.view.JstlView: name 'listpage.html'; URL [listpage.html]] based on requested media type 'text/html'
2017-11-09 17:52:49.868 DEBUG 9020 --- [nio-8088-exec-4] o.s.web.servlet.DispatcherServlet        : Rendering view [org.springframework.web.servlet.view.JstlView: name 'listpage.html'; URL [listpage.html]] in DispatcherServlet with name 'dispatcherServlet'
2017-11-09 17:52:49.868 DEBUG 9020 --- [nio-8088-exec-4] o.s.web.servlet.view.JstlView            : Forwarding to resource [listpage.html] in InternalResourceView 'listpage.html'
2017-11-09 17:52:49.869 DEBUG 9020 --- [nio-8088-exec-4] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/secure/listpage.html]
2017-11-09 17:52:49.870 DEBUG 9020 --- [nio-8088-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /secure/listpage.html
2017-11-09 17:52:49.875 DEBUG 9020 --- [nio-8088-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/secure/listpage.html]
2017-11-09 17:52:49.875 DEBUG 9020 --- [nio-8088-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping  : Matching patterns for request [/secure/listpage.html] are [/**]
2017-11-09 17:52:49.875 DEBUG 9020 --- [nio-8088-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for request [/secure/listpage.html] are {}
2017-11-09 17:52:49.875 DEBUG 9020 --- [nio-8088-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapping [/secure/listpage.html] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@7383eae2]]] and 1 interceptor
2017-11-09 17:52:49.876 DEBUG 9020 --- [nio-8088-exec-4] o.s.web.servlet.DispatcherServlet        : Last-Modified value for[/secure/listpage.html] is: -1
2017-11-09 17:52:49.878 DEBUG 9020 --- [nio-8088-exec-4] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-11-09 17:52:49.878 DEBUG 9020 --- [nio-8088-exec-4] o.s.web.servlet.DispatcherServlet        : Successfully completed request

ls src/main/resources/static/
listpage.html  loginpage.html

Comment: Can you share the directory structure of the application?
Comment: To me it looks like you need to place the listpage.html in a different directory.
Answer: You're defined your controller as @RestController which cause the error when access the secure/list page. My advice was create another controller with @Controller annotations instead of @RestController.
@Controller
public class SecureListController {
    @RequestMapping(value={"/secure/list"}, method=RequestMethod.GET)
    public ModelAndView getSecureList() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("listpage.html");
        return modelAndView;
    }
}

Comment: If that were true then why does the /login path work with ModelAndView?
Comment: Oh it's seems because Spring looking your `listpage.html` on `secure` directory, but your `listpage.html` was located on `src/resources/static`. Then try move your `listpage.html` into `src/resources/static/secure`.
Comment: ... this thing is looking for resources relative to the uri and not just "within the classpath"
Comment: Yes, I've small experience with Spring, but seeing your error message `Looking up handler method for path /secure/listpage.html` and `Did not find handler method for [/secure/listpage.html]` would it mean that you need to put `listpage.html` under `secure` directory?
Answer: For me adding thymeleaf dependancy solved it.
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

method
@GetMapping("item")
public ModelAndView getItems() {
    return new ModelAndView("item");
}

SpringBoot 404 on ModelAndView


php performance check changes var

Question: I have a    question about performance in php.
I have multiple variables of settings which I want to write in session. First I thought it would make sense to check it before if it need the change:
if($settings['search_rule'] != $searchRule)
{
    $_SESSION['search_rule'] = $searchRule;
}

But I am not sure if it would be better just write
$_SESSION['search_rule'] = $searchRule;

So I can skip the IF for less steps in code. I think not that it would make much difference but I want to improve my code.
For example if I have 10 settings and just one changes which one is faster?
Comment: Only use the `if` when you need to know a change is made (i.e. call another function or create a message saying it's been updated), just replacing the value is a lot clearer if that is all you are doing.
Comment: You can answer your own question: You current code says: If the value of `$settings['search_rule']` is **not equal** to `$searchRule`, then write it to the session, else do not modify it. Do you only want to modify the value if it has the specified value or is this just a "If not exists create else update" thing?
Comment: I just want change the vars. My thoughts are about the performance of writing in vars VS an IF. May the writing is slower if just one of 10 changes so an if would be faster in this example. In this code the target is to check if the values changes.
Answer: if it is always will have a value, you can go with the second option.
$_SESSION['search_rule'] = $searchRule;

php performance check changes var


Looping Through Temporal Dataset

Question: I have a point feature class that includes columns with temporal data. How would I go about collecting the mean center of the first 10 years of data, then the first 20, first 30, etc... and exporting the results as points using a Python loop? I would like to measure the population shift of the data over time without have to manually calculate the effect of each decade.
I apologize if this is a naive question as I am just getting into programming.
The link below contains a file geodatabase that includes the feature class I'm working with.
https://drive.google.com/file/d/0Bw8dHpiUsYU7QjlKQnRSaDV5RnM/view?usp=sharing
Answer: I ended up going with something like this.
<code>import os
import arcpy

in_feature = "C:\\temp\\mlb_birth.gdb\\mlb_birthplaces"
out_features = "C:\\temp\\mlb_birth.gdb"

for x in range(10, 140, 10):
    year_range = int(1870 + x)

    year_out_name = os.path.join(out_features, "Years_{0}".format(x))
    mean_out_name = os.path.join(out_features, "Mean_{0}".format(x))

    arcpy.Select_analysis(in_feature, year_out_name, "birthYear <= {0}".format(year_range))

    arcpy.MeanCenter_stats(year_out_name, mean_out_name, "#", "#", "#")
</code>

Looping Through Temporal Dataset


Saving CoreGraphics drawing actions?

Question: Is there some way I can do something like:
@implementation MyView
- (void)drawRect:(CGRect)rect
{
   // Get the thing I'm supposed to draw (CGImageRef, pattern, etc.) and draw it
   // i.e. not real code
   CGContextDrawWhatever(self.objectThatHoldsDrawing.drawing);
}

Answer: Have a look at CGPathRef, CGLayer (or maybe even just CALayer, but that's mostly equivalent to a drawRect: method) and rendering (e.g. buffering) to image context and just renderind the image.
Comment: Thanks for the response, I was thinking of something liek that. Can you give me an example with CGLayer?
Comment: Have a look at http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_layers/dq_layers.html

Saving CoreGraphics drawing actions?


Azure Blob Storage: Filter Event Subscription for the BlobCreated Event by Container Name

Question: I have an Azure storage account with a blob storage which hosts multiple containers. So far, I've successfully created an event which listens to the Microsoft.Storage.BlobCreated event and calls a webhook inside my Web API project.
When the event is being processed in the Web API, we would like to know into which container the file was uploaded to. The current schema does not support this information.
Our intended workaround will be to append the container name into the file name and parse it in the Web API and start the processing logic based on the container name. Is there a better way to append the container name to the BlobCreated Event data?
There seems to be an issue about this on GitHub with no official solution. I'm hoping that there already is a better workaround.
Answer: For now no SDK to get the container trigger. There are some existing issues about function or other services. For now no direct way to implement this.
However from the event response, you could find the subject contains the container details, so you could just use split() method to get it.
For testing purposes, I use the logic app with js. The subject is the subject in the event json data. The split with / and get the container name in the array. In the actual situations, after the container name is the virtual path and suppose this is not what you want so just get the index 4 value is the container name.
Comment: Oh, you are right. The container name is indeed included in the path  I can extract it from there. Thank you!


Access nested class

Question: I want to make my code easier to use and this is what I'm trying.
public class Engine
{
    public class FileMgr
    {

    }

}

I can access this like Engine.FileMgr thats ok but.
I want my classes in seperated files so i want it like this
public class Engine
{
  public static FileMgr fileMgr;

}

public class FileMgr
{

}

But I cant access it anymore like Engine.FileMgr.
Is there any better way to do this?
Comment: Sorry, but how is having to use a class name as if it were part of the namespace supposed to make things easier to read?
Comment: I don't see the point. You can just access it as `FileMgr` now, without the `Engine.` part.
Comment: @Nyerguds OP wants to access fileMgr property of type FileMgr class not the class itself ... read the question. Btw You should be able to call Enging.fileMgr but You need to instantiate it ofc !
Comment: @MajkeloDev that's not the way the question reads to me at all. Time to vote for closing it as the question is too unclear.
Answer: You could make the Engine class partial and put the FileMgr details inside of it.
public partial class Engine
{
    // Engine stuff here.
}

public partial class Engine
{
    public class FileMgr
    {

    }
}

Comment: It simply isn't possible to downvote this hack enough. You have taken an already bad idea (exposing the inner workings of a class through making a subclass public), you then you introduce the complication of an unnecessary partial class. This is bad.
Comment: @DavidArno The OP didn't ask if it was good or bad. The OP asked how to do it. I showed him/her how to do it.
Answer: The parent type Engine is now dropped from the class name of Engine.FileMgr since it is no longer a child class of Engine. That is like it is supposed to be. Simply remove Engine. from the type name and you are set.
If you really really really want to have the class name prefixed, use a partial class of Engine across the two files.
// Engine.cs
public partial class Engine
{
}

// Engine.FileMgr.cs
public partial class Engine
{
    public class FileMgr
    {

    }
}

Comment: because you have written more. ;)
Comment: It simply isn't possible to downvote this hack enough. You have taken an already bad idea (exposing the inner workings of a class through making a subclass public), you then you introduce the complication of an unnecessary partial class. This is bad.
Comment: I didn't propose a hack. Where did I do that? I propose to drop the type name from the class name, making the nested class not nested any more (if that is the intention of OP, it is okay, right?). If it has to remain a nested class there is nothing wrong with splitting some logic over different files, is it?
Answer: Engine.fileMgr is now is a field and no longer a type. SO you should use it like this:
FileMgr fm = Engine.fileMgr;

Access nested class


Inno Setup atalho área de trabalho via código

Question: Eu gostaria de criar um atalho na área de trabalho via código pelo inno setup eu preciso desativar a opção para criar um "exe" na pasta principal e fica desabilitado para por atalho na área de trabalho e preciso de por via código.
Answer: Bom dia,
Utilizando o inno setup eu crio icone de área de trabalho, seguindo como exemplo o código abaixo. No meu caso onde está escrito "Systec Smart" é o nome que eu quero que fique.
<code>[Icons]
;cria no menu iniciar
Name: "{userdesktop}\Systec Smart"; Filename: "{app}\Systec\SystecSmart\{#MyAppExeName}"; IconFilename: "{app}\Systec\SystecSmart\{#MyAppExeName}";         Parameters: "SystecSmart.exe"; Comment: "Systec Smart"
</code>
Espero ter ajudado!


About an uncommon theorem of Morrey

Question: I am searching for a reference with the following result: Let $\Omega \subset R^n$ an open bounded domain with smooth boundary . Let $2\leq p < n$ and $u \in W^{1,p}(\Omega)\cap L^{\infty}(\Omega)$. Suppose that for all ball $B$ $ \subset \Omega $ we have
$$ || \nabla u||_{L^p (B)} \leq M r^{n/p}$$
where $r$ is the radius of $B$ and $M$ is a constant that doesn't depend on $B$. Then $u$ is in some Holder space.
I only know some results of Morrey in the case $n<p$. I never saw the above result and there is a paper that I am reading a paper where the authors use this property without say a reference .
Someone could point me a reference with the result please?
Thanks in advance
Answer: Ok, here is it. We will exactly follow the proof of Morrey's theorem.
I will replace
$$ \|\nabla u\|_{L^p(B)}\leq Mr^{N/p} $$
by
$$ \|\nabla u\|_{L^p(Q)}\leq Mr^{N/p} \tag 1$$
where $Q$ is a cube with length $r$. I do this only intend to match the proof of Morry.
Now, for any $x\in Q$ where $Q$ is a cube with length $r$ and centered at origin, we have
$$ \lvert u(x)-u(0)\rvert\leq \left\lvert\int_0^1 \nabla u(tx)dt\cdot x\right\rvert \leq r\int_0^1\lvert \nabla u(tx)\rvert dx$$
Next, we define
$$ \bar u:=\frac{1}{|Q|}\int_0^1 u(z)\,dz $$
and we have
$$\lvert \bar u-u(0)\rvert\leq \frac{r}{|Q|}\int_Q\int_0^1 \lvert \nabla u(tx)\rvert\,dtdx=\frac{1}{r^{N-1}}\int_0^1\int_Q\lvert \nabla u(tx)\rvert\,dxdt =\frac{1}{r^{N-1}}\int_0^1\int_{tQ}\lvert \nabla u(y)\rvert\,dy\frac{1}{t^N}dt $$
Now calling $(1)$, we have
$$ \int_{tQ}\lvert \nabla u(y)\rvert\,dy\leq |tQ|^{\frac{1}{p'}}\left(\int_{tQ}\lvert \nabla u(y)\rvert^p\,dy \right)^{\frac{1}{p}}\leq |tQ|^{\frac{1}{p'}} (tr)^{\frac{N}{p}}$$
Hence, we have
$$\lvert \bar u-u(0)\rvert\leq C\frac{1}{r^{N-1}}r^{\frac{N}{p'}}r^{\frac{N}{p}}\int_0^1 \frac{1}{t^{N}}t^{\frac{N}{p'}}t^{\frac{N}{p}}dt=C\frac{1}{r^{N-1}}r^{\frac{N}{p'}}r^{\frac{N}{p}}\int_0^1 1dt\leq C'r$$
where $C$ in an constant does not depends on $r$.
The rest will go exactly as the Morrey's proof. I suggest you to read Haïm Brezis' book, Page 282, theorem $9.12$
In the end, we actually conclude that you have Lipschitz continuous function, which is way more strong then Holder continuous.
Comment: nowhere, the inequality $(1)$ is the key. Also, if $p>n$, it is just Morry inequality.
Comment: where you used that $p< n$ ?
Comment: This proof is good because it generalizes to show that $u\in C^{0,\alpha}(\Omega)$ provided that $R^{p-n}\int_{B_R(x)\cap\Omega} |Du|^p\leq CR^{p\alpha}$, which is another version of Morrey's lemma useful in the theory of the minimal surface equation.
Answer: Another way of looking at this is through the Lebesgue Differentiation theorem, that says: If $v\in L^p_{loc}(\Omega)$ then for a.e. $x\in \Omega$ we have
$$
g_v(x,r):= \frac{1}{|B(x,r)|}\int_{B(x,r)} |v|^p dy \to |v(x)|^p \qquad \text{ as } r\to 0.
$$
Your hypothesis implies that $g_{\nabla u} (x,r)\leq M$ and so $|\nabla u(x)|^p \leq M$ for a.e. $x$. Therefore $\nabla u \in L^\infty_{loc}(\Omega)$ and so $u$ is locally Lipschitz continuous (and therefore in every Hölder space of order $\alpha<1$).
Comment: Indeed, this is a better way then mine!

About an uncommon theorem of Morrey


Reconhecimento de placas - OpenCV - Tesseract

Question: Estou fazendo um TCC com o tema de reconhecimento de placas automotivas. Encontrei um problema ao usar a função pytesseract.image_to_string. Essa função deveria converter imagem em string. tentei criar uma traineddata usando JTESSBOXEDITOR e o SERAK TESSERACT TRAINER porem não obtive sucesso, pois as não está saindo as informações certas das placas...
Gostaria de saber se alguém consegue me ajudar com esse problema. Se o problema realmente foi o trinamento ou se pode haver outros problemas.
EDIÇÃO:
O problema não estava 100% no treinamento e sim na qualidade da imagem.
O problema agora se tornou melhorar o tratamento dessa imagem. Essa é a que chega para extrair o texto no imagetostring. Porém, devido aos "ruídos" ela não está pegando a informação.
Se alguém souber como ajudar.
Me indicaram usar projeção de histograma para retirar os pixels indesejados...
Comment: Obrigado por tentar ajudar.
Eu consegui resolver essa etapa.
Basicamente agora precisa melhorar a imagem que chega pra extrair a informação.
Answer: A biblioteca pytesseract é utilizada para o OCR.
O .traineddata foi criado a partir da fonte "Mandatory", que é a utilizadas em placas automotivas e salva com o nome Mandatory.traineddata
Código
try:
    from PIL import Image
except ImportError:
    import Image
import cv2
import pytesseract
import numpy as np
import urllib.request

def show_image(name, img):
    cv2.imshow(name, img)
    cv2.waitKey(0)

resp = urllib.request.urlopen("https://i.stack.imgur.com/davGw.jpg")
img = np.asarray(bytearray(resp.read()), dtype="uint8")
img = cv2.imdecode(img, cv2.IMREAD_COLOR)

copia = img.copy()
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(img_gray,127,255,cv2.THRESH_BINARY)

(_, conts, hierarquia) = cv2.findContours(thresh,cv2.RETR_CCOMP,2)

i=0
placa = ""
for cnt in conts:
    area = cv2.contourArea(cnt)
    x,y,w,h = cv2.boundingRect(cnt)
    if 2000 <= area <= 10000:
        cv2.rectangle(copia,(x,y),(x+w,y+h),(0,0,255),3)
        roi = thresh[y-2:y+h+2, x-2:x+w+2]
        show_image('ROI', roi)
        if i<4:
            txt = pytesseract.image_to_string(Image.fromarray(roi), lang='Mandatory', boxes=False, \
            config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')
            print(txt)
            placa += txt
        else:
            txt = pytesseract.image_to_string(Image.fromarray(roi), lang='Mandatory', boxes=False, \
            config='--psm 10 --oem 3 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVXWYZ')
            print(txt)
            placa += txt
        i+=1

print(placa[::-1])
show_image('Contornos', copia)

Explicação

Carrega a imagem do imgur e converte para o formato padrão da biblioteca OpenCV

resp = urllib.request.urlopen("https://i.stack.imgur.com/davGw.jpg")
img = np.asarray(bytearray(resp.read()), dtype="uint8")
img = cv2.imdecode(img, cv2.IMREAD_COLOR)

Copia a imagem original, converte para Escala de Cinza e depois binariza.

copia = img.copy()
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(img_gray,127,255,cv2.THRESH_BINARY)

Encontra os contornos da Imagem, com a flag de hierarquia RETR_CCOMP

(_, conts, hierarquia) = cv2.findContours(thresh,cv2.RETR_CCOMP,2)

A iteração for em cada contorno encontrado com cv2.findContours()

for cnt in conts:

Encontra a área de cada contorno e as fronteiras em formato de retângulo do contorno, em que são encontradas o ponto inicial (x,y), a altura h e a largura w

area = cv2.contourArea(cnt)
x,y,w,h = cv2.boundingRect(cnt)

Se a área está entre 2000 e 10000 px

if 2000 <= area <= 10000:

A Região de interesse (ROI) é criada em cada contorno com um slicer e é mostrada:

roi = thresh[y-2:y+h+2, x-2:x+w+2]

Como os quatro primeiros contornos são os números, pois os contornos são encontrados da direita para a esquerda, utiliza o Tesseract para dígitos nos quatro primeiros contornos:

    if i<4:
        txt = pytesseract.image_to_string(Image.fromarray(roi), lang='Mandatory', boxes=False, \
        config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')
        print(txt)
        placa += txt

Nos outros, para letras maiúsculas:

    else:
        txt = pytesseract.image_to_string(Image.fromarray(roi), lang='Mandatory', boxes=False, \
        config='--psm 10 --oem 3 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVXWYZ')
        print(txt)
        placa += txt

Configurações de pytesseract.image_to_string():

Carrega a imagem do caractere da região de interesse com Image.fromarray(roi)
A fonte treinada é carregada lang='Mandatory'
onfig='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789'
O psm é 10 para um caractere, pois a lista de psm é a seguinte:
Page segmentation modes:
  0    Orientation and script detection (OSD) only.
  1    Automatic page segmentation with OSD.
  2    Automatic page segmentation, but no OSD, or OCR.
  3    Fully automatic page segmentation, but no OSD. (Default)
  4    Assume a single column of text of variable sizes.
  5    Assume a single uniform block of vertically aligned text.
  6    Assume a single uniform block of text.
  7    Treat the image as a single text line.
  8    Treat the image as a single word.
  9    Treat the image as a single word in a circle.
 10    Treat the image as a single character.
 11    Sparse text. Find as much text as possible in no particular order.
 12    Sparse text with OSD.
 13    Raw line. Treat the image as a single text line,
                        bypassing hacks that are Tesseract-specific.

A de oem é 3, ou seja, a padrão. A lista enum do OCR Engine mode é:
  TesseractOnly           0   Run Tesseract only - fastest
  CubeOnly                1   Run Cube only - better accuracy, but slower
  TesseractCubeCombined   2   Run both and combine results - best accuracy
  Default                 3   Specify this mode to indicate that any of the above modes should be automatically inferred from the variables in the language-specific config, or if not specified in any of the above should be set to the default OEM_TESSERACT_ONLY.

E o whitelist para números ou letras em maiúsculo

Inverte a string placa

print(placa[::-1])
Resultado

Saída:  EZG3164

Obs.: Foi utilizado a versão 3.x de OpenCV, no caso de ser 2.x há algumas incompatibilidades. Uma delas pode ser resolvida lendo esta resposta.


Checking property existence of include fragment in MyBatis

Question: Is there some way to check if a property in include fragment has been set?
Example:
<include refid="myFilterLocation">
  <property name="model" value="model_name"/>
  ...
  ...
  ...
</include>

And then when this fragment is used:
<if test="....">
 AND ${model}= #{${param}.model,jdbcType=VARCHAR}
</if>

Is there some way to test if the property is not sent?. I want not send properties if I do not want to filter in my query.
Kind regards.
Comment: You should avoid injection parameters `${param}` as much as you can. These can be vulnerable to SQL Injection. Use normal parameters `#{param}` by default. Resort to injection parameters only in very special cases.
Comment: param is another include property, its value depends on how I extract a register. Ex: "c.model" where c is the alias for table "car" for example.
Answer: When the property is not defined, ${model} is not replaced at all, so you may be able to write the condition as follows.
<if test='"${model}" != "\${model}"'>
  AND ${model}= #{${param}.model,jdbcType=VARCHAR}
</if>

Note the unusual usage of single/double quotes and the backslash before the second dollar sign.
(Longer explanation)
There are two variables ${model} in the test attribute value, however the second one is escaped using a leading backslash.
So, when the 'model' property is specified, the included if element will look as follows.
<if test='"model_name" != "${model}"'>

The condition is a simple two strings comparison and the result is true.
Note that the backslash used to escape the variable is removed (that's how MyBatis' variable escaping works).
Now, when the 'model' property is not specified, the variable is not replaced, so the if element looks as follows after the inclusion.
<if test='"${model}" != "${model}"'>

As you can see, the two strings are the same and the result of the condition is false.
And OGNL requires double quotes for string literal.
If you use single quotes, NumberFormatException may be thrown (OGNL recognizes it as a char, it seems).
Comment: @quark Added a longer explanation. :)
Comment: ooh, thank you, in fact is very simple, known that "\" scapes.
Comment: I tried this but even though I can get the value of ${model} and use it inside my query, somehow the  doesn't work
Comment: @sikidhart Then it may be a different problem. This Q&A is about `` specified inside ``, not the usual `${}` referencing runtime parameter.
Answer: You can check if a parameter is not null using:
<if test="param != null">
  ...
</if>

Comment: That I want to test is if property named "model" is set. I want to avoid define properties that I do not want to use. That's to say, I wold test something like  but this does not work if property model is not defined.
Comment: @quark I am not sure but I think you cannot identify if the parameter has been set or not in an include (I may be wrong). Since your question is highly specialized I would go to the root team and participate in the mailing list firstname.lastname@example.com. Great minds will answer you there.


OpenGL ES 2.0 - read texture value (like with texelFetch)

Question: I have a look up texture as red channel (R8). I upload it to Pixel Shader and there I want to read pixel value (range 0 - 255) and use value for indexing an array of colors. Texture is non-power of two. If I do
float v = texture2D(s, c).r;
int index = int(v * 255);

results are not always correct. First, there is linear filter pressent, that mess up result (and for NPT texture, only linear filter is supported in ES 2.0) and sometimes also round error occurs (in interpolation of texcoord)
Is there any solution ?
Comment: did you research Uniform Buffer Objects ? not sure if they are in ES2.0
Comment: Afaik they dont exist in gles, in gl they appeared with version 3
Answer: It's not true that only linear filter is supported in ES 2.0 with NPOT textures. Nearest filtering is supported as well. You do however, often have to set CLAMP_TO_EDGE for NPOT. (Newer hardware has no limitations on NPOT at all though)
Set this when creating the texture:
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );

You might also want to declare the precision if you are having rounding errors.
e.g. use 'highp float' instead of 'float'


How can i maintain pagination with filter using tableselect?

Question: I am creating listing of content using tableselect form type like as follow.
function partners_list($form,&$form_state){
        $header = array(
            'name'=>array('data' => t('Name'),'field' => 'ept.name'),
            'location'=>array('data' => t('Location'),'field' => 'ept.location'),
        );

        $form['search_ew_partner'] = array(
            '#type' => 'textfield',
            '#prefix' => '

',
            '#suffix' => '
',
            '#default_value'=> $form_state['values']['search_ew_partner'],
            '#attributes' => array(
                'placeholder' => 'Search by name'
            )
        );

        $form['submit'] = array(
            '#type' => 'submit',
            '#value' => t('Search'),

            '#prefix' => '
',
            '#suffix' => 'Reset

',

            '#attributes' => array(
                'class' => array('btn blueBtn')
            )
        );

        if(empty($form_state['input'])){
            $results = partners_list_submit($form,$form_state);
            $results=$results['filter_result'];
        }else{
            $results=$form_state['filter_result'];
        }

        $options = array();
        if(!empty($results)){
            foreach($results as $result){
                $options[$result->id] = array(
                    'name' => array('data' => array('#type' => 'link', '#title' => $result->name, '#href' => "partner/details/".$result->id)),
                    'location' => ucfirst($result->location),
                );
            }
        }

        $form['ew_partners'] = array(
            '#type' => 'tableselect',
            '#header' => $header,
            '#options' => $options,
            '#empty' => t('No partners.'),
            '#attributes' => array(
                'class' => array('table')
            )
        );

        $form['pager'] = array('#markup' => theme('pager'));
        return $form;
    }

    function partners_list_submit(&$form,&$form_state){
        $form_state['rebuild'] = true;
        $header = array(
            'name'=>array('data' => t('Name'),'field' => 'ept.name'),
            'location'=>array('data' => t('Location'),'field' => 'ept.location'),
        );

        $query = db_select('ew_partners', 'ept');
        if(!empty($form_state['values']['search_ew_partner'])){
            $query->condition('ept.name', '%' . $form_state['values']['search_ew_partner'] . '%', 'LIKE');
        }

        $query = $query->extend('PagerDefault')->extend('TableSort');
        $query->fields('ept',array('id','name','location'));
        $query->limit(2)
            ->orderByHeader($header);

        $results = $query->execute();
        $form_state['filter_result']=$results;
        $form_state['no_cache'] = true;
        return $form_state;
    }

Listing and pagination coming properly when there is no filtration on table. After filtration, pagination is not working.
I think due to form_state values are reset when I clicked on next page.
The page count and the result for first page is coming properly.
How can i maintain pagination with filter using tableselect?
Thank you in advance
Any help appreciated.
Answer: The search form method type is post thats why when you are submitting the search form the form values are sending as post therefore the form values are coming properly for  first page. When your are clicking on second page the form state values are reseting with new form state values as form has default value.
So rather than using post method form I will suggest to use get method type form for search so that the search keyword will always persist in url.
So you have to add
$form['#method'] = 'get';

Access all filtered values using $_GET like
$form['search_ew_partner'] = array(
            '#type' => 'textfield',
            '#prefix' => '

',
            '#suffix' => '
',
            '#default_value'=> $_GET['search_ew_partner'],
            '#attributes' => array(
                'placeholder' => 'Search by name'
            )
        );

And change your code like
if(!empty($_GET['search_ew_partner'])){
            $query->condition('ept.name', '%' . $_GET['search_ew_partner'] . '%', 'LIKE');
        }

Comment: Is it not possible with post form type?


Why does my CAPTCHA code fail?

Question: I created a captcha for a contact form. Everything works fine but whatever the number I enter into the captcha form it always shows invalid captcha
<code><?php
if(isset($_POST['norobot']))
 {
  if(md5($_POST['norobot']) == $_SESSION['randomnr2'])
  {
  echo "Validation Success";
  $_SESSION['name'] = $name ;
  $_SESSION['phone_no'] = $phone;
  $_SESSION['mailid'] = $mailid;
  $_SESSION['msg'] = $msg;
  $_SESSION['category'] = $category;
  header("Location:thankyou.php");
  }
  else
   {
   $Error = 'Invalid CAPTCHA';
   }
 }
}
?>
</code>
can any one say what the solution is?
Comment: At the time the captcha image is displayed, what's the value of $_SESSION['randomnr2'] (and don't assume, get your code to display it along with the captcha). When you come to check the POST, again, dump _SESSION['randomnr2'] and $_POST['norobot'] to get more clues. You've not posted enough code for anyone to be able to help you beyond this, but I would suspect you are regenerating $_SESSION['randomnr2']
Comment: Might just be missing `session_start()`
Answer: How are you storing $_SESSION['randomnr2'] ?  Are you md5()'ing it?
Might help to see all the code.

Why does my CAPTCHA code fail?


Task scheduler not logging failure to operational view in event viewer

Question: The task scheduler below clearly errors but it never shows up under the task scheduler operational view as being failed but completed successfully.  I want to send an email on a failed task scheduler to myself.  I could do this on my old box not this Windows 2012 R2 Server box.

Application: Tournaments.NotificationProcessor.exe Framework Version:
  v4.0.30319 Description: The process was terminated due to an unhandled
  exception. Exception Info: System.NullReferenceException Stack: at
  System.Data.Entity.Core.Common.Internal.Materialization.Coordinator1[[System.__Canon,
  mscorlib, Version=184.108.40.206, Culture=neutral,
  PublicKeyToken=b77a5c561934e089]].ReadNextElement(System.Data.Entity.Core.Common.Internal.Materialization.Shaper)
  at
  System.Data.Entity.Core.Common.Internal.Materialization.Shaper1+SimpleEnumerator[[System.__Canon,
  mscorlib, Version=22.214.171.124, Culture=neutral,
  PublicKeyToken=b77a5c561934e089]].MoveNext() at
  System.Linq.Enumerable.FirstOrDefault[System.__Canon, mscorlib,
  Version=18.104.22.168, Culture=neutral,
  PublicKeyToken=b77a5c561934e089]
  at Tournaments.Data.Repositories.Games.GamesRepository.GetGame(Int32,
  System.Nullable1) at
  Tournaments.NotificationProcessor.Program.Main(System.String[])
Comment: `Application: Tournaments.NotificationProcessor.exe [...] at Tournaments.Data.Repositories.Games.GamesRepository.GetGame(Int32, System.Nullable1) at Tournaments.NotificationProcessor.Program.Main(System.String[])`  You know, for some reason, I suspect this has more to do with a gaming server than a professional systems administration type-of-thing.
Comment: I dont get what you are saying
Answer: There's a difference between the task failing to run and the task itself having an error within the task.  The task scheduler just runs the task, it doesn't monitor errors within the task, only if it can run it and close the task properly.  So the errors you are receiving within your Tournaments.NotificationProcessor.exe application have to notify you within that app itself...the task scheduler won't know that these errors occurred.
Comment: Before I upgraded my server to server 2012 it did do this.  It would notify mean of any .NET exceptions because the task did not finish.


Include content created through InDesign/RoboHelp

Question: Is there a way to display content under a Drupal site/url that is not managed by Drupal?
Specifically, we have a large manual (1700 pages) that is managed with Adobe InDesign which is used to create a printed version, one large PDF version, PDF versions of each chapter, and also an HTML version of the manual via RoboHelp. Because of the printing requirements, this is not content that we plan to migrate into Drupal (but we still need to display it on/through the site).
Example: Here is the page for the manual. You can see links for the Complete PDF and individual PDF chapters (which we would probably manage as Media - TBD). The question revolves around the HTML version of the manual. As you can see, at this point we have pretty much jumped out of the container of the hosting site (SharePoint), and are now displaying just the pure, rendered HTML that was generated out of InDesign/RoboHelp.
The content is updated annually and we have requirements to archive each version so that it is still accessible. Our current process makes it fairly easy because of the way that the /Manuals_HTML1 folder is excluded from SharePoint so we just make a copy of the content and give it a new folder based on the year.
So, it doesn't appear to make sense to include this as actual content in Drupal. It feels like it would be some sort of exclusion within Drupal that essentially sends it outside of Drupal. Maybe a mod_rewrite solution?
Brand new to Drupal and currently in a week-long training. Definitely learning tons about how to handle the rest of the site, but this one is an open item.
Answer: Looks like the correct term is HTML or static microsite. These can be managed via .htaccess as seen in Maintaining a static microsite alongside Drupal's docroot.


Ruby novice: Writing a method to read mulitple arrays from a file?

Question: I have a file with ten separate columns of data separated by spaces. I have written the following code (and it works), but I feel that there is a much cleaner way to do what I am doing here:
#Generate ten separate arrays in which to store the columns
c0 = []; c1 = []; c2 = []; c3 = []; c4 = [];
c5 = []; c6 = []; c7 = []; c8 = []; c9 = [];

#Append each item in each line to its own array
File.open(filename, 'r').each_line do |line|
  line = line.strip.split(' ')
  c0 << line[0]; c1 << line[1]; c2 << line[2]; c3 << line[3]; c4 << line[4];
  c5 << line[5]; c6 << line[6]; c7 << line[7]; c8 << line[8]; c9 << line[9];
end

I tried to write a method to accomplish this task, but I am essentially clueless on where to start.  I imagine that there is a cleaner way to initialize n-number of arrays than how I have done...what is the 'ruby' way of doing that?  Is it possible to do everything I am doing here in a single method that returns 10 arrays?  Help/hints on how to accomplish this would be greatly appreciated.
Answer: Maybe this piece of code helps:
   c = []
   File.open(filename, 'r').each_line do |line|
      line = line.strip.split(' ')
      columns = Hash.new
      index=0
      line.each do |column|
        columns[index] = column
        index+=1
      end
      c.push(columns)
    end

Where each column is a Hash with an index by line part, and all lines are stored in a array
Comment: What is part exactly?  I'm getting an undefined variable error.  Sorry, I'm a ruby and programming noob (and have little experience working with hashes).  Thanks!
Comment: sorry, the line is: columns[index] =column.
Answer: File.open(filename, 'r') do |infile|
  c = infile.lines.map(&:split).transpose
end

Now c is a two-dimensional array where each row is an array representing a column from the original file so c[0] = c0 and so on.
EDIT: This is probably a little dense. Some explanation:
infile.lines is an array where each element is a line from the file.
infile.lines.map(&:split) is short for infile.lines.map { |line| line.split }.
' ' is the default char to split on.
transpose turns the columns into rows.
Comment: I tried this on a few separate files. I got the following error: test.rb:7:in `transpose': element size differs (10 should be 9) (IndexError).  Any ideas?
Comment: test.rb:7:in `transpose': element size differs (0 should be 6) (IndexError)  This is an error that returned on a file with 6 columns.
Comment: Try it without transpose and see what you get. The length of each array should be the same.
Comment: Great!  This worked well.  I imagine that when I generated the data, there may be a line that doesn't have the same number of columns (which leads me to believe I should check my data-generating code!)  This is likely responsible for the transpose error.  I made a test file with the same number of items in each row, and all of the code (transpose included) worked on that.  Thanks!
Comment: Oh, I see what the problem was.  There was a blank line at the bottom of my data file!  (Well, the one with 6 columns anyway).  That error above makes sense now.
Comment: Glad you were able to make it work. Please my edit for more detail on what's happening in there.
Comment: The extra details were very helpful.  Can you use "&:method_name" for any object/method that you want to write shorthand like that?
Comment: I mean, ".map(&:method_name)".  I imagine the method would have to be part of the Array class (or inherit from Array?)
Comment: Notice that I didn't supply any parameters to split. If I were supplying a parameter then I would use the long version with a block: map { |l| l.split(',') }. It's possible to supply parameters in some cases but it becomes unwieldy.


Why did logistic regression fail to model non-linear predictors, but cubic splines didnt?

Question: I am reading Elements of Statistical Learning and was looking at Chapters 4 and 5.
In chapter 4 logistic regression is performed on some 'South African Heart Disease Data'. The final model did not accurately model 2 parameters: systolic blood pressure, and obesity - the Z scores were found to be statistically insignificant (1.023 and -1.187). It was mentioned:
"Systolic blood pressure (sbp) is not significant! Nor
is obesity, and its sign is negative. This confusion is a result of the correlation between the set of predictors. On their own, both sbp and obesity
are significant, and with positive sign. However, in the presence of many other correlated variables, they are no longer needed (and can even get a
negative sign)."
Model reduction was then performed to find a subset of variables but both of these predictors were still excluded.
In chapter 5 natural cubic splines are then used to fit the data. Plots of the splines for systolic blood pressure and obesity reveal they are inherently non-linear. Here are the plots:
The book then states:
" Both sbp and obesity are included in this model, while they were not in the linear model. The figure explains why, since their
contributions are inherently nonlinear."
To me this is suggesting that logistic regression could not accurately model these non-linear predictors. However, just because they are non-linear in the predictors, surely provided their parameters are linear, logistic regression should still be able to model this?
So my question is : Is logistic regression not always able to accurately model non-linear predictors?
Any further clarification of what was going on in this example would also be appreciated.
Comment: I suspect you are misunderstanding what they did. logistic regression models the log odds as a linear function of the inputs.  So you add non linear transformations (eg splines) of the inputs as extra inputs, and then logistic regression will model (logodds) as a nonlinear function of the original inputs. eg adding X^2 as an input as well as the original X
Comment: You can also use splines of covariables as predictors in logistic regression, see [here](https://stats.stackexchange.com/search?q=splines+logist*+regres*+answers%3A1).
Comment: @medium-dimensional I have updated the question text with the full name of the text.
Comment: thank you for both your comments. Yes, it seems i misunderstood the process, but it makes sense now!
Comment: @Sean Would you have any interest in posting a self-answer that summarizes what you learned in order to "close out" this question? Two additional advantages are that doing so forces you to think carefully about what you learned, which might reveal that there are gaps in your knowledge, and the community can respond to your post (comments, downvotes) if you do have a misconception. (If it turns out you got it wrong and get downvoted, you can always delete the post to remove the downvotes.)
Comment: @seanv507 Your comment seems a lot like an answer. Do you want to turn it into one?
Answer: I suspect you are misunderstanding what they did.
Logistic regression models the log odds as a linear function of the inputs.
But by adding nonlinear transformations (eg splines) of the inputs as extra inputs, logistic regression will model the log odds as a nonlinear function of the original inputs. eg adding $X^2$ as an input as well as the original X.


McFadden Pseudo R² in averaged model

Question: McFadden's pseudo-R² is a well-known coefficient of determination. If I am right, it can be calculated by 1-(mod_deviance/null_deviance), where mod_deviance is the deviance value for the fitted model and null_deviance is the deviance for the null model which includes only an intercept as predictor.
Can we calculate McFadden's pseudo-R² for averaged models? This is a non-answered question to me, for example here: Pseudo R-squared of averaged model.
My case is similar - I work in R and use the MuMIn-package. My "averaged model" is obtained from the top-ranked models in the dredge function. This is: I try all possible variable combinations and those models with the best fit are averaged with the function model.avg. The summary of averaged models misses information about the model's deviance. p2R in the pscl-package yields no result. So - no chance to calculate McFadden's pseudo-R²?
A collegue of mine proposed the following. Suppose we have an averaged model with model-averaged coefficients:
Model-averaged coefficients:
(full average)
               Estimate Std. Error Adjusted_SE z_value Pr(>|z|)
(Intercept)     -0.2541     0.9522      0.9538   0.266   0.7899
A              -13.1497     8.6335      8.6969   1.512   0.1305
B               22.0164    10.9753     11.0683   1.989   0.0467 *
C                8.8932    10.3528     10.3964   0.855   0.3923
D                0.1664     1.4000      1.4020   0.119   0.9055

(conditional average)
               Estimate Std. Error Adjusted_SE z_value Pr(>|z|)
(Intercept)     -0.2541     0.9522      0.9538   0.266   0.7899
A              -13.9140     8.2605      8.3305   1.670   0.0949 .
B               22.0164    10.9753     11.0683   1.989   0.0467 *
C               14.8596     9.5094      9.5886   1.550   0.1212
D                0.9010     3.1548      3.1596   0.285   0.7755
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Now, he "builds" a new model by taking the estimates of the conditional average model:
my_new_model <- glmer(presence_absence ~ offset(-13.9140*A)+ offset(22.0164*B)+
    offset(0.9010*D)+ offset(14.8596*C)+ (1|RandomFactor),
    family="binomial", data=my_data, na.action="na.fail")

In the summary of the model, we find the model's deviance:
AIC      BIC     logLik deviance df.resid
140.6    146.7    -68.3    136.6      150

Now, we also calculate a Null-model:
NullModel<-glmer(presence_absence ~ (1|RandomFactor), family = "binomial",
    data=my_data, na.action = "na.fail" )

In the NullModels summary we find the Null deviance:
AIC      BIC   logLik deviance df.resid
198      204      -97      194      150

And finally, McFadden's R²:
1-(136.6/ 194)
[1] 0.2958763

To be honest, this approach seems rather complicated to me; however, it might be useful. I fear that I am not enough a statistician to fully understand what he did there and he couldn't explain it properly to me. I wonder if his approach is valid - if so, why is it not implemented in any function/package (as far as I know...)?
I'd appreciate comments and ideas on this!
Comment: If what you mean by an average model is simply a model with arithmetic average of coefficients of the same parameters among top candidate models, then it is also a another model where deviance can be calculated. The methodology suggested in your question in my opinion is correct. Basically you may pretend that you came up with the average model without actually averaging, but with some other  fitting algorithm and compare it to the null deviance.
Comment: The coefficients are a weighted average, where weights are the IC-weights. The reason the deviance is not given is that there are several ways to calculate the averaged-predicted values, which obviously change the residuals, and hence the deviance.
Comment: Thank you for your comment, @Cagdas Ozgenc. I am not sure wheather the output of `model.avg` in `MuMIn`-package is simply a model with arithmetic average of coefficients. I will try to find out. If the methodology really is correct, then I wonder why it's not possible to obtain the deviance of an averaged model...
Comment: Do you think there is a "preferable" way to calculate the average-predicted values in order to obtain a reasonable deviance-value @Kamil Bartón? As there are several ways, I suppose there is no gold standard - however, perhaps you can indicate a good starting point to calculate an averaged model's deviance. A sensible way to provide - for example - McFadden's pseudo-R² for an averaged model could be to also be transparent about the calculation of the averaged-predicted values, I suppose.
Answer: In this special case, it became a standard procedure to calculate the R² for all models used to build the final average model. In a results table, one can then show the range of these R²-values.

McFadden Pseudo R² in averaged model


Cannot access base class' protected method in assignment operator

Question: I have a rather simple data structure, that is part of an interface. As such, the structure's base class is also an interface that does not define any data.
This structure may have different data implementations, but they all must be assignable from each other. This is simplified MCVE of the problem:
class ThingInterface {
  public:
    // Assignment operator required from the interface to all derived classes
    virtual void operator=(const ThingInterface& other) = 0;
  protected:
    virtual int GetValue() const = 0;
};

class ThingImplementation : public ThingInterface {
  public:
    void operator=(const ThingInterface& other) override { value = other.GetValue(); }
  protected:
    virtual int GetValue() const { return value; }
  private:
    int value;
};

The reason GetValue is protected is that in my actual code, that value describes internal state (Initialized, NotInitialized, Error...). It still needs to be copied though.
MSVC error:
error C2248: 'ThingInterface::GetValue': cannot access protected member declared in class 'ThingInterface'
note: see declaration of 'ThingInterface::GetType'
note: see declaration of 'ThingInterface'

Error seen on Ideone:
prog.cpp: In member function 'virtual void ThingImplementation::operator=(const ThingInterface&)':
prog.cpp:11:83: error: 'virtual int ThingInterface::GetValue() const' is protected within this context
     void operator=(const ThingInterface& other) override { value = other.GetValue(); }
                                                                                ^
prog.cpp:6:17: note: declared protected here
     virtual int GetValue() const = 0;
                 ^~~~~~~~

I was under the implession that it's possible to access base class' protected members. What's going on here?
Comment: You can access your own base class protected members, but not members of other object which has type of base class. That's the short answer
Comment: That private cannot be accessed even by the derived classes, that's all
Comment: Oh, I get it now. I can access `this->protectedMethod` but not `randomPointer->protectedMethod`. I fixed the problem by instead determining the state of the other object based on it's public interface behavior.
Comment: What's the difference between private and protected then?
Comment: @TomášZato its not about the pointer, but the type of the pointer. Protected means that the class who inherits the member will be able to access that inherited member. Here you are trying to explicity access GetValue() on the original ThingInterface. It would work if other was of type ThingImplementation&
Answer: In this snippet:
void operator=(const ThingInterface& other) override { value = other.GetValue(); }

protected only applies to this here. In your case, accessing GetValue() from anywhere but ThingInterface through other is illegal. Clang has a nice error message that might make more sense:

note: can only access this member on an object of type
  'ThingImplementation'
virtual int GetValue() const = 0;
            ^


Prove that parallel projection creates a constant ratio $\vert \text{original}\vert:\vert\text{image}\vert$

Question: Consider the folloing parallel projection:

According to the book $\displaystyle \frac {\vert AB\vert}{\vert A'B'\vert}=\frac{\vert BC\vert}{\vert B'C'\vert}=\frac{\vert CD\vert}{\vert C'D'\vert}=\ldots$
Now I'm usually not shy of a little Euclididean geometry, but I cannot for the life of me prove that this holds. Any suggestions?
Comment: Similar triangles?
Comment: I guessed that. But which ones?
Comment: The $IXX'$ where I is the intersection of 2 lines, and $X$ stands for $A, B, C, D$ or $E$. If $I$ does not exist then the ratio is simply 1.
Comment: ${AB\over A'B'}={\sin\alpha\over\sin\beta}$,
Comment: @user202729 As far as I can see, that only gives me that $\displaystyle \frac{\vert AI\vert}{\vert A'I\vert}=\frac{\vert BI\vert}{\vert B'I\vert}$, but not much more
Comment: Well, you can prove $\frac a b = \frac c d \Rightarrow \frac a b = \frac c d = \frac {a+c} {b+d}$ yourself, and then apply into that equality.
Comment: This is the first time I've ever seen that $\frac a b = \frac c d \Rightarrow \frac a b = \frac c d = \frac {a+c} {b+d}$. But that does do the trick. Could you convert this into an answer so that I can accept.
Answer: Parallel transform $A'B'$ s.t. $B=B'$, then you can use sine rule
 $${AB\over A'B'}={\sin\alpha\over\sin\beta}.$$ $\alpha$ is the angle between $A'A$ and $A'B$, $\beta$ is the angle between $AA'$ and $AB$. Two angles are the same for others.
Comment: But you cannot assume $B=B'$ without loss of generality. So what if $B\neq B'$...?
Comment: Parallel transform $A'B'$ s.t. $B=B'$. i.e. just draw another  line through $B'$ that parallel $A'B'$ and intersect $AA'$ in point $A''$. Then you can see a triangle $ABA''$ and  $A'B'=A''B$. @gebruiker
Comment: @gebruiker English is not my first language, maybe some misrepresentation in the answer.
Comment: Ah, I get what you're saying now. It's essentially the same as what [Narasimham is doing](https://math.stackexchange.com/a/2455654/145141), right? +1
Answer: More of trig and construction. The ratios you gave from the book are cosine ratios of inclination angle $\alpha$ between two large sides of triangles with red hypotenuse parallels added from your sketch.
So all $\alpha$ s have the same value equal to these ratios.
Comment: Just to use some different phrasing: Say $A''$ is the point where the red hypothenuse intersects $AA'$ and define $B''$, $C''$, etc analogously. Then $\triangle ABA''$ is similar to $\triangle BCB''$ is similar to ...(etc)
Comment: Correct. I left this as hint for further recognition of similarities in the series of right triangles. Also did not add many labels to avoid crowding  in your original diagram.
Answer: Consider 2 lines intersecting at point  $X$ in the first quadrant, acute angle.
1)Choose any point, say, on the lower ray, label it $ B'.$
2) Choose any point on the upper ray, label it $B.$
We get a $\triangle AB'B.$
Draw a parallel line to $B'B$ intersecting at $XB'$ at $A',$  say, to the left of $B'$, I.e. $XA' \lt XB',$  and intersecting $XB$ at $A$.
$\triangle XA'A$ and $\triangle XB'B$ are similar.
1)$BB'/AA' = XB'/XA' = XA'/XA' + A'B'/XA' =$
$1+ A'B'/XA'$.
Likewise:
2) $BB'/AA' = 1 + AB/XA$.
Subtracting 2) from 1) yields:
$A'B'/XA' = AB/XA,$ or
$A'B'/AB= XA'/XA.$
To continue:
Draw a parallel to $B'B$ intersecting at $C'$ and $C$, proceed likewise.


What is a state-of-the-art, practical, and economical solution to get a Linux from online that installs Intel MKL?

Question: I am working on an old Mac (Early 2013 machine) and need to run something from Intel MKL. I would like to avoid using my Mac to run Intel MKL, and I heard there could be ways to run a virtual machine on a cloud. So, what is the state-of-the-art and yet practical solution for doing so? Sorry I am quite ignorant about the capability of cloud computing, but I guess one can have a Linux system running on a nIntel CPU remotely that can be accessed online, and then Intel MKL can be installed there, am I correct, and how that can be done?
Comment: Pretty sure it doesn't NEED to me an Intel CPU and it should run on that old Mac too. Assuming then that the issue is the age (and hence performance) of the machine, you just need access to ANY other computer. For cloud, I don't know what you can get for free, but if you are willing to pay (price depends on requirements, hard to say much more without details) there are loads of options, unsurprisingly. However, depending on your situation there might be other, possibly better, options, but you did not provide nearly enough information for me to suggest anything specific.
Comment: @Qubit: It does have to at least be an x86 CPU, but a 2013 Mac is in the middle of Mac's x86-64 Intel era.  If it was an old PowerPC Mac, it wouldn't run MKL except in a slow emulator.  I assume zell wants to run on something more powerful than an old Haswell or Sandybridge, though; an old Mac would be fine to develop and test on, but throughput is not as good as a modern CPU with more cores and higher sustained clocks, and more memory bandwidth.  (And with AVX2 + FMA, if that 2013 machine isn't Haswell.)
Comment: Of course, running on newer hardware will hopefully be faster. But getting access to something like that depends on his circuimstances. In academia for instance, it's relatively simple. Outside, I imagine it's hard to get something for free outside limited trial periods, but I could be wrong. In any case, installing MKL is usually not a problem, given that it is free software.
Answer: Maybe Intel Devcloud is the one that you are looking for.
Intel DevCloud is a cloud service provided by Intel that comes with pre-installed Intel optimised frameworks, tools and libraries where you can get the oneMKL library as well. Intel Devcloud offers complimentary access to a wide range of intel architectures to help you get instant hands-on experience.
You can try creating a devcloud account by registering using the below link
https://www.intel.com/content/www/us/en/forms/idz/devcloud-registration.html?tgt=https://www.intel.com/content/www/us/en/secure/forms/devcloud-enrollment/account-provisioning.html
and to know more about how to connect and use Intel devcloud, you can get the details from the following link
https://devcloud.intel.com/oneapi/documentation/shell-commands/
Hope the provided information helps.


Eloquent using parent field in subquery

Question: How can i access the 'id' field in the first query, and use it in the second subquery in this example:
   $users = \App\User::where('active', 1)->where('deleted_at', null)->where('recieve_jobagent', 1)
        ->with(['companies' => function($query){

            $query->where('active', 1);

            $query->with(['posts' => function($posts){

                $posts->where('active', 1);

                $posts->whereHas('users', function($postUsers){

                    **I do not have access to 'users.id' here**
                    $postUsers->where('user_id', 'users.id');

                });

            }]);

        }]);

I'm using lumen 5.1.
My goal here is to get all users and their related companies, and the companies' posts that isn't related to the user.
Answer: I dont think you need to do that.  You can use whereHas and by drilling down into the relationships it returns only the ones that belong to the thing you're querying.
Without seeing your relationship structure, I would assume you need something like this?
$users = \App\User::where('active', 1)->where('deleted_at', null)->where('recieve_jobagent', 1)
    ->whereHas('companies', function($query){
        $query->where('active', 1)
        ->whereHas('posts', function($query){
            where('active', 1);
        })
    })
    ->with(['companies' => function($query){
        $query->where('active', 1);
        $query->with(['posts' => function($posts){
            $posts->where('active', 1);
        }]);
    }]);

Comment: You probably want to open your own question @jessehouwing but you sounds like you would need to use `whereHas` with your query criteria and then `with` to lazy load all trainers.
Comment: This looks great! I added the bounty because I have a similar problem. The model is as follows: Class -> Trainers. I want all classes where one of the trainers is X, then return the class with all of its trainers. Looks like `whereHas` will do the trick. Right?
Comment: encounter a similar problem, but it is really helpful ans!

Eloquent using parent field in subquery


Calculating end time from a start time and a percent complete in mysql

Question: What I have:
I have a timestamp field with the time that my project started
The user is updating the project with the % complete (1 to 100 not .01 to 1)
I want to insert into an updates table with an estimated completion timestamp.
Basic math:
How to get the full time of the project:
(now() - start timestamp) * (100 / percent complete)

End Time:
start timestamp + full time of the project estimated above

I then need to insert the end time estimated into the table during the insert.
Comment: What part are you having trouble with? What did you try?
Answer: Right away you've got a problem, never mind the math.
The timestamp datatype automatically updates to the current time every time you touch the row. That means when "the user updates the project", the start_timestamp column will update too!
You need the datetime datatype instead, which stores the same type of data, but doesn't update when other columns are updated.
Comment: hmm, i've tested a few times, and this doesn't seem true
Answer: I think i've got it.
select timestampadd(second, round(timestampdiff(second, '2012-08-22 22:00:30', now())*(100/33)), '2012-08-22 22:00:30');

that returns:
2012-08-30 17:28:41
the 33 would be the %, and the other timestamps are the start time.  Works perfect.


Show that there exist a function $\varphi\in H^{1}(\mathbb{R}^N)$ such that $f(\varphi)>0$ .

Question: Let $a\in L^{\infty}(\mathbb{R}^N)$ with $a^{+}\neq 0$, here $a^{+}=\max\{a(x),0\}$. Given
$$ f(u)=\int_{\mathbb{R}^N}\int_{\mathbb{R}^N}\frac{a(x)u^{2}(x)a(y)u^{2}(y)}{|x-y|^{N-2}}dxdy,~~\forall u\in H^{1}(\mathbb{R}^N).$$
Show that there exist a function $\varphi\in H^{1}(\mathbb{R}^N)$ such that $f(\varphi)>0$.
I tried to assume that for all  $\varphi\in H^{1}(\mathbb{R}^N)$ such that $f(\varphi)\leq 0$, but I can't find a contradiction.
Answer: It is not true. Take
$$a(x) = \begin{cases} 1,\ x=0 \ \\ 0, \ x\neq 0\end{cases}$$
Then $a^+ = 1$, but $f(\phi) = 0$ for any $\phi \in H^1(\mathbb{R}^n)$. However if you assume that $a > C > 0$ on some set of positive measure $A \subset \mathbb{R}^N$. Then let $\phi = \chi_A$, where  $\chi_A$ is the characteristic function of $A$. Then
$$f(\phi) = \int_A\int_A \frac{a(x)a(y)}{|x-y|^{N-2}}dydx > C \int_A \int_{A}\frac{1}{|x-y|^{N-2}}dydx >0$$
Note that the Hilbert transform is well defined on $C^1_c$, so the above integral converges.
Comment: In particular, if you replace the condition $a^+ \neq 0$ with the more natural $||a||_{\infty} \neq 0$, then the statement is true.