@classmethod<EOL><INDENT>def generateReport(cls,<EOL>options,<EOL>replaceReport,<EOL>hyperSearchJob,<EOL>metricsKeys):<DEDENT>
<EOL>if hyperSearchJob is None:<EOL><INDENT>hyperSearchJob = cls.loadSavedHyperSearchJob(<EOL>permWorkDir=options["<STR_LIT>"],<EOL>outputLabel=options["<STR_LIT>"])<EOL><DEDENT>modelIDs = hyperSearchJob.queryModelIDs()<EOL>bestModel = None<EOL>metricstmp = set()<EOL>searchVar = set()<EOL>for modelInfo in _iterModels(modelIDs):<EOL><INDENT>if modelInfo.isFinished():<EOL><INDENT>vars = list(modelInfo.getParamLabels().keys())<EOL>searchVar.update(vars)<EOL>metrics = modelInfo.getReportMetrics()<EOL>metricstmp.update(list(metrics.keys()))<EOL><DEDENT><DEDENT>if metricsKeys is None:<EOL><INDENT>metricsKeys = metricstmp<EOL><DEDENT>reportWriter = _ReportCSVWriter(hyperSearchJob=hyperSearchJob,<EOL>metricsKeys=metricsKeys,<EOL>searchVar=searchVar,<EOL>outputDirAbsPath=options["<STR_LIT>"],<EOL>outputLabel=options["<STR_LIT>"],<EOL>replaceReport=replaceReport)<EOL>modelStats = _ModelStats()<EOL>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL>searchParams = hyperSearchJob.getParams()<EOL>(optimizationMetricKey, maximizeMetric) = (<EOL>_PermutationUtils.getOptimizationMetricInfo(searchParams))<EOL>formatStr = None<EOL>foundMetricsKeySet = set(metricsKeys)<EOL>sortedMetricsKeys = []<EOL>jobInfo = _clientJobsDB().jobInfo(hyperSearchJob.getJobID())<EOL>if jobInfo.cancel == <NUM_LIT:1>:<EOL><INDENT>raise Exception(jobInfo.workerCompletionMsg)<EOL><DEDENT>try:<EOL><INDENT>results = json.loads(jobInfo.results)<EOL><DEDENT>except Exception as e:<EOL><INDENT>print("<STR_LIT>""<STR_LIT>")<EOL>print("<STR_LIT>", jobInfo)<EOL>print("<STR_LIT>", jobInfo.results)<EOL>print("<STR_LIT>", e)<EOL>raise<EOL><DEDENT>bestModelNum = results["<STR_LIT>"]<EOL>bestModelIterIndex = None<EOL>totalWallTime = <NUM_LIT:0><EOL>totalRecords = <NUM_LIT:0><EOL>scoreModelIDDescList = []<EOL>for (i, modelInfo) in enumerate(_iterModels(modelIDs)):<EOL><INDENT>reportWriter.emit(modelInfo)<EOL>totalRecords+=modelInfo.getNumRecords()<EOL>format = "<STR_LIT>"<EOL>startTime = modelInfo.getStartTime()<EOL>if modelInfo.isFinished():<EOL><INDENT>endTime = modelInfo.getEndTime()<EOL>st = datetime.strptime(startTime, format)<EOL>et = datetime.strptime(endTime, format)<EOL>totalWallTime+=(et-st).seconds<EOL><DEDENT>modelStats.update(modelInfo)<EOL>expDesc = modelInfo.getModelDescription()<EOL>reportMetrics = modelInfo.getReportMetrics()<EOL>optimizationMetrics = modelInfo.getOptimizationMetrics()<EOL>if modelInfo.getModelID() == bestModelNum:<EOL><INDENT>bestModel = modelInfo<EOL>bestModelIterIndex=i<EOL>bestMetric = list(optimizationMetrics.values())[<NUM_LIT:0>]<EOL><DEDENT>if optimizationMetrics:<EOL><INDENT>assert len(optimizationMetrics) == <NUM_LIT:1>, (<EOL>"<STR_LIT>" % (<EOL>len(optimizationMetrics), optimizationMetrics, modelInfo))<EOL><DEDENT>if modelInfo.getCompletionReason().isEOF():<EOL><INDENT>scoreModelIDDescList.append((list(optimizationMetrics.values())[<NUM_LIT:0>],<EOL>modelInfo.getModelID(),<EOL>modelInfo.getGeneratedDescriptionFile(),<EOL>modelInfo.getParamLabels()))<EOL><DEDENT>print("<STR_LIT>" % (i, modelInfo, expDesc))<EOL>if (modelInfo.isFinished() and<EOL>not (modelInfo.getCompletionReason().isStopped or<EOL>modelInfo.getCompletionReason().isEOF())):<EOL><INDENT>print("<STR_LIT>" % modelInfo.getCompletionMsg())<EOL><DEDENT>if reportMetrics:<EOL><INDENT>foundMetricsKeySet.update(iter(reportMetrics.keys()))<EOL>if len(sortedMetricsKeys) != len(foundMetricsKeySet):<EOL><INDENT>sortedMetricsKeys = sorted(foundMetricsKeySet)<EOL>maxKeyLen = max([len(k) for k in sortedMetricsKeys])<EOL>formatStr = "<STR_LIT>" % (maxKeyLen+<NUM_LIT:2>)<EOL><DEDENT>for key in sortedMetricsKeys:<EOL><INDENT>if key in reportMetrics:<EOL><INDENT>if key == optimizationMetricKey:<EOL><INDENT>m = "<STR_LIT>" % reportMetrics[key]<EOL><DEDENT>else:<EOL><INDENT>m = "<STR_LIT>" % reportMetrics[key]<EOL><DEDENT>print(formatStr % (key+"<STR_LIT::>"), m)<EOL><DEDENT><DEDENT>print()<EOL><DEDENT><DEDENT>print("<STR_LIT>")<EOL>if len(modelIDs) > <NUM_LIT:0>:<EOL><INDENT>print("<STR_LIT>" % (<EOL>len(modelIDs),<EOL>("<STR_LIT>"<EOL>if (modelStats.numCompletedKilled + modelStats.numCompletedEOF) ==<EOL>len(modelIDs)<EOL>else "<STR_LIT>" % (<EOL>len(modelIDs) - (<EOL>modelStats.numCompletedKilled + modelStats.numCompletedEOF +<EOL>modelStats.numCompletedStopped)))))<EOL>if modelStats.numStatusOther > <NUM_LIT:0>:<EOL><INDENT>print("<STR_LIT>" % (<EOL>modelStats.numStatusOther))<EOL><DEDENT>print("<STR_LIT>" % modelStats.numStatusWaitingToStart)<EOL>print("<STR_LIT>" % modelStats.numStatusRunning)<EOL>print("<STR_LIT>" % modelStats.numStatusCompleted)<EOL>if modelStats.numCompletedOther > <NUM_LIT:0>:<EOL><INDENT>print("<STR_LIT>" % (<EOL>modelStats.numCompletedOther))<EOL><DEDENT>print("<STR_LIT>" % modelStats.numCompletedEOF)<EOL>print("<STR_LIT>" % modelStats.numCompletedStopped)<EOL>print("<STR_LIT>" % modelStats.numCompletedOrphaned)<EOL>print("<STR_LIT>" % modelStats.numCompletedKilled)<EOL>print("<STR_LIT>" % modelStats.numCompletedError)<EOL>assert modelStats.numStatusOther == <NUM_LIT:0>, "<STR_LIT>" % (<EOL>modelStats.numStatusOther)<EOL>assert modelStats.numCompletedOther == <NUM_LIT:0>, "<STR_LIT>" % (<EOL>modelStats.numCompletedOther)<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>print()<EOL>global gCurrentSearch<EOL>jobStatus = hyperSearchJob.getJobStatus(gCurrentSearch._workers)<EOL>jobResults = jobStatus.getResults()<EOL>if "<STR_LIT>" in jobResults:<EOL><INDENT>print("<STR_LIT>")<EOL>pprint.pprint(jobResults["<STR_LIT>"], indent=<NUM_LIT:4>)<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>if bestModel is not None:<EOL><INDENT>maxKeyLen = max([len(k) for k in sortedMetricsKeys])<EOL>maxKeyLen = max(maxKeyLen, len(optimizationMetricKey))<EOL>formatStr = "<STR_LIT>" % (maxKeyLen+<NUM_LIT:2>)<EOL>bestMetricValue = list(bestModel.getOptimizationMetrics().values())[<NUM_LIT:0>]<EOL>optimizationMetricName = list(bestModel.getOptimizationMetrics().keys())[<NUM_LIT:0>]<EOL>print()<EOL>print("<STR_LIT>" % (<EOL>optimizationMetricName, maximizeMetric))<EOL>print("<STR_LIT>" % (<EOL>bestModelIterIndex, bestModel, bestModel.getModelDescription()))<EOL>print(formatStr % (optimizationMetricName+"<STR_LIT::>"), bestMetricValue)<EOL>print()<EOL>print("<STR_LIT>"  % totalRecords)<EOL>print()<EOL>print("<STR_LIT>" % totalWallTime)<EOL>hsJobParams = hyperSearchJob.getParams()<EOL><DEDENT>if options["<STR_LIT>"] > <NUM_LIT:0>:<EOL><INDENT>print("<STR_LIT>" % (<EOL>options["<STR_LIT>"]))<EOL>scoreModelIDDescList.sort()<EOL>scoreModelIDDescList = scoreModelIDDescList[<EOL><NUM_LIT:0>:options["<STR_LIT>"]]<EOL>i = -<NUM_LIT:1><EOL>for (score, modelID, description, paramLabels) in scoreModelIDDescList:<EOL><INDENT>i += <NUM_LIT:1><EOL>outDir = os.path.join(options["<STR_LIT>"], "<STR_LIT>" % (i))<EOL>print("<STR_LIT>" %(modelID, outDir))<EOL>if not os.path.exists(outDir):<EOL><INDENT>os.makedirs(outDir)<EOL><DEDENT>base_description_path = os.path.join(options["<STR_LIT>"],<EOL>"<STR_LIT>")<EOL>base_description_relpath = os.path.relpath(base_description_path,<EOL>start=outDir)<EOL>description = description.replace(<EOL>"<STR_LIT>",<EOL>"<STR_LIT>" % base_description_relpath)<EOL>fd = open(os.path.join(outDir, "<STR_LIT>"), "<STR_LIT:wb>")<EOL>fd.write(description)<EOL>fd.close()<EOL>fd = open(os.path.join(outDir, "<STR_LIT>"), "<STR_LIT:wb>")<EOL>writer = csv.writer(fd)<EOL>colNames = list(paramLabels.keys())<EOL>colNames.sort()<EOL>writer.writerow(colNames)<EOL>row = [paramLabels[x] for x in colNames]<EOL>writer.writerow(row)<EOL>fd.close()<EOL>print("<STR_LIT>")<EOL>mod = imp.load_source("<STR_LIT:description>", os.path.join(outDir,<EOL>"<STR_LIT>"))<EOL>model_description = mod.descriptionInterface.getModelDescription()<EOL>fd = open(os.path.join(outDir, "<STR_LIT>"), "<STR_LIT:wb>")<EOL>fd.write("<STR_LIT>" % (getCopyrightHead(),<EOL>pprint.pformat(model_description)))<EOL>fd.close()<EOL><DEDENT>print()<EOL><DEDENT>reportWriter.finalize()<EOL>return model_description<EOL>
Prints all available results in the given HyperSearch job and emits
        model information to the permutations report csv.

        The job may be completed or still in progress.

        Parameters:
        ----------------------------------------------------------------------
        options:        NupicRunPermutations options dict
        replaceReport:  True to replace existing report csv, if any; False to
                        append to existing report csv, if any
        hyperSearchJob: _HyperSearchJob instance; if None, will get it from saved
                        jobID, if any
        metricsKeys:    sequence of report metrics key names to include in report;
                        if None, will pre-scan all modelInfos to generate a complete
                        list of metrics key names.
        retval:         model parameters