validacion.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import logging
  2. import pandas as pd
  3. from common.data import APPDIRS
  4. logger = logging.getLogger('simsdt.validacion')
  5. def load_results(filename):
  6. flujos_mct = pd.read_pickle(
  7. f'{APPDIRS["DATA"]}/flujos_inter_{filename}.dat')
  8. flujos_paises = pd.read_pickle(
  9. f'{APPDIRS["DATA"]}/flujos_paises_{filename}.dat')
  10. return flujos_mct[1:2], flujos_paises[1:2]
  11. def validar_flujos(filename, mctp, mes):
  12. finter, fnetos = load_results(filename)
  13. # Para Guatemala
  14. GU = {'FLUJOS':
  15. {'EXP NS': 0,
  16. 'IMP NS': 0,
  17. 'EXP SN': 0,
  18. 'IMP SN': 0,
  19. 'POR NS': 0,
  20. 'POR SN': 0},
  21. 'CASO': 0}
  22. if fnetos['GUA'][1] > 0:
  23. GU['FLUJOS']['EXP SN'] = finter['GUAELS'][1] + finter['GUAHON'][1]
  24. GU['FLUJOS']['IMP SN'] = 0
  25. else:
  26. GU['FLUJOS']['EXP SN'] = 0
  27. GU['FLUJOS']['IMP SN'] = finter['GUAELS'][1] + finter['GUAHON'][1]
  28. # Para El Salvador
  29. ES = {'FLUJOS':
  30. {'EXP NS': 0,
  31. 'IMP NS': 0,
  32. 'EXP SN': 0,
  33. 'IMP SN': 0,
  34. 'POR NS': 0,
  35. 'POR SN': 0},
  36. 'CASO': 0}
  37. if finter['GUAELS'][1] > 0 and finter['ELSHON'][1] > 0:
  38. ES['FLUJOS']['IMP NS'] = fnetos['ES'][1]
  39. ES['FLUJOS']['POR NS'] = finter['ELSHON'][1]
  40. ES['CASO'] = 1
  41. elif finter['GUAELS'][1] > 0 and finter['ELSHON'][1] < 0:
  42. ES['FLUJOS']['IMP NS'] = -1*finter['GUAELS'][1]
  43. ES['FLUJOS']['IMP SN'] = finter['ELSHON'][1]
  44. ES['CASO'] = 2
  45. elif finter['GUAELS'][1] < 0 and finter['ELSHON'][1] < 0:
  46. ES['FLUJOS']['EXP SN'] = fnetos['ES'][1]
  47. ES['FLUJOS']['POR SN'] = -1*finter['ELSHON'][1]
  48. ES['CASO'] = 3
  49. # Para Honduras
  50. HO = {'FLUJOS':
  51. {'EXP NS': 0,
  52. 'IMP NS': 0,
  53. 'EXP SN': 0,
  54. 'IMP SN': 0,
  55. 'POR NS': 0,
  56. 'POR SN': 0},
  57. 'CASO': 0}
  58. if finter['GUAHON'][1] > 0 and finter['ELSHON'][1] > 0 and finter['HONNIC'][1] > 0:
  59. HO['FLUJOS']['IMP NS'] = fnetos['HON'][1]
  60. HO['FLUJOS']['POR NS'] = finter['HONNIC'][1]
  61. HO['CASO'] = 1
  62. elif finter['GUAHON'][1] > 0 and finter['ELSHON'][1] > 0 and finter['HONNIC'][1] < 0:
  63. HO['FLUJOS']['IMP NS'] = -1*(finter['GUAHON'][1]+finter['ELSHON'])
  64. HO['FLUJOS']['IMP SN'] = finter['HONNIC'][1]
  65. HO['CASO'] = 2
  66. elif finter['GUAHON'][1] < 0 and finter['ELSHON'][1] < 0 and finter['HONNIC'][1] < 0:
  67. HO['FLUJOS']['EXP SN'] = fnetos['HON'][1]
  68. HO['FLUJOS']['POR SN'] = -1*finter['HONNIC'][1]
  69. HO['CASO'] = 3
  70. # Para NICARAGUA
  71. NI = {'FLUJOS':
  72. {'EXP NS': 0,
  73. 'IMP NS': 0,
  74. 'EXP SN': 0,
  75. 'IMP SN': 0,
  76. 'POR NS': 0,
  77. 'POR SN': 0},
  78. 'CASO': 0}
  79. if finter['HONNIC'][1] > 0 and finter['NICCRI'][1] > 0:
  80. NI['FLUJOS']['IMP NS'] = fnetos['NIC'][1]
  81. NI['FLUJOS']['POR NS'] = finter['NICRI'][1]
  82. NI['CASO'] = 1
  83. elif finter['HONNIC'][1] > 0 and finter['NICCRI'][1] < 0:
  84. NI['FLUJOS']['IMP NS'] = -1*finter['HONNIC'][1]
  85. NI['FLUJOS']['IMP SN'] = finter['NICCRI'][1]
  86. NI['CASO'] = 2
  87. elif finter['HONNIC'][1] < 0 and finter['NICCRI'][1] < 0:
  88. NI['FLUJOS']['EXP SN'] = fnetos['NIC'][1]
  89. NI['FLUJOS']['POR SN'] = -1*finter['NICCRI'][1]
  90. NI['CASO'] = 3
  91. # Para COSTA RICA
  92. CR = {'FLUJOS':
  93. {'EXP NS': 0,
  94. 'IMP NS': 0,
  95. 'EXP SN': 0,
  96. 'IMP SN': 0,
  97. 'POR NS': 0,
  98. 'POR SN': 0},
  99. 'CASO': 0}
  100. if finter['NICCRI'][1] > 0 and finter['CRIPAN'][1] >= 0:
  101. CR['FLUJOS']['IMP NS'] = fnetos['CR'][1]
  102. CR['FLUJOS']['POR NS'] = finter['CRIPAN'][1]
  103. CR['CASO'] = 1
  104. elif finter['NICCRI'][1] > 0 and finter['CRIPAN'][1] < 0:
  105. CR['FLUJOS']['IMP NS'] = -1*finter['NICCRI'][1]
  106. CR['FLUJOS']['IMP SN'] = finter['CRIPAN'][1]
  107. CR['CASO'] = 2
  108. elif finter['NICCRI'][1] < 0 and finter['CRIPAN'][1] < 0:
  109. CR['FLUJOS']['EXP SN'] = fnetos['CR'][1]
  110. CR['FLUJOS']['POR SN'] = -1*finter['CRIPAN'][1]
  111. CR['CASO'] = 3
  112. # Para Panama
  113. PA = {'FLUJOS':
  114. {'EXP NS': 0,
  115. 'IMP NS': 0,
  116. 'EXP SN': 0,
  117. 'IMP SN': 0,
  118. 'POR NS': 0,
  119. 'POR SN': 0},
  120. 'CASO': 0}
  121. if fnetos['PAN'][1] >= 0:
  122. PA['FLUJOS']['EXP SN'] = -1*finter['CRIPAN'][1]
  123. PA['FLUJOS']['IMP SN'] = 0
  124. else:
  125. PA['FLUJOS']['EXP SN'] = 0
  126. PA['FLUJOS']['IMP SN'] = -1*finter['CRIPAN'][1]
  127. res = {
  128. 'GU': GU,
  129. 'ES': ES,
  130. 'HO': HO,
  131. 'NI': NI,
  132. 'CR': CR,
  133. 'PA': PA
  134. }
  135. for (k, v) in res.items():
  136. for (kf, w) in v.items():
  137. if kf == 'FLUJOS':
  138. for (f, mw) in w.items():
  139. if round(abs(mw), 2) > mctp.loc[(1, k)][f]:
  140. if v['CASO'] == 1:
  141. logger.error(
  142. f"La restriccion de importacion total {k}debe cambiar a {mctp.loc[mes, k][f]}")
  143. elif v['CASO'] == 2:
  144. logger.error(
  145. f"La restriccion de mct ns {k} debe cambiar a {mctp.loc[mes, k][f]}")
  146. if v['CASO'] == 3:
  147. logger.error(
  148. f"La restriccion de exportacion total {k} debe cambiar a {mctp.loc[mes, k][f]}")
  149. else:
  150. pass
  151. # mctp = pd.read_excel('subasta_A2101_enero.xlsx', 'mctp', index_col=[0, 1])