Esempio n. 1
0
class MazeApp(App):

    started = BooleanProperty(False)
    '''Indicates if has finished the build()
    '''

    app_settings = ObjectProperty(None)
    '''Reference of :class:`~designer.designer_settings.DesignerSettings`.
       :data:`designer_settings` is a :class:`~kivy.properties.ObjectProperty`
    '''

    title = 'Supermarket Maze V{}'.format(__version__)

    def on_stop(self, *args):
        pass

    def on_request_close(self, *args):
        print('Requesting a close from app..')
        return False

    def build(self):
        self.bind(on_start=self.post_build_init)
        self.root = Shell()
        self.dir = os.path.join(os.path.dirname(__file__))
        self.app_settings = AppSettings()
        self.app_settings.pre_load_settings()
        self.app_settings.load_settings()
        self._setup()

    def post_build_init(self, ev):
        win = self._app_window
        win.bind(on_keyboard=self._key_handler)
        win.bind(on_request_close=self.on_request_close)

    def _key_handler(self, *args):
        key = args[1]
        # 27 is "escape" on computers
        print('key pressed: ', key)

    def _setup(self, *args):
        '''To setup the properties of different classes
        '''
        print('Setting up App environment..')
        self.root.statusbar.bind(height=self.root.on_statusbar_height)
        self.root.actionbar.bind(height=self.root.on_actionbar_height)

        self.root.app_settings = self.app_settings
        self.root.setup()

        idx = self.root.screen_names.index('Game')
        self.root.go_screen(idx)

        self.started = True

    def _cancel_popup(self, *args):
        '''EventHandler for all self._popup when self._popup.content
           emits 'on_cancel' or equivalent.
        '''

        self._popup.dismiss()
Esempio n. 2
0
 def build(self):
     self.bind(on_start=self.post_build_init)
     self.root = Shell()
     self.dir = os.path.join(os.path.dirname(__file__))
     self.app_settings = AppSettings()
     self.app_settings.pre_load_settings()
     self.app_settings.load_settings()
     self._setup()
Esempio n. 3
0
    def dbInit():
        sqlPaths = [
            AppSettings().dropSQLTablePath,
            AppSettings().createSQLTablePath,
            AppSettings().fillSQLTablesPath
        ]
        with open(AppSettings().allSqlCommandsPath, 'w') as total:
            for sqlPath in sqlPaths:
                with open(sqlPath, 'r') as f:
                    content = f.read()
                    total.write(content + '\n')

        applySQLcommands(AppSettings().allSqlCommandsPath)
Esempio n. 4
0
    def generate(self):

        dna = Chromosome()
        cfg = AppSettings()

        if not os.path.exists("reports"):  # pragma: no cover
            os.makedirs("reports")

        f = open("reports/_" + str(int(time.time())) + ".txt", "w")
        f.write("==========================\n" +
                "==========================\n" + "==\n"
                "== Genetics\n\n" + "nucleotides: " + dna.nucleotides() +
                "\n" + "expected length: " +
                str(cfg.genetics.chromosome_length) + "\n\n" + "Examples: \n")
        for i in range(1, 10):
            dna = Chromosome()
            f.write(dna.sequence + "\n")

        lengths = []
        for i in range(1, 1000):
            dna = Chromosome()
            lengths.append(len(dna.sequence))

        f.write(f"\n\nmean_length (standard deviation):  " + \
                f"{statistics.mean(lengths)} " + \
                f"({statistics.stdev(lengths)})")
        f.close()
Esempio n. 5
0
def init_application(config_path=None):
    """
    Set up the 3 main classes: Configuration, Personality and Context
    This should be something that we can setup a unit test with
    :param config_path:
    :return:
    """
    global app_config
    global terminal_old_settings
    terminal_old_settings = termios.tcgetattr(sys.stdin)

    app_config = AppSettings(config_path)
    personality = PersonalitySettings(app_config)
    states = personality.get_states()
    context = UserContext(states, app_config, personality)
    app_config.context = context
    globalvars.app_context = context
Esempio n. 6
0
    def __init__(self):
        self.appSettings = AppSettings()
        self.lbtesting = os.getenv('LB-TESTING') or '0'
        if self.lbtesting == '1':
            # print('Using AppSettingsTest')
            self.appSettings = AppSettingsTest()

        self.process()
Esempio n. 7
0
    def __init__(self):

        super().__init__()
        self.settings = AppSettings()

        self.d_print("XpathEva constructor")

        self.filename = ""
        self.xpath_query = ""

        openFile = QAction(QIcon('open.png'), 'Open', self)
        openFile.setShortcut('Ctrl+O')
        openFile.setStatusTip('Open new File')
        openFile.triggered.connect(self.show_dialog)
        appQuit = QAction(QIcon('quit.png'), 'Quit', self)
        appQuit.setShortcut('Ctrl+Q')
        appQuit.setStatusTip('Quit')
        appQuit.triggered.connect(self._quit)

        menu_bar = self.menuBar()
        fileMenu = menu_bar.addMenu('&File')
        fileMenu.addAction(openFile)
        fileMenu.addAction(appQuit)

        self.filename_label_default_text = "Filename: "
        self.filename_label = QLabel(self)
        self.filename_label.setText(self.filename_label_default_text)
        self.query_edit = QLineEdit()
        self.query_result = QTextEdit()
        self.evaluate_btn_filler = HSpaceFiller()
        self.evaluate_button = QPushButton("Evaluate")
        self.evaluate_button.clicked.connect(self.evaluate_query)

        self.vbox = QVBoxLayout()
        self.evaluate_button_layout = QHBoxLayout()

        self.evaluate_button_layout.addWidget(self.evaluate_btn_filler)
        self.evaluate_button_layout.addWidget(self.evaluate_button)

        self.vbox.addWidget(self.filename_label)
        self.vbox.addWidget(self.query_edit)
        self.vbox.addWidget(self.query_result)
        # self.vbox.addWidget(self.evaluate_button)
        self.vbox.addLayout(self.evaluate_button_layout)

        self.central_widget = QWidget()
        self.central_widget.setLayout(self.vbox)
        self.setCentralWidget(self.central_widget)

        self.setGeometry(self.settings.x_position, self.settings.y_position,
                         self.settings.window_width,
                         self.settings.window_height)
        self.setWindowTitle('XpathEva')
        self.statusBar().showMessage('Ready')
        self.show()

        self.xpath_query_result = XpathQueryResult
Esempio n. 8
0
    def __init__(self, app, *args):
        QWidget.__init__(self, *args)
        self.session = None
        self.repo = None
        self.app = app
        self.system = SystemSettings()
        self.settings = AppSettings()

        self.main_layout = QHBoxLayout()
        self.setLayout(self.main_layout)

        self.launcher = InitialPanel(self.system, self.settings)

        self.main_layout.addWidget(self.launcher)

        self.main_layout.setContentsMargins(0, 0, 0, 0)

        self.launcher.on_settings_ready.connect(self.setSettings)
        self.launcher.on_root_selection.connect(self.start_session)
Esempio n. 9
0
    def test_inversion_diffs(self):
        cfg = AppSettings()

        reps = 1000
        deltas = []  # observed number of differences

        for _ in range(0, reps):
            dna = Chromosome()
            old_seq = dna.sequence
            dna.inversion()
            deltas.append(
                sum(1 for a, b in zip(old_seq, dna.sequence) if a != b))

        pmfs = []
        expected_deltas = []  # expected differences

        # Assumes the length of an inversion is drawn from a negative binomial
        # distribution. Calculates the probability of each length until
        # 99.99% of the distribution is accounted for. The expected number of
        # differences for each length is multiplied by the probability of that length
        # and the sum of that gives the expected differences overall.
        k = 0
        while sum(pmfs) <= 0.9999:
            pmf = nbinom.pmf(k, 1, (1 - cfg.genetics.mutation_length /
                                    (1 + cfg.genetics.mutation_length)))
            pmfs.append(pmf)

            diffs = math.floor(
                k / 2) * (1 - 1 / len(Chromosome.nucleotides())) * 2
            expected_deltas.append(pmf * diffs)
            k += 1

        expected_delta = sum(expected_deltas)

        # Since we are multiplying the binomial distribution (probably of differences at
        # a given lenght) by a negative binomial distribution (probability of a length)
        # we must compute the variance of two independent random variables
        # is Var(X * Y) = var(x) * var(y) + var(x) * mean(y) + mean(x) * var(y)
        # http://www.odelama.com/data-analysis/Commonly-Used-Math-Formulas/

        mean_binom = cfg.genetics.mutation_length
        var_binom = binom.var(mean_binom, 1 / (len(Chromosome.nucleotides())))

        mean_nbinom = cfg.genetics.mutation_length
        var_nbinom = nbinom.var(cfg.genetics.mutation_length,
                                mean_nbinom / (1 + mean_nbinom))

        var = var_binom * var_nbinom + \
              var_binom * mean_nbinom + \
              mean_binom * var_nbinom

        observed_delta = sum(deltas) / reps
        conf_99 = ((var / reps)**(1 / 2)) * 5
        assert expected_delta - conf_99 < observed_delta < expected_delta + conf_99
Esempio n. 10
0
    def substitutions(self):
        """Randomly changes charcters in the dna according to the poisson 
           distribution. expected changes length * mutation rate * nonsynomous
           substitutions."""

        cfg = AppSettings()
        num = poisson.rvs(cfg.genetics.mutation_rate * len(self.sequence))
        positions = nrand.randint(0, len(self.sequence), size=num)
        for pos in positions:
            self.sequence = self.sequence[:pos] + \
                            random.choice(self.nucleotides()) + \
                            self.sequence[(pos + 1):]
Esempio n. 11
0
def getDb():
    dbParams = AppSettings().dbParams()
    db = QtSql.QSqlDatabase.addDatabase("QMYSQL")
    db.setHostName(dbParams['host'])
    db.setDatabaseName(dbParams['db'])
    db.setUserName(dbParams['user'])
    db.setPassword(dbParams['passwd'])
    ok = db.open()
    if not ok:
        QtGui.QMessageBox.warning(None, "Error", "Invalid database!")
        sys.exit(-1)
    return db
Esempio n. 12
0
    def __init__(self, sequence=None):
        if sequence is not None:
            self.sequence = sequence
            return

        cfg = AppSettings()
        self.sequence = ""
        p = cfg.genetics.chromosome_length / (1 +
                                              cfg.genetics.chromosome_length)

        while (random.random() <= p):
            self.sequence += random.choice(self.nucleotides())
Esempio n. 13
0
    def __init__(self):
        super().__init__()

        # use for testing,  set os.environ['LB-TESTING'] = '1' to turn or '0' to turn off

        self.appSettings = AppSettings()
        self.lbtesting = os.getenv('LB-TESTING') or '0'
        if self.lbtesting == '1':
            #print('Using AppSettingsTest')
            self.appSettings = AppSettingsTest()

        self.data = None
        self.copyFile = None
        self.default_folder = None  # '{}/temp'.format(str(Path.home()))
        self.child_folder_dict = AppSettings().getProjectFolders()
        # self.lbproject_name = os.getenv('LB_PROJECT_name') or 'example'
        #self.lbproject_name =
        self.working_folder_name_default = 'example'
        self.env_default = 'dev'
        self.description = ['NEED TO ADD DESCRIPTION']

        self.fixEnv()
Esempio n. 14
0
    def deletion(self):
        """Deletes a random sequence of characters from a random position on the string. 
           The length is taken from the negative binomial distribution."""

        # Prevents an out of bounds error for random.randint()
        if (len(self.sequence) == 0):
            return

        pos = random.randint(0, len(self.sequence) - 1)

        cfg = AppSettings()
        p = cfg.genetics.mutation_length / (1 + cfg.genetics.mutation_length)

        while (random.random() <= p):
            self.sequence = self.sequence[:pos] + self.sequence[(pos + 1):]
Esempio n. 15
0
    def __init__(self, foldername=None, filename='context.template.list.json'):
        super().__init__(foldername, filename)

        self.tagid = 'templates'
        self.lbtesting = os.getenv('LB-TESTING') or '0'
        self.appSettings = AppSettings()
        #print('lbtesting', self.lbtesting)
        if self.lbtesting == '1':
            self.appSettings = AppSettingsTest()
            # default to source code resource, assume we are going to copy
            #self.setFolderName(self.appSettings.getResourceFolder('shared'))
            self.setFolderName((self.appSettings.getFolder('shared-folder')))

        self.setFolderName(self.appSettings.getFolder('shared-folder'))

        self.read()
Esempio n. 16
0
    def insertion(self):
        """Inserts a random length of random nucleotide characters into a sequence
           at a random location. """

        if (len(self.sequence) <= 1):
            pos = 0
        else:
            pos = random.randint(0, len(self.sequence) - 1)

        cfg = AppSettings()
        p = cfg.genetics.mutation_length / (1 + cfg.genetics.mutation_length)

        while (random.random() <= p):
            self.sequence = self.sequence[:pos] + \
            random.choice(self.nucleotides()) + \
            self.sequence[pos:]
Esempio n. 17
0
    def inversion(self):

        if (len(self.sequence) <= 1):
            pos = 0
        else:
            pos = random.randint(0, len(self.sequence) - 1)

        cfg = AppSettings()
        p = cfg.genetics.mutation_length / (1 + cfg.genetics.mutation_length)

        length = nbinom.rvs(
            1,
            cfg.genetics.mutation_length / (1 + cfg.genetics.mutation_length))

        self.sequence = self.sequence[:pos] + \
                        self.sequence[pos:(pos + length)][::-1] + \
                        self.sequence[(pos + length):]
Esempio n. 18
0
    def test_random_chromosome_length(self):
        """Ensures that random chromosomes are created at the correct average
           length."""
        reps = 1000
        cfg = AppSettings()
        lengths = []
        for _ in range(0, reps):
            chrom = Chromosome()
            lengths.append(len(chrom.sequence))

        mean_length = float(sum(lengths)) / len(lengths)
        expected_length = cfg.genetics.chromosome_length

        p = 1 - (expected_length / (1 + expected_length))
        conf_99 = (nbinom.var(1, p) / reps)**(1 / 2) * 4
        assert (expected_length - conf_99) <= mean_length <= (expected_length +
                                                              conf_99)
Esempio n. 19
0
 def test_say_and_save(self):
     config = AppSettings.create()
     p = PersonalitySettings.create(config.get_personality_path())
     v = VoiceLibrary(config, p)
     v.say("testing say", None, True)
     item = v.get_saying("testing say")
     print(item)
     self.assertIn("file_path", item)
     self.assertTrue(os.path.isfile(item["file_path"]))
     self.assertIn("keep_while_exists", item)
     self.assertIsNone(item["keep_while_exists"])
     self.assertIn("text", item)
     self.assertEqual("testing say", item["text"])
     self.assertIn("timestamp", item)
     self.assertIsNotNone(item["timestamp"])
     os.remove(item["file_path"])
     self.assertFalse(os.path.isfile(item["file_path"]))
Esempio n. 20
0
 def run(self):
     self.quiz_data = self.read_quiz_data()
     self.app_settings = AppSettings.from_file(self.settings_file_path)
     disciplines: Set[str] = {
         quiz_item.discipline
         for quiz_item in self.quiz_data
     }
     chosen_discipline: str = self.menu_prompt.choose(disciplines)
     topics: List[str] = [
         quiz_item.topic for quiz_item in self.quiz_data
         if quiz_item.discipline == chosen_discipline
     ]
     chosen_topic: str = self.menu_prompt.choose(topics)
     questions: List[QuizItem] = [
         quiz_item for quiz_item in self.quiz_data
         if quiz_item.topic == chosen_topic
     ]
     QuestionPrompt(questions, self.app_settings).ask_question().show()
Esempio n. 21
0
    def test_insertion_length(self):
        """Tests that insertion mutations are of the correct length"""
        cfg = AppSettings()
        reps = 1000
        deltas = []

        for _ in range(0, reps):
            dna = Chromosome()
            init_length = len(dna.sequence)
            dna.insertion()
            deltas.append(len(dna.sequence) - init_length)

        expected_delta = cfg.genetics.mutation_length
        var = nbinom.var(
            1,
            cfg.genetics.mutation_length / (1 + cfg.genetics.mutation_length))

        conf_99 = ((var / reps)**(1 / 2)) * 4
        observed_delta = (sum(deltas) / reps)
        assert (expected_delta - conf_99) < observed_delta < (expected_delta +
                                                              conf_99)
Esempio n. 22
0
 def test_say_seconds(self):
     config = AppSettings.create()
     p = PersonalitySettings.create(config.get_personality_path())
     v = VoiceLibrary(config, p)
     self.assertEqual("", v.convert_seconds_to_saying(0))  # 0 seconds
     self.assertEqual("1 second",
                      v.convert_seconds_to_saying(1))  # 45 seconds
     self.assertEqual("45 seconds",
                      v.convert_seconds_to_saying(45))  # 45 seconds
     self.assertEqual("1 minute",
                      v.convert_seconds_to_saying(60))  # 2 minutes
     self.assertEqual("2 minutes",
                      v.convert_seconds_to_saying(120))  # 2 minutes
     self.assertEqual(
         "2 minutes and 3 seconds",
         v.convert_seconds_to_saying(123))  # 2 minutes and 3 seconds
     self.assertEqual("1 hour",
                      v.convert_seconds_to_saying(1 * 60 * 60))  # 1 hour
     self.assertEqual("2 hours",
                      v.convert_seconds_to_saying(2 * 60 * 60))  # 2 hours
     self.assertEqual("2 hours and 1 minute",
                      v.convert_seconds_to_saying(2 * 60 * 60 +
                                                  60))  # 2 hours
     self.assertEqual(
         "2 hours and 34 minutes",
         v.convert_seconds_to_saying(2 * 60 * 60 +
                                     34 * 60))  # 2 hours and 34 minutes
     self.assertEqual("2 hours 1 minute and 1 second",
                      v.convert_seconds_to_saying(2 * 60 * 60 + 60 +
                                                  1))  # 2 hours
     self.assertEqual("2 hours 34 minutes and 15 seconds",
                      v.convert_seconds_to_saying(
                          2 * 60 * 60 + 34 * 60 +
                          15))  # 2 hours 34 minutes and 15 seconds
     self.assertEqual(
         "2 hours and 15 seconds",
         v.convert_seconds_to_saying(
             2 * 60 * 60 + 15))  # 2 hours 34 minutes and 15 seconds
Esempio n. 23
0
    def test_substitutions_changes(self):
        """Test that substitions occur at the expected rate."""
        cfg = AppSettings()
        reps = 1000
        deltas = []

        for _ in range(0, reps):
            seq = "a" * 100
            dna = Chromosome(seq)
            dna.substitutions()
            deltas.append(sum(1 for a, b in zip(seq, dna.sequence) if a != b))

        # Expand the conf_99 to compensate for repeated mutations in the same place
        expected_delta = cfg.genetics.mutation_rate * 100 * \
                         (1 - 1/len(Chromosome.nucleotides()))

        # Because there is a little slop around synonymous substitions I multiply
        # the confidence by 10 just to limit the number of failing tests.
        conf_99 = ((poisson.var(cfg.genetics.mutation_rate * 100) / 1000)
                   **(1 / 2)) * 10
        observed_delta = sum(deltas) / reps
        assert (expected_delta - conf_99) < observed_delta < (expected_delta +
                                                              conf_99)
Esempio n. 24
0
    def test_deletion_length(self):
        """Test that deletions return the correct averge length"""
        cfg = AppSettings()
        reps = 1000
        deltas = []

        for _ in range(0, reps):
            dna = Chromosome()
            init_length = len(dna.sequence)
            dna.deletion()
            deltas.append(init_length - len(dna.sequence))

        expected_delta = cfg.genetics.mutation_length
        var = nbinom.var(
            1,
            cfg.genetics.mutation_length / (1 + cfg.genetics.mutation_length))

        # Because there is a little slop around short strings or positions near the
        # end of the string, I multiply
        # the confidence by 10 just to limit the number of failing tests.
        conf_99 = ((var / reps)**(1 / 2)) * 10
        observed_delta = sum(deltas) / reps
        assert (expected_delta - conf_99) < observed_delta < (expected_delta +
                                                              conf_99)
Esempio n. 25
0
import time
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

#Define some globals
GPIO.setmode(GPIO.BCM)
camera = RCamera()  #Camera connected to camera pins
credentials: Credentials = Credentials()
device_client: IoTCClient = None  #IoTHubDeviceClient.create_from_connection_string(credentials.get_credentail_info(CredentialInfo.connection_string))
start_time = datetime.now()
tfclassifier = TFClassify()
tfclassifier2 = TFClassify2()
log: logging.Logger = app_logger.get_logger()
log.info(f"TensorFlow took {datetime.now() - start_time} seconds to load")

modelTier = "tier1"
_app_settings = AppSettings()
_app_settings.ensure_label_folders_exist(modelTier)

modelTier2 = "tier2"
_app_settings2 = AppSettings()
_app_settings.ensure_label_folders_exist(modelTier2)

# Set the trigger word for setting a sub tier model lookup
_subTierTrigger = _app_settings.get_SubTierTrigger(modelTier)
_subTierTrigger2 = _app_settings2.get_SubTierTrigger(modelTier2)

_USE_TEST_MODE = False
_USE_BLOB_STORGE = False

#_app_settings = AppSettings()
#These commands are sent by IoT Central to the device
Esempio n. 26
0
 def nucleotides():
     """Returns all the nucleotides that can be used in a dna string"""
     cfg = AppSettings()
     return (cfg.genetics.behaviors + cfg.genetics.gene_delimiter +
             cfg.genetics.receptor_delimiter + cfg.genetics.wildcards)
Esempio n. 27
0
 def setUpClass(cls):
     if os.path.exists(AppSettings()._cacheRoot):
         shutil.rmtree(AppSettings()._cacheRoot)
Esempio n. 28
0
 def cacheRoot(self):
     return os.path.join(AppSettings().cacheRoot, self.category)
Esempio n. 29
0
from app_settings import AppSettings
import iot_events.iot_commands as iot_commands

#Define some globals
GPIO.setmode(GPIO.BCM)
move_sensor = MotionSensor(17)  #Motion Sensor control connected to GPIO pin 17
red_led = LED(18)   #LED connected to GPIO pin 18
camera = RCamera()  #Camera connected to camera pins
credentials: Credentials = Credentials()
device_client: IoTCClient = None #IoTHubDeviceClient.create_from_connection_string(credentials.get_credentail_info(CredentialInfo.connection_string))
start_time = datetime.now()
tfclassifier = TFClassify()
log:logging.Logger = app_logger.get_logger()
#print(f"TensorFlow took {datetime.now() - start_time} seconds to load")
log.info(f"TensorFlow took {datetime.now() - start_time} seconds to load")
_app_settings = AppSettings()
_app_settings.ensure_label_folders_exist()
_USE_TEST_MODE = False
#These commands are sent by IoT Central to the device

_IoT_Commands = {
    'DownloadModel': iot_commands.iot_download_model,
    'UploadImages': iot_commands.iot_upload_images,
    'Blink': iot_commands.iot_blink
}

async def send_iot_message(message=""):
    await device_events.send_iot_message(device_client, message)

def movement_detected():
    global start_time, _USE_TEST_MODE
import time
from lobe import ImageModel
#from lobe.signature import Signature
from lobe.signature import ImageClassificationSignature
import lobe
from app_settings import AppSettings

modelTier = "tier1"
_app_settings = AppSettings()

sigpath = _app_settings.get_SignaturePath(modelTier)
#sig:Signature = Signature(sigpath)
sig: ImageClassificationSignature = ImageClassificationSignature(sigpath)

#sig:Signature = Signature('./tf_models_lite/tier1/signature.json')
model: ImageModel = ImageModel.load_from_signature(sig)

#test_model: ImageModel = ImageModel.load_from_signature(Signature('./tf_models_lite/sig.json'))
#model_not_a_bee: ImageModel = ImageModel.load()
'''
The TFClassify class (Tensor Flow Classifier), takes a TensorFlow model and allows you
to pass multiple images to it via the addImage() or addImages() methods. It then
returns the predicted classification of the images as a DICT array
{
    'image': '<image_path_sent_to_classifier>',
    'prediction': 'output_prediction_from_TensorFlow'>
}
'''


class TFClassify: