-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_var.py
133 lines (108 loc) · 3.67 KB
/
gen_var.py
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
import sys, os, tarfile, itertools, shutil
try:
import yaml
except ImportError:
print ("Trying to Install required module: requests\n")
os.system('pip3 install PyYAML')
import yaml
# Cartesian product of lists
def cartesian_prod(list1 = [], bool_var1 = False, list2 = [], bool_var2 = False, list3 = [], bool_var3 = False):
full_list = []
if bool_var1 and bool_var2 and bool_var3:
full_list = list(itertools.product(list1,list2, list3))
elif bool_var1 and bool_var2:
full_list = list(itertools.product(list1,list2))
elif bool_var1 and bool_var3:
full_list = list(itertools.product(list1,list3))
elif bool_var2 and bool_var3:
full_list = list(itertools.product(list2,list3))
return full_list
def moveAllFilesinDir(srcDir, dstDir):
file_names = os.listdir(srcDir)
# Check if both the are directories
if os.path.isdir(srcDir) and os.path.isdir(dstDir):
for file_name in file_names:
shutil.move(os.path.join(srcDir, file_name), dstDir)
else:
print("srcDir & dstDir should be Directories")
if os.path.isdir(srcDir):
os.rmdir(srcDir)
if __name__ == '__main__':
############## inputs #####################
with open('rendered_wano.yml') as file:
wano_file = yaml.full_load(file)
###########################################
outdict = {}
outdict["iter"] = []
var_float = []
var_int = []
var_str = []
keys = []
if wano_file["Float"]:
var_begin = wano_file["VarF-begin"]
var_end = wano_file["VarF-end"]
var_npoints = wano_file["N-points"]
keys.append("Float")
step = (var_end-var_begin)/var_npoints
for i in range(0,var_npoints):
z_0 = var_begin + step*i
z_0 = round(z_0,3)
var_float.append(float(z_0))
if wano_file["Int"]:
var_begin = wano_file["VarI-begin"]
var_end = wano_file["VarI-end"]
step = wano_file["Step"]
if step == 0:
print( "N_points must fit in the interval" )
sys.exit()
for z_0 in range(var_begin,var_end, step):
#z_0 = var_begin + int(step*i)
print(z_0)
var_int.append(z_0)
keys.append("Int")
if wano_file["Structures"]:
if os.path.isfile("Structures.tar"):
print ("File exist")
file = tarfile.open("Structures.tar",'r')
file.extractall('Structures')
#[print(os.path.isdir('Structures/'+ jj)) for jj in var_str]
srcDir = 'Structures/Molecules'
dstDir = 'Structures'
moveAllFilesinDir(srcDir, dstDir)
var_str = os.listdir(dstDir)
keys.append("Structures")
#outdict["iter"] = file.getnames()
#print(outdict)
else:
print ("File not exist")
tar = tarfile.open("Structures.tar", "w")
tar.close()
var_str = []
# Catersian product
true_list = [wano_file["Float"], wano_file["Int"], wano_file["Structures"]]
ii = 0
for jj in true_list:
if jj:
ii += 1
if ii > 1:
input_list = cartesian_prod(var_float,true_list[0], var_int, true_list[1], var_str, true_list[2])
elif true_list[0]:
input_list = var_float
elif true_list[1]:
input_list = keys
elif true_list[2]:
input_list = var_str
#input_list = list(itertools.product(var_float,var_str))
#print(input_list)
# input_list = [list(x) for x in input_list]
input_dict = {}
temp_dict = {}
keys1 = list(range(len(input_list)))
outdict["iter"] = keys1
for i in keys1:
temp_dict = {i: dict(zip(keys,input_list[i]))}
input_dict.update(temp_dict)
with open("input_dict.yml",'w') as out:
yaml.dump(input_dict, out, default_flow_style = False)
with open("output_dict.yml",'w') as out:
yaml.dump(outdict, out,default_flow_style = False)