Search code examples
pythonregexgraphviz

What is re.sub in graphviz.Source()


I am trying to use this function called draw_tree.

def draw_tree(t, df, size=10, ratio=0.6, precision=0):
    """ Draws a representation of a random forest in IPython.
    Parameters:
    -----------
    t: The tree you wish to draw
    df: The data used to train the tree. This is used to get the names of the features.
    """
    s=export_graphviz(t, out_file=None, feature_names=df.columns, filled=True,
                      special_characters=True, rotate=True, precision=precision)
    IPython.display.display(graphviz.Source(re.sub('Tree {',
            f'Tree {{ size={size}; ratio={ratio}', s)))

However when I use it in my program, I get an error:

NameError: name 're' is not defined

Here is the call to the function

draw_tree(m.estimators_[0],df,precision=3)

I am unsure what this re object is meant to be. I am aware that the graphviz library has changed quite a bit between versions, so perhaps I am not using the correct version or prehaps I'm missing a required module?


Solution

  • Adding import re should solve the issue.

    This is a module where regular expressions can be applied, see: https://docs.python.org/3/library/re.html

    re.sub will change the string argument of graphviz.Source according to s in your case.

    You're calling graphviz.Source via IPython.display.display method as an argument. So re is indeed a top-level module you have to import first to get this working. This has nothing to do with graphviz library, because you're first make some substitutions to the string which will be an argument for the graphviz library. And the substitution will be evaluated first, before calling any graphviz sources or Ipython sources.

    The solution is then to add import re before calling graphviz.Source with the re.sub(…) method inside. (Importing re in the same function or in the module, doesn't matter.)

    It should also be mentioned that your code snippet where the replacement is defined, seems not to work. After resolving your issue with the re library, you have to bugfix your replacement regex. I've already linked the method re.sub for you.