scout-rig 0.2.6 → 0.2.7

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.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/.vimproject +31 -13
  3. data/README.md +27 -4
  4. data/Rakefile +1 -0
  5. data/VERSION +1 -1
  6. data/doc/Improvements.md +396 -0
  7. data/doc/StartHere.md +95 -0
  8. data/doc/developer/Architecture.md +232 -0
  9. data/doc/developer/DataConversionInternals.md +407 -0
  10. data/doc/developer/DesignPrinciples.md +254 -0
  11. data/doc/developer/PythonTaskRunnerInternals.md +371 -0
  12. data/doc/developer/PythonWorkflowInternals.md +539 -0
  13. data/doc/developer/ScoutPythonInternals.md +504 -0
  14. data/doc/user/Cookbook.md +337 -0
  15. data/doc/user/DefiningPythonTasks.md +708 -0
  16. data/doc/user/DrivingWorkflowsFromPython.md +418 -0
  17. data/doc/user/IteratingPythonData.md +142 -0
  18. data/doc/user/PassingDataToPython.md +223 -0
  19. data/doc/user/PythonEnvironments.md +197 -0
  20. data/doc/user/RunningPythonFromRuby.md +267 -0
  21. data/doc/user/ScriptingPython.md +281 -0
  22. data/doc/user/TestingAndPackaging.md +487 -0
  23. data/lib/scout/python/script.rb +11 -2
  24. data/lib/scout/workflow/python/inputs.rb +33 -23
  25. data/lib/scout/workflow/python/task.rb +219 -22
  26. data/lib/scout/workflow/python.rb +236 -9
  27. data/lib/scout-rig/testing/availability.rb +608 -0
  28. data/python/README.md +51 -0
  29. data/python/scout/__init__.py +2 -0
  30. data/python/scout/runner.py +193 -29
  31. data/python/scout/task_errors.py +6 -0
  32. data/research/00_scope_and_themes.md +131 -0
  33. data/research/data-conversion-and-scripting-analysis.md +209 -0
  34. data/research/design-philosophy-analysis.md +246 -0
  35. data/research/python-helper-package-analysis.md +272 -0
  36. data/research/pythonworkflow-analysis.md +199 -0
  37. data/research/scoutpython-bridge-analysis.md +185 -0
  38. data/research/synthesis-report.md +109 -0
  39. data/research/testing-availability-baseline.md +1625 -0
  40. data/research/testing-availability-final-report.md +500 -0
  41. data/research/validation-report.md +149 -0
  42. data/scout-rig.gemspec +35 -5
  43. data/test/scout/python/test_util.rb +13 -0
  44. data/test/scout/test_python.rb +10 -0
  45. data/test/scout/workflow/python/test_task.rb +1119 -1
  46. data/test/scout-rig/testing/test_availability.rb +495 -0
  47. data/test/scout-rig/testing/test_test_support.rb +166 -0
  48. data/test/test_helper.rb +96 -19
  49. metadata +47 -4
  50. data/doc/Python.md +0 -482
  51. data/doc/PythonWorkflow.md +0 -163
@@ -0,0 +1,337 @@
1
+ # Cookbook
2
+
3
+ Practical recipes for common scout-rig use cases. Each recipe is
4
+ self-contained and can be adapted to your needs.
5
+
6
+ ## Audience
7
+
8
+ This page is intended for:
9
+
10
+ - ✓ Workflow authors looking for ready-to-use patterns
11
+ - ✓ Agent developers needing concrete examples
12
+ - ✗ Framework contributors
13
+
14
+ ## Ruby → Python recipes
15
+
16
+ ### Run a numpy computation and get the result back
17
+
18
+ ```ruby
19
+ result = ScoutPython.run :numpy, as: :np do
20
+ a = np.array([1, 2, 3, 4, 5])
21
+ a.mean()
22
+ end
23
+ puts result # => 3.0
24
+ ```
25
+
26
+ ### Run pandas on a TSV and return a modified TSV
27
+
28
+ ```ruby
29
+ tsv = TSV.setup([], "Gene~LogFC,PValue#:type=:list")
30
+ tsv["BRCA1"] = [2.5, 0.001]
31
+ tsv["TP53"] = [-1.8, 0.003]
32
+ tsv["EGFR"] = [0.5, 0.45]
33
+
34
+ df = ScoutPython.tsv2df(tsv)
35
+
36
+ filtered_df = ScoutPython.run "pandas", as: :pd do
37
+ df = pd.DataFrame.new(tsv.values)
38
+ df["LogFC"].abs() > 1
39
+ end
40
+
41
+ filtered_tsv = ScoutPython.df2tsv(filtered_df)
42
+ ```
43
+
44
+ ### Pass a TSV to a Python script and get a computed result
45
+
46
+ ```ruby
47
+ tsv = TSV.setup([], "Gene~LogFC,PValue#:type=:list")
48
+ tsv["BRCA1"] = [2.5, 0.001]
49
+
50
+ result = ScoutPython.script <<~PY, df: tsv
51
+ result = len(df)
52
+ PY
53
+ puts result # => 1
54
+ ```
55
+
56
+ ### Import a specific class from a module
57
+
58
+ ```ruby
59
+ ScoutPython.run do
60
+ Linear = ScoutPython.get_class "torch.nn", "Linear"
61
+ layer = Linear.new(10, 5)
62
+ end
63
+ ```
64
+
65
+ ### Iterate a numpy array with a progress bar
66
+
67
+ ```ruby
68
+ ScoutPython.run :numpy, as: :np do
69
+ arr = np.arange(1000)
70
+ ScoutPython.iterate(arr, bar: "Processing") do |val|
71
+ # Process val
72
+ end
73
+ end
74
+ ```
75
+
76
+ ## Python task recipes
77
+
78
+ ### Basic Python task
79
+
80
+ ```python
81
+ # python/tasks/greet.py
82
+ import scout
83
+
84
+ def hello(name: str, excited: bool = False) -> str:
85
+ """
86
+ Greet a person.
87
+
88
+ Args:
89
+ name: The name of the person to greet.
90
+ excited: Whether to add an exclamation mark.
91
+ """
92
+ return f"Hello, {name}{'!' if excited else ''}"
93
+
94
+ scout.task(hello)
95
+ ```
96
+
97
+ ```ruby
98
+ # Workflow
99
+ module GreetWF
100
+ extend Workflow
101
+ extend PythonWorkflow
102
+ self.name = 'Greet'
103
+
104
+ python_task :hello
105
+ end
106
+
107
+ job = GreetWF.job(:hello, name: "World")
108
+ puts job.run
109
+ ```
110
+
111
+ ### Task accepting a file input
112
+
113
+ ```python
114
+ # python/tasks/count_lines.py
115
+ import scout
116
+ from pathlib import Path
117
+
118
+ def count_lines(file: Path) -> int:
119
+ """Count the number of lines in a file.
120
+
121
+ Args:
122
+ file: Path to the file to count.
123
+ """
124
+ with open(file) as f:
125
+ return sum(1 for _ in f)
126
+
127
+ scout.task(count_lines)
128
+ ```
129
+
130
+ ```ruby
131
+ module FileWF
132
+ extend Workflow
133
+ extend PythonWorkflow
134
+ self.name = 'FileTools'
135
+
136
+ python_task :count_lines
137
+ end
138
+
139
+ job = FileWF.job(:count_lines, file: "data.txt")
140
+ puts job.run
141
+ ```
142
+
143
+ ### Task accepting a list input
144
+
145
+ ```python
146
+ # python/tasks/join_strings.py
147
+ import scout
148
+
149
+ def join_strings(items: list[str], separator: str = ", ") -> str:
150
+ """
151
+ Join a list of strings.
152
+
153
+ Args:
154
+ items: The strings to join.
155
+ separator: The separator to use.
156
+ """
157
+ return separator.join(items)
158
+
159
+ scout.task(join_strings)
160
+ ```
161
+
162
+ ```ruby
163
+ module JoinWF
164
+ extend Workflow
165
+ extend PythonWorkflow
166
+ self.name = 'Joiner'
167
+
168
+ python_task :join_strings
169
+ end
170
+
171
+ job = JoinWF.job(:join_strings, items: ["a", "b", "c"])
172
+ puts job.run # => "a, b, c"
173
+ ```
174
+
175
+ ### Multi-function Python file
176
+
177
+ ```python
178
+ # python/tasks/math_ops.py
179
+ import scout
180
+
181
+ def add(a: int, b: int) -> int:
182
+ """Add two numbers."""
183
+ return a + b
184
+
185
+ def multiply(a: float, b: float) -> float:
186
+ """Multiply two numbers."""
187
+ return a * b
188
+
189
+ scout.task(add)
190
+ scout.task(multiply)
191
+ ```
192
+
193
+ ```ruby
194
+ module MathWF
195
+ extend Workflow
196
+ extend PythonWorkflow
197
+ self.name = 'Math'
198
+
199
+ python_task :math_ops # Creates both :add and :multiply
200
+ end
201
+
202
+ puts MathWF.job(:add, a: 3, b: 4).run # => 7
203
+ puts MathWF.job(:multiply, a: 3.0, b: 4.0).run # => 12.0
204
+ ```
205
+
206
+ ### Loading Python tasks from a directory
207
+
208
+ ```ruby
209
+ module ToolsWF
210
+ extend Workflow
211
+ extend PythonWorkflow
212
+ self.name = 'Tools'
213
+
214
+ # Load all .py files from a directory
215
+ load_python_tasks
216
+ end
217
+ ```
218
+
219
+ ## Python → Ruby recipes
220
+
221
+ ### Running a Scout workflow from Python
222
+
223
+ ```python
224
+ import scout
225
+ import scout.workflow as sw
226
+
227
+ wf = sw.Workflow('Baking')
228
+ print(wf.tasks())
229
+
230
+ # Synchronous
231
+ result = wf.run('bake_muffin_tray', add_blueberries=True)
232
+
233
+ # Asynchronous
234
+ step = wf.fork('bake_muffin_tray', add_blueberries=True)
235
+ step.join()
236
+ print(step.load())
237
+ ```
238
+
239
+ ### Reading a Scout TSV from Python
240
+
241
+ ```python
242
+ import scout
243
+
244
+ df = scout.tsv('gene_expression.tsv')
245
+ print(df.shape)
246
+ print(df.head())
247
+ ```
248
+
249
+ ### Running a remote Scout workflow from Python
250
+
251
+ The remote client is `python/scout/workflow/remote.py`, but `scout.workflow`
252
+ is a module, not a package, so the ordinary import path is broken at HEAD:
253
+
254
+ ```python
255
+ # `from scout.workflow.remote import RemoteWorkflow` fails:
256
+ # ModuleNotFoundError: No module named 'scout.workflow.remote';
257
+ # 'scout.workflow' is not a package
258
+ import importlib.util
259
+ spec = importlib.util.spec_from_file_location(
260
+ 'scout.workflow.remote', 'python/scout/workflow/remote.py')
261
+ remote = importlib.util.module_from_spec(spec)
262
+ spec.loader.exec_module(remote)
263
+
264
+ wf = remote.RemoteWorkflow('http://localhost:1900/Baking')
265
+ print(wf.tasks) # attribute, not a method
266
+
267
+ step = wf.job('bake_muffin_tray', add_blueberries=True)
268
+ step.wait()
269
+ print(step.json())
270
+ ```
271
+
272
+ ### Passing a DataFrame as workflow input
273
+
274
+ ```python
275
+ import scout
276
+ import scout.workflow as sw
277
+ import pandas as pd
278
+
279
+ df = pd.DataFrame({'Gene': ['BRCA1'], 'Score': [1.2]})
280
+ df = df.set_index('Gene')
281
+
282
+ wf = sw.Workflow('Analysis')
283
+ result = wf.run('process_genes', data=df)
284
+ print(result)
285
+ ```
286
+
287
+ ## Debugging recipes
288
+
289
+ ### Inspecting Python task metadata
290
+
291
+ ```bash
292
+ python python/tasks/greet.py --scout-metadata
293
+ ```
294
+
295
+ Output (formatted):
296
+ ```json
297
+ [{
298
+ "name": "hello",
299
+ "description": "Greet a person.",
300
+ "returns": "string",
301
+ "params": [
302
+ {"name": "name", "type": "string", "required": true, "default": null, "help": "The name of the person to greet."},
303
+ {"name": "exited", "type": "boolean", "scout": false, "default": false, "help": "Whether to add an exclamation mark."}
304
+ ]
305
+ }]
306
+ ```
307
+
308
+ ### Running a Python task from the CLI
309
+
310
+ ```bash
311
+ # Single function
312
+ python python/tasks/greet.py --name World --excited
313
+
314
+ # Multi-function file
315
+ python python/tasks/math_ops.py add --a 3 --b 4
316
+ python python/tasks/math_ops.py multiply --a 3.0 --b 4.0
317
+ ```
318
+
319
+ ### Verifying Python package import
320
+
321
+ ```bash
322
+ PYTHONPATH=python python -c "import scout; print(scout.__file__)"
323
+ ```
324
+
325
+ ## See also
326
+
327
+ - [Running Python from Ruby](RunningPythonFromRuby.md) — Execution modes.
328
+ - [Passing Data to Python](PassingDataToPython.md) — Data conversion.
329
+ - [Scripting Python](ScriptingPython.md) — Script facility.
330
+ - [Defining Python Tasks](DefiningPythonTasks.md) — Python task definition.
331
+ - [Driving Workflows from Python](DrivingWorkflowsFromPython.md) — Python-side usage.
332
+ - [Iterating Python Data](IteratingPythonData.md) — Traversing Python
333
+ iterables (the progress-bar recipes above).
334
+ - [Python Environments](PythonEnvironments.md) — Choosing the Python
335
+ environment, and when the subprocess route avoids native-library conflicts.
336
+ - [Testing and Packaging](TestingAndPackaging.md) — Building the gem and
337
+ the Python package.