#!/usr/bin/env python
"""
This script analyzes a number of mechanisms that are provided
with GraTeLPy and times execution time of these analyses.

The following mechansims are analyzed with one client:
Reversible Substrate Inhibition
Glycolysis Gluconeogenesis
Cdc42 Yeast

The following mechanisms are analyzed with two clients:
Cdc42 Yeast
Single-Layer MAPK

All analyses are repeated three times.
"""

import os
path = os.path.split(os.path.realpath(__file__))[0]

from multiprocessing import freeze_support
from gratelpy import analyze, get_mechanism
from gratelpy.pyin import resource_path
from os.path import join
from time import time

import sys
import networkx

def main():
    print('\nGraTeLPy Copyright (C) 2013  Georg R Walther, Matthew Hartley.\n'
          'This program comes with ABSOLUTELY NO WARRANTY.\n'
          'This is free software, and you are welcome to redistribute it '
          'under certain conditions.\n'
          'For details visit https://github.com/gratelpy/gratelpy and '
          'read our LICENSE or contact the author at gratelpy@gmail.com.\n')

    proc_1 = [(get_mechanism('reversible_substrate_inhibition.txt'), 4),
              (get_mechanism('glycolysis_mechanism.txt'), 7),
              (get_mechanism('cdc42_yeast.txt'), 8)]

    proc_2 = [(get_mechanism('cdc42_yeast.txt'), 8),
              (get_mechanism('single_layer_mapk_mechanism.txt'), 9)]

    py_v = sys.version_info
    py_v_str = '.'.join('%s' % i for i in py_v[:2])

    print('Your Python version is %s' % py_v_str)
    print('Your NetworkX version is %s.' % str(networkx.__version__))
    print('')
    print('One client:')
    print('')

    for m in proc_1:
        timings = []
        print('Analyzing %s ...' % str(m[0])) 
        for i in range(3):
            now = time()
            _ = analyze(m[0], m[1], 1)
            timings.append(time()-now)
            print('run time %.4f seconds' % timings[-1])
        print('median %.4f seconds' % sorted(timings)[1])

    print('')
    print('Two clients:')
    print('')

    for m in proc_2:
        timings = []
        print('Analyzing %s ...' % str(m[0])) 
        for i in range(3):
            now = time()
            _ = analyze(m[0], m[1], 2)
            timings.append(time()-now)
            print('run time %.4f seconds' % timings[-1])
        print('median %.4f seconds' % sorted(timings)[1])

if __name__ == "__main__":
    freeze_support()
    main()
