コード例 #1
0
ファイル: GWNeuralNetwork.cs プロジェクト: dtklinh/CRFTool
        public GWNeuralNetwork(List <NeuralNetworkInputNode <InputData> > inputNodes, int[] innerLayers)
        {
            Graph            = new GWGraph <NeuralNetworkNodeData, NeuralNetworkEdgeData, GWNeuralNetwork <InputData> >();
            Graph.GraphStyle = GraphStyle.Directed;

            InputNodes = inputNodes.ToList();

            // init layers
            Layers = new List <GWNode <NeuralNetworkNodeData, NeuralNetworkEdgeData, GWNeuralNetwork <InputData> > > [innerLayers.Length + 2];
            for (int layer = 0; layer < Layers.Length; layer++)
            {
                Layers[layer] = new List <GWNode <NeuralNetworkNodeData, NeuralNetworkEdgeData, GWNeuralNetwork <InputData> > >();
            }

            //inputLayer:
            for (int i = 0; i < InputNodes.Count; i++)
            {
                var node = Graph.CreateNode();
                node.Data = InputNodes[i];
                Layers[0].Add(node);
            }

            //create inner layers
            var random      = new Random();
            int defConnects = 4;

            for (int layer = 1; layer <= innerLayers.Length; layer++)
            {
                Layers[layer] = new List <GWNode <NeuralNetworkNodeData, NeuralNetworkEdgeData, GWNeuralNetwork <InputData> > >();
                for (int i = 0; i < innerLayers[layer - 1]; i++)
                {
                    var node = Graph.CreateNode();
                    node.Data = new NeuralNetworkNodeData();

                    //connect with former layer
                    for (int k = 0; k < defConnects; k++)
                    {
                        var otherNode = Layers[layer - 1].RandomElement(random);
                        var edge      = Graph.CreateEdge(otherNode, node);
                        edge.Data        = new NeuralNetworkEdgeData();
                        edge.Data.Weight = random.NextDouble();
                    }

                    Layers[layer].Add(node);
                }
            }

            //create endingPoint
            var endnode = Graph.CreateNode();

            endnode.Data = new NeuralNetworkNodeData();
            for (int i = 0; i < Layers[Layers.Length - 2].Count; i++)
            {
                var edge = Graph.CreateEdge(Layers[Layers.Length - 2][i], endnode);
                edge.Data        = new NeuralNetworkEdgeData();
                edge.Data.Weight = random.NextDouble();
            }
            Layers[Layers.Length - 1].Add(endnode);
        }
コード例 #2
0
ファイル: CRFInput.cs プロジェクト: MarlonWelter/CRFTool
        public static GWGraph <SGLNodeData, SGLEdgeData, SGLGraphData> ParseFile_CRF_Class(string file)
        {
            var graph = new GWGraph <SGLNodeData, SGLEdgeData, SGLGraphData>();

            graph.Data = new SGLGraphData();

            using (var reader = new StreamReader(file))
            {
                //if (reader.ReadLine() != "reflabels:")
                //    throw new IOException("unbekanntes FileFormat - File sollte mit 'numlabels:' beginnen");
                //int refLabels = int.Parse(reader.ReadLine());
                //if (reader.ReadLine() != "observations:")
                //    throw new IOException("unbekanntes FileFormat - File sollte mit 'numlabels:' beginnen");
                //int observations = int.Parse(reader.ReadLine());

                if (reader.ReadLine() != "nodes:")
                {
                    throw new IOException("unbekanntes FileFormat - File sollte 'nodes:' in Zeile 3 enthalten");
                }

                string line = string.Empty;
                while ((line = reader.ReadLine()) != "edges:" && line.NotNullOrEmpty())
                {
                    string[] words  = Regex.Split(line, "\\s");
                    var      node   = graph.CreateNode();
                    var      scores = new List <double>();
                    node.Data = new SGLNodeData();
                    node.Data.ReferenceLabel = int.Parse(words[1]);
                    node.Data.Observation    = int.Parse(words[2]);

                    if (words.Length > 3)
                    {
                        node.Data.AssignedLabel = int.Parse(words[3]);
                    }

                    node.Data.Id = words[0];
                }
                while ((line = reader.ReadLine()).NotNullOrEmpty())
                {
                    string[] words = Regex.Split(line, "\\s");
                    string   name  = words[0] + "_" + words[1];
                    string   check = words[1] + "_" + words[0];

                    var edge = graph.CreateEdge(graph.Nodes.First((n) => n.Data.Id.Equals(words[0])), graph.Nodes.First((n) => n.Data.Id.Equals(words[1])));

                    edge.Data = new SGLEdgeData();
                }
            }
            return(graph);
        }
コード例 #3
0
ファイル: CRFInput.cs プロジェクト: MarlonWelter/CRFTool
        //Davids files
        public static GWGraph <ICRFNodeData, ICRFEdgeData, ICRFGraphData> ParseFileFormatThree(string file)
        {
            var graph = new GWGraph <ICRFNodeData, ICRFEdgeData, ICRFGraphData>();

            var edgeMode = false;
            int CountAdd = 0;

            using (var reader = new StreamReader(file))
            {
                string line = string.Empty;
                while ((line = reader.ReadLine()) != null)
                {
                    if (line.Equals("NODES"))
                    {
                        reader.ReadLine();
                        edgeMode = false;
                    }
                    else if (line.Equals("EDGES"))
                    {
                        reader.ReadLine();
                        edgeMode = true;
                    }
                    else if (!edgeMode)
                    {
                        string[] words  = Regex.Split(line, ";");
                        var      node   = graph.CreateNode();
                        var      scoreI = int.Parse(words[1]);
                        var      scoreN = int.Parse(words[2]);
                        var      scores = new List <double>();
                        scores.AddRange(scoreN + CountAdd, scoreI + CountAdd);
                        graph.Nodes.Add(node);
                        node.Data.Scores         = scores.ToArray();
                        node.Data.Id             = words[0];
                        node.Data.ReferenceLabel = (words[3].Equals("N")) ? 0 : 1;
                    }
                    else
                    {
                        string[] words  = Regex.Split(line, ";");
                        var      headId = words[0];
                        var      footId = words[1];
                        var      head   = graph.Nodes.First(n => n.Data.Id.Equals(headId));
                        var      foot   = graph.Nodes.First(n => n.Data.Id.Equals(footId));

                        var edge = new GWEdge <ICRFNodeData, ICRFEdgeData, ICRFGraphData>(head, foot);
                        graph.Edges.Add(edge);
                    }
                }
            }
            return(graph);
        }
コード例 #4
0
ファイル: IProteinGraph.cs プロジェクト: dtklinh/CRFTool
        public static GWGraph <ICRFNodeData, ICRFEdgeData, ICRFGraphData> CreateGraph(this ProteinGraph graph, IDictionary <ResidueNodeData, double[]> nodescores, IDictionary <SimpleEdgeData, double[, ]> edgescores)
        {
            var clonegraph = new GWGraph <ICRFNodeData, ICRFEdgeData, ICRFGraphData>();

            foreach (var node in graph.Nodes)
            {
                var clone = clonegraph.CreateNode();
                clone.Data = new CRFNodeData()
                {
                    Id = node.Data.Residue.Id, Scores = nodescores[node.Data]
                };
                clonegraph.Nodes.Add(clone);
            }
            foreach (var edge in graph.Edges)
            {
                var newEdge = new GWEdge <ICRFNodeData, ICRFEdgeData, ICRFGraphData>(clonegraph.Nodes.First(n => n.Data.Id.Equals(edge.Head.Data.Residue.Id)),
                                                                                     clonegraph.Nodes.First(n => n.Data.Id.Equals(edge.Foot.Data.Residue.Id)));
                newEdge.Data        = new CRFEdgeData();
                newEdge.Data.Scores = edgescores[edge.Data];
                clonegraph.Edges.Add(newEdge);
            }

            return(clonegraph);
        }
コード例 #5
0
ファイル: CRFInput.cs プロジェクト: MarlonWelter/CRFTool
        public static IGWGraph <ICRFNodeData, ICRFEdgeData, ICRFGraphData> ParseFileFormatTwo(string file)
        {
            var graph = new GWGraph <ICRFNodeData, ICRFEdgeData, ICRFGraphData>();

            using (var reader = new StreamReader(file))
            {
                string line = string.Empty;
                while ((line = reader.ReadLine()) != null)
                {
                    if (line.StartsWith("#"))
                    {
                        continue;
                    }
                    else if (line.StartsWith("NODE"))
                    {
                        string[] words  = Regex.Split(line, "\\s").Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
                        var      node   = graph.CreateNode();
                        var      scores = new List <double>();
                        for (int counter = 0; counter < 2; counter++)
                        {
                            try
                            {
                                scores.Add(double.Parse(words[counter + 5], CultureInfo.InvariantCulture));
                            }
                            catch (FormatException ex)
                            {
                                if (words[counter + 5].Equals("N_INF"))
                                {
                                    scores.Add(double.NegativeInfinity);
                                }
                                else
                                {
                                    throw ex;
                                }
                            }
                        }
                        graph.Nodes.Add(node);
                        node.Data                = new CRFNodeData();
                        node.Data.Scores         = scores.ToArray();
                        node.Data.Id             = words[1];
                        node.Data.ReferenceLabel = int.Parse(words[7]);
                    }
                    else if (line.StartsWith("NEIGH"))
                    {
                        string[] words = Regex.Split(line, "\\s").Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();

                        var stringNode = words[1].Substring(0, words[1].Length - 1);

                        for (int nb = 2; nb < words.Length; nb++)
                        {
                            //Don't use the double occurence edges
                            if (stringNode.CompareTo(words[nb]) > 0)
                            {
                                continue;
                            }

                            var edge = new GWEdge <ICRFNodeData, ICRFEdgeData, ICRFGraphData>(graph.Nodes.First((n) => n.Data.Id.Equals(stringNode)), graph.Nodes.First((n) => n.Data.Id.Equals(words[nb])));
                            edge.Data = new CRFEdgeData();
                            graph.Edges.Add(edge);
                        }
                    }
                }
            }
            return(graph);
        }
コード例 #6
0
ファイル: CRFInput.cs プロジェクト: MarlonWelter/CRFTool
        public static IGWGraph <ICRFNodeData, ICRFEdgeData, ICRFGraphData> ParseFile(string file)
        {
            var graph = new GWGraph <ICRFNodeData, ICRFEdgeData, ICRFGraphData>();

            using (var reader = new StreamReader(file))
            {
                if (reader.ReadLine() != "numlabels:")
                {
                    throw new IOException("unbekanntes FileFormat - File sollte mit 'numlabels:' beginnen");
                }

                int numberLabels = int.Parse(reader.ReadLine());
                if (reader.ReadLine() != "nodes:")
                {
                    throw new IOException("unbekanntes FileFormat - File sollte 'nodes:' in Zeile 3 enthalten");
                }

                string line = string.Empty;
                while ((line = reader.ReadLine()) != "edges:")
                {
                    string[] words  = Regex.Split(line, "\\s");
                    var      node   = graph.CreateNode();
                    var      scores = new List <double>();
                    for (int counter = 1; counter < numberLabels + 1; counter++)
                    {
                        try
                        {
                            scores.Add(double.Parse(words[counter], CultureInfo.InvariantCulture));
                        }
                        catch (FormatException ex)
                        {
                            if (words[counter].Equals("N_INF"))
                            {
                                scores.Add(double.NegativeInfinity);
                            }
                            else
                            {
                                throw ex;
                            }
                        }
                    }
                    graph.Nodes.Add(node);
                    node.Data.Scores = scores.ToArray();
                    node.Data.Id     = words[0];
                }
                while ((line = reader.ReadLine()) != null)
                {
                    string[] words = Regex.Split(line, "\\s");
                    string   name  = words[0] + "_" + words[1];
                    string   check = words[1] + "_" + words[0];

                    //Don't use the double occurence edges
                    if (words[0].CompareTo(words[1]) > 0)
                    {
                        continue;
                    }

                    var edge = new GWEdge <ICRFNodeData, ICRFEdgeData, ICRFGraphData>(graph.Nodes.First((n) => n.Data.Id.Equals(words[0])), graph.Nodes.First((n) => n.Data.Id.Equals(words[1])));

                    graph.Edges.Add(edge);

                    edge.Data.Scores = new double[numberLabels, numberLabels];
                    int word = 2;
                    for (int dounter = 0; dounter < numberLabels; dounter++)
                    {
                        for (int counter = 0; counter < numberLabels; counter++)
                        {
                            edge.Data.Scores[dounter, counter] = (double.Parse(words[word], CultureInfo.InvariantCulture));
                            word++;
                        }
                    }
                }
            }
            return(graph);
        }