-
Notifications
You must be signed in to change notification settings - Fork 419
/
make_materials_table.py
82 lines (55 loc) · 1.93 KB
/
make_materials_table.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
"""
This helper script creates a documentation page from the materials database
that is stored in JSON format.
To run it, just do
python ./make_materials_table.py
This will create the `pyroomacoustics.materials.database.rst` file in the doc.
"""
import json
from pathlib import Path
from tabulate import tabulate
datafile = Path("../pyroomacoustics/data/materials.json")
docfile = Path("./pyroomacoustics.materials.database.rst")
def format_freq(f):
if f < 1000:
return "{:d} Hz".format(f)
else:
return "{:d} kHz".format(f // 1000)
def print_section(title, data, headers, file):
print(title, file=file)
print("-" * len(title), file=file)
print(file=file)
for subtitle, materials in data.items():
print(subtitle, file=file)
print("^" * len(subtitle), file=file)
print(file=file)
table = []
for keyword, p in materials.items():
# fill the table
row = [keyword, p["description"]]
for c in p["coeffs"]:
row.append("{:.2f}".format(c))
# pad possibly missing coefficients
while len(row) < len(headers):
row.append("")
table.append(row)
print(tabulate(table, headers, tablefmt="rst"), file=file)
print(file=file)
if __name__ == "__main__":
with open(datafile, "r") as f:
data = json.load(f)
headers = ["keyword", "description"] + [
format_freq(f) for f in data["center_freqs"]
]
with open(docfile, "w") as f:
print("Materials Database", file=f)
print("==================", file=f)
print(file=f)
sections = {
"absorption": "Absorption Coefficients",
"scattering": "Scattering Coefficients",
}
for key, sectitle in sections.items():
print_section(
title=sectitle, data=data[key], headers=headers, file=f,
)