Ejemplo n.º 1
0
    def test_process_item(self):
        test_props = [['band_gap_pbe', 'atomic_density'],
                      ['bulk_modulus', 'vickers_hardness']]
        linlsq_correlation_values = [0.011294932700383722, 0.49302857395714916]
        path_lengths = [None, 2]

        for source_db, is_quantity_db in zip(
            (self.propstore, self.quantity_store), (False, True)):
            for props, expected_correlation_val, expected_path_length in \
                    zip(test_props, linlsq_correlation_values, path_lengths):
                builder = CorrelationBuilder(source_db,
                                             self.correlation,
                                             props=props,
                                             from_quantity_db=is_quantity_db)
                processed = None
                prop_x, prop_y = props
                for item in builder.get_items():
                    if item['x_name'] == prop_x and \
                            item['y_name'] == prop_y:
                        processed = builder.process_item(item)
                        break
                # print(processed)
                self.assertIsNotNone(processed)
                self.assertIsInstance(processed, tuple)
                px, py, correlation, func_name, n_points, path_length = processed
                self.assertEqual(px, prop_x)
                self.assertEqual(py, prop_y)
                self.assertAlmostEqual(correlation, expected_correlation_val)
                self.assertEqual(func_name, 'linlsq')
                self.assertEqual(n_points, 200)
                self.assertEqual(path_length, expected_path_length)
Ejemplo n.º 2
0
    def test_correlation_funcs(self):
        def custom_correlation_func(x, y):
            return 0.5

        correlation_values = {
            k: v
            for k, v in self.correlation_values_bulk_vickers.items()
        }
        correlation_values['test_correlation.custom_correlation_func'] = 0.5

        builder = CorrelationBuilder(
            self.propstore,
            self.correlation,
            props=['vickers_hardness', 'bulk_modulus'],
            funcs=['all', custom_correlation_func],
            from_quantity_db=False)

        self.assertEqual(
            set(builder._funcs.keys()),
            set(correlation_values.keys()),
            msg="Are there new built-in functions in the correlation builder?")

        for item in builder.get_items():
            if item['x_name'] == 'bulk_modulus' and \
                    item['y_name'] == 'vickers_hardness':
                processed = builder.process_item(item)
                self.assertIsInstance(processed, tuple)
                prop_x, prop_y, correlation, func_name, n_points, path_length = processed
                self.assertEqual(prop_x, 'bulk_modulus')
                self.assertEqual(prop_y, 'vickers_hardness')
                self.assertIn(func_name, correlation_values.keys())
                self.assertAlmostEqual(correlation,
                                       correlation_values[func_name])
                self.assertEqual(n_points, 200)
                self.assertEqual(path_length, 2)
Ejemplo n.º 3
0
    def test_process_item(self):
        test_props = [['band_gap_pbe', 'total_magnetization_normalized_vol'],
                      ['bulk_modulus', 'vickers_hardness']]
        linlsq_correlation_values = [0.03620401274778131, 0.4155837083845686]
        path_lengths = [None, 2]

        for props, expected_correlation_val, expected_path_length in \
                zip(test_props, linlsq_correlation_values, path_lengths):
            builder = CorrelationBuilder(self.propstore,
                                         self.materials,
                                         self.correlation,
                                         props=props)
            processed = None
            prop_x, prop_y = props
            for item in builder.get_items():
                if item['x_name'] == prop_x and \
                        item['y_name'] == prop_y:
                    processed = builder.process_item(item)
                    break

            self.assertIsNotNone(processed)
            self.assertIsInstance(processed, tuple)
            px, py, correlation, func_name, n_points, path_length = processed
            self.assertEqual(px, prop_x)
            self.assertEqual(py, prop_y)
            self.assertAlmostEqual(correlation, expected_correlation_val)
            self.assertEqual(func_name, 'linlsq')
            self.assertEqual(n_points, 200)
            self.assertEqual(path_length, expected_path_length)
Ejemplo n.º 4
0
    def test_database_write(self):
        builder = CorrelationBuilder(self.propstore,
                                     self.materials,
                                     self.correlation,
                                     props=self.propnet_props + self.mp_props,
                                     funcs='all')

        runner = Runner([builder])
        runner.run()

        data = list(self.correlation.query(criteria={}))
        # count = n_props**2 * n_funcs
        # n_props = 4, n_funcs = 5
        self.assertEqual(len(data), 80)

        for d in data:
            self.assertIsInstance(d, dict)
            self.assertEqual(
                set(d.keys()), {
                    'property_x', 'property_y', 'correlation',
                    'correlation_func', 'n_points', 'shortest_path_length',
                    'id', '_id', 'last_updated'
                })
            self.assertEqual(d['n_points'], 200)
            if d['property_x'] == 'vickers_hardness' and \
                    d['property_y'] == 'bulk_modulus':
                self.assertAlmostEqual(
                    d['correlation'], self.correlation_values_vickers_bulk[
                        d['correlation_func']])
            elif d['property_x'] == 'bulk_modulus' and \
                    d['property_y'] == 'vickers_hardness':
                self.assertAlmostEqual(
                    d['correlation'], self.correlation_values_bulk_vickers[
                        d['correlation_func']])
Ejemplo n.º 5
0
def get_graph_data(x_prop, y_prop, z_prop, color_prop):
    props_to_get = set(prop for prop in (x_prop, y_prop, z_prop, color_prop)
                       if prop is not None)

    query_data = CorrelationBuilder.get_data_from_quantity_db(store, *props_to_get,
                                                              include_id=True)

    if any(prop not in query_data for prop in props_to_get) or \
            any(len(query_data[prop]) == 0 for prop in props_to_get):
        return {'query_success': False}

    data = {
        'query_success': True,
        'x': query_data[x_prop],
        'x_name': x_prop,
        'y': query_data[y_prop],
        'y_name': y_prop,
        'mpids': query_data['_id'],
        'zoom': None,
        'color_range': None
    }
    if z_prop:
        data.update({'z': query_data[z_prop], 'z_name': z_prop, 'z_enabled': True})
    if color_prop:
        data.update({'color': query_data[color_prop], 'color_name': color_prop, 'color_enabled': True})

    return data
Ejemplo n.º 6
0
 def test_serial_runner_quantity_db(self):
     # This only runs over the 4 properties in the database because
     # the mongomock db cannot be indexed and is therefore very slow
     builder = CorrelationBuilder(self.quantity_store,
                                  self.correlation,
                                  props=self.propnet_props,
                                  from_quantity_db=True)
     runner = Runner([builder])
     runner.run()
Ejemplo n.º 7
0
    def test_sample_size_limit(self):
        sample_sizes = [50, 300]
        expected_n_points = [50, 200]

        for sample_size, n_points in zip(sample_sizes, expected_n_points):
            correlation_store = MemoryStore()
            builder = CorrelationBuilder(
                self.propstore,
                correlation_store,
                props=['bulk_modulus', 'vickers_hardness'],
                funcs='linlsq',
                sample_size=sample_size,
                from_quantity_db=False)
            runner = Runner([builder])
            runner.run()

            data = list(correlation_store.query(criteria={}))
            for d in data:
                self.assertEqual(d['n_points'], n_points)

        with self.assertRaises(ValueError):
            _ = CorrelationBuilder(self.propstore,
                                   self.correlation,
                                   sample_size=1)
Ejemplo n.º 8
0
def _get_correlation_values():
    full_propstore = MemoryStore()
    with open(os.path.join(CORR_TEST_DIR, "correlation_propnet_data.json"),
              'r') as f:
        data = json.load(f)
    full_propstore.connect()
    full_propstore.update(jsanitize(data, strict=True, allow_bson=True))
    correlation_store = MemoryStore()
    builder = CorrelationBuilder(full_propstore,
                                 correlation_store,
                                 props=PROPNET_PROPS,
                                 funcs='all',
                                 from_quantity_db=False)
    runner = Runner([builder])
    runner.run()
    return builder
Ejemplo n.º 9
0
 def test_multiproc_runner(self):
     builder = CorrelationBuilder(self.propstore,
                                  self.correlation,
                                  from_quantity_db=False)
     runner = Runner([builder], max_workers=4)
     runner.run()
Ejemplo n.º 10
0
 def test_serial_runner(self):
     builder = CorrelationBuilder(self.propstore,
                                  self.correlation,
                                  from_quantity_db=False)
     runner = Runner([builder])
     runner.run()
Ejemplo n.º 11
0
    def test_database_and_file_write(self):
        builder = CorrelationBuilder(self.propstore,
                                     self.correlation,
                                     props=self.propnet_props,
                                     funcs='all',
                                     out_file=os.path.join(
                                         TEST_DATA_DIR, "test_output.json"),
                                     from_quantity_db=False)

        runner = Runner([builder])
        runner.run()

        # Test database output
        data = list(self.correlation.query(criteria={}))
        # count = n_props**2 * n_funcs
        # n_props = 4, n_funcs = 6
        self.assertEqual(len(data),
                         96,
                         msg="Are there new built-in funcs in the builder?")

        for d in data:
            self.assertIsInstance(d, dict)
            self.assertEqual(
                set(d.keys()), {
                    'property_x', 'property_y', 'correlation',
                    'correlation_func', 'n_points', 'shortest_path_length',
                    'id', '_id', 'last_updated'
                })
            self.assertEqual(d['n_points'], 200)
            if d['property_x'] == 'vickers_hardness' and \
                    d['property_y'] == 'bulk_modulus':
                # print("{}: {}".format(d['correlation_func'], d['correlation']))
                self.assertAlmostEqual(
                    d['correlation'], self.correlation_values_vickers_bulk[
                        d['correlation_func']])
            elif d['property_x'] == 'bulk_modulus' and \
                    d['property_y'] == 'vickers_hardness':
                # print("{}: {}".format(d['correlation_func'], d['correlation']))
                self.assertAlmostEqual(
                    d['correlation'], self.correlation_values_bulk_vickers[
                        d['correlation_func']])
        # Test file output
        expected_file_data = loadfn(
            os.path.join(TEST_DATA_DIR, 'correlation_outfile.json'))
        actual_file_data = loadfn(
            os.path.join(TEST_DATA_DIR, 'test_output.json'))

        self.assertIsInstance(actual_file_data, dict)
        self.assertEqual(actual_file_data.keys(), expected_file_data.keys())
        self.assertEqual(set(actual_file_data['properties']),
                         set(expected_file_data['properties']))

        expected_props = expected_file_data['properties']
        actual_props = actual_file_data['properties']

        for prop_x, prop_y in product(expected_props, repeat=2):
            iex, iey = expected_props.index(prop_x), expected_props.index(
                prop_y)
            iax, iay = actual_props.index(prop_x), actual_props.index(prop_y)

            self.assertEqual(actual_file_data['n_points'][iax][iay],
                             expected_file_data['n_points'][iex][iey])
            self.assertEqual(
                actual_file_data['shortest_path_length'][iax][iay],
                expected_file_data['shortest_path_length'][iex][iey])

            for f in builder._funcs.keys():
                self.assertAlmostEqual(
                    actual_file_data['correlation'][f][iax][iay],
                    expected_file_data['correlation'][f][iex][iey])
Ejemplo n.º 12
0
 def test_multiproc_runner(self):
     builder = CorrelationBuilder(self.propstore, self.materials,
                                  self.correlation)
     runner = Runner([builder], max_workers=2)
     runner.run()
Ejemplo n.º 13
0
 def test_serial_runner(self):
     builder = CorrelationBuilder(self.propstore, self.materials,
                                  self.correlation)
     runner = Runner([builder])
     runner.run()