-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinspect_material_raw_data.py
More file actions
103 lines (83 loc) · 2.71 KB
/
Copy pathinspect_material_raw_data.py
File metadata and controls
103 lines (83 loc) · 2.71 KB
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
import json
from typing import Any
from sqlalchemy import text
from app.core.database import SessionLocal
def describe_value(value: Any) -> str:
if isinstance(value, dict):
return f"dict with {len(value)} keys"
if isinstance(value, list):
return f"list with {len(value)} items"
return f"{type(value).__name__}: {value!r}"
def main() -> None:
db = SessionLocal()
try:
result = db.execute(
text(
"""
SELECT
id,
mp_id,
formula,
pretty_formula,
raw_data
FROM materials
WHERE id IN (:lifepo4_id)
OR formula IN ('LiFePO4', 'NaFePO4')
ORDER BY id
LIMIT 5;
"""
),
{"lifepo4_id": 5},
)
materials = result.mappings().all()
if not materials:
print("No matching materials found.")
return
for material in materials:
print("\n" + "=" * 80)
print(
f"id={material['id']} "
f"mp_id={material['mp_id']} "
f"formula={material['formula']} "
f"pretty_formula={material['pretty_formula']}"
)
raw_data = material["raw_data"]
if raw_data is None:
print("raw_data: None")
continue
# PostgreSQL JSON may already be returned as a dict.
if isinstance(raw_data, str):
raw_data = json.loads(raw_data)
if not isinstance(raw_data, dict):
print(f"Unexpected raw_data type: {type(raw_data).__name__}")
print(raw_data)
continue
print("\nTop-level raw_data keys:")
for key in sorted(raw_data):
print(f"- {key}: {describe_value(raw_data[key])}")
print("\nLikely composition-related fields:")
for key in (
"composition",
"composition_reduced",
"formula_pretty",
"formula_anonymous",
"chemsys",
"elements",
"nelements",
):
if key in raw_data:
print(f"\n{key}:")
print(
json.dumps(
raw_data[key],
indent=2,
default=str,
)
)
except Exception as exc:
print(f"Inspection failed: {exc}")
raise
finally:
db.close()
if __name__ == "__main__":
main()