From f0c12fbdce04d09bf13b141d549e5e385c64caad Mon Sep 17 00:00:00 2001 From: Hind-M Date: Tue, 1 Jun 2021 18:39:31 +0200 Subject: First version allowing to fetch remote datasets --- src/python/test/test_remote_datasets.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/python/test/test_remote_datasets.py (limited to 'src/python/test') diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py new file mode 100644 index 00000000..c4e752a7 --- /dev/null +++ b/src/python/test/test_remote_datasets.py @@ -0,0 +1,22 @@ +# This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT. +# See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details. +# Author(s): Hind Montassif +# +# Copyright (C) 2021 Inria +# +# Modification(s): +# - YYYY/MM Author: Description of the modification + + +from gudhi.datasets import remote + +def test_fetch_remote_datasets(): + # Test files download from given urls + assert 'remote_datasets/spiral_2d.csv' == remote.fetch("https://raw.githubusercontent.com/Hind-M/gudhi-data/main/spiral_2d.csv", "spiral_2d.csv") + assert 'remote_datasets/sphere3D_pts_on_grid.off' == remote.fetch("https://raw.githubusercontent.com/Hind-M/gudhi-data/main/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off") + + # Test files download with checksums provided + assert 'remote_datasets/spiral_2d.csv' == remote.fetch("https://raw.githubusercontent.com/Hind-M/gudhi-data/main/spiral_2d.csv", "spiral_2d.csv", checksum_flag = True, + file_checksum = '37530355d980d957c4ec06b18c775f90a91e446107d06c6201c9b4000b077f38') + assert 'remote_datasets/sphere3D_pts_on_grid.off' == remote.fetch("https://raw.githubusercontent.com/Hind-M/gudhi-data/main/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off", + checksum_flag = True, file_checksum = '32f96d2cafb1177f0dd5e0a019b6ff5658e14a619a7815ae55ad0fc5e8bd3f88') -- cgit v1.2.3 From c2f0cf79af04ea3586a70c0a121a200353e989ac Mon Sep 17 00:00:00 2001 From: Hind-M Date: Wed, 2 Jun 2021 11:30:09 +0200 Subject: Add wrapping function to fecth spiral_2d.csv directly --- src/python/gudhi/datasets/remote.py | 24 +++++++++++++++++++++--- src/python/test/test_remote_datasets.py | 3 +++ 2 files changed, 24 insertions(+), 3 deletions(-) (limited to 'src/python/test') diff --git a/src/python/gudhi/datasets/remote.py b/src/python/gudhi/datasets/remote.py index 27076785..4a300b15 100644 --- a/src/python/gudhi/datasets/remote.py +++ b/src/python/gudhi/datasets/remote.py @@ -46,11 +46,11 @@ def fetch(url, filename, dirname = "remote_datasets", checksum_flag = False, fil Parameters ---------- url : string - The url to fetch the dataset from + The url to fetch the dataset from. filename : string - The filename to download + The name to give to downloaded file. dirname : string - The directory to save the file to. + The directory to save the file to. Default is "remote_datasets". checksum_flag : boolean To set if the user wants the file checksum. Default is 'False'. Note that if checksum_flag is set to 'True', the file_checksum must be provided. @@ -83,3 +83,21 @@ def fetch(url, filename, dirname = "remote_datasets", checksum_flag = False, fil file_checksum)) return file_path + +def fetch_spiral_2d(filename = "spiral_2d.csv", dirname = "remote_datasets"): + """ + Fetch spiral_2d.csv remotely + + Parameters + ---------- + filename : string + The name to give to downloaded file. Default is "spiral_2d.csv" + dirname : string + The directory to save the file to. Default is "remote_datasets". + + Returns + ------- + file_path: string + Full path of the created file. + """ + return fetch("https://raw.githubusercontent.com/Hind-M/gudhi-data/main/spiral_2d.csv", filename, dirname, True, '37530355d980d957c4ec06b18c775f90a91e446107d06c6201c9b4000b077f38') diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index c4e752a7..dc854e25 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -20,3 +20,6 @@ def test_fetch_remote_datasets(): file_checksum = '37530355d980d957c4ec06b18c775f90a91e446107d06c6201c9b4000b077f38') assert 'remote_datasets/sphere3D_pts_on_grid.off' == remote.fetch("https://raw.githubusercontent.com/Hind-M/gudhi-data/main/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off", checksum_flag = True, file_checksum = '32f96d2cafb1177f0dd5e0a019b6ff5658e14a619a7815ae55ad0fc5e8bd3f88') + + # Test spiral_2d.csv wrapping function + assert 'remote_datasets/spiral_2d.csv' == remote.fetch_spiral_2d() -- cgit v1.2.3 From baa2e67036dae8ec63321a4d9ff4e913780a8757 Mon Sep 17 00:00:00 2001 From: Hind-M Date: Wed, 2 Jun 2021 16:00:40 +0200 Subject: Modify test to consider both slash and backslash in the returned file path --- src/python/test/test_remote_datasets.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'src/python/test') diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index dc854e25..a822ebaa 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -9,17 +9,36 @@ from gudhi.datasets import remote +import re def test_fetch_remote_datasets(): # Test files download from given urls - assert 'remote_datasets/spiral_2d.csv' == remote.fetch("https://raw.githubusercontent.com/Hind-M/gudhi-data/main/spiral_2d.csv", "spiral_2d.csv") - assert 'remote_datasets/sphere3D_pts_on_grid.off' == remote.fetch("https://raw.githubusercontent.com/Hind-M/gudhi-data/main/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off") + path_file_dw = remote.fetch("https://raw.githubusercontent.com/Hind-M/gudhi-data/main/spiral_2d.csv", "spiral_2d.csv") + names_dw = re.split(r' |/|\\', path_file_dw) + assert 'remote_datasets' == names_dw[0] + assert 'spiral_2d.csv' == names_dw[1] + + path_file_dw = remote.fetch("https://raw.githubusercontent.com/Hind-M/gudhi-data/main/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off") + names_dw = re.split(r' |/|\\', path_file_dw) + assert 'remote_datasets' == names_dw[0] + assert 'sphere3D_pts_on_grid.off' == names_dw[1] + # Test files download with checksums provided - assert 'remote_datasets/spiral_2d.csv' == remote.fetch("https://raw.githubusercontent.com/Hind-M/gudhi-data/main/spiral_2d.csv", "spiral_2d.csv", checksum_flag = True, + path_file_dw = remote.fetch("https://raw.githubusercontent.com/Hind-M/gudhi-data/main/spiral_2d.csv", "spiral_2d.csv", checksum_flag = True, file_checksum = '37530355d980d957c4ec06b18c775f90a91e446107d06c6201c9b4000b077f38') - assert 'remote_datasets/sphere3D_pts_on_grid.off' == remote.fetch("https://raw.githubusercontent.com/Hind-M/gudhi-data/main/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off", + names_dw = re.split(r' |/|\\', path_file_dw) + assert 'remote_datasets' == names_dw[0] + assert 'spiral_2d.csv' == names_dw[1] + + path_file_dw = remote.fetch("https://raw.githubusercontent.com/Hind-M/gudhi-data/main/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off", checksum_flag = True, file_checksum = '32f96d2cafb1177f0dd5e0a019b6ff5658e14a619a7815ae55ad0fc5e8bd3f88') + names_dw = re.split(r' |/|\\', path_file_dw) + assert 'remote_datasets' == names_dw[0] + assert 'sphere3D_pts_on_grid.off' == names_dw[1] # Test spiral_2d.csv wrapping function - assert 'remote_datasets/spiral_2d.csv' == remote.fetch_spiral_2d() + path_file_dw = remote.fetch_spiral_2d() + names_dw = re.split(r' |/|\\', path_file_dw) + assert 'remote_datasets' == names_dw[0] + assert 'spiral_2d.csv' == names_dw[1] -- cgit v1.2.3 From 3ee453718eebc7274b19caef4b79d8ec2754d583 Mon Sep 17 00:00:00 2001 From: Hind-M Date: Thu, 3 Jun 2021 16:40:59 +0200 Subject: Modify urls to point to GUDHI/gudhi-data repo --- src/python/gudhi/datasets/remote.py | 3 ++- src/python/test/test_remote_datasets.py | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'src/python/test') diff --git a/src/python/gudhi/datasets/remote.py b/src/python/gudhi/datasets/remote.py index 4a300b15..525a7b66 100644 --- a/src/python/gudhi/datasets/remote.py +++ b/src/python/gudhi/datasets/remote.py @@ -100,4 +100,5 @@ def fetch_spiral_2d(filename = "spiral_2d.csv", dirname = "remote_datasets"): file_path: string Full path of the created file. """ - return fetch("https://raw.githubusercontent.com/Hind-M/gudhi-data/main/spiral_2d.csv", filename, dirname, True, '37530355d980d957c4ec06b18c775f90a91e446107d06c6201c9b4000b077f38') + return fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", filename, dirname, True, + '37530355d980d957c4ec06b18c775f90a91e446107d06c6201c9b4000b077f38') diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index a822ebaa..63ad7885 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -13,25 +13,25 @@ import re def test_fetch_remote_datasets(): # Test files download from given urls - path_file_dw = remote.fetch("https://raw.githubusercontent.com/Hind-M/gudhi-data/main/spiral_2d.csv", "spiral_2d.csv") + path_file_dw = remote.fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv") names_dw = re.split(r' |/|\\', path_file_dw) assert 'remote_datasets' == names_dw[0] assert 'spiral_2d.csv' == names_dw[1] - path_file_dw = remote.fetch("https://raw.githubusercontent.com/Hind-M/gudhi-data/main/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off") + path_file_dw = remote.fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off") names_dw = re.split(r' |/|\\', path_file_dw) assert 'remote_datasets' == names_dw[0] assert 'sphere3D_pts_on_grid.off' == names_dw[1] # Test files download with checksums provided - path_file_dw = remote.fetch("https://raw.githubusercontent.com/Hind-M/gudhi-data/main/spiral_2d.csv", "spiral_2d.csv", checksum_flag = True, + path_file_dw = remote.fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv", checksum_flag = True, file_checksum = '37530355d980d957c4ec06b18c775f90a91e446107d06c6201c9b4000b077f38') names_dw = re.split(r' |/|\\', path_file_dw) assert 'remote_datasets' == names_dw[0] assert 'spiral_2d.csv' == names_dw[1] - path_file_dw = remote.fetch("https://raw.githubusercontent.com/Hind-M/gudhi-data/main/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off", + path_file_dw = remote.fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off", checksum_flag = True, file_checksum = '32f96d2cafb1177f0dd5e0a019b6ff5658e14a619a7815ae55ad0fc5e8bd3f88') names_dw = re.split(r' |/|\\', path_file_dw) assert 'remote_datasets' == names_dw[0] -- cgit v1.2.3 From bbe2e25a204be50eb422db71b4cf314b92797d4e Mon Sep 17 00:00:00 2001 From: Hind-M Date: Fri, 4 Jun 2021 12:21:11 +0200 Subject: Remove checksum_flag parameter and use value of 'file_checksum is not None' instead --- src/python/gudhi/datasets/remote.py | 13 +++---------- src/python/test/test_remote_datasets.py | 7 ++++--- 2 files changed, 7 insertions(+), 13 deletions(-) (limited to 'src/python/test') diff --git a/src/python/gudhi/datasets/remote.py b/src/python/gudhi/datasets/remote.py index 525a7b66..fdd20f74 100644 --- a/src/python/gudhi/datasets/remote.py +++ b/src/python/gudhi/datasets/remote.py @@ -39,7 +39,7 @@ def _checksum_sha256(file_path): sha256_hash.update(buffer) return sha256_hash.hexdigest() -def fetch(url, filename, dirname = "remote_datasets", checksum_flag = False, file_checksum = None): +def fetch(url, filename, dirname = "remote_datasets", file_checksum = None): """ Fetch the wanted dataset from the given url and save it in file_path @@ -51,12 +51,8 @@ def fetch(url, filename, dirname = "remote_datasets", checksum_flag = False, fil The name to give to downloaded file. dirname : string The directory to save the file to. Default is "remote_datasets". - checksum_flag : boolean - To set if the user wants the file checksum. Default is 'False'. - Note that if checksum_flag is set to 'True', the file_checksum must be provided. file_checksum : string The file checksum using sha256 to check against the one computed on the downloaded file. - To be considered, checksum_flag must be set to 'True'. Default is 'None'. Returns @@ -71,10 +67,7 @@ def fetch(url, filename, dirname = "remote_datasets", checksum_flag = False, fil urlretrieve(url, file_path) - if (checksum_flag): - if file_checksum is None: - raise ValueError("The file checksum must be provided - different from None - for the check to be performed.") - + if file_checksum is not None: checksum = _checksum_sha256(file_path) if file_checksum != checksum: raise IOError("{} has a SHA256 checksum : {}, " @@ -100,5 +93,5 @@ def fetch_spiral_2d(filename = "spiral_2d.csv", dirname = "remote_datasets"): file_path: string Full path of the created file. """ - return fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", filename, dirname, True, + return fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", filename, dirname, '37530355d980d957c4ec06b18c775f90a91e446107d06c6201c9b4000b077f38') diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index 63ad7885..6c9217c8 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -25,14 +25,15 @@ def test_fetch_remote_datasets(): # Test files download with checksums provided - path_file_dw = remote.fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv", checksum_flag = True, - file_checksum = '37530355d980d957c4ec06b18c775f90a91e446107d06c6201c9b4000b077f38') + path_file_dw = remote.fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv", + file_checksum = '37530355d980d957c4ec06b18c775f90a91e446107d06c6201c9b4000b077f38') names_dw = re.split(r' |/|\\', path_file_dw) assert 'remote_datasets' == names_dw[0] assert 'spiral_2d.csv' == names_dw[1] path_file_dw = remote.fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off", - checksum_flag = True, file_checksum = '32f96d2cafb1177f0dd5e0a019b6ff5658e14a619a7815ae55ad0fc5e8bd3f88') + file_checksum = '32f96d2cafb1177f0dd5e0a019b6ff5658e14a619a7815ae55ad0fc5e8bd3f88') + names_dw = re.split(r' |/|\\', path_file_dw) assert 'remote_datasets' == names_dw[0] assert 'sphere3D_pts_on_grid.off' == names_dw[1] -- cgit v1.2.3 From 82524c5b0a6ab02b020574b2200a8721f3ed424c Mon Sep 17 00:00:00 2001 From: Hind-M Date: Mon, 7 Jun 2021 15:03:14 +0200 Subject: Add test with wrong checksum Add functions to avoid redundant code --- src/python/test/test_remote_datasets.py | 43 +++++++++++++++++---------------- 1 file changed, 22 insertions(+), 21 deletions(-) (limited to 'src/python/test') diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index 6c9217c8..e252980d 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -10,36 +10,37 @@ from gudhi.datasets import remote import re +import os.path +import pytest -def test_fetch_remote_datasets(): - # Test files download from given urls - path_file_dw = remote.fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv") - names_dw = re.split(r' |/|\\', path_file_dw) - assert 'remote_datasets' == names_dw[0] - assert 'spiral_2d.csv' == names_dw[1] +def check_dir_file_names(path_file_dw, filename, dirname): + assert os.path.isfile(path_file_dw) - path_file_dw = remote.fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off") names_dw = re.split(r' |/|\\', path_file_dw) - assert 'remote_datasets' == names_dw[0] - assert 'sphere3D_pts_on_grid.off' == names_dw[1] + assert dirname == names_dw[0] + assert filename == names_dw[1] +def check_fetch_output(url, filename, dirname = "remote_datasets", file_checksum = None): + path_file_dw = remote.fetch(url, filename, dirname, file_checksum) + check_dir_file_names(path_file_dw, filename, dirname) + +def test_fetch_remote_datasets(): + # Test fetch with a wrong checksum + with pytest.raises(OSError): + check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv", file_checksum = 'XXXXXXXXXX') - # Test files download with checksums provided - path_file_dw = remote.fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv", + # Test files download from given urls with checksums provided + check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv", file_checksum = '37530355d980d957c4ec06b18c775f90a91e446107d06c6201c9b4000b077f38') - names_dw = re.split(r' |/|\\', path_file_dw) - assert 'remote_datasets' == names_dw[0] - assert 'spiral_2d.csv' == names_dw[1] - path_file_dw = remote.fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off", + check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off", file_checksum = '32f96d2cafb1177f0dd5e0a019b6ff5658e14a619a7815ae55ad0fc5e8bd3f88') - names_dw = re.split(r' |/|\\', path_file_dw) - assert 'remote_datasets' == names_dw[0] - assert 'sphere3D_pts_on_grid.off' == names_dw[1] + # Test files download from given urls without checksums + check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv") + + check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off") # Test spiral_2d.csv wrapping function path_file_dw = remote.fetch_spiral_2d() - names_dw = re.split(r' |/|\\', path_file_dw) - assert 'remote_datasets' == names_dw[0] - assert 'spiral_2d.csv' == names_dw[1] + check_dir_file_names(path_file_dw, 'spiral_2d.csv', 'remote_datasets') -- cgit v1.2.3 From aa600c433e1f756bec4323e29e86786b937d9443 Mon Sep 17 00:00:00 2001 From: Hind-M Date: Mon, 15 Nov 2021 11:12:27 +0100 Subject: Print files licenses when available Wrap bunny fetching Add corresponding tests --- src/python/gudhi/datasets/remote.py | 38 ++++++++++++++++++++++- src/python/test/test_remote_datasets.py | 55 ++++++++++++++++++++++++++------- 2 files changed, 81 insertions(+), 12 deletions(-) (limited to 'src/python/test') diff --git a/src/python/gudhi/datasets/remote.py b/src/python/gudhi/datasets/remote.py index aef4b277..7e8f9ce7 100644 --- a/src/python/gudhi/datasets/remote.py +++ b/src/python/gudhi/datasets/remote.py @@ -39,7 +39,7 @@ def _checksum_sha256(file_path): sha256_hash.update(buffer) return sha256_hash.hexdigest() -def fetch(url, filename, dirname = "remote_datasets", file_checksum = None): +def fetch(url, filename, dirname = "remote_datasets", file_checksum = None, accept_license = False): """ Fetch the wanted dataset from the given url and save it in file_path @@ -54,6 +54,9 @@ def fetch(url, filename, dirname = "remote_datasets", file_checksum = None): file_checksum : string The file checksum using sha256 to check against the one computed on the downloaded file. Default is 'None'. + accept_license : boolean + Flag to specify if user accepts the file LICENSE and prevents from printing the corresponding license terms. + Default is False Returns ------- @@ -69,6 +72,7 @@ def fetch(url, filename, dirname = "remote_datasets", file_checksum = None): if not exists(dirname): makedirs(dirname) + # Get the file urlretrieve(url, file_path) if file_checksum is not None: @@ -78,6 +82,13 @@ def fetch(url, filename, dirname = "remote_datasets", file_checksum = None): "different from expected : {}." "The file may be corrupted or the given url may be wrong !".format(file_path, checksum, file_checksum)) + # Print license terms unless accept_license is set to True + if not accept_license: + license_file = join(dirname, "LICENSE") + if exists(license_file) and (file_path != license_file): + with open(license_file, 'r') as f: + print(f.read()) + return file_path def fetch_spiral_2d(filename = "spiral_2d.csv", dirname = "remote_datasets"): @@ -98,3 +109,28 @@ def fetch_spiral_2d(filename = "spiral_2d.csv", dirname = "remote_datasets"): """ return fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", filename, dirname, '37530355d980d957c4ec06b18c775f90a91e446107d06c6201c9b4000b077f38') + +def fetch_bunny(filename = "bunny.off", dirname = "remote_datasets/bunny", accept_license = False): + """ + Fetch bunny.off remotely and its LICENSE file + + Parameters + ---------- + filename : string + The name to give to downloaded file. Default is "bunny.off" + dirname : string + The directory to save the file to. Default is "remote_datasets/bunny". + accept_license : boolean + Flag to specify if user accepts the file LICENSE and prevents from printing the corresponding license terms. + Default is False + + Returns + ------- + files_paths: list of strings + Full paths of the created file and its LICENSE. + """ + + return [fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points//bunny/LICENSE", "LICENSE", dirname, + 'aeb1bad319b7d74fa0b8076358182f9c6b1284c67cc07dc67cbc9bc73025d956'), + fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points//bunny/bunny.off", filename, dirname, + '11852d5e73e2d4bd7b86a2c5cc8a5884d0fbb72539493e8cec100ea922b19f5b', accept_license)] diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index e252980d..e777abc6 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -11,36 +11,69 @@ from gudhi.datasets import remote import re import os.path +import io +import sys import pytest -def check_dir_file_names(path_file_dw, filename, dirname): +def _check_dir_file_names(path_file_dw, filename, dirname): assert os.path.isfile(path_file_dw) names_dw = re.split(r' |/|\\', path_file_dw) - assert dirname == names_dw[0] - assert filename == names_dw[1] + # Case where inner directories are created in "remote_datasets/"; e.g: "remote_datasets/bunny" + if len(names_dw) >= 3: + for i in range(len(names_dw)-1): + assert re.split(r' |/|\\', dirname)[i] == names_dw[i] + assert filename == names_dw[i+1] + else: + assert dirname == names_dw[0] + assert filename == names_dw[1] -def check_fetch_output(url, filename, dirname = "remote_datasets", file_checksum = None): +def _check_fetch_output(url, filename, dirname = "remote_datasets", file_checksum = None): path_file_dw = remote.fetch(url, filename, dirname, file_checksum) - check_dir_file_names(path_file_dw, filename, dirname) + _check_dir_file_names(path_file_dw, filename, dirname) + +def _get_bunny_license_print(accept_license = False): + capturedOutput = io.StringIO() + # Redirect stdout + sys.stdout = capturedOutput + remote.fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points//bunny/bunny.off", "bunny.off", "remote_datasets/bunny", + '11852d5e73e2d4bd7b86a2c5cc8a5884d0fbb72539493e8cec100ea922b19f5b', accept_license) + # Reset redirect + sys.stdout = sys.__stdout__ + return capturedOutput def test_fetch_remote_datasets(): # Test fetch with a wrong checksum with pytest.raises(OSError): - check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv", file_checksum = 'XXXXXXXXXX') + _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv", file_checksum = 'XXXXXXXXXX') # Test files download from given urls with checksums provided - check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv", + _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv", file_checksum = '37530355d980d957c4ec06b18c775f90a91e446107d06c6201c9b4000b077f38') - check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off", + _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off", file_checksum = '32f96d2cafb1177f0dd5e0a019b6ff5658e14a619a7815ae55ad0fc5e8bd3f88') # Test files download from given urls without checksums - check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv") + _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv") - check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off") + _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off") # Test spiral_2d.csv wrapping function path_file_dw = remote.fetch_spiral_2d() - check_dir_file_names(path_file_dw, 'spiral_2d.csv', 'remote_datasets') + _check_dir_file_names(path_file_dw, 'spiral_2d.csv', 'remote_datasets') + + # Test printing existing LICENSE file when fetching bunny.off with accept_license = False (default) + # Fetch LICENSE file + remote.fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points//bunny/LICENSE", "LICENSE", "remote_datasets/bunny", + 'aeb1bad319b7d74fa0b8076358182f9c6b1284c67cc07dc67cbc9bc73025d956') + with open("remote_datasets/bunny/LICENSE") as f: + assert f.read() == _get_bunny_license_print().getvalue().rstrip("\n") + + # Test not printing bunny.off LICENSE when accept_license = True + assert "" == _get_bunny_license_print(accept_license = True).getvalue() + + # Test fetch_bunny wrapping function + path_file_dw = remote.fetch_bunny() + _check_dir_file_names(path_file_dw[0], 'LICENSE', 'remote_datasets/bunny') + _check_dir_file_names(path_file_dw[1], 'bunny.off', 'remote_datasets/bunny') -- cgit v1.2.3 From 8d1e7aeb3416194d00f45587d1ecea85ba218028 Mon Sep 17 00:00:00 2001 From: Hind-M Date: Fri, 28 Jan 2022 16:21:33 +0100 Subject: Return arrays of points instead of files paths when fetching bunny.npy and spiral_2d.csv --- src/python/gudhi/datasets/remote.py | 83 +++++++++++++++++++++------------ src/python/test/test_remote_datasets.py | 33 +++++++------ 2 files changed, 72 insertions(+), 44 deletions(-) (limited to 'src/python/test') diff --git a/src/python/gudhi/datasets/remote.py b/src/python/gudhi/datasets/remote.py index 7e8f9ce7..ef797417 100644 --- a/src/python/gudhi/datasets/remote.py +++ b/src/python/gudhi/datasets/remote.py @@ -7,17 +7,17 @@ # Modification(s): # - YYYY/MM Author: Description of the modification -import hashlib - from os.path import join, exists from os import makedirs from urllib.request import urlretrieve +import hashlib +import numpy as np def _checksum_sha256(file_path): """ - Compute the file checksum using sha256 + Compute the file checksum using sha256. Parameters ---------- @@ -26,7 +26,7 @@ def _checksum_sha256(file_path): Returns ------- - The hex digest of file_path + The hex digest of file_path. """ sha256_hash = hashlib.sha256() chunk_size = 4096 @@ -39,9 +39,9 @@ def _checksum_sha256(file_path): sha256_hash.update(buffer) return sha256_hash.hexdigest() -def fetch(url, filename, dirname = "remote_datasets", file_checksum = None, accept_license = False): +def _fetch_remote(url, filename, dirname = "remote_datasets", file_checksum = None, accept_license = False): """ - Fetch the wanted dataset from the given url and save it in file_path + Fetch the wanted dataset from the given url and save it in file_path. Parameters ---------- @@ -56,7 +56,7 @@ def fetch(url, filename, dirname = "remote_datasets", file_checksum = None, acce Default is 'None'. accept_license : boolean Flag to specify if user accepts the file LICENSE and prevents from printing the corresponding license terms. - Default is False + Default is False. Returns ------- @@ -66,14 +66,8 @@ def fetch(url, filename, dirname = "remote_datasets", file_checksum = None, acce file_path = join(dirname, filename) - # Check for an already existing file at file_path - if not exists(file_path): - # Create directory if not existing - if not exists(dirname): - makedirs(dirname) - - # Get the file - urlretrieve(url, file_path) + # Get the file + urlretrieve(url, file_path) if file_checksum is not None: checksum = _checksum_sha256(file_path) @@ -93,44 +87,71 @@ def fetch(url, filename, dirname = "remote_datasets", file_checksum = None, acce def fetch_spiral_2d(filename = "spiral_2d.csv", dirname = "remote_datasets"): """ - Fetch spiral_2d.csv remotely + Fetch "spiral_2d.csv" remotely. Parameters ---------- filename : string - The name to give to downloaded file. Default is "spiral_2d.csv" + The name to give to downloaded file. Default is "spiral_2d.csv". dirname : string The directory to save the file to. Default is "remote_datasets". Returns ------- - file_path: string - Full path of the created file. + points: array + Array of points stored in "spiral_2d.csv". """ - return fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", filename, dirname, - '37530355d980d957c4ec06b18c775f90a91e446107d06c6201c9b4000b077f38') + file_url = "https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv" + file_checksum = '37530355d980d957c4ec06b18c775f90a91e446107d06c6201c9b4000b077f38' -def fetch_bunny(filename = "bunny.off", dirname = "remote_datasets/bunny", accept_license = False): + archive_path = join(dirname, filename) + + if not exists(archive_path): + # Create directory if not existing + if not exists(dirname): + makedirs(dirname) + + file_path_pkl = _fetch_remote(file_url, filename, dirname, file_checksum) + + return np.loadtxt(file_path_pkl) + else: + return np.loadtxt(archive_path) + +def fetch_bunny(filename = "bunny.npy", dirname = "remote_datasets/bunny", accept_license = False): """ - Fetch bunny.off remotely and its LICENSE file + Fetch "bunny.npy" remotely and its LICENSE file. Parameters ---------- filename : string - The name to give to downloaded file. Default is "bunny.off" + The name to give to downloaded file. Default is "bunny.npy". dirname : string The directory to save the file to. Default is "remote_datasets/bunny". accept_license : boolean Flag to specify if user accepts the file LICENSE and prevents from printing the corresponding license terms. - Default is False + Default is False. Returns ------- - files_paths: list of strings - Full paths of the created file and its LICENSE. + points: array + Array of points stored in "bunny.npy". """ - return [fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points//bunny/LICENSE", "LICENSE", dirname, - 'aeb1bad319b7d74fa0b8076358182f9c6b1284c67cc07dc67cbc9bc73025d956'), - fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points//bunny/bunny.off", filename, dirname, - '11852d5e73e2d4bd7b86a2c5cc8a5884d0fbb72539493e8cec100ea922b19f5b', accept_license)] + file_url = "https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/bunny.npy" + file_checksum = '13f7842ebb4b45370e50641ff28c88685703efa5faab14edf0bb7d113a965e1b' + license_url = "https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/LICENSE" + license_checksum = 'aeb1bad319b7d74fa0b8076358182f9c6b1284c67cc07dc67cbc9bc73025d956' + + archive_path = join(dirname, filename) + + if not exists(archive_path): + # Create directory if not existing + if not exists(dirname): + makedirs(dirname) + + license_path = _fetch_remote(license_url, "LICENSE", dirname, license_checksum) + file_path_pkl = _fetch_remote(file_url, filename, dirname, file_checksum, accept_license) + + return np.load(file_path_pkl, mmap_mode='r') + else: + return np.load(archive_path, mmap_mode='r') diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index e777abc6..56a273b4 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -10,13 +10,14 @@ from gudhi.datasets import remote import re -import os.path +from os.path import isfile, exists +from os import makedirs import io import sys import pytest def _check_dir_file_names(path_file_dw, filename, dirname): - assert os.path.isfile(path_file_dw) + assert isfile(path_file_dw) names_dw = re.split(r' |/|\\', path_file_dw) # Case where inner directories are created in "remote_datasets/"; e.g: "remote_datasets/bunny" @@ -29,15 +30,20 @@ def _check_dir_file_names(path_file_dw, filename, dirname): assert filename == names_dw[1] def _check_fetch_output(url, filename, dirname = "remote_datasets", file_checksum = None): - path_file_dw = remote.fetch(url, filename, dirname, file_checksum) + if not exists(dirname): + makedirs(dirname) + path_file_dw = remote._fetch_remote(url, filename, dirname, file_checksum) _check_dir_file_names(path_file_dw, filename, dirname) def _get_bunny_license_print(accept_license = False): capturedOutput = io.StringIO() # Redirect stdout sys.stdout = capturedOutput - remote.fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points//bunny/bunny.off", "bunny.off", "remote_datasets/bunny", - '11852d5e73e2d4bd7b86a2c5cc8a5884d0fbb72539493e8cec100ea922b19f5b', accept_license) + + if not exists("remote_datasets/bunny"): + makedirs("remote_datasets/bunny") + remote._fetch_remote("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/bunny.npy", "bunny.npy", "remote_datasets/bunny", + '13f7842ebb4b45370e50641ff28c88685703efa5faab14edf0bb7d113a965e1b', accept_license) # Reset redirect sys.stdout = sys.__stdout__ return capturedOutput @@ -60,20 +66,21 @@ def test_fetch_remote_datasets(): _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off") # Test spiral_2d.csv wrapping function - path_file_dw = remote.fetch_spiral_2d() - _check_dir_file_names(path_file_dw, 'spiral_2d.csv', 'remote_datasets') + spiral_2d_arr = remote.fetch_spiral_2d() + assert spiral_2d_arr.shape == (114562, 2) - # Test printing existing LICENSE file when fetching bunny.off with accept_license = False (default) + # Test printing existing LICENSE file when fetching bunny.npy with accept_license = False (default) # Fetch LICENSE file - remote.fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points//bunny/LICENSE", "LICENSE", "remote_datasets/bunny", + if not exists("remote_datasets/bunny"): + makedirs("remote_datasets/bunny") + remote._fetch_remote("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/LICENSE", "LICENSE", "remote_datasets/bunny", 'aeb1bad319b7d74fa0b8076358182f9c6b1284c67cc07dc67cbc9bc73025d956') with open("remote_datasets/bunny/LICENSE") as f: assert f.read() == _get_bunny_license_print().getvalue().rstrip("\n") - # Test not printing bunny.off LICENSE when accept_license = True + # Test not printing bunny.npy LICENSE when accept_license = True assert "" == _get_bunny_license_print(accept_license = True).getvalue() # Test fetch_bunny wrapping function - path_file_dw = remote.fetch_bunny() - _check_dir_file_names(path_file_dw[0], 'LICENSE', 'remote_datasets/bunny') - _check_dir_file_names(path_file_dw[1], 'bunny.off', 'remote_datasets/bunny') + bunny_arr = remote.fetch_bunny() + assert bunny_arr.shape == (35947, 3) -- cgit v1.2.3 From ad7a50fb87ed4237b9a02165eac39ae355dd5440 Mon Sep 17 00:00:00 2001 From: Hind-M Date: Tue, 1 Feb 2022 10:32:03 +0100 Subject: Fetch spiral_2d.npy file instead of csv Add some modifications related to those done on files in gudhi-data --- src/python/gudhi/datasets/remote.py | 20 ++++++++++---------- src/python/test/test_remote_datasets.py | 14 +++++++------- 2 files changed, 17 insertions(+), 17 deletions(-) (limited to 'src/python/test') diff --git a/src/python/gudhi/datasets/remote.py b/src/python/gudhi/datasets/remote.py index ef797417..3498a645 100644 --- a/src/python/gudhi/datasets/remote.py +++ b/src/python/gudhi/datasets/remote.py @@ -85,24 +85,24 @@ def _fetch_remote(url, filename, dirname = "remote_datasets", file_checksum = No return file_path -def fetch_spiral_2d(filename = "spiral_2d.csv", dirname = "remote_datasets"): +def fetch_spiral_2d(filename = "spiral_2d.npy", dirname = "remote_datasets/spiral_2d"): """ - Fetch "spiral_2d.csv" remotely. + Fetch "spiral_2d.npy" remotely. Parameters ---------- filename : string - The name to give to downloaded file. Default is "spiral_2d.csv". + The name to give to downloaded file. Default is "spiral_2d.npy". dirname : string - The directory to save the file to. Default is "remote_datasets". + The directory to save the file to. Default is "remote_datasets/spiral_2d". Returns ------- points: array - Array of points stored in "spiral_2d.csv". + Array of points stored in "spiral_2d.npy". """ - file_url = "https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv" - file_checksum = '37530355d980d957c4ec06b18c775f90a91e446107d06c6201c9b4000b077f38' + file_url = "https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d/spiral_2d.npy" + file_checksum = '88312ffd6df2e2cb2bde9c0e1f962d7d644c6f58dc369c7b377b298dacdc4eaf' archive_path = join(dirname, filename) @@ -113,9 +113,9 @@ def fetch_spiral_2d(filename = "spiral_2d.csv", dirname = "remote_datasets"): file_path_pkl = _fetch_remote(file_url, filename, dirname, file_checksum) - return np.loadtxt(file_path_pkl) + return np.load(file_path_pkl, mmap_mode='r') else: - return np.loadtxt(archive_path) + return np.load(archive_path, mmap_mode='r') def fetch_bunny(filename = "bunny.npy", dirname = "remote_datasets/bunny", accept_license = False): """ @@ -140,7 +140,7 @@ def fetch_bunny(filename = "bunny.npy", dirname = "remote_datasets/bunny", accep file_url = "https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/bunny.npy" file_checksum = '13f7842ebb4b45370e50641ff28c88685703efa5faab14edf0bb7d113a965e1b' license_url = "https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/LICENSE" - license_checksum = 'aeb1bad319b7d74fa0b8076358182f9c6b1284c67cc07dc67cbc9bc73025d956' + license_checksum = 'b763dbe1b2fc6015d05cbf7bcc686412a2eb100a1f2220296e3b4a644c69633a' archive_path = join(dirname, filename) diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index 56a273b4..2057c63b 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -51,21 +51,21 @@ def _get_bunny_license_print(accept_license = False): def test_fetch_remote_datasets(): # Test fetch with a wrong checksum with pytest.raises(OSError): - _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv", file_checksum = 'XXXXXXXXXX') + _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d/spiral_2d.npy", "spiral_2d.npy", file_checksum = 'XXXXXXXXXX') # Test files download from given urls with checksums provided - _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv", - file_checksum = '37530355d980d957c4ec06b18c775f90a91e446107d06c6201c9b4000b077f38') + _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d/spiral_2d.npy", "spiral_2d.npy", + file_checksum = '88312ffd6df2e2cb2bde9c0e1f962d7d644c6f58dc369c7b377b298dacdc4eaf') _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off", file_checksum = '32f96d2cafb1177f0dd5e0a019b6ff5658e14a619a7815ae55ad0fc5e8bd3f88') # Test files download from given urls without checksums - _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv") + _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d/spiral_2d.npy", "spiral_2d.npy") _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off") - # Test spiral_2d.csv wrapping function + # Test fetch_spiral_2d wrapping function spiral_2d_arr = remote.fetch_spiral_2d() assert spiral_2d_arr.shape == (114562, 2) @@ -74,9 +74,9 @@ def test_fetch_remote_datasets(): if not exists("remote_datasets/bunny"): makedirs("remote_datasets/bunny") remote._fetch_remote("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/LICENSE", "LICENSE", "remote_datasets/bunny", - 'aeb1bad319b7d74fa0b8076358182f9c6b1284c67cc07dc67cbc9bc73025d956') + 'b763dbe1b2fc6015d05cbf7bcc686412a2eb100a1f2220296e3b4a644c69633a') with open("remote_datasets/bunny/LICENSE") as f: - assert f.read() == _get_bunny_license_print().getvalue().rstrip("\n") + assert f.read().rstrip("\n") == _get_bunny_license_print().getvalue().rstrip("\n") # Test not printing bunny.npy LICENSE when accept_license = True assert "" == _get_bunny_license_print(accept_license = True).getvalue() -- cgit v1.2.3 From 741f4f182479d1e5e78e9eb9180adce0a72e99b6 Mon Sep 17 00:00:00 2001 From: Hind-M Date: Wed, 2 Feb 2022 10:38:15 +0100 Subject: Modify remote fetching test to increase its coverage --- src/python/test/test_remote_datasets.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) (limited to 'src/python/test') diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index 2057c63b..dac9ee80 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -40,8 +40,6 @@ def _get_bunny_license_print(accept_license = False): # Redirect stdout sys.stdout = capturedOutput - if not exists("remote_datasets/bunny"): - makedirs("remote_datasets/bunny") remote._fetch_remote("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/bunny.npy", "bunny.npy", "remote_datasets/bunny", '13f7842ebb4b45370e50641ff28c88685703efa5faab14edf0bb7d113a965e1b', accept_license) # Reset redirect @@ -65,22 +63,17 @@ def test_fetch_remote_datasets(): _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off") - # Test fetch_spiral_2d wrapping function - spiral_2d_arr = remote.fetch_spiral_2d() - assert spiral_2d_arr.shape == (114562, 2) + # Test fetch_spiral_2d and fetch_bunny wrapping functions (twice, to test case of already fetched files) + for i in range(2): + spiral_2d_arr = remote.fetch_spiral_2d() + assert spiral_2d_arr.shape == (114562, 2) + + bunny_arr = remote.fetch_bunny() + assert bunny_arr.shape == (35947, 3) # Test printing existing LICENSE file when fetching bunny.npy with accept_license = False (default) - # Fetch LICENSE file - if not exists("remote_datasets/bunny"): - makedirs("remote_datasets/bunny") - remote._fetch_remote("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/LICENSE", "LICENSE", "remote_datasets/bunny", - 'b763dbe1b2fc6015d05cbf7bcc686412a2eb100a1f2220296e3b4a644c69633a') with open("remote_datasets/bunny/LICENSE") as f: assert f.read().rstrip("\n") == _get_bunny_license_print().getvalue().rstrip("\n") # Test not printing bunny.npy LICENSE when accept_license = True assert "" == _get_bunny_license_print(accept_license = True).getvalue() - - # Test fetch_bunny wrapping function - bunny_arr = remote.fetch_bunny() - assert bunny_arr.shape == (35947, 3) -- cgit v1.2.3 From 19689c712a1f5945e664f9c74c14b6994e7afaaf Mon Sep 17 00:00:00 2001 From: Hind-M Date: Wed, 2 Feb 2022 16:14:17 +0100 Subject: Try to fix failing test in windows --- src/python/test/test_remote_datasets.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/python/test') diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index dac9ee80..643485f9 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -40,6 +40,9 @@ def _get_bunny_license_print(accept_license = False): # Redirect stdout sys.stdout = capturedOutput + if not exists("remote_datasets/bunny"): + makedirs("remote_datasets/bunny") + remote._fetch_remote("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/bunny.npy", "bunny.npy", "remote_datasets/bunny", '13f7842ebb4b45370e50641ff28c88685703efa5faab14edf0bb7d113a965e1b', accept_license) # Reset redirect @@ -72,6 +75,11 @@ def test_fetch_remote_datasets(): assert bunny_arr.shape == (35947, 3) # Test printing existing LICENSE file when fetching bunny.npy with accept_license = False (default) + # Fetch LICENSE file + if not exists("remote_datasets/bunny"): + makedirs("remote_datasets/bunny") + remote._fetch_remote("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/LICENSE", "LICENSE", "remote_datasets/bunny", + 'b763dbe1b2fc6015d05cbf7bcc686412a2eb100a1f2220296e3b4a644c69633a') with open("remote_datasets/bunny/LICENSE") as f: assert f.read().rstrip("\n") == _get_bunny_license_print().getvalue().rstrip("\n") -- cgit v1.2.3 From a2d55f9bbf0f45e3ae4c147f734ce04f5bc87ab8 Mon Sep 17 00:00:00 2001 From: Hind-M Date: Wed, 2 Feb 2022 21:32:55 +0100 Subject: Another attempt to fix windows failing test: move fetch_bunny to the end --- src/python/test/test_remote_datasets.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src/python/test') diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index 643485f9..5e607d73 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -66,14 +66,6 @@ def test_fetch_remote_datasets(): _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off") - # Test fetch_spiral_2d and fetch_bunny wrapping functions (twice, to test case of already fetched files) - for i in range(2): - spiral_2d_arr = remote.fetch_spiral_2d() - assert spiral_2d_arr.shape == (114562, 2) - - bunny_arr = remote.fetch_bunny() - assert bunny_arr.shape == (35947, 3) - # Test printing existing LICENSE file when fetching bunny.npy with accept_license = False (default) # Fetch LICENSE file if not exists("remote_datasets/bunny"): @@ -85,3 +77,11 @@ def test_fetch_remote_datasets(): # Test not printing bunny.npy LICENSE when accept_license = True assert "" == _get_bunny_license_print(accept_license = True).getvalue() + + # Test fetch_spiral_2d and fetch_bunny wrapping functions (twice, to test case of already fetched files) + for i in range(2): + spiral_2d_arr = remote.fetch_spiral_2d() + assert spiral_2d_arr.shape == (114562, 2) + + bunny_arr = remote.fetch_bunny() + assert bunny_arr.shape == (35947, 3) -- cgit v1.2.3 From 6109fd920ba477f89e83fea3df9803232c169463 Mon Sep 17 00:00:00 2001 From: Hind-M Date: Thu, 3 Feb 2022 10:24:38 +0100 Subject: Remove archive folder before testing wrapping functions --- src/python/test/test_remote_datasets.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/python/test') diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index 5e607d73..93a8a982 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -78,6 +78,9 @@ def test_fetch_remote_datasets(): # Test not printing bunny.npy LICENSE when accept_license = True assert "" == _get_bunny_license_print(accept_license = True).getvalue() + # Remove "remote_datasets" directory and all its content + import shutil + shutil.rmtree("remote_datasets") # Test fetch_spiral_2d and fetch_bunny wrapping functions (twice, to test case of already fetched files) for i in range(2): spiral_2d_arr = remote.fetch_spiral_2d() -- cgit v1.2.3 From a13282e4da9910a5d2bdadf97040095ae5b7880a Mon Sep 17 00:00:00 2001 From: Hind-M Date: Fri, 4 Feb 2022 15:39:51 +0100 Subject: Store fetched datasets in user directory by default --- src/python/gudhi/datasets/remote.py | 68 ++++++++++++++++++++++++++------- src/python/test/test_remote_datasets.py | 31 +++++++++++---- 2 files changed, 79 insertions(+), 20 deletions(-) (limited to 'src/python/test') diff --git a/src/python/gudhi/datasets/remote.py b/src/python/gudhi/datasets/remote.py index 3498a645..3d6c01b0 100644 --- a/src/python/gudhi/datasets/remote.py +++ b/src/python/gudhi/datasets/remote.py @@ -7,14 +7,52 @@ # Modification(s): # - YYYY/MM Author: Description of the modification -from os.path import join, exists +from os.path import join, exists, expanduser from os import makedirs from urllib.request import urlretrieve import hashlib +import shutil import numpy as np +def get_data_home(data_home = None): + """ + Return the path of the remote datasets directory. + This folder is used to store remotely fetched datasets. + By default the datasets directory is set to a folder named 'remote_datasets' in the user home folder. + Alternatively, it can be set by giving an explicit folder path. The '~' symbol is expanded to the user home folder. + If the folder does not already exist, it is automatically created. + + Parameters + ---------- + data_home : string + The path to remote datasets directory. Default is `None`, meaning that the data home directory will be set to "~/remote_datasets". + + Returns + ------- + data_home: string + The path to remote datasets directory. + """ + if data_home is None: + data_home = join("~", "remote_datasets") + data_home = expanduser(data_home) + makedirs(data_home, exist_ok=True) + return data_home + + +def clear_data_home(data_home = None): + """ + Delete all the content of the data home cache. + + Parameters + ---------- + data_home : string, default is None. + The path to remote datasets directory. If `None`, the default directory to be removed is set to "~/remote_datasets". + """ + data_home = get_data_home(data_home) + shutil.rmtree(data_home) + def _checksum_sha256(file_path): """ Compute the file checksum using sha256. @@ -85,7 +123,7 @@ def _fetch_remote(url, filename, dirname = "remote_datasets", file_checksum = No return file_path -def fetch_spiral_2d(filename = "spiral_2d.npy", dirname = "remote_datasets/spiral_2d"): +def fetch_spiral_2d(filename = "spiral_2d.npy", dirname = None): """ Fetch "spiral_2d.npy" remotely. @@ -94,7 +132,7 @@ def fetch_spiral_2d(filename = "spiral_2d.npy", dirname = "remote_datasets/spira filename : string The name to give to downloaded file. Default is "spiral_2d.npy". dirname : string - The directory to save the file to. Default is "remote_datasets/spiral_2d". + The directory to save the file to. Default is None, meaning that the data home will be set to "~/remote_datasets/spiral_2d". Returns ------- @@ -104,20 +142,22 @@ def fetch_spiral_2d(filename = "spiral_2d.npy", dirname = "remote_datasets/spira file_url = "https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d/spiral_2d.npy" file_checksum = '88312ffd6df2e2cb2bde9c0e1f962d7d644c6f58dc369c7b377b298dacdc4eaf' + if dirname is None: + dirname = join(get_data_home(dirname), "spiral_2d") + makedirs(dirname, exist_ok=True) + else: + dirname = get_data_home(dirname) + archive_path = join(dirname, filename) if not exists(archive_path): - # Create directory if not existing - if not exists(dirname): - makedirs(dirname) - file_path_pkl = _fetch_remote(file_url, filename, dirname, file_checksum) return np.load(file_path_pkl, mmap_mode='r') else: return np.load(archive_path, mmap_mode='r') -def fetch_bunny(filename = "bunny.npy", dirname = "remote_datasets/bunny", accept_license = False): +def fetch_bunny(filename = "bunny.npy", dirname = None, accept_license = False): """ Fetch "bunny.npy" remotely and its LICENSE file. @@ -126,7 +166,7 @@ def fetch_bunny(filename = "bunny.npy", dirname = "remote_datasets/bunny", accep filename : string The name to give to downloaded file. Default is "bunny.npy". dirname : string - The directory to save the file to. Default is "remote_datasets/bunny". + The directory to save the file to. Default is None, meaning that the data home will be set to "~/remote_datasets/bunny". accept_license : boolean Flag to specify if user accepts the file LICENSE and prevents from printing the corresponding license terms. Default is False. @@ -142,13 +182,15 @@ def fetch_bunny(filename = "bunny.npy", dirname = "remote_datasets/bunny", accep license_url = "https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/LICENSE" license_checksum = 'b763dbe1b2fc6015d05cbf7bcc686412a2eb100a1f2220296e3b4a644c69633a' + if dirname is None: + dirname = join(get_data_home(dirname), "bunny") + makedirs(dirname, exist_ok=True) + else: + dirname = get_data_home(dirname) + archive_path = join(dirname, filename) if not exists(archive_path): - # Create directory if not existing - if not exists(dirname): - makedirs(dirname) - license_path = _fetch_remote(license_url, "LICENSE", dirname, license_checksum) file_path_pkl = _fetch_remote(file_url, filename, dirname, file_checksum, accept_license) diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index 93a8a982..27eb51b0 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -10,7 +10,7 @@ from gudhi.datasets import remote import re -from os.path import isfile, exists +from os.path import isfile, isdir, expanduser from os import makedirs import io import sys @@ -30,8 +30,7 @@ def _check_dir_file_names(path_file_dw, filename, dirname): assert filename == names_dw[1] def _check_fetch_output(url, filename, dirname = "remote_datasets", file_checksum = None): - if not exists(dirname): - makedirs(dirname) + makedirs(dirname, exist_ok=True) path_file_dw = remote._fetch_remote(url, filename, dirname, file_checksum) _check_dir_file_names(path_file_dw, filename, dirname) @@ -40,8 +39,7 @@ def _get_bunny_license_print(accept_license = False): # Redirect stdout sys.stdout = capturedOutput - if not exists("remote_datasets/bunny"): - makedirs("remote_datasets/bunny") + makedirs("remote_datasets/bunny", exist_ok=True) remote._fetch_remote("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/bunny.npy", "bunny.npy", "remote_datasets/bunny", '13f7842ebb4b45370e50641ff28c88685703efa5faab14edf0bb7d113a965e1b', accept_license) @@ -68,8 +66,7 @@ def test_fetch_remote_datasets(): # Test printing existing LICENSE file when fetching bunny.npy with accept_license = False (default) # Fetch LICENSE file - if not exists("remote_datasets/bunny"): - makedirs("remote_datasets/bunny") + makedirs("remote_datasets/bunny", exist_ok=True) remote._fetch_remote("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/LICENSE", "LICENSE", "remote_datasets/bunny", 'b763dbe1b2fc6015d05cbf7bcc686412a2eb100a1f2220296e3b4a644c69633a') with open("remote_datasets/bunny/LICENSE") as f: @@ -88,3 +85,23 @@ def test_fetch_remote_datasets(): bunny_arr = remote.fetch_bunny() assert bunny_arr.shape == (35947, 3) + + # Check that default dir was created + assert isdir(expanduser("~/remote_datasets")) == True + + # Test clear_data_home + clear_data_home() + assert isdir(expanduser("~/remote_datasets")) == False + + # Test fetch_spiral_2d and fetch_bunny wrapping functions with data directory different from default + spiral_2d_arr = remote.fetch_spiral_2d(dirname = "~/test") + assert spiral_2d_arr.shape == (114562, 2) + + bunny_arr = remote.fetch_bunny(dirname = "~/test") + assert bunny_arr.shape == (35947, 3) + + assert isdir(expanduser("~/test")) == True + + # Test clear_data_home with data directory different from default + clear_data_home("~/test") + assert isdir(expanduser("~/test")) == False -- cgit v1.2.3 From b0071de9ee7b6b4feb2eb9f19ceb759de21c997f Mon Sep 17 00:00:00 2001 From: Hind-M Date: Fri, 4 Feb 2022 16:09:54 +0100 Subject: Add forgotten module name before func call --- src/python/test/test_remote_datasets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/python/test') diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index 27eb51b0..9532b4ec 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -90,7 +90,7 @@ def test_fetch_remote_datasets(): assert isdir(expanduser("~/remote_datasets")) == True # Test clear_data_home - clear_data_home() + remote.clear_data_home() assert isdir(expanduser("~/remote_datasets")) == False # Test fetch_spiral_2d and fetch_bunny wrapping functions with data directory different from default @@ -103,5 +103,5 @@ def test_fetch_remote_datasets(): assert isdir(expanduser("~/test")) == True # Test clear_data_home with data directory different from default - clear_data_home("~/test") + remote.clear_data_home("~/test") assert isdir(expanduser("~/test")) == False -- cgit v1.2.3 From b5d7d6c2857d305ba2828065310c11edefb37c4e Mon Sep 17 00:00:00 2001 From: Hind-M Date: Mon, 7 Feb 2022 13:03:45 +0100 Subject: Test get_data_home and clear_data_home on a separate folder --- src/python/test/test_remote_datasets.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'src/python/test') diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index 9532b4ec..c160f270 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -87,21 +87,20 @@ def test_fetch_remote_datasets(): assert bunny_arr.shape == (35947, 3) # Check that default dir was created - assert isdir(expanduser("~/remote_datasets")) == True - - # Test clear_data_home - remote.clear_data_home() - assert isdir(expanduser("~/remote_datasets")) == False + assert isdir(expanduser("~/remote_datasets")) # Test fetch_spiral_2d and fetch_bunny wrapping functions with data directory different from default - spiral_2d_arr = remote.fetch_spiral_2d(dirname = "~/test") + spiral_2d_arr = remote.fetch_spiral_2d(dirname = "~/another_fetch_folder") assert spiral_2d_arr.shape == (114562, 2) - bunny_arr = remote.fetch_bunny(dirname = "~/test") + bunny_arr = remote.fetch_bunny(dirname = "~/another_fetch_folder") assert bunny_arr.shape == (35947, 3) - assert isdir(expanduser("~/test")) == True + assert isdir(expanduser("~/another_fetch_folder")) + + # Test get_data_home and clear_data_home on new empty folder + empty_data_home = remote.get_data_home(data_home="empty_folder") + assert isdir(empty_data_home) - # Test clear_data_home with data directory different from default - remote.clear_data_home("~/test") - assert isdir(expanduser("~/test")) == False + remote.clear_data_home(data_home=empty_data_home) + assert not isdir(empty_data_home) -- cgit v1.2.3 From e9b020adf11d48ce7a88932a5fe12cef011e72c9 Mon Sep 17 00:00:00 2001 From: Hind-M Date: Mon, 7 Feb 2022 13:18:57 +0100 Subject: Separate tests into different functions and remove all test folders at the end --- src/python/test/test_remote_datasets.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'src/python/test') diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index c160f270..2e595423 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -7,15 +7,17 @@ # Modification(s): # - YYYY/MM Author: Description of the modification - from gudhi.datasets import remote + import re -from os.path import isfile, isdir, expanduser -from os import makedirs +import shutil import io import sys import pytest +from os.path import isfile, isdir, expanduser +from os import makedirs + def _check_dir_file_names(path_file_dw, filename, dirname): assert isfile(path_file_dw) @@ -76,8 +78,9 @@ def test_fetch_remote_datasets(): assert "" == _get_bunny_license_print(accept_license = True).getvalue() # Remove "remote_datasets" directory and all its content - import shutil shutil.rmtree("remote_datasets") + +def test_fetch_remote_datasets_wrapped(): # Test fetch_spiral_2d and fetch_bunny wrapping functions (twice, to test case of already fetched files) for i in range(2): spiral_2d_arr = remote.fetch_spiral_2d() @@ -98,6 +101,14 @@ def test_fetch_remote_datasets(): assert isdir(expanduser("~/another_fetch_folder")) + # Remove test folders + shutil.rmtree(expanduser("~/remote_datasets")) + shutil.rmtree(expanduser("~/another_fetch_folder")) + + assert not isdir(expanduser("~/remote_datasets")) + assert not isdir(expanduser("~/another_fetch_folder")) + +def test_data_home(): # Test get_data_home and clear_data_home on new empty folder empty_data_home = remote.get_data_home(data_home="empty_folder") assert isdir(empty_data_home) -- cgit v1.2.3 From e964ec32247ce02fb12939cfcddaeabc04639869 Mon Sep 17 00:00:00 2001 From: Hind-M Date: Mon, 7 Feb 2022 16:52:55 +0100 Subject: Del used variables before removing folders --- src/python/test/test_remote_datasets.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/python/test') diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index 2e595423..cb53cb85 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -102,6 +102,8 @@ def test_fetch_remote_datasets_wrapped(): assert isdir(expanduser("~/another_fetch_folder")) # Remove test folders + del spiral_2d_arr + del bunny_arr shutil.rmtree(expanduser("~/remote_datasets")) shutil.rmtree(expanduser("~/another_fetch_folder")) -- cgit v1.2.3 From 5c0c731fdd2bc41c2a4833be1612dca5a082c337 Mon Sep 17 00:00:00 2001 From: Hind-M Date: Wed, 2 Mar 2022 10:26:52 +0100 Subject: Modifications following PR review --- src/python/gudhi/datasets/remote.py | 60 ++++++++++++++++++--------------- src/python/test/test_remote_datasets.py | 38 ++++++++++----------- 2 files changed, 51 insertions(+), 47 deletions(-) (limited to 'src/python/test') diff --git a/src/python/gudhi/datasets/remote.py b/src/python/gudhi/datasets/remote.py index 3d6c01b0..618fa80e 100644 --- a/src/python/gudhi/datasets/remote.py +++ b/src/python/gudhi/datasets/remote.py @@ -20,14 +20,14 @@ def get_data_home(data_home = None): """ Return the path of the remote datasets directory. This folder is used to store remotely fetched datasets. - By default the datasets directory is set to a folder named 'remote_datasets' in the user home folder. + By default the datasets directory is set to a folder named 'gudhi_data' in the user home folder. Alternatively, it can be set by giving an explicit folder path. The '~' symbol is expanded to the user home folder. If the folder does not already exist, it is automatically created. Parameters ---------- data_home : string - The path to remote datasets directory. Default is `None`, meaning that the data home directory will be set to "~/remote_datasets". + The path to remote datasets directory. Default is `None`, meaning that the data home directory will be set to "~/gudhi_data". Returns ------- @@ -35,7 +35,7 @@ def get_data_home(data_home = None): The path to remote datasets directory. """ if data_home is None: - data_home = join("~", "remote_datasets") + data_home = join("~", "gudhi_data") data_home = expanduser(data_home) makedirs(data_home, exist_ok=True) return data_home @@ -43,12 +43,12 @@ def get_data_home(data_home = None): def clear_data_home(data_home = None): """ - Delete all the content of the data home cache. + Delete the data home cache directory and all its content. Parameters ---------- data_home : string, default is None. - The path to remote datasets directory. If `None`, the default directory to be removed is set to "~/remote_datasets". + The path to remote datasets directory. If `None`, the default directory to be removed is set to "~/gudhi_data". """ data_home = get_data_home(data_home) shutil.rmtree(data_home) @@ -77,7 +77,7 @@ def _checksum_sha256(file_path): sha256_hash.update(buffer) return sha256_hash.hexdigest() -def _fetch_remote(url, filename, dirname = "remote_datasets", file_checksum = None, accept_license = False): +def _fetch_remote(url, filename, dirname = "gudhi_data", file_checksum = None, accept_license = False): """ Fetch the wanted dataset from the given url and save it in file_path. @@ -88,10 +88,10 @@ def _fetch_remote(url, filename, dirname = "remote_datasets", file_checksum = No filename : string The name to give to downloaded file. dirname : string - The directory to save the file to. Default is "remote_datasets". + The directory to save the file to. Default is "gudhi_data". file_checksum : string The file checksum using sha256 to check against the one computed on the downloaded file. - Default is 'None'. + Default is 'None', which means the checksum is not checked. accept_license : boolean Flag to specify if user accepts the file LICENSE and prevents from printing the corresponding license terms. Default is False. @@ -100,6 +100,11 @@ def _fetch_remote(url, filename, dirname = "remote_datasets", file_checksum = No ------- file_path: string Full path of the created file. + + Raises + ------ + IOError + If the computed SHA256 checksum of file does not match the one given by the user. """ file_path = join(dirname, filename) @@ -123,32 +128,37 @@ def _fetch_remote(url, filename, dirname = "remote_datasets", file_checksum = No return file_path +def _get_archive_and_dir(dirname, filename, label): + if dirname is None: + dirname = join(get_data_home(dirname), label) + makedirs(dirname, exist_ok=True) + else: + dirname = get_data_home(dirname) + + archive_path = join(dirname, filename) + + return archive_path, dirname + def fetch_spiral_2d(filename = "spiral_2d.npy", dirname = None): """ - Fetch "spiral_2d.npy" remotely. + Fetch spiral_2d dataset remotely. Parameters ---------- filename : string The name to give to downloaded file. Default is "spiral_2d.npy". dirname : string - The directory to save the file to. Default is None, meaning that the data home will be set to "~/remote_datasets/spiral_2d". + The directory to save the file to. Default is None, meaning that the data home will be set to "~/gudhi_data/spiral_2d". Returns ------- points: array - Array of points stored in "spiral_2d.npy". + Array of points. """ file_url = "https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d/spiral_2d.npy" file_checksum = '88312ffd6df2e2cb2bde9c0e1f962d7d644c6f58dc369c7b377b298dacdc4eaf' - if dirname is None: - dirname = join(get_data_home(dirname), "spiral_2d") - makedirs(dirname, exist_ok=True) - else: - dirname = get_data_home(dirname) - - archive_path = join(dirname, filename) + archive_path, dirname = _get_archive_and_dir(dirname, filename, "spiral_2d") if not exists(archive_path): file_path_pkl = _fetch_remote(file_url, filename, dirname, file_checksum) @@ -159,14 +169,14 @@ def fetch_spiral_2d(filename = "spiral_2d.npy", dirname = None): def fetch_bunny(filename = "bunny.npy", dirname = None, accept_license = False): """ - Fetch "bunny.npy" remotely and its LICENSE file. + Fetch Stanford bunny dataset remotely and its LICENSE file. Parameters ---------- filename : string The name to give to downloaded file. Default is "bunny.npy". dirname : string - The directory to save the file to. Default is None, meaning that the data home will be set to "~/remote_datasets/bunny". + The directory to save the file to. Default is None, meaning that the data home will be set to "~/gudhi_data/bunny". accept_license : boolean Flag to specify if user accepts the file LICENSE and prevents from printing the corresponding license terms. Default is False. @@ -174,7 +184,7 @@ def fetch_bunny(filename = "bunny.npy", dirname = None, accept_license = False): Returns ------- points: array - Array of points stored in "bunny.npy". + Array of points. """ file_url = "https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/bunny.npy" @@ -182,13 +192,7 @@ def fetch_bunny(filename = "bunny.npy", dirname = None, accept_license = False): license_url = "https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/LICENSE" license_checksum = 'b763dbe1b2fc6015d05cbf7bcc686412a2eb100a1f2220296e3b4a644c69633a' - if dirname is None: - dirname = join(get_data_home(dirname), "bunny") - makedirs(dirname, exist_ok=True) - else: - dirname = get_data_home(dirname) - - archive_path = join(dirname, filename) + archive_path, dirname = _get_archive_and_dir(dirname, filename, "bunny") if not exists(archive_path): license_path = _fetch_remote(license_url, "LICENSE", dirname, license_checksum) diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index cb53cb85..c44ac22b 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -22,7 +22,7 @@ def _check_dir_file_names(path_file_dw, filename, dirname): assert isfile(path_file_dw) names_dw = re.split(r' |/|\\', path_file_dw) - # Case where inner directories are created in "remote_datasets/"; e.g: "remote_datasets/bunny" + # Case where inner directories are created in "test_gudhi_data/"; e.g: "test_gudhi_data/bunny" if len(names_dw) >= 3: for i in range(len(names_dw)-1): assert re.split(r' |/|\\', dirname)[i] == names_dw[i] @@ -31,7 +31,7 @@ def _check_dir_file_names(path_file_dw, filename, dirname): assert dirname == names_dw[0] assert filename == names_dw[1] -def _check_fetch_output(url, filename, dirname = "remote_datasets", file_checksum = None): +def _check_fetch_output(url, filename, dirname = "test_gudhi_data", file_checksum = None): makedirs(dirname, exist_ok=True) path_file_dw = remote._fetch_remote(url, filename, dirname, file_checksum) _check_dir_file_names(path_file_dw, filename, dirname) @@ -41,9 +41,9 @@ def _get_bunny_license_print(accept_license = False): # Redirect stdout sys.stdout = capturedOutput - makedirs("remote_datasets/bunny", exist_ok=True) + makedirs("test_gudhi_data/bunny", exist_ok=True) - remote._fetch_remote("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/bunny.npy", "bunny.npy", "remote_datasets/bunny", + remote._fetch_remote("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/bunny.npy", "bunny.npy", "test_gudhi_data/bunny", '13f7842ebb4b45370e50641ff28c88685703efa5faab14edf0bb7d113a965e1b', accept_license) # Reset redirect sys.stdout = sys.__stdout__ @@ -68,19 +68,21 @@ def test_fetch_remote_datasets(): # Test printing existing LICENSE file when fetching bunny.npy with accept_license = False (default) # Fetch LICENSE file - makedirs("remote_datasets/bunny", exist_ok=True) - remote._fetch_remote("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/LICENSE", "LICENSE", "remote_datasets/bunny", + makedirs("test_gudhi_data/bunny", exist_ok=True) + remote._fetch_remote("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/LICENSE", "LICENSE", "test_gudhi_data/bunny", 'b763dbe1b2fc6015d05cbf7bcc686412a2eb100a1f2220296e3b4a644c69633a') - with open("remote_datasets/bunny/LICENSE") as f: + with open("test_gudhi_data/bunny/LICENSE") as f: assert f.read().rstrip("\n") == _get_bunny_license_print().getvalue().rstrip("\n") # Test not printing bunny.npy LICENSE when accept_license = True assert "" == _get_bunny_license_print(accept_license = True).getvalue() - # Remove "remote_datasets" directory and all its content - shutil.rmtree("remote_datasets") + # Remove "test_gudhi_data" directory and all its content + shutil.rmtree("test_gudhi_data") def test_fetch_remote_datasets_wrapped(): + # Check if gudhi_data default dir exists already + to_be_removed = not isdir(expanduser("~/gudhi_data")) # Test fetch_spiral_2d and fetch_bunny wrapping functions (twice, to test case of already fetched files) for i in range(2): spiral_2d_arr = remote.fetch_spiral_2d() @@ -90,29 +92,27 @@ def test_fetch_remote_datasets_wrapped(): assert bunny_arr.shape == (35947, 3) # Check that default dir was created - assert isdir(expanduser("~/remote_datasets")) + assert isdir(expanduser("~/gudhi_data")) # Test fetch_spiral_2d and fetch_bunny wrapping functions with data directory different from default - spiral_2d_arr = remote.fetch_spiral_2d(dirname = "~/another_fetch_folder") + spiral_2d_arr = remote.fetch_spiral_2d(dirname = "./another_fetch_folder_for_test") assert spiral_2d_arr.shape == (114562, 2) - bunny_arr = remote.fetch_bunny(dirname = "~/another_fetch_folder") + bunny_arr = remote.fetch_bunny(dirname = "./another_fetch_folder_for_test") assert bunny_arr.shape == (35947, 3) - assert isdir(expanduser("~/another_fetch_folder")) + assert isdir(expanduser("./another_fetch_folder_for_test")) # Remove test folders del spiral_2d_arr del bunny_arr - shutil.rmtree(expanduser("~/remote_datasets")) - shutil.rmtree(expanduser("~/another_fetch_folder")) - - assert not isdir(expanduser("~/remote_datasets")) - assert not isdir(expanduser("~/another_fetch_folder")) + if to_be_removed: + shutil.rmtree(expanduser("~/gudhi_data")) + shutil.rmtree(expanduser("./another_fetch_folder_for_test")) def test_data_home(): # Test get_data_home and clear_data_home on new empty folder - empty_data_home = remote.get_data_home(data_home="empty_folder") + empty_data_home = remote.get_data_home(data_home="empty_folder_for_test") assert isdir(empty_data_home) remote.clear_data_home(data_home=empty_data_home) -- cgit v1.2.3 From ef8284cce27a8f11947e7f076034aa2fd8b5a395 Mon Sep 17 00:00:00 2001 From: Hind-M Date: Wed, 4 May 2022 15:27:34 +0200 Subject: Ask for file_path as parameter of remote fetching functions instead of both dirname and filename Modify remote fetching test --- src/python/gudhi/datasets/remote.py | 106 +++++++++++++++----------------- src/python/test/test_remote_datasets.py | 94 ++++++++++------------------ 2 files changed, 83 insertions(+), 117 deletions(-) (limited to 'src/python/test') diff --git a/src/python/gudhi/datasets/remote.py b/src/python/gudhi/datasets/remote.py index 8b3baef4..5b535911 100644 --- a/src/python/gudhi/datasets/remote.py +++ b/src/python/gudhi/datasets/remote.py @@ -7,7 +7,7 @@ # Modification(s): # - YYYY/MM Author: Description of the modification -from os.path import join, exists, expanduser +from os.path import join, split, exists, expanduser from os import makedirs, remove from urllib.request import urlretrieve @@ -60,7 +60,7 @@ def _checksum_sha256(file_path): Parameters ---------- file_path: string - Full path of the created file. + Full path of the created file including filename. Returns ------- @@ -77,7 +77,7 @@ def _checksum_sha256(file_path): sha256_hash.update(buffer) return sha256_hash.hexdigest() -def _fetch_remote(url, filename, dirname, file_checksum = None, accept_license = False): +def _fetch_remote(url, file_path, file_checksum = None): """ Fetch the wanted dataset from the given url and save it in file_path. @@ -85,21 +85,11 @@ def _fetch_remote(url, filename, dirname, file_checksum = None, accept_license = ---------- url : string The url to fetch the dataset from. - filename : string - The name to give to downloaded file. - dirname : string - The directory to save the file to. + file_path : string + Full path of the downloaded file including filename. file_checksum : string The file checksum using sha256 to check against the one computed on the downloaded file. Default is 'None', which means the checksum is not checked. - accept_license : boolean - Flag to specify if user accepts the file LICENSE and prevents from printing the corresponding license terms. - Default is False. - - Returns - ------- - file_path: string - Full path of the created file. Raises ------ @@ -107,8 +97,6 @@ def _fetch_remote(url, filename, dirname, file_checksum = None, accept_license = If the computed SHA256 checksum of file does not match the one given by the user. """ - file_path = join(dirname, filename) - # Get the file urlretrieve(url, file_path) @@ -121,36 +109,41 @@ def _fetch_remote(url, filename, dirname, file_checksum = None, accept_license = "different from expected : {}." "The file may be corrupted or the given url may be wrong !".format(file_path, checksum, file_checksum)) - # Print license terms unless accept_license is set to True - if not accept_license: - license_file = join(dirname, "LICENSE") - if exists(license_file) and (file_path != license_file): - with open(license_file, 'r') as f: - print(f.read()) +def _get_archive_path(file_path, label): + """ + Get archive path based on file_path given by user and label. - return file_path + Parameters + ---------- + file_path: string + Full path of the file to get including filename, or None. + label: string + Label used along with 'data_home' to get archive path, in case 'file_path' is None. -def _get_archive_and_dir(dirname, filename, label): - if dirname is None: - dirname = join(get_data_home(dirname), label) + Returns + ------- + Full path of archive including filename. + """ + if file_path is None: + archive_path = join(get_data_home(), label) + dirname = split(archive_path)[0] makedirs(dirname, exist_ok=True) else: - dirname = get_data_home(dirname) - - archive_path = join(dirname, filename) + archive_path = file_path + dirname = split(archive_path)[0] + makedirs(dirname, exist_ok=True) - return archive_path, dirname + return archive_path -def fetch_spiral_2d(filename = "spiral_2d.npy", dirname = None): +def fetch_spiral_2d(file_path = None): """ Fetch spiral_2d dataset remotely. Parameters ---------- - filename : string - The name to give to downloaded file. Default is "spiral_2d.npy". - dirname : string - The directory to save the file to. Default is None, meaning that the downloaded file will be put in "~/gudhi_data/points/spiral_2d". + file_path : string + Full path of the downloaded file including filename. + Default is None, meaning that it's set to "data_home/points/spiral_2d/spiral_2d.npy". Returns ------- @@ -158,28 +151,25 @@ def fetch_spiral_2d(filename = "spiral_2d.npy", dirname = None): Array of shape (114562, 2). """ file_url = "https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d/spiral_2d.npy" - file_checksum = '88312ffd6df2e2cb2bde9c0e1f962d7d644c6f58dc369c7b377b298dacdc4eaf' + file_checksum = '2226024da76c073dd2f24b884baefbfd14928b52296df41ad2d9b9dc170f2401' - archive_path, dirname = _get_archive_and_dir(dirname, filename, "points/spiral_2d") + archive_path = _get_archive_path(file_path, "points/spiral_2d/spiral_2d.npy") if not exists(archive_path): - file_path_pkl = _fetch_remote(file_url, filename, dirname, file_checksum) + _fetch_remote(file_url, archive_path, file_checksum) - return np.load(file_path_pkl, mmap_mode='r') - else: - return np.load(archive_path, mmap_mode='r') + return np.load(archive_path, mmap_mode='r') -def fetch_bunny(filename = "bunny.npy", dirname = None, accept_license = False): +def fetch_bunny(file_path = None, accept_license = False): """ Fetch Stanford bunny dataset remotely and its LICENSE file. This dataset contains 35947 vertices. Parameters ---------- - filename : string - The name to give to downloaded file. Default is "bunny.npy". - dirname : string - The directory to save the file to. Default is None, meaning that the downloaded files will be put in "~/gudhi_data/points/bunny". + file_path : string + Full path of the downloaded file including filename. + Default is None, meaning that it's set to "data_home/points/bunny/bunny.npy". accept_license : boolean Flag to specify if user accepts the file LICENSE and prevents from printing the corresponding license terms. Default is False. @@ -191,16 +181,20 @@ def fetch_bunny(filename = "bunny.npy", dirname = None, accept_license = False): """ file_url = "https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/bunny.npy" - file_checksum = '13f7842ebb4b45370e50641ff28c88685703efa5faab14edf0bb7d113a965e1b' - license_url = "https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/LICENSE" + file_checksum = 'f382482fd89df8d6444152dc8fd454444fe597581b193fd139725a85af4a6c6e' + license_url = "https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/bunny.LICENSE" license_checksum = 'b763dbe1b2fc6015d05cbf7bcc686412a2eb100a1f2220296e3b4a644c69633a' - archive_path, dirname = _get_archive_and_dir(dirname, filename, "points/bunny") + archive_path = _get_archive_path(file_path, "points/bunny/bunny.npy") if not exists(archive_path): - license_path = _fetch_remote(license_url, "LICENSE", dirname, license_checksum) - file_path_pkl = _fetch_remote(file_url, filename, dirname, file_checksum, accept_license) - - return np.load(file_path_pkl, mmap_mode='r') - else: - return np.load(archive_path, mmap_mode='r') + _fetch_remote(file_url, archive_path, file_checksum) + license_path = join(split(archive_path)[0], "bunny.LICENSE") + _fetch_remote(license_url, license_path, license_checksum) + # Print license terms unless accept_license is set to True + if not accept_license: + if exists(license_path): + with open(license_path, 'r') as f: + print(f.read()) + + return np.load(archive_path, mmap_mode='r') diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index c44ac22b..5d0d397d 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -9,76 +9,48 @@ from gudhi.datasets import remote -import re import shutil import io import sys import pytest -from os.path import isfile, isdir, expanduser -from os import makedirs +from os.path import isdir, expanduser, exists +from os import remove -def _check_dir_file_names(path_file_dw, filename, dirname): - assert isfile(path_file_dw) +def test_data_home(): + # Test get_data_home and clear_data_home on new empty folder + empty_data_home = remote.get_data_home(data_home="empty_folder_for_test") + assert isdir(empty_data_home) - names_dw = re.split(r' |/|\\', path_file_dw) - # Case where inner directories are created in "test_gudhi_data/"; e.g: "test_gudhi_data/bunny" - if len(names_dw) >= 3: - for i in range(len(names_dw)-1): - assert re.split(r' |/|\\', dirname)[i] == names_dw[i] - assert filename == names_dw[i+1] - else: - assert dirname == names_dw[0] - assert filename == names_dw[1] + remote.clear_data_home(data_home=empty_data_home) + assert not isdir(empty_data_home) -def _check_fetch_output(url, filename, dirname = "test_gudhi_data", file_checksum = None): - makedirs(dirname, exist_ok=True) - path_file_dw = remote._fetch_remote(url, filename, dirname, file_checksum) - _check_dir_file_names(path_file_dw, filename, dirname) +def test_fetch_remote(): + # Test fetch with a wrong checksum + with pytest.raises(OSError): + remote._fetch_remote("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d/spiral_2d.npy", "tmp_spiral_2d.npy", file_checksum = 'XXXXXXXXXX') + assert not exists("tmp_spiral_2d.npy") def _get_bunny_license_print(accept_license = False): capturedOutput = io.StringIO() # Redirect stdout sys.stdout = capturedOutput - makedirs("test_gudhi_data/bunny", exist_ok=True) + bunny_arr = remote.fetch_bunny("./tmp_for_test/bunny.npy", accept_license) + assert bunny_arr.shape == (35947, 3) + remove("./tmp_for_test/bunny.npy") - remote._fetch_remote("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/bunny.npy", "bunny.npy", "test_gudhi_data/bunny", - '13f7842ebb4b45370e50641ff28c88685703efa5faab14edf0bb7d113a965e1b', accept_license) # Reset redirect sys.stdout = sys.__stdout__ return capturedOutput -def test_fetch_remote_datasets(): - # Test fetch with a wrong checksum - with pytest.raises(OSError): - _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d/spiral_2d.npy", "spiral_2d.npy", file_checksum = 'XXXXXXXXXX') - - # Test files download from given urls with checksums provided - _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d/spiral_2d.npy", "spiral_2d.npy", - file_checksum = '88312ffd6df2e2cb2bde9c0e1f962d7d644c6f58dc369c7b377b298dacdc4eaf') - - _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off", - file_checksum = '32f96d2cafb1177f0dd5e0a019b6ff5658e14a619a7815ae55ad0fc5e8bd3f88') - - # Test files download from given urls without checksums - _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d/spiral_2d.npy", "spiral_2d.npy") - - _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off") - - # Test printing existing LICENSE file when fetching bunny.npy with accept_license = False (default) - # Fetch LICENSE file - makedirs("test_gudhi_data/bunny", exist_ok=True) - remote._fetch_remote("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/bunny/LICENSE", "LICENSE", "test_gudhi_data/bunny", - 'b763dbe1b2fc6015d05cbf7bcc686412a2eb100a1f2220296e3b4a644c69633a') - with open("test_gudhi_data/bunny/LICENSE") as f: - assert f.read().rstrip("\n") == _get_bunny_license_print().getvalue().rstrip("\n") - +def test_print_bunny_license(): # Test not printing bunny.npy LICENSE when accept_license = True assert "" == _get_bunny_license_print(accept_license = True).getvalue() - - # Remove "test_gudhi_data" directory and all its content - shutil.rmtree("test_gudhi_data") + # Test printing bunny.LICENSE file when fetching bunny.npy with accept_license = False (default) + with open("./tmp_for_test/bunny.LICENSE") as f: + assert f.read().rstrip("\n") == _get_bunny_license_print().getvalue().rstrip("\n") + shutil.rmtree("./tmp_for_test") def test_fetch_remote_datasets_wrapped(): # Check if gudhi_data default dir exists already @@ -93,27 +65,27 @@ def test_fetch_remote_datasets_wrapped(): # Check that default dir was created assert isdir(expanduser("~/gudhi_data")) + # Check downloaded files + assert exists(expanduser("~/gudhi_data/points/spiral_2d/spiral_2d.npy")) + assert exists(expanduser("~/gudhi_data/points/bunny/bunny.npy")) + assert exists(expanduser("~/gudhi_data/points/bunny/bunny.LICENSE")) # Test fetch_spiral_2d and fetch_bunny wrapping functions with data directory different from default - spiral_2d_arr = remote.fetch_spiral_2d(dirname = "./another_fetch_folder_for_test") + spiral_2d_arr = remote.fetch_spiral_2d("./another_fetch_folder_for_test/spiral_2d.npy") assert spiral_2d_arr.shape == (114562, 2) - bunny_arr = remote.fetch_bunny(dirname = "./another_fetch_folder_for_test") + bunny_arr = remote.fetch_bunny("./another_fetch_folder_for_test/bunny.npy") assert bunny_arr.shape == (35947, 3) - assert isdir(expanduser("./another_fetch_folder_for_test")) + assert isdir("./another_fetch_folder_for_test") + # Check downloaded files + assert exists("./another_fetch_folder_for_test/spiral_2d.npy") + assert exists("./another_fetch_folder_for_test/bunny.npy") + assert exists("./another_fetch_folder_for_test/bunny.LICENSE") # Remove test folders del spiral_2d_arr del bunny_arr if to_be_removed: shutil.rmtree(expanduser("~/gudhi_data")) - shutil.rmtree(expanduser("./another_fetch_folder_for_test")) - -def test_data_home(): - # Test get_data_home and clear_data_home on new empty folder - empty_data_home = remote.get_data_home(data_home="empty_folder_for_test") - assert isdir(empty_data_home) - - remote.clear_data_home(data_home=empty_data_home) - assert not isdir(empty_data_home) + shutil.rmtree("./another_fetch_folder_for_test") -- cgit v1.2.3 From 52d5b524403a43bfdc0b27a7feeec04e9c9c34c2 Mon Sep 17 00:00:00 2001 From: Hind-M Date: Thu, 5 May 2022 17:43:12 +0200 Subject: Add GUDHI_DATA environment variable option --- src/python/gudhi/datasets/remote.py | 16 +++++++++++----- src/python/test/test_remote_datasets.py | 13 ++++++++++++- 2 files changed, 23 insertions(+), 6 deletions(-) (limited to 'src/python/test') diff --git a/src/python/gudhi/datasets/remote.py b/src/python/gudhi/datasets/remote.py index 5b535911..eac8caf3 100644 --- a/src/python/gudhi/datasets/remote.py +++ b/src/python/gudhi/datasets/remote.py @@ -8,7 +8,7 @@ # - YYYY/MM Author: Description of the modification from os.path import join, split, exists, expanduser -from os import makedirs, remove +from os import makedirs, remove, environ from urllib.request import urlretrieve import hashlib @@ -21,13 +21,16 @@ def get_data_home(data_home = None): Return the path of the remote datasets directory. This folder is used to store remotely fetched datasets. By default the datasets directory is set to a folder named 'gudhi_data' in the user home folder. - Alternatively, it can be set by giving an explicit folder path. The '~' symbol is expanded to the user home folder. + Alternatively, it can be set by the 'GUDHI_DATA' environment variable. + The '~' symbol is expanded to the user home folder. If the folder does not already exist, it is automatically created. Parameters ---------- data_home : string - The path to remote datasets directory. Default is `None`, meaning that the data home directory will be set to "~/gudhi_data". + The path to remote datasets directory. + Default is `None`, meaning that the data home directory will be set to "~/gudhi_data", + if the 'GUDHI_DATA' environment variable does not exist. Returns ------- @@ -35,7 +38,7 @@ def get_data_home(data_home = None): The path to remote datasets directory. """ if data_home is None: - data_home = join("~", "gudhi_data") + data_home = environ.get("GUDHI_DATA", join("~", "gudhi_data")) data_home = expanduser(data_home) makedirs(data_home, exist_ok=True) return data_home @@ -48,7 +51,9 @@ def clear_data_home(data_home = None): Parameters ---------- data_home : string, default is None. - The path to remote datasets directory. If `None`, the default directory to be removed is set to "~/gudhi_data". + The path to remote datasets directory. + If `None` and the 'GUDHI_DATA' environment variable does not exist, + the default directory to be removed is set to "~/gudhi_data". """ data_home = get_data_home(data_home) shutil.rmtree(data_home) @@ -170,6 +175,7 @@ def fetch_bunny(file_path = None, accept_license = False): file_path : string Full path of the downloaded file including filename. Default is None, meaning that it's set to "data_home/points/bunny/bunny.npy". + In this case, the LICENSE file would be downloaded as "data_home/points/bunny/bunny.LICENSE". accept_license : boolean Flag to specify if user accepts the file LICENSE and prevents from printing the corresponding license terms. Default is False. diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index 5d0d397d..af26d77c 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -15,7 +15,7 @@ import sys import pytest from os.path import isdir, expanduser, exists -from os import remove +from os import remove, environ def test_data_home(): # Test get_data_home and clear_data_home on new empty folder @@ -89,3 +89,14 @@ def test_fetch_remote_datasets_wrapped(): if to_be_removed: shutil.rmtree(expanduser("~/gudhi_data")) shutil.rmtree("./another_fetch_folder_for_test") + +def test_gudhi_data_env(): + # Set environment variable "GUDHI_DATA" + environ["GUDHI_DATA"] = "./test_folder_from_env_var" + bunny_arr = remote.fetch_bunny() + assert bunny_arr.shape == (35947, 3) + assert exists("./test_folder_from_env_var/points/bunny/bunny.npy") + assert exists("./test_folder_from_env_var/points/bunny/bunny.LICENSE") + # Remove test folder + del bunny_arr + shutil.rmtree("./test_folder_from_env_var") -- cgit v1.2.3 From f344700ebee65de9ccc8799f2ec4e1c633ab864e Mon Sep 17 00:00:00 2001 From: Hind-M Date: Thu, 5 May 2022 18:07:52 +0200 Subject: Remove default data home test (because of 'GUDHI_DATA' environment variable option) --- src/python/test/test_remote_datasets.py | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) (limited to 'src/python/test') diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index af26d77c..6f569fd2 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -53,30 +53,16 @@ def test_print_bunny_license(): shutil.rmtree("./tmp_for_test") def test_fetch_remote_datasets_wrapped(): - # Check if gudhi_data default dir exists already - to_be_removed = not isdir(expanduser("~/gudhi_data")) - # Test fetch_spiral_2d and fetch_bunny wrapping functions (twice, to test case of already fetched files) + # Test fetch_spiral_2d and fetch_bunny wrapping functions with data directory different from default (twice, to test case of already fetched files) + # Default case is not tested because it would fail in case the user sets the 'GUDHI_DATA' environment variable locally for i in range(2): - spiral_2d_arr = remote.fetch_spiral_2d() + spiral_2d_arr = remote.fetch_spiral_2d("./another_fetch_folder_for_test/spiral_2d.npy") assert spiral_2d_arr.shape == (114562, 2) - bunny_arr = remote.fetch_bunny() + bunny_arr = remote.fetch_bunny("./another_fetch_folder_for_test/bunny.npy") assert bunny_arr.shape == (35947, 3) - # Check that default dir was created - assert isdir(expanduser("~/gudhi_data")) - # Check downloaded files - assert exists(expanduser("~/gudhi_data/points/spiral_2d/spiral_2d.npy")) - assert exists(expanduser("~/gudhi_data/points/bunny/bunny.npy")) - assert exists(expanduser("~/gudhi_data/points/bunny/bunny.LICENSE")) - - # Test fetch_spiral_2d and fetch_bunny wrapping functions with data directory different from default - spiral_2d_arr = remote.fetch_spiral_2d("./another_fetch_folder_for_test/spiral_2d.npy") - assert spiral_2d_arr.shape == (114562, 2) - - bunny_arr = remote.fetch_bunny("./another_fetch_folder_for_test/bunny.npy") - assert bunny_arr.shape == (35947, 3) - + # Check that the directory was created assert isdir("./another_fetch_folder_for_test") # Check downloaded files assert exists("./another_fetch_folder_for_test/spiral_2d.npy") @@ -86,8 +72,6 @@ def test_fetch_remote_datasets_wrapped(): # Remove test folders del spiral_2d_arr del bunny_arr - if to_be_removed: - shutil.rmtree(expanduser("~/gudhi_data")) shutil.rmtree("./another_fetch_folder_for_test") def test_gudhi_data_env(): -- cgit v1.2.3 From a809771b6d7381d233656f7a0b02211559189bfe Mon Sep 17 00:00:00 2001 From: Hind-M Date: Fri, 6 May 2022 09:52:26 +0200 Subject: Delete bunny array before removing the file --- src/python/test/test_remote_datasets.py | 1 + 1 file changed, 1 insertion(+) (limited to 'src/python/test') diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index 6f569fd2..cde9fa22 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -38,6 +38,7 @@ def _get_bunny_license_print(accept_license = False): bunny_arr = remote.fetch_bunny("./tmp_for_test/bunny.npy", accept_license) assert bunny_arr.shape == (35947, 3) + del bunny_arr remove("./tmp_for_test/bunny.npy") # Reset redirect -- cgit v1.2.3 From ce34ee3e5c28c48d605f23332cfa3c10e471a047 Mon Sep 17 00:00:00 2001 From: Hind-M Date: Tue, 24 May 2022 15:57:52 +0200 Subject: Make get_data_home function private --- src/python/doc/datasets.rst | 2 -- src/python/gudhi/datasets/remote.py | 6 +++--- src/python/test/test_remote_datasets.py | 4 ++-- 3 files changed, 5 insertions(+), 7 deletions(-) (limited to 'src/python/test') diff --git a/src/python/doc/datasets.rst b/src/python/doc/datasets.rst index d2975533..8b0912c4 100644 --- a/src/python/doc/datasets.rst +++ b/src/python/doc/datasets.rst @@ -127,6 +127,4 @@ We provide some ready-to-use datasets that are not available by default when get 2D spiral with 114562 vertices. -.. autofunction:: gudhi.datasets.remote.get_data_home - .. autofunction:: gudhi.datasets.remote.clear_data_home diff --git a/src/python/gudhi/datasets/remote.py b/src/python/gudhi/datasets/remote.py index d2ae2a75..7e6f647f 100644 --- a/src/python/gudhi/datasets/remote.py +++ b/src/python/gudhi/datasets/remote.py @@ -16,7 +16,7 @@ import shutil import numpy as np -def get_data_home(data_home = None): +def _get_data_home(data_home = None): """ Return the path of the remote datasets directory. This folder is used to store remotely fetched datasets. @@ -55,7 +55,7 @@ def clear_data_home(data_home = None): If `None` and the 'GUDHI_DATA' environment variable does not exist, the default directory to be removed is set to "~/gudhi_data". """ - data_home = get_data_home(data_home) + data_home = _get_data_home(data_home) shutil.rmtree(data_home) def _checksum_sha256(file_path): @@ -130,7 +130,7 @@ def _get_archive_path(file_path, label): Full path of archive including filename. """ if file_path is None: - archive_path = join(get_data_home(), label) + archive_path = join(_get_data_home(), label) dirname = split(archive_path)[0] makedirs(dirname, exist_ok=True) else: diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index cde9fa22..e5d2de82 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -18,8 +18,8 @@ from os.path import isdir, expanduser, exists from os import remove, environ def test_data_home(): - # Test get_data_home and clear_data_home on new empty folder - empty_data_home = remote.get_data_home(data_home="empty_folder_for_test") + # Test _get_data_home and clear_data_home on new empty folder + empty_data_home = remote._get_data_home(data_home="empty_folder_for_test") assert isdir(empty_data_home) remote.clear_data_home(data_home=empty_data_home) -- cgit v1.2.3