Skip to content

Commit

Permalink
Merge branch 'main' into dev-build-defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Dec 9, 2024
2 parents a3638d7 + 7fbadde commit a497e5d
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 40 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/python-wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ jobs:
run: |
pnpm run --aggregate-output --filter "@itk-wasm/${{ matrix.package }}-build" test:python:wasi
- uses: pyodide/pyodide-actions/install-browser@e3edcb8db4f1ec4604e402013f3ae2ee9f114441
if: ${{ matrix.python-minor-version > 10 }}
- uses: thewtex/pyodide-actions/install-browser@1e32e8a037a3e99a845dd7ebad6b057a40b7e2c0
if: ${{ matrix.python-minor-version > 11 && matrix.os == 'ubuntu-22.04' }}
with:
runner: selenium
browser: chrome
browser-version: latest

- name: Test python on chrome
if: ${{ matrix.python-minor-version > 10 && matrix.package != 'dicom' && matrix.os == 'ubuntu-22.04' || (matrix.package != 'mesh-io' && matrix.package != 'image-io' && matrix.package != 'dicom' && matrix.os != 'macos-14' )}}
if: ${{ matrix.python-minor-version > 11 && matrix.package != 'dicom' && matrix.os == 'ubuntu-22.04' }}
run: |
pnpm run --aggregate-output --filter "@itk-wasm/${{ matrix.package }}-build" test:python:emscripten
2 changes: 1 addition & 1 deletion packages/core/typescript/itk-wasm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "itk-wasm",
"version": "1.0.0-b.182",
"version": "1.0.0-b.184",
"description": "High-performance spatial analysis in a web browser, Node.js, and reproducible execution across programming languages and hardware architectures.",
"type": "module",
"module": "./dist/index.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
function canonicalType(parameterType) {
// Strip extras
const canonical = parameterType.split(' ')[0]
let canonical = parameterType.split(' ')[0]
canonical = canonical.split(':')[0]
return canonical
}

export default canonicalType
export default canonicalType
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import interfaceJsonTypeToInterfaceType from '../interface-json-type-to-interfac
import canonicalType from '../canonical-type.js'

function functionModuleArgs(interfaceJson) {
let functionArgs = ""
let functionArgs = ''
interfaceJson['inputs'].forEach((value) => {
const canonical = canonicalType(value.type)
const pythonType = interfaceJsonTypeToPythonType.get(canonical)
functionArgs += ` ${snakeCase(value.name)}: ${pythonType},\n`
})
const outputFiles = interfaceJson.outputs.filter(o => { return o.type.includes('FILE') })
const outputFiles = interfaceJson.outputs.filter((o) => {
return o.type.includes('FILE')
})
outputFiles.forEach((output) => {
const isArray = output.itemsExpectedMax > 1
const optionName = `${output.name}`
Expand All @@ -22,37 +24,36 @@ function functionModuleArgs(interfaceJson) {
}
})
interfaceJson['parameters'].forEach((value) => {
if (value.name === "memory-io" || value.name === "version") {
if (value.name === 'memory-io' || value.name === 'version') {
return
}
const canonical = canonicalType(value.type)
const pythonType = interfaceJsonTypeToPythonType.get(canonical)
if (interfaceJsonTypeToInterfaceType.has(value.type)) {
if(value.required && value.itemsExpectedMax > 1) {
if (value.required && value.itemsExpectedMax > 1) {
functionArgs += ` ${snakeCase(value.name)}: List[${pythonType}] = [],\n`
} else {
functionArgs += ` ${snakeCase(value.name)}: Optional[${pythonType}] = None,\n`
}
} else {
if(value.itemsExpectedMax > 1) {
if (value.itemsExpectedMax > 1) {
if (value.required) {
functionArgs += ` ${snakeCase(value.name)}: List[${pythonType}]`
} else {
functionArgs += ` ${snakeCase(value.name)}: Optional[List[${pythonType}]]`
}
} else {
functionArgs += ` ${snakeCase(value.name)}: ${pythonType}`

}
if(value.type === "BOOL") {
if (value.type === 'BOOL') {
functionArgs += ` = False,\n`
} else if(value.type === "TEXT") {
} else if (value.type.startsWith('TEXT')) {
if (value.default) {
functionArgs += ` = "${value.default}",\n`
} else {
functionArgs += ` = "",\n`
}
} else if(value.required && value.itemsExpectedMax > 1) {
} else if (value.required && value.itemsExpectedMax > 1) {
functionArgs += ` = [],\n`
} else {
if (value.itemsExpectedMax > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ from itkwasm import (
const interfaceType = interfaceJsonTypeToInterfaceType.get(output.type)
const isArray = output.itemsExpectedMax > 1
switch (interfaceType) {
case "TextFile":
case "BinaryFile":
case 'TextFile':
case 'BinaryFile':
if (isArray) {
haveArray = true
pipelineOutputs += ` *${snakeCase(output.name)}_pipeline_outputs,\n`
Expand Down Expand Up @@ -97,12 +97,12 @@ from itkwasm import (
if (interfaceJsonTypeToInterfaceType.has(input.type)) {
const interfaceType = interfaceJsonTypeToInterfaceType.get(input.type)
switch (interfaceType) {
case "TextFile":
case "BinaryFile":
case 'TextFile':
case 'BinaryFile':
pipelineInputs += ` PipelineInput(InterfaceTypes.${interfaceType}, ${interfaceType}(PurePosixPath(${snakeCase(input.name)}))),\n`
break
case "TextStream":
case "BinaryStream":
case 'TextStream':
case 'BinaryStream':
pipelineInputs += ` PipelineInput(InterfaceTypes.${interfaceType}, ${interfaceType}(${snakeCase(input.name)})),\n`
break
default:
Expand All @@ -113,7 +113,7 @@ from itkwasm import (

let args = ` args: List[str] = ['--memory-io',]\n`
let inputCount = 0
args += " # Inputs\n"
args += ' # Inputs\n'
interfaceJson.inputs.forEach((input) => {
const snakeName = snakeCase(input.name)
if (interfaceJsonTypeToInterfaceType.has(input.type)) {
Expand All @@ -122,7 +122,9 @@ from itkwasm import (
args += ` if not Path(${snakeName}).exists():\n`
args += ` raise FileNotFoundError("${snakeName} does not exist")\n`
}
const name = interfaceType.includes('File') ? `str(PurePosixPath(${snakeName}))` : `'${inputCount.toString()}'`
const name = interfaceType.includes('File')
? `str(PurePosixPath(${snakeName}))`
: `'${inputCount.toString()}'`
args += ` args.append(${name})\n`
inputCount++
} else {
Expand All @@ -131,7 +133,7 @@ from itkwasm import (
})

let outputCount = 0
args += " # Outputs\n"
args += ' # Outputs\n'
interfaceJson.outputs.forEach((output) => {
const snake = snakeCase(output.name)
if (interfaceJsonTypeToInterfaceType.has(output.type)) {
Expand All @@ -158,15 +160,15 @@ from itkwasm import (
}
})

args += " # Options\n"
args += ' # Options\n'
args += ` input_count = len(pipeline_inputs)\n`
interfaceJson.parameters.forEach((parameter) => {
if (parameter.name === 'memory-io' || parameter.name === 'version') {
// Internal
return
}
const snake = snakeCase(parameter.name)
if (parameter.type === "BOOL") {
if (parameter.type === 'BOOL') {
args += ` if ${snake}:\n`
args += ` args.append('--${parameter.name}')\n`
} else if (parameter.itemsExpectedMax > 1) {
Expand All @@ -177,7 +179,9 @@ from itkwasm import (
args += ` args.append('--${parameter.name}')\n`
args += ` for value in ${snake}:\n`
if (interfaceJsonTypeToInterfaceType.has(parameter.type)) {
const interfaceType = interfaceJsonTypeToInterfaceType.get(parameter.type)
const interfaceType = interfaceJsonTypeToInterfaceType.get(
parameter.type
)
if (interfaceType.includes('File')) {
// for files
args += ` input_file = str(PurePosixPath(value))\n`
Expand All @@ -195,12 +199,19 @@ from itkwasm import (
args += ` input_count += 1\n`
}
} else {
if (parameter.type.startsWith('TEXT:{')) {
const choices = parameter.type.split('{')[1].split('}')[0].split(',')
args += ` if ${snake} not in (${choices.map((c) => `'${c}'`).join(',')}):\n`
args += ` raise ValueError(f'${snake} must be one of ${choices.join(', ')}')\n`
}
args += ` args.append(str(value))\n`
}
} else {
if (interfaceJsonTypeToInterfaceType.has(parameter.type)) {
args += ` if ${snake} is not None:\n`
const interfaceType = interfaceJsonTypeToInterfaceType.get(parameter.type)
const interfaceType = interfaceJsonTypeToInterfaceType.get(
parameter.type
)
if (interfaceType.includes('File')) {
// for files
args += ` input_file = str(PurePosixPath(${snakeCase(parameter.name)}))\n`
Expand All @@ -222,6 +233,11 @@ from itkwasm import (
}
} else {
args += ` if ${snake}:\n`
if (parameter.type.startsWith('TEXT:{')) {
const choices = parameter.type.split('{')[1].split('}')[0].split(',')
args += ` if ${snake} not in (${choices.map((c) => `'${c}'`).join(',')}):\n`
args += ` raise ValueError(f'${snake} must be one of ${choices.join(', ')}')\n`
}
args += ` args.append('--${parameter.name}')\n`
args += ` args.append(str(${snake}))\n`
}
Expand All @@ -234,34 +250,36 @@ from itkwasm import (
const canonical = canonicalType(type)
const pythonType = interfaceJsonTypeToPythonType.get(canonical)
switch (pythonType) {
case "os.PathLike":
case 'os.PathLike':
return `Path(${value}.data.path)`
case "str":
case 'str':
if (type === 'TEXT') {
return `${value}`
} else {
return `${value}.data.data`
}
case "bytes":
case 'bytes':
return `${value}.data.data`
case "int":
case 'int':
return `int(${value})`
case "bool":
case 'bool':
return `bool(${value})`
case "float":
case 'float':
return `float(${value})`
case "Any":
case 'Any':
return `${value}.data`
default:
return `${value}.data`
}
}
outputCount = 0
const jsonOutputs = interfaceJson['outputs']
const numOutputs = interfaceJson.outputs.filter(o => !o.type.includes('FILE')).length
const numOutputs = interfaceJson.outputs.filter(
(o) => !o.type.includes('FILE')
).length
if (numOutputs > 1) {
postOutput += ' result = (\n'
} else if (numOutputs === 1){
} else if (numOutputs === 1) {
postOutput = ' result = '
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,13 @@ function functionModule(
functionContent += ' args.push(inputCountString)\n\n'
}
} else {
functionContent += ' args.push(value.toString())\n\n'
if (parameter.type.startsWith('TEXT:{')) {
const choices = parameter.type.split('{')[1].split('}')[0].split(',')
functionContent += ` if (![${choices.map((c) => `'${c}'`).join(', ')}].includes(options.${camel})) {\n`
functionContent += ` throw new Error('"${parameter.name}" option must be one of ${choices.join(', ')}')\n`
functionContent += ' }\n'
}
functionContent += ' args.push(value.toString())\n'
}
functionContent += forNode ? ' })\n' : ' }))\n'
} else {
Expand Down Expand Up @@ -518,6 +524,12 @@ function functionModule(
functionContent += ` args.push('--${parameter.name}', inputCountString)\n\n`
}
} else {
if (parameter.type.startsWith('TEXT:{')) {
const choices = parameter.type.split('{')[1].split('}')[0].split(',')
functionContent += ` if (![${choices.map((c) => `'${c}'`).join(', ')}].includes(options.${camel})) {\n`
functionContent += ` throw new Error('"${parameter.name}" option must be one of ${choices.join(', ')}')\n`
functionContent += ' }\n'
}
functionContent += ` args.push('--${parameter.name}', options.${camel}.toString())\n\n`
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"itk-wasm": "1.0.0-b.177"
"itk-wasm": "1.0.0-b.183"
},
"devDependencies": {
"@itk-wasm/demo-app": "^0.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/typescript/itk-wasm/src/version.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const version = '1.0.0-b.182'
const version = '1.0.0-b.184'

export default version
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ int main( int argc, char * argv[] )
itk::wasm::InputTextStream inputJson;
pipeline.add_option("input-json", inputJson, "The input json")->type_name("INPUT_JSON");

std::string stringChoice = "valuea";
pipeline.add_option("--string-choice", stringChoice, "A string choice, one of: valuea, valueb, or valuec")->check(CLI::IsMember({"valuea", "valueb", "valuec"}));

itk::wasm::OutputTextStream outputJson;
pipeline.add_option("output-json", outputJson, "The output json")->type_name("OUTPUT_JSON");

Expand Down

0 comments on commit a497e5d

Please sign in to comment.