Skip to content

Commit 3300978

Browse files
committed
BUG: fix various errors
* fix inconsistent use of class/instance variables * fix instance download tests accordingly
1 parent 5b1738b commit 3300978

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

idc_index/index.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ def __init__(self):
133133
},
134134
}
135135

136+
# these will point to the dataframes containing the respective indices, once installed
137+
self.sm_index = None
138+
self.sm_instance_index = None
139+
self.clinical_index = None
140+
136141
# Lookup s5cmd
137142
self.s5cmdPath = shutil.which("s5cmd")
138143
if self.s5cmdPath is None:
@@ -355,7 +360,9 @@ def fetch_index(self, index_name) -> None:
355360
# self.index[["series_aws_url", "SeriesInstanceUID"]],
356361
# on="SeriesInstanceUID", how="left"
357362
# )
358-
setattr(self.__class__, index_name, index_table)
363+
# TODO: consider switching to class variable!
364+
# setattr(self.__class__, index_name, index_table)
365+
setattr(self, index_name, index_table)
359366
self.indices_overview[index_name]["installed"] = True
360367
self.indices_overview[index_name]["file_path"] = filepath
361368

@@ -695,12 +702,18 @@ def get_instance_file_URL(self, sopInstanceUID, source_bucket_location="aws"):
695702
# sm_instance_index is required to complete this operation - install it!
696703
self.fetch_index("sm_instance_index")
697704

698-
if sopInstanceUID not in self.sm_instance_index["SOPInstanceUID"].values:
705+
if self.sm_instance_index is None:
706+
logger.error(
707+
"sm_instance_index could not be installed. Please install it first using fetch_index."
708+
)
709+
return None
710+
711+
if sopInstanceUID not in self.sm_instance_index["SOPInstanceUID"].values: # pylint: disable=unsubscriptable-object
699712
raise ValueError("SOPInstanceUID not found in IDC sm_instance_index.")
700713

701714
# merge with the main index to get series_aws_url
702-
selected_instance_df = self.sm_instance_index[
703-
self.sm_instance_index["SOPInstanceUID"] == sopInstanceUID
715+
selected_instance_df = self.sm_instance_index[ # pylint: disable=unsubscriptable-object
716+
self.sm_instance_index["SOPInstanceUID"] == sopInstanceUID # pylint: disable=unsubscriptable-object
704717
].copy()[["SeriesInstanceUID", "SOPInstanceUID", "crdc_instance_uuid"]]
705718
selected_instance_df = pd.merge(
706719
selected_instance_df,
@@ -1765,8 +1778,8 @@ def download_from_selection(
17651778
# If SOPInstanceUID(s) are given, we need to join the main index with the instance-level index
17661779
sm_instance_index = None
17671780
if sopInstanceUID:
1768-
if hasattr(
1769-
self, "sm_instance_index"
1781+
if (
1782+
self.sm_instance_index is not None
17701783
): # check if instance-level index is installed
17711784
download_df = self.sm_instance_index
17721785
sm_instance_index = self.sm_instance_index
@@ -2182,12 +2195,12 @@ def sql_query(self, sql_query):
21822195
logger.debug("Executing SQL query: " + sql_query)
21832196
# TODO: find a more elegant way to automate the following: https://www.perplexity.ai/search/write-python-code-that-iterate-XY9ppywbQFSRnOpgbwx_uQ
21842197
index = self.index
2185-
if hasattr(self, "sm_index"):
2198+
if self.sm_index is not None:
21862199
sm_index = self.sm_index
2187-
if hasattr(self, "sm_instance_index"):
2200+
if self.sm_instance_index is not None:
21882201
sm_instance_index = self.sm_instance_index
2189-
if hasattr(self, "clinical_index"):
2202+
if self.clinical_index is not None:
21902203
clinical_index = self.clinical_index
2191-
if hasattr(self, "prior_versions_index"):
2204+
if self.prior_versions_index is not None:
21922205
prior_versions_index = self.prior_versions_index
21932206
return duckdb.query(sql_query).to_df()

tests/idcindex.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,7 @@ def test_download_dicom_series(self):
190190
self.assertEqual(sum([len(files) for r, d, files in os.walk(temp_dir)]), 3)
191191

192192
def test_download_dicom_instance(self):
193-
i = IDCClient()
194-
i.fetch_index("sm_instance_index")
193+
self.client.fetch_index("sm_instance_index")
195194
with tempfile.TemporaryDirectory() as temp_dir:
196195
self.client.download_dicom_instance(
197196
sopInstanceUID="1.3.6.1.4.1.5962.99.1.528744472.1087975700.1641206284312.14.0",
@@ -210,8 +209,7 @@ def test_download_dicom_series_gcs(self):
210209
self.assertEqual(sum([len(files) for r, d, files in os.walk(temp_dir)]), 3)
211210

212211
def test_download_dicom_instance_gcs(self):
213-
i = IDCClient()
214-
i.fetch_index("sm_instance_index")
212+
self.client.fetch_index("sm_instance_index")
215213
with tempfile.TemporaryDirectory() as temp_dir:
216214
self.client.download_dicom_instance(
217215
sopInstanceUID="1.3.6.1.4.1.5962.99.1.528744472.1087975700.1641206284312.14.0",

0 commit comments

Comments
 (0)