Created
March 15, 2017 01:33
-
-
Save wmayner/7a9189d5dded28aee2cf214ed1ee9718 to your computer and use it in GitHub Desktop.
Evaluating a random cut vs. other PyPhi functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import os | |
import pickle | |
import random | |
from time import time | |
import numpy as np | |
import pyphi | |
from pyphi import Network, Subsystem | |
from pyphi.compute.big_phi import (big_mip_bipartitions, evaluate_cut, | |
_null_bigmip, _find_mip_sequential) | |
# Setup | |
# ~~~~~ | |
pyphi.config.CACHE_BIGMIPS = False | |
pyphi.config.PARALLEL_CUT_EVALUATION = False | |
N = 5 | |
ITERATIONS = 10 | |
filename = 'test-data-{}-nodes.pkl'.format(N) | |
if os.path.exists(filename): | |
print('Loading random networks, subsystems, and ' | |
'their unpartitioned constellations... ', end='', flush=True) | |
with open(filename, 'rb') as f: | |
rand_nets, rand_subs, unpartitioned_constellations = pickle.load(f) | |
print('done.', flush=True) | |
else: | |
print('Making random networks and subsystems... ', end='', flush=True) | |
tpms = [np.random.randint(2, size=[2]*N + [N]) for i in range(ITERATIONS)] | |
cms = [np.random.randint(2, size=[N, N]) for i in range(ITERATIONS)] | |
print('done.', flush=True) | |
def get_rand_sub(net): | |
while True: | |
try: | |
state = np.random.randint(2, size=N) | |
return Subsystem(net, state, range(N)) | |
except pyphi.exceptions.StateUnreachableError: | |
pass | |
rand_nets = [Network(tpms[i], connectivity_matrix=cms[i]) | |
for i in range(ITERATIONS)] | |
rand_subs = [get_rand_sub(rand_nets[i]) | |
for i in range(ITERATIONS)] | |
unpartitioned_constellations = [] | |
for i in range(ITERATIONS): | |
print('Precomputing unpartitioned constellations... ' | |
'{} / {}'.format(i, ITERATIONS), | |
end='\r', flush=True) | |
unpartitioned_constellations.append( | |
pyphi.compute.constellation(rand_subs[i]) | |
) | |
print('\rPrecomputing unpartitioned constellations... done.') | |
print('\nSaving test data... ', end='') | |
test_data = (rand_nets, rand_subs, unpartitioned_constellations) | |
with open(filename, 'wb') as f: | |
pickle.dump(test_data, f) | |
print('done.', flush=True) | |
cuts = big_mip_bipartitions(range(N)) | |
def mean_time(func, *args): | |
start = time() | |
for i in range(ITERATIONS): | |
print('{}: {} / {}'.format(func.__name__, i, ITERATIONS), end='\r') | |
func(i, *args) | |
end = time() | |
result = round((end - start) / ITERATIONS, 4) | |
print('{}: done. '.format(func.__name__)) | |
print(' Mean time: {}s'.format(result)) | |
return result | |
# Test functions | |
# ~~~~~~~~~~~~~~ | |
def big_mip(i): | |
subsystem = rand_subs[i] | |
pyphi.compute.big_mip(subsystem) | |
def naive_big_mip(i): | |
subsystem = rand_subs[i] | |
unpartitioned_constellation = pyphi.compute.constellation(subsystem) | |
min_mip = _null_bigmip(subsystem) | |
min_mip.phi = float('inf') | |
_find_mip_sequential(subsystem, cuts, unpartitioned_constellation, min_mip) | |
def unpartitioned_constellation(i): | |
subsystem = rand_subs[i] | |
unpartitioned_constellation = pyphi.compute.constellation(subsystem) | |
evaluate_cut(subsystem, random.choice(cuts), | |
unpartitioned_constellation) | |
def random_cut(i): | |
subsystem = rand_subs[i] | |
unpartitioned_constellation = unpartitioned_constellations[i] | |
evaluate_cut(subsystem, random.choice(cuts), | |
unpartitioned_constellation) | |
print('\nTiming functions ({} nodes, {} iterations)'.format(N, ITERATIONS)) | |
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') | |
results = {'nodes': N} | |
results.update({ | |
func.__name__: mean_time(func) | |
for func in [big_mip, naive_big_mip, unpartitioned_constellation, | |
random_cut] | |
}) | |
with open('results-{}-nodes.json'.format(N), 'wt') as f: | |
json.dump(results, f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment