Elegant SciPy
by Juan Nunez-Iglesias , Stfan van der Walt , and Harriet Dashnow
Copyright 2017 Juan Nunez-Iglesias, Stfan van der Walt, and Harriet Dashnow. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com/safari). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Editor: Nan Barber
- Production Editor: Melanie Yarbrough
- Copyeditor: Christina Edwards
- Proofreader: Rachel Monaghan
- Indexer: Judy McConville
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Rebecca Demarest
- August 2017: First Edition
Revision History for the First Edition
- 2017-08-10: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781491922873 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Elegant SciPy, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-491-92287-3
[LSI]
Preface
Unlike the stereotypical wedding dress, it wasto use a technical termelegant, like a computer algorithm that achieves an impressive outcome with just a few lines of code.
Graeme Simsion, The Rosie Effect
Welcome to Elegant SciPy. Were going to spend rather a lot of time focusing on the SciPy bit of the title, so lets take a moment to reflect on the Elegant bit. There are plenty of manuals, tutorials, and documentation websites out there that describe the SciPy library. Elegant SciPy goes further. More than just teaching you how to write code that works, we will inspire you to write code that rocks!
In The Rosie Effect (hilarious book; go read its prequel The Rosie Project when youre done with Elegant SciPy), Graeme Simsion twists the conventions of the word elegant around. Most would use it to describe the visual simplicity, style, and grace of, say, the first iPhone. Instead Graeme Simsions hero, Don Tillman, uses a computer algorithm to define elegance. We hope that you will understand exactly what he means after reading this book; that you will read or write a piece of elegant code, and feel calmed in the glow of its beauty and grace. (Note: The authors may be prone to hyperbole.)
A good piece of code just feels right. When you look at it, its intent is clear, it is often concise (but not so concise as to be obscure), and it is efficient at executing the task at hand. For the authors, the joy of analyzing elegant code lies in the lessons hidden within, and the way it inspires us to be creative in how we approach new coding problems.
Ironically, creativity can also tempt us to show off cleverness at the expense of the reader, and write obtuse code that is hard to understand. PEP8 (the Python style guide) and PEP20 (the Zen of Python) remind us that code is read much more often than it is written and therefore readability counts.
The conciseness of elegant code comes through abstraction and the judicious use of functions, not just through packing in a bunch of nested function calls. It may take a minute or two to grok, but it should ultimately provide a crisp, ah-ha! moment of understanding. Once you know the various components of the code, its correctness should be obvious. This can be aided by clear variable and function names, and carefully crafted comments that explain the code, rather than merely describe it.
In the New York Times, software engineer J. Bradford Hipps recently argued that to write better code, [one should] read Virginia Woolf:
As a practice, software development is far more creative than algorithmic.
The developer stands before her source code editor in the same way the author confronts the blank page. [] They may also share a healthy impatience for the ways things have always been done and a generative desire to break conventions. When the module is finished or the pages complete, their quality is judged against many of the same standards: elegance, concision, cohesion; the discovery of symmetries where none were seen to exist. Yes, even beauty.
This is the position we take in this book.
Now that weve dealt with the elegant part of the title, lets come back to the SciPy.
Depending on context, SciPy can mean a software library, an ecosystem, or a community. Part of what makes SciPy great is that it has excellent online documentation and tutorials, rendering Just Another Reference book pointless; instead, Elegant SciPy wants to present the best code built with SciPy.
The code we have chosen highlights clever, elegant uses of advanced features of NumPy, SciPy, and related libraries. The beginning reader will learn to apply these libraries to real-world problems using beautiful code. And we use real scientific data to motivate our examples.
Like SciPy itself, we wanted Elegant SciPy to be driven by the community. Weve taken many of our examples from working code found in the wider scientific Python ecosystem, selecting them for their illustration of the principles of elegant code we outlined above.
Who Is This Book For?
Elegant SciPy is intended to inspire you to take your Python to the next level. You will learn SciPy by example, from the very best code.
Before starting, you should at least have seen Python, and know about variables, functions, loops, and maybe a bit of NumPy. You might have even honed your Python skills with advanced material, such as Fluent Python. If this doesnt describe you, you should start with some beginner Python tutorials, such as Software Carpentry, before continuing with this book.
But perhaps you dont know whether the SciPy stack is a library or a menu item from the International House of Pancakes, and you arent sure about best practices. Perhaps you are a scientist who has read some Python tutorials online, and have downloaded some analysis scripts from another lab or a previous member of your own lab, and have fiddled with them. And you might think that you are more or less alone when you learn to code SciPy. You are not.
As we progress, we will teach you how to use the internet as your reference. And we will point you to the mailing lists, repositories, and conferences where you will meet like-minded scientists who are a little further in their journey than you.
This is a book that you will read once, but may return to for inspiration (and maybe to admire some elegant code snippets!).
Why SciPy?
The NumPy and SciPy libraries make up the core of the Scientific Python ecosystem. The SciPy software library implements a set of functions for processing scientific data, such as statistics, signal processing, image processing, and function optimization. SciPy is built on top of NumPy, the Python numerical array computation library. Building on NumPy and SciPy, an entire ecosystem of apps and libraries has grown dramatically over the past few years, spanning a broad spectrum of disciplines that includes astronomy, biology, meteorology and climate science, and materials science, among others.