@@ -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 ()
0 commit comments