<commit_before>"""
Commands that were in the Makefile for running various tests. Roughly
converted to a Python script so we can run on all platforms. Work in
progress.

Buildbot shows how to run the tests, but here are some examples:

unit:
./topographica -p 'targets=["unit"]' topo/tests/runtests.py

default set:
./topographica topo/tests/runtests.py

all:
./topographica -p 'targets=["all"]' topo/tests/runtests.py

Ideally, the 'unit' target (i.e. unit tests) would be all that people
need to run. Developers, for checking they haven't messed obvious
stuff up. Users, for checking a Topographica installation functions ok
on their system. The other, slower targets would only need to be run
by buildbot (on behalf of everyone).

Unfortunately, the unit tests do not currently cover enough! It's an
important project to improve them.
"""

# CEBALERT: need to fix the issue with global_params reporting name=X
# is unused.


# Comments from the Makefile that I haven't processed yet:
#
# (1)
# CB: Beyond 14 dp, the results of the current tests do not match on
# ppc64 and i686 (using linux).  In the future, decimal=14 might have
# to be reduced (if the tests change, or to accommodate other
# processors/platforms).
#


import glob
import os
import sys
import tempfile
import commands

import param
from topo.misc.commandline import global_params as p

p.add(
    targets = param.List(default=[]),

    extra_args = param.String(default=""), 

    coverage = param.Boolean(default=False),

    timing = param.Boolean(default=False),

    weave = param.Boolean(default=True),

    testdp = param.Number(default=7)
    
    )


if p.coverage:
    coverage_cmd = "bin/coverage run --rcfile=doc/buildbot/coveragerc -a -p"
else:
    coverage_cmd = ""

if p.timing:
    timing_cmd = "/usr/bin/time"
else:
    timing_cmd = ""


# http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
def which(program):
    import os
    def is_exe(fpath):
        return os.path.exists(fpath) and os.access(fpath, os.X_OK)

    fpath, fname = os.path.split(program)
    if fpath:
        if is_exe(program):
            return program
    else:
        for path in os.environ["PATH"].split(os.pathsep):
            exe_file = os.path.join(path, program)
            if is_exe(exe_file):
                return exe_file
    return ""

xvfb = which('xvfb-run')
if not xvfb:
    print "xvfb-run not found; any GUI components that are run will display windows"
else:
    xvfb = xvfb + " -a"

if len(p.targets)==0:
    # DEFAULT
    p.targets = ['unit','traintests','snapshots','gui','maptests']
elif p.targets == ['all']:
    # ALL
    p.targets = []


if not p.weave:
    importweave = "-c 'import_weave=False'"
else:
    importweave = ""

# ->params ?
tests_dir = param.resolve_path("topo/tests",path_to_file=False)
scripts_dir = param.resolve_path("examples",path_to_file=False) ### XXX
topographica_script = xvfb + " " + timing_cmd + coverage_cmd + " " + sys.argv[0] + " " + importweave +  " " + p.extra_args

def _runc(cmd):
    print cmd
    return os.system(cmd)
    

import topo.misc.keyedlist
target = topo.misc.keyedlist.KeyedList()

# CEBALERT: need to pick which scripts to include for traintests and
# speedtests and startupspeedtests (see test_script.py).
#
# From the Makefile:
# SCRIPTS= ^hierarchical.ty ^lissom_or.ty ^lissom_oo_or.ty ^som_retinotopy.ty ^sullivan_neurocomputing04.ty ^lissom.ty ^lissom_fsa.ty ^gcal.ty ^lissom_whisker_barrels.ty
# CEB: tests on these scripts temporarily suspended (SF.net #2053538)
# ^lissom_oo_or_homeostatic.ty ^lissom_oo_or_homeostatic_tracked.ty
# ^lissom_or_noshrinking.ty  - only matches to 4 dp with IMPORT_WEAVE=0 
# Now I'm using this list for train-tests:
from setup import TRAINSCRIPTS

# (and a different list for speedtests - see test_script.py).
#
# Also: special cases from the Makefile to consider restoring:
#
##topo/tests/lissom.ty_DATA:
##	./topographica -c 'from topo.tests.test_script import generate_data; generate_data(script="examples/lissom.ty",data_filename="tests/lissom.ty_DATA",run_for=[1,99,150],look_at="V1",cortex_density=8,retina_density=6,lgn_density=6,dims=["or","od","dr","dy","cr","sf"])'
##
##topo/tests/lissom_fsa.ty_DATA:
##	./topographica -c 'from topo.tests.test_script import generate_data; generate_data(script="examples/lissom_fsa.ty",data_filename="tests/lissom_fsa.ty_DATA",run_for=[1,99,150],look_at="FSA",cortex_density=8,retina_density=24,lgn_density=24)'
##
##topo/tests/lissom_whisker_barrels.ty_DATA:
##	./topographica -c 'from topo.tests.test_script import generate_data; generate_data(script="examples/lissom_whisker_barrels.ty",data_filename="tests/lissom_whisker_barrels.ty_DATA",run_for=[1,99,150],look_at="S1")'



target['traintests'] = []
for script in TRAINSCRIPTS:
    script_path = os.path.join(scripts_dir,script)
    target['traintests'].append(topographica_script +  " -c 'from topo.tests.test_script import test_script; test_script(script=\"%(script_path)s\",decimal=%(dp)s)'"%dict(script_path=script_path,dp=p.testdp))

target['speedtests'] = []
SPEEDSCRIPTS = TRAINSCRIPTS
SPEEDSCRIPTS.remove("hierarchical.ty") # CEBALERT: remove problematic example (doesn't work for some densities)<commit_msg>Can't import TRAINSCRIPTS from setup.py in some tar.gz versions of Topographica. Not sure why (file not present?).<commit_after>"""
Commands that were in the Makefile for running various tests. Roughly
converted to a Python script so we can run on all platforms. Work in
progress.

Buildbot shows how to run the tests, but here are some examples:

unit:
./topographica -p 'targets=["unit"]' topo/tests/runtests.py

default set:
./topographica topo/tests/runtests.py

all:
./topographica -p 'targets=["all"]' topo/tests/runtests.py

Ideally, the 'unit' target (i.e. unit tests) would be all that people
need to run. Developers, for checking they haven't messed obvious
stuff up. Users, for checking a Topographica installation functions ok
on their system. The other, slower targets would only need to be run
by buildbot (on behalf of everyone).

Unfortunately, the unit tests do not currently cover enough! It's an
important project to improve them.
"""

# CEBALERT: need to fix the issue with global_params reporting name=X
# is unused.


# Comments from the Makefile that I haven't processed yet:
#
# (1)
# CB: Beyond 14 dp, the results of the current tests do not match on
# ppc64 and i686 (using linux).  In the future, decimal=14 might have
# to be reduced (if the tests change, or to accommodate other
# processors/platforms).
#


import glob
import os
import sys
import tempfile
import commands

import param
from topo.misc.commandline import global_params as p

p.add(
    targets = param.List(default=[]),

    extra_args = param.String(default=""), 

    coverage = param.Boolean(default=False),

    timing = param.Boolean(default=False),

    weave = param.Boolean(default=True),

    testdp = param.Number(default=7)
    
    )


if p.coverage:
    coverage_cmd = "bin/coverage run --rcfile=doc/buildbot/coveragerc -a -p"
else:
    coverage_cmd = ""

if p.timing:
    timing_cmd = "/usr/bin/time"
else:
    timing_cmd = ""


# http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
def which(program):
    import os
    def is_exe(fpath):
        return os.path.exists(fpath) and os.access(fpath, os.X_OK)

    fpath, fname = os.path.split(program)
    if fpath:
        if is_exe(program):
            return program
    else:
        for path in os.environ["PATH"].split(os.pathsep):
            exe_file = os.path.join(path, program)
            if is_exe(exe_file):
                return exe_file
    return ""

xvfb = which('xvfb-run')
if not xvfb:
    print "xvfb-run not found; any GUI components that are run will display windows"
else:
    xvfb = xvfb + " -a"

if len(p.targets)==0:
    # DEFAULT
    p.targets = ['unit','traintests','snapshots','gui','maptests']
elif p.targets == ['all']:
    # ALL
    p.targets = []


if not p.weave:
    importweave = "-c 'import_weave=False'"
else:
    importweave = ""

# ->params ?
tests_dir = param.resolve_path("topo/tests",path_to_file=False)
scripts_dir = param.resolve_path("examples",path_to_file=False) ### XXX
topographica_script = xvfb + " " + timing_cmd + coverage_cmd + " " + sys.argv[0] + " " + importweave +  " " + p.extra_args

def _runc(cmd):
    print cmd
    return os.system(cmd)
    

import topo.misc.keyedlist
target = topo.misc.keyedlist.KeyedList()

# CEBALERT: need to pick which scripts to include for traintests and
# speedtests and startupspeedtests (see test_script.py).
#
# From the Makefile:
# SCRIPTS= ^hierarchical.ty ^lissom_or.ty ^lissom_oo_or.ty ^som_retinotopy.ty ^sullivan_neurocomputing04.ty ^lissom.ty ^lissom_fsa.ty ^gcal.ty ^lissom_whisker_barrels.ty
# CEB: tests on these scripts temporarily suspended (SF.net #2053538)
# ^lissom_oo_or_homeostatic.ty ^lissom_oo_or_homeostatic_tracked.ty
# ^lissom_or_noshrinking.ty  - only matches to 4 dp with IMPORT_WEAVE=0 
# Now I'm using this list for train-tests:

# CEBALERT: this list should be defined in one place.
#from setup import TRAINSCRIPTS
TRAINSCRIPTS = [
    "hierarchical.ty",
    "lissom_or.ty",
    "lissom_oo_or.ty",
    "som_retinotopy.ty",
    "sullivan_neurocomputing04.ty",
    "lissom.ty",
    "lissom_fsa.ty",
    "gcal.ty"
    ]


# (and a different list for speedtests - see test_script.py).
#
# Also: special cases from the Makefile to consider restoring:
#
##topo/tests/lissom.ty_DATA:
##	./topographica -c 'from topo.tests.test_script import generate_data; generate_data(script="examples/lissom.ty",data_filename="tests/lissom.ty_DATA",run_for=[1,99,150],look_at="V1",cortex_density=8,retina_density=6,lgn_density=6,dims=["or","od","dr","dy","cr","sf"])'
##
##topo/tests/lissom_fsa.ty_DATA:
##	./topographica -c 'from topo.tests.test_script import generate_data; generate_data(script="examples/lissom_fsa.ty",data_filename="tests/lissom_fsa.ty_DATA",run_for=[1,99,150],look_at="FSA",cortex_density=8,retina_density=24,lgn_density=24)'
##
##topo/tests/lissom_whisker_barrels.ty_DATA:
##	./topographica -c 'from topo.tests.test_script import generate_data; generate_data(script="examples/lissom_whisker_barrels.ty",data_filename="tests/lissom_whisker_barrels.ty_DATA",run_for=[1,99,150],look_at="S1")'



target['traintests'] = []
for script in TRAINSCRIPTS:
    script_path = os.path.join(scripts_dir,script)
    target['traintests'].append(topographica_script +  " -c 'from topo.tests.test_script import test_script; test_script(script=\"%(script_path)s\",decimal=%(dp)s)'"%dict(script_path=script_path,dp=p.testdp))

target['speedtests'] = []
SPEEDSCRIPTS = TRAINSCRIPTS
SPEEDSCRIPTS.remove("hierarchical.ty") # CEBALERT: remove problematic example (doesn't work for some densities)
{'fraction_non_alphanumeric': 0.1237037037037037, 'fraction_numerical': 0.010925925925925926, 'mean_word_length': 4.427638190954774, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1168097', 'n_tokens_mistral': 3945, 'n_tokens_neox': 3784, 'n_words': 933}