Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def compute():
"""Compute the polynomial."""
if what == "numpy":
y = eval(expr)
else:
y = ne.evaluate(expr)
return len(y)
if __name__ == '__main__':
if len(sys.argv) > 1: # first arg is the package to use
what = sys.argv[1]
if len(sys.argv) > 2: # second arg is the number of threads to use
nthreads = int(sys.argv[2])
if "ncores" in dir(ne):
ne.set_num_threads(nthreads)
if what not in ("numpy", "numexpr"):
print "Unrecognized module:", what
sys.exit(0)
print "Computing: '%s' using %s with %d points" % (expr, what, N)
t0 = time()
result = compute()
ts = round(time() - t0, 3)
print "*** Time elapsed:", ts
"""
import os
import sys
sys.setrecursionlimit(8192)
import time
import re
import shutil
import zlib
import numpy as np
import bcolz
import numexpr as ne
bcolz.blosc_set_nthreads(2)
ne.set_num_threads(2)
import sqlalchemy as sql
import database
import compression
from gemini_utils import get_gt_cols
def get_samples(metadata):
return [x['name'] for x in metadata.tables['samples'].select().order_by("sample_id").execute()]
def get_n_variants(cur):
return next(iter(cur.execute(sql.text("select count(*) from variants"))))[0]
def get_bcolz_dir(db):
if not "://" in db:
return db + ".gts"
else:
def execute(threadCount):
n = 100000000 # 10 times fewer than C due to speed issues.
delta = 1.0 / n
startTime = time()
set_num_threads(threadCount)
value = arange(n, dtype=double)
pi = 4.0 * delta * evaluate('1.0 / (1.0 + ((value - 0.5) * delta) ** 2)').sum()
elapseTime = time() - startTime
out(__file__, pi, n, elapseTime, threadCount)
ne.evaluate('a>1000')
print "numexpr--> %.3g" % ((time()-t0)/ntimes,)
t0 = time()
for i in xrange(ntimes):
eval('a>1000')
print "numpy--> %.3g" % ((time()-t0)/ntimes,)
if __name__ == "__main__":
print "****** Testing with 1 thread..."
ne.set_num_threads(1)
for N in range(10, 20):
bench(2**N)
print "****** Testing with 2 threads..."
ne.set_num_threads(2)
for N in range(10, 20):
bench(2**N)
prior = 4 - np.log(73.53 * p0 * (N ** -0.478))
logger.debug("Finding blocks...")
# This is where the computation happens. Following Scargle et al. 2012.
# This loop has been optimized for speed:
# * the expression for the fitness function has been rewritten to
# avoid multiple log computations, and to avoid power computations
# * the use of scipy.weave and numexpr has been evaluated. The latter
# gives a big gain (~40%) if used for the fitness function. No other
# gain is obtained by using it anywhere else
# Set numexpr precision to low (more than enough for us), which is
# faster than high
oldaccuracy = numexpr.set_vml_accuracy_mode("low")
numexpr.set_num_threads(1)
numexpr.set_vml_num_threads(1)
# Speed tricks: resolve once for all the functions which will be used
# in the loop
numexpr_evaluate = numexpr.evaluate
numexpr_re_evaluate = numexpr.re_evaluate
# Pre-compute this
aranges = np.arange(N + 1, 0, -1)
for R in range(N):
br = block_length[R + 1]
T_k = (
block_length[: R + 1] - br
) # this looks like it is not used, but it actually is,
# MapPy
# from mappy.utilities import composite
from mpglue import raster_tools
from mpglue.helpers import _iteration_parameters_values
# Numpy
try:
import numpy as np
except ImportError:
raise ImportError('NumPy must be installed')
# Numexpr
try:
import numexpr as ne
ne.set_num_threads(mpr.cpu_count())
numexpr_installed = True
except:
numexpr_installed = False
# Carray
# try:
# import carray as ca
# carray_installed = True
# except:
# carray_installed = False
# GDAL
try:
from osgeo import gdal
from osgeo.gdalconst import *
except ImportError:
args = []
varsDict = vars()
r = np.empty(self.size, dtype=self.dtype)
for I in range(Case._MAX_ARGS):
argName = 'x%d'%I
if argName in self.expr:
argArray = np.arange(self.size, dtype=self.dtype)
varsDict[argName] = argArray
args.append(argArray)
x1 = args[0]
x2 = args[1]
# Run benchmark `tries` times
ne3.set_nthreads(self.nthreads)
ne2.set_num_threads(self.nthreads)
# Numba does not have dynamic thread setting
for I in range(self.tries):
# Grab local references to our attributes
timings = self.timings; stats = self.stats
t0 = pc()
nefunc = ne3.NumExpr( self.expr )
t1 = pc()
nefunc()
t2 = pc()
timings['total_run'][I] = t2 - t1
timings['total_assemble'][I] = t1 - t0
timings['total'][I] = t2 - t0
if bool(self.compares):
for name, func in self.compares.items():
elapsedTimes_ne = timeit.repeat("N.calcRlogRdotv_numexpr(R, v)",
setup=setup, number=1, repeat=repeat)
meanTime_np = np.mean(elapsedTimes_np)
meanTime_ne = np.mean(elapsedTimes_ne)
expectedGainFactor = meanTime_np / meanTime_ne
return expectedGainFactor
hasNumexpr = True
try:
import numexpr as ne
except ImportError:
hasNumexpr = False
if hasNumexpr and 'OMP_NUM_THREADS' in os.environ:
try:
nThreads = int(os.environ['OMP_NUM_THREADS'])
ne.set_num_threads(nThreads)
except TypeError as ValueError:
print 'Unrecognized OMP_NUM_THREADS', os.environ['OMP_NUM_THREADS']
pass
LoadConfig()
def set_numexpr_threads(n=None):
# if we are using numexpr, set the threads to n
# otherwise reset
if _NUMEXPR_INSTALLED and _USE_NUMEXPR:
if n is None:
n = ne.detect_number_of_cores()
ne.set_num_threads(n)