<commit_before># coding=utf-8
""" Implements utility classes that have to do with traffic patterns, such as
    traffic matrix, and network traffic classes (commodities)
"""
# import json
# import random
import numpy as np
cimport numpy as np
from traffic cimport TrafficClass

# class TrafficMatrix(dict):
#     """
#     Represents a traffic matrix, extends basic dictionary type
#
#     """
#
#     def permute(self, rand=None):
#         """
#         Permute this traffic matrix randomly
#
#         :param rand: instance of a Python :py:mod:`random` object
#         """
#         v = self.values()
#         random.shuffle(v, rand)
#         for i, k in enumerate(self.iterkeys()):
#             self[k] = v[i]
#
#     def dump(self, fname):
#         """
#         Save the traffic matrix to a file
#         :param fname: filename to save to
#         """
#         with open(fname, 'w') as f:
#             json.dump({"{}->{}".format(k[0], k[1]): v for k, v in self.iteritems()}, f)
#
#     @staticmethod
#     def load(fname):
#         """
#         Load a traffic matrix from a file
#         :param fname: filename to load from
#         :return: a new TrafficMatrix
#         """
#         with open(fname, 'r') as f:
#             return TrafficMatrix({tuple(map(int, k.split('->'))): v for k, v in json.load(f).iteritems()})



cdef class TrafficClass(object):
    """ Represents a traffic class. All members are public
    """

    def __init__(self, int tcid, str name, int src, int dst,
                 np.ndarray vol_flows=np.zeros(1),
                 np.ndarray vol_bytes=np.zeros(1), int priority=1,
                 src_ip_prefix=None, dst_ip_prefix=None,
                 src_app_ports=None, dst_app_ports=None):
        """ Creates a new traffic class.

        :param tcid: unique traffic class identifier
        :param name: traffic class name, for human readability (e.g., 'web',
            'ssh', etc.)
        :param src: nodeID that is the ingress for this traffic class
        :param dst: nodeID that is the egress for this traffic class
        :param vol_flows: number of flows for this traffic class
        :param vol_bytes: number of bytes for this traffic class
        :param priority: traffic class priority, as an integer (higher number means higher priority)
        :param src_ip_prefix: ingress IP prefix (CIDR notation)
        :param dst_ip_prefix: egress IP prefix (CIDR notation)
        :param scrAppPorts: packet application ports (source)
        :param dst_app_ports: packet application ports (destination)
        """

        self.ID = tcid
        self.name = name
        self.src = src
        self.dst = dst
        self.volFlows = np.ma.masked_array(data=vol_flows, mask=np.ma.nomask)
        self.volBytes = np.ma.masked_array(data=vol_bytes, mask=np.ma.nomask)
        assert self.volFlows.size == self.volBytes.size
        self.priority = priority
        self.srcIPPrefix = src_ip_prefix
        self.dstIPPrefix = dst_ip_prefix
        self.srcAppPorts = src_app_ports
        self.dstAppPorts = dst_app_ports

    def __repr__(self):
        return "TrafficClass {} -> {}, {}, ID={}".format(self.src, self.dst,
                                                         self.name, self.ID)

    cpdef tuple iepair(self):
        """
        Return the ingress-egress pair as a tuple

        :return:  ingress-egress pair
        :rtype: tuple
        """
        return self.src, self.dst

    cpdef int ingress(self):
        return self.src

    cpdef int egress(self):
        return self.dst

    def __hash__(self):
        return hash((self.ID, self.src, self.dst, self.name))

    def __richcmp__(TrafficClass self, other not None, int op):
        sametype = isinstance(other, TrafficClass)
        if op == 2:
            return sametype and (self.ID == other.ID and
                self.src == other.src and
                self.dst == other.dst and
                self.name == other.name)
        elif op == 3:
            return not sametype or not (self.ID == other.ID and
                self.src == other.src and
                self.dst == other.dst and
                self.name == other.name)
        else:
            raise TypeError

    def __copy__(self):
        return TrafficClass(self.ID, self.name, self.src, self.dst,
                            self.volFlows, self.volBytes, self.priority,
                            self.srcIPPrefix, self.dstIPPrefix,
                            self.srcAppPorts, self.dstAppPorts)

    def encode(self):
        return {'type': 'TrafficClass', 'src': self.src, 'dst': self.dst,
                'name': self.name, 'id': self.ID}

    @staticmethod
    def decode(d):
        return TrafficClass(d['id'], d['name'], d['src'], d['dst'])<commit_msg>Removed traffic matrix from sol as that has been factored out into TMgen project.<commit_after># coding=utf-8
""" Implements utility classes that have to do with traffic patterns, such as
    traffic matrix, and network traffic classes (commodities)
"""
import numpy as np
cimport numpy as np
from traffic cimport TrafficClass

cdef class TrafficClass(object):
    """ Represents a traffic class. All members are public
    """

    def __init__(self, int tcid, str name, int src, int dst,
                 np.ndarray vol_flows=np.zeros(1),
                 np.ndarray vol_bytes=np.zeros(1), int priority=1,
                 src_ip_prefix=None, dst_ip_prefix=None,
                 src_app_ports=None, dst_app_ports=None):
        """ Creates a new traffic class.

        :param tcid: unique traffic class identifier
        :param name: traffic class name, for human readability (e.g., 'web',
            'ssh', etc.)
        :param src: nodeID that is the ingress for this traffic class
        :param dst: nodeID that is the egress for this traffic class
        :param vol_flows: number of flows for this traffic class
        :param vol_bytes: number of bytes for this traffic class
        :param priority: traffic class priority, as an integer (higher number means higher priority)
        :param src_ip_prefix: ingress IP prefix (CIDR notation)
        :param dst_ip_prefix: egress IP prefix (CIDR notation)
        :param scrAppPorts: packet application ports (source)
        :param dst_app_ports: packet application ports (destination)
        """

        self.ID = tcid
        self.name = name
        self.src = src
        self.dst = dst
        self.volFlows = np.ma.masked_array(data=vol_flows, mask=np.ma.nomask)
        self.volBytes = np.ma.masked_array(data=vol_bytes, mask=np.ma.nomask)
        assert self.volFlows.size == self.volBytes.size
        self.priority = priority
        self.srcIPPrefix = src_ip_prefix
        self.dstIPPrefix = dst_ip_prefix
        self.srcAppPorts = src_app_ports
        self.dstAppPorts = dst_app_ports

    def __repr__(self):
        return "TrafficClass {} -> {}, {}, ID={}".format(self.src, self.dst,
                                                         self.name, self.ID)

    cpdef tuple iepair(self):
        """
        Return the ingress-egress pair as a tuple

        :return:  ingress-egress pair
        :rtype: tuple
        """
        return self.src, self.dst

    cpdef int ingress(self):
        return self.src

    cpdef int egress(self):
        return self.dst

    def __hash__(self):
        return hash((self.ID, self.src, self.dst, self.name))

    def __richcmp__(TrafficClass self, other not None, int op):
        sametype = isinstance(other, TrafficClass)
        if op == 2:
            return sametype and (self.ID == other.ID and
                self.src == other.src and
                self.dst == other.dst and
                self.name == other.name)
        elif op == 3:
            return not sametype or not (self.ID == other.ID and
                self.src == other.src and
                self.dst == other.dst and
                self.name == other.name)
        else:
            raise TypeError

    def __copy__(self):
        return TrafficClass(self.ID, self.name, self.src, self.dst,
                            self.volFlows, self.volBytes, self.priority,
                            self.srcIPPrefix, self.dstIPPrefix,
                            self.srcAppPorts, self.dstAppPorts)

    def encode(self):
        return {'type': 'TrafficClass', 'src': self.src, 'dst': self.dst,
                'name': self.name, 'id': self.ID}

    @staticmethod
    def decode(d):
        return TrafficClass(d['id'], d['name'], d['src'], d['dst'])
{'fraction_non_alphanumeric': 0.1035492561789856, 'fraction_numerical': 0.0016399203467260162, 'mean_word_length': 2.0384341637010674, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6411144', 'n_tokens_mistral': 2507, 'n_tokens_neox': 2440, 'n_words': 759}