|
a |
|
b/examples/utils.py |
|
|
1 |
import os |
|
|
2 |
from urllib.request import urlopen |
|
|
3 |
|
|
|
4 |
|
|
|
5 |
def download_file(url, filepath): |
|
|
6 |
response = urlopen(url) |
|
|
7 |
|
|
|
8 |
target_dir = os.path.dirname(filepath) |
|
|
9 |
if target_dir and not os.path.exists(target_dir): |
|
|
10 |
os.makedirs(target_dir) |
|
|
11 |
|
|
|
12 |
# Check if the request was successful |
|
|
13 |
if response.status == 200: |
|
|
14 |
with open(filepath, 'wb') as f: |
|
|
15 |
f.write(response.read()) |
|
|
16 |
else: |
|
|
17 |
print(f"Failed to download file. Status code: {response.status_code}") |
|
|
18 |
|
|
|
19 |
return filepath |
|
|
20 |
|
|
|
21 |
def get_demo_data(): |
|
|
22 |
demo_data_url = "https://www.dropbox.com/scl/fi/covbvo6f547kak4em3cjd/sybil_example.zip?rlkey=7a13nhlc9uwga9x7pmtk1cf1c&st=dqi0cf9k&dl=1" |
|
|
23 |
|
|
|
24 |
zip_file_name = "sybil_example.zip" |
|
|
25 |
cache_dir = os.path.expanduser("~/.sybil") |
|
|
26 |
zip_file_path = os.path.join(cache_dir, zip_file_name) |
|
|
27 |
os.makedirs(cache_dir, exist_ok=True) |
|
|
28 |
if not os.path.exists(zip_file_path): |
|
|
29 |
print(f"Downloading demo data to {zip_file_path}") |
|
|
30 |
download_file(demo_data_url, zip_file_path) |
|
|
31 |
|
|
|
32 |
demo_data_dir = os.path.join(cache_dir, "sybil_example") |
|
|
33 |
image_data_dir = os.path.join(demo_data_dir, "sybil_demo_data") |
|
|
34 |
if not os.path.exists(demo_data_dir): |
|
|
35 |
print(f"Extracting demo data to {demo_data_dir}") |
|
|
36 |
import zipfile |
|
|
37 |
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref: |
|
|
38 |
zip_ref.extractall(demo_data_dir) |
|
|
39 |
|
|
|
40 |
dicom_files = os.listdir(image_data_dir) |
|
|
41 |
dicom_files = [os.path.join(image_data_dir, x) for x in dicom_files] |
|
|
42 |
return dicom_files |