<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pickle
import sys


def print_fish(data):
    import PIL
    import matplotlib.pyplot as plt
    from images2gif import writeGif
    fishes = sorted(
        [item for item in data if isinstance(item, Fish)],
        key=lambda x: x.iteration
    )
    settings = dict(
        (x.name, x.value) for x in data if isinstance(x, Parameter)
    )
    nets_lst = sorted(
        [item for item in data if isinstance(item, Net)],
        key=lambda x: (x.iteration, x._id)
    )
    nets = {}
    for nt in nets_lst:
        if not nt.iteration in nets:
            nets[nt.iteration] = []
        nets[nt.iteration].append(nt)
    colors = [[]] * len(set(x.rank for x in fishes))
    avail_colors = ['r', 'g', 'b', 'y']
    for i in range(len(colors)):
        colors[i] = avail_colors[i]
    imgs = []

    def get_img(coll):
        # import io
        # points = [(fish.x, fish.y) for fish in coll]
        fig = plt.figure()
        plt.xlim([0, settings['WORLD_WIDTH']])
        plt.ylim([0, settings['WORLD_HEIGHT']])
        # x, y = zip(*points)
        # plt.plot(x, y, 'o')
        # plt.set_color_cycle()
        ax = fig.add_subplot(111)
        for fish in coll:
            plt.plot([fish.x], [fish.y], '%so' % colors[fish.rank])
            ax.annotate(
                '%d' % fish.num,
                xy=(fish.x, fish.y),
                # textcoords='offset points'
            )
        for net in nets.get(fish.iteration, []):
            circle = plt.Circle((net.x, net.y), net.size, color='r')
            fig.gca().add_artist(circle)
        # buf = io.BytesIO()
        canvas = plt.get_current_fig_manager().canvas
        canvas.draw()
        pil_image = PIL.Image.fromstring(
            'RGB', canvas.get_width_height(),
            canvas.tostring_rgb()
        )
        # pil_image.save(buf, 'PNG')
        plt.close()

        return pil_image

    collection = []
    last_iter = -1
    for fish in fishes:
        if fish.iteration > last_iter and collection:
            imgs.append(get_img(collection))
            collection = []
            if fish.iteration > 5:
                break
        collection.append(fish)
        last_iter = fish.iteration
    if collection:
        imgs.append(get_img(collection))

    writeGif('visualisation.gif', imgs)


def get_file_scp(username, password):
    # Assumes that the datafile resides at
    # ~/HPC-A/fish/tests/output.dat
    import paramiko
    import scp
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect('jotunn.rhi.hi.is', 22, username, password)
    scp_client = scp.SCPClient(client.get_transport())
    scp_client.get('~/HPC-A/fish/tests/output.dat')
    with open('output.dat', 'r') as f:
        return pickle.load(f)
<commit_msg>Add circles for the nets and add counter for number of fish in nets<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pickle
import sys


def print_fish(data):
    import PIL
    import matplotlib.pyplot as plt
    from images2gif import writeGif
    fishes = sorted(
        [item for item in data if isinstance(item, Fish)],
        key=lambda x: x.iteration
    )
    settings = dict(
        (x.name, x.value) for x in data if isinstance(x, Parameter)
    )
    nets_lst = sorted(
        [item for item in data if isinstance(item, Net)],
        key=lambda x: (x.iteration, x._id)
    )
    nets = {}
    for nt in nets_lst:
        if not nt.iteration in nets:
            nets[nt.iteration] = []
        nets[nt.iteration].append(nt)
    colors = [[]] * len(set(x.rank for x in fishes))
    avail_colors = ['r', 'g', 'b', 'y']
    for i in range(len(colors)):
        colors[i] = avail_colors[i]
    imgs = []

    def get_img(coll):
        # import io
        # points = [(fish.x, fish.y) for fish in coll]
        fig = plt.figure()
        plt.xlim([0, settings['WORLD_WIDTH']])
        plt.ylim([0, settings['WORLD_HEIGHT']])
        # x, y = zip(*points)
        # plt.plot(x, y, 'o')
        # plt.set_color_cycle()
        ax = fig.add_subplot(111)
        for fish in coll:
            plt.plot([fish.x], [fish.y], '%so' % colors[fish.rank])
            ax.annotate(
                '%d' % fish.num,
                xy=(fish.x, fish.y),
                # textcoords='offset points'
            )
        for net in nets.get(fish.iteration, []):
            circle = plt.Circle(
                (net.x, net.y), net.size, color='r', fill=False)
            fig.gca().add_artist(circle)
            ax.annotate(
                '%d' % net.fish,
                xy=(net.x, net.y)
            )
        # buf = io.BytesIO()
        canvas = plt.get_current_fig_manager().canvas
        canvas.draw()
        pil_image = PIL.Image.fromstring(
            'RGB', canvas.get_width_height(),
            canvas.tostring_rgb()
        )
        # pil_image.save(buf, 'PNG')
        plt.close()

        return pil_image

    collection = []
    last_iter = -1
    for fish in fishes:
        if fish.iteration > last_iter and collection:
            imgs.append(get_img(collection))
            collection = []
        collection.append(fish)
        last_iter = fish.iteration
    if collection:
        imgs.append(get_img(collection))

    writeGif('visualisation.gif', imgs, duration=0.2)


def get_file_scp(username, password):
    # Assumes that the datafile resides at
    # ~/HPC-A/fish/tests/output.dat
    import paramiko
    import scp
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect('jotunn.rhi.hi.is', 22, username, password)
    scp_client = scp.SCPClient(client.get_transport())
    scp_client.get('~/HPC-A/fish/tests/output.dat')
    with open('output.dat', 'r') as f:
        return pickle.load(f)

{'fraction_non_alphanumeric': 0.13287302121926575, 'fraction_numerical': 0.0038733580330077466, 'mean_word_length': 2.053470437017995, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4605310', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1962, 'n_words': 441}