-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset_analyse_nber.py
1037 lines (873 loc) · 48.6 KB
/
dataset_analyse_nber.py
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
"""
Analyse and Construct Meta Data for NBER Data
Dependancies
------------
pyeconlab.NBERWTFConstructor
Notes
-----
1. Should these be converted over to using the internally constucted raw data? [No Keep using Object Library]
"""
import os
import gc
from pyeconlab import NBERWTFConstructor
import matplotlib.pyplot as plt
import pandas as pd
SOURCE_DATA_DIR = os.path.expanduser("~/work-data/datasets/36a376e5a01385782112519bddfac85e/")
#-------------------#
#-Setup Directories-#
#-------------------#
from dataset_info import RESULTS_DIR
RESULTS = RESULTS_DIR["nber"]
#-Levels-#
SITCR2L4 = True
SITCR2L3 = True
SITCR2L2 = True
SITCR2L1 = True
#-Execution Blocks-#
RAW_PRODUCTCODE_COMPOSITION_TABLES = True #Drop World Observations
ADJUSTED_PRODUCTCODE_COMPOSITION_TABLES = True #Adjusted for HongKong-China, Drop World Observations
INTERTEMPORAL_PRODUCTCODE_ADJUSTMENTS = True #Adjusted for HongKong-China, Drop World Observations This also computes adjustments tables for 6200, 7400, 8400
INTERTEMPORAL_PRODUCTCODE_ADJUSTMENTS_DATALOSS = True
RAW_COUNTRYCODE_COMPOSITION_TABLES = True
RAW_SIMPLESTATS_TABLE = True
RAW_UNOFFICIALCODES_CNTRY_PLOTS = True
DATASET_SIMPLESTATS_TABLE = True
DATASET_PERCENTWORLDTRADE_PLOTS = True
DATASET_PRODUCTCODE_INTERTEMPORAL_TABLES = True
#-1974 to 2000 Datasets-#
DATASET_7400_SIMPLESTATS_TABLE = True
DATASET_7400_PERCENTWORLDTRADE_PLOTS = True
DATASET_7400_PRODUCTCODE_INTERTEMPORAL_TABLES = True
#-1984 to 2000 Datasets-#
DATASET_8400_SIMPLESTATS_TABLE = True
DATASET_8400_PERCENTWORLDTRADE_PLOTS = True
DATASET_8400_PRODUCTCODE_INTERTEMPORAL_TABLES = True
#### ------------------ ####
#### ---> RAW DATA <--- ####
#### ------------------ ####
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
## ---> Product Composition Tables <--- ##
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
if RAW_PRODUCTCODE_COMPOSITION_TABLES:
#
# Note: All tables include Meta Data in the Index (SITCR2 Official Code Indicator)
#
print "Running RAW_PRODUCTCODE_COMPOSITION_TABLES ... (drop_world_observations())"
if SITCR2L4:
#-Data: SITC Level 4-#
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
DIR = RESULTS + "intertemporal-productcodes-sitcl4/raw/"
nber.drop_world_observations() #This Keeps NES
#-Intertemporal ProductCode Indicators and Values Tables at Various Levels-#
#-P Index-#
df = nber.intertemporal_productcodes_dataset(tabletype='indicator')
df.to_excel(DIR + 'intertemporal_sitc4_wmeta.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='value')
df.to_excel(DIR + 'intertemporal_sitc4_values_wmeta.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', level=3)
df.to_excel(DIR + 'intertemporal_sitc4_valuecompositions_L3_wmeta.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', level=2)
df.to_excel(DIR + 'intertemporal_sitc4_valuecompositions_L2_wmeta.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', level=1)
df.to_excel(DIR + 'intertemporal_sitc4_valuecompositions_L1_wmeta.xlsx')
#-Intertemporal Composition Tables by Country x Product at Various Higher Levels-#
#-CP Index-#
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', level=3)
df.to_excel(DIR + 'intertemporal_sitc4_exporter_valuecompositions_L3_wmeta_cpidx.xlsx')
#- This Script could be made quicker by adjusting CP and PC Indexes Here -#
# df.reorder_levels(order=['productcode', 'country'])
# df.to_excel(DIR + 'intertemporal_sitc4_exporter_valuecompositions_L3_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', level=3)
df.to_excel(DIR + 'intertemporal_sitc4_importer_valuecompositions_L3_wmeta_cpidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', level=2)
df.to_excel(DIR + 'intertemporal_sitc4_exporter_valuecompositions_L2_wmeta_cpidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', level=2)
df.to_excel(DIR + 'intertemporal_sitc4_importer_valuecompositions_L2_wmeta_cpidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', level=1)
df.to_excel(DIR + 'intertemporal_sitc4_exporter_valuecompositions_L1_wmeta_cpidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', level=1)
df.to_excel(DIR + 'intertemporal_sitc4_importer_valuecompositions_L1_wmeta_cpidx.xlsx')
#-Intertemporal Composition Tables by Product x Country at Various Higher Levels-#
#-PC Index-#
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', cpidx=False, level=3)
df.to_excel(DIR + 'intertemporal_sitc4_exporter_valuecompositions_L3_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', cpidx=False, level=3)
df.to_excel(DIR + 'intertemporal_sitc4_importer_valuecompositions_L3_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', cpidx=False, level=2)
df.to_excel(DIR + 'intertemporal_sitc4_exporter_valuecompositions_L2_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', cpidx=False, level=2)
df.to_excel(DIR + 'intertemporal_sitc4_importer_valuecompositions_L2_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', cpidx=False, level=1)
df.to_excel(DIR + 'intertemporal_sitc4_exporter_valuecompositions_L1_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', cpidx=False, level=1)
df.to_excel(DIR + 'intertemporal_sitc4_importer_valuecompositions_L1_wmeta_pcidx.xlsx')
del nber, df
#------#
if SITCR2L3:
#-Data: SITC Level 3-#
#-Intertemporal ProductCode Indicators and Values Tables at Various Levels-#
DIR = RESULTS + "intertemporal-productcodes-sitcl3/raw/"
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
nber.drop_world_observations() #This Keeps NES nber.collapse_to_productcode_level(level=3, verbose=True)
nber.collapse_to_productcode_level(level=3, verbose=True)
df = nber.intertemporal_productcodes_dataset(tabletype='indicator')
df.to_excel(DIR + 'intertemporal_sitc3_wmeta.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='value')
df.to_excel(DIR + 'intertemporal_sitc3_values_wmeta.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', level=2)
df.to_excel(DIR + 'intertemporal_sitc3_valuecompositions_L2_wmeta.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', level=1)
df.to_excel(DIR + 'intertemporal_sitc3_valuecompositions_L1_wmeta.xlsx')
#-Intertemporal Composition Tables by Country x Product at Various Higher Levels-#
#-CP Index-#
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', level=2)
df.to_excel(DIR + 'intertemporal_sitc3_exporter_valuecompositions_L2_wmeta_cpidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', level=2)
df.to_excel(DIR + 'intertemporal_sitc3_importer_valuecompositions_L2_wmeta_cpidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', level=1)
df.to_excel(DIR + 'intertemporal_sitc3_exporter_valuecompositions_L1_wmeta_cpidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', level=1)
df.to_excel(DIR + 'intertemporal_sitc3_importer_valuecompositions_L1_wmeta_cpidx.xlsx')
#-Intertemporal Composition Tables by Product x Country at Various Higher Levels-#
#-PC Index-#
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', cpidx=False, level=2)
df.to_excel(DIR + 'intertemporal_sitc3_exporter_valuecompositions_L2_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', cpidx=False, level=2)
df.to_excel(DIR + 'intertemporal_sitc3_importer_valuecompositions_L2_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', cpidx=False, level=1)
df.to_excel(DIR + 'intertemporal_sitc3_exporter_valuecompositions_L1_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', cpidx=False, level=1)
df.to_excel(DIR + 'intertemporal_sitc3_importer_valuecompositions_L1_wmeta_pcidx.xlsx')
del nber, df
#------#
if SITCR2L2:
#-Data: SITC Level 2-#
#-Intertemporal ProductCode Indicators and Values Tables at Various Levels-#
DIR = RESULTS + "intertemporal-productcodes-sitcl2/raw/"
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
nber.drop_world_observations() #This Keeps NES
nber.collapse_to_productcode_level(level=2, verbose=True)
df = nber.intertemporal_productcodes_dataset(tabletype='indicator')
df.to_excel(DIR + 'intertemporal_sitc2_wmeta.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='value')
df.to_excel(DIR + 'intertemporal_sitc2_values_wmeta.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', level=1)
df.to_excel(DIR + 'intertemporal_sitc2_valuecompositions_L1_wmeta.xlsx')
#-Intertemporal Composition Tables by Country x Product at Various Higher Levels-#
#-CP Index-#
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', level=1)
df.to_excel(DIR + 'intertemporal_sitc2_exporter_valuecompositions_L1_wmeta_cpidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', level=1)
df.to_excel(DIR + 'intertemporal_sitc2_importer_valuecompositions_L1_wmeta_cpidx.xlsx')
#-Intertemporal Composition Tables by Product x Country at Various Higher Levels-#
#-PC Index-#
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', cpidx=False, level=1)
df.to_excel(DIR + 'intertemporal_sitc2_exporter_valuecompositions_L1_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', cpidx=False, level=1)
df.to_excel(DIR + 'intertemporal_sitc2_importer_valuecompositions_L1_wmeta_pcidx.xlsx')
del nber, df
#------#
#-Intertemporal Exporters-#
#-Intertemporal Number of Exporters Data at SITC4 Level-#
DIR = RESULTS + "intertemporal-exporters/raw/"
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
nber.drop_world_observations() #This Keeps NES
df = nber.intertemporal_productcode_exporters(meta=True)
df.to_excel(DIR + 'intertemporal_sitc4_numcntry_wmeta.xlsx')
del nber, df
#-Intertemporal Number of Exporters Data at SITC3 Level-#
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
nber.drop_world_observations() #This Keeps NES
nber.collapse_to_productcode_level(level=3, verbose=True)
df = nber.intertemporal_productcode_exporters(meta=True)
df.to_excel(DIR + 'intertemporal_sitc3_numcntry_wmeta.xlsx')
del nber, df
#-Intertemporal Number of Exporters Data at SITC2-#
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
nber.drop_world_observations() #This Keeps NES
nber.collapse_to_productcode_level(level=2, verbose=True)
df = nber.intertemporal_productcode_exporters(meta=True)
df.to_excel(DIR + 'intertemporal_sitc2_numcntry_wmeta.xlsx')
del nber, df
#-Intertemporal Number of Exporters Data at SITC1-#
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
nber.drop_world_observations() #This Keeps NES
nber.collapse_to_productcode_level(level=1, verbose=True)
df = nber.intertemporal_productcode_exporters(meta=True)
df.to_excel(DIR + 'intertemporal_sitc1_numcntry_wmeta.xlsx')
del nber, df
#-----------------------------#
#-ADJUSTED COMPOSITION TABLES-#
#-----------------------------#
if ADJUSTED_PRODUCTCODE_COMPOSITION_TABLES:
#
# Note: All tables include Meta Data in the Index (SITCR2 Official Code Indicator)
#
print "Running ADJUSTED_PRODUCTCODE_COMPOSITION_TABLES ..."
if SITCR2L4:
#-Data: SITC Level 4-#
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
DIR = RESULTS + "intertemporal-productcodes-sitcl4/" #These are Primary so Keep at Base Level
nber.adjust_china_hongkongdata(verbose=True)
nber.drop_world_observations(verbose=True) #This Keeps NES
#-Intertemporal ProductCode Indicators and Values Tables at Various Levels-#
#-P Index-#
df = nber.intertemporal_productcodes_dataset(tabletype='indicator')
df.to_excel(DIR + 'intertemporal_sitc4_wmeta.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='value')
df.to_excel(DIR + 'intertemporal_sitc4_values_wmeta.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', level=3)
df.to_excel(DIR + 'intertemporal_sitc4_valuecompositions_L3_wmeta.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', level=2)
df.to_excel(DIR + 'intertemporal_sitc4_valuecompositions_L2_wmeta.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', level=1)
df.to_excel(DIR + 'intertemporal_sitc4_valuecompositions_L1_wmeta.xlsx')
#-Intertemporal Composition Tables by Country x Product at Various Higher Levels-#
#-CP Index-#
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', level=3)
df.to_excel(DIR + 'intertemporal_sitc4_exporter_valuecompositions_L3_wmeta_cpidx.xlsx')
#- This Script could be made quicker by adjusting CP and PC Indexes Here -#
# df.reorder_levels(order=['productcode', 'country'])
# df.to_excel(DIR + 'intertemporal_sitc4_exporter_valuecompositions_L3_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', level=3)
df.to_excel(DIR + 'intertemporal_sitc4_importer_valuecompositions_L3_wmeta_cpidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', level=2)
df.to_excel(DIR + 'intertemporal_sitc4_exporter_valuecompositions_L2_wmeta_cpidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', level=2)
df.to_excel(DIR + 'intertemporal_sitc4_importer_valuecompositions_L2_wmeta_cpidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', level=1)
df.to_excel(DIR + 'intertemporal_sitc4_exporter_valuecompositions_L1_wmeta_cpidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', level=1)
df.to_excel(DIR + 'intertemporal_sitc4_importer_valuecompositions_L1_wmeta_cpidx.xlsx')
#-Intertemporal Composition Tables by Product x Country at Various Higher Levels-#
#-PC Index-#
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', cpidx=False, level=3)
df.to_excel(DIR + 'intertemporal_sitc4_exporter_valuecompositions_L3_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', cpidx=False, level=3)
df.to_excel(DIR + 'intertemporal_sitc4_importer_valuecompositions_L3_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', cpidx=False, level=2)
df.to_excel(DIR + 'intertemporal_sitc4_exporter_valuecompositions_L2_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', cpidx=False, level=2)
df.to_excel(DIR + 'intertemporal_sitc4_importer_valuecompositions_L2_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', cpidx=False, level=1)
df.to_excel(DIR + 'intertemporal_sitc4_exporter_valuecompositions_L1_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', cpidx=False, level=1)
df.to_excel(DIR + 'intertemporal_sitc4_importer_valuecompositions_L1_wmeta_pcidx.xlsx')
del nber, df
#------#
if SITCR2L3:
#-Data: SITC Level 3-#
#-Intertemporal ProductCode Indicators and Values Tables at Various Levels-#
DIR = RESULTS + "intertemporal-productcodes-sitcl3/"
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
nber.adjust_china_hongkongdata(verbose=True)
nber.drop_world_observations(verbose=True) #This Keeps NES
nber.collapse_to_productcode_level(level=3, verbose=True)
df = nber.intertemporal_productcodes_dataset(tabletype='indicator')
df.to_excel(DIR + 'intertemporal_sitc3_wmeta.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='value')
df.to_excel(DIR + 'intertemporal_sitc3_values_wmeta.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', level=2)
df.to_excel(DIR + 'intertemporal_sitc3_valuecompositions_L2_wmeta.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', level=1)
df.to_excel(DIR + 'intertemporal_sitc3_valuecompositions_L1_wmeta.xlsx')
#-Intertemporal Composition Tables by Country x Product at Various Higher Levels-#
#-CP Index-#
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', level=2)
df.to_excel(DIR + 'intertemporal_sitc3_exporter_valuecompositions_L2_wmeta_cpidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', level=2)
df.to_excel(DIR + 'intertemporal_sitc3_importer_valuecompositions_L2_wmeta_cpidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', level=1)
df.to_excel(DIR + 'intertemporal_sitc3_exporter_valuecompositions_L1_wmeta_cpidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', level=1)
df.to_excel(DIR + 'intertemporal_sitc3_importer_valuecompositions_L1_wmeta_cpidx.xlsx')
#-Intertemporal Composition Tables by Product x Country at Various Higher Levels-#
#-PC Index-#
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', cpidx=False, level=2)
df.to_excel(DIR + 'intertemporal_sitc3_exporter_valuecompositions_L2_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', cpidx=False, level=2)
df.to_excel(DIR + 'intertemporal_sitc3_importer_valuecompositions_L2_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', cpidx=False, level=1)
df.to_excel(DIR + 'intertemporal_sitc3_exporter_valuecompositions_L1_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', cpidx=False, level=1)
df.to_excel(DIR + 'intertemporal_sitc3_importer_valuecompositions_L1_wmeta_pcidx.xlsx')
del nber, df
#------#
if SITCR2L2:
#-Data: SITC Level 2-#
#-Intertemporal ProductCode Indicators and Values Tables at Various Levels-#
DIR = RESULTS + "intertemporal-productcodes-sitcl2/"
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
nber.adjust_china_hongkongdata(verbose=True)
nber.drop_world_observations(verbose=True) #This Keeps NES
nber.collapse_to_productcode_level(level=2, verbose=True)
df = nber.intertemporal_productcodes_dataset(tabletype='indicator')
df.to_excel(DIR + 'intertemporal_sitc2_wmeta.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='value')
df.to_excel(DIR + 'intertemporal_sitc2_values_wmeta.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', level=1)
df.to_excel(DIR + 'intertemporal_sitc2_valuecompositions_L1_wmeta.xlsx')
#-Intertemporal Composition Tables by Country x Product at Various Higher Levels-#
#-CP Index-#
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', level=1)
df.to_excel(DIR + 'intertemporal_sitc2_exporter_valuecompositions_L1_wmeta_cpidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', level=1)
df.to_excel(DIR + 'intertemporal_sitc2_importer_valuecompositions_L1_wmeta_cpidx.xlsx')
#-Intertemporal Composition Tables by Product x Country at Various Higher Levels-#
#-PC Index-#
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='exporter', cpidx=False, level=1)
df.to_excel(DIR + 'intertemporal_sitc2_exporter_valuecompositions_L1_wmeta_pcidx.xlsx')
df = nber.intertemporal_productcodes_dataset(tabletype='composition', countries='importer', cpidx=False, level=1)
df.to_excel(DIR + 'intertemporal_sitc2_importer_valuecompositions_L1_wmeta_pcidx.xlsx')
del nber, df
#------#
#-Intertemporal Number of Exporters Data at SITC4 Level-#
DIR = RESULTS + "intertemporal-exporters/"
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
nber.adjust_china_hongkongdata(verbose=True)
nber.drop_world_observations(verbose=True) #This Keeps NES
df = nber.intertemporal_productcode_exporters(meta=True)
df.to_excel(DIR + 'intertemporal_sitc4_numcntry_wmeta.xlsx')
del nber, df
#-Intertemporal Number of Exporters Data at SITC3 Level-#
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
nber.adjust_china_hongkongdata(verbose=True)
nber.drop_world_observations(verbose=True) #This Keeps NES
nber.collapse_to_productcode_level(level=3, verbose=True)
df = nber.intertemporal_productcode_exporters(meta=True)
df.to_excel(DIR + 'intertemporal_sitc3_numcntry_wmeta.xlsx')
del nber, df
#-Intertemporal Number of Exporters Data at SITC2-#
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
nber.adjust_china_hongkongdata(verbose=True)
nber.drop_world_observations(verbose=True) #This Keeps NES
nber.collapse_to_productcode_level(level=2, verbose=True)
df = nber.intertemporal_productcode_exporters(meta=True)
df.to_excel(DIR + 'intertemporal_sitc2_numcntry_wmeta.xlsx')
del nber, df
#-Intertemporal Number of Exporters Data at SITC1-#
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
nber.drop_world_observations() #This Keeps NES
nber.collapse_to_productcode_level(level=1, verbose=True)
df = nber.intertemporal_productcode_exporters(meta=True)
df.to_excel(DIR + 'intertemporal_sitc1_numcntry_wmeta.xlsx')
del nber, df
if INTERTEMPORAL_PRODUCTCODE_ADJUSTMENTS:
print "Running INTERTEMPORAL_PRODUCTCODE_ADJUSTMENTS ..."
if SITCR2L4:
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
DIR = RESULTS + "intertemporal-productcodes-sitcl4/" #These are Primary so Keep at Base Level
nber.adjust_china_hongkongdata(verbose=True)
nber.drop_world_observations(verbose=True) #This Keeps NES
#-Intertemporally Consistent Codes Adjustments Table-#
nber.drop_alpha_productcodes(verbose=True) #-Drop These as they remove small amounts of information-#
drop_items, collapse_items, adjust_table = nber.intertemporal_productcode_lists(return_table=True, include_special=(True, "6200"), tabletype="indicator")
print drop_items
print collapse_items
adjust_table.to_excel(DIR + "intertemporal_productcodes_sitcl4_adjustments.xlsx")
pd.Series(drop_items, name="drop").to_csv(DIR + "intertemporal_productcodes_sitcl4_drop.csv")
pd.Series(collapse_items, name="collapse").to_csv(DIR + "intertemporal_productcodes_sitcl4_collapse.csv")
#-Values-#
drop_items, collapse_items, adjust_table = nber.intertemporal_productcode_lists(return_table=True, include_special=(True, "6200"), tabletype="value")
print drop_items
print collapse_items
adjust_table.to_excel(DIR + "intertemporal_productcodes_sitcl4_value_adjustments.xlsx")
#-Composition-#
drop_items, collapse_items, adjust_table = nber.intertemporal_productcode_lists(return_table=True, include_special=(True, "6200"), tabletype="composition")
print drop_items
print collapse_items
adjust_table.to_excel(DIR + "intertemporal_productcodes_sitcl4_composition_adjustments.xlsx")
del nber, adjust_table
if SITCR2L3:
DIR = RESULTS + "intertemporal-productcodes-sitcl3/"
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
nber.adjust_china_hongkongdata(verbose=True)
nber.drop_world_observations(verbose=True) #This Keeps NES
nber.collapse_to_productcode_level(level=3, verbose=True)
#-Intertemporally Consistent Codes Adjustments Table-#
nber.drop_alpha_productcodes(verbose=True) #-Drop These as they remove small amounts of information-#
drop_items, collapse_items, adjust_table = nber.intertemporal_productcode_lists(return_table=True, include_special=(True, "6200"), tabletype="indicator")
print drop_items
print collapse_items
adjust_table.to_excel(DIR + "intertemporal_productcodes_sitcl3_adjustments.xlsx")
pd.Series(drop_items, name="drop").to_csv(DIR + "intertemporal_productcodes_sitcl3_drop.csv")
pd.Series(collapse_items, name="collapse").to_csv(DIR + "intertemporal_productcodes_sitcl3_collapse.csv")
#-Values-#
drop_items, collapse_items, adjust_table = nber.intertemporal_productcode_lists(return_table=True, include_special=(True, "6200"), tabletype="value")
print drop_items
print collapse_items
adjust_table.to_excel(DIR + "intertemporal_productcodes_sitcl3_value_adjustments.xlsx")
#-Composition-#
drop_items, collapse_items, adjust_table = nber.intertemporal_productcode_lists(return_table=True, include_special=(True, "6200"), tabletype="composition")
print drop_items
print collapse_items
adjust_table.to_excel(DIR + "intertemporal_productcodes_sitcl3_composition_adjustments.xlsx")
del nber, adjust_table
if SITCR2L2:
DIR = RESULTS + "intertemporal-productcodes-sitcl2/"
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
nber.adjust_china_hongkongdata(verbose=True)
nber.drop_world_observations(verbose=True) #This Keeps NES
nber.collapse_to_productcode_level(level=2, verbose=True)
#-Intertemporally Consistent Codes Adjustments Table-#
nber.drop_alpha_productcodes(verbose=True) #-Drop These as they remove small amounts of information-#
drop_items, collapse_items, adjust_table = nber.intertemporal_productcode_lists(return_table=True, include_special=(True, "6200"), tabletype="indicator")
print drop_items
print collapse_items
adjust_table.to_excel(DIR + "intertemporal_productcodes_sitcl2_adjustments.xlsx")
pd.Series(drop_items, name="drop").to_csv(DIR + "intertemporal_productcodes_sitcl2_drop.csv")
pd.Series(collapse_items, name="collapse").to_csv(DIR + "intertemporal_productcodes_sitcl2_collapse.csv")
#-Value-#
drop_items, collapse_items, adjust_table = nber.intertemporal_productcode_lists(return_table=True, include_special=(True, "6200"), tabletype="value")
print drop_items
print collapse_items
adjust_table.to_excel(DIR + "intertemporal_productcodes_sitcl2_value_adjustments.xlsx")
#-Composition-#
drop_items, collapse_items, adjust_table = nber.intertemporal_productcode_lists(return_table=True, include_special=(True, "6200"), tabletype="composition")
print drop_items
print collapse_items
adjust_table.to_excel(DIR + "intertemporal_productcodes_sitcl2_composition_adjustments.xlsx")
del nber, adjust_table
#--------------------#
#-Other Time Periods-#
#--------------------#
periods = [xrange(1974,2000+1,1), xrange(1984,2000+1,1)]
for period in periods:
start_year = period[0]
end_year = period[-1]
SpecialCaseDef = "%s%s"%(str(start_year)[2:], str(end_year)[2:])
print "Running INTERTEMPORAL_PRODUCTCODE_ADJUSTMENTS ... Years => %s to %s ... (Special Case -> %s)" % (start_year,end_year, SpecialCaseDef)
if SITCR2L4:
DIR = RESULTS + "intertemporal-productcodes-sitcl4/" #These are Primary so Keep at Base Level
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR, years=period, verbose=True)
nber.complete_dataset = True
nber.adjust_china_hongkongdata(verbose=True)
nber.drop_world_observations(verbose=True) #This Keeps NES
#-Intertemporally Consistent Codes Adjustments Table-#
nber.drop_alpha_productcodes(verbose=True) #-Drop These as they remove small amounts of information-#
drop_items, collapse_items, adjust_table = nber.intertemporal_productcode_lists(return_table=True, include_special=(True, SpecialCaseDef), tabletype="indicator")
print drop_items
print collapse_items
adjust_table.to_excel(DIR + "intertemporal_productcodes_sitcl4_%sto%s_adjustments.xlsx"%(start_year,end_year))
pd.Series(drop_items, name="drop").to_csv(DIR + "intertemporal_productcodes_sitcl4_%sto%s_drop.csv"%(start_year,end_year))
pd.Series(collapse_items, name="collapse").to_csv(DIR + "intertemporal_productcodes_sitcl4_%sto%s_collapse.csv"%(start_year,end_year))
#-Values-#
drop_items, collapse_items, adjust_table = nber.intertemporal_productcode_lists(return_table=True, include_special=(True, SpecialCaseDef), tabletype="value")
print drop_items
print collapse_items
adjust_table.to_excel(DIR + "intertemporal_productcodes_sitcl4_%sto%s_value_adjustments.xlsx"%(start_year,end_year))
#-Composition-#
drop_items, collapse_items, adjust_table = nber.intertemporal_productcode_lists(return_table=True, include_special=(True, SpecialCaseDef), tabletype="composition")
print drop_items
print collapse_items
adjust_table.to_excel(DIR + "intertemporal_productcodes_sitcl4_%sto%s_composition_adjustments.xlsx"%(start_year,end_year))
del nber, adjust_table
if SITCR2L3:
DIR = RESULTS + "intertemporal-productcodes-sitcl3/"
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR, years=period, verbose=True)
nber.complete_dataset = True
nber.adjust_china_hongkongdata(verbose=True)
nber.drop_world_observations(verbose=True) #This Keeps NES
nber.collapse_to_productcode_level(level=3, verbose=True)
#-Intertemporally Consistent Codes Adjustments Table-#
nber.drop_alpha_productcodes(verbose=True) #-Drop These as they remove small amounts of information-#
drop_items, collapse_items, adjust_table = nber.intertemporal_productcode_lists(return_table=True, include_special=(True, SpecialCaseDef), tabletype="indicator")
print drop_items
print collapse_items
adjust_table.to_excel(DIR + "intertemporal_productcodes_sitcl3_%sto%s_adjustments.xlsx"%(start_year,end_year))
pd.Series(drop_items, name="drop").to_csv(DIR + "intertemporal_productcodes_sitcl3_%sto%s_drop.csv"%(start_year,end_year))
pd.Series(collapse_items, name="collapse").to_csv(DIR + "intertemporal_productcodes_sitcl3_%sto%s_collapse.csv"%(start_year,end_year))
#-Values-#
drop_items, collapse_items, adjust_table = nber.intertemporal_productcode_lists(return_table=True, include_special=(True, SpecialCaseDef), tabletype="value")
print drop_items
print collapse_items
adjust_table.to_excel(DIR + "intertemporal_productcodes_sitcl3_%sto%s_value_adjustments.xlsx"%(start_year,end_year))
#-Composition-#
drop_items, collapse_items, adjust_table = nber.intertemporal_productcode_lists(return_table=True, include_special=(True, SpecialCaseDef), tabletype="composition")
print drop_items
print collapse_items
adjust_table.to_excel(DIR + "intertemporal_productcodes_sitcl3_%sto%s_composition_adjustments.xlsx"%(start_year,end_year))
del nber, adjust_table
if SITCR2L2:
DIR = RESULTS + "intertemporal-productcodes-sitcl2/"
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR, years=period, verbose=True)
nber.complete_dataset = True
nber.adjust_china_hongkongdata(verbose=True)
nber.drop_world_observations(verbose=True) #This Keeps NES
nber.collapse_to_productcode_level(level=2, verbose=True)
#-Intertemporally Consistent Codes Adjustments Table-#
nber.drop_alpha_productcodes(verbose=True) #-Drop These as they remove small amounts of information-#
drop_items, collapse_items, adjust_table = nber.intertemporal_productcode_lists(return_table=True, include_special=(True, SpecialCaseDef), tabletype="indicator")
print drop_items
print collapse_items
adjust_table.to_excel(DIR + "intertemporal_productcodes_sitcl2_%sto%s_adjustments.xlsx"%(start_year,end_year))
pd.Series(drop_items, name="drop").to_csv(DIR + "intertemporal_productcodes_sitcl2_%sto%s_drop.csv"%(start_year,end_year))
pd.Series(collapse_items, name="collapse").to_csv(DIR + "intertemporal_productcodes_sitcl2_%sto%s_collapse.csv"%(start_year,end_year))
#-Value-#
drop_items, collapse_items, adjust_table = nber.intertemporal_productcode_lists(return_table=True, include_special=(True, SpecialCaseDef), tabletype="value")
print drop_items
print collapse_items
adjust_table.to_excel(DIR + "intertemporal_productcodes_sitcl2_%sto%s_value_adjustments.xlsx"%(start_year,end_year))
#-Composition-#
drop_items, collapse_items, adjust_table = nber.intertemporal_productcode_lists(return_table=True, include_special=(True, SpecialCaseDef), tabletype="composition")
print drop_items
print collapse_items
adjust_table.to_excel(DIR + "intertemporal_productcodes_sitcl2_%sto%s_composition_adjustments.xlsx"%(start_year,end_year))
del nber, adjust_table
if INTERTEMPORAL_PRODUCTCODE_ADJUSTMENTS_DATALOSS:
print "Running INTERTEMPORAL_PRODUCTCODE_ADJUSTMENTS_DATALOSS ..."
#-World Values-#
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
world_values = nber.dataset.loc[(nber.dataset.importer == "World") & (nber.dataset.exporter == "World")].groupby(["year"]).sum()["value"]
del nber
gc.collect()
DIR = RESULTS + "intertemporal-productcodes-sitcl4/plots/" #These are Primary so Keep at Base Level
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
nber.adjust_china_hongkongdata(verbose=True)
nber.drop_world_observations(verbose=True) #This Keeps NES
#-Intertemporally Consistent Codes Adjustments Table-#
nber.drop_alpha_productcodes(verbose=True) #-Drop These as they remove small amounts of information-#
#-Obtain Different Datasets based on Value Rules-#
rowavgnorms = [0.01,0.5,1,2,5]
rowmaxs = [1,5,10,20,50]
num_products = []
for rowavgnorm in rowavgnorms:
for rowmax in rowmaxs:
drop_items, collapse_items = nber.intertemporal_productcode_lists(return_table=False, include_special=(True, "6200"), tabletype="composition", value_check=(True,rowavgnorm,rowmax))
keep_items = set(nber.dataset.sitc4.unique()).difference(set(drop_items))
num_products.append((rowavgnorm, rowmax, len(keep_items)))
data = nber.dataset.loc[nber.dataset["sitc4"].isin(keep_items)]
yearly_values = data.groupby("year").sum()["value"]
percent_values = yearly_values.div(world_values)*100
fig = percent_values.plot(title="Parameters: rowavgnorm(%s); rowmax(%s)"%(rowavgnorm, rowmax))
plt.savefig(DIR + "rowavgnorm(%s)_rowmax(%s)_percent_wld.pdf"%(rowavgnorm, rowmax))
plt.close()
del data
gc.collect()
print num_products
#This Study shows that a good balance is struck with rowavgnorm = 1, and rowmaxs = 5
#This isn't overly distorting during the early periods and captures most of the dataset value
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
## ---> Country Composition Tables <--- ##
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
if RAW_COUNTRYCODE_COMPOSITION_TABLES:
print "Running RAW_COUNTRYCODE_COMPOSITION_TABLES ..."
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
#-Data: ISO3C -#
#-Intertemporal CountryCode Indicators-#
DIR = RESULTS + "intertemporal-countrycodes/"
iiso3n, eiso3n = nber.intertemporal_countrycodes_dataset(verbose=True)
iiso3n.to_excel(DIR + "intertemporal_iiso3n.xlsx")
eiso3n.to_excel(DIR + "intertemporal_eiso3n.xlsx")
nber.reset_dataset()
nber.complete_dataset = True
iiso3n, eiso3n = nber.intertemporal_countrycodes_raw_data(verbose=True)
iiso3n.to_excel(DIR + "raw_intertemporal_iiso3n.xlsx")
eiso3n.to_excel(DIR + "raw_intertemporal_eiso3n.xlsx")
#### ----> END COMPOSITION TABLES <---- ####
#### ----> SIMPLE STATS TABLES <---- ####
if RAW_SIMPLESTATS_TABLE:
from pyeconlab.trade.util import describe
print "Running RAW_SIMPLESTATS_TABLE ..."
DIR = RESULTS + "tables/"
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
table = describe(nber.dataset, productcode="sitc4", importer="importer", exporter="exporter")
#-Excel Table-#
table.to_excel(DIR + "raw_wtf_stats.xlsx")
#-Latex Snippet-#
with open(DIR + "raw_wtf_stats_table.latex", "w") as latex_file:
latex_file.write(table.to_latex())
del nber
#-Change this to a Single Table-#
# #-By Year-#
# for year in xrange(1962,2000+1,1):
# nber_year = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR, years=[year])
# table = describe(nber_year.dataset, productcode="sitc4", importer="importer", exporter="exporter")
# #-Excel Table-#
# table.to_excel(DIR + "raw_wtf_stats_%s.xlsx" % year)
# #-Latex Snippet-#
# with open(DIR + "raw_wtf_stats_%s.latex" % year, "w") as latex_file:
# latex_file.write(table.to_latex())
#### ----> END SIMPLE STATS TABLES <---- ####
#### -------------------- #####
#### ----> PLOTTING <---- #####
#### -------------------- #####
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
## ---> Unofficial Product Code Plotes <--- ##
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
if RAW_UNOFFICIALCODES_CNTRY_PLOTS:
print "Running RAW_UNOFFICIALCODES_CNTRY_PLOTS ..."
DIR = RESULTS + "plots/percent_unofficial_codes/"
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
nber.add_sitcr2_official_marker()
#-World Export Values-#
world_export = nber.dataset.loc[(nber.dataset.exporter == "World")]
world_export = world_export.groupby(['year', 'SITCR2']).sum()['value']
world_export = world_export.unstack(level=['SITCR2'])
world_export['%'] = world_export[0].div(world_export[0] + world_export[1])*100
s = world_export['%']
s.plot(title="WLD (Percent of Export in Unofficial Codes)", yticks=[0,25,50,75,100])
plt.savefig(DIR + 'WLD_percent_unofficial_export.png')
plt.close()
#-Country Export Values-#
nber.countries_only()
cntry_export = nber.dataset.loc[(nber.dataset.eiso3c != '.')]
cntry_export = cntry_export.groupby(['eiso3c', 'year', 'SITCR2']).sum()['value']
cntry_export = cntry_export.unstack(level=['SITCR2'])
cntry_export['%'] = cntry_export[0].div(cntry_export[0]+cntry_export[1])*100
s = cntry_export['%']
for cntry in s.index.levels[0]:
s.ix[cntry].plot(title="%s (Percent of Export in Unofficial Codes)" % cntry, yticks=[0,25,50,75,100])
plt.savefig(DIR + '%s_percent_unofficial_export.png' % cntry)
plt.close()
#### ------------------------------------------------------------------------------------------- ####
#### ------------------ ####
#### ---> DATASETS <--- ####
#### ------------------ ####
import gc
import glob
import pandas as pd
from dataset_info import TARGET_DATASET_DIR
SOURCE_DIR = TARGET_DATASET_DIR['nber']
STORES = glob.glob(SOURCE_DIR + "*.h5")
STORES = [x for x in STORES if x.split("/")[-1][0:3] != "raw"] #Filter Out RAW Files
TARGET_DIR = RESULTS + "tables/"
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
## ----> SIMPLE STATS TABLES <---- ##
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
if DATASET_SIMPLESTATS_TABLE:
print
print "Running DATASET_SIMPLESTATS_TABLE ..."
print
from pyeconlab.trade.util import describe
for dataset_file in STORES:
print "Running STATS on File %s" % dataset_file
store = pd.HDFStore(dataset_file)
datasets = store.keys()
for dataset in sorted(datasets):
dataset = dataset.strip("/") #Remove Directory Structure
print "Computing SIMPLE STATS for dataset: %s" % dataset
data = pd.read_hdf(dataset_file, key=dataset)
productcode = "".join(dataset_file.split("/")[-1].split("-")[2].split("r2l"))
dataset_table = describe(data, table_name=dataset, productcode=productcode)
if dataset == "A":
table = dataset_table
else:
table = table.merge(dataset_table, left_index=True, right_index=True)
store.close()
#-Excel Table-#
fl = dataset_file.split("/")[-1].split(".")[0] + "_stats" + ".xlsx"
table.to_excel(TARGET_DIR + fl)
#-Latex Snippet-#
fl = dataset_file.split("/")[-1].split(".")[0] + "_stats" + ".tex"
with open(TARGET_DIR + fl, "w") as latex_file:
latex_file.write(table.to_latex())
# #-By Year-#
# Is this Required? #
if DATASET_PERCENTWORLDTRADE_PLOTS:
print
print "Running DATASET_PERCENTWORLDTRADE_PLOTS..."
print
TARGET_DIR = RESULTS + "plots/percent_world_values/"
#-World Values-#
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
world_values = nber.dataset.loc[(nber.dataset.importer == "World") & (nber.dataset.exporter == "World")].groupby(["year"]).sum()["value"]
del nber
gc.collect()
for dataset_file in STORES:
print "Producing GRAPH on File %s" % dataset_file
store = pd.HDFStore(dataset_file)
datasets = store.keys()
for dataset in sorted(datasets):
print "Computing GRAPH for dataset: %s" % dataset
data = pd.read_hdf(dataset_file, key=dataset)
yearly_values = data.groupby(["year"]).sum()["value"]
percent_values = yearly_values.div(world_values)*100
fig = percent_values.plot(title="Dataset: %s (%s)"%(dataset, dataset_file))
plt.savefig(TARGET_DIR + "%s_%s_percent_wld.pdf"%(dataset, dataset_file.split('/')[-1].split('.')[0]))
plt.close()
store.close()
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
## ----> INTERTEMPORAL PRODUCTCODE TABLES <---- ##
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
if DATASET_PRODUCTCODE_INTERTEMPORAL_TABLES:
print "[6200] Running DATASET_PRODUCTCODE_INTERTEMPORAL_TABLES ..."
def split_filenames(fl):
dataset, data_type, classification, years = fl.split("-")
classification, product_level = classification[:-2], classification[-1:]
return dataset, data_type, classification, product_level
TARGET_DIR = RESULTS + "intertemporal-productcodes/"
for store in STORES:
print "Computing Composition Tables for: %s" % store
dataset, data_type, classification, product_level = split_filenames(store.split("/")[-1])
store = pd.HDFStore(store)
for dataset in store.keys():
print "Computing table for dataset: %s ..." % dataset
dataset = dataset.strip("/")
intertemp_product = store[dataset].groupby(["year", "sitc%s"%product_level]).sum().unstack("year")
intertemp_product.columns = intertemp_product.columns.droplevel()
intertemp_product.to_excel(TARGET_DIR + "intertemporal_product_%s_%sl%s_%s.xlsx"%(data_type, classification, product_level, dataset))
store.close()
## --------------------------------------------------- OTHER YEARS ----------------------------------------------------------------- ##
#------------------------#
#-NBER DATA 1974 to 2000-#
#------------------------#
LOCAL_DIR = "Y7400/"
STORES = glob.glob(SOURCE_DIR + LOCAL_DIR + "*.h5")
STORES = [x for x in STORES if x.split("/")[-1][0:3] != "raw"] #Filter Out RAW Files
if DATASET_7400_SIMPLESTATS_TABLE:
print
print "Running DATASET_7400_SIMPLESTATS_TABLE ..."
print
TARGET_DIR = RESULTS + "tables/" + LOCAL_DIR
from pyeconlab.trade.util import describe
for dataset_file in STORES:
print "Running STATS on File %s" % dataset_file
store = pd.HDFStore(dataset_file)
datasets = store.keys()
for dataset in sorted(datasets):
dataset = dataset.strip("/") #Remove Directory Structure
print "Computing SIMPLE STATS for dataset: %s" % dataset
data = pd.read_hdf(dataset_file, key=dataset)
productcode = "".join(dataset_file.split("/")[-1].split("-")[2].split("r2l"))
dataset_table = describe(data, table_name=dataset, productcode=productcode)
if dataset == "A":
table = dataset_table
else:
table = table.merge(dataset_table, left_index=True, right_index=True)
del data
gc.collect()
store.close()
#-Excel Table-#
fl = dataset_file.split("/")[-1].split(".")[0] + "_stats" + ".xlsx"
table.to_excel(TARGET_DIR + fl)
#-Latex Snippet-#
fl = dataset_file.split("/")[-1].split(".")[0] + "_stats" + ".tex"
with open(TARGET_DIR + fl, "w") as latex_file:
latex_file.write(table.to_latex())
if DATASET_7400_PERCENTWORLDTRADE_PLOTS:
print
print "[7400] Running DATASET_7400_PERCENTWORLDTRADE_PLOTS..."
print
TARGET_DIR = RESULTS + "plots/percent_world_values/" + LOCAL_DIR
#-World Values-#
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
world_values = nber.dataset.loc[(nber.dataset.importer == "World") & (nber.dataset.exporter == "World")].groupby(["year"]).sum()["value"]
del nber
gc.collect()
for dataset_file in STORES:
print "Producing GRAPH on File %s" % dataset_file
store = pd.HDFStore(dataset_file)
datasets = store.keys()
for dataset in sorted(datasets):
print "Computing GRAPH for dataset: %s" % dataset
data = pd.read_hdf(dataset_file, key=dataset)
yearly_values = data.groupby(["year"]).sum()["value"]
percent_values = yearly_values.div(world_values)*100
fig = percent_values.plot(title="Dataset: %s (%s)"%(dataset, dataset_file))
plt.savefig(TARGET_DIR + "%s_%s_percent_wld.pdf"%(dataset, dataset_file.split('/')[-1].split('.')[0]))
plt.close()
del data
gc.collect()
store.close()
if DATASET_7400_PRODUCTCODE_INTERTEMPORAL_TABLES:
print "[7400] Running DATASET_PRODUCTCODE_INTERTEMPORAL_TABLES ..."
def split_filenames(fl):
dataset, data_type, classification, years = fl.split("-")
classification, product_level = classification[:-2], classification[-1:]
return dataset, data_type, classification, product_level
TARGET_DIR = RESULTS + "intertemporal-productcodes/Y7400/"
for store in STORES:
print "Computing Composition Tables for: %s" % store
dataset, data_type, classification, product_level = split_filenames(store.split("/")[-1])
store = pd.HDFStore(store)
for dataset in store.keys():
print "Computing table for dataset: %s ..." % dataset
dataset = dataset.strip("/")
intertemp_product = store[dataset].groupby(["year", "sitc%s"%product_level]).sum().unstack("year")
intertemp_product.columns = intertemp_product.columns.droplevel()
intertemp_product.to_excel(TARGET_DIR + "intertemporal_product_%s_%sl%s_%s.xlsx"%(data_type, classification, product_level, dataset))
del intertemp_product
gc.collect()
store.close()
#------------------------#
#-NBER DATA 1984 to 2000-#
#------------------------#
LOCAL_DIR = "Y8400/"
STORES = glob.glob(SOURCE_DIR + LOCAL_DIR + "*.h5")
STORES = [x for x in STORES if x.split("/")[-1][0:3] != "raw"] #Filter Out RAW Files
if DATASET_8400_SIMPLESTATS_TABLE:
print
print "[8400] Running DATASET_8400_SIMPLESTATS_TABLE ..."
print
TARGET_DIR = RESULTS + "tables/" + LOCAL_DIR
from pyeconlab.trade.util import describe
for dataset_file in STORES:
print "Running STATS on File %s" % dataset_file
store = pd.HDFStore(dataset_file)
datasets = store.keys()
for dataset in sorted(datasets):
dataset = dataset.strip("/") #Remove Directory Structure
print "Computing SIMPLE STATS for dataset: %s" % dataset
data = pd.read_hdf(dataset_file, key=dataset)
productcode = "".join(dataset_file.split("/")[-1].split("-")[2].split("r2l"))
dataset_table = describe(data, table_name=dataset, productcode=productcode)
if dataset == "A":
table = dataset_table
else:
table = table.merge(dataset_table, left_index=True, right_index=True)
del data
gc.collect()
store.close()
#-Excel Table-#
fl = dataset_file.split("/")[-1].split(".")[0] + "_stats" + ".xlsx"
table.to_excel(TARGET_DIR + fl)
#-Latex Snippet-#
fl = dataset_file.split("/")[-1].split(".")[0] + "_stats" + ".tex"
with open(TARGET_DIR + fl, "w") as latex_file:
latex_file.write(table.to_latex())
if DATASET_8400_PERCENTWORLDTRADE_PLOTS:
print
print "[8400] Running DATASET_8400_PERCENTWORLDTRADE_PLOTS..."
print
TARGET_DIR = RESULTS + "plots/percent_world_values/" + LOCAL_DIR
#-World Values-#
nber = NBERWTFConstructor(source_dir=SOURCE_DATA_DIR)
world_values = nber.dataset.loc[(nber.dataset.importer == "World") & (nber.dataset.exporter == "World")].groupby(["year"]).sum()["value"]
del nber
gc.collect()
for dataset_file in STORES:
print "Producing GRAPH on File %s" % dataset_file
store = pd.HDFStore(dataset_file)
datasets = store.keys()
for dataset in sorted(datasets):
print "Computing GRAPH for dataset: %s" % dataset