Skip to content

Commit bea32df

Browse files
committed
Convert to CIVData on ComponentInterfaceValuePostSerializer
1 parent ca49960 commit bea32df

7 files changed

Lines changed: 108 additions & 151 deletions

File tree

app/grandchallenge/algorithms/serializers.py

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from grandchallenge.components.backends.exceptions import (
2626
CIVNotEditableException,
2727
)
28-
from grandchallenge.components.models import CIVData
2928
from grandchallenge.components.serializers import (
3029
ComponentInterfaceSerializer,
3130
ComponentInterfaceValuePostSerializer,
@@ -236,11 +235,10 @@ def validate(self, data):
236235
"please try again after they have completed"
237236
)
238237

239-
inputs = data.pop("inputs")
238+
self.inputs = data.pop("inputs")
240239
data["algorithm_interface"] = (
241-
self.validate_inputs_and_return_matching_interface(inputs=inputs)
240+
self.validate_inputs_and_return_matching_interface()
242241
)
243-
self.inputs = self.reformat_inputs(serialized_civs=inputs)
244242

245243
if Job.objects.get_jobs_with_same_inputs(
246244
inputs=self.inputs,
@@ -284,12 +282,12 @@ def create(self, validated_data):
284282

285283
return job
286284

287-
def validate_inputs_and_return_matching_interface(self, *, inputs):
285+
def validate_inputs_and_return_matching_interface(self):
288286
"""
289287
Validates that the provided inputs match one of the configured interfaces of
290288
the algorithm and returns that AlgorithmInterface
291289
"""
292-
provided_inputs = {i["interface"] for i in inputs}
290+
provided_inputs = {civ_data.socket for civ_data in self.inputs}
293291
annotated_qs = annotate_input_output_counts(
294292
self._algorithm.interfaces, inputs=provided_inputs
295293
)
@@ -306,42 +304,3 @@ def validate_inputs_and_return_matching_interface(self, *, inputs):
306304
f"following input combinations: "
307305
f"{oxford_comma([f'Interface {n}: {oxford_comma(interface.inputs.all())}' for n, interface in enumerate(self._algorithm.interfaces.all(), start=1)])}"
308306
)
309-
310-
@staticmethod
311-
def reformat_inputs(*, serialized_civs):
312-
"""Takes serialized CIV data and returns list of CIVData objects."""
313-
possible_keys = [
314-
"image",
315-
"value",
316-
"file",
317-
"user_upload",
318-
"upload_session",
319-
]
320-
321-
data = []
322-
for civ in serialized_civs:
323-
found_keys = [
324-
key for key in possible_keys if civ.get(key) is not None
325-
]
326-
327-
if not found_keys:
328-
raise serializers.ValidationError(
329-
f"You must provide at least one of {possible_keys}"
330-
)
331-
332-
if len(found_keys) > 1:
333-
raise serializers.ValidationError(
334-
f"You can only provide one of {possible_keys} for each interface."
335-
)
336-
337-
try:
338-
data.append(
339-
CIVData(
340-
interface_slug=civ["interface"].slug,
341-
value=civ[found_keys[0]],
342-
)
343-
)
344-
except ValidationError as e:
345-
raise serializers.ValidationError(e)
346-
347-
return data

app/grandchallenge/components/forms.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,9 @@ def clean(self):
149149
for key, value in cleaned_data.items():
150150
if key.startswith(INTERFACE_FORM_FIELD_PREFIX):
151151
keys_to_remove.append(key)
152-
inputs.append(
153-
CIVData(
154-
interface_slug=key[len(INTERFACE_FORM_FIELD_PREFIX) :],
155-
value=value,
156-
)
157-
)
152+
interface_slug = key[len(INTERFACE_FORM_FIELD_PREFIX) :]
153+
socket = ComponentInterface.objects.get(slug=interface_slug)
154+
inputs.append(CIVData(socket=socket, value=value))
158155

159156
for key in keys_to_remove:
160157
cleaned_data.pop(key)
@@ -265,12 +262,9 @@ def process_object_data(self):
265262
civ_data_objects = []
266263
for key, value in self.cleaned_data.items():
267264
if key.startswith(INTERFACE_FORM_FIELD_PREFIX):
268-
civ_data_objects.append(
269-
CIVData(
270-
interface_slug=key[len(INTERFACE_FORM_FIELD_PREFIX) :],
271-
value=value,
272-
)
273-
)
265+
interface_slug = key[len(INTERFACE_FORM_FIELD_PREFIX) :]
266+
socket = ComponentInterface.objects.get(slug=interface_slug)
267+
civ_data_objects.append(CIVData(socket=socket, value=value))
274268

275269
try:
276270
self.instance.validate_civ_data_objects_and_execute_linked_task(

app/grandchallenge/components/models.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,7 +1559,7 @@ def retrieve_existing_civs(*, civ_data_objects):
15591559
elif civ_data.image:
15601560
try:
15611561
civs = ComponentInterfaceValue.objects.filter(
1562-
interface__slug=civ_data.interface_slug,
1562+
interface=civ_data.socket,
15631563
image=civ_data.image,
15641564
).all()
15651565
existing_civs.extend(civs)
@@ -1571,7 +1571,7 @@ def retrieve_existing_civs(*, civ_data_objects):
15711571
# values can be of different types, including None and False
15721572
try:
15731573
civs = ComponentInterfaceValue.objects.filter(
1574-
interface__slug=civ_data.interface_slug,
1574+
interface=civ_data.socket,
15751575
value=civ_data.value,
15761576
).all()
15771577
existing_civs.extend(civs)
@@ -2263,8 +2263,8 @@ def update_size_in_storage(self):
22632263
class CIVData:
22642264

22652265
@property
2266-
def interface_slug(self):
2267-
return self._interface_slug
2266+
def socket(self):
2267+
return self._socket
22682268

22692269
@property
22702270
def value(self):
@@ -2295,13 +2295,15 @@ def dicom_upload_with_name(self):
22952295
return self._dicom_upload_with_name
22962296

22972297
def __repr__(self):
2298-
return f"CIVData(interface_slug={self._interface_slug!r}, value={self._initial_value!r})"
2298+
return (
2299+
f"CIVData(socket={self._socket!r}, value={self._initial_value!r})"
2300+
)
22992301

23002302
def __str__(self):
23012303
return f"CIVData: {self.__dict__}"
23022304

2303-
def __init__(self, *, interface_slug, value):
2304-
self._interface_slug = interface_slug
2305+
def __init__(self, *, socket, value):
2306+
self._socket = socket
23052307
self._initial_value = value
23062308
self._json_value = None
23072309
self._image = None
@@ -2311,17 +2313,15 @@ def __init__(self, *, interface_slug, value):
23112313
self._file_civ = None
23122314
self._dicom_upload_with_name = None
23132315

2314-
ci = ComponentInterface.objects.get(slug=interface_slug)
2315-
2316-
if ci.super_kind == ci.SuperKind.VALUE:
2316+
if socket.super_kind == socket.SuperKind.VALUE:
23172317
self._init_json_civ_data()
2318-
elif ci.super_kind == ci.SuperKind.IMAGE:
2318+
elif socket.super_kind == socket.SuperKind.IMAGE:
23192319
self._init_image_civ_data()
2320-
elif ci.super_kind == ci.SuperKind.FILE:
2320+
elif socket.super_kind == socket.SuperKind.FILE:
23212321
self._init_file_civ_data()
23222322
else:
23232323
raise NotImplementedError(
2324-
f"Unknown interface super kind: {ci.super_kind}"
2324+
f"Unknown interface super kind: {socket.super_kind}"
23252325
)
23262326

23272327
self.validate()
@@ -2334,7 +2334,7 @@ def _init_json_civ_data(self):
23342334
self._json_value = self._initial_value
23352335
else:
23362336
raise ValidationError(
2337-
f"Unknown data type {type(self._initial_value)} for interface {self._interface_slug}"
2337+
f"Unknown data type {type(self._initial_value)} for interface {self._socket}"
23382338
)
23392339

23402340
def _init_image_civ_data(self):
@@ -2352,7 +2352,7 @@ def _init_image_civ_data(self):
23522352
self._image = None
23532353
else:
23542354
raise ValidationError(
2355-
f"Unknown data type {type(self._initial_value)} for interface {self._interface_slug}"
2355+
f"Unknown data type {type(self._initial_value)} for interface {self._socket}"
23562356
)
23572357

23582358
def _init_file_civ_data(self):
@@ -2364,7 +2364,7 @@ def _init_file_civ_data(self):
23642364
self._file_civ = None
23652365
else:
23662366
raise ValidationError(
2367-
f"Unknown data type {type(self._initial_value)} for interface {self._interface_slug}"
2367+
f"Unknown data type {type(self._initial_value)} for interface {self._socket}"
23682368
)
23692369

23702370
def validate(self):
@@ -2464,17 +2464,17 @@ def _update_civ(self, *, civ_data, user=None, linked_task=None):
24642464

24652465
try:
24662466
if (
2467-
civ_data.interface_slug
2467+
civ_data.socket.slug
24682468
not in self.base_object.allowed_socket_slugs
24692469
):
24702470
raise CINotAllowedException(
2471-
f"Socket {civ_data.interface_slug!r} is not allowed "
2471+
f"Socket {civ_data.socket!r} is not allowed "
24722472
f"for this {self.base_object._meta.model_name}."
24732473
)
24742474
except AttributeError:
24752475
pass
24762476

2477-
ci = ComponentInterface.objects.get(slug=civ_data.interface_slug)
2477+
ci = civ_data.socket
24782478
current_civ = self.get_current_value_for_interface(
24792479
interface=ci, user=user
24802480
)

0 commit comments

Comments
 (0)