Monday, September 16, 2013

India : A map of Sex Ratios Districtwise

I have not spent much time on javascript and have always avoided any opportunity to work with javascript related frameworks. Recently after many postponements I started working on D3.js and here is the output of what I could achieve over a weekend.

If you want to use the svg along with state and district codes visit my github repo

A few of the observations that I could made from the graph are
  1. Coastal regions have a better sex ratio.
  2. South India does better when compared to North India.
  3. I was assuming that Karnataka would have a better ratio than that of Bangalore. But I was surprised to find that AP does better than Karnataka.
  4. In-fact a viz done by a friend of mine addresses precisely that. As per the viz there is a lot of improvement over the last decade which kind of justifies my surprise ;)

Read further for credits and my learning resources.
Note that this is a very crude implementation. I wanted to get the working map out there and then work on improving features and code quality.
  1. DashingD3JS helped me get started and thinking with joins helped me understand joins better.
  2. I used the color palettes form Color Brewer. It has a nice set of palettes and comes shipped with D3. A nice graphical view of the same is a smart implementation done using D3.js and ColorBrewer again.
  3. I used some sample codes from here.
  4. I used the svg map from wikimedia
  5. It used the names and didn't use the district codes assigned by the Census 2011. There was no easy way of mapping these details.
  6. I used the official document from census and used this temporarily identify the missing codes and names.
  7. Most of the changes were because of the spelling mistake or abbreviations. I had to fix these manually.
  8. I ignored that fact that there can be different districts with same name in different states. This caused a little headache and tookup more than 3-4 hours to sort it out.
  9. I have shared the update svg map along with state and district codes on github

Tuesday, August 20, 2013

Gita Audio files for download

Download Audio Files of Gita(Bhagavadgita) Chanting in Sanskrit. 

Recently my mom wanted the sankrit chanting of Gita slokas on her phone. I googled and was surprised that I had to spend more than half an hour to search and download the files. The closest was this( but the links were all broken. I had to manually copy the urls to browser and download. If you are searching for the download of Gita files you are in luck. Just click on the download links in below table.

If you are a developer and want to know what I did read on.

Introductory Dhyana Slokas
Chapter 1 – The Despondency of Arjuna
Chapter 2 – Sankhya Yoga 
Chapter 3 – The Yoga of Action   
Chapter 4 – The Yoga of Wisdom
Chapter 5 – The Yoga of Renunciation of Action
Chapter 6 – The Yoga of Meditation
Chapter 7 – The Yoga of Wisdom & Realisation
Chapter 8 – The Yoga of the Imperishable Brahman
Chapter 9 – The Yoga of the Kingly Science & Secret
Chapter 10 – The Yoga of the Divine Glories
Chapter 11 – The Yoga of the Vision of Cosmic Form
Chapter 12 – The Yoga of Devotion
Chapter 13 – The Yoga of Distinction
Chapter 14 – The Yoga of the Division of Tri-Gunas
Chapter 15 – The Yoga of the Supreme Spirit
Chapter 16 – The Yoga of the Divine & the Demon
Chapter 17 – The Yoga of the Threefold Faith
Chapter 18 – The Yoga of Liberation by Renunciation

Techies read on. Well I was not in mood to spend time on automation/macros. So today I took out some time to automate those and learnt a few things in the process :P

  1. It is difficult to copy the columns from a HTML table. Download . This extension will help you highlight(lowlight in fact :P) and copy the table contents to a google doc.
  2. In google doc you can select a column. I selected the column that contains the download url (just text and not the download link)
  3. By the way when you have a column that is created using formula but you want to convert that to just plain values. Do a copy of the column and then do a past special with 'paste only values' option.
  4. You can use  =LEFT(B2, FIND("mp3", B2)+2) to extract the text ending with first occurance of mp3
  5. To force download a file on the client side you can use the download attribute in the anchor tag. Check out my links Download MP3 to know more :)
    BTW if you are thinking why am I mixing up stuff, I recently realised that the reason most people don't share their learnings is that they try to make it very organized and in the process loose their motivation. So I am planning to share my knowledge more in an adhoc manner which reduces the burden on me.

    Sunday, June 16, 2013

    Hacking into Karnataka Election Results Website

    "Statistics hide more than what they reveal", I could related to the meaning of this quote after coming across a post election results number crunching done by an analyst.

    It all started with this analysis of facebook. This analysis goes on to say that if BJP and KJP had stayed together, their combined votes would have been greater than that of the Congress in 93 constituencies. What this analyst omits is whether BJP+KJP would have won in all these 93 seats? Definitely not, because there might be other parties who could have garnered more votes that BJP+KJP or Congress. Though the analyst doesn't mention that BJP would have won in 93 seats, it kind of leads the end user to believe so. I am not blaming the analyst here, but sharing my opinion of what it made me think.

    So out of curiosity I just wanted to see in how many places BJP+KJP votes would have been greater than the winning votes. So I set out find out the data of the Karnataka Election results. I was not able to find any open data that was in a consumable format for a developer[Kindly let me know if there is one. I thought it was better to hack into their site than file for an RTI]. All I had was this  . An ill designed website(Can you still imagine that they use tables within tables :P) that would let me check the results only by the constituencies and not any other parameter. So if I need to get the complete data I need to visit 223 such pages that gives me results for their respective constituencies. So I looked at the URL naming patterns and it turned out that there was a co-relation between the results url and the constituency code. So I took a list of all the constituency codes from the drop-down on the page, used some notepad++ macros to clean up the HTML mess and make it a php array. Once I had the array of constituency codes I wrote a simple script to scrape the 223 urls and get the relevant data. Then I imported this data into my database using a CSV file. I wrote a few views handlers to expose the database table to views on my drupal site. Created three views to display the data.

    I also made the output available in a json format so that other developers can use the data to form their own visualizations.

    The end points are
    1. Votes recieved :
    2. Constituencies  :
    3. Cities               :

    Using these data I was able to validate the following two theories.
    1. BJP + KJP : BJP along with KJP could have won only in 75 constituencies, thought there combined votes would have been more than Congress in 93 locations.
    2. Marginal Votes : Since the Indian electoral system follows winner takes it all approach, marginal votes play an important role deciding the outcome. Infact it is because of these marginal votes that the corrupt politicians are ready to pay huge premiums of money and liquour for every single vote. And Independent parties though they may not have a huge share of votes, they can play the spoil sport by ruining some big shots chances. So I did a simple calculation to see in how many places the sum of all votes received by all the independents was greater than the difference between the top two candidates. Surprisingly it turns out that they could have played a crucial role in 47 constituencies.
    3. That made me curious about the Independent parties. So I dug in a little more deep using the screens I had created. And I found out that there are 1217 Independent candidates who participated in these elections. with the least number of votes being 52 votes :P
    The following are some of the visulization I built using this data. 

    In the first visualization I have ordered Congress first, BJP + KJP next. I have kept their colours similar so that you get a sense of how they could have performed togehter and then followed by JDU and independents. 

    With this basic Proof of Concept of how the data can be made publicly accessible using the APIs and how the data can be used to make various analysis and visulizations of the results I hope the Gov will consider making the next election results open data. 

    * I was able to complete all of these in a span of 3 days. Special thanks to which give me the ideal ambiance to work on this project. Thanks to my company for sponsoring the devthon event and supporting me.