from conmorph.connections import RecurrentInhibition
import numpy as np
import matplotlib.pyplot as plt

from conmorph.util import load_spec

labels=["polar", "circular"]

ex_ex_connections = [RecurrentInhibition.load("../../data/{:s}_recurrent_inhibition.pickle".format(label)) for label
                     in labels]
model = load_spec("morphological_model.json")

print("### Sanity checks ###")
density_ex = model["excitatory_neurons"]["density"]
density_in = model["inhibitory_neurons"]["density"]
volume = 4.0/3*np.pi* model["inhibitory_neurons"]["long_axis"]*model["inhibitory_neurons"]["short_axis"]**2


print("Expected number of containing axonal clouds: axonal_volume*density_in={:.1f}".format(density_in*volume))
[print("{:s}: {:.1f}".format(label, np.mean(conn.get_numbers_of_containing_axonal_clouds()))) for label, conn in zip(
    labels, ex_ex_connections)]
print()


print("Expected number of neighbors: number_of_neighbors_in_axonal_clouds*no_axonal_clouds = (density_ex*volume)*("
      "volume*density_in) = {:.1f}".format(
    density_ex*density_in*volume**2))
[print("{:s}: {:.1f}".format(label, np.mean(conn.get_number_of_neighbors()))) for label, conn in zip(
    labels, ex_ex_connections)]
print()
conn = ex_ex_connections[0]
distance_step = 20
max_distance = model["inhibitory_neurons"]["long_axis"]*2
bins = np.arange(0, max_distance, distance_step)
offsets= [-2, 2]
bin_width = 1
for conn, label, off in zip(ex_ex_connections, labels, offsets):
    assert isinstance(conn, RecurrentInhibition)
    distances = conn.get_distances_to_neighbors()
    print(label)
    print(len(distances))
    print(np.mean(distances))
    print(np.min(distances))
    print(np.max(distances))
    print(np.std(distances))

    distance_histo, _ = np.histogram(distances, bins)
    print(np.sum(distance_histo))
    print()
    #plt.bar(bins[:-1] + off, distance_histo, width=0.2, label=label)
    #plt.legend()

#plt.show()

exit()

#print(volume/float(v_x)**3*number_of_excitatory_neurons)
#print([np.sum(c)/number_of_inhibitory_neurons for c in connectivities])

# What could I analyse?

# distance dependence of connectivity














def get_recurrent_inhibition(in_ex_connectivity):
    n_ex = in_ex_connectivity.shape[1]
    ex_ex_connectivity = np.zeros((n_ex, n_ex))
    for idx in range(n_ex):
        connected_interneuron_indices = np.where(in_ex_connectivity[:, idx] == 1)[0]
        connected_interneurons = in_ex_connectivity[connected_interneuron_indices, :]
        ex_ex_connectivity[idx,:] = np.sum(connected_interneurons, axis=0)
    return ex_ex_connectivity

ex_ex_conns = [get_recurrent_inhibition(c) for c in connectivities]

print([np.sum(c) for c in ex_ex_conns])
print([np.mean(np.diag(c)) for c in ex_ex_conns])

bins = np.arange(10)
offsets= [-0.2, 0.2]
labels=["polar", "circular"]
for c, off, l in zip(ex_ex_conns, offsets, labels):
    hist, bins = np.histogram(c, bins=bins)
    print(bins.shape)
    print(hist.shape)
    plt.bar(bins[2:]+off-1, hist[1:], width=0.2, label=l )
plt.legend()
plt.show()


exit()
# in-degree and out-degree
# connectivity distribution of excitatory pairs