1 Introduction to Programming for Scientists Lecture 7 Prof. Steven Ludtke N410, 12 Homework Review import os, sys from PIL import Image import ImageF...

Author:
Virgil Freeman

0 downloads 28 Views 305KB Size

Prof. Steven Ludtke N410, [email protected] Wednesday, April 13, 2011

1

Homework Review import os, sys from PIL import Image import ImageFilter directory='G:/pictures' files=os.listdir(directory) for i in files: ! filepath=directory+'/'+i ! im=Image.open(filepath) ! filename,ext=os.path.splitext(filepath) ! im2=im.filter(ImageFilter.BLUR) ! im2.save(filename + ".png","PNG")

Wednesday, April 13, 2011

2

import glob import Image a=raw_input("x resize?") b=raw_input("y resize?") l=glob.glob('*.jpg') for fname in l: ! Image.open(fname).resize((int(a),int(b))).save('%s.png'%fname[:-4])

Wednesday, April 13, 2011

3

pickle/shelve from pickle import dump,load,dumps,loads dump(obj,file)!!

!

# stores ‘obj’ in ‘file’

obj=load(file)!!

!

# restores ‘obj’ from file

str=dumps(obj)! !

# pickled representation of obj

obj=loads(str)!!

!

# restore representation of obj

import shelve!!

!

# dictionary-like object on disk

dic=shelve.open(filename) dic=shelve.open(filename,writeback=True) dic.close()

Wednesday, April 13, 2011

4

numpy http://www.tramy.us/

# numpy book

from numpy import * a=arange(60) b=a.reshape(10,6)

# make 2-D matrix

c=a.reshape(3,4,5) # make 3-D (tensor) b.shape

# current dimensions

b.size

# total number of elements

b.ndim

# dimensionality

b.dtype

# type of value stored

b.astype(“”)

Wednesday, April 13, 2011

5

numpy a=zeros((nx,ny,...)) a=fromfunction(lambda i,j:i+j,(4,5)) a=arange(0,20,.1) a*10

#

# multiply each element !

b=sin(a) # sin() of each element c=a[c>0] c.sort()

# condition, returns elements >0 # sort values in-place

c.mean(),var(),std(),prod()

# average, variance, standard dev, product

inner(a,b), outer(a,b)

# inner and outer matrix products

dot(a,b), cross(a,b)

# dot and cross products (similar to above)

histogram(a,bins,range)

# compute a histogram of ‘a’

from PIL import Image im=Image.fromarray(a,”L”) # initialize a PIL image with a 2D array

Wednesday, April 13, 2011

6

scipy Clustering package (scipy.cluster) Constants (scipy.constants) Fourier transforms (scipy.fftpack) Integration and ODEs (scipy.integrate) Interpolation (scipy.interpolate) Input and output (scipy.io) Linear algebra (scipy.linalg) Maximum entropy models (scipy.maxentropy) Miscellaneous routines (scipy.misc) Multi-dimensional image processing (scipy.ndimage) Orthogonal distance regression (scipy.odr) Optimization and root finding (scipy.optimize) Signal processing (scipy.signal) Sparse matrices (scipy.sparse) Sparse linear algebra (scipy.sparse.linalg) Spatial algorithms and data structures (scipy.spatial) Special functions (scipy.special) Statistical functions (scipy.stats) Image Array Manipulation and Convolution (scipy.stsci) Wednesday, April 13, 2011

7

matplotlib (pylab) Matlab-like plotting library http://matplotlib.sourceforge.net/users/pyplot_tutorial.html ipython --pylab

# special mode for interaction with pylab

x=arange(0,4*pi,0.05)

# from numpy

y=sin(x)

# easy to apply a function to a list of values

plot(x,y)

# plot x,y and open a display window

python from pylab import *

# <-- only if you don’t use ipython

x=arange(0,4*pi,0.05) y=sin(x)

# easy to apply a function to a list of values

plot(x,y)

# plot x,y and open a display window

show()

# opens the plot window (blocks on some machines)

Wednesday, April 13, 2011

8

matplotlib (pylab) ipython --pylab

# special mode for interaction with pylab

x=arange(0,4*pi,0.05)

# from numpy

y=sin(x)

# easy to apply a function to a list of values

y2=cos(x) figure(1)

# start a new figure

subplot(211)

# make a 1x2 set of plots and move to 1st

plot(x,y)

# plot x,y and open a display window

ylabel(“sin(x)”) subplot(212)

# start on the 2nd subplot

plot(x,y2)

# second plot

ylabel(“cos(x)”) xlabel(“x”)

Wednesday, April 13, 2011

9

Motorola 6800 CPU

72 instructions (197 opcodes)

8 bit data bus (0-255)

16 bit address bus (64k max RAM)

6 registers: −

8 bit ACCA

−

8 bit ACCB

−

16 bit IX

−

16 bit PC

−

16 bit SP

−

6 bit CC

Wednesday, April 13, 2011

10

CPU Communications CPU Address Bus

0000 0000 0000 0000 0000 0000

Wednesday, April 13, 2011

0000 0000 0000 0000 0000 0000

0000 0000 0000 0000 0000 0000

0000 0001 0010 0011 0100 0101

R/ W

Data Bus

1010 1100 0000 1000 0000 1000

0110 1110 0111 0000 0000 0110

11

Communications Are computers useful without i/o ? KB/Mouse, video, sound, printer, digital camera, other computers, scanner, TV tuner, home control, specialized equipment (micromill, 3-D printer, telescope control, etc.), scientific instruments (!) Memory mapping Polling vs Interupts

Wednesday, April 13, 2011

12

http://commons.wikimedia.org/wiki/Image:Typical_intel_chipset.jpg Wednesday, April 13, 2011

13

PCIe (1x/16x) PCI (32/64) AGP (1x-8x)

Speed

Range

(Mb/s)

(m)

250/4000 internal 133

internal

266-2133* internal

description

connector

16x mainly for video 64 not widely used largely obsolete, for video

network (10/100/1000)

1/12/120

100

1000=gigabit, 10G exists

802.11 (a/b/g/n)

6/1/6/30

120//250

N is the new standard, most still use G

wireless

bluetooth

0.3

~10

short range device comm

wireless

IRDA

.01-1(?)

1

bluetooth instead

wireless

4.5/?

external disks, video

5/5

external disks, etc.

15

pre-USB for printers

USB (1/2/3*)

50/100 (400?) 2/57/570

Parallel

1

Serial

0.01

old-style modems, specialty devices

PS/2

n/a

keyboard & mouse

firewire/1394 (400/800)

Wednesday, April 13, 2011

14

Disk Interfaces Speed (Mb/s) SATA

300/600

IDE/ATA

3-133

Fibre Channel

100-400

SCSI

5-640

firewire/1394 (400/800)

50/100(400?)

USB (1/2/3*)

2/57/570

Actual Disk Perf.

80-150, 200-300

Memory Bandwidth

~30,000

Wednesday, April 13, 2011

connector

optical, long range (>50km) many connectors, external, short range

15

Video Interfaces HDCP

Description

HDMI

X

High definition (digital) consumer video (and audio)

DVI

X

Newer style computer video, digital, supports HDCP,

VGA

Old style computer video, supports high resolution, but analog

Component

Further improved quality, supports higher resolutions

S-video

Improved quality, still 640x480

Composite

Oldest style consumer video, poor quality, poor resolution (640x480)

Wednesday, April 13, 2011

Connector

16

Homework 7 1.

We’re now going to take the program from homework 5 and make it work with matplotlib. You can use your original solution to homework 5 as a starting point. You have 4 tasks: 1.

Many of you didn’t store the x/y data internally in the object you created. While this was ok for homework 5, please correct it for this homework assignment, as we are going to be extending this object in future homework assignments. That is, please ‘fix’ your object so the data is stored in the object and not taken as parameters to the various methods. Also make sure there is a separate method (other than just __init__) for reading data from disk.

2.

If neceesary, change the data storage of the x/y data from Python lists to use numpy arrays.

3.

Modify the histogram method so it makes use of matplotlib to display the results of the histogram graphically. Feel free to redo the histogram calculation using capabilities of numpy/scipy as well, though this is not required.

4.

Add an additional method to plot the x/y data as a 2-D line plot using matplotlib.

Wednesday, April 13, 2011

17

Our partners will collect data and use cookies for ad personalization and measurement. Learn how we and our ad partner Google, collect and use data. Agree & Close