Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into real-interactive-un…
Browse files Browse the repository at this point in the history
…it-test
  • Loading branch information
DanielYang59 committed Jan 4, 2025
2 parents 54dba4c + eb12217 commit 0042689
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repos:
types_or: [python, jupyter]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.13.0
rev: v1.14.0
hooks:
- id: mypy
additional_dependencies: [types-requests, types-PyYAML]
Expand Down
75 changes: 73 additions & 2 deletions pymatviz/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,78 @@ def label(self) -> str:

@property
def unit(self) -> str | None:
"""Unit associated with the key."""
return _keys[self.value].get("unit")
"""Unit associated with the key with HTML tags for sub/superscripts."""
if not (unit := _keys[self.value].get("unit")):
return None

# Map Unicode characters to their ASCII equivalents
superscript_map = {
"⁰": "0",
"¹": "1",
"²": "2",
"³": "3",
"⁴": "4",
"⁵": "5",
"⁶": "6",
"⁷": "7",
"⁸": "8",
"⁹": "9",
"⁻": "-",
"½": "1/2",
}
subscript_map = {
"₀": "0",
"₁": "1",
"₂": "2",
"₃": "3",
"₄": "4",
"₅": "5",
"₆": "6",
"₇": "7",
"₈": "8",
"₉": "9",
"₋": "-",
"½": "1/2",
}

# Process character by character
html_str = ""
in_super = in_sub = False

idx = 0
while idx < len(unit):
char = unit[idx]

# Check if character is superscript
if new_char := superscript_map.get(char):
if not in_super:
html_str += "<sup>"
in_super = True
html_str += new_char
# Check if character is subscript
elif new_char := subscript_map.get(char):
if not in_sub:
html_str += "<sub>"
in_sub = True
html_str += new_char
else:
# Close any open tags
if in_super:
html_str += "</sup>"
in_super = False
if in_sub:
html_str += "</sub>"
in_sub = False
html_str += char
idx += 1

# Close any remaining open tags
if in_super:
html_str += "</sup>"
if in_sub:
html_str += "</sub>"

return html_str

@property
def category(self) -> str:
Expand Down Expand Up @@ -296,6 +366,7 @@ def __reduce_ex__(self, proto: object) -> tuple[type, tuple[str]]:
cohesive_energy_per_atom = "cohesive_energy_per_atom"
heat_of_formation = "heat_of_formation"
heat_of_reaction = "heat_of_reaction"
e_form = "e_form"
e_form_per_atom = "e_form_per_atom"
e_form_pred = "e_form_pred"
e_form_true = "e_form_true"
Expand Down
28 changes: 17 additions & 11 deletions pymatviz/keys.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ electronic:
mobility:
label: Carrier Mobility
symbol: μ
unit: cm²/V·s
unit: cm²/Vs
effective_mass:
label: Effective Mass
symbol: m<sub>e</sub>
Expand Down Expand Up @@ -184,11 +184,11 @@ electronic:
electron_mobility:
label: Electron Mobility
symbol: μ<sub>e</sub>
unit: cm²/V·s
unit: cm²/Vs
hole_mobility:
label: Hole Mobility
symbol: μ<sub>h</sub>
unit: cm²/V·s
unit: cm²/Vs

thermodynamic:
energy:
Expand Down Expand Up @@ -248,6 +248,11 @@ thermodynamic:
symbol: E<sub>coh</sub>/N<sub>atoms</sub>
unit: eV/atom
description: Energy required to break crystal into isolated neutral atoms
e_form:
label: E<sub>form</sub>
symbol: E<sub>form</sub>
unit: eV/atom
description: All-atoms formation energy relative to constituent element reference states
e_form_per_atom:
label: E<sub>form</sub>
symbol: E<sub>form</sub>/N<sub>atoms</sub>
Expand Down Expand Up @@ -354,35 +359,35 @@ mechanical:
fracture_toughness:
label: Fracture Toughness
symbol: K<sub>IC</sub>
unit: MPa·m<sup>1/2</sup>
unit: MPa⋅m½
sound_velocity:
label: Sound Velocity
symbol: v<sub>s</sub>
unit: m/s
thermal_conductivity:
label: Thermal Conductivity
symbol: κ
unit: W/m·K
unit: W/mK
thermal_expansion:
label: Thermal Expansion
symbol: α
unit: 1/K
lattice_thermal_conductivity:
label: Lattice Thermal Conductivity
symbol: κ<sub>lattice</sub>
unit: W/m·K
unit: W/mK
electronic_thermal_conductivity:
label: Electronic Thermal Conductivity
symbol: κ<sub>electronic</sub>
unit: W/m·K
unit: W/mK
heat_capacity:
label: Heat Capacity
symbol: C<sub>V</sub>
unit: J/K
specific_heat_capacity:
label: Specific Heat Capacity
symbol: c<sub>p</sub>
unit: J/kg·K
unit: J/kgK
thermal_expansion_coefficient:
label: Thermal Expansion Coefficient
symbol: α<sub>thermal</sub>
Expand All @@ -398,7 +403,7 @@ mechanical:
viscosity:
label: Viscosity
symbol: η
unit: Pa·s
unit: Pas
strain:
label: Strain
symbol: ε
Expand Down Expand Up @@ -456,7 +461,7 @@ thermal:
thermal_resistivity:
label: Thermal Resistivity
symbol: ρ<sub>th</sub>
unit: K·m/W
unit: Km/W
description: Resistance to heat flow, inverse of thermal conductivity
thermal_time_constant:
label: Thermal Time Constant
Expand All @@ -473,7 +478,7 @@ magnetic:
magnetic_moment:
label: Magnetic Moment
symbol: μ<sub>B</sub>
unit: μB
unit: μ<sub>B</sub>
magmoms:
label: Magnetic Moments
symbol: μ
Expand Down Expand Up @@ -1134,6 +1139,7 @@ metrics:
volume_error:
label: Volume Error
symbol: V<sub>err</sub>
unit: ų
max_force_error:
label: Max Force Error
max_stress_error:
Expand Down
2 changes: 1 addition & 1 deletion pymatviz/ptable/ptable_plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def ptable_heatmap_plotly(
if border is not False:
border = border or {}
border_color = border.pop("color", "darkgray")
border_width = border.pop("width", 2)
border_width = border.pop("width", 0.5)

common_kwargs = dict(
z=np.where(tile_texts, 1, np.nan), showscale=False, hoverinfo="none"
Expand Down
41 changes: 41 additions & 0 deletions tests/ptable/plotly/test_ptable_plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,44 @@ def custom_colorscale(_element_symbol: str, _value: float, split_idx: int) -> st
for trace in fig.data:
# Each element tile should have a color array with custom colors
assert trace.fillcolor in {"rgb(255,0,0)", "rgb(0,0,255)"}


def test_ptable_heatmap_plotly_colorbar() -> None:
"""Test colorbar customization in ptable_heatmap_plotly."""
data = {"Fe": 1.234, "O": 5.678}

# Test colorbar title and formatting
colorbar = dict(
title="Test Title", tickformat=".2f", orientation="v", len=0.8, x=1.1
)

fig = pmv.ptable_heatmap_plotly(data, colorbar=colorbar)

# Get the colorbar from the figure
colorbar_trace = next(trace for trace in fig.data if hasattr(trace, "colorbar"))
actual_colorbar = colorbar_trace.colorbar

# Check colorbar properties were set correctly
assert actual_colorbar.title.text == "<br><br>Test Title"
assert actual_colorbar.tickformat == ".2f"
assert actual_colorbar.orientation == "v"
assert actual_colorbar.len == 0.8
assert actual_colorbar.x == 1.1

# Test horizontal colorbar title formatting
h_colorbar = dict(title="Horizontal Title", orientation="h", y=0.8)

fig = pmv.ptable_heatmap_plotly(data, colorbar=h_colorbar)
h_colorbar_trace = next(trace for trace in fig.data if hasattr(trace, "colorbar"))
actual_h_colorbar = h_colorbar_trace.colorbar

# Check horizontal colorbar properties
assert (
actual_h_colorbar.title.text == "Horizontal Title<br>"
) # Horizontal title has break after
assert actual_h_colorbar.orientation == "h"
assert actual_h_colorbar.y == 0.8

# Test disabling colorbar
fig = pmv.ptable_heatmap_plotly(data, show_scale=False)
assert not any(trace.showscale for trace in fig.data)
Loading

0 comments on commit 0042689

Please sign in to comment.