Skip to content

Commit 6206430

Browse files
amdeckergtrevisanyumouwei
authored
Add docstrings for modules/classes/methods (#325)
* Add docstrings for modules/classes/methods * revisions * update pylint ignore * disable docstring checks for multiprocessing * minor whitespace changes * fixup! minor whitespace changes * ignore small ad-hoc classes in math * Minor changes to /machine/ files * minor change to d3d _get_efit_dict docstring * move get_n1_bradial_parameters into drafts * Update DummyDatabase docstring * minor docstring updates * Update _get_ohmic_parameters docstring * Minor mods to physics.py for both cmod & d3d --------- Co-authored-by: gtrevisan <gtrevisan@users.noreply.github.com> Co-authored-by: yumouwei <46117079+yumouwei@users.noreply.github.com>
1 parent 0d41951 commit 6206430

57 files changed

Lines changed: 2667 additions & 438 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

disruption_py/config.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/usr/bin/env python3
22

3+
"""
4+
Loads configuration settings using Dynaconf for a given tokamak.
5+
"""
6+
37
import os
48
from enum import Enum
59
from typing import Union
@@ -10,6 +14,19 @@
1014

1115

1216
def config(tokamak: Union[Enum, str] = None):
17+
"""
18+
Load and cache the configuration.
19+
20+
Parameters
21+
----------
22+
tokamak : Union[Enum, str], optional
23+
Tokamak name or Enum. Defaults to "default".
24+
25+
Returns
26+
-------
27+
Dynaconf
28+
Configuration settings.
29+
"""
1330
if tokamak is None:
1431
tokamak = "default"
1532
elif isinstance(tokamak, Enum):

disruption_py/core/multiprocess.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env python3
22

3+
# TODO: https://github.com/MIT-PSFC/disruption-py/pull/271
4+
# pylint: disable=missing-class-docstring
5+
# pylint: disable=missing-function-docstring
6+
# pylint: disable=missing-module-docstring
7+
38
import multiprocessing
49
import threading
510
from enum import Enum

disruption_py/core/physics_method/caching.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env python3
22

3+
"""
4+
This module provides decorators and utility functions for caching the results of
5+
expensive method calls in physics calculations.
6+
"""
7+
38
import functools
49
import threading
510
from typing import Callable, List
@@ -11,16 +16,22 @@
1116

1217

1318
def cache_method(method: Callable) -> Callable:
14-
"""Decorates a function as a cached method and instantiates its cache.
19+
"""
20+
Decorates a function as a cached method and instantiates its cache.
1521
1622
Cached methods are functions that run expensive operations on data in the shot
1723
and may be reused. The cache is used to store the results of the parameter
1824
method so that it is only calculated once per shot for a given timebase.
1925
2026
Parameters
2127
----------
22-
method: Callable
28+
method : Callable
2329
The method to be cached.
30+
31+
Returns
32+
-------
33+
Callable
34+
The wrapped method with caching functionality.
2435
"""
2536

2637
@functools.wraps(method)
@@ -51,6 +62,23 @@ def wrapper(*args, **kwargs):
5162
def get_method_cache_key(
5263
method: Callable, times: np.ndarray, other_params: dict = None
5364
):
65+
"""
66+
Generate a cache key for the specified method and parameters.
67+
68+
Parameters
69+
----------
70+
method : Callable
71+
The method for which the cache key is being generated.
72+
times : np.ndarray
73+
An array of time values, where the first and last times are used in the key.
74+
other_params : dict, optional
75+
A dictionary of additional parameters that may affect the cache key. Default is None.
76+
77+
Returns
78+
-------
79+
tuple
80+
A tuple representing the cache key.
81+
"""
5482
current_thread_id = threading.get_ident()
5583
hashable_other_params = frozenset((other_params or {}).items())
5684
return (
@@ -70,6 +98,27 @@ def manually_cache(
7098
method_name: str,
7199
method_columns: List[str],
72100
) -> bool:
101+
"""
102+
Manually cache results based on the provided DataFrame and method details.
103+
104+
Parameters
105+
----------
106+
physics_method_params : PhysicsMethodParams
107+
The parameters containing the shot ID and logger for logging.
108+
data : pd.DataFrame
109+
The DataFrame containing the data to be cached.
110+
method : Callable
111+
The method for which the results are being cached.
112+
method_name : str
113+
The name of the method being cached, used for logging.
114+
method_columns : List[str]
115+
The list of columns to check and cache.
116+
117+
Returns
118+
-------
119+
bool
120+
True if caching was successful, False if there were missing columns.
121+
"""
73122
if method_columns is None:
74123
return False
75124
if not hasattr(physics_method_params, "cached_results"):

disruption_py/core/physics_method/decorator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/usr/bin/env python3
22

3+
"""
4+
This module provides a decorator to signify methods that calculate physics quantities.
5+
"""
6+
37
from typing import Callable, List, Union
48

59
from disruption_py.core.physics_method.caching import cache_method

disruption_py/core/physics_method/metadata.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/usr/bin/env python3
22

3+
"""
4+
Module for defining metadata classes for physics methods.
5+
"""
6+
37
from dataclasses import dataclass, fields
48
from typing import Callable, List, Union
59

@@ -32,6 +36,15 @@ def __post_init__(self):
3236

3337
@dataclass(frozen=True)
3438
class BoundMethodMetadata(MethodMetadata):
39+
"""
40+
Metadata for a bound method, extending `MethodMetadata`.
41+
42+
Attributes
43+
----------
44+
bound_method : Callable
45+
The method that is bound to this metadata.
46+
"""
47+
3548
bound_method: Callable
3649

3750
@classmethod
@@ -42,11 +55,21 @@ def bind(
4255
physics_method_params: PhysicsMethodParams,
4356
):
4457
"""
45-
Evaluate arguments to decorators to usable values.
46-
47-
Some parameters provided to the physics_method decorators can take method
48-
that are evaluated at runtime. `resolve_for` evaluates all of these methods
49-
and returns a new instance of `MethodMetadata`without function parameters.
58+
Bind a method to its metadata and resolve any callable parameters.
59+
60+
Parameters
61+
----------
62+
method_metadata : MethodMetadata
63+
Metadata instance containing the method's unresolved parameters.
64+
bound_method : Callable
65+
The callable method to be bound.
66+
physics_method_params : PhysicsMethodParams
67+
Parameters required for resolving the method.
68+
69+
Returns
70+
-------
71+
BoundMethodMetadata
72+
A new instance of `BoundMethodMetadata` with resolved parameters.
5073
"""
5174
new_method_metadata_params = {}
5275
bind_to = (getattr(bound_method, "__self__", None),)
@@ -71,7 +94,8 @@ def bind(
7194

7295

7396
def is_parametered_method(method: Callable) -> bool:
74-
"""Returns whether the method is decorated with `physics_method` decorator
97+
"""
98+
Returns whether the method is decorated with `physics_method` decorator
7599
76100
Parameters
77101
----------
@@ -87,7 +111,8 @@ def is_parametered_method(method: Callable) -> bool:
87111

88112

89113
def get_method_metadata(method: Callable, should_throw: bool = False) -> MethodMetadata:
90-
"""Get method metadata for method
114+
"""
115+
Get method metadata for method
91116
92117
Parameters
93118
----------

disruption_py/core/physics_method/params.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/usr/bin/env python3
22

3+
"""
4+
Module for defining parameters used in physics methods for DisruptionPy.
5+
"""
6+
37
import logging
48
from dataclasses import dataclass, field
59
from typing import Any, Dict
@@ -13,8 +17,10 @@
1317

1418
@dataclass
1519
class PhysicsMethodParams:
16-
"""Holder for useful variables for the physics methods like an MDSplus connection
17-
and the timebase for the data."""
20+
"""
21+
Holder for useful variables for the physics methods like an MDSplus connection
22+
and the timebase for the data.
23+
"""
1824

1925
logger = logging.getLogger("disruption_py")
2026

@@ -31,10 +37,19 @@ class PhysicsMethodParams:
3137
cached_results: Dict[str, Any] = field(default_factory=dict)
3238

3339
@property
34-
def disrupted(self):
40+
def disrupted(self) -> bool:
41+
"""
42+
Check if the disruption time is set.
43+
44+
Returns
45+
-------
46+
bool
47+
True if disruption time is not None, False otherwise.
48+
"""
3549
return self.disruption_time is not None
3650

37-
def cleanup(self):
51+
def cleanup(self) -> None:
52+
"""Clean up resources used by the physics method parameters."""
3853
self.mds_conn.cleanup()
3954
self.times = None
4055
self.cached_results.clear()

0 commit comments

Comments
 (0)