10
0
mirror of https://github.com/QuantumPackage/qp2.git synced 2024-12-22 20:34:58 +01:00

Added qp_json

This commit is contained in:
Anthony Scemama 2023-05-10 11:14:57 +02:00
parent 0272c6489a
commit f314c5abc2
2 changed files with 76 additions and 8 deletions

View File

@ -42,13 +42,15 @@ import sys, os
import scipy import scipy
import scipy.stats import scipy.stats
from math import sqrt, gamma, exp from math import sqrt, gamma, exp
import json import qp_json
def read_data(filename,state): def read_data(ezfio_filename,state):
""" Read energies and PT2 from input file """ """ Read energies and PT2 from input file """
with open(filename,'r') as f: data = qp_json.load_last(ezfio_filename)
lines = json.load(f)['fci'] for method in data.keys():
x = data[method]
lines = x
print(f"State: {state}") print(f"State: {state}")
@ -138,15 +140,15 @@ def compute(data):
return mu, err, bias, p return mu, err, bias, p
filename = sys.argv[1] ezfio_filename = sys.argv[1]
print(filename) print(ezfio_filename)
if len(sys.argv) > 2: if len(sys.argv) > 2:
state = int(sys.argv[2]) state = int(sys.argv[2])
else: else:
state = 1 state = 1
data = read_data(filename,state) data = read_data(ezfio_filename,state)
mu, err, bias, _ = compute(data) mu, err, bias, _ = compute(data)
print(" %s: %8.3f +/- %5.3f eV\n"%(filename, mu, err)) print(" %s: %8.3f +/- %5.3f eV\n"%(ezfio_filename, mu, err))
import numpy as np import numpy as np
A = np.array( [ [ data[-1][1], 1. ], A = np.array( [ [ data[-1][1], 1. ],

View File

@ -0,0 +1,66 @@
#!/usr/bin/env python
import os
import json
def fix_json(s):
"""Properly termitates an incomplete JSON file"""
s = s.replace(' ','')
s = s.replace('\n','')
s = s.replace('\t','')
s = s.replace(",{}",'')
tmp = [ c for c in s if c in "[]{}" ]
tmp = "".join(tmp)
tmp_old = ""
while tmp != tmp_old:
tmp_old = tmp
tmp = tmp.replace("{}","")
tmp = tmp.replace("[]","")
while s[-1] in [ ',', '\n', ' ', '\t' ]:
s = s[:-1]
tmp = [ c for c in tmp ]
tmp.reverse()
for c in tmp:
if c == '[': s += "]"
elif c == '{': s += "}"
return s
def load(filename):
"""Loads a JSON file after calling the fix_json function."""
with open(filename,'r') as f:
data = f.read()
new_data = fix_json(data)
return json.loads(new_data)
def load_all(ezfio_filename):
"""Loads all JSON files of an EZFIO."""
d = {}
prefix = ezfio_filename+'/json/'
for filename in [ x for x in os.listdir(prefix) if x.endswith(".json")]:
d[filename] = load(prefix+filename)
return d
def load_last(ezfio_filename):
"""Loads last JSON file of an EZFIO."""
d = {}
prefix = ezfio_filename+'/json/'
l = [ x for x in os.listdir(prefix) if x.endswith(".json")]
l.sort()
filename = l[-1]
print(filename)
return load(prefix+filename)
def fix(ezfio_filename):
"""Fixes all JSON files in an EZFIO."""
d = load_all(ezfio_filename)
prefix = ezfio_filename+'/json/'
for filename in d.keys():
with open(prefix+filename, 'w') as json_file:
json.dump(d[filename], json_file)