Ejemplo n.º 1
0
 def test_tuple_binding_pads_to_index_if_necessary(self) -> None:
     # this allows the source to more easily go from None to a partialy tuple None -> (3, None) -> (3, 4)
     source = typing.cast(typing.Any, TupleModel())
     self.assertEqual(None, source.tuple)
     binding0 = Binding.TuplePropertyBinding(source, "tuple", 0)
     binding2 = Binding.TuplePropertyBinding(source, "tuple", 2)
     binding0.update_source("abc")
     self.assertEqual(("abc", ), source.tuple)
     binding2.update_source("ghi")
     self.assertEqual(("abc", None, "ghi"), source.tuple)
Ejemplo n.º 2
0
    def __connect_pick_graphic(self, src, target, pick_graphic, computation):
        def _update_collection_index(axis, value):
            libertem_metadata = copy.deepcopy(src.metadata.get('libertem-io'))
            if not libertem_metadata:
                return
            file_parameters = libertem_metadata['file_parameters']
            file_type = file_parameters.pop('type')
            current_index = libertem_metadata['display_slice']['start']
            current_index = np.unravel_index(current_index, target.data.shape)
            if value == current_index[axis]:
                return
            executor = Registry.get_component('libertem_executor')
            if not executor:
                return
            executor = executor.ensure_sync()
            ds = dataset.load(file_type, executor, **file_parameters)
            roi = np.zeros(ds.shape.nav, dtype=bool)
            if axis == 0:
                roi[value, current_index[1]] = True
                current_index = (value, current_index[1])
            else:
                roi[current_index[0], value] = True
                current_index = (current_index[0], value)
            result = UDFRunner(PickUDF()).run_for_dataset(ds,
                                                          executor,
                                                          roi=roi)
            result_array = np.squeeze(np.array(result['intensity']))
            new_metadata = copy.deepcopy(src.metadata)
            new_display_slice = np.ravel_multi_index(current_index,
                                                     target.data.shape)
            new_metadata['libertem-io']['display_slice'][
                'start'] = new_display_slice
            new_xdata = self.__api.create_data_and_metadata(
                result_array, metadata=new_metadata)
            src.set_data_and_metadata(new_xdata)

        computation.pick_graphic_binding_0 = Binding.TuplePropertyBinding(
            pick_graphic._graphic,
            'position',
            0,
            converter=FloatTupleToIntTupleConverter(target.data.shape[0], 0))
        computation.pick_graphic_binding_1 = Binding.TuplePropertyBinding(
            pick_graphic._graphic,
            'position',
            1,
            converter=FloatTupleToIntTupleConverter(target.data.shape[1], 1))
        computation.pick_graphic_binding_0.target_setter = functools.partial(
            _update_collection_index, 0)
        computation.pick_graphic_binding_1.target_setter = functools.partial(
            _update_collection_index, 1)
Ejemplo n.º 3
0
 def test_tuple_property_binding_refcount(self) -> None:
     binding = Binding.TuplePropertyBinding(Model.PropertyModel((1, 2, 3)),
                                            "value", 1)
     binding_ref = weakref.ref(binding)
     del binding
     self.assertIsNone(binding_ref())
Ejemplo n.º 4
0
    def __connect_pick_graphic(self,
                               src,
                               target,
                               pick_graphic,
                               computation,
                               do_wait=-1):
        def _update_collection_index(axis, value):
            if src.xdata.is_collection or src.xdata.is_sequence:
                display_item = self.__api.application._application.document_model.get_display_item_for_data_item(
                    src._data_item)
                collection_index = display_item.display_data_channel.collection_index
                if axis == 0:
                    if value != collection_index[0]:
                        display_item.display_data_channel.collection_index = (
                            value, collection_index[1], 0)
                else:
                    if value != collection_index[1]:
                        display_item.display_data_channel.collection_index = (
                            collection_index[0], value, 0)
            else:
                libertem_metadata = copy.deepcopy(
                    src.metadata.get('libertem-io'))
                if not libertem_metadata:
                    return
                file_parameters = libertem_metadata['file_parameters']
                file_type = file_parameters.pop('type')
                current_index = libertem_metadata['display_slice']['start']
                current_index = np.unravel_index(current_index,
                                                 target.data.shape)
                if value == current_index[axis]:
                    return
                executor = Registry.get_component('libertem_executor')
                if not executor:
                    return
                executor = executor.ensure_sync()
                ds = dataset.load(file_type, executor, **file_parameters)
                roi = np.zeros(ds.shape.nav, dtype=bool)
                if axis == 0:
                    roi[value, current_index[1]] = True
                    current_index = (value, current_index[1])
                else:
                    roi[current_index[0], value] = True
                    current_index = (current_index[0], value)
                result = UDFRunner(PickUDF()).run_for_dataset(ds,
                                                              executor,
                                                              roi=roi)
                result_array = np.squeeze(np.array(result['intensity']))
                new_metadata = copy.deepcopy(src.metadata)
                new_display_slice = np.ravel_multi_index(
                    current_index, target.data.shape)
                new_metadata['libertem-io']['display_slice'][
                    'start'] = new_display_slice
                new_xdata = self.__api.create_data_and_metadata(
                    result_array, metadata=new_metadata)
                src.set_data_and_metadata(new_xdata)

        if do_wait > 0:
            starttime = time.time()
            while target.data is None:
                if time.time() - starttime > do_wait:
                    break
                time.sleep(0.1)

        if target.data is None:
            return
        shape = target.data.shape
        computation.pick_graphic_binding_0 = Binding.TuplePropertyBinding(
            pick_graphic._graphic,
            'position',
            0,
            converter=FloatTupleToIntTupleConverter(shape[0], 0))
        computation.pick_graphic_binding_1 = Binding.TuplePropertyBinding(
            pick_graphic._graphic,
            'position',
            1,
            converter=FloatTupleToIntTupleConverter(shape[1], 1))
        computation.pick_graphic_binding_0.target_setter = functools.partial(
            _update_collection_index, 0)
        computation.pick_graphic_binding_1.target_setter = functools.partial(
            _update_collection_index, 1)

        def collection_index_changed(key):
            if src.xdata.is_collection:
                display_item = self.__api.application._application.document_model.get_display_item_for_data_item(
                    src._data_item)
                if key == 'collection_index':
                    collection_index = display_item.display_data_channel.collection_index
                    if int(pick_graphic.position[0] *
                           shape[0]) != collection_index[0]:
                        computation.pick_graphic_binding_0.update_source(
                            collection_index)
                    if int(pick_graphic.position[1] *
                           shape[1]) != collection_index[1]:
                        computation.pick_graphic_binding_1.update_source(
                            collection_index)

        if src.xdata.is_collection:
            display_item = self.__api.application._application.document_model.get_display_item_for_data_item(
                src._data_item)
            computation.collection_index_changed_event_listener = display_item.display_data_channel.property_changed_event.listen(
                collection_index_changed)