def calculate_EEIO_model(model, year, location, demandtype, perspective, include_data_quality): """ Calculates an EEIO model result :param model: A useeopy Model object :param year: int, year of demand :param location: string, location acronym of demand :param demandtype: string, either 'production' or 'consumption' :param perspective: string, either 'DIRECT' or 'INDIRECT' :return: A useeiopy Result object """ result = {} demandfile = model.path + model.name + "_FinalDemand.csv" demandcolumnname = str(year) + '_' + location + "_" + demandtype demand = dd.demandtodict(demandcolumnname, demandfile) if (perspective == 'FINAL'): p = calc.FINAL_PERSPECTIVE r = calc.calculate(model.model, demand, p) result['LCI_f'] = r.lci_contributions.transpose() result['LCIA_f'] = r.lcia_contributions.transpose() elif (perspective == 'DIRECT'): p = calc.DIRECT_PERSPECTIVE r = calc.calculate(model.model, demand, p) result['LCI_d'] = r.lci_contributions.transpose() result['LCIA_d'] = r.lcia_contributions.transpose() else: log.error('Perspective must be DIRECT or FINAL.') new_result = Result(result, model, location, demandtype, perspective, year, include_data_quality) return new_result
def test_final_perspective(self): r = calc.calculate(self.model, self.demand, calc.FINAL_PERSPECTIVE) self._check_totals(r) flows = [self.co2, self.so2, self.water] expected = { '1/electricity/us': [0, 0, 0], '2/steel parts/us': [0, 0, 0], '3/car assembly/us': [1.111111111, 0.065843621, 1.522633745] } c = r.lcia_contributions for sector, values in expected.items(): for i in range(0, len(flows)): self.assertAlmostEqual(c.loc[flows[i], sector], values[i])
def test_direct_perspective(self): r = calc.calculate(self.model, self.demand, calc.DIRECT_PERSPECTIVE) self._check_totals(r) flows = [self.co2, self.so2, self.water] expected = { '1/electricity/us': [0.617283951, 0.041152263, 1.028806584], '2/steel parts/us': [0.49382716, 0.024691358, 0.49382716], '3/car assembly/us': [0, 0, 0] } c = r.lcia_contributions for sector, values in expected.items(): for i in range(0, len(flows)): self.assertAlmostEqual(c.loc[flows[i], sector], values[i])
def calculate(full_model: model.Model, demand: dict, perspective=calc.DIRECT_PERSPECTIVE) -> calc.Result: """ Calculates a result for the given model and demand. :param full_model: The input-output model that should be calculated. :param demand: A dictionary containing the demand values of input-output sectors :param perspective: The perspective of the contribution results. See the documentation for more information about the different result perspectives. """ return calc.calculate(full_model, demand, perspective)
def test_intermediate_perspective(self): r = calc.calculate(self.model, self.demand, calc.INTERMEDIATE_PERSPECTIVE) self._check_totals(r) flows = [self.co2, self.so2, self.water] expected = { '1/electricity/us': [0.685871056, 0.045724737, 1.143118427], '2/steel parts/us': [0.823045267, 0.045724737, 1.005944216], '3/car assembly/us': [1.234567901, 0.073159579, 1.691815272] } c = r.lcia_contributions for sector, values in expected.items(): for i in range(0, len(flows)): self.assertAlmostEqual(c.loc[flows[i], sector], values[i])
def calculate(model, year, location, demandtype, perspective, modelname, modelpath): demandfile = modelpath + modelname + "_FinalDemand.csv" demandcolumnname = str(year) + '_' + location + "_" + demandtype demand = dd.demandtodict(demandcolumnname, demandfile) if (perspective == 'FINAL'): p = calc.FINAL_PERSPECTIVE else: p = calc.DIRECT_PERSPECTIVE result = calc.calculate(model, demand, p) lciacontributions = result.lcia_contributions.transpose() lciaflowcontributions = result.lcia_flow_contributions.transpose() resultsfolder = modelpath + "results/" if os.path.exists(resultsfolder) is False: os.mkdir(resultsfolder) lciacontributions.to_csv(resultsfolder + modelname + "_" + str(year) + "_" + location + "_" + demandtype + "_" + perspective + "_" + "lciacontributions.csv") lciaflowcontributions.to_csv(resultsfolder + modelname + "_" + str(year) + "_" + location + "_" + demandtype + "_" + perspective + "_" + "lciaflowcontributions.csv")