Accessing ZEMAX/ OpticStudio from Python using PyZDDE

PyZDDEIconCodeAtGithub

ZEMAX (also known as OpticStudio), a leading tool for design and analysis of optical systems, provides an in-built DDE server that enables access to ZEMAX’s features externally using any Windows application. The ability to access and control ZEMAX externally allows it to be used in many interesting and powerful ways, some of which has been documented in the “ZEMAX Extensions” chapter of the ZEMAX manual. An excellent toolbox called MZDDE, developed by Derek Griffith at CSIR, already exists for users of MATLAB. When I tried to look for an equivalent toolbox in Python, I couldn’t find one, so I decided to write one myself.

PyZDDE is a Python-based toolbox for communicating with ZEMAX using the DDE messaging protocol. It is similar to MZDDE and very much inspired by it. Currently PyZDDE is not as extensive as MZDDE as it is a work in progress. However, it has some distinguishing features — it can be used with regular Python scripts and also in an interactive environment such as an IPython shell, QtConsole or IPython notebook. The ability to interact with ZEMAX from an IPython notebook using PyZDDE can be a powerful tool for both teaching and documentation. Please visit the github repository for downloading and using the code. The github repository also contains an accompanying wiki page which describes briefly on how to use the PyZDDE toolbox.

Here I would like to briefly document the communication process between ZEMAX and Python using PyZDDE. But before that I would like to show you how simple it is to communicate with Zemax using PyZDDE using a “Hello world” example (it gets and prints the running Zemax version):

import pyzdde.zdde as pyz
link = pyz.createLink()
print("Hello Zemax version: ", link.zGetVersion())
link.close()

ZPL Macro syntax highlighting using Notepad++

Using macros, written in the Zemax Programming Language (ZPL), is a powerful way to extend the functionality of ZEMAX. Zemax doesn’t provide a special editor for writing ZPL macros, and it recommends using text editors such as the NOTEPAD editor.

I love editors with syntax highlighting. Since there are no standard editors (with syntax highlighting) for writing ZPL codes, I decided to use Notepad++. It also allowed me to create a style-file which enabled syntax highlighting and support minimal code folding as shown below. The style-file contains the latest keyword, numeric function and string function definitions (as of April 2013). I have shared the style-files here in the post.

In order to download the files and see the quick instructions to use them, please continue reading.

zplmacroimg01

UPDATE:
Today (05/06/2014) I got to know about another project, created by Xinda Hu, for syntax highlighting and code folding of macro programs for both Zemax and Code V. It seems to be quite sophisticated and uses a modified form of Solarized theme. It also provides auto-complete and function calltips in the editor, which I believe can be of great help while coding. He has uploaded the required files at his GibHub page here (Zemax) and here (Code V). He has also created a GIF animation to demo the capability of his highlighter project, which can be found here. I highly recommend using his project for ZPL syntax highlighting.

Continue reading

eigshow in Python

Few days back I was playing with the eigshow demo in Matlab. It is a pretty useful demonstration of eigenvalue and singular value problems for 2 by 2 matrices. A good description of the eigshow demo in Matlab is here. I was a little surprised not to find an “eigshow” implementation in my favorite computing language — Python. So I thought it might be a good idea to write my own, and here is the result.

The figure below shows the basic structure of the program which is somewhat similar to Matlab’s eigshow. There are some differences too, such as it displays the eigenvalues and eigenvectors in the eigen mode, and the singular values and singular vectors in the SVD mode. The program also displays the rank, trace, and determinant along with the matrix. It also gives a visible warning if the eigen/singular values and/or vectors are complex (as it can only plots the real values and vectors).

eigshow1

eigshow2

eigshow3

(Please continue reading to know about basic function and code)
Continue reading

Python for Scientific Computing – a collection of resources

This post is about the Python ecosystem for scientific/ technical computing. Generally, when someone says that he/she is using Python for technical computing, we must interpret it as the “Python ecosystem for scientific/ technical computing”. Vanilla Python, which is a general purpose, versatile language was not designed for and is not suitable for technical computing (such as linear algebra, symbolic computing, vectorized operations etc.) in itself. However, the language provided just the right set of tools, and a framework within which scientists and engineers could easily implement their ideas. Python was quickly embraced by the general scientific community which built several packages using Python that are quite suitable for technical computing. Currently there hundreds of different Python-based libraries. This post is meant to be a basic introduction to a core set of scientific packages in the Python ecosystem, for someone new to Python (though I highly doubt I have any such audiences).

Python is a very powerful language for doing all sorts of things, and at all stages of research — from general computing, system programming, design of experiments, building device interfaces, connecting and controlling multiple hardware/software tools, to heavy scientific number crunching, data analysis and visualization. Python is an interpreted, general purpose, object-oriented, high-level scripting language, which supports multiple programming paradigms — procedural, object-oriented, and functional programming. The core design philosophy of Python are simplicity, code readability, and expressivity.

Python is easy to learn. It is intuitive and simple, yet it is powerful, beautiful and expressive.

What makes Python particularly attractive for scientist and engineers is that it is open-source, highly portable, intuitive to use, and features dynamic and strong typing. It provides both interactive and script based programming environments like MATLAB. It also features automatic memory management and garbage collection enabling scientists and engineers (with or without strong computer science background) to direct their time and energy on their algorithm and let the interpreter handle the low-level stuffs. All the above qualities in addition to its large scientific community-support allows greater opportunity for code-sharing, open and collaborative research, and thus it supports the philosophy of reproducible research.

Python itself is a full featured programming language with a large set of tools in the standard library (sure you have heard “batteries included”!!) What is even more attractive is that there is a whole technical computing ecosystem around Python built by the different scientific communities. Most of the tools are built on top of Numpy, which itself is built on top of Python. Numpy extends Python with capabilities such as vectorization, homogeneous arrays, multi-dimensional arrays, fast element-wise operations, broadcasting and universal functions that are essential for scientific/technical computing. The figure below shows a basic landscape of the scientific python ecosystem. Please note that it is not meant to be a complete, rather a very basic reference to the most common tools around Python for scientific computing.

Scientific Python Ecosystem (simplified)

The ease and interactivity of the language coupled with the availability of good community support and specialized scientific libraries enables a newcomer to quickly learn and do meaningful work using Python. The interested reader can read more about the arguments about advantages of Python and how it compares to other languages here.

When I started learning Python I collected a number of resources related to the use of Python for scientific and numerical computation. I think these resources may help someone who is just getting started using Python for Scientific computing. For this reason I have decided to share a list of those resources here in this blog post.

Continue reading

My experience of the edX’s Computer Graphics course

StanfordDragonSmallMy interest in computer generated images and animation goes back to when I was in my 9th grade. Our computer teacher (Miss. Banurekha) wanted me and my dear friend Siddharth Choraria to participate in an inter-school computer graphics competition. I wasn’t any computer rock-star, but she had seen my pencil-sketches on the class notice board and thought that we do something useful. Of course, Siddharth  was a brilliant student and we worked well together.

We spent several days and nights on the project. We had decided to portray a short story about the inevitable effects of war on human society. After I drew each scene on a graph paper, we manually transferred the co-ordinates (of the best-fit lines approximating the curves in the image) to the computer and drew on it using GW BASIC. There was no concept of key-framing, so we redrew each frame repeatedly, changing only the portions required for creating the animation. We also had no idea that we could use matrices multiplications to transform objects in CG.

Since then I have been very interested in computer graphics. However, I never really took the deep plunge to explore the CG world. So, when the opportunity to learn modern computer graphics from one of the world’s best known professors of CG, Prof. Ravi Ramamoorthi, came in the form of a MOOC course (Foundations of Computer Graphics BerkeleyX), I just couldn’t resist. My main motivation for the course was not only to make pretty (CG) pictures, but also to learn 3D geometry used in CG, ray-tracing and OpenGL so that I could use them in other areas such as computational imaging, scientific computation and visualization. I also wanted to know how people create photo-realistic effects.

Continue reading

Installing OpenCV on Debian Linux

OpenCV_DebianIn this post I will describe the process of installing OpenCV (both versions 2.4.2 and 2.4.3) on Debian Linux (especially Debian-6). After installing, we will do some tests to verify the installation and also see some examples. As I was trying to install OpenCV on Debian I found that although there is lot of information on similar topics (see the references at the end), I still had to dig around a bit to understand the process completely. Some of the excellent guides are now outdated. Also, I found a few that have all the “what to do” but not “why to do”. Then as I started jotting down my own notes into my OneNote notebook, I realized that I should share these notes with others who are interested. I hope to update this document as I find more useful information in future.

1. Before installing the Prerequisites:

It is recommended [Reference 2] that you update and upgrade your current system before installing OpenCV. You can do this from the terminal using the following two commands:

sudo apt-get update
sudo apt-get upgrade

Continue reading