-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource
513 lines (471 loc) · 22.5 KB
/
Source
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// Edward Davis 9937240
// Last modified:
//
// Compiled using Visual Studio 2017
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iterator>
#include <sstream>
#include <fstream>
#include <memory>
#include <cstdio>
#include <chrono>
#include <ctime>
#include "measurement_class.h"
int main()
{
//std::vector<std::string> person_data{ "Chris", "Ed Davis", "Carys", "Dan", "Ella", "Lesley", "Ed Neal", "Alex", "Mirin", "Chris Dooley", "Johnny" };
//nominal_data person_variable("Person", person_data, datestamps_from_single_date("20/04/2020", person_data.size()));
////person_variable.print_data();
//std::vector<std::string> eye_colour_data{ "brown", "blue", "blue", "green", "brown", "brown", "blue", "blue", "brown", "brown", "blue" };
//nominal_data eye_colour_variable("Eye Colour", eye_colour_data, datestamps_from_single_date("20/04/2020", eye_colour_data.size()));
////eye_colour_variable.print_data();
//std::vector<double> age_data{ 22, 21, 20, 25, 22, 21, 22, 40, 24, 21, 22 };
//double_data age_variable("Age", age_data, datestamps_from_single_date("19/04/2020", age_data.size()));
////age_variable.print_data();
//std::vector<double> height_data{ 154.1, 148.6, 139.7, 145.2, 146.0, 151.9, 143.1, 147.2, 144.5, 140.4, 150.3 };
//std::vector<double> height_errors{ 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.1, 2.2, 1.9, 1.8, 2.6 };
//double_data height_variable("Height", height_data, height_errors, datestamps_from_single_date("20/04/2020", height_data.size()));
////height_variable.summary();
//experiment test;
//test.set_title("test experiment");
//test.add_variable(person_variable);
//test.add_variable(eye_colour_variable);
//test.add_variable(age_variable);
//test.add_variable(height_variable);
//test.save_experiment();
//experiment test_2;
//test_2.set_title("test experiment 2");
//test_2.add_variable(person_variable);
//test_2.add_variable(eye_colour_variable);
//test_2.add_variable(age_variable);
//test_2.add_variable(height_variable);
//test_2.save_experiment();
//std::vector<std::string> person_data_2{ "Chris", "Ed Davis", "Carys" };
//nominal_data person_variable_2("Person", person_data_2, datestamps_from_single_date("30/04/2020", person_data_2.size()));
//std::vector<std::string> eye_colour_data_2{ "brown", "brown", "blue" };
//nominal_data eye_colour_variable_2("Eye Colour", eye_colour_data_2, datestamps_from_single_date("30/04/2020", eye_colour_data_2.size()));
//std::vector<double> age_data_2{ 22, 22, 21 };
//double_data age_variable_2("Age", age_data_2, datestamps_from_single_date("30/04/2020", age_data_2.size()));
//std::vector<double> height_data_2{ 180, 165, 179 };
//std::vector<double> height_errors_2{ 1, 1, 1 };
//double_data height_variable_2("Height", height_data_2, height_errors_2, datestamps_from_single_date("30/04/2020", height_data_2.size()));
//experiment new_exp{ person_variable_2, "test experiment" };
//new_exp.add_variable(eye_colour_variable_2);
//new_exp.add_variable(age_variable_2, true);
//new_exp.add_variable(height_variable_2);
//std::cout << test << std::endl;
//std::cout << new_exp << "\n\n\n\n\n" << std::endl;
//test.update_experiment(new_exp);
//std::cout << test << std::endl;
//experiment exp_f{ "Music Data.csv" };
//std::cout << exp_f << std::endl;
//std::vector<std::string> dates{ exp_f.get_datestamps() };
// vector to contain any experiment edited during the program
std::vector<experiment> edited_experiments;
bool program_running{ true };
bool program_shutting_down{ false };
std::cout << "*** Welcome to the Edward Davis Data Management System ***\n" << std::endl;
while (program_running) {
std::cout << "List of quick commands\n"
<< " print ID: returns a table showing the data from the selected experiment\n"
<< " summarise ID: returns a statistical summary of the experiment data by variable\n"
<< " delete ID: deletes the selected experiment - this cannot be undone!\n"
<< " saved experiments: shows a list of available experiments with their ID number\n"
<< " select ID: selects an experiment to input data\n"
<< " new: creates a new experiment to input data\n"
<< " [filename.csv] creates a new experiment from a csv file\n"
<< " x: exits the program and saves edited experiments"
<< std::endl;
// Search for previous saved experiments
std::ifstream saved_experiments("list_of_experimentfiles.txt");
if (!saved_experiments.good()) {
std::cerr << "ERROR: File could not be opened" << std::endl;
std::cin.get();
return(1);
}
std::string filename;
int file_number{ 0 };
std::vector<std::string> list_of_selections;
std::vector<std::string> list_of_available_experiments;
std::cout << "\nList of Available Experiments: " << std::endl;
while (getline(saved_experiments, filename))
{
file_number++;
list_of_selections.push_back(std::to_string(file_number));
list_of_available_experiments.push_back(filename);
std::cout << "\t(" << file_number << ") " << filename << std::endl;
}
saved_experiments.close();
std::cout << std::endl;
// experiment object for the selected experiment object
experiment exp_for_input;
// initial inputs - for quick access to saved data
bool getting_input{ true };
while (getting_input)
{
std::string input;
getline(std::cin, input);
if (is_trigger_used(input, "print")) {
std::string selected_experiment_number{ get_selected_experiment_ID(input, "print") };
// check selection is valid
bool selection_valid{ check_selection_is_valid(selected_experiment_number, list_of_selections) };
if (selection_valid) {
int selected_experiment_number_int{ stoi(selected_experiment_number) };
int selected_experiment_index{ selected_experiment_number_int - 1 };
experiment selected_experiment(list_of_available_experiments[stoi(selected_experiment_number) - 1] + ".txt");
std::cout << selected_experiment << std::endl;
} else std::cout << "I'm sorry, '" << selected_experiment_number << "' is not a valid selection" << std::endl;
} else if (is_trigger_used(input, "summarise")) {
std::string selected_experiment_number{ get_selected_experiment_ID(input, "summarise") };
// check selection is valid
bool selection_valid{ check_selection_is_valid(selected_experiment_number, list_of_selections) };
if (selection_valid) {
int selected_experiment_number_int{ stoi(selected_experiment_number) };
int selected_experiment_index{ selected_experiment_number_int - 1 };
experiment selected_experiment(list_of_available_experiments[stoi(selected_experiment_number) - 1] + ".txt");
selected_experiment.summary();
} else std::cout << "I'm sorry, '" << selected_experiment_number << "' is not a valid selection" << std::endl;
} else if (is_trigger_used(input, "delete")) {
std::string selected_experiment_number{ get_selected_experiment_ID(input, "delete") };
// check selection is valid
bool selection_valid{ check_selection_is_valid(selected_experiment_number, list_of_selections) };
if (selection_valid) {
int selected_experiment_number_int{ stoi(selected_experiment_number) };
int selected_experiment_index{ selected_experiment_number_int - 1 };
// delete experiment
experiment selected_experiment(list_of_available_experiments[stoi(selected_experiment_number) - 1] + ".txt");
selected_experiment.delete_experiment();
// update the available experiments list
std::ifstream saved_experiments("list_of_experimentfiles.txt");
if (!saved_experiments.good()) {
std::cerr << "ERROR: File could not be opened" << std::endl;
} else {
file_number = 0;
list_of_selections.clear();
list_of_available_experiments.clear();
while (getline(saved_experiments, filename)) {
file_number++;
list_of_selections.push_back(std::to_string(file_number));
list_of_available_experiments.push_back(filename);
std::cout << "\t(" << file_number << ") " << filename << std::endl;
}
saved_experiments.close();
std::cout << std::endl;
}
} else std::cout << "I'm sorry, '" << selected_experiment_number << "' is not a valid selection" << std::endl;
} else if (input == "saved experiments") {
// Search for previous saved experiments
std::ifstream saved_experiments("list_of_experimentfiles.txt");
if (!saved_experiments.good()) {
std::cerr << "ERROR: File could not be opened" << std::endl;
} else {
file_number = 0;
list_of_selections.clear();
list_of_available_experiments.clear();
while (getline(saved_experiments, filename))
{
file_number++;
list_of_selections.push_back(std::to_string(file_number));
list_of_available_experiments.push_back(filename);
std::cout << "\t(" << file_number << ") " << filename << std::endl;
}
saved_experiments.close();
std::cout << std::endl;
}
} else if (is_trigger_used(input, "select")) {
std::string selected_experiment_number{ get_selected_experiment_ID(input, "select") };
// check selection is valid
bool selection_valid{ check_selection_is_valid(selected_experiment_number, list_of_selections) };
if (selection_valid) {
int selected_experiment_number_int{ stoi(selected_experiment_number) };
int selected_experiment_index{ selected_experiment_number_int - 1 };
experiment selected_experiment(list_of_available_experiments[selected_experiment_index] + ".txt");
exp_for_input = selected_experiment;
getting_input = false;
} else std::cout << "I'm sorry, '" << selected_experiment_number << "' is not a valid selection" << std::endl;
} else if (input == "help") {
std::cout << "List of quick commands\n"
<< " print ID: returns a table showing the data from the selected experiment\n"
<< " summarise ID: returns a statistical summary of the experiment data by variable\n"
<< " delete ID: deletes the selected experiment - this cannot be undone!\n"
<< " saved experiments: shows a list of available experiments with their ID number\n"
<< " select ID: selects an experiment to input data\n"
<< " [filename.csv] creates a new experiment from a csv file\n"
<< " new: creates a new experiment to input data\n"
<< " x: exits the program and saves edited experiments"
<< std::endl;
} else if (input.find(".csv") != std::string::npos) {
// Create new experiment from csv file
experiment exp_csv{ input };
std::cout << "File found, please name your experiment" << std::endl;
bool title_accepted{ false };
while (!title_accepted) {
std::string csv_exp_title;
getline(std::cin, csv_exp_title);
if (csv_exp_title.find("|") != std::string::npos) {
std::cout << "ERROR: Please do not use '|' in your title" << std::endl;
} else {
title_accepted = true;
exp_csv.set_title(csv_exp_title);
}
}
std::cout << exp_csv << std::endl;
std::cout << "\nWould you like to [save] or [select] this file?" << std::endl;
bool csv_choice_accepted{ false };
bool save_csv{ false };
bool select_csv{ false };
while (!csv_choice_accepted) {
std::string csv_choice;
getline(std::cin, csv_choice);
if (csv_choice == "save") {
save_csv = true;
csv_choice_accepted = true;
} else if (csv_choice == "select") {
select_csv = true;
csv_choice_accepted = true;
} else {
std::cout << "ERROR: Please type either [save] or [select]" << std::endl;
}
}
if (save_csv) exp_csv.save_experiment();
else if (select_csv) {
exp_for_input = exp_csv;
getting_input = false;
}
} else if (input == "x") {
getting_input = false;
program_running = false;
program_shutting_down = true;
break;
} else {
bool selection_valid{ false };
for (size_t i{ 0 }; i < list_of_selections.size(); i++)
{
if (input == list_of_selections[i]) selection_valid = true;
}
if (selection_valid) std::cout << "Please use one of the quick commands with the data (type [help] to see list of commands)" << std::endl;
else std::cout << "I'm sorry, '" << input << "' is not a valid input" << std::endl;
}
}
if (program_shutting_down) break;
// input data for selected experiment
std::cout << exp_for_input.get_title() << " so far..." << std::endl;
std::cout << exp_for_input << std::endl;
std::cout << "Inputting data for " << exp_for_input.get_title() << "..." << std::endl;
std::vector<std::string> variable_names_for_input{ exp_for_input.get_list_of_variable_names() };
std::vector<std::string> variable_types_for_input{ exp_for_input.get_variable_types() };
int input_row{ exp_for_input.get_number_of_rows() + 1 };
bool inputting_data{ true };
int number_of_new_rows{ 0 };
// Find out how many of each variable are in the select experiment
int num_doubles{ 0 };
int num_nominals{ 0 };
int num_errors{ 0 };
int num_datestamps{ 0 };
for (size_t i{ 0 }; i < variable_types_for_input.size(); i++) {
if (variable_types_for_input[i] == "double") num_doubles++;
else if (variable_types_for_input[i] == "nominal") num_nominals++;
else if (variable_types_for_input[i] == "error") num_errors++;
else if (variable_types_for_input[i] == "datestamp") num_datestamps++;
}
// Containers for input data: vectors of variable data
std::vector<std::vector<double>> double_input_data(num_doubles);
std::vector < std::vector<double>> error_input_data(num_errors);
std::vector < std::vector<std::string>> nominal_input_data(num_nominals);
std::vector < std::vector<std::string>> variable_datestamp_input_data(num_datestamps);
std::vector<std::string> experiment_datestamp_input_data;
while (inputting_data)
{
int double_count{ 0 };
int error_count{ 0 };
int nominal_count{ 0 };
int datestamp_count{ 0 };
// Input the data for each column row by row
for (int input_col{ 0 }; input_col < exp_for_input.get_number_of_variables(); input_col++) {
// Check inputs depending on variable type
bool input_accepted{ false };
while (!input_accepted) {
std::cout << variable_names_for_input[input_col] << "[" << input_row << "]: ";
std::string input_data_point;
getline(std::cin, input_data_point);
std::cout << std::endl;
if (variable_types_for_input[input_col] == "double") {
if (!is_data_point_a_number(input_data_point)) {
std::cerr << "ERROR: Variable only accepts numerical data" << std::endl;
} else {
// Add double to data
input_accepted = true;
double_input_data[double_count].push_back(stod(input_data_point));
double_count++;
}
} else if (variable_types_for_input[input_col] == "error") {
if (!is_data_point_a_number(input_data_point)) {
std::cerr << "ERROR: Variable only accepts numerical data" << std::endl;
} else {
// Add double to data
input_accepted = true;
error_input_data[error_count].push_back(stod(input_data_point));
error_count++;
}
} else if (variable_types_for_input[input_col] == "datestamp") {
if (!is_data_point_a_datestamp(input_data_point)) {
std::cerr << "ERROR: Variable only accepts dates with format dd/mm/yyyy" << std::endl;
} else {
// Add datestamp to data
input_accepted = true;
// Update experiment datestamps if appropriate
if (variable_names_for_input[input_col] == "Datestamps") {
experiment_datestamp_input_data.push_back(input_data_point);
}
variable_datestamp_input_data[datestamp_count].push_back(input_data_point);
datestamp_count++;
}
} else if (variable_types_for_input[input_col] == "nominal") {
input_accepted = true;
nominal_input_data[nominal_count].push_back(input_data_point);
nominal_count++;
}
}
}
number_of_new_rows++;
std::cout << "Enter another row? [y/n]" << std::endl;
bool accepted_choice{ false };
std::string row_choice;
while (!accepted_choice) {
getline(std::cin, row_choice);
if (row_choice == "y") {
accepted_choice = true;
input_row++;
} else if (row_choice == "n") {
accepted_choice = true;
inputting_data = false;
} else std::cerr << "ERROR: Please enter 'y' to enter a new row or 'n' to stop data input" << std::endl;
}
}
std::cout << "Adding data to " << exp_for_input.get_title() << "..." << std::endl;
// Create a temporary experiment with new variable data
int double_index{ 0 };
int nominal_index{ 0 };
experiment temp_experiment;
temp_experiment.set_title(exp_for_input.get_title());
for (size_t i{ 0 }; i < variable_types_for_input.size(); i++) {
std::string type{ variable_types_for_input[i] };
// Make double variables
if (type == "double") {
// Initialise variable member data
std::string variable_name{ variable_names_for_input[i] };
std::vector<double> variable_data;
std::vector<double> variable_error_data;
std::vector<std::string> variable_datestamp_data;
bool found_error_data{ false };
bool found_datestamp_data{ false };
// get the variable data
variable_data = double_input_data[double_index];
// look for associated variables (error or datestamp)
for (size_t j{ 0 }; j < variable_names_for_input.size(); j++) {
if (variable_names_for_input[j].find(variable_name) != std::string::npos) {
// work out whether associated variable is an error or datestamp
if (variable_names_for_input[j].find("Error") != std::string::npos) {
// find out location of error and get the data
std::vector<std::string>::iterator it = std::find(variable_names_for_input.begin(), variable_names_for_input.end(), variable_names_for_input[j]);
int error_index = std::distance(variable_names_for_input.begin(), it);
// find out how many error variables came before it
int count_errors_before{ 0 };
for (int k{ 0 }; k < error_index; k++) {
if (variable_names_for_input[k].find("Error") != std::string::npos) count_errors_before++;
}
variable_error_data = error_input_data[count_errors_before];
found_error_data = true;
} else if (variable_names_for_input[j].find("Datestamps") != std::string::npos) {
// find out location of datestamp and get the data
std::vector<std::string>::iterator it = std::find(variable_names_for_input.begin(), variable_names_for_input.end(), variable_names_for_input[j]);
int datestamp_index = std::distance(variable_names_for_input.begin(), it);
// find out how many datestamp variables came before it
int count_datestamps_before{ 0 };
for (int k{ 0 }; k < datestamp_index; k++) {
if (variable_names_for_input[k].find("Datestamps") != std::string::npos) count_datestamps_before++;
}
variable_datestamp_data = variable_datestamp_input_data[count_datestamps_before];
found_datestamp_data = true;
}
}
}
// If can't find error data, set errors to 0
bool force_include_error{ false };
std::vector<double> zeroes_list;
for (int k{ 0 }; k < number_of_new_rows; k++) zeroes_list.push_back(0);
if (!found_error_data) {
variable_error_data = zeroes_list;
}
if (found_error_data && variable_error_data == zeroes_list) force_include_error = true;
// If can't find datestamp data, use the experiment datestamps
bool force_include_datestamp{ false };
if (!found_datestamp_data) {
variable_datestamp_data = experiment_datestamp_input_data;
}
if (found_datestamp_data && variable_datestamp_data == experiment_datestamp_input_data) {
force_include_datestamp = true;
}
// Create variable and add to experiment
double_data temp_variable{ variable_name, variable_data, variable_error_data, variable_datestamp_data };
temp_experiment.add_variable(temp_variable, force_include_datestamp = force_include_datestamp, force_include_error = force_include_error);
double_index++;
} else if (type == "nominal") {
// Make nominal variables
// Initialise variable member data
std::string variable_name{ variable_names_for_input[i] };
std::vector<std::string> variable_data;
std::vector<std::string> variable_datestamp_data;
bool found_datestamp_data{ false };
// get the variable data
variable_data = nominal_input_data[nominal_index];
// look for associated variables (only datestamps)
for (size_t j{ 0 }; j < variable_names_for_input.size(); j++) {
if (variable_names_for_input[j].find(variable_name) != std::string::npos && variable_names_for_input[j] != variable_name) {
// find out location of datestamp and get the data
std::vector<std::string>::iterator it = std::find(variable_names_for_input.begin(), variable_names_for_input.end(), variable_names_for_input[j]);
int datestamp_index = std::distance(variable_names_for_input.begin(), it);
// find out how many datestamp variables came before it
int count_datestamps_before{ 0 };
for (int k{ 0 }; k < datestamp_index; k++) {
if (variable_names_for_input[k].find("Datestamps") != std::string::npos) count_datestamps_before++;
}
variable_datestamp_data = variable_datestamp_input_data[count_datestamps_before];
found_datestamp_data = true;
}
}
// If can't find datestamp data, use the experiment datestamps
bool force_include_datestamp{ false };
if (!found_datestamp_data) {
variable_datestamp_data = experiment_datestamp_input_data;
}
if (found_datestamp_data && variable_datestamp_data == experiment_datestamp_input_data) {
force_include_datestamp = true;
}
// Create variable and add to experiment
nominal_data temp_variable{ variable_name, variable_data, variable_datestamp_data };
temp_experiment.add_variable(temp_variable, force_include_datestamp = force_include_datestamp);
nominal_index++;
}
}
// Summarise the input data
std::cout << "Data input stopped." << std::endl << "Here is the data that was added:" << std::endl
<< temp_experiment << std::endl << "And a summary of your input: " << std::endl;
temp_experiment.summary();
// Add inputs to select experiment
std::cout << "Adding new input to " << exp_for_input.get_title() << std::endl;
exp_for_input.update_experiment(temp_experiment);
std::cout << exp_for_input << std::endl;
// Add experiment to container
edited_experiments.push_back(exp_for_input);
}
// Save all experiments in container
std::cout << "Exiting...saving experiment changes" << std::endl;
for (size_t i{ 0 }; i < edited_experiments.size(); i++) {
edited_experiments[i].save_experiment();
}
return 0;
}