def runExperiment(n, w, threshold, cellsPerColumn, folder, numTrials=5,
                  cleverTMSDRs=False):
  """
  Run a PoolOfPairsLocation1DExperiment various union sizes.
  """
  if not os.path.exists(folder):
    try:
      os.makedirs(folder)
    except OSError:
      # Multiple parallel tasks might create the folder. That's fine.
      pass

  filename = "{}/n_{}_w_{}_threshold_{}_cellsPerColumn_{}.json".format(
    folder, n, w, threshold, cellsPerColumn)
  if len(glob.glob(filename)) == 0:
    print("Starting: {}/n_{}_w_{}_threshold_{}_cellsPerColumn_{}".format(
      folder, n, w, threshold, cellsPerColumn))

    result = defaultdict(list)
    for _ in xrange(numTrials):

      exp = PoolOfPairsLocation1DExperiment(**{
        "numMinicolumns": n,
        "numActiveMinicolumns": w,
        "poolingThreshold": threshold,
        "cellsPerColumn": cellsPerColumn,
        "minicolumnSDRs": generateMinicolumnSDRs(n=n, w=w, threshold=threshold),
      })

      if cleverTMSDRs:
        exp.trainWithSpecificPairSDRs(carefullyCollideContexts(
          numContexts=25, numCells=cellsPerColumn, numMinicolumns = n))
      else:
        exp.train()

      for unionSize in [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]:
        additionalSDRCounts = exp.testInferenceOnUnions(unionSize)

        result[unionSize] += additionalSDRCounts

    with open(filename, "w") as fOut:
      json.dump(sorted(result.items(), key=lambda x: x[0]),
                fOut)
      print("Wrote:", filename)
Run a PoolOfPairsLocation1DExperiment various union sizes.