-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_to_latex.py
257 lines (193 loc) · 7.42 KB
/
json_to_latex.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import os
import json
from collections import OrderedDict
def generate_filename(a, s, r):
name = "a{:0>3}_s{:0>2}_r{:0>3}"
angle = str(int(a * 10))
span = str(s).replace(".", "")
ratio = str(int(round(r * 100, 0))).replace(".", "")
filename = name.format(angle, span, ratio)
return filename
def generate_filepaths(path, angle, spans, ratios):
filepaths = []
filenames = [generate_filename(angle, s, r) for r in ratios for s in spans]
for f in filenames:
filepath = os.path.join(path, f)
filepaths.append(filepath)
return filepaths
def save_json(path, dims, data):
filename = generate_filename(dims)
filepath = os.path.join(path, filename)
with open(filepath, "w") as f:
json.dump(data, f, indent=4)
f.close()
def read_json_data_info(files_in, txt):
lines_data = []
i = 0
for file_in in files_in:
with open(file_in, "r") as f:
data_in = json.load(f)
print("opening: {}".format(data_in["arch_info"]))
ratio = data_in["arch_info"]["ratio"] / 2
span = data_in["arch_info"]["span"]
bricks_total = data_in["reach_data_coop"]["bricks_total"]
# mass_total = data_in["analysis_data"]["100%"]["mass_kg"]
# correct self-weight, for some reason karamba output total weight wrong
mass_total = bricks_total * 2.70928 # kg/brick
height = ratio * span
if i % 6 == 0:
lines_data.append(txt[0])
lines_data.append(txt[1].format(ratio))
lines_data.append(txt[6].format(bricks_total, mass_total, height))
i += 1
f.close()
return lines_data
def read_json_data_reach(files_in, txt):
lines_data = []
i = 0
for file_in in files_in:
with open(file_in, "r") as f:
data_in = json.load(f)
print("opening: {}".format(data_in["arch_info"]))
ratio = data_in["arch_info"]["ratio"] / 2
reach_ratio_coop = data_in["reach_data_coop"]["reach_ratio"]
# reach_ratio_single = data_in["reach_data_single"]["reach_ratio"]
avg_reach_score_coop = data_in["reach_data_coop"]["avg_reach_score"]
# avg_reach_score_single = data_in["reach_data_single"]["avg_reach_score"]
if i % 6 == 0:
lines_data.append(txt[0])
lines_data.append(txt[1].format(ratio))
if reach_ratio_coop < 0.80:
lines_data.append(
txt[3].format(
"lightred",
reach_ratio_coop * 100,
avg_reach_score_coop,
)
)
elif reach_ratio_coop < 0.99:
lines_data.append(
txt[3].format(
"lightyellow",
reach_ratio_coop * 100,
avg_reach_score_coop,
)
)
else:
lines_data.append(
txt[2].format(
reach_ratio_coop * 100,
avg_reach_score_coop,
)
)
i += 1
f.close()
return lines_data
def read_json_data_force(files_in, txt, force_limit):
lines_data = []
i = 0
for file_in in files_in:
with open(file_in, "r") as f:
data_in = json.load(f)
print("opening: {}".format(data_in["arch_info"]))
ratio = data_in["arch_info"]["ratio"] / 2
force_100 = data_in["analysis_data"]["100%"]["rob_support_kg"]
force_75 = data_in["analysis_data"]["75%"]["rob_support_kg"]
force_50 = data_in["analysis_data"]["50%"]["rob_support_kg"]
if i % 6 == 0:
lines_data.append(txt[0])
lines_data.append(txt[1].format(ratio))
if force_50 > force_limit:
lines_data.append(
txt[5].format(
"lightred",
force_100,
force_75,
force_50,
)
)
elif force_75 > force_limit:
lines_data.append(
txt[5].format(
"lightred",
force_100,
force_75,
force_50,
)
)
elif force_100 > force_limit:
lines_data.append(
txt[5].format(
"lightyellow",
force_100,
force_75,
force_50,
)
)
else:
lines_data.append(
txt[4].format(
force_100,
force_75,
force_50,
)
)
i += 1
f.close()
return lines_data
def write_text_data(path, file_out, lines_data):
filepath_out = os.path.join(path, file_out)
with open(filepath_out, "w") as f_out:
for line in lines_data:
f_out.write(line)
f_out.write("\n")
f_out.write("\\\\")
f_out.close()
##################
# SET VARIABLES
##################
paths = [
"C:/Users/Edvard/Documents/GitHub/rob_fab_reach/_data_track/results_planar_arches",
"C:/Users/Edvard/Documents/GitHub/rob_fab_reach/_data_notrack/results_planar_arches",
]
force_limit = [70, 235] # IRB5710-70/2.70 # IRB6700-235/2.65
angles = [0, 7.5, 15, 22.5]
spans = [2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
ratios = [0.25, 0.50, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0]
txt = [
"\\\\[-0.2em]\cmidrule{2-8}\\\\[-1.3em]",
"&\multicolumn{{1}}{{c|}}{{\makecell[lc]{{\\\\{:.3f}}}}}",
"&\\tiny{{\makecell[lc]{{C:{:.0f}\\%\\\\fab:{}}}}}",
"&\\tiny\cellcolor{{{}}}{{\makecell[lc]{{C:{:.0f}\\%\\\\fab:{}}}}}",
"&\\tiny{{\makecell[lc]{{max:{:.0f}\\\\75\\%:{:.0f}\\\\50\\%:{:.0f}}}}}",
"&\\tiny\cellcolor{{{}}}{{\makecell[lc]{{max:{:.0f}\\\\75\\%:{:.0f}\\\\50\\%:{:.0f}}}}}",
"&\\footnotesize{{\makecell[lc]{{n={}\\\\{:.0f}kg\\\\{:.2f}m}}}}",
]
# generate these files
file_out = [
"_a{:0>3}_reachability_latex.txt",
"_a{:0>3}_force_latex.txt",
"_a{:0>3}_info_latex.txt",
]
##################
# RUN CODE
##################
path_idx = 0 # 0 = track, 1 = no_track
angle_idxs = [0, 1, 2, 3]
files_in = generate_filepaths(paths[path_idx], angles[0], spans, ratios)
# GENERATE INFO FILE (same for all angles)
lines_data = read_json_data_info(files_in, txt)
lines_data.pop(0) # remove 1st line
file_out_save = file_out[2].format(int(angles[0] * 10))
write_text_data(paths[path_idx], file_out_save, lines_data)
# GENERATE REACH & FORCE FILES (same for all angles)
for angle_idx in angle_idxs:
files_in = generate_filepaths(paths[path_idx], angles[angle_idx], spans, ratios)
lines_data = read_json_data_reach(files_in, txt)
lines_data.pop(0) # remove 1st line
file_out_save = file_out[0].format(int(angles[angle_idx] * 10))
write_text_data(paths[path_idx], file_out_save, lines_data)
lines_data = read_json_data_force(files_in, txt, force_limit[path_idx])
lines_data.pop(0) # remove 1st line
file_out_save = file_out[1].format(int(angles[angle_idx] * 10))
write_text_data(paths[path_idx], file_out_save, lines_data)