Skip to content

Commit b0c3e28

Browse files
authored
ENH: add helper function to load clinical tables (#134)
1 parent afe8975 commit b0c3e28

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

idc_index/index.py

+23
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,29 @@ def fetch_index(self, index_name) -> None:
362362
self.clinical_data_dir,
363363
)
364364

365+
def get_clinical_table(self, table_name):
366+
"""
367+
Returns the requested clinical table as a pandas DataFrame.
368+
369+
Args:
370+
table_name (str): Name of the clinical table to be loaded.
371+
372+
Returns:
373+
pandas.DataFrame: The requested clinical table.
374+
"""
375+
if self.clinical_data_dir is None:
376+
logger.error(
377+
"Clinical data directory is not available. Please fetch clinical_index first."
378+
)
379+
return None
380+
381+
table_path = os.path.join(self.clinical_data_dir, table_name)
382+
if not os.path.exists(table_path):
383+
logger.error(f"Table {table_name} is not found in {table_path}.")
384+
return None
385+
386+
return pd.read_parquet(table_path)
387+
365388
def get_collections(self):
366389
"""
367390
Returns the collections present in IDC

tests/idcindex.py

+3
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,9 @@ def test_clinical_index_install(self):
563563
assert i.indices_overview["clinical_index"]["installed"] is True
564564
assert len(os.listdir(i.clinical_data_dir)) > 0
565565

566+
nlst_clinical = i.get_clinical_table("nlst_clinical")
567+
assert nlst_clinical is not None
568+
566569

567570
if __name__ == "__main__":
568571
unittest.main()

0 commit comments

Comments
 (0)