Python Cython.Build.cythonize() Examples

The following are 30 code examples of Cython.Build.cythonize(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module Cython.Build , or try the search function .
Example #1
Source File: __init__.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def maybe_cythonize_extensions(top_path, config):
    """Tweaks for building extensions between release and development mode."""
    is_release = os.path.exists(os.path.join(top_path, 'PKG-INFO'))

    if is_release:
        build_from_c_and_cpp_files(config.ext_modules)
    else:
        message = ('Please install cython with a version >= {0} in order '
                   'to build a scikit-learn development version.').format(
                       CYTHON_MIN_VERSION)
        try:
            import Cython
            if LooseVersion(Cython.__version__) < CYTHON_MIN_VERSION:
                message += ' Your version of Cython was {0}.'.format(
                    Cython.__version__)
                raise ValueError(message)
            from Cython.Build import cythonize
        except ImportError as exc:
            exc.args += (message,)
            raise

        config.ext_modules = cythonize(config.ext_modules) 
Example #2
Source File: setup.py    From numcodecs with MIT License 6 votes vote down vote up
def compat_extension():
    info('setting up compat extension')

    extra_compile_args = list(base_compile_args)

    if have_cython:
        sources = ['numcodecs/compat_ext.pyx']
    else:
        sources = ['numcodecs/compat_ext.c']

    # define extension module
    extensions = [
        Extension('numcodecs.compat_ext',
                  sources=sources,
                  extra_compile_args=extra_compile_args),
    ]

    if have_cython:
        extensions = cythonize(extensions)

    return extensions 
Example #3
Source File: setup.py    From lightfm with Apache License 2.0 6 votes vote down vote up
def run(self):
        from Cython.Build import cythonize

        self.generate_pyx()

        cythonize(
            [
                Extension(
                    "lightfm._lightfm_fast_no_openmp",
                    ["lightfm/_lightfm_fast_no_openmp.pyx"],
                ),
                Extension(
                    "lightfm._lightfm_fast_openmp",
                    ["lightfm/_lightfm_fast_openmp.pyx"],
                    extra_link_args=["-fopenmp"],
                ),
            ]
        ) 
Example #4
Source File: setup.py    From scikit-multiflow with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def maybe_cythonize_extensions(top_path, config):
    """Tweaks for building extensions between release and development mode."""
    is_release = os.path.exists(os.path.join(top_path, 'PKG-INFO'))
    if is_release:
        build_from_c_and_cpp_files(config.ext_modules)
    else:
        message = ('Please install cython with a version >= {0} in order '
                   'to build a scikit-multiflow development version.').format(
                       CYTHON_MIN_VERSION)
        try:
            import Cython
            if LooseVersion(Cython.__version__) < CYTHON_MIN_VERSION:
                message += ' Your version of Cython was {0}.'.format(
                    Cython.__version__)
                raise ValueError(message)
            from Cython.Build import cythonize
        except ImportError as exc:
            exc.args += (message,)
            raise
        config.ext_modules = cythonize(config.ext_modules,
                                       compiler_directives={'language_level': 3}) 
Example #5
Source File: setup.py    From dimod with Apache License 2.0 6 votes vote down vote up
def run(self):
        # add numpy headers
        import numpy
        self.include_dirs.append(numpy.get_include())

        # add dimod headers
        include = os.path.join(os.path.dirname(__file__), 'dimod', 'include')
        self.include_dirs.append(include)

        if self.build_tests:

            test_extensions = [Extension('*', ['tests/test_*'+ext])]
            if USE_CYTHON:
                test_extensions = cythonize(test_extensions,
                                            # annotate=True
                                            )
            self.extensions.extend(test_extensions)

        super().run() 
Example #6
Source File: setup.py    From python-gssapi with ISC License 6 votes vote down vote up
def ext_modules(self):
        if SOURCE_EXT != 'pyx':
            return getattr(self, '_ext_modules', None)

        if getattr(self, '_ext_modules', None) is None:
            return None

        if getattr(self, '_last_run_command', None) in DONT_CYTHONIZE_FOR:
            return self._ext_modules

        if getattr(self, '_cythonized_ext_modules', None) is None:
            self._cythonized_ext_modules = cythonize(
                self._ext_modules,
                language_level=2,
            )

        return self._cythonized_ext_modules 
Example #7
Source File: setup.py    From modl with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def configuration(parent_package='', top_path=None):
    if os.path.exists('MANIFEST'):
        os.remove('MANIFEST')

    from numpy.distutils.misc_util import Configuration
    config = Configuration(None, parent_package, top_path)

    config.set_options(ignore_setup_xxx_py=True,
                       assume_default_configuration=True,
                       delegate_options_to_subpackages=True,
                       quiet=True)

    config.add_subpackage('modl')

    config.ext_modules = cythonize(config.ext_modules, nthreads=4)

    return config 
Example #8
Source File: setup.py    From pairtools with MIT License 6 votes vote down vote up
def get_ext_modules():
    ext = '.pyx' if HAVE_CYTHON else '.c'
    src_files = glob.glob(os.path.join(
        os.path.dirname(__file__),
        "pairtools", "*" + ext))

    ext_modules = []
    for src_file in src_files:
        name = "pairtools." + os.path.splitext(os.path.basename(src_file))[0]
        ext_modules.append(Extension(name, [src_file]))

    if HAVE_CYTHON:
        # .pyx to .c
        ext_modules = cythonize(ext_modules)  #, annotate=True

    return ext_modules 
Example #9
Source File: setup.py    From tierpsy-tracker with MIT License 6 votes vote down vote up
def _get_ext_modules():
  #build cython files
  # python3 setup.py build_ext --inplace
  path_parts = [MODULE_NAME, 'analysis', 'ske_create', 'segWormPython', 'cython_files']
  cython_path = os.path.join(*path_parts)
  cython_path_e = os.path.join(MODULE_NAME, 'analysis', 'stage_aligment')

  def _add_path(f_list):
  	return [os.path.join(cython_path, x) for x in f_list]

  def _get_mod_path(name):
      return '.'.join(path_parts + [name])

  ext_files = {
  	"circCurvature" : ["circCurvature.pyx", "c_circCurvature.c"],
  	"curvspace" : ["curvspace.pyx", "c_curvspace.c"]
  }

  include_dirs = [numpy.get_include()]
  ext_modules = cythonize(os.path.join(cython_path, "*_cython.pyx"))
  ext_modules += cythonize(os.path.join(cython_path_e, "*.pyx"))
  ext_modules += [Extension(_get_mod_path(name), 
                            sources=_add_path(files), 
                            include_dirs=include_dirs) 
  			   for name, files in ext_files.items()]
  return ext_modules 
Example #10
Source File: setup.py    From spfeas with MIT License 6 votes vote down vote up
def setup_package():

    metadata = dict(name=spfeas_name,
                    maintainer=maintainer,
                    maintainer_email=maintainer_email,
                    description=description,
                    license=license_file,
                    version=__version__,
                    long_description=long_description,
                    author=author_file,
                    packages=get_packages(),
                    package_data=get_package_data(),
                    ext_modules=cythonize(get_pyx_list()),
                    include_dirs=[np.get_include()],
                    cmdclass=dict(build_ext=build_ext),
                    zip_safe=False,
                    download_url=git_url,
                    install_requires=required_packages,
                    entry_points=get_console_dict())

    setup(**metadata) 
Example #11
Source File: __init__.py    From Splunking-Crime with GNU Affero General Public License v3.0 6 votes vote down vote up
def maybe_cythonize_extensions(top_path, config):
    """Tweaks for building extensions between release and development mode."""
    is_release = os.path.exists(os.path.join(top_path, 'PKG-INFO'))

    if is_release:
        build_from_c_and_cpp_files(config.ext_modules)
    else:
        message = ('Please install cython with a version >= {0} in order '
                   'to build a scikit-learn development version.').format(
                       CYTHON_MIN_VERSION)
        try:
            import Cython
            if LooseVersion(Cython.__version__) < CYTHON_MIN_VERSION:
                message += ' Your version of Cython was {0}.'.format(
                    Cython.__version__)
                raise ValueError(message)
            from Cython.Build import cythonize
        except ImportError as exc:
            exc.args += (message,)
            raise

        config.ext_modules = cythonize(config.ext_modules) 
Example #12
Source File: setup.py    From piqueserver with GNU General Public License v3.0 6 votes vote down vote up
def run(self):

        from Cython.Build import cythonize

        if USE_ASAN:
            from Cython.Compiler import Options
            # make asan/valgrind's memory leak results better
            Options.generate_cleanup_code = True

        compiler_directives = {'language_level': 3, 'embedsignature': True}
        if linetrace:
            compiler_directives['linetrace'] = True

        self.extensions = cythonize(self.extensions, compiler_directives=compiler_directives)

        _build_ext.run(self)

        run_setup(os.path.join(os.getcwd(), "setup.py"),
                  ['build_py'] + extra_args) 
Example #13
Source File: setup.py    From urh with GNU General Public License v3.0 6 votes vote down vote up
def get_extensions():
    filenames = [os.path.splitext(f)[0] for f in os.listdir("src/urh/cythonext") if f.endswith(".pyx")]
    extensions = [Extension("urh.cythonext." + f, ["src/urh/cythonext/" + f + ".pyx"],
                            extra_compile_args=[OPEN_MP_FLAG],
                            extra_link_args=[OPEN_MP_FLAG],
                            language="c++") for f in filenames]

    ExtensionHelper.USE_RELATIVE_PATHS = True
    device_extensions, device_extras = ExtensionHelper.get_device_extensions_and_extras()
    extensions += device_extensions

    if NO_NUMPY_WARNINGS_FLAG:
        for extension in extensions:
            extension.extra_compile_args.append(NO_NUMPY_WARNINGS_FLAG)

    extensions = cythonize(extensions, compiler_directives=COMPILER_DIRECTIVES, compile_time_env=device_extras)
    return extensions 
Example #14
Source File: setup.py    From cu2qu with Apache License 2.0 5 votes vote down vote up
def run(self):
        if with_cython and not has_cython:
            from distutils.errors import DistutilsSetupError

            raise DistutilsSetupError(
                "%s is required when creating sdist --with-cython"
                % required_cython
            )

        if has_cython:
            from Cython.Build import cythonize

            cythonize(
                self.distribution.ext_modules,
                force=True,  # always regenerate *.c sources
                quiet=not self.verbose,
                compiler_directives={
                    "language_level": 3,
                    "embedsignature": True
                },
            )

        _sdist.run(self)


# don't build extensions if user explicitly requested --without-cython 
Example #15
Source File: build.py    From streaming-form-data with MIT License 5 votes vote down vote up
def build(setup_kwargs):
    file_ext = 'pyx' if USE_CYTHON else 'c'

    extensions = [
        Extension(
            'streaming_form_data._parser',
            ['streaming_form_data/_parser.{}'.format(file_ext)],
        )
    ]

    if USE_CYTHON:
        extensions = cythonize(extensions)

    setup_kwargs.update({'ext_modules': extensions}) 
Example #16
Source File: build.py    From tinydecred with ISC License 5 votes vote down vote up
def build(setup_kwargs):
    try:
        from Cython.Build import cythonize

        setup_kwargs.update(
            dict(
                ext_modules=cythonize(["decred/crypto/secp256k1/field.py"]),
                cmdclass=dict(build_ext=BuildExt),
            )
        )
    except Exception:
        pass 
Example #17
Source File: setup.py    From CenterNet with Apache License 2.0 5 votes vote down vote up
def make_cython_ext(name, module, sources):
    extra_compile_args = None
    if platform.system() != 'Windows':
        extra_compile_args = {
            'cxx': ['-Wno-unused-function', '-Wno-write-strings']
        }

    extension = Extension(
        '{}.{}'.format(module, name),
        [os.path.join(*module.split('.'), p) for p in sources],
        include_dirs=[np.get_include()],
        language='c++',
        extra_compile_args=extra_compile_args)
    extension, = cythonize(extension)
    return extension 
Example #18
Source File: setup.py    From mining with MIT License 5 votes vote down vote up
def generate_cython():
    print("Cythonizing sources")
    p = subprocess.call([sys.executable,
                        os.path.join(DATA_DIR, 'scripts', 'cythonize.py'),
                        'mining'],
                        cwd=DATA_DIR)
    if p != 0:
        raise RuntimeError("Running cythonize failed!") 
Example #19
Source File: __init__.py    From sktime with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def maybe_cythonize_extensions(top_path, config):
    """Tweaks for building extensions between release and development mode."""
    with_openmp = check_openmp_support()

    is_release = os.path.exists(os.path.join(top_path, 'PKG-INFO'))

    if is_release:
        build_from_c_and_cpp_files(config.ext_modules)
    else:
        message = ('Please install Cython with a version >= {0} in order '
                   'to build a sktime development version.').format(
            CYTHON_MIN_VERSION)
        try:
            import Cython
            if LooseVersion(Cython.__version__) < CYTHON_MIN_VERSION:
                message += ' Your version of Cython was {0}.'.format(
                    Cython.__version__)
                raise ValueError(message)
            from Cython.Build import cythonize
        except ImportError as exc:
            exc.args += (message,)
            raise

        n_jobs = 1
        with contextlib.suppress(ImportError):
            import joblib
            if LooseVersion(joblib.__version__) > LooseVersion("0.13.0"):
                # earlier joblib versions don't account for CPU affinity
                # constraints, and may over-estimate the number of available
                # CPU particularly in CI (cf loky#114)
                n_jobs = joblib.effective_n_jobs()

        config.ext_modules = cythonize(
            config.ext_modules,
            nthreads=n_jobs,
            compile_time_env={'SKTIME_OPENMP_SUPPORTED': with_openmp},
            compiler_directives={'language_level': 3}
        ) 
Example #20
Source File: __init__.py    From skoot with MIT License 5 votes vote down vote up
def maybe_cythonize_extensions(top_path, config):
    """Tweaks for building extensions between release and development mode."""
    is_release = os.path.exists(os.path.join(top_path, 'PKG-INFO'))

    if is_release:
        print("Release detected--building from source files")
        build_from_c_f_and_cpp_files(config.ext_modules)
    else:
        print("Development build detected--building from .pyx & .pyf")
        message = ('Please install cython with a version >= {0} in order '
                   'to build a {1} development version.').format(
                       CYTHON_MIN_VERSION, DEFAULT_ROOT)
        try:
            import Cython
            loose_cython_ver = LooseVersion(Cython.__version__)  # type: str
            if loose_cython_ver < CYTHON_MIN_VERSION:
                message += ' Your version of Cython was {0}.'.format(
                    Cython.__version__)
                raise ValueError(message)
            from Cython.Build import cythonize
        except ImportError as exc:
            exc.args += (message,)
            raise

        # cythonize or fortranize...
        config.ext_modules = cythonize(config.ext_modules) 
Example #21
Source File: setup.py    From pyresample with GNU Lesser General Public License v3.0 5 votes vote down vote up
def cythonize(extensions, **_ignore):
            """Fake function to compile from C/C++ files instead of compiling .pyx files with cython."""
            for extension in extensions:
                sources = []
                for sfile in extension.sources:
                    path, ext = os.path.splitext(sfile)
                    if ext in ('.pyx', '.py'):
                        if extension.language == 'c++':
                            ext = '.cpp'
                        else:
                            ext = '.c'
                        sfile = path + ext
                    sources.append(sfile)
                extension.sources[:] = sources
            return extensions 
Example #22
Source File: setup.py    From hmmlearn with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def finalize_options(self):
        # The key point: here, Cython and numpy will have been installed by
        # pip.
        from Cython.Build import cythonize
        import numpy as np
        import numpy.distutils

        self.distribution.ext_modules[:] = cythonize("**/*.pyx")
        # Sadly, this part needs to be done manually.
        for ext in self.distribution.ext_modules:
            for k, v in np.distutils.misc_util.get_info("npymath").items():
                setattr(ext, k, v)
            ext.include_dirs = [np.get_include()]

        super().finalize_options() 
Example #23
Source File: setup.py    From numcodecs with MIT License 5 votes vote down vote up
def zstd_extension():
    info('setting up Zstandard extension')

    zstd_sources = []
    extra_compile_args = list(base_compile_args)
    include_dirs = []
    define_macros = []

    # setup sources - use zstd bundled in blosc
    zstd_sources += glob('c-blosc/internal-complibs/zstd*/common/*.c')
    zstd_sources += glob('c-blosc/internal-complibs/zstd*/compress/*.c')
    zstd_sources += glob('c-blosc/internal-complibs/zstd*/decompress/*.c')
    zstd_sources += glob('c-blosc/internal-complibs/zstd*/dictBuilder/*.c')
    include_dirs += [d for d in glob('c-blosc/internal-complibs/zstd*')
                     if os.path.isdir(d)]
    include_dirs += [d for d in glob('c-blosc/internal-complibs/zstd*/*')
                     if os.path.isdir(d)]
    # define_macros += [('CYTHON_TRACE', '1')]

    if have_cython:
        sources = ['numcodecs/zstd.pyx']
    else:
        sources = ['numcodecs/zstd.c']

    # define extension module
    extensions = [
        Extension('numcodecs.zstd',
                  sources=sources + zstd_sources,
                  include_dirs=include_dirs,
                  define_macros=define_macros,
                  extra_compile_args=extra_compile_args,
                  ),
    ]

    if have_cython:
        extensions = cythonize(extensions)

    return extensions 
Example #24
Source File: setup.py    From pydpc with GNU Lesser General Public License v3.0 5 votes vote down vote up
def extensions():
    from numpy import get_include
    from Cython.Build import cythonize
    ext_core = Extension(
        "pydpc.core",
        sources=["ext/core.pyx", "ext/_core.c"],
        include_dirs=[get_include()],
        extra_compile_args=["-O3", "-std=c99"])
    exts = [ext_core]
    return cythonize(exts) 
Example #25
Source File: setup.py    From numcodecs with MIT License 5 votes vote down vote up
def lz4_extension():
    info('setting up LZ4 extension')

    extra_compile_args = list(base_compile_args)
    define_macros = []

    # setup sources - use LZ4 bundled in blosc
    lz4_sources = glob('c-blosc/internal-complibs/lz4*/*.c')
    include_dirs = [d for d in glob('c-blosc/internal-complibs/lz4*') if os.path.isdir(d)]
    include_dirs += ['numcodecs']
    # define_macros += [('CYTHON_TRACE', '1')]

    if have_cython:
        sources = ['numcodecs/lz4.pyx']
    else:
        sources = ['numcodecs/lz4.c']

    # define extension module
    extensions = [
        Extension('numcodecs.lz4',
                  sources=sources + lz4_sources,
                  include_dirs=include_dirs,
                  define_macros=define_macros,
                  extra_compile_args=extra_compile_args,
                  ),
    ]

    if have_cython:
        extensions = cythonize(extensions)

    return extensions 
Example #26
Source File: setup.py    From numcodecs with MIT License 5 votes vote down vote up
def vlen_extension():
    info('setting up vlen extension')

    extra_compile_args = list(base_compile_args)
    define_macros = []

    # setup sources
    include_dirs = ['numcodecs']
    # define_macros += [('CYTHON_TRACE', '1')]

    if have_cython:
        sources = ['numcodecs/vlen.pyx']
    else:
        sources = ['numcodecs/vlen.c']

    # define extension module
    extensions = [
        Extension('numcodecs.vlen',
                  sources=sources,
                  include_dirs=include_dirs,
                  define_macros=define_macros,
                  extra_compile_args=extra_compile_args,
                  ),
    ]

    if have_cython:
        extensions = cythonize(extensions)

    return extensions 
Example #27
Source File: setup.py    From pyrocksdb with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def cythonize(extensions): return extensions 
Example #28
Source File: setup.py    From PyChemia with MIT License 5 votes vote down vote up
def run(self):
            # Make sure the compiled Cython files in the distribution are
            # up-to-date
            from Cython.Build import cythonize
            cythonize(ext_modules, annotate=True, compiler_directives={'embedsignature': True})
            _sdist.run(self) 
Example #29
Source File: setup_accel.py    From skl-groups with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def cython_ext(extension, **kw):
    assert len(extension.sources) == 1
    base, ext = os.path.splitext(extension.sources[0])
    # setuptools sometimes "nicely" turns .pyx into .c for us
    assert ext in {'.pyx', '.c'}
    pyx_path = base + '.pyx'
    c_path = base + '.c'

    try:
        from Cython.Build import cythonize
    except ImportError:
        try:
            pyx_time = os.path.getmtime(pyx_path)
            c_time = os.path.getmtime(c_path)
            if pyx_time > c_time:
                import datetime
                msg = "{pyx_name} file has mtime {pyx_t}, {c_name} has {c_t}"
                raise ValueError(msg.format(
                    pyx_name=os.path.basename(pyx_path),
                    c_name=os.path.basename(c_path),
                    pyx_t=datetime.datetime.fromtimestamp(pyx_time),
                    c_t=datetime.datetime.fromtimestamp(c_time),
                ))
        except (OSError, ValueError) as e:
            msg = "{} extension needs to be compiled, but cython not available:"
            raise ImportError(msg.format(extension.name) + '\n' + str(e))
        else:
            extension.sources[0] = c_path
            return extension
    else:
        return cythonize([extension])[0]


# TODO: don't do this for egg_info, etc 
Example #30
Source File: setup.py    From rpforest with Apache License 2.0 5 votes vote down vote up
def run(self):
        if not CYTHON_AVAILABLE:
            raise CythonNotInstalled
        if not NUMPY_AVAILABLE:
            raise NumpyNotInstalled
        extensions = define_extensions("pyx")
        # language_level sets it py2 compatible
        cythonize(extensions, compiler_directives={"language_level": "2"})