Skip to content

Commit

Permalink
Re-organize download_uci_data
Browse files Browse the repository at this point in the history
Summary: Re-organize download_uci_data

Reviewed By: Yonathae

Differential Revision: D54293308

fbshipit-source-id: 8e27c47b95596f1a415133364bb7bf53cd513131
  • Loading branch information
rodrigodesalvobraz authored and facebook-github-bot committed Feb 28, 2024
1 parent 7631a3b commit ca4d285
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions pearl/utils/uci_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,27 @@ def download_uci_data(data_path: str) -> None:
"""

for dataset_name in uci_urls.keys():
url = uci_urls[dataset_name]["url"] + "/" + uci_urls[dataset_name]["file_name"]
filename = os.path.join(data_path, uci_urls[dataset_name]["file_name"])
unzipped_dataset_dirpath = os.path.join(data_path, dataset_name)
zip_filepath = os.path.join(data_path, uci_urls[dataset_name]["file_name"])

# Download the zip file
url = uci_urls[dataset_name]["url"] + "/" + uci_urls[dataset_name]["file_name"]
response = requests_get(url)
if response.status_code != 200:
raise Exception(f"Failed to download {dataset_name} dataset from {url}.")

# Locally save the zip file
with open(filename, "wb") as f:
with open(zip_filepath, "wb") as f:
f.write(response.content)

# Unzip the file
unzip_filepath = os.path.join(data_path, dataset_name)
try:
with zipfile.ZipFile(filename, "r") as z:
z.extractall(unzip_filepath)
with zipfile.ZipFile(zip_filepath, "r") as z:
z.extractall(unzipped_dataset_dirpath)
except zipfile.BadZipFile:
raise zipfile.BadZipFile(
f"Bad zip file: {filename}. Please delete corrupt file and run again."
f"Bad zip file: {zip_filepath}. Please delete corrupt file and run again."
)

# Delete the zip file
os.remove(filename)
os.remove(zip_filepath)

0 comments on commit ca4d285

Please sign in to comment.