forked from numenta/nupic-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
659 lines (536 loc) · 18.3 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
import sys
import os
import subprocess
import shutil
import glob
import urllib2
import tarfile
import re
import numpy
from setuptools import setup, find_packages, Extension
"""
This file builds and installs the NuPIC binaries.
"""
NUPIC_CORE_BUCKET = (
"https://s3-us-west-2.amazonaws.com/artifacts.numenta.org/numenta/nupic.core"
)
REPO_DIR = os.path.dirname(os.path.realpath(__file__))
DARWIN_PLATFORM = "darwin"
LINUX_PLATFORM = "linux"
UNIX_PLATFORMS = [LINUX_PLATFORM, DARWIN_PLATFORM]
WINDOWS_PLATFORMS = ["windows"]
def downloadFile(url, destFile, silent=False):
"""
Download a file to the specified location
"""
if not silent:
print "Downloading from\n\t%s\nto\t%s.\n" % (url, destFile)
destDir = os.path.dirname(destFile)
if not os.path.exists(destDir):
os.makedirs(destDir)
try:
response = urllib2.urlopen(url)
except urllib2.URLError:
return False
with open(destFile, "wb") as fileObj:
totalSize = response.info().getheader("Content-Length").strip()
totalSize = int(totalSize)
bytesSoFar = 0
# Download chunks writing them to target file
chunkSize = 8192
oldPercent = 0
while True:
chunk = response.read(chunkSize)
bytesSoFar += len(chunk)
if not chunk:
break
fileObj.write(chunk)
# Show progress
if not silent:
percent = (float(bytesSoFar) / totalSize) * 100
percent = int(percent)
if percent != oldPercent and percent % 5 == 0:
print ("Downloaded %i of %i bytes (%i%%)."
% (bytesSoFar, totalSize, int(percent)))
oldPercent = percent
return True
def unpackFile(package, dirToUnpack, destDir, silent=False):
"""
Unpack package file to the specified directory
"""
if not silent:
print "Unpacking %s into %s..." % (package, destDir)
with tarfile.open(package, "r:gz") as tarFileObj:
tarFileObj.extractall(destDir)
# Copy subdirectories to a level up
subDirs = os.listdir(destDir + "/" + dirToUnpack)
for subDir in subDirs:
shutil.rmtree(destDir + "/" + subDir, True)
shutil.move(destDir + "/" + dirToUnpack + "/" + subDir,
destDir + "/" + subDir)
shutil.rmtree(destDir + "/" + dirToUnpack, True)
def printOptions(optionsDesc):
"""
Print command line options.
"""
print "Options:\n"
for option in optionsDesc:
optionUsage = "--" + option[0]
if option[1] != "":
optionUsage += "=[" + option[1] + "]"
optionDesc = option[2]
print " " + optionUsage.ljust(30) + " = " + optionDesc
def getCommandLineOptions():
# optionDesc = [name, value, description]
optionsDesc = []
optionsDesc.append(
["nupic-core-dir",
"dir",
"(optional) Absolute path to nupic.core binary release directory"]
)
optionsDesc.append(
["skip-compare-versions",
"",
"(optional) Skip nupic.core version comparison"]
)
# Read command line options looking for extra options
# For example, an user could type:
# python setup.py install --nupic-core-dir="path/to/release"
# which will set the nupic.core release dir
optionsValues = dict()
for arg in sys.argv[:]:
optionFound = False
for option in optionsDesc:
name = option[0]
if "--" + name in arg:
value = None
hasValue = (option[1] != "")
if hasValue:
value = arg.partition("=")[2]
optionsValues[name] = value
sys.argv.remove(arg)
optionFound = True
break
if not optionFound:
if ("--help-nupic" in arg):
printOptions(optionsDesc)
sys.exit()
return optionsValues
def getPlatformInfo():
"""
Identify platform
"""
if "linux" in sys.platform:
platform = "linux"
elif "darwin" in sys.platform:
platform = "darwin"
elif "windows" in sys.platform:
platform = "windows"
else:
raise Exception("Platform '%s' is unsupported!" % sys.platform)
if sys.maxsize > 2**32:
bitness = "64"
else:
bitness = "32"
return platform, bitness
def getVersion():
"""
Get version from local file.
"""
with open("VERSION", "r") as versionFile:
return versionFile.read().strip()
def generateSwigWrap(swigExecutable, swigFlags, interfaceFile):
"""
Generate C++ code from the specified SWIG interface file.
"""
wrap = interfaceFile.replace(".i", "_wrap.cxx")
cmd = swigExecutable + " -c++ -python "
for flag in swigFlags:
cmd += flag + " "
cmd += interfaceFile
print cmd
proc = subprocess.Popen(cmd, shell=True)
proc.wait()
return wrap
def findRequirements():
"""
Read the requirements.txt file and parse into requirements for setup's
install_requirements option.
"""
requirementsPath = os.path.join(REPO_DIR, "external/common/requirements.txt")
return [
line.strip()
for line in open(requirementsPath).readlines()
if not line.startswith("#")
]
def getLibPrefix(platform):
"""
Returns the default system prefix of a compiled library.
"""
if platform in UNIX_PLATFORMS:
return "lib"
elif platform in WINDOWS_PLATFORMS:
return ""
def getStaticLibExtension(platform):
"""
Returns the default system extension of a compiled static library.
"""
if platform in UNIX_PLATFORMS:
return ".a"
elif platform in WINDOWS_PLATFORMS:
return ".lib"
def getSharedLibExtension(platform):
"""
Returns the default system extension of a compiled shared library.
"""
if platform in UNIX_PLATFORMS:
return ".so"
elif platform in WINDOWS_PLATFORMS:
return ".dll"
def extractNupicCoreTarget():
# First, get the nupic.core SHA and remote location from local config.
nupicConfig = {}
if os.path.exists(REPO_DIR + "/.nupic_config"):
execfile(
os.path.join(REPO_DIR, ".nupic_config"), {}, nupicConfig
)
elif os.path.exists(os.environ["HOME"] + "/.nupic_config"):
execfile(
os.path.join(os.environ["HOME"], ".nupic_config"), {}, nupicConfig
)
else:
execfile(
os.path.join(REPO_DIR, ".nupic_modules"), {}, nupicConfig
)
return nupicConfig["NUPIC_CORE_COMMITISH"]
def getDefaultNupicCoreDirectories():
# Default nupic.core location is relative to the NuPIC checkout.
return (
REPO_DIR + "/extensions/core/build/release",
REPO_DIR + "/extensions/core"
)
def getExtensionModules(nupicCoreReleaseDir, platform, bitness):
#
# Gives the version of Python necessary to get installation directories
# for use with pythonVersion, etc.
#
if sys.version_info < (2, 7):
raise Exception("Fatal Error: Python 2.7 or later is required.")
pythonVersion = str(sys.version_info[0]) + '.' + str(sys.version_info[1])
#
# Find out where system installation of python is.
#
pythonPrefix = sys.prefix
pythonPrefix = pythonPrefix.replace("\\", "/")
pythonIncludeDir = pythonPrefix + "/include/python" + pythonVersion
#
# Finds out version of Numpy and headers' path.
#
numpyIncludeDir = numpy.get_include()
numpyIncludeDir = numpyIncludeDir.replace("\\", "/")
commonDefines = [
("NUPIC2", None),
("NTA_OS_" + platform.upper(), None),
("NTA_ARCH_" + bitness, None),
("NTA_PYTHON_SUPPORT", pythonVersion),
("NTA_INTERNAL", None),
("NTA_ASSERTIONS_ON", None),
("NTA_ASM", None),
("HAVE_CONFIG_H", None),
("BOOST_NO_WREGEX", None)]
commonIncludeDirs = [
REPO_DIR + "/external/" + platform + bitness + "/include",
REPO_DIR + "/external/common/include",
REPO_DIR + "/extensions",
REPO_DIR,
nupicCoreReleaseDir + "/include",
pythonIncludeDir,
numpyIncludeDir]
commonCompileFlags = [
# Adhere to c++11 spec
"-std=c++11",
# Generate 32 or 64 bit code
"-m" + bitness,
# `position independent code`, required for shared libraries
"-fPIC",
"-fvisibility=hidden",
"-Wall",
"-Wreturn-type",
"-Wunused",
"-Wno-unused-parameter"]
if platform == "darwin":
commonCompileFlags.append("-stdlib=libc++")
commonLinkFlags = [
"-m" + bitness,
"-fPIC",
"-L" + pythonPrefix + "/lib",
"-L" + nupicCoreReleaseDir + "/lib",
"-lkj",
"-lcapnp",
"-lcapnpc",
]
commonLibraries = [
"dl",
"python" + pythonVersion,
"kj",
"capnp",
"capnpc"]
if platform == "linux":
commonLibraries.extend(["pthread"])
commonObjects = [
nupicCoreReleaseDir + "/lib/" +
getLibPrefix(platform) + "nupic_core" + getStaticLibExtension(platform)]
pythonSupportSources = [
"extensions/py_support/NumpyVector.cpp",
"extensions/py_support/PyArray.cpp",
"extensions/py_support/PyHelpers.cpp",
"extensions/py_support/PythonStream.cpp"]
extensions = []
libDynamicCppRegion = Extension(
"nupic." + getLibPrefix(platform) + "cpp_region",
extra_compile_args=commonCompileFlags,
define_macros=commonDefines,
extra_link_args=commonLinkFlags,
include_dirs=commonIncludeDirs,
libraries=commonLibraries,
sources=pythonSupportSources +
["extensions/cpp_region/PyRegion.cpp",
"extensions/cpp_region/unittests/PyHelpersTest.cpp"],
extra_objects=commonObjects)
extensions.append(libDynamicCppRegion)
#
# SWIG
#
swigDir = REPO_DIR + "/external/common/share/swig/3.0.2"
swigExecutable = (
REPO_DIR + "/external/" + platform + bitness + "/bin/swig"
)
# SWIG options from:
# https://github.com/swig/swig/blob/master/Source/Modules/python.cxx#L111
swigFlags = [
"-features",
"autodoc=0,directors=0",
"-noproxyimport",
"-keyword",
"-modern",
"-modernargs",
"-noproxydel",
"-fvirtual",
"-fastunpack",
"-nofastproxy",
"-fastquery",
"-outputtuple",
"-castmode",
"-nosafecstrings",
"-w402", #TODO silence warnings
"-w503",
"-w511",
"-w302",
"-w362",
"-w312",
"-w389",
"-DSWIG_PYTHON_LEGACY_BOOL",
"-I" + swigDir + "/python",
"-I" + swigDir]
for define in commonDefines:
item = "-D" + define[0]
if define[1]:
item += define[0] + "=" + define[1]
swigFlags.append(item)
for includeDir in commonIncludeDirs:
item = "-I" + includeDir
swigFlags.append(item)
wrapAlgorithms = generateSwigWrap(swigExecutable,
swigFlags,
"nupic/bindings/algorithms.i")
libModuleAlgorithms = Extension(
"nupic.bindings._algorithms",
extra_compile_args=commonCompileFlags,
define_macros=commonDefines,
extra_link_args=commonLinkFlags,
include_dirs=commonIncludeDirs,
libraries=commonLibraries,
sources=pythonSupportSources + [wrapAlgorithms],
extra_objects=commonObjects)
extensions.append(libModuleAlgorithms)
wrapEngineInternal = generateSwigWrap(swigExecutable,
swigFlags,
"nupic/bindings/engine_internal.i")
libModuleEngineInternal = Extension(
"nupic.bindings._engine_internal",
extra_compile_args=commonCompileFlags,
define_macros=commonDefines,
extra_link_args=commonLinkFlags,
include_dirs=commonIncludeDirs,
libraries=commonLibraries,
sources=pythonSupportSources + [wrapEngineInternal],
extra_objects=commonObjects)
extensions.append(libModuleEngineInternal)
wrapMath = generateSwigWrap(swigExecutable,
swigFlags,
"nupic/bindings/math.i")
libModuleMath = Extension(
"nupic.bindings._math",
extra_compile_args=commonCompileFlags,
define_macros=commonDefines,
extra_link_args=commonLinkFlags,
include_dirs=commonIncludeDirs,
libraries=commonLibraries,
sources=pythonSupportSources + [wrapMath,
"nupic/bindings/PySparseTensor.cpp"],
extra_objects=commonObjects)
extensions.append(libModuleMath)
return extensions
def getCommandLineOption(name, options):
if name in options:
return options[name]
def prepareNupicCore(options, platform, bitness):
nupicCoreReleaseDir = getCommandLineOption("nupic-core-dir", options)
nupicCoreSourceDir = None
fetchNupicCore = True
if nupicCoreReleaseDir:
# User specified that they have their own nupic.core
fetchNupicCore = False
else:
nupicCoreReleaseDir, nupicCoreSourceDir = getDefaultNupicCoreDirectories()
nupicCoreCommitish = extractNupicCoreTarget()
if fetchNupicCore:
# User has not specified 'nupic.core' location, so we'll download the
# binaries.
nupicCoreRemoteUrl = (NUPIC_CORE_BUCKET + "/nupic_core-"
+ nupicCoreCommitish + "-" + platform + bitness + ".tar.gz")
nupicCoreLocalPackage = (nupicCoreSourceDir + "/nupic_core-"
+ nupicCoreCommitish + "-" + platform + bitness + ".tar.gz")
nupicCoreLocalDirToUnpack = ("nupic_core-"
+ nupicCoreCommitish + "-" + platform + bitness)
if os.path.exists(nupicCoreLocalPackage):
print ("Target nupic.core package already exists at "
+ nupicCoreLocalPackage + ".")
unpackFile(
nupicCoreLocalPackage, nupicCoreLocalDirToUnpack, nupicCoreReleaseDir
)
else:
print "Attempting to fetch nupic.core binaries..."
downloadSuccess = downloadFile(
nupicCoreRemoteUrl, nupicCoreLocalPackage
)
# TODO: Give user a way to clean up all the downloaded binaries. It can
# be manually done with `rm -rf $NUPIC_CORE/extensions/core` but would
# be cleaner with something like `python setup.py clean`.
if not downloadSuccess:
raise Exception("Failed to download nupic.core tarball from %s}! "
"Ensure you have an internet connection and that the "
"remote tarball exists." % nupicCoreRemoteUrl)
else:
print "Download successful."
unpackFile(nupicCoreLocalPackage,
nupicCoreLocalDirToUnpack,
nupicCoreReleaseDir)
else:
print "Using nupic.core binaries at " + nupicCoreReleaseDir
if getCommandLineOption("skip-compare-versions", options):
skipCompareVersions = True
else:
skipCompareVersions = not fetchNupicCore
if not skipCompareVersions:
# Compare expected version of nupic.core against installed version
with open(nupicCoreReleaseDir + "/include/nupic/Version.hpp",
"r") as fileObj:
content = fileObj.read()
nupicCoreVersionFound = re.search(
"#define NUPIC_CORE_VERSION \"([a-z0-9]+)\"", content
).group(1)
if nupicCoreCommitish != nupicCoreVersionFound:
raise Exception(
"Fatal Error: Unexpected version of nupic.core! "
"Expected %s, but detected %s."
% (nupicCoreCommitish, nupicCoreVersionFound)
)
return nupicCoreReleaseDir
def postProcess():
# Copy proto files located at nupic.core dir into nupic dir
buildDir = glob.glob(REPO_DIR + "/build/lib.*/")[0]
protoBuildDir = nupicCoreReleaseDir + "/include/nupic/proto"
protoSourceDir = buildDir + "/nupic/bindings/proto"
if not os.path.exists(protoSourceDir):
os.makedirs(protoSourceDir)
for fileName in glob.glob(protoBuildDir + "/*.capnp"):
shutil.copy(fileName, protoSourceDir)
# Copy binaries located at nupic.core dir into source dir
print ("Copying binaries from " + nupicCoreReleaseDir + "/bin" + " to "
+ REPO_DIR + "/bin...")
if not os.path.exists(REPO_DIR + "/bin"):
os.makedirs(REPO_DIR + "/bin")
shutil.copy(
nupicCoreReleaseDir + "/bin/py_region_test", REPO_DIR + "/bin"
)
# Copy cpp_region located at build dir into source dir
shutil.copy(buildDir + "/nupic/" + getLibPrefix(platform) + "cpp_region" +
getSharedLibExtension(platform), REPO_DIR + "/nupic")
options = getCommandLineOptions()
platform, bitness = getPlatformInfo()
if platform == DARWIN_PLATFORM and not "ARCHFLAGS" in os.environ:
raise Exception("To build NuPIC in OS X, you must "
"`export ARCHFLAGS=\"-arch x86_64\"`.")
# Build and setup NuPIC
cwd = os.getcwd()
os.chdir(REPO_DIR)
try:
haveBuild = False
buildCommands = ["build", "install", "develop", "bdist", "bdist_wheel"]
for arg in sys.argv[:]:
if arg in buildCommands:
haveBuild = True
if haveBuild:
nupicCoreReleaseDir = prepareNupicCore(options, platform, bitness)
extensions = getExtensionModules(
nupicCoreReleaseDir, platform, bitness
)
else:
extensions = []
setup(
name="nupic",
version=getVersion(),
install_requires=findRequirements(),
packages=find_packages(),
# A lot of this stuff may not be packaged properly, most of it was added
# in an effort to get a binary package prepared for nupic.regression
# testing on Travis-CI, but it wasn't done the right way. I'll be
# refactoring a lot of this for
# https://github.com/numenta/nupic/issues/408, so this will be changing
# soon. -- Matt
package_data={
"nupic.support": ["nupic-default.xml",
"nupic-logging.conf"],
"nupic": ["README.md", "LICENSE.txt"],
"nupic.data": ["*.json"],
"nupic.frameworks.opf.exp_generator": ["*.json", "*.tpl"],
"nupic.frameworks.opf.jsonschema": ["*.json"],
"nupic.swarming.jsonschema": ["*.json"]
},
include_package_data=True,
ext_modules=extensions,
description="Numenta Platform for Intelligent Computing",
author="Numenta",
author_email="help@numenta.org",
url="https://github.com/numenta/nupic",
classifiers=[
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"License :: OSI Approved :: GNU General Public License (GPL)",
"Operating System :: MacOS :: MacOS X",
"Operating System :: POSIX :: Linux",
# It has to be "5 - Production/Stable" or else pypi rejects it!
"Development Status :: 5 - Production/Stable",
"Environment :: Console",
"Intended Audience :: Science/Research",
"Topic :: Scientific/Engineering :: Artificial Intelligence"
],
long_description = """\
Numenta Platform for Intelligent Computing: a machine intelligence platform that implements the HTM learning algorithms. HTM is a detailed computational theory of the neocortex. At the core of HTM are time-based continuous learning algorithms that store and recall spatial and temporal patterns. NuPIC is suited to a variety of problems, particularly anomaly detection and prediction of streaming data sources.
For more information, see http://numenta.org or the NuPIC wiki at https://github.com/numenta/nupic/wiki.
""")
if haveBuild:
postProcess()
finally:
os.chdir(cwd)