<commit_before>from bw2io.package import BW2Package
from lcopt.model import LcoptModel, unnormalise_unit
from copy import deepcopy
from collections import OrderedDict
from warnings import warn


def validate_imported_model(model):
    db = model.database['items']
    ecoinvent_name = model.ecoinventName
    ecoinvent_items = [x['items'] for x in model.external_databases if x['name'] == ecoinvent_name][0]
    ecoinvent_links = []

    for key, item in db.items():
        if item.get('ext_link'):
            if item['ext_link'][0] == ecoinvent_name:
                ecoinvent_links.append(item['ext_link'])

    for link in ecoinvent_links:
        if not ecoinvent_items.get(link):
            warn("{} not found in ecoinvent 3.3 cutoff database".format(link))
            return False

    return True   


def create_LcoptModel_from_BW2Package(import_filename):
    
    import_data = BW2Package.load_file(import_filename)
    orig_db = import_data[0]['data']
    db_name = import_data[0]['name']
    model = LcoptModel(db_name)
    db = deepcopy(orig_db)

    temp_param_set = []

    for k, v in db.items():
        exchanges = []

        for e in v['exchanges']:

            exc_name = e.pop('name')
            exc_input = e.pop('input')
            exc_unit = unnormalise_unit(e.pop('unit'))
            exc_amount = e.pop('amount')
            exc_type = e.pop('type')

            temp_param_set.append({'from': exc_name, 'to': v['name'], 'amount': exc_amount})

            if e.get('location'):
                e.pop('location')

            if exc_type == 'production':
                this_exc = {
                    'name': exc_name,
                    'type': exc_type,
                    'unit': exc_unit,
                    'lcopt_type': 'intermediate',
                }
                this_exc = {**this_exc, **e}<commit_msg>normalise parameters to 1 unit output when production amount != 1<commit_after>from bw2io.package import BW2Package
from lcopt.model import LcoptModel, unnormalise_unit
from copy import deepcopy
from collections import OrderedDict
from warnings import warn


def validate_imported_model(model):
    db = model.database['items']
    ecoinvent_name = model.ecoinventName
    ecoinvent_items = [x['items'] for x in model.external_databases if x['name'] == ecoinvent_name][0]
    ecoinvent_links = []

    for key, item in db.items():
        if item.get('ext_link'):
            if item['ext_link'][0] == ecoinvent_name:
                ecoinvent_links.append(item['ext_link'])

    for link in ecoinvent_links:
        if not ecoinvent_items.get(link):
            warn("{} not found in ecoinvent 3.3 cutoff database".format(link))
            return False

    return True   


def create_LcoptModel_from_BW2Package(import_filename):
    
    import_data = BW2Package.load_file(import_filename)
    orig_db = import_data[0]['data']
    db_name = import_data[0]['name']
    model = LcoptModel(db_name)
    db = deepcopy(orig_db)

    temp_param_set = []

    for k, v in db.items():
        exchanges = []
        production_amount = v['production amount']

        if production_amount != 1:
            print("NOTE: Production amount for {} is not 1 unit ({}). Parameters for this process will be divided by {} to normalise to one unit of output".format(v['name'], production_amount, production_amount))

        for e in v['exchanges']:

            exc_name = e.pop('name')
            exc_input = e.pop('input')
            exc_unit = unnormalise_unit(e.pop('unit'))
            exc_amount = e.pop('amount') / production_amount
            exc_type = e.pop('type')

            temp_param_set.append({'from': exc_name, 'to': v['name'], 'amount': exc_amount})

            if e.get('location'):
                e.pop('location')

            if exc_type == 'production':
                this_exc = {
                    'name': exc_name,
                    'type': exc_type,
                    'unit': exc_unit,
                    'lcopt_type': 'intermediate',
                }
                this_exc = {**this_exc, **e}
{'fraction_non_alphanumeric': 0.11508325171400588, 'fraction_numerical': 0.005876591576885406, 'mean_word_length': 2.3346938775510204, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6394847', 'n_tokens_mistral': 1271, 'n_tokens_neox': 1213, 'n_words': 288}