def testDumpsObject(self): testClass = InferenceShifter() testClass.a = 5 testClass.b = {'b': (17,)} encoded = json.dumps(testClass, sort_keys=True) self.assertEqual( encoded, '{"_inferenceBuffer": null, "a": 5, "b": {"b": {"py/tuple": [17]}}, ' '"py/object": "nupic.data.inference_shifter.InferenceShifter"}')
def testDates(self): d = datetime.date(year=2012, month=9, day=25) serialized = json.dumps(d) self.assertEqual(serialized, '{"py/object": "datetime.date", ' '"py/repr": "datetime.date(2012, 9, 25)"}') deserialized = json.loads(serialized) self.assertEqual(type(deserialized), datetime.date) self.assertEqual(deserialized.isoformat(), d.isoformat())
def testDates(self): d = datetime.date(year=2012, month=9, day=25) serialized = json.dumps(d) self.assertEqual( serialized, '{"py/object": "datetime.date", ' '"py/repr": "datetime.date(2012, 9, 25)"}') deserialized = json.loads(serialized) self.assertEqual(type(deserialized), datetime.date) self.assertEqual(deserialized.isoformat(), d.isoformat())
def testDumpsObject(self): testClass = InferenceShifter() testClass.a = 5 testClass.b = {'b': (17, )} encoded = json.dumps(testClass, sort_keys=True) self.assertEqual( encoded, '{"_inferenceBuffer": null, "a": 5, "b": {"b": {"py/tuple": [17]}}, ' '"py/object": "nupic.data.inference_shifter.InferenceShifter"}')
def testDatetimes(self): d = datetime.datetime(year=2012, month=9, day=25, hour=14, minute=33, second=8, microsecond=455969) serialized = json.dumps(d) self.assertEqual(serialized, '{"py/object": "datetime.datetime", "py/repr": ' '"datetime.datetime(2012, 9, 25, 14, 33, 8, 455969)"}') deserialized = json.loads(serialized) self.assertEqual(type(deserialized), datetime.datetime) self.assertEqual(deserialized.isoformat(), d.isoformat())
def testDatetimes(self): d = datetime.datetime(year=2012, month=9, day=25, hour=14, minute=33, second=8, microsecond=455969) serialized = json.dumps(d) self.assertEqual(serialized, '{"py/object": "datetime.datetime", "py/repr": ' '"datetime.datetime(2012, 9, 25, 14, 33, 8, 455969)"}') deserialized = json.loads(serialized) self.assertEqual(type(deserialized), datetime.datetime) self.assertEqual(deserialized.isoformat(), d.isoformat())
def testObjectWithNonStringKeys(self): testClass = InferenceShifter() testClass.a = 5 testClass.b = {(4, 5): (17, )} encoded = json.dumps(testClass, sort_keys=True) self.assertEqual( encoded, '{"_inferenceBuffer": null, "a": 5, "b": {"py/dict/keys": ' '["{\\"py/tuple\\": [4, 5]}"], "{\\"py/tuple\\": [4, 5]}": ' '{"py/tuple": [17]}}, "py/object": ' '"nupic.data.inference_shifter.InferenceShifter"}') decoded = json.loads(encoded) self.assertEqual(decoded.a, 5) self.assertEqual(type(decoded.b), dict) self.assertEqual(len(decoded.b.keys()), 1) self.assertTupleEqual(decoded.b.keys()[0], (4, 5)) self.assertTupleEqual(decoded.b[(4, 5)], (17, ))
def testObjectWithNonStringKeys(self): testClass = InferenceShifter() testClass.a = 5 testClass.b = {(4, 5): (17,)} encoded = json.dumps(testClass, sort_keys=True) self.assertEqual( encoded, '{"_inferenceBuffer": null, "a": 5, "b": {"py/dict/keys": ' '["{\\"py/tuple\\": [4, 5]}"], "{\\"py/tuple\\": [4, 5]}": ' '{"py/tuple": [17]}}, "py/object": ' '"nupic.data.inference_shifter.InferenceShifter"}') decoded = json.loads(encoded) self.assertEqual(decoded.a, 5) self.assertEqual(type(decoded.b), dict) self.assertEqual(len(decoded.b.keys()), 1) self.assertTupleEqual(decoded.b.keys()[0], (4, 5)) self.assertTupleEqual(decoded.b[(4, 5)], (17,))
def testComplex(self): self.assertEqual(json.loads(json.dumps(2 + 1j)), 2 + 1j)
def testPrimitives(self): self.assertEqual(json.loads(json.dumps(None)), None) self.assertEqual(json.loads(json.dumps(True)), True) self.assertEqual(json.loads(json.dumps(False)), False) self.assertEqual(json.loads(json.dumps(-5)), -5) self.assertEqual(json.loads(json.dumps(0)), 0) self.assertEqual(json.loads(json.dumps(5)), 5) self.assertEqual(json.loads(json.dumps(7.7)), 7.7) self.assertEqual(json.loads(json.dumps('hello')), 'hello') self.assertEqual(json.loads(json.dumps(5L)), 5L) self.assertEqual(json.loads(json.dumps(u'hello')), u'hello') self.assertEqual(json.loads(json.dumps([5, 6, 7])), [5, 6, 7]) self.assertEqual(json.loads(json.dumps({'5': 6, '7': 8})), {'5': 6, '7': 8})
def testNonStringKeys(self): original = {1.1: 1, 5: {(7, 8, 9): {1.1: 5}}} result = json.loads(json.dumps(original)) self.assertEqual(original, result)
def testPrimitives(self): self.assertEqual(json.loads(json.dumps(None)), None) self.assertEqual(json.loads(json.dumps(True)), True) self.assertEqual(json.loads(json.dumps(False)), False) self.assertEqual(json.loads(json.dumps(-5)), -5) self.assertEqual(json.loads(json.dumps(0)), 0) self.assertEqual(json.loads(json.dumps(5)), 5) self.assertEqual(json.loads(json.dumps(7.7)), 7.7) self.assertEqual(json.loads(json.dumps('hello')), 'hello') self.assertEqual(json.loads(json.dumps(5L)), 5L) self.assertEqual(json.loads(json.dumps(u'hello')), u'hello') self.assertEqual(json.loads(json.dumps([5, 6, 7])), [5, 6, 7]) self.assertEqual(json.loads(json.dumps({ '5': 6, '7': 8 })), { '5': 6, '7': 8 })
def testDumpsWithIndent(self): d = {'a': 1, 'b': {'c': 2}} s = json.dumps(d, indent=2, sort_keys=True) self.assertEqual(s, '{\n "a": 1,\n "b": {\n "c": 2\n }\n}')
def testComplex(self): self.assertEqual(json.loads(json.dumps(2 + 1j)), 2 + 1j)
def testDumpsTuple(self): self.assertEqual(json.dumps((5, 6, 7)), '{"py/tuple": [5, 6, 7]}')
def testTuple(self): self.assertTupleEqual(json.loads(json.dumps((5, 6, 7))), (5, 6, 7))
def testDumpsTuple(self): self.assertEqual(json.dumps((5, 6, 7)), '{"py/tuple": [5, 6, 7]}')
def testTuple(self): self.assertTupleEqual(json.loads(json.dumps((5, 6, 7))), (5, 6, 7))
def testBasicDumps(self): d = {'a': 1, 'b': {'c': 2}} s = json.dumps(d, sort_keys=True) self.assertEqual(s, '{"a": 1, "b": {"c": 2}}')
def testBasicDumps(self): d = {'a': 1, 'b': {'c': 2}} s = json.dumps(d, sort_keys=True) self.assertEqual(s, '{"a": 1, "b": {"c": 2}}')
def testDumpsWithIndent(self): d = {'a': 1, 'b': {'c': 2}} s = json.dumps(d, indent=2, sort_keys=True) self.assertEqual(s, '{\n "a": 1,\n "b": {\n "c": 2\n }\n}')
def runWithConfig(swarmConfig, options, outDir=None, outputLabel="default", permWorkDir=None, verbosity=1): """ Starts a swarm, given an dictionary configuration. @param swarmConfig {dict} A complete [swarm description](https://github.com/numenta/nupic/wiki/Running-Swarms#the-swarm-description) object. @param outDir {string} Optional path to write swarm details (defaults to current working directory). @param outputLabel {string} Optional label for output (defaults to "default"). @param permWorkDir {string} Optional location of working directory (defaults to current working directory). @param verbosity {int} Optional (1,2,3) increasing verbosity of output. @returns {object} Model parameters """ global g_currentVerbosityLevel g_currentVerbosityLevel = verbosity # Generate the description and permutations.py files in the same directory # for reference. if outDir is None: outDir = os.getcwd() if permWorkDir is None: permWorkDir = os.getcwd() # _checkOverwrite(options, outDir) overwrite = options["overwrite"] if not overwrite: for name in ("description.py", "permutations.py"): if os.path.exists(os.path.join(outDir, name)): raise RuntimeError("The %s file already exists and will be " "overwritten by this tool. If it is OK to overwrite " "this file, use the --overwrite option." % \ os.path.join(outDir, "description.py")) # The overwrite option has already been used, so should be removed from the # config at this point. del options["overwrite"] # # _generateExpFilesFromSwarmDescription(swarmConfig, outDir) # The expGenerator expects the JSON without newlines for an unknown reason. expDescConfig = json.dumps(swarmConfig) expDescConfig = expDescConfig.splitlines() expDescConfig = "".join(expDescConfig) expGenerator([ "--description=%s" % (expDescConfig), "--outDir=%s" % (outDir)]) # options["expDescConfig"] = swarmConfig options["outputLabel"] = outputLabel options["outDir"] = outDir options["permWorkDir"] = permWorkDir # runOptions = _injectDefaultOptions(options) # return dict(DEFAULT_OPTIONS, **options) runOptions = dict(DEFAULT_OPTIONS, **options) # # _validateOptions(runOptions) # def _validateOptions(options): if "expDescJsonPath" not in runOptions \ and "expDescConfig" not in runOptions \ and "permutationsScriptPath" not in runOptions: raise Exception("Options must contain one of the following: " "expDescJsonPath, expDescConfig, or " "permutationsScriptPath.") # # return _runAction(runOptions) # def _runAction(runOptions): if not os.path.exists(runOptions["outDir"]): os.makedirs(runOptions["outDir"]) if not os.path.exists(runOptions["permWorkDir"]): os.makedirs(runOptions["permWorkDir"]) action = runOptions["action"] # Print Nupic HyperSearch results from the current or last run if action == "report": returnValue = _HyperSearchRunner.generateReport( options=runOptions, replaceReport=runOptions["replaceReport"], hyperSearchJob=None, metricsKeys=None) # Run HyperSearch elif action in ("run", "dryRun", "pickup"): returnValue = _runHyperSearch(runOptions) # def _runHyperSearch(runOptions): global gCurrentSearch # Run HyperSearch startTime = time.time() search = _HyperSearchRunner(runOptions) # Save in global for the signal handler. gCurrentSearch = search if runOptions["action"] in ("run", "dryRun"): search.runNewSearch() else: search.pickupSearch() # Generate reports # Print results and generate report csv file modelParams = _HyperSearchRunner.generateReport( options=runOptions, replaceReport=runOptions["replaceReport"], hyperSearchJob=search.peekSearchJob(), metricsKeys=search.getDiscoveredMetricsKeys()) secs = time.time() - startTime hours = int(secs) / (60 * 60) secs -= hours * (60 * 60) minutes = int(secs) / 60 secs -= minutes * 60 print "Elapsed time (h:mm:ss): %d:%02d:%02d" % (hours, minutes, int(secs)) jobID = search.peekSearchJob().getJobID() print "Hypersearch ClientJobs job ID: ", jobID return modelParams
def testNonStringKeys(self): original = {1.1: 1, 5: {(7, 8, 9): {1.1: 5}}} result = json.loads(json.dumps(original)) self.assertEqual(original, result)