private void UpdateScrollbars() { //without this the scrollbar thumbbars flicker if (scrolling) { return; //to get rid of this, we'll need to take the display translation transformation into account } NeuronArray theNeuronArray = MainWindow.theNeuronArray; double totalWidth = theNeuronArray.arraySize / theNeuronArray.rows * dp.NeuronDisplaySize; double visibleWidth = theCanvas.ActualWidth; scrollBarH.Minimum = -visibleWidth + 3 * dp.NeuronDisplaySize; scrollBarH.Maximum = totalWidth - 3 * dp.NeuronDisplaySize; scrollBarH.Value = -dp.DisplayOffset.X; scrollBarH.ViewportSize = visibleWidth; scrollBarH.SmallChange = dp.NeuronDisplaySize * 1.1; scrollBarH.LargeChange = visibleWidth; scrollBarHOldValue = scrollBarH.Value; double totalHeight = theNeuronArray.rows * dp.NeuronDisplaySize; double visibleHeight = theCanvas.ActualHeight; scrollBarV.Minimum = -visibleHeight + 3 * dp.NeuronDisplaySize; scrollBarV.Maximum = totalHeight - 3 * dp.NeuronDisplaySize; scrollBarV.Value = -dp.DisplayOffset.Y; scrollBarV.ViewportSize = visibleHeight; scrollBarV.SmallChange = dp.NeuronDisplaySize * 1.1; scrollBarV.LargeChange = visibleHeight; scrollBarVOldValue = scrollBarV.Value; }
//this reloads the file which was being used on the previous run of the program private void Window_ContentRendered(object sender, EventArgs e) { //if the left shift key is pressed, don't load the file if (Keyboard.IsKeyUp(Key.LeftShift)) { try { currentFileName = (string)Properties.Settings.Default["CurrentFile"]; if (currentFileName != "") { LoadFile(currentFileName); } else //force a new file creation on startup if no file name set { theNeuronArray = new NeuronArray(); arrayView.Dp.NeuronDisplaySize = 62; arrayView.Dp.DisplayOffset = new Point(0, 0); theNeuronArray.Initialize(450, 15); setTitleBar(); Update(); } } //various errors might have happened so we'll just ignore them all and start with a fresh file catch (Exception e1) { e1.GetType(); MessageBox.Show("Error encountered in file load: " + e1.Message); } } }
public void CreateEmptyNetwork() { theNeuronArray = new NeuronArray(); arrayView.Dp.NeuronDisplaySize = 62; arrayView.Dp.DisplayOffset = new Point(0, 0); theNeuronArray.Initialize(450, 15); Update(); }
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); }
public static bool Save(NeuronArray theNeuronArray, string fileName) { string tempFile = System.IO.Path.GetTempFileName(); FileStream file = File.Create(tempFile); XmlSerializer writer = new XmlSerializer(typeof(NeuronArray), GetModuleTypes()); writer.Serialize(file, theNeuronArray); file.Position = 0;; XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(file); XmlElement root = xmldoc.DocumentElement; XmlNode neuronsNode = xmldoc.CreateNode("element", "Neurons", ""); root.AppendChild(neuronsNode); for (int i = 0; i < theNeuronArray.arraySize; i++) { Neuron n = theNeuronArray.GetCompleteNeuron(i); if (n.inUse) { string label = theNeuronArray.GetNeuronLabel(n.id); //this is needed bacause inUse is true if any synapse points to this neuron--we don't need to bother with that if it's the only thing if (n.synapses.Count != 0 || label != "" || n.lastCharge != 0 || n.leakRate != 0.1f || n.model != Neuron.modelType.Std) { XmlNode neuronNode = xmldoc.CreateNode("element", "Neuron", ""); neuronsNode.AppendChild(neuronNode); XmlNode attrNode = xmldoc.CreateNode("element", "Id", ""); attrNode.InnerText = n.id.ToString(); neuronNode.AppendChild(attrNode); if (n.lastCharge != 0) { attrNode = xmldoc.CreateNode("element", "LastCharge", ""); attrNode.InnerText = n.lastCharge.ToString(); neuronNode.AppendChild(attrNode); } if (n.leakRate != 0.1f) { attrNode = xmldoc.CreateNode("element", "LeakRate", ""); attrNode.InnerText = n.leakRate.ToString(); neuronNode.AppendChild(attrNode); } if (n.model != Neuron.modelType.Std) { attrNode = xmldoc.CreateNode("element", "Model", ""); attrNode.InnerText = n.model.ToString(); neuronNode.AppendChild(attrNode); } if (label != "") { attrNode = xmldoc.CreateNode("element", "Label", ""); attrNode.InnerText = label; neuronNode.AppendChild(attrNode); } if (n.synapses.Count > 0) { XmlNode synapsesNode = xmldoc.CreateNode("element", "Synapses", ""); neuronNode.AppendChild(synapsesNode); foreach (Synapse s in n.synapses) { XmlNode synapseNode = xmldoc.CreateNode("element", "Synapse", ""); synapsesNode.AppendChild(synapseNode); if (s.weight != 1) { attrNode = xmldoc.CreateNode("element", "Weight", ""); attrNode.InnerText = s.weight.ToString(); synapseNode.AppendChild(attrNode); } attrNode = xmldoc.CreateNode("element", "TargetNeuron", ""); attrNode.InnerText = s.targetNeuron.ToString(); synapseNode.AppendChild(attrNode); if (s.isHebbian) { attrNode = xmldoc.CreateNode("element", "IsHebbian", ""); attrNode.InnerText = "true"; synapseNode.AppendChild(attrNode); } } } } } } file.Position = 0; xmldoc.Save(file); file.Close(); File.Copy(tempFile, fileName, true); File.Delete(tempFile); return(true); }
public void PasteNeurons() { NeuronArray myClipBoard = MainWindow.myClipBoard; if (targetNeuronIndex == -1) { return; } if (myClipBoard == null) { return; } //We are pasting neurons from the clipboard. //The arrays have different sizes so we may by row-col. //first check to see if the destination is claar and warn List <int> targetNeurons = new List <int>(); for (int i = 0; i < myClipBoard.arraySize; i++) { if (myClipBoard.GetNeuron(i, true) != null) { targetNeurons.Add(GetNeuronArrayId(i)); } } MainWindow.theNeuronArray.GetNeuronLocation(targetNeuronIndex, out int col, out int row); if (col + myClipBoard.Cols > MainWindow.theNeuronArray.Cols || row + myClipBoard.rows > MainWindow.theNeuronArray.rows) { MessageBoxResult result = MessageBox.Show("Paste would exceed neuron array boundary!", "Error", MessageBoxButton.OK); return; } if (!IsDestinationClear(targetNeurons, 0, true)) { MessageBoxResult result = MessageBox.Show("Some desination neurons are in use and will be overwritten, continue?", "Continue", MessageBoxButton.YesNo); if (result == MessageBoxResult.No) { return; } } MainWindow.theNeuronArray.SetUndoPoint(); //now paste the neurons for (int i = 0; i < myClipBoard.arraySize; i++) { if (myClipBoard.GetNeuron(i) != null) { int destID = GetNeuronArrayId(i); MainWindow.theNeuronArray.GetNeuron(destID).AddUndoInfo(); Neuron n = myClipBoard.GetCompleteNeuron(i, true); n.Owner = myClipBoard; n.synapses = myClipBoard.GetSynapsesList(i); Neuron sourceNeuron = n.Clone(); sourceNeuron.id = destID; while (sourceNeuron.label != "" && MainWindow.theNeuronArray.GetNeuron(sourceNeuron.label) != null) { int num = 0; int digitCount = 0; while (sourceNeuron.label != "" && Char.IsDigit(sourceNeuron.label[sourceNeuron.label.Length - 1])) { int.TryParse(sourceNeuron.label[sourceNeuron.label.Length - 1].ToString(), out int digit); num = num + (int)Math.Pow(10, digitCount) * digit; digitCount++; sourceNeuron.label = sourceNeuron.label.Substring(0, sourceNeuron.label.Length - 1); } num++; sourceNeuron.label = sourceNeuron.label + num.ToString(); } sourceNeuron.Owner = MainWindow.theNeuronArray; sourceNeuron.Label = sourceNeuron.label; MainWindow.theNeuronArray.SetNeuron(destID, sourceNeuron); foreach (Synapse s in n.Synapses) { MainWindow.theNeuronArray.GetNeuron(destID). AddSynapseWithUndo(GetNeuronArrayId(s.TargetNeuron), s.Weight, s.model); } } } //handle boundary synapses foreach (BoundarySynapse b in boundarySynapsesOut) { int sourceID = GetNeuronArrayId(b.innerNeuronID); Neuron targetNeuron = MainWindow.theNeuronArray.GetNeuron(b.outerNeuronID); if (targetNeuron != null) { MainWindow.theNeuronArray.GetNeuron(sourceID).AddSynapseWithUndo(targetNeuron.id, b.weight, b.model); } } foreach (BoundarySynapse b in boundarySynapsesIn) { int targetID = GetNeuronArrayId(b.innerNeuronID); Neuron sourceNeuron = MainWindow.theNeuronArray.GetNeuron(b.outerNeuronID); if (sourceNeuron != null) { sourceNeuron.AddSynapseWithUndo(targetID, b.weight, b.model); } } //paste modules foreach (ModuleView mv in myClipBoard.modules) { ModuleView newMV = new ModuleView() { FirstNeuron = GetNeuronArrayId(mv.FirstNeuron), TheModule = mv.TheModule, Color = mv.Color, Height = mv.Height, Width = mv.Width, Label = mv.Label, CommandLine = mv.CommandLine, }; MainWindow.theNeuronArray.modules.Add(newMV); } Update(); }
private bool LoadFile(string fileName) { CloseAllModuleDialogs(); CloseHistoryWindow(); CloseNotesWindow(); theNeuronArrayView.theSelection.selectedRectangles.Clear(); CloseAllModuleDialogs(); SuspendEngine(); try { // Load the data from the XML to the Brainsim Engine. FileStream file = File.Open(fileName, FileMode.Open); XmlSerializer reader = new XmlSerializer(typeof(NeuronArray), GetModuleTypes()); theNeuronArray = (NeuronArray)reader.Deserialize(file); file.Close(); } catch (Exception e1) { if (e1.InnerException != null) { MessageBox.Show("File Load failed because:\r\n " + e1.Message + "\r\nAnd:\r\n" + e1.InnerException.Message); } else { MessageBox.Show("File Load failed because:\r\n " + e1.Message); } return(false); } for (int i = 0; i < theNeuronArray.arraySize; i++) { if (theNeuronArray.GetNeuron(i) == null) { theNeuronArray.SetNeuron(i, new Neuron(i)); } if (theNeuronArray.GetNeuron(i).CurrentCharge > 0 || theNeuronArray.GetNeuron(i).LastCharge > 0) { theNeuronArray.AddToFiringQueue(theNeuronArray.GetNeuron(i).Id); } } //Update all the synapses to ensure that the synapse-from lists are correct foreach (Neuron n in theNeuronArray.Neurons()) { if (n.SynapsesFrom != null) { n.SynapsesFrom.Clear(); } } foreach (Neuron n in theNeuronArray.Neurons()) { if (n.Synapses != null) { foreach (Synapse s in n.Synapses) { n.AddSynapse(s.TargetNeuron, s.Weight, theNeuronArray, false); s.N = theNeuronArray.GetNeuron(s.TargetNeuron); } } if (n.CurrentCharge >= 1 || n.LastCharge >= 1 || n.Model == Neuron.modelType.LIF) { theNeuronArray.AddToFiringQueue(n.Id); } } theNeuronArray.CheckSynapseArray(); theNeuronArrayView.Update(); setTitleBar(); Task.Delay(1000).ContinueWith(t => ShowDialogs()); foreach (ModuleView na in theNeuronArray.modules) { if (na.TheModule != null) { na.TheModule.SetUpAfterLoad(); } } if (theNeuronArray.displayParams != null) { theNeuronArrayView.Dp = theNeuronArray.displayParams; } NeuronArrayView.SortAreas(); Update(); SetShowSynapsesCheckBox(theNeuronArray.ShowSynapses); OpenHistoryWindow(); ResumeEngine(); return(true); }
//copy the selection to a clipboard public void CopyNeurons() { //get list of neurons to copy List <int> neuronsToCopy = theSelection.EnumSelectedNeurons(); theSelection.GetSelectedBoundingRectangle(out int X1o, out int Y1o, out int X2o, out int Y2o); myClipBoard = new NeuronArray(); myClipBoard.Initialize((X2o - X1o + 1) * (Y2o - Y1o + 1), (Y2o - Y1o + 1)); //by setting neurons to null, we can handle odd-shaped selections for (int i = 0; i < myClipBoard.arraySize; i++) { myClipBoard.SetNeuron(i, null); } //copy the neurons foreach (int nID in neuronsToCopy) { int destId = GetClipboardId(X1o, Y1o, nID); //copy the source neuron to the clipboard Neuron sourceNeuron = MainWindow.theNeuronArray.GetNeuron(nID); Neuron destNeuron = sourceNeuron.Clone(); destNeuron.Id = destId; myClipBoard.SetNeuron(destId, destNeuron); } //copy the synapses (this is two-pass so we make sure all neurons exist prior to copying foreach (int nID in neuronsToCopy) { Neuron sourceNeuron = MainWindow.theNeuronArray.GetNeuron(nID); int destId = GetClipboardId(X1o, Y1o, nID); Neuron destNeuron = myClipBoard.GetNeuron(destId); if (sourceNeuron.synapses != null) { foreach (Synapse s in sourceNeuron.synapses) { //only copy synapses with both ends in the selection if (neuronsToCopy.Contains(s.TargetNeuron)) { destNeuron.AddSynapse(GetClipboardId(X1o, Y1o, s.TargetNeuron), s.Weight); } } } } //copy modules foreach (ModuleView mv in MainWindow.theNeuronArray.modules) { if (theSelection.NeuronInSelection(mv.FirstNeuron) > 0 && theSelection.NeuronInSelection(mv.LastNeuron) > 0) { ModuleView newMV = new ModuleView() { FirstNeuron = GetClipboardId(X1o, Y1o, mv.FirstNeuron), TheModule = mv.TheModule, Color = mv.Color, Height = mv.Height, Width = mv.Width, Label = mv.Label, CommandLine = mv.CommandLine, }; myClipBoard.modules.Add(newMV); } } }
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); }
public static bool Save(NeuronArray theNeuronArray, string fileName) { //Check for file access if (!CanWriteTo(fileName, out string message)) { MessageBox.Show("Could not save file because: " + message); return(false); } MainWindow.thisWindow.SetProgress(0, "Saving Network File"); string tempFile = System.IO.Path.GetTempFileName(); FileStream file = File.Create(tempFile); Type[] extraTypes = GetModuleTypes(); try { XmlSerializer writer = new XmlSerializer(typeof(NeuronArray), extraTypes); writer.Serialize(file, theNeuronArray); } catch (Exception e) { MessageBox.Show("Xml file write failed because: " + e.Message); return(false); } file.Position = 0;; XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(file); XmlElement root = xmldoc.DocumentElement; XmlNode neuronsNode = xmldoc.CreateNode("element", "Neurons", ""); root.AppendChild(neuronsNode); for (int i = 0; i < theNeuronArray.arraySize; i++) { var progress = i / (float)theNeuronArray.arraySize; progress *= 100; if (MainWindow.thisWindow.SetProgress(progress, "")) { MainWindow.thisWindow.SetProgress(100, ""); return(false); } Neuron n = theNeuronArray.GetNeuronForDrawing(i); if (n.inUse || n.Label != "") { n = theNeuronArray.GetCompleteNeuron(i); string label = n.Label; //this is needed bacause inUse is true if any synapse points to this neuron--we don't need to bother with that if it's the only thing if (n.synapses.Count != 0 || label != "" || n.lastCharge != 0 || n.leakRate != 0.1f || n.model != Neuron.modelType.IF) { XmlNode neuronNode = xmldoc.CreateNode("element", "Neuron", ""); neuronsNode.AppendChild(neuronNode); XmlNode attrNode = xmldoc.CreateNode("element", "Id", ""); attrNode.InnerText = n.id.ToString(); neuronNode.AppendChild(attrNode); if (n.model != Neuron.modelType.IF) { attrNode = xmldoc.CreateNode("element", "Model", ""); attrNode.InnerText = n.model.ToString(); neuronNode.AppendChild(attrNode); } if (n.model != Neuron.modelType.Color && n.lastCharge != 0) { attrNode = xmldoc.CreateNode("element", "LastCharge", ""); attrNode.InnerText = n.lastCharge.ToString(); neuronNode.AppendChild(attrNode); } if (n.model == Neuron.modelType.Color && n.LastChargeInt != 0) { attrNode = xmldoc.CreateNode("element", "LastCharge", ""); attrNode.InnerText = n.LastChargeInt.ToString(); neuronNode.AppendChild(attrNode); } if (n.leakRate != 0.1f) { attrNode = xmldoc.CreateNode("element", "LeakRate", ""); attrNode.InnerText = n.leakRate.ToString(); neuronNode.AppendChild(attrNode); } if (n.axonDelay != 0) { attrNode = xmldoc.CreateNode("element", "AxonDelay", ""); attrNode.InnerText = n.axonDelay.ToString(); neuronNode.AppendChild(attrNode); } if (label != "") { attrNode = xmldoc.CreateNode("element", "Label", ""); attrNode.InnerText = label; neuronNode.AppendChild(attrNode); } if (MainWindow.arrayView.IsShowingSnapses(n.id)) { attrNode = xmldoc.CreateNode("element", "ShowSynapses", ""); attrNode.InnerText = "True"; neuronNode.AppendChild(attrNode); } if (n.synapses.Count > 0) { XmlNode synapsesNode = xmldoc.CreateNode("element", "Synapses", ""); neuronNode.AppendChild(synapsesNode); foreach (Synapse s in n.synapses) { XmlNode synapseNode = xmldoc.CreateNode("element", "Synapse", ""); synapsesNode.AppendChild(synapseNode); if (s.weight != 1) { attrNode = xmldoc.CreateNode("element", "Weight", ""); attrNode.InnerText = s.weight.ToString(); synapseNode.AppendChild(attrNode); } attrNode = xmldoc.CreateNode("element", "TargetNeuron", ""); attrNode.InnerText = s.targetNeuron.ToString(); synapseNode.AppendChild(attrNode); if (s.model != Synapse.modelType.Fixed) { attrNode = xmldoc.CreateNode("element", "Model", ""); attrNode.InnerText = s.model.ToString(); synapseNode.AppendChild(attrNode); } } } } } } //a way to get the xml as a string //using (var stringWriter = new StringWriter()) //using (var xmlTextWriter = XmlWriter.Create(stringWriter)) //{ // xmldoc.WriteTo(xmlTextWriter); // xmlTextWriter.Flush(); // string xxx = stringWriter.GetStringBuilder().ToString(); //} file.Position = 0; xmldoc.Save(file); file.Close(); try { File.Copy(tempFile, fileName, true); File.Delete(tempFile); } catch (Exception e) { MainWindow.thisWindow.SetProgress(100, ""); MessageBox.Show("Could not save file because: " + e.Message); return(false); } MainWindow.thisWindow.SetProgress(100, ""); return(true); }
//if you pass in the fileName 'ClipBoard', the save is to the windows clipboard public static bool Save(NeuronArray theNeuronArray, string fileName) { Stream file; string tempFile = ""; bool fromClipboard = fileName == "ClipBoard"; if (!fromClipboard) { //Check for file access if (!CanWriteTo(fileName, out string message)) { MessageBox.Show("Could not save file because: " + message); return(false); } MainWindow.thisWindow.SetProgress(0, "Saving Network File"); tempFile = System.IO.Path.GetTempFileName(); file = File.Create(tempFile); } else { file = new MemoryStream(); } Type[] extraTypes = GetModuleTypes(); try { XmlSerializer writer = new XmlSerializer(typeof(NeuronArray), extraTypes); writer.Serialize(file, theNeuronArray); } catch (Exception e) { if (e.InnerException != null) { MessageBox.Show("Xml file write failed because: " + e.InnerException.Message); } else { MessageBox.Show("Xml file write failed because: " + e.Message); } MainWindow.thisWindow.SetProgress(100, ""); return(false); } file.Position = 0;; XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(file); XmlElement root = xmldoc.DocumentElement; XmlNode neuronsNode = xmldoc.CreateNode("element", "Neurons", ""); root.AppendChild(neuronsNode); for (int i = 0; i < theNeuronArray.arraySize; i++) { if (!fromClipboard) { var progress = i / (float)theNeuronArray.arraySize; progress *= 100; if (MainWindow.thisWindow.SetProgress(progress, "")) { MainWindow.thisWindow.SetProgress(100, ""); return(false); } } Neuron n = theNeuronArray.GetNeuronForDrawing(i); if (fromClipboard) { n.Owner = theNeuronArray; } if (n.inUse || n.Label != "" || fromClipboard) { n = theNeuronArray.GetCompleteNeuron(i, fromClipboard); n.Owner = theNeuronArray; string label = n.Label; if (n.ToolTip != "") { label += Neuron.toolTipSeparator + n.ToolTip; } //this is needed bacause inUse is true if any synapse points to this neuron-- //we don't need to bother with that if it's the only thing if (n.synapses.Count != 0 || label != "" || n.lastCharge != 0 || n.leakRate != 0.1f || n.model != Neuron.modelType.IF) { XmlNode neuronNode = xmldoc.CreateNode("element", "Neuron", ""); neuronsNode.AppendChild(neuronNode); XmlNode attrNode = xmldoc.CreateNode("element", "Id", ""); attrNode.InnerText = n.id.ToString(); neuronNode.AppendChild(attrNode); if (n.model != Neuron.modelType.IF) { attrNode = xmldoc.CreateNode("element", "Model", ""); attrNode.InnerText = n.model.ToString(); neuronNode.AppendChild(attrNode); } if (n.model != Neuron.modelType.Color && n.lastCharge != 0) { attrNode = xmldoc.CreateNode("element", "LastCharge", ""); attrNode.InnerText = n.lastCharge.ToString(); neuronNode.AppendChild(attrNode); } if (n.model == Neuron.modelType.Color && n.LastChargeInt != 0) { attrNode = xmldoc.CreateNode("element", "LastCharge", ""); attrNode.InnerText = n.LastChargeInt.ToString(); neuronNode.AppendChild(attrNode); } if (n.leakRate != 0.1f) { attrNode = xmldoc.CreateNode("element", "LeakRate", ""); attrNode.InnerText = n.leakRate.ToString(); neuronNode.AppendChild(attrNode); } if (n.axonDelay != 0) { attrNode = xmldoc.CreateNode("element", "AxonDelay", ""); attrNode.InnerText = n.axonDelay.ToString(); neuronNode.AppendChild(attrNode); } if (label != "") { attrNode = xmldoc.CreateNode("element", "Label", ""); attrNode.InnerText = label; neuronNode.AppendChild(attrNode); } if (n.ShowSynapses) { attrNode = xmldoc.CreateNode("element", "ShowSynapses", ""); attrNode.InnerText = "True"; neuronNode.AppendChild(attrNode); } if (n.RecordHistory && !fromClipboard) { attrNode = xmldoc.CreateNode("element", "RecordHistory", ""); attrNode.InnerText = "True"; neuronNode.AppendChild(attrNode); } if (n.synapses.Count > 0) { XmlNode synapsesNode = xmldoc.CreateNode("element", "Synapses", ""); neuronNode.AppendChild(synapsesNode); foreach (Synapse s in n.synapses) { XmlNode synapseNode = xmldoc.CreateNode("element", "Synapse", ""); synapsesNode.AppendChild(synapseNode); if (s.weight != 1) { attrNode = xmldoc.CreateNode("element", "Weight", ""); attrNode.InnerText = s.weight.ToString(); synapseNode.AppendChild(attrNode); } attrNode = xmldoc.CreateNode("element", "TargetNeuron", ""); attrNode.InnerText = s.targetNeuron.ToString(); synapseNode.AppendChild(attrNode); if (s.model != Synapse.modelType.Fixed) { attrNode = xmldoc.CreateNode("element", "Model", ""); attrNode.InnerText = s.model.ToString(); synapseNode.AppendChild(attrNode); } } } } } } file.Position = 0; xmldoc.Save(file); if (!fromClipboard) { file.Close(); try { File.Copy(tempFile, fileName, true); File.Delete(tempFile); MainWindow.thisWindow.SetProgress(100, ""); } catch (Exception e) { MainWindow.thisWindow.SetProgress(100, ""); MessageBox.Show("Could not save file because: " + e.Message); return(false); } } else { file.Position = 0; StreamReader str = new StreamReader(file); string temp = str.ReadToEnd(); Clipboard.SetText(temp); } return(true); }
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); }