-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRead_Mojo.py
63 lines (50 loc) · 2.74 KB
/
Read_Mojo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# In another terminal window, download and extract the
# latest stable h2o.jar from http://www.h2o.ai/download/
# and
# https://graphviz.org/download/ (download it and add to path)
"""
cd h2o-3.44.0.2
"""
# Run the PrintMojo tool from the command line.
# This requires that graphviz is installed.
tree = 100
path_mojo = r"D:\0 - UnB\PG_Freeze Casting\Bessa - FreezeCasting\mojos\Grid_GBM_Train_model_python_1701130971119_130_model_1.zip" # gbm
path_mojo_2 = r"D:\0 - UnB\PG_Freeze Casting\Bessa - FreezeCasting\mojos\Grid_DRF_Train_model_python_1701175593760_1_model_1.zip" # drf
f"""
java -cp h2o.jar hex.genmodel.tools.PrintMojo --tree 400 -i "D:\0 - UnB\PG_Freeze Casting\Bessa - FreezeCasting\mojos\Grid_DRF_Train_model_python_1701175593760_1_model_1.zip" -o model.gv -f 20 -d 3
dot -Tpng model.gv -o model.png
"""
# The image will be saved at h2o-3.36.1.2 folder as model.png
"""Code that doesnt work for me below"""
import h2o
import subprocess
from IPython.display import Image
mojo_file_name = "mojos/Grid_DRF_Train_model_python_1663115455582_1_model_1.zip"
h2o_jar_path = '/h2o-3.36.1.2/h2o.jar'
mojo_full_path = mojo_file_name
gv_file_path = "h2o-3.36.1.2/model.gv"
image_file_name = "mojos/drf_tree"
def generateTree(h2o_jar_path, mojo_full_path, gv_file_path, image_file_path, tree_id = 0):
"""Function to generate graphviz tree from the saved mojo"""
image_file_path = image_file_path + "_" + str(tree_id) + ".png"
result = subprocess.call(["java", "-cp", h2o_jar_path, "hex.genmodel.tools.PrintMojo", "--tree", str(tree_id), "-i", mojo_full_path , "-o", gv_file_path ], shell=False)
result = subprocess.call(["ls",gv_file_path], shell = False)
if result == 0:
print("Success: Graphviz file " + gv_file_path + " is generated.")
else:
print("Error: Graphviz file " + gv_file_path + " could not be generated.")
def generateTreeImage(gv_file_path, image_file_path, tree_id):
"""Function to generate png from graphviz tree"""
image_file_path = image_file_path + "_" + str(tree_id) + ".png"
result = subprocess.call(["dot", "-Tpng", gv_file_path, "-o", image_file_path], shell=False)
result = subprocess.call(["ls", image_file_path], shell = False)
if result == 0:
print("Success: Image File " + image_file_path + " is generated.")
print("Now you can execute the follow line as-it-is to see the tree graph:")
print("Image(filename='" + image_file_path + "\')")
else:
print("Error: Image file " + image_file_path + " could not be generated.")
# Just change the tree id in the function below to get which particular tree you want
tree_id = 100
generateTree(h2o_jar_path, mojo_full_path, gv_file_path, image_file_name, tree_id)
generateTreeImage(gv_file_path, image_file_name, tree_id)