Book Image

SciPy Recipes

By : V Kishore Ayyadevara, Ruben Oliva Ramos
Book Image

SciPy Recipes

By: V Kishore Ayyadevara, Ruben Oliva Ramos

Overview of this book

With the SciPy Stack, you get the power to effectively process, manipulate, and visualize your data using the popular Python language. Utilizing SciPy correctly can sometimes be a very tricky proposition. This book provides the right techniques so you can use SciPy to perform different data science tasks with ease. This book includes hands-on recipes for using the different components of the SciPy Stack such as NumPy, SciPy, matplotlib, and pandas, among others. You will use these libraries to solve real-world problems in linear algebra, numerical analysis, data visualization, and much more. The recipes included in the book will ensure you get a practical understanding not only of how a particular feature in SciPy Stack works, but also of its application to real-world problems. The independent nature of the recipes also ensure that you can pick up any one and learn about a particular feature of SciPy without reading through the other recipes, thus making the book a very handy and useful guide.
Table of Contents (11 chapters)

Running SciPy in PyCharm

For projects of moderate to large size, using a full-fledged Integrated Development Environment (IDE) is essential for efficient code production. An IDE might look, at first, like an imposing tool due to its complexity. A typical modern IDE integrates tools for editing, project management, building and running code, debugging, profiling, packaging for distribution, and integration with version control systems. As with any sophisticated tool, there is a learning curve associated with mastering a new IDE. The gains in productivity in the software development process, however, easily outweigh the time spent learning how to use an IDE.

Getting started

PyCharm is an IDE developed by JetBrains, providing a well-rounded collection of tools designed with Python development in mind. To experiment with PyCharm, go to the following address: https://www.jetbrains.com/pycharm/.

Click on Download Now, and you will be given a choice between the Professional Version and the Community Version. Choose the one that best suits you. The Community Version has limited capabilities but can be used for free. The Professional Version requires the purchase of a license.

After the download has finished, run the downloaded installer and follow the instructions.

How to do it...

Start PyCharm according to the directions for your operating system. Depending on the version of PyCharm you are running, you may be asked to enter licensing information. After that, the startup window will show up on your screen, as shown in the following screenshot:

PyCharm requires the definition of a Project containing all files related to a particular development project. This may seem like overkill if we just want to run a single script, but PyCharm's project structure is very flexible. It does not require the setting up of a main entry point, and allows for several running configurations. This way, it is easy to run and debug individual scripts independently.

Click on Create New Project on the startup window, and the New Project window will pop up, as shown in the following screenshot:

Select the following options in the New Project window:

In the Project type pane at the left, select Pure Python.

In the Location field, enter the directory where you want the project to be saved. You can use the button at the right, labeled with ..., to choose the directory visually. If the directory does not exist, it will be created for you. In this example, we are using pycharm-test as the directory for our project. This will also be the name of the project in PyCharm.

Choose the Python interpreter for the project. For the purposes of this book, this should be the interpreter in the Python environment that was installed according to the instructions given previously in this chapter.

It is very important that you choose the correct environment for the project you are starting. Besides using the chosen interpreter for running code, some key features of PyCharm, such as Intellisense code completion, use this information to analyze your code. In particular, if you are using a virtual environment, make sure you select the Python interpreter corresponding to the correct environment.

Click the Create button, and the project creation process will begin.

If this is the first time you are running PyCharm, you may notice that, at the bottom right of the PyCharm window, there is an indicator stating that there are processes running in the background:

This indicator appears because PyCharm is indexing the Python packages currently in your environment. This index is used by several PyCharm components, including Intellisense, syntax highlighting, and syntax checker. The indexing process may take up to a few minutes, depending on how fast your computer is and how many packages are installed in the Python tree. You can continue to use PyCharm during the indexing process, but you may observe degraded responsiveness. Everything should go back to normal once the indexing process is finished.

Let's now take a brief tour of the main PyCharm window, shown in the following screenshot:

Right now, no files are being edited, and the right pane does not show any code. The left pane is currently showing the Project Navigator, from which all project components can be easily accessed. Right now, the project has no files.

Click on the arrow to the left of External Libraries. This gives you access to the Python tree corresponding to the Python interpreter that is being used in this project. Notice that the interpreter itself is listed right at the top of the list. You can use this pane to navigate through the Python installation, and open the source of any installed module in the editor .

Do not edit the source of any module in the Python distribution unless you know what you are doing. This information should be considered read-only in most cases. Changing the source of an installed module may break it, in which case you will probably need to scrap your whole Python tree and reinstall it.

Another useful component of the PyCharm window is the tool menu, represented by a little box on the extreme bottom left of the screen. Hover your mouse pointer over the box, and the menu shown in the following screenshot will be displayed:

This menu contains shortcuts to frequently used tools. If you work a lot with PyCharm, this menu is one of the best time savers offered by the IDE.

Let's now create some code and run it. Right-click on the project name, pycharm-test, on the Project Navigator and select New from the context menu. Then select Python File in the submenu that pops up. Enter the name of the file in the appropriate field, without the .py extension. Click OK and the file will be created and opened in the Editor pane.

Enter the following code in the pycharm-test file. To get a feel for PyCharm's coding features, it is recommended that you type the code yourself, instead of copying and pasting it to the Editor window:

def collatz_sequence(n):
collatz_seq = [n]
while n != 1:
if n % 2 == 0:
n //= 2
else:
n = 3 * n + 1
collatz_seq.append(n)
return collatz_seq


while True:
n_input = input("Enter starting value (enter 0 to exit): ")
try:
n = int(n_input)
except ValueError:
print('Invalid integer: {}'.format(n_input))
continue
if n < 0:
print('n must be positive')
if n == 0:
break
cseq = collatz_sequence(n)
print('Collatz sequence for n={}:\n{}'.format(n, cseq))

print('Thanks for playing, I hope you had fun.')

Notice the following as the code is typed:

  • PyCharm will offer suggestions as you type the code. PyCharm analyzes your code on the fly, and searches the libraries and code you have already written for sensible suggestions as to what you are trying to type. To accept a suggestion, simply press Enter.
  • If there are several alternatives, you can move up and down the list of suggestions with the arrow keys.
  • Syntax errors are highlighted as they appear, and you can correct them before running the code.
  • Suspicious code is also indicated. For example, if you define a variable or import and do not use it, PyCharm will warn you.

This code lets the user experiment with the famous Collatz problem, which is is an apparently simple problem that has so far resisted any approach anyone has tried. The problem consists of showing that a sequence constructed from a simple mathematical rule will always be finite, independently of the starting value of the sequence. Here, we simply use it as a example of code to be tested, but interested readers can read about the history of the problem at the following site: https://en.wikipedia.org/wiki/Collatz_conjecture.

To run the code, save the file and select pycharm-test on the Project Navigator. Right-click on pycharm-test and select Run pycharm-test. The script will start and, if there are no syntax errors, you will be prompted for a number on the PyCharm console, shown as follows:

Enter starting value (enter 0 to exit): 

Enter a positive integer and press Enter. The script will compute the corresponding Collatz sequence and print it on the screen:

Enter starting value (enter 0 to exit): 7
Collatz sequence for n=7:
[7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
Enter starting value (enter 0 to exit): 22
Collatz sequence for n=22:
[22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]

After you are done experimenting with the script, enter 0 in the prompt line and the program will stop:

Enter starting value (enter 0 to exit): 0
Thanks for playing, I hope you had fun.
Process finished with exit code 0

Let's now use this code to experiment with the debugger. In the text editor, change the code that reads n = 3 * n + 1 to n = 2 * n + 1 and save the script. Run the code and enter a positive integer in the prompt. You will notice that no output is produced, and the script seems to be running in a loop. On the PyCharm menu, select Run-Stop pycharm-test. The script will be stopped and a KeyboardInterrupt exception will be reported in the console window. Stopping the script this way is equivalent to pressing Ctrl + C at the keyboard.

Let's set up a breakpoint to debug the code. We want to stop the script right after it enters the loop, at the line:

n_input = input("Enter starting value (enter 0 to exit): ")

To do this, click on the gutter at the left of the line, where we want to insert a breakpoint. The newly inserted breakpoint will be highlighted, as shown in the following screenshot:

Let's now start the script in debugging mode, by right-clicking pycharm-test on the Project Navigator and selecting Debug pycharm-test. The script will start and break at the line that asks for user input. Select Run-Step over on the menu, or press the F8 key, to run the next line in the script. When prompted, enter a positive integer and press Enter on the keyboard. At this point, the next line in the script will be highlighted:

try:

Continue to step over the code, until you get to the line:

cseq = collatz_sequence(n)

At this point, we want to go into the collatz_sequence() function to see what it is doing. To do this, select Run-Step into, or press F7. This will bring you into the code for collatz_sequence(). Now, continue to step over code (F8) until you can guess what is happening with the code.

You will notice that in the if statement inside the while loop, the n = 2 * n + 1 statement is being repeatedly executed. Notice what happens in the Editor window. After a few runs through the loop, the editor will show something like the following screenshot:

Notice that as you step over code, PyCharm displays an update about the contents of the collate_seq list. You will notice that this list keeps growing and, if you let the code run long enough, you will eventually get an exception due to a memory fault.

So, what is happening here? When the code gets to the if statement, it does the n % 2 == 0 test. This returns True only when n is even. If n is odd, the code falls through the else clause, where n is updated by the n = 2 * n + 1 statement. As a result, the updated value of n will again be an odd number and, the next time the loop body is executed, the code will again fall into the else clause. It is clear that this leads to an infinite loop.

Change the n = 2 * n + 1 line back to n = 3 * n + 1 to fix the code, and run it again to make sure everything is back as it should be.

The attentive reader will notice that, with the correct line of code, n = 3 * n + 1, if a particular value of n is odd, the next value of n will be even. Then, there will be a sequence of steps in which the number n is halved in the n //= 2 statement, until we get another odd number. One of the difficulties with the Collatz problem is that it seems that the number of times that n is halved is quite unpredictable, even though the code is completely deterministic. The reader might want to investigate the problem by him or herself, but be warned, this can be quite a rabbit hole!

In the preceding examples, we have barely scratched the surface of all features offered by PyCharm. The reader is invited to try using PyCharm on one of his/her projects, and to read the documentation to learn all that is available.