Python pyximport.install() Examples

The following are 8 code examples of pyximport.install(). 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 pyximport , or try the search function .
Example #1
Source File: ppmigen.py    From socialsent with Apache License 2.0 6 votes vote down vote up
def run(count_path, out_path, smooth=0, cds=True, normalize=False, neg=1):
    counts = create_representation("Explicit", count_path, normalize=False)
    old_mat = counts.m
    index = counts.wi
    smooth = old_mat.sum() * smooth

    # getting marginal probs
    row_probs = old_mat.sum(1) + smooth
    col_probs = old_mat.sum(0) + smooth
    if cds:
        col_probs = np.power(col_probs, 0.75)
    row_probs = row_probs / row_probs.sum()
    col_probs = col_probs / col_probs.sum()

    # building PPMI matrix
    ppmi_mat = make_ppmi_mat(old_mat, row_probs, col_probs, smooth, neg=neg, normalize=normalize)
    import pyximport
    pyximport.install(setup_args={"include_dirs": np.get_include()})
    from representations import sparse_io
    sparse_io.export_mat_eff(ppmi_mat.row, ppmi_mat.col, ppmi_mat.data, out_path + ".bin")
    util.write_pickle(index, out_path + "-index.pkl") 
Example #2
Source File: 比较速度.py    From Python_Master_Courses with GNU General Public License v3.0 5 votes vote down vote up
def main():
    import time
    # 启动pyx编译器
    import pyximport
    pyximport.install()
    # Cython的素数算法实现
    import primesCy
    # Python的素数算法实现
    import primes

    print("Cython:")

    t1 = time.time()
    print(primesCy.primes(500))

    t2 = time.time()
    print("Cython time: %s" % (t2 - t1))

    print("")

    print("Python")

    t1 = time.time()
    print(primes.primes(500))

    t2 = time.time()
    print("Python time: %s" % (t2 - t1)) 
Example #3
Source File: umr.py    From spyder-kernels with MIT License 5 votes vote down vote up
def activate_cython(self):
        """
        Activate Cython support.

        We need to run this here because if the support is
        active, we don't to run the UMR at all.
        """
        run_cython = os.environ.get("SPY_RUN_CYTHON") == "True"

        if run_cython:
            try:
                __import__('Cython')
                self.has_cython = True
            except Exception:
                pass

            if self.has_cython:
                # Import pyximport to enable Cython files support for
                # import statement
                import pyximport
                pyx_setup_args = {}

                # Add Numpy include dir to pyximport/distutils
                try:
                    import numpy
                    pyx_setup_args['include_dirs'] = numpy.get_include()
                except Exception:
                    pass

                # Setup pyximport and enable Cython files reload
                pyximport.install(setup_args=pyx_setup_args,
                                  reload_support=True) 
Example #4
Source File: cythonmagic.py    From Computable with MIT License 5 votes vote down vote up
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) 
Example #5
Source File: matrix_serializer.py    From socialsent with Apache License 2.0 5 votes vote down vote up
def load_matrix(f):
    if not f.endswith('.bin'):
        f += ".bin"
    import pyximport
    pyximport.install(setup_args={"include_dirs": np.get_include()})
    from socialsent.representations import sparse_io
    return sparse_io.retrieve_mat_as_coo(f).tocsr() 
Example #6
Source File: cooccurgen.py    From socialsent with Apache License 2.0 5 votes vote down vote up
def run(word_gen, index, window_size, out_file):
    context = []
    pair_counts = Counter()
    for word in word_gen:
        context.append(index[word])
        if len(context) > window_size * 2 + 1:
            context.pop(0)
        pair_counts = _process_context(context, pair_counts, window_size)
    import pyximport
    pyximport.install(setup_args={"include_dirs": np.get_include()})
    from representations import sparse_io
    sparse_io.export_mat_from_dict(pair_counts, out_file) 
Example #7
Source File: Util.py    From PReMVOS with MIT License 5 votes vote down vote up
def geo_dist(img, pts):
  # Import these only on demand since pyximport interferes with pycocotools
  import pyximport
  pyximport.install()
  from ReID_net.datasets.Util import sweep

  img = np.copy(img) / 255.0
  #G = nd.gaussian_gradient_magnitude(img, 1.0)
  img = cv2.GaussianBlur(img, (3,3), 1.0)
  #G = cv2.Laplacian(img,cv2.CV_64F)
  sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)
  sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)
  sobel_abs = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
  sobel_abs = (sobel_abs[:, :, 0] ** 2 + sobel_abs[:, :, 1] ** 2 + sobel_abs[:, :, 2] ** 2) ** (1 / 2.0)

  #G = (G[:, :, 0] ** 2 + G[:, :, 1] ** 2 + G[:, :, 2] ** 2) ** (1 / 2.0)
  # c = 1 + G * 200
  # c = G / np.max(G)
  #c=sobel_abs / 255.0
  c=1+sobel_abs
  # plt.imshow(sobel_abs)
  # plt.colorbar()
  # plt.show()

  dt = np.zeros_like(c)
  dt[:] = 1000
  dt[pts] = 0
  sweeps = [dt, dt[:, ::-1], dt[::-1], dt[::-1, ::-1]]
  costs = [c, c[:, ::-1], c[::-1], c[::-1, ::-1]]
  for i, (a, c) in enumerate(it.cycle(list(zip(sweeps, costs)))):
    # print i,
    if sweep.sweep(a, c) < 1.0 or i >= 40:
      break
  return dt 
Example #8
Source File: detection.py    From yolact with MIT License 4 votes vote down vote up
def traditional_nms(self, boxes, masks, scores, iou_threshold=0.5, conf_thresh=0.05):
        import pyximport
        pyximport.install(setup_args={"include_dirs":np.get_include()}, reload_support=True)

        from utils.cython_nms import nms as cnms

        num_classes = scores.size(0)

        idx_lst = []
        cls_lst = []
        scr_lst = []

        # Multiplying by max_size is necessary because of how cnms computes its area and intersections
        boxes = boxes * cfg.max_size

        for _cls in range(num_classes):
            cls_scores = scores[_cls, :]
            conf_mask = cls_scores > conf_thresh
            idx = torch.arange(cls_scores.size(0), device=boxes.device)

            cls_scores = cls_scores[conf_mask]
            idx = idx[conf_mask]

            if cls_scores.size(0) == 0:
                continue
            
            preds = torch.cat([boxes[conf_mask], cls_scores[:, None]], dim=1).cpu().numpy()
            keep = cnms(preds, iou_threshold)
            keep = torch.Tensor(keep, device=boxes.device).long()

            idx_lst.append(idx[keep])
            cls_lst.append(keep * 0 + _cls)
            scr_lst.append(cls_scores[keep])
        
        idx     = torch.cat(idx_lst, dim=0)
        classes = torch.cat(cls_lst, dim=0)
        scores  = torch.cat(scr_lst, dim=0)

        scores, idx2 = scores.sort(0, descending=True)
        idx2 = idx2[:cfg.max_num_detections]
        scores = scores[:cfg.max_num_detections]

        idx = idx[idx2]
        classes = classes[idx2]

        # Undo the multiplication above
        return boxes[idx] / cfg.max_size, masks[idx], classes, scores