File size: 6,482 Bytes
b2e2343 | 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 | #!/usr/bin/env python
# ----------------------------------------------------------------------
# Copyright (C) 2014-2015, Numenta, Inc. Unless you have an agreement
# with Numenta, Inc., for a separate license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero Public License for more details.
#
# You should have received a copy of the GNU Affero Public License
# along with this program. If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------
import argparse
import os
try:
import simplejson as json
except ImportError:
import json
from nab.runner import Runner
from nab.util import (detectorNameToClass, checkInputs)
def getDetectorClassConstructors(detectors):
"""
Takes in names of detectors. Collects class names that correspond to those
detectors and returns them in a dict. The dict maps detector name to class
names. Assumes the detectors have been imported.
"""
detectorConstructors = {
d : globals()[detectorNameToClass(d)] for d in detectors}
return detectorConstructors
def main(args):
root = os.path.dirname(os.path.realpath(__file__))
numCPUs = int(args.numCPUs) if args.numCPUs is not None else None
dataDir = os.path.join(root, args.dataDir)
windowsFile = os.path.join(root, args.windowsFile)
resultsDir = os.path.join(root, args.resultsDir)
profilesFile = os.path.join(root, args.profilesFile)
thresholdsFile = os.path.join(root, args.thresholdsFile)
runner = Runner(dataDir=dataDir,
labelPath=windowsFile,
resultsDir=resultsDir,
profilesPath=profilesFile,
thresholdPath=thresholdsFile,
numCPUs=numCPUs)
runner.initialize()
if args.detect:
detectorConstructors = getDetectorClassConstructors(args.detectors)
runner.detect(detectorConstructors)
if args.optimize:
runner.optimize(args.detectors)
if args.score:
with open(args.thresholdsFile) as thresholdConfigFile:
detectorThresholds = json.load(thresholdConfigFile)
runner.score(args.detectors, detectorThresholds)
if args.normalize:
try:
runner.normalize()
except AttributeError("Error: you must run the scoring step with the "
"normalization step."):
return
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--detect",
help="Generate detector results but do not analyze results "
"files.",
default=False,
action="store_true")
parser.add_argument("--optimize",
help="Optimize the thresholds for each detector and user "
"profile combination",
default=False,
action="store_true")
parser.add_argument("--score",
help="Analyze results in the results directory",
default=False,
action="store_true")
parser.add_argument("--normalize",
help="Normalize the final scores",
default=False,
action="store_true")
parser.add_argument("--skipConfirmation",
help="If specified will skip the user confirmation step",
default=False,
action="store_true")
parser.add_argument("--dataDir",
default="data",
help="This holds all the label windows for the corpus.")
parser.add_argument("--resultsDir",
default="results",
help="This will hold the results after running detectors "
"on the data")
parser.add_argument("--windowsFile",
default=os.path.join("labels", "combined_windows.json"),
help="JSON file containing ground truth labels for the "
"corpus.")
parser.add_argument("-d", "--detectors",
nargs="*",
type=str,
default=["null", "numenta", "random", "skyline"],
help="Comma separated list of detector(s) to use, e.g. "
"null,numenta")
parser.add_argument("-p", "--profilesFile",
default=os.path.join("config", "profiles.json"),
help="The configuration file to use while running the "
"benchmark.")
parser.add_argument("-t", "--thresholdsFile",
default=os.path.join("config", "thresholds.json"),
help="The configuration file that stores thresholds for "
"each combination of detector and username")
parser.add_argument("-n", "--numCPUs",
default=None,
help="The number of CPUs to use to run the "
"benchmark. If not specified all CPUs will be used.")
args = parser.parse_args()
if (not args.detect
and not args.optimize
and not args.score
and not args.normalize):
args.detect = True
args.optimize = True
args.score = True
args.normalize = True
if len(args.detectors) == 1:
# Handle comma-seperated list argument.
args.detectors = args.detectors[0].split(',')
# The following imports are necessary for getDetectorClassConstructors to
# automatically figure out the detector classes.
# Only import numenta detector if used so as to avoid unnecessary dependency
if "numenta" in args.detectors:
from nab.detectors.numenta.numenta_detector import NumentaDetector
from nab.detectors.skyline.skyline_detector import SkylineDetector
from nab.detectors.random.random_detector import RandomDetector
from nab.detectors.null.null_detector import NullDetector
from nab.detectors.democratic.democratic_detector import DemocraticDetector
if args.skipConfirmation or checkInputs(args):
main(args)
|