-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathpoland_microsim_gui_feb_20_2020.py
970 lines (784 loc) · 48.5 KB
/
poland_microsim_gui_feb_20_2020.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
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 6 21:27:09 2020
@author: wb305167
"""
import json
from tkinter import *
import tkinter as tk
from tkinter import ttk
import tkinter.font as tkfont
from tkinter.messagebox import showinfo
from tkinter import filedialog
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams.update({'figure.autolayout': True})
from taxcalc import *
from PIL import Image,ImageTk
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.number = 0
self.widgets = []
self.grid()
self.createWidgets()
self.reform={}
self.selected_item = ""
self.selected_value = ""
self.selected_year = 2019
self.sub_directory = "taxcalc"
self.data_filename = "cit_poland.csv"
self.weights_filename = "cit_weights_poland1.csv"
self.policy_filename = "current_law_policy_poland.json"
self.growfactors_filename = "current_law_policy_poland.json"
self.benchmark_filename = "tax_incentives_benchmark.json"
self.total_revenue_text1 = ""
self.reform_revenue_text1 = ""
self.reform_filename = "app01_reform.json"
self.fontStyle = tkfont.Font(family="Calibri", size="12")
self.fontStyle_sub_title = tkfont.Font(family="Calibri", size="14", weight="bold")
self.fontStyle_title = tkfont.Font(family="Calibri", size="18", weight="bold")
self.s = ttk.Style()
self.s.configure('my.TButton', font=self.fontStyle)
self.text_font = ('Calibri', '12')
# positions
self.title_pos_x = 0.5
self.title_pos_y = 0.0
self. block_1_title_pos_x = 0.1
self.block_1_title_pos_y = 0.1
self.block_title_entry_gap_y = 0.05
self.block_entry_entry_gap_y = 0.05
self.block_1_entry_x = 0.15
self.entry_entry_gap_y = 0.03
self.block_1_entry_1_y = (self.block_1_title_pos_y+self.block_title_entry_gap_y)
self.block_1_entry_2_y = (self.block_1_entry_1_y+self.block_entry_entry_gap_y)
self.block_1_entry_3_y = (self.block_1_entry_2_y+self.block_entry_entry_gap_y)
self.block_1_entry_4_y = (self.block_1_entry_3_y+self.block_entry_entry_gap_y)
self.entry_button_gap = 0.02
self.button_1_pos_x = 0.05
self.block_block_gap = 0.1
self.block_1A_title_pos_y = self.block_1_entry_3_y + self.entry_button_gap + self.block_block_gap
self.button_1_pos_y = self.block_1A_title_pos_y + self.block_title_entry_gap_y
self.block_2_entry_1_1_x = 0.03
self.block_2_title_pos_y = self.button_1_pos_y + self.block_block_gap
self.text_entry_gap = 0.03
self.block_2_entry_1_1_y = (self.block_2_title_pos_y+
self.block_title_entry_gap_y+
self.text_entry_gap)
self.block_2_combo_entry_gap_x = 0.21
self.block_2_entry_entry_gap_x = 0.04
self.block_2_entry_1_2_x = self.block_2_entry_1_1_x + self.block_2_combo_entry_gap_x
self.block_2_entry_1_3_x = self.block_2_entry_1_2_x + self.block_2_entry_entry_gap_x
self.block_3_title_pos_x = 0.3
self.block_3_title_pos_y = self.block_1A_title_pos_y
self.block_3_entry_x = 0.35
self.block_3_entry_y = self.block_3_title_pos_y + self.block_title_entry_gap_y
self.button_3_pos_x = 0.3
self.button_3_pos_y = self.block_3_entry_y + 2*self.entry_button_gap
self.button_add_reform_x = self.block_2_entry_1_3_x + self.block_2_entry_entry_gap_x + 0.03
self.root_title=Label(text="Poland Tax Microsimulation Model",
font = self.fontStyle_title)
self.root_title.place(relx = self.title_pos_x, rely = self.title_pos_y, anchor = "n")
self.l1=Label(text="Data Inputs",
font = self.fontStyle_sub_title)
self.l1.place(relx = self.block_1_title_pos_x, rely = self.block_1_title_pos_y, anchor = "w")
self.entry_data_filename = Entry(width=30, font = self.fontStyle)
self.entry_data_filename.place(relx = self.block_1_entry_x,
rely = self.block_1_entry_1_y,
anchor = "e")
self.entry_data_filename.insert(END, self.data_filename)
self.button_data_filename = ttk.Button(text = "Change Data File", style='my.TButton', command=self.input_data_filename)
self.button_data_filename.place(relx = self.block_1_entry_x,
rely = self.block_1_entry_1_y, anchor = "w")
#button.place(x=140,y=50)
self.entry_weights_filename = Entry(width=30, font = self.fontStyle)
self.entry_weights_filename.place(relx = self.block_1_entry_x,
rely = self.block_1_entry_2_y, anchor = "e")
self.entry_weights_filename.insert(END, self.weights_filename)
self.button_weights_filename = ttk.Button(text = "Change Weights File", style='my.TButton', command=self.input_weights_filename)
self.button_weights_filename.place(relx = self.block_1_entry_x,
rely = self.block_1_entry_2_y, anchor = "w")
self.entry_policy_filename = Entry(width=30, font = self.fontStyle)
self.entry_policy_filename.place(relx = self.block_1_entry_x,
rely = self.block_1_entry_3_y, anchor = "e")
self.entry_policy_filename.insert(END, self.policy_filename)
self.button_policy_filename = ttk.Button(text = "Change Policy File", style='my.TButton', command=self.input_policy_filename)
self.button_policy_filename.place(relx = self.block_1_entry_x,
rely = self.block_1_entry_3_y, anchor = "w")
"""
self.entry_growfactors_filename = Entry(width=30, font = self.fontStyle)
self.entry_growfactors_filename.place(relx = self.block_1_entry_x,
rely = self.block_1_entry_4_y, anchor = "e")
self.entry_growfactors_filename.insert(END, self.policy_filename)
self.button_growfactors_filename = ttk.Button(text = "Change Growfactors File", style='my.TButton', command=self.input_growfactors_filename)
self.button_growfactors_filename.place(relx = self.block_1_entry_x,
rely = self.block_1_entry_4_y, anchor = "w")
"""
self.l1A=Label(text="Current Law",
font = self.fontStyle_sub_title)
self.l1A.place(relx = self.block_1_title_pos_x, rely = self.block_1A_title_pos_y, anchor = "w")
self.button_generate_revenue_curr_law = ttk.Button(text = "Generate Current Law Total Revenues", style='my.TButton', command=self.generate_total_revenues)
self.button_generate_revenue_curr_law.place(relx = self.button_1_pos_x,
rely = self.button_1_pos_y, anchor = "w")
self.l2=Label(text="Reform", font = self.fontStyle_sub_title)
self.l2.place(relx = self.block_1_title_pos_x, rely = self.block_2_title_pos_y, anchor = "w")
self.l3=Label(text="Select Policy Parameter: ", font = self.fontStyle)
self.l3.place(relx = self.block_2_entry_1_1_x,
rely = self.block_2_entry_1_1_y-self.text_entry_gap, anchor = "w")
self.current_law_policy, self.policy_options_list = self.policy_options()
self.policy_options_list.remove('gst_rate')
self.block_widget_dict = {}
self.block_selected_dict = {}
self.num_reforms = 1
self.block_widget_dict[1] = {}
self.block_selected_dict[1] = {}
self.block_widget_dict[1][1] = ttk.Combobox(value=self.policy_options_list, font=self.text_font, name=str(self.num_reforms))
self.block_widget_dict[1][1].current(1)
self.block_widget_dict[1][1].place(relx = self.block_2_entry_1_1_x,
rely = self.block_2_entry_1_1_y, anchor = "w", width=300)
self.block_widget_dict[1][1].bind("<<ComboboxSelected>>", self.show_policy_selection)
self.l4=Label(text="Year: ", font = self.fontStyle)
self.l4.place(relx = self.block_2_entry_1_2_x,
rely = self.block_2_entry_1_1_y-self.text_entry_gap, anchor = "w")
self.block_widget_dict[1][2] = Entry(width=6, font = self.fontStyle)
self.block_widget_dict[1][2].place(relx = self.block_2_entry_1_2_x, rely = self.block_2_entry_1_1_y, anchor = "w")
self.l5=Label(text="Value: ", font = self.fontStyle)
self.l5.place(relx = self.block_2_entry_1_3_x,
rely = self.block_2_entry_1_1_y-self.text_entry_gap, anchor = "w")
self.block_widget_dict[1][3] = Entry(width=10, font = self.fontStyle)
self.block_widget_dict[1][3].place(relx = self.block_2_entry_1_3_x, rely = self.block_2_entry_1_1_y, anchor = "w")
self.num_reforms += 1
self.button_add_reform = ttk.Button(text="+", style='my.TButton', command=self.create_policy_widgets, width=2)
self.button_add_reform.place(relx = self.button_add_reform_x, rely = self.block_2_entry_1_1_y, anchor = "w")
self.button_generate_revenue_policy = ttk.Button(text = "Generate Revenue under Reform", style='my.TButton', command=self.apply_policy_change)
self.button_2_pos_y = (self.block_2_entry_1_1_y+(self.num_reforms-1)*(self.entry_entry_gap_y)) +self.entry_button_gap
self.button_generate_revenue_policy.place(relx = self.button_1_pos_x,
rely = self.button_2_pos_y, anchor = "w")
self.l3A=Label(text="Tax Expenditures",
font = self.fontStyle_sub_title)
self.l3A.place(relx = self.block_3_title_pos_x, rely = self.block_3_title_pos_y, anchor = "w")
self.entry_benchmark_filename = Entry(width=30, font = self.fontStyle)
self.entry_benchmark_filename.place(relx = self.block_3_entry_x,
rely = self.block_3_entry_y,
anchor = "e")
self.entry_benchmark_filename.insert(END, self.benchmark_filename)
self.button_benchmark_filename = ttk.Button(text = "Change Benchmark File", style='my.TButton', command=self.input_benchmark_filename)
self.button_benchmark_filename.place(relx = self.block_3_entry_x,
rely = self.block_3_entry_y, anchor = "w")
self.button_generate_tax_expenditures = ttk.Button(text = "Generate Tax Expenditures", style='my.TButton', command=self.generate_tax_expenditures)
self.button_generate_tax_expenditures.place(relx = self.button_3_pos_x,
rely = self.button_3_pos_y, anchor = "w")
self.image = ImageTk.PhotoImage(Image.open("blank.png"))
#image = tk.PhotoImage(file="blank.png")
self.pic = tk.Label(image=self.image)
self.pic.place(relx = 0.5, rely = 0.2, anchor = "nw")
self.pic.image = self.image
def Add_Reform(self, event=None):
self.num_reforms.set(self.num_reforms.get() + 1)
def createWidgets(self):
#self.cloneButton = Button ( self, text='Clone', command=self.clone)
#self.cloneButton.grid()
return
def create_policy_widgets(self):
self.block_widget_dict[self.num_reforms] = {}
self.block_selected_dict[self.num_reforms] = {}
self.block_widget_dict[self.num_reforms][1] = ttk.Combobox(value=self.policy_options_list, font=self.text_font, name=str(self.num_reforms))
self.block_widget_dict[self.num_reforms][1].current(1)
self.block_widget_dict[self.num_reforms][1].place(relx = self.block_2_entry_1_1_x,
rely = (self.block_2_entry_1_1_y+
(self.num_reforms-1)*(self.entry_entry_gap_y)), anchor = "w", width=300)
self.block_widget_dict[self.num_reforms][1].bind("<<ComboboxSelected>>", self.show_policy_selection)
self.block_widget_dict[self.num_reforms][2] = Entry(width=6, font=self.fontStyle)
self.block_widget_dict[self.num_reforms][2].place(relx = self.block_2_entry_1_2_x,
rely = (self.block_2_entry_1_1_y+
(self.num_reforms-1)*(self.entry_entry_gap_y)), anchor = "w")
self.block_widget_dict[self.num_reforms][3] = Entry(width=14, font=self.fontStyle)
self.block_widget_dict[self.num_reforms][3].place(relx = self.block_2_entry_1_3_x,
rely = (self.block_2_entry_1_1_y+
(self.num_reforms-1)*(self.entry_entry_gap_y)), anchor = "w")
self.num_reforms += 1
self.button_2_pos_y = (self.block_2_entry_1_1_y+(self.num_reforms-1)*(self.entry_entry_gap_y))+self.entry_button_gap
self.button_generate_revenue_policy.place(relx = self.button_1_pos_x,
rely = self.button_2_pos_y, anchor = "w")
def popup_window(self):
window = tk.Toplevel()
label = tk.Label(window, text="Hello World!")
label.pack(fill='x', padx=50, pady=5)
button_close = tk.Button(window, text="Close", style='my.TButton', command=window.destroy)
button_close.pack(fill='x')
def popup_showinfo(self):
showinfo("ShowInfo", "Hello World!")
def generate_total_revenues(self):
#global total_revenue_text1
#global reform_revenue_text1
#global selected_year
self.selected_year=2019
# create Records object containing pit.csv and pit_weights.csv input data
recs = Records()
grecs = GSTRecords()
#print(data_filename)
#print(weights_filename)
# create CorpRecords object using cross-section data
crecs1 = CorpRecords(data=self.data_filename, weights=self.weights_filename)
# Note: weights argument is optional
assert isinstance(crecs1, CorpRecords)
assert crecs1.current_year == 2017
# create Policy object containing current-law policy
#policy_filename = 'current_law_policy_poland.json'
pol = Policy(DEFAULTS_FILENAME=self.policy_filename)
# specify Calculator objects for current-law policy
calc1 = Calculator(policy=pol, records=recs, corprecords=crecs1,
gstrecords=grecs, verbose=False)
# NOTE: calc1 now contains a PRIVATE COPY of pol and a PRIVATE COPY of recs,
# so we can continue to use pol and recs in this script without any
# concern about side effects from Calculator method calls on calc1.
assert isinstance(calc1, Calculator)
assert calc1.current_year == 2017
np.seterr(divide='ignore', invalid='ignore')
# popup window for the Results
window = tk.Toplevel()
window.geometry("600x500+140+140")
label = tk.Label(window, text="Results")
label.place(relx = 0.50, rely = 0.05)
self.s = ttk.Style()
self.s.configure('my.TButton', font=self.fontStyle)
button_close = ttk.Button(window, text="Close", style='my.TButton', command=window.destroy)
button_close.place(relx = 0.50, rely = 0.90)
calc1.advance_to_year(self.selected_year)
# Produce DataFrame of results using cross-section
calc1.calc_all()
dump_vars = ['CIT_ID_NO', 'legal_form', 'sector', 'province', 'small_business', 'revenue', 'expenditure', 'income', 'tax_base_before_deductions', 'deductions_from_tax_base',
'income_tax_base_after_deductions', 'citax']
dumpdf_1 = calc1.dataframe_cit(dump_vars)
dumpdf_1.to_csv('app00_poland1.csv', index=False, float_format='%.0f')
Business_Profit1 = calc1.carray('income')
Tax_Free_Incomes1 = calc1.carray('tax_free_income_total')
Tax_Base_Before_Deductions1 = calc1.carray('tax_base_before_deductions')
Deductions1 = calc1.carray('deductions_from_tax_base')
Tax_Base_After_Deductions1 = calc1.carray('income_tax_base_after_deductions')
citax1 = calc1.carray('citax')
weight1 = calc1.carray('weight')
etr1 = np.divide(citax1, Business_Profit1)
weighted_etr1 = etr1*weight1.values
weighted_etr_overall1 = (sum(weighted_etr1[~np.isnan(weighted_etr1)])/
sum(weight1.values[~np.isnan(weighted_etr1)]))
wtd_citax1 = citax1 * weight1
citax_collection1 = wtd_citax1.sum()
citax_collection_billions1 = citax_collection1/10**9
citax_collection_str1 = '{0:.2f}'.format(citax_collection_billions1)
print('\n\n\n')
print('TAX COLLECTION FOR THE YEAR - 2017\n')
print("The CIT Collection in billions is: ", citax_collection_billions1)
total_revenue_text=""
total_revenue_text = "TAX COLLECTION FOR THE YEAR - " + str(self.selected_year)+" : "+str(citax_collection_str1)+" bill Zlotys"
revenue_label = Label(window, text=total_revenue_text, font=self.fontStyle)
revenue_label.place(relx = 0.05, rely = 0.20, anchor = "w")
#revenue_label.config(window, text=total_revenue_text)
df_sector = dumpdf_1.groupby(['sector']).sum()
df_sector['citax_millions'] = df_sector['citax']/14**6
df_province = dumpdf_1.groupby(['province']).sum()
df_province['citax_millions'] = df_province['citax']/14**6
df_small_business = dumpdf_1.groupby(['small_business']).sum()
df_small_business['citax_millions'] = df_small_business['citax']/14**6
cmap = plt.cm.tab10
colors = cmap(np.arange(len(df_sector)) % cmap.N)
ax = df_sector.plot(kind='bar', use_index=True, y='citax_millions',
legend=False, rot=90,
figsize=(8,8), color=colors)
ax.set_ylabel('CIT in million Zlotys')
ax.set_xlabel('')
ax.set_title(' CIT collection by sector (2017)', fontweight="bold")
pic_filename1 = 'CIT Collection 2017.png'
plt.savefig(pic_filename1)
"""
img2 = ImageTk.PhotoImage(Image.open(pic_filename1))
pic.configure(image=img2)
pic.image = img2
"""
# Show Image
img1 = Image.open(pic_filename1)
img2 = img1.resize((500, 500), Image.ANTIALIAS)
img3 = ImageTk.PhotoImage(img2)
self.pic.configure(image=img3)
self.pic.image = img3
"""
plt.show()
cmap = plt.cm.tab14
colors = cmap(np.arange(len(df_province)) % cmap.N)
ax = df_province.plot(kind='bar', use_index=True, y='citax_millions',
legend=False, rot=90,
figsize=(8,8), color=colors)
ax.set_ylabel('CIT in million Zlotys')
ax.set_xlabel('')
ax.set_title(' CIT collection by Province (2017)', fontweight="bold")
plt.show()
cmap = plt.cm.tab14
colors = cmap(np.arange(len(df_province)) % cmap.N)
ax = df_small_business.plot(kind='bar', use_index=True, y='citax_millions',
legend=False, rot=90,
figsize=(8,8), color=colors)
ax.set_ylabel('CIT in million Zlotys')
ax.set_xlabel('')
ax.set_title(' CIT collection by Type of Business (2017)', fontweight="bold")
plt.show()
"""
def read_reform_dict(self, block_selected_dict):
years=[]
for k in block_selected_dict.keys():
if (block_selected_dict[k]['selected_year'] not in years):
years = years + [block_selected_dict[k]['selected_year']]
ref = {}
ref['policy']={}
for year in years:
policy_dict = {}
for k in block_selected_dict.keys():
if block_selected_dict[k]['selected_year']==year:
policy_dict['_'+block_selected_dict[k]['selected_item']]=[float(block_selected_dict[k]['selected_value'])]
ref['policy'][int(year)] = policy_dict
years.sort()
years = [int(x) for x in years]
return years, ref
def apply_policy_change(self):
for num in range(1, self.num_reforms):
self.block_selected_dict[num]['selected_item']= self.block_widget_dict[num][1].get()
self.block_selected_dict[num]['selected_value']= self.block_widget_dict[num][3].get()
self.block_selected_dict[num]['selected_year']= self.block_widget_dict[num][2].get()
#print(self.block_selected_dict)
# create Records object containing pit.csv and pit_weights.csv input data
recs = Records()
grecs = GSTRecords()
print(self.data_filename)
print(self.weights_filename)
# create CorpRecords object using cross-section data
crecs1 = CorpRecords(data=self.data_filename, weights=self.weights_filename)
# Note: weights argument is optional
assert isinstance(crecs1, CorpRecords)
assert crecs1.current_year == 2017
# create Policy object containing current-law policy
pol = Policy()
# specify Calculator objects for current-law policy
calc1 = Calculator(policy=pol, records=recs, corprecords=crecs1,
gstrecords=grecs, verbose=False)
assert isinstance(calc1, Calculator)
assert calc1.current_year == 2017
np.seterr(divide='ignore', invalid='ignore')
pol2 = Policy()
years, self.reform=self.read_reform_dict(self.block_selected_dict)
print(self.reform)
#reform = Calculator.read_json_param_objects('app01_reform.json', None)
pol2.implement_reform(self.reform['policy'])
calc2 = Calculator(policy=pol2, records=recs, corprecords=crecs1,
gstrecords=grecs, verbose=False)
# popup window for the Results
window = tk.Toplevel()
window.geometry("600x500+140+140")
label = tk.Label(window, text="Results", font=self.fontStyle)
label.place(relx = 0.05, rely = 0.14)
self.s = ttk.Style()
self.s.configure('my.TButton', font=self.fontStyle)
button_close = ttk.Button(window, text="Close", style='my.TButton', command=window.destroy)
button_close.place(relx = 0.50, rely = 0.90)
total_revenue_text={}
reform_revenue_text={}
revenue_dict={}
revenue_amount_dict = {}
num = 1
#for year in range(years[0], years[-1]+1):
for year in range(2019, 2024):
calc1.advance_to_year(year)
calc2.advance_to_year(year)
# NOTE: calc1 now contains a PRIVATE COPY of pol and a PRIVATE COPY of recs,
# so we can continue to use pol and recs in this script without any
# concern about side effects from Calculator method calls on calc1.
# Produce DataFrame of results using cross-section
calc1.calc_all()
dump_vars = ['CIT_ID_NO', 'legal_form', 'sector', 'province', 'small_business', 'revenue', 'expenditure', 'income', 'tax_base_before_deductions', 'deductions_from_tax_base',
'income_tax_base_after_deductions', 'citax']
dumpdf_1 = calc1.dataframe_cit(dump_vars)
dumpdf_1.to_csv('app00_poland1.csv', index=False, float_format='%.0f')
Business_Profit1 = calc1.carray('income')
Tax_Free_Incomes1 = calc1.carray('tax_free_income_total')
Tax_Base_Before_Deductions1 = calc1.carray('tax_base_before_deductions')
Deductions1 = calc1.carray('deductions_from_tax_base')
Tax_Base_After_Deductions1 = calc1.carray('income_tax_base_after_deductions')
citax1 = calc1.carray('citax')
weight1 = calc1.carray('weight')
etr1 = np.divide(citax1, Business_Profit1)
weighted_etr1 = etr1*weight1.values
weighted_etr_overall1 = (sum(weighted_etr1[~np.isnan(weighted_etr1)])/
sum(weight1.values[~np.isnan(weighted_etr1)]))
wtd_citax1 = citax1 * weight1
citax_collection1 = wtd_citax1.sum()
citax_collection_billions1 = citax_collection1/10**9
citax_collection_str1 = '{0:.2f}'.format(citax_collection_billions1)
print('\n\n\n')
print('TAX COLLECTION FOR THE YEAR - 2017\n')
print("The CIT Collection in billions is: ", citax_collection_billions1)
total_revenue_text[year] = "TAX COLLECTION UNDER CURRENT LAW FOR THE YEAR - " + str(year)+" : "+str(citax_collection_str1)+" bill Zlotys"
#self.l6.config(text=total_revenue_text1)
#self.l6.place(relx = 0.1, rely = 0.7+(num-1)*0.1, anchor = "w")
# Produce DataFrame of results using cross-section
calc2.calc_all()
dump_vars = ['CIT_ID_NO', 'legal_form', 'sector', 'province', 'small_business', 'revenue', 'expenditure', 'income', 'tax_base_before_deductions', 'deductions_from_tax_base',
'income_tax_base_after_deductions', 'citax']
dumpdf_2 = calc2.dataframe_cit(dump_vars)
dumpdf_2.to_csv('app00_poland2.csv', index=False, float_format='%.0f')
Business_Profit2 = calc2.carray('income')
Tax_Free_Incomes2 = calc2.carray('tax_free_income_total')
Tax_Base_Before_Deductions2 = calc2.carray('tax_base_before_deductions')
Deductions2 = calc2.carray('deductions_from_tax_base')
Tax_Base_After_Deductions2 = calc2.carray('income_tax_base_after_deductions')
citax2 = calc2.carray('citax')
weight2 = calc2.carray('weight')
etr2 = np.divide(citax2, Business_Profit2)
weighted_etr2 = etr2*weight2.values
weighted_etr_overall2 = (sum(weighted_etr2[~np.isnan(weighted_etr2)])/
sum(weight2.values[~np.isnan(weighted_etr2)]))
wtd_citax2 = citax2 * weight2
citax_collection2 = wtd_citax2.sum()
citax_collection_billions2 = citax_collection2/10**9
citax_collection_str2 = '{0:.2f}'.format(citax_collection_billions2)
print('\n\n\n')
print('TAX COLLECTION FOR THE YEAR UNDER REFORM - 2017\n')
print("The CIT Collection in billions is: ", citax_collection_billions2)
reform_revenue_text[year] = "TAX COLLECTION UNDER REFORM FOR THE YEAR - " + str(year)+" : "+str(citax_collection_str2)+" bill Zlotys"
revenue_dict[year]={}
revenue_amount_dict[year]={}
revenue_dict[year]['current_law']={}
revenue_amount_dict[year]['current_law']={}
revenue_dict[year]['current_law']['Label'] = Label(window, text=total_revenue_text[year], font=self.fontStyle)
revenue_dict[year]['current_law']['Label'].place(relx = 0.05, rely = 0.1+(num-1)*0.15, anchor = "w")
revenue_amount_dict[year]['current_law']['amount'] = citax_collection_str1
#self.l6=Label(text=self.total_revenue_text1)
#self.l6.place(relx = 0.4, rely = 0.1, anchor = "w")
revenue_dict[year]['reform']={}
revenue_amount_dict[year]['reform']={}
revenue_dict[year]['reform']['Label'] = Label(window, text=reform_revenue_text[year], font=self.fontStyle)
revenue_dict[year]['reform']['Label'].place(relx = 0.05, rely = 0.15+(num-1)*0.15, anchor = "w")
revenue_amount_dict[year]['reform']['amount'] = citax_collection_str2
#self.l7=Label(text=self.reform_revenue_text1)
#self.l7.place(relx = 0.4, rely = 0.15, anchor = "w")
num += 1
#print(revenue_amount_dict)
df_revenue_proj = pd.DataFrame(revenue_amount_dict)
df_revenue_proj = df_revenue_proj.T
df_revenue_proj['Current Law'] = df_revenue_proj['current_law'].apply(pd.Series)
df_revenue_proj['Reform'] = df_revenue_proj['reform'].apply(pd.Series)
df_revenue_proj = df_revenue_proj.drop(['current_law', 'reform'], axis=1)
df_revenue_proj['Current Law'] = pd.to_numeric(df_revenue_proj['Current Law'])
df_revenue_proj['Reform'] = pd.to_numeric(df_revenue_proj['Reform'])
print(df_revenue_proj)
ax = df_revenue_proj.plot(y=["Current Law", "Reform"], kind="bar", rot=0,
figsize=(8,8))
ax.set_ylabel('(billion Zlotys)')
ax.set_xlabel('')
ax.set_title('CIT Revenue - Current Law vs. Reforms', fontweight="bold")
pic_filename2 = 'CIT - Current Law and Reforms.png'
plt.savefig(pic_filename2)
img1 = Image.open(pic_filename2)
img2 = img1.resize((500, 500), Image.ANTIALIAS)
img3 = ImageTk.PhotoImage(img2)
self.pic.configure(image=img3)
self.pic.image = img3
def generate_tax_expenditures(self):
# create Records object containing pit.csv and pit_weights.csv input data
recs = Records()
grecs = GSTRecords()
crecs1 = CorpRecords(data=self.data_filename, weights=self.weights_filename)
# Note: weights argument is optional
assert isinstance(crecs1, CorpRecords)
assert crecs1.current_year == 2017
# create Policy object containing current-law policy
pol = Policy()
# specify Calculator objects for current-law policy
calc1 = Calculator(policy=pol, records=recs, corprecords=crecs1,
gstrecords=grecs, verbose=False)
assert isinstance(calc1, Calculator)
assert calc1.current_year == 2017
np.seterr(divide='ignore', invalid='ignore')
# Produce DataFrame of results using cross-section
calc1.calc_all()
#sector=calc1.carray('sector')
weight = calc1.carray('weight')
dump_vars = ['CIT_ID_NO', 'legal_form', 'sector', 'province', 'small_business',
'revenue', 'expenditure', 'income', 'tax_base_before_deductions',
'deductions_from_tax_base',
'income_tax_base_after_deductions', 'citax']
dumpdf = calc1.dataframe_cit(dump_vars)
#create the weight variable
dumpdf['weight']= weight
dumpdf= dumpdf.rename(columns={'citax':"tax_collected_under_current_policy"})
dumpdf['weighted_tax_collected_under_current_policy']= dumpdf['weight']*dumpdf['tax_collected_under_current_policy']
dumpdf['ID_NO']= "A"+ dumpdf['CIT_ID_NO'].astype('str')
benchmark = Calculator.read_json_param_objects(self.benchmark_filename, None)
base_year = list(benchmark['policy'].keys())[0]
#reform = dict(benchmark)
reform = copy.deepcopy(benchmark)
with open('taxcalc/'+self.policy_filename) as f:
current_law_policy = json.load(f)
ref_dict = benchmark['policy']
var_list = []
tax_expediture_list = []
tax_expediture_list_polish = []
for pkey, sdict in ref_dict.items():
for k, s in sdict.items():
reform.pop("policy")
mydict={}
mydict[k]=s
mydict0={}
mydict0[pkey]=mydict
reform['policy']=mydict0
pol2 = Policy()
pol2.implement_reform(reform['policy'])
calc2 = Calculator(policy=pol2, records=recs, corprecords=crecs1,
gstrecords=grecs, verbose=False)
calc2.calc_all()
weight2 = calc2.carray('weight')
dump_vars = ['CIT_ID_NO', 'citax']
dumpdf_2 = calc2.dataframe_cit(dump_vars)
dumpdf_2['weight']= weight2
dumpdf_2['ID_NO']= "A"+ dumpdf_2['CIT_ID_NO'].astype('int').astype('str')
dumpdf_2 = dumpdf_2.rename(columns={'citax':"tax_collected_under_benchmark"+ k})
dumpdf_2['weighted_tax_collected_under_benchmark'+ k]= dumpdf_2['weight']*dumpdf_2['tax_collected_under_benchmark'+ k]
dumpdf = pd.merge(dumpdf, dumpdf_2, how="inner", on="ID_NO")
#calculating expenditure
dumpdf['tax_expenditure_'+current_law_policy[k]['description']]= (dumpdf["weighted_tax_collected_under_benchmark"+ k]- dumpdf['weighted_tax_collected_under_current_policy'])/10**6
dumpdf['tax_expenditure_'+current_law_policy[k]['long_name']]= (dumpdf["weighted_tax_collected_under_benchmark"+ k]- dumpdf['weighted_tax_collected_under_current_policy'])/10**6
var_list = var_list + [k]
tax_expediture_list = tax_expediture_list + ['tax_expenditure_'+current_law_policy[k]['description']]
tax_expediture_list_polish = tax_expediture_list_polish + ['tax_expenditure_'+current_law_policy[k]['long_name']]
#Summarize here
tax_expenditure_df = dumpdf[tax_expediture_list].sum(axis = 0)
tax_expenditure_df= tax_expenditure_df.reset_index()
tax_expenditure_df.columns = ['Tax Expenditure', 'Million Zlotys']
tax_expenditure_df.to_csv('tax_expenditures_sum.csv',index=False, float_format='%.0f')
print(tax_expenditure_df)
tax_expenditure_df = dumpdf[tax_expediture_list_polish].sum(axis = 0)
tax_expenditure_df= tax_expenditure_df.reset_index()
tax_expenditure_df.columns = ['Wydatki Podatkowe', 'Milion Zlotys']
tax_expenditure_df.to_csv('tax_expenditures_sum_polish.csv', encoding='utf-8', index=False, float_format='%.0f')
tax_expenditure_df.to_csv('tax_expenditures_sum_polish.txt', encoding='utf-8', sep=',', index=False)
# This is the Overall Tax Expenditures
pol3 = Policy()
reform = Calculator.read_json_param_objects(self.benchmark_filename, None)
pol3.implement_reform(reform['policy'])
calc2 = Calculator(policy=pol3, records=recs, corprecords=crecs1,
gstrecords=grecs, verbose=False)
# popup window for the Results
window = tk.Toplevel()
window.geometry("700x600+140+140")
label = tk.Label(window, text="Tax Expenditures", font=self.fontStyle_sub_title)
label.place(relx = 0.40, rely = 0.02)
self.s = ttk.Style()
self.s.configure('my.TButton', font=self.fontStyle)
button_close = ttk.Button(window, text="Close", style='my.TButton', command=window.destroy)
button_close.place(relx = 0.50, rely = 0.90)
total_revenue_text={}
reform_revenue_text={}
tax_expenditure_text = {}
revenue_dict={}
revenue_amount_dict = {}
tax_expenditure = {}
num = 1
#for year in range(years[0], years[-1]+1):
for year in range(2019, 2024):
calc1.advance_to_year(year)
calc2.advance_to_year(year)
# NOTE: calc1 now contains a PRIVATE COPY of pol and a PRIVATE COPY of recs,
# so we can continue to use pol and recs in this script without any
# concern about side effects from Calculator method calls on calc1.
# Produce DataFrame of results using cross-section
calc1.calc_all()
dump_vars = ['CIT_ID_NO', 'legal_form', 'sector', 'province', 'small_business', 'revenue', 'expenditure', 'income', 'tax_base_before_deductions', 'deductions_from_tax_base',
'income_tax_base_after_deductions', 'citax']
dumpdf_1 = calc1.dataframe_cit(dump_vars)
dumpdf_1.to_csv('app00_poland1.csv', index=False, float_format='%.0f')
Business_Profit1 = calc1.carray('income')
Tax_Free_Incomes1 = calc1.carray('tax_free_income_total')
Tax_Base_Before_Deductions1 = calc1.carray('tax_base_before_deductions')
Deductions1 = calc1.carray('deductions_from_tax_base')
Tax_Base_After_Deductions1 = calc1.carray('income_tax_base_after_deductions')
citax1 = calc1.carray('citax')
weight1 = calc1.carray('weight')
etr1 = np.divide(citax1, Business_Profit1)
weighted_etr1 = etr1*weight1.values
weighted_etr_overall1 = (sum(weighted_etr1[~np.isnan(weighted_etr1)])/
sum(weight1.values[~np.isnan(weighted_etr1)]))
wtd_citax1 = citax1 * weight1
citax_collection1 = wtd_citax1.sum()
citax_collection_billions1 = citax_collection1/10**9
citax_collection_str1 = '{0:.2f}'.format(citax_collection_billions1)
print('\n\n\n')
print('TAX COLLECTION UNDER CURRENT LAW FOR THE YEAR - '+str(year)+': ', citax_collection_billions1)
total_revenue_text[year] = "TAX COLLECTION UNDER CURRENT LAW FOR THE YEAR - " + str(year)+" : "+str(citax_collection_str1)+" bill Zlotys"
#self.l6.config(text=total_revenue_text1)
#self.l6.place(relx = 0.1, rely = 0.7+(num-1)*0.1, anchor = "w")
# Produce DataFrame of results using cross-section
calc2.calc_all()
dump_vars = ['CIT_ID_NO', 'legal_form', 'sector', 'province', 'small_business', 'revenue', 'expenditure', 'income', 'tax_base_before_deductions', 'deductions_from_tax_base',
'income_tax_base_after_deductions', 'citax']
dumpdf_2 = calc2.dataframe_cit(dump_vars)
dumpdf_2.to_csv('app00_poland2.csv', index=False, float_format='%.0f')
Business_Profit2 = calc2.carray('income')
Tax_Free_Incomes2 = calc2.carray('tax_free_income_total')
Tax_Base_Before_Deductions2 = calc2.carray('tax_base_before_deductions')
Deductions2 = calc2.carray('deductions_from_tax_base')
Tax_Base_After_Deductions2 = calc2.carray('income_tax_base_after_deductions')
citax2 = calc2.carray('citax')
weight2 = calc2.carray('weight')
etr2 = np.divide(citax2, Business_Profit2)
weighted_etr2 = etr2*weight2.values
weighted_etr_overall2 = (sum(weighted_etr2[~np.isnan(weighted_etr2)])/
sum(weight2.values[~np.isnan(weighted_etr2)]))
wtd_citax2 = citax2 * weight2
citax_collection2 = wtd_citax2.sum()
citax_collection_billions2 = citax_collection2/10**9
citax_expenditure_billions = citax_collection_billions2 - citax_collection_billions1
citax_collection_str2 = '{0:.2f}'.format(citax_collection_billions2)
citax_expenditure_billions_str = '{0:.5f}'.format(citax_expenditure_billions)
print('\n\n\n')
print('TAX COLLECTION UNDER BENCHMARK POLICY FOR THE YEAR - '+str(year)+': ', citax_collection_billions2)
revenue_amount_dict[year]={}
revenue_amount_dict[year]['current_law']={}
revenue_amount_dict[year]['current_law']['amount'] = citax_collection_billions1
revenue_amount_dict[year]['benchmark']={}
revenue_amount_dict[year]['benchmark']={}
revenue_amount_dict[year]['benchmark']['amount'] = citax_collection_billions2
revenue_amount_dict[year]['tax_expenditure']={}
revenue_amount_dict[year]['tax_expenditure']={}
revenue_amount_dict[year]['tax_expenditure']['amount'] = citax_expenditure_billions
reform_revenue_text[year] = "TAX COLLECTION UNDER BENCHMARK FOR THE YEAR - " + str(year)+" : "+str(citax_collection_str2)+" bill Zlotys"
tax_expenditure_text[year] = "TAX EXPENDITURES FOR THE YEAR - " + str(year)+" : "+citax_expenditure_billions_str+" bill Zlotys"
revenue_dict[year]={}
revenue_dict[year]['current_law'] = {}
revenue_dict[year]['current_law']['Label'] = Label(window, text=total_revenue_text[year], font=self.fontStyle)
revenue_dict[year]['current_law']['Label'].place(relx = 0.05, rely = 0.1+(num-1)*0.2, anchor = "w")
revenue_dict[year]['benchmark'] = {}
revenue_dict[year]['benchmark']['Label'] = Label(window, text=reform_revenue_text[year], font=self.fontStyle)
revenue_dict[year]['benchmark']['Label'].place(relx = 0.05, rely = 0.13+(num-1)*0.2, anchor = "w")
revenue_dict[year]['tax_expenditure'] = {}
revenue_dict[year]['tax_expenditure']['Label'] = Label(window, text=tax_expenditure_text[year], font=self.fontStyle)
revenue_dict[year]['tax_expenditure']['Label'].place(relx = 0.05, rely = 0.16+(num-1)*0.2, anchor = "w")
num += 1
#print(revenue_amount_dict)
df_revenue_proj = pd.DataFrame(revenue_amount_dict)
df_revenue_proj = df_revenue_proj.T
df_revenue_proj['Current Law'] = df_revenue_proj['current_law'].apply(pd.Series)
df_revenue_proj['Benchmark'] = df_revenue_proj['benchmark'].apply(pd.Series)
df_revenue_proj = df_revenue_proj.drop(['current_law', 'benchmark'], axis=1)
df_revenue_proj['Current Law'] = pd.to_numeric(df_revenue_proj['Current Law'])
df_revenue_proj['Benchmark'] = pd.to_numeric(df_revenue_proj['Benchmark'])
print(df_revenue_proj)
ax = df_revenue_proj.plot(y=["Current Law", "Benchmark"], kind="bar", rot=0,
figsize=(8,8))
ax.set_ylabel('(billion Zlotys)')
ax.set_xlabel('')
ax.set_title('CIT - Tax Collection under Current Law vs. Benchmark', fontweight="bold")
pic_filename3 = 'CIT - Current Law and Benchmark.png'
plt.savefig(pic_filename3)
img1 = Image.open(pic_filename3)
img2 = img1.resize((500, 500), Image.ANTIALIAS)
img3 = ImageTk.PhotoImage(img2)
self.pic.configure(image=img3)
self.pic.image = img3
def newselection1(self, event):
print('selected1:', event.widget.get())
def newselection2(self, event):
print('selected2:', event.widget.get())
def input_data_filename(self):
filez = filedialog.askopenfilenames(title='Choose a file')
#self.master.update()
#filename_path = tk.splitlist(filez)[0]
filename_path = filez[0]
filename_list = filename_path.split('/')
self.data_filename = filename_list[-1]
self.entry_data_filename.delete(0,END)
self.entry_data_filename.insert(0,self.data_filename)
def input_weights_filename(self):
filez = filedialog.askopenfilenames(title='Choose a file')
#print(filez[0])
#self.master.update()
#filename_path = tk.splitlist(filez)[0]
#filename_list = filename_path.split('/')
filename_path = filez[0]
filename_list = filename_path.split('/')
self.weights_filename = filename_list[-1]
self.entry_weights_filename.delete(0,END)
self.entry_weights_filename.insert(0,self.weights_filename)
def input_policy_filename(self):
filez = filedialog.askopenfilenames(title='Choose a file')
#self.master.update()
#filename_path = tk.splitlist(filez)[0]
filename_path = filez[0]
filename_list = filename_path.split('/')
self.policy_filename = filename_list[-1]
self.entry_policy_filename.delete(0,END)
self.entry_policy_filename.insert(0,self.policy_filename)
def input_benchmark_filename(self):
filez = filedialog.askopenfilenames(title='Choose a file')
#self.master.update()
#filename_path = tk.splitlist(filez)[0]
filename_path = filez[0]
filename_list = filename_path.split('/')
self.benchmark_filename = filename_list[-1]
self.entry_benchmark_filename.delete(0,END)
self.entry_benchmark_filename.insert(0,self.benchmark_filename)
def policy_options(self):
self.sub_directory
self.policy_filename
with open(self.sub_directory+'/'+self.policy_filename) as f:
current_law_policy = json.load(f)
current_law_policy_sorted = dict(sorted(current_law_policy.items()))
policy_options_list = []
for k, s in current_law_policy_sorted.items():
#print(k)
#print(current_law_policy[k]['description'])
#policy_option_list = policy_option_list + [current_law_policy[k]['description']]
policy_options_list = policy_options_list + [k[1:]]
return (current_law_policy, policy_options_list)
def policy_reform():
self.reform={}
self.reform['policy']={}
self.reform['policy']['_'+self.selected_item]={}
self.updated_year = self.block_widget_dict[1][2].get()
self.updated_value = self.block_widget_dict[1][3].get()
self.reform['policy']['_'+selected_item][self.updated_year]=[self.updated_value]
print(self.reform)
def show_policy_selection(self, event):
active_widget_number = int(str(event.widget)[1:])
print("active_widget_number: ", active_widget_number)
num = active_widget_number
#for num in range(1, self.num_reforms):
self.selected_item = self.block_widget_dict[num][1].get()
self.selected_value = self.current_law_policy['_'+ self.selected_item]['value'][0]
self.selected_year = self.current_law_policy['_'+ self.selected_item]['row_label'][0]
self.block_selected_dict[num]['selected_item']= self.block_widget_dict[num][1].get()
self.block_selected_dict[num]['selected_value']= self.current_law_policy['_'+ self.selected_item]['value'][0]
self.block_selected_dict[num]['selected_year']= self.current_law_policy['_'+ self.selected_item]['row_label'][0]
self.block_widget_dict[num][3].delete(0, END)
self.block_widget_dict[num][3].insert(END, self.selected_value)
self.block_widget_dict[num][2].delete(0, END)
self.block_widget_dict[num][2].insert(END, self.selected_year)
for num in range(1, self.num_reforms):
self.block_selected_dict[num]['selected_value']= self.block_widget_dict[num][3].get()
self.block_selected_dict[num]['selected_year']= self.block_widget_dict[num][2].get()
#print(self.block_selected_dict)
return
# --- main ---
def main():
root = tk.Tk()
root.geometry('1000x600')
app = Application(root)
app.mainloop()
if __name__ == '__main__':
main()
#main()
"""
#button(row=6, column=1, sticky = W, pady = (0,25), padx = (0,0))
root.mainloop()
if __name__ == "__main__":
app = Application()
app.master.title("Sample application")
app.mainloop()
"""