def draw_neuron_activation(mymap, named=True, symbols=False):  # iterates through EACH neuron and finds closest vector
    words = distances = empty_list(mymap.size, 1)

    if named:
        vectors = mymap.vectors
        keys = mymap.keys
    else:
        vectors = []
        keys = []
        idea_names = mymap.space.idea_names
        for item in mymap.space.table:
            keys.append(idea_names[item])
            vectors.append(mymap.space.table[item])

    if symbols:
        s = mymap.space.symbol_vectors
        keys = []
        vectors = []
        for item in s:
            keys.append(mymap.space.idea_names[item])
            vectors.append(s[item])

    for neuron in flatten(mymap.neurons):
        weight = neuron.weight
        match = fn.find_best_match(weight, vectors)
        distance = fn.distance(weight, vectors[match])
        x = neuron.position
        x = fn.to_int(x)
        words[x[0]][x[1]] = keys[match]
        #       distances[x[0]][x[1]] = distance
    word_plot(words)
    return words
def draw_item_activation(mymap, named=True, overwrite=False, symbols=False):
    words = empty_list(mymap.size, 1)
    mymap.renormalize()

    if named:
        vectors = mymap.vectors
        keys = mymap.keys
    else:
        vectors = []
        keys = []
        idea_names = mymap.space.idea_names
        for item in mymap.space.table:
            keys.append(idea_names[item])
            vectors.append(mymap.space.table[item])

    if symbols:
        s = mymap.space.symbol_vectors
        keys = []
        vectors = []
        for item in s:
            keys.append(item)
            vectors.append(s[item])

    for i, vector in enumerate(vectors):
        match = fn.find_best_match(vector, mymap.weights)
        x = mymap.positions[match]
        x = fn.to_int(x)
        w = words[x[0]][x[1]]
        if w == "" or overwrite:
            if overwrite:
                winner = fn.find_best_match(mymap.weights[match], mymap.vectors)
                w = keys[winner]
            else:
                w = keys[i]
        else:
            w = w + "," + keys[i]
        words[x[0]][x[1]] = w
    word_plot(words)
    return words
def get_distances_to_nearest(mymap):
    distances = empty_list(mymap.size, 1)
    vectors = mymap.vectors
    matches = []
    for neuron in flatten(mymap.neurons):
        weight = neuron.weight
        match = fn.find_best_match(weight, vectors)
        matches.append(match)
        distance = fn.distance(weight, vectors[match])
        x = neuron.position
        x = fn.to_int(x)
        distances[x[0]][x[1]] = distance
    c = Counter(matches)
    print c
    print "items mapped : " + str(len(sorted(c)))
    return distances
def draw_clusters_per_item(mymap, clusters):
    cluster_map = empty_list(mymap.size, 1)

    vectors = mymap.vectors
    keys = mymap.keys

    for neuron in flatten(mymap.neurons):
        weight = neuron.weight
        match = fn.find_best_match(weight, vectors)
        key = keys[match]
        cluster = clusters[key]
        x = neuron.position
        x = fn.to_int(x)
        cluster_map[x[0]][x[1]] = key
    #        cluster_map[x[0]][x[1]] = cluster
    return cluster_map