def measure(self, protocol: Protocol) -> float:
        disentanglement_scores = []
        non_constant_positions = 0

        for j in range(self.max_message_length):
            symbols_j = [message[j] for message in protocol.values()]
            symbol_mutual_info = []
            symbol_entropy = compute_entropy(symbols_j)
            for i in range(self.num_concept_slots):
                concepts_i = [
                    flatten_derivation(derivation)[i]
                    for derivation in protocol.keys()
                ]
                mutual_info = compute_mutual_information(concepts_i, symbols_j)
                symbol_mutual_info.append(mutual_info)
            symbol_mutual_info.sort(reverse=True)

            if symbol_entropy > 0:
                disentanglement_score = (
                    symbol_mutual_info[0] -
                    symbol_mutual_info[1]) / symbol_entropy
                disentanglement_scores.append(disentanglement_score)
                non_constant_positions += 1
            if non_constant_positions > 0:
                return sum(disentanglement_scores) / non_constant_positions
            else:
                return np.nan
Exemplo n.º 2
0
 def _protocol_to_tensor(
     self, protocol: Protocol
 ) -> Dict[Tuple[torch.LongTensor, torch.LongTensor], torch.LongTensor]:
     vocab = get_vocab_from_protocol(protocol)
     concept_set = set(concept for derivation in protocol.keys()
                       for concept in flatten_derivation(derivation))
     concepts = {concept: idx for idx, concept in enumerate(concept_set)}
     tensorized_protocol = {}
     for derivation, message in protocol.items():
         derivation = derivation_to_tensor(derivation, concepts)
         message = torch.LongTensor([vocab[char] for char in message])
         tensorized_protocol[derivation] = torch.nn.functional.one_hot(
             message, num_classes=len(vocab)).reshape(-1)
     return tensorized_protocol
Exemplo n.º 3
0
def test(protocol, datestring, batch_id):
    """
    Takes a Protocol object and runs a test for it
    Outputs the dump to a file
    """
    if not args.local_path.endswith("/"):
        args.local_path += "/"

    protocol_obj = Protocol(args.host, args.remote_path, args.local_path, protocol)
    filename = args.remote_path[args.remote_path.rfind("/")+1:]
    filepath = args.local_path+filename

    remote_hostname = args.host
    if "@" in remote_hostname:
        remote_hostname = remote_hostname[remote_hostname.find("@")+1:]

    local_ip = get_ip_address(args.interface)

    dump = TcpDump(batch_id, protocol, filepath, args.interface, remote_hostname, local_ip)

    dump.start()
    protocol_obj.run()
    dump.stop()

    protocol_name = protocol_obj.protocol

    # Abort if no packets were captured
    if not dump.output:
        return

    # Remove packets array if not needed
    if not args.store_packets:
        del dump.output["packets"]
    dump.output["stored_packets"] = args.store_packets

    # Make sure the dump directory exists
    if not os.path.exists("packet_dumps"):
        os.makedirs("packet_dumps")

    # Write dump file
    with open("packet_dumps/%s_%s_%s.dump" % (protocol_name, filename, datestring), "w") as f:
        json.dump(dump.output, f)

    # Remove file if flag is set
    if args.delete_files:
        os.remove(filepath)
 def measure(self, protocol: Protocol) -> float:
     vocab = list(get_vocab_from_protocol(protocol))
     num_symbols = len(vocab)
     bow_protocol = {}
     for derivation, message in protocol.items():
         message_bow = [0 for _ in range(num_symbols)]
         for symbol in message:
             message_bow[vocab.index(symbol)] += 1
         bow_protocol[derivation] = [str(symbol) for symbol in message_bow]
     return super().measure(bow_protocol)
Exemplo n.º 5
0
 def __init__(self, baseProto):
     Protocol.__init__(self)
     from weakref import WeakKeyDictionary
     self.__subscribers = WeakKeyDictionary()
     baseProto.addImpliedProtocol(self, protocols.NO_ADAPTER_NEEDED, 1)
Exemplo n.º 6
0
 def __init__(self,baseProto):
     Protocol.__init__(self)
     from weakref import WeakKeyDictionary
     self.__subscribers = WeakKeyDictionary()
     baseProto.addImpliedProtocol(self, protocols.NO_ADAPTER_NEEDED, 1)
Exemplo n.º 7
0
from protocols import Word, Message, MessageList, Protocol
from wheel_protocols import WheelGraph, forward_others, forward_all
from typing import Dict

graph = WheelGraph(8)
protocol1 = Protocol(graph, forward_others)
protocol2 = Protocol(graph, forward_all)

from_center = [
    Message('center', '0', Word(['x0'])),
    Message('center', '1', Word(['x1'])),
    Message('center', '2', Word(['x2'])),
    Message('center', '3', Word(['x3'])),
    Message('center', '4', Word(['x4'])),
    Message('center', '5', Word(['x5'])),
    Message('center', '6', Word(['x6'])),
    Message('center', '7', Word(['x7'])),
]
from_0 = [
    Message('0', '1', Word(['a'])),
    Message('0', '4', Word(['b'])),
    Message('0', 'center', Word(['c'])),
]

initial_messages = MessageList(graph, from_center)

transcript1 = protocol1.simulate(initial_messages, 2)
transcript2 = protocol2.simulate(initial_messages, 2)

def aggregate(incoming: Dict[str, Word]) -> Word:
    return sum(incoming.values(), Word())
Exemplo n.º 8
0
def get_vocab_from_protocol(protocol: Protocol) -> Dict[str, int]:
    character_set = set(c for message in protocol.values() for c in message)
    return {char: idx for idx, char in enumerate(character_set)}