Skip to content

Commit 66db562

Browse files
committed
document args missing from doc strings
standardize to Google doc str format add type hints
1 parent 1e9dacc commit 66db562

File tree

9 files changed

+166
-116
lines changed

9 files changed

+166
-116
lines changed

crystal_toolkit/apps/main.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from dash.exceptions import PreventUpdate
1616
from flask_caching import Cache
1717
from monty.serialization import loadfn
18+
from pymatgen.core import Structure
1819
from pymatgen.core import __version__ as pmg_version
1920
from pymatgen.ext.matproj import MPRester, MPRestError
2021

@@ -455,7 +456,9 @@ def update_search_term_on_page_load(href: str) -> str:
455456
[Input(search_component.id("input"), "value")],
456457
[State(search_component.id("input"), "n_submit")],
457458
)
458-
def perform_search_on_page_load(search_term: str, n_submit: int | None):
459+
def perform_search_on_page_load(
460+
search_term: str, n_submit: int | None
461+
) -> tuple[int, int]:
459462
"""
460463
Loading with an mpid in the URL requires populating the search term with
461464
the mpid, this callback forces that search to then take place by force updating
@@ -495,7 +498,9 @@ def update_url_pathname_from_search_term(mpid: str | None) -> str:
495498
Output(transformation_component.id("input_structure"), "data"),
496499
[Input(search_component.id(), "data"), Input(upload_component.id(), "data")],
497500
)
498-
def master_update_structure(search_mpid: str | None, upload_data: dict | None):
501+
def master_update_structure(
502+
search_mpid: str | None, upload_data: dict | None
503+
) -> Structure:
499504
"""
500505
A new structure is loaded either from the search component or from the
501506
upload component. This callback triggers the update, and uses the callback

crystal_toolkit/components/diffraction.py

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import math
24

35
import numpy as np
@@ -33,7 +35,7 @@ def __init__(self, *args, initial_structure=None, **kwargs):
3335
super().__init__(*args, **kwargs)
3436
self.create_store("structure", initial_data=initial_structure)
3537

36-
def layout(self):
38+
def layout(self) -> Columns:
3739

3840
voltage = self.get_numerical_input(
3941
kwarg_label="voltage",
@@ -84,7 +86,7 @@ def generate_diffraction_pattern(structure, *args):
8486
return dcc.Graph(
8587
figure=calculator.get_plot_2d(structure),
8688
responsive=False,
87-
config={"displayModeBar": False, "displaylogo": False},
89+
config=dict(displayModeBar=False, displaylogo=False),
8890
)
8991

9092

@@ -166,32 +168,39 @@ def V(x, c, alphagamma):
166168
)
167169

168170
@staticmethod
169-
def twotheta_to_q(twotheta, xray_wavelength):
170-
"""
171-
Convert twotheta to Q.
171+
def two_theta_to_q(two_theta: float, xray_wavelength: float) -> float:
172+
"""Angular conversion from 2*theta to q in small-angle scattering (SAS).
172173
173-
:param twotheta: in degrees
174-
:param xray_wavelength: in Ångstroms
175-
:return:
174+
Args:
175+
two_theta (float): in degrees
176+
xray_wavelength (float): in Ångstroms
177+
178+
Returns:
179+
float: Q in Ångstroms^-1
176180
"""
177181
# thanks @rwoodsrobinson
178-
return (4 * np.pi / xray_wavelength) * np.sin(np.deg2rad(twotheta) / 2)
182+
return (4 * np.pi / xray_wavelength) * np.sin(np.deg2rad(two_theta) / 2)
179183

180184
@staticmethod
181-
def grain_to_hwhm(tau, two_theta, K=0.9, wavelength="CuKa"):
182-
"""
183-
:param tau: grain size in nm
184-
:param two_theta: angle (in 2-theta)
185-
:param K: shape factor (default 0.9)
186-
:param wavelength: wavelength radiation in nm
187-
:return: half-width half-max (alpha or gamma), for line profile
185+
def grain_to_hwhm(
186+
tau: float, two_theta: float, K: float = 0.9, wavelength: float | str = "CuKa"
187+
) -> float:
188+
"""_summary_
189+
190+
Args:
191+
tau (float): grain size in nm
192+
two_theta (float): angle (in 2-theta)
193+
K (float, optional): shape factor (default 0.9). Defaults to 0.9.
194+
wavelength (float | str, optional): wavelength radiation in nm. Defaults to "CuKa".
195+
196+
Returns:
197+
float: half-width half-max (alpha or gamma), for line profile
188198
"""
189199
if isinstance(wavelength, str):
190200
wavelength = WAVELENGTHS[wavelength]
201+
# Scherrer equation for half-width half max
191202
# factor of 0.1 to convert wavelength to nm
192-
return (
193-
0.5 * K * 0.1 * wavelength / (tau * abs(np.cos(two_theta / 2)))
194-
) # Scherrer equation for half-width half max
203+
return 0.5 * K * 0.1 * wavelength / (tau * abs(np.cos(two_theta / 2)))
195204

196205
@property
197206
def _sub_layouts(self):
@@ -200,7 +209,7 @@ def _sub_layouts(self):
200209
"peak_profile": "G",
201210
"shape_factor": 0.94,
202211
"rad_source": "CuKa",
203-
"x_axis": "twotheta",
212+
"x_axis": "two_theta",
204213
"crystallite_size": 0.1,
205214
}
206215

@@ -288,7 +297,7 @@ def _sub_layouts(self):
288297
help_str="Can choose between 2𝜃 or Q, where Q is the magnitude of the reciprocal lattice and "
289298
"independent of radiation source.", # TODO: improve
290299
options=[
291-
{"label": "2𝜃", "value": "twotheta"},
300+
{"label": "2𝜃", "value": "two_theta"},
292301
{"label": "Q", "value": "Q"},
293302
],
294303
)
@@ -323,14 +332,16 @@ def _sub_layouts(self):
323332
"static_image": static_image,
324333
}
325334

326-
def layout(self, static_image=False):
327-
"""
328-
Get the standard XRD diffraction pattern layout.
335+
def layout(self, static_image: bool = False) -> Columns:
336+
"""Get the standard XRD diffraction pattern layout.
329337
330-
:param static_image: If True, will show a static image instead of an interactive graph.
331-
:return:
332-
"""
338+
Args:
339+
static_image (bool, optional): If True, will show a static image instead of an interactive graph.
340+
Defaults to False.
333341
342+
Returns:
343+
Columns: from crystal_toolkit.helpers.layouts
344+
"""
334345
sub_layouts = self._sub_layouts
335346
if static_image:
336347
inner = sub_layouts["static_image"]
@@ -415,10 +426,10 @@ def get_figure(
415426
layout = XRayDiffractionComponent.default_xrd_plot_style
416427

417428
if x_axis == "Q":
418-
x_peak = XRayDiffractionComponent.twotheta_to_q(
429+
x_peak = XRayDiffractionComponent.two_theta_to_q(
419430
x_peak, WAVELENGTHS[rad_source]
420431
)
421-
x = XRayDiffractionComponent.twotheta_to_q(x, WAVELENGTHS[rad_source])
432+
x = XRayDiffractionComponent.two_theta_to_q(x, WAVELENGTHS[rad_source])
422433
layout["xaxis"]["title"] = "Q / Å⁻¹"
423434
else:
424435
layout["xaxis"]["title"] = "2𝜃 / º"

crystal_toolkit/components/phase_diagram.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -715,11 +715,21 @@ def create_table(chemsys, pd_time, n_clicks, pd, rows):
715715
Input(self.id("chemsys-external"), "data"),
716716
],
717717
)
718-
def get_chemsys_from_mpid_or_chemsys(mpid, chemsys_external: str):
719-
"""
720-
:param mpid: mpid
721-
:param chemsys_external: chemsys, e.g. "Co-O"
722-
:return: chemsys
718+
def get_chemsys_from_mpid_or_chemsys(
719+
mpid: str, chemsys_external: str
720+
) -> str | None:
721+
"""Get the chemical system as a string of elements sorted alphabetically and joined by dashes,
722+
by convention for use in database keys.
723+
724+
Args:
725+
mpid (str): MP material ID.
726+
chemsys_external (str): chemsys, e.g. "Co-O"
727+
728+
Raises:
729+
PreventUpdate: ctx is None or not triggered or trigger["value"] is None.
730+
731+
Returns:
732+
str | None: chemical system, e.g. "O-Si" for SiO2.
723733
"""
724734
ctx = dash.callback_context
725735

crystal_toolkit/components/pourbaix.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ def get_pourbaix_diagram(pourbaix_entries, **kwargs):
751751
Input(self.get_all_kwargs_id(), "value"),
752752
],
753753
)
754-
def make_figure(pourbaix_entries, *args):
754+
def make_figure(pourbaix_entries, *args) -> go.Figure:
755755

756756
if pourbaix_entries is None:
757757
raise PreventUpdate

crystal_toolkit/components/structure.py

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -115,33 +115,40 @@ def __init__(
115115
show_position_button: bool = DEFAULTS["show_position_button"],
116116
**kwargs,
117117
):
118+
"""Create a StructureMoleculeComponent from a structure or molecule.
119+
120+
Args:
121+
struct_or_mol (None |, optional): input structure or molecule. Defaults to None.
122+
id (str, optional): canonical id. Defaults to None.
123+
className (str, optional): extra geometric elements to add to the 3D scene. Defaults to "box".
124+
scene_additions (Scene | None, optional): bonding strategy from pymatgen NearNeighbors class.
125+
Defaults to None.
126+
bonding_strategy (str, optional): options for the bonding strategy.
127+
bonding_strategy_kwargs (dict | None, optional): color scheme, see Legend class.
128+
Defaults to None.
129+
color_scheme (str, optional): color scale, see Legend class.
130+
color_scale (str | None, optional): radius strategy, see Legend class.
131+
Defaults to None.
132+
radius_strategy (str, optional): optional): radius strategy, see Legend class.
133+
unit_cell_choice (str, optional): whether to draw repeats of atoms on periodic images.
134+
draw_image_atoms (bool, optional): whether to draw sites bonded outside the unit cell.
135+
bonded_sites_outside_unit_cell (bool, optional): whether to hide or show incomplete bonds.
136+
Defaults to DEFAULTS[ "bonded_sites_outside_unit_cell" ].
137+
hide_incomplete_bonds (bool, optional): whether to hide or show the compass.
138+
show_compass (bool, optional): scene settings (lighting etc.) to pass to CrystalToolkitScene.
139+
scene_settings (dict | None, optional): a site property used for grouping of atoms for
140+
mouseover/interaction. Defaults to None.
141+
group_by_site_property (str | None, optional): a site property used for grouping of atoms for
142+
mouseover/interaction. Defaults to None.
143+
show_legend (bool, optional): optional): show or hide legend panel within the scene.
144+
show_settings (bool, optional): show or hide scene control bar.
145+
show_controls (bool, optional): show or hide the full screen button within the scene control bar.
146+
show_expand_button (bool, optional): show or hide the image download button within the scene control bar.
147+
show_image_button (bool, optional): show or hide the file export button within the scene control bar.
148+
show_export_button (bool, optional): show or hide the revert position button within the scene control bar.
149+
show_position_button (bool, optional): extra keyword arguments to pass to MPComponent. e.g. Wyckoff label.
150+
**kwargs: a CSS dimension specifying width/height of Div.
118151
"""
119-
Create a StructureMoleculeComponent from a structure or molecule.
120-
121-
:param struct_or_mol: input structure or molecule
122-
:param id: canonical id
123-
:param scene_additions: extra geometric elements to add to the 3D scene
124-
:param bonding_strategy: bonding strategy from pymatgen NearNeighbors class
125-
:param bonding_strategy_kwargs: options for the bonding strategy
126-
:param color_scheme: color scheme, see Legend class
127-
:param color_scale: color scale, see Legend class
128-
:param radius_strategy: radius strategy, see Legend class
129-
:param draw_image_atoms: whether to draw repeats of atoms on periodic images
130-
:param bonded_sites_outside_unit_cell: whether to draw sites bonded outside the unit cell
131-
:param hide_incomplete_bonds: whether to hide or show incomplete bonds
132-
:param show_compass: whether to hide or show the compass
133-
:param scene_settings: scene settings (lighting etc.) to pass to CrystalToolkitScene
134-
:param group_by_site_property: a site property used for grouping of atoms for mouseover/interaction,
135-
:param show_legend: show or hide legend panel within the scene
136-
:param show_controls: show or hide scene control bar
137-
:param show_expand_button: show or hide the full screen button within the scene control bar
138-
:param show_image_button: show or hide the image download button within the scene control bar
139-
:param show_export_button: show or hide the file export button within the scene control bar
140-
:param show_position_button: show or hide the revert position button within the scene control bar
141-
e.g. Wyckoff label
142-
:param kwargs: extra keyword arguments to pass to MPComponent
143-
"""
144-
145152
super().__init__(id=id, default_data=struct_or_mol, **kwargs)
146153
self.className = className
147154
self.show_legend = show_legend
@@ -898,9 +905,13 @@ def _sub_layouts(self):
898905
}
899906

900907
def layout(self, size: str = "500px") -> html.Div:
901-
"""
902-
:param size: a CSS dimension specifying width/height of Div
903-
:return: A html.Div containing the 3D structure or molecule
908+
"""Get the layout for this component.
909+
910+
Args:
911+
size (str, optional): a CSS dimension specifying width/height of Div. Defaults to "500px".
912+
913+
Returns:
914+
html.Div: A html.Div containing the 3D structure or molecule
904915
"""
905916
return html.Div(
906917
self._sub_layouts["struct"], style={"width": size, "height": size}

0 commit comments

Comments
 (0)