Mini-Projekt 2020-03-25: Begriffsbaum mithilfe von SPARQL & Python erstellen - Teil 3

Begriffsbaum mithilfe von SPARQL & Python erstellen - Teil 3


Hui! Fast schon fertig!
(Nur ein, zwei Kleinigkeiten sind noch suboptimal - finde die Fehler!)

Code

from ete3 import Tree
import pandas as pd

#Warmspielereien
unrooted_tree = Tree( "(A,B,(C,D));" )
print(unrooted_tree)

rooted_tree = Tree( "((A,B),(C,D));" )
print(rooted_tree)

rooted_tree = Tree( "(Informatik,((Bastelstube,Bastelhuber),(Wastel,(Wastelhuber,Splock))));" )
print(rooted_tree)

rooted_tree = Tree( "(Bastelstube,Bastelhuber,(Wastelhuber,Splock)Wastl)Informatik;", format=1 )
print(rooted_tree.get_ascii(show_internal=True))

#Lesen des als pickle gespeicherten DFs
unpickled_df = pd.read_pickle("/home/zarko/Dokumente/KnowledgeGraph/subclasses_df.pickle")

print(unpickled_df)

#Programm
from ete3 import Tree, TreeStyle, TextFace, add_face_to_node
from collections import defaultdict

def build_tree(nodes):
    root = None
    for i in nodes:
        if i[0] == 'Wurzel':
            root = i
    if not root:
        raise ValueError('no root!')
    data = defaultdict(list)
    for i in nodes:
        if i[0] != 'Wurzel':
            data[i[1]] = []
            data[i[0]].append(i[1])
    return data, root[1]

def dict_tree_to_str(tree, root):
    if not tree[root]:
        return f'{root}'
    subtrees = [dict_tree_to_str(tree, st) for st in tree[root]]
    return f'({",".join(subtrees)}){root}'

#Baum-Liste bilden
tree_csv = [['Wurzel','Informatik']]
for row in unpickled_df.itertuples(index=True, name='Pandas'):
    print(row[4],row[2])
    tree_csv.append([row[4],row[2]])
print(tree_csv)

#tree_csv = [[-1, 1], [1, 2], [1, 3], [2, 4], [3, 5], [3, 6], [5, 7]]
tree_dict, root = build_tree(tree_csv)
t = Tree(dict_tree_to_str(tree_dict, root)+';',  format=1)

ts = TreeStyle()
ts.show_leaf_name = False
def my_layout(node): # https://github.com/etetoolkit/ete/issues/219
    F = TextFace(node.name, tight_text=True)
    add_face_to_node(F, node, column=0, position="branch-right")
ts.layout_fn = my_layout

t.render("/home/zarko/Dokumente/KnowledgeGraph/mytree.png", w=183, units="mm", tree_style=ts)


Output (Nur erzeugtes Bild)


Links

pandas.read_pickle — pandas 1.0.3 documentation Tree Structure in python using CSV data - Stack Overflow ete3.Tree Python Example Working With Tree Data Structures — ETE Toolkit - analysis and visualization of trees ETE Toolkit - Download ETE Toolkit - Analysis and Visualization of (phylogenetic) trees Tree plotting in Python - Stack Overflow GitHub - phrb/3d-spanning-tree: Python script for generating 3d spanning trees. [Personal project] I made 3D Trees generator in Python (youtube demo) : Python pandas.DataFrame.to_pickle — pandas 1.0.3 documentation


Vorfazit

Das kommt der Sache schon sehr nahe, die ich mit dem Herein-Hinaus-Zoomen meinen wollte (in diesem Blog-Post: https://slaughterlaughter.blogspot.com/2020/03/kompetenz.html)
Diesbezüglich - aber auch darüberhinausbezüglich - habe ich noch etwas vor. Ett kütt. Bin aber grade etwas ruhebedürftig, so wie sich das bei mir anfühlt. Corona + Coden, das strengt schon etwas an, auch ohne große Kraftanstrengung. Öh, ... äh... iwi so. lalalo.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Kleiner Nachschub

...

ts = TreeStyle()
ts.show_leaf_name = True
ts.mode = "c"
ts.arc_start = -360 # 0 degrees = 3 o'clock
ts.arc_span = 360
t.show(tree_style=ts)
 
 
 
http://etetoolkit.org/docs/latest/tutorial/tutorial_drawing.html
 
 

Kommentare

  1. Data science Training in Hyderabad. Data science is an inter-disciplinary field that uses scientific methods, processes, algorithms and systems to extract knowledge and insights from many structural and unstructured data. Data science is related to data mining, machine learning and big data.
    Data Science Training In Hyderabad

    Data Science Course In Hyderabad

    AntwortenLöschen

Kommentar veröffentlichen

Beliebte Posts aus diesem Blog

·

Es brennt.

Bye, bye Nord Stream 2!