humalab 0.0.5__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.

Files changed (42) hide show
  1. humalab/__init__.py +25 -0
  2. humalab/assets/__init__.py +8 -2
  3. humalab/assets/files/resource_file.py +96 -6
  4. humalab/assets/files/urdf_file.py +49 -11
  5. humalab/assets/resource_operator.py +139 -0
  6. humalab/constants.py +48 -5
  7. humalab/dists/__init__.py +7 -0
  8. humalab/dists/bernoulli.py +26 -1
  9. humalab/dists/categorical.py +25 -0
  10. humalab/dists/discrete.py +27 -2
  11. humalab/dists/distribution.py +11 -0
  12. humalab/dists/gaussian.py +27 -2
  13. humalab/dists/log_uniform.py +29 -3
  14. humalab/dists/truncated_gaussian.py +33 -4
  15. humalab/dists/uniform.py +24 -0
  16. humalab/episode.py +291 -11
  17. humalab/humalab.py +93 -38
  18. humalab/humalab_api_client.py +297 -95
  19. humalab/humalab_config.py +49 -0
  20. humalab/humalab_test.py +46 -17
  21. humalab/metrics/__init__.py +11 -5
  22. humalab/metrics/code.py +59 -0
  23. humalab/metrics/metric.py +69 -102
  24. humalab/metrics/scenario_stats.py +163 -0
  25. humalab/metrics/summary.py +45 -24
  26. humalab/run.py +224 -101
  27. humalab/scenarios/__init__.py +11 -0
  28. humalab/{scenario.py → scenarios/scenario.py} +130 -136
  29. humalab/scenarios/scenario_operator.py +114 -0
  30. humalab/{scenario_test.py → scenarios/scenario_test.py} +150 -269
  31. humalab/utils.py +37 -0
  32. {humalab-0.0.5.dist-info → humalab-0.0.7.dist-info}/METADATA +1 -1
  33. humalab-0.0.7.dist-info/RECORD +39 -0
  34. humalab/assets/resource_manager.py +0 -58
  35. humalab/evaluators/__init__.py +0 -16
  36. humalab/humalab_main.py +0 -119
  37. humalab/metrics/dist_metric.py +0 -22
  38. humalab-0.0.5.dist-info/RECORD +0 -37
  39. {humalab-0.0.5.dist-info → humalab-0.0.7.dist-info}/WHEEL +0 -0
  40. {humalab-0.0.5.dist-info → humalab-0.0.7.dist-info}/entry_points.txt +0 -0
  41. {humalab-0.0.5.dist-info → humalab-0.0.7.dist-info}/licenses/LICENSE +0 -0
  42. {humalab-0.0.5.dist-info → humalab-0.0.7.dist-info}/top_level.txt +0 -0
humalab/__init__.py CHANGED
@@ -1,9 +1,34 @@
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
16
+ from humalab import assets
17
+ from humalab import metrics
18
+ from humalab import scenarios
2
19
  from humalab.run import Run
20
+ from humalab.constants import MetricDimType, GraphType
21
+ # from humalab import evaluators
3
22
 
4
23
  __all__ = [
5
24
  "init",
6
25
  "finish",
7
26
  "login",
27
+ "assets",
28
+ "metrics",
29
+ "scenarios",
8
30
  "Run",
31
+ "MetricDimType",
32
+ "GraphType",
33
+ # "evaluators",
9
34
  ]
@@ -1,4 +1,10 @@
1
- from .resource_manager import ResourceManager
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
+
7
+ from .resource_operator import download, list_resources
2
8
  from .files import ResourceFile, URDFFile
3
9
 
4
- __all__ = ["ResourceManager", "ResourceFile", "URDFFile"]
10
+ __all__ = ["download", "list_resources", "ResourceFile", "URDFFile"]
@@ -1,41 +1,131 @@
1
1
  from datetime import datetime
2
+ from enum import Enum
3
+
4
+ from humalab.constants import DEFAULT_PROJECT
5
+
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
+ """
12
+ URDF = "urdf"
13
+ MJCF = "mjcf"
14
+ USD = "usd"
15
+ MESH = "mesh"
16
+ VIDEO = "video"
17
+ IMAGE = "image"
18
+ DATA = "data"
19
+
2
20
 
3
21
 
4
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
+ """
5
37
  def __init__(self,
6
38
  name: str,
7
39
  version: int,
8
40
  filename: str,
9
- resource_type: str,
41
+ resource_type: str | ResourceType,
42
+ project: str = DEFAULT_PROJECT,
10
43
  description: str | None = None,
11
44
  created_at: datetime | None = None):
45
+ self._project = project
12
46
  self._name = name
13
47
  self._version = version
14
48
  self._filename = filename
15
- self._resource_type = resource_type
49
+ self._resource_type = ResourceType(resource_type)
16
50
  self._description = description
17
51
  self._created_at = created_at
18
52
 
53
+ @property
54
+ def project(self) -> str:
55
+ """The project name this resource belongs to.
56
+
57
+ Returns:
58
+ str: The project name.
59
+ """
60
+ return self._project
61
+
19
62
  @property
20
63
  def name(self) -> str:
64
+ """The resource name.
65
+
66
+ Returns:
67
+ str: The resource name.
68
+ """
21
69
  return self._name
22
-
70
+
23
71
  @property
24
72
  def version(self) -> int:
73
+ """The version number of this resource.
74
+
75
+ Returns:
76
+ int: The version number.
77
+ """
25
78
  return self._version
26
-
79
+
27
80
  @property
28
81
  def filename(self) -> str:
82
+ """The local filesystem path to the resource file.
83
+
84
+ Returns:
85
+ str: The file path.
86
+ """
29
87
  return self._filename
30
-
88
+
31
89
  @property
32
- def resource_type(self) -> str:
90
+ def resource_type(self) -> ResourceType:
91
+ """The type of resource.
92
+
93
+ Returns:
94
+ ResourceType: The resource type.
95
+ """
33
96
  return self._resource_type
34
97
 
35
98
  @property
36
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
+ """
37
105
  return self._created_at
38
106
 
39
107
  @property
40
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
+ """
41
114
  return self._description
115
+
116
+ def __repr__(self) -> str:
117
+ """String representation of the resource file.
118
+
119
+ Returns:
120
+ str: String representation with all attributes.
121
+ """
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})"
123
+
124
+ def __str__(self) -> str:
125
+ """String representation of the resource file.
126
+
127
+ Returns:
128
+ str: Same as __repr__.
129
+ """
130
+ return self.__repr__()
131
+
@@ -1,45 +1,73 @@
1
- from datetime import datetime
2
1
  import os
3
2
  import glob
4
- from humalab.assets.files.resource_file import ResourceFile
3
+ from datetime import datetime
4
+
5
+ from humalab.assets.files.resource_file import ResourceFile, ResourceType
5
6
  from humalab.assets.archive import extract_archive
7
+ from humalab.constants import DEFAULT_PROJECT
6
8
 
7
9
 
8
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
+ """
9
21
  def __init__(self,
10
22
  name: str,
11
23
  version: int,
12
24
  filename: str,
25
+ project: str = DEFAULT_PROJECT,
13
26
  urdf_filename: str | None = None,
14
27
  description: str | None = None,
15
28
  created_at: datetime | None = None,):
16
- super().__init__(name=name,
29
+ super().__init__(project=project,
30
+ name=name,
17
31
  version=version,
18
32
  description=description,
19
33
  filename=filename,
20
- resource_type="URDF",
34
+ resource_type=ResourceType.URDF,
21
35
  created_at=created_at)
22
36
  self._urdf_base_filename = urdf_filename
23
37
  self._urdf_filename, self._root_path = self._extract()
24
38
  self._urdf_filename = os.path.join(self._urdf_filename, self._urdf_filename)
25
39
 
26
40
  def _extract(self):
27
- working_path = os.path.dirname(self._filename)
28
- if os.path.exists(self._filename):
29
- _, ext = os.path.splitext(self._filename)
41
+ """Extract the URDF archive and locate the main URDF file.
42
+
43
+ Returns:
44
+ tuple[str, str]: (urdf_filename, root_path)
45
+ """
46
+ working_path = os.path.dirname(self.filename)
47
+ if os.path.exists(self.filename):
48
+ _, ext = os.path.splitext(self.filename)
30
49
  ext = ext.lstrip('.') # Remove leading dot
31
50
  if ext.lower() != "urdf":
32
- extract_archive(self._filename, working_path)
51
+ extract_archive(self.filename, working_path)
33
52
  try:
34
- os.remove(self._filename)
53
+ os.remove(self.filename)
35
54
  except Exception as e:
36
- print(f"Error removing saved file {self._filename}: {e}")
55
+ print(f"Error removing saved file {self.filename}: {e}")
37
56
  local_filename = self.search_resource_file(self._urdf_base_filename, working_path)
38
57
  if local_filename is None:
39
58
  raise ValueError(f"Resource filename {self._urdf_base_filename} not found in {working_path}")
40
59
  return local_filename, working_path
41
60
 
42
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
+ """
43
71
  found_filename = None
44
72
  if resource_filename:
45
73
  search_path = os.path.join(working_path, "**")
@@ -58,8 +86,18 @@ class URDFFile(ResourceFile):
58
86
 
59
87
  @property
60
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
+ """
61
94
  return self._urdf_filename
62
-
95
+
63
96
  @property
64
97
  def root_path(self) -> str:
98
+ """Root directory containing the extracted URDF and assets.
99
+
100
+ Returns:
101
+ str: The root path.
102
+ """
65
103
  return self._root_path
@@ -0,0 +1,139 @@
1
+ from humalab.constants import DEFAULT_PROJECT
2
+ from humalab.assets.files.resource_file import ResourceFile, ResourceType
3
+ from humalab.humalab_config import HumalabConfig
4
+ from humalab.humalab_api_client import HumaLabApiClient
5
+ from humalab.assets.files.urdf_file import URDFFile
6
+ import os
7
+ from typing import Any, Optional
8
+
9
+
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
+ """
21
+ return os.path.join(humalab_config.workspace_path, "assets", name, f"{version}")
22
+
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
+ """
34
+ asset_dir = _asset_dir(humalab_config, name, version)
35
+ if not os.path.exists(asset_dir):
36
+ os.makedirs(asset_dir, exist_ok=True)
37
+ return True
38
+ return False
39
+
40
+ def download(name: str,
41
+ version: int | None=None,
42
+ project: str = DEFAULT_PROJECT,
43
+
44
+ host: str | None = None,
45
+ api_key: str | None = None,
46
+ timeout: float | None = None,
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
+ """
61
+ humalab_config = HumalabConfig()
62
+
63
+ api_client = HumaLabApiClient(base_url=host,
64
+ api_key=api_key,
65
+ timeout=timeout)
66
+
67
+ resource = api_client.get_resource(project_name=project, name=name, version=version)
68
+ filename = os.path.basename(resource['resource_url'])
69
+ filename = os.path.join(_asset_dir(humalab_config, name, resource["version"]), filename)
70
+ if _create_asset_dir(humalab_config, name, resource["version"]):
71
+ file_content = api_client.download_resource(project_name=project, name="lerobot")
72
+ with open(filename, "wb") as f:
73
+ f.write(file_content)
74
+
75
+ if resource["resource_type"].lower() == "urdf":
76
+ return URDFFile(project=project,
77
+ name=name,
78
+ version=resource["version"],
79
+ description=resource.get("description"),
80
+ filename=filename,
81
+ urdf_filename=resource.get("filename"),
82
+ created_at=resource.get("created_at"))
83
+
84
+ return ResourceFile(project=project,
85
+ name=name,
86
+ version=resource["version"],
87
+ filename=filename,
88
+ resource_type=resource["resource_type"],
89
+ description=resource.get("description"),
90
+ created_at=resource.get("created_at"))
91
+
92
+ def list_resources(project: str = DEFAULT_PROJECT,
93
+ resource_types: Optional[list[str | ResourceType]] = None,
94
+ limit: int = 20,
95
+ offset: int = 0,
96
+ latest_only: bool = True,
97
+
98
+ host: str | None = None,
99
+ api_key: str | None = None,
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
+ """
116
+ api_client = HumaLabApiClient(base_url=host,
117
+ api_key=api_key,
118
+ timeout=timeout)
119
+
120
+ resource_type_string = None
121
+ if resource_types:
122
+ resource_type_strings = {rt.value if isinstance(rt, ResourceType) else rt for rt in resource_types}
123
+ resource_type_string = ",".join(resource_type_strings)
124
+ resp = api_client.get_resources(project_name=project,
125
+ resource_types=resource_type_string,
126
+ limit=limit,
127
+ offset=offset,
128
+ latest_only=latest_only)
129
+ resources = resp.get("resources", [])
130
+ ret_list = []
131
+ for resource in resources:
132
+ ret_list.append(ResourceFile(name=resource["name"],
133
+ version=resource.get("version"),
134
+ project=project,
135
+ filename=resource.get("filename"),
136
+ resource_type=resource.get("resource_type"),
137
+ description=resource.get("description"),
138
+ created_at=resource.get("created_at")))
139
+ return ret_list
humalab/constants.py CHANGED
@@ -1,7 +1,50 @@
1
+ """Constants and enumerations used throughout the HumaLab SDK."""
2
+
1
3
  from enum import Enum
2
4
 
3
- class EpisodeStatus(Enum):
4
- PASS = "pass"
5
- FAILED = "failed"
6
- CANCELED = "canceled"
7
- ERROR = "error"
5
+
6
+ RESERVED_NAMES = {
7
+ "sceanario",
8
+ "seed",
9
+ }
10
+ """Set of reserved names that cannot be used for metric or artifact keys."""
11
+
12
+ DEFAULT_PROJECT = "default"
13
+ """Default project name used when no project is specified."""
14
+
15
+
16
+ class ArtifactType(Enum):
17
+ """Types of artifacts that can be stored"""
18
+ METRICS = "metrics" # Run & Episode
19
+ SCENARIO_STATS = "scenario_stats" # Run only
20
+ PYTHON = "python" # Run & Episode
21
+ CODE = "code" # Run & Episode (YAML)
22
+
23
+
24
+ class MetricType(Enum):
25
+ """Enumeration of metric types.
26
+
27
+ Maps to corresponding artifact types for metrics and scenario statistics.
28
+ """
29
+ METRICS = ArtifactType.METRICS.value
30
+ SCENARIO_STATS = ArtifactType.SCENARIO_STATS.value
31
+
32
+
33
+ class GraphType(Enum):
34
+ """Types of graphs supported by Humalab."""
35
+ NUMERIC = "numeric"
36
+ LINE = "line"
37
+ BAR = "bar"
38
+ SCATTER = "scatter"
39
+ HISTOGRAM = "histogram"
40
+ GAUSSIAN = "gaussian"
41
+ HEATMAP = "heatmap"
42
+ THREE_D_MAP = "3d_map"
43
+
44
+
45
+ class MetricDimType(Enum):
46
+ """Types of metric dimensions"""
47
+ ZERO_D = "0d"
48
+ ONE_D = "1d"
49
+ TWO_D = "2d"
50
+ THREE_D = "3d"
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
@@ -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)):
@@ -31,14 +45,25 @@ class Bernoulli(Distribution):
31
45
  return True
32
46
  if not isinstance(arg1, (int, float)):
33
47
  if isinstance(arg1, (list, np.ndarray)):
34
- if len(arg1) > dimensions:
48
+ if len(arg1) != dimensions:
35
49
  return False
50
+
36
51
  return True
37
52
 
38
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
+ """
39
59
  return self._generator.binomial(n=1, p=self._p, size=self._size)
40
60
 
41
61
  def __repr__(self) -> str:
62
+ """String representation of the Bernoulli distribution.
63
+
64
+ Returns:
65
+ str: String representation showing p and size.
66
+ """
42
67
  return f"Bernoulli(p={self._p}, size={self._size})"
43
68
 
44
69
  @staticmethod
@@ -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:
@@ -41,18 +56,28 @@ class Discrete(Distribution):
41
56
  return True
42
57
  if not isinstance(arg1, int):
43
58
  if isinstance(arg1, (list, np.ndarray)):
44
- if len(arg1) > dimensions:
59
+ if len(arg1) != dimensions:
45
60
  return False
46
61
  if not isinstance(arg2, int):
47
62
  if isinstance(arg2, (list, np.ndarray)):
48
- if len(arg2) > dimensions:
63
+ if len(arg2) != dimensions:
49
64
  return False
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
@@ -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