# Path: AdditiveModelsTreesRelatedModels/RegressionTree.py
# class decisionnode:
#     def __init__(self, col=-1, value=None, result=None, left_child=None, right_child=None, sets=None):
#     def equal(self, other):
# def ave(a):
# def divideset(X, Y, col, val):
# def quadratic(X, Y, col, val):
# def buildtree(X, Y, depth=0, max_depth=99999999):
# def predict(obs, tree):
# def cost_complexity(tree_node, total_q=0., terminal_num=0):
# def do_prune(tree, t):
# def possible_pruning(tree, node, tree_list):
# def pruning(tree):
# def selection(tree, alpha=0.1):
# def cookdata(inputs, outputs, Ttype):
#     X1 = []
#     X2 = []
#     Y1 = []
#     Y2 = []
#     X1, X2, Y1, Y2 = divideset(X, Y, col, val)
# 
# Path: LinearRegression/prostate.py
# def loaddata():
# def cookdata(data, output, Ttype):
# def cookdata2(data, output, Ttype):
# def calculate_correlation(data):
# def standardize(data):
# def checkdata(train_data):
# def savebinary(X, Y):
# def loadbinary(X, Y):
#         EX = np.sum(column)/len(column)
#         EX2 = np.sum(column*column)/len(column)
#         V = np.sqrt(EX2-EX*EX)
#         EX = np.sum(column)/len(column)
#         EX2 = np.sum(column*column)/len(column)


import numpy as np
from AdditiveModelsTreesRelatedModels import RegressionTree
from LinearRegression import prostate
from PIL import Image, ImageDraw
    w = getwidth(tree)*100
    h = getdepth(tree)*100 + 120
    
    img = Image.new('RGB', (w, h), (255, 255, 255))
    draw = ImageDraw.Draw(img)
    
    drawnode(draw, tree, w/2, 20)
    img.save(jpeg, 'JPEG')

def drawnode(draw, tree, x, y):
    if tree.result == None:
        w1 = getwidth(tree.left_child)*100
        w2 = getwidth(tree.right_child)*100
        
        left = x-(w1+w2)/2
        right = x+(w1+w2)/2
        
        draw.text((x-20,y-10), str(tree.col)+':'+str(tree.value), (0, 0, 0))
        
        draw.line((x, y, left+w1/2, y+100), fill=(255, 0, 0))
        draw.line((x, y, right-w2/2, y+100), fill=(255, 0, 0))
        
        drawnode(draw, tree.left_child, left+w1/2, y+100)
        drawnode(draw, tree.right_child, right-w2/2, y+100)
    else:
        txt = '%d' % tree.result
        draw.text((x-20, y), txt, (0, 0, 0))

if __name__ == '__main__':
    inputs, output, Ttype = prostate.loaddata()

# Path: AdditiveModelsTreesRelatedModels/RegressionTree.py
# class decisionnode:
#     def __init__(self, col=-1, value=None, result=None, left_child=None, right_child=None, sets=None):
#     def equal(self, other):
# def ave(a):
# def divideset(X, Y, col, val):
# def quadratic(X, Y, col, val):
# def buildtree(X, Y, depth=0, max_depth=99999999):
# def predict(obs, tree):
# def cost_complexity(tree_node, total_q=0., terminal_num=0):
# def do_prune(tree, t):
# def possible_pruning(tree, node, tree_list):
# def pruning(tree):
# def selection(tree, alpha=0.1):
# def cookdata(inputs, outputs, Ttype):
#     X1 = []
#     X2 = []
#     Y1 = []
#     Y2 = []
#     X1, X2, Y1, Y2 = divideset(X, Y, col, val)
# 
# Path: LinearRegression/prostate.py
# def loaddata():
# def cookdata(data, output, Ttype):
# def cookdata2(data, output, Ttype):
# def calculate_correlation(data):
# def standardize(data):
# def checkdata(train_data):
# def savebinary(X, Y):
# def loadbinary(X, Y):
#         EX = np.sum(column)/len(column)
#         EX2 = np.sum(column*column)/len(column)
#         V = np.sqrt(EX2-EX*EX)
#         EX = np.sum(column)/len(column)
#         EX2 = np.sum(column*column)/len(column)


# Path: Boosting/GradientBoostingTree.py
import numpy as np
from AdditiveModelsTreesRelatedModels import RegressionTree
from LinearRegression import prostate
from PIL import Image, ImageDraw

    w = getwidth(tree)*100
    h = getdepth(tree)*100 + 120
    
    img = Image.new('RGB', (w, h), (255, 255, 255))
    draw = ImageDraw.Draw(img)
    
    drawnode(draw, tree, w/2, 20)
    img.save(jpeg, 'JPEG')

def drawnode(draw, tree, x, y):
    if tree.result == None:
        w1 = getwidth(tree.left_child)*100
        w2 = getwidth(tree.right_child)*100
        
        left = x-(w1+w2)/2
        right = x+(w1+w2)/2
        
        draw.text((x-20,y-10), str(tree.col)+':'+str(tree.value), (0, 0, 0))
        
        draw.line((x, y, left+w1/2, y+100), fill=(255, 0, 0))
        draw.line((x, y, right-w2/2, y+100), fill=(255, 0, 0))
        
        drawnode(draw, tree.left_child, left+w1/2, y+100)
        drawnode(draw, tree.right_child, right-w2/2, y+100)
    else:
        txt = '%d' % tree.result
        draw.text((x-20, y), txt, (0, 0, 0))

if __name__ == '__main__':
    inputs, output, Ttype = prostate.loaddata()

    train_data, train_out, test_data, test_out = RegressionTree.cookdata(inputs, output, Ttype)