# Path: Echobase/Common/errors.py
# def check_dims(arr, nd):
# def check_type(obj, typ):
# def check_function(obj):
# def check_path(path):
# def make_path(path):
# def check_path_overwrite(path):
# def check_has_key(dictionary, key_ref):
# 
# Path: Echobase/Common/display.py
# def my_display(txt, verbose, str_path=None):
# def par_watch_stdout(ar, dt=1.0, truncate=1000):


import numpy as np
import nmf
import nnls
import matrix_utils
import multiprocessing as mp
from ....Common import errors, display

    for qmeas_dict in qmeas_list:
        for param_dict in param_list:
            if qmeas_dict['param_id'] == param_dict['param_id']:
                optimization_dict['alpha'].append(param_dict['alpha'])
                optimization_dict['beta'].append(param_dict['beta'])
                optimization_dict['rank'].append(param_dict['rank'])
                optimization_dict['error'].append(qmeas_dict['error'])
                optimization_dict['pct_sparse_subgraph'].append(qmeas_dict['pct_sparse_subgraph'])
                optimization_dict['pct_sparse_coef'].append(qmeas_dict['pct_sparse_coef'])

    for key in optimization_dict.keys():
        optimization_dict[key] = np.array(optimization_dict[key])

    # Find optimum parameter set
    opt_param_dict = {}
    opt_param_dict['rank'] = int(optimization_dict['rank'][optimization_dict['error'] < np.percentile(optimization_dict['error'], search_pct)].mean().round())
    opt_param_dict['alpha'] = optimization_dict['alpha'][optimization_dict['error'] < np.percentile(optimization_dict['error'], search_pct)].mean()
    opt_param_dict['beta'] = optimization_dict['beta'][optimization_dict['error'] < np.percentile(optimization_dict['error'], search_pct)].mean()

    print('Optimal Rank: {}'.format(opt_param_dict['rank']))
    print('Optimal Alpha: {}'.format(opt_param_dict['alpha']))
    print('Optimal Beta: {}'.format(opt_param_dict['beta']))


    return optimization_dict, opt_param_dict


def _cons_seeds(param_dict):
    # Display output

# Path: Echobase/Common/errors.py
# def check_dims(arr, nd):
# def check_type(obj, typ):
# def check_function(obj):
# def check_path(path):
# def make_path(path):
# def check_path_overwrite(path):
# def check_has_key(dictionary, key_ref):
# 
# Path: Echobase/Common/display.py
# def my_display(txt, verbose, str_path=None):
# def par_watch_stdout(ar, dt=1.0, truncate=1000):


# Path: Echobase/Network/Partitioning/Subgraph/optimize_nmf.py
import numpy as np
import nmf
import nnls
import matrix_utils
import multiprocessing as mp
from ....Common import errors, display


    for qmeas_dict in qmeas_list:
        for param_dict in param_list:
            if qmeas_dict['param_id'] == param_dict['param_id']:
                optimization_dict['alpha'].append(param_dict['alpha'])
                optimization_dict['beta'].append(param_dict['beta'])
                optimization_dict['rank'].append(param_dict['rank'])
                optimization_dict['error'].append(qmeas_dict['error'])
                optimization_dict['pct_sparse_subgraph'].append(qmeas_dict['pct_sparse_subgraph'])
                optimization_dict['pct_sparse_coef'].append(qmeas_dict['pct_sparse_coef'])

    for key in optimization_dict.keys():
        optimization_dict[key] = np.array(optimization_dict[key])

    # Find optimum parameter set
    opt_param_dict = {}
    opt_param_dict['rank'] = int(optimization_dict['rank'][optimization_dict['error'] < np.percentile(optimization_dict['error'], search_pct)].mean().round())
    opt_param_dict['alpha'] = optimization_dict['alpha'][optimization_dict['error'] < np.percentile(optimization_dict['error'], search_pct)].mean()
    opt_param_dict['beta'] = optimization_dict['beta'][optimization_dict['error'] < np.percentile(optimization_dict['error'], search_pct)].mean()

    print('Optimal Rank: {}'.format(opt_param_dict['rank']))
    print('Optimal Alpha: {}'.format(opt_param_dict['alpha']))
    print('Optimal Beta: {}'.format(opt_param_dict['beta']))


    return optimization_dict, opt_param_dict


def _cons_seeds(param_dict):
    # Display output

    display.my_display('Optimizing parameter set: {} \n'.format(param_dict['param_id']), True, param_dict['str_path'])