Skip to content

Commit

Permalink
Merge pull request #1307 from gaffney2010/file-stuff
Browse files Browse the repository at this point in the history
Create function to handle relative paths
  • Loading branch information
drvinceknight authored Apr 7, 2020
2 parents ecad2a1 + 25514fc commit f7244e6
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 28 deletions.
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
11 changes: 8 additions & 3 deletions axelrod/tests/integration/test_tournament.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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

Expand Down Expand Up @@ -48,7 +50,8 @@ def setUpClass(cls):
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 @@ -93,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 @@ -116,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
14 changes: 9 additions & 5 deletions axelrod/tests/unit/test_tournament.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import io
import logging
import os
import pathlib
import pickle
import warnings
from multiprocessing import Queue, cpu_count

from axelrod.load_data_ import axl_filename
import numpy as np
import pandas as pd
from tqdm import tqdm
Expand Down Expand Up @@ -86,7 +89,8 @@ def setUpClass(cls):
[200, 200, 1, 200, 200],
]

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

def setUp(self):
self.test_tournament = axl.Tournament(
Expand Down Expand Up @@ -726,7 +730,8 @@ def test_write_to_csv_with_results(self):
)
tournament.play(filename=self.filename, progress_bar=False)
df = pd.read_csv(self.filename)
expected_df = pd.read_csv("test_outputs/expected_test_tournament.csv")
path = pathlib.Path("test_outputs/expected_test_tournament.csv")
expected_df = pd.read_csv(axl_filename(path))
self.assertTrue(df.equals(expected_df))

def test_write_to_csv_without_results(self):
Expand All @@ -739,9 +744,8 @@ def test_write_to_csv_without_results(self):
)
tournament.play(filename=self.filename, progress_bar=False, build_results=False)
df = pd.read_csv(self.filename)
expected_df = pd.read_csv(
"test_outputs/expected_test_tournament_no_results.csv"
)
path = pathlib.Path("test_outputs/expected_test_tournament_no_results.csv")
expected_df = pd.read_csv(axl_filename(path))
self.assertTrue(df.equals(expected_df))


Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ hypothesis==3.2
matplotlib>=2.0.0
numpy>=1.9.2
pandas>=0.18.1
pathlib>=1.0.1
prompt-toolkit>=1.0.7
pyyaml>=3.01
scipy>=0.19.0
Expand Down

0 comments on commit f7244e6

Please sign in to comment.