|
3 | 3 | # License: BSD 3 clause |
4 | 4 |
|
5 | 5 | """ |
6 | | -Utility functions for bigfish.stack subpackage. |
| 6 | +Utility functions for simfish package. |
7 | 7 | """ |
8 | 8 |
|
9 | 9 | import os |
| 10 | +import sys |
| 11 | +import zipfile |
| 12 | + |
10 | 13 | import numpy as np |
11 | 14 | import pandas as pd |
| 15 | + |
12 | 16 | import bigfish.stack as stack |
13 | 17 |
|
| 18 | +from urllib.request import urlretrieve |
| 19 | + |
14 | 20 |
|
15 | 21 | # ### Templates ### |
16 | 22 |
|
| 23 | +def load_extract_template(path_output, verbose=True): |
| 24 | + """Download template dataset zipfile and extract it. |
| 25 | +
|
| 26 | + Parameters |
| 27 | + ---------- |
| 28 | + path_output : str |
| 29 | + Path location to save dataset. |
| 30 | + verbose : bool, default=True |
| 31 | + Show download progression. |
| 32 | +
|
| 33 | + Returns |
| 34 | + ------- |
| 35 | + path_final : str |
| 36 | + Path of the downloaded dataset. |
| 37 | +
|
| 38 | + """ |
| 39 | + # check parameters |
| 40 | + stack.check_parameter( |
| 41 | + path_output=str, |
| 42 | + verbose=bool) |
| 43 | + |
| 44 | + # get remote url |
| 45 | + remote_url = "https://zenodo.org/record/6106718/files/templates.zip" |
| 46 | + |
| 47 | + # get output paths |
| 48 | + path_download = os.path.join(path_output, "templates.zip") |
| 49 | + path_final = os.path.join(path_output, "templates") |
| 50 | + |
| 51 | + # download and save data |
| 52 | + if verbose: |
| 53 | + urlretrieve(remote_url, path_download, _reporthook) |
| 54 | + print() |
| 55 | + else: |
| 56 | + urlretrieve(remote_url, path_download) |
| 57 | + |
| 58 | + # extract zipfile |
| 59 | + with zipfile.ZipFile(path_download, 'r') as zip_ref: |
| 60 | + zip_ref.extractall(path_output) |
| 61 | + |
| 62 | + # remove zipfile |
| 63 | + os.remove(path_download) |
| 64 | + if verbose: |
| 65 | + print("Templates downloaded and ready!") |
| 66 | + |
| 67 | + return path_final |
| 68 | + |
| 69 | + |
| 70 | +def _reporthook(count, block_size, total_size): |
| 71 | + if count == 0: |
| 72 | + pass |
| 73 | + else: |
| 74 | + progress_size = int(count * block_size / (1024 * 1024)) |
| 75 | + percent = min(int(count * block_size * 100 / total_size), 100) |
| 76 | + sys.stdout.write("\r...{0}% ({1}MB)".format(percent, progress_size)) |
| 77 | + sys.stdout.flush() |
| 78 | + |
| 79 | + return |
| 80 | + |
| 81 | + |
17 | 82 | def read_index_template(path_template_directory): |
18 | 83 | """Load and read dataframe with templates metadata. |
19 | 84 |
|
|
0 commit comments