File size: 4,223 Bytes
39c21b2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | import pandas as pd
from pymatgen.io.cif import CifWriter
from gen_xrd import create_xrd_tensor
import argparse
import re
import os
from tqdm import tqdm
import random
def good_file_format(struct_filename):
format = r'^mp-\d+_Structure[.]pickle$'
return re.match(format, struct_filename)
def struct2xrd_filename(struct_filename):
assert good_file_format(struct_filename)
return struct_filename.replace('Structure', 'XRD')
def extract_mp_from_filename(struct_filename):
assert good_file_format(struct_filename)
return struct_filename.split('_')[0]
def save_df_with_indices(mpids, cifs, xrds, indices, name):
assert len(mpids) == len(cifs)
assert len(cifs) == len(xrds)
mpids = [mpids[i] for i in range(len(mpids)) if i in indices]
cifs = [cifs[i] for i in range(len(cifs)) if i in indices]
xrds = [xrds[i] for i in range(len(xrds)) if i in indices]
the_df = pd.DataFrame(columns=['material_id', 'cif', 'xrd'], dtype=object)
the_df['material_id'] = mpids
the_df['cif'] = cifs
the_df['xrd'] = xrds
os.makedirs(args.save_filepath, exist_ok=True)
the_df.to_pickle(os.path.join(args.save_filepath, f'{name}.csv'))
return
def main(args):
random.seed(args.seed)
noshows = list()
too_big = list()
cifs = list()
xrds = list()
mpids = list()
for struct_file in tqdm(os.listdir(args.struct_dir_pickled)):
the_mpid = extract_mp_from_filename(struct_filename=struct_file)
struct_filepath = os.path.join(args.struct_dir_pickled, struct_file)
xrd_filepath = os.path.join(args.xrd_dir_pickled, struct2xrd_filename(struct_file))
if not (os.path.exists(struct_filepath) and os.path.exists(xrd_filepath)):
noshows.append(the_mpid)
continue
the_structure = pd.read_pickle(struct_filepath)
if the_structure.num_sites >= args.max_atoms:
too_big.append(the_mpid)
continue
the_xrd = create_xrd_tensor(args, pd.read_pickle(xrd_filepath))
cif_writer = CifWriter(the_structure)
cif_string = cif_writer.__str__()
cifs.append(cif_string)
xrds.append(the_xrd)
mpids.append(the_mpid)
indices = list(range(len(mpids)))
random.shuffle(indices)
assert args.train_ratio + args.val_ratio < 1
train_end = int(len(mpids) * args.train_ratio)
val_end = train_end + int(len(mpids) * args.val_ratio)
train_indices = indices[:train_end]
val_indices = indices[train_end:val_end]
test_indices = indices[val_end:]
for curr_indices, curr_name in zip([train_indices, val_indices, test_indices], ['train', 'val', 'test']):
save_df_with_indices(mpids=mpids, cifs=cifs, xrds=xrds, indices=curr_indices, name=curr_name)
print('noshows:', len(noshows), ' : ', noshows)
print(f'too big: {len(too_big)} / {len(mpids) + len(too_big)}')
return
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Generate XRD patterns from CIF descriptions')
parser.add_argument(
'--max_theta',
default = 180,
type=int,
)
parser.add_argument(
'--min_theta',
default = 0,
type=int,
)
parser.add_argument(
'--xrd_vector_dim',
default = 512,
type=int,
)
parser.add_argument(
'--save_filepath',
default='/home/gabeguo/cdvae_xrd/data/mp_trigonal',
type=str,
)
parser.add_argument(
'--struct_dir_pickled',
default='/home/gabeguo/mp_dataset/updated_crystallography_data/pickled_positions/Trigonal',
type=str
)
parser.add_argument(
'--xrd_dir_pickled',
default='/home/gabeguo/mp_dataset/updated_crystallography_data/pickled_xrds/Trigonal',
type=str
)
parser.add_argument(
'--seed',
default=0,
type=str
)
parser.add_argument(
'--train_ratio',
default=0.8,
type=float
)
parser.add_argument(
'--val_ratio',
default=0.1,
type=float
)
parser.add_argument(
'--max_atoms',
default=50,
type=float
)
args = parser.parse_args()
main(args)
|