Example #1
0
 def setUpClass(cls):
     add_builtin_models_to_registry()
     cls.materials = MemoryStore()
     cls.materials.connect()
     materials = loadfn(os.path.join(TEST_DATA_DIR, "test_materials.json"))
     materials = jsanitize(materials, strict=True, allow_bson=True)
     cls.materials.update(materials)
     cls.propstore = None
Example #2
0
    def test_models_have_unique_names(self):
        add_builtin_models_to_registry()
        from propnet.models.serialized import _EQUATION_MODEL_NAMES_LIST
        serialized_model_names_set = set(_EQUATION_MODEL_NAMES_LIST)
        serialized_model_names_list = _EQUATION_MODEL_NAMES_LIST.copy()
        for name in serialized_model_names_set:
            serialized_model_names_list.remove(name)
        self.assertTrue(len(serialized_model_names_list) == 0,
                        msg="Two or more equation models have the "
                            "same name(s): {}".format(serialized_model_names_list))

        from propnet.models.python import _PYTHON_MODEL_NAMES_LIST
        python_model_names_set = set(_PYTHON_MODEL_NAMES_LIST)
        python_model_names_list = _PYTHON_MODEL_NAMES_LIST.copy()
        for name in python_model_names_set:
            python_model_names_list.remove(name)
        self.assertTrue(len(python_model_names_list) == 0,
                        msg="Two or more python models have the "
                            "same name(s): {}".format(python_model_names_list))

        from propnet.models.composite import _COMPOSITE_MODEL_NAMES_LIST
        composite_model_names_set = set(_COMPOSITE_MODEL_NAMES_LIST)
        composite_model_names_list = _COMPOSITE_MODEL_NAMES_LIST.copy()
        for name in composite_model_names_set:
            composite_model_names_list.remove(name)
        self.assertTrue(len(composite_model_names_list) == 0,
                        msg="Two or more composite models have the "
                            "same name(s): {}".format(composite_model_names_list))

        all_models_set = set.union(serialized_model_names_set,
                                   python_model_names_set,
                                   composite_model_names_set)
        all_models_list = \
            _EQUATION_MODEL_NAMES_LIST + \
            _PYTHON_MODEL_NAMES_LIST + \
            _COMPOSITE_MODEL_NAMES_LIST
        for name in all_models_set:
            all_models_list.remove(name)

        sources = defaultdict(list)
        for name in all_models_list:
            for model_type, name_list in zip(('serialized', 'python', 'composite'),
                                             (_EQUATION_MODEL_NAMES_LIST,
                                              _PYTHON_MODEL_NAMES_LIST,
                                              _COMPOSITE_MODEL_NAMES_LIST)):
                if name in name_list:
                    sources[name].append(model_type)
        self.assertTrue(len(all_models_list) == 0,
                        msg="Two or more models of different types"
                            " have the same name {{name: [model types]}}:\n{}".format(dict(sources)))
Example #3
0
 def setUpClass(cls):
     add_builtin_models_to_registry()
     # This is the visible light dataset used in the propnet paper
     path = os.path.join(TEST_DATA_DIR, "vis_bg_ri_data.csv")
     test_data = pd.read_csv(path)
     graph = Graph()
     materials = [
         Material([QuantityFactory.create_quantity("band_gap", bg)])
         for bg in test_data['Band Gap']
     ]
     cls.evaluated = [graph.evaluate(mat) for mat in materials]
     cls.benchmarks = [{
         "refractive_index": n
     } for n in test_data['Refractive Index']]
Example #4
0
    def setUpClass(cls):
        add_builtin_models_to_registry()
        cls.propnet_props = PROPNET_PROPS

        cls.propstore = MemoryStore()
        cls.propstore.connect()
        materials_file = os.path.join(TEST_DATA_DIR,
                                      "correlation_propnet_data.json")
        if os.path.exists(materials_file):
            with open(materials_file, 'r') as f:
                materials = json.load(f)
            materials = jsanitize(materials, strict=True, allow_bson=True)
            cls.propstore.update(materials)

        cls.quantity_store = MemoryStore()
        cls.quantity_store.connect()
        quantities_file = os.path.join(
            TEST_DATA_DIR, "correlation_propnet_quantity_data.json")
        if os.path.exists(quantities_file):
            with open(quantities_file, 'r') as f:
                quantities = json.load(f)
            quantities = jsanitize(quantities, strict=True, allow_bson=True)
            cls.quantity_store.update(quantities, key='internal_id')

        cls.correlation = None

        # vickers hardness (x-axis) vs. bulk modulus (y-axis)
        cls.correlation_values_vickers_bulk = {
            'linlsq': 0.49302857395714916,
            'pearson': 0.7021599347421844,
            'spearman': 0.7658801470036752,
            'mic': 0.7069707677575771,
            'theilsen': 0.4498696692135892,
            'ransac': 0.412850153689996
        }

        cls.correlation_values_bulk_vickers = {
            'linlsq': 0.49302857395714916,
            'pearson': 0.7021599347421844,
            'spearman': 0.7658801470036752,
            'mic': 0.7069707677575771,
            'theilsen': 0.46461142885951223,
            'ransac': 0.3699261228882036
        }
Example #5
0
 def setUpClass(cls):
     Registry.clear_all_registries()
     add_builtin_models_to_registry()
Example #6
0
 def tearDownClass(cls) -> None:
     Registry.clear_all_registries()
     add_builtin_models_to_registry()
Example #7
0
 def tearDownClass(cls) -> None:
     Registry.clear_all_registries()
     from propnet.models import add_builtin_models_to_registry
     add_builtin_models_to_registry()
Example #8
0
 def setUpClass(cls):
     add_builtin_models_to_registry()
     cls.mpr = mpr
     mpid = 'mp-1153'
     cls.mat = cls.mpr.get_material_for_mpid(mpid)
Example #9
0
    def setUpClass(cls):
        add_builtin_models_to_registry()
        # Inspiration was taken from the GraphTest class
        # I tried to construct the dictionaries for comparison
        # without writing out every one explicitly by reusing
        # information where it was applicable.
        # If this is too unreadable, can change to writing it
        # out explicitly in a JSON file and importing it. Would
        # still need to replace some fields dynamically.
        symbols = StorageTest.generate_symbols()

        cls.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,
                'is_builtin': False} for k in ['A', 'B', 'C']
        }
        cls.custom_syms_as_dicts['C'].update(
            {"units": None,
             "shape": None,
             "object_type": "str",
             "category": "object"})

        cls.custom_symbols_json = copy.deepcopy(cls.custom_syms_as_dicts)
        for k in ['A', 'B']:
            cls.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]]))]
        cls.quantities_custom_symbol = {"A": a,
                                         "B": b}

        cls.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 cls.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': cls.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'])]

        cls.sq_custom_sym_json = copy.deepcopy(cls.sq_custom_sym_as_dicts)
        for sym in ['A', 'B']:
            for q, p in zip(cls.sq_custom_sym_json[sym], provenances_json[sym]):
                q['symbol_type'] = cls.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 = Registry("models")['band_gap_refractive_index_moss']
        refractive_indices = [bg_ri_model.evaluate({"Eg": bg}).pop('refractive_index') for bg in band_gaps]

        cls.quantities_canonical_symbol = {"band_gaps": band_gaps,
                                            "refractive_indices": refractive_indices}

        cls.sq_canonical_sym_as_dicts_no_value = copy.deepcopy(cls.sq_custom_sym_as_dicts)
        cls.sq_canonical_sym_as_dicts_no_value['band_gaps'] = cls.sq_canonical_sym_as_dicts_no_value.pop('A')
        cls.sq_canonical_sym_as_dicts_no_value['refractive_indices'] = cls.sq_canonical_sym_as_dicts_no_value.pop('B')

        for d, sq in zip(cls.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(cls.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')

        cls.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)
        ]

        cls.sq_canonical_sym_json_no_value = copy.deepcopy(cls.sq_canonical_sym_as_dicts_no_value)

        for sym in ["band_gaps", "refractive_indices"]:
            for q, p in zip(cls.sq_canonical_sym_json_no_value[sym], provenances_json[sym]):
                q['provenance'] = p

        cls.quantity_with_uncertainty = NumQuantity.from_weighted_mean(b)
        cls.sq_with_uncertainty_as_dict_no_numbers = {
            '@module': 'propnet.dbtools.storage',
            '@class': 'StorageQuantity',
            'internal_id': cls.quantity_with_uncertainty._internal_id,
            'data_type': 'NumQuantity',
            'symbol_type': symbols['B'],
            'units': 'dimensionless',
            'provenance': ProvenanceStore.from_provenance_element(
                cls.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': cls.custom_symbols_json['B'],
                 'internal_id': b['internal_id'],
                 'tags': [],
                 'provenance': b['provenance']}
                for b in cls.sq_custom_sym_json['B']],
            'source': cls.quantity_with_uncertainty.provenance.source
        }

        cls.sq_with_uncertainty_json_no_numbers = copy.deepcopy(cls.sq_with_uncertainty_as_dict_no_numbers)
        cls.sq_with_uncertainty_json_no_numbers.update({"symbol_type": cls.custom_symbols_json['B'],
                                                         "provenance": provenances_json})
        cls.sq_with_uncertainty_numbers = {"value": 42.0,
                                            "uncertainty": 4.0}

        obj_symbol = symbols['C']
        cls.object_quantity = QuantityFactory.create_quantity(obj_symbol, "Test string")
        cls.sq_object_as_dict = copy.deepcopy(cls.sq_custom_sym_as_dicts['A'][0])
        cls.sq_object_as_dict.update({
            "data_type": "ObjQuantity",
            "symbol_type": symbols['C'],
            "internal_id": cls.object_quantity._internal_id,
            "value": "Test string",
            "units": None,
            "provenance": ProvenanceStore.from_provenance_element(cls.object_quantity.provenance)
        })
        cls.sq_object_json = copy.deepcopy(cls.sq_object_as_dict)
        cls.sq_object_json.update(
            {"symbol_type": cls.custom_syms_as_dicts['C'],
             "provenance": {'@module': 'propnet.dbtools.storage',
                            '@class': 'ProvenanceStore',
                            'model': None,
                            'inputs': None,
                            'source': cls.object_quantity.provenance.source}}
        )

        # This setting allows dict differences to be shown in full
        cls.maxDiff = None