Exemplo n.º 1
0
        public void DeleteSelection(bool deleteBoundarySynapses = true)
        {
            MainWindow.theNeuronArray.SetUndoPoint();
            List <int> neuronsToDelete = theSelection.EnumSelectedNeurons();

            foreach (int nID in neuronsToDelete)
            {
                Neuron n = MainWindow.theNeuronArray.GetNeuron(nID);
                if (deleteBoundarySynapses)
                {
                    foreach (Synapse s in n.synapsesFrom)
                    {
                        Neuron source = MainWindow.theNeuronArray.GetNeuron(s.targetNeuron);
                        if (source != null && theSelection.NeuronInSelection(source.id) == 0)
                        {
                            source.DeleteSynapseWithUndo(n.id);
                        }
                    }
                }
                for (int i = 0; i < n.synapses.Count; i++)
                {
                    n.DeleteSynapseWithUndo(n.synapses[i].targetNeuron);
                }
                n.AddUndoInfo();
                n.CurrentCharge = 0;
                n.LastCharge    = 0;
                n.Model         = Neuron.modelType.IF;

                n.Label = "";
                n.Update();
            }
            DeleteModulesInSelection();
            Update();
        }
Exemplo n.º 2
0
        public void MoveOneNeuron(Neuron n, Neuron nNewLocation)
        {
            n.AddUndoInfo();
            nNewLocation.AddUndoInfo();

            //copy the neuron attributes and delete them from the old neuron.
            n.Copy(nNewLocation);
            MainWindow.theNeuronArray.SetCompleteNeuron(nNewLocation);
            if (FiringHistory.NeuronIsInFiringHistory(n.id))
            {
                FiringHistory.RemoveNeuronFromHistoryWindow(n.id);
                FiringHistory.AddNeuronToHistoryWindow(nNewLocation.id);
            }

            //for all the synapses going out this neuron, change to going from new location
            //don't use a foreach here because the body of the loop may delete a list entry
            for (int k = 0; k < n.Synapses.Count; k++)
            {
                Synapse s = n.Synapses[k];
                if (s.targetNeuron != n.id)
                {
                    nNewLocation.AddSynapseWithUndo(s.targetNeuron, s.weight, s.model);
                }
                else
                {
                    nNewLocation.AddSynapseWithUndo(nNewLocation.id, s.weight, s.model);
                }
                n.DeleteSynapseWithUndo(n.synapses[k].targetNeuron);
            }

            //for all the synapses coming into this neuron, change the synapse target to new location
            for (int k = 0; k < n.SynapsesFrom.Count; k++)
            {
                Synapse reverseSynapse = n.SynapsesFrom[k]; //(from synapses are sort-of backward
                if (reverseSynapse.targetNeuron != -1)      //?
                {
                    Neuron sourceNeuron = MainWindow.theNeuronArray.GetNeuron(reverseSynapse.targetNeuron);
                    sourceNeuron.DeleteSynapseWithUndo(n.id);
                    if (sourceNeuron.id != n.id)
                    {
                        sourceNeuron.AddSynapseWithUndo(nNewLocation.id, reverseSynapse.weight, reverseSynapse.model);
                    }
                }
            }

            n.Clear();
        }
Exemplo n.º 3
0
        private static void SetValueInSelectedNeurons(Neuron n, string property)
        {
            bool neuronInSelection = NeuronInSelection(n.id);

            if (neuronInSelection)
            {
                List <int> theNeurons = theNeuronArrayView.theSelection.EnumSelectedNeurons();
                //special case for label because they are auto-incremented,
                //clear all the labels first to avoid collisions
                if (property == "label")
                {
                    for (int i = 0; i < theNeurons.Count; i++)
                    {
                        Neuron n1 = MainWindow.theNeuronArray.GetNeuron(theNeurons[i]);
                        if (n1.id != n.id)
                        {
                            n1.label = "";
                            n1.Update();
                        }
                    }
                }
                for (int i = 0; i < theNeurons.Count; i++)
                {
                    Neuron n1 = MainWindow.theNeuronArray.GetNeuron(theNeurons[i]);
                    n1.AddUndoInfo();
                    switch (property)
                    {
                    case "currentCharge":
                        if (n.model == Neuron.modelType.Color)
                        {
                            n1.SetValueInt(n.LastChargeInt);
                        }
                        else
                        {
                            n1.currentCharge = n.currentCharge;
                            n1.lastCharge    = n.currentCharge;
                        }
                        break;

                    case "leakRate": n1.leakRate = n.leakRate; break;

                    case "axonDelay": n1.axonDelay = n.axonDelay; break;

                    case "model": n1.model = n.model; break;

                    case "enable": n1.leakRate = n.leakRate; break;

                    case "history":
                        if (FiringHistory.NeuronIsInFiringHistory(n.id))
                        {
                            FiringHistory.AddNeuronToHistoryWindow(n1.id);
                            OpenHistoryWindow();
                        }
                        else
                        {
                            FiringHistory.RemoveNeuronFromHistoryWindow(n1.id);
                        }
                        break;

                    case "synapses":
                        if (MainWindow.arrayView.IsShowingSnapses(n.id))
                        {
                            MainWindow.arrayView.AddShowSynapses(n1.id);
                        }
                        else
                        {
                            MainWindow.arrayView.RemoveShowSynapses(n1.id);
                        }
                        break;

                    case "label":
                        if (n.label == "")
                        {
                            n1.label = "";
                        }
                        else if (n.id != n1.id)
                        {
                            string newLabel = n.label;
                            while (MainWindow.theNeuronArray.GetNeuron(newLabel) != null)
                            {
                                int num        = 0;
                                int digitCount = 0;
                                while (Char.IsDigit(newLabel[newLabel.Length - 1]))
                                {
                                    int.TryParse(newLabel[newLabel.Length - 1].ToString(), out int digit);
                                    num = num + (int)Math.Pow(10, digitCount) * digit;
                                    digitCount++;
                                    newLabel = newLabel.Substring(0, newLabel.Length - 1);
                                }
                                num++;
                                newLabel = newLabel + num.ToString();
                            }
                            n1.label = newLabel;
                        }
                        break;
                    }
                    n1.Update();
                }
            }
        }
Exemplo n.º 4
0
        private static void Cm_Closed(object sender, RoutedEventArgs e)
        {
            if (sender is ContextMenu cm)
            {
                if (!cm.IsOpen)
                {
                    return;
                }
                cm.IsOpen = false;
                if (cmCancelled)
                {
                    MainWindow.Update();
                    return;
                }
                MainWindow.theNeuronArray.SetUndoPoint();
                int    neuronID = (int)cm.GetValue(NeuronIDProperty);
                Neuron n        = MainWindow.theNeuronArray.GetNeuron(neuronID);
                n.AddUndoInfo();

                bool    applyToAll = false;
                Control cc         = Utils.FindByName(cm, "ApplyToSelection");
                if (cc is CheckBox cb)
                {
                    if (cb.IsChecked == true)
                    {
                        applyToAll = true;
                    }
                }

                cc = Utils.FindByName(cm, "Label");
                if (cc is TextBox tb)
                {
                    string newLabel = tb.Text;
                    if (int.TryParse(newLabel, out int dummy))
                    {
                        newLabel = "_" + newLabel;
                    }
                    if (labelChanged)
                    {
                        MainWindow.theNeuronArray.SetUndoPoint();
                        n.Label = newLabel;
                        if (applyToAll)
                        {
                            SetValueInSelectedNeurons(n, "label");
                        }
                    }
                }
                cc = Utils.FindByName(cm, "Model");
                if (cc is ComboBox cb0)
                {
                    ListBoxItem      lbi = (ListBoxItem)cb0.SelectedItem;
                    Neuron.modelType nm  = (Neuron.modelType)System.Enum.Parse(typeof(Neuron.modelType), lbi.Content.ToString());
                    if (modelChanged)
                    {
                        n.model = nm;
                        if (applyToAll)
                        {
                            SetValueInSelectedNeurons(n, "model");
                        }
                    }
                }
                cc = Utils.FindByName(cm, "CurrentCharge");
                if (cc is ComboBox tb1)
                {
                    if (n.model == Neuron.modelType.Color)
                    {
                        try
                        {
                            uint newCharge = Convert.ToUInt32(tb1.Text, 16);
                            if (chargeChanged)
                            {
                                n.SetValueInt((int)newCharge);
                                n.lastCharge = newCharge;
                                if (applyToAll)
                                {
                                    SetValueInSelectedNeurons(n, "currentCharge");
                                }
                                Utils.AddToValues(newCharge, colorValues);
                            }
                        }
                        catch { };
                    }
                    else
                    {
                        float.TryParse(tb1.Text, out float newCharge);
                        if (chargeChanged)
                        {
                            n.SetValue(newCharge);
                            n.lastCharge = newCharge;
                            if (applyToAll)
                            {
                                SetValueInSelectedNeurons(n, "currentCharge");
                            }
                            Utils.AddToValues(newCharge, currentChargeValues);
                        }
                    }
                }
                cc = Utils.FindByName(cm, "LeakRate");
                if (cc is ComboBox tb2)
                {
                    float.TryParse(tb2.Text, out float leakRate);
                    if (leakRateChanged)
                    {
                        n.LeakRate = leakRate;
                        if (applyToAll)
                        {
                            SetValueInSelectedNeurons(n, "leakRate");
                        }
                        if (n.model == Neuron.modelType.LIF)
                        {
                            Utils.AddToValues(leakRate, leakRateValues);
                        }
                        if (n.model == Neuron.modelType.Random)
                        {
                            Utils.AddToValues(leakRate, stdDevValues);
                        }
                        if (n.model == Neuron.modelType.Burst)
                        {
                            Utils.AddToValues(leakRate, axonDelayValues);
                        }
                    }
                }
                else
                {
                    n.leakRate = 0;
                }
                cc = Utils.FindByName(cm, "AxonDelay");
                if (cc is ComboBox tb3)
                {
                    int.TryParse(tb3.Text, out int axonDelay);
                    if (axonDelayChanged)
                    {
                        n.axonDelay = axonDelay;
                        if (applyToAll)
                        {
                            SetValueInSelectedNeurons(n, "axonDelay");
                        }
                        if (n.model == Neuron.modelType.Random)
                        {
                            Utils.AddToValues(axonDelay, meanValues);
                        }
                        else if (n.model == Neuron.modelType.Always)
                        {
                            Utils.AddToValues(axonDelay, alwaysDelayValues);
                        }
                        else if (n.model == Neuron.modelType.Burst)
                        {
                            Utils.AddToValues(axonDelay, alwaysDelayValues);
                        }
                        else
                        {
                            Utils.AddToValues(axonDelay, axonDelayValues);
                        }
                    }
                }
                cc = Utils.FindByName(cm, "Synapses");
                if (cc is CheckBox cb2)
                {
                    if (synapsesChanged)
                    {
                        if (cb2.IsChecked == true)
                        {
                            MainWindow.arrayView.AddShowSynapses(n.id);
                        }
                        else
                        {
                            MainWindow.arrayView.RemoveShowSynapses(n.id);
                        }
                        if (applyToAll)
                        {
                            SetValueInSelectedNeurons(n, "synapses");
                        }
                    }
                }

                cc = Utils.FindByName(cm, "Enabled");
                if (cc is CheckBox cb1)
                {
                    if (enabledChanged)
                    {
                        if (cb1.IsChecked == true)
                        {
                            n.leakRate = Math.Abs(n.leakRate);
                        }
                        else
                        {
                            n.leakRate = Math.Abs(n.leakRate) * -1.0f;
                        }

                        if (applyToAll)
                        {
                            SetValueInSelectedNeurons(n, "enable");
                        }
                    }
                }

                cc = Utils.FindByName(cm, "History");
                if (cc is CheckBox cb3)
                {
                    if (historyChanged)
                    {
                        if (cb3.IsChecked == true)
                        {
                            FiringHistory.AddNeuronToHistoryWindow(n.id);
                            OpenHistoryWindow();
                        }
                        else
                        {
                            FiringHistory.RemoveNeuronFromHistoryWindow(n.id);
                        }
                        if (applyToAll)
                        {
                            SetValueInSelectedNeurons(n, "history");
                        }
                    }
                }
                n.Update();
            }
            MainWindow.Update();
        }