+/IBCS
git clone https://git.julia.blue/+/IBCS
c6bf06e
master
1''' 2Created on Oct 14, 2010 3 4@author: Peter Harrington 5''' 6import matplotlib .pyplot as plt 7 8decisionNode = dict (boxstyle = "sawtooth" ,fc = "0.8" ) 9leafNode = dict (boxstyle = "round4" ,fc = "0.8" ) 10arrow_args = dict (arrowstyle = "<-" ) 11 12 13def getNumLeafs (myTree ): 14numLeafs = 0 15firstStr = myTree .keys ()[0 ] 16secondDict = myTree [firstStr ] 17for key in secondDict .keys (): 18if type ( 19secondDict [key ] 20 ).__name__ == 'dict' :#test to see if the nodes are dictonaires, if not they are leaf nodes 21numLeafs += getNumLeafs (secondDict [key ]) 22else : 23numLeafs += 1 24return numLeafs 25 26 27def getTreeDepth (myTree ): 28maxDepth = 0 29firstStr = myTree .keys ()[0 ] 30secondDict = myTree [firstStr ] 31for key in secondDict .keys (): 32if type ( 33secondDict [key ] 34 ).__name__ == 'dict' :#test to see if the nodes are dictonaires, if not they are leaf nodes 35thisDepth = 1 + getTreeDepth (secondDict [key ]) 36else : 37thisDepth = 1 38if thisDepth > maxDepth : 39maxDepth = thisDepth 40return maxDepth 41 42 43def plotNode (nodeTxt ,centerPt ,parentPt ,nodeType ): 44createPlot .ax1 .annotate ( 45nodeTxt , 46xy = parentPt , 47xycoords = 'axes fraction' , 48xytext = centerPt , 49textcoords = 'axes fraction' , 50va = "center" , 51ha = "center" , 52bbox = nodeType , 53arrowprops = arrow_args ) 54 55 56def plotMidText (cntrPt ,parentPt ,txtString ): 57xMid = (parentPt [0 ]- cntrPt [0 ])/ 2.0 + cntrPt [0 ] 58yMid = (parentPt [1 ]- cntrPt [1 ])/ 2.0 + cntrPt [1 ] 59createPlot .ax1 .text ( 60xMid ,yMid ,txtString ,va = "center" ,ha = "center" ,rotation = 30 ) 61 62 63def plotTree (myTree ,parentPt , 64nodeTxt ):#if the first key tells you what feat was split on 65numLeafs = getNumLeafs (myTree )#this determines the x width of this tree 66# depth = getTreeDepth(myTree) 67firstStr = myTree .keys ()[0 ]#the text label for this node should be this 68cntrPt = (plotTree .xOff + (1.0 + float (numLeafs ))/ 2.0 / plotTree .totalW , 69plotTree .yOff ) 70plotMidText (cntrPt ,parentPt ,nodeTxt ) 71plotNode (firstStr ,cntrPt ,parentPt ,decisionNode ) 72secondDict = myTree [firstStr ] 73plotTree .yOff = plotTree .yOff - 1.0 / plotTree .totalD 74for key in secondDict .keys (): 75if type ( 76secondDict [key ] 77 ).__name__ == 'dict' :#test to see if the nodes are dictonaires, if not they are leaf nodes 78plotTree (secondDict [key ],cntrPt ,str (key ))#recursion 79else :#it's a leaf node print the leaf node 80plotTree .xOff = plotTree .xOff + 1.0 / plotTree .totalW 81plotNode (secondDict [key ], (plotTree .xOff ,plotTree .yOff ),cntrPt , 82leafNode ) 83plotMidText ((plotTree .xOff ,plotTree .yOff ),cntrPt ,str (key )) 84plotTree .yOff = plotTree .yOff + 1.0 / plotTree .totalD 85 86 87# if you do get a dictonary you know it's a tree, and the first element will be another dict 88 89 90def createPlot (inTree ): 91fig = plt .figure (1 ,facecolor = 'white' ) 92fig .clf () 93axprops = dict (xticks = [],yticks = []) 94createPlot .ax1 = plt .subplot (111 ,frameon = False ,** axprops )#no ticks 95# createPlot.ax1 = plt.subplot(111, frameon=False) #ticks for demo puropses 96plotTree .totalW = float (getNumLeafs (inTree )) 97plotTree .totalD = float (getTreeDepth (inTree )) 98plotTree .xOff = - 0.5 / plotTree .totalW 99plotTree .yOff = 1.0 100plotTree (inTree , (0.5 ,1.0 ),'' ) 101plt .show () 102 103 104# def createPlot(): 105# fig = plt.figure(1, facecolor='white') 106# fig.clf() 107# createPlot.ax1 = plt.subplot(111, frameon=False) #ticks for demo puropses 108# plotNode('a decision node', (0.5, 0.1), (0.1, 0.5), decisionNode) 109# plotNode('a leaf node', (0.8, 0.1), (0.3, 0.8), leafNode) 110# plt.show() 111 112 113def retrieveTree (i ): 114listOfTrees = [{ 115'no surfacing' : { 1160 :'no' , 1171 : { 118'flippers' : { 1190 :'no' , 1201 :'yes' 121 } 122 } 123 } 124 }, { 125'no surfacing' : { 1260 :'no' , 1271 : { 128'flippers' : { 1290 : { 130'head' : { 1310 :'no' , 1321 :'yes' 133 } 134 }, 1351 :'no' 136 } 137 } 138 } 139 }] 140return listOfTrees [i ] 141 142 143# createPlot(thisTree)