 I teach aerospace engineering at IIT Bombay and I'm on a break for a couple of years to start the Indian edition of NANDE. So Maya V is a 3D visualization package that is written in Python and what I'm going to talk to you about today is something that I've been building over the last 10 years and I will talk about the tool itself, talk to you about the features and about why some of these features are the way they are and some lessons that I learned as a developer. It all started in 1998 when I was looking, when I was doing my PhD, I started, I finished my undergrad course in IIT Madras and stayed on for a PhD in aerospace engineering and I had colleagues that needed 3D visualization tools. They want that many free, easy to use open source tools, we can afford commercial tools. So what they were looking at was something like this, maybe I'll come back with this in a bit, but here's a more easy to understand picture. So here's a room, it's an open room, it's a cutaway, so this part is actually cut out, this part is open, that part is open and there's a fire here. So the question I'm trying to understand is how does the fluid flow, how does the air flow, where is the smoke and all those questions, where is it hot, where is it cold, things like that. So these are kind of problems that people will like to simulate, visualize, understand that data and it's a lot of data, it's not a stream of data, it's not a data base, it's basically at any point you have some information, you have either temperature information or you have like a vector, you have a vector field or a scale of field, the vector field is basically associated with each point in space and so tell you what how the air is flowing. So if I want to visualize this, I would do something like this, I create say an isosurface and this is at 550 degrees Kelvin, so it basically says there's an isosurface of 550 Kelvin, of course you can make it more fancy by throwing in an animation, playing around with the data, a lot of this involves interactive visualization, so it's not just that I produce a final picture, you need that for a publication, but when you're actually trying to understand and explore, you need interactivity. The other thing that you see here, all of these lines that are here, these are called streamlines, it basically suggests that if I were to introduce say Agarbati piece here, it's going to start flowing like that and it's color coded as per the temperature. So the idea is you kind of get some sense of how what is going on in this complicated piece. So what this problem was is basically you have supersonic jets that are mixing and they're trying to understand the physics in this. So these are the kind of problems that some of these people use looking and they're doing what's called CFD, so how many of you have heard of CFD? There's only one, wow, thank you. So CFD stands for the rest of you's computational fluid dynamics. The idea is you're trying to simulate what's going to happen. For example, if I wanted to simulate the temperature distribution in this room, I would like to simulate it in order to say size my air condition or place those air conditions. Now it looks easy on the face of it, but it's actually very nasty, very difficult problems. These are non-linear partial differential equations that are not the same. But the idea is you have a lot of data that you've generated, either collected or simulated. So at this point, I wanted to help my colleagues visualize this and I found the visualization tool called PTK for short. It's a very popular graphics library which basically allows you to do 3D graphics, imaging and visualization. Now, note the difference between graphics and visualization. Graphics basically is saying here are some primitives to put them on the screen. Visualization is here's some data. Give me an ISO control of this. Here's some data. Okay, take a slice through that data and show me what's happening in that size. That is visualization. So, PTK lets you do that visualization. It's implemented in C++ and it has wrappers to Python, Java, TCL, etc. So I was learning Python at this time and started writing some Python scripts in my colleagues. Now, PTK itself has a Python architecture. I'll show you a brief slide about what that means. And it's humongous. It's like 900 classes. And typically, this is how it works, a Python architecture. You basically have a source which generates data. It could be a data file or it could be something you have constructed. And then you have a filter which basically processes that data. You have a map of that takes the filtered data, again that's raw data, and generates graphics primitives. The graphics primitives are connected to an actor which has geometry information, texture information, blah, blah, blah. And then you display that or write that out to a file. So these are called syncs. Those are called sources. These are called filters, mappers, and actors. That's a standard graphics pipeline terminal. And here's a typical Python script. This is how it looks. You basically import PTK, create a source, set up your map up, set up your actor, set the properties of that actor, hook it up in that pipeline, and off you go. So that's a very simplistic description, obviously. It's just a comb. But who wants to learn a graphic story? Definitely the scientists don't want to. And imagine them learning a 900-pound gorilla literally in this case. It's hard. So what I did was I started working on some simple user interface for certain scripts that were tailor-made to their needs. So I released it, made it open source. And it was very popular. It looked kind of like this little gory thing as a user interface. But essentially, you can plot, look at some visualization, click on some buttons, slice things around, look at your data. So here's a vector visualization. So here's a vector field. The cons basically indicate the flow, direction of the flow. There's actually a flow inside a half. It's from some city scale. So as I was doing this, I was learning Python. And I started learning the introspective capabilities of Python. And I realized that you can't generate UI for 900 classes. It was madness to me. So instead, I relied on the consistency of the VTK API. I'm not sure if there are... But it doesn't make sense to generate this every time. So I passed the VTK class. Given the module, I'll grab the live object, figure out what its methods are, and present the user interface on the flight. The other thing I could do was I could take an object in the VTK pipeline, go up and down, and figure out what the pipeline itself looks like. So I could add, given a standard VTK Python script, I could like add these four lines and actually produce something like that. Basically says, here's a graphical representation of your pipeline. And there is... I double click on, say, the property, and it pops up an automatic UI which I can just change and edit an object. So if I'm doing a visualization, that's really convenient. And then VTK is again 900 classes, and I myself don't know all of it. So I needed something to explore it, so I wrote up a little documentation. So it started, so it was one afternoon, it's a hack. And all of this is kind of an afternoon doing something else, playing around. So the lesson I learned there was Python rocks. Makes a big deal. VTK is awesome. The other major things were runtime introspection is really, really problem. Automatic UIs are cool and fun, but really these two things make them. Runtime introspection, dynamic program. And it makes the world a different. So with a small bit of code, you can generate a lot of stuff, which is very unusual for me at the time because I was a C++ programmer. Doing any reflection at that point was kind of crazy in C++. Whereas with Python, it was just natural. But then as I was building this, it kind of grew as a programmer. And realized that as I released it, it became popular, people started downloading it and things like that. So it was very specific. It was tied down to very specific visualizations that I had to build to draw the UI. So then came MyAvi 1.0. That was the first time MyAvi actually came into being. And it looked something like this. Again, the visualizations look similar, but it's a VK interface UI. And you can notice that I've started slapping together stuff that has been accumulated in the previous project. But it was essentially a rewrite. There's the kind of stuff you can do. You can generate that entire visualization you see over there on the UI. And that was the point. The point was I didn't want the programmer to start writing these scripts themselves. So this was released in 2001 May, 10 years ago. It had a GUI. And it also had a command line interface, which was essentially like that of a typical command line application. It wasn't quite a Python programmable API. It was more like I can say MyAvi minus D opens this file minus M, start that module. But essentially came out of one idea at one point that I have over generalization. And I started just coding. Fortunately, I had wonderful advisors who let me say, okay, you're having fun, go do something productive. And I had like one month, I didn't do anything for my Ph.D. And I ended up doing this. We all call it the Ph.D. procrastination project. That's what happened. And then I got busy with my Ph.D. And submitted that in 2004. In the meantime, I had made a few releases of MyAvi because it was fairly popular. So what were the issues at this point? At this point, many people started using it. There are a lot of people in the Python community who are doing sci-fi computing who are starting to play with MyAvi. The problem was all the people who are using MyAvi to actually do realistic visualizations are a large fraction of them. There are two fractions. One fraction was those that wanted an application. You start up the application, click, click, click, click, click, click. For a large part of the people who are dissatisfied, well, basically people who would like to program it from Python. And if you have programmed ever in Python, so how many of you have actually programmed in Python? Great, awesome. So all of you know the power of the interactive interpreter. Hopefully, right? So you basically in startups say, I Python, type away and do your thing. So that's kind of big power of Python. Nobody could actually do that that easily because it was, there was no model view separation. It was all kind of this gobbledy geek of TK editor mixed up with the model and all that. No MBC, had a clunky TK filter UI, which wasn't kind of modern looking enough. And it was not easy to embed. Basically, I could reuse MyAvi in other context. So then came MyAvi 2. So I finished my PhD and I was no longer an apprentice. It wasn't quite Yoda yet, but. So at that time, I was looking for an academic position and I'd known Nthought for many years before that. And they basically funded me to work part-time on building MyAvi 2, which was complete rewrite again, to build MyAvi 2. So I spent about eight months concerning for Nthought. And then I came across Nthought as basically released as part of their Nthought tool suite, which is a BST license open source toolkit that we use internally at Nthought to build applications for commercial customers. For straights. So like the read, that basically gives you Python objects on steel. So here's a typical trace example. Notice that it's kind of straight Python. How many of you have programmed Django? Okay. So in some of these constructs you see here, are very Django-esque in that model. Basically, you do a bunch of imports, create a class, and this is a class. So I'm trying to create a class for a cow, which has an age, has an age. And notice that I'm actually typing many of these videos. Not typing with my fingers, but typing it in the computer science. It has only legs and so are the children. So the thing is, I can declare attributes of a Python object to have a specific type. And if I supply say a floating point number here, it will scream at me saying, I expected a string, you gave me a floating point. The other thing is, the instant I actually specify this, I've given the computer enough metadata to know that that's a string. Oh, this is a range, this is an integer between 0 and 4. So the computer basically has the metadata information about what that quantity is. The other thing you can do is, you can basically hook up handlers. So when a variable, when an attribute changes, it will fire any callbacks of it. All of that in this. And finally, the killer feature was, I can instantiate it. But I can do c.edit traits here and it will pop up a UI that looks like this. So you have cost. I didn't write a single line of UI code. And this will work on WX Python or PySide. So what we did was, I came across traits, so I married VTK. So VTK is a C++ type. And the Python layer sitting on top of VTK is basically very C++-ish. It doesn't have the same idioms that a Python program would be used to. And it doesn't interface very clearly to NumPy arrays. For anybody who's doing science with Python, they use NumPy arrays since they're bread and butter. But you can't just say, okay, here are the bunch of points that I constructed with NumPy array. Visualize it. You can't do that with VTK. So what I did was, I built a layer called TVTK, Trated VTK, which basically took all of these 900 plus classes. So you have an installation of VTK Python. You install traits. It will basically grab your installed VTK module, parse it on the fly with your Python code. Figure out that. Generate shim classes for each and every VTK class. Bundle that all into a zip file and give you a Trated TVTK classes. And that would allow me to marry NumPy traits, which means if I change the toggle variable, it will fire back. It can hook up to callbacks and all of the goodies that I get. And UIs came for free. Notice that I just had to create a class and said it's edit traits. I got a UI. So all of the UIs you'll see are basically generated automatically because I am able to parse the entire VTK class. And the beauty is, the whole is greater than the sample class. It's not just putting these two together that gives you the map. The whole is greater than the sample class. So the lesson here is that the API matters a lot. You need to cater to the idioms of that specific class of users that you're targeting this to. In this particular case, it matters a lot to people that the API that you expose is Python. People tend to use it as a normal Python program as well. Now, when I did this, you can imagine I have 900 plus classes. There are a bunch of different platforms. There are 32 bit issues, 64 bit issues. I had to make sure that the whole VTK library, which by the way, I have no control over. It's been installed by the way. I have to make sure that it parses cleanly and works fine. So I needed to use TDD for all of this. I made sure that I'd like proper test cases and made sure that it actually works. It was a big lifestyle. Now, the big lesson was throw away code. You're going to have to throw it in someone. So I had to literally rewrite the whole thing. But it's not too hard. Once you get the hang of programming, it's like speaking. The advantage is, once you do all of that, you end up getting awards. So I applied, happened to apply for a cross India award and happened to get two awards. One for Miami and one for TDD itself. So I've run through some features of Miami too, given that that's the underlying architecture that we have. So what Miami tool provides is something that is a nice UI, which allows you to, again, look at the pipeline, double click. And you basically start configuring the objects to visualize your data and the visualization as usual. Also embeds a little IPI session here. So if you want to use the application, you can still script it. For example, if you drag this and just drop it there, you can script it, like, which is kind of convenient. But then came the other class of users. The previous case, I mean, I could do this with Miami one, but this I couldn't. So what we did was we added another shim layer on top of the Miami object-oriented IPI to give it like these one-liners. So here is, okay, this is all setting up the data. This one line, mlap.conto3D x squared by 2 plus y squared plus z squared. It's essentially taking iso-surfaces of this scalar function, which means I've created a cube of data from minus 5 to 5, minus 5 to 5, minus 5 to 5, 3 times from cube. And I'm kind of looking at this scalar field. At each point, I calculate this quantity and associate it with a point. So I have no cube of data, as these two lines will go. This mlap.conto3D will essentially give me iso-surfaces. And all it takes is that one line. So this is a big win. Because scientists like that, they want to be able to say, okay, here's x, here's y plotting. Or here's x, y, z, give me something else. Here's another simple example. And the power is, I can do all of this from the interactive interpreter. And people who do science and engineering, basically they come from typically using something like IDL or MATLAB, where this is the norm. You're basically typing stuff, exploring data, plotting it, closing that plot, making a fresh plot. It all has to be interactive. So this is not just a static picture in this case, it is, but you can actually interact with it, explore it, change, toggle buttons and so on. So the other big lesson was, listen to your users. And obviously you have to, if you're paying, but in this case it was open source. And you make sure that you made the library extensible, built it in a general fashion. And this made such a big difference. Many people actually use Miami because of this one line. Because it was just so convenient. The other big advantage is you can now embed Miami in your customized applications. So here's an application that's basically exploring the Lorentz equations. And those are the equations, by the way, on the right you see there. Those are text fields that you can actually edit. Maybe I'll just show this as a demo. So this is about 150 lines of Python commented. So that's the data. Essentially, it's created a queue and it's exploring these differential equations and allowing me to visualize that and plot them. So for example, an interesting thing I could do. Let's remove the non-linear thing, this equation. The retractor is gone. So I actually changed the equations right there. Get squared, it's gone again. Make it back xz and there's your balance. You can toggle these parameters. It's actually generating all of that on the client. I can explore it, visualize it and play with it. And notice that this is a custom application. This is not Maya V, the full-fledged application. And it's pure Python. And you can basically, this is available as well. So I can now customize, build an application with dialogues. And if you look at that code, it's available online. There's not a single line of WX Python or QT code on that. So basically with just Python, rates, Maya V, you can. In addition to this, it also has a bunch of other interesting features. One is automatic script recording. It's such a cool feature that I've actually done on it. So I'll create a little helix here. Those three lines are going to create a little helix for me. Now the nice thing is, Maya V also has this powerful user interface. So I click on that and I get the full-fledged user interface. Now the problem is, I may be used to these one-liners, but I may want to do some customization. I want to change the color scheme. I want to do something. I don't know what Python code to write for that. So as I click on this little button here, it gives me a little window. Where I'm going to just rotate this. It'll actually dump the lines of code that we've done. I'm going to say, change the background. It's white. And it will say, scene.scene.background is 110. I can say, tube set the radius factor to, and it will keep track of that. And basically all of the code you see there, is readable Python code. That you can actually save to a file, dump, and execute. We also found that people needed to use it off-screen. So if you want a web-based, so this is a web-based app called Sage, again, which uses based on Python. You can actually embed a Maya V plot inside this by dumping a little x85 here and actually use it as that. We'll run through quick real-world applications. So Maya V has been used world over. It's been used for 3D visualization of the brain, the neuro-imaging. This particular visualization is from Berkeley. This is one from France. And this actually won an award for this tool. It's a tool that's actually based on Python. It's called Connectome Viewer. It's an open source tool. Here's an application from NAL. They have a tool where they have, they actually build the supercomputer, write the software for that, to simulate weather prediction. They wanted to customize Maya V to view the weather simulation data. And here's another simple customization that we did. They have an OpenGL in-house app, and it controls four other instances of Maya V through the network. So Maya V also has a nice network interface using Crystal. Here's an app, an mThought. It's basically a dental implant app that we have. And again, it uses Maya V internally, way down deep inside. So basically, Maya V is reusable, Pythonic 3D graphics is what we have. Here's a summary of the lessons. Use a clean object model and the right design patterns. It really, really makes a difference. All of these features came out simply because of that. Traits are a big deal. The instant you switch over, it kind of forces you to use a model you control the approach. It forces you to make sure you're focusing on your object, on your object model. Scriptable APIs really matters. TDD, and of course Python long. So thank you. Let me know if you have any questions.