[1242] | 1 | # This file is a part of Framsticks SDK. http://www.framsticks.com/
|
---|
| 2 | # Copyright (C) 2023 Maciej Komosinski and Szymon Ulatowski.
|
---|
| 3 | # See LICENSE.txt for details.
|
---|
| 4 |
|
---|
| 5 | import matplotlib.pyplot as plt
|
---|
| 6 | from matplotlib.collections import LineCollection
|
---|
| 7 | import sys
|
---|
| 8 | import csv
|
---|
| 9 |
|
---|
| 10 | # Visualizing the influence of Framsticks f1/f4 modifier sequences. See also geneprops_test.cpp
|
---|
| 11 | # usage:
|
---|
| 12 | # ./geneprops_test f 3 -e | python3 frams/_demos/geneprops_graph.py
|
---|
| 13 | #
|
---|
| 14 | # See also https://colab.research.google.com/drive/1x9FTAUQKFhvxX_IyEcglWRWa4ylZeY-7?usp=sharing
|
---|
| 15 |
|
---|
| 16 | #################################################
|
---|
| 17 | # loading
|
---|
| 18 |
|
---|
| 19 | nodes_list = [] # ['modifiers', value]
|
---|
| 20 |
|
---|
| 21 | for line in csv.reader(sys.stdin.readlines(),delimiter='\t'):
|
---|
| 22 | if line[0]=='-':
|
---|
| 23 | line[0]=''
|
---|
| 24 | nodes_list.append([line[0],float(line[1])])
|
---|
| 25 |
|
---|
| 26 | nodes_dict = {} # ['modifiers', value, [children,...]]
|
---|
| 27 |
|
---|
| 28 | for n in sorted(nodes_list, key=lambda e: len(e[0])):
|
---|
| 29 | tree_node = [n[0],n[1],[]]
|
---|
| 30 | if n[0]!='':
|
---|
| 31 | nodes_dict[n[0][:-1]][2].append(tree_node)
|
---|
| 32 | nodes_dict[n[0]] = tree_node
|
---|
| 33 |
|
---|
| 34 | #################################################
|
---|
| 35 | # plotting
|
---|
| 36 |
|
---|
| 37 | fig, ax = plt.subplots(figsize=(4, 4))
|
---|
| 38 | ax.set(title="Tree of modifier values")
|
---|
| 39 |
|
---|
| 40 | segs=[]
|
---|
| 41 | for n in nodes_dict.values():
|
---|
| 42 | for child in n[2]:
|
---|
| 43 | segs.append( ((len(n[0]),n[1]),(len(n[0])+1,child[1])) )
|
---|
| 44 |
|
---|
[1302] | 45 | linecol = LineCollection(segs, linewidths=2, linestyle='solid', color='#ddddff')
|
---|
[1242] | 46 | ax.add_collection(linecol)
|
---|
| 47 |
|
---|
| 48 | node_x = [len(n[0]) for n in nodes_dict.values()]
|
---|
| 49 | node_y = [n[1] for n in nodes_dict.values()]
|
---|
| 50 |
|
---|
| 51 | ax.plot(node_x, node_y, "o", color="w", markerfacecolor="r")
|
---|
| 52 |
|
---|
| 53 | for n in nodes_dict.values():
|
---|
| 54 | ax.annotate(n[0], (len(n[0]),n[1]), textcoords="offset points", horizontalalignment="right", verticalalignment='center', xytext=(-4,0))
|
---|
| 55 |
|
---|
| 56 | ax.xaxis.set_visible(False)
|
---|
[1302] | 57 | plt.tight_layout()
|
---|
[1242] | 58 | plt.show()
|
---|