Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use example values for JSON interfaces in examples #65

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions grand_challenge_forge/generation_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import json
import os
import shutil
import uuid
from datetime import datetime, timezone
from pathlib import Path

import black
from jinja2 import FileSystemLoader, StrictUndefined, TemplateNotFound
from jinja2.sandbox import ImmutableSandboxedEnvironment

Expand All @@ -27,20 +29,36 @@ def is_file(component_interface):
] == "File" and not component_interface["relative_path"].endswith(".json")


def has_example_value(component_interface):
return (
"example_value" in component_interface
and component_interface["example_value"] is not None
)


def create_civ_stub_file(*, target_path, component_interface):
"""Creates a stub based on a component interface"""
target_path.parent.mkdir(parents=True, exist_ok=True)

if has_example_value(component_interface):
target_path.write_text(
json.dumps(
component_interface["example_value"],
indent=4,
)
)
return

# Copy over an example
if is_json(component_interface):
src = RESOURCES_PATH / "example.json"
shutil.copy(RESOURCES_PATH / "example.json", target_path)
elif is_image(component_interface):
target_path = target_path / f"{str(uuid.uuid4())}.mha"
target_path.parent.mkdir(parents=True, exist_ok=True)
src = RESOURCES_PATH / "example.mha"
shutil.copy(RESOURCES_PATH / "example.mha", target_path)
else:
target_path.parent.mkdir(parents=True, exist_ok=True)
src = RESOURCES_PATH / "example.txt"

shutil.copy(src, target_path)
shutil.copy(RESOURCES_PATH / "example.txt", target_path)


def ci_to_civ(component_interface):
Expand All @@ -60,7 +78,9 @@ def ci_to_civ(component_interface):
f"{component_interface['relative_path']}"
)
if component_interface["super_kind"] == "Value":
civ["value"] = '{"some_key": "some_value"}'
civ["value"] = component_interface.get(
"example_value", {"some_key": "some_value"}
)
return {
**civ,
"interface": component_interface,
Expand Down Expand Up @@ -140,10 +160,25 @@ def copy_and_render(
# Copy non-template files
shutil.copy2(source_file, output_file)

apply_black(output_path)


def check_allowed_source(path):
if PARTIALS_PATH.resolve() not in path.resolve().parents:
raise PermissionError(
f"Only files under {PARTIALS_PATH} are allowed "
"to be copied or rendered"
)


def apply_black(target_path):
for python_file in target_path.glob("**/*.py"):
# Use direct black format call because black
# CLI entrypoint ignores files in .gitignore

black.format_file_in_place(
python_file,
fast=False,
mode=black.Mode(),
write_back=black.WriteBack.YES,
)
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ def run():
# For now, let us make bogus predictions
{%- for ci in algorithm.outputs %}
output_{{ ci.slug.replace("-", "_")}} =
{%- if ci | is_image %} numpy.eye(4, 2)
{%- if ci | has_example_value %} {{ ci.example_value }}
{%- elif ci | is_image %} numpy.eye(4, 2)
{%- elif ci | is_json %} {"content": "should match the required format"}
{%- elif ci | is_file %} "content: should match the required format"
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def run():
# For now, let us make bogus predictions
{%- for ci in phase.algorithm_outputs %}
output_{{ ci.slug.replace("-", "_")}} =
{%- if ci | is_image %} numpy.eye(4, 2)
{%- if ci | has_example_value %} {{ ci.example_value }}
{%- elif ci | is_image %} numpy.eye(4, 2)
{%- elif ci | is_json %} {"content": "should match the required format"}
{%- elif ci | is_file %} "content: should match the required format"
{% endif %}
Expand Down
5 changes: 5 additions & 0 deletions grand_challenge_forge/partials/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ def is_file(arg):
@register_simple_filter
def has_file(arg):
return any(generation_utils.is_file(item) for item in arg)


@register_simple_filter
def has_example_value(arg):
return generation_utils.has_example_value(arg)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ python = ">3.10"
jinja2 = "*"
click = "*"
jsonschema = "*"
black = "23.9.1"

[tool.poetry.dev-dependencies]
pytest = "*"
Expand Down
54 changes: 36 additions & 18 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,51 +38,59 @@
"slug": "input-ci-slug",
"kind": "Segmentation",
"super_kind": "Image",
"relative_path": "images/input-value"
"relative_path": "images/input-value",
"example_value": None
},
{
"slug": "another-input-ci-slug",
"kind": "Anything",
"super_kind": "File",
"relative_path": "another-input-value.json"
"relative_path": "another-input-value.json",
"example_value": {"key": "value"}
},
{
"slug": "yet-another-input-ci-slug",
"kind": "Anything",
"super_kind": "Value",
"relative_path": "yet-another-input-value.json"
"relative_path": "yet-another-input-value.json",
"example_value": {"key": "value"}
},
{
"slug": "yet-another-non-json-input-ci-slug",
"kind": "Anything",
"super_kind": "File",
"relative_path": "yet-another-non-json-input-value"
"relative_path": "yet-another-non-json-input-value",
"example_value": None
}
],
"algorithm_outputs": [
{
"slug": "output-ci-slug",
"kind": "Image",
"super_kind": "Image",
"relative_path": "images/output-value"
"relative_path": "images/output-value",
"example_value": None
},
{
"slug": "another-output-ci-slug",
"kind": "Anything",
"super_kind": "File",
"relative_path": "output-value.json"
"relative_path": "output-value.json",
"example_value": {"key": "value"}
},
{
"slug": "yet-another-output-ci-slug",
"kind": "Anything",
"super_kind": "Value",
"relative_path": "yet-another-output-value.json"
"relative_path": "yet-another-output-value.json",
"example_value": {"key": "value"}
},
{
"slug": "yet-another-non-json-output-ci-slug",
"kind": "Anything",
"super_kind": "File",
"relative_path": "yet-another-non-json-output-value"
"relative_path": "yet-another-non-json-output-value",
"example_value": None
}
]
},
Expand All @@ -97,15 +105,17 @@
"slug": "input-ci-slug",
"kind": "Image",
"super_kind": "Image",
"relative_path": "images/input-value"
"relative_path": "images/input-value",
"example_value": None
}
],
"algorithm_outputs": [
{
"slug": "another-output-ci-slug",
"kind": "Anything",
"super_kind": "File",
"relative_path": "output-value.json"
"relative_path": "output-value.json",
"example_value": {"key": "value"}
}
]
}
Expand All @@ -124,51 +134,59 @@
"slug": "input-ci-slug",
"kind": "Segmentation",
"super_kind": "Image",
"relative_path": "images/input-value"
"relative_path": "images/input-value",
"example_value": None
},
{
"slug": "another-input-ci-slug",
"kind": "Anything",
"super_kind": "File",
"relative_path": "another-input-value.json"
"relative_path": "another-input-value.json",
"example_value": {"key": "value"}
},
{
"slug": "yet-another-input-ci-slug",
"kind": "Anything",
"super_kind": "Value",
"relative_path": "yet-another-input-value.json"
"relative_path": "yet-another-input-value.json",
"example_value": {"key": "value"}
},
{
"slug": "yet-another-non-json-input-ci-slug",
"kind": "Anything",
"super_kind": "File",
"relative_path": "yet-another-non-json-input-value"
"relative_path": "yet-another-non-json-input-value",
"example_value": None
}
],
"outputs": [
{
"slug": "output-ci-slug",
"kind": "Image",
"super_kind": "Image",
"relative_path": "images/output-value"
"relative_path": "images/output-value",
"example_value": None
},
{
"slug": "another-output-ci-slug",
"kind": "Anything",
"super_kind": "File",
"relative_path": "output-value.json"
"relative_path": "output-value.json",
"example_value": {"key": "value"}
},
{
"slug": "yet-another-output-ci-slug",
"kind": "Anything",
"super_kind": "Value",
"relative_path": "yet-another-output-value.json"
"relative_path": "yet-another-output-value.json",
"example_value": {"key": "value"}
},
{
"slug": "yet-another-non-json-output-ci-slug",
"kind": "Anything",
"super_kind": "File",
"relative_path": "yet-another-non-json-output-value"
"relative_path": "yet-another-non-json-output-value",
"example_value": None
}
]
}
Expand Down
Loading