I'm attempting to create a one-dimensional cellular automata. For a given rule (between 1 and 255), the number is converted into the corresponding 8-digit binary number and paired to all 8 possible combinations of 0's and 1's.
The code seems to work fine outside of putting it in a function. However, when I run the second half, it gives me the AttributeError: 'list' object has no attribute 'shape'. I defined 'values' as a Numpy array and if I run the code without the stepping portion, it stays as an array. I am unsure what is going on. Any guidance would be appreciated!
def cellular_step(value, rule=110):
blist = np.unpackbits(np.uint8(rule))
truple = [(0,0,0),(0,0,1),(0,1,0),(0,1,1),(1,0,0),(1,0,1),(1,1,0),(1,1,1)]
lookup_dict = {}
for indx,lmr in enumerate(truple):
lookup_dict[lmr] = blist[indx]
next_step = np.array([],dtype=np.int8)
for i in range(value.shape[0]):
indices = range(i-1,i+2)
neighbourhood = value.take(indices,mode='wrap')
b = tuple(neighbourhood)
a = lookup_dict.get(b)
next_step = np.append(next_step,a)
return[next_step]
value = np.zeros(5, dtype=np.int8)
value[ len(value)//2 ] = 1
nsteps = 10
grid = np.ndarray( [nsteps,len(value)], dtype=np.int8)
for n in range(nsteps):
value = cellular_step(value,rule=30)
grid[n,:] = value
plt.imshow(grid)