Execute Excel Macro, Then Import Into Access Table On Multiple Excel Files In A Folder

Question: I need to import multiple Excel spreadsheets into an Access table.  The spreadsheets currently look like this:
ID | Year | Sales | Commissions
-- | ---- | ----- | -----------
 1 | 2016 | 1,000 |  100
 2 | 2016 | 2,000 |  200
 3 | 2016 | 3,000 |  300
 4 | 2016 | 1,000 |  300

They need to look like this:
ID | Name | Year | Month | Sales | Commissions | Discount | Net Sales | %
-- | ---- | ---- | ----- | ----- | ----------- | -------- | --------- | -
 1 | John | 2016 |  2    | 1,000 |  100        |          |           |
 2 | Mary | 2016 |  2    | 2,000 |  200        |          |           |
 3 | Jake | 2016 |  2    | 3,000 |  300        |          |           |
 4 | Bob  | 2016 |  2    | 1,000 |  300        |          |           |

The last three columns will be blank.  Names ("John, Mary...") will use a lookup based on ID on another spreadsheet, but this can be done later in Access.  3 rows will have to be deleted at the top as well.  The month ("2") will be in the file path of the Excel file.  It will be the only number in the file name.  I.e., "2" for February.  All the spreadsheets will be in the same folder.  Each Excel workbook will have a spreadsheet titles "DataSheet".  This is the spreadsheet that will be imported from each workbook.
Can I write a script in Access to do all of this?
So far I've got this for the Excel macro:
  Sub Macro2()
    Rows("1:3").Select
    Range("A3").Activate
    Selection.Delete Shift:=xlUp
    Columns("B:B").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "Name"
    Columns("D:D").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "Month"
    Range("G1").Select
    ActiveCell.FormulaR1C1 = "Discount"
    Range("H1").Select
    ActiveCell.FormulaR1C1 = "Net Sales"
    Range("I1").Select
    ActiveCell.FormulaR1C1 = " %"
    Range("G2").Select
End Sub

Here's something like this would import multiple files:
    Dim strPathFile As String, strFile As String, strPath As String
    Dim strTable As String
    Dim blnHasFieldNames As Boolean
    blnHasFieldNames = False
    strTable = "tablename"
    strFile = Dir(strPath & "*.xls")
    Do While Len(strFile) > 0
          strPathFile = strPath & strFile
          DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
                strTable, strPathFile, blnHasFieldNames
          strFile = Dir()
    Loop

Answer: This looks like some simple vlookups to me.  Once you get the excel stuff straightened out, and you are ready to import multiple files into a table, just run a script like this.
Sub ImportAllFilesIntoOneTable

        Dim strPathFile As String, strFile As String, strPath As String
        Dim strTable As String
        Dim blnHasFieldNames As Boolean

        ' Change this next line to True if the first row in EXCEL worksheet
        ' has field names
        blnHasFieldNames = False

        ' Replace C:\Documents\ with the real path to the folder that
        ' contains the EXCEL files
        strPath = "C:\Documents\"

        ' Replace tablename with the real name of the table into which
        ' the data are to be imported
        strTable = "tablename"

        strFile = Dir(strPath & "*.xls")
        Do While Len(strFile) > 0
              strPathFile = strPath & strFile
              DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
                    strTable, strPathFile, blnHasFieldNames

        ' Uncomment out the next code step if you want to delete the
        ' EXCEL file after it's been imported
        '       Kill strPathFile

              strFile = Dir()
        Loop

End Sub


Problema ao customizar a barra de pesquisa

Question: Olá, estou tendo problemas em conseguir fixar a barra de pesquisa no centro da navbar, tendo em vista que toda alteração na css não muda em nada.
HTML:

          <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">

          <link href="src/css/style.css" rel="stylesheet" media="all">

                <form class="form-inline mr-auto">
                    <input class="form-control" type="search" placeholder="Search">
                </form>

CSS:
.navbar {
    background-color: #3f3f3f;
}

#search .form-control {
    width: 200px;
    text-align: center;
}

Obrigado pela paciência.
Comment: Com ajuda de muitos aqui percebi vários erros no código, revisarei tudo. Obrigado!
Comment: Está tendo fazer sua barra de pesquisar ser responsiva? Isso talvez ajude: https://www.w3schools.com/howto/howto_css_searchbar.asp
Comment: @WandersonRodrigo Oi, na verdade estou tentando fixar a barra de pesquisa no centro da navbar, tendo em vista que atualmente ela está no canto esquerdo, desculpe se eu não fui claro na pergunta.
Comment: Olá @Nithogg, tudo bem?

Já tentou atribuir a class="fixed-top"  no seu navbar? Acredito que possa solucionar seu problema.
Comment: Estes exemplos aqui podem ajudar:[link](https://www.codeply.com/go/kTGlK9Axdk)
Answer: Cara seu código tem alguns errinhos.
Primeiro vc tem no CSS uma classe pro ID #search, mas no HTML nenhum elemento tem esse ID.
Vc pode utilizar as classes nativas do Bootstrap para alinhar do jeito que vc quer. Primeiro com w-100 vc deixa o form com 100% da largura, depois com m-auto vc deixa o input centralizado.
OBS: Vc usou algumas classes como mr-auto de maneira equivocada ao meu ver, isso tb atrapalha. Além disso estão faltando classes importantes na sua navbar, tente seguir a documentação! https://getbootstrap.com/docs/4.0/components/navbar/
Execute em "Página toda" para ver como ficou, pois vc está usando a classe navbar-expand-md na navbar, isso não deixa ela aparecer nas tela pequenas

          <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">

          <link href="src/css/style.css" rel="stylesheet" media="all">

                <form class="form-inline w-100" id="search">
                    <input class="form-control m-auto" type="search" placeholder="Search">
                </form>

Comment: Desculpe essas bobeiras, estou há muito tempo sem utilizar bootstrap e fiquei meio perdido agora que voltei. Vou revisar as classes ausentes.
Obrigado pela ajuda didática! ;D
Comment: O importante não eh corrigir a qq custo, o importante eh entender onde se está errando, Pq, e como corrigir
Comment: @Nithogg tranquilo framework eh para isso, já vem com as classes embutidas para serem usadas mesmo rss. A parte chata eh que temos que seguir a documentação. Inclusive o próprio bootstrap 4 já tem todas as classes de Flex nem precisa escrever na mão como o amigo da outra resposta sugeriu... Procure nos utilits d bs4 por Flex que vc vai ver ;)
Answer: Segue amigo

    <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">
<link href="src/css/style.css" rel="stylesheet" media="all">

        <form class="form-inline">
            <input class="form-control" type="search" placeholder="Search">
        </form>

CSS
.navbar {
     background-color: #3f3f3f;
        }

#search {
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center
}


create shared library with main to call undefined function and provide function body in other project

Question: I need to create a shared library using cmake, and I must call function run() in a library function. But the project which uses this library should provide the body of this function.
The real cases use systemC which force library to implement main function. To avoid further complexity, I try to simplify the case like this:
MyInterface.h
void run();

MyLibraryAction.cpp
#include "MyInterface.h"

int main(){
    run();
}

The cmake content is:
add_library(mylib SHARED MyLibraryAction.cpp)

set_target_properties(mylib PROPERTIES PUBLIC_HEADER MyInterface.h)

configure_file(mylib.pc.in mylib.pc @ONLY)

install(
  TARGETS mylib
  DESTINATION ${CMAKE_INSTALL_LIBDIR}
  PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

install(FILES ${CMAKE_BINARY_DIR}/mylib.pc
        DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)

I am wondering if that is possible. If so, am I doing it right? is using extern relevant in this context?
I am get error when I try to build and create the library:

undefined reference to run()
Comment: Perhaps this can be solved using [*weak symbols*](https://en.wikipedia.org/wiki/Weak_symbol)? You could try making `run` a weak symbol in the library.
Comment: Where is the `run` function defined? Where is it implemented? You only seem to have a declaration.
Comment: @Someprogrammerdude yes, I just wrote these demo files to clarify my problem. I fixed that.
Comment: @Someprogrammerdude: "Also, where is the run function defined?" - "But the project which uses this library should provide the body of this function.". The question is not about why particular **code** doesn't work. The question is about how to make particular **idea** to work.
Comment: @Someprogrammerdude, @Tsyvarev yes the other project should provide the body of `run` function.
Answer: As mentioned in the comments, weak symbols worked fine in this case.
MyInterface.h
void run();

and then have an implementation for run function with weak symbol:
InterfaceWeakImplementation.h
void __attribute__((weak)) run(){
    // pass
}

and have the actual implementation in caller project
InterfaceStrongImplementation.h
void run(){
    // Some real work
}


Please help me understand a Figure in Bishop's "Pattern Recognition and Machine Learning", Sec 1.5.1 Minimizing the misclassification rate

Question: The figure is Figure 1.24 on page 40 of Bishop's "Pattern Recognition and Machine Learning", Sec 1.5.1 Minimizing the misclassification rate:

I don't understand this figure, starting from "Errors arise from the blue, green, and red regions, ..." For example, why does the red region represent points from $\mathcal{C}_2$? If this is because $p(x,\mathcal{C}_2)$ is greater than $p(x,\mathcal{C}_1)$ there, points $x<x_0$ are from $\mathcal{C}_1$ so the classification is correct. Why is it still green which means error?
Answer: The answer is the understanding of the notation of joint probability distribution $p(x,\mathcal C_k)$. This joint probability distribution comes from ground-truth, perhaps a long-term collection of X-ray images and confirmed clinic diagnoses of whether the patients truly have cancer. So, $\mathcal C_k$ represents the truth; God defines that, while $\mathcal R_k$ represents the efforts of how we human being tries to make decision. The sum of red and green is the first integral of equation (1.78) $$\int_{\mathcal R_1}p(x,\mathcal C_2)dx.$$
The $\mathcal C_2$ in the integrand means that the sample $x$ is actually from class $\mathcal C_2$, as ground truth, but the integral bound $\mathcal R_1$ means that we human being thinks $x$ belongs to $\mathcal R_1$, or mistakenly assigned to class $\mathcal C_1$.


Switch Statements for Grades Javascript

Question: So I have to get the grade from a prompt then produce an alert box that says something about the grade using switch statements. This is what i have so far but it just tells me back the grade I have given it if i add an alert box at the bottom
    var grade = prompt("What grade did you get for the assessment?");

switch(grade) {
    case 0: "A" = ("Excellent"); break;
    case 1: "B" = "Very Good"; break;
    case 2: "C" = "Good"; break;
    case 3: "D" = "Pass"; break;
    case 4: "E" = "Almost a pass"; break;
    case 5: "F" = "Fail"; break;
    default : "Please enter a grade from A-F";
}

Comment: And your question/issue is?
Comment: What's your question?
Comment: You don't seem to understand the syntax of switch statements. Google it and look it up.
Comment: Have you googled it ?
Comment: http://www.w3schools.com/js/tryit.asp?filename=tryjs_prompt
Comment: Switch statements http://www.w3schools.com/js/js_switch.asp
Comment: Why all the hate? He has an issue and needs help
Comment: @samrodrigues - i see you asked a few questions in the past. Did you not google them? We all come here to learn. We should all try and remember that we all knew little once upon a time too :)
Comment: @darren Well its just a question and i did provide resources to help him  :)
Answer: Your switch statement is a little messed up.
I won't post full code for you. I always find typing out, rather than copy/pasting helps with the learning and memory retention, though you would be looking for something a little more like this:
var grade = prompt("What grade did you get for the assessment?");
var result; // this will hold a result from our switch

switch(grade){
 case "A": // this is what will be typed (case sensitive)
    result = "Excellent"; // assign some text to result - no brackets
    break;
case "B": // this is what will be typed (case sensitive)
    result = "Very good";
    break;
default:
    result = "please enter.." //something wasn't matched
}
console.log(result); // spits out the result var

To break the switch down:
switch(yourVar)

you pass in yourVar to the switch for evaluation
case "D":
//
break;

When yourVar == "D" do whatever is between case and break
default:
//..

When there is no match use whatever is after default.
Answer: var grade = prompt("What grade did you get for the assessment?");

var message;

switch (grade) {
    case "A":
        message = "Excellent";
        break;

    case "B":
        message = "Very Good";
        break;

    case "C":
        message = "Good";
        break;

    case "D":
        message = "Pass";
        break;

    case "E":
        message = "Almost a pass";
        break;

    case "F":
        message = "Fail";
        break;

    default:
        message = "Please enter a grade from A-F";
}

alert(message);

Switch Statements for Grades Javascript


Standard Deviation given n, Σx, Σx²

Question: I have been trying to solve this problem for over half an hour now. I don't know if it is to do with my incapability of using a calculator or a misunderstanding in standard deviation.
Question:
A sample of 10 people complete a task and their times, x, are recorded. Given that Σx = 273 and Σx²=7561, calculate the mean and standard deviation of the times taken.
Attempt of a solution:
The mean would be Σx / n. Therefore, the mean is 273/10 = 27.3
As for standard deviation, since it is a sample, we would use the formula:  Link to the formula
But when I type  √(7561/(10-1))-27.3² (square root over whole thing) , I get 9.74 (3sf)
Actual Answer:
= 3.29 (EDIT: The answer on the book was incorrect?)
Comment: $\sum (x-\bar{x})^2 \neq \sum x^2$
Comment: Andrew Li Oh... how would you write the numerator then?
Comment: $\sum (x - \bar x)^2 = \sum x^2 - 2\bar x\sum x + n \bar x^2$
Answer: sample standard deviation $= \sqrt{\left(\dfrac{\sum (x-\bar x)^2}{n-1}\right)}$$= \sqrt{\left(\dfrac{\sum x^2 - 2\bar x \sum X + n\bar x ^2}{9}\right)}$
$=\sqrt{\left(\dfrac{\sum x^2 - \frac{(\sum X)^2}{n}}{9}\right)}$
$ = \sqrt{\left(\dfrac{7561 - \frac{273^2}{10}}{9}\right)} = \sqrt{12.0111} = 3.47$
Comment: The book is diving by 10 instead of 9 and that is how you get 3.29
Comment: Square root of 12.0111 is 3.47?
Comment: Then that is the answer?  The book is wrong
Comment: Satish Ramanathan Oh ok thanks for clearing that up :)
Answer: Just keep it short and simple. Use the formula $Var(X)=E(X^2)-[E(X)]^2$
$$Var(X)=\frac1n \sum_{i=1}^n x_i^2-\left[\frac1n \sum_{i=1}^n x_i\right]^2=\frac1{10}\cdot 7561-27.3^2$$
Therefore the standard deviation of the sample is $\sqrt{\frac1{10}\cdot 7561-27.3^2}=3.2878..\approx3.29$
Remark:
This is the standard deviation of the sample. That it was asked for in the exercise. The book is not wrong.
Sometimes it is asked for an unbiased estimator for the standard deviation of the population. Then the formula is $$Var(X)=\frac{1}{n-1} \left[\sum_{i=1}^n x_i^2-n\cdot \left[\frac1n \sum_{i=1}^n x_i\right]^2\right]$$
Comment: Apparently the answer on the book was incorrect because it should be dividing by n-1 since it is sample standard deviation? Can you confirm this?
Comment: @coder80 It depends. The standard deviation of the sample is about $3.29$. But if  it supposed to calculate an unbiased estimator for the standard deviation of the population then it should be divided by 9. But this is not mentioned in the exercise. So the answer of the book is correct.
Comment: @coder80 I´ve made an edit/remark. I hope it makes some aspects more comprehensible.

Standard Deviation given n, Σx, Σx²


How to put repeating buttons in a template without losing functionality

Question: In a project (Prism, MVVM, Windows8.1 StoreApp) I have a piece of XAML like this:
<code>                <GridView
                x:Name="grvLetters"
                Grid.Row="1"
                Margin="80,0,0,0"
                HorizontalAlignment="Left"
                VerticalAlignment="Top">
                <Button x:Name="btnA"
                    Content="A"
                    Command="{Binding Letter_Clicked}"
                    CommandParameter="{Binding ElementName=btnA, Path=Content}"/>
                <Button x:Name="btnB"
                    Content="B"
                    Command="{Binding Letter_Clicked}"
                    CommandParameter="{Binding ElementName=btnB, Path=Content}"/>
                <Button x:Name="btnC"
                    Content="C"
                    Command="{Binding Letter_Clicked}"
                    CommandParameter="{Binding ElementName=btnC, Path=Content}"/>
                <Button x:Name="btnD"
                    Content="D"
                    Command="{Binding Letter_Clicked}"
                    CommandParameter="{Binding ElementName=btnD, Path=Content}"/>
                <Button x:Name="btnE"
                    Content="E"
                    Command="{Binding Letter_Clicked}"
                    CommandParameter="{Binding ElementName=btnE, Path=Content}"/>
                <Button x:Name="btnF"
                    Content="F"
                    Command="{Binding Letter_Clicked}"
                    CommandParameter="{Binding ElementName=btnF, Path=Content}"/>
            </GridView>
</code>
I would like to put this in a DataTemplate, but when I do that, the command is not forwarded to the ViewModel. Here's what I have been trying so far:
This is what I have in the template (based on the below suggestion of Daniel Sklenitzka):
<code>     <DataTemplate  x:Key="LetterButton">
        <Button
            x:Name="btnLetter"
            Command="{Binding Letter_Clicked}"
            CommandParameter="{Binding ElementName=grvLetters, Path=Content}"
            />
    </DataTemplate>
</code>
That suggestion does not work, so I have changed back to:
<code>    <DataTemplate  x:Key="LetterButton">
        <Button
            x:Name="btnLetter"
            Command="{Binding Letter_Clicked}"
            CommandParameter="{Binding ElementName=btnLetter, Path=Content}"
            />
    </DataTemplate>
</code>
In order to have the ItemsSource for the GridView I have added this in the ViewModel:
<code>        Public Property Alfabet As String() = {"A", "B", "C", "D", "E", "F"}
</code>
The XAML for the GridView has been changed into:
<code> <GridView
                x:Name="grvLetters"
                ItemsSource="{Binding Alfabet}"
                Grid.Row="1"
                Margin="80,0,0,0"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                ItemTemplate="{Binding Source={StaticResource LetterButton}}"
                >
</code>
Unfortunately this won't work. The letters appear perfectly on the View, but there is no reaction in the ViewModel when a button is clicked.
The buttonclick would have to be caught in the viewmodel (this is not the problem, the command is received in the ViewModel when I use the first XAML above.
But how to improve this approach? Any suggestions ? More than welcome.
Bye
Answer: When you use an ItemsSource and DataTemplates, the container (GridView in your case) creates an instance of the template for every element in the ItemsSource collection, and it sets the element as the DataContext of this instance.
That means that the DataContext of your Button is a letter, and the letter doesn't have a property Letter_Clicked, which is why the binding fails.
Try binding the command like this:
<code>Command="{Binding DataContext.Letter_Clicked, ElementName=grvLetters}"
</code>
Comment: Hi, and thanks for your reply, but I'm afraid I dont fully understand the code you provide.

When I have the DataTemplate in the Page.Resources, then it's not possible to refer to grvLetters, only to the btnLetter. Next, you suggest a change in binding of the command, but Command cannot be bound to DataContext. In Prism MVVM the Command is binding to a commandDelegate in the ViewModel. What you suggest would mean that the commandDelegate's name would have to change. I will try further, your explanation of the details of DataTemplates is clear enough.

Bye
Comment: The Binding is resolved at runtime, so it can refer to grvLetters, because the instantiated template is in the same visual tree as the GridView.

As for the command: You don't need to change anything. The binding as defined in my answer is resolved like this:
First the element with name grvLetters is resolved, resulting in your GridView. Then the DataContext property of this element is resolved, resulting in the same object that the bindings in your original example refer to (the ViewModel defining your Command). And eventually the Letter_Clicked property of the ViewModel is resolved.
Comment: I did what you suggested, but it won't work, whereas the implementation with the repeating buttons like in the first code snippet keeps working correctly. On the same page that is! I have updated the Original question to show the code as it is now.
Comment: What I can add to this is the error message that results from the change in the code. It says: _Error: BindingExpression path error: 'Content' property not found on 'Windows.UI.Xaml.Controls.GridView'. BindingExpression: Path='Content' DataItem='Windows.UI.Xaml.Controls.GridView'; target element is 'Windows.UI.Xaml.Controls.Button' (Name='btnLetter'); target property is 'CommandParameter' (type 'Object')._ It doesn't surprise me, because the content is part of the Button, and nof part ot the GridView. So unfortunately your suggestion is for the worse...
Comment: Please read again, the code you posted above is not what I suggested. Try this:
Comment: Thanks for your patience. I have tried literaly would you suggest in your last posting, but it doen not improve. I think the Command binding cannot be changed like you suggest, because it must have the name of the DelegateCommand in the ViewModel. And binding the Content would have the reverse effect: the button would react on changes in the ViewModel. The CommandParameter should be bound to the Button itself, passing through the Content of the Button. NB! The original XAML with the repeating buttons still works with that implementation, on the same View!
Comment: I'm not sure what you're really trying to accomplish, but I tried it myself and it worked fine.
Using just {Binding} for the parameter will result in the letter itself being passed to the command.


Swift SceneKit lighting and effecting emission texture

Question: I'm developing an app which is about the solar system.
I'm trying to turnoff the Emission Texture, where the light hits the surface of the planet. But the problem is that an emission texture by default, always shows the emission points regardless the absence or presence of the light.
My request in a nutshell: ( I wanna hide the emission points, on places where the light hits the surface )
override func viewDidLoad() {
    super.viewDidLoad()

    let scene = SCNScene()

    let earth = SCNSphere(radius: 1)
    let earthNode = SCNNode()
    let earthMaterial = SCNMaterial()
    earthMaterial.diffuse.contents = UIImage(named: "earth.jpg")
    earthMaterial.emission.contents = UIImage(named: "earthEmission.jpg")
    earth.materials = [earthMaterial]
    earthNode.geometry = earth
    scene.rootNode.addChildNode(earthNode)

    let lightNode = SCNNode()
    lightNode.light = SCNLight()
    lightNode.light?.type = .omni
    lightNode.position = SCNVector3(x: 0, y: 10, z: 5)
    scene.rootNode.addChildNode(lightNode)

    sceneView.scene = scene

}

Comment: Facing the same problem right now, emission should only show on the night-side, i.e. the side lit by the SCNLight. Anyone any ideas?
Comment: Does SceneKit have bitmap masking of textures?
Answer: SceneKit's shader modifiers are a perfect fit for this kind of task.
You can see footage of the final result here.
Fragment shader modifier

We can use _lightingContribution.diffuse (RGB (vec3) color representing lights that are applied to the diffuse) to determine areas of an object (in this case - Earth) that are illuminated and then use it to mask the emission texture in the fragment shader modifier.
The way you use it is really up to you. Here's the simplest solution I've come up with (using GLSL syntax, though it will be automatically converted to Metal at runtime if you are using it)
uniform sampler2D emissionTexture;

vec3 light = _lightingContribution.diffuse;
float lum = max(0.0, 1 - (0.2126*light.r + 0.7152*light.g + 0.0722*light.b)); // 1
vec4 emission = texture2D(emissionTexture, _surface.diffuseTexcoord) * lum; // 2, 3
_output.color += emission; // 4

calculate luminance (using formula from here) of the _lightingContribution.diffuse color (in case the lighting is not pure white)
subtract it from one to get luminance of the "dark side"
get emission from a custom texture using diffuse UV coordinates (granted emission and diffuse textures have the same ones) and apply luminance to it by multiplication
Add it to the final output color (the same way regular emission is applied)

That's it for the shader part, now let's go though the Swift side of things.

Swift setup
First-off, we are not going to use emission.contents property of a material, instead we would need to create a custom SCNMaterialProperty
let emissionTexture = UIImage(named: "earthEmission.jpg")!
let emission = SCNMaterialProperty(contents: emissionTexture)

and set it to the material using setValue(_:forKey:)
earthMaterial.setValue(emission, forKey: "emissionTexture")

Pay close attention to the key – it should be the same as the uniform in the shader modifier. Also you don't need to persist the material property yourself, setValue creates a strong reference.
All that is left to do is to set the fragment shader modifier to the material:
let shaderModifier =
"""
uniform sampler2D emissionTexture;

vec3 light = _lightingContribution.diffuse;
float lum = max(0.0, 1 - (0.2126*light.r + 0.7152*light.g + 0.0722*light.b));
vec4 emission = texture2D(emissionTexture, _surface.diffuseTexcoord) * lum;
_output.color += emission;
"""
earthMaterial.shaderModifiers = [.fragment: shaderModifier]

Here's footage of this shader modifier in motion.
Note that a light source has to be quite bright otherwise dim lights are going to be seen around the "globe". I had to set lightNode.light?.intensity to at least 2000 in your setup for it to work as expected. You might want to experiment with the way luminosity is calculated and applied to emission to get better results.

In case you might need it, _lightingContribution is a structure available in the fragment shader modifier that has also has ambient and specular members (below is Metal syntax):
struct SCNShaderLightingContribution {
    float3 ambient;
    float3 diffuse;
    float3 specular;
} _lightingContribution;

Comment: Thank you Lësha, that worked exactly as I needed it. Also very happy I know now how to setup shader modifiers, have done that before in Unity, just not in Swift/Scenekit yet!
Answer: I like Lësha's answer, made a small modification to the shader so that it will work with lower light levels. Added a threshold (t) below which emission values will not show, and then between the threshold and zero it interpolates values between diffuse and diffuse + emission. Changing the value of t adusts the width of the band depicting the transition between night and day. I also appended a 0.5 multiplier on the emission formula, since the emission texture I'm using looked artificially bright without it.
let shaderModifier =
"""
uniform sampler2D emissionTexture;

vec3 light = _lightingContribution.diffuse;
float lum = max(0.0, 1 - (0.2126*light.r + 0.7152*light.g + 0.0722*light.b));
vec4 emission = texture2D(emissionTexture, _surface.diffuseTexcoord) * lum * 0.5;
float t = 0.11; // no emission will show above this threshold
_output.color = vec4(
    light.r > t ? _output.color.r : light.r/t * _output.color.r + (1-light.r/t) * (_output.color.r + emission.r),
    light.g > t ? _output.color.g : light.g/t * _output.color.g + (1-light.g/t) * (_output.color.g + emission.g),
    light.b > t ? _output.color.b : light.b/t * _output.color.b + (1-light.b/t) * (_output.color.b + emission.b),1);
"""


Server-side rendering of WPF UserControl

Question: I am writing a server side console app in C#/.Net 4.5 that gets some data and creates static chart images that are saved to be displayed by a web server.
I am mostly using the method described here:
http://lordzoltan.blogspot.com/2010/09/using-wpf-to-render-bitmaps.html
However, I added a mainContainer.UpdateLayout(); after the Arrange() so that the databindings would update and be visible in the rendered image, as well as a Measure() before it for good... ah, I'm not gonna go there.
Here is the method that does the rendering:
void RenderAndSave(UIElement target, string filename, int width, int height)
{
    var mainContainer = new Grid
    {
        HorizontalAlignment = HorizontalAlignment.Stretch,
        VerticalAlignment = VerticalAlignment.Stretch
    };

    mainContainer.Children.Add(target);

    mainContainer.Measure(new Size(width, height));
    mainContainer.Arrange(new Rect(0, 0, width, height));
    mainContainer.UpdateLayout();

    var encoder = new PngBitmapEncoder();
    var render = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);

    render.Render(mainContainer);
    encoder.Frames.Add(BitmapFrame.Create(render));
    using (var s = File.Open(filename, FileMode.Create))
    {
        encoder.Save(s);
    }
}

The target parameter to the method will be an instance of a WPF/XAML UserControl I made - fairly simple at this point, just a grid with some text databinding to a ViewModel object that I assigned to the DataContext.
The saved image on disk looks good EXCEPT for the OxyPlot Plot object - it is entirely white.
Now, when I am in the designer in Visual Studio 2013, I can see it. I have added a design-time DataContext which is the same object that I use at runtime (this is a spike I am doing - the viewmodel is not in its final form yet, just having a bunch of default data while I work out the kinks). In the designer I see the chart as OxyPlot paints it.
Is there anything special I need to do in order to get my rendering to also contain this OxyPlot chart? It is more or less the point of the exercise so it would be awesome to actually get it to show up!
Thanks in advance for any insights and suggestions!
Comment: Givens the wide array of platforms supported, I would suspect that the problem is in how the OxyPlot WPF control was implemented. Perhaps it is a WinForms control embedded in a WPF control, which may be problematic. I do not know if that helps.
Comment: You could be right about this, of course - there is the chance that there is no way to do this without the control existing in a full WPF environment. In that case, I will go looking for another charting component. However, if I could just find out what the designer does that I do not, then I am hopeful that I could trigger it to paint itself..
Comment: @swiszcz: Could you explain why the answers provided here won't work for you?
Answer: If you're correctly binding data at runtime as well, then it should work.

 [STAThread]
 static void Main(string[] args)
 {
     string filename = "wpfimg.png";

     RenderAndSave(new UserControl1(), filename, 300, 300);

     PictureBox pb = new PictureBox();
     pb.Width = 350;
     pb.Height = 350;
     pb.Image = System.Drawing.Image.FromFile(filename);

     Form f = new Form();
     f.Width = 375;
     f.Height = 375;
     f.Controls.Add(pb);
     f.ShowDialog();
 }

XAML:
<UserControl x:Class="WpfApp92.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:oxy="http://oxyplot.org/wpf"
             xmlns:local="clr-namespace:WpfApp92"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">

    <Grid>
        <oxy:PlotView Model="{Binding Model}"/>
    </Grid>
</UserControl>

CS:
public partial class UserControl1 : UserControl
{
    public PlotModel Model { get; set; }

    public UserControl1()
    {
        InitializeComponent();

        Model = new PlotModel();
        Model.LegendBorderThickness = 0;
        Model.LegendOrientation = LegendOrientation.Horizontal;
        Model.LegendPlacement = LegendPlacement.Outside;
        Model.LegendPosition = LegendPosition.BottomCenter;
        Model.Title = "Simple model";
        var categoryAxis1 = new CategoryAxis();
        categoryAxis1.MinorStep = 1;
        categoryAxis1.ActualLabels.Add("Category A");
        categoryAxis1.ActualLabels.Add("Category B");
        categoryAxis1.ActualLabels.Add("Category C");
        categoryAxis1.ActualLabels.Add("Category D");
        Model.Axes.Add(categoryAxis1);
        var linearAxis1 = new LinearAxis();
        linearAxis1.AbsoluteMinimum = 0;
        linearAxis1.MaximumPadding = 0.06;
        linearAxis1.MinimumPadding = 0;
        Model.Axes.Add(linearAxis1);
        var columnSeries1 = new ColumnSeries();
        columnSeries1.StrokeThickness = 1;
        columnSeries1.Title = "Series 1";
        columnSeries1.Items.Add(new ColumnItem(25, -1));
        columnSeries1.Items.Add(new ColumnItem(137, -1));
        columnSeries1.Items.Add(new ColumnItem(18, -1));
        columnSeries1.Items.Add(new ColumnItem(40, -1));
        Model.Series.Add(columnSeries1);
        var columnSeries2 = new ColumnSeries();
        columnSeries2.StrokeThickness = 1;
        columnSeries2.Title = "Series 2";
        columnSeries2.Items.Add(new ColumnItem(12, -1));
        columnSeries2.Items.Add(new ColumnItem(14, -1));
        columnSeries2.Items.Add(new ColumnItem(120, -1));
        columnSeries2.Items.Add(new ColumnItem(26, -1));
        Model.Series.Add(columnSeries2);

        DataContext = this;
    }
}

Comment: Thanks for your answer, and sorry for my late comment on it. Well, your example obviously works. And I have two examples (proprietary code, unfortunately) where it doesn't... I hope I'll find some time in a near future to produce a minimal reproducible example to find out when (and why) it's not working.
Comment: You're very welcome. Sorry to hear about that... Certainly post a [MCVE](https://stackoverflow.com/help/mcve) whenever you have a chance, and send me a note about it, I'll be happy to look at it again. Cheers.
Answer: I don't know anything about this OxyPlat, but I do know that most charts are often rendered using hardware APIs. Hardware acceleration is usually error-prone when working outside the expected environment (i.e. a client showing a visible Desktop window).
On application initialization, try disabling hardware acceleration:
RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;

Another possible tweak is to call DoEvents() before you render your bitmap. Possibly with priority set to SystemIdle. This will make sure your DataContext has been successfully bound.
public static void DoEvents(
    DispatcherPriority priority = DispatcherPriority.Background)
{
    Dispatcher.CurrentDispatcher.Invoke(new Action(delegate { }), priority);
}

Comment: Our app is very heavy, so additionally removing a hardware support wouldn't be the best idea. Sorry for a late comment on your answer!


skip robot verification in firebase phone authorisation using react native

Question: While phone verification in firebase, the app opens a App-Name/auth/handler page and does robot verification and returns to app again and then an OTP is recieved.
I want to skip this middle step of robot verification.
I tried using
<code> firebase().auth().settings.appVerificationDisabledForTesting = true;
</code>
but it seems only for testing purpose and in IOS.
Comment: This is known as a recaptcha, so see some of these results for previous questions about this topic: https://www.google.com/search?q=skip+recaptcha+in+firebase+phone+authorisation+using+react+native
Answer: This works on android by registering your application to work correctly with Google's SafetyNet.
Check the documentation on android here: https://firebase.google.com/docs/auth/android/phone-auth#enable-app-verification
There has also been a similar question on how to enable SafetyNet in case you struggle: https://stackoverflow.com/a/64657110/3668241
Maybe also of interest:
For iOS to avoid the verification page, you need to enable silent push notification: https://firebase.google.com/docs/auth/ios/phone-auth#start-receiving-silent-notifications


Given a continuous map, prove the induced topology is equal to the relative topology of $S^1$

Question:
Let's look at the circle $S^1=\{(x,y)\in\Bbb R^2\mid x^2+y^2=1\}$ and the map $f:\Bbb R\to\Bbb R^2, f(t):(\cos(t),\sin(t)).$
A subset $A\subseteq S^1$ is said to be good if $f^{-1}(A)$ is open in $\Bbb R$. Prove that good sets generate a topology and that that topology is equal to the relative topology on $S^1$ as a subset of $\Bbb R^2$ with the standard topology.

My attempt:
Let $G:=\{A\subseteq S^1\mid f^{-1}(A)\text{ is open in }\Bbb R\}.$
$S^1,\emptyset\in G$ because $f^{-1}(S^1)=\Bbb R$ and $f^{-1}(\emptyset)=\emptyset$ are open in $\Bbb R.$$\tag 1$
Let $I$ be some set of indices and $A_i\in G,i\in I$. Then, $f^{-1}(A_i)=V_i$ is open in $\Bbb R$. We have
$$\tag 2 f^{-1}\left(\bigcup_{i\in I} A_i\right)=\bigcup_{i\in I} f^{-1}(A_i)=\bigcup_{i\in I} V_i\quad\text{
 is open in }\Bbb R\implies \bigcup_{i\in I}A_i\in G.$$
Let $k\in\Bbb N.$ We then have $$\tag 3 f^{-1}\left(\bigcap_{i=1}^k A_i\right)=\bigcap_{i=1}^kf^{-1}(A_i)=\bigcap_{i=1}^k V_i\quad\text{ is open in }\Bbb R\implies \bigcap_{i=1}^k A_i\in G.$$
Therefore, $G$ is a topology.
Now, let $U\subseteq\Bbb R^2$ be some open set.
$f(\Bbb R)=S^1$ is closed in $\Bbb R^2.$ Let $s\in S^1$.
$\forall r>0,$ we have $B(s,r)\nsubseteq S^1,$ so, I think $f^{-1}(U)=f^{-1}(A\cap U).$
Let $\tau$ denote the relative topology on $S^1$.
Since $f$ is continuous on $\Bbb R,$ for all open $U\subseteq\Bbb R^2, f^{-1}(U)$ is open in $\Bbb R$. Therefore, $f^{-1}(A\cap U)=f^{-1}(U)$ is open, hence $A\cap U\in\tau$ is also a good set, that is $A\cap U\in G.$
Since $U\subseteq\Bbb R^2$ was arbitrary and therefore, $A\cap U\in\tau$ as well, we have $\tau\subseteq G.$
We also need the other inclusion, $G\subseteq\tau,$ but, I don't know how to start with it. Are there any fallacies in my prior reasoning and how should I continue?
Comment: OK: when you get back to it, I hope you find that my last, corrected comment does get you there. It's getting near my bedtime too! Sorry about the false starts.
Comment: I have a problem with showing that a good set must be relatively open, that is $G\subseteq\tau$. $G$ denotes the *good sets* and $\tau$ the relative topology on $S^1$. I know $X\text{ open }\implies f^{-1}(X)\text{ is open }$ when $f$ is continuous, but the converse is not true. I thought open arcs, a basis for $\tau,$ were included in the part $\tau\subseteq G$. So, I'm not sure if I'm required to prove $f^{-1}(A)\text{ open in }\Bbb R\implies A\text{ relatively open in }S^1.$
Comment: Sorry, I did get things the wrong way round in my earlier (now deleted) comment. To prove $G \subseteq \tau$, first show that $f$ is a local homeomorphism: for every $t \in \Bbb{R}$ there are $a, b \in \Bbb{R}$ such that $t \in (a, b)$ and the restriction $f|_{(a, b)}$ of $f$ to the open interval $(a, b)$ is a homeomorphism of $(a, b)$ onto to an open subset of $S^1$. $G \subseteq \tau$ follows from this.
Comment: I've fixed the comment.
Comment: Sorry, my brain is dead at this time of the day. Thank you and cheers @RobArthan!
Answer: Your proof of $\tau \subset G$ is essentially correct, although your notation and reasoning is somewhat unclear.

What is $A$ in  $f^{-1}(U)=f^{-1}(A\cap U)$? You certainly mean  $f^{-1}(U)=f^{-1}(S^1\cap U)$ which is correct. In fact, for each function $\phi : X \to Y$ between sets $X,Y$ we have $f^{-1}(U) = f^{-1}(f(X) \cap U)$ for all $U \subset Y$. Now it suffices to observe the sets $S^1 \cap U$ with $U \subset \mathbb R^2$ open are precisely the open sets in relative topology on $S^1$.

Yes, $f(\Bbb R)=S^1$ is closed in $\Bbb R^2$, but it is not relevant for $\tau \subset G$. The statement "Let $s\in S^1$.$\forall r>0,$ we have $B(s,r)\nsubseteq S^1$" is also correct, but it does not have any value for your proof. You can omit it.

To show that $G \subset \tau$, let us consider the continuous surjection $\bar f : \mathbb R  \to S^1, \bar f(t) = f(t) = (\cos t, \sin t)$. Here $S^1$ is endowed with $\tau$. For $A \subset S^1$ we have $A = f(f^{-1}(A)) = \bar f(f^{-1}(A))$.
If $A \in G$, then $f^{-1}(A)$ is open and can be written as the union of open intervals $(a,b)$. We claim that all $\bar f((a,b))$ are open in $S^1$; this will prove that $A$ is the union of open subsets of $S^1$, hence is itself open in $S^1$.
The claim is trivial for $b - a > 2\pi$ because then $\bar f((a,b)) = S^1$. For $b - a \le 2\pi$ the closed interval $[a,a+2\pi]$ contains $(a,b)$. The set $K = [a,a+2\pi] \setminus (a,b)$ is compact, hence $\bar f(K) \subset S^1$ is compact, thus closed in $S^1$. Therefore $S^1 \setminus \bar f(K)$ is open in $S^1$.  We have $S^1 = \bar f([a,a+2\pi]) = \bar f(K \cup (a,b)) = \bar f(K) \cup \bar f((a,b))$. But $K$ and $(a,b)$ are disjoint, thus $s \in K$ and $t \in (a,b)$ cannot have the same image under $\bar f$ (note that the only two distinct points in $[a,a+2\pi]$ having the same image under $\bar f$ are $a$ and $a+2\pi$ which are both contained in $K$). We conclude that $\bar f(K)$ and $\bar f((a,b))$ are disjoint, hence $\bar f((a,b)) = S^1 \setminus \bar f(K)$ which is open in $S^1$.


How can access a server (SSH access) situated under 2 layer of private network from a PC having public IP?

Question: I need to access (SSH) my client's server (Ubuntu Server 18.04) to provide support. But the server in client location is situated in private network.
I could have connected with port forwarding in client's router but even they got private IP from ISP. So if I want to connect to the server I need to arrange port forwarding in both client's router and their ISP's router or I need to ask my client to buy a public IP.
Is there any solution to overcome this situation (installing a software/package in server, using reverse proxy server, using something like reverse tcp payload etc) so that they don't have to buy public IP or port forward at ISP's router?
Please note that I have public IP, so it is possible to directly reach my PC from the server. Below file is a image containing visual representation of client's network.
https://file.io/1DIu2I
Any help would be really appreciated.
Comment: The proper solution for this is to get a public IP on the machine you need to reach. With IPv6 there will be enough IP addresses to remove all need for NAT.
Answer: What you need is Reverse SSH, a term used to create a secure tunnel from server A to your machine B. Note that this connection has to be initiated from the server A. Basically something like:

ssh -R 3011:localhost:22 user@B

B is your machine in case. Since you mentioned that you have a public IP, the server can access your machine. Once the authentication is successful, there will be a secure tunnel through which you can ssh back into the server A.

ssh -p 3011 user@localhost

By first look, it might seem that you are accessing your own machine (localhost) but you actually are accessing the server A through the tunnel created in first step. You can read more about this at: https://blog.devolutions.net/2017/3/what-is-reverse-ssh-port-forwarding which also mentions some additional flags which are not necessary, but good to know in some cases.
PS. The link to gofile.io is not accessible
Comment: Thanks for the answer. Indeed this is very helpful. Tons of thanks!
PS: I've edited the image link.
Comment: If the answer is correct, would you mind accepting it for others to trust in the answer. And also, that link is still not valid, gives 403 error, fyi
Comment: Apologies. Image link is updated again. I'm new to this platform so I don't have any idea on how to accept the answer. Would you mind guiding me on that please?
Comment: @TamalBiswas look here: https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work


Imagens grandes corrompidas ou truncadas ao puxar do SQL server com PHP

Question: Estou buscando imagens do banco SQL-server através do PHP e jQuery+AJAX. A imagem quando é pequena carrega tudo completinho, mas quando a imagem é um pouco grande da esse erro no JavaScript:

Image corrupt or truncated: data:image/jpg;base64,/9j/4TSPRXhpZgAASUkqAAgAAAARAA4BAgAgAAAA2gAAAA8BAgAFAAAA+gAAABAB...

E a imagem fica conforme abaixo, já fiz um teste abrindo o arquivo direto uma pagina PHP sem o uso do jQuery+AJAX e dá o mesmo problema, então eu creio que o problema seja no PHP ou no SQL-server.

mssql_connect('192.168.1.xxx', 'sa', 'senhaDoBanco');
mssql_select_db('database');
$query = "exec sp_funcionarioDetalhes";
$result = mssql_query($query);
$obj = mssql_fetch_object($result);
header("Content-type: image/jpeg");
echo $array[1]->fun_foto;

Vou ser mais técnico, talvez assim alguém dê mais importância ao problema.
vou passar todos os códigos.
arquivo funcionarioDetalhes.html
<?php
/*
 * Maison K. Sakamoto 03/11/2014 - Funcionário Detalhes
 */
session_start('login');
if(!@$_SESSION["conectado"]=='sim'){
        echo "";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Sistemas BTR - Transportes</title>
    <link rel="stylesheet" type="text/css" media="screen" href="../css/grid/style.css" />

    <fieldset class="field-principal ui-widget ui-widget-content ui-corner-all">
        <legend class="ui-widget ui-widget-header ui-corner-all">Funcionário - Detalhes</legend>

            <fieldset class="ui-widget ui-widget-content ui-corner-all listaFuncionario">
                <legend>NOME</legend>

            </fieldset>

            <fieldset class="grid_2 alpha coluna1"> <legend>FILIAL</legend>     <input id="fun_filial" type="text" readonly=""/> </fieldset>
            <fieldset class="grid_2 alpha coluna1"> <legend>NASCIMENTO</legend> <input id="fun_dt_nascimento" type="text" readonly=""/> </fieldset>
            <fieldset class="grid_2 alpha coluna1"> <legend>ADMISSÃO</legend>   <input id="fun_dt_admissao" type="text" readonly=""/> </fieldset>
            <fieldset class="grid_2 alpha coluna1"> <legend>DEMISSÃO</legend>   <input id="fun_dt_demissao" type="text" readonly=""/> </fieldset>
            <fieldset class="grid_2 alpha coluna1"> <legend>FÉRIAS</legend>     <input id="fun_dt_ferias" type="text" readonly=""/> </fieldset>

            <fieldset class="grid_2 alpha coluna1"> <legend>CPF</legend>        <input id="fun_cpf" type="text" /> </fieldset>
            <fieldset class="grid_2 alpha coluna1"> <legend>PIS</legend>        <input id="fun_pis" type="text" readonly=""/> </fieldset>
            <fieldset class="grid_2 alpha coluna1"> <legend>CTPS</legend>       <input id="fun_ctps" type="text" readonly=""/> </fieldset>
            <fieldset class="grid_2 alpha coluna1"> <legend>SALÁRIO</legend>    <input id="fun_salario" type="text" readonly=""/> </fieldset>

            <fieldset class="grid_4 alpha coluna1"> <legend>ENDEREÇO</legend>   <textarea id="fun_endereco" readonly=""/> </fieldset>
            <fieldset class="grid_2 alpha coluna1"> <legend>CEP</legend>        <input id="fun_cep" type="text" readonly=""/> </fieldset>
            <fieldset class="grid_2 alpha coluna1"> <legend>CIDADE</legend>     <input id="fun_cidade" type="text" readonly=""> </fieldset>
            <fieldset class="grid_4 alpha coluna1"> <legend>CARGO</legend>     <input id="fun_cargo" type="text" readonly=""> </fieldset>

            <fieldset class="grid_8 alpha coluna1"> <legend>AÇÕES</legend>
                <button id="btGravar" >Gravar Alteração</button>
                <button id="btListaFerias">Imprimir Lista de Férias</button>
                <button id="btDadosFun">Imprimir Dados do Funcionário</button>
            </fieldset>

            <fieldset class="grid_2 alpha coluna1 foto"> <legend>FOTO</legend>

 </fieldset>

    </fieldset>

<link rel="stylesheet" href="../js/jqwidgets-ver3.4.0/jqwidgets/styles/jqx.base.css"    type="text/css" />

Arquivo funcionarioDetalhes.js
    /*
 * MAISON K. SAKAMOTO - 03/11/2014 - CRIAÇÃO DO ARQUIVO
 */

var fun= {};

fun.start=function(){
    $('button').button();
    $('#btGravar').click(function(){ });
    $('#btListaFerias').click(function(){ });
    $('#btDadosFun').click(function(){ });
    fun.buscarFuncionarios();
};

fun.buscarFuncionarios=function(){
    var array = custom.ajax(new Object(),'buscarFuncionarios','../view/rh/vFuncionarioDetalhes.php');
    fun.setListaFuncionarios(array);
};

fun.setListaFuncionarios=function(json){
    $("#div_fun").jqxListBox({
            source: json, width: 280, height: 500, selectedIndex: 0,
            displayMember: 'fun_nome', valueMember: 'fun_id'
    });
    $("#div_fun").bind('select', function(event){
        if (event.args){
            var obj = event.args.item.originalItem;
            fun.preencheForm(obj);
        }
    });
    var t = $("#div_fun").jqxListBox('getSelectedItem');
    fun.preencheForm(t.originalItem);
};

fun.preencheForm = function(obj){
    $("#fun_filial").val( obj.unidade );
    $("#fun_dt_nascimento").val( obj.fun_dt_nascimento );
    $("#fun_dt_admissao").val( obj.fun_dt_admissao );
    $("#fun_dt_demissao").val( obj.fun_dt_demissao );
    $("#fun_dt_ferias").val( obj.fun_dt_ferias );
    $("#fun_cargo").val( obj.fun_cargo );
    $("#fun_cpf").val( obj.fun_cpf );
    $("#fun_pis").val( obj.fun_pis );
    $("#fun_ctps").val( obj.fun_ctps_numero + "" +obj.fun_ctps_serie+ " " +obj.fun_ctps_uf);
    $("#fun_salario").val( obj.fun_salario );
    $("#fun_endereco").val( obj.fun_endereco +' \r\nNº '+obj.fun_n_endereco+' \r\n'+obj.fun_bairro );
    $("#fun_cep").val( obj.fun_cep );
    $("#fun_cidade").val( obj.fun_cidade );
    //$("#fun_foto").empty().append( $('').attr('src',"data:image/png;base64," + obj.fun_foto) );
    var img = $('').attr('src',"data:image/jpg;base64," + obj.fun_foto);
    $("#fun_foto").empty().append( img );
};

fun.start();

Arquivo vFuncionario.php (viewer)
include_once '../../controller/OpenDB.php';
include_once '../../controller/ColFuncionario.php';

@session_start('login');

$colFuncionario = new ColFuncionario();
$c = new OpenDB();                                                  // CLASSE DO BANCO DE DADOS

$funcao = $_REQUEST['funcao'];
call_user_func($funcao);

function buscarFuncionarios(){
    global $colFuncionario;
    global $c;
    $c->conCordilheira();
    $array = $colFuncionario->buscarFuncionarios();
    echo json_encode( $array );
}

Arquivo ColFuncionario.php ( controller )
    <?php
/*
 * Autor: Maison K. Sakamoto
 * Revisao: 0
 * Data: 25/07/2012
 *
 * Descricao:
 * Controle de Funcionario
 */
class ColFuncionario{

        public function __construct(){

        }

        public function inserirFuncionario($obj){
            //STRTOUPPER RESOLVIDO PROBLEMA DE ACENTUAÇÃO EM MAIUSCULAS
            $query = "insert into tab_funcionario(cargo_id,func_nome,func_hab,func_hab_venc,func_cpf) values
                ($obj->cargo_id,'".strtoupper(strtr($obj->func_nome,"áéíóúâêôãõàèìòùç","ÁÉÍÓÚÂÊÔÃÕÀÈÌÒÙÇ")).
                    "','$obj->func_hab',STR_TO_DATE('$obj->func_hab_venc','%d/%m/%Y'),'$obj->func_cpf')";
            mysql_query($query);
            return "mysql_info: ".mysql_info()."
"."mysql_error: ".mysql_error()."
query: ".$query;
        }

        public function getCargos(){
            $query = "select * from tab_cargo";
            $result = mysql_query($query);
            while($obj = mysql_fetch_object($result)){
                $array[] = $obj;
            }
            return $array;
        }

        public function getMotoristas(){
            $query = "select * from tab_funcionario where cargo_id = 1 and func_inativo is null ORDER BY func_nome";
            $result = mysql_query($query);
            while($obj = mysql_fetch_object($result)){
                $array[] = $obj;
            }
            return $array;
        }

        // BUSCA FUNCIONARIOS DO SQL-SERVER BANCO DE DADOS CORDILHEIRA
        public function buscarFuncionarios(){
            //ini_set('mssql.charset', 'UTF-8');
            $query = "exec sp_funcionarioDetalhes";
            $result = mssql_query($query);
            while($obj = mssql_fetch_object($result)){
                $obj->fun_nome = mb_convert_encoding($obj->fun_nome, 'UTF-8');
                $obj->fun_cidade = mb_convert_encoding($obj->fun_cidade, 'UTF-8');
                $obj->unidade = mb_convert_encoding($obj->unidade, 'UTF-8');
                $obj->fun_endereco = mb_convert_encoding($obj->fun_endereco, 'UTF-8');
                $obj->fun_bairro = mb_convert_encoding($obj->fun_bairro, 'UTF-8');
                $obj->fun_cargo = mb_convert_encoding($obj->fun_cargo, 'UTF-8');
                $obj->fun_foto = base64_encode( $obj->fun_foto);
                $array[] = $obj;
            }
            return $array;
        }
}

Arquivo OpenDB.php
class OpenDB{
    public function conCordilheira(){

        //1 passo - Conecta ao servidor SQL 2008 R2 Cordilheira
        $conn = mssql_connect('192.168.1.xxx', 'sa', 'senhaDoBanco');
                mssql_select_db('databaseDoMeuSqlServer');

        //$conn = sqlsrv_connect( $serverName, $connectionInfo);

        if( $conn ) {
            return $conn;
             //echo "Connection established.
";
        }else{
             echo "Connection could not be established.
";
             die( print_r(mssql_erro(), true));
        }

    }
}

resultado
Answer: Não tenho experiência com MSSQL, mas você pode tentar alterar os valores dessas três diretivas no PHP.INI, preferencialmente uma por vez e ver qual delas realmente funcionou, assim, caso solucione, você mesmo ajudará a comunidade.
mssql.textsize  = 2147483647
mssql.textlimit = 2147483647
odbc.defaultlrl = "100K"

Uma consideração a ser feita com respeito a terceira opção é que, pela documentação, o valor a ser passado é um inteiro, representando o número de bytes que serão passados para as variáveis quando campos longos forem lidos porém, de acordo com essa resposta no SOEN, em tempo de execução, você pode definir uma string.
Para definir um valor a uma diretiva do PHP em tempo de execução, use ini_set():
ini_set("odbc.defaultlrl", "100K");

No exemplo acima ficou definido 100K que seriam 96K a mais que o padrão.
Vale salientar que as outras duas diretivas já estão nos valores máximos permitidos e que podem não ser alteradas via ini_set() em versões antigas do PHP.
Espero que ajude
Comment: ja vou testar esses param 1 mim.
Comment: não deu certo :(
Comment: Só pra constar, as duas diretivas que precisavam ser feitas no PHP.INI foram descomentadas e depois de alteradas você reiniciou o servidor, certo? E a outra, você invocou ini_set() antes de fazer a consulta ao campo em questão, certo?
Comment: agora deu certo, testei certinho p/ ver qual parametro que faz a magica, é o mssql.textsize, eu não sabia o tamanho das imagens, mas tem mais de 2mb cada uma, e eu puxava num array de sei lá quantos funcionários, agora vou arrumar no php p/ diminuir essas imagens antes de enviar via ajax p/ pagina, valeu mesmo.
Answer: Como funciona para imagens pequenas, pode ser problema de memória, tente aumentar a memoria disponível para o PHP e seu banco.
Aumentar memoria php.ini
memory_limit = 64M

.htaccess
php_value_memory_limit 64M

Tente aumentar a memória de alguma forma e teste o código outra vez, quem sabe realmente é isso.
Comment: vou ver e já te respondo
Comment: os dois parametros já esta com 128M
Comment: Coloca mais que isso, acho que 128 é pouco. Não custa tentar.


How to order VST plugins correctly

Question: I have recently noticed that the order of the VST plugins on an audio track can make a great difference on its sound.
I usually use virtual preamps, compressors, distortion and chorus effects, equalizers, reverb and delay. I also work with MIDI tracks.
So are there general rules or tips on how to organize VSTs? How does the order affect the sound exactly? How about track folders, as they already add an effect on all the sub-tracks?
I would like to know if there's a way to arrange VSTs properly, so that every plugin uses its full potential.
Answer:
Are there general rules or tips on how to organize VSTs? (...) I would like to know if there's a way to arrange VSTs properly, so that every plugin uses its full potential.

There's no correct order. It's all about preference, what you want to achieve, and the context.
You'll find a lot of suggestions, like putting time-based effects (like delay or reverb) at the end , and compressors at the start. These make a lot of sense in some scenarios, but shouldn't be taken as absolute rules.
This page, for example, suggests this order:

filter -> dynamics -> distortion -> eq -> pitch -> modulation -> gain -> time

This page suggests this order:

utility -> filters -> intelligent processor -> dynamics -> gain -> time ->

Both are very similar. That pattern is very common for electric guitars (and it's the most commonly recommended pattern), but what about other contexts?
In mastering, if you are going for 0dBFS, you definitely want the limiter at the very end of the chain, so it would be a big mistake to implement this pattern there. In mixing you will want some processes in parallel rather than in series (more on that later), and you might want to compress and eq each effect separately. For synths it's very common to put a filter at or near the end of the chain, after distortion. Guitarists break that pattern all the time.
Stop thinking about "what's correct". If you have a sound in mind, start thinking about what you need to achieve it. If you are not sure or you are experimenting, switch the order of things and see what you like most.
Another important thing to consider is not only the order, but the way you are chaining everything. You can go in series or parallel. In series you connect the output of one effect to the input of the other. In parallel you send the signal to two (or more) effects to process it separately.
Series:

Parallel:

How does the order affect the sound exactly?

One effect will process what the previous one feeds to it. Think about a gain and wave shaper chain. With the gain before the wave shaper, the gain will also control how much the signal is distorted, it is also functioning as a "wave shaper amount". With the gain after the wave shaper you only control the volume of the output of the wave shaper, the gain can't affect the amount of distortion induced by the wave shaper.
Or think about a pitch corrector and a pitch shifter. If you put the corrector before the shifter then you can go wherever you want with the shifter, but if you put the corrector after the shifter the corrector will try to correct whatever you are doing with the shifter, will try to put the frequency of the signal where it thinks it should be.
To know what will happen, you also need to know what the effects do. Once you know exactly what the effects do, then knowing what your chain is doing becomes easier. Your chain and order is explicitly and literally telling you "I'm doing this, then that, then that, and finally that".

How about track folders, as they already add an effect on all the sub-tracks?

It's pretty much the same. The only difference is that you are feeding the sum of all those tracks to the effects chain, instead of one just track.

Related questions:
Why does pedal order matter?
Where to apply my effects pedal?
Where should I put delay pedal, before reverb or after?
Answer: There are basically two classes of effects: linear ones and non-linear ones.  From an idealized signal processing view point (discounting noise and quantization), the order of linear effects can be interchanged without a net change in the result.  A lot of effects are also time-invariant: start your signal an arbitrary time later, and the result will be the same, just coming late by the same amount of time.
Notable effects that are not time invariant are Leslies and Wahwahs since they come with their own independent timing.
Effects that are both linear as well as time-invariant include a lot of tone adjustments (like a graphical equalizer) and most delays/reverbs.
So the basic setup boils down to how one arranges the non-linear elements in between linear blocks.
One non-linear element are dynamic compressors.  Another one are distortions.  Do we want dynamic compressors before or after distortion?  Depends on our goals.
The net result will be a tone with stabilized volume.  Putting the the dynamic compressor before the distortion will feed the distortion with stabilized volume, leading to a constant distortion quality for sustained tones.  Putting it behind the distortion will feed the distortion with the raw volume, leading to a timing- and strength-determined distortion, giving a sound quality that changes over time and can be stronger influenced.  Both make sense for some settings.
Now reverb generally wants to give your sound ambience.  Putting any nonlinear effect like a distortion after reverb will put artifacts in that ambience that "don't make sense".  On the other hand, some kind of delay line play might want to suggest chorus effects in which case it might make sense to put the delay lines before some nonlinear devices.
And so on.  A graphical equalizer before a distortion device will not do much useful since clean sound has a pretty sparse frequency spectrum, so you approximately only meddle with the balance of the strings, and the changes in tone characteristics (sound getting brighter or duller) really get lost in the distortion.  After the distortion device, a graphical equilizer has much more material to work with and thus is more effective for shaping a particular sound.
So basically the key is to experiment and see which combinations lead to useful changes, and which don't.
Comment: Lots of great points. Two remarks: **1.** linear does not actually imply that effects [commute](http://en.wikipedia.org/wiki/Commutator), to use the mathematical term; only linear operators with a common eigenbasis commute. That is indeed given for time-invariant effects, but a wah-wah and a delay will _not_ commute. **2.** You're right that a equaliser before distortion won't shape as much and an obviously the result character, but still it's often very useful, for instance boosting higher mids so the low harmonics won't be too dominant in the distortion.
Answer: Put generally, EQ in front of a compressor will change the way the compressor engages.  This might be a good thing!  Say you have an acoustic guitar track that's got some rumble in it.  You put it through a comp->EQ and you're fighting it all the way.  You put it through a high-pass EQ first to take the undesirable LF away first, and you get a far more musical result from your comp.
In general:  do your forensic/surgical EQ pre-comp, do your overall broad shaping EQ after the comp.  This rule may be broken constantly, but it's a good place to start.
In general: do reverbs later in the chain than sooner...they're much more fragile and can be dramatically altered by post-processing.

How to order VST plugins correctly


Determining the Lie algebra $\frak g$ to a given Lie group $G$

Question: In an old exercise sheet there was the following Lie group:
$$G = \left\{ \begin{pmatrix} a & b \\ 0 & a^{-1}\end{pmatrix} \Big|\ a> 0\ \text{and}\ b\in \mathbb R\right\}$$
and in order to see whether i understood things correctly, I wanted to determine its corresponding Lie algebra $\frak g$. However, I don't exactly know how to approach this problem.
What I've tried:
My first naive approach was to consider $G$ as a Lie subgroup of $\operatorname{GL}_2(\mathbb R)$ and use the one-parameter subgroup $$\gamma(t) = e^{tA}$$ since $$A \in {\frak{g}} \iff e^{tA} \in G\quad \forall t\in \mathbb R$$
However, I'm not sure how to proceed from here since, unlike in the examples of the common Lie subgroups $O(n), \operatorname{SO}(n)$ or $\operatorname{GL}_n(\mathbb R)$, I don't see a straight forward property of elements of $G$ that would be of help in order to use the exponential map accordingly.

My second thought was choosing a one-parameter subgroup $$\gamma\colon \mathbb R\to G$$ s.t. $\gamma(0) = E$, one possible one-parameter subgroup would be $$\gamma(t) = \begin{pmatrix} 1+t & 0 \\ 0 & (1+t)^{-1}\end{pmatrix}$$
Now clearly $\gamma(0) = E$. But now I'm not entirely sure. I thought about considering $\gamma'(0)$ in order to obtain a tangent vector at $E$ and then determine the unique corresponding left-invariant vector field $\widetilde X$ via $$\widetilde X(H) = dL_H(\gamma'(0))$$ where $H$ denotes a fixed but arbitrary element of $G$ and $dL_H$ is the push-forward (the differential) of the left-translation map $$L_H\colon G\to G,\ A\mapsto HA$$
My question:
Would my second attempt somehow be reasonable? Or do I confuse some things?
Additionally, I'd like to ask whether someone could give me a hint regarding my first approach (embedding $G\hookrightarrow \operatorname{GL_2}(\mathbb R)$)
Thanks for any help.
Answer: You can just think of $\mathfrak{g}$ as the tangent space to $G$ at the identity. What do you do then? You consider $$\begin{pmatrix} a & b \\ 0 & a^{-1} \end{pmatrix}$$with $a>0$ and $b\in \Bbb R$, pretend that $a$ and $b$ depend on a parameter $t$, assume that $a(0)=1$ and $b(0)=0$, write (say) $\dot{a}(0)= r$ and $\dot{b}(0)=s$ with $r,s\in \Bbb R$, and take the derivative with respect to $t$ at $t=0$ to obtain a generic element of $\mathfrak{g}$. Since $(a^{-1})^{\boldsymbol \cdot} = -\dot{a}a^{-2}$, it follows that $$\mathfrak{g} \cong \left\{ \begin{pmatrix} r & s \\ 0 & -r \end{pmatrix} \mid r,s\in \Bbb R\right\}.$$
Your $G$ is a subgroup of ${\rm GL}(2,\Bbb R)$, so $\mathfrak{g}$ is a subalgebra of $\mathfrak{gl}(2,\Bbb R)$. In particular, $\exp^G = \exp^{{\rm GL(2,\Bbb R)}}\big|_{\mathfrak{g}}$.
Comment: Yes, exactly, but $r$ and $s$ are the derivatives evaluated at $t=0$. Each Lie group has its own exponential map. I am saying that the exponential map of the group $G$ equals the exponential map of the ${\rm GL}(2,\Bbb R)$ restricted to $\mathfrak{g}$. Your second approach requires a lot more of work, because you'd have to do it for one-parameter subgroups realizing all initial velocities in $T_{{\rm Id}_2}G$, but you chose a single one which has no reason to be special at all.
Comment: To elaborate further, what you tried to do was what I did with $a(t)=1+rt$ and $b(t)=st$. But the curves $a$ and $b$ don't need to define together a one-parameter subgroup. We're computing a single tangent space to $G$ and the value of the differential of a function at a single tangent vector doesn't depend on the curve chosen to realize such vector.
Comment: Wow, thanks so much. Your help is greatly appreciated. I will try to work it out appropriately and maybe return to your answer in the next couple of hours or at latest tomorrow. Please give me some additional time to fully appreciate what you did but i will gladly accept your answer eventually. Thanks so much!
Comment: Good luck! Let me know if you get stuck on anything.
Comment: I don't see any quick solution with exp right now. But if you know what a generic element of $G$ looks like, it doesn't matter, the strategy from the answer should always work.
Comment: $a(0)=1$ is crucial to say that $(a^{-1})^{\cdot}(0)= -r$. In this case, $b(0)=0$ doesn't matter (but if you don't have it, your original curve doesn't start at the identity matrix).
Comment: Thanks for your help Ivo Terek. Did i understand you correctly that you take the derivative of $\begin{pmatrix} a(t) & b(t) \\ 0 & a^{-1}(t)\end{pmatrix}$ with respect to $t$ and denote $\dot{a}(t)$ by $r$ and $\dot{b}(t)$ by $s$?

Furthermore, what does $\exp^G = \exp^{\operatorname{GL(2,\mathbb R)}}\Big|_{\frak g}$ mean?

Would my second approach in my question somehow work?
Comment: Thanks again Ivo Terek, your solution provides great insights, i worked it out and gained some new perspective. I was just wondering, would the approach via the exponential map (the first attempt above) be reasonable for this problem, too? Do you happen to have a hint regarding how to tackle a problem like this via the exponential map if I don't see a particular defining property like for, say orthogonal matrices which satisy $A^TA = E$ which we can throw into $\exp(\cdot)$ as usual?
Comment: Beautiful, thanks so much!
Comment: I am trying to follow your answer, but totally lost in the general process. I presume this is a bit of a template on an easy example of how to find the Lie algebra from the Lie group in more general cases. The step "assume that $a(0)=1$ and $b(0)=0$ is something you can always do with the parameters? Is there some rule-of-thumb? Thank you!
Comment: $a(0) = 1$ and $b(0)=0$ make the whole initial matrix (i.e., at time zero) equal to the identity matrix. And $\mathfrak{g}$ is the tangent space to $G$ at the identity.
Comment: Thank you very much! I get it now! However, this doesn't seem to play any role in the actual process of getting the Lie algebra matrix, which starts at the definition of $r$ and $s$ as the derivatives, am I right?
Comment: (I will erase  at least one or two of these last comments, no worries about clutter) - I think I get it! Without $a(0)=1$ the expression $(a^{-1})^{\boldsymbol \cdot} = -\dot{a}a^{-2}$ would become a problem with a $0$ in the denominator. Is that it? It act like a check that the expression derived is well defined?


Change in order of predictors breaks logistic model estimation (glm, R)

Question: I am fitting a binomial logistic regression in R using glm. By chance, I have found out that if I change the order of my predictor variables, glm fails to estimate the model. The message I get is  unexpected result from lpSolveAPI for primal test.
I am using the safeBinaryRegression package, so I am confident there are no separation issues between my outcome and predictor variables. However, I am not so confident that there are no quasi-separation issues among my predictor variables. Am I correct that if this is the case, then I might be running into multicolinearity, and this is the source of glm not being able to fit the model?
If so, my question is for advice on how to approach the issue. Should I look for the predictor variables highly correlated and omit one of them? Is there any convenient way of doing so for 11 categorical predictors?
What I see right now:
lModel <- glm(mob_change ~ education + gender + start_age +
           income + dist_change + lu_change + dou_change +
           marriage + student2work + wh_change,
           data = regression_data,
           family = binomial())
# Fine, and I can inspect the model. No predictor has
# std. error > 1.05

# Now if I move the last variable (or any of the last three,
# for what I've tested) to
# be the first in predictor ...
lModel.3 <- glm(mob_change ~ wh_change + gender + education +
                start_age + income + dist_change + lu_change +
                dou_change + marriage + student2work,
            data = regression_data,
            family = binomial())

Error in separator(X, Y, purpose = "find") :
  unexpected result from lpSolveAPI for primal test

Comment: `safeBinaryRegression` changes the `glm` function from `stat` to test for separation first & it's this test that's giving an error message for some reason - not even telling you whether there's separation or not. Of course the order of the predictors is irrelevant to the question.
Comment: What are the form of your independent variables?  All categorical, all continuous, or a mix?
Comment: All but `start_age` are categorical, Andrew
Answer: The order the predictors are entered into the model is of course irrelevant to the question of whether there's separation in the data. The safeBinaryRegression package† masks the usual glm function from the stats package, which fits generalized linear models, so that, for logistic regression, glm uses a linear programming algorithm to check for both complete & quasi-complete separation before trying to fit anything. If it finds separation it reports

Separation exists among the sample points.

or

The following terms are causing separation among the sample points:

depending on whether you've asked it just to test for separation or to find the predictors causing it. Otherwise it reports nothing.

Unexpected result from lpSolveAPI for primal test

,however, is a software error message, not a statistical one. You could perhaps try on a machine with more memory, but it's probably safe to trust the results from when you didn't get an error. Using stats:::glm (i.e using the glm function from stats when safeBinaryRegression's loaded) should reach the same results regardless of the order of predictors; it will often report non-convergence or predicted probabilities of nought or one in cases of separation.
Multicollinearity among the predictors is another issue entirely. Generalized variance inflation factors (see the vif function‡ from the car  package) are useful for assessing its extent in models with more than one degree of freedom per predictor.
† Konis (2007), "Linear programming algorithms for detecting separated data in binary logistic regression models", DPhil., U. Oxf.
‡ Fox & Monette (1992), "Generalized collinearity diagnostics", JASA, 87, pp178–183.
Comment: I think the use of prior information is a great idea.  It's just that ignoring prior information (as we often do when there is not complete separation) doesn't hurt as much as some think.
Comment: I am confused about whether separation is worth special treatment.  Regression coefficients that go to $\infty$ are correct, and provide the correct predictions.  It is just that their standard errors are useless and one must use the likelihood ratio $\chi^2$ test instead of Wald tests in this situation.
Comment: @FrankHarrell: I listed some of the special treatments [here](http://stats.stackexchange.com/questions/11109/); perhaps "Do nothing" should've been at the top of the list. I suppose the "fixes" all involve priors, explicit or implicit, that cast doubt on very high coefficient estimates, & when that's reasonable, better estimates & predictive performance are to be expected.
Comment: I've tried the `safeBinaryRegression`, and it does not complain. I was suspecting this had something to do with separation by inspecting the part of the source code where the message comes from. But I indeed agree this seems like an error in the optimization routine, and not in the statistics. It is just unfortunate I cannot find other people with the same error anywhere...


Retornar tags associadas aos posts

Question: Eu uso a query abaixo para retornar todos os posts marcados com a tag definida:
SELECT imprensa_posts.slug AS slug, tipo, titulo, data
FROM imprensa_posts
JOIN imprensa_tags_posts ON imprensa_tags_posts.id_post = imprensa_posts.id_post
JOIN imprensa_tags ON imprensa_tags.id_tag = imprensa_tags_posts.id_tag
WHERE imprensa_tags.slug = 'tag-4'
ORDER BY data DESC

Mas preciso também pegar todas as tags associadas a cada post retornado. Tentei dessa forma:
SELECT imprensa_posts.slug AS slug, tipo, titulo, data,
GROUP_CONCAT(imprensa_tags.slug) AS tag_slugs, GROUP_CONCAT(tag) AS tags
FROM imprensa_posts
JOIN imprensa_tags_posts ON imprensa_tags_posts.id_post = imprensa_posts.id_post
JOIN imprensa_tags ON imprensa_tags.id_tag = imprensa_tags_posts.id_tag
WHERE imprensa_tags.slug = 'tag-4'
GROUP BY imprensa_posts.id_post
ORDER BY data DESC

Mas não funciona. A query retorna tag-4 para todos os posts.
UPDATE
Recriei as tabelas no SQL Fiddle: http://sqlfiddle.com/#!9/3c04b/5
Answer: E se você fizer algo assim
SELECT imprensa_posts.slug AS slug, tipo, titulo, data,
GROUP_CONCAT(imprensa_tags.slug) AS tag_slugs, GROUP_CONCAT(tag) AS tags
FROM imprensa_posts
JOIN imprensa_tags_posts ON imprensa_tags_posts.id_post = imprensa_posts.id_post
JOIN imprensa_tags ON imprensa_tags.id_tag = imprensa_tags_posts.id_tag
WHERE EXISTS (SELECT *
              FROM imprensa_tags it
              JOIN imprensa_tags_posts itp ON it.id_tag = itp.id_tag
              WHERE slug = 'tag-4' AND itp.id_post = imprensa_posts.id_post)
GROUP BY imprensa_posts.id_post
ORDER BY data DESC

O segredo está no EXISTS
EXISTS (SELECT *
        FROM imprensa_tags it
        JOIN imprensa_tags_posts itp ON it.id_tag = itp.id_tag
        WHERE slug = 'tag-4' AND itp.id_post = imprensa_posts.id_post )

Ele só vai retornar aqueles que existerem essa tag relacionada.
-
Outra solução que pensei
SELECT imprensa_posts.slug AS slug, tipo, titulo, data,
       (SELECT GROUP_CONCAT(slug)
        FROM imprensa_tags it
        JOIN imprensa_tags_posts itp ON it.id_tag = itp.id_tag
        WHERE itp.id_post = imprensa_posts.id_post) AS tag_slugs,
       (SELECT GROUP_CONCAT(tag)
        FROM imprensa_tags it
        JOIN imprensa_tags_posts itp ON it.id_tag = itp.id_tag
        WHERE itp.id_post = imprensa_posts.id_post) AS tags
FROM imprensa_posts
JOIN imprensa_tags_posts ON imprensa_tags_posts.id_post = imprensa_posts.id_post
JOIN imprensa_tags ON imprensa_tags.id_tag = imprensa_tags_posts.id_tag
WHERE imprensa_tags.slug = 'tag-4'
GROUP BY imprensa_posts.id_post
ORDER BY data DESC

Comment: Tem alguma coisa errada: Error Code: 1054. Unknown column 'itg.id_post' in 'where clause'
Comment: Está retornando o mesmo resultado. Ambas as queries funcionam corretamente com exceção de um ponto: se determinado post está associado a uma tag que não é a informada no WHERE, esta não é mostrada nos GROUP_CONCATs
Comment: @marcelo2605 mas você não quer filtrar os posts somente aos que possuem a `'tag-4'` relacionado?
Comment: @marcelo2605 Acho que entendi o que você quis dizer, vou alterar pera.
Comment: @marcelo2605 Alterei o SELECT do `EXISTS`, da uma checada
Comment: @marcelo2605 era `itp`, já corrigi.
Comment: Está retornando a mesma coisa. Ele retorna todos os posts vinculados a tag-4. Mas se o post está vinculado também a outra tag, esta na aparece nos GROUP_CONCATs
Comment: @marcelo2605 Você executou exatamente o exemplo que sugeri?
Comment: @marcelo2605 Coloquei outra solução com 2 `sub select`, da uma verificada.
Comment: Mesma coisa. Vou criar um SQL Fiddle e posto aqui.
Comment: @marcelo2605 Vai ajudar bastante :/
Comment: Coloquei na pergunta. Rode a primeira query e veja que o Release 2 está vinculado a duas tags (tag 1 e tag 7). Mas quando eu rodo a segunda query, só aparece a tag 1.
Comment: @marcelo2605 o primeiro select que eu fiz com `EXISTS` está correto não? aqui trouxe certo tag1 e tag7
Comment: @marcelo2605 Arrumei o com 2 `sub select`, agora está retornando igual o primeiro
Comment: Funcionou! Muito obrigado.

Retornar tags associadas aos posts


Usability of a button inside a UICollectionViewCell?

Question: I have a ProductVC.swift (ProductViewController) file and a ProductCell.swift. The ProductVC contains a UICollectinView and ProductCell is a specific UICollectionViewCell.
ProductCell.xib looks like this:

ProductVC contains an array with all the cell data (products) and populates the cells.
My goal: The user should have the possibility to like an product. He can do it by clicking the like button on the top right corner of every cell. Every cell shows a specific product which is specified by a productID.
My Problem: The like button action (IBAction func) is in the ProductCell. ProductCell doesn´t have the cell data. Cell data is stored in ProductVC in an array. So I don´t know how catch the product(productID) the user wants to like.
My Tries: With the code below I can get the indexPath of the cell where the user clicked the like button. But I can´t use this indexPath to get the product data because the data is stored in ProductVC. I could also store the data in ProductCell but it is not a clean way. Is it possible mb to give this indexPath to the ProductVC?
extension UICollectionView {
    func indexPathForView(_ view: UIView) -> IndexPath? {
        let center = view.center
        let viewCenter = self.convert(center, from: view.superview)
        let indexPath = self.indexPathForItem(at: viewCenter)
        return indexPath
    }
}

let superview = self.superview as! UICollectionView

if let indexPath = superview.indexPathForView(button) {
    print(indexPath) // indexPath of the cell where the button was pressed
}

SOLVED Solution is a callback closure:
//UICollectionViewCell
var saveProductLike: ((_ index: Int) -> Void)?
@IBAction func likedButtonClicked(_ sender: UIButton) {
    print("Liked button clicked!")
    let productArrayIndex = calculateProductArrayIndex(for: sender)
    saveProductLike?(productArrayIndex!)
}

//UIViewController
cell.saveProductLike = { (index) -> Void in
    print(index)
}

Comment: Do you really believe no one has ever wondered about this before? Really? I don't think so. So please search before asking. This is a well-known problem and is already well solved; there is no need to ask about it again. Good survey of techniques here: https://stackoverflow.com/questions/9274494/how-to-know-the-uitableview-row-number Nothing much has changed since then (except maybe the language).
Comment: If you are iOS 14 only, there is one new addition to those techniques, namely, you can attach a UIAction directly to the button when you configure the cell in `cellForItem`.
Answer: There are several approaches to solve this but I'll talk about the most common one which is using delegation.
protocol ProductCellDelegate: AnyObject {
  func productCellDidPressLikeButton(_ cell: ProductCell)
}

in ProductCell define a property weak var delegate: ProductCellDelegate? and in the target action of the like button inform your delegate
@objc private likeButtonPressed(_ sender: Any) {
  delegate?.productCellDidPressLikeButton(self)
}

In your view controller you could conform to the protocol and implement it like this:
func productCellDidPressLikeButton(_ cell: ProductCell) {
  guard let ip = collectionView.indexPath(for: cell) else { return }
  // process event, get product via index...
}

Then you need to set the view controller to be the delegate in collectionView(_:willDisplay:forItemAt:) or
collectionView(_:cellForItemAt:): cell.delegate = self.
Comment: Thank you for you answer but your solution didn´t work for me. I also tryed a delegate solution but it didn´t work for me. Are you sure that your implementation is correct? I miss something like "productCell.delegate = self" in my ViewController. Anyway the problem is solved with a callback closure.
Comment: I edited my answer, of course you need to assign the protocol witness to the cell's `delegate` property


Where are the Page Manager "Caching options"?

Question: In D7 I'm using Ctools, Panels (3), Views, Page Manager etc., but in Page Manager when gear-icon-clicking the content panes I don't find any "Caching options" as I have seen described. What reasons can there be for this.
It's probably something really simple, I just can't find it...
Comment: Are you sure you are clicking on a content type, and not a region? Regions have the same gear icon, but can't have caching options.
Comment: Around 7:20-7:50 you can see how to get to the caching options for a panel pane
http://www.youtube.com/watch?v=jRoMgZTg1GE&t=7m20s
In this video the guy is removing the pane, but right above that is the selection for caching.
Answer:

Although the language is german in this screenshot, location of the menu/options should be the same.


Average distance of two points on circle vs sphere

Question: The average distance of two random points on a circle of radius $1$ is $4/\pi \approx 1.27$ (you can find this online easily).
The average distance of two points on a sphere is $4/3 \approx 1.33$.
The two problems can be stated in terms of the average distance from a particular point
on the circle or sphere equivalently.
I am trying to find the error in the following line of reasoning that gives a wrong result in the second question:
Say we are looking for the average distance to the north pole. Condition on a particular ring for the second point at a given longitude $\phi=\phi_1$. Then the average distance conditioned on the point being on that longitude is $4/\pi$ due to the first problem.
Therefore $E[distance\ on\ sphere]=E[E[distance\ on\ sphere|\phi]]=E[4/\pi]=4/\pi$.
Can you help me identify the fallacy?
Answer: What is intuitively wrong with your north pole analogy is that points nearer the equator of the sphere are more likely than your meridian ring argument would suggest.
You would be conditioning on the event that longitude $\phi=\phi_1$ of probability $0$, and that can lead to Borel–Kolmogorov paradoxes and worse: your conditional density is essentially $\frac00$.
Your ring argument led you to suppose latitude $\theta$ is uniformly distributed on $[-\frac\pi2,\frac\pi2]$ , when on a sphere it is in fact $\sin \theta$ which is uniformly distributed.  [More usually latitude is described as $\phi$ and longitude as $\lambda$ but that does not matter.]
This would not affect the answer if you were measuring distances on the circumference of the circle and surface of the sphere (the errors would cancel out by symmetry and you would get $\frac{\pi}{2}$ in both cases), but it does matter when measuring distances through the interior of the circle and sphere.
Comment: Hi. That makes sense in an intuitive basis (surely there is more mass around the equator so these should be weighted more) but I am still having issues to see the error in the argument mathematically. Once I condition on $\phi$ the problem seems to reduce to the 2D problem on circles (or maybe not?).
Comment: I have added to the middle of my answer
Comment: that's very interesting and seems to address my question. Thanks!!


Random Forests performs far less better than Naive Bayes

Question: In one of my Machine Learning courses I have to find the best predictor for this dataset and its binary target "Caesarian".
First of all, I tried to improve the datas : there are few features. I did One-Hot-Encoding on :

Delivery Time
Blood Pressure
Heart Problem (already done in the database as binary)

I normalized my datas for the Age and Delivery Number columns.
Then I did k-fold with Random Forests, Naive Bayes, GLM and some other, I tried several k for the k-fold, $k\in \{1,2,3,6,8,16\}$, and I was very surprise by the very poor performances on prediction for the Naive bayes (more than 30% error on average for the k-fold, no matter what k), and most of all, I was extremely surprise that RandomForest performs a 40% mistake on average, no matter what $k$ and with different values for the number of trees, from tens to thousands, and also different values for the number of variables ramdomly sampled as candidates at each split.
I tried Leave-One-Out to compute the error, and it's the same, the Naive Bayes model's quite bad, and still surprisingly, the Random Forest's worst than Naive Bayes.
Is there something I missed out ? I can understand that Naive Bayes can have a bad classification performance as it really depends on the datas and it's a really weak classifier, but what about RandomForest ?
So two questions now :

Random Forest aren't suppose to perform better ?
What can I do to improve them or at least understand why it's so bad ?

Here are the boxplots of the MSE I got for a 6-fold :

Note : I didn't tried to optimize Bagging yet
Answer: It depends on the "true" data generation process. If the model does not match this process, then it will not perform as well as a model (perhaps a simpler one) that does match it. So a GLM typically performs worse than a random forest, but not when the true data generation process is exactly like a linear regression.
For example: I simulate data below (using R) from a logistic regression model. A GLM has a higher accuracy (85%) than a random forest (79%) on the test set.
In general, models perform better when their form and assumptions match what truly generated the data. Our models are never perfect, but they perform well when they approximate the process close enough.
<code>library(randomForest)
inv_logit <- function(x) exp(x) / (1 + exp(x))
set.seed(1839)
n <- 1000
x1 <- rnorm(n)
x2 <- runif(n)
x3 <- rpois(n, 4.5)
y_prob <- inv_logit(
  -.2 + .3 * x1 + .1 * x2 + -.2 * x3 + # main effects
    x1 * x2 + 1.4 * x2 * x3 + .1 * x1 * x2 * x3 # interactions
)
y <- rbinom(n, 1, y_prob)

cases <- sample(seq_len(n), 700)
dat <- data.frame(x1, x2, x3, y)
train <- dat[cases, ]
test <- dat[-cases, ]

mod1 <- glm(y ~ x1 * x2 * x3, binomial, train)
pred1 <- predict(mod1, test, type = "response") > .5
sum(diag(table(test$y, pred1))) / sum(table(test$y, pred1))

mod2 <- randomForest(factor(y) ~ ., train)
pred2 <- predict(mod2, test)
sum(diag(table(test$y, pred2))) / sum(table(test$y, pred2))
</code>
Comment: Thanks ! I now understand that whatever model won't systematically work on each data, and in some cases RandomForest may not be the appropriate model, even if it's a strong classifier
Comment: I'm surprised that randomforest does so well
Comment: Bingo! That's a good way to put it
Answer: As Mark notes, how accurate you can be depends on the data.
For example, if a stock market index is really a random walk, you aren't going to be able to predict it well.
I took this data set and let it run for 30 minutes in an automated program that searches through a bunch of models. Even after that time, the crossvalidation accuracy was 72.5% (27.5% incorrect). That's not much better than Naive Bayes.
Because running the data through a fully automated procedure is not likely to be what you Machine Learning instructor wants, I'm going to skip the details.
In general, I've found Random Forests to work better than Naive Bayes, but there are certainly exceptions. Methods that never work better tend to disappear; methods that are still used tend to work well in some set of circumstances.
Answer: In my opinion, a potentially more important reason as to why random forests are doing quite poorly here is mostly due to the size of your dataset.
Your dataset is comprised of a grand total of 80 observations with five features which is incredibly small for fitting a random forest. Consider how a classification tree generates its predictions, the base learner for a random forest ensemble. The final predicted probability of being in a specific class will just be the proportion of observations (in a single terminal node of the tree) that belong to this class to the total number of observations in that same terminal node. A random forest fits many of these decision trees on bootstrap samples of your data and then takes an average of all of the trees to get the final prediction. Thus, with only 80 observations your fitted probabilities of belonging to one class or the other will be based off very few observations which will more than likely lead to large variances in your predictions (i.e. severe overfitting). Thus, it is really not surprising to see that simpler models work better here because a random forest in general requires a large amount of observations to work well. This also holds true for models such as neural networks, gradient boosting, regularized greedy forests, and other very (popular) methods that many regard as "high performing" algorithms (as of 2018, anyway).
In general, I have found that the less data you have, the less complex your models can be. Ensemble based methods and models that fit a large amount of parameters do not fare well on small amounts of data because there simply isn't enough information to estimate very complicated relationships within the data; which is in general why most people use these more complex algorithms in the first place.


Any interesting stats from hand washing data?

Question: I installed a flow meter on a faucet in one of my school's dorm buildings, with the intention of trying to gather some interesting data on patterns at the faucet. It collects flow readings on the sink, every second.
I have gathered the duration, the amount of water used, and whether they used the hot or cold faucet.
I would like to use this data to train a neural network model to identify hand-washing events from a set of data that it doesn't know the classification vector for.
What types of models (I'm using Python) could I use to train a neural network to identify patterns like this?  Also, what types of other stats aside from average wash duration time could I gather from the data?
Comment: There doesn't appear to be any explicit information about hand-washing in your data, so how do you propose to identify "hand-washing events"?
Comment: If every observed event is a hand-washing event, then you know nothing at all about *non* hand-washing events!  How, then, could you hope to discriminate between them?
Comment: So, every event in this data set is assumed to be a hand-washing event.  Could I use these events to train a model on faucets that have events that include handwashing, brushing teeth, and more?
Answer: Neural networks need targets for training. If you have data for handwashing events and for other faucet activities but you haven't labeled them as handwashing/other then there's no way of knowing which flow rates correspond to handwashing and which correspond to other things.
The most you can do is to perform cluster analysis to group data into similar sets. However, from this there is no way of telling which group is handwashing or if a handwashing group was even identified. If you had two groups you might get a group of data where people washed their hands or their clothes at a high temperature, and people who washed their hands or clothes at a low temperature.


BigQuery Temp Table Column has no Name

Question: I'm trying to create a temp table in BigQuery, something like:
CREATE TEMP TABLE myTmpTable AS
  SELECT t.event_id, MAX(t.event_date)
  FROM eventsTable t
  WHERE t.field_name = "foo"
  AND t.new_string = "bar"
  GROUP BY t.event_id;

This results in error "CREATE TABLE columns must be named, but column 2 has no name". I understand that it can't extract a column name from MAX(t.event_date). Is there a way I can specify a column name?
Comment: Is there a way I can specify a column name? - `SELECT t.event_id, MAX(t.event_date) AS max_event_date`. Meantime the whole SELECT looks wrong to me - if you group by `issue_id` then `event_id` should be somehow aggregated. Or you might want to group by `event_id` instead!
Comment: oh thanks @MikhailBerlyant - that works, I was trying something like MAX(t.event_date) "max_event_date". D'oh. If you want to post as an answer I'll accept.
Answer:
Is there a way I can specify a column name?

Use below
SELECT t.event_id, MAX(t.event_date) AS max_event_date

Meantime the whole SELECT looks wrong to me - if you group by issue_id then event_id should be somehow aggregated. Or you might want to group by event_id instead!
Comment: Thanks, yeah that was a typo, I've corrected it now to "GROUP BY t.event_id;"

BigQuery Temp Table Column has no Name