def __init__()

in runtime/native/python/treelite_runtime/predictor.py [0:0]


  def __init__(self, libpath, nthread=None, verbose=False):
    if os.path.isdir(libpath):  # libpath is a directory
      # directory is given; locate shared library inside it
      basename = os.path.basename(libpath.rstrip('/\\'))
      lib_found = False
      for ext in ['.so', '.dll', '.dylib']:
        path = os.path.join(libpath, basename + ext)
        if os.path.exists(path):
          lib_found = True
          break
      if not lib_found:
        raise TreeliteError('Directory {} doesn\'t appear '.format(libpath)+\
                            'to have any dynamic shared library '+\
                            '(.so/.dll/.dylib).')
    else:      # libpath is actually the name of shared library file
      fileext = os.path.splitext(libpath)[1]
      if fileext == '.dll' or fileext == '.so' or fileext == '.dylib':
        path = libpath
      else:
        raise TreeliteError('Specified path {} has wrong '.format(libpath) + \
                            'file extension ({}); '.format(fileext) +\
                            'the share library must have one of the '+\
                            'following extensions: .so / .dll / .dylib')
    self.handle = ctypes.c_void_p()
    if not re.match(r'^[a-zA-Z]+://', path):
      path = os.path.abspath(path)
    _check_call(_LIB.TreelitePredictorLoad(
        c_str(path),
        ctypes.c_int(nthread if nthread is not None else -1),
        ctypes.byref(self.handle)))
    # save # of features
    num_feature = ctypes.c_size_t()
    _check_call(_LIB.TreelitePredictorQueryNumFeature(
        self.handle,
        ctypes.byref(num_feature)))
    self.num_feature_ = num_feature.value
    # save # of output groups
    num_output_group = ctypes.c_size_t()
    _check_call(_LIB.TreelitePredictorQueryNumOutputGroup(
        self.handle,
        ctypes.byref(num_output_group)))
    self.num_output_group_ = num_output_group.value

    if verbose:
      log_info(__file__, lineno(),
               'Dynamic shared library {} has been '.format(path)+\
               'successfully loaded into memory')