X

Help Articles are product support tips and information straight from the NV5 Geospatial Technical Support team developed to help you use our products to their fullest potential.



2589 Rate this article:
No rating

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
Please login or register to post comments.
Featured

End-of-Life Policy Enforcement for ENVI 5.3 / IDL 8.5 and Earlier Versions

5/6/2024

April 1, 2024 Dear ENVI/IDL Customer,  We are reaching out to notify you of our supported... more »

How to Upgrade licenses to ENVI 6.x / IDL 9.x

12/5/2023

What is the new Upgrade function? Starting with ENVI 6.0 and IDL 9.0, we have implemented an... more »

What to do if the 'License Administrator - License Server' for the Next-Generation License Server does not start?

6/13/2023

Background: With the release of ENVI 5.7 & IDL 8.9 and the corresponding Next-Generation licensing... more »

Next-Generation Licensing FAQ

4/28/2023

  NV5 Geospatial has adopted a new licensing technology for all future releases of our ENVI, IDL... more »

The IDL Virtual Machine

6/6/2013

What is the IDL Virtual Machine? An IDL Virtual Machine is a runtime version of IDL that can... more »