Skip to content

Instantly share code, notes, and snippets.

@jeffrafter
Created October 15, 2008 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeffrafter/16980 to your computer and use it in GitHub Desktop.
Save jeffrafter/16980 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'fastercsv'
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
def set_patient_esitmated_age(patient, age, date_of_registration)
age = age.to_i
patient_estimated_birthyear = date_of_registration.year - age
patient_estimated_birthmonth = 7
patient_estimated_birthday = 1
patient.birthdate = Date.new(patient_estimated_birthyear, patient_estimated_birthmonth, patient_estimated_birthday)
patient.birthdate_estimated = true
end
def set_patient_reason_for_starting(patient, date, reason, adult_or_peds)
encounter = patient.encounters.create(:encounter_datetime => date, :encounter_type => EncounterType.find_by_name("HIV Staging").encounter_type_id)
yes = Concept.find_by_name("Yes").id
stage = reason.to_i
obs = stage < 3 ? Concept.find_by_name("CD4 Count < 250").id : Concept.find_by_name("WHO stage #{stage_number} #{adult_or_peds}").id
encounter.observations.create(:concept_id => obs, :value_coded => yes, :obs_datetime => date, :patient_id => patient.id)
end
def create_patient(options)
p = Patient.create(:gender => options[:gender])
set_patient_estimated_age(p, options[:age], options[:date_of_registration])
p.patient_names.create(:given_name => options[:given_name], :family_name => [:family_name])
# The address is blank do we need a TA/village?
p.patient_addresses.create
# Do we need to give a national id?
# Do we need to worry about preceding/padding 0s ("%04d" % arv_number)?
p.patient_identifiers.create(:identifier => "SAL#{options[:arv_number]}", :identifier_type => PatientIdentifierType.find_by_name("Arv national id").id)
p.patient_identifiers.create(:identifier => options[:occupation], :identifier_type => PatientIdentifierType.find_by_name("Occupation").id)
p.save!
p
end
def prescribe_drug(patient, drug, dose, frequency, encounter, date = nil)
encounter ||= patient.encounters.create(:encounter_datetime => date, :encounter_type => EncounterType.find_by_name("ART Visit").encounter_type_id)
encounter.observations.create(:value_drug => drug.drug_id, :value_text => frequency, :value_numeric => dose, :concept_id => Concept.find_by_name("Prescribed Dose").concept_id, :obs_datetime => encounter.encounter_datetime)
encounter
end
def dispense_drugs(patient, date, drugs)
encounter = patient.encounters.create(:encounter_datetime => date, :encounter_type => EncounterType.find_by_name("Give Drugs").encounter_type_id)
drugs.each{|hash|
order = encounter.orders.create(:order_type_id => 1)
drug_order = order.drug_orders.create(:drug_inventory_id => hash[:drug].drug_id, :quantity => hash[:quantity])
}
end
def read(filename)
starter_drug = Drug.find_by_name("Stavudine 30 Lamivudine 150")
continuation_drug = Drug.find_by_name("Stavudine 30 Lamivudine 150 Nevirapine 200")
FasterCSV.foreach(File.dirname(__FILE__) + "/../" + file_name) do |row|
# Need to tune the dates
# Need to tune occupation (so it uses the standard set)
# Need to tune outcome so it uses the concept names
# Need to add age (even if you force it yourself)
options = {
:given_name => row[0],
:family_name => row[1],
:gender => row[2],
:date_of_registration => row[3].to_date,
:outcome => Concept.find_like_name(row[4]).first,
:occupation => row[5].capitalize,
:reason_for_starting => row[6],
:arv_number => row[7],
:continuation => row[8] == 'continuation',
:age => row[9]
}
p = create_patient(row)
if (options[:continuation])
dispense_drugs(p, options[:date_of_registration], [{:drug => starter_drug, :quantity => 30}, {:drug => continuation_drug, :quantity => 15}]
elsif
dispense_drugs(p, options[:date_of_registration], [{:drug => continuation_drug, :quantity => 60}]
end
set_patient_reason_for_starting(p, options[:reason_for_starting], options[:date_of_registration], options[:age] <= 14 ? 'peds' : 'adult')
# Is this the correct outcome date? Death date and transfer date will likely be different.
# Will the outcome calculation assume that since they got drugs that they are On ART instead of what we put here?
# In any event keep it at the end of this function!
p.set_outcome(options[:outcome], options[:date_of_registration])
p.save!
end
end
User.current_user = User.find(:first)
Location.current_location = Location.find(:first)
puts "Creating old Patients"
read("b0nds_csv.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment