X
2577

A few hints while using Jupyter Notebook and IDL-Python Bridge

 

This article presents some hints while using the IDL Python bridge and Jupyter Notebook. It specifically covers the transfer of variables between Python and IDL in IDL Jupyter Notebook

 

  1. How to run Python code from IDL Jupyter Notebook

The standard IDL->Python Bridge syntax is currently returning an error while launched in IDL Jupyter Notebook

In [0]     print,'Hi, I am in IDL'
In [1]     python.run('import os')

           print('Hi, I am in Python now')

Or

In [2]     python.run('import os')

           print('Hi, I am in Python now')

 

[Output]

Hi, I am in IDL

 

% PYTHON_RUN: Exception: No module named 'idlpython'.
% Execution halted at: $MAIN$          

% Attempt to call undefined function: 'PRINT'.
% Execution halted at: $MAIN$  

 

Thus, in order to run Python code from the IDL Notebook you will need to use the below magic commands:

 

In [0]     print,'Hi, I am in IDL'

 

In [3] %%python print('Hi, I am in Python now') import os

OR

In [4]     >>>

           print('Hi, I am in Python now')

           import os   

 

[Output]

 

Hi, I am in IDL

 

Hi, I am in Python now

 

 

 

  1. How to pass a data variable from IDL to Python using IDL Jupyter Notebook

Due to the above limitation, you cannot get data from IDL to Python directly (e.g. you cannot use Python.X. command to push IDL data to Python).

In [5]        print,'Hi, I am in IDL'

              X=bytarr(5,5)

              help,X

              Python.X = X

 

[Output]

X               BYTE      = Array[5, 5]

% PYTHON_RUN: Exception: No module named 'idlpython'.
% Execution halted at: $MAIN$  

 

You rather need to pull your IDL data from Python using a command such as x=IDL.x. For example:

In [6]     print,'Hi, I am in IDL'

           X=bytarr(5,5)

           help,X

 

[Output]

 

Hi, I am in IDL

X               BYTE      = Array[5, 5]

 

In [7]      >>>

            print('Hi, I am in python now')

            X=IDL.X

            print('X size :',X.shape)

            print('X: ',X) 

 

[Output]

 

Hi, I am in python now

X size : (5, 5)

X:  [[0 0 0 0 0]

    [0 0 0 0 0]

    [0 0 0 0 0]
 
    [0 0 0 0 0]

    [0 0 0 0 0]]

 

 

  1. How to pass a data variable from Python to IDL using IDL Jupyter Notebook

 

In [8]      >>>

            print('Hi, I am in python')
 
            import numpy as np

            X= np.arange(5).reshape(1,-1)

            X=np.repeat(X,5,axis=0)

            print('X size: ',X.shape)

            print('X: ',X)

            IDL.X = X

 

[Output]

Hi, I am in python

X size:  (5, 5)

X:  [[0 1 2 3 4]

    [0 1 2 3 4]

    [0 1 2 3 4]

    [0 1 2 3 4]

    [0 1 2 3 4]]

 

In [9]   print,'I am in IDL now'

         help,X

         print,’X :’,X

[Output]

I am in IDL now

X               LONG      = Array[5, 5]

X :        0           1           2           3           4

           0           1           2           3           4

           0           1           2           3           4

           0           1           2           3           4

           0           1           2           3           4

 

  1. How to display a Python graphics in IDL graphic system using Jupyter Notebook
  • Create the graphic in Python without displaying it
  • Store the pixel buffer in a numpy array
  • Pass that array to IDL
  • Display the array in IDL. Note that due to the differences in array structure between Python and IDL, you must transpose the array and rotate it by 90 degrees.

 

In [10]       >>>

              print('Hi, I am in python')

              import matplotlib.pyplot as plt

              import numpy as np

              X=np.arange(0,10,0.1)

              Y=np.sin(X)/X

              Fig, AX=plt.subplots()

              AX.plot(X,Y)

              AX.set_title('A simple plot for IDL')

              Fig.canvas.draw()

              P=np.array(Fig.canvas.renderer._renderer)

              IDL.P=P

 

[Output]

Hi, I am in python

 

In [10]          

print,'Hi I am in IDL now'

help,P

im=image(transpose(P))

im.rotate,-90

 

[Output]

 

Hi I am in IDL now

P               BYTE      = Array[4, 640, 480]

 

 

-----------------------
created by BC on 6/11/2020 - reviewed by BC-NA on 6/11/2020