コード例 #1
0
        public static bool Load(ref NeuronArray theNeuronArray, string fileName)
        {
            FileStream file;

            try
            {
                file = File.Open(fileName, FileMode.Open, FileAccess.Read);
            }
            catch (Exception e)
            {
                MessageBox.Show("Could not open file because: " + e.Message);
                RemoveFileFromMRUList(fileName);
                return(false);
            }

            // first check if the required start tag is present in the file...
            byte[] buffer = new byte[60];
            file.Read(buffer, 0, 60);
            string line = Encoding.UTF8.GetString(buffer, 0, buffer.Length);

            if (line.Contains("NeuronArray"))
            {
                file.Seek(0, SeekOrigin.Begin);
            }
            else
            {
                file.Close();
                MessageBox.Show("File is no valid Brain Simulator II XML file.");
                return(false);
            }

            MainWindow.thisWindow.SetProgress(0, "Loading Network File");
            theNeuronArray = new NeuronArray();

            XmlSerializer reader1 = new XmlSerializer(typeof(NeuronArray), GetModuleTypes());

            theNeuronArray = (NeuronArray)reader1.Deserialize(file);
            file.Close();

            //the above automatically loads the content of the neuronArray object but can't load the neurons themselves
            //because of formatting changes
            XmlDocument xmldoc = new XmlDocument();
            XmlNodeList neuronNodes;
            FileStream  fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            xmldoc.Load(fs);
            fs.Close();

            int arraySize = theNeuronArray.arraySize;

            theNeuronArray.Initialize(arraySize, theNeuronArray.rows);
            neuronNodes = xmldoc.GetElementsByTagName("Neuron");

            for (int i = 0; i < neuronNodes.Count; i++)
            {
                var progress = i / (float)neuronNodes.Count;
                progress *= 100;
                if (progress != 0 && MainWindow.thisWindow.SetProgress(progress, ""))
                {
                    MainWindow.thisWindow.SetProgress(100, "");
                    return(false);
                }

                XmlElement  neuronNode = (XmlElement)neuronNodes[i];
                XmlNodeList idNodes    = neuronNode.GetElementsByTagName("Id");
                int         id         = i; //this is a hack to read files where all neurons were included but no Id's
                if (idNodes.Count > 0)
                {
                    int.TryParse(idNodes[0].InnerText, out id);
                }
                if (id == -1)
                {
                    continue;
                }

                Neuron n = theNeuronArray.GetNeuron(id);
                n.Owner = theNeuronArray;
                n.id    = id;

                foreach (XmlElement node in neuronNode.ChildNodes)
                {
                    string name = node.Name;
                    switch (name)
                    {
                    case "Label":
                        n.Label = node.InnerText;
                        break;

                    case "Model":
                        Enum.TryParse(node.InnerText, out Neuron.modelType theModel);
                        n.model = theModel;
                        break;

                    case "LeakRate":
                        float.TryParse(node.InnerText, out float leakRate);
                        n.leakRate = leakRate;
                        break;

                    case "AxonDelay":
                        int.TryParse(node.InnerText, out int axonDelay);
                        n.axonDelay = axonDelay;
                        break;

                    case "LastCharge":
                        if (n.model != Neuron.modelType.Color)
                        {
                            float.TryParse(node.InnerText, out float lastCharge);
                            n.LastCharge    = lastCharge;
                            n.currentCharge = lastCharge;
                        }
                        else     //is color
                        {
                            int.TryParse(node.InnerText, out int lastChargeInt);
                            n.LastChargeInt = lastChargeInt;
                            n.currentCharge = lastChargeInt;     //current charge is not used on color neurons
                        }
                        break;

                    case "ShowSynapses":
                        bool.TryParse(node.InnerText, out bool showSynapses);
                        if (showSynapses)
                        {
                            MainWindow.arrayView.AddShowSynapses(n.id);
                        }
                        break;

                    case "Synapses":
                        theNeuronArray.SetCompleteNeuron(n);
                        XmlNodeList synapseNodess = node.GetElementsByTagName("Synapse");
                        foreach (XmlNode synapseNode in synapseNodess)
                        {
                            Synapse s = new Synapse();
                            foreach (XmlNode synapseAttribNode in synapseNode.ChildNodes)
                            {
                                string name1 = synapseAttribNode.Name;
                                switch (name1)
                                {
                                case "TargetNeuron":
                                    int.TryParse(synapseAttribNode.InnerText, out int target);
                                    s.targetNeuron = target;
                                    break;

                                case "Weight":
                                    float.TryParse(synapseAttribNode.InnerText, out float weight);
                                    s.weight = weight;
                                    break;

                                case "IsHebbian":         //Obsolete: backwards compatibility
                                    bool.TryParse(synapseAttribNode.InnerText, out bool isheb);
                                    if (isheb)
                                    {
                                        s.model = Synapse.modelType.Hebbian1;
                                    }
                                    else
                                    {
                                        s.model = Synapse.modelType.Fixed;
                                    }
                                    break;

                                case "Model":
                                    Enum.TryParse(synapseAttribNode.InnerText, out Synapse.modelType model);
                                    s.model = model;
                                    break;
                                }
                            }
                            n.AddSynapse(s.targetNeuron, s.weight, s.model);
                        }
                        break;
                    }
                }
                theNeuronArray.SetCompleteNeuron(n);
            }
            MainWindow.thisWindow.SetProgress(100, "");
            return(true);
        }
コード例 #2
0
ファイル: XmlFile.cs プロジェクト: stjordanis/BrainSimII
        public static bool Load(ref NeuronArray theNeuronArray, string fileName)
        {
            FileStream file = File.Open(fileName, FileMode.Open);

            XmlSerializer reader1 = new XmlSerializer(typeof(NeuronArray), GetModuleTypes());

            theNeuronArray = (NeuronArray)reader1.Deserialize(file);
            file.Close();

            //the above automatically loads the content of the neuronArray object but can't load the neurons themselves
            //because of formatting changes
            XmlDocument xmldoc = new XmlDocument();
            XmlNodeList neuronNodes;
            FileStream  fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            xmldoc.Load(fs);
            fs.Close();

            int arraySize = theNeuronArray.arraySize;

            theNeuronArray.Initialize(arraySize);
            neuronNodes = xmldoc.GetElementsByTagName("Neuron");

            for (int i = 0; i < neuronNodes.Count; i++)
            {
                XmlElement  neuronNode = (XmlElement)neuronNodes[i];
                XmlNodeList idNodes    = neuronNode.GetElementsByTagName("Id");
                if (idNodes.Count < 1)
                {
                    continue;
                }
                int id = -1;
                int.TryParse(idNodes[0].InnerText, out id);
                if (id == -1)
                {
                    continue;
                }
                Neuron n = theNeuronArray.GetNeuron(id);
                n.Owner = theNeuronArray;
                n.id    = id;

                foreach (XmlElement node in neuronNode.ChildNodes)
                {
                    string name = node.Name;
                    switch (name)
                    {
                    case "Label":
                        n.label = node.InnerText;
                        break;

                    case "Model":
                        Enum.TryParse(node.InnerText, out Neuron.modelType theModel);
                        n.model = theModel;
                        break;

                    case "LeakRate":
                        float.TryParse(node.InnerText, out float leakRate);
                        n.leakRate = leakRate;
                        break;

                    case "LastCharge":
                        float.TryParse(node.InnerText, out float lastCharge);
                        n.LastCharge    = lastCharge;
                        n.currentCharge = lastCharge;
                        break;

                    case "Synapses":
                        theNeuronArray.SetCompleteNeuron(n);
                        XmlNodeList synapseNodess = node.GetElementsByTagName("Synapse");
                        foreach (XmlNode synapseNode in synapseNodess)
                        {
                            Synapse s = new Synapse();
                            foreach (XmlNode synapseAttribNode in synapseNode.ChildNodes)
                            {
                                string name1 = synapseAttribNode.Name;
                                switch (name1)
                                {
                                case "TargetNeuron":
                                    int.TryParse(synapseAttribNode.InnerText, out int target);
                                    s.targetNeuron = target;
                                    break;

                                case "Weight":
                                    float.TryParse(synapseAttribNode.InnerText, out float weight);
                                    s.weight = weight;
                                    break;

                                case "IsHebbian":
                                    bool.TryParse(synapseAttribNode.InnerText, out bool isheb);
                                    s.isHebbian = isheb;

                                    break;
                                }
                            }
                            n.AddSynapse(s.targetNeuron, s.weight, s.isHebbian);
                        }
                        break;
                    }
                }
                theNeuronArray.SetCompleteNeuron(n);
            }
            return(true);
        }
コード例 #3
0
ファイル: XmlFile.cs プロジェクト: Moorelife/BrainSimII
        public static bool Load(ref NeuronArray theNeuronArray, string fileName)
        {
            FileStream file = File.Open(fileName, FileMode.Open);

            XmlSerializer reader1 = new XmlSerializer(typeof(NeuronArray), GetModuleTypes());

            theNeuronArray = (NeuronArray)reader1.Deserialize(file);
            file.Close();

            //the above automatically loads the content of the neuronArray object but can't load the neurons themselves
            //because of formatting changes
            XmlDocument xmldoc = new XmlDocument();
            XmlNodeList neuronNodes;
            FileStream  fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            xmldoc.Load(fs);
            fs.Close();

            int arraySize = theNeuronArray.arraySize;

            theNeuronArray.Initialize(arraySize);
            neuronNodes = xmldoc.GetElementsByTagName("Neuron");

            for (int i = 0; i < neuronNodes.Count; i++)
            {
                var progress = i / (float)neuronNodes.Count;
                if (i % 1000 == 0)
                {
                    MainWindow.thisWindow.SetProgress(progress);
                }

                XmlElement  neuronNode = (XmlElement)neuronNodes[i];
                XmlNodeList idNodes    = neuronNode.GetElementsByTagName("Id");
                int         id         = i; //this is a hack to read files where all neurons were included but no Id's
                if (idNodes.Count > 0)
                {
                    int.TryParse(idNodes[0].InnerText, out id);
                }
                if (id == -1)
                {
                    continue;
                }

                Neuron n = theNeuronArray.GetNeuron(id);
                n.Owner = theNeuronArray;
                n.id    = id;

                foreach (XmlElement node in neuronNode.ChildNodes)
                {
                    string name = node.Name;
                    switch (name)
                    {
                    case "Label":
                        n.Label = node.InnerText;
                        break;

                    case "Model":
                        Enum.TryParse(node.InnerText, out Neuron.modelType theModel);
                        n.model = theModel;
                        break;

                    case "LeakRate":
                        float.TryParse(node.InnerText, out float leakRate);
                        n.leakRate = leakRate;
                        break;

                    case "AxonDelay":
                        int.TryParse(node.InnerText, out int axonDelay);
                        n.axonDelay = axonDelay;
                        break;

                    case "LastCharge":
                        if (n.model != Neuron.modelType.Color)
                        {
                            float.TryParse(node.InnerText, out float lastCharge);
                            n.LastCharge    = lastCharge;
                            n.currentCharge = lastCharge;
                        }
                        else     //is color
                        {
                            int.TryParse(node.InnerText, out int lastChargeInt);
                            n.LastChargeInt = lastChargeInt;
                            n.currentCharge = lastChargeInt;     //current charge is not used on color neurons
                        }
                        break;

                    case "ShowSynapses":
                        bool.TryParse(node.InnerText, out bool showSynapses);
                        if (showSynapses)
                        {
                            MainWindow.arrayView.AddShowSynapses(n.id);
                        }
                        break;

                    case "Synapses":
                        theNeuronArray.SetCompleteNeuron(n);
                        XmlNodeList synapseNodess = node.GetElementsByTagName("Synapse");
                        foreach (XmlNode synapseNode in synapseNodess)
                        {
                            Synapse s = new Synapse();
                            foreach (XmlNode synapseAttribNode in synapseNode.ChildNodes)
                            {
                                string name1 = synapseAttribNode.Name;
                                switch (name1)
                                {
                                case "TargetNeuron":
                                    int.TryParse(synapseAttribNode.InnerText, out int target);
                                    s.targetNeuron = target;
                                    break;

                                case "Weight":
                                    float.TryParse(synapseAttribNode.InnerText, out float weight);
                                    s.weight = weight;
                                    break;

                                case "IsHebbian":         //Obsolete: backwards compatibility
                                    bool.TryParse(synapseAttribNode.InnerText, out bool isheb);
                                    if (isheb)
                                    {
                                        s.model = Synapse.modelType.Hebbian1;
                                    }
                                    else
                                    {
                                        s.model = Synapse.modelType.Fixed;
                                    }
                                    break;

                                case "Model":
                                    Enum.TryParse(synapseAttribNode.InnerText, out Synapse.modelType model);
                                    s.model = model;
                                    break;
                                }
                            }
                            n.AddSynapse(s.targetNeuron, s.weight, s.model);
                        }
                        break;
                    }
                }
                theNeuronArray.SetCompleteNeuron(n);
            }
            MainWindow.thisWindow.SetProgress(-1);
            return(true);
        }