Python Debugging Session

Slides:



Advertisements
Similar presentations
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
Advertisements

Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
Mathcad Variable Names A string of characters (including numbers and some “special” characters (e.g. #, %, _, and a few more) Cannot start with a number.
Graphics CSE 581 – Interactive Computer Graphics Mathematics for Computer Graphics CSE 581 – Roger Crawfis (slides developed from Korea University slides)
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
Introduction to MATLAB adapted from Dr. Rolf Lakaemper.
Tutorial 1: Getting Started with Adobe Dreamweaver CS4.
Array Addition  Two arrays can be added if and only if both arrays have exactly the same dimensions.  Assuming the dimension requirement is satisfied,
Scientific Computing with NumPy & SciPy NumPy Installation and Documentation  Not much on the home page—don’t buy the guide, it’s.
Khoros Yongqun He Dept. of Computer Science, Virginia Tech.
Adobe CS6: The Graphic Design Portfolio Project 1: International Symbols Setting up the Workspace Drawing Basic Shapes.
Linear algebra: matrix Eigen-value Problems Eng. Hassan S. Migdadi Part 1.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1.
Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
CSE 455 : Computer Vision MATLAB 101 Getting Started with MATLAB.
Netbeans QuickStart. Creating a project File->New Project –For now you want General->Java Application –Then fill in the project details.
See Winter 2016CISC101 - Prof. McLeod1.
Debugging using By: Samuel Ashby. What is debugging?  A bug is an error in either a program or the hardware itself.  Debugging is first locating and.
Physics 114: Lecture 1 Overview of Class Intro to MATLAB
PH2150 Scientific Computing Skills
Introduction to MATLAB
Linear Algebra review (optional)
Numpy (Numerical Python)
Testing and Debugging.
Introduction to MATLAB
Introduction to MATLAB
Lecture 25: Backprop and convnets
Using Visual Studio with C#
Python NumPy AILab Batselem Jagvaral 2016 March.
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Introduction to Python programming
Introduction to MATLAB
Debugging with Eclipse
CSE Social Media & Text Analytics
Python Review.
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Numerical Computing in Python
Use of Mathematics using Technology (Maltlab)
MATLAB Tutorial Dr. David W. Graham.
PH2150 Scientific Computing Skills
Matlab tutorial course
MXNet Internals Cyrus M. Vahid, Principal Solutions Architect,
CSCI N207 Data Analysis Using Spreadsheet
Introduction to Image Processing in Python
Darkened picture background with full-color circle (Intermediate)
Numpy (Numerical Python)
Numpy (Numerical Python)
MATLAB Programming Indexing Copyright © Software Carpentry 2011
Python-NumPy Tutorial
Our Environment We will exercise on Microsoft Visual C++ v.6
INTRODUCTION TO MATLAB
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
MATLAB Programming Basics Copyright © Software Carpentry 2011
Python Basics Wen-Yen Hsu, Hsiao-Lung Chan Dept Electrical Engineering
Introduction to MATLAB
Simulation And Modeling
Dr. Sampath Jayarathna Cal Poly Pomona
R Course 1st Lecture.
Matlab Training Session 2: Matrix Operations and Relational Operators
Linear Algebra review (optional)
Dr. Sampath Jayarathna Old Dominion University
Math review - scalars, vectors, and matrices
Laboratory in Oceanography: Data and Methods
Numpy, pylab, matplotlib (follow-up)
Debugging with Eclipse
Tutorial 9 Using Action Queries and Advanced Table Relationships
Announcements.
Jingbo Yang, Andrey Kurenkov
Presentation transcript:

Python Debugging Session CS 5670 Qianqian Wang

PyCharm Debugging Techniques See here for basic tutorials

Virtualenv Environment Configurations In Settings/Preferences dialog (⌘,), select Project: <project name> | Project Interpreter. In the Project Interpreter page, click and select Add. In the left-hand pane of the Add Python Interpreter dialog box, select Virtualenv Environment. Select Existing environment, Specify the virtual environment in your file system, e.g., {full path to}/cs5670_python_env/bin/python2.7 Reference: Pycharm Help Page

Run/Debug Configurations Open the Run/Debug Configuration dialog [via Run | Edit Configurations] Ex. path to gui.py Ex. parameters of gui.py [-t resources/sample-correspondance.json -c resources/sample-config.json] Reference: Pycharm Help Page

Use Pycharm Debugger Set breakpoints: just click in the left gutter Click Debug Button Start Debugging! Step through your program Create a watch Evaluate an expression or enable the Python console in the Debugger Reference: Pycharm Help Page

Numpy array visualization During debugging, click ‘View as Array’ to visualize the array Want to visualize high-dimensional array? Try proper slicing

Virtual Machine v.s. Python Virtual Environment

Different levels of isolation: Python Virtual Environment: isolate only python packages VMs: isolate everything Applications running in a virtual environment share an underlying operating system, while VM systems can run different operating systems.

Numpy Basics

Slicing [Manual] Tips: What is an N Dimensional array? Slicing is simply setting an ordered subset. range: a:b, ‘:’ is a special character that represents the range logical mask any subset Indexing a single element can be viewed as slicing. Compare X[a, b, c] with X[a:a+1, b:b+1, c:c+1]. Dimension loss and expansion. Loss: set the slicing range for a dimension to a single scalar np.sum, np.mean, np.median, ... Expansion: np.newaxis, np.reshape, ... Slicing [Manual] What is an N Dimensional array? Write explicitly, X[0:m1, 0:m2, …, 0:mN] N: number of dimensions (axes) m1, m2, …, mN: length of each dimension (axis)

Slicing Examples Practices: Given an RGB image X[0:h, 0:w, 0:3] Get G channel of a RGB image X[:, :, 1] RGB to BGR X[:, :, [2, 1, 0]] Center-crop a RGB image X[r1:r2, c1:c2 , :] Downsample a RGB image by a factor of 2 X[0:h:2, 0:w:2, :]

Stacking[Manual] and Concatenating [Manual] np.stack(), np.concatenate() np.stack() requires that all input array must have the same shape, and the stacked array has one more dimension than the input arrays. np.concatenate() requires that the input arrays must have the same shape, except in the dimension corresponding to axis

Concatenation Examples

Vectorization Turn your loops to Numpy vector manipulation Vectorization enables fast parallel computation

Vectorization Example 1: element-wise multiplication For-Loop -- Inefficient Numpy Vector -- Efficient! >>> a = [1, 2, 3, 4, 5] >>> b = [6, 7, 8, 9, 10] >>> [x * y for x, y in zip(a, b)] [6, 14, 24, 36, 50] >>> import numpy as np >>> a = np.array([1, 2, 3, 4, 5]) >>> b = np.array([6, 7, 8, 9, 10]) >>> a * b array([ 6, 14, 24, 36, 50])

Vectorization Example 2: compute gaussian kernel For Loop Numpy Vector hc = height // 2 wc = width // 2 gaussian = np.zeros((height, width)) for i in range(height): for j in range(width): gaussian[i, j] = np.exp(-((i - hc)**2 + (j - wc)**2)/(2.0*sigma**2)) gaussian /= np.sum(gaussian) Numpy Vector hc = height // 2 wc = width // 2 grid = np.mgrid[-hc:hc+1, -wc:wc+1] # 2 x height x width gaussian = np.exp(-np.sum(grid**2, axis=0)/(2.0*sigma**2)) gaussian /= np.sum(gaussian)

Vectorization Example 2: compute gaussian kernel and plot Height = width = 9999, sigma = 1000 For Loop: ~106s Vectorization: ~12s

Other useful functions: vector operations: inner product [np.inner()], outer product [np.outer()], cross product [np.cross()], matrix multiplication [np.dot()] , matrix inverse [np.linalg.inv()] special matrices/vectors: np.zeros(), np.ones(), np.identity(), np.linspace(), np.arange() matrix reshaping: np.reshape(), np.transpose() (row_axis, column_axis, channel_axis) → (channel_axis, row_axis, column_axis): np.transpose(X,[2, 0, 1]) statistics: np.min(), np.max(), np.mean(), np.median(), np.sum() logical arrays: np.logical_and(), np.logical_or(), np.logical_not()

Q & A