Skip to content

Commit d223e22

Browse files
committed
add loader function for templates data
1 parent 234c578 commit d223e22

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

simfish/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
__version__ = "0.1.0dev"
1616

1717

18+
from .utils import load_extract_template
1819
from .utils import read_index_template
1920
from .utils import build_templates
2021
from .utils import build_template
@@ -39,6 +40,7 @@
3940

4041

4142
_utils = [
43+
"load_extract_template",
4244
"read_index_template",
4345
"build_templates",
4446
"build_template"]

simfish/utils.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,82 @@
33
# License: BSD 3 clause
44

55
"""
6-
Utility functions for bigfish.stack subpackage.
6+
Utility functions for simfish package.
77
"""
88

99
import os
10+
import sys
11+
import zipfile
12+
1013
import numpy as np
1114
import pandas as pd
15+
1216
import bigfish.stack as stack
1317

18+
from urllib.request import urlretrieve
19+
1420

1521
# ### Templates ###
1622

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+
1782
def read_index_template(path_template_directory):
1883
"""Load and read dataframe with templates metadata.
1984

0 commit comments

Comments
 (0)