<commit_before>#!/usr/bin/env python
# ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2014, Numenta, Inc.  Unless you have an agreement
# with Numenta, Inc., for a separate license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------

import pprint
from random import shuffle
import unittest2 as unittest

from nupic.data.pattern_machine import PatternMachine, ConsecutivePatternMachine
from nupic.data.sequence_machine import SequenceMachine
from nupic.test.temporal_memory_test_machine import TemporalMemoryTestMachine
from nupic.research.temporal_memory import TemporalMemory



class AbstractTemporalMemoryTest(unittest.TestCase):

  def setUp(self):
    self.defaultTMParams = None
    self.tm = None
    self.patternMachine = None
    self.sequenceMachine = None
    self.tmTestMachine = None

    print ("\n"
           "======================================================\n"
           "Test: {0} \n"
           "{1}\n"
           "======================================================\n"
    ).format(self.id(), self.shortDescription())


  def initTM(self, overrides=None):
    params = self.defaultTMParams
    params.update(overrides or {})
    self.tm = TemporalMemory(**params)

    print "Initialized new TM with parameters:"
    print pprint.pformat(params)
    print


  def finishSetUp(self, patternMachine):
    self.patternMachine = patternMachine
    self.sequenceMachine = SequenceMachine(self.patternMachine)
    self.tmTestMachine = TemporalMemoryTestMachine(self.tm)


  # ==============================
  # Helper functions
  # ==============================

  def feedTM(self, sequence, learn=True, num=1):
    self.showInput(sequence, learn=learn, num=num)

    repeatedSequence = sequence * num
    results = self.tmTestMachine.feedSequence(repeatedSequence, learn=learn)

    detailedResults = self.tmTestMachine.computeDetailedResults(
      results,
      repeatedSequence)

    print self.tmTestMachine.prettyPrintDetailedResults(detailedResults,
                                                        repeatedSequence,
                                                        self.patternMachine)
    print

    if learn:
      print self.tmTestMachine.prettyPrintConnections()

    return detailedResults


  def showInput(self, sequence, learn=True, num=1):
    sequenceText = self.sequenceMachine.prettyPrintSequence(sequence)
    learnText = "(learning {0})".format("enabled" if learn else "disabled")
    numText = " [{0} times]".format(num) if num > 1 else ""
    print "Feeding sequence {0}{1}:\n{2}".format(
      learnText, numText, sequenceText)
    print



class BasicTemporalMemoryTest(AbstractTemporalMemoryTest):

  def setUp(self):
    super(BasicTemporalMemoryTest, self).setUp()

    self.defaultTMParams = {
      "columnDimensions": [6],
      "cellsPerColumn": 4,
      "initialPermanence": 0.3,
      "connectedPermanence": 0.5,<commit_msg>Added VERBOSITY option to test<commit_after>#!/usr/bin/env python
# ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2014, Numenta, Inc.  Unless you have an agreement
# with Numenta, Inc., for a separate license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------

import pprint
from random import shuffle
import unittest2 as unittest

from nupic.data.pattern_machine import PatternMachine, ConsecutivePatternMachine
from nupic.data.sequence_machine import SequenceMachine
from nupic.test.temporal_memory_test_machine import TemporalMemoryTestMachine
from nupic.research.temporal_memory import TemporalMemory



VERBOSITY = 1



class AbstractTemporalMemoryTest(unittest.TestCase):

  def setUp(self):
    self.defaultTMParams = None
    self.tm = None
    self.patternMachine = None
    self.sequenceMachine = None
    self.tmTestMachine = None

    print ("\n"
           "======================================================\n"
           "Test: {0} \n"
           "{1}\n"
           "======================================================\n"
    ).format(self.id(), self.shortDescription())


  def initTM(self, overrides=None):
    params = self.defaultTMParams
    params.update(overrides or {})
    self.tm = TemporalMemory(**params)

    print "Initialized new TM with parameters:"
    print pprint.pformat(params)
    print


  def finishSetUp(self, patternMachine):
    self.patternMachine = patternMachine
    self.sequenceMachine = SequenceMachine(self.patternMachine)
    self.tmTestMachine = TemporalMemoryTestMachine(self.tm)


  # ==============================
  # Helper functions
  # ==============================

  def feedTM(self, sequence, learn=True, num=1):
    self.showInput(sequence, learn=learn, num=num)

    repeatedSequence = sequence * num
    results = self.tmTestMachine.feedSequence(repeatedSequence, learn=learn)

    detailedResults = self.tmTestMachine.computeDetailedResults(
      results,
      repeatedSequence)

    print self.tmTestMachine.prettyPrintDetailedResults(detailedResults,
                                                        repeatedSequence,
                                                        self.patternMachine,
                                                        verbosity=VERBOSITY)
    print

    if learn:
      print self.tmTestMachine.prettyPrintConnections()

    return detailedResults


  def showInput(self, sequence, learn=True, num=1):
    sequenceText = self.sequenceMachine.prettyPrintSequence(sequence,
                                                            verbosity=VERBOSITY)
    learnText = "(learning {0})".format("enabled" if learn else "disabled")
    numText = " [{0} times]".format(num) if num > 1 else ""
    print "Feeding sequence {0}{1}:\n{2}".format(
      learnText, numText, sequenceText)
    print



class BasicTemporalMemoryTest(AbstractTemporalMemoryTest):

  def setUp(self):
    super(BasicTemporalMemoryTest, self).setUp()

    self.defaultTMParams = {
      "columnDimensions": [6],
      "cellsPerColumn": 4,
      "initialPermanence": 0.3,
      "connectedPermanence": 0.5,
{'fraction_non_alphanumeric': 0.1602275734321249, 'fraction_numerical': 0.005953956073035195, 'mean_word_length': 2.8664961636828643, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7248784', 'n_tokens_mistral': 2021, 'n_tokens_neox': 1889, 'n_words': 561}