Skip to content

Commit ff16f77

Browse files
langnermarcharper
authored andcommitted
Spruce up fingerprint (mostly AshlockFingerprint)
* Prefix internal functions and methods with underscores * Test everything through the external API only * Be explicit and local in unit tests (no self.strategy and self.probe)
1 parent 5223fe5 commit ff16f77

File tree

2 files changed

+181
-189
lines changed

2 files changed

+181
-189
lines changed

axelrod/fingerprint.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@
44
from tempfile import mkstemp
55
from typing import Any, List, Union
66

7-
import axelrod as axl
7+
import dask as da
8+
import dask.dataframe as dd
89
import matplotlib.pyplot as plt
910
import numpy as np
1011
import tqdm
12+
from mpl_toolkits.axes_grid1 import make_axes_locatable
13+
14+
import axelrod as axl
1115
from axelrod import Player
1216
from axelrod.interaction_utils import (compute_final_score_per_turn,
1317
read_interactions_from_file)
1418
from axelrod.strategy_transformers import DualTransformer, JossAnnTransformer
15-
from mpl_toolkits.axes_grid1 import make_axes_locatable
16-
17-
import dask as da
18-
import dask.dataframe as dd
1919

2020
Point = namedtuple("Point", "x y")
2121

2222

23-
def create_points(step: float, progress_bar: bool = True) -> List[Point]:
23+
def _create_points(step: float, progress_bar: bool = True) -> List[Point]:
2424
"""Creates a set of Points over the unit square.
2525
2626
A Point has coordinates (x, y). This function constructs points that are
@@ -59,7 +59,7 @@ def create_points(step: float, progress_bar: bool = True) -> List[Point]:
5959
return points
6060

6161

62-
def create_jossann(point: Point, probe: Any) -> Player:
62+
def _create_jossann(point: Point, probe: Any) -> Player:
6363
"""Creates a JossAnn probe player that matches the Point.
6464
6565
If the coordinates of point sums to more than 1 the parameters are
@@ -95,7 +95,7 @@ def create_jossann(point: Point, probe: Any) -> Player:
9595
return joss_ann
9696

9797

98-
def create_probes(
98+
def _create_probes(
9999
probe: Union[type, Player], points: list, progress_bar: bool = True
100100
) -> List[Player]:
101101
"""Creates a set of probe strategies over the unit square.
@@ -121,11 +121,11 @@ def create_probes(
121121
"""
122122
if progress_bar:
123123
points = tqdm.tqdm(points, desc="Generating probes")
124-
probes = [create_jossann(point, probe) for point in points]
124+
probes = [_create_jossann(point, probe) for point in points]
125125
return probes
126126

127127

128-
def create_edges(points: List[Point], progress_bar: bool = True) -> list:
128+
def _create_edges(points: List[Point], progress_bar: bool = True) -> list:
129129
"""Creates a set of edges for a spatial tournament.
130130
131131
Constructs edges that correspond to `points`. All edges begin at 0, and
@@ -152,7 +152,7 @@ def create_edges(points: List[Point], progress_bar: bool = True) -> list:
152152
return edges
153153

154154

155-
def generate_data(interactions: dict, points: list, edges: list) -> dict:
155+
def _generate_data(interactions: dict, points: list, edges: list) -> dict:
156156
"""Generates useful data from a spatial tournament.
157157
158158
Matches interactions from `results` to their corresponding Point in
@@ -186,7 +186,7 @@ def generate_data(interactions: dict, points: list, edges: list) -> dict:
186186
return point_scores
187187

188188

189-
def reshape_data(data: dict, points: list, size: int) -> np.ndarray:
189+
def _reshape_data(data: dict, points: list, size: int) -> np.ndarray:
190190
"""Shape the data so that it can be plotted easily.
191191
192192
Parameters
@@ -232,7 +232,7 @@ def __init__(
232232
self.strategy = strategy
233233
self.probe = probe
234234

235-
def construct_tournament_elements(
235+
def _construct_tournament_elements(
236236
self, step: float, progress_bar: bool = True
237237
) -> tuple:
238238
"""Build the elements required for a spatial tournament
@@ -258,9 +258,9 @@ def construct_tournament_elements(
258258
original player, the rest are the probes.
259259
260260
"""
261-
self.points = create_points(step, progress_bar=progress_bar)
262-
edges = create_edges(self.points, progress_bar=progress_bar)
263-
probe_players = create_probes(
261+
self.points = _create_points(step, progress_bar=progress_bar)
262+
edges = _create_edges(self.points, progress_bar=progress_bar)
263+
probe_players = _create_probes(
264264
self.probe, self.points, progress_bar=progress_bar
265265
)
266266

@@ -316,7 +316,7 @@ def fingerprint(
316316
if filename is None:
317317
temp_file_descriptor, filename = mkstemp() # type: ignore
318318

319-
edges, tourn_players = self.construct_tournament_elements(
319+
edges, tourn_players = self._construct_tournament_elements(
320320
step, progress_bar=progress_bar
321321
)
322322

@@ -340,7 +340,7 @@ def fingerprint(
340340
os.close(temp_file_descriptor)
341341
os.remove(filename)
342342

343-
self.data = generate_data(self.interactions, self.points, edges)
343+
self.data = _generate_data(self.interactions, self.points, edges)
344344
return self.data
345345

346346
def plot(
@@ -374,7 +374,7 @@ def plot(
374374
A heat plot of the results of the spatial tournament
375375
"""
376376
size = int((1 / self.step) // 1) + 1
377-
plotting_data = reshape_data(self.data, self.points, size)
377+
plotting_data = _reshape_data(self.data, self.points, size)
378378
fig, ax = plt.subplots()
379379
cax = ax.imshow(plotting_data, cmap=cmap, interpolation=interpolation)
380380

0 commit comments

Comments
 (0)