-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
list_indices and fetch_index implemented #101
Changes from 4 commits
9e1817c
3e5b526
85f4091
0c36cb1
98375be
aa021d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
|
||
aws_endpoint_url = "https://s3.amazonaws.com" | ||
gcp_endpoint_url = "https://storage.googleapis.com" | ||
asset_endpoint_url = f"https://github.com/ImagingDataCommons/idc-index-data/releases/download/{idc_index_data.__version__}" | ||
|
||
logging.basicConfig(format="%(asctime)s - %(message)s", level=logging.INFO) | ||
logger = logging.getLogger(__name__) | ||
|
@@ -58,9 +59,8 @@ def client(cls) -> IDCClient: | |
return cls._client | ||
|
||
def __init__(self): | ||
# Read main index file | ||
file_path = idc_index_data.IDC_INDEX_PARQUET_FILEPATH | ||
|
||
# Read index file | ||
logger.debug(f"Reading index file v{idc_index_data.__version__}") | ||
self.index = pd.read_parquet(file_path) | ||
# self.index = self.index.astype(str).replace("nan", "") | ||
|
@@ -69,9 +69,26 @@ def __init__(self): | |
{"Modality": pd.Series.unique, "series_size_MB": "sum"} | ||
) | ||
|
||
self.indices_overview = { | ||
"index": { | ||
"description": "Main index containing one row per DICOM series.", | ||
"installed": True, | ||
"url": None, | ||
}, | ||
"sm_index": { | ||
"description": "DICOM Slide Microscopy series-level index.", | ||
"installed": False, | ||
"url": f"{asset_endpoint_url}/sm_index.parquet", | ||
}, | ||
"sm_instance_index": { | ||
"description": "DICOM Slide Microscopy instance-level index.", | ||
"installed": False, | ||
"url": f"{asset_endpoint_url}/sm_instance_index.parquet", | ||
}, | ||
} | ||
|
||
# Lookup s5cmd | ||
self.s5cmdPath = shutil.which("s5cmd") | ||
|
||
if self.s5cmdPath is None: | ||
# Workaround to support environment without a properly setup PATH | ||
# See https://github.com/Slicer/Slicer/pull/7587 | ||
|
@@ -80,16 +97,12 @@ def __init__(self): | |
if str(script).startswith("s5cmd/bin/s5cmd"): | ||
self.s5cmdPath = script.locate().resolve(strict=True) | ||
break | ||
|
||
if self.s5cmdPath is None: | ||
raise FileNotFoundError( | ||
"s5cmd executable not found. Please install s5cmd from https://github.com/peak/s5cmd#installation" | ||
) | ||
|
||
self.s5cmdPath = str(self.s5cmdPath) | ||
|
||
logger.debug(f"Found s5cmd executable: {self.s5cmdPath}") | ||
|
||
# ... and check it can be executed | ||
subprocess.check_call([self.s5cmdPath, "--help"], stdout=subprocess.DEVNULL) | ||
|
||
|
@@ -177,6 +190,45 @@ def get_idc_version(): | |
idc_version = Version(idc_index_data.__version__).major | ||
return f"v{idc_version}" | ||
|
||
def list_indices(self): | ||
""" | ||
Lists all available indices including their installation status. | ||
|
||
Returns: | ||
indices_overview (pd.DataFrame): DataFrame containing information per index. | ||
""" | ||
|
||
return pd.DataFrame.from_dict(self.indices_overview, orient="index") | ||
|
||
def fetch_index(self, index) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this function I would not just fetch the file, but also load it into the class variable named as the index name - consistent with the main index. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
""" | ||
Downloads requested index. | ||
|
||
Args: | ||
index (str): Name of the index to be downloaded. | ||
""" | ||
|
||
if index not in self.indices_overview: | ||
logger.error(f"Index {index} is not available and can not be fetched.") | ||
elif self.indices_overview[index]["installed"]: | ||
logger.warning( | ||
f"Index {index} already installed and will not be fetched again." | ||
) | ||
else: | ||
response = requests.get(self.indices_overview[index]["url"], timeout=30) | ||
if response.status_code == 200: | ||
filepath = os.path.join( | ||
idc_index_data.IDC_INDEX_PARQUET_FILEPATH.parents[0], | ||
f"{index}.parquet", | ||
) | ||
with open(filepath, mode="wb") as file: | ||
file.write(response.content) | ||
self.indices_overview[index]["installed"] = True | ||
else: | ||
logger.error( | ||
f"Failed to fetch index from URL {self.indices_overview[index]['url']}: {response.status_code}" | ||
) | ||
|
||
def get_collections(self): | ||
""" | ||
Returns the collections present in IDC | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the motivation to convert to a dataframe here? To me at least, this adds unnecessary complexity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to display it in a readable way to the user.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally would not this function at all. I think it pollutes the API without good reason. I don't think we would want to have a dedicated function that would make a DF for every other dict class variable, right?
If we want a convenience function that would make a DataFrame from a dict, it is better to add a generic helper function that would do this for any dict.