-
Notifications
You must be signed in to change notification settings - Fork 4
/
concatenated_code.txt
1033 lines (837 loc) · 29.3 KB
/
concatenated_code.txt
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <math.h>
#include <unistd.h>
#include <assert.h>
#include "greatest.h"
#include "file_input_output.h"
#include "terminal_user_input.h"
// #define EVALUATE
//Define a testing suite that is external to reduce code in this file
SUITE_EXTERN(external_suite);
//Define the debug level. Outputs verbose output if enabled.
// #define DEBUG
//Datatype allows classifications to be stored very efficiently
//Is an array of char *, which is a double char *
//In order to use this struct, you must first define an array of char* on the class
typedef struct {
my_string *categories;
int num_categories;
} Classifier_List;
//Datatype is euclidean point
typedef struct point {
float *dimension;
//category must be in the categories array
int category;
} Point;
//Dataset holds all of the points
typedef struct dataset {
//d - the dimensionality of the dataset
int dimensionality;
int num_points;
Point* points;
} Dataset;
//Distance holds the distance from a point, to another point
typedef struct point_neighbour_relationship {
float distance;
Point *neighbour_pointer;
} Point_Neighbour_Relationship;
//Since a comparison point is a distinctly different entity to a data point
typedef struct comparision_point {
float *dimension;
Point_Neighbour_Relationship *neighbour;
} Comparison_Point;
//Apparently C doesn't have boolean types
typedef enum boolean {
false,
true
} bool;
//Distance
//Return: number with the distance, a float
//Inputs, euclidean point, x and euclidean point y
float point_distance(Comparison_Point x, Point y, int dimensions) {
float dist = 0;
float sum = 0;
//for each element in each, get the squared difference
for (int i = 0; i < dimensions; i++) {
//sum this squared difference
sum = sum + pow(x.dimension[i] - y.dimension[i], 2);
}
//get this sum and find the square root
dist = sqrt(sum);
return dist;
}
//Compare two integers
int compare_int(const void *v1, const void *v2) {
//if value 1 is greater than value 2, positive,
//if equal, 0
//if value 1 less value 2, negative
int n1 = *(int*)v1;
int n2 = *(int*)v2;
if (n1 - n2 > 1) {
return 1;
} else if (n1 - n2 < -1) {
return -1;
}
return n1 - n2;
}
//Calculate the mode
int mode(int *values, int num_values) {
//Sort the array
int current_counter = 1;
int max_count = 1;
int max_index = 0;
//Count the number of each number
qsort(values, num_values, sizeof(int), compare_int);
#ifdef DEBUG
printf("[DEBUG] Values[%d]: %d\n", 0, values[0]);
#endif
for (int i = 1; i < num_values; i++) {
//if this is the same as teh last
if (values[i-1] == values[i]) {
//increase the couter
current_counter += 1;
} else if (current_counter > max_count) {
//if the counter is greater than the max counter
//set the max counter to counter
max_count = current_counter;
//update the max_index
max_index = i - 1;
#ifdef DEBUG
printf("[DEBUG] Max index updated to %d\n", i - 1);
#endif
//set the couter to 0
current_counter = 0;
}
//If it's the last one, and the loop doesn't go through again
if (current_counter > max_count) {
//if the counter is greater than the max counter
//set the max counter to counter
max_count = current_counter;
//update the max_index
max_index = i;
#ifdef DEBUG
printf("[DEBUG] Max index updated to %d\n", i - 1);
#endif
}
//Keep a reference to an instance of the highest counted number in the array
#ifdef DEBUG
printf("[DEBUG] Values[%d]: %d\n", i, values[i]);
#endif
}
return values[max_index];
}
//Doing a k nearest neighbour search
int knn_search(int k, Comparison_Point compare, Dataset *datapoints) {
//Warn if k is even
if (k % 2 == 0) {
printf("[WARN] Warning: %d is even. Tie cases have undefined behviour\n", k);
}
//create an array the length of k to put all of the compared points in
compare.neighbour = (Point_Neighbour_Relationship*)malloc(k*sizeof(Point_Neighbour_Relationship));
//For the first k points, just add whatever junk into the array. This way we can just update the largest.
for (int i = 0; i < k; i++) {
float distance = point_distance(compare, datapoints->points[i], datapoints->dimensionality);
compare.neighbour[i].distance = distance;
compare.neighbour[i].neighbour_pointer = datapoints->points+i;
}
//Get the euclidean distance to every neighbour,
for (int i = k; i < datapoints->num_points; i++) {
float distance = point_distance(compare, datapoints->points[i], datapoints->dimensionality);
#ifdef DEBUG
printf("[DEBUG] Point distance: %lf\n", distance);
#endif
//if the neighbour is closer than the last, or it's null pointer distance closest keep it in a distance array
//loop through all of the values for k, and keep the value from the comparison list for the compare point which is update_index.
float max = 0;
int update_index = 0;
for (int j = 0; j < k; j++) {
if (compare.neighbour[j].distance > max) {
max = compare.neighbour[j].distance;
update_index = j;
}
#ifdef DEBUG
printf("[DEBUG] Distance[%d]: %lf\n", j, compare.neighbour[j].distance);
#endif
}
#ifdef DEBUG
printf("[DEBUG] update_index max distance identified to be: %d at distance: %lf\n", update_index, compare.neighbour[update_index].distance);
#endif
//if the current point distance is less than the largest recorded distance, or if the distances haven't been set
if (compare.neighbour[update_index].distance > distance) {
//Update the distance at update_index
#ifdef DEBUG
//printf("[DEBUG] Neighbour number: %d is either null or distance is shorter, updating pointer\n", i);
printf("[DEBUG] Compare neighbour[%d] = %lf\n", update_index, distance);
#endif
compare.neighbour[update_index].distance = distance;
//compare.neighbour[i].neighbour_pointer = &datapoints->points[i];
compare.neighbour[update_index].neighbour_pointer = datapoints->points+i;
#ifdef DEBUG
printf("[DEBUG] category of new point: %d\n", datapoints->points[i].category);
#endif
}
#ifdef DEBUG
printf("==========================================\n");
#endif
}
//Now find the most frequently occurring neighbour pointer type
//first get all the neighbour pointer categories and put them into a neighbour list
int neighbour_categories[k];
for (int c = 0; c < k; c++) {
neighbour_categories[c] = compare.neighbour[c].neighbour_pointer->category;
#ifdef DEBUG
printf("[DEBUG] compare.neighbour[%d].distance: %lf\n", c, compare.neighbour[c].distance);
printf("[DEBUG] Category[%d]: %d\n", c, neighbour_categories[c]);
#endif
}
#ifdef DEBUG
printf("[DEBUG] k is :%d\n", k);
#endif
//Find the mode of the categories
//Call fuction with array of int and the length of the array and return the result
return mode(neighbour_categories, k);
}
//Function that takes in a classification integer, and returns a classification string
//Requires a map between the integers and the string in the form of a classification_map datatype
my_string classify(Classifier_List category_map, int category) {
my_string class = category_map.categories[category];
return class;
}
Point read_point_user(int num_dimensions, int num_categories) {
Point user_point;
user_point.dimension = (float*)malloc(num_dimensions*sizeof(float));
for (int i = 0; i < num_dimensions; i++) {
printf("%dth dimension: ", i);
user_point.dimension[i] = read_float("");
}
user_point.category = read_integer_range("Enter a category ID: ", 0, num_categories - 1);
return user_point;
}
//Passing by reference is less safe, but as a result of the performance increase
//it is justified
void print_point(Point *point_arg, int dimensions) {
printf("(");
int i = 0;
do {
if (i > 0) {
printf(", ");
}
printf("%lf", point_arg->dimension[i]);
i++;
} while(i < dimensions);
printf(") %d\n", point_arg->category);
}
//Read in a dataset given the number of categories
Dataset read_dataset_user(int num_categories) {
//Note only good for small datasets. If items are being added for a huge number
//this operation would get very expensive. Implemention of a linked list would
//potentially be beneficial for this calculation
Dataset user_dataset;
user_dataset.dimensionality = read_integer("Enter the number of dimensions of your classification data: ");
//Number of points (dynamically updated for UX)
user_dataset.num_points = 1;
user_dataset.points = (Point*)malloc(sizeof(Point));
bool enter_another = true;
do {
user_dataset.points[user_dataset.num_points - 1] = read_point_user(user_dataset.dimensionality, num_categories);
enter_another = read_boolean("Enter another? [y/n] ");
if (enter_another) {
user_dataset.num_points += 1;
user_dataset.points = (Point*)realloc(user_dataset.points, user_dataset.num_points*sizeof(Point));
} else {
break;
}
} while(enter_another);
return user_dataset;
}
//Large dataset shouldn't be copied to support large datasets
void print_dataset(Dataset *dataset_arg) {
printf("Dataset\nDimensionality: %d\nNumber of Points: %d\n", dataset_arg->dimensionality, dataset_arg->num_points);
for (int i = 0; i < dataset_arg->num_points; i++) {
print_point(dataset_arg->points + i, dataset_arg->dimensionality);
}
}
Classifier_List read_classes_user() {
//In future, read string and similar calls could be mocked to allow unit testing
//Since the framework already exists for terminal_user input, this could be expanded
Classifier_List classes;
classes.categories = (my_string*)malloc(sizeof(my_string));
classes.num_categories = 1;
bool enter_another = true;
do {
classes.categories[classes.num_categories - 1] = read_string("Category name: ");
enter_another = read_boolean("Enter another? [y/n] ");
if (enter_another) {
classes.num_categories += 1;
classes.categories = (my_string*)realloc(classes.categories, classes.num_categories*sizeof(my_string));
} else {
break;
}
} while(enter_another);
return classes;
}
void print_classes(Classifier_List classes) {
for (int i = 0; i < classes.num_categories; i++) {
printf("Categories: %s\n", classes.categories[i].str);
}
}
Comparison_Point read_comparison_point_user(int num_dimensions) {
Comparison_Point user_point;
user_point.dimension = (float*)malloc(num_dimensions*sizeof(float));
for (int i = 0; i < num_dimensions; i++) {
printf("%dth dimension: ", i);
user_point.dimension[i] = read_float("");
}
//TODO fix memory allocation
return user_point;
}
int count_fields(char *buffer) {
int count = 1;
int pos = 0;
char current;
do {
current = buffer[pos];
// printf("%c", current);
if (current == ',') {
count++;
}
pos++;
} while(current != '\n' && current != '\0');
#ifdef DEBUG
printf("[DEBUG] Number of fields: %d\n", count);
#endif
return count;
}
int get_class_num(my_string in_string, Classifier_List *class_list) {
//Check to see if any of the strings are present in the classifier list
//Could improve with a Levenshtein Distance calculation to account for human errors
//Also, if i is zero, we won't even need to check ifit's in there, we know it's not
#ifdef DEBUG
printf("[DEBUG] class_list->num_categories: %d\n", class_list->num_categories);
#endif
for (int i = 0; i < class_list->num_categories; i++) {
if (strcmp(class_list->categories[i].str, in_string.str) == 0) {
return i;
}
}
//If it isn't present in the existing array, we need to add it in.
//Increment the count of categories
class_list->num_categories++;
#ifdef DEBUG
printf("[DEBUG] Class list categories: %d\n", class_list->num_categories);
#endif
class_list->categories = realloc(class_list->categories, sizeof(my_string) * class_list->num_categories);
class_list->categories[class_list->num_categories - 1] = in_string;
return class_list->num_categories - 1;
}
//Function to read lines from CSV
//Takes a file name
my_string extract_field(my_string line, int field) {
my_string return_value;
//Using https://support.microsoft.com/en-us/help/51327/info-strtok-c-function----documentation-supplement
if (field > count_fields(line.str)) {
strcpy(return_value.str, "\0");
return return_value;
}
//Potentially unsafe
char *token;
token = strtok(line.str, " ,");
//Call strtok "field" times
//Return that value of the token
for (int i = 1; i < field; i++) {
#ifdef DEBUG
printf("[DEBUG] Token is: %s\n", token);
#endif
token = strtok(NULL, " ,");
#ifdef DEBUG
printf("[DEBUG] Before copy in loop\n");
#endif
}
strncpy(return_value.str, token, sizeof(return_value.str));
return return_value;
}
int count_lines(my_string filename) {
FILE *file;
if (access(filename.str, F_OK) == -1) {
printf("[ERROR] Could not find file");
return -1;
}
file = fopen(filename.str, "r");
char buffer[1024];
int count = 0;
while(fgets(buffer, 1024, file)) {
count++;
}
fclose(file);
#ifdef DEBUG
printf("[DEBUG] Line number is: %d\n", count);
#endif
return count;
}
Dataset new_dataset() {
Point *points = {NULL};
Dataset new = {0, 0, points};
return new;
}
//function that takes in a line, and returns a point
//parse point
//TODO ADD UNIT TESTS!!!
Point parse_point(my_string line, int num_dimensions, Classifier_List *class_list) {
float *dimensions = (float*)malloc(num_dimensions*sizeof(float));
for (int i = 0; i < num_dimensions; i++) {
//Go through and pull out the first num fields, and construct a point out of them
// pass the string into a function that just mocks out and returns 1
//Since the extract_field function extracts with a base 1, rather than base of 0
dimensions[i] = atof(extract_field(line, i + 1).str);
}
Point curr_point;
curr_point.dimension = dimensions;
//Since the data for the class is one after the
curr_point.category = get_class_num(extract_field(line, num_dimensions + 1), class_list);
#ifdef DEBUG
print_point(&curr_point, num_dimensions);
#endif
return curr_point;
}
Dataset read_dataset_file(my_string filename, Classifier_List *class_list) {
// Read the number of lines in before opening the files
int num_lines = count_lines(filename);
//From that, it should return some struct
FILE *file;
if (access(filename.str, F_OK) == -1) {
printf("[ERROR] Could not find file");
}
file = fopen(filename.str, "r");
//Struct should contain a 2d array with the lines, in each with data separated into array elements
char *buffer;
buffer = (char*)malloc(sizeof(char) * 1024);
fscanf(file, "%s\n", buffer);
//Count the commas
int num_dimensions = count_fields(buffer) - 1;
//create a Dataset which can hold the rest of the data
//dimensionality is the number of fields -1
//number of points is the number of lines -1, assuming the last line is a blank line
Point *points = (Point*)malloc((num_lines-1)*sizeof(Point));
Dataset return_dataset = {num_dimensions, num_lines - 1, points};
my_string buffer_string;
strcpy(buffer_string.str, buffer);
int i = 0;
//For each line, parse the point and add it to the dataset
do {
points[i] = parse_point(buffer_string, num_dimensions, class_list);
i++;
//Don't do this on the last iteration of the loop
if (!(i == num_lines - 1)) {
fscanf(file, "%s\n", buffer);
strcpy(buffer_string.str, buffer);
}
} while (i < num_lines - 1);
// Now we can essentially read in the first "count" fields and cast to float
// Read in the last field, IE count and add a class for the
free(buffer);
return return_dataset;
}
Classifier_List new_classifier_list() {
int num_categories = 0;
my_string *categories;
categories = malloc(sizeof(my_string));
Classifier_List new_list = {categories, num_categories};
return new_list;
}
//Takes k as a parameter and also a dataset
//Measure the accuracy of the knn given a dataset, using the remove one method
float evaluate_knn(int k, Dataset *benchmark_dataset) {
float accuracy;
Dataset comparison_dataset = new_dataset();
comparison_dataset.dimensionality = benchmark_dataset->dimensionality;
comparison_dataset.num_points = benchmark_dataset->num_points - 1;
comparison_dataset.points = (Point*)malloc(comparison_dataset.num_points*sizeof(Point));
int sum_correct = 0;
// Make a copy of the dataset, except missing the i'th term.
for (int i = 0; i < benchmark_dataset->num_points; i++) {
//Loop through the dataset the number of times there are points
#ifdef DEBUG
printf("i:%d\n", i);
#endif
for (int j = 0; j < comparison_dataset.num_points; j++) {
//Don't copy the ith term
//Index will point to the correct term
int index;
if (j >= i) {
index = j + 1;
} else {
index = j;
}
#ifdef DEBUG
printf("Index: %d\n", index);
#endif
comparison_dataset.points[j] = benchmark_dataset->points[index];
}
//Create a comparison point out of that i'th term
Comparison_Point compare = {benchmark_dataset->points[i].dimension, NULL};
#ifdef DEBUG
printf("Gets to the knn search\n");
#endif
//if the classification matches the category, add it to a sum
if (knn_search(k, compare, &comparison_dataset) == benchmark_dataset->points[i].category) {
sum_correct++;
}
}
accuracy = (float)sum_correct / (float)benchmark_dataset->num_points;
//Print out the percent accuracy for that value of k
#ifdef DEBUG
printf("Accuracy is %lf\%", accuracy * 100);
#endif
return accuracy;
}
#ifndef NDEBUG
//Definitions required for the testrunner
GREATEST_MAIN_DEFS();
#endif
//This main function takes commandline arguments
int main (int argc, char **argv) {
//Wrapped in #ifndef so we can make a release version
#ifndef NDEBUG
//Setup required testing
GREATEST_MAIN_BEGIN();
//Runs tests from external file specified above
RUN_SUITE(external_suite);
//Show results of the testing
GREATEST_MAIN_END();
#endif
Classifier_List class_list = new_classifier_list();
my_string filename = read_string("Filename: ");
//This is in user mode:
Dataset generic_dataset = read_dataset_file(filename, &class_list);
#ifndef EVALUATE
bool another_point = true;
do {
Comparison_Point compare = read_comparison_point_user(generic_dataset.dimensionality);
int k = read_integer("k: ");
int category = knn_search(k, compare, &generic_dataset);
free(compare.neighbour);
#ifdef DEBUG
printf("[DEBUG] Category is: %d\n", category);
#endif
my_string class = classify(class_list, category);
printf("Point classified as: %s\n", class.str);
another_point = read_boolean("Classfy another point? ");
} while(another_point);
#endif
#ifdef EVALUATE
for (int k = 1; k < generic_dataset.num_points; k = k + 2) {
printf("k: %d, accuracy: %lf\n", k, evaluate_knn(k, &generic_dataset));
}
//for values of k up to the number of points that exist in the dataset
#endif
return 0;
}
#include "knn.c"
#include "greatest.h"
#include "terminal_user_input.h"
//Defining tolerances for tests
#define FLOAT_TOLERANCE 0.01
TEST mode_3_inputs(void) {
//Setup array of integers
int inputs[3] = {3, 3, 7};
//Pass array of integers into function
//Check mode is correct
ASSERT_EQ(3, mode(inputs, 3));
PASS();
}
TEST mode_with_zero (void) {
//Setup array of integers
int inputs[3] = {0, 1, 1};
//Pass array of integers into function
//Check mode is correct
ASSERT_EQ(1, mode(inputs, 3));
PASS();
}
TEST mode_7_inputs(void) {
//Setup array of integers
int inputs[7] = {1, 2, 3, 1, 7, 8, 1};
//Pass array of integers into function
//Check mode is correct
ASSERT_EQ(1, mode(inputs, 7));
PASS();
}
//Test bimodal
//Compare two integers that are equal
TEST compare_ints(void) {
int n1 = 1;
int n2 = 1;
ASSERT_EQ(compare_int(&n1, &n2), 0);
PASS();
}
//Compare two integers that are equal
TEST compare_greater_int(void) {
int n1 = 2;
int n2 = 1;
ASSERT_EQ(compare_int(&n1, &n2), 1);
PASS();
}
//Compare two integers that are equal
TEST compare_very_different_int_negative (void) {
int n1 = 1;
int n2 = 4;
ASSERT_EQ(compare_int(&n1, &n2), -1);
PASS();
}
TEST compare_very_different_int_positive (void) {
int n1 = 4;
int n2 = 1;
ASSERT_EQ(compare_int(&n1, &n2), 1);
PASS();
}
/* A test runs various assertions, then calls PASS(), FAIL(), or SKIP(). */
TEST distance_3_dimensions(void) {
float array1[3] = {2.0, 2.0, 2.0};
Comparison_Point point1 = {array1, NULL};
float array2[3] = {5.0, 5.0, 5.0};
Point point2 = {array2, 0, NULL};
ASSERT_IN_RANGE(5.1962, point_distance(point1, point2, 3), FLOAT_TOLERANCE);
PASS();
}
TEST distance_10_dimensions(void) {
float array1[10] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
Comparison_Point point1 = {array1, NULL};
float array2[10] = {10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0};
Point point2 = {array2, 0};
ASSERT_IN_RANGE(28.4605, point_distance(point1, point2, 10), FLOAT_TOLERANCE);
PASS();
}
TEST distance_1_dimension(void) {
float array1[1] = {3.0};
Comparison_Point point1 = {array1, NULL};
float array2[1] = {6.0};
Point point2 = {array2, 0};
ASSERT_IN_RANGE(3.0, point_distance(point1, point2, 1), FLOAT_TOLERANCE);
PASS();
}
TEST distance_1_dimension_fraction(void) {
float array1[1] = {3.0};
Comparison_Point point1 = {array1, NULL};
float array2[1] = {3.5};
Point point2 = {array2, 0};
ASSERT_IN_RANGE(0.5, point_distance(point1, point2, 1), FLOAT_TOLERANCE);
PASS();
}
//How do I initialise arrays with the {} curly braces syntax?
//Test the creation of an array (neighbours) with distances to every single point,
//taking one point and a dataset
//We need a distance associated with a point
//Test k NN search
//Ensure that the returned array contains the correct integers
//Test the nearest 1 neighbour can be found
TEST find_1_nearest_neighbour(void) {
int k = 1;
int category = 0;
//Pass it the stuff it needs, the dataset,
float dimensions[] = {5};
Point point1 = {dimensions, category};
//Since we've only got a length of 1, just use a pointer straight to the single point
Point* points = &point1;
Dataset single_point_dataset = {1, 1, points};
float comparison_dimensions[] = {3};
//TODO, fix the comparison point category
Comparison_Point compare = {comparison_dimensions, NULL};
//One point to compare to the rest
ASSERT_EQ(category, knn_search(k, compare, &single_point_dataset));
free(compare.neighbour);
PASS();
}
//One dimensional, 5 point dataset, find average of k=3 neighbours
//Test the code can handle updating 3 of the 5 without having to update a distance
TEST find_3_nearest_neighbour(void) {
//Setup
int k = 3;
//Pass it the stuff it needs, the dataset,
float dimensions0[] = {5.0};
Point point0 = {dimensions0, 0};
float dimensions1[] = {6.0};
Point point1 = {dimensions1, 1};
float dimensions2[] = {7.0};
Point point2 = {dimensions2, 1};
float dimensions3[] = {0.0};
Point point3 = {dimensions3, 0};
float dimensions4[] = {-1.0};
Point point4 = {dimensions4, 0};
//Since we've only got a length of 1, just use a pointer straight to the single point
Point points[5] = {point0, point1, point2, point3, point4};
Dataset point_dataset = {1, 5, points};
float comparison_dimensions[] = {6.5};
//TODO, fix the comparison point category
Comparison_Point compare = {comparison_dimensions, NULL};
int category = knn_search(k, compare, &point_dataset);
free(compare.neighbour);
//One point to compare to the rest
ASSERT_EQ(1, category);
PASS();
}
TEST classify_int(void) {
//The class integer to be selected
int class = 0;
//Using only the minimum 1 categories
Classifier_List flower_map;
flower_map.categories = (my_string*)malloc(sizeof(my_string));
strcpy(flower_map.categories[0].str, "Iris");
my_string category = classify(flower_map, class);
ASSERT_STR_EQ("Iris", category.str);
PASS();
free(flower_map.categories);
}
TEST extract_field_1(void) {
//From a string of "1.1, 1.2, 1.3, 1.4", extract field 1
my_string test_line;
strcpy(test_line.str, "1.1, 1.2, 1.3, 1.4");
ASSERT_STR_EQ("1.1", extract_field(test_line, 1).str);
PASS();
}
TEST extract_field_4(void) {
//From a string of "1.1, 1.2, 1.3, 1.4", extract field 1
my_string test_line;
strcpy(test_line.str, "1.1, 1.2, 1.3, 1.4");
ASSERT_STR_EQ("1.4", extract_field(test_line, 4).str);
PASS();
}
TEST extract_field_different_formatting(void) {
//From a string of "1.1, 1.2, 1.3, 1.4", extract field 1
my_string test_line;
strcpy(test_line.str, "1.1,,,''1.2,three, 6");
ASSERT_STR_EQ("three", extract_field(test_line, 3).str);
PASS();
}
TEST extract_flower_field(void) {
//From a string of "1.1, 1.2, 1.3, 1.4", extract field 1
my_string test_line;
strcpy(test_line.str, "5.1,3.5,1.4,0.2,Iris-setosa");
ASSERT_STR_EQ("Iris-setosa", extract_field(test_line, 5).str);
PASS();
}
TEST field_2(void) {
//From a string of "1.1, 1.2, 1.3, 1.4", extract field 1
my_string test_line;
strcpy(test_line.str, "5.1,3.5,1.4,0.2,Iris-setosa");
ASSERT_STR_EQ("3.5", extract_field(test_line, 2).str);
PASS();
}
TEST out_of_bounds(void) {
//From a string of "1.1, 1.2, 1.3, 1.4", extract field 1
my_string test_line;
strcpy(test_line.str, "5.1,3.5,1.4,0.2,Iris-setosa");
ASSERT_STR_EQ("\0", extract_field(test_line, 6).str);
PASS();
}
TEST gets_class_int(void) {
//Pass in a string, with a class_list which contains it, see if the correct value is returned
my_string strings[4] = {{"mycategory1"}, {"mycategory2"}, {"mycategory3"}, {"mycategory4"}};
Classifier_List class_list = {strings, 4};
ASSERT_EQ(0, get_class_num(class_list.categories[0], &class_list));
PASS();
}
TEST initialise_category(void) {
Classifier_List new_list = new_classifier_list();
strcpy(new_list.categories[0].str, "Testing Category");
ASSERT_EQ(0, new_list.num_categories);
PASS();
}
TEST create_first_category(void) {
//Pass in a string, with a class_list which contains it, see if the correct value is returned
my_string first_class = {"Test Category"};
Classifier_List class_list = new_classifier_list();
ASSERT_EQ(0, get_class_num(first_class, &class_list));
ASSERT_STR_EQ(first_class.str, class_list.categories[0].str);
PASS();
}
TEST create_new_category(void) {
//Pass in a string, with a class_list which contains it, see if the correct value is returned
my_string first_class = {"Test Category"};
my_string second_class = {"Class2"};
Classifier_List class_list = new_classifier_list();
ASSERT_EQ(0, get_class_num(first_class, &class_list));
ASSERT_STR_EQ(first_class.str, class_list.categories[0].str);
ASSERT_EQ(1, get_class_num(second_class, &class_list));
ASSERT_STR_EQ(second_class.str, class_list.categories[1].str);
#ifdef DEBUG
print_classes(class_list);
#endif
PASS();
}
TEST knn_accuracy(void) {
//Comments step through the expected classification of the knn
//for each point removed and then consider the percentage correct for that k
//In this case k=3
float dimensions0[] = {5.0};
Point point0 = {dimensions0, 0};
//Classed 1
//Incorrect
float dimensions1[] = {6.0};
Point point1 = {dimensions1, 1};
//Classed 0
//Incorrect
float dimensions2[] = {7.0};
Point point2 = {dimensions2, 1};
//Classed 0
//Incorrect
float dimensions3[] = {0.0};
Point point3 = {dimensions3, 0};
//Classed 0
//Correct
float dimensions4[] = {-1.0};
Point point4 = {dimensions4, 0};
//Classed 0
//correct
//Count is 2
// 2/5=0.4
Point points[5] = {point0, point1, point2, point3, point4};
Dataset test_dataset = {1, 5, points};
evaluate_knn(3, &test_dataset);
ASSERT_IN_RANGE(0.4, evaluate_knn(3, &test_dataset), FLOAT_TOLERANCE);
PASS();
}
//Test that the correct number is returned after a call to the string is passed to the classifier
/* Suites can group multiple tests with common setup. */
SUITE(external_suite) {
RUN_TEST(distance_1_dimension);
RUN_TEST(distance_1_dimension_fraction);
RUN_TEST(distance_3_dimensions);
RUN_TEST(distance_10_dimensions);
RUN_TEST(classify_int);
RUN_TEST(mode_3_inputs);
RUN_TEST(mode_with_zero);
RUN_TEST(mode_7_inputs);
RUN_TEST(compare_ints);
RUN_TEST(compare_greater_int);
RUN_TEST(compare_very_different_int_positive);
RUN_TEST(compare_very_different_int_negative);
RUN_TEST(find_1_nearest_neighbour);