forked from lucaparisi91/pimc-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputFileTools.py
150 lines (114 loc) · 3.92 KB
/
inputFileTools.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from functools import reduce
import re
import pandas as pd
import numpy as np
import re
import copy
import json
import itertools
import os
from . import moves
def getJson(j,queryString):
if queryString=="":
return j
pattern="([a-zA-Z0-9]+)(?:\[(\d+)\])?(?:/(.+))?"
m=re.match(pattern,queryString)
print(m[3])
if m is not None:
key=m[1]
index=m[2]
remainder=m[3]
result=j[key]
if index is not None:
result=result[int(index)]
if remainder is not None:
return getJson(result,remainder)
else:
return result
def setJson(j,queryString,value):
if queryString=="":
return j
pattern="([a-zA-Z0-9]+)(?:\[(\d+)\])?(?:/(.+))?"
m=re.match(pattern,queryString)
key=m[1]
index=m[2]
remainder=m[3]
if m is not None:
if remainder is None:
if index is None:
j[key]=value
else:
j[key][int(index)]=value
else:
result=j[key]
if index is not None:
result=result[int(index)]
setJson(result,remainder,value)
def expandJson(jTtemplate, **kwds):
'''
jTemplate : template json input file
Takes in input a series of kwd , valude with value a vector and kwd the value to update
TODO : mondodb queries
'''
js=[]
#alues=[ v.flatten() for v in np.meshgrid(*kwds.values()) ]
for currentValues in itertools.product(*kwds.values() ):
j=copy.deepcopy(jTemplate)
for kwd,value in zip( kwds.keys(), currentValues):
setJson(j,kwd,value)
js.append(j)
return js
def createInputFilesFromTemplate(template,params):
keys=list(params.keys())
N=len(params[keys[0]])
js=[]
for i in range(N):
j=copy.deepcopy(template)
for key in keys:
value=params[key][i]
setJson(j,str(key),value)
js.append(j)
return js
class numpyCompatibleEncoder(json.JSONEncoder):
# Handles the default behavior of
# the encoder when it parses an object 'obj'
def default(self, obj):
# If the object is a numpy array
if isinstance(obj, np.ndarray):
# Convert to Python List
return obj.tolist()
else:
if isinstance(obj, np.int64) or isinstance(obj, np.int32):
return int(obj)
# Let the base class Encoder handle the object
return json.JSONEncoder.default(self, obj)
def createSimFolders(settings):
'''
vector of json file of format
"folder" : name of the folder to create
"jSon" : vector of {name : content}
'''
for j in settings:
folder=j["folder"]
if not os.path.exists(folder):
os.makedirs(folder)
for jSonInput in j["jSon"]:
filename=os.path.join( folder, jSonInput[0] )
content=jSonInput[1]
with open(filename,"w") as f:
json.dump(content,f,indent=4,cls=numpyCompatibleEncoder)
def cartesianProduct(kwds):
rows=[]
for currentValues in itertools.product(*kwds.values() ):
rows.append(np.array(currentValues))
return pd.DataFrame(rows,columns=kwds.keys())
def setMoveParameters(j):
for move in j["movesTable"]:
if "reconstructionMaxLength" in move["move"].keys():
move["move"]["reconstructionMaxLength"]=max( int(0.2 * int(j["nBeads"]) ) , 1 )
if move["move"]["kind"]=="open" or move["move"]["kind"]=="close":
move["move"]["C"]=1
move["move"]["reconstructionMaxLength"]=1
if move["move"]["kind"] != "levy":
move["move"]["reconstructionMaxLength"]= max(1, move["move"]["reconstructionMaxLength"]//3)
return j