Skip to content
Snippets Groups Projects
Commit 1da4b5be authored by Caroline DE POURTALES's avatar Caroline DE POURTALES
Browse files

cant convert because of the dump function - to see next meeting

parent 24196341
No related branches found
No related tags found
1 merge request!3Decision tree done
...@@ -25,9 +25,7 @@ class DecisionTreeComponent(): ...@@ -25,9 +25,7 @@ class DecisionTreeComponent():
self.uploaded_dt = UploadedDecisionTree(tree, 'SKL', filename_tree, maxdepth=tree.get_depth(), feature_names=feature_names, nb_classes=tree.n_classes_) self.uploaded_dt = UploadedDecisionTree(tree, 'SKL', filename_tree, maxdepth=tree.get_depth(), feature_names=feature_names, nb_classes=tree.n_classes_)
#need a function that takes as input UploadedDecisionTree and gives DecisionTree #need a function that takes as input UploadedDecisionTree and gives DecisionTree
#self.dt = DecisionTree(from_dt=) self.dt = DecisionTree(from_dt=self.uploaded_dt.dump())
dt = open("pages/application/DecisionTree/meteo.dt", "r").read()
self.dt = DecisionTree(from_dt=dt)
dot_source = visualize(self.dt) dot_source = visualize(self.dt)
self.network = [dbc.Row(dash_interactive_graphviz.DashInteractiveGraphviz(dot_source=dot_source, style = {"width": "60%", self.network = [dbc.Row(dash_interactive_graphviz.DashInteractiveGraphviz(dot_source=dot_source, style = {"width": "60%",
......
...@@ -265,7 +265,6 @@ class DecisionTree(): ...@@ -265,7 +265,6 @@ class DecisionTree():
# computing the sets to hit # computing the sets to hit
to_hit = self.prepare_sets(inst, term) to_hit = self.prepare_sets(inst, term)
for type in xtype : for type in xtype :
if type == "AXp": if type == "AXp":
explanation_dic.update(self.enumerate_abductive(to_hit, enum, solver, htype, term)) explanation_dic.update(self.enumerate_abductive(to_hit, enum, solver, htype, term))
......
...@@ -221,86 +221,34 @@ class UploadedDecisionTree: ...@@ -221,86 +221,34 @@ class UploadedDecisionTree:
domains = {f:[j for j in fvmap[f] if((fvmap[f][j][1]))] for f in fvmap} domains = {f:[j for j in fvmap[f] if((fvmap[f][j][1]))] for f in fvmap}
internal, terminal = walk_tree(self.tree, domains, [], []) internal, terminal = walk_tree(self.tree, domains, [], [])
#.dt
if filename and maxdepth: dt=""
fname = os.path.splitext(os.path.basename(filename))[0] dt += f"{self.n_nodes}\n{self.tree.id}\n"
dir_name = os.path.join(output, 'tree', fname) dt += f"I {' '.join(dict.fromkeys([str(i) for i,_,_,_ in internal]))}\n"
dir_name = os.path.join(dir_name, f"{self.tool}{maxdepth}") dt += f"T {' '.join([str(i) for i,_ in terminal ])}\n"
for i,c in terminal:
if self.tool == 'ITI': dt += f"{i} T {c}\n"
dir_name = os.path.join(dir_name, self.tool) for i,f, j, n in internal:
elif filename: dt += f"{i} {f} {j} {n}\n"
fname = os.path.splitext(os.path.basename(filename))[0]
dir_name = os.path.join(output, f'tree/{fname}/{self.tool}')
else:
fname = "tree"
dir_name = os.path.join(output)
try:
os.stat(dir_name)
except:
os.makedirs(dir_name)
fname = os.path.join(dir_name, fname+'.dt') #.map
print("saving dtree to ", fname) map=""
map+="Categorical\n"
with open(fname, 'w') as fp: map+=f"{len(fvmap)}\n"
fp.write(f"{self.n_nodes}\n{self.tree.id}\n") for f in fvmap:
fp.write(f"I {' '.join(dict.fromkeys([str(i) for i,_,_,_ in internal]))}\n") for v in fvmap[f]:
fp.write(f"T {' '.join([str(i) for i,_ in terminal ])}\n") if (fvmap[f][v][1] == True):
for i,c in terminal: map+=f"{f} {v} ={fvmap[f][v][2]}\n"
fp.write(f"{i} T {c}\n") if (fvmap[f][v][1] == False) and self.tool == "ITI":
for i,f, j, n in internal: map+=f"{f} {v} !={fvmap[f][v][2]}\n"
fp.write(f"{i} {f} {j} {n}\n")
if filename and maxdepth:
fname = os.path.splitext(os.path.basename(filename))[0]
dir_name = os.path.join(output, 'map', fname)
if self.tool == "ITI":
dir_name = os.path.join(dir_name, self.tool)
else:
dir_name = os.path.join(dir_name, f'{self.tool}{maxdepth}')
elif filename:
fname = os.path.splitext(os.path.basename(filename))[0]
dir_name = os.path.join(output, f'map/{fname}/{self.tool}')
else:
fname = "tree"
dir_name = os.path.join(output)
try:
os.stat(dir_name)
except:
os.makedirs(dir_name)
fname = os.path.join(dir_name, fname+'.map')
print("saving dtree map to ", fname)
with open(fname, 'w') as fp:
fp.write("Categorical\n")
fp.write(f"{len(fvmap)}\n")
for f in fvmap:
for v in fvmap[f]:
if (fvmap[f][v][1] == True):
fp.write(f"{f} {v} ={fvmap[f][v][2]}\n")
if (fvmap[f][v][1] == False) and self.tool == "ITI":
fp.write(f"{f} {v} !={fvmap[f][v][2]}\n")
if feat_names is not None: for i,fid in enumerate(feat_names):
if filename: f=f'f{i}'
fname = os.path.splitext(os.path.basename(filename))[0] map+f'{fid}:{f},'+",".join([f'{fvmap[f][v][2]}:{v}' for v in fvmap[f] if(fvmap[f][v][1])])+'\n'
fname = os.path.join(dir_name, fname+'.txt') #
else: print(dt)
fname = os.path.join(dir_name, 'map.txt') print(map)
return dt, map
print("saving feature map to ", fname)
with open(fname, 'w') as fp:
for i,fid in enumerate(feat_names):
f=f'f{i}'
fp.write(f'{fid}:{f},'+",".join([f'{fvmap[f][v][2]}:{v}' for v in fvmap[f] if(fvmap[f][v][1])])+'\n')
#
print('Done')
# end dump fct
def build_tree(self, json_tree=None, feature_names=None): def build_tree(self, json_tree=None, feature_names=None):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment