Skip to content

Commit

Permalink
Plotting robustness; allow unrecognised distortion names (in case use…
Browse files Browse the repository at this point in the history
…r renames), and add tests
  • Loading branch information
kavanase committed May 20, 2024
1 parent c30b3bc commit 2115ac2
Show file tree
Hide file tree
Showing 19 changed files with 2,624 additions and 123 deletions.
33 changes: 21 additions & 12 deletions shakenbreak/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def _purge_data_dicts(
"""
Purges dictionaries of displacements and energies so that they are consistent
(i.e. contain data for same distortions).
To achieve this, it removes any data point from disp_dict if its energy is not
To achieve this, it removes any data point from ``disp_dict`` if its energy is not
in the energy dict (this may be due to relaxation not converged).
Args:
Expand Down Expand Up @@ -334,8 +334,8 @@ def _get_displacement_dict(
disp_dict, energies_dict = _purge_data_dicts(
disp_dict=disp_dict,
energies_dict=energies_dict,
) # make disp and energies dict consistent by removing any data point
# if its energy is not in the energy dict and viceversa
) # make disp and energies dict consistent by removing any data point from disp_dict
# if its energy is not in the energy dict (this may be due to relaxation not converged etc)
else:
warnings.warn(
"Structure comparison algorithm struggled matching lattices. "
Expand Down Expand Up @@ -389,7 +389,7 @@ def _format_datapoints_from_other_chargestates(
keys.append(float(entry.split("%")[0]) / 100)
elif isinstance(entry, str) and ("Rattled_from_" in entry or "Dimer_from_" in entry):
keys.append(0.0) # Rattled and Dimer will be plotted at x = 0.0
elif entry == "Rattled" or entry == "Dimer": # add 0.0 for Rattled
elif entry in ["Rattled", "Dimer"]: # add 0.0 for Rattled
# (to avoid problems when sorting distortions)
keys.append(0.0)
else:
Expand All @@ -398,7 +398,7 @@ def _format_datapoints_from_other_chargestates(
if disp_dict:
# Sort displacements in same order as distortions and energies,
# for proper color mapping
sorted_disp = [disp_dict[k] for k in energies_dict["distortions"] if k in disp_dict]
sorted_disp = [disp_dict.get(k, None) for k in energies_dict["distortions"]]
# Save the values of the displacements from *other charge states*
# As the displacements will be re-sorted -> we'll need to
# find the index of t
Expand Down Expand Up @@ -720,6 +720,17 @@ def _format_colorbar(
return cbar


def _parse_other_charge_state_label(distortion_key: str) -> tuple:
try:
other_charge_state = int(distortion_key.split("_")[-1])
label = f"From {'+' if other_charge_state > 0 else ''}{other_charge_state} charge state"
except ValueError:
other_charge_state = distortion_key.split("_")[-1]
label = f"From {other_charge_state}"

return label


# Main plotting functions:
def plot_all_defects(
defect_charges_dict: dict,
Expand Down Expand Up @@ -1364,7 +1375,6 @@ def plot_colorbar(
]
)
for i, j in zip(imported_indices.keys(), range(other_charges)):
other_charge_state = int(list(energies_dict["distortions"].keys())[i].split("_")[-1])
sorted_i = imported_indices[i] # index for the sorted dicts
ax.scatter( # plot any datapoints where disp could not be determined as black
np.array(keys)[i],
Expand All @@ -1382,8 +1392,9 @@ def plot_colorbar(
cmap=(colormap if isinstance(sorted_disp[sorted_i], float) else None),
norm=norm if isinstance(sorted_disp[sorted_i], float) else None,
alpha=1,
label=f"From {'+' if other_charge_state > 0 else ''}{other_charge_state} "
f"charge state",
label=_parse_other_charge_state_label(
list(energies_dict["distortions"].keys())[i]
),
)

# Plot reference energy
Expand Down Expand Up @@ -1668,7 +1679,7 @@ def plot_datasets(
)

if len(sorted_distortions) > 0 and [
key for key in dataset["distortions"] if (key != "Rattled" and key != "Dimer")
key for key in dataset["distortions"] if key not in ["Rattled", "Dimer"]
]: # more than just Rattled
if imported_indices: # Exclude datapoints from other charge states
non_imported_sorted_indices = [
Expand Down Expand Up @@ -1697,7 +1708,6 @@ def plot_datasets(
] # number of other charge states whose distortions have been imported
)
for i, j in zip(imported_indices, range(other_charges)):
other_charge_state = int(list(dataset["distortions"].keys())[i].split("_")[-1])
ax.scatter( # distortions from other charge states
np.array(keys)[i],
list(dataset["distortions"].values())[i],
Expand All @@ -1712,8 +1722,7 @@ def plot_datasets(
j
], # different markers for different charge states
alpha=1,
label=f"From {'+' if other_charge_state > 0 else ''}{other_charge_state} "
f"charge state",
label=_parse_other_charge_state_label(list(dataset["distortions"].keys())[i]),
)

datasets[0]["Unperturbed"] = 0.0 # unperturbed energy of first dataset (our reference energy)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
167 changes: 167 additions & 0 deletions tests/data/vasp/Va_O1_1/Bond_Distortion_-10.0%/CONTCAR
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
-10.0%__num_neighbours=1__Vac_O_mult32
1.0000000000000000
0.0000000000000000 10.7468629999999994 0.0000000000000000
-10.7468629999999994 0.0000000000000000 0.0000000000000000
0.0000000000000000 0.0000000000000000 9.7925889999999995
Sr Cu O
16 32 31
Direct
0.0037897070559317 0.2322286940356099 0.2474684795675954
0.5029467696266002 0.2444122619552690 0.2484263533544291
0.0034689130778392 0.7577940394743933 0.2477409557839511
0.5030501821196411 0.7452480225804839 0.2485983846999517
0.2542748979733569 -0.0052943503159013 0.7475276922958372
0.7533851164075865 -0.0050955343637915 0.7472480638453840
0.2561481059496141 0.4949101329049210 0.7474700927701745
0.7507384345264557 0.4950495930547266 0.7464732072482453
0.0035481149729006 0.4950264315443118 0.5092666850102833
0.5036822362244660 0.4948508678430448 0.4971428970167249
0.0037027816716622 -0.0047548846571159 0.5004814011572042
0.5036438978273238 -0.0051011424495776 0.4984696344865095
0.2572136410790219 0.2458828462993617 -0.0029853067565458
0.7493404840094916 0.2461189360199540 -0.0036495895073987
0.2571683435598047 0.7434663978143000 -0.0026608967288014
0.7494832484428599 0.7437789675542859 -0.0033281762959586
0.2464649050713530 0.3725406426801747 0.3665902885851178
0.7614315048742382 0.3733429297926198 0.3630423531477938
0.2532762445626128 0.8697815659344715 0.3733190889600400
0.7536481303781565 0.8697532972218815 0.3730853779814507
0.5037409345906982 0.1199010547362999 0.8720037736197943
0.0038884820403823 0.1186184229811875 0.8753004868229426
0.5035295146994602 0.6202226983900506 0.8716597460002612
0.0028321767141280 0.6142240753917423 0.8895893023188183
0.3779016296413417 0.4946628803697204 0.1251855216678644
0.8926954888254516 0.4945346097432934 0.1017250070519275
0.3786256593320883 0.9945506292804910 0.1242901498275271
0.8790680064230043 -0.0050294189848051 0.1161995488339077
0.6287192060667910 0.2452714270349685 0.6234687771075705
0.1294772919174247 0.2452829453692279 0.6204007452633590
0.6288653807657467 0.7447703959556151 0.6236656202105658
0.1295303002573071 0.7446647311873299 0.6204442101160891
0.2464141544624578 0.6180272086262101 0.3670173896975889
0.7612283554982611 0.6170227486239186 0.3639235942915107
0.2533311365352238 0.1210622428626773 0.3735390305140303
0.7537060628266189 0.1210422222109875 0.3726742153942516
0.5034147936277870 0.3694388511344822 0.8715110932647577
0.0028649140934841 0.3751498002119301 0.8896362032323032
0.5036606706323422 0.8696397643285384 0.8720180884597719
0.0040417333229443 0.8706870802849916 0.8756608414575658
0.1071866708086518 0.4945101714697508 0.0944460811396127
0.6256589822785830 0.4949528136934154 0.1235634997787016
0.1280877428764534 -0.0052647921419321 0.1162828068624879
0.6287934079116688 -0.0051944520670080 0.1245870513357437
0.3789734068459950 0.2452479424367734 0.6236213192474379
0.8785950591504228 0.2453088249375758 0.6203822443195152
0.3790668777209551 0.7443927989611656 0.6237820983986385
0.8785351581157782 0.7448486044723043 0.6203393272541239
0.5024950813566519 0.4947817003193312 0.2512324179647330
0.0034897648394232 -0.0049757171656387 0.2442950247800839
0.5035884475916053 0.9946662138654262 0.2496366304332246
0.2524204639287531 0.2455096337245567 0.7476708224628261
0.7553846999233655 0.2457863332632439 0.7472186545291423
0.2524323492731623 0.7439847831790249 0.7477736979338343
0.7555372474840261 0.7443337053963269 0.7474600996792684
0.0039432528249745 0.2416216100905275 0.0022618522362743
0.5034365937983669 0.2446621682943173 -0.0018354616972073
0.0039724212777635 0.7476249328512929 0.0024731865429819
0.5034908820707059 0.7449469114626006 -0.0017024817023552
0.2527672473286091 -0.0046799767911240 0.4979744948396508
0.7540444581857819 -0.0044229653148365 0.4975375050052958
0.2456792553479534 0.4950932318760762 0.4946637178024132
0.7605504026856679 0.4948700836791362 0.4925018216597514
0.0039498547938550 0.2477852893680156 0.4954997344840444
0.5038268606821740 0.2462225300368116 0.4976757720517880
0.0039843965581335 0.7423341416948261 0.4956141510103977
0.5039620188895495 0.7436615221947543 0.4978626909331401
0.2561653627386242 -0.0054103281138310 -0.0040995097552242
0.7510647144223463 -0.0049422285073624 0.9958809532169793
0.2575593216408595 0.4946983307431652 -0.0040380093518351
0.7465058935780393 0.4952518325744138 -0.0050963538111108
0.0022517826845059 0.4947132231288545 0.7559591208167450
0.5032674639419525 0.4948966841166381 0.7459064848376834
0.0039473411300450 -0.0054751176985068 0.7482040164396860
0.5040215192733168 -0.0052288491248106 0.7468377094934904
0.2507298030902603 0.2445865182843065 0.2460568339819935
0.7570807312709198 0.2440352147050702 0.2446351424719439
0.2506223069207714 0.7457995190913568 0.2463454024243012
0.7568932030755009 0.7462980723517217 0.2453961181754296

0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00
Loading

0 comments on commit 2115ac2

Please sign in to comment.