Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def bench(N):
print "*** array length:", N
a = np.arange(N)
t0 = time()
ntimes = (1000*2**15) // N
for i in xrange(ntimes):
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,)
import numpy as np
import numexpr as ne
import pandas as pd
from pandas import DataFrame, eval as pd_eval
import seamless.pandeval
from seamless.pandeval.core.computation.eval import eval
# Sample data
s = np.zeros(6, 'S5')
v = np.array([5,8,3,6,7,2])
s[:4] = "Test", "Test2", "Test", "Test"
s_u = np.array([ss.decode() for ss in s]) # s as unicode
dic = {"s": s, "v": v}
dic_u = {"s": s_u, "v": v}
print("Golden standard (numexpr with ugly syntax)\t", ne.compute("(s == 'Test') & (v > 3)", dic))
df = DataFrame(dic)
print("pandas DataFrame with bytes (error)\t\t", df.eval("s == 'Test' and v > 3").values)
try: # ugly syntax AND does not work
print(df.eval("s == b'Test' and v > 3").values)
except AttributeError:
print("*" * 50)
import traceback; traceback.print_exc(0)
print("*" * 50)
df_u = DataFrame(dic_u)
print("pandas DataFrame with unicode (works)\t\t", df_u.eval("s == 'Test' and v > 3").values)
print("pandas eval with bytes (wrong)\t\t\t", pd_eval("s == 'Test' and v > 3", global_dict=dic))
print("pandas eval with unicode (wrong)\t\t", pd_eval("s == 'Test' and v > 3", global_dict=dic_u))
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:
if expression:
compare_times(expression, 1)
sys.exit(0)
nexpr = 0
for expr in expressions:
nexpr += 1
compare_times(expr, nexpr)
print
if __name__ == '__main__':
import numexpr
numexpr.print_versions()
numpy.seterr(all='ignore')
numexpr.set_vml_accuracy_mode('low')
numexpr.set_vml_num_threads(2)
if len(sys.argv) > 1:
expression = sys.argv[1]
print "expression-->", expression
compare(expression)
else:
compare()
tratios = numpy.array(numpy_ttime) / numpy.array(numexpr_ttime)
stratios = numpy.array(numpy_sttime) / numpy.array(numexpr_sttime)
ntratios = numpy.array(numpy_nttime) / numpy.array(numexpr_nttime)
print "eval method: %s" % eval_method
print "*************** Numexpr vs NumPy speed-ups *******************"
# we do not include PyQt because it is an indirect dependency. It might
# be a good idea to include those too, but the list will get long in
# that case.
print("""
python {py}
larray {la}
numexpr {ne}
numpy {np}
pandas {pd}
pytables {pt}
pyyaml {yml}
bcolz {bc}
bottleneck {bn}
matplotlib {mpl}
vitables {vt}
""".format(py=py_version, la=la.__version__, ne=ne.__version__,
np=np.__version__, pd=pd.__version__, pt=pt.__version__,
yml=yaml.__version__,
bc=bcolz_version, bn=bn_version, mpl=mpl_version, vt=vt_version,
))
parser.exit()
pass
# Check for NumPy
check_import('numpy', min_numpy_version)
# Check for Numexpr
numexpr_here = False
try:
import numexpr
except ImportError:
print_warning("Numexpr not detected. Disabling support for it.")
else:
if numexpr.__version__ >= min_numexpr_version:
numexpr_here = True
print ( "* Found %(pkgname)s %(pkgver)s package installed."
% {'pkgname': 'numexpr', 'pkgver': numexpr.__version__} )
else:
print_warning(
"Numexpr %s detected, but version is not >= %s. "
"Disabling support for it." % (
numexpr.__version__, min_numexpr_version))
########### End of version checks ##########
# carray version
VERSION = open('VERSION').read().strip()
# Create the version.py file
open('carray/version.py', 'w').write('__version__ = "%s"\n' % VERSION)
# Global variables
CFLAGS = os.environ.get('CFLAGS', '').split()
compare_times(expression, 1)
sys.exit(0)
nexpr = 0
for expr in expressions:
nexpr += 1
compare_times(expr, nexpr)
print
if __name__ == '__main__':
import numexpr
numexpr.print_versions()
numpy.seterr(all='ignore')
numexpr.set_vml_accuracy_mode('low')
numexpr.set_vml_num_threads(2)
if len(sys.argv) > 1:
expression = sys.argv[1]
print "expression-->", expression
compare(expression)
else:
compare()
tratios = numpy.array(numpy_ttime) / numpy.array(numexpr_ttime)
stratios = numpy.array(numpy_sttime) / numpy.array(numexpr_sttime)
ntratios = numpy.array(numpy_nttime) / numpy.array(numexpr_nttime)
print "eval method: %s" % eval_method
print "*************** Numexpr vs NumPy speed-ups *******************"
# print "numpy total:", sum(numpy_ttime)/iterations
def _getNCPUs(self):
return 1
def __get_nbits(self):
abits = platform.architecture()[0]
nbits = re.compile('(\d+)bit').search(abits).group(1)
return nbits
def _is_32bit(self):
return self.__get_nbits() == '32'
def _is_64bit(self):
return self.__get_nbits() == '64'
class LinuxCPUInfo(CPUInfoBase):
info = None
def __init__(self):
if self.info is not None:
return
info = [{}]
ok, output = getoutput(['uname', '-m'])
if ok:
info[0]['uname_m'] = output.strip()
try:
fo = open('/proc/cpuinfo')
except EnvironmentError as e:
warnings.warn(str(e), UserWarning)
else:
for line in fo:
name_value = [s.strip() for s in line.split(':', 1)]
import warnings
try:
from collections.abc import Mapping
except ImportError:
from collections import Mapping
import numexpr as ne
__all__ = ['ScalarProperty']
class ScalarProperty(object):
_evaluate = staticmethod(ne.evaluate)
def __init__(self, expr, name=None, unit=None, symbol=None):
'''
Represents a scalar particle property.
expr - The expression how to calcualte the value (string).
name - the name the property can be accessed by.
unit - unit of property.
symbol - symbol used in formulas. Defaults to 'name' if omitted.
'''
self._expr = expr
self._name = name
self._unit = unit
self._symbol = symbol
self._func_cache = None # Optimized numexpr function if available