-
Notifications
You must be signed in to change notification settings - Fork 268
/
Copy pathload_data_.py
79 lines (67 loc) · 2.15 KB
/
load_data_.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import pathlib
import pkgutil
from typing import Callable, Dict, List, Optional, Tuple
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,
get_data: Callable[[str, str], Optional[bytes]] = pkgutil.get_data,
) -> List[List[str]]:
"""Loads a data file stored in the Axelrod library's data subdirectory,
likely for parameters for a strategy."""
path = str(pathlib.Path(directory) / filename)
data_bytes = get_data(__name__, path)
if data_bytes is None:
raise FileNotFoundError(f"Some loader issue for path {path}")
data = data_bytes.decode("UTF-8", "replace")
rows = []
for line in data.split("\n"):
if line.startswith("#") or len(line) == 0:
continue
s = line.split(", ")
rows.append(s)
return rows
def load_weights(
filename: str = "ann_weights.csv", directory: str = "data"
) -> Dict[str, Tuple[int, int, List[float]]]:
"""Load Neural Network Weights."""
rows = load_file(filename, directory)
d = dict()
for row in rows:
name = str(row[0])
num_features = int(row[1])
num_hidden = int(row[2])
weights = list(map(float, row[3:]))
d[name] = (num_features, num_hidden, weights)
return d
def load_pso_tables(filename="pso_gambler.csv", directory="data"):
"""Load lookup tables."""
rows = load_file(filename, directory)
d = dict()
for row in rows:
(
name,
a,
b,
c,
) = (
str(row[0]),
int(row[1]),
int(row[2]),
int(row[3]),
)
values = list(map(float, row[4:]))
d[(name, int(a), int(b), int(c))] = values
return d