# Path: sdopt/datagen/paths.py
# DATADIR = join(_ROOT, 'data')
# 
# Path: sdopt/parsers/dag_parser.py
# def read_problem(filename, crosscheck_nl=True, show_sparsity=True):
#     with lines_of(filename) as lines:
#         problem = parse(lines)
#     setup(problem)
#     if crosscheck_nl:
#         nl_filename = filename[:-4]+'.nl' # filename.dag -> filename.nl
#         bsp = read_flattened_ampl(nl_filename, show_sparsity)
#         crosscheck_sparsity_pattern(problem, bsp.csr_mat)
#         crosscheck_names(problem, bsp.row_names, bsp.col_names)
#     return problem
# 
# Path: sdopt/nodes/reverse_ad.py
# def prepare_evaluation_code(prob, tracepoint=[ ], only_forward=False):
#     with closing(stringIO()) as code: 
#         code.write(preamble)
#         write_constraint_evaluation_code(prob, code, only_forward)
#         code.write(postamble)
#         code.write( main_func.substitute(v=prob.refsols[0], tp=list(tracepoint), 
#                                          ncons =prob.ncons, nvars=prob.nvars, 
#                                          nzeros=prob.nzeros) )
#         return code.getvalue()
# 
# Path: sdopt/util/misc.py
# def import_code(code):
#     import imp
#     module = imp.new_module('someFakeName')
#     try:
#         exec_(code, module.__dict__)
#     except:
#         print(code)
#         raise
#     return module
# 
# Path: sdopt/tests/JacobianTest.py
# def dump_code(partial_code, logfilename):
#     name = os.path.basename(logfilename)[:-4]
#     debugcode = os.path.join(gettempdir(), name+'.py')
#     print('Dumping debug code at location:', debugcode) 
#     with open(debugcode, 'w') as f:
#         f.write(partial_code)    


import numpy as np
from numpy.linalg import cond, norm
from os.path import join
from ..datagen.paths import DATADIR
from ..parsers.dag_parser import read_problem
from ..nodes.reverse_ad import prepare_evaluation_code
from ..util.misc import import_code 
from ..tests.JacobianTest import dump_code
from __future__ import print_function

def diagnostics(basename):
    dagfilename = join(DATADIR, basename+'.dag')
    prob = read_problem(dagfilename, show_sparsity=False)
    partial_code = prepare_evaluation_code(prob) 

# Path: sdopt/datagen/paths.py
# DATADIR = join(_ROOT, 'data')
# 
# Path: sdopt/parsers/dag_parser.py
# def read_problem(filename, crosscheck_nl=True, show_sparsity=True):
#     with lines_of(filename) as lines:
#         problem = parse(lines)
#     setup(problem)
#     if crosscheck_nl:
#         nl_filename = filename[:-4]+'.nl' # filename.dag -> filename.nl
#         bsp = read_flattened_ampl(nl_filename, show_sparsity)
#         crosscheck_sparsity_pattern(problem, bsp.csr_mat)
#         crosscheck_names(problem, bsp.row_names, bsp.col_names)
#     return problem
# 
# Path: sdopt/nodes/reverse_ad.py
# def prepare_evaluation_code(prob, tracepoint=[ ], only_forward=False):
#     with closing(stringIO()) as code: 
#         code.write(preamble)
#         write_constraint_evaluation_code(prob, code, only_forward)
#         code.write(postamble)
#         code.write( main_func.substitute(v=prob.refsols[0], tp=list(tracepoint), 
#                                          ncons =prob.ncons, nvars=prob.nvars, 
#                                          nzeros=prob.nzeros) )
#         return code.getvalue()
# 
# Path: sdopt/util/misc.py
# def import_code(code):
#     import imp
#     module = imp.new_module('someFakeName')
#     try:
#         exec_(code, module.__dict__)
#     except:
#         print(code)
#         raise
#     return module
# 
# Path: sdopt/tests/JacobianTest.py
# def dump_code(partial_code, logfilename):
#     name = os.path.basename(logfilename)[:-4]
#     debugcode = os.path.join(gettempdir(), name+'.py')
#     print('Dumping debug code at location:', debugcode) 
#     with open(debugcode, 'w') as f:
#         f.write(partial_code)    


# Path: sdopt/util/condition_number.py
import numpy as np
from numpy.linalg import cond, norm
from os.path import join
from ..datagen.paths import DATADIR
from ..parsers.dag_parser import read_problem
from ..nodes.reverse_ad import prepare_evaluation_code
from ..util.misc import import_code 
from ..tests.JacobianTest import dump_code

from __future__ import print_function

def diagnostics(basename):
    dagfilename = join(DATADIR, basename+'.dag')
    prob = read_problem(dagfilename, show_sparsity=False)
    partial_code = prepare_evaluation_code(prob) 

    rev_ad = import_code(partial_code)