-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.c
150 lines (127 loc) · 3.79 KB
/
Program.c
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <errno.h>
int getLengthInt(int i)
{
char str[20];
int len;
sprintf(str, "%d", i);
len = strlen(str);
return len;
}
int getLengthDouble(double d)
{
char str[20];
int len;
sprintf(str, "%lf", d);
len = strlen(str);
return len;
}
char* toString(int i)
{
char *str = malloc(20);
sprintf(str, "%d", i);
return str;
}
int writeSizesToFile(char* fileName, unsigned int sampleSize, unsigned int sampleNumber)
{
FILE *resultFile = fopen(fileName, "w");
if(resultFile == NULL)
{
fprintf(stderr, "Could not open file\n");
return 1;
}
printf("Writing %u sample sizes\n", sampleNumber);
for(int sample = 0; sample < sampleNumber; sample++)
{
fprintf(resultFile, "%u\n", sampleSize);
}
fclose(resultFile);
return 0;
}
// TODO: Move to extra file
int run(char* call)
{
FILE *p = popen(call, "re");
char buf[256];
if (p == NULL)
{
int return_code;
fprintf(stderr,
"Can't run %s; failed with %s - using system call instead\n",
call, strerror(errno));
return_code = system(call);
return return_code;
}
printf("Running %s", call);
while(fgets(buf, sizeof(buf), p) != 0)
{
printf("%s", buf);
}
return pclose(p);
}
int main (int argc, char *argv[])
{
unsigned int population_size, sample_size, sample_count;
double probability;
double sensitivity = 0.909;
double specificity = 0.991;
int seed = 11;
int return_code;
char *sampling_call;
char extration_call[100];
char trial_results_name[100];
double confidence_level;
char *resultsName = "Sampling/sampleResult.tsv";
if (argc < 5 || argc > 8
|| sscanf(argv[1], "%ud", &population_size) != 1
|| sscanf(argv[2], "%ud", &sample_size) != 1
|| sscanf(argv[3], "%ud", &sample_count) != 1
|| sscanf(argv[4], "%lf", &probability) != 1
|| (argc > 5 && sscanf(argv[5], "%d", &seed) != 1)
|| (argc > 6 && sscanf(argv[6], "%lf", &sensitivity) != 1)
|| (argc > 7 && sscanf(argv[7], "%lf", &specificity) != 1))
{
fprintf(stderr, "Usage: %s <Ps> <Ss> <Sc> <p> [<s>] [<se>] [<sp>]\n",
argv[0]);
return 1;
}
sampling_call = calloc(34 + getLengthDouble(probability) + 1 +
getLengthInt(population_size) + 1 +
getLengthInt(sample_count) + 1 +
getLengthInt(-1) + 1 +
getLengthInt(seed) + 1 +
getLengthDouble(sensitivity) + 1 +
getLengthDouble(specificity) + 2, sizeof(char));
sprintf(sampling_call, "cd Sampling && make && ./Program.x %lf %d %d %d %d %lf %lf\n",
probability, population_size, sample_count, -1, seed, sensitivity, specificity);
if (writeSizesToFile(resultsName, sample_size, sample_count) != 0)
{
return 1;
}
printf("Running sampling with %s\n", sampling_call);
return_code = run(sampling_call);
if (return_code != 0)
{
fprintf(stderr, "Running the sampling program failed with %d\n",
return_code);
return 1;
}
// Extraction
strcpy(trial_results_name, resultsName);
confidence_level = 0.90;
sprintf(extration_call, "(cd Extraction && make) && Extraction/Program.x"
" %s %d %d %lf",
trial_results_name, sample_count, population_size,
confidence_level);
return_code = run(extration_call);
if (return_code != 0)
{
fprintf(stderr, "Running the extraction program failed with %d\n",
return_code);
return 1;
}
return 0;
}