def get_aggregated_quantities(self): """ Return mean values for all quantities for each symbol. Returns: (dict<Symbol, weighted_mean) mapping from a Symbol to an aggregated statistic. """ # TODO: proper weighting system, and more flexibility in object handling aggregated = {} for symbol, quantities in self._quantities_by_symbol.items(): if not symbol.category == 'object': aggregated[symbol] = NumQuantity.from_weighted_mean( list(quantities)) return aggregated
def setUp(self): # Inspiration was taken from the GraphTest class # I tried to construct the dictionaries for comparison # without writing out every one explicity by reusing # information where it was applicable. # If this is too unreadable, can change to writing it # out explicity in a JSON file and importing it. Would # still need to replace some fields dynamically. symbols = StorageTest.generate_symbols() self.custom_syms_as_dicts = { k: {'@module': 'propnet.core.symbols', '@class': 'Symbol', 'name': k, 'display_names': [k], 'display_symbols': [k], 'units': (1, ()), 'shape': 1, 'object_type': None, 'comment': None, 'category': 'property', 'constraint': None, 'default_value': None} for k in ['A', 'B', 'C'] } self.custom_syms_as_dicts['C'].update( {"units": None, "shape": None, "object_type": "str", "category": "object"}) self.custom_symbols_json = copy.deepcopy(self.custom_syms_as_dicts) for k in ['A', 'B']: self.custom_symbols_json[k]['units'] = [1, []] a = [QuantityFactory.create_quantity(symbols['A'], 19), QuantityFactory.create_quantity(symbols['A'], 23)] b = [QuantityFactory.create_quantity(symbols['B'], 38, provenance=ProvenanceElement(model='model1', inputs=[a[0]])), QuantityFactory.create_quantity(symbols['B'], 46, provenance=ProvenanceElement(model='model1', inputs=[a[1]]))] self.quantities_custom_symbol = {"A": a, "B": b} self.sq_custom_sym_as_dicts = { k: [{'@module': 'propnet.dbtools.storage', '@class': 'StorageQuantity', 'internal_id': vv._internal_id, 'data_type': 'NumQuantity', 'symbol_type': symbols[k], 'value': vv.magnitude, 'units': 'dimensionless', 'provenance': ProvenanceStore.from_provenance_element(vv.provenance), 'tags': [], 'uncertainty': None} for vv in v] for k, v in self.quantities_custom_symbol.items() } provenances_json = { "A": [{'@module': 'propnet.dbtools.storage', '@class': 'ProvenanceStore', 'model': None, 'inputs': None, 'source': aa.provenance.source} for aa in a]} provenances_json['B'] = [ {'@module': 'propnet.dbtools.storage', '@class': 'ProvenanceStore', 'model': 'model1', 'inputs': [{'@module': 'propnet.dbtools.storage', '@class': 'ProvenanceStoreQuantity', 'data_type': 'NumQuantity', 'symbol_type': self.custom_symbols_json['A'], 'internal_id': q.provenance.inputs[0]._internal_id, 'tags': [], 'provenance': p}], 'source': q.provenance.source} for q, p in zip(b, provenances_json['A'])] self.sq_custom_sym_json = copy.deepcopy(self.sq_custom_sym_as_dicts) for sym in ['A', 'B']: for q, p in zip(self.sq_custom_sym_json[sym], provenances_json[sym]): q['symbol_type'] = self.custom_symbols_json[sym] q['provenance'] = p band_gaps = [QuantityFactory.create_quantity('band_gap', 3.3, 'eV'), QuantityFactory.create_quantity('band_gap', 2.1, 'eV')] bg_ri_model = DEFAULT_MODEL_DICT['band_gap_refractive_index_moss'] refractive_indices = [bg_ri_model.evaluate({"Eg": bg}).pop('refractive_index') for bg in band_gaps] self.quantities_canonical_symbol = {"band_gaps": band_gaps, "refractive_indices": refractive_indices} self.sq_canonical_sym_as_dicts_no_value = copy.deepcopy(self.sq_custom_sym_as_dicts) self.sq_canonical_sym_as_dicts_no_value['band_gaps'] = self.sq_canonical_sym_as_dicts_no_value.pop('A') self.sq_canonical_sym_as_dicts_no_value['refractive_indices'] = self.sq_canonical_sym_as_dicts_no_value.pop('B') for d, sq in zip(self.sq_canonical_sym_as_dicts_no_value['band_gaps'], band_gaps): d.update({ "internal_id": sq._internal_id, "symbol_type": "band_gap", "units": "electron_volt", "provenance": ProvenanceStore.from_provenance_element(sq.provenance) }) d.pop('value') for d, sq in zip(self.sq_canonical_sym_as_dicts_no_value['refractive_indices'], refractive_indices): d.update({ "internal_id": sq._internal_id, "symbol_type": "refractive_index", "units": "dimensionless", "provenance": ProvenanceStore.from_provenance_element(sq.provenance) }) d.pop('value') self.sq_canonical_sym_values = {"band_gaps": [3.3, 2.1], "refractive_indices": [2.316340583741216, 2.593439239956374]} provenances_json['band_gaps'] = [ {'@module': 'propnet.dbtools.storage', '@class': 'ProvenanceStore', 'model': None, 'inputs': None, 'source': bg.provenance.source} for bg in band_gaps ] provenances_json['refractive_indices'] = [{ '@module': 'propnet.dbtools.storage', '@class': 'ProvenanceStore', 'model': 'band_gap_refractive_index_moss', 'inputs': [{'@module': 'propnet.dbtools.storage', '@class': 'ProvenanceStoreQuantity', 'data_type': 'NumQuantity', 'symbol_type': 'band_gap', 'internal_id': bg._internal_id, 'tags': [], 'provenance': pj}], 'source': ri.provenance.source} for bg, pj, ri in zip(band_gaps, provenances_json['band_gaps'], refractive_indices) ] self.sq_canonical_sym_json_no_value = copy.deepcopy(self.sq_canonical_sym_as_dicts_no_value) for sym in ["band_gaps", "refractive_indices"]: for q, p in zip(self.sq_canonical_sym_json_no_value[sym], provenances_json[sym]): q['provenance'] = p self.quantity_with_uncertainty = NumQuantity.from_weighted_mean(b) self.sq_with_uncertainty_as_dict_no_numbers = { '@module': 'propnet.dbtools.storage', '@class': 'StorageQuantity', 'internal_id': self.quantity_with_uncertainty._internal_id, 'data_type': 'NumQuantity', 'symbol_type': symbols['B'], 'units': 'dimensionless', 'provenance': ProvenanceStore.from_provenance_element( self.quantity_with_uncertainty.provenance), 'tags': []} provenances_json = { '@module': 'propnet.dbtools.storage', '@class': 'ProvenanceStore', 'model': 'aggregation', 'inputs': [ {'@module': 'propnet.dbtools.storage', '@class': 'ProvenanceStoreQuantity', 'data_type': 'NumQuantity', 'symbol_type': self.custom_symbols_json['B'], 'internal_id': b['internal_id'], 'tags': [], 'provenance': b['provenance']} for b in self.sq_custom_sym_json['B']], 'source': self.quantity_with_uncertainty.provenance.source } self.sq_with_uncertainty_json_no_numbers = copy.deepcopy(self.sq_with_uncertainty_as_dict_no_numbers) self.sq_with_uncertainty_json_no_numbers.update({"symbol_type": self.custom_symbols_json['B'], "provenance": provenances_json}) self.sq_with_uncertainty_numbers = {"value": 42.0, "uncertainty": 4.0} obj_symbol = symbols['C'] self.object_quantity = QuantityFactory.create_quantity(obj_symbol, "Test string") self.sq_object_as_dict = copy.deepcopy(self.sq_custom_sym_as_dicts['A'][0]) self.sq_object_as_dict.update({ "data_type": "ObjQuantity", "symbol_type": symbols['C'], "internal_id": self.object_quantity._internal_id, "value": "Test string", "units": None, "provenance": ProvenanceStore.from_provenance_element(self.object_quantity.provenance) }) self.sq_object_json = copy.deepcopy(self.sq_object_as_dict) self.sq_object_json.update( {"symbol_type": self.custom_syms_as_dicts['C'], "provenance": {'@module': 'propnet.dbtools.storage', '@class': 'ProvenanceStore', 'model': None, 'inputs': None, 'source': self.object_quantity.provenance.source}} ) # This setting allows dict differences to be shown in full self.maxDiff = None