def plot_num_outgoing_connections(self, conn_type, fig_cnt=1):

        fn = self.params['merged_conn_list_%s' % conn_type]
        print 'Loading:', fn
        if not os.path.exists(fn):
            print 'Merging connlists ...'
            cmd = 'python merge_connlists.py %s' % self.params['params_fn']
            os.system(cmd)

        if not self.conn_lists.has_key(conn_type):
            self.load_connlist(conn_type)
        conn_list = self.conn_lists[conn_type]

        (n_src, n_tgt, syn_type) = utils.resolve_src_tgt(conn_type, self.params)
        n_tgts = np.zeros(n_src)
        w_out = np.zeros(n_src)
        n_srcs = np.zeros(n_tgt)
        w_in = np.zeros(n_tgt)
        for i in xrange(conn_list[:, 0].size):
            src, tgt, w, delay = conn_list[i, :4]
            n_tgts[src] += 1 # count how often src connects to some other cell
            n_srcs[tgt] += 1 # count how often tgt is the target cell
            w_out[src] += w
            w_in[tgt] += w

        n_out_mean = n_tgts.mean()
        n_out_sem = n_tgts.std() / np.sqrt(n_src)
        n_in_mean = n_srcs.mean()
        n_in_sem = n_srcs.std() / np.sqrt(n_tgt)
        print '\nConvergence:\nNumber of %s cells that get no %s input: %d = %.2f percent' % (self.conn_type_dict[conn_type[1]], self.conn_type_dict[conn_type[0]], (n_srcs == 0).nonzero()[0].size, (n_srcs==0).nonzero()[0].size / n_tgt * 100.)
        print 'Divergence: Number of %s cells that have no %s target: %d = %.2f percent\n' % (self.conn_type_dict[conn_type[0]], self.conn_type_dict[conn_type[1]], (n_tgts == 0).nonzero()[0].size, (n_tgts==0).nonzero()[0].size/float(n_src)*100.)
        print '%s cells that do not connect to other %s cells:' % (self.conn_type_dict[conn_type[0]], self.conn_type_dict[conn_type[1]]), (n_tgts == 0).nonzero()[0]
        print 'Weight in %.2e +- %.2e' % (w_in.mean(), w_in.std())
        print 'Weight out %.2e +- %.2e' % (w_out.mean(), w_out.std())
#        print 'debug n_tgts', n_tgts
#        print 'debug w_out', w_out

        # OUTGOING CONNECTIONS
        # plot number of outgoing connections
        ax = self.fig.add_subplot(self.n_fig_y, self.n_fig_x, fig_cnt)
        ax.bar(range(n_src), n_tgts, width=1)
        ax.set_xlim((0, n_src))
        ax.set_xlabel('Source index')
        ax.set_ylabel('Number of outgoing connections')
        title = 'Every %s cell connects on average to $%.2f\pm%.2f \, (%.1f\pm%.2f\, \%% $ of the) %s cells' % (self.conn_type_dict[conn_type[0]], \
                n_out_mean, n_out_sem, n_out_mean / n_tgt * 100., n_out_sem / n_tgt * 100.,  self.conn_type_dict[conn_type[1]])
        print title
        ax.set_title(title)

        # INCOMING CONNECTIONS
        # plot number of outgoing connections
        ax = self.fig.add_subplot(self.n_fig_y, self.n_fig_x, fig_cnt + 1)
        ax.bar(range(n_tgt), n_srcs, width=1)
        ax.set_xlim((0, n_tgt))
        ax.set_xlabel('Target index')
        ax.set_ylabel('Number of incoming connections')
        title = 'Every %s cell receives on average input from $ %.2f \pm %.2f \,(%.1f \pm %.2f \, \%% $ of the) %s cells' % (self.conn_type_dict[conn_type[1]], \
                n_in_mean, n_in_sem, n_in_mean / n_tgt * 100., n_in_sem / n_tgt * 100.,  self.conn_type_dict[conn_type[0]])
        print title
        ax.set_title(title)


        self.fig = self.create_fig()
        ax = self.fig.add_subplot(221)
        ax.bar(range(n_src), w_out, width=1)
        ax.set_xlim((0, n_src))
        ax.set_xlabel('Source neuron')
        ax.set_ylabel('Sum of outgoing weights')

        ax = self.fig.add_subplot(222)
        ax.bar(range(n_tgt), w_in, width=1)
        ax.set_xlim((0, n_tgt))
        ax.set_xlabel('Target neuron')
        ax.set_ylabel('Sum of incoming weights')

        # plot the sorted weights
        w_out_srt = w_out.copy()
        w_out_srt.sort()
        ax = self.fig.add_subplot(223)
        ax.bar(range(n_src), w_out_srt, width=1)
        ax.set_xlim((0, n_src))
        ax.set_xlabel('Source neuron')
        ax.set_ylabel('Sum of outgoing weights')

        w_in_srt = w_in.copy()
        w_in_srt.sort()
        ax = self.fig.add_subplot(224)
        ax.bar(range(n_tgt), w_in_srt, width=1)
        ax.set_xlim((0, n_tgt))
        ax.set_xlabel('Source neuron')
        ax.set_ylabel('Sum of incoming weights')
    for i in xrange(conn_data[:, 0].size):
        src, tgt, w, delay = conn_data[i, :4]
        n_in[tgt] += 1

    return n_in


fn = params['merged_conn_list_%s' % conn_type] 
if not os.path.exists(fn):
    os.system('python merge_connlists.py %s' % params['folder_name'])
output_fn = params['figures_folder'] + 'weights_and_delays_%s.png' % (conn_type)

d = np.loadtxt(fn)
print 'debug', d.shape, fn

(n_src, n_tgt, syn_type) = utils.resolve_src_tgt(conn_type, params)
n_in = get_incoming_connection_numbers(d, n_tgt)
string = 'n_%s = %.2f +- %.2f' % (conn_type, n_in.mean(), n_in.std())
string += '\nn_%s_min = %.2f' % (conn_type, n_in.min())
string += '\nn_%s_max = %.2f' % (conn_type, n_in.max())
out_fn = params['data_folder'] + 'nconn_%s.txt' % (conn_type)
f = open(out_fn, 'w')
f.write(string)
f.flush()
f.close()
print 'Writing to:', out_fn 
print string

weights = d[:, 2]
delays = d[:, 3]
w_mean, w_std = weights.mean(), weights.std()