Utils Download#
download
#
| CLASS | DESCRIPTION |
|---|---|
URLValidationError |
Raised when URL validation fails. |
DownloadError |
Base exception for download-related errors. |
DownloadResult |
Container for download result metadata. |
| FUNCTION | DESCRIPTION |
|---|---|
download_url |
Download file from URL. |
download_urls |
Download multiple files from URLs. |
list_datasets |
List available datasets. |
get_dataset_info |
Get detailed information about a specific dataset. |
download_dataset |
Download a dataset from the registry. |
Classes#
URLValidationError
#
Bases: ValueError
Raised when URL validation fails.
DownloadError
#
Bases: Exception
Base exception for download-related errors.
DownloadResult(url: str, filepath: str, success: bool, file_size: int | None = None, duration: float | None = None, error: str | None = None)
#
Container for download result metadata.
Source code in spectre/utils/download.py
Functions#
validate_url(url: str) -> None
#
Validate URL format and scheme.
| PARAMETER | DESCRIPTION |
|---|---|
url
|
URL to validate.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
URLValidationError
|
If URL is invalid or unsupported scheme. |
Source code in spectre/utils/download.py
download_url(url: str, filepath: str, overwrite: bool = False, show_progress: bool = True, validate: bool = True, max_retries: int = 3, retry_delay: float = 1.0, timeout: float = 30.0, progress_callback: Callable[[int, int], None] | None = None) -> DownloadResult
#
Download file from URL.
| PARAMETER | DESCRIPTION |
|---|---|
url
|
URL to download file from.
TYPE:
|
filepath
|
Filepath to save downloaded file to.
TYPE:
|
overwrite
|
If True, overwrite existing file, by default False.
TYPE:
|
show_progress
|
Show download progress, by default True.
TYPE:
|
validate
|
Validate URL before downloading, by default True.
TYPE:
|
max_retries
|
Maximum number of retry attempts, by default 3.
TYPE:
|
retry_delay
|
Delay between retries in seconds, by default 1.0.
TYPE:
|
timeout
|
Download timeout in seconds, by default 30.0.
TYPE:
|
progress_callback
|
Custom progress callback function(downloaded, total), by default None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DownloadResult
|
Download result with metadata. |
| RAISES | DESCRIPTION |
|---|---|
URLValidationError
|
If URL validation fails. |
DownloadError
|
If download fails after all retries. |
ValueError
|
If filepath is invalid. |
Source code in spectre/utils/download.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
download_urls(urls: list[str], filepaths: list[str] | str, overwrite: bool = False, show_progress: bool = True, **kwargs) -> list[DownloadResult]
#
Download multiple files from URLs.
| PARAMETER | DESCRIPTION |
|---|---|
urls
|
List of URLs to download.
TYPE:
|
filepaths
|
List of filepaths or directory path. If string (directory), filenames will be inferred from URLs.
TYPE:
|
overwrite
|
If True, overwrite existing files, by default False.
TYPE:
|
show_progress
|
Show download progress, by default True.
TYPE:
|
**kwargs
|
Additional arguments passed to download_url().
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
list[DownloadResult]
|
List of download results. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If urls and filepaths lengths do not match. |
Examples:
>>> # Download to specific files
>>> results = download_urls(
... ["http://example.com/file1.txt", "http://example.com/file2.txt"],
... ["local1.txt", "local2.txt"],
... )
>>>
>>> # Download to directory (filenames from URLs)
>>> results = download_urls(
... ["http://example.com/file1.txt", "http://example.com/file2.txt"],
... "./",
... )
Source code in spectre/utils/download.py
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | |
list_datasets(tags: list[str] | None = None, show_details: bool = True) -> None
#
List available datasets.
| PARAMETER | DESCRIPTION |
|---|---|
tags
|
Filter datasets by tags, by default None.
TYPE:
|
show_details
|
Show detailed information, by default True.
TYPE:
|
Examples:
>>> list_datasets()
>>> list_datasets(tags=["molecular-dynamics"])
>>> list_datasets(show_details=False) # Just names
Source code in spectre/utils/download.py
get_dataset_info(dataset_name: str) -> dict
#
Get detailed information about a specific dataset.
| PARAMETER | DESCRIPTION |
|---|---|
dataset_name
|
Name of the dataset.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
Dataset information. |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If dataset is not found. |
Examples:
Source code in spectre/utils/download.py
download_dataset(dataset_name: str, filepath: str | None = None, create_dataset: bool = True, overwrite: bool = False, show_progress: bool = True, **kwargs) -> tuple[DownloadResult, Any | None]
#
Download a dataset from the registry.
| PARAMETER | DESCRIPTION |
|---|---|
dataset_name
|
Name of the dataset to download.
TYPE:
|
filepath
|
Custom filepath, by default None (uses dataset name).
TYPE:
|
create_dataset
|
Create DatasetWeighted object, by default True.
TYPE:
|
overwrite
|
Overwrite existing files, by default False.
TYPE:
|
show_progress
|
Show download progress, by default True.
TYPE:
|
**kwargs
|
Additional arguments passed to download_url() and create_dataset().
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[DownloadResult, DatasetWeighted | None]
|
Download result and optionally created dataset. |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If dataset is not found. |
Examples:
>>> # Download and create dataset
>>> result, dataset = download_dataset("adp-feat-dist-bf2")
>>> print(f"Downloaded: {result.success}")
>>> print(f"Dataset shape: {dataset.data.shape}")
>>>
>>> # Just download the file
>>> result, _ = download_dataset(
... "adp-feat-dist-bf2",
... filepath="my_data.txt",
... create_dataset=False,
... )
Source code in spectre/utils/download.py
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 | |