neurons.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import numpy as np
  2. from tqdm import tqdm
  3. from conmorph.connections import OverlapMatrix
  4. from interneuron_polarity.model.morphology.shapes import get_overlap, distance
  5. class NeuronType:
  6. def __init__(self, somata, axon, dendrite):
  7. self.somata = somata
  8. self.axon = axon
  9. self.dendrite = dendrite
  10. def generate_instance(self, number):
  11. somata_positions = self.somata.generate(number)
  12. axon_orientations = self.axon.orientation.generate(number)
  13. dendrite_orientations = self.dendrite.orientation.generate(number)
  14. return NeuronTypeInstance(self, number, somata_positions, axon_orientations, dendrite_orientations)
  15. class NeuronTypeInstance:
  16. def __init__(self, type, number, somata_positions, axon_orientations, dendrite_orientations):
  17. self.type = type
  18. self.number = number
  19. self.somata_positions = somata_positions
  20. self.axon_orientations = axon_orientations
  21. self.dendrite_orientations = dendrite_orientations
  22. def overlap_onto(self, other_instance, V):
  23. overlap = np.zeros((self.number, other_instance.number))
  24. # calculate all overlaps of this axons with other dendrites
  25. number_of_overlaps = self.number*other_instance.number
  26. t = tqdm(total=number_of_overlaps)
  27. #!!! Make this faster by just considering the volume around the axon
  28. # Waste less volume by getting the minimal rectangle around the form and then turn the orientation of the other form
  29. for projecting_idx in np.arange(0, self.number):
  30. center_projecting = self.somata_positions[:, projecting_idx]
  31. orientation = self.axon_orientations[:, projecting_idx]
  32. volume = V
  33. density_axon = self.type.axon.form.get_density(center_projecting, orientation, volume)
  34. for projected_idx in np.arange(0, other_instance.number):
  35. center_projected = other_instance.somata_positions[:, projected_idx]
  36. if distance(center_projecting, center_projected) > (self.type.axon.form.r_long+other_instance.type.dendrite.form.r_long):
  37. overlap[projecting_idx, projected_idx] = 0
  38. else:
  39. density_dendrite = other_instance.type.dendrite.form.get_density(center_projected, other_instance.dendrite_orientations[:, projected_idx], volume)
  40. overlap[projecting_idx, projected_idx] = get_overlap(density_axon,density_dendrite)
  41. t.update(1)
  42. return overlap
  43. class NeuralTissue(object):
  44. def __init__(self, volume, excitatory_population, inhibitory_population):
  45. self.volume = volume
  46. self.excitatory_population = excitatory_population
  47. self.inhibitory_population = inhibitory_population
  48. def calculate_overlap_matrix(self, number_of_excitatory_neurons, number_of_inhibitory_neurons):
  49. total_number_of_neurons = number_of_excitatory_neurons+number_of_inhibitory_neurons
  50. excitatory_neurons = self.excitatory_population.generate_instance(number_of_excitatory_neurons)
  51. inhibitory_neurons = self.inhibitory_population.generate_instance(number_of_inhibitory_neurons)
  52. overlaps = np.zeros((total_number_of_neurons, total_number_of_neurons))
  53. print("Ex-ex")
  54. overlaps[:number_of_excitatory_neurons, :number_of_excitatory_neurons] = excitatory_neurons.overlap_onto(excitatory_neurons, self.volume)
  55. print("Ex-in")
  56. overlaps[:number_of_excitatory_neurons, number_of_excitatory_neurons:] = excitatory_neurons.overlap_onto(inhibitory_neurons, self.volume)
  57. print("In-ex")
  58. overlaps[number_of_excitatory_neurons:, :number_of_excitatory_neurons] = inhibitory_neurons.overlap_onto(excitatory_neurons, self.volume)
  59. print("In-in")
  60. overlaps[number_of_excitatory_neurons:, number_of_excitatory_neurons:] = inhibitory_neurons.overlap_onto(inhibitory_neurons, self.volume)
  61. overlap = OverlapMatrix(excitatory_neurons, inhibitory_neurons, overlaps)
  62. return overlap