diff --git a/.jules/bolt.md b/.jules/bolt.md index d87bd6d..9c452cf 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -8,3 +8,7 @@ ## 2024-03-29 - ASE Custom JSON encoding vs standard JSON **Learning:** ASE's custom JSON encoder (`ase.io.jsonio.encode`) will generate dicts with special keys like `__ndarray__` or `__complex__` (e.g. `{"__ndarray__": [[5], "int64", ...]}`). When optimizing JSON deserialization using faster alternatives like `orjson`, it's critical to realize that a normal `json.loads` or `orjson.loads` will deserialize this into a Python dictionary, while ASE's custom `decode` will properly reconstruct the underlying numpy array. Bypassing ASE's decoder without checking for these keys leads to downstream type errors (e.g. `KeyError: '__ndarray__'`). **Action:** When replacing or wrapping ASE's jsonio with `orjson`, always fall back to ASE's `decode` if the payload string contains `__ndarray__` or `__complex__` markers, to ensure custom objects are correctly reconstructed. + +## 2024-06-28 - Optimizing Pandas DataFrame Iteration +**Learning:** Iterating over a DataFrame using `df.iterrows()` wraps every single row into a Pandas Series object. This abstraction is incredibly slow and a notorious bottleneck in Python performance, particularly for larger dataframes or within comprehension expressions. +**Action:** When a full pass iteration through a Pandas dataframe is necessary (e.g. creating dictionary mappings), immediately convert the dataframe to a list of native Python dictionaries using `df.to_dict("records")`. Iterating over this list skips the Pandas Series wrapper overhead, providing massive performance boosts for lookup generations. diff --git a/src/lavello_mlips/verify_processed_omol25.py b/src/lavello_mlips/verify_processed_omol25.py index 9d1c47c..72c8f86 100644 --- a/src/lavello_mlips/verify_processed_omol25.py +++ b/src/lavello_mlips/verify_processed_omol25.py @@ -77,8 +77,15 @@ def main() -> None: ) logger.info(f"Loaded {len(df)} records from Parquet.") - parquet_by_sha = {row["geom_sha1"]: row for _, row in df.iterrows()} - parquet_by_argone_rel = {row["argonne_rel"]: row for _, row in df.iterrows()} + + # PERFORMANCE OPTIMIZATION: + # Converting the DataFrame to a list of dictionaries using `to_dict("records")` + # and iterating over the list is significantly faster than using `df.iterrows()`. + # `iterrows()` adds massive overhead by wrapping every row into a Pandas Series. + df_records = df.to_dict("records") + parquet_by_sha = {row["geom_sha1"]: row for row in df_records} + parquet_by_argone_rel = {row["argonne_rel"]: row for row in df_records} + logger.info(f"Loading ExtXYZ file from {args.extxyz} (this may take a moment)...") all_atoms = read(str(args.extxyz), index=":") if not isinstance(all_atoms, list): @@ -108,7 +115,7 @@ def get_dump_entry(at): info = dict(at.info) rel = info.get("argonne_rel") pq_row = parquet_by_argone_rel.get(rel) - pq_data = pq_row.to_dict() if pq_row is not None else None + pq_data = pq_row return {"xyz": info, "parquet": pq_data} duplicates = {