def setup():
    table = ChainOutputTable()
    mock_row1 = {'population': {1: 100.0, 2: 50.0}, 'area': {1: 1000, 2: 400}}
    mock_row2 = {'population': {1: 125.0, 2: 25.0}, 'area': {1: 1200, 2: 200}}
    table.append(mock_row1)
    table.append(mock_row2)
    return table, mock_row1, mock_row2
Exemple #2
0
def pipe_to_table(chain, handlers):
    table = ChainOutputTable()
    interval = math.floor(len(chain) / 100)
    counter = 0
    for row in handle_chain(chain, handlers):
        if counter % interval == 0:
            print(f"Step {counter}")
            table.append(row)
            print(row)
        counter += 1
    return table
Exemple #3
0
def pipe_to_table(chain,
                  handlers,
                  display=True,
                  display_frequency=100,
                  bin_frequency=1):
    table = ChainOutputTable()
    display_interval = math.floor(len(chain) / display_frequency)
    counter = 0
    for row in handle_chain(chain, handlers):
        if counter % display_interval == 0:
            if display:
                print(f"Step {counter}")
                print(row)
        if counter % bin_frequency == 0:
            table.append(row)
        counter += 1
    return table
Exemple #4
0
def handle_scores_separately(chain, handlers):
    table = ChainOutputTable()

    initialScores = {
        key: handler(chain.state)
        for key, handler in handlers.items() if key != "flips"
    }
    table.append(initialScores)

    nhandlers = {
        key: value
        for key, value in handlers.items() if key != "flips"
    }

    jsonToText = '{'
    jsonSave = False
    if "flips" in list(handlers.keys()):
        jsonToText += '{0: ' + json.dumps(handlers['flips'](chain.state)) + '}'
        jsonSave = True

    for row in handle_chain(chain, nhandlers):
        table.append(row)
        if jsonSave:
            jsonToText += "{" + str(chain.counter + 1) \
            + ":" + json.dumps(handlers["flips"](chain.state)) + "}"
    jsonToText += '}'

    return (table, jsonToText, nhandlers)
Exemple #5
0
def pipe_to_table(chain,
                  handlers,
                  display=True,
                  display_frequency=100,
                  bin_frequency=100):
    table = ChainOutputTable()
    display_interval = math.floor(len(chain) / display_frequency)
    counter = 0
    now = datetime.datetime.now().strftime("%Y-%m-%d_%H_%M_%S")
    with open(f"./logs/flips_{now}.log", 'w') as f:
        f.write("{ \"flips\": [\n")
        for state in chain:
            row = {key: handler(state) for key, handler in handlers.items()}
            f.write(json.dumps(state.flips) + ",")
            if counter % display_interval == 0:
                if display:
                    print(f"Step {counter}")
                    print(row)
            if counter % bin_frequency == 0:
                table.append(row)
                f.write("\n")
            counter += 1
        f.write("\n]\n}\n")
    return table
Exemple #6
0
def pipe_to_table(chain, handlers):
    table = ChainOutputTable()
    for row in handle_chain(chain, handlers):
        table.append(row)
    return table