Python programming memory leak? Maybe? File search & destroy script

Question: I've got a python script that searches for files in a directory and does so infinitely while the computer is running. Here is the code:
import fnmatch
import os
import shutil
import datetime
import time
import gc
# This is a python script that removes the "conflicted" copies of
# files that dropbox creates when one computer has the same copy of
# a file as another computer.
# Written by Alexander Alvonellos
# 05/10/2012

class cleanUpConflicts:
    rootPath = 'D:\Dropbox'
    destDir = 'D:\Conflicted'
    def __init__(self):
        self.removeConflicted()
        return

    def cerr(message):
        f = open('./LOG.txt', 'a')
        date = str(datetime.datetime.now())
        s = ''
        s += date[0:19] #strip floating point
        s += ' : '
        s += str(message)
        s += '\n'
        f.write(s)
        f.close()
        del f
        del s
        del date
        return

    def removeConflicted(self):
        matches = []
        for root, dirnames, filenames in os.walk(self.rootPath):
            for filename in fnmatch.filter(filenames, '*conflicted*.*'):
                matches.append(os.path.join(root, filename))
                cerr(os.path.join(root, filename))
                shutil.move(os.path.join(root, filename), os.path.join(destDir, filename))
        del matches
            return

def main():
    while True:
        conf = cleanUpConflicts()
        gc.collect()
        del conf
        reload(os)
        reload(fnmatch)
        reload(shutil)
        time.sleep(10)
    return

main()

Anyway. There's a memory leak that adds nearly 1 meg every every ten seconds or so. I don't understand why the memory isn't being deallocated. By the end of it, this script will continuously eat gigs of memory without even trying. This is frustrating. Anyone have any tips? I've tried everything, I think.
Here's the updated version after making some of the changes that were suggested here:
import fnmatch
import os
import shutil
import datetime
import time
import gc
import re
# This is a python script that removes the "conflicted" copies of
# files that dropbox creates when one computer has the same copy of
# a file as another computer.
# Written by Alexander Alvonellos
# 05/10/2012

rootPath = 'D:\Dropbox'
destDir = 'D:\Conflicted'

def cerr(message):
    f = open('./LOG.txt', 'a')
    date = str(datetime.datetime.now())
    s = ''
    s += date[0:19] #strip floating point
    s += ' : '
    s += str(message)
    s += '\n'
    f.write(s)
    f.close()
    return

def removeConflicted():
    for root, dirnames, filenames in os.walk(rootPath):
        for filename in fnmatch.filter(filenames, '*conflicted*.*'):
            cerr(os.path.join(root, filename))
            shutil.move(os.path.join(root, filename), os.path.join(destDir, filename))
return

def main():
    #while True:
    for i in xrange(0,2):
        #time.sleep(1)
        removeConflicted()
        re.purge()
        gc.collect()
    return
main()

I've done some research effort on this problem and it seems like there might be a bug in fnmatch, which has a regular expression engine that doesn't purge after being used. That's why I call re.purge(). I've tinkered with this for a couple of hours now.
I've also found that doing:
print gc.collect()

Returns 0 with every iteration.
Whoever downvoted me is clearly mistaken. I really need some help here. Here's the link that I was talking about: Why am I leaking memory with this python loop?
Comment: I would try not reloading those modules every ten seconds - I can't see any reason for it. You also shouldn't need all the `del` statements (although I can see why you might try them if you have a leak) and you certainly don't need `return` at the end of every function.
Comment: I'm reloading because I am trying to track down the source of this leak. Do you have any other suggestions? Once I get a pointer as to where this is coming from, I'll make some changes to my code.
Comment: don't know about the leak, but `def cerr(message)` should really read `def cerr(self, message)`, and the call further down should be `self.cerr(...)`. how can you have a leak if your code isn't even functional?
Comment: Is there a particular reason why you're re-instantiating a new `cleanUpConflicts` object every time you come into the loop instead of just instantiating it once and re-using it? That seems a bit suspicious to me offhand.
Comment: It's kind of a janky program flow to begin with.  You basically are using `__init__()` to make your class pseudo-callable, then not really using the class as a class at all, as there is no stateful or instance information maintained since you blow it away and recreate every loop...
Comment: Why are you re-instantiating `cleanUpConflicts` over and over again?
Comment: Have you tried poking at the other methods in the gc module to see what is hanging around?
Comment: I can't reproduce a memory leak.
Comment: You say it leaks 1 Mb every 10 seconds. I see sleep (10). So it's leaking 1 Mb every iteration? How many files are there and how long is each iteration?
Comment: There are about 20000 files or so and each iteration takes about 15 seconds.
Comment: This script is running on windows server 2008 on python 2.7. I got rid of the object orientation. Let me post an update.
Comment: Regarding your "bug with fnmatch", you only are using one pattern and it will keep reusing that cached pattern. So you are only purging one entry.
Answer: At a guess, something keeps references to the instances created with each main iteration.
Suggestions:

Drop the class and make 1-2 functions
Drop matches; isn't used?
Look at inotify (Linux) or similar for Windows; it can watch the dir and act only when needed; no continuous scanning
Comment: I redid the code, got rid of the class, and made the changes that were suggested here, all except for the event watching functions. There's still a memory  leak. Any more suggestions?
Comment: @AlexanderAlvonellos: Please replace your code example with the more comprehensible short one you said you just altered.
Answer: Your code could be shortened to this:
import fnmatch
import os
import shutil
import datetime
import time

ROOT_PATH = r'D:/Dropbox'
DEST_DIR = r'D:/Conflicted'

def cerr(message, log):
    date = str(datetime.datetime.now())
    msg = "%s : %s\n" % (date[0:19], message)
    log.write(msg)

def removeConflicted(log):
    for root, dirnames, filenames in os.walk(ROOT_PATH):
        for filename in fnmatch.filter(filenames, '*conflicted*.*'):
            # 1: comment out this line and check for leak
            cerr(os.path.join(root, filename), log)
            # 2: then comment out this line instead and check
            shutil.move(
                os.path.join(root, filename),
                os.path.join(DEST_DIR, filename))

def main():
    with open('./LOG.txt', 'a') as log:
        while True:
            print "loop"
            removeConflicted(log)
            time.sleep(10)

if __name__ == "__main__":
    main()

See if your memory leak occurs if there are NO files to process. That is, point it at empty directories and determine if the leak is occuring when its doing the move.
You don't need the re.purge() or to mess with the gc module.
Comment: Let me try that and get back to you. Thank you so much for helping me.
Comment: @AlexanderAlvonellos: I made comments at two lines you can try commenting out, while pointing at your large directory.
Comment: The memory leak still happens regardless of which combination I comment out those lines, and also regardless of whether or not the directory I point the script at is empty. What do I do now? I appreciate your help, but even trying your script verbatim still doesn't solve the memory leak problem.
Comment: @AlexanderAlvonellos: Keep reducing the code until you discover the leak.


How to keep the state variable value intact upon screen reload

Question: I am new to React and I tried to toggle the Login/Logout based on the current state of authentication. I've used Google OAuth to perform the authentication.
I have a state variable to say if the user is authenticated or not and is defaulted to false. Upon successful authentication, I set the state to true.
Now the problem is, after completing a successful authentication, when I refresh the screen, the screen reloads and I see the console.log printing false and login appears momentarily. And after a second the console.log prints true and then the logout appears. How do I avoid showing login screen (for that one second after refreshing the screen) when the authentication is completed? Can someone help me please? Thanks.
const [isAuthenticated, setIsAuthenticated] = useState(false);

useEffect(() => {
    setIsAuthenticated(false)
}, [])

const handleSuccessAuth = x => {
    setIsAuthenticated(true)
}

const handleFailureAuth = x => {
    setIsAuthenticated(false)
}

const handleLogout = x => {
    setIsAuthenticated(false)
}

console.log(isAuthenticated)
if(!isAuthenticated)
{
    return (

                <LoginView
                    handleSuccessAuth = {handleSuccessAuth}
                    handleFailureAuth = {handleFailureAuth}
                />

           )
}
else
{
    return (

                <LogoutView
                    handleLogout = {handleLogout}
                />

)
}

Comment: maybe you should store whether the user is authenticated or not in the local storage, and use this value when calling `useState`
Comment: @OlivierBoissé You should never store user authentication details in local storage. That is a big security no no.
Comment: pfff, I didn't mean to store the authentication details, just a flag with `true` or `false` value whether the user is authenticated or not
Answer: If your variable goes from false to true it means your code is doing something, probably an AJAX call, my recommendation is to show a loading scree/message until the AJAX request is completed.
Answer: There is no way to keep the variable intact on reload but you can keep a variable that tracks if the user authentication has been initialized and show a loading indicator in the meantime

const [isAuthenticated, setIsAuthenticated] = useState(false);
const [authLoaded, setAuthLoaded] = useState(false)

// this does nothing, passing false to useState above sets the initial value
// useEffect(() => {
//     setIsAuthenticated(false)
// }, [])

const handleSuccessAuth = x => {
    setIsAuthenticated(true)
    setAuthLoaded(true)
}

const handleFailureAuth = x => {
    setIsAuthenticated(false)
    setAuthLoaded(true)
}

const handleLogout = x => {
    setIsAuthenticated(false)
}

console.log(isAuthenticated)

if (!authLoaded) {
  return
loading...

}

if(!isAuthenticated)
{
    return (

                <LoginView
                    handleSuccessAuth = {handleSuccessAuth}
                    handleFailureAuth = {handleFailureAuth}
                />

           )
}
else
{
    return (

                <LogoutView
                    handleLogout = {handleLogout}
                />

)
}
Comment: Will this not return "loading…" every time? I don't think Login screen will ever appear.
Comment: Once the authentication is handled either `handleSuccessAuth` or `handleFailureAuth` gets called I'm assuming and both will switch `authLoaded` to true and reveal either the login or logout page
Comment: I'm sorry, but I don't get it. For the authentication to happen, we have to have authLoaded set to be true. But we always return "loading…" not letting the next statement to execute.
Am I missing something?
Comment: @Gosu sorry I was missing a closing bracket. You're right, you should only show loading if `!authLoaded`
Comment: @Gosu I see what you're saying now. This is more of an organization problem. You should store the authLoaded variable in the LoginView and handle the loading screen in there as well.
Answer: I believe olivier-boisse alluded to using localStorage to persist your state. You can use an useEffect hook to persist your isAuthenticated to localStorage when the value updates, and use a state initializer function to read in the initial state from local storage.
const [isAuthenticated, setIsAuthenticated] = useState(() => {
  return !!JSON.parse(localStorage.getItem('auth');
});

useEffect(() => {
  localStorage.setItem('auth', JSON.stringify(isAuthenticated));
}, [isAuthenticated]);


When creating content with new term, show all term fields for input also

Question: Taxonomy terms are clubs and there are many in vocabulry, with fields to enter address, long/lat etc.
User adds content Concert and can pick a club (terms) from Simple Hierarchical select.
If club is not in vocabulary user can add new term. While doing this he should also be able to input fields for new term (address, long/lat) while creating content.
How can i achieve this ?
Answer: You can use combination of two modules. First is Entity Reference, where you have to create your taxonomy term reference field. Then install module Inline Entity Form and use widget Inline entity form - Multiple values (in number of values of this field select: 1).
In node create form you will have then field, where you can select from existing terms (autocomplete) and also you will be able to create new term, if term does no exists - this will create inline form, which would have all fields specified in taxonomy vocabulary structure (see image below).

If you select existing term, or create new, it will looks like this:
Comment: Thx Jure, this works nice. Only problem left is hierarchy. For example user wants to add club in Ljubljana, so he should choose parent term (Ljubljana), and add new child term after.


Read a csv file from aws s3 using boto and pandas

Question: I have already read through the answers available here and here and these do not help.
I am trying to read a csv object from S3 bucket and have been able to successfully read the data using the following code.
srcFileName="gossips.csv"
def on_session_started():
  print("Starting new session.")
  conn = S3Connection()
  my_bucket = conn.get_bucket("randomdatagossip", validate=False)
  print("Bucket Identified")
  print(my_bucket)
  key = Key(my_bucket,srcFileName)
  key.open()
  print(key.read())
  conn.close()

on_session_started()

However, if I try to read the same object using pandas as a data frame, I get an error. The most common one being S3ResponseError: 403 Forbidden
def on_session_started2():
  print("Starting Second new session.")
  conn = S3Connection()
  my_bucket = conn.get_bucket("randomdatagossip", validate=False)
  #     url = "https://s3.amazonaws.com/randomdatagossip/gossips.csv"
  #     urllib2.urlopen(url)

  for line in smart_open.smart_open('s3://my_bucket/gossips.csv'):
     print line
  #     data = pd.read_csv(url)
  #     print(data)

on_session_started2()

What am I doing wrong? I am on python 2.7 and cannot use Python 3.
Comment: Don't use those outdated example without knowing what you are doing. Go check out boto3
Comment: Since you're already using [`smart_open`](https://github.com/RaRe-Technologies/smart_open), just do `data = pd.read_csv(smart_open.smart_open('s3://randomdatagossip/gossips.csv'))`.
Answer: Here is what I have done to successfully read the df from a csv on S3.
import pandas as pd
import boto3

bucket = "yourbucket"
file_name = "your_file.csv"

s3 = boto3.client('s3')
# 's3' is a key word. create connection to S3 using default config and all buckets within S3

obj = s3.get_object(Bucket= bucket, Key= file_name)
# get object and file (key) from bucket

initial_df = pd.read_csv(obj['Body']) # 'Body' is a key word

Comment: This does not work with recent versions of pandas. See my answer to a similar question https://stackoverflow.com/a/46323684/1451649 that works for pandas 0.20.3
Comment: How ca I load only a subset of the csv instead of the whole file?
Answer: This worked for me.
import pandas as pd
import boto3
import io

s3_file_key = 'data/test.csv'
bucket = 'data-bucket'

s3 = boto3.client('s3')
obj = s3.get_object(Bucket=bucket, Key=s3_file_key)

initial_df = pd.read_csv(io.BytesIO(obj['Body'].read()))

Comment: Works with parquet as well as long as it is a single parquet file in the s3 key: df = pd.read_parquet(io.BytesIO(obj['Body'].read()))
Comment: Finally found something that worked out of the box for me. Thank you!
Comment: What is s3_file_key?
Answer: Maybe you can try to use pandas read_sql and pyathena:
from pyathena import connect
import pandas as pd

conn = connect(s3_staging_dir='s3://bucket/folder',region_name='region')
df = pd.read_sql('select * from database.table', conn)


Restricting the output of a model didn't improve the loss value (MSE) of the model evaluation

Question: There is a deep model for prediction.
The outputs are some numbers between 0 and 80. (In the dataset the outputs are 0-80)
The model Loss value is 70 and I would like to reduce it.
I printed the outputs after evaluating the model by test values and some of the predicted values are more than 80 or less than 0.
I decided to set up the final layer to predict just in 0-80 in the training step, therefore I set a lambda layer after final Dense layer to clip output values.
The codes:
def relu_advanced(x):
    return K.relu(x, max_value=80)

def createModel4():
    model = models.Sequential()
    model.add(Conv2D(256,(3, 3),
                     activation='relu',
                     input_shape=(320,20,1), padding='same'))
    model.add(MaxPooling2D((2, 2)))

    model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
    model.add(MaxPooling2D((2, 2)))

    model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
    model.add(MaxPooling2D((2,2)))

    model.add(Flatten())
    #model.add(Dense(5*320, activation= 'relu'))
    model.add(Dense(5*320))
    model.add(Lambda(relu_advanced))

    model.summary()
    return model

I tested the model with and without the relu_advanced and unfortunately, the Loss value (MSE) is increased with advanced_relu!
While there is no value much than 80 or less than zero, I don't know what may happen that the Loss (MSE) is increased?
Thank you
Comment: Your model is _huge_, you may want to try fewer parameters and once you have a model training _then_ try scaling up the number of parameters.
Answer: ReLU's are great for some purposes, but it's important to remember that they will set your gradient to zero if the values input to the ReLU are outside the allowed bounds. This is particularly problematic at the final layer since no other layers will be able to update on that iteration on that training example (the zero gradient stays zero during all of backprop).
You may have better luck using a properly scaled squashing function, e.g. sigmoid(x) * 80. This will ensure you have a gradient everywhere, albeit a much smaller gradient near 0 and 80 than at 40.


No unique stationary distribution?

Question: I found the following Markov chain on wikipedia.
$$
P = \begin{bmatrix}
0.9 & 0.075 & 0.025\\
0.15 & 0.8 & 0.05\\
0.25 & 0.25 & 0.5
\end{bmatrix}
$$
I tried to find the stationary distribution of the chain by trying to solve $$\pi P=\pi$$
but I cannot get a unique solution. How is this possible? Clearly, a unique solution has to exist! Right? Any help is appreciated.
Answer: Solving $\pi P = \pi$ will not result in a unique solution. Essentially, you are solving $\pi(P-I)= \vec{0}$ where $P-I$ should have at least one zero eigenvalue (in your case, it does). So you get an uncountable infinity of solutions, since if $\vec{x}$ is a solution, $k\vec{x}$ is also a solution for any $k \in \mathbb{R}$.
The way you get a solution to be unique is to consider only probability measures, i.e. by requiring that all components of $\pi$ sum to 1.
Answer: Multiplying out the following $$[\pi_1,\pi_2,\pi_3]P=[\pi_1,\pi_2,\pi_3]$$ gives three equations to solve. $$0.9\pi_1+0.15\pi_2+0.25\pi_3=\pi_1\\0.075\pi_1+0.8\pi_2+0.25\pi_3=\pi_2\\0.025\pi_1+0.05\pi_2+0.5\pi_3=\pi_3$$
Solving these, I get $$\pi_1=2\pi_2,\pi_2=5\pi_3$$Normalisation$$\implies \pi=\left[\frac58,\frac5{16},\frac1{16}\right]$$
Answer: Iterate the Markov chain:
$$\pi_{t+1} = \pi_t P = \pi_{t-1} P^2 = \pi_0  P^t$$
The stationary distribution will be
$$\pi_{\infty} = \pi_0 P^{\infty}  $$
where
$$ P^{\infty} = \lim_{n\to \infty} P^n = \lim U^\top D^n U = U^\top (\lim D^n) U$$
(where $U^\top D^n U$ is the eigenvectors/values decomposition of $P$). Due to the Perron-Frobenius theorem a Markovian matrix has 1 as its largest eigenvalue. Therefore $\lim D^n$ exists and is finite.

No unique stationary distribution?


"wget ca-directory=/etc/ssl/certs" failing on Ubuntu 14.04

Question: Ubuntu 14.04's default wget cannot handle SSL. How can I fix this? I do not want to use --no-certificate-check.
The certificate I want to authenticate is authenticated against the Root CA from Thawte.
Comment: What message does it fail with? Is there any authentication or session management required on the source site?
Answer: The filenames in the CA directory are not free-form. Each file is for one single CA certificate and the name of the file must be identical to the <hash_value>.<n> based of the subject name  i.e. <hash_value> is the value returned by:
 openssl x509 -subject_hash -in cafile.cert

and <n>  n is an integer, typically 0. If the hash value already exists then n will be incremented.
Normally I would set up the directory with a descriptive filename for actual CA files i.e. thawte.ca.cert and the hash value as a symlink to that file. The openssl c_rehash utility will set up those those symlinks for you.
Comment: As download each of the [root certificates](http://www.thawte.com/roots/), run the above command and create a symbolic link????  Oh well, since I'm bored...  `wget http://www.thawte.com/roots/thawte_Primary_Root_CA.pem ; openssl x509 -in thawte_Primary_Root_CA.pem -noout -subject_hash ; ln -s thawte_Primary_Root_CA.pem 2e4eed3c.0`  BTW the hash value differs between openssl above and below versions 1.0 ...
Comment: Very Interesting. Could you give a step-by-step for Linux CLI?

“wget ca-directory=/etc/ssl/certs” failing on Ubuntu 14.04


Transparent mysql_error logging: How can I read mysql_error() twice?

Question: I want to do a transparent mysql_error logging in my php app. I've done the following yet:
function mysql_query_log_error($query) {
    $debug_trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
    $caller = $debug_trace[1];
    $file = substr($caller[file], strrpos($caller[file], '/')+1);
    $line = $caller[line];
    $error = mysql_error();

    $error_query = "INSERT INTO Common.ERRsql SET "
        ."errQuery = '".mysql_real_escape_string($query)."', "
        ."errText  = '".mysql_real_escape_string($error)."', "
        ."errDate  = NOW(), "
        ."errFile = '".mysql_real_escape_string($file)."', "
        ."errLine = '".mysql_real_escape_string($line)."' ";
    mysql_query($error_query);

}

function mysql_query_log($query) {
    $result = mysql_query($query) or mysql_query_log_error($query);
    return $result;
}

But when I read mysql_error() in the mysql_query_log_error() function, the original caller can't access the mysql_error() (because it returns an empty string, so it's "read out").
How can I do it in a transparent way, I mean, the mysql_error() will remain intact?
Comment: don't you think that using the same storage that just produced an error is not too wise?
Comment: well yes, but most of the errors came from wrong SQL statements (table name changed, columns changed, unproper string handling, etc.)
Answer: You assign the original error to a variable, then pass that variable into the function. There's no need trying to duplicate the error, when you can get it once and store it in a variable.
Comment: But in this way, I have to change the original caller code, to grab the error message from the variable. But, well yes, I could change the `mysql_error()` in the same way to `mysql_error_my()` and handle a global variable in that.
Answer: This should accomplish what you're trying to do :
function mysql_query_log_error($query, $error) {
    $debug_trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
    $caller = $debug_trace[1];
    $file = substr($caller[file], strrpos($caller[file], '/')+1);
    $line = $caller[line];

    $error_query = "INSERT INTO Common.ERRsql SET "
        ."errQuery = '".mysql_real_escape_string($query)."', "
        ."errText  = '".mysql_real_escape_string($error)."', "
        ."errDate  = NOW(), "
        ."errFile = '".mysql_real_escape_string($file)."', "
        ."errLine = '".mysql_real_escape_string($line)."' ";
    mysql_query($error_query);

}

function mysql_query_log($query) {
    $result = mysql_query($query) or mysql_query_log_error($query, mysql_error());
    return $result;
}

In your mysql_query_log function, you will directly store the error in a variable.
Comment: No, I want to change `mysql_query()` calls in my app to `mysql_query_log()` without change in the original code. And some cases in the original code they use `mysql_query()or die(mysql_error())`, but if I change it to `mysql_query_log()or die(mysql_error())` the mysql_error() will return empty string, because I have read it out in `mysql_query_log_error()`


Changing the plotted confidence intervals plotted for an ARIMA model in R

Question: I'm having trouble changing what prediction/confidence intervals/bands are plotted when plotting a forecasted arima object generated.

model <- auto.arima(x)
forecast <- forecast(model, h=29)
plot(forecast)

I see the call automagically chooses some standard intervals (maybe 90% and 95%?), but the documentation for forecast.Arima() and plot.forecast() don't seem to mention which they are and how to alter which are plotted. The closest I can see to it is in the plot.forecast() help page, you can change the colors the prediction intervals are shaded with.
Ideally, I'd like to see 70% and 90% bands on my 29 step-ahead forecast plot.
thanks!
Comment: you might want to look at a blog post which I helped craft   http://www.autobox.com/cms/index.php/blog/entry/you-should-have-50-confidence-in-your-confidence-limits which details some deficiencies in the standard computation of ARIMA/TF forecast uncertainties.
Comment: Interesting. What would you suggest I do in lieu of prediction intervals?
Comment: I have implemented a couple of things within AUTOBOX that resample the model errors and take into account pulses. They are still prediction intervals just a tad more realistic ( maybe a lot more realistic ! ). Have you ever noticed how tight the confidence intervals are from standard solutions ... including SAS , STATA and everybody else except AUTOBOX.
Answer: After a lunch break, a think, and some good luck, I stumbled upon the answer! It's an option in forecast():

forecast <- forecast(model, h=29, level=c(70,90))

where the argument level takes the appropriate confidence intervals.


Display Oracle transfered bytes

Question: Is there a way to display Oracle uploaded and downloaded bytes from client applications?
Are there any internal traffic counters?
Answer: There are session and system stats:
SELECT NAME, VALUE FROM GV$SYSSTAT WHERE NAME IN ('bytes sent via SQL*Net to client', 'bytes received via SQL*Net from client', 'SQL*Net roundtrips to/from client');

The values are since the instance started.
Session level view is GV$SESSTAT.

Display Oracle transfered bytes


Como trocar de tab ao deslizar sobre a tela?

Question: Tenho uma Activity que tem 2 tabs e queria saber como trocar de tab não somente ao clicar sobre uma delas, mas também quando deslizar(para a esquerda e direita) sobre a tela.
Minha activity:
public class MainActivity extends Activity {
    private Fragment1 frag1;
    private Fragment2 frag2;
    ActionBar bar;
    Tab tab1;
    Tab tab1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bar = getActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        frag1 = new Fragment1();
        frag2 = new Fragment2();

        tab1 = bar.newTab();
        tab1.setText("Tab1");
        tab1.setTabListener(new ConfigTab(frag1, R.id.frag1));
        bar.addTab(tab1);

        tab2 = bar.newTab();
        tab2.setText("Tab2");
        tab2.setTabListener(new ConfigTab(frag2, R.id.frag1));
        bar.addTab(tab2);

    }
}

E a classe configTab:
public class ConfigTab implements TabListener {
    private Fragment fragment;
    private int placeholder;

    public ConfigTab(Fragment fragment, int placeholder) {
        this.fragment = fragment;
        this.placeholder = placeholder;
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.add(placeholder, fragment);
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.remove(fragment);
    }
}

Erro print na mainActivity, destacado abaixo:

Print do erro ocorrido:
Comment: É um pouco extenso responder à tua pergunta sem saber como é o teu código. Existe algum trabalho por trás do que pedes e esse trabalho na forma de uma resposta pode não ir ao encontro do teu caso em particular. Seria melhor colocares o código relevante na pergunta para obteres uma ajuda mais direcionada ao teu caso em particular.
Comment: @Zuul pronto postei.. as tabs então funcionando perfeitamente quando clicadas... somente queria que ao deslizar na tela trocar automaticamente as tabs.
Comment: adicionei uma foto no meu post da linha aonde ocorre o erro...
Answer: Quando implementei isso, fiz da seguinte forma. No layout do activity, coloquei assim:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v4.view.ViewPager>

Então o único elemento View no meu activity é o ViewPager. Para inicializar o ViewPager, no método onCreate:
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

Para este viewPager atribui um adapter, no caso uma classe que chamei de TabsPagerAdapter:
package br.com.exemplo.application.adapters;

import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v13.app.FragmentPagerAdapter;
import br.com.exemplo.presentation.views.fragments.PrimeiroFragment;
import br.com.exemplo.presentation.views.fragments.SegundoFragment;

public class TabsPagerAdapter extends FragmentPagerAdapter {
public PrimeiroFragment primeiroFragment;
public SegundoFragment segundoFragment;

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
    primeiroFragment = new PrimeiroFragment();
    segundoFragment = new SegundoFragment();
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        // Seu primeiro fragment
        return primeiroFragment;
    case 1:
        // Seu segundo fragment
        return segundoFragment;
    default:
        return null;
    }

}

@Override
public int getCount() {
    // get item count - quantidade de tabs
    return 2;
}

}

Voltando ao Activity, atribua o adapter ao viewPager, crie os objetos Tab e atribua estes à ActionBar:
viewPager.setAdapter(mAdapter);
ActionBar actionBar = getActionBar();
String[] tabs = {"Lista Publicações", "Nova Publicação"};
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adicione aqui as tabs:
for (String tab_name : tabs) {
    actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));

}

E também adicione um OnPageChangeListener ao seu viewPager:
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });

Bem, agora acredito que com o viewPager funciona. Esse OnPageChangeListener pode ser útil a você, porém não é obrigatório. Para os TabListeners implementei a interface ActionBar.TabListener no meu activity:
...
public class MainActivity extends Activity implements ActionBar.TabListener{
...

E com isso são criados os métodos que você usa no ConfigTab no activity. (Se preferir, atribua o ConfigTab como TabListener para cada objeto Tab e não implemente a interface ActionBar.TabListener):
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

Comment: esse seu código faz com que ao "deslizar" na tela do android pra esquerda e direita mudar os tabs?
Comment: Sim sim, irá funcionar tanto clicando sobre as tabs quanto deslizando com o dedo. Desculpe, faltou eu explicar que o método `onPageScrolled` é chamado sempre que você desliza entre tabs.
Comment: hum ok... vou tentar implementar aqui.. +1 na sua reposta.. depois que eu implementar eu marco como respondida^^ obrigado
Comment: Para fazer isso é preciso criar os objetos do tipo Tab e atribuir a cada um o TabListener mas da forma como  você fez `tab = bar.newTab()` e `bar.addTab(tab)` não daria certo neste caso. Acredito que os `TabListeners` não sejam necessarios devido o uso do `PageChangeListener` que implementa três métodos diferentes para detectar mudança de Tabs que podem ser o suficiente para seu caso.
Comment: essa linha nao da certo mAdapter.listPublicationsFragment.refreshListPublications();
Comment: Coloquei por engano, pode remove-la
Comment: Vamos [continuar esta discussão no bate-papo](http://chat.stackexchange.com/rooms/18080/discussion-between-leosantana-and-pedro-rangel).
Comment: precisa instancia o mdapter? se sim ele precisa de um fragmentmanager como fazer? pois mAdapter =  new TabsPagerAdapter(getFragmentManager()); da erro
Comment: vou adicionar uma foto do erro^^
Comment: A inicialização do Adapter está correta. No erro é alegado não encontrar a classe TabsPagerAdapter, então verifique se o nome do pacote está correto. Por exemplo, no código que coloquei na primeira linha está `package br.com.exemplo.application.adapter` mas no seu projeto deve ser diferente, algo como `com.example.teste`. Verifique se o conteudo da primeira linha `package` de cada classe é realmente o mesmo que o nome do pacote.
Comment: acho q encontrei o erro.. e na biblioteca de suporte
Comment: eu estava utilizando a biblioteca v4 e deu certo em tudo... viewPager.setCurrentItem(tab.getPosition()); não funciona da erro
Comment: era só colocar a linha if(viewPager!=null)
Comment: Fiz outra alteração no código, agora com os objetos Tab sendo criados. Observe ai logo apos apontar o adapter do ViewPager
Comment: muito boa^^ só achei estranho eu não poder utilizar a biblioteca de suporte v.13 somente a v.4
Comment: O suporte v13 engloba o v4, então se você for no diretório `libs` e excluir a biblioteca v4 não terá problema manter somente a v13.
Comment: porque na instancia de mAdapter = new TabsPagerAdapter(getSupportFragmentManager()); sou obrigado a usa o suporte impossibilitando usar a biblioteca v13 no FragmentPagerAdapter, pois mAdapter = new TabsPagerAdapter(FragmentManager()); nao funciona


centering components with css/html/angular

Question: Hi my project is located at
https://stackblitz.com/github/skwallace36/temprepo
I am trying to center the trees/and or the the two tree names at the top...
I am struggling to achieve this due to the crazy structure of the angular components and other wacky css/html which I'm not really too good at...
I don't think it would be helpful to post css/html snippets because I'm honestly not sure where I should try to achieve this, or if something I already have is preventing it...
I have tried all sorts of center, margin, etc solutions in so many different places.. I cannot find a solution
SOS and thank you very much for checking out my stack blitz
Comment: you want to make that treeconatiner component center of the page
Comment: @chellappan yeah... try clicking between the trees at the top. I want them to somehow be centered no many how many nodes and such. I'm just not sure if it's possible because hypothetically it could have a ton of nodes
Answer: Add this to your style.scss file
.treebuttoncontainer {
  text-align: center;
}

.trees {
  justify-content: center;
}

DEMO
Comment: you should probably add some additional scoping from their parent container to avoid CSS collisions with other items
Comment: glad i could help
Comment: hey man could you checkout https://stackoverflow.com/questions/51430443/struggling-to-keep-content-on-page-in-angular-project-css-html?


Method in Main program doesn't work when moved to a class

Question: Beginner android question:  I have a method in a Main program that works just fine:
 setRadioButtons(answer, value);

... void setRadioButtons (int answer, int value) {
    if (answer == 1)
        ((RadioButton) findViewById(R.id.C1Button)).setChecked(true);
    else if (answer == 2) ....

However this should really be in a class. When I move it to a class it fails:
Context context= getApplicationContext();
myQ1.setRadioButtons(answer, value, context);

 ...    public void setRadioButtons(int answer, int value, int context) {
        setContentView(context);
        if (answer == 1)
          ((RadioButton) findViewById(R.id.C1Button)).setChecked(true);

The method call in the Main program shows context underlined in red with the warning:
Wrong 3rd argument type. Found: 'android.content.Context', required: 'int'

This must have something to do with context but I really can't figure it out.
Answer: public void setRadioButtons(int answer, int value, int context)

is expecting an int, int, int for parameters. You provided instead an int, int, Context
Change the method signature to expect int, int, context like so:
public void setRadioButtons(int answer, int value, Context context)

Comment: None of these suggestion have worked. Not when passing the argument as an int or Context.  Even hard wiring the setContentView statement causes a fatal exception: setContentView(R.layout.layout2). Apparently the layout info gets lost when calling the method.
Answer: Context has it's own class.
Anyway, what you should pass to
setContentView

is not a context, but a layout resource id like
R.layout.example

Comment: Didn't work even when hard coding setContentView(R.layout.layout2); This causes a fatal exception.
Answer: It's because you have declared context as int. Actually it should be Context instance in the method. Change
  public void setRadioButtons(int answer, int value, Context context)


Why is my Rules Component not being scheduled accurately?

Question: My aim is to send an email to new students at certain weeks after admission. E.g week 28, week 32, week 45.
I set up components with action to send email.
I configured a rule with event: after saving new content type(student).
The content type has a field - date of admission.
I scheduled component evaluation based on the offset of 'date of admission'.
So for example:
Date of admission: 02/03/13 (March 2nd 2013)
Offset: 224 days
However when the rule is triggered and I go to admin/config/workflow/rules/schedule, I noticed that the scheduled date is 16/10/13 rather than 12/10/13.
Please how can I resolve this?
Comment: It's not very clear what you're trying to achieve, what your rule does, and what conditions trigger it. Please clarify.
Comment: Question has been edited accordingly.
Answer: When scheduling a Rules Component, you specify a value within Scheduled evaluation date. One of the options to do so, is to specify Relative dates like "+1 day", as mentioned within the Rules UI:

... any other values in GMT known by the PHP strtotime() function (like "+1 day")

As per your "offset = 224 days", I'm assuming you used a value like "+224 days".
However since you want to schedule the Rules Component a certain number of weeks after admission (like "week 28, week 32, week 45."), try to specify Relative dates like "+28 weeks", "+32 weeks", "+45 weeks". That might help for the Rules Scheduler to show a more accurate date.


Replacing MVC's built in User objects with Facebook Connect

Question: I have an ASP.NET MVC 3 project where I have NO local user management.  I have intergratred Facebook Connect successfully.  While this works, it makes my Controllers and Views messy and verbose.
I'd like to replace the default objects such as the User object exposed by Controllers and Views to return my FacebookUser object instead.
Anyone have a better solution than having my Controllers digging around in FacebookWebContex.? It just feels dirty.
Answer: Not quite sure which facebook library you are using.  But if you impliment IPrincipal and IIdentity in your FacebookUser object, you will be able to set HttpContext.Current.User to that FacebookUser which will allow you to pull that FacebookUser instance from the User property in the controller.
Comment: Where/when in the request life cycle should one set this object?  If I can do this before the view or controller access that property that would be an ideal solution.


Load multiple locale specific properties files in internationalization with resource bundle in java

Question: I have four properties files

Application.properties
Application_fr_FR.properties
Database.properties
Database_fr_FR.properties

So now I need internationalization in multiple programs, so now I need to load multiple properties files and get the key-value pair of values from properties files specific to a locale. For that I have a ResourceBundleService.java
public class ResourceBundleService {
    private static String language;
    private static String country;
    private static Locale currentLocale;
    static ResourceBundle labels;
    static {
        labels = ResourceBundle
                .getBundle("uday.properties.Application");
        labels = append(Database.properties");
        //** how to append existing resource bundle with new properties file?
    }

    public static String getLabel(String resourceIndex, Locale locale) {
        return labels.getString(resourceIndex);
        //How to get locale specific messages??
    }
}

Hope the question is clear.
Answer: You need to call ResourceBundle.getBundle(baseName, locale) each time  in getLabel. ResourceBundle maintains an internal cache so it wont load all props files each time:
public static String getLabel(String resourceIndex, Locale locale) {
    ResourceBundle b1 = ResourceBundle.getBundle("uday.properties.Application", locale);
    if (b1.contains(resourceIndex)) {
       return b1.getString(resourceIndex);
    }
    ResourceBundle b2 = ResourceBundle.getBundle("uday.properties.Database", locale);
    return b2.getString(resourceIndex);
}

Comment: Oh thank you for the information on internal cache but how do i call multiple properties files??????
Comment: Oh thank you for the information on internal cache but how do i call multiple properties files??????
Comment: I doubt theres a better solution than trying to find in Application first and if not found in Database, see update
Answer: For the time being use Application_fr.properties; les Canadiens will be thankful. With Locale.setDefault(availableLocale) elect an available locale. The root locale properties, Application.properties, should also contain the language keys. You could copy the French ones. In that case you need not set the default locale.
Comment: How do I load Database.properties and Application.properties at the same time so that I can one access point for ResourceBundle??
Comment: @EvgenlyDorofeev answered that. Do not try to use an additional Map for all, as the bundles are already optimized. A `getLabel(String rIx, Locale, ResourceBundle... bundles)` might have its uses too.
Answer: Lets check this implementation on github it works really nice. It requires the following function naming convention:

MultiplePropertiesResourceBundle is an abstract base implementation to allow to combine a ResourceBundle from multiple properties files whereas these properties files must end with the same name - the base-name for these combined ResourceBundle.

If you'll use it at the first you need to implement abstract class MultiplePropertiesResourceBundle as below:
import ch.dueni.util.MultiplePropertiesResourceBundle;

public class CombinedResources extends MultiplePropertiesResourceBundle {

    public  CombinedResources() {
        super("package_with_bundles");
    }

}

then you should implement empty class which extends out CombinedResources:
public class CombinedResources_en extends CombinedResources {}

and so on for other languages. After that you can use your bundle as below:
ResourceBundle bundle = ResourceBundle.getBundle("CombinedResources");

This bundle will be use all properties files inside package_with_bundles. For more information just look inside github repo.


What is the best way to keep your site code in git and pull in core and contrib in the same repository?

Question: Back when drupal was in CVS I had my sites in a git repository and then pulled in core and contrib via CVS. The two systems happily co-existed and I could track changes to everything and make patches to contrib modules. Now with git I am unsure whether to just download contrib and core and check it into my site's git repository, or maybe use submodules?
I am wondering if there are any best practices on this yet? I am particularly puzzled about how to have core as a submodule if my repo uses drupals root directory as its own root?
Answer: Not exactly the same question as https://drupal.stackexchange.com/questions/260/deploying-drupal-with-git but I have the same answer for you.
http://freso.dk/en/2011/02/26/managing_fresodk_from_cvs_in_svn_to_git
I think it can be useful to have Drupal Core as a real check out, makes it for example easier to track hacks. Because sometimes, there simply is no other way, be it changes to .htaccess or maybe you applied some patches for bugs which affect you directly.
Comment: I like that approach. The only thing I am still unsure of is how to push the repo to remote? I mean if the base checkout is from drupal.org?
Comment: You can have as many remotes as you want. To make it easier, you could rename the drupal remote to drupal (git branch rename origin drupal), then add your own as the origin (git remote add origin email@example.com:repo.git). You can then still configure the core branch (7.x) to pull from drupal by default (git branch --set-upstream drupal/7.x 7.x)
Comment: Perfect. That is just what I was looking for!
Comment: Just a small correction to `git branch rename origin drupal` - it should be `git remote rename origin drupal`.
Answer: As you shouldn't be hacking core, reverse your thinking: instead of making core a submodule of your site, make your custom code a submodule of core. Once you clone the core repository, you can do whatever you want to it, including adding your own submodules to it.
Alternatively, consider not using the core git repository for your site and update to the latest release using other functions, like drush. This way, you'd only have to submodule contrib modules. Unless you're contributing to core or doing bleeding edge things that depend on bug fixes that haven't made it into a release yet, the utility of getting all commits via VCS is minimal.


What is the correlation of a truncated bivariate Normal distribution?

Question: $X$ and $Y$ are independent standard normal variables.  If we generate $n$ samples $(x,y),$ what is the correlation for samples $(x, y)$ with  $x+y\gt0$?
Comment: The generation as such has no impact on the correlation. The pair $(X,Y)$ is distributed from a bivariate $N(0_2,I_2)$ distribution restricted to the half space $x+y>0$ and the correlation is defined as usual by$$\int_{x+y>0} (xy-\mathbb E[X]^2) c \exp\{-(x^2+y^2)/2\}\,\text dx\text dy$$where $c$ is the proper normalising constant.
Comment: A plot of this truncated density strongly suggests the correlation ought to be *negative,* not zero.
Comment: thanks, so it means that the correlation still be zero?
Comment: You have to compute this integral to check whether or not it is zero.
Comment: You can easily approximate this quantity by simulation`x=rnorm(T);y=rnorm(T);mean(x[x+y>0]*y[x+y>0])-mean(x[x+y>0])^2`
Answer: The question asks about the correlation coefficient of a truncated standard bivariate Normal distribution, where it is limited to the half-plane $X+Y\gt 0.$  (See the figure below.)
To work with this, it's convenient to let $U=X+Y$ (truncated to $U\gt 0$) and $V=X-Y$ (not truncated at all).  Evidently

$U/\sqrt 2$ has a Half-normal distribution,

$V/\sqrt 2$ has a standard Normal distribution, and

$(U,V)$ is independent.

The last observation follows directly from the independence of $(X+Y, X-Y).$
It is immediate that

$E[U/\sqrt 2] = \sqrt{2/\pi},$

$E[V] = 0,$

$E[U^2/2] = 1,$

$E[V^2/2] = 1,$ and

$E[UV] = E[U]E[V] = 0.$

Consequently

$\operatorname{Var}(U) = E[U^2]-E[U]^2 = 2 - 2/\pi,$

$\operatorname{Var}(V) = E[V^2]-E[V]^2 = 2,$ and

$\operatorname{Cov}(U,V) = E[UV] - E[U]E[V] = 0.$

The question asks about the correlation of $((U+V)/2, (U-V)/2),$ whose formula is
$$\begin{aligned}
\rho = \operatorname{Cor}((U+V)/2, (U-V)/2) &= \operatorname{Cor}(U+V, U-V) \\ &=\frac{\operatorname{Cov}(U+V,U-V)} {\sqrt{\operatorname{Var}(U+V)\operatorname{Var}(U-V)}}.
\end{aligned}$$
The standard rules of covariance (namely, its bilinarity) reduce these expressions to

$\operatorname{Cov}(U+V,U-V) = \operatorname{Var}(U)-  \operatorname{Var}(V) = -2/\pi,$
$\operatorname{Var}(U\pm V) = \operatorname{Var}(U) + \operatorname{Var}(V) \pm 2\operatorname{Cov}(U,V) = 2-2/\pi.$

The formula for $\rho$ thereby simplifies to

$$\rho = \frac{-2/\pi}{\sqrt{(2 - 2/\pi)^2}} = \frac{\pi}{1-\pi} \approx -0.4669422.$$

This plot shows 2000 independent $(X,Y)$ values.  Those with $X+Y\gt 0$ are darker.  Their least-squares fit appears in red: it is a line expected to have slope $\pi/(1-\pi)$ passing through the point $(\sqrt{1/\pi}, \sqrt{1/\pi}).$  It comes very close to that in this sample, helping to check the correctness of the formulas.


Is deactivating modules safe regarding settings?

Question: I had a strange error after uploading a module to a shared hosting server when going to admin/modules before even activating the module (image_desaturate_formatter).

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'sites/all/modules/image_desaturate_formatter/image_desaturate_fo' for key 'PRIMARY': INSERT INTO {system} (filename, name, type, owner, info) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4); Array ( [:db_insert_placeholder_0] => sites/all/modules/image_desaturate_formatter/image_desaturate_formatter.module [:db_insert_placeholder_1] => image_desaturate_formatter [:db_insert_placeholder_2] => module [:db_insert_placeholder_3] => [:db_insert_placeholder_4] => a:11:{s:4:"name";s:26:"Image Desaturate Formatter";s:4:"core";s:3:"7.x";s:7:"package";s:5:"Field";s:7:"project";s:26:"image_desaturate_formatter";s:12:"dependencies";a:2:{i:0;s:5:"field";i:1;s:5:"image";}s:7:"version";s:11:"7.x-2.0-rc1";s:9:"datestamp";s:10:"1356346648";s:11:"description";s:0:"";s:3:"php";s:5:"5.2.4";s:5:"files";a:0:{}s:9:"bootstrap";i:0;} ) in system_update_files_database() (line 2307 of xxx/htdocs/site/modules/system/system.module).

Now the update module isn't working anymore but just showing the following message, when I try to start the update process manually:

Failed to get available update data for 27 projects.

Could the module upload error be related to the update module not working?
Is it safe to stepwise deactivate the modules to check if update isn't working due memory issues? Do I loose all the settings I've made?

EDIT/UPDATE:
I have limited access to server logs and there is a

The timeout specified has expired: ap_content_length_filter: apr_bucket_read() failed

and the "reports" section doesn't show anything except the PDO above.
I now have disabled every module except core and tried to run update in between with no effort. The update module isn't working anymore. Is there something else i could do?
Answer:
It's impossible to say from here. Theoretically yes.
It depends on the module - a specific module could easily clear its variables in hook_disable(), or hook_uninstall(), or not at all. You'll have to check the install file for each module you disable to see how it will affect what you're doing.

It sounds very much like you have two copies of the image_desaturate_formatter module in your Drupal install though; that could account for the error.
Regarding server logs - your host should give you access to these (I've never come across a host that would deny you access to your own logs, it's criminal!) so give them a call, they might have valuable information in them.
Comment: And tell them, "I demand my rights!" `;)`
Comment: Me too... :D And i was (partly) wrong with the logs. They are hidden in the admin-interface of the hosting service. But show only (or at least) the last 24 hours. There's a "The timeout specified has expired: ap_content_length_filter: apr_bucket_read() failed" from last night, so i suspect the update module issue something on it's own. Currently I'm deactivating modules step by step...
Comment: Thanks @Clive for pointing out some ideas. I learned something...
Comment: @kiamlaluno That just made me laugh-spit my sandwich all over my keyboard ;)
Comment: @Cive They always say to feed your keyboard too. `;)` Mine is a little sad: There isn't much food that can fall behind the keys. I blame Apple for this. `;)`
Comment: @kiamlaluno I just shook my Apple keyboard and loads of crumbs and other crap came out - you're just not trying hard enough ;)


Classifier for lipstick colors

Question: I need to classify lips by lipstick they use(basically by color). Currently I have 26 classes but in general it might be above 200.
I already tried to do something convnets and multilayer perceptron. Unfortunately both of them didn't help very much.

CNN returned .45 accuracy on test with 3 channels image.
With MPL I've got up to .06 accuracy. For this neural net I converted image matrix to vector.

What else would you recommend? And which kind of data preprocessing might be useful here? Currently one of the biggest problem I think on is how to classify very similar lipstick colors. On a real life fotos they look very similar.
Comment: Can you share a few images and desired response
Answer: Neural Networks are not the magical things. It wouldn't be able to find something that doesn't presented on the image. Since color is the only feature then due to different lighting environments the same color can look differently and there is nothing that you can do about it. The right thing to do is to check whether all mistakes produced by the network are meaningful. In case if even person cannot identify the difference between classes then it's the best than you can have.
Also I don't think that Neural Networks for this problem is the best way to start. I think that it's much easier to try to extract some features from the image. For instance, you can convert each image from RGB to HUE and extract histogram per each channel. Than use this features (per each sample concatenate 3 histograms in one vector) to train some simple classifier like logistic regression or SVM. Histograms are much easier to explore and to understand. In addition, it can be useful to apply some algorithm that crop lips from the image and then extract features.
Comment: Pixels in the raw images aren't good prediction features. Each pixel doesn't encode valuable information about the image. I would suggest trying to use some classic computer vision methods for feature extraction from images and use them as an image representation for classifier.
Comment: Thank you for your answer. Yes, I understand that nets are not magic think. I also tried apply SVM on raw RGB images and as I expected id didn't help but idea with HUE looks good.

Classifier for lipstick colors


Implementing @font-face as of late 2012

Question: I've been spending a few evenings investigating the best way to implement @font-face with todays modern browsers. I am a full time web/system developer with a background as a graphic designer I find the opportunities and possibilites with web design becoming more and more interesting. So, I made some testing and would like to hear if anyone has advices, any better ideas or inputs on this. My testing scenario looks like this.
I tested with ALOT of different online tools and applications but in the end I boiled this down to one online service and one application.

FontSquirrel, online tool - free. http://www.fontsquirrel.com/fontface/generator
FontXChange, application for mac/win - 99$. http://fontgear.net/fontxchange.html

I used a font that was shipped with my Mac, Tamil Sangam MN and Tamil Sangam MN Bold, both came as open type, .otf.

TamilSangamMN.otf - 290 KB
TamilSangamMNBold.otf - 271 KB

Investigating conversion sizes
FontSquirrel
This is a good online tool, it's very good. There are a few different modes and I've used the Basic and Optimal. The output of my .otf file was svg, ttf, eot and woff.
Basic
Regular

TamilSangamMN.svg 233 KB
TamilSangamMN.ttf 71 KB
TamilSangamMN.eot 25 KB
TamilSangamMN.woff 30 KB
Total: 359 KB

Bold

TamilSangamMNbold.svg 225 KB
TamilSangamMNbold.ttf 69 KB
TamilSangamMNbold.eot 25 KB
TamilSangamMNbold.woff 30 KB
Total: 349 KB

Optimal
First thing I noticed with the optimal fonts are that they are considerably much smaller than the basic variants ~ 260 KB!
Regular

TamilSangamMN.svg 33 KB
TamilSangamMN.ttf 25 KB
TamilSangamMN.eot 15 KB
TamilSangamMN.woff 17 KB
Total: 90 KB

Bold

TamilSangamMNbold.svg 33 KB
TamilSangamMNbold.ttf 25 KB
TamilSangamMNbold.eot 15 KB
TamilSangamMNbold.woff 17 KB
Total: 90 KB

FontXChange
This tool can do so much more than just juggling fonts for the web. It can convert several formats between each other; like opentype, true type, web fonts, post script and so on. The result is overall very high quality the files are so large, almost twice the size of FontSquirrels Basic versions and over 7 times bigger than FontSquirrels Optimal version.
Regular

TamilSangamMN.svg 480 KB
TamilSangamMN.ttf 72 KB
TamilSangamMN.eot 72 KB
TamilSangamMN.woff 80 KB
Total: 704 KB

Bold

TamilSangamMNbold.svg 463 KB
TamilSangamMNbold.ttf 69 KB
TamilSangamMNbold.eot 70 KB
TamilSangamMNbold.woff 80 KB
Total: 682 KB

Setting up the CSS
It puzzled me a bit in the start that the actual order in the font list mattered. Then I discovered that some takes the first format that it finds compatible rather than sticking with the format that is optimal - and it sucks. After some laborations I found that this is the optimal way to format your css (note the order of the file types | important!).
@font-face {
    font-family: 'TamilSangam';
    src: url('.eot');
    src: url('.svg') format('svg'),
         url('.eot?#iefix') format('embedded-opentype'),
         url('.woff') format('woff'),
         url('.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;
}

My test results
Regular fonts
I did mark the best version with a *
Firefox MAC (15.0.1)

FontSquirrel Optimal: Renders a bit to fat
FontSquirrel Basic: Renders a bit to fat
FontXChange 4.0: Renders a bit to fat, but the kerning is better than the FontSquirrel version *

Firefox Windows (15.0.1)

FontSquirrel Optimal: Renders very nice
FontSquirrel Basic: Font gets jagged/choppy
FontXChange 4.0: Renders very nice *

Safari Mac (6.0)

FontSquirrel Optimal: Perfect render
FontSquirrel Basic: Perfect render
FontXChange 4.0: Perfect render *

Chrome Mac (21.0)

FontSquirrel Optimal: A bit fat
FontSquirrel Basic: A bit fat
FontXChange 4.0: Perfect render *

Chrome Windows (21.0)

FontSquirrel Optimal: Perfect render
FontSquirrel Basic: Perfect render
FontXChange 4.0: Perfect render *

Internet Explorer (9.0)

FontSquirrel Optimal: Perfect render *
FontSquirrel Basic: Perfect render
FontXChange 4.0: Font gets jagged/choppy

Bold fonts
Firefox MAC (15.0.1)

FontSquirrel Optimal: Renders very fat *
FontSquirrel Basic: Renders very fat
FontXChange 4.0: Renders very fat, but better kerning (I would like to select this one for the mac but since the windows version of firefox isn't compatible here it has to go)

Firefox Windows (15.0.1)

FontSquirrel Optimal: Renders ok, not very antialiased *
FontSquirrel Basic: Font gets jagged/choppy
FontXChange 4.0: Font gets a bit distorted/jagged

Safari Mac (6.0)

FontSquirrel Optimal: Perfect render *
FontSquirrel Basic: Perfect render
FontXChange 4.0: Renders fat

Chrome Mac (21.0)

FontSquirrel Optimal: Perfect render *
FontSquirrel Basic: Good render, a few percent to fat
FontXChange 4.0: Renders fat

Chrome Windows (21.0)

FontSquirrel Optimal: Perfect render *
FontSquirrel Basic: Perfect render
FontXChange 4.0: Renders very fat

Internet Explorer (9.0)

FontSquirrel Optimal: Perfect render *
FontSquirrel Basic: Perfect render
FontXChange 4.0: Font gets jagged/choppy

The final implementation
I usually organize my webfonts in the following pattern, <webfonts> / <conversion source> / <conversion method> / <fonts>
/* Regular */
@font-face {
    font-family: 'TamilSangam';
    src: url('webfonts/fontsquirrel/optimal/tamil_sangam_mn.eot');
    src: url('webfonts/fontxchange/TamilSangamMN.svg#TamilSangamMN') format('svg'),
         url('webfonts/fontsquirrel/optimal/tamil_sangam_mn.eot?#iefix') format('embedded-opentype'),
         url('webfonts/fontxchange/TamilSangamMN.woff') format('woff'),
         url('webfonts/fontxchange/TamilSangamMN.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;
}

/* Bold */
@font-face {
    font-family: 'TamilSangam';
    src: url('webfonts/fontsquirrel/optimal/tamil_sangam_mn_bold.eot');
    src: url('webfonts/fontsquirrel/optimal/tamil_sangam_mn_bold.svg#tamil_sangam_mnbold') format('svg'),
         url('webfonts/fontsquirrel/optimal/tamil_sangam_mn_bold.eot?#iefix') format('embedded-opentype'),
         url('webfonts/fontsquirrel/optimal/tamil_sangam_mn_bold.woff') format('woff'),
         url('webfonts/fontsquirrel/optimal/tamil_sangam_mn_bold.ttf') format('truetype');
    font-weight: bold;
    font-style: normal;
}

Graphical overview of the result (fullsize at https://i.stack.imgur.com/atb98.png)

Conclusions and quests
There isn't one single tool out there that will deliver fonts that render nice on the Mac and Windows in all browsers. You must experiment and test on each font. The methodology posted above is just a simple way and suggestion how to test and experiment with @font-face's.
Is there anything you think I could change in my methods or implementation, is there any application or service that I missed?
All the best / T
Comment: Do you have an actual question to ask or problem to solve? It seems you're merely sharing some research and implementation without asking a concrete question, but just seeking opinions. That seems more apt for a blog than a Q&A site.
Comment: Did update with a more specific question
Comment: I, for one, applaud your thorough post.
Comment: It's a great list. Unfortunately, it doesn't work for IE8. So, I had to move the `.eot?#iefix` lines above the `.svg#TamilSangamMN` lines. I got that from another stackoverflow question about font-face & IE8.
Answer: @font-face code
You can add src: local('ò?'), which looks for local font with this name, forcing browser to ignore local fonts if they happen to have the same name as your custom font. You can also use this inversely to restrict custom fonts from downloading. see mobile support
I generally see the ?#iefix line second after the standard .eot src, though I can't say I have ever needed it, nor know if specific positioning is required (apart from the .eot being listed first).
Additional control over the fonts
If you are looking for more control with the fonts in the scenario where fonts fail to load, or dealing with the FOUC in IE, I have a jQuery plugin which will let you hide fonts while they are loading, and allow you to alter the font size on fail so your fallback font doesn't destroy your layout. How to know if a font (@font-face) has already been loaded?
IE6-8 bug
Additionally, IE6-8 can have issues with some font's .eot file. This can be fixed by either (full guide here):

Converting a new .eot file online. If this doesn't work, then the problem is the file properties themselves.
Use FontForge to edit the name properties of the font file, then resave and reconvert.

CORS
Seems to only relate to IE and FF. All other browsers (only latest versions tested) don't have problems.
CORS are a common problem with fonts and occur when you are loading fonts from another domain or hostname. This includes specifying your site with a www or not. The @font-face code needs to be relatively referenced, as does the CSS file. You will also have issues if defining a <base> tag in your html. If this isn't possible, or if you don't want to worry about CORS, then you'll need to place the following code in an .htaccess file in your font directory:
<FilesMatch "\.(ttf|otf|eot|woff)$">
  <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
  </IfModule>
</FilesMatch>

MIME Types
404 issues on font serving may be caused by incorrect MIME definitions, see more here: Mime type for WOFF fonts?
Mobile Support
Mobile support is pretty bad. Android doesn't properly support it until 4.0, and Windows Mobile, as far as I am aware doesn't support it at all. I am investigating any work around or solutions for this. Best I have so far is to use How to know if a font (@font-face) has already been loaded? to display a picture of the text on font load failure. This really only works for site titles and icons, otherwise it's a horribly poor work around, bad for SEO and bad UX.
See @font-face support.
Additionally, Android 2.2 - x.x.x versions will fail if your @font-face uses local(), which is also used as a fix for IE. Multiple stylesheets may be needed if you want to cover all your bases. See more here: https://stackoverflow.com/a/4520467/1455709
SVG and Chrome
Chrome won't use the SVG font if you're including the #fontName in the url. It'll also use WOFF before SVG - and doesn't do a good job rendering it. This is probably the reason why everyone sooks about crappy font rendering in Chrome... To overcome this is an additional @font-face declaration is required:
@media screen and (-webkit-min-device-pixel-ratio:0) {
    @font-face {
        font-family: 'myFont';
        src: url('fonts/myFont.svg') format('svg');
    }
}

Implementing @font-face as of late 2012


Append the parent selector to the end with Sass

Question: Ok, suppose I have the following traditional CSS
.social-media { /* ... */ }
.social-media .twitter { /* ... */ }
.social-media .facebook { /* ... */ }
ul.social-media { /* ... */ }

So, I tried to do it like this with SCSS:
.social-media {

    /* ... */

    .twitter {
        /* ... */
    }
    .facebook {
        /* ... */
    }

    // Here's the problem:
    ul& {
        /* ... */
    }
}

The last part does not work, because it seems like the ampersand should only appear at the beginning of a selector. Is there a workaround?
Comment: You need to consider rethinking the way you're naming things. Why do you have an element with the class `social-media`, and then an unordered list inside of it with the same class name?
Comment: @zakang you didn't understand it properly. I have some styles defined to .social-media elements, in general. And then, I want to apply some particular styles to the UL elements with the `.social-media` class. There's nothing like `.social-media ul.social-media` in my code, like you're implying.
Comment: My fault, too much code scanning and not enough reading :)
Answer: Sass 3.2 and older
The only thing you can do is reverse your nesting or not nest at all:
    .social-media {
    /* ... */

    .twitter {
        /* ... */
    }
    .facebook {
        /* ... */
    }
}

ul.social-media {
    /* ... */
}

Sass 3.3 and later
You can do that using interpolation and the @at-root directive:
.social-media {
    /* ... */

    // Here's the solution:
    @at-root ul#{&} {
        /* ... */
    }
}

However, if your parent selector contains multiple selectors, you'll need to use selector-append instead:
.social-media, .doodads {
    /* ... */

    // Here's the solution:
    @at-root #{selector-append(ul, &)} {
        /* ... */
    }
}

Output:
.social-media, .doodads {
  /* ... */
}
ul.social-media, ul.doodads {
  /* ... */
}

Comment: @AdrianFlorescu Check your dependencies.  If you have the correct version, you likely have a library that depends on an older version.  The `sass --version` command will only report the newest version you have installed, rather than all versions.
Comment: That's what I was afraid of... Incredibly you can darken and lighten colors from your sass and you cannot do something like this. Thanks for your answer!
Comment: It has to do with the fact that using `&` is not simple string replacement.  If your parent selector had been `ul.foo`, you might have ended up with `ulul.foo` if that were the case.
Comment: hm... this solution works fine on sassmeister, but on my local, with same version of sass as sassmeister, this gives me an error :(
Comment: @cimmanon, you are right, I have an older version of compass that depends on sass o~> 3.3.0.rc.1 so.... not sure if I'll be able to upgrade for that. Thank for the answer!


why are all my new netbeans projects managed by subversion by default?

Question: when i create a new netbeans project netbeans tend to manage it with subversion by default.
i dont want that.
is it a way to remove it?
Answer: The project is not being monitored by svn if you create it in a directory that is not monitored by svn.
Let's take a quick 'example'...
Say you have checked out some code from an SVN repository, into the folder /home/mine/foo.  foo becomes your SVN_ROOT.... All the code in any sub directory of foo came from your svn directory.  If you create a project in /home/mine/foo/NewNBProject, NetBeans (and the svn command line program) will assume that you want this new project to be checked into the svn repo, eventually.
If you create your project in /home/mine/bar/NewNBProject, NetBeans will not associate the project's files with a repository [assuming you did not use svn to get bar from an svn repository].
Answer: What kind of projects are you creating in Netbeans? I have used Netbeans to create new Java and new Ruby projects without this issue. Could it be that you are creating a new SVN project by mistake?
Comment: i click: file -> php -> php application. its a new php 5 application that will be created from scratch. but it will be svn monitored once created. and i have no idea why.


When should an F-test be one sided vs two sided?

Question: If we increase both numerator and denominator degrees of freedom of the F distribution, then the pdf narrows down on the value 1.
This suggests that a two-sided test is reasonable: If we have high degrees of freedom, then finding e.g. our test statistic $F=0.034$ is extremely unlikely: not only high, but also low values are unlikely in this case.
Yet I have so far not yet a textbook use a two sided test of the F test. Instead, we reject the null hypothesis only when the test statistic is very large.

Why don't textbooks use a two sided test?
When is a two sided test reasonable for the F test, and when is a one sided test reasonable?
Comment: The upper tail distribution is used for omnibus tests for difference, but the lower tail distribution is used for omnibus tests of equivalence.
Answer: In the context of ANOVA, the F ratio puts between variance in the numerator and within in the denominator. You only care if the ratio is > 1 (if it were not, you'd need not even conduct any test). Therefore you always have a directional hypothesis and thus a 1-tailed test. Even in other uses of the F ratio, it is standard to put the larger variance in the numerator, so again it becomes 1-tailed.
Comment: Simple and straightforward answer. No unnecessary gobbledygook.
Answer: Every classical hypothesis test has a test statistic, and an implicit ordering on what values of the test statistic constitute more or less evidence for the alternative hypothesis.  To understand the appropriate "tail area" for your critical region or p-value, you need to understand the ordering-of-evidence inherent in the test statistic.  The test statistic and its (often implicit) ordering effectively defines a total order on the set of all possible observed outcomes, which tells you what outcomes constitute more or less evidence for the alternative hypothesis.  (You can find more discussion of the mathematical structure of a hypothesis test in this related answer.)
In hypothesis tests where the null-distribution is an F-distribution (i.e., the test statistic has an F-distribution, conditional on the null hypothesis being true), it is usually the case that the test statistic is a positive measure of deviation away from the outcome least conducive to the alternative hypothesis.  In these cases a test statistic of zero represents the least possible evidence for the alternative, and higher values represent more evidence for the alternative.  In this case, the p-value of the test is obtained solely from the upper tail of the F-distribution (i.e., outcomes that are at least as conducive to the alternative hypothesis as some cut-off outcome) and so the test is "one-sided".
An important thing to remember in hypothesis testing is that the test statistic and its null distribution are not sufficient to describe the test.  You also need to know the evidentiary ordering of the test statistic that describes whether an outcome is more or less conducive to the alternative.  Often this is not stated explicitly, and you need to look at the nature of the test statistic to figure it out.
Comment: Thank you. Let's say though that we are doing some test where a very high F value is conducive to the alternative hypothesis, but an F very close to zero is not, but that both degrees of freedom are very large, so that a value of F=0.000000001 is very unlikely under the null but is also not conducive to the aternative. Then surely, if we find F=0.00000001, we would be wise to consider that maybe a third hypothesis is correct, and both the null and the alternative are incorrect? A reasonable statistician would surely try to find out what that third hypothesis might be?
Comment: You aren't allowed to change your hypotheses mid-way through a hypothesis test, so no "third hypothesis" is relevant.  If low values of the test statistic F are less conducive to the specified alternative hypothesis (as you have described) then a low observed value will yield a high p-value (calculated using only the upper tail) and you will not reject the null.  If you then decide that a different kind of test is warranted, that is a separate issue.


Module webform2pdf modify template

Question: I have sereval webforms et each webform send a mail with a pdf. I would like to do a template for each webform.
I use webform2pdf in Drupal 7 to send pdf by email.
How can i do that ?
Answer: I'm working with this module right now too.
Once the module is enabled (and the required tcpdf library is intalled), you will see a new "Generate PDF" tab when editing each of your webforms. This is where you can add your PDF template under "Content of the PDF Document"
If you enter stuff here, it overrides anything you may have set globally (under /admin/config/content/webform2pdf).
You can use html and tokens here, for example...

INTERN NAME: [submission:values:intern_name]

I hope this helps.
If you get this working, maybe you (or somebody reading this) can help me with a problem I'm having: I want to add conditional logic for some of my fields so they only show up in the PDF if a value is entered.
I tried this via the interface (I have PHP filter module installed), but it resulted in a completely blank PDF, so now I'm going to try doing a template override instead.
Comment: Thanks Anne for your response. I need use php too to do my template. In fact, i must do tables in my pdf with a lot of fields. I thought that i could use a hook to do that but i can't.
Comment: Anne, i tried to use php filter in content of body and i can do it. I have a pdf with information. I test that :
".$i."";
}
?>
      Are you PHP Code in drop down list Format text ?

Module webform2pdf modify template


Runtime code compilation

Question: I am working on a project which teaches JavaScript techniques (like Code Academy). Mainly I do this for self education purposes.
I have difficulties with one important part:
As the whole project is a web project, the user types in his code directly into the web site. My problem is, how do I check if the submitted code is passes all tests for the current exercise? My approach would be to eval the code and run my unit tests against it. The thing here is, how do I limit the scope for the eval'ed code and how can I test it? I already found a solution here about how to limit the scope:
<code>function maskedEval(scr)
{
    // set up an object to serve as the context for the code
    // being evaluated.
    var mask = {};
    // mask global properties
    for (p in this)
        mask[p] = undefined;

    // execute script in private context
    (new Function( "with(this) { " + scr + "}")).call(mask);
}
</code>
But I don't have a solution how to run my test against the evaluated code.
Is this the correct way to do this or should I do it completely different?
Comment: why dumb it down at all? just run it and check the results and resulting side-effects. and, btw, that eval "protection" has holes large enough to drive a truck through...
Comment: Sure, but I want to keep the user code in a separate namespace
Comment: sounds like a perfect job for an iframe. you can scan contentWindow from above to access anything you could "on-page", if the domains match as you say...
Answer: If you run the code on the client-side there is no real need to limit its scope as it can only affect the client himself.
If, however, you send the code to be evaluated on the server side you might want to look into sandboxing libraries for node.js such as http://gf3.github.io/sandbox/ (the first one I found on google, there are probably many more).

Runtime code compilation


Help on evaluating $\lim_{x\rightarrow \frac{\pi}{4}}(\tan x)^ {\tan 2x} $

Question: $\lim_{x\rightarrow \frac{\pi}{4}}(\tan x)^ {\tan 2x} = ? $
I know that's an indetermination of the form $1^{\infty}$, but I don't see any way to solve it.
Answer: Another method, is using $\ln$
$$\ln(\lim_{x \to \frac\pi4}{\tan x}^{\tan 2x}) = \lim_{x \to \frac\pi4}\ln({\tan x}^{\tan 2x}) = \lim_{x \to \frac\pi4} (\tan 2x) \ln({\tan x})$$
$${\tan 2x} = \frac{2 \tan x}{1 - \tan^2x}$$
So:
$$\ln\left(\lim_{x \to \frac\pi4}{(\tan x)^{\tan 2x}}\right) = \lim_{y \to 1}{ \left(\frac{2y}{1 - y^2}\right) \ln{y}} = \lim_{y \to 1} \left(\frac{2y}{(1 - y)(1 + y)}\right) \ln({y})$$
$$= \lim_{y \to 1} \left(\frac{1}{1 - y}\right) \ln({y}) = -1$$
Therefore:
$$\lim_{x \to \frac \pi4}{{\tan x}^{\tan 2x}} = e^{-1}$$
Answer: $$\left[\lim_{x\to\frac\pi4}\left(1+(\tan x-1)\right)^{\frac1{\tan x-1}}\right]^{\lim_{x\to\frac\pi4}\tan2x(\tan x-1)}$$
The inner limit converges to $e$
For the exponent
$$\lim_{x\to\frac\pi4}\tan2x(\tan x-1)=\lim_{x\to\frac\pi4}\frac{2\tan x(\tan x-1)}{-(\tan x+1)(\tan x-1)}=\cdots=-1$$
as $\tan2A=\dfrac{2\tan A}{1-\tan^2A}$
Comment: Making $\lim_{x\rightarrow 0} (1+x)^{1/x} = e$ appear there was beautiful... thanks


logstash parse with offline logs will give better performance or online?

Question: I have ELK stack installed and about to do performance testing.
Getting below doubt which am not able to resolve myself, expertise suggestions/opinions would be helpful.
I am doubtful on,
1.  whether to do logstash on LIVE - meaning, install logstash and run ELK in parallel with my performance testing on application.
2. Or First do the performance testing collect logs and feed logs to logstash offline. (this option is very much possible, as am running this test for about 30minutes only)
Which will b better performant ?
My application is on Java and since logstash also uses JVM for its parsing, am afraid it will have impact on my application performance.
Considering this, I prefer to go with option 2 , but would like to know are there any benefits/advantages going with option 1 that am missing ??
Help/suggestions much appreciated
Answer: Test your real environment under real conditions to get anything meaningful.
Will you run logstash on the server? Or will you feed your logs in the background to i.e. Kafka as described in my blogpost you summoned me from? Or will you run a batch job and then after the fact collect the logs?
Of course doing anything on the server itself during processing will have an impact and also tuning your JVM will have a big influence on how well everything performs. In general it is not an issue to run multiple JVMs on the same server.
Do your tests once with logstash / kafka / flume or any other log processing or shipping tool you want to use enabled and then run a second pass without these tools to get an idea of how much they impact the performance.


Sum of interior angles of convex polygon

Question:
A convex polygon has its interior angles in arithmetic progression, the least angle being $120°$ and common difference $5°$. Find the number of sides of the polygon.

To solve this I use the fact that the sum of the interior angles of a convex polygon in $(n-2)180°$. We can write the equation
$$120n + \frac{n(n-1)}{2}5 = (n-2)180.$$
Solving this we get the polynomial $n^2-25n+144=0$. This admits two solutions $9 \ \& \ 16$.
The answer given at the back is $n=9$. So my question is why is $16$ not possible?
Answer: For $n=16,$  the maximum angle $=120^\circ+(16-1)5^\circ>180^\circ$
But what is Convex Polygon?


Como suavizar uma curva no R

Question: O objetivo é obter o gráfico suavizado do data.frame
x <- c(1e-04, 0.0014,  0.018, 0.24, 3.2, 42, 560, 7500, 1e+05)
y <- c(0, 7, 10, 7, 0, -7, -10, -7, 0)
df <- data.frame(x, y)

A curva gerada por

library(ggplot2)
ggplot(data = df, aes(x, y)) + scale_x_log10() + geom_point() + geom_line()

ficou ligada por retas. Como fazer uma curva mais suave com o ggplot2?
Comment: Muito reticulada? O gráfico está apenas plotando os pontos, ligando por linhas. Se você quer encaixar uma curva nos pontos que você tem, você talvez queira fazer uma regressão polinomial para esse caso -- seria isso que você está buscando?
Comment: Usei o termo reticulada para expressar os pontos ligados por linhas. Vou consertar. Acho que a regressão polinomial pode resolver o problema. Mas, como eu gostaria que a curva passasse por todos os pontos, a ordem do polinômio tem que ser igual ao número de pontos, certo? Com 9 pontos acho que funciona, o meu caso real tem 50 pontos. Vou tentar a sua solução.
Answer:
É melhor interpolar os pontos usando splines:
sp <- data.frame(spline(log10(df$x), df$y, n = 8*nrow(df)))
sp$x <- 10 ** sp$x
ggplot(data = df, aes(x, y)) + scale_x_log10() + geom_point() + geom_line(data=sp)

O número 8 representa a quantidade de pontos criados para cada ponto original (contando o próprio ponto original); quanto maior esse número, mais suave é a curva.
Como seu gráfico possui uma escala logarítmica em x, foi necessário tirar o logaritmos dos valores de x antes de interpolar os pontos, para garantir que a função criará pontos equidistantes no eixo x do gráfico.
Answer: Uma outra forma bem rápida de ajustar uma linha suavizada é usar o próprio geom_smooth do ggplot2:
library(ggplot2)
ggplot(data = df, aes(x, y)) + scale_x_log10() + geom_point() + geom_smooth()

Update: Seguindo o comentário do Marcos, fazendo a linha de ajuste passar pelos pontos com um polinômio:
 ggplot(data = df, aes(x, y)) + scale_x_log10() + geom_point() +
     geom_smooth(method = "lm", se = FALSE, formula = y ~ poly(x, nrow(df) - 1))

Comment: O objetivo é que a curva passe sobre os pontos. Usando a sua sugestão consegui com `geom_smooth(method = "lm", se = FALSE, formula = y ~ poly(x, nrow(df) - 1))`
Comment: Ah, me desculpe, Marcos! Não percebi que era para passar pelos pontos! Vou atualizar a resposta com sua sugestão!

Como suavizar uma curva no R


Modify the triggering event of an event handler

Question: I'd like to change the triggering event of a set of event handlers via jQuery.
For example, if I have:

Hello world

And
$("#abc").on("mouseover",alert);

Is there any way to reassign the mouseover event to mousemove? Note that it should not keep the original mousemove event.
(I want to be able to modify the behavior of an object on a site via a user script)
Comment: Also, for some reason, `$(...).data("events")` isn't working now. Any reason why?
Answer: I'm fairly sure it's as simple as this:
$("#abc").mouseover($("#abc").mouseout);

Turns out, it's actually not that simple. You want this:
$("#abc").data("events")["mouseover"][0].handler;

Comment: I tried that with `off(mouseover)`. Using Visual Event, I can see that the mouseover event is still there, and the mouseout event is an empty bit.
Comment: Let's see. Editing my answer.
Comment: Actually, I want to make a mouseover-->mousemove (just used a different example). I have this code, and I'm trying to run it on the SO main page: `$('.post-tag').each(function(){
$(this).mousemove($(this).mouseover);
$(this).off("mouseover");

});`
Comment: Hmm, for some reason, `data("events")` isn't working. I'll be back in half an hour and I'll investigate.
Comment: Which version of jQuery are you using?
Comment: On the SO main page, try this in your console `$($(".post-tag")[0]).data("events")`


AWS SqsListener deserialize Custom Object with Jackson

Question: I can only listen to strings once I try to receive a custom object it throws the following error. It seems that I need to teach Spring to handle my custom object (B2BOrder)
org.springframework.messaging.converter.MessageConversionException: Cannot convert from [java.lang.String] to [br.com.b2breservas.api.model.B2BOrder] for GenericMessage [payload={"comments":"95d29059-8552-42fa-8fd9-a1d776416269"},
My SQSConfig
@Configuration
@EnableSqs
public class SqsConfig {

    private static final String DEFAULT_THREAD_NAME_PREFIX = ClassUtils.getShortName(SimpleMessageListenerContainer.class) + "-";

    @Bean
    public QueueMessagingTemplate myMessagingTemplate(AmazonSQSAsync amazonSqs, ResourceIdResolver resolver) {
        ObjectMapper mapper = new ObjectMapper()
                .registerModule(new ParameterNamesModule())
                .registerModule(new Jdk8Module())
                .registerModule(new JodaModule())
                .registerModule(new JavaTimeModule());
        // configure the Jackson mapper as needed
        // maybe I need to do something here!

        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setSerializedPayloadClass(String.class);
        converter.setStrictContentTypeMatch(false);
        converter.setObjectMapper(mapper);

        return new QueueMessagingTemplate(amazonSqs, resolver, converter);
    }

    @Bean
    public ClientConfiguration sqsClientConfiguration() {
        return new ClientConfiguration()
                .withConnectionTimeout(30000)
                .withRequestTimeout(30000)
                .withClientExecutionTimeout(30000);
    }

    @Bean
    public ExecutorFactory sqsExecutorFactory() {
        return new ExecutorFactory() {
            @Override
            public ExecutorService newExecutor() {
                return new ThreadPoolExecutor(2, 2, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
            }
        };
    }

    @Value("${b2b.b2b.accesstoken}")
    public String accesstoken;

    @Value("${b2b.b2b.secretkey}")
    public String secretkey;

    @Bean
    public AmazonSQSAsync amazonSqs(ClientConfiguration sqsClientConfiguration, ExecutorFactory sqsExecutorFactory) {
        BasicAWSCredentials credential = new BasicAWSCredentials(accesstoken, secretkey);
        return AmazonSQSAsyncClientBuilder.standard()
                .withClientConfiguration(sqsClientConfiguration)
                .withExecutorFactory(sqsExecutorFactory)
//                .withEndpointConfiguration(sqsEndpointConfiguration)
//                .withCredentials(credentialsProvider)
                .withCredentials(new AWSStaticCredentialsProvider(credential))
                .build();
    }

    @Bean
    public AsyncTaskExecutor queueContainerTaskEecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setThreadNamePrefix(DEFAULT_THREAD_NAME_PREFIX);
        threadPoolTaskExecutor.setCorePoolSize(2);
        threadPoolTaskExecutor.setMaxPoolSize(2);
        // No use of a thread pool executor queue to avoid retaining message to long in memory
        threadPoolTaskExecutor.setQueueCapacity(0);
        threadPoolTaskExecutor.afterPropertiesSet();
        return threadPoolTaskExecutor;
    }

    @Bean
    public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(AmazonSQSAsync amazonSqs, AsyncTaskExecutor queueContainerTaskEecutor) {
        SimpleMessageListenerContainerFactory factory = new SimpleMessageListenerContainerFactory();
        factory.setAmazonSqs(amazonSqs);
        factory.setAutoStartup(true);
//        factory.setQueueMessageHandler();
        factory.setMaxNumberOfMessages(1);
        factory.setWaitTimeOut(20);
        factory.setTaskExecutor(queueContainerTaskEecutor);
        return factory;
    }

}

Listener
@Component
public class SqsHub {

    @SqsListener(
            "https://sqs.us-west-2.amazonaws.com/3234/32443-checkout.fifo"
    )
    public void listen(B2BOrder message) {
//  public void listen(String message) { THIS WORKS!!
        System.out.println("!!!! received message {} {}" + message.toString());
    }
}

Sending
    ....
    @Autowired
    AmazonSQSAsync amazonSqs;

    @GetMapping("/yay")
    public String yay() {
        try {
            B2BOrder pendingOrder = new B2BOrder();
            pendingOrder.setComments(UUID.randomUUID().toString());
            String pendingOrderJson = objectMapper.writeValueAsString(pendingOrder);
            QueueMessagingTemplate queueMessagingTemplate = new QueueMessagingTemplate(amazonSqs);
            Map<String, Object> headers = new HashMap<>();
            headers.put(SqsMessageHeaders.SQS_GROUP_ID_HEADER, "my-application");
            headers.put(SqsMessageHeaders.SQS_DEDUPLICATION_ID_HEADER, UUID.randomUUID().toString());
            queueMessagingTemplate.convertAndSend("booking-checkout.fifo", pendingOrderJson, headers);
        } catch (final AmazonClientException | JsonProcessingException ase) {
            System.out.println("Error Message: " + ase.getMessage());
        }
        return "sdkjfn";
    }
    ....

Simple Custom Object

public class B2BOrder implements Serializable {

    @JsonProperty
    private String comments;

}

UPDATE
@Michiel answer took me here, but still got the same error.
    @Autowired
    public ObjectMapper objectMapper;

    @Bean
    public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(AmazonSQSAsync amazonSqs, AsyncTaskExecutor queueContainerTaskEecutor) {
        SimpleMessageListenerContainerFactory factory = new SimpleMessageListenerContainerFactory();
        factory.setAmazonSqs(amazonSqs);
        factory.setAutoStartup(true);
        QueueMessageHandlerFactory queueMessageHandlerFactory = new QueueMessageHandlerFactory();
        queueMessageHandlerFactory.setAmazonSqs(amazonSqs);
        MappingJackson2MessageConverter jsonMessageConverter = new MappingJackson2MessageConverter();
        jsonMessageConverter.setObjectMapper(objectMapper);
        queueMessageHandlerFactory.setMessageConverters(Collections.singletonList(jsonMessageConverter));
        factory.setQueueMessageHandler(queueMessageHandlerFactory.createQueueMessageHandler());
//        factory.setMaxNumberOfMessages(1);
        factory.setWaitTimeOut(20);
        factory.setTaskExecutor(queueContainerTaskEecutor);
        return factory;
    }
    ```

Answer: Although you have registered a MessageConverter, it is only configured to be used in the outgoing request (using the QueueMessagingTemplate). Your MessageListener does not have a MessageConverter configuration. Therefore, incoming messages can only be retrieved as a 'raw' type such as String.
In your snippet you have commented the following line of code:
//        factory.setQueueMessageHandler();

This is the location where you could set a QueueMessageHandler that itself has one or more MessageConverters attached.
[edit]
Sure:
QueueMessageHandlerFactory handlerFactory = new QueueMessageHandlerFactory();
handlerFactory.setMessageConverters(yourJacksonConfig);
QueueMessageHandler messageHandler = handlerFactory.createQueueMessageHandler();
factory.setQueueMessageHandler(messageHandler);

This Spring documentation might be of help.
Comment: it makes sense. do you have any example of a message converter I could look at?
Answer: I'm using a SimpleMessageListenerContainer which is defined in a Bean as follows
@Bean
@Primary
fun simpleMessageListenerContainerFactory(amazonSQS: AmazonSQSAsync): SimpleMessageListenerContainerFactory =
    SimpleMessageListenerContainerFactory().apply {
        setAmazonSqs(amazonSQS)
        setMaxNumberOfMessages(10)
        setAutoStartup(false)
    }

(by the way, I'm using Kotlin)
This is bean is created outside my project, so in order to inject into the SimpleMessageListenerContainerFactory a new converter I've done as @Michiel has suggested:
@Bean
fun Handler(): QueueMessageHandlerFactory {
    val handlerFactory = QueueMessageHandlerFactory()
    handlerFactory.messageConverters = listOf(jsonMessageConverter())
    return handlerFactory
}


Polynomial roots with sum of coefficients prime

Question: There exists a quadratic polynomial $P(x)$ with integer coefficients such that: (a) both of its roots are positive integers, (b) the sum of its coefficients is prime, and (c) for some integer k, $P(k) = −55$. Show that one of its roots is $2$, and find the other root.
This is a problem from Canada 2001. I found this on a note by Adeel Khan.
My process is, Let the roots be $\alpha$ and $\beta$ and $P(x)=ax^2+bx+c$. By the second condition, we have
$$a(1-\alpha)(1-\beta)=\pm p\text{ for some prime }p$$
Clearly, $a,b$ are distinct, otherwise it violates the third condition. Therefore, one of them is $2$ (WLOG $\alpha=2$) and $a=\pm1$.
If $a=1$, $$(1-\beta)=p\implies\beta=1-p$$
$$b=-(\alpha+\beta)=p-3$$
$$c=\alpha\beta=2-2p$$
hence, $$P(2)=4a+2b+c=4a+2p-6+2-2p=4a+4(p-1)=0\implies p-1=a=1\implies p=2\implies\beta=-1$$
which is a contradiction. Therefore $a=-1$.
Tell me if I have done any mistake and what to do next.
Comment: **Correction:** $a=1-p=1$ leads to a contradiction. That's not $p-1$.
Comment: Did you mean to write "Clearly $\alpha$, $\beta$ are unique"?  instead of $a,b$?  I don't see how you conclude that $a,b$ are distinct...
Comment: I think what you have done so far (with the correction) is right, then you just have to go on : you must have $a=-1$, $\alpha=2$ and $\beta=p+1$. so $P(x)=x^2-(p+3)x+2(p+1)$ ...
Answer: Assume the quadratic to be $a(x-p)(x-q)$ having positive roots p and q.
The sum of coefficients is $a(1-p-q+pq)$ = $a(p-1)(q-1)$.
For this to be prime, any two out of a,$(p-1)$ or $(q-1)$ be 1 and the other must be prime.
Therefore, at least one out of p or q will be $24.
This proves the first part.
Now, figure out $a(x-p)(x-2)=-55$
We shall take a=$1$ (as both roots are not same)
We end with $(x-p)(x-2)=-55$
Now, the factors should one out of $(-11,5) , (11,-5), (-5,11)$ or $(5,-11)$
Here, $(-11,5)$ and $(-5,11)$ give $p=18$
while $(11,-5) $ and $(5,-11)$ give $p=-9$
Since, p is a positive root.
Therefore p should be $18$ and the value of k is $7$ or $13$
Comment: May I know why my answer was downvoted?


How does a fixed point of a dynamical system change when the system is changed slightly?

Question: I've come across this problem in the course of my work, and I'm a bit stuck on it.
Suppose I have an $n$-dimensional smooth dynamical system
$$
\dot{x_i} = f_i(\mathbf{x}),
$$
and suppose there is a fixed point $\hat{\mathbf{x}}$, i.e. $f_i(\hat{\mathbf{x}})=0$ for every $i$.
Now let us define a new dynamical system
$$
\dot{x_i} = f'_i(\mathbf{x}),
$$
where $f_i'(\mathbf{x}) = f_i(\mathbf{x}) + \epsilon\delta f_i(\mathbf{x})$, for some set of arbitrary smooth functions $\delta f_i$.
It's possible that the fixed point of the original system was a bifurcation point, so that there will be no corresponding fixed point in the new system, no matter how small we make $\epsilon$. But supposing this isn't the case, the fixed point should just move to a slightly different position. That is, $\hat{\mathbf{x}}' = \hat{\mathbf{x}} + \delta \hat{\mathbf{x}}$, where every $f_i'(\hat{\mathbf{x}}')=0$ and $\delta\hat{\mathbf{x}}$ will be of the same order as $\epsilon$.
What I want is an expression for $\delta\hat{\mathbf{x}}$ (in the small $\epsilon$ limit) in terms of $\epsilon$ and the partial derivatives of the functions $f_i$ and $\delta f_i$ at the original fixed point. It seems like it should be possible, but after going cross-eyed scribbling on the whiteboard I haven't come up with the answer. I'm probably just missing some simple trick - can anyone help me out?
Answer: I will in the following do nothing but simple calculus trick. Assume $f'(x)=f(x)+\epsilon g(x)$ where $g(x)$ is arbitrary smooth function. Denote fixed points of two dynamical systems by $x_0,x_0'$ so that $f(x_0)=f'(x_0')=0$ and $\delta x=x_0'-x_0$. Then we have
$$\begin{align}f'(x'_0)&=f(x_0+\delta x)+\epsilon g(x_0+\delta x)\\
&=f(x_0)+Jf(x_0)\delta x+\epsilon\left(g(x_0)+Jg(x_0)\delta x\right)\\
&=Jf(x_0)\delta x+\epsilon g(x_0)\\
&=0\end{align}$$
Hence $\delta x=-\epsilon(Jf)^{-1}(x_0)g(x_0)$ where $Jf$ is Jacob matrix. Note that in the second line, $f(x_0)=0$ and $\epsilon\delta xJg(x_0)$ is infinitesimal compared to $\epsilon$.
Comment: Thanks - I knew it would be simple, I just wasn't looking at it in the right way. In the multivariate case it becomes $\delta \mathbf{x} = -\epsilon J^{-1}\mathbf{g}(\mathbf{x}_0)$, where $J$ is the Jacobian matrix, i.e. $J_{ij} = \partial f_i/\partial x_j$, or $(J^{-1})_{ij} = \partial x_i/\partial f_j$.
Comment: @Nathaniel Oh yes, it should be an inverse Jacob matrix. My naive symbols misled myself.