-1

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)
apappy
  • 1
  • 1

2 Answers2

0

At the end of cellular_step, you returned a list which is then assigned to the variable value. Thus value is no longer a Numpy array and hence do not have the attribute shape.

lightalchemist
  • 10,031
  • 4
  • 47
  • 55
0

Why do you return[next_step] ?

Doing return next_step is a priori what you are looking for.


I defined 'values' as a Numpy array and if I run the code without the stepping portion, it stays as an array.

Actually not. See,

>>> type(cellular_step(value, rule=30))
<type 'list'>
keepAlive
  • 6,369
  • 5
  • 24
  • 39