Skip to content

Commit

Permalink
Fix #1095. Auto titles now handle computations with multiple outputs.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmeyer committed Jul 11, 2024
1 parent 980ed57 commit 47e5a56
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
43 changes: 35 additions & 8 deletions nion/swift/model/DocumentModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1382,8 +1382,14 @@ def __computation_needs_update(self, computation: Symbolic.Computation) -> None:
self.__computation_queue.append(computation)
self.dispatch_task(self.__recompute)

def __establish_computation_dependencies(self, computation_title: str, old_inputs: typing.Set[Persistence.PersistentObject], new_inputs: typing.Set[Persistence.PersistentObject], old_outputs: typing.Set[Persistence.PersistentObject], new_outputs: typing.Set[Persistence.PersistentObject]) -> None:
def __establish_computation_dependencies(self, computation: Symbolic.Computation) -> None:
# establish dependencies between input and output items.
old_inputs = computation._inputs
old_outputs = computation._outputs
input_items = computation.input_items
new_inputs = set(input_items)
output_items = computation.output_items
new_outputs = set(output_items)
with self.__dependency_tree_lock:
removed_inputs = old_inputs - new_inputs
added_inputs = new_inputs - old_inputs
Expand Down Expand Up @@ -1419,10 +1425,10 @@ def __establish_computation_dependencies(self, computation_title: str, old_input
for output in added_outputs:
for input in same_inputs:
self.__add_dependency(input, output)
if isinstance(output, DataItem.DataItem):
output.computation_title_changed(computation_title)
if removed_inputs or added_inputs or removed_outputs or added_outputs:
self.__transaction_manager._rebuild_transactions()
computation._inputs = new_inputs
computation._outputs = new_outputs

# live state, and dependencies

Expand Down Expand Up @@ -2158,11 +2164,32 @@ def __computation_update_dependencies(self, computation: Symbolic.Computation) -
# when a computation output is changed, this function is called to establish dependencies.
# if other parts of the computation are changed (inputs, values, etc.), the __computation_changed
# will handle the change (and trigger a new computation).
input_items = set(computation.input_items)
output_items = set(computation.output_items)
self.__establish_computation_dependencies(computation.label or _("Processed"), computation._inputs, input_items, computation._outputs, output_items)
computation._inputs = input_items
computation._outputs = output_items
self.__establish_computation_dependencies(computation)

# update the output titles

data_item_outputs = {output for output in computation._outputs if isinstance(output, DataItem.DataItem)}
has_multiple_data_item_outputs = len(data_item_outputs) > 1

base_computation_title = computation.label or _("Processed")

for result in computation.results:
if result_name := result.name:
output = computation.get_output(result_name)
if isinstance(output, DataItem.DataItem):
computation_title = base_computation_title
if has_multiple_data_item_outputs:
computation_title += " - " + result.label
output.computation_title_changed(computation_title)
data_item_outputs.remove(output)

# update any output titles that were missed due to missing result name

for index, output in enumerate(data_item_outputs):
computation_title = base_computation_title
if has_multiple_data_item_outputs:
computation_title += " - " + str(index)
output.computation_title_changed(computation_title)

def __make_computation(self, processing_id: str,
inputs: typing.List[typing.Tuple[DisplayItem.DisplayItem, typing.Optional[DataItem.DataItem], typing.Optional[Graphics.Graphic]]],
Expand Down
41 changes: 41 additions & 0 deletions nion/swift/test/DataItem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import math
import threading
import time
import typing
import unittest
import weakref

Expand All @@ -29,6 +30,7 @@
from nion.swift.model import Utility
from nion.swift.test import TestContext
from nion.ui import TestUI
from nion.utils import Geometry
from nion.utils import Recorder


Expand Down Expand Up @@ -1504,6 +1506,45 @@ def test_computed_data_item_title(self):
data_item.title = "source2"
self.assertEqual("source2 (Negate)", data_item2.title)

class GenerateTwoOutputs:
computation_id = "gen2outputs"
label = "2Out"
inputs = {"src": {"label": "Input Data Item"}}
outputs = {"a": {"label": "A"}, "b": {"label": "B"}}

def __init__(self, computation: Facade.Computation, **kwargs: typing.Any) -> None:
self.computation = computation

def execute(self) -> None:
pass

def commit(self) -> None:
self.computation.set_referenced_xdata('a', DataAndMetadata.new_data_and_metadata(numpy.random.randn(4,4)))
self.computation.set_referenced_xdata('b', DataAndMetadata.new_data_and_metadata(numpy.random.randn(4,4)))

def test_computed_data_item_title_with_multiple_outputs(self):
# requirement: dynamic_titles
Symbolic.register_computation_type("gen2outputs", self.GenerateTwoOutputs)
with TestContext.create_memory_context() as test_context:
document_model = test_context.create_document_model()
data_item = DataItem.DataItem()
data_item.title = "source"
document_model.append_data_item(data_item)
data_item_a = DataItem.DataItem()
document_model.append_data_item(data_item_a)
data_item_b = DataItem.DataItem()
document_model.append_data_item(data_item_b)

computation = document_model.create_computation()
computation.create_input_item("src", Symbolic.make_item(data_item))
computation.create_output_item("a", Symbolic.make_item(data_item_a))
computation.create_output_item("b", Symbolic.make_item(data_item_b))
computation.processing_id = "gen2outputs"
document_model.append_computation(computation)

self.assertEqual("source (2Out - A)", data_item_a.title)
self.assertEqual("source (2Out - B)", data_item_b.title)

def test_setting_dynamic_title_immediately_updates_title_stream(self):
# requirement: dynamic_titles
with TestContext.create_memory_context() as test_context:
Expand Down

0 comments on commit 47e5a56

Please sign in to comment.