def test_serialization(self): BaTiO3_se_fpath = os.path.join( self.TEST_FILES_DIR, "chemenv", "structure_environments_files", "se_mp-5020.json", ) with open(BaTiO3_se_fpath) as f: dd = json.load(f) se = StructureEnvironments.from_dict(dd) lse = LightStructureEnvironments.from_structure_environments( strategy=SimplestChemenvStrategy(), structure_environments=se ) cf = ConnectivityFinder() sc = cf.get_structure_connectivity(light_structure_environments=lse) sc_from_dict = StructureConnectivity.from_dict(sc.as_dict()) assert sc.light_structure_environments == sc_from_dict.light_structure_environments assert set(sc._graph.nodes()) == set(sc_from_dict._graph.nodes()) assert set(sc._graph.edges()) == set(sc_from_dict._graph.edges()) sc_from_json = StructureConnectivity.from_dict(json.loads(json.dumps(sc.as_dict()))) assert sc.light_structure_environments == sc_from_json.light_structure_environments assert set(sc._graph.nodes()) == set(sc_from_json._graph.nodes()) assert set(sc._graph.edges()) == set(sc_from_json._graph.edges()) if bson is not None: bson_data = bson.BSON.encode(sc.as_dict()) sc_from_bson = StructureConnectivity.from_dict(bson_data.decode()) assert sc.light_structure_environments == sc_from_bson.light_structure_environments assert set(sc._graph.nodes()) == set(sc_from_bson._graph.nodes()) assert set(sc._graph.edges()) == set(sc_from_bson._graph.edges())
def test_structure_environments_neighbors_sets(self): f = open("{}/{}".format(se_files_dir, 'se_mp-7000.json'), 'r') dd = json.load(f) f.close() se = StructureEnvironments.from_dict(dd) isite = 6 nb_set = se.neighbors_sets[isite][4][0] nb_set_surface_points = np.array([[1.0017922780870239, 0.99301365328679292], [1.0017922780870239, 0.0], [2.2237615554448569, 0.0], [2.2237615554448569, 0.016430233861405744], [2.25, 0.016430233861405744], [2.25, 0.99301365328679292]]) self.assertTrue(np.allclose(np.array(nb_set.voronoi_grid_surface_points()), nb_set_surface_points))
def test_read_write_structure_environments(self): f = open( "{}/{}".format(json_files_dir, "test_T--4_FePO4_icsd_4266.json"), "r") dd = json.load(f) f.close() atom_indices = dd["atom_indices"] struct = Structure.from_dict(dd["structure"]) self.lgf.setup_structure(struct) se = self.lgf.compute_structure_environments( only_indices=atom_indices, maximum_distance_factor=2.25, get_from_hints=True) f = open("tmp_dir/se.json", "w") json.dump(se.as_dict(), f) f.close() f = open("tmp_dir/se.json", "r") dd = json.load(f) f.close() se2 = StructureEnvironments.from_dict(dd) self.assertEqual(se, se2) strategy = SimplestChemenvStrategy() lse = LightStructureEnvironments.from_structure_environments( structure_environments=se, strategy=strategy, valences="undefined") f = open("tmp_dir/lse.json", "w") json.dump(lse.as_dict(), f) f.close() f = open("tmp_dir/lse.json", "r") dd = json.load(f) f.close() lse2 = LightStructureEnvironments.from_dict(dd) self.assertEqual(lse, lse2)
def test_read_structure_environments(self): f = open("{}/{}".format(json_files_dir, 'test_T--4_FePO4_icsd_4266.json'), 'r') dd = json.load(f) f.close() atom_indices = dd['atom_indices'] struct = Structure.from_dict(dd['structure']) self.lgf.setup_structure(struct) se = self.lgf.compute_structure_environments_detailed_voronoi(only_indices=atom_indices, maximum_distance_factor=2.25) f = open('tmp_dir/se.json', 'w') json.dump(se.as_dict(), f) f.close() f = open('tmp_dir/se.json', 'r') dd = json.load(f) f.close() se2 = StructureEnvironments.from_dict(dd) self.assertEqual(se, se2) _strategy = SimplestChemenvStrategy() light_se = LightStructureEnvironments(_strategy, se) f = open('tmp_dir/light_se.json', 'w') json.dump(light_se.as_dict(), f) f.close() f = open('tmp_dir/light_se.json', 'r') dd = json.load(f) f.close() light_se2 = LightStructureEnvironments.from_dict(dd) self.assertEqual(light_se._strategy, light_se2._strategy) self.assertEqual(light_se._structure, light_se2._structure) self.assertEqual(light_se._bva_valences, light_se2._bva_valences) self.assertEqual(light_se._coordination_environments, light_se2._coordination_environments) self.assertEqual(light_se._neighbors, light_se2._neighbors) self.assertEqual(light_se, light_se2)
def test_read_write_structure_environments(self): f = open( "{}/{}".format(json_files_dir, 'test_T--4_FePO4_icsd_4266.json'), 'r') dd = json.load(f) f.close() atom_indices = dd['atom_indices'] struct = Structure.from_dict(dd['structure']) self.lgf.setup_structure(struct) se = self.lgf.compute_structure_environments( only_indices=atom_indices, maximum_distance_factor=2.25) f = open('tmp_dir/se.json', 'w') json.dump(se.as_dict(), f) f.close() f = open('tmp_dir/se.json', 'r') dd = json.load(f) f.close() se2 = StructureEnvironments.from_dict(dd) self.assertEqual(se, se2) strategy = SimplestChemenvStrategy() lse = LightStructureEnvironments.from_structure_environments( structure_environments=se, strategy=strategy, valences='undefined') f = open('tmp_dir/lse.json', 'w') json.dump(lse.as_dict(), f) f.close() f = open('tmp_dir/lse.json', 'r') dd = json.load(f) f.close() lse2 = LightStructureEnvironments.from_dict(dd) self.assertEqual(lse, lse2)
def test_read_write_structure_environments(self): f = open("{}/{}".format(json_files_dir, 'test_T--4_FePO4_icsd_4266.json'), 'r') dd = json.load(f) f.close() atom_indices = dd['atom_indices'] struct = Structure.from_dict(dd['structure']) self.lgf.setup_structure(struct) se = self.lgf.compute_structure_environments(only_indices=atom_indices, maximum_distance_factor=2.25, get_from_hints=True) f = open('tmp_dir/se.json', 'w') json.dump(se.as_dict(), f) f.close() f = open('tmp_dir/se.json', 'r') dd = json.load(f) f.close() se2 = StructureEnvironments.from_dict(dd) self.assertEqual(se, se2) strategy = SimplestChemenvStrategy() lse = LightStructureEnvironments.from_structure_environments(structure_environments=se, strategy=strategy, valences='undefined') f = open('tmp_dir/lse.json', 'w') json.dump(lse.as_dict(), f) f.close() f = open('tmp_dir/lse.json', 'r') dd = json.load(f) f.close() lse2 = LightStructureEnvironments.from_dict(dd) self.assertEqual(lse, lse2)
def run_task(self, fw_spec): logging.basicConfig(filename='chemenv_light_structure_environments.log', format='%(levelname)s:%(module)s:%(funcName)s:%(message)s', level=logging.DEBUG) identifier = fw_spec['identifier'] criteria = {'identifier': identifier} # Where to get the full structure environments object se_database = fw_spec['structure_environments_database'] entry = se_database.collection.find_one(criteria) if fw_spec['structure_environments_setup'] == 'from_gridfs': gfs_fileobject = se_database.gridfs.get(entry['structure_environments']) dd = json.load(gfs_fileobject) se = StructureEnvironments.from_dict(dd) elif fw_spec['structure_environments_setup'] == 'from_storedfile': se_storage_server = fw_spec['se_storage_server'] se_filepath = entry['structure_environments_file'] se_storage_server.get(se_filepath, 'se.json') f = open('se.json', 'r') dd = json.load(f) f.close() se = StructureEnvironments.from_dict(dd) else: raise RuntimeError('Wrong structure_environments_setup : ' '"{}" is not allowed'.format(fw_spec['structure_environments_setup'])) # Compute the light structure environments chemenv_strategy = fw_spec['chemenv_strategy'] if 'valences' in fw_spec: valences = fw_spec['valences'] valences_origin = fw_spec['valences_origin'] else: valences = 'undefined' valences_origin = 'None' lse = LightStructureEnvironments.from_structure_environments(strategy=chemenv_strategy, structure_environments=se, valences=valences, valences_origin=valences_origin) # Write to json file if 'json_file' in fw_spec: json_file = fw_spec['json_file'] else: json_file = 'light_structure_environments.json' f = open(json_file, 'w') json.dump(lse.as_dict(), f) f.close() # Save to database if 'mongo_database' in fw_spec: database = fw_spec['mongo_database'] entry = {'identifier': identifier, 'elements': [elmt.symbol for elmt in lse.structure.composition.elements], 'nelements': len(lse.structure.composition.elements), 'pretty_formula': lse.structure.composition.reduced_formula, 'nsites': len(lse.structure), 'chemenv_statistics': lse.get_statistics(statistics_fields='ALL', bson_compatible=True) } saving_option = fw_spec['saving_option'] if saving_option == 'gridfs': gridfs_msonables = {'structure': lse.structure, 'light_structure_environments': lse} elif saving_option == 'storefile': gridfs_msonables = None if 'lse_prefix' in fw_spec: lse_prefix = fw_spec['lse_prefix'] if not lse_prefix.isalpha(): raise ValueError('Prefix for light_structure_environments file is "{}" ' 'while it should be alphabetic'.format(lse_prefix)) else: lse_prefix = '' if lse_prefix: lse_rfilename = '{}_{}.json'.format(lse_prefix, fw_spec['storefile_basename']) else: lse_rfilename = '{}.json'.format(fw_spec['storefile_basename']) lse_rfilepath = '{}/{}'.format(fw_spec['storefile_dirpath'], lse_rfilename) storage_server = fw_spec['storage_server'] storage_server.put(localpath=json_file, remotepath=lse_rfilepath, overwrite=True, makedirs=False) entry['light_structure_environments_file'] = lse_rfilepath else: raise ValueError('Saving option is "{}" while it should be ' '"gridfs" or "storefile"'.format(saving_option)) criteria = {'identifier': identifier} if database.collection.find(criteria).count() == 1: database.update_entry(query=criteria, entry_update=entry, gridfs_msonables=gridfs_msonables) else: database.insert_entry(entry=entry, gridfs_msonables=gridfs_msonables)
def test_structure_environments_neighbors_sets(self): f = open("{}/{}".format(se_files_dir, "se_mp-7000.json"), "r") dd = json.load(f) f.close() se = StructureEnvironments.from_dict(dd) isite = 6 nb_set = se.neighbors_sets[isite][4][0] nb_set_surface_points = np.array([ [1.0017922780870239, 0.99301365328679292], [1.0017922780870239, 0.0], [2.2237615554448569, 0.0], [2.2237615554448569, 0.0060837], [2.25, 0.0060837], [2.25, 0.99301365328679292], ]) self.assertTrue( np.allclose(np.array(nb_set.voronoi_grid_surface_points()), nb_set_surface_points)) neighb_sites = nb_set.neighb_sites coords = [ np.array([0.2443798, 1.80409653, -1.13218359]), np.array([1.44020353, 1.11368738, 1.13218359]), np.array([2.75513098, 2.54465207, -0.70467298]), np.array([0.82616785, 3.65833945, 0.70467298]), ] np.testing.assert_array_almost_equal(coords[0], neighb_sites[0].coords) np.testing.assert_array_almost_equal(coords[1], neighb_sites[1].coords) np.testing.assert_array_almost_equal(coords[2], neighb_sites[2].coords) np.testing.assert_array_almost_equal(coords[3], neighb_sites[3].coords) neighb_coords = nb_set.coords np.testing.assert_array_almost_equal(coords, neighb_coords[1:]) np.testing.assert_array_almost_equal( nb_set.structure[nb_set.isite].coords, neighb_coords[0]) normdist = nb_set.normalized_distances self.assertAlmostEqual( sorted(normdist), sorted([ 1.0017922783963027, 1.0017922780870239, 1.000000000503177, 1.0 ]), ) normang = nb_set.normalized_angles self.assertAlmostEqual( sorted(normang), sorted([ 0.9999999998419052, 1.0, 0.9930136530585189, 0.9930136532867929 ]), ) dist = nb_set.distances self.assertAlmostEqual( sorted(dist), sorted([ 1.6284399814843944, 1.6284399809816534, 1.6255265861208676, 1.6255265853029401, ]), ) ang = nb_set.angles self.assertAlmostEqual( sorted(ang), sorted([ 3.117389876236432, 3.117389876729275, 3.095610709498583, 3.0956107102102024, ]), ) nb_set_info = nb_set.info self.assertAlmostEqual(nb_set_info["normalized_angles_mean"], 0.996506826547) self.assertAlmostEqual(nb_set_info["normalized_distances_std"], 0.000896138995037) self.assertAlmostEqual(nb_set_info["angles_std"], 0.0108895833142) self.assertAlmostEqual(nb_set_info["distances_std"], 0.00145669776056) self.assertAlmostEqual(nb_set_info["distances_mean"], 1.62698328347) self.assertEqual( nb_set.__str__(), "Neighbors Set for site #6 :\n" " - Coordination number : 4\n" " - Voronoi indices : 1, 4, 5, 6\n", ) self.assertFalse(nb_set.__ne__(nb_set)) self.assertEqual(nb_set.__hash__(), 4)
def test_structure_environments(self): with ScratchDir("."): f = open("{}/{}".format(se_files_dir, 'se_mp-7000.json'), 'r') dd = json.load(f) f.close() se = StructureEnvironments.from_dict(dd) isite = 6 csm_and_maps_fig, csm_and_maps_subplot = se.get_csm_and_maps( isite=isite) np.testing.assert_array_almost_equal( csm_and_maps_subplot.lines[0].get_xydata().flatten(), [0.0, 0.53499332]) np.testing.assert_array_almost_equal( csm_and_maps_subplot.lines[1].get_xydata().flatten(), [1.0, 0.47026441]) np.testing.assert_array_almost_equal( csm_and_maps_subplot.lines[2].get_xydata().flatten(), [2.0, 0.00988778]) environments_figure, environments_subplot = se.get_environments_figure( isite=isite) np.testing.assert_array_almost_equal( np.array(environments_subplot.patches[0].get_xy()), [[1., 1.], [1., 0.99301365], [1.00179228, 0.99301365], [1.00179228, 1.], [1., 1.]]) np.testing.assert_array_almost_equal( np.array(environments_subplot.patches[1].get_xy()), [[1., 0.99301365], [1., 0.], [1.00179228, 0.], [1.00179228, 0.99301365], [1., 0.99301365]]) np.testing.assert_array_almost_equal( np.array(environments_subplot.patches[2].get_xy()), [[1.00179228, 1.], [1.00179228, 0.99301365], [2.25, 0.99301365], [2.25, 1.], [1.00179228, 1.]]) np.testing.assert_array_almost_equal( np.array(environments_subplot.patches[3].get_xy()), [[1.00179228, 0.99301365], [1.00179228, 0.], [2.22376156, 0.], [2.22376156, 0.0060837], [2.25, 0.0060837], [2.25, 0.99301365], [1.00179228, 0.99301365]]) np.testing.assert_array_almost_equal( np.array(environments_subplot.patches[4].get_xy()), [[2.22376156, 0.0060837], [2.22376156, 0.], [2.25, 0.], [2.25, 0.0060837], [2.22376156, 0.0060837]]) se.save_environments_figure(isite=isite, imagename='image.png') self.assertTrue(os.path.exists('image.png')) self.assertEqual(len(se.differences_wrt(se)), 0) self.assertFalse(se.__ne__(se)) ce = se.ce_list[isite][4][0] self.assertTrue(ce.__len__(), 4) symbol, mingeom = ce.minimum_geometry( symmetry_measure_type='csm_wocs_ctwocc') self.assertEqual(symbol, 'T:4') self.assertAlmostEqual(mingeom['symmetry_measure'], 0.00988778424054) np.testing.assert_array_almost_equal( mingeom['other_symmetry_measures']['rotation_matrix_wcs_csc'], [[ -0.8433079817973094, -0.19705747216466898, 0.5000000005010193 ], [0.4868840909509757, 0.11377118475194581, 0.8660254034951744], [ -0.22754236927612112, 0.9737681809261427, 1.3979531202869064e-13 ]]) self.assertEqual(mingeom['detailed_voronoi_index'], { 'index': 0, 'cn': 4 }) self.assertAlmostEqual( mingeom['other_symmetry_measures'] ['scaling_factor_wocs_ctwocc'], 1.6270605877934026) ce_string = ce.__str__() self.assertTrue('csm1 (with central site) : 0.00988' in ce_string) self.assertTrue( 'csm2 (without central site) : 0.00981' in ce_string) self.assertTrue('csm1 (with central site) : 12.987' in ce_string) self.assertTrue( 'csm2 (without central site) : 11.827' in ce_string) self.assertTrue('csm1 (with central site) : 32.466' in ce_string) self.assertTrue( 'csm2 (without central site) : 32.466' in ce_string) self.assertTrue('csm1 (with central site) : 34.644' in ce_string) self.assertTrue( 'csm2 (without central site) : 32.466' in ce_string) mingeoms = ce.minimum_geometries( symmetry_measure_type='csm_wocs_ctwocc', max_csm=12.0) self.assertEqual(len(mingeoms), 2) mingeoms = ce.minimum_geometries( symmetry_measure_type='csm_wocs_ctwcc', max_csm=12.0) self.assertEqual(len(mingeoms), 1) mingeoms = ce.minimum_geometries(n=3) self.assertEqual(len(mingeoms), 3) ce2 = se.ce_list[7][4][0] self.assertTrue(ce.is_close_to(ce2, rtol=0.01, atol=1e-4)) self.assertFalse(ce.is_close_to(ce2, rtol=0.0, atol=1e-8)) self.assertFalse(ce.__eq__(ce2)) self.assertTrue(ce.__ne__(ce2))
def test_light_structure_environments(self): with ScratchDir("."): f = open("{}/{}".format(se_files_dir, 'se_mp-7000.json'), 'r') dd = json.load(f) f.close() se = StructureEnvironments.from_dict(dd) strategy = SimplestChemenvStrategy() lse = LightStructureEnvironments.from_structure_environments( structure_environments=se, strategy=strategy, valences='undefined') isite = 6 nb_set = lse.neighbors_sets[isite][0] neighb_coords = [ np.array([0.2443798, 1.80409653, -1.13218359]), np.array([1.44020353, 1.11368738, 1.13218359]), np.array([2.75513098, 2.54465207, -0.70467298]), np.array([0.82616785, 3.65833945, 0.70467298]) ] neighb_indices = [0, 3, 5, 1] neighb_images = [[0, 0, -1], [0, 0, 0], [0, 0, -1], [0, 0, 0]] np.testing.assert_array_almost_equal(neighb_coords, nb_set.neighb_coords) np.testing.assert_array_almost_equal( neighb_coords, [s.coords for s in nb_set.neighb_sites]) nb_sai = nb_set.neighb_sites_and_indices np.testing.assert_array_almost_equal( neighb_coords, [sai['site'].coords for sai in nb_sai]) np.testing.assert_array_almost_equal( neighb_indices, [sai['index'] for sai in nb_sai]) nb_iai = nb_set.neighb_indices_and_images np.testing.assert_array_almost_equal( neighb_indices, [iai['index'] for iai in nb_iai]) np.testing.assert_array_equal( neighb_images, [iai['image_cell'] for iai in nb_iai]) self.assertEqual(nb_set.__len__(), 4) self.assertEqual(nb_set.__hash__(), 4) self.assertFalse(nb_set.__ne__(nb_set)) self.assertEqual( nb_set.__str__(), 'Neighbors Set for site #6 :\n' ' - Coordination number : 4\n' ' - Neighbors sites indices : 0, 1, 2, 3\n') stats = lse.get_statistics() neighbors = lse.strategy.get_site_neighbors( site=lse.structure[isite]) self.assertArrayAlmostEqual( neighbors[0].coords, np.array([0.2443798, 1.80409653, -1.13218359])) self.assertArrayAlmostEqual( neighbors[1].coords, np.array([1.44020353, 1.11368738, 1.13218359])) self.assertArrayAlmostEqual( neighbors[2].coords, np.array([2.75513098, 2.54465207, -0.70467298])) self.assertArrayAlmostEqual( neighbors[3].coords, np.array([0.82616785, 3.65833945, 0.70467298])) equiv_site_index_and_transform = lse.strategy.equivalent_site_index_and_transform( neighbors[0]) self.assertEqual(equiv_site_index_and_transform[0], 0) self.assertArrayAlmostEqual(equiv_site_index_and_transform[1], [0.0, 0.0, 0.0]) self.assertArrayAlmostEqual(equiv_site_index_and_transform[2], [0.0, 0.0, -1.0]) equiv_site_index_and_transform = lse.strategy.equivalent_site_index_and_transform( neighbors[1]) self.assertEqual(equiv_site_index_and_transform[0], 3) self.assertArrayAlmostEqual(equiv_site_index_and_transform[1], [0.0, 0.0, 0.0]) self.assertArrayAlmostEqual(equiv_site_index_and_transform[2], [0.0, 0.0, 0.0]) self.assertEqual(stats['atom_coordination_environments_present'], {'Si': { 'T:4': 3.0 }}) self.assertEqual(stats['coordination_environments_atom_present'], {'T:4': { 'Si': 3.0 }}) self.assertEqual( stats['fraction_atom_coordination_environments_present'], {'Si': { 'T:4': 1.0 }}) site_info_ce = lse.get_site_info_for_specie_ce(specie=Species( 'Si', 4), ce_symbol='T:4') np.testing.assert_array_almost_equal(site_info_ce['fractions'], [1.0, 1.0, 1.0]) np.testing.assert_array_almost_equal(site_info_ce['csms'], [ 0.009887784240541068, 0.009887786546730826, 0.009887787384385317 ]) self.assertEqual(site_info_ce['isites'], [6, 7, 8]) site_info_allces = lse.get_site_info_for_specie_allces( specie=Species('Si', 4)) self.assertEqual(site_info_allces['T:4'], site_info_ce) self.assertFalse(lse.contains_only_one_anion('I-')) self.assertFalse(lse.contains_only_one_anion_atom('I')) self.assertTrue( lse.site_contains_environment(isite=isite, ce_symbol='T:4')) self.assertFalse( lse.site_contains_environment(isite=isite, ce_symbol='S:4')) self.assertFalse( lse.structure_contains_atom_environment(atom_symbol='Si', ce_symbol='S:4')) self.assertTrue( lse.structure_contains_atom_environment(atom_symbol='Si', ce_symbol='T:4')) self.assertFalse( lse.structure_contains_atom_environment(atom_symbol='O', ce_symbol='T:4')) self.assertTrue(lse.uniquely_determines_coordination_environments) self.assertFalse(lse.__ne__(lse)) envs = lse.strategy.get_site_coordination_environments( lse.structure[6]) self.assertEqual(len(envs), 1) self.assertEqual(envs[0][0], 'T:4') multi_strategy = MultiWeightsChemenvStrategy.stats_article_weights_parameters( ) lse_multi = LightStructureEnvironments.from_structure_environments( strategy=multi_strategy, structure_environments=se, valences='undefined') self.assertAlmostEqual( lse_multi.coordination_environments[isite][0]['csm'], 0.009887784240541068) self.assertAlmostEqual( lse_multi.coordination_environments[isite][0]['ce_fraction'], 1.0) self.assertEqual( lse_multi.coordination_environments[isite][0]['ce_symbol'], 'T:4')
def test_delta_csms_weight(self): # Get the StructureEnvironments for K2NaNb2Fe7Si8H4O31 (mp-743972) with open(os.path.join(se_files_dir, "se_mp-743972.json")) as f: dd = json.load(f) se = StructureEnvironments.from_dict(dd) # Get neighbors sets for which we get the weights cn_maps = [(12, 3), (12, 2), (13, 2), (12, 0), (12, 1), (13, 0), (13, 1)] nbsets = {cn_map: se.neighbors_sets[0][cn_map[0]][cn_map[1]] for cn_map in cn_maps} effective_csm_estimator = { "function": "power2_inverse_decreasing", "options": {"max_csm": 8.0}, } weight_estimator = { "function": "smootherstep", "options": {"delta_csm_min": 0.5, "delta_csm_max": 3.0}, } symmetry_measure_type = "csm_wcs_ctwcc" delta_weight = DeltaCSMNbSetWeight( effective_csm_estimator=effective_csm_estimator, weight_estimator=weight_estimator, symmetry_measure_type=symmetry_measure_type, ) additional_info = {} cn_map = (12, 3) delta_w = delta_weight.weight( nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info, ) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) cn_map = (12, 2) delta_w = delta_weight.weight( nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info, ) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) cn_map = (12, 0) delta_w = delta_weight.weight( nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info, ) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) cn_map = (12, 1) delta_w = delta_weight.weight( nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info, ) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) cn_map = (13, 2) delta_w = delta_weight.weight( nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info, ) self.assertAlmostEqual(delta_w, 1.0, delta=1e-8) cn_map = (13, 0) delta_w = delta_weight.weight( nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info, ) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) cn_map = (13, 1) delta_w = delta_weight.weight( nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info, ) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) effective_csm_estimator = { "function": "power2_inverse_decreasing", "options": {"max_csm": 8.0}, } weight_estimator = { "function": "smootherstep", "options": {"delta_csm_min": -1.0, "delta_csm_max": 3.0}, } symmetry_measure_type = "csm_wcs_ctwcc" delta_weight = DeltaCSMNbSetWeight( effective_csm_estimator=effective_csm_estimator, weight_estimator=weight_estimator, symmetry_measure_type=symmetry_measure_type, ) additional_info = {} cn_map = (12, 3) delta_w = delta_weight.weight( nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info, ) self.assertAlmostEqual(delta_w, 0.040830741048481355, delta=1e-8) cn_map = (13, 2) delta_w = delta_weight.weight( nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info, ) self.assertAlmostEqual(delta_w, 1.0, delta=1e-8) cn_map = (13, 0) delta_w = delta_weight.weight( nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info, ) self.assertAlmostEqual(delta_w, 0.103515625, delta=1e-8) cn_map = (13, 1) delta_w = delta_weight.weight( nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info, ) self.assertAlmostEqual(delta_w, 0.103515625, delta=1e-8) # Get the StructureEnvironments for SiO2 (mp-7000) with open(os.path.join(se_files_dir, "se_mp-7000.json")) as f: dd = json.load(f) se = StructureEnvironments.from_dict(dd) # Get neighbors sets for which we get the weights cn_maps = [(2, 0), (4, 0)] nbsets = {cn_map: se.neighbors_sets[6][cn_map[0]][cn_map[1]] for cn_map in cn_maps} effective_csm_estimator = { "function": "power2_inverse_decreasing", "options": {"max_csm": 8.0}, } weight_estimator = { "function": "smootherstep", "options": {"delta_csm_min": 0.5, "delta_csm_max": 3.0}, } symmetry_measure_type = "csm_wcs_ctwcc" delta_weight = DeltaCSMNbSetWeight( effective_csm_estimator=effective_csm_estimator, weight_estimator=weight_estimator, symmetry_measure_type=symmetry_measure_type, ) additional_info = {} cn_map = (2, 0) delta_w = delta_weight.weight( nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info, ) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) cn_map = (4, 0) delta_w = delta_weight.weight( nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info, ) self.assertAlmostEqual(delta_w, 1.0, delta=1e-8)
def test_structure_environments(self): f = open("{}/{}".format(se_files_dir, 'se_mp-7000.json'), 'r') dd = json.load(f) f.close() se = StructureEnvironments.from_dict(dd) isite = 6 csm_and_maps_fig, csm_and_maps_subplot = se.get_csm_and_maps(isite=isite) np.testing.assert_array_almost_equal(csm_and_maps_subplot.lines[0].get_xydata().flatten(), [0.0, 0.53499332]) np.testing.assert_array_almost_equal(csm_and_maps_subplot.lines[1].get_xydata().flatten(), [1.0, 0.47026441]) np.testing.assert_array_almost_equal(csm_and_maps_subplot.lines[2].get_xydata().flatten(), [2.0, 0.00988778]) environments_figure, environments_subplot = se.get_environments_figure(isite=isite) np.testing.assert_array_almost_equal(np.array(environments_subplot.patches[0].get_xy()), [[1., 1.], [1., 0.99301365], [1.00179228, 0.99301365], [1.00179228, 1.], [1., 1.]]) np.testing.assert_array_almost_equal(np.array(environments_subplot.patches[1].get_xy()), [[1., 0.99301365], [1., 0.], [1.00179228, 0.], [1.00179228, 0.99301365], [1., 0.99301365]]) np.testing.assert_array_almost_equal(np.array(environments_subplot.patches[2].get_xy()), [[1.00179228, 1.], [1.00179228, 0.99301365], [2.25, 0.99301365], [2.25, 1.], [1.00179228, 1.]]) np.testing.assert_array_almost_equal(np.array(environments_subplot.patches[3].get_xy()), [[1.00179228, 0.99301365], [1.00179228, 0.], [2.22376156, 0.], [2.22376156, 0.0060837], [2.25, 0.0060837], [2.25, 0.99301365], [1.00179228, 0.99301365]]) np.testing.assert_array_almost_equal(np.array(environments_subplot.patches[4].get_xy()), [[2.22376156, 0.0060837], [2.22376156, 0.], [2.25, 0.], [2.25, 0.0060837], [2.22376156, 0.0060837]]) imgpath = os.path.join('tmp_dir', 'image.png') se.save_environments_figure(isite=isite, imagename=imgpath) self.assertTrue(os.path.exists(imgpath)) self.assertEqual(len(se.differences_wrt(se)), 0) self.assertFalse(se.__ne__(se)) ce = se.ce_list[isite][4][0] self.assertTrue(ce.__len__(), 4) symbol, mingeom = ce.minimum_geometry(symmetry_measure_type='csm_wocs_ctwocc') self.assertEqual(symbol, 'T:4') self.assertAlmostEqual(mingeom['symmetry_measure'], 0.00988778424054) np.testing.assert_array_almost_equal(mingeom['other_symmetry_measures']['rotation_matrix_wcs_csc'], [[-0.8433079817973094, -0.19705747216466898, 0.5000000005010193], [0.4868840909509757, 0.11377118475194581, 0.8660254034951744], [-0.22754236927612112, 0.9737681809261427, 1.3979531202869064e-13]]) self.assertEqual(mingeom['detailed_voronoi_index'], {'index': 0, 'cn': 4}) self.assertAlmostEqual(mingeom['other_symmetry_measures']['scaling_factor_wocs_ctwocc'], 1.6270605877934026) ce_string = ce.__str__() self.assertTrue('csm1 (with central site) : 0.00988' in ce_string) self.assertTrue('csm2 (without central site) : 0.00981' in ce_string) self.assertTrue('csm1 (with central site) : 12.987' in ce_string) self.assertTrue('csm2 (without central site) : 11.827' in ce_string) self.assertTrue('csm1 (with central site) : 32.466' in ce_string) self.assertTrue('csm2 (without central site) : 32.466' in ce_string) self.assertTrue('csm1 (with central site) : 34.644' in ce_string) self.assertTrue('csm2 (without central site) : 32.466' in ce_string) mingeoms = ce.minimum_geometries(symmetry_measure_type='csm_wocs_ctwocc', max_csm=12.0) self.assertEqual(len(mingeoms), 2) mingeoms = ce.minimum_geometries(symmetry_measure_type='csm_wocs_ctwcc', max_csm=12.0) self.assertEqual(len(mingeoms), 1) mingeoms = ce.minimum_geometries(n=3) self.assertEqual(len(mingeoms), 3) ce2 = se.ce_list[7][4][0] self.assertTrue(ce.is_close_to(ce2, rtol=0.01, atol=1e-4)) self.assertFalse(ce.is_close_to(ce2, rtol=0.0, atol=1e-8)) self.assertFalse(ce.__eq__(ce2)) self.assertTrue(ce.__ne__(ce2))
def test_light_structure_environments(self): f = open("{}/{}".format(se_files_dir, 'se_mp-7000.json'), 'r') dd = json.load(f) f.close() se = StructureEnvironments.from_dict(dd) strategy = SimplestChemenvStrategy() lse = LightStructureEnvironments.from_structure_environments(structure_environments=se, strategy=strategy, valences='undefined') isite = 6 nb_set = lse.neighbors_sets[isite][0] neighb_coords = [np.array([0.2443798, 1.80409653, -1.13218359]), np.array([1.44020353, 1.11368738, 1.13218359]), np.array([2.75513098, 2.54465207, -0.70467298]), np.array([0.82616785, 3.65833945, 0.70467298])] neighb_indices = [0, 3, 5, 1] neighb_images = [[0, 0, -1], [0, 0, 0], [0, 0, -1], [0, 0, 0]] np.testing.assert_array_almost_equal(neighb_coords, nb_set.neighb_coords) np.testing.assert_array_almost_equal(neighb_coords, [s.coords for s in nb_set.neighb_sites]) nb_sai = nb_set.neighb_sites_and_indices np.testing.assert_array_almost_equal(neighb_coords, [sai['site'].coords for sai in nb_sai]) np.testing.assert_array_almost_equal(neighb_indices, [sai['index'] for sai in nb_sai]) nb_iai = nb_set.neighb_indices_and_images np.testing.assert_array_almost_equal(neighb_indices, [iai['index'] for iai in nb_iai]) np.testing.assert_array_equal(neighb_images, [iai['image_cell'] for iai in nb_iai]) self.assertEqual(nb_set.__len__(), 4) self.assertEqual(nb_set.__hash__(), 4) self.assertFalse(nb_set.__ne__(nb_set)) self.assertEqual(nb_set.__str__(), 'Neighbors Set for site #6 :\n' ' - Coordination number : 4\n' ' - Neighbors sites indices : 0, 1, 2, 3\n') stats = lse.get_statistics() neighbors = lse.strategy.get_site_neighbors(site=lse.structure[isite]) self.assertArrayAlmostEqual(neighbors[0].coords, np.array([ 0.2443798, 1.80409653, -1.13218359])) self.assertArrayAlmostEqual(neighbors[1].coords, np.array([ 1.44020353, 1.11368738, 1.13218359])) self.assertArrayAlmostEqual(neighbors[2].coords, np.array([ 2.75513098, 2.54465207, -0.70467298])) self.assertArrayAlmostEqual(neighbors[3].coords, np.array([ 0.82616785, 3.65833945, 0.70467298])) equiv_site_index_and_transform = lse.strategy.equivalent_site_index_and_transform(neighbors[0]) self.assertEqual(equiv_site_index_and_transform[0], 0) self.assertArrayAlmostEqual(equiv_site_index_and_transform[1], [0.0, 0.0, 0.0]) self.assertArrayAlmostEqual(equiv_site_index_and_transform[2], [0.0, 0.0, -1.0]) equiv_site_index_and_transform = lse.strategy.equivalent_site_index_and_transform(neighbors[1]) self.assertEqual(equiv_site_index_and_transform[0], 3) self.assertArrayAlmostEqual(equiv_site_index_and_transform[1], [0.0, 0.0, 0.0]) self.assertArrayAlmostEqual(equiv_site_index_and_transform[2], [0.0, 0.0, 0.0]) self.assertEqual(stats['atom_coordination_environments_present'], {'Si': {'T:4': 3.0}}) self.assertEqual(stats['coordination_environments_atom_present'], {'T:4': {'Si': 3.0}}) self.assertEqual(stats['fraction_atom_coordination_environments_present'], {'Si': {'T:4': 1.0}}) site_info_ce = lse.get_site_info_for_specie_ce(specie=Specie('Si', 4), ce_symbol='T:4') np.testing.assert_array_almost_equal(site_info_ce['fractions'], [1.0, 1.0, 1.0]) np.testing.assert_array_almost_equal(site_info_ce['csms'], [0.009887784240541068, 0.009887786546730826, 0.009887787384385317]) self.assertEqual(site_info_ce['isites'], [6, 7, 8]) site_info_allces = lse.get_site_info_for_specie_allces(specie=Specie('Si', 4)) self.assertEqual(site_info_allces['T:4'], site_info_ce) self.assertFalse(lse.contains_only_one_anion('I-')) self.assertFalse(lse.contains_only_one_anion_atom('I')) self.assertTrue(lse.site_contains_environment(isite=isite, ce_symbol='T:4')) self.assertFalse(lse.site_contains_environment(isite=isite, ce_symbol='S:4')) self.assertFalse(lse.structure_contains_atom_environment(atom_symbol='Si', ce_symbol='S:4')) self.assertTrue(lse.structure_contains_atom_environment(atom_symbol='Si', ce_symbol='T:4')) self.assertFalse(lse.structure_contains_atom_environment(atom_symbol='O', ce_symbol='T:4')) self.assertTrue(lse.uniquely_determines_coordination_environments) self.assertFalse(lse.__ne__(lse)) envs = lse.strategy.get_site_coordination_environments(lse.structure[6]) self.assertEqual(len(envs), 1) self.assertEqual(envs[0][0], 'T:4') multi_strategy = MultiWeightsChemenvStrategy.stats_article_weights_parameters() lse_multi = LightStructureEnvironments.from_structure_environments(strategy=multi_strategy, structure_environments=se, valences='undefined') self.assertAlmostEqual(lse_multi.coordination_environments[isite][0]['csm'], 0.009887784240541068) self.assertAlmostEqual(lse_multi.coordination_environments[isite][0]['ce_fraction'], 1.0) self.assertEqual(lse_multi.coordination_environments[isite][0]['ce_symbol'], 'T:4')
def test_coordination_sequences(self): BaTiO3_se_fpath = os.path.join( self.TEST_FILES_DIR, "chemenv", "structure_environments_files", "se_mp-5020.json", ) with open(BaTiO3_se_fpath, "r") as f: dd = json.load(f) se = StructureEnvironments.from_dict(dd) lse = LightStructureEnvironments.from_structure_environments( strategy=SimplestChemenvStrategy(), structure_environments=se) cf = ConnectivityFinder() sc = cf.get_structure_connectivity(light_structure_environments=lse) ccs_oct = sc.get_connected_components(environments_symbols=["O:6"]) ccs_all = sc.get_connected_components( environments_symbols=["O:6", "C:12"]) assert len(ccs_oct) == 1 assert len(ccs_all) == 1 cc_oct = ccs_oct[0] cc_all = ccs_all[0] cc_oct_node = list(cc_oct.graph.nodes())[0] cseq = cc_oct.coordination_sequence(source_node=cc_oct_node, path_size=6) assert cseq == {1: 6, 2: 18, 3: 38, 4: 66, 5: 102, 6: 146} cc_all_oct_node = next(n for n in cc_all.graph.nodes() if n.coordination_environment == "O:6") cc_all_cuboct_node = next(n for n in cc_all.graph.nodes() if n.coordination_environment == "C:12") cseq = cc_all.coordination_sequence(source_node=cc_all_oct_node, path_size=6) assert cseq == {1: 14, 2: 74, 3: 218, 4: 442, 5: 746, 6: 1130} cseq = cc_all.coordination_sequence(source_node=cc_all_cuboct_node, path_size=6) assert cseq == {1: 26, 2: 122, 3: 298, 4: 554, 5: 890, 6: 1306} cseq = cc_all.coordination_sequence(source_node=cc_all_cuboct_node, path_size=6, include_source=True) assert cseq == {0: 1, 1: 26, 2: 122, 3: 298, 4: 554, 5: 890, 6: 1306} cseq = cc_all.coordination_sequence(source_node=cc_all_oct_node, path_size=4, coordination="env:number") assert cseq == { 1: { "O:6": 6, "C:12": 8 }, 2: { "O:6": 26, "C:12": 48 }, 3: { "O:6": 90, "C:12": 128 }, 4: { "O:6": 194, "C:12": 248 }, } cseq = cc_all.coordination_sequence(source_node=cc_all_cuboct_node, path_size=4, coordination="env:number") assert cseq == { 1: { "O:6": 8, "C:12": 18 }, 2: { "O:6": 48, "C:12": 74 }, 3: { "O:6": 128, "C:12": 170 }, 4: { "O:6": 248, "C:12": 306 }, } cseq = cc_all.coordination_sequence( source_node=cc_all_cuboct_node, path_size=4, coordination="env:number", include_source=True, ) assert cseq == { 0: { "C:12": 1 }, 1: { "O:6": 8, "C:12": 18 }, 2: { "O:6": 48, "C:12": 74 }, 3: { "O:6": 128, "C:12": 170 }, 4: { "O:6": 248, "C:12": 306 }, }
def test_light_structure_environments(self): with ScratchDir("."): with open(f"{se_files_dir}/se_mp-7000.json") as f: dd = json.load(f) se = StructureEnvironments.from_dict(dd) strategy = SimplestChemenvStrategy() lse = LightStructureEnvironments.from_structure_environments( structure_environments=se, strategy=strategy, valences="undefined") isite = 6 nb_set = lse.neighbors_sets[isite][0] neighb_coords = [ np.array([0.2443798, 1.80409653, -1.13218359]), np.array([1.44020353, 1.11368738, 1.13218359]), np.array([2.75513098, 2.54465207, -0.70467298]), np.array([0.82616785, 3.65833945, 0.70467298]), ] neighb_indices = [0, 3, 5, 1] neighb_images = [[0, 0, -1], [0, 0, 0], [0, 0, -1], [0, 0, 0]] np.testing.assert_array_almost_equal(neighb_coords, nb_set.neighb_coords) np.testing.assert_array_almost_equal( neighb_coords, [s.coords for s in nb_set.neighb_sites]) nb_sai = nb_set.neighb_sites_and_indices np.testing.assert_array_almost_equal( neighb_coords, [sai["site"].coords for sai in nb_sai]) np.testing.assert_array_almost_equal( neighb_indices, [sai["index"] for sai in nb_sai]) nb_iai = nb_set.neighb_indices_and_images np.testing.assert_array_almost_equal( neighb_indices, [iai["index"] for iai in nb_iai]) np.testing.assert_array_equal( neighb_images, [iai["image_cell"] for iai in nb_iai]) self.assertEqual(nb_set.__len__(), 4) self.assertEqual(nb_set.__hash__(), 4) self.assertFalse(nb_set.__ne__(nb_set)) self.assertEqual( nb_set.__str__(), "Neighbors Set for site #6 :\n" " - Coordination number : 4\n" " - Neighbors sites indices : 0, 1, 2, 3\n", ) stats = lse.get_statistics() neighbors = lse.strategy.get_site_neighbors( site=lse.structure[isite]) self.assertArrayAlmostEqual( neighbors[0].coords, np.array([0.2443798, 1.80409653, -1.13218359])) self.assertArrayAlmostEqual( neighbors[1].coords, np.array([1.44020353, 1.11368738, 1.13218359])) self.assertArrayAlmostEqual( neighbors[2].coords, np.array([2.75513098, 2.54465207, -0.70467298])) self.assertArrayAlmostEqual( neighbors[3].coords, np.array([0.82616785, 3.65833945, 0.70467298])) equiv_site_index_and_transform = lse.strategy.equivalent_site_index_and_transform( neighbors[0]) self.assertEqual(equiv_site_index_and_transform[0], 0) self.assertArrayAlmostEqual(equiv_site_index_and_transform[1], [0.0, 0.0, 0.0]) self.assertArrayAlmostEqual(equiv_site_index_and_transform[2], [0.0, 0.0, -1.0]) equiv_site_index_and_transform = lse.strategy.equivalent_site_index_and_transform( neighbors[1]) self.assertEqual(equiv_site_index_and_transform[0], 3) self.assertArrayAlmostEqual(equiv_site_index_and_transform[1], [0.0, 0.0, 0.0]) self.assertArrayAlmostEqual(equiv_site_index_and_transform[2], [0.0, 0.0, 0.0]) self.assertEqual(stats["atom_coordination_environments_present"], {"Si": { "T:4": 3.0 }}) self.assertEqual(stats["coordination_environments_atom_present"], {"T:4": { "Si": 3.0 }}) self.assertEqual( stats["fraction_atom_coordination_environments_present"], {"Si": { "T:4": 1.0 }}, ) site_info_ce = lse.get_site_info_for_specie_ce(specie=Species( "Si", 4), ce_symbol="T:4") np.testing.assert_array_almost_equal(site_info_ce["fractions"], [1.0, 1.0, 1.0]) np.testing.assert_array_almost_equal( site_info_ce["csms"], [ 0.009887784240541068, 0.009887786546730826, 0.009887787384385317 ], ) self.assertEqual(site_info_ce["isites"], [6, 7, 8]) site_info_allces = lse.get_site_info_for_specie_allces( specie=Species("Si", 4)) self.assertEqual(site_info_allces["T:4"], site_info_ce) self.assertFalse(lse.contains_only_one_anion("I-")) self.assertFalse(lse.contains_only_one_anion_atom("I")) self.assertTrue( lse.site_contains_environment(isite=isite, ce_symbol="T:4")) self.assertFalse( lse.site_contains_environment(isite=isite, ce_symbol="S:4")) self.assertFalse( lse.structure_contains_atom_environment(atom_symbol="Si", ce_symbol="S:4")) self.assertTrue( lse.structure_contains_atom_environment(atom_symbol="Si", ce_symbol="T:4")) self.assertFalse( lse.structure_contains_atom_environment(atom_symbol="O", ce_symbol="T:4")) self.assertTrue(lse.uniquely_determines_coordination_environments) self.assertFalse(lse.__ne__(lse)) envs = lse.strategy.get_site_coordination_environments( lse.structure[6]) self.assertEqual(len(envs), 1) self.assertEqual(envs[0][0], "T:4") multi_strategy = MultiWeightsChemenvStrategy.stats_article_weights_parameters( ) lse_multi = LightStructureEnvironments.from_structure_environments( strategy=multi_strategy, structure_environments=se, valences="undefined") self.assertAlmostEqual( lse_multi.coordination_environments[isite][0]["csm"], 0.009887784240541068, ) self.assertAlmostEqual( lse_multi.coordination_environments[isite][0]["ce_fraction"], 1.0) self.assertEqual( lse_multi.coordination_environments[isite][0]["ce_symbol"], "T:4")
def test_self_csms_weight(self): # Get the StructureEnvironments for K2NaNb2Fe7Si8H4O31 (mp-743972) f = open(os.path.join(se_files_dir, 'se_mp-743972.json'), 'r') dd = json.load(f) f.close() se = StructureEnvironments.from_dict(dd) # Get neighbors sets for which we get the weights cn_maps = [(12, 3), (12, 2), (13, 2), (12, 0), (12, 1)] nbsets = {cn_map: se.neighbors_sets[0][cn_map[0]][cn_map[1]] for cn_map in cn_maps} effective_csm_estimator = {'function': 'power2_inverse_decreasing', 'options': {'max_csm': 8.0}} weight_estimator = {'function': 'power2_decreasing_exp', 'options': {'max_csm': 8.0, 'alpha': 1.0}} weight_estimator2 = {'function': 'power2_decreasing_exp', 'options': {'max_csm': 8.1, 'alpha': 1.0}} symmetry_measure_type = 'csm_wcs_ctwcc' self_weight = SelfCSMNbSetWeight(effective_csm_estimator=effective_csm_estimator, weight_estimator=weight_estimator, symmetry_measure_type=symmetry_measure_type) self_weight2 = SelfCSMNbSetWeight(effective_csm_estimator=effective_csm_estimator, weight_estimator=weight_estimator2, symmetry_measure_type=symmetry_measure_type) self.assertNotEqual(self_weight, self_weight2) additional_info = {} cn_map = (12, 3) self_w = self_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(self_w, 0.11671945916431022, delta=1e-8) cn_map = (12, 2) self_w = self_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(self_w, 0.0, delta=1e-8) cn_map = (12, 0) self_w = self_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(self_w, 0.0, delta=1e-8) cn_map = (12, 1) self_w = self_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(self_w, 0.0, delta=1e-8) cn_map = (13, 2) self_w = self_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(self_w, 0.14204073172729198, delta=1e-8) # Get the StructureEnvironments for SiO2 (mp-7000) f = open(os.path.join(se_files_dir, 'se_mp-7000.json'), 'r') dd = json.load(f) f.close() se = StructureEnvironments.from_dict(dd) # Get neighbors sets for which we get the weights cn_maps = [(2, 0), (4, 0)] nbsets = {cn_map: se.neighbors_sets[6][cn_map[0]][cn_map[1]] for cn_map in cn_maps} effective_csm_estimator = {'function': 'power2_inverse_decreasing', 'options': {'max_csm': 8.0}} weight_estimator = {'function': 'power2_decreasing_exp', 'options': {'max_csm': 8.0, 'alpha': 1.0}} symmetry_measure_type = 'csm_wcs_ctwcc' self_weight = SelfCSMNbSetWeight(effective_csm_estimator=effective_csm_estimator, weight_estimator=weight_estimator, symmetry_measure_type=symmetry_measure_type) additional_info = {} cn_map = (2, 0) self_w = self_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(self_w, 0.8143992162836029, delta=1e-8) cn_map = (4, 0) self_w = self_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(self_w, 0.99629742352359496, delta=1e-8)
def test_delta_csms_weight(self): # Get the StructureEnvironments for K2NaNb2Fe7Si8H4O31 (mp-743972) f = open(os.path.join(se_files_dir, 'se_mp-743972.json'), 'r') dd = json.load(f) f.close() se = StructureEnvironments.from_dict(dd) # Get neighbors sets for which we get the weights cn_maps = [(12, 3), (12, 2), (13, 2), (12, 0), (12, 1), (13, 0), (13, 1)] nbsets = {cn_map: se.neighbors_sets[0][cn_map[0]][cn_map[1]] for cn_map in cn_maps} effective_csm_estimator = {'function': 'power2_inverse_decreasing', 'options': {'max_csm': 8.0}} weight_estimator = {'function': 'smootherstep', 'options': {'delta_csm_min': 0.5, 'delta_csm_max': 3.0}} symmetry_measure_type = 'csm_wcs_ctwcc' delta_weight = DeltaCSMNbSetWeight(effective_csm_estimator=effective_csm_estimator, weight_estimator=weight_estimator, symmetry_measure_type=symmetry_measure_type) additional_info = {} cn_map = (12, 3) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) cn_map = (12, 2) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) cn_map = (12, 0) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) cn_map = (12, 1) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) cn_map = (13, 2) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 1.0, delta=1e-8) cn_map = (13, 0) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) cn_map = (13, 1) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) effective_csm_estimator = {'function': 'power2_inverse_decreasing', 'options': {'max_csm': 8.0}} weight_estimator = {'function': 'smootherstep', 'options': {'delta_csm_min': -1.0, 'delta_csm_max': 3.0}} symmetry_measure_type = 'csm_wcs_ctwcc' delta_weight = DeltaCSMNbSetWeight(effective_csm_estimator=effective_csm_estimator, weight_estimator=weight_estimator, symmetry_measure_type=symmetry_measure_type) additional_info = {} cn_map = (12, 3) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.040830741048481355, delta=1e-8) cn_map = (13, 2) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 1.0, delta=1e-8) cn_map = (13, 0) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.103515625, delta=1e-8) cn_map = (13, 1) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.103515625, delta=1e-8) # Get the StructureEnvironments for SiO2 (mp-7000) f = open(os.path.join(se_files_dir, 'se_mp-7000.json'), 'r') dd = json.load(f) f.close() se = StructureEnvironments.from_dict(dd) # Get neighbors sets for which we get the weights cn_maps = [(2, 0), (4, 0)] nbsets = {cn_map: se.neighbors_sets[6][cn_map[0]][cn_map[1]] for cn_map in cn_maps} effective_csm_estimator = {'function': 'power2_inverse_decreasing', 'options': {'max_csm': 8.0}} weight_estimator = {'function': 'smootherstep', 'options': {'delta_csm_min': 0.5, 'delta_csm_max': 3.0}} symmetry_measure_type = 'csm_wcs_ctwcc' delta_weight = DeltaCSMNbSetWeight(effective_csm_estimator=effective_csm_estimator, weight_estimator=weight_estimator, symmetry_measure_type=symmetry_measure_type) additional_info = {} cn_map = (2, 0) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) cn_map = (4, 0) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 1.0, delta=1e-8)
def test_self_csms_weight(self): # Get the StructureEnvironments for K2NaNb2Fe7Si8H4O31 (mp-743972) f = open(os.path.join(se_files_dir, 'se_mp-743972.json'), 'r') dd = json.load(f) f.close() se = StructureEnvironments.from_dict(dd) # Get neighbors sets for which we get the weights cn_maps = [(12, 3), (12, 2), (13, 2), (12, 0), (12, 1)] nbsets = {cn_map: se.neighbors_sets[0][cn_map[0]][cn_map[1]] for cn_map in cn_maps} effective_csm_estimator = {'function': 'power2_inverse_decreasing', 'options': {'max_csm': 8.0}} weight_estimator = {'function': 'power2_decreasing_exp', 'options': {'max_csm': 8.0, 'alpha': 1.0}} weight_estimator2 = {'function': 'power2_decreasing_exp', 'options': {'max_csm': 8.1, 'alpha': 1.0}} symmetry_measure_type = 'csm_wcs_ctwcc' self_weight = SelfCSMNbSetWeight(effective_csm_estimator=effective_csm_estimator, weight_estimator=weight_estimator, symmetry_measure_type=symmetry_measure_type) self_weight2 = SelfCSMNbSetWeight(effective_csm_estimator=effective_csm_estimator, weight_estimator=weight_estimator2, symmetry_measure_type=symmetry_measure_type) self.assertNotEqual(self_weight, self_weight2) additional_info = {} cn_map = (12, 3) self_w = self_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(self_w, 0.11671945916431022, delta=1e-8) cn_map = (12, 2) self_w = self_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(self_w, 0.0, delta=1e-8) cn_map = (12, 0) self_w = self_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(self_w, 0.0, delta=1e-8) cn_map = (12, 1) self_w = self_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(self_w, 0.0, delta=1e-8) cn_map = (13, 2) self_w = self_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(self_w, 0.14204073172729198, delta=1e-8) # Get the StructureEnvironments for SiO2 (mp-7000) f = open(os.path.join(se_files_dir, 'se_mp-7000.json'), 'r') dd = json.load(f) f.close() se = StructureEnvironments.from_dict(dd) # Get neighbors sets for which we get the weights cn_maps = [(2, 0), (4, 0)] nbsets = {cn_map: se.neighbors_sets[6][cn_map[0]][cn_map[1]] for cn_map in cn_maps} effective_csm_estimator = {'function': 'power2_inverse_decreasing', 'options': {'max_csm': 8.0}} weight_estimator = {'function': 'power2_decreasing_exp', 'options': {'max_csm': 8.0, 'alpha': 1.0}} symmetry_measure_type = 'csm_wcs_ctwcc' self_weight = SelfCSMNbSetWeight(effective_csm_estimator=effective_csm_estimator, weight_estimator=weight_estimator, symmetry_measure_type=symmetry_measure_type) additional_info = {} cn_map = (2, 0) self_w = self_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(self_w, 0.8143992162836029, delta=1e-8) cn_map = (4, 0) self_w = self_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(self_w, 0.99629742352359496, delta=1e-8)
def test_structure_environments_neighbors_sets(self): f = open("{}/{}".format(se_files_dir, 'se_mp-7000.json'), 'r') dd = json.load(f) f.close() se = StructureEnvironments.from_dict(dd) isite = 6 nb_set = se.neighbors_sets[isite][4][0] nb_set_surface_points = np.array([[1.0017922780870239, 0.99301365328679292], [1.0017922780870239, 0.0], [2.2237615554448569, 0.0], [2.2237615554448569, 0.0060837], [2.25, 0.0060837], [2.25, 0.99301365328679292]]) self.assertTrue(np.allclose(np.array(nb_set.voronoi_grid_surface_points()), nb_set_surface_points)) neighb_sites = nb_set.neighb_sites coords = [np.array([0.2443798, 1.80409653, -1.13218359]), np.array([1.44020353, 1.11368738, 1.13218359]), np.array([2.75513098, 2.54465207, -0.70467298]), np.array([0.82616785, 3.65833945, 0.70467298])] np.testing.assert_array_almost_equal(coords[0], neighb_sites[0].coords) np.testing.assert_array_almost_equal(coords[1], neighb_sites[1].coords) np.testing.assert_array_almost_equal(coords[2], neighb_sites[2].coords) np.testing.assert_array_almost_equal(coords[3], neighb_sites[3].coords) neighb_coords = nb_set.coords np.testing.assert_array_almost_equal(coords, neighb_coords[1:]) np.testing.assert_array_almost_equal(nb_set.structure[nb_set.isite].coords, neighb_coords[0]) normdist = nb_set.normalized_distances self.assertAlmostEqual(sorted(normdist), sorted([1.0017922783963027, 1.0017922780870239, 1.000000000503177, 1.0])) normang = nb_set.normalized_angles self.assertAlmostEqual(sorted(normang), sorted([0.9999999998419052, 1.0, 0.9930136530585189, 0.9930136532867929])) dist = nb_set.distances self.assertAlmostEqual(sorted(dist), sorted([1.6284399814843944, 1.6284399809816534, 1.6255265861208676, 1.6255265853029401])) ang = nb_set.angles self.assertAlmostEqual(sorted(ang), sorted([3.117389876236432, 3.117389876729275, 3.095610709498583, 3.0956107102102024])) nb_set_info = nb_set.info self.assertAlmostEqual(nb_set_info['normalized_angles_mean'], 0.996506826547) self.assertAlmostEqual(nb_set_info['normalized_distances_std'], 0.000896138995037) self.assertAlmostEqual(nb_set_info['angles_std'], 0.0108895833142) self.assertAlmostEqual(nb_set_info['distances_std'], 0.00145669776056) self.assertAlmostEqual(nb_set_info['distances_mean'], 1.62698328347) self.assertEqual(nb_set.__str__(), 'Neighbors Set for site #6 :\n' ' - Coordination number : 4\n' ' - Voronoi indices : 1, 4, 5, 6\n') self.assertFalse(nb_set.__ne__(nb_set)) self.assertEqual(nb_set.__hash__(), 4)
def test_delta_csms_weight(self): # Get the StructureEnvironments for K2NaNb2Fe7Si8H4O31 (mp-743972) f = open(os.path.join(se_files_dir, 'se_mp-743972.json'), 'r') dd = json.load(f) f.close() se = StructureEnvironments.from_dict(dd) # Get neighbors sets for which we get the weights cn_maps = [(12, 3), (12, 2), (13, 2), (12, 0), (12, 1), (13, 0), (13, 1)] nbsets = {cn_map: se.neighbors_sets[0][cn_map[0]][cn_map[1]] for cn_map in cn_maps} effective_csm_estimator = {'function': 'power2_inverse_decreasing', 'options': {'max_csm': 8.0}} weight_estimator = {'function': 'smootherstep', 'options': {'delta_csm_min': 0.5, 'delta_csm_max': 3.0}} symmetry_measure_type = 'csm_wcs_ctwcc' delta_weight = DeltaCSMNbSetWeight(effective_csm_estimator=effective_csm_estimator, weight_estimator=weight_estimator, symmetry_measure_type=symmetry_measure_type) additional_info = {} cn_map = (12, 3) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) cn_map = (12, 2) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) cn_map = (12, 0) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) cn_map = (12, 1) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) cn_map = (13, 2) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 1.0, delta=1e-8) cn_map = (13, 0) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) cn_map = (13, 1) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) effective_csm_estimator = {'function': 'power2_inverse_decreasing', 'options': {'max_csm': 8.0}} weight_estimator = {'function': 'smootherstep', 'options': {'delta_csm_min': -1.0, 'delta_csm_max': 3.0}} symmetry_measure_type = 'csm_wcs_ctwcc' delta_weight = DeltaCSMNbSetWeight(effective_csm_estimator=effective_csm_estimator, weight_estimator=weight_estimator, symmetry_measure_type=symmetry_measure_type) additional_info = {} cn_map = (12, 3) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.040830741048481355, delta=1e-8) cn_map = (13, 2) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 1.0, delta=1e-8) cn_map = (13, 0) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.103515625, delta=1e-8) cn_map = (13, 1) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.103515625, delta=1e-8) # Get the StructureEnvironments for SiO2 (mp-7000) f = open(os.path.join(se_files_dir, 'se_mp-7000.json'), 'r') dd = json.load(f) f.close() se = StructureEnvironments.from_dict(dd) # Get neighbors sets for which we get the weights cn_maps = [(2, 0), (4, 0)] nbsets = {cn_map: se.neighbors_sets[6][cn_map[0]][cn_map[1]] for cn_map in cn_maps} effective_csm_estimator = {'function': 'power2_inverse_decreasing', 'options': {'max_csm': 8.0}} weight_estimator = {'function': 'smootherstep', 'options': {'delta_csm_min': 0.5, 'delta_csm_max': 3.0}} symmetry_measure_type = 'csm_wcs_ctwcc' delta_weight = DeltaCSMNbSetWeight(effective_csm_estimator=effective_csm_estimator, weight_estimator=weight_estimator, symmetry_measure_type=symmetry_measure_type) additional_info = {} cn_map = (2, 0) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 0.0, delta=1e-8) cn_map = (4, 0) delta_w = delta_weight.weight(nb_set=nbsets[cn_map], structure_environments=se, cn_map=cn_map, additional_info=additional_info) self.assertAlmostEqual(delta_w, 1.0, delta=1e-8)
def run_task(self, fw_spec): logging.basicConfig( filename='chemenv_light_structure_environments.log', format='%(levelname)s:%(module)s:%(funcName)s:%(message)s', level=logging.DEBUG) identifier = fw_spec['identifier'] criteria = {'identifier': identifier} # Where to get the full structure environments object se_database = fw_spec['structure_environments_database'] entry = se_database.collection.find_one(criteria) if fw_spec['structure_environments_setup'] == 'from_gridfs': gfs_fileobject = se_database.gridfs.get( entry['structure_environments']) dd = json.load(gfs_fileobject) se = StructureEnvironments.from_dict(dd) elif fw_spec['structure_environments_setup'] == 'from_storedfile': se_storage_server = fw_spec['se_storage_server'] se_filepath = entry['structure_environments_file'] se_storage_server.get(se_filepath, 'se.json') f = open('se.json', 'r') dd = json.load(f) f.close() se = StructureEnvironments.from_dict(dd) else: raise RuntimeError('Wrong structure_environments_setup : ' '"{}" is not allowed'.format( fw_spec['structure_environments_setup'])) # Compute the light structure environments chemenv_strategy = fw_spec['chemenv_strategy'] if 'valences' in fw_spec: valences = fw_spec['valences'] valences_origin = fw_spec['valences_origin'] else: valences = 'undefined' valences_origin = 'None' lse = LightStructureEnvironments.from_structure_environments( strategy=chemenv_strategy, structure_environments=se, valences=valences, valences_origin=valences_origin) # Write to json file if 'json_file' in fw_spec: json_file = fw_spec['json_file'] else: json_file = 'light_structure_environments.json' f = open(json_file, 'w') json.dump(lse.as_dict(), f) f.close() # Save to database if 'mongo_database' in fw_spec: database = fw_spec['mongo_database'] entry = { 'identifier': identifier, 'elements': [elmt.symbol for elmt in lse.structure.composition.elements], 'nelements': len(lse.structure.composition.elements), 'pretty_formula': lse.structure.composition.reduced_formula, 'nsites': len(lse.structure), 'chemenv_statistics': lse.get_statistics(statistics_fields='ALL', bson_compatible=True) } saving_option = fw_spec['saving_option'] if saving_option == 'gridfs': gridfs_msonables = { 'structure': lse.structure, 'light_structure_environments': lse } elif saving_option == 'storefile': gridfs_msonables = None if 'lse_prefix' in fw_spec: lse_prefix = fw_spec['lse_prefix'] if not lse_prefix.isalpha(): raise ValueError( 'Prefix for light_structure_environments file is "{}" ' 'while it should be alphabetic'.format(lse_prefix)) else: lse_prefix = '' if lse_prefix: lse_rfilename = '{}_{}.json'.format( lse_prefix, fw_spec['storefile_basename']) else: lse_rfilename = '{}.json'.format( fw_spec['storefile_basename']) lse_rfilepath = '{}/{}'.format(fw_spec['storefile_dirpath'], lse_rfilename) storage_server = fw_spec['storage_server'] storage_server.put(localpath=json_file, remotepath=lse_rfilepath, overwrite=True, makedirs=False) entry['light_structure_environments_file'] = lse_rfilepath else: raise ValueError( 'Saving option is "{}" while it should be ' '"gridfs" or "storefile"'.format(saving_option)) criteria = {'identifier': identifier} if database.collection.find(criteria).count() == 1: database.update_entry(query=criteria, entry_update=entry, gridfs_msonables=gridfs_msonables) else: database.insert_entry(entry=entry, gridfs_msonables=gridfs_msonables)