humalab 0.0.4__py3-none-any.whl → 0.0.5__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.

@@ -1,4 +1,4 @@
1
- from .resource import ResourceManager
1
+ from .resource_manager import ResourceManager
2
2
  from .files import ResourceFile, URDFFile
3
3
 
4
4
  __all__ = ["ResourceManager", "ResourceFile", "URDFFile"]
@@ -25,7 +25,7 @@ class URDFFile(ResourceFile):
25
25
 
26
26
  def _extract(self):
27
27
  working_path = os.path.dirname(self._filename)
28
- if not os.path.exists(working_path):
28
+ if os.path.exists(self._filename):
29
29
  _, ext = os.path.splitext(self._filename)
30
30
  ext = ext.lstrip('.') # Remove leading dot
31
31
  if ext.lower() != "urdf":
@@ -31,13 +31,14 @@ class ResourceManager:
31
31
  return False
32
32
 
33
33
  def download(self,
34
+ project: str,
34
35
  name: str,
35
36
  version: int | None=None) -> Any:
36
- resource = self._api_client.get_resource(name=name, version=version)
37
- file_content = self._api_client.download_resource(name="lerobot")
37
+ resource = self._api_client.get_resource(project_name=project, name=name, version=version)
38
38
  filename = os.path.basename(resource['resource_url'])
39
39
  filename = os.path.join(self._asset_dir(name, resource["version"]), filename)
40
40
  if self._create_asset_dir(name, resource["version"]):
41
+ file_content = self._api_client.download_resource(project_name=project, name="lerobot")
41
42
  with open(filename, "wb") as f:
42
43
  f.write(file_content)
43
44
 
@@ -20,6 +20,21 @@ class Bernoulli(Distribution):
20
20
  self._p = p
21
21
  self._size = size
22
22
 
23
+ @staticmethod
24
+ def validate(dimensions: int, *args) -> bool:
25
+ arg1 = args[0]
26
+ if dimensions == 0:
27
+ if not isinstance(arg1, (int, float)):
28
+ return False
29
+ return True
30
+ if dimensions == -1:
31
+ return True
32
+ if not isinstance(arg1, (int, float)):
33
+ if isinstance(arg1, (list, np.ndarray)):
34
+ if len(arg1) > dimensions:
35
+ return False
36
+ return True
37
+
23
38
  def _sample(self) -> int | float | np.ndarray:
24
39
  return self._generator.binomial(n=1, p=self._p, size=self._size)
25
40
 
@@ -25,6 +25,10 @@ class Categorical(Distribution):
25
25
  weights = [w / weight_sum for w in weights]
26
26
  self._weights = weights
27
27
 
28
+ @staticmethod
29
+ def validate(dimensions: int, *args) -> bool:
30
+ return True
31
+
28
32
  def _sample(self) -> int | float | np.ndarray:
29
33
  return self._generator.choice(self._choices, size=self._size, p=self._weights)
30
34
 
humalab/dists/discrete.py CHANGED
@@ -26,6 +26,28 @@ class Discrete(Distribution):
26
26
  self._high = np.array(high)
27
27
  self._size = size
28
28
  self._endpoint = endpoint if endpoint is not None else True
29
+
30
+ @staticmethod
31
+ def validate(dimensions: int, *args) -> bool:
32
+ arg1 = args[0]
33
+ arg2 = args[1]
34
+ if dimensions == 0:
35
+ if not isinstance(arg1, int):
36
+ return False
37
+ if not isinstance(arg2, int):
38
+ return False
39
+ return True
40
+ if dimensions == -1:
41
+ return True
42
+ if not isinstance(arg1, int):
43
+ if isinstance(arg1, (list, np.ndarray)):
44
+ if len(arg1) > dimensions:
45
+ return False
46
+ if not isinstance(arg2, int):
47
+ if isinstance(arg2, (list, np.ndarray)):
48
+ if len(arg2) > dimensions:
49
+ return False
50
+ return True
29
51
 
30
52
  def _sample(self) -> int | float | np.ndarray:
31
53
  return self._generator.integers(self._low, self._high, size=self._size, endpoint=self._endpoint)
humalab/dists/gaussian.py CHANGED
@@ -23,6 +23,28 @@ class Gaussian(Distribution):
23
23
  self._scale = scale
24
24
  self._size = size
25
25
 
26
+ @staticmethod
27
+ def validate(dimensions: int, *args) -> bool:
28
+ arg1 = args[0]
29
+ arg2 = args[1]
30
+ if dimensions == 0:
31
+ if not isinstance(arg1, (int, float)):
32
+ return False
33
+ if not isinstance(arg2, (int, float)):
34
+ return False
35
+ return True
36
+ if dimensions == -1:
37
+ return True
38
+ if not isinstance(arg1, (int, float)):
39
+ if isinstance(arg1, (list, np.ndarray)):
40
+ if len(arg1) > dimensions:
41
+ return False
42
+ if not isinstance(arg2, (int, float)):
43
+ if isinstance(arg2, (list, np.ndarray)):
44
+ if len(arg2) > dimensions:
45
+ return False
46
+ return True
47
+
26
48
  def _sample(self) -> int | float | np.ndarray:
27
49
  return self._generator.normal(loc=self._loc, scale=self._scale, size=self._size)
28
50
 
@@ -22,6 +22,28 @@ class LogUniform(Distribution):
22
22
  self._log_low = np.log(np.array(low))
23
23
  self._log_high = np.log(np.array(high))
24
24
  self._size = size
25
+
26
+ @staticmethod
27
+ def validate(dimensions: int, *args) -> bool:
28
+ arg1 = args[0]
29
+ arg2 = args[1]
30
+ if dimensions == 0:
31
+ if not isinstance(arg1, (int, float)):
32
+ return False
33
+ if not isinstance(arg2, (int, float)):
34
+ return False
35
+ return True
36
+ if dimensions == -1:
37
+ return True
38
+ if not isinstance(arg1, (int, float)):
39
+ if isinstance(arg1, (list, np.ndarray)):
40
+ if len(arg1) > dimensions:
41
+ return False
42
+ if not isinstance(arg2, (int, float)):
43
+ if isinstance(arg2, (list, np.ndarray)):
44
+ if len(arg2) > dimensions:
45
+ return False
46
+ return True
25
47
 
26
48
  def _sample(self) -> int | float | np.ndarray:
27
49
  return np.exp(self._generator.uniform(self._log_low, self._log_high, size=self._size))
@@ -29,6 +29,42 @@ class TruncatedGaussian(Distribution):
29
29
  self._high = high
30
30
  self._size = size
31
31
 
32
+ @staticmethod
33
+ def validate(dimensions: int, *args) -> bool:
34
+ arg1 = args[0]
35
+ arg2 = args[1]
36
+ arg3 = args[2]
37
+ arg4 = args[3]
38
+ if dimensions == 0:
39
+ if not isinstance(arg1, (int, float)):
40
+ return False
41
+ if not isinstance(arg2, (int, float)):
42
+ return False
43
+ if not isinstance(arg3, (int, float)):
44
+ return False
45
+ if not isinstance(arg4, (int, float)):
46
+ return False
47
+ return True
48
+ if dimensions == -1:
49
+ return True
50
+ if not isinstance(arg1, (int, float)):
51
+ if isinstance(arg1, (list, np.ndarray)):
52
+ if len(arg1) > dimensions:
53
+ return False
54
+ if not isinstance(arg2, (int, float)):
55
+ if isinstance(arg2, (list, np.ndarray)):
56
+ if len(arg2) > dimensions:
57
+ return False
58
+ if not isinstance(arg3, (int, float)):
59
+ if isinstance(arg3, (list, np.ndarray)):
60
+ if len(arg3) > dimensions:
61
+ return False
62
+ if not isinstance(arg4, (int, float)):
63
+ if isinstance(arg4, (list, np.ndarray)):
64
+ if len(arg4) > dimensions:
65
+ return False
66
+ return True
67
+
32
68
  def _sample(self) -> int | float | np.ndarray:
33
69
  samples = self._generator.normal(loc=self._loc, scale=self._scale, size=self._size)
34
70
  mask = (samples < self._low) | (samples > self._high)
humalab/dists/uniform.py CHANGED
@@ -23,6 +23,28 @@ class Uniform(Distribution):
23
23
  self._high = np.array(high)
24
24
  self._size = size
25
25
 
26
+ @staticmethod
27
+ def validate(dimensions: int, *args) -> bool:
28
+ arg1 = args[0]
29
+ arg2 = args[1]
30
+ if dimensions == 0:
31
+ if not isinstance(arg1, (int, float)):
32
+ return False
33
+ if not isinstance(arg2, (int, float)):
34
+ return False
35
+ return True
36
+ if dimensions == -1:
37
+ return True
38
+ if not isinstance(arg1, (int, float)):
39
+ if isinstance(arg1, (list, np.ndarray)):
40
+ if len(arg1) > dimensions:
41
+ return False
42
+ if not isinstance(arg2, (int, float)):
43
+ if isinstance(arg2, (list, np.ndarray)):
44
+ if len(arg2) > dimensions:
45
+ return False
46
+ return True
47
+
26
48
  def _sample(self) -> int | float | np.ndarray:
27
49
  return self._generator.uniform(self._low, self._high, size=self._size)
28
50
 
humalab/episode.py ADDED
@@ -0,0 +1,26 @@
1
+
2
+ from humalab.scenario import Scenario
3
+ from omegaconf import DictConfig, OmegaConf
4
+
5
+
6
+ class Episode:
7
+ def __init__(self, run_id: str, episode_id: str, scenario_conf: DictConfig):
8
+ self.run_id = run_id
9
+ self.episode_id = episode_id
10
+ self.scenario_conf = scenario_conf
11
+
12
+ @property
13
+ def scenario(self) -> DictConfig:
14
+ return self.scenario_conf
15
+
16
+ def finish(self):
17
+ print(f"Finishing episode {self.episode_id} for scenario {self.scenario.name}")
18
+
19
+ @property
20
+ def yaml(self) -> str:
21
+ """The current scenario configuration as a YAML string.
22
+
23
+ Returns:
24
+ str: The current scenario as a YAML string.
25
+ """
26
+ return OmegaConf.to_yaml(self.scenario_conf)
@@ -0,0 +1,16 @@
1
+ import random
2
+
3
+ def evaluate(video_path: str | list, task: str | list) -> dict | list[dict]:
4
+ """
5
+ Evaluate the video and return metrics.
6
+ """
7
+ # Placeholder implementation
8
+ if isinstance(video_path, list):
9
+ return [{"video_path": vp, "task": t,
10
+ "score": random.uniform(0, 1),
11
+ "confidence": random.uniform(0, 1)} for vp, t in zip(video_path, task)]
12
+ else:
13
+ return {"video_path": video_path, "task": task, "score": random.uniform(0, 1),
14
+ "confidence": random.uniform(0, 1)}
15
+
16
+ __all__ = ["evaluate"]
humalab/humalab.py CHANGED
@@ -1,15 +1,14 @@
1
1
  from contextlib import contextmanager
2
2
 
3
- from humalab.assets.files.resource_file import ResourceFile
4
- from humalab.assets.files.urdf_file import URDFFile
5
- from humalab.assets.resource_manager import ResourceManager
3
+ from omegaconf import OmegaConf
4
+
6
5
  from humalab.run import Run
7
6
  from humalab.humalab_config import HumalabConfig
8
7
  from humalab.humalab_api_client import HumaLabApiClient
9
8
  from humalab.constants import EpisodeStatus
9
+ import requests
10
10
 
11
11
  import uuid
12
- import os
13
12
 
14
13
  from collections.abc import Generator
15
14
 
@@ -18,16 +17,24 @@ from humalab.scenario import Scenario
18
17
  _cur_run: Run | None = None
19
18
 
20
19
  def _pull_scenario(client: HumaLabApiClient,
20
+ project_name: str,
21
21
  scenario: str | list | dict | None = None,
22
22
  scenario_id: str | None = None,) -> str | list | dict | None:
23
23
  if scenario_id is not None:
24
- scenario_response = client.get_scenario(uuid=scenario_id)
24
+ scenario_arr = scenario_id.split(":")
25
+ if len(scenario_arr) < 1:
26
+ raise ValueError("Invalid scenario_id format. Expected 'scenario_id' or 'scenario_name:version'.")
27
+ scenario_real_id = scenario_arr[0]
28
+ scenario_version = int(scenario_arr[1]) if len(scenario_arr) > 1 else None
29
+
30
+ scenario_response = client.get_scenario(
31
+ project_name=project_name,
32
+ uuid=scenario_real_id, version=scenario_version)
25
33
  return scenario_response["yaml_content"]
26
34
  return scenario
27
35
 
28
36
  @contextmanager
29
- def init(entity: str | None = None,
30
- project: str | None = None,
37
+ def init(project: str | None = None,
31
38
  name: str | None = None,
32
39
  description: str | None = None,
33
40
  id: str | None = None,
@@ -38,13 +45,31 @@ def init(entity: str | None = None,
38
45
  api_key: str | None = None,
39
46
  seed: int | None=None,
40
47
  timeout: float | None = None,
41
- num_env: int | None = None
48
+ # num_env: int | None = None,
49
+ auto_create_scenario: bool = False,
42
50
  ) -> Generator[Run, None, None]:
51
+ """
52
+ Initialize a new HumaLab run.
53
+
54
+ Args:
55
+ project: The project name under which to create the run.
56
+ name: The name of the run.
57
+ description: A description of the run.
58
+ id: The unique identifier for the run. If None, a new UUID will be generated.
59
+ tags: A list of tags to associate with the run.
60
+ scenario: The scenario configuration as a string, list, or dict.
61
+ scenario_id: The unique identifier of a pre-defined scenario to use.
62
+ base_url: The base URL of the HumaLab server.
63
+ api_key: The API key for authentication.
64
+ seed: An optional seed for scenario randomization.
65
+ timeout: The timeout for API requests.
66
+ # num_env: The number of parallel environments to run. (Not supported yet.)
67
+ auto_create_scenario: Whether to automatically create the scenario if it does not exist.
68
+ """
43
69
  global _cur_run
44
70
  run = None
45
71
  try:
46
72
  humalab_config = HumalabConfig()
47
- entity = entity or humalab_config.entity
48
73
  project = project or "default"
49
74
  name = name or ""
50
75
  description = description or ""
@@ -58,24 +83,62 @@ def init(entity: str | None = None,
58
83
  api_key=api_key,
59
84
  timeout=timeout)
60
85
  final_scenario = _pull_scenario(client=api_client,
86
+ project_name=project,
61
87
  scenario=scenario,
62
88
  scenario_id=scenario_id)
89
+
90
+ project_resp = api_client.create_project(name=project)
91
+
63
92
  scenario_inst = Scenario()
64
93
  scenario_inst.init(run_id=id,
65
94
  scenario=final_scenario,
66
95
  seed=seed,
67
96
  episode_id=str(uuid.uuid4()),
68
- num_env=num_env)
97
+ #num_env=num_env
98
+ )
99
+ if scenario_id is None and scenario is not None and auto_create_scenario:
100
+ scenario_response = api_client.create_scenario(
101
+ project_name=project_resp['name'],
102
+ name=f"{name} scenario",
103
+ description="Auto-created scenario",
104
+ yaml_content=OmegaConf.to_yaml(scenario_inst.template),
105
+ )
106
+ scenario_id = scenario_response['uuid']
107
+ try:
108
+ run_response = api_client.get_run(run_id=id)
109
+ api_client.update_run(
110
+ run_id=run_response['run_id'],
111
+ )
112
+
113
+ except requests.HTTPError as e:
114
+ if e.response.status_code == 404:
115
+ # If not found then create a new run,
116
+ # so ignore not found error.
117
+ run_response = None
118
+ else:
119
+ # Otherwise re-raise the exception.
120
+ raise
121
+
122
+ if run_response is None:
123
+ run_response = api_client.create_run(name=name,
124
+ project_name=project_resp['name'],
125
+ description=description,
126
+ tags=tags)
127
+ id = run_response['run_id']
128
+ api_client.update_run(
129
+ run_id=id,
130
+ description=description,
131
+ )
69
132
 
70
133
  run = Run(
71
- entity=entity,
72
- project=project,
73
- name=name,
74
- description=description,
75
- id=id,
76
- tags=tags,
134
+ project=project_resp['name'],
135
+ name=run_response["name"],
136
+ description=run_response.get("description"),
137
+ id=run_response['run_id'],
138
+ tags=run_response.get("tags"),
77
139
  scenario=scenario_inst,
78
140
  )
141
+
79
142
  _cur_run = run
80
143
  yield run
81
144
  finally:
@@ -94,124 +157,8 @@ def login(api_key: str | None = None,
94
157
  host: str | None = None,
95
158
  force: bool | None = None,
96
159
  timeout: float | None = None) -> bool:
97
- # TODO: Validate api_key against host given.
98
- # and retrieve entity information.
99
160
  humalab_config = HumalabConfig()
100
161
  humalab_config.api_key = api_key or humalab_config.api_key
101
162
  humalab_config.base_url = host or humalab_config.base_url
102
163
  humalab_config.timeout = timeout or humalab_config.timeout
103
164
  return True
104
-
105
-
106
- if __name__ == "__main__":
107
- login(api_key="GSdIImnRJs1TQRpkN74PyIVHhX8_PISLOI9NVF6uO94",
108
- host="http://localhost:8000")
109
-
110
- with init(entity="default",
111
- project="test",
112
- name="my first run",
113
- description="testing the humalab sdk",
114
- tags=["tag1", "tag2"],
115
- scenario_id="cb9668c6-99fe-490c-a97c-e8c1f06b54a6",
116
- num_env=None) as run:
117
- print(f"Run ID: {run.id}")
118
- print(f"Run Name: {run.name}")
119
- print(f"Run Description: {run.description}")
120
- print(f"Run Tags: {run.tags}")
121
- print(f"Run Scenario YAML:\n{run.scenario.yaml}")
122
-
123
- scenario = run.scenario
124
- # Simulate some operations
125
- print("CUP position: ", scenario.scenario.cup.position)
126
- print("CUP orientation: ", scenario.scenario.cup.orientation)
127
- print("Asset: ", scenario.scenario.cup.asset)
128
- print("Friction: ", scenario.scenario.cup.friction)
129
- print("Num Objects: ", scenario.scenario.num_objects)
130
- scenario.reset()
131
- print("======================SCENARIO RESET==========================")
132
- print("CUP position: ", scenario.scenario.cup.position)
133
- print("CUP orientation: ", scenario.scenario.cup.orientation)
134
- print("Asset: ", scenario.scenario.cup.asset)
135
- print("Friction: ", scenario.scenario.cup.friction)
136
- print("Num Objects: ", scenario.scenario.num_objects)
137
-
138
- scenario_string = """
139
- scenario:
140
- cup:
141
- position: "${uniform: [0.7, 0.7, 0.7],
142
- [1.5, 1.3, 0.7], [2, 3]}"
143
- orientation: "${uniform: 0.3, 0.7}"
144
- asset: "${categorical: ['lerobot', 'apple2', 'apple3'], [0.1, 0.3, 0.5]}"
145
- friction: "${gaussian: 0.3, 0.05}"
146
- hello: 13
147
- jkjk: test
148
- num_objects: "${discrete: 5, 10}"
149
- dfdjkjk: "hello"
150
- """
151
- with init(entity="default",
152
- project="test",
153
- name="my first run",
154
- description="testing the humalab sdk",
155
- tags=["tag1", "tag2"],
156
- scenario=scenario_string,
157
- num_env=None) as run:
158
- print(f"Run ID: {run.id}")
159
- print(f"Run Name: {run.name}")
160
- print(f"Run Description: {run.description}")
161
- print(f"Run Tags: {run.tags}")
162
- print(f"Run Scenario YAML:\n{run.scenario.yaml}")
163
-
164
- scenario = run.scenario
165
- # Simulate some operations
166
- print("CUP position: ", scenario.scenario.cup.position)
167
- print("CUP orientation: ", scenario.scenario.cup.orientation)
168
- print("Asset: ", scenario.scenario.cup.asset)
169
- print("Friction: ", scenario.scenario.cup.friction)
170
- print("Num Objects: ", scenario.scenario.num_objects)
171
- scenario.reset()
172
- print("======================SCENARIO RESET==========================")
173
- print("CUP position: ", scenario.scenario.cup.position)
174
- print("CUP orientation: ", scenario.scenario.cup.orientation)
175
- print("Asset: ", scenario.scenario.cup.asset)
176
- print("Friction: ", scenario.scenario.cup.friction)
177
- print("Num Objects: ", scenario.scenario.num_objects)
178
-
179
- resource = ResourceManager()
180
- urdf_file: URDFFile = resource.download(name="lerobot", version=1)
181
- print("URDF File: ", urdf_file.filename)
182
- print("URDF Description: ", urdf_file.description)
183
- print("URDF Created At: ", urdf_file.created_at)
184
- print("URDF Root Path: ", urdf_file._root_path)
185
- print("URDF Root Path: ", urdf_file._urdf_filename)
186
-
187
- urdf_file: URDFFile = resource.download(name="lerobot")
188
- print("URDF File: ", urdf_file.filename)
189
- print("URDF Description: ", urdf_file.description)
190
- print("URDF Created At: ", urdf_file.created_at)
191
- print("URDF Root Path: ", urdf_file._root_path)
192
- print("URDF Root Path: ", urdf_file._urdf_filename)
193
-
194
- atlas_file: ResourceFile = resource.download(name="atlas_description_test")
195
- print("Atlas File: ", atlas_file.filename)
196
- print("Atlas Description: ", atlas_file.description)
197
- print("Atlas Created At: ", atlas_file.created_at)
198
-
199
-
200
- """
201
- humalab_config = HumalabConfig()
202
- base_url = humalab_config.base_url
203
- api_key = humalab_config.api_key
204
- timeout = humalab_config.timeout
205
-
206
- api_client = HumaLabApiClient(base_url=base_url,
207
- api_key=api_key,
208
- timeout=timeout)
209
- resource = api_client.get_resource(name="lerobot", version=1)
210
- print("Resource metadata: ", resource)
211
- file_content = api_client.download_resource(name="lerobot")
212
- filename = os.path.basename(resource['resource_url'])
213
- filename = os.path.join(humalab_config.workspace_path, filename)
214
- with open(filename, "wb") as f:
215
- f.write(file_content)
216
- print(f"Resource file downloaded: {filename}")
217
- """