@@ -119,6 +119,15 @@ def create_cohort(self, cohort: Cohort):
119119 cohort .cohort_end_date
120120 ))
121121
122+ # Method to insert cohort data in bulk from a dataframe
123+ def create_cohort_in_bulk (self , cohort_df : pd .DataFrame ):
124+ # make duckdb to treat cohort_df dataframe as a virtual table named "cohort_df"
125+ self .conn .register ("cohort_df" , cohort_df )
126+ self .conn .execute ('''
127+ INSERT INTO cohort (subject_id, cohort_definition_id, cohort_start_date, cohort_end_date)
128+ SELECT subject_id, cohort_definition_id, cohort_start_date, cohort_end_date FROM cohort_df
129+ ''' )
130+
122131 def get_cohort_definition (self , cohort_definition_id ):
123132 results = self .conn .execute (f'''
124133 SELECT id, name, description, created_date, creation_info, created_by FROM cohort_definition
@@ -417,12 +426,8 @@ def get_concept_hierarchy(self, concept_id: int):
417426
418427 results = self .execute_query (query , params = {"concept_id" : concept_id })
419428
420- # Collect all concept IDs involved in the hierarchy
421- concept_ids = set ()
422- for row in results :
423- concept_ids .add (row ['ancestor_concept_id' ])
424- concept_ids .add (row ['descendant_concept_id' ])
425-
429+ # Collect all unique concept IDs involved in the hierarchy using set comprehension
430+ concept_ids = {row ['ancestor_concept_id' ] for row in results } | {row ['descendant_concept_id' ] for row in results }
426431 # Fetch details of each concept
427432 concept_details = {}
428433 if concept_ids :
@@ -442,19 +447,17 @@ def get_concept_hierarchy(self, concept_id: int):
442447 ancestor_id = row ['ancestor_concept_id' ]
443448 descendant_id = row ['descendant_concept_id' ]
444449
445- if ancestor_id not in hierarchy :
446- hierarchy [ancestor_id ] = {"details" : concept_details [ancestor_id ], "children" : []}
447- if descendant_id not in hierarchy :
448- hierarchy [descendant_id ] = {"details" : concept_details [descendant_id ], "children" : []}
449- # Link descendants to their ancestor node
450- hierarchy [ancestor_id ]["children" ].append (hierarchy [descendant_id ])
451-
452- if descendant_id not in reverse_hierarchy :
453- reverse_hierarchy [descendant_id ] = {"details" : concept_details [descendant_id ], "parents" : []}
454- if ancestor_id not in reverse_hierarchy :
455- reverse_hierarchy [ancestor_id ] = {"details" : concept_details [ancestor_id ], "parents" : []}
456- # Link ancestors to their descendant (child) node
457- reverse_hierarchy [descendant_id ]["parents" ].append (reverse_hierarchy [ancestor_id ])
450+ ancestor_entry = hierarchy .setdefault (
451+ ancestor_id , {"details" : concept_details [ancestor_id ], "children" : []})
452+ descendant_entry = hierarchy .setdefault (
453+ descendant_id , {"details" : concept_details [descendant_id ], "children" : []})
454+ ancestor_entry ["children" ].append (descendant_entry )
455+
456+ desc_entry_rev = reverse_hierarchy .setdefault (
457+ descendant_id , {"details" : concept_details [descendant_id ], "parents" : []})
458+ ancestor_entry_rev = reverse_hierarchy .setdefault (
459+ ancestor_id , {"details" : concept_details [ancestor_id ], "parents" : []})
460+ desc_entry_rev ["parents" ].append (ancestor_entry_rev )
458461
459462 # Return the parent hierarchy and children hierarchy of the specified concept
460463 return reverse_hierarchy [concept_id ], hierarchy [concept_id ]
0 commit comments