-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
13 additions
and
83 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,25 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# test_new_big_phi.py | ||
# test_iit4.py | ||
|
||
import json | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from pyphi import compute | ||
from pyphi import jsonify, new_big_phi | ||
from pyphi.examples import EXAMPLES | ||
from pyphi.jsonify import jsonify | ||
from pyphi.new_big_phi import sia | ||
from pyphi.relations import relations | ||
|
||
NETWORKS = ["basic", "basic_noisy_selfloop", "fig4", "grid3", "xor"] | ||
EXAMPLE_NAMES = ["basic", "basic_noisy_selfloop", "fig4", "grid3", "xor"] | ||
|
||
DATA_PATH = Path("test/data/iit4/phi_structure") | ||
|
||
def remove_ids(dct: dict): | ||
has_id = False | ||
|
||
for key, value in dct.items(): | ||
if isinstance(value, dict): | ||
remove_ids(value) | ||
|
||
if isinstance(value, list): | ||
for item in value: | ||
if isinstance(item, dict): | ||
remove_ids(item) | ||
|
||
if key == "__id__": | ||
has_id = True | ||
|
||
if has_id: | ||
del dct["__id__"] | ||
|
||
|
||
def expected_json(type, example): | ||
PATH = f"test/data/new_big_phi/{type}/{type}_{example}.json" | ||
|
||
with open(PATH) as f: | ||
expected = json.load(f) | ||
|
||
return expected | ||
|
||
|
||
def assert_equality(actual, expected): | ||
actual = jsonify(actual) | ||
|
||
if isinstance(actual, dict): | ||
assert isinstance(expected, dict) | ||
|
||
remove_ids(actual) | ||
remove_ids(expected) | ||
|
||
else: # should be lists if not dictionaries | ||
assert isinstance(actual, list) | ||
assert isinstance(expected, list) | ||
|
||
for item in actual + expected: | ||
if isinstance(item, dict): | ||
remove_ids(item) | ||
|
||
@pytest.mark.parametrize("example_name", EXAMPLE_NAMES) | ||
def test(example_name): | ||
subsystem = EXAMPLES["subsystem"][example_name]() | ||
actual = new_big_phi.phi_structure(subsystem) | ||
expected = load_expected(example_name) | ||
assert actual == expected | ||
|
||
|
||
# Tests | ||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
|
||
@pytest.mark.parametrize("case_name", NETWORKS) | ||
def test_sia(case_name): | ||
example_func = EXAMPLES["subsystem"][case_name] | ||
actual = sia(example_func(), parallel=False) | ||
expected = expected_json("sia", case_name) | ||
|
||
assert_equality(actual, expected) | ||
|
||
|
||
@pytest.mark.parametrize("case_name", NETWORKS) | ||
def test_compute_subsystem_ces(case_name): | ||
example_func = EXAMPLES["subsystem"][case_name] | ||
actual = compute.ces(example_func()) | ||
expected = expected_json("ces", case_name) | ||
|
||
assert_equality(actual, expected) | ||
|
||
|
||
@pytest.mark.parametrize("case_name", NETWORKS) | ||
def test_relations(case_name): | ||
subsystem = EXAMPLES["subsystem"][case_name]() | ||
ces = compute.ces(subsystem) | ||
actual = relations(ces, parallel=False) | ||
expected = expected_json("relations", case_name) | ||
|
||
assert_equality(actual, expected) | ||
def load_expected(example_name): | ||
with open(DATA_PATH / f"{example_name}.json") as f: | ||
return jsonify.load(f) |