def test_add_event(self): from jicbioimage.core.image import History history = History() def null(): return None args = ["arg1", "arg2"] kwargs = dict(kwarg1="kwarg1", kwarg2="kwarg2") event = history.add_event(function=null, args=args, kwargs=kwargs) self.assertEqual(len(history), 1) self.assertTrue(isinstance(event, History.Event)) self.assertEqual(event.function, null) self.assertEqual(event.args, args) self.assertEqual(event.kwargs, kwargs)
def test_add_event(self): from jicbioimage.core.image import History history = History() def null(): return None args = ["arg1", "arg2"] kwargs = dict(kwarg1="kwarg1", kwarg2="kwarg2") event = history.add_event(function=null, args=args, kwargs=kwargs) self.assertEqual(len(history), 1) self.assertTrue(isinstance(event, History.Event)) self.assertEqual(event.function, null) self.assertEqual(event.args, args) self.assertEqual(event.kwargs, kwargs)
def func_as_transformation(*args, **kwargs): # Take copies of the args and kwargs for use in the history. # We will need to remove the image from either the kwargs # or the args before we use h_args and h_kwargs to create a # history event. h_args = list(args[:]) h_kwargs = kwargs.copy() # Get the input image, so that we can get the history from it. # Also, strip the image for h_args/h_kwargs. input_image = kwargs.get("image", None) if input_image is None: # The image is the first item of args. input_image = args[0] h_args.pop(0) else: # The image is in kwargs. h_kwargs.pop("image") def array_to_str(value): if isinstance(value, np.ndarray): value = repr(value) return value h_args = [array_to_str(v) for v in h_args] for key, value in h_kwargs.items(): h_kwargs[key] = array_to_str(value) # Get the history from the image. history = History() if hasattr(input_image, "history"): history.extend(input_image.history) image = func(*args, **kwargs) if not isinstance(image, _BaseImageWithHistory): image = Image.from_array(image, log_in_history=False) # Update the history of the image. image.history = history image.history.add_event(func, h_args, h_kwargs) if AutoWrite.on: fpath = AutoName.name(func) image.write(fpath) return image
def test_event_repr_no_args_and_no_kwargs(self): from jicbioimage.core.image import History def split(s, sep, maxsplit): return s.split(sep, maxsplit) args = [] kwargs = {} event = History.Event(split, args, kwargs) self.assertEqual(repr(event), "<History.Event(split(image))>")
def test_event_repr(self): from jicbioimage.core.image import History def split(s, sep, maxsplit): return s.split(sep, maxsplit) args = [","] kwargs = {"maxsplit": 1} event = History.Event(split, args, kwargs) self.assertEqual(repr(event), "<History.Event(split(image, ',', maxsplit=1))>")
def test_initialisation(self): from jicbioimage.core.image import History def null(): return None args = ["arg1", "arg2"] kwargs = dict(kwarg1="kwarg1", kwarg2="kwarg2") event = History.Event(function=null, args=args, kwargs=kwargs) self.assertTrue(isinstance(event, History.Event)) self.assertEqual(event.function, null) self.assertEqual(event.args, args) self.assertEqual(event.kwargs, kwargs)
def test_initialisation_with_creation(self): from jicbioimage.core.image import History history = History("awesome") self.assertEqual(history.creation, "awesome")
def test_initialisation(self): from jicbioimage.core.image import History history = History() self.assertTrue(isinstance(history, History)) self.assertEqual(len(history), 0) self.assertTrue(history.creation is None)