本文整理汇总了Python中pyximport.install函数的典型用法代码示例。如果您正苦于以下问题:Python install函数的具体用法?Python install怎么用?Python install使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了install函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write_spline_data
def write_spline_data():
"""Precompute spline coefficients and save them to data files that
are #included in the remaining c source code. This is a little devious.
"""
import scipy.special
import pyximport
pyximport.install(setup_args={"include_dirs": [np.get_include()]})
sys.path.insert(0, "src/vonmises")
import buildspline
del sys.path[0]
n_points = 1024
miny, maxy = 1e-5, 700
y = np.logspace(np.log10(miny), np.log10(maxy), n_points)
x = scipy.special.iv(1, y) / scipy.special.iv(0, y)
# fit the inverse function
derivs = buildspline.createNaturalSpline(x, np.log(y))
if not os.path.exists("src/vonmises/data/inv_mbessel_x.dat"):
np.savetxt("src/vonmises/data/inv_mbessel_x.dat", x, newline=",\n")
if not os.path.exists("src/vonmises/data/inv_mbessel_y.dat"):
np.savetxt("src/vonmises/data/inv_mbessel_y.dat", np.log(y), newline=",\n")
if not os.path.exists("src/vonmises/data/inv_mbessel_deriv.dat"):
np.savetxt("src/vonmises/data/inv_mbessel_deriv.dat", derivs, newline=",\n")
示例2: cython_pyximport
def cython_pyximport(self, line, cell):
"""Compile and import a Cython code cell using pyximport.
The contents of the cell are written to a `.pyx` file in the current
working directory, which is then imported using `pyximport`. This
magic requires a module name to be passed::
%%cython_pyximport modulename
def f(x):
return 2.0*x
The compiled module is then imported and all of its symbols are
injected into the user's namespace. For most purposes, we recommend
the usage of the `%%cython` magic.
"""
module_name = line.strip()
if not module_name:
raise ValueError('module name must be given')
fname = module_name + '.pyx'
with io.open(fname, 'w', encoding='utf-8') as f:
f.write(cell)
if 'pyximport' not in sys.modules:
import pyximport
pyximport.install(reload_support=True)
if module_name in self._reloads:
module = self._reloads[module_name]
reload(module)
else:
__import__(module_name)
module = sys.modules[module_name]
self._reloads[module_name] = module
self._import_all(module)
示例3: __init__
def __init__(self, call=None, *args):
# Delay import until use of class instance. We were getting some very
# odd build-as-we-go errors running tests and documentation otherwise
import pyximport
pyximport.install()
try:
ext = os.path.splitext(call)[1].lower()
print('ext', ext)
if ext == '.py' or ext == '.pyx': # python/cython file
print('profiling python/cython file ...')
subprocess.call(['python', '-m', 'cProfile',
'-o', 'profile.prof', call])
s = pstats.Stats('profile.prof')
stats = s.strip_dirs().sort_stats('time')
self.stats = stats
except:
print('profiling function call ...')
self.args = args
self.call = call
cProfile.runctx('self._profile_function()', globals(), locals(),
'profile.prof')
s = pstats.Stats('profile.prof')
stats = s.strip_dirs().sort_stats('time')
self.stats = stats
示例4: test_cython_quicksort
def test_cython_quicksort(self):
import pyximport; pyximport.install()
import quick_cython
l = [5, 1, 3, 5, 2]
self.assertEqual(sorted(l), quick_cython.quicksort(l))
示例5: install_pyx
def install_pyx():
try:
import pyximport
log.info('Gearing up with Cython')
pyximport.install()
except Exception, e:
log.info(e)
示例6: test_hamming_speed_01
def test_hamming_speed_01():
import time
#Importing cython module:
import pyximport; pyximport.install()
import hamming_cython
num_peptide = 1000
protein_length = 1000
peptide_list = get_peptide_list_random(n=num_peptide)
protein_seq = get_protein_seq_random(protein_length=protein_length)
xi = time.time()
match_list = get_match_list(peptide_list, protein_seq, sim_fraction_cutoff=0.5)
xj = time.time()
dx = xj-xi
print ['%.2fs' % dx, 'num_peptide=',num_peptide, 'protein_length=',protein_length, 'len(match_list)', len(match_list)]
#print match_list[0:5]
xi = time.time()
match_list = hamming_cython.get_match_list_cython(peptide_list, protein_seq, 0.5)
xj = time.time()
dx = xj-xi
print ['%.2fs' % dx, 'num_peptide=',num_peptide, 'protein_length=',protein_length, 'len(match_list)', len(match_list)]
#print match_list[0:5]
xi = time.time()
match_list = hamming_cython.get_match_list_numpy(peptide_list, protein_seq, 0.5)
xj = time.time()
dx = xj-xi
print ['%.2fs' % dx, 'num_peptide=',num_peptide, 'protein_length=',protein_length, 'len(match_list)', len(match_list)]
示例7: call
def call(self, fun, arg=list()):
'''
Call a function in the load path.
'''
name = fun[:fun.rindex('.')]
try:
fn_, path, desc = imp.find_module(name, self.module_dirs)
mod = imp.load_module(name, fn_, path, desc)
except ImportError:
if self.opts.get('cython_enable', True) is True:
# The module was not found, try to find a cython module
try:
import pyximport
pyximport.install()
for mod_dir in self.module_dirs:
for fn_ in os.listdir(mod_dir):
if name == fn_[:fn_.rindex('.')]:
# Found it, load the mod and break the loop
mod = pyximport.load_module(
name, os.path.join(mod_dir, fn_)
)
return getattr(
mod, fun[fun.rindex('.') + 1:])(*arg)
except ImportError:
log.info("Cython is enabled in options though it's not "
"present in the system path. Skipping Cython "
"modules.")
return getattr(mod, fun[fun.rindex('.') + 1:])(*arg)
示例8: load_matrix
def load_matrix(f):
if not f.endswith('.bin'):
f += ".bin"
import pyximport
pyximport.install(setup_args={"include_dirs": np.get_include()})
from representations import sparse_io
return sparse_io.retrieve_mat_as_coo(f).tocsr()
示例9: test_cython2_quicksort
def test_cython2_quicksort(self):
import pyximport; pyximport.install()
import quick_cython_2
l = [3, 2, 5, 1, 2, 5]
quick_cython_2.quicksort(l)
self.assertEqual(sorted(l), l)
示例10: benchmark
def benchmark(v, pyx, b='b'):
# write each .pyx iteration
with open('faster_hash_v{}.pyx'.format(v), mode='w') as f:
f.write(pyx)
# install .pyx
pyximport.install()
# timeit
print('v{}:'.format(v), timeit.timeit('fnv1a(b)', setup='from faster_hash_v{v} import fnv1a; b = {b}"{u}"'.format(v=v, u=u, b=b)))
示例11: init
def init():
global _initialized
if not _initialized:
pyximport.install(
setup_args = {
'include_dirs': [np.get_include()]
}
)
_initialized = True
示例12: test_PyDecay
def test_PyDecay():
import pyximport
pyximport.install()
from _cvodes_anyode import PyDecay
pd = PyDecay(1.0)
tout, yout = pd.adaptive(1.0, 1.0)
for t, y in zip(tout, yout):
assert abs(y - exp(-t)) < 1e-9
示例13: test_PyDecay_mxsteps
def test_PyDecay_mxsteps():
import pyximport
pyximport.install()
from _odeint_anyode import PyDecay
import pytest
pd = PyDecay(1.0)
with pytest.raises(Exception):
tout, yout = pd.adaptive(1.0, 1.0, mxsteps=1)
示例14: fit_transform
def fit_transform(self, X:Union[csr_matrix, memmap],
n_docs_distribution,
n_jobs=1,
verbose=False,
joblib_backend='multiprocessing',
use_cython:bool=False):
"""Main method of PMI class.
"""
assert isinstance(X, (memmap, csr_matrix))
assert isinstance(n_docs_distribution, numpy.ndarray)
matrix_size = X.shape
sample_range = list(range(0, matrix_size[0]))
feature_range = list(range(0, matrix_size[1]))
n_total_document = sum(n_docs_distribution)
logger.debug(msg='Start calculating PMI with n(process)={}'.format(n_jobs))
logger.debug(msg='size(input_matrix)={} * {}'.format(X.shape[0], X.shape[1]))
if use_cython:
import pyximport; pyximport.install()
from DocumentFeatureSelection.pmi.pmi_cython import main
logger.warning(msg='n_jobs parameter is invalid when use_cython=True')
pmi_score_csr_source = main(X=X,
n_docs_distribution=n_docs_distribution,
sample_range=sample_range,
feature_range=feature_range,
n_total_doc=n_total_document,
verbose=False)
else:
self.pmi = pmi
pmi_score_csr_source = joblib.Parallel(n_jobs=n_jobs, backend=joblib_backend)(
joblib.delayed(self.docId_word_PMI)(
X=X,
n_docs_distribution=n_docs_distribution,
feature_index=feature_index,
sample_index=sample_index,
n_total_doc=n_total_document,
verbose=verbose
)
for sample_index in sample_range
for feature_index in feature_range
)
row_list = [t[0] for t in pmi_score_csr_source]
col_list = [t[1] for t in pmi_score_csr_source]
data_list = [t[2] for t in pmi_score_csr_source]
pmi_featured_csr_matrix = csr_matrix((data_list, (row_list, col_list)),
shape=(X.shape[0],
X.shape[1]))
logging.debug(msg='End calculating PMI')
return pmi_featured_csr_matrix
示例15: mu_q_method
def mu_q_method(e):
cython_header = 'import numpy as np\ncimport numpy as np\nctypedef np.double_t DTYPE_t\ncimport cython\n\[email protected](False)\[email protected](False)\[email protected](True)\ndef mu_q(%s):\n\tcdef double mu_q\n'
#@todo - for Cython cdef variables and generalize function def()
arg_values = {}
eps_arr = e
arg_values['e_arr'] = eps_arr
for name, theta_arr in zip(self.rand_var_names, self.theta_arrs):
arg_values[ '%s_flat' % name ] = theta_arr
arg_values.update(self._get_arg_values_dG())
def_dec = 'np.ndarray[DTYPE_t, ndim=1] '
def_dec += ', np.ndarray[DTYPE_t, ndim=1] '.join(arg_values)
cython_header = cython_header % def_dec
cython_header += ' cdef double '
cython_header += ', '.join(self.var_names) + ', eps, dG, q\n'
cython_header += ' cdef int i_'
cython_header += ', i_'.join(self.var_names) + '\n'
if self.cached_dG:
cython_header = cython_header.replace(r'1] dG_grid', r'%i] dG_grid' % self.n_rand_vars)
if self.compiled_eps_loop == False:
cython_header = cython_header.replace(r'np.ndarray[DTYPE_t, ndim=1] e_arr', r'double e_arr')
cython_header = cython_header.replace(r'eps,', r'eps = e_arr,')
cython_code = (cython_header + self.code).replace('\t', ' ')
cython_file_name = 'spirrid_cython.pyx'
regenerate_code = True
if os.path.exists(cython_file_name):
f_in = open(cython_file_name, 'r').read()
if f_in == cython_code:
regenerate_code = False
if regenerate_code:
infile = open('spirrid_cython.pyx', 'w')
infile.write(cython_code)
infile.close()
print 'pyx file updated'
import pyximport
pyximport.install(reload_support = True)
import spirrid_cython
if regenerate_code:
reload(spirrid_cython)
mu_q = spirrid_cython.mu_q
if self.compiled_eps_loop:
mu_q_arr = mu_q(**arg_values)
else:
# Python loop over eps
#
mu_q_arr = np.zeros_like(eps_arr)
for idx, e in enumerate(eps_arr):
# C loop over random dimensions
#
arg_values['e_arr'] = e # prepare the parameter
mu_q_val = mu_q(**arg_values)
# add the value to the return array
mu_q_arr[idx] = mu_q_val
return mu_q_arr, None