Using Cellular Automata and Climate Data to Generate Visual Art

After the recording of the first demo with my band Mundo Kumo where we create music inspired on per reviewed papers about climate change, it was time to create some of the videos that we will be using on our live show. My initial idea was to use computer generated art combined with real climate data. I had experience with coding and complex artificial intelligence algorithms however not much experience using artificial intelligence (AI) to generate art.

The code used here was developed using the python language and it can be downloaded freely on my github repository called AlgorithmicArt. Although this was the initial idea about the videos,  the idea changed a lot after and we are not using all the videos explained here. However they are still a really good starting point.

When doing a little bit of research on the topic i found that Cellular automata was probably one of the easiest way to start. The concept of cellular automata was created by the mathematician Stephen Wolfram and some of the rules are very similar to the majority vote model used widely on statistical mechanics.

The first intuitive step is the use of the one-dimensional cellular automata (the easiest to implement and understand) . Thus picking a cellular automaton rule, it is possible to create a picture similarly to the pictures found on the internet.

Frame248.jpeg

 

To make things a little bit more interesting, a video can be made showing the evolution of the cellular automata grouping sequential gifs. This can be done using different alternatives. For example using ubuntu and the imagemagick package:

convert -delay 20 -loop 0 *.jpg myimage.gif

The final video is a thing like this:

The second step is to add the climate information in the code and combine with the cellular automata. I used global temperature anomalies from NOAA. I maped the colors based on the range of temperature anomalies. Thus, the colors (global temperature anomalies) and the evolution of the cellular automata are connected:

Next post I will talk about two-dimensional cellular automata.

 

Think Python AND R and not just PYTHON OR R: Creating vectors


In this post I will talk a little bit about how python and R work with vectors. I am using python 2.7.13 and R 3.4.3, both 64-bit on a Ubuntu 16.04 and I am also using the free book called A Hands-On Introduction to Using Python in the Atmospheric and Oceanic Sciences by prof. Johnny Lin as a guide.

Creating vectors

The first thing about a vector is how to create one. There are several ways to create a vector in R. For example, if one needs to create a logical vector:

a = vector(mode = "logical", length = 5)
b = c(FALSE, FALSE, FALSE, FALSE, FALSE)
c = rep(FALSE,5)
## [1] FALSE FALSE FALSE FALSE FALSE
## [1] FALSE FALSE FALSE FALSE FALSE
## [1] FALSE FALSE FALSE FALSE FALSE

or a numerical vector:

a = vector(mode = "numeric", length = 5)
b = c(0, 0, 0, 0, 0)
c = rep(x=0, 5)
## [1] 0 0 0 0 0
## [1] 0 0 0 0 0
## [1] 0 0 0 0 0

The vector() function produces a vector of the given length and mode, the c() function is a generic function which combines its arguments and rep() function replicates the values in x also returning a vector.

In python we can use the numpy package which has lots of methods to create and manipulate arrays/vectors. Thus importing the package numpy and generating the same vectors in python:

import numpy as np
a = np.array([False,False,False,False])
b = np.full(4, False, bool)
print a 
print b
## [False False False False]
## [False False False False]

or a numerical vector:

import numpy as np
a = np.array([0,0,0,0])
b = np.full(4, 0, float)
print a
print b
## [0 0 0 0]
## [ 0.  0.  0.  0.]

Why are they different? Remember the dynamical typing? Vector a is a vector of integers and b is a vector of float. The attribute dtype gives the data-type of the array’s elements:

c = np.array([0.0,0.0,0.0,0.0])
print a.dtype  
print b.dtype
print c 
print c.dtype
## int64
## float64
## [ 0.  0.  0.  0.]
## float64

Indexing Vectors

One major difference between python and R is how they address the element in the vector. In python the element addresses start with zero, so the first element of vector a is a[0], the second is a[1], etc.

import numpy as np
d = np.array(range(1,5))
print d
print d[0], d[3] 
## [1 2 3 4]
## 1 4

In R the element addresses follows the ordinal value thus starting from one. Consequently the first element of vector a is a[1], the second is a[2], etc.

d = seq(4)
print(d)
cat(d[1], d[4], sep=" ")
## [1] 1 2 3 4
## 1 4

Be careful!!!!

Python and R have the same method range() but they do different things. In python range() returns a list containing an arithmetic progression of integers. range(i, j) returns ([i, i+1, i+2,\ldots , j-1]) and the default is i=0.

f = range(5)
g = range(2,5)
print f
print g
## [0, 1, 2, 3, 4]
## [2, 3, 4]

In R the methods similar to range() are seq(), seq_along(), seq_len() (please check the R documentation to see the differences between them) which generates regular sequences. However the default starting value is 1.

f = seq(5)
g = seq(2,5)
print(f)
print(g)
## [1] 1 2 3 4 5
## [1] 2 3 4 5

The method range() in R returns a vector containing the minimum and maximum of all the given arguments.

range(f)
range(5)
## [1] 1 5
## [1] 5 5

If you have any question, suggestion or opinion about this post please feel free to write a comment below.

Think Python AND R and not just PYTHON OR R: From NULL to String types


Continuing the saga of types in python and R, in this short post i will talk briefly about the NULL object (or NoneType in python) and how python and R handle the String type. I am using python 2.7.13 and R 3.3.3, both 64-bit on Ubuntu 16.04 and I am also using the free book called A Hands-On Introduction to Using Python in the Atmospheric and Oceanic Sciences by prof. Johnny Lin as a guide.

The NULL/None type

The NoneType in python and the NULL object in R are basically used to signify the absence of a value in many situations. It is often returned by expressions and functions whose value is undefined.  Because variables are dynamically typed, objects with value NULL can be changed by replacement operators and will be coerced to the type of the right-hand side. This object is also good to “safely” initialize a parameter, making sure to set the variable to a real value later.
For example, to initialize a variable to NULL (or None in python), and if later on the code tries to do an operation with the variable before the variable has been reassigned to a non-Null Type (or non-NoneType in python) variable, the interpreter will give an error.

In addition because NULL (None) is a special type (object) it has a relational operator to test if an object is NULL (or None). Using R:

a = NULL
a == NULL
is.null(a)
## logical(0)
## [1] TRUE

And using pyhton:

a = None
print a == None
print a is None
## True
## True

In python it is possible to use the common relational operator == but it is not recommended. One should use is instead of ==. More information about why here and an example here.

String variables

In python or R string variables (character vectors) are created by setting text in either paired single or double quotes.
Python uses the operator (+) to join strings together:

a = "Hello"
b = "World"
print a 
print b 
print a + b 
## Hello
## World
## HelloWorld

However R does not use the same operator. There are diffrent forms to concatenate strings in R and one of the simplest way is to use the functions paste() (by default includes a space caracther between the strings but this can be easily changed) or paste0().

paste(a,b)
paste0(a,b)
## [1] "Hello World"
## [1] "HelloWorld"

If you have any question, suggestion or opinion about this post please feel free to write a comment below.