Programming for Geographical Information Analysis: Advanced Skills

Post on 03-Feb-2016

35 views 0 download

Tags:

description

Programming for Geographical Information Analysis: Advanced Skills. Lecture 7: Libraries II: Science Dr Andy Evans. Things we might want to do. Mathematics: Algebra; calculus; vector and matrix mathematics; etc. Statistics: Hypotheses testing; sample comparisons; regression; etc. - PowerPoint PPT Presentation

Transcript of Programming for Geographical Information Analysis: Advanced Skills

Programming for Geographical Information Analysis:

Advanced Skills

Lecture 8: Libraries II: ScienceDr Andy Evans

Things we might want to do

Mathematics:Algebra; calculus; vector and matrix mathematics; etc.

Statistics:Hypotheses testing; sample comparisons; regression; etc.

Graph theory/Network analysis:Network form analysis; statistics of centrality etc.; flow efficiency.

Text processing:Parsing; Natural Language Processing; Statistics.

MathematicsStatisticsGraphs and NetworksText and Language

Mathematics

The classic texts for scientific computing are the Numerical Recipes books.

Java code available to buy:http://www.nr.com/aboutJava.html

Numerical Recipes

For java there is also Hang T. Lau (2003) A Numerical Library in Java for Scientists and Engineers

Colthttp://acs.lbl.gov/software/colt/JSciencehttp://www.jscience.org/JAMA http://math.nist.gov/javanumerics/jama/

Maple

Commercial mathematics application (commercial licence $2,845).Does, for example, algebraic manipulation, calculus, etc.Outputs processes as C, C#, Java, Fortran, Visual Basic, and MATLAB code.C and Java APIs for program connection.

Mathematics

StatisticsGraphs and NetworksText and Language

Statistics

R (GNU):http://www.r-project.org/

Developed as a free version on the stats language S, combined with a functional programming language.

Programming languages

We’ve dealt with Imperative Programming languages: commands about what to do to change the state of the program (i.e. its collected variables).

These are usually also Procedural, in that the program is divided into procedures to change states.

Most Procedural languages are now Object Orientated.

Programming languagesThe other branch of languages allow Declarative Programming: concentrates on describing what a program should do, not how, and avoiding state changes.

Clearest examples are Functional Programming: everything is described as a reference to another function:

a = x + 10;x = y + 2;Run program for the argument y = 12

Also Logical Programming: same kind of thing but based on finding logical proofs/derivations.

Things that fall into the category mortal includes humans.Socrates is human.Run program to find if Socrates is mortal?

Declarative languages

Examples: Lisp; Prolog; (bits of SQL)

Beloved of academics, but weren’t used much in the real world, until recently (except SQL).

Advantage is that they avoid unlimited internal and external state changes, therefore much easier to check and predict.

Prolog useful for language processing.

A version of Lisp, Scheme, inspired elements of R.

R

Language and a series of packages.

Written in C/C++/Fortran but Java can be used.

Functional language but with procedural and OOP elements.

Uses scalars, matrices, vectors, and lists.

Can replace the GUI with a variety of alternatives.

Powerful and increasingly stats software of choice, but steep learning curve and massive range of add-on packages.

RGui

Packages

Lots come with it.Comprehensive R Archive Network (CRAN):http://cran.r-project.org/web/packages/available_packages_by_name.html

Packages → Set CRAN Mirror…

Packages → Install package(s)…

library() : list packageslibrary(packageName) : load package for uselibrary(help = packageName) : what’s in a packagedetach("package:packageName") : unload

Example

data1 <- read.csv("m:\\r-projects\\data.tab", header = TRUE)

attach(data1)

plot(Age, Desperation, main="Age vs. Desperation")

lineeq <- lm(Desperation ~ Age, data=data1)

x <- seq(min(Age), max(Age), by=10.0)

newData <- data.frame(Age = x)

predictions <- predict(lineeq, newdata = newData)

lines(Age, predictions)detach(data1)rm (data1, lineeq, newData, predictions, x)

Working with R

R uses ‘Workspace’ directories.Good practice to work in a new directory for each project (File → Change Dir…)

Dataset names etc. must have a letter before any numbers.

R constructs data objects, that can be seen with objects() and removed with rm(objectName).If you save the workspace, it saves these objects in an .RData file.

Working with R

Commands can be separated by new lines or enclosed thus: {command;command;command}

If you fail to close a command, you’ll see “+”.

You can load scripts of commands. Note that on Windows you just have to be careful to adjust all filepaths, thus:source("c:\\scripts\\commands.r")

Orsource("c:/scripts/commands.r")

The scripts are just text files of commands.

Quick tipsSimplest data structure is the vector of datax <- c(10.4, 5.6, 3.1, 6.4, 21.7)

attach() makes data available by column name (cp. detach(name)).

Vector elements can be searched and selected using indices or expressions in [], e.g.:y <- x[!is.na(x)] where na is “Not available”

In operations using 2 vectors, the shortest gets cycled through as much as is needed.

Other data structures

Matrices or more generally arraysFactors (handle categorical data).Vectors or lists (latter can be recursive)Data frames – tables of dataFunctions (store code)

Each data element is assigned a mode, or data type: logical, numeric, complex, character or raw.

Quick tips

$ can be used to look inside objects, e.g. myData$column1

Operators: +, -, *, / and ^ (i.e. raise to the power)

Functions include: log, exp, sin, cos, tan, sqrt, max, min, length, sum, mean, var (variance), sort.

Help

Best start is “Introduction to R”:http://cran.r-project.org/doc/manuals/R-intro.html

?solve : help for solve functionhelp.start() : start the HTML help??solve : search help for solve?help : info other help systems

R-Spatial

Large number of packages dealing with spatial analysis, including:

Mapping (incl. GoogleMap/Chart, and KML production)Point pattern and cluster analysis.Geographically Weighted Regression.Network mathematics.Kriging and other interpolation.

Excellent starting point is James Cheshire’s (CASA) :http://spatialanalysis.co.uk/r/

Non-package addons

R-Forge:http://r-forge.r-project.org/

GUIs, bridges to other languages, etc.

Programming R

Has its own flow control:if ( condition ) {

statement 1

} else {

statement 2

}

for (i in 1:3) print(i)

Note that this is actually a “for-each” loop - “:” just generates a list of numbers, so you can also do this:x <- c("Hello","World")

for (i in x) print(i)

Programming with R

Various options, but best is rJava:http://cran.r-project.org/web/packages/rJava/index.html

Two parts:rJava itself : lets R use Java objects.JRI (Java/R Interface) : lets Java use R.

JRI

Start by setting up an Rengine object.Can run it with or without an R prompt GUI.

Send in standard R commands using Rengine’s eval(String) method.

Can also assign () various values to a symbol re.assign(“x”, “10.0,20.0,30.0”);

Methods for dealing with GUI elements (see also the iPlot and JavaGD packages).

Getting data back

Two mechanisms:

Push: Get back an object containing the information R would have output to the console (and a bit more).

Callback:Java provides methods which R calls when different tasks done.

Push

Get back a REXP object:

Contains R output and other information.

rexp.toString() : shows content.

Can filter out information with:rexp.asDoubleArray()

rexp.asStringArray()

etc.

Callback

Add an object to handle events:rengine.addMainLoopCallbacks(RMainLoopCallbacks)

Largely set up to manage user interface interaction.

RMainLoopCallbacks contains methods called at key moments, for example:rReadConsole()

Called while R is waiting for user input.

Floating point numbers

Be aware that floating point numbers are rounded.

For example, in R, floating point numbers are rounded to (typically) 53 binary digits accuracy.

This means numbers may differ depending on the algorithm sequence used to generate them.

There is no guarantee that even simple floating point numbers will be accurate at large decimal places, even if they don’t appear to use them.

Floating point numbers

David Goldberg (1991), “What Every Computer Scientist Should Know About Floating-Point Arithmetic”, ACM Computing Surveys, 23/1, 5–48 http://www.validlab.com/goldberg/paper.pdf

http://floating-point-gui.de/

Hacker's Delight by Henry S. Warren Jr

Randall Hyde’s “Write Great Code” series.

http://ta.twi.tudelft.nl/users/vuik/wi211/disasters.html

Mathematics

Statistics

Graphs and NetworksText and Language

Graph/Network maths

Graph theory deals with networks as mixes of nodes and vertices (edges).Was limited to relatively simple graphs until more data on links and more processing power.Now huge research and development area.

Network statistics

Distribution/average of node degree (edges connected).

Distances:Eccentricity: distance from a node to the node furthest from it.Average path length: average eccentricity.Radius: minimum eccentricity in the graph.Diameter: maximum eccentricity in the graph.

Global clustering: how many nodes are connected in complete connection triangles (triadic closures) as a proportion of the connected triplets in the graph.

Other key statistics

Centrality: various measures, including degree, but two are:Betweenness centrality: number of shortest paths passing through a node.Closeness centrality: average of shortest paths to all other nodes.

Node degree (or other) correlation: how similar are nodes to their neighbours?

SoftwareMasses of software E.g. Inflow

Network CentralitySmall-World Networks Cluster Analysis Network Density Prestige / Influence Structural Equivalence Network Neighborhood External / Internal Ratio Weighted Average Path Length Shortest Paths & Path Distribution

http://en.wikipedia.org/wiki/Social_network_analysis_software

Pajek - for Large Network Analysishttp://pajek.imfm.si/doku.php?id=pajek

Programming GraphsGUESS (Open Source Java program)http://graphexploration.cond.org/Nicely uses GraphML, XML for representing graphs.

JUNG libraryhttp://jung.sourceforge.net/

R: various packages, including igraph.

MathematicsStatistics

Graphs and Networks

Text and Language

Text analysis

Processing of text.

Natural language processing and statistics.

Processing text: Regex

Java Regular Expressionsjava.util.regex

Regular expressions:Powerful search, compare (and replace) tools.

(other types of regex include direct replace options – in java regex these are separate methods)

Regex

Standard java:

if ((email.indexOf(“@” > 0) &&

(email.endsWith(“.org”))) {

return true;

}

Regex version:

if(email.matches(“[A-Za-z]+@[A-Za-z]+\\.org”)) return true;

Example components[abc] a, b, or c (simple class) [^abc] Any character except a, b, or c (negation) [a-zA-Z] a through z, or A through Z, inclusive (range) [a-d[m-p]] a through d, or m through p: [a-dm-p] (union) [a-z&&[def]] d, e, or f (intersection) [a-z&&[^bc]] a through z, except for b and c: [ad-z] (subtraction) [a-z&&[^m-p]] a through z, and not m through p: [a-lq-z] (subtraction). Any character (may or may not match line terminators) \d A digit: [0-9] \D A non-digit: [^0-9] \s A whitespace character: [ \t\n\x0B\f\r] \S A non-whitespace character: [^\s] \w A word character: [a-zA-Z_0-9] \W A non-word character: [^\w]? Once or not at all* Zero or more times+ One or more times

Matching

Find all words that start with a number.

Pattern p = Pattern.compile(“\\d\\.*”);

Matcher m = p.matcher(stringToSearch);

while (m.find()) {

String temp = m.group();

System.out.println(temp);

}

Replacing

replaceFirst(String regex, String replacement)

replaceAll(String regex, String replacement)

Regex

Good start is the tutorial at:http://docs.oracle.com/javase/tutorial/essential/regex/

Also Mehran Habibi’s Java Regular Expressions.

Natural Language Processing

A large part is Part of Speech (POS) Tagging:Marking up of text into nouns, verbs, etc., usually based on the location in the text and other context rules.

Often formulates these rules using machine-learning (of various kinds), training the program on corpora of marked-up text.

Used for :Text understanding.Knowledge capture and use.Text forensics.

NLP Libraries

Popular are:

Natural Language Toolkit (NLTK; Python)http://www.nltk.org/

OpenNLP (Java)http://opennlp.apache.org/index.html

OpenNLP

Sentence recognition and tokenising.Name extraction (including placenames).POS Tagging.Text classification.

For clear examples, see the manual at:http://opennlp.apache.org/documentation.html

Other info

Other than the Numerical Recipes books, the other classic texts are Donald E. Knuth’s The Art of Computer ProgrammingFundamental Algorithms Seminumerical Algorithms Sorting and SearchingCombinatorial Algorithms

But at this stage, you’re better off getting…

Other infoMichael T. Goodrich and Roberto Tamassia’s Data Structures and Algorithms in Java.

Basic java, arrays and list.Recursion in algorithms.Key mathematical algorithms.Algorithm analysis.Data storage structures (stacks, queues,

hashtables, binary trees, etc.)Search and sort.Text processing.Graph/network analysis.Memory management.

Next Lecture

Modelling I: Netlogo

PracticalRrJavaNetwork visualisation