@@ -41,7 +41,7 @@ def make_counterfactuals(data, scen_no, scen_file, labels):
4141 An object contaning a mofified IO system
4242 """
4343 # set basic data and variables
44-
44+ print ( f"Scenario { scen_no } started" )
4545 data = deepcopy (data )
4646
4747 x_ = ops .IOT .x (data .Z , data .Y )
@@ -65,13 +65,15 @@ def make_counterfactuals(data, scen_no, scen_file, labels):
6565 inv_diag_x_int = np .diag (ops .inv (ops .IOT .x (data .Z , data .Y )))
6666
6767 A = ops .IOT .A (data .Z , inv_diag_x_int )
68+
6869 data .A = counterfactual (scen_file , scen_no , A , "A" , labels )
6970
7071 data .Y = counterfactual (scen_file , scen_no , data .Y , "Y" , labels )
7172
7273 L = ops .IOT .L (data .A )
7374
7475 x_new = ops .IOT .x_IAy (L , data .Y .sum (1 ))
76+
7577 diag_x_new = np .diag (x_new )
7678
7779 diag_yj_new = np .diag (data .Y .sum (axis = 0 ))
@@ -107,13 +109,13 @@ def make_counterfactuals(data, scen_no, scen_file, labels):
107109 data .E = counterfactual (scen_file , scen_no , data .E , "E" , labels )
108110 data .R = counterfactual (scen_file , scen_no , data .R , "R" , labels )
109111 data .M = counterfactual (scen_file , scen_no , data .M , "M" , labels )
110-
111112 # Apply policy to final demand extension coefficient matrices
112113 data .EY = counterfactual (scen_file , scen_no , data .EY , "EY" , labels )
113114 data .RY = counterfactual (scen_file , scen_no , data .RY , "RY" , labels )
114115 data .MY = counterfactual (scen_file , scen_no , data .MY , "MY" , labels )
115116
116- # print((1-np.sum(x_)/np.sum(x_new))*100)
117+ #print((1-np.sum(x_)/np.sum(x_new))*100)
118+ print (f"Scenario { scen_no } completed" )
117119
118120 return data
119121
@@ -147,8 +149,7 @@ def counterfactual(scen_file, scen_no, M, M_name, labels):
147149 elif scen_no .startswith ("scenario_" ):
148150 pass
149151 else :
150- raise KeyError ("only integer or explicit name (scenario_x)" +
151- "are allowed" )
152+ raise KeyError ("only integer or explicit name (scenario_x) are allowed" )
152153
153154
154155 scenario = pd .read_excel (scen_file , sheet_name = scen_no , header = 1 , index = None )
@@ -339,7 +340,6 @@ def counterfactual_engine(M, inter_sets, subs=False, copy=False):
339340
340341 int4 = inter_sets ["at2" ]
341342 int4 = basic_add (int3 , int4 )
342-
343343 M [np .ix_ (i , g )] = int4
344344
345345 if subs is True :
@@ -422,7 +422,7 @@ def make_new(filtered_changes, M, M_name, labels):
422422 try :
423423 change_type = entry .change_type
424424 ide = entry .identifier # used during debugging
425-
425+
426426 # Collecting the specified coordinates for the intevention
427427 # coordinates for region and category
428428 # Row items (i) => Supplied category or extension category
@@ -433,11 +433,14 @@ def make_new(filtered_changes, M, M_name, labels):
433433 cat_d = sing_pos (entry .cat_d , column_labels )
434434 # Identify coordinates
435435 orig_coor = coord (cat_o , reg_o , no_reg_labs , no_row_labs )
436+ #print(f"row\n ide: {ide}, row: {entry.reg_o}, {entry.cat_o}, {orig_coor}")
436437 dest_coor = coord (cat_d , reg_d , no_reg_labs , no_col_labs )
438+ #print(f"columns\n ide: {ide}, column: {entry.reg_d}, {entry.cat_d}, {dest_coor}")
439+
437440 # organize main changes
438441 kt1 = {"kt" : entry .kt1 , "kp" : entry .kp1 }
439442 kt2 = {"kt" : entry .kt2 , "kp" : entry .kp2 }
440-
443+
441444 intervention = {"change_type" : change_type ,
442445 "ide" : ide ,
443446 "i" : orig_coor ,
@@ -447,32 +450,32 @@ def make_new(filtered_changes, M, M_name, labels):
447450 "at1" : entry .at1 ,
448451 "at2" : entry .at2 ,
449452 }
450-
453+
451454 substitution = False
452455 copy = False
453-
456+
454457 # the following is only relevant for susbtitution
455458 if "x" in [entry .Sub , entry .Copy ]:
456-
459+
457460 sub_reg_o = sing_pos (entry .reg_o_sc , reg_labels )
458461 sub_cat_o = sing_pos (entry .cat_o_sc , row_labels )
459-
462+
460463 # Column items => Consumption / manufacturing activity
461464 sub_reg_d = sing_pos (entry .reg_d_sc , reg_labels )
462465 sub_cat_d = sing_pos (entry .cat_d_sc , column_labels )
463-
466+
464467 # Translate coordinates from str to numerical position
465468 sub_orig_coor = coord (sub_cat_o , sub_reg_o , no_reg_labs , no_row_labs )
466469 sub_dest_coor = coord (sub_cat_d , sub_reg_d , no_reg_labs , no_col_labs )
467-
470+
468471 intervention ["swk" ] = entry .swk
469472 intervention ["i1" ] = sub_orig_coor
470473 intervention ["g1" ] = sub_dest_coor
471474 intervention ["sk1" ] = entry .sk1
472475 intervention ["sk2" ] = entry .sk2
473476 intervention ["sk3" ] = entry .sk3
474477 intervention ["sk4" ] = entry .sk4
475-
478+
476479 if entry .Copy == "x" :
477480 copy = True
478481 elif entry .Sub == "x" :
@@ -481,112 +484,5 @@ def make_new(filtered_changes, M, M_name, labels):
481484 raise ValueError (f"Check in this entry for potential coordinate errors in your scenario settings:\n { entry } " )
482485
483486 M = counterfactual_engine (M , intervention , substitution , copy )
484-
487+
485488 return M
486-
487- # =============================================================================
488- # =============================================================================
489- # # Here I put work that I started but I still need to finish
490- # =============================================================================
491- # =============================================================================
492- # =============================================================================
493- #
494- #
495- # def make_counterfactuals_SUT(data, scen_no, scen_file, labels):
496- # """
497- # Calculate all the counterfactual SUT matrices
498- #
499- # Parameters
500- # ----------
501- # data : obj
502- # An object containing all necessary matrices of the SUT system
503- #
504- # scen_no : int
505- # the identification number of the scenario to reference in scen_file
506- #
507- # scen_file : str
508- # the directory where the scenarios.xlsx file is store
509- #
510- # labels : obj
511- # an object containing all labels for the SUT matrices
512- #
513- # Outputs
514- # -------
515- # An object contaning a mofified SUT system
516- # """
517- #
518- # met = ops.PxP_ITA_MSC
519- #
520- # w = ops.IOT.B(data.W, data.inv_diag_g) # Primary input coef
521- # e = ops.IOT.B(data.E, data.inv_diag_g) # emissions extension coef
522- # r = ops.IOT.B(data.R, data.inv_diag_g) # Resources extension coef
523- # m = ops.IOT.B(data.M, data.inv_diag_g) # Materials extension coef
524- # S = met.S(data.U, data.inv_diag_g) # industry coefficients for intermediate use table
525- #
526- # # Start first from a supply approach
527- # # Supply matrix counterfactual
528- # data.V = counterfactual(scen_file, scen_no, data.V, "V", labels)
529- # # new total industry output
530- # g1 = np.sum(data.V, axis=0)
531- # # industry use coefficients counterfactual
532- # S_ = counterfactual(scen_file, scen_no, S, "S", labels)
533- #
534- # data.U = counterfactual(scen_file, scen_no, S_ @ np.diag(g1), "U", labels) # industry use transactions counterfactual
535- #
536- # W_ = np.array(ops.IOT.R(w, np.diag(g1)))
537- #
538- # g2 = np.array(W_[:9].sum(0)) + data.U.sum(0) # recalculate total industry output
539- #
540- # g_dif = np.multiply(g2, ops.inv(g1))*100 # calculate the difference between original and new total industry input
541- #
542- # # print([round((1-l)*100,4) for l in g_dif if 1-l>.5e-3 and l!=0])
543- # q2 = np.sum(data.U, axis=1) + np.sum(data.Y, axis=1)
544- #
545- # # updating the supply table to match the new total industry input
546- # D = met.D(data.V, np.diag(ops.inv(data.V.sum(1))))
547- # data.V = D @ np.diag(q2)
548- #
549- # q1 = np.sum(data.V, axis=0) # total product output
550- #
551- # q_dif = np.multiply(q2, ops.inv(q1))
552- #
553- # g1 = np.sum(data.V, axis=1)
554- #
555- # data.E = met.R(e, np.diag(x))
556- #
557- # data.R = met.R(r, np.diag(x))
558- #
559- # data.M = met.R(m, np.diag(x))
560- #
561- #
562- # return(IOT)
563- # def balancing_operation(V, U, Y, W):
564- # """
565- # Re-balancing of supply-use tables after data changes
566- #
567- # Parameters
568- # ----------
569- # V (supply) : numpy.array
570- #
571- # U (use) : numpy.array
572- #
573- # Y (final_demand) : numpy.array
574- #
575- # W (primary_inputs) : numpy.array
576- #
577- # Output
578- # ------
579- # output : dict
580- #
581- # It outputs a dictionary containing a re-balanced supply-use tables system
582- # where:
583- # V = supply table
584- #
585- # U = use table
586- #
587- # Y = final demand
588- #
589- # W = primary inputs
590- #
591- # """
592- # =============================================================================
0 commit comments