Пример #1
0
    def information(activation):
        data_t = activation

        if bins == -1:
            data_t = [
                bin_array(t, bins=30, low=t.min(), high=t.max())
                for t in data_t
            ]
        else:
            data_t = [
                bin_array(t, bins=bins, low=t.min(), high=t.max())
                for t in data_t
            ]

        #data_t = [binarize(t) for t in data_t]
        data_t = [hash_data(t) for t in data_t]

        h_t = np.array([entropy_of_data(t) for t in data_t])
        #h_t_x = np.array([__conditional_entropy(t, data_x) for t in data_t])
        h_t_y = np.array([__conditional_entropy(t, data_y) for t in data_t])

        h_t_t = np.array(
            [__conditional_entropy(t1, t2) for (t1, t2) in pairwise(data_t)])

        i_x_t = h_t  # - h_t_x # H(T, X) is 0 since every element in X is unique
        i_y_t = h_t - h_t_y
        i_t_t = h_t[:-1] - h_t_t

        return i_x_t, i_y_t, i_t_t
Пример #2
0
def join(strhash=None):
    key = hash_data(gen_rand_ip())
    print("Join with index %s" % key)
    if not len(nodes):
        node = Node(key)
        node.predecessor = node
        node.setsuccessor(node)
        nodes.append(node)
    else:
        # root = find(strhash)
        root = rootnode().predecessor
        print("Root node %s" % root)
        successor = root.finger[0]

        while root != successor and key < successor.node_id:
            print("%s < %s" % (key, successor.node_id))
            successor = successor.finger[0]
        successor = successor.predecessor
        print("Set %s as predecessor of %s" % (key, successor.node_id))

        newnode = Node(key)
        newnode.setsuccessor(successor)
        if successor.predecessor is not None:
            newnode.predecessor = successor.predecessor
            successor.predecessor.setsuccessor(newnode)
            successor.predecessor = newnode
        else:
            # addition of second node in network
            newnode.predecessor = successor
            successor.predecessor = newnode
            successor.setsuccessor(newnode)
        nodes.append(newnode)
Пример #3
0
Файл: app.py Проект: cadl/GT
def hears():
    """
    This route listens for incoming events from Slack and uses the event handler helper function to route events
    to our Bot.
    """
    slack_event = json.loads(request.data)
    # ============= Slack URL Verification ============ #
    if "challenge" in slack_event:
        return make_response(slack_event["challenge"], 200, {"content_type": "application/json"})

    # ============ Slack Token Verification =========== #
    if pyBot.verification != slack_event.get("token"):
        message = f"Invalid Slack verification token: {slack_event['token']}\npyBot has: {pyBot.verification}\n\n"
        # By adding "X-Slack-No-Retry" : 1 to our response headers, we turn off Slack's automatic retries during
        # development.
        return make_response(message, 403, {"X-Slack-No-Retry": 1})

    # ====== Prevent Duplicate Processing ====== #
    _, created = get_or_create_event_log(hash_data(slack_event))
    if not created:
        return make_response("Got it.", 200, {"content_type": "application/json"})

    # ====== Process Incoming Events from Slack ======= #
    if "event" in slack_event:
        event_type = slack_event["event"]["type"]
        # Then handle the event by event_type and have your bot respond
        return _event_handler(event_type, slack_event)

    return make_response("[NO EVENT IN SLACK REQUEST] These are not the droids you're looking for.",
                         404, {"X-Slack-No-Retry": 1})
Пример #4
0
def finddata(strhash, key):  # still linear
    root = find(strhash)
    successor = find(strhash).finger[0]
    hashkey = hash_data(key)
    while root != successor and hashkey < successor.node_id:
        successor = successor.finger[0]
    print("searching in %s" % successor.node_id)
    return successor.find_data(key)
Пример #5
0
def insert(strhash, key, data):
    hashkey = hash_data(key)
    print("Hashed key %s" % hashkey)
    root = find(strhash)
    successor = find(strhash).finger[0]
    while root != successor and hashkey < successor.node_id:
        successor = successor.finger[0]
    successor.insert_data(key, data)
Пример #6
0
def calculate_information_tishby(input_values, labels, bins=30):
    # activation layers*test_case*neuron -> value)

    # calculate information I(X,T) and I(T,Y) where X is the input and Y is the output
    # and T is any layer
    data_x = hash_data(input_values)
    data_y = hash_data(labels)

    #data_x = binarize(input_values)
    #data_y = binarize(labels)

    def information(activation):
        data_t = activation

        if bins == -1:
            data_t = [
                bin_array(t, bins=30, low=t.min(), high=t.max())
                for t in data_t
            ]
        else:
            data_t = [
                bin_array(t, bins=bins, low=t.min(), high=t.max())
                for t in data_t
            ]

        #data_t = [binarize(t) for t in data_t]
        data_t = [hash_data(t) for t in data_t]

        h_t = np.array([entropy_of_data(t) for t in data_t])
        #h_t_x = np.array([__conditional_entropy(t, data_x) for t in data_t])
        h_t_y = np.array([__conditional_entropy(t, data_y) for t in data_t])

        h_t_t = np.array(
            [__conditional_entropy(t1, t2) for (t1, t2) in pairwise(data_t)])

        i_x_t = h_t  # - h_t_x # H(T, X) is 0 since every element in X is unique
        i_y_t = h_t - h_t_y
        i_t_t = h_t[:-1] - h_t_t

        return i_x_t, i_y_t, i_t_t

    return information
    def data(self):
        """Ensure data is in the form of a HashLeaf data structures and has
        the correct height. Separate method from `HashLeaf` as there are
        different requirements
        """
        # assert isinstance(self._left, MerkleLeaf), "Data is not of type `MerkleLeaf`"
        # assert isinstance(self._right, MerkleLeaf), "Data is not of type `MerkleLeaf`"

        assert self._left.height == self._right.height, "Left and right branch not balanced"

        return hash_data(self._left.data + self._right.data)
Пример #8
0
    def compute_single(saved, dist):
        x_test_hash = hash_data(x_test)
        data_x = x_test_hash
        for _ in range(dist - 1):
            data_x = np.concatenate((data_x, x_test_hash))

        y_test_hash = hash_data(y_test)
        data_y = y_test_hash
        for _ in range(dist - 1):
            data_y = np.concatenate((data_y, y_test_hash))

        # saved data where every number is binned
        saved_bin = [[
            bin_array(layer, bins=args.bins, low=layer.min(), high=layer.max())
            for layer in epoch
        ] for epoch in saved]
        # saved data where every number is hashed
        saved_hash = [[hash_data(layer) for layer in epoch]
                      for epoch in saved_bin]

        data_t = {}
        for t in range(len(saved_hash[0])):
            data_t[t] = np.array([], dtype=np.int64)
        for epoch in range(len(saved_hash)):
            for t in range(len(saved_hash[0])):
                data_t[t] = np.concatenate([data_t[t], saved_hash[epoch][t]])
        data_t = list(data_t.values())

        h_t = np.array([entropy_of_data(t) for t in data_t])
        h_t_x = np.array([__conditional_entropy(t, data_x) for t in data_t])
        h_t_y = np.array([__conditional_entropy(t, data_y) for t in data_t])

        i_x_t = h_t - h_t_x
        i_y_t = h_t - h_t_y

        return i_x_t, i_y_t
Пример #9
0
Файл: app.py Проект: cadl/GT
def _event_handler(event_type, slack_event):
    """
    A helper function that routes events from Slack to our Bot
    by event type and subtype.

    Parameters
    ----------
    event_type : str
        type of event recieved from Slack
    slack_event : dict
        JSON response from a Slack reaction event

    Returns
    ----------
    obj
        Response object with 200 - ok or 500 - No Event Handler error

    """
    # ============= Reaction Added Events ============= #
    # If the user has added an emoji reaction to one message
    if event_type == "reaction_added":
        user_id = slack_event["event"]["user"]
        # Some messages aren't authored by "users," like those created by incoming webhooks.
        # reaction_added events related to these messages will not include an item_user.
        item_user_id = slack_event["event"].get("item_user")
        reaction = slack_event["event"]["reaction"]
        # only log others' poultry_leg reaction to a real user
        if item_user_id and reaction == 'poultry_leg' and (CONFIG.DEBUG or item_user_id != user_id):
            message = json.dumps(slack_event["event"]["item"], separators=(',', ':'))
            print(f'{user_id} ({reaction}) > {item_user_id} @({message})')
            create_user_message_reaction_log(to_user_id=item_user_id, from_user_id=user_id,
                                             message_hash=hash_data(slack_event["event"]["item"]),
                                             reaction=reaction)
            # pyBot.notify_being_added_poultry_leg(user_id=user_id, item_user_id=item_user_id)
        return make_response("reaction logged", 200, )
    # If the user has mentioned the app
    elif event_type == "app_mention":
        # text = slack_event["event"]["text"]
        channel = slack_event["event"]["channel"]
        pyBot.tell_leaderboard(channel)
        return make_response("reaction logged", 200, )

    # ============= Event Type Not Found! ============= #
    # If the event_type does not have a handler
    message = "You have not added an event handler for the %s" % event_type
    # Return a helpful error message
    return make_response(message, 200, {"X-Slack-No-Retry": 1})
 def data(self):
     """str: Allow the user to query the hashed data stored in the
     MerkleLeaf
     """
     return hash_data(self._left.encode() + self._right.encode())