def analyze_polarity(polarity_dict, pid_dict, location_to_store, filename):
    final_csv_data = [['source', 'target', 'Pos', 'Neg', 'Neu']]
    print '++++ POLARITY INFORMATION ++++'
    type_abr = {'participants': 'P', 'nonparticipants':'NP'}
    for pid in polarity_dict:
        src_new = hlp.getpid(pid_dict, pid)
        print '**** For PID: P' + str(src_new) + ' ****'
        for (src, trg) in polarity_dict[pid]:
            trg_new, trg_type = hlp.getpid(pid_dict, trg, return_p_type=True)
            print '(P'+str(src_new)+','+ type_abr[trg_type]+str(trg_new)+')'
            temp = ['P'+str(src_new), type_abr[trg_type]+str(trg_new)]
            for polarity in polarity_dict[pid][(src, trg)]:
                temp.append(polarity)
            final_csv_data.append(temp)
    hlp.writecsv(final_csv_data, location_to_store+filename, delimiter_sym=',')
def create_undirected_edges(data, pid_dict):
    edges_dict = {}
    for datum in data:
        node1 = hlp.getpid(pid_dict, datum[pr.m_source])
        node2 = hlp.getpid(pid_dict, datum[pr.m_target])
        if node1 < node2:
            edge_pair = (node1, node2)
        else:
            edge_pair = (node2, node1)
        if edge_pair not in edges_dict:
            edges_dict[edge_pair] = [0, [0,0,0]]
        temp = edges_dict[edge_pair]
        temp[0] += 1
        if datum[-1] == 'P':
            temp[1][0] += 1
        elif datum[-1] == 'U':
            temp[1][1] += 1
        elif datum[-1] == 'N':
            temp[1][2] += 1
        else:
            print 'Something fishy:', datum
        edges_dict[edge_pair] = temp

    networkx_edges = []
    pos_neg = [0, 0]
    for edge_pair in edges_dict:
        edge_values = edges_dict[edge_pair]
        polarity_count = edge_values[1]
        if 0 == polarity_count[0] and 0 == polarity_count[2]:
            # there are no + or - messages, ignore edge
            continue
        if polarity_count[0] >= polarity_count[2]:
            polarity = 'P'
            pos_neg[0] += 1
        else:
            polarity = 'N'
            pos_neg[1] += 1
        networkx_edges.append((edge_pair[0], edge_pair[1],
                               {'weight': edge_values[0], 'polarity': polarity,
                                'color': 'red' if polarity == 'N' else 'green'}))
    return networkx_edges, edges_dict, pos_neg
def analyze_info(reciprocity_dict, pid_dict, location_to_store, filename):
    final_csv_data = []
    for pid in reciprocity_dict:
        pid_new = hlp.getpid(pid_dict, pid)
        print '**** For PID: P' + str(pid_new) + ' ****'
        pid_data = reciprocity_dict[pid]
        for target_type in pid_data:
            target_data = pid_data[target_type]
            for target_pid in target_data:
                target_pid_new = hlp.getpid(pid_dict, target_pid)
                print 'P' + str(pid_new) + '-' + target_type + str(target_pid_new)
                temp = get_reci_probability(target_data[target_pid], True)
                starting_data = ['P' + str(pid_new), target_type + str(target_pid_new)]
                starting_data.extend(temp)
                final_csv_data.append(starting_data)
    header = ['source', 'target',
              'Pr(+|+)', 'Pr(-|+)', 'Pr(U|+)', 'Pr(X|+)',
              'Pr(+|U)', 'Pr(-|U)', 'Pr(U|U)', 'Pr(X|U)',
              'Pr(+|-)', 'Pr(-|-)', 'Pr(U|-)', 'Pr(X|-)']
    toWrite = [header] + final_csv_data
    hlp.writecsv(toWrite, location_to_store + filename, delimiter_sym=',')