<commit_before>"""
Definition of network object.
"""

import numpy as num
import matplotlib.pyplot as plt
import copy
import itertools 
import logging
from kreveik.classes import *
import kreveik.probes as probes
from kreveik import *


class TopologicalNetwork(ProbeableObj):
    """
    This object is a stripped down network, designated to be a core 
    object for all network-like objects, like sub-graphs and motifs.
    """
    def __init__ (self,adjacency_matrix):
        ProbeableObj.__init__(self)
        self.adjacency = num.array(adjacency_matrix,dtype=bool)
        self.code = str(len(self.adjacency))+"-"+str(reduce(lambda x,y : 2*x+y, 
                                                              self.adjacency.flatten()*1))
        self.n_nodes = len(self.adjacency)
    
    def indegree(self):
        return self.adjacency.sum(axis=0)
    
    def outdegree(self):
        return self.adjacency.sum(axis=1)
    
    def plot(self):
        """Opens a window, draws the graph into the window.
           Requires Tk, and of course a windowing system.
        """
        import Tkinter as tk
        import math
        window= tk.Tk()
        canvas_size = 400
        drawing = tk.Canvas(window, height=canvas_size, width=canvas_size, background="white")
        n_nodes = self.n_nodes
        radius = 150
        node_radius = 10
        
        drawing.create_text(200,10,text = "Network:"+str(id(self)))
        
        list_of_coordinates = [(radius*math.sin(2*math.pi*n/n_nodes)+canvas_size/2,radius*math.cos(2*math.pi*n/n_nodes)+canvas_size/2) for n in range(n_nodes)]
        
        for linksto,node in enumerate(self.adjacency):
            for linksfrom,link in enumerate(node):
                
                if linksto == linksfrom and link==True:
                    angle = math.atan2(list_of_coordinates[linksto][1]-200,
                                      list_of_coordinates[linksto][0]-200)
                    
                    drawing.create_line(list_of_coordinates[linksto][0]+node_radius*math.cos(angle),
                                        list_of_coordinates[linksto][1]+node_radius*math.sin(angle),
                                        list_of_coordinates[linksto][0]+node_radius*2*(math.cos(angle+20)),
                                        list_of_coordinates[linksto][1]+node_radius*2*math.sin(angle+20),
                                        list_of_coordinates[linksto][0]+node_radius*4*(math.cos(angle)),
                                        list_of_coordinates[linksto][1]+node_radius*4*math.sin(angle),
                                        list_of_coordinates[linksto][0]+node_radius*2*math.cos(angle-20),
                                        list_of_coordinates[linksto][1]+node_radius*2*(math.sin(angle-20)),
                                        list_of_coordinates[linksto][0]+node_radius*math.cos(angle),
                                        list_of_coordinates[linksto][1]+node_radius*math.sin(angle),
                                        smooth=True,joinstyle="round",fill="black",width=2,arrow="last"
                                        )
                
                elif link == True: 
                    angle = math.atan2(list_of_coordinates[linksto][1]-list_of_coordinates[linksfrom][1],
                                   list_of_coordinates[linksto][0]-list_of_coordinates[linksfrom][0])
                
                    drawing.create_line(list_of_coordinates[linksfrom][0]+node_radius*math.cos(angle),
                                        list_of_coordinates[linksfrom][1]+node_radius*math.sin(angle),
                                        list_of_coordinates[linksto][0]-node_radius*math.cos(angle),
                                        list_of_coordinates[linksto][1]-node_radius*math.sin(angle),
                                        fill="black",width=2,arrow="last")
        
        for node_ctr,(x,y) in enumerate(list_of_coordinates):
            if self.state == []:
                node_color = "white"
                text_color = "black"
            else:
                if self.state[-1][node_ctr] == True: 
                    node_color = "black"
                    text_color = "white"
                else:                
                    node_color = "white"
                    text_color = "black"<commit_msg>Fixed a bug in network.plot(). Now, motifs can be plotted also.<commit_after>"""
Definition of network object.
"""

import numpy as num
import matplotlib.pyplot as plt
import copy
import itertools 
import logging
from kreveik.classes import *
import kreveik.probes as probes
from kreveik import *


class TopologicalNetwork(ProbeableObj):
    """
    This object is a stripped down network, designated to be a core 
    object for all network-like objects, like sub-graphs and motifs.
    """
    def __init__ (self,adjacency_matrix):
        ProbeableObj.__init__(self)
        self.adjacency = num.array(adjacency_matrix,dtype=bool)
        self.code = str(len(self.adjacency))+"-"+str(reduce(lambda x,y : 2*x+y, 
                                                              self.adjacency.flatten()*1))
        self.n_nodes = len(self.adjacency)
    
    def indegree(self):
        return self.adjacency.sum(axis=0)
    
    def outdegree(self):
        return self.adjacency.sum(axis=1)
    
    def plot(self):
        """Opens a window, draws the graph into the window.
           Requires Tk, and of course a windowing system.
        """
        import Tkinter as tk
        import math
        window= tk.Tk()
        canvas_size = 400
        drawing = tk.Canvas(window, height=canvas_size, width=canvas_size, background="white")
        n_nodes = self.n_nodes
        radius = 150
        node_radius = 10
        
        drawing.create_text(200,10,text = "Network:"+str(id(self)))
        
        list_of_coordinates = [(radius*math.sin(2*math.pi*n/n_nodes)+canvas_size/2,radius*math.cos(2*math.pi*n/n_nodes)+canvas_size/2) for n in range(n_nodes)]
        
        for linksto,node in enumerate(self.adjacency):
            for linksfrom,link in enumerate(node):
                
                if linksto == linksfrom and link==True:
                    angle = math.atan2(list_of_coordinates[linksto][1]-200,
                                      list_of_coordinates[linksto][0]-200)
                    
                    drawing.create_line(list_of_coordinates[linksto][0]+node_radius*math.cos(angle),
                                        list_of_coordinates[linksto][1]+node_radius*math.sin(angle),
                                        list_of_coordinates[linksto][0]+node_radius*2*(math.cos(angle+20)),
                                        list_of_coordinates[linksto][1]+node_radius*2*math.sin(angle+20),
                                        list_of_coordinates[linksto][0]+node_radius*4*(math.cos(angle)),
                                        list_of_coordinates[linksto][1]+node_radius*4*math.sin(angle),
                                        list_of_coordinates[linksto][0]+node_radius*2*math.cos(angle-20),
                                        list_of_coordinates[linksto][1]+node_radius*2*(math.sin(angle-20)),
                                        list_of_coordinates[linksto][0]+node_radius*math.cos(angle),
                                        list_of_coordinates[linksto][1]+node_radius*math.sin(angle),
                                        smooth=True,joinstyle="round",fill="black",width=2,arrow="last"
                                        )
                
                elif link == True: 
                    angle = math.atan2(list_of_coordinates[linksto][1]-list_of_coordinates[linksfrom][1],
                                   list_of_coordinates[linksto][0]-list_of_coordinates[linksfrom][0])
                
                    drawing.create_line(list_of_coordinates[linksfrom][0]+node_radius*math.cos(angle),
                                        list_of_coordinates[linksfrom][1]+node_radius*math.sin(angle),
                                        list_of_coordinates[linksto][0]-node_radius*math.cos(angle),
                                        list_of_coordinates[linksto][1]-node_radius*math.sin(angle),
                                        fill="black",width=2,arrow="last")
        
        for node_ctr,(x,y) in enumerate(list_of_coordinates):
            if type() != Network:
                node_color = "white"
                text_color = "black"
            elif self.state == num.array([[]]):
                node_color = "white"
                text_color = "black"
            else:
                if self.state[-1][node_ctr] == True: 
                    node_color = "black"
                    text_color = "white"
                else:                
                    node_color = "white"
                    text_color = "black"
{'fraction_non_alphanumeric': 0.10315057741899317, 'fraction_numerical': 0.014799865455768583, 'mean_word_length': 1.641397690257625, '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': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2457317', 'n_tokens_mistral': 2488, 'n_tokens_neox': 2364, 'n_words': 381}