Scientific Computing with Python on Raspberry Pi

Scientific Computing with Python on Raspberry Pi

Translation organized by Python Tribe (python.freelycode.com), no reproduction allowed, sharing is welcome.

This article is a guide on how to install a Python scientific computing stack suitable for geospatial analysis on Raspberry Pi 3. The whole process takes only a few minutes.

The Raspberry Pi 3 was announced two weeks ago with substantial improvements in computing power over previous versions. It can function as a fully operational Wi-Fi connected Linux desktop, albeit with limited capabilities. However, it is fully capable of running a Python scientific computing stack, including Jupyter, pandas, matplotlib, scipy, scikit-learn, and OSMnx.

Despite (or because of?) its low power consumption, it is an ideal choice for researchers and engineers facing low-overhead and repetitive tasks, including geocoding, web scraping, periodic API calls, or repetitive statistical or spatial analysis (based on small datasets). It is also a great way to set up a simple server or experiment using Linux. This guide is aimed at beginners in the Raspberry Pi and Linux world, who are interested in setting up a Python environment on these $35, credit card-sized computers. We will cover everything you need to do before you get started (if your Pi is already up and running, feel free to skip steps 1 and 2).

Step 1: Hardware Preparation

Assuming you have a usable phone charger, HDMI cable, mouse, and keyboard, you will only need to spend no more than $45 to acquire the other necessary items. Here’s what you need:

1. A Raspberry Pi ($35)

2. A 5V 1A power supply (I am using an old Android charger, a micro-USB cable, about $5)

3. A micro SD card and a standard SD card adapter (about $9)

4. An HDMI cable to connect to your display (I already have one, about $5)

5. USB mouse/keyboard (I already have a set, about $15)

6. Optional: If you have used the Raspberry Pi on your desktop computer, you can use a cheap USB switch to toggle the mouse/keyboard between the computer and Raspberry Pi.

Step 2: Install Raspbian Operating System on Raspberry Pi

Now we will install Raspbian (a Debian Linux operating system customized for Raspberry Pi), then boot the operating system and connect to Wi-Fi.

1. Insert your micro SD card into the SD adapter slot and place it into your computer.

2. Download the NOOBS installer for Raspbian and extract it to your desktop.

3. Download SDFormatter and install it (this tool is particularly useful if you end up needing to refresh the Raspberry Pi system, as handling Linux partitions on a desktop computer can be tricky)

4. Open SDFormatter, select the SD card drive, and click format

5. After formatting is complete, copy the NOOBS files from the desktop to the SD card

6. Remove the SD card from the adapter slot and insert it into the Raspberry Pi

7. Connect the mouse, keyboard, HDMI, and power to the Raspberry Pi

8. After NOOBS boots, select your language, choose Raspbian, and click install

Once the installation is complete, click OK, and the new operating system will boot. The Raspberry Pi 3 has Wi-Fi capability: click the Wi-Fi network panel item in the top right corner of the screen and select your network to connect.

Step 3: Update Packages

Next, we update the existing software. Open a terminal window and run the following commands, one at a time. The first line retrieves the updated package list from the repository, then the second line fetches the latest versions of the installed packages. The last two lines list the installed system packages and Python packages and dump them to a file for reference.

Scientific Computing with Python on Raspberry Pi

apt-get is the Debian tool for installing and updating packages. We will use it to install packages instead of pip, as these packages are precompiled, meaning they only take a few seconds instead of minutes to install. When a Python package cannot be installed using apt-get, we will use pip to install (and compile) it.

Step 4: Install Python Basic Tools

As mentioned in previous steps, the Raspberry Pi comes with several Python packages already installed. We still need to download some packages to complement the basic environment configuration. In the terminal window, run the following commands:

Scientific Computing with Python on Raspberry Pi

build-essential is necessary for building Debian packages; python-dev, python-distlib, and python-setuptools provide several Python development and packaging tools; python-pip and python-wheel are used to install Python packages; libzmq-dev is for Jupyter notebooks; libgdal-dev is used for spatial analysis.

Step 5: Install pandas Dependencies

pandas has several recommended and optional dependencies that can unlock features or enhance performance. To install them, run the following two commands:

Scientific Computing with Python on Raspberry Pi

The first command uses apt-get to install the available recommended dependencies, and the second command uses pip to install the two dependencies that are not available in the apt repository.

Step 6: Install Python Scientific Computing Stack

Fortunately, we can use apt-get to install the entire large, complex Python scientific computing stack without needing to compile everything. This makes the process faster.

Scientific Computing with Python on Raspberry Pi

If you need specific versions of these packages, or want to have newer versions than those in the Debian repository, you can use pip to install them, but be prepared to face a slow compilation process.

Step 7 (Optional): Install Other Useful Packages

We are all done! However, if you want to choose to install more useful packages, run the following two commands:

Scientific Computing with Python on Raspberry Pi

requests provides an elegant interface for making HTTP requests, pil provides Python imaging capabilities, scrapy is a web scraping framework, geopy provides geocoding and distance measurement capabilities, shapely allows for a large number of 2D geometric operations, and pyproj provides mapping transformation capabilities. In the second command, jupyter allows for interactive coding, geopandas spatializes pandas, and OSMnx lets you use OpenStreetMap street networks.

Conclusion

Our Python scientific stack is now available on Raspberry Pi. Start Jupyter, load some data with pandas, or draw maps using basemap. Due to the memory limitations of the Raspberry Pi, you cannot load huge datasets, but everything else runs quite well. It is especially suitable for repetitive, customized, or low-overhead tasks such as geocoding and web scraping.

Original English text: http://geoffboeing.com/2016/03/scientific-python-raspberry-pi/ Translator: wkl123

Leave a Comment