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(http://www.astrojyoti.com/bhagavadgitamp3.htm) 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
http://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita00-intro.mp3
Chapter 1 – The Despondency of Arjunahttp://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita01.mp3
    
Chapter 2 – Sankhya Yogahttp://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita02.mp3 
Chapter 3 – The Yoga of Actionhttp://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita03.mp3   
Chapter 4 – The Yoga of Wisdomhttp://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita04.mp3
Chapter 5 – The Yoga of Renunciation of Actionhttp://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita05.mp3
Chapter 6 – The Yoga of Meditationhttp://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita06.mp3
Chapter 7 – The Yoga of Wisdom & Realisationhttp://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita07.mp3
Chapter 8 – The Yoga of the Imperishable Brahmanhttp://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita08.mp3
Chapter 9 – The Yoga of the Kingly Science & Secrethttp://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita09.mp3
Chapter 10 – The Yoga of the Divine Glorieshttp://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita10.mp3
Chapter 11 – The Yoga of the Vision of Cosmic Formhttp://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita11.mp3
Chapter 12 – The Yoga of Devotionhttp://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita12.mp3
Chapter 13 – The Yoga of Distinctionhttp://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita13.mp3
Chapter 14 – The Yoga of the Division of Tri-Gunashttp://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita14.mp3
Chapter 15 – The Yoga of the Supreme Spirithttp://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita15.mp3
Chapter 16 – The Yoga of the Divine & the Demonhttp://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita16.mp3
Chapter 17 – The Yoga of the Threefold Faithhttp://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita17.mp3
Chapter 18 – The Yoga of Liberation by Renunciationhttp://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita18.mp3
Appendixhttp://media.astrojyoti.com/stotras/BhagavdGeeta/Bhagavad-gita-app.mp3


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 https://chrome.google.com/webstore/detail/table-capture/iebpjdmgckacbodjpijphcplhebcmeop?hl=en . 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 http://eciresults.nic.in/ConstituencywiseS10171.htm  . 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 : http://diasporaindica.com/rest/ker.json
    2. Constituencies  : http://diasporaindica.com/rest/parties.json
    3. Cities               : http://diasporaindica.com/rest/cities.json

    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. http://diasporaindica.com/karnic_bjp_kjp
    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. http://diasporaindica.com/karnataka-election-results?candidate_name=&citi_name=&party_name=Independent&items_per_page=All&offset=0&order=votes_recieved&sort=asc 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 www.devthon.org which give me the ideal ambiance to work on this project. Thanks to my company www.azrisolutions.com for sponsoring the devthon event and supporting me.