if not (1 <= tmp['fmin']['theta'] < com.S and \ 1e-50 <= tmp['fmin']['m'] < 1-1e-50): del (tmp['fmin']) # find the model with the higher likelihood: met = min(tmp, key=lambda x: tmp[x]['lnL']) # load it as 'etienne' model com.set_model(tmp[met]['model']) lrt = com.lrt('ewens', 'etienne') best = 'ewens' if lrt > 0.05 else 'etienne' print 'Best model by LRT was: ' + best com.generate_random_neutral_distribution(model=best) pval, neut_h = com.test_neutrality(model=best, gens=10000, full=True) #draw_shannon_distrib(neut_h, abd.shannon) draw_shannon_distrib(neut_h, com.shannon, outfile='Fulcaldea_stuessyi_log_shannon_dist.pdf', filetype='pdf') print 'P-value for neutrality test was: ', pval out = open('Fulcaldea_stuessyi_log_shannon_neutral_data.tsv', 'w') out.write('# shannon:' + str(com.shannon) + '\n') out.write('\n'.join([str(s for s in neut_h)]) + '\n') out.close() com.dump_community('Fulcaldea_stuessyi_log_ecolopy.pik')
import matplotlib matplotlib.use('Agg') from ecolopy_dev import Community from ecolopy_dev.utils import draw_shannon_distrib abd = Community('dataset_trial/bci_short.txt') abd.fit_model('etienne') abd.set_current_model('etienne') pval, neut_h = abd.test_neutrality (model='etienne', gens=1000, full=True) draw_shannon_distrib(neut_h, abd.shannon, outfile='lala.png') abd2 = Community('test2.txt') abd2.fit_model('lognormal') abd2.set_current_model('lognormal') pval, neut_h = abd2.test_neutrality (model='lognormal', gens=1000, full=True) draw_shannon_distrib(neut_h,abd2.shannon) bci = Community('../dataset_trial/bci_full.txt') bci.fit_model('lognormal') bci.set_current_model('lognormal') pval, bci_neut_h = bci.test_neutrality (model='lognormal', gens=1000, full=True)
print ' optimization failed: ' + e.args[0] # in case optimization by fmin failed to found correct values for theta and m: if not (1 <= tmp['fmin']['theta'] < com.S and \ 1e-50 <= tmp['fmin']['m'] < 1-1e-50): del (tmp['fmin']) # find the model with the higher likelihood: met = min(tmp, key=lambda x: tmp[x]['lnL']) # load it as 'etienne' model com.set_model(tmp[met]['model']) lrt = abd.lrt('ewens', 'etienne') best = 'ewens' if lrt > 0.05 else 'etienne' print 'Best model by LRT was: ' + best com.generate_random_neutral_distribution(model=best) pval, neut_h = com.test_neutrality (model=best, gens=10000, full=True) #draw_shannon_distrib(neut_h, abd.shannon) draw_shannon_distrib(neut_h, abd.shannon, outfile='Fulcaldea_stuessyi_1kfrac_shannon_dist.pdf', filetype='pdf') print 'P-value for neutrality test was: ' + pval out = open('Fulcaldea_stuessyi_1kfrac_shannon_neutral_data.tsv', 'w') out.write('# shannon:' + str(shannon) + '\n') out.write('\n'.join([str(s for s in neut_h)]) + '\n') out.close() com.dump_abundance('Fulcaldea_stuessyi_1kfrac_ecolopy.pik')
# find the model with the higher likelihood: met = min(tmp, key=lambda x: tmp[x]['lnL']) # load it as 'etienne' model com.set_model(tmp[met]['model']) lrt = com.lrt('ewens', 'etienne') best = 'ewens' if lrt > 0.05 else 'etienne' print 'Best model by LRT was: ' + best com.generate_random_neutral_distribution(model=best) pval, neut_h = com.test_neutrality(model=best, gens=10000, full=True, method='shannon', n_cpus=4) draw_shannon_distrib(neut_h, com.shannon, outfile='test_log_shannon_dist.pdf', filetype='pdf') print 'P-value for neutrality test was: ', pval out = open('test_log_shannon_neutral_data.tsv', 'w') out.write('# shannon:' + str(com.shannon) + '\n') out.write('\n'.join([str(s) for s in neut_h]) + '\n') out.close() com.dump_community('test_log_ecolopy.pik')
except Exception as e: print ' optimization failed: ' + e.args[0] # in case optimization by fmin failed to found correct values for theta and m: if not (1 <= tmp['fmin']['theta'] < com.S and \ 1e-50 <= tmp['fmin']['m'] < 1-1e-50): del (tmp['fmin']) # find the model with the higher likelihood: met = min(tmp, key=lambda x: tmp[x]['lnL']) # load it as 'etienne' model com.set_model(tmp[met]['model']) lrt = com.lrt('ewens', 'etienne') best = 'ewens' if lrt > 0.05 else 'etienne' print 'Best model by LRT was: ' + best com.generate_random_neutral_distribution(model=best) pval, neut_h = com.test_neutrality (model=best, gens=10000, full=True, method='shannon', n_cpus=4) draw_shannon_distrib(neut_h, com.shannon, outfile='test_log_shannon_dist.pdf', filetype='pdf') print 'P-value for neutrality test was: ', pval out = open('test_log_shannon_neutral_data.tsv', 'w') out.write('# shannon:' + str(com.shannon) + '\n') out.write('\n'.join([str(s) for s in neut_h]) + '\n') out.close() com.dump_community('test_log_ecolopy.pik')
t0 = time() print """ This is exactly the starting point in order to compute a neutrality test. First we should generate many random distributions, and compute their shannon entropy. To finally compare the distribution of shannon entropy to our real data. All this is done with the test_neutrality function, by default only 100 generations will be computed, so it is a good thing to change default options, this function will return the p-value of rejection of neutrality: >>> com.test_neutrality(gens=1000) %s (still this is a quick example, number of generation around 100000 should be used) """ % (com.test_neutrality(gens=1000)) took = time()-t0 print ' ...it took:', int(took/60), 'min', int (took%60), 'sec' print """ ... type 'enter' to continue """ raw_input() print """ This is it for the main function of the package, however user will find nice. ability to store an abundance object: >>> com.dump_abundance('stored_bci.pik') to load it afterward, (it will avoid from running again computation of K(D,A)) >>> com = Community ('stored_bci.pik')