-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
49 lines (41 loc) · 1.26 KB
/
helper.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
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 26 14:07:33 2022
"""
import numpy as np
import matplotlib.pyplot as plt
import json
from json import JSONEncoder
from matplotlib import colors
cmap = colors.ListedColormap(['black', 'blue', 'red', 'green', 'yellow', 'grey', 'pink', 'orange', 'cyan', 'darkred'])
def printGrid(ax, data):
ax.imshow(data, cmap=cmap, norm=colors.Normalize(vmin=0, vmax=9))
ax.grid(which='major', axis='both', color='w', linewidth=1)
ax.set_xticks(np.arange(-.5, len(data[0]), 1));
ax.set_yticks(np.arange(-.5, len(data), 1));
ax.set_yticklabels([])
ax.set_xticklabels([])
def getSize(grid):
return f'{len(grid)}x{len(grid[0])}'
def displayIO(grids):
plt.rcParams['figure.figsize'] = [20, 5]
fig, axs = plt.subplots(1,len(grids));
fig.figsize=(12, 1)
for i, grid in enumerate(grids):
axs[i].set_title(f'{grid[1]}');
printGrid(axs[i], grid[0]);
plt.show()
class HitchHikersEncoder(JSONEncoder):
"""
Used as follows:
import json
json.dumps(g, indent=4, cls=HitchHikersEncoder)
"""
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
if isinstance(obj, np.floating):
return float(obj)
if isinstance(obj, np.ndarray):
return obj.tolist()
return obj.__dict__