The SPIDER Python Library |
Overview
Installation
Example functions: readdoc, writedoc, and makeSpiderFilename
The Spider Python Library : Spiderutils.py
Coding Examples
The Python Imaging Library
Displaying images with Tkinter
Array operations : Spiderarray.py
Spire compatibility : Spiderscripts.py
The SPIDER Python Library was developed to provide functions for handling SPIDER files in your Python programs. In particular, your scripts can read or write Spider document files in a single line. Data columns from doc files can be treated as arrays. The Python Imaging Library provides general image processing operations for SPIDER images, and lets you display them in Tkinter. Numerical array operations may also be applied to SPIDER images and volumes.
Note: The SPIDER Python Library does NOT let your scripts call SPIDER operations, as in
output = AP_MQ(input arguments...)
For that, you need the Python Wrapper for SPIDER (under development).
Python vs SPIDER batch files
There are some differences to keep in mind:
ENDIF or LB1.
"file001.dat", not FILE001.
There are a few Python tutorials out there:
Instant Python,
various Python tutorials, and the Python web site has a few pointers
The conventions below:
Function names are bold.
Function keywords are inside parentheses.
Keywords in square brackets are optional, and have default values. That means if the definition looks like:
readdoc (filename [, column=1, keys=0]),
you can use
readdoc(filename), and it assumes you mean readdoc(filename, column=1, keys=0).
The Python prompt, >>>, assumes you have started Python and imported the Spider Python Library:
% python Python 2.3.3 (#9, May 25 2004, 10:53:39) [C] on irix6 >>> from Spider.Spiderutils import * >>> # type python commands at the prompt
Download the file SpiderPythonLibrary-1.0.tar.gz from http://www.wadsworth.org/spider_doc/spider/download.
Extract the files:
gunzip SpiderPythonLibrary-1.0.tar.gz
tar xvf SpiderPythonLibrary-1.0.tar
cd SpiderPythonLibrary-1.0
Run setup.py install (or just copy the Spider/ directory to your Python site-packages directory.)
'all' (in quotes)
;spi/hcc 06-OCT-2004 AT 10:53:47 defocus.hcc
; / MICROGRAPH DEFOCUS ASTIG.ANG ASTIG.MAG
1 5 1.0000 36797. 88. 196.0
2 5 7.0000 31989. -77. 151.0
3 5 14.000 23748. 53. 85.0
4 5 16.000 21499. -74. 496.0
5 5 27.000 14098. 33. 336.0
>>> readdoc('defocus.hcc') # same as readdoc('defocus.hcc', column=1)
[1.0, 7.0, 14.0, 16.0, 27.0]
>>> readdoc('defocus.hcc', column=2)
[36797.0, 31989.0, 23748.0, 21499.0, 14098.0]
>>> readdoc('defocus.hcc', column=2, keys=1)
{1: 36797.0, 2: 31989.0, 3: 23748.0, 4: 21499.0, 5: 14098.0}
>>> # the output is a Python dictionary - items are accessed by key:
>>> d = readdoc('defocus.hcc', column=2, keys=1)
>>> d[3]
23748.0 # for key=3: the value from column 2
>>> d = readdoc('defocus.hcc', keys='all')
>>> d[5]
[27.0, 14098.0, 33.0, 336.0] # all the values for key=5
NB: this function only works for document files from Spider version 11.0 or later (with data separated by spaces).
readdoc)
>>> A = [1.0, 7.0, 14.0, 16.0, 27.0] # create a list of numbers
>>> B = [36797, 31989, 23748, 21499, 14098] # another list
>>> hdrs = ['mics', 'defocus'] # a list of strings for column headings
>>> writedoc('file001.dat', columns=[A,B], headers=hdrs)
produces the file, file001.dat:
;dat/dat 03-MAR-2006 AT 15:28:34 file001.dat
; / mics defocus
1 2 1 36797
2 2 7 31989
3 2 14 23748
4 2 16 21499
5 2 27 14098
>>> writedoc('file002.dat', lines=[A,B])
produces the file, file002.dat:
;dat/dat 03-MAR-2006 AT 15:29:50 file002.dat
1 5 1 7 14 16 27
2 5 36797 31989 23748 21499 14098
Remember that columns (and lines) must refer to a list of lists.
>>> makeSpiderFilename('mic***.dat', 123)
'mic123.dat'
>>> makeSpiderFilename('tmp/mic****.gtp', 23)
'tmp/mic0023.gtp'
>>> makeSpiderFilename('mic***.dat', 123456)
'mic123456.dat'
>>> getfilenumber("mic0089.ext")
'0089' # note, output is a string
>>> int(getfilenumber("mic0089.ext"))
89 # output converted to integer