0% found this document useful (0 votes)
70 views

1 - Introduction To Python

This document provides an introduction to Python programming. It discusses Python's history and popularity across organizations. It outlines the chapter topics which include Python setup, syntax elements, non-numerical and numerical examples using Python modules. It covers installing Python, verifying the installation, using the Python interpreter versus scripts, setting up a text editor for Python development. It discusses Python style guidelines and provides examples of Python code and syntax rules. The document is intended to teach the basics of Python programming.

Uploaded by

HERiTAGE1981
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

1 - Introduction To Python

This document provides an introduction to Python programming. It discusses Python's history and popularity across organizations. It outlines the chapter topics which include Python setup, syntax elements, non-numerical and numerical examples using Python modules. It covers installing Python, verifying the installation, using the Python interpreter versus scripts, setting up a text editor for Python development. It discusses Python style guidelines and provides examples of Python code and syntax rules. The document is intended to teach the basics of Python programming.

Uploaded by

HERiTAGE1981
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

CHAPTER 1


Introduction to

Python

Hervé Gross, PhD - Reservoir Engineer


Advanced Resources and Risk Technology
[email protected]

© ADVANCED RESOURCES AND RISK TECHNOLOGY This document can only be distributed within UFRGS
Motivation for

o Python is a general-purpose scripting and programming language


o Powerful, scalable, stable
o Embraced by organizations of all sizes: Google, NASA, AR2Tech
o Cross-platform (same code everywhere), free (widespread), open-source (well-documented)
o First release in 1991, Python 2.0 in October 2000, Python 3.0 in December 2008
o Python 3.+ adopted slowly (not backwards compatible), v 2.7.+ ubiquitous but phasing out (discussion here)
o Project creator Guido van Rossum was said to be a fan of the Monty Python

o The modularity of Python has led to developments in all scientific computation domains, including
machine learning

© ADVANCED RESOURCES AND RISK TECHNOLOGY 2


Chapter outline

1 – Python Set-up
Python installation, trying the interpreter, setting up a text editor for Python
2 – Python syntax elements
Variables, operators, controls, conditions, classes, objects
3 – Non-numerical examples
String manipulations, file I/O and data management
4 – Numerical examples
Numpy and SciPy modules: using Python for scientific purposes
5 – Geostatistics with Python
Python in AR2Gems: geostatistics with Python, and more
6 - Resources and references

© ADVANCED RESOURCES AND RISK TECHNOLOGY 3


1 - Python set-up

© ADVANCED RESOURCES AND RISK TECHNOLOGY 4


Python interpreter and Python scripts

o Unix and Mac have Python installed by default,


Windows requires downloading Python at
http://python.org/ (free and open-source)
o Three ways to use Python:
o interpreter: command line, executing one
command at a time, displaying the result of each
operation. More interactive, great for testing or
syntax-checks, but not suited to long series of
commands.
o Python files (extension *.py): simple text files with
sequences of commands (a “script”) executed at
once with no or minimal interaction. Editing files www.python.org : the central hub for
downloads, documentation, tutorials
and running files are two distinct operations. This is
our preferred approach.
o Jupyter Notebooks: https://ipython.org/
notebook.html
Use the interpreter or notebook only for experiments,
the goal of this class is to write Python scripts.

© ADVANCED RESOURCES AND RISK TECHNOLOGY 5


Verify that Python is installed (Windows)

1. Open a DOS terminal: in the Windows Start menu, look for cmd in the Search program and files bar
2. In the prompt, type “python” + enter to fire up the Python interpreter. Notice the Python version
information.
3. Three >>> will appear. All commands should now be in written in Python syntax.

4. Type: print “Hello world!” + enter.


5. Type: 16*9 + enter.
6. Use up-arrow to repeat a command.
7. Use CTRL+z to exit from the Python interpreter and return to DOS (FYI: CTRL+d on Mac and Unix).

© ADVANCED RESOURCES AND RISK TECHNOLOGY 6


Pythonic style guide

In your Python interpreter type:


>>> import this
And meditate…

Python is based on the vision that code should be easy to write and read
o One (preferably obvious) way to do things
o No optimization at the cost of clarity
o Design a small core of functions and make it easy for the community to create extensions

© ADVANCED RESOURCES AND RISK TECHNOLOGY 7


Text editor-based Python

o Not free but really good : PyCharm

o Free, open-source Notepad++ is our preferred free text editor: tabs, syntax highlights, auto-indent…

o Do not use Notepad or Wordpad: problems with line breaks, indentation, automatic case, no highlights

o Configure Notepad++ to make it work for you:

o Go to Settings > Preferences

o New Document > Format (Line Ending) : Unix/OSX (more portable than Windows EOL)

o Tab Settings: [Default] Tab size : 2, check “replace by space” (no tabulation)

o Font: prefer monospaced (=fixed-width) fonts for readability : all characters and spaces have the the same
width: Courier New (our default), Lucinda Console, Consolas, Inconsolata, … (avoid fonts that
are optimized for text/headings like Arial, Times… and be wary of 1, l, i, 0, o and O looking alike).

o Syntax highlighting : highlight colors for keywords, comments and operators should be used

o Color scheme: Settings > Style Configurator (vim dark blue is easy on the eyes)

Mac: TextWrangler or jEdit, Unix: vi, pico, Kdevelop

http://notepad-plus-plus.org/
http://hivelogic.com/articles/top-10-programming-fonts/

© ADVANCED RESOURCES AND RISK TECHNOLOGY 8


Create a shortcut for a useful command

A useful action

Assigned to CTRL + R

© ADVANCED RESOURCES AND RISK TECHNOLOGY 9


Exercise 1 that
Ex. 2: Test : Try your
it outPython
! configuration works

© ADVANCED RESOURCES AND RISK TECHNOLOGY 10


Some syntax rules and guidelines

o Official Python style guideline (PEP8): https://www.python.org/dev/peps/pep-0008 - exhaustive and detailed. A good
read on your spare time to pick up good syntax habits right from the start.

o End of a statement = end of line (no need for “;” like in C++ but Python tolerates it)

o Comments begin with a pound/hash sign # and run until the end of the line

o Indentation matters: blocks that go together must have the same number of leading whitespaces

o PEP8 recommends indentation with 4 spaces, Google uses 2 spaces, AR2Tech likes 2 spaces also (clear enough)

o Do not indent with tabs, tabs are not consistently ported across systems and text editors

o Maximum suggested code line length is 72 characters (comments or docstrings must not exceed 72 characters)

o Wrapping of long lines can be done in two ways:

o Implied line continuation inside parentheses, brackets, braces: indent rest of expression such that it aligns with ( or [ or {

o Use backslashes at end of line (to be avoided whenever possible, for readability)

© ADVANCED RESOURCES AND RISK TECHNOLOGY 11


So, what does Python look like?

import sys

# A general comment goes here

def word_count_dict(filename):
"""Returns a word/count dict for this filename."""
# Utility used by count() and Topcount().
word_count = {} # Map each word to its count
input_file = open(filename, 'r')
for line in input_file:
words = line.split()
for word in words:
word = word.lower()
# Special case if we're seeing this word for the first time.
if not word in word_count:
word_count[word] = 1
else:
word_count[word] = word_count[word] + 1
input_file.close() # Not strictly required, but good form.
return word_count

Things to notice

indentation, comments, docstrings, the function definition syntax, the keywords import, for, if, else, in,
return, the use of : , the .dosomethingonprefix() syntax, and a usage of fairly complex objects (such as
maps here: every word is mapped to an integer, reflecting its count in filename)

© ADVANCED RESOURCES AND RISK TECHNOLOGY 12


For the style-challenged among us

In DOS (not in the Python interpreter) type:


> pip install pep8
You can now test your PEP8 compliance by checking code with the following DOS
command:
> pep8 mycode.py
[this is not a Python interpreter command, it is a DOS command]

It will return a log of all syntax PEP8 unconformities.


These are best practices more than hard rules but it helps to start with good habits
Readability is the goal here, and PEP8 offers a consistent and well-thought out style guide

© ADVANCED RESOURCES AND RISK TECHNOLOGY 13


2 - Python syntax elements

© ADVANCED RESOURCES AND RISK TECHNOLOGY 14


Variables Identifiers

NB : Python idiom: in Python variables are called “identifiers”

o There are no variable declaration and no variable initialization in Python (dynamic language, all checks at run-time)
o Just assign identifiers directly using the equal sign = (this means that, regardless of what it meant before)
o Python identifiers are case-sensitive : A and a are two different things
o The scope of identifiers is the interpreter session, in the session no names are private (caution)
o Because types are not specified, use identifier names judiciously (use plurals for lists of things, for instance)
o “Throwaway” by convention are 2 underscores: __ (when a function returns values you do not need)


assign

assign again (overwrites previous value)

© ADVANCED RESOURCES AND RISK TECHNOLOGY 15


Identifiers types

Five useful data types : applepies = 4 # An integer assignment


o Numbers : int, long, float, complex. Use the decimal point to unitprice = 10.99 # A floating point
assign an integer as float (notice the difference between customer = “Jane” # A string
10/3 and 10/3.)
print applepies
o Strings : assigned with ‘single’ or “double” quotation print unitprice
marks. print customer
The slice operator is [ ] or [ : ], indexing starts at 0 and goes until
n-1, negative indexing is allowed (-1 is the last element index, -2
one before last, …, -n is the first element). Use operator + to
concatenate, use * to repeat.

o Lists : list of items that can be of different types, assigned with [ , , , ]

Indexing starts at 0 and goes until n-1, same slice [ : ], + and * operators as strings.

o Tuples : tuples are read-only lists : no element can be altered, removed or appended. Assigned with ( , , , ), same slice
operator [ : ], + and *. Very Pythonic!

This syntax is not allowed with tuples: mytuple[4] = 1000 because it alters one element. It would be allowed in lists.

o Dictionaries : key-value pairs, assigned with { } braces. Elements can be accessed and added using [ ] square brackets
dict[key] = value

Any type of variable can be used as a key or value (although keys are generally numbers or strings).

© ADVANCED RESOURCES AND RISK TECHNOLOGY 16


Conversions between types
Function Description
int(x [,base]) Converts x to an integer. base specifies the base if x is a string.
long(x [,base] ) Converts x to a long integer. base specifies the base if x is a string.
float(x) Converts x to a floating-point number.
complex(real [,imag]) Creates a complex number.
str(x) Converts object x to a string representation.
repr(x) Converts object x to an expression string.
eval(str) Evaluates a string and returns an object.
tuple(s) Converts s to a tuple.
list(s) Converts s to a list.
set(s) Converts s to a set.
dict(d) Creates a dictionary. d must be a sequence of (key,value) tuples.
frozenset(s) Converts s to a frozen set.
chr(x) Converts an integer to a character.
unichr(x) Converts an integer to a Unicode character.
ord(x) Converts a single character to its integer value.
hex(x) Converts an integer to a hexadecimal string.
oct(x) Converts an integer to an octal string.

http://www.tutorialspoint.com/python/python_variable_types.htm

© ADVANCED RESOURCES AND RISK TECHNOLOGY 17


Reserved identifiers (aka keywords)

Names can be any string except one of the 31 reserved names:

and del from not while


as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try

© ADVANCED RESOURCES AND RISK TECHNOLOGY 18


Lists: a very useful container

Lists are containers just like C++ arrays but can contain different types of objects
x = [12,3,4,"test"]

print "Length of the list :",len(x)

print "The element at position 0 is ",x[0]

print "The element at position 3 is ",x[3]

The square brackets [ ] are used to access an element in the list using an index value (integer)
The command range(n) generates a list of integers from 0 to n-1 :
print range(10)

x = range(10)

print "The element at position 5 is ",x[5]

Indexing in Python can be positive or negative


The first element has index zero x[0] = 0 or –n : x[-10] = 0
The last element has index 9 x[9] = 9 or -1 : x[-1] = 9

Very useful!

Allows users to get the value of
the last element without needing
to know how many elements the
list contains.

© ADVANCED RESOURCES AND RISK TECHNOLOGY 19


Adding elements to a list

Create an empty list


x =[]
Append element to the list
x.append(2.0)

x.append("pop")

x.append(-9)
The list has three elements :
x[0] is 2.0

x[1] is ‘pop’

x[2] is -9
Also, lists use negative indexing (from –n to -1):

x[-3] 2.0 

x[-2] is ‘pop’

x[-1] is -9


© ADVANCED RESOURCES AND RISK TECHNOLOGY 20


Loops

Python’s for statement iterates over the items of any sequence (a list or a string) in the
order that they appear in the sequence (rather than iterating with a step and halting
condition like in C):
words = ['we', 'are', 'ar2tech']
 RESULT:
we 2

for w in words:
 are 3

print w, len(w) ar2tech 7

Notice that w is not an index, it is the element of the sequence itself.


One particularity of Python over other languages is the use of indentation to define blocks
of code within a control flow statement.
Using a list (range(10) creates a list) :
for i in range(10) :

print i
i will sequentially takes the values 0 to 9 as generated by range(10).
NB: range() can be overloaded: range(start_int,end_int), or range(start_int, end_int, increment)

© ADVANCED RESOURCES AND RISK TECHNOLOGY 21


Python idioms for list
Bad (but functional) syntax Pythonic syntax

sq = []
FOR

for i in range(10): sq = [i*i for i in range(10)]


sq.append(i*i) FOR LOOP INCLUDED HERE

prime = [2,3,5,7,11,13,17,19]
FOR + IF

next = [] # initialize list prime = [2,3,5,7,11,13,17,19]


for i in prime: next = [i+1 for i in prime if i > 10]
if i > 10: FOR LOOP IF LOOP
next.append(i + 1)
ENUMERATE

prime = [2,3,5,7,11,13,17,19] prime = [2,3,5,7,11,13,17,19]


for idx in range(len(prime)): for idx, item in enumerate(prime):
print idx, prime[idx] print idx, item

CLEAN WAY TO HAVE A COUNTER AND THE ASSOCIATED ELEMENT OF A LIST

© ADVANCED RESOURCES AND RISK TECHNOLOGY 22


If, elif, else – conditional statements

print 'The value you entered is...‘
 o Indentation delimits the scope of statements
if n<0:

print 'negative! so we changed it to 0.‘
 o Use colon (:) after the logical test, it is common to forget it
n=0

elif n==0:
 o Use elif, not elseif (think elif and else as 4-letter words).
print 'zero‘

o No { } are used to delimit blocks, only indentation
elif n<=4:

print 'lesser than or equal to four‘
 o No ( ) are used in the condition tests
else:

print 'Larger than four‘
 o Boolean are spelled out completely : and or not
print 'Thanks for checking.'
o Evaluate long conditions outside of the if-line

o Any of the following evaluate to False: 0, None, empty (string, list, dictionary), and of course False.
o Hint: empty means false, so do not test len(str)==0 instead, just check if str.
o Never compare with True or False (bad form), just use if var.
o Useful:
o break : get out of the current if or for
o pass: do nothing (useful if the syntax requires something to be present)
o continue: stop current iteration here and start the next iteration of the loop

© ADVANCED RESOURCES AND RISK TECHNOLOGY 23


Functions

In Python, a function is defined with the keyword def. For instance:


def square_value(x) :
'''square_value(x) return x*x'''
return x*x

The function body must be indented.


It is a best practice to name functions using the lowercase_and_underscores naming convention.
About Docstrings
The text between the three single quotes ''' ''' is a docstring:

The docstring is used to generate the text seen when typing help(..) on a function or module:
help(module) prints the docstring located at the header of the module.py file

help(module.function) or help(module.class) prints the docstring inside the function or class

Docstrings should be descriptive, usable, and explicit.

© ADVANCED RESOURCES AND RISK TECHNOLOGY 24


Objects and classes

class IndicatorTransform :
'''Indicator transform for Multidimensional array''' o Python is an object-oriented
def __init__(self,threshold) :
'''input the list of thresholds''' programming language
self.tresh = threshold

#method that defines the () operator


o Objects are self-sufficient variables,
def __call__(self, data) : with specified content and behavior
i = []
for t in threshold :
i.append( [int(d< t) for d in data] ) o Just like in C++, an object is an
return I
instance of a class
o A class is an abstraction that define
To use the class IndicatorTransform, you first need to create an object of type
IndicatorTransform: the objects behavior
indCoding = IndicatorTransform([3,4,5]) o Classes can be seen as functions
And the call it using the () operator where parameters are stored
print ’data set 1: ’,indCoding(range(0,10)) internally
print ’data set 2: ’,indCoding(range(10,-1,-1))
o Notice the __init__ (two underscores)
The threshold values are entered only once and internally stored
constructor
print indCoding.tresh
o Class naming use CapWords
convention.

© ADVANCED RESOURCES AND RISK TECHNOLOGY 25


3 – Non-numerical examples

© ADVANCED RESOURCES AND RISK TECHNOLOGY 26


Strings

o Enclosed in single or double quotes (single quotes more frequent)


o Usual C-like escaping sequences \’ \” \n \t
o If enclosed in single quotes, inner double quotes need not be escaped (and vice-versa)
o Use backslash \ to continue across lines
o Enclose in three single or three double quotes for multi-line strings
o Strings are immutable: create new strings instead of modifying existing ones
o Slice with [ … ], index starts at 0, negative indexing possible, use colon : to have a
range
o Use len( ) to measure the length of a string (just like any list)
o Concatenate with +, repeat with * (for instance ‘_’*10 creates a ten-character long
underscore line)
o No implicit conversion : numeric types must be converted with str(…)
pi = 3.14

text = 'the value of pi is '+ str(pi)

© ADVANCED RESOURCES AND RISK TECHNOLOGY 27


Common string methods

o s.lower(), s.upper() returns the lowercase or uppercase version of the string


o s.strip() returns a string with whitespace removed from the start and end
o s.isalpha() s.isdigit() s.isspace() tests if all the string chars are in the various character classes
o s.startswith('other'), s.endswith('other') tests if the string starts or ends with the given other string
o s.find('other') searches for the given other string in s, returns the first index where it begins or -1 if not found
o s.replace('old', 'new') returns a string where all occurrences of 'old' have been replaced by 'new'
o s.split('delim') returns a list of substrings separated by the given delimiter. The delimiter is not a regular
expression, it's just text. 'aaa,bbb,ccc'.split(',') -> ['aaa', 'bbb', 'ccc']. As a convenient special case s.split() (with no
arguments) splits on all whitespace chars.
o s.join(list) opposite of split(), joins the elements in the given list together using the string as the delimiter.
Example: '---'.join(['aaa', 'bbb', 'ccc']) results in aaa---bbb---ccc

© ADVANCED RESOURCES AND RISK TECHNOLOGY 28


https://developers.google.com/edu/python/introduction

Python Modules

A file of Python code is called a module. The file “alex.py" is also known as the module “alex".

A module contains variable definitions like, "x = 6" and "def foo()".
Suppose the file “alex.py" contains a "def foo()". The fully qualified name of that foo function is “alex.foo".
Various Python modules can name their functions and variables whatever they want, and the variable names will not conflict --
module1.foo is different from module2.foo.

We have the standard "sys" module that contains some standard system facilities, like the argv list, and exit() function. With the
statement "import sys" you can access the definitions in the sys module and makes them available by their fully-qualified name, e.g.
sys.exit().

import sys

# Now can refer to sys.xxx facilities

sys.exit(0)

There is another import form that looks like this: "from sys import argv, exit". That makes argv and exit() available by their
short names (no prefix). However, we recommend the original form with the fully-qualified names because it makes it easier to
determine where a function or identifier came from.

There are many modules and packages which are bundled with a standard installation of the Python interpreter, so you don't have do
anything extra to use them. These are collectively known as the "Python Standard Library." Commonly used modules/packages include:

• sys : access to exit(), argv, stdin, stdout


• re : regular expressions
• os : operating system interface, file system

You can find the documentation of all the Standard Library modules and packages at http://docs.python.org/library.

© ADVANCED RESOURCES AND RISK TECHNOLOGY 29


Using modules

1. A module is a file containing Python definitions and statements.


import math # imports the entire module, all objects

print math.sin(math.pi) # namespace prefix necessary to access objects

2. It is possible to import only parts of the modules


from math import sin, pi # import specific objects separated by commas

print sin(pi) # no namespace prefix necessary

3. When no ambiguity exists, it is possible to import all the module without namespace using wildcard *
from math import * # imports the entire module, all objects (like 1)

print sin(pi), log(e) # no namespace necessary but beware of name conflicts

© ADVANCED RESOURCES AND RISK TECHNOLOGY 30


Getting help with modules

To get help on a module, import it first, then type help(module_name) or


help(module.class), or help(module.function).
You will see the help text (which comes from the docstrings written in the module) for each
module or class or function.

o To scroll through the help reader line by line : use <space> or <enter>,
o To skip lines, use s or p and specify the number of screens or positions (lines) to scroll down
by,
o To quit the help and return to the command, use q or f.

Also useful is the interactive help. From anywhere in the Python command, type help() and
follow the instructions. Just hit <enter> in a empty help> line to return to the Python command.
Each module is only imported once per interpreter session. Therefore, if you change your
modules, you must restart the interpreter – or, if it’s just one module you want to test
interactively, use reload(modulename).

© ADVANCED RESOURCES AND RISK TECHNOLOGY 31


Create a Python package

o A package is a folder containing a collection of modules and a __init__.py file


- somepackage (Folder)

- __init__.py (File)

- somemodule.py (File)

- SomeClass (Code)

- some_function (Code)
o In the __init__.py file, we list what modules to import:
from somepackage import SomeClass
from somepackage import some_function

© ADVANCED RESOURCES AND RISK TECHNOLOGY 32


I/O and file management

Two techniques to open and write/read a file


A C-like technique (to be avoided)
f = open('inputfile.txt')

a = f.read()

print a

f.close()

A more Pythonic method using the with … as syntax:


with open('inputfile.txt') as f:

for line in f:

print line

And to write to a file:


with open('thefilepath') as thefile:

for item in thelist:

thefile.write("%s\n" % item)

© ADVANCED RESOURCES AND RISK TECHNOLOGY 33


Module to read/write a csv file

o Use the csv module, and specify the delimiter :


o To read:
import csv

with open('eggs.csv', 'rb') as csvfile:

spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')

for row in spamreader:

print ', '.join(row)

o To write:
import csv

with open('eggs.csv', 'wb') as csvfile:

spamwriter = csv.writer(csvfile, delimiter=' ',

quotechar='|', quoting=csv.QUOTE_MINIMAL)

spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])

spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])

© ADVANCED RESOURCES AND RISK TECHNOLOGY 34


For files that need not be seen

by human eyes

Use the pickle module:


import pickle
pickle.dump(itemlist, outfile)
To read pickled data back:
itemlist = pickle.load(infile)
Pickling is the simplest, most efficient and portable way to “serialize” identifiers in Python
(ie. turn any object hierarchy, classes, data, etc. into a flat byte stream). Unpickling is the
opposite process.
Pickled files are usually suffixed “*.pyc”.
Disclaimer: never unpickle data received from an untrusted or unauthenticated source as it may perform dangerous actions...

NB: Also used for serialization is the simplejson module, with more custom options (thus
less portability):
https://github.com/simplejson/simplejson

© ADVANCED RESOURCES AND RISK TECHNOLOGY 35


Launching programs from Python

(aka spawning new processes)

Use the subprocess module (preferred over older modules such as os.system)
subprocess.call(args, shell=True)
Example of system call:
status = subprocess.call("mycmd" + " myarg", shell=True)
Try calling AR2Gems from the Python command line.

https://docs.python.org/2/library/subprocess.html
http://stackoverflow.com/questions/89228/calling-an-external-command-in-python
https://docs.python.org/2/library/subprocess.html#replacing-older-functions-with-the-subprocess-module

© ADVANCED RESOURCES AND RISK TECHNOLOGY 36


4 – Numerical examples

© ADVANCED RESOURCES AND RISK TECHNOLOGY 37


Suggested Python libraries for science

o Scipy/numpy Library for scientific computing : http://www.scipy.org/install.html


o Matrices, linear system, FFT
o Image analysis, optimization, ...
o SciKit Learn : machine learning modules : http://scikit-learn.sourceforge.net/stable/
o Matplotlib (part of scipy)
o Library for data visualization
o Similar syntax to Matlab

These libraries are not mandatory, but are required for more advanced numerical
computing.

Unofficial precompiled libraries for Windows:


http://www.lfd.uci.edu/~gohlke/pythonlibs/

© ADVANCED RESOURCES AND RISK TECHNOLOGY 38


SciPy

import scipy
SciPy is an open source library of scientific and engineering tools for Python. SciPy provides modules for:
o statistics
o optimization
o numerical integration
o linear algebra
o Fourier transforms
o signal processing
o image processing
o genetic algorithms
o ODE solvers
From http://www.scipy.org/more_about_SciPy

© ADVANCED RESOURCES AND RISK TECHNOLOGY 39


SciPy arrays

o a powerful N-dimensional array object


o advanced array slicing methods (to select array elements)
o array reshaping methods
An array created with scipy is significantly faster than a python list

import scipy
data = scipy.empty( (100), dtype=float )
print data[0:5]
print ’Size of array: ’,data.shape

© ADVANCED RESOURCES AND RISK TECHNOLOGY 40


Creation of SciPy arrays

100x3 Array of zeros :


data = scipy.zeros((100,3), dtype=int)
100x3 Array of ones :
data = scipy.ones((100,3), dtype=int)
From Python list
data = scipy.array([1,2,4,5])
To Python list
data = scipy.arange(10) # list integers from 0 to 9
dataList = data.tolist()

© ADVANCED RESOURCES AND RISK TECHNOLOGY 41


SciPy array shapes

A SciPy array must have a fixed number of elements, set when the array is created.

This is different from Python lists that can be extended anytime (no append() in SciPy arrays) .

The shape of an array can be retrieved with the method shape:
data = scipy.ones((100,3), dtype=int

print data.shape
The shape of an array can be changed with the method reshape(...)
data = scipy.arange(50)

data.reshape((2,25))

print data.shape
Note that the total size of the array is unchanged 2*25 = 50.

© ADVANCED RESOURCES AND RISK TECHNOLOGY 42


SciPy indexing and slicing

Accessing a multidimensional arrays is done with the [ , , ] operator


x = scipy.arange(50).reshape((2,25))

print x[0,12]

print x[0,:], x[0,:].shape

print x[0,3:12]

Submatrices
The : operator allows slicing an array and define a subblock within an array.

Be careful that slicing is not copying, see for instance :
x = scipy.arange(50).reshape((2,25))

y = print data[0,:]

y[0] = -99

print x

© ADVANCED RESOURCES AND RISK TECHNOLOGY 43


Slicing

An array of bool or integer can also be used for slicing :


x = scipy.arange(25) # integers from 0 to 24

id = x < 10 # Test each element of x to see if True or False

print id.shape # same shape as x

print id # True or False array

print x[id] # integers from 0 to 9

id1 = scipy.array([0,1,6,9]) # specific integer values

print x[id1].shape # 4 elements only

print x[id1] # 4 elements of x located at positions in id1

© ADVANCED RESOURCES AND RISK TECHNOLOGY 44


ScyPy Statistical module

from scipy import stats


Generating uniform random number between 0,1:
r = scipy.random.uniform(0,1,100)

print ’mean: ’, scipy.mean(r),’ Median: ’,scipy.median(r),\

’ std: ’, scipy.std(r)
Sort the random sequence in place
print ’Index of the minimum: ’, r.argmin()

r.sort()

print ’Index of the minimum: ’, r.argmin()
Get the inverse of distribution
x = scipy.stats.norm.cdf(0.1)

print x

print scipy.stats.norm.ppf(x)

© ADVANCED RESOURCES AND RISK TECHNOLOGY 45


5 – Geostatistics with Python

© ADVANCED RESOURCES AND RISK TECHNOLOGY 46


Python with AR2Gems

To access the Python console go to Scripts


> Show Script Editor

The top part of the console is the Python


interpreter, the bottom part is the script output
window (for error outputs, or for print
commands). The Run button or the F5
keyboard shortcut executes the entire script.

The console offers the customary File functions


(New, Open, Save, Save As) and Edit
operations (Copy, Cut, Paste, Undo, Redo).

To access functions inside ar2gems, you have


to import the sgems module first:

import sgems

© ADVANCED RESOURCES AND RISK TECHNOLOGY 47


Python commands in AR2Gems

import sgems

execute(string_command) 

Run in the Python interface any command that you would normally run from the command prompt. Use ‘single quotes’ to delimit the
command string. Returns the number of arguments received by the process. This is a very useful and versatile Python command.
get_property(string_gridname,string_property)

Returns a property vector
set_property(string_gridname,string_property,tuple_values)

Change or create a property of a grid
get_dims(string_gridname)

Get ni,nj,nk dimension of a regular grid
get_grid_size(string_gridname)

Get the number of blocks of a grid
set_region(string_gridname,string_region, tuple_values)

Import a region to a grid
get_region(string_gridname,string_region)

Export a region from a grid
set_active_region(string_gridname,string_region)

Select an active region on a grid (NONE unselect region)

© ADVANCED RESOURCES AND RISK TECHNOLOGY 48


Python commands in AR2Gems

nan()

Return the SGeMS value for NaN
get_property_list(string_gridname)

Return the list of property name in a grid
get_location(string_gridname,integer_nodeid)

Return the x,y,z location of a grid based on the nodeid
get_nodeid(string_gridname, float_x, float_y, float_z)

Return the nodeid from a x,y,z location
get_closest_nodeid(string_gridname, float_x, float_y, float_z)

Return the closest nodeid from a x,y,z location

© ADVANCED RESOURCES AND RISK TECHNOLOGY 49


Python commands in AR2gems

set_categorical_property_int(string_gridname,string_property,tuple_values)

Set a categorical property from a list of integer
set_categorical_property_alpha(string_gridname,string_property,string_catdefinition,tuple_values)

Set a categorical property from a list of alphanumeric entries (string)
get_categorical_definition(string_gridname,string_property)

Get the categorical definition from a categorical property
get_properties_in_group(string_gridname,string_group)

Get the name of the member property for a group
new_point_set(string_pointsetname, tuple_float_x, tuple_float_y, tuple_float_z)

Create a new point set given a set of x,y,z coordinates

The coordinates of all nodes in a grid can be obtained by getting the standard properties _X_, _Y_ or _Z_

© ADVANCED RESOURCES AND RISK TECHNOLOGY 50


Example
Ex. 3: Gain
2: vertical
familiarity
trend
with our data set

© ADVANCED RESOURCES AND RISK TECHNOLOGY 51


References

https://developers.google.com/edu/python/
http://en.wikipedia.org/wiki/Python_%28programming_language%29
Unofficial Windows Binaries for Python Extension Packages

http://www.lfd.uci.edu/~gohlke/pythonlibs/
Data Analysis:

http://pandas.pydata.org/
Windows
o Do not use Notepad and Wordpad
o Free and Open-Source:
o Notepad++: http://notepad-plus-plus.org/
o Jedit: http://www.jedit.org/
Mac
o TextWrangler: http://www.textwrangler.com/products/textwrangler/ (free)
o Jedit
Unix
o Any text editor works (vi, pico, Kate, Nedit)

© ADVANCED RESOURCES AND RISK TECHNOLOGY 52

You might also like