def __init__(self, rnn_type, nin, nout, ninp, nhid, nlayers, max_memory_size, lr, batch_size, clip, optimizer, train_weights_before_predict, weights_trainer, learn_iterations, tie_weights, w_window, dropout, is_cuda): criterion = nn.CrossEntropyLoss() super(BaseMixtureOfRNNsLearner, self).__init__(criterion, nout, learn_iterations) self.optimizer_algorithm = optimizer #general parameters self.rnn_type = rnn_type self.nin = nin self.nout = nout self.ninp = ninp self.nhid = nhid self.max_memory_size = max_memory_size self.dropout = dropout self.rnns = [] self.ids = [] self.id_count = 0 self.lr = lr self.batch_size = batch_size self.hidden = [] self.clip = clip self.window_size = w_window self.nlayers = nlayers self.train_weights_before_predict = train_weights_before_predict self.cuda_available = is_cuda self.outputs = torch.zeros(self.max_memory_size, 20, self.batch_size, self.nout) self.weights_trainer = weights_trainer self.tie_weights = tie_weights self.weights_added = Observable() self.weights_removed = Observable() self.weights_updated = Observable() self._initialize_model()
def __init__(self,board,players=(),fixed_handicap=0,komi=0,custom_handicap=0,ruleset=None): Observable.__init__(self) self._board = board self._testboard = None if ruleset: self.ruleset = ruleset else: self.ruleset = rules.AGARules() self.history = [] # the board at all times in the past self.moves = [] # the list of moves if not players: black_player = player.PlayerSettings('black','Black') white_player = player.PlayerSettings('black','Black',komi=komi) players = [player.Player(p, self) for p in (black_player, white_player)] else: for p in players: p.game = self self.players = players self.next_player = self.players[0] self.winner = None self.state = PLACE_HANDICAP # Handle handicap, komi etc self.ruleset.setup(self) # If manual handicap stones need to be placed, change to that team; # otherwise, begin the game if not self.skip_completed_handicap(): self.start()
def __init__(self): Observable.__init__(self) self.options = ProcessingOptions(0, ProcessingArea(246, 266, 200, 300), 5, 4, 1) self.folder_path = None self.log = None self.p = Processor()
def __init__(self, argv, map=None, timeout=None, close_when_done=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, preexec_fn=None, bufsize=0, **popen_keyw): """Accepts all the same arguments and keywords as `subprocess.Popen`. Input or outputs specified as `PIPE` (now the default) for are wrapped in an asynchronous pipe dispatcher. The timeout is used to create an alarm, which can be cancelled by calling `cancel_timeout()`, `communicate()`, `wait()` or `kill()`. """ Observable.__init__(self) self._map = map # Create the subprocess itself, wrapping preexec_fn in the clear_signals call Popen.__init__(self, argv, preexec_fn=lambda: self.clear_signals(preexec_fn), stdin=stdin, stdout=stdout, stderr=stderr, **popen_keyw) # Set the timeout on the subprocess. If it fails, ignore the failure. try: fto = float(timeout) self._alarmobj = alarm.alarm(fto, self.kill) if fto > 0 else None except: self._alarmobj = None # Wrap the pipe I/O. Sets the Popen and pipe buffer sizes the same; perhaps not optimal. if stdout == PIPE: self.stdout = OutputPipeDispatcher(self.stdout, map=map, ignore_broken_pipe=True, universal_newlines=self.universal_newlines, maxdata=bufsize) self.stdout.obs_add(self._pipe_event) if stderr == PIPE: self.stderr = OutputPipeDispatcher(self.stderr, map=map, ignore_broken_pipe=True, universal_newlines=self.universal_newlines, maxdata=bufsize) self.stderr.obs_add(self._pipe_event) if stdin == PIPE: self.stdin = InputPipeDispatcher(self.stdin, map=map, ignore_broken_pipe=True, close_when_done=close_when_done, maxdata=bufsize) self.stdin.obs_add(self._pipe_event)
def __init__(self, fh, map=None, maxdata=None, ignore_broken_pipe=False, logger=None, **obsopt): """Wrap a dispatcher around the passed filehandle. If `ignore_broken_pipe` is `True`, an `EPIPE` or `EBADF` error will call `handle_close()` instead of `handle_expt()`. Useful when broken pipes should be handled quietly. `logger` is a logger which will be used to log unusual exceptions; otherwise, they will be printed to stderr. """ self.maxdata = maxdata if maxdata else self.pipe_maxdata self.__logger = logger if ignore_broken_pipe: self.__ignore_errno = [EPIPE, EBADF] else: self.__ignore_errno = [] self.__filehandle = fh # Check for overduplication of the file descriptor and close the extra fddup = os.dup(fh.fileno()) file_dispatcher.__init__(self, fddup, map=map) if (self._fileno != fddup): os.close(fddup) Observable.__init__(self, **obsopt)
def __init__(self, rnn_type, consolidate_moment, consolidate_threshold, nin, nout, ninp, nhid, nlayers, max_memory_size, max_ltm_size, init_stm_size, lr, batch_size, clip, optimizer, ltm_reinstatement, stm_consolidation, train_weights_before_predict, weights_trainer, residual_weights_trainer=None, tie_weights=False, w_window=20, recent_window_size=5, dropout=0.2, is_cuda = True): self.optimizers = [] self.consolidate_moment = consolidate_moment self.consolidation_threshold = consolidate_threshold self.reinstatement_threshold = consolidate_threshold self.consolidate_moment_ticks = 1 self.residual_weights_trainer = None self.ltm_size = 0 self.memory_size = 0 self.max_ltm_size = max_ltm_size self.init_stm_size = init_stm_size self.max_memory_size = max_memory_size self.stm_consolidation = stm_consolidation self.ltm_reinstatement = ltm_reinstatement self.recent_window_size = recent_window_size self.weight_history = [] self.weights_copied = Observable() self.weights_moved = Observable() self.ltm_size_updated = Observable() super(CloneLearner, self).__init__(rnn_type, nin, nout, ninp, nhid, nlayers, max_memory_size, lr, batch_size, clip, optimizer, train_weights_before_predict, weights_trainer, tie_weights, w_window, dropout, is_cuda) self.weights_updated.register(self.save_weights_history)
def __init__(self): Observable.__init__(self) self.local_players = [] self.remote_players = [] self.accept_local_moves = False self.game = None self.confirmed_dead_stones_remote = set() self.confirmed_dead_stones = False
def __init__(self, argv, map=None, timeout=None, close_when_done=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, preexec_fn=None, bufsize=0, **popen_keyw): """Accepts all the same arguments and keywords as `subprocess.Popen`. Input or outputs specified as `PIPE` (now the default) for are wrapped in an asynchronous pipe dispatcher. The timeout is used to create an alarm, which can be cancelled by calling `cancel_timeout()`, `communicate()`, `wait()` or `kill()`. """ Observable.__init__(self) self._map = map # Create the subprocess itself, wrapping preexec_fn in the clear_signals call Popen.__init__(self, argv, preexec_fn=lambda: self.clear_signals(preexec_fn), stdin=stdin, stdout=stdout, stderr=stderr, **popen_keyw) # Set the timeout on the subprocess. If it fails, ignore the failure. try: fto = float(timeout) self._alarmobj = alarm.alarm(fto, self.kill) if fto > 0 else None except: self._alarmobj = None # Wrap the pipe I/O. Sets the Popen and pipe buffer sizes the same; perhaps not optimal. if stdout == PIPE: self.stdout = OutputPipeDispatcher( self.stdout, map=map, ignore_broken_pipe=True, universal_newlines=self.universal_newlines, maxdata=bufsize) self.stdout.obs_add(self._pipe_event) if stderr == PIPE: self.stderr = OutputPipeDispatcher( self.stderr, map=map, ignore_broken_pipe=True, universal_newlines=self.universal_newlines, maxdata=bufsize) self.stderr.obs_add(self._pipe_event) if stdin == PIPE: self.stdin = InputPipeDispatcher(self.stdin, map=map, ignore_broken_pipe=True, close_when_done=close_when_done, maxdata=bufsize) self.stdin.obs_add(self._pipe_event)
def __init__(self, master, width, *args, **kwargs): tk.Canvas.__init__(self, master, width=width, height=width / 7 * 6, *args, **kwargs) Observable.__init__(self) self._width = width self._shape_ids = Matrix(7, 6) self._init_ui() self.bind('<Button-1>', self._on_click)
def __init__(self, size, grid=None): # do need option to pass in grid ? RE FACTOR - probably don't need this Observable.__init__(self) self._colOrdStart = ord('a') self._colOrdEnd = self._colOrdStart + size - 1 self._grid = grid if grid else [[None for c in range(size)] for r in range(size)] self.size = size # create regular expression to match moves. Do it here so only done once. r = r'[a-' + chr(self._colOrdEnd) + 'A-' + chr( self._colOrdEnd).upper() + ']' # add number range. r += '[1-' + str(size) + ']' self._movePattern = r
def __init__(self, ag_voc_params, id): Observable.__init__(self) seed() self.id = id n_ag = len(ag_voc_params) self.ag_voc_param = ag_voc_params[id] self.feat_extractor = FeatureExtraction() self.identificator = AgentIndentification(ag_voc_params) self.pres_estimator = PresenceEstimation(n_ag) self.val_estimator = ValueEstimation(n_ag, lr=0.05, discount=0.9) self.decision_maker = ActionSelection(n_ag, lr=0.05) self.reflex = Reflex() self.motor = MotorExecution(self.ag_voc_param) self.amp = 0 self.adapt = True
def test_custom_event(): subject = Observable() custom_event = CustomEvent() subject.on('custom', custom_event) subject.on(['custom', 'custom_alias'], custom_event) # same reference assert(subject.custom and subject.custom_alias is custom_event) assert(one_handler and two_handler in subject.custom.handlers) # update an event subject.on('custom', CustomEvent()) assert(subject.custom is not custom_event)
def __init__(self, size, root, testMode=False): Observable.__init__(self) self.pawnPromoted = False self._highlightMoves = tkinter.IntVar() self._highlightMoves.set(1) self._mouseOverGridRef = None self._mouseOverMoveGridRefs = [] self._selectedGridRef = None self._selectedMoveGridRefs = [] self._player1Type = tkinter.StringVar() self._player1Type.set(ChessPlayers.HUMAN) self._player2Type = tkinter.StringVar() self._player2Type.set(ChessPlayers.COMPUTER_LEVEL0) self.pWhiteDropDown = None self.pBlackDropDown = None middleColumn = tkinter.Frame(root) middleColumn.grid(row=0, column=1) self.playerLabel = tkinter.Label(middleColumn) self.playerLabel.grid(row=0, sticky='nsew') self.feedbackLabel = tkinter.Label(middleColumn) self.feedbackLabel.grid(row=1, sticky='nsew') self.boardCanvas = BoardCanvas(middleColumn, 8) self.boardCanvas.grid(row=2, sticky='nsew') self.addObserver(self.boardCanvas) self._moveText = tkinter.Text(root, width=20, state='disabled', wrap=tkinter.WORD, background=self.boardCanvas.colours[1]) self._moveText.grid(row=0, column=0, sticky='nsew') helpFrame = self._createHelpFrame(root) helpFrame.grid(row=0, column=2, sticky='nsew') # follow movements of the mouse self.boardCanvas.bind('<Motion>', self.mouseMovement) self.boardCanvas.bind('<Button-1>', self.selectSquare)
def test_event_with_many_handlers_attached(): subject = Observable() subject.on('many', [one_handler, two_handler]) assert(subject.events['many'] is subject.many) assert(one_handler and two_handler in subject.many.handlers) # remove a handler subject.off('many', one_handler) assert(one_handler not in subject.many.handlers) # remove the event subject.off('many') assert('many' not in subject.events) with raises(AttributeError): subject.many
def __init__(self, serial, callback, do_timestamp=True): """ :param serial: A Serial object for communicating with a serial port. It needs to have a read timeout set. Otherwise, this class object might hang forever if a end line character is never received. http://pyserial.readthedocs.io/en/latest/shortintro.html#readline :type Serial :param callback: A callback method for calling back to owner when error occurs. :param do_timestamp: Add a timestamp to each line intercepted from the serial port. """ Thread.__init__(self, name=self.__class__.__name__) Observable.__init__(self) self.setDaemon(True) self._stop = Event() self._do_timestamp = do_timestamp self._port = serial self.logger = logging.getLogger(self.__class__.__name__) self._start_time = None # Is set when first log line arrives from serial port. self._callback = callback codecs.register_error('backslashreplace', self.backslash_replace)
def __init__(self, fh, map=None, maxdata=None, ignore_broken_pipe=False, logger=None, **obsopt): """Wrap a dispatcher around the passed filehandle. If `ignore_broken_pipe` is `True`, an `EPIPE` or `EBADF` error will call `handle_close()` instead of `handle_expt()`. Useful when broken pipes should be handled quietly. `logger` is a logger which will be used to log unusual exceptions; otherwise, they will be printed to stderr. """ self.maxdata = maxdata if maxdata else self.pipe_maxdata self.__logger = logger if ignore_broken_pipe: self.__ignore_errno = [EPIPE, EBADF] else: self.__ignore_errno = [] self.__filehandle = fh # Check for overduplication of the file descriptor and close the extra fddup = os.dup(fh.fileno()) file_dispatcher.__init__(self, fddup, map=map) if (self._fileno != fddup): os.close (fddup) Observable.__init__(self, **obsopt)
def test_many_events_with_dictionary(): subject = Observable() subject.on({ # setting events of an observable with dictionary 'one': one_handler, 'two': two_handler, 'custom': CustomEvent(), 'many': [one_handler, two_handler]}) subject.trigger('one two custom many', 1, 2, a=3, b=4)
def __init__(self, lattice, basis, connections, default_value=None): """Store points and connections""" Observable.__init__(self) self.points = {} self.connections = {} self.xmax = -1000 self.ymax = -1000 self.xmin = 1000 self.ymin = 1000 for lx, ly in lattice.items(): # Add the points by superimposing the basis onto each lattice point for b in basis: bx, by = b self.points[(lx + bx, ly + by)] = default_value self.connections[(lx + bx, ly + by)] = [] self.xmin = min(self.xmin, lx + bx) self.ymin = min(self.ymin, ly + by) self.ymax = max(self.ymax, ly + by) self.xmax = max(self.xmax, lx + bx) # Now connect the points for lx, ly in lattice.items(): try: for c in connections: cx1, cy1, cx2, cy2 = c a = (lx + cx1, ly + cy1) b = (lx + cx2, ly + cy2) if a in self.points and b in self.points: self.connections[a].append(b) self.connections[b].append(a) except KeyError: raise Exception("Invalid connections")
def test_event_with_handler_attached(): subject = Observable() subject.on('one', one_handler) subject.on('two', two_handler) assert(subject.events['one'] is subject.one) assert(subject.events['two'] is subject.two) # trigger subject.one(1, 2, a=3, b=4) subject.one.trigger(1, 2, a=3, b=4) subject.trigger('one', 1, 2, a=3, b=4) subject.events['one'].trigger(1, 2, a=3, b=4) subject.trigger('one two', 1, 2, a=3, b=4) subject.trigger(['one', 'two'], 1, 2, a=3, b=4)
def __init__(self, entries, destiny, use_common_path=True): threading.Thread.__init__(self) Observable.__init__(self) self.entries = entries[:] self.destiny = destiny.replace(' ', '\\ ') self.use_common_path = use_common_path
def test_update(self): observable = Observable() observer = Observer(1) observable.addObserver(observer) observable.updateObservers(2) self.assertEqual(observer.data, 2)
self.plotly_user_config['plotly_streaming_tokens'][0]) self.stream.open() def notify(self, observable, *args, **kwargs): # write to plotly if 'value' in kwargs: # self.stream.write({'x': datetime.datetime.now(), 'y': kwargs['value']}) self.stream.write({'x': kwargs['timestamp'], 'y': kwargs['value']}) # import plotly.plotly as py # from plotly.graph_objs import * # trace0 = Scatter( # x=[1, 2, 3, 4], # y=[10, 15, 13, 17] # ) # trace1 = Scatter( # x=[1, 2, 3, 4], # y=[16, 5, 11, 9] # ) # data = Data([trace0, trace1]) # unique_url = py.plot(data, filename = 'basic-line') if __name__ == '__main__': subject = Observable() plyobs = plotlyObserver(subject, 'config.json', 'test stream plot') subject.notify_observers(value=45) subject.notify_observers(value=55)
def __init__(self): Observable.__init__(self) self._game = Game()
"""Alarm Event names""" from observer import Observable import logging LOGGER = logging.getLogger('alarm') # Events MOTION_DETECTED = 'motion_detected' # Basic handlers def log_it(*args, **kwargs): LOGGER.debug("%s, %s", args, kwargs) # Setup events observable = Observable() observable.on(MOTION_DETECTED, [log_it])
def test_no_callable_handler(): subject = Observable() with raises(TypeError): subject.on('error', NotCallable())
def test_events_with_same_handlers_attached(): subject = Observable() subject.on('many many2', [one_handler, two_handler]) assert(subject.many is not subject.many2) assert(one_handler and two_handler in subject.many.handlers) assert(one_handler and two_handler in subject.many2.handlers)
def test_events_with_same_handler_attached(): subject = Observable() subject.on(['one', 'one2'], one_handler) assert(subject.one is not subject.one2) assert(one_handler in subject.one.handlers) assert(one_handler in subject.one2.handlers)
def __init__(self, category, drawCallback=None): SimObject.__init__(self, category, drawCallback) Observable.__init__(self) self.gid = entityManager.nextGID(self)
def __init__(self,grid): Observable.__init__(self) self.grid = grid self.territory = {}
############################################################################### # Build the model ############################################################################### vocsize = len(corpora.vocabulary) print('Vocabulary size:', vocsize) print('Creating learner...') learner = get_learner(args, vocsize) print(f'Number of parameters: {learner.get_num_parameters()/1e6:1.2f}M') if args.cuda: learner.cuda() if args.load_from: learner.load_from(torch.load(args.load_from)) domain_switched = Observable() timestep_updated = Observable() learner.train_mode() if args.log_weights: weight_logger = log_utils.WeightsLogger(learner, args.log_weights) domain_switched.register(weight_logger.domain_switched) timestep_updated.register(weight_logger.timestep_updated) ############################################################################### # Training code ############################################################################### def generate_text(sequence_id, position, sequence_type, sequence_type_name): learner.evaluate_mode() input = Variable(torch.rand(1, 1).mul(len(corpora.vocabulary)).long(),