Archive

Archive for the ‘python’ Category

homebrew + python

December 30th, 2011 rupert No comments

Read http://docs.python-guide.org/en/latest/starting/installation/

Installation

/usr/local/Cellar% brew install python --framework
==> Downloading http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tar.bz2
File already downloaded in /Users/rupert/Library/Caches/Homebrew
==> Patching
patching file Lib/whichdb.py
Hunk #1 succeeded at 91 with fuzz 1.
==> ./configure --prefix=/usr/local/Cellar/python/2.7.2 --enable-framework=/usr/local/Cellar/python/2.7.2/Frameworks
==> make
==> make install
==> Downloading http://pypi.python.org/packages/source/d/distribute/distribute-0.6.24.tar.gz
File already downloaded in /Users/rupert/Library/Caches/Homebrew
==> /usr/local/Cellar/python/2.7.2/bin/python setup.py install
==> Caveats
A "distutils.cfg" has been written to:
  /usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils
specifing the install-scripts folder as:
  /usr/local/share/python
 
If you install Python packages via "python setup.py install", easy_install, pip,
any provided scripts will go into the install-scripts folder above, so you may
want to add it to your PATH.
 
Distribute has been installed, so easy_install is available.
To update distribute itself outside of Homebrew:
    /usr/local/share/python/easy_install pip
    /usr/local/share/python/pip install --upgrade distribute
 
See: https://github.com/mxcl/homebrew/wiki/Homebrew-and-Python
 
Framework Python was installed to:
  /usr/local/Cellar/python/2.7.2/Frameworks/Python.framework
 
You may want to symlink this Framework to a standard OS X location,
such as:
    mkdir ~/Frameworks
    ln -s "/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework" ~/Frameworks
==> Summary
/usr/local/Cellar/python/2.7.2: 4808 files, 77M, built in 86 seconds
brew install python --framework  76.78s user 21.49s system 112% cpu 1:27.00 total

Symlinks

The following links were created below to ensure that 2.7 is the latest Python picked up during installation of other software (i.e postgresql)

sudo ln -s /usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7 /System/Library/Frameworks/Python.framework/Versions/2.7
sudo ln -s /usr/local/share/python/easy_install /usr/bin/easy_install
sudo ln -s /usr/local/share/python/easy_install-2.7 /usr/bin/easy_install-2.7
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /usr/bin/python2.7
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config /usr/bin/python2.7-config
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/pydoc2.7 /usr/bin/pydoc2.7
sudo ln -s /usr/bin/python2.7 /usr/bin/python
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7 /System/Library/Frameworks/Python.framework/Versions/Current
mkdir -p /Library/Python/2.7
ln -s /usr/local/lib/python2.7/site-packages /Library/Python/2.7/site-packages

Site-Packages. Where?

Note that site-packages will be installed in

/usr/local/lib/python2.7/site-packages
~% ls -la /Library/Python/2.6/site-packages
total 136
drwxrwxr-x  14 root    admin    476 30 Dec 18:13 ./
drwxrwxr-x   4 root    admin    136 30 Dec 18:11 ../
-rw-r--r--@  1 rupert  admin   6148 30 Dec 18:13 .DS_Store
-rw-rw-r--   1 root    admin    119 11 Feb  2010 README
-rw-r--r--   1 rupert  admin    241 30 Dec 18:01 easy-install.pth
-rw-r--r--   1 rupert  admin   3129 30 Dec 18:02 googlemaps-1.0.2-py2.6.egg-info
-rw-r--r--   1 rupert  admin  19703 16 Oct  2009 googlemaps.py
-rw-r--r--   1 rupert  admin  19153 30 Dec 18:02 googlemaps.pyc
drwxr-xr-x  39 root    admin   1326  7 Feb  2010 mod_python/
-rw-r--r--   1 root    admin    267  7 Feb  2010 mod_python-3.3.2_dev_20080819-py2.6.egg-info
drwxr-xr-x   8 rupert  admin    272 30 Dec 18:13 nominatim/
-rw-r--r--   1 rupert  admin   4462 30 Dec 18:08 nominatim-0.90-py2.6.egg
drwxr-xr-x  15 rupert  admin    510 30 Dec 18:01 simplejson/
drwxr-xr-x   4 rupert  admin    136 30 Dec 18:13 simplejson-2.3.1-py2.6.egg/
~% ls -la /Library/Python/2.7/site-packages
lrwxr-xr-x  1 root  admin  38 30 Dec 18:23 /Library/Python/2.7/site-packages@ -> /usr/local/lib/python2.7/site-packages
~% which python
/usr/local/bin/python
~% python --version
Python 2.7.2

Removing Point Outliers

November 19th, 2007 rupert No comments

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.

2007-11-19_102428.png

Here the original dots are marked as red, while the final dots after removing the outliers were colored as green.

Categories: python Tags:

PyDev Plugin For Eclipse

November 3rd, 2007 rupert No comments

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/.

PyDev Plugin For Eclipse

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″.

Python Config

Categories: python Tags: ,

Variable scoping in Python

October 27th, 2007 rupert No comments

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
>>>

Categories: python Tags:

Diving into Python

August 12th, 2007 rupert No comments

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.

Categories: python Tags: