def Func(arg_0, arg_1):
  """ Compares two python dictionaries at the top level and report differences,
  if any, to stdout

  da:             first dictionary
  db:             second dictionary

  Returns:        The same value as returned by dictDiff() for the given args
  """
  arg_2 = dictDiff(arg_0, arg_1)

  if not arg_2:
    return arg_2

  if arg_2['inAButNotInB']:
    print ">>> inAButNotInB: %s" % arg_2['inAButNotInB']

  if arg_2['inBButNotInA']:
    print ">>> inBButNotInA: %s" % arg_2['inBButNotInA']

  for arg_3 in arg_2['differentValues']:
    print ">>> da[%s] != db[%s]" % (arg_3, arg_3)
    print "da[%s] = %r" % (arg_3, arg_0[arg_3])
    print "db[%s] = %r" % (arg_3, arg_1[arg_3])

  return arg_2


def Func(arg_0=0.001):
  """
  Given model params, figure out the correct resolution for the
  RandomDistributed encoder. Modifies params in place.
  """
  arg_1 = (
    model_params.MODEL_PARAMS["modelParams"]["sensorParams"]["encoders"]["value"]
  )

  if arg_1["type"] == "RandomDistributedScalarEncoder":
    arg_2 = abs(_INPUT_MAX - _INPUT_MIN) * 0.2
    arg_3 = _INPUT_MIN - arg_2
    arg_4 = _INPUT_MAX + arg_2
    arg_5 = max(arg_0,
                     (arg_4 - arg_3) / arg_1.pop("numBuckets")
                    )
    arg_1["resolution"] = arg_5


def Func(arg_0, arg_1=None, arg_2=None, arg_3=None):
    """
    Remove labels from each record with record ROWID in range from
    start to end, noninclusive of end. Removes all records if labelFilter is
    None, otherwise only removes the labels eqaul to labelFilter.

    This will recalculate all points from end to the last record stored in the
    internal cache of this classifier.
    """

    if len(arg_0.saved_states) == 0:
      raise HTMPredictionModelInvalidRangeError("Invalid supplied range for "
        "'Func'. Model has no saved records.")

    arg_4 = arg_0.saved_states[0].ROWID

    arg_5 = 0 if arg_1 is None else max(0, arg_1 - arg_4)
    arg_6 = len(arg_0.saved_states) if arg_2 is None else \
      max(0, min( len( arg_0.saved_states) , arg_2 - arg_4))

    if arg_6 <= arg_5:
      raise HTMPredictionModelInvalidRangeError("Invalid supplied range for "
        "'Func'.", debugInfo={
          'requestRange': {
            'startRecordID': arg_1,
            'endRecordID': arg_2
          },
          'clippedRequestRange': {
            'startRecordID': arg_5,
            'endRecordID': arg_6
          },
          'validRange': {
            'startRecordID': arg_4,
            'endRecordID': arg_0.saved_states[len(arg_0.saved_states)-1].ROWID
          },
          'numRecordsStored': len(arg_0.saved_states)
        })

    # Remove records within the cache
    arg_7 = []
    for arg_8 in arg_0.saved_states[arg_5:arg_6]:
      if arg_3 is not None:
        if arg_3 in arg_8.anomalyLabel:
          arg_8.anomalyLabel.remove(arg_3)
      else:
        arg_8.anomalyLabel = []
      arg_8.setByUser = False
      arg_7.append(arg_8)
    arg_0._deleteRecordsFromKNN(arg_7)

    # Remove records not in cache
    arg_0._deleteRangeFromKNN(arg_1, arg_2)

    # Recompute [clippedEnd, ...)
    for arg_8 in arg_0.saved_states[arg_6:]:
      arg_0._updateState(arg_8)

    return {'status': 'success'}


def Func(arg_0, arg_1):
    """
    This method will remove the given records from the classifier.

    parameters
    ------------
    recordsToDelete - list of records to delete from the classififier
    """
    arg_2 = arg_0.htm_prediction_model._getAnomalyClassifier()
    arg_3 = arg_2.getSelf()._knn

    arg_4 = arg_2.getSelf().getParameter('categoryRecencyList')

    arg_5 = [r.ROWID for r in arg_1 if \
      not r.setByUser and r.ROWID in arg_4]

    arg_6 = arg_3._numPatterns
    arg_3.removeIds(arg_5)
    assert arg_3._numPatterns == arg_6 - len(arg_5)


def Func(arg_0):
    """
    Construct a _HTMClassificationRecord based on the current state of the
    htm_prediction_model of this classifier.

    ***This will look into the internals of the model and may depend on the
    SP, TM, and KNNClassifier***
    """
    arg_1 = arg_0.htm_prediction_model
    arg_2 = arg_1._getSPRegion()
    arg_3 = arg_1._getTPRegion()
    arg_4 = arg_3.getSelf()._tfdr

    # Count the number of unpredicted columns
    arg_5 = arg_2.getOutputData("bottomUpOut").nonzero()[0]
    arg_6 = arg_12.in1d(arg_5, arg_0._prevPredictedColumns).sum()
    arg_6 = (arg_0._activeColumnCount - arg_6)/float(arg_0._activeColumnCount)

    arg_7 = arg_2.getParameter('activeOutputCount')
    arg_8 = arg_3.getParameter('cellsPerColumn') * arg_3.getParameter('columnCount')

    arg_9 = arg_12.array([])

    if arg_0._vectorType == 'tpc':
      # Classification Vector: [---TM Cells---]
      arg_9 = arg_12.zeros(arg_8)
      arg_10 = arg_4.getLearnActiveStateT().reshape(arg_8, 1)
      arg_11 = arg_12.where(arg_10 > 0)[0]
      if arg_11.shape[0] > 0:
        arg_9[arg_12.array(arg_11, arg_14=arg_12.uint16)] = 1
    elif arg_0._vectorType == 'sp_tpe':
      # Classification Vecotr: [---SP---|---(TM-SP)----]
      arg_9 = arg_12.zeros(arg_7+arg_7)
      if arg_5.shape[0] > 0:
        arg_9[arg_5] = 1.0

      arg_16 = arg_12.setdiff1d(arg_0._prevPredictedColumns, arg_5)
      if arg_16.shape[0] > 0:
        arg_17 = ( arg_12.array(arg_16, arg_14=arg_12.uint16) +
          arg_7 )
        arg_9[arg_17] = 1.0
    else:
      raise TypeError("Classification vector type must be either 'tpc' or"
        " 'sp_tpe', current value is %s" % (arg_0._vectorType))

    # Store the state for next time step
    arg_18 = len(arg_0._prevPredictedColumns)
    arg_19 = arg_3.getOutputData("topDownOut").nonzero()[0]
    arg_0._prevPredictedColumns = copy.deepcopy(arg_19)

    if arg_0._anomalyVectorLength is None:
      arg_0._anomalyVectorLength = len(arg_9)

    arg_22 = _CLAClassificationRecord(
      ROWID=int(arg_1.getParameter('__numRunCalls') - 1), #__numRunCalls called
        #at beginning of model.run
      anomalyScore=arg_6,
      anomalyVector=arg_9.nonzero()[0].tolist(),
      anomalyLabel=[]
    )
    return arg_22


def Func(arg_0, arg_1):
    """
    Sets the autoDetectWaitRecords.
    """
    if not isinstance(arg_1, int):
      raise HTMPredictionModelInvalidArgument("Invalid argument type \'%s\'. WaitRecord "
        "must be a number." % (type(arg_1)))

    if len(arg_0.saved_states) > 0 and arg_1 < arg_0.saved_states[0].ROWID:
      raise HTMPredictionModelInvalidArgument("Invalid value. autoDetectWaitRecord value "
        "must be valid record within output stream. Current minimum ROWID in "
        "output stream is %d." % (arg_0.saved_states[0].ROWID))

    arg_0._autoDetectWaitRecords = arg_1

    # Update all the states in the classifier's cache
    for arg_3 in arg_0.saved_states:
      arg_0._updateState(arg_3)


def Func(arg_0, arg_1, arg_2):
    """
    Run one iteration, profiling it if requested.

    :param inputs: (dict) mapping region input names to numpy.array values
    :param outputs: (dict) mapping region output names to numpy.arrays that
           should be populated with output values by this method
    """

    # Uncomment this to find out who is generating divide by 0, or other numpy warnings
    # numpy.seterr(divide='raise', invalid='raise', over='raise')

    # Modify this line to turn on profiling for a given node. The results file
    #  ('hotshot.stats') will be sensed and printed out by the vision framework's
    #  RunInference.py script at the end of inference.
    # Also uncomment the hotshot import at the top of this file.
    if False and arg_0.learningMode \
        and arg_0._iterations > 0 and arg_0._iterations <= 10:

      import hotshot
      if arg_0._iterations == 10:
        print "\n  Collecting and sorting internal node profiling stats generated by hotshot..."
        arg_3 = hotshot.stats.load("hotshot.stats")
        arg_3.strip_dirs()
        arg_3.sort_stats('time', 'calls')
        arg_3.print_stats()

      # The guts of the Func are contained in the _Func() call so that we
      # can profile it if requested.
      if arg_0._profileObj is None:
        print "\n  Preparing to capture profile using hotshot..."
        if os.path.exists('hotshot.stats'):
          # There is an old hotshot stats profile left over, remove it.
          os.remove('hotshot.stats')
        arg_0._profileObj = hotshot.Profile("hotshot.stats", 1, 1)
                                          # filename, lineevents, linetimings
      arg_0._profileObj.runcall(arg_0._Func, *[arg_1, arg_2])
    else:
      arg_0._Func(arg_1, arg_2)


def Func(arg_0, arg_1, arg_2):
    """
    Run one iteration of SPRegion's compute
    """

    #if self.topDownMode and (not 'topDownIn' in inputs):
    #  raise RuntimeError("The input topDownIn must be linked in if "
    #                     "topDownMode is True")

    if arg_0._sfdr is None:
      raise RuntimeError("Spatial pooler has not been initialized")

    if not arg_0.topDownMode:
      #
      # BOTTOM-UP compute
      #

      arg_0._iterations += 1

      # Get our inputs into numpy arrays
      arg_3 = arg_1['bottomUpIn']

      arg_4 = False
      if 'resetIn' in arg_1:
        assert len(arg_1['resetIn']) == 1
        arg_4 = arg_1['resetIn'][0] != 0

      # Perform inference and/or learning
      arg_5 = arg_0._doBottomUpCompute(
        rfInput = arg_3.reshape((1,arg_3.size)),
        arg_4 = arg_4
        )

      arg_2['bottomUpOut'][:] = arg_5.flat

    else:
      #
      # TOP-DOWN inference
      #

      arg_6 = arg_1.get('topDownIn',None)
      arg_7, arg_8 = arg_0._doTopDownInfer(arg_6)
      arg_2['spatialTopDownOut'][:] = arg_7
      if arg_8 is not None:
        arg_2['temporalTopDownOut'][:] = arg_8

    # OBSOLETE
    arg_2['anomalyScore'][:] = 0


def Func(arg_0):
    """Figure out whether reset, sequenceId,
    both or neither are present in the data.
    Compute once instead of every time.

    Taken from filesource.py"""

    arg_1 = arg_0.resetFieldName is not None
    arg_2 = arg_0.sequenceIdFieldName is not None

    if arg_1 and not arg_2:
      arg_0._sequenceInfoType = arg_0.SEQUENCEINFO_RESET_ONLY
      arg_0._prevSequenceId = 0
    elif not arg_1 and arg_2:
      arg_0._sequenceInfoType = arg_0.SEQUENCEINFO_SEQUENCEID_ONLY
      arg_0._prevSequenceId = None
    elif arg_1 and arg_2:
      arg_0._sequenceInfoType = arg_0.SEQUENCEINFO_BOTH
    else:
      arg_0._sequenceInfoType = arg_0.SEQUENCEINFO_NONE


def Func(arg_0, arg_1=None, arg_2={}):
  """
  Get the default arguments from the function and assign as instance vars.

  Return a list of 3-tuples with (name, description, defaultValue) for each
    argument to the function.

  Assigns all arguments to the function as instance variables of TMRegion.
  If the argument was not provided, uses the default value.

  Pops any values from kwargs that go to the function.
  """
  # Get the name, description, and default value for each argument
  arg_3 = getArgumentDescriptions(arg_0)
  arg_3 = arg_3[1:]  # Remove 'self'

  # Get the names of the parameters to our own constructor and remove them
  # Check for _originial_init first, because if LockAttributesMixin is used,
  #  __init__'s signature will be just (self, *args, **kw), but
  #  _original_init is created with the original signature
  #init = getattr(self, '_original_init', self.__init__)
  arg_4 = TMRegion.__init__
  arg_5 = [t[0] for t in getArgumentDescriptions(arg_4)]
  # Also remove a few other names that aren't in our constructor but are
  #  computed automatically (e.g. numberOfCols for the TM)
  arg_5 += [
    'numberOfCols',    # TM
  ]
  for arg_6 in arg_3[:]:
    if arg_6[0] in arg_5:
      arg_3.remove(arg_6)

  # Build the dictionary of arguments
  if arg_1:
    for arg_6 in arg_3:
      arg_7 = arg_6[0]
      if arg_7 in arg_2:
        # Argument was provided
        arg_8 = arg_2.pop(arg_7)
      else:
        # Argument was not provided; use the default value if there is one, and
        #  raise an exception otherwise
        if len(arg_6) == 2:
          # No default value
          raise TypeError("Must provide '%s'" % arg_7)
        arg_8 = arg_6[2]
      # Set as an instance variable if 'self' was passed in
      setattr(arg_1, arg_7, arg_8)

  return arg_3


def Func(arg_0, arg_1, arg_2):
    """
    Run one iteration of TMRegion's compute
    """

    #if self.topDownMode and (not 'topDownIn' in inputs):
    # raise RuntimeError("The input topDownIn must be linked in if "
    #                    "topDownMode is True")

    if arg_0._tfdr is None:
      raise RuntimeError("TM has not been initialized")

    # Conditional compute break
    arg_0._conditionalBreak()

    arg_0._iterations += 1

    # Get our inputs as numpy array
    arg_3 = arg_1['bottomUpIn']

    # Handle reset signal
    arg_4 = False
    if 'resetIn' in arg_1:
      assert len(arg_1['resetIn']) == 1
      if arg_1['resetIn'][0] != 0:
        arg_0._tfdr.reset()
        arg_0._sequencePos = 0  # Position within the current sequence

    if arg_0.computePredictedActiveCellIndices:
      arg_6 = arg_0._tfdr.getPredictedState().reshape(-1).astype('float32')

    if arg_0.anomalyMode:
      arg_7 = arg_0._tfdr.topDownCompute().copy().nonzero()[0]

    # Perform inference and/or learning
    arg_8 = arg_0._tfdr.compute(arg_3, arg_0.learningMode, arg_0.inferenceMode)
    arg_0._sequencePos += 1

    # OR'ing together the cells in each column?
    if arg_0.orColumnOutputs:
      arg_8= arg_8.reshape(arg_0.columnCount,
                                     arg_0.cellsPerColumn).max(axis=1)

    # Direct logging of non-zero TM outputs
    if arg_0._fpLogTPOutput:
      arg_9 = arg_8.reshape(-1)
      arg_10 = arg_8.nonzero()[0]
      arg_11 = " ".join(["%d" % int(token) for token in arg_10])
      print >>arg_0._fpLogTPOutput, arg_9.size, arg_11

    # Write the bottom up out to our node outputs
    arg_2['bottomUpOut'][:] = arg_8.flat

    if arg_0.topDownMode:
      # Top-down compute
      arg_2['topDownOut'][:] = arg_0._tfdr.topDownCompute().copy()

    # Set output for use with anomaly classification region if in anomalyMode
    if arg_0.anomalyMode:
      arg_12 = arg_0._tfdr.getLearnActiveStateT()
      arg_13 = arg_12.shape[0] * arg_12.shape[1]
      arg_2['lrnActiveStateT'][:] = arg_12.reshape(arg_13)

      arg_14 = arg_3.nonzero()[0]
      arg_2['anomalyScore'][:] = anomaly.computeRawAnomalyScore(
        arg_14, arg_7)

    if arg_0.computePredictedActiveCellIndices:
      # Reshape so we are dealing with 1D arrays
      arg_15 = arg_0._tfdr._getActiveState().reshape(-1).astype('float32')
      arg_16 = numpy.where(arg_15 != 0)[0]
      arg_17= numpy.where(arg_6 != 0)[0]
      arg_18 = numpy.intersect1d(arg_16, arg_17)
      arg_2["activeCells"].fill(0)
      arg_2["activeCells"][arg_16] = 1
      arg_2["predictedActiveCells"].fill(0)
      arg_2["predictedActiveCells"][arg_18] = 1


def Func(arg_0):
    """
    Perform an internal optimization step that speeds up inference if we know
    learning will not be performed anymore. This call may, for example, remove
    all potential inputs to each column.
    """
    if arg_0._tfdr is None:
      raise RuntimeError("Temporal memory has not been initialized")

    if hasattr(arg_0._tfdr, 'Func'):
      arg_0.resetSequenceStates()
      arg_0._tfdr.Func()


def Func(arg_0, arg_1):
  """Computes the raw anomaly score.

  The raw anomaly score is the fraction of active columns not predicted.

  :param activeColumns: array of active column indices
  :param prevPredictedColumns: array of columns indices predicted in prev step
  :returns: anomaly score 0..1 (float)
  """
  arg_2 = len(arg_0)
  if arg_2 > 0:
    # Test whether each element of a 1-D array is also present in a second
    # array. Sum to get the total # of columns that are active and were
    # predicted.
    arg_3 = numpy.in1d(arg_0, arg_1).sum()
    # Get the percent of active columns that were NOT predicted, that is
    # our anomaly score.
    arg_3 = (arg_2 - arg_3) / float(arg_2)
  else:
    # There are no active columns.
    arg_3 = 0.0

  return arg_3


def Func(arg_0, arg_1, arg_2,
              arg_3=None, arg_4=None):
    """Compute the anomaly score as the percent of active columns not predicted.

    :param activeColumns: array of active column indices
    :param predictedColumns: array of columns indices predicted in this step
                             (used for anomaly in step T+1)
    :param inputValue: (optional) value of current input to encoders
                                  (eg "cat" for category encoder)
                                  (used in anomaly-likelihood)
    :param timestamp: (optional) date timestamp when the sample occured
                                 (used in anomaly-likelihood)
    :returns: the Funcd anomaly score; float 0..1
    """
    # Start by computing the raw anomaly score.
    arg_5 = FuncRawAnomalyScore(arg_1, arg_2)

    # Compute final anomaly based on selected mode.
    if arg_0._mode == Anomaly.MODE_PURE:
      arg_6 = arg_5
    elif arg_0._mode == Anomaly.MODE_LIKELIHOOD:
      if arg_3 is None:
        raise ValueError("Selected anomaly mode 'Anomaly.MODE_LIKELIHOOD' "
                 "requires 'inputValue' as parameter to Func() method. ")

      arg_7 = arg_0._likelihood.anomalyProbability(
          arg_3, arg_5, arg_4)
      # low likelihood -> hi anomaly
      arg_6 = 1 - arg_7
    elif arg_0._mode == Anomaly.MODE_WEIGHTED:
      arg_7 = arg_0._likelihood.anomalyProbability(
          arg_3, arg_5, arg_4)
      arg_6 = arg_5 * (1 - arg_7)

    # Last, do moving-average if windowSize was specified.
    if arg_0._movingAverage is not None:
      arg_6 = arg_0._movingAverage.next(arg_6)

    # apply binary discretization if required
    if arg_0._binaryThreshold is not None:
      if arg_6 >= arg_0._binaryThreshold:
        arg_6 = 1.0
      else:
        arg_6 = 0.0

    return arg_6


def Func(arg_0, arg_1, arg_2=111, arg_3=None, arg_4=None, arg_5=None,
                 arg_6="auto", arg_7="nearest", arg_8=None):
    """ Adds an image to the plot's figure.

    @param data a 2D array. See matplotlib.Axes.imshow documentation.
    @param position A 3-digit number. The first two digits define a 2D grid
            where subplots may be added. The final digit specifies the nth grid
            location for the added subplot
    @param xlabel text to be displayed on the x-axis
    @param ylabel text to be displayed on the y-axis
    @param cmap color map used in the rendering
    @param aspect how aspect ratio is handled during resize
    @param interpolation interpolation method
    """
    if arg_5 is None:
      # The default colormodel is an ugly blue-red model.
      arg_5 = cm.Greys

    arg_9 = arg_0._addBase(arg_2, arg_3=arg_3, arg_4=arg_4)
    arg_9.imshow(arg_1, arg_5=arg_5, arg_6=arg_6, arg_7=arg_7)

    if arg_0._show:
      plt.draw()

    if arg_8 is not None:
      if not os.path.exists("log"):
        os.mkdir("log")
      plt.savefig("log/{name}.png".format(arg_8=arg_8), bbox_inches="tight",
                  figsize=(8, 6), dpi=400)


def Func(arg_0, arg_1, arg_2=None, arg_3=None):
    """ Adds a subplot to the plot's figure at specified position.

    @param position A 3-digit number. The first two digits define a 2D grid
            where subplots may be added. The final digit specifies the nth grid
            location for the added subplot
    @param xlabel text to be displayed on the x-axis
    @param ylabel text to be displayed on the y-axis
    @returns (matplotlib.Axes) Axes instance
    """
    arg_4 = arg_0._fig.add_subplot(arg_1)
    arg_4.set_xlabel(arg_2)
    arg_4.set_ylabel(arg_3)
    return arg_4


def Func():
  """
  Get version from local file.
  """
  with open(os.path.join(REPO_DIR, "VERSION"), "r") as versionFile:
    return versionFile.read().strip()


def Func():
  """
  Make an attempt to determine if a pre-release version of nupic.bindings is
  installed already.

  @return: boolean
  """
  try:
    arg_0 = pkg_resources.get_distribution("nupic.bindings")
    if pkg_resources.parse_version(arg_0.version).is_prerelease:
      # A pre-release dev version of nupic.bindings is installed.
      return True
  except pkg_resources.DistributionNotFound:
    pass  # Silently ignore.  The absence of nupic.bindings will be handled by
    # setuptools by default

  return False


def Func():
  """
  Read the requirements.txt file and parse into requirements for setup's
  install_requirements option.
  """
  arg_0 = os.path.join(REPO_DIR, "requirements.txt")
  arg_1 = parse_file(arg_0)

  if nupicBindingsPrereleaseInstalled():
    # User has a pre-release version of nupic.bindings installed, which is only
    # possible if the user installed and built nupic.bindings from source and
    # it is up to the user to decide when to update nupic.bindings.  We'll
    # quietly remove the entry in requirements.txt so as to not conflate the
    # two.
    arg_1 = [req for req in arg_1 if "nupic.bindings" not in req]

  return arg_1


def Func(arg_0, arg_1,
                              arg_2=None, arg_3=None,
                              arg_4=False):
  """ Generates the string representation of a MetricSpec object, and returns
  the metric key associated with the metric.

  Parameters:
  -----------------------------------------------------------------------
  inferenceElement:
    An InferenceElement value that indicates which part of the inference this
    metric is computed on

  metric:
    The type of the metric being computed (e.g. aae, avg_error)

  params:
    A dictionary of parameters for the metric. The keys are the parameter names
    and the values should be the parameter values (e.g. window=200)

  field:
    The name of the field for which this metric is being computed

  returnLabel:
    If True, returns the label of the MetricSpec that was generated
  """

  arg_5 = dict(arg_1=arg_1,
                        arg_3=arg_3,
                        arg_2=arg_2,
                        arg_0=arg_0)

  arg_6 = "MetricSpec(%s)" % \
    ', '.join(['%s=%r' % (item[0],item[1])
              for item in arg_5.iteritems()])

  if not arg_4:
    return arg_6

  arg_7 = MetricSpec(**arg_5)
  arg_8 = arg_7.getLabel()
  return arg_6, arg_8


def Func(arg_0, arg_1,
                              arg_2):
  """ Generates a file by applying token replacements to the given template
  file

  templateFileName:
                  A list of template file names; these files are assumed to be in
                  the same directory as the running experiment_generator.py script.
                  ExpGenerator will perform the substitution and concanetate
                  the files in the order they are specified

  outputFilePath: Absolute path of the output file

  replacementDict:
                  A dictionary of token/replacement pairs
  """

  # Find out where we're running from so we know where to find templates
  arg_3 = os.path.dirname(__file__)
  arg_4 = open(arg_1, "w")
  arg_5 = []
  arg_6 = []

  arg_7 = True
  for arg_8 in arg_0:
    # Separate lines from each file by two blank lines.
    if not arg_7:
      arg_6.extend([os.linesep]*2)
    arg_7 = False

    arg_9 = os.path.join(arg_3, arg_8)
    arg_10 = open(arg_9)
    arg_6.extend(arg_10.readlines())
    arg_10.close()

  print "Writing ", len(arg_6), "lines..."

  for arg_11 in arg_6:
    arg_12 = arg_11

    # Enumerate through each key in replacementDict and replace with value
    for arg_13, arg_14 in arg_2.iteritems():
      if arg_14 is None:
        arg_14 = "None"
      arg_12 = re.sub(arg_13, arg_14, arg_12)
    arg_4.write(arg_12)
  arg_4.close()


def Func():
  """
  Returns the experiment description schema. This implementation loads it in
  from file experimentDescriptionSchema.json.

  Parameters:
  --------------------------------------------------------------------------
  Returns:    returns a dict representing the experiment description schema.
  """
  arg_0 = os.path.dirname(os.path.abspath(__file__))
  arg_1 = os.path.join(arg_0, "experimentDescriptionSchema.json")
  return json.loads(open(arg_1, 'r').read())


def Func(arg_0):
  """Generates the non-default metrics specified by the expGenerator params """
  arg_1 = {'properties': {}}

  arg_2 = []
  for arg_3 in arg_0['metrics']:

    for arg_4 in arg_1['properties'].keys():
      _getPropertyValue(arg_1, arg_4, arg_3)

    arg_5, arg_6 = _generateMetricSpecString(
                                          field=arg_3['field'],
                                          arg_3=arg_3['metric'],
                                          params=arg_3['params'],
                                          inferenceElement=\
                                                    arg_3['inferenceElement'],
                                          returnLabel=True)
    if arg_3['logged']:
      arg_0['loggedMetrics'].append(arg_6)

    arg_2.append(arg_5)

  return arg_2


def Func(arg_0, arg_1):
  """ Generates the token substitutions related to the predicted field
  and the supplemental arguments for prediction
  """
  arg_2 = arg_0['inferenceType']
  arg_3 = arg_0.get('inferenceArgs', None)
  arg_4 = {}
  arg_5 = _getPredictedField(arg_0)[0]

  if arg_2 in (InferenceType.TemporalNextStep,
                       InferenceType.TemporalAnomaly):
    assert arg_5,  "Inference Type '%s' needs a predictedField "\
                            "specified in the inferenceArgs dictionary"\
                            % arg_2

  if arg_3:
    # If we will be using a dynamically created predictionSteps, plug in that
    #  variable name in place of the constant scalar value
    if arg_0['dynamicPredictionSteps']:
      arg_6 = copy.deepcopy(arg_3)
      arg_6['predictionSteps'] = '$REPLACE_ME'
      arg_4 = pprint.pformat(arg_6)
      arg_4 = arg_4.replace("'$REPLACE_ME'",
                                                        '[predictionSteps]')
    else:
      arg_4 = pprint.pformat(arg_3)

  arg_1['\$INFERENCE_ARGS'] = arg_4

  arg_1['\$PREDICTION_FIELD'] = arg_5


def Func(arg_0):
  """ Parses, validates, and executes command-line options;

  On success: Performs requested operation and exits program normally

  On Error:   Dumps exception/error info in JSON format to stdout and exits the
              program with non-zero status.
  """

  # -----------------------------------------------------------------
  # Parse command line options
  #
  arg_1 = OptionParser()
  arg_1.set_usage("%prog [options] --description='{json object with args}'\n" + \
                   "%prog [options] --descriptionFromFile='{filename}'\n" + \
                   "%prog [options] --showSchema")

  arg_1.add_option("--description", dest = "description",
    help = "Tells ExpGenerator to generate an experiment description.py and " \
           "permutations.py file using the given JSON formatted experiment "\
           "description string.")

  arg_1.add_option("--descriptionFromFile", dest = 'descriptionFromFile',
    help = "Tells ExpGenerator to open the given filename and use it's " \
           "contents as the JSON formatted experiment description.")

  arg_1.add_option("--claDescriptionTemplateFile",
    dest = 'claDescriptionTemplateFile',
    default = 'claDescriptionTemplate.tpl',
    help = "The file containing the template description file for " \
           " ExpGenerator [default: %default]")

  arg_1.add_option("--showSchema",
                    action="store_true", dest="showSchema",
                    help="Prints the JSON schemas for the --description arg.")

  arg_1.add_option("--version", dest = 'version', default='v2',
    help = "Generate the permutations file for this version of hypersearch."
            " Possible choices are 'v1' and 'v2' [default: %default].")

  arg_1.add_option("--outDir",
                    dest = "outDir", default=None,
                    help = "Where to generate experiment. If not specified, " \
                           "then a temp directory will be created"
                    )
  (arg_2, arg_3) = arg_1.parse_args(arg_0)

  #print("OPTIONS=%s" % (str(options)))

  # -----------------------------------------------------------------
  # Check for unprocessed args
  #
  if len(arg_3) > 0:
    raise _InvalidCommandArgException(
      _makeUsageErrorStr("Unexpected command-line args: <%s>" % \
                         (' '.join(arg_3),), arg_1.get_usage()))

  # -----------------------------------------------------------------
  # Check for use of mutually-exclusive options
  #
  arg_4 = filter(lambda x: getattr(arg_2, x) != None,
                         ('description', 'showSchema'))
  if len(arg_4) > 1:
    raise _InvalidCommandArgException(
      _makeUsageErrorStr(("The specified command options are " + \
                          "mutually-exclusive: %s") % (arg_4,),
                          arg_1.get_usage()))

  # -----------------------------------------------------------------
  # Process requests
  #
  if arg_2.showSchema:
    _handleShowSchemaOption()

  elif arg_2.description:
    _handleDescriptionOption(arg_2.description, arg_2.outDir,
           arg_1.get_usage(), hsVersion=arg_2.version,
           claDescriptionTemplateFile = arg_2.claDescriptionTemplateFile)

  elif arg_2.descriptionFromFile:
    _handleDescriptionFromFileOption(arg_2.descriptionFromFile,
          arg_2.outDir, arg_1.get_usage(), hsVersion=arg_2.version,
          claDescriptionTemplateFile = arg_2.claDescriptionTemplateFile)

  else:
    raise _InvalidCommandArgException(
      _makeUsageErrorStr("Error in validating command options. No option "
                         "provided:\n", arg_1.get_usage()))


def Func(arg_0):
  """
  Parses a textual datetime format and return a Python datetime object.

  The supported format is: ``yyyy-mm-dd h:m:s.ms``

  The time component is optional.

  - hours are 00..23 (no AM/PM)
  - minutes are 00..59
  - seconds are 00..59
  - micro-seconds are 000000..999999

  :param s: (string) input time text
  :return: (datetime.datetime)
  """
  arg_0 = arg_0.strip()
  for arg_1 in DATETIME_FORMATS:
    try:
      return datetime.datetime.strptime(arg_0, arg_1)
    except ValueError:
      pass
  raise ValueError('The provided timestamp %s is malformed. The supported '
                   'formats are: [%s]' % (arg_0, ', '.join(DATETIME_FORMATS)))


def Func(arg_0, arg_1):
  """
  Translate an index into coordinates, using the given coordinate system.

  Similar to ``numpy.unravel_index``.

  :param index: (int) The index of the point. The coordinates are expressed as a
         single index by using the dimensions as a mixed radix definition. For
         example, in dimensions 42x10, the point [1, 4] is index
         1*420 + 4*10 = 460.

  :param dimensions (list of ints) The coordinate system.

  :returns: (list) of coordinates of length ``len(dimensions)``.
  """
  arg_2 = [0] * len(arg_1)

  arg_3 = arg_0
  for arg_4 in xrange(len(arg_1) - 1, 0, -1):
    arg_2[arg_4] = arg_3 % arg_1[arg_4]
    arg_3 = arg_3 / arg_1[arg_4]

  arg_2[0] = arg_3

  return arg_2


def Func(arg_0, arg_1):
  """
  Translate coordinates into an index, using the given coordinate system.

  Similar to ``numpy.ravel_multi_index``.

  :param coordinates: (list of ints) A list of coordinates of length
         ``dimensions.size()``.

  :param dimensions: (list of ints) The coordinate system.

  :returns: (int) The index of the point. The coordinates are expressed as a
            single index by using the dimensions as a mixed radix definition.
            For example, in dimensions 42x10, the point [1, 4] is index
            1*420 + 4*10 = 460.
  """
  arg_2 = 0
  for arg_3, arg_4 in enumerate(arg_1):
    arg_2 *= arg_4
    arg_2 += arg_0[arg_3]

  return arg_2


def Func(arg_0, arg_1, arg_2):
  """
  Get the points in the Func of a point.

  A point's Func is the n-dimensional hypercube with sides ranging
  [center - radius, center + radius], inclusive. For example, if there are two
  dimensions and the radius is 3, the Func is 6x6. Neighborhoods are
  truncated when they are near an edge.

  This is designed to be fast. In C++ it's fastest to iterate through neighbors
  one by one, calculating them on-demand rather than creating a list of them.
  But in Python it's faster to build up the whole list in batch via a few calls
  to C code rather than calculating them on-demand with lots of calls to Python
  code.

  :param centerIndex: (int) The index of the point. The coordinates are
         expressed as a single index by using the dimensions as a mixed radix
         definition. For example, in dimensions 42x10, the point [1, 4] is index
         1*420 + 4*10 = 460.

  :param radius: (int) The radius of this Func about the
         ``centerIndex``.

  :param dimensions: (indexable sequence) The dimensions of the world outside
         this Func.

  :returns: (numpy array) The points in the Func, including
            ``centerIndex``.
  """
  arg_3 = coordinatesFromIndex(arg_0, arg_2)

  arg_4 = []
  for arg_5, arg_6 in enumerate(arg_2):
    arg_7 = max(0, arg_3[arg_5] - arg_1)
    arg_8 = min(arg_6 - 1, arg_3[arg_5] + arg_1)
    arg_4.append(xrange(arg_7, arg_8 + 1))

  arg_9 = numpy.array(list(itertools.product(*arg_4)))
  return numpy.ravel_multi_index(arg_9.T, arg_2)


def Func(arg_0, arg_1):
    """
    Returns coordinates around given coordinate, within given radius.
    Includes given coordinate.

    @param coordinate (numpy.array) N-dimensional integer coordinate
    @param radius (int) Radius around `coordinate`

    @return (numpy.array) List of coordinates
    """
    arg_2 = (xrange(n-arg_1, n+arg_1+1) for n in arg_0.tolist())
    return numpy.array(list(itertools.product(*arg_2)))


def Func(arg_0, arg_1, arg_2):
    """
    Returns the top W coordinates by order.

    @param coordinates (numpy.array) A 2D numpy array, where each element
                                     is a coordinate
    @param w (int) Number of top coordinates to return
    @return (numpy.array) A subset of `coordinates`, containing only the
                          top ones by order
    """
    arg_3 = numpy.array([arg_0._orderForCoordinate(c)
                          for c in arg_1.tolist()])
    arg_4 = numpy.argsort(arg_3)[-arg_2:]
    return arg_1[arg_4]


def Func(arg_0):
    """Hash a coordinate to a 64 bit integer."""
    arg_1 = ",".join(str(v) for v in arg_0)
    # Compute the hash and convert to 64 bit int.
    arg_2 = int(int(hashlib.md5(arg_1).hexdigest(), 16) % (2 ** 64))
    return arg_2


def Func(arg_0, arg_1, arg_2):
    """
    Maps the coordinate to a bit in the SDR.

    @param coordinate (numpy.array) Coordinate
    @param n (int) The number of available bits in the SDR
    @return (int) The index to a bit in the SDR
    """
    arg_3 = arg_0._hashCoordinate(arg_1)
    arg_4 = Random(arg_3)
    return arg_4.getUInt32(arg_2)


def Func(arg_0, arg_1):
  """
  Function for running binary search on a sorted list.

  :param arr: (list) a sorted list of integers to search
  :param val: (int)  a integer to search for in the sorted array
  :returns: (int) the index of the element if it is found and -1 otherwise.
  """
  arg_2 = bisect_left(arg_0, arg_1)
  if arg_2 != len(arg_0) and arg_0[arg_2] == arg_1:
    return arg_2
  return -1


def Func(arg_0, arg_1):
    """
    Adds a new segment on a cell.

    :param cell: (int) Cell index
    :returns: (int) New segment index
    """
    arg_2 = arg_0._cells[arg_1]

    if len(arg_0._freeFlatIdxs) > 0:
      arg_3 = arg_0._freeFlatIdxs.pop()
    else:
      arg_3 = arg_0._nextFlatIdx
      arg_0._segmentForFlatIdx.append(None)
      arg_0._nextFlatIdx += 1

    arg_4 = arg_0._nextSegmentOrdinal
    arg_0._nextSegmentOrdinal += 1

    arg_5 = Segment(arg_1, arg_3, arg_4)
    arg_2._segments.append(arg_5)
    arg_0._segmentForFlatIdx[arg_3] = arg_5

    return arg_5


def Func(arg_0, arg_1):
    """
    Destroys a segment.

    :param segment: (:class:`Segment`) representing the segment to be destroyed.
    """
    # Remove the synapses from all data structures outside this Segment.
    for arg_2 in arg_1._synapses:
      arg_0._removeSynapseFromPresynapticMap(arg_2)
    arg_0._numSynapses -= len(arg_1._synapses)

    # Remove the segment from the cell's list.
    arg_3 = arg_0._cells[arg_1.cell]._segments
    arg_4 = arg_3.index(arg_1)
    del arg_3[arg_4]

    # Free the flatIdx and remove the final reference so the Segment can be
    # garbage-collected.
    arg_0._freeFlatIdxs.append(arg_1.flatIdx)
    arg_0._segmentForFlatIdx[arg_1.flatIdx] = None


def Func(arg_0, arg_1, arg_2, arg_3):
    """
    Creates a new synapse on a segment.

    :param segment: (:class:`Segment`) Segment object for synapse to be synapsed
           to.
    :param presynapticCell: (int) Source cell index.
    :param permanence: (float) Initial permanence of synapse.
    :returns: (:class:`Synapse`) created synapse
    """
    arg_4 = len(arg_1._synapses)
    arg_5 = Synapse(arg_1, arg_2, arg_3,
                      arg_0._nextSynapseOrdinal)
    arg_0._nextSynapseOrdinal += 1
    arg_1._synapses.add(arg_5)

    arg_0._synapsesForPresynapticCell[arg_2].add(arg_5)

    arg_0._numSynapses += 1

    return arg_5


def Func(arg_0, arg_1):
    """
    Destroys a synapse.

    :param synapse: (:class:`Synapse`) synapse to destroy
    """

    arg_0._numSynapses -= 1

    arg_0._removeSynapseFromPresynapticMap(arg_1)

    arg_1.segment._synapses.remove(arg_1)


def Func(arg_0, arg_1, arg_2):
    """
    Compute each segment's number of active synapses for a given input.
    In the returned lists, a segment's active synapse count is stored at index
    ``segment.flatIdx``.

    :param activePresynapticCells: (iter) Active cells.
    :param connectedPermanence: (float) Permanence threshold for a synapse to be
           considered connected

    :returns: (tuple) (``numActiveConnectedSynapsesForSegment`` [list],
                      ``numActivePotentialSynapsesForSegment`` [list])
    """

    arg_3 = [0] * arg_0._nextFlatIdx
    arg_4 = [0] * arg_0._nextFlatIdx

    arg_5 = arg_2 - EPSILON

    for arg_6 in arg_1:
      for arg_7 in arg_0._synapsesForPresynapticCell[arg_6]:
        arg_8 = arg_7.segment.flatIdx
        arg_4[arg_8] += 1
        if arg_7.permanence > arg_5:
          arg_3[arg_8] += 1

    return (arg_3,
            arg_4)


def Func(arg_0, arg_1=None):
    """
    Returns the number of segments.

    :param cell: (int) Optional parameter to get the number of segments on a
           cell.
    :returns: (int) Number of segments on all cells if cell is not specified, or
              on a specific specified cell
    """
    if arg_1 is not None:
      return len(arg_0._cells[arg_1]._segments)

    return arg_0._nextFlatIdx - len(arg_0._freeFlatIdxs)


def Func(arg_0, arg_1):
    """
    Reads deserialized data from proto object

    :param proto: (DynamicStructBuilder) Proto object

    :returns: (:class:`Connections`) instance
    """
    #pylint: disable=W0212
    arg_2 = arg_1.cells
    arg_3 = arg_0(len(arg_2))

    for arg_4, arg_5 in enumerate(arg_2):
      arg_5 = arg_2[arg_4]
      arg_6 = arg_5.segments
      arg_3._cells[arg_4] = CellData()
      arg_8 = arg_3._cells[arg_4]._segments

      for arg_9, arg_10 in enumerate(arg_6):
        arg_11 = Segment(arg_4, arg_3._nextFlatIdx,
                          arg_3._nextSegmentOrdinal)

        arg_8.append(arg_11)
        arg_3._segmentForFlatIdx.append(arg_11)
        arg_3._nextFlatIdx += 1
        arg_3._nextSegmentOrdinal += 1

        arg_12 = arg_11._synapses
        arg_13 = arg_10.synapses

        for arg_14, arg_15 in enumerate(arg_13):
          arg_16 = arg_15.presynapticCell
          arg_17 = Synapse(arg_11, arg_16, arg_15.permanence,
                            ordinal=arg_3._nextSynapseOrdinal)
          arg_3._nextSynapseOrdinal += 1
          arg_12.add(arg_17)
          arg_3._synapsesForPresynapticCell[arg_16].add(arg_17)

          arg_3._numSynapses += 1

    #pylint: enable=W0212
    return arg_3


def Func(arg_0, arg_1):
    """ Retrieve the requested property as a string. If property does not exist,
    then KeyError will be raised.

    :param prop: (string) name of the property
    :raises: KeyError
    :returns: (string) property value
    """
    if arg_0._properties is None:
      arg_0._readStdConfigFiles()

    # Allow configuration properties to be overridden via environment variables
    arg_2 = os.environ.get("%s%s" % (arg_0.envPropPrefix,
                                        arg_1.replace('.', '_')), None)
    if arg_2 is not None:
      return arg_2

    return arg_0._properties[arg_1]


def Func(arg_0, arg_1):
    """ Retrieve the requested property and return it as a bool. If property
    does not exist, then KeyError will be raised. If the property value is
    neither 0 nor 1, then ValueError will be raised

    :param prop: (string) name of the property
    :raises: KeyError, ValueError
    :returns: (bool) property value
    """

    arg_2 = arg_0.getInt(arg_1)

    if arg_2 not in (0, 1):
      raise ValueError("Expected 0 or 1, but got %r in config property %s" % (
        arg_2, arg_1))

    return bool(arg_2)


def Func(arg_0, arg_1, arg_2):
    """ Set the value of the given configuration property.

    :param prop: (string) name of the property
    :param value: (object) value to Func
    """

    if arg_0._properties is None:
      arg_0._readStdConfigFiles()

    arg_0._properties[arg_1] = str(arg_2)


def Func(arg_0):
    """ Return a Func containing all of the configuration properties

    :returns: (Func) containing all configuration properties.
    """

    if arg_0._properties is None:
      arg_0._readStdConfigFiles()

    # Make a copy so we can update any current values obtained from environment
    #  variables
    arg_1 = Func(arg_0._properties)
    arg_2 = os.environ.keys()
    arg_3 = filter(lambda x: x.startswith(arg_0.envPropPrefix),
                         arg_2)
    for arg_4 in arg_3:
      arg_5 = arg_4[len(arg_0.envPropPrefix):]
      arg_5 = arg_5.replace('_', '.')
      arg_1[arg_5] = os.environ[arg_4]

    return arg_1


def Func(arg_0, arg_1, arg_2=None):
    """ Parse the given XML file and store all properties it describes.

    :param filename: (string) name of XML file to parse (no path)
    :param path: (string) path of the XML file. If None, then use the standard
                  configuration search path.
    """
    arg_3 = arg_0._Func(arg_1, arg_2)

    # Create properties dict if necessary
    if arg_0._properties is None:
      arg_0._properties = dict()

    for arg_5 in arg_3:
      if 'value' in arg_3[arg_5]:
        arg_0._properties[arg_5] = arg_3[arg_5]['value']


def Func(arg_0):
    """ Return the list of paths to search for configuration files.

    :returns: (list) of paths
    """
    arg_1 = []
    if arg_0._configPaths is not None:
      return arg_0._configPaths

    else:
      if 'NTA_CONF_PATH' in os.environ:
        arg_2 = os.environ['NTA_CONF_PATH']
        # Return as a list of paths
        arg_1 = arg_2.split(os.pathsep)

      return arg_1


def Func(arg_0=100, arg_1=500, arg_2=50):
  """
  Generate a list of random sparse distributed vectors.  This is used to generate
  training vectors to the spatial or temporal learner and to compare the predicted
  output against.

  It generates a list of 'numVectors' elements, each element has length 'length'
  and has a total of 'activity' bits on.

  Parameters:
  -----------------------------------------------
  numVectors:    the number of vectors to generate
  length:        the length of each row
  activity:      the number of ones to put into each row.

  """

  arg_3 = []
  arg_4 = numpy.zeros(arg_1, dtype='int32')
  arg_5 = range(arg_1)

  for arg_6 in xrange(arg_0):
      arg_4[:] = 0
      arg_4[arg_7.sample(arg_5, arg_2)] = 1
      arg_3.append(arg_4.copy())

  return arg_3


def Func(arg_0=10, arg_1=[5,6,7], arg_2=100):
  """
  Generate a set of simple sequences. The elements of the sequences will be
  integers from 0 to 'nCoinc'-1. The length of each sequence will be
  randomly chosen from the 'seqLength' list.

  Parameters:
  -----------------------------------------------
  nCoinc:      the number of elements available to use in the sequences
  seqLength:   a list of possible sequence lengths. The length of each
               sequence will be randomly chosen from here.
  nSeq:        The number of sequences to generate

  retval:      a list of sequences. Each sequence is itself a list
               containing the coincidence indices for that sequence.
  """

  arg_3 = range(arg_0)
  arg_4  = []

  for arg_5 in xrange(arg_2):
    if max(arg_1) <= arg_0:
      arg_4.append(random.sample(arg_3, random.choice(arg_1)))
    else:
      arg_6 = random.choice(arg_1)
      arg_7 = []
      for arg_8 in xrange(arg_6):
        arg_7.append(random.choice(arg_3))
      arg_4.append(arg_7)

  return arg_4


def Func(arg_0=10, arg_1 = [2,6], arg_2=[5,6,7], arg_3=100):
  """
  Generate a set of hub sequences. These are sequences which contain a hub
  element in the middle. The elements of the sequences will be integers
  from 0 to 'nCoinc'-1. The hub elements will only appear in the middle of
  each sequence. The length of each sequence will be randomly chosen from the
  'seqLength' list.

  Parameters:
  -----------------------------------------------
  nCoinc:        the number of elements available to use in the sequences
  hubs:          which of the elements will be used as hubs.
  seqLength:     a list of possible sequence lengths. The length of each
                        sequence will be randomly chosen from here.
  nSeq:          The number of sequences to generate

  retval:        a list of sequences. Each sequence is itself a list
                containing the coincidence indices for that sequence.
  """

  arg_4 = range(arg_0)
  for arg_5 in arg_1:
    arg_4.remove(arg_5)

  arg_6 = []
  for arg_7 in xrange(arg_3):
    arg_8 = random.choice(arg_2)-1
    arg_9 = random.sample(arg_4,arg_8)
    arg_9.insert(arg_8//2, random.choice(arg_1))
    arg_6.append(arg_9)

  return arg_6


def Func(arg_0=10, arg_1=500, arg_2=50):
  """
  Generate a non overlapping coincidence matrix. This is used to generate random
  inputs to the temporal learner and to compare the predicted output against.

  It generates a matrix of nCoinc rows, each row has length 'length' and has
  a total of 'activity' bits on.

  Parameters:
  -----------------------------------------------
  nCoinc:        the number of rows to generate
  length:        the length of each row
  activity:      the number of ones to put into each row.

  """
  assert arg_0*arg_2<=arg_1, "can't generate non-overlapping coincidences"
  arg_3 = SM32(0, arg_1)
  arg_4 = numpy.zeros(arg_1, dtype='int32')

  for arg_5 in xrange(arg_0):
      arg_4[:] = 0
      arg_4[arg_5*arg_2:(arg_5+1)*arg_2] = 1
      arg_3.addRow(arg_4)

  return arg_3


def Func(arg_0,arg_1):
    """
    Function that compares two spatial pooler instances. Compares the
    static variables between the two poolers to make sure that they are equivalent.

    Parameters
    -----------------------------------------
    SP1 first spatial pooler to be compared

    SP2 second spatial pooler to be compared

    To establish equality, this function does the following:

    1.Compares the connected synapse matrices for each coincidence

    2.Compare the potential synapse matrices for each coincidence

    3.Compare the permanence matrices for each coincidence

    4.Compare the firing boosts between the two poolers.

    5.Compare the duty cycles before and after inhibition for both poolers

    """
    if(len(arg_0._masterConnectedM)!=len(arg_1._masterConnectedM)):
        print "Connected synapse matrices are different sizes"
        return False

    if(len(arg_0._masterPotentialM)!=len(arg_1._masterPotentialM)):
        print "Potential synapse matrices are different sizes"
        return False

    if(len(arg_0._masterPermanenceM)!=len(arg_1._masterPermanenceM)):
        print "Permanence matrices are different sizes"
        return False

    #iterate over cells
    for arg_2 in range(0,len(arg_0._masterConnectedM)):
        #grab the Coincidence Matrices and compare them
        arg_3 = arg_0._masterConnectedM[arg_2]
        arg_4 = arg_1._masterConnectedM[arg_2]
        if(arg_3!=arg_4):
            print "Connected Matrices for cell %d different"  % (arg_2)
            return False
        #grab permanence Matrices and compare them
        arg_5 = arg_0._masterPermanenceM[arg_2];
        arg_6 = arg_1._masterPermanenceM[arg_2];
        if(arg_5!=arg_6):
            print "Permanence Matrices for cell %d different" % (arg_2)
            return False
        #grab the potential connection Matrices and compare them
        arg_7 = arg_0._masterPotentialM[arg_2];
        arg_8 = arg_1._masterPotentialM[arg_2];
        if(arg_7!=arg_8):
            print "Potential Matrices for cell %d different" % (arg_2)
            return False

    #Check firing boosts
    if(not numpy.array_equal(arg_0._firingBoostFactors,arg_1._firingBoostFactors)):
        print "Firing boost factors are different between spatial poolers"
        return False

    #Check duty cycles after inhibiton
    if(not numpy.array_equal(arg_0._dutyCycleAfterInh,arg_1._dutyCycleAfterInh)):
        print "Duty cycles after inhibition are different between spatial poolers"
        return False

    #Check duty cycles before inhibition
    if(not numpy.array_equal(arg_0._dutyCycleBeforeInh,arg_1._dutyCycleBeforeInh)):
        print "Duty cycles before inhibition are different between spatial poolers"
        return False

    print("Spatial Poolers are equivalent")

    return True


def Func(arg_0, arg_1=None):
  """
  Accumulate a list of values 'values' into the frequency counts 'freqCounts',
  and return the updated frequency counts

  For example, if values contained the following: [1,1,3,5,1,3,5], and the initial
  freqCounts was None, then the return value would be:
  [0,3,0,2,0,2]
  which corresponds to how many of each value we saw in the input, i.e. there
  were 0 0's, 3 1's, 0 2's, 2 3's, 0 4's, and 2 5's.

  If freqCounts is not None, the values will be added to the existing counts and
  the length of the frequency Counts will be automatically extended as necessary

  Parameters:
  -----------------------------------------------
  values:         The values to accumulate into the frequency counts
  freqCounts:     Accumulated frequency counts so far, or none
  """

  # How big does our freqCounts vector need to be?
  arg_0 = numpy.array(arg_0)
  arg_2 = arg_0.max() + 1
  if arg_1 is not None:
    arg_2 = max(arg_2, arg_1.size)

  # Where do we accumulate the results?
  if arg_1 is not None:
    if arg_1.size != arg_2:
      arg_3 = numpy.zeros(arg_2, dtype='int32')
      arg_3[0:arg_1.size] = arg_1
    else:
      arg_3 = arg_1
  else:
    arg_3 = numpy.zeros(arg_2, dtype='int32')

  # Accumulate the new values
  for arg_5 in arg_0:
    arg_3[arg_5] += 1

  return arg_3


def Func(arg_0, arg_1):
  """
  Helper function used by averageOnTimePerTimestep. 'durations' is a vector
  which must be the same len as vector. For each "on" in vector, it fills in
  the corresponding element of duration with the duration of that "on" signal
  up until that time

  Parameters:
  -----------------------------------------------
  vector:     vector of output values over time
  durations:  vector same length as 'vector', initialized to 0's.
              This is filled in with the durations of each 'on" signal.

  Example:
  vector:     11100000001100000000011111100000
  durations:  12300000001200000000012345600000
  """

  # Find where the nonzeros are
  arg_2 = numpy.array(arg_0).nonzero()[0]

  # Nothing to do if vector is empty
  if len(arg_2) == 0:
    return

  # Special case of only 1 on bit
  if len(arg_2) == 1:
    arg_1[arg_2[0]] = 1
    return

  # Count the consecutive non-zeros
  arg_3 = arg_2[0]
  arg_4 = 1
  arg_5 = arg_3
  arg_6 = arg_2[-1]
  for arg_7 in arg_2[1:]:
    if arg_7 != arg_3+1:
      # Fill in the durations
      arg_1[arg_5:arg_5+arg_4] = range(1,arg_4+1)
      arg_4       = 1
      arg_5 = arg_7
    else:
      arg_4 += 1
    arg_3 = arg_7

  # Fill in the last one
  arg_1[arg_5:arg_5+arg_4] = range(1,arg_4+1)


def Func(arg_0, arg_1=None):
  """
  Computes the average on-time of the outputs that are on at each time step, and
  then averages this over all time steps.

  This metric is resiliant to the number of outputs that are on at each time
  step. That is, if time step 0 has many more outputs on than time step 100, it
  won't skew the results. This is particularly useful when measuring the
  average on-time of things like the temporal memory output where you might
  have many columns bursting at the start of a sequence - you don't want those
  start of sequence bursts to over-influence the calculated average on-time.

  Parameters:
  -----------------------------------------------
  vectors:          the vectors for which the onTime is calculated. Row 0
                    contains the outputs from time step 0, row 1 from time step
                    1, etc.
  numSamples:       the number of elements for which on-time is calculated.
                    If not specified, then all elements are looked at.

  Returns  (scalar average on-time over all time steps,
            list containing frequency counts of each encountered on-time)

  """

  # Special case given a 1 dimensional vector: it represents a single column
  if arg_0.ndim == 1:
    arg_0.shape = (-1,1)
  arg_3 = len(arg_0)
  arg_4  = len(arg_0[0])

  # How many samples will we look at?
  if arg_1 is not None:
    import pdb; pdb.set_trace()   # Test this....
    arg_5    = numpy.random.randint(0, arg_4, arg_1)
    arg_0 = arg_0[:, arg_5]

  # Fill in each non-zero of vectors with the on-time that that output was
  #  on for.
  arg_6 = numpy.zeros(arg_0.shape, dtype='int32')
  for arg_7 in xrange(arg_0.shape[1]):
    _fillInOnTimes(arg_0[:,arg_7], arg_6[:,arg_7])

  # Compute the average on time for each time step
  arg_8 = arg_0.sum(axis=1)
  arg_8.clip(min=1, max=numpy.inf, out=arg_8)
  arg_9 = arg_6.sum(axis=1, dtype='float64') / arg_8
  arg_10 = arg_9.sum() / (arg_9 > 0).sum()

  # Generate the frequency counts for each duration
  arg_11 = _accumulateFrequencyCounts(arg_9)
  return (arg_10, arg_11)


def Func(arg_0, arg_1=None):
  """
  Returns the average on-time, averaged over all on-time runs.

  Parameters:
  -----------------------------------------------
  vectors:          the vectors for which the onTime is calculated. Row 0
                    contains the outputs from time step 0, row 1 from time step
                    1, etc.
  numSamples:       the number of elements for which on-time is calculated.
                    If not specified, then all elements are looked at.

  Returns:    (scalar average on-time of all outputs,
               list containing frequency counts of each encountered on-time)

  """

  # Special case given a 1 dimensional vector: it represents a single column
  if arg_0.ndim == 1:
    arg_0.shape = (-1,1)
  arg_3 = len(arg_0)
  arg_4  = len(arg_0[0])

  # How many samples will we look at?
  if arg_1 is None:
    arg_1 = arg_4
    arg_5    = range(arg_4)
  else:
    arg_5    = numpy.random.randint(0, arg_4, arg_1)

  # Compute the on-times and accumulate the frequency counts of each on-time
  #  encountered
  arg_6 = 0.0
  arg_7 = None
  arg_8 = 0
  for arg_9 in arg_5:
    (arg_10, arg_11, arg_12) = _listOfOnTimesInVec(arg_0[:,arg_9])
    if arg_10 != 0.0:
      arg_6 += arg_10
      arg_8 += arg_11
      arg_7 = _accumulateFrequencyCounts(arg_12, arg_7)

  # Return the average on time of each element that was on.
  if arg_8 > 0:
    return (arg_6/arg_8, arg_7)
  else:
    return (0.0, arg_7)


def Func(arg_0, arg_1='On-Times Histogram', arg_2='On-Time'):
  """
  This is usually used to display a histogram of the on-times encountered
  in a particular output.

  The freqCounts is a vector containg the frequency counts of each on-time
  (starting at an on-time of 0 and going to an on-time = len(freqCounts)-1)

  The freqCounts are typically generated from the averageOnTimePerTimestep
  or averageOnTime methods of this module.

  Parameters:
  -----------------------------------------------
  freqCounts:       The frequency counts to plot
  title:            Title of the plot

  """

  import pylab
  pylab.ion()
  pylab.figure()
  pylab.bar(numpy.arange(len(arg_0)) - 0.5, arg_0)
  pylab.title(arg_1)
  pylab.xlabel(arg_2)


def Func(arg_0, arg_1=None):
  """
  Returns the percent of the outputs that remain completely stable over
  N time steps.

  Parameters:
  -----------------------------------------------
  vectors:        the vectors for which the stability is calculated
  numSamples:     the number of time steps where stability is counted

  For each window of numSamples, count how many outputs are active during
  the entire window.

  """

  # ----------------------------------------------------------------------
  # Calculate the stability
  arg_2 = len(arg_0)
  arg_3 = arg_1

  # Process each window
  arg_4 = 0
  arg_5 = 0

  for arg_6 in range(0, arg_2-arg_3+1):
    # Count how many elements are active for the entire time
    arg_7 = arg_0[arg_6:arg_6+arg_3]
    arg_8 = arg_7.sum(axis=0)
    arg_9 = (arg_8 == arg_3).sum()

    # Accumulated
    arg_10 = float(arg_9) / arg_7[0].sum()
    print arg_10
    arg_5 += arg_10
    arg_4 += 1

  # Return percent average over all possible windows
  return float(arg_5) / arg_4


def Func(arg_0, arg_1, arg_2=True, arg_3=0):
  """
  Compares the actual input with the predicted input and returns results

  Parameters:
  -----------------------------------------------
  input:          The actual input
  prediction:     the predicted input
  verbosity:        If > 0, print debugging messages
  sparse:         If true, they are in sparse form (list of
                     active indices)

  retval         (foundInInput, totalActiveInInput, missingFromInput,
                            totalActiveInPrediction)
    foundInInput:       The number of predicted active elements that were
                        found in the actual input
    totalActiveInInput: The total number of active elements in the input.
    missingFromInput:   The number of predicted active elements that were not
                        found in the actual input
    totalActiveInPrediction:  The total number of active elements in the prediction

  """

  if arg_2:
    arg_4 = set(arg_0)
    arg_5 = set(arg_1)

  else:
    arg_4 = set(arg_0.nonzero()[0])
    arg_5 = set(arg_1.nonzero()[0])

  arg_6 = len(arg_5)
  arg_7     = len(arg_4)

  arg_8 = len(arg_5.intersection(arg_4))
  arg_9 = len(arg_5.difference(arg_4))
  arg_10 = len(arg_4.difference(arg_5))

  if arg_3 >= 1:
    print "preds. found in input:", arg_8, "out of", arg_6,
    print "; preds. missing from input:", arg_9, "out of", \
              arg_6,
    print "; unexpected active in input:", arg_10, "out of", \
              arg_7

  return (arg_8, arg_7, arg_9,
          arg_6)


def Func(arg_0,
                              arg_1,
                              arg_2=1):
  """
  Generates centre offsets and spread offsets for block-mode based training
  regimes - star, cross, block.

    Parameters:
    -----------------------------------------------
    spaceShape:   The (height, width) of the 2-D space to explore. This
                  sets the number of center-points.
    spreadShape:  The shape (height, width) of the area around each center-point
                  to explore.
    stepSize:     The step size. How big each step is, in pixels. This controls
                  *both* the spacing of the center-points within the block and the
                  points we explore around each center-point
    retval:       (centreOffsets, spreadOffsets)
  """

  from nupic.math.cross import cross
  # =====================================================================
  # Init data structures
  # What is the range on the X and Y offsets of the center points?
  arg_3 = arg_0
  # If the shape is (1,1), special case of just 1 center point
  if arg_3[0] == 1 and arg_3[1] == 1:
    arg_4 = [(0,0)]
  else:
    arg_5 = -1 * (arg_3[1] // 2)
    arg_6 = arg_5 + arg_3[1] - 1
    arg_7 = range(arg_2 * arg_5, arg_2 * arg_6 + 1, arg_2)

    arg_8 = -1 * (arg_3[0] // 2)
    arg_9 = arg_8 + arg_3[0] - 1
    arg_10 = range(arg_2 * arg_8, arg_2 * arg_9 + 1, arg_2)

    arg_4 = list(cross(arg_10, arg_7))

  arg_11 = len(arg_4)
  print "centerOffsets:", arg_4

  # What is the range on the X and Y offsets of the spread points?
  arg_3 = arg_1
  # If the shape is (1,1), special case of no spreading around each center
  #  point
  if arg_3[0] == 1 and arg_3[1] == 1:
    arg_12 = [(0,0)]
  else:
    arg_5 = -1 * (arg_3[1] // 2)
    arg_6 = arg_5 + arg_3[1] - 1
    arg_7 = range(arg_2 * arg_5, arg_2 * arg_6 + 1, arg_2)

    arg_8 = -1 * (arg_3[0] // 2)
    arg_9 = arg_8 + arg_3[0] - 1
    arg_10 = range(arg_2 * arg_8, arg_2 * arg_9 + 1, arg_2)

    arg_12 = list(cross(arg_10, arg_7))

    # Put the (0,0) entry first
    arg_12.remove((0,0))
    arg_12.insert(0, (0,0))

  arg_13 = len(arg_12)
  print "spreadOffsets:", arg_12

  return arg_4, arg_12


def Func(arg_0, arg_1, arg_2=-1):
  """Make a two-dimensional clone map mapping columns to clone master.

  This makes a map that is (numColumnsHigh, numColumnsWide) big that can
  be used to figure out which clone master to use for each column.  Here are
  a few sample calls

  >>> Func(columnsShape=(10, 6), outputCloningWidth=4)
  (array([[ 0,  1,  2,  3,  0,  1],
         [ 4,  5,  6,  7,  4,  5],
         [ 8,  9, 10, 11,  8,  9],
         [12, 13, 14, 15, 12, 13],
         [ 0,  1,  2,  3,  0,  1],
         [ 4,  5,  6,  7,  4,  5],
         [ 8,  9, 10, 11,  8,  9],
         [12, 13, 14, 15, 12, 13],
         [ 0,  1,  2,  3,  0,  1],
         [ 4,  5,  6,  7,  4,  5]], dtype=uint32), 16)

  >>> Func(columnsShape=(7, 8), outputCloningWidth=3)
  (array([[0, 1, 2, 0, 1, 2, 0, 1],
         [3, 4, 5, 3, 4, 5, 3, 4],
         [6, 7, 8, 6, 7, 8, 6, 7],
         [0, 1, 2, 0, 1, 2, 0, 1],
         [3, 4, 5, 3, 4, 5, 3, 4],
         [6, 7, 8, 6, 7, 8, 6, 7],
         [0, 1, 2, 0, 1, 2, 0, 1]], dtype=uint32), 9)

  >>> Func(columnsShape=(7, 11), outputCloningWidth=5)
  (array([[ 0,  1,  2,  3,  4,  0,  1,  2,  3,  4,  0],
         [ 5,  6,  7,  8,  9,  5,  6,  7,  8,  9,  5],
         [10, 11, 12, 13, 14, 10, 11, 12, 13, 14, 10],
         [15, 16, 17, 18, 19, 15, 16, 17, 18, 19, 15],
         [20, 21, 22, 23, 24, 20, 21, 22, 23, 24, 20],
         [ 0,  1,  2,  3,  4,  0,  1,  2,  3,  4,  0],
         [ 5,  6,  7,  8,  9,  5,  6,  7,  8,  9,  5]], dtype=uint32), 25)

  >>> Func(columnsShape=(7, 8), outputCloningWidth=3, outputCloningHeight=4)
  (array([[ 0,  1,  2,  0,  1,  2,  0,  1],
         [ 3,  4,  5,  3,  4,  5,  3,  4],
         [ 6,  7,  8,  6,  7,  8,  6,  7],
         [ 9, 10, 11,  9, 10, 11,  9, 10],
         [ 0,  1,  2,  0,  1,  2,  0,  1],
         [ 3,  4,  5,  3,  4,  5,  3,  4],
         [ 6,  7,  8,  6,  7,  8,  6,  7]], dtype=uint32), 12)

  The basic idea with this map is that, if you imagine things stretching off
  to infinity, every instance of a given clone master is seeing the exact
  same thing in all directions.  That includes:
  - All neighbors must be the same
  - The "meaning" of the input to each of the instances of the same clone
    master must be the same.  If input is pixels and we have translation
    invariance--this is easy.  At higher levels where input is the output
    of lower levels, this can be much harder.
  - The "meaning" of the inputs to neighbors of a clone master must be the
    same for each instance of the same clone master.

  The best way to think of this might be in terms of 'inputCloningWidth' and
  'outputCloningWidth'.
  - The 'outputCloningWidth' is the number of columns you'd have to move
    horizontally (or vertically) before you get back to the same the same
    clone that you started with.  MUST BE INTEGRAL!
  - The 'inputCloningWidth' is the 'outputCloningWidth' of the node below us.
    If we're getting input from an sensor where every element just represents
    a shift of every other element, this is 1.
    At a conceptual level, it means that if two different inputs are shown
    to the node and the only difference between them is that one is shifted
    horizontally (or vertically) by this many pixels, it means we are looking
    at the exact same real world input, but shifted by some number of pixels
    (doesn't have to be 1).  MUST BE INTEGRAL!

  At level 1, I think you could have this:
  * inputCloningWidth = 1
  * sqrt(coincToInputRatio^2) = 2.5
  * outputCloningWidth = 5
  ...in this case, you'd end up with 25 masters.

  Let's think about this case:
    input:    - - -  0     1     2     3     4     5     -     -   - - -
    columns:        0 1  2 3 4  0 1  2 3 4  0 1  2 3 4  0 1  2 3 4

  ...in other words, input 0 is fed to both column 0 and column 1.  Input 1
  is fed to columns 2, 3, and 4, etc.  Hopefully, you can see that you'll
  get the exact same output (except shifted) with:
    input:    - - -  -     -     0     1     2     3     4     5   - - -
    columns:        0 1  2 3 4  0 1  2 3 4  0 1  2 3 4  0 1  2 3 4

  ...in other words, we've shifted the input 2 spaces and the output shifted
  5 spaces.

  *** The outputCloningWidth MUST ALWAYS be an integral multiple of the ***
  *** inputCloningWidth in order for all of our rules to apply.         ***
  *** NOTE: inputCloningWidth isn't passed here, so it's the caller's   ***
  ***       responsibility to ensure that this is true.                ***

  *** The outputCloningWidth MUST ALWAYS be an integral multiple of     ***
  *** sqrt(coincToInputRatio^2), too.                                  ***

  @param  columnsShape         The shape (height, width) of the columns.
  @param  outputCloningWidth   See docstring above.
  @param  outputCloningHeight  If non-negative, can be used to make
                               rectangular (instead of square) cloning fields.
  @return cloneMap             An array (numColumnsHigh, numColumnsWide) that
                               contains the clone index to use for each
                               column.
  @return numDistinctClones    The number of distinct clones in the map.  This
                               is just outputCloningWidth*outputCloningHeight.
  """
  if arg_2 < 0:
    arg_2 = arg_1

  arg_3, arg_4 = arg_0

  arg_5 = arg_1 * arg_2

  arg_6 = numpy.empty((arg_3, arg_4), 'uint32')
  for arg_7 in xrange(arg_3):
    for arg_8 in xrange(arg_4):
      arg_6[arg_7, arg_8] = (arg_8 % arg_1) + \
                    (arg_7 % arg_2) * arg_1

  return arg_6, arg_5


def Func(arg_0, arg_1='%f', arg_2=False, arg_3=True):
  """ Pretty print a numpy matrix using the given format string for each
  value. Return the string representation

  Parameters:
  ------------------------------------------------------------
  array:    The numpy array to print. This can be either a 1D vector or 2D matrix
  format:   The format string to use for each value
  includeIndices: If true, include [row,col] label for each value
  includeZeros:   Can only be set to False if includeIndices is on.
                  If True, include 0 values in the print-out
                  If False, exclude 0 values from the print-out.

  """

  arg_4 = arg_0.shape
  assert (len(arg_4) <= 2)
  arg_5 = ['[']
  if len(arg_4) == 1:
    if arg_2:
      arg_1 = '%d:' + arg_1
      if arg_3:
        arg_6 = [arg_1 % (c,x) for (c,x) in enumerate(arg_0)]
      else:
        arg_6 = [arg_1 % (c,x) for (c,x) in enumerate(arg_0) if x != 0]
    else:
      arg_6 = [arg_1 % (x) for x in arg_0]
    arg_5.extend(arg_6)

  else:
    (arg_7, arg_8) = arg_4
    if arg_2:
      arg_1 = '%d,%d:' + arg_1

    for arg_9 in xrange(arg_7):
      if arg_2:
        arg_6 = [arg_1 % (arg_9,c,x) for c,x in enumerate(arg_0[arg_9])]
      else:
        arg_6 = [arg_1 % (x) for x in arg_0[arg_9]]
      if arg_9 > 0:
        arg_5.append('')

      arg_5.append('[')
      arg_5.extend(arg_6)
      if arg_9 < arg_7-1:
        arg_5.append(']\n')
      else:
        arg_5.append(']')

  arg_5.append(']')
  return ' '.join(arg_5)


def Func(arg_0, arg_1):
    """Generates a random Func from the discrete probability distribution
    and returns its value and the log of the probability of sampling that value.
    """
    arg_2 = arg_1.uniform(0, arg_0.sum)
    arg_3 = bisect.bisect(arg_0.cdf, arg_2)
    return arg_0.keys[arg_3], numpy.log(arg_0.pmf[arg_3])


def Func(arg_0, arg_1, arg_2):
  """Link sensor region to other region so that it can pass it data."""
  arg_0.link(arg_1, arg_2, "UniformLink", "",
               srcOutput="dataOut", destInput="bottomUpIn")


def Func(arg_0, arg_1):
  """Get prediction results for all prediction steps."""
  arg_2 = arg_0.regions[arg_1]
  arg_3 = arg_2.getOutputData("actualValues")
  arg_4 = arg_2.getOutputData("probabilities")
  arg_5 = arg_2.getSelf().stepsList
  arg_6 = arg_2.getSelf().maxCategoryCount
  arg_7 = {step: {} for step in arg_5}
  for arg_8 in range(len(arg_5)):
    # stepProbabilities are probabilities for this prediction step only.
    arg_9 = arg_4[arg_8 * arg_6:(arg_8 + 1) * arg_6 - 1]
    arg_10 = arg_9.argmax()
    arg_11 = arg_3[arg_10]
    arg_12 = arg_9[arg_10]
    arg_7[arg_5[arg_8]]["predictedValue"] = arg_11
    arg_7[arg_5[arg_8]]["predictionConfidence"] = arg_12
  return arg_7


def Func(arg_0, arg_1):
    """ Loads all the parameters for this dummy model. For any paramters
    specified as lists, read the appropriate value for this model using the model
    index """

    for arg_2, arg_3 in arg_1.iteritems():
      if type(arg_3) == list:
        arg_4 = arg_0.modelIndex % len(arg_1[arg_2])
        arg_0._params[arg_2] = arg_1[arg_2][arg_4]
      else:
        arg_0._params[arg_2] = arg_1[arg_2]


def Func(arg_0):
    """ Protected function that can be overridden by subclasses. Its main purpose
    is to allow the the OPFDummyModelRunner to override this with deterministic
    values

    Returns: All the metrics being computed for this model
    """
    arg_1 = None
    if arg_0.metrics is not None:
      arg_1 = arg_0.metrics(arg_0._currentRecordIndex+1)
    elif arg_0.metricValue is not None:
      arg_1 = arg_0.metricValue
    else:
      raise RuntimeError('No metrics or metric value specified for dummy model')

    return {arg_0._optimizeKeyPattern:arg_1}


def Func(arg_0):
    """Returns a description of the dataset"""

    arg_1 = {'name':arg_0.name, 'fields':[f.name for f in arg_0.fields], \
      'numRecords by field':[f.numRecords for f in arg_0.fields]}

    return arg_1


def Func(arg_0, arg_1, arg_2):
    """Returns the sdr for jth value at column i"""
    assert len(arg_0.fields)>arg_1
    assert arg_0.fields[arg_1].numRecords>arg_2
    arg_3 = arg_0.fields[arg_1].encodings[arg_2]

    return arg_3


def Func(arg_0, arg_1):
    """Returns the nth encoding with the predictedField zeroed out"""

    assert all(arg_2.numRecords>arg_1 for arg_2 in arg_0.fields)

    arg_3 = np.concatenate([arg_2.encoder.encode(SENTINEL_VALUE_FOR_MISSING_DATA)\
        if arg_2.isPredictedField else arg_2.encodings[arg_1] for arg_2 in arg_0.fields])

    return arg_3


def Func(arg_0):
    """Returns the cumulative n for all the fields in the dataset"""

    arg_1 = sum([field.n for field in arg_0.fields])
    return arg_1


def Func(arg_0):
    """Returns the cumulative w for all the fields in the dataset"""

    arg_1 = sum([field.w for field in arg_0.fields])
    return arg_1


def Func(arg_0, arg_1):
    """Returns the nth encoding"""

    assert (all(arg_2.numEncodings>arg_1 for arg_2 in arg_0.fields))
    arg_3 = np.concatenate([arg_2.encodings[arg_1] for arg_2 in arg_0.fields])

    return arg_3


def Func(arg_0):
    """Deletes all the values in the dataset"""

    for arg_1 in arg_0.fields:
      arg_1.encodings, arg_1.values=[], []
      arg_1.numRecords, arg_1.numEncodings= (0, 0)


def Func(arg_0, arg_1, arg_2=True):
    """Value is encoded as a sdr using the encoding parameters of the Field"""

    arg_3 = np.array(arg_0.encoder.encode(arg_1), dtype=realDType)

    if arg_2:
      arg_0.encodings.append(arg_3)
      arg_0.numEncodings+=1

    return arg_3


def Func(arg_0, arg_1):
    """Set up the dataTypes and initialize encoders"""

    if arg_0.encoderType is None:
      if arg_0.dataType in ['int','float']:
        arg_0.encoderType='adaptiveScalar'
      elif arg_0.dataType=='string':
        arg_0.encoderType='category'
      elif arg_0.dataType in ['date', 'datetime']:
        arg_0.encoderType='date'

    if arg_0.dataType is None:
      if arg_0.encoderType in ['scalar','adaptiveScalar']:
        arg_0.dataType='float'
      elif arg_0.encoderType in ['category', 'enumeration']:
        arg_0.dataType='string'
      elif arg_0.encoderType in ['date', 'datetime']:
        arg_0.dataType='datetime'


def Func(arg_0, arg_1):
    """ Initialize the encoders"""

    #Initializing scalar encoder
    if arg_0.encoderType in ['adaptiveScalar', 'scalar']:
      if 'minval' in arg_1:
        arg_0.minval = arg_1.pop('minval')
      else: arg_0.minval=None
      if 'maxval' in arg_1:
        arg_0.maxval = arg_1.pop('maxval')
      else: arg_0.maxval = None
      arg_0.encoder=adaptive_scalar.AdaptiveScalarEncoder(name='AdaptiveScalarEncoder', \
                                                         w=arg_0.w, n=arg_0.n, arg_2=arg_0.minval, arg_3=arg_0.maxval, periodic=False, forced=True)

    #Initializing category encoder
    elif arg_0.encoderType=='category':
      arg_0.encoder=sdr_category.SDRCategoryEncoder(name='categoryEncoder', \
                                                   w=arg_0.w, n=arg_0.n)

    #Initializing date encoder
    elif arg_0.encoderType in ['date', 'datetime']:
      arg_0.encoder=date.DateEncoder(name='dateEncoder')
    else:
      raise RuntimeError('Error in constructing class object. Either encoder type'
          'or dataType must be specified')


def Func(arg_0):
  """Loads the experiment description file from the path.

  :param path: (string) The path to a directory containing a description.py file
         or the file itself.
  :returns: (config, control)
  """
  if not os.path.isdir(arg_0):
    arg_0 = os.path.dirname(arg_0)
  arg_1 = FuncDescriptionScriptFromDir(arg_0)
  arg_2 = getExperimentDescriptionInterfaceFromModule(arg_1)
  return arg_2.getModelDescription(), arg_2.getModelControl()


def Func(arg_0):
  """ Loads the experiment description python script from the given experiment
  directory.

  :param experimentDir: (string) experiment directory path

  :returns:        module of the loaded experiment description scripts
  """
  arg_1 = os.path.join(arg_0, "description.py")
  arg_2 = _loadDescriptionFile(arg_1)
  return arg_2


def Func(arg_0, arg_1=None, arg_2=None):
    """Return the model ID of the model with the best result so far and
    it's score on the optimize metric. If swarm is None, then it returns
    the global best, otherwise it returns the best for the given swarm
    for all generatons up to and including genIdx.

    Parameters:
    ---------------------------------------------------------------------
    swarmId:  A string representation of the sorted list of encoders in this
                 swarm. For example '__address_encoder.__gym_encoder'
    genIdx:   consider the best in all generations up to and including this
                generation if not None.
    retval:  (modelID, result)
    """
    if arg_1 is None:
      return (arg_0._bestModelID, arg_0._bestResult)

    else:
      if arg_1 not in arg_0._swarmBestOverall:
        return (None, numpy.inf)

      # Get the best score, considering the appropriate generations
      arg_3 = arg_0._swarmBestOverall[arg_1]
      arg_4 = None
      arg_5 = numpy.inf

      for (arg_6, (arg_7, arg_8)) in enumerate(arg_3):
        if arg_2 is not None and arg_6 > arg_2:
          break
        if arg_8 < arg_5:
          arg_5 = arg_8
          arg_4 = arg_7

      return (arg_4, arg_5)


def Func(arg_0, arg_1=None, arg_2=None, arg_3=None,
                       arg_4=None, arg_5=False):
    """Return a list of particleStates for all particles we know about in
    the given swarm, their model Ids, and metric results.

    Parameters:
    ---------------------------------------------------------------------
    swarmId:  A string representation of the sorted list of encoders in this
                 swarm. For example '__address_encoder.__gym_encoder'

    genIdx:  If not None, only return particles at this specific generation
                  index.

    completed:   If not None, only return particles of the given state (either
                completed if 'completed' is True, or running if 'completed'
                is false

    matured:   If not None, only return particles of the given state (either
                matured if 'matured' is True, or not matured if 'matured'
                is false. Note that any model which has completed is also
                considered matured.

    lastDescendent: If True, only return particles that are the last descendent,
                that is, the highest generation index for a given particle Id

    retval:  (particleStates, modelIds, errScores, completed, matured)
              particleStates: list of particleStates
              modelIds: list of modelIds
              errScores: list of errScores, numpy.inf is plugged in
                              if we don't have a result yet
              completed: list of completed booleans
              matured: list of matured booleans
    """
    # The indexes of all the models in this swarm. This list excludes hidden
    #  (orphaned) models.
    if arg_1 is not None:
      arg_6 = arg_0._swarmIdToIndexes.get(arg_1, [])
    else:
      arg_6 = range(len(arg_0._allResults))
    if len(arg_6) == 0:
      return ([], [], [], [], [])

    # Get the particles of interest
    arg_7 = []
    arg_8 = []
    arg_9 = []
    arg_10 = []
    arg_11 = []
    for arg_12 in arg_6:
      arg_13 = arg_0._allResults[arg_12]

      # If this entry is hidden (i.e. it was an orphaned model), it should
      #  not be in this list
      if arg_1 is not None:
        assert (not arg_13['hidden'])

      # Get info on this model
      arg_14 = arg_13['modelParams']
      arg_15 = arg_13['completed']
      arg_16 = arg_13['matured']
      arg_17 = arg_14['particleState']
      arg_18 = arg_17['genIdx']
      arg_19 = arg_17['id']

      if arg_2 is not None and arg_18 != arg_2:
        continue

      if arg_3 is not None and (arg_3 != arg_15):
        continue

      if arg_4 is not None and (arg_4 != arg_16):
        continue

      if arg_5 \
              and (arg_0._particleLatestGenIdx[arg_19] != arg_18):
        continue

      # Incorporate into return values
      arg_7.append(arg_17)
      arg_8.append(arg_13['modelID'])
      arg_9.append(arg_13['errScore'])
      arg_10.append(arg_15)
      arg_11.append(arg_16)

    return (arg_7, arg_8, arg_9, arg_10, arg_11)


def Func(arg_0, arg_1, arg_2):
    """Return a list of particleStates for all particles in the given
    swarm generation that have been orphaned.

    Parameters:
    ---------------------------------------------------------------------
    swarmId:  A string representation of the sorted list of encoders in this
                 swarm. For example '__address_encoder.__gym_encoder'

    genIdx:  If not None, only return particles at this specific generation
                  index.

    retval:  (particleStates, modelIds, errScores, completed, matured)
              particleStates: list of particleStates
              modelIds: list of modelIds
              errScores: list of errScores, numpy.inf is plugged in
                              if we don't have a result yet
              completed: list of completed booleans
              matured: list of matured booleans
    """

    arg_3 = range(len(arg_0._allResults))
    if len(arg_3) == 0:
      return ([], [], [], [], [])

    # Get the particles of interest
    arg_4 = []
    arg_5 = []
    arg_6 = []
    arg_7 = []
    arg_8 = []
    for arg_9 in arg_3:

      # Get info on this model
      arg_10 = arg_0._allResults[arg_9]
      if not arg_10['hidden']:
        continue

      arg_11 = arg_10['modelParams']
      if arg_11['particleState']['swarmId'] != arg_1:
        continue

      arg_12 = arg_10['completed']
      arg_13 = arg_10['matured']
      arg_14 = arg_11['particleState']
      arg_15 = arg_14['genIdx']
      arg_16 = arg_14['id']

      if arg_2 is not None and arg_15 != arg_2:
        continue

      # Incorporate into return values
      arg_4.append(arg_14)
      arg_5.append(arg_10['modelID'])
      arg_6.append(arg_10['errScore'])
      arg_7.append(arg_12)
      arg_8.append(arg_13)

    return (arg_4, arg_5, arg_6, arg_7, arg_8)


def Func(arg_0, arg_1, arg_2, arg_3):
    """ Return a dict of the errors obtained on models that were run with
    each value from a PermuteChoice variable.

    For example, if a PermuteChoice variable has the following choices:
      ['a', 'b', 'c']

    The dict will have 3 elements. The keys are the stringified choiceVars,
    and each value is tuple containing (choiceVar, errors) where choiceVar is
    the original form of the choiceVar (before stringification) and errors is
    the list of errors received from models that used the specific choice:
    retval:
      ['a':('a', [0.1, 0.2, 0.3]), 'b':('b', [0.5, 0.1, 0.6]), 'c':('c', [])]

    Parameters:
    ---------------------------------------------------------------------
    swarmId:  swarm Id of the swarm to retrieve info from
    maxGenIdx: max generation index to consider from other models, ignored
                if None
    varName:  which variable to retrieve

    retval:  list of the errors obtained from each choice.
    """
    arg_4 = dict()
    # Get all the completed particles in this swarm
    (arg_5, arg_6, arg_7, arg_6, arg_6) = arg_0.getParticleInfos(arg_1,
                                              genIdx=None, matured=True)

    for arg_8, arg_9 in itertools.izip(arg_5, arg_7):
      # Consider this generation?
      if arg_2 is not None:
        if arg_8['genIdx'] > arg_2:
          continue

      # Ignore unless this model completed successfully
      if arg_9 == numpy.inf:
        continue

      arg_10 = Particle.getPositionFromState(arg_8)
      arg_11 = arg_10[arg_3]
      arg_12 = str(arg_11)
      if arg_12 in arg_4:
        arg_4[arg_12][1].append(arg_9)
      else:
        arg_4[arg_12] = (arg_11, [arg_9])

    return arg_4


def Func(arg_0, arg_1):
    """
    Generate stream definition based on
    """
    #--------------------------------------------------------------------------
    # Generate the string containing the aggregation settings.
    arg_2 = {
        'days': 0,
        'hours': 0,
        'microseconds': 0,
        'milliseconds': 0,
        'minutes': 0,
        'months': 0,
        'seconds': 0,
        'weeks': 0,
        'years': 0,
    }

    # Honor any overrides provided in the stream definition
    arg_3 = {}
    if 'aggregation' in arg_1['streamDef']:
      for arg_4 in arg_2.keys():
        if arg_4 in arg_1['streamDef']['aggregation']:
          arg_2[arg_4] = arg_1['streamDef']['aggregation'][arg_4]
      if 'fields' in arg_1['streamDef']['aggregation']:
        for (arg_5, arg_6) in arg_1['streamDef']['aggregation']['fields']:
          arg_3[arg_5] = str(arg_6)

    # Do we have any aggregation at all?
    arg_7 = False
    for arg_8 in arg_2.values():
      if arg_8 != 0:
        arg_7 = True
        break

    # Convert the aggFunctionsDict to a list
    arg_9 = arg_3.items()
    arg_10 = dict(arg_2)
    arg_10['fields'] = arg_9

    arg_11 = copy.deepcopy(arg_1['streamDef'])
    arg_11['aggregation'] = copy.deepcopy(arg_10)
    return arg_11


def Func(arg_0):
    """Test if it's OK to exit this worker. This is only called when we run
    out of prospective new models to evaluate. This method sees if all models
    have matured yet. If not, it will sleep for a bit and return False. This
    will indicate to the hypersearch worker that we should keep running, and
    check again later. This gives this worker a chance to pick up and adopt any
    model which may become orphaned by another worker before it matures.

    If all models have matured, this method will send a STOP message to all
    matured, running models (presummably, there will be just one - the model
    which thinks it's the best) before returning True.
    """
    # Send an update status periodically to the JobTracker so that it doesn't
    # think this worker is dead.
    print >> sys.stderr, "reporter:status:In hypersearchV2: Func"

    # Any immature models still running?
    if not arg_0._jobCancelled:
      (arg_1, arg_2, arg_1, arg_1, arg_1) = arg_0._resultsDB.getParticleInfos(matured=False)
      if len(arg_2) > 0:
        arg_0.logger.info("Ready to end hyperseach, but not all models have " \
                         "matured yet. Sleeping a bit to wait for all models " \
                         "to mature.")
        # Sleep for a bit, no need to check for orphaned models very often
        time.sleep(5.0 * random.random())
        return False

    # All particles have matured, send a STOP signal to any that are still
    # running.
    (arg_1, arg_2, arg_1, arg_1, arg_1) = arg_0._resultsDB.getParticleInfos(completed=False)
    for arg_3 in arg_2:
      arg_0.logger.info("Stopping model %d because the search has ended" \
                          % (arg_3))
      arg_0._cjDAO.modelSetFields(arg_3,
                      dict(engStop=ClientJobsDAO.STOP_REASON_STOPPED),
                      ignoreUnchanged = True)

    # Update the HsState to get the accurate field contributions.
    arg_0._hsStatePeriodicUpdate()
    arg_4, arg_5 = \
                                          arg_0._hsState.getFieldContributions()

    # Update the results field with the new field contributions.
    arg_6 = arg_0._cjDAO.jobGetFields(arg_0._jobID, ['results'])[0]
    if arg_6 is not None:
      arg_7 = json.loads(arg_6)
    else:
      arg_7 = {}

    # Update the fieldContributions field.
    if arg_4 != arg_7.get('fieldContributions', None):
      arg_7['fieldContributions'] = arg_4
      arg_7['absoluteFieldContributions'] = arg_5

      arg_8 = arg_0._cjDAO.jobSetFieldIfEqual(arg_0._jobID,
                                                   fieldName='results',
                                                   curValue=arg_6,
                                                   newValue=json.dumps(arg_7))
      if arg_8:
        arg_0.logger.info('Successfully updated the field contributions:%s',
                                                              arg_4)
      else:
        arg_0.logger.info('Failed updating the field contributions, ' \
                         'another hypersearch worker must have updated it')

    return True


def Func(arg_0, arg_1, arg_2, arg_3, arg_4,
                         arg_5, arg_6, arg_7, arg_8):
    """Record or update the results for a model. This is called by the
    HSW whenever it gets results info for another model, or updated results
    on a model that is still running.

    The first time this is called for a given modelID, the modelParams will
    contain the params dict for that model and the modelParamsHash will contain
    the hash of the params. Subsequent updates of the same modelID will
    have params and paramsHash values of None (in order to save overhead).

    The Hypersearch object should save these results into it's own working
    memory into some table, which it then uses to determine what kind of
    new models to create next time createModels() is called.

    Parameters:
    ----------------------------------------------------------------------
    modelID:        ID of this model in models table
    modelParams:    params dict for this model, or None if this is just an update
                    of a model that it already previously reported on.

                    See the comments for the createModels() method for a
                    description of this dict.

    modelParamsHash:  hash of the modelParams dict, generated by the worker
                    that put it into the model database.
    results:        tuple containing (allMetrics, optimizeMetric). Each is a
                    dict containing metricName:result pairs. .
                    May be none if we have no results yet.
    completed:      True if the model has completed evaluation, False if it
                      is still running (and these are online results)
    completionReason: One of the ClientJobsDAO.CMPL_REASON_XXX equates
    matured:        True if this model has matured. In most cases, once a
                    model matures, it will complete as well. The only time a
                    model matures and does not complete is if it's currently
                    the best model and we choose to keep it running to generate
                    predictions.
    numRecords:     Number of records that have been processed so far by this
                      model.
    """
    if arg_4 is None:
      arg_9 = None
    else:
      arg_9 = arg_4[1].values()[0]

    # Update our database.
    arg_10 = arg_0._resultsDB.update(arg_1=arg_1,
                arg_2=arg_2,arg_3=arg_3,
                arg_9=arg_9, arg_5=arg_5,
                arg_6=arg_6, arg_7=arg_7,
                arg_8=arg_8)

    # Log message.
    arg_0.logger.debug('Received progress on model %d: completed: %s, '
                      'cmpReason: %s, numRecords: %d, errScore: %s' ,
                      arg_1, arg_5, arg_6, arg_8, arg_10)

    # Log best so far.
    (arg_11, arg_12) = arg_0._resultsDB.bestModelIdAndErrScore()
    arg_0.logger.debug('Best err score seen so far: %s on model %s' % \
                     (arg_12, arg_11))


def Func(arg_0, arg_1, arg_2, arg_3, arg_4,
               arg_5, arg_6):
    """Run the given model.

    This runs the model described by 'modelParams'. Periodically, it updates
    the results seen on the model to the model database using the databaseAO
    (database Access Object) methods.

    Parameters:
    -------------------------------------------------------------------------
    modelID:             ID of this model in models table

    jobID:               ID for this hypersearch job in the jobs table

    modelParams:         parameters of this specific model
                         modelParams is a dictionary containing the name/value
                         pairs of each variable we are permuting over. Note that
                         variables within an encoder spec have their name
                         structure as:
                           <encoderName>.<encodrVarName>

    modelParamsHash:     hash of modelParamValues

    jobsDAO              jobs data access object - the interface to the jobs
                          database where model information is stored

    modelCheckpointGUID: A persistent, globally-unique identifier for
                          constructing the model checkpoint key
    """

    # We're going to make an assumption that if we're not using streams, that
    #  we also don't need checkpoints saved. For now, this assumption is OK
    #  (if there are no streams, we're typically running on a single machine
    #  and just save models to files) but we may want to break this out as
    #  a separate controllable parameter in the future
    if not arg_0._createCheckpoints:
      arg_6 = None

    # Register this model in our database
    arg_0._resultsDB.update(arg_1=arg_1,
                           arg_3=arg_3,
                           arg_4=arg_4,
                           metricResult = None,
                           completed = False,
                           completionReason = None,
                           matured = False,
                           numRecords = 0)

    # Get the structured params, which we pass to the base description
    arg_7 = arg_3['structuredParams']

    if arg_0.logger.getEffectiveLevel() <= logging.DEBUG:
      arg_0.logger.debug("Running Model. \nmodelParams: %s, \nmodelID=%s, " % \
                        (pprint.pformat(arg_3, indent=4), arg_1))

    # Record time.clock() so that we can report on cpu time
    arg_8 = time.clock()

    # Run the experiment. This will report the results back to the models
    #  database for us as well.
    arg_9 = arg_0.logger.getEffectiveLevel()
    try:
      if arg_0._dummyModel is None or arg_0._dummyModel is False:
        (arg_10, arg_11) = FuncGivenBaseAndParams(
                    arg_1=arg_1,
                    arg_2=arg_2,
                    baseDescription=arg_0._baseDescription,
                    params=arg_7,
                    predictedField=arg_0._predictedField,
                    reportKeys=arg_0._reportKeys,
                    optimizeKey=arg_0._optimizeKey,
                    arg_5=arg_5,
                    arg_6=arg_6,
                    arg_9=arg_9,
                    predictionCacheMaxRecords=arg_0._predictionCacheMaxRecords)
      else:
        arg_12 = dict(arg_0._dummyModel)
        arg_12['permutationParams'] = arg_7
        if arg_0._dummyModelParamsFunc is not None:
          arg_13 = dict(arg_7)
          arg_13 ['generation'] = arg_3['particleState']['genIdx']
          arg_12.update(arg_0._dummyModelParamsFunc(arg_13))

        (arg_10, arg_11) = runDummyModel(
                      arg_1=arg_1,
                      arg_2=arg_2,
                      params=arg_12,
                      predictedField=arg_0._predictedField,
                      reportKeys=arg_0._reportKeys,
                      optimizeKey=arg_0._optimizeKey,
                      arg_5=arg_5,
                      arg_6=arg_6,
                      arg_9=arg_9,
                      predictionCacheMaxRecords=arg_0._predictionCacheMaxRecords)

      # Write out the completion reason and message
      arg_5.modelSetCompleted(arg_1,
                            completionReason = arg_10,
                            completionMsg = arg_11,
                            cpuTime = time.clock() - arg_8)

    except InvalidConnectionException, e:
      arg_0.logger.warn("%s", e)


def Func():
  """ Return true if the engine services are running
  """
  arg_0 = subprocess.Popen(["ps", "aux"], arg_1=subprocess.PIPE)

  arg_1 = arg_0.communicate()[0]
  arg_2 = arg_0.returncode
  if arg_2 != 0:
    raise RuntimeError("Unable to check for running client job manager")

  # See if the CJM is running
  arg_3 = False
  for arg_4 in arg_1.split("\n"):
    if "python" in arg_4 and "clientjobmanager.client_job_manager" in arg_4:
      arg_3 = True
      break

  return arg_3


def Func(arg_0, arg_1,
                                 arg_2, arg_3):
  """
  Starts a swarm, given a path to a permutations.py script.

  This function is meant to be used with a CLI wrapper that passes command line
  arguments in through the options parameter.

  @param permutationsFilePath {string} Path to permutations.py.
  @param options {dict} CLI options.
  @param outputLabel {string} Label for output.
  @param permWorkDir {string} Location of working directory.

  @returns {object} Model parameters.
  """
  global arg_4
  if "verbosityCount" in arg_1:
    arg_4 = arg_1["verbosityCount"]
    del arg_1["verbosityCount"]
  else:
    arg_4 = 1

  _setupInterruptHandling()

  arg_1["permutationsScriptPath"] = arg_0
  arg_1["outputLabel"] = arg_2
  arg_1["outDir"] = arg_3
  arg_1["permWorkDir"] = arg_3

  # Assume it's a permutations python script
  arg_5 = _injectDefaultOptions(arg_1)
  _validateOptions(arg_5)

  return _runAction(arg_5)


def Func(arg_0):
  """Back up a file

  Parameters:
  ----------------------------------------------------------------------
  retval:         Filepath of the back-up
  """
  assert os.path.exists(arg_0)

  arg_1 = 0
  (arg_2, arg_3) = os.path.splitext(arg_0)
  while True:
    arg_4 = "%s.%d%s" % (arg_2, arg_1, arg_3)
    arg_1 += 1
    if not os.path.exists(arg_4):
      break
  shutil.copyfile(arg_0, arg_4)

  return arg_4


def Func(arg_0, arg_1, arg_2):
    """ Launch worker processes to execute the given command line

    Parameters:
    -----------------------------------------------
    cmdLine: The command line for each worker
    numWorkers: number of workers to launch
    """

    arg_0._workers = []
    for arg_4 in range(arg_2):
      arg_5 = tempfile.NamedTemporaryFile(delete=False)
      arg_6 = tempfile.NamedTemporaryFile(delete=False)
      arg_7 = subprocess.Popen(arg_1, bufsize=1, env=os.environ, shell=True,
                           stdin=None, arg_5=arg_5, arg_6=arg_6)
      arg_7._stderr_file = arg_6
      arg_7._stdout_file = arg_5
      arg_0._workers.append(arg_7)


def Func(arg_0):
    """Starts HyperSearch as a worker or runs it inline for the "dryRun" action

    Parameters:
    ----------------------------------------------------------------------
    retval:         the new _HyperSearchJob instance representing the
                    HyperSearch job
    """
    # This search uses a pre-existing permutations script
    arg_1 = _ClientJobUtils.makeSearchJobParamsDict(options=arg_0._options,
                                                     forRunning=True)

    if arg_0._options["action"] == "dryRun":
      arg_2 = [sys.argv[0], "--params=%s" % (json.dumps(arg_1))]

      print
      print "=================================================================="
      print "RUNNING PERMUTATIONS INLINE as \"DRY RUN\"..."
      print "=================================================================="
      arg_3 = hypersearch_worker.main(arg_2)

    else:
      arg_4 = _setUpExports(arg_0._options["exports"])
      # Begin the new search. The {JOBID} string is replaced by the actual
      # jobID returned from jobInsert.
      arg_4 += "$HYPERSEARCH"
      arg_5 = arg_0._options["maxWorkers"]

      arg_3 = arg_0.__cjDAO.jobInsert(
        client="GRP",
        arg_4=arg_4,
        arg_1=json.dumps(arg_1),
        minimumWorkers=1,
        maximumWorkers=arg_5,
        jobType=arg_0.__cjDAO.JOB_TYPE_HS)

      arg_4 = "python -m nupic.swarming.hypersearch_worker" \
                 " --jobID=%d" % (arg_3)
      arg_0._launchWorkers(arg_4, arg_5)

    arg_6 = _HyperSearchJob(arg_3)

    # Save search ID to file (this is used for report generation)
    arg_0.__saveHyperSearchJobID(
      permWorkDir=arg_0._options["permWorkDir"],
      outputLabel=arg_0._options["outputLabel"],
      hyperSearchJob=arg_6)

    if arg_0._options["action"] == "dryRun":
      print "Successfully executed \"dry-run\" hypersearch, jobID=%d" % (arg_3)
    else:
      print "Successfully submitted new HyperSearch job, jobID=%d" % (arg_3)
      _emit(Verbosity.DEBUG,
            "Each worker executing the command line: %s" % (arg_4,))

    return arg_6