IDL to Numeric/numarray Mapping

Question

How do Interactive Data Language (IDL) commands translate into Numeric/numarray functions?

My Python Book Cover Image Advertisement: In 2012, I published the book A Hands-On Introduction to Using Python in the Atmospheric and Oceanic Sciences. If you're new to Python, you may find it useful!

Answer

Below is a summary mapping between IDL and numarray. Most of the mapping also applies to Numeric and NumPy. The vast majority of content on this page is a copy of the IDL to numarray mapping page (5 Nov 2002 version, copied 5 Jun 2004) from the Space Telescope Science Institute (STScI, the developers of numarray). Since they didn't seem to be adding anything more to the page, I decided to make a copy (it's in the public domain) and add additional topics. NumPy for IDL Users provides a more current mapping summary than this sheet.

In some cases, there are multiple Python equivalents for a given IDL construct. I also mention a few MLab and MA functions, since both modules are distributed with Numeric and numarray. Some of the descriptions are incomplete. I have not checked to see whether the descriptions correspond with the most recent versions of either IDL or numarray and there may be some errors.

Interpreter Management:

IDLPython Equivalent


.run <filename> import <module> (.py assumed)
reload(module) to recompile/re-execute
@filename execfile(filename)
exit <EOF> (e.g. ctrl-D)
up-arrow (cmd recall) up-arrow

Operators:

IDLPython Equivalent


<, > (clipping) Currently no operator equivalent. The functional equivalents are shown in the following examples.
a < 100. choose(greater(a, 100.),(a, 100.))
a > 100. choose(less(a, 100.),(a, 100.)) or for both clip(a, min_val, max_val). [For python 2.1 or greater, the following should work: choose(a < 100, (a, 100.))].
mod %
# (matrix multiply) multiply.outer() is equivalent for some applications, matrixmultiply() for others.
+, -, *, / Same
^ **

Boolean Operators:

Note: The Python Boolean operators (e.g. and, or) do not operate element-wise on arrays like they do in IDL. For element-wise comparison, use the logical_ prefixed ufuncs described below.

IDLPython Equivalent


and and, logical_and()
not not, logical_not()
or or, logical_or()
xor xor, logical_xor()

Bitwise Operators:

IDLPython Equivalent


and & (bitwise and)
or | (bitwise or)
xor ^ (bitwise xor)
not ~ (bitwise not)
ishft(a, 1) a << 1 (left shift)
ishft(a,-1) a >> 1 (right shift)

Comparison Operators:

The Python functional forms below are array ufuncs. The operator versions only work in Python 2.1. Use the functional form for Python 2.0

IDLPython Equivalent


eq ==, equal()
ge >=, greater_equal()
gt >, greater()
le <=, less_equal()
lt <, less()
ne !=, not_equal()

Slicing and Indexing:

Note: Order of indices in Numeric/numarray is opposite of IDL! For a 2-dimension array:

a[i,j] in IDL is equivalent to a[j,i] in Python

For a given dimension, slices may be specified:

IDLPython Equivalent


i:j i:j (except for different interpretation for j)
i:* i:
*:i :i
a[i,*] a[:,i]
<No IDL equiv.> i:j:s (stride, take every s'th element)
<No IDL equiv.> i:-j
<No IDL equiv.> Empty arrays and slices are permitted (e.g. a[0:0] is an array of length 0)
<No IDL equiv.> -i (indexing from end of array)
<No IDL equiv.> ... (fill in unspecified dimensions)
<No IDL equiv.> NewAxis (add new axis to array shape as part of subscript to match shapes)

Note: Slicing in Python generally does not create a new array, but instead creates a virtual array (non-contiguous in memory) that points to the same memory locations as the original array, e.g. b = a[0:10:2] followed by b[0] = 5 will change a[0] to 5 as well. You must use b = copy(a[0:10:2]) to get a complete new copy of the data.

This may sound confusing, but it can be very useful if you are working with big arrays and need to limit memory use. Unlike IDL, every new slice in Python does not generate a new array in memory.

Index Arrays:

IDLPython Equivalent


newarr = arrayvar[indexarr] newarr = arrayvar[indexarr]
arrayvar[indexarr] = valuearr arrayvar[indexarr] = valuearr

The syntax of using an index array for specifying subarrays of 1-D arrays is currently the same in Python and IDL. For n-D cases the syntax is different enough such that a brief description is not possible. Originally IDL used parenthesis instead of square brackets for array indexing.

Array Creation:

IDLPython Equivalent


fltarr()
dblarr()
complexarr()
intarr()
longarr()
bytarr()
make_array()
array(seq, [type]) to create from existing sequence; zeros(shape, [type]) to create array filled with 0's; ones(shape, [type]) to create array filled with 1's.
  
strarr() Lists and tuples are more appropriate (chararray does exist to handle large fixed length strings).
  
findgen()
dindgen()
indgen()
lindgen()
arrayrange(size, [type]), a.k.a. arange(), for vectors
  
sindgen() No direct equivalent, but trivial to duplicate.
  
replicate() repeat() (more general)

Array Conversions:

IDLPython Equivalent


byte(arrayvar)
fix(arrayvar)
long(arrayvar)
float(arrayvar)
double(arrayvar)
complex(arrayvar)
arrayvar.astype(typecode)

Array Manipulation:

IDLPython Equivalent


reform() reshape()
Also arrayvar.flat(), ravel(), or changing the shape attribute. The MLab module also has the squeeze function that removes degenerate dimensions.
sort() argsort(), i.e. indices needed to sort
arrayvar[sort(arrayvar)] sort(arrayvar), i.e. sorted array
max() max() [ufunc] (also argmax() and maximum in the MA module).
min() min() [ufunc] (also argmin() and maximum in the MA module).)
where() nonzero(); see this article for details.
arrayvar[where(condition)] compress(condition, arrayvar)
transpose() transpose()
[a, b] (array concatenation) concatenate()

In general, Python array manipulation functions are far more general and powerful than IDL manipulation functions, but also harder to understand. These include:

choose()
take()
repeat()
resize()
nonzero()
compress()
reshape()
resize()
concatenation()
transpose()
diagonal()
identity()
argmax()
searchsorted()
sort()
argsort()

The Python "list comprehensions" construct provides a terse syntax for creating lists, in conjunction with other functions/operations. For example: [len(i) for i in strarr] will produce the list [1,3] if strarr equals ["a","abc"] (this is a handy way of creating an iterable list of lengths).

Array Operations:

IDL: If size or shape mismatches, result is coerced to smaller of two and shape of one.

Python: Shape or mismatch results in error if one array not a "subset" of other's shape. If subset, "broadcasting" is used.

IDLPython Equivalent


byte Int8
UInt8
int Int16
long Int32
float Float32
double Float64
complex Complex64
Complex128

Math Functions:

The Python functional forms below are array ufuncs.

IDLPython Equivalent


abs() absolute(), fabs()
acos()arccos()
alog()log()
alog10()log10()
asin()arcsin()
atan()arctan()
atan(y,x)arctan2()
ceil()ceil()
conj()conjugate()
cos()cos()
cosh()cosh()
exp()exp()
floor()floor()
imaginary() complexvar.imag (.real for real component) Python 2.2 only; complexvar.getimag() (complexvar.getreal() for real)
invert()Matrix module
ishft() right_shift(), left_shift()
round()???
sin()sin()
sinh()sinh()
sqrt()sqrt()
tan()tan()
tanh()tanh()

Python ufuncs, with no exact IDL equivalents:

IDLPython Equivalent


Comparison:
greater(), greater_equal(),
less(), less_equal(),
equal(), not_equal(),
bitwise_and(), bitwise_or(), bitwise_xor(),
logical_and(), logical_or(), logical_xor(),
add, subtract, multiply,
divide, remainder,
power, negative, hypot

 

For binary ufuncs, the following methods exist:
reduce() Apply operator to all elements in array. add.reduce (a.k.a. sum) is equivalent to the total() function in IDL.
accumulate() Generate new array whose elements equal the operation applied to the first n elements of the input array. add.accumulate (a.k.a. cumsum) is equivalent to an integrate function.
outer() Produce all combinations of results (multiply.outer(x,y) is equivalent to an outer product).

Other functions:

IDLPython Equivalent


fft() fft() [FFT module] (not available yet)
convol() Not available yet.
randomu() random(), uniform(). See module RandomArray.
randomn() RNG module (not available yet)
reverse(a) (a is a vector) a[::-1]
total() sum()

Programming:

IDLPython Equivalent


execute() exec()
n_elements() len(), size(). In Python, the easiest way to test whether a variable is defined is using try/except.
n_params() Closest equivalent is len(*args). Ability to supply default values for parameters replaces some uses of n_params().
_extra **kwargs (see also *args)
size() shape(), arrayvar.type()
wait time.sleep()

Return to the Tips and Examples index page.

Content on this page which is a copy of the STScI IDL to numarray mapping page (5 Nov 2002 version) are in the public domain. Additional content is covered by this license.

Valid HTML 4.01! Valid CSS! Updated: July 7, 2008 by Johnny Lin <email address>. Thanks to Boyd Blackwell for additions.