Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create function to handle relative paths #1307

Merged
merged 5 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion axelrod/load_data_.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
from typing import Dict, List, Tuple
import pathlib
from typing import Dict, List, Text, Tuple

import pkg_resources


def axl_filename(path: pathlib.Path) -> pathlib.Path:
"""Given a path under Axelrod/, return absolute filepath.

Parameters
----------
axl_path
A pathlib.Path object with the relative directory under Axelrod/

Returns
-------
A pathlib.Path object with the absolute directory.
"""
# We go up a dir because this code is located in Axelrod/axelrod.
axl_path = pathlib.Path(__file__).resolve().parent.parent
return axl_path / path


def load_file(filename: str, directory: str) -> List[List[str]]:
"""Loads a data file stored in the Axelrod library's data subdirectory,
likely for parameters for a strategy."""
Expand Down
5 changes: 4 additions & 1 deletion axelrod/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.transforms as transforms
import pathlib
import tqdm
from numpy import arange, median, nan_to_num

from .result_set import ResultSet
from .load_data_ import axl_filename

titleType = List[str]
namesType = List[str]
Expand Down Expand Up @@ -323,7 +325,8 @@ def save_all_plots(

for method, name in plots:
f = getattr(self, method)(title="{} - {}".format(title_prefix, name))
f.savefig("{}_{}.{}".format(prefix, method, filetype))
path = pathlib.Path("{}_{}.{}".format(prefix, method, filetype))
f.savefig(axl_filename(path))
plt.close(f)

if progress_bar:
Expand Down
32 changes: 20 additions & 12 deletions axelrod/tests/integration/test_tournament.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import unittest

import filecmp
import pathlib

import axelrod as axl
from axelrod.load_data_ import axl_filename
from axelrod.strategy_transformers import FinalTransformer
from axelrod.tests.property import tournaments

from hypothesis import given, settings


class TestTournament(unittest.TestCase):
@classmethod
def setUpClass(cls):
Expand All @@ -32,20 +35,23 @@ def setUpClass(cls):
]
cls.expected_outcome.sort()

@given(tournaments(
strategies=axl.short_run_time_strategies,
min_size=10,
max_size=30,
min_turns=2,
max_turns=210,
min_repetitions=1,
max_repetitions=4,
))
@given(
tournaments(
strategies=axl.short_run_time_strategies,
min_size=10,
max_size=30,
min_turns=2,
max_turns=210,
min_repetitions=1,
max_repetitions=4,
)
)
@settings(max_examples=1)
def test_big_tournaments(self, tournament):
"""A test to check that tournament runs with a sample of non-cheating
strategies."""
filename = "test_outputs/test_tournament.csv"
path = pathlib.Path("test_outputs/test_tournament.csv")
filename = axl_filename(path)
self.assertIsNone(
tournament.play(progress_bar=False, filename=filename, build_results=False)
)
Expand Down Expand Up @@ -90,7 +96,8 @@ def test_repeat_tournament_deterministic(self):
turns=2,
repetitions=2,
)
files.append("test_outputs/stochastic_tournament_{}.csv".format(_))
path = pathlib.Path("test_outputs/stochastic_tournament_{}.csv".format(_))
files.append(axl_filename(path))
tournament.play(progress_bar=False, filename=files[-1], build_results=False)
self.assertTrue(filecmp.cmp(files[0], files[1]))

Expand All @@ -113,7 +120,8 @@ def test_repeat_tournament_stochastic(self):
turns=2,
repetitions=2,
)
files.append("test_outputs/stochastic_tournament_{}.csv".format(_))
path = pathlib.Path("test_outputs/stochastic_tournament_{}.csv".format(_))
files.append(axl_filename(path))
tournament.play(progress_bar=False, filename=files[-1], build_results=False)
self.assertTrue(filecmp.cmp(files[0], files[1]))

Expand Down
11 changes: 8 additions & 3 deletions axelrod/tests/unit/test_deterministic_cache.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import unittest
import os
import pathlib
import pickle

import axelrod as axl
from axelrod.load_data_ import axl_filename

C, D = axl.Action.C, axl.Action.D

Expand All @@ -12,8 +14,10 @@ class TestDeterministicCache(unittest.TestCase):
def setUpClass(cls):
cls.test_key = (axl.TitForTat(), axl.Defector())
cls.test_value = [(C, D), (D, D), (D, D)]
cls.test_save_file = "test_cache_save.txt"
cls.test_load_file = "test_cache_load.txt"
save_path = pathlib.Path("test_outputs/test_cache_save.txt")
cls.test_save_file = axl_filename(save_path)
load_path = pathlib.Path("test_outputs/test_cache_load.txt")
cls.test_load_file = axl_filename(load_path)
test_data_to_pickle = {("Tit For Tat", "Defector"): [(C, D), (D, D), (D, D)]}
cls.test_pickle = pickle.dumps(test_data_to_pickle)

Expand Down Expand Up @@ -92,7 +96,8 @@ def test_load(self):
self.assertEqual(self.cache[self.test_key], self.test_value)

def test_load_error_for_inccorect_format(self):
filename = "test_outputs/test.cache"
path = pathlib.Path("test_outputs/test.cache")
filename = axl_filename(path)
with open(filename, "wb") as io:
pickle.dump(range(5), io)

Expand Down
22 changes: 14 additions & 8 deletions axelrod/tests/unit/test_fingerprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
from unittest.mock import patch

import os

from tempfile import mkstemp

import matplotlib.pyplot

import numpy as np
import pathlib

import axelrod as axl
from axelrod.fingerprint import AshlockFingerprint, Point, TransitiveFingerprint
from axelrod.load_data_ import axl_filename
from axelrod.strategy_transformers import DualTransformer, JossAnnTransformer
from axelrod.tests.property import strategy_lists

Expand Down Expand Up @@ -200,7 +199,8 @@ def test_temp_file_creation(self):

RecordedMksTemp.reset_record()
af = AshlockFingerprint(axl.TitForTat)
filename = "test_outputs/test_fingerprint.csv"
path = pathlib.Path("test_outputs/test_fingerprint.csv")
filename = axl_filename(path)

self.assertEqual(RecordedMksTemp.record, [])

Expand All @@ -216,7 +216,8 @@ def test_temp_file_creation(self):
self.assertFalse(os.path.isfile(filename))

def test_fingerprint_with_filename(self):
filename = "test_outputs/test_fingerprint.csv"
path = pathlib.Path("test_outputs/test_fingerprint.csv")
filename = axl_filename(path)
af = AshlockFingerprint(axl.TitForTat)
af.fingerprint(
turns=1, repetitions=1, step=0.5, progress_bar=False, filename=filename
Expand Down Expand Up @@ -430,7 +431,8 @@ def test_init_with_not_default_number(self):
)

def test_fingerprint_with_filename(self):
filename = "test_outputs/test_fingerprint.csv"
path = pathlib.Path("test_outputs/test_fingerprint.csv")
filename = axl_filename(path)
strategy = axl.TitForTat()
tf = TransitiveFingerprint(strategy)
tf.fingerprint(turns=1, repetitions=1, progress_bar=False, filename=filename)
Expand All @@ -441,8 +443,11 @@ def test_fingerprint_with_filename(self):
def test_serial_fingerprint(self):
strategy = axl.TitForTat()
tf = TransitiveFingerprint(strategy)
path = pathlib.Path("test_outputs/test_fingerprint.csv")
tf.fingerprint(
repetitions=1, progress_bar=False, filename="test_outputs/tran_fin.csv"
repetitions=1,
progress_bar=False,
filename=axl_filename(path),
)
self.assertEqual(tf.data.shape, (50, 50))

Expand All @@ -455,7 +460,8 @@ def test_parallel_fingerprint(self):

def test_analyse_cooperation_ratio(self):
tf = TransitiveFingerprint(axl.TitForTat)
filename = "test_outputs/test_fingerprint.csv"
path = pathlib.Path("test_outputs/test_fingerprint.csv")
filename = axl_filename(path)
with open(filename, "w") as f:
f.write(
"""Interaction index,Player index,Opponent index,Repetition,Player name,Opponent name,Actions
Expand Down
17 changes: 17 additions & 0 deletions axelrod/tests/unit/test_load_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os
import pathlib
import unittest

from axelrod.load_data_ import axl_filename


class TestLoadData(unittest.TestCase):
def test_axl_filename(self):
path = pathlib.Path("axelrod/strategies/titfortat.py")
actual_fn = axl_filename(path)

# First go from "unit" up to "tests", then up to "axelrod"
dirname = os.path.dirname(__file__)
expected_fn = os.path.join(dirname, "../../strategies/titfortat.py")

self.assertTrue(os.path.samefile(actual_fn, expected_fn))
6 changes: 4 additions & 2 deletions axelrod/tests/unit/test_plot.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import unittest

import tempfile

import matplotlib
import matplotlib.pyplot as plt
import pathlib

from numpy import mean

import axelrod as axl
from axelrod.load_data_ import axl_filename


class TestPlot(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.filename = "test_outputs/test_results.csv"
path = pathlib.Path("test_outputs/test_results.csv")
cls.filename = axl_filename(path)

cls.players = [axl.Alternator(), axl.TitForTat(), axl.Defector()]
cls.repetitions = 3
Expand Down
16 changes: 11 additions & 5 deletions axelrod/tests/unit/test_resultset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import pandas as pd
from dask.dataframe.core import DataFrame
from numpy import mean, nanmedian, std
import pathlib

import axelrod as axl
from axelrod.load_data_ import axl_filename
from axelrod.result_set import create_counter_dict
from axelrod.tests.property import prob_end_tournaments, tournaments

Expand All @@ -18,7 +20,8 @@ class TestResultSet(unittest.TestCase):
@classmethod
def setUpClass(cls):

cls.filename = "test_outputs/test_results.csv"
path = pathlib.Path("test_outputs/test_results.csv")
cls.filename = str(axl_filename(path))

cls.players = [axl.Alternator(), axl.TitForTat(), axl.Defector()]
cls.repetitions = 3
Expand Down Expand Up @@ -192,7 +195,7 @@ def test_init(self):
def _clear_matrix(self, matrix):
for i, row in enumerate(matrix):
for j, _ in enumerate(row):
matrix[i][j] = 0
matrix[i][j] = 0

def test_ne_vectors(self):
rs_1 = axl.ResultSet(self.filename, self.players, self.repetitions)
Expand Down Expand Up @@ -676,7 +679,8 @@ class TestResultSetSpatialStructure(TestResultSet):
@classmethod
def setUpClass(cls):

cls.filename = "test_outputs/test_results_spatial.csv"
path = pathlib.Path("test_outputs/test_results_spatial.csv")
cls.filename = str(axl_filename(path))
cls.players = [axl.Alternator(), axl.TitForTat(), axl.Defector()]
cls.turns = 5
cls.edges = [(0, 1), (0, 2)]
Expand Down Expand Up @@ -856,7 +860,8 @@ class TestResultSetSpatialStructureTwo(TestResultSetSpatialStructure):
@classmethod
def setUpClass(cls):

cls.filename = "test_outputs/test_results_spatial_two.csv"
path = pathlib.Path("test_outputs/test_results_spatial_two.csv")
cls.filename = str(axl_filename(path))
cls.players = [
axl.Alternator(),
axl.TitForTat(),
Expand Down Expand Up @@ -1057,7 +1062,8 @@ class TestResultSetSpatialStructureThree(TestResultSetSpatialStructure):
@classmethod
def setUpClass(cls):

cls.filename = "test_outputs/test_results_spatial_three.csv"
path = pathlib.Path("test_outputs/test_results_spatial_three.csv")
cls.filename = str(axl_filename(path))
cls.players = [
axl.Alternator(),
axl.TitForTat(),
Expand Down
Loading