Python Tools for Scientists

Book description

Python Tools for Scientists introduces you to the most popular coding tools for scientific research, such as Anaconda, Spyder, Jupyter Notebooks, and JupyterLab, as well as dozens of important Python libraries for working with data, including NumPy, matplotlib, and pandas. No prior programming experience is required.

You'll set up a professional programming environment, receive a crash course on programming with Python, and tour the many tools and libraries available for working with data, creating visualizations, simulating natural events, and more. In the book's applied projects, you'll use these tools to write programs that perform tasks like simulating globular star clusters, building ships for a wargame simulator, creating an interactive science slideshow, and classifying animal species.

You'll learn:

•The best way to set up your computer for science and engineering work with Python
•The basics of Python programming, including the language's syntax and best practices
•The purpose of dozens of Python's most popular scientific libraries, with deep dives into NumPy, matplotlib, seaborn, pandas, and scikit-learn
•How to choose the best plotting library for your needs

Even established scientists sometimes struggle to implement Python at work, partly because so many choices are available. This book guides you through the ecosystem of Python's libraries and tools, so you can find the ones best suited to your needs. Regardless of your field of study, Python Tools for Scientists is an indispensable owner's manual for setting up and using your computer for science.

Table of contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. Dedication
  5. About the Author
  6. About the Technical Reviewer
  7. BRIEF CONTENTS
  8. CONTENTS IN DETAIL
  9. ACKNOWLEDGMENTS
  10. INTRODUCTION
    1. Why Python?
    2. Navigating This Book
      1. Part I: Setting Up Your Scientific Coding Environment
      2. Part II: A Python Primer
      3. Part III: The Anaconda Ecosystem
      4. Part IV: The Essential Libraries
      5. Appendix
    3. Updates and Errata
    4. Leaving Reviews
  11. PART I: SETTING UP YOUR SCIENTIFIC CODING ENVIRONMENT
  12. 1 INSTALLING AND LAUNCHING ANACONDA
    1. About Anaconda
    2. Installing Anaconda on Windows
    3. Installing Anaconda on macOS
    4. Installing Anaconda on Linux
    5. Getting to Know Anaconda Navigator
      1. Launching Navigator
      2. The Home Tab
      3. The Environments Tab
      4. The Learning Tab
      5. The Community Tab
      6. File Menu
    6. Summary
  13. 2 KEEPING ORGANIZED WITH CONDA ENVIRONMENTS
    1. Understanding Conda Environments
    2. Working with Conda Environments Using Navigator
      1. Launching Navigator
      2. Creating a New Environment
      3. Managing Packages
      4. Duplicating Environments
      5. Backing Up Environments
      6. Removing Environments
    3. Working with Conda Environments Using the Command Line Interface
      1. Launching the Command Line Interface
      2. Creating a New Environment
      3. Specifying an Environment’s Location
      4. Managing Packages
      5. Duplicating and Sharing Environments
      6. Restoring Environments
      7. Removing Environments
      8. Cleaning the Package Cache
    4. Summary
  14. 3 SIMPLE SCRIPTING IN THE JUPYTER QT CONSOLE
    1. Installing seaborn
    2. Installing and Launching the Jupyter Qt Console Using Navigator
    3. Installing and Launching the Jupyter Qt Console Using the CLI
    4. The Qt Console Controls
      1. Choosing a Syntax Style
      2. Using Keyboard Shortcuts
      3. Using Tabs and Kernels
      4. Printing and Saving
      5. Multiline Editing
    5. Summary
  15. 4 SERIOUS SCRIPTING WITH SPYDER
    1. Installing and Launching Spyder with Anaconda Navigator
    2. Installing and Launching Spyder Using the CLI
    3. Launching Spyder from the Start Menu
    4. Configuring the Spyder Interface
    5. Using Spyder with Environments and Packages
      1. The Naive Approach
      2. The Modular Approach
    6. Using Project Files and Folders
      1. Creating a Project in a New Directory
      2. Creating a Project in an Existing Directory
      3. Using the Project Pane
    7. The Help Pane
    8. The IPython Console
      1. Using the Console for Output and Plotting
      2. Using Kernels with the Console
      3. Clearing the Namespace
      4. The History Pane
      5. Special Consoles
    9. The Editor Pane
      1. Writing a Program Using the Editor
      2. Defining Code Cells
      3. Setting the Run Configuration
      4. Autocompleting Text
    10. The Code Analysis Pane
    11. The Variable Explorer Pane
    12. The Profiler Pane
    13. The Debugger Pane
    14. Summary
  16. 5 JUPYTER NOTEBOOK: AN INTERACTIVE JOURNAL FOR COMPUTATIONAL RESEARCH
    1. Installing Jupyter Notebook
      1. The Naive Approach
      2. The Modular Approach
    2. Your First Jupyter Notebook
      1. Creating Dedicated Project Folders
      2. Navigating the Notebook Dashboard and User Interface
      3. Naming a Notebook
      4. Adding Text with a Markdown Cell
      5. Adding Code and Making Plots with a Code Cell
      6. Working with Output Cells
      7. Adding an Image with a Markdown Cell
      8. Saving the Notebook
      9. Closing the Notebook
    3. Getting Help
      1. Keyboard Shortcuts
      2. The Command Palette
    4. Using Notebook Extensions
      1. Installing Extensions
      2. Enabling Extensions
    5. Working with Widgets
      1. Installing ipywidgets
      2. Creating Widgets with Interact
      3. Creating Widgets with Interactive
      4. Manually Creating Widgets
      5. Handling Events
      6. Customizing Widgets
      7. Embedding Widgets in Other Formats
    6. Sharing Notebooks
      1. Checking and Running Notebooks with the Kernel Menu
      2. Downloading Notebooks
      3. Sharing Notebooks via GitHub and Gist
      4. Sharing Notebooks via Jupyter Notebook Viewer
      5. Sharing Notebooks via Binder
      6. Other Sharing Options
      7. Trusting Notebooks
    7. Turning Notebooks into Slideshows
      1. Installing the RISE Extension
      2. Creating a Slideshow
      3. Using Speaker Notes
    8. Summary
  17. 6 JUPYTERLAB: YOUR CENTER FOR SCIENCE
    1. When to Use JupyterLab Instead of Notebook?
    2. Installing JupyterLab
      1. The Naive Approach
      2. The Modular Approach
    3. Building a 3D Astronomical Simulation
      1. Using Dedicated Project Folders
      2. The JupyterLab Interface
      3. The Menu Bar
      4. The Left Sidebar
      5. Creating a New Notebook
      6. Naming the Notebook
      7. Using Markdown Cells
      8. Adding Code and Making Plots
      9. Adding a Console
      10. Displaying an Image File
      11. Exploring the Simulation
      12. Opening Multiple Notebooks
      13. Saving the Workspace
      14. Clearing the Workspace
      15. Closing the Workspace
    4. Taking Advantage of the JupyterLab Interface
      1. Creating Synchronized Views
      2. Copying Cells Between Notebooks
      3. Staying Focused by Using Single Document Mode
    5. Using the Text Editor
      1. Running a Script in a Terminal
      2. Running a Script in a Notebook
      3. Simultaneously Writing and Documenting Code
    6. Using JupyterLab Extensions
      1. Installing and Managing Extensions with the Extension Manager
      2. Installing and Managing Extensions Using the CLI
      3. Installing ipywidgets for JupyterLab
      4. Creating Custom Extensions
    7. Sharing
    8. Summary
  18. PART II: A PYTHON PRIMER
  19. 7 INTEGERS, FLOATS, AND STRINGS
    1. Mathematical Expressions
      1. Mathematical Operators
      2. The Assignment Operator
      3. Augmented Assignment Operators
      4. Precedence
      5. The math Module
    2. Error Messages
    3. Data Types
      1. Accessing the Data Type
      2. Integers
      3. Floats
      4. Strings
    4. Summary
  20. 8 VARIABLES
    1. Variables Have Identities
    2. Assigning Variables
      1. Using Expressions
      2. Operator Overloading
      3. Using Functions
      4. Chained Assignment and Internment
      5. Using f-Strings
    3. Naming Variables
      1. Reserved Keywords
      2. Variables Are Case Sensitive
      3. Best Practices for Naming Variables
      4. Managing Dynamic Typing Issues
      5. Handling Insignificant Variables
    4. Getting User Input
    5. Using Comparison Operators
    6. Summary
  21. 9 THE CONTAINER DATA TYPES
    1. Tuples
      1. Creating Tuples
      2. Converting Other Types to Tuples
      3. Working with Tuples
    2. Lists
      1. Creating Lists
      2. Working with Lists
    3. Sets
      1. Creating Sets
      2. Working with Sets
      3. Creating Frozensets
    4. Dictionaries
      1. Creating Dictionaries
      2. Combining Two Sequences into a Dictionary
      3. Creating Empty Dictionaries and Values
      4. Working with Dictionaries
    5. Summary
  22. 10 FLOW CONTROL
    1. The if Statement
      1. Working with Code Blocks
      2. Using the else and elif Clauses
      3. Using Ternary Expressions
      4. Using Boolean Operators
    2. Loops
      1. The while Statement
      2. The for Statement
      3. Loop Control Statements
      4. Replacing Loops with Comprehensions
    3. Handling Exceptions
      1. Using try and except
      2. Forcing Exceptions with the raise Keyword
      3. Ignoring Errors
    4. Tracing Execution with Logging
    5. Summary
  23. 11 FUNCTIONS AND MODULES
    1. Defining Functions
      1. Using Parameters and Arguments
      2. Positional and Keyword Arguments
      3. Using Default Values
      4. Returning Values
      5. Naming Functions
      6. Built-in Functions
    2. Functions and the Flow of Execution
      1. Using Namespaces and Scopes
      2. Using Global Variables
      3. Using a main() Function
    3. Advanced Function Topics
      1. Recursion
      2. Designing Functions
      3. Lambda Functions
      4. Generators
    4. Modules
      1. Importing Modules
      2. Inspecting Modules
      3. Writing Your Own Modules
      4. Naming Modules
      5. Writing Modules That Work in Stand-Alone Mode
      6. Built-in Modules
    5. Summary
  24. 12 FILES AND FOLDERS
    1. Creating a New Spyder Project
    2. Working with Directory Paths
      1. The Operating System Module
      2. Absolute vs. Relative Paths
      3. The pathlib Module
      4. The Shell Utilities Module
    3. Working with Text Files
      1. Reading a Text File
      2. Closing Files Using the with Statement
      3. Writing to a Text File
      4. Reading and Writing Text Files Using pathlib
    4. Working with Complex Data
      1. Pickling Data
      2. Shelving Pickled Data
      3. Storing Data with JSON
      4. Catching Exceptions When Opening Files
      5. Other Storage Solutions
    5. Summary
  25. 13 OBJECT-ORIENTED PROGRAMMING
    1. When to Use OOP
    2. Creating a New Spyder Project
    3. Defining the Frigate Class
      1. Defining Instance Methods
      2. Instantiating Objects and Calling Instance Methods
    4. Defining a Guided-Missile Frigate Class Using Inheritance
      1. Instantiating a New Guided-Missile Frigate Object
      2. Using the super() Function for Inheritance
    5. Objects Within Objects: Defining the Fleet Class
    6. Reducing Code Redundancy with Dataclasses
      1. Using Decorators
      2. Defining the Ship Class
      3. Identifying Friend or Foe with Fields and Post-Init Processing
      4. Optimizing Dataclasses with __slots__
    7. Making a Class Module
    8. Summary
  26. 14 DOCUMENTING YOUR WORK
    1. Comments
      1. Single-Line Comments
      2. Multiline Comments
      3. Inline Comments
      4. Commenting-Out Code
    2. Docstrings
      1. Documenting Modules
      2. Documenting Classes
      3. Documenting Functions and Methods
      4. Keeping Docstrings Up to Date with doctest
      5. Checking Docstrings in the Spyder Code Analysis Pane
    3. Summary
  27. PART III: THE ANACONDA ECOSYSTEM
  28. 15 THE SCIENTIFIC LIBRARIES
    1. The SciPy Stack
      1. NumPy
      2. SciPy
      3. SymPy
      4. pandas
    2. A General Machine Learning Library: scikit-learn
    3. The Deep Learning Frameworks
      1. TensorFlow
      2. Keras
      3. PyTorch
    4. The Computer Vision Libraries
      1. OpenCV
      2. scikit-image
      3. PIL/Pillow
    5. The Natural Language Processing Libraries
      1. NLTK
      2. spaCy
    6. The Helper Libraries
      1. Requests
      2. Beautiful Soup
      3. Regex
      4. Dask
    7. Summary
  29. 16 THE INFOVIS, SCIVIS, AND DASHBOARDING LIBRARIES
    1. InfoVis and SciVis Libraries
      1. Matplotlib
      2. seaborn
      3. The pandas Plotting API
      4. Altair
      5. Bokeh
      6. Plotly
      7. HoloViews
      8. Datashader
      9. Mayavi and ParaView
    2. Dashboards
      1. Dash
      2. Streamlit
      3. Voilà
      4. Panel
    3. Choosing a Plotting Library
      1. Size of Dataset
      2. Types of Plots
      3. Format
      4. Versatility
      5. Maturity
      6. Making the Final Choice
    4. Summary
  30. 17 THE GEOVIS LIBRARIES
    1. The Geospatial Libraries
      1. GeoPandas
      2. Cartopy
      3. Geoplot
      4. Plotly
      5. folium
      6. ipyleaflet
      7. GeoViews: The HoloViz Approach
      8. KeplerGL
      9. pydeck
      10. Bokeh
    2. Choosing a GeoVis Library
    3. Summary
  31. PART IV: THE ESSENTIAL LIBRARIES
  32. 18 NUMPY: NUMERICAL PYTHON
    1. Introducing the Array
      1. Describing Arrays Using Dimension and Shape
      2. Creating Arrays
      3. Accessing Array Attributes
      4. Indexing and Slicing Arrays
    2. Manipulating Arrays
      1. Shaping and Transposing
      2. Joining Arrays
      3. Splitting Arrays
    3. Doing Math Using Arrays
      1. Vectorization
      2. Broadcasting
      3. The Matrix Dot Product
      4. Incrementing and Decrementing Arrays
      5. Using NumPy Functions
    4. Reading and Writing Array Data
    5. Summary
  33. 19 DEMYSTIFYING MATPLOTLIB
    1. Anatomy of a Plot
    2. The pyplot and Object-Oriented Approaches
    3. Using the pyplot Approach
      1. Creating and Manipulating Plots with pyplot Methods
      2. Working with Subplots
      3. Building Multipanel Displays Using GridSpec
    4. Using the Object-Oriented Style
      1. Creating and Manipulating Plots with the Object-Oriented Style
      2. Working with Subplots
      3. Building Multipanel Displays Using GridSpec
      4. Insetting Plots
      5. Plotting in 3D
      6. Animating Plots
    5. Styling Plots
      1. Changing Runtime Configuration Parameters
      2. Creating and Using a Style File
      3. Applying Style Sheets
    6. Summary
  34. 20 PANDAS, SEABORN, AND SCIKIT-LEARN
    1. Introducing the pandas Series and DataFrame
      1. The Series Data Structure
      2. The DataFrame Data Structure
    2. The Palmer Penguins Project
      1. The Project Outline
      2. Setting Up the Project
      3. Importing Packages and Setting Up the Display
      4. Loading the Dataset
      5. Displaying the DataFrame and Renaming Columns
      6. Checking for Duplicates
      7. Handling Missing Values
      8. Exploring the Dataset
      9. Predicting Penguin Species Using K-Nearest Neighbors
    3. Summary
  35. 21 MANAGING DATES AND TIMES WITH PYTHON AND PANDAS
    1. Python datetime Module
      1. Getting the Current Date and Time
      2. Assigning Timestamps and Calculating Time Delta
      3. Formatting Dates and Times
      4. Converting Strings to Dates and Times
      5. Plotting with datetime Objects
      6. Creating Naive vs. Aware Objects
    2. Time Series and Date Functionality with pandas
      1. Parsing Time Series Information
      2. Creating Date Ranges
      3. Creating Periods
      4. Creating Time Deltas
      5. Shifting Dates with Offsets
      6. Indexing and Slicing Time Series
      7. Resampling Time Series
    3. Summary
  36. APPENDIX ANSWERS TO THE “TEST YOUR KNOWLEDGE” CHALLENGES
  37. INDEX

Product information

  • Title: Python Tools for Scientists
  • Author(s): Lee Vaughan
  • Release date: January 2023
  • Publisher(s): No Starch Press
  • ISBN: 9781718502666