CIS 192: Lecture 13 Scientific Computing and Unit Testing



Similar documents
Intro to scientific programming (with Python) Pietro Berkes, Brandeis University

Python Testing with unittest, nose, pytest

Python as a Testing Tool. Chris Withers

Profiling, debugging and testing with Python. Jonathan Bollback, Georg Rieckh and Jose Guzman

Survey of Unit-Testing Frameworks. by John Szakmeister and Tim Woods

Exercise 0. Although Python(x,y) comes already with a great variety of scientic Python packages, we might have to install additional dependencies:

Self-review 9.3 What is PyUnit? PyUnit is the unit testing framework that comes as standard issue with the Python system.

Advanced Functions and Modules

Scientific Programming in Python

Software Testing with Python

Test Driven Development in Python

Python. Python. 1 Python. M.Ulvrova, L.Pouilloux (ENS LYON) Informatique L3 Automne / 25

ANSA and μeta as a CAE Software Development Platform

Unit testing with mock code EuroPython 2004 Stefan Schwarzer p.1/25

Writing robust scientific code with testing (and Python) Pietro Berkes, Enthought UK

Python for Scientific Computing.

Software Testing. Theory and Practicalities

Wrestling with Python Unit testing. Warren Viant

Advanced Topics: Biopython

MACHINE LEARNING IN HIGH ENERGY PHYSICS

CME 193: Introduction to Scientific Python Lecture 8: Unit testing, more modules, wrap up

Python programming Testing

Big Data Paradigms in Python

Optimizing and interfacing with Cython. Konrad HINSEN Centre de Biophysique Moléculaire (Orléans) and Synchrotron Soleil (St Aubin)

CIS 192: Lecture 10 Web Development with Flask

Parallel Computing in Python: multiprocessing. Konrad HINSEN Centre de Biophysique Moléculaire (Orléans) and Synchrotron Soleil (St Aubin)

Assignment 2: Option Pricing and the Black-Scholes formula The University of British Columbia Science One CS Instructor: Michael Gelbart

An Introduction to APGL

From mathematics to a nice figure in a LaTeX document

SpiraTest / SpiraTeam Automated Unit Testing Integration & User Guide Inflectra Corporation

IERG 4080 Building Scalable Internet-based Services

Automated Testing with Python

Gillcup Documentation

LINES AND PLANES CHRIS JOHNSON

Python for Test Automation i. Python for Test Automation

6.170 Tutorial 3 - Ruby Basics

Introduction to Python

Zabin Visram Room CS115 CS126 Searching. Binary Search

PGR Computing Programming Skills

CUDAMat: a CUDA-based matrix class for Python

Part VI. Scientific Computing in Python

New Tools for Testing Web Applications with Python

Assignment 4 CPSC 217 L02 Purpose. Important Note. Data visualization

Tools and Techniques for Developing Atmospheric Python Software: Insight from the Python ARM Radar Toolkit

Programming Languages & Tools

Introduction to Python

Continuous Integration

Moving from CS 61A Scheme to CS 61B Java

CSE 6040 Computing for Data Analytics: Methods and Tools

Testing Python. Applying Unit Testing, TDD, BDD and Acceptance Testing

Matrix Multiplication

Calling R Externally : from command line, Python, Java, and C++

F.IF.7b: Graph Root, Piecewise, Step, & Absolute Value Functions

2: Computer Performance

Python and Google App Engine

Unit Testing webmethods Integrations using JUnit Practicing TDD for EAI projects

Ruby in the context of scientific computing

Postprocessing with Python

Module 10. Coding and Testing. Version 2 CSE IIT, Kharagpur

Introduction to Python

Exercise 4 Learning Python language fundamentals

Simulation software for rapid, accurate simulation modeling

AMATH 352 Lecture 3 MATLAB Tutorial Starting MATLAB Entering Variables

CRASH COURSE PYTHON. Het begint met een idee

Introduction to the course, Eclipse and Python

The Clean programming language. Group 25, Jingui Li, Daren Tuzi

Survey of the Mathematics of Big Data

Java Program Coding Standards Programming for Information Technology

Adaptive Stable Additive Methods for Linear Algebraic Calculations

Today's Topics. COMP 388/441: Human-Computer Interaction. simple 2D plotting. 1D techniques. Ancient plotting techniques. Data Visualization:

Why (and Why Not) to Use Fortran

CIS 192: Lecture 10 Web Development with Flask

Python for Chemistry in 21 days

Scientific Programming, Analysis, and Visualization with Python. Mteor 227 Fall 2015

THE NAS KERNEL BENCHMARK PROGRAM

How To Develop Software

Outline. NP-completeness. When is a problem easy? When is a problem hard? Today. Euler Circuits

How To Train A Face Recognition In Python And Opencv

Chemical and Biological Engineering Calculations using Python 3. Jeffrey J. Heys

Monitis Project Proposals for AUA. September 2014, Yerevan, Armenia

A linear combination is a sum of scalars times quantities. Such expressions arise quite frequently and have the form

CS 1133, LAB 2: FUNCTIONS AND TESTING

Analytic Modeling in Python

Data Visualization. Christopher Simpkins

Developing an Inventory Management System for Second Life

Programming in Python. Basic information. Teaching. Administration Organisation Contents of the Course. Jarkko Toivonen. Overview of Python

Automated Testing Options for PL/SQL Steven Feuerstein PL/SQL Evangelist, Quest Software

Recall the basic property of the transpose (for any A): v A t Aw = v w, v, w R n.

Active Learning SVM for Blogs recommendation

Visualizing Data: Scalable Interactivity

JMulTi/JStatCom - A Data Analysis Toolkit for End-users and Developers

System Level Integration and Test Leveraging Software Unit Testing Techniques

SLANGTNG - SOFTWARE FOR STOCHASTIC STRUCTURAL ANALYSIS MADE EASY

Cryptography and Network Security Department of Computer Science and Engineering Indian Institute of Technology Kharagpur

Java in Education. Choosing appropriate tool for creating multimedia is the first step in multimedia design

QEngine Technical Paper. Building Maintainable Test Cases with QEngine

Transcription:

CIS 192: Lecture 13 Scientific Computing and Unit Testing Lili Dworkin University of Pennsylvania

Scientific Computing I Python is really popular in the scientific and statistical computing world I Why? Python is slow! I So we use libraries with routines written in C/C++ I We will look at Numpy/Scipy and Matplotlib/Pylab I But first, how do we benchmark Python?

Cosine Similarity I Goal: find how similar two vectors are I One measure: compute angle between them I Cosine similarity of two vectors U and V : I cos(0) = 1, cos( ) = 1 cos( ) = U V kukkv k

Cosine Similarity def cosine_similarity(u,v): mag1, mag2, dot = 0.0, 0.0, 0.0 for a,b in zip(u,v): dot += a * b mag1 += a ** 2 mag2 += b ** 2 return dot / (math.sqrt(mag1) * math.sqrt(mag2))

Timeit Previously I used time.time() don t do that. Instead: >>> import timeit >>> t = timeit.timer("<statement to time>", "<setup code>") >>> t.timeit() I The second argument is usually an import that sets up a virtual environment for the statement I timeit calls the statement 1 million times and returns the total elapsed time I timing.py

Numpy/Scipy I Basic operations numpy_demo.py I Doing things faster numpy_timing.py

Numpy/Scipy I Fast integer and floating point types (numpy.int, numpy.float) I Fast arrays (numpy.array) and matrices(numpy.ndarray), and fast operations over every element I Tons of functions and algorithms, including linear algebra, Fourier transforms, etc I Scipy depends on Numpy, and gathers a lot of high level science and engineering modules together (integrate, linalg, optimize, stats...)

Matplotlib/Pylab I Plotting/charting library I Good alternative to Excel I Matpotlib is the whole package I matplotlib.pyplot - non-interactive plotting (scripting) I matplotlib.pylab - interactive calculations and plotting I pylab_demo.py

Polya s Urn

Doctest The doctest module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to verify that they work exactly as shown.

Doctest >>> import doctest >>> options = (doctest.ignore_exception_detail doctest.normalize_whitespace) >>> doctest.testmod(optionflags=options)

Doctest How do exceptions work? >>> factorial(30.1) Traceback (most recent call last):... ValueError: n must be exact integer If we specified doctest.ignore_exception_detail, everything to the right of the colon is ignored. Example in doctest_demo.py.

Unit Testing I Well-written programs can be broken up into units I I I I Functions, methods Classes Modules Packages I Unit testing aims to test the functionality of all the units in your program

Unittest I The unittest module makes it relatively easy to write a suite of unit tests for your programs I Modeled after JUnit, a Java unit testing framework I Caveat: many ways to use it, we ll just look at one approach

Random Python s random module: >>> import random >>> random.random() 0.41872335193283494 >>> random.randint(10, 20) 18 >>> seq = [2, 4, 6, 8, 10] >>> random.shuffle(seq) >>> seq [8, 4, 10, 2, 6] >>> random.choice(seq) 4 >>> random.sample(seq, 3) [4, 2, 6]

Unittest import unittest import random class TestSequenceFunctions(unittest.TestCase): def setup(self): self.seq = range(10) def test_shuffle(self): def test_choice(self): def test_sample(self): if name == ' main ': unittest.main()

Unittest I Inherit from unittest.testcase, abaseclassfortestcase I Here we used it to define multiple test cases at once I The setup method is called before each test case is run I Any method whose name starts with test defines a test case I unittest.main() runs all test cases

Unittest How do we write test cases? Use TestCase.assert* methods: I self.assertequal() I self.asserttrue() I self.assertraises() Let s practice in testing.py.

Digression Each of the following functions takes a callable and a list of arguments to provide it: self.assertraises(valueerror, random.sample, self.seq, 20) and Thread(target=add, args=(5,6))

Digression Let s look at the headers : assertraises(exception, callable, *args, **kwds) vs. Thread(target=None, args=(), kwargs={})

Unittest I unittest distinguishes between failures and errors I Failure: The assert statement was wrong I self.assertequal(range(5), range(10)) I Error: Something is wrong with the code I print 5 + hi