-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.py
More file actions
121 lines (95 loc) · 3.45 KB
/
utils.py
File metadata and controls
121 lines (95 loc) · 3.45 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import math
import numpy as np
import random
import json, jsonlines, datetime, string
from collections import Counter
def answer2jsonl(answers, model_outputs, questions, out_file):
# Confirm we have answers for all questions
assert len(answers) == len(questions)
outputs = []
for q_idx in range(len(answers)):
output = {"question": questions[q_idx]["question"], "answer": questions[q_idx]["answer"], "prediction": answers[q_idx], "explanation": model_outputs[q_idx]}
outputs.append(output)
with jsonlines.open(out_file, mode='w') as fout:
fout.write_all(outputs)
def read_jsonl(in_file):
questions = []
with open(in_file) as fin:
for line in fin:
question = json.loads(line)
questions.append(question)
return questions
class Vec2D(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Vec2D(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Vec2D(self.x - other.x, self.y - other.y)
def __mul__(self, other):
return self.x*other.x + self.y*other.y
def __abs__(self):
return math.sqrt(self.x**2 + self.y**2)
def __eq__(self, other):
return np.allclose(self.x, other.x) and \
np.allclose(self.y, other.y)
def __str__(self):
return '(%g, %g)' % (self.x, self.y)
def __ne__(self, other):
return not self.__eq__(other) # reuse __eq__
class Vec3D(object):
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __add__(self, other):
return Vec3D(self.x + other.x, self.y + other.y, self.z + other.z)
def __sub__(self, other):
return Vec3D(self.x - other.x, self.y - other.y, self.z - other.z)
def __mul__(self, other):
return self.x*other.x + self.y*other.y + self.z * other.z
def __abs__(self):
return math.sqrt(self.x**2 + self.y**2 + self.z**2)
def __eq__(self, other):
return np.allclose(self.x, other.x) and \
np.allclose(self.y, other.y) and \
np.allclose(self.z, other.z)
def __str__(self):
return '(%g, %g, %g)' % (self.x, self.y, self.z)
def __ne__(self, other):
return not self.__eq__(other) # reuse __eq__
class HexGrid():
def __init__(self, radius):
deltas = [[1,0,-1],[0,1,-1],[-1,1,0],[-1,0,1],[0,-1,1],[1,-1,0]]
self.radius = radius
self.tiles = {(0, 0, 0): "X"}
for r in range(radius + 1):
a = 0
b = -r
c = +r
for j in range(6):
num_of_hexas_in_edge = r
for i in range(num_of_hexas_in_edge):
a = a+deltas[j][0]
b = b+deltas[j][1]
c = c+deltas[j][2]
self.tiles[a,b,c] = "X"
def show(self):
l = []
for y in range(20):
l.append([])
for x in range(60):
l[y].append(".")
for (a,b,c), tile in self.tiles.items():
l[self.radius-1-b][a-c+(2*(self.radius-1))] = self.tiles[a,b,c]
mapString = ""
for y in range(len(l)):
for x in range(len(l[y])):
mapString += l[y][x]
mapString += "\n"
print(mapString)
if __name__ == "__main__":
pos = Vec2D(1,2) + Vec2D(2,3)
pos2 = Vec3D(1,2,3) + Vec3D(2,3,1)
gg = HexGrid(radius=1)