-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
106 lines (93 loc) · 4.44 KB
/
Copy pathProgram.cs
File metadata and controls
106 lines (93 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CountQuery
{
class LogRecord
{
public double Improvment;
public string SetupName;
public string Scenario;
public LogRecord(string setupname, string scenario, string improvment)
{
if (improvment.EndsWith("%")) improvment = improvment.Substring(0, improvment.Length - 1);
if (!double.TryParse(improvment, out Improvment)) throw new Exception("bad double value for Improvment");
SetupName = setupname;
Scenario = scenario;
}
}
class Program
{
static int Main(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("Usage: CASPERCountQuery logfilename.csv MODE AggColumn");
return 1;
}
string inpFile = args[0];
int mode = int.Parse(args[1]);
string AggColumn = args[2].ToString();
int AggColumnInd = -1;
try
{
var logFile = File.ReadAllLines(inpFile);
// check header of csv
var header = logFile[0].Split(',');
AggColumnInd = Array.FindIndex(header, h => h.Equals(AggColumn));
bool check1 = header[0] == "SetupName";
bool check2 = header[1] == "Scenario";
bool check3 = header[AggColumnInd] == AggColumn;
if (!check1 || !check2 || !check3) throw new Exception("header of CSV does not pass the check.");
List<LogRecord> records = new List<LogRecord>(logFile.Count() - 1);
for (int i = 1; i < logFile.Count(); i++)
{
var record = logFile[i].Split(',');
if (record.Count() != header.Count()) throw new Exception("Bad split by comma in record " + i);
records.Add(new LogRecord(record[0], record[1], record[AggColumnInd]));
}
var allSetupNames = records.Select(r => r.SetupName);
var DistSetupNames = allSetupNames.Distinct().OrderBy(s => s).ToArray();
double[,] output = new double[DistSetupNames.Count(), DistSetupNames.Count()];
for (int i = 0; i < DistSetupNames.Count(); ++i) for (int j = 0; j < DistSetupNames.Count(); ++j) output[i, j] = 0.0;
foreach (var record in records.GroupBy(r => r.Scenario))
{
for (int i = 0; i < DistSetupNames.Count(); ++i)
for (int j = 0; j < DistSetupNames.Count(); ++j)
switch (mode)
{
case 1:
if (record.First(r => r.SetupName == DistSetupNames[i]).Improvment >= record.First(r => r.SetupName == DistSetupNames[j]).Improvment) output[i, j]++;
break;
case 2:
output[i, j] += record.First(r => r.SetupName == DistSetupNames[i]).Improvment - record.First(r => r.SetupName == DistSetupNames[j]).Improvment;
break;
case 3:
if (record.First(r => r.SetupName == DistSetupNames[i]).Improvment > record.First(r => r.SetupName == DistSetupNames[j]).Improvment) output[i, j]++;
break;
default:
throw new Exception("Bad MODE");
}
}
for (int i = -1; i < DistSetupNames.Count(); ++i)
{
for (int j = -1; j < DistSetupNames.Count(); ++j)
{
if (i == -1 && j == -1) System.Console.Write("\t");
else if (i == -1 && j != -1) System.Console.Write(DistSetupNames[j] + "\t");
else if (i != -1 && j == -1) System.Console.Write(Environment.NewLine + DistSetupNames[i] + "\t");
else System.Console.Write(output[i, j].ToString("F3") + "\t");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return 0;
}
}
}