+ All Categories
Home > Documents > Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike...

Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike...

Date post: 21-Jul-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
49
Best practices in scientific programming Software Carpentry, Part I Valentin H¨ anel [email protected] Technische Universit¨ at Berlin Bernstein Center for Computational Neuroscience Berlin Python Winterschool Warsaw, Feb 2010 Slides based on material by Pietro Berkes 1 / 49
Transcript
Page 1: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Best practices in scientific programmingSoftware Carpentry, Part I

Valentin [email protected]

Technische Universitat BerlinBernstein Center for Computational Neuroscience Berlin

Python Winterschool Warsaw, Feb 2010Slides based on material by Pietro Berkes

1 / 49

Page 2: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Todays Schedule

Morning

Valentin

Agile MethodsUnit TestingVersion Control

Rike

Unit Testing ExamplesSubversionDebuggingProfiling

2 / 49

Page 3: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Todays Schedule

Afternoon

Niko

General Design Principles

Object Oriented Programming in Python

Object Oriented Design Principles

Design Patterns

3 / 49

Page 4: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Motivation

Many scientists write code regularly but few have formally beentrained to do so

Best practices can make a lot of difference

Development methodologies are established in the softwareengineering industry

We can learn a lot from them to improve our coding skills

4 / 49

Page 5: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Scenarios

Lone student/scientist

Small team of scientists, working on a common library

Speed of development more important than execution speed

Often need to try out different ideas quickly:

rapid prototyping of a proposed algorithmre-use/modify existing code

5 / 49

Page 6: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Outline

1 Introduction

2 Agile methods

3 Unit Testing

4 Version Control

5 Additional techniques

6 / 49

Page 7: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

What is a Development Methodology

Consist of:

A philosophy that governs the style and approach towardsdevelopment

A set of tools and models to support the particular approach

Help answer the following questions:

How far ahead should I plan?

What should I prioritize?

When do I write tests and documentation?

7 / 49

Page 8: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

The Waterfall Model, Royce 1970

Requirements

Design

Implementat ion

Testing

Maintenence

8 / 49

Page 9: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Agile Methods

Agile methods emerged during the late 90’s

Generic name for set of more specific paradigms

Set of best practices

Particularly suited for:

small teams ( less than 10 people)unpredictable or rapidly changing requirements

9 / 49

Page 10: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Prominent Features of Agile methods

Minimal planning

Small development iterations

Rely heavily on testing

Promote collaboration and teamwork

Very adaptive

10 / 49

Page 11: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

The Basic Agile Workflow

Define Test

Write SimplestVersion of Code

Ensure TestPasses

Writte Better Version of Code

11 / 49

Page 12: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Example

Define Test

function my sum should return the sum of a list.

12 / 49

Page 13: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Example

Write SimplestVersion of Code

1 def my_sum(my_list ):

2 """ Compute sum of list elements. """3 answer = 0

4 for item in my_list:

5 answer = answer + item

6 return answer

13 / 49

Page 14: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Example

Ensure TestPasses

1 >>> my_sum ([1 ,2 ,3])

2 6

14 / 49

Page 15: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Example

Writte Better Version of Code

1 def my_sum(my_list ):

2 """ Compute sum of list elements. """3 return sum(my_list)

15 / 49

Page 16: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Agile methods

16 / 49

Page 17: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Whats Next

Look at tools to support the agile workflow

Better testing with Unit Tests

Keeping track of changes and collaborating with Version Control

Additional techniques

17 / 49

Page 18: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Outline

1 Introduction

2 Agile methods

3 Unit Testing

4 Version Control

5 Additional techniques

18 / 49

Page 19: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Unit Tests

Definition of a Unit

The smallest testable piece of code

Example: my sum

We wish to automate testing of our units

In python we use the package unittest

19 / 49

Page 20: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Example

1 import unittest

2

3 def my_sum(my_list ):

4 """ Compute sum of list elements. """5 return sum(my_list)

6

7 class Test(unittest.TestCase ):

8 def test_my_sum(self):

9 self.assertEqual(my_sum ([1 ,2 ,3]) ,6)

10

11 if __name__ == "__main__":

12 unittest.main()

20 / 49

Page 21: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Running the Example

1 % python example -test2.py

2 .

3 --------------------------------------------------------

4 Ran 1 test in 0.000s

5

6 OK

21 / 49

Page 22: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

The Basic Agile Workflow - Reloaded

Define Unit Test

Write SimplestVersion of Unit

Ensure Unit TestPasses

Writte Better Version of Unit

22 / 49

Page 23: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Goals

check code works

check design works

catch regression

23 / 49

Page 24: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Benefits

Easier to test the whole, if the units work

Can modify parts, and be sure the rest still works

Provide examples of how to use code

24 / 49

Page 25: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

How to Test ?

Test with simple cases, using hard coded solutions

my sum([1,2,3]) == 6

Test special or boundary cases

my sum([]) == 0

Test that meaningful error messages are raised upon corrupt input

my sum([’1’, ’a’])→ TypeError: unsupported operand type(s) for +: ’int’and ’str’

25 / 49

Page 26: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

What Makes a Good Test?

independent (of each other, and of user input)

repeatable (i.e. deterministic)

self-contained

26 / 49

Page 27: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Stuff Thats Harder to Test

Probabilistic code

Use toy examples as validation

Consider fixing the seed for your pseudo random number generator

Hardware

use mock up software that behaves like the hardware should

Plots

(any creative ideas welcome)

27 / 49

Page 28: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Test Suits

All unit tests are collected into a test suite

Execute the entire test suite with a single command

Can be used to provide reports and statistics

28 / 49

Page 29: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Refactoring

This is what its called when you write a better version of your code.

Re-organisation of your code without changing its function:

remove duplicates by creating functions and methodsincrease modularity by breaking large code blocks into unitsrename and restructure code to increase readability and reveal intention

Always refactor one step at a time, and use the unit tests to checkcode still works

Learn how to use automatic refactoring tools to make your life easier

29 / 49

Page 30: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Dealing with Bugs

Isolate the bug (using a debugger)

Write a unit test to expose the bug

Fix the code, and ensure the test passes

Use the test to catch the bug should it reappear

Debugger

A program to run your code one step at a time, and giving you the abilityto inspect its current state.

30 / 49

Page 31: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Dealing with Bugs

31 / 49

Page 32: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Introducing New Features

Split feature into units

Use the agile workflow

Tests drive the development

Keep the iterations small

32 / 49

Page 33: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Some Last Thoughts

Tests increase the confidence that your code works correctly, not onlyfor yourself but also for your reviewers

Tests are the only way to trust your code

It might take you a while to get used to the idea, but it will pay offquite rapidly

Questions?

33 / 49

Page 34: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Outline

1 Introduction

2 Agile methods

3 Unit Testing

4 Version Control

5 Additional techniques

34 / 49

Page 35: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

What is Version Control?

Problem 1

”Help my code worked yesterday, but I can’t recall what I changed!”

Problem 2

”We would like to work together, but we don’t know how!”

Version control is a method to track changes in source code

Concurrent editing is possible via merging

35 / 49

Page 36: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Features

Revert to previous versions

Document developer effort

Who changed what, when and why?

Easy collaboration across the globe

36 / 49

Page 37: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Where the Versions are Stored?

Repository

ZazaYarikXenia

repository is located on a server

Developers must connect to this server

37 / 49

Page 38: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Contents of the Repository

Version 22 Version 23 Version 24

Version: 23Author: ValentinDate : 07.02.2010Message: Improve my_sumChanges: [...]

38 / 49

Page 39: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Basic Version Control Workflow

39 / 49

Page 40: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

What Will We Use ?

Many different systems available

We will use the de-facto standard:

40 / 49

Page 41: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Some Last Thoughts

Use version control for anything thats text

CodeThesisLetters

We will be using centralised version control, note there exists alsodecentralised version control

Again, it might take a while to get used to the idea, but it will pay offrapidly.

Questions

41 / 49

Page 42: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Outline

1 Introduction

2 Agile methods

3 Unit Testing

4 Version Control

5 Additional techniques

42 / 49

Page 43: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Pair Programming

Two developers, one computer

Two roles: driver and navigator

Driver sits at keyboard

Navigator observes and instructs

Switch roles every so often

43 / 49

Page 44: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Optimization for Speed

Readable code is usually better than fast code

Only optimize if its absolutely necessary

Only optimize your bottlenecks

...and identify these using a profiler, for example cprofile

Profiler

A tool to measure and provide statistics on the execution time of code.

44 / 49

Page 45: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Prototyping

If you are unsure how to implement something, write a prototype

Hack together a proof of concept quickly

No tests, no documentation

Use this to explore the feasability of your idea

When you are ready, scrap the prototype and start with the unit tests

45 / 49

Page 46: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Coding Style

Give your variables meaningful names

Adhere to coding conventions

OR use a consistent style

Use automated tools to ensure adherence: pylint

46 / 49

Page 47: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Documentation

Minimum requirement: at least a docstring

For a library document arguments and return objects

Use tools to automatically generated website from code: pydoc

47 / 49

Page 48: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

Results

Every scientific result (especially if important) should beindependently reproduced at least internally before publication.(German Research Council 1999)

Increasing pressure to make the source used in publications available

With unit tested code you need not be embarrassed to publish yourcode

Using version control allows you to share and collaborate easily

48 / 49

Page 49: Best practices in scientific programming Software ... - Python · Unit Testing Version Control Rike Unit Testing Examples Subversion Debugging Pro ling 2 / 49. Todays Schedule Afternoon

The Last Slide

Open source tools used to make this presentation:

wiki2beamerLATEXbeamerdia

Questions ?

49 / 49


Recommended