Some simple tools for implementing a command line version of the TemporalNetworks tools
Exemple #1
0
        /// <summary>
        /// Parallely computes the betweenness preference distribution of a given temporal network
        /// </summary>
        /// <param name="args"></param>
        public static void Run(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("Usage: TempNet T2 [temporal_network_file] [output_file] [aggregationWndow=1] [undirected=false]");
                return;
            }
            string out_file = args[2];

            if (!CmdlTools.PromptExistingFile(out_file))
            {
                Console.WriteLine("User opted to exit.");
                return;
            }

            bool undirected        = false;
            int  aggregationWindow = 1;

            if (args.Length >= 4)
            {
                aggregationWindow = int.Parse(args[3]);
            }
            if (args.Length >= 5)
            {
                undirected = Boolean.Parse(args[4]);
            }


            Console.Write("Reading temporal network as {0} network...", undirected ? "undirected" : "directed");
            TemporalNetwork temp_net = TemporalNetwork.ReadFromFile(args[1], undirected: undirected);

            Console.WriteLine(" done.");

            Console.WriteLine("Applying aggregation window, length = {0}, time steps before = {1}", aggregationWindow, temp_net.Length);
            temp_net.AggregateTime(aggregationWindow);
            Console.WriteLine("done, time steps after = {0}", temp_net.Length);

            Console.Write("Building aggregate networks ...");
            temp_net.ReduceToTwoPaths();
            Console.WriteLine(" done.");

            Console.Write("Writing transition matrix ...");
            WriteTwopathTransitionMatrix(temp_net, out_file);
            Console.WriteLine("done.");
        }
        /// <summary>
        /// Runs an SI spreading process on a temporal network and outputs the dynamics of the infection size
        /// </summary>
        /// <param name="args"></param>
        public static void Run(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("Usage: TempNet spreading [temporal_network_file] [output_file]");
                return;
            }
            string out_file = args[2];

            if (!CmdlTools.PromptExistingFile(out_file))
            {
                Console.WriteLine("User opted to exit.");
                return;
            }

            Console.Write("Reading temporal network as directed...");
            TemporalNetwork temp_net = TemporalNetwork.ReadFromFile(args[1]);

            // Prevent the network from being reduced to two paths
            temp_net.StripEdgesToTwoPaths = false;
            Console.WriteLine("done.");

            Console.Write("Running SI spreading on temporal network with {0} time steps ...", temp_net.Length);
            string times  = null;
            string output = SISpreading.RunSpreading(temp_net, out times, 1d);

            Console.WriteLine(" done.");

            if (output != null)
            {
                Console.Write("Finished path discovery. Writing path length matrix");
                System.IO.File.WriteAllText(out_file, output);
                System.IO.File.WriteAllText(out_file + ".times", times);
                Console.WriteLine("done.");
            }
        }
Exemple #3
0
        public static void Run(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("Usage: TempNet tikz [temporal_network_file] [output_file]");
                return;
            }
            string out_file = args[2];

            if (!CmdlTools.PromptExistingFile(out_file))
            {
                Console.WriteLine("User opted to exit.");
                return;
            }

            Console.Write("Reading temporal network ...");
            TemporalNetwork temp_net = TemporalNetwork.ReadFromFile(args[1]);

            Console.WriteLine("done.");

            Console.Write("Exporting to tikz...");
            TikzExporter.CreateTikzUnfolding(out_file, null, temp_net);
            Console.WriteLine(" done.");
        }
Exemple #4
0
        public static void Run(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("Usage: TempNet visualize [temporal_network_file] [output_png] [square=TRUE] [border=5]");
                return;
            }
            string out_file = args[2];

            bool square = true;
            int  border = 5;

            if (args.Length >= 4)
            {
                square = bool.Parse(args[3]);
            }

            if (args.Length >= 5)
            {
                border = int.Parse(args[4]);
            }

            if (!CmdlTools.PromptExistingFile(out_file))
            {
                Console.WriteLine("User opted to exit.");
                return;
            }

            Console.Write("Reading temporal network ...");
            TemporalNetwork temp_net = TemporalNetwork.ReadFromFile(args[1]);

            Console.WriteLine("done.");

            CreateBitmap(temp_net, out_file, square, border);
            Console.WriteLine(" done.");
        }
        /// <summary>
        /// Runs a random walk process on an aggregate networks and outputs the dynamics of the infection size
        /// </summary>
        /// <param name="args"></param>
        public static void Run(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("Usage: TempNet rw [network_file] [output_file] [RWMode=static_first] [aggregationWindow=1] [steps=1000]");
                Console.WriteLine("\t where RWMode can be ... ");
                Console.WriteLine("\t static_first\t Performs a random walk on the aggregate network that considers betweenness preferences computed from the temporal network");
                Console.WriteLine("\t static_second\t ...");
                return;
            }
            string         out_file = args[2];
            RandomWalkMode mode     = RandomWalkMode.StaticFirstOrder;
            int            runs     = 1;
            int            length   = 1000;

            int aggregationWindow = 1;

            if (args.Length >= 4)
            {
                if (args[3] == "static_second")
                {
                    mode = RandomWalkMode.StaticSecondOrder;
                }
                else
                {
                    Console.WriteLine("Unknown walk mode '{0}'", args[3]);
                }
            }
            if (args.Length >= 5)
            {
                aggregationWindow = int.Parse(args[4]);
            }
            if (args.Length >= 6)
            {
                runs = int.Parse(args[5]);
            }
            if (args.Length >= 7)
            {
                length = int.Parse(args[6]);
            }

            if (!CmdlTools.PromptExistingFile(out_file))
            {
                Console.WriteLine("User opted to exit.");
                return;
            }

            Console.Write("Reading temporal network as undirected network...");
            TemporalNetwork temp_net = TemporalNetwork.ReadFromFile(args[1], true);

            Console.WriteLine("done.");

            if (aggregationWindow != 1)
            {
                Console.WriteLine("Applying aggregation window, length = {0}, time steps before = {1}", aggregationWindow, temp_net.Length);
                temp_net.AggregateTime(aggregationWindow);
                Console.WriteLine("done, time steps after = {0}", temp_net.Length);
            }

            Console.Write("Building aggregate network...");
            WeightedNetwork aggregateNet = temp_net.AggregateNetwork;

            Console.WriteLine("done.");

            Console.WriteLine("Starting random walk ...");

            Dictionary <int, double> tvd = new Dictionary <int, double>();
            RandomWalk walk = new RandomWalk(temp_net, mode);

            for (int t = 0; t < length; t++)
            {
                walk.Step();
                tvd[t] = walk.TVD;
                Console.WriteLine("Node = {0}, step = {1}, tvd={2:0.000}", walk.CurrentNode, t, tvd[t]);
            }

            Console.Write("Writing time series of total variation distance ...");
            StringBuilder sb = new StringBuilder();

            foreach (var step in tvd)
            {
                sb.AppendLine(string.Format(System.Globalization.CultureInfo.GetCultureInfo("en-US").NumberFormat, "{0} {1}", step.Key, step.Value));
            }
            System.IO.File.WriteAllText(out_file + ".dat", sb.ToString());
            Console.WriteLine(" done.");
        }
        /// <summary>
        /// Creates the simple example network with betweenness preference that can be seen in the paper in Figure 2 (right)
        /// </summary>
        /// <param name="args"></param>
        public static void Run(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Usage: TempNet example [output_file]");
                return;
            }

            string out_file = args[1];

            if (!CmdlTools.PromptExistingFile(out_file))
            {
                Console.WriteLine("User opted to exit.");
                return;
            }

            TemporalNetwork temporal_net = new TemporalNetwork();

            int k = 0;

            for (int i = 0; i < 1; i++)
            {
                // Add edges according to two paths
                temporal_net.AddTemporalEdge(k++, "c", "e");
                temporal_net.AddTemporalEdge(k++, "e", "f");

                temporal_net.AddTemporalEdge(k++, "a", "e");
                temporal_net.AddTemporalEdge(k++, "e", "g");

                temporal_net.AddTemporalEdge(k++, "c", "e");
                temporal_net.AddTemporalEdge(k++, "e", "f");

                temporal_net.AddTemporalEdge(k++, "a", "e");
                temporal_net.AddTemporalEdge(k++, "e", "g");

                temporal_net.AddTemporalEdge(k++, "c", "e");
                temporal_net.AddTemporalEdge(k++, "e", "f");

                // Note that the next added edge additionally continues a two-path e -> f -> e
                temporal_net.AddTemporalEdge(k++, "f", "e");
                temporal_net.AddTemporalEdge(k++, "e", "b");

                // An additional edge that will be filtered during preprocessing
                temporal_net.AddTemporalEdge(k++, "e", "b");

                temporal_net.AddTemporalEdge(k++, "c", "e");
                temporal_net.AddTemporalEdge(k++, "e", "f");

                temporal_net.AddTemporalEdge(k++, "b", "e");
                temporal_net.AddTemporalEdge(k++, "e", "g");

                temporal_net.AddTemporalEdge(k++, "c", "e");
                temporal_net.AddTemporalEdge(k++, "e", "f");

                temporal_net.AddTemporalEdge(k++, "c", "e");
                temporal_net.AddTemporalEdge(k++, "e", "f");
            }

            Console.Write("Saving to file ...");
            TemporalNetwork.SaveToFile(out_file, temporal_net);
            Console.WriteLine(" done.");
        }
        /// <summary>
        /// Parallely computes the betweenness preference distribution of a given temporal network
        /// </summary>
        /// <param name="args"></param>
        public static void Run(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("Usage: TempNet distribution [temporal_network_file] [output_file] [aggregationWndow=1] [undirected=false]");
                return;
            }
            string out_file = args[2];

            if (!CmdlTools.PromptExistingFile(out_file))
            {
                Console.WriteLine("User opted to exit.");
                return;
            }

            bool undirected = false;
            int aggregationWindow = 1;            
            if (args.Length >= 4)
                aggregationWindow = int.Parse(args[3]);
            if (args.Length >= 5)
                undirected = Boolean.Parse(args[4]);


            Console.Write("Reading temporal network as {0} network...", undirected ? "undirected" : "directed");
            TemporalNetwork temp_net = TemporalNetwork.ReadFromFile(args[1], undirected:undirected);
            Console.WriteLine(" done.");

            Console.WriteLine("Applying aggregation window, length = {0}, time steps before = {1}", aggregationWindow, temp_net.Length);
            temp_net.AggregateTime(aggregationWindow);
            Console.WriteLine("done, time steps after = {0}", temp_net.Length);

            Console.Write("Preparing temporal network ...");
            temp_net.ReduceToTwoPaths();
            Console.WriteLine(" done.");

            double nodeNum = temp_net.AggregateNetwork.VertexCount;
            double current = 0d;
            double last_perc = 0d;

            Console.WriteLine("Computing betweenness preference for {0} nodes ...", nodeNum);

            // Parallely compute betweenness preference for all nodes
#if DEBUG 
            foreach(string v in temp_net.AggregateNetwork.Vertices)
#else
            Parallel.ForEach<string>(temp_net.AggregateNetwork.Vertices, v =>
#endif
            {
                double betweennessPref = BetweennessPref.GetBetweennessPref(temp_net, v);

                // Synchronized access to file and to counters ... 
                if(temp_net.AggregateNetwork.GetIndeg(v)>0 && temp_net.AggregateNetwork.GetOutdeg(v)>0)
                    lock (out_file)
                    {
                        System.IO.File.AppendAllText(out_file, v + " " + string.Format(System.Globalization.CultureInfo.GetCultureInfo("en-US").NumberFormat, "{0:0.000000}\n", betweennessPref));
                        current++;
                        if (100 * current / nodeNum >= last_perc + 5d)
                        {
                            last_perc = 100 * current / nodeNum;
                            Console.WriteLine("Completed for {0} nodes [{1:0.0} %]", current, last_perc);
                        }
                    }
            }
#if !DEBUG
                );
#endif
            Console.WriteLine("done.");
        }
Exemple #8
0
        public static void Run(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("Usage: TempNet G2_null [weighted_edge_list.agg.1.edges] [output_file]");
                return;
            }
            string out_file   = args[2];
            string input_file = args[1];

            if (!CmdlTools.PromptExistingFile(out_file))
            {
                return;
            }

            Console.Write("Reading edge list...");
            string[] agg1 = System.IO.File.ReadAllLines(input_file);
            Console.WriteLine("done.");

            // Prepare output file
            StringBuilder output = new StringBuilder();

            output.AppendLine("source target weight");

            Console.Write("Computing cumulative edge weights...");
            // Compute the cumulative weight of all outgoing edges for all nodes
            Dictionary <string, float> sum_out_w = new Dictionary <string, float>();

            for (int i = 1; i < agg1.Length; i++)
            {
                string[] fields = agg1[i].Split(' ');
                if (!sum_out_w.ContainsKey(fields[0]))
                {
                    sum_out_w[fields[0]] = float.Parse(fields[2], nf);
                }
                else
                {
                    sum_out_w[fields[0]] += float.Parse(fields[2], nf);
                }
            }
            Console.WriteLine("done.");

            Console.Write("Computing null model edge weights...");
            // Compute the null model second-order edge weights for all second-order edges (i.e. two-paths)
            for (int i = 1; i < agg1.Length; i++)
            {
                for (int j = 1; j < agg1.Length; j++)
                {
                    string[] fields1 = agg1[i].Split(' ');
                    string[] fields2 = agg1[j].Split(' ');

                    // This is a two-path candidate
                    string a = fields1[0];
                    string b = fields1[1];
                    string c = fields2[0];
                    string d = fields2[1];

                    // It is a possible two-path if b==c
                    if (b == c)
                    {
                        float w_ab = float.Parse(fields1[2], nf);
                        float w_cd = float.Parse(fields2[2], nf);
                        float null_model_weight = 0.5f * w_ab * w_cd / (sum_out_w[c]);
                        output.AppendLine(string.Format(nf, "({0};{1}) ({1};{2}) {3:0.0000}", a, b, d, null_model_weight));
                    }
                }
            }
            Console.WriteLine("done.");

            // Write to file
            Console.Write("Writing output file...");
            System.IO.File.WriteAllText(out_file, output.ToString());
            Console.WriteLine("done.");
        }
        /// <summary>
        /// Runs a random walk process on an aggregate networks and outputs the dynamics of the infection size
        /// </summary>
        /// <param name="args"></param>
        public static void Run(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("Usage: TempNet rw [network_file] [output_file] [WalkType=random] [aggregationWindow=1] [runs=1] [length=100000]");
                Console.WriteLine("\t where WalkType can be ... ");
                Console.WriteLine("\t bwp_pres\t Performs a random walk on the aggregate network that considers betweenness preferences computed from the temporal network");
                Console.WriteLine("\t bwp_null\t ...");
                return;
            }
            string out_file = args[2];
            string type     = "bwp_pres";
            int    runs     = 1;
            int    length   = 100000;

            int aggregationWindow = 1;

            if (args.Length >= 4)
            {
                type = args[3];
            }
            if (args.Length >= 5)
            {
                aggregationWindow = int.Parse(args[4]);
            }
            if (args.Length >= 6)
            {
                runs = int.Parse(args[5]);
            }
            if (args.Length >= 7)
            {
                length = int.Parse(args[6]);
            }

            if (!CmdlTools.PromptExistingFile(out_file))
            {
                Console.WriteLine("User opted to exit.");
                return;
            }

            Console.Write("Reading temporal network as undirected network...");
            TemporalNetwork temp_net = TemporalNetwork.ReadFromFile(args[1], true);

            Console.WriteLine("done.");

            if (aggregationWindow != 1)
            {
                Console.WriteLine("Applying aggregation window, length = {0}, time steps before = {1}", aggregationWindow, temp_net.Length);
                temp_net.AggregateTime(aggregationWindow);
                Console.WriteLine("done, time steps after = {0}", temp_net.Length);
            }

            Console.Write("Building aggregate network...");
            WeightedNetwork aggregateNet = temp_net.AggregateNetwork;

            Console.WriteLine("done.");

            for (int r = 2000; r <= 2000 + runs; r++)
            {
                Console.WriteLine("Running Random walk [{0}] in mode [{1}] ...", r, type);
                IDictionary <int, double> tvd = null;
                if (type == "bwp_pres")
                {
                    tvd = RandomWalk.RunRW_BWP(temp_net, length, null_model: false);
                }
                else if (type == "bwp_null")
                {
                    tvd = RandomWalk.RunRW_BWP(temp_net, length, null_model: true);
                }
                else
                {
                    Console.WriteLine("\nError: RWType {0} unknown", type);
                    return;
                }

                Console.Write("Writing time series of total variation distance ...");
                StringBuilder sb = new StringBuilder();
                foreach (var step in tvd)
                {
                    sb.AppendLine(string.Format(System.Globalization.CultureInfo.GetCultureInfo("en-US").NumberFormat, "{0} {1}", step.Key, step.Value));
                }
                System.IO.File.WriteAllText(out_file + "_r_" + r + ".dat", sb.ToString());
                Console.WriteLine(" done.");
            }
        }
Exemple #10
0
        /// <summary>
        /// Outputs a simple example temporal network
        /// </summary>
        /// <param name="args"></param>
        public static void Run(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("Usage: TempNet aggregate [temporal_network_file] [output_file] [aggregationWindow=1] [weighted_aggregate_networks=false]");
                return;
            }

            string out_file          = args[2];
            bool   two_path          = false;
            int    aggregationWindow = 1;

            if (args.Length >= 4)
            {
                aggregationWindow = int.Parse(args[3]);
            }

            if (args.Length >= 5)
            {
                two_path = bool.Parse(args[4]);
            }

            if (!CmdlTools.PromptExistingFile(out_file))
            {
                Console.WriteLine("User opted to exit.");
                return;
            }

            Console.Write("Reading temporal network as undirected...");
            TemporalNetwork temp_net = TemporalNetwork.ReadFromFile(args[1], undirected: true);

            Console.WriteLine(" done.");

            Console.WriteLine(temp_net.Length);
            Console.Write("Applying agggregation window [{0}] ...", aggregationWindow);
            temp_net.AggregateTime(aggregationWindow);
            Console.WriteLine("done.");
            Console.WriteLine(temp_net.Length);

            Console.Write("Reducing to two path networks ...");
            temp_net.ReduceToTwoPaths();
            Console.WriteLine("done.");

            if (!two_path)
            {
                Console.Write("Saving temporal network ...");
                TemporalNetwork.SaveToFile(out_file, temp_net);
                Console.WriteLine(" done.");
            }
            else
            {
                //WeightedNetwork net = new WeightedNetwork();
                //foreach(string tp in temp_net.TwoPathWeights.Keys)
                //{
                //    string[] comps = tp.Split(',');
                //    net.AddEdge(comps[0], comps[2], EdgeType.Directed, temp_net.TwoPathWeights[tp]);
                //}
                Console.Write("Saving weighted first-order aggregate network ...");
                WeightedNetwork.SaveToFile(out_file + ".1.edges", temp_net.AggregateNetwork);
                Console.WriteLine(" done.");

                Console.Write("Saving weighted second-order aggregate network ...");
                WeightedNetwork.SaveToFile(out_file + ".2.edges", temp_net.SecondOrderAggregateNetwork);
                Console.WriteLine(" done.");
            }
        }
        /// <summary>
        /// Creates randomized temporal networks based on a shuffling of edges or two-paths
        /// </summary>
        /// <param name="args"></param>
        public static void Run(string[] args)
        {
            if (args.Length < 4)
            {
                Console.WriteLine("Usage: TempNet shuffle [what] [original_net_file] [output_file] [undirected=false] [length=0]");
                Console.WriteLine("... where [what] is ...");
                Console.WriteLine("\t 'twopaths' \t Shuffle the two-paths in the original network. Preserves the aggregate network and the betweenness preference distribution.");
                Console.WriteLine("\t 'edges' \t Shuffles the edges in the original network. Preserves the aggregate network only.");
                Console.WriteLine("");
                return;
            }
            string shuffling = args[1];
            string out_file  = args[3];

            if (!CmdlTools.PromptExistingFile(out_file))
            {
                Console.WriteLine("User opted to exit.");
                return;
            }

            bool undirected = false;

            if (args.Length >= 5)
            {
                undirected = bool.Parse(args[4]);
            }

            int length = 0;

            if (args.Length == 6)
            {
                length = int.Parse(args[5]);
            }

            TemporalNetwork output = null;

            Console.Write("Reading temporal network as {0} network...", undirected ? "undirected" : "directed");
            TemporalNetwork temp_net = TemporalNetwork.ReadFromFile(args[2], undirected: undirected);

            Console.WriteLine(" done.");

            Console.Write("Extracting two paths and building aggregate network...");

            temp_net.ReduceToTwoPaths();

            Console.WriteLine(" done.");

            if (shuffling == "twopaths")
            {
                Console.Write("Shuffling two paths ...");

                output = TemporalNetworkEnsemble.ShuffleTwoPaths(temp_net, length);

                Console.WriteLine(" done.");
            }
            else if (shuffling == "edges")
            {
                Console.Write("Shuffling edges ...");

                output = TemporalNetworkEnsemble.ShuffleEdges(temp_net, length);

                Console.WriteLine(" done.");
            }
            else
            {
                Console.WriteLine("{0} is not a valid parameter!", shuffling);
            }

            if (output != null)
            {
                Console.Write("Saving temporal network to {0}...", out_file);
                TemporalNetwork.SaveToFile(out_file, output);
                Console.WriteLine(" done.");
            }
        }