-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.cc
1517 lines (1338 loc) · 43.3 KB
/
process.cc
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
/*
* Copyright (C) 2020 Andrea Mazzoleni
*
* This file is licensed under a
* Creative Commons Attribution 4.0 International License.
*
* You should have received a copy of the license along with this
* work. If not, see <http://creativecommons.org/licenses/by/4.0/>.
*/
#include <set>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iterator>
#include <sys/types.h>
#include <dirent.h>
#include <limits.h>
using namespace std;
// Minimum to show
#define LIMIT_FIT_DAYS 15
#define LIMIT_FIT 2500
#define LIMIT_COUNTRY_DAYS 10
#define LIMIT_COUNTRY 1000
#define LIMIT_STATE 500
#define LIMIT_CITY 250
#define KIND_COUNTRY 0
#define KIND_STATE 1
#define KIND_CITY 2
#define KIND_MIXED 3
struct day {
string date;
mutable int ricoverati;
mutable int terapia_intensiva;
mutable int totale_ospedalizzati;
mutable int isolamento_domiciliare;
mutable int positivi;
mutable int nuovi_attualmente_positivi;
mutable int dimessi_guariti;
mutable int deceduti;
mutable int totale_casi;
mutable int totale_casi_fit;
mutable int tamponi;
mutable int casi_testati;
mutable bool has_fit;
mutable bool has_data;
day(void);
bool operator<(const day& A) const;
};
day::day(void)
{
ricoverati = 0;
terapia_intensiva = 0;
totale_ospedalizzati = 0;
isolamento_domiciliare = 0;
positivi = 0;
nuovi_attualmente_positivi = 0;
dimessi_guariti = 0;
deceduti = 0;
totale_casi = 0;
totale_casi_fit = 0;
tamponi = 0;
casi_testati = 0;
has_fit = false;
has_data = false;
}
bool day::operator<(const day& A) const
{
return date < A.date;
}
typedef set<day> day_set;
struct place {
string trimmed;
string city;
string state;
string country;
int kind;
mutable double r0;
mutable int rmse;
mutable int limite_casi;
mutable string outbreak;
mutable string acceleration;
mutable string turning;
mutable string steady;
mutable string ending;
mutable int max_casi;
mutable int max_isolamento_domiciliare;
mutable int max_ricoverati;
mutable int max_terapia_intensiva;
mutable int max_tamponi;
mutable int max_casi_testati;
mutable int max_positivi;
mutable int max_dimessi_guariti;
mutable day_set days;
place(void);
bool operator<(const place& A) const;
unsigned font_size() const;
string name() const;
double skew() const;
};
place::place(void)
{
kind = -1;
r0 = 0;
rmse = 0;
limite_casi = 0;
max_casi = 0;
max_isolamento_domiciliare = 0;
max_ricoverati = 0;
max_terapia_intensiva = 0;
max_tamponi = 0;
max_casi_testati = 0;
max_positivi = 0;
max_dimessi_guariti = 0;
}
bool place::operator<(const place& A) const
{
return trimmed < A.trimmed;
}
unsigned place::font_size() const
{
unsigned base = 100;
if (max_casi > 0)
base += log10(max_casi) * 30;
return base;
}
string place::name() const
{
switch (kind) {
case KIND_COUNTRY: return country;
case KIND_STATE: return state;
case KIND_CITY: return city;
}
fprintf(stderr, "Unexpected kind\n");
exit(EXIT_FAILURE);
}
double place::skew() const
{
double last_skew = 0;
for(day_set::const_iterator i=days.begin();i!=days.end();++i) {
if (i->has_data && i->has_fit) {
int casi_skew = i->totale_casi - i->totale_casi_fit;
if (rmse != 0)
last_skew = casi_skew / (double)rmse;
}
}
return last_skew;
}
typedef set<place> place_set;
string trim(string s)
{
string r;
for (size_t i=0;i<s.length();++i) {
if (s[i] == '_' || isspace(s[i])) {
r += "_";
continue;
}
if (ispunct(s[i]))
continue;
r += tolower(s[i]);
}
return r;
}
const char* stok(char** s, char sep)
{
char* begin = *s;
char* end = begin;
if (!*begin)
return 0;
// unquote
if (*begin == '"') {
++begin;
++end;
while (*end && *end != '"')
++end;
}
while (*end && *end != sep)
++end;
if (*end == 0) {
*s = end;
} else {
*end = 0;
*s = end + 1;
}
// unquote
if (end > begin && end[-1] == '"')
end[-1] = 0;
return begin;
}
int itok(char** s, char sep)
{
const char* t = stok(s, sep);
if (!t)
return 0;
return atoi(t);
}
void load_csv(int kind, place_set& bag, const char* file, string force_date = "")
{
char buf[1024];
char* s;
FILE* f = fopen(file, "r");
if (!f) {
fprintf(stderr, "Failed opening %s\n", file);
exit(EXIT_FAILURE);
}
bool first_line = true;
bool old_format = false;
while ((s = fgets(buf, sizeof(buf), f)) != 0) {
size_t len;
if (!s)
continue;
// trim spaces at the end
len = strlen(s);
while (len && isspace(s[len-1]))
--len;
s[len] = 0;
if (s[0] == 0)
continue;
// handle first line
if (first_line) {
first_line = false;
const char* format_nazione = "data,stato,ricoverati_con_sintomi,terapia_intensiva,totale_ospedalizzati,isolamento_domiciliare,totale_positivi,variazione_totale_positivi,nuovi_positivi,dimessi_guariti,deceduti,totale_casi,tamponi,casi_testati,note_it,note_en";
const char* format_regioni = "data,stato,codice_regione,denominazione_regione,lat,long,ricoverati_con_sintomi,terapia_intensiva,totale_ospedalizzati,isolamento_domiciliare,totale_positivi,variazione_totale_positivi,nuovi_positivi,dimessi_guariti,deceduti,totale_casi,tamponi,casi_testati,note_it,note_en";
const char* format_province = "data,stato,codice_regione,denominazione_regione,codice_provincia,denominazione_provincia,sigla_provincia,lat,long,totale_casi,note_it,note_en";
const char* format_skip_1 = "Province/State,Country/Region,Last Update,Confirmed,Deaths,Recovered";
const char* format_skip_2 = "Province/State,Country/Region,Last Update,Confirmed,Deaths,Recovered,Latitude,Longitude";
const char* format_mixed_1 = "FIPS,Admin2,Province_State,Country_Region,Last_Update,Lat,Long_,Confirmed,Deaths,Recovered,Active,Combined_Key";
const char* format_mixed_2 = "FIPS,Admin2,Province_State,Country_Region,Last_Update,Lat,Long_,Confirmed,Deaths,Recovered,Active,Combined_Key,Incidence_Rate,Case-Fatality_Ratio";
// skip UTF header
if (s[0] == (char)0xEF && s[1] == (char)0xBB && s[2] == (char)0xBF)
s += 3;
// check for recognized formats
if (kind == KIND_COUNTRY && strcmp(s, format_nazione) == 0)
continue;
if (kind == KIND_STATE && strcmp(s, format_regioni) == 0)
continue;
if (kind == KIND_CITY && strcmp(s, format_province) == 0)
continue;
if (kind == KIND_MIXED && strcmp(s, format_skip_1) == 0) {
old_format = true;
continue;
}
if (kind == KIND_MIXED && strcmp(s, format_skip_2) == 0) {
old_format = true;
continue;
}
if (kind == KIND_MIXED && strcmp(s, format_mixed_1) == 0)
continue;
if (kind == KIND_MIXED && strcmp(s, format_mixed_2) == 0)
continue;
fprintf(stderr, "Unknown format '%s' for file '%s'\n", s, file);
exit(EXIT_FAILURE);
}
if (kind == KIND_CITY) {
struct place p;
struct day d;
p.kind = kind;
d.date = stok(&s, ','); // date
stok(&s, ','); // stato
stok(&s, ','); // codice_regione
p.state = stok(&s, ',');
stok(&s, ','); // codice_provincia
p.city = stok(&s, ',');
stok(&s, ','); // sigla_provincia
stok(&s, ','); // lat
stok(&s, ','); // long
d.totale_casi = itok(&s, ',');
p.country = "Italia";
p.trimmed = trim(p.city);
// truncate to include only the day
d.date = d.date.substr(0, 10);
d.has_data = true;
// insert
pair<const place_set::iterator, bool> j = bag.insert(p);
j.first->days.insert(d);
} else if (kind == KIND_COUNTRY || kind == KIND_STATE) {
struct place p;
struct day d;
p.kind = kind;
d.date = stok(&s, ','); // date
if (kind == KIND_COUNTRY) {
stok(&s, ','); // stato
p.country = "Italia";
p.trimmed = trim(p.country);
} else if (kind == KIND_STATE) {
stok(&s, ','); // stato
stok(&s, ','); // codice_regione
p.state = stok(&s, ',');
stok(&s, ','); // lat
stok(&s, ','); // long
p.country = "Italia";
p.trimmed = trim(p.state);
}
d.ricoverati = itok(&s, ',');
d.terapia_intensiva = itok(&s, ',');
d.totale_ospedalizzati = itok(&s, ',');
d.isolamento_domiciliare = itok(&s, ',');
d.positivi = itok(&s, ',');
stok(&s, ','); // variazione_totale_positivi,
d.nuovi_attualmente_positivi = itok(&s, ',');
d.dimessi_guariti = itok(&s, ',');
d.deceduti = itok(&s, ',');
d.totale_casi = itok(&s, ',');
d.tamponi = itok(&s, ',');
d.casi_testati = itok(&s, ',');
// truncate to include only the day
d.date = d.date.substr(0, 10);
d.has_data = true;
pair<const place_set::iterator, bool> j = bag.insert(p);
j.first->days.insert(d);
} else if (kind == KIND_MIXED) {
struct day d;
string city, state, country;
if (old_format) {
const char* format_skip_2 = "Province/State,Country/Region,Last Update,Confirmed,Deaths,Recovered,Latitude,Longitude";
state = stok(&s, ','); // Province_State
country = stok(&s, ','); // Country_Region
d.date = stok(&s, ','); // Last_Update
d.totale_casi = itok(&s, ','); // Confirmed
d.deceduti = itok(&s, ','); // Deaths
d.dimessi_guariti = itok(&s, ','); // Recovered
d.positivi = d.totale_casi - d.deceduti - d.dimessi_guariti;
// fix name duplication, like France, France
if (state == country)
state = "";
} else {
stok(&s, ','); // FIPS
city = stok(&s, ','); // Admin2
state = stok(&s, ','); // Province_State
country = stok(&s, ','); // Country_Region
d.date = stok(&s, ','); // Last_Update
stok(&s, ','); // Lat
stok(&s, ','); // Long
d.totale_casi = itok(&s, ','); // Confirmed
d.deceduti = itok(&s, ','); // Deaths
d.dimessi_guariti = itok(&s, ','); // Recovered
d.positivi = itok(&s, ','); // Active
}
// special lines for recovered with negative deceduti
if (state == "Recovered")
continue;
// special lines with negative positivi
if (city == "Unassigned" || city == "unassigned")
continue;
if (country == "Mainland China")
country = "China";
if (country == "Korea, South")
country = "South Korea";
// convert early date format
if (d.date.substr(0,5) != "2020-") {
int y, m, dd;
if (sscanf(d.date.c_str(), "%d/%d/%d", &m, &dd, &y) != 3) {
fprintf(stderr, "Ignoring date %s\n", d.date.c_str());
continue;
}
if (y < 2000)
y += 2000;
char buf[32];
sprintf(buf, "%04d-%02d-%02d", y, m, dd);
d.date = buf;
}
// truncate to include only the day
d.date = d.date.substr(0, 10);
d.has_data = true;
// data not reliable from 04-04 with a big increase, maybe added external provinces ?
if (country == "France")
continue;
if (country == "US" && old_format)
continue;
if (country == "US" && city == "Out of GA")
city = "hide";
if (country == "US" && city == "Out of TN")
city = "hide";
if (country == "US" && city == "Michigan Department of Corrections (MDOC)")
city = "hide";
// override the date
if (force_date.length())
d.date = force_date;
if (city.length() == 0 && state.length() == 0) {
// country
place p;
p.kind = KIND_COUNTRY;
p.country = country;
p.trimmed = trim(p.country);
pair<const place_set::iterator, bool> j = bag.insert(p);
j.first->days.insert(d);
} else if (city.length() == 0) {
// ignored
} else {
// city
if (city != "hide") {
place p;
p.kind = KIND_CITY;
p.city = city;
p.state = state;
p.country = country;
p.trimmed = trim(p.city + "_" + p.country);
{
pair<const place_set::iterator, bool> j = bag.insert(p);
j.first->days.insert(d);
}
}
// state
place p_s;
p_s.kind = KIND_STATE;
p_s.state = state;
p_s.country = country;
p_s.trimmed = trim(p_s.state);
{
pair<const place_set::iterator, bool> j = bag.insert(p_s);
pair<const day_set::iterator, bool> k = j.first->days.insert(d);
// if not inserted, add counters
if (!k.second) {
k.first->totale_casi += d.totale_casi;
k.first->deceduti += d.deceduti;
k.first->dimessi_guariti += d.dimessi_guariti;
k.first->positivi += d.positivi;
}
}
// country
place p_c;
p_c.kind = KIND_COUNTRY;
p_c.country = country;
p_c.trimmed = trim(p_c.country);
{
pair<const place_set::iterator, bool> j = bag.insert(p_c);
pair<const day_set::iterator, bool> k = j.first->days.insert(d);
// if not inserted, add counters
if (!k.second) {
k.first->totale_casi += d.totale_casi;
k.first->deceduti += d.deceduti;
k.first->dimessi_guariti += d.dimessi_guariti;
k.first->positivi += d.positivi;
}
}
}
} else {
fprintf(stderr, "Invalid kind\n");
exit(EXIT_FAILURE);
}
}
fclose(f);
}
void load_dir(int kind, place_set& bag, const char* file)
{
DIR* dir;
struct dirent* dirent;
dir = opendir(file);
if (!dir) {
fprintf(stderr, "Failed opening %s\n", file);
exit(EXIT_FAILURE);
}
while ((dirent = readdir(dir)) != 0) {
const char* ext;
char path[PATH_MAX];
if (dirent->d_name[0] == '.')
continue;
ext = strstr(dirent->d_name, ".csv");
if (!ext)
continue;
string force_date = dirent->d_name;
if (force_date.length() < 10) {
fprintf(stderr, "Ignoring file %s\n", dirent->d_name);
continue;
}
force_date = force_date.substr(6, 4) + "-" + force_date.substr(0, 2) + "-" + force_date.substr(3, 2);
snprintf(path, sizeof(path), "%s/%s", file, dirent->d_name);
load_csv(kind, bag, path, force_date);
}
closedir(dir);
}
string next_date(string date)
{
int y, m, d;
char buf[16];
int day_for_month[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (sscanf(date.c_str(), "%d-%d-%d", &y, &m, &d) != 3)
return "";
if (m < 1 || m > 12)
return "";
++d;
if (d > day_for_month[m-1]) {
d = 1;
++m;
}
if (m == 13) {
m = 1;
++y;
}
sprintf(buf, "%04d-%02d-%02d", y, m, d);
return buf;
}
void setup(place_set& bag)
{
// compute max_casi
for (place_set::iterator i=bag.begin();i!=bag.end();++i) {
i->max_casi = 0;
day_set::iterator prev = i->days.end();
for (day_set::iterator j=i->days.begin();j!=i->days.end();++j) {
// fix decreasing
if (prev != i->days.end()) {
if (j->totale_casi < prev->totale_casi)
j->totale_casi = prev->totale_casi;
if (j->deceduti < prev->deceduti)
j->deceduti = prev->deceduti;
if (j->dimessi_guariti < prev->dimessi_guariti)
j->dimessi_guariti = prev->dimessi_guariti;
}
// recompute positivi
j->positivi = j->totale_casi - j->dimessi_guariti - j->deceduti;
// max
if (i->max_casi < j->totale_casi)
i->max_casi = j->totale_casi;
if (i->max_isolamento_domiciliare < j->isolamento_domiciliare)
i->max_isolamento_domiciliare = j->isolamento_domiciliare;
if (i->max_ricoverati < j->ricoverati)
i->max_ricoverati = j->ricoverati;
if (i->max_terapia_intensiva < j->terapia_intensiva)
i->max_terapia_intensiva = j->terapia_intensiva;
if (i->max_tamponi < j->tamponi)
i->max_tamponi = j->tamponi;
if (i->max_casi_testati < j->casi_testati)
i->max_casi_testati = j->casi_testati;
if (i->max_positivi < j->positivi)
i->max_positivi = j->positivi;
if (i->max_dimessi_guariti < j->dimessi_guariti)
i->max_dimessi_guariti = j->dimessi_guariti;
prev = j;
}
prev = i->days.end();
for (day_set::iterator j=i->days.begin();j!=i->days.end();++j) {
// avoid holes in the day sequence
if (prev != i->days.end()) {
string next_1 = next_date(prev->date);
if (next_1 != j->date) {
string next_2 = next_date(next_1);
string next_3 = next_date(next_2);
if (next_2 == j->date) {
// interpolate 2
day d = *prev;
d.date = next_1;
d.has_data = true;
d.totale_casi = (prev->totale_casi + j->totale_casi) / 2;
d.deceduti = (prev->deceduti + j->deceduti) / 2;
d.dimessi_guariti = (prev->dimessi_guariti + j->dimessi_guariti) / 2;
d.positivi = (prev->positivi + j->positivi) / 2;
i->days.insert(d);
} else if (next_3 == j->date) {
// interpolate 3
day d1 = *prev;
d1.date = next_1;
d1.has_data = true;
d1.totale_casi = (2*prev->totale_casi + j->totale_casi) / 3;
d1.deceduti = (2*prev->deceduti + j->deceduti) / 3;
d1.dimessi_guariti = (2*prev->dimessi_guariti + j->dimessi_guariti) / 3;
d1.positivi = (2*prev->positivi + j->positivi) / 3;
i->days.insert(d1);
day d2 = *prev;
d2.date = next_1;
d2.has_data = true;
d2.totale_casi = (prev->totale_casi + 2*j->totale_casi) / 3;
d2.deceduti = (prev->deceduti + 2*j->deceduti) / 3;
d2.dimessi_guariti = (prev->dimessi_guariti + 2*j->dimessi_guariti) / 3;
d2.positivi = (prev->positivi + 2*j->positivi) / 3;
i->days.insert(d2);
} else {
i->days.erase(i->days.begin(), j);
}
}
}
prev = j;
}
}
}
void load_fit(place_set& bag)
{
char buf[256000];
char* s;
const char* tag1 = "### Ca: ";
const char* tag2 = "### R0: ";
const char* tag3 = "### RMSE: ";
const char* tag4 = "### Clim: ";
const char* tag5 = "### Outbreak: ";
const char* tag6 = "### Acceleration: ";
const char* tag7 = "### Turning: ";
const char* tag8 = "### Steady: ";
const char* tag9 = "### Ending: ";
size_t tag1_len = strlen(tag1);
size_t tag2_len = strlen(tag2);
size_t tag3_len = strlen(tag3);
size_t tag4_len = strlen(tag4);
size_t tag5_len = strlen(tag5);
size_t tag6_len = strlen(tag6);
size_t tag7_len = strlen(tag7);
size_t tag8_len = strlen(tag8);
size_t tag9_len = strlen(tag9);
for (place_set::iterator i=bag.begin();i!=bag.end();++i) {
// empty case
if (i->days.size() == 0)
continue;
string trimmed = i->trimmed;
string res = "res/" + trimmed + ".res";
FILE* f = fopen(res.c_str(), "r");
// if missing, there is no fit estimation
if (!f)
continue;
while ((s = fgets(buf, sizeof(buf), f)) != 0) {
size_t len = strlen(s);
// trim spaces at the end
while (len && isspace(s[len-1]))
--len;
s[len] = 0;
if (strncmp(s, tag1, tag1_len) == 0) {
s += tag1_len;
const char* t = stok(&s, ' ');
day_set::iterator j = i->days.begin();
day_set::iterator prev = i->days.end();
while (t) {
// if no more date, insert new one
if (j == i->days.end()) {
day d;
d.date = next_date(prev->date);
if (d.date.length() == 0) {
printf("Invalid date %s\n", prev->date.c_str());
break;
}
pair<day_set::iterator,bool> n = i->days.insert(d);
j = n.first;
}
int v = atoi(t);
j->totale_casi_fit = v;
j->has_fit = true;
prev = j;
++j;
// date is dense then skip 9 values and read the 10th
for(int k=0;k<10 && t!=0;++k)
t = stok(&s, ' ');
}
} else if (strncmp(s, tag2, tag2_len) == 0) {
s += tag2_len;
const char* t = stok(&s, ' ');
i->r0 = t ? atof(t) : 0;
} else if (strncmp(s, tag3, tag3_len) == 0) {
s += tag3_len;
const char* t = stok(&s, ' ');
i->rmse = t ? atoi(t) : 0;
} else if (strncmp(s, tag4, tag4_len) == 0) {
s += tag4_len;
const char* t = stok(&s, ' ');
i->limite_casi = t ? atoi(t) : 0;
} else if (strncmp(s, tag5, tag5_len) == 0) {
s += tag5_len;
const char* t = stok(&s, ' ');
if (t)
i->outbreak = t;
} else if (strncmp(s, tag6, tag6_len) == 0) {
s += tag6_len;
const char* t = stok(&s, ' ');
if (t)
i->acceleration = t;
} else if (strncmp(s, tag7, tag7_len) == 0) {
s += tag6_len;
const char* t = stok(&s, ' ');
if (t)
i->turning = t;
} else if (strncmp(s, tag8, tag8_len) == 0) {
s += tag8_len;
const char* t = stok(&s, ' ');
if (t)
i->steady = t;
} else if (strncmp(s, tag9, tag9_len) == 0) {
s += tag9_len;
const char* t = stok(&s, ' ');
if (t)
i->ending = t;
} else {
printf("Ignored line '%s' for file '%s'\n", s, trimmed.c_str());
}
}
fclose(f);
}
}
void html_header(FILE* f, const char* title)
{
fprintf(f, "<!doctype html public \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n");
fprintf(f, "<html>\n");
fprintf(f, "<head>\n");
fprintf(f, "<!-- Global site tag (gtag.js) - Google Analytics -->\n");
fprintf(f, "<script async src=\"https://www.googletagmanager.com/gtag/js?id=UA-21967501-6\"></script>\n");
fprintf(f, "<script>\n");
fprintf(f, " window.dataLayer = window.dataLayer || [];\n");
fprintf(f, "taLayer.push(arguments);}\n");
fprintf(f, " gtag('js', new Date());\n");
fprintf(f, "\n");
fprintf(f, " gtag('config', 'UA-21967501-6');\n");
fprintf(f, "</script>\n");
fprintf(f, "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
fprintf(f, "<!--\n");
fprintf(f, "Icon from: https://www.iconfinder.com/iconsets/covid-19-1\n");
fprintf(f, "License Creative Commons (Attribution 3.0 Unported)\n");
fprintf(f, "-->\n");
fprintf(f, "<link rel=\"icon\" type=\"image/png\" sizes=\"256x256\" href=\"favicon.png\">\n");
fprintf(f, "<title>%s</title>\n", title);
fprintf(f, "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n");
fprintf(f, "<body>\n");
}
void html_footer(FILE* f)
{
fprintf(f, "<p>License <a href=\"https://github.com/amadvance/covid19-italy/blob/master/LICENSE\">CC-BY-4.0</a></p>\n");
fprintf(f, "</body>\n");
fprintf(f, "</html>\n");
}
#define LAST 5
void table_date(FILE* out, const place& p)
{
day_set::reverse_iterator last;
fprintf(out, "<tr><th>Dati aggiornati al</th>");
last = p.days.rbegin();
while (last->totale_casi == 0)
++last;
for (int i=0;i<LAST-1;++i) {
if (last == p.days.rend())
break;
fprintf(out, "<td>%s</td>", last->date.substr(0,10).c_str());
++last;
}
fprintf(out, "</tr>\n");
}
void table_stat(FILE* out, const place& p, int index)
{
int past[LAST];
day_set::reverse_iterator last;
const char* msg;
last = p.days.rbegin();
while (last->totale_casi == 0)
++last;
for (int i=0;i<LAST;++i) {
switch (index) {
case 0 : past[i] = last->totale_casi; break;
case 1 : past[i] = last->positivi; break;
case 2 : past[i] = last->tamponi; break;
case 3 : past[i] = last->deceduti; break;
case 4 : past[i] = last->dimessi_guariti; break;
case 5 : past[i] = last->isolamento_domiciliare; break;
case 6 : past[i] = last->ricoverati; break;
case 7 : past[i] = last->terapia_intensiva; break;
case 8 : past[i] = last->casi_testati; break;
}
++last;
}
switch (index) {
case 0 : msg = "Casi"; break;
case 1 : msg = "Positivi"; break;
case 2 : msg = "Tamponi"; break;
case 3 : msg = "Deceduti"; break;
case 4 : msg = "Guariti"; break;
case 5 : msg = "Isolamento Domiciliare"; break;
case 6 : msg = "Ricoverati"; break;
case 7 : msg = "Terapia Intensiva"; break;
case 8 : msg = "Soggestti Testati"; break;
}
fprintf(out, "<tr><th>%s</th>", msg);
for (int i=0;i<LAST - 1;++i) {
if (past[i+1]) {
double grow;
int delta = past[i] - past[i+1];
grow = 100.0 * past[i] / past[i+1] - 100.0;
fprintf(out, "<td>%d (%+.1f%%, %+d)</td>\n", past[i], grow, delta);
} else {
fprintf(out, "<td>%d</td>\n", past[i]);
}
}
fprintf(out, "</tr>\n");
}
void save_analyze(FILE* f, string name)
{
string res = "res/" + name + ".res";
fprintf(f, "try\n");
fprintf(f, "\tres = fitVirusCV19(@%s);\n", name.c_str());
fprintf(f, "\tf = fopen('%s','w');\n", res.c_str());
fprintf(f, "\tfprintf(f, '### Ca: ');\n");
fprintf(f, "\tfprintf(f, '%%.0f ', res.Ca);\n");
fprintf(f, "\tfprintf(f, '\\n');\n");
fprintf(f, "\tfprintf(f, '### R0: ');\n");
fprintf(f, "\tfprintf(f, '%%.1f\\n', res.R0);\n");
fprintf(f, "\tfprintf(f, '### RMSE: ');\n");
fprintf(f, "\tfprintf(f, '%%.0f\\n', res.RMSE);\n");
fprintf(f, "\tfprintf(f, '### Clim: ');\n");
fprintf(f, "\tfprintf(f, '%%.0f\\n', res.Clim);\n");
fprintf(f, "\tfprintf(f, '### Outbreak: ');\n");
fprintf(f, "\tfprintf(f, '%%s\\n',res.tp0);\n");
fprintf(f, "\tfprintf(f, '### Acceleration: ');\n");
fprintf(f, "\tfprintf(f, '%%s\\n',res.tp1);\n");
fprintf(f, "\tfprintf(f, '### Turning: ');\n");
fprintf(f, "\tfprintf(f, '%%s\\n',res.tp2);\n");
fprintf(f, "\tfprintf(f, '### Steady: ');\n");
fprintf(f, "\tfprintf(f, '%%s\\n',res.tp3);\n");
fprintf(f, "\tfprintf(f, '### Ending: ');\n");
fprintf(f, "\tfprintf(f, '%%s\\n',res.tp4);\n");
fprintf(f, "\tfclose(f);\n");
fprintf(f, "catch\n");
fprintf(f, "\twarning('Skip %s')\n", name.c_str());
fprintf(f, "end\n");
}
#define CASI_COUNT 8
#define NUOVI_CASI_COUNT 8
#define POSITIVI_COUNT 8
void save_place(FILE* plot, FILE* analyze, FILE* out, const place& p)
{
// new file
string trimmed = p.trimmed;
string dat = "dat/" + trimmed + ".dat";
string fit = "dat/" + trimmed + ".fit";
string get = "get/" + trimmed + ".m";
int monotone_totale_casi = 1;
// empty case
if (p.days.size() == 0)
return;
day_set::iterator first = p.days.begin();
FILE* f_dat = fopen(dat.c_str(), "w");
if (!f_dat) {
fprintf(stderr, "Failed opening %s\n", dat.c_str());
exit(EXIT_FAILURE);
}
FILE* f_fit = fopen(fit.c_str(), "w");
if (!f_fit) {
fprintf(stderr, "Failed opening %s\n", fit.c_str());
exit(EXIT_FAILURE);
}
FILE* f_get = fopen(get.c_str(), "w");
if (!f_get) {
fprintf(stderr, "Failed opening %s\n", get.c_str());
exit(EXIT_FAILURE);
}
fprintf(f_get, "function [country, C,date0] = get_%s()\n", trimmed.c_str());
fprintf(f_get, "country = '%s';\n", trimmed.c_str());
fprintf(f_get, "date0=datenum('%s');\n", first->date.substr(0,10).c_str());
fprintf(f_get, "C = [\n");
int casi_mean_map[CASI_COUNT] = { 0 };
int casi7_mean_map[CASI_COUNT] = { 0 };
int positivi_mean_map[POSITIVI_COUNT] = { 0 };
int casi_count;
int casi7_count;
int positivi_count;
casi_count = 7;
positivi_count = 7;
casi7_count = 7;
if (p.kind == KIND_CITY) {
fprintf(f_dat, "%s\tCasi\tNuoviCasiPercentuale\tNuoviCasi\tNuoviCasiMedia2Giorni\n",
trimmed.c_str());
} else {
fprintf(f_dat, "%s\tRicoverati\tTerapiaIntensiva\tOspedalizzati\tIsolamentoDomiciliare\tPositivi\tNuoviPositivi\tGuariti\tDeceduti\tCasi\tTamponi\tNuoviCasi\tNuoviCasi\tNuoviCasi%dGiorni\tNuoviPositivi%dGiorni\tNuoviPositivi%dGiorni\n",
trimmed.c_str(), casi_count, positivi_count, positivi_count);
}
fprintf(f_fit, "%s\tCasi\tNuoviCasi\tStimaCasi\tStimaNuoviCasi\tNuoviCasi%dGiorni\tNuoviCasi7Giorni\n", trimmed.c_str(), casi_count);
unsigned count = 0;
unsigned count_roll = p.days.size() / 40;
if (count_roll < 1)
count_roll = 1;
day_set::iterator prev = p.days.end();
for (day_set::iterator i=p.days.begin();i!=p.days.end();++i) {
int casi_delta;
int casi_fit_delta;
char casi_str[16];
char casi_delta_str[16];
char casi_fit_delta_str[16];
char casi_fit_str[16];
char casi_mean_str[16];
char casi7_mean_str[16];
// casi delta
if (prev != p.days.end()) {
casi_delta = i->totale_casi - prev->totale_casi;
casi_fit_delta = i->totale_casi_fit - prev->totale_casi_fit;
sprintf(casi_delta_str, "%d", casi_delta);