Monday, March 14, 2011

Iterate through a variable depth nested hash in ruby

I had a hash with variable depth and wanted to get info from it without being lost in the iteration.
Essentially, this was a sort of hierarchical arrangement of diagnoses from which I wanted to get the full diagnosis name by traversing the hash to the last node and back.

Well, I fail to exactly explain what I had and what I wanted to achieve. I guess an example will do the trick.
Here is my Hash:
{
"HEART FAILURE":{
"RIGHT VENTRICULAR":{},
"LEFT VENTRICULAR":{},
"CONGESTIVE CARDIAC":{},
"OTHER":{}
},
"MYOCARDIAL INFARCTION":{},
"ANGINA":{},
"HEPATITIS":{
"ACUTE":{
"DRUG INDUCED":{},
"HEPATITIS A":{},
"HEPATITIS B":{},
"HEPATITIS C":{},
"UNEXPLAINED":{},
"OTHER":{}
},

"CHRONIC":{
"DRUG INDUCED":{},
"HEPATITIS A":{},
"HEPATITIS B":{},
"HEPATITIS C":{},
"UNEXPLAINED":{},
"OTHER":{}
},

"OTHER":{}
}
}


What I wanted to get is an array which looks like

["HEART FAILURE RIGHT VENTRICULAR",
"HEART FAILURE LEFT VENTRICULAR",
"HEART FAILURE CONGESTIVE CARDIAC",
"HEART FAILURE OTHER",
"MYOCARDIAL INFARCTION",
.
.
.
"HEPATITIS ACUTE DRUG INDUCED",
"HEPATITIS ACUTE HEPATITIS A",
.
.
.
"HEPATITIS CHRONIC OTHER",
"HEPATITIS CHRONIC UNEXPLAINED",
"HEPATITIS OTHER"
]


I hope you get the idea!

I guess what I needed was a classical Depth First Search algorithm and this is how I implemented it in Ruby.

def self.all_diagnoses(diagnosis_hash = my_hash, deep_list = [], short_list = [])
diagnosis_hash.each do |k,v|
if v.blank? #if the value is an empty hash, then we are at the last node
short_list.push k
deep_list << short_list.join(" ")
short_list.pop
else
short_list.push k
all_diagnoses(v, deep_list, short_list)
short_list.pop
end
end
deep_list.sort
end


Have tested this on a hash with up to three levels and it works!
Any observations? Post them below

Wednesday, August 26, 2009

Uniquify an array of hashes

Almost at the close of one of the cohort tasks I am working on and I need to uniquify an array of hashes. Many suggestions point to redefining #hash and #eql? for the hash instances before I put them in the array. And voila! there was anoitulos
and I qoute:
"Assuming your hashes are always single key-value pairs, this will work:

a.map {|h| h.to_a[0]}.uniq.map {|k,v| {k => v}}

Hash.to_a creates an array of key-value arrays, so the first map gets you:

[[:a, 1], [:a, 2], [:a, 1]]

uniq on Arrays does what you want, giving you:

[[:a, 1], [:a, 2]]

and then the second map puts them back together as hashes again.
"

Tuesday, August 04, 2009

Denzel

He's 21 months old now. He wants to do whatever grownups do. If I buy him a toy cellphone, he's gonna only use it for 1 day and he's back to crying for my cell.

Monday, July 27, 2009

ACEDN.org

Just launched my first proffessional website http://acedn.org. It has been a great learning experience for me and it has reaffirmed my passion for the field. The client and founder of the website's organisation Tony Ayuninjam has been a great client and he's a man with a helluva idea. Just checkout what he has in store for Malawi and Africa in general!

Tuesday, March 31, 2009

Monitor this. Monitor that!

This is Baobab Health Trust's 44th Day in the new offices at Community Health Sciences Unit and still this developer hasn't finalized BASM. Well, he has an explanation to that!
  • The link to remote site hasn't been persistent
  • He was committed to development of reports for Mateme sites and
  • He has been busy with feature development and bug fixes for BART 9.2
What on the www is BART, Mateme and BASM?

Baobab Health Trust(organisation this developer works for), a Malawi-based non governmental organization, addresses the health crisis in Malawi by innovating appropriate technology that tackles developing country health care problems. The core of Baobab's approach is the use of easy-to-use touchscreen clinical workstations at the point of patient care. This system efficiently and accurately guides low-skilled health care workers through the diagnosis and treatment of patients according to national protocols. The system also captures timely and accurate data that is used by health care workers during patient visits to supplement decision making.

One of the system Baobab Health developed and supports is BART . This is a Ruby on Rails application running on a low cost touch screen work station which aides in the treatment and care of HIV / AIDS patients.

One other system is Mateme which developed using the same technology as BART but is used in rural health centres for outpatient registration and diagnoses.

Baobab Health uses low-cost information appliances and servers that are significantly more robust in harsh environments than traditional computers and these are powered by generic 12v batteries producing a combined voltage of 48 volts.

One challenge was monitoring of these batteries remotely to identify faulty batteries before power supply reaches critically low levels. Then comes in BASM - Baobab Server Monitoring system. This software was primary looking at addressing early warning mechanism for power supply but I will be looking froward to extending this to include other server critical resources.



Friday, November 14, 2008

Back to my roots?

Dad died in May 1994. Since then, mum being very religious catholic, has been organising a requiem mass annually to pray for the departed in the family . Initially, it was only for late dad but now it has gone as far as every known death in the extended family, even though the dead are non-catholics. This year the mass was celebrated on 8th Nov within the grounds of Likangala model primary school where mum is head teacher. My journey to Likangala was one which raised a lot of fond memories and there were a lot of new learning experiences on the way from Lilongwe to Likangala - Zomba(including bribing a police officer to let me pass through the Chingeni police road block without producing a receipt for the 5 new blankets I was taking to Zomba. OH MY GOD!! This was a really!really! new experience. I had... well I reserve this for a blog entry).

This blog has been in existence for some years but I only decided to revamp it today and I will be writing a whole lot of blog entries from the paragraph above. So stick around.

Tuesday, June 20, 2006

This is a forum for sanity minds. You will get some unquestioned answers. Does that make any sense?