Example #1
0
    def task_load_map(self, file):
        try:
            self.camera = camera.Camera()

            self.element_tree = None
            self.osm_helper = None
            self.renderer = None

            from gc import collect as gc_collect
            gc_collect()

            log('-- loading map:', file)
            self._status(status='parsing xml')
            self.element_tree = ElementTree.parse(file)
            gc_collect()
            self.renderer = renderer.Renderer(self.camera, self.element_tree)
            try:
                self.osm_helper = self.renderer.osm_helper
            except AttributeError:
                log('Could not find OsmHelper in Renderer, creating a duplicate in Gui',
                    level=2)
                self.osm_helper = osm_helper.OsmHelper(self.element_tree)
            self.settings = {}
            self.renderer.center_camera()
            self.settings['zoom'] = self.camera.zoom_level
            self.dirty = True
            log('-- map loaded')
        except FileNotFoundError:
            log('File {} does not exist.'.format(file), level=3)
            log(level=3)
            log('Use "{0} -f FILE" to specify a map file or "{0} -h" to show help.'
                .format(sys.argv[0]),
                level=3)
 def onClose(self, wasClean, code, reason):
     try:
         del(self.dispatchers[self.peer])
     except:
         pass        
     gc_collect()
     print("WebSocket connection closed: %s"%( reason ))
Example #3
0
 def deinit(self):
     if self.log_handler:
         self.logger.removeFileHandler(self.log_handler)
     self.debug("Remove all session agents")
     self.agents.clear()
     if RUNNING_PYPY:
         gc_collect()
 def update_status(self, data):
     if data != self.data:
         self.data = data
         system('cls')
         if self.data["playback_status"] != "Idle":
             print(
                 f"Song Details:\n   [Title] {self.data['song_title']}\n[Duration] {self.data['song_duration']}\n  [Status] {self.data['playback_status']}"
             )
             self.RPC.update(
                 large_image="groove_music",
                 large_text=f"Groove Music DRP v{self.version}",
                 small_image=("play" if self.data['playback_status']
                              == "Playing" else "pause"),
                 small_text=self.data['playback_status'],
                 details=self.data['song_title'],
                 state=
                 f"Duration: {self.data['song_duration']//60}min {self.data['song_duration']%60}s"
             )
         else:
             print(f"Groove Music is not opened")
             self.RPC.update(large_image="groove_music",
                             large_text=f"Groove Music DRP v{self.version}",
                             small_image="pause",
                             small_text="Idle",
                             details="No Song Selected",
                             state=f"Idle")
     del data
     gc_collect()
Example #5
0
 def deinit(self):
     if self.log_handler:
         self.logger.removeFileHandler(self.log_handler)
     self.debug("Remove all session agents")
     self.agents.clear()
     if RUNNING_PYPY:
         gc_collect()
Example #6
0
def DBN(X, y, feature_names):
    _, y = change_class_labels(y)
    train_X, val_X, train_y, val_y = train_test_split(X,
                                                      y,
                                                      test_size=0.20,
                                                      random_state=42)

    # train
    lambda1s = [0.005]  # numpy.arange(0.0700,-0.001,-0.001)

    for i in range(len(lambda1s)):
        classifier, training_time = dbn.train_model(train_set_x_org=train_X,
                                                    train_set_y_org=train_y,
                                                    valid_set_x_org=val_X,
                                                    valid_set_y_org=val_y)

        # the scores/ weights
        param0 = classifier.params[0].get_value()

        results = sorted(zip(map(lambda x: round(x, 4), param0),
                             feature_names),
                         reverse=True)

    gc_collect()
    return [x[1] for x in results]
Example #7
0
 def wrapper(*args, **kwargs):
     # TODO: find 'self' object - untested
     report = kwargs.get('self')
     if report is None:
         for i in args:
             if hasattr(i, 'dirLog'):
                 report = i
     if report is None:
         report = globals().get('self')
     if report is None:
         raise LogError
     # Configure logger
     import logging
     bolNewLog = False
     try:
         if not os_path_isfile(report.dirLog):
             open(report.dirLog, 'w').close()
             bolNewLog = True
         strFormat = '%(asctime)s %(levelname)s: %(message)s'
         logging.basicConfig(filename=report.dirLog, filemode='a',
                             format=strFormat, level=logging.DEBUG)
     except Exception as err:
         print(err)
         raise LogError
     if callable(fun):
         fun(args, kwargs)
     logging.shutdown()
     del logging
     gc_collect()
     return bolNewLog
Example #8
0
    def connect(self):
        self.discon()

        self.mqtt = MQTTClient(
            self.mac,
            self.secrets.MQTT_SERVER,
            user=self.secrets.MQTT_USER.encode(UTF8),
            password=self.secrets.MQTT_PASSWORD.encode(UTF8))

        def rx(tpc, msg):
            msg = msg.decode(UTF8)
            gc_collect()
            try:
                for t, cb in self.cb.items():
                    if t == tpc:
                        cb(msg)
                        break
            except Exception as e:
                print_exception(e)
                raise RxEx()

        self.mqtt.set_callback(rx)

        self.mqtt.connect()
        sleep(0.5)

        if self.do_pub_cfg:
            self.do_pub_cfg = not (self.pub_cfg())
            sleep(0.5)

        if self.topic != None:
            self.mqtt.subscribe(self.topic)
            sleep(0.5)

        gc_collect()
Example #9
0
 def discon(self):
     if self.mqtt != None:
         try:
             self.mqtt.disconnect()
         except:
             pass
         self.mqtt = None
     gc_collect()
Example #10
0
 def pub_cfg(self):
     ok = True
     for i, obj in enumerate(self.obj):
         print(obj.__class__.__name__)
         gc_collect()
         if not self.pub_json(obj.cfg_tpc(), obj.cfg(**self.cfg[i]), retain=True):
             ok = False
             print('>fail')
     return ok
Example #11
0
    def test_Awaitable(self):
        def gen():
            yield

        #@types.coroutine
        #def coro():
        #    yield

        #async def new_coro():
        #    pass

        class Bar(object):
            def __await__(self):
                yield

        class MinimalCoro(Coroutine):
            def send(self, value):
                return value

            def throw(self, typ, val=None, tb=None):
                super(MinimalCoro, self).throw(typ, val, tb)

            def __await__(self):
                yield

        non_samples = [None, int(), gen(), object()]
        for x in non_samples:
            self.assertNotIsInstance(x, Awaitable)
            self.assertFalse(issubclass(type(x), Awaitable), repr(type(x)))
            self.assertFalse(inspect.isawaitable(x))

        samples = [Bar(), MinimalCoro()]
        for x in samples:
            self.assertIsInstance(x, Awaitable)
            self.assertTrue(issubclass(type(x), Awaitable))
            self.assertTrue(inspect.isawaitable(x))

        #c = coro()
        #self.assertIsInstance(c, Awaitable)
        #c.close() # awoid RuntimeWarning that coro() was not awaited

        #c = new_coro()
        #self.assertIsInstance(c, Awaitable)
        #c.close() # awoid RuntimeWarning that coro() was not awaited

        class CoroLike(object):
            pass

        Coroutine.register(CoroLike)
        self.assertTrue(inspect.isawaitable(CoroLike()))
        self.assertTrue(isinstance(CoroLike(), Awaitable))
        self.assertTrue(issubclass(CoroLike, Awaitable))
        CoroLike = None
        gc_collect()  # Kill CoroLike to clean-up ABCMeta cache
Example #12
0
 def rx(tpc, msg):
     msg = msg.decode(UTF8)
     gc_collect()
     try:
         for t, cb in self.cb.items():
             if t == tpc:
                 cb(msg)
                 break
     except Exception as e:
         print_exception(e)
         raise RxEx()
 def add(self, cls, priority=False):
     gc_collect()
     self.print(cls, '__init__')
     obj = cls(self)
     on = set(obj.update_on if hasattr(obj, 'update_on') else ())
     if priority:
         self.obj.insert(0, (obj, on))
     else:
         self.obj.append((obj, on))
     self.mem_alloc()
     return obj
Example #14
0
 def pub_cfg(self):
     if self.led:
         self.led.fast_blink()
     ok = True
     for i, obj in enumerate(self.obj):
         print(obj.__class__.__name__)
         gc_collect()
         if not self.pub_json(obj.cfg_tpc(), obj.cfg(**self.cfg[i]), retain=True):
             ok = False
             print('pub_cfg', 'err')
     if self.led:
         self.led.off()
     return ok
Example #15
0
    def run(self):
        assert self.root._native_ is not None
        self.display.auto_refresh = False
        self.display.show(self.root._native_)

        # raise RuntimeError()

        print("Current widget tree:")
        self.root._print_tree(fn=lambda w: (w.coord, w.dims, w.isshowing()))

        print("setting up recurring updates...")
        now = monotonic_ns()
        sort = lambda pack: pack[2]
        updates = sorted(
            ([fn, int(period * 1e9), now + int(period * 1e6)]
             for fn, period in self._iter_registered_updates_()),
            key=sort,
        )

        # run each event first
        for (fn, _, _) in updates:
            fn()

        gc_collect()
        print(gc.mem_free())
        print("starting loop...")
        touch_loop = self.touch_loop

        display_refresh = self.display.refresh

        loop = touch_loop.loop

        while True:
            display_refresh()
            loop()
            display_refresh()
            loop()
            display_refresh()
            loop()
            gc_collect()

            # run_updates
            now = monotonic_ns()
            for (pack, (fn, period, run_next)) in zip(updates, updates):
                if run_next < now:
                    fn()
                    pack[2] = now + period
                else:
                    break
            updates.sort(key=sort)
    def main_process(self):
        """ method for main process """
        system('cls')
        try:
            if self.automatic:
                while True:
                    try:
                        data = self.fetch_info()
                        if data == self.data:
                            del data
                            gc_collect()
                            continue
                        gc_collect()
                    except IndexError:
                        data = {"playback_status": "Idle"}
                    self.update_status(data)
                    del data
                    gc_collect()
                    sleep(self.REFRESH_DELAY)
                    gc_collect()
            else:
                while True:
                    try:
                        data = self.fetch_info()
                    except IndexError:
                        data = {"playback_status": "Idle"}
                    self.update_status(data)
                    input("\nenter to update status")

        except KeyboardInterrupt:
            return
Example #17
0
    def test_Awaitable(self):
        def gen():
            yield

        #@types.coroutine
        #def coro():
        #    yield

        #async def new_coro():
        #    pass

        class Bar(object):
            def __await__(self):
                yield

        class MinimalCoro(Coroutine):
            def send(self, value):
                return value
            def throw(self, typ, val=None, tb=None):
                super(MinimalCoro, self).throw(typ, val, tb)
            def __await__(self):
                yield

        non_samples = [None, int(), gen(), object()]
        for x in non_samples:
            self.assertNotIsInstance(x, Awaitable)
            self.assertFalse(issubclass(type(x), Awaitable), repr(type(x)))
            self.assertFalse(inspect.isawaitable(x))

        samples = [Bar(), MinimalCoro()]
        for x in samples:
            self.assertIsInstance(x, Awaitable)
            self.assertTrue(issubclass(type(x), Awaitable))
            self.assertTrue(inspect.isawaitable(x))

        #c = coro()
        #self.assertIsInstance(c, Awaitable)
        #c.close() # awoid RuntimeWarning that coro() was not awaited

        #c = new_coro()
        #self.assertIsInstance(c, Awaitable)
        #c.close() # awoid RuntimeWarning that coro() was not awaited

        class CoroLike(object): pass
        Coroutine.register(CoroLike)
        self.assertTrue(inspect.isawaitable(CoroLike()))
        self.assertTrue(isinstance(CoroLike(), Awaitable))
        self.assertTrue(issubclass(CoroLike, Awaitable))
        CoroLike = None
        gc_collect() # Kill CoroLike to clean-up ABCMeta cache
Example #18
0
 def add(self, cls, priority=False, **opt):
     gc_collect()
     self.print(cls, '__init__')
     obj = cls(self, **opt)
     gc_collect()
     on = set(obj.update_on) if hasattr(obj, 'update') and hasattr(
         obj, 'update_on') else set()
     if on:
         print('update_on', ':', on)
     if priority:
         self.obj.insert(0, (obj, on))
     else:
         self.obj.append((obj, on))
     self.mem_alloc()
     return obj
Example #19
0
 def _send(self, method, url, json):
     print('REQUEST: {}: {}'.format(method, json))
     try:
         resp = requests.request(method=method,
                                 url=url,
                                 json=json,
                                 headers=self._http_headers)
     except OSError as e:
         print('RESPONSE: failed to perform request: {}'.format(e))
         if not self._wlan.isconnected():
             print('wifi connection lost, restarting...')
             time.sleep(3)
             reset()
     else:
         print('RESPONSE: {}...'.format(str(resp.json())[:100]))
         gc_collect()
Example #20
0
    def connect(self):
        self.discon()

        self.mqtt = MQTTClient(
            self.mac,
            self.secrets.MQTT_SERVER,
            keepalive=60,
            user=self.secrets.MQTT_USER.encode(UTF8),
            password=self.secrets.MQTT_PASSWORD.encode(UTF8)
        )

        def rx(tpc, msg):
            if self.led:
                self.led.on()
            print('  >', 'rx', tpc)
            print('  >', 'rx', msg)
            try:
                msg = msg.decode(UTF8)
                gc_collect()
                for t, cb in self.cb.items():
                    if t == tpc:
                        print('  >', 'rx', 'cb')
                        cb(msg)
                        break
            except Exception as e:
                self.log(e)
            else:
                self.err = 0
            if self.led:
                self.led.off()
        
        self.mqtt.set_callback(rx)

        self.mqtt.connect()
        sleep(0.5)
        
        if self.do_pub_cfg:
            self.do_pub_cfg = not(self.pub_cfg())
            sleep(0.5)
            
        if self.topic != None:
            print('subscribe', self.topic)
            self.mqtt.subscribe(self.topic)
            sleep(0.5)

        gc_collect()
Example #21
0
    def deinit(self):
        self.summarize()
        self.aggressivity = None
        self.debug("Remove all project agents")
        for agent in self.application().agents:
            self.agents.remove(agent, False)
        self.agents.clear()

        remove = self.directory.destroy()
        if remove:
            logger = self.application().logger
            logger.removeFileHandler(self.log_handler)
            logger.filename = None

        self.directory = None
        if RUNNING_PYPY:
            gc_collect()
Example #22
0
    def deinit(self):
        self.summarize()
        self.aggressivity = None
        self.debug("Remove all project agents")
        for agent in self.application().agents:
            self.agents.remove(agent, False)
        self.agents.clear()

        remove = self.directory.destroy()
        if remove:
            logger = self.application().logger
            logger.removeFileHandler(self.log_handler)
            logger.filename = None

        self.directory = None
        if RUNNING_PYPY:
            gc_collect()
Example #23
0
 def rx(tpc, msg):
     if self.led:
         self.led.on()
     print('  >', 'rx', tpc)
     print('  >', 'rx', msg)
     try:
         msg = msg.decode(UTF8)
         gc_collect()
         for t, cb in self.cb.items():
             if t == tpc:
                 print('  >', 'rx', 'cb')
                 cb(msg)
                 break
     except Exception as e:
         self.log(e)
     else:
         self.err = 0
     if self.led:
         self.led.off()
Example #24
0
 def sub(self, tpc, cb):
     gc_collect()
     self.cb[tpc.encode(UTF8)] = cb
     first = next(iter(self.cb))
     if len(self.cb) == 1:
         self.topic = first
     else:
         self.topic = b''
         for i, char in enumerate(first):
             for t in self.cb.keys():
                 if t[i] != char:
                     char = None
                     break
             if char == None:
                 break
             else:
                 self.topic += chr(char)
         self.topic += b'#'
     gc_collect()
    def fetch_info(self):
        session = async_run(self.get_session())

        data = {}
        data["song_title"] = async_run(self.get_song_title(session))
        data["song_duration"] = round(
            (session.get_timeline_properties().end_time.duration) / 10**7)
        data["playback_status"] = "Paused" if session.get_playback_info(
        ).playback_status == 5 else "Playing"
        #print(f"Current Media Info:{current_media_info}")
        #
        #print(f"Playback Info: {playback_info}")
        #controls_info = self.__class__.convert_to_dict(playback_info['controls'])
        #print(f"Playback Controls Info: {controls_info}")
        #
        #position_info = self.__class__.convert_to_dict(timeline_info['position'])
        #print(f"Position Info: {position_info}")
        del session
        gc_collect()
        return data
Example #26
0
def MLP(X, y, feature_names):
    _, y = change_class_labels(y)
    train_X, val_X, train_y, val_y = train_test_split(X, y, test_size=0.20, random_state=42)

    lambda1s = [0.01]

    for i in range(len(lambda1s)):
        classifier, training_time = mlp.train_model(train_set_x_org=train_X,
                                                     train_set_y_org=train_y,
                                                     valid_set_x_org=val_X,
                                                     valid_set_y_org=val_y
                                                    )


        # the scores/ weights
        param0 = classifier.params[0].get_value()

        # get the list of (feat_weight, feat_name) in descending order according to the feat_weight.
        results = sorted(zip(map(lambda x: round(x, 4), param0), feature_names), reverse=True)

    gc_collect()
    return [x[1] for x in results]
Example #27
0
 def wait(self):
     while self.reconnect:
         try:
             self.connect()
             while True:
                 gc_collect()
                 self.mqtt.wait_msg()
         except Exception as e:
             self.err += 1
             self.log(e)
             self.discon()
             if self.err > 1000:
                 machine.reset()
             if self.err > 60:
                 if self.led:
                     led.slow_blink()
                 # add a bit of randomness in case every device on the network are all retrying at the same time:
                 sleep(60 + randint(3))
                 if self.led:
                     self.led.off()
             else:
                 sleep(self.err)
Example #28
0
 def pub_json(self, tpc, obj, **kwarg):
     gc_collect()
     with BytesIO() as json:
         json_dump(obj, json)
         gc_collect()
         ok = self.publish(tpc, json.getvalue(), **kwarg)
     gc_collect()
     return ok
Example #29
0
    def destroy(self):
        if self._destroyed:
            return
        self._destroyed = True

        # Destroy all project agents
        self.aggressivity = None
        self.debugger = None
        for agent in self.application().agents:
            self.agents.remove(agent, False)
        self.agents.clear()

        # Keep project directory?
        keep = self.directory.keepDirectory()
        if not keep:
            # Don't keep the directory: destroy log file
            logger = self.application().logger
            logger.unlinkFile()
            # And then remove the whole directory
            self.directory.rmtree()
        self.directory = None

        if RUNNING_PYPY:
            gc_collect()
Example #30
0
 def wait(self, led=None):
     while self.reconnect:
         try:
             if wifi.is_connected():
                 self.connect()
                 while True:
                     gc_collect()
                     try:
                         self.mqtt.wait_msg()
                     except RxEx:
                         pass
             else:
                 print('No WiFi')
         except Exception as e:
             print('MQTT' + ':')
             print_exception(e)
             self.discon()
         if self.reconnect:
             if led:
                 led.slow_blink()
                 sleep(random_int(5))
                 led.off()
             else:
                 sleep(random_int(8))
Example #31
0
 def pub_json(self, tpc, obj, **kwarg):
     gc_collect()
     print(tpc)
     print(obj)
     if wifi.is_connected() and self.mqtt:
         if type(tpc) != type(b''):
             tpc = tpc.encode(UTF8)
         with BytesIO() as json:
             json_dump(obj, json)
             gc_collect()
             self.mqtt.publish(tpc, json.getvalue(), **kwarg)
             sleep(0.5)
         gc_collect()
         return True
     return False
Example #32
0
 def deinit(self):
     if self.log_handler:
         self.logger.removeFileHandler(self.log_handler)
     self.agents.clear()
     if RUNNING_PYPY:
         gc_collect()
Example #33
0
valid_set_y_org=numpy.loadtxt(filename,delimiter='\t',dtype=object)
prev,valid_set_y_org=cl.change_class_labels(valid_set_y_org)
# test set
filename=data_dir + "GM12878_200bp_Data_3Cl_l2normalized_TestSet.txt";
test_set_x_org=numpy.loadtxt(filename,delimiter='\t',dtype='float32')
filename=data_dir + "GM12878_200bp_Classes_3Cl_l2normalized_TestSet.txt";
test_set_y_org=numpy.loadtxt(filename,delimiter='\t',dtype=object)
prev,test_set_y_org=cl.change_class_labels(test_set_y_org)

filename=data_dir + "GM12878_Features_Unique.txt";
features=numpy.loadtxt(filename,delimiter='\t',dtype=object)  

rng=numpy.random.RandomState(1000)

# train
classifier_trained,training_time=logistic_sgd.train_model(learning_rate=0.1, n_epochs=1000,
            train_set_x_org=train_set_x_org,train_set_y_org=train_set_y_org,
            valid_set_x_org=valid_set_x_org,valid_set_y_org=valid_set_y_org, 
            batch_size=200)
# test
test_set_y_pred,test_set_y_pred_prob,test_time=logistic_sgd.test_model(classifier_trained,test_set_x_org)
print test_set_y_pred
print test_set_y_pred_prob
print test_time
# evaluate classification performance
perf,conf_mat=cl.perform(test_set_y_org,test_set_y_pred,numpy.unique(train_set_y_org))
print perf
print conf_mat

gc_collect()
Example #34
0
def cleanly_handle_plan_request(msg, maze, pub):
    handle_plan_request(msg, maze, pub)
    gc_collect()
Example #35
0
 def __del__(self):
     if self.channels:
         while self.channels:
             self.drop_channel(self.channels.pop())
         gc_collect()
Example #36
0
 def pub_state(self):
     gc_collect()
     return self.pub_json(self.obj[0].base_tpc(), self.state)
     gc_collect()