def eval(self, metric, conds=None): """Evalutes a metric with the given conditions.""" rawkey = (metric, conds if conds is None else frozenset(conds)) if rawkey in self.rawcache: return self.rawcache[rawkey] m = self.get_metric(metric) series = [] for dep in m.dependencies: d = self.eval(dep[0], conds=conds) s = None if d is None else raw_to_series(d, dep[1], dep[2]) series.append(s) raw = m(series=series, conds=conds, known_tables=self.known_tables) if raw is None: return raw self.rawcache[rawkey] = raw # write back to db if (m.name in self.known_tables) or (not self.write): return raw rec = self.recorder rawd = raw.to_dict(orient='list') for i in range(len(raw)): d = rec.new_datum(m.name) for field, dbtype, shape in m.schema: fname = m.schema.byte_names[field] val = rawd[str(field)][i] d = d.add_val(fname, val, type=dbtype, shape=shape) d.record() self.known_tables.add(m.name) return raw
def agents(entry, exit, decom, info): """Computes the Agents table. This is tricky because both the AgentExit table and the DecomSchedule table may not be present in the database. Furthermore, the Info table does not contain the AgentId column. This computation handles the calculation of the ExitTime in the face a significant amounts of missing data. """ mergeon = ['SimId', 'AgentId'] ent = tools.raw_to_series(entry, ['SimId', 'AgentId'], 'Kind') idx = ent.index df = entry[[ 'SimId', 'AgentId', 'Kind', 'Spec', 'Prototype', 'ParentId', 'Lifetime', 'EnterTime' ]] if exit is None: agent_exit = pd.Series(index=idx, data=[np.nan] * len(idx)) agent_exit.name = 'ExitTime' else: agent_exit = agent_exit.reindex(index=idx) df = pd.merge(df, agent_exit.reset_index(), on=mergeon) if decom is not None: df = tools.merge_and_fillna_col( df, decom[['SimId', 'AgentId', 'DecomTime']], 'ExitTime', 'DecomTime', on=mergeon) df = tools.merge_and_fillna_col(df, info[['SimId', 'Duration']], 'ExitTime', 'Duration', on=['SimId']) return df
def eval(self, metric, conds=None): """Evalutes a metric with the given conditions.""" rawkey = (metric, conds if conds is None else frozenset(conds)) if rawkey in self.rawcache: return self.rawcache[rawkey] m = self.get_metric(metric) series = [] for dep in m.dependencies: d = self.eval(dep[0], conds=conds) s = None if d is None else raw_to_series(d, dep[1], dep[2]) series.append(s) raw = m(series=series, conds=conds, known_tables=self.known_tables) if raw is None: return raw self.rawcache[rawkey] = raw # write back to db if (m.name in self.known_tables) or (not self.write): return raw rec = self.recorder rawd = raw.to_dict(outtype='list') for i in range(len(raw)): d = rec.new_datum(m.name) for field, dbtype, shape in m.schema: fname = m.schema.byte_names[field] val = rawd[str(field)][i] d = d.add_val(fname, val, dbtype=dbtype, shape=shape) d.record() self.known_tables.add(m.name) return raw
def test_agents(): exp = pd.DataFrame(np.array([ (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 22, 'Region', ':agents:NullRegion', 'USA', -1, -1, 0, 120.0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 23, 'Inst', ':agents:NullInst', 'utility', 22, -1, 0, 120.0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 24, 'Facility', ':agents:Source', 'MineU235', 23, -1, 0, 120.0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 25, 'Facility', ':agents:Source', 'U238', 23, -1, 0, 120.0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 26, 'Facility', ':agents:Source', 'DU', 23, -1, 0, 120.0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 27, 'Facility', ':agents:Source', 'DU2', 23, -1, 0, 120.0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 28, 'Facility', ':Brightlite:FuelfabFacility', 'LWR Fuel FAb', 23, -1, 0, 120.0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 29, 'Facility', ':Brightlite:ReprocessFacility', 'LWR Seperation', 23, -1, 0, 120.0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 30, 'Facility', ':Brightlite:ReprocessFacility', 'FR Reprocess', 23, -1, 0, 120.0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 31, 'Facility', ':agents:Sink', 'SINK', 23, -1, 0, 120.0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 32, 'Facility', ':Brightlite:FuelfabFacility', 'FR Fuel Fab', 23, -1, 0, 120.0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 33, 'Inst', ':cycamore:DeployInst', 'utility2', 22, -1, 0, 120.0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 34, 'Facility', ':Brightlite:ReactorFacility', 'LWR', 33, -1, 5, 120.0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 35, 'Facility', ':Brightlite:ReactorFacility', 'LWR', 33, -1, 5, 120.0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 36, 'Facility', ':Brightlite:ReactorFacility', 'FRx', 33, -1, 10, 120.0), ], dtype=ensure_dt_bytes([ ('SimId', 'O'), ('AgentId', '<i8'), ('Kind', 'O'), ('Spec', 'O'), ('Prototype', 'O'), ('ParentId', '<i8'), ('Lifetime', '<i8'), ('EnterTime', '<i8'), ('ExitTime', '<f8')])) ) agent_entry = pd.DataFrame(np.array([ (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 22, 'Region', ':agents:NullRegion', 'USA', -1, -1, 0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 23, 'Inst', ':agents:NullInst', 'utility', 22, -1, 0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 24, 'Facility', ':agents:Source', 'MineU235', 23, -1, 0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 25, 'Facility', ':agents:Source', 'U238', 23, -1, 0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 26, 'Facility', ':agents:Source', 'DU', 23, -1, 0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 27, 'Facility', ':agents:Source', 'DU2', 23, -1, 0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 28, 'Facility', ':Brightlite:FuelfabFacility', 'LWR Fuel FAb', 23, -1, 0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 29, 'Facility', ':Brightlite:ReprocessFacility', 'LWR Seperation', 23, -1, 0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 30, 'Facility', ':Brightlite:ReprocessFacility', 'FR Reprocess', 23, -1, 0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 31, 'Facility', ':agents:Sink', 'SINK', 23, -1, 0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 32, 'Facility', ':Brightlite:FuelfabFacility', 'FR Fuel Fab', 23, -1, 0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 33, 'Inst', ':cycamore:DeployInst', 'utility2', 22, -1, 0), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 34, 'Facility', ':Brightlite:ReactorFacility', 'LWR', 33, -1, 5), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 35, 'Facility', ':Brightlite:ReactorFacility', 'LWR', 33, -1, 5), (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 36, 'Facility', ':Brightlite:ReactorFacility', 'FRx', 33, -1, 10), ], dtype=ensure_dt_bytes([ ('SimId', 'O'), ('AgentId', '<i8'), ('Kind', 'O'), ('Spec', 'O'), ('Prototype', 'O'), ('ParentId', '<i8'), ('Lifetime', '<i8'), ('EnterTime', '<i8')])) ) info = pd.DataFrame({'Duration': {0: 120}, 'SimId': {0: UUID('f22f2281-2464-420a-8325-37320fd418f8')}, }) dur = info.set_index(['SimId']) series = [raw_to_series(agent_entry, ['SimId', 'AgentId'], col) \ for col in ('Kind', 'Spec', 'Prototype', 'ParentId', 'Lifetime', 'EnterTime')] series += [None, None, dur] obs = metrics.agents.func(series) assert_frame_equal(exp, obs)
def timelist(info): """In case the sim does not have entries for every timestep, this populates a list with all timesteps in the duration. """ info = tools.raw_to_series(info, ('SimId', ), 'Duration') tl = [] for sim, dur in info.iteritems(): for i in range(dur): tl.append((sim, i)) tl = pd.DataFrame(tl, columns=['SimId', 'TimeStep']) return tl
def timelist(info): """In case the sim does not have entries for every timestep, this populates a list with all timesteps in the duration. """ info = tools.raw_to_series(info, ('SimId',), 'Duration') tl = [] for sim, dur in info.iteritems(): for i in range(dur): tl.append((sim, i)) tl = pd.DataFrame(tl, columns=['SimId', 'TimeStep']) return tl
def explicit_inventory_by_agent(expinv): """The Inventory By Agent metric groups the inventories by Agent (keeping all nuc information) """ inv_index = ['SimId', 'AgentId', 'Time', 'InventoryName', 'NucId'] inv = tools.raw_to_series( expinv, ['SimId', 'AgentId', 'Time', 'InventoryName', 'NucId'], 'Quantity') inv = inv.groupby(level=inv_index).sum() inv.name = 'Quantity' rtn = inv.reset_index() return rtn
def explicit_inventory_by_agent(expinv): """The Inventory By Agent metric groups the inventories by Agent (keeping all nuc information) """ inv_index = ['SimId', 'AgentId', 'Time', 'InventoryName', 'NucId'] inv = tools.raw_to_series(expinv, ['SimId', 'AgentId', 'Time', 'InventoryName', 'NucId'], 'Quantity') inv = inv.groupby(level=inv_index).sum() inv.name = 'Quantity' rtn = inv.reset_index() return rtn
def explicit_inventory_by_nuc(expinv): """The Inventory By Nuc metric groups the inventories by nuclide and discards the agent information it is attached to (providing fuel cycle-wide nuclide inventories) """ inv_index = ['SimId', 'Time', 'InventoryName', 'NucId'] inv = tools.raw_to_series(expinv, ('SimId', 'Time', 'InventoryName', 'NucId'), 'Quantity') inv = inv.groupby(level=inv_index).sum() inv.name = 'Quantity' rtn = inv.reset_index() return rtn
def decay_heat(acts): """Decay heat metric returns the instantaneous decay heat of a nuclide in a material (Q value * activity) indexed by the SimId, QualId, ResourceId, ObjId, TimeCreated, and NucId. """ tools.raise_no_pyne('DecayHeat could not be computed', HAVE_PYNE) act = tools.raw_to_series(acts, ('SimId', 'QualId', 'ResourceId', 'ObjId', 'TimeCreated', 'NucId'), 'Activity') dh = [] for (simid, qual, res, obj, time, nuc), a in act.iteritems(): val = (data.MeV_per_MJ * a * data.q_val(nuc)) dh.append(val) dh = pd.Series(dh, index=act.index) dh.name = 'DecayHeat' rtn = dh.reset_index() return rtn
def activity(mats): """Activity metric returns the instantaneous activity of a nuclide in a material (material mass * decay constant / atomic mass) indexed by the SimId, QualId, ResourceId, ObjId, TimeCreated, and NucId. """ tools.raise_no_pyne('Activity could not be computed', HAVE_PYNE) mass = tools.raw_to_series(mats, ('SimId', 'QualId', 'ResourceId', 'ObjId', 'TimeCreated', 'NucId'), 'Mass') act = [] for (simid, qual, res, obj, time, nuc), m in mass.iteritems(): val = (1000 * data.N_A * m * data.decay_const(nuc) \ / data.atomic_mass(nuc)) act.append(val) act = pd.Series(act, index=mass.index) act.name = 'Activity' rtn = act.reset_index() return rtn
def agents(entry, exit, decom, info): """Computes the Agents table. This is tricky because both the AgentExit table and the DecomSchedule table may not be present in the database. Furthermore, the Info table does not contain the AgentId column. This computation handles the calculation of the ExitTime in the face a significant amounts of missing data. """ mergeon = ['SimId', 'AgentId'] ent = tools.raw_to_series(entry, ['SimId', 'AgentId'], 'Kind') idx = ent.index df = entry[['SimId', 'AgentId', 'Kind', 'Spec', 'Prototype', 'ParentId', 'Lifetime', 'EnterTime']] if exit is None: agent_exit = pd.Series(index=idx, data=[np.nan]*len(idx)) agent_exit.name = 'ExitTime' else: agent_exit = agent_exit.reindex(index=idx) df = pd.merge(df, agent_exit.reset_index(), on=mergeon) if decom is not None: df = tools.merge_and_fillna_col(df, decom[['SimId', 'AgentId', 'DecomTime']], 'ExitTime', 'DecomTime', on=mergeon) df = tools.merge_and_fillna_col(df, info[['SimId', 'Duration']], 'ExitTime', 'Duration', on=['SimId']) return df
def test_agents(): exp = pd.DataFrame( np.array( [ ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 22, "Region", ":agents:NullRegion", "USA", -1, -1, 0, 120.0, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 23, "Inst", ":agents:NullInst", "utility", 22, -1, 0, 120.0, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 24, "Facility", ":agents:Source", "MineU235", 23, -1, 0, 120.0, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 25, "Facility", ":agents:Source", "U238", 23, -1, 0, 120.0, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 26, "Facility", ":agents:Source", "DU", 23, -1, 0, 120.0, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 27, "Facility", ":agents:Source", "DU2", 23, -1, 0, 120.0, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 28, "Facility", ":Brightlite:FuelfabFacility", "LWR Fuel FAb", 23, -1, 0, 120.0, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 29, "Facility", ":Brightlite:ReprocessFacility", "LWR Seperation", 23, -1, 0, 120.0, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 30, "Facility", ":Brightlite:ReprocessFacility", "FR Reprocess", 23, -1, 0, 120.0, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 31, "Facility", ":agents:Sink", "SINK", 23, -1, 0, 120.0, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 32, "Facility", ":Brightlite:FuelfabFacility", "FR Fuel Fab", 23, -1, 0, 120.0, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 33, "Inst", ":cycamore:DeployInst", "utility2", 22, -1, 0, 120.0, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 34, "Facility", ":Brightlite:ReactorFacility", "LWR", 33, -1, 5, 120.0, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 35, "Facility", ":Brightlite:ReactorFacility", "LWR", 33, -1, 5, 120.0, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 36, "Facility", ":Brightlite:ReactorFacility", "FRx", 33, -1, 10, 120.0, ), ], dtype=ensure_dt_bytes( [ ("SimId", "O"), ("AgentId", "<i8"), ("Kind", "O"), ("Spec", "O"), ("Prototype", "O"), ("ParentId", "<i8"), ("Lifetime", "<i8"), ("EnterTime", "<i8"), ("ExitTime", "<f8"), ] ), ) ) agent_entry = pd.DataFrame( np.array( [ (UUID("f22f2281-2464-420a-8325-37320fd418f8"), 22, "Region", ":agents:NullRegion", "USA", -1, -1, 0), (UUID("f22f2281-2464-420a-8325-37320fd418f8"), 23, "Inst", ":agents:NullInst", "utility", 22, -1, 0), (UUID("f22f2281-2464-420a-8325-37320fd418f8"), 24, "Facility", ":agents:Source", "MineU235", 23, -1, 0), (UUID("f22f2281-2464-420a-8325-37320fd418f8"), 25, "Facility", ":agents:Source", "U238", 23, -1, 0), (UUID("f22f2281-2464-420a-8325-37320fd418f8"), 26, "Facility", ":agents:Source", "DU", 23, -1, 0), (UUID("f22f2281-2464-420a-8325-37320fd418f8"), 27, "Facility", ":agents:Source", "DU2", 23, -1, 0), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 28, "Facility", ":Brightlite:FuelfabFacility", "LWR Fuel FAb", 23, -1, 0, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 29, "Facility", ":Brightlite:ReprocessFacility", "LWR Seperation", 23, -1, 0, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 30, "Facility", ":Brightlite:ReprocessFacility", "FR Reprocess", 23, -1, 0, ), (UUID("f22f2281-2464-420a-8325-37320fd418f8"), 31, "Facility", ":agents:Sink", "SINK", 23, -1, 0), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 32, "Facility", ":Brightlite:FuelfabFacility", "FR Fuel Fab", 23, -1, 0, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 33, "Inst", ":cycamore:DeployInst", "utility2", 22, -1, 0, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 34, "Facility", ":Brightlite:ReactorFacility", "LWR", 33, -1, 5, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 35, "Facility", ":Brightlite:ReactorFacility", "LWR", 33, -1, 5, ), ( UUID("f22f2281-2464-420a-8325-37320fd418f8"), 36, "Facility", ":Brightlite:ReactorFacility", "FRx", 33, -1, 10, ), ], dtype=ensure_dt_bytes( [ ("SimId", "O"), ("AgentId", "<i8"), ("Kind", "O"), ("Spec", "O"), ("Prototype", "O"), ("ParentId", "<i8"), ("Lifetime", "<i8"), ("EnterTime", "<i8"), ] ), ) ) info = pd.DataFrame({"Duration": {0: 120}, "SimId": {0: UUID("f22f2281-2464-420a-8325-37320fd418f8")}}) dur = info.set_index(["SimId"]) series = [ raw_to_series(agent_entry, ["SimId", "AgentId"], col) for col in ("Kind", "Spec", "Prototype", "ParentId", "Lifetime", "EnterTime") ] series += [None, None, dur] obs = metrics.agents.func(series) assert_frame_equal(exp, obs)