forked from iccmr-quantum/VQH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control_to_setup2.py
42 lines (35 loc) · 1.28 KB
/
control_to_setup2.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
import json
import csv
import numpy as np
from collections import OrderedDict
json_file_path = 'midi/qubo_control.json'
csv_file_path = 'output.csv'
def json_to_csv(json_fp, csv_fp):
with open(json_fp, 'r') as file:
data = json.load(file)
mapping_dict = {True: '', False: ' '}
#main_order = ['c', 'e', 'g', 'b']
main_order = ['s1', 's2', 's3', 's4']
key_names = {'s1': 'c', 's2': 'e', 's3': 'g', 's4': 'b'}
sub_order = main_order
ordered_data = {key_names[k]: {key_names[sk]: data[k][sk] for sk in sub_order} for k in main_order}
data = ordered_data
with open(csv_fp, 'w', newline='') as file:
csv_writer = csv.writer(file)
# Write the header
headers = ['h1'] + list(data[next(iter(data))].keys()) # Extract keys from the first item
csv_writer.writerow(headers)
# Write the data
for key, values in data.items():
row = list(values.values())
#print(row)
test = np.array(row) < 0
#print(test)
row = [mapping_dict[value]+f'{float(row[i]):.1f}' for i, value in enumerate(test)]
row = [key] + row
csv_writer.writerow(row)
"""
while True:
json_to_csv(json_file_path, csv_file_path)
time.sleep(0.1) # Sleep for 0.1 seconds
"""