Reproduction scripts

The scripts in reproduction/ reproduce the figures and tables of the publications. They are plain top-level scripts: they build sys.argv by hand and call hetero.cli.main() in a loop over the swept parameters, so they execute on import and cannot be introspected by autodoc. They are therefore embedded verbatim below with literalinclude.

Run them from the repository root; results land under hetero.config.res_root.

python reproduction/different_distros.py
python reproduction/different_inputs.py
python reproduction/uq.py
python reproduction/energy.py
python reproduction/robustness.py
python reproduction/demo.py     # runs the demo/ scripts instead

reproduction/visualization.ipynb is a notebook that re-renders the figures of an existing result folder. It is not included in this build because nbsphinx is not part of the docs extra; open it with Jupyter directly.

Heterogeneity distributions

Sweeps the heterogeneity distribution (lognormal, gamma, normal, uniform) for both dynamics.

import sys
import os
from os.path import join as osjoin

from hetero.cli import main
from hetero.config import res_root

root = osjoin(res_root)
os.makedirs(root, exist_ok=True)

dyns = ['LI', 'LIF']
for dyn in dyns:
    for dist in ['lognormal', 'gamma', 'normal', 'uniform']:
        try:
            sys.argv = [
                "hetero",  
                "--stim", "lorenz",
                "--dyn", dyn,
                "--root", root,
                "--distro", dist,
                ]

            main()
        except Exception as e:
            print(e)

Input classes

Sweeps the stimulus classes and the readouts used for both dynamics.

import sys
import os
from os.path import join as osjoin

from hetero.cli import main
from hetero.config import res_root


root = osjoin(res_root)
os.makedirs(root, exist_ok=True)

# "hetero",  "--N", "9", "--n_trn", "5", "--n_tst", "1", "--n_trials", "1", 

dyns = ['LI', 'LIF']
for dyn in dyns:

    # lorenz
    try:
        sys.argv = [
            "hetero", 
            "--stim", "lorenz",
            "--dyn", dyn,
            "--root", root,
            ]

        main()
    except Exception as e:
        print(e)


    # mackey-glass
    try:
        sys.argv = [
            "hetero", 
            "--stim", "mackey_glass-10",
            "--stim", "mackey_glass-50",
            "--stim", "mackey_glass-80",
            "--dyn", dyn,
            "--root", root,
            ]

        main()
    except Exception as e:
        print(e)


    # narma
    try:
        sys.argv = [
            "hetero", 
            "--stim", "narma", 
            "--dyn", dyn,
            "--root", root,
            ]

        main()
    except Exception as e:
        print(e)


    # sign
    try:
        sys.argv = [
            "hetero", 
            "--stim", "sign", 
            "--dyn", dyn,
            "--root", root,
            ]

        main()
    except Exception as e:
        print(e)

Uncertainty quantification

Sweeps the number of trials and network realizations to separate trial-level from realization-level score variability.

import sys
import os
from os.path import join as osjoin

from hetero.cli import main
from hetero.config import res_root

root = osjoin(res_root, 'uq')
os.makedirs(root, exist_ok=True)

dyns = ['LI', 'LIF']
for dyn in dyns:
    # default (large) N
    try:
        sys.argv = [
            "hetero",  
            "--stim", "lorenz",
            "--dyn", dyn,
            "--root", osjoin(res_root, 'uq'),
            "--n_realizations", str(10),
            "--n_trials", str(1),
            "--nouvx", "--noangle"
            ]

        main()
    except Exception as e:
        print(e)


    # small N (=50)
    try:
        sys.argv = [
            "hetero",  
            "--N", "50",
            "--stim", "lorenz",
            "--dyn", dyn,
            "--root", root,
            "--n_realizations", str(10),
            "--n_trials", str(1),
            "--nouvx", "--noangle"
            ]

        main()
    except Exception as e:
        print(e)

Energy

Sweeps the gains and evaluates the energy cost of the recurrent dynamics.

import sys
import os
from os.path import join as osjoin

from hetero.cli import main
from hetero.config import res_root

# res_root = '/media/arash/OS/Users/arash/hetero_data/'
# res_root = '/media/arash/Expansion/after_conn/energy/'
# os.makedirs(res_root, exist_ok=True)


root = osjoin(res_root, 'energy')
os.makedirs(root, exist_ok=True)

dyns = ['LI', 'LIF']
for dyn in dyns:
    # changing N
    # Nroot = osjoin(res_root, 'N')
    for N in [10, 20, 50, 100, 250, 500]:
        try:
            njobs = -1 if N<500 else 4                        
            sys.argv = [
                "hetero",  
                "--N", str(N),
                "--stim", "lorenz",
                "--dyn", str(dyn),
                "--root", root,
                "--ncore", str(njobs),
                "--n_realizations", "1",
                "--n_trials", "1",
                "--n_trn", str(20*100), 
                "--disable_scale_by_size", 
                "--noviz", "--noangle","--nosvd",
                ]

            main()
        except Exception as e:
            print(e)


    # changing J
    # Jroot = osjoin(res_root, 'J')
    for J in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
             12, 14, 16, 18, 20, 22, 24, 26, 28, 30,
             15, 25]:
        try:
            njobs = -1 #if N<500 else 4
            sys.argv = [
                "hetero",  
                "--J", str(J),
                "--stim", "lorenz",
                "--dyn", str(dyn),
                "--root", root,
                "--n_realizations", "1",
                "--n_trials", "1",
                "--n_trn", str(20*100), 
                "--disable_scale_by_size",  
                "--noviz", "--noangle","--nosvd",
                ]

            main()
        except Exception as e:
            print(e)

    # changing Ju
    for Ju in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
               15, 20, 25, 30]:
        try:
            sys.argv = [
                "hetero",  
                "--Ju", str(Ju),
                "--stim", "lorenz",
                "--dyn", str(dyn),
                "--root", root,
                "--n_realizations", "1",
                "--n_trials", "1",
                "--n_trn", str(20*100), 
                "--disable_scale_by_size",  
                "--noviz", "--noangle","--nosvd",
                ]

            main()
        except Exception as e:
            print(e)

    # # changing Jn
    for Jn in [0, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1, 1.1, 
               1.2, 1.4, 1.6, 1.8, 2, 
               3, 4, 5]:
        try:
            sys.argv = [
                "hetero",  
                "--Jn", str(Jn),
                "--stim", "lorenz",
                "--dyn", str(dyn),
                "--root", root,
                "--n_realizations", "1",
                "--n_trials", "1",
                "--n_trn", str(20*100), 
                "--disable_scale_by_size",  
                "--noviz", "--noangle","--nosvd",
                ]

            main()
        except Exception as e:
            print(e)


    # changing p
    for p in [0.01, .03, .05, .1, .15, .2, .25, .3]:
        try:
            sys.argv = [
                "hetero",  
                "--p", str(p),
                "--stim", "lorenz",
                "--dyn", str(dyn),
                "--root", root,
                "--n_realizations", "1",
                "--n_trials", "1",
                "--n_trn", str(20*100), 
                "--disable_scale_by_size",  
                "--noviz", "--noangle","--nosvd",
                ]

            main()
        except Exception as e:
            print(e)
    
    # changing f
    for f in [0, 0.01, .2, .25, .5, .75, .8, .99, 1.0]: 
        try:
            sys.argv = [
                "hetero",  
                "--f", str(f),
                "--stim", "lorenz",
                "--dyn", str(dyn),
                "--root", root,
                "--n_realizations", "1",
                "--n_trials", "1",
                "--n_trn", str(20*100), 
                "--disable_scale_by_size",  
                "--noviz", "--noangle","--nosvd",
                ]

            main()
        except Exception as e:
            print(e)


    # changing wdist
    for sig0 in [0, .1, .5, 1., 2, 3, 5, 10]: 
        try:
            sys.argv = [
                "hetero",  
                "--sig0", str(sig0),
                "--stim", "lorenz",
                "--dyn", str(dyn),
                "--root", root,
                "--n_realizations", "1",
                "--n_trials", "1",
                "--n_trn", str(20*100), 
                "--disable_scale_by_size",  
                "--noviz", "--noangle","--nosvd",
                ]

            main()
        except Exception as e:
            print(e)


    
    # changing distros
    for distro in ['lognormal', 'gamma', 'normal', 'uniform']: 
        try:
            sys.argv = [
                "hetero",  
                "--distro", str(distro),
                "--stim", "lorenz",
                "--dyn", str(dyn),
                "--root", root,
                "--n_realizations", "1",
                "--n_trials", "1",
                "--n_trn", str(20*100), 
                "--disable_scale_by_size",  
                "--noviz", "--noangle","--nosvd",
                ]

            main()
        except Exception as e:
            print(e)


    




Robustness

Sweeps the input and network perturbations and reports the score degradation.

import sys
import os
from os.path import join as osjoin

from hetero.cli import main
from hetero.config import res_root

# root = '/media/arash/Expansion/after_conn/'
root = osjoin(res_root)
os.makedirs(root, exist_ok=True)


dyns = ['LI', 'LIF']
for dyn in dyns:

    # changing N
    for N in [10, 20, 50, 100, 250, 500]:
        try:
            njobs = -1 if N<500 else 4
            sys.argv = [
                "hetero",  
                "--N", str(N),
                "--stim", "lorenz",
                "--dyn", dyn,
                "--root", root,
                "--ncore", str(njobs),
                "--nouvx", "--notau", "--noangle",  "--nosvd"
                ]

            main()
        except Exception as e:
            print(e)


    # changing J
    for J in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
             12, 14, 16, 18, 20, 22, 24, 26, 28, 30,
             15, 25]:
        try:
            sys.argv = [
                "hetero",  
                "--J", str(J),
                "--stim", "lorenz",
                "--dyn", dyn,
                "--root", root,
                "--notau", "--noangle", "--noconn", "--nosvd",
                ]

            main()
        except Exception as e:
            print(e)

    # changing Ju
    for Ju in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
               15, 20, 25, 30]:
        try:
            sys.argv = [
                "hetero",  
                "--Ju", str(Ju),
                "--stim", "lorenz",
                "--dyn", dyn,
                "--root", root,
                "--notau", "--noangle", "--noconn", "--nosvd",
                ]

            main()
        except Exception as e:
            print(e)

    # changing Jn
    for Jn in [0, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1, 1.1, 
               1.2, 1.4, 1.6, 1.8, 2, 
               3, 4, 5]:
        try:
            sys.argv = [
                "hetero",  
                "--Jn", str(Jn),
                "--stim", "lorenz",
                "--dyn", dyn,
                "--root", root,
                "--notau", "--noangle", "--noconn", "--nosvd",
                ]

            main()
        except Exception as e:
            print(e)



    # changing p
    for p in [0.01, .03, .05, .1, .15, .2, .25, .3]:
        try:
            sys.argv = [
                "hetero",  
                "--p", str(p),
                "--stim", "lorenz",
                "--dyn", dyn,
                "--root", root,
                "--notau", "--noangle", "--nosvd",
                ]

            main()
        except Exception as e:
            print(e)
    
    # changing f
    for f in [0, 0.01, .2, .25, .5, .75, .8, .99, 1.0]: 
        try:
            sys.argv = [
                "hetero",  
                "--f", str(f),
                "--stim", "lorenz",
                "--dyn", dyn,
                "--root", root,
                "--notau", "--noangle", "--nosvd",
                ]

            main()
        except Exception as e:
            print(e)


    # changing sigma0
    for sig0 in [0, .1, .5, 1., 2, 3, 5, 10]: 
        try:
            sys.argv = [
                "hetero",  
                "--sig0", str(sig0),
                "--stim", "lorenz",
                "--dyn", dyn,
                "--root", root,
                "--notau", "--noangle", "--nosvd",
                ]

            main()
        except Exception as e:
            print(e)

Demo driver

Thin wrapper that runs the demo/ scripts described in Demo.

import os
import sys
import inspect
from os.path import join as osjoin

# Demo is implemented as pre reviewers request
# it is not intended to be part of the hetero package
# thus we import its path manually
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
demodir = osjoin(parentdir, 'demo')
sys.path.insert(0, demodir) 

from demo_cli import main

# from hetero.config import res_root



# MANTE (two choice)
# Ju = 100
# J = 0
# for stoch in ["stim", "delay"]:
#     try:
#         sys.argv = [
#             "main", 
#             "--N", "50", 
#             "--dataset", "mante",
#             "--stoch", stoch,
#             "--J", str(J),
#             "--Ju", str(Ju),
#             "--Nnet", str(2),
#             "--dim_ring", "2",
#             "--name", f"mante_dim2_N50_{stoch}_J{J}_Ju{Ju}"
#             ]

#         main()
#     except Exception as e:
#         print(e)

# MANTE 
Ju = 100
J = 0
for stoch in ["stim", "delay"]:
    try:
        sys.argv = [
            "main",  
            "--dataset", "mante",
            "--stoch", stoch,
            "--J", str(J),
            "--Ju", str(Ju),
            "--name", f"mante_{stoch}_J{J}_Ju{Ju}",
            "--Nnet", "10",
            ]

        main()
    except Exception as e:
        print(e)


try:
    sys.argv = [
        "main",  
        "--dataset", "speechcommands",
        "--stoch", stoch,
        "--J", str(J),
        "--Ju", str(Ju),
        "--name", f"SC_J{J}_Ju{Ju}",
        "--Nnet", "10",
        ]

    main()
except Exception as e:
    print(e)


try:
    sys.argv = [
        "main",  
        "--dataset", "mnist",
        "--stoch", stoch,
        "--J", str(J),
        "--Ju", str(Ju),
        "--name", f"mnist_J{J}_Ju{Ju}",
        "--Nnet", "10",
        ]

    main()
except Exception as e:
    print(e)