How to create a movie?? (from matlab code) I figured the error , now how to stop the animation and put colormaps

���� 24/01/2012 01:31 ��, �/� Gousios George ������:

���� 23/01/2012 08:39 ��, �/� Tony Yu ������:

2012/1/23 Gousios George <ggeo1@…3830…>

          ���� 23/01/2012 08:10

��, �/� Benjamin Root ������:

2012/1/23 Gousios George <ggeo1@…3830…>

                      ����

23/01/2012 07:48 ��, �/� Tony Yu ������:

                              2012/1/23

Gousios George <ggeo1@…3830…>

                                  ���� 23/01/2012

06:52 ��, �/� Tony Yu ������:

                                          2012/1/23

Gousios George <ggeo1@…3830…>

                                              ����

21/01/2012 07:43 ��,
�/� Gousios George
������:

                                                ����

21/01/2012 07:05 ��,
�/� Tony Yu ������:

                                                    On

Sat, Jan 21,
2012 at 11:31
AM, Gousios
George <ggeo1@…3830…>
wrote:

                                                      ����

21/01/2012
04:54 ��, �/�
Tony Yu
������:

                                                      On

Sat, Jan 21,
2012 at 9:07
AM, Gousios
George <ggeo1@…3830…>
wrote:

                                                      Hello ,

i have the
following code
in matlab and
trying to do
it in

                                                      matplotlib.



                                                      I have this

code in matlab
(in a function
showGraphs):

                                                      ...

                                                      m =

size(myList,
3);

                                                      for k = 1:m

                                                      � � g =

myList(:, :,
k);

                                                      � � image(g +
                                                      � � axis off

                                                      � � axis

square

                                                      � � M(k) =

getframe;

                                                      end;



                                                      and in another

file (another
function):

                                                      ...

                                                      M =

showGraphs(grids)

                                                      movie(M, 1)







                                                      I did so far:



                                                      def

showGraphs(data):

                                                      � �

data=sc.array([data])

                                                      � �

n=sc.shape(data)[2]

                                                      � � for k in

range(n):

                                                      � � � �

mydata=data[:,:,k]

                                                      � � � �

#plt.imshow(mydata+1)
� �–>>
this doesn’t
work

                                                      Also ,in order

to do the
animation :

                                                      grids=...(result

from another
function)

result=showGraph(grids)

fig=plt.figure()

ani=animation.FuncAnimation(fig,result,interval=30,blit=True)

                                                      plt.show()



                                                      Right now the

program says
"TypeError:
‘NoneType’
object is not

                                                      callable" (it

has errors in
the animation
call)

                                                      What should be

my approach to
this in order
to have the
animation?

                                                      Thank you!
                                                      You're getting

that error
because the
second
argument to
FuncAnimation
(result in
your example)
should be a
function (not
always; see
example 2
linked below).
Right now, if
your
showGraphs
function is
defined in
full, it
returns a
value of None,
which gets
saved to
result
(hence the
error).

                                                      You should

take a look at
some of the
image
animation
examples (ex1, ex2).

                                                      -Tony

I did now :

                                                      def

showGraphs(data):

                                                      ���

data=sc.array([data])

                                                      ���

n=sc.shape(data)[2]

��� ims=[]

                                                      ��� for k in

range(n):

                                                      �������

mydata=data[:,:,k]

                                                      �������

im=plt.imshow(mydata+1)

                                                      �������

ims.append([im])

                                                      ��� return ims



                                                      and now it

gives me
"TypeError:
Invalid
dimensions for
image data.

                                                      Please post

short, but
executable
examples when
possible. I’m
not sure what
your data
looks like,
but your call
to sc.array
is strange
(I’m not sure
why you have
square
brackets,
which
effectively
adds an
unnecessary
dimension to
your data).

                                                      The code

attached below
should work.

                                                      Cheers,

                                                      -Tony



                                                      import numpy

as np

                                                      import

matplotlib.pyplot
as plt

                                                      from

matplotlib.animation
import
ArtistAnimation

                                                      fig =

plt.figure()

                                                      def

showGraphs(data):

                                                      ��� data =

np.asarray(data)

unnecessary

in this
example

                                                      ��� n =

np.shape(data)[2]

                                                      ��� ims = []

                                                      ��� #for

mydata in
np.rollaxis(data,
-1):

                                                      ��� for k in

range(n):

                                                      ������� mydata

= data[:, :,
k]

                                                      ������� im =

plt.imshow(mydata)

                                                      �������

ims.append([im])

                                                      ��� return ims



                                                      # 5 frames of

a random 20 x
20 image

                                                      data =

np.random.uniform(size=(20,
20, 5))

                                                      ims =

showGraphs(data)

                                                      ani =

ArtistAnimation(fig,
ims)

                                                      plt.show()
                                                Now,it gives me 2

figures (why 2?) but
empty.(maybe i
didn’t convert right
the matlab code
below?)

                                                The data in the

showGraphs function
is the result from
this function (in
matlab):

                                                .........

                                                grids = zeros(n, n,

t + 1);

                                                grids(:, :, 1) =

rest;

                                                for i = 2:(t + 1)

                                                ��� Extended =

extendLat1(rest);

                                                ��� rest =

applyExtended(Extended);

                                                ��� grids(:, :, i) =

rest;

                                                end;



                                                And i did this in

python:

                                                ��

grids=sc.zeros((area,area,t+1))

                                                ���

rest=grids[:,:,0]

                                                ��� for i in

range(1,t):

                                                �������

extended=extend(rest)

                                                �������

rest=apply_extend(extended)

                                                �������

grids[:,:,i]=rest

                                                ��� return grids



                                                Thanks for helping!

Any help?Thanks!

                                            Sorry, but could you

be a bit clearer:

                                            * Did the example I

provide work for you?

                                            * Assuming it did, are

you essentially
replacing data in my
example with grids in
this most recent
example?

                                            * I don't get 2 figures:

are you asking about the
code from my last
message?

                                            * Please provide a

minimal, executable
example

                                            -Tony

Ok,i 'll try!

                                  1) Yes,your example works fine.

                                  2) Yes,i return grids from another

function (work) doing :

                                  ��� grids=work(area,...,time)

                                  ��� result=showGraphs(grids)

                                  ���

ani=ArtistAnimation(fig,result)

                                  ��� plt.show()



                                  The contents of the work function

are shown above.

                                  I just want to make sure that i

have done the conversion from
matlab right.

                                  Maybe the problem is somewhere

else?

                                  Thank you.
                                Did you get 2 figures with my

original example, or just in your
modified code? If it’s only in your
modified code, then there’s
something you’re not showing which
is creating the extra figure.

                                Unfortunately, the `work` function

you showed in the previous message
is not executable (apply_extend,
extend, etc. aren’t defined),
which it makes it very difficult to
debug. If my code snippet worked for
you, then the only difference is in
how you’ve defined grids—do you
know how it’s different?

                                -Tony

                                �
                      Ok,i am sending you the whole code of the

program. (forget the names of the functions i
showed you so far , i use other names).

                      EMPTY=0������ #empty cell

                      TREE=1������� #tree cell

                      BURNING=2���� #burning cell

                      probTree=0.8� #probability of tree

                      probBurning=0.001 #a tree ignites with this

probability� even if no neighbor is burning

                      probLighting=0.0001� #chance that a tree been

hit from light

                      probResistBurn=0.3�� #chance that a tree does

not burn even if it’s neighbor burns

                      t=20

                      area=50





                      def spread(cell,N,E,S,W):

                      �

                      ��� if cell==EMPTY:

                      ������� return EMPTY

                      ��� elif cell==BURNING:

                      ������� return EMPTY

                      ��� else:

                      ������� if (N==BURNING or E==BURNING or

S==BURNING or W==BURNING):

                      ����������� if (sc.rand()<probResistBurn):

                      ��������������� return TREE

                      ����������� else:

                      ��������������� return BURNING

                      ������� elif (sc.rand()<probLighting

*(1-probResistBurn)):

                      ����������� return BURNING

                      ������� else:

                      ����������� return TREE

                      �������

                      def extend(lat):

                      ��� lat = sc.matrix(lat)

                      ��� extendRows= sc.vstack([lat[-1,:], lat,

lat[0,:]])

                      ��� extendLat=sc.hstack([extendRows[:,-1],

extendRows, extendRows[:,0]])

                      ��� return extendLat

                      ���

                      def apply_extend(matr):

                      ��� #matr = sc.matrix(matr)

                      ��� matr=sc.array([matr])

                      ��� area=sc.shape(matr)[0]

                      ��� final=sc.zeros((area,area))

                      ��� for i in range(1,area):

                      ������� for j in range(1,area):

                      ����������� cell=matr[i,j]

                      ����������� N=matr[i-1,j]

                      ����������� E=matr[i,j+1]

                      ����������� S=matr[i+1,j]

                      ����������� W=matr[i,j-1]

                      �����������

final[i-1,j-1]=spread(cell,N,E,S,W)

                      ��� return final

                      ���

                      def

fire(area,probTree,probBurning,probLighting,probResistBurn,t):

                      ���

                      ���

trees_or_burns=sc.rand(area,area)<probTree�

tree or a burning tree

                      ���

burns=trees_or_burns*(sc.rand(area,area)<probBurning)��
#� burning tree

                      ���

trees=trees_or_burns-burns�����������������������������

tree

                      ���

empties=sc.ones((area,area))-trees_or_burns������������

empty cell

                      �������

                      ��� forest=empties*EMPTY + trees*TREE +

burns*BURNING�� #the whole forest

                      ���

                      ��� grids=sc.zeros((area,area,t+1))

                      ��� forest=grids[:,:,0]

                      ��� for i in range(1,):

                      ������� extended_forest=extend(forest)

                      ������� forest=apply_extend(extended_forest)

                      ������� grids[:,:,i]=forest

                      ��� return grids

                      ���

                      def Graph(data):

                      ��� data=sc.array(data)

                      ��� n=sc.shape(data)[2]

                      ��� ims=[]

                      ��� for i in range(n):

                      ������� mydata=data[:,:,i]

                      ������� im=plt.imshow(mydata)


                        ������� ims.append([im])

                        ��� return ims

                        �����������

if name==“main”:

                      ���

grids=fire(area,probTree,probBurning,probLighting,probResistBurn,t)

                      ��� result=Graph(grids)

                      ��� fig=plt.figure()

                      ���

#ani=animation.FuncAnimation(fig,result,interval=30,blit=True,repeat_delay=1000)

                      ��� ani=ArtistAnimation(fig,result)

                      ��� plt.show()



                      Also, i am sending you the code in matlab� :
                    In Graph(), you call "plt.imshow()", which will

create a new figure automatically if one doesn’t
exist already.� Change fig=plt.figure() to
fig=plt.gcf()

                    Ben Root
          Ok,now it shows me only one figure but it's full of blue

color and doesn’t do anything.

          Thanks
        Actually, it's plotting your data correctly.� If you

print out the values of mydata in the Graph loop, you’ll
see that everything is zero.

        -Tony

You are right they are all zeros!I don’t unsderstand…

  I am not sure if the function extend� causes the problem.



  The extend function from matlab is :



  function extlat = extendLat1(lat);

  extendRows = [lat(end, :); lat; lat(1, :)];

  extlat = [extendRows(:, end) extendRows extendRows(:, 1)];



  I tested my version and i am sure that it gives the right

results.But here,in matlab it has “lat” as argument . In python i
create “lat = sc.matrix(lat)” .I think this is right??

  Also , the function apply_extend in matlab is:



  function newmat = applyExtended(matExt);

  n = size(matExt, 1) - 2;

  newmat = zeros(n);���

  for i = 2:(n + 1)

  ��� for j = 2:(n + 1)

  ������� site = matExt(i, j);

  ������� N = matExt(i - 1, j);

  ������� E = matExt(i, j + 1);

  ������� S = matExt(i + 1, j);

  ������� W = matExt(i, j - 1);

  ������� newmat(i - 1, j - 1) = spread(site, N, E, S, W);

  ��� end;

  end;



  I think i have done it right.



  Also ,the function fire in matlab :



  function grids = fire(n, probTree, probBurning, chanceLightning,

chanceImmune, t);

  global EMPTY TREE BURNING probLightning probImmune

  EMPTY = 0; TREE = 1; BURNING = 2;

  probLightning = chanceLightning;

  probImmune = chanceImmune;



  treesOrBurns = (rand(n) < probTree);



  burns = treesOrBurns .* (rand(n) < probBurning);



  trees = treesOrBurns - burns;



  empties = 1 - treesOrBurns;



  forest =� empties * EMPTY + trees * TREE + burns * BURNING;



  grids = zeros(n, n, t + 1);

  grids(:, :, 1) = forest;

  for i = 2:(t + 1)

  ��� forestExtended = extendLat1(forest);

  ��� forest = applyExtended(forestExtended);

  ��� grids(:, :, i) = forest;

  end;





  Could someone figure where am i doing wrong?(i have the whole code

of mine above).

  Thanks!
Ok, i figured the error! It was at the "forest=grids[:,:,0]".It

should be “grids[:,:,0]=forest”.

Now i want to ask 2 questions.

1) How to stop the animation??It should stop when the simulation

ends but now it starts all over again.

2) How to use colormaps ,because i didn't understand from [here](http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps)    �

or here .

And how can i put specific colors for specific values.For example

for my value “EMPTY->color white”,TREE->color green.

Thank you!