def test_aggregated_LCIA_single_method_augment_on_fly(data_for_testing): projects.set_current(data_for_testing['project']) assert "techno_UP" in databases assert "biosphere" in databases assert "techno_agg_LCIA" not in databases assert data_for_testing['m1_name'] in methods DatabaseAggregator(up_db_name="techno_UP", agg_db_name="techno_agg_LCIA", database_type='LCIA', method_list=[data_for_testing['m1_name']], biosphere='biosphere', overwrite=False).generate() assert "techno_agg_LCIA" in databases assert len(Database("techno_agg_LCIA")) == len(Database("techno_UP")) lca_unit_process = LCA({("techno_UP", "A"): 1}, method=data_for_testing['m1_name']) lca_unit_process.lci() lca_unit_process.lcia() lca_LCIA = LCA({("techno_agg_LCIA", "A"): 1}, method=data_for_testing['m1_name']) lca_LCIA.lci() lca_LCIA.lcia() assert lca_unit_process.score == lca_LCIA.score
def dispatch_LCIA_calc_to_workers(base_dir, project_name, database_name, cpus, method_shortlist_name): projects.set_current(project_name) results_folder = os.path.join(base_dir, database_name, 'results') if method_shortlist_name is not None: method_short_list_fp = os.path.join(results_folder, 'reference_files', method_shortlist_name+'.pickle') assert os.path.isfile(method_short_list_fp), "Couldn't read the specified method_shortlist_name {}. Aborting".format(method_short_list_fp) method_list = pickle.load(open(method_short_list_fp, 'rb')) print("Calculating LCIA score arrays for the following categories:") print(method_list) else: method_list = list(methods) print("Calculating LCIA score arrays for all {} impact categories".format(len(method_list))) method_sublists = chunks(method_list, ceil(len(method_list)/cpus)) with open(os.path.join(results_folder, 'reference_files', 'bio_dict.pickle'), 'rb') as f: ref_bio_dict = pickle.load(f) workers = [] for m in method_sublists: j = mp.Process(target=whole_method_LCIA_calculator, args=(m, results_folder, ref_bio_dict ) ) workers.append(j) for w in workers: w.start() w.join()
def test_add_unit_score_exchange_and_cf(data_for_testing): """ Augment methods and biosphere database for unit scores""" # Make sure project received with expected initial data (possibly delete # since this does not test the function, but rather the testing itself) projects.set_current(data_for_testing['project']) assert "biosphere" in databases method_name = data_for_testing['m1_name'] assert method_name in methods assert len(Database('biosphere')) == 2 loaded_biosphere_before = Database('biosphere').load() method = Method(method_name) loaded_method_before = method.load() assert len(loaded_method_before) == 2 ef_code = method.get_abbreviation() assert ('biosphere', ef_code) not in loaded_biosphere_before # Augment method and biosphere database add_unit_score_exchange_and_cf(method=method_name, biosphere='biosphere') assert len(Database('biosphere')) == 3 loaded_biosphere_after = Database('biosphere').load() assert ('biosphere', ef_code) in loaded_biosphere_after new_ef = get_activity(('biosphere', ef_code)) assert new_ef['name'] == 'Unit impact for {}'.format(method_name) method = Method(method_name) loaded_method_after = method.load() assert len(loaded_method_after) == 3 assert (('biosphere', ef_code), 1) in loaded_method_after
def test_add_unit_score_exchange_and_cf_act_exists(data_for_testing): projects.set_current(data_for_testing['project']) method_name = data_for_testing['m1_name'] assert len(Database('biosphere')) == 2 loaded_biosphere_before = Database('biosphere').load() method1_name = data_for_testing['m1_name'] method1 = Method(method1_name) loaded_method1_before = method1.load() assert len(loaded_method1_before) == 2 ef1_code = method1.get_abbreviation() assert ('biosphere', ef1_code) not in loaded_biosphere_before # Manually add ef to biosphere biosphere_data = Database('biosphere').load() biosphere_data[("biosphere", Method(method_name).get_abbreviation())] = { 'name': 'Unit impact for {}'.format(method_name), 'type': 'unit exchange', 'unit': Method(method_name).metadata['unit'] } Database('biosphere').write(biosphere_data) assert len(Database('biosphere')) == 3 # run function, should not change the length of biosphere, but should add # cf to method add_unit_score_exchange_and_cf(method_name, biosphere='biosphere') loaded_method1_after = method1.load() assert len(loaded_method1_after) == 3 assert len(Database('biosphere')) == 3
def test_add_unit_score_exchange_and_cf_no_such_biosphere(data_for_testing): projects.set_current(data_for_testing['project']) method_name = data_for_testing['m1_name'] with pytest.raises( ValueError, match="Database biosphereXX not in registered databases"): add_unit_score_exchange_and_cf(method_name, biosphere='biosphereXX')
def test_add_unit_score_exchange_and_cf_no_such_method(data_for_testing): projects.set_current(data_for_testing['project']) assert "biosphere" in databases method_name = ('some', 'fake', 'method') with pytest.raises(ValueError) as exc_info: add_unit_score_exchange_and_cf(method_name, biosphere='biosphere') assert exc_info.value.args[ 0] == "Method ('some', 'fake', 'method') not in registered methods"
def test_aggregated_LCIA_multiple_methods_already_augmented(data_for_testing): projects.set_current(data_for_testing['project']) assert "techno_UP" in databases assert "biosphere" in databases assert "techno_agg_LCIA" not in databases assert data_for_testing['m1_name'] in methods assert data_for_testing['m2_name'] in methods assert len(methods) == 2 add_all_unit_score_exchanges_and_cfs(biosphere='biosphere') agg_db = DatabaseAggregator( up_db_name="techno_UP", agg_db_name="techno_agg_LCIA", database_type='LCIA', method_list=[data_for_testing['m1_name'], data_for_testing['m2_name']], biosphere='biosphere', overwrite=False).generate() assert "techno_agg_LCIA" in databases assert len(Database("techno_agg_LCIA")) == len(Database("techno_UP")) lca_unit_process = LCA({("techno_UP", "A"): 1}, method=data_for_testing['m1_name']) lca_unit_process.lci() lca_unit_process.lcia() lca_LCIA = LCA({("techno_agg_LCIA", "A"): 1}, method=data_for_testing['m1_name']) lca_LCIA.lci() lca_LCIA.lcia() assert lca_unit_process.score == lca_LCIA.score score_in_B = lca_LCIA.biosphere_matrix[lca_LCIA.biosphere_dict[( 'biosphere', Method(data_for_testing['m1_name']).get_abbreviation())], lca_LCIA.activity_dict[( "techno_agg_LCIA", "A")]] assert score_in_B == lca_LCIA.score lca_unit_process = LCA({("techno_UP", "A"): 1}, method=data_for_testing['m2_name']) lca_unit_process.lci() lca_unit_process.lcia() lca_LCIA = LCA({("techno_agg_LCIA", "A"): 1}, method=data_for_testing['m2_name']) lca_LCIA.lci() lca_LCIA.lcia() assert lca_unit_process.score == lca_LCIA.score score_in_B = lca_LCIA.biosphere_matrix[lca_LCIA.biosphere_dict[( 'biosphere', Method(data_for_testing['m2_name']).get_abbreviation())], lca_LCIA.activity_dict[( "techno_agg_LCIA", "A")]] assert score_in_B == lca_LCIA.score
def test_aggregated_LCIA_no_methods(data_for_testing): projects.set_current(data_for_testing['project']) assert "techno_UP" in databases assert "biosphere" in databases assert "techno_agg_LCIA" not in databases with pytest.raises( ValueError, match= 'Need to pass a list of method identifiers to create an LCIA score database, none passed' ): DatabaseAggregator(up_db_name="techno_UP", agg_db_name="techno_agg_LCIA", database_type='LCIA', method_list=[], biosphere='biosphere', overwrite=False)
def test_aggregated_bd_already_exists(data_for_testing, recwarn): projects.set_current(data_for_testing['project']) assert "techno_UP" in databases assert "biosphere" in databases assert "techno_agg_LCIA" not in databases Database('techno_agg_LCIA').register() agg_db = DatabaseAggregator( up_db_name="techno_UP", agg_db_name="techno_agg_LCIA", database_type='LCIA', method_list=[data_for_testing['m1_name'], data_for_testing['m2_name']], biosphere='biosphere', overwrite=False) w = recwarn[-1] assert str( w.message ) == "A database named techno_agg_LCIA already exists, set `overwrite` to True to overwrite"
def test_aggregated_invalid_db_type(data_for_testing): projects.set_current(data_for_testing['project']) assert "techno_UP" in databases assert "biosphere" in databases assert "techno_agg_LCIA" not in databases with pytest.raises( ValueError, match= 'other is not a valid database type, should be "LCI" or "LCIA"'): DatabaseAggregator(up_db_name="techno_UP", agg_db_name="techno_agg_LCIA", database_type='other', method_list=[ data_for_testing['m1_name'], data_for_testing['m2_name'] ], biosphere='biosphere', overwrite=False)
def concat_lcia_samples_arrays_from_method_tuple( result_dir, method, sb_id_list=None, totals_or_per_exchanges="totals", project_name=None, sim_name=None, dest=None, fail_if_samples_batches_different=False, ignore_missing=True): """ Concatenate and save LCIA sample arrays in result dir Parameters ------------ result_type : str Path to the directory where data used or generated by bw2preagg is saved. method : tuple LCIA method identification in brightway2 (tuple) sb_id_list : list List of samples_batch ids. If None, all samples_batch are processed. totals_or_per_exchanges : str, default="totals" Deal with totals or results per elementary flows project_name : str Name of the brightway2 project where the database is imported sim_name : str, default=None Name to give the directory in which results will be saved. If None, a default name is generated from sb_id_list dest : str Path to location where concatenated array directory will be saved fail_if_samples_batches_different : bool, default=False If False, will raise ValueError if arrays available in samples_batch folders are not the same If True, will concatenate only those arrays that are available in all samples_batch folders ignore_missing : bool, default=True If False, will concatenate arrays in LCI folder only if all expected LCI arrays are present """ if project_name: projects.set_current(_check_project(project_name)) method = _check_method(method) abbr = Method(method).get_abbreviation() concat_samples_arrays_in_result_type_dir( result_dir=result_dir, sb_id_list=sb_id_list, result_type_dirname=abbr, totals_or_per_exchanges=totals_or_per_exchanges, sim_name=sim_name, dest=dest, fail_if_samples_batches_different=fail_if_samples_batches_different, ignore_missing=ignore_missing)
def test_aggregated_LCI(data_for_testing): projects.set_current(data_for_testing['project']) assert "techno_UP" in databases assert "biosphere" in databases assert "techno_agg_LCI" not in databases assert data_for_testing['m1_name'] in methods agg_db = DatabaseAggregator(up_db_name="techno_UP", agg_db_name="techno_agg_LCI", database_type='LCI', method_list=[data_for_testing['m1_name']], biosphere='biosphere', overwrite=False).generate() assert "techno_agg_LCI" in databases assert len(Database("techno_agg_LCI")) == len(Database("techno_UP")) lca_unit_process = LCA({("techno_UP", "A"): 1}, method=data_for_testing['m1_name']) lca_unit_process.lci() lca_unit_process.lcia() lca_LCI = LCA({("techno_agg_LCI", "A"): 1}, method=data_for_testing['m1_name']) lca_LCI.lci() lca_LCI.lcia() for act, col in lca_LCI.activity_dict.items(): row = lca_LCI.product_dict[act] # Make sure production is 1 assert lca_LCI.technosphere_matrix[row, col] == 1.0 # Make sure other elements of the technosphere matrix are 0 #assert lca_LCI.technosphere_matrix.sum(axis=0)[col]==1 for ef, ef_row in lca_unit_process.biosphere_dict.items(): up_lci = lca_unit_process.inventory.sum(axis=1)[ef_row] LCI_lci = lca_LCI.biosphere_matrix[lca_LCI.biosphere_dict[ef], lca_LCI.activity_dict[( "techno_agg_LCI", "A")]] assert up_lci == LCI_lci assert lca_unit_process.score == lca_LCI.score