graphistry / pygraphistry

PyGraphistry is a Python library to quickly load, shape, embed, and explore big graphs with the GPU-accelerated Graphistry visual graph analyzer
BSD 3-Clause "New" or "Revised" License
2.16k stars 206 forks source link

[FEA] Smart dependency manager to replace lazy imports #468

Open tanmoyio opened 1 year ago

tanmoyio commented 1 year ago

Motivations:

This is how we are managing dependencies now

# current state of lazy import 
def lazy_umap_import_has_dependancy():
    try:
        import warnings

        warnings.filterwarnings("ignore")
        import umap  # noqa

        return True, "ok", umap
    except ModuleNotFoundError as e:
        return False, e, None

After dependency manager it will look like

deps = DepManager()
cuml, has_cuml = deps.cuml
umap, has_umap = deps.umap

Aim

working example

Screenshot 2023-04-18 at 9 50 45 PM
lmeyerov commented 1 year ago

Better code reuse here seems good, and enables memoization etc tricks

Also, I think somewhere in the code I realized a useful trick was, when just a check is needed, instead of trying via an import (slow), do a check for the module in the path, which skips the costs of actually importing (which runs all sorts of code in the dep's top-level)

dcolinmorgan commented 1 year ago

is it cheating to use another library to help?

import importlib.util, sys
def check_umap():
  umap_spec = importlib.util.find_spec("umap")
  has_umap = umap_spec is not None
  if has_umap and "umap" not in sys.modules:
     import umap
     return umap, has_umap
  else:
     return None, has_umap

i see the way we do it now is better for >3.6, but since we want to check and not import every time perhaps if > try?

lmeyerov commented 1 year ago

Hmm, these calls do different things;

So yeah, maybe: