def popcode(y, resolution):
    N = y.shape[0]
    pc = np.zeros((N, resolution), 'float32')

    for i in range(N):
        # assert 0 <= y[i] <= 1

        # y in [0, 1]
        x = y[i] * (resolution - 1)
        # x in [0, res-1]
        l = np.floor(x)  # l in [0, res-1]
        u = np.ceil(x)  # u in [0, res-1]

        if l == u:
            pc[i, l] = 1
            continue

        lr = x - l
        if False:
            ur = u - x
            assert_allclose(lr + ur, 1)

        pc[i, l] = 1 - lr
        pc[i, u] = lr

    return pc
def test_encryption():
    password = '******'
    for nbits in [8, 16, 20]:
        bits = np.round(np.random.rand(nbits))
        msg = bits_encrypt(bits, password)
        bits2 = bits_decrypt(msg, password, len(bits))
        assert_allclose(bits, bits2)
def test_popcode_inv():
    for _, a, b in examples:
        b = np.array([b]).astype('float32')
        a2 = popcode_inv(b)
        # print(' a = %s' % a)
        # print('a2 = %s' % a2)
        # print(' b = %s' % b)
        assert_allclose(a2[0], a)
def test_popcode():
    for resolution, a, b in examples:
        a = np.array([a]).astype('float32')
        b2 = popcode(a, resolution)
        # print(' a = %s' % a)
        # print(' b = %s' % b)
        # print('b2 = %s' % b2)
        assert_allclose(b2[0], b)
def check_scale_score_variants_test():
    print('Original scale_score')
    v = data['v5']
    x0 = scale_score(v)
    check_scale_score(v, x0)
    
    for id_algo, algo in algos.items():
        print('Variant %s' % id_algo)
        x = algo(v)
        check_scale_score(v, x)
        assert_allclose(x, x0)
def test_popcode1():
    resolution = 10
    N = 5
    for resolution in [2, 5, 10, 20]:
        a = np.random.rand(N)
        b = popcode(a, resolution=resolution)
        a2 = popcode_inv(b)

        # print('---')
        # print(' a = %s' % a)
        # print(' b = %s' % b)
        # print('a2 = %s' % a2)

        assert b.shape == (N, resolution)
        assert a2.shape == a.shape
        assert_allclose(a2, a)
def check_logs_formats(id_agent, agent, id_robot, robot):  # @UnusedVariable

    with create_tmp_dir() as root:
        os.mkdir(os.path.join(root, 'config'))
        data_central = DataCentral(root)

        # Simulate two episodes
        # NO! there is a bug in bag reading; the messages are read
        # in timestamp order; and for now different episodes can
        # have overlapping timestamps
        try:
            simulate(data_central, id_agent=id_agent, id_robot=id_robot,
                 max_episode_len=2,
                 num_episodes=1,  # changed from 2 (see above)
                 cumulative=False,
                 id_episodes=None,
                 stateful=False,
                 interval_print=None,
                 write_extra=True)
        except UnsupportedSpec:
            return

        log_index = data_central.get_log_index()
        log_index.reindex()

        streams = log_index.get_streams_for(id_robot, id_agent)
        if len(streams) != 1:
            msg = 'Expected to find 1 stream, not %d' % len(streams)
            raise Exception(msg)

        stream_orig = streams[0]

        for logs_format, interface in LogsFormat.formats.items():
            try:
                dirname = os.path.join(root, logs_format)
                safe_makedirs(dirname)
                filename = os.path.join(dirname, 'example.%s' % logs_format)
                written = []
                id_stream = 'example'
                with interface.write_stream(filename, id_stream,
                                            robot.get_spec()) as writer:
                    for observations in stream_orig.read():
                        logger.info('Writing %s:%s (%s)' % 
                              (observations['id_episode'],
                               observations['counter'],
                               observations['timestamp']))
                        writer.push_observations(observations)
                        written.append(observations)

                count = 0
                for obs_read in interface.read_from_stream(filename,
                                                           id_stream):
                    logger.info('Reading %s:%s (%s)' % 
                          (obs_read['id_episode'],
                           obs_read['counter'],
                           obs_read['timestamp']))

                    original = written[count]

                    try:
                        if obs_read['counter'] != original['counter']:
                            msg = ('Not even the counter is the same!'
                                   ' %s vs %s' % 
                                   (obs_read['counter'], original['counter']))
                            raise Exception(msg)

                        assert_allclose(obs_read['timestamp'],
                                        original['timestamp'])
                        assert_allclose(obs_read['observations'],
                                        original['observations'])
                        assert_allclose(obs_read['commands'],
                                        original['commands'])
                    except:
                        logger.error('Error at count = %d' % count)
                        logger.error('  original: %s' % original)
                        logger.error('  obs_read: %s' % obs_read)
                        raise
                    count += 1

                if count != len(written):
                    msg = ('I wrote %d entries, but obtained %d.' % 
                           (len(written), count))
                    raise Exception(msg)
            except:
                logger.error('Could not pass tests for format %r.'
                             % logs_format)
                raise
def check_conversions(stream_spec1, nuisance):
    # print('Checking %s / %s ' % (stream_spec1, nuisance))
    nuisance_inv = None
    try:
        try:
            stream_spec2 = nuisance.transform_spec(stream_spec1)
        except UnsupportedSpec as e:
            logger.info('Skipping %s/%s because incompatible: %s' % 
                        (stream_spec1, nuisance, e))
            return

        value1 = stream_spec1.get_random_value()
        stream_spec1.check_valid_value(value1)


        value2 = nuisance.transform_value(value1)
        stream_spec2.check_valid_value(value2)

        try:
            nuisance_inv = nuisance.inverse()
        except NuisanceNotInvertible as e:
            logger.info('Skipping some tests %s/%s because not invertible:'
                        ' %s' % (stream_spec1, nuisance, e))
            return

        try:
            stream_spec1b = nuisance_inv.transform_spec(stream_spec2)
        except UnsupportedSpec as e:
            msg = ('The inverse of the nuisance does not seem to be able '
                   'to handle the result:\n%s\n\n'
                   '  stream_spec1: %s\n'
                   '      nuisance: %s\n'
                   '  stream_spec2: %s\n'
                   '  nuisance_inv: %s\n' % 
                   (indent(str(e), '>'),
                    stream_spec1.to_yaml(), nuisance,
                    stream_spec2.to_yaml(), nuisance_inv))
            raise ValueError(msg)

        try:
            StreamSpec.check_same_spec(stream_spec1, stream_spec1b)
        except Exception as e:
            msg = ('The inverse of the nuisance does not recreate the '
                   'initial spec:\n%s\n\n'
                   '  stream_spec1: %s\n'
                   '      nuisance: %s\n'
                   '  stream_spec2: %s\n'
                   '  nuisance_inv: %s\n'
                   ' stream_spec1b: %s\n' % 
                   (indent(str(e), '>'),
                    stream_spec1.to_yaml(), nuisance,
                    stream_spec2.to_yaml(), nuisance_inv,
                    stream_spec1b.to_yaml()))
            raise ValueError(msg)

        value1b = nuisance_inv.transform_value(value2)
        stream_spec1.check_valid_value(value1b)

        # TODO: if exact
        assert_allclose(value1, value1b, rtol=1e-5)

    except:
        logger.error('Error while testing:')
        logger.error(' stream_spec:  %s ' % stream_spec1.to_yaml())
        logger.error(' nuisance:     %s' % nuisance)
        logger.error(' nuisance_inv: %s' % nuisance_inv)
        raise
Example #9
0
def check_gray_code(code):
    for i in range(len(code) - 1):
        w1 = code[i]
        w2 = code[i + 1]
        dist = np.sum(np.abs(w1 - w2))
        assert_allclose(dist, 1)
 def scipy_image_resample_test4(self):
     """ Resampling should be invertible if using order = 0 """
     a = np.random.rand(10, 10).astype('float32')
     b = scipy_image_resample(a, (20, 20), order=0)
     a1 = scipy_image_resample(b, (10, 10), order=0)
     assert_allclose(a1, a)
def check_cmdline(id_agent, agent, id_robot, robot):  # @UnusedVariable
    try:
        agent.init(robot.get_spec())
    except UnsupportedSpec:
        return
    
    with create_tmp_dir() as root:
        os.mkdir(os.path.join(root, 'config'))  # XXX make it automatic
        data_central = DataCentral(root)
        log_index = data_central.get_log_index()

        def execute_command(*args):
            arguments = ['-d', root, '--contracts'] + list(args)
            boot_olympics_manager(arguments)

        assert not log_index.has_streams_for_robot(id_robot)
        formats = LogsFormat.formats.keys()

        for logs_format in formats:
            execute_command('--logformat', logs_format,
                            'simulate', '-a', id_agent, '-r', id_robot,
                             '--num_episodes', '2', '--episode_len', '2')
            execute_command('--logformat', logs_format,
                            'simulate', '-a', id_agent, '-r', id_robot,
                             '--num_episodes', '2', '--episode_len', '2')

        assert not log_index.has_streams_for_robot(id_robot)
        log_index.reindex()
        assert log_index.has_streams_for_robot(id_robot)
        n = len(formats)
        assert_allclose(len(log_index.get_streams_for_robot(id_robot)), 2 * n)
        assert_allclose(len(log_index.get_streams_for(id_robot,
                                                      id_agent)), 2 * n)
        assert_allclose(len(log_index.get_episodes_for_robot(id_robot)), 4 * n)
        assert_allclose(len(log_index.get_episodes_for_robot(id_robot,
                                                             id_agent)), 4 * n)

        execute_command('learn-log', '-a', id_agent, '-r', id_robot)

        
        try:
            agent.get_servo()
        except NotImplementedError:
            pass
        else:         
            execute_command('servo', '-a', id_agent, '-r', id_robot,
                                '--num_episodes', '1',
                                '--max_episode_len', '1')

        try:
            agent.get_predictor()
        except NotImplementedError:
            pass
        else:         
            execute_command('predict', '-a', id_agent, '-r', id_robot)
    
        execute_command('list-logs')
        execute_command('list-logs', '-e')
        execute_command('list-logs', '-l')
        execute_command('list-logs', '-s')

        execute_command('list-logs', '-R')

        # TODO: publish
        # execute_command('batch') # TODO: test batch 

        execute_command('list-agents')
        execute_command('list-agents', '-v')
        execute_command('list-robots')
        execute_command('list-robots', '-v')
        execute_command('list-states')
        execute_command('list-states', '-v')