<commit_before>#! /usr/bin/env python
###############################################################################
import nose
import sys
import io
import random
import os
import glob
import subprocess
import numpy.testing

from mann import agent

here = os.path.abspath(os.path.dirname(__file__))


###############################################################################
# Unit tests for the LensAgent class
###############################################################################
def reset_LensAgent():
    agent.LensAgent.prototypes = [[0, 1, 1, 0]]
    agent.LensAgent.agent_count = 0


def reset_LensAgent_20():
    reset_LensAgent()
    agent.LensAgent.prototypes = [[0, 1, 0, 0, 0, 0, 1, 1, 0, 0,
                                   1, 1, 0, 0, 1, 0, 0, 1, 0, 1]]


@nose.with_setup(reset_LensAgent)
def test_lens_agent_get_key_single():
    test_lens_agent = agent.LensAgent(4)
    assert test_lens_agent.get_key() == 0


@nose.with_setup(reset_LensAgent)
def test_lens_agent_get_key_multiple():
    list_of_test_agents = []
    for i in range(10):
        test_multiple_lens_agent = agent.LensAgent(4)
        list_of_test_agents.append(test_multiple_lens_agent)
    assert list_of_test_agents[0].get_key() == 0
    assert list_of_test_agents[-1].get_key() == 9


@nose.with_setup(reset_LensAgent)
def test_lens_agent_state_init():
    test_lens_agent = agent.LensAgent(4)
    assert test_lens_agent.get_state() == [0] * 4


@nose.with_setup(reset_LensAgent)
def test_lens_agent_set_get_state():
    test_lens_agent = agent.LensAgent(4)
    test_lens_agent.set_state([1, 2, 3, 4])
    assert test_lens_agent.get_state() == [1, 2, 3, 4]

    try:
        test_lens_agent.set_state([1, 2])
    except ValueError:
        assert test_lens_agent.get_state() == [1, 2, 3, 4]
        assert True
    else:
        assert False


@nose.with_setup(reset_LensAgent)
def test_list_to_str_delim():
    test_lens_agent = agent.LensAgent(4)
    expected_string = "1 3 5"
    output_string = test_lens_agent._list_to_str_delim([1, 3, 5], delim=" ")
    assert output_string == expected_string


@nose.with_setup(reset_LensAgent_20)
def test_create_weight_file():
    test_lens_agent = agent.LensAgent(20)
    assert test_lens_agent.get_key() == 0

    search_file = os.path.join(here, 'lens', 'weights', 'AgentWgt000000.wt')
    if os.path.exists(search_file):
        globed = glob.glob(search_file)
        print('glob: ', globed)
        os.remove(globed[0])

    # where I want the weight file saved
    weight_output_dir = os.path.join(here, 'lens', 'weights')
    # where the .in file to create weights is
    weight_in_file = os.path.join(here, 'lens', 'AutoEncoderArch.in')

    prototype = agent.LensAgent.prototypes[0]
    assert(isinstance(prototype, list))

    # create the weight file
    test_lens_agent.create_weight_file(weight_in_file,
                                       weight_output_dir,
                                       prototype,
                                       50, 0, 3)

    # search directory for weight file
    expected_weight_file_name = os.path.join(here, 'lens', 'weights',
                                             'wgt000000.wt')
    assert os.path.exists(expected_weight_file_name) is True


@nose.with_setup(reset_LensAgent_20)
def test_lens_agent_seed_agent_no_update():
    test_lens_agent = agent.LensAgent(20)
    assert test_lens_agent.get_state() == [0] * 20
    weight_base_example = '0, 1, 0, 0, 0, 0, 1, 1, 0, 0,' +\
                          '1, 1, 0, 0, 1, 0, 0, 1, 0, 1'
    test_lens_agent.seed_agent_no_update(weight_base_example)

    expected_state = [0, 1, 0, 0, 0, 0, 1, 1, 0, 0,
                      1, 1, 0, 0, 1, 0, 0, 1, 0, 1]
    assert(test_lens_agent.get_state() == expected_state)


def test_lens_agent_seed():
    test_lens_agent = agent.LensAgent(4)
    assert test_lens_agent.get_state() == [0] * 4
    test_lens_agent.seed_agent()
    assert test_lens_agent.get_state() == [1] * 4


@nose.with_setup(reset_LensAgent)
def test_update_agent_state_default():
    pass


@nose.with_setup(reset_LensAgent)
def test_update_agent_state():
    list_of_predecessors = []
    for i in range(3):
        lens_agent_predecessor = agent.LensAgent(10)
        list_of_predecessors.append(lens_agent_predecessor)
    test_lens_agent = agent.LensAgent(10)
    assert test_lens_agent.get_key() == 3

    test_lens_agent.set_predecessors(list_of_predecessors)
    assert test_lens_agent.predecessors[0].get_key() == 0
    assert test_lens_agent.predecessors[-1].get_key() == 2

    here = os.path.abspath(os.path.dirname(__file__))
    lens_in_file_dir = here + '/' + 'lens/UpdateFromInfl.in'<commit_msg>fix seed function, but needs a way to seed the RNG<commit_after>#! /usr/bin/env python
###############################################################################
import nose
import sys
import io
import random
import os
import glob
import subprocess
import numpy.testing

from mann import agent

here = os.path.abspath(os.path.dirname(__file__))


###############################################################################
# Unit tests for the LensAgent class
###############################################################################
def reset_LensAgent():
    agent.LensAgent.prototypes = [[0, 1, 1, 0]]
    agent.LensAgent.agent_count = 0


def reset_LensAgent_20():
    reset_LensAgent()
    agent.LensAgent.prototypes = [[0, 1, 0, 0, 0, 0, 1, 1, 0, 0,
                                   1, 1, 0, 0, 1, 0, 0, 1, 0, 1]]


@nose.with_setup(reset_LensAgent)
def test_lens_agent_get_key_single():
    test_lens_agent = agent.LensAgent(4)
    assert test_lens_agent.get_key() == 0


@nose.with_setup(reset_LensAgent)
def test_lens_agent_get_key_multiple():
    list_of_test_agents = []
    for i in range(10):
        test_multiple_lens_agent = agent.LensAgent(4)
        list_of_test_agents.append(test_multiple_lens_agent)
    assert list_of_test_agents[0].get_key() == 0
    assert list_of_test_agents[-1].get_key() == 9


@nose.with_setup(reset_LensAgent)
def test_lens_agent_state_init():
    test_lens_agent = agent.LensAgent(4)
    assert test_lens_agent.get_state() == [0] * 4


@nose.with_setup(reset_LensAgent)
def test_lens_agent_set_get_state():
    test_lens_agent = agent.LensAgent(4)
    test_lens_agent.set_state([1, 2, 3, 4])
    assert test_lens_agent.get_state() == [1, 2, 3, 4]

    try:
        test_lens_agent.set_state([1, 2])
    except ValueError:
        assert test_lens_agent.get_state() == [1, 2, 3, 4]
        assert True
    else:
        assert False


@nose.with_setup(reset_LensAgent)
def test_list_to_str_delim():
    test_lens_agent = agent.LensAgent(4)
    expected_string = "1 3 5"
    output_string = test_lens_agent._list_to_str_delim([1, 3, 5], delim=" ")
    assert output_string == expected_string


@nose.with_setup(reset_LensAgent_20)
def test_create_weight_file():
    test_lens_agent = agent.LensAgent(20)
    assert test_lens_agent.get_key() == 0

    search_file = os.path.join(here, 'lens', 'weights', 'AgentWgt000000.wt')
    if os.path.exists(search_file):
        globed = glob.glob(search_file)
        print('glob: ', globed)
        os.remove(globed[0])

    # where I want the weight file saved
    weight_output_dir = os.path.join(here, 'lens', 'weights')
    # where the .in file to create weights is
    weight_in_file = os.path.join(here, 'lens', 'AutoEncoderArch.in')

    prototype = agent.LensAgent.prototypes[0]
    assert(isinstance(prototype, list))

    # create the weight file
    test_lens_agent.create_weight_file(weight_in_file,
                                       weight_output_dir,
                                       prototype,
                                       50, 0, 3)

    # search directory for weight file
    expected_weight_file_name = os.path.join(here, 'lens', 'weights',
                                             'wgt000000.wt')
    assert os.path.exists(expected_weight_file_name) is True


@nose.with_setup(reset_LensAgent_20)
def test_lens_agent_seed_agent_no_update():
    test_lens_agent = agent.LensAgent(20)
    assert test_lens_agent.get_state() == [0] * 20
    weight_base_example = '0, 1, 0, 0, 0, 0, 1, 1, 0, 0,' +\
                          '1, 1, 0, 0, 1, 0, 0, 1, 0, 1'
    test_lens_agent.seed_agent_no_update(weight_base_example)

    expected_state = [0, 1, 0, 0, 0, 0, 1, 1, 0, 0,
                      1, 1, 0, 0, 1, 0, 0, 1, 0, 1]
    assert(test_lens_agent.get_state() == expected_state)


@nose.with_setup(reset_LensAgent_20)
def test_lens_agent_seed():
    test_lens_agent = agent.LensAgent(20)
    assert test_lens_agent.get_state() == [0] * 20

    expected_prototype = [0, 1, 0, 0, 0, 0, 1, 1, 0, 0,
                          1, 1, 0, 0, 1, 0, 0, 1, 0, 1]
    assert(expected_prototype == test_lens_agent.prototype)

    weight_base_example = '0, 1, 0, 0, 0, 0, 1, 1, 0, 0,' +\
                          '1, 1, 0, 0, 1, 0, 0, 1, 0, 1'
    lens_in_file = os.path.join(here, 'lens', 'AutoEncoderArch-update.in')
    agent_self_ex_file = os.path.join(here, 'lens', 'Infl.ex')
    agent_self_out_file = os.path.join(here, 'lens', 'AgentState.out')
    criterion = 3
    epsilon = 0.2
    test_lens_agent.seed_agent_no_update(weight_base_example)

    test_lens_agent.seed_agent(weight_base_example,
                               lens_in_file,
                               agent_self_ex_file,
                               agent_self_out_file,
                               criterion, epsilon)
    print('new state: ', test_lens_agent.get_state(), file=sys.stderr)

    expected_state = [0.00535452, 0.995756, 0.00328139, 0.00315037, 0.00399676,
                      0.00647343, 0.994951, 0.994197, 0.0038641, 0.00499099,
                      0.994252, 0.995074, 0.00527825, 0.00148673, 0.995454,
                      0.00472569, 0.00459107, 0.994304, 0.00434961, 0.998149]

    print('\ncannot fully test lens_agent_seed until we can seed LENS\n',
          file=sys.stderr)
    assert(len(test_lens_agent.get_state()) == len(expected_state))
    assert(len(expected_state) == 20)
    # assert test_lens_agent.get_state() == expected_state


@nose.with_setup(reset_LensAgent)
def test_update_agent_state_default():
    pass


@nose.with_setup(reset_LensAgent)
def test_update_agent_state():
    list_of_predecessors = []
    for i in range(3):
        lens_agent_predecessor = agent.LensAgent(10)
        list_of_predecessors.append(lens_agent_predecessor)
    test_lens_agent = agent.LensAgent(10)
    assert test_lens_agent.get_key() == 3

    test_lens_agent.set_predecessors(list_of_predecessors)
    assert test_lens_agent.predecessors[0].get_key() == 0
    assert test_lens_agent.predecessors[-1].get_key() == 2

    here = os.path.abspath(os.path.dirname(__file__))
    lens_in_file_dir = here + '/' + 'lens/UpdateFromInfl.in'
{'fraction_non_alphanumeric': 0.15377532228360957, 'fraction_numerical': 0.046224677716390425, 'mean_word_length': 3.1660912926735714, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3916786', 'n_tokens_mistral': 4258, 'n_tokens_neox': 3697, 'n_words': 746}