void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) //work here { PathBasedImbalance PIF = new PathBasedImbalance(); for (int p = 0; p < progressBar1.Maximum; p++) { //for (int j = 0; j < 1000000000; j++) { } backgroundWorker1.ReportProgress(p + 1); curYearLabel = Years[p].ToString(); var output_writer_data = new StreamWriter(file, true); double[,] matrix = PIF.supportScript(inputFile, order, isNull, Years[p]); //Angela int totalRows = matrix.GetLength(0); int totalCols = matrix.GetLength(1); for (int i = 0; i < totalRows; i++) { string[] dataRow = new string[totalCols]; for (int j = 0; j < totalCols; j++) { dataRow[j] = matrix[i, j].ToString();//>= 0? mTable[i, j].ToString() : "#N/A"; } output_writer_data.WriteLine(String.Join(",", dataRow)); output_writer_data.Flush(); } output_writer_data.Close(); } }
public void iterate_rand(int n, int order, int size, int pos_target, int neg_target, int pos_max, int neg_max) { string NetID; Console.WriteLine("size: {2} pos: {0} neg: {1}", pos_target, neg_target, size); Random rnd = new Random(Guid.NewGuid().GetHashCode()); //ITERATING n (100) TIMES for (int i = 0; i < n; i++) { int[,] x_plus = rand_matrix_roof(size, pos_target, pos_max); int[,] x_minus = rand_matrix_roof(size, neg_target, neg_max); NetID = rnd.Next(10000).ToString(); PathBasedImbalance g = new PathBasedImbalance(NetID, x_plus, x_minus); g.infoTable(this, order); } }
public void supportScript(string inputFile, int order, bool Null) { //var logger = new StreamWriter("Log.txt"); //string file_name = "alliance trade mids and rivalries multiplex data.csv"; //string output_file = "results_big.csv"; string file_name = inputFile.Replace(@"\\", @"\"); string output_file = "temp_output.csv"; int val; // to check if int or string var main_reader = new StreamReader(file_name); //output_writer must append --> option var output_writer = new StreamWriter(output_file, true); //Printing Title if (order == 3) { if (Null) { output_writer.Write("networkid,i,j,positive_1,negative_1,imbal_1,p_r_1,n_r_1,rand_imbal_1,positive_2,negative_2,total_positive_2,total_negative_2,imbal_2,p_r_2,n_r_2,rand_imbal_2,positive_3,negative_3,total_positive_3,total_negative_3,imbal_3,p_r_3,n_r_3,rand_imbal_3"); } else { output_writer.Write("networkid,i,j,positive_1,negative_1,imbal_1,positive_2,negative_2,total_positive_2,total_negative_2,imbal_2,positive_3,negative_3,total_positive_3,total_negative_3,imbal_3,"); } } else if (order == 2) { if (Null) { output_writer.WriteLine("networkid,i,j,positive_1,negative_1,imbal_1,p_r_1,n_r_1,rand_imbal_1,positive_2,negative_2,total_positive_2,total_negative_2,imbal_2,p_r_2,n_r_2,rand_imbal_2"); } else { output_writer.WriteLine("networkid,i,j,positive_1,negative_1,imbal_1,positive_2,negative_2,total_positive_2,total_negative_2,imbal_2"); } } else if (order == 1) { if (Null) { output_writer.WriteLine("networkid,i,j,positive_1,negative_1,imbal_1,p_r_1,n_r_1,rand_imbal_1"); } else { output_writer.WriteLine("networkid,i,j,positive_1,negative_1,imbal_1"); } } //finding first element var reader_ = new StreamReader(file_name); string current_year = reader_.ReadLine(); var reader__ = reader_.ReadLine().Split(','); int num_cols = reader__.Length; current_year = reader__[0]; //logger.WriteLine("First current_year value = {0}", current_year); reader_.Close(); StreamReader local_reader = new StreamReader(file_name); //Scrolling through file while (!main_reader.EndOfStream) { var main_line = main_reader.ReadLine(); //logger.WriteLine("In first while while main_reader at :{0}",main_line); var cols = main_line.Split(','); // /Console.WriteLine("The num_cols : {0}",num_cols); if (!int.TryParse(cols[0], out val)) { continue; } HashSet <string> states_set = new HashSet <string>(); //Building set of distinct nodes for matrix size & indices //logger.WriteLine("Building HashSet for : {0}", current_year); while (current_year != "" && current_year == cols[0]) { if (main_reader.EndOfStream) { cols[0] = "end"; break; } states_set.Add(cols[1]); states_set.Add(cols[2]); string line = main_reader.ReadLine(); //Console.WriteLine(line); cols = line.Split(','); }//main_reader aka cols is at next year. //Graph to be called using current_year as NetID string NetID = current_year; Dictionary <string, int> states_hash = new Dictionary <string, int>(); //Building HashMap //logger.WriteLine("Building HashMap for year : {0}", current_year); int idx = 0; //Printing hashMap gathered for current year foreach (var element in states_set) { states_hash.Add(element, idx); //logger.WriteLine("{0}-->{1}", element, states_hash[element]); idx++; } int n_nodes = states_set.Count; int[,] x_plus_ref = new int[n_nodes, n_nodes]; int[,] x_minus_ref = new int[n_nodes, n_nodes]; while (!local_reader.EndOfStream && current_year != cols[0]) { var local_line = local_reader.ReadLine(); //logger.WriteLine("local:{0}", local_line); var row_in_year = local_line.Split(','); if (!int.TryParse(row_in_year[0], out val)) { continue; } current_year = row_in_year[0]; if (row_in_year[1] == row_in_year[2]) { continue; } for (int i = 3; i < num_cols; i++) { //logger.WriteLine("key_i = {0}, key_j = {1}",row_in_year[1], row_in_year[2]); if (int.Parse(row_in_year[i]) > 0) { //logger.WriteLine("pos: {0},{1}-->{2},{3}", states_hash[row_in_year[1]], states_hash[row_in_year[2]], row_in_year[1], row_in_year[2]); x_plus_ref[states_hash[row_in_year[1]], states_hash[row_in_year[2]]]++; } if (int.Parse(row_in_year[i]) < 0) { //logger.WriteLine("neg: {0},{1}-->{2},{3}", states_hash[row_in_year[1]], states_hash[row_in_year[2]], row_in_year[1], row_in_year[2]); x_minus_ref[states_hash[row_in_year[1]], states_hash[row_in_year[2]]]++; } } }//current_year now is cols[0] of exit i.e next year PathBasedImbalance g_ref = new PathBasedImbalance(NetID, x_plus_ref, x_minus_ref); g_ref.infoTable(states_hash, order, Null); g_ref.printDataTable(output_writer); //int ptr = 0; //g_ref.findPaths(0,0,ref ptr); Array.Clear(x_plus_ref, 0, x_plus_ref.Length); Array.Clear(x_minus_ref, 0, x_minus_ref.Length); states_hash.Clear(); states_set.Clear(); //logger.WriteLine("Table Printed for : {0}", NetID); Console.WriteLine("Table Printed for : {0}", NetID); } main_reader.Close(); local_reader.Close(); output_writer.Close(); //logger.Close(); //MessageBox.Show("Temp Output saved"); }
public double[,] displayScript(string inputFile, int order, bool Null) { string file_name = inputFile.Replace(@"\\", @"\"); int val; // to check if int or string var main_reader = new StreamReader(file_name); //output_writer must append --> option double [,] output = null; //finding first element var reader_ = new StreamReader(file_name); string current_year = reader_.ReadLine(); var reader__ = reader_.ReadLine().Split(','); int num_cols = reader__.Length; current_year = reader__[0]; //logger.WriteLine("First current_year value = {0}", current_year); reader_.Close(); //For display int endYear = Int32.Parse(current_year); StreamReader local_reader = new StreamReader(file_name); //Scrolling through file while (!main_reader.EndOfStream) { //if(Int32.Parse(current_year)<startYear)continue; if (Int32.Parse(current_year) > endYear) { break; } var main_line = main_reader.ReadLine(); //logger.WriteLine("In first while while main_reader at :{0}",main_line); var cols = main_line.Split(','); // /Console.WriteLine("The num_cols : {0}",num_cols); if (!int.TryParse(cols[0], out val)) { continue; } HashSet <string> states_set = new HashSet <string>(); //Building set of distinct nodes for matrix size & indices //logger.WriteLine("Building HashSet for : {0}", current_year); while (current_year != "" && current_year == cols[0]) { if (main_reader.EndOfStream) { cols[0] = "end"; break; } states_set.Add(cols[1]); states_set.Add(cols[2]); string line = main_reader.ReadLine(); //Console.WriteLine(line); cols = line.Split(','); }//main_reader aka cols is at next year. //Graph to be called using current_year as NetID string NetID = current_year; Dictionary <string, int> states_hash = new Dictionary <string, int>(); //Building HashMap //logger.WriteLine("Building HashMap for year : {0}", current_year); int idx = 0; //Printing hashMap gathered for current year foreach (var element in states_set) { states_hash.Add(element, idx); //logger.WriteLine("{0}-->{1}", element, states_hash[element]); idx++; } int n_nodes = states_set.Count; int[,] x_plus_ref = new int[n_nodes, n_nodes]; int[,] x_minus_ref = new int[n_nodes, n_nodes]; while (!local_reader.EndOfStream && current_year != cols[0]) { var local_line = local_reader.ReadLine(); //logger.WriteLine("local:{0}", local_line); var row_in_year = local_line.Split(','); if (!int.TryParse(row_in_year[0], out val)) { continue; } current_year = row_in_year[0]; if (row_in_year[1] == row_in_year[2]) { continue; } for (int i = 3; i < num_cols; i++) { //logger.WriteLine("key_i = {0}, key_j = {1}",row_in_year[1], row_in_year[2]); if (int.Parse(row_in_year[i]) > 0) { //logger.WriteLine("pos: {0},{1}-->{2},{3}", states_hash[row_in_year[1]], states_hash[row_in_year[2]], row_in_year[1], row_in_year[2]); x_plus_ref[states_hash[row_in_year[1]], states_hash[row_in_year[2]]]++; } if (int.Parse(row_in_year[i]) < 0) { //logger.WriteLine("neg: {0},{1}-->{2},{3}", states_hash[row_in_year[1]], states_hash[row_in_year[2]], row_in_year[1], row_in_year[2]); x_minus_ref[states_hash[row_in_year[1]], states_hash[row_in_year[2]]]++; } } }//current_year now is cols[0] of exit i.e next year PathBasedImbalance g_ref = new PathBasedImbalance(NetID, x_plus_ref, x_minus_ref); g_ref.infoTable(states_hash, order, Null); output = g_ref.ConvertDataTableToMatrix(); Array.Clear(x_plus_ref, 0, x_plus_ref.Length); Array.Clear(x_minus_ref, 0, x_minus_ref.Length); states_hash.Clear(); states_set.Clear(); } main_reader.Close(); local_reader.Close(); return(output); }