Example #1
0
def action_test_1():
    shape = (20, 20)
    d_rotx = diffeo_from_function(shape, rotx)

    d1 = Diffeomorphism2D(d_rotx)
    d2 = Diffeomorphism2D(diffeo_inverse(d_rotx))
    a1 = Action(d1, label='rotx', primitive=True,
                invertible=False, original_cmd='rotx')
    a2 = Action(d2, label='rotx_inv', primitive=True,
                invertible=False, original_cmd='rotx_inv')

    assert_allclose(Action.similarity(a1, a1), +1)
    assert_allclose(Action.similarity(a1, a2), -1)
    assert_allclose(Action.similarity(a2, a1), -1)
    assert_allclose(Action.similarity(a2, a2), +1)
Example #2
0
def action_compress_test_1():
    shape = (20, 20)
    d_rotx = diffeo_from_function(shape, rotx)
    d1 = Diffeomorphism2D(d_rotx)
    d2 = Diffeomorphism2D(diffeo_inverse(d_rotx))
    a1 = Action(d1, label='rotx', primitive=True,
                invertible=False, original_cmd='rotx')
    a2 = Action(d2, label='rotx_inv', primitive=True,
                invertible=False, original_cmd='rotx_inv')

    threshold = 0.999

    actions2, info = actions_compress([a1], threshold)  # @UnusedVariable
    assert_allclose(len(actions2), 1)
    assert(not actions2[0].invertible)

    actions2, info = actions_compress([a1, a2], threshold)  # @UnusedVariable
    assert_allclose(len(actions2), 1)
    assert(actions2[0].invertible)
def similarity_analysis(global_options, data, args):  # @UnusedVariable
    usage = ""
    parser = OptionParser(usage=usage)
    parser.disable_interspersed_args()
    parser.add_option("--template", default="lena.jpg")
    (options, args) = parser.parse_args(args)
    if args:
        raise Exception('Extra args')

    if not os.path.exists(options.template):
        raise Exception('Template %r not found.' % options.template)

    cmds = data['actions']
    cmds = sorted(cmds, key=lambda x: '%s' % x.original_cmd)
    publisher = data['publisher']

    print('Compressing %d actions' % len(cmds))
    cmds2, info2 = actions_compress(cmds, threshold=0.9974)  # @UnusedVariable
    describe_info(publisher.section('cmd2'), cmds, info2)

    print('After compressing, we have %d actions' % len(cmds2))
    cmds3 = actions_commutators(cmds2)
    print('With commutators, we have %d actions' % len(cmds3))
    cmds4 = actions_remove_similar_to_identity(cmds3, threshold=0.01)
    print('After removing similar, we have %s' % len(cmds4))
    print('Now compressing commutators...')
    cmds5, info5 = actions_compress(cmds3, threshold=0.9974)  # @UnusedVariable
    print('After compressing, we have %s' % len(cmds5))

    # load template
    template_name = options.template
    template = imread(template_name)
    example_diffeo = cmds[0].diffeo.d
    width = example_diffeo.shape[1]  # note inverted
    height = example_diffeo.shape[0]
    template = resize(template, width, height)
    publisher.array_as_image('template', template)

    for cmd in cmds:
        break
        if not cmd.primitive:
            continue
        print('Plotting %s' % cmd.label)
        section_name = '%s-%s-%s' % (cmd, cmd.label, cmd.original_cmd)
        s = publisher.section(section_name)
        d = cmd.diffeo.d
        d2 = diffeo_compose(d, d)
        d4 = diffeo_compose(d2, d2)
        d8 = diffeo_compose(d4, d4)
        e = diffeo_inverse(d)
        e2 = diffeo_compose(e, e)
        e4 = diffeo_compose(e2, e2)
        e8 = diffeo_compose(e4, e4)

        def show(x):
            return diffeo_apply(x, template)

        s.array_as_image('e8', show(e8))
        s.array_as_image('e4', show(e4))
        s.array_as_image('e2', show(e2))
        s.array_as_image('e', show(e))
        s.array_as_image('d', show(d))
        s.array_as_image('d2', show(d2))
        s.array_as_image('d4', show(d4))
        s.array_as_image('d8', show(d8))
Example #4
0
def similarity_analysis(global_options, data, args):  # @UnusedVariable
    usage = ""
    parser = OptionParser(usage=usage)
    parser.disable_interspersed_args()
    parser.add_option("--template", default="lena.jpg")
    (options, args) = parser.parse_args(args)
    if args:
        raise Exception('Extra args')

    if not os.path.exists(options.template):
        raise Exception('Template %r not found.' % options.template)

    cmds = data['actions']
    cmds = sorted(cmds, key=lambda x: '%s' % x.original_cmd)
    publisher = data['publisher']

    print('Compressing %d actions' % len(cmds))
    cmds2, info2 = actions_compress(cmds, threshold=0.9974)  # @UnusedVariable
    describe_info(publisher.section('cmd2'), cmds, info2)

    print('After compressing, we have %d actions' % len(cmds2))
    cmds3 = actions_commutators(cmds2)
    print('With commutators, we have %d actions' % len(cmds3))
    cmds4 = actions_remove_similar_to_identity(cmds3, threshold=0.01)
    print('After removing similar, we have %s' % len(cmds4))
    print('Now compressing commutators...')
    cmds5, info5 = actions_compress(cmds3, threshold=0.9974)  # @UnusedVariable
    print('After compressing, we have %s' % len(cmds5))

    # load template
    template_name = options.template
    template = imread(template_name)
    example_diffeo = cmds[0].diffeo.d
    width = example_diffeo.shape[1]  # note inverted
    height = example_diffeo.shape[0]
    template = resize(template, width, height)
    publisher.array_as_image('template', template)

    for cmd in cmds:
        break
        if not cmd.primitive:
            continue
        print('Plotting %s' % cmd.label)
        section_name = '%s-%s-%s' % (cmd, cmd.label, cmd.original_cmd)
        s = publisher.section(section_name)
        d = cmd.diffeo.d
        d2 = diffeo_compose(d, d)
        d4 = diffeo_compose(d2, d2)
        d8 = diffeo_compose(d4, d4)
        e = diffeo_inverse(d)
        e2 = diffeo_compose(e, e)
        e4 = diffeo_compose(e2, e2)
        e8 = diffeo_compose(e4, e4)

        def show(x):
            return diffeo_apply(x, template)

        s.array_as_image('e8', show(e8))
        s.array_as_image('e4', show(e4))
        s.array_as_image('e2', show(e2))
        s.array_as_image('e', show(e))
        s.array_as_image('d', show(d))
        s.array_as_image('d2', show(d2))
        s.array_as_image('d4', show(d4))
        s.array_as_image('d8', show(d8))