Chapter 1: The Scientific Python Ecosystem¶
Part I — Scientific Python for Researchers
Learning Objectives¶
By the end of this chapter you will be able to:
- Set up a reproducible development environment (conda, VS Code, Jupyter)
- Understand the role of each core library: NumPy, Pandas, Matplotlib, GeoPandas
- Manage packages and environments for reproducible science
# Standard imports — add chapter-specific imports below
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
From Click to Script: A New Perspective¶
You are likely accustomed to using word processors, spreadsheets, and GIS software. You might believe it is always easier to click a button than to write code. We usually refer to code used for automating a task as a script; therefore, we will use this term to clarify that this is not a book about software engineering. This is a book for non-programmers who realize the importance of Python in spatial data science.
Imagine you are working with a spreadsheet that includes country names, continents, population, and GDP (Gross Domestic Product). As showed in the figure below.
Suppose you need to calculate the total population and the world's GDP. How many clicks would that take?
- Step 1: Click on the spreadsheet file to open it in a program such as Excel or LibreOffice Calc.
- Step 2: Click on an empty cell and type the '=' symbol, followed by the SUM function.
- Step 3: Click and drag to select the entire column of data, from the very first row to the bottom. It will be necessary to scroll through hundreds of rows, for example, ranging from A1 to A1000.
- Step 4: Type the closing parenthesis and press Enter.
Then, repeat the entire process for the GDP column.
All of these steps and clicks can be replaced by the code below.
import geopandas as gpd
# The URL for the Natural Earth dataset
url = "https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip"
# Loading the data with specific columns
world = gpd.read_file(url, columns=['NAME', 'CONTINENT', 'POP_EST', 'GDP_MD', 'geometry'])
# Calculating and printing the total population instantly
print (world.POP_EST.sum())
7654092021.3
Then, simply type the following line to calculate the total world GDP:
print (world.GDP_MD.sum())
87344872
Now that we have loaded the spatial data using GeoPandas, I will give you a sneak peek at what Python can do:
world.plot(column='GDP_MD', legend=True, figsize=(10, 5))
<Axes: >
We can easily repeat the process for the population data:
# Now, let's visualize population instead of GDP
world.plot(column='POP_EST', legend=True, figsize=(10, 5))
<Axes: >
Keep in mind
Don't worry if you didn't understand everything yet; we will cover each detail throughout the book.
Given that we have a continent column, how can I calculate the total population per continent and create a bar chart? It is easy to imagine that this would require many clicks in a spreadsheet program, but in Python, we only need one line of code, as shown below.
# Group by continent, sum the population, and plot a bar chart—all in one go!
world.groupby('CONTINENT')['POP_EST'].sum().plot(kind='bar', title='Population by Continent')
<Axes: title={'center': 'Population by Continent'}, xlabel='CONTINENT'>
Then, we just add one more line for the GDP:
world.groupby('CONTINENT')['GDP_MD'].sum().plot(kind='bar', title='GDP by Continent')
<Axes: title={'center': 'GDP by Continent'}, xlabel='CONTINENT'>
It's up to you!
Using your preferred spreadsheet program, download the dataset and create bar charts for both population and GDP.
You likely already know that computers are the best tool for automating repetitive tasks, but I hope you will soon realize that this is best achieved by writing scripts. Therefore, we need a different mindset—one that many authors refer to as 'computational thinking'. Computational Thinking is the thought processes involved in formulating a problem and its solution(s) so that the solutions are represented in a form that can be effectively carried out by an information-processing agent.
Computational Thinking
"Computational thinking is a fundamental skill for everyone, not just for computer scientists." — Jeannette Wing (2006)
Computational thinking helps solve problems across different disciplines and is incredibly valuable for data scientists. It is built upon four main pillars: decomposition, pattern recognition, abstraction, and algorithmic thinking.
- Decomposition: Breaking down a complex problem into smaller, manageable parts.
- Pattern Recognition: Identifying trends or similarities within or among problems.
- Abstraction: Focusing on the important information only, ignoring irrelevant details.
- Algorithmic Thinking: Developing a step-by-step solution to the problem.
To see these pillars in action, let's look at a practical problem: Calculating the percentage of the world's GDP contributed by each country.
Decomposition¶
Instead of trying to solve the whole problem at once, we divide it into smaller, manageable tasks:
- Calculate the total world GDP by summing the values for all countries.
- For each specific country, divide its individual GDP by the total world GDP.
- Multiply the result by 100 to get the percentage.
Pattern Recognition¶
We notice a recurring pattern: the calculation for one country is identical to the calculation for any other. We can apply a single expression to all rows:
$$ \text{GDP Percentage} = \left( \frac{\text{Country GDP}}{\text{Total World GDP}} \right) \times 100 $$
Abstraction¶
Abstraction refers to filtering out unnecessary details to focus on the essential attributes of a system. In our case, it means treating a complex nation simply as a GDP value. Through abstraction, we realize that we are not just looking at countries and economies; we are dealing with a standard percentage calculation. This allows us to use existing tools and functions in Python that were designed for this exact purpose.
Algorithms¶
What is an Algorithm?
"An algorithm is a finite method, written in a fixed vocabulary, governed by precise instructions, moving in discrete steps, 1, 2, 3, ..., whose execution requires no insight, cleverness, intuition, intelligence, or perspicuity, and that sooner or later comes to an end."
— David Berlinski (2000)
This definition captures a crucial characteristic: the intelligence lies in the construction of the algorithm, not in its execution. The algorithm is a way to represent and share knowledge about a specific problem so that it can be executed by a computer and understood by anyone who speaks the language. A computer can execute the algorithm, and any person familiar with the language can understand it.
The following section presents, at a high level, the steps to calculate the percentage of the total GDP for each country. We will show how these steps are translated into the Python programming language using the Pandas library. Don’t worry about the technical details of the code yet; focus on the algorithmic concept. Notice how we can perform all these operations with just a few lines of code.
Step 1: Load the input data
# Using the Natural Earth dataset we loaded earlier
import geopandas as gpd
url = "https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip"
world = gpd.read_file(url)
Step 2: Calculate the total world GDP
total_gdp = world['GDP_MD'].sum()
Step 3: Calculate the GDP percentage for each country
world['GDP_Share'] = (world['GDP_MD'] / total_gdp) * 100
Step 4: Display the results
world.set_index("NAME")['GDP_Share'].sort_values(ascending=False)
NAME
United States of America 24.538620
China 16.421002
Japan 5.818051
Germany 4.420549
India 3.284599
...
Vanuatu 0.001069
W. Sahara 0.001038
Antarctica 0.001028
Falkland Is. 0.000323
Fr. S. Antarctic Lands 0.000018
Name: GDP_Share, Length: 177, dtype: float64
By looking at these four steps, we can see Berlinski’s definition in action. The intelligence of this process lies in the construction of the algorithm—the logical sequence we designed—rather than in its execution. Once the instructions are written in the fixed vocabulary of Python and Pandas, the computer simply follows them to reach the final result. In this sense, an algorithm is a way to represent and share our knowledge about a problem in a format that both humans and machines can understand.
The Intelligence is Yours
As Berlinski noted, the execution of an algorithm requires no "insight" or "cleverness" from the computer. The true intelligence belongs to you, the author. By breaking the problem down and writing these steps, you have transformed your knowledge into a reusable tool that any computer can run.
Setting Up Your Geospatial Lab¶
The Foundations: Python and the Jupyter Philosophy¶
"Python is a high-level, interpreted, and general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python is renowned for its clear and readable syntax, which simplifies both learning and writing code (LUTZ, 2013). Its design philosophy emphasizes code legibility, allowing programmers to express concepts in fewer lines of code compared to languages like C++ or Java."
Did you know?
The name "Python" isn't actually about the snake! It’s a tribute to the British comedy group *Monty Python's Flying Circus*. Guido van Rossum is a big fan of their 1970s BBC show.
Jupyter and Literate Programming
"While software developers often use IDEs (Integrated Development Environments), many data analysts prefer Jupyter Notebooks. This is an interactive computing environment that allows users to create and share documents containing live code, visualizations, and narrative text.
Jupyter Notebooks promote the concept of Literate Programming, a paradigm introduced by Donald Knuth in 1984. The core idea is that a program should be written to be understandable by both humans and computers. By integrating code with documentation and explanations in a narrative format, Literate Programming makes code more accessible, transparent, and reproducible."
To help you get started, I have organized this chapter into three distinct paths. If you want to start coding immediately without installing anything, the Cloud Path is for you. If you prefer to work on your own machine, the Local Path offers two choices: a user-friendly visual installer (Anaconda) or a professional setup using the Terminal. No matter which path you choose, the end result will be the same: a powerful environment ready to process geospatial data. Select the one that best fits your current needs; you can always explore the others later.
The Cloud Path: Google Colab¶
(The "Fast Track")
Google Colab, short for Colaboratory, is a free, browser-based notebook environment hosted by Google. Nothing gets installed on your own machine: Colab already ships with numpy, pandas, matplotlib, and most of the everyday scientific Python stack, and a plain pip install handles anything it doesn't already have — including this book's geopandas, rasterio, and, later on, dissmodel itself. All it asks of you is a Google account and an internet connection.
To try this chapter without installing anything, open its companion notebook directly in Colab:
Open Chapter 1 in Google Colab →
The first time you run a cell, Colab will prompt you to save your own copy (File → Save a copy in Drive) — do that, and feel free to experiment freely from there; nothing you change touches the original.
The Local Path: Professional Environments¶
Working locally means installing Python and this book's libraries on your own machine — a slower start, but the setup you would actually use for a real research project, and the one Chapter 5's Git, GitHub, and pytest workflow assumes throughout. Two ways to get there:
The Guided Experience (Anaconda)¶
Best for: Windows users and those who prefer a visual interface (GUI)
Anaconda is a Python distribution built specifically for data science and scientific computing, and installing it gives you three things at once: conda, a package and environment manager well-suited to tricky binary dependencies (a genuine advantage for this book, since both geopandas and rasterio lean on the GDAL and PROJ C libraries underneath); Jupyter Notebook, already installed and ready to launch; and Anaconda Navigator, a graphical interface for managing packages and environments without ever opening a terminal. Download it from anaconda.com — DataCamp's installation walkthrough is a solid reference if you hit a platform-specific snag.
The Developer's Choice (Terminal & Virtual Environments)¶
Best for: Linux/macOS users and those seeking total control over their setup
The alternative skips Anaconda entirely: a plain Python installation, pip, and the standard library's own venv module for keeping one project's packages separate from another's. It asks a little more of you up front — no GUI, commands instead of menus — but it's the leaner, more transparent setup, and the one this book's own dissmodel package is built and tested against. Chapter 5 walks through it command by command, from python -m venv venv all the way to your first git push.
# Run this cell to confirm your environment is ready for Chapter 2
import sys
print("Python version:", sys.version)
import numpy, pandas, matplotlib
print("numpy, pandas, and matplotlib all imported successfully — you're ready for Chapter 2.")
Python version: 3.12.3 (main, Nov 6 2025, 13:44:16) [GCC 13.3.0] numpy, pandas, and matplotlib all imported successfully — you're ready for Chapter 2.
Package Management with conda and pip¶
Both paths above end up installing packages — the only question is which tool does the installing. pip, Python's own package installer, and conda, Anaconda's package and environment manager, solve the same basic problem in two different ways.
pip installs from the Python Package Index (PyPI) and only ever manages Python packages: if something like rasterio needs a compiled C library underneath — GDAL, in that case — pip expects it to already be on your system, or bundles a pre-built copy directly into the package it downloads. conda installs from its own repositories (Anaconda's defaults, or the community-maintained conda-forge) and manages non-Python dependencies right alongside the Python package itself — which is exactly why GDAL-based geospatial libraries are sometimes noticeably easier to get running with
conda install -c conda-forge geopandas rasterio
than with pip install geopandas rasterio on a machine where GDAL isn't already configured correctly.
conda also creates isolated environments — self-contained installations with their own package versions, so a dependency one project needs never silently breaks another:
conda create -n geobook python=3.11
conda activate geobook
This is the same idea as venv from the Developer's Choice path above, and Chapter 5 puts that alternative to work in full, end to end. Between here and the end of Part I, either tool gets you everything you need; starting in Chapter 5, this book standardizes on venv and pip, matching the installation instructions of the dissmodel package itself. If you started out with Anaconda, that's no obstacle later on — pip install <package> inside an active conda environment works exactly the same way any later chapter's instructions expect.
Core Libraries at a Glance¶
Every tool this book relies on is open source, built by and for the scientific Python community, and installable through either path above. Think of this as a map of the stack rather than a tutorial — each library gets its own proper introduction the first time it actually matters:
| Library | What it's for | First appears in |
|---|---|---|
numpy |
Fast, array-based numerical computing | Chapter 2 |
pandas |
Tabular data — spreadsheets, CSVs, database-style tables | Chapter 3 |
matplotlib / seaborn |
Static charts and plots | Chapter 4 |
geopandas |
pandas, extended with geometry — the vector data workhorse |
Chapter 7 |
rasterio |
Reading, writing, and manipulating raster (grid) data | Chapter 8 |
xarray |
Labeled, multidimensional arrays — think NetCDF and climate data | Chapter 9 |
libpysal |
Spatial weights and spatial statistics | Chapter 10 |
salabim |
Discrete-event simulation | Chapter 19 |
dissmodel |
This book's own framework for discrete spatial modeling | Chapter 21 |
Notice the progression: the first three rows are general-purpose data tools you'd reach for with any dataset, spatial or not. Everything from geopandas on is where "geospatial" and "modeling" — the two words in this book's title — start to mean something specific.
Jupyter Notebooks and VS Code¶
Whichever path you chose, you'll spend most of this book inside a Jupyter notebook — this very chapter is one. A notebook is built from cells, each either a code cell (Python, run with Shift+Enter) or a markdown cell (formatted text, exactly like the paragraph you're reading now). Code cells run in the order you run them, not automatically top to bottom, and a variable defined in one cell stays available to every cell after it for as long as the notebook's kernel keeps running — which is exactly why editing an earlier cell and forgetting to re-run it is one of the most common sources of confusing errors for beginners. When a notebook starts behaving strangely, Kernel → Restart & Run All is the reliable fix: it clears every variable and re-executes the whole notebook from the top, giving you the exact state a fresh reader would see.
If you took the Local Path, Visual Studio Code is worth installing alongside Python: its free Jupyter extension opens .ipynb files with the same cell-by-cell workflow as Colab, but adds a full code editor's features on top — autocomplete, inline documentation, an integrated terminal, and the Git tooling Chapter 5 relies on — all in one window, instead of switching between a browser tab and a separate terminal window.
Summary¶
Key concepts introduced¶
- Scripts versus point-and-click tools, and why writing code — not just running it once — is what makes an analysis repeatable
- Literate programming and the notebook format: code, output, and narrative text living in one document
- Three ways to get a working Python environment: Google Colab (cloud, zero install), Anaconda (guided, GUI-based), or a plain Python installation with
venv(lean, terminal-based) pipversuscondaas two different package managers, and whyconda-forgeis often the smoother path specifically for GDAL-dependent geospatial libraries- A map of the libraries this book builds on, from
numpyandpandashere in Part I throughdissmodelitself in Part III
From here, Chapter 2 assumes you have a working notebook — Colab, Anaconda, or your own terminal setup — open and ready to run code.
Further Reading¶
- Mark Lutz, Learning Python (O'Reilly, 2013) — the standard, comprehensive reference for the language itself
- Jeannette M. Wing, "Computational Thinking," Communications of the ACM 49(3), 2006 — the paper that gave the term its modern meaning
- Project Jupyter documentation, The Jupyter Notebook: https://jupyter-notebook.readthedocs.io/
- Anaconda documentation, Getting started: https://docs.anaconda.com/getting-started/
- Google Colab, official introductory notebook: https://colab.research.google.com/notebooks/intro.ipynb