Skip to content

Commit

Permalink
feat(bindgen): Check that input image files exists in Python/WASI
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed May 26, 2023
1 parent 7bea0b8 commit 866f7a7
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ def apply_presentation_state_to_image(

args: List[str] = ['--memory-io',]
# Inputs
if not Path(image_in).exists():
raise FileNotFoundError("image_in does not exist")
args.append(str(PurePosixPath(image_in)))
if not Path(presentation_state_file).exists():
raise FileNotFoundError("presentation_state_file does not exist")
args.append(str(PurePosixPath(presentation_state_file)))
# Outputs
args.append('0')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def read_dicom_encapsulated_pdf(

args: List[str] = ['--memory-io',]
# Inputs
if not Path(dicom_file).exists():
raise FileNotFoundError("dicom_file does not exist")
args.append(str(PurePosixPath(dicom_file)))
# Outputs
args.append('0')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ def structured_report_to_html(

args: List[str] = ['--memory-io',]
# Inputs
if not Path(dicom_file).exists():
raise FileNotFoundError("dicom_file does not exist")
args.append(str(PurePosixPath(dicom_file)))
# Outputs
args.append('0')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ def structured_report_to_text(

args: List[str] = ['--memory-io',]
# Inputs
if not Path(dicom_file).exists():
raise FileNotFoundError("dicom_file does not exist")
args.append(str(PurePosixPath(dicom_file)))
# Outputs
args.append('0')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pathlib import Path
from pathlib import Path
import numpy as np
import json

Expand Down Expand Up @@ -43,3 +43,18 @@ def test_apply_presentation_state_to_dicom_image():
# slice to get only the pixel buffer from the baseline image (pgm file)
baseline_pixels = baseline_buffer[15:]
assert np.array_equal(np.frombuffer(baseline_pixels, dtype=np.uint8), output_image.data.ravel())

def test_apply_presentation_state_to_dicom_image_input_does_not_exist():
from itkwasm_dicom_wasi import apply_presentation_state_to_image

input_file = 'gsps-pstate-test-input-image-does-not-exist.dcm'
input_file_path = Path('..', '..', 'test', 'data', 'input', input_file)

p_state_file = 'gsps-pstate-test-input-pstate.dcm'
p_state_file_path = Path('..', '..', 'test', 'data', 'input', p_state_file)

try:
p_state_json_out, output_image = apply_presentation_state_to_image(input_file_path, p_state_file_path)
assert False
except FileNotFoundError:
pass
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated file. Do not edit.

import os
from typing import Dict, Tuple, Optional
from typing import Dict, Tuple, Optional, List

from itkwasm import (
environment_dispatch,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated file. Do not edit.

import os
from typing import Dict, Tuple, Optional
from typing import Dict, Tuple, Optional, List

from itkwasm import (
environment_dispatch,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated file. Do not edit.

import os
from typing import Dict, Tuple, Optional
from typing import Dict, Tuple, Optional, List

from itkwasm import (
environment_dispatch,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated file. Do not edit.

import os
from typing import Dict, Tuple, Optional
from typing import Dict, Tuple, Optional, List

from itkwasm import (
environment_dispatch,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated file. Do not edit.

import os
from typing import Dict, Tuple, Optional
from typing import Dict, Tuple, Optional, List

from itkwasm import (
environment_dispatch,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated file. Do not edit.

import os
from typing import Dict, Tuple, Optional
from typing import Dict, Tuple, Optional, List

from itkwasm import (
environment_dispatch,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated file. Do not edit.

import os
from typing import Dict, Tuple, Optional
from typing import Dict, Tuple, Optional, List

from itkwasm import (
environment_dispatch,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated file. Do not edit.

import os
from typing import Dict, Tuple, Optional
from typing import Dict, Tuple, Optional, List

from itkwasm import (
environment_dispatch,
Expand Down
10 changes: 7 additions & 3 deletions src/bindgen/python.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,18 @@ from itkwasm import (
let inputCount = 0
args += " # Inputs\n"
interfaceJson.inputs.forEach((input) => {
const snakeName = snakeCase(input.name)
if (interfaceJsonTypeToInterfaceType.has(input.type)) {
const interfaceType = interfaceJsonTypeToInterfaceType.get(input.type)
const name = interfaceType.includes('File') ? `str(PurePosixPath(${snakeCase(input.name)}))` : `'${inputCount.toString()}'`
if (interfaceType.includes('File')) {
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()}'`
args += ` args.append(${name})\n`
inputCount++
} else {
const snake = snakeCase(input.name)
args += ` args.append(str(${snake}))\n`
args += ` args.append(str(${snakeName}))\n`
}
})

Expand Down

0 comments on commit 866f7a7

Please sign in to comment.