Exemplo n.º 1
0
    def __init__(self):
        with\
            Autowired('DbTestData', singleton=False) as DbTestData,\
            Autowired('Config', singleton=False) as Config:
            self.TRAIN_SET_VALUES = numpy.array(list(DbTestData.trainComments))
            self.PROBLEM_OIDS = numpy.array(list(DbTestData.problemOids))
            self.TRAIN_SET = pandas.read_csv(Config.TRAINING_SET_FILE,
                                             '\t',
                                             encoding='utf-8',
                                             names=Config.TRAINING_SET_COLUMNS)
            self.PROBLEM_SET = pandas.read_csv(
                Config.PROBLEM_SET_FILE,
                '\t',
                encoding='utf-8',
                names=Config.PROBLEM_SET_COLUMNS)

            self.PROBLEM_SET_VALUES = numpy.array(
                list(DbTestData.problemComments))
            self.MAX_LENGTH = 6
            self.OUTPUT_NEURONS = 4
            self.TRAIN_RESULTS = numpy.array([
                2,
                3,
                3,
                1,
            ])
            self.PROBLEM_MODEL_INPUT = [[98, 111, 111, 0, 0, 0],
                                        [102, 101, 102, 101, 102, 101]]
            self.TRAIN_MODEL_INPUT = [[98, 108, 97, 98, 108, 97],
                                      [98, 101, 101, 97, 0, 0],
                                      [98, 101, 97, 97, 0, 0],
                                      [98, 97, 122, 0, 0, 0]]
            self.PROBLEM_MODEL_RESULT = [[0.3, 0.1, 0.6, 0],
                                         [0, 0.3, 0.1, 0.6]]
Exemplo n.º 2
0
 def setUp(self):
     with Autowired('WinterBootTestData', self):
         if self.WinterBootTestData.undefinedConsumedServiceId in consumers:
             del consumers[
                 self.WinterBootTestData.undefinedConsumedServiceId]
         import testpackage
         autoload(testpackage)
         Autowired(self.WinterBootTestData.undefinedConsumedServiceId)
 def testdisplayTransaction_prints_the_transaction(self):
     with\
             Autowired('UiTestData',self),\
             Autowired('AiTestData',self),\
             unittest.mock.patch('sys.stdout') as mockedStdout:
         displayAccuracyService.call(self.AiTestData.ACCURACY)
     TestHelper.assertPrintedOn(
         mockedStdout,
         self.UiTestData.ACCURACY_STRING.format(self.AiTestData.ACCURACY))
Exemplo n.º 4
0
 def setUp(self):
     with \
             Autowired('DataTestData', self),\
             Autowired('AiTestData', self),\
             Autowired('UpdateTestData', self),\
             MockedService('PrepareDataService', self),\
             MockedService('NeuralNetBuilderService', self),\
             MockedService('NeuralNetTrainerService', self),\
             MockedService('AccuracyCheckService', self),\
             MockedService('UpdateService', self):
         categorizerService.call()
    def setUp(self):

        with \
          Autowired('UpdateTestData', self),\
          Autowired('DbTestData', self),\
          MockedService('RowUpdateService', self),\
          MockedService('ConnectionService', self),\
          MockedService('CategoryService', self):

            updateService.call(self.UpdateTestData.data)

            self.handleOneRowArgs = self.RowUpdateService.call
Exemplo n.º 6
0
 def setUp(self):
     with \
             Autowired('DbTestData', self),\
             unittest.mock.patch('psycopg2.connect', new = self.DbTestData.connector) as connector:
         self.fakeConnection = self.DbTestData.connection
         self.connector = connector
         self.connection = connectionService.call()
 def setUp(self):
     with Autowired('DbTestData', self, singleton=False):
         self.fakeConnection = self.DbTestData.connection
         updateDBService.call(self.fakeConnection,
                              self.DbTestData.oidAsString,
                              self.DbTestData.fetched_row,
                              self.DbTestData.choice)
 def setUp(self):
     with\
             Autowired('DataTestData',self),\
             MockedService('NumericConverterService', self):
         self.result = prepareDataService.call(
             self.DataTestData.TRAIN_SET, self.DataTestData.PROBLEM_SET)
     self.callList = self.NumericConverterService.call.mock_calls
 def setUp(self):
     with Autowired('UpdateTestData') as testData:
         self.existingChoice = testData.existingChoice
         self.nonExistingChoice = testData.nonExistingChoice
         self.regexConformantChoiceInput = testData.regexConformantChoiceInput
         self.choiceFromRegexConformantInput = testData.choiceFromRegexConformantInput
         self.regexConformantChoiceInput = testData.regexConformantChoiceInput
         self.regexConformantChoiceInput = testData.regexConformantChoiceInput
         self.options = testData.PREPARED_OPTIONS
    def setUp(self):
        with Autowired('AiTestData', self, singleton=False):
            self.model = self.AiTestData.model

            self.result = neuralNetTrainerService.call(
                self.AiTestData.TRAIN_VALUES, self.AiTestData.TRAIN_RESULTS,
                self.model)
            self.compileCallKwargs = TestHelper.callKwArgument(
                self.model.compile)
            self.fitCallKwargs = TestHelper.callKwArgument(self.model.fit)
Exemplo n.º 11
0
 def test_displayOption_displays_choice_number_probability_and_categories(
         self):
     with\
             Autowired('UpdateTestData', self),\
             unittest.mock.patch('sys.stdout') as mockedStdout:
         optionDisplayService.call(
             self.UpdateTestData.resultKeys[0],
             self.UpdateTestData.data.problemValues[0][2],
             self.UpdateTestData.categories[3])
     TestHelper.assertPrintedOn(mockedStdout,
                                self.UpdateTestData.outputOfFirstOption)
Exemplo n.º 12
0
 def setUp(self):
     with\
             Autowired('DataTestData', self),\
             MockedService('keras.Sequential') as self.model:
         self.result = neuralNetBuilderService().call(self.DataTestData.MAX_LENGTH, self.DataTestData.OUTPUT_NEURONS )
     self.firstCallList = TestHelper.callArgument(self.model, 0)
Exemplo n.º 13
0
#coding=utf-8
import unittest
from winterboot.Autowired import Autowired
import keras
import tensorflow
import TestHelper
from winterboot.MockedService import MockedService

neuralNetBuilderService = Autowired('NeuralNetBuilderService')
config = Autowired('Config')()

class testNeuralNetBuilderService(unittest.TestCase):

    def setUp(self):
        with\
                Autowired('DataTestData', self),\
                MockedService('keras.Sequential') as self.model:
            self.result = neuralNetBuilderService().call(self.DataTestData.MAX_LENGTH, self.DataTestData.OUTPUT_NEURONS )
        self.firstCallList = TestHelper.callArgument(self.model, 0)

    def test_it_builds_a_Sequential_model(self):
        self.model.assert_called_once()
    def test_first_layer_is_Dense(self):
        self.assertEqual(keras.layers.Dense, type(self.firstCallList[0]))
    def test_first_layer_have_FIRST_LAYER_NEURONS_number_of_neurons(self):
        self.assertEqual(config.FIRST_LAYER_NEURONS, self.firstCallList[0].units)
    def test_first_layer_have_relu_activation(self):
        self.assertEqual(tensorflow.nn.relu, self.firstCallList[0].activation)
    def test_second_layer_is_Dense(self):
        self.assertEqual(keras.layers.Dense, type(self.firstCallList[1]))
    def test_second_layer_have_SECOND_LAYER_NEURONS_number_of_neurons(self):
Exemplo n.º 14
0
from winterboot.Autowired import Autowired
from winterboot.Service import Service
from categorizerai.ai.AIData import AIData

connectionService = Autowired('ConnectionService')
categoryService = Autowired('CategoryService')
rowUpdateService = Autowired('RowUpdateService')



@Service
class UpdateService:

    def call(self, data: AIData) -> None:
        connection = connectionService.call()
        categories = categoryService.call(connection)
    
        for rowNumber in range(len(data.problemResults)):
            rowUpdateService.call(rowNumber, data, connection, categories)
        connection.close()
Exemplo n.º 15
0
import unittest
from winterboot.Autowired import Autowired
import TestHelper

optionDisplayService = Autowired('OptionDisplayService')


class Test(unittest.TestCase):
    def test_displayOption_displays_choice_number_probability_and_categories(
            self):
        with\
                Autowired('UpdateTestData', self),\
                unittest.mock.patch('sys.stdout') as mockedStdout:
            optionDisplayService.call(
                self.UpdateTestData.resultKeys[0],
                self.UpdateTestData.data.problemValues[0][2],
                self.UpdateTestData.categories[3])
        TestHelper.assertPrintedOn(mockedStdout,
                                   self.UpdateTestData.outputOfFirstOption)


if __name__ == "__main__":
    unittest.main()
import unittest
from winterboot.Autowired import Autowired
import TestHelper

displayAccuracyService = Autowired("DisplayAccuracyService")


class Test(unittest.TestCase):
    def testdisplayTransaction_prints_the_transaction(self):
        with\
                Autowired('UiTestData',self),\
                Autowired('AiTestData',self),\
                unittest.mock.patch('sys.stdout') as mockedStdout:
            displayAccuracyService.call(self.AiTestData.ACCURACY)
        TestHelper.assertPrintedOn(
            mockedStdout,
            self.UiTestData.ACCURACY_STRING.format(self.AiTestData.ACCURACY))


if __name__ == "__main__":
    unittest.main()
Exemplo n.º 17
0
#!/usr/bin/python3
#coding=utf-8
import pandas
from winterboot.Autowired import Autowired
from winterboot.Service import Service

config = Autowired('Config')()
prepareDataService = Autowired('PrepareDataService')
neuralNetBuilderService = Autowired('NeuralNetBuilderService')
neuralNetTrainerService = Autowired('NeuralNetTrainerService')
accuracyCheckService = Autowired('AccuracyCheckService')
updateService = Autowired('UpdateService')

@Service
class CategorizerService(object):
    def call(self) -> None:
        trainSet = pandas.read_csv(config.TRAINING_SET_FILE,'\t',encoding='utf-8',names=config.TRAINING_SET_COLUMNS)
        problemSet = pandas.read_csv(config.PROBLEM_SET_FILE,'\t',encoding='utf-8',names=config.PROBLEM_SET_COLUMNS)
        
        data = prepareDataService.call(trainSet, problemSet)
        model = neuralNetBuilderService.call(data.max_length, data.numberOfOutputNeurons)
        accuracy = neuralNetTrainerService.call(data.trainValues, data.trainResults, model)
        accuracyCheckService.call(accuracy)
        data.problemResults=model.predict(data.problemValues)
        
        updateService.call(data)

Exemplo n.º 18
0
from winterboot.Autowired import Autowired
from winterboot.Service import Service
from categorizerai.ai.AIData import AIData
import psycopg2

rowProviderService = Autowired('RowProviderService')
transactionDisplayService = Autowired('TransactionDisplayService')
updateDBService = Autowired('UpdateDBService')
optionPreparatorService = Autowired('OptionPreparatorService')
choiceObtainerService = Autowired('ChoiceObtainerService')


@Service
class RowUpdateService:
    def call(self, rowNumber: int, data: AIData,
             connection: psycopg2.extensions.connection,
             categories: tuple) -> None:
        oidAsStr = str(data.problemOids[rowNumber])
        row = rowProviderService.call(connection, oidAsStr)
        transactionDisplayService.call(row)
        options = optionPreparatorService.call(rowNumber, data, categories)
        choice = choiceObtainerService.call(options)
        if choice is not None:
            updateDBService.call(connection, oidAsStr, row, choice)
Exemplo n.º 19
0
import unittest
from winterboot.Autowired import Autowired
from winterboot.MockedService import MockedService
import TestHelper

choiceAskService = Autowired("ChoiceAskService")


class Test(unittest.TestCase):
    def setUp(self):
        with\
                Autowired('UiTestData', self),\
                MockedService('categorizerai.ui.ChoiceAskService.input') as self.mockedInput:
            self.answer = choiceAskService.call()

    def test_askUserForChoice_displays_prompt(self):
        TestHelper.assertCallParameter(self.UiTestData.PROMPT,
                                       self.mockedInput, 0)

    def test_askUserForChoice_returns_value_given_in_stdin(self):
        self.assertEqual(self.UiTestData.ANSWER_TO_CHOICE_PROMPT, self.answer)


if __name__ == "__main__":
    unittest.main()
Exemplo n.º 20
0
from winterboot.Autowired import Autowired
from winterboot.Service import Service
import types

config = Autowired('Config')()


@Service
class UpdateDBService:
    def call(self, connection, oid, row, choice):
        table = config.figureOutTable(row)
        sentence = self._createUpdateSentence(oid, choice, table)
        cursor = connection.cursor()
        cursor.execute(sentence)
        connection.commit()
        cursor.close()

    def _createUpdateSentence(self, oid, choice, table):
        args = types.SimpleNamespace()
        args.table = table
        args.choice = choice
        args.oid = oid
        sentence = config.SQL_TO_UPDATE_RECORD.format(args)
        return sentence
Exemplo n.º 21
0
#coding=utf-8
import unittest
from winterboot.Autowired import Autowired
import TestHelper

categoryService = Autowired('CategoryService')
config = Autowired('Config')()


class Test(unittest.TestCase):
    def setUp(self):
        with Autowired('DbTestData', self, singleton=False):
            self.fakeConnection = self.DbTestData.connection
            self.categories = categoryService.call(self.fakeConnection)

    def test_returns_a_dict_keyed_with_the_element_ID_COLUMN_POSITION_IN_CATEGORIES_TABLE(
            self):
        firstRow = self.DbTestData.all_rows[0]
        firstRowKey = firstRow[config.ID_COLUMN_POSITION_IN_CATEGORIES_TABLE]
        self.assertEqual(firstRow, self.categories[firstRowKey])

    def test_the_returned_dict_contains_elements_for_all_different_keys(self):
        self.assertEqual(len(self.DbTestData.all_rows), len(self.categories))

    def test_closes_the_connection(self):
        self.fakeConnection.cursor.assert_called_once()

    def test_executes_SQL_TO_OBTAIN_CATEGORIES(self):
        self.fakeConnection.cursor.execute.assert_called_once()
        TestHelper.assertCallParameter(config.SQL_TO_OBTAIN_CATEGORIES,
                                       self.fakeConnection.cursor.execute, 0)
#coding=utf-8
import unittest
from winterboot.Autowired import Autowired
import TestHelper

neuralNetTrainerService = Autowired('NeuralNetTrainerService')
config = Autowired('Config')()


class Test(unittest.TestCase):
    def setUp(self):
        with Autowired('AiTestData', self, singleton=False):
            self.model = self.AiTestData.model

            self.result = neuralNetTrainerService.call(
                self.AiTestData.TRAIN_VALUES, self.AiTestData.TRAIN_RESULTS,
                self.model)
            self.compileCallKwargs = TestHelper.callKwArgument(
                self.model.compile)
            self.fitCallKwargs = TestHelper.callKwArgument(self.model.fit)

    def test_trainNeuralNet_compiles_the_model(self):
        self.model.compile.assert_called_once()

    def test_trainNeuralNet_returns_model_accuracy(self):
        self.assertEqual(self.AiTestData.ACCURACY, self.result)

    def test_loss_funtions_is_sparse_categorical_crossentropy(self):
        self.assertEqual("sparse_categorical_crossentropy",
                         self.compileCallKwargs['loss'])
import unittest
from winterboot.Autowired import Autowired
from winterboot.MockedService import MockedService
import TestHelper

updateService = Autowired("UpdateService")


class Test(unittest.TestCase):
    def setUp(self):

        with \
          Autowired('UpdateTestData', self),\
          Autowired('DbTestData', self),\
          MockedService('RowUpdateService', self),\
          MockedService('ConnectionService', self),\
          MockedService('CategoryService', self):

            updateService.call(self.UpdateTestData.data)

            self.handleOneRowArgs = self.RowUpdateService.call

    def test_handleUpdates_obtains_a_connection(self):
        self.ConnectionService.call.assert_called_once()

    def test_fetches_categories_using_the_connection(self):
        self.CategoryService.call.assert_called_once_with(
            self.DbTestData.connection)

    def test_calls_RowUpdateService_for_all_rows_in_problem_results(self):
        self.assertEqual(len(self.UpdateTestData.data.problemResults),
Exemplo n.º 24
0
 def setUp(self):
     with\
             Autowired('UiTestData', self),\
             MockedService('categorizerai.ui.ChoiceAskService.input') as self.mockedInput:
         self.answer = choiceAskService.call()
Exemplo n.º 25
0
 def answerIs(self,choice):
     with Autowired('ChoiceAskService', self):
         self.ChoiceAskService.call.return_value = choice
Exemplo n.º 26
0
 def test_TestData_instances_are_differ(self):
     TestTestData = Autowired('TestTestData')
     self.assertNotEqual(TestTestData(),TestTestData())
Exemplo n.º 27
0
 def setUp(self):
     with Autowired('DbTestData', self, singleton=False):
         self.fakeConnection = self.DbTestData.connection
         self.categories = categoryService.call(self.fakeConnection)
import unittest
from winterboot.Autowired import Autowired
from winterboot.MockedService import MockedService
import TestHelper

optionPreparatorService = Autowired('OptionPreparatorService')

class Test(unittest.TestCase):

    def setUp(self):
        with\
            Autowired('UpdateTestData', self, singleton=False),\
            MockedService('OptionDisplayService', self):
            self.result = optionPreparatorService.call(
                self.UpdateTestData.rowNumber, self.UpdateTestData.data, self.UpdateTestData.categories)
    def test_prepareOptionsToOffer_returns_a_dict_keyed_by_small_numbers(self):
        self.assertEqual(self.UpdateTestData.resultKeys, list(self.result.keys()))

    def test_the_first_element_in_the_value_tuple_is_the_probability(self):
        self.assertEqual(self.UpdateTestData.data.problemValues[0][2], self.result[1][0])

    def test_the_second_element_in_the_value_tuple_is_a_value_of_the_categories_dict(self):
        self.assertEqual(self.UpdateTestData.categories[3], self.result[1][1])

    def test_prepareOptionsToOffer_displays_choice_key(self):
        TestHelper.assertCallParameter(self.UpdateTestData.resultKeys[0], self.OptionDisplayService.call, 0)

    def test_prepareOptionsToOffer_displays_probability(self):
        TestHelper.assertCallParameter(self.UpdateTestData.data.problemValues[0][2], self.OptionDisplayService.call, 1)

    def test_prepareOptionsToOffer_displays_categories(self):
 def testName(self):
     origTestData = Autowired('TestTestData')()
     with TestDataForStub('TestTestData', self):
         self.assertEqual(origTestData, self.TestTestData)
 def setUp(self):
     with\
         Autowired('UpdateTestData', self, singleton=False),\
         MockedService('OptionDisplayService', self):
         self.result = optionPreparatorService.call(
             self.UpdateTestData.rowNumber, self.UpdateTestData.data, self.UpdateTestData.categories)