Skip to content
Snippets Groups Projects
Commit 4041da48 authored by Yannick DAYER's avatar Yannick DAYER
Browse files

Merge branch 'delayed-sample-modification' into 'master'

DelayedSample tweak

See merge request !98
parents a3e67c42 6852ad45
No related branches found
No related tags found
1 merge request!98DelayedSample tweak
Pipeline #63242 failed
......@@ -137,6 +137,14 @@ class DelayedSample(Sample):
self._delayed_attributes = parent_attr.copy()
if delayed_attributes is not None:
# Sanity check, `delayed_attributes` can not be present in `kwargs`
# as well
for name, attr in delayed_attributes.items():
if name in kwargs:
raise ValueError(
"`{}` can not be in both `delayed_attributes` and "
"`kwargs` inputs".format(name)
)
if self._delayed_attributes is None:
self._delayed_attributes = delayed_attributes.copy()
else:
......@@ -155,7 +163,17 @@ class DelayedSample(Sample):
# Create the delayed attributes, but leave their values as None for now.
if self._delayed_attributes is not None:
kwargs.update({k: None for k in self._delayed_attributes})
update = {}
for k in list(self._delayed_attributes):
if k not in kwargs:
update[k] = None
else:
# k is not a delay_attribute anymore
del self._delayed_attributes[k]
if len(self._delayed_attributes) == 0:
self._delayed_attributes = None
kwargs.update(update)
# kwargs.update({k: None for k in self._delayed_attributes})
# Set attribute from kwargs
_copy_attributes(self, None, kwargs)
self._load = load
......
......@@ -5,6 +5,7 @@ import pickle
import tempfile
import numpy as np
import pytest
from bob.pipelines import (
DelayedSample,
......@@ -124,15 +125,15 @@ def test_delayed_samples():
delayed_sample.annot = "changed"
assert delayed_sample.annot == "changed"
# test DelayedSample.from_smaple
# Test DelayedSample.from_sample
# check if a non-delayed sample is correctly converted to a delayed sample
# Check if a non-delayed sample is correctly converted to a delayed sample
non_delayed_sample = Sample(1)
delayed_sample = DelayedSample.from_sample(non_delayed_sample, annot="test")
assert delayed_sample.data == 1
assert delayed_sample.annot == "test"
# check if converting a delayed sample will not load the data and delayed attributes
# Check if converting a delayed sample will not load the data and delayed attributes
raise_error = True
def never_load():
......@@ -144,9 +145,30 @@ def test_delayed_samples():
delayed_sample = DelayedSample(
never_load, delayed_attributes=dict(annot=never_load)
)
# should not raise an error when creating the delayed sample
# Should not raise an error when creating the delayed sample
child_sample = DelayedSample.from_sample(delayed_sample)
raise_error = False
assert child_sample.data == 0
assert child_sample.annot == 0
# If attribute is in kwargs and delayed_sample, it should raise an exception
with pytest.raises(ValueError):
DelayedSample(
load_data,
delayed_attributes={"annotations": load_annot},
annotations="some_other_annotations",
)
# kwargs should take precedence over the parent's delayed_attributes
parent = DelayedSample(
load_data,
delayed_attributes={"annotation": load_annot},
non_delay_attribute=4,
)
# annotation=100 overwrites parent.annotation
child = DelayedSample(lambda: 12, parent=parent, annotation=100)
assert child.data == 12
assert child._delayed_attributes is None
assert child.annotation == 100
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment