Prey-Predator population evolution¶

A modern stack for scientist, python style¶

It's like scientific community using numerical tools is split in two category : the one that have all the nice toys (and do some data-science and stuff), and the other. I'm in the other category, but I like funny toys. That's why I will present you an overview of modern (and nice) tools that you can use in order to solve non-data-science related problems.

Someone ask me about using Bokeh to have a real-time plotting when solving coupled ordinary differential equation. He asks me about the Lokta-Volterra problem. Long story short, this is about modeling the relation between the prey and predator populations. These can be complex problems, but we will stick to the simple case. The hypothesis are:

1. The prey population finds ample food at all times.
2. The food supply of the predator population depends entirely on the size of the prey population.
3. The rate of change of population is proportional to its size.
4. During the process, the environment does not change in favour of one species and genetic adaptation is inconsequential.
5. Predators have limitless appetite.

(Thank you, Wikipedia)

This can be modeled with two couples ODE :

$$\begin{cases} \frac{\partial x}{\partial t} = \alpha\,x - \beta\,x\,y\\ \frac{\partial y}{\partial t} = \gamma\,x\,y - \sigma\,y \end{cases}$$

Where $x$ and $y$ are the prey and the predator population. The other parameters have to be chosen wisely. I'm more lazy than wise, so I will just use some data from the Wikipedia article. ¯\(ツ)

What I want to show you

1. how to solve an coupled ODE with the new scipy api solve_ivp
2. how to use xarray, holoviews and streamz to plot and save the results during the resolution.

These libs are modern (yet not always stable?) and nice tools : xarray provide a data structure which is a kind of pandas Dataframe for gridded data, Holoviews a high level plotting abstraction, rely on matplotlib (static) or bokeh (interactive) backend and provide streaming abilities, and finally streamz give tools to process data via a pipeline of functions.

I will not describe these tools, just demonstrate how we can use them to have an effective computation + vizu + storage pipeline when solving an ODE.

Let's start ! First, the imports.

In [1]:
import functools as ft

import pylab as pl
import numpy as np
from scipy.integrate import solve_ivp

import xarray as xr
import holoviews as hv
from holoviews.streams import Buffer
import streamz

# custom pylab style
pl.style.use("publication")

%matplotlib inline


We have to activate the notebook integration from Holoviews. Here, we use the bokeh backend, but matplotlib is fine too.

In [2]:
hv.notebook_extension("bokeh")