By Rupert
Posts tagged python
Jpeg decoder problem on TileCache on MacOSX Leopard
May 12th
There is a problem with a “jpeg decoder” because Python Imaging Library (PIL) cannot find the jpeg libraries. Remember that we installed kyngchaos UnixIO libraries, therefore the PIL setup.py script should point to use those libraries.
Actually, I even installed the jpegsrc manually. I believe there is no need to do this, since UnixIO Image libraries is sufficient enough for PIL to install. What is important is to ensure that python tests succeeded. Once you get passed that, then installing PIL should work.
1. Extract Imaging-1.1.6.tar.gz 2. Edit setup.py to reflect: JPEG_ROOT="/Library/Frameworks/UnixImageIO.framework/unix" 3. python setup.py build_ext -i 4. If no test fails, then go ahead and install 5. python setup.py install
Removing Point Outliers
Nov 19th
In my previous post to remove point outliers, I tried using R and PLR in PostGres. Although, I only scratched the surface on the spatial analyzing capabilities of R, I needed something more extensible for my internet purposes. I decided to use Python’s pragmatic benefits and ease in programming. Idea was to pull out the vector points from PostGIS, process it using an algorithm (ideally minimum convex hull but it could be expensive later on) and then remove the outliers.
Numpy, a scientific python library, blends easily by using basic functions for mathematical array computations such as mean, median, standard deviation and variance. For now, the algorithm takes a 90% threshold, taken from “Dealing with ‘Outliers’: Maintain Your Data’s Integrity”
Consider this collection of 10 scores, sorted from smallest to largest: x 8 25 35 41 50 75 75 79 92 129 ^ The median of these 10 values of x is 62.5, computed as (75+50)/2. Next, calculate the absolute value of the deviation of original data from median: x med abs_dev 50 62.5 12.5 75 62.5 12.5 75 62.5 12.5 79 62.5 16.5 41 62.5 21.5 ->| 35 62.5 27.5 ->| MEDIAN(abs_dev) = 24.5 = (21.5+27.5)/2 92 62.5 29.5 25 62.5 37.5 8 62.5 54.5 129 62.5 66.5 Next, compute a test statistic which is the column of absolute values computed above, divided by the mediate of the absolute values: Test Stat = abs_dev / (Med of abs Dev) Med of Test x Median abs_dev abs dev Statistic Outlier? 8 62.5 54.5 24.5 2.22449 25 62.5 37.5 24.5 1.53061 35 62.5 27.5 24.5 1.12245 41 62.5 21.5 24.5 0.87755 50 62.5 12.5 24.5 0.51020 75 62.5 12.5 24.5 0.51020 75 62.5 12.5 24.5 0.51020 79 62.5 16.5 24.5 0.67347 92 62.5 29.5 24.5 1.20408 129 62.5 66.5 24.5 2.71429 Yes The decision rule then is to compare this test statistic with an arbitrary cutoff point. A cutoff of 2.5 is conservative; 4.5 or 5 is more rigorous. If the Test Statistic > Critical value (=2.5), then define the observed value as an outlier. According to this cutoff value, the data above have one outlier (x=129).
Implementing this in Python…
P = 116.32977 39.905319,116.329906 39.90464,116.329907 39.90464,116.329918 39.904675,116.330047 39.904683
multipoints = getPointsString() print multipoints pobj = getPointArray(multipoints) p = pobj.p; x = pobj.x; y = pobj.y; #print "Median:", median(p) #print "Std:", p.std(axis=0) #print "Min:", p.min(axis=0) #print "Max:", p.max(axis=0) pmed = median(p) pdev = p - pmed pdev_abs = abs(pdev) med_pdev = median( pdev_abs ) pfinal = pdev_abs / med_pdev
Where getPointsString() = “116.32977 39.905319,116.329906 39.90464,116.329907 39.90464,116.329918 39.904675,116.330047 39.904683..” a list of point geometries. We can easily get the median, std, and even minimum (min) and maximum (max) values in the array.
Here the original dots are marked as red, while the final dots after removing the outliers were colored as green.
PyDev Plugin For Eclipse
Nov 3rd
I had “indentation” problems a few times when I was doing Python using vim. Also, my chinese characters were not displaying correctly on VIM as well. But when I open the file in IDLE, I can read the chinese characters fine. Well I found a quick workaround for my indent problem by using the default Python Editor which is IDLE:
1. Edit -> Select ALL
2. Format -> Untabify Region
3. Specify “4″ spaces for the tabs.
4. To run your program, just press F5.
Well, just recently I managed some time and went to the ShowMeDo site for Python. There I found out about PyDev, please watch the screencast, its worth it! Firing Eclipse, I immediately added the plugin from the update site: http://pydev.sourceforge.net/updates/.
Once you have PyDev set, you need to tell Eclipse where your PYTHON bin, PYTHONPATH and other settings.
1. Go to Window -> Preferences
2. Collapse PyDev from left panel
3. Select “Interpreter-Python”
4. Click on “New” on the “Python Interpreters” Group.
5. Specify where python.exe.
Note: It would automatically add the libraries in your PYTHONPATH. Again, I insist you watch the screencast from Fabio to guide you through. To run your program just hit “F9″.
Variable scoping in Python
Oct 27th
In python, variables defined in a function is only local to that function.
def myfunct(): i = 1 return "Hello World" i = 0 print myfunct() print i
When you run the code above, it would spit out:
>>>
Hello
0
>>>
In order to manipulate a variable within the function defined in main, we need to use ‘global‘.
def myfunct(): global i i = 1 return "Hello World" i = 0 print myfunct() print i
When you run the code above, it would spit out:
>>>
Hello
1
>>>
Diving into Python
Aug 12th
Yup, Im now diving into Python. So far, Im loving it. Not as hard as java, very pragmatic programming. I got converted when I saw Awaretek’s “Python Learning Foundation” which I believe deserves a lot of merit for promoting Python. One of the podcasts from www.directionmag.com states that Python is #4 among GIS professional as a must-have progamming language experience.
Moving forward, I kickstarted with a few tutorials. Fast-reading most of the tutorials from http://www.upriss.org.uk/python/PythonCourse.html and trying out the exercises. It was fabolous. Simple yet powerful. Then I saw Alex Martelli’s tutorial from http://www.aleax.it/Python/py25.pdf. Those who have programming experience like Java/C, would benefit much from Martell’s PDF tutorial.
Next stop.. using databases with python.. Head on to the database section of AwareTek.
1. I’ve dowloaded Python’s DB Interface for Postgres ‘psycopg2‘. http://www.initd.org/tracker/psycopg/wiki/PsycopgTwo
2. Steve Holden’s dbPythonIntro.pdf. Presents a quick overview of the DB API. Important pages would be:
- 26/65 Connection Example (includes Postgres)
- 29/65 Connection Methods
- 30/65 Executing Queries
- 31/65 Retrieving Results
3. Accessing PostgreSQL with Python and Psycopg2. This three page tutorial will quickly give you a background of how to execute queries, fetch queries using column index or by names.

Comments