forked from DarkEnergySurvey/ugali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
323 lines (275 loc) · 10 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
from __future__ import print_function
import sys
import os
import io
try:
from setuptools import setup, find_packages
from setuptools.command.install import install as _install
except ImportError:
from distutils.core import setup
from distutils.command.install import install as _install
def find_packages():
return ['ugali','ugali.analysis','ugali.config','ugali.observation',
'ugali.preprocess','ugali.simulation','ugali.candidate',
'ugali.utils']
import distutils.cmd
import versioneer
VERSION = versioneer.get_version()
NAME = 'ugali'
HERE = os.path.abspath(os.path.dirname(__file__))
URL = 'https://github.com/DarkEnergySurvey/ugali'
DESC = "Ultra-faint galaxy likelihood toolkit."
LONG_DESC = "%s\n%s"%(DESC,URL)
CLASSIFIERS = """\
Development Status :: 4 - Beta
Intended Audience :: Science/Research
Intended Audience :: Developers
License :: OSI Approved :: MIT License
Natural Language :: English
Operating System :: MacOS :: MacOS X
Operating System :: POSIX :: Linux
Programming Language :: Python :: 2
Programming Language :: Python :: 3
Topic :: Scientific/Engineering
Topic :: Scientific/Engineering :: Astronomy
Topic :: Scientific/Engineering :: Physics
"""
RELEASE_URL = URL+'/releases/download/v1.8.0'
UGALIDIR = os.getenv("UGALIDIR","$HOME/.ugali")
ISOSIZE = "~1MB"
CATSIZE = "~20MB"
TSTSIZE = "~1MB"
# Could find file size dynamically, but it's a bit slow...
# int(urllib.urlopen(ISOCHRONES).info().getheaders("Content-Length")[0])/1024**2
SURVEYS = ['des','ps1','sdss','lsst']
MODELS = ['bressan2012','marigo2017','dotter2008','dotter2016']
class ProgressFileIO(io.FileIO):
def __init__(self, path, *args, **kwargs):
self._total_size = os.path.getsize(path)
io.FileIO.__init__(self, path, *args, **kwargs)
def read(self, size):
count = self.tell()/size
self.progress_bar(count,size,self._total_size)
return io.FileIO.read(self, size)
@staticmethod
def progress_bar(count, block_size, total_size):
block = 100*block_size/float(total_size)
progress = count*block
if progress % 5 < 1.01*block:
msg = '\r[{:51}] ({:d}%)'.format(int(progress//2)*'='+'>',int(progress))
sys.stdout.write(msg)
sys.stdout.flush()
class TarballCommand(distutils.cmd.Command,object):
""" Command for downloading data files """
description = "install data files"
user_options = [
('ugali-dir=',None,
'path to install data files [default: %s]'%UGALIDIR),
('force','f',
'force installation (overwrite any existing files)')
]
boolean_options = ['force']
release_url = RELEASE_URL
_tarball = None
_dirname = None
def initialize_options(self):
self.ugali_dir = os.path.expandvars(UGALIDIR)
self.force = False
# Not really the best way, but ok...
self.tarball = self._tarball
self.dirname = self._dirname
def finalize_options(self):
# Required by abstract base class
pass
@property
def path(self):
return os.path.join(self.ugali_dir,self.dirname)
def check_exists(self):
return os.path.exists(self.path)
def install_tarball(self, tarball):
try:
from urllib.request import urlopen, urlretrieve
from urllib.error import HTTPError
except ImportError:
from urllib import urlopen, urlretrieve
from urllib2 import HTTPError
import tarfile
if not os.path.exists(self.ugali_dir):
print("creating %s"%self.ugali_dir)
os.makedirs(self.ugali_dir)
os.chdir(self.ugali_dir)
url = os.path.join(self.release_url,tarball)
print("downloading %s..."%url)
if urlopen(url).getcode() >= 400:
raise Exception('url does not exist')
urlretrieve(url,tarball,reporthook=ProgressFileIO.progress_bar)
print('')
if not os.path.exists(tarball):
raise HTTPError()
print("extracting %s..."%tarball)
with tarfile.open(fileobj=ProgressFileIO(tarball),mode='r:gz') as tar:
## Check if the directory exists?
#if os.path.exists(tar.next().name) and not self.force:
# print("directory found; skipping installation")
tar.extractall()
tar.close()
print('')
print("removing %s"%tarball)
os.remove(tarball)
def run(self):
if self.dry_run:
print("skipping data install")
return
if self.check_exists():
print("found %s"%self.path)
if self.force:
print("overwriting directory")
else:
print("use '--force' to overwrite")
return
self.install_tarball(self.tarball)
class CatalogCommand(TarballCommand):
""" Command for downloading catalog files """
description = "install catalog files"
_tarball = 'ugali-catalogs.tar.gz'
_dirname = 'catalogs'
class TestsCommand(TarballCommand):
""" Command for downloading catalog files """
description = "install test data"
_tarball = 'ugali-test-data.tar.gz'
_dirname = 'testdata'
class IsochroneCommand(TarballCommand):
""" Command for downloading isochrone files """
description = "install isochrone files"
user_options = TarballCommand.user_options + [
('survey=',None,
'survey set [default: None]'),
('model=',None,
'isochrone model [default: None]')
]
_tarball = 'ugali-isochrones-tiny.tar.gz'
_dirname = 'isochrones'
def initialize_options(self):
super(IsochroneCommand,self).initialize_options()
self.survey = None
self.model = None
def finalize_options(self):
super(IsochroneCommand,self).finalize_options()
self._build_surveys()
self._build_models()
def _build_surveys(self):
if self.survey is None:
self.surveys = SURVEYS
else:
self.survey = self.survey.lower()
if self.survey not in SURVEYS:
raise Exception("unrecognized survey: '%s'"%self.survey)
self.surveys = [self.survey]
def _build_models(self):
if self.model is None:
self.models = MODELS
else:
self.model = self.model.lower()
if self.model not in MODELS:
raise Exception("unrecognized model: '%s'"%self.model)
self.models = [self.model]
def run(self):
if self.dry_run:
print("skipping data install")
return
if (self.survey is None) and (self.model is None):
self.tarball = self._tarball
self.dirname = self._dirname
super(IsochroneCommand,self).run()
return
for survey in self.surveys:
for model in self.models:
self.tarball = "ugali-%s-%s.tar.gz"%(survey,model)
self.dirname = "isochrones/%s/%s"%(survey,model)
super(IsochroneCommand,self).run()
class install(_install):
"""
Subclass the setuptools 'install' class.
"""
user_options = _install.user_options + [
('isochrones',None,"install isochrone files (%s)"%ISOSIZE),
('catalogs',None,"install catalog files (%s)"%CATSIZE),
('tests',None,"install test data (%s)"%TSTSIZE),
('ugali-dir=',None,"install file directory [default: %s]"%UGALIDIR),
]
boolean_options = _install.boolean_options + ['isochrones','catalogs']
def initialize_options(self):
_install.initialize_options(self)
self.ugali_dir = os.path.expandvars(UGALIDIR)
self.isochrones = False
self.catalogs = False
self.tests = False
def run(self):
# run superclass install
_install.run(self)
# Could ask user whether they want to install isochrones, but
# pip filters sys.stdout, so the prompt never gets sent:
# https://github.com/pypa/pip/issues/2732#issuecomment-97119093
if self.isochrones:
self.install_isochrones()
if self.catalogs:
self.install_catalogs()
if self.tests:
self.install_tests()
def install_isochrones(self):
"""
Call to isochrone install command:
http://stackoverflow.com/a/24353921/4075339
"""
cmd_obj = self.distribution.get_command_obj('isochrones')
cmd_obj.force = self.force
if self.ugali_dir: cmd_obj.ugali_dir = self.ugali_dir
self.run_command('isochrones')
def install_catalogs(self):
"""
Call to catalog install command:
http://stackoverflow.com/a/24353921/4075339
"""
cmd_obj = self.distribution.get_command_obj('catalogs')
cmd_obj.force = self.force
if self.ugali_dir: cmd_obj.ugali_dir = self.ugali_dir
self.run_command('catalogs')
def install_tests(self):
"""
Call to catalog install command:
http://stackoverflow.com/a/24353921/4075339
"""
cmd_obj = self.distribution.get_command_obj('tests')
cmd_obj.force = self.force
if self.ugali_dir: cmd_obj.ugali_dir = self.ugali_dir
self.run_command('tests')
CMDCLASS = versioneer.get_cmdclass()
CMDCLASS['isochrones'] = IsochroneCommand
CMDCLASS['catalogs'] = CatalogCommand
CMDCLASS['tests'] = TestsCommand
CMDCLASS['install'] = install
setup(
name=NAME,
version=VERSION,
cmdclass=CMDCLASS,
url=URL,
author='Keith Bechtol & Alex Drlica-Wagner',
author_email='[email protected], [email protected]',
scripts = [],
install_requires=[
'astropy',
'matplotlib',
'numpy >= 1.9.0',
'scipy >= 0.14.0',
'healpy >= 1.6.0',
'fitsio >= 0.9.10',
'emcee >= 2.1.0',
'corner >= 1.0.0',
'pyyaml >= 3.10',
],
packages=find_packages(),
description=DESC,
long_description=LONG_DESC,
platforms='any',
classifiers = [_f for _f in CLASSIFIERS.split('\n') if _f]
)