Skip to content

Commit d2adf15

Browse files
committed
WIP: Add support for more builder parameters
* conda - for conda-forge packages * pypi - for PyPI packages * python - for Python version
1 parent 87ff6fb commit d2adf15

2 files changed

Lines changed: 69 additions & 7 deletions

File tree

‎README.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,29 @@ the SciJava Common wiki.
1919

2020
See also:
2121
* [Python Scripting](https://imagej.net/scripting/python) on the ImageJ wiki.
22+
23+
## Example
24+
25+
```python
26+
#@script (language="appose-python", pypi=["cellcast"])
27+
28+
#@ Img image
29+
30+
#@ Double (value=1.0, description="minimum percentile value for normalization") pmin
31+
#@ Double (value=99.8, description="maximum percentile value for normalization") pmax
32+
#@ Double (value=0.479, description="Polygon probability threshold") prob_threshold
33+
#@ Double (value=0.3, description="Non-Maximum Suppression threshold") nms_threshold
34+
#@ Boolean (value=true, description="Set True for GPU inference via WebGPU, False for CPU inference") gpu
35+
36+
#@output Img labels
37+
38+
import cellcast.models.stardist_2d as stardist
39+
labels = stardist.predict_versatile_fluo(
40+
image,
41+
pmin,
42+
pmax,
43+
prob_threshold,
44+
nms_threshold,
45+
gpu,
46+
)
47+
```

‎src/main/java/org/scijava/plugins/scripting/appose/python/ApposePythonScriptEngine.java‎

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,18 +237,49 @@ private Environment buildEnvironment(final ScriptInfo info)
237237
throws ScriptException
238238
{
239239
if (info == null) return null;
240-
final String envRef = info.get("env");
241-
if (envRef == null) return null;
242240

243-
final File envFile = resolveEnvFile(envRef, info.getPath());
241+
final String envValue = info.get("env");
242+
final File envFile = envValue == null ? null : resolveEnvFile(envValue, info.getPath());
244243
final String envName = sanitizeEnvName(info.getPath());
245244

245+
final String scheme = info.get("scheme");
246+
final List<String> pypi = toListOfStrings(info.get("pypi"));
247+
final List<String> conda = toListOfStrings(info.get("conda"));
248+
final String python = info.get("python");
249+
246250
log.info("Building Appose environment: " + envName);
247-
try {
248-
String scheme = info.get("scheme");
249-
Builder<?> builder = scheme == null ?
250-
Appose.file(envFile) : Appose.file(envFile).scheme(scheme);
251251

252+
try {
253+
final Builder<?> builder;
254+
if (envFile == null) {
255+
// No environment file given -- use inline builder.
256+
if (conda.isEmpty()) {
257+
// No conda packages required -- use uv.
258+
String[] pypiPkgs = pypi.toArray(new String[0]);
259+
builder = python == null ?
260+
Appose.uv().include(pypiPkgs) :
261+
Appose.uv().python(python).include(pypiPkgs);
262+
}
263+
else {
264+
// Conda packages specified -- use pixi.
265+
List<String> condaPkgList = new ArrayList<>();
266+
if (conda.stream().anyMatch(v -> v.matches("python\\b.*"))) {
267+
// Python was not included in the conda package list -- add it manually.
268+
// If python version value was given, use that, otherwise open-ended.
269+
condaPkgList.add(python != null ? "python=" + python : "python");
270+
}
271+
condaPkgList.addAll(conda);
272+
String[] condaPkgs = condaPkgList.toArray(new String[0]);
273+
String[] pypiPkgs = pypi.toArray(new String[0]);
274+
builder = Appose.pixi().conda(condaPkgs).pypi(pypiPkgs);
275+
}
276+
}
277+
else {
278+
// Build with the given environment file.
279+
builder = scheme == null ?
280+
Appose.file(envFile) :
281+
Appose.file(envFile).scheme(scheme);
282+
}
252283
return builder.name(envName).build();
253284
}
254285
catch (final BuildException e) {
@@ -259,6 +290,11 @@ private Environment buildEnvironment(final ScriptInfo info)
259290
}
260291
}
261292

293+
private List<String> toListOfStrings(String value) {
294+
// TODO Auto-generated method stub
295+
throw new UnsupportedOperationException("Unimplemented method 'toListOfStrings'");
296+
}
297+
262298
/** Resolves an (optionally relative) env file path against the script path. */
263299
private static File resolveEnvFile(final String envRef,
264300
final String scriptPath)

0 commit comments

Comments
 (0)