-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
116 lines (85 loc) · 2.91 KB
/
utils.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
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
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 10 16:13:21 2024
Author: Alexandros Stratoudakis
e-mail: [email protected]
"""
import numpy as np
def is_iter(arr: object) -> bool:
"""
Function that returns True if the input is an iterable and false otherwise.
Args:
arr (object): Input array
Returns:
bool: True if arr is iterable, False if not.
"""
try:
# noinspection PyTypeChecker
iter(arr)
except:
return False
else:
return True
def pad_to_shape(arr: np.ndarray, new_shape: tuple) -> np.ndarray:
"""
Function that uses numpy.pad to pad with zeros around the input arr to a given shape.
Args:
arr (np.ndarray): Input numpy.ndarray.
new_shape (tuple): The input array will be padded to this shape.
Returns:
padded_arr (np.ndarray): The reshaped, padded array.
"""
pad_height_1 = int(np.ceil((new_shape[0] - arr.shape[0]) / 2))
pad_height_2 = int(np.floor((new_shape[0] - arr.shape[0]) / 2))
pad_width_1 = int(np.ceil((new_shape[1] - arr.shape[1]) / 2))
pad_width_2 = int(np.floor((new_shape[1] - arr.shape[1]) / 2))
padded_arr = np.pad(arr, pad_width=((pad_height_1, pad_height_2),
(pad_width_1, pad_width_2)), mode='constant', constant_values=0)
return padded_arr
def pidx(idx: int, max_idx: int) -> int:
"""
Function that treats periodic indices. When the input index exceeds the maximum,
it is mapped to the appropriate value, starting from the beginning.
Args:
idx (int): Input index.
max_idx (int): Maximum allowed index.
Returns:
int: Index, respecting periodicity.
"""
if idx <= max_idx:
return idx
else:
return idx - max_idx - 1
class Seeds:
"""A class containing various initial seeds"""
# --------Periodic--------
blinker = [[1, 1, 1]]
toad = np.array([[0, 1, 1, 1],
[1, 1, 1, 0]])
beacon = np.array([[1, 1, 0, 0],
[1, 1, 0, 0],
[0, 0, 1, 1],
[0, 0, 1, 1]])
# --------Spaceships--------
glider = np.array([
[0, 1, 0],
[0, 0, 1],
[1, 1, 1]
])
lwss = np.array([[1, 0, 0, 1, 0], # Lightweight spaceship
[0, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[0, 1, 1, 1, 1]
]
)
# --------Methuselahs--------
r_pentomino = np.array([[0, 1, 1],
[1, 1, 0],
[0, 1, 0]])
die_hard = np.array([[0, 0, 0, 0, 0, 0, 1, 0],
[1, 1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 1, 1]])
acorn = np.array([[0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[1, 1, 0, 0, 1, 1, 1]
])