def confirm_setup(self): print("This is the last page") this_filter = create_filter_from_description(parse_filter_widget(self.filter_widget)) db = findMainWindow().loader.database.db this_item_set = [WurstProcess(x) for x in w.get_many(db, *this_filter)] #this_item = w.get_one(db, *this_filter) print(this_item_set) item_string = "" for n, this_item in enumerate(this_item_set): item_string += "{} ({}) [{}]".format(this_item['name'], this_item['unit'], this_item['location']) if n != len(this_item_set): item_string += "\n" self.processLabel.setText(item_string) if len(this_item_set) > 1: self.processDescriptionLabel.setText('Base processes: ') else: self.processDescriptionLabel.setText('Base process: ') location_list = ", ".join([x['display'] for x in self.location_widget.checked_items]) self.locationLabel.setText(location_list)
def find_markets(self, grid_locations=None, voltage='high'): elec_filter_base = [ {'filter': 'equals', 'args': ['unit', 'kilowatt hour']}, {'filter': 'startswith', 'args': ['name', 'market for electricity, {} voltage'.format(voltage)]}, {'filter': 'doesnt_contain_any', 'args': ['name', ['aluminium industry', 'internal use in coal mining', 'Swiss Federal Railways', 'label-certified', 'electricity, from municipal waste incineration' ]]}, ] if grid_locations: elec_filter_base += [{'filter': 'either', 'args': [{'filter': 'equals', 'args': ['location', x]} for x in grid_locations] }] elec_filter = create_filter_from_description(elec_filter_base) elec_list = list(w.get_many(self.loader.database.db, *elec_filter)) grid_dict = { e['location']: ecoinvent_to_image_locations(e['location'])[0] for e in elec_list if 'IAI' not in e['location'] and e['location'] not in ['GLO'] and len(ecoinvent_to_image_locations(e['location'])) == 1 } return elec_list, grid_dict
def classify_technologies(self, elec_list=None): if not elec_list: elec_list = self.markets assert elec_list exchange_filter = [{'filter': 'equals', 'args': ['unit', 'kilowatt hour']}, {'filter': 'exclude', 'args': [ {'filter': 'startswith', 'args': ['name', 'market ']}, ]}, ] wurst_exchange_filter = create_filter_from_description(exchange_filter) exchange_dict = defaultdict(list) for x in elec_list: exc_list = list(w.get_many(x['exchanges'], *wurst_exchange_filter)) for e in exc_list: e['image_location'] = get_loc(x['location'])[0] e['image_classification'] = classify_by_name(e['name']) assert not exchange_dict[x['location']] exchange_dict[x['location']] = exc_list return exchange_dict
def find_markets(self, grid_locations=None): elec_filter_base = [ {'filter': 'equals', 'args': ['unit', 'kilowatt hour']}, {'filter': 'startswith', 'args': ['name', 'market for electricity, high voltage']}, {'filter': 'doesnt_contain_any', 'args': ['name', ['Swiss Federal Railways', 'label-certified']]}, ] if grid_locations: elec_filter_base += [{'filter': 'either', 'args': [{'filter': 'equals', 'args': ['location', x]} for x in grid_locations] }] elec_filter = create_filter_from_description(elec_filter_base) elec_list = list(w.get_many(self.loader.database.db, *elec_filter)) grid_dict = { e['location']: get_loc(e['location'])[0] for e in elec_list if 'IAI' not in e['location'] and e['location'] not in ['GLO'] and len(get_loc(e['location'])) == 1 } return elec_list, grid_dict
def format(self, recipe_item): function = recipe_item['function'] original_kwargs = recipe_item.get('kwargs', {}) copy_kwargs = copy(original_kwargs) for k, v in copy_kwargs.items(): if "_filter" in k: print(k) this_filter = create_filter_from_description(v) these_items = list( w.get_many(self.loader.database.db, *this_filter)) string = '\n'.join([ '{name} ({unit}) [{location}]'.format(**x) for x in these_items ]) copy_kwargs[k] = string elif isinstance(v, list): print("{} is a list".format(v)) if k not in ['database', 'db']: copy_kwargs[k] = ', '.join(v) if function == 'transfer_pv': if 'factor' in copy_kwargs.keys(): copy_kwargs['amount'] = copy_kwargs['factor'] * 100 copy_kwargs['unit'] = "%" else: copy_kwargs['unit'] = "" mask = self.masks[function] return mask.format(**copy_kwargs)
def restrict_locations(self): base_filter = parse_filter_widget(self.filter_widget) no_location_filter = [x for x in base_filter if x['args'][0] != 'location'] this_filter = create_filter_from_description(base_filter) no_location = create_filter_from_description(no_location_filter) db = findMainWindow().loader.database.db this_item = w.get_one(db, *this_filter) item_location = this_item['location'] other_items = w.get_many(db, *no_location) other_locations = [x['location'] for x in other_items] other_locations = [x for x in other_locations if x != 'RoW'] locations = list(set(other_locations + [item_location])) print(locations) self.location_widget.find_and_disable(locations)
def electricity_filter(self, grid_locations=None, voltage='high'): elec_filter_base = [ {'filter': 'equals', 'args': ['unit', 'kilowatt hour']}, {'filter': 'startswith', 'args': ['name', 'market for electricity, {} voltage'.format(voltage)]}, {'filter': 'doesnt_contain_any', 'args': ['name', ['aluminium industry', 'internal use in coal mining', 'Swiss Federal Railways', 'label-certified', 'electricity, from municipal waste incineration' ]]}, ] if grid_locations: elec_filter_base += [{'filter': 'either', 'args': [{'filter': 'equals', 'args': ['location', x]} for x in grid_locations] }] return create_filter_from_description(elec_filter_base)
def test_relink(market, loader): market_filter_desc = [{ 'filter': 'equals', 'args': ['unit', 'kilowatt hour'] }, { 'filter': 'startswith', 'args': ['name', 'market'] }, { 'filter': 'contains', 'args': ['name', 'high'] }, { 'filter': 'equals', 'args': ['location', 'GB'] }] market_filter = create_filter_from_description(market_filter_desc) market_activity = w.get_one(loader.database.db, *market_filter) original_exchange_dict = { x['name']: x['amount'] for x in market_activity['exchanges'] } print(original_exchange_dict) market.relink() market_activity = w.get_one(loader.database.db, *market_filter) new_exchange_dict = { x['name']: x['amount'] for x in market_activity['exchanges'] } for k, v in original_exchange_dict.items(): if new_exchange_dict[k] != v: mismatch = True break assert mismatch
def market(loader): database = loader.database market_filter_desc = [{ 'filter': 'equals', 'args': ['unit', 'kilowatt hour'] }, { 'filter': 'startswith', 'args': ['name', 'market'] }, { 'filter': 'contains', 'args': ['name', 'high'] }, { 'filter': 'equals', 'args': ['location', 'GB'] }] market_filter = create_filter_from_description(market_filter_desc) market_activity = w.get_one(database.db, *market_filter) return FuturaMarket(market_activity, database)
def setup_market(self): force_market = [{'filter': 'startswith', 'args': ['name', 'market']}] base_filter = parse_filter_widget(self.filter_widget) full_filter = force_market + base_filter self.base_market_filter = full_filter this_filter = create_filter_from_description(full_filter) db = findMainWindow().loader.database.db self.base_market = w.get_one(db, *this_filter) self.market = FuturaMarket(self.base_market, findMainWindow().loader.database) self.dataframe = self.generate_dataframe() self.model = PandasModel(self.dataframe) self.tableView.setModel(self.model) self.tableView.selectionModel().selectionChanged.connect( self.table_selected) self.tableView.doubleClicked.connect(self.set_production) self.tableView.horizontalHeader().sectionClicked.connect( self.update_chart) self.update_chart()
def regionalisation_wizard(self): print('Starting the wizard') rw = RegionalisationWizard() if rw.exec_(): print('Wizard Complete') filter_description = parse_filter_widget(rw.filter_widget) print(filter_description) this_filter = create_filter_from_description(filter_description) db = findMainWindow().loader.database.db this_item_set = [ WurstProcess(x) for x in w.get_many(db, *this_filter) ] #this_item = w.get_one(db, *this_filter) #print(this_item) location_code_list = [ x['code'] for x in rw.location_widget.checked_items ] if len(this_item_set) == 1: recipe_entry = { 'action': 'regionalisation', 'tasks': [{ 'function': 'create_regional_activities_from_filter', 'kwargs': { 'new_regions': location_code_list, 'base_activity_filter': filter_description, } }] } else: recipe_entry = { 'action': 'regionalisation', 'tasks': [{ 'function': 'regionalise_multiple_processes', 'kwargs': { 'locations': location_code_list, 'base_activity_filter': filter_description, } }] } loader = findMainWindow().loader executor = FuturaRecipeExecutor(findMainWindow().loader) executor.execute_recipe_action(recipe_entry) loader.recipe['actions'].append(recipe_entry) signals.update_recipe.emit() # create_regional_activities(this_item, location_code_list, db) no_location_filter_description = [ x for x in filter_description if x['args'][0] != 'location' ] new_locations_filter_description = [{ 'filter': 'equals', 'args': ['location', l] } for l in location_code_list] new_locations_filter_description = [{ 'filter': 'either', 'args': new_locations_filter_description }] new_locations_filter_description = no_location_filter_description + new_locations_filter_description new_locations_filter = create_filter_from_description( new_locations_filter_description) result = list(w.get_many(db, *new_locations_filter)) print([ "{} ({}) [{}]".format(x['name'], x['unit'], x['location']) for x in result ]) message = QtWidgets.QMessageBox() message.setText("{} new processes created".format(len(result))) message.setInformativeText("\n".join([ "{} ({}) [{}]".format(x['name'], x['unit'], x['location']) for x in result ])) message.exec_() signals.reset_status_message.emit() else: print('Wizard Cancelled')
def test_regionalisation(loader): database = loader.database original_database_size = len(database.db) locations = ['GB'] base_activity_filter = [{ 'filter': 'equals', 'args': ['database', 'Carma CCS'] }, { 'filter': 'equals', 'args': ['unit', 'kilowatt hour'] }, { 'filter': 'either', 'args': [{ 'filter': 'contains', 'args': ['name', 'hard coal'] }, { 'filter': 'contains', 'args': ['name', 'Hard coal'] }] }, { 'filter': 'equals', 'args': ['location', 'GLO'] }] progress_message = 'coal CCS for GB' regionalise_multiple_processes(database, locations, base_activity_filter, progress_message) intermediate_database_size = len(database.db) assert intermediate_database_size > original_database_size gb_activity_filter = [{ 'filter': 'equals', 'args': ['database', 'Carma CCS'] }, { 'filter': 'equals', 'args': ['unit', 'kilowatt hour'] }, { 'filter': 'either', 'args': [{ 'filter': 'contains', 'args': ['name', 'hard coal'] }, { 'filter': 'contains', 'args': ['name', 'Hard coal'] }] }, { 'filter': 'equals', 'args': ['location', 'GB'] }] gb = create_filter_from_description(gb_activity_filter) gb_list = list(w.get_many(database.db, *gb)) assert gb_list locations = ['DE'] base_activity_filter = [{ 'filter': 'equals', 'args': ['database', 'Carma CCS'] }, { 'filter': 'equals', 'args': ['unit', 'kilowatt hour'] }, { 'filter': 'either', 'args': [{ 'filter': 'contains', 'args': ['name', 'lignite'] }, { 'filter': 'contains', 'args': ['name', 'Lignite'] }] }, { 'filter': 'equals', 'args': ['location', 'GLO'] }] progress_message = 'lignite CCS for DE' regionalise_multiple_processes(database, locations, base_activity_filter, progress_message) final_database_size = len(database.db) assert final_database_size > intermediate_database_size de_activity_filter = [{ 'filter': 'equals', 'args': ['database', 'Carma CCS'] }, { 'filter': 'equals', 'args': ['unit', 'kilowatt hour'] }, { 'filter': 'either', 'args': [{ 'filter': 'contains', 'args': ['name', 'lignite'] }, { 'filter': 'contains', 'args': ['name', 'Lignite'] }] }, { 'filter': 'equals', 'args': ['location', 'DE'] }] de = create_filter_from_description(de_activity_filter) de_list = list(w.get_many(database.db, *de)) assert de_list
def classify_exchanges(self, elec_list=None): if not elec_list: elec_list = self.markets assert elec_list exchange_filter = [{'filter': 'equals', 'args': ['unit', 'kilowatt hour']}, {'filter': 'exclude', 'args': [ {'filter': 'startswith', 'args': ['name', 'market ']}, ]}, ] wurst_exchange_filter = create_filter_from_description(exchange_filter) full_exchange_list = [] for x in elec_list: full_exchange_list.extend(list(w.get_many(x['exchanges'], *wurst_exchange_filter))) exchange_tuples = {(x['name'], x['location']) for x in full_exchange_list} # return exchange_names # assert 0 convert_dict = OrderedDict() convert_dict["import"] = "Imports" convert_dict.update( { 'coal, post': 'Coal CCS', 'coal, oxy': 'Coal CCS', 'coal, pre': 'Coal CCS', 'lignite, post': 'Coal CCS', 'lignite, pre': 'Coal CCS', 'lignite, oxy': 'Coal CCS', 'gas, post': 'Natural gas CCS', 'gas, pre': 'Natural gas CCS', 'gas, oxy': 'Natural gas CCS', 'wood burning power plant 20 MW, truck 25km, post': 'Biomass CCS', 'co-generation, biogas': 'Biomass CHP', 'co-generation, wood': 'Biomass CHP', 'biogas': 'Biomass ST', 'biogas': 'Biomass ST', 'wood': 'Biomass ST', 'co-generation, hard coal': 'Coal CHP', 'co-generation, lignite': 'Coal CHP', 'hard coal': 'Coal ST', 'lignite': 'Coal ST', 'hydro': 'Hydro', 'co-generation, natural gas': 'Natural gas CHP', 'natural gas, combined cycle': 'Natural gas CC', 'natural gas': 'Natural gas OC', 'nuclear': 'Nuclear', 'co-generation, oil': 'Oil CHP', 'oil': 'Oil ST', 'solar': 'Solar PV', 'offshore': 'Wind offshore', 'onshore': 'Wind onshore', 'peat': 'Other', 'blast furnace gas': 'Other', 'geothermal': 'Other', 'coal': 'Coal ST', 'diesel': 'Other', 'digester sludge': 'Other', 'CC plant': 'Other', 'BIGCC': 'IGCC', } ) # translate all exchanges to their IMAGE equivalents exchange_dict = {} for e in exchange_tuples: found = False for n in convert_dict.keys(): if n in e[0]: exchange_dict[e] = convert_dict[n] found = True break if not found: exchange_dict[e] = 'Non-Specified' return exchange_dict
def create_filter(self): description = parse_filter_widget(self) return create_filter_from_description(description)
def update_grid(self, grid_locations=None): if not grid_locations: grid_locations = self.all_locations if isinstance(grid_locations, str): grid_locations = [grid_locations] assert isinstance(grid_locations, list) #get electricity market(s) elec_filter_base = [{ 'filter': 'equals', 'args': ['unit', 'kilowatt hour'] }, { 'filter': 'startswith', 'args': ['name', 'market for electricity, high voltage'] }, { 'filter': 'doesnt_contain_any', 'args': ['name', ['Swiss Federal Railways', 'label-certified']] }, { 'filter': 'either', 'args': [{ 'filter': 'equals', 'args': ['location', x] } for x in grid_locations] }] elec_filter = create_filter_from_description(elec_filter_base) elec_list = list(w.get_many(self.loader.database.db, *elec_filter)) # get exchanges to categorise exchange_filter = [ { 'filter': 'equals', 'args': ['unit', 'kilowatt hour'] }, { 'filter': 'exclude', 'args': [ { 'filter': 'startswith', 'args': ['name', 'market '] }, ] }, ] wurst_exchange_filter = create_filter_from_description(exchange_filter) full_exchange_list = [] for x in elec_list: full_exchange_list.extend( list(w.get_many(x['exchanges'], *wurst_exchange_filter))) exchange_names = set([x['name'] for x in full_exchange_list]) convert_dict = OrderedDict() convert_dict["import"] = "Imports" convert_dict.update({ "coal": "Coal", "lignite": "Coal", "oil": "Oil", "natural gas": "Natural Gas", "wood": "Combustible Renewables", "biogas": "Combustible Renewables", "peat": "Other Combustibles", "blast furnace gas": "Other Combustibles", "nuclear": "Nuclear", "hydro": "Hydro", "wind": "Wind", "solar": "Solar", "geothermal": "Geothermal", }) # translate all exchanges to their IEA equivalents exchange_dict = {} for e in exchange_names: found = False for n in convert_dict.keys(): if n in e: exchange_dict[e] = convert_dict[n] found = True break if not found: exchange_dict[e] = 'Non-Specified' last_fm = None for market in elec_list: stratification_dict = self.aggregated_data.xs( market['location'][:2]).to_dict()['Latest Year Total'] fm = FuturaMarket(market, self.loader.database) # create a dataframe of market production volumes by exchange pv_df = pd.DataFrame([{ 'input': k, 'production volume': v['production volume'] } for k, v in fm.process_dict.items()]) # add a Group column to classify each exchange to an IEA type pv_df['Group'] = pv_df['input'].apply( lambda x: exchange_dict.get(x, None)) grand_total = pv_df['production volume'].sum() # figure out how to stratify the data based on the proportion of production within IEA groups stratification_data = {} for g, v in pv_df.groupby('Group'): this_total = v['production volume'].sum() stratification_data[g] = {} # print(v) for row_index, row in v.iterrows(): if this_total != 0: stratification_data[g][row[ 'input']] = row['production volume'] / this_total else: stratification_data[g][row['input']] = 0 # multiply these proportions by the actual new grid mix sections actual_stratification = { k: v * grand_total for k, v in stratification_dict.items() } final_dict = {} for k, v in stratification_data.items(): this_pv = actual_stratification[k] for x, n in v.items(): final_dict[x] = n * this_pv # apply the new numbers to the FuturaMarket for k, v in final_dict.items(): fm.set_pv(k, v) fm.relink() print('Updated grid mix for {}'.format( cc.convert(market['location'], to="short_name")))