ocean-runner 0.2.13__tar.gz → 0.2.15__tar.gz

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 ocean-runner might be problematic. Click here for more details.

@@ -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.
@@ -1,6 +1,6 @@
1
1
  # ocean-runner
2
2
 
3
- Ocean Runner is a package that brings a fluent API for APP creation and running in the scope of OceanProtocol.
3
+ Ocean Runner is a package that eases algorithm creation in the scope of OceanProtocol.
4
4
 
5
5
 
6
6
  ## Installation
@@ -17,33 +17,40 @@ uv add ocean-runner
17
17
 
18
18
  ```python
19
19
  import random
20
- from ocean_runner import Algorithm, Config
20
+ from ocean_runner import Algorithm
21
+
22
+ algorithm = Algorithm()
23
+
21
24
 
25
+ @algorithm.run
26
+ def run():
27
+ return random.randint()
22
28
 
23
- Algorithm().run(lambda _: random.randint()).save_results()
29
+
30
+ if __name__ == "__main__":
31
+ algorithm()
24
32
  ```
25
33
 
26
- 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:
34
+ This code snippet will:
27
35
 
28
- - Read the OceanProtocol JobDetails from the environment variables and use default file paths.
29
- - Generate a random integer.
30
- - Store the result in a "result.txt" file within the default outputs path.
36
+ - Read the OceanProtocol JobDetails from the environment variables and use default configuration file paths.
37
+ - Execute the run function.
38
+ - Execute the default saving function, storing the result in a "result.txt" file within the default outputs path.
31
39
 
32
40
  ### Tuning
33
41
 
34
42
  #### Application Config
35
43
 
36
- The application configuration can be tweaked by passing a Config instance to its' constructor.
44
+ The application configuration can be tweaked by passing a Config instance to its constructor.
37
45
 
38
46
  ```python
39
- Algorithm(
47
+ from ocean_runner import Algorithm, Config
48
+
49
+ algorithm = Algorithm(
40
50
  Config(
41
51
  custom_input: ... # dataclass
42
52
  # Custom algorithm parameters dataclass.
43
53
 
44
- error_callback: ... # Callable[[Exception], None]
45
- # Callback to run on exceptions.
46
-
47
54
  logger: ... # type: logging.Logger
48
55
  # Custom logger to use.
49
56
 
@@ -59,6 +66,8 @@ Algorithm(
59
66
  ```python
60
67
  import logging
61
68
 
69
+ from ocean_runner import Algorithm, Config
70
+
62
71
 
63
72
  @dataclass
64
73
  class CustomInput:
@@ -68,19 +77,13 @@ class CustomInput:
68
77
  logger = logging.getLogger(__name__)
69
78
 
70
79
 
71
- Algorithm(
80
+ algorithm = Algorithm(
72
81
  Config(
73
82
  custom_input: CustomInput,
74
83
  """
75
84
  Load the Algorithm's Custom Input into a CustomInput dataclass instance.
76
85
  """
77
86
 
78
- error_callback: lambda ex: logger.exception(ex),
79
- """
80
- Run this callback when an exception is caught
81
- NOTE: it's not recommended to catch exceptions this way. Should re-raise and halt the execution.
82
- """
83
-
84
87
  source_paths: [Path("/algorithm/src")],
85
88
  """
86
89
  Source paths to include in the PATH. '/algorithm/src' is the default since our templates place the algorithm source files there.
@@ -120,44 +123,72 @@ Algorithm(
120
123
 
121
124
  ```
122
125
 
123
- ## Default behaviours
126
+ #### Behaviour Config
124
127
 
125
- ### Default implementations
126
-
127
- As seen in the minimal example, all methods implemented in `Algorithm` have a default implementation which will be commented here.
128
+ 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.
128
129
 
129
130
  ```python
131
+ from pathlib import Path
130
132
 
131
- (
132
- Algorithm()
133
-
134
- """
135
- Default constructor, will use default values of Config.
136
- """
137
-
138
- .validate()
139
-
140
- """
141
- Will validate the algorithm's job detail instance, checking for the existence of:
142
- - `job_details.ddos`
143
- - `job_details.files`
144
- """
133
+ import pandas as pd
134
+ from ocean_runner import Algorithm
145
135
 
146
- .run()
136
+ algorithm = Algorithm()
147
137
 
148
- """
149
- Has NO default implementation, must pass a callback that returns a result of any type.
150
- """
151
138
 
152
- .save_results()
139
+ @algorithm.on_error
140
+ def error_callback(ex: Exception):
141
+ algorithm.logger.exception(ex)
142
+ raise algorithm.Error() from ex
153
143
 
154
- """
155
- Stores the result of running the algorithm in "outputs/results.txt"
156
- """
157
144
 
158
- )
145
+ @algorithm.validate
146
+ def val():
147
+ assert algorithm.job_details.files, "Empty input dir"
148
+
149
+
150
+ @algorithm.run
151
+ def run() -> pd.DataFrame:
152
+ _, filename = next(algorithm.job_details.next_path())
153
+ return pd.read_csv(filename).describe(include="all")
154
+
155
+
156
+ @algorithm.save_results
157
+ def save(results: pd.DataFrame, path: Path):
158
+ algorithm.logger.info(f"Descriptive statistics: {results}")
159
+ results.to_csv(path / "results.csv")
160
+
161
+
162
+ if __name__ == "__main__":
163
+ algorithm()
164
+ ```
165
+
166
+
167
+
168
+ ### Default implementations
169
+
170
+ As seen in the minimal example, all methods implemented in `Algorithm` have a default implementation which will be commented here.
171
+
172
+ ```python
173
+ .validate()
174
+
175
+ """
176
+ Will validate the algorithm's job detail instance, checking for the existence of:
177
+ - `job_details.ddos`
178
+ - `job_details.files`
179
+ """
180
+
181
+ .run()
182
+
183
+ """
184
+ Has NO default implementation, must pass a callback that returns a result of any type.
185
+ """
159
186
 
187
+ .save_results()
160
188
 
189
+ """
190
+ Stores the result of running the algorithm in "outputs/results.txt"
191
+ """
161
192
  ```
162
193
 
163
194
  ### Job Details
@@ -165,7 +196,7 @@ As seen in the minimal example, all methods implemented in `Algorithm` have a de
165
196
  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.
166
197
 
167
198
  Environment variables:
168
- - `DIDS` Input dataset(s) DID's, must have format: `["abc..90"]`
169
- - `TRANSFORMATION_DID` Algorithm DID, must have format: `abc..90`
170
- - `SECRET` Algorithm secret.
199
+ - `DIDS` (optional) Input dataset(s) DID's, must have format: `["abc..90"]`. Defaults to reading them automatically from the `DDO` data directory.
200
+ - `TRANSFORMATION_DID` (optional, default="DEFAULT"): Algorithm DID, must have format: `abc..90`.
201
+ - `SECRET` (optional, default="DEFAULT"): Algorithm secret.
171
202
  - `BASE_DIR` (optional, default="/data"): Base path to the OceanProtocol data directories.
@@ -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"""
@@ -0,0 +1,172 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import InitVar, asdict, dataclass, field
4
+ from logging import Logger
5
+ from pathlib import Path
6
+ from typing import Callable, Generic, TypeVar
7
+
8
+ from oceanprotocol_job_details import JobDetails
9
+
10
+ from ocean_runner.config import Config
11
+
12
+ JobDetailsT = TypeVar("JobDetailsT")
13
+ ResultT = TypeVar("ResultT")
14
+
15
+
16
+ def default_error_callback(_, e: Exception) -> None:
17
+ raise e
18
+
19
+
20
+ def default_validation(algorithm: Algorithm) -> None:
21
+ algorithm.logger.info("Validating input using default validation")
22
+ assert algorithm.job_details.ddos, "DDOs missing"
23
+ assert algorithm.job_details.files, "Files missing"
24
+
25
+
26
+ def default_save(*, result: ResultT, base: Path, algorithm: Algorithm) -> None:
27
+ algorithm.logger.info("Saving results using default save")
28
+ with open(base / "result.txt", "w+") as f:
29
+ f.write(str(result))
30
+
31
+
32
+ @dataclass
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
+ """
39
+
40
+ config: InitVar[Config | None] = None
41
+ logger: Logger = field(init=False)
42
+ _job_details: JobDetails[JobDetailsT] = field(init=False)
43
+ _result: ResultT | None = field(default=None, init=False)
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
+ )
54
+
55
+ def __post_init__(self, config: Config | None) -> None:
56
+ config: Config = config or Config()
57
+
58
+ # Configure logger
59
+ if config.logger:
60
+ self.logger = config.logger
61
+ else:
62
+ import logging
63
+
64
+ logging.basicConfig(
65
+ level=logging.DEBUG,
66
+ format="%(asctime)s | %(levelname)-8s | %(name)s | %(message)s",
67
+ datefmt="%Y-%m-%d %H:%M:%S",
68
+ )
69
+ self.logger = logging.getLogger("ocean_runner")
70
+
71
+ # Normalize base_dir
72
+ if isinstance(config.environment.base_dir, str):
73
+ config.environment.base_dir = Path(config.environment.base_dir)
74
+
75
+ # Extend sys.path for custom imports
76
+ if config.source_paths:
77
+ import sys
78
+
79
+ sys.path.extend([str(path.absolute()) for path in config.source_paths])
80
+ self.logger.debug(f"Added [{len(config.source_paths)}] entries to PATH")
81
+
82
+ # Load job details
83
+ self._job_details = JobDetails.load(
84
+ _type=config.custom_input,
85
+ base_dir=config.environment.base_dir,
86
+ dids=config.environment.dids,
87
+ transformation_did=config.environment.transformation_did,
88
+ secret=config.environment.secret,
89
+ )
90
+
91
+ self.logger.info("Loaded JobDetails")
92
+ self.logger.debug(asdict(self.job_details))
93
+
94
+ self.config = config
95
+
96
+ class Error(RuntimeError): ...
97
+
98
+ @property
99
+ def job_details(self) -> JobDetails:
100
+ if not self._job_details:
101
+ raise Algorithm.Error("JobDetails not initialized or missing")
102
+ return self._job_details
103
+
104
+ @property
105
+ def result(self) -> ResultT:
106
+ if self._result is None:
107
+ raise Algorithm.Error("Result missing, run the algorithm first")
108
+ return self._result
109
+
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."""
136
+ try:
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
+
168
+ except Exception as e:
169
+ self.logger.exception("Error during algorithm execution")
170
+ self._error_callback(e)
171
+
172
+ return self._result
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "ocean-runner"
3
- version = "0.2.13"
3
+ version = "0.2.15"
4
4
  description = "A fluent API for OceanProtocol algorithms"
5
5
  authors = [
6
6
  { name = "AgrospAI", email = "agrospai@udl.cat" },
@@ -1,178 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import inspect
4
- from dataclasses import InitVar, asdict, dataclass, field
5
- from logging import Logger
6
- from pathlib import Path
7
- from typing import Callable, Generic, Self, TypeVar
8
-
9
- from oceanprotocol_job_details import JobDetails
10
-
11
- from ocean_runner.config import Config
12
-
13
- JobDetailsT = TypeVar("JobDetailsT")
14
- ResultT = TypeVar("ResultT")
15
-
16
-
17
- def default_error_callback(_: Algorithm, e: Exception) -> None:
18
- raise e
19
-
20
-
21
- def default_validation(algorithm: Algorithm) -> None:
22
- algorithm.logger.info("Validating input using default validation")
23
- assert algorithm.job_details.ddos, "DDOs missing"
24
- assert algorithm.job_details.files, "Files missing"
25
-
26
-
27
- def default_save(*, result: ResultT, base: Path, algorithm: Algorithm) -> None:
28
- algorithm.logger.info("Saving results using default save")
29
- with open(base / "result.txt", "w+") as f:
30
- f.write(str(result))
31
-
32
-
33
- @dataclass
34
- class Algorithm(Generic[JobDetailsT, ResultT]):
35
-
36
- config: InitVar[Config | None] = None
37
- logger: Logger = field(init=False)
38
- _job_details: JobDetails[JobDetailsT] = field(init=False)
39
- _result: ResultT | None = field(default=None, init=False)
40
- error_callback = default_error_callback
41
-
42
- def __post_init__(self, config: Config | None) -> None:
43
- 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
-
56
- def _setup_logger(self, config: Config) -> None:
57
- if config.logger:
58
- self.logger = config.logger
59
- else:
60
- import logging
61
-
62
- logging.basicConfig(
63
- level=logging.DEBUG,
64
- format="%(asctime)s | %(levelname)-8s | %(name)s | %(message)s",
65
- datefmt="%Y-%m-%d %H:%M:%S",
66
- )
67
- self.logger = logging.getLogger("ocean_runner")
68
-
69
- def _load_job_details(self, config: Config) -> None:
70
- if isinstance(config.environment.base_dir, str):
71
- config.environment.base_dir = Path(config.environment.base_dir)
72
- if config.source_paths:
73
- import sys
74
-
75
- sys.path.extend([str(path.absolute()) for path in config.source_paths])
76
- self.logger.debug(f"Added [{len(config.source_paths)}] entries to PATH")
77
-
78
- self._job_details = JobDetails.load(
79
- _type=config.custom_input,
80
- base_dir=config.environment.base_dir,
81
- dids=config.environment.dids,
82
- transformation_did=config.environment.transformation_did,
83
- secret=config.environment.secret,
84
- )
85
- self.logger.info("Loaded JobDetails")
86
- self.logger.debug(asdict(self.job_details))
87
-
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
- # ---------------------
128
-
129
- @property
130
- def job_details(self) -> JobDetails:
131
- if not self._job_details:
132
- raise Algorithm.Error("JobDetails not initialized or missing")
133
- return self._job_details
134
-
135
- @property
136
- def result(self) -> ResultT:
137
- if self._result is None:
138
- raise Algorithm.Error("Result missing, run the algorithm first")
139
- return self._result
140
-
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...")
169
- try:
170
- callback(
171
- result=self.result,
172
- base=override_path or self.job_details.paths.outputs,
173
- algorithm=self,
174
- )
175
- except Exception as e:
176
- self.error_callback(e)
177
-
178
- class Error(RuntimeError): ...
File without changes
File without changes