0

I'm new to python and have the following problem:

Every Class 'Neuron' has two lists (map_dendrites, map_axons) in which I want to store IDs from other Neurons.

My Problem is that if 'map_synapses' calls 'add_axon' it writes the given ID in the list 'map_dendrites' from the calling Neuron-Object and not in the map of the called Neuron. Surprisingly it works otherwise when 'add_dendrites' is called although the code is the same (but executed first).

Does anybody know how to fix this? Thank you for your support.

# coding=utf-8

class Neuron(object):
    "Klasse Neuron"

    c_dendrites = 1  # Inputs
    c_axons = 1  # Outputs

    c_unmap_dendrites = c_dendrites # Unmapped entspricht am Anfang der Gesamtanzahl der vorhandenen Dednrites
    c_unmap_axons = c_axons # same here but axons

    map_dendrites = [0 for i in range(10)] # Verkabelung Dendrites
    map_axons = [0 for i in range(10)] # Verkabelung Axons

    def __init__(self, id, x, y, z): # 3D-Koordinaten
        "Konstruktor Neuron"

        self.id = id # Jedes Neuron besitzt eine eindeutige ID

        print 'Neuron ID %d (%d/%d/%d) erstellt' % (id, x, y, z)


    def add_dendrite(self, id): # Aus Sicht des Neurons (add_dendrite fügt an ein self.axon ein dendrite des anfragenden Neurons)

        success = False

        if(self.c_unmap_axons != 0):

            #print 'range %d - %d' % (self.c_axons, self.c_unmap_axons)
            print 'Übergebene ID: %d' % id
            #print 'Self ID: %d' % self.id
            #print len(self.map_axons)


            self.map_axons[self.c_axons - self.c_unmap_axons] = id  # Auffüllen von 0 bis self.c_axons
            print 'testX: %d' % self.map_axons[0]
            self.c_unmap_axons -= 1
            success = True

        return success


    def add_axon(self, id): # Aus Sicht des Neurons (add_axon fügt an ein self.dendrite ein Axon des anfragenden Neurons)

        success = False

        if (self.c_unmap_dendrites != 0):

            #print 'range %d - %d' % (self.c_axons, self.c_unmap_axons)
            print 'Übergebene ID: %d' % id
            #print 'Self ID: %d' % self.id
            #print len(self.map_axons)
            print 'test5: %d' % self.map_dendrites[0]
            self.map_dendrites[self.c_dendrites - self.c_unmap_dendrites] = id  # Auffüllen von 0 bis self.c_dendrites
            print 'test6: %d' % self.map_dendrites[0]
# Er nimmt hier die falsche Map
            self.c_unmap_dendrites -= 1
            success = True

        return success


    def map_synapses(self, anzahl_neuronen, ar_neurons):

        import Tools

        print 'map_synapses: Mappe Dendrites'

        # 1. Dendrites verkabeln aus self-Perspektive

        while (0 != self.c_unmap_dendrites):

            # print control1.anzahl_neuronen

            ran_neuron = Tools.randomInt(0, anzahl_neuronen - 1)  # Neuron würfeln

            while (self.id == ran_neuron):  # Gewürfeltes Neuron darf nicht das neuron selbst sein

                ran_neuron = Tools.randomInt(0, anzahl_neuronen - 1)  # Neuron würfeln

            if(ar_neurons[ran_neuron].add_dendrite(self.id)):

                #print 'range %d - %d' % (self.c_dendrites, self.c_unmap_dendrites)
                print 'Speichere %d in map_dendrites an Stelle %d' % (ran_neuron, self.c_dendrites - self.c_unmap_dendrites)
                self.map_dendrites[self.c_dendrites - self.c_unmap_dendrites] = ran_neuron
                print 'test1: %d' % self.map_dendrites[0]
                self.c_unmap_dendrites -= 1
                print 'Dendrite mapped'

        print 'map_synapses: Mappe Dendrites abgeschlossen'

        # 2. Axons verkabeln aus self-Perspektive

        print 'test2: %d' % self.map_dendrites[0]
        print 'map_synapses: Mappe Axons'

        while (0 != self.c_unmap_axons):

            ran_neuron = Tools.randomInt(0, anzahl_neuronen - 1)  # Neuron würfeln

            while (self.id == ran_neuron): # Gewürfeltes Neuron darf nicht das neuron selbst sein

                ran_neuron = Tools.randomInt(0, anzahl_neuronen - 1)  # Neuron würfeln


            print 'test3: %d' % self.map_dendrites[0]

            # 1. Dendrites verkabeln aus self-Perspektive

            print 'ran %d' % ran_neuron

            if (ar_neurons[ran_neuron].add_axon(self.id)): ## add_axon fehlerhaft !!!!!!!!!!!!!!!!!
                print 'test4: %d' % self.map_dendrites[0]
                print 'Speichere %d in map_axons an Stelle %d' % (ran_neuron, self.c_axons - self.c_unmap_axons)
                self.map_axons[self.c_axons - self.c_unmap_axons] = ran_neuron
                self.c_unmap_axons -= 1
                print 'Axon mapped'


        print 'map_synapses: Mappe Axons abgeschlossen'
        print 'map_synpases: ID %d abgeschlossen' % self.id

        self.getStatus()

        return ar_neurons

    def getStatus(self):

        print 'getStatus: Neuron ID %d' % self.id
        print 'getStatus: Dendrites_Map:'
        print ''.join(map(str, self.map_dendrites))
        print 'getStatus: Axons_Map:'
        print ''.join(map(str, self.map_axons))
2
  • this is a really long example... If you could shorten it to comply more with mcve, that would help us help you much more easily. Commented Aug 25, 2016 at 15:28
  • I think what you need is the object property not class property. Class property is shared among all of its instances. Commented Aug 25, 2016 at 15:30

1 Answer 1

2

It think the issue might be the scope of your variables. You are assigning them like this at the beginning of the class

class Neuron(object):
    c_dendrites = 1  # Inputs
    c_axons = 1  # Outputs
    c_unmap_dendrites = c_dendrites # Unmapped entspricht am Anfang der Gesamtanzahl der vorhandenen Dednrites
    c_unmap_axons = c_axons # same here but axons
    map_dendrites = [0 for i in range(10)] # Verkabelung Dendrites
    map_axons = [0 for i in range(10)] # Verkabelung Axons

but that makes them just for the class itself they aren't specific to any instance. Static?

You should use your init function and use self to define the variables that means it will be tied to the instance and then whenever you need to use them also use self.

def __init__(self, id, x, y, z): # 3D-Koordinaten
    "Konstruktor Neuron"
    self.c_dendrites = 1  # Inputs
    self.c_axons = 1  # Outputs
    self.c_unmap_dendrites = c_dendrites # Unmapped entspricht am Anfang der Gesamtanzahl der vorhandenen Dednrites
    self.c_unmap_axons = c_axons # same here but axons
    self.map_dendrites = [0 for i in range(10)] # Verkabelung Dendrites
    self.map_axons = [0 for i in range(10)] # Verkabelung Axons
    self.id = id # Jedes Neuron besitzt eine eindeutige ID
    print 'Neuron ID %d (%d/%d/%d) erstellt' % (id, x, y, z)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot dude :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.