Introducing Spyder, the Scientific PYthon Development EnviRonment
If you want to use Anaconda for science projects, one of the first things to consider is the spyder package, which is included in the basic Anaconda installation. Spyder is short for Scientific PYthon Development EnviRonment. Think of it as an IDE for scientific programming within Python.
You'll probably want the latest version available, because it's under fairly constant development. You can be sure your entire Anaconda installation is up to date with the command:
conda update anaconda
There are two ways to launch spyder. If you're using the Anaconda Navigator, you simply can click the spyder icon.
Figure 1. You can start spyder directly from the Anaconda Navigator.
If you have a terminal window open, you can launch spyder simply
spyder and pressing enter. You may get a pop-up window saying
that spyder is not the latest version, which is just because the version
within Anaconda is a few revisions behind.
Once you start spyder, you should see an open editor window on the left-hand side and a Python console window on the lower right-hand side.
Figure 2. Starting spyder gives you an empty editor window to begin your first project.
The upper right-hand side is used for a help browser, a variable explorer and a file explorer. Like most IDEs, you can change which panes are visible and their layout within the window.
You can begin working with spyder immediately in the console window. The new default in spyder is to provide an IPython console that you can use to interact with the Python engine directly. It works, essentially, the same way that it works on the command line. The big difference is that spyder can inspect the contents of the Python engine and can do things like display variables and their contents within the variable explorer.
Figure 3. You can interact directly with the IPython console.
Although this is fine for smaller code snippets, you'll likely end up working on much larger chunks of code. In that case, you can use the editor to write functions and larger pieces. In order to execute this Python code, you can click the green arrow icon, click the menu item Run→Run, or press the F5 key. Again, the results are available from within the variable explorer. If instead you click the blue arrow icon (or click the menu item Debug→Debug), your code will be run within the IPython debugger, which lets you step through your code one line at a time.
Figure 4. Spyder includes a front end, allowing you to interact with ipdb, the IPython debugger.
You can gain more control over the debugging by adding breakpoints to your code. To do so, double-click the left-hand gutter in the editor pane. You should see a dot added for each breakpoint you insert.
Several tools are available for working on code and algorithm quality. You'll probably want to start with a static code analysis. Run it by clicking the "Source→Run static code analysis" menu item or by pressing F8. This will run the analysis and will provide the results in a new pane that will pop up in the top right-hand pane.
Figure 5. You can run a static code analysis to check for syntactic errors.
The results are categorized into convention breaks, refactoring suggestions, syntax warnings and actual errors in your code. This will catch the most obvious errors.
Once you have code that actually works, the next step is to check that code's performance. Spyder includes a front end that gives you access to the profiler included in the standard Python library. Start it by clicking the Run→Profile menu item or by pressing F10. Once it finishes, a new pane will appear in the same upper-left-hand location.
Figure 6. When you run the profiler, you'll get a display of how much time is being used in each function.
Unfortunately, the default profiler goes down only to the function level, and that may not be fine enough in detail. If that's the case, you can dive into one of the great features of spyder, its plugin architecture. Several plugins are already included within the Anaconda repository. Use the following command to install the line profiler plugin:
conda install -c spyder-ide spyder-line-profiler
Next you can add the function decorator
@profile to any functions you
want to explore, and then start the line profiler either by clicking
the Run→Profile line by line menu item or by pressing
see the results in a new output pane.
Figure 7. You can use the line profiler to see how efficient functions are in more detail.
You can look at how much time is spent on each line, both per hit and the total for the complete program run. This way, you can focus on the most costly parts of your code to get better performance.
Along with optimizing time, the other parameter you'll want to consider optimizing is memory usage. This is becoming much more important as more and more research is focusing on big data problems. In those cases, use the following command to install the memory profiler plugin for spyder:
conda install -c spyder-ide spyder-memory-profiler
Once the plugin is installed, you can add the decorator
as with the line profiler. Start the memory profiler by
clicking "Run→Profile memory line by line" or by pressing
Ctrl-Shift-F10. Another pane will appear in the top right-hand
side where you can see how memory usage changes after each line
of code. This will allow you to figure out which lines of code are being
wasteful and where to focus your brain power for improving performance.
Figure 8. There is a memory profiler plugin for spyder that allows you to figure out how to optimize memory usage.
For scientific computing, the last item I want to look at is the ability to visualize data. Humans often can make intuitive leaps by being able to see how data looks. The default setting for spyder is that graphs are drawn inline within the IPython console.
Figure 9. By default, spyder allows you to generate plots within the IPython console.
This is fine for a quick glance at the data, but it isn't the easiest to look at. If you click Tools→Preferences, you'll see a new window where you can change this behavior and have plots show up in a different window instead.
Figure 10. You can change the preferences on how plots are generated and displayed.
If you rerun the code, you'll now get the plot in a new window. This allows for the ability to play with the plot display and even save off the final image. If you change the settings around plotting in the preferences, you may need to restart the IPython engine to pick up the new preferences.
Figure 11. Generating plots in their own window allows for more interaction.
And, that should be enough to get started using spyder in your computational science problems. In my next article, I plan to look at the version of Jupyter that is included with Anaconda and see how it can be used effectively. Both tools are good, but they fit within slightly different ecological niches.
Limited Time Offer
Take Linux Journal for a test drive. Download our September issue for FREE.
Topic of the Week
The cloud has become synonymous with all things data storage. It additionally equates to the many web-centric services accessing that same back-end data storage, but the term also has evolved to mean so much more.