[importers] Add therapies/lesions/drugtakes in random importer
authorVincent Michel <vincent.michel@logilab.fr>
Fri, 17 Jan 2014 09:09:24 +0000
changeset 648 ca8a5168d561
parent 647 de3c857db0e6
child 649 767b35392314
[importers] Add therapies/lesions/drugtakes in random importer
importers/random_import.py
--- a/importers/random_import.py	Tue Dec 17 17:29:14 2013 +0100
+++ b/importers/random_import.py	Fri Jan 17 09:09:24 2014 +0000
@@ -42,13 +42,15 @@
 ###############################################################################
 ### UTILITY FUNCTIONS #########################################################
 ###############################################################################
-def random_date(_type=None):
+def random_date(_type=None, start=None, end=None):
     """
     This function will return a random datetime between two datetime
     objects.
     From http://stackoverflow.com/questions/553303/generate-a-random-date-between-two-other-dates
     """
-    if _type=='age':
+    if start:
+        end = PROJECT_DATETIMES[1]
+    elif _type=='age':
         start, end = AGE_DATETIMES[0], AGE_DATETIMES[1]
     else:
         start, end = PROJECT_DATETIMES[0], PROJECT_DATETIMES[1]
@@ -412,3 +414,40 @@
         import_constrat_images(store, subject_eid, center, study, mri_1, mri_2)
     store.flush()
     store.commit()
+
+
+    ###############################################################################
+    ### DISEASE/THERAPY ###########################################################
+    ###############################################################################
+    diseases = [r[0] for r in store.rql('Any X ORDERBY RANDOM() LIMIT 10 '
+                                        'WHERE X is Disease')]
+    locations = [r[0] for r in store.rql('Any X ORDERBY RANDOM() LIMIT 20 '
+                                         'WHERE X is BodyLocation')]
+    drugs = [r[0] for r in store.rql('Any X ORDERBY RANDOM() LIMIT 20 '
+                                         'WHERE X is Drug')]
+    if diseases:
+        for subject_eid in subjects:
+            disease = diseases[nr.randint(len(diseases))]
+            location = locations[nr.randint(len(locations))]
+            drug = drugs[nr.randint(len(drugs))]
+            store.relate(subject_eid, 'related_diseases', disease)
+            if nr.randint(3):
+                store.relate(subject_eid, 'related_lesions', location)
+            start_date = random_date()
+            stop_date = random_date(start=start_date)
+            therapy = store.create_entity('Therapy',
+                                          identifier=u'therapy_%s' % subject_eid,
+                                           start_date=start_date,
+                                           stop_date=stop_date if nr.randint(2)
+                                          else None)
+            store.relate(therapy.eid, 'therapy_for', disease)
+            store.relate(subject_eid, 'related_therapies', therapy.eid)
+            stop_date = random_date(start=start_date)
+            drugtake = store.create_entity('DrugTake',
+                                           start_taking_date=start_date,
+                                           stop_taking_date=stop_date if nr.randint(2)
+                                           else None,
+                                           taken_in_therapy=therapy.eid,
+                                           drug=drug)
+        store.flush()
+        store.commit()