Exemple #1
0
    def __init__(self, src_vocab_size, trg_vocab_size, embed_size, hidden_size, latent_size, num_layers, dpt=0.3, share_params=False):
        super(CVAE, self).__init__()
        self.if_zero = False
        self.share_params = share_params
        
        self.src_embedding = nn.Embedding(src_vocab_size, embed_size)
        self.trg_embedding = nn.Embedding(trg_vocab_size, embed_size)
        self.src_embedding.weight.data.copy_((torch.rand(src_vocab_size, embed_size) - 0.5) * 2)
        self.trg_embedding.weight.data.copy_((torch.rand(trg_vocab_size, embed_size) - 0.5) * 2)

        if share_params:
            self.shared_encoder = encoder.SharedEncoder(embed_size, hidden_size, num_layers, dpt)
        else:
            self.src_encoder_t = encoder.Encoder(src_vocab_size, embed_size, hidden_size, num_layers, dpt, self.src_embedding)
            self.src_encoder_i = encoder.Encoder(src_vocab_size, embed_size, hidden_size, num_layers, dpt, self.src_embedding)
            self.trg_encoder = encoder.Encoder(trg_vocab_size, embed_size, hidden_size, num_layers, dpt, self.trg_embedding)
        
        #self.decoder = decoder.BasicDecoder(trg_vocab_size, embed_size, hidden_size, latent_size, num_layers, dpt, word_dpt, self.trg_embedding)
        self.decoder = decoder.BasicAttentionDecoder(trg_vocab_size, embed_size, 2 * hidden_size, latent_size, num_layers, dpt, self.trg_embedding)
        #self.decoder = decoder.DummyDecoder(trg_vocab_size, embed_size, hidden_size, latent_size, num_layers, dpt, self.trg_embedding)
        #self.decoder = decoder.BahdanauAttnDecoder(trg_vocab_size, embed_size, hidden_size, latent_size, num_layers, dpt, self.trg_embedding)
        
        self.p = inferer.Prior(hidden_size, latent_size, dpt)
#         self.p = inferer.SelfAttentionPrior(hidden_size, latent_size, dpt)
        #self.q = inferer.ApproximatePosterior(hidden_size, latent_size, dpt)
        self.q = inferer.LSTMAttentionApproximatePosterior(hidden_size, latent_size, dpt)
    def testStoreMultipleEncodings(self):
        context = StorageOnlyContext()
        cache = encoder.EncodingMemoryCache(context)
        # This particular test needs the context to know about the cache.
        context.cache = cache
        my_encoder = encoder.Encoder(
            context, encoder.OptionValueSet(encoder.OptionSet(),
                                            '--parameters'))
        cache.StoreEncoder(my_encoder)
        videofile = encoder.Videofile('x/foo_640_480_20.yuv')
        my_encoding = encoder.Encoding(my_encoder, 123, videofile)

        testresult = {'foo': 'bar'}
        my_encoding.result = testresult
        cache.StoreEncoding(my_encoding)
        my_encoding = encoder.Encoding(my_encoder, 246, videofile)
        my_encoding.result = testresult
        cache.StoreEncoding(my_encoding)
        result = cache.AllScoredRates(my_encoder, videofile)
        self.assertEquals(2, len(result))
        result = cache.AllScoredEncodings(123, videofile)
        self.assertEquals(1, len(result))
        # Verify that it's working correctly with a new videofile object.
        videofile2 = encoder.Videofile(videofile.filename)
        result = cache.AllScoredEncodings(123, videofile2)
        my_encoding = encoder.Encoding(my_encoder, 123, videofile2)
        self.assertTrue(cache.ReadEncodingResult(my_encoding))
        # Verify that it's working correctly with an encoder created via hashname.
        encoder2 = encoder.Encoder(context, filename=my_encoder.Hashname())
        encoding2 = encoder2.Encoding(123, videofile2)
        self.assertTrue(cache.ReadEncodingResult(encoding2))
Exemple #3
0
 def testEncodersInMultipleRepos(self):
     test_tools.EmptyWorkDirectory()
     context = StorageOnlyContext()
     cache = encoder.EncodingDiskCache(context)
     context.cache = cache
     my_encoder = encoder.Encoder(
         context, encoder.OptionValueSet(encoder.OptionSet(),
                                         '--parameters'))
     other_dir = os.path.join(encoder_configuration.conf.sysdir(),
                              'multirepo_test')
     os.mkdir(other_dir)
     other_cache = encoder.EncodingDiskCache(context,
                                             scoredir='multirepo_test')
     encoder_configuration.conf.override_scorepath_for_test([other_dir])
     other_cache.StoreEncoder(my_encoder)
     encoders = cache.AllEncoderFilenames(only_workdir=True)
     self.assertEquals(0, len(encoders))
     encoders = cache.AllEncoderFilenames(only_workdir=False)
     self.assertEquals(1, len(encoders))
     fetched_encoder = encoder.Encoder(context, filename=encoders[0])
     self.assertEquals(my_encoder.parameters.ToString(),
                       fetched_encoder.parameters.ToString())
     my_encoding = encoder.Encoding(
         my_encoder, 123, encoder.Videofile('x/foo_640_480_20.yuv'))
     testresult = {'foo': 'bar'}
     my_encoding.result = testresult
     other_cache.StoreEncoding(my_encoding)
     # With a specified directory, we should find it in only one place.
     self.assertTrue(other_cache.ReadEncodingResult(my_encoding))
     # Without a specified directory, we should find it on the searchpath.
     self.assertTrue(cache.ReadEncodingResult(my_encoding))
     # Without a searchpath, we shouldn't find it in the default cache.
     encoder_configuration.conf.override_scorepath_for_test([])
     self.assertFalse(cache.ReadEncodingResult(my_encoding))
Exemple #4
0
 def testInitFromFile(self):
     context = encoder.Context(DummyCodec())
     my_encoder = encoder.Encoder(
         context, encoder.OptionValueSet(encoder.OptionSet(),
                                         '--parameters'))
     my_encoder.Store()
     new_encoder = encoder.Encoder(context, filename=my_encoder.Hashname())
     self.assertEquals(new_encoder.parameters, my_encoder.parameters)
Exemple #5
0
def task5_read_encoder():
    enc1 = enc.Encoder("A")
    enc2 = enc.Encoder("B")
    while (1):
        ## The shared variable q8 holds the current position of the pitch motor
        q8.put(enc1.read())
        ## The shared variable q9 holds the current position of the roll motor
        q9.put(enc2.read())
        yield (0)
Exemple #6
0
 def test_ParametersCanBeStoredAndRetrieved(self):
     context = encoder.Context(DummyCodec())
     my_encoder = encoder.Encoder(
         context, encoder.OptionValueSet(encoder.OptionSet(),
                                         '--parameters'))
     my_encoder.Store()
     filename = my_encoder.Hashname()
     next_encoder = encoder.Encoder(context, filename=filename)
     self.assertEqual(my_encoder.parameters, next_encoder.parameters)
Exemple #7
0
 def testInitFromBrokenFile(self):
     context = encoder.Context(DummyCodec())
     my_encoder = encoder.Encoder(
         context, encoder.OptionValueSet(encoder.OptionSet(),
                                         '--parameters'))
     my_encoder.Store()
     # Break stored object. Note: This uses knowledge of the memory cache.
     old_filename = my_encoder.Hashname()
     parameters = context.cache.encoders[old_filename].parameters
     parameters.other_parts.append('--extra-stuff')
     # Now Hashname() should return a different value.
     with self.assertRaisesRegexp(encoder.Error,
                                  'contains wrong arguments'):
         # pylint: disable=W0612
         new_encoder = encoder.Encoder(context, filename=old_filename)
Exemple #8
0
def task3_closed_loop():
    ''' Function that implements task 3, a task which runs the closed loop motor control '''
    cL = loop.Closed_Loop()
    cL.set_setpoint(4000)

    while True:
        #gain_set = float(inputstr)
        #print(gain_set)
        cL.set_cont_gain(0.045)
        mot1 = motor.MotorDriver()
        enc1 = encoder.Encoder("A")
        enc1.zero()
        last_time = utime.ticks_ms()
        times = []
        positions = []

        for x in range(300):
            #utime.sleep_ms(10)
            times.append(utime.ticks_ms())
            pos = enc1.read()
            positions.append(pos)
            actuation = cL.control(pos)
            mot1.set_duty_cycle(actuation)
            last_time = utime.ticks_ms()
            yield (0)

        starttime = times[0]
        i = 0
        for time in times:
            times[i] = time - starttime
            i += 1

        print(positions)
        print(times)
 def __init__(self,
              input_dim,
              output_dim,
              model_dim,
              n_head,
              key_dim,
              value_dim,
              hidden_dim,
              n_layers,
              pad_idx,
              max_seq_len=200,
              dropout=0.1):
     super(Transformer, self).__init__()
     self.pad_idx = pad_idx
     self.encoder = encoder.Encoder(input_dim=input_dim,
                                    model_dim=model_dim,
                                    n_head=n_head,
                                    key_dim=key_dim,
                                    value_dim=value_dim,
                                    n_layers=n_layers,
                                    hidden_dim=hidden_dim,
                                    max_seq_len=max_seq_len,
                                    dropout=dropout)
     self.decoder = decoder.Decoder(input_dim=input_dim,
                                    model_dim=model_dim,
                                    n_head=n_head,
                                    key_dim=key_dim,
                                    value_dim=value_dim,
                                    n_layers=n_layers,
                                    hidden_dim=hidden_dim,
                                    max_seq_len=max_seq_len,
                                    dropout=dropout)
     self.linear_output = nn.Linear(in_features=model_dim,
                                    out_features=output_dim)
    def testReadResultEncodedInAst(self):
        # We've changed the storage format for results from AST to JSON.
        # This test verifies that AST formatted results are still readable.
        context = StorageOnlyContext()
        cache = encoder.EncodingDiskCache(context)
        my_encoder = encoder.Encoder(
            context, encoder.OptionValueSet(encoder.OptionSet(),
                                            '--parameters'))
        cache.StoreEncoder(my_encoder)
        my_encoding = encoder.Encoding(
            my_encoder, 123, encoder.Videofile('x/foo_640_480_20.yuv'))
        testresult = {'foo': 'bar'}
        my_encoding.result = testresult
        # The following code is a copy of cache.StoreEncoding, with the
        # encoding step changed.
        dirname = '%s/%s/%s' % (cache.workdir, my_encoding.encoder.Hashname(),
                                cache.context.codec.SpeedGroup(
                                    my_encoding.bitrate))
        videoname = my_encoding.videofile.basename
        with open('%s/%s.result' % (dirname, videoname), 'w') as resultfile:
            resultfile.write(str(my_encoding.result))

        my_encoding.result = None
        result = cache.ReadEncodingResult(my_encoding)
        self.assertEquals(result, testresult)
 def StartEncoder(self, context):
     return encoder.Encoder(
         context,
         encoder.OptionValueSet(
             self.option_set,
             '--rc-lookahead 0 --preset faster --tune psnr --threads 4',
             formatter=self.option_formatter))
Exemple #12
0
    def testBrokenStoredEncoding(self):
        context = StorageOnlyContext()
        other_dir = os.path.join(encoder_configuration.conf.sysdir(),
                                 'broken_files')
        os.mkdir(other_dir)

        cache = encoder.EncodingDiskCache(context, scoredir='broken_files')
        # This particular test needs the context to know about the cache.
        context.cache = cache
        my_encoder = encoder.Encoder(
            context, encoder.OptionValueSet(encoder.OptionSet(),
                                            '--parameters'))
        cache.StoreEncoder(my_encoder)
        # Cache should start off empty.
        self.assertFalse(cache.AllScoredEncodingsForEncoder(my_encoder))
        videofile = encoder.Videofile('x/foo_640_480_20.yuv')
        my_encoding = encoder.Encoding(my_encoder, 123, videofile)
        testresult = {'foo': 'bar'}
        my_encoding.result = testresult
        cache.StoreEncoding(my_encoding)
        # TODO(hta): Expose the filename generation as a function for testing.
        with open(
                os.path.join(
                    cache.workdir, my_encoding.encoder.Hashname(),
                    cache.context.codec.SpeedGroup(my_encoding.bitrate),
                    '%s.result' % my_encoding.videofile.basename),
                'w') as scorefile:
            scorefile.write('stuff that is not valid json')

        result = cache.AllScoredEncodingsForEncoder(my_encoder)
        self.assertFalse(result)
        self.assertEquals(1, len(cache.bad_encodings))
Exemple #13
0
    def _create_blocks(self):
        #create the environment-agent interface (decoder)
        self.decoder = decoder.Decoder(self, -1)
        #create the "hippocampus" / state tracer
        self.action_buffer = OrNode(self.network, (self.n_actions, 1), -1)
        self.hippocampus = hippocampus.Hippocampus(self, -1)
        #create the "cortex" / reward estimator
        #create the action encoder
        if self.n_replicates > 1:
            self.cortex = cortex.MultiCortex(self,
                                             -1,
                                             noisy=self.noisy,
                                             n_replicates=self.n_replicates,
                                             dynrange=self.dynrange)
            self.encoder = encoder.MultiEncoder(self, -1)
        else:
            self.cortex = cortex.Cortex(self, -1, noisy=self.noisy)
            self.encoder = encoder.Encoder(self, -1)

        #create stubs for SNIP
        self.stubs['state'] = self.network.createInputStubGroup(
            size=self.n_states)
        self.stubs['action'] = self.network.createInputStubGroup(
            size=self.n_actions)
        self.stubs['reward'] = self.network.createInputStubGroup(size=1)
        self.stubs['punishment'] = self.network.createInputStubGroup(size=1)
        self.stubs['draw'] = self.network.createInputStubGroup(size=1)
Exemple #14
0
def check_collision(clientid, clients):
    decd = encoder.Decoder()
    decd.addData(player_coordinates[clientid])
    decd.processData()
    _, clientx, clienty, clientscore = decd.getData()
    for id in player_coordinates:
        if clientid == id:
            continue

        decd.addData(player_coordinates[id])
        if decd.processData():
            _, x, y, score = decd.getData()

            dim = (30 + math.sqrt(score)) // 2
            clientdim = (30 + math.sqrt(clientscore)) // 2
            dist = ((x + dim) -
                    (clientx + clientdim))**2 + ((y + dim) -
                                                 (clienty + clientdim))**2
            if (dim + clientdim)**2 > dist:
                # Two players collided
                encd = encoder.Encoder()
                if math.sqrt(score) > math.sqrt(clientscore) + 5:
                    # client lost
                    encd.setDeathData()
                    clients[clientid].send(encd.getBytes())
                    encd.setKillData(clientscore)
                    clients[id].send(encd.getBytes())
                elif math.sqrt(clientscore) > math.sqrt(score) + 5:
                    # client won
                    encd.setDeathData()
                    clients[id].send(encd.getBytes())
                    encd.setKillData(score)
                    clients[clientid].send(encd.getBytes())
def Encoder1_fun():
    ''' Function which runs for Task 1, which toggles twice every second in a
    way which is only slightly silly.  '''
    state = STATE_0
    while True:
        ######### STATE 0: Initialize Encoder ###################################
        if state == STATE_0:
            # Init state
            Encoder_1 = encoder.Encoder(4, pyb.Pin.board.PB6,
                                        pyb.Pin.board.PB7)
            state = STATE_1
######### STATE 1: Zero Encoder for Step Response #######################
        elif state == STATE_1:
            # Read encoder and update the shared variable
            Encoder_1.zero_encoder()
            state = STATE_2


######### STATE 2: Get Encoder Values ##################################
        elif state == STATE_2:
            if Run.get():
                # Read encoder and update the shared variable
                enc_1_position.put(Encoder_1.read_encoder())
            else:
                state == STATE_1
        yield (state)
Exemple #16
0
    def __init__(self):
        '''
            初始化串口相关数据
        '''
        port = rospy.get_param('~serial_port', '/dev/ttyUSB0')
        baudrate = rospy.get_param('~serial_baudrate', 460800)
        self.data_io = serial.Serial(port, baudrate, timeout=0.1)

        self.rx_Data = []
        self.tx_Data = []
        self.io_Busy = False  #当前串口是否被占用
        '''
            初始化ros相关数据
        '''
        self.robo_Width = rospy.get_param('~robo_width', 0.265)
        self.enc_pulse_per_meter = rospy.get_param('~pulse_per_meter', 3800)
        rospy.Subscriber('/cmd_vel', Twist, callback=self.SPD)
        rospy.Subscriber('/led', UInt8, callback=self.Set_LED)
        rospy.Service('/get_battery_state', AskBattery, self.Ask_Battery)
        '''
            IO变量
        '''
        self.v_Left = 0
        self.v_Right = 0
        self.encoder = encoder.Encoder()
        '''
            开启IO循环
        '''
        thread.start_new_thread(self.Thread_IO_Loop, ())
    def encode_trace(self, data):
        data_encoder = encoder.Encoder()
        events = data_encoder.get_events(data)
        cases = data_encoder.get_cases(data)

        columns = events
        columns = np.append(
            events, ["case_id", "event_nr", "remaining_time", "elapsed_time"])
        encoded_data = pd.DataFrame(columns=columns)

        i = 0
        for case in cases:
            df = data[data['case_id'] == case]
            for j in range(0, max(df['event_nr'])):
                case_data = []
                event_length = j + 1
                for event in events:
                    case_data.append(
                        len(df[(df['activity_name'] == event)
                               & (df['event_nr'] <= event_length)]) > 0)
                case_data.append(case)
                case_data.append(event_length)
                remaining_time = data_encoder.calculate_remaining_time(
                    df, event_length)
                case_data.append(remaining_time)
                elapsed_time = data_encoder.calculate_elapsed_time(
                    df, event_length)
                case_data.append(elapsed_time)
                encoded_data.loc[i] = case_data
                i = i + 1

        return encoded_data
Exemple #18
0
 def StartEncoder(self, context):
     return encoder.Encoder(
         context,
         encoder.OptionValueSet(
             self.option_set,
             '--profile baseline --preset slow --tune psnr',
             formatter=self.option_formatter))
Exemple #19
0
    def __init__(self, latent_spaces, batch_size):
        super(ANVAE, self).__init__()

        self.batch_size = batch_size
        self.latent_spaces = 3
        self.level_sizes = [1, 1, 1]
        self.input_s = [32, 32, 1]
        self.latent_channels = 20
        self.h_dim = 1000

        self.encoder = encoder.Encoder(self.latent_spaces, self.input_s)
        self.decoder = decoder.Decoder(self.encoder(
            tf.zeros([self.batch_size, 32, 32, 1]), False),
                                       latent_channels=self.latent_channels,
                                       level_sizes=self.level_sizes)
        self.discriminator = discriminator.Discriminator(
            self.latent_spaces, self.input_s, self.h_dim)

        self.lr_ae = .0001
        self.lr_dc = .0001
        self.lr_gen = .0001

        self.ae_optimizer = tf.keras.optimizers.Adamax(self.lr_ae, clipnorm=2)
        self.gen_optimizer = tf.keras.optimizers.Adamax(self.lr_gen,
                                                        clipnorm=2)
        self.dc_optimizer = tf.keras.optimizers.Adamax(self.lr_dc, clipnorm=2)

        self.ae_loss_weight = 1.
        self.gen_loss_weight = 6.
        self.dc_loss_weight = 6.

        self.lastEncVars = []
        self.lastDecVars = []
        self.lastDiscVars = []

        self.debugCount = 0
        self.counter = 1

        self.log_writer = tf.summary.create_file_writer(logdir='./tf_summary')
        self.step_count = 0

        self.conv_layers = []
        self.sr_u = {}
        self.sr_v = {}
        self.num_power_iter = 4

        for layer in self.encoder.layers:
            if isinstance(layer, tf.keras.layers.Conv2D) or isinstance(
                    layer, tf.keras.layers.DepthwiseConv2D):
                self.conv_layers.append(layer)

        for layer in self.decoder.layers:
            if isinstance(layer, tf.keras.layers.Conv2D) or isinstance(
                    layer, tf.keras.layers.DepthwiseConv2D):
                self.conv_layers.append(layer)

        for layer in self.discriminator.layers:
            if isinstance(layer, tf.keras.layers.Conv2D) or isinstance(
                    layer, tf.keras.layers.DepthwiseConv2D):
                self.conv_layers.append(layer)
Exemple #20
0
 def StartEncoder(self, context):
     return encoder.Encoder(
         context,
         encoder.OptionValueSet(
             self.option_set,
             '--rc-lookahead 0 --ref 2 --vbv-init 0.8 --preset veryslow',
             formatter=self.option_formatter))
 def __init__(self, name='vp8'):
     super(Vp8Codec, self).__init__(name)
     self.extension = 'webm'
     self.options = [
         encoder.Option('overshoot-pct', ['0', '15', '30', '45']),
         encoder.Option('undershoot-pct', ['0', '25', '50', '75', '100']),
         # CQ mode is not considered for end-usage at the moment.
         encoder.Option('end-usage', ['cbr', 'vbr']),
         # End-usage cq doesn't really make sense unless we also set q to something
         # between min and max. This is being checked.
         # encoder.Option('end-usage', ['cbr', 'vbr', 'cq']),
         encoder.Option('end-usage', ['cbr', 'vbr']),
         encoder.Option('min-q', ['0', '2', '4', '8', '16', '24']),
         encoder.Option('max-q', ['32', '56', '63']),
         encoder.Option(
             'buf-sz',
             ['200', '500', '1000', '2000', '4000', '8000', '16000']),
         encoder.Option(
             'buf-initial-sz',
             ['200', '400', '800', '1000', '2000', '4000', '8000', '16000'
              ]),
         encoder.Option('max-intra-rate',
                        ['100', '200', '400', '600', '800', '1200']),
         encoder.ChoiceOption(['good', 'best', 'rt']),
     ]
     self.start_encoder = encoder.Encoder(
         self, """ --lag-in-frames=0 \
   --kf-min-dist=3000 \
   --kf-max-dist=3000 --cpu-used=0 --static-thresh=0 \
   --token-parts=1 --drop-frame=0 --end-usage=cbr --min-q=2 --max-q=56 \
   --undershoot-pct=100 --overshoot-pct=15 --buf-sz=1000 \
   --buf-initial-sz=800 --buf-optimal-sz=1000 --max-intra-rate=1200 \
   --resize-allowed=0 --drop-frame=0 --passes=1 --good --noise-sensitivity=0 """
     )
Exemple #22
0
def motor_2 ():
    ''' Define a task to run both motors in order. Configure the motor using the motor_driver.py file. Print the data to the serial port and plot the data in pc_main_lab3'''

    # call class MotorDriver()
    motor_2 = motor_driver.MotorDriver('C1', 20000)

    # call class Controller()
    ctr_2 = controller.Controller(0.01, 16000, 30)

    ## define the encoder that is used to read the motor position
    encC = enc.Encoder('C')

    # define x to be True to collect half of the data points an allow more time for printing data
    x = True

    while True:
        # initialize the motor controller using class Controller()
        motor_2.set_duty_cycle(ctr_2.outputValue(encC.read()))

        # create a string of data to be plotted
        data = '2, ' + str(encC.read()) + ', ' + str(utime.ticks_ms()) + '\r\n'

        # print data every other pass through the while loop
        if x == True:
            print_task.put (data)
        x = not x

        # yield to another task and resume at this line
        yield (0)
Exemple #23
0
def motor_1 ():
    ''' Define a task to run both motors in order. Configure the motor using the motor_driver.py file. Print the data to the serial port and plot the data in pc_main_lab3'''

    # call class MotorDriver()
    motor = motor_driver.MotorDriver('A10', 20000)

    # call class Controller()
    ctr = controller.Controller(0.01, 16000, 30)
    
    ## define the encoder that is used to read the motor position
    encB = enc.Encoder('B')
    
    # run the proportional contorller on the motor and feed the data into the shares and queues using put()
    x = True
    while True:

        # set the motor duty cycle using class Controller()
        motor.set_duty_cycle(ctr.outputValue(encB.read()))

        # create a string of data to be plotted
        data = '1, ' + str(encB.read()) + ', ' + str(utime.ticks_ms()) + '\r\n'

        # print data every other pass through the while loop
        if x == True:
            print_task.put (data)

        # yield to another task and resume at this line
        yield (0)
Exemple #24
0
def write_problems_with_sdrs(input_folder, output_folder):
    """
    Read problems from the input folder
    Encode all the windows using an auto-encoder
    Write the problems with the added encoded windows to the output_folder
    """

    import encoder

    # get all the windows from the problems in the folder
    # in order to train the encoder
    problems = []
    for sub_folder in os.listdir(input_folder):
        f = os.path.join(input_folder, sub_folder)
        problems += get_problems(f)

    input_windows = get_windows(problems)
    (num_windows, height, width) = input_windows.shape

    # initialize and train the encoder
    enc = encoder.Encoder(height * width)
    errs = enc.train(input_windows)
    
    problems = []
    # get all the problems from the folder in order to create the SDRs
    for sub_folder in os.listdir(input_folder):
        problems = get_problems(os.path.join(input_folder, sub_folder))
        for i in range(len(problems)):
            # write the original form of the problem in the new file
            out_file = os.path.join(output_folder, sub_folder, problems[i]['Attributes']['title'] + '.txt')
            write_problem(problems[i], out_file)

            # create and write to the new file SDRs for all the windows in the problem
            with open(out_file, 'a') as f:
                f.write('== SDRs ==\n')
                # for every window in the problem
                for win in np.concatenate((problems[i]['Input'], problems[i]['Output'])):
                    # create SDR by encoding the window and then converting the list of float
                    # values to 1s and 0s
                    encoded = enc.encode(win.reshape((1, height, width, 1)))
                    m = np.mean(encoded)
                    encoded = (encoded >= m).astype(int)

                    # write the SDR to the file
                    size = int(len(encoded[0]) ** 0.5)
                    for line in encoded.reshape((size, size)):
                        f.write(''.join([str(x) for x in line]) + '\n')
                    f.write('\n')

                    print('Sparsity ', float(np.sum(encoded) * 100) / encoded.shape[1])

                    # decode the SDR
                    decoded = enc.decode(encoded)
                    m = np.mean(decoded)
                    decoded = (decoded >= m).astype(int)

                    # print the similarity between the input and the output
                    print(np.sum(win == decoded.reshape(win.shape)) * 100.0 / (height * width))
                    print("---------------")
Exemple #25
0
 def getEncodedData(self, articleKeys=None):
     enc = encoder.Encoder()
     # TODO(jimhug): Only return initially visible section in first reply.
     maxSections = min(MAX_SECTIONS, len(self.sections))
     enc.writeInt(maxSections)
     for section in db.get(self.sections[:maxSections]):
         section.encode(enc, articleKeys)
     return enc.getRaw()
Exemple #26
0
 def test_Changevalue(self):
     config = encoder.OptionValueSet(
         encoder.OptionSet(encoder.Option('foo', ['foo', 'bar'])),
         '--foo=foo')
     context = encoder.Context(DummyCodec())
     my_encoder = encoder.Encoder(context, config)
     next_encoder = my_encoder.ChangeValue('foo', 'bar')
     self.assertEquals(next_encoder.parameters, '--foo=bar')
Exemple #27
0
 def test_ParametersCanChangeMayReturnTrue(self):
     context = encoder.Context(DummyCodec())
     my_encoder = encoder.Encoder(
         context,
         encoder.OptionValueSet(
             encoder.OptionSet(encoder.Option('key', ['value1', 'value2'])),
             '--parameters'))
     self.assertTrue(my_encoder.ParametersCanChange())
def main():
    c = communication.Communication("/dev/ttyUSB0")
    iv = perform_IV_exchange(c)
    Cipher = cipher.Cipher(iv)
    Encoder = encoder.Encoder(Cipher)

    while True:
        communicate(c, Encoder, Cipher)
    def encode_trace(self, data, additional_columns=None, prefix_length=1):
        data_encoder = encoder.Encoder()
        events = data_encoder.get_events(data).tolist()
        cases = data_encoder.get_cases(data)

        init_prefix = prefix_length
        if prefix_length == 0:
            init_prefix = 1
            prefix_length = max(data['event_nr'])
        iteration_max = prefix_length

        columns = []
        columns.append("case_id")
        columns.append("event_nr")
        columns.append("remaining_time")
        columns.append("elapsed_time")
        for i in range(1, prefix_length + 1):
            columns.append("prefix_" + str(i))
            for additional_column in additional_columns:
                columns.append(additional_column + "_" + str(i))

        encoded_data = []

        for case in cases:
            df = data[data['case_id'] == case]

            for event_length in range(init_prefix, prefix_length + 1):
                if len(df) < event_length:
                    continue

                case_data = []
                case_data.append(case)
                case_data.append(event_length)
                remaining_time = data_encoder.calculate_remaining_time(
                    df, event_length)
                case_data.append(remaining_time)
                elapsed_time = data_encoder.calculate_elapsed_time(
                    df, event_length)
                case_data.append(elapsed_time)

                case_events = df[
                    df['event_nr'] <= event_length]['activity_name'].tolist()
                for index in range(0, iteration_max):
                    if index < len(case_events):
                        case_data.append(case_events[index])
                        for additional_column in additional_columns:
                            event_attribute = df[df['event_nr'] == (
                                index +
                                1)][additional_column].apply(str).item()
                            case_data.append(event_attribute)
                    else:
                        case_data.append(0)
                        for additional_column in additional_columns:
                            case_data.append(0)
                encoded_data.append(case_data)

        df = pd.DataFrame(columns=columns, data=encoded_data)
        return df
Exemple #30
0
def motor2_fun():

    # A motor object
    pinENA = pyb.Pin(pyb.Pin.board.PC1, pyb.Pin.OUT_PP)
    pinIN1A = pyb.Pin(pyb.Pin.board.PA0, pyb.Pin.OUT_PP)
    pinIN2A = pyb.Pin(pyb.Pin.board.PA1, pyb.Pin.OUT_PP)
    mot = motor.MotorDriver([pinIN1A, pinIN2A, pinENA], 5, [1, 2])
    # An encoder object
    enc = encoder.Encoder([pyb.Pin.board.PC6, pyb.Pin.board.PC7], 8, [1, 2])
    # A controller object
    cont = controller.Controller(K_p=0.10)

    setup = True
    state = 0

    while (True):

        # Stopped
        if state == 0:
            if pos_ctrl2.get():
                state = 1
            elif step_rsp2.get():
                state = 2
            elif get_data2.get():
                state = 3
            else:
                mot.set_duty_cycle(0)

        # Position Control
        elif state == 1:
            if pos_ctrl2.get():
                cont.set_setpoint(setpoint1.get())
                mot.set_duty_cycle(cont.run(enc.read()))
            else:
                state = 0

        # Step Response
        elif state == 2:
            if setup:
                cont.clear_data()
                cont.set_setpoint(setpoint1.get())
                enc.zero()
                stop = utime.ticks_add(utime.ticks_ms(), 1000)
                setup = False
            elif utime.ticks_diff(stop, utime.ticks_ms()) > 0:
                mot.set_duty_cycle(cont.run(enc.read()))
            else:
                step_rsp2.put(False)
                setup = True
                state = 0

        # Get Data
        elif state == 3:
            for datum in cont.get_data():
                print(str(datum[0]) + ', ' + str(datum[1]))
            state = 0

        yield (state)