# Path: html_visual/vis_tree.py
# def prune_species(pth):
# def add_image(pth, genealogy):
#     def find_image(pth, name):
# def find_path(tree, target):
#     def not_a_child_in_tree(tree, node):
# def parse_tree_file(pth, candidate_list=None):
#     def build_tree(parent, adj_list, candidate_list=None):
#     def build_node(name):
#     def genealogy_by_topology(tree_pth):
# def genealogy_with_style(genealogy):
#     def process_node(node):
# def evolution_graph(pth, k=5):
#     def build_node(spc_id, reward, info=None):
#     def build_tree(parent):
# def evolution_add_image(pth, evolution):
#     def find_image(pth, name):
# def get_config():
#     def post_process(args):
# 
# Path: html_visual/vis_spc_tree.py
# def get_config():
#     def post_process(args):
# def generate_full_tree(pth):
# def parent_child_performance(pth):
# def filter_spc(evolution_graph, spc_id):


import os
import init_path
import glob
import json
import argparse
import subprocess
from shutil import copyfile
from html_visual import vis_tree
from html_visual import vis_spc_tree
    # first burst out all the images
    print('Try to burst %s images' % trn_sess_path) 

    command_template = 'python ../env/visualize_species.py -i %s -v 0'
    # process = subprocess.Popen(command_template % \
    #     (os.path.join(trn_sess_path, 'species_data')),
    #     shell=True, stdout=subprocess.PIPE
    # )
    # process.wait()
    # if process.returncode:
    #     print('%s: Cannot burst species images.' % trn_sess_path)
    #     return
    # else:
    #     print('%s: Success.' % trn_sess_path)

    # step 0: initialize the 'visual' directory for the training session
    visual_path = os.path.join(trn_sess_path, 'visual')
    if os.path.exists( visual_path ) == False:
        os.makedirs( visual_path ) 

    copyfile(GENEALOGY_HTML, os.path.join(visual_path, 'genealogy.html'))
    copyfile(GENEALOGY_HTML, os.path.join(visual_path, 'evolution.html'))
    copyfile(GENEALOGY_HTML, os.path.join(visual_path, 'expand_genealogy.html'))

    # step 1: generate the evolution json
    species_data_path = os.path.join(trn_sess_path, 'species_data')
    evolution = vis_tree.evolution_graph(species_data_path, k=args.topk)

    # step 2: generate the genealogy json
    gene_path = os.path.join(trn_sess_path, 'gene_tree.npy')

# Path: html_visual/vis_tree.py
# def prune_species(pth):
# def add_image(pth, genealogy):
#     def find_image(pth, name):
# def find_path(tree, target):
#     def not_a_child_in_tree(tree, node):
# def parse_tree_file(pth, candidate_list=None):
#     def build_tree(parent, adj_list, candidate_list=None):
#     def build_node(name):
#     def genealogy_by_topology(tree_pth):
# def genealogy_with_style(genealogy):
#     def process_node(node):
# def evolution_graph(pth, k=5):
#     def build_node(spc_id, reward, info=None):
#     def build_tree(parent):
# def evolution_add_image(pth, evolution):
#     def find_image(pth, name):
# def get_config():
#     def post_process(args):
# 
# Path: html_visual/vis_spc_tree.py
# def get_config():
#     def post_process(args):
# def generate_full_tree(pth):
# def parent_child_performance(pth):
# def filter_spc(evolution_graph, spc_id):


# Path: html_visual/html_visual.py
import os
import init_path
import glob
import json
import argparse
import subprocess
from shutil import copyfile
from html_visual import vis_tree
from html_visual import vis_spc_tree

    # first burst out all the images
    print('Try to burst %s images' % trn_sess_path) 

    command_template = 'python ../env/visualize_species.py -i %s -v 0'
    # process = subprocess.Popen(command_template % \
    #     (os.path.join(trn_sess_path, 'species_data')),
    #     shell=True, stdout=subprocess.PIPE
    # )
    # process.wait()
    # if process.returncode:
    #     print('%s: Cannot burst species images.' % trn_sess_path)
    #     return
    # else:
    #     print('%s: Success.' % trn_sess_path)

    # step 0: initialize the 'visual' directory for the training session
    visual_path = os.path.join(trn_sess_path, 'visual')
    if os.path.exists( visual_path ) == False:
        os.makedirs( visual_path ) 

    copyfile(GENEALOGY_HTML, os.path.join(visual_path, 'genealogy.html'))
    copyfile(GENEALOGY_HTML, os.path.join(visual_path, 'evolution.html'))
    copyfile(GENEALOGY_HTML, os.path.join(visual_path, 'expand_genealogy.html'))

    # step 1: generate the evolution json
    species_data_path = os.path.join(trn_sess_path, 'species_data')
    evolution = vis_tree.evolution_graph(species_data_path, k=args.topk)

    # step 2: generate the genealogy json
    gene_path = os.path.join(trn_sess_path, 'gene_tree.npy')

    candidate = vis_tree.prune_species(species_data_path)