humalab 0.0.6__py3-none-any.whl → 0.0.7__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of humalab might be problematic. Click here for more details.
- humalab/__init__.py +14 -0
- humalab/assets/__init__.py +6 -0
- humalab/assets/files/resource_file.py +70 -6
- humalab/assets/files/urdf_file.py +35 -1
- humalab/assets/resource_operator.py +51 -3
- humalab/constants.py +10 -1
- humalab/dists/__init__.py +7 -0
- humalab/dists/bernoulli.py +24 -0
- humalab/dists/categorical.py +25 -0
- humalab/dists/discrete.py +25 -0
- humalab/dists/distribution.py +11 -0
- humalab/dists/gaussian.py +25 -0
- humalab/dists/log_uniform.py +27 -1
- humalab/dists/truncated_gaussian.py +29 -0
- humalab/dists/uniform.py +24 -0
- humalab/episode.py +118 -8
- humalab/humalab.py +53 -14
- humalab/humalab_api_client.py +3 -8
- humalab/humalab_config.py +49 -0
- humalab/metrics/__init__.py +6 -0
- humalab/metrics/code.py +37 -6
- humalab/metrics/metric.py +42 -8
- humalab/metrics/scenario_stats.py +83 -15
- humalab/metrics/summary.py +23 -8
- humalab/run.py +53 -7
- humalab/scenarios/__init__.py +7 -0
- humalab/scenarios/scenario.py +41 -38
- humalab/scenarios/scenario_operator.py +41 -9
- {humalab-0.0.6.dist-info → humalab-0.0.7.dist-info}/METADATA +1 -1
- humalab-0.0.7.dist-info/RECORD +39 -0
- humalab-0.0.6.dist-info/RECORD +0 -39
- {humalab-0.0.6.dist-info → humalab-0.0.7.dist-info}/WHEEL +0 -0
- {humalab-0.0.6.dist-info → humalab-0.0.7.dist-info}/entry_points.txt +0 -0
- {humalab-0.0.6.dist-info → humalab-0.0.7.dist-info}/licenses/LICENSE +0 -0
- {humalab-0.0.6.dist-info → humalab-0.0.7.dist-info}/top_level.txt +0 -0
humalab/__init__.py
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
"""HumaLab SDK - Python library for robotics and embodied AI experimentation.
|
|
2
|
+
|
|
3
|
+
The HumaLab SDK provides tools for managing scenarios, runs, episodes, and metrics
|
|
4
|
+
for robotics experiments and simulations. It supports probabilistic scenario generation,
|
|
5
|
+
metric tracking, and integration with the HumaLab platform.
|
|
6
|
+
|
|
7
|
+
Main components:
|
|
8
|
+
- init: Context manager for creating and managing runs
|
|
9
|
+
- Run: Represents a complete experimental run
|
|
10
|
+
- Episode: Represents a single episode within a run
|
|
11
|
+
- Scenario: Manages scenario configurations with distributions
|
|
12
|
+
- Metrics: Base class for tracking various metric types
|
|
13
|
+
"""
|
|
14
|
+
|
|
1
15
|
from humalab.humalab import init, finish, login
|
|
2
16
|
from humalab import assets
|
|
3
17
|
from humalab import metrics
|
humalab/assets/__init__.py
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
"""Asset management for resources like URDF files, meshes, and media.
|
|
2
|
+
|
|
3
|
+
This module provides functionality for downloading and listing versioned resources
|
|
4
|
+
from HumaLab, including URDF robot descriptions, meshes, videos, and other data files.
|
|
5
|
+
"""
|
|
6
|
+
|
|
1
7
|
from .resource_operator import download, list_resources
|
|
2
8
|
from .files import ResourceFile, URDFFile
|
|
3
9
|
|
|
@@ -4,6 +4,11 @@ from enum import Enum
|
|
|
4
4
|
from humalab.constants import DEFAULT_PROJECT
|
|
5
5
|
|
|
6
6
|
class ResourceType(Enum):
|
|
7
|
+
"""Enumeration of supported resource file types.
|
|
8
|
+
|
|
9
|
+
Supported types include URDF, MJCF, USD formats for robot descriptions,
|
|
10
|
+
MESH for 3D models, VIDEO and IMAGE for media files, and DATA for generic data.
|
|
11
|
+
"""
|
|
7
12
|
URDF = "urdf"
|
|
8
13
|
MJCF = "mjcf"
|
|
9
14
|
USD = "usd"
|
|
@@ -15,6 +20,20 @@ class ResourceType(Enum):
|
|
|
15
20
|
|
|
16
21
|
|
|
17
22
|
class ResourceFile:
|
|
23
|
+
"""Represents a resource file stored in HumaLab.
|
|
24
|
+
|
|
25
|
+
Resource files are versioned assets that can be downloaded and used in runs.
|
|
26
|
+
They include robot descriptions, meshes, media files, and other data.
|
|
27
|
+
|
|
28
|
+
Attributes:
|
|
29
|
+
project (str): The project name this resource belongs to.
|
|
30
|
+
name (str): The resource name.
|
|
31
|
+
version (int): The version number of this resource.
|
|
32
|
+
filename (str): The local filesystem path to the resource file.
|
|
33
|
+
resource_type (ResourceType): The type of resource.
|
|
34
|
+
created_at (datetime | None): When the resource was created.
|
|
35
|
+
description (str | None): Optional description of the resource.
|
|
36
|
+
"""
|
|
18
37
|
def __init__(self,
|
|
19
38
|
name: str,
|
|
20
39
|
version: int,
|
|
@@ -33,35 +52,80 @@ class ResourceFile:
|
|
|
33
52
|
|
|
34
53
|
@property
|
|
35
54
|
def project(self) -> str:
|
|
55
|
+
"""The project name this resource belongs to.
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
str: The project name.
|
|
59
|
+
"""
|
|
36
60
|
return self._project
|
|
37
|
-
|
|
61
|
+
|
|
38
62
|
@property
|
|
39
63
|
def name(self) -> str:
|
|
64
|
+
"""The resource name.
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
str: The resource name.
|
|
68
|
+
"""
|
|
40
69
|
return self._name
|
|
41
|
-
|
|
70
|
+
|
|
42
71
|
@property
|
|
43
72
|
def version(self) -> int:
|
|
73
|
+
"""The version number of this resource.
|
|
74
|
+
|
|
75
|
+
Returns:
|
|
76
|
+
int: The version number.
|
|
77
|
+
"""
|
|
44
78
|
return self._version
|
|
45
|
-
|
|
79
|
+
|
|
46
80
|
@property
|
|
47
81
|
def filename(self) -> str:
|
|
82
|
+
"""The local filesystem path to the resource file.
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
str: The file path.
|
|
86
|
+
"""
|
|
48
87
|
return self._filename
|
|
49
|
-
|
|
88
|
+
|
|
50
89
|
@property
|
|
51
90
|
def resource_type(self) -> ResourceType:
|
|
91
|
+
"""The type of resource.
|
|
92
|
+
|
|
93
|
+
Returns:
|
|
94
|
+
ResourceType: The resource type.
|
|
95
|
+
"""
|
|
52
96
|
return self._resource_type
|
|
53
97
|
|
|
54
98
|
@property
|
|
55
99
|
def created_at(self) -> datetime | None:
|
|
100
|
+
"""When the resource was created.
|
|
101
|
+
|
|
102
|
+
Returns:
|
|
103
|
+
datetime | None: The creation timestamp, or None if not available.
|
|
104
|
+
"""
|
|
56
105
|
return self._created_at
|
|
57
106
|
|
|
58
107
|
@property
|
|
59
108
|
def description(self) -> str | None:
|
|
109
|
+
"""Optional description of the resource.
|
|
110
|
+
|
|
111
|
+
Returns:
|
|
112
|
+
str | None: The description, or None if not provided.
|
|
113
|
+
"""
|
|
60
114
|
return self._description
|
|
61
|
-
|
|
115
|
+
|
|
62
116
|
def __repr__(self) -> str:
|
|
117
|
+
"""String representation of the resource file.
|
|
118
|
+
|
|
119
|
+
Returns:
|
|
120
|
+
str: String representation with all attributes.
|
|
121
|
+
"""
|
|
63
122
|
return f"ResourceFile(project={self._project}, name={self._name}, version={self._version}, filename={self._filename}, resource_type={self._resource_type}, description={self._description}, created_at={self._created_at})"
|
|
64
|
-
|
|
123
|
+
|
|
65
124
|
def __str__(self) -> str:
|
|
125
|
+
"""String representation of the resource file.
|
|
126
|
+
|
|
127
|
+
Returns:
|
|
128
|
+
str: Same as __repr__.
|
|
129
|
+
"""
|
|
66
130
|
return self.__repr__()
|
|
67
131
|
|
|
@@ -8,6 +8,16 @@ from humalab.constants import DEFAULT_PROJECT
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
class URDFFile(ResourceFile):
|
|
11
|
+
"""Represents a URDF (Unified Robot Description Format) file resource.
|
|
12
|
+
|
|
13
|
+
URDF files describe robot kinematics and geometry. This class handles
|
|
14
|
+
automatic extraction of compressed URDF archives and locates the main
|
|
15
|
+
URDF file within the extracted contents.
|
|
16
|
+
|
|
17
|
+
Attributes:
|
|
18
|
+
urdf_filename (str | None): Path to the main URDF file.
|
|
19
|
+
root_path (str): Root directory containing the extracted URDF and assets.
|
|
20
|
+
"""
|
|
11
21
|
def __init__(self,
|
|
12
22
|
name: str,
|
|
13
23
|
version: int,
|
|
@@ -28,6 +38,11 @@ class URDFFile(ResourceFile):
|
|
|
28
38
|
self._urdf_filename = os.path.join(self._urdf_filename, self._urdf_filename)
|
|
29
39
|
|
|
30
40
|
def _extract(self):
|
|
41
|
+
"""Extract the URDF archive and locate the main URDF file.
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
tuple[str, str]: (urdf_filename, root_path)
|
|
45
|
+
"""
|
|
31
46
|
working_path = os.path.dirname(self.filename)
|
|
32
47
|
if os.path.exists(self.filename):
|
|
33
48
|
_, ext = os.path.splitext(self.filename)
|
|
@@ -44,6 +59,15 @@ class URDFFile(ResourceFile):
|
|
|
44
59
|
return local_filename, working_path
|
|
45
60
|
|
|
46
61
|
def search_resource_file(self, resource_filename: str | None, working_path: str) -> str | None:
|
|
62
|
+
"""Search for a URDF file in the working directory.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
resource_filename (str | None): Optional specific filename to search for.
|
|
66
|
+
working_path (str): Directory to search within.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
str | None: Path to the found URDF file, or None if not found.
|
|
70
|
+
"""
|
|
47
71
|
found_filename = None
|
|
48
72
|
if resource_filename:
|
|
49
73
|
search_path = os.path.join(working_path, "**")
|
|
@@ -62,8 +86,18 @@ class URDFFile(ResourceFile):
|
|
|
62
86
|
|
|
63
87
|
@property
|
|
64
88
|
def urdf_filename(self) -> str | None:
|
|
89
|
+
"""Path to the main URDF file.
|
|
90
|
+
|
|
91
|
+
Returns:
|
|
92
|
+
str | None: The URDF file path.
|
|
93
|
+
"""
|
|
65
94
|
return self._urdf_filename
|
|
66
|
-
|
|
95
|
+
|
|
67
96
|
@property
|
|
68
97
|
def root_path(self) -> str:
|
|
98
|
+
"""Root directory containing the extracted URDF and assets.
|
|
99
|
+
|
|
100
|
+
Returns:
|
|
101
|
+
str: The root path.
|
|
102
|
+
"""
|
|
69
103
|
return self._root_path
|
|
@@ -8,23 +8,56 @@ from typing import Any, Optional
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
def _asset_dir(humalab_config: HumalabConfig, name: str, version: int) -> str:
|
|
11
|
+
"""Get the local directory path for a specific asset version.
|
|
12
|
+
|
|
13
|
+
Args:
|
|
14
|
+
humalab_config (HumalabConfig): Configuration containing workspace path.
|
|
15
|
+
name (str): Asset name.
|
|
16
|
+
version (int): Asset version.
|
|
17
|
+
|
|
18
|
+
Returns:
|
|
19
|
+
str: Path to the asset directory.
|
|
20
|
+
"""
|
|
11
21
|
return os.path.join(humalab_config.workspace_path, "assets", name, f"{version}")
|
|
12
22
|
|
|
13
23
|
def _create_asset_dir(humalab_config: HumalabConfig, name: str, version: int) -> bool:
|
|
24
|
+
"""Create the local directory for an asset if it doesn't exist.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
humalab_config (HumalabConfig): Configuration containing workspace path.
|
|
28
|
+
name (str): Asset name.
|
|
29
|
+
version (int): Asset version.
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
bool: True if directory was created, False if it already existed.
|
|
33
|
+
"""
|
|
14
34
|
asset_dir = _asset_dir(humalab_config, name, version)
|
|
15
35
|
if not os.path.exists(asset_dir):
|
|
16
36
|
os.makedirs(asset_dir, exist_ok=True)
|
|
17
37
|
return True
|
|
18
38
|
return False
|
|
19
39
|
|
|
20
|
-
def download(name: str,
|
|
40
|
+
def download(name: str,
|
|
21
41
|
version: int | None=None,
|
|
22
42
|
project: str = DEFAULT_PROJECT,
|
|
23
|
-
|
|
43
|
+
|
|
24
44
|
host: str | None = None,
|
|
25
45
|
api_key: str | None = None,
|
|
26
46
|
timeout: float | None = None,
|
|
27
47
|
) -> Any:
|
|
48
|
+
"""Download a resource from HumaLab.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
name (str): The resource name to download.
|
|
52
|
+
version (int | None): Optional specific version. If None, downloads latest.
|
|
53
|
+
project (str): The project name. Defaults to DEFAULT_PROJECT.
|
|
54
|
+
host (str | None): Optional API host override.
|
|
55
|
+
api_key (str | None): Optional API key override.
|
|
56
|
+
timeout (float | None): Optional timeout override.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
ResourceFile | URDFFile: The downloaded resource file object.
|
|
60
|
+
"""
|
|
28
61
|
humalab_config = HumalabConfig()
|
|
29
62
|
|
|
30
63
|
api_client = HumaLabApiClient(base_url=host,
|
|
@@ -61,10 +94,25 @@ def list_resources(project: str = DEFAULT_PROJECT,
|
|
|
61
94
|
limit: int = 20,
|
|
62
95
|
offset: int = 0,
|
|
63
96
|
latest_only: bool = True,
|
|
64
|
-
|
|
97
|
+
|
|
65
98
|
host: str | None = None,
|
|
66
99
|
api_key: str | None = None,
|
|
67
100
|
timeout: float | None = None,) -> list[ResourceFile]:
|
|
101
|
+
"""List available resources from HumaLab.
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
project (str): The project name. Defaults to DEFAULT_PROJECT.
|
|
105
|
+
resource_types (Optional[list[str | ResourceType]]): Filter by resource types.
|
|
106
|
+
limit (int): Maximum number of resources to return. Defaults to 20.
|
|
107
|
+
offset (int): Pagination offset. Defaults to 0.
|
|
108
|
+
latest_only (bool): Only return latest versions. Defaults to True.
|
|
109
|
+
host (str | None): Optional API host override.
|
|
110
|
+
api_key (str | None): Optional API key override.
|
|
111
|
+
timeout (float | None): Optional timeout override.
|
|
112
|
+
|
|
113
|
+
Returns:
|
|
114
|
+
list[ResourceFile]: List of resource file objects.
|
|
115
|
+
"""
|
|
68
116
|
api_client = HumaLabApiClient(base_url=host,
|
|
69
117
|
api_key=api_key,
|
|
70
118
|
timeout=timeout)
|
humalab/constants.py
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
+
"""Constants and enumerations used throughout the HumaLab SDK."""
|
|
2
|
+
|
|
1
3
|
from enum import Enum
|
|
2
4
|
|
|
3
5
|
|
|
4
6
|
RESERVED_NAMES = {
|
|
5
|
-
"sceanario"
|
|
7
|
+
"sceanario",
|
|
8
|
+
"seed",
|
|
6
9
|
}
|
|
10
|
+
"""Set of reserved names that cannot be used for metric or artifact keys."""
|
|
7
11
|
|
|
8
12
|
DEFAULT_PROJECT = "default"
|
|
13
|
+
"""Default project name used when no project is specified."""
|
|
9
14
|
|
|
10
15
|
|
|
11
16
|
class ArtifactType(Enum):
|
|
@@ -17,6 +22,10 @@ class ArtifactType(Enum):
|
|
|
17
22
|
|
|
18
23
|
|
|
19
24
|
class MetricType(Enum):
|
|
25
|
+
"""Enumeration of metric types.
|
|
26
|
+
|
|
27
|
+
Maps to corresponding artifact types for metrics and scenario statistics.
|
|
28
|
+
"""
|
|
20
29
|
METRICS = ArtifactType.METRICS.value
|
|
21
30
|
SCENARIO_STATS = ArtifactType.SCENARIO_STATS.value
|
|
22
31
|
|
humalab/dists/__init__.py
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
"""Probability distributions for scenario randomization.
|
|
2
|
+
|
|
3
|
+
This module provides various probability distribution classes used in scenario generation,
|
|
4
|
+
including uniform, gaussian, bernoulli, categorical, discrete, log-uniform, and truncated
|
|
5
|
+
gaussian distributions. Each supports 0D (scalar) and multi-dimensional (1D-3D) variants.
|
|
6
|
+
"""
|
|
7
|
+
|
|
1
8
|
from .bernoulli import Bernoulli
|
|
2
9
|
from .categorical import Categorical
|
|
3
10
|
from .discrete import Discrete
|
humalab/dists/bernoulli.py
CHANGED
|
@@ -4,6 +4,11 @@ from typing import Any
|
|
|
4
4
|
import numpy as np
|
|
5
5
|
|
|
6
6
|
class Bernoulli(Distribution):
|
|
7
|
+
"""Bernoulli distribution for binary outcomes.
|
|
8
|
+
|
|
9
|
+
Samples binary values (0 or 1) with a specified probability of success.
|
|
10
|
+
Supports scalar outputs as well as multi-dimensional arrays with 1D variants.
|
|
11
|
+
"""
|
|
7
12
|
def __init__(self,
|
|
8
13
|
generator: np.random.Generator,
|
|
9
14
|
p: float | Any,
|
|
@@ -22,6 +27,15 @@ class Bernoulli(Distribution):
|
|
|
22
27
|
|
|
23
28
|
@staticmethod
|
|
24
29
|
def validate(dimensions: int, *args) -> bool:
|
|
30
|
+
"""Validate distribution parameters for the given dimensions.
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
dimensions (int): The number of dimensions (0 for scalar, -1 for any).
|
|
34
|
+
*args: The distribution parameters (p).
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
bool: True if parameters are valid, False otherwise.
|
|
38
|
+
"""
|
|
25
39
|
arg1 = args[0]
|
|
26
40
|
if dimensions == 0:
|
|
27
41
|
if not isinstance(arg1, (int, float)):
|
|
@@ -37,9 +51,19 @@ class Bernoulli(Distribution):
|
|
|
37
51
|
return True
|
|
38
52
|
|
|
39
53
|
def _sample(self) -> int | float | np.ndarray:
|
|
54
|
+
"""Generate a sample from the Bernoulli distribution.
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
int | float | np.ndarray: Sampled binary value(s) (0 or 1).
|
|
58
|
+
"""
|
|
40
59
|
return self._generator.binomial(n=1, p=self._p, size=self._size)
|
|
41
60
|
|
|
42
61
|
def __repr__(self) -> str:
|
|
62
|
+
"""String representation of the Bernoulli distribution.
|
|
63
|
+
|
|
64
|
+
Returns:
|
|
65
|
+
str: String representation showing p and size.
|
|
66
|
+
"""
|
|
43
67
|
return f"Bernoulli(p={self._p}, size={self._size})"
|
|
44
68
|
|
|
45
69
|
@staticmethod
|
humalab/dists/categorical.py
CHANGED
|
@@ -3,6 +3,12 @@ from humalab.dists.distribution import Distribution
|
|
|
3
3
|
import numpy as np
|
|
4
4
|
|
|
5
5
|
class Categorical(Distribution):
|
|
6
|
+
"""Categorical distribution for discrete choices.
|
|
7
|
+
|
|
8
|
+
Samples from a list of choices with optional weights. If weights are not
|
|
9
|
+
provided, samples uniformly from all choices. Weights are automatically
|
|
10
|
+
normalized to sum to 1.
|
|
11
|
+
"""
|
|
6
12
|
def __init__(self,
|
|
7
13
|
generator: np.random.Generator,
|
|
8
14
|
choices: list,
|
|
@@ -27,12 +33,31 @@ class Categorical(Distribution):
|
|
|
27
33
|
|
|
28
34
|
@staticmethod
|
|
29
35
|
def validate(dimensions: int, *args) -> bool:
|
|
36
|
+
"""Validate distribution parameters for the given dimensions.
|
|
37
|
+
|
|
38
|
+
Args:
|
|
39
|
+
dimensions (int): The number of dimensions (0 for scalar, -1 for any).
|
|
40
|
+
*args: The distribution parameters (choices, weights).
|
|
41
|
+
|
|
42
|
+
Returns:
|
|
43
|
+
bool: Always returns True as categorical accepts any parameters.
|
|
44
|
+
"""
|
|
30
45
|
return True
|
|
31
46
|
|
|
32
47
|
def _sample(self) -> int | float | np.ndarray:
|
|
48
|
+
"""Generate a sample from the categorical distribution.
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
int | float | np.ndarray: Sampled choice(s) from the list.
|
|
52
|
+
"""
|
|
33
53
|
return self._generator.choice(self._choices, size=self._size, p=self._weights)
|
|
34
54
|
|
|
35
55
|
def __repr__(self) -> str:
|
|
56
|
+
"""String representation of the categorical distribution.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
str: String representation showing choices, size, and weights.
|
|
60
|
+
"""
|
|
36
61
|
return f"Categorical(choices={self._choices}, size={self._size}, weights={self._weights})"
|
|
37
62
|
|
|
38
63
|
@staticmethod
|
humalab/dists/discrete.py
CHANGED
|
@@ -4,6 +4,12 @@ from typing import Any
|
|
|
4
4
|
import numpy as np
|
|
5
5
|
|
|
6
6
|
class Discrete(Distribution):
|
|
7
|
+
"""Discrete uniform distribution over integers.
|
|
8
|
+
|
|
9
|
+
Samples integer values uniformly from a range [low, high). The endpoint
|
|
10
|
+
parameter controls whether the upper bound is inclusive or exclusive.
|
|
11
|
+
Supports scalar outputs as well as multi-dimensional arrays with 1D variants.
|
|
12
|
+
"""
|
|
7
13
|
def __init__(self,
|
|
8
14
|
generator: np.random.Generator,
|
|
9
15
|
low: int | Any,
|
|
@@ -29,6 +35,15 @@ class Discrete(Distribution):
|
|
|
29
35
|
|
|
30
36
|
@staticmethod
|
|
31
37
|
def validate(dimensions: int, *args) -> bool:
|
|
38
|
+
"""Validate distribution parameters for the given dimensions.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
dimensions (int): The number of dimensions (0 for scalar, -1 for any).
|
|
42
|
+
*args: The distribution parameters (low, high).
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
bool: True if parameters are valid, False otherwise.
|
|
46
|
+
"""
|
|
32
47
|
arg1 = args[0]
|
|
33
48
|
arg2 = args[1]
|
|
34
49
|
if dimensions == 0:
|
|
@@ -50,9 +65,19 @@ class Discrete(Distribution):
|
|
|
50
65
|
return True
|
|
51
66
|
|
|
52
67
|
def _sample(self) -> int | float | np.ndarray:
|
|
68
|
+
"""Generate a sample from the discrete distribution.
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
int | float | np.ndarray: Sampled integer value(s) from [low, high).
|
|
72
|
+
"""
|
|
53
73
|
return self._generator.integers(self._low, self._high, size=self._size, endpoint=self._endpoint)
|
|
54
74
|
|
|
55
75
|
def __repr__(self) -> str:
|
|
76
|
+
"""String representation of the discrete distribution.
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
str: String representation showing low, high, size, and endpoint.
|
|
80
|
+
"""
|
|
56
81
|
return f"Discrete(low={self._low}, high={self._high}, size={self._size}, endpoint={self._endpoint})"
|
|
57
82
|
|
|
58
83
|
@staticmethod
|
humalab/dists/distribution.py
CHANGED
|
@@ -3,6 +3,12 @@ from abc import ABC, abstractmethod
|
|
|
3
3
|
import numpy as np
|
|
4
4
|
|
|
5
5
|
class Distribution(ABC):
|
|
6
|
+
"""Abstract base class for probability distributions.
|
|
7
|
+
|
|
8
|
+
All distribution classes inherit from this base class and must implement
|
|
9
|
+
the _sample() method. Distributions maintain a random number generator
|
|
10
|
+
and track the last sampled value.
|
|
11
|
+
"""
|
|
6
12
|
def __init__(self,
|
|
7
13
|
generator: np.random.Generator) -> None:
|
|
8
14
|
"""
|
|
@@ -27,6 +33,11 @@ class Distribution(ABC):
|
|
|
27
33
|
|
|
28
34
|
@abstractmethod
|
|
29
35
|
def _sample(self) -> int | float | np.ndarray:
|
|
36
|
+
"""Generate a sample from the distribution.
|
|
37
|
+
|
|
38
|
+
Returns:
|
|
39
|
+
int | float | np.ndarray: The sampled value(s).
|
|
40
|
+
"""
|
|
30
41
|
pass
|
|
31
42
|
|
|
32
43
|
@property
|
humalab/dists/gaussian.py
CHANGED
|
@@ -4,6 +4,12 @@ import numpy as np
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
class Gaussian(Distribution):
|
|
7
|
+
"""Gaussian (normal) distribution.
|
|
8
|
+
|
|
9
|
+
Samples values from a normal distribution with specified mean (loc) and
|
|
10
|
+
standard deviation (scale). Supports scalar outputs as well as multi-dimensional
|
|
11
|
+
arrays with 1D, 2D, or 3D variants.
|
|
12
|
+
"""
|
|
7
13
|
def __init__(self,
|
|
8
14
|
generator: np.random.Generator,
|
|
9
15
|
loc: float | Any,
|
|
@@ -25,6 +31,15 @@ class Gaussian(Distribution):
|
|
|
25
31
|
|
|
26
32
|
@staticmethod
|
|
27
33
|
def validate(dimensions: int, *args) -> bool:
|
|
34
|
+
"""Validate distribution parameters for the given dimensions.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
dimensions (int): The number of dimensions (0 for scalar, -1 for any).
|
|
38
|
+
*args: The distribution parameters (loc, scale).
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
bool: True if parameters are valid, False otherwise.
|
|
42
|
+
"""
|
|
28
43
|
arg1 = args[0]
|
|
29
44
|
arg2 = args[1]
|
|
30
45
|
if dimensions == 0:
|
|
@@ -46,9 +61,19 @@ class Gaussian(Distribution):
|
|
|
46
61
|
return True
|
|
47
62
|
|
|
48
63
|
def _sample(self) -> int | float | np.ndarray:
|
|
64
|
+
"""Generate a sample from the Gaussian distribution.
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
int | float | np.ndarray: Sampled value(s) from N(loc, scale).
|
|
68
|
+
"""
|
|
49
69
|
return self._generator.normal(loc=self._loc, scale=self._scale, size=self._size)
|
|
50
70
|
|
|
51
71
|
def __repr__(self) -> str:
|
|
72
|
+
"""String representation of the Gaussian distribution.
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
str: String representation showing loc, scale, and size.
|
|
76
|
+
"""
|
|
52
77
|
return f"Gaussian(loc={self._loc}, scale={self._scale}, size={self._size})"
|
|
53
78
|
|
|
54
79
|
@staticmethod
|
humalab/dists/log_uniform.py
CHANGED
|
@@ -4,6 +4,13 @@ from typing import Any
|
|
|
4
4
|
import numpy as np
|
|
5
5
|
|
|
6
6
|
class LogUniform(Distribution):
|
|
7
|
+
"""Log-uniform distribution.
|
|
8
|
+
|
|
9
|
+
Samples values uniformly in log-space, useful for hyperparameters that
|
|
10
|
+
span multiple orders of magnitude (e.g., learning rates). The result is
|
|
11
|
+
exp(uniform(log(low), log(high))). Supports scalar outputs as well as
|
|
12
|
+
multi-dimensional arrays with 1D variants.
|
|
13
|
+
"""
|
|
7
14
|
def __init__(self,
|
|
8
15
|
generator: np.random.Generator,
|
|
9
16
|
low: float | Any,
|
|
@@ -25,6 +32,15 @@ class LogUniform(Distribution):
|
|
|
25
32
|
|
|
26
33
|
@staticmethod
|
|
27
34
|
def validate(dimensions: int, *args) -> bool:
|
|
35
|
+
"""Validate distribution parameters for the given dimensions.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
dimensions (int): The number of dimensions (0 for scalar, -1 for any).
|
|
39
|
+
*args: The distribution parameters (low, high).
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
bool: True if parameters are valid, False otherwise.
|
|
43
|
+
"""
|
|
28
44
|
arg1 = args[0]
|
|
29
45
|
arg2 = args[1]
|
|
30
46
|
if dimensions == 0:
|
|
@@ -46,9 +62,19 @@ class LogUniform(Distribution):
|
|
|
46
62
|
return True
|
|
47
63
|
|
|
48
64
|
def _sample(self) -> int | float | np.ndarray:
|
|
65
|
+
"""Generate a sample from the log-uniform distribution.
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
int | float | np.ndarray: Sampled value(s) in log-space.
|
|
69
|
+
"""
|
|
49
70
|
return np.exp(self._generator.uniform(self._log_low, self._log_high, size=self._size))
|
|
50
|
-
|
|
71
|
+
|
|
51
72
|
def __repr__(self) -> str:
|
|
73
|
+
"""String representation of the log-uniform distribution.
|
|
74
|
+
|
|
75
|
+
Returns:
|
|
76
|
+
str: String representation showing low, high, and size.
|
|
77
|
+
"""
|
|
52
78
|
return f"LogUniform(low={np.exp(self._log_low)}, high={np.exp(self._log_high)}, size={self._size})"
|
|
53
79
|
|
|
54
80
|
@staticmethod
|
|
@@ -4,6 +4,13 @@ import numpy as np
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
class TruncatedGaussian(Distribution):
|
|
7
|
+
"""Truncated Gaussian (normal) distribution.
|
|
8
|
+
|
|
9
|
+
Samples values from a normal distribution with specified mean (loc) and
|
|
10
|
+
standard deviation (scale), but constrained to lie within [low, high].
|
|
11
|
+
Values outside the bounds are resampled until they fall within range.
|
|
12
|
+
Supports scalar outputs as well as multi-dimensional arrays with 1D, 2D, or 3D variants.
|
|
13
|
+
"""
|
|
7
14
|
def __init__(self,
|
|
8
15
|
generator: np.random.Generator,
|
|
9
16
|
loc: float | Any,
|
|
@@ -31,6 +38,15 @@ class TruncatedGaussian(Distribution):
|
|
|
31
38
|
|
|
32
39
|
@staticmethod
|
|
33
40
|
def validate(dimensions: int, *args) -> bool:
|
|
41
|
+
"""Validate distribution parameters for the given dimensions.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
dimensions (int): The number of dimensions (0 for scalar, -1 for any).
|
|
45
|
+
*args: The distribution parameters (loc, scale, low, high).
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
bool: True if parameters are valid, False otherwise.
|
|
49
|
+
"""
|
|
34
50
|
arg1 = args[0]
|
|
35
51
|
arg2 = args[1]
|
|
36
52
|
arg3 = args[2]
|
|
@@ -66,6 +82,14 @@ class TruncatedGaussian(Distribution):
|
|
|
66
82
|
return True
|
|
67
83
|
|
|
68
84
|
def _sample(self) -> int | float | np.ndarray:
|
|
85
|
+
"""Generate a sample from the truncated Gaussian distribution.
|
|
86
|
+
|
|
87
|
+
Samples are generated from N(loc, scale) and resampled if they fall
|
|
88
|
+
outside [low, high].
|
|
89
|
+
|
|
90
|
+
Returns:
|
|
91
|
+
int | float | np.ndarray: Sampled value(s) within [low, high].
|
|
92
|
+
"""
|
|
69
93
|
samples = self._generator.normal(loc=self._loc, scale=self._scale, size=self._size)
|
|
70
94
|
mask = (samples < self._low) | (samples > self._high)
|
|
71
95
|
while np.any(mask):
|
|
@@ -74,6 +98,11 @@ class TruncatedGaussian(Distribution):
|
|
|
74
98
|
return samples
|
|
75
99
|
|
|
76
100
|
def __repr__(self) -> str:
|
|
101
|
+
"""String representation of the truncated Gaussian distribution.
|
|
102
|
+
|
|
103
|
+
Returns:
|
|
104
|
+
str: String representation showing loc, scale, low, high, and size.
|
|
105
|
+
"""
|
|
77
106
|
return f"TruncatedGaussian(loc={self._loc}, scale={self._scale}, low={self._low}, high={self._high}, size={self._size})"
|
|
78
107
|
|
|
79
108
|
@staticmethod
|