Skip to content

Commit 02067fa

Browse files
GiovanniCanalidario-coscia
authored andcommitted
fix problem doc
1 parent 3f630d8 commit 02067fa

File tree

10 files changed

+120
-102
lines changed

10 files changed

+120
-102
lines changed

pina/problem/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Module for Problems."""
1+
"""Module for the Problems."""
22

33
__all__ = [
44
"AbstractProblem",

pina/problem/abstract_problem.py

Lines changed: 72 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Module for AbstractProblem class"""
1+
"""Module for the AbstractProblem class"""
22

33
from abc import ABCMeta, abstractmethod
44
from copy import deepcopy
@@ -11,20 +11,16 @@
1111

1212
class AbstractProblem(metaclass=ABCMeta):
1313
"""
14-
The abstract `AbstractProblem` class. All the class defining a PINA Problem
15-
should be inherited from this class.
14+
Abstract base class for PINA problems. All specific problem types should
15+
inherit from this class.
1616
17-
In the definition of a PINA problem, the fundamental elements are:
18-
the output variables, the condition(s), and the domain(s) where the
19-
conditions are applied.
17+
A PINA problem is defined by key components, which typically include output
18+
variables, conditions, and domains over which the conditions are applied.
2019
"""
2120

2221
def __init__(self):
2322
"""
24-
TODO
25-
26-
:return: _description_
27-
:rtype: _type_
23+
Initialization of the :class:`AbstractProblem` class.
2824
"""
2925
self._discretised_domains = {}
3026
# create collector to manage problem data
@@ -48,31 +44,31 @@ def __init__(self):
4844
@property
4945
def batching_dimension(self):
5046
"""
51-
TODO
47+
Get batching dimension.
5248
53-
:return: _description_
54-
:rtype: _type_
49+
:return: The batching dimension.
50+
:rtype int
5551
"""
5652
return self._batching_dimension
5753

5854
@batching_dimension.setter
5955
def batching_dimension(self, value):
6056
"""
61-
TODO
57+
Set the batching dimension.
6258
63-
:return: _description_
64-
:rtype: _type_
59+
:param int value: The batching dimension.
6560
"""
6661
self._batching_dimension = value
6762

6863
# back compatibility 0.1
6964
@property
7065
def input_pts(self):
7166
"""
72-
TODO
67+
Return a dictionary mapping condition names to their corresponding
68+
input points.
7369
74-
:return: _description_
75-
:rtype: _type_
70+
:return: The input points of the problem.
71+
:rtype: dict
7672
"""
7773
to_return = {}
7874
for cond_name, cond in self.conditions.items():
@@ -85,21 +81,21 @@ def input_pts(self):
8581
@property
8682
def discretised_domains(self):
8783
"""
88-
TODO
84+
Return a dictionary mapping domains to their corresponding sampled
85+
points.
8986
90-
:return: _description_
91-
:rtype: _type_
87+
:return: The discretised domains.
88+
:rtype dict
9289
"""
9390
return self._discretised_domains
9491

9592
def __deepcopy__(self, memo):
9693
"""
97-
Implements deepcopy for the
98-
:class:`~pina.problem.abstract_problem.AbstractProblem` class.
94+
Perform a deep copy of the :class:`AbstractProblem` instance.
9995
100-
:param dict memo: Memory dictionary, to avoid excess copy
101-
:return: The deep copy of the
102-
:class:`~pina.problem.abstract_problem.AbstractProblem` class
96+
:param dict memo: A dictionary used to track objects already copied
97+
during the deep copy process to prevent redundant copies.
98+
:return: A deep copy of the :class:`AbstractProblem` instance.
10399
:rtype: AbstractProblem
104100
"""
105101
cls = self.__class__
@@ -114,7 +110,7 @@ def are_all_domains_discretised(self):
114110
"""
115111
Check if all the domains are discretised.
116112
117-
:return: True if all the domains are discretised, False otherwise
113+
:return: ``True`` if all domains are discretised, ``False`` otherwise.
118114
:rtype: bool
119115
"""
120116
return all(
@@ -124,12 +120,10 @@ def are_all_domains_discretised(self):
124120
@property
125121
def input_variables(self):
126122
"""
127-
The input variables of the AbstractProblem, whose type depends on the
128-
type of domain (spatial, temporal, and parameter).
129-
130-
:return: the input variables of self
131-
:rtype: list
123+
Get the input variables of the problem.
132124
125+
:return: The input variables of the problem.
126+
:rtype: list[str]
133127
"""
134128
variables = []
135129

@@ -144,51 +138,58 @@ def input_variables(self):
144138

145139
@input_variables.setter
146140
def input_variables(self, variables):
141+
"""
142+
Set the input variables of the AbstractProblem.
143+
144+
:param list[str] variables: The input variables of the problem.
145+
:raises RuntimeError: Not implemented.
146+
"""
147147
raise RuntimeError
148148

149149
@property
150150
@abstractmethod
151151
def output_variables(self):
152152
"""
153-
The output variables of the problem.
153+
Get the output variables of the problem.
154154
"""
155155

156156
@property
157157
@abstractmethod
158158
def conditions(self):
159159
"""
160-
The conditions of the problem.
160+
Get the conditions of the problem.
161+
162+
:return: The conditions of the problem.
163+
:rtype: dict
161164
"""
162165
return self.conditions
163166

164167
def discretise_domain(
165168
self, n=None, mode="random", domains="all", sample_rules=None
166169
):
167170
"""
168-
Generate a set of points to span the `Location` of all the conditions of
169-
the problem.
171+
Discretize the problem's domains by sampling a specified number of
172+
points according to the selected sampling mode.
170173
171-
:param n: Number of points to sample, see Note below
172-
for reference.
173-
:type n: int
174-
:param mode: Mode for sampling, defaults to ``random``.
174+
:param int n: The number of points to sample.
175+
:param mode: The sampling method. Default is ``random``.
175176
Available modes include: random sampling, ``random``;
176177
latin hypercube sampling, ``latin`` or ``lh``;
177178
chebyshev sampling, ``chebyshev``; grid sampling ``grid``.
178-
:param variables: variable(s) to sample, defaults to 'all'.
179-
:type variables: str | list[str]
180-
:param domains: Domain from where to sample, defaults to 'all'.
179+
:param domains: The domains from which to sample. Default is ``all``.
181180
:type domains: str | list[str]
181+
:param dict sample_rules: A dictionary of custom sampling rules.
182+
:raises RuntimeError: If both ``n`` and ``sample_rules`` are specified.
183+
:raises RuntimeError: If neither ``n`` nor ``sample_rules`` are set.
182184
183185
:Example:
184-
>>> pinn.discretise_domain(n=10, mode='grid')
185-
>>> pinn.discretise_domain(n=10, mode='grid', domain=['bound1'])
186-
>>> pinn.discretise_domain(n=10, mode='grid', variables=['x'])
186+
>>> problem.discretise_domain(n=10, mode='grid')
187+
>>> problem.discretise_domain(n=10, mode='grid', domains=['gamma1'])
187188
188189
.. warning::
189190
``random`` is currently the only implemented ``mode`` for all
190191
geometries, i.e. ``EllipsoidDomain``, ``CartesianDomain``,
191-
``SimplexDomain`` and the geometries compositions ``Union``,
192+
``SimplexDomain``, and geometry compositions ``Union``,
192193
``Difference``, ``Exclusion``, ``Intersection``. The
193194
modes ``latin`` or ``lh``, ``chebyshev``, ``grid`` are only
194195
implemented for ``CartesianDomain``.
@@ -218,12 +219,31 @@ def discretise_domain(
218219
raise RuntimeError("You have to specify either n or sample_rules.")
219220

220221
def _apply_default_discretization(self, n, mode, domains):
222+
"""
223+
Apply default discretization to the problem's domains.
224+
225+
:param int n: The number of points to sample.
226+
:param mode: The sampling method.
227+
:param domains: The domains from which to sample.
228+
:type domains: str | list[str]
229+
"""
221230
for domain in domains:
222231
self.discretised_domains[domain] = (
223232
self.domains[domain].sample(n, mode).sort_labels()
224233
)
225234

226235
def _apply_custom_discretization(self, sample_rules, domains):
236+
"""
237+
Apply custom discretization to the problem's domains.
238+
239+
:param dict sample_rules: A dictionary of custom sampling rules.
240+
:param domains: The domains from which to sample.
241+
:type domains: str | list[str]
242+
:raises RuntimeError: If the keys of the sample_rules dictionary are not
243+
the same as the input variables.
244+
:raises RuntimeError: If custom discretisation is applied on a domain
245+
that is not a CartesianDomain.
246+
"""
227247
if sorted(list(sample_rules.keys())) != sorted(self.input_variables):
228248
raise RuntimeError(
229249
"The keys of the sample_rules dictionary must be the same as "
@@ -247,10 +267,10 @@ def _apply_custom_discretization(self, sample_rules, domains):
247267

248268
def add_points(self, new_points_dict):
249269
"""
250-
Add input points to a sampled condition
251-
:param new_points_dict: Dictionary of input points (condition_name:
252-
LabelTensor)
253-
:raises RuntimeError: if at least one condition is not already sampled
270+
Add new points to an already sampled domain.
271+
272+
:param dict new_points_dict: The dictionary mapping new points to their
273+
corresponding domain.
254274
"""
255275
for k, v in new_points_dict.items():
256276
self.discretised_domains[k] = LabelTensor.vstack(

pina/problem/inverse_problem.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Module for the ParametricProblem class"""
1+
"""Module for the InverseProblem class"""
22

33
from abc import abstractmethod
44
import torch
@@ -7,19 +7,14 @@
77

88
class InverseProblem(AbstractProblem):
99
"""
10-
The class for the definition of inverse problems, i.e., problems
11-
with unknown parameters that have to be learned during the training process
12-
from given data.
13-
14-
Here's an example of a spatial inverse ODE problem, i.e., a spatial
15-
ODE problem with an unknown parameter `alpha` as coefficient of the
16-
derivative term.
17-
18-
:Example:
19-
TODO
10+
Class for defining inverse problems, where the objective is to determine
11+
unknown parameters through training, based on given data.
2012
"""
2113

2214
def __init__(self):
15+
"""
16+
Initialization of the :class:`InverseProblem` class.
17+
"""
2318
super().__init__()
2419
# storing unknown_parameters for optimization
2520
self.unknown_parameters = {}
@@ -33,23 +28,34 @@ def __init__(self):
3328
@abstractmethod
3429
def unknown_parameter_domain(self):
3530
"""
36-
The parameters' domain of the problem.
31+
The domain of the unknown parameters of the problem.
3732
"""
3833

3934
@property
4035
def unknown_variables(self):
4136
"""
42-
The parameters of the problem.
37+
Get the unknown variables of the problem.
38+
39+
:return: The unknown variables of the problem.
40+
:rtype: list[str]
4341
"""
4442
return self.unknown_parameter_domain.variables
4543

4644
@property
4745
def unknown_parameters(self):
4846
"""
49-
The parameters of the problem.
47+
Get the unknown parameters of the problem.
48+
49+
:return: The unknown parameters of the problem.
50+
:rtype: torch.nn.Parameter
5051
"""
5152
return self.__unknown_parameters
5253

5354
@unknown_parameters.setter
5455
def unknown_parameters(self, value):
56+
"""
57+
Set the unknown parameters of the problem.
58+
59+
:param torch.nn.Parameter value: The unknown parameters of the problem.
60+
"""
5561
self.__unknown_parameters = value

pina/problem/parametric_problem.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,23 @@
77

88
class ParametricProblem(AbstractProblem):
99
"""
10-
The class for the definition of parametric problems, i.e., problems
11-
with parameters among the input variables.
12-
13-
Here's an example of a spatial parametric ODE problem, i.e., a spatial
14-
ODE problem with an additional parameter `alpha` as coefficient of the
15-
derivative term.
16-
17-
:Example:
18-
TODO
10+
Class for defining parametric problems, where certain input variables are
11+
treated as parameters that can vary, allowing the model to adapt to
12+
different scenarios based on the chosen parameters.
1913
"""
2014

2115
@abstractmethod
2216
def parameter_domain(self):
2317
"""
24-
The parameters' domain of the problem.
18+
The domain of the parameters of the problem.
2519
"""
2620

2721
@property
2822
def parameters(self):
2923
"""
30-
The parameters' variables of the problem.
24+
Get the parameters of the problem.
25+
26+
:return: The parameters of the problem.
27+
:rtype: list[str]
3128
"""
3229
return self.parameter_domain.variables

pina/problem/spatial_problem.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@
77

88
class SpatialProblem(AbstractProblem):
99
"""
10-
The class for the definition of spatial problems, i.e., for problems
11-
with spatial input variables.
12-
13-
Here's an example of a spatial 1-dimensional ODE problem.
14-
15-
:Example:
16-
TODO
10+
Class for defining spatial problems, where the problem domain is defined in
11+
terms of spatial variables.
1712
"""
1813

1914
@abstractmethod
@@ -25,6 +20,9 @@ def spatial_domain(self):
2520
@property
2621
def spatial_variables(self):
2722
"""
28-
The spatial input variables of the problem.
23+
Get the spatial input variables of the problem.
24+
25+
:return: The spatial input variables of the problem.
26+
:rtype: list[str]
2927
"""
3028
return self.spatial_domain.variables

0 commit comments

Comments
 (0)