Skip to content

Instantly share code, notes, and snippets.

@robsyme
Last active April 29, 2025 14:11
Show Gist options
  • Save robsyme/ec3d862c0f0d2af5713e5e7fb63782af to your computer and use it in GitHub Desktop.
Save robsyme/ec3d862c0f0d2af5713e5e7fb63782af to your computer and use it in GitHub Desktop.
nextflow.preview.topic = true
workflow {
def statham = new groovy.json.JsonSlurper()
Channel.topic('model_updates')
.tap { updates }
.map { path -> statham.parseText(path.text) }
.until { obj -> obj?.score > 100 }
.tap { limit }
.last()
.view { model -> "Final model: $model" }
Setup()
Model(updates, limit)
}
process Model {
publishDir 'results', mode: 'copy', saveAs: { "out.iteration_${task.index}.json" }
input:
path("start.json")
val(limiter)
output:
path("updated.json"), topic: model_updates
script:
"""
#!/usr/bin/env python
import json
import random
# Read in updated.json
with open('start.json') as f:
data = json.load(f)
# Add a new step object
new_step = {
"step": data['step'] + 1,
"score": data['score'] + random.randint(1, 10)
}
# Pretty print the updated data to updated.json
with open('updated.json', 'w') as f:
json.dump(new_step, f)
"""
}
process Setup {
output: path("start.json"), topic: model_updates
script:
"""
echo '{ "step": 0, "score": 0 }' > start.json
"""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment