ocean-runner 0.2.13__py3-none-any.whl → 0.2.15__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.
ocean_runner/config.py CHANGED
@@ -2,7 +2,7 @@ import os
2
2
  from dataclasses import asdict, dataclass, field
3
3
  from logging import Logger
4
4
  from pathlib import Path
5
- from typing import Callable, Iterable, TypeVar
5
+ from typing import Iterable, TypeVar
6
6
 
7
7
  T = TypeVar("T")
8
8
 
@@ -43,9 +43,6 @@ class Config:
43
43
  custom_input: T | None = None
44
44
  """Algorithm's custom input types, must be a dataclass_json"""
45
45
 
46
- error_callback: Callable[[Exception], None] = None
47
- """Callback to execute upon exceptions"""
48
-
49
46
  logger: Logger | None = None
50
47
  """Logger to use in the algorithm"""
51
48
 
@@ -54,7 +51,5 @@ class Config:
54
51
  )
55
52
  """Paths that should be included so the code executes correctly"""
56
53
 
57
- environment: Environment = field(
58
- default_factory=lambda: Environment(),
59
- )
54
+ environment: Environment = field(default_factory=lambda: Environment())
60
55
  """Mock of environment data"""
ocean_runner/runner.py CHANGED
@@ -1,10 +1,9 @@
1
1
  from __future__ import annotations
2
2
 
3
- import inspect
4
3
  from dataclasses import InitVar, asdict, dataclass, field
5
4
  from logging import Logger
6
5
  from pathlib import Path
7
- from typing import Callable, Generic, Self, TypeVar
6
+ from typing import Callable, Generic, TypeVar
8
7
 
9
8
  from oceanprotocol_job_details import JobDetails
10
9
 
@@ -14,7 +13,7 @@ JobDetailsT = TypeVar("JobDetailsT")
14
13
  ResultT = TypeVar("ResultT")
15
14
 
16
15
 
17
- def default_error_callback(_: Algorithm, e: Exception) -> None:
16
+ def default_error_callback(_, e: Exception) -> None:
18
17
  raise e
19
18
 
20
19
 
@@ -32,28 +31,31 @@ def default_save(*, result: ResultT, base: Path, algorithm: Algorithm) -> None:
32
31
 
33
32
  @dataclass
34
33
  class Algorithm(Generic[JobDetailsT, ResultT]):
34
+ """
35
+ A configurable algorithm runner that behaves like a FastAPI app:
36
+ - You register `validate`, `run`, and `save_results` via decorators.
37
+ - You execute the full pipeline by calling `app()`.
38
+ """
35
39
 
36
40
  config: InitVar[Config | None] = None
37
41
  logger: Logger = field(init=False)
38
42
  _job_details: JobDetails[JobDetailsT] = field(init=False)
39
43
  _result: ResultT | None = field(default=None, init=False)
40
- error_callback = default_error_callback
44
+
45
+ # Decorator-registered callbacks
46
+ _validate_fn: Callable[[Algorithm], None] | None = field(default=None, init=False)
47
+ _run_fn: Callable[[Algorithm], ResultT] | None = field(default=None, init=False)
48
+ _save_fn: Callable[[ResultT, Path, Algorithm], None] | None = field(
49
+ default=None, init=False
50
+ )
51
+ _error_callback: Callable[[Algorithm, Exception], None] = field(
52
+ default=default_error_callback, init=False
53
+ )
41
54
 
42
55
  def __post_init__(self, config: Config | None) -> None:
43
56
  config: Config = config or Config()
44
- if config.error_callback:
45
- self.error_callback = config.error_callback
46
-
47
- self._setup_logger(config)
48
- self._load_job_details(config)
49
- self._load_user_defined_functions()
50
- self._maybe_autorun(config)
51
-
52
- # ---------------------
53
- # Setup helpers
54
- # ---------------------
55
57
 
56
- def _setup_logger(self, config: Config) -> None:
58
+ # Configure logger
57
59
  if config.logger:
58
60
  self.logger = config.logger
59
61
  else:
@@ -66,15 +68,18 @@ class Algorithm(Generic[JobDetailsT, ResultT]):
66
68
  )
67
69
  self.logger = logging.getLogger("ocean_runner")
68
70
 
69
- def _load_job_details(self, config: Config) -> None:
71
+ # Normalize base_dir
70
72
  if isinstance(config.environment.base_dir, str):
71
73
  config.environment.base_dir = Path(config.environment.base_dir)
74
+
75
+ # Extend sys.path for custom imports
72
76
  if config.source_paths:
73
77
  import sys
74
78
 
75
79
  sys.path.extend([str(path.absolute()) for path in config.source_paths])
76
80
  self.logger.debug(f"Added [{len(config.source_paths)}] entries to PATH")
77
81
 
82
+ # Load job details
78
83
  self._job_details = JobDetails.load(
79
84
  _type=config.custom_input,
80
85
  base_dir=config.environment.base_dir,
@@ -82,49 +87,13 @@ class Algorithm(Generic[JobDetailsT, ResultT]):
82
87
  transformation_did=config.environment.transformation_did,
83
88
  secret=config.environment.secret,
84
89
  )
90
+
85
91
  self.logger.info("Loaded JobDetails")
86
92
  self.logger.debug(asdict(self.job_details))
87
93
 
88
- # ---------------------
89
- # Auto-detect functions
90
- # ---------------------
91
-
92
- def _load_user_defined_functions(self) -> None:
93
- caller = inspect.getmodule(inspect.stack()[2][0])
94
- if not caller:
95
- return
96
-
97
- for name, default in {
98
- "validation": default_validation,
99
- "run": None,
100
- "save": default_save,
101
- }.items():
102
- fn = getattr(caller, name, None)
103
- if callable(fn):
104
- self.logger.debug(f"Found user-defined '{name}' function")
105
- setattr(self, f"_user_{name}", fn)
106
- elif default:
107
- setattr(self, f"_user_{name}", default)
108
-
109
- self._caller_module = caller
110
-
111
- # ---------------------
112
- # Auto-run logic
113
- # ---------------------
114
-
115
- def _maybe_autorun(self, config) -> None:
116
- """Automatically runs if caller has __ocean_runner_autorun__ = True"""
117
- if (
118
- getattr(self, "_caller_module", None)
119
- and getattr(self._caller_module, "__ocean_runner_autorun__", False)
120
- and not getattr(config, "_from_test", False)
121
- ):
122
- self.logger.info("Auto-running algorithm...")
123
- self.validate().run().save_results()
124
-
125
- # ---------------------
126
- # Main API
127
- # ---------------------
94
+ self.config = config
95
+
96
+ class Error(RuntimeError): ...
128
97
 
129
98
  @property
130
99
  def job_details(self) -> JobDetails:
@@ -138,41 +107,66 @@ class Algorithm(Generic[JobDetailsT, ResultT]):
138
107
  raise Algorithm.Error("Result missing, run the algorithm first")
139
108
  return self._result
140
109
 
141
- def validate(self, callback: Callable[[Self], None] | None = None) -> Self:
142
- callback = callback or getattr(self, "_user_validation", default_validation)
143
- self.logger.info("Validating instance...")
144
- try:
145
- callback(self)
146
- except Exception as e:
147
- self.error_callback(e)
148
- return self
149
-
150
- def run(self, callback: Callable[[Self], ResultT] | None = None) -> Self:
151
- callback = callback or getattr(self, "_user_run", None)
152
- if callback is None:
153
- raise Algorithm.Error("No 'run' function found")
154
- self.logger.info("Running algorithm...")
155
- try:
156
- self._result = callback(self)
157
- except Exception as e:
158
- self.error_callback(e)
159
- return self
160
-
161
- def save_results(
162
- self,
163
- callback: Callable[[ResultT, Path, Algorithm], None] | None = None,
164
- *,
165
- override_path: Path | None = None,
166
- ) -> None:
167
- callback = callback or getattr(self, "_user_save", default_save)
168
- self.logger.info("Saving results...")
110
+ # ---------------------------
111
+ # Decorators (FastAPI-style)
112
+ # ---------------------------
113
+
114
+ def validate(self, fn: Callable[[], None]) -> Callable[[], None]:
115
+ self._validate_fn = fn
116
+ return fn
117
+
118
+ def run(self, fn: Callable[[], ResultT]) -> Callable[[], ResultT]:
119
+ self._run_fn = fn
120
+ return fn
121
+
122
+ def save_results(self, fn: Callable[[ResultT, Path], None]) -> Callable:
123
+ self._save_fn = fn
124
+ return fn
125
+
126
+ def on_error(self, fn: Callable[[Exception], None]) -> Callable:
127
+ self._error_callback = fn
128
+ return fn
129
+
130
+ # ---------------------------
131
+ # Execution Pipeline
132
+ # ---------------------------
133
+
134
+ def __call__(self) -> ResultT | None:
135
+ """Executes the algorithm pipeline: validate → run → save_results."""
169
136
  try:
170
- callback(
171
- result=self.result,
172
- base=override_path or self.job_details.paths.outputs,
173
- algorithm=self,
174
- )
137
+ # Validation step
138
+ if self._validate_fn:
139
+ self.logger.info("Running custom validation...")
140
+ self._validate_fn()
141
+ else:
142
+ self.logger.info("Running default validation...")
143
+ self.default_validation(self)
144
+
145
+ # Run step
146
+ if self._run_fn:
147
+ self.logger.info("Running algorithm...")
148
+ self._result = self._run_fn()
149
+ else:
150
+ self.logger.warning("No run() function defined. Skipping execution.")
151
+ self._result = None
152
+
153
+ # Save step
154
+ if self._save_fn:
155
+ self.logger.info("Saving results...")
156
+ self._save_fn(
157
+ self._result,
158
+ self.job_details.paths.outputs,
159
+ )
160
+ else:
161
+ self.logger.info("No save_results() defined. Using default.")
162
+ default_save(
163
+ result=self._result,
164
+ base=self.job_details.paths.outputs,
165
+ algorithm=self,
166
+ )
167
+
175
168
  except Exception as e:
176
- self.error_callback(e)
169
+ self.logger.exception("Error during algorithm execution")
170
+ self._error_callback(e)
177
171
 
178
- class Error(RuntimeError): ...
172
+ return self._result
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ocean-runner
3
- Version: 0.2.13
3
+ Version: 0.2.15
4
4
  Summary: A fluent API for OceanProtocol algorithms
5
5
  Project-URL: Homepage, https://github.com/AgrospAI/ocean-runner
6
6
  Project-URL: Issues, https://github.com/AgrospAI/ocean-runner/issues
@@ -23,7 +23,7 @@ Description-Content-Type: text/markdown
23
23
 
24
24
  # ocean-runner
25
25
 
26
- Ocean Runner is a package that brings a fluent API for APP creation and running in the scope of OceanProtocol.
26
+ Ocean Runner is a package that eases algorithm creation in the scope of OceanProtocol.
27
27
 
28
28
 
29
29
  ## Installation
@@ -40,33 +40,40 @@ uv add ocean-runner
40
40
 
41
41
  ```python
42
42
  import random
43
- from ocean_runner import Algorithm, Config
43
+ from ocean_runner import Algorithm
44
+
45
+ algorithm = Algorithm()
46
+
44
47
 
48
+ @algorithm.run
49
+ def run():
50
+ return random.randint()
45
51
 
46
- Algorithm().run(lambda _: random.randint()).save_results()
52
+
53
+ if __name__ == "__main__":
54
+ algorithm()
47
55
  ```
48
56
 
49
- To use minimally the API, you can just provide a callback to the run method, defaulting for the rest of behaviours. This code snippet will:
57
+ This code snippet will:
50
58
 
51
- - Read the OceanProtocol JobDetails from the environment variables and use default file paths.
52
- - Generate a random integer.
53
- - Store the result in a "result.txt" file within the default outputs path.
59
+ - Read the OceanProtocol JobDetails from the environment variables and use default configuration file paths.
60
+ - Execute the run function.
61
+ - Execute the default saving function, storing the result in a "result.txt" file within the default outputs path.
54
62
 
55
63
  ### Tuning
56
64
 
57
65
  #### Application Config
58
66
 
59
- The application configuration can be tweaked by passing a Config instance to its' constructor.
67
+ The application configuration can be tweaked by passing a Config instance to its constructor.
60
68
 
61
69
  ```python
62
- Algorithm(
70
+ from ocean_runner import Algorithm, Config
71
+
72
+ algorithm = Algorithm(
63
73
  Config(
64
74
  custom_input: ... # dataclass
65
75
  # Custom algorithm parameters dataclass.
66
76
 
67
- error_callback: ... # Callable[[Exception], None]
68
- # Callback to run on exceptions.
69
-
70
77
  logger: ... # type: logging.Logger
71
78
  # Custom logger to use.
72
79
 
@@ -82,6 +89,8 @@ Algorithm(
82
89
  ```python
83
90
  import logging
84
91
 
92
+ from ocean_runner import Algorithm, Config
93
+
85
94
 
86
95
  @dataclass
87
96
  class CustomInput:
@@ -91,19 +100,13 @@ class CustomInput:
91
100
  logger = logging.getLogger(__name__)
92
101
 
93
102
 
94
- Algorithm(
103
+ algorithm = Algorithm(
95
104
  Config(
96
105
  custom_input: CustomInput,
97
106
  """
98
107
  Load the Algorithm's Custom Input into a CustomInput dataclass instance.
99
108
  """
100
109
 
101
- error_callback: lambda ex: logger.exception(ex),
102
- """
103
- Run this callback when an exception is caught
104
- NOTE: it's not recommended to catch exceptions this way. Should re-raise and halt the execution.
105
- """
106
-
107
110
  source_paths: [Path("/algorithm/src")],
108
111
  """
109
112
  Source paths to include in the PATH. '/algorithm/src' is the default since our templates place the algorithm source files there.
@@ -143,44 +146,72 @@ Algorithm(
143
146
 
144
147
  ```
145
148
 
146
- ## Default behaviours
149
+ #### Behaviour Config
147
150
 
148
- ### Default implementations
149
-
150
- As seen in the minimal example, all methods implemented in `Algorithm` have a default implementation which will be commented here.
151
+ To fully configure the behaviour of the algorithm as in the [Minimal Example](#minimal-example), you can do it decorating your defined function as in the following example, which features all the possible algorithm customization.
151
152
 
152
153
  ```python
154
+ from pathlib import Path
153
155
 
154
- (
155
- Algorithm()
156
-
157
- """
158
- Default constructor, will use default values of Config.
159
- """
160
-
161
- .validate()
162
-
163
- """
164
- Will validate the algorithm's job detail instance, checking for the existence of:
165
- - `job_details.ddos`
166
- - `job_details.files`
167
- """
156
+ import pandas as pd
157
+ from ocean_runner import Algorithm
168
158
 
169
- .run()
159
+ algorithm = Algorithm()
170
160
 
171
- """
172
- Has NO default implementation, must pass a callback that returns a result of any type.
173
- """
174
161
 
175
- .save_results()
162
+ @algorithm.on_error
163
+ def error_callback(ex: Exception):
164
+ algorithm.logger.exception(ex)
165
+ raise algorithm.Error() from ex
176
166
 
177
- """
178
- Stores the result of running the algorithm in "outputs/results.txt"
179
- """
180
167
 
181
- )
168
+ @algorithm.validate
169
+ def val():
170
+ assert algorithm.job_details.files, "Empty input dir"
171
+
172
+
173
+ @algorithm.run
174
+ def run() -> pd.DataFrame:
175
+ _, filename = next(algorithm.job_details.next_path())
176
+ return pd.read_csv(filename).describe(include="all")
177
+
178
+
179
+ @algorithm.save_results
180
+ def save(results: pd.DataFrame, path: Path):
181
+ algorithm.logger.info(f"Descriptive statistics: {results}")
182
+ results.to_csv(path / "results.csv")
183
+
184
+
185
+ if __name__ == "__main__":
186
+ algorithm()
187
+ ```
188
+
189
+
190
+
191
+ ### Default implementations
192
+
193
+ As seen in the minimal example, all methods implemented in `Algorithm` have a default implementation which will be commented here.
194
+
195
+ ```python
196
+ .validate()
197
+
198
+ """
199
+ Will validate the algorithm's job detail instance, checking for the existence of:
200
+ - `job_details.ddos`
201
+ - `job_details.files`
202
+ """
203
+
204
+ .run()
205
+
206
+ """
207
+ Has NO default implementation, must pass a callback that returns a result of any type.
208
+ """
182
209
 
210
+ .save_results()
183
211
 
212
+ """
213
+ Stores the result of running the algorithm in "outputs/results.txt"
214
+ """
184
215
  ```
185
216
 
186
217
  ### Job Details
@@ -188,7 +219,7 @@ As seen in the minimal example, all methods implemented in `Algorithm` have a de
188
219
  To load the OceanProtocol JobDetails instance, the program will read some environment variables, they can be mocked passing an instance of `Environment` through the configuration of the algorithm.
189
220
 
190
221
  Environment variables:
191
- - `DIDS` Input dataset(s) DID's, must have format: `["abc..90"]`
192
- - `TRANSFORMATION_DID` Algorithm DID, must have format: `abc..90`
193
- - `SECRET` Algorithm secret.
222
+ - `DIDS` (optional) Input dataset(s) DID's, must have format: `["abc..90"]`. Defaults to reading them automatically from the `DDO` data directory.
223
+ - `TRANSFORMATION_DID` (optional, default="DEFAULT"): Algorithm DID, must have format: `abc..90`.
224
+ - `SECRET` (optional, default="DEFAULT"): Algorithm secret.
194
225
  - `BASE_DIR` (optional, default="/data"): Base path to the OceanProtocol data directories.
@@ -0,0 +1,7 @@
1
+ ocean_runner/__init__.py,sha256=awAmE6kZhuwcrD3gT7qFZArdhiuzW-EFTA6tGKhw06k,138
2
+ ocean_runner/config.py,sha256=gyyUotPJ7n8wPPdsJZIBUT4zBlkoNbhV876JDTdPNsY,1398
3
+ ocean_runner/runner.py,sha256=0UbKFCN_tuhanACOJShiPkpOrg3tQhkohuH1QweAbgA,5726
4
+ ocean_runner-0.2.15.dist-info/METADATA,sha256=cr_MIftAYwQbcn3eZwaH_GeM6JdG7mxZLtI4vmTlnoc,6562
5
+ ocean_runner-0.2.15.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
6
+ ocean_runner-0.2.15.dist-info/licenses/LICENSE,sha256=_B25KqK4amoADWkMN150tnZFm_Fy7VvZpvIC8ZydWdI,1053
7
+ ocean_runner-0.2.15.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- ocean_runner/__init__.py,sha256=awAmE6kZhuwcrD3gT7qFZArdhiuzW-EFTA6tGKhw06k,138
2
- ocean_runner/config.py,sha256=EgloiUTLBvca4B_pr2eNa8kHYfsyVE73ZxszLCbiDYY,1525
3
- ocean_runner/runner.py,sha256=-nYH6_TtAEJ9RWv_PWmeDMyfJOhQ1tlrJrNHi7ZmYKo,5941
4
- ocean_runner-0.2.13.dist-info/METADATA,sha256=orDe6w9hnST2HI33Ta3rU2-kUg9ulFSq9mQhLOhuVxI,5919
5
- ocean_runner-0.2.13.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
6
- ocean_runner-0.2.13.dist-info/licenses/LICENSE,sha256=_B25KqK4amoADWkMN150tnZFm_Fy7VvZpvIC8ZydWdI,1053
7
- ocean_runner-0.2.13.dist-info/RECORD,,