Skip to content

Commit

Permalink
Merge pull request #198 from usnistgov/BUGFIX/DuplicateOutput
Browse files Browse the repository at this point in the history
Bugfix/duplicate output
  • Loading branch information
celic authored Sep 9, 2022
2 parents 219d232 + b3ee04d commit cbb3b7f
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 99 deletions.
32 changes: 27 additions & 5 deletions cpp/conditioning_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,13 +446,14 @@ int main(int argc, char* argv[]) {
string timestamp = getCurrentTimestamp();
string outputfilename;
string inputfilename;
string commandline = recreateCommandLine(argc, argv);

for(int i = 0; i < argc; i++) {
for (int i = 0; i < argc; i++) {
std::string Str = std::string(argv[i]);
if("--version" == Str) {
if ("--version" == Str) {
printVersion("conditioning");
exit(0);
}
}
}

while ((opt = getopt(argc, argv, "vnqo:i:c:")) != -1) {
Expand Down Expand Up @@ -489,6 +490,7 @@ int main(int argc, char* argv[]) {
NonIidTestRun testRunNonIid;
testRunNonIid.type = "Conditioning";
testRunNonIid.timestamp = timestamp;
testRunNonIid.commandline = commandline;

// Parse args
if (vetted && argc != 4) {
Expand All @@ -511,7 +513,17 @@ int main(int argc, char* argv[]) {

// get h_in; note that h_in <= n_in
h_in = inputLongDoubleOption(argv[3], 0.0L, (long double) n_in, "h_in");
if (h_in <= 0.0L) print_usage();
if (h_in <= 0.0L) {
if (jsonOutput) {
testRunNonIid.errorLevel = -1;
testRunNonIid.errorMsg = "Error with input: generating h_in.";
ofstream output;
output.open(outputfilename);
output << testRunNonIid.GetAsJson();
output.close();
}
print_usage();
}

if (!vetted) {
if (argc == 4) {
Expand All @@ -520,7 +532,17 @@ int main(int argc, char* argv[]) {
} else {
// If h_p is provided via command line, use that value
h_p = inputLongDoubleOption(argv[4], 0.0L, 1.0L, "h_p");
if (h_p <= 0) print_usage();
if (h_p <= 0) {
if (jsonOutput) {
testRunNonIid.errorLevel = -1;
testRunNonIid.errorMsg = "Error with input: generating h_p.";
ofstream output;
output.open(outputfilename);
output << testRunNonIid.GetAsJson();
output.close();
}
print_usage();
}
}
}
}
Expand Down
66 changes: 44 additions & 22 deletions cpp/iid_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,20 @@ int main(int argc, char* argv[]) {
bool jsonOutput = false;
string timestamp = getCurrentTimestamp();
string outputfilename;
string commandline = recreateCommandLine(argc, argv);

for(int i = 0; i < argc; i++) {
IidTestRun testRun;
testRun.timestamp = timestamp;
testRun.commandline = commandline;

for (int i = 0; i < argc; i++) {
std::string Str = std::string(argv[i]);
if("--version" == Str) {
if ("--version" == Str) {
printVersion("iid");
exit(0);
}
}
}

while ((opt = getopt(argc, argv, "icatvl:qo:")) != -1) {
switch (opt) {
case 'i':
Expand All @@ -104,6 +109,16 @@ int main(int argc, char* argv[]) {
case 'l':
inint = strtoull(optarg, &nextOption, 0);
if ((inint > ULONG_MAX) || (errno == EINVAL) || (nextOption == NULL) || (*nextOption != ',')) {

testRun.errorLevel = -1;
testRun.errorMsg = "Error on index/samples.";

if (jsonOutput) {
ofstream output;
output.open(outputfilename);
output << testRun.GetAsJson();
output.close();
}
print_usage();
}
subsetIndex = inint;
Expand All @@ -112,6 +127,15 @@ int main(int argc, char* argv[]) {

inint = strtoull(nextOption, NULL, 0);
if ((inint > ULONG_MAX) || (errno == EINVAL)) {
testRun.errorLevel = -1;
testRun.errorMsg = "Error on index/samples.";

if (jsonOutput) {
ofstream output;
output.open(outputfilename);
output << testRun.GetAsJson();
output.close();
}
print_usage();
}

Expand Down Expand Up @@ -146,8 +170,6 @@ int main(int argc, char* argv[]) {
// get filename
file_path = argv[0];

IidTestRun testRun;
testRun.timestamp = timestamp;
testRun.filename = file_path;

if (argc == 2) {
Expand Down Expand Up @@ -231,19 +253,19 @@ int main(int argc, char* argv[]) {
int sample_size = data.len;

if ((verbose == 1) || (verbose == 2))
printf("Calculating baseline statistics...\n");
printf("Calculating baseline statistics...\n");

calc_stats(&data, rawmean, median);

if(verbose == 2) {
printf("\tRaw Mean: %f\n", rawmean);
printf("\tMedian: %f\n", median);
printf("\tBinary: %s\n\n", (alphabet_size == 2 ? "true" : "false"));
} else if(verbose > 2) {
printf("Raw Mean = %.17g\n", rawmean);
printf("Median = %.17g\n", median);
printf("Binary = %s\n", (alphabet_size == 2 ? "true" : "false"));
}
if (verbose == 2) {
printf("\tRaw Mean: %f\n", rawmean);
printf("\tMedian: %f\n", median);
printf("\tBinary: %s\n\n", (alphabet_size == 2 ? "true" : "false"));
} else if (verbose > 2) {
printf("Raw Mean = %.17g\n", rawmean);
printf("Median = %.17g\n", median);
printf("Binary = %s\n", (alphabet_size == 2 ? "true" : "false"));
}

IidTestCase tc;
tc.mean = rawmean;
Expand Down Expand Up @@ -303,7 +325,7 @@ int main(int argc, char* argv[]) {
} else {
printf("** Failed chi square tests\n\n");
}
} else if(verbose > 2) {
} else if (verbose > 2) {
if (chi_square_test_pass) {
printf("Chi square tests: Passed\n");
} else {
Expand All @@ -321,7 +343,7 @@ int main(int argc, char* argv[]) {
} else {
printf("** Failed length of longest repeated substring test\n\n");
}
} else if(verbose > 2) {
} else if (verbose > 2) {
if (len_LRS_test_pass) {
printf("Length of longest repeated substring test: Passed\n");
} else {
Expand All @@ -338,13 +360,13 @@ int main(int argc, char* argv[]) {
printf("** Passed IID permutation tests\n\n");
} else {
printf("** Failed IID permutation tests\n\n");
}
} else if(verbose > 2) {
}
} else if (verbose > 2) {
if (perm_test_pass) {
printf("IID permutation tests: Passed\n");
} else {
printf("IID permutation tests: Failed\n");
}
}
}

testRun.testCases.push_back(tc);
Expand Down
65 changes: 49 additions & 16 deletions cpp/non_iid_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,25 @@ int main(int argc, char* argv[]) {
bool jsonOutput = false;
string timestamp = getCurrentTimestamp();
string outputfilename;
string commandline = recreateCommandLine(argc, argv);

NonIidTestRun testRun;
testRun.timestamp = timestamp;
testRun.commandline = commandline;

data.word_size = 0;

initial_entropy = true;
all_bits = true;

for(int i = 0; i < argc; i++) {
for (int i = 0; i < argc; i++) {
std::string Str = std::string(argv[i]);
if("--version" == Str) {
if ("--version" == Str) {
printVersion("noniid");
exit(0);
}
}
}

while ((opt = getopt(argc, argv, "icatvql:o:")) != -1) {
switch (opt) {
case 'i':
Expand All @@ -112,6 +118,15 @@ int main(int argc, char* argv[]) {
case 'l':
inint = strtoull(optarg, &nextOption, 0);
if ((inint > ULONG_MAX) || (errno == EINVAL) || (nextOption == NULL) || (*nextOption != ',')) {
testRun.errorLevel = -1;
testRun.errorMsg = "Error on index/samples.";

if (jsonOutput) {
ofstream output;
output.open(outputfilename);
output << testRun.GetAsJson();
output.close();
}
print_usage();
}
subsetIndex = inint;
Expand All @@ -120,6 +135,15 @@ int main(int argc, char* argv[]) {

inint = strtoull(nextOption, NULL, 0);
if ((inint > ULONG_MAX) || (errno == EINVAL)) {
testRun.errorLevel = -1;
testRun.errorMsg = "Error on index/samples.";

if (jsonOutput) {
ofstream output;
output.open(outputfilename);
output << testRun.GetAsJson();
output.close();
}
print_usage();
}
subsetSize = inint;
Expand Down Expand Up @@ -153,24 +177,33 @@ int main(int argc, char* argv[]) {
char hash[65];
sha256_file(file_path, hash);

NonIidTestRun testRun;
testRun.timestamp = timestamp;
testRun.sha256 = hash;
testRun.filename = file_path;

if (argc == 2) {
// get bits per word
inint = atoi(argv[1]);
if (inint < 1 || inint > 8) {

testRun.errorLevel = -1;
testRun.errorMsg = "Invalid bits per symbol.";

if (jsonOutput) {
ofstream output;
output.open(outputfilename);
output << testRun.GetAsJson();
output.close();
}

printf("Invalid bits per symbol.\n");
print_usage();
} else {
data.word_size = inint;
}
}

if(verbose>1) {
if(subsetSize == 0) printf("Opening file: '%s'\n", file_path);
if (verbose > 1) {
if (subsetSize == 0) printf("Opening file: '%s'\n", file_path);
else printf("Opening file: '%s', reading block %ld of size %ld\n", file_path, subsetIndex, subsetSize);
}

Expand Down Expand Up @@ -318,7 +351,7 @@ int main(int argc, char* argv[]) {
NonIidTestCase tc635;

if ((verbose == 1) || (verbose == 2)) printf("\nRunning Tuple Estimates...\n");

if (((data.alph_size > 2) || !initial_entropy)) {
SAalgs(data.bsymbols, data.blen, 2, bin_t_tuple_res, bin_lrs_res, verbose, "Bitstring");
if (bin_t_tuple_res >= 0.0) {
Expand All @@ -340,7 +373,7 @@ int main(int argc, char* argv[]) {
tc635.testCaseNumber = "T-Tuple Test";
testRun.testCases.push_back(tc635);

// Section 6.3.6 - Estimate entropy with LRS Test
// Section 6.3.6 - Estimate entropy with LRS Test
NonIidTestCase tc636;

if ((((data.alph_size > 2) || !initial_entropy)) && (bin_lrs_res >= 0.0)) {
Expand Down Expand Up @@ -467,18 +500,18 @@ int main(int argc, char* argv[]) {
h_assessed = min(h_assessed, H_original);
}

if((verbose == 1) || (verbose == 2)) {
if(initial_entropy) {
if ((verbose == 1) || (verbose == 2)) {
if (initial_entropy) {
printf("\nH_original: %f\n", H_original);
if(data.alph_size > 2) {
if (data.alph_size > 2) {
printf("H_bitstring: %f\n", H_bitstring);
printf("min(H_original, %d X H_bitstring): %f\n", data.word_size, min(H_original, data.word_size*H_bitstring));
printf("min(H_original, %d X H_bitstring): %f\n", data.word_size, min(H_original, data.word_size * H_bitstring));
}
} else {
printf("\nh': %f\n", H_bitstring);
}
} else if(verbose > 2) {
if((data.alph_size > 2) || !initial_entropy) {
} else if (verbose > 2) {
if ((data.alph_size > 2) || !initial_entropy) {
printf("H_bitstring = %.17g\n", H_bitstring);
printf("H_bitstring Per Symbol = %.17g\n", H_bitstring * data.word_size);
}
Expand Down
Loading

0 comments on commit cbb3b7f

Please sign in to comment.