GetData.py 2.45 KB
Newer Older
1
2
3
4
5
from pathlib import Path
import sys
import urllib.request
import tarfile

Schellhorn's avatar
Schellhorn committed
6
7
datadir = Path(__file__).parent.parent.parent.parent / 'InputData'

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#shows progress of download
def dlProgress(counter, blocksize, size):
    p = counter*blocksize*100.0/size
    sys.stdout.write("\rProgress: %d%%" % p)
    sys.stdout.flush()

#function to ask permission for donwload from user
def query_yes_no(question):
   choices = "[y/n] "
   valid_answer = {'y':True, 'ye':True, 'yes':True, 'n':False, 'ne':False}

   while True:
    sys.stdout.write(question + choices)
    answer = input().lower()
    if answer in valid_answer:
        return valid_answer[answer]
    else:
        sys.stdout.write("Please respond with 'yes' or 'no'. \n")

#downloads the input data to the InputData folder. problemFamily has to be either Phase, CT or Ptychography
def getData(problemFamily):
	if problemFamily == 'Phase':
Schellhorn's avatar
Schellhorn committed
30
		my_file = datadir/'Phase'/'pupil.pmod'
31
	elif problemFamily == 'CT':
Schellhorn's avatar
Schellhorn committed
32
	    	my_file = datadir/'CT'/'ART_SheppLogan.mat'
33
	elif problemFamily == 'Ptychography':
Schellhorn's avatar
Schellhorn committed
34
		my_file = datadir/'Ptychography'/'gaenseliesel.png'
Schellhorn's avatar
Schellhorn committed
35
36
37
	elif problemFamily == 'OrbitalTomog':
		errMsg = 'OrbitalTomog downloader is yet a work in progress'
		raise IOError(errMsg)
38
39
40
41
42
43
	else:
		print("Invalid input in GetData.GetData. problemFamily has to be Phase, CT or Ptychography")
		return -1

	if not(my_file.is_file()):
		print(problemFamily + " input data is missing.") 
44
		if query_yes_no("Do you want to download the " + problemFamily + " input data?"):
Schellhorn's avatar
Schellhorn committed
45
		    urllib.request.urlretrieve(" http://vaopt.math.uni-goettingen.de/data/" + problemFamily +  ".tar.gz", datadir/(problemFamily + '.tar.gz'), reporthook=dlProgress)
46
		    print("\nExtracting data...")
Schellhorn's avatar
Schellhorn committed
47
48
		    tar = tarfile.open(datadir/(problemFamily + '.tar.gz'), "r:gz")
		    tar.extractall(datadir/problemFamily)
49
50
51
52
53
54
55
56
		    tar.close()
		if not(my_file.is_file()):
			print('***************************************************************************************')
			print('* Input data still missing.  Please try automatic download again or manually download *') 
			print('*     http://vaopt.math.uni-goettingen.de/data/' + problemFamily +  '.tar.gz                           *')
			print('* Save and unpack the ' + problemFamily +  '.tar.gz datafile in the                                    *')
			print('*    ProxMatlab/InputData subdirectory                                                *')
			print('***************************************************************************************')