-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.py
More file actions
1416 lines (1205 loc) · 48.6 KB
/
analysis.py
File metadata and controls
1416 lines (1205 loc) · 48.6 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
This module file contain functions regarding the performence analysis of the BuRNN training
"""
__author__ = "Pierre-Alexandre HO"
__date__ = "2025-02-03"
__version__ = "1.0"
# standard python modules
import os
from pathlib import Path
from glob import glob
from typing import List
# NN packages
import ase
import torch
import yaml
import schnetpack.transform as trn
import schnetpack as spk
# Data & vizualisation
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import seaborn as sns
from plotly.subplots import make_subplots
from matplotlib import pyplot as plt
class YamlParser:
def __init__(self, yaml_file):
"""
Initializes the parser by loading the YAML file.
:param yaml_file: Path to the YAML file.
"""
self.data = self._load_yaml(yaml_file)
def _load_yaml(self, yaml_file):
"""
Loads and parses the YAML file.
:param yaml_file: Path to the YAML file.
:return: Parsed YAML data as a dictionary.
"""
with open(yaml_file, 'r') as file:
return yaml.safe_load(file)
def get_cutoff(self):
"""
Retrieves the cutoff value from the parsed YAML data.
:return: The cutoff value from the YAML file.
"""
try:
return self.data['globals']['cutoff']
except KeyError as e:
raise KeyError(f"Missing key in YAML file: {e}")
def get_calculator(model_path, device) -> spk.interfaces.SpkCalculator:
"""
Create a SchnetPack calculator from the given model path.
Args:
model_path (Path): Path to the SchNet model.
Returns:
spk.interfaces.SpkCalculator: A configured SchnetPack calculator.
"""
# get cutoff from configuration file
model_args_path = model_path.parent / 'config.yaml'
cutoff = YamlParser(model_args_path).get_cutoff()
model_path = os.path.join(model_path.parent,'best_model')
calculator = spk.interfaces.SpkCalculator(
model_file = model_path, # path to model
dtype=torch.float32, #
neighbor_list=trn.ASENeighborList(cutoff=cutoff), # neighbor list
energy_key='V_BuRNN', # name of energy property in model
force_key='F_BuRNN', # name of force property in model
energy_unit="eV", # units of energy property predicted by ase
device=device, # device for computation
)
return calculator
def predict_energy_and_forces(system: ase.Atoms, calculator):
"""
Predict energy and forces for the given atomic system.
Args:
system (ase.Atoms): Atomic system for which predictions are required.
Returns:
tuple: Predicted energy (float) and forces (np.ndarray).
"""
# Set the predictive calculator for the atomic system.
system.set_calculator(calculator)
# Perform predictions.
energy = system.get_potential_energy()
forces = system.get_forces()
return energy, forces
def initialize_calculator(name_model: str, base_path: str, device: torch.device):
"""
Initializes the predictive calculator using the specified model.
Parameters
----------
name_model : str
The name of the model to load.
base_path : str
The base directory where the model is stored.
device : torch.device
The device to use for computation (e.g., 'cpu' or 'cuda').
Returns
-------
Any
The initialized predictive calculator.
"""
model_path = Path(base_path) / name_model
print(model_path)
pred_calculator = get_calculator(model_path=model_path, device=device)
return pred_calculator
def parse_energy_force(data_base, pred_calculator) -> dict[str, np.ndarray]:
"""
Extracts reference and predicted energies and force magnitudes from an ASE database.
The force magnitude is computed as the Euclidean norm of the force vector
for each atom in each molecular structure.
Parameters
----------
data_base : Any
An ASE database containing molecular structures, atomic numbers,
reference energies, and force components.
pred_calculator : Any
A predictive model used to compute energy and force predictions.
Returns
-------
dict[str, np.ndarray]
A dictionary with:
- 'idx' : np.ndarray
A NumPy array containing the indices of processed structures.
- 'energies' : np.ndarray of shape (n_structures, 2)
- Column 0: Reference energy from the database.
- Column 1: Predicted energy from the model.
- 'magnitude' : np.ndarray of shape (n_structures, max_atoms, 2)
- Column 0: Reference force magnitude for each atom.
- Column 1: Predicted force magnitude for each atom.
- NaN values are used for padding in structures with fewer atoms.
"""
n_structure: int = len(data_base)
list_idx = []
energies = np.empty((n_structure, 2))
numbers_atoms = []
# Récupérer le nombre maximal d'atomes pour dimensionner le tableau 4D
for db_item in data_base:
numbers_atoms.append(int(db_item['_n_atoms']) )
if db_item['_idx'] == n_structure-1:
break
max_atoms = max(numbers_atoms)
# Initialisation du tableau 4D avec des NaN
magnitude = np.full((n_structure, max_atoms, 2, 3), np.nan)
for idx in range(n_structure):
list_idx.append(idx)
db_item = data_base[idx]
molecule = ase.Atoms(numbers=db_item['_atomic_numbers'], positions=db_item['_positions'])
energy, force = predict_energy_and_forces(molecule, pred_calculator)
# Calcul des magnitudes des forces par atome
ref = db_item['F_BuRNN'].numpy()
pred = force
# Insérer dans le tableau en conservant la structure (NaN pour padding)
n_atoms = ref.shape[0]
magnitude[idx, :n_atoms, 0] = ref
magnitude[idx, :n_atoms, 1] = pred
# Stockage des énergies
energies[idx] = [db_item['V_BuRNN'][0].numpy(), energy]
return {'idx': np.array(list_idx), 'energies': energies, 'magnitude': magnitude}
def parse_split(split: str) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
"""
Loads and extracts train, validation, and test indices from a saved NumPy file.
Parameters
----------
split : str
Path to the `.npz` file containing the split indices.
Returns
-------
tuple[np.ndarray, np.ndarray, np.ndarray]
- train (np.ndarray): Array of training set indices.
- val (np.ndarray): Array of validation set indices.
- test (np.ndarray): Array of test set indices.
"""
split = np.load(split)
train = split['train_idx']
val = split['val_idx']
test = split['test_idx']
return train, val, test
def split(idx, train, val, test):
"""
Assigns a dataset split ('train', 'val', 'test') based on index membership.
Parameters
----------
idx : int
The structure index.
train : set
Set of training indices.
val : set
Set of validation indices.
test : set
Set of test indices.
Returns
-------
str
The split category ('train', 'val', 'test') or 'error' if not found.
"""
if idx in train:
return 'train'
elif idx in val:
return 'val'
elif idx in test:
return 'test'
else:
return 'error'
def add_split(df: pd.DataFrame, train: set, val: set, test: set) -> pd.DataFrame:
"""
Adds a 'Split' column to the DataFrame indicating the dataset partition.
Parameters
----------
df : pd.DataFrame
DataFrame containing a 'structure_id' column.
train : set
Set of training indices.
val : set
Set of validation indices.
test : set
Set of test indices.
Returns
-------
pd.DataFrame
DataFrame with an added 'Split' column.
"""
df['Split'] = df['structure_id'].apply(lambda idx: split(idx, train, val, test))
return df
def compute_rmse(errors: np.ndarray) -> float:
"""
Computes the Root Mean Square Error (RMSE).
Parameters
----------
errors : np.ndarray
Array containing the prediction errors.
Returns
-------
float
The RMSE value.
"""
return np.sqrt(np.mean(errors.explode()))
def compute_mae(y_ref: np.ndarray, y_pred: np.ndarray) -> float:
"""
Computes the Mean Absolute Error (MAE).
Parameters
----------
y_ref : np.ndarray
Array of references values.
y_pred : np.ndarray
Array of predicted values.
Returns
-------
float
The MAE value.
"""
return np.mean(np.abs(y_ref - y_pred).explode())
def compute_r2(dft_error: np.ndarray, pred_error: np.ndarray) -> float:
"""
Computes the R² score with division safety check.
Parameters
----------
dft_error : np.ndarray
Array containing the dft errors.
pred_error : np.ndarray
Array containing the prediction errors.
Returns
-------
float
The R² score.
"""
denominator = np.sum(dft_error.explode())
return 1 - (np.sum(pred_error.explode()) / denominator) if denominator != 0 else float('nan')
def energy_analysis(energy_array: np.ndarray) -> pd.DataFrame:
"""
Perform energy analysis on an array of reference and predicted energy values.
Parameters
----------
energy_array : np.ndarray
A 2D numpy array where the first column contains reference energy values
and the second column contains predicted energy values.
Returns
-------
pd.DataFrame
A DataFrame containing the structure IDs, reference energy, predicted energy,
dft error, and prediction error for each structure.
Notes
-----
- NaN values in the reference and predicted energy columns are removed before analysis.
- The dft error is calculated as the squared deviation from the mean of the reference values.
- The prediction error is calculated as the squared difference between the reference and predicted values.
"""
structure_list = list(range(len(energy_array)))
y_ref = energy_array[:, 0] # Reference values
y_ref = y_ref[~np.isnan(y_ref)]
y_pred = energy_array[:, 1] # Predicted values
y_pred = y_pred[~np.isnan(y_pred)]
# Squared deviations and residuals
dft_error = (y_ref - np.mean(y_ref)) ** 2 # Total variance
pred_error = (y_ref - y_pred) ** 2 # Squared residuals
# Creating DataFrame
df = pd.DataFrame({
'structure_id': structure_list,
'dft_energy': y_ref,
'pred_energy': y_pred,
'dft_error_square': dft_error,
'pred_error_square': pred_error
})
return df
def force_analysis(magnitude_array: list[np.ndarray]) -> pd.DataFrame:
"""
Analyzes the force magnitudes from reference and predicted data.
Parameters
----------
magnitude_array : list[np.ndarray]
A list of NumPy arrays where each entry corresponds to a structure.
Each array has shape (n_atoms, 2), where:
- Column 0: Reference magnitudes
- Column 1: Predicted magnitudes
Returns
-------
pd.DataFrame
A DataFrame containing:
- 'structure_id': The index of the structure.
- 'atom_id': The index of each atom within the structure.
- 'dft_force': The reference magnitude values.
- 'pred_force': The predicted magnitude values.
- 'dft_error_square': The dft error.
- 'pred_error_square': The prediction error.
Notes
-----
- NaN values in the reference and predicted energy columns are removed before analysis.
- The dft error is calculated as the squared deviation from the mean of the reference values.
- The prediction error is calculated as the squared difference between the reference and predicted values.
"""
structure_list, atom_list = [], []
y_ref, y_pred = [], []
dft_error, pred_error = [], []
for idx, structure in enumerate(magnitude_array):
ref_values = structure[:, 0]
ref_values = ref_values[~np.isnan(ref_values).any(axis=1)]
pred_values = structure[:, 1]
pred_values = pred_values[~np.isnan(pred_values).any(axis=1)]
y_ref.extend(ref_values) # Reference values
y_pred.extend(pred_values) # Predicted values
n_atoms = len(ref_values)
structure_list.extend([idx] * n_atoms) # Repeat structure index for each atom
atom_list.extend(range(n_atoms)) # Atom indices
dft_error.extend((ref_values - np.mean(ref_values)) ** 2) # Extract dft error
pred_error.extend((ref_values - pred_values) ** 2) # Extract prediction error
# Creating DataFrame
df = pd.DataFrame({
'structure_id': structure_list,
'atom_id': atom_list,
'dft_force': y_ref,
'pred_force': y_pred,
'dft_error_square': dft_error,
'pred_error_square': pred_error
})
return df
def compute_metrics(df: pd.DataFrame) -> dict[str, float | list]:
"""
Computes prediction metrics (RMSE, MAE, R²) from a given DataFrame.
Parameters
----------
df : pd.DataFrame
DataFrame containing columns:
- 'y_ref' (np.ndarray): Reference force magnitudes or energy.
- 'y_pred' (np.ndarray): Predicted force magnitudes or energy.
- 'dft_error_square' (np.ndarray): Squared dft error values.
- 'pred_error_square' (np.ndarray): Squared prediction error values.
Returns
-------
dict[str, float if no split | list if split in df.colums]
Dictionary containing:
- 'RMSE' : Root Mean Squared Error.
- 'MAE' : Mean Absolute Error.
- 'R²' : Coefficient of determination.
"""
# Give the property name (force or energy) of the ref/pred column
ref_property = next((name for name in df.columns if name in ["dft_force", 'dft_energy']), None)
pred_property = next((name for name in df.columns if name in ["pred_force", 'pred_energy']), None)
if 'Split' in df.columns and not df['Split'].empty:
split_list = df['Split'].unique().tolist()
rmse = [compute_rmse(df['pred_error_square'])]
mae = [compute_mae(df[ref_property], df[pred_property])]
r2 = [compute_r2(df['dft_error_square'], df['pred_error_square'])]
for split in split_list:
sub_df = df[df['Split']==split]
rmse.append(compute_rmse(sub_df['pred_error_square']))
mae.append(compute_mae(sub_df[ref_property], sub_df[pred_property]))
r2.append(compute_r2(sub_df['dft_error_square'], sub_df['pred_error_square']))
split_list = ['Global'] + split_list
print(f"Metrics:")
print("+" + "-" * 12 + ("+" + "-" * 12) * len(split_list) + "+")
print("| {:^10} ".format("Metric") + "".join(f"| {split:^10} " for split in split_list) + "|")
print("+" + "-" * 12 + ("+" + "-" * 12) * len(split_list) + "+")
print("| {:^10} ".format("RMSE") + "".join(f"| {val:^10.4f} " for val in rmse) + "|")
print("| {:^10} ".format("MAE") + "".join(f"| {val:^10.4f} " for val in mae) + "|")
print("| {:^10} ".format("R²") + "".join(f"| {val:^10.4f} " for val in r2) + "|")
print("+" + "-" * 12 + ("+" + "-" * 12) * len(split_list) + "+")
return {"Split":split_list, "RMSE": rmse, "MAE": mae, "R²": r2}
else:
rmse = compute_rmse(df['pred_error_square'])
mae = compute_mae(df[ref_property], df[pred_property])
r2 = compute_r2(df['dft_error_square'], df['pred_error_square'])
print(f"Global Metrics:\n"
f"{'-'*20}\n"
f"RMSE : {rmse:.4f}\n"
f"MAE : {mae:.4f}\n"
f"R² : {r2:.4f}")
return {"RMSE": rmse, "MAE": mae, "R²": r2}
def box_ref_pred(model_df: pd.DataFrame) -> None:
"""
Generates a boxplot comparing reference and predicted energies.
Parameters
----------
model_df : pd.DataFrame
A DataFrame containing at least the following columns:
- 'structure_id' : Index of the structures.
- 'dft_energy' : Reference energy values.
- 'pred_energy' : Predicted energy values.
Returns
-------
None
Displays an interactive boxplot comparing reference and predicted energy distributions.
"""
# Convert to long format
model_df_long = model_df.melt(id_vars=["structure_id"],
value_vars=["dft_energy", "pred_energy"],
var_name="Type",
value_name="Energy")
# Box plot
fig = px.box(model_df_long, x="Type", y="Energy", title="Boxplot of Reference vs Predicted Energy", hover_name='structure_id')
fig.show()
def plot_ref_vs_pred_old(df: pd.DataFrame, ref_col: str, pred_col: str, idx_col: str, title: str, units: str,
hover_info: dict = None, split_col: str = None, save:str = None, show: bool = False)-> None:
"""
Generates a scatter plot comparing predicted and reference values, with optional coloring by dataset split.
Parameters
----------
df : pd.DataFrame
DataFrame containing reference and predicted values.
ref_col : str
Column name for reference (dft) values.
pred_col : str
Column name for predicted values.
idx_col : str
Column name for index or structure identifier (used for hover text).
title : str
Title of the plot.
units : str
Units of the plot in MathJax format: <sup>-1</sup>
hover_info : dict, optional
Dictionary containing additional columns to display in hover text.
Example: {"label": "Atom", "col": "atom_id"}
split_col : str, optional
Column name indicating dataset split (e.g., "train", "val", "test").
If provided, points will be colored based on this split.
save : str, optional
Path name of the file to save in html
show : bool, optional
Tell if the figure has to be show, by default False
Returns
-------
None
"""
fig = go.Figure()
# Add identity line (x=y)
fig.add_trace(go.Scatter(
x=df[ref_col], y=df[ref_col],
mode='lines',
name='x=y',
line=dict(color='gray', width=1, dash='dash')
))
# Manage hover text and custom data
hovertemplate = f"<b>Structure #: %{{text}}</b><br>Ref: %{{x}}<br>Pred: %{{y}}"
customdata = None
if hover_info:
hover_label = hover_info.get("label", "Custom Data")
hover_col = hover_info.get("col")
if hover_col and hover_col in df.columns:
hovertemplate = f"<b>Structure #: %{{text}}</b><br>{hover_label}: %{{customdata[0]}}<br>Ref: %{{x}}<br>Pred: %{{y}}"
customdata = df[[hover_col]].to_numpy()
# Define color mapping for splits
split_colors = {'train': 'blue', 'val': 'orange', 'test': 'green'}
if split_col and split_col in df.columns:
unique_splits = df[split_col].unique()
for split in unique_splits:
subset = df[df[split_col] == split]
fig.add_trace(go.Scatter(
x=subset[ref_col].explode(), y=subset[pred_col].explode(),
mode='markers',
marker=dict(color=split_colors.get(split, 'gray'), size=6, opacity=0.7),
hovertemplate=hovertemplate,
text=subset[idx_col],
customdata=subset[[hover_col]].to_numpy() if hover_info else None,
name=f'Predictions ({split})'
))
else:
# Default scatter plot without split coloring
fig.add_trace(go.Scatter(
x=df[ref_col], y=df[pred_col],
mode='markers',
marker=dict(color='blue', size=6, opacity=0.7),
hovertemplate=hovertemplate,
text=df[idx_col],
customdata=customdata,
name='Predictions'
))
# Styling axes
fig.update_xaxes(title_text=f'<b>Referecence Values</b>', showline=True, linewidth=2,
linecolor='black', mirror=True)
fig.update_yaxes(title_text=f'<b>Predicted {title}</b>', showline=True, linewidth=2,
linecolor='black', mirror=True)
# Layout settings
fig.update_layout(
title=f'<b>{title} Prediction vs Reference in {units}</b>',
plot_bgcolor='white',
xaxis=dict(showgrid=True, gridwidth=1, gridcolor='lightgrey', zeroline=False),
yaxis=dict(showgrid=True, gridwidth=1, gridcolor='lightgrey', zeroline=False)
)
if save:
if not save.endswith('.html'):
save = save + '.html'
fig.write_html(save)
if show:
fig.show()
def plot_ref_vs_pred(df: pd.DataFrame, ref_col: str, pred_col: str, idx_col: str, title: str, units: str,
hover_info: dict = None, split_col: str = None, save: str = None, show: bool = False)-> None:
"""
Generates a scatter plot comparing predicted and reference values, with optional coloring by dataset split.
Parameters
----------
df : pd.DataFrame
DataFrame containing reference and predicted values.
ref_col : str
Column name for reference (dft) values.
pred_col : str
Column name for predicted values.
idx_col : str
Column name for index or structure identifier (used for hover text).
title : str
Title of the plot.
units : str
Units of the plot in MathJax format: <sup>-1</sup>
hover_info : dict, optional
Dictionary containing additional columns to display in hover text.
Example: {"label": "Atom", "col": "atom_id"}
split_col : str, optional
Column name indicating dataset split (e.g., "train", "val", "test").
If provided, points will be colored based on this split.
save : str, optional
Path name of the file to save in html
Returns
-------
None
"""
fig = go.Figure()
# Manage hover text and custom data
hovertemplate = f"<b>Structure #: %{{text}}</b><br>Ref: %{{x}}<br>Pred: %{{y}}"
customdata = None
if hover_info:
hover_label = hover_info.get("label", "Custom Data")
hover_col = hover_info.get("col")
if hover_col and hover_col in df.columns:
hovertemplate = f"<b>Structure #: %{{text}}</b><br>{hover_label}: %{{customdata[0]}}<br>Ref: %{{x}}<br>Pred: %{{y}}"
customdata = df[[hover_col]].to_numpy()
# Define color mapping for splits
split_colors = {'train': 'blue', 'val': 'orange', 'test': 'green'}
if split_col and split_col in df.columns:
unique_splits = df[split_col].unique()
for split in unique_splits:
subset = df[df[split_col] == split]
ref_subset = subset[ref_col].apply(pd.Series)
pred_subset = subset[pred_col].apply(pd.Series)
# loop for x y z point
for i in range(3):
if i == 0:
value = f"{'' if ref_subset.shape[1] == 1 else 'x'}"
# Add identity line (x=y)
if split == 'train': # add only one time
fig.add_trace(go.Scatter(
x=ref_subset.iloc[:, i], y=ref_subset.iloc[:, i],
mode='lines',
name='x=y',
line=dict(color='gray', width=1, dash='dash')
))
elif i == 1:
value = 'y'
else:
value = 'z'
fig.add_trace(go.Scatter(
x=ref_subset.iloc[:, i], y=pred_subset.iloc[:, i],
mode='markers',
marker=dict(color=split_colors.get(split, 'gray'), size=6, opacity=0.7),
hovertemplate=hovertemplate,
text=subset[idx_col],
customdata=subset[[hover_col]].to_numpy() if hover_info else None,
name=f'{split.capitalize()} {value}'
))
if ref_subset.shape[1] == 1:
break
else:
# Default scatter plot without split coloring
ref_values = df[ref_col].apply(pd.Series)
pred_values = df[pred_col].apply(pd.Series)
for i in range(ref_values.shape[1]):
if i == 0:
value = f"{'' if ref_values.shape[1] == 1 else 'x'}"
# Add identity line (x=y)
fig.add_trace(go.Scatter(
x=ref_values.iloc[:, i], y=ref_values.iloc[:, i],
mode='lines',
name='x=y',
line=dict(color='gray', width=1, dash='dash')
))
elif i == 1:
value = 'y'
else:
value = 'z'
fig.add_trace(go.Scatter(
x=ref_values.iloc[:, i], y=pred_values.iloc[:, i],
mode='markers',
marker=dict(color='blue', size=6, opacity=0.7),
hovertemplate=hovertemplate,
text=df[idx_col],
customdata=customdata,
name=f'Predictions {value}'
))
if ref_values.shape[1] == 1:
break
# Styling axes
fig.update_xaxes(title_text=f'<b>Reference Values</b>', showline=True, linewidth=2,
linecolor='black', mirror=True)
fig.update_yaxes(title_text=f'<b>Predicted {title}</b>', showline=True, linewidth=2,
linecolor='black', mirror=True)
# Layout settings
fig.update_layout(
title=f'<b>{title} Prediction vs Reference in {units}</b>',
plot_bgcolor='white',
xaxis=dict(showgrid=True, gridwidth=1, gridcolor='lightgrey', zeroline=False),
yaxis=dict(showgrid=True, gridwidth=1, gridcolor='lightgrey', zeroline=False)
)
if save:
if not save.endswith('.html'):
save = save + '.html'
fig.write_html(save)
if show:
fig.show()
def plot_residuals(df: pd.DataFrame, ref_col: str, pred_col: str, idx_col: str, title: str, units: str,
split_col: str = None, hover_info: dict = None, standardized: bool = True,
save: str = None, show: bool = False)-> None:
"""
Plots residuals (standardized or not) versus predicted values.
Parameters
----------
df : pd.DataFrame
DataFrame containing reference and predicted values.
ref_col : str
Column name for reference values.
pred_col : str
Column name for predicted values.
idx_col : str
Column name for structure identifiers.
title : str
Title of the plot.
units : str
Units of the plot in MathJax format: <sup>-1</sup>
split_col : str, optional
Column name for data splits (train/val/test). Default is None.
hover_info : dict, optional
Dictionary with hover information. Should contain "label" and "col" keys. Default is None.
standardized: bool, optional
Default = True, enable to standardized the residuals
save : str, optional
Path name of the file to save in html
show : bool, optional
Tell if the figure has to be show, by default False
Returns
-------
None
"""
if not 'pred_error' in df.columns:
df['pred_error'] = df[pred_col] - df[ref_col]
flat_pred_error = df['pred_error'].explode()
flat_pred_error = flat_pred_error.reset_index(drop=True)
std_dev = np.std(flat_pred_error)
fig = go.Figure()
if standardized == True:
df['std_residuals'] = df['pred_error'] / std_dev
residual_col = 'std_residuals'
y_title = 'Standardized residuals'
# Add threshold lines
fig.add_hline(y=0, line_width=2, line_dash="dash", line_color="black", layer="below")
fig.add_hline(y=-2, line_width=2, line_dash="dash", line_color="red", layer="below")
fig.add_hline(y=2, line_width=2, line_dash="dash", line_color="red", layer="below")
else:
residual_col = 'pred_error'
y_title = 'Residuals'
avg = np.mean(flat_pred_error)
# Add threshold lines
fig.add_hline(y=np.mean(flat_pred_error), line_width=2, line_dash="dash", line_color="black", layer="below")
fig.add_hline(y=avg-std_dev, line_width=2, line_dash="dash", line_color="red", layer="below")
fig.add_hline(y=avg+std_dev, line_width=2, line_dash="dash", line_color="red", layer="below")
# Manage hover text and custom data
hovertemplate = f"<b>Structure #: %{{text}}</b><br>Ref: %{{customdata[0]:.3f}}<br>Pred: %{{x:.3f}}"
customdata = df[[ref_col]].to_numpy()
if hover_info:
hover_label = hover_info.get("label", "Custom Data")
hover_col = hover_info.get("col")
if hover_col and hover_col in df.columns:
hovertemplate = f"<b>Structure #: %{{text}}</b><br>{hover_label}: %{{customdata[0]}}<br>Ref: %{{customdata[1]:.3f}}<br>Pred: %{{x:.3f}}"
customdata = df[[hover_col]].to_numpy()
# Define color mapping for splits
split_colors = {'train': 'blue', 'val': 'orange', 'test': 'green'}
if split_col and split_col in df.columns: #if not None AND split in df
unique_splits = df[split_col].unique()
for split in unique_splits:
subset = df[df[split_col] == split]
pred_subset = subset[pred_col].apply(pd.Series)
residual_subset = subset[residual_col].apply(pd.Series)
# loop for x y z point
for i in range(3):
if i == 0:
value = f"{'' if pred_subset.shape[1] == 1 else 'x'}"
elif i == 1:
value = 'y'
else:
value = 'z'
fig.add_trace(go.Scatter(
x=pred_subset.iloc[:, i], y=residual_subset.iloc[:, i],
mode='markers',
marker=dict(color=split_colors.get(split, 'gray'), size=6, opacity=0.7),
hovertemplate=hovertemplate,
text=subset[idx_col],
customdata = (
subset[['atom_id', 'dft_force']].to_numpy()
if 'atom_id' in subset.columns and 'dft_force' in subset.columns
else subset[['dft_energy']].to_numpy()
),
name=f'{split.capitalize()} {value}'
))
if pred_subset.shape[1] == 1:
break
else:
# Default scatter plot without split coloring
residual_values = df[residual_col].apply(pd.Series)
pred_values = df[pred_col].apply(pd.Series)
for i in range(3):
if i == 0:
value = f"{'' if pred_values.shape[1] == 1 else 'x'}"
elif i == 1:
value = 'y'
else:
value = 'z'
fig.add_trace(go.Scatter(
x=pred_values.iloc[:, i], y=residual_values.iloc[:, i],
mode='markers',
marker=dict(color='blue', size=6, opacity=0.7),
hovertemplate=hovertemplate,
text=df[idx_col],
customdata=customdata,
name=f'Residuals {value}'
))
if pred_values.shape[1] == 1:
break
# Styling & legend part
fig.update_xaxes(title_text=f'<b>{title} predicted ({units})</b>', showgrid=False, showline=True, linewidth=2, linecolor='black')
fig.update_yaxes(title_text=f'<b>{y_title} ({units})</b>', showgrid=False, showline=True, linewidth=2, linecolor='black')
fig.add_trace(go.Scatter(x=[None], y=[None], mode='lines', name='2 Standard Deviation',
line=dict(color='red', width=1, dash='dash')))
fig.update_layout(
title=dict(text=f'<b>{title} {y_title.lower()} versus predicted values</b>',
subtitle=dict(
text=f'Estimated Standard Deviation σ: {std_dev:.4f} {units}',
font=dict(color='gray', size =13)
)),
plot_bgcolor='white',
legend=dict(
itemsizing='constant',
itemwidth=30,
y=1.0,
x=1.0,
xanchor='right',
yanchor='top'
)
)
if save:
if not save.endswith('.html'):
save = save + '.html'
fig.write_html(save)
if show:
fig.show()
def plot_residual_density(df: pd.DataFrame, ref_col: str, pred_col: str, units: str, split_col: str = 'Split',
save: str = None, show: bool = False) -> None:
"""
Plots the density of residuals for a given DataFrame.
This function calculates the residuals between predicted and reference values,
then plots the density of these residuals using Kernel Density Estimation (KDE).
It also adds vertical lines to indicate ±2 standard deviations from the mean.
Parameters
----------
df : pd.DataFrame
The DataFrame containing the data. It must include the predicted and reference columns.
ref_col : str
The column name for reference values.
pred_col : str
The column name for predicted values.
units : str
Units of the plot in LaTeX format: $^{-1}$
split_col : str, optional
The column name for split categories (default is 'Split').
save : str, optional
Path name of the file to save in svg
show : bool, optional
Tell if the figure has to be show, by default False
Returns
-------
None
This function displays a plot and does not return any value.
Notes
-----
- The function assumes that the DataFrame `df` contains the specified columns.
- The 'pred_error' column is created if it does not already exist.
"""
# Check if 'pred_error' column exists, if not, create it
if 'pred_error' not in df.columns:
df['pred_error'] = df[pred_col] - df[ref_col]
residual_col = 'pred_error'
# Check if split_col exists in the DataFrame
if split_col not in df.columns:
split_col = None
unique_labels = ['density']
else:
unique_labels = list(reversed(sorted(df['Split'].unique().tolist())))
# Flatten the 'pred_error' column
flat_pred_error = df['pred_error'].explode()
flat_pred_error = flat_pred_error.reset_index(drop=True)
# Calculate standard deviation and mean
std_dev = np.std(flat_pred_error)
avg = np.mean(flat_pred_error)
# Convert 'pred_error' to a DataFrame for plotting
residuals = df[residual_col].apply(pd.Series)
res_shape = residuals.shape[1]
# Plot setup
fig, ax = plt.subplots(1, res_shape, sharey=True, tight_layout=True, figsize=(5*res_shape, 5))
fig.suptitle('Density Plot of Residuals', fontsize=16)