{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":14313,"databundleVersionId":862019,"sourceType":"competition"},{"sourceId":9830816,"sourceType":"datasetVersion","datasetId":6029410}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install -q swifter\n!pip install -U -q ipywidgets\n!pip install -q pymatgen","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T21:38:44.404663Z","iopub.execute_input":"2024-12-01T21:38:44.405083Z","iopub.status.idle":"2024-12-01T21:39:47.708302Z","shell.execute_reply.started":"2024-12-01T21:38:44.405046Z","shell.execute_reply":"2024-12-01T21:39:47.707104Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pip install -q pymatgen ase","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T21:39:47.710992Z","iopub.execute_input":"2024-12-01T21:39:47.711506Z","iopub.status.idle":"2024-12-01T21:40:00.241006Z","shell.execute_reply.started":"2024-12-01T21:39:47.711443Z","shell.execute_reply":"2024-12-01T21:40:00.239578Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nimport random\nimport numpy as np \nimport torch \n\nimport os\nimport shutil\n\nimport pymatgen.core as mg\nfrom pymatgen.io.xyz import XYZ\n\nfrom pymatgen.symmetry.analyzer import SpacegroupAnalyzer\nfrom pymatgen.symmetry.analyzer import PointGroupAnalyzer\n\nfrom sympy import Plane, Point3D\nfrom sympy import symbols, Eq\n\nimport sys\nimport time\n\nfrom pymatgen.core import Molecule\n\nfrom pymatgen.core import Molecule\nfrom pymatgen.io.ase import AseAtomsAdaptor\nfrom ase.calculators.emt import EMT  # A simple force field for demonstration\nfrom ase.optimize import BFGS  # BFGS optimizer for geometry optimization","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T21:40:00.242954Z","iopub.execute_input":"2024-12-01T21:40:00.243460Z","iopub.status.idle":"2024-12-01T21:40:05.131251Z","shell.execute_reply.started":"2024-12-01T21:40:00.243404Z","shell.execute_reply":"2024-12-01T21:40:05.129999Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Finding symmetry of every molecule","metadata":{}},{"cell_type":"code","source":"class Configuration:\n    seed = 42\n    train_path = '/kaggle/input/champs-scalar-coupling/train.csv'\n    test_path = '/kaggle/input/champs-scalar-coupling/test.csv'\n    structures_path = '/kaggle/input/champs-scalar-coupling/structures.csv'\n    structures_dir = '/kaggle/input/champs-scalar-coupling/structures'\n    xyz_output_dir = '/kaggle/working/xyz'\n    picture_output_dir = '/kaggle/working/picture'\ncfg = Configuration()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T21:40:05.133717Z","iopub.execute_input":"2024-12-01T21:40:05.134285Z","iopub.status.idle":"2024-12-01T21:40:05.139908Z","shell.execute_reply.started":"2024-12-01T21:40:05.134246Z","shell.execute_reply":"2024-12-01T21:40:05.138729Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Parameters\nd_tol = 0.3  # Distance tolerance for point group analysis\ne_tol = 0.01  # Symmetry tolerance for point group analysis","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T21:40:05.141144Z","iopub.execute_input":"2024-12-01T21:40:05.141652Z","iopub.status.idle":"2024-12-01T21:40:05.166347Z","shell.execute_reply.started":"2024-12-01T21:40:05.141589Z","shell.execute_reply":"2024-12-01T21:40:05.165166Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"symmetry_array = {} \ndirectory_path = '/kaggle/input/champs-scalar-coupling/structures'\n\nln = 131000\nn=0\n\noutput_path = '/kaggle/working/sym/sym_molecules_xyz'\nos.makedirs(output_path, exist_ok=True)\n\nfile_num_dic = {}\n\nfor filename in os.listdir(directory_path):\n    if filename[10:16] not in file_num_dic:\n        file_num_dic[filename[10:16]] = 1\n        xyz_file = os.path.join(directory_path, filename)\n\n        with open(xyz_file) as f:\n            xyz_structure = f.read()\n\n        # Load molecule data from XYZ file\n        xyz = XYZ.from_str(xyz_structure)\n        molecule_xyz = xyz.molecule  # Convert to molecule object\n        \n        # Perform symmetry analysis to group equivalent atoms\n        pga = PointGroupAnalyzer(molecule_xyz, d_tol, e_tol)\n        symmetry = pga.get_pointgroup()\n        \n        symmetry_str = symmetry.sch_symbol\n\n        if symmetry_str in symmetry_array:\n            symmetry_array[symmetry_str] += 1\n        else:\n            symmetry_array[symmetry_str] = 1\n        \n        \n        if symmetry_str != \"C1\":\n            source_file = os.path.join(directory_path, filename)\n            copy_file = os.path.join(output_path, os.path.basename(source_file))\n            \n            # Add the file copy operation here\n            shutil.copy(source_file, copy_file)\n\n    #percent = (n + 1) / ln * 100\n    #n+=1\n    #bar = '=' * int(percent // 2) + '-' * (50 - int(percent // 2))\n    \n    # Print the progress bar on the same line\n    #sys.stdout.write(f\"\\r[{bar}] {percent:.2f}%\")\n    #sys.stdout.flush()\n        \nprint(symmetry_array)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T21:40:05.167907Z","iopub.execute_input":"2024-12-01T21:40:05.168307Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sym_dic = symmetry_array.copy()\n#del sym_dic['Cs']\nprint(sym_dic)\nvalues = list(sym_dic.values())\nprint(len(values))\n#values = np.array(value)/ln*100\n#print(values)\nlabels = list(sym_dic.keys())\n\nplt.figure(figsize=(8, 3))\nplt.yscale('log')\nplt.bar(labels, values, align='center', alpha=0.5, ecolor='black', capsize=10)\n\n# Set x-ticks and labels on the current Axes object\nax = plt.gca()  # Get the current Axes\nax.set_xticks(range(len(labels)))\nax.set_xticklabels(labels)\n\n# Additional plot settings\nplt.xlabel('Space group')\nplt.ylabel('Number of molecules')\nplt.tick_params(axis='both', which='major', labelsize=10)\nplt.xticks(rotation=45)\nplt.legend([\"Molecules\"], loc=\"upper right\")  # Legend with label\n\n# Show the plot\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(len(file_num_dic))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T02:11:27.358473Z","iopub.status.idle":"2024-11-20T02:11:27.358874Z","shell.execute_reply.started":"2024-11-20T02:11:27.358691Z","shell.execute_reply":"2024-11-20T02:11:27.358715Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Naming all the atom the same, force symmetry","metadata":{}},{"cell_type":"code","source":"from pymatgen.io.ase import AseAtomsAdaptor\nfrom ase.calculators.emt import EMT  # A simple force field for demonstration\nfrom ase.optimize import BFGS  # BFGS optimizer for geometry optimization\n\nsymmetry_array_forced = {} \nsymmetry_array_initial = {} \ndirectory_path = '/kaggle/input/champs-scalar-coupling/structures'\n\nln = 131000\nn=0\n\noutput_path = '/kaggle/working/sym/sym_molecules_forced_xyz'\nos.makedirs(output_path, exist_ok=True)\n\nfile_num_dic = {}\nadaptor = AseAtomsAdaptor()\n\nfor filename in os.listdir(directory_path):\n    if filename[10:16] not in file_num_dic:\n        file_num_dic[filename[10:16]] = 1\n        xyz_file = os.path.join(directory_path, filename)\n\n        with open(xyz_file) as f:\n            xyz_structure = f.read()\n\n        # Load molecule data from XYZ file\n        xyz = XYZ.from_str(xyz_structure)\n        molecule_xyz = xyz.molecule  # Convert to molecule object\n        \n        # Perform symmetry analysis to group equivalent atoms\n        pga = PointGroupAnalyzer(molecule_xyz, d_tol, e_tol)\n        symmetry = pga.get_pointgroup()\n        \n        symmetry_str = symmetry.sch_symbol\n\n        if symmetry_str in symmetry_array_initial:\n            symmetry_array_initial[symmetry_str] += 1\n        else:\n            symmetry_array_initial[symmetry_str] = 1\n        \n        if symmetry_str == 'C1':\n            simplified_species = [site.species_string for site in molecule_xyz]\n            for i in range(len(simplified_species)):\n                simplified_species[i] = 'H'\n            molecule_same_atoms = Molecule(simplified_species, molecule_xyz.cart_coords)            \n            pga2 = PointGroupAnalyzer(molecule_same_atoms, d_tol, e_tol)\n            symmetry2 = pga2.get_pointgroup()\n        \n            symmetry_str = symmetry2.sch_symbol\n        \n        if symmetry_str in symmetry_array_forced:\n            symmetry_array_forced[symmetry_str] += 1\n        else:\n            symmetry_array_forced[symmetry_str] = 1\n\n        if symmetry_str != \"C1\":\n            source_file = os.path.join(directory_path, filename)\n            copy_file = os.path.join(output_path, os.path.basename(source_file))\n            \n            # Add the file copy operation here\n            shutil.copy(source_file, copy_file)\n        \n    #percent = (n + 1) / ln * 100\n    #n+=1\n    #bar = '=' * int(percent // 2) + '-' * (50 - int(percent // 2))\n    \n    # Print the progress bar on the same line\n    #sys.stdout.write(f\"\\r[{bar}] {percent:.2f}%\")\n    #sys.stdout.flush()\n        \nprint(symmetry_array)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T04:02:35.654581Z","iopub.execute_input":"2024-11-20T04:02:35.655613Z","iopub.status.idle":"2024-11-20T04:42:41.102797Z","shell.execute_reply.started":"2024-11-20T04:02:35.655570Z","shell.execute_reply":"2024-11-20T04:42:41.101720Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(symmetry_array_initial)\nprint(symmetry_array_forced)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T04:49:06.992717Z","iopub.execute_input":"2024-11-20T04:49:06.993102Z","iopub.status.idle":"2024-11-20T04:49:06.998201Z","shell.execute_reply.started":"2024-11-20T04:49:06.993069Z","shell.execute_reply":"2024-11-20T04:49:06.997095Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sym_dic = symmetry_array_forced.copy()\n#del sym_dic['Cs']\nprint(sym_dic)\nvalues = list(sym_dic.values())\nprint(len(values))\n#values = np.array(value)/ln*100\nprint(np.array(values).sum())\nlabels = list(sym_dic.keys())\n\nplt.figure(figsize=(8, 3))\nplt.yscale('log')\nplt.bar(labels, values, align='center', alpha=0.5, ecolor='black', capsize=10)\n\n# Set x-ticks and labels on the current Axes object\nax = plt.gca()  # Get the current Axes\nax.set_xticks(range(len(labels)))\nax.set_xticklabels(labels)\n\n# Additional plot settings\nplt.xlabel('Space group')\nplt.ylabel('Number of molecules')\nplt.tick_params(axis='both', which='major', labelsize=10)\nplt.legend([\"Molecules\"], loc=\"upper right\")  # Legend with label\nplt.xticks(rotation=45)\n\n# Show the plot\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T05:08:40.344142Z","iopub.execute_input":"2024-11-20T05:08:40.344521Z","iopub.status.idle":"2024-11-20T05:08:40.863267Z","shell.execute_reply.started":"2024-11-20T05:08:40.344487Z","shell.execute_reply":"2024-11-20T05:08:40.862161Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Taking only files with symmetry","metadata":{}},{"cell_type":"code","source":"'''directory_path = '/kaggle/input/10k-molecular-coupling-constant-images/xyz'\ndirectory_pic_path = '/kaggle/input/10k-molecular-coupling-constant-images/picture'\n\nsymmetry_array_forced = {}\n\nln = 131000\nn=0\n\nfile_num_dic = {}\n\noutput_path = '/kaggle/working/10k_sym_picture'\nos.makedirs(output_path, exist_ok=True)\n\nfor filename in os.listdir(directory_path):\n    if filename[10:16] not in file_num_dic:\n        file_num_dic[filename[10:16]] = 1\n        xyz_file = os.path.join(directory_path, filename)\n\n        with open(xyz_file) as f:\n            xyz_structure = f.read()\n\n        # Load molecule data from XYZ file\n        xyz = XYZ.from_str(xyz_structure)\n        molecule_xyz = xyz.molecule  # Convert to molecule object\n        \n        # Perform symmetry analysis to group equivalent atoms\n        pga = PointGroupAnalyzer(molecule_xyz, d_tol, e_tol)\n        symmetry = pga.get_pointgroup()\n        \n        symmetry_str = symmetry.sch_symbol\n        \n        if symmetry_str == 'C1':\n            simplified_species = [site.species_string for site in molecule_xyz]\n            for i in range(len(simplified_species)):\n                simplified_species[i] = 'H'\n            molecule_same_atoms = Molecule(simplified_species, molecule_xyz.cart_coords)            \n            pga2 = PointGroupAnalyzer(molecule_same_atoms, d_tol, e_tol)\n            symmetry2 = pga2.get_pointgroup()\n        \n            symmetry_str = symmetry2.sch_symbol\n        \n        if symmetry_str in symmetry_array_forced:\n            symmetry_array_forced[symmetry_str] += 1\n        else:\n            symmetry_array_forced[symmetry_str] = 1\n\n        \n        if symmetry_str != \"C1\":\n            source_file = os.path.join(directory_pic_path, filename[:-3] + 'jpg')\n            copy_file = os.path.join(output_path, os.path.basename(source_file))\n            \n            # Add the file copy operation here\n            shutil.copy(source_file, copy_file)\n\n    percent = (n + 1) / ln * 100\n    n+=1\n    bar = '=' * int(percent // 2) + '-' * (50 - int(percent // 2))\n    \n    # Print the progress bar on the same line\n    sys.stdout.write(f\"\\r[{bar}] {percent:.2f}%\")\n    sys.stdout.flush()'''","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T05:10:11.833119Z","iopub.execute_input":"2024-11-20T05:10:11.833508Z","iopub.status.idle":"2024-11-20T05:10:17.511461Z","shell.execute_reply.started":"2024-11-20T05:10:11.833475Z","shell.execute_reply":"2024-11-20T05:10:17.510014Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#for filename in os.listdir(output_path):\n#    print(filename)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T02:11:27.367056Z","iopub.status.idle":"2024-11-20T02:11:27.367399Z","shell.execute_reply.started":"2024-11-20T02:11:27.367233Z","shell.execute_reply":"2024-11-20T02:11:27.367251Z"}},"outputs":[],"execution_count":null}]}