Using Visual Studio 2013 for Python (3.4) with NumPy and SciPy on Windows

There seem to be various editors for Python and there are many articles online (e.g., this blog post) that discuss the features of the various editors. PyCharm by JetBrains seems pretty popular, but while I was Googling for Python editors, I came across Python Tools for Visual Studio. Coming from a C# background, I thought I’d give it a shot before trying out a totally new editor (I’ve moved onto Spyder now though).

The first thing you need to do is download PTVS from CodePlex. I downloaded PTVS 2.1 VS 2013.msi since I’m on VS2013. Of course you’ll need to install Python first if you haven’t done so already – I installed 3.4 (64-bit initially – but had to revert to 32-bit later).

At this point you should be able to create a Python project in Visual Studio – here is a good tutorial on how to create your first Python program in VS. Basically you create a new Python project, very similar to how you would create a .net application.


Creating a new project creates a new .py file with one line of code:


Now you hit F5, it runs your Python code:


This is the easy part. What I was having trouble was figuring out how to add external libraries and import these. Following is the simple Python code I was trying to run (from the Udacity Machine Learning class):

import numpy as np X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]]) Y = np.array([1, 1, 1, 2, 2, 2]) from sklearn.naive_bayes import GaussianNB clf = GaussianNB(), Y) print(clf.predict([[-0.8, -1]]))

This is when I started having trouble. VS kept complaining about “No module named ‘numpy’.” and kept stopping at the import statement.

If you google ‘visual studio no module named numpy’ or ‘no module named numpy’ you’ll find tons of threads with various suggestions, including installing/upgrading pip (pip comes with 3.4), using easy_install and running other registry updates. I couldn’t get any of these to work. With pip, it gave me false hope looking as if everything was fine:


but then stopped out with errors (more on installing using command line later).

After lots of searching, I decided to try the numpy Windows installer available at There’s only 32-bit version available on this official site, so I just tried installing that, but ended up with the following error saying python was not found in the registry:


Here’s what finally worked –

I uninstalled my 64-bit Python and reinstalled Python 32-bit.

Then tried installing numpy – which worked!


Similarly, I could install other modules too. sklearn installed like a charm:


The previous code requires scipy, so I installed scipy too (from


Now my Python code runs like a charm in Visual Studio! :-)


So just to recap, these are the steps to follow:

  1. Install Python 32-bit. Make sure ‘Add python.exe to path’ is enabled.
  2. Install Python Tools for Visual Studio.
  3. Install numpy 32-bit and any other external modules you need.
  4. Run you code!

Seems pretty straight-forward, but lots of people, including myself, seem to have trouble getting Visual Studio to work with Python, especially getting the external modules to work.

By the way, there are ‘unofficial’ versions of numpy available in 64-bit, and also Windows versions of Python available (like IronPython), but I haven’t really played around with these. I’m sure some of these combinations would work equally well.

Lastly, it seems pretty straightforward to install Python modules using pip. Essentially there are (at least?) 3 ways –

– if there’s a zip file (or a tar.gz), simply download and unzip into a folder (it should contain a file called – see the YouTube video here. Then go to that folder in a command prompt and do:
python install

– if you downloaded a .whl file, just open a command prompt and type:
pip install some-package.whl

– install directly using pip (no need to change directory or anything, just open a command window):
pip install –U packageName (e.g., PySide)

You will need to use one of the above two methods to install packages that don’t have the Windows installers. For instance, matplotlib had an installer, but it’s got dependency on six which doesn’t have an installer. Matplotlib also requires dateutil and pyparsing, and  was a great resource to download these modules from.

Posted in General | Tagged , , , , , , | 1 Comment

Launching Spyder on Windows

I didn’t really want to write a separate post on this, but it doesn’t really fit in with using the Visual Studio for Windows post, so here it is.

If you install Spyder using any of the standalone installers after installing Python, you’ll have trouble launching the Spyder IDE. If you go to python_dir/Scripts, you’ll see the following:


but clicking on spyder.dat will launch a command prompt for a split second and then disappear. This seems to be a common issues as asked here and here (and other places).

What I did was taking a quick screenshot of the screen that appears for a sec. This is what is actually says:


It was pretty easy to fix the issue once I knew what the problem was. It was a matter of opening up a command prompt and typing:

pip install -U PySide

Once that was done, I created a new shortcut on the desktop and pointed it to spyder.bat. Now Spyder launches without any issues :-)


Posted in General | Tagged , , | Leave a comment

Charting with WPF/C#

Visualization techniques have been evolving rapidly and with a growing trend towards ‘big data’ and ‘analytics’ there are so many technologies to choose from. If the development technology is not a constraint, D3 is arguably the best way to go, but if you are looking for some charting capabilities within a WPF/C# development environment your free choices are somewhat limited. Based on what I have tried out so far and read in various forums, here are some popular ones:

  1. MS Chart Controls – Pretty decent collection of controls, but you need to use ‘old’ Windows Forms to use these. You can host the Form within a WPF application if you really want.
  2. WPF Toolkit Charting Controls – I think this is the first real WPF charting control Microsoft released. Charting options are good, but they still look like the old Forms-based charts. Here is a good tutorial to get started.
  3. D3 1.0 – Pretty easy to get started, but I felt the number of options available was rather limited. First attempt to have D3 library capabilities in a WPF environment I think. There is a good tutorial here on how to get started with these charts. Seems like the WPF effort has somewhat been abandoned and there is more emphasis on having the D3 capabilities in Silverlight now which is called D3 2.0.
  4. Metro Charts – “This project provides a small library to display charts in Modern UI Style (formerly known as Metro) in WPF, Silverlight and Windows 8 applications”. This is the best I have seen so far and the UI looks much better and modern compared to all others. The trouble is, these are aimed towards Windows 8 applications and you need Visual Studio 2012 to run the sample code you get off the site. You still can get these to run on Windows 7 with Visual Studio 2010, and that’s what I’m going to focus on in this post.

– To get started, download the sample code from (if you don’t want to go through the steps yourself, just download the project

which contains all the changes I’m discussing here and you should be good to go! Just rename to a .zip – WordPress doesn’t allow me to upload zip files). You should have the following folders and files:


– Open the solution file. Permanently remove source control since we won’t be needing this.


– At this point you’ll get a bunch of errors starting from:


and your Solution Explorer will like this:


– Delete all projects except for the following three:


– Save the workspace and close Visual Studio

– Now, open Windows explorer and open the .csproj file in a text editor (like NotePad++) and change the ‘TargetFrameworkVersion’ from 4.5 to 4.0. You need to do this for the 3 files under De.TorstenMandelkow.MetroChart, De.TorstenMandelkow.MetroChart.WPF and TestApplicationWPF folders.


– If you want, you can clean up your folder a bit as well so that you have only the following on disk. This step is optional.


– Open the main solution (i.e., MetroChart.sln). Remove source control association bindings if a message pops up. You should see the following where the source code is now loaded into VS.


– Change startup project to TestApplicationWPF


– Hit F5, and you should see the following Smile


One of the things I wanted to do was find the underlying data when a user double clicks on a chart. To do this, you can add something like this in your xaml:


Then in your code behind, you can handle this event in whatever the way you want.


Play around. Using Metro Charts is fairly straight forward.

Posted in General | Tagged , , , , , , , , , | 4 Comments

Accessing NCBO Annotator Web Service in C#

The NCBO Annotator allows you to get annotations for (biomedical) text from a number of standard ontologies. For instance, if I want to find the corresponding RadLex codes for ‘abdomen knee’, I can type them into the textbox, and restrict the ontology list to just RadLex.


This manual approach works fine if you just want a few sentences annotated, but if you need to annotate multiple sentences in a systematic manner you need to do this programmatically. This is where the annotate web services come in. There are several client examples as well where you can find some sample code in several languages. There is a Java annotator client example, but unfortunately there is no C# example. For something I’m trying to implement I need to (or rather, I prefer to) use C#. Translating some Java code to C# isn’t hard, but in case someone’s looking for a ready-to-use C# example, following is a function/method you can use:

private void GetData()
            Uri address = new Uri("");

            // Create the web request  
            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

            // Set type to POST  
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Annotator Client Example - Annotator";

            String text = "abdomen knee";

            StringBuilder data = new StringBuilder();

            // Configure the form parameters
            data.Append("longestOnly=" + "false&");
            data.Append("wholeWordOnly=" + "true&");
            data.Append("filterNumber=" + "true&");
            data.Append("withDefaultStopWords=" + "true&");
            data.Append("isTopWordsCaseSensitive=" + "false&");
            data.Append("mintermSize=" + "3&");
            data.Append("scored=" + "true&");
            data.Append("withSynonyms=" + "true&");
            data.Append("ontologiesToExpand=" + "&");
            data.Append("ontologiesToKeepInResult=" + "1057&"); // from Use comma sep. list of ont. ids
            data.Append("isVirtualOntologyId=" + "true&");
            data.Append("semanticTypes=" + "&");
            data.Append("levelMax=" + "0&");
            data.Append("mappingTypes=" + "&"); //null, Automatic, Manual 
            data.Append("textToAnnotate=" + text + "&");
            data.Append("format=" + "xml&"); //Options are 'text', 'xml', 'tabDelimited'   
            data.Append("apikey=" + "YOUR_KEY");

            // Create a byte array of the data we want to send  
            byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());

            // Set the content length in the request headers  
            request.ContentLength = byteData.Length;

            // Write data  
            using (Stream postStream = request.GetRequestStream())
                postStream.Write(byteData, 0, byteData.Length);

            // Get response  
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

                XmlDocument doc = new XmlDocument();

                XmlNodeList nodes = doc.SelectNodes("/success/data/annotatorResultBean/annotations/annotationBean");
                foreach (XmlNode node in nodes)
                    string radlexId = node.SelectSingleNode("concept/localConceptId").InnerText.Split('/')[1];
                    string radlexDescription = node.SelectSingleNode("concept/preferredName").InnerText;

                // Or print to console [uncomment line: doc.Load(response.GetResponseStream());]
                //StreamReader reader = new StreamReader(response.GetResponseStream());

You can set breakpoints at radlexID/radlexDescription to see that what you get programmatically is exactly the same as what you get when you type the text directly into website. Per the comment in the code, pick the list of ontologies you are interested in from and pass it as a comma separated list (e.g., if I’m interested in RadLex and Snomed, I’ll use:
“data.Append(“ontologiesToKeepInResult=” + “1057,1353&”);
where 1353 is the ID for Snomed.

Before you can use the web service, you’ll need to sign up at and get an API key first (which you will then pass as the value for ‘apikey’).

Posted in General | Tagged , , , , , | 1 Comment

Extracting RadLex codes and descriptions from ontology

I was trying to find a flat list of RadLex terms that just had the IDs and descriptions. You can easily browse RadLex using the RadLex Term Browser,  but my requirement was to do a quick lookup using a local database. I couldn’t find something online that was readily available, so I wrote some code to extract just the Id, description fields from RadLex.owl (which you can download from In the attached file you can find the list of 34k+ RadLex terms. It’s an Excel file, so you can easily import it into a SQL Server or MySQL  database. I can’t upload a tab delimited .txt file to (see, so  if anyone needs the file in a different format, just let me know.

8-Nov-2012 update: Seems like there’s a new SQL script that contains all RadLex terms – I haven’t used it yet, but might be better/newer/more complete than the original Excel file I put together.

Posted in General | Tagged , , | 5 Comments