| |
| import pandas as pd |
| import argparse |
| import json |
| from ase import Atoms |
| import ast |
|
|
| def deep_convert(val): |
| """ |
| Recursively convert string representations of Python/JSON literals |
| into actual Python objects. |
| |
| Rules: |
| - Strings like "[...]" -> list |
| - Strings like "{...}" -> dict |
| - Nested structures are handled recursively |
| - Invalid formats are safely ignored |
| """ |
|
|
| |
| if isinstance(val, str): |
|
|
| val_strip = val.strip() |
|
|
| |
| if ( |
| (val_strip.startswith("[") and val_strip.endswith("]")) or |
| (val_strip.startswith("{") and val_strip.endswith("}")) |
| ): |
| |
| try: |
| val = json.loads(val_strip) |
| except json.JSONDecodeError: |
| |
| try: |
| val = ast.literal_eval(val_strip) |
| except (ValueError, SyntaxError): |
| return val |
|
|
| |
| if isinstance(val, list): |
| return [deep_convert(v) for v in val] |
|
|
| |
| if isinstance(val, dict): |
| return {k: deep_convert(v) for k, v in val.items()} |
|
|
| |
| return val |
|
|
| def read_phonix_summary(filename): |
| |
| df = pd.read_csv(filename) |
| |
| structures = [] |
| for i, struct_str in enumerate(df['structure'].values): |
| |
| mpid = df['mp_id'].values[i] |
| |
| if isinstance(struct_str, str): |
| try: |
| struct_dict = json.loads(struct_str) |
| atoms = Atoms( |
| symbols=struct_dict["symbols"], |
| scaled_positions=struct_dict["positions"], |
| cell=struct_dict["cell"], |
| pbc=struct_dict["pbc"] |
| ) |
| structures.append(atoms) |
| except Exception as e: |
| print(f"Warning: Failed to parse structure at row {mpid}.") |
| structures.append(None) |
| else: |
| print(f"Warning: 'structure' column contains non-string value at row {mpid}, skipping.") |
| structures.append(None) |
| |
| df['structure'] = structures |
| |
| df = df.map(deep_convert) |
| return df |
|
|
| def main(options): |
| |
| print(" Reading", options.filename) |
|
|
| df = read_phonix_summary(options.filename) |
| print(df.head()) |
| |
| print(df.count()) |
|
|
| df_filtered = df[df['kc[W/mK]'] > df['kp[W/mK]']] |
| print(df_filtered[['kc[W/mK]', 'kp[W/mK]']]) |
|
|
|
|
|
|
| if __name__ == '__main__': |
| |
| parser = argparse.ArgumentParser(description='Input parameters') |
|
|
| parser.add_argument('-f', '--filename', dest='filename', type=str, |
| default="out_csv/all_data.csv", help="input file name") |
| |
| args = parser.parse_args() |
| |
| main(args) |
|
|