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_fragment(self, parent=None, flow=None, direction=None, comment=None, value=None, balance=False, **kwargs): """ :param parent: :param flow: :param direction: :param comment: :param value: :param balance: :param kwargs: :return: """ if flow is None: ch = cyoa('(N)ew flow or (S)earch for flow? ', 'ns') if ch == 'n': flow = self.new_flow() elif ch == 's': index = self.current_archive or select_archive(self) elem = {'i': False, 'e': True, 'a': None}[cyoa('(I)ntermediate, (E)lementary, or (A)ll flows? ', 'aei', 'I').lower()] flow = self.find_flow(None, index=index, elementary=elem) if flow is None: return None flow = flow.entity() else: raise ValueError print('Creating fragment with flow %s' % flow) direction = direction or {'i': 'Input', 'o': 'Output'}[cyoa('flow is (I)nput or (O)utput?', 'IO').lower()] comment = comment or ifinput('Enter FragmentFlow comment: ', '') if parent is None: # direction reversed for UX! user inputs direction w.r.t. fragment, not w.r.t. parent if value is None: value = 1.0 frag = self.new_fragment(flow, comp_dir(direction), Comment=comment, exchange_value=value, **kwargs) else: if parent.term.is_null: self.terminate_to_foreground(parent) if value is None: val = ifinput('Exchange value (%s per %s): ' % (flow.unit(), parent.unit), '1.0') if val == '1.0': value = 1.0 else: value = parse_math(val) frag = self[0].add_child_fragment_flow(parent, flow, direction, Comment=comment, exchange_value=value, **kwargs) if balance: frag.set_balance_flow() self.traverse(parent) if self.db.is_elementary(frag.flow): self.terminate_to_foreground(frag) return frag
def new_quantity(self, name=None, unit=None, comment=None): name = name or input('Enter quantity name: ') unit = unit or input('Unit by string: ') comment = comment or ifinput('Quantity Comment: ', '') q = LcQuantity.new(name, unit, Comment=comment) self._catalog[0].add(q) return q
def edit_flow(self): flow = pick_one(self._catalog[0].flows()) print('Select field to edit:') field = menu_list(*flow.keys()) if field == -1 or field is None: return True new = ifinput('Enter new value for %s: ' % field, flow[field]) flow[field] = new
def _prompt_add(self, entity): if entity is None: return True p = ifinput('Add to selection? y/n', 'y') if p == 'y': self.selected.add(entity) return entity return True
def _edit_entity(entity): print('Select field to edit:') field = menu_list(*entity.keys()) if field == -1 or field is None: return True new = ifinput('Enter new value for %s: ' % field, entity[field]) if len(new) > 0: entity[field] = new else: print('Not updating.')
def _edit_entity(entity): print('Select field to edit:') field = menu_list(*entity.keys()) if field == -1 or field is None: return True new = ifinput('Enter new value for %s: ' % field, entity[field]) if len(new) > 0: entity[field] = new else: print('Not updating.')
def _narrow_search(result_set): key = ifinput('Enter search key', 'Name') val = input('Enter search expression (regexp): ') n = [r for r in result_set if key in r.keys() and bool(re.search(val, r[key], flags=re.IGNORECASE))] if len(n) == 0: print('No results') return result_set else: return n
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 _update_ev(frag, scenario): val = frag.exchange_value(scenario) cur = str(val) upd = ifinput('New value: ', cur) if upd != cur: new_val = parse_math(upd) if new_val != val: if scenario is None: frag.reset_cache() frag.cached_ev = new_val else: frag.set_exchange_value(scenario, new_val)
def del_fragment(self, fragment): if isinstance(fragment, str): fragment = self.frag(fragment) print('%s' % fragment) self._show_frag_children(fragment) ts = self[0].linked_terms(fragment) if len(ts) > 0: print('Links to this fragment (terminations will be replaced with None):') for i in ts: print('%s' % i._parent) if ifinput('Are you sure?? y/n:', 'n') == 'y': for i in ts: i.update(None) self._del_fragment(fragment)
def del_quantity(self, quantity): """ This will remove all characterizations for a quantity, and then delete the quantity :param quantity: :return: """ print('Flows characterized by the quantity:') count = False for f in self._find_cfs(quantity): count = True print('%s' % f) if quantity is f.reference_entity: print(' *** reference quantity *** %s' % f.get_uuid()) if count: if ifinput('Really delete this quantity? y/n', 'y') != 'y': print('Aborted.') return for f in self._find_cfs(quantity): f.del_characterization(quantity) self._entities.pop(quantity._uuid) print('Deleted from foreground.')
def _observe(c, scenario): if scenario is None: prompt = 'Observed value' else: prompt = 'Scenario value' print('%s' % c) print(' Cached EV: %6.4g\n Observed EV: %6.4g [%s]' % (c.cached_ev, c.observed_ev, c.flow.unit())) if scenario is None: string_ev = '%10g' % c.observed_ev else: string_ev = '%10g' % c.exchange_value(scenario) print(' Scenario EV: %s [%s]' % (string_ev, c.flow.unit())) val = ifinput('%s ("=" to use cached): ' % prompt, string_ev) if val != string_ev: if val == '=': new_val = c.cached_ev else: new_val = parse_math(val) if scenario is None: c.observed_ev = new_val else: c.set_exchange_value(scenario, new_val)
def edit_characterizations(flow): char = pick_one(cf for cf in flow.characterizations()) val = float(ifinput('enter new characterization value: ', char.value)) char.value = val
def terminate_by_search(self, frag, index=None): print('%s' % frag) print('Search for termination.') string = ifinput('Enter search term: ', frag['Name']) return pick_one(self.search(index, 'p', Name=string, show=False))
def ifinput(self, query, default): if self._interactive: return ifinput(query, default) return default
def new_flow(self, flow=None, name=None, cas=None, quantity=None, comment=None, compartment=None, local_unit=None, **kwargs): if flow is None: name = name or self.input('Enter flow name: ', 'New flow') cas = cas or self.ifinput('Enter CAS number (or none): ', '') if quantity is None: if self._interactive: print('Choose reference quantity or none to create new: ') q = pick_one([fp for fp in self._qdb.flow_properties]) if q is None: q = self.new_quantity() quantity = q else: print('Using mass as reference quantity') quantity = self._qdb.get_canonical('mass') comment = comment or self.input('Enter flow comment: ', '') if compartment is None: if self._interactive: print('Choose compartment:') compartment = pick_compartment( self._qdb.c_mgr.compartments).to_list() else: print('Designating Intermediate flow') compartment = self._qdb.c_mgr.find_matching( 'Intermediate Flows').to_list() else: compartment = self._qdb.c_mgr.find_matching( compartment).to_list() if local_unit is not None: local_conv = quantity.convert(to=local_unit) if local_conv is None: print('Falling back to default unit: %s' % quantity.unit()) local_unit = None flow = LcFlow.new(name, quantity, CasNumber=cas, Compartment=compartment, Comment=comment, local_unit=local_unit, **kwargs) else: quantity = flow.reference_entity if self._interactive: 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._qdb.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 specify_foreground(self): folder = ifinput('Choose foreground: ', self._catalog.fg) self._m.workon(folder) return True
def build_child_flows(self, fragment, scenario=None, background_children=False): """ Given a terminated fragment, construct child flows corresponding to the termination's complementary exchanges. :param fragment: the parent fragment :param scenario: :param background_children: if true, automatically terminate child flows to background. :return: """ if fragment.is_background: return None # no child flows for background nodes term = fragment.termination(scenario=scenario) if term.is_null or term.is_fg: return None if term.term_node.entity_type == 'process': if scenario is not None: print('Automatic building of child flows is not supported for scenario terminations of process nodes.') return [] if len([c for c in self.child_flows(fragment)]) != 0: print('Warning: fragment already has child flows. Continuing will result in (possibly many) ') if ifinput('duplicate child flows. Continue? y/n', 'n') != 'y': return [] int_exch = self.gen_exchanges(term.term_node, term.term_flow, term.direction) # in process case- possible to specify multiple identical children from the same node """ this means that build_child_flows will have undesirable results if called for a process- terminated node with similar exchanges for multiple scenarios. maybe changing node terminations should only be permitted for subfragments. something to think about. """ elif term.term_node.entity_type == 'fragment': if term.term_node.reference_entity is not None: the_ref = _recursive_ref(term.term_node) correct_reference = True else: the_ref = term.term_node correct_reference = False int_exch = self.get_fragment_inventory(the_ref, scenario=scenario) if correct_reference: surrogate_in = next(x for x in int_exch if x.flow is term.term_flow and x.direction == term.direction) # or StopIter int_exch.remove(surrogate_in) int_exch.append(ExchangeValue(the_ref, the_ref.flow, comp_dir(the_ref.direction), value=the_ref.cached_ev)) for x in int_exch: x.value *= 1.0 / surrogate_in.value # in subfragment case- child flows aggregate so we don't want to create duplicate children for x in int_exch: match = [c for c in self.child_flows(fragment) if c.flow == x.flow and c.direction == x.direction] if len(match) > 1: raise AmbiguousReference('Multiple child flows matching %s' % x) elif len(match) == 1: int_exch.remove(x) # don't make a new child if one is already found else: raise AmbiguousTermination('Cannot figure out entity type for %s' % term) children = [] for exch in int_exch: child = self[0].add_child_ff_from_exchange(fragment, exch) if background_children: self.terminate_to_background(child) children.append(child) return children
def edit_characterizations(flow): char = pick_one(cf for cf in flow.characterizations()) val = float(ifinput('enter new characterization value: ', char.value)) char.value = val