def _create_flow(self, exchange): """ makes a flow entity and adds to the db :param exchange: :return: """ if 'intermediate' in exchange.tag: uid = exchange.attrib['intermediateExchangeId'] cat = [self._cls_to_text(exchange.classification)] elif 'elementary' in exchange.tag: uid = exchange.attrib['elementaryExchangeId'] cat = self._cat_to_text(exchange.compartment) else: raise AttributeError('No exchange type found for id %s' % exchange.attrib['id']) if self[uid] is not None: return self[uid] if 'casNumber' in exchange.attrib: cas = exchange.attrib['casNumber'] else: cas = '' q = self._create_quantity(exchange) n = exchange.name.text c = 'EcoSpold02 Flow' f = LcFlow(uid, Name=n, CasNumber=cas, Comment=c, Compartment=cat) f.add_characterization(quantity=q, reference=True) self.add(f) return f
def _flow_from_json(self, entity_j, uid): entity_j.pop('externalId') # TODO if 'referenceQuantity' in entity_j: entity_j.pop('referenceQuantity') chars = entity_j.pop('characterizations', []) flow = LcFlow(uid, **entity_j) for c in chars: v = None q = self[c['quantity']] # this is required because of foreground; _process_from_json unaffected if q is None: continue # import json # import sys # print(ext_ref) # json.dump(c, sys.stdout, indent=2) # raise KeyError if 'value' in c: v = c['value'] if 'isReference' in c: is_ref = c['isReference'] else: is_ref = False flow.add_characterization(q, reference=is_ref, value=v) return flow
def _create_flow(self, exchange): """ makes a flow entity and adds to the db :param exchange: :return: """ if 'intermediate' in exchange.tag: uid = exchange.attrib['intermediateExchangeId'] cat = [self._cls_to_text(exchange.classification)] elif 'elementary' in exchange.tag: uid = exchange.attrib['elementaryExchangeId'] cat = self._cat_to_text(exchange.compartment) else: raise AttributeError('No exchange type found for id %s' % exchange.attrib['id']) f = self[uid] if f is not None: return f if 'casNumber' in exchange.attrib: cas = exchange.attrib['casNumber'] else: cas = '' q = self._create_quantity(exchange) n = exchange.name.text c = 'EcoSpold02 Flow' f = LcFlow(uid, Name=n, CasNumber=cas, Comment=c, Compartment=cat) f.add_characterization(quantity=q, reference=True) self.add(f) return f
def _create_flow(self, row): key = self._flow_key(row) u = self._key_to_id(key) f = self._try_flow(u, key) if f is None: f = LcFlow(u, Name=row['name'], CasNumber='', Compartment=[row['compartment'], row['subcompartment']], Comment=row['note']) self._print('Created new flow with %s ' % self._upstream_flow_key(f)) f.add_characterization(self._mass, reference=True) f.set_external_ref(key) self.add(f) return f
def _create_flow(self, o): """ :param o: objectified flow :return: an LcFlow """ ns = find_ns(o.nsmap, 'Flow') n = grab_flow_name(o, ns=ns) u = str(find_common(o, 'UUID')[0]) c = str(find_common(o, 'generalComment')[0]) cas = str(find_tag(o, 'CASNumber', ns=ns)[0]) cat = find_common(o, 'category') if cat == ['']: cat = find_common(o, 'class') cat = [str(i) for i in cat] f = LcFlow(u, Name=n, CasNumber=cas, Comment=c, Compartment=cat) f.set_external_ref('%s/%s' % (typeDirs['Flow'], u)) ref_to_ref = get_reference_flow_property_id(o, ns=ns) for fp in o['flowProperties'].getchildren(): if int(fp.attrib['dataSetInternalID']) == ref_to_ref: is_ref = True else: is_ref = False val = float(find_tag(fp, 'meanValue', ns=ns)[0]) ref = find_tag(fp, 'referenceToFlowPropertyDataSet', ns=ns)[0] rfp_uuid = ref.attrib['refObjectId'] rfp_uri = ref.attrib['uri'] try: q = self._check_or_retrieve_child(rfp_uuid, rfp_uri) except (HTTPError, XMLSyntaxError, KeyError): continue try: f.add_characterization(q, reference=is_ref, value=val) except DuplicateCharacterizationError: print('Duplicate Characterization in entity %s\n %s = %g' % (u, q, val)) # let it go try: self.add(f) except KeyError: print('Found duplicate entity %s' % u) raise return f
def new_flow(self, flow=None, name=None, cas=None, quantity=None, comment=None, compartment=None): if flow is None: name = name or input('Enter flow name: ') cas = cas or ifinput('Enter CAS number (or none): ', '') print('Choose reference quantity or none to create new: ') if quantity is None: q = pick_one(self.flow_properties) if q is None: q = self.new_quantity() quantity = q comment = comment or input('Enter flow comment: ') if compartment is None: print('Choose compartment:') compartment = pick_compartment(self.db.compartments).to_list() flow = LcFlow.new(name, quantity, CasNumber=cas, Compartment=compartment, Comment=comment) # flow.add_characterization(q, reference=True) else: quantity = flow.reference_entity self._catalog[0].add(flow) flow.profile() while ifinput('Add characterizations for this flow? y/n', 'n') != 'n': ch = cyoa('[n]ew or [e]xisting quantity? ', 'en', 'e') if ch == 'n': cq = self.new_quantity() else: cq = pick_one(self[0].quantities()) if cq is None: cq = self.new_quantity() val = parse_math(input('Value (1 %s = x %s): ' % (quantity.unit(), cq.unit()))) flow.add_characterization(cq, value=val) return flow
def _create_dummy_flow_from_exch(uid, exch): n = str(find_common(exch, 'shortDescription')) print('Creating DUMMY flow (%s) with name %s' % (uid, n)) return LcFlow(uid, Name=n, Comment='Dummy flow (HTTP or XML error)', Compartment=['dummy flows'])
def test_add_intflows(self): self.cm.set_local(self._test_file) f2 = LcFlow.new("Dummy flow", None, Compartment=['Products']) self.assertIsNone( self.cm.find_matching(f2['Compartment'], interact=False)) products = self.cm.add_compartment(f2['Compartment']) self.assertIs(self.cm.find_matching(f2['Compartment']), products)
def _create_flow(self, u, unit, ext_ref, Name=None, Compartment=None, **kwargs): if u in self._entities: f = self[u] else: f = LcFlow(u, Name=Name, Compartment=Compartment, **kwargs) f.set_external_ref(ext_ref) q = self._create_quantity(unit) if q is None: raise ValueError f.add_characterization(quantity=q, reference=True) self.add(f) f.update(kwargs)
def create_flow(self): name = input('Enter flow name: ') cas = ifinput('Enter CAS number (or none): ', '') print('Choose reference quantity: ') q = pick_one(self._catalog[0].quantities()) print('Choose compartment:') comment = input('Enter comment: ') c = pick_compartment(self._flowdb.compartments) flow = LcFlow.new(name, q, CasNumber=cas, Compartment=c.to_list(), Comment=comment) # flow.add_characterization(q, reference=True) self._catalog[0].add(flow) return flow
def _create_flow(self, u, unit, ext_ref, Name=None, Compartment=None, **kwargs): upstream_key = ', '.join([Name] + Compartment) f = self._try_flow(u, upstream_key) if f is None: f = LcFlow(u, Name=Name, Compartment=Compartment, **kwargs) f.set_external_ref(ext_ref) q = self._create_quantity(unit) if q is None: raise ValueError f.add_characterization(quantity=q, reference=True) self.add(f) else: f.update(kwargs)
def _create_flow(self, row, compartment): flowable = row['Substance Name'].lower() cas = transform_numeric_cas(row['Formatted CAS #']) ext_ref = self._flow_key(flowable, compartment) u = self._key_to_nsuuid(ext_ref) f = self[u] if f is None: f = LcFlow(u, external_ref=ext_ref, Name=flowable, Compartment=[compartment], ReferenceQuantity=self._mass, CasNumber=cas or '') self.add(f) return f
def _create_flow(self, row): key = self._flow_key(row) u = self._key_to_nsuuid(key) if u in self._entities: return self[u] f = LcFlow(u, Name=row['name'], CasNumber='', Compartment=[row['compartment'], row['subcompartment']], Comment=row['note']) self._print('Created new flow with %s ' % key) f.add_characterization(self._mass, reference=True) f.set_external_ref(key) self.add(f) return f
def _create_flow(self, exch): """ An ecospold01 exchange is really just a long attribute list, plus an inputGroup or outputGroup (ignored here) :param exch: :return: """ number = int(exch.get('number')) uid = self._key_to_nsuuid(number) try_f = self[uid] if try_f is not None: f = try_f assert f.entity_type == 'flow', "Expected flow, found %s" % f.entity_type else: # generate flow n = exch.get("name") q = self._create_quantity(exch.get("unit")) c = not_none(exch.get("generalComment")) cas = not_none(exch.get("CASNumber")) cat = [exch.get('category'), exch.get('subCategory')] f = LcFlow(uid, Name=n, CasNumber=cas, Comment=c, Compartment=cat) f.add_characterization(q, reference=True) f.set_external_ref(number) self.add(f) if exch.get("unit") != f.unit(): local_q = self._create_quantity(exch.get("unit")) if not f.has_characterization(local_q): if (f.unit(), local_q.unit()) in conversion_dict: val = conversion_dict[(f.unit(), local_q.unit())] elif (local_q.unit(), f.unit()) in conversion_dict: val = 1.0 / conversion_dict[(local_q.unit(), f.unit())] else: print('Flow %s needs characterization for unit %s' % (f, local_q)) val = parse_math(input('Enter conversion factor 1 %s = x %s' % (f.unit(), local_q))) f.add_characterization(local_q, value=val) return f
def test_elementary(self): self.cm.set_local(self._test_file) f1 = LcFlow.new("Dummy flow", None, Compartment=['Emissions to air']) f2 = LcFlow.new("Dummy flow", None, Compartment=['Products']) self.assertTrue(self.cm.is_elementary(f1)) self.assertFalse(self.cm.is_elementary(f2))
def _create_flow(self, o): """ :param o: objectified flow :return: an LcFlow """ u = str(find_common(o, 'UUID')) try_f = self[u] if try_f is not None: return try_f ns = find_ns(o.nsmap, 'Flow') n = grab_flow_name(o, ns=ns) c = str(find_common(o, 'generalComment')) cas = str(find_tag(o, 'CASNumber', ns=ns)) cat = find_tags(o, 'category', ns='common') if cat == ['']: cat = find_tags(o, 'class', ns='common') cat = [str(i) for i in cat] if str(find_tag(o, 'typeOfDataSet', ns=ns)) == 'Elementary flow': f = LcFlow(u, Name=n, CasNumber=cas, Comment=c, Compartment=cat) else: f = LcFlow(u, Name=n, CasNumber=cas, Comment=c, Compartment=['Intermediate flows'], Class=cat) f.set_external_ref('%s/%s' % (typeDirs['Flow'], u)) ref_to_ref = get_reference_flow_property_id(o, ns=ns) for fp in o['flowProperties'].getchildren(): if int(fp.attrib['dataSetInternalID']) == ref_to_ref: is_ref = True else: is_ref = False val = float(find_tag(fp, 'meanValue', ns=ns)) ref = find_tag(fp, 'referenceToFlowPropertyDataSet', ns=ns) rfp_uuid = ref.attrib['refObjectId'] rfp_uri = ref.attrib['uri'] try: q = self._check_or_retrieve_child(rfp_uuid, rfp_uri) except (HTTPError, XMLSyntaxError, KeyError): continue try: f.add_characterization(q, reference=is_ref, value=val) except DuplicateCharacterizationError: print('Duplicate Characterization in entity %s\n %s = %g' % (u, q, val)) # let it go self.add(f) return f
def _create_flow(self, exch): """ An ecospold01 exchange is really just a long attribute list, plus an inputGroup or outputGroup (ignored here) :param exch: :return: """ number = int(exch.get('number')) uid = self._key_to_id(number) try_f = self[uid] if try_f is not None: f = try_f assert f.entity_type == 'flow', "Expected flow, found %s" % f.entity_type else: # generate flow n = exch.get("name") q = self._create_quantity(exch.get("unit")) c = not_none(exch.get("generalComment")) cas = not_none(exch.get("CASNumber")) cat = [exch.get('category'), exch.get('subCategory')] f = LcFlow(uid, Name=n, CasNumber=cas, Comment=c, Compartment=cat) f.add_characterization(q, reference=True) f.set_external_ref(number) self.add(f) if exch.get("unit") != f.unit(): local_q = self._create_quantity(exch.get("unit")) if not f.has_characterization(local_q): if (f.unit(), local_q.unit()) not in conversion_dict: print('Flow %s needs characterization for unit %s' % (f, local_q)) val = parse_math(input('Enter conversion factor 1 %s = x %s' % (f.unit(), local_q))) else: val = conversion_dict[(f.unit(), local_q.unit())] f.add_characterization(local_q, value=val) return f
def new_flow(self, name, ref_qty, CasNumber='', **kwargs): u = self._check_key_unused(name) f = LcFlow(u, Name=name, ReferenceQuantity=ref_qty, CasNumber=CasNumber, origin=self.ref, external_ref=name, **kwargs) self.add_entity_and_children(f) return f